Bug Summary

File:3rdparty/sqlite3/sqlite3.c
Warning:line 80072, column 46
The left operand of '+' is a garbage value

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/2024-12-20-143410-95304-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.47.1. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite. To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library. (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file. Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19**
20** The content in this amalgamation comes from Fossil check-in
21** b95d11e958643b969c47a8e5857f3793b9e6.
22*/
23#define SQLITE_CORE1 1
24#define SQLITE_AMALGAMATION1 1
25#ifndef SQLITE_PRIVATEstatic
26# define SQLITE_PRIVATEstatic static
27#endif
28/************** Begin file sqliteInt.h ***************************************/
29/*
30** 2001 September 15
31**
32** The author disclaims copyright to this source code. In place of
33** a legal notice, here is a blessing:
34**
35** May you do good and not evil.
36** May you find forgiveness for yourself and forgive others.
37** May you share freely, never taking more than you give.
38**
39*************************************************************************
40** Internal interface definitions for SQLite.
41**
42*/
43#ifndef SQLITEINT_H
44#define SQLITEINT_H
45
46/* Special Comments:
47**
48** Some comments have special meaning to the tools that measure test
49** coverage:
50**
51** NO_TEST - The branches on this line are not
52** measured by branch coverage. This is
53** used on lines of code that actually
54** implement parts of coverage testing.
55**
56** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
57** and the correct answer is still obtained,
58** though perhaps more slowly.
59**
60** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
61** and the correct answer is still obtained,
62** though perhaps more slowly.
63**
64** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
65** that would be harmless and undetectable
66** if it did occur.
67**
68** In all cases, the special comment must be enclosed in the usual
69** slash-asterisk...asterisk-slash comment marks, with no spaces between the
70** asterisks and the comment text.
71*/
72
73/*
74** Make sure the Tcl calling convention macro is defined. This macro is
75** only used by test code and Tcl integration code.
76*/
77#ifndef SQLITE_TCLAPI
78# define SQLITE_TCLAPI
79#endif
80
81/*
82** Include the header file used to customize the compiler options for MSVC.
83** This should be done first so that it can successfully prevent spurious
84** compiler warnings due to subsequent content in this file and other files
85** that are included by this file.
86*/
87/************** Include msvc.h in the middle of sqliteInt.h ******************/
88/************** Begin file msvc.h ********************************************/
89/*
90** 2015 January 12
91**
92** The author disclaims copyright to this source code. In place of
93** a legal notice, here is a blessing:
94**
95** May you do good and not evil.
96** May you find forgiveness for yourself and forgive others.
97** May you share freely, never taking more than you give.
98**
99******************************************************************************
100**
101** This file contains code that is specific to MSVC.
102*/
103#ifndef SQLITE_MSVC_H
104#define SQLITE_MSVC_H
105
106#if defined(_MSC_VER)
107#pragma warning(disable : 4054)
108#pragma warning(disable : 4055)
109#pragma warning(disable : 4100)
110#pragma warning(disable : 4127)
111#pragma warning(disable : 4130)
112#pragma warning(disable : 4152)
113#pragma warning(disable : 4189)
114#pragma warning(disable : 4206)
115#pragma warning(disable : 4210)
116#pragma warning(disable : 4232)
117#pragma warning(disable : 4244)
118#pragma warning(disable : 4305)
119#pragma warning(disable : 4306)
120#pragma warning(disable : 4702)
121#pragma warning(disable : 4706)
122#endif /* defined(_MSC_VER) */
123
124#if defined(_MSC_VER) && !defined(_WIN64)
125#undef SQLITE_4_BYTE_ALIGNED_MALLOC
126#define SQLITE_4_BYTE_ALIGNED_MALLOC
127#endif /* defined(_MSC_VER) && !defined(_WIN64) */
128
129#if !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800
130#define HAVE_LOG2 0
131#endif /* !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800 */
132
133#endif /* SQLITE_MSVC_H */
134
135/************** End of msvc.h ************************************************/
136/************** Continuing where we left off in sqliteInt.h ******************/
137
138/*
139** Special setup for VxWorks
140*/
141/************** Include vxworks.h in the middle of sqliteInt.h ***************/
142/************** Begin file vxworks.h *****************************************/
143/*
144** 2015-03-02
145**
146** The author disclaims copyright to this source code. In place of
147** a legal notice, here is a blessing:
148**
149** May you do good and not evil.
150** May you find forgiveness for yourself and forgive others.
151** May you share freely, never taking more than you give.
152**
153******************************************************************************
154**
155** This file contains code that is specific to Wind River's VxWorks
156*/
157#if defined(__RTP__) || defined(_WRS_KERNEL)
158/* This is VxWorks. Set up things specially for that OS
159*/
160#include <vxWorks.h>
161#include <pthread.h> /* amalgamator: dontcache */
162#define OS_VXWORKS0 1
163#define SQLITE_OS_OTHER0 0
164#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
165#define SQLITE_OMIT_LOAD_EXTENSION1 1
166#define SQLITE_ENABLE_LOCKING_STYLE0 0
167#define HAVE_UTIME 1
168#else
169/* This is not VxWorks. */
170#define OS_VXWORKS0 0
171#define HAVE_FCHOWN1 1
172#define HAVE_READLINK1 1
173#define HAVE_LSTAT1 1
174#endif /* defined(_WRS_KERNEL) */
175
176/************** End of vxworks.h *********************************************/
177/************** Continuing where we left off in sqliteInt.h ******************/
178
179/*
180** These #defines should enable >2GB file support on POSIX if the
181** underlying operating system supports it. If the OS lacks
182** large file support, or if the OS is windows, these should be no-ops.
183**
184** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
185** system #includes. Hence, this block of code must be the very first
186** code in all source files.
187**
188** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
189** on the compiler command line. This is necessary if you are compiling
190** on a recent machine (ex: Red Hat 7.2) but you want your code to work
191** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
192** without this option, LFS is enable. But LFS does not exist in the kernel
193** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
194** portability you should omit LFS.
195**
196** The previous paragraph was written in 2005. (This paragraph is written
197** on 2008-11-28.) These days, all Linux kernels support large files, so
198** you should probably leave LFS enabled. But some embedded platforms might
199** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
200**
201** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
202*/
203#ifndef SQLITE_DISABLE_LFS
204# define _LARGE_FILE1 1
205# ifndef _FILE_OFFSET_BITS64
206# define _FILE_OFFSET_BITS64 64
207# endif
208# define _LARGEFILE_SOURCE1 1
209#endif
210
211/* The GCC_VERSION and MSVC_VERSION macros are used to
212** conditionally include optimizations for each of these compilers. A
213** value of 0 means that compiler is not being used. The
214** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
215** optimizations, and hence set all compiler macros to 0
216**
217** There was once also a CLANG_VERSION macro. However, we learn that the
218** version numbers in clang are for "marketing" only and are inconsistent
219** and unreliable. Fortunately, all versions of clang also recognize the
220** gcc version numbers and have reasonable settings for gcc version numbers,
221** so the GCC_VERSION macro will be set to a correct non-zero value even
222** when compiling with clang.
223*/
224#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
225# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
226#else
227# define GCC_VERSION(4*1000000+2*1000+1) 0
228#endif
229#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
230# define MSVC_VERSION0 _MSC_VER
231#else
232# define MSVC_VERSION0 0
233#endif
234
235/*
236** Some C99 functions in "math.h" are only present for MSVC when its version
237** is associated with Visual Studio 2013 or higher.
238*/
239#ifndef SQLITE_HAVE_C99_MATH_FUNCS(1)
240# if MSVC_VERSION0==0 || MSVC_VERSION0>=1800
241# define SQLITE_HAVE_C99_MATH_FUNCS(1) (1)
242# else
243# define SQLITE_HAVE_C99_MATH_FUNCS(1) (0)
244# endif
245#endif
246
247/* Needed for various definitions... */
248#if defined(__GNUC__4) && !defined(_GNU_SOURCE)
249# define _GNU_SOURCE
250#endif
251
252#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
253# define _BSD_SOURCE
254#endif
255
256/*
257** Macro to disable warnings about missing "break" at the end of a "case".
258*/
259#if defined(__has_attribute)0
260# if __has_attribute(fallthrough)1
261# define deliberate_fall_through__attribute__((fallthrough)); __attribute__((fallthrough));
262# endif
263#endif
264#if !defined(deliberate_fall_through__attribute__((fallthrough));)
265# define deliberate_fall_through__attribute__((fallthrough));
266#endif
267
268/*
269** For MinGW, check to see if we can include the header file containing its
270** version information, among other things. Normally, this internal MinGW
271** header file would [only] be included automatically by other MinGW header
272** files; however, the contained version information is now required by this
273** header file to work around binary compatibility issues (see below) and
274** this is the only known way to reliably obtain it. This entire #if block
275** would be completely unnecessary if there was any other way of detecting
276** MinGW via their preprocessor (e.g. if they customized their GCC to define
277** some MinGW-specific macros). When compiling for MinGW, either the
278** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
279** defined; otherwise, detection of conditions specific to MinGW will be
280** disabled.
281*/
282#if defined(_HAVE_MINGW_H)
283# include "mingw.h"
284#elif defined(_HAVE__MINGW_H)
285# include "_mingw.h"
286#endif
287
288/*
289** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
290** define is required to maintain binary compatibility with the MSVC runtime
291** library in use (e.g. for Windows XP).
292*/
293#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
294 defined(_WIN32) && !defined(_WIN64) && \
295 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
296 defined(__MSVCRT__)
297# define _USE_32BIT_TIME_T
298#endif
299
300/* Optionally #include a user-defined header, whereby compilation options
301** may be set prior to where they take effect, but after platform setup.
302** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
303** file.
304*/
305#ifdef SQLITE_CUSTOM_INCLUDE
306# define INC_STRINGIFY_(f) #f
307# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
308# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
309#endif
310
311/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
312** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
313** MinGW.
314*/
315/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
316/************** Begin file sqlite3.h *****************************************/
317/*
318** 2001-09-15
319**
320** The author disclaims copyright to this source code. In place of
321** a legal notice, here is a blessing:
322**
323** May you do good and not evil.
324** May you find forgiveness for yourself and forgive others.
325** May you share freely, never taking more than you give.
326**
327*************************************************************************
328** This header file defines the interface that the SQLite library
329** presents to client programs. If a C-function, structure, datatype,
330** or constant definition does not appear in this file, then it is
331** not a published API of SQLite, is subject to change without
332** notice, and should not be referenced by programs that use SQLite.
333**
334** Some of the definitions that are in this file are marked as
335** "experimental". Experimental interfaces are normally new
336** features recently added to SQLite. We do not anticipate changes
337** to experimental interfaces but reserve the right to make minor changes
338** if experience from use "in the wild" suggest such changes are prudent.
339**
340** The official C-language API documentation for SQLite is derived
341** from comments in this file. This file is the authoritative source
342** on how SQLite interfaces are supposed to operate.
343**
344** The name of this file under configuration management is "sqlite.h.in".
345** The makefile makes some minor changes to this file (such as inserting
346** the version number) and changes its name to "sqlite3.h" as
347** part of the build process.
348*/
349#ifndef SQLITE3_H
350#define SQLITE3_H
351#include <stdarg.h> /* Needed for the definition of va_list */
352
353/*
354** Make sure we can call this stuff from C++.
355*/
356#if 0
357extern "C" {
358#endif
359
360
361/*
362** Facilitate override of interface linkage and calling conventions.
363** Be aware that these macros may not be used within this particular
364** translation of the amalgamation and its associated header file.
365**
366** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the
367** compiler that the target identifier should have external linkage.
368**
369** The SQLITE_CDECL macro is used to set the calling convention for
370** public functions that accept a variable number of arguments.
371**
372** The SQLITE_APICALL macro is used to set the calling convention for
373** public functions that accept a fixed number of arguments.
374**
375** The SQLITE_STDCALL macro is no longer used and is now deprecated.
376**
377** The SQLITE_CALLBACK macro is used to set the calling convention for
378** function pointers.
379**
380** The SQLITE_SYSAPI macro is used to set the calling convention for
381** functions provided by the operating system.
382**
383** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and
384** SQLITE_SYSAPI macros are used only when building for environments
385** that require non-default calling conventions.
386*/
387#ifndef SQLITE_EXTERNextern
388# define SQLITE_EXTERNextern extern
389#endif
390#ifndef SQLITE_API
391# define SQLITE_API
392#endif
393#ifndef SQLITE_CDECL
394# define SQLITE_CDECL
395#endif
396#ifndef SQLITE_APICALL
397# define SQLITE_APICALL
398#endif
399#ifndef SQLITE_STDCALL
400# define SQLITE_STDCALL SQLITE_APICALL
401#endif
402#ifndef SQLITE_CALLBACK
403# define SQLITE_CALLBACK
404#endif
405#ifndef SQLITE_SYSAPI
406# define SQLITE_SYSAPI
407#endif
408
409/*
410** These no-op macros are used in front of interfaces to mark those
411** interfaces as either deprecated or experimental. New applications
412** should not use deprecated interfaces - they are supported for backwards
413** compatibility only. Application writers should be aware that
414** experimental interfaces are subject to change in point releases.
415**
416** These macros used to resolve to various kinds of compiler magic that
417** would generate warning messages when they were used. But that
418** compiler magic ended up generating such a flurry of bug reports
419** that we have taken it all out and gone back to using simple
420** noop macros.
421*/
422#define SQLITE_DEPRECATED
423#define SQLITE_EXPERIMENTAL
424
425/*
426** Ensure these symbols were not defined by some previous header file.
427*/
428#ifdef SQLITE_VERSION"3.47.1"
429# undef SQLITE_VERSION"3.47.1"
430#endif
431#ifdef SQLITE_VERSION_NUMBER3047001
432# undef SQLITE_VERSION_NUMBER3047001
433#endif
434
435/*
436** CAPI3REF: Compile-Time Library Version Numbers
437**
438** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
439** evaluates to a string literal that is the SQLite version in the
440** format "X.Y.Z" where X is the major version number (always 3 for
441** SQLite3) and Y is the minor version number and Z is the release number.)^
442** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
443** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
444** numbers used in [SQLITE_VERSION].)^
445** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
446** be larger than the release from which it is derived. Either Y will
447** be held constant and Z will be incremented or else Y will be incremented
448** and Z will be reset to zero.
449**
450** Since [version 3.6.18] ([dateof:3.6.18]),
451** SQLite source code has been stored in the
452** <a href="http://www.fossil-scm.org/">Fossil configuration management
453** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
454** a string which identifies a particular check-in of SQLite
455** within its configuration management system. ^The SQLITE_SOURCE_ID
456** string contains the date and time of the check-in (UTC) and a SHA1
457** or SHA3-256 hash of the entire source tree. If the source code has
458** been edited in any way since it was last checked in, then the last
459** four hexadecimal digits of the hash may be modified.
460**
461** See also: [sqlite3_libversion()],
462** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463** [sqlite_version()] and [sqlite_source_id()].
464*/
465#define SQLITE_VERSION"3.47.1" "3.47.1"
466#define SQLITE_VERSION_NUMBER3047001 3047001
467#define SQLITE_SOURCE_ID"2024-11-25 12:07:48 b95d11e958643b969c47a8e5857f3793b9e69700b8f1469371386369a26e577e" "2024-11-25 12:07:48 b95d11e958643b969c47a8e5857f3793b9e69700b8f1469371386369a26e577e"
468
469/*
470** CAPI3REF: Run-Time Library Version Numbers
471** KEYWORDS: sqlite3_version sqlite3_sourceid
472**
473** These interfaces provide the same information as the [SQLITE_VERSION],
474** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
475** but are associated with the library instead of the header file. ^(Cautious
476** programmers might include assert() statements in their application to
477** verify that values returned by these interfaces match the macros in
478** the header, and thus ensure that the application is
479** compiled with matching library and header files.
480**
481** <blockquote><pre>
482** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
483** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
484** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
485** </pre></blockquote>)^
486**
487** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
488** macro. ^The sqlite3_libversion() function returns a pointer to the
489** to the sqlite3_version[] string constant. The sqlite3_libversion()
490** function is provided for use in DLLs since DLL users usually do not have
491** direct access to string constants within the DLL. ^The
492** sqlite3_libversion_number() function returns an integer equal to
493** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
494** a pointer to a string constant whose value is the same as the
495** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
496** using an edited copy of [the amalgamation], then the last four characters
497** of the hash might be different from [SQLITE_SOURCE_ID].)^
498**
499** See also: [sqlite_version()] and [sqlite_source_id()].
500*/
501SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.47.1";
502SQLITE_API const char *sqlite3_libversion(void);
503SQLITE_API const char *sqlite3_sourceid(void);
504SQLITE_API int sqlite3_libversion_number(void);
505
506/*
507** CAPI3REF: Run-Time Library Compilation Options Diagnostics
508**
509** ^The sqlite3_compileoption_used() function returns 0 or 1
510** indicating whether the specified option was defined at
511** compile time. ^The SQLITE_ prefix may be omitted from the
512** option name passed to sqlite3_compileoption_used().
513**
514** ^The sqlite3_compileoption_get() function allows iterating
515** over the list of options that were defined at compile time by
516** returning the N-th compile time option string. ^If N is out of range,
517** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
518** prefix is omitted from any strings returned by
519** sqlite3_compileoption_get().
520**
521** ^Support for the diagnostic functions sqlite3_compileoption_used()
522** and sqlite3_compileoption_get() may be omitted by specifying the
523** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
524**
525** See also: SQL functions [sqlite_compileoption_used()] and
526** [sqlite_compileoption_get()] and the [compile_options pragma].
527*/
528#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
529SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
530SQLITE_API const char *sqlite3_compileoption_get(int N);
531#else
532# define sqlite3_compileoption_used(X) 0
533# define sqlite3_compileoption_get(X) ((void*)0)
534#endif
535
536/*
537** CAPI3REF: Test To See If The Library Is Threadsafe
538**
539** ^The sqlite3_threadsafe() function returns zero if and only if
540** SQLite was compiled with mutexing code omitted due to the
541** [SQLITE_THREADSAFE] compile-time option being set to 0.
542**
543** SQLite can be compiled with or without mutexes. When
544** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
545** are enabled and SQLite is threadsafe. When the
546** [SQLITE_THREADSAFE] macro is 0,
547** the mutexes are omitted. Without the mutexes, it is not safe
548** to use SQLite concurrently from more than one thread.
549**
550** Enabling mutexes incurs a measurable performance penalty.
551** So if speed is of utmost importance, it makes sense to disable
552** the mutexes. But for maximum safety, mutexes should be enabled.
553** ^The default behavior is for mutexes to be enabled.
554**
555** This interface can be used by an application to make sure that the
556** version of SQLite that it is linking against was compiled with
557** the desired setting of the [SQLITE_THREADSAFE] macro.
558**
559** This interface only reports on the compile-time mutex setting
560** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
561** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
562** can be fully or partially disabled using a call to [sqlite3_config()]
563** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
564** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
565** sqlite3_threadsafe() function shows only the compile-time setting of
566** thread safety, not any run-time changes to that setting made by
567** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
568** is unchanged by calls to sqlite3_config().)^
569**
570** See the [threading mode] documentation for additional information.
571*/
572SQLITE_API int sqlite3_threadsafe(void);
573
574/*
575** CAPI3REF: Database Connection Handle
576** KEYWORDS: {database connection} {database connections}
577**
578** Each open SQLite database is represented by a pointer to an instance of
579** the opaque structure named "sqlite3". It is useful to think of an sqlite3
580** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
581** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
582** and [sqlite3_close_v2()] are its destructors. There are many other
583** interfaces (such as
584** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
585** [sqlite3_busy_timeout()] to name but three) that are methods on an
586** sqlite3 object.
587*/
588typedef struct sqlite3 sqlite3;
589
590/*
591** CAPI3REF: 64-Bit Integer Types
592** KEYWORDS: sqlite_int64 sqlite_uint64
593**
594** Because there is no cross-platform way to specify 64-bit integer types
595** SQLite includes typedefs for 64-bit signed and unsigned integers.
596**
597** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
598** The sqlite_int64 and sqlite_uint64 types are supported for backwards
599** compatibility only.
600**
601** ^The sqlite3_int64 and sqlite_int64 types can store integer values
602** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
603** sqlite3_uint64 and sqlite_uint64 types can store integer values
604** between 0 and +18446744073709551615 inclusive.
605*/
606#ifdef SQLITE_INT64_TYPE
607 typedef SQLITE_INT64_TYPE sqlite_int64;
608# ifdef SQLITE_UINT64_TYPE
609 typedef SQLITE_UINT64_TYPE sqlite_uint64;
610# else
611 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
612# endif
613#elif defined(_MSC_VER) || defined(__BORLANDC__)
614 typedef __int64 sqlite_int64;
615 typedef unsigned __int64 sqlite_uint64;
616#else
617 typedef long long int sqlite_int64;
618 typedef unsigned long long int sqlite_uint64;
619#endif
620typedef sqlite_int64 sqlite3_int64;
621typedef sqlite_uint64 sqlite3_uint64;
622
623/*
624** If compiling for a processor that lacks floating point support,
625** substitute integer for floating-point.
626*/
627#ifdef SQLITE_OMIT_FLOATING_POINT
628# define double sqlite3_int64
629#endif
630
631/*
632** CAPI3REF: Closing A Database Connection
633** DESTRUCTOR: sqlite3
634**
635** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
636** for the [sqlite3] object.
637** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
638** the [sqlite3] object is successfully destroyed and all associated
639** resources are deallocated.
640**
641** Ideally, applications should [sqlite3_finalize | finalize] all
642** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
643** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
644** with the [sqlite3] object prior to attempting to close the object.
645** ^If the database connection is associated with unfinalized prepared
646** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
647** sqlite3_close() will leave the database connection open and return
648** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
649** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
650** it returns [SQLITE_OK] regardless, but instead of deallocating the database
651** connection immediately, it marks the database connection as an unusable
652** "zombie" and makes arrangements to automatically deallocate the database
653** connection after all prepared statements are finalized, all BLOB handles
654** are closed, and all backups have finished. The sqlite3_close_v2() interface
655** is intended for use with host languages that are garbage collected, and
656** where the order in which destructors are called is arbitrary.
657**
658** ^If an [sqlite3] object is destroyed while a transaction is open,
659** the transaction is automatically rolled back.
660**
661** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
662** must be either a NULL
663** pointer or an [sqlite3] object pointer obtained
664** from [sqlite3_open()], [sqlite3_open16()], or
665** [sqlite3_open_v2()], and not previously closed.
666** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
667** argument is a harmless no-op.
668*/
669SQLITE_API int sqlite3_close(sqlite3*);
670SQLITE_API int sqlite3_close_v2(sqlite3*);
671
672/*
673** The type for a callback function.
674** This is legacy and deprecated. It is included for historical
675** compatibility and is not documented.
676*/
677typedef int (*sqlite3_callback)(void*,int,char**, char**);
678
679/*
680** CAPI3REF: One-Step Query Execution Interface
681** METHOD: sqlite3
682**
683** The sqlite3_exec() interface is a convenience wrapper around
684** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
685** that allows an application to run multiple statements of SQL
686** without having to use a lot of C code.
687**
688** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
689** semicolon-separate SQL statements passed into its 2nd argument,
690** in the context of the [database connection] passed in as its 1st
691** argument. ^If the callback function of the 3rd argument to
692** sqlite3_exec() is not NULL, then it is invoked for each result row
693** coming out of the evaluated SQL statements. ^The 4th argument to
694** sqlite3_exec() is relayed through to the 1st argument of each
695** callback invocation. ^If the callback pointer to sqlite3_exec()
696** is NULL, then no callback is ever invoked and result rows are
697** ignored.
698**
699** ^If an error occurs while evaluating the SQL statements passed into
700** sqlite3_exec(), then execution of the current statement stops and
701** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
702** is not NULL then any error message is written into memory obtained
703** from [sqlite3_malloc()] and passed back through the 5th parameter.
704** To avoid memory leaks, the application should invoke [sqlite3_free()]
705** on error message strings returned through the 5th parameter of
706** sqlite3_exec() after the error message string is no longer needed.
707** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
708** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
709** NULL before returning.
710**
711** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
712** routine returns SQLITE_ABORT without invoking the callback again and
713** without running any subsequent SQL statements.
714**
715** ^The 2nd argument to the sqlite3_exec() callback function is the
716** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
717** callback is an array of pointers to strings obtained as if from
718** [sqlite3_column_text()], one for each column. ^If an element of a
719** result row is NULL then the corresponding string pointer for the
720** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
721** sqlite3_exec() callback is an array of pointers to strings where each
722** entry represents the name of corresponding result column as obtained
723** from [sqlite3_column_name()].
724**
725** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
726** to an empty string, or a pointer that contains only whitespace and/or
727** SQL comments, then no SQL statements are evaluated and the database
728** is not changed.
729**
730** Restrictions:
731**
732** <ul>
733** <li> The application must ensure that the 1st parameter to sqlite3_exec()
734** is a valid and open [database connection].
735** <li> The application must not close the [database connection] specified by
736** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
737** <li> The application must not modify the SQL statement text passed into
738** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
739** <li> The application must not dereference the arrays or string pointers
740** passed as the 3rd and 4th callback parameters after it returns.
741** </ul>
742*/
743SQLITE_API int sqlite3_exec(
744 sqlite3*, /* An open database */
745 const char *sql, /* SQL to be evaluated */
746 int (*callback)(void*,int,char**,char**), /* Callback function */
747 void *, /* 1st argument to callback */
748 char **errmsg /* Error msg written here */
749);
750
751/*
752** CAPI3REF: Result Codes
753** KEYWORDS: {result code definitions}
754**
755** Many SQLite functions return an integer result code from the set shown
756** here in order to indicate success or failure.
757**
758** New error codes may be added in future versions of SQLite.
759**
760** See also: [extended result code definitions]
761*/
762#define SQLITE_OK0 0 /* Successful result */
763/* beginning-of-error-codes */
764#define SQLITE_ERROR1 1 /* Generic error */
765#define SQLITE_INTERNAL2 2 /* Internal logic error in SQLite */
766#define SQLITE_PERM3 3 /* Access permission denied */
767#define SQLITE_ABORT4 4 /* Callback routine requested an abort */
768#define SQLITE_BUSY5 5 /* The database file is locked */
769#define SQLITE_LOCKED6 6 /* A table in the database is locked */
770#define SQLITE_NOMEM7 7 /* A malloc() failed */
771#define SQLITE_READONLY8 8 /* Attempt to write a readonly database */
772#define SQLITE_INTERRUPT9 9 /* Operation terminated by sqlite3_interrupt()*/
773#define SQLITE_IOERR10 10 /* Some kind of disk I/O error occurred */
774#define SQLITE_CORRUPT11 11 /* The database disk image is malformed */
775#define SQLITE_NOTFOUND12 12 /* Unknown opcode in sqlite3_file_control() */
776#define SQLITE_FULL13 13 /* Insertion failed because database is full */
777#define SQLITE_CANTOPEN14 14 /* Unable to open the database file */
778#define SQLITE_PROTOCOL15 15 /* Database lock protocol error */
779#define SQLITE_EMPTY16 16 /* Internal use only */
780#define SQLITE_SCHEMA17 17 /* The database schema changed */
781#define SQLITE_TOOBIG18 18 /* String or BLOB exceeds size limit */
782#define SQLITE_CONSTRAINT19 19 /* Abort due to constraint violation */
783#define SQLITE_MISMATCH20 20 /* Data type mismatch */
784#define SQLITE_MISUSE21 21 /* Library used incorrectly */
785#define SQLITE_NOLFS22 22 /* Uses OS features not supported on host */
786#define SQLITE_AUTH23 23 /* Authorization denied */
787#define SQLITE_FORMAT24 24 /* Not used */
788#define SQLITE_RANGE25 25 /* 2nd parameter to sqlite3_bind out of range */
789#define SQLITE_NOTADB26 26 /* File opened that is not a database file */
790#define SQLITE_NOTICE27 27 /* Notifications from sqlite3_log() */
791#define SQLITE_WARNING28 28 /* Warnings from sqlite3_log() */
792#define SQLITE_ROW100 100 /* sqlite3_step() has another row ready */
793#define SQLITE_DONE101 101 /* sqlite3_step() has finished executing */
794/* end-of-error-codes */
795
796/*
797** CAPI3REF: Extended Result Codes
798** KEYWORDS: {extended result code definitions}
799**
800** In its default configuration, SQLite API routines return one of 30 integer
801** [result codes]. However, experience has shown that many of
802** these result codes are too coarse-grained. They do not provide as
803** much information about problems as programmers might like. In an effort to
804** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
805** and later) include
806** support for additional result codes that provide more detailed information
807** about errors. These [extended result codes] are enabled or disabled
808** on a per database connection basis using the
809** [sqlite3_extended_result_codes()] API. Or, the extended code for
810** the most recent error can be obtained using
811** [sqlite3_extended_errcode()].
812*/
813#define SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8)) (SQLITE_ERROR1 | (1<<8))
814#define SQLITE_ERROR_RETRY(1 | (2<<8)) (SQLITE_ERROR1 | (2<<8))
815#define SQLITE_ERROR_SNAPSHOT(1 | (3<<8)) (SQLITE_ERROR1 | (3<<8))
816#define SQLITE_IOERR_READ(10 | (1<<8)) (SQLITE_IOERR10 | (1<<8))
817#define SQLITE_IOERR_SHORT_READ(10 | (2<<8)) (SQLITE_IOERR10 | (2<<8))
818#define SQLITE_IOERR_WRITE(10 | (3<<8)) (SQLITE_IOERR10 | (3<<8))
819#define SQLITE_IOERR_FSYNC(10 | (4<<8)) (SQLITE_IOERR10 | (4<<8))
820#define SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)) (SQLITE_IOERR10 | (5<<8))
821#define SQLITE_IOERR_TRUNCATE(10 | (6<<8)) (SQLITE_IOERR10 | (6<<8))
822#define SQLITE_IOERR_FSTAT(10 | (7<<8)) (SQLITE_IOERR10 | (7<<8))
823#define SQLITE_IOERR_UNLOCK(10 | (8<<8)) (SQLITE_IOERR10 | (8<<8))
824#define SQLITE_IOERR_RDLOCK(10 | (9<<8)) (SQLITE_IOERR10 | (9<<8))
825#define SQLITE_IOERR_DELETE(10 | (10<<8)) (SQLITE_IOERR10 | (10<<8))
826#define SQLITE_IOERR_BLOCKED(10 | (11<<8)) (SQLITE_IOERR10 | (11<<8))
827#define SQLITE_IOERR_NOMEM(10 | (12<<8)) (SQLITE_IOERR10 | (12<<8))
828#define SQLITE_IOERR_ACCESS(10 | (13<<8)) (SQLITE_IOERR10 | (13<<8))
829#define SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)) (SQLITE_IOERR10 | (14<<8))
830#define SQLITE_IOERR_LOCK(10 | (15<<8)) (SQLITE_IOERR10 | (15<<8))
831#define SQLITE_IOERR_CLOSE(10 | (16<<8)) (SQLITE_IOERR10 | (16<<8))
832#define SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)) (SQLITE_IOERR10 | (17<<8))
833#define SQLITE_IOERR_SHMOPEN(10 | (18<<8)) (SQLITE_IOERR10 | (18<<8))
834#define SQLITE_IOERR_SHMSIZE(10 | (19<<8)) (SQLITE_IOERR10 | (19<<8))
835#define SQLITE_IOERR_SHMLOCK(10 | (20<<8)) (SQLITE_IOERR10 | (20<<8))
836#define SQLITE_IOERR_SHMMAP(10 | (21<<8)) (SQLITE_IOERR10 | (21<<8))
837#define SQLITE_IOERR_SEEK(10 | (22<<8)) (SQLITE_IOERR10 | (22<<8))
838#define SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) (SQLITE_IOERR10 | (23<<8))
839#define SQLITE_IOERR_MMAP(10 | (24<<8)) (SQLITE_IOERR10 | (24<<8))
840#define SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)) (SQLITE_IOERR10 | (25<<8))
841#define SQLITE_IOERR_CONVPATH(10 | (26<<8)) (SQLITE_IOERR10 | (26<<8))
842#define SQLITE_IOERR_VNODE(10 | (27<<8)) (SQLITE_IOERR10 | (27<<8))
843#define SQLITE_IOERR_AUTH(10 | (28<<8)) (SQLITE_IOERR10 | (28<<8))
844#define SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) (SQLITE_IOERR10 | (29<<8))
845#define SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) (SQLITE_IOERR10 | (30<<8))
846#define SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) (SQLITE_IOERR10 | (31<<8))
847#define SQLITE_IOERR_DATA(10 | (32<<8)) (SQLITE_IOERR10 | (32<<8))
848#define SQLITE_IOERR_CORRUPTFS(10 | (33<<8)) (SQLITE_IOERR10 | (33<<8))
849#define SQLITE_IOERR_IN_PAGE(10 | (34<<8)) (SQLITE_IOERR10 | (34<<8))
850#define SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)) (SQLITE_LOCKED6 | (1<<8))
851#define SQLITE_LOCKED_VTAB(6 | (2<<8)) (SQLITE_LOCKED6 | (2<<8))
852#define SQLITE_BUSY_RECOVERY(5 | (1<<8)) (SQLITE_BUSY5 | (1<<8))
853#define SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) (SQLITE_BUSY5 | (2<<8))
854#define SQLITE_BUSY_TIMEOUT(5 | (3<<8)) (SQLITE_BUSY5 | (3<<8))
855#define SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)) (SQLITE_CANTOPEN14 | (1<<8))
856#define SQLITE_CANTOPEN_ISDIR(14 | (2<<8)) (SQLITE_CANTOPEN14 | (2<<8))
857#define SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)) (SQLITE_CANTOPEN14 | (3<<8))
858#define SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)) (SQLITE_CANTOPEN14 | (4<<8))
859#define SQLITE_CANTOPEN_DIRTYWAL(14 | (5<<8)) (SQLITE_CANTOPEN14 | (5<<8)) /* Not Used */
860#define SQLITE_CANTOPEN_SYMLINK(14 | (6<<8)) (SQLITE_CANTOPEN14 | (6<<8))
861#define SQLITE_CORRUPT_VTAB(11 | (1<<8)) (SQLITE_CORRUPT11 | (1<<8))
862#define SQLITE_CORRUPT_SEQUENCE(11 | (2<<8)) (SQLITE_CORRUPT11 | (2<<8))
863#define SQLITE_CORRUPT_INDEX(11 | (3<<8)) (SQLITE_CORRUPT11 | (3<<8))
864#define SQLITE_READONLY_RECOVERY(8 | (1<<8)) (SQLITE_READONLY8 | (1<<8))
865#define SQLITE_READONLY_CANTLOCK(8 | (2<<8)) (SQLITE_READONLY8 | (2<<8))
866#define SQLITE_READONLY_ROLLBACK(8 | (3<<8)) (SQLITE_READONLY8 | (3<<8))
867#define SQLITE_READONLY_DBMOVED(8 | (4<<8)) (SQLITE_READONLY8 | (4<<8))
868#define SQLITE_READONLY_CANTINIT(8 | (5<<8)) (SQLITE_READONLY8 | (5<<8))
869#define SQLITE_READONLY_DIRECTORY(8 | (6<<8)) (SQLITE_READONLY8 | (6<<8))
870#define SQLITE_ABORT_ROLLBACK(4 | (2<<8)) (SQLITE_ABORT4 | (2<<8))
871#define SQLITE_CONSTRAINT_CHECK(19 | (1<<8)) (SQLITE_CONSTRAINT19 | (1<<8))
872#define SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)) (SQLITE_CONSTRAINT19 | (2<<8))
873#define SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)) (SQLITE_CONSTRAINT19 | (3<<8))
874#define SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)) (SQLITE_CONSTRAINT19 | (4<<8))
875#define SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)) (SQLITE_CONSTRAINT19 | (5<<8))
876#define SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)) (SQLITE_CONSTRAINT19 | (6<<8))
877#define SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) (SQLITE_CONSTRAINT19 | (7<<8))
878#define SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)) (SQLITE_CONSTRAINT19 | (8<<8))
879#define SQLITE_CONSTRAINT_VTAB(19 | (9<<8)) (SQLITE_CONSTRAINT19 | (9<<8))
880#define SQLITE_CONSTRAINT_ROWID(19 |(10<<8)) (SQLITE_CONSTRAINT19 |(10<<8))
881#define SQLITE_CONSTRAINT_PINNED(19 |(11<<8)) (SQLITE_CONSTRAINT19 |(11<<8))
882#define SQLITE_CONSTRAINT_DATATYPE(19 |(12<<8)) (SQLITE_CONSTRAINT19 |(12<<8))
883#define SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)) (SQLITE_NOTICE27 | (1<<8))
884#define SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)) (SQLITE_NOTICE27 | (2<<8))
885#define SQLITE_NOTICE_RBU(27 | (3<<8)) (SQLITE_NOTICE27 | (3<<8))
886#define SQLITE_WARNING_AUTOINDEX(28 | (1<<8)) (SQLITE_WARNING28 | (1<<8))
887#define SQLITE_AUTH_USER(23 | (1<<8)) (SQLITE_AUTH23 | (1<<8))
888#define SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) (SQLITE_OK0 | (1<<8))
889#define SQLITE_OK_SYMLINK(0 | (2<<8)) (SQLITE_OK0 | (2<<8)) /* internal use only */
890
891/*
892** CAPI3REF: Flags For File Open Operations
893**
894** These bit values are intended for use in the
895** 3rd parameter to the [sqlite3_open_v2()] interface and
896** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
897**
898** Only those flags marked as "Ok for sqlite3_open_v2()" may be
899** used as the third argument to the [sqlite3_open_v2()] interface.
900** The other flags have historically been ignored by sqlite3_open_v2(),
901** though future versions of SQLite might change so that an error is
902** raised if any of the disallowed bits are passed into sqlite3_open_v2().
903** Applications should not depend on the historical behavior.
904**
905** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into
906** [sqlite3_open_v2()] does *not* cause the underlying database file
907** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into
908** [sqlite3_open_v2()] has historically be a no-op and might become an
909** error in future versions of SQLite.
910*/
911#define SQLITE_OPEN_READONLY0x00000001 0x00000001 /* Ok for sqlite3_open_v2() */
912#define SQLITE_OPEN_READWRITE0x00000002 0x00000002 /* Ok for sqlite3_open_v2() */
913#define SQLITE_OPEN_CREATE0x00000004 0x00000004 /* Ok for sqlite3_open_v2() */
914#define SQLITE_OPEN_DELETEONCLOSE0x00000008 0x00000008 /* VFS only */
915#define SQLITE_OPEN_EXCLUSIVE0x00000010 0x00000010 /* VFS only */
916#define SQLITE_OPEN_AUTOPROXY0x00000020 0x00000020 /* VFS only */
917#define SQLITE_OPEN_URI0x00000040 0x00000040 /* Ok for sqlite3_open_v2() */
918#define SQLITE_OPEN_MEMORY0x00000080 0x00000080 /* Ok for sqlite3_open_v2() */
919#define SQLITE_OPEN_MAIN_DB0x00000100 0x00000100 /* VFS only */
920#define SQLITE_OPEN_TEMP_DB0x00000200 0x00000200 /* VFS only */
921#define SQLITE_OPEN_TRANSIENT_DB0x00000400 0x00000400 /* VFS only */
922#define SQLITE_OPEN_MAIN_JOURNAL0x00000800 0x00000800 /* VFS only */
923#define SQLITE_OPEN_TEMP_JOURNAL0x00001000 0x00001000 /* VFS only */
924#define SQLITE_OPEN_SUBJOURNAL0x00002000 0x00002000 /* VFS only */
925#define SQLITE_OPEN_SUPER_JOURNAL0x00004000 0x00004000 /* VFS only */
926#define SQLITE_OPEN_NOMUTEX0x00008000 0x00008000 /* Ok for sqlite3_open_v2() */
927#define SQLITE_OPEN_FULLMUTEX0x00010000 0x00010000 /* Ok for sqlite3_open_v2() */
928#define SQLITE_OPEN_SHAREDCACHE0x00020000 0x00020000 /* Ok for sqlite3_open_v2() */
929#define SQLITE_OPEN_PRIVATECACHE0x00040000 0x00040000 /* Ok for sqlite3_open_v2() */
930#define SQLITE_OPEN_WAL0x00080000 0x00080000 /* VFS only */
931#define SQLITE_OPEN_NOFOLLOW0x01000000 0x01000000 /* Ok for sqlite3_open_v2() */
932#define SQLITE_OPEN_EXRESCODE0x02000000 0x02000000 /* Extended result codes */
933
934/* Reserved: 0x00F00000 */
935/* Legacy compatibility: */
936#define SQLITE_OPEN_MASTER_JOURNAL0x00004000 0x00004000 /* VFS only */
937
938
939/*
940** CAPI3REF: Device Characteristics
941**
942** The xDeviceCharacteristics method of the [sqlite3_io_methods]
943** object returns an integer which is a vector of these
944** bit values expressing I/O characteristics of the mass storage
945** device that holds the file that the [sqlite3_io_methods]
946** refers to.
947**
948** The SQLITE_IOCAP_ATOMIC property means that all writes of
949** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
950** mean that writes of blocks that are nnn bytes in size and
951** are aligned to an address which is an integer multiple of
952** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
953** that when data is appended to a file, the data is appended
954** first then the size of the file is extended, never the other
955** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
956** information is written to disk in the same order as calls
957** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
958** after reboot following a crash or power loss, the only bytes in a
959** file that were written at the application level might have changed
960** and that adjacent bytes, even bytes within the same sector are
961** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
962** flag indicates that a file cannot be deleted when open. The
963** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
964** read-only media and cannot be changed even by processes with
965** elevated privileges.
966**
967** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
968** filesystem supports doing multiple write operations atomically when those
969** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
970** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
971**
972** The SQLITE_IOCAP_SUBPAGE_READ property means that it is ok to read
973** from the database file in amounts that are not a multiple of the
974** page size and that do not begin at a page boundary. Without this
975** property, SQLite is careful to only do full-page reads and write
976** on aligned pages, with the one exception that it will do a sub-page
977** read of the first page to access the database header.
978*/
979#define SQLITE_IOCAP_ATOMIC0x00000001 0x00000001
980#define SQLITE_IOCAP_ATOMIC5120x00000002 0x00000002
981#define SQLITE_IOCAP_ATOMIC1K0x00000004 0x00000004
982#define SQLITE_IOCAP_ATOMIC2K0x00000008 0x00000008
983#define SQLITE_IOCAP_ATOMIC4K0x00000010 0x00000010
984#define SQLITE_IOCAP_ATOMIC8K0x00000020 0x00000020
985#define SQLITE_IOCAP_ATOMIC16K0x00000040 0x00000040
986#define SQLITE_IOCAP_ATOMIC32K0x00000080 0x00000080
987#define SQLITE_IOCAP_ATOMIC64K0x00000100 0x00000100
988#define SQLITE_IOCAP_SAFE_APPEND0x00000200 0x00000200
989#define SQLITE_IOCAP_SEQUENTIAL0x00000400 0x00000400
990#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 0x00000800
991#define SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 0x00001000
992#define SQLITE_IOCAP_IMMUTABLE0x00002000 0x00002000
993#define SQLITE_IOCAP_BATCH_ATOMIC0x00004000 0x00004000
994#define SQLITE_IOCAP_SUBPAGE_READ0x00008000 0x00008000
995
996/*
997** CAPI3REF: File Locking Levels
998**
999** SQLite uses one of these integer values as the second
1000** argument to calls it makes to the xLock() and xUnlock() methods
1001** of an [sqlite3_io_methods] object. These values are ordered from
1002** lest restrictive to most restrictive.
1003**
1004** The argument to xLock() is always SHARED or higher. The argument to
1005** xUnlock is either SHARED or NONE.
1006*/
1007#define SQLITE_LOCK_NONE0 0 /* xUnlock() only */
1008#define SQLITE_LOCK_SHARED1 1 /* xLock() or xUnlock() */
1009#define SQLITE_LOCK_RESERVED2 2 /* xLock() only */
1010#define SQLITE_LOCK_PENDING3 3 /* xLock() only */
1011#define SQLITE_LOCK_EXCLUSIVE4 4 /* xLock() only */
1012
1013/*
1014** CAPI3REF: Synchronization Type Flags
1015**
1016** When SQLite invokes the xSync() method of an
1017** [sqlite3_io_methods] object it uses a combination of
1018** these integer values as the second argument.
1019**
1020** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1021** sync operation only needs to flush data to mass storage. Inode
1022** information need not be flushed. If the lower four bits of the flag
1023** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1024** If the lower four bits equal SQLITE_SYNC_FULL, that means
1025** to use Mac OS X style fullsync instead of fsync().
1026**
1027** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1028** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1029** settings. The [synchronous pragma] determines when calls to the
1030** xSync VFS method occur and applies uniformly across all platforms.
1031** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1032** energetic or rigorous or forceful the sync operations are and
1033** only make a difference on Mac OSX for the default SQLite code.
1034** (Third-party VFS implementations might also make the distinction
1035** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1036** operating systems natively supported by SQLite, only Mac OSX
1037** cares about the difference.)
1038*/
1039#define SQLITE_SYNC_NORMAL0x00002 0x00002
1040#define SQLITE_SYNC_FULL0x00003 0x00003
1041#define SQLITE_SYNC_DATAONLY0x00010 0x00010
1042
1043/*
1044** CAPI3REF: OS Interface Open File Handle
1045**
1046** An [sqlite3_file] object represents an open file in the
1047** [sqlite3_vfs | OS interface layer]. Individual OS interface
1048** implementations will
1049** want to subclass this object by appending additional fields
1050** for their own use. The pMethods entry is a pointer to an
1051** [sqlite3_io_methods] object that defines methods for performing
1052** I/O operations on the open file.
1053*/
1054typedef struct sqlite3_file sqlite3_file;
1055struct sqlite3_file {
1056 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1057};
1058
1059/*
1060** CAPI3REF: OS Interface File Virtual Methods Object
1061**
1062** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1063** [sqlite3_file] object (or, more commonly, a subclass of the
1064** [sqlite3_file] object) with a pointer to an instance of this object.
1065** This object defines the methods used to perform various operations
1066** against the open file represented by the [sqlite3_file] object.
1067**
1068** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1069** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1070** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1071** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1072** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1073** to NULL.
1074**
1075** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1076** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1077** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1078** flag may be ORed in to indicate that only the data of the file
1079** and not its inode needs to be synced.
1080**
1081** The integer values to xLock() and xUnlock() are one of
1082** <ul>
1083** <li> [SQLITE_LOCK_NONE],
1084** <li> [SQLITE_LOCK_SHARED],
1085** <li> [SQLITE_LOCK_RESERVED],
1086** <li> [SQLITE_LOCK_PENDING], or
1087** <li> [SQLITE_LOCK_EXCLUSIVE].
1088** </ul>
1089** xLock() upgrades the database file lock. In other words, xLock() moves the
1090** database file lock in the direction NONE toward EXCLUSIVE. The argument to
1091** xLock() is always one of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1092** SQLITE_LOCK_NONE. If the database file lock is already at or above the
1093** requested lock, then the call to xLock() is a no-op.
1094** xUnlock() downgrades the database file lock to either SHARED or NONE.
1095** If the lock is already at or below the requested lock state, then the call
1096** to xUnlock() is a no-op.
1097** The xCheckReservedLock() method checks whether any database connection,
1098** either in this process or in some other process, is holding a RESERVED,
1099** PENDING, or EXCLUSIVE lock on the file. It returns, via its output
1100** pointer parameter, true if such a lock exists and false otherwise.
1101**
1102** The xFileControl() method is a generic interface that allows custom
1103** VFS implementations to directly control an open file using the
1104** [sqlite3_file_control()] interface. The second "op" argument is an
1105** integer opcode. The third argument is a generic pointer intended to
1106** point to a structure that may contain arguments or space in which to
1107** write return values. Potential uses for xFileControl() might be
1108** functions to enable blocking locks with timeouts, to change the
1109** locking strategy (for example to use dot-file locks), to inquire
1110** about the status of a lock, or to break stale locks. The SQLite
1111** core reserves all opcodes less than 100 for its own use.
1112** A [file control opcodes | list of opcodes] less than 100 is available.
1113** Applications that define a custom xFileControl method should use opcodes
1114** greater than 100 to avoid conflicts. VFS implementations should
1115** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1116** recognize.
1117**
1118** The xSectorSize() method returns the sector size of the
1119** device that underlies the file. The sector size is the
1120** minimum write that can be performed without disturbing
1121** other bytes in the file. The xDeviceCharacteristics()
1122** method returns a bit vector describing behaviors of the
1123** underlying device:
1124**
1125** <ul>
1126** <li> [SQLITE_IOCAP_ATOMIC]
1127** <li> [SQLITE_IOCAP_ATOMIC512]
1128** <li> [SQLITE_IOCAP_ATOMIC1K]
1129** <li> [SQLITE_IOCAP_ATOMIC2K]
1130** <li> [SQLITE_IOCAP_ATOMIC4K]
1131** <li> [SQLITE_IOCAP_ATOMIC8K]
1132** <li> [SQLITE_IOCAP_ATOMIC16K]
1133** <li> [SQLITE_IOCAP_ATOMIC32K]
1134** <li> [SQLITE_IOCAP_ATOMIC64K]
1135** <li> [SQLITE_IOCAP_SAFE_APPEND]
1136** <li> [SQLITE_IOCAP_SEQUENTIAL]
1137** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1138** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1139** <li> [SQLITE_IOCAP_IMMUTABLE]
1140** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1141** <li> [SQLITE_IOCAP_SUBPAGE_READ]
1142** </ul>
1143**
1144** The SQLITE_IOCAP_ATOMIC property means that all writes of
1145** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1146** mean that writes of blocks that are nnn bytes in size and
1147** are aligned to an address which is an integer multiple of
1148** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1149** that when data is appended to a file, the data is appended
1150** first then the size of the file is extended, never the other
1151** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1152** information is written to disk in the same order as calls
1153** to xWrite().
1154**
1155** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1156** in the unread portions of the buffer with zeros. A VFS that
1157** fails to zero-fill short reads might seem to work. However,
1158** failure to zero-fill short reads will eventually lead to
1159** database corruption.
1160*/
1161typedef struct sqlite3_io_methods sqlite3_io_methods;
1162struct sqlite3_io_methods {
1163 int iVersion;
1164 int (*xClose)(sqlite3_file*);
1165 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1166 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1167 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1168 int (*xSync)(sqlite3_file*, int flags);
1169 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1170 int (*xLock)(sqlite3_file*, int);
1171 int (*xUnlock)(sqlite3_file*, int);
1172 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1173 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1174 int (*xSectorSize)(sqlite3_file*);
1175 int (*xDeviceCharacteristics)(sqlite3_file*);
1176 /* Methods above are valid for version 1 */
1177 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1178 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1179 void (*xShmBarrier)(sqlite3_file*);
1180 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1181 /* Methods above are valid for version 2 */
1182 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1183 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1184 /* Methods above are valid for version 3 */
1185 /* Additional methods may be added in future releases */
1186};
1187
1188/*
1189** CAPI3REF: Standard File Control Opcodes
1190** KEYWORDS: {file control opcodes} {file control opcode}
1191**
1192** These integer constants are opcodes for the xFileControl method
1193** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1194** interface.
1195**
1196** <ul>
1197** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1198** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1199** opcode causes the xFileControl method to write the current state of
1200** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1201** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1202** into an integer that the pArg argument points to.
1203** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
1204**
1205** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1206** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1207** layer a hint of how large the database file will grow to be during the
1208** current transaction. This hint is not guaranteed to be accurate but it
1209** is often close. The underlying VFS might choose to preallocate database
1210** file space based on this hint in order to help writes to the database
1211** file run faster.
1212**
1213** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1214** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1215** implements [sqlite3_deserialize()] to set an upper bound on the size
1216** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1217** If the integer pointed to is negative, then it is filled in with the
1218** current limit. Otherwise the limit is set to the larger of the value
1219** of the integer pointed to and the current database size. The integer
1220** pointed to is set to the new limit.
1221**
1222** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1223** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1224** extends and truncates the database file in chunks of a size specified
1225** by the user. The fourth argument to [sqlite3_file_control()] should
1226** point to an integer (type int) containing the new chunk-size to use
1227** for the nominated database. Allocating database file space in large
1228** chunks (say 1MB at a time), may reduce file-system fragmentation and
1229** improve performance on some systems.
1230**
1231** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1232** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1233** to the [sqlite3_file] object associated with a particular database
1234** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1235**
1236** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1237** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1238** to the [sqlite3_file] object associated with the journal file (either
1239** the [rollback journal] or the [write-ahead log]) for a particular database
1240** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1241**
1242** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1243** No longer in use.
1244**
1245** <li>[[SQLITE_FCNTL_SYNC]]
1246** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1247** sent to the VFS immediately before the xSync method is invoked on a
1248** database file descriptor. Or, if the xSync method is not invoked
1249** because the user has configured SQLite with
1250** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1251** of the xSync method. In most cases, the pointer argument passed with
1252** this file-control is NULL. However, if the database file is being synced
1253** as part of a multi-database commit, the argument points to a nul-terminated
1254** string containing the transactions super-journal file name. VFSes that
1255** do not need this signal should silently ignore this opcode. Applications
1256** should not call [sqlite3_file_control()] with this opcode as doing so may
1257** disrupt the operation of the specialized VFSes that do require it.
1258**
1259** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1260** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1261** and sent to the VFS after a transaction has been committed immediately
1262** but before the database is unlocked. VFSes that do not need this signal
1263** should silently ignore this opcode. Applications should not call
1264** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1265** operation of the specialized VFSes that do require it.
1266**
1267** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1268** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1269** retry counts and intervals for certain disk I/O operations for the
1270** windows [VFS] in order to provide robustness in the presence of
1271** anti-virus programs. By default, the windows VFS will retry file read,
1272** file write, and file delete operations up to 10 times, with a delay
1273** of 25 milliseconds before the first retry and with the delay increasing
1274** by an additional 25 milliseconds with each subsequent retry. This
1275** opcode allows these two values (10 retries and 25 milliseconds of delay)
1276** to be adjusted. The values are changed for all database connections
1277** within the same process. The argument is a pointer to an array of two
1278** integers where the first integer is the new retry count and the second
1279** integer is the delay. If either integer is negative, then the setting
1280** is not changed but instead the prior value of that setting is written
1281** into the array entry, allowing the current retry settings to be
1282** interrogated. The zDbName parameter is ignored.
1283**
1284** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1285** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1286** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1287** write ahead log ([WAL file]) and shared memory
1288** files used for transaction control
1289** are automatically deleted when the latest connection to the database
1290** closes. Setting persistent WAL mode causes those files to persist after
1291** close. Persisting the files is useful when other processes that do not
1292** have write permission on the directory containing the database file want
1293** to read the database file, as the WAL and shared memory files must exist
1294** in order for the database to be readable. The fourth parameter to
1295** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1296** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1297** WAL mode. If the integer is -1, then it is overwritten with the current
1298** WAL persistence setting.
1299**
1300** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1301** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1302** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1303** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1304** xDeviceCharacteristics methods. The fourth parameter to
1305** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1306** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1307** mode. If the integer is -1, then it is overwritten with the current
1308** zero-damage mode setting.
1309**
1310** <li>[[SQLITE_FCNTL_OVERWRITE]]
1311** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1312** a write transaction to indicate that, unless it is rolled back for some
1313** reason, the entire database file will be overwritten by the current
1314** transaction. This is used by VACUUM operations.
1315**
1316** <li>[[SQLITE_FCNTL_VFSNAME]]
1317** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1318** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1319** final bottom-level VFS are written into memory obtained from
1320** [sqlite3_malloc()] and the result is stored in the char* variable
1321** that the fourth parameter of [sqlite3_file_control()] points to.
1322** The caller is responsible for freeing the memory when done. As with
1323** all file-control actions, there is no guarantee that this will actually
1324** do anything. Callers should initialize the char* variable to a NULL
1325** pointer in case this file-control is not implemented. This file-control
1326** is intended for diagnostic use only.
1327**
1328** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1329** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1330** [VFSes] currently in use. ^(The argument X in
1331** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1332** of type "[sqlite3_vfs] **". This opcodes will set *X
1333** to a pointer to the top-level VFS.)^
1334** ^When there are multiple VFS shims in the stack, this opcode finds the
1335** upper-most shim only.
1336**
1337** <li>[[SQLITE_FCNTL_PRAGMA]]
1338** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1339** file control is sent to the open [sqlite3_file] object corresponding
1340** to the database file to which the pragma statement refers. ^The argument
1341** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1342** pointers to strings (char**) in which the second element of the array
1343** is the name of the pragma and the third element is the argument to the
1344** pragma or NULL if the pragma has no argument. ^The handler for an
1345** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1346** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1347** or the equivalent and that string will become the result of the pragma or
1348** the error message if the pragma fails. ^If the
1349** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1350** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1351** file control returns [SQLITE_OK], then the parser assumes that the
1352** VFS has handled the PRAGMA itself and the parser generates a no-op
1353** prepared statement if result string is NULL, or that returns a copy
1354** of the result string if the string is non-NULL.
1355** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1356** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1357** that the VFS encountered an error while handling the [PRAGMA] and the
1358** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1359** file control occurs at the beginning of pragma statement analysis and so
1360** it is able to override built-in [PRAGMA] statements.
1361**
1362** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1363** ^The [SQLITE_FCNTL_BUSYHANDLER]
1364** file-control may be invoked by SQLite on the database file handle
1365** shortly after it is opened in order to provide a custom VFS with access
1366** to the connection's busy-handler callback. The argument is of type (void**)
1367** - an array of two (void *) values. The first (void *) actually points
1368** to a function of type (int (*)(void *)). In order to invoke the connection's
1369** busy-handler, this function should be invoked with the second (void *) in
1370** the array as the only argument. If it returns non-zero, then the operation
1371** should be retried. If it returns zero, the custom VFS should abandon the
1372** current operation.
1373**
1374** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1375** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1376** to have SQLite generate a
1377** temporary filename using the same algorithm that is followed to generate
1378** temporary filenames for TEMP tables and other internal uses. The
1379** argument should be a char** which will be filled with the filename
1380** written into memory obtained from [sqlite3_malloc()]. The caller should
1381** invoke [sqlite3_free()] on the result to avoid a memory leak.
1382**
1383** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1384** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1385** maximum number of bytes that will be used for memory-mapped I/O.
1386** The argument is a pointer to a value of type sqlite3_int64 that
1387** is an advisory maximum number of bytes in the file to memory map. The
1388** pointer is overwritten with the old value. The limit is not changed if
1389** the value originally pointed to is negative, and so the current limit
1390** can be queried by passing in a pointer to a negative number. This
1391** file-control is used internally to implement [PRAGMA mmap_size].
1392**
1393** <li>[[SQLITE_FCNTL_TRACE]]
1394** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1395** to the VFS about what the higher layers of the SQLite stack are doing.
1396** This file control is used by some VFS activity tracing [shims].
1397** The argument is a zero-terminated string. Higher layers in the
1398** SQLite stack may generate instances of this file control if
1399** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1400**
1401** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1402** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1403** pointer to an integer and it writes a boolean into that integer depending
1404** on whether or not the file has been renamed, moved, or deleted since it
1405** was first opened.
1406**
1407** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
1408** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
1409** underlying native file handle associated with a file handle. This file
1410** control interprets its argument as a pointer to a native file handle and
1411** writes the resulting value there.
1412**
1413** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1414** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1415** opcode causes the xFileControl method to swap the file handle with the one
1416** pointed to by the pArg argument. This capability is used during testing
1417** and only needs to be supported when SQLITE_TEST is defined.
1418**
1419** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1420** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1421** be advantageous to block on the next WAL lock if the lock is not immediately
1422** available. The WAL subsystem issues this signal during rare
1423** circumstances in order to fix a problem with priority inversion.
1424** Applications should <em>not</em> use this file-control.
1425**
1426** <li>[[SQLITE_FCNTL_ZIPVFS]]
1427** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1428** VFS should return SQLITE_NOTFOUND for this opcode.
1429**
1430** <li>[[SQLITE_FCNTL_RBU]]
1431** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1432** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1433** this opcode.
1434**
1435** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
1436** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
1437** the file descriptor is placed in "batch write mode", which
1438** means all subsequent write operations will be deferred and done
1439** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
1440** that do not support batch atomic writes will return SQLITE_NOTFOUND.
1441** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
1442** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
1443** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
1444** no VFS interface calls on the same [sqlite3_file] file descriptor
1445** except for calls to the xWrite method and the xFileControl method
1446** with [SQLITE_FCNTL_SIZE_HINT].
1447**
1448** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
1449** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
1450** operations since the previous successful call to
1451** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
1452** This file control returns [SQLITE_OK] if and only if the writes were
1453** all performed successfully and have been committed to persistent storage.
1454** ^Regardless of whether or not it is successful, this file control takes
1455** the file descriptor out of batch write mode so that all subsequent
1456** write operations are independent.
1457** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
1458** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1459**
1460** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
1461** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
1462** operations since the previous successful call to
1463** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1464** ^This file control takes the file descriptor out of batch write mode
1465** so that all subsequent write operations are independent.
1466** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1467** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1468**
1469** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1470** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
1471** to block for up to M milliseconds before failing when attempting to
1472** obtain a file lock using the xLock or xShmLock methods of the VFS.
1473** The parameter is a pointer to a 32-bit signed integer that contains
1474** the value that M is to be set to. Before returning, the 32-bit signed
1475** integer is overwritten with the previous value of M.
1476**
1477** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1478** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1479** a database file. The argument is a pointer to a 32-bit unsigned integer.
1480** The "data version" for the pager is written into the pointer. The
1481** "data version" changes whenever any change occurs to the corresponding
1482** database file, either through SQL statements on the same database
1483** connection or through transactions committed by separate database
1484** connections possibly in other processes. The [sqlite3_total_changes()]
1485** interface can be used to find if any database on the connection has changed,
1486** but that interface responds to changes on TEMP as well as MAIN and does
1487** not provide a mechanism to detect changes to MAIN only. Also, the
1488** [sqlite3_total_changes()] interface responds to internal changes only and
1489** omits changes made by other database connections. The
1490** [PRAGMA data_version] command provides a mechanism to detect changes to
1491** a single attached database that occur due to other database connections,
1492** but omits changes implemented by the database connection on which it is
1493** called. This file control is the only mechanism to detect changes that
1494** happen either internally or externally and that are associated with
1495** a particular attached database.
1496**
1497** <li>[[SQLITE_FCNTL_CKPT_START]]
1498** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
1499** in wal mode before the client starts to copy pages from the wal
1500** file to the database file.
1501**
1502** <li>[[SQLITE_FCNTL_CKPT_DONE]]
1503** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
1504** in wal mode after the client has finished copying pages from the wal
1505** file to the database file, but before the *-shm file is updated to
1506** record the fact that the pages have been checkpointed.
1507**
1508** <li>[[SQLITE_FCNTL_EXTERNAL_READER]]
1509** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect
1510** whether or not there is a database client in another process with a wal-mode
1511** transaction open on the database or not. It is only available on unix.The
1512** (void*) argument passed with this file-control should be a pointer to a
1513** value of type (int). The integer value is set to 1 if the database is a wal
1514** mode database and there exists at least one client in another process that
1515** currently has an SQL transaction open on the database. It is set to 0 if
1516** the database is not a wal-mode db, or if there is no such connection in any
1517** other process. This opcode cannot be used to detect transactions opened
1518** by clients within the current process, only within other processes.
1519**
1520** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1521** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the
1522** [checksum VFS shim] only.
1523**
1524** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1525** If there is currently no transaction open on the database, and the
1526** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1527** purges the contents of the in-memory page cache. If there is an open
1528** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1529** </ul>
1530*/
1531#define SQLITE_FCNTL_LOCKSTATE1 1
1532#define SQLITE_FCNTL_GET_LOCKPROXYFILE2 2
1533#define SQLITE_FCNTL_SET_LOCKPROXYFILE3 3
1534#define SQLITE_FCNTL_LAST_ERRNO4 4
1535#define SQLITE_FCNTL_SIZE_HINT5 5
1536#define SQLITE_FCNTL_CHUNK_SIZE6 6
1537#define SQLITE_FCNTL_FILE_POINTER7 7
1538#define SQLITE_FCNTL_SYNC_OMITTED8 8
1539#define SQLITE_FCNTL_WIN32_AV_RETRY9 9
1540#define SQLITE_FCNTL_PERSIST_WAL10 10
1541#define SQLITE_FCNTL_OVERWRITE11 11
1542#define SQLITE_FCNTL_VFSNAME12 12
1543#define SQLITE_FCNTL_POWERSAFE_OVERWRITE13 13
1544#define SQLITE_FCNTL_PRAGMA14 14
1545#define SQLITE_FCNTL_BUSYHANDLER15 15
1546#define SQLITE_FCNTL_TEMPFILENAME16 16
1547#define SQLITE_FCNTL_MMAP_SIZE18 18
1548#define SQLITE_FCNTL_TRACE19 19
1549#define SQLITE_FCNTL_HAS_MOVED20 20
1550#define SQLITE_FCNTL_SYNC21 21
1551#define SQLITE_FCNTL_COMMIT_PHASETWO22 22
1552#define SQLITE_FCNTL_WIN32_SET_HANDLE23 23
1553#define SQLITE_FCNTL_WAL_BLOCK24 24
1554#define SQLITE_FCNTL_ZIPVFS25 25
1555#define SQLITE_FCNTL_RBU26 26
1556#define SQLITE_FCNTL_VFS_POINTER27 27
1557#define SQLITE_FCNTL_JOURNAL_POINTER28 28
1558#define SQLITE_FCNTL_WIN32_GET_HANDLE29 29
1559#define SQLITE_FCNTL_PDB30 30
1560#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31 31
1561#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32 32
1562#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33 33
1563#define SQLITE_FCNTL_LOCK_TIMEOUT34 34
1564#define SQLITE_FCNTL_DATA_VERSION35 35
1565#define SQLITE_FCNTL_SIZE_LIMIT36 36
1566#define SQLITE_FCNTL_CKPT_DONE37 37
1567#define SQLITE_FCNTL_RESERVE_BYTES38 38
1568#define SQLITE_FCNTL_CKPT_START39 39
1569#define SQLITE_FCNTL_EXTERNAL_READER40 40
1570#define SQLITE_FCNTL_CKSM_FILE41 41
1571#define SQLITE_FCNTL_RESET_CACHE42 42
1572
1573/* deprecated names */
1574#define SQLITE_GET_LOCKPROXYFILE2 SQLITE_FCNTL_GET_LOCKPROXYFILE2
1575#define SQLITE_SET_LOCKPROXYFILE3 SQLITE_FCNTL_SET_LOCKPROXYFILE3
1576#define SQLITE_LAST_ERRNO4 SQLITE_FCNTL_LAST_ERRNO4
1577
1578
1579/*
1580** CAPI3REF: Mutex Handle
1581**
1582** The mutex module within SQLite defines [sqlite3_mutex] to be an
1583** abstract type for a mutex object. The SQLite core never looks
1584** at the internal representation of an [sqlite3_mutex]. It only
1585** deals with pointers to the [sqlite3_mutex] object.
1586**
1587** Mutexes are created using [sqlite3_mutex_alloc()].
1588*/
1589typedef struct sqlite3_mutex sqlite3_mutex;
1590
1591/*
1592** CAPI3REF: Loadable Extension Thunk
1593**
1594** A pointer to the opaque sqlite3_api_routines structure is passed as
1595** the third parameter to entry points of [loadable extensions]. This
1596** structure must be typedefed in order to work around compiler warnings
1597** on some platforms.
1598*/
1599typedef struct sqlite3_api_routines sqlite3_api_routines;
1600
1601/*
1602** CAPI3REF: File Name
1603**
1604** Type [sqlite3_filename] is used by SQLite to pass filenames to the
1605** xOpen method of a [VFS]. It may be cast to (const char*) and treated
1606** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1607** may also be passed to special APIs such as:
1608**
1609** <ul>
1610** <li> sqlite3_filename_database()
1611** <li> sqlite3_filename_journal()
1612** <li> sqlite3_filename_wal()
1613** <li> sqlite3_uri_parameter()
1614** <li> sqlite3_uri_boolean()
1615** <li> sqlite3_uri_int64()
1616** <li> sqlite3_uri_key()
1617** </ul>
1618*/
1619typedef const char *sqlite3_filename;
1620
1621/*
1622** CAPI3REF: OS Interface Object
1623**
1624** An instance of the sqlite3_vfs object defines the interface between
1625** the SQLite core and the underlying operating system. The "vfs"
1626** in the name of the object stands for "virtual file system". See
1627** the [VFS | VFS documentation] for further information.
1628**
1629** The VFS interface is sometimes extended by adding new methods onto
1630** the end. Each time such an extension occurs, the iVersion field
1631** is incremented. The iVersion value started out as 1 in
1632** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
1633** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
1634** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
1635** may be appended to the sqlite3_vfs object and the iVersion value
1636** may increase again in future versions of SQLite.
1637** Note that due to an oversight, the structure
1638** of the sqlite3_vfs object changed in the transition from
1639** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
1640** and yet the iVersion field was not increased.
1641**
1642** The szOsFile field is the size of the subclassed [sqlite3_file]
1643** structure used by this VFS. mxPathname is the maximum length of
1644** a pathname in this VFS.
1645**
1646** Registered sqlite3_vfs objects are kept on a linked list formed by
1647** the pNext pointer. The [sqlite3_vfs_register()]
1648** and [sqlite3_vfs_unregister()] interfaces manage this list
1649** in a thread-safe way. The [sqlite3_vfs_find()] interface
1650** searches the list. Neither the application code nor the VFS
1651** implementation should use the pNext pointer.
1652**
1653** The pNext field is the only field in the sqlite3_vfs
1654** structure that SQLite will ever modify. SQLite will only access
1655** or modify this field while holding a particular static mutex.
1656** The application should never modify anything within the sqlite3_vfs
1657** object once the object has been registered.
1658**
1659** The zName field holds the name of the VFS module. The name must
1660** be unique across all VFS modules.
1661**
1662** [[sqlite3_vfs.xOpen]]
1663** ^SQLite guarantees that the zFilename parameter to xOpen
1664** is either a NULL pointer or string obtained
1665** from xFullPathname() with an optional suffix added.
1666** ^If a suffix is added to the zFilename parameter, it will
1667** consist of a single "-" character followed by no more than
1668** 11 alphanumeric and/or "-" characters.
1669** ^SQLite further guarantees that
1670** the string will be valid and unchanged until xClose() is
1671** called. Because of the previous sentence,
1672** the [sqlite3_file] can safely store a pointer to the
1673** filename if it needs to remember the filename for some reason.
1674** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1675** must invent its own temporary name for the file. ^Whenever the
1676** xFilename parameter is NULL it will also be the case that the
1677** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1678**
1679** The flags argument to xOpen() includes all bits set in
1680** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
1681** or [sqlite3_open16()] is used, then flags includes at least
1682** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1683** If xOpen() opens a file read-only then it sets *pOutFlags to
1684** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
1685**
1686** ^(SQLite will also add one of the following flags to the xOpen()
1687** call, depending on the object being opened:
1688**
1689** <ul>
1690** <li> [SQLITE_OPEN_MAIN_DB]
1691** <li> [SQLITE_OPEN_MAIN_JOURNAL]
1692** <li> [SQLITE_OPEN_TEMP_DB]
1693** <li> [SQLITE_OPEN_TEMP_JOURNAL]
1694** <li> [SQLITE_OPEN_TRANSIENT_DB]
1695** <li> [SQLITE_OPEN_SUBJOURNAL]
1696** <li> [SQLITE_OPEN_SUPER_JOURNAL]
1697** <li> [SQLITE_OPEN_WAL]
1698** </ul>)^
1699**
1700** The file I/O implementation can use the object type flags to
1701** change the way it deals with files. For example, an application
1702** that does not care about crash recovery or rollback might make
1703** the open of a journal file a no-op. Writes to this journal would
1704** also be no-ops, and any attempt to read the journal would return
1705** SQLITE_IOERR. Or the implementation might recognize that a database
1706** file will be doing page-aligned sector reads and writes in a random
1707** order and set up its I/O subsystem accordingly.
1708**
1709** SQLite might also add one of the following flags to the xOpen method:
1710**
1711** <ul>
1712** <li> [SQLITE_OPEN_DELETEONCLOSE]
1713** <li> [SQLITE_OPEN_EXCLUSIVE]
1714** </ul>
1715**
1716** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1717** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
1718** will be set for TEMP databases and their journals, transient
1719** databases, and subjournals.
1720**
1721** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1722** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1723** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1724** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1725** SQLITE_OPEN_CREATE, is used to indicate that file should always
1726** be created, and that it is an error if it already exists.
1727** It is <i>not</i> used to indicate the file should be opened
1728** for exclusive access.
1729**
1730** ^At least szOsFile bytes of memory are allocated by SQLite
1731** to hold the [sqlite3_file] structure passed as the third
1732** argument to xOpen. The xOpen method does not have to
1733** allocate the structure; it should just fill it in. Note that
1734** the xOpen method must set the sqlite3_file.pMethods to either
1735** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
1736** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
1737** element will be valid after xOpen returns regardless of the success
1738** or failure of the xOpen call.
1739**
1740** [[sqlite3_vfs.xAccess]]
1741** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1742** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1743** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1744** to test whether a file is at least readable. The SQLITE_ACCESS_READ
1745** flag is never actually used and is not implemented in the built-in
1746** VFSes of SQLite. The file is named by the second argument and can be a
1747** directory. The xAccess method returns [SQLITE_OK] on success or some
1748** non-zero error code if there is an I/O error or if the name of
1749** the file given in the second argument is illegal. If SQLITE_OK
1750** is returned, then non-zero or zero is written into *pResOut to indicate
1751** whether or not the file is accessible.
1752**
1753** ^SQLite will always allocate at least mxPathname+1 bytes for the
1754** output buffer xFullPathname. The exact size of the output buffer
1755** is also passed as a parameter to both methods. If the output buffer
1756** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1757** handled as a fatal error by SQLite, vfs implementations should endeavor
1758** to prevent this by setting mxPathname to a sufficiently large value.
1759**
1760** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1761** interfaces are not strictly a part of the filesystem, but they are
1762** included in the VFS structure for completeness.
1763** The xRandomness() function attempts to return nBytes bytes
1764** of good-quality randomness into zOut. The return value is
1765** the actual number of bytes of randomness obtained.
1766** The xSleep() method causes the calling thread to sleep for at
1767** least the number of microseconds given. ^The xCurrentTime()
1768** method returns a Julian Day Number for the current date and time as
1769** a floating point value.
1770** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1771** Day Number multiplied by 86400000 (the number of milliseconds in
1772** a 24-hour day).
1773** ^SQLite will use the xCurrentTimeInt64() method to get the current
1774** date and time if that method is available (if iVersion is 2 or
1775** greater and the function pointer is not NULL) and will fall back
1776** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1777**
1778** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1779** are not used by the SQLite core. These optional interfaces are provided
1780** by some VFSes to facilitate testing of the VFS code. By overriding
1781** system calls with functions under its control, a test program can
1782** simulate faults and error conditions that would otherwise be difficult
1783** or impossible to induce. The set of system calls that can be overridden
1784** varies from one VFS to another, and from one version of the same VFS to the
1785** next. Applications that use these interfaces must be prepared for any
1786** or all of these interfaces to be NULL or for their behavior to change
1787** from one release to the next. Applications must not attempt to access
1788** any of these methods if the iVersion of the VFS is less than 3.
1789*/
1790typedef struct sqlite3_vfs sqlite3_vfs;
1791typedef void (*sqlite3_syscall_ptr)(void);
1792struct sqlite3_vfs {
1793 int iVersion; /* Structure version number (currently 3) */
1794 int szOsFile; /* Size of subclassed sqlite3_file */
1795 int mxPathname; /* Maximum file pathname length */
1796 sqlite3_vfs *pNext; /* Next registered VFS */
1797 const char *zName; /* Name of this virtual file system */
1798 void *pAppData; /* Pointer to application-specific data */
1799 int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*,
1800 int flags, int *pOutFlags);
1801 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1802 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1803 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1804 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1805 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1806 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1807 void (*xDlClose)(sqlite3_vfs*, void*);
1808 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1809 int (*xSleep)(sqlite3_vfs*, int microseconds);
1810 int (*xCurrentTime)(sqlite3_vfs*, double*);
1811 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1812 /*
1813 ** The methods above are in version 1 of the sqlite_vfs object
1814 ** definition. Those that follow are added in version 2 or later
1815 */
1816 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1817 /*
1818 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1819 ** Those below are for version 3 and greater.
1820 */
1821 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1822 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1823 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1824 /*
1825 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1826 ** New fields may be appended in future versions. The iVersion
1827 ** value will increment whenever this happens.
1828 */
1829};
1830
1831/*
1832** CAPI3REF: Flags for the xAccess VFS method
1833**
1834** These integer constants can be used as the third parameter to
1835** the xAccess method of an [sqlite3_vfs] object. They determine
1836** what kind of permissions the xAccess method is looking for.
1837** With SQLITE_ACCESS_EXISTS, the xAccess method
1838** simply checks whether the file exists.
1839** With SQLITE_ACCESS_READWRITE, the xAccess method
1840** checks whether the named directory is both readable and writable
1841** (in other words, if files can be added, removed, and renamed within
1842** the directory).
1843** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1844** [temp_store_directory pragma], though this could change in a future
1845** release of SQLite.
1846** With SQLITE_ACCESS_READ, the xAccess method
1847** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
1848** currently unused, though it might be used in a future release of
1849** SQLite.
1850*/
1851#define SQLITE_ACCESS_EXISTS0 0
1852#define SQLITE_ACCESS_READWRITE1 1 /* Used by PRAGMA temp_store_directory */
1853#define SQLITE_ACCESS_READ2 2 /* Unused */
1854
1855/*
1856** CAPI3REF: Flags for the xShmLock VFS method
1857**
1858** These integer constants define the various locking operations
1859** allowed by the xShmLock method of [sqlite3_io_methods]. The
1860** following are the only legal combinations of flags to the
1861** xShmLock method:
1862**
1863** <ul>
1864** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1865** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1866** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1867** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1868** </ul>
1869**
1870** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1871** was given on the corresponding lock.
1872**
1873** The xShmLock method can transition between unlocked and SHARED or
1874** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1875** and EXCLUSIVE.
1876*/
1877#define SQLITE_SHM_UNLOCK1 1
1878#define SQLITE_SHM_LOCK2 2
1879#define SQLITE_SHM_SHARED4 4
1880#define SQLITE_SHM_EXCLUSIVE8 8
1881
1882/*
1883** CAPI3REF: Maximum xShmLock index
1884**
1885** The xShmLock method on [sqlite3_io_methods] may use values
1886** between 0 and this upper bound as its "offset" argument.
1887** The SQLite core will never attempt to acquire or release a
1888** lock outside of this range
1889*/
1890#define SQLITE_SHM_NLOCK8 8
1891
1892
1893/*
1894** CAPI3REF: Initialize The SQLite Library
1895**
1896** ^The sqlite3_initialize() routine initializes the
1897** SQLite library. ^The sqlite3_shutdown() routine
1898** deallocates any resources that were allocated by sqlite3_initialize().
1899** These routines are designed to aid in process initialization and
1900** shutdown on embedded systems. Workstation applications using
1901** SQLite normally do not need to invoke either of these routines.
1902**
1903** A call to sqlite3_initialize() is an "effective" call if it is
1904** the first time sqlite3_initialize() is invoked during the lifetime of
1905** the process, or if it is the first time sqlite3_initialize() is invoked
1906** following a call to sqlite3_shutdown(). ^(Only an effective call
1907** of sqlite3_initialize() does any initialization. All other calls
1908** are harmless no-ops.)^
1909**
1910** A call to sqlite3_shutdown() is an "effective" call if it is the first
1911** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
1912** an effective call to sqlite3_shutdown() does any deinitialization.
1913** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1914**
1915** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1916** is not. The sqlite3_shutdown() interface must only be called from a
1917** single thread. All open [database connections] must be closed and all
1918** other SQLite resources must be deallocated prior to invoking
1919** sqlite3_shutdown().
1920**
1921** Among other things, ^sqlite3_initialize() will invoke
1922** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
1923** will invoke sqlite3_os_end().
1924**
1925** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1926** ^If for some reason, sqlite3_initialize() is unable to initialize
1927** the library (perhaps it is unable to allocate a needed resource such
1928** as a mutex) it returns an [error code] other than [SQLITE_OK].
1929**
1930** ^The sqlite3_initialize() routine is called internally by many other
1931** SQLite interfaces so that an application usually does not need to
1932** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
1933** calls sqlite3_initialize() so the SQLite library will be automatically
1934** initialized when [sqlite3_open()] is called if it has not be initialized
1935** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1936** compile-time option, then the automatic calls to sqlite3_initialize()
1937** are omitted and the application must call sqlite3_initialize() directly
1938** prior to using any other SQLite interface. For maximum portability,
1939** it is recommended that applications always invoke sqlite3_initialize()
1940** directly prior to using any other SQLite interface. Future releases
1941** of SQLite may require this. In other words, the behavior exhibited
1942** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1943** default behavior in some future release of SQLite.
1944**
1945** The sqlite3_os_init() routine does operating-system specific
1946** initialization of the SQLite library. The sqlite3_os_end()
1947** routine undoes the effect of sqlite3_os_init(). Typical tasks
1948** performed by these routines include allocation or deallocation
1949** of static resources, initialization of global variables,
1950** setting up a default [sqlite3_vfs] module, or setting up
1951** a default configuration using [sqlite3_config()].
1952**
1953** The application should never invoke either sqlite3_os_init()
1954** or sqlite3_os_end() directly. The application should only invoke
1955** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
1956** interface is called automatically by sqlite3_initialize() and
1957** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
1958** implementations for sqlite3_os_init() and sqlite3_os_end()
1959** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1960** When [custom builds | built for other platforms]
1961** (using the [SQLITE_OS_OTHER=1] compile-time
1962** option) the application must supply a suitable implementation for
1963** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1964** implementation of sqlite3_os_init() or sqlite3_os_end()
1965** must return [SQLITE_OK] on success and some other [error code] upon
1966** failure.
1967*/
1968SQLITE_API int sqlite3_initialize(void);
1969SQLITE_API int sqlite3_shutdown(void);
1970SQLITE_API int sqlite3_os_init(void);
1971SQLITE_API int sqlite3_os_end(void);
1972
1973/*
1974** CAPI3REF: Configuring The SQLite Library
1975**
1976** The sqlite3_config() interface is used to make global configuration
1977** changes to SQLite in order to tune SQLite to the specific needs of
1978** the application. The default configuration is recommended for most
1979** applications and so this routine is usually not necessary. It is
1980** provided to support rare applications with unusual needs.
1981**
1982** <b>The sqlite3_config() interface is not threadsafe. The application
1983** must ensure that no other SQLite interfaces are invoked by other
1984** threads while sqlite3_config() is running.</b>
1985**
1986** The first argument to sqlite3_config() is an integer
1987** [configuration option] that determines
1988** what property of SQLite is to be configured. Subsequent arguments
1989** vary depending on the [configuration option]
1990** in the first argument.
1991**
1992** For most configuration options, the sqlite3_config() interface
1993** may only be invoked prior to library initialization using
1994** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1995** The exceptional configuration options that may be invoked at any time
1996** are called "anytime configuration options".
1997** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1998** [sqlite3_shutdown()] with a first argument that is not an anytime
1999** configuration option, then the sqlite3_config() call will return SQLITE_MISUSE.
2000** Note, however, that ^sqlite3_config() can be called as part of the
2001** implementation of an application-defined [sqlite3_os_init()].
2002**
2003** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2004** ^If the option is unknown or SQLite is unable to set the option
2005** then this routine returns a non-zero [error code].
2006*/
2007SQLITE_API int sqlite3_config(int, ...);
2008
2009/*
2010** CAPI3REF: Configure database connections
2011** METHOD: sqlite3
2012**
2013** The sqlite3_db_config() interface is used to make configuration
2014** changes to a [database connection]. The interface is similar to
2015** [sqlite3_config()] except that the changes apply to a single
2016** [database connection] (specified in the first argument).
2017**
2018** The second argument to sqlite3_db_config(D,V,...) is the
2019** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2020** that indicates what aspect of the [database connection] is being configured.
2021** Subsequent arguments vary depending on the configuration verb.
2022**
2023** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2024** the call is considered successful.
2025*/
2026SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2027
2028/*
2029** CAPI3REF: Memory Allocation Routines
2030**
2031** An instance of this object defines the interface between SQLite
2032** and low-level memory allocation routines.
2033**
2034** This object is used in only one place in the SQLite interface.
2035** A pointer to an instance of this object is the argument to
2036** [sqlite3_config()] when the configuration option is
2037** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2038** By creating an instance of this object
2039** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2040** during configuration, an application can specify an alternative
2041** memory allocation subsystem for SQLite to use for all of its
2042** dynamic memory needs.
2043**
2044** Note that SQLite comes with several [built-in memory allocators]
2045** that are perfectly adequate for the overwhelming majority of applications
2046** and that this object is only useful to a tiny minority of applications
2047** with specialized memory allocation requirements. This object is
2048** also used during testing of SQLite in order to specify an alternative
2049** memory allocator that simulates memory out-of-memory conditions in
2050** order to verify that SQLite recovers gracefully from such
2051** conditions.
2052**
2053** The xMalloc, xRealloc, and xFree methods must work like the
2054** malloc(), realloc() and free() functions from the standard C library.
2055** ^SQLite guarantees that the second argument to
2056** xRealloc is always a value returned by a prior call to xRoundup.
2057**
2058** xSize should return the allocated size of a memory allocation
2059** previously obtained from xMalloc or xRealloc. The allocated size
2060** is always at least as big as the requested size but may be larger.
2061**
2062** The xRoundup method returns what would be the allocated size of
2063** a memory allocation given a particular requested size. Most memory
2064** allocators round up memory allocations at least to the next multiple
2065** of 8. Some allocators round up to a larger multiple or to a power of 2.
2066** Every memory allocation request coming in through [sqlite3_malloc()]
2067** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2068** that causes the corresponding memory allocation to fail.
2069**
2070** The xInit method initializes the memory allocator. For example,
2071** it might allocate any required mutexes or initialize internal data
2072** structures. The xShutdown method is invoked (indirectly) by
2073** [sqlite3_shutdown()] and should deallocate any resources acquired
2074** by xInit. The pAppData pointer is used as the only parameter to
2075** xInit and xShutdown.
2076**
2077** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
2078** the xInit method, so the xInit method need not be threadsafe. The
2079** xShutdown method is only called from [sqlite3_shutdown()] so it does
2080** not need to be threadsafe either. For all other methods, SQLite
2081** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2082** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2083** it is by default) and so the methods are automatically serialized.
2084** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2085** methods must be threadsafe or else make their own arrangements for
2086** serialization.
2087**
2088** SQLite will never invoke xInit() more than once without an intervening
2089** call to xShutdown().
2090*/
2091typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2092struct sqlite3_mem_methods {
2093 void *(*xMalloc)(int); /* Memory allocation function */
2094 void (*xFree)(void*); /* Free a prior allocation */
2095 void *(*xRealloc)(void*,int); /* Resize an allocation */
2096 int (*xSize)(void*); /* Return the size of an allocation */
2097 int (*xRoundup)(int); /* Round up request size to allocation size */
2098 int (*xInit)(void*); /* Initialize the memory allocator */
2099 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2100 void *pAppData; /* Argument to xInit() and xShutdown() */
2101};
2102
2103/*
2104** CAPI3REF: Configuration Options
2105** KEYWORDS: {configuration option}
2106**
2107** These constants are the available integer configuration options that
2108** can be passed as the first argument to the [sqlite3_config()] interface.
2109**
2110** Most of the configuration options for sqlite3_config()
2111** will only work if invoked prior to [sqlite3_initialize()] or after
2112** [sqlite3_shutdown()]. The few exceptions to this rule are called
2113** "anytime configuration options".
2114** ^Calling [sqlite3_config()] with a first argument that is not an
2115** anytime configuration option in between calls to [sqlite3_initialize()] and
2116** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2117**
2118** The set of anytime configuration options can change (by insertions
2119** and/or deletions) from one release of SQLite to the next.
2120** As of SQLite version 3.42.0, the complete set of anytime configuration
2121** options is:
2122** <ul>
2123** <li> SQLITE_CONFIG_LOG
2124** <li> SQLITE_CONFIG_PCACHE_HDRSZ
2125** </ul>
2126**
2127** New configuration options may be added in future releases of SQLite.
2128** Existing configuration options might be discontinued. Applications
2129** should check the return code from [sqlite3_config()] to make sure that
2130** the call worked. The [sqlite3_config()] interface will return a
2131** non-zero [error code] if a discontinued or unsupported configuration option
2132** is invoked.
2133**
2134** <dl>
2135** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2136** <dd>There are no arguments to this option. ^This option sets the
2137** [threading mode] to Single-thread. In other words, it disables
2138** all mutexing and puts SQLite into a mode where it can only be used
2139** by a single thread. ^If SQLite is compiled with
2140** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2141** it is not possible to change the [threading mode] from its default
2142** value of Single-thread and so [sqlite3_config()] will return
2143** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2144** configuration option.</dd>
2145**
2146** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2147** <dd>There are no arguments to this option. ^This option sets the
2148** [threading mode] to Multi-thread. In other words, it disables
2149** mutexing on [database connection] and [prepared statement] objects.
2150** The application is responsible for serializing access to
2151** [database connections] and [prepared statements]. But other mutexes
2152** are enabled so that SQLite will be safe to use in a multi-threaded
2153** environment as long as no two threads attempt to use the same
2154** [database connection] at the same time. ^If SQLite is compiled with
2155** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2156** it is not possible to set the Multi-thread [threading mode] and
2157** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2158** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2159**
2160** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2161** <dd>There are no arguments to this option. ^This option sets the
2162** [threading mode] to Serialized. In other words, this option enables
2163** all mutexes including the recursive
2164** mutexes on [database connection] and [prepared statement] objects.
2165** In this mode (which is the default when SQLite is compiled with
2166** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2167** to [database connections] and [prepared statements] so that the
2168** application is free to use the same [database connection] or the
2169** same [prepared statement] in different threads at the same time.
2170** ^If SQLite is compiled with
2171** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2172** it is not possible to set the Serialized [threading mode] and
2173** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2174** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2175**
2176** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2177** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2178** a pointer to an instance of the [sqlite3_mem_methods] structure.
2179** The argument specifies
2180** alternative low-level memory allocation routines to be used in place of
2181** the memory allocation routines built into SQLite.)^ ^SQLite makes
2182** its own private copy of the content of the [sqlite3_mem_methods] structure
2183** before the [sqlite3_config()] call returns.</dd>
2184**
2185** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2186** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2187** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2188** The [sqlite3_mem_methods]
2189** structure is filled with the currently defined memory allocation routines.)^
2190** This option can be used to overload the default memory allocation
2191** routines with a wrapper that simulations memory allocation failure or
2192** tracks memory usage, for example. </dd>
2193**
2194** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2195** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2196** type int, interpreted as a boolean, which if true provides a hint to
2197** SQLite that it should avoid large memory allocations if possible.
2198** SQLite will run faster if it is free to make large memory allocations,
2199** but some application might prefer to run slower in exchange for
2200** guarantees about memory fragmentation that are possible if large
2201** allocations are avoided. This hint is normally off.
2202** </dd>
2203**
2204** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2205** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2206** interpreted as a boolean, which enables or disables the collection of
2207** memory allocation statistics. ^(When memory allocation statistics are
2208** disabled, the following SQLite interfaces become non-operational:
2209** <ul>
2210** <li> [sqlite3_hard_heap_limit64()]
2211** <li> [sqlite3_memory_used()]
2212** <li> [sqlite3_memory_highwater()]
2213** <li> [sqlite3_soft_heap_limit64()]
2214** <li> [sqlite3_status64()]
2215** </ul>)^
2216** ^Memory allocation statistics are enabled by default unless SQLite is
2217** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2218** allocation statistics are disabled by default.
2219** </dd>
2220**
2221** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2222** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2223** </dd>
2224**
2225** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2226** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2227** that SQLite can use for the database page cache with the default page
2228** cache implementation.
2229** This configuration option is a no-op if an application-defined page
2230** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2231** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2232** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2233** and the number of cache lines (N).
2234** The sz argument should be the size of the largest database page
2235** (a power of two between 512 and 65536) plus some extra bytes for each
2236** page header. ^The number of extra bytes needed by the page header
2237** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2238** ^It is harmless, apart from the wasted memory,
2239** for the sz parameter to be larger than necessary. The pMem
2240** argument must be either a NULL pointer or a pointer to an 8-byte
2241** aligned block of memory of at least sz*N bytes, otherwise
2242** subsequent behavior is undefined.
2243** ^When pMem is not NULL, SQLite will strive to use the memory provided
2244** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2245** a page cache line is larger than sz bytes or if all of the pMem buffer
2246** is exhausted.
2247** ^If pMem is NULL and N is non-zero, then each database connection
2248** does an initial bulk allocation for page cache memory
2249** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2250** of -1024*N bytes if N is negative, . ^If additional
2251** page cache memory is needed beyond what is provided by the initial
2252** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2253** additional cache line. </dd>
2254**
2255** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2256** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2257** that SQLite will use for all of its dynamic memory allocation needs
2258** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2259** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2260** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2261** [SQLITE_ERROR] if invoked otherwise.
2262** ^There are three arguments to SQLITE_CONFIG_HEAP:
2263** An 8-byte aligned pointer to the memory,
2264** the number of bytes in the memory buffer, and the minimum allocation size.
2265** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2266** to using its default memory allocator (the system malloc() implementation),
2267** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2268** memory pointer is not NULL then the alternative memory
2269** allocator is engaged to handle all of SQLites memory allocation needs.
2270** The first pointer (the memory pointer) must be aligned to an 8-byte
2271** boundary or subsequent behavior of SQLite will be undefined.
2272** The minimum allocation size is capped at 2**12. Reasonable values
2273** for the minimum allocation size are 2**5 through 2**8.</dd>
2274**
2275** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2276** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2277** pointer to an instance of the [sqlite3_mutex_methods] structure.
2278** The argument specifies alternative low-level mutex routines to be used
2279** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2280** the content of the [sqlite3_mutex_methods] structure before the call to
2281** [sqlite3_config()] returns. ^If SQLite is compiled with
2282** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2283** the entire mutexing subsystem is omitted from the build and hence calls to
2284** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2285** return [SQLITE_ERROR].</dd>
2286**
2287** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2288** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2289** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2290** [sqlite3_mutex_methods]
2291** structure is filled with the currently defined mutex routines.)^
2292** This option can be used to overload the default mutex allocation
2293** routines with a wrapper used to track mutex usage for performance
2294** profiling or testing, for example. ^If SQLite is compiled with
2295** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2296** the entire mutexing subsystem is omitted from the build and hence calls to
2297** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2298** return [SQLITE_ERROR].</dd>
2299**
2300** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2301** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2302** the default size of lookaside memory on each [database connection].
2303** The first argument is the
2304** size of each lookaside buffer slot and the second is the number of
2305** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2306** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2307** option to [sqlite3_db_config()] can be used to change the lookaside
2308** configuration on individual connections.)^ </dd>
2309**
2310** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2311** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2312** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2313** the interface to a custom page cache implementation.)^
2314** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2315**
2316** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2317** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2318** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2319** the current page cache implementation into that object.)^ </dd>
2320**
2321** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2322** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2323** global [error log].
2324** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2325** function with a call signature of void(*)(void*,int,const char*),
2326** and a pointer to void. ^If the function pointer is not NULL, it is
2327** invoked by [sqlite3_log()] to process each logging event. ^If the
2328** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2329** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2330** passed through as the first parameter to the application-defined logger
2331** function whenever that function is invoked. ^The second parameter to
2332** the logger function is a copy of the first parameter to the corresponding
2333** [sqlite3_log()] call and is intended to be a [result code] or an
2334** [extended result code]. ^The third parameter passed to the logger is
2335** log message after formatting via [sqlite3_snprintf()].
2336** The SQLite logging interface is not reentrant; the logger function
2337** supplied by the application must not invoke any SQLite interface.
2338** In a multi-threaded application, the application-defined logger
2339** function must be threadsafe. </dd>
2340**
2341** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2342** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2343** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2344** then URI handling is globally disabled.)^ ^If URI handling is globally
2345** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2346** [sqlite3_open16()] or
2347** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2348** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2349** connection is opened. ^If it is globally disabled, filenames are
2350** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2351** database connection is opened. ^(By default, URI handling is globally
2352** disabled. The default value may be changed by compiling with the
2353** [SQLITE_USE_URI] symbol defined.)^
2354**
2355** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2356** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2357** argument which is interpreted as a boolean in order to enable or disable
2358** the use of covering indices for full table scans in the query optimizer.
2359** ^The default setting is determined
2360** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2361** if that compile-time option is omitted.
2362** The ability to disable the use of covering indices for full table scans
2363** is because some incorrectly coded legacy applications might malfunction
2364** when the optimization is enabled. Providing the ability to
2365** disable the optimization allows the older, buggy application code to work
2366** without change even with newer versions of SQLite.
2367**
2368** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2369** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2370** <dd> These options are obsolete and should not be used by new code.
2371** They are retained for backwards compatibility but are now no-ops.
2372** </dd>
2373**
2374** [[SQLITE_CONFIG_SQLLOG]]
2375** <dt>SQLITE_CONFIG_SQLLOG
2376** <dd>This option is only available if sqlite is compiled with the
2377** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2378** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2379** The second should be of type (void*). The callback is invoked by the library
2380** in three separate circumstances, identified by the value passed as the
2381** fourth parameter. If the fourth parameter is 0, then the database connection
2382** passed as the second argument has just been opened. The third argument
2383** points to a buffer containing the name of the main database file. If the
2384** fourth parameter is 1, then the SQL statement that the third parameter
2385** points to has just been executed. Or, if the fourth parameter is 2, then
2386** the connection being passed as the second parameter is being closed. The
2387** third parameter is passed NULL In this case. An example of using this
2388** configuration option can be seen in the "test_sqllog.c" source file in
2389** the canonical SQLite source tree.</dd>
2390**
2391** [[SQLITE_CONFIG_MMAP_SIZE]]
2392** <dt>SQLITE_CONFIG_MMAP_SIZE
2393** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2394** that are the default mmap size limit (the default setting for
2395** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2396** ^The default setting can be overridden by each database connection using
2397** either the [PRAGMA mmap_size] command, or by using the
2398** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2399** will be silently truncated if necessary so that it does not exceed the
2400** compile-time maximum mmap size set by the
2401** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2402** ^If either argument to this option is negative, then that argument is
2403** changed to its compile-time default.
2404**
2405** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2406** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2407** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2408** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2409** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2410** that specifies the maximum size of the created heap.
2411**
2412** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2413** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2414** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2415** is a pointer to an integer and writes into that integer the number of extra
2416** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2417** The amount of extra space required can change depending on the compiler,
2418** target platform, and SQLite version.
2419**
2420** [[SQLITE_CONFIG_PMASZ]]
2421** <dt>SQLITE_CONFIG_PMASZ
2422** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2423** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2424** sorter to that integer. The default minimum PMA Size is set by the
2425** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2426** to help with sort operations when multithreaded sorting
2427** is enabled (using the [PRAGMA threads] command) and the amount of content
2428** to be sorted exceeds the page size times the minimum of the
2429** [PRAGMA cache_size] setting and this value.
2430**
2431** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2432** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2433** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2434** becomes the [statement journal] spill-to-disk threshold.
2435** [Statement journals] are held in memory until their size (in bytes)
2436** exceeds this threshold, at which point they are written to disk.
2437** Or if the threshold is -1, statement journals are always held
2438** exclusively in memory.
2439** Since many statement journals never become large, setting the spill
2440** threshold to a value such as 64KiB can greatly reduce the amount of
2441** I/O required to support statement rollback.
2442** The default value for this setting is controlled by the
2443** [SQLITE_STMTJRNL_SPILL] compile-time option.
2444**
2445** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2446** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2447** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2448** of type (int) - the new value of the sorter-reference size threshold.
2449** Usually, when SQLite uses an external sort to order records according
2450** to an ORDER BY clause, all fields required by the caller are present in the
2451** sorted records. However, if SQLite determines based on the declared type
2452** of a table column that its values are likely to be very large - larger
2453** than the configured sorter-reference size threshold - then a reference
2454** is stored in each sorted record and the required column values loaded
2455** from the database as records are returned in sorted order. The default
2456** value for this option is to never use this optimization. Specifying a
2457** negative value for this option restores the default behavior.
2458** This option is only available if SQLite is compiled with the
2459** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2460**
2461** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
2462** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
2463** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
2464** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2465** database created using [sqlite3_deserialize()]. This default maximum
2466** size can be adjusted up or down for individual databases using the
2467** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2468** configuration setting is never used, then the default maximum is determined
2469** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2470** compile-time option is not set, then the default maximum is 1073741824.
2471**
2472** [[SQLITE_CONFIG_ROWID_IN_VIEW]]
2473** <dt>SQLITE_CONFIG_ROWID_IN_VIEW
2474** <dd>The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability
2475** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is
2476** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2477** defaults to on. This configuration option queries the current setting or
2478** changes the setting to off or on. The argument is a pointer to an integer.
2479** If that integer initially holds a value of 1, then the ability for VIEWs to
2480** have ROWIDs is activated. If the integer initially holds zero, then the
2481** ability is deactivated. Any other initial value for the integer leaves the
2482** setting unchanged. After changes, if any, the integer is written with
2483** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite
2484** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2485** recommended case) then the integer is always filled with zero, regardless
2486** if its initial value.
2487** </dl>
2488*/
2489#define SQLITE_CONFIG_SINGLETHREAD1 1 /* nil */
2490#define SQLITE_CONFIG_MULTITHREAD2 2 /* nil */
2491#define SQLITE_CONFIG_SERIALIZED3 3 /* nil */
2492#define SQLITE_CONFIG_MALLOC4 4 /* sqlite3_mem_methods* */
2493#define SQLITE_CONFIG_GETMALLOC5 5 /* sqlite3_mem_methods* */
2494#define SQLITE_CONFIG_SCRATCH6 6 /* No longer used */
2495#define SQLITE_CONFIG_PAGECACHE7 7 /* void*, int sz, int N */
2496#define SQLITE_CONFIG_HEAP8 8 /* void*, int nByte, int min */
2497#define SQLITE_CONFIG_MEMSTATUS9 9 /* boolean */
2498#define SQLITE_CONFIG_MUTEX10 10 /* sqlite3_mutex_methods* */
2499#define SQLITE_CONFIG_GETMUTEX11 11 /* sqlite3_mutex_methods* */
2500/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2501#define SQLITE_CONFIG_LOOKASIDE13 13 /* int int */
2502#define SQLITE_CONFIG_PCACHE14 14 /* no-op */
2503#define SQLITE_CONFIG_GETPCACHE15 15 /* no-op */
2504#define SQLITE_CONFIG_LOG16 16 /* xFunc, void* */
2505#define SQLITE_CONFIG_URI17 17 /* int */
2506#define SQLITE_CONFIG_PCACHE218 18 /* sqlite3_pcache_methods2* */
2507#define SQLITE_CONFIG_GETPCACHE219 19 /* sqlite3_pcache_methods2* */
2508#define SQLITE_CONFIG_COVERING_INDEX_SCAN20 20 /* int */
2509#define SQLITE_CONFIG_SQLLOG21 21 /* xSqllog, void* */
2510#define SQLITE_CONFIG_MMAP_SIZE22 22 /* sqlite3_int64, sqlite3_int64 */
2511#define SQLITE_CONFIG_WIN32_HEAPSIZE23 23 /* int nByte */
2512#define SQLITE_CONFIG_PCACHE_HDRSZ24 24 /* int *psz */
2513#define SQLITE_CONFIG_PMASZ25 25 /* unsigned int szPma */
2514#define SQLITE_CONFIG_STMTJRNL_SPILL26 26 /* int nByte */
2515#define SQLITE_CONFIG_SMALL_MALLOC27 27 /* boolean */
2516#define SQLITE_CONFIG_SORTERREF_SIZE28 28 /* int nByte */
2517#define SQLITE_CONFIG_MEMDB_MAXSIZE29 29 /* sqlite3_int64 */
2518#define SQLITE_CONFIG_ROWID_IN_VIEW30 30 /* int* */
2519
2520/*
2521** CAPI3REF: Database Connection Configuration Options
2522**
2523** These constants are the available integer configuration options that
2524** can be passed as the second argument to the [sqlite3_db_config()] interface.
2525**
2526** New configuration options may be added in future releases of SQLite.
2527** Existing configuration options might be discontinued. Applications
2528** should check the return code from [sqlite3_db_config()] to make sure that
2529** the call worked. ^The [sqlite3_db_config()] interface will return a
2530** non-zero [error code] if a discontinued or unsupported configuration option
2531** is invoked.
2532**
2533** <dl>
2534** [[SQLITE_DBCONFIG_LOOKASIDE]]
2535** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2536** <dd> ^This option takes three additional arguments that determine the
2537** [lookaside memory allocator] configuration for the [database connection].
2538** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2539** pointer to a memory buffer to use for lookaside memory.
2540** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2541** may be NULL in which case SQLite will allocate the
2542** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2543** size of each lookaside buffer slot. ^The third argument is the number of
2544** slots. The size of the buffer in the first argument must be greater than
2545** or equal to the product of the second and third arguments. The buffer
2546** must be aligned to an 8-byte boundary. ^If the second argument to
2547** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2548** rounded down to the next smaller multiple of 8. ^(The lookaside memory
2549** configuration for a database connection can only be changed when that
2550** connection is not currently using lookaside memory, or in other words
2551** when the "current value" returned by
2552** [sqlite3_db_status](D,[SQLITE_DBSTATUS_LOOKASIDE_USED],...) is zero.
2553** Any attempt to change the lookaside memory configuration when lookaside
2554** memory is in use leaves the configuration unchanged and returns
2555** [SQLITE_BUSY].)^</dd>
2556**
2557** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2558** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2559** <dd> ^This option is used to enable or disable the enforcement of
2560** [foreign key constraints]. There should be two additional arguments.
2561** The first argument is an integer which is 0 to disable FK enforcement,
2562** positive to enable FK enforcement or negative to leave FK enforcement
2563** unchanged. The second parameter is a pointer to an integer into which
2564** is written 0 or 1 to indicate whether FK enforcement is off or on
2565** following this call. The second parameter may be a NULL pointer, in
2566** which case the FK enforcement setting is not reported back. </dd>
2567**
2568** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
2569** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2570** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2571** There should be two additional arguments.
2572** The first argument is an integer which is 0 to disable triggers,
2573** positive to enable triggers or negative to leave the setting unchanged.
2574** The second parameter is a pointer to an integer into which
2575** is written 0 or 1 to indicate whether triggers are disabled or enabled
2576** following this call. The second parameter may be a NULL pointer, in
2577** which case the trigger setting is not reported back.
2578**
2579** <p>Originally this option disabled all triggers. ^(However, since
2580** SQLite version 3.35.0, TEMP triggers are still allowed even if
2581** this option is off. So, in other words, this option now only disables
2582** triggers in the main database schema or in the schemas of ATTACH-ed
2583** databases.)^ </dd>
2584**
2585** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
2586** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
2587** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
2588** There should be two additional arguments.
2589** The first argument is an integer which is 0 to disable views,
2590** positive to enable views or negative to leave the setting unchanged.
2591** The second parameter is a pointer to an integer into which
2592** is written 0 or 1 to indicate whether views are disabled or enabled
2593** following this call. The second parameter may be a NULL pointer, in
2594** which case the view setting is not reported back.
2595**
2596** <p>Originally this option disabled all views. ^(However, since
2597** SQLite version 3.35.0, TEMP views are still allowed even if
2598** this option is off. So, in other words, this option now only disables
2599** views in the main database schema or in the schemas of ATTACH-ed
2600** databases.)^ </dd>
2601**
2602** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
2603** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2604** <dd> ^This option is used to enable or disable the
2605** [fts3_tokenizer()] function which is part of the
2606** [FTS3] full-text search engine extension.
2607** There should be two additional arguments.
2608** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2609** positive to enable fts3_tokenizer() or negative to leave the setting
2610** unchanged.
2611** The second parameter is a pointer to an integer into which
2612** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2613** following this call. The second parameter may be a NULL pointer, in
2614** which case the new setting is not reported back. </dd>
2615**
2616** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
2617** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
2618** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
2619** interface independently of the [load_extension()] SQL function.
2620** The [sqlite3_enable_load_extension()] API enables or disables both the
2621** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2622** There should be two additional arguments.
2623** When the first argument to this interface is 1, then only the C-API is
2624** enabled and the SQL function remains disabled. If the first argument to
2625** this interface is 0, then both the C-API and the SQL function are disabled.
2626** If the first argument is -1, then no changes are made to state of either the
2627** C-API or the SQL function.
2628** The second parameter is a pointer to an integer into which
2629** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
2630** is disabled or enabled following this call. The second parameter may
2631** be a NULL pointer, in which case the new setting is not reported back.
2632** </dd>
2633**
2634** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
2635** <dd> ^This option is used to change the name of the "main" database
2636** schema. ^The sole argument is a pointer to a constant UTF8 string
2637** which will become the new schema name in place of "main". ^SQLite
2638** does not make a copy of the new main schema name string, so the application
2639** must ensure that the argument passed into this DBCONFIG option is unchanged
2640** until after the database connection closes.
2641** </dd>
2642**
2643** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
2644** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
2645** <dd> Usually, when a database in wal mode is closed or detached from a
2646** database handle, SQLite checks if this will mean that there are now no
2647** connections at all to the database. If so, it performs a checkpoint
2648** operation before closing the connection. This option may be used to
2649** override this behavior. The first parameter passed to this operation
2650** is an integer - positive to disable checkpoints-on-close, or zero (the
2651** default) to enable them, and negative to leave the setting unchanged.
2652** The second parameter is a pointer to an integer
2653** into which is written 0 or 1 to indicate whether checkpoints-on-close
2654** have been disabled - 0 if they are not disabled, 1 if they are.
2655** </dd>
2656**
2657** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
2658** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
2659** the [query planner stability guarantee] (QPSG). When the QPSG is active,
2660** a single SQL query statement will always use the same algorithm regardless
2661** of values of [bound parameters].)^ The QPSG disables some query optimizations
2662** that look at the values of bound parameters, which can make some queries
2663** slower. But the QPSG has the advantage of more predictable behavior. With
2664** the QPSG active, SQLite will always use the same query plan in the field as
2665** was used during testing in the lab.
2666** The first argument to this setting is an integer which is 0 to disable
2667** the QPSG, positive to enable QPSG, or negative to leave the setting
2668** unchanged. The second parameter is a pointer to an integer into which
2669** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
2670** following this call.
2671** </dd>
2672**
2673** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
2674** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
2675** include output for any operations performed by trigger programs. This
2676** option is used to set or clear (the default) a flag that governs this
2677** behavior. The first parameter passed to this operation is an integer -
2678** positive to enable output for trigger programs, or zero to disable it,
2679** or negative to leave the setting unchanged.
2680** The second parameter is a pointer to an integer into which is written
2681** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2682** it is not disabled, 1 if it is.
2683** </dd>
2684**
2685** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2686** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2687** [VACUUM] in order to reset a database back to an empty database
2688** with no schema and no content. The following process works even for
2689** a badly corrupted database file:
2690** <ol>
2691** <li> If the database connection is newly opened, make sure it has read the
2692** database schema by preparing then discarding some query against the
2693** database, or calling sqlite3_table_column_metadata(), ignoring any
2694** errors. This step is only necessary if the application desires to keep
2695** the database in WAL mode after the reset if it was in WAL mode before
2696** the reset.
2697** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2698** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2699** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2700** </ol>
2701** Because resetting a database is destructive and irreversible, the
2702** process requires the use of this obscure API and multiple steps to
2703** help ensure that it does not happen by accident. Because this
2704** feature must be capable of resetting corrupt databases, and
2705** shutting down virtual tables may require access to that corrupt
2706** storage, the library must abandon any installed virtual tables
2707** without calling their xDestroy() methods.
2708**
2709** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
2710** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
2711** "defensive" flag for a database connection. When the defensive
2712** flag is enabled, language features that allow ordinary SQL to
2713** deliberately corrupt the database file are disabled. The disabled
2714** features include but are not limited to the following:
2715** <ul>
2716** <li> The [PRAGMA writable_schema=ON] statement.
2717** <li> The [PRAGMA journal_mode=OFF] statement.
2718** <li> The [PRAGMA schema_version=N] statement.
2719** <li> Writes to the [sqlite_dbpage] virtual table.
2720** <li> Direct writes to [shadow tables].
2721** </ul>
2722** </dd>
2723**
2724** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
2725** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
2726** "writable_schema" flag. This has the same effect and is logically equivalent
2727** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
2728** The first argument to this setting is an integer which is 0 to disable
2729** the writable_schema, positive to enable writable_schema, or negative to
2730** leave the setting unchanged. The second parameter is a pointer to an
2731** integer into which is written 0 or 1 to indicate whether the writable_schema
2732** is enabled or disabled following this call.
2733** </dd>
2734**
2735** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
2736** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
2737** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
2738** the legacy behavior of the [ALTER TABLE RENAME] command such it
2739** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2740** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
2741** additional information. This feature can also be turned on and off
2742** using the [PRAGMA legacy_alter_table] statement.
2743** </dd>
2744**
2745** [[SQLITE_DBCONFIG_DQS_DML]]
2746** <dt>SQLITE_DBCONFIG_DQS_DML</dt>
2747** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
2748** the legacy [double-quoted string literal] misfeature for DML statements
2749** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
2750** default value of this setting is determined by the [-DSQLITE_DQS]
2751** compile-time option.
2752** </dd>
2753**
2754** [[SQLITE_DBCONFIG_DQS_DDL]]
2755** <dt>SQLITE_DBCONFIG_DQS_DDL</dt>
2756** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
2757** the legacy [double-quoted string literal] misfeature for DDL statements,
2758** such as CREATE TABLE and CREATE INDEX. The
2759** default value of this setting is determined by the [-DSQLITE_DQS]
2760** compile-time option.
2761** </dd>
2762**
2763** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
2764** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</dt>
2765** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
2766** assume that database schemas are untainted by malicious content.
2767** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
2768** takes additional defensive steps to protect the application from harm
2769** including:
2770** <ul>
2771** <li> Prohibit the use of SQL functions inside triggers, views,
2772** CHECK constraints, DEFAULT clauses, expression indexes,
2773** partial indexes, or generated columns
2774** unless those functions are tagged with [SQLITE_INNOCUOUS].
2775** <li> Prohibit the use of virtual tables inside of triggers or views
2776** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
2777** </ul>
2778** This setting defaults to "on" for legacy compatibility, however
2779** all applications are advised to turn it off if possible. This setting
2780** can also be controlled using the [PRAGMA trusted_schema] statement.
2781** </dd>
2782**
2783** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
2784** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</dt>
2785** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
2786** the legacy file format flag. When activated, this flag causes all newly
2787** created database file to have a schema format version number (the 4-byte
2788** integer found at offset 44 into the database header) of 1. This in turn
2789** means that the resulting database file will be readable and writable by
2790** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting,
2791** newly created databases are generally not understandable by SQLite versions
2792** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there
2793** is now scarcely any need to generate database files that are compatible
2794** all the way back to version 3.0.0, and so this setting is of little
2795** practical use, but is provided so that SQLite can continue to claim the
2796** ability to generate new database files that are compatible with version
2797** 3.0.0.
2798** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
2799** the [VACUUM] command will fail with an obscure error when attempting to
2800** process a table with generated columns and a descending index. This is
2801** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2802** either generated columns or descending indexes.
2803** </dd>
2804**
2805** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]]
2806** <dt>SQLITE_DBCONFIG_STMT_SCANSTATUS</dt>
2807** <dd>The SQLITE_DBCONFIG_STMT_SCANSTATUS option is only useful in
2808** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears
2809** a flag that enables collection of the sqlite3_stmt_scanstatus_v2()
2810** statistics. For statistics to be collected, the flag must be set on
2811** the database handle both when the SQL statement is prepared and when it
2812** is stepped. The flag is set (collection of statistics is enabled)
2813** by default. This option takes two arguments: an integer and a pointer to
2814** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2815** leave unchanged the statement scanstatus option. If the second argument
2816** is not NULL, then the value of the statement scanstatus setting after
2817** processing the first argument is written into the integer that the second
2818** argument points to.
2819** </dd>
2820**
2821** [[SQLITE_DBCONFIG_REVERSE_SCANORDER]]
2822** <dt>SQLITE_DBCONFIG_REVERSE_SCANORDER</dt>
2823** <dd>The SQLITE_DBCONFIG_REVERSE_SCANORDER option changes the default order
2824** in which tables and indexes are scanned so that the scans start at the end
2825** and work toward the beginning rather than starting at the beginning and
2826** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the
2827** same as setting [PRAGMA reverse_unordered_selects]. This option takes
2828** two arguments which are an integer and a pointer to an integer. The first
2829** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2830** reverse scan order flag, respectively. If the second argument is not NULL,
2831** then 0 or 1 is written into the integer that the second argument points to
2832** depending on if the reverse scan order flag is set after processing the
2833** first argument.
2834** </dd>
2835**
2836** </dl>
2837*/
2838#define SQLITE_DBCONFIG_MAINDBNAME1000 1000 /* const char* */
2839#define SQLITE_DBCONFIG_LOOKASIDE1001 1001 /* void* int int */
2840#define SQLITE_DBCONFIG_ENABLE_FKEY1002 1002 /* int int* */
2841#define SQLITE_DBCONFIG_ENABLE_TRIGGER1003 1003 /* int int* */
2842#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004 1004 /* int int* */
2843#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005 1005 /* int int* */
2844#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006 1006 /* int int* */
2845#define SQLITE_DBCONFIG_ENABLE_QPSG1007 1007 /* int int* */
2846#define SQLITE_DBCONFIG_TRIGGER_EQP1008 1008 /* int int* */
2847#define SQLITE_DBCONFIG_RESET_DATABASE1009 1009 /* int int* */
2848#define SQLITE_DBCONFIG_DEFENSIVE1010 1010 /* int int* */
2849#define SQLITE_DBCONFIG_WRITABLE_SCHEMA1011 1011 /* int int* */
2850#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012 1012 /* int int* */
2851#define SQLITE_DBCONFIG_DQS_DML1013 1013 /* int int* */
2852#define SQLITE_DBCONFIG_DQS_DDL1014 1014 /* int int* */
2853#define SQLITE_DBCONFIG_ENABLE_VIEW1015 1015 /* int int* */
2854#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT1016 1016 /* int int* */
2855#define SQLITE_DBCONFIG_TRUSTED_SCHEMA1017 1017 /* int int* */
2856#define SQLITE_DBCONFIG_STMT_SCANSTATUS1018 1018 /* int int* */
2857#define SQLITE_DBCONFIG_REVERSE_SCANORDER1019 1019 /* int int* */
2858#define SQLITE_DBCONFIG_MAX1019 1019 /* Largest DBCONFIG */
2859
2860/*
2861** CAPI3REF: Enable Or Disable Extended Result Codes
2862** METHOD: sqlite3
2863**
2864** ^The sqlite3_extended_result_codes() routine enables or disables the
2865** [extended result codes] feature of SQLite. ^The extended result
2866** codes are disabled by default for historical compatibility.
2867*/
2868SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2869
2870/*
2871** CAPI3REF: Last Insert Rowid
2872** METHOD: sqlite3
2873**
2874** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
2875** has a unique 64-bit signed
2876** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2877** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2878** names are not also used by explicitly declared columns. ^If
2879** the table has a column of type [INTEGER PRIMARY KEY] then that column
2880** is another alias for the rowid.
2881**
2882** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2883** the most recent successful [INSERT] into a rowid table or [virtual table]
2884** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2885** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2886** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2887** zero.
2888**
2889** As well as being set automatically as rows are inserted into database
2890** tables, the value returned by this function may be set explicitly by
2891** [sqlite3_set_last_insert_rowid()]
2892**
2893** Some virtual table implementations may INSERT rows into rowid tables as
2894** part of committing a transaction (e.g. to flush data accumulated in memory
2895** to disk). In this case subsequent calls to this function return the rowid
2896** associated with these internal INSERT operations, which leads to
2897** unintuitive results. Virtual table implementations that do write to rowid
2898** tables in this way can avoid this problem by restoring the original
2899** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2900** control to the user.
2901**
2902** ^(If an [INSERT] occurs within a trigger then this routine will
2903** return the [rowid] of the inserted row as long as the trigger is
2904** running. Once the trigger program ends, the value returned
2905** by this routine reverts to what it was before the trigger was fired.)^
2906**
2907** ^An [INSERT] that fails due to a constraint violation is not a
2908** successful [INSERT] and does not change the value returned by this
2909** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2910** and INSERT OR ABORT make no changes to the return value of this
2911** routine when their insertion fails. ^(When INSERT OR REPLACE
2912** encounters a constraint violation, it does not fail. The
2913** INSERT continues to completion after deleting rows that caused
2914** the constraint problem so INSERT OR REPLACE will always change
2915** the return value of this interface.)^
2916**
2917** ^For the purposes of this routine, an [INSERT] is considered to
2918** be successful even if it is subsequently rolled back.
2919**
2920** This function is accessible to SQL statements via the
2921** [last_insert_rowid() SQL function].
2922**
2923** If a separate thread performs a new [INSERT] on the same
2924** database connection while the [sqlite3_last_insert_rowid()]
2925** function is running and thus changes the last insert [rowid],
2926** then the value returned by [sqlite3_last_insert_rowid()] is
2927** unpredictable and might not equal either the old or the new
2928** last insert [rowid].
2929*/
2930SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2931
2932/*
2933** CAPI3REF: Set the Last Insert Rowid value.
2934** METHOD: sqlite3
2935**
2936** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2937** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2938** without inserting a row into the database.
2939*/
2940SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2941
2942/*
2943** CAPI3REF: Count The Number Of Rows Modified
2944** METHOD: sqlite3
2945**
2946** ^These functions return the number of rows modified, inserted or
2947** deleted by the most recently completed INSERT, UPDATE or DELETE
2948** statement on the database connection specified by the only parameter.
2949** The two functions are identical except for the type of the return value
2950** and that if the number of rows modified by the most recent INSERT, UPDATE
2951** or DELETE is greater than the maximum value supported by type "int", then
2952** the return value of sqlite3_changes() is undefined. ^Executing any other
2953** type of SQL statement does not modify the value returned by these functions.
2954**
2955** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2956** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2957** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2958**
2959** Changes to a view that are intercepted by
2960** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2961** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2962** DELETE statement run on a view is always zero. Only changes made to real
2963** tables are counted.
2964**
2965** Things are more complicated if the sqlite3_changes() function is
2966** executed while a trigger program is running. This may happen if the
2967** program uses the [changes() SQL function], or if some other callback
2968** function invokes sqlite3_changes() directly. Essentially:
2969**
2970** <ul>
2971** <li> ^(Before entering a trigger program the value returned by
2972** sqlite3_changes() function is saved. After the trigger program
2973** has finished, the original value is restored.)^
2974**
2975** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2976** statement sets the value returned by sqlite3_changes()
2977** upon completion as normal. Of course, this value will not include
2978** any changes performed by sub-triggers, as the sqlite3_changes()
2979** value will be saved and restored after each sub-trigger has run.)^
2980** </ul>
2981**
2982** ^This means that if the changes() SQL function (or similar) is used
2983** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2984** returns the value as set when the calling statement began executing.
2985** ^If it is used by the second or subsequent such statement within a trigger
2986** program, the value returned reflects the number of rows modified by the
2987** previous INSERT, UPDATE or DELETE statement within the same trigger.
2988**
2989** If a separate thread makes changes on the same database connection
2990** while [sqlite3_changes()] is running then the value returned
2991** is unpredictable and not meaningful.
2992**
2993** See also:
2994** <ul>
2995** <li> the [sqlite3_total_changes()] interface
2996** <li> the [count_changes pragma]
2997** <li> the [changes() SQL function]
2998** <li> the [data_version pragma]
2999** </ul>
3000*/
3001SQLITE_API int sqlite3_changes(sqlite3*);
3002SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3*);
3003
3004/*
3005** CAPI3REF: Total Number Of Rows Modified
3006** METHOD: sqlite3
3007**
3008** ^These functions return the total number of rows inserted, modified or
3009** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3010** since the database connection was opened, including those executed as
3011** part of trigger programs. The two functions are identical except for the
3012** type of the return value and that if the number of rows modified by the
3013** connection exceeds the maximum value supported by type "int", then
3014** the return value of sqlite3_total_changes() is undefined. ^Executing
3015** any other type of SQL statement does not affect the value returned by
3016** sqlite3_total_changes().
3017**
3018** ^Changes made as part of [foreign key actions] are included in the
3019** count, but those made as part of REPLACE constraint resolution are
3020** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3021** are not counted.
3022**
3023** The [sqlite3_total_changes(D)] interface only reports the number
3024** of rows that changed due to SQL statement run against database
3025** connection D. Any changes by other database connections are ignored.
3026** To detect changes against a database file from other database
3027** connections use the [PRAGMA data_version] command or the
3028** [SQLITE_FCNTL_DATA_VERSION] [file control].
3029**
3030** If a separate thread makes changes on the same database connection
3031** while [sqlite3_total_changes()] is running then the value
3032** returned is unpredictable and not meaningful.
3033**
3034** See also:
3035** <ul>
3036** <li> the [sqlite3_changes()] interface
3037** <li> the [count_changes pragma]
3038** <li> the [changes() SQL function]
3039** <li> the [data_version pragma]
3040** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3041** </ul>
3042*/
3043SQLITE_API int sqlite3_total_changes(sqlite3*);
3044SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
3045
3046/*
3047** CAPI3REF: Interrupt A Long-Running Query
3048** METHOD: sqlite3
3049**
3050** ^This function causes any pending database operation to abort and
3051** return at its earliest opportunity. This routine is typically
3052** called in response to a user action such as pressing "Cancel"
3053** or Ctrl-C where the user wants a long query operation to halt
3054** immediately.
3055**
3056** ^It is safe to call this routine from a thread different from the
3057** thread that is currently running the database operation. But it
3058** is not safe to call this routine with a [database connection] that
3059** is closed or might close before sqlite3_interrupt() returns.
3060**
3061** ^If an SQL operation is very nearly finished at the time when
3062** sqlite3_interrupt() is called, then it might not have an opportunity
3063** to be interrupted and might continue to completion.
3064**
3065** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3066** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3067** that is inside an explicit transaction, then the entire transaction
3068** will be rolled back automatically.
3069**
3070** ^The sqlite3_interrupt(D) call is in effect until all currently running
3071** SQL statements on [database connection] D complete. ^Any new SQL statements
3072** that are started after the sqlite3_interrupt() call and before the
3073** running statement count reaches zero are interrupted as if they had been
3074** running prior to the sqlite3_interrupt() call. ^New SQL statements
3075** that are started after the running statement count reaches zero are
3076** not effected by the sqlite3_interrupt().
3077** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3078** SQL statements is a no-op and has no effect on SQL statements
3079** that are started after the sqlite3_interrupt() call returns.
3080**
3081** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether
3082** or not an interrupt is currently in effect for [database connection] D.
3083** It returns 1 if an interrupt is currently in effect, or 0 otherwise.
3084*/
3085SQLITE_API void sqlite3_interrupt(sqlite3*);
3086SQLITE_API int sqlite3_is_interrupted(sqlite3*);
3087
3088/*
3089** CAPI3REF: Determine If An SQL Statement Is Complete
3090**
3091** These routines are useful during command-line input to determine if the
3092** currently entered text seems to form a complete SQL statement or
3093** if additional input is needed before sending the text into
3094** SQLite for parsing. ^These routines return 1 if the input string
3095** appears to be a complete SQL statement. ^A statement is judged to be
3096** complete if it ends with a semicolon token and is not a prefix of a
3097** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3098** string literals or quoted identifier names or comments are not
3099** independent tokens (they are part of the token in which they are
3100** embedded) and thus do not count as a statement terminator. ^Whitespace
3101** and comments that follow the final semicolon are ignored.
3102**
3103** ^These routines return 0 if the statement is incomplete. ^If a
3104** memory allocation fails, then SQLITE_NOMEM is returned.
3105**
3106** ^These routines do not parse the SQL statements thus
3107** will not detect syntactically incorrect SQL.
3108**
3109** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3110** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3111** automatically by sqlite3_complete16(). If that initialization fails,
3112** then the return value from sqlite3_complete16() will be non-zero
3113** regardless of whether or not the input SQL is complete.)^
3114**
3115** The input to [sqlite3_complete()] must be a zero-terminated
3116** UTF-8 string.
3117**
3118** The input to [sqlite3_complete16()] must be a zero-terminated
3119** UTF-16 string in native byte order.
3120*/
3121SQLITE_API int sqlite3_complete(const char *sql);
3122SQLITE_API int sqlite3_complete16(const void *sql);
3123
3124/*
3125** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3126** KEYWORDS: {busy-handler callback} {busy handler}
3127** METHOD: sqlite3
3128**
3129** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3130** that might be invoked with argument P whenever
3131** an attempt is made to access a database table associated with
3132** [database connection] D when another thread
3133** or process has the table locked.
3134** The sqlite3_busy_handler() interface is used to implement
3135** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3136**
3137** ^If the busy callback is NULL, then [SQLITE_BUSY]
3138** is returned immediately upon encountering the lock. ^If the busy callback
3139** is not NULL, then the callback might be invoked with two arguments.
3140**
3141** ^The first argument to the busy handler is a copy of the void* pointer which
3142** is the third argument to sqlite3_busy_handler(). ^The second argument to
3143** the busy handler callback is the number of times that the busy handler has
3144** been invoked previously for the same locking event. ^If the
3145** busy callback returns 0, then no additional attempts are made to
3146** access the database and [SQLITE_BUSY] is returned
3147** to the application.
3148** ^If the callback returns non-zero, then another attempt
3149** is made to access the database and the cycle repeats.
3150**
3151** The presence of a busy handler does not guarantee that it will be invoked
3152** when there is lock contention. ^If SQLite determines that invoking the busy
3153** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3154** to the application instead of invoking the
3155** busy handler.
3156** Consider a scenario where one process is holding a read lock that
3157** it is trying to promote to a reserved lock and
3158** a second process is holding a reserved lock that it is trying
3159** to promote to an exclusive lock. The first process cannot proceed
3160** because it is blocked by the second and the second process cannot
3161** proceed because it is blocked by the first. If both processes
3162** invoke the busy handlers, neither will make any progress. Therefore,
3163** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3164** will induce the first process to release its read lock and allow
3165** the second process to proceed.
3166**
3167** ^The default busy callback is NULL.
3168**
3169** ^(There can only be a single busy handler defined for each
3170** [database connection]. Setting a new busy handler clears any
3171** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3172** or evaluating [PRAGMA busy_timeout=N] will change the
3173** busy handler and thus clear any previously set busy handler.
3174**
3175** The busy callback should not take any actions which modify the
3176** database connection that invoked the busy handler. In other words,
3177** the busy handler is not reentrant. Any such actions
3178** result in undefined behavior.
3179**
3180** A busy handler must not close the database connection
3181** or [prepared statement] that invoked the busy handler.
3182*/
3183SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3184
3185/*
3186** CAPI3REF: Set A Busy Timeout
3187** METHOD: sqlite3
3188**
3189** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3190** for a specified amount of time when a table is locked. ^The handler
3191** will sleep multiple times until at least "ms" milliseconds of sleeping
3192** have accumulated. ^After at least "ms" milliseconds of sleeping,
3193** the handler returns 0 which causes [sqlite3_step()] to return
3194** [SQLITE_BUSY].
3195**
3196** ^Calling this routine with an argument less than or equal to zero
3197** turns off all busy handlers.
3198**
3199** ^(There can only be a single busy handler for a particular
3200** [database connection] at any given moment. If another busy handler
3201** was defined (using [sqlite3_busy_handler()]) prior to calling
3202** this routine, that other busy handler is cleared.)^
3203**
3204** See also: [PRAGMA busy_timeout]
3205*/
3206SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3207
3208/*
3209** CAPI3REF: Convenience Routines For Running Queries
3210** METHOD: sqlite3
3211**
3212** This is a legacy interface that is preserved for backwards compatibility.
3213** Use of this interface is not recommended.
3214**
3215** Definition: A <b>result table</b> is memory data structure created by the
3216** [sqlite3_get_table()] interface. A result table records the
3217** complete query results from one or more queries.
3218**
3219** The table conceptually has a number of rows and columns. But
3220** these numbers are not part of the result table itself. These
3221** numbers are obtained separately. Let N be the number of rows
3222** and M be the number of columns.
3223**
3224** A result table is an array of pointers to zero-terminated UTF-8 strings.
3225** There are (N+1)*M elements in the array. The first M pointers point
3226** to zero-terminated strings that contain the names of the columns.
3227** The remaining entries all point to query results. NULL values result
3228** in NULL pointers. All other values are in their UTF-8 zero-terminated
3229** string representation as returned by [sqlite3_column_text()].
3230**
3231** A result table might consist of one or more memory allocations.
3232** It is not safe to pass a result table directly to [sqlite3_free()].
3233** A result table should be deallocated using [sqlite3_free_table()].
3234**
3235** ^(As an example of the result table format, suppose a query result
3236** is as follows:
3237**
3238** <blockquote><pre>
3239** Name | Age
3240** -----------------------
3241** Alice | 43
3242** Bob | 28
3243** Cindy | 21
3244** </pre></blockquote>
3245**
3246** There are two columns (M==2) and three rows (N==3). Thus the
3247** result table has 8 entries. Suppose the result table is stored
3248** in an array named azResult. Then azResult holds this content:
3249**
3250** <blockquote><pre>
3251** azResult&#91;0] = "Name";
3252** azResult&#91;1] = "Age";
3253** azResult&#91;2] = "Alice";
3254** azResult&#91;3] = "43";
3255** azResult&#91;4] = "Bob";
3256** azResult&#91;5] = "28";
3257** azResult&#91;6] = "Cindy";
3258** azResult&#91;7] = "21";
3259** </pre></blockquote>)^
3260**
3261** ^The sqlite3_get_table() function evaluates one or more
3262** semicolon-separated SQL statements in the zero-terminated UTF-8
3263** string of its 2nd parameter and returns a result table to the
3264** pointer given in its 3rd parameter.
3265**
3266** After the application has finished with the result from sqlite3_get_table(),
3267** it must pass the result table pointer to sqlite3_free_table() in order to
3268** release the memory that was malloced. Because of the way the
3269** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3270** function must not try to call [sqlite3_free()] directly. Only
3271** [sqlite3_free_table()] is able to release the memory properly and safely.
3272**
3273** The sqlite3_get_table() interface is implemented as a wrapper around
3274** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3275** to any internal data structures of SQLite. It uses only the public
3276** interface defined here. As a consequence, errors that occur in the
3277** wrapper layer outside of the internal [sqlite3_exec()] call are not
3278** reflected in subsequent calls to [sqlite3_errcode()] or
3279** [sqlite3_errmsg()].
3280*/
3281SQLITE_API int sqlite3_get_table(
3282 sqlite3 *db, /* An open database */
3283 const char *zSql, /* SQL to be evaluated */
3284 char ***pazResult, /* Results of the query */
3285 int *pnRow, /* Number of result rows written here */
3286 int *pnColumn, /* Number of result columns written here */
3287 char **pzErrmsg /* Error msg written here */
3288);
3289SQLITE_API void sqlite3_free_table(char **result);
3290
3291/*
3292** CAPI3REF: Formatted String Printing Functions
3293**
3294** These routines are work-alikes of the "printf()" family of functions
3295** from the standard C library.
3296** These routines understand most of the common formatting options from
3297** the standard library printf()
3298** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3299** See the [built-in printf()] documentation for details.
3300**
3301** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3302** results into memory obtained from [sqlite3_malloc64()].
3303** The strings returned by these two routines should be
3304** released by [sqlite3_free()]. ^Both routines return a
3305** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3306** memory to hold the resulting string.
3307**
3308** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3309** the standard C library. The result is written into the
3310** buffer supplied as the second parameter whose size is given by
3311** the first parameter. Note that the order of the
3312** first two parameters is reversed from snprintf().)^ This is an
3313** historical accident that cannot be fixed without breaking
3314** backwards compatibility. ^(Note also that sqlite3_snprintf()
3315** returns a pointer to its buffer instead of the number of
3316** characters actually written into the buffer.)^ We admit that
3317** the number of characters written would be a more useful return
3318** value but we cannot change the implementation of sqlite3_snprintf()
3319** now without breaking compatibility.
3320**
3321** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3322** guarantees that the buffer is always zero-terminated. ^The first
3323** parameter "n" is the total size of the buffer, including space for
3324** the zero terminator. So the longest string that can be completely
3325** written will be n-1 characters.
3326**
3327** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3328**
3329** See also: [built-in printf()], [printf() SQL function]
3330*/
3331SQLITE_API char *sqlite3_mprintf(const char*,...);
3332SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3333SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3334SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3335
3336/*
3337** CAPI3REF: Memory Allocation Subsystem
3338**
3339** The SQLite core uses these three routines for all of its own
3340** internal memory allocation needs. "Core" in the previous sentence
3341** does not include operating-system specific [VFS] implementation. The
3342** Windows VFS uses native malloc() and free() for some operations.
3343**
3344** ^The sqlite3_malloc() routine returns a pointer to a block
3345** of memory at least N bytes in length, where N is the parameter.
3346** ^If sqlite3_malloc() is unable to obtain sufficient free
3347** memory, it returns a NULL pointer. ^If the parameter N to
3348** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3349** a NULL pointer.
3350**
3351** ^The sqlite3_malloc64(N) routine works just like
3352** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3353** of a signed 32-bit integer.
3354**
3355** ^Calling sqlite3_free() with a pointer previously returned
3356** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3357** that it might be reused. ^The sqlite3_free() routine is
3358** a no-op if is called with a NULL pointer. Passing a NULL pointer
3359** to sqlite3_free() is harmless. After being freed, memory
3360** should neither be read nor written. Even reading previously freed
3361** memory might result in a segmentation fault or other severe error.
3362** Memory corruption, a segmentation fault, or other severe error
3363** might result if sqlite3_free() is called with a non-NULL pointer that
3364** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3365**
3366** ^The sqlite3_realloc(X,N) interface attempts to resize a
3367** prior memory allocation X to be at least N bytes.
3368** ^If the X parameter to sqlite3_realloc(X,N)
3369** is a NULL pointer then its behavior is identical to calling
3370** sqlite3_malloc(N).
3371** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3372** negative then the behavior is exactly the same as calling
3373** sqlite3_free(X).
3374** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3375** of at least N bytes in size or NULL if insufficient memory is available.
3376** ^If M is the size of the prior allocation, then min(N,M) bytes
3377** of the prior allocation are copied into the beginning of buffer returned
3378** by sqlite3_realloc(X,N) and the prior allocation is freed.
3379** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3380** prior allocation is not freed.
3381**
3382** ^The sqlite3_realloc64(X,N) interfaces works the same as
3383** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3384** of a 32-bit signed integer.
3385**
3386** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3387** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3388** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3389** ^The value returned by sqlite3_msize(X) might be larger than the number
3390** of bytes requested when X was allocated. ^If X is a NULL pointer then
3391** sqlite3_msize(X) returns zero. If X points to something that is not
3392** the beginning of memory allocation, or if it points to a formerly
3393** valid memory allocation that has now been freed, then the behavior
3394** of sqlite3_msize(X) is undefined and possibly harmful.
3395**
3396** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3397** sqlite3_malloc64(), and sqlite3_realloc64()
3398** is always aligned to at least an 8 byte boundary, or to a
3399** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3400** option is used.
3401**
3402** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3403** must be either NULL or else pointers obtained from a prior
3404** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3405** not yet been released.
3406**
3407** The application must not read or write any part of
3408** a block of memory after it has been released using
3409** [sqlite3_free()] or [sqlite3_realloc()].
3410*/
3411SQLITE_API void *sqlite3_malloc(int);
3412SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3413SQLITE_API void *sqlite3_realloc(void*, int);
3414SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3415SQLITE_API void sqlite3_free(void*);
3416SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3417
3418/*
3419** CAPI3REF: Memory Allocator Statistics
3420**
3421** SQLite provides these two interfaces for reporting on the status
3422** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3423** routines, which form the built-in memory allocation subsystem.
3424**
3425** ^The [sqlite3_memory_used()] routine returns the number of bytes
3426** of memory currently outstanding (malloced but not freed).
3427** ^The [sqlite3_memory_highwater()] routine returns the maximum
3428** value of [sqlite3_memory_used()] since the high-water mark
3429** was last reset. ^The values returned by [sqlite3_memory_used()] and
3430** [sqlite3_memory_highwater()] include any overhead
3431** added by SQLite in its implementation of [sqlite3_malloc()],
3432** but not overhead added by the any underlying system library
3433** routines that [sqlite3_malloc()] may call.
3434**
3435** ^The memory high-water mark is reset to the current value of
3436** [sqlite3_memory_used()] if and only if the parameter to
3437** [sqlite3_memory_highwater()] is true. ^The value returned
3438** by [sqlite3_memory_highwater(1)] is the high-water mark
3439** prior to the reset.
3440*/
3441SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3442SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3443
3444/*
3445** CAPI3REF: Pseudo-Random Number Generator
3446**
3447** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3448** select random [ROWID | ROWIDs] when inserting new records into a table that
3449** already uses the largest possible [ROWID]. The PRNG is also used for
3450** the built-in random() and randomblob() SQL functions. This interface allows
3451** applications to access the same PRNG for other purposes.
3452**
3453** ^A call to this routine stores N bytes of randomness into buffer P.
3454** ^The P parameter can be a NULL pointer.
3455**
3456** ^If this routine has not been previously called or if the previous
3457** call had N less than one or a NULL pointer for P, then the PRNG is
3458** seeded using randomness obtained from the xRandomness method of
3459** the default [sqlite3_vfs] object.
3460** ^If the previous call to this routine had an N of 1 or more and a
3461** non-NULL P then the pseudo-randomness is generated
3462** internally and without recourse to the [sqlite3_vfs] xRandomness
3463** method.
3464*/
3465SQLITE_API void sqlite3_randomness(int N, void *P);
3466
3467/*
3468** CAPI3REF: Compile-Time Authorization Callbacks
3469** METHOD: sqlite3
3470** KEYWORDS: {authorizer callback}
3471**
3472** ^This routine registers an authorizer callback with a particular
3473** [database connection], supplied in the first argument.
3474** ^The authorizer callback is invoked as SQL statements are being compiled
3475** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3476** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3477** and [sqlite3_prepare16_v3()]. ^At various
3478** points during the compilation process, as logic is being created
3479** to perform various actions, the authorizer callback is invoked to
3480** see if those actions are allowed. ^The authorizer callback should
3481** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3482** specific action but allow the SQL statement to continue to be
3483** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3484** rejected with an error. ^If the authorizer callback returns
3485** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3486** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3487** the authorizer will fail with an error message.
3488**
3489** When the callback returns [SQLITE_OK], that means the operation
3490** requested is ok. ^When the callback returns [SQLITE_DENY], the
3491** [sqlite3_prepare_v2()] or equivalent call that triggered the
3492** authorizer will fail with an error message explaining that
3493** access is denied.
3494**
3495** ^The first parameter to the authorizer callback is a copy of the third
3496** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3497** to the callback is an integer [SQLITE_COPY | action code] that specifies
3498** the particular action to be authorized. ^The third through sixth parameters
3499** to the callback are either NULL pointers or zero-terminated strings
3500** that contain additional details about the action to be authorized.
3501** Applications must always be prepared to encounter a NULL pointer in any
3502** of the third through the sixth parameters of the authorization callback.
3503**
3504** ^If the action code is [SQLITE_READ]
3505** and the callback returns [SQLITE_IGNORE] then the
3506** [prepared statement] statement is constructed to substitute
3507** a NULL value in place of the table column that would have
3508** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3509** return can be used to deny an untrusted user access to individual
3510** columns of a table.
3511** ^When a table is referenced by a [SELECT] but no column values are
3512** extracted from that table (for example in a query like
3513** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3514** is invoked once for that table with a column name that is an empty string.
3515** ^If the action code is [SQLITE_DELETE] and the callback returns
3516** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3517** [truncate optimization] is disabled and all rows are deleted individually.
3518**
3519** An authorizer is used when [sqlite3_prepare | preparing]
3520** SQL statements from an untrusted source, to ensure that the SQL statements
3521** do not try to access data they are not allowed to see, or that they do not
3522** try to execute malicious statements that damage the database. For
3523** example, an application may allow a user to enter arbitrary
3524** SQL queries for evaluation by a database. But the application does
3525** not want the user to be able to make arbitrary changes to the
3526** database. An authorizer could then be put in place while the
3527** user-entered SQL is being [sqlite3_prepare | prepared] that
3528** disallows everything except [SELECT] statements.
3529**
3530** Applications that need to process SQL from untrusted sources
3531** might also consider lowering resource limits using [sqlite3_limit()]
3532** and limiting database size using the [max_page_count] [PRAGMA]
3533** in addition to using an authorizer.
3534**
3535** ^(Only a single authorizer can be in place on a database connection
3536** at a time. Each call to sqlite3_set_authorizer overrides the
3537** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3538** The authorizer is disabled by default.
3539**
3540** The authorizer callback must not do anything that will modify
3541** the database connection that invoked the authorizer callback.
3542** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3543** database connections for the meaning of "modify" in this paragraph.
3544**
3545** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3546** statement might be re-prepared during [sqlite3_step()] due to a
3547** schema change. Hence, the application should ensure that the
3548** correct authorizer callback remains in place during the [sqlite3_step()].
3549**
3550** ^Note that the authorizer callback is invoked only during
3551** [sqlite3_prepare()] or its variants. Authorization is not
3552** performed during statement evaluation in [sqlite3_step()], unless
3553** as stated in the previous paragraph, sqlite3_step() invokes
3554** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3555*/
3556SQLITE_API int sqlite3_set_authorizer(
3557 sqlite3*,
3558 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3559 void *pUserData
3560);
3561
3562/*
3563** CAPI3REF: Authorizer Return Codes
3564**
3565** The [sqlite3_set_authorizer | authorizer callback function] must
3566** return either [SQLITE_OK] or one of these two constants in order
3567** to signal SQLite whether or not the action is permitted. See the
3568** [sqlite3_set_authorizer | authorizer documentation] for additional
3569** information.
3570**
3571** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3572** returned from the [sqlite3_vtab_on_conflict()] interface.
3573*/
3574#define SQLITE_DENY1 1 /* Abort the SQL statement with an error */
3575#define SQLITE_IGNORE2 2 /* Don't allow access, but don't generate an error */
3576
3577/*
3578** CAPI3REF: Authorizer Action Codes
3579**
3580** The [sqlite3_set_authorizer()] interface registers a callback function
3581** that is invoked to authorize certain SQL statement actions. The
3582** second parameter to the callback is an integer code that specifies
3583** what action is being authorized. These are the integer action codes that
3584** the authorizer callback may be passed.
3585**
3586** These action code values signify what kind of operation is to be
3587** authorized. The 3rd and 4th parameters to the authorization
3588** callback function will be parameters or NULL depending on which of these
3589** codes is used as the second parameter. ^(The 5th parameter to the
3590** authorizer callback is the name of the database ("main", "temp",
3591** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3592** is the name of the inner-most trigger or view that is responsible for
3593** the access attempt or NULL if this access attempt is directly from
3594** top-level SQL code.
3595*/
3596/******************************************* 3rd ************ 4th ***********/
3597#define SQLITE_CREATE_INDEX1 1 /* Index Name Table Name */
3598#define SQLITE_CREATE_TABLE2 2 /* Table Name NULL */
3599#define SQLITE_CREATE_TEMP_INDEX3 3 /* Index Name Table Name */
3600#define SQLITE_CREATE_TEMP_TABLE4 4 /* Table Name NULL */
3601#define SQLITE_CREATE_TEMP_TRIGGER5 5 /* Trigger Name Table Name */
3602#define SQLITE_CREATE_TEMP_VIEW6 6 /* View Name NULL */
3603#define SQLITE_CREATE_TRIGGER7 7 /* Trigger Name Table Name */
3604#define SQLITE_CREATE_VIEW8 8 /* View Name NULL */
3605#define SQLITE_DELETE9 9 /* Table Name NULL */
3606#define SQLITE_DROP_INDEX10 10 /* Index Name Table Name */
3607#define SQLITE_DROP_TABLE11 11 /* Table Name NULL */
3608#define SQLITE_DROP_TEMP_INDEX12 12 /* Index Name Table Name */
3609#define SQLITE_DROP_TEMP_TABLE13 13 /* Table Name NULL */
3610#define SQLITE_DROP_TEMP_TRIGGER14 14 /* Trigger Name Table Name */
3611#define SQLITE_DROP_TEMP_VIEW15 15 /* View Name NULL */
3612#define SQLITE_DROP_TRIGGER16 16 /* Trigger Name Table Name */
3613#define SQLITE_DROP_VIEW17 17 /* View Name NULL */
3614#define SQLITE_INSERT18 18 /* Table Name NULL */
3615#define SQLITE_PRAGMA19 19 /* Pragma Name 1st arg or NULL */
3616#define SQLITE_READ20 20 /* Table Name Column Name */
3617#define SQLITE_SELECT21 21 /* NULL NULL */
3618#define SQLITE_TRANSACTION22 22 /* Operation NULL */
3619#define SQLITE_UPDATE23 23 /* Table Name Column Name */
3620#define SQLITE_ATTACH24 24 /* Filename NULL */
3621#define SQLITE_DETACH25 25 /* Database Name NULL */
3622#define SQLITE_ALTER_TABLE26 26 /* Database Name Table Name */
3623#define SQLITE_REINDEX27 27 /* Index Name NULL */
3624#define SQLITE_ANALYZE28 28 /* Table Name NULL */
3625#define SQLITE_CREATE_VTABLE29 29 /* Table Name Module Name */
3626#define SQLITE_DROP_VTABLE30 30 /* Table Name Module Name */
3627#define SQLITE_FUNCTION31 31 /* NULL Function Name */
3628#define SQLITE_SAVEPOINT32 32 /* Operation Savepoint Name */
3629#define SQLITE_COPY0 0 /* No longer used */
3630#define SQLITE_RECURSIVE33 33 /* NULL NULL */
3631
3632/*
3633** CAPI3REF: Deprecated Tracing And Profiling Functions
3634** DEPRECATED
3635**
3636** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3637** instead of the routines described here.
3638**
3639** These routines register callback functions that can be used for
3640** tracing and profiling the execution of SQL statements.
3641**
3642** ^The callback function registered by sqlite3_trace() is invoked at
3643** various times when an SQL statement is being run by [sqlite3_step()].
3644** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3645** SQL statement text as the statement first begins executing.
3646** ^(Additional sqlite3_trace() callbacks might occur
3647** as each triggered subprogram is entered. The callbacks for triggers
3648** contain a UTF-8 SQL comment that identifies the trigger.)^
3649**
3650** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3651** the length of [bound parameter] expansion in the output of sqlite3_trace().
3652**
3653** ^The callback function registered by sqlite3_profile() is invoked
3654** as each SQL statement finishes. ^The profile callback contains
3655** the original statement text and an estimate of wall-clock time
3656** of how long that statement took to run. ^The profile callback
3657** time is in units of nanoseconds, however the current implementation
3658** is only capable of millisecond resolution so the six least significant
3659** digits in the time are meaningless. Future versions of SQLite
3660** might provide greater resolution on the profiler callback. Invoking
3661** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
3662** profile callback.
3663*/
3664SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
3665 void(*xTrace)(void*,const char*), void*);
3666SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3667 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
3668
3669/*
3670** CAPI3REF: SQL Trace Event Codes
3671** KEYWORDS: SQLITE_TRACE
3672**
3673** These constants identify classes of events that can be monitored
3674** using the [sqlite3_trace_v2()] tracing logic. The M argument
3675** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3676** the following constants. ^The first argument to the trace callback
3677** is one of the following constants.
3678**
3679** New tracing constants may be added in future releases.
3680**
3681** ^A trace callback has four arguments: xCallback(T,C,P,X).
3682** ^The T argument is one of the integer type codes above.
3683** ^The C argument is a copy of the context pointer passed in as the
3684** fourth argument to [sqlite3_trace_v2()].
3685** The P and X arguments are pointers whose meanings depend on T.
3686**
3687** <dl>
3688** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
3689** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3690** first begins running and possibly at other times during the
3691** execution of the prepared statement, such as at the start of each
3692** trigger subprogram. ^The P argument is a pointer to the
3693** [prepared statement]. ^The X argument is a pointer to a string which
3694** is the unexpanded SQL text of the prepared statement or an SQL comment
3695** that indicates the invocation of a trigger. ^The callback can compute
3696** the same text that would have been returned by the legacy [sqlite3_trace()]
3697** interface by using the X argument when X begins with "--" and invoking
3698** [sqlite3_expanded_sql(P)] otherwise.
3699**
3700** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
3701** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
3702** information as is provided by the [sqlite3_profile()] callback.
3703** ^The P argument is a pointer to the [prepared statement] and the
3704** X argument points to a 64-bit integer which is approximately
3705** the number of nanoseconds that the prepared statement took to run.
3706** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
3707**
3708** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
3709** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3710** statement generates a single row of result.
3711** ^The P argument is a pointer to the [prepared statement] and the
3712** X argument is unused.
3713**
3714** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
3715** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3716** connection closes.
3717** ^The P argument is a pointer to the [database connection] object
3718** and the X argument is unused.
3719** </dl>
3720*/
3721#define SQLITE_TRACE_STMT0x01 0x01
3722#define SQLITE_TRACE_PROFILE0x02 0x02
3723#define SQLITE_TRACE_ROW0x04 0x04
3724#define SQLITE_TRACE_CLOSE0x08 0x08
3725
3726/*
3727** CAPI3REF: SQL Trace Hook
3728** METHOD: sqlite3
3729**
3730** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3731** function X against [database connection] D, using property mask M
3732** and context pointer P. ^If the X callback is
3733** NULL or if the M mask is zero, then tracing is disabled. The
3734** M argument should be the bitwise OR-ed combination of
3735** zero or more [SQLITE_TRACE] constants.
3736**
3737** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P)
3738** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or
3739** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each
3740** database connection may have at most one trace callback.
3741**
3742** ^The X callback is invoked whenever any of the events identified by
3743** mask M occur. ^The integer return value from the callback is currently
3744** ignored, though this may change in future releases. Callback
3745** implementations should return zero to ensure future compatibility.
3746**
3747** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3748** ^The T argument is one of the [SQLITE_TRACE]
3749** constants to indicate why the callback was invoked.
3750** ^The C argument is a copy of the context pointer.
3751** The P and X arguments are pointers whose meanings depend on T.
3752**
3753** The sqlite3_trace_v2() interface is intended to replace the legacy
3754** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
3755** are deprecated.
3756*/
3757SQLITE_API int sqlite3_trace_v2(
3758 sqlite3*,
3759 unsigned uMask,
3760 int(*xCallback)(unsigned,void*,void*,void*),
3761 void *pCtx
3762);
3763
3764/*
3765** CAPI3REF: Query Progress Callbacks
3766** METHOD: sqlite3
3767**
3768** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
3769** function X to be invoked periodically during long running calls to
3770** [sqlite3_step()] and [sqlite3_prepare()] and similar for
3771** database connection D. An example use for this
3772** interface is to keep a GUI updated during a large query.
3773**
3774** ^The parameter P is passed through as the only parameter to the
3775** callback function X. ^The parameter N is the approximate number of
3776** [virtual machine instructions] that are evaluated between successive
3777** invocations of the callback X. ^If N is less than one then the progress
3778** handler is disabled.
3779**
3780** ^Only a single progress handler may be defined at one time per
3781** [database connection]; setting a new progress handler cancels the
3782** old one. ^Setting parameter X to NULL disables the progress handler.
3783** ^The progress handler is also disabled by setting N to a value less
3784** than 1.
3785**
3786** ^If the progress callback returns non-zero, the operation is
3787** interrupted. This feature can be used to implement a
3788** "Cancel" button on a GUI progress dialog box.
3789**
3790** The progress handler callback must not do anything that will modify
3791** the database connection that invoked the progress handler.
3792** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3793** database connections for the meaning of "modify" in this paragraph.
3794**
3795** The progress handler callback would originally only be invoked from the
3796** bytecode engine. It still might be invoked during [sqlite3_prepare()]
3797** and similar because those routines might force a reparse of the schema
3798** which involves running the bytecode engine. However, beginning with
3799** SQLite version 3.41.0, the progress handler callback might also be
3800** invoked directly from [sqlite3_prepare()] while analyzing and generating
3801** code for complex queries.
3802*/
3803SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3804
3805/*
3806** CAPI3REF: Opening A New Database Connection
3807** CONSTRUCTOR: sqlite3
3808**
3809** ^These routines open an SQLite database file as specified by the
3810** filename argument. ^The filename argument is interpreted as UTF-8 for
3811** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3812** order for sqlite3_open16(). ^(A [database connection] handle is usually
3813** returned in *ppDb, even if an error occurs. The only exception is that
3814** if SQLite is unable to allocate memory to hold the [sqlite3] object,
3815** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
3816** object.)^ ^(If the database is opened (and/or created) successfully, then
3817** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
3818** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
3819** an English language description of the error following a failure of any
3820** of the sqlite3_open() routines.
3821**
3822** ^The default encoding will be UTF-8 for databases created using
3823** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
3824** created using sqlite3_open16() will be UTF-16 in the native byte order.
3825**
3826** Whether or not an error occurs when it is opened, resources
3827** associated with the [database connection] handle should be released by
3828** passing it to [sqlite3_close()] when it is no longer required.
3829**
3830** The sqlite3_open_v2() interface works like sqlite3_open()
3831** except that it accepts two additional parameters for additional control
3832** over the new database connection. ^(The flags parameter to
3833** sqlite3_open_v2() must include, at a minimum, one of the following
3834** three flag combinations:)^
3835**
3836** <dl>
3837** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
3838** <dd>The database is opened in read-only mode. If the database does
3839** not already exist, an error is returned.</dd>)^
3840**
3841** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
3842** <dd>The database is opened for reading and writing if possible, or
3843** reading only if the file is write protected by the operating
3844** system. In either case the database must already exist, otherwise
3845** an error is returned. For historical reasons, if opening in
3846** read-write mode fails due to OS-level permissions, an attempt is
3847** made to open it in read-only mode. [sqlite3_db_readonly()] can be
3848** used to determine whether the database is actually
3849** read-write.</dd>)^
3850**
3851** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
3852** <dd>The database is opened for reading and writing, and is created if
3853** it does not already exist. This is the behavior that is always used for
3854** sqlite3_open() and sqlite3_open16().</dd>)^
3855** </dl>
3856**
3857** In addition to the required flags, the following optional flags are
3858** also supported:
3859**
3860** <dl>
3861** ^(<dt>[SQLITE_OPEN_URI]</dt>
3862** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
3863**
3864** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
3865** <dd>The database will be opened as an in-memory database. The database
3866** is named by the "filename" argument for the purposes of cache-sharing,
3867** if shared cache mode is enabled, but the "filename" is otherwise ignored.
3868** </dd>)^
3869**
3870** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
3871** <dd>The new database connection will use the "multi-thread"
3872** [threading mode].)^ This means that separate threads are allowed
3873** to use SQLite at the same time, as long as each thread is using
3874** a different [database connection].
3875**
3876** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
3877** <dd>The new database connection will use the "serialized"
3878** [threading mode].)^ This means the multiple threads can safely
3879** attempt to use the same database connection at the same time.
3880** (Mutexes will block any actual concurrency, but in this mode
3881** there is no harm in trying.)
3882**
3883** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3884** <dd>The database is opened [shared cache] enabled, overriding
3885** the default shared cache setting provided by
3886** [sqlite3_enable_shared_cache()].)^
3887** The [use of shared cache mode is discouraged] and hence shared cache
3888** capabilities may be omitted from many builds of SQLite. In such cases,
3889** this option is a no-op.
3890**
3891** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3892** <dd>The database is opened [shared cache] disabled, overriding
3893** the default shared cache setting provided by
3894** [sqlite3_enable_shared_cache()].)^
3895**
3896** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt>
3897** <dd>The database connection comes up in "extended result code mode".
3898** In other words, the database behaves as if
3899** [sqlite3_extended_result_codes(db,1)] were called on the database
3900** connection as soon as the connection is created. In addition to setting
3901** the extended result code mode, this flag also causes [sqlite3_open_v2()]
3902** to return an extended result code.</dd>
3903**
3904** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3905** <dd>The database filename is not allowed to contain a symbolic link</dd>
3906** </dl>)^
3907**
3908** If the 3rd parameter to sqlite3_open_v2() is not one of the
3909** required combinations shown above optionally combined with other
3910** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
3911** then the behavior is undefined. Historic versions of SQLite
3912** have silently ignored surplus bits in the flags parameter to
3913** sqlite3_open_v2(), however that behavior might not be carried through
3914** into future versions of SQLite and so applications should not rely
3915** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3916** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause
3917** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE
3918** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not
3919** by sqlite3_open_v2().
3920**
3921** ^The fourth parameter to sqlite3_open_v2() is the name of the
3922** [sqlite3_vfs] object that defines the operating system interface that
3923** the new database connection should use. ^If the fourth parameter is
3924** a NULL pointer then the default [sqlite3_vfs] object is used.
3925**
3926** ^If the filename is ":memory:", then a private, temporary in-memory database
3927** is created for the connection. ^This in-memory database will vanish when
3928** the database connection is closed. Future versions of SQLite might
3929** make use of additional special filenames that begin with the ":" character.
3930** It is recommended that when a database filename actually does begin with
3931** a ":" character you should prefix the filename with a pathname such as
3932** "./" to avoid ambiguity.
3933**
3934** ^If the filename is an empty string, then a private, temporary
3935** on-disk database will be created. ^This private database will be
3936** automatically deleted as soon as the database connection is closed.
3937**
3938** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3939**
3940** ^If [URI filename] interpretation is enabled, and the filename argument
3941** begins with "file:", then the filename is interpreted as a URI. ^URI
3942** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3943** set in the third argument to sqlite3_open_v2(), or if it has
3944** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3945** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3946** URI filename interpretation is turned off
3947** by default, but future releases of SQLite might enable URI filename
3948** interpretation by default. See "[URI filenames]" for additional
3949** information.
3950**
3951** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3952** authority, then it must be either an empty string or the string
3953** "localhost". ^If the authority is not an empty string or "localhost", an
3954** error is returned to the caller. ^The fragment component of a URI, if
3955** present, is ignored.
3956**
3957** ^SQLite uses the path component of the URI as the name of the disk file
3958** which contains the database. ^If the path begins with a '/' character,
3959** then it is interpreted as an absolute path. ^If the path does not begin
3960** with a '/' (meaning that the authority section is omitted from the URI)
3961** then the path is interpreted as a relative path.
3962** ^(On windows, the first component of an absolute path
3963** is a drive specification (e.g. "C:").)^
3964**
3965** [[core URI query parameters]]
3966** The query component of a URI may contain parameters that are interpreted
3967** either by SQLite itself, or by a [VFS | custom VFS implementation].
3968** SQLite and its built-in [VFSes] interpret the
3969** following query parameters:
3970**
3971** <ul>
3972** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3973** a VFS object that provides the operating system interface that should
3974** be used to access the database file on disk. ^If this option is set to
3975** an empty string the default VFS object is used. ^Specifying an unknown
3976** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3977** present, then the VFS specified by the option takes precedence over
3978** the value passed as the fourth parameter to sqlite3_open_v2().
3979**
3980** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
3981** "rwc", or "memory". Attempting to set it to any other value is
3982** an error)^.
3983** ^If "ro" is specified, then the database is opened for read-only
3984** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3985** third argument to sqlite3_open_v2(). ^If the mode option is set to
3986** "rw", then the database is opened for read-write (but not create)
3987** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3988** been set. ^Value "rwc" is equivalent to setting both
3989** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
3990** set to "memory" then a pure [in-memory database] that never reads
3991** or writes from disk is used. ^It is an error to specify a value for
3992** the mode parameter that is less restrictive than that specified by
3993** the flags passed in the third parameter to sqlite3_open_v2().
3994**
3995** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3996** "private". ^Setting it to "shared" is equivalent to setting the
3997** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3998** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3999** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4000** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4001** a URI filename, its value overrides any behavior requested by setting
4002** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4003**
4004** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4005** [powersafe overwrite] property does or does not apply to the
4006** storage media on which the database file resides.
4007**
4008** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4009** which if set disables file locking in rollback journal modes. This
4010** is useful for accessing a database on a filesystem that does not
4011** support locking. Caution: Database corruption might result if two
4012** or more processes write to the same database and any one of those
4013** processes uses nolock=1.
4014**
4015** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4016** parameter that indicates that the database file is stored on
4017** read-only media. ^When immutable is set, SQLite assumes that the
4018** database file cannot be changed, even by a process with higher
4019** privilege, and so the database is opened read-only and all locking
4020** and change detection is disabled. Caution: Setting the immutable
4021** property on a database file that does in fact change can result
4022** in incorrect query results and/or [SQLITE_CORRUPT] errors.
4023** See also: [SQLITE_IOCAP_IMMUTABLE].
4024**
4025** </ul>
4026**
4027** ^Specifying an unknown parameter in the query component of a URI is not an
4028** error. Future versions of SQLite might understand additional query
4029** parameters. See "[query parameters with special meaning to SQLite]" for
4030** additional information.
4031**
4032** [[URI filename examples]] <h3>URI filename examples</h3>
4033**
4034** <table border="1" align=center cellpadding=5>
4035** <tr><th> URI filenames <th> Results
4036** <tr><td> file:data.db <td>
4037** Open the file "data.db" in the current directory.
4038** <tr><td> file:/home/fred/data.db<br>
4039** file:///home/fred/data.db <br>
4040** file://localhost/home/fred/data.db <br> <td>
4041** Open the database file "/home/fred/data.db".
4042** <tr><td> file://darkstar/home/fred/data.db <td>
4043** An error. "darkstar" is not a recognized authority.
4044** <tr><td style="white-space:nowrap">
4045** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4046** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4047** C:. Note that the %20 escaping in this example is not strictly
4048** necessary - space characters can be used literally
4049** in URI filenames.
4050** <tr><td> file:data.db?mode=ro&cache=private <td>
4051** Open file "data.db" in the current directory for read-only access.
4052** Regardless of whether or not shared-cache mode is enabled by
4053** default, use a private cache.
4054** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4055** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4056** that uses dot-files in place of posix advisory locking.
4057** <tr><td> file:data.db?mode=readonly <td>
4058** An error. "readonly" is not a valid option for the "mode" parameter.
4059** Use "ro" instead: "file:data.db?mode=ro".
4060** </table>
4061**
4062** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4063** query components of a URI. A hexadecimal escape sequence consists of a
4064** percent sign - "%" - followed by exactly two hexadecimal digits
4065** specifying an octet value. ^Before the path or query components of a
4066** URI filename are interpreted, they are encoded using UTF-8 and all
4067** hexadecimal escape sequences replaced by a single byte containing the
4068** corresponding octet. If this process generates an invalid UTF-8 encoding,
4069** the results are undefined.
4070**
4071** <b>Note to Windows users:</b> The encoding used for the filename argument
4072** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4073** codepage is currently defined. Filenames containing international
4074** characters must be converted to UTF-8 prior to passing them into
4075** sqlite3_open() or sqlite3_open_v2().
4076**
4077** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4078** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4079** features that require the use of temporary files may fail.
4080**
4081** See also: [sqlite3_temp_directory]
4082*/
4083SQLITE_API int sqlite3_open(
4084 const char *filename, /* Database filename (UTF-8) */
4085 sqlite3 **ppDb /* OUT: SQLite db handle */
4086);
4087SQLITE_API int sqlite3_open16(
4088 const void *filename, /* Database filename (UTF-16) */
4089 sqlite3 **ppDb /* OUT: SQLite db handle */
4090);
4091SQLITE_API int sqlite3_open_v2(
4092 const char *filename, /* Database filename (UTF-8) */
4093 sqlite3 **ppDb, /* OUT: SQLite db handle */
4094 int flags, /* Flags */
4095 const char *zVfs /* Name of VFS module to use */
4096);
4097
4098/*
4099** CAPI3REF: Obtain Values For URI Parameters
4100**
4101** These are utility routines, useful to [VFS|custom VFS implementations],
4102** that check if a database file was a URI that contained a specific query
4103** parameter, and if so obtains the value of that query parameter.
4104**
4105** The first parameter to these interfaces (hereafter referred to
4106** as F) must be one of:
4107** <ul>
4108** <li> A database filename pointer created by the SQLite core and
4109** passed into the xOpen() method of a VFS implementation, or
4110** <li> A filename obtained from [sqlite3_db_filename()], or
4111** <li> A new filename constructed using [sqlite3_create_filename()].
4112** </ul>
4113** If the F parameter is not one of the above, then the behavior is
4114** undefined and probably undesirable. Older versions of SQLite were
4115** more tolerant of invalid F parameters than newer versions.
4116**
4117** If F is a suitable filename (as described in the previous paragraph)
4118** and if P is the name of the query parameter, then
4119** sqlite3_uri_parameter(F,P) returns the value of the P
4120** parameter if it exists or a NULL pointer if P does not appear as a
4121** query parameter on F. If P is a query parameter of F and it
4122** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4123** a pointer to an empty string.
4124**
4125** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4126** parameter and returns true (1) or false (0) according to the value
4127** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4128** value of query parameter P is one of "yes", "true", or "on" in any
4129** case or if the value begins with a non-zero number. The
4130** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4131** query parameter P is one of "no", "false", or "off" in any case or
4132** if the value begins with a numeric zero. If P is not a query
4133** parameter on F or if the value of P does not match any of the
4134** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4135**
4136** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4137** 64-bit signed integer and returns that integer, or D if P does not
4138** exist. If the value of P is something other than an integer, then
4139** zero is returned.
4140**
4141** The sqlite3_uri_key(F,N) returns a pointer to the name (not
4142** the value) of the N-th query parameter for filename F, or a NULL
4143** pointer if N is less than zero or greater than the number of query
4144** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4145** the name of the first query parameter, 1 for the second parameter, and
4146** so forth.
4147**
4148** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4149** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4150** is not a database file pathname pointer that the SQLite core passed
4151** into the xOpen VFS method, then the behavior of this routine is undefined
4152** and probably undesirable.
4153**
4154** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
4155** parameter can also be the name of a rollback journal file or WAL file
4156** in addition to the main database file. Prior to version 3.31.0, these
4157** routines would only work if F was the name of the main database file.
4158** When the F parameter is the name of the rollback journal or WAL file,
4159** it has access to all the same query parameters as were found on the
4160** main database file.
4161**
4162** See the [URI filename] documentation for additional information.
4163*/
4164SQLITE_API const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam);
4165SQLITE_API int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault);
4166SQLITE_API sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64);
4167SQLITE_API const char *sqlite3_uri_key(sqlite3_filename z, int N);
4168
4169/*
4170** CAPI3REF: Translate filenames
4171**
4172** These routines are available to [VFS|custom VFS implementations] for
4173** translating filenames between the main database file, the journal file,
4174** and the WAL file.
4175**
4176** If F is the name of an sqlite database file, journal file, or WAL file
4177** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
4178** returns the name of the corresponding database file.
4179**
4180** If F is the name of an sqlite database file, journal file, or WAL file
4181** passed by the SQLite core into the VFS, or if F is a database filename
4182** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
4183** returns the name of the corresponding rollback journal file.
4184**
4185** If F is the name of an sqlite database file, journal file, or WAL file
4186** that was passed by the SQLite core into the VFS, or if F is a database
4187** filename obtained from [sqlite3_db_filename()], then
4188** sqlite3_filename_wal(F) returns the name of the corresponding
4189** WAL file.
4190**
4191** In all of the above, if F is not the name of a database, journal or WAL
4192** filename passed into the VFS from the SQLite core and F is not the
4193** return value from [sqlite3_db_filename()], then the result is
4194** undefined and is likely a memory access violation.
4195*/
4196SQLITE_API const char *sqlite3_filename_database(sqlite3_filename);
4197SQLITE_API const char *sqlite3_filename_journal(sqlite3_filename);
4198SQLITE_API const char *sqlite3_filename_wal(sqlite3_filename);
4199
4200/*
4201** CAPI3REF: Database File Corresponding To A Journal
4202**
4203** ^If X is the name of a rollback or WAL-mode journal file that is
4204** passed into the xOpen method of [sqlite3_vfs], then
4205** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
4206** object that represents the main database file.
4207**
4208** This routine is intended for use in custom [VFS] implementations
4209** only. It is not a general-purpose interface.
4210** The argument sqlite3_file_object(X) must be a filename pointer that
4211** has been passed into [sqlite3_vfs].xOpen method where the
4212** flags parameter to xOpen contains one of the bits
4213** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use
4214** of this routine results in undefined and probably undesirable
4215** behavior.
4216*/
4217SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4218
4219/*
4220** CAPI3REF: Create and Destroy VFS Filenames
4221**
4222** These interfaces are provided for use by [VFS shim] implementations and
4223** are not useful outside of that context.
4224**
4225** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
4226** database filename D with corresponding journal file J and WAL file W and
4227** with N URI parameters key/values pairs in the array P. The result from
4228** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
4229** is safe to pass to routines like:
4230** <ul>
4231** <li> [sqlite3_uri_parameter()],
4232** <li> [sqlite3_uri_boolean()],
4233** <li> [sqlite3_uri_int64()],
4234** <li> [sqlite3_uri_key()],
4235** <li> [sqlite3_filename_database()],
4236** <li> [sqlite3_filename_journal()], or
4237** <li> [sqlite3_filename_wal()].
4238** </ul>
4239** If a memory allocation error occurs, sqlite3_create_filename() might
4240** return a NULL pointer. The memory obtained from sqlite3_create_filename(X)
4241** must be released by a corresponding call to sqlite3_free_filename(Y).
4242**
4243** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
4244** of 2*N pointers to strings. Each pair of pointers in this array corresponds
4245** to a key and value for a query parameter. The P parameter may be a NULL
4246** pointer if N is zero. None of the 2*N pointers in the P array may be
4247** NULL pointers and key pointers should not be empty strings.
4248** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
4249** be NULL pointers, though they can be empty strings.
4250**
4251** The sqlite3_free_filename(Y) routine releases a memory allocation
4252** previously obtained from sqlite3_create_filename(). Invoking
4253** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4254**
4255** If the Y parameter to sqlite3_free_filename(Y) is anything other
4256** than a NULL pointer or a pointer previously acquired from
4257** sqlite3_create_filename(), then bad things such as heap
4258** corruption or segfaults may occur. The value Y should not be
4259** used again after sqlite3_free_filename(Y) has been called. This means
4260** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
4261** then the corresponding [sqlite3_module.xClose() method should also be
4262** invoked prior to calling sqlite3_free_filename(Y).
4263*/
4264SQLITE_API sqlite3_filename sqlite3_create_filename(
4265 const char *zDatabase,
4266 const char *zJournal,
4267 const char *zWal,
4268 int nParam,
4269 const char **azParam
4270);
4271SQLITE_API void sqlite3_free_filename(sqlite3_filename);
4272
4273/*
4274** CAPI3REF: Error Codes And Messages
4275** METHOD: sqlite3
4276**
4277** ^If the most recent sqlite3_* API call associated with
4278** [database connection] D failed, then the sqlite3_errcode(D) interface
4279** returns the numeric [result code] or [extended result code] for that
4280** API call.
4281** ^The sqlite3_extended_errcode()
4282** interface is the same except that it always returns the
4283** [extended result code] even when extended result codes are
4284** disabled.
4285**
4286** The values returned by sqlite3_errcode() and/or
4287** sqlite3_extended_errcode() might change with each API call.
4288** Except, there are some interfaces that are guaranteed to never
4289** change the value of the error code. The error-code preserving
4290** interfaces include the following:
4291**
4292** <ul>
4293** <li> sqlite3_errcode()
4294** <li> sqlite3_extended_errcode()
4295** <li> sqlite3_errmsg()
4296** <li> sqlite3_errmsg16()
4297** <li> sqlite3_error_offset()
4298** </ul>
4299**
4300** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4301** text that describes the error, as either UTF-8 or UTF-16 respectively,
4302** or NULL if no error message is available.
4303** (See how SQLite handles [invalid UTF] for exceptions to this rule.)
4304** ^(Memory to hold the error message string is managed internally.
4305** The application does not need to worry about freeing the result.
4306** However, the error string might be overwritten or deallocated by
4307** subsequent calls to other SQLite interface functions.)^
4308**
4309** ^The sqlite3_errstr(E) interface returns the English-language text
4310** that describes the [result code] E, as UTF-8, or NULL if E is not an
4311** result code for which a text error message is available.
4312** ^(Memory to hold the error message string is managed internally
4313** and must not be freed by the application)^.
4314**
4315** ^If the most recent error references a specific token in the input
4316** SQL, the sqlite3_error_offset() interface returns the byte offset
4317** of the start of that token. ^The byte offset returned by
4318** sqlite3_error_offset() assumes that the input SQL is UTF8.
4319** ^If the most recent error does not reference a specific token in the input
4320** SQL, then the sqlite3_error_offset() function returns -1.
4321**
4322** When the serialized [threading mode] is in use, it might be the
4323** case that a second error occurs on a separate thread in between
4324** the time of the first error and the call to these interfaces.
4325** When that happens, the second error will be reported since these
4326** interfaces always report the most recent result. To avoid
4327** this, each thread can obtain exclusive use of the [database connection] D
4328** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4329** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4330** all calls to the interfaces listed here are completed.
4331**
4332** If an interface fails with SQLITE_MISUSE, that means the interface
4333** was invoked incorrectly by the application. In that case, the
4334** error code and message may or may not be set.
4335*/
4336SQLITE_API int sqlite3_errcode(sqlite3 *db);
4337SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4338SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4339SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4340SQLITE_API const char *sqlite3_errstr(int);
4341SQLITE_API int sqlite3_error_offset(sqlite3 *db);
4342
4343/*
4344** CAPI3REF: Prepared Statement Object
4345** KEYWORDS: {prepared statement} {prepared statements}
4346**
4347** An instance of this object represents a single SQL statement that
4348** has been compiled into binary form and is ready to be evaluated.
4349**
4350** Think of each SQL statement as a separate computer program. The
4351** original SQL text is source code. A prepared statement object
4352** is the compiled object code. All SQL must be converted into a
4353** prepared statement before it can be run.
4354**
4355** The life-cycle of a prepared statement object usually goes like this:
4356**
4357** <ol>
4358** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4359** <li> Bind values to [parameters] using the sqlite3_bind_*()
4360** interfaces.
4361** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4362** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4363** to step 2. Do this zero or more times.
4364** <li> Destroy the object using [sqlite3_finalize()].
4365** </ol>
4366*/
4367typedef struct sqlite3_stmt sqlite3_stmt;
4368
4369/*
4370** CAPI3REF: Run-time Limits
4371** METHOD: sqlite3
4372**
4373** ^(This interface allows the size of various constructs to be limited
4374** on a connection by connection basis. The first parameter is the
4375** [database connection] whose limit is to be set or queried. The
4376** second parameter is one of the [limit categories] that define a
4377** class of constructs to be size limited. The third parameter is the
4378** new limit for that construct.)^
4379**
4380** ^If the new limit is a negative number, the limit is unchanged.
4381** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4382** [limits | hard upper bound]
4383** set at compile-time by a C preprocessor macro called
4384** [limits | SQLITE_MAX_<i>NAME</i>].
4385** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4386** ^Attempts to increase a limit above its hard upper bound are
4387** silently truncated to the hard upper bound.
4388**
4389** ^Regardless of whether or not the limit was changed, the
4390** [sqlite3_limit()] interface returns the prior value of the limit.
4391** ^Hence, to find the current value of a limit without changing it,
4392** simply invoke this interface with the third parameter set to -1.
4393**
4394** Run-time limits are intended for use in applications that manage
4395** both their own internal database and also databases that are controlled
4396** by untrusted external sources. An example application might be a
4397** web browser that has its own databases for storing history and
4398** separate databases controlled by JavaScript applications downloaded
4399** off the Internet. The internal databases can be given the
4400** large, default limits. Databases managed by external sources can
4401** be given much smaller limits designed to prevent a denial of service
4402** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4403** interface to further control untrusted SQL. The size of the database
4404** created by an untrusted script can be contained using the
4405** [max_page_count] [PRAGMA].
4406**
4407** New run-time limit categories may be added in future releases.
4408*/
4409SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4410
4411/*
4412** CAPI3REF: Run-Time Limit Categories
4413** KEYWORDS: {limit category} {*limit categories}
4414**
4415** These constants define various performance limits
4416** that can be lowered at run-time using [sqlite3_limit()].
4417** The synopsis of the meanings of the various limits is shown below.
4418** Additional information is available at [limits | Limits in SQLite].
4419**
4420** <dl>
4421** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4422** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4423**
4424** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4425** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4426**
4427** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4428** <dd>The maximum number of columns in a table definition or in the
4429** result set of a [SELECT] or the maximum number of columns in an index
4430** or in an ORDER BY or GROUP BY clause.</dd>)^
4431**
4432** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4433** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4434**
4435** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4436** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4437**
4438** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4439** <dd>The maximum number of instructions in a virtual machine program
4440** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4441** the equivalent tries to allocate space for more than this many opcodes
4442** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4443**
4444** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4445** <dd>The maximum number of arguments on a function.</dd>)^
4446**
4447** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4448** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4449**
4450** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4451** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4452** <dd>The maximum length of the pattern argument to the [LIKE] or
4453** [GLOB] operators.</dd>)^
4454**
4455** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4456** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4457** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4458**
4459** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4460** <dd>The maximum depth of recursion for triggers.</dd>)^
4461**
4462** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4463** <dd>The maximum number of auxiliary worker threads that a single
4464** [prepared statement] may start.</dd>)^
4465** </dl>
4466*/
4467#define SQLITE_LIMIT_LENGTH0 0
4468#define SQLITE_LIMIT_SQL_LENGTH1 1
4469#define SQLITE_LIMIT_COLUMN2 2
4470#define SQLITE_LIMIT_EXPR_DEPTH3 3
4471#define SQLITE_LIMIT_COMPOUND_SELECT4 4
4472#define SQLITE_LIMIT_VDBE_OP5 5
4473#define SQLITE_LIMIT_FUNCTION_ARG6 6
4474#define SQLITE_LIMIT_ATTACHED7 7
4475#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH8 8
4476#define SQLITE_LIMIT_VARIABLE_NUMBER9 9
4477#define SQLITE_LIMIT_TRIGGER_DEPTH10 10
4478#define SQLITE_LIMIT_WORKER_THREADS11 11
4479
4480/*
4481** CAPI3REF: Prepare Flags
4482**
4483** These constants define various flags that can be passed into
4484** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4485** [sqlite3_prepare16_v3()] interfaces.
4486**
4487** New flags may be added in future releases of SQLite.
4488**
4489** <dl>
4490** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4491** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4492** that the prepared statement will be retained for a long time and
4493** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4494** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4495** be used just once or at most a few times and then destroyed using
4496** [sqlite3_finalize()] relatively soon. The current implementation acts
4497** on this hint by avoiding the use of [lookaside memory] so as not to
4498** deplete the limited store of lookaside memory. Future versions of
4499** SQLite may act on this hint differently.
4500**
4501** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4502** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4503** to be required for any prepared statement that wanted to use the
4504** [sqlite3_normalized_sql()] interface. However, the
4505** [sqlite3_normalized_sql()] interface is now available to all
4506** prepared statements, regardless of whether or not they use this
4507** flag.
4508**
4509** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4510** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4511** to return an error (error code SQLITE_ERROR) if the statement uses
4512** any virtual tables.
4513** </dl>
4514*/
4515#define SQLITE_PREPARE_PERSISTENT0x01 0x01
4516#define SQLITE_PREPARE_NORMALIZE0x02 0x02
4517#define SQLITE_PREPARE_NO_VTAB0x04 0x04
4518
4519/*
4520** CAPI3REF: Compiling An SQL Statement
4521** KEYWORDS: {SQL statement compiler}
4522** METHOD: sqlite3
4523** CONSTRUCTOR: sqlite3_stmt
4524**
4525** To execute an SQL statement, it must first be compiled into a byte-code
4526** program using one of these routines. Or, in other words, these routines
4527** are constructors for the [prepared statement] object.
4528**
4529** The preferred routine to use is [sqlite3_prepare_v2()]. The
4530** [sqlite3_prepare()] interface is legacy and should be avoided.
4531** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4532** for special purposes.
4533**
4534** The use of the UTF-8 interfaces is preferred, as SQLite currently
4535** does all parsing using UTF-8. The UTF-16 interfaces are provided
4536** as a convenience. The UTF-16 interfaces work by converting the
4537** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4538**
4539** The first argument, "db", is a [database connection] obtained from a
4540** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4541** [sqlite3_open16()]. The database connection must not have been closed.
4542**
4543** The second argument, "zSql", is the statement to be compiled, encoded
4544** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4545** and sqlite3_prepare_v3()
4546** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4547** and sqlite3_prepare16_v3() use UTF-16.
4548**
4549** ^If the nByte argument is negative, then zSql is read up to the
4550** first zero terminator. ^If nByte is positive, then it is the maximum
4551** number of bytes read from zSql. When nByte is positive, zSql is read
4552** up to the first zero terminator or until the nByte bytes have been read,
4553** whichever comes first. ^If nByte is zero, then no prepared
4554** statement is generated.
4555** If the caller knows that the supplied string is nul-terminated, then
4556** there is a small performance advantage to passing an nByte parameter that
4557** is the number of bytes in the input string <i>including</i>
4558** the nul-terminator.
4559** Note that nByte measure the length of the input in bytes, not
4560** characters, even for the UTF-16 interfaces.
4561**
4562** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4563** past the end of the first SQL statement in zSql. These routines only
4564** compile the first statement in zSql, so *pzTail is left pointing to
4565** what remains uncompiled.
4566**
4567** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4568** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4569** to NULL. ^If the input text contains no SQL (if the input is an empty
4570** string or a comment) then *ppStmt is set to NULL.
4571** The calling procedure is responsible for deleting the compiled
4572** SQL statement using [sqlite3_finalize()] after it has finished with it.
4573** ppStmt may not be NULL.
4574**
4575** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4576** otherwise an [error code] is returned.
4577**
4578** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4579** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4580** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4581** are retained for backwards compatibility, but their use is discouraged.
4582** ^In the "vX" interfaces, the prepared statement
4583** that is returned (the [sqlite3_stmt] object) contains a copy of the
4584** original SQL text. This causes the [sqlite3_step()] interface to
4585** behave differently in three ways:
4586**
4587** <ol>
4588** <li>
4589** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4590** always used to do, [sqlite3_step()] will automatically recompile the SQL
4591** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4592** retries will occur before sqlite3_step() gives up and returns an error.
4593** </li>
4594**
4595** <li>
4596** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4597** [error codes] or [extended error codes]. ^The legacy behavior was that
4598** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4599** and the application would have to make a second call to [sqlite3_reset()]
4600** in order to find the underlying cause of the problem. With the "v2" prepare
4601** interfaces, the underlying reason for the error is returned immediately.
4602** </li>
4603**
4604** <li>
4605** ^If the specific value bound to a [parameter | host parameter] in the
4606** WHERE clause might influence the choice of query plan for a statement,
4607** then the statement will be automatically recompiled, as if there had been
4608** a schema change, on the first [sqlite3_step()] call following any change
4609** to the [sqlite3_bind_text | bindings] of that [parameter].
4610** ^The specific value of a WHERE-clause [parameter] might influence the
4611** choice of query plan if the parameter is the left-hand side of a [LIKE]
4612** or [GLOB] operator or if the parameter is compared to an indexed column
4613** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4614** </li>
4615** </ol>
4616**
4617** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4618** the extra prepFlags parameter, which is a bit array consisting of zero or
4619** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4620** sqlite3_prepare_v2() interface works exactly the same as
4621** sqlite3_prepare_v3() with a zero prepFlags parameter.
4622*/
4623SQLITE_API int sqlite3_prepare(
4624 sqlite3 *db, /* Database handle */
4625 const char *zSql, /* SQL statement, UTF-8 encoded */
4626 int nByte, /* Maximum length of zSql in bytes. */
4627 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4628 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4629);
4630SQLITE_API int sqlite3_prepare_v2(
4631 sqlite3 *db, /* Database handle */
4632 const char *zSql, /* SQL statement, UTF-8 encoded */
4633 int nByte, /* Maximum length of zSql in bytes. */
4634 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4635 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4636);
4637SQLITE_API int sqlite3_prepare_v3(
4638 sqlite3 *db, /* Database handle */
4639 const char *zSql, /* SQL statement, UTF-8 encoded */
4640 int nByte, /* Maximum length of zSql in bytes. */
4641 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4642 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4643 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4644);
4645SQLITE_API int sqlite3_prepare16(
4646 sqlite3 *db, /* Database handle */
4647 const void *zSql, /* SQL statement, UTF-16 encoded */
4648 int nByte, /* Maximum length of zSql in bytes. */
4649 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4650 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4651);
4652SQLITE_API int sqlite3_prepare16_v2(
4653 sqlite3 *db, /* Database handle */
4654 const void *zSql, /* SQL statement, UTF-16 encoded */
4655 int nByte, /* Maximum length of zSql in bytes. */
4656 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4657 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4658);
4659SQLITE_API int sqlite3_prepare16_v3(
4660 sqlite3 *db, /* Database handle */
4661 const void *zSql, /* SQL statement, UTF-16 encoded */
4662 int nByte, /* Maximum length of zSql in bytes. */
4663 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4664 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4665 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4666);
4667
4668/*
4669** CAPI3REF: Retrieving Statement SQL
4670** METHOD: sqlite3_stmt
4671**
4672** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4673** SQL text used to create [prepared statement] P if P was
4674** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4675** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4676** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4677** string containing the SQL text of prepared statement P with
4678** [bound parameters] expanded.
4679** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4680** string containing the normalized SQL text of prepared statement P. The
4681** semantics used to normalize a SQL statement are unspecified and subject
4682** to change. At a minimum, literal values will be replaced with suitable
4683** placeholders.
4684**
4685** ^(For example, if a prepared statement is created using the SQL
4686** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4687** and parameter :xyz is unbound, then sqlite3_sql() will return
4688** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4689** will return "SELECT 2345,NULL".)^
4690**
4691** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4692** is available to hold the result, or if the result would exceed the
4693** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4694**
4695** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4696** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4697** option causes sqlite3_expanded_sql() to always return NULL.
4698**
4699** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4700** are managed by SQLite and are automatically freed when the prepared
4701** statement is finalized.
4702** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4703** is obtained from [sqlite3_malloc()] and must be freed by the application
4704** by passing it to [sqlite3_free()].
4705**
4706** ^The sqlite3_normalized_sql() interface is only available if
4707** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4708*/
4709SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4710SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4711#ifdef SQLITE_ENABLE_NORMALIZE
4712SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4713#endif
4714
4715/*
4716** CAPI3REF: Determine If An SQL Statement Writes The Database
4717** METHOD: sqlite3_stmt
4718**
4719** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4720** and only if the [prepared statement] X makes no direct changes to
4721** the content of the database file.
4722**
4723** Note that [application-defined SQL functions] or
4724** [virtual tables] might change the database indirectly as a side effect.
4725** ^(For example, if an application defines a function "eval()" that
4726** calls [sqlite3_exec()], then the following SQL statement would
4727** change the database file through side-effects:
4728**
4729** <blockquote><pre>
4730** SELECT eval('DELETE FROM t1') FROM t2;
4731** </pre></blockquote>
4732**
4733** But because the [SELECT] statement does not change the database file
4734** directly, sqlite3_stmt_readonly() would still return true.)^
4735**
4736** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4737** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4738** since the statements themselves do not actually modify the database but
4739** rather they control the timing of when other statements modify the
4740** database. ^The [ATTACH] and [DETACH] statements also cause
4741** sqlite3_stmt_readonly() to return true since, while those statements
4742** change the configuration of a database connection, they do not make
4743** changes to the content of the database files on disk.
4744** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4745** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4746** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4747** sqlite3_stmt_readonly() returns false for those commands.
4748**
4749** ^This routine returns false if there is any possibility that the
4750** statement might change the database file. ^A false return does
4751** not guarantee that the statement will change the database file.
4752** ^For example, an UPDATE statement might have a WHERE clause that
4753** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4754** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a
4755** read-only no-op if the table already exists, but
4756** sqlite3_stmt_readonly() still returns false for such a statement.
4757**
4758** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN]
4759** statement, then sqlite3_stmt_readonly(X) returns the same value as
4760** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted.
4761*/
4762SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4763
4764/*
4765** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4766** METHOD: sqlite3_stmt
4767**
4768** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4769** prepared statement S is an EXPLAIN statement, or 2 if the
4770** statement S is an EXPLAIN QUERY PLAN.
4771** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4772** an ordinary statement or a NULL pointer.
4773*/
4774SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4775
4776/*
4777** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement
4778** METHOD: sqlite3_stmt
4779**
4780** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN
4781** setting for [prepared statement] S. If E is zero, then S becomes
4782** a normal prepared statement. If E is 1, then S behaves as if
4783** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if
4784** its SQL text began with "[EXPLAIN QUERY PLAN]".
4785**
4786** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared.
4787** SQLite tries to avoid a reprepare, but a reprepare might be necessary
4788** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode.
4789**
4790** Because of the potential need to reprepare, a call to
4791** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be
4792** reprepared because it was created using [sqlite3_prepare()] instead of
4793** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and
4794** hence has no saved SQL text with which to reprepare.
4795**
4796** Changing the explain setting for a prepared statement does not change
4797** the original SQL text for the statement. Hence, if the SQL text originally
4798** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0)
4799** is called to convert the statement into an ordinary statement, the EXPLAIN
4800** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S)
4801** output, even though the statement now acts like a normal SQL statement.
4802**
4803** This routine returns SQLITE_OK if the explain mode is successfully
4804** changed, or an error code if the explain mode could not be changed.
4805** The explain mode cannot be changed while a statement is active.
4806** Hence, it is good practice to call [sqlite3_reset(S)]
4807** immediately prior to calling sqlite3_stmt_explain(S,E).
4808*/
4809SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode);
4810
4811/*
4812** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4813** METHOD: sqlite3_stmt
4814**
4815** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4816** [prepared statement] S has been stepped at least once using
4817** [sqlite3_step(S)] but has neither run to completion (returned
4818** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4819** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
4820** interface returns false if S is a NULL pointer. If S is not a
4821** NULL pointer and is not a pointer to a valid [prepared statement]
4822** object, then the behavior is undefined and probably undesirable.
4823**
4824** This interface can be used in combination [sqlite3_next_stmt()]
4825** to locate all prepared statements associated with a database
4826** connection that are in need of being reset. This can be used,
4827** for example, in diagnostic routines to search for prepared
4828** statements that are holding a transaction open.
4829*/
4830SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4831
4832/*
4833** CAPI3REF: Dynamically Typed Value Object
4834** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4835**
4836** SQLite uses the sqlite3_value object to represent all values
4837** that can be stored in a database table. SQLite uses dynamic typing
4838** for the values it stores. ^Values stored in sqlite3_value objects
4839** can be integers, floating point values, strings, BLOBs, or NULL.
4840**
4841** An sqlite3_value object may be either "protected" or "unprotected".
4842** Some interfaces require a protected sqlite3_value. Other interfaces
4843** will accept either a protected or an unprotected sqlite3_value.
4844** Every interface that accepts sqlite3_value arguments specifies
4845** whether or not it requires a protected sqlite3_value. The
4846** [sqlite3_value_dup()] interface can be used to construct a new
4847** protected sqlite3_value from an unprotected sqlite3_value.
4848**
4849** The terms "protected" and "unprotected" refer to whether or not
4850** a mutex is held. An internal mutex is held for a protected
4851** sqlite3_value object but no mutex is held for an unprotected
4852** sqlite3_value object. If SQLite is compiled to be single-threaded
4853** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4854** or if SQLite is run in one of reduced mutex modes
4855** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4856** then there is no distinction between protected and unprotected
4857** sqlite3_value objects and they can be used interchangeably. However,
4858** for maximum code portability it is recommended that applications
4859** still make the distinction between protected and unprotected
4860** sqlite3_value objects even when not strictly required.
4861**
4862** ^The sqlite3_value objects that are passed as parameters into the
4863** implementation of [application-defined SQL functions] are protected.
4864** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4865** are protected.
4866** ^The sqlite3_value object returned by
4867** [sqlite3_column_value()] is unprotected.
4868** Unprotected sqlite3_value objects may only be used as arguments
4869** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4870** [sqlite3_value_dup()].
4871** The [sqlite3_value_blob | sqlite3_value_type()] family of
4872** interfaces require protected sqlite3_value objects.
4873*/
4874typedef struct sqlite3_value sqlite3_value;
4875
4876/*
4877** CAPI3REF: SQL Function Context Object
4878**
4879** The context in which an SQL function executes is stored in an
4880** sqlite3_context object. ^A pointer to an sqlite3_context object
4881** is always first parameter to [application-defined SQL functions].
4882** The application-defined SQL function implementation will pass this
4883** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4884** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4885** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4886** and/or [sqlite3_set_auxdata()].
4887*/
4888typedef struct sqlite3_context sqlite3_context;
4889
4890/*
4891** CAPI3REF: Binding Values To Prepared Statements
4892** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4893** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4894** METHOD: sqlite3_stmt
4895**
4896** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4897** literals may be replaced by a [parameter] that matches one of following
4898** templates:
4899**
4900** <ul>
4901** <li> ?
4902** <li> ?NNN
4903** <li> :VVV
4904** <li> @VVV
4905** <li> $VVV
4906** </ul>
4907**
4908** In the templates above, NNN represents an integer literal,
4909** and VVV represents an alphanumeric identifier.)^ ^The values of these
4910** parameters (also called "host parameter names" or "SQL parameters")
4911** can be set using the sqlite3_bind_*() routines defined here.
4912**
4913** ^The first argument to the sqlite3_bind_*() routines is always
4914** a pointer to the [sqlite3_stmt] object returned from
4915** [sqlite3_prepare_v2()] or its variants.
4916**
4917** ^The second argument is the index of the SQL parameter to be set.
4918** ^The leftmost SQL parameter has an index of 1. ^When the same named
4919** SQL parameter is used more than once, second and subsequent
4920** occurrences have the same index as the first occurrence.
4921** ^The index for named parameters can be looked up using the
4922** [sqlite3_bind_parameter_index()] API if desired. ^The index
4923** for "?NNN" parameters is the value of NNN.
4924** ^The NNN value must be between 1 and the [sqlite3_limit()]
4925** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
4926**
4927** ^The third argument is the value to bind to the parameter.
4928** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4929** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4930** is ignored and the end result is the same as sqlite3_bind_null().
4931** ^If the third parameter to sqlite3_bind_text() is not NULL, then
4932** it should be a pointer to well-formed UTF8 text.
4933** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4934** it should be a pointer to well-formed UTF16 text.
4935** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4936** it should be a pointer to a well-formed unicode string that is
4937** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4938** otherwise.
4939**
4940** [[byte-order determination rules]] ^The byte-order of
4941** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4942** found in first character, which is removed, or in the absence of a BOM
4943** the byte order is the native byte order of the host
4944** machine for sqlite3_bind_text16() or the byte order specified in
4945** the 6th parameter for sqlite3_bind_text64().)^
4946** ^If UTF16 input text contains invalid unicode
4947** characters, then SQLite might change those invalid characters
4948** into the unicode replacement character: U+FFFD.
4949**
4950** ^(In those routines that have a fourth argument, its value is the
4951** number of bytes in the parameter. To be clear: the value is the
4952** number of <u>bytes</u> in the value, not the number of characters.)^
4953** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4954** is negative, then the length of the string is
4955** the number of bytes up to the first zero terminator.
4956** If the fourth parameter to sqlite3_bind_blob() is negative, then
4957** the behavior is undefined.
4958** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4959** or sqlite3_bind_text16() or sqlite3_bind_text64() then
4960** that parameter must be the byte offset
4961** where the NUL terminator would occur assuming the string were NUL
4962** terminated. If any NUL characters occurs at byte offsets less than
4963** the value of the fourth parameter then the resulting string value will
4964** contain embedded NULs. The result of expressions involving strings
4965** with embedded NULs is undefined.
4966**
4967** ^The fifth argument to the BLOB and string binding interfaces controls
4968** or indicates the lifetime of the object referenced by the third parameter.
4969** These three options exist:
4970** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished
4971** with it may be passed. ^It is called to dispose of the BLOB or string even
4972** if the call to the bind API fails, except the destructor is not called if
4973** the third parameter is a NULL pointer or the fourth parameter is negative.
4974** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that
4975** the application remains responsible for disposing of the object. ^In this
4976** case, the object and the provided pointer to it must remain valid until
4977** either the prepared statement is finalized or the same SQL parameter is
4978** bound to something else, whichever occurs sooner.
4979** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the
4980** object is to be copied prior to the return from sqlite3_bind_*(). ^The
4981** object and pointer to it must remain valid until then. ^SQLite will then
4982** manage the lifetime of its private copy.
4983**
4984** ^The sixth argument to sqlite3_bind_text64() must be one of
4985** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4986** to specify the encoding of the text in the third parameter. If
4987** the sixth argument to sqlite3_bind_text64() is not one of the
4988** allowed values shown above, or if the text encoding is different
4989** from the encoding specified by the sixth parameter, then the behavior
4990** is undefined.
4991**
4992** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
4993** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
4994** (just an integer to hold its size) while it is being processed.
4995** Zeroblobs are intended to serve as placeholders for BLOBs whose
4996** content is later written using
4997** [sqlite3_blob_open | incremental BLOB I/O] routines.
4998** ^A negative value for the zeroblob results in a zero-length BLOB.
4999**
5000** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5001** [prepared statement] S to have an SQL value of NULL, but to also be
5002** associated with the pointer P of type T. ^D is either a NULL pointer or
5003** a pointer to a destructor function for P. ^SQLite will invoke the
5004** destructor D with a single argument of P when it is finished using
5005** P. The T parameter should be a static string, preferably a string
5006** literal. The sqlite3_bind_pointer() routine is part of the
5007** [pointer passing interface] added for SQLite 3.20.0.
5008**
5009** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5010** for the [prepared statement] or with a prepared statement for which
5011** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5012** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
5013** routine is passed a [prepared statement] that has been finalized, the
5014** result is undefined and probably harmful.
5015**
5016** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5017** ^Unbound parameters are interpreted as NULL.
5018**
5019** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5020** [error code] if anything goes wrong.
5021** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5022** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5023** [SQLITE_MAX_LENGTH].
5024** ^[SQLITE_RANGE] is returned if the parameter
5025** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
5026**
5027** See also: [sqlite3_bind_parameter_count()],
5028** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5029*/
5030SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5031SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5032 void(*)(void*));
5033SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5034SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5035SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5036SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5037SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5038SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5039SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5040 void(*)(void*), unsigned char encoding);
5041SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5042SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5043SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5044SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5045
5046/*
5047** CAPI3REF: Number Of SQL Parameters
5048** METHOD: sqlite3_stmt
5049**
5050** ^This routine can be used to find the number of [SQL parameters]
5051** in a [prepared statement]. SQL parameters are tokens of the
5052** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5053** placeholders for values that are [sqlite3_bind_blob | bound]
5054** to the parameters at a later time.
5055**
5056** ^(This routine actually returns the index of the largest (rightmost)
5057** parameter. For all forms except ?NNN, this will correspond to the
5058** number of unique parameters. If parameters of the ?NNN form are used,
5059** there may be gaps in the list.)^
5060**
5061** See also: [sqlite3_bind_blob|sqlite3_bind()],
5062** [sqlite3_bind_parameter_name()], and
5063** [sqlite3_bind_parameter_index()].
5064*/
5065SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5066
5067/*
5068** CAPI3REF: Name Of A Host Parameter
5069** METHOD: sqlite3_stmt
5070**
5071** ^The sqlite3_bind_parameter_name(P,N) interface returns
5072** the name of the N-th [SQL parameter] in the [prepared statement] P.
5073** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5074** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5075** respectively.
5076** In other words, the initial ":" or "$" or "@" or "?"
5077** is included as part of the name.)^
5078** ^Parameters of the form "?" without a following integer have no name
5079** and are referred to as "nameless" or "anonymous parameters".
5080**
5081** ^The first host parameter has an index of 1, not 0.
5082**
5083** ^If the value N is out of range or if the N-th parameter is
5084** nameless, then NULL is returned. ^The returned string is
5085** always in UTF-8 encoding even if the named parameter was
5086** originally specified as UTF-16 in [sqlite3_prepare16()],
5087** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5088**
5089** See also: [sqlite3_bind_blob|sqlite3_bind()],
5090** [sqlite3_bind_parameter_count()], and
5091** [sqlite3_bind_parameter_index()].
5092*/
5093SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5094
5095/*
5096** CAPI3REF: Index Of A Parameter With A Given Name
5097** METHOD: sqlite3_stmt
5098**
5099** ^Return the index of an SQL parameter given its name. ^The
5100** index value returned is suitable for use as the second
5101** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5102** is returned if no matching parameter is found. ^The parameter
5103** name must be given in UTF-8 even if the original statement
5104** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5105** [sqlite3_prepare16_v3()].
5106**
5107** See also: [sqlite3_bind_blob|sqlite3_bind()],
5108** [sqlite3_bind_parameter_count()], and
5109** [sqlite3_bind_parameter_name()].
5110*/
5111SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5112
5113/*
5114** CAPI3REF: Reset All Bindings On A Prepared Statement
5115** METHOD: sqlite3_stmt
5116**
5117** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5118** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5119** ^Use this routine to reset all host parameters to NULL.
5120*/
5121SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5122
5123/*
5124** CAPI3REF: Number Of Columns In A Result Set
5125** METHOD: sqlite3_stmt
5126**
5127** ^Return the number of columns in the result set returned by the
5128** [prepared statement]. ^If this routine returns 0, that means the
5129** [prepared statement] returns no data (for example an [UPDATE]).
5130** ^However, just because this routine returns a positive number does not
5131** mean that one or more rows of data will be returned. ^A SELECT statement
5132** will always have a positive sqlite3_column_count() but depending on the
5133** WHERE clause constraints and the table content, it might return no rows.
5134**
5135** See also: [sqlite3_data_count()]
5136*/
5137SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5138
5139/*
5140** CAPI3REF: Column Names In A Result Set
5141** METHOD: sqlite3_stmt
5142**
5143** ^These routines return the name assigned to a particular column
5144** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5145** interface returns a pointer to a zero-terminated UTF-8 string
5146** and sqlite3_column_name16() returns a pointer to a zero-terminated
5147** UTF-16 string. ^The first parameter is the [prepared statement]
5148** that implements the [SELECT] statement. ^The second parameter is the
5149** column number. ^The leftmost column is number 0.
5150**
5151** ^The returned string pointer is valid until either the [prepared statement]
5152** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5153** reprepared by the first call to [sqlite3_step()] for a particular run
5154** or until the next call to
5155** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5156**
5157** ^If sqlite3_malloc() fails during the processing of either routine
5158** (for example during a conversion from UTF-8 to UTF-16) then a
5159** NULL pointer is returned.
5160**
5161** ^The name of a result column is the value of the "AS" clause for
5162** that column, if there is an AS clause. If there is no AS clause
5163** then the name of the column is unspecified and may change from
5164** one release of SQLite to the next.
5165*/
5166SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5167SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5168
5169/*
5170** CAPI3REF: Source Of Data In A Query Result
5171** METHOD: sqlite3_stmt
5172**
5173** ^These routines provide a means to determine the database, table, and
5174** table column that is the origin of a particular result column in
5175** [SELECT] statement.
5176** ^The name of the database or table or column can be returned as
5177** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5178** the database name, the _table_ routines return the table name, and
5179** the origin_ routines return the column name.
5180** ^The returned string is valid until the [prepared statement] is destroyed
5181** using [sqlite3_finalize()] or until the statement is automatically
5182** reprepared by the first call to [sqlite3_step()] for a particular run
5183** or until the same information is requested
5184** again in a different encoding.
5185**
5186** ^The names returned are the original un-aliased names of the
5187** database, table, and column.
5188**
5189** ^The first argument to these interfaces is a [prepared statement].
5190** ^These functions return information about the Nth result column returned by
5191** the statement, where N is the second function argument.
5192** ^The left-most column is column 0 for these routines.
5193**
5194** ^If the Nth column returned by the statement is an expression or
5195** subquery and is not a column value, then all of these functions return
5196** NULL. ^These routines might also return NULL if a memory allocation error
5197** occurs. ^Otherwise, they return the name of the attached database, table,
5198** or column that query result column was extracted from.
5199**
5200** ^As with all other SQLite APIs, those whose names end with "16" return
5201** UTF-16 encoded strings and the other functions return UTF-8.
5202**
5203** ^These APIs are only available if the library was compiled with the
5204** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5205**
5206** If two or more threads call one or more
5207** [sqlite3_column_database_name | column metadata interfaces]
5208** for the same [prepared statement] and result column
5209** at the same time then the results are undefined.
5210*/
5211SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5212SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5213SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5214SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5215SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5216SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5217
5218/*
5219** CAPI3REF: Declared Datatype Of A Query Result
5220** METHOD: sqlite3_stmt
5221**
5222** ^(The first parameter is a [prepared statement].
5223** If this statement is a [SELECT] statement and the Nth column of the
5224** returned result set of that [SELECT] is a table column (not an
5225** expression or subquery) then the declared type of the table
5226** column is returned.)^ ^If the Nth column of the result set is an
5227** expression or subquery, then a NULL pointer is returned.
5228** ^The returned string is always UTF-8 encoded.
5229**
5230** ^(For example, given the database schema:
5231**
5232** CREATE TABLE t1(c1 VARIANT);
5233**
5234** and the following statement to be compiled:
5235**
5236** SELECT c1 + 1, c1 FROM t1;
5237**
5238** this routine would return the string "VARIANT" for the second result
5239** column (i==1), and a NULL pointer for the first result column (i==0).)^
5240**
5241** ^SQLite uses dynamic run-time typing. ^So just because a column
5242** is declared to contain a particular type does not mean that the
5243** data stored in that column is of the declared type. SQLite is
5244** strongly typed, but the typing is dynamic not static. ^Type
5245** is associated with individual values, not with the containers
5246** used to hold those values.
5247*/
5248SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5249SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5250
5251/*
5252** CAPI3REF: Evaluate An SQL Statement
5253** METHOD: sqlite3_stmt
5254**
5255** After a [prepared statement] has been prepared using any of
5256** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5257** or [sqlite3_prepare16_v3()] or one of the legacy
5258** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5259** must be called one or more times to evaluate the statement.
5260**
5261** The details of the behavior of the sqlite3_step() interface depend
5262** on whether the statement was prepared using the newer "vX" interfaces
5263** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5264** [sqlite3_prepare16_v2()] or the older legacy
5265** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5266** new "vX" interface is recommended for new applications but the legacy
5267** interface will continue to be supported.
5268**
5269** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5270** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5271** ^With the "v2" interface, any of the other [result codes] or
5272** [extended result codes] might be returned as well.
5273**
5274** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5275** database locks it needs to do its job. ^If the statement is a [COMMIT]
5276** or occurs outside of an explicit transaction, then you can retry the
5277** statement. If the statement is not a [COMMIT] and occurs within an
5278** explicit transaction then you should rollback the transaction before
5279** continuing.
5280**
5281** ^[SQLITE_DONE] means that the statement has finished executing
5282** successfully. sqlite3_step() should not be called again on this virtual
5283** machine without first calling [sqlite3_reset()] to reset the virtual
5284** machine back to its initial state.
5285**
5286** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5287** is returned each time a new row of data is ready for processing by the
5288** caller. The values may be accessed using the [column access functions].
5289** sqlite3_step() is called again to retrieve the next row of data.
5290**
5291** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5292** violation) has occurred. sqlite3_step() should not be called again on
5293** the VM. More information may be found by calling [sqlite3_errmsg()].
5294** ^With the legacy interface, a more specific error code (for example,
5295** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5296** can be obtained by calling [sqlite3_reset()] on the
5297** [prepared statement]. ^In the "v2" interface,
5298** the more specific error code is returned directly by sqlite3_step().
5299**
5300** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5301** Perhaps it was called on a [prepared statement] that has
5302** already been [sqlite3_finalize | finalized] or on one that had
5303** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5304** be the case that the same database connection is being used by two or
5305** more threads at the same moment in time.
5306**
5307** For all versions of SQLite up to and including 3.6.23.1, a call to
5308** [sqlite3_reset()] was required after sqlite3_step() returned anything
5309** other than [SQLITE_ROW] before any subsequent invocation of
5310** sqlite3_step(). Failure to reset the prepared statement using
5311** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5312** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5313** sqlite3_step() began
5314** calling [sqlite3_reset()] automatically in this circumstance rather
5315** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5316** break because any application that ever receives an SQLITE_MISUSE error
5317** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5318** can be used to restore the legacy behavior.
5319**
5320** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5321** API always returns a generic error code, [SQLITE_ERROR], following any
5322** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5323** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5324** specific [error codes] that better describes the error.
5325** We admit that this is a goofy design. The problem has been fixed
5326** with the "v2" interface. If you prepare all of your SQL statements
5327** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5328** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5329** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5330** then the more specific [error codes] are returned directly
5331** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5332*/
5333SQLITE_API int sqlite3_step(sqlite3_stmt*);
5334
5335/*
5336** CAPI3REF: Number of columns in a result set
5337** METHOD: sqlite3_stmt
5338**
5339** ^The sqlite3_data_count(P) interface returns the number of columns in the
5340** current row of the result set of [prepared statement] P.
5341** ^If prepared statement P does not have results ready to return
5342** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
5343** interfaces) then sqlite3_data_count(P) returns 0.
5344** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5345** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5346** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5347** will return non-zero if previous call to [sqlite3_step](P) returned
5348** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5349** where it always returns zero since each step of that multi-step
5350** pragma returns 0 columns of data.
5351**
5352** See also: [sqlite3_column_count()]
5353*/
5354SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5355
5356/*
5357** CAPI3REF: Fundamental Datatypes
5358** KEYWORDS: SQLITE_TEXT
5359**
5360** ^(Every value in SQLite has one of five fundamental datatypes:
5361**
5362** <ul>
5363** <li> 64-bit signed integer
5364** <li> 64-bit IEEE floating point number
5365** <li> string
5366** <li> BLOB
5367** <li> NULL
5368** </ul>)^
5369**
5370** These constants are codes for each of those types.
5371**
5372** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5373** for a completely different meaning. Software that links against both
5374** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5375** SQLITE_TEXT.
5376*/
5377#define SQLITE_INTEGER1 1
5378#define SQLITE_FLOAT2 2
5379#define SQLITE_BLOB4 4
5380#define SQLITE_NULL5 5
5381#ifdef SQLITE_TEXT3
5382# undef SQLITE_TEXT3
5383#else
5384# define SQLITE_TEXT3 3
5385#endif
5386#define SQLITE3_TEXT3 3
5387
5388/*
5389** CAPI3REF: Result Values From A Query
5390** KEYWORDS: {column access functions}
5391** METHOD: sqlite3_stmt
5392**
5393** <b>Summary:</b>
5394** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5395** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5396** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5397** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5398** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5399** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5400** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5401** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5402** [sqlite3_value|unprotected sqlite3_value] object.
5403** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5404** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5405** or a UTF-8 TEXT result in bytes
5406** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5407** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5408** TEXT in bytes
5409** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5410** datatype of the result
5411** </table></blockquote>
5412**
5413** <b>Details:</b>
5414**
5415** ^These routines return information about a single column of the current
5416** result row of a query. ^In every case the first argument is a pointer
5417** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5418** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5419** and the second argument is the index of the column for which information
5420** should be returned. ^The leftmost column of the result set has the index 0.
5421** ^The number of columns in the result can be determined using
5422** [sqlite3_column_count()].
5423**
5424** If the SQL statement does not currently point to a valid row, or if the
5425** column index is out of range, the result is undefined.
5426** These routines may only be called when the most recent call to
5427** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5428** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5429** If any of these routines are called after [sqlite3_reset()] or
5430** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5431** something other than [SQLITE_ROW], the results are undefined.
5432** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5433** are called from a different thread while any of these routines
5434** are pending, then the results are undefined.
5435**
5436** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5437** each return the value of a result column in a specific data format. If
5438** the result column is not initially in the requested format (for example,
5439** if the query returns an integer but the sqlite3_column_text() interface
5440** is used to extract the value) then an automatic type conversion is performed.
5441**
5442** ^The sqlite3_column_type() routine returns the
5443** [SQLITE_INTEGER | datatype code] for the initial data type
5444** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5445** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5446** The return value of sqlite3_column_type() can be used to decide which
5447** of the first six interface should be used to extract the column value.
5448** The value returned by sqlite3_column_type() is only meaningful if no
5449** automatic type conversions have occurred for the value in question.
5450** After a type conversion, the result of calling sqlite3_column_type()
5451** is undefined, though harmless. Future
5452** versions of SQLite may change the behavior of sqlite3_column_type()
5453** following a type conversion.
5454**
5455** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5456** or sqlite3_column_bytes16() interfaces can be used to determine the size
5457** of that BLOB or string.
5458**
5459** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5460** routine returns the number of bytes in that BLOB or string.
5461** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5462** the string to UTF-8 and then returns the number of bytes.
5463** ^If the result is a numeric value then sqlite3_column_bytes() uses
5464** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5465** the number of bytes in that string.
5466** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5467**
5468** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5469** routine returns the number of bytes in that BLOB or string.
5470** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5471** the string to UTF-16 and then returns the number of bytes.
5472** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5473** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5474** the number of bytes in that string.
5475** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5476**
5477** ^The values returned by [sqlite3_column_bytes()] and
5478** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5479** of the string. ^For clarity: the values returned by
5480** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5481** bytes in the string, not the number of characters.
5482**
5483** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5484** even empty strings, are always zero-terminated. ^The return
5485** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5486**
5487** ^Strings returned by sqlite3_column_text16() always have the endianness
5488** which is native to the platform, regardless of the text encoding set
5489** for the database.
5490**
5491** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5492** [unprotected sqlite3_value] object. In a multithreaded environment,
5493** an unprotected sqlite3_value object may only be used safely with
5494** [sqlite3_bind_value()] and [sqlite3_result_value()].
5495** If the [unprotected sqlite3_value] object returned by
5496** [sqlite3_column_value()] is used in any other way, including calls
5497** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5498** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5499** Hence, the sqlite3_column_value() interface
5500** is normally only useful within the implementation of
5501** [application-defined SQL functions] or [virtual tables], not within
5502** top-level application code.
5503**
5504** These routines may attempt to convert the datatype of the result.
5505** ^For example, if the internal representation is FLOAT and a text result
5506** is requested, [sqlite3_snprintf()] is used internally to perform the
5507** conversion automatically. ^(The following table details the conversions
5508** that are applied:
5509**
5510** <blockquote>
5511** <table border="1">
5512** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5513**
5514** <tr><td> NULL <td> INTEGER <td> Result is 0
5515** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5516** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5517** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5518** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5519** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5520** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5521** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5522** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5523** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5524** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5525** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5526** <tr><td> TEXT <td> BLOB <td> No change
5527** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5528** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5529** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator
5530** </table>
5531** </blockquote>)^
5532**
5533** Note that when type conversions occur, pointers returned by prior
5534** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5535** sqlite3_column_text16() may be invalidated.
5536** Type conversions and pointer invalidations might occur
5537** in the following cases:
5538**
5539** <ul>
5540** <li> The initial content is a BLOB and sqlite3_column_text() or
5541** sqlite3_column_text16() is called. A zero-terminator might
5542** need to be added to the string.</li>
5543** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5544** sqlite3_column_text16() is called. The content must be converted
5545** to UTF-16.</li>
5546** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5547** sqlite3_column_text() is called. The content must be converted
5548** to UTF-8.</li>
5549** </ul>
5550**
5551** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5552** not invalidate a prior pointer, though of course the content of the buffer
5553** that the prior pointer references will have been modified. Other kinds
5554** of conversion are done in place when it is possible, but sometimes they
5555** are not possible and in those cases prior pointers are invalidated.
5556**
5557** The safest policy is to invoke these routines
5558** in one of the following ways:
5559**
5560** <ul>
5561** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5562** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5563** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5564** </ul>
5565**
5566** In other words, you should call sqlite3_column_text(),
5567** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5568** into the desired format, then invoke sqlite3_column_bytes() or
5569** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5570** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5571** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5572** with calls to sqlite3_column_bytes().
5573**
5574** ^The pointers returned are valid until a type conversion occurs as
5575** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5576** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5577** and BLOBs is freed automatically. Do not pass the pointers returned
5578** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5579** [sqlite3_free()].
5580**
5581** As long as the input parameters are correct, these routines will only
5582** fail if an out-of-memory error occurs during a format conversion.
5583** Only the following subset of interfaces are subject to out-of-memory
5584** errors:
5585**
5586** <ul>
5587** <li> sqlite3_column_blob()
5588** <li> sqlite3_column_text()
5589** <li> sqlite3_column_text16()
5590** <li> sqlite3_column_bytes()
5591** <li> sqlite3_column_bytes16()
5592** </ul>
5593**
5594** If an out-of-memory error occurs, then the return value from these
5595** routines is the same as if the column had contained an SQL NULL value.
5596** Valid SQL NULL returns can be distinguished from out-of-memory errors
5597** by invoking the [sqlite3_errcode()] immediately after the suspect
5598** return value is obtained and before any
5599** other SQLite interface is called on the same [database connection].
5600*/
5601SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5602SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5603SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5604SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5605SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5606SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5607SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5608SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5609SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5610SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5611
5612/*
5613** CAPI3REF: Destroy A Prepared Statement Object
5614** DESTRUCTOR: sqlite3_stmt
5615**
5616** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5617** ^If the most recent evaluation of the statement encountered no errors
5618** or if the statement is never been evaluated, then sqlite3_finalize() returns
5619** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5620** sqlite3_finalize(S) returns the appropriate [error code] or
5621** [extended error code].
5622**
5623** ^The sqlite3_finalize(S) routine can be called at any point during
5624** the life cycle of [prepared statement] S:
5625** before statement S is ever evaluated, after
5626** one or more calls to [sqlite3_reset()], or after any call
5627** to [sqlite3_step()] regardless of whether or not the statement has
5628** completed execution.
5629**
5630** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5631**
5632** The application must finalize every [prepared statement] in order to avoid
5633** resource leaks. It is a grievous error for the application to try to use
5634** a prepared statement after it has been finalized. Any use of a prepared
5635** statement after it has been finalized can result in undefined and
5636** undesirable behavior such as segfaults and heap corruption.
5637*/
5638SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5639
5640/*
5641** CAPI3REF: Reset A Prepared Statement Object
5642** METHOD: sqlite3_stmt
5643**
5644** The sqlite3_reset() function is called to reset a [prepared statement]
5645** object back to its initial state, ready to be re-executed.
5646** ^Any SQL statement variables that had values bound to them using
5647** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5648** Use [sqlite3_clear_bindings()] to reset the bindings.
5649**
5650** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5651** back to the beginning of its program.
5652**
5653** ^The return code from [sqlite3_reset(S)] indicates whether or not
5654** the previous evaluation of prepared statement S completed successfully.
5655** ^If [sqlite3_step(S)] has never before been called on S or if
5656** [sqlite3_step(S)] has not been called since the previous call
5657** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return
5658** [SQLITE_OK].
5659**
5660** ^If the most recent call to [sqlite3_step(S)] for the
5661** [prepared statement] S indicated an error, then
5662** [sqlite3_reset(S)] returns an appropriate [error code].
5663** ^The [sqlite3_reset(S)] interface might also return an [error code]
5664** if there were no prior errors but the process of resetting
5665** the prepared statement caused a new error. ^For example, if an
5666** [INSERT] statement with a [RETURNING] clause is only stepped one time,
5667** that one call to [sqlite3_step(S)] might return SQLITE_ROW but
5668** the overall statement might still fail and the [sqlite3_reset(S)] call
5669** might return SQLITE_BUSY if locking constraints prevent the
5670** database change from committing. Therefore, it is important that
5671** applications check the return code from [sqlite3_reset(S)] even if
5672** no prior call to [sqlite3_step(S)] indicated a problem.
5673**
5674** ^The [sqlite3_reset(S)] interface does not change the values
5675** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5676*/
5677SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5678
5679
5680/*
5681** CAPI3REF: Create Or Redefine SQL Functions
5682** KEYWORDS: {function creation routines}
5683** METHOD: sqlite3
5684**
5685** ^These functions (collectively known as "function creation routines")
5686** are used to add SQL functions or aggregates or to redefine the behavior
5687** of existing SQL functions or aggregates. The only differences between
5688** the three "sqlite3_create_function*" routines are the text encoding
5689** expected for the second parameter (the name of the function being
5690** created) and the presence or absence of a destructor callback for
5691** the application data pointer. Function sqlite3_create_window_function()
5692** is similar, but allows the user to supply the extra callback functions
5693** needed by [aggregate window functions].
5694**
5695** ^The first parameter is the [database connection] to which the SQL
5696** function is to be added. ^If an application uses more than one database
5697** connection then application-defined SQL functions must be added
5698** to each database connection separately.
5699**
5700** ^The second parameter is the name of the SQL function to be created or
5701** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5702** representation, exclusive of the zero-terminator. ^Note that the name
5703** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5704** ^Any attempt to create a function with a longer name
5705** will result in [SQLITE_MISUSE] being returned.
5706**
5707** ^The third parameter (nArg)
5708** is the number of arguments that the SQL function or
5709** aggregate takes. ^If this parameter is -1, then the SQL function or
5710** aggregate may take any number of arguments between 0 and the limit
5711** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5712** parameter is less than -1 or greater than 127 then the behavior is
5713** undefined.
5714**
5715** ^The fourth parameter, eTextRep, specifies what
5716** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5717** its parameters. The application should set this parameter to
5718** [SQLITE_UTF16LE] if the function implementation invokes
5719** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5720** implementation invokes [sqlite3_value_text16be()] on an input, or
5721** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5722** otherwise. ^The same SQL function may be registered multiple times using
5723** different preferred text encodings, with different implementations for
5724** each encoding.
5725** ^When multiple implementations of the same function are available, SQLite
5726** will pick the one that involves the least amount of data conversion.
5727**
5728** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5729** to signal that the function will always return the same result given
5730** the same inputs within a single SQL statement. Most SQL functions are
5731** deterministic. The built-in [random()] SQL function is an example of a
5732** function that is not deterministic. The SQLite query planner is able to
5733** perform additional optimizations on deterministic functions, so use
5734** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5735**
5736** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5737** flag, which if present prevents the function from being invoked from
5738** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
5739** index expressions, or the WHERE clause of partial indexes.
5740**
5741** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
5742** all application-defined SQL functions that do not need to be
5743** used inside of triggers, view, CHECK constraints, or other elements of
5744** the database schema. This flags is especially recommended for SQL
5745** functions that have side effects or reveal internal application state.
5746** Without this flag, an attacker might be able to modify the schema of
5747** a database file to include invocations of the function with parameters
5748** chosen by the attacker, which the application will then execute when
5749** the database file is opened and read.
5750**
5751** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5752** function can gain access to this pointer using [sqlite3_user_data()].)^
5753**
5754** ^The sixth, seventh and eighth parameters passed to the three
5755** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5756** pointers to C-language functions that implement the SQL function or
5757** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5758** callback only; NULL pointers must be passed as the xStep and xFinal
5759** parameters. ^An aggregate SQL function requires an implementation of xStep
5760** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5761** SQL function or aggregate, pass NULL pointers for all three function
5762** callbacks.
5763**
5764** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5765** and xInverse) passed to sqlite3_create_window_function are pointers to
5766** C-language callbacks that implement the new function. xStep and xFinal
5767** must both be non-NULL. xValue and xInverse may either both be NULL, in
5768** which case a regular aggregate function is created, or must both be
5769** non-NULL, in which case the new function may be used as either an aggregate
5770** or aggregate window function. More details regarding the implementation
5771** of aggregate window functions are
5772** [user-defined window functions|available here].
5773**
5774** ^(If the final parameter to sqlite3_create_function_v2() or
5775** sqlite3_create_window_function() is not NULL, then it is destructor for
5776** the application data pointer. The destructor is invoked when the function
5777** is deleted, either by being overloaded or when the database connection
5778** closes.)^ ^The destructor is also invoked if the call to
5779** sqlite3_create_function_v2() fails. ^When the destructor callback is
5780** invoked, it is passed a single argument which is a copy of the application
5781** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5782**
5783** ^It is permitted to register multiple implementations of the same
5784** functions with the same name but with either differing numbers of
5785** arguments or differing preferred text encodings. ^SQLite will use
5786** the implementation that most closely matches the way in which the
5787** SQL function is used. ^A function implementation with a non-negative
5788** nArg parameter is a better match than a function implementation with
5789** a negative nArg. ^A function where the preferred text encoding
5790** matches the database encoding is a better
5791** match than a function where the encoding is different.
5792** ^A function where the encoding difference is between UTF16le and UTF16be
5793** is a closer match than a function where the encoding difference is
5794** between UTF8 and UTF16.
5795**
5796** ^Built-in functions may be overloaded by new application-defined functions.
5797**
5798** ^An application-defined function is permitted to call other
5799** SQLite interfaces. However, such calls must not
5800** close the database connection nor finalize or reset the prepared
5801** statement in which the function is running.
5802*/
5803SQLITE_API int sqlite3_create_function(
5804 sqlite3 *db,
5805 const char *zFunctionName,
5806 int nArg,
5807 int eTextRep,
5808 void *pApp,
5809 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5810 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5811 void (*xFinal)(sqlite3_context*)
5812);
5813SQLITE_API int sqlite3_create_function16(
5814 sqlite3 *db,
5815 const void *zFunctionName,
5816 int nArg,
5817 int eTextRep,
5818 void *pApp,
5819 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5820 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5821 void (*xFinal)(sqlite3_context*)
5822);
5823SQLITE_API int sqlite3_create_function_v2(
5824 sqlite3 *db,
5825 const char *zFunctionName,
5826 int nArg,
5827 int eTextRep,
5828 void *pApp,
5829 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5830 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5831 void (*xFinal)(sqlite3_context*),
5832 void(*xDestroy)(void*)
5833);
5834SQLITE_API int sqlite3_create_window_function(
5835 sqlite3 *db,
5836 const char *zFunctionName,
5837 int nArg,
5838 int eTextRep,
5839 void *pApp,
5840 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5841 void (*xFinal)(sqlite3_context*),
5842 void (*xValue)(sqlite3_context*),
5843 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5844 void(*xDestroy)(void*)
5845);
5846
5847/*
5848** CAPI3REF: Text Encodings
5849**
5850** These constant define integer codes that represent the various
5851** text encodings supported by SQLite.
5852*/
5853#define SQLITE_UTF81 1 /* IMP: R-37514-35566 */
5854#define SQLITE_UTF16LE2 2 /* IMP: R-03371-37637 */
5855#define SQLITE_UTF16BE3 3 /* IMP: R-51971-34154 */
5856#define SQLITE_UTF164 4 /* Use native byte order */
5857#define SQLITE_ANY5 5 /* Deprecated */
5858#define SQLITE_UTF16_ALIGNED8 8 /* sqlite3_create_collation only */
5859
5860/*
5861** CAPI3REF: Function Flags
5862**
5863** These constants may be ORed together with the
5864** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5865** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5866** [sqlite3_create_function_v2()].
5867**
5868** <dl>
5869** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
5870** The SQLITE_DETERMINISTIC flag means that the new function always gives
5871** the same output when the input parameters are the same.
5872** The [abs|abs() function] is deterministic, for example, but
5873** [randomblob|randomblob()] is not. Functions must
5874** be deterministic in order to be used in certain contexts such as
5875** with the WHERE clause of [partial indexes] or in [generated columns].
5876** SQLite might also optimize deterministic functions by factoring them
5877** out of inner loops.
5878** </dd>
5879**
5880** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
5881** The SQLITE_DIRECTONLY flag means that the function may only be invoked
5882** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5883** schema structures such as [CHECK constraints], [DEFAULT clauses],
5884** [expression indexes], [partial indexes], or [generated columns].
5885** <p>
5886** The SQLITE_DIRECTONLY flag is recommended for any
5887** [application-defined SQL function]
5888** that has side-effects or that could potentially leak sensitive information.
5889** This will prevent attacks in which an application is tricked
5890** into using a database file that has had its schema surreptitiously
5891** modified to invoke the application-defined function in ways that are
5892** harmful.
5893** <p>
5894** Some people say it is good practice to set SQLITE_DIRECTONLY on all
5895** [application-defined SQL functions], regardless of whether or not they
5896** are security sensitive, as doing so prevents those functions from being used
5897** inside of the database schema, and thus ensures that the database
5898** can be inspected and modified using generic tools (such as the [CLI])
5899** that do not have access to the application-defined functions.
5900** </dd>
5901**
5902** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
5903** The SQLITE_INNOCUOUS flag means that the function is unlikely
5904** to cause problems even if misused. An innocuous function should have
5905** no side effects and should not depend on any values other than its
5906** input parameters. The [abs|abs() function] is an example of an
5907** innocuous function.
5908** The [load_extension() SQL function] is not innocuous because of its
5909** side effects.
5910** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
5911** exactly the same. The [random|random() function] is an example of a
5912** function that is innocuous but not deterministic.
5913** <p>Some heightened security settings
5914** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
5915** disable the use of SQL functions inside views and triggers and in
5916** schema structures such as [CHECK constraints], [DEFAULT clauses],
5917** [expression indexes], [partial indexes], and [generated columns] unless
5918** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5919** are innocuous. Developers are advised to avoid using the
5920** SQLITE_INNOCUOUS flag for application-defined functions unless the
5921** function has been carefully audited and found to be free of potentially
5922** security-adverse side-effects and information-leaks.
5923** </dd>
5924**
5925** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5926** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5927** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5928** This flag instructs SQLite to omit some corner-case optimizations that
5929** might disrupt the operation of the [sqlite3_value_subtype()] function,
5930** causing it to return zero rather than the correct subtype().
5931** All SQL functions that invoke [sqlite3_value_subtype()] should have this
5932** property. If the SQLITE_SUBTYPE property is omitted, then the return
5933** value from [sqlite3_value_subtype()] might sometimes be zero even though
5934** a non-zero subtype was specified by the function argument expression.
5935**
5936** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5937** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5938** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5939** result.
5940** Every function that invokes [sqlite3_result_subtype()] should have this
5941** property. If it does not, then the call to [sqlite3_result_subtype()]
5942** might become a no-op if the function is used as term in an
5943** [expression index]. On the other hand, SQL functions that never invoke
5944** [sqlite3_result_subtype()] should avoid setting this property, as the
5945** purpose of this property is to disable certain optimizations that are
5946** incompatible with subtypes.
5947**
5948** [[SQLITE_SELFORDER1]] <dt>SQLITE_SELFORDER1</dt><dd>
5949** The SQLITE_SELFORDER1 flag indicates that the function is an aggregate
5950** that internally orders the values provided to the first argument. The
5951** ordered-set aggregate SQL notation with a single ORDER BY term can be
5952** used to invoke this function. If the ordered-set aggregate notation is
5953** used on a function that lacks this flag, then an error is raised. Note
5954** that the ordered-set aggregate syntax is only available if SQLite is
5955** built using the -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option.
5956** </dd>
5957** </dl>
5958*/
5959#define SQLITE_DETERMINISTIC0x000000800 0x000000800
5960#define SQLITE_DIRECTONLY0x000080000 0x000080000
5961#define SQLITE_SUBTYPE0x000100000 0x000100000
5962#define SQLITE_INNOCUOUS0x000200000 0x000200000
5963#define SQLITE_RESULT_SUBTYPE0x001000000 0x001000000
5964#define SQLITE_SELFORDER10x002000000 0x002000000
5965
5966/*
5967** CAPI3REF: Deprecated Functions
5968** DEPRECATED
5969**
5970** These functions are [deprecated]. In order to maintain
5971** backwards compatibility with older code, these functions continue
5972** to be supported. However, new applications should avoid
5973** the use of these functions. To encourage programmers to avoid
5974** these functions, we will not explain what they do.
5975*/
5976#ifndef SQLITE_OMIT_DEPRECATED1
5977SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5978SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5979SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5980SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5981SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5982SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5983 void*,sqlite3_int64);
5984#endif
5985
5986/*
5987** CAPI3REF: Obtaining SQL Values
5988** METHOD: sqlite3_value
5989**
5990** <b>Summary:</b>
5991** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5992** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5993** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5994** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5995** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5996** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5997** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5998** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5999** the native byteorder
6000** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6001** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6002** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
6003** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
6004** or a UTF-8 TEXT in bytes
6005** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
6006** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6007** TEXT in bytes
6008** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
6009** datatype of the value
6010** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
6011** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
6012** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
6013** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
6014** against a virtual table.
6015** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
6016** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
6017** </table></blockquote>
6018**
6019** <b>Details:</b>
6020**
6021** These routines extract type, size, and content information from
6022** [protected sqlite3_value] objects. Protected sqlite3_value objects
6023** are used to pass parameter information into the functions that
6024** implement [application-defined SQL functions] and [virtual tables].
6025**
6026** These routines work only with [protected sqlite3_value] objects.
6027** Any attempt to use these routines on an [unprotected sqlite3_value]
6028** is not threadsafe.
6029**
6030** ^These routines work just like the corresponding [column access functions]
6031** except that these routines take a single [protected sqlite3_value] object
6032** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
6033**
6034** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6035** in the native byte-order of the host machine. ^The
6036** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
6037** extract UTF-16 strings as big-endian and little-endian respectively.
6038**
6039** ^If [sqlite3_value] object V was initialized
6040** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
6041** and if X and Y are strings that compare equal according to strcmp(X,Y),
6042** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
6043** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
6044** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6045**
6046** ^(The sqlite3_value_type(V) interface returns the
6047** [SQLITE_INTEGER | datatype code] for the initial datatype of the
6048** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
6049** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
6050** Other interfaces might change the datatype for an sqlite3_value object.
6051** For example, if the datatype is initially SQLITE_INTEGER and
6052** sqlite3_value_text(V) is called to extract a text value for that
6053** integer, then subsequent calls to sqlite3_value_type(V) might return
6054** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
6055** occurs is undefined and may change from one release of SQLite to the next.
6056**
6057** ^(The sqlite3_value_numeric_type() interface attempts to apply
6058** numeric affinity to the value. This means that an attempt is
6059** made to convert the value to an integer or floating point. If
6060** such a conversion is possible without loss of information (in other
6061** words, if the value is a string that looks like a number)
6062** then the conversion is performed. Otherwise no conversion occurs.
6063** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6064**
6065** ^Within the [xUpdate] method of a [virtual table], the
6066** sqlite3_value_nochange(X) interface returns true if and only if
6067** the column corresponding to X is unchanged by the UPDATE operation
6068** that the xUpdate method call was invoked to implement and if
6069** and the prior [xColumn] method call that was invoked to extracted
6070** the value for that column returned without setting a result (probably
6071** because it queried [sqlite3_vtab_nochange()] and found that the column
6072** was unchanging). ^Within an [xUpdate] method, any value for which
6073** sqlite3_value_nochange(X) is true will in all other respects appear
6074** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
6075** than within an [xUpdate] method call for an UPDATE statement, then
6076** the return value is arbitrary and meaningless.
6077**
6078** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6079** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6080** interfaces. ^If X comes from an SQL literal value, or a table column,
6081** or an expression, then sqlite3_value_frombind(X) returns zero.
6082**
6083** Please pay particular attention to the fact that the pointer returned
6084** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6085** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6086** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6087** or [sqlite3_value_text16()].
6088**
6089** These routines must be called from the same thread as
6090** the SQL function that supplied the [sqlite3_value*] parameters.
6091**
6092** As long as the input parameter is correct, these routines can only
6093** fail if an out-of-memory error occurs during a format conversion.
6094** Only the following subset of interfaces are subject to out-of-memory
6095** errors:
6096**
6097** <ul>
6098** <li> sqlite3_value_blob()
6099** <li> sqlite3_value_text()
6100** <li> sqlite3_value_text16()
6101** <li> sqlite3_value_text16le()
6102** <li> sqlite3_value_text16be()
6103** <li> sqlite3_value_bytes()
6104** <li> sqlite3_value_bytes16()
6105** </ul>
6106**
6107** If an out-of-memory error occurs, then the return value from these
6108** routines is the same as if the column had contained an SQL NULL value.
6109** Valid SQL NULL returns can be distinguished from out-of-memory errors
6110** by invoking the [sqlite3_errcode()] immediately after the suspect
6111** return value is obtained and before any
6112** other SQLite interface is called on the same [database connection].
6113*/
6114SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6115SQLITE_API double sqlite3_value_double(sqlite3_value*);
6116SQLITE_API int sqlite3_value_int(sqlite3_value*);
6117SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6118SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6119SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6120SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6121SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6122SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6123SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6124SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6125SQLITE_API int sqlite3_value_type(sqlite3_value*);
6126SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6127SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6128SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6129
6130/*
6131** CAPI3REF: Report the internal text encoding state of an sqlite3_value object
6132** METHOD: sqlite3_value
6133**
6134** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
6135** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding
6136** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
6137** returns something other than SQLITE_TEXT, then the return value from
6138** sqlite3_value_encoding(X) is meaningless. ^Calls to
6139** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)],
6140** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or
6141** [sqlite3_value_bytes16(X)] might change the encoding of the value X and
6142** thus change the return from subsequent calls to sqlite3_value_encoding(X).
6143**
6144** This routine is intended for used by applications that test and validate
6145** the SQLite implementation. This routine is inquiring about the opaque
6146** internal state of an [sqlite3_value] object. Ordinary applications should
6147** not need to know what the internal state of an sqlite3_value object is and
6148** hence should not need to use this interface.
6149*/
6150SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6151
6152/*
6153** CAPI3REF: Finding The Subtype Of SQL Values
6154** METHOD: sqlite3_value
6155**
6156** The sqlite3_value_subtype(V) function returns the subtype for
6157** an [application-defined SQL function] argument V. The subtype
6158** information can be used to pass a limited amount of context from
6159** one SQL function to another. Use the [sqlite3_result_subtype()]
6160** routine to set the subtype for the return value of an SQL function.
6161**
6162** Every [application-defined SQL function] that invokes this interface
6163** should include the [SQLITE_SUBTYPE] property in the text
6164** encoding argument when the function is [sqlite3_create_function|registered].
6165** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6166** might return zero instead of the upstream subtype in some corner cases.
6167*/
6168SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6169
6170/*
6171** CAPI3REF: Copy And Free SQL Values
6172** METHOD: sqlite3_value
6173**
6174** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6175** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
6176** is a [protected sqlite3_value] object even if the input is not.
6177** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6178** memory allocation fails. ^If V is a [pointer value], then the result
6179** of sqlite3_value_dup(V) is a NULL value.
6180**
6181** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6182** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6183** then sqlite3_value_free(V) is a harmless no-op.
6184*/
6185SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6186SQLITE_API void sqlite3_value_free(sqlite3_value*);
6187
6188/*
6189** CAPI3REF: Obtain Aggregate Function Context
6190** METHOD: sqlite3_context
6191**
6192** Implementations of aggregate SQL functions use this
6193** routine to allocate memory for storing their state.
6194**
6195** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6196** for a particular aggregate function, SQLite allocates
6197** N bytes of memory, zeroes out that memory, and returns a pointer
6198** to the new memory. ^On second and subsequent calls to
6199** sqlite3_aggregate_context() for the same aggregate function instance,
6200** the same buffer is returned. Sqlite3_aggregate_context() is normally
6201** called once for each invocation of the xStep callback and then one
6202** last time when the xFinal callback is invoked. ^(When no rows match
6203** an aggregate query, the xStep() callback of the aggregate function
6204** implementation is never called and xFinal() is called exactly once.
6205** In those cases, sqlite3_aggregate_context() might be called for the
6206** first time from within xFinal().)^
6207**
6208** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6209** when first called if N is less than or equal to zero or if a memory
6210** allocation error occurs.
6211**
6212** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6213** determined by the N parameter on first successful call. Changing the
6214** value of N in any subsequent call to sqlite3_aggregate_context() within
6215** the same aggregate function instance will not resize the memory
6216** allocation.)^ Within the xFinal callback, it is customary to set
6217** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6218** pointless memory allocations occur.
6219**
6220** ^SQLite automatically frees the memory allocated by
6221** sqlite3_aggregate_context() when the aggregate query concludes.
6222**
6223** The first parameter must be a copy of the
6224** [sqlite3_context | SQL function context] that is the first parameter
6225** to the xStep or xFinal callback routine that implements the aggregate
6226** function.
6227**
6228** This routine must be called from the same thread in which
6229** the aggregate SQL function is running.
6230*/
6231SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6232
6233/*
6234** CAPI3REF: User Data For Functions
6235** METHOD: sqlite3_context
6236**
6237** ^The sqlite3_user_data() interface returns a copy of
6238** the pointer that was the pUserData parameter (the 5th parameter)
6239** of the [sqlite3_create_function()]
6240** and [sqlite3_create_function16()] routines that originally
6241** registered the application defined function.
6242**
6243** This routine must be called from the same thread in which
6244** the application-defined function is running.
6245*/
6246SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6247
6248/*
6249** CAPI3REF: Database Connection For Functions
6250** METHOD: sqlite3_context
6251**
6252** ^The sqlite3_context_db_handle() interface returns a copy of
6253** the pointer to the [database connection] (the 1st parameter)
6254** of the [sqlite3_create_function()]
6255** and [sqlite3_create_function16()] routines that originally
6256** registered the application defined function.
6257*/
6258SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6259
6260/*
6261** CAPI3REF: Function Auxiliary Data
6262** METHOD: sqlite3_context
6263**
6264** These functions may be used by (non-aggregate) SQL functions to
6265** associate auxiliary data with argument values. If the same argument
6266** value is passed to multiple invocations of the same SQL function during
6267** query execution, under some circumstances the associated auxiliary data
6268** might be preserved. An example of where this might be useful is in a
6269** regular-expression matching function. The compiled version of the regular
6270** expression can be stored as auxiliary data associated with the pattern string.
6271** Then as long as the pattern string remains the same,
6272** the compiled regular expression can be reused on multiple
6273** invocations of the same function.
6274**
6275** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the auxiliary data
6276** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6277** value to the application-defined function. ^N is zero for the left-most
6278** function argument. ^If there is no auxiliary data
6279** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6280** returns a NULL pointer.
6281**
6282** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as auxiliary data for the
6283** N-th argument of the application-defined function. ^Subsequent
6284** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6285** sqlite3_set_auxdata(C,N,P,X) call if the auxiliary data is still valid or
6286** NULL if the auxiliary data has been discarded.
6287** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6288** SQLite will invoke the destructor function X with parameter P exactly
6289** once, when the auxiliary data is discarded.
6290** SQLite is free to discard the auxiliary data at any time, including: <ul>
6291** <li> ^(when the corresponding function parameter changes)^, or
6292** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6293** SQL statement)^, or
6294** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6295** parameter)^, or
6296** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6297** allocation error occurs.)^
6298** <li> ^(during the original sqlite3_set_auxdata() call if the function
6299** is evaluated during query planning instead of during query execution,
6300** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
6301**
6302** Note the last two bullets in particular. The destructor X in
6303** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6304** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6305** should be called near the end of the function implementation and the
6306** function implementation should not make any use of P after
6307** sqlite3_set_auxdata() has been called. Furthermore, a call to
6308** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6309** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6310** condition occurred during the sqlite3_set_auxdata() call or if the
6311** function is being evaluated during query planning rather than during
6312** query execution.
6313**
6314** ^(In practice, auxiliary data is preserved between function calls for
6315** function parameters that are compile-time constants, including literal
6316** values and [parameters] and expressions composed from the same.)^
6317**
6318** The value of the N parameter to these interfaces should be non-negative.
6319** Future enhancements may make use of negative N values to define new
6320** kinds of function caching behavior.
6321**
6322** These routines must be called from the same thread in which
6323** the SQL function is running.
6324**
6325** See also: [sqlite3_get_clientdata()] and [sqlite3_set_clientdata()].
6326*/
6327SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6328SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6329
6330/*
6331** CAPI3REF: Database Connection Client Data
6332** METHOD: sqlite3
6333**
6334** These functions are used to associate one or more named pointers
6335** with a [database connection].
6336** A call to sqlite3_set_clientdata(D,N,P,X) causes the pointer P
6337** to be attached to [database connection] D using name N. Subsequent
6338** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P
6339** or a NULL pointer if there were no prior calls to
6340** sqlite3_set_clientdata() with the same values of D and N.
6341** Names are compared using strcmp() and are thus case sensitive.
6342**
6343** If P and X are both non-NULL, then the destructor X is invoked with
6344** argument P on the first of the following occurrences:
6345** <ul>
6346** <li> An out-of-memory error occurs during the call to
6347** sqlite3_set_clientdata() which attempts to register pointer P.
6348** <li> A subsequent call to sqlite3_set_clientdata(D,N,P,X) is made
6349** with the same D and N parameters.
6350** <li> The database connection closes. SQLite does not make any guarantees
6351** about the order in which destructors are called, only that all
6352** destructors will be called exactly once at some point during the
6353** database connection closing process.
6354** </ul>
6355**
6356** SQLite does not do anything with client data other than invoke
6357** destructors on the client data at the appropriate time. The intended
6358** use for client data is to provide a mechanism for wrapper libraries
6359** to store additional information about an SQLite database connection.
6360**
6361** There is no limit (other than available memory) on the number of different
6362** client data pointers (with different names) that can be attached to a
6363** single database connection. However, the implementation is optimized
6364** for the case of having only one or two different client data names.
6365** Applications and wrapper libraries are discouraged from using more than
6366** one client data name each.
6367**
6368** There is no way to enumerate the client data pointers
6369** associated with a database connection. The N parameter can be thought
6370** of as a secret key such that only code that knows the secret key is able
6371** to access the associated data.
6372**
6373** Security Warning: These interfaces should not be exposed in scripting
6374** languages or in other circumstances where it might be possible for an
6375** an attacker to invoke them. Any agent that can invoke these interfaces
6376** can probably also take control of the process.
6377**
6378** Database connection client data is only available for SQLite
6379** version 3.44.0 ([dateof:3.44.0]) and later.
6380**
6381** See also: [sqlite3_set_auxdata()] and [sqlite3_get_auxdata()].
6382*/
6383SQLITE_API void *sqlite3_get_clientdata(sqlite3*,const char*);
6384SQLITE_API int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*));
6385
6386/*
6387** CAPI3REF: Constants Defining Special Destructor Behavior
6388**
6389** These are special values for the destructor that is passed in as the
6390** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6391** argument is SQLITE_STATIC, it means that the content pointer is constant
6392** and will never change. It does not need to be destroyed. ^The
6393** SQLITE_TRANSIENT value means that the content will likely change in
6394** the near future and that SQLite should make its own private copy of
6395** the content before returning.
6396**
6397** The typedef is necessary to work around problems in certain
6398** C++ compilers.
6399*/
6400typedef void (*sqlite3_destructor_type)(void*);
6401#define SQLITE_STATIC((sqlite3_destructor_type)0) ((sqlite3_destructor_type)0)
6402#define SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ((sqlite3_destructor_type)-1)
6403
6404/*
6405** CAPI3REF: Setting The Result Of An SQL Function
6406** METHOD: sqlite3_context
6407**
6408** These routines are used by the xFunc or xFinal callbacks that
6409** implement SQL functions and aggregates. See
6410** [sqlite3_create_function()] and [sqlite3_create_function16()]
6411** for additional information.
6412**
6413** These functions work very much like the [parameter binding] family of
6414** functions used to bind values to host parameters in prepared statements.
6415** Refer to the [SQL parameter] documentation for additional information.
6416**
6417** ^The sqlite3_result_blob() interface sets the result from
6418** an application-defined function to be the BLOB whose content is pointed
6419** to by the second parameter and which is N bytes long where N is the
6420** third parameter.
6421**
6422** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6423** interfaces set the result of the application-defined function to be
6424** a BLOB containing all zero bytes and N bytes in size.
6425**
6426** ^The sqlite3_result_double() interface sets the result from
6427** an application-defined function to be a floating point value specified
6428** by its 2nd argument.
6429**
6430** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6431** cause the implemented SQL function to throw an exception.
6432** ^SQLite uses the string pointed to by the
6433** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6434** as the text of an error message. ^SQLite interprets the error
6435** message string from sqlite3_result_error() as UTF-8. ^SQLite
6436** interprets the string from sqlite3_result_error16() as UTF-16 using
6437** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6438** ^If the third parameter to sqlite3_result_error()
6439** or sqlite3_result_error16() is negative then SQLite takes as the error
6440** message all text up through the first zero character.
6441** ^If the third parameter to sqlite3_result_error() or
6442** sqlite3_result_error16() is non-negative then SQLite takes that many
6443** bytes (not characters) from the 2nd parameter as the error message.
6444** ^The sqlite3_result_error() and sqlite3_result_error16()
6445** routines make a private copy of the error message text before
6446** they return. Hence, the calling function can deallocate or
6447** modify the text after they return without harm.
6448** ^The sqlite3_result_error_code() function changes the error code
6449** returned by SQLite as a result of an error in a function. ^By default,
6450** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6451** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6452**
6453** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6454** error indicating that a string or BLOB is too long to represent.
6455**
6456** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6457** error indicating that a memory allocation failed.
6458**
6459** ^The sqlite3_result_int() interface sets the return value
6460** of the application-defined function to be the 32-bit signed integer
6461** value given in the 2nd argument.
6462** ^The sqlite3_result_int64() interface sets the return value
6463** of the application-defined function to be the 64-bit signed integer
6464** value given in the 2nd argument.
6465**
6466** ^The sqlite3_result_null() interface sets the return value
6467** of the application-defined function to be NULL.
6468**
6469** ^The sqlite3_result_text(), sqlite3_result_text16(),
6470** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6471** set the return value of the application-defined function to be
6472** a text string which is represented as UTF-8, UTF-16 native byte order,
6473** UTF-16 little endian, or UTF-16 big endian, respectively.
6474** ^The sqlite3_result_text64() interface sets the return value of an
6475** application-defined function to be a text string in an encoding
6476** specified by the fifth (and last) parameter, which must be one
6477** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6478** ^SQLite takes the text result from the application from
6479** the 2nd parameter of the sqlite3_result_text* interfaces.
6480** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces
6481** other than sqlite3_result_text64() is negative, then SQLite computes
6482** the string length itself by searching the 2nd parameter for the first
6483** zero character.
6484** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6485** is non-negative, then as many bytes (not characters) of the text
6486** pointed to by the 2nd parameter are taken as the application-defined
6487** function result. If the 3rd parameter is non-negative, then it
6488** must be the byte offset into the string where the NUL terminator would
6489** appear if the string where NUL terminated. If any NUL characters occur
6490** in the string at a byte offset that is less than the value of the 3rd
6491** parameter, then the resulting string will contain embedded NULs and the
6492** result of expressions operating on strings with embedded NULs is undefined.
6493** ^If the 4th parameter to the sqlite3_result_text* interfaces
6494** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6495** function as the destructor on the text or BLOB result when it has
6496** finished using that result.
6497** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6498** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6499** assumes that the text or BLOB result is in constant space and does not
6500** copy the content of the parameter nor call a destructor on the content
6501** when it has finished using that result.
6502** ^If the 4th parameter to the sqlite3_result_text* interfaces
6503** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6504** then SQLite makes a copy of the result into space obtained
6505** from [sqlite3_malloc()] before it returns.
6506**
6507** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
6508** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
6509** when the encoding is not UTF8, if the input UTF16 begins with a
6510** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6511** string and the rest of the string is interpreted according to the
6512** byte-order specified by the BOM. ^The byte-order specified by
6513** the BOM at the beginning of the text overrides the byte-order
6514** specified by the interface procedure. ^So, for example, if
6515** sqlite3_result_text16le() is invoked with text that begins
6516** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6517** first two bytes of input are skipped and the remaining input
6518** is interpreted as UTF16BE text.
6519**
6520** ^For UTF16 input text to the sqlite3_result_text16(),
6521** sqlite3_result_text16be(), sqlite3_result_text16le(), and
6522** sqlite3_result_text64() routines, if the text contains invalid
6523** UTF16 characters, the invalid characters might be converted
6524** into the unicode replacement character, U+FFFD.
6525**
6526** ^The sqlite3_result_value() interface sets the result of
6527** the application-defined function to be a copy of the
6528** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6529** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6530** so that the [sqlite3_value] specified in the parameter may change or
6531** be deallocated after sqlite3_result_value() returns without harm.
6532** ^A [protected sqlite3_value] object may always be used where an
6533** [unprotected sqlite3_value] object is required, so either
6534** kind of [sqlite3_value] object can be used with this interface.
6535**
6536** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6537** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6538** also associates the host-language pointer P or type T with that
6539** NULL value such that the pointer can be retrieved within an
6540** [application-defined SQL function] using [sqlite3_value_pointer()].
6541** ^If the D parameter is not NULL, then it is a pointer to a destructor
6542** for the P parameter. ^SQLite invokes D with P as its only argument
6543** when SQLite is finished with P. The T parameter should be a static
6544** string and preferably a string literal. The sqlite3_result_pointer()
6545** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6546**
6547** If these routines are called from within the different thread
6548** than the one containing the application-defined function that received
6549** the [sqlite3_context] pointer, the results are undefined.
6550*/
6551SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6552SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6553 sqlite3_uint64,void(*)(void*));
6554SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6555SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6556SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6557SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6558SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6559SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6560SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6561SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6562SQLITE_API void sqlite3_result_null(sqlite3_context*);
6563SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6564SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6565 void(*)(void*), unsigned char encoding);
6566SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6567SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6568SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6569SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6570SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6571SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6572SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6573
6574
6575/*
6576** CAPI3REF: Setting The Subtype Of An SQL Function
6577** METHOD: sqlite3_context
6578**
6579** The sqlite3_result_subtype(C,T) function causes the subtype of
6580** the result from the [application-defined SQL function] with
6581** [sqlite3_context] C to be the value T. Only the lower 8 bits
6582** of the subtype T are preserved in current versions of SQLite;
6583** higher order bits are discarded.
6584** The number of subtype bytes preserved by SQLite might increase
6585** in future releases of SQLite.
6586**
6587** Every [application-defined SQL function] that invokes this interface
6588** should include the [SQLITE_RESULT_SUBTYPE] property in its
6589** text encoding argument when the SQL function is
6590** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6591** property is omitted from the function that invokes sqlite3_result_subtype(),
6592** then in some cases the sqlite3_result_subtype() might fail to set
6593** the result subtype.
6594**
6595** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6596** SQL function that invokes the sqlite3_result_subtype() interface
6597** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6598** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6599** by default.
6600*/
6601SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6602
6603/*
6604** CAPI3REF: Define New Collating Sequences
6605** METHOD: sqlite3
6606**
6607** ^These functions add, remove, or modify a [collation] associated
6608** with the [database connection] specified as the first argument.
6609**
6610** ^The name of the collation is a UTF-8 string
6611** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6612** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6613** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6614** considered to be the same name.
6615**
6616** ^(The third argument (eTextRep) must be one of the constants:
6617** <ul>
6618** <li> [SQLITE_UTF8],
6619** <li> [SQLITE_UTF16LE],
6620** <li> [SQLITE_UTF16BE],
6621** <li> [SQLITE_UTF16], or
6622** <li> [SQLITE_UTF16_ALIGNED].
6623** </ul>)^
6624** ^The eTextRep argument determines the encoding of strings passed
6625** to the collating function callback, xCompare.
6626** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6627** force strings to be UTF16 with native byte order.
6628** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6629** on an even byte address.
6630**
6631** ^The fourth argument, pArg, is an application data pointer that is passed
6632** through as the first argument to the collating function callback.
6633**
6634** ^The fifth argument, xCompare, is a pointer to the collating function.
6635** ^Multiple collating functions can be registered using the same name but
6636** with different eTextRep parameters and SQLite will use whichever
6637** function requires the least amount of data transformation.
6638** ^If the xCompare argument is NULL then the collating function is
6639** deleted. ^When all collating functions having the same name are deleted,
6640** that collation is no longer usable.
6641**
6642** ^The collating function callback is invoked with a copy of the pArg
6643** application data pointer and with two strings in the encoding specified
6644** by the eTextRep argument. The two integer parameters to the collating
6645** function callback are the length of the two strings, in bytes. The collating
6646** function must return an integer that is negative, zero, or positive
6647** if the first string is less than, equal to, or greater than the second,
6648** respectively. A collating function must always return the same answer
6649** given the same inputs. If two or more collating functions are registered
6650** to the same collation name (using different eTextRep values) then all
6651** must give an equivalent answer when invoked with equivalent strings.
6652** The collating function must obey the following properties for all
6653** strings A, B, and C:
6654**
6655** <ol>
6656** <li> If A==B then B==A.
6657** <li> If A==B and B==C then A==C.
6658** <li> If A&lt;B THEN B&gt;A.
6659** <li> If A&lt;B and B&lt;C then A&lt;C.
6660** </ol>
6661**
6662** If a collating function fails any of the above constraints and that
6663** collating function is registered and used, then the behavior of SQLite
6664** is undefined.
6665**
6666** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6667** with the addition that the xDestroy callback is invoked on pArg when
6668** the collating function is deleted.
6669** ^Collating functions are deleted when they are overridden by later
6670** calls to the collation creation functions or when the
6671** [database connection] is closed using [sqlite3_close()].
6672**
6673** ^The xDestroy callback is <u>not</u> called if the
6674** sqlite3_create_collation_v2() function fails. Applications that invoke
6675** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6676** check the return code and dispose of the application data pointer
6677** themselves rather than expecting SQLite to deal with it for them.
6678** This is different from every other SQLite interface. The inconsistency
6679** is unfortunate but cannot be changed without breaking backwards
6680** compatibility.
6681**
6682** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6683*/
6684SQLITE_API int sqlite3_create_collation(
6685 sqlite3*,
6686 const char *zName,
6687 int eTextRep,
6688 void *pArg,
6689 int(*xCompare)(void*,int,const void*,int,const void*)
6690);
6691SQLITE_API int sqlite3_create_collation_v2(
6692 sqlite3*,
6693 const char *zName,
6694 int eTextRep,
6695 void *pArg,
6696 int(*xCompare)(void*,int,const void*,int,const void*),
6697 void(*xDestroy)(void*)
6698);
6699SQLITE_API int sqlite3_create_collation16(
6700 sqlite3*,
6701 const void *zName,
6702 int eTextRep,
6703 void *pArg,
6704 int(*xCompare)(void*,int,const void*,int,const void*)
6705);
6706
6707/*
6708** CAPI3REF: Collation Needed Callbacks
6709** METHOD: sqlite3
6710**
6711** ^To avoid having to register all collation sequences before a database
6712** can be used, a single callback function may be registered with the
6713** [database connection] to be invoked whenever an undefined collation
6714** sequence is required.
6715**
6716** ^If the function is registered using the sqlite3_collation_needed() API,
6717** then it is passed the names of undefined collation sequences as strings
6718** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6719** the names are passed as UTF-16 in machine native byte order.
6720** ^A call to either function replaces the existing collation-needed callback.
6721**
6722** ^(When the callback is invoked, the first argument passed is a copy
6723** of the second argument to sqlite3_collation_needed() or
6724** sqlite3_collation_needed16(). The second argument is the database
6725** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6726** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6727** sequence function required. The fourth parameter is the name of the
6728** required collation sequence.)^
6729**
6730** The callback function should register the desired collation using
6731** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6732** [sqlite3_create_collation_v2()].
6733*/
6734SQLITE_API int sqlite3_collation_needed(
6735 sqlite3*,
6736 void*,
6737 void(*)(void*,sqlite3*,int eTextRep,const char*)
6738);
6739SQLITE_API int sqlite3_collation_needed16(
6740 sqlite3*,
6741 void*,
6742 void(*)(void*,sqlite3*,int eTextRep,const void*)
6743);
6744
6745#ifdef SQLITE_ENABLE_CEROD
6746/*
6747** Specify the activation key for a CEROD database. Unless
6748** activated, none of the CEROD routines will work.
6749*/
6750SQLITE_API void sqlite3_activate_cerod(
6751 const char *zPassPhrase /* Activation phrase */
6752);
6753#endif
6754
6755/*
6756** CAPI3REF: Suspend Execution For A Short Time
6757**
6758** The sqlite3_sleep() function causes the current thread to suspend execution
6759** for at least a number of milliseconds specified in its parameter.
6760**
6761** If the operating system does not support sleep requests with
6762** millisecond time resolution, then the time will be rounded up to
6763** the nearest second. The number of milliseconds of sleep actually
6764** requested from the operating system is returned.
6765**
6766** ^SQLite implements this interface by calling the xSleep()
6767** method of the default [sqlite3_vfs] object. If the xSleep() method
6768** of the default VFS is not implemented correctly, or not implemented at
6769** all, then the behavior of sqlite3_sleep() may deviate from the description
6770** in the previous paragraphs.
6771**
6772** If a negative argument is passed to sqlite3_sleep() the results vary by
6773** VFS and operating system. Some system treat a negative argument as an
6774** instruction to sleep forever. Others understand it to mean do not sleep
6775** at all. ^In SQLite version 3.42.0 and later, a negative
6776** argument passed into sqlite3_sleep() is changed to zero before it is relayed
6777** down into the xSleep method of the VFS.
6778*/
6779SQLITE_API int sqlite3_sleep(int);
6780
6781/*
6782** CAPI3REF: Name Of The Folder Holding Temporary Files
6783**
6784** ^(If this global variable is made to point to a string which is
6785** the name of a folder (a.k.a. directory), then all temporary files
6786** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6787** will be placed in that directory.)^ ^If this variable
6788** is a NULL pointer, then SQLite performs a search for an appropriate
6789** temporary file directory.
6790**
6791** Applications are strongly discouraged from using this global variable.
6792** It is required to set a temporary folder on Windows Runtime (WinRT).
6793** But for all other platforms, it is highly recommended that applications
6794** neither read nor write this variable. This global variable is a relic
6795** that exists for backwards compatibility of legacy applications and should
6796** be avoided in new projects.
6797**
6798** It is not safe to read or modify this variable in more than one
6799** thread at a time. It is not safe to read or modify this variable
6800** if a [database connection] is being used at the same time in a separate
6801** thread.
6802** It is intended that this variable be set once
6803** as part of process initialization and before any SQLite interface
6804** routines have been called and that this variable remain unchanged
6805** thereafter.
6806**
6807** ^The [temp_store_directory pragma] may modify this variable and cause
6808** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6809** the [temp_store_directory pragma] always assumes that any string
6810** that this variable points to is held in memory obtained from
6811** [sqlite3_malloc] and the pragma may attempt to free that memory
6812** using [sqlite3_free].
6813** Hence, if this variable is modified directly, either it should be
6814** made NULL or made to point to memory obtained from [sqlite3_malloc]
6815** or else the use of the [temp_store_directory pragma] should be avoided.
6816** Except when requested by the [temp_store_directory pragma], SQLite
6817** does not free the memory that sqlite3_temp_directory points to. If
6818** the application wants that memory to be freed, it must do
6819** so itself, taking care to only do so after all [database connection]
6820** objects have been destroyed.
6821**
6822** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6823** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6824** features that require the use of temporary files may fail. Here is an
6825** example of how to do this using C++ with the Windows Runtime:
6826**
6827** <blockquote><pre>
6828** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6829** &nbsp; TemporaryFolder->Path->Data();
6830** char zPathBuf&#91;MAX_PATH + 1&#93;;
6831** memset(zPathBuf, 0, sizeof(zPathBuf));
6832** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6833** &nbsp; NULL, NULL);
6834** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6835** </pre></blockquote>
6836*/
6837SQLITE_API char *sqlite3_temp_directory;
6838
6839/*
6840** CAPI3REF: Name Of The Folder Holding Database Files
6841**
6842** ^(If this global variable is made to point to a string which is
6843** the name of a folder (a.k.a. directory), then all database files
6844** specified with a relative pathname and created or accessed by
6845** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6846** to be relative to that directory.)^ ^If this variable is a NULL
6847** pointer, then SQLite assumes that all database files specified
6848** with a relative pathname are relative to the current directory
6849** for the process. Only the windows VFS makes use of this global
6850** variable; it is ignored by the unix VFS.
6851**
6852** Changing the value of this variable while a database connection is
6853** open can result in a corrupt database.
6854**
6855** It is not safe to read or modify this variable in more than one
6856** thread at a time. It is not safe to read or modify this variable
6857** if a [database connection] is being used at the same time in a separate
6858** thread.
6859** It is intended that this variable be set once
6860** as part of process initialization and before any SQLite interface
6861** routines have been called and that this variable remain unchanged
6862** thereafter.
6863**
6864** ^The [data_store_directory pragma] may modify this variable and cause
6865** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6866** the [data_store_directory pragma] always assumes that any string
6867** that this variable points to is held in memory obtained from
6868** [sqlite3_malloc] and the pragma may attempt to free that memory
6869** using [sqlite3_free].
6870** Hence, if this variable is modified directly, either it should be
6871** made NULL or made to point to memory obtained from [sqlite3_malloc]
6872** or else the use of the [data_store_directory pragma] should be avoided.
6873*/
6874SQLITE_API char *sqlite3_data_directory;
6875
6876/*
6877** CAPI3REF: Win32 Specific Interface
6878**
6879** These interfaces are available only on Windows. The
6880** [sqlite3_win32_set_directory] interface is used to set the value associated
6881** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6882** zValue, depending on the value of the type parameter. The zValue parameter
6883** should be NULL to cause the previous value to be freed via [sqlite3_free];
6884** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6885** prior to being used. The [sqlite3_win32_set_directory] interface returns
6886** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6887** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6888** [sqlite3_data_directory] variable is intended to act as a replacement for
6889** the current directory on the sub-platforms of Win32 where that concept is
6890** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6891** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6892** sqlite3_win32_set_directory interface except the string parameter must be
6893** UTF-8 or UTF-16, respectively.
6894*/
6895SQLITE_API int sqlite3_win32_set_directory(
6896 unsigned long type, /* Identifier for directory being set or reset */
6897 void *zValue /* New value for directory being set or reset */
6898);
6899SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6900SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6901
6902/*
6903** CAPI3REF: Win32 Directory Types
6904**
6905** These macros are only available on Windows. They define the allowed values
6906** for the type argument to the [sqlite3_win32_set_directory] interface.
6907*/
6908#define SQLITE_WIN32_DATA_DIRECTORY_TYPE1 1
6909#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 2
6910
6911/*
6912** CAPI3REF: Test For Auto-Commit Mode
6913** KEYWORDS: {autocommit mode}
6914** METHOD: sqlite3
6915**
6916** ^The sqlite3_get_autocommit() interface returns non-zero or
6917** zero if the given database connection is or is not in autocommit mode,
6918** respectively. ^Autocommit mode is on by default.
6919** ^Autocommit mode is disabled by a [BEGIN] statement.
6920** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6921**
6922** If certain kinds of errors occur on a statement within a multi-statement
6923** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6924** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6925** transaction might be rolled back automatically. The only way to
6926** find out whether SQLite automatically rolled back the transaction after
6927** an error is to use this function.
6928**
6929** If another thread changes the autocommit status of the database
6930** connection while this routine is running, then the return value
6931** is undefined.
6932*/
6933SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6934
6935/*
6936** CAPI3REF: Find The Database Handle Of A Prepared Statement
6937** METHOD: sqlite3_stmt
6938**
6939** ^The sqlite3_db_handle interface returns the [database connection] handle
6940** to which a [prepared statement] belongs. ^The [database connection]
6941** returned by sqlite3_db_handle is the same [database connection]
6942** that was the first argument
6943** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6944** create the statement in the first place.
6945*/
6946SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6947
6948/*
6949** CAPI3REF: Return The Schema Name For A Database Connection
6950** METHOD: sqlite3
6951**
6952** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6953** for the N-th database on database connection D, or a NULL pointer of N is
6954** out of range. An N value of 0 means the main database file. An N of 1 is
6955** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6956** databases.
6957**
6958** Space to hold the string that is returned by sqlite3_db_name() is managed
6959** by SQLite itself. The string might be deallocated by any operation that
6960** changes the schema, including [ATTACH] or [DETACH] or calls to
6961** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6962** occur on a different thread. Applications that need to
6963** remember the string long-term should make their own copy. Applications that
6964** are accessing the same database connection simultaneously on multiple
6965** threads should mutex-protect calls to this API and should make their own
6966** private copy of the result prior to releasing the mutex.
6967*/
6968SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6969
6970/*
6971** CAPI3REF: Return The Filename For A Database Connection
6972** METHOD: sqlite3
6973**
6974** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6975** associated with database N of connection D.
6976** ^If there is no attached database N on the database
6977** connection D, or if database N is a temporary or in-memory database, then
6978** this function will return either a NULL pointer or an empty string.
6979**
6980** ^The string value returned by this routine is owned and managed by
6981** the database connection. ^The value will be valid until the database N
6982** is [DETACH]-ed or until the database connection closes.
6983**
6984** ^The filename returned by this function is the output of the
6985** xFullPathname method of the [VFS]. ^In other words, the filename
6986** will be an absolute pathname, even if the filename used
6987** to open the database originally was a URI or relative pathname.
6988**
6989** If the filename pointer returned by this routine is not NULL, then it
6990** can be used as the filename input parameter to these routines:
6991** <ul>
6992** <li> [sqlite3_uri_parameter()]
6993** <li> [sqlite3_uri_boolean()]
6994** <li> [sqlite3_uri_int64()]
6995** <li> [sqlite3_filename_database()]
6996** <li> [sqlite3_filename_journal()]
6997** <li> [sqlite3_filename_wal()]
6998** </ul>
6999*/
7000SQLITE_API sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName);
7001
7002/*
7003** CAPI3REF: Determine if a database is read-only
7004** METHOD: sqlite3
7005**
7006** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
7007** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7008** the name of a database on connection D.
7009*/
7010SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
7011
7012/*
7013** CAPI3REF: Determine the transaction state of a database
7014** METHOD: sqlite3
7015**
7016** ^The sqlite3_txn_state(D,S) interface returns the current
7017** [transaction state] of schema S in database connection D. ^If S is NULL,
7018** then the highest transaction state of any schema on database connection D
7019** is returned. Transaction states are (in order of lowest to highest):
7020** <ol>
7021** <li value="0"> SQLITE_TXN_NONE
7022** <li value="1"> SQLITE_TXN_READ
7023** <li value="2"> SQLITE_TXN_WRITE
7024** </ol>
7025** ^If the S argument to sqlite3_txn_state(D,S) is not the name of
7026** a valid schema, then -1 is returned.
7027*/
7028SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
7029
7030/*
7031** CAPI3REF: Allowed return values from sqlite3_txn_state()
7032** KEYWORDS: {transaction state}
7033**
7034** These constants define the current transaction state of a database file.
7035** ^The [sqlite3_txn_state(D,S)] interface returns one of these
7036** constants in order to describe the transaction state of schema S
7037** in [database connection] D.
7038**
7039** <dl>
7040** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt>
7041** <dd>The SQLITE_TXN_NONE state means that no transaction is currently
7042** pending.</dd>
7043**
7044** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt>
7045** <dd>The SQLITE_TXN_READ state means that the database is currently
7046** in a read transaction. Content has been read from the database file
7047** but nothing in the database file has changed. The transaction state
7048** will advanced to SQLITE_TXN_WRITE if any changes occur and there are
7049** no other conflicting concurrent write transactions. The transaction
7050** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or
7051** [COMMIT].</dd>
7052**
7053** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt>
7054** <dd>The SQLITE_TXN_WRITE state means that the database is currently
7055** in a write transaction. Content has been written to the database file
7056** but has not yet committed. The transaction state will change to
7057** to SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd>
7058*/
7059#define SQLITE_TXN_NONE0 0
7060#define SQLITE_TXN_READ1 1
7061#define SQLITE_TXN_WRITE2 2
7062
7063/*
7064** CAPI3REF: Find the next prepared statement
7065** METHOD: sqlite3
7066**
7067** ^This interface returns a pointer to the next [prepared statement] after
7068** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
7069** then this interface returns a pointer to the first prepared statement
7070** associated with the database connection pDb. ^If no prepared statement
7071** satisfies the conditions of this routine, it returns NULL.
7072**
7073** The [database connection] pointer D in a call to
7074** [sqlite3_next_stmt(D,S)] must refer to an open database
7075** connection and in particular must not be a NULL pointer.
7076*/
7077SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
7078
7079/*
7080** CAPI3REF: Commit And Rollback Notification Callbacks
7081** METHOD: sqlite3
7082**
7083** ^The sqlite3_commit_hook() interface registers a callback
7084** function to be invoked whenever a transaction is [COMMIT | committed].
7085** ^Any callback set by a previous call to sqlite3_commit_hook()
7086** for the same database connection is overridden.
7087** ^The sqlite3_rollback_hook() interface registers a callback
7088** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
7089** ^Any callback set by a previous call to sqlite3_rollback_hook()
7090** for the same database connection is overridden.
7091** ^The pArg argument is passed through to the callback.
7092** ^If the callback on a commit hook function returns non-zero,
7093** then the commit is converted into a rollback.
7094**
7095** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
7096** return the P argument from the previous call of the same function
7097** on the same [database connection] D, or NULL for
7098** the first call for each function on D.
7099**
7100** The commit and rollback hook callbacks are not reentrant.
7101** The callback implementation must not do anything that will modify
7102** the database connection that invoked the callback. Any actions
7103** to modify the database connection must be deferred until after the
7104** completion of the [sqlite3_step()] call that triggered the commit
7105** or rollback hook in the first place.
7106** Note that running any other SQL statements, including SELECT statements,
7107** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
7108** the database connections for the meaning of "modify" in this paragraph.
7109**
7110** ^Registering a NULL function disables the callback.
7111**
7112** ^When the commit hook callback routine returns zero, the [COMMIT]
7113** operation is allowed to continue normally. ^If the commit hook
7114** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7115** ^The rollback hook is invoked on a rollback that results from a commit
7116** hook returning non-zero, just as it would be with any other rollback.
7117**
7118** ^For the purposes of this API, a transaction is said to have been
7119** rolled back if an explicit "ROLLBACK" statement is executed, or
7120** an error or constraint causes an implicit rollback to occur.
7121** ^The rollback callback is not invoked if a transaction is
7122** automatically rolled back because the database connection is closed.
7123**
7124** See also the [sqlite3_update_hook()] interface.
7125*/
7126SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
7127SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
7128
7129/*
7130** CAPI3REF: Autovacuum Compaction Amount Callback
7131** METHOD: sqlite3
7132**
7133** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback
7134** function C that is invoked prior to each autovacuum of the database
7135** file. ^The callback is passed a copy of the generic data pointer (P),
7136** the schema-name of the attached database that is being autovacuumed,
7137** the size of the database file in pages, the number of free pages,
7138** and the number of bytes per page, respectively. The callback should
7139** return the number of free pages that should be removed by the
7140** autovacuum. ^If the callback returns zero, then no autovacuum happens.
7141** ^If the value returned is greater than or equal to the number of
7142** free pages, then a complete autovacuum happens.
7143**
7144** <p>^If there are multiple ATTACH-ed database files that are being
7145** modified as part of a transaction commit, then the autovacuum pages
7146** callback is invoked separately for each file.
7147**
7148** <p><b>The callback is not reentrant.</b> The callback function should
7149** not attempt to invoke any other SQLite interface. If it does, bad
7150** things may happen, including segmentation faults and corrupt database
7151** files. The callback function should be a simple function that
7152** does some arithmetic on its input parameters and returns a result.
7153**
7154** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional
7155** destructor for the P parameter. ^If X is not NULL, then X(P) is
7156** invoked whenever the database connection closes or when the callback
7157** is overwritten by another invocation of sqlite3_autovacuum_pages().
7158**
7159** <p>^There is only one autovacuum pages callback per database connection.
7160** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
7161** previous invocations for that database connection. ^If the callback
7162** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
7163** then the autovacuum steps callback is canceled. The return value
7164** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
7165** be some other error code if something goes wrong. The current
7166** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
7167** return codes might be added in future releases.
7168**
7169** <p>If no autovacuum pages callback is specified (the usual case) or
7170** a NULL pointer is provided for the callback,
7171** then the default behavior is to vacuum all free pages. So, in other
7172** words, the default behavior is the same as if the callback function
7173** were something like this:
7174**
7175** <blockquote><pre>
7176** &nbsp; unsigned int demonstration_autovac_pages_callback(
7177** &nbsp; void *pClientData,
7178** &nbsp; const char *zSchema,
7179** &nbsp; unsigned int nDbPage,
7180** &nbsp; unsigned int nFreePage,
7181** &nbsp; unsigned int nBytePerPage
7182** &nbsp; ){
7183** &nbsp; return nFreePage;
7184** &nbsp; }
7185** </pre></blockquote>
7186*/
7187SQLITE_API int sqlite3_autovacuum_pages(
7188 sqlite3 *db,
7189 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
7190 void*,
7191 void(*)(void*)
7192);
7193
7194
7195/*
7196** CAPI3REF: Data Change Notification Callbacks
7197** METHOD: sqlite3
7198**
7199** ^The sqlite3_update_hook() interface registers a callback function
7200** with the [database connection] identified by the first argument
7201** to be invoked whenever a row is updated, inserted or deleted in
7202** a [rowid table].
7203** ^Any callback set by a previous call to this function
7204** for the same database connection is overridden.
7205**
7206** ^The second argument is a pointer to the function to invoke when a
7207** row is updated, inserted or deleted in a rowid table.
7208** ^The first argument to the callback is a copy of the third argument
7209** to sqlite3_update_hook().
7210** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
7211** or [SQLITE_UPDATE], depending on the operation that caused the callback
7212** to be invoked.
7213** ^The third and fourth arguments to the callback contain pointers to the
7214** database and table name containing the affected row.
7215** ^The final callback parameter is the [rowid] of the row.
7216** ^In the case of an update, this is the [rowid] after the update takes place.
7217**
7218** ^(The update hook is not invoked when internal system tables are
7219** modified (i.e. sqlite_sequence).)^
7220** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
7221**
7222** ^In the current implementation, the update hook
7223** is not invoked when conflicting rows are deleted because of an
7224** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
7225** invoked when rows are deleted using the [truncate optimization].
7226** The exceptions defined in this paragraph might change in a future
7227** release of SQLite.
7228**
7229** Whether the update hook is invoked before or after the
7230** corresponding change is currently unspecified and may differ
7231** depending on the type of change. Do not rely on the order of the
7232** hook call with regards to the final result of the operation which
7233** triggers the hook.
7234**
7235** The update hook implementation must not do anything that will modify
7236** the database connection that invoked the update hook. Any actions
7237** to modify the database connection must be deferred until after the
7238** completion of the [sqlite3_step()] call that triggered the update hook.
7239** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
7240** database connections for the meaning of "modify" in this paragraph.
7241**
7242** ^The sqlite3_update_hook(D,C,P) function
7243** returns the P argument from the previous call
7244** on the same [database connection] D, or NULL for
7245** the first call on D.
7246**
7247** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
7248** and [sqlite3_preupdate_hook()] interfaces.
7249*/
7250SQLITE_API void *sqlite3_update_hook(
7251 sqlite3*,
7252 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7253 void*
7254);
7255
7256/*
7257** CAPI3REF: Enable Or Disable Shared Pager Cache
7258**
7259** ^(This routine enables or disables the sharing of the database cache
7260** and schema data structures between [database connection | connections]
7261** to the same database. Sharing is enabled if the argument is true
7262** and disabled if the argument is false.)^
7263**
7264** This interface is omitted if SQLite is compiled with
7265** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7266** compile-time option is recommended because the
7267** [use of shared cache mode is discouraged].
7268**
7269** ^Cache sharing is enabled and disabled for an entire process.
7270** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
7271** In prior versions of SQLite,
7272** sharing was enabled or disabled for each thread separately.
7273**
7274** ^(The cache sharing mode set by this interface effects all subsequent
7275** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
7276** Existing database connections continue to use the sharing mode
7277** that was in effect at the time they were opened.)^
7278**
7279** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
7280** successfully. An [error code] is returned otherwise.)^
7281**
7282** ^Shared cache is disabled by default. It is recommended that it stay
7283** that way. In other words, do not use this routine. This interface
7284** continues to be provided for historical compatibility, but its use is
7285** discouraged. Any use of shared cache is discouraged. If shared cache
7286** must be used, it is recommended that shared cache only be enabled for
7287** individual database connections using the [sqlite3_open_v2()] interface
7288** with the [SQLITE_OPEN_SHAREDCACHE] flag.
7289**
7290** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
7291** and will always return SQLITE_MISUSE. On those systems,
7292** shared cache mode should be enabled per-database connection via
7293** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
7294**
7295** This interface is threadsafe on processors where writing a
7296** 32-bit integer is atomic.
7297**
7298** See Also: [SQLite Shared-Cache Mode]
7299*/
7300SQLITE_API int sqlite3_enable_shared_cache(int);
7301
7302/*
7303** CAPI3REF: Attempt To Free Heap Memory
7304**
7305** ^The sqlite3_release_memory() interface attempts to free N bytes
7306** of heap memory by deallocating non-essential memory allocations
7307** held by the database library. Memory used to cache database
7308** pages to improve performance is an example of non-essential memory.
7309** ^sqlite3_release_memory() returns the number of bytes actually freed,
7310** which might be more or less than the amount requested.
7311** ^The sqlite3_release_memory() routine is a no-op returning zero
7312** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7313**
7314** See also: [sqlite3_db_release_memory()]
7315*/
7316SQLITE_API int sqlite3_release_memory(int);
7317
7318/*
7319** CAPI3REF: Free Memory Used By A Database Connection
7320** METHOD: sqlite3
7321**
7322** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7323** memory as possible from database connection D. Unlike the
7324** [sqlite3_release_memory()] interface, this interface is in effect even
7325** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7326** omitted.
7327**
7328** See also: [sqlite3_release_memory()]
7329*/
7330SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7331
7332/*
7333** CAPI3REF: Impose A Limit On Heap Size
7334**
7335** These interfaces impose limits on the amount of heap memory that will be
7336** by all database connections within a single process.
7337**
7338** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7339** soft limit on the amount of heap memory that may be allocated by SQLite.
7340** ^SQLite strives to keep heap memory utilization below the soft heap
7341** limit by reducing the number of pages held in the page cache
7342** as heap memory usages approaches the limit.
7343** ^The soft heap limit is "soft" because even though SQLite strives to stay
7344** below the limit, it will exceed the limit rather than generate
7345** an [SQLITE_NOMEM] error. In other words, the soft heap limit
7346** is advisory only.
7347**
7348** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
7349** N bytes on the amount of memory that will be allocated. ^The
7350** sqlite3_hard_heap_limit64(N) interface is similar to
7351** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
7352** when the hard heap limit is reached.
7353**
7354** ^The return value from both sqlite3_soft_heap_limit64() and
7355** sqlite3_hard_heap_limit64() is the size of
7356** the heap limit prior to the call, or negative in the case of an
7357** error. ^If the argument N is negative
7358** then no change is made to the heap limit. Hence, the current
7359** size of heap limits can be determined by invoking
7360** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7361**
7362** ^Setting the heap limits to zero disables the heap limiter mechanism.
7363**
7364** ^The soft heap limit may not be greater than the hard heap limit.
7365** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
7366** is invoked with a value of N that is greater than the hard heap limit,
7367** the soft heap limit is set to the value of the hard heap limit.
7368** ^The soft heap limit is automatically enabled whenever the hard heap
7369** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
7370** the soft heap limit is outside the range of 1..N, then the soft heap
7371** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the
7372** hard heap limit is enabled makes the soft heap limit equal to the
7373** hard heap limit.
7374**
7375** The memory allocation limits can also be adjusted using
7376** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
7377**
7378** ^(The heap limits are not enforced in the current implementation
7379** if one or more of following conditions are true:
7380**
7381** <ul>
7382** <li> The limit value is set to zero.
7383** <li> Memory accounting is disabled using a combination of the
7384** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7385** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7386** <li> An alternative page cache implementation is specified using
7387** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7388** <li> The page cache allocates from its own memory pool supplied
7389** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7390** from the heap.
7391** </ul>)^
7392**
7393** The circumstances under which SQLite will enforce the heap limits may
7394** changes in future releases of SQLite.
7395*/
7396SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7397SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
7398
7399/*
7400** CAPI3REF: Deprecated Soft Heap Limit Interface
7401** DEPRECATED
7402**
7403** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7404** interface. This routine is provided for historical compatibility
7405** only. All new applications should use the
7406** [sqlite3_soft_heap_limit64()] interface rather than this one.
7407*/
7408SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7409
7410
7411/*
7412** CAPI3REF: Extract Metadata About A Column Of A Table
7413** METHOD: sqlite3
7414**
7415** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7416** information about column C of table T in database D
7417** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7418** interface returns SQLITE_OK and fills in the non-NULL pointers in
7419** the final five arguments with appropriate values if the specified
7420** column exists. ^The sqlite3_table_column_metadata() interface returns
7421** SQLITE_ERROR if the specified column does not exist.
7422** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7423** NULL pointer, then this routine simply checks for the existence of the
7424** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7425** does not. If the table name parameter T in a call to
7426** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7427** undefined behavior.
7428**
7429** ^The column is identified by the second, third and fourth parameters to
7430** this function. ^(The second parameter is either the name of the database
7431** (i.e. "main", "temp", or an attached database) containing the specified
7432** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7433** for the table using the same algorithm used by the database engine to
7434** resolve unqualified table references.
7435**
7436** ^The third and fourth parameters to this function are the table and column
7437** name of the desired column, respectively.
7438**
7439** ^Metadata is returned by writing to the memory locations passed as the 5th
7440** and subsequent parameters to this function. ^Any of these arguments may be
7441** NULL, in which case the corresponding element of metadata is omitted.
7442**
7443** ^(<blockquote>
7444** <table border="1">
7445** <tr><th> Parameter <th> Output<br>Type <th> Description
7446**
7447** <tr><td> 5th <td> const char* <td> Data type
7448** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7449** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7450** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7451** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7452** </table>
7453** </blockquote>)^
7454**
7455** ^The memory pointed to by the character pointers returned for the
7456** declaration type and collation sequence is valid until the next
7457** call to any SQLite API function.
7458**
7459** ^If the specified table is actually a view, an [error code] is returned.
7460**
7461** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7462** is not a [WITHOUT ROWID] table and an
7463** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7464** parameters are set for the explicitly declared column. ^(If there is no
7465** [INTEGER PRIMARY KEY] column, then the outputs
7466** for the [rowid] are set as follows:
7467**
7468** <pre>
7469** data type: "INTEGER"
7470** collation sequence: "BINARY"
7471** not null: 0
7472** primary key: 1
7473** auto increment: 0
7474** </pre>)^
7475**
7476** ^This function causes all database schemas to be read from disk and
7477** parsed, if that has not already been done, and returns an error if
7478** any errors are encountered while loading the schema.
7479*/
7480SQLITE_API int sqlite3_table_column_metadata(
7481 sqlite3 *db, /* Connection handle */
7482 const char *zDbName, /* Database name or NULL */
7483 const char *zTableName, /* Table name */
7484 const char *zColumnName, /* Column name */
7485 char const **pzDataType, /* OUTPUT: Declared data type */
7486 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7487 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7488 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7489 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7490);
7491
7492/*
7493** CAPI3REF: Load An Extension
7494** METHOD: sqlite3
7495**
7496** ^This interface loads an SQLite extension library from the named file.
7497**
7498** ^The sqlite3_load_extension() interface attempts to load an
7499** [SQLite extension] library contained in the file zFile. If
7500** the file cannot be loaded directly, attempts are made to load
7501** with various operating-system specific extensions added.
7502** So for example, if "samplelib" cannot be loaded, then names like
7503** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7504** be tried also.
7505**
7506** ^The entry point is zProc.
7507** ^(zProc may be 0, in which case SQLite will try to come up with an
7508** entry point name on its own. It first tries "sqlite3_extension_init".
7509** If that does not work, it constructs a name "sqlite3_X_init" where the
7510** X is consists of the lower-case equivalent of all ASCII alphabetic
7511** characters in the filename from the last "/" to the first following
7512** "." and omitting any initial "lib".)^
7513** ^The sqlite3_load_extension() interface returns
7514** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7515** ^If an error occurs and pzErrMsg is not 0, then the
7516** [sqlite3_load_extension()] interface shall attempt to
7517** fill *pzErrMsg with error message text stored in memory
7518** obtained from [sqlite3_malloc()]. The calling function
7519** should free this memory by calling [sqlite3_free()].
7520**
7521** ^Extension loading must be enabled using
7522** [sqlite3_enable_load_extension()] or
7523** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7524** prior to calling this API,
7525** otherwise an error will be returned.
7526**
7527** <b>Security warning:</b> It is recommended that the
7528** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7529** interface. The use of the [sqlite3_enable_load_extension()] interface
7530** should be avoided. This will keep the SQL function [load_extension()]
7531** disabled and prevent SQL injections from giving attackers
7532** access to extension loading capabilities.
7533**
7534** See also the [load_extension() SQL function].
7535*/
7536SQLITE_API int sqlite3_load_extension(
7537 sqlite3 *db, /* Load the extension into this database connection */
7538 const char *zFile, /* Name of the shared library containing extension */
7539 const char *zProc, /* Entry point. Derived from zFile if 0 */
7540 char **pzErrMsg /* Put error message here if not 0 */
7541);
7542
7543/*
7544** CAPI3REF: Enable Or Disable Extension Loading
7545** METHOD: sqlite3
7546**
7547** ^So as not to open security holes in older applications that are
7548** unprepared to deal with [extension loading], and as a means of disabling
7549** [extension loading] while evaluating user-entered SQL, the following API
7550** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7551**
7552** ^Extension loading is off by default.
7553** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7554** to turn extension loading on and call it with onoff==0 to turn
7555** it back off again.
7556**
7557** ^This interface enables or disables both the C-API
7558** [sqlite3_load_extension()] and the SQL function [load_extension()].
7559** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7560** to enable or disable only the C-API.)^
7561**
7562** <b>Security warning:</b> It is recommended that extension loading
7563** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7564** rather than this interface, so the [load_extension()] SQL function
7565** remains disabled. This will prevent SQL injections from giving attackers
7566** access to extension loading capabilities.
7567*/
7568SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7569
7570/*
7571** CAPI3REF: Automatically Load Statically Linked Extensions
7572**
7573** ^This interface causes the xEntryPoint() function to be invoked for
7574** each new [database connection] that is created. The idea here is that
7575** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7576** that is to be automatically loaded into all new database connections.
7577**
7578** ^(Even though the function prototype shows that xEntryPoint() takes
7579** no arguments and returns void, SQLite invokes xEntryPoint() with three
7580** arguments and expects an integer result as if the signature of the
7581** entry point where as follows:
7582**
7583** <blockquote><pre>
7584** &nbsp; int xEntryPoint(
7585** &nbsp; sqlite3 *db,
7586** &nbsp; const char **pzErrMsg,
7587** &nbsp; const struct sqlite3_api_routines *pThunk
7588** &nbsp; );
7589** </pre></blockquote>)^
7590**
7591** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7592** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7593** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7594** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7595** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7596** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7597** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7598**
7599** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7600** on the list of automatic extensions is a harmless no-op. ^No entry point
7601** will be called more than once for each database connection that is opened.
7602**
7603** See also: [sqlite3_reset_auto_extension()]
7604** and [sqlite3_cancel_auto_extension()]
7605*/
7606SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7607
7608/*
7609** CAPI3REF: Cancel Automatic Extension Loading
7610**
7611** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7612** initialization routine X that was registered using a prior call to
7613** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7614** routine returns 1 if initialization routine X was successfully
7615** unregistered and it returns 0 if X was not on the list of initialization
7616** routines.
7617*/
7618SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7619
7620/*
7621** CAPI3REF: Reset Automatic Extension Loading
7622**
7623** ^This interface disables all automatic extensions previously
7624** registered using [sqlite3_auto_extension()].
7625*/
7626SQLITE_API void sqlite3_reset_auto_extension(void);
7627
7628/*
7629** Structures used by the virtual table interface
7630*/
7631typedef struct sqlite3_vtab sqlite3_vtab;
7632typedef struct sqlite3_index_info sqlite3_index_info;
7633typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7634typedef struct sqlite3_module sqlite3_module;
7635
7636/*
7637** CAPI3REF: Virtual Table Object
7638** KEYWORDS: sqlite3_module {virtual table module}
7639**
7640** This structure, sometimes called a "virtual table module",
7641** defines the implementation of a [virtual table].
7642** This structure consists mostly of methods for the module.
7643**
7644** ^A virtual table module is created by filling in a persistent
7645** instance of this structure and passing a pointer to that instance
7646** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7647** ^The registration remains valid until it is replaced by a different
7648** module or until the [database connection] closes. The content
7649** of this structure must not change while it is registered with
7650** any database connection.
7651*/
7652struct sqlite3_module {
7653 int iVersion;
7654 int (*xCreate)(sqlite3*, void *pAux,
7655 int argc, const char *const*argv,
7656 sqlite3_vtab **ppVTab, char**);
7657 int (*xConnect)(sqlite3*, void *pAux,
7658 int argc, const char *const*argv,
7659 sqlite3_vtab **ppVTab, char**);
7660 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7661 int (*xDisconnect)(sqlite3_vtab *pVTab);
7662 int (*xDestroy)(sqlite3_vtab *pVTab);
7663 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7664 int (*xClose)(sqlite3_vtab_cursor*);
7665 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7666 int argc, sqlite3_value **argv);
7667 int (*xNext)(sqlite3_vtab_cursor*);
7668 int (*xEof)(sqlite3_vtab_cursor*);
7669 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7670 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7671 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7672 int (*xBegin)(sqlite3_vtab *pVTab);
7673 int (*xSync)(sqlite3_vtab *pVTab);
7674 int (*xCommit)(sqlite3_vtab *pVTab);
7675 int (*xRollback)(sqlite3_vtab *pVTab);
7676 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7677 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7678 void **ppArg);
7679 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7680 /* The methods above are in version 1 of the sqlite_module object. Those
7681 ** below are for version 2 and greater. */
7682 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7683 int (*xRelease)(sqlite3_vtab *pVTab, int);
7684 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7685 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7686 ** Those below are for version 3 and greater. */
7687 int (*xShadowName)(const char*);
7688 /* The methods above are in versions 1 through 3 of the sqlite_module object.
7689 ** Those below are for version 4 and greater. */
7690 int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema,
7691 const char *zTabName, int mFlags, char **pzErr);
7692};
7693
7694/*
7695** CAPI3REF: Virtual Table Indexing Information
7696** KEYWORDS: sqlite3_index_info
7697**
7698** The sqlite3_index_info structure and its substructures is used as part
7699** of the [virtual table] interface to
7700** pass information into and receive the reply from the [xBestIndex]
7701** method of a [virtual table module]. The fields under **Inputs** are the
7702** inputs to xBestIndex and are read-only. xBestIndex inserts its
7703** results into the **Outputs** fields.
7704**
7705** ^(The aConstraint[] array records WHERE clause constraints of the form:
7706**
7707** <blockquote>column OP expr</blockquote>
7708**
7709** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7710** stored in aConstraint[].op using one of the
7711** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7712** ^(The index of the column is stored in
7713** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7714** expr on the right-hand side can be evaluated (and thus the constraint
7715** is usable) and false if it cannot.)^
7716**
7717** ^The optimizer automatically inverts terms of the form "expr OP column"
7718** and makes other simplifications to the WHERE clause in an attempt to
7719** get as many WHERE clause terms into the form shown above as possible.
7720** ^The aConstraint[] array only reports WHERE clause terms that are
7721** relevant to the particular virtual table being queried.
7722**
7723** ^Information about the ORDER BY clause is stored in aOrderBy[].
7724** ^Each term of aOrderBy records a column of the ORDER BY clause.
7725**
7726** The colUsed field indicates which columns of the virtual table may be
7727** required by the current scan. Virtual table columns are numbered from
7728** zero in the order in which they appear within the CREATE TABLE statement
7729** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7730** the corresponding bit is set within the colUsed mask if the column may be
7731** required by SQLite. If the table has at least 64 columns and any column
7732** to the right of the first 63 is required, then bit 63 of colUsed is also
7733** set. In other words, column iCol may be required if the expression
7734** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7735** non-zero.
7736**
7737** The [xBestIndex] method must fill aConstraintUsage[] with information
7738** about what parameters to pass to xFilter. ^If argvIndex>0 then
7739** the right-hand side of the corresponding aConstraint[] is evaluated
7740** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7741** is true, then the constraint is assumed to be fully handled by the
7742** virtual table and might not be checked again by the byte code.)^ ^(The
7743** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
7744** is left in its default setting of false, the constraint will always be
7745** checked separately in byte code. If the omit flag is change to true, then
7746** the constraint may or may not be checked in byte code. In other words,
7747** when the omit flag is true there is no guarantee that the constraint will
7748** not be checked again using byte code.)^
7749**
7750** ^The idxNum and idxStr values are recorded and passed into the
7751** [xFilter] method.
7752** ^[sqlite3_free()] is used to free idxStr if and only if
7753** needToFreeIdxStr is true.
7754**
7755** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7756** the correct order to satisfy the ORDER BY clause so that no separate
7757** sorting step is required.
7758**
7759** ^The estimatedCost value is an estimate of the cost of a particular
7760** strategy. A cost of N indicates that the cost of the strategy is similar
7761** to a linear scan of an SQLite table with N rows. A cost of log(N)
7762** indicates that the expense of the operation is similar to that of a
7763** binary search on a unique indexed field of an SQLite table with N rows.
7764**
7765** ^The estimatedRows value is an estimate of the number of rows that
7766** will be returned by the strategy.
7767**
7768** The xBestIndex method may optionally populate the idxFlags field with a
7769** mask of SQLITE_INDEX_SCAN_* flags. One such flag is
7770** [SQLITE_INDEX_SCAN_HEX], which if set causes the [EXPLAIN QUERY PLAN]
7771** output to show the idxNum has hex instead of as decimal. Another flag is
7772** SQLITE_INDEX_SCAN_UNIQUE, which if set indicates that the query plan will
7773** return at most one row.
7774**
7775** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7776** SQLite also assumes that if a call to the xUpdate() method is made as
7777** part of the same statement to delete or update a virtual table row and the
7778** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7779** any database changes. In other words, if the xUpdate() returns
7780** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7781** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7782** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7783** the xUpdate method are automatically rolled back by SQLite.
7784**
7785** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7786** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7787** If a virtual table extension is
7788** used with an SQLite version earlier than 3.8.2, the results of attempting
7789** to read or write the estimatedRows field are undefined (but are likely
7790** to include crashing the application). The estimatedRows field should
7791** therefore only be used if [sqlite3_libversion_number()] returns a
7792** value greater than or equal to 3008002. Similarly, the idxFlags field
7793** was added for [version 3.9.0] ([dateof:3.9.0]).
7794** It may therefore only be used if
7795** sqlite3_libversion_number() returns a value greater than or equal to
7796** 3009000.
7797*/
7798struct sqlite3_index_info {
7799 /* Inputs */
7800 int nConstraint; /* Number of entries in aConstraint */
7801 struct sqlite3_index_constraint {
7802 int iColumn; /* Column constrained. -1 for ROWID */
7803 unsigned char op; /* Constraint operator */
7804 unsigned char usable; /* True if this constraint is usable */
7805 int iTermOffset; /* Used internally - xBestIndex should ignore */
7806 } *aConstraint; /* Table of WHERE clause constraints */
7807 int nOrderBy; /* Number of terms in the ORDER BY clause */
7808 struct sqlite3_index_orderby {
7809 int iColumn; /* Column number */
7810 unsigned char desc; /* True for DESC. False for ASC. */
7811 } *aOrderBy; /* The ORDER BY clause */
7812 /* Outputs */
7813 struct sqlite3_index_constraint_usage {
7814 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7815 unsigned char omit; /* Do not code a test for this constraint */
7816 } *aConstraintUsage;
7817 int idxNum; /* Number used to identify the index */
7818 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7819 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7820 int orderByConsumed; /* True if output is already ordered */
7821 double estimatedCost; /* Estimated cost of using this index */
7822 /* Fields below are only available in SQLite 3.8.2 and later */
7823 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7824 /* Fields below are only available in SQLite 3.9.0 and later */
7825 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7826 /* Fields below are only available in SQLite 3.10.0 and later */
7827 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7828};
7829
7830/*
7831** CAPI3REF: Virtual Table Scan Flags
7832**
7833** Virtual table implementations are allowed to set the
7834** [sqlite3_index_info].idxFlags field to some combination of
7835** these bits.
7836*/
7837#define SQLITE_INDEX_SCAN_UNIQUE0x00000001 0x00000001 /* Scan visits at most 1 row */
7838#define SQLITE_INDEX_SCAN_HEX0x00000002 0x00000002 /* Display idxNum as hex */
7839 /* in EXPLAIN QUERY PLAN */
7840
7841/*
7842** CAPI3REF: Virtual Table Constraint Operator Codes
7843**
7844** These macros define the allowed values for the
7845** [sqlite3_index_info].aConstraint[].op field. Each value represents
7846** an operator that is part of a constraint term in the WHERE clause of
7847** a query that uses a [virtual table].
7848**
7849** ^The left-hand operand of the operator is given by the corresponding
7850** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7851** operand is the rowid.
7852** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7853** operators have no left-hand operand, and so for those operators the
7854** corresponding aConstraint[].iColumn is meaningless and should not be
7855** used.
7856**
7857** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7858** value 255 are reserved to represent functions that are overloaded
7859** by the [xFindFunction|xFindFunction method] of the virtual table
7860** implementation.
7861**
7862** The right-hand operands for each constraint might be accessible using
7863** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7864** operand is only available if it appears as a single constant literal
7865** in the input SQL. If the right-hand operand is another column or an
7866** expression (even a constant expression) or a parameter, then the
7867** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7868** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7869** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7870** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7871** always return SQLITE_NOTFOUND.
7872**
7873** The collating sequence to be used for comparison can be found using
7874** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7875** tables, the collating sequence of constraints does not matter (for example
7876** because the constraints are numeric) and so the sqlite3_vtab_collation()
7877** interface is not commonly needed.
7878*/
7879#define SQLITE_INDEX_CONSTRAINT_EQ2 2
7880#define SQLITE_INDEX_CONSTRAINT_GT4 4
7881#define SQLITE_INDEX_CONSTRAINT_LE8 8
7882#define SQLITE_INDEX_CONSTRAINT_LT16 16
7883#define SQLITE_INDEX_CONSTRAINT_GE32 32
7884#define SQLITE_INDEX_CONSTRAINT_MATCH64 64
7885#define SQLITE_INDEX_CONSTRAINT_LIKE65 65
7886#define SQLITE_INDEX_CONSTRAINT_GLOB66 66
7887#define SQLITE_INDEX_CONSTRAINT_REGEXP67 67
7888#define SQLITE_INDEX_CONSTRAINT_NE68 68
7889#define SQLITE_INDEX_CONSTRAINT_ISNOT69 69
7890#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL70 70
7891#define SQLITE_INDEX_CONSTRAINT_ISNULL71 71
7892#define SQLITE_INDEX_CONSTRAINT_IS72 72
7893#define SQLITE_INDEX_CONSTRAINT_LIMIT73 73
7894#define SQLITE_INDEX_CONSTRAINT_OFFSET74 74
7895#define SQLITE_INDEX_CONSTRAINT_FUNCTION150 150
7896
7897/*
7898** CAPI3REF: Register A Virtual Table Implementation
7899** METHOD: sqlite3
7900**
7901** ^These routines are used to register a new [virtual table module] name.
7902** ^Module names must be registered before
7903** creating a new [virtual table] using the module and before using a
7904** preexisting [virtual table] for the module.
7905**
7906** ^The module name is registered on the [database connection] specified
7907** by the first parameter. ^The name of the module is given by the
7908** second parameter. ^The third parameter is a pointer to
7909** the implementation of the [virtual table module]. ^The fourth
7910** parameter is an arbitrary client data pointer that is passed through
7911** into the [xCreate] and [xConnect] methods of the virtual table module
7912** when a new virtual table is be being created or reinitialized.
7913**
7914** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7915** is a pointer to a destructor for the pClientData. ^SQLite will
7916** invoke the destructor function (if it is not NULL) when SQLite
7917** no longer needs the pClientData pointer. ^The destructor will also
7918** be invoked if the call to sqlite3_create_module_v2() fails.
7919** ^The sqlite3_create_module()
7920** interface is equivalent to sqlite3_create_module_v2() with a NULL
7921** destructor.
7922**
7923** ^If the third parameter (the pointer to the sqlite3_module object) is
7924** NULL then no new module is created and any existing modules with the
7925** same name are dropped.
7926**
7927** See also: [sqlite3_drop_modules()]
7928*/
7929SQLITE_API int sqlite3_create_module(
7930 sqlite3 *db, /* SQLite connection to register module with */
7931 const char *zName, /* Name of the module */
7932 const sqlite3_module *p, /* Methods for the module */
7933 void *pClientData /* Client data for xCreate/xConnect */
7934);
7935SQLITE_API int sqlite3_create_module_v2(
7936 sqlite3 *db, /* SQLite connection to register module with */
7937 const char *zName, /* Name of the module */
7938 const sqlite3_module *p, /* Methods for the module */
7939 void *pClientData, /* Client data for xCreate/xConnect */
7940 void(*xDestroy)(void*) /* Module destructor function */
7941);
7942
7943/*
7944** CAPI3REF: Remove Unnecessary Virtual Table Implementations
7945** METHOD: sqlite3
7946**
7947** ^The sqlite3_drop_modules(D,L) interface removes all virtual
7948** table modules from database connection D except those named on list L.
7949** The L parameter must be either NULL or a pointer to an array of pointers
7950** to strings where the array is terminated by a single NULL pointer.
7951** ^If the L parameter is NULL, then all virtual table modules are removed.
7952**
7953** See also: [sqlite3_create_module()]
7954*/
7955SQLITE_API int sqlite3_drop_modules(
7956 sqlite3 *db, /* Remove modules from this connection */
7957 const char **azKeep /* Except, do not remove the ones named here */
7958);
7959
7960/*
7961** CAPI3REF: Virtual Table Instance Object
7962** KEYWORDS: sqlite3_vtab
7963**
7964** Every [virtual table module] implementation uses a subclass
7965** of this object to describe a particular instance
7966** of the [virtual table]. Each subclass will
7967** be tailored to the specific needs of the module implementation.
7968** The purpose of this superclass is to define certain fields that are
7969** common to all module implementations.
7970**
7971** ^Virtual tables methods can set an error message by assigning a
7972** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7973** take care that any prior string is freed by a call to [sqlite3_free()]
7974** prior to assigning a new string to zErrMsg. ^After the error message
7975** is delivered up to the client application, the string will be automatically
7976** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7977*/
7978struct sqlite3_vtab {
7979 const sqlite3_module *pModule; /* The module for this virtual table */
7980 int nRef; /* Number of open cursors */
7981 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7982 /* Virtual table implementations will typically add additional fields */
7983};
7984
7985/*
7986** CAPI3REF: Virtual Table Cursor Object
7987** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7988**
7989** Every [virtual table module] implementation uses a subclass of the
7990** following structure to describe cursors that point into the
7991** [virtual table] and are used
7992** to loop through the virtual table. Cursors are created using the
7993** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7994** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7995** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7996** of the module. Each module implementation will define
7997** the content of a cursor structure to suit its own needs.
7998**
7999** This superclass exists in order to define fields of the cursor that
8000** are common to all implementations.
8001*/
8002struct sqlite3_vtab_cursor {
8003 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
8004 /* Virtual table implementations will typically add additional fields */
8005};
8006
8007/*
8008** CAPI3REF: Declare The Schema Of A Virtual Table
8009**
8010** ^The [xCreate] and [xConnect] methods of a
8011** [virtual table module] call this interface
8012** to declare the format (the names and datatypes of the columns) of
8013** the virtual tables they implement.
8014*/
8015SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
8016
8017/*
8018** CAPI3REF: Overload A Function For A Virtual Table
8019** METHOD: sqlite3
8020**
8021** ^(Virtual tables can provide alternative implementations of functions
8022** using the [xFindFunction] method of the [virtual table module].
8023** But global versions of those functions
8024** must exist in order to be overloaded.)^
8025**
8026** ^(This API makes sure a global version of a function with a particular
8027** name and number of parameters exists. If no such function exists
8028** before this API is called, a new function is created.)^ ^The implementation
8029** of the new function always causes an exception to be thrown. So
8030** the new function is not good for anything by itself. Its only
8031** purpose is to be a placeholder function that can be overloaded
8032** by a [virtual table].
8033*/
8034SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
8035
8036/*
8037** CAPI3REF: A Handle To An Open BLOB
8038** KEYWORDS: {BLOB handle} {BLOB handles}
8039**
8040** An instance of this object represents an open BLOB on which
8041** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
8042** ^Objects of this type are created by [sqlite3_blob_open()]
8043** and destroyed by [sqlite3_blob_close()].
8044** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
8045** can be used to read or write small subsections of the BLOB.
8046** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
8047*/
8048typedef struct sqlite3_blob sqlite3_blob;
8049
8050/*
8051** CAPI3REF: Open A BLOB For Incremental I/O
8052** METHOD: sqlite3
8053** CONSTRUCTOR: sqlite3_blob
8054**
8055** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
8056** in row iRow, column zColumn, table zTable in database zDb;
8057** in other words, the same BLOB that would be selected by:
8058**
8059** <pre>
8060** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
8061** </pre>)^
8062**
8063** ^(Parameter zDb is not the filename that contains the database, but
8064** rather the symbolic name of the database. For attached databases, this is
8065** the name that appears after the AS keyword in the [ATTACH] statement.
8066** For the main database file, the database name is "main". For TEMP
8067** tables, the database name is "temp".)^
8068**
8069** ^If the flags parameter is non-zero, then the BLOB is opened for read
8070** and write access. ^If the flags parameter is zero, the BLOB is opened for
8071** read-only access.
8072**
8073** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
8074** in *ppBlob. Otherwise an [error code] is returned and, unless the error
8075** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
8076** the API is not misused, it is always safe to call [sqlite3_blob_close()]
8077** on *ppBlob after this function it returns.
8078**
8079** This function fails with SQLITE_ERROR if any of the following are true:
8080** <ul>
8081** <li> ^(Database zDb does not exist)^,
8082** <li> ^(Table zTable does not exist within database zDb)^,
8083** <li> ^(Table zTable is a WITHOUT ROWID table)^,
8084** <li> ^(Column zColumn does not exist)^,
8085** <li> ^(Row iRow is not present in the table)^,
8086** <li> ^(The specified column of row iRow contains a value that is not
8087** a TEXT or BLOB value)^,
8088** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
8089** constraint and the blob is being opened for read/write access)^,
8090** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
8091** column zColumn is part of a [child key] definition and the blob is
8092** being opened for read/write access)^.
8093** </ul>
8094**
8095** ^Unless it returns SQLITE_MISUSE, this function sets the
8096** [database connection] error code and message accessible via
8097** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8098**
8099** A BLOB referenced by sqlite3_blob_open() may be read using the
8100** [sqlite3_blob_read()] interface and modified by using
8101** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
8102** different row of the same table using the [sqlite3_blob_reopen()]
8103** interface. However, the column, table, or database of a [BLOB handle]
8104** cannot be changed after the [BLOB handle] is opened.
8105**
8106** ^(If the row that a BLOB handle points to is modified by an
8107** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8108** then the BLOB handle is marked as "expired".
8109** This is true if any column of the row is changed, even a column
8110** other than the one the BLOB handle is open on.)^
8111** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
8112** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
8113** ^(Changes written into a BLOB prior to the BLOB expiring are not
8114** rolled back by the expiration of the BLOB. Such changes will eventually
8115** commit if the transaction continues to completion.)^
8116**
8117** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
8118** the opened blob. ^The size of a blob may not be changed by this
8119** interface. Use the [UPDATE] SQL command to change the size of a
8120** blob.
8121**
8122** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
8123** and the built-in [zeroblob] SQL function may be used to create a
8124** zero-filled blob to read or write using the incremental-blob interface.
8125**
8126** To avoid a resource leak, every open [BLOB handle] should eventually
8127** be released by a call to [sqlite3_blob_close()].
8128**
8129** See also: [sqlite3_blob_close()],
8130** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
8131** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
8132*/
8133SQLITE_API int sqlite3_blob_open(
8134 sqlite3*,
8135 const char *zDb,
8136 const char *zTable,
8137 const char *zColumn,
8138 sqlite3_int64 iRow,
8139 int flags,
8140 sqlite3_blob **ppBlob
8141);
8142
8143/*
8144** CAPI3REF: Move a BLOB Handle to a New Row
8145** METHOD: sqlite3_blob
8146**
8147** ^This function is used to move an existing [BLOB handle] so that it points
8148** to a different row of the same database table. ^The new row is identified
8149** by the rowid value passed as the second argument. Only the row can be
8150** changed. ^The database, table and column on which the blob handle is open
8151** remain the same. Moving an existing [BLOB handle] to a new row is
8152** faster than closing the existing handle and opening a new one.
8153**
8154** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8155** it must exist and there must be either a blob or text value stored in
8156** the nominated column.)^ ^If the new row is not present in the table, or if
8157** it does not contain a blob or text value, or if another error occurs, an
8158** SQLite error code is returned and the blob handle is considered aborted.
8159** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
8160** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
8161** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
8162** always returns zero.
8163**
8164** ^This function sets the database handle error code and message.
8165*/
8166SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8167
8168/*
8169** CAPI3REF: Close A BLOB Handle
8170** DESTRUCTOR: sqlite3_blob
8171**
8172** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
8173** unconditionally. Even if this routine returns an error code, the
8174** handle is still closed.)^
8175**
8176** ^If the blob handle being closed was opened for read-write access, and if
8177** the database is in auto-commit mode and there are no other open read-write
8178** blob handles or active write statements, the current transaction is
8179** committed. ^If an error occurs while committing the transaction, an error
8180** code is returned and the transaction rolled back.
8181**
8182** Calling this function with an argument that is not a NULL pointer or an
8183** open blob handle results in undefined behavior. ^Calling this routine
8184** with a null pointer (such as would be returned by a failed call to
8185** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8186** is passed a valid open blob handle, the values returned by the
8187** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
8188*/
8189SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
8190
8191/*
8192** CAPI3REF: Return The Size Of An Open BLOB
8193** METHOD: sqlite3_blob
8194**
8195** ^Returns the size in bytes of the BLOB accessible via the
8196** successfully opened [BLOB handle] in its only argument. ^The
8197** incremental blob I/O routines can only read or overwriting existing
8198** blob content; they cannot change the size of a blob.
8199**
8200** This routine only works on a [BLOB handle] which has been created
8201** by a prior successful call to [sqlite3_blob_open()] and which has not
8202** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8203** to this routine results in undefined and probably undesirable behavior.
8204*/
8205SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
8206
8207/*
8208** CAPI3REF: Read Data From A BLOB Incrementally
8209** METHOD: sqlite3_blob
8210**
8211** ^(This function is used to read data from an open [BLOB handle] into a
8212** caller-supplied buffer. N bytes of data are copied into buffer Z
8213** from the open BLOB, starting at offset iOffset.)^
8214**
8215** ^If offset iOffset is less than N bytes from the end of the BLOB,
8216** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
8217** less than zero, [SQLITE_ERROR] is returned and no data is read.
8218** ^The size of the blob (and hence the maximum value of N+iOffset)
8219** can be determined using the [sqlite3_blob_bytes()] interface.
8220**
8221** ^An attempt to read from an expired [BLOB handle] fails with an
8222** error code of [SQLITE_ABORT].
8223**
8224** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
8225** Otherwise, an [error code] or an [extended error code] is returned.)^
8226**
8227** This routine only works on a [BLOB handle] which has been created
8228** by a prior successful call to [sqlite3_blob_open()] and which has not
8229** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8230** to this routine results in undefined and probably undesirable behavior.
8231**
8232** See also: [sqlite3_blob_write()].
8233*/
8234SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
8235
8236/*
8237** CAPI3REF: Write Data Into A BLOB Incrementally
8238** METHOD: sqlite3_blob
8239**
8240** ^(This function is used to write data into an open [BLOB handle] from a
8241** caller-supplied buffer. N bytes of data are copied from the buffer Z
8242** into the open BLOB, starting at offset iOffset.)^
8243**
8244** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
8245** Otherwise, an [error code] or an [extended error code] is returned.)^
8246** ^Unless SQLITE_MISUSE is returned, this function sets the
8247** [database connection] error code and message accessible via
8248** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
8249**
8250** ^If the [BLOB handle] passed as the first argument was not opened for
8251** writing (the flags parameter to [sqlite3_blob_open()] was zero),
8252** this function returns [SQLITE_READONLY].
8253**
8254** This function may only modify the contents of the BLOB; it is
8255** not possible to increase the size of a BLOB using this API.
8256** ^If offset iOffset is less than N bytes from the end of the BLOB,
8257** [SQLITE_ERROR] is returned and no data is written. The size of the
8258** BLOB (and hence the maximum value of N+iOffset) can be determined
8259** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
8260** than zero [SQLITE_ERROR] is returned and no data is written.
8261**
8262** ^An attempt to write to an expired [BLOB handle] fails with an
8263** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
8264** before the [BLOB handle] expired are not rolled back by the
8265** expiration of the handle, though of course those changes might
8266** have been overwritten by the statement that expired the BLOB handle
8267** or by other independent statements.
8268**
8269** This routine only works on a [BLOB handle] which has been created
8270** by a prior successful call to [sqlite3_blob_open()] and which has not
8271** been closed by [sqlite3_blob_close()]. Passing any other pointer in
8272** to this routine results in undefined and probably undesirable behavior.
8273**
8274** See also: [sqlite3_blob_read()].
8275*/
8276SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8277
8278/*
8279** CAPI3REF: Virtual File System Objects
8280**
8281** A virtual filesystem (VFS) is an [sqlite3_vfs] object
8282** that SQLite uses to interact
8283** with the underlying operating system. Most SQLite builds come with a
8284** single default VFS that is appropriate for the host computer.
8285** New VFSes can be registered and existing VFSes can be unregistered.
8286** The following interfaces are provided.
8287**
8288** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
8289** ^Names are case sensitive.
8290** ^Names are zero-terminated UTF-8 strings.
8291** ^If there is no match, a NULL pointer is returned.
8292** ^If zVfsName is NULL then the default VFS is returned.
8293**
8294** ^New VFSes are registered with sqlite3_vfs_register().
8295** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
8296** ^The same VFS can be registered multiple times without injury.
8297** ^To make an existing VFS into the default VFS, register it again
8298** with the makeDflt flag set. If two different VFSes with the
8299** same name are registered, the behavior is undefined. If a
8300** VFS is registered with a name that is NULL or an empty string,
8301** then the behavior is undefined.
8302**
8303** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
8304** ^(If the default VFS is unregistered, another VFS is chosen as
8305** the default. The choice for the new VFS is arbitrary.)^
8306*/
8307SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
8308SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8309SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8310
8311/*
8312** CAPI3REF: Mutexes
8313**
8314** The SQLite core uses these routines for thread
8315** synchronization. Though they are intended for internal
8316** use by SQLite, code that links against SQLite is
8317** permitted to use any of these routines.
8318**
8319** The SQLite source code contains multiple implementations
8320** of these mutex routines. An appropriate implementation
8321** is selected automatically at compile-time. The following
8322** implementations are available in the SQLite core:
8323**
8324** <ul>
8325** <li> SQLITE_MUTEX_PTHREADS
8326** <li> SQLITE_MUTEX_W32
8327** <li> SQLITE_MUTEX_NOOP
8328** </ul>
8329**
8330** The SQLITE_MUTEX_NOOP implementation is a set of routines
8331** that does no real locking and is appropriate for use in
8332** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8333** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
8334** and Windows.
8335**
8336** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
8337** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8338** implementation is included with the library. In this case the
8339** application must supply a custom mutex implementation using the
8340** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
8341** before calling sqlite3_initialize() or any other public sqlite3_
8342** function that calls sqlite3_initialize().
8343**
8344** ^The sqlite3_mutex_alloc() routine allocates a new
8345** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
8346** routine returns NULL if it is unable to allocate the requested
8347** mutex. The argument to sqlite3_mutex_alloc() must one of these
8348** integer constants:
8349**
8350** <ul>
8351** <li> SQLITE_MUTEX_FAST
8352** <li> SQLITE_MUTEX_RECURSIVE
8353** <li> SQLITE_MUTEX_STATIC_MAIN
8354** <li> SQLITE_MUTEX_STATIC_MEM
8355** <li> SQLITE_MUTEX_STATIC_OPEN
8356** <li> SQLITE_MUTEX_STATIC_PRNG
8357** <li> SQLITE_MUTEX_STATIC_LRU
8358** <li> SQLITE_MUTEX_STATIC_PMEM
8359** <li> SQLITE_MUTEX_STATIC_APP1
8360** <li> SQLITE_MUTEX_STATIC_APP2
8361** <li> SQLITE_MUTEX_STATIC_APP3
8362** <li> SQLITE_MUTEX_STATIC_VFS1
8363** <li> SQLITE_MUTEX_STATIC_VFS2
8364** <li> SQLITE_MUTEX_STATIC_VFS3
8365** </ul>
8366**
8367** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8368** cause sqlite3_mutex_alloc() to create
8369** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8370** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8371** The mutex implementation does not need to make a distinction
8372** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8373** not want to. SQLite will only request a recursive mutex in
8374** cases where it really needs one. If a faster non-recursive mutex
8375** implementation is available on the host platform, the mutex subsystem
8376** might return such a mutex in response to SQLITE_MUTEX_FAST.
8377**
8378** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8379** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8380** a pointer to a static preexisting mutex. ^Nine static mutexes are
8381** used by the current version of SQLite. Future versions of SQLite
8382** may add additional static mutexes. Static mutexes are for internal
8383** use by SQLite only. Applications that use SQLite mutexes should
8384** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8385** SQLITE_MUTEX_RECURSIVE.
8386**
8387** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8388** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8389** returns a different mutex on every call. ^For the static
8390** mutex types, the same mutex is returned on every call that has
8391** the same type number.
8392**
8393** ^The sqlite3_mutex_free() routine deallocates a previously
8394** allocated dynamic mutex. Attempting to deallocate a static
8395** mutex results in undefined behavior.
8396**
8397** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8398** to enter a mutex. ^If another thread is already within the mutex,
8399** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8400** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8401** upon successful entry. ^(Mutexes created using
8402** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8403** In such cases, the
8404** mutex must be exited an equal number of times before another thread
8405** can enter.)^ If the same thread tries to enter any mutex other
8406** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8407**
8408** ^(Some systems (for example, Windows 95) do not support the operation
8409** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8410** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8411** sqlite3_mutex_try() as an optimization, so this is acceptable
8412** behavior. The exceptions are unix builds that set the
8413** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8414** sqlite3_mutex_try() is required.)^
8415**
8416** ^The sqlite3_mutex_leave() routine exits a mutex that was
8417** previously entered by the same thread. The behavior
8418** is undefined if the mutex is not currently entered by the
8419** calling thread or is not currently allocated.
8420**
8421** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(),
8422** sqlite3_mutex_leave(), or sqlite3_mutex_free() is a NULL pointer,
8423** then any of the four routines behaves as a no-op.
8424**
8425** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8426*/
8427SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8428SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8429SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8430SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8431SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8432
8433/*
8434** CAPI3REF: Mutex Methods Object
8435**
8436** An instance of this structure defines the low-level routines
8437** used to allocate and use mutexes.
8438**
8439** Usually, the default mutex implementations provided by SQLite are
8440** sufficient, however the application has the option of substituting a custom
8441** implementation for specialized deployments or systems for which SQLite
8442** does not provide a suitable implementation. In this case, the application
8443** creates and populates an instance of this structure to pass
8444** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8445** Additionally, an instance of this structure can be used as an
8446** output variable when querying the system for the current mutex
8447** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8448**
8449** ^The xMutexInit method defined by this structure is invoked as
8450** part of system initialization by the sqlite3_initialize() function.
8451** ^The xMutexInit routine is called by SQLite exactly once for each
8452** effective call to [sqlite3_initialize()].
8453**
8454** ^The xMutexEnd method defined by this structure is invoked as
8455** part of system shutdown by the sqlite3_shutdown() function. The
8456** implementation of this method is expected to release all outstanding
8457** resources obtained by the mutex methods implementation, especially
8458** those obtained by the xMutexInit method. ^The xMutexEnd()
8459** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8460**
8461** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8462** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8463** xMutexNotheld) implement the following interfaces (respectively):
8464**
8465** <ul>
8466** <li> [sqlite3_mutex_alloc()] </li>
8467** <li> [sqlite3_mutex_free()] </li>
8468** <li> [sqlite3_mutex_enter()] </li>
8469** <li> [sqlite3_mutex_try()] </li>
8470** <li> [sqlite3_mutex_leave()] </li>
8471** <li> [sqlite3_mutex_held()] </li>
8472** <li> [sqlite3_mutex_notheld()] </li>
8473** </ul>)^
8474**
8475** The only difference is that the public sqlite3_XXX functions enumerated
8476** above silently ignore any invocations that pass a NULL pointer instead
8477** of a valid mutex handle. The implementations of the methods defined
8478** by this structure are not required to handle this case. The results
8479** of passing a NULL pointer instead of a valid mutex handle are undefined
8480** (i.e. it is acceptable to provide an implementation that segfaults if
8481** it is passed a NULL pointer).
8482**
8483** The xMutexInit() method must be threadsafe. It must be harmless to
8484** invoke xMutexInit() multiple times within the same process and without
8485** intervening calls to xMutexEnd(). Second and subsequent calls to
8486** xMutexInit() must be no-ops.
8487**
8488** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8489** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8490** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8491** memory allocation for a fast or recursive mutex.
8492**
8493** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8494** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8495** If xMutexInit fails in any way, it is expected to clean up after itself
8496** prior to returning.
8497*/
8498typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8499struct sqlite3_mutex_methods {
8500 int (*xMutexInit)(void);
8501 int (*xMutexEnd)(void);
8502 sqlite3_mutex *(*xMutexAlloc)(int);
8503 void (*xMutexFree)(sqlite3_mutex *);
8504 void (*xMutexEnter)(sqlite3_mutex *);
8505 int (*xMutexTry)(sqlite3_mutex *);
8506 void (*xMutexLeave)(sqlite3_mutex *);
8507 int (*xMutexHeld)(sqlite3_mutex *);
8508 int (*xMutexNotheld)(sqlite3_mutex *);
8509};
8510
8511/*
8512** CAPI3REF: Mutex Verification Routines
8513**
8514** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8515** are intended for use inside assert() statements. The SQLite core
8516** never uses these routines except inside an assert() and applications
8517** are advised to follow the lead of the core. The SQLite core only
8518** provides implementations for these routines when it is compiled
8519** with the SQLITE_DEBUG flag. External mutex implementations
8520** are only required to provide these routines if SQLITE_DEBUG is
8521** defined and if NDEBUG is not defined.
8522**
8523** These routines should return true if the mutex in their argument
8524** is held or not held, respectively, by the calling thread.
8525**
8526** The implementation is not required to provide versions of these
8527** routines that actually work. If the implementation does not provide working
8528** versions of these routines, it should at least provide stubs that always
8529** return true so that one does not get spurious assertion failures.
8530**
8531** If the argument to sqlite3_mutex_held() is a NULL pointer then
8532** the routine should return 1. This seems counter-intuitive since
8533** clearly the mutex cannot be held if it does not exist. But
8534** the reason the mutex does not exist is because the build is not
8535** using mutexes. And we do not want the assert() containing the
8536** call to sqlite3_mutex_held() to fail, so a non-zero return is
8537** the appropriate thing to do. The sqlite3_mutex_notheld()
8538** interface should also return 1 when given a NULL pointer.
8539*/
8540#ifndef NDEBUG1
8541SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8542SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8543#endif
8544
8545/*
8546** CAPI3REF: Mutex Types
8547**
8548** The [sqlite3_mutex_alloc()] interface takes a single argument
8549** which is one of these integer constants.
8550**
8551** The set of static mutexes may change from one SQLite release to the
8552** next. Applications that override the built-in mutex logic must be
8553** prepared to accommodate additional static mutexes.
8554*/
8555#define SQLITE_MUTEX_FAST0 0
8556#define SQLITE_MUTEX_RECURSIVE1 1
8557#define SQLITE_MUTEX_STATIC_MAIN2 2
8558#define SQLITE_MUTEX_STATIC_MEM3 3 /* sqlite3_malloc() */
8559#define SQLITE_MUTEX_STATIC_MEM24 4 /* NOT USED */
8560#define SQLITE_MUTEX_STATIC_OPEN4 4 /* sqlite3BtreeOpen() */
8561#define SQLITE_MUTEX_STATIC_PRNG5 5 /* sqlite3_randomness() */
8562#define SQLITE_MUTEX_STATIC_LRU6 6 /* lru page list */
8563#define SQLITE_MUTEX_STATIC_LRU27 7 /* NOT USED */
8564#define SQLITE_MUTEX_STATIC_PMEM7 7 /* sqlite3PageMalloc() */
8565#define SQLITE_MUTEX_STATIC_APP18 8 /* For use by application */
8566#define SQLITE_MUTEX_STATIC_APP29 9 /* For use by application */
8567#define SQLITE_MUTEX_STATIC_APP310 10 /* For use by application */
8568#define SQLITE_MUTEX_STATIC_VFS111 11 /* For use by built-in VFS */
8569#define SQLITE_MUTEX_STATIC_VFS212 12 /* For use by extension VFS */
8570#define SQLITE_MUTEX_STATIC_VFS313 13 /* For use by application VFS */
8571
8572/* Legacy compatibility: */
8573#define SQLITE_MUTEX_STATIC_MASTER2 2
8574
8575
8576/*
8577** CAPI3REF: Retrieve the mutex for a database connection
8578** METHOD: sqlite3
8579**
8580** ^This interface returns a pointer the [sqlite3_mutex] object that
8581** serializes access to the [database connection] given in the argument
8582** when the [threading mode] is Serialized.
8583** ^If the [threading mode] is Single-thread or Multi-thread then this
8584** routine returns a NULL pointer.
8585*/
8586SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8587
8588/*
8589** CAPI3REF: Low-Level Control Of Database Files
8590** METHOD: sqlite3
8591** KEYWORDS: {file control}
8592**
8593** ^The [sqlite3_file_control()] interface makes a direct call to the
8594** xFileControl method for the [sqlite3_io_methods] object associated
8595** with a particular database identified by the second argument. ^The
8596** name of the database is "main" for the main database or "temp" for the
8597** TEMP database, or the name that appears after the AS keyword for
8598** databases that are added using the [ATTACH] SQL command.
8599** ^A NULL pointer can be used in place of "main" to refer to the
8600** main database file.
8601** ^The third and fourth parameters to this routine
8602** are passed directly through to the second and third parameters of
8603** the xFileControl method. ^The return value of the xFileControl
8604** method becomes the return value of this routine.
8605**
8606** A few opcodes for [sqlite3_file_control()] are handled directly
8607** by the SQLite core and never invoke the
8608** sqlite3_io_methods.xFileControl method.
8609** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8610** a pointer to the underlying [sqlite3_file] object to be written into
8611** the space pointed to by the 4th parameter. The
8612** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8613** the [sqlite3_file] object associated with the journal file instead of
8614** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8615** a pointer to the underlying [sqlite3_vfs] object for the file.
8616** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8617** from the pager.
8618**
8619** ^If the second parameter (zDbName) does not match the name of any
8620** open database file, then SQLITE_ERROR is returned. ^This error
8621** code is not remembered and will not be recalled by [sqlite3_errcode()]
8622** or [sqlite3_errmsg()]. The underlying xFileControl method might
8623** also return SQLITE_ERROR. There is no way to distinguish between
8624** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8625** xFileControl method.
8626**
8627** See also: [file control opcodes]
8628*/
8629SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8630
8631/*
8632** CAPI3REF: Testing Interface
8633**
8634** ^The sqlite3_test_control() interface is used to read out internal
8635** state of SQLite and to inject faults into SQLite for testing
8636** purposes. ^The first parameter is an operation code that determines
8637** the number, meaning, and operation of all subsequent parameters.
8638**
8639** This interface is not for use by applications. It exists solely
8640** for verifying the correct operation of the SQLite library. Depending
8641** on how the SQLite library is compiled, this interface might not exist.
8642**
8643** The details of the operation codes, their meanings, the parameters
8644** they take, and what they do are all subject to change without notice.
8645** Unlike most of the SQLite API, this function is not guaranteed to
8646** operate consistently from one release to the next.
8647*/
8648SQLITE_API int sqlite3_test_control(int op, ...);
8649
8650/*
8651** CAPI3REF: Testing Interface Operation Codes
8652**
8653** These constants are the valid operation code parameters used
8654** as the first argument to [sqlite3_test_control()].
8655**
8656** These parameters and their meanings are subject to change
8657** without notice. These values are for testing purposes only.
8658** Applications should not use any of these parameters or the
8659** [sqlite3_test_control()] interface.
8660*/
8661#define SQLITE_TESTCTRL_FIRST5 5
8662#define SQLITE_TESTCTRL_PRNG_SAVE5 5
8663#define SQLITE_TESTCTRL_PRNG_RESTORE6 6
8664#define SQLITE_TESTCTRL_PRNG_RESET7 7 /* NOT USED */
8665#define SQLITE_TESTCTRL_FK_NO_ACTION7 7
8666#define SQLITE_TESTCTRL_BITVEC_TEST8 8
8667#define SQLITE_TESTCTRL_FAULT_INSTALL9 9
8668#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10 10
8669#define SQLITE_TESTCTRL_PENDING_BYTE11 11
8670#define SQLITE_TESTCTRL_ASSERT12 12
8671#define SQLITE_TESTCTRL_ALWAYS13 13
8672#define SQLITE_TESTCTRL_RESERVE14 14 /* NOT USED */
8673#define SQLITE_TESTCTRL_JSON_SELFCHECK14 14
8674#define SQLITE_TESTCTRL_OPTIMIZATIONS15 15
8675#define SQLITE_TESTCTRL_ISKEYWORD16 16 /* NOT USED */
8676#define SQLITE_TESTCTRL_GETOPT16 16
8677#define SQLITE_TESTCTRL_SCRATCHMALLOC17 17 /* NOT USED */
8678#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17 17
8679#define SQLITE_TESTCTRL_LOCALTIME_FAULT18 18
8680#define SQLITE_TESTCTRL_EXPLAIN_STMT19 19 /* NOT USED */
8681#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19 19
8682#define SQLITE_TESTCTRL_NEVER_CORRUPT20 20
8683#define SQLITE_TESTCTRL_VDBE_COVERAGE21 21
8684#define SQLITE_TESTCTRL_BYTEORDER22 22
8685#define SQLITE_TESTCTRL_ISINIT23 23
8686#define SQLITE_TESTCTRL_SORTER_MMAP24 24
8687#define SQLITE_TESTCTRL_IMPOSTER25 25
8688#define SQLITE_TESTCTRL_PARSER_COVERAGE26 26
8689#define SQLITE_TESTCTRL_RESULT_INTREAL27 27
8690#define SQLITE_TESTCTRL_PRNG_SEED28 28
8691#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS29 29
8692#define SQLITE_TESTCTRL_SEEK_COUNT30 30
8693#define SQLITE_TESTCTRL_TRACEFLAGS31 31
8694#define SQLITE_TESTCTRL_TUNE32 32
8695#define SQLITE_TESTCTRL_LOGEST33 33
8696#define SQLITE_TESTCTRL_USELONGDOUBLE34 34 /* NOT USED */
8697#define SQLITE_TESTCTRL_LAST34 34 /* Largest TESTCTRL */
8698
8699/*
8700** CAPI3REF: SQL Keyword Checking
8701**
8702** These routines provide access to the set of SQL language keywords
8703** recognized by SQLite. Applications can uses these routines to determine
8704** whether or not a specific identifier needs to be escaped (for example,
8705** by enclosing in double-quotes) so as not to confuse the parser.
8706**
8707** The sqlite3_keyword_count() interface returns the number of distinct
8708** keywords understood by SQLite.
8709**
8710** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8711** makes *Z point to that keyword expressed as UTF8 and writes the number
8712** of bytes in the keyword into *L. The string that *Z points to is not
8713** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8714** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8715** or L are NULL or invalid pointers then calls to
8716** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8717**
8718** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8719** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8720** if it is and zero if not.
8721**
8722** The parser used by SQLite is forgiving. It is often possible to use
8723** a keyword as an identifier as long as such use does not result in a
8724** parsing ambiguity. For example, the statement
8725** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8726** creates a new table named "BEGIN" with three columns named
8727** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8728** using keywords as identifiers. Common techniques used to avoid keyword
8729** name collisions include:
8730** <ul>
8731** <li> Put all identifier names inside double-quotes. This is the official
8732** SQL way to escape identifier names.
8733** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8734** but it is what SQL Server does and so lots of programmers use this
8735** technique.
8736** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8737** with "Z".
8738** <li> Include a digit somewhere in every identifier name.
8739** </ul>
8740**
8741** Note that the number of keywords understood by SQLite can depend on
8742** compile-time options. For example, "VACUUM" is not a keyword if
8743** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8744** new keywords may be added to future releases of SQLite.
8745*/
8746SQLITE_API int sqlite3_keyword_count(void);
8747SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8748SQLITE_API int sqlite3_keyword_check(const char*,int);
8749
8750/*
8751** CAPI3REF: Dynamic String Object
8752** KEYWORDS: {dynamic string}
8753**
8754** An instance of the sqlite3_str object contains a dynamically-sized
8755** string under construction.
8756**
8757** The lifecycle of an sqlite3_str object is as follows:
8758** <ol>
8759** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8760** <li> ^Text is appended to the sqlite3_str object using various
8761** methods, such as [sqlite3_str_appendf()].
8762** <li> ^The sqlite3_str object is destroyed and the string it created
8763** is returned using the [sqlite3_str_finish()] interface.
8764** </ol>
8765*/
8766typedef struct sqlite3_str sqlite3_str;
8767
8768/*
8769** CAPI3REF: Create A New Dynamic String Object
8770** CONSTRUCTOR: sqlite3_str
8771**
8772** ^The [sqlite3_str_new(D)] interface allocates and initializes
8773** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8774** [sqlite3_str_new()] must be freed by a subsequent call to
8775** [sqlite3_str_finish(X)].
8776**
8777** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8778** valid [sqlite3_str] object, though in the event of an out-of-memory
8779** error the returned object might be a special singleton that will
8780** silently reject new text, always return SQLITE_NOMEM from
8781** [sqlite3_str_errcode()], always return 0 for
8782** [sqlite3_str_length()], and always return NULL from
8783** [sqlite3_str_finish(X)]. It is always safe to use the value
8784** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8785** to any of the other [sqlite3_str] methods.
8786**
8787** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8788** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8789** length of the string contained in the [sqlite3_str] object will be
8790** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8791** of [SQLITE_MAX_LENGTH].
8792*/
8793SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8794
8795/*
8796** CAPI3REF: Finalize A Dynamic String
8797** DESTRUCTOR: sqlite3_str
8798**
8799** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8800** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8801** that contains the constructed string. The calling application should
8802** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8803** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8804** errors were encountered during construction of the string. ^The
8805** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8806** string in [sqlite3_str] object X is zero bytes long.
8807*/
8808SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8809
8810/*
8811** CAPI3REF: Add Content To A Dynamic String
8812** METHOD: sqlite3_str
8813**
8814** These interfaces add content to an sqlite3_str object previously obtained
8815** from [sqlite3_str_new()].
8816**
8817** ^The [sqlite3_str_appendf(X,F,...)] and
8818** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8819** functionality of SQLite to append formatted text onto the end of
8820** [sqlite3_str] object X.
8821**
8822** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8823** onto the end of the [sqlite3_str] object X. N must be non-negative.
8824** S must contain at least N non-zero bytes of content. To append a
8825** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8826** method instead.
8827**
8828** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8829** zero-terminated string S onto the end of [sqlite3_str] object X.
8830**
8831** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8832** single-byte character C onto the end of [sqlite3_str] object X.
8833** ^This method can be used, for example, to add whitespace indentation.
8834**
8835** ^The [sqlite3_str_reset(X)] method resets the string under construction
8836** inside [sqlite3_str] object X back to zero bytes in length.
8837**
8838** These methods do not return a result code. ^If an error occurs, that fact
8839** is recorded in the [sqlite3_str] object and can be recovered by a
8840** subsequent call to [sqlite3_str_errcode(X)].
8841*/
8842SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8843SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8844SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8845SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8846SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8847SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8848
8849/*
8850** CAPI3REF: Status Of A Dynamic String
8851** METHOD: sqlite3_str
8852**
8853** These interfaces return the current status of an [sqlite3_str] object.
8854**
8855** ^If any prior errors have occurred while constructing the dynamic string
8856** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8857** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8858** [SQLITE_NOMEM] following any out-of-memory error, or
8859** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8860** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8861**
8862** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8863** of the dynamic string under construction in [sqlite3_str] object X.
8864** ^The length returned by [sqlite3_str_length(X)] does not include the
8865** zero-termination byte.
8866**
8867** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8868** content of the dynamic string under construction in X. The value
8869** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8870** and might be freed or altered by any subsequent method on the same
8871** [sqlite3_str] object. Applications must not used the pointer returned
8872** [sqlite3_str_value(X)] after any subsequent method call on the same
8873** object. ^Applications may change the content of the string returned
8874** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8875** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8876** write any byte after any subsequent sqlite3_str method call.
8877*/
8878SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8879SQLITE_API int sqlite3_str_length(sqlite3_str*);
8880SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8881
8882/*
8883** CAPI3REF: SQLite Runtime Status
8884**
8885** ^These interfaces are used to retrieve runtime status information
8886** about the performance of SQLite, and optionally to reset various
8887** highwater marks. ^The first argument is an integer code for
8888** the specific parameter to measure. ^(Recognized integer codes
8889** are of the form [status parameters | SQLITE_STATUS_...].)^
8890** ^The current value of the parameter is returned into *pCurrent.
8891** ^The highest recorded value is returned in *pHighwater. ^If the
8892** resetFlag is true, then the highest record value is reset after
8893** *pHighwater is written. ^(Some parameters do not record the highest
8894** value. For those parameters
8895** nothing is written into *pHighwater and the resetFlag is ignored.)^
8896** ^(Other parameters record only the highwater mark and not the current
8897** value. For these latter parameters nothing is written into *pCurrent.)^
8898**
8899** ^The sqlite3_status() and sqlite3_status64() routines return
8900** SQLITE_OK on success and a non-zero [error code] on failure.
8901**
8902** If either the current value or the highwater mark is too large to
8903** be represented by a 32-bit integer, then the values returned by
8904** sqlite3_status() are undefined.
8905**
8906** See also: [sqlite3_db_status()]
8907*/
8908SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8909SQLITE_API int sqlite3_status64(
8910 int op,
8911 sqlite3_int64 *pCurrent,
8912 sqlite3_int64 *pHighwater,
8913 int resetFlag
8914);
8915
8916
8917/*
8918** CAPI3REF: Status Parameters
8919** KEYWORDS: {status parameters}
8920**
8921** These integer constants designate various run-time status parameters
8922** that can be returned by [sqlite3_status()].
8923**
8924** <dl>
8925** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8926** <dd>This parameter is the current amount of memory checked out
8927** using [sqlite3_malloc()], either directly or indirectly. The
8928** figure includes calls made to [sqlite3_malloc()] by the application
8929** and internal memory usage by the SQLite library. Auxiliary page-cache
8930** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8931** this parameter. The amount returned is the sum of the allocation
8932** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8933**
8934** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8935** <dd>This parameter records the largest memory allocation request
8936** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8937** internal equivalents). Only the value returned in the
8938** *pHighwater parameter to [sqlite3_status()] is of interest.
8939** The value written into the *pCurrent parameter is undefined.</dd>)^
8940**
8941** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8942** <dd>This parameter records the number of separate memory allocations
8943** currently checked out.</dd>)^
8944**
8945** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8946** <dd>This parameter returns the number of pages used out of the
8947** [pagecache memory allocator] that was configured using
8948** [SQLITE_CONFIG_PAGECACHE]. The
8949** value returned is in pages, not in bytes.</dd>)^
8950**
8951** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8952** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8953** <dd>This parameter returns the number of bytes of page cache
8954** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8955** buffer and where forced to overflow to [sqlite3_malloc()]. The
8956** returned value includes allocations that overflowed because they
8957** where too large (they were larger than the "sz" parameter to
8958** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8959** no space was left in the page cache.</dd>)^
8960**
8961** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8962** <dd>This parameter records the largest memory allocation request
8963** handed to the [pagecache memory allocator]. Only the value returned in the
8964** *pHighwater parameter to [sqlite3_status()] is of interest.
8965** The value written into the *pCurrent parameter is undefined.</dd>)^
8966**
8967** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8968** <dd>No longer used.</dd>
8969**
8970** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8971** <dd>No longer used.</dd>
8972**
8973** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8974** <dd>No longer used.</dd>
8975**
8976** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8977** <dd>The *pHighwater parameter records the deepest parser stack.
8978** The *pCurrent value is undefined. The *pHighwater value is only
8979** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8980** </dl>
8981**
8982** New status parameters may be added from time to time.
8983*/
8984#define SQLITE_STATUS_MEMORY_USED0 0
8985#define SQLITE_STATUS_PAGECACHE_USED1 1
8986#define SQLITE_STATUS_PAGECACHE_OVERFLOW2 2
8987#define SQLITE_STATUS_SCRATCH_USED3 3 /* NOT USED */
8988#define SQLITE_STATUS_SCRATCH_OVERFLOW4 4 /* NOT USED */
8989#define SQLITE_STATUS_MALLOC_SIZE5 5
8990#define SQLITE_STATUS_PARSER_STACK6 6
8991#define SQLITE_STATUS_PAGECACHE_SIZE7 7
8992#define SQLITE_STATUS_SCRATCH_SIZE8 8 /* NOT USED */
8993#define SQLITE_STATUS_MALLOC_COUNT9 9
8994
8995/*
8996** CAPI3REF: Database Connection Status
8997** METHOD: sqlite3
8998**
8999** ^This interface is used to retrieve runtime status information
9000** about a single [database connection]. ^The first argument is the
9001** database connection object to be interrogated. ^The second argument
9002** is an integer constant, taken from the set of
9003** [SQLITE_DBSTATUS options], that
9004** determines the parameter to interrogate. The set of
9005** [SQLITE_DBSTATUS options] is likely
9006** to grow in future releases of SQLite.
9007**
9008** ^The current value of the requested parameter is written into *pCur
9009** and the highest instantaneous value is written into *pHiwtr. ^If
9010** the resetFlg is true, then the highest instantaneous value is
9011** reset back down to the current value.
9012**
9013** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
9014** non-zero [error code] on failure.
9015**
9016** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
9017*/
9018SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
9019
9020/*
9021** CAPI3REF: Status Parameters for database connections
9022** KEYWORDS: {SQLITE_DBSTATUS options}
9023**
9024** These constants are the available integer "verbs" that can be passed as
9025** the second argument to the [sqlite3_db_status()] interface.
9026**
9027** New verbs may be added in future releases of SQLite. Existing verbs
9028** might be discontinued. Applications should check the return code from
9029** [sqlite3_db_status()] to make sure that the call worked.
9030** The [sqlite3_db_status()] interface will return a non-zero error code
9031** if a discontinued or unsupported verb is invoked.
9032**
9033** <dl>
9034** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
9035** <dd>This parameter returns the number of lookaside memory slots currently
9036** checked out.</dd>)^
9037**
9038** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
9039** <dd>This parameter returns the number of malloc attempts that were
9040** satisfied using lookaside memory. Only the high-water value is meaningful;
9041** the current value is always zero.)^
9042**
9043** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
9044** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
9045** <dd>This parameter returns the number malloc attempts that might have
9046** been satisfied using lookaside memory but failed due to the amount of
9047** memory requested being larger than the lookaside slot size.
9048** Only the high-water value is meaningful;
9049** the current value is always zero.)^
9050**
9051** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
9052** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
9053** <dd>This parameter returns the number malloc attempts that might have
9054** been satisfied using lookaside memory but failed due to all lookaside
9055** memory already being in use.
9056** Only the high-water value is meaningful;
9057** the current value is always zero.)^
9058**
9059** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
9060** <dd>This parameter returns the approximate number of bytes of heap
9061** memory used by all pager caches associated with the database connection.)^
9062** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
9063**
9064** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
9065** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
9066** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
9067** pager cache is shared between two or more connections the bytes of heap
9068** memory used by that pager cache is divided evenly between the attached
9069** connections.)^ In other words, if none of the pager caches associated
9070** with the database connection are shared, this request returns the same
9071** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
9072** shared, the value returned by this call will be smaller than that returned
9073** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
9074** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
9075**
9076** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
9077** <dd>This parameter returns the approximate number of bytes of heap
9078** memory used to store the schema for all databases associated
9079** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9080** ^The full amount of memory used by the schemas is reported, even if the
9081** schema memory is shared with other database connections due to
9082** [shared cache mode] being enabled.
9083** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
9084**
9085** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
9086** <dd>This parameter returns the approximate number of bytes of heap
9087** and lookaside memory used by all prepared statements associated with
9088** the database connection.)^
9089** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
9090** </dd>
9091**
9092** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
9093** <dd>This parameter returns the number of pager cache hits that have
9094** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
9095** is always 0.
9096** </dd>
9097**
9098** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
9099** <dd>This parameter returns the number of pager cache misses that have
9100** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
9101** is always 0.
9102** </dd>
9103**
9104** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
9105** <dd>This parameter returns the number of dirty cache entries that have
9106** been written to disk. Specifically, the number of pages written to the
9107** wal file in wal mode databases, or the number of pages written to the
9108** database file in rollback mode databases. Any pages written as part of
9109** transaction rollback or database recovery operations are not included.
9110** If an IO or other error occurs while writing a page to disk, the effect
9111** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
9112** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
9113** </dd>
9114**
9115** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
9116** <dd>This parameter returns the number of dirty cache entries that have
9117** been written to disk in the middle of a transaction due to the page
9118** cache overflowing. Transactions are more efficient if they are written
9119** to disk all at once. When pages spill mid-transaction, that introduces
9120** additional overhead. This parameter can be used help identify
9121** inefficiencies that can be resolved by increasing the cache size.
9122** </dd>
9123**
9124** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
9125** <dd>This parameter returns zero for the current value if and only if
9126** all foreign key constraints (deferred or immediate) have been
9127** resolved.)^ ^The highwater mark is always 0.
9128** </dd>
9129** </dl>
9130*/
9131#define SQLITE_DBSTATUS_LOOKASIDE_USED0 0
9132#define SQLITE_DBSTATUS_CACHE_USED1 1
9133#define SQLITE_DBSTATUS_SCHEMA_USED2 2
9134#define SQLITE_DBSTATUS_STMT_USED3 3
9135#define SQLITE_DBSTATUS_LOOKASIDE_HIT4 4
9136#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5 5
9137#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6 6
9138#define SQLITE_DBSTATUS_CACHE_HIT7 7
9139#define SQLITE_DBSTATUS_CACHE_MISS8 8
9140#define SQLITE_DBSTATUS_CACHE_WRITE9 9
9141#define SQLITE_DBSTATUS_DEFERRED_FKS10 10
9142#define SQLITE_DBSTATUS_CACHE_USED_SHARED11 11
9143#define SQLITE_DBSTATUS_CACHE_SPILL12 12
9144#define SQLITE_DBSTATUS_MAX12 12 /* Largest defined DBSTATUS */
9145
9146
9147/*
9148** CAPI3REF: Prepared Statement Status
9149** METHOD: sqlite3_stmt
9150**
9151** ^(Each prepared statement maintains various
9152** [SQLITE_STMTSTATUS counters] that measure the number
9153** of times it has performed specific operations.)^ These counters can
9154** be used to monitor the performance characteristics of the prepared
9155** statements. For example, if the number of table steps greatly exceeds
9156** the number of table searches or result rows, that would tend to indicate
9157** that the prepared statement is using a full table scan rather than
9158** an index.
9159**
9160** ^(This interface is used to retrieve and reset counter values from
9161** a [prepared statement]. The first argument is the prepared statement
9162** object to be interrogated. The second argument
9163** is an integer code for a specific [SQLITE_STMTSTATUS counter]
9164** to be interrogated.)^
9165** ^The current value of the requested counter is returned.
9166** ^If the resetFlg is true, then the counter is reset to zero after this
9167** interface call returns.
9168**
9169** See also: [sqlite3_status()] and [sqlite3_db_status()].
9170*/
9171SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
9172
9173/*
9174** CAPI3REF: Status Parameters for prepared statements
9175** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
9176**
9177** These preprocessor macros define integer codes that name counter
9178** values associated with the [sqlite3_stmt_status()] interface.
9179** The meanings of the various counters are as follows:
9180**
9181** <dl>
9182** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
9183** <dd>^This is the number of times that SQLite has stepped forward in
9184** a table as part of a full table scan. Large numbers for this counter
9185** may indicate opportunities for performance improvement through
9186** careful use of indices.</dd>
9187**
9188** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
9189** <dd>^This is the number of sort operations that have occurred.
9190** A non-zero value in this counter may indicate an opportunity to
9191** improvement performance through careful use of indices.</dd>
9192**
9193** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
9194** <dd>^This is the number of rows inserted into transient indices that
9195** were created automatically in order to help joins run faster.
9196** A non-zero value in this counter may indicate an opportunity to
9197** improvement performance by adding permanent indices that do not
9198** need to be reinitialized each time the statement is run.</dd>
9199**
9200** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
9201** <dd>^This is the number of virtual machine operations executed
9202** by the prepared statement if that number is less than or equal
9203** to 2147483647. The number of virtual machine operations can be
9204** used as a proxy for the total work done by the prepared statement.
9205** If the number of virtual machine operations exceeds 2147483647
9206** then the value returned by this statement status code is undefined.
9207**
9208** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
9209** <dd>^This is the number of times that the prepare statement has been
9210** automatically regenerated due to schema changes or changes to
9211** [bound parameters] that might affect the query plan.
9212**
9213** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
9214** <dd>^This is the number of times that the prepared statement has
9215** been run. A single "run" for the purposes of this counter is one
9216** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
9217** The counter is incremented on the first [sqlite3_step()] call of each
9218** cycle.
9219**
9220** [[SQLITE_STMTSTATUS_FILTER_MISS]]
9221** [[SQLITE_STMTSTATUS_FILTER HIT]]
9222** <dt>SQLITE_STMTSTATUS_FILTER_HIT<br>
9223** SQLITE_STMTSTATUS_FILTER_MISS</dt>
9224** <dd>^SQLITE_STMTSTATUS_FILTER_HIT is the number of times that a join
9225** step was bypassed because a Bloom filter returned not-found. The
9226** corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of
9227** times that the Bloom filter returned a find, and thus the join step
9228** had to be processed as normal.
9229**
9230** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
9231** <dd>^This is the approximate number of bytes of heap memory
9232** used to store the prepared statement. ^This value is not actually
9233** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
9234** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
9235** </dd>
9236** </dl>
9237*/
9238#define SQLITE_STMTSTATUS_FULLSCAN_STEP1 1
9239#define SQLITE_STMTSTATUS_SORT2 2
9240#define SQLITE_STMTSTATUS_AUTOINDEX3 3
9241#define SQLITE_STMTSTATUS_VM_STEP4 4
9242#define SQLITE_STMTSTATUS_REPREPARE5 5
9243#define SQLITE_STMTSTATUS_RUN6 6
9244#define SQLITE_STMTSTATUS_FILTER_MISS7 7
9245#define SQLITE_STMTSTATUS_FILTER_HIT8 8
9246#define SQLITE_STMTSTATUS_MEMUSED99 99
9247
9248/*
9249** CAPI3REF: Custom Page Cache Object
9250**
9251** The sqlite3_pcache type is opaque. It is implemented by
9252** the pluggable module. The SQLite core has no knowledge of
9253** its size or internal structure and never deals with the
9254** sqlite3_pcache object except by holding and passing pointers
9255** to the object.
9256**
9257** See [sqlite3_pcache_methods2] for additional information.
9258*/
9259typedef struct sqlite3_pcache sqlite3_pcache;
9260
9261/*
9262** CAPI3REF: Custom Page Cache Object
9263**
9264** The sqlite3_pcache_page object represents a single page in the
9265** page cache. The page cache will allocate instances of this
9266** object. Various methods of the page cache use pointers to instances
9267** of this object as parameters or as their return value.
9268**
9269** See [sqlite3_pcache_methods2] for additional information.
9270*/
9271typedef struct sqlite3_pcache_page sqlite3_pcache_page;
9272struct sqlite3_pcache_page {
9273 void *pBuf; /* The content of the page */
9274 void *pExtra; /* Extra information associated with the page */
9275};
9276
9277/*
9278** CAPI3REF: Application Defined Page Cache.
9279** KEYWORDS: {page cache}
9280**
9281** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
9282** register an alternative page cache implementation by passing in an
9283** instance of the sqlite3_pcache_methods2 structure.)^
9284** In many applications, most of the heap memory allocated by
9285** SQLite is used for the page cache.
9286** By implementing a
9287** custom page cache using this API, an application can better control
9288** the amount of memory consumed by SQLite, the way in which
9289** that memory is allocated and released, and the policies used to
9290** determine exactly which parts of a database file are cached and for
9291** how long.
9292**
9293** The alternative page cache mechanism is an
9294** extreme measure that is only needed by the most demanding applications.
9295** The built-in page cache is recommended for most uses.
9296**
9297** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
9298** internal buffer by SQLite within the call to [sqlite3_config]. Hence
9299** the application may discard the parameter after the call to
9300** [sqlite3_config()] returns.)^
9301**
9302** [[the xInit() page cache method]]
9303** ^(The xInit() method is called once for each effective
9304** call to [sqlite3_initialize()])^
9305** (usually only once during the lifetime of the process). ^(The xInit()
9306** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
9307** The intent of the xInit() method is to set up global data structures
9308** required by the custom page cache implementation.
9309** ^(If the xInit() method is NULL, then the
9310** built-in default page cache is used instead of the application defined
9311** page cache.)^
9312**
9313** [[the xShutdown() page cache method]]
9314** ^The xShutdown() method is called by [sqlite3_shutdown()].
9315** It can be used to clean up
9316** any outstanding resources before process shutdown, if required.
9317** ^The xShutdown() method may be NULL.
9318**
9319** ^SQLite automatically serializes calls to the xInit method,
9320** so the xInit method need not be threadsafe. ^The
9321** xShutdown method is only called from [sqlite3_shutdown()] so it does
9322** not need to be threadsafe either. All other methods must be threadsafe
9323** in multithreaded applications.
9324**
9325** ^SQLite will never invoke xInit() more than once without an intervening
9326** call to xShutdown().
9327**
9328** [[the xCreate() page cache methods]]
9329** ^SQLite invokes the xCreate() method to construct a new cache instance.
9330** SQLite will typically create one cache instance for each open database file,
9331** though this is not guaranteed. ^The
9332** first parameter, szPage, is the size in bytes of the pages that must
9333** be allocated by the cache. ^szPage will always a power of two. ^The
9334** second parameter szExtra is a number of bytes of extra storage
9335** associated with each page cache entry. ^The szExtra parameter will
9336** a number less than 250. SQLite will use the
9337** extra szExtra bytes on each page to store metadata about the underlying
9338** database page on disk. The value passed into szExtra depends
9339** on the SQLite version, the target platform, and how SQLite was compiled.
9340** ^The third argument to xCreate(), bPurgeable, is true if the cache being
9341** created will be used to cache database pages of a file stored on disk, or
9342** false if it is used for an in-memory database. The cache implementation
9343** does not have to do anything special based with the value of bPurgeable;
9344** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
9345** never invoke xUnpin() except to deliberately delete a page.
9346** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
9347** false will always have the "discard" flag set to true.
9348** ^Hence, a cache created with bPurgeable false will
9349** never contain any unpinned pages.
9350**
9351** [[the xCachesize() page cache method]]
9352** ^(The xCachesize() method may be called at any time by SQLite to set the
9353** suggested maximum cache-size (number of pages stored by) the cache
9354** instance passed as the first argument. This is the value configured using
9355** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
9356** parameter, the implementation is not required to do anything with this
9357** value; it is advisory only.
9358**
9359** [[the xPagecount() page cache methods]]
9360** The xPagecount() method must return the number of pages currently
9361** stored in the cache, both pinned and unpinned.
9362**
9363** [[the xFetch() page cache methods]]
9364** The xFetch() method locates a page in the cache and returns a pointer to
9365** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9366** The pBuf element of the returned sqlite3_pcache_page object will be a
9367** pointer to a buffer of szPage bytes used to store the content of a
9368** single database page. The pExtra element of sqlite3_pcache_page will be
9369** a pointer to the szExtra bytes of extra storage that SQLite has requested
9370** for each entry in the page cache.
9371**
9372** The page to be fetched is determined by the key. ^The minimum key value
9373** is 1. After it has been retrieved using xFetch, the page is considered
9374** to be "pinned".
9375**
9376** If the requested page is already in the page cache, then the page cache
9377** implementation must return a pointer to the page buffer with its content
9378** intact. If the requested page is not already in the cache, then the
9379** cache implementation should use the value of the createFlag
9380** parameter to help it determined what action to take:
9381**
9382** <table border=1 width=85% align=center>
9383** <tr><th> createFlag <th> Behavior when page is not already in cache
9384** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9385** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9386** Otherwise return NULL.
9387** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9388** NULL if allocating a new page is effectively impossible.
9389** </table>
9390**
9391** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
9392** will only use a createFlag of 2 after a prior call with a createFlag of 1
9393** failed.)^ In between the xFetch() calls, SQLite may
9394** attempt to unpin one or more cache pages by spilling the content of
9395** pinned pages to disk and synching the operating system disk cache.
9396**
9397** [[the xUnpin() page cache method]]
9398** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9399** as its second argument. If the third parameter, discard, is non-zero,
9400** then the page must be evicted from the cache.
9401** ^If the discard parameter is
9402** zero, then the page may be discarded or retained at the discretion of
9403** page cache implementation. ^The page cache implementation
9404** may choose to evict unpinned pages at any time.
9405**
9406** The cache must not perform any reference counting. A single
9407** call to xUnpin() unpins the page regardless of the number of prior calls
9408** to xFetch().
9409**
9410** [[the xRekey() page cache methods]]
9411** The xRekey() method is used to change the key value associated with the
9412** page passed as the second argument. If the cache
9413** previously contains an entry associated with newKey, it must be
9414** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9415** to be pinned.
9416**
9417** When SQLite calls the xTruncate() method, the cache must discard all
9418** existing cache entries with page numbers (keys) greater than or equal
9419** to the value of the iLimit parameter passed to xTruncate(). If any
9420** of these pages are pinned, they are implicitly unpinned, meaning that
9421** they can be safely discarded.
9422**
9423** [[the xDestroy() page cache method]]
9424** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9425** All resources associated with the specified cache should be freed. ^After
9426** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9427** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9428** functions.
9429**
9430** [[the xShrink() page cache method]]
9431** ^SQLite invokes the xShrink() method when it wants the page cache to
9432** free up as much of heap memory as possible. The page cache implementation
9433** is not obligated to free any memory, but well-behaved implementations should
9434** do their best.
9435*/
9436typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9437struct sqlite3_pcache_methods2 {
9438 int iVersion;
9439 void *pArg;
9440 int (*xInit)(void*);
9441 void (*xShutdown)(void*);
9442 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9443 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9444 int (*xPagecount)(sqlite3_pcache*);
9445 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9446 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9447 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9448 unsigned oldKey, unsigned newKey);
9449 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9450 void (*xDestroy)(sqlite3_pcache*);
9451 void (*xShrink)(sqlite3_pcache*);
9452};
9453
9454/*
9455** This is the obsolete pcache_methods object that has now been replaced
9456** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9457** retained in the header file for backwards compatibility only.
9458*/
9459typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9460struct sqlite3_pcache_methods {
9461 void *pArg;
9462 int (*xInit)(void*);
9463 void (*xShutdown)(void*);
9464 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9465 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9466 int (*xPagecount)(sqlite3_pcache*);
9467 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9468 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9469 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9470 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9471 void (*xDestroy)(sqlite3_pcache*);
9472};
9473
9474
9475/*
9476** CAPI3REF: Online Backup Object
9477**
9478** The sqlite3_backup object records state information about an ongoing
9479** online backup operation. ^The sqlite3_backup object is created by
9480** a call to [sqlite3_backup_init()] and is destroyed by a call to
9481** [sqlite3_backup_finish()].
9482**
9483** See Also: [Using the SQLite Online Backup API]
9484*/
9485typedef struct sqlite3_backup sqlite3_backup;
9486
9487/*
9488** CAPI3REF: Online Backup API.
9489**
9490** The backup API copies the content of one database into another.
9491** It is useful either for creating backups of databases or
9492** for copying in-memory databases to or from persistent files.
9493**
9494** See Also: [Using the SQLite Online Backup API]
9495**
9496** ^SQLite holds a write transaction open on the destination database file
9497** for the duration of the backup operation.
9498** ^The source database is read-locked only while it is being read;
9499** it is not locked continuously for the entire backup operation.
9500** ^Thus, the backup may be performed on a live source database without
9501** preventing other database connections from
9502** reading or writing to the source database while the backup is underway.
9503**
9504** ^(To perform a backup operation:
9505** <ol>
9506** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9507** backup,
9508** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9509** the data between the two databases, and finally
9510** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9511** associated with the backup operation.
9512** </ol>)^
9513** There should be exactly one call to sqlite3_backup_finish() for each
9514** successful call to sqlite3_backup_init().
9515**
9516** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9517**
9518** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9519** [database connection] associated with the destination database
9520** and the database name, respectively.
9521** ^The database name is "main" for the main database, "temp" for the
9522** temporary database, or the name specified after the AS keyword in
9523** an [ATTACH] statement for an attached database.
9524** ^The S and M arguments passed to
9525** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9526** and database name of the source database, respectively.
9527** ^The source and destination [database connections] (parameters S and D)
9528** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9529** an error.
9530**
9531** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9532** there is already a read or read-write transaction open on the
9533** destination database.
9534**
9535** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9536** returned and an error code and error message are stored in the
9537** destination [database connection] D.
9538** ^The error code and message for the failed call to sqlite3_backup_init()
9539** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9540** [sqlite3_errmsg16()] functions.
9541** ^A successful call to sqlite3_backup_init() returns a pointer to an
9542** [sqlite3_backup] object.
9543** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9544** sqlite3_backup_finish() functions to perform the specified backup
9545** operation.
9546**
9547** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9548**
9549** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9550** the source and destination databases specified by [sqlite3_backup] object B.
9551** ^If N is negative, all remaining source pages are copied.
9552** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9553** are still more pages to be copied, then the function returns [SQLITE_OK].
9554** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9555** from source to destination, then it returns [SQLITE_DONE].
9556** ^If an error occurs while running sqlite3_backup_step(B,N),
9557** then an [error code] is returned. ^As well as [SQLITE_OK] and
9558** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9559** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9560** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9561**
9562** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9563** <ol>
9564** <li> the destination database was opened read-only, or
9565** <li> the destination database is using write-ahead-log journaling
9566** and the destination and source page sizes differ, or
9567** <li> the destination database is an in-memory database and the
9568** destination and source page sizes differ.
9569** </ol>)^
9570**
9571** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9572** the [sqlite3_busy_handler | busy-handler function]
9573** is invoked (if one is specified). ^If the
9574** busy-handler returns non-zero before the lock is available, then
9575** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9576** sqlite3_backup_step() can be retried later. ^If the source
9577** [database connection]
9578** is being used to write to the source database when sqlite3_backup_step()
9579** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9580** case the call to sqlite3_backup_step() can be retried later on. ^(If
9581** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9582** [SQLITE_READONLY] is returned, then
9583** there is no point in retrying the call to sqlite3_backup_step(). These
9584** errors are considered fatal.)^ The application must accept
9585** that the backup operation has failed and pass the backup operation handle
9586** to the sqlite3_backup_finish() to release associated resources.
9587**
9588** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9589** on the destination file. ^The exclusive lock is not released until either
9590** sqlite3_backup_finish() is called or the backup operation is complete
9591** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9592** sqlite3_backup_step() obtains a [shared lock] on the source database that
9593** lasts for the duration of the sqlite3_backup_step() call.
9594** ^Because the source database is not locked between calls to
9595** sqlite3_backup_step(), the source database may be modified mid-way
9596** through the backup process. ^If the source database is modified by an
9597** external process or via a database connection other than the one being
9598** used by the backup operation, then the backup will be automatically
9599** restarted by the next call to sqlite3_backup_step(). ^If the source
9600** database is modified by the using the same database connection as is used
9601** by the backup operation, then the backup database is automatically
9602** updated at the same time.
9603**
9604** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9605**
9606** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9607** application wishes to abandon the backup operation, the application
9608** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9609** ^The sqlite3_backup_finish() interfaces releases all
9610** resources associated with the [sqlite3_backup] object.
9611** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9612** active write-transaction on the destination database is rolled back.
9613** The [sqlite3_backup] object is invalid
9614** and may not be used following a call to sqlite3_backup_finish().
9615**
9616** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9617** sqlite3_backup_step() errors occurred, regardless or whether or not
9618** sqlite3_backup_step() completed.
9619** ^If an out-of-memory condition or IO error occurred during any prior
9620** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9621** sqlite3_backup_finish() returns the corresponding [error code].
9622**
9623** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9624** is not a permanent error and does not affect the return value of
9625** sqlite3_backup_finish().
9626**
9627** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9628** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9629**
9630** ^The sqlite3_backup_remaining() routine returns the number of pages still
9631** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9632** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9633** in the source database at the conclusion of the most recent
9634** sqlite3_backup_step().
9635** ^(The values returned by these functions are only updated by
9636** sqlite3_backup_step(). If the source database is modified in a way that
9637** changes the size of the source database or the number of pages remaining,
9638** those changes are not reflected in the output of sqlite3_backup_pagecount()
9639** and sqlite3_backup_remaining() until after the next
9640** sqlite3_backup_step().)^
9641**
9642** <b>Concurrent Usage of Database Handles</b>
9643**
9644** ^The source [database connection] may be used by the application for other
9645** purposes while a backup operation is underway or being initialized.
9646** ^If SQLite is compiled and configured to support threadsafe database
9647** connections, then the source database connection may be used concurrently
9648** from within other threads.
9649**
9650** However, the application must guarantee that the destination
9651** [database connection] is not passed to any other API (by any thread) after
9652** sqlite3_backup_init() is called and before the corresponding call to
9653** sqlite3_backup_finish(). SQLite does not currently check to see
9654** if the application incorrectly accesses the destination [database connection]
9655** and so no error code is reported, but the operations may malfunction
9656** nevertheless. Use of the destination database connection while a
9657** backup is in progress might also cause a mutex deadlock.
9658**
9659** If running in [shared cache mode], the application must
9660** guarantee that the shared cache used by the destination database
9661** is not accessed while the backup is running. In practice this means
9662** that the application must guarantee that the disk file being
9663** backed up to is not accessed by any connection within the process,
9664** not just the specific connection that was passed to sqlite3_backup_init().
9665**
9666** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9667** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9668** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9669** APIs are not strictly speaking threadsafe. If they are invoked at the
9670** same time as another thread is invoking sqlite3_backup_step() it is
9671** possible that they return invalid values.
9672**
9673** <b>Alternatives To Using The Backup API</b>
9674**
9675** Other techniques for safely creating a consistent backup of an SQLite
9676** database include:
9677**
9678** <ul>
9679** <li> The [VACUUM INTO] command.
9680** <li> The [sqlite3_rsync] utility program.
9681** </ul>
9682*/
9683SQLITE_API sqlite3_backup *sqlite3_backup_init(
9684 sqlite3 *pDest, /* Destination database handle */
9685 const char *zDestName, /* Destination database name */
9686 sqlite3 *pSource, /* Source database handle */
9687 const char *zSourceName /* Source database name */
9688);
9689SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9690SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9691SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9692SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9693
9694/*
9695** CAPI3REF: Unlock Notification
9696** METHOD: sqlite3
9697**
9698** ^When running in shared-cache mode, a database operation may fail with
9699** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9700** individual tables within the shared-cache cannot be obtained. See
9701** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9702** ^This API may be used to register a callback that SQLite will invoke
9703** when the connection currently holding the required lock relinquishes it.
9704** ^This API is only available if the library was compiled with the
9705** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9706**
9707** See Also: [Using the SQLite Unlock Notification Feature].
9708**
9709** ^Shared-cache locks are released when a database connection concludes
9710** its current transaction, either by committing it or rolling it back.
9711**
9712** ^When a connection (known as the blocked connection) fails to obtain a
9713** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9714** identity of the database connection (the blocking connection) that
9715** has locked the required resource is stored internally. ^After an
9716** application receives an SQLITE_LOCKED error, it may call the
9717** sqlite3_unlock_notify() method with the blocked connection handle as
9718** the first argument to register for a callback that will be invoked
9719** when the blocking connections current transaction is concluded. ^The
9720** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9721** call that concludes the blocking connection's transaction.
9722**
9723** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9724** there is a chance that the blocking connection will have already
9725** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9726** If this happens, then the specified callback is invoked immediately,
9727** from within the call to sqlite3_unlock_notify().)^
9728**
9729** ^If the blocked connection is attempting to obtain a write-lock on a
9730** shared-cache table, and more than one other connection currently holds
9731** a read-lock on the same table, then SQLite arbitrarily selects one of
9732** the other connections to use as the blocking connection.
9733**
9734** ^(There may be at most one unlock-notify callback registered by a
9735** blocked connection. If sqlite3_unlock_notify() is called when the
9736** blocked connection already has a registered unlock-notify callback,
9737** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9738** called with a NULL pointer as its second argument, then any existing
9739** unlock-notify callback is canceled. ^The blocked connections
9740** unlock-notify callback may also be canceled by closing the blocked
9741** connection using [sqlite3_close()].
9742**
9743** The unlock-notify callback is not reentrant. If an application invokes
9744** any sqlite3_xxx API functions from within an unlock-notify callback, a
9745** crash or deadlock may be the result.
9746**
9747** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9748** returns SQLITE_OK.
9749**
9750** <b>Callback Invocation Details</b>
9751**
9752** When an unlock-notify callback is registered, the application provides a
9753** single void* pointer that is passed to the callback when it is invoked.
9754** However, the signature of the callback function allows SQLite to pass
9755** it an array of void* context pointers. The first argument passed to
9756** an unlock-notify callback is a pointer to an array of void* pointers,
9757** and the second is the number of entries in the array.
9758**
9759** When a blocking connection's transaction is concluded, there may be
9760** more than one blocked connection that has registered for an unlock-notify
9761** callback. ^If two or more such blocked connections have specified the
9762** same callback function, then instead of invoking the callback function
9763** multiple times, it is invoked once with the set of void* context pointers
9764** specified by the blocked connections bundled together into an array.
9765** This gives the application an opportunity to prioritize any actions
9766** related to the set of unblocked database connections.
9767**
9768** <b>Deadlock Detection</b>
9769**
9770** Assuming that after registering for an unlock-notify callback a
9771** database waits for the callback to be issued before taking any further
9772** action (a reasonable assumption), then using this API may cause the
9773** application to deadlock. For example, if connection X is waiting for
9774** connection Y's transaction to be concluded, and similarly connection
9775** Y is waiting on connection X's transaction, then neither connection
9776** will proceed and the system may remain deadlocked indefinitely.
9777**
9778** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9779** detection. ^If a given call to sqlite3_unlock_notify() would put the
9780** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9781** unlock-notify callback is registered. The system is said to be in
9782** a deadlocked state if connection A has registered for an unlock-notify
9783** callback on the conclusion of connection B's transaction, and connection
9784** B has itself registered for an unlock-notify callback when connection
9785** A's transaction is concluded. ^Indirect deadlock is also detected, so
9786** the system is also considered to be deadlocked if connection B has
9787** registered for an unlock-notify callback on the conclusion of connection
9788** C's transaction, where connection C is waiting on connection A. ^Any
9789** number of levels of indirection are allowed.
9790**
9791** <b>The "DROP TABLE" Exception</b>
9792**
9793** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9794** always appropriate to call sqlite3_unlock_notify(). There is however,
9795** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9796** SQLite checks if there are any currently executing SELECT statements
9797** that belong to the same connection. If there are, SQLITE_LOCKED is
9798** returned. In this case there is no "blocking connection", so invoking
9799** sqlite3_unlock_notify() results in the unlock-notify callback being
9800** invoked immediately. If the application then re-attempts the "DROP TABLE"
9801** or "DROP INDEX" query, an infinite loop might be the result.
9802**
9803** One way around this problem is to check the extended error code returned
9804** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9805** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9806** the special "DROP TABLE/INDEX" case, the extended error code is just
9807** SQLITE_LOCKED.)^
9808*/
9809SQLITE_API int sqlite3_unlock_notify(
9810 sqlite3 *pBlocked, /* Waiting connection */
9811 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9812 void *pNotifyArg /* Argument to pass to xNotify */
9813);
9814
9815
9816/*
9817** CAPI3REF: String Comparison
9818**
9819** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9820** and extensions to compare the contents of two buffers containing UTF-8
9821** strings in a case-independent fashion, using the same definition of "case
9822** independence" that SQLite uses internally when comparing identifiers.
9823*/
9824SQLITE_API int sqlite3_stricmp(const char *, const char *);
9825SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9826
9827/*
9828** CAPI3REF: String Globbing
9829*
9830** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9831** string X matches the [GLOB] pattern P.
9832** ^The definition of [GLOB] pattern matching used in
9833** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9834** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9835** is case sensitive.
9836**
9837** Note that this routine returns zero on a match and non-zero if the strings
9838** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9839**
9840** See also: [sqlite3_strlike()].
9841*/
9842SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9843
9844/*
9845** CAPI3REF: String LIKE Matching
9846*
9847** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9848** string X matches the [LIKE] pattern P with escape character E.
9849** ^The definition of [LIKE] pattern matching used in
9850** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9851** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9852** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9853** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9854** insensitive - equivalent upper and lower case ASCII characters match
9855** one another.
9856**
9857** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9858** only ASCII characters are case folded.
9859**
9860** Note that this routine returns zero on a match and non-zero if the strings
9861** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9862**
9863** See also: [sqlite3_strglob()].
9864*/
9865SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9866
9867/*
9868** CAPI3REF: Error Logging Interface
9869**
9870** ^The [sqlite3_log()] interface writes a message into the [error log]
9871** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9872** ^If logging is enabled, the zFormat string and subsequent arguments are
9873** used with [sqlite3_snprintf()] to generate the final output string.
9874**
9875** The sqlite3_log() interface is intended for use by extensions such as
9876** virtual tables, collating functions, and SQL functions. While there is
9877** nothing to prevent an application from calling sqlite3_log(), doing so
9878** is considered bad form.
9879**
9880** The zFormat string must not be NULL.
9881**
9882** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9883** will not use dynamically allocated memory. The log message is stored in
9884** a fixed-length buffer on the stack. If the log message is longer than
9885** a few hundred characters, it will be truncated to the length of the
9886** buffer.
9887*/
9888SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9889
9890/*
9891** CAPI3REF: Write-Ahead Log Commit Hook
9892** METHOD: sqlite3
9893**
9894** ^The [sqlite3_wal_hook()] function is used to register a callback that
9895** is invoked each time data is committed to a database in wal mode.
9896**
9897** ^(The callback is invoked by SQLite after the commit has taken place and
9898** the associated write-lock on the database released)^, so the implementation
9899** may read, write or [checkpoint] the database as required.
9900**
9901** ^The first parameter passed to the callback function when it is invoked
9902** is a copy of the third parameter passed to sqlite3_wal_hook() when
9903** registering the callback. ^The second is a copy of the database handle.
9904** ^The third parameter is the name of the database that was written to -
9905** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9906** is the number of pages currently in the write-ahead log file,
9907** including those that were just committed.
9908**
9909** The callback function should normally return [SQLITE_OK]. ^If an error
9910** code is returned, that error will propagate back up through the
9911** SQLite code base to cause the statement that provoked the callback
9912** to report an error, though the commit will have still occurred. If the
9913** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9914** that does not correspond to any valid SQLite error code, the results
9915** are undefined.
9916**
9917** A single database handle may have at most a single write-ahead log callback
9918** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9919** previously registered write-ahead log callback. ^The return value is
9920** a copy of the third parameter from the previous call, if any, or 0.
9921** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the
9922** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9923** overwrite any prior [sqlite3_wal_hook()] settings.
9924*/
9925SQLITE_API void *sqlite3_wal_hook(
9926 sqlite3*,
9927 int(*)(void *,sqlite3*,const char*,int),
9928 void*
9929);
9930
9931/*
9932** CAPI3REF: Configure an auto-checkpoint
9933** METHOD: sqlite3
9934**
9935** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9936** [sqlite3_wal_hook()] that causes any database on [database connection] D
9937** to automatically [checkpoint]
9938** after committing a transaction if there are N or
9939** more frames in the [write-ahead log] file. ^Passing zero or
9940** a negative value as the nFrame parameter disables automatic
9941** checkpoints entirely.
9942**
9943** ^The callback registered by this function replaces any existing callback
9944** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9945** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9946** configured by this function.
9947**
9948** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9949** from SQL.
9950**
9951** ^Checkpoints initiated by this mechanism are
9952** [sqlite3_wal_checkpoint_v2|PASSIVE].
9953**
9954** ^Every new [database connection] defaults to having the auto-checkpoint
9955** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9956** pages. The use of this interface
9957** is only necessary if the default setting is found to be suboptimal
9958** for a particular application.
9959*/
9960SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9961
9962/*
9963** CAPI3REF: Checkpoint a database
9964** METHOD: sqlite3
9965**
9966** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9967** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9968**
9969** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9970** [write-ahead log] for database X on [database connection] D to be
9971** transferred into the database file and for the write-ahead log to
9972** be reset. See the [checkpointing] documentation for addition
9973** information.
9974**
9975** This interface used to be the only way to cause a checkpoint to
9976** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9977** interface was added. This interface is retained for backwards
9978** compatibility and as a convenience for applications that need to manually
9979** start a callback but which do not need the full power (and corresponding
9980** complication) of [sqlite3_wal_checkpoint_v2()].
9981*/
9982SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9983
9984/*
9985** CAPI3REF: Checkpoint a database
9986** METHOD: sqlite3
9987**
9988** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9989** operation on database X of [database connection] D in mode M. Status
9990** information is written back into integers pointed to by L and C.)^
9991** ^(The M parameter must be a valid [checkpoint mode]:)^
9992**
9993** <dl>
9994** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9995** ^Checkpoint as many frames as possible without waiting for any database
9996** readers or writers to finish, then sync the database file if all frames
9997** in the log were checkpointed. ^The [busy-handler callback]
9998** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9999** ^On the other hand, passive mode might leave the checkpoint unfinished
10000** if there are concurrent readers or writers.
10001**
10002** <dt>SQLITE_CHECKPOINT_FULL<dd>
10003** ^This mode blocks (it invokes the
10004** [sqlite3_busy_handler|busy-handler callback]) until there is no
10005** database writer and all readers are reading from the most recent database
10006** snapshot. ^It then checkpoints all frames in the log file and syncs the
10007** database file. ^This mode blocks new database writers while it is pending,
10008** but new database readers are allowed to continue unimpeded.
10009**
10010** <dt>SQLITE_CHECKPOINT_RESTART<dd>
10011** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
10012** that after checkpointing the log file it blocks (calls the
10013** [busy-handler callback])
10014** until all readers are reading from the database file only. ^This ensures
10015** that the next writer will restart the log file from the beginning.
10016** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
10017** database writer attempts while it is pending, but does not impede readers.
10018**
10019** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
10020** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
10021** addition that it also truncates the log file to zero bytes just prior
10022** to a successful return.
10023** </dl>
10024**
10025** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
10026** the log file or to -1 if the checkpoint could not run because
10027** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
10028** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
10029** log file (including any that were already checkpointed before the function
10030** was called) or to -1 if the checkpoint could not run due to an error or
10031** because the database is not in WAL mode. ^Note that upon successful
10032** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
10033** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
10034**
10035** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
10036** any other process is running a checkpoint operation at the same time, the
10037** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
10038** busy-handler configured, it will not be invoked in this case.
10039**
10040** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
10041** exclusive "writer" lock on the database file. ^If the writer lock cannot be
10042** obtained immediately, and a busy-handler is configured, it is invoked and
10043** the writer lock retried until either the busy-handler returns 0 or the lock
10044** is successfully obtained. ^The busy-handler is also invoked while waiting for
10045** database readers as described above. ^If the busy-handler returns 0 before
10046** the writer lock is obtained or while waiting for database readers, the
10047** checkpoint operation proceeds from that point in the same way as
10048** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10049** without blocking any further. ^SQLITE_BUSY is returned in this case.
10050**
10051** ^If parameter zDb is NULL or points to a zero length string, then the
10052** specified operation is attempted on all WAL databases [attached] to
10053** [database connection] db. In this case the
10054** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
10055** an SQLITE_BUSY error is encountered when processing one or more of the
10056** attached WAL databases, the operation is still attempted on any remaining
10057** attached databases and SQLITE_BUSY is returned at the end. ^If any other
10058** error occurs while processing an attached database, processing is abandoned
10059** and the error code is returned to the caller immediately. ^If no error
10060** (SQLITE_BUSY or otherwise) is encountered while processing the attached
10061** databases, SQLITE_OK is returned.
10062**
10063** ^If database zDb is the name of an attached database that is not in WAL
10064** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10065** zDb is not NULL (or a zero length string) and is not the name of any
10066** attached database, SQLITE_ERROR is returned to the caller.
10067**
10068** ^Unless it returns SQLITE_MISUSE,
10069** the sqlite3_wal_checkpoint_v2() interface
10070** sets the error information that is queried by
10071** [sqlite3_errcode()] and [sqlite3_errmsg()].
10072**
10073** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
10074** from SQL.
10075*/
10076SQLITE_API int sqlite3_wal_checkpoint_v2(
10077 sqlite3 *db, /* Database handle */
10078 const char *zDb, /* Name of attached database (or NULL) */
10079 int eMode, /* SQLITE_CHECKPOINT_* value */
10080 int *pnLog, /* OUT: Size of WAL log in frames */
10081 int *pnCkpt /* OUT: Total number of frames checkpointed */
10082);
10083
10084/*
10085** CAPI3REF: Checkpoint Mode Values
10086** KEYWORDS: {checkpoint mode}
10087**
10088** These constants define all valid values for the "checkpoint mode" passed
10089** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
10090** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
10091** meaning of each of these checkpoint modes.
10092*/
10093#define SQLITE_CHECKPOINT_PASSIVE0 0 /* Do as much as possible w/o blocking */
10094#define SQLITE_CHECKPOINT_FULL1 1 /* Wait for writers, then checkpoint */
10095#define SQLITE_CHECKPOINT_RESTART2 2 /* Like FULL but wait for readers */
10096#define SQLITE_CHECKPOINT_TRUNCATE3 3 /* Like RESTART but also truncate WAL */
10097
10098/*
10099** CAPI3REF: Virtual Table Interface Configuration
10100**
10101** This function may be called by either the [xConnect] or [xCreate] method
10102** of a [virtual table] implementation to configure
10103** various facets of the virtual table interface.
10104**
10105** If this interface is invoked outside the context of an xConnect or
10106** xCreate virtual table method then the behavior is undefined.
10107**
10108** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
10109** [database connection] in which the virtual table is being created and
10110** which is passed in as the first argument to the [xConnect] or [xCreate]
10111** method that is invoking sqlite3_vtab_config(). The C parameter is one
10112** of the [virtual table configuration options]. The presence and meaning
10113** of parameters after C depend on which [virtual table configuration option]
10114** is used.
10115*/
10116SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
10117
10118/*
10119** CAPI3REF: Virtual Table Configuration Options
10120** KEYWORDS: {virtual table configuration options}
10121** KEYWORDS: {virtual table configuration option}
10122**
10123** These macros define the various options to the
10124** [sqlite3_vtab_config()] interface that [virtual table] implementations
10125** can use to customize and optimize their behavior.
10126**
10127** <dl>
10128** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
10129** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
10130** <dd>Calls of the form
10131** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
10132** where X is an integer. If X is zero, then the [virtual table] whose
10133** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
10134** support constraints. In this configuration (which is the default) if
10135** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
10136** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
10137** specified as part of the users SQL statement, regardless of the actual
10138** ON CONFLICT mode specified.
10139**
10140** If X is non-zero, then the virtual table implementation guarantees
10141** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
10142** any modifications to internal or persistent data structures have been made.
10143** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
10144** is able to roll back a statement or database transaction, and abandon
10145** or continue processing the current SQL statement as appropriate.
10146** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
10147** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
10148** had been ABORT.
10149**
10150** Virtual table implementations that are required to handle OR REPLACE
10151** must do so within the [xUpdate] method. If a call to the
10152** [sqlite3_vtab_on_conflict()] function indicates that the current ON
10153** CONFLICT policy is REPLACE, the virtual table implementation should
10154** silently replace the appropriate rows within the xUpdate callback and
10155** return SQLITE_OK. Or, if this is not possible, it may return
10156** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
10157** constraint handling.
10158** </dd>
10159**
10160** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
10161** <dd>Calls of the form
10162** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
10163** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10164** prohibits that virtual table from being used from within triggers and
10165** views.
10166** </dd>
10167**
10168** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
10169** <dd>Calls of the form
10170** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
10171** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10172** identify that virtual table as being safe to use from within triggers
10173** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
10174** virtual table can do no serious harm even if it is controlled by a
10175** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
10176** flag unless absolutely necessary.
10177** </dd>
10178**
10179** [[SQLITE_VTAB_USES_ALL_SCHEMAS]]<dt>SQLITE_VTAB_USES_ALL_SCHEMAS</dt>
10180** <dd>Calls of the form
10181** [sqlite3_vtab_config](db,SQLITE_VTAB_USES_ALL_SCHEMA) from within the
10182** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10183** instruct the query planner to begin at least a read transaction on
10184** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10185** virtual table is used.
10186** </dd>
10187** </dl>
10188*/
10189#define SQLITE_VTAB_CONSTRAINT_SUPPORT1 1
10190#define SQLITE_VTAB_INNOCUOUS2 2
10191#define SQLITE_VTAB_DIRECTONLY3 3
10192#define SQLITE_VTAB_USES_ALL_SCHEMAS4 4
10193
10194/*
10195** CAPI3REF: Determine The Virtual Table Conflict Policy
10196**
10197** This function may only be called from within a call to the [xUpdate] method
10198** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
10199** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
10200** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
10201** of the SQL statement that triggered the call to the [xUpdate] method of the
10202** [virtual table].
10203*/
10204SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
10205
10206/*
10207** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
10208**
10209** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
10210** method of a [virtual table], then it might return true if the
10211** column is being fetched as part of an UPDATE operation during which the
10212** column value will not change. The virtual table implementation can use
10213** this hint as permission to substitute a return value that is less
10214** expensive to compute and that the corresponding
10215** [xUpdate] method understands as a "no-change" value.
10216**
10217** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
10218** the column is not changed by the UPDATE statement, then the xColumn
10219** method can optionally return without setting a result, without calling
10220** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
10221** In that case, [sqlite3_value_nochange(X)] will return true for the
10222** same column in the [xUpdate] method.
10223**
10224** The sqlite3_vtab_nochange() routine is an optimization. Virtual table
10225** implementations should continue to give a correct answer even if the
10226** sqlite3_vtab_nochange() interface were to always return false. In the
10227** current implementation, the sqlite3_vtab_nochange() interface does always
10228** returns false for the enhanced [UPDATE FROM] statement.
10229*/
10230SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
10231
10232/*
10233** CAPI3REF: Determine The Collation For a Virtual Table Constraint
10234** METHOD: sqlite3_index_info
10235**
10236** This function may only be called from within a call to the [xBestIndex]
10237** method of a [virtual table]. This function returns a pointer to a string
10238** that is the name of the appropriate collation sequence to use for text
10239** comparisons on the constraint identified by its arguments.
10240**
10241** The first argument must be the pointer to the [sqlite3_index_info] object
10242** that is the first parameter to the xBestIndex() method. The second argument
10243** must be an index into the aConstraint[] array belonging to the
10244** sqlite3_index_info structure passed to xBestIndex.
10245**
10246** Important:
10247** The first parameter must be the same pointer that is passed into the
10248** xBestMethod() method. The first parameter may not be a pointer to a
10249** different [sqlite3_index_info] object, even an exact copy.
10250**
10251** The return value is computed as follows:
10252**
10253** <ol>
10254** <li><p> If the constraint comes from a WHERE clause expression that contains
10255** a [COLLATE operator], then the name of the collation specified by
10256** that COLLATE operator is returned.
10257** <li><p> If there is no COLLATE operator, but the column that is the subject
10258** of the constraint specifies an alternative collating sequence via
10259** a [COLLATE clause] on the column definition within the CREATE TABLE
10260** statement that was passed into [sqlite3_declare_vtab()], then the
10261** name of that alternative collating sequence is returned.
10262** <li><p> Otherwise, "BINARY" is returned.
10263** </ol>
10264*/
10265SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
10266
10267/*
10268** CAPI3REF: Determine if a virtual table query is DISTINCT
10269** METHOD: sqlite3_index_info
10270**
10271** This API may only be used from within an [xBestIndex|xBestIndex method]
10272** of a [virtual table] implementation. The result of calling this
10273** interface from outside of xBestIndex() is undefined and probably harmful.
10274**
10275** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and
10276** 3. The integer returned by sqlite3_vtab_distinct()
10277** gives the virtual table additional information about how the query
10278** planner wants the output to be ordered. As long as the virtual table
10279** can meet the ordering requirements of the query planner, it may set
10280** the "orderByConsumed" flag.
10281**
10282** <ol><li value="0"><p>
10283** ^If the sqlite3_vtab_distinct() interface returns 0, that means
10284** that the query planner needs the virtual table to return all rows in the
10285** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
10286** [sqlite3_index_info] object. This is the default expectation. If the
10287** virtual table outputs all rows in sorted order, then it is always safe for
10288** the xBestIndex method to set the "orderByConsumed" flag, regardless of
10289** the return value from sqlite3_vtab_distinct().
10290** <li value="1"><p>
10291** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
10292** that the query planner does not need the rows to be returned in sorted order
10293** as long as all rows with the same values in all columns identified by the
10294** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
10295** is doing a GROUP BY.
10296** <li value="2"><p>
10297** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
10298** that the query planner does not need the rows returned in any particular
10299** order, as long as rows with the same values in all columns identified
10300** by "aOrderBy" are adjacent.)^ ^(Furthermore, when two or more rows
10301** contain the same values for all columns identified by "colUsed", all but
10302** one such row may optionally be omitted from the result.)^
10303** The virtual table is not required to omit rows that are duplicates
10304** over the "colUsed" columns, but if the virtual table can do that without
10305** too much extra effort, it could potentially help the query to run faster.
10306** This mode is used for a DISTINCT query.
10307** <li value="3"><p>
10308** ^(If the sqlite3_vtab_distinct() interface returns 3, that means the
10309** virtual table must return rows in the order defined by "aOrderBy" as
10310** if the sqlite3_vtab_distinct() interface had returned 0. However if
10311** two or more rows in the result have the same values for all columns
10312** identified by "colUsed", then all but one such row may optionally be
10313** omitted.)^ Like when the return value is 2, the virtual table
10314** is not required to omit rows that are duplicates over the "colUsed"
10315** columns, but if the virtual table can do that without
10316** too much extra effort, it could potentially help the query to run faster.
10317** This mode is used for queries
10318** that have both DISTINCT and ORDER BY clauses.
10319** </ol>
10320**
10321** <p>The following table summarizes the conditions under which the
10322** virtual table is allowed to set the "orderByConsumed" flag based on
10323** the value returned by sqlite3_vtab_distinct(). This table is a
10324** restatement of the previous four paragraphs:
10325**
10326** <table border=1 cellspacing=0 cellpadding=10 width="90%">
10327** <tr>
10328** <td valign="top">sqlite3_vtab_distinct() return value
10329** <td valign="top">Rows are returned in aOrderBy order
10330** <td valign="top">Rows with the same value in all aOrderBy columns are adjacent
10331** <td valign="top">Duplicates over all colUsed columns may be omitted
10332** <tr><td>0<td>yes<td>yes<td>no
10333** <tr><td>1<td>no<td>yes<td>no
10334** <tr><td>2<td>no<td>yes<td>yes
10335** <tr><td>3<td>yes<td>yes<td>yes
10336** </table>
10337**
10338** ^For the purposes of comparing virtual table output values to see if the
10339** values are same value for sorting purposes, two NULL values are considered
10340** to be the same. In other words, the comparison operator is "IS"
10341** (or "IS NOT DISTINCT FROM") and not "==".
10342**
10343** If a virtual table implementation is unable to meet the requirements
10344** specified above, then it must not set the "orderByConsumed" flag in the
10345** [sqlite3_index_info] object or an incorrect answer may result.
10346**
10347** ^A virtual table implementation is always free to return rows in any order
10348** it wants, as long as the "orderByConsumed" flag is not set. ^When the
10349** the "orderByConsumed" flag is unset, the query planner will add extra
10350** [bytecode] to ensure that the final results returned by the SQL query are
10351** ordered correctly. The use of the "orderByConsumed" flag and the
10352** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful
10353** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed"
10354** flag might help queries against a virtual table to run faster. Being
10355** overly aggressive and setting the "orderByConsumed" flag when it is not
10356** valid to do so, on the other hand, might cause SQLite to return incorrect
10357** results.
10358*/
10359SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
10360
10361/*
10362** CAPI3REF: Identify and handle IN constraints in xBestIndex
10363**
10364** This interface may only be used from within an
10365** [xBestIndex|xBestIndex() method] of a [virtual table] implementation.
10366** The result of invoking this interface from any other context is
10367** undefined and probably harmful.
10368**
10369** ^(A constraint on a virtual table of the form
10370** "[IN operator|column IN (...)]" is
10371** communicated to the xBestIndex method as a
10372** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use
10373** this constraint, it must set the corresponding
10374** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under
10375** the usual mode of handling IN operators, SQLite generates [bytecode]
10376** that invokes the [xFilter|xFilter() method] once for each value
10377** on the right-hand side of the IN operator.)^ Thus the virtual table
10378** only sees a single value from the right-hand side of the IN operator
10379** at a time.
10380**
10381** In some cases, however, it would be advantageous for the virtual
10382** table to see all values on the right-hand of the IN operator all at
10383** once. The sqlite3_vtab_in() interfaces facilitates this in two ways:
10384**
10385** <ol>
10386** <li><p>
10387** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10388** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10389** is an [IN operator] that can be processed all at once. ^In other words,
10390** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10391** by which the virtual table can ask SQLite if all-at-once processing
10392** of the IN operator is even possible.
10393**
10394** <li><p>
10395** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates
10396** to SQLite that the virtual table does or does not want to process
10397** the IN operator all-at-once, respectively. ^Thus when the third
10398** parameter (F) is non-negative, this interface is the mechanism by
10399** which the virtual table tells SQLite how it wants to process the
10400** IN operator.
10401** </ol>
10402**
10403** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times
10404** within the same xBestIndex method call. ^For any given P,N pair,
10405** the return value from sqlite3_vtab_in(P,N,F) will always be the same
10406** within the same xBestIndex call. ^If the interface returns true
10407** (non-zero), that means that the constraint is an IN operator
10408** that can be processed all-at-once. ^If the constraint is not an IN
10409** operator or cannot be processed all-at-once, then the interface returns
10410** false.
10411**
10412** ^(All-at-once processing of the IN operator is selected if both of the
10413** following conditions are met:
10414**
10415** <ol>
10416** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10417** integer. This is how the virtual table tells SQLite that it wants to
10418** use the N-th constraint.
10419**
10420** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was
10421** non-negative had F>=1.
10422** </ol>)^
10423**
10424** ^If either or both of the conditions above are false, then SQLite uses
10425** the traditional one-at-a-time processing strategy for the IN constraint.
10426** ^If both conditions are true, then the argvIndex-th parameter to the
10427** xFilter method will be an [sqlite3_value] that appears to be NULL,
10428** but which can be passed to [sqlite3_vtab_in_first()] and
10429** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10430** of the IN constraint.
10431*/
10432SQLITE_API int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle);
10433
10434/*
10435** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10436**
10437** These interfaces are only useful from within the
10438** [xFilter|xFilter() method] of a [virtual table] implementation.
10439** The result of invoking these interfaces from any other context
10440** is undefined and probably harmful.
10441**
10442** The X parameter in a call to sqlite3_vtab_in_first(X,P) or
10443** sqlite3_vtab_in_next(X,P) should be one of the parameters to the
10444** xFilter method which invokes these routines, and specifically
10445** a parameter that was previously selected for all-at-once IN constraint
10446** processing use the [sqlite3_vtab_in()] interface in the
10447** [xBestIndex|xBestIndex method]. ^(If the X parameter is not
10448** an xFilter argument that was selected for all-at-once IN constraint
10449** processing, then these routines return [SQLITE_ERROR].)^
10450**
10451** ^(Use these routines to access all values on the right-hand side
10452** of the IN constraint using code like the following:
10453**
10454** <blockquote><pre>
10455** &nbsp; for(rc=sqlite3_vtab_in_first(pList, &pVal);
10456** &nbsp; rc==SQLITE_OK && pVal;
10457** &nbsp; rc=sqlite3_vtab_in_next(pList, &pVal)
10458** &nbsp; ){
10459** &nbsp; // do something with pVal
10460** &nbsp; }
10461** &nbsp; if( rc!=SQLITE_OK ){
10462** &nbsp; // an error has occurred
10463** &nbsp; }
10464** </pre></blockquote>)^
10465**
10466** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P)
10467** routines return SQLITE_OK and set *P to point to the first or next value
10468** on the RHS of the IN constraint. ^If there are no more values on the
10469** right hand side of the IN constraint, then *P is set to NULL and these
10470** routines return [SQLITE_DONE]. ^The return value might be
10471** some other value, such as SQLITE_NOMEM, in the event of a malfunction.
10472**
10473** The *ppOut values returned by these routines are only valid until the
10474** next call to either of these routines or until the end of the xFilter
10475** method from which these routines were called. If the virtual table
10476** implementation needs to retain the *ppOut values for longer, it must make
10477** copies. The *ppOut values are [protected sqlite3_value|protected].
10478*/
10479SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut);
10480SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut);
10481
10482/*
10483** CAPI3REF: Constraint values in xBestIndex()
10484** METHOD: sqlite3_index_info
10485**
10486** This API may only be used from within the [xBestIndex|xBestIndex method]
10487** of a [virtual table] implementation. The result of calling this interface
10488** from outside of an xBestIndex method are undefined and probably harmful.
10489**
10490** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
10491** the [xBestIndex] method of a [virtual table] implementation, with P being
10492** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
10493** J being a 0-based index into P->aConstraint[], then this routine
10494** attempts to set *V to the value of the right-hand operand of
10495** that constraint if the right-hand operand is known. ^If the
10496** right-hand operand is not known, then *V is set to a NULL pointer.
10497** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
10498** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
10499** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10500** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
10501** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
10502** something goes wrong.
10503**
10504** The sqlite3_vtab_rhs_value() interface is usually only successful if
10505** the right-hand operand of a constraint is a literal value in the original
10506** SQL statement. If the right-hand operand is an expression or a reference
10507** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
10508** will probably return [SQLITE_NOTFOUND].
10509**
10510** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
10511** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10512** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
10513**
10514** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
10515** and remains valid for the duration of the xBestIndex method call.
10516** ^When xBestIndex returns, the sqlite3_value object returned by
10517** sqlite3_vtab_rhs_value() is automatically deallocated.
10518**
10519** The "_rhs_" in the name of this routine is an abbreviation for
10520** "Right-Hand Side".
10521*/
10522SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
10523
10524/*
10525** CAPI3REF: Conflict resolution modes
10526** KEYWORDS: {conflict resolution mode}
10527**
10528** These constants are returned by [sqlite3_vtab_on_conflict()] to
10529** inform a [virtual table] implementation what the [ON CONFLICT] mode
10530** is for the SQL statement being evaluated.
10531**
10532** Note that the [SQLITE_IGNORE] constant is also used as a potential
10533** return value from the [sqlite3_set_authorizer()] callback and that
10534** [SQLITE_ABORT] is also a [result code].
10535*/
10536#define SQLITE_ROLLBACK1 1
10537/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
10538#define SQLITE_FAIL3 3
10539/* #define SQLITE_ABORT 4 // Also an error code */
10540#define SQLITE_REPLACE5 5
10541
10542/*
10543** CAPI3REF: Prepared Statement Scan Status Opcodes
10544** KEYWORDS: {scanstatus options}
10545**
10546** The following constants can be used for the T parameter to the
10547** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
10548** different metric for sqlite3_stmt_scanstatus() to return.
10549**
10550** When the value returned to V is a string, space to hold that string is
10551** managed by the prepared statement S and will be automatically freed when
10552** S is finalized.
10553**
10554** Not all values are available for all query elements. When a value is
10555** not available, the output variable is set to -1 if the value is numeric,
10556** or to NULL if it is a string (SQLITE_SCANSTAT_NAME).
10557**
10558** <dl>
10559** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
10560** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
10561** set to the total number of times that the X-th loop has run.</dd>
10562**
10563** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
10564** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
10565** to the total number of rows examined by all iterations of the X-th loop.</dd>
10566**
10567** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
10568** <dd>^The "double" variable pointed to by the V parameter will be set to the
10569** query planner's estimate for the average number of rows output from each
10570** iteration of the X-th loop. If the query planner's estimates was accurate,
10571** then this value will approximate the quotient NVISIT/NLOOP and the
10572** product of this value for all prior loops with the same SELECTID will
10573** be the NLOOP value for the current loop.
10574**
10575** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
10576** <dd>^The "const char *" variable pointed to by the V parameter will be set
10577** to a zero-terminated UTF-8 string containing the name of the index or table
10578** used for the X-th loop.
10579**
10580** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
10581** <dd>^The "const char *" variable pointed to by the V parameter will be set
10582** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10583** description for the X-th loop.
10584**
10585** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECTID</dt>
10586** <dd>^The "int" variable pointed to by the V parameter will be set to the
10587** id for the X-th query plan element. The id value is unique within the
10588** statement. The select-id is the same value as is output in the first
10589** column of an [EXPLAIN QUERY PLAN] query.
10590**
10591** [[SQLITE_SCANSTAT_PARENTID]] <dt>SQLITE_SCANSTAT_PARENTID</dt>
10592** <dd>The "int" variable pointed to by the V parameter will be set to the
10593** the id of the parent of the current query element, if applicable, or
10594** to zero if the query element has no parent. This is the same value as
10595** returned in the second column of an [EXPLAIN QUERY PLAN] query.
10596**
10597** [[SQLITE_SCANSTAT_NCYCLE]] <dt>SQLITE_SCANSTAT_NCYCLE</dt>
10598** <dd>The sqlite3_int64 output value is set to the number of cycles,
10599** according to the processor time-stamp counter, that elapsed while the
10600** query element was being processed. This value is not available for
10601** all query elements - if it is unavailable the output variable is
10602** set to -1.
10603** </dl>
10604*/
10605#define SQLITE_SCANSTAT_NLOOP0 0
10606#define SQLITE_SCANSTAT_NVISIT1 1
10607#define SQLITE_SCANSTAT_EST2 2
10608#define SQLITE_SCANSTAT_NAME3 3
10609#define SQLITE_SCANSTAT_EXPLAIN4 4
10610#define SQLITE_SCANSTAT_SELECTID5 5
10611#define SQLITE_SCANSTAT_PARENTID6 6
10612#define SQLITE_SCANSTAT_NCYCLE7 7
10613
10614/*
10615** CAPI3REF: Prepared Statement Scan Status
10616** METHOD: sqlite3_stmt
10617**
10618** These interfaces return information about the predicted and measured
10619** performance for pStmt. Advanced applications can use this
10620** interface to compare the predicted and the measured performance and
10621** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
10622**
10623** Since this interface is expected to be rarely used, it is only
10624** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
10625** compile-time option.
10626**
10627** The "iScanStatusOp" parameter determines which status information to return.
10628** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
10629** of this interface is undefined. ^The requested measurement is written into
10630** a variable pointed to by the "pOut" parameter.
10631**
10632** The "flags" parameter must be passed a mask of flags. At present only
10633** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10634** is specified, then status information is available for all elements
10635** of a query plan that are reported by "EXPLAIN QUERY PLAN" output. If
10636** SQLITE_SCANSTAT_COMPLEX is not specified, then only query plan elements
10637** that correspond to query loops (the "SCAN..." and "SEARCH..." elements of
10638** the EXPLAIN QUERY PLAN output) are available. Invoking API
10639** sqlite3_stmt_scanstatus() is equivalent to calling
10640** sqlite3_stmt_scanstatus_v2() with a zeroed flags parameter.
10641**
10642** Parameter "idx" identifies the specific query element to retrieve statistics
10643** for. Query elements are numbered starting from zero. A value of -1 may be
10644** to query for statistics regarding the entire query. ^If idx is out of range
10645** - less than -1 or greater than or equal to the total number of query
10646** elements used to implement the statement - a non-zero value is returned and
10647** the variable that pOut points to is unchanged.
10648**
10649** See also: [sqlite3_stmt_scanstatus_reset()]
10650*/
10651SQLITE_API int sqlite3_stmt_scanstatus(
10652 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10653 int idx, /* Index of loop to report on */
10654 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10655 void *pOut /* Result written here */
10656);
10657SQLITE_API int sqlite3_stmt_scanstatus_v2(
10658 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
10659 int idx, /* Index of loop to report on */
10660 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
10661 int flags, /* Mask of flags defined below */
10662 void *pOut /* Result written here */
10663);
10664
10665/*
10666** CAPI3REF: Prepared Statement Scan Status
10667** KEYWORDS: {scan status flags}
10668*/
10669#define SQLITE_SCANSTAT_COMPLEX0x0001 0x0001
10670
10671/*
10672** CAPI3REF: Zero Scan-Status Counters
10673** METHOD: sqlite3_stmt
10674**
10675** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10676**
10677** This API is only available if the library is built with pre-processor
10678** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10679*/
10680SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10681
10682/*
10683** CAPI3REF: Flush caches to disk mid-transaction
10684** METHOD: sqlite3
10685**
10686** ^If a write-transaction is open on [database connection] D when the
10687** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
10688** pages in the pager-cache that are not currently in use are written out
10689** to disk. A dirty page may be in use if a database cursor created by an
10690** active SQL statement is reading from it, or if it is page 1 of a database
10691** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
10692** interface flushes caches for all schemas - "main", "temp", and
10693** any [attached] databases.
10694**
10695** ^If this function needs to obtain extra database locks before dirty pages
10696** can be flushed to disk, it does so. ^If those locks cannot be obtained
10697** immediately and there is a busy-handler callback configured, it is invoked
10698** in the usual manner. ^If the required lock still cannot be obtained, then
10699** the database is skipped and an attempt made to flush any dirty pages
10700** belonging to the next (if any) database. ^If any databases are skipped
10701** because locks cannot be obtained, but no other error occurs, this
10702** function returns SQLITE_BUSY.
10703**
10704** ^If any other error occurs while flushing dirty pages to disk (for
10705** example an IO error or out-of-memory condition), then processing is
10706** abandoned and an SQLite [error code] is returned to the caller immediately.
10707**
10708** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10709**
10710** ^This function does not set the database handle error code or message
10711** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10712*/
10713SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10714
10715/*
10716** CAPI3REF: The pre-update hook.
10717** METHOD: sqlite3
10718**
10719** ^These interfaces are only available if SQLite is compiled using the
10720** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10721**
10722** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10723** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10724** on a database table.
10725** ^At most one preupdate hook may be registered at a time on a single
10726** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10727** the previous setting.
10728** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10729** with a NULL pointer as the second parameter.
10730** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10731** the first parameter to callbacks.
10732**
10733** ^The preupdate hook only fires for changes to real database tables; the
10734** preupdate hook is not invoked for changes to [virtual tables] or to
10735** system tables like sqlite_sequence or sqlite_stat1.
10736**
10737** ^The second parameter to the preupdate callback is a pointer to
10738** the [database connection] that registered the preupdate hook.
10739** ^The third parameter to the preupdate callback is one of the constants
10740** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10741** kind of update operation that is about to occur.
10742** ^(The fourth parameter to the preupdate callback is the name of the
10743** database within the database connection that is being modified. This
10744** will be "main" for the main database or "temp" for TEMP tables or
10745** the name given after the AS keyword in the [ATTACH] statement for attached
10746** databases.)^
10747** ^The fifth parameter to the preupdate callback is the name of the
10748** table that is being modified.
10749**
10750** For an UPDATE or DELETE operation on a [rowid table], the sixth
10751** parameter passed to the preupdate callback is the initial [rowid] of the
10752** row being modified or deleted. For an INSERT operation on a rowid table,
10753** or any operation on a WITHOUT ROWID table, the value of the sixth
10754** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10755** seventh parameter is the final rowid value of the row being inserted
10756** or updated. The value of the seventh parameter passed to the callback
10757** function is not defined for operations on WITHOUT ROWID tables, or for
10758** DELETE operations on rowid tables.
10759**
10760** ^The sqlite3_preupdate_hook(D,C,P) function returns the P argument from
10761** the previous call on the same [database connection] D, or NULL for
10762** the first call on D.
10763**
10764** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10765** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10766** provide additional information about a preupdate event. These routines
10767** may only be called from within a preupdate callback. Invoking any of
10768** these routines from outside of a preupdate callback or with a
10769** [database connection] pointer that is different from the one supplied
10770** to the preupdate callback results in undefined and probably undesirable
10771** behavior.
10772**
10773** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10774** in the row that is being inserted, updated, or deleted.
10775**
10776** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10777** a [protected sqlite3_value] that contains the value of the Nth column of
10778** the table row before it is updated. The N parameter must be between 0
10779** and one less than the number of columns or the behavior will be
10780** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10781** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10782** behavior is undefined. The [sqlite3_value] that P points to
10783** will be destroyed when the preupdate callback returns.
10784**
10785** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10786** a [protected sqlite3_value] that contains the value of the Nth column of
10787** the table row after it is updated. The N parameter must be between 0
10788** and one less than the number of columns or the behavior will be
10789** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10790** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10791** behavior is undefined. The [sqlite3_value] that P points to
10792** will be destroyed when the preupdate callback returns.
10793**
10794** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10795** callback was invoked as a result of a direct insert, update, or delete
10796** operation; or 1 for inserts, updates, or deletes invoked by top-level
10797** triggers; or 2 for changes resulting from triggers called by top-level
10798** triggers; and so forth.
10799**
10800** When the [sqlite3_blob_write()] API is used to update a blob column,
10801** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10802** in this case the new values are not available. In this case, when a
10803** callback made with op==SQLITE_DELETE is actually a write using the
10804** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10805** the index of the column being written. In other cases, where the
10806** pre-update hook is being invoked for some other reason, including a
10807** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10808**
10809** See also: [sqlite3_update_hook()]
10810*/
10811#if defined(SQLITE_ENABLE_PREUPDATE_HOOK1)
10812SQLITE_API void *sqlite3_preupdate_hook(
10813 sqlite3 *db,
10814 void(*xPreUpdate)(
10815 void *pCtx, /* Copy of third arg to preupdate_hook() */
10816 sqlite3 *db, /* Database handle */
10817 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10818 char const *zDb, /* Database name */
10819 char const *zName, /* Table name */
10820 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10821 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10822 ),
10823 void*
10824);
10825SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10826SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10827SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10828SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10829SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *);
10830#endif
10831
10832/*
10833** CAPI3REF: Low-level system error code
10834** METHOD: sqlite3
10835**
10836** ^Attempt to return the underlying operating system error code or error
10837** number that caused the most recent I/O error or failure to open a file.
10838** The return value is OS-dependent. For example, on unix systems, after
10839** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10840** called to get back the underlying "errno" that caused the problem, such
10841** as ENOSPC, EAUTH, EISDIR, and so forth.
10842*/
10843SQLITE_API int sqlite3_system_errno(sqlite3*);
10844
10845/*
10846** CAPI3REF: Database Snapshot
10847** KEYWORDS: {snapshot} {sqlite3_snapshot}
10848**
10849** An instance of the snapshot object records the state of a [WAL mode]
10850** database for some specific point in history.
10851**
10852** In [WAL mode], multiple [database connections] that are open on the
10853** same database file can each be reading a different historical version
10854** of the database file. When a [database connection] begins a read
10855** transaction, that connection sees an unchanging copy of the database
10856** as it existed for the point in time when the transaction first started.
10857** Subsequent changes to the database from other connections are not seen
10858** by the reader until a new read transaction is started.
10859**
10860** The sqlite3_snapshot object records state information about an historical
10861** version of the database file so that it is possible to later open a new read
10862** transaction that sees that historical version of the database rather than
10863** the most recent version.
10864*/
10865typedef struct sqlite3_snapshot {
10866 unsigned char hidden[48];
10867} sqlite3_snapshot;
10868
10869/*
10870** CAPI3REF: Record A Database Snapshot
10871** CONSTRUCTOR: sqlite3_snapshot
10872**
10873** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10874** new [sqlite3_snapshot] object that records the current state of
10875** schema S in database connection D. ^On success, the
10876** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10877** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10878** If there is not already a read-transaction open on schema S when
10879** this function is called, one is opened automatically.
10880**
10881** If a read-transaction is opened by this function, then it is guaranteed
10882** that the returned snapshot object may not be invalidated by a database
10883** writer or checkpointer until after the read-transaction is closed. This
10884** is not guaranteed if a read-transaction is already open when this
10885** function is called. In that case, any subsequent write or checkpoint
10886** operation on the database may invalidate the returned snapshot handle,
10887** even while the read-transaction remains open.
10888**
10889** The following must be true for this function to succeed. If any of
10890** the following statements are false when sqlite3_snapshot_get() is
10891** called, SQLITE_ERROR is returned. The final value of *P is undefined
10892** in this case.
10893**
10894** <ul>
10895** <li> The database handle must not be in [autocommit mode].
10896**
10897** <li> Schema S of [database connection] D must be a [WAL mode] database.
10898**
10899** <li> There must not be a write transaction open on schema S of database
10900** connection D.
10901**
10902** <li> One or more transactions must have been written to the current wal
10903** file since it was created on disk (by any connection). This means
10904** that a snapshot cannot be taken on a wal mode database with no wal
10905** file immediately after it is first opened. At least one transaction
10906** must be written to it first.
10907** </ul>
10908**
10909** This function may also return SQLITE_NOMEM. If it is called with the
10910** database handle in autocommit mode but fails for some other reason,
10911** whether or not a read transaction is opened on schema S is undefined.
10912**
10913** The [sqlite3_snapshot] object returned from a successful call to
10914** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10915** to avoid a memory leak.
10916**
10917** The [sqlite3_snapshot_get()] interface is only available when the
10918** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10919*/
10920SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10921 sqlite3 *db,
10922 const char *zSchema,
10923 sqlite3_snapshot **ppSnapshot
10924);
10925
10926/*
10927** CAPI3REF: Start a read transaction on an historical snapshot
10928** METHOD: sqlite3_snapshot
10929**
10930** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10931** transaction or upgrades an existing one for schema S of
10932** [database connection] D such that the read transaction refers to
10933** historical [snapshot] P, rather than the most recent change to the
10934** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10935** on success or an appropriate [error code] if it fails.
10936**
10937** ^In order to succeed, the database connection must not be in
10938** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10939** is already a read transaction open on schema S, then the database handle
10940** must have no active statements (SELECT statements that have been passed
10941** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10942** SQLITE_ERROR is returned if either of these conditions is violated, or
10943** if schema S does not exist, or if the snapshot object is invalid.
10944**
10945** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10946** snapshot has been overwritten by a [checkpoint]. In this case
10947** SQLITE_ERROR_SNAPSHOT is returned.
10948**
10949** If there is already a read transaction open when this function is
10950** invoked, then the same read transaction remains open (on the same
10951** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10952** is returned. If another error code - for example SQLITE_PROTOCOL or an
10953** SQLITE_IOERR error code - is returned, then the final state of the
10954** read transaction is undefined. If SQLITE_OK is returned, then the
10955** read transaction is now open on database snapshot P.
10956**
10957** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10958** database connection D does not know that the database file for
10959** schema S is in [WAL mode]. A database connection might not know
10960** that the database file is in [WAL mode] if there has been no prior
10961** I/O on that database connection, or if the database entered [WAL mode]
10962** after the most recent I/O on the database connection.)^
10963** (Hint: Run "[PRAGMA application_id]" against a newly opened
10964** database connection in order to make it ready to use snapshots.)
10965**
10966** The [sqlite3_snapshot_open()] interface is only available when the
10967** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10968*/
10969SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10970 sqlite3 *db,
10971 const char *zSchema,
10972 sqlite3_snapshot *pSnapshot
10973);
10974
10975/*
10976** CAPI3REF: Destroy a snapshot
10977** DESTRUCTOR: sqlite3_snapshot
10978**
10979** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10980** The application must eventually free every [sqlite3_snapshot] object
10981** using this routine to avoid a memory leak.
10982**
10983** The [sqlite3_snapshot_free()] interface is only available when the
10984** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10985*/
10986SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10987
10988/*
10989** CAPI3REF: Compare the ages of two snapshot handles.
10990** METHOD: sqlite3_snapshot
10991**
10992** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10993** of two valid snapshot handles.
10994**
10995** If the two snapshot handles are not associated with the same database
10996** file, the result of the comparison is undefined.
10997**
10998** Additionally, the result of the comparison is only valid if both of the
10999** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
11000** last time the wal file was deleted. The wal file is deleted when the
11001** database is changed back to rollback mode or when the number of database
11002** clients drops to zero. If either snapshot handle was obtained before the
11003** wal file was last deleted, the value returned by this function
11004** is undefined.
11005**
11006** Otherwise, this API returns a negative value if P1 refers to an older
11007** snapshot than P2, zero if the two handles refer to the same database
11008** snapshot, and a positive value if P1 is a newer snapshot than P2.
11009**
11010** This interface is only available if SQLite is compiled with the
11011** [SQLITE_ENABLE_SNAPSHOT] option.
11012*/
11013SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
11014 sqlite3_snapshot *p1,
11015 sqlite3_snapshot *p2
11016);
11017
11018/*
11019** CAPI3REF: Recover snapshots from a wal file
11020** METHOD: sqlite3_snapshot
11021**
11022** If a [WAL file] remains on disk after all database connections close
11023** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
11024** or because the last process to have the database opened exited without
11025** calling [sqlite3_close()]) and a new connection is subsequently opened
11026** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
11027** will only be able to open the last transaction added to the WAL file
11028** even though the WAL file contains other valid transactions.
11029**
11030** This function attempts to scan the WAL file associated with database zDb
11031** of database handle db and make all valid snapshots available to
11032** sqlite3_snapshot_open(). It is an error if there is already a read
11033** transaction open on the database, or if the database is not a WAL mode
11034** database.
11035**
11036** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
11037**
11038** This interface is only available if SQLite is compiled with the
11039** [SQLITE_ENABLE_SNAPSHOT] option.
11040*/
11041SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
11042
11043/*
11044** CAPI3REF: Serialize a database
11045**
11046** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
11047** that is a serialization of the S database on [database connection] D.
11048** If P is not a NULL pointer, then the size of the database in bytes
11049** is written into *P.
11050**
11051** For an ordinary on-disk database file, the serialization is just a
11052** copy of the disk file. For an in-memory database or a "TEMP" database,
11053** the serialization is the same sequence of bytes which would be written
11054** to disk if that database where backed up to disk.
11055**
11056** The usual case is that sqlite3_serialize() copies the serialization of
11057** the database into memory obtained from [sqlite3_malloc64()] and returns
11058** a pointer to that memory. The caller is responsible for freeing the
11059** returned value to avoid a memory leak. However, if the F argument
11060** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
11061** are made, and the sqlite3_serialize() function will return a pointer
11062** to the contiguous memory representation of the database that SQLite
11063** is currently using for that database, or NULL if the no such contiguous
11064** memory representation of the database exists. A contiguous memory
11065** representation of the database will usually only exist if there has
11066** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
11067** values of D and S.
11068** The size of the database is written into *P even if the
11069** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
11070** of the database exists.
11071**
11072** After the call, if the SQLITE_SERIALIZE_NOCOPY bit had been set,
11073** the returned buffer content will remain accessible and unchanged
11074** until either the next write operation on the connection or when
11075** the connection is closed, and applications must not modify the
11076** buffer. If the bit had been clear, the returned buffer will not
11077** be accessed by SQLite after the call.
11078**
11079** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11080** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11081** allocation error occurs.
11082**
11083** This interface is omitted if SQLite is compiled with the
11084** [SQLITE_OMIT_DESERIALIZE] option.
11085*/
11086SQLITE_API unsigned char *sqlite3_serialize(
11087 sqlite3 *db, /* The database connection */
11088 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
11089 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
11090 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
11091);
11092
11093/*
11094** CAPI3REF: Flags for sqlite3_serialize
11095**
11096** Zero or more of the following constants can be OR-ed together for
11097** the F argument to [sqlite3_serialize(D,S,P,F)].
11098**
11099** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
11100** a pointer to contiguous in-memory database that it is currently using,
11101** without making a copy of the database. If SQLite is not currently using
11102** a contiguous in-memory database, then this option causes
11103** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
11104** using a contiguous in-memory database if it has been initialized by a
11105** prior call to [sqlite3_deserialize()].
11106*/
11107#define SQLITE_SERIALIZE_NOCOPY0x001 0x001 /* Do no memory allocations */
11108
11109/*
11110** CAPI3REF: Deserialize a database
11111**
11112** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
11113** [database connection] D to disconnect from database S and then
11114** reopen S as an in-memory database based on the serialization contained
11115** in P. The serialized database P is N bytes in size. M is the size of
11116** the buffer P, which might be larger than N. If M is larger than N, and
11117** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
11118** permitted to add content to the in-memory database as long as the total
11119** size does not exceed M bytes.
11120**
11121** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
11122** invoke sqlite3_free() on the serialization buffer when the database
11123** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
11124** SQLite will try to increase the buffer size using sqlite3_realloc64()
11125** if writes on the database cause it to grow larger than M bytes.
11126**
11127** Applications must not modify the buffer P or invalidate it before
11128** the database connection D is closed.
11129**
11130** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
11131** database is currently in a read transaction or is involved in a backup
11132** operation.
11133**
11134** It is not possible to deserialized into the TEMP database. If the
11135** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
11136** function returns SQLITE_ERROR.
11137**
11138** The deserialized database should not be in [WAL mode]. If the database
11139** is in WAL mode, then any attempt to use the database file will result
11140** in an [SQLITE_CANTOPEN] error. The application can set the
11141** [file format version numbers] (bytes 18 and 19) of the input database P
11142** to 0x01 prior to invoking sqlite3_deserialize(D,S,P,N,M,F) to force the
11143** database file into rollback mode and work around this limitation.
11144**
11145** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
11146** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
11147** [sqlite3_free()] is invoked on argument P prior to returning.
11148**
11149** This interface is omitted if SQLite is compiled with the
11150** [SQLITE_OMIT_DESERIALIZE] option.
11151*/
11152SQLITE_API int sqlite3_deserialize(
11153 sqlite3 *db, /* The database connection */
11154 const char *zSchema, /* Which DB to reopen with the deserialization */
11155 unsigned char *pData, /* The serialized database content */
11156 sqlite3_int64 szDb, /* Number bytes in the deserialization */
11157 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
11158 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
11159);
11160
11161/*
11162** CAPI3REF: Flags for sqlite3_deserialize()
11163**
11164** The following are allowed values for 6th argument (the F argument) to
11165** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
11166**
11167** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
11168** in the P argument is held in memory obtained from [sqlite3_malloc64()]
11169** and that SQLite should take ownership of this memory and automatically
11170** free it when it has finished using it. Without this flag, the caller
11171** is responsible for freeing any dynamically allocated memory.
11172**
11173** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
11174** grow the size of the database using calls to [sqlite3_realloc64()]. This
11175** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
11176** Without this flag, the deserialized database cannot increase in size beyond
11177** the number of bytes specified by the M parameter.
11178**
11179** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
11180** should be treated as read-only.
11181*/
11182#define SQLITE_DESERIALIZE_FREEONCLOSE1 1 /* Call sqlite3_free() on close */
11183#define SQLITE_DESERIALIZE_RESIZEABLE2 2 /* Resize using sqlite3_realloc64() */
11184#define SQLITE_DESERIALIZE_READONLY4 4 /* Database is read-only */
11185
11186/*
11187** Undo the hack that converts floating point types to integer for
11188** builds on processors without floating point support.
11189*/
11190#ifdef SQLITE_OMIT_FLOATING_POINT
11191# undef double
11192#endif
11193
11194#if defined(__wasi__)
11195# undef SQLITE_WASI
11196# define SQLITE_WASI 1
11197# ifndef SQLITE_OMIT_LOAD_EXTENSION1
11198# define SQLITE_OMIT_LOAD_EXTENSION1
11199# endif
11200# ifndef SQLITE_THREADSAFE2
11201# define SQLITE_THREADSAFE2 0
11202# endif
11203#endif
11204
11205#if 0
11206} /* End of the 'extern "C"' block */
11207#endif
11208#endif /* SQLITE3_H */
11209
11210/******** Begin file sqlite3rtree.h *********/
11211/*
11212** 2010 August 30
11213**
11214** The author disclaims copyright to this source code. In place of
11215** a legal notice, here is a blessing:
11216**
11217** May you do good and not evil.
11218** May you find forgiveness for yourself and forgive others.
11219** May you share freely, never taking more than you give.
11220**
11221*************************************************************************
11222*/
11223
11224#ifndef _SQLITE3RTREE_H_
11225#define _SQLITE3RTREE_H_
11226
11227
11228#if 0
11229extern "C" {
11230#endif
11231
11232typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
11233typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
11234
11235/* The double-precision datatype used by RTree depends on the
11236** SQLITE_RTREE_INT_ONLY compile-time option.
11237*/
11238#ifdef SQLITE_RTREE_INT_ONLY
11239 typedef sqlite3_int64 sqlite3_rtree_dbl;
11240#else
11241 typedef double sqlite3_rtree_dbl;
11242#endif
11243
11244/*
11245** Register a geometry callback named zGeom that can be used as part of an
11246** R-Tree geometry query as follows:
11247**
11248** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
11249*/
11250SQLITE_API int sqlite3_rtree_geometry_callback(
11251 sqlite3 *db,
11252 const char *zGeom,
11253 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
11254 void *pContext
11255);
11256
11257
11258/*
11259** A pointer to a structure of the following type is passed as the first
11260** argument to callbacks registered using rtree_geometry_callback().
11261*/
11262struct sqlite3_rtree_geometry {
11263 void *pContext; /* Copy of pContext passed to s_r_g_c() */
11264 int nParam; /* Size of array aParam[] */
11265 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
11266 void *pUser; /* Callback implementation user data */
11267 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
11268};
11269
11270/*
11271** Register a 2nd-generation geometry callback named zScore that can be
11272** used as part of an R-Tree geometry query as follows:
11273**
11274** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
11275*/
11276SQLITE_API int sqlite3_rtree_query_callback(
11277 sqlite3 *db,
11278 const char *zQueryFunc,
11279 int (*xQueryFunc)(sqlite3_rtree_query_info*),
11280 void *pContext,
11281 void (*xDestructor)(void*)
11282);
11283
11284
11285/*
11286** A pointer to a structure of the following type is passed as the
11287** argument to scored geometry callback registered using
11288** sqlite3_rtree_query_callback().
11289**
11290** Note that the first 5 fields of this structure are identical to
11291** sqlite3_rtree_geometry. This structure is a subclass of
11292** sqlite3_rtree_geometry.
11293*/
11294struct sqlite3_rtree_query_info {
11295 void *pContext; /* pContext from when function registered */
11296 int nParam; /* Number of function parameters */
11297 sqlite3_rtree_dbl *aParam; /* value of function parameters */
11298 void *pUser; /* callback can use this, if desired */
11299 void (*xDelUser)(void*); /* function to free pUser */
11300 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
11301 unsigned int *anQueue; /* Number of pending entries in the queue */
11302 int nCoord; /* Number of coordinates */
11303 int iLevel; /* Level of current node or entry */
11304 int mxLevel; /* The largest iLevel value in the tree */
11305 sqlite3_int64 iRowid; /* Rowid for current entry */
11306 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
11307 int eParentWithin; /* Visibility of parent node */
11308 int eWithin; /* OUT: Visibility */
11309 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
11310 /* The following fields are only available in 3.8.11 and later */
11311 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
11312};
11313
11314/*
11315** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
11316*/
11317#define NOT_WITHIN0 0 /* Object completely outside of query region */
11318#define PARTLY_WITHIN1 1 /* Object partially overlaps query region */
11319#define FULLY_WITHIN2 2 /* Object fully contained within query region */
11320
11321
11322#if 0
11323} /* end of the 'extern "C"' block */
11324#endif
11325
11326#endif /* ifndef _SQLITE3RTREE_H_ */
11327
11328/******** End of sqlite3rtree.h *********/
11329/******** Begin file sqlite3session.h *********/
11330
11331#if !defined(__SQLITESESSION_H_1) && defined(SQLITE_ENABLE_SESSION1)
11332#define __SQLITESESSION_H_1 1
11333
11334/*
11335** Make sure we can call this stuff from C++.
11336*/
11337#if 0
11338extern "C" {
11339#endif
11340
11341
11342/*
11343** CAPI3REF: Session Object Handle
11344**
11345** An instance of this object is a [session] that can be used to
11346** record changes to a database.
11347*/
11348typedef struct sqlite3_session sqlite3_session;
11349
11350/*
11351** CAPI3REF: Changeset Iterator Handle
11352**
11353** An instance of this object acts as a cursor for iterating
11354** over the elements of a [changeset] or [patchset].
11355*/
11356typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
11357
11358/*
11359** CAPI3REF: Create A New Session Object
11360** CONSTRUCTOR: sqlite3_session
11361**
11362** Create a new session object attached to database handle db. If successful,
11363** a pointer to the new object is written to *ppSession and SQLITE_OK is
11364** returned. If an error occurs, *ppSession is set to NULL and an SQLite
11365** error code (e.g. SQLITE_NOMEM) is returned.
11366**
11367** It is possible to create multiple session objects attached to a single
11368** database handle.
11369**
11370** Session objects created using this function should be deleted using the
11371** [sqlite3session_delete()] function before the database handle that they
11372** are attached to is itself closed. If the database handle is closed before
11373** the session object is deleted, then the results of calling any session
11374** module function, including [sqlite3session_delete()] on the session object
11375** are undefined.
11376**
11377** Because the session module uses the [sqlite3_preupdate_hook()] API, it
11378** is not possible for an application to register a pre-update hook on a
11379** database handle that has one or more session objects attached. Nor is
11380** it possible to create a session object attached to a database handle for
11381** which a pre-update hook is already defined. The results of attempting
11382** either of these things are undefined.
11383**
11384** The session object will be used to create changesets for tables in
11385** database zDb, where zDb is either "main", or "temp", or the name of an
11386** attached database. It is not an error if database zDb is not attached
11387** to the database when the session object is created.
11388*/
11389SQLITE_API int sqlite3session_create(
11390 sqlite3 *db, /* Database handle */
11391 const char *zDb, /* Name of db (e.g. "main") */
11392 sqlite3_session **ppSession /* OUT: New session object */
11393);
11394
11395/*
11396** CAPI3REF: Delete A Session Object
11397** DESTRUCTOR: sqlite3_session
11398**
11399** Delete a session object previously allocated using
11400** [sqlite3session_create()]. Once a session object has been deleted, the
11401** results of attempting to use pSession with any other session module
11402** function are undefined.
11403**
11404** Session objects must be deleted before the database handle to which they
11405** are attached is closed. Refer to the documentation for
11406** [sqlite3session_create()] for details.
11407*/
11408SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
11409
11410/*
11411** CAPI3REF: Configure a Session Object
11412** METHOD: sqlite3_session
11413**
11414** This method is used to configure a session object after it has been
11415** created. At present the only valid values for the second parameter are
11416** [SQLITE_SESSION_OBJCONFIG_SIZE] and [SQLITE_SESSION_OBJCONFIG_ROWID].
11417**
11418*/
11419SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg);
11420
11421/*
11422** CAPI3REF: Options for sqlite3session_object_config
11423**
11424** The following values may passed as the the 2nd parameter to
11425** sqlite3session_object_config().
11426**
11427** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd>
11428** This option is used to set, clear or query the flag that enables
11429** the [sqlite3session_changeset_size()] API. Because it imposes some
11430** computational overhead, this API is disabled by default. Argument
11431** pArg must point to a value of type (int). If the value is initially
11432** 0, then the sqlite3session_changeset_size() API is disabled. If it
11433** is greater than 0, then the same API is enabled. Or, if the initial
11434** value is less than zero, no change is made. In all cases the (int)
11435** variable is set to 1 if the sqlite3session_changeset_size() API is
11436** enabled following the current call, or 0 otherwise.
11437**
11438** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
11439** the first table has been attached to the session object.
11440**
11441** <dt>SQLITE_SESSION_OBJCONFIG_ROWID <dd>
11442** This option is used to set, clear or query the flag that enables
11443** collection of data for tables with no explicit PRIMARY KEY.
11444**
11445** Normally, tables with no explicit PRIMARY KEY are simply ignored
11446** by the sessions module. However, if this flag is set, it behaves
11447** as if such tables have a column "_rowid_ INTEGER PRIMARY KEY" inserted
11448** as their leftmost columns.
11449**
11450** It is an error (SQLITE_MISUSE) to attempt to modify this setting after
11451** the first table has been attached to the session object.
11452*/
11453#define SQLITE_SESSION_OBJCONFIG_SIZE1 1
11454#define SQLITE_SESSION_OBJCONFIG_ROWID2 2
11455
11456/*
11457** CAPI3REF: Enable Or Disable A Session Object
11458** METHOD: sqlite3_session
11459**
11460** Enable or disable the recording of changes by a session object. When
11461** enabled, a session object records changes made to the database. When
11462** disabled - it does not. A newly created session object is enabled.
11463** Refer to the documentation for [sqlite3session_changeset()] for further
11464** details regarding how enabling and disabling a session object affects
11465** the eventual changesets.
11466**
11467** Passing zero to this function disables the session. Passing a value
11468** greater than zero enables it. Passing a value less than zero is a
11469** no-op, and may be used to query the current state of the session.
11470**
11471** The return value indicates the final state of the session object: 0 if
11472** the session is disabled, or 1 if it is enabled.
11473*/
11474SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
11475
11476/*
11477** CAPI3REF: Set Or Clear the Indirect Change Flag
11478** METHOD: sqlite3_session
11479**
11480** Each change recorded by a session object is marked as either direct or
11481** indirect. A change is marked as indirect if either:
11482**
11483** <ul>
11484** <li> The session object "indirect" flag is set when the change is
11485** made, or
11486** <li> The change is made by an SQL trigger or foreign key action
11487** instead of directly as a result of a users SQL statement.
11488** </ul>
11489**
11490** If a single row is affected by more than one operation within a session,
11491** then the change is considered indirect if all operations meet the criteria
11492** for an indirect change above, or direct otherwise.
11493**
11494** This function is used to set, clear or query the session object indirect
11495** flag. If the second argument passed to this function is zero, then the
11496** indirect flag is cleared. If it is greater than zero, the indirect flag
11497** is set. Passing a value less than zero does not modify the current value
11498** of the indirect flag, and may be used to query the current state of the
11499** indirect flag for the specified session object.
11500**
11501** The return value indicates the final state of the indirect flag: 0 if
11502** it is clear, or 1 if it is set.
11503*/
11504SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
11505
11506/*
11507** CAPI3REF: Attach A Table To A Session Object
11508** METHOD: sqlite3_session
11509**
11510** If argument zTab is not NULL, then it is the name of a table to attach
11511** to the session object passed as the first argument. All subsequent changes
11512** made to the table while the session object is enabled will be recorded. See
11513** documentation for [sqlite3session_changeset()] for further details.
11514**
11515** Or, if argument zTab is NULL, then changes are recorded for all tables
11516** in the database. If additional tables are added to the database (by
11517** executing "CREATE TABLE" statements) after this call is made, changes for
11518** the new tables are also recorded.
11519**
11520** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
11521** defined as part of their CREATE TABLE statement. It does not matter if the
11522** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
11523** KEY may consist of a single column, or may be a composite key.
11524**
11525** It is not an error if the named table does not exist in the database. Nor
11526** is it an error if the named table does not have a PRIMARY KEY. However,
11527** no changes will be recorded in either of these scenarios.
11528**
11529** Changes are not recorded for individual rows that have NULL values stored
11530** in one or more of their PRIMARY KEY columns.
11531**
11532** SQLITE_OK is returned if the call completes without error. Or, if an error
11533** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
11534**
11535** <h3>Special sqlite_stat1 Handling</h3>
11536**
11537** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
11538** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
11539** <pre>
11540** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
11541** </pre>
11542**
11543** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
11544** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
11545** are recorded for rows for which (idx IS NULL) is true. However, for such
11546** rows a zero-length blob (SQL value X'') is stored in the changeset or
11547** patchset instead of a NULL value. This allows such changesets to be
11548** manipulated by legacy implementations of sqlite3changeset_invert(),
11549** concat() and similar.
11550**
11551** The sqlite3changeset_apply() function automatically converts the
11552** zero-length blob back to a NULL value when updating the sqlite_stat1
11553** table. However, if the application calls sqlite3changeset_new(),
11554** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
11555** iterator directly (including on a changeset iterator passed to a
11556** conflict-handler callback) then the X'' value is returned. The application
11557** must translate X'' to NULL itself if required.
11558**
11559** Legacy (older than 3.22.0) versions of the sessions module cannot capture
11560** changes made to the sqlite_stat1 table. Legacy versions of the
11561** sqlite3changeset_apply() function silently ignore any modifications to the
11562** sqlite_stat1 table that are part of a changeset or patchset.
11563*/
11564SQLITE_API int sqlite3session_attach(
11565 sqlite3_session *pSession, /* Session object */
11566 const char *zTab /* Table name */
11567);
11568
11569/*
11570** CAPI3REF: Set a table filter on a Session Object.
11571** METHOD: sqlite3_session
11572**
11573** The second argument (xFilter) is the "filter callback". For changes to rows
11574** in tables that are not attached to the Session object, the filter is called
11575** to determine whether changes to the table's rows should be tracked or not.
11576** If xFilter returns 0, changes are not tracked. Note that once a table is
11577** attached, xFilter will not be called again.
11578*/
11579SQLITE_API void sqlite3session_table_filter(
11580 sqlite3_session *pSession, /* Session object */
11581 int(*xFilter)(
11582 void *pCtx, /* Copy of third arg to _filter_table() */
11583 const char *zTab /* Table name */
11584 ),
11585 void *pCtx /* First argument passed to xFilter */
11586);
11587
11588/*
11589** CAPI3REF: Generate A Changeset From A Session Object
11590** METHOD: sqlite3_session
11591**
11592** Obtain a changeset containing changes to the tables attached to the
11593** session object passed as the first argument. If successful,
11594** set *ppChangeset to point to a buffer containing the changeset
11595** and *pnChangeset to the size of the changeset in bytes before returning
11596** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
11597** zero and return an SQLite error code.
11598**
11599** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
11600** each representing a change to a single row of an attached table. An INSERT
11601** change contains the values of each field of a new database row. A DELETE
11602** contains the original values of each field of a deleted database row. An
11603** UPDATE change contains the original values of each field of an updated
11604** database row along with the updated values for each updated non-primary-key
11605** column. It is not possible for an UPDATE change to represent a change that
11606** modifies the values of primary key columns. If such a change is made, it
11607** is represented in a changeset as a DELETE followed by an INSERT.
11608**
11609** Changes are not recorded for rows that have NULL values stored in one or
11610** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
11611** no corresponding change is present in the changesets returned by this
11612** function. If an existing row with one or more NULL values stored in
11613** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11614** only an INSERT is appears in the changeset. Similarly, if an existing row
11615** with non-NULL PRIMARY KEY values is updated so that one or more of its
11616** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
11617** DELETE change only.
11618**
11619** The contents of a changeset may be traversed using an iterator created
11620** using the [sqlite3changeset_start()] API. A changeset may be applied to
11621** a database with a compatible schema using the [sqlite3changeset_apply()]
11622** API.
11623**
11624** Within a changeset generated by this function, all changes related to a
11625** single table are grouped together. In other words, when iterating through
11626** a changeset or when applying a changeset to a database, all changes related
11627** to a single table are processed before moving on to the next table. Tables
11628** are sorted in the same order in which they were attached (or auto-attached)
11629** to the sqlite3_session object. The order in which the changes related to
11630** a single table are stored is undefined.
11631**
11632** Following a successful call to this function, it is the responsibility of
11633** the caller to eventually free the buffer that *ppChangeset points to using
11634** [sqlite3_free()].
11635**
11636** <h3>Changeset Generation</h3>
11637**
11638** Once a table has been attached to a session object, the session object
11639** records the primary key values of all new rows inserted into the table.
11640** It also records the original primary key and other column values of any
11641** deleted or updated rows. For each unique primary key value, data is only
11642** recorded once - the first time a row with said primary key is inserted,
11643** updated or deleted in the lifetime of the session.
11644**
11645** There is one exception to the previous paragraph: when a row is inserted,
11646** updated or deleted, if one or more of its primary key columns contain a
11647** NULL value, no record of the change is made.
11648**
11649** The session object therefore accumulates two types of records - those
11650** that consist of primary key values only (created when the user inserts
11651** a new record) and those that consist of the primary key values and the
11652** original values of other table columns (created when the users deletes
11653** or updates a record).
11654**
11655** When this function is called, the requested changeset is created using
11656** both the accumulated records and the current contents of the database
11657** file. Specifically:
11658**
11659** <ul>
11660** <li> For each record generated by an insert, the database is queried
11661** for a row with a matching primary key. If one is found, an INSERT
11662** change is added to the changeset. If no such row is found, no change
11663** is added to the changeset.
11664**
11665** <li> For each record generated by an update or delete, the database is
11666** queried for a row with a matching primary key. If such a row is
11667** found and one or more of the non-primary key fields have been
11668** modified from their original values, an UPDATE change is added to
11669** the changeset. Or, if no such row is found in the table, a DELETE
11670** change is added to the changeset. If there is a row with a matching
11671** primary key in the database, but all fields contain their original
11672** values, no change is added to the changeset.
11673** </ul>
11674**
11675** This means, amongst other things, that if a row is inserted and then later
11676** deleted while a session object is active, neither the insert nor the delete
11677** will be present in the changeset. Or if a row is deleted and then later a
11678** row with the same primary key values inserted while a session object is
11679** active, the resulting changeset will contain an UPDATE change instead of
11680** a DELETE and an INSERT.
11681**
11682** When a session object is disabled (see the [sqlite3session_enable()] API),
11683** it does not accumulate records when rows are inserted, updated or deleted.
11684** This may appear to have some counter-intuitive effects if a single row
11685** is written to more than once during a session. For example, if a row
11686** is inserted while a session object is enabled, then later deleted while
11687** the same session object is disabled, no INSERT record will appear in the
11688** changeset, even though the delete took place while the session was disabled.
11689** Or, if one field of a row is updated while a session is disabled, and
11690** another field of the same row is updated while the session is enabled, the
11691** resulting changeset will contain an UPDATE change that updates both fields.
11692*/
11693SQLITE_API int sqlite3session_changeset(
11694 sqlite3_session *pSession, /* Session object */
11695 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
11696 void **ppChangeset /* OUT: Buffer containing changeset */
11697);
11698
11699/*
11700** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11701** METHOD: sqlite3_session
11702**
11703** By default, this function always returns 0. For it to return
11704** a useful result, the sqlite3_session object must have been configured
11705** to enable this API using sqlite3session_object_config() with the
11706** SQLITE_SESSION_OBJCONFIG_SIZE verb.
11707**
11708** When enabled, this function returns an upper limit, in bytes, for the size
11709** of the changeset that might be produced if sqlite3session_changeset() were
11710** called. The final changeset size might be equal to or smaller than the
11711** size in bytes returned by this function.
11712*/
11713SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession);
11714
11715/*
11716** CAPI3REF: Load The Difference Between Tables Into A Session
11717** METHOD: sqlite3_session
11718**
11719** If it is not already attached to the session object passed as the first
11720** argument, this function attaches table zTbl in the same manner as the
11721** [sqlite3session_attach()] function. If zTbl does not exist, or if it
11722** does not have a primary key, this function is a no-op (but does not return
11723** an error).
11724**
11725** Argument zFromDb must be the name of a database ("main", "temp" etc.)
11726** attached to the same database handle as the session object that contains
11727** a table compatible with the table attached to the session by this function.
11728** A table is considered compatible if it:
11729**
11730** <ul>
11731** <li> Has the same name,
11732** <li> Has the same set of columns declared in the same order, and
11733** <li> Has the same PRIMARY KEY definition.
11734** </ul>
11735**
11736** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
11737** are compatible but do not have any PRIMARY KEY columns, it is not an error
11738** but no changes are added to the session object. As with other session
11739** APIs, tables without PRIMARY KEYs are simply ignored.
11740**
11741** This function adds a set of changes to the session object that could be
11742** used to update the table in database zFrom (call this the "from-table")
11743** so that its content is the same as the table attached to the session
11744** object (call this the "to-table"). Specifically:
11745**
11746** <ul>
11747** <li> For each row (primary key) that exists in the to-table but not in
11748** the from-table, an INSERT record is added to the session object.
11749**
11750** <li> For each row (primary key) that exists in the to-table but not in
11751** the from-table, a DELETE record is added to the session object.
11752**
11753** <li> For each row (primary key) that exists in both tables, but features
11754** different non-PK values in each, an UPDATE record is added to the
11755** session.
11756** </ul>
11757**
11758** To clarify, if this function is called and then a changeset constructed
11759** using [sqlite3session_changeset()], then after applying that changeset to
11760** database zFrom the contents of the two compatible tables would be
11761** identical.
11762**
11763** It an error if database zFrom does not exist or does not contain the
11764** required compatible table.
11765**
11766** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11767** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
11768** may be set to point to a buffer containing an English language error
11769** message. It is the responsibility of the caller to free this buffer using
11770** sqlite3_free().
11771*/
11772SQLITE_API int sqlite3session_diff(
11773 sqlite3_session *pSession,
11774 const char *zFromDb,
11775 const char *zTbl,
11776 char **pzErrMsg
11777);
11778
11779
11780/*
11781** CAPI3REF: Generate A Patchset From A Session Object
11782** METHOD: sqlite3_session
11783**
11784** The differences between a patchset and a changeset are that:
11785**
11786** <ul>
11787** <li> DELETE records consist of the primary key fields only. The
11788** original values of other fields are omitted.
11789** <li> The original values of any modified fields are omitted from
11790** UPDATE records.
11791** </ul>
11792**
11793** A patchset blob may be used with up to date versions of all
11794** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11795** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11796** attempting to use a patchset blob with old versions of the
11797** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11798**
11799** Because the non-primary key "old.*" fields are omitted, no
11800** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11801** is passed to the sqlite3changeset_apply() API. Other conflict types work
11802** in the same way as for changesets.
11803**
11804** Changes within a patchset are ordered in the same way as for changesets
11805** generated by the sqlite3session_changeset() function (i.e. all changes for
11806** a single table are grouped together, tables appear in the order in which
11807** they were attached to the session object).
11808*/
11809SQLITE_API int sqlite3session_patchset(
11810 sqlite3_session *pSession, /* Session object */
11811 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
11812 void **ppPatchset /* OUT: Buffer containing patchset */
11813);
11814
11815/*
11816** CAPI3REF: Test if a changeset has recorded any changes.
11817**
11818** Return non-zero if no changes to attached tables have been recorded by
11819** the session object passed as the first argument. Otherwise, if one or
11820** more changes have been recorded, return zero.
11821**
11822** Even if this function returns zero, it is possible that calling
11823** [sqlite3session_changeset()] on the session handle may still return a
11824** changeset that contains no changes. This can happen when a row in
11825** an attached table is modified and then later on the original values
11826** are restored. However, if this function returns non-zero, then it is
11827** guaranteed that a call to sqlite3session_changeset() will return a
11828** changeset containing zero changes.
11829*/
11830SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11831
11832/*
11833** CAPI3REF: Query for the amount of heap memory used by a session object.
11834**
11835** This API returns the total amount of heap memory in bytes currently
11836** used by the session object passed as the only argument.
11837*/
11838SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
11839
11840/*
11841** CAPI3REF: Create An Iterator To Traverse A Changeset
11842** CONSTRUCTOR: sqlite3_changeset_iter
11843**
11844** Create an iterator used to iterate through the contents of a changeset.
11845** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11846** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11847** SQLite error code is returned.
11848**
11849** The following functions can be used to advance and query a changeset
11850** iterator created by this function:
11851**
11852** <ul>
11853** <li> [sqlite3changeset_next()]
11854** <li> [sqlite3changeset_op()]
11855** <li> [sqlite3changeset_new()]
11856** <li> [sqlite3changeset_old()]
11857** </ul>
11858**
11859** It is the responsibility of the caller to eventually destroy the iterator
11860** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11861** changeset (pChangeset) must remain valid until after the iterator is
11862** destroyed.
11863**
11864** Assuming the changeset blob was created by one of the
11865** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11866** [sqlite3changeset_invert()] functions, all changes within the changeset
11867** that apply to a single table are grouped together. This means that when
11868** an application iterates through a changeset using an iterator created by
11869** this function, all changes that relate to a single table are visited
11870** consecutively. There is no chance that the iterator will visit a change
11871** the applies to table X, then one for table Y, and then later on visit
11872** another change for table X.
11873**
11874** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11875** may be modified by passing a combination of
11876** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11877**
11878** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11879** and therefore subject to change.
11880*/
11881SQLITE_API int sqlite3changeset_start(
11882 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11883 int nChangeset, /* Size of changeset blob in bytes */
11884 void *pChangeset /* Pointer to blob containing changeset */
11885);
11886SQLITE_API int sqlite3changeset_start_v2(
11887 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11888 int nChangeset, /* Size of changeset blob in bytes */
11889 void *pChangeset, /* Pointer to blob containing changeset */
11890 int flags /* SESSION_CHANGESETSTART_* flags */
11891);
11892
11893/*
11894** CAPI3REF: Flags for sqlite3changeset_start_v2
11895**
11896** The following flags may passed via the 4th parameter to
11897** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11898**
11899** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11900** Invert the changeset while iterating through it. This is equivalent to
11901** inverting a changeset using sqlite3changeset_invert() before applying it.
11902** It is an error to specify this flag with a patchset.
11903*/
11904#define SQLITE_CHANGESETSTART_INVERT0x0002 0x0002
11905
11906
11907/*
11908** CAPI3REF: Advance A Changeset Iterator
11909** METHOD: sqlite3_changeset_iter
11910**
11911** This function may only be used with iterators created by the function
11912** [sqlite3changeset_start()]. If it is called on an iterator passed to
11913** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11914** is returned and the call has no effect.
11915**
11916** Immediately after an iterator is created by sqlite3changeset_start(), it
11917** does not point to any change in the changeset. Assuming the changeset
11918** is not empty, the first call to this function advances the iterator to
11919** point to the first change in the changeset. Each subsequent call advances
11920** the iterator to point to the next change in the changeset (if any). If
11921** no error occurs and the iterator points to a valid change after a call
11922** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11923** Otherwise, if all changes in the changeset have already been visited,
11924** SQLITE_DONE is returned.
11925**
11926** If an error occurs, an SQLite error code is returned. Possible error
11927** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11928** SQLITE_NOMEM.
11929*/
11930SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11931
11932/*
11933** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11934** METHOD: sqlite3_changeset_iter
11935**
11936** The pIter argument passed to this function may either be an iterator
11937** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11938** created by [sqlite3changeset_start()]. In the latter case, the most recent
11939** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11940** is not the case, this function returns [SQLITE_MISUSE].
11941**
11942** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
11943** outputs are set through these pointers:
11944**
11945** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
11946** depending on the type of change that the iterator currently points to;
11947**
11948** *pnCol is set to the number of columns in the table affected by the change; and
11949**
11950** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11951** the name of the table affected by the current change. The buffer remains
11952** valid until either sqlite3changeset_next() is called on the iterator
11953** or until the conflict-handler function returns.
11954**
11955** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11956** is an indirect change, or false (0) otherwise. See the documentation for
11957** [sqlite3session_indirect()] for a description of direct and indirect
11958** changes.
11959**
11960** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11961** SQLite error code is returned. The values of the output variables may not
11962** be trusted in this case.
11963*/
11964SQLITE_API int sqlite3changeset_op(
11965 sqlite3_changeset_iter *pIter, /* Iterator object */
11966 const char **pzTab, /* OUT: Pointer to table name */
11967 int *pnCol, /* OUT: Number of columns in table */
11968 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11969 int *pbIndirect /* OUT: True for an 'indirect' change */
11970);
11971
11972/*
11973** CAPI3REF: Obtain The Primary Key Definition Of A Table
11974** METHOD: sqlite3_changeset_iter
11975**
11976** For each modified table, a changeset includes the following:
11977**
11978** <ul>
11979** <li> The number of columns in the table, and
11980** <li> Which of those columns make up the tables PRIMARY KEY.
11981** </ul>
11982**
11983** This function is used to find which columns comprise the PRIMARY KEY of
11984** the table modified by the change that iterator pIter currently points to.
11985** If successful, *pabPK is set to point to an array of nCol entries, where
11986** nCol is the number of columns in the table. Elements of *pabPK are set to
11987** 0x01 if the corresponding column is part of the tables primary key, or
11988** 0x00 if it is not.
11989**
11990** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11991** in the table.
11992**
11993** If this function is called when the iterator does not point to a valid
11994** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11995** SQLITE_OK is returned and the output variables populated as described
11996** above.
11997*/
11998SQLITE_API int sqlite3changeset_pk(
11999 sqlite3_changeset_iter *pIter, /* Iterator object */
12000 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
12001 int *pnCol /* OUT: Number of entries in output array */
12002);
12003
12004/*
12005** CAPI3REF: Obtain old.* Values From A Changeset Iterator
12006** METHOD: sqlite3_changeset_iter
12007**
12008** The pIter argument passed to this function may either be an iterator
12009** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12010** created by [sqlite3changeset_start()]. In the latter case, the most recent
12011** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
12012** Furthermore, it may only be called if the type of change that the iterator
12013** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
12014** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
12015**
12016** Argument iVal must be greater than or equal to 0, and less than the number
12017** of columns in the table affected by the current change. Otherwise,
12018** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
12019**
12020** If successful, this function sets *ppValue to point to a protected
12021** sqlite3_value object containing the iVal'th value from the vector of
12022** original row values stored as part of the UPDATE or DELETE change and
12023** returns SQLITE_OK. The name of the function comes from the fact that this
12024** is similar to the "old.*" columns available to update or delete triggers.
12025**
12026** If some other error occurs (e.g. an OOM condition), an SQLite error code
12027** is returned and *ppValue is set to NULL.
12028*/
12029SQLITE_API int sqlite3changeset_old(
12030 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12031 int iVal, /* Column number */
12032 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
12033);
12034
12035/*
12036** CAPI3REF: Obtain new.* Values From A Changeset Iterator
12037** METHOD: sqlite3_changeset_iter
12038**
12039** The pIter argument passed to this function may either be an iterator
12040** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12041** created by [sqlite3changeset_start()]. In the latter case, the most recent
12042** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
12043** Furthermore, it may only be called if the type of change that the iterator
12044** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
12045** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
12046**
12047** Argument iVal must be greater than or equal to 0, and less than the number
12048** of columns in the table affected by the current change. Otherwise,
12049** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
12050**
12051** If successful, this function sets *ppValue to point to a protected
12052** sqlite3_value object containing the iVal'th value from the vector of
12053** new row values stored as part of the UPDATE or INSERT change and
12054** returns SQLITE_OK. If the change is an UPDATE and does not include
12055** a new value for the requested column, *ppValue is set to NULL and
12056** SQLITE_OK returned. The name of the function comes from the fact that
12057** this is similar to the "new.*" columns available to update or delete
12058** triggers.
12059**
12060** If some other error occurs (e.g. an OOM condition), an SQLite error code
12061** is returned and *ppValue is set to NULL.
12062*/
12063SQLITE_API int sqlite3changeset_new(
12064 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12065 int iVal, /* Column number */
12066 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
12067);
12068
12069/*
12070** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
12071** METHOD: sqlite3_changeset_iter
12072**
12073** This function should only be used with iterator objects passed to a
12074** conflict-handler callback by [sqlite3changeset_apply()] with either
12075** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
12076** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
12077** is set to NULL.
12078**
12079** Argument iVal must be greater than or equal to 0, and less than the number
12080** of columns in the table affected by the current change. Otherwise,
12081** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
12082**
12083** If successful, this function sets *ppValue to point to a protected
12084** sqlite3_value object containing the iVal'th value from the
12085** "conflicting row" associated with the current conflict-handler callback
12086** and returns SQLITE_OK.
12087**
12088** If some other error occurs (e.g. an OOM condition), an SQLite error code
12089** is returned and *ppValue is set to NULL.
12090*/
12091SQLITE_API int sqlite3changeset_conflict(
12092 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12093 int iVal, /* Column number */
12094 sqlite3_value **ppValue /* OUT: Value from conflicting row */
12095);
12096
12097/*
12098** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
12099** METHOD: sqlite3_changeset_iter
12100**
12101** This function may only be called with an iterator passed to an
12102** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
12103** it sets the output variable to the total number of known foreign key
12104** violations in the destination database and returns SQLITE_OK.
12105**
12106** In all other cases this function returns SQLITE_MISUSE.
12107*/
12108SQLITE_API int sqlite3changeset_fk_conflicts(
12109 sqlite3_changeset_iter *pIter, /* Changeset iterator */
12110 int *pnOut /* OUT: Number of FK violations */
12111);
12112
12113
12114/*
12115** CAPI3REF: Finalize A Changeset Iterator
12116** METHOD: sqlite3_changeset_iter
12117**
12118** This function is used to finalize an iterator allocated with
12119** [sqlite3changeset_start()].
12120**
12121** This function should only be called on iterators created using the
12122** [sqlite3changeset_start()] function. If an application calls this
12123** function with an iterator passed to a conflict-handler by
12124** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
12125** call has no effect.
12126**
12127** If an error was encountered within a call to an sqlite3changeset_xxx()
12128** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
12129** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
12130** to that error is returned by this function. Otherwise, SQLITE_OK is
12131** returned. This is to allow the following pattern (pseudo-code):
12132**
12133** <pre>
12134** sqlite3changeset_start();
12135** while( SQLITE_ROW==sqlite3changeset_next() ){
12136** // Do something with change.
12137** }
12138** rc = sqlite3changeset_finalize();
12139** if( rc!=SQLITE_OK ){
12140** // An error has occurred
12141** }
12142** </pre>
12143*/
12144SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
12145
12146/*
12147** CAPI3REF: Invert A Changeset
12148**
12149** This function is used to "invert" a changeset object. Applying an inverted
12150** changeset to a database reverses the effects of applying the uninverted
12151** changeset. Specifically:
12152**
12153** <ul>
12154** <li> Each DELETE change is changed to an INSERT, and
12155** <li> Each INSERT change is changed to a DELETE, and
12156** <li> For each UPDATE change, the old.* and new.* values are exchanged.
12157** </ul>
12158**
12159** This function does not change the order in which changes appear within
12160** the changeset. It merely reverses the sense of each individual change.
12161**
12162** If successful, a pointer to a buffer containing the inverted changeset
12163** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
12164** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
12165** zeroed and an SQLite error code returned.
12166**
12167** It is the responsibility of the caller to eventually call sqlite3_free()
12168** on the *ppOut pointer to free the buffer allocation following a successful
12169** call to this function.
12170**
12171** WARNING/TODO: This function currently assumes that the input is a valid
12172** changeset. If it is not, the results are undefined.
12173*/
12174SQLITE_API int sqlite3changeset_invert(
12175 int nIn, const void *pIn, /* Input changeset */
12176 int *pnOut, void **ppOut /* OUT: Inverse of input */
12177);
12178
12179/*
12180** CAPI3REF: Concatenate Two Changeset Objects
12181**
12182** This function is used to concatenate two changesets, A and B, into a
12183** single changeset. The result is a changeset equivalent to applying
12184** changeset A followed by changeset B.
12185**
12186** This function combines the two input changesets using an
12187** sqlite3_changegroup object. Calling it produces similar results as the
12188** following code fragment:
12189**
12190** <pre>
12191** sqlite3_changegroup *pGrp;
12192** rc = sqlite3_changegroup_new(&pGrp);
12193** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
12194** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
12195** if( rc==SQLITE_OK ){
12196** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
12197** }else{
12198** *ppOut = 0;
12199** *pnOut = 0;
12200** }
12201** </pre>
12202**
12203** Refer to the sqlite3_changegroup documentation below for details.
12204*/
12205SQLITE_API int sqlite3changeset_concat(
12206 int nA, /* Number of bytes in buffer pA */
12207 void *pA, /* Pointer to buffer containing changeset A */
12208 int nB, /* Number of bytes in buffer pB */
12209 void *pB, /* Pointer to buffer containing changeset B */
12210 int *pnOut, /* OUT: Number of bytes in output changeset */
12211 void **ppOut /* OUT: Buffer containing output changeset */
12212);
12213
12214
12215/*
12216** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
12217*/
12218SQLITE_API int sqlite3changeset_upgrade(
12219 sqlite3 *db,
12220 const char *zDb,
12221 int nIn, const void *pIn, /* Input changeset */
12222 int *pnOut, void **ppOut /* OUT: Inverse of input */
12223);
12224
12225
12226
12227/*
12228** CAPI3REF: Changegroup Handle
12229**
12230** A changegroup is an object used to combine two or more
12231** [changesets] or [patchsets]
12232*/
12233typedef struct sqlite3_changegroup sqlite3_changegroup;
12234
12235/*
12236** CAPI3REF: Create A New Changegroup Object
12237** CONSTRUCTOR: sqlite3_changegroup
12238**
12239** An sqlite3_changegroup object is used to combine two or more changesets
12240** (or patchsets) into a single changeset (or patchset). A single changegroup
12241** object may combine changesets or patchsets, but not both. The output is
12242** always in the same format as the input.
12243**
12244** If successful, this function returns SQLITE_OK and populates (*pp) with
12245** a pointer to a new sqlite3_changegroup object before returning. The caller
12246** should eventually free the returned object using a call to
12247** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
12248** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
12249**
12250** The usual usage pattern for an sqlite3_changegroup object is as follows:
12251**
12252** <ul>
12253** <li> It is created using a call to sqlite3changegroup_new().
12254**
12255** <li> Zero or more changesets (or patchsets) are added to the object
12256** by calling sqlite3changegroup_add().
12257**
12258** <li> The result of combining all input changesets together is obtained
12259** by the application via a call to sqlite3changegroup_output().
12260**
12261** <li> The object is deleted using a call to sqlite3changegroup_delete().
12262** </ul>
12263**
12264** Any number of calls to add() and output() may be made between the calls to
12265** new() and delete(), and in any order.
12266**
12267** As well as the regular sqlite3changegroup_add() and
12268** sqlite3changegroup_output() functions, also available are the streaming
12269** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
12270*/
12271SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
12272
12273/*
12274** CAPI3REF: Add a Schema to a Changegroup
12275** METHOD: sqlite3_changegroup_schema
12276**
12277** This method may be used to optionally enforce the rule that the changesets
12278** added to the changegroup handle must match the schema of database zDb
12279** ("main", "temp", or the name of an attached database). If
12280** sqlite3changegroup_add() is called to add a changeset that is not compatible
12281** with the configured schema, SQLITE_SCHEMA is returned and the changegroup
12282** object is left in an undefined state.
12283**
12284** A changeset schema is considered compatible with the database schema in
12285** the same way as for sqlite3changeset_apply(). Specifically, for each
12286** table in the changeset, there exists a database table with:
12287**
12288** <ul>
12289** <li> The name identified by the changeset, and
12290** <li> at least as many columns as recorded in the changeset, and
12291** <li> the primary key columns in the same position as recorded in
12292** the changeset.
12293** </ul>
12294**
12295** The output of the changegroup object always has the same schema as the
12296** database nominated using this function. In cases where changesets passed
12297** to sqlite3changegroup_add() have fewer columns than the corresponding table
12298** in the database schema, these are filled in using the default column
12299** values from the database schema. This makes it possible to combined
12300** changesets that have different numbers of columns for a single table
12301** within a changegroup, provided that they are otherwise compatible.
12302*/
12303SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
12304
12305/*
12306** CAPI3REF: Add A Changeset To A Changegroup
12307** METHOD: sqlite3_changegroup
12308**
12309** Add all changes within the changeset (or patchset) in buffer pData (size
12310** nData bytes) to the changegroup.
12311**
12312** If the buffer contains a patchset, then all prior calls to this function
12313** on the same changegroup object must also have specified patchsets. Or, if
12314** the buffer contains a changeset, so must have the earlier calls to this
12315** function. Otherwise, SQLITE_ERROR is returned and no changes are added
12316** to the changegroup.
12317**
12318** Rows within the changeset and changegroup are identified by the values in
12319** their PRIMARY KEY columns. A change in the changeset is considered to
12320** apply to the same row as a change already present in the changegroup if
12321** the two rows have the same primary key.
12322**
12323** Changes to rows that do not already appear in the changegroup are
12324** simply copied into it. Or, if both the new changeset and the changegroup
12325** contain changes that apply to a single row, the final contents of the
12326** changegroup depends on the type of each change, as follows:
12327**
12328** <table border=1 style="margin-left:8ex;margin-right:8ex">
12329** <tr><th style="white-space:pre">Existing Change </th>
12330** <th style="white-space:pre">New Change </th>
12331** <th>Output Change
12332** <tr><td>INSERT <td>INSERT <td>
12333** The new change is ignored. This case does not occur if the new
12334** changeset was recorded immediately after the changesets already
12335** added to the changegroup.
12336** <tr><td>INSERT <td>UPDATE <td>
12337** The INSERT change remains in the changegroup. The values in the
12338** INSERT change are modified as if the row was inserted by the
12339** existing change and then updated according to the new change.
12340** <tr><td>INSERT <td>DELETE <td>
12341** The existing INSERT is removed from the changegroup. The DELETE is
12342** not added.
12343** <tr><td>UPDATE <td>INSERT <td>
12344** The new change is ignored. This case does not occur if the new
12345** changeset was recorded immediately after the changesets already
12346** added to the changegroup.
12347** <tr><td>UPDATE <td>UPDATE <td>
12348** The existing UPDATE remains within the changegroup. It is amended
12349** so that the accompanying values are as if the row was updated once
12350** by the existing change and then again by the new change.
12351** <tr><td>UPDATE <td>DELETE <td>
12352** The existing UPDATE is replaced by the new DELETE within the
12353** changegroup.
12354** <tr><td>DELETE <td>INSERT <td>
12355** If one or more of the column values in the row inserted by the
12356** new change differ from those in the row deleted by the existing
12357** change, the existing DELETE is replaced by an UPDATE within the
12358** changegroup. Otherwise, if the inserted row is exactly the same
12359** as the deleted row, the existing DELETE is simply discarded.
12360** <tr><td>DELETE <td>UPDATE <td>
12361** The new change is ignored. This case does not occur if the new
12362** changeset was recorded immediately after the changesets already
12363** added to the changegroup.
12364** <tr><td>DELETE <td>DELETE <td>
12365** The new change is ignored. This case does not occur if the new
12366** changeset was recorded immediately after the changesets already
12367** added to the changegroup.
12368** </table>
12369**
12370** If the new changeset contains changes to a table that is already present
12371** in the changegroup, then the number of columns and the position of the
12372** primary key columns for the table must be consistent. If this is not the
12373** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup
12374** object has been configured with a database schema using the
12375** sqlite3changegroup_schema() API, then it is possible to combine changesets
12376** with different numbers of columns for a single table, provided that
12377** they are otherwise compatible.
12378**
12379** If the input changeset appears to be corrupt and the corruption is
12380** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12381** occurs during processing, this function returns SQLITE_NOMEM.
12382**
12383** In all cases, if an error occurs the state of the final contents of the
12384** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12385*/
12386SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12387
12388/*
12389** CAPI3REF: Add A Single Change To A Changegroup
12390** METHOD: sqlite3_changegroup
12391**
12392** This function adds the single change currently indicated by the iterator
12393** passed as the second argument to the changegroup object. The rules for
12394** adding the change are just as described for [sqlite3changegroup_add()].
12395**
12396** If the change is successfully added to the changegroup, SQLITE_OK is
12397** returned. Otherwise, an SQLite error code is returned.
12398**
12399** The iterator must point to a valid entry when this function is called.
12400** If it does not, SQLITE_ERROR is returned and no change is added to the
12401** changegroup. Additionally, the iterator must not have been opened with
12402** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also
12403** returned.
12404*/
12405SQLITE_API int sqlite3changegroup_add_change(
12406 sqlite3_changegroup*,
12407 sqlite3_changeset_iter*
12408);
12409
12410
12411
12412/*
12413** CAPI3REF: Obtain A Composite Changeset From A Changegroup
12414** METHOD: sqlite3_changegroup
12415**
12416** Obtain a buffer containing a changeset (or patchset) representing the
12417** current contents of the changegroup. If the inputs to the changegroup
12418** were themselves changesets, the output is a changeset. Or, if the
12419** inputs were patchsets, the output is also a patchset.
12420**
12421** As with the output of the sqlite3session_changeset() and
12422** sqlite3session_patchset() functions, all changes related to a single
12423** table are grouped together in the output of this function. Tables appear
12424** in the same order as for the very first changeset added to the changegroup.
12425** If the second or subsequent changesets added to the changegroup contain
12426** changes for tables that do not appear in the first changeset, they are
12427** appended onto the end of the output changeset, again in the order in
12428** which they are first encountered.
12429**
12430** If an error occurs, an SQLite error code is returned and the output
12431** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
12432** is returned and the output variables are set to the size of and a
12433** pointer to the output buffer, respectively. In this case it is the
12434** responsibility of the caller to eventually free the buffer using a
12435** call to sqlite3_free().
12436*/
12437SQLITE_API int sqlite3changegroup_output(
12438 sqlite3_changegroup*,
12439 int *pnData, /* OUT: Size of output buffer in bytes */
12440 void **ppData /* OUT: Pointer to output buffer */
12441);
12442
12443/*
12444** CAPI3REF: Delete A Changegroup Object
12445** DESTRUCTOR: sqlite3_changegroup
12446*/
12447SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
12448
12449/*
12450** CAPI3REF: Apply A Changeset To A Database
12451**
12452** Apply a changeset or patchset to a database. These functions attempt to
12453** update the "main" database attached to handle db with the changes found in
12454** the changeset passed via the second and third arguments.
12455**
12456** The fourth argument (xFilter) passed to these functions is the "filter
12457** callback". If it is not NULL, then for each table affected by at least one
12458** change in the changeset, the filter callback is invoked with
12459** the table name as the second argument, and a copy of the context pointer
12460** passed as the sixth argument as the first. If the "filter callback"
12461** returns zero, then no attempt is made to apply any changes to the table.
12462** Otherwise, if the return value is non-zero or the xFilter argument to
12463** is NULL, all changes related to the table are attempted.
12464**
12465** For each table that is not excluded by the filter callback, this function
12466** tests that the target database contains a compatible table. A table is
12467** considered compatible if all of the following are true:
12468**
12469** <ul>
12470** <li> The table has the same name as the name recorded in the
12471** changeset, and
12472** <li> The table has at least as many columns as recorded in the
12473** changeset, and
12474** <li> The table has primary key columns in the same position as
12475** recorded in the changeset.
12476** </ul>
12477**
12478** If there is no compatible table, it is not an error, but none of the
12479** changes associated with the table are applied. A warning message is issued
12480** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
12481** one such warning is issued for each table in the changeset.
12482**
12483** For each change for which there is a compatible table, an attempt is made
12484** to modify the table contents according to the UPDATE, INSERT or DELETE
12485** change. If a change cannot be applied cleanly, the conflict handler
12486** function passed as the fifth argument to sqlite3changeset_apply() may be
12487** invoked. A description of exactly when the conflict handler is invoked for
12488** each type of change is below.
12489**
12490** Unlike the xFilter argument, xConflict may not be passed NULL. The results
12491** of passing anything other than a valid function pointer as the xConflict
12492** argument are undefined.
12493**
12494** Each time the conflict handler function is invoked, it must return one
12495** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
12496** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
12497** if the second argument passed to the conflict handler is either
12498** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12499** returns an illegal value, any changes already made are rolled back and
12500** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
12501** actions are taken by sqlite3changeset_apply() depending on the value
12502** returned by each invocation of the conflict-handler function. Refer to
12503** the documentation for the three
12504** [SQLITE_CHANGESET_OMIT|available return values] for details.
12505**
12506** <dl>
12507** <dt>DELETE Changes<dd>
12508** For each DELETE change, the function checks if the target database
12509** contains a row with the same primary key value (or values) as the
12510** original row values stored in the changeset. If it does, and the values
12511** stored in all non-primary key columns also match the values stored in
12512** the changeset the row is deleted from the target database.
12513**
12514** If a row with matching primary key values is found, but one or more of
12515** the non-primary key fields contains a value different from the original
12516** row value stored in the changeset, the conflict-handler function is
12517** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
12518** database table has more columns than are recorded in the changeset,
12519** only the values of those non-primary key fields are compared against
12520** the current database contents - any trailing database table columns
12521** are ignored.
12522**
12523** If no row with matching primary key values is found in the database,
12524** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12525** passed as the second argument.
12526**
12527** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
12528** (which can only happen if a foreign key constraint is violated), the
12529** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12530** passed as the second argument. This includes the case where the DELETE
12531** operation is attempted because an earlier call to the conflict handler
12532** function returned [SQLITE_CHANGESET_REPLACE].
12533**
12534** <dt>INSERT Changes<dd>
12535** For each INSERT change, an attempt is made to insert the new row into
12536** the database. If the changeset row contains fewer fields than the
12537** database table, the trailing fields are populated with their default
12538** values.
12539**
12540** If the attempt to insert the row fails because the database already
12541** contains a row with the same primary key values, the conflict handler
12542** function is invoked with the second argument set to
12543** [SQLITE_CHANGESET_CONFLICT].
12544**
12545** If the attempt to insert the row fails because of some other constraint
12546** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
12547** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
12548** This includes the case where the INSERT operation is re-attempted because
12549** an earlier call to the conflict handler function returned
12550** [SQLITE_CHANGESET_REPLACE].
12551**
12552** <dt>UPDATE Changes<dd>
12553** For each UPDATE change, the function checks if the target database
12554** contains a row with the same primary key value (or values) as the
12555** original row values stored in the changeset. If it does, and the values
12556** stored in all modified non-primary key columns also match the values
12557** stored in the changeset the row is updated within the target database.
12558**
12559** If a row with matching primary key values is found, but one or more of
12560** the modified non-primary key fields contains a value different from an
12561** original row value stored in the changeset, the conflict-handler function
12562** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
12563** UPDATE changes only contain values for non-primary key fields that are
12564** to be modified, only those fields need to match the original values to
12565** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12566**
12567** If no row with matching primary key values is found in the database,
12568** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12569** passed as the second argument.
12570**
12571** If the UPDATE operation is attempted, but SQLite returns
12572** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12573** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
12574** This includes the case where the UPDATE operation is attempted after
12575** an earlier call to the conflict handler function returned
12576** [SQLITE_CHANGESET_REPLACE].
12577** </dl>
12578**
12579** It is safe to execute SQL statements, including those that write to the
12580** table that the callback related to, from within the xConflict callback.
12581** This can be used to further customize the application's conflict
12582** resolution strategy.
12583**
12584** All changes made by these functions are enclosed in a savepoint transaction.
12585** If any other error (aside from a constraint failure when attempting to
12586** write to the target database) occurs, then the savepoint transaction is
12587** rolled back, restoring the target database to its original state, and an
12588** SQLite error code returned.
12589**
12590** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12591** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
12592** may set (*ppRebase) to point to a "rebase" that may be used with the
12593** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
12594** is set to the size of the buffer in bytes. It is the responsibility of the
12595** caller to eventually free any such buffer using sqlite3_free(). The buffer
12596** is only allocated and populated if one or more conflicts were encountered
12597** while applying the patchset. See comments surrounding the sqlite3_rebaser
12598** APIs for further details.
12599**
12600** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
12601** may be modified by passing a combination of
12602** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
12603**
12604** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
12605** and therefore subject to change.
12606*/
12607SQLITE_API int sqlite3changeset_apply(
12608 sqlite3 *db, /* Apply change to "main" db of this handle */
12609 int nChangeset, /* Size of changeset in bytes */
12610 void *pChangeset, /* Changeset blob */
12611 int(*xFilter)(
12612 void *pCtx, /* Copy of sixth arg to _apply() */
12613 const char *zTab /* Table name */
12614 ),
12615 int(*xConflict)(
12616 void *pCtx, /* Copy of sixth arg to _apply() */
12617 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12618 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12619 ),
12620 void *pCtx /* First argument passed to xConflict */
12621);
12622SQLITE_API int sqlite3changeset_apply_v2(
12623 sqlite3 *db, /* Apply change to "main" db of this handle */
12624 int nChangeset, /* Size of changeset in bytes */
12625 void *pChangeset, /* Changeset blob */
12626 int(*xFilter)(
12627 void *pCtx, /* Copy of sixth arg to _apply() */
12628 const char *zTab /* Table name */
12629 ),
12630 int(*xConflict)(
12631 void *pCtx, /* Copy of sixth arg to _apply() */
12632 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12633 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12634 ),
12635 void *pCtx, /* First argument passed to xConflict */
12636 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
12637 int flags /* SESSION_CHANGESETAPPLY_* flags */
12638);
12639
12640/*
12641** CAPI3REF: Flags for sqlite3changeset_apply_v2
12642**
12643** The following flags may passed via the 9th parameter to
12644** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
12645**
12646** <dl>
12647** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
12648** Usually, the sessions module encloses all operations performed by
12649** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
12650** SAVEPOINT is committed if the changeset or patchset is successfully
12651** applied, or rolled back if an error occurs. Specifying this flag
12652** causes the sessions module to omit this savepoint. In this case, if the
12653** caller has an open transaction or savepoint when apply_v2() is called,
12654** it may revert the partially applied changeset by rolling it back.
12655**
12656** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12657** Invert the changeset before applying it. This is equivalent to inverting
12658** a changeset using sqlite3changeset_invert() before applying it. It is
12659** an error to specify this flag with a patchset.
12660**
12661** <dt>SQLITE_CHANGESETAPPLY_IGNORENOOP <dd>
12662** Do not invoke the conflict handler callback for any changes that
12663** would not actually modify the database even if they were applied.
12664** Specifically, this means that the conflict handler is not invoked
12665** for:
12666** <ul>
12667** <li>a delete change if the row being deleted cannot be found,
12668** <li>an update change if the modified fields are already set to
12669** their new values in the conflicting row, or
12670** <li>an insert change if all fields of the conflicting row match
12671** the row being inserted.
12672** </ul>
12673**
12674** <dt>SQLITE_CHANGESETAPPLY_FKNOACTION <dd>
12675** If this flag it set, then all foreign key constraints in the target
12676** database behave as if they were declared with "ON UPDATE NO ACTION ON
12677** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
12678** or SET DEFAULT.
12679*/
12680#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001 0x0001
12681#define SQLITE_CHANGESETAPPLY_INVERT0x0002 0x0002
12682#define SQLITE_CHANGESETAPPLY_IGNORENOOP0x0004 0x0004
12683#define SQLITE_CHANGESETAPPLY_FKNOACTION0x0008 0x0008
12684
12685/*
12686** CAPI3REF: Constants Passed To The Conflict Handler
12687**
12688** Values that may be passed as the second argument to a conflict-handler.
12689**
12690** <dl>
12691** <dt>SQLITE_CHANGESET_DATA<dd>
12692** The conflict handler is invoked with CHANGESET_DATA as the second argument
12693** when processing a DELETE or UPDATE change if a row with the required
12694** PRIMARY KEY fields is present in the database, but one or more other
12695** (non primary-key) fields modified by the update do not contain the
12696** expected "before" values.
12697**
12698** The conflicting row, in this case, is the database row with the matching
12699** primary key.
12700**
12701** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
12702** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
12703** argument when processing a DELETE or UPDATE change if a row with the
12704** required PRIMARY KEY fields is not present in the database.
12705**
12706** There is no conflicting row in this case. The results of invoking the
12707** sqlite3changeset_conflict() API are undefined.
12708**
12709** <dt>SQLITE_CHANGESET_CONFLICT<dd>
12710** CHANGESET_CONFLICT is passed as the second argument to the conflict
12711** handler while processing an INSERT change if the operation would result
12712** in duplicate primary key values.
12713**
12714** The conflicting row in this case is the database row with the matching
12715** primary key.
12716**
12717** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
12718** If foreign key handling is enabled, and applying a changeset leaves the
12719** database in a state containing foreign key violations, the conflict
12720** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
12721** exactly once before the changeset is committed. If the conflict handler
12722** returns CHANGESET_OMIT, the changes, including those that caused the
12723** foreign key constraint violation, are committed. Or, if it returns
12724** CHANGESET_ABORT, the changeset is rolled back.
12725**
12726** No current or conflicting row information is provided. The only function
12727** it is possible to call on the supplied sqlite3_changeset_iter handle
12728** is sqlite3changeset_fk_conflicts().
12729**
12730** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
12731** If any other constraint violation occurs while applying a change (i.e.
12732** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
12733** invoked with CHANGESET_CONSTRAINT as the second argument.
12734**
12735** There is no conflicting row in this case. The results of invoking the
12736** sqlite3changeset_conflict() API are undefined.
12737**
12738** </dl>
12739*/
12740#define SQLITE_CHANGESET_DATA1 1
12741#define SQLITE_CHANGESET_NOTFOUND2 2
12742#define SQLITE_CHANGESET_CONFLICT3 3
12743#define SQLITE_CHANGESET_CONSTRAINT4 4
12744#define SQLITE_CHANGESET_FOREIGN_KEY5 5
12745
12746/*
12747** CAPI3REF: Constants Returned By The Conflict Handler
12748**
12749** A conflict handler callback must return one of the following three values.
12750**
12751** <dl>
12752** <dt>SQLITE_CHANGESET_OMIT<dd>
12753** If a conflict handler returns this value no special action is taken. The
12754** change that caused the conflict is not applied. The session module
12755** continues to the next change in the changeset.
12756**
12757** <dt>SQLITE_CHANGESET_REPLACE<dd>
12758** This value may only be returned if the second argument to the conflict
12759** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
12760** is not the case, any changes applied so far are rolled back and the
12761** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
12762**
12763** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
12764** handler, then the conflicting row is either updated or deleted, depending
12765** on the type of change.
12766**
12767** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
12768** handler, then the conflicting row is removed from the database and a
12769** second attempt to apply the change is made. If this second attempt fails,
12770** the original row is restored to the database before continuing.
12771**
12772** <dt>SQLITE_CHANGESET_ABORT<dd>
12773** If this value is returned, any changes applied so far are rolled back
12774** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
12775** </dl>
12776*/
12777#define SQLITE_CHANGESET_OMIT0 0
12778#define SQLITE_CHANGESET_REPLACE1 1
12779#define SQLITE_CHANGESET_ABORT2 2
12780
12781/*
12782** CAPI3REF: Rebasing changesets
12783** EXPERIMENTAL
12784**
12785** Suppose there is a site hosting a database in state S0. And that
12786** modifications are made that move that database to state S1 and a
12787** changeset recorded (the "local" changeset). Then, a changeset based
12788** on S0 is received from another site (the "remote" changeset) and
12789** applied to the database. The database is then in state
12790** (S1+"remote"), where the exact state depends on any conflict
12791** resolution decisions (OMIT or REPLACE) made while applying "remote".
12792** Rebasing a changeset is to update it to take those conflict
12793** resolution decisions into account, so that the same conflicts
12794** do not have to be resolved elsewhere in the network.
12795**
12796** For example, if both the local and remote changesets contain an
12797** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
12798**
12799** local: INSERT INTO t1 VALUES(1, 'v1');
12800** remote: INSERT INTO t1 VALUES(1, 'v2');
12801**
12802** and the conflict resolution is REPLACE, then the INSERT change is
12803** removed from the local changeset (it was overridden). Or, if the
12804** conflict resolution was "OMIT", then the local changeset is modified
12805** to instead contain:
12806**
12807** UPDATE t1 SET b = 'v2' WHERE a=1;
12808**
12809** Changes within the local changeset are rebased as follows:
12810**
12811** <dl>
12812** <dt>Local INSERT<dd>
12813** This may only conflict with a remote INSERT. If the conflict
12814** resolution was OMIT, then add an UPDATE change to the rebased
12815** changeset. Or, if the conflict resolution was REPLACE, add
12816** nothing to the rebased changeset.
12817**
12818** <dt>Local DELETE<dd>
12819** This may conflict with a remote UPDATE or DELETE. In both cases the
12820** only possible resolution is OMIT. If the remote operation was a
12821** DELETE, then add no change to the rebased changeset. If the remote
12822** operation was an UPDATE, then the old.* fields of change are updated
12823** to reflect the new.* values in the UPDATE.
12824**
12825** <dt>Local UPDATE<dd>
12826** This may conflict with a remote UPDATE or DELETE. If it conflicts
12827** with a DELETE, and the conflict resolution was OMIT, then the update
12828** is changed into an INSERT. Any undefined values in the new.* record
12829** from the update change are filled in using the old.* values from
12830** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
12831** the UPDATE change is simply omitted from the rebased changeset.
12832**
12833** If conflict is with a remote UPDATE and the resolution is OMIT, then
12834** the old.* values are rebased using the new.* values in the remote
12835** change. Or, if the resolution is REPLACE, then the change is copied
12836** into the rebased changeset with updates to columns also updated by
12837** the conflicting remote UPDATE removed. If this means no columns would
12838** be updated, the change is omitted.
12839** </dl>
12840**
12841** A local change may be rebased against multiple remote changes
12842** simultaneously. If a single key is modified by multiple remote
12843** changesets, they are combined as follows before the local changeset
12844** is rebased:
12845**
12846** <ul>
12847** <li> If there has been one or more REPLACE resolutions on a
12848** key, it is rebased according to a REPLACE.
12849**
12850** <li> If there have been no REPLACE resolutions on a key, then
12851** the local changeset is rebased according to the most recent
12852** of the OMIT resolutions.
12853** </ul>
12854**
12855** Note that conflict resolutions from multiple remote changesets are
12856** combined on a per-field basis, not per-row. This means that in the
12857** case of multiple remote UPDATE operations, some fields of a single
12858** local change may be rebased for REPLACE while others are rebased for
12859** OMIT.
12860**
12861** In order to rebase a local changeset, the remote changeset must first
12862** be applied to the local database using sqlite3changeset_apply_v2() and
12863** the buffer of rebase information captured. Then:
12864**
12865** <ol>
12866** <li> An sqlite3_rebaser object is created by calling
12867** sqlite3rebaser_create().
12868** <li> The new object is configured with the rebase buffer obtained from
12869** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
12870** If the local changeset is to be rebased against multiple remote
12871** changesets, then sqlite3rebaser_configure() should be called
12872** multiple times, in the same order that the multiple
12873** sqlite3changeset_apply_v2() calls were made.
12874** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
12875** <li> The sqlite3_rebaser object is deleted by calling
12876** sqlite3rebaser_delete().
12877** </ol>
12878*/
12879typedef struct sqlite3_rebaser sqlite3_rebaser;
12880
12881/*
12882** CAPI3REF: Create a changeset rebaser object.
12883** EXPERIMENTAL
12884**
12885** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
12886** point to the new object and return SQLITE_OK. Otherwise, if an error
12887** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
12888** to NULL.
12889*/
12890SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
12891
12892/*
12893** CAPI3REF: Configure a changeset rebaser object.
12894** EXPERIMENTAL
12895**
12896** Configure the changeset rebaser object to rebase changesets according
12897** to the conflict resolutions described by buffer pRebase (size nRebase
12898** bytes), which must have been obtained from a previous call to
12899** sqlite3changeset_apply_v2().
12900*/
12901SQLITE_API int sqlite3rebaser_configure(
12902 sqlite3_rebaser*,
12903 int nRebase, const void *pRebase
12904);
12905
12906/*
12907** CAPI3REF: Rebase a changeset
12908** EXPERIMENTAL
12909**
12910** Argument pIn must point to a buffer containing a changeset nIn bytes
12911** in size. This function allocates and populates a buffer with a copy
12912** of the changeset rebased according to the configuration of the
12913** rebaser object passed as the first argument. If successful, (*ppOut)
12914** is set to point to the new buffer containing the rebased changeset and
12915** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
12916** responsibility of the caller to eventually free the new buffer using
12917** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
12918** are set to zero and an SQLite error code returned.
12919*/
12920SQLITE_API int sqlite3rebaser_rebase(
12921 sqlite3_rebaser*,
12922 int nIn, const void *pIn,
12923 int *pnOut, void **ppOut
12924);
12925
12926/*
12927** CAPI3REF: Delete a changeset rebaser object.
12928** EXPERIMENTAL
12929**
12930** Delete the changeset rebaser object and all associated resources. There
12931** should be one call to this function for each successful invocation
12932** of sqlite3rebaser_create().
12933*/
12934SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
12935
12936/*
12937** CAPI3REF: Streaming Versions of API functions.
12938**
12939** The six streaming API xxx_strm() functions serve similar purposes to the
12940** corresponding non-streaming API functions:
12941**
12942** <table border=1 style="margin-left:8ex;margin-right:8ex">
12943** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12944** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12945** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12946** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12947** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12948** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12949** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12950** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12951** </table>
12952**
12953** Non-streaming functions that accept changesets (or patchsets) as input
12954** require that the entire changeset be stored in a single buffer in memory.
12955** Similarly, those that return a changeset or patchset do so by returning
12956** a pointer to a single large buffer allocated using sqlite3_malloc().
12957** Normally this is convenient. However, if an application running in a
12958** low-memory environment is required to handle very large changesets, the
12959** large contiguous memory allocations required can become onerous.
12960**
12961** In order to avoid this problem, instead of a single large buffer, input
12962** is passed to a streaming API functions by way of a callback function that
12963** the sessions module invokes to incrementally request input data as it is
12964** required. In all cases, a pair of API function parameters such as
12965**
12966** <pre>
12967** &nbsp; int nChangeset,
12968** &nbsp; void *pChangeset,
12969** </pre>
12970**
12971** Is replaced by:
12972**
12973** <pre>
12974** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12975** &nbsp; void *pIn,
12976** </pre>
12977**
12978** Each time the xInput callback is invoked by the sessions module, the first
12979** argument passed is a copy of the supplied pIn context pointer. The second
12980** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12981** error occurs the xInput method should copy up to (*pnData) bytes of data
12982** into the buffer and set (*pnData) to the actual number of bytes copied
12983** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12984** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12985** error code should be returned. In all cases, if an xInput callback returns
12986** an error, all processing is abandoned and the streaming API function
12987** returns a copy of the error code to the caller.
12988**
12989** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12990** invoked by the sessions module at any point during the lifetime of the
12991** iterator. If such an xInput callback returns an error, the iterator enters
12992** an error state, whereby all subsequent calls to iterator functions
12993** immediately fail with the same error code as returned by xInput.
12994**
12995** Similarly, streaming API functions that return changesets (or patchsets)
12996** return them in chunks by way of a callback function instead of via a
12997** pointer to a single large buffer. In this case, a pair of parameters such
12998** as:
12999**
13000** <pre>
13001** &nbsp; int *pnChangeset,
13002** &nbsp; void **ppChangeset,
13003** </pre>
13004**
13005** Is replaced by:
13006**
13007** <pre>
13008** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
13009** &nbsp; void *pOut
13010** </pre>
13011**
13012** The xOutput callback is invoked zero or more times to return data to
13013** the application. The first parameter passed to each call is a copy of the
13014** pOut pointer supplied by the application. The second parameter, pData,
13015** points to a buffer nData bytes in size containing the chunk of output
13016** data being returned. If the xOutput callback successfully processes the
13017** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
13018** it should return some other SQLite error code. In this case processing
13019** is immediately abandoned and the streaming API function returns a copy
13020** of the xOutput error code to the application.
13021**
13022** The sessions module never invokes an xOutput callback with the third
13023** parameter set to a value less than or equal to zero. Other than this,
13024** no guarantees are made as to the size of the chunks of data returned.
13025*/
13026SQLITE_API int sqlite3changeset_apply_strm(
13027 sqlite3 *db, /* Apply change to "main" db of this handle */
13028 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
13029 void *pIn, /* First arg for xInput */
13030 int(*xFilter)(
13031 void *pCtx, /* Copy of sixth arg to _apply() */
13032 const char *zTab /* Table name */
13033 ),
13034 int(*xConflict)(
13035 void *pCtx, /* Copy of sixth arg to _apply() */
13036 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
13037 sqlite3_changeset_iter *p /* Handle describing change and conflict */
13038 ),
13039 void *pCtx /* First argument passed to xConflict */
13040);
13041SQLITE_API int sqlite3changeset_apply_v2_strm(
13042 sqlite3 *db, /* Apply change to "main" db of this handle */
13043 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
13044 void *pIn, /* First arg for xInput */
13045 int(*xFilter)(
13046 void *pCtx, /* Copy of sixth arg to _apply() */
13047 const char *zTab /* Table name */
13048 ),
13049 int(*xConflict)(
13050 void *pCtx, /* Copy of sixth arg to _apply() */
13051 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
13052 sqlite3_changeset_iter *p /* Handle describing change and conflict */
13053 ),
13054 void *pCtx, /* First argument passed to xConflict */
13055 void **ppRebase, int *pnRebase,
13056 int flags
13057);
13058SQLITE_API int sqlite3changeset_concat_strm(
13059 int (*xInputA)(void *pIn, void *pData, int *pnData),
13060 void *pInA,
13061 int (*xInputB)(void *pIn, void *pData, int *pnData),
13062 void *pInB,
13063 int (*xOutput)(void *pOut, const void *pData, int nData),
13064 void *pOut
13065);
13066SQLITE_API int sqlite3changeset_invert_strm(
13067 int (*xInput)(void *pIn, void *pData, int *pnData),
13068 void *pIn,
13069 int (*xOutput)(void *pOut, const void *pData, int nData),
13070 void *pOut
13071);
13072SQLITE_API int sqlite3changeset_start_strm(
13073 sqlite3_changeset_iter **pp,
13074 int (*xInput)(void *pIn, void *pData, int *pnData),
13075 void *pIn
13076);
13077SQLITE_API int sqlite3changeset_start_v2_strm(
13078 sqlite3_changeset_iter **pp,
13079 int (*xInput)(void *pIn, void *pData, int *pnData),
13080 void *pIn,
13081 int flags
13082);
13083SQLITE_API int sqlite3session_changeset_strm(
13084 sqlite3_session *pSession,
13085 int (*xOutput)(void *pOut, const void *pData, int nData),
13086 void *pOut
13087);
13088SQLITE_API int sqlite3session_patchset_strm(
13089 sqlite3_session *pSession,
13090 int (*xOutput)(void *pOut, const void *pData, int nData),
13091 void *pOut
13092);
13093SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
13094 int (*xInput)(void *pIn, void *pData, int *pnData),
13095 void *pIn
13096);
13097SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
13098 int (*xOutput)(void *pOut, const void *pData, int nData),
13099 void *pOut
13100);
13101SQLITE_API int sqlite3rebaser_rebase_strm(
13102 sqlite3_rebaser *pRebaser,
13103 int (*xInput)(void *pIn, void *pData, int *pnData),
13104 void *pIn,
13105 int (*xOutput)(void *pOut, const void *pData, int nData),
13106 void *pOut
13107);
13108
13109/*
13110** CAPI3REF: Configure global parameters
13111**
13112** The sqlite3session_config() interface is used to make global configuration
13113** changes to the sessions module in order to tune it to the specific needs
13114** of the application.
13115**
13116** The sqlite3session_config() interface is not threadsafe. If it is invoked
13117** while any other thread is inside any other sessions method then the
13118** results are undefined. Furthermore, if it is invoked after any sessions
13119** related objects have been created, the results are also undefined.
13120**
13121** The first argument to the sqlite3session_config() function must be one
13122** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
13123** interpretation of the (void*) value passed as the second parameter and
13124** the effect of calling this function depends on the value of the first
13125** parameter.
13126**
13127** <dl>
13128** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
13129** By default, the sessions module streaming interfaces attempt to input
13130** and output data in approximately 1 KiB chunks. This operand may be used
13131** to set and query the value of this configuration setting. The pointer
13132** passed as the second argument must point to a value of type (int).
13133** If this value is greater than 0, it is used as the new streaming data
13134** chunk size for both input and output. Before returning, the (int) value
13135** pointed to by pArg is set to the final value of the streaming interface
13136** chunk size.
13137** </dl>
13138**
13139** This function returns SQLITE_OK if successful, or an SQLite error code
13140** otherwise.
13141*/
13142SQLITE_API int sqlite3session_config(int op, void *pArg);
13143
13144/*
13145** CAPI3REF: Values for sqlite3session_config().
13146*/
13147#define SQLITE_SESSION_CONFIG_STRMSIZE1 1
13148
13149/*
13150** Make sure we can call this stuff from C++.
13151*/
13152#if 0
13153}
13154#endif
13155
13156#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
13157
13158/******** End of sqlite3session.h *********/
13159/******** Begin file fts5.h *********/
13160/*
13161** 2014 May 31
13162**
13163** The author disclaims copyright to this source code. In place of
13164** a legal notice, here is a blessing:
13165**
13166** May you do good and not evil.
13167** May you find forgiveness for yourself and forgive others.
13168** May you share freely, never taking more than you give.
13169**
13170******************************************************************************
13171**
13172** Interfaces to extend FTS5. Using the interfaces defined in this file,
13173** FTS5 may be extended with:
13174**
13175** * custom tokenizers, and
13176** * custom auxiliary functions.
13177*/
13178
13179
13180#ifndef _FTS5_H
13181#define _FTS5_H
13182
13183
13184#if 0
13185extern "C" {
13186#endif
13187
13188/*************************************************************************
13189** CUSTOM AUXILIARY FUNCTIONS
13190**
13191** Virtual table implementations may overload SQL functions by implementing
13192** the sqlite3_module.xFindFunction() method.
13193*/
13194
13195typedef struct Fts5ExtensionApi Fts5ExtensionApi;
13196typedef struct Fts5Context Fts5Context;
13197typedef struct Fts5PhraseIter Fts5PhraseIter;
13198
13199typedef void (*fts5_extension_function)(
13200 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
13201 Fts5Context *pFts, /* First arg to pass to pApi functions */
13202 sqlite3_context *pCtx, /* Context for returning result/error */
13203 int nVal, /* Number of values in apVal[] array */
13204 sqlite3_value **apVal /* Array of trailing arguments */
13205);
13206
13207struct Fts5PhraseIter {
13208 const unsigned char *a;
13209 const unsigned char *b;
13210};
13211
13212/*
13213** EXTENSION API FUNCTIONS
13214**
13215** xUserData(pFts):
13216** Return a copy of the pUserData pointer passed to the xCreateFunction()
13217** API when the extension function was registered.
13218**
13219** xColumnTotalSize(pFts, iCol, pnToken):
13220** If parameter iCol is less than zero, set output variable *pnToken
13221** to the total number of tokens in the FTS5 table. Or, if iCol is
13222** non-negative but less than the number of columns in the table, return
13223** the total number of tokens in column iCol, considering all rows in
13224** the FTS5 table.
13225**
13226** If parameter iCol is greater than or equal to the number of columns
13227** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
13228** an OOM condition or IO error), an appropriate SQLite error code is
13229** returned.
13230**
13231** xColumnCount(pFts):
13232** Return the number of columns in the table.
13233**
13234** xColumnSize(pFts, iCol, pnToken):
13235** If parameter iCol is less than zero, set output variable *pnToken
13236** to the total number of tokens in the current row. Or, if iCol is
13237** non-negative but less than the number of columns in the table, set
13238** *pnToken to the number of tokens in column iCol of the current row.
13239**
13240** If parameter iCol is greater than or equal to the number of columns
13241** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
13242** an OOM condition or IO error), an appropriate SQLite error code is
13243** returned.
13244**
13245** This function may be quite inefficient if used with an FTS5 table
13246** created with the "columnsize=0" option.
13247**
13248** xColumnText:
13249** If parameter iCol is less than zero, or greater than or equal to the
13250** number of columns in the table, SQLITE_RANGE is returned.
13251**
13252** Otherwise, this function attempts to retrieve the text of column iCol of
13253** the current document. If successful, (*pz) is set to point to a buffer
13254** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13255** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
13256** if an error occurs, an SQLite error code is returned and the final values
13257** of (*pz) and (*pn) are undefined.
13258**
13259** xPhraseCount:
13260** Returns the number of phrases in the current query expression.
13261**
13262** xPhraseSize:
13263** If parameter iCol is less than zero, or greater than or equal to the
13264** number of phrases in the current query, as returned by xPhraseCount,
13265** 0 is returned. Otherwise, this function returns the number of tokens in
13266** phrase iPhrase of the query. Phrases are numbered starting from zero.
13267**
13268** xInstCount:
13269** Set *pnInst to the total number of occurrences of all phrases within
13270** the query within the current row. Return SQLITE_OK if successful, or
13271** an error code (i.e. SQLITE_NOMEM) if an error occurs.
13272**
13273** This API can be quite slow if used with an FTS5 table created with the
13274** "detail=none" or "detail=column" option. If the FTS5 table is created
13275** with either "detail=none" or "detail=column" and "content=" option
13276** (i.e. if it is a contentless table), then this API always returns 0.
13277**
13278** xInst:
13279** Query for the details of phrase match iIdx within the current row.
13280** Phrase matches are numbered starting from zero, so the iIdx argument
13281** should be greater than or equal to zero and smaller than the value
13282** output by xInstCount(). If iIdx is less than zero or greater than
13283** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
13284**
13285** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
13286** to the column in which it occurs and *piOff the token offset of the
13287** first token of the phrase. SQLITE_OK is returned if successful, or an
13288** error code (i.e. SQLITE_NOMEM) if an error occurs.
13289**
13290** This API can be quite slow if used with an FTS5 table created with the
13291** "detail=none" or "detail=column" option.
13292**
13293** xRowid:
13294** Returns the rowid of the current row.
13295**
13296** xTokenize:
13297** Tokenize text using the tokenizer belonging to the FTS5 table.
13298**
13299** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
13300** This API function is used to query the FTS table for phrase iPhrase
13301** of the current query. Specifically, a query equivalent to:
13302**
13303** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
13304**
13305** with $p set to a phrase equivalent to the phrase iPhrase of the
13306** current query is executed. Any column filter that applies to
13307** phrase iPhrase of the current query is included in $p. For each
13308** row visited, the callback function passed as the fourth argument
13309** is invoked. The context and API objects passed to the callback
13310** function may be used to access the properties of each matched row.
13311** Invoking Api.xUserData() returns a copy of the pointer passed as
13312** the third argument to pUserData.
13313**
13314** If parameter iPhrase is less than zero, or greater than or equal to
13315** the number of phrases in the query, as returned by xPhraseCount(),
13316** this function returns SQLITE_RANGE.
13317**
13318** If the callback function returns any value other than SQLITE_OK, the
13319** query is abandoned and the xQueryPhrase function returns immediately.
13320** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
13321** Otherwise, the error code is propagated upwards.
13322**
13323** If the query runs to completion without incident, SQLITE_OK is returned.
13324** Or, if some error occurs before the query completes or is aborted by
13325** the callback, an SQLite error code is returned.
13326**
13327**
13328** xSetAuxdata(pFts5, pAux, xDelete)
13329**
13330** Save the pointer passed as the second argument as the extension function's
13331** "auxiliary data". The pointer may then be retrieved by the current or any
13332** future invocation of the same fts5 extension function made as part of
13333** the same MATCH query using the xGetAuxdata() API.
13334**
13335** Each extension function is allocated a single auxiliary data slot for
13336** each FTS query (MATCH expression). If the extension function is invoked
13337** more than once for a single FTS query, then all invocations share a
13338** single auxiliary data context.
13339**
13340** If there is already an auxiliary data pointer when this function is
13341** invoked, then it is replaced by the new pointer. If an xDelete callback
13342** was specified along with the original pointer, it is invoked at this
13343** point.
13344**
13345** The xDelete callback, if one is specified, is also invoked on the
13346** auxiliary data pointer after the FTS5 query has finished.
13347**
13348** If an error (e.g. an OOM condition) occurs within this function,
13349** the auxiliary data is set to NULL and an error code returned. If the
13350** xDelete parameter was not NULL, it is invoked on the auxiliary data
13351** pointer before returning.
13352**
13353**
13354** xGetAuxdata(pFts5, bClear)
13355**
13356** Returns the current auxiliary data pointer for the fts5 extension
13357** function. See the xSetAuxdata() method for details.
13358**
13359** If the bClear argument is non-zero, then the auxiliary data is cleared
13360** (set to NULL) before this function returns. In this case the xDelete,
13361** if any, is not invoked.
13362**
13363**
13364** xRowCount(pFts5, pnRow)
13365**
13366** This function is used to retrieve the total number of rows in the table.
13367** In other words, the same value that would be returned by:
13368**
13369** SELECT count(*) FROM ftstable;
13370**
13371** xPhraseFirst()
13372** This function is used, along with type Fts5PhraseIter and the xPhraseNext
13373** method, to iterate through all instances of a single query phrase within
13374** the current row. This is the same information as is accessible via the
13375** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
13376** to use, this API may be faster under some circumstances. To iterate
13377** through instances of phrase iPhrase, use the following code:
13378**
13379** Fts5PhraseIter iter;
13380** int iCol, iOff;
13381** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13382** iCol>=0;
13383** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13384** ){
13385** // An instance of phrase iPhrase at offset iOff of column iCol
13386** }
13387**
13388** The Fts5PhraseIter structure is defined above. Applications should not
13389** modify this structure directly - it should only be used as shown above
13390** with the xPhraseFirst() and xPhraseNext() API methods (and by
13391** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
13392**
13393** This API can be quite slow if used with an FTS5 table created with the
13394** "detail=none" or "detail=column" option. If the FTS5 table is created
13395** with either "detail=none" or "detail=column" and "content=" option
13396** (i.e. if it is a contentless table), then this API always iterates
13397** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13398**
13399** In all cases, matches are visited in (column ASC, offset ASC) order.
13400** i.e. all those in column 0, sorted by offset, followed by those in
13401** column 1, etc.
13402**
13403** xPhraseNext()
13404** See xPhraseFirst above.
13405**
13406** xPhraseFirstColumn()
13407** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
13408** and xPhraseNext() APIs described above. The difference is that instead
13409** of iterating through all instances of a phrase in the current row, these
13410** APIs are used to iterate through the set of columns in the current row
13411** that contain one or more instances of a specified phrase. For example:
13412**
13413** Fts5PhraseIter iter;
13414** int iCol;
13415** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13416** iCol>=0;
13417** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13418** ){
13419** // Column iCol contains at least one instance of phrase iPhrase
13420** }
13421**
13422** This API can be quite slow if used with an FTS5 table created with the
13423** "detail=none" option. If the FTS5 table is created with either
13424** "detail=none" "content=" option (i.e. if it is a contentless table),
13425** then this API always iterates through an empty set (all calls to
13426** xPhraseFirstColumn() set iCol to -1).
13427**
13428** The information accessed using this API and its companion
13429** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
13430** (or xInst/xInstCount). The chief advantage of this API is that it is
13431** significantly more efficient than those alternatives when used with
13432** "detail=column" tables.
13433**
13434** xPhraseNextColumn()
13435** See xPhraseFirstColumn above.
13436**
13437** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13438** This is used to access token iToken of phrase iPhrase of the current
13439** query. Before returning, output parameter *ppToken is set to point
13440** to a buffer containing the requested token, and *pnToken to the
13441** size of this buffer in bytes.
13442**
13443** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13444** or equal to the number of phrases in the query as reported by
13445** xPhraseCount(), or if iToken is equal to or greater than the number of
13446** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13447 are both zeroed.
13448**
13449** The output text is not a copy of the query text that specified the
13450** token. It is the output of the tokenizer module. For tokendata=1
13451** tables, this includes any embedded 0x00 and trailing data.
13452**
13453** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13454** This is used to access token iToken of phrase hit iIdx within the
13455** current row. If iIdx is less than zero or greater than or equal to the
13456** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13457** output variable (*ppToken) is set to point to a buffer containing the
13458** matching document token, and (*pnToken) to the size of that buffer in
13459** bytes. This API is not available if the specified token matches a
13460** prefix query term. In that case both output variables are always set
13461** to 0.
13462**
13463** The output text is not a copy of the document text that was tokenized.
13464** It is the output of the tokenizer module. For tokendata=1 tables, this
13465** includes any embedded 0x00 and trailing data.
13466**
13467** This API can be quite slow if used with an FTS5 table created with the
13468** "detail=none" or "detail=column" option.
13469**
13470** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
13471** If parameter iCol is less than zero, or greater than or equal to the
13472** number of columns in the table, SQLITE_RANGE is returned.
13473**
13474** Otherwise, this function attempts to retrieve the locale associated
13475** with column iCol of the current row. Usually, there is no associated
13476** locale, and output parameters (*pzLocale) and (*pnLocale) are set
13477** to NULL and 0, respectively. However, if the fts5_locale() function
13478** was used to associate a locale with the value when it was inserted
13479** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated
13480** buffer containing the name of the locale in utf-8 encoding. (*pnLocale)
13481** is set to the size in bytes of the buffer, not including the
13482** nul-terminator.
13483**
13484** If successful, SQLITE_OK is returned. Or, if an error occurs, an
13485** SQLite error code is returned. The final value of the output parameters
13486** is undefined in this case.
13487**
13488** xTokenize_v2:
13489** Tokenize text using the tokenizer belonging to the FTS5 table. This
13490** API is the same as the xTokenize() API, except that it allows a tokenizer
13491** locale to be specified.
13492*/
13493struct Fts5ExtensionApi {
13494 int iVersion; /* Currently always set to 4 */
13495
13496 void *(*xUserData)(Fts5Context*);
13497
13498 int (*xColumnCount)(Fts5Context*);
13499 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
13500 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
13501
13502 int (*xTokenize)(Fts5Context*,
13503 const char *pText, int nText, /* Text to tokenize */
13504 void *pCtx, /* Context passed to xToken() */
13505 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
13506 );
13507
13508 int (*xPhraseCount)(Fts5Context*);
13509 int (*xPhraseSize)(Fts5Context*, int iPhrase);
13510
13511 int (*xInstCount)(Fts5Context*, int *pnInst);
13512 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
13513
13514 sqlite3_int64 (*xRowid)(Fts5Context*);
13515 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
13516 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
13517
13518 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
13519 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
13520 );
13521 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
13522 void *(*xGetAuxdata)(Fts5Context*, int bClear);
13523
13524 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
13525 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
13526
13527 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
13528 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
13529
13530 /* Below this point are iVersion>=3 only */
13531 int (*xQueryToken)(Fts5Context*,
13532 int iPhrase, int iToken,
13533 const char **ppToken, int *pnToken
13534 );
13535 int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
13536
13537 /* Below this point are iVersion>=4 only */
13538 int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn);
13539 int (*xTokenize_v2)(Fts5Context*,
13540 const char *pText, int nText, /* Text to tokenize */
13541 const char *pLocale, int nLocale, /* Locale to pass to tokenizer */
13542 void *pCtx, /* Context passed to xToken() */
13543 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
13544 );
13545};
13546
13547/*
13548** CUSTOM AUXILIARY FUNCTIONS
13549*************************************************************************/
13550
13551/*************************************************************************
13552** CUSTOM TOKENIZERS
13553**
13554** Applications may also register custom tokenizer types. A tokenizer
13555** is registered by providing fts5 with a populated instance of the
13556** following structure. All structure methods must be defined, setting
13557** any member of the fts5_tokenizer struct to NULL leads to undefined
13558** behaviour. The structure methods are expected to function as follows:
13559**
13560** xCreate:
13561** This function is used to allocate and initialize a tokenizer instance.
13562** A tokenizer instance is required to actually tokenize text.
13563**
13564** The first argument passed to this function is a copy of the (void*)
13565** pointer provided by the application when the fts5_tokenizer_v2 object
13566** was registered with FTS5 (the third argument to xCreateTokenizer()).
13567** The second and third arguments are an array of nul-terminated strings
13568** containing the tokenizer arguments, if any, specified following the
13569** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
13570** to create the FTS5 table.
13571**
13572** The final argument is an output variable. If successful, (*ppOut)
13573** should be set to point to the new tokenizer handle and SQLITE_OK
13574** returned. If an error occurs, some value other than SQLITE_OK should
13575** be returned. In this case, fts5 assumes that the final value of *ppOut
13576** is undefined.
13577**
13578** xDelete:
13579** This function is invoked to delete a tokenizer handle previously
13580** allocated using xCreate(). Fts5 guarantees that this function will
13581** be invoked exactly once for each successful call to xCreate().
13582**
13583** xTokenize:
13584** This function is expected to tokenize the nText byte string indicated
13585** by argument pText. pText may or may not be nul-terminated. The first
13586** argument passed to this function is a pointer to an Fts5Tokenizer object
13587** returned by an earlier call to xCreate().
13588**
13589** The third argument indicates the reason that FTS5 is requesting
13590** tokenization of the supplied text. This is always one of the following
13591** four values:
13592**
13593** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13594** or removed from the FTS table. The tokenizer is being invoked to
13595** determine the set of tokens to add to (or delete from) the
13596** FTS index.
13597**
13598** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13599** against the FTS index. The tokenizer is being called to tokenize
13600** a bareword or quoted string specified as part of the query.
13601**
13602** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13603** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
13604** followed by a "*" character, indicating that the last token
13605** returned by the tokenizer will be treated as a token prefix.
13606**
13607** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13608** satisfy an fts5_api.xTokenize() request made by an auxiliary
13609** function. Or an fts5_api.xColumnSize() request made by the same
13610** on a columnsize=0 database.
13611** </ul>
13612**
13613** The sixth and seventh arguments passed to xTokenize() - pLocale and
13614** nLocale - are a pointer to a buffer containing the locale to use for
13615** tokenization (e.g. "en_US") and its size in bytes, respectively. The
13616** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in
13617** which case nLocale is always 0) to indicate that the tokenizer should
13618** use its default locale.
13619**
13620** For each token in the input string, the supplied callback xToken() must
13621** be invoked. The first argument to it should be a copy of the pointer
13622** passed as the second argument to xTokenize(). The third and fourth
13623** arguments are a pointer to a buffer containing the token text, and the
13624** size of the token in bytes. The 4th and 5th arguments are the byte offsets
13625** of the first byte of and first byte immediately following the text from
13626** which the token is derived within the input.
13627**
13628** The second argument passed to the xToken() callback ("tflags") should
13629** normally be set to 0. The exception is if the tokenizer supports
13630** synonyms. In this case see the discussion below for details.
13631**
13632** FTS5 assumes the xToken() callback is invoked for each token in the
13633** order that they occur within the input text.
13634**
13635** If an xToken() callback returns any value other than SQLITE_OK, then
13636** the tokenization should be abandoned and the xTokenize() method should
13637** immediately return a copy of the xToken() return value. Or, if the
13638** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
13639** if an error occurs with the xTokenize() implementation itself, it
13640** may abandon the tokenization and return any error code other than
13641** SQLITE_OK or SQLITE_DONE.
13642**
13643** If the tokenizer is registered using an fts5_tokenizer_v2 object,
13644** then the xTokenize() method has two additional arguments - pLocale
13645** and nLocale. These specify the locale that the tokenizer should use
13646** for the current request. If pLocale and nLocale are both 0, then the
13647** tokenizer should use its default locale. Otherwise, pLocale points to
13648** an nLocale byte buffer containing the name of the locale to use as utf-8
13649** text. pLocale is not nul-terminated.
13650**
13651** FTS5_TOKENIZER
13652**
13653** There is also an fts5_tokenizer object. This is an older, deprecated,
13654** version of fts5_tokenizer_v2. It is similar except that:
13655**
13656** <ul>
13657** <li> There is no "iVersion" field, and
13658** <li> The xTokenize() method does not take a locale argument.
13659** </ul>
13660**
13661** Legacy fts5_tokenizer tokenizers must be registered using the
13662** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2().
13663**
13664** Tokenizer implementations registered using either API may be retrieved
13665** using both xFindTokenizer() and xFindTokenizer_v2().
13666**
13667** SYNONYM SUPPORT
13668**
13669** Custom tokenizers may also support synonyms. Consider a case in which a
13670** user wishes to query for a phrase such as "first place". Using the
13671** built-in tokenizers, the FTS5 query 'first + place' will match instances
13672** of "first place" within the document set, but not alternative forms
13673** such as "1st place". In some applications, it would be better to match
13674** all instances of "first place" or "1st place" regardless of which form
13675** the user specified in the MATCH query text.
13676**
13677** There are several ways to approach this in FTS5:
13678**
13679** <ol><li> By mapping all synonyms to a single token. In this case, using
13680** the above example, this means that the tokenizer returns the
13681** same token for inputs "first" and "1st". Say that token is in
13682** fact "first", so that when the user inserts the document "I won
13683** 1st place" entries are added to the index for tokens "i", "won",
13684** "first" and "place". If the user then queries for '1st + place',
13685** the tokenizer substitutes "first" for "1st" and the query works
13686** as expected.
13687**
13688** <li> By querying the index for all synonyms of each query term
13689** separately. In this case, when tokenizing query text, the
13690** tokenizer may provide multiple synonyms for a single term
13691** within the document. FTS5 then queries the index for each
13692** synonym individually. For example, faced with the query:
13693**
13694** <codeblock>
13695** ... MATCH 'first place'</codeblock>
13696**
13697** the tokenizer offers both "1st" and "first" as synonyms for the
13698** first token in the MATCH query and FTS5 effectively runs a query
13699** similar to:
13700**
13701** <codeblock>
13702** ... MATCH '(first OR 1st) place'</codeblock>
13703**
13704** except that, for the purposes of auxiliary functions, the query
13705** still appears to contain just two phrases - "(first OR 1st)"
13706** being treated as a single phrase.
13707**
13708** <li> By adding multiple synonyms for a single term to the FTS index.
13709** Using this method, when tokenizing document text, the tokenizer
13710** provides multiple synonyms for each token. So that when a
13711** document such as "I won first place" is tokenized, entries are
13712** added to the FTS index for "i", "won", "first", "1st" and
13713** "place".
13714**
13715** This way, even if the tokenizer does not provide synonyms
13716** when tokenizing query text (it should not - to do so would be
13717** inefficient), it doesn't matter if the user queries for
13718** 'first + place' or '1st + place', as there are entries in the
13719** FTS index corresponding to both forms of the first token.
13720** </ol>
13721**
13722** Whether it is parsing document or query text, any call to xToken that
13723** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
13724** is considered to supply a synonym for the previous token. For example,
13725** when parsing the document "I won first place", a tokenizer that supports
13726** synonyms would call xToken() 5 times, as follows:
13727**
13728** <codeblock>
13729** xToken(pCtx, 0, "i", 1, 0, 1);
13730** xToken(pCtx, 0, "won", 3, 2, 5);
13731** xToken(pCtx, 0, "first", 5, 6, 11);
13732** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
13733** xToken(pCtx, 0, "place", 5, 12, 17);
13734**</codeblock>
13735**
13736** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
13737** xToken() is called. Multiple synonyms may be specified for a single token
13738** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
13739** There is no limit to the number of synonyms that may be provided for a
13740** single token.
13741**
13742** In many cases, method (1) above is the best approach. It does not add
13743** extra data to the FTS index or require FTS5 to query for multiple terms,
13744** so it is efficient in terms of disk space and query speed. However, it
13745** does not support prefix queries very well. If, as suggested above, the
13746** token "first" is substituted for "1st" by the tokenizer, then the query:
13747**
13748** <codeblock>
13749** ... MATCH '1s*'</codeblock>
13750**
13751** will not match documents that contain the token "1st" (as the tokenizer
13752** will probably not map "1s" to any prefix of "first").
13753**
13754** For full prefix support, method (3) may be preferred. In this case,
13755** because the index contains entries for both "first" and "1st", prefix
13756** queries such as 'fi*' or '1s*' will match correctly. However, because
13757** extra entries are added to the FTS index, this method uses more space
13758** within the database.
13759**
13760** Method (2) offers a midpoint between (1) and (3). Using this method,
13761** a query such as '1s*' will match documents that contain the literal
13762** token "1st", but not "first" (assuming the tokenizer is not able to
13763** provide synonyms for prefixes). However, a non-prefix query like '1st'
13764** will match against "1st" and "first". This method does not require
13765** extra disk space, as no extra entries are added to the FTS index.
13766** On the other hand, it may require more CPU cycles to run MATCH queries,
13767** as separate queries of the FTS index are required for each synonym.
13768**
13769** When using methods (2) or (3), it is important that the tokenizer only
13770** provide synonyms when tokenizing document text (method (3)) or query
13771** text (method (2)), not both. Doing so will not cause any errors, but is
13772** inefficient.
13773*/
13774typedef struct Fts5Tokenizer Fts5Tokenizer;
13775typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2;
13776struct fts5_tokenizer_v2 {
13777 int iVersion; /* Currently always 2 */
13778
13779 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13780 void (*xDelete)(Fts5Tokenizer*);
13781 int (*xTokenize)(Fts5Tokenizer*,
13782 void *pCtx,
13783 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13784 const char *pText, int nText,
13785 const char *pLocale, int nLocale,
13786 int (*xToken)(
13787 void *pCtx, /* Copy of 2nd argument to xTokenize() */
13788 int tflags, /* Mask of FTS5_TOKEN_* flags */
13789 const char *pToken, /* Pointer to buffer containing token */
13790 int nToken, /* Size of token in bytes */
13791 int iStart, /* Byte offset of token within input text */
13792 int iEnd /* Byte offset of end of token within input text */
13793 )
13794 );
13795};
13796
13797/*
13798** New code should use the fts5_tokenizer_v2 type to define tokenizer
13799** implementations. The following type is included for legacy applications
13800** that still use it.
13801*/
13802typedef struct fts5_tokenizer fts5_tokenizer;
13803struct fts5_tokenizer {
13804 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
13805 void (*xDelete)(Fts5Tokenizer*);
13806 int (*xTokenize)(Fts5Tokenizer*,
13807 void *pCtx,
13808 int flags, /* Mask of FTS5_TOKENIZE_* flags */
13809 const char *pText, int nText,
13810 int (*xToken)(
13811 void *pCtx, /* Copy of 2nd argument to xTokenize() */
13812 int tflags, /* Mask of FTS5_TOKEN_* flags */
13813 const char *pToken, /* Pointer to buffer containing token */
13814 int nToken, /* Size of token in bytes */
13815 int iStart, /* Byte offset of token within input text */
13816 int iEnd /* Byte offset of end of token within input text */
13817 )
13818 );
13819};
13820
13821
13822/* Flags that may be passed as the third argument to xTokenize() */
13823#define FTS5_TOKENIZE_QUERY0x0001 0x0001
13824#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
13825#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
13826#define FTS5_TOKENIZE_AUX0x0008 0x0008
13827
13828/* Flags that may be passed by the tokenizer implementation back to FTS5
13829** as the third argument to the supplied xToken callback. */
13830#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
13831
13832/*
13833** END OF CUSTOM TOKENIZERS
13834*************************************************************************/
13835
13836/*************************************************************************
13837** FTS5 EXTENSION REGISTRATION API
13838*/
13839typedef struct fts5_api fts5_api;
13840struct fts5_api {
13841 int iVersion; /* Currently always set to 3 */
13842
13843 /* Create a new tokenizer */
13844 int (*xCreateTokenizer)(
13845 fts5_api *pApi,
13846 const char *zName,
13847 void *pUserData,
13848 fts5_tokenizer *pTokenizer,
13849 void (*xDestroy)(void*)
13850 );
13851
13852 /* Find an existing tokenizer */
13853 int (*xFindTokenizer)(
13854 fts5_api *pApi,
13855 const char *zName,
13856 void **ppUserData,
13857 fts5_tokenizer *pTokenizer
13858 );
13859
13860 /* Create a new auxiliary function */
13861 int (*xCreateFunction)(
13862 fts5_api *pApi,
13863 const char *zName,
13864 void *pUserData,
13865 fts5_extension_function xFunction,
13866 void (*xDestroy)(void*)
13867 );
13868
13869 /* APIs below this point are only available if iVersion>=3 */
13870
13871 /* Create a new tokenizer */
13872 int (*xCreateTokenizer_v2)(
13873 fts5_api *pApi,
13874 const char *zName,
13875 void *pUserData,
13876 fts5_tokenizer_v2 *pTokenizer,
13877 void (*xDestroy)(void*)
13878 );
13879
13880 /* Find an existing tokenizer */
13881 int (*xFindTokenizer_v2)(
13882 fts5_api *pApi,
13883 const char *zName,
13884 void **ppUserData,
13885 fts5_tokenizer_v2 **ppTokenizer
13886 );
13887};
13888
13889/*
13890** END OF REGISTRATION API
13891*************************************************************************/
13892
13893#if 0
13894} /* end of the 'extern "C"' block */
13895#endif
13896
13897#endif /* _FTS5_H */
13898
13899/******** End of fts5.h *********/
13900
13901/************** End of sqlite3.h *********************************************/
13902/************** Continuing where we left off in sqliteInt.h ******************/
13903
13904/*
13905** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
13906*/
13907#define SQLITE_MUTEX_STATIC_TEMPDIR11 SQLITE_MUTEX_STATIC_VFS111
13908
13909/*
13910** Include the configuration header output by 'configure' if we're using the
13911** autoconf-based build
13912*/
13913#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13914#include "sqlite_cfg.h"
13915#define SQLITECONFIG_H 1
13916#endif
13917
13918/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13919/************** Begin file sqliteLimit.h *************************************/
13920/*
13921** 2007 May 7
13922**
13923** The author disclaims copyright to this source code. In place of
13924** a legal notice, here is a blessing:
13925**
13926** May you do good and not evil.
13927** May you find forgiveness for yourself and forgive others.
13928** May you share freely, never taking more than you give.
13929**
13930*************************************************************************
13931**
13932** This file defines various limits of what SQLite can process.
13933*/
13934
13935/*
13936** The maximum length of a TEXT or BLOB in bytes. This also
13937** limits the size of a row in a table or index.
13938**
13939** The hard limit is the ability of a 32-bit signed integer
13940** to count the size: 2^31-1 or 2147483647.
13941*/
13942#ifndef SQLITE_MAX_LENGTH2147483645
13943# define SQLITE_MAX_LENGTH2147483645 1000000000
13944#endif
13945
13946/*
13947** This is the maximum number of
13948**
13949** * Columns in a table
13950** * Columns in an index
13951** * Columns in a view
13952** * Terms in the SET clause of an UPDATE statement
13953** * Terms in the result set of a SELECT statement
13954** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
13955** * Terms in the VALUES clause of an INSERT statement
13956**
13957** The hard upper limit here is 32676. Most database people will
13958** tell you that in a well-normalized database, you usually should
13959** not have more than a dozen or so columns in any table. And if
13960** that is the case, there is no point in having more than a few
13961** dozen values in any of the other situations described above.
13962*/
13963#ifndef SQLITE_MAX_COLUMN2000
13964# define SQLITE_MAX_COLUMN2000 2000
13965#endif
13966
13967/*
13968** The maximum length of a single SQL statement in bytes.
13969**
13970** It used to be the case that setting this value to zero would
13971** turn the limit off. That is no longer true. It is not possible
13972** to turn this limit off.
13973*/
13974#ifndef SQLITE_MAX_SQL_LENGTH1000000000
13975# define SQLITE_MAX_SQL_LENGTH1000000000 1000000000
13976#endif
13977
13978/*
13979** The maximum depth of an expression tree. This is limited to
13980** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
13981** want to place more severe limits on the complexity of an
13982** expression. A value of 0 means that there is no limit.
13983*/
13984#ifndef SQLITE_MAX_EXPR_DEPTH1000
13985# define SQLITE_MAX_EXPR_DEPTH1000 1000
13986#endif
13987
13988/*
13989** The maximum number of terms in a compound SELECT statement.
13990** The code generator for compound SELECT statements does one
13991** level of recursion for each term. A stack overflow can result
13992** if the number of terms is too large. In practice, most SQL
13993** never has more than 3 or 4 terms. Use a value of 0 to disable
13994** any limit on the number of terms in a compound SELECT.
13995*/
13996#ifndef SQLITE_MAX_COMPOUND_SELECT500
13997# define SQLITE_MAX_COMPOUND_SELECT500 500
13998#endif
13999
14000/*
14001** The maximum number of opcodes in a VDBE program.
14002** Not currently enforced.
14003*/
14004#ifndef SQLITE_MAX_VDBE_OP250000000
14005# define SQLITE_MAX_VDBE_OP250000000 250000000
14006#endif
14007
14008/*
14009** The maximum number of arguments to an SQL function.
14010*/
14011#ifndef SQLITE_MAX_FUNCTION_ARG127
14012# define SQLITE_MAX_FUNCTION_ARG127 127
14013#endif
14014
14015/*
14016** The suggested maximum number of in-memory pages to use for
14017** the main database table and for temporary tables.
14018**
14019** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
14020** which means the cache size is limited to 2048000 bytes of memory.
14021** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
14022** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
14023*/
14024#ifndef SQLITE_DEFAULT_CACHE_SIZE128
14025# define SQLITE_DEFAULT_CACHE_SIZE128 -2000
14026#endif
14027
14028/*
14029** The default number of frames to accumulate in the log file before
14030** checkpointing the database in WAL mode.
14031*/
14032#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
14033# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000 1000
14034#endif
14035
14036/*
14037** The maximum number of attached databases. This must be between 0
14038** and 125. The upper bound of 125 is because the attached databases are
14039** counted using a signed 8-bit integer which has a maximum value of 127
14040** and we have to allow 2 extra counts for the "main" and "temp" databases.
14041*/
14042#ifndef SQLITE_MAX_ATTACHED10
14043# define SQLITE_MAX_ATTACHED10 10
14044#endif
14045
14046
14047/*
14048** The maximum value of a ?nnn wildcard that the parser will accept.
14049** If the value exceeds 32767 then extra space is required for the Expr
14050** structure. But otherwise, we believe that the number can be as large
14051** as a signed 32-bit integer can hold.
14052*/
14053#ifndef SQLITE_MAX_VARIABLE_NUMBER500000
14054# define SQLITE_MAX_VARIABLE_NUMBER500000 32766
14055#endif
14056
14057/* Maximum page size. The upper bound on this value is 65536. This a limit
14058** imposed by the use of 16-bit offsets within each page.
14059**
14060** Earlier versions of SQLite allowed the user to change this value at
14061** compile time. This is no longer permitted, on the grounds that it creates
14062** a library that is technically incompatible with an SQLite library
14063** compiled with a different limit. If a process operating on a database
14064** with a page-size of 65536 bytes crashes, then an instance of SQLite
14065** compiled with the default page-size limit will not be able to rollback
14066** the aborted transaction. This could lead to database corruption.
14067*/
14068#ifdef SQLITE_MAX_PAGE_SIZE65536
14069# undef SQLITE_MAX_PAGE_SIZE65536
14070#endif
14071#define SQLITE_MAX_PAGE_SIZE65536 65536
14072
14073
14074/*
14075** The default size of a database page.
14076*/
14077#ifndef SQLITE_DEFAULT_PAGE_SIZE4096
14078# define SQLITE_DEFAULT_PAGE_SIZE4096 4096
14079#endif
14080#if SQLITE_DEFAULT_PAGE_SIZE4096>SQLITE_MAX_PAGE_SIZE65536
14081# undef SQLITE_DEFAULT_PAGE_SIZE4096
14082# define SQLITE_DEFAULT_PAGE_SIZE4096 SQLITE_MAX_PAGE_SIZE65536
14083#endif
14084
14085/*
14086** Ordinarily, if no value is explicitly provided, SQLite creates databases
14087** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
14088** device characteristics (sector-size and atomic write() support),
14089** SQLite may choose a larger value. This constant is the maximum value
14090** SQLite will choose on its own.
14091*/
14092#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
14093# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 8192
14094#endif
14095#if SQLITE_MAX_DEFAULT_PAGE_SIZE8192>SQLITE_MAX_PAGE_SIZE65536
14096# undef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
14097# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 SQLITE_MAX_PAGE_SIZE65536
14098#endif
14099
14100
14101/*
14102** Maximum number of pages in one database file.
14103**
14104** This is really just the default value for the max_page_count pragma.
14105** This value can be lowered (or raised) at run-time using that the
14106** max_page_count macro.
14107*/
14108#ifndef SQLITE_MAX_PAGE_COUNT0xfffffffe
14109# define SQLITE_MAX_PAGE_COUNT0xfffffffe 0xfffffffe /* 4294967294 */
14110#endif
14111
14112/*
14113** Maximum length (in bytes) of the pattern in a LIKE or GLOB
14114** operator.
14115*/
14116#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
14117# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
14118#endif
14119
14120/*
14121** Maximum depth of recursion for triggers.
14122**
14123** A value of 1 means that a trigger program will not be able to itself
14124** fire any triggers. A value of 0 means that no trigger programs at all
14125** may be executed.
14126*/
14127#ifndef SQLITE_MAX_TRIGGER_DEPTH1000
14128# define SQLITE_MAX_TRIGGER_DEPTH1000 1000
14129#endif
14130
14131/************** End of sqliteLimit.h *****************************************/
14132/************** Continuing where we left off in sqliteInt.h ******************/
14133
14134/* Disable nuisance warnings on Borland compilers */
14135#if defined(__BORLANDC__)
14136#pragma warn -rch /* unreachable code */
14137#pragma warn -ccc /* Condition is always true or false */
14138#pragma warn -aus /* Assigned value is never used */
14139#pragma warn -csu /* Comparing signed and unsigned */
14140#pragma warn -spa /* Suspicious pointer arithmetic */
14141#endif
14142
14143/*
14144** A few places in the code require atomic load/store of aligned
14145** integer values.
14146*/
14147#ifndef __has_extension
14148# define0 __has_extension(x)0 0 /* compatibility with non-clang compilers */
14149#endif
14150#if GCC_VERSION(4*1000000+2*1000+1)>=4007000 || __has_extension(c_atomic)1
14151# define SQLITE_ATOMIC_INTRINSICS1 1
14152# define AtomicLoad(PTR)__atomic_load_n((PTR),0) __atomic_load_n((PTR),__ATOMIC_RELAXED0)
14153# define AtomicStore(PTR,VAL)__atomic_store_n((PTR),(VAL),0) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED0)
14154#else
14155# define SQLITE_ATOMIC_INTRINSICS1 0
14156# define AtomicLoad(PTR)__atomic_load_n((PTR),0) (*(PTR))
14157# define AtomicStore(PTR,VAL)__atomic_store_n((PTR),(VAL),0) (*(PTR) = (VAL))
14158#endif
14159
14160/*
14161** Include standard header files as necessary
14162*/
14163#ifdef HAVE_STDINT_H
14164#include <stdint.h>
14165#endif
14166#ifdef HAVE_INTTYPES_H
14167#include <inttypes.h>
14168#endif
14169
14170/*
14171** The following macros are used to cast pointers to integers and
14172** integers to pointers. The way you do this varies from one compiler
14173** to the next, so we have developed the following set of #if statements
14174** to generate appropriate macros for a wide range of compilers.
14175**
14176** The correct "ANSI" way to do this is to use the intptr_t type.
14177** Unfortunately, that typedef is not available on all compilers, or
14178** if it is available, it requires an #include of specific headers
14179** that vary from one machine to the next.
14180**
14181** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
14182** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
14183** So we have to define the macros in different ways depending on the
14184** compiler.
14185*/
14186#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
14187# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(intptr_t)(X))
14188# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(intptr_t)(X))
14189#elif defined(__PTRDIFF_TYPE__long int) /* This case should work for GCC */
14190# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(__PTRDIFF_TYPE__long int)(X))
14191# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(__PTRDIFF_TYPE__long int)(X))
14192#elif !defined(__GNUC__4) /* Works for compilers other than LLVM */
14193# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)&((char*)0)[X])
14194# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(((char*)X)-(char*)0))
14195#else /* Generates a warning - but it always works */
14196# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(X))
14197# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(X))
14198#endif
14199
14200/*
14201** Macros to hint to the compiler that a function should or should not be
14202** inlined.
14203*/
14204#if defined(__GNUC__4)
14205# define SQLITE_NOINLINE__attribute__((noinline)) __attribute__((noinline))
14206# define SQLITE_INLINE__attribute__((always_inline)) inline __attribute__((always_inline)) inline
14207#elif defined(_MSC_VER) && _MSC_VER>=1310
14208# define SQLITE_NOINLINE__attribute__((noinline)) __declspec(noinline)
14209# define SQLITE_INLINE__attribute__((always_inline)) inline __forceinline
14210#else
14211# define SQLITE_NOINLINE__attribute__((noinline))
14212# define SQLITE_INLINE__attribute__((always_inline)) inline
14213#endif
14214#if defined(SQLITE_COVERAGE_TEST) || defined(__STRICT_ANSI__)
14215# undef SQLITE_INLINE__attribute__((always_inline)) inline
14216# define SQLITE_INLINE__attribute__((always_inline)) inline
14217#endif
14218
14219/*
14220** Make sure that the compiler intrinsics we desire are enabled when
14221** compiling with an appropriate version of MSVC unless prevented by
14222** the SQLITE_DISABLE_INTRINSIC define.
14223*/
14224#if !defined(SQLITE_DISABLE_INTRINSIC)
14225# if defined(_MSC_VER) && _MSC_VER>=1400
14226# if !defined(_WIN32_WCE)
14227# include <intrin.h>
14228# pragma intrinsic(_byteswap_ushort)
14229# pragma intrinsic(_byteswap_ulong)
14230# pragma intrinsic(_byteswap_uint64)
14231# pragma intrinsic(_ReadWriteBarrier)
14232# else
14233# include <cmnintrin.h>
14234# endif
14235# endif
14236#endif
14237
14238/*
14239** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit
14240** SEH support if the -DSQLITE_OMIT_SEH option is given.
14241*/
14242#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
14243# define SQLITE_USE_SEH 1
14244#else
14245# undef SQLITE_USE_SEH
14246#endif
14247
14248/*
14249** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly
14250** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14251*/
14252#if defined(SQLITE_DIRECT_OVERFLOW_READ1) && SQLITE_DIRECT_OVERFLOW_READ1+1==1
14253 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14254# undef SQLITE_DIRECT_OVERFLOW_READ1
14255#else
14256 /* In all other cases, enable */
14257# define SQLITE_DIRECT_OVERFLOW_READ1 1
14258#endif
14259
14260
14261/*
14262** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
14263** 0 means mutexes are permanently disable and the library is never
14264** threadsafe. 1 means the library is serialized which is the highest
14265** level of threadsafety. 2 means the library is multithreaded - multiple
14266** threads can use SQLite as long as no two threads try to use the same
14267** database connection at the same time.
14268**
14269** Older versions of SQLite used an optional THREADSAFE macro.
14270** We support that for legacy.
14271**
14272** To ensure that the correct value of "THREADSAFE" is reported when querying
14273** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
14274** logic is partially replicated in ctime.c. If it is updated here, it should
14275** also be updated there.
14276*/
14277#if !defined(SQLITE_THREADSAFE2)
14278# if defined(THREADSAFE)
14279# define SQLITE_THREADSAFE2 THREADSAFE
14280# else
14281# define SQLITE_THREADSAFE2 1 /* IMP: R-07272-22309 */
14282# endif
14283#endif
14284
14285/*
14286** Powersafe overwrite is on by default. But can be turned off using
14287** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
14288*/
14289#ifndef SQLITE_POWERSAFE_OVERWRITE1
14290# define SQLITE_POWERSAFE_OVERWRITE1 1
14291#endif
14292
14293/*
14294** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
14295** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
14296** which case memory allocation statistics are disabled by default.
14297*/
14298#if !defined(SQLITE_DEFAULT_MEMSTATUS1)
14299# define SQLITE_DEFAULT_MEMSTATUS1 1
14300#endif
14301
14302/*
14303** Exactly one of the following macros must be defined in order to
14304** specify which memory allocation subsystem to use.
14305**
14306** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
14307** SQLITE_WIN32_MALLOC // Use Win32 native heap API
14308** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
14309** SQLITE_MEMDEBUG // Debugging version of system malloc()
14310**
14311** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
14312** assert() macro is enabled, each call into the Win32 native heap subsystem
14313** will cause HeapValidate to be called. If heap validation should fail, an
14314** assertion will be triggered.
14315**
14316** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
14317** the default.
14318*/
14319#if defined(SQLITE_SYSTEM_MALLOC1) \
14320 + defined(SQLITE_WIN32_MALLOC) \
14321 + defined(SQLITE_ZERO_MALLOC) \
14322 + defined(SQLITE_MEMDEBUG)>1
14323# error "Two or more of the following compile-time configuration options\
14324 are defined but at most one is allowed:\
14325 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
14326 SQLITE_ZERO_MALLOC"
14327#endif
14328#if defined(SQLITE_SYSTEM_MALLOC1) \
14329 + defined(SQLITE_WIN32_MALLOC) \
14330 + defined(SQLITE_ZERO_MALLOC) \
14331 + defined(SQLITE_MEMDEBUG)==0
14332# define SQLITE_SYSTEM_MALLOC1 1
14333#endif
14334
14335/*
14336** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
14337** sizes of memory allocations below this value where possible.
14338*/
14339#if !defined(SQLITE_MALLOC_SOFT_LIMIT1024)
14340# define SQLITE_MALLOC_SOFT_LIMIT1024 1024
14341#endif
14342
14343/*
14344** We need to define _XOPEN_SOURCE as follows in order to enable
14345** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
14346** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
14347** it.
14348*/
14349#if !defined(_XOPEN_SOURCE700) && !defined(__DARWIN__) && !defined(__APPLE__)
14350# define _XOPEN_SOURCE700 600
14351#endif
14352
14353/*
14354** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
14355** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
14356** make it true by defining or undefining NDEBUG.
14357**
14358** Setting NDEBUG makes the code smaller and faster by disabling the
14359** assert() statements in the code. So we want the default action
14360** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
14361** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
14362** feature.
14363*/
14364#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
14365# define NDEBUG1 1
14366#endif
14367#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
14368# undef NDEBUG1
14369#endif
14370
14371/*
14372** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
14373*/
14374#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
14375# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
14376#endif
14377
14378/*
14379** The testcase() macro is used to aid in coverage testing. When
14380** doing coverage testing, the condition inside the argument to
14381** testcase() must be evaluated both true and false in order to
14382** get full branch coverage. The testcase() macro is inserted
14383** to help ensure adequate test coverage in places where simple
14384** condition/decision coverage is inadequate. For example, testcase()
14385** can be used to make sure boundary values are tested. For
14386** bitmask tests, testcase() can be used to make sure each bit
14387** is significant and used at least once. On switch statements
14388** where multiple cases go to the same block of code, testcase()
14389** can insure that all cases are evaluated.
14390*/
14391#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
14392# ifndef SQLITE_AMALGAMATION1
14393 extern unsigned int sqlite3CoverageCounter;
14394# endif
14395# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__14395; }
14396#else
14397# define testcase(X)
14398#endif
14399
14400/*
14401** The TESTONLY macro is used to enclose variable declarations or
14402** other bits of code that are needed to support the arguments
14403** within testcase() and assert() macros.
14404*/
14405#if !defined(NDEBUG1) || defined(SQLITE_COVERAGE_TEST)
14406# define TESTONLY(X) X
14407#else
14408# define TESTONLY(X)
14409#endif
14410
14411/*
14412** Sometimes we need a small amount of code such as a variable initialization
14413** to setup for a later assert() statement. We do not want this code to
14414** appear when assert() is disabled. The following macro is therefore
14415** used to contain that setup code. The "VVA" acronym stands for
14416** "Verification, Validation, and Accreditation". In other words, the
14417** code within VVA_ONLY() will only run during verification processes.
14418*/
14419#ifndef NDEBUG1
14420# define VVA_ONLY(X) X
14421#else
14422# define VVA_ONLY(X)
14423#endif
14424
14425/*
14426** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
14427** and mutation testing
14428*/
14429#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
14430# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
14431#endif
14432
14433/*
14434** The ALWAYS and NEVER macros surround boolean expressions which
14435** are intended to always be true or false, respectively. Such
14436** expressions could be omitted from the code completely. But they
14437** are included in a few cases in order to enhance the resilience
14438** of SQLite to unexpected behavior - to make the code "self-healing"
14439** or "ductile" rather than being "brittle" and crashing at the first
14440** hint of unplanned behavior.
14441**
14442** In other words, ALWAYS and NEVER are added for defensive code.
14443**
14444** When doing coverage testing ALWAYS and NEVER are hard-coded to
14445** be true and false so that the unreachable code they specify will
14446** not be counted as untested code.
14447*/
14448#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
14449# define ALWAYS(X)(X) (1)
14450# define NEVER(X)(X) (0)
14451#elif !defined(NDEBUG1)
14452# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
14453# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
14454#else
14455# define ALWAYS(X)(X) (X)
14456# define NEVER(X)(X) (X)
14457#endif
14458
14459/*
14460** Some conditionals are optimizations only. In other words, if the
14461** conditionals are replaced with a constant 1 (true) or 0 (false) then
14462** the correct answer is still obtained, though perhaps not as quickly.
14463**
14464** The following macros mark these optimizations conditionals.
14465*/
14466#if defined(SQLITE_MUTATION_TEST)
14467# define OK_IF_ALWAYS_TRUE(X)(X) (1)
14468# define OK_IF_ALWAYS_FALSE(X)(X) (0)
14469#else
14470# define OK_IF_ALWAYS_TRUE(X)(X) (X)
14471# define OK_IF_ALWAYS_FALSE(X)(X) (X)
14472#endif
14473
14474/*
14475** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
14476** defined. We need to defend against those failures when testing with
14477** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
14478** during a normal build. The following macro can be used to disable tests
14479** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
14480*/
14481#if defined(SQLITE_TEST_REALLOC_STRESS)
14482# define ONLY_IF_REALLOC_STRESS(X)(0) (X)
14483#elif !defined(NDEBUG1)
14484# define ONLY_IF_REALLOC_STRESS(X)(0) ((X)?(assert(0)((void) (0)),1):0)
14485#else
14486# define ONLY_IF_REALLOC_STRESS(X)(0) (0)
14487#endif
14488
14489/*
14490** Declarations used for tracing the operating system interfaces.
14491*/
14492#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
14493 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
14494 extern int sqlite3OSTrace;
14495# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
14496# define SQLITE_HAVE_OS_TRACE
14497#else
14498# define OSTRACE(X)
14499# undef SQLITE_HAVE_OS_TRACE
14500#endif
14501
14502/*
14503** Is the sqlite3ErrName() function needed in the build? Currently,
14504** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
14505** OSTRACE is enabled), and by several "test*.c" files (which are
14506** compiled using SQLITE_TEST).
14507*/
14508#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
14509 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
14510# define SQLITE_NEED_ERR_NAME
14511#else
14512# undef SQLITE_NEED_ERR_NAME
14513#endif
14514
14515/*
14516** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
14517*/
14518#ifdef SQLITE_OMIT_EXPLAIN
14519# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
14520#endif
14521
14522/*
14523** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE
14524*/
14525#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE)
14526# define SQLITE_OMIT_ALTERTABLE
14527#endif
14528
14529#define SQLITE_DIGIT_SEPARATOR'_' '_'
14530
14531/*
14532** Return true (non-zero) if the input is an integer that is too large
14533** to fit in 32-bits. This macro is used inside of various testcase()
14534** macros to verify that we have tested SQLite for large-file support.
14535*/
14536#define IS_BIG_INT(X)(((X)&~(i64)0xffffffff)!=0) (((X)&~(i64)0xffffffff)!=0)
14537
14538/*
14539** The macro unlikely() is a hint that surrounds a boolean
14540** expression that is usually false. Macro likely() surrounds
14541** a boolean expression that is usually true. These hints could,
14542** in theory, be used by the compiler to generate better code, but
14543** currently they are just comments for human readers.
14544*/
14545#define likely(X)(X) (X)
14546#define unlikely(X)(X) (X)
14547
14548/************** Include hash.h in the middle of sqliteInt.h ******************/
14549/************** Begin file hash.h ********************************************/
14550/*
14551** 2001 September 22
14552**
14553** The author disclaims copyright to this source code. In place of
14554** a legal notice, here is a blessing:
14555**
14556** May you do good and not evil.
14557** May you find forgiveness for yourself and forgive others.
14558** May you share freely, never taking more than you give.
14559**
14560*************************************************************************
14561** This is the header file for the generic hash-table implementation
14562** used in SQLite.
14563*/
14564#ifndef SQLITE_HASH_H
14565#define SQLITE_HASH_H
14566
14567/* Forward declarations of structures. */
14568typedef struct Hash Hash;
14569typedef struct HashElem HashElem;
14570
14571/* A complete hash table is an instance of the following structure.
14572** The internals of this structure are intended to be opaque -- client
14573** code should not attempt to access or modify the fields of this structure
14574** directly. Change this structure only by using the routines below.
14575** However, some of the "procedures" and "functions" for modifying and
14576** accessing this structure are really macros, so we can't really make
14577** this structure opaque.
14578**
14579** All elements of the hash table are on a single doubly-linked list.
14580** Hash.first points to the head of this list.
14581**
14582** There are Hash.htsize buckets. Each bucket points to a spot in
14583** the global doubly-linked list. The contents of the bucket are the
14584** element pointed to plus the next _ht.count-1 elements in the list.
14585**
14586** Hash.htsize and Hash.ht may be zero. In that case lookup is done
14587** by a linear search of the global list. For small tables, the
14588** Hash.ht table is never allocated because if there are few elements
14589** in the table, it is faster to do a linear search than to manage
14590** the hash table.
14591*/
14592struct Hash {
14593 unsigned int htsize; /* Number of buckets in the hash table */
14594 unsigned int count; /* Number of entries in this table */
14595 HashElem *first; /* The first element of the array */
14596 struct _ht { /* the hash table */
14597 unsigned int count; /* Number of entries with this hash */
14598 HashElem *chain; /* Pointer to first entry with this hash */
14599 } *ht;
14600};
14601
14602/* Each element in the hash table is an instance of the following
14603** structure. All elements are stored on a single doubly-linked list.
14604**
14605** Again, this structure is intended to be opaque, but it can't really
14606** be opaque because it is used by macros.
14607*/
14608struct HashElem {
14609 HashElem *next, *prev; /* Next and previous elements in the table */
14610 void *data; /* Data associated with this element */
14611 const char *pKey; /* Key associated with this element */
14612};
14613
14614/*
14615** Access routines. To delete, insert a NULL pointer.
14616*/
14617SQLITE_PRIVATEstatic void sqlite3HashInit(Hash*);
14618SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
14619SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash*, const char *pKey);
14620SQLITE_PRIVATEstatic void sqlite3HashClear(Hash*);
14621
14622/*
14623** Macros for looping over all elements of a hash table. The idiom is
14624** like this:
14625**
14626** Hash h;
14627** HashElem *p;
14628** ...
14629** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
14630** SomeStructure *pData = sqliteHashData(p);
14631** // do something with pData
14632** }
14633*/
14634#define sqliteHashFirst(H)((H)->first) ((H)->first)
14635#define sqliteHashNext(E)((E)->next) ((E)->next)
14636#define sqliteHashData(E)((E)->data) ((E)->data)
14637/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14638/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14639
14640/*
14641** Number of entries in a hash table
14642*/
14643#define sqliteHashCount(H)((H)->count) ((H)->count)
14644
14645#endif /* SQLITE_HASH_H */
14646
14647/************** End of hash.h ************************************************/
14648/************** Continuing where we left off in sqliteInt.h ******************/
14649/************** Include parse.h in the middle of sqliteInt.h *****************/
14650/************** Begin file parse.h *******************************************/
14651#define TK_SEMI1 1
14652#define TK_EXPLAIN2 2
14653#define TK_QUERY3 3
14654#define TK_PLAN4 4
14655#define TK_BEGIN5 5
14656#define TK_TRANSACTION6 6
14657#define TK_DEFERRED7 7
14658#define TK_IMMEDIATE8 8
14659#define TK_EXCLUSIVE9 9
14660#define TK_COMMIT10 10
14661#define TK_END11 11
14662#define TK_ROLLBACK12 12
14663#define TK_SAVEPOINT13 13
14664#define TK_RELEASE14 14
14665#define TK_TO15 15
14666#define TK_TABLE16 16
14667#define TK_CREATE17 17
14668#define TK_IF18 18
14669#define TK_NOT19 19
14670#define TK_EXISTS20 20
14671#define TK_TEMP21 21
14672#define TK_LP22 22
14673#define TK_RP23 23
14674#define TK_AS24 24
14675#define TK_COMMA25 25
14676#define TK_WITHOUT26 26
14677#define TK_ABORT27 27
14678#define TK_ACTION28 28
14679#define TK_AFTER29 29
14680#define TK_ANALYZE30 30
14681#define TK_ASC31 31
14682#define TK_ATTACH32 32
14683#define TK_BEFORE33 33
14684#define TK_BY34 34
14685#define TK_CASCADE35 35
14686#define TK_CAST36 36
14687#define TK_CONFLICT37 37
14688#define TK_DATABASE38 38
14689#define TK_DESC39 39
14690#define TK_DETACH40 40
14691#define TK_EACH41 41
14692#define TK_FAIL42 42
14693#define TK_OR43 43
14694#define TK_AND44 44
14695#define TK_IS45 45
14696#define TK_ISNOT46 46
14697#define TK_MATCH47 47
14698#define TK_LIKE_KW48 48
14699#define TK_BETWEEN49 49
14700#define TK_IN50 50
14701#define TK_ISNULL51 51
14702#define TK_NOTNULL52 52
14703#define TK_NE53 53
14704#define TK_EQ54 54
14705#define TK_GT55 55
14706#define TK_LE56 56
14707#define TK_LT57 57
14708#define TK_GE58 58
14709#define TK_ESCAPE59 59
14710#define TK_ID60 60
14711#define TK_COLUMNKW61 61
14712#define TK_DO62 62
14713#define TK_FOR63 63
14714#define TK_IGNORE64 64
14715#define TK_INITIALLY65 65
14716#define TK_INSTEAD66 66
14717#define TK_NO67 67
14718#define TK_KEY68 68
14719#define TK_OF69 69
14720#define TK_OFFSET70 70
14721#define TK_PRAGMA71 71
14722#define TK_RAISE72 72
14723#define TK_RECURSIVE73 73
14724#define TK_REPLACE74 74
14725#define TK_RESTRICT75 75
14726#define TK_ROW76 76
14727#define TK_ROWS77 77
14728#define TK_TRIGGER78 78
14729#define TK_VACUUM79 79
14730#define TK_VIEW80 80
14731#define TK_VIRTUAL81 81
14732#define TK_WITH82 82
14733#define TK_NULLS83 83
14734#define TK_FIRST84 84
14735#define TK_LAST85 85
14736#define TK_CURRENT86 86
14737#define TK_FOLLOWING87 87
14738#define TK_PARTITION88 88
14739#define TK_PRECEDING89 89
14740#define TK_RANGE90 90
14741#define TK_UNBOUNDED91 91
14742#define TK_EXCLUDE92 92
14743#define TK_GROUPS93 93
14744#define TK_OTHERS94 94
14745#define TK_TIES95 95
14746#define TK_GENERATED96 96
14747#define TK_ALWAYS97 97
14748#define TK_MATERIALIZED98 98
14749#define TK_REINDEX99 99
14750#define TK_RENAME100 100
14751#define TK_CTIME_KW101 101
14752#define TK_ANY102 102
14753#define TK_BITAND103 103
14754#define TK_BITOR104 104
14755#define TK_LSHIFT105 105
14756#define TK_RSHIFT106 106
14757#define TK_PLUS107 107
14758#define TK_MINUS108 108
14759#define TK_STAR109 109
14760#define TK_SLASH110 110
14761#define TK_REM111 111
14762#define TK_CONCAT112 112
14763#define TK_PTR113 113
14764#define TK_COLLATE114 114
14765#define TK_BITNOT115 115
14766#define TK_ON116 116
14767#define TK_INDEXED117 117
14768#define TK_STRING118 118
14769#define TK_JOIN_KW119 119
14770#define TK_CONSTRAINT120 120
14771#define TK_DEFAULT121 121
14772#define TK_NULL122 122
14773#define TK_PRIMARY123 123
14774#define TK_UNIQUE124 124
14775#define TK_CHECK125 125
14776#define TK_REFERENCES126 126
14777#define TK_AUTOINCR127 127
14778#define TK_INSERT128 128
14779#define TK_DELETE129 129
14780#define TK_UPDATE130 130
14781#define TK_SET131 131
14782#define TK_DEFERRABLE132 132
14783#define TK_FOREIGN133 133
14784#define TK_DROP134 134
14785#define TK_UNION135 135
14786#define TK_ALL136 136
14787#define TK_EXCEPT137 137
14788#define TK_INTERSECT138 138
14789#define TK_SELECT139 139
14790#define TK_VALUES140 140
14791#define TK_DISTINCT141 141
14792#define TK_DOT142 142
14793#define TK_FROM143 143
14794#define TK_JOIN144 144
14795#define TK_USING145 145
14796#define TK_ORDER146 146
14797#define TK_GROUP147 147
14798#define TK_HAVING148 148
14799#define TK_LIMIT149 149
14800#define TK_WHERE150 150
14801#define TK_RETURNING151 151
14802#define TK_INTO152 152
14803#define TK_NOTHING153 153
14804#define TK_FLOAT154 154
14805#define TK_BLOB155 155
14806#define TK_INTEGER156 156
14807#define TK_VARIABLE157 157
14808#define TK_CASE158 158
14809#define TK_WHEN159 159
14810#define TK_THEN160 160
14811#define TK_ELSE161 161
14812#define TK_INDEX162 162
14813#define TK_ALTER163 163
14814#define TK_ADD164 164
14815#define TK_WINDOW165 165
14816#define TK_OVER166 166
14817#define TK_FILTER167 167
14818#define TK_COLUMN168 168
14819#define TK_AGG_FUNCTION169 169
14820#define TK_AGG_COLUMN170 170
14821#define TK_TRUEFALSE171 171
14822#define TK_FUNCTION172 172
14823#define TK_UPLUS173 173
14824#define TK_UMINUS174 174
14825#define TK_TRUTH175 175
14826#define TK_REGISTER176 176
14827#define TK_VECTOR177 177
14828#define TK_SELECT_COLUMN178 178
14829#define TK_IF_NULL_ROW179 179
14830#define TK_ASTERISK180 180
14831#define TK_SPAN181 181
14832#define TK_ERROR182 182
14833#define TK_QNUMBER183 183
14834#define TK_SPACE184 184
14835#define TK_ILLEGAL185 185
14836
14837/************** End of parse.h ***********************************************/
14838/************** Continuing where we left off in sqliteInt.h ******************/
14839#include <stdio.h>
14840#include <stdlib.h>
14841#include <string.h>
14842#include <assert.h>
14843#include <stddef.h>
14844#include <ctype.h>
14845
14846/*
14847** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
14848** This allows better measurements of where memcpy() is used when running
14849** cachegrind. But this macro version of memcpy() is very slow so it
14850** should not be used in production. This is a performance measurement
14851** hack only.
14852*/
14853#ifdef SQLITE_INLINE_MEMCPY
14854# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
14855 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14856#endif
14857
14858/*
14859** If compiling for a processor that lacks floating point support,
14860** substitute integer for floating-point
14861*/
14862#ifdef SQLITE_OMIT_FLOATING_POINT
14863# define double sqlite_int64
14864# define float sqlite_int64
14865# define fabs(X) ((X)<0?-(X):(X))
14866# define sqlite3IsOverflow(X) 0
14867# ifndef SQLITE_BIG_DBL(1e99)
14868# define SQLITE_BIG_DBL(1e99) (((sqlite3_int64)1)<<50)
14869# endif
14870# define SQLITE_OMIT_DATETIME_FUNCS 1
14871# define SQLITE_OMIT_TRACE 1
14872# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
14873# undef SQLITE_HAVE_ISNAN1
14874#endif
14875#ifndef SQLITE_BIG_DBL(1e99)
14876# define SQLITE_BIG_DBL(1e99) (1e99)
14877#endif
14878
14879/*
14880** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
14881** afterward. Having this macro allows us to cause the C compiler
14882** to omit code used by TEMP tables without messy #ifndef statements.
14883*/
14884#ifdef SQLITE_OMIT_TEMPDB
14885#define OMIT_TEMPDB0 1
14886#else
14887#define OMIT_TEMPDB0 0
14888#endif
14889
14890/*
14891** The "file format" number is an integer that is incremented whenever
14892** the VDBE-level file format changes. The following macros define the
14893** the default file format for new databases and the maximum file format
14894** that the library can read.
14895*/
14896#define SQLITE_MAX_FILE_FORMAT4 4
14897#ifndef SQLITE_DEFAULT_FILE_FORMAT4
14898# define SQLITE_DEFAULT_FILE_FORMAT4 4
14899#endif
14900
14901/*
14902** Determine whether triggers are recursive by default. This can be
14903** changed at run-time using a pragma.
14904*/
14905#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
14906# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS0 0
14907#endif
14908
14909/*
14910** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
14911** on the command-line
14912*/
14913#ifndef SQLITE_TEMP_STORE1
14914# define SQLITE_TEMP_STORE1 1
14915#endif
14916
14917/*
14918** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
14919** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
14920** to zero.
14921*/
14922#if SQLITE_TEMP_STORE1==3 || SQLITE_THREADSAFE2==0
14923# undef SQLITE_MAX_WORKER_THREADS8
14924# define SQLITE_MAX_WORKER_THREADS8 0
14925#endif
14926#ifndef SQLITE_MAX_WORKER_THREADS8
14927# define SQLITE_MAX_WORKER_THREADS8 8
14928#endif
14929#ifndef SQLITE_DEFAULT_WORKER_THREADS0
14930# define SQLITE_DEFAULT_WORKER_THREADS0 0
14931#endif
14932#if SQLITE_DEFAULT_WORKER_THREADS0>SQLITE_MAX_WORKER_THREADS8
14933# undef SQLITE_MAX_WORKER_THREADS8
14934# define SQLITE_MAX_WORKER_THREADS8 SQLITE_DEFAULT_WORKER_THREADS0
14935#endif
14936
14937/*
14938** The default initial allocation for the pagecache when using separate
14939** pagecaches for each database connection. A positive number is the
14940** number of pages. A negative number N translations means that a buffer
14941** of -1024*N bytes is allocated and used for as many pages as it will hold.
14942**
14943** The default value of "20" was chosen to minimize the run-time of the
14944** speedtest1 test program with options: --shrink-memory --reprepare
14945*/
14946#ifndef SQLITE_DEFAULT_PCACHE_INITSZ20
14947# define SQLITE_DEFAULT_PCACHE_INITSZ20 20
14948#endif
14949
14950/*
14951** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
14952*/
14953#ifndef SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff
14954# define SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff 0x7fffffff
14955#endif
14956
14957/*
14958** The compile-time options SQLITE_MMAP_READWRITE and
14959** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
14960** You must choose one or the other (or neither) but not both.
14961*/
14962#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
14963#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
14964#endif
14965
14966/*
14967** GCC does not define the offsetof() macro so we'll have to do it
14968** ourselves.
14969*/
14970#ifndef offsetof
14971#define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14972#endif
14973
14974/*
14975** Macros to compute minimum and maximum of two numbers.
14976*/
14977#ifndef MIN
14978# define MIN(A,B)((A)<(B)?(A):(B)) ((A)<(B)?(A):(B))
14979#endif
14980#ifndef MAX
14981# define MAX(A,B)((A)>(B)?(A):(B)) ((A)>(B)?(A):(B))
14982#endif
14983
14984/*
14985** Swap two objects of type TYPE.
14986*/
14987#define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
14988
14989/*
14990** Check to see if this machine uses EBCDIC. (Yes, believe it or
14991** not, there are still machines out there that use EBCDIC.)
14992*/
14993#if 'A' == '\301'
14994# define SQLITE_EBCDIC 1
14995#else
14996# define SQLITE_ASCII1 1
14997#endif
14998
14999/*
15000** Integers of known sizes. These typedefs might change for architectures
15001** where the sizes very. Preprocessor macros are available so that the
15002** types can be conveniently redefined at compile-type. Like this:
15003**
15004** cc '-DUINTPTR_TYPE=long long int' ...
15005*/
15006#ifndef UINT32_TYPEunsigned int
15007# ifdef HAVE_UINT32_T
15008# define UINT32_TYPEunsigned int uint32_t
15009# else
15010# define UINT32_TYPEunsigned int unsigned int
15011# endif
15012#endif
15013#ifndef UINT16_TYPEunsigned short int
15014# ifdef HAVE_UINT16_T
15015# define UINT16_TYPEunsigned short int uint16_t
15016# else
15017# define UINT16_TYPEunsigned short int unsigned short int
15018# endif
15019#endif
15020#ifndef INT16_TYPEshort int
15021# ifdef HAVE_INT16_T
15022# define INT16_TYPEshort int int16_t
15023# else
15024# define INT16_TYPEshort int short int
15025# endif
15026#endif
15027#ifndef UINT8_TYPEunsigned char
15028# ifdef HAVE_UINT8_T
15029# define UINT8_TYPEunsigned char uint8_t
15030# else
15031# define UINT8_TYPEunsigned char unsigned char
15032# endif
15033#endif
15034#ifndef INT8_TYPEsigned char
15035# ifdef HAVE_INT8_T
15036# define INT8_TYPEsigned char int8_t
15037# else
15038# define INT8_TYPEsigned char signed char
15039# endif
15040#endif
15041typedef sqlite_int64 i64; /* 8-byte signed integer */
15042typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
15043typedef UINT32_TYPEunsigned int u32; /* 4-byte unsigned integer */
15044typedef UINT16_TYPEunsigned short int u16; /* 2-byte unsigned integer */
15045typedef INT16_TYPEshort int i16; /* 2-byte signed integer */
15046typedef UINT8_TYPEunsigned char u8; /* 1-byte unsigned integer */
15047typedef INT8_TYPEsigned char i8; /* 1-byte signed integer */
15048
15049/*
15050** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
15051** that can be stored in a u32 without loss of data. The value
15052** is 0x00000000ffffffff. But because of quirks of some compilers, we
15053** have to specify the value in the less intuitive manner shown:
15054*/
15055#define SQLITE_MAX_U32((((u64)1)<<32)-1) ((((u64)1)<<32)-1)
15056
15057/*
15058** The datatype used to store estimates of the number of rows in a
15059** table or index.
15060*/
15061typedef u64 tRowcnt;
15062
15063/*
15064** Estimated quantities used for query planning are stored as 16-bit
15065** logarithms. For quantity X, the value stored is 10*log2(X). This
15066** gives a possible range of values of approximately 1.0e986 to 1e-986.
15067** But the allowed values are "grainy". Not every value is representable.
15068** For example, quantities 16 and 17 are both represented by a LogEst
15069** of 40. However, since LogEst quantities are suppose to be estimates,
15070** not exact values, this imprecision is not a problem.
15071**
15072** "LogEst" is short for "Logarithmic Estimate".
15073**
15074** Examples:
15075** 1 -> 0 20 -> 43 10000 -> 132
15076** 2 -> 10 25 -> 46 25000 -> 146
15077** 3 -> 16 100 -> 66 1000000 -> 199
15078** 4 -> 20 1000 -> 99 1048576 -> 200
15079** 10 -> 33 1024 -> 100 4294967296 -> 320
15080**
15081** The LogEst can be negative to indicate fractional values.
15082** Examples:
15083**
15084** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
15085*/
15086typedef INT16_TYPEshort int LogEst;
15087
15088/*
15089** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
15090*/
15091#ifndef SQLITE_PTRSIZE8
15092# if defined(__SIZEOF_POINTER__8)
15093# define SQLITE_PTRSIZE8 __SIZEOF_POINTER__8
15094# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
15095 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
15096 (defined(__APPLE__) && defined(__ppc__)) || \
15097 (defined(__TOS_AIX__) && !defined(__64BIT__))
15098# define SQLITE_PTRSIZE8 4
15099# else
15100# define SQLITE_PTRSIZE8 8
15101# endif
15102#endif
15103
15104/* The uptr type is an unsigned integer large enough to hold a pointer
15105*/
15106#if defined(HAVE_STDINT_H)
15107 typedef uintptr_t uptr;
15108#elif SQLITE_PTRSIZE8==4
15109 typedef u32 uptr;
15110#else
15111 typedef u64 uptr;
15112#endif
15113
15114/*
15115** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
15116** something between S (inclusive) and E (exclusive).
15117**
15118** In other words, S is a buffer and E is a pointer to the first byte after
15119** the end of buffer S. This macro returns true if P points to something
15120** contained within the buffer S.
15121*/
15122#define SQLITE_WITHIN(P,S,E)(((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
15123
15124/*
15125** P is one byte past the end of a large buffer. Return true if a span of bytes
15126** between S..E crosses the end of that buffer. In other words, return true
15127** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
15128**
15129** S is the start of the span. E is one byte past the end of end of span.
15130**
15131** P
15132** |-----------------| FALSE
15133** |-------|
15134** S E
15135**
15136** P
15137** |-----------------|
15138** |-------| TRUE
15139** S E
15140**
15141** P
15142** |-----------------|
15143** |-------| FALSE
15144** S E
15145*/
15146#define SQLITE_OVERFLOW(P,S,E)(((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P))) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P)))
15147
15148/*
15149** Macros to determine whether the machine is big or little endian,
15150** and whether or not that determination is run-time or compile-time.
15151**
15152** For best performance, an attempt is made to guess at the byte-order
15153** using C-preprocessor macros. If that is unsuccessful, or if
15154** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
15155** at run-time.
15156**
15157** If you are building SQLite on some obscure platform for which the
15158** following ifdef magic does not work, you can always include either:
15159**
15160** -DSQLITE_BYTEORDER=1234
15161**
15162** or
15163**
15164** -DSQLITE_BYTEORDER=4321
15165**
15166** to cause the build to work for little-endian or big-endian processors,
15167** respectively.
15168*/
15169#ifndef SQLITE_BYTEORDER1234 /* Replicate changes at tag-20230904a */
15170# if defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_BIG_ENDIAN__4321
15171# define SQLITE_BYTEORDER1234 4321
15172# elif defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_LITTLE_ENDIAN__1234
15173# define SQLITE_BYTEORDER1234 1234
15174# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
15175# define SQLITE_BYTEORDER1234 4321
15176# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
15177 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
15178 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
15179 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
15180# define SQLITE_BYTEORDER1234 1234
15181# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
15182# define SQLITE_BYTEORDER1234 4321
15183# else
15184# define SQLITE_BYTEORDER1234 0
15185# endif
15186#endif
15187#if SQLITE_BYTEORDER1234==4321
15188# define SQLITE_BIGENDIAN0 1
15189# define SQLITE_LITTLEENDIAN1 0
15190# define SQLITE_UTF16NATIVE2 SQLITE_UTF16BE3
15191#elif SQLITE_BYTEORDER1234==1234
15192# define SQLITE_BIGENDIAN0 0
15193# define SQLITE_LITTLEENDIAN1 1
15194# define SQLITE_UTF16NATIVE2 SQLITE_UTF16LE2
15195#else
15196# ifdef SQLITE_AMALGAMATION1
15197 const int sqlite3one = 1;
15198# else
15199 extern const int sqlite3one;
15200# endif
15201# define SQLITE_BIGENDIAN0 (*(char *)(&sqlite3one)==0)
15202# define SQLITE_LITTLEENDIAN1 (*(char *)(&sqlite3one)==1)
15203# define SQLITE_UTF16NATIVE2 (SQLITE_BIGENDIAN0?SQLITE_UTF16BE3:SQLITE_UTF16LE2)
15204#endif
15205
15206/*
15207** Constants for the largest and smallest possible 64-bit signed integers.
15208** These macros are designed to work correctly on both 32-bit and 64-bit
15209** compilers.
15210*/
15211#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
15212#define LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) (0xffffffff|(((u64)0xffffffff)<<32))
15213#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
15214
15215/*
15216** Round up a number to the next larger multiple of 8. This is used
15217** to force 8-byte alignment on 64-bit architectures.
15218**
15219** ROUND8() always does the rounding, for any argument.
15220**
15221** ROUND8P() assumes that the argument is already an integer number of
15222** pointers in size, and so it is a no-op on systems where the pointer
15223** size is 8.
15224*/
15225#define ROUND8(x)(((x)+7)&~7) (((x)+7)&~7)
15226#if SQLITE_PTRSIZE8==8
15227# define ROUND8P(x)(x) (x)
15228#else
15229# define ROUND8P(x)(x) (((x)+7)&~7)
15230#endif
15231
15232/*
15233** Round down to the nearest multiple of 8
15234*/
15235#define ROUNDDOWN8(x)((x)&~7) ((x)&~7)
15236
15237/*
15238** Assert that the pointer X is aligned to an 8-byte boundary. This
15239** macro is used only within assert() to verify that the code gets
15240** all alignment restrictions correct.
15241**
15242** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
15243** underlying malloc() implementation might return us 4-byte aligned
15244** pointers. In that case, only verify 4-byte alignment.
15245*/
15246#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
15247# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&3)==0)
15248#else
15249# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&7)==0)
15250#endif
15251
15252/*
15253** Disable MMAP on platforms where it is known to not work
15254*/
15255#if defined(__OpenBSD__) || defined(__QNXNTO__)
15256# undef SQLITE_MAX_MMAP_SIZE20971520
15257# define SQLITE_MAX_MMAP_SIZE20971520 0
15258#endif
15259
15260/*
15261** Default maximum size of memory used by memory-mapped I/O in the VFS
15262*/
15263#ifdef __APPLE__
15264# include <TargetConditionals.h>
15265#endif
15266#ifndef SQLITE_MAX_MMAP_SIZE20971520
15267# if defined(__linux__1) \
15268 || defined(_WIN32) \
15269 || (defined(__APPLE__) && defined(__MACH__)) \
15270 || defined(__sun) \
15271 || defined(__FreeBSD__) \
15272 || defined(__DragonFly__)
15273# define SQLITE_MAX_MMAP_SIZE20971520 0x7fff0000 /* 2147418112 */
15274# else
15275# define SQLITE_MAX_MMAP_SIZE20971520 0
15276# endif
15277#endif
15278
15279/*
15280** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
15281** default MMAP_SIZE is specified at compile-time, make sure that it does
15282** not exceed the maximum mmap size.
15283*/
15284#ifndef SQLITE_DEFAULT_MMAP_SIZE0
15285# define SQLITE_DEFAULT_MMAP_SIZE0 0
15286#endif
15287#if SQLITE_DEFAULT_MMAP_SIZE0>SQLITE_MAX_MMAP_SIZE20971520
15288# undef SQLITE_DEFAULT_MMAP_SIZE0
15289# define SQLITE_DEFAULT_MMAP_SIZE0 SQLITE_MAX_MMAP_SIZE20971520
15290#endif
15291
15292/*
15293** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not
15294** the Abstract Syntax Tree tracing logic is turned on.
15295*/
15296#if !defined(SQLITE_AMALGAMATION1)
15297SQLITE_PRIVATEstatic u32 sqlite3TreeTrace;
15298#endif
15299#if defined(SQLITE_DEBUG) \
15300 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \
15301 || defined(SQLITE_ENABLE_TREETRACE))
15302# define TREETRACE_ENABLED0 1
15303# define TREETRACE(K,P,S,X) \
15304 if(sqlite3TreeTrace&(K)) \
15305 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
15306 sqlite3DebugPrintf X
15307#else
15308# define TREETRACE(K,P,S,X)
15309# define TREETRACE_ENABLED0 0
15310#endif
15311
15312/* TREETRACE flag meanings:
15313**
15314** 0x00000001 Beginning and end of SELECT processing
15315** 0x00000002 WHERE clause processing
15316** 0x00000004 Query flattener
15317** 0x00000008 Result-set wildcard expansion
15318** 0x00000010 Query name resolution
15319** 0x00000020 Aggregate analysis
15320** 0x00000040 Window functions
15321** 0x00000080 Generated column names
15322** 0x00000100 Move HAVING terms into WHERE
15323** 0x00000200 Count-of-view optimization
15324** 0x00000400 Compound SELECT processing
15325** 0x00000800 Drop superfluous ORDER BY
15326** 0x00001000 LEFT JOIN simplifies to JOIN
15327** 0x00002000 Constant propagation
15328** 0x00004000 Push-down optimization
15329** 0x00008000 After all FROM-clause analysis
15330** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
15331** 0x00020000 Transform DISTINCT into GROUP BY
15332** 0x00040000 SELECT tree dump after all code has been generated
15333** 0x00080000 NOT NULL strength reduction
15334*/
15335
15336/*
15337** Macros for "wheretrace"
15338*/
15339SQLITE_PRIVATEstatic u32 sqlite3WhereTrace;
15340#if defined(SQLITE_DEBUG) \
15341 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
15342# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
15343# define WHERETRACE_ENABLED 1
15344#else
15345# define WHERETRACE(K,X)
15346#endif
15347
15348/*
15349** Bits for the sqlite3WhereTrace mask:
15350**
15351** (---any--) Top-level block structure
15352** 0x-------F High-level debug messages
15353** 0x----FFF- More detail
15354** 0xFFFF---- Low-level debug messages
15355**
15356** 0x00000001 Code generation
15357** 0x00000002 Solver
15358** 0x00000004 Solver costs
15359** 0x00000008 WhereLoop inserts
15360**
15361** 0x00000010 Display sqlite3_index_info xBestIndex calls
15362** 0x00000020 Range an equality scan metrics
15363** 0x00000040 IN operator decisions
15364** 0x00000080 WhereLoop cost adjustments
15365** 0x00000100
15366** 0x00000200 Covering index decisions
15367** 0x00000400 OR optimization
15368** 0x00000800 Index scanner
15369** 0x00001000 More details associated with code generation
15370** 0x00002000
15371** 0x00004000 Show all WHERE terms at key points
15372** 0x00008000 Show the full SELECT statement at key places
15373**
15374** 0x00010000 Show more detail when printing WHERE terms
15375** 0x00020000 Show WHERE terms returned from whereScanNext()
15376*/
15377
15378
15379/*
15380** An instance of the following structure is used to store the busy-handler
15381** callback for a given sqlite handle.
15382**
15383** The sqlite.busyHandler member of the sqlite struct contains the busy
15384** callback for the database handle. Each pager opened via the sqlite
15385** handle is passed a pointer to sqlite.busyHandler. The busy-handler
15386** callback is currently invoked only from within pager.c.
15387*/
15388typedef struct BusyHandler BusyHandler;
15389struct BusyHandler {
15390 int (*xBusyHandler)(void *,int); /* The busy callback */
15391 void *pBusyArg; /* First arg to busy callback */
15392 int nBusy; /* Incremented with each busy call */
15393};
15394
15395/*
15396** Name of table that holds the database schema.
15397**
15398** The PREFERRED names are used wherever possible. But LEGACY is also
15399** used for backwards compatibility.
15400**
15401** 1. Queries can use either the PREFERRED or the LEGACY names
15402** 2. The sqlite3_set_authorizer() callback uses the LEGACY name
15403** 3. The PRAGMA table_list statement uses the PREFERRED name
15404**
15405** The LEGACY names are stored in the internal symbol hash table
15406** in support of (2). Names are translated using sqlite3PreferredTableName()
15407** for (3). The sqlite3FindTable() function takes care of translating
15408** names for (1).
15409**
15410** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema".
15411*/
15412#define LEGACY_SCHEMA_TABLE"sqlite_master" "sqlite_master"
15413#define LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master" "sqlite_temp_master"
15414#define PREFERRED_SCHEMA_TABLE"sqlite_schema" "sqlite_schema"
15415#define PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema" "sqlite_temp_schema"
15416
15417
15418/*
15419** The root-page of the schema table.
15420*/
15421#define SCHEMA_ROOT1 1
15422
15423/*
15424** The name of the schema table. The name is different for TEMP.
15425*/
15426#define SCHEMA_TABLE(x)((!0)&&(x==1)?"sqlite_temp_master":"sqlite_master") \
15427 ((!OMIT_TEMPDB0)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master":LEGACY_SCHEMA_TABLE"sqlite_master")
15428
15429/*
15430** A convenience macro that returns the number of elements in
15431** an array.
15432*/
15433#define ArraySize(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
15434
15435/*
15436** Determine if the argument is a power of two
15437*/
15438#define IsPowerOfTwo(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
15439
15440/*
15441** The following value as a destructor means to use sqlite3DbFree().
15442** The sqlite3DbFree() routine requires two parameters instead of the
15443** one parameter that destructors normally want. So we have to introduce
15444** this magic value that the code knows to handle differently. Any
15445** pointer will work here as long as it is distinct from SQLITE_STATIC
15446** and SQLITE_TRANSIENT.
15447*/
15448#define SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ((sqlite3_destructor_type)sqlite3OomClear)
15449
15450/*
15451** When SQLITE_OMIT_WSD is defined, it means that the target platform does
15452** not support Writable Static Data (WSD) such as global and static variables.
15453** All variables must either be on the stack or dynamically allocated from
15454** the heap. When WSD is unsupported, the variable declarations scattered
15455** throughout the SQLite code must become constants instead. The SQLITE_WSD
15456** macro is used for this purpose. And instead of referencing the variable
15457** directly, we use its constant as a key to lookup the run-time allocated
15458** buffer that holds real variable. The constant is also the initializer
15459** for the run-time allocated buffer.
15460**
15461** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
15462** macros become no-ops and have zero performance impact.
15463*/
15464#ifdef SQLITE_OMIT_WSD
15465 #define SQLITE_WSD const
15466 #define GLOBAL(t,v)v (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
15467 #define sqlite3GlobalConfigsqlite3Config GLOBAL(struct Sqlite3Config, sqlite3Config)sqlite3Config
15468SQLITE_API int sqlite3_wsd_init(int N, int J);
15469SQLITE_API void *sqlite3_wsd_find(void *K, int L);
15470#else
15471 #define SQLITE_WSD
15472 #define GLOBAL(t,v)v v
15473 #define sqlite3GlobalConfigsqlite3Config sqlite3Config
15474#endif
15475
15476/*
15477** The following macros are used to suppress compiler warnings and to
15478** make it clear to human readers when a function parameter is deliberately
15479** left unused within the body of a function. This usually happens when
15480** a function is called via a function pointer. For example the
15481** implementation of an SQL aggregate step callback may not use the
15482** parameter indicating the number of arguments passed to the aggregate,
15483** if it knows that this is enforced elsewhere.
15484**
15485** When a function parameter is not used at all within the body of a function,
15486** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
15487** However, these macros may also be used to suppress warnings related to
15488** parameters that may or may not be used depending on compilation options.
15489** For example those parameters only used in assert() statements. In these
15490** cases the parameters are named as per the usual conventions.
15491*/
15492#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
15493#define UNUSED_PARAMETER2(x,y)(void)(x),(void)(y) UNUSED_PARAMETER(x)(void)(x),UNUSED_PARAMETER(y)(void)(y)
15494
15495/*
15496** Forward references to structures
15497*/
15498typedef struct AggInfo AggInfo;
15499typedef struct AuthContext AuthContext;
15500typedef struct AutoincInfo AutoincInfo;
15501typedef struct Bitvec Bitvec;
15502typedef struct CollSeq CollSeq;
15503typedef struct Column Column;
15504typedef struct Cte Cte;
15505typedef struct CteUse CteUse;
15506typedef struct Db Db;
15507typedef struct DbClientData DbClientData;
15508typedef struct DbFixer DbFixer;
15509typedef struct Schema Schema;
15510typedef struct Expr Expr;
15511typedef struct ExprList ExprList;
15512typedef struct FKey FKey;
15513typedef struct FpDecode FpDecode;
15514typedef struct FuncDestructor FuncDestructor;
15515typedef struct FuncDef FuncDef;
15516typedef struct FuncDefHash FuncDefHash;
15517typedef struct IdList IdList;
15518typedef struct Index Index;
15519typedef struct IndexedExpr IndexedExpr;
15520typedef struct IndexSample IndexSample;
15521typedef struct KeyClass KeyClass;
15522typedef struct KeyInfo KeyInfo;
15523typedef struct Lookaside Lookaside;
15524typedef struct LookasideSlot LookasideSlot;
15525typedef struct Module Module;
15526typedef struct NameContext NameContext;
15527typedef struct OnOrUsing OnOrUsing;
15528typedef struct Parse Parse;
15529typedef struct ParseCleanup ParseCleanup;
15530typedef struct PreUpdate PreUpdate;
15531typedef struct PrintfArguments PrintfArguments;
15532typedef struct RCStr RCStr;
15533typedef struct RenameToken RenameToken;
15534typedef struct Returning Returning;
15535typedef struct RowSet RowSet;
15536typedef struct Savepoint Savepoint;
15537typedef struct Select Select;
15538typedef struct SQLiteThread SQLiteThread;
15539typedef struct SelectDest SelectDest;
15540typedef struct Subquery Subquery;
15541typedef struct SrcItem SrcItem;
15542typedef struct SrcList SrcList;
15543typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
15544typedef struct Table Table;
15545typedef struct TableLock TableLock;
15546typedef struct Token Token;
15547typedef struct TreeView TreeView;
15548typedef struct Trigger Trigger;
15549typedef struct TriggerPrg TriggerPrg;
15550typedef struct TriggerStep TriggerStep;
15551typedef struct UnpackedRecord UnpackedRecord;
15552typedef struct Upsert Upsert;
15553typedef struct VTable VTable;
15554typedef struct VtabCtx VtabCtx;
15555typedef struct Walker Walker;
15556typedef struct WhereInfo WhereInfo;
15557typedef struct Window Window;
15558typedef struct With With;
15559
15560
15561/*
15562** The bitmask datatype defined below is used for various optimizations.
15563**
15564** Changing this from a 64-bit to a 32-bit type limits the number of
15565** tables in a join to 32 instead of 64. But it also reduces the size
15566** of the library by 738 bytes on ix86.
15567*/
15568#ifdef SQLITE_BITMASK_TYPE
15569 typedef SQLITE_BITMASK_TYPE Bitmask;
15570#else
15571 typedef u64 Bitmask;
15572#endif
15573
15574/*
15575** The number of bits in a Bitmask. "BMS" means "BitMask Size".
15576*/
15577#define BMS((int)(sizeof(Bitmask)*8)) ((int)(sizeof(Bitmask)*8))
15578
15579/*
15580** A bit in a Bitmask
15581*/
15582#define MASKBIT(n)(((Bitmask)1)<<(n)) (((Bitmask)1)<<(n))
15583#define MASKBIT64(n)(((u64)1)<<(n)) (((u64)1)<<(n))
15584#define MASKBIT32(n)(((unsigned int)1)<<(n)) (((unsigned int)1)<<(n))
15585#define SMASKBIT32(n)((n)<=31?((unsigned int)1)<<(n):0) ((n)<=31?((unsigned int)1)<<(n):0)
15586#define ALLBITS((Bitmask)-1) ((Bitmask)-1)
15587#define TOPBIT(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) (((Bitmask)1)<<(BMS((int)(sizeof(Bitmask)*8))-1))
15588
15589/* A VList object records a mapping between parameters/variables/wildcards
15590** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
15591** variable number associated with that parameter. See the format description
15592** on the sqlite3VListAdd() routine for more information. A VList is really
15593** just an array of integers.
15594*/
15595typedef int VList;
15596
15597/*
15598** Defer sourcing vdbe.h and btree.h until after the "u8" and
15599** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
15600** pointer types (i.e. FuncDef) defined above.
15601*/
15602/************** Include os.h in the middle of sqliteInt.h ********************/
15603/************** Begin file os.h **********************************************/
15604/*
15605** 2001 September 16
15606**
15607** The author disclaims copyright to this source code. In place of
15608** a legal notice, here is a blessing:
15609**
15610** May you do good and not evil.
15611** May you find forgiveness for yourself and forgive others.
15612** May you share freely, never taking more than you give.
15613**
15614******************************************************************************
15615**
15616** This header file (together with is companion C source-code file
15617** "os.c") attempt to abstract the underlying operating system so that
15618** the SQLite library will work on both POSIX and windows systems.
15619**
15620** This header file is #include-ed by sqliteInt.h and thus ends up
15621** being included by every source file.
15622*/
15623#ifndef _SQLITE_OS_H_
15624#define _SQLITE_OS_H_
15625
15626/*
15627** Attempt to automatically detect the operating system and setup the
15628** necessary pre-processor macros for it.
15629*/
15630/************** Include os_setup.h in the middle of os.h *********************/
15631/************** Begin file os_setup.h ****************************************/
15632/*
15633** 2013 November 25
15634**
15635** The author disclaims copyright to this source code. In place of
15636** a legal notice, here is a blessing:
15637**
15638** May you do good and not evil.
15639** May you find forgiveness for yourself and forgive others.
15640** May you share freely, never taking more than you give.
15641**
15642******************************************************************************
15643**
15644** This file contains pre-processor directives related to operating system
15645** detection and/or setup.
15646*/
15647#ifndef SQLITE_OS_SETUP_H
15648#define SQLITE_OS_SETUP_H
15649
15650/*
15651** Figure out if we are dealing with Unix, Windows, or some other operating
15652** system.
15653**
15654** After the following block of preprocess macros, all of
15655**
15656** SQLITE_OS_KV
15657** SQLITE_OS_OTHER
15658** SQLITE_OS_UNIX
15659** SQLITE_OS_WIN
15660**
15661** will defined to either 1 or 0. One of them will be 1. The others will be 0.
15662** If none of the macros are initially defined, then select either
15663** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform.
15664**
15665** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15666** must provide its own VFS implementation together with sqlite3_os_init()
15667** and sqlite3_os_end() routines.
15668*/
15669#if !defined(SQLITE_OS_KV0) && !defined(SQLITE_OS_OTHER0) && \
15670 !defined(SQLITE_OS_UNIX1) && !defined(SQLITE_OS_WIN0)
15671# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15672 defined(__MINGW32__) || defined(__BORLANDC__)
15673# define SQLITE_OS_WIN0 1
15674# define SQLITE_OS_UNIX1 0
15675# else
15676# define SQLITE_OS_WIN0 0
15677# define SQLITE_OS_UNIX1 1
15678# endif
15679#endif
15680#if SQLITE_OS_OTHER0+1>1
15681# undef SQLITE_OS_KV0
15682# define SQLITE_OS_KV0 0
15683# undef SQLITE_OS_UNIX1
15684# define SQLITE_OS_UNIX1 0
15685# undef SQLITE_OS_WIN0
15686# define SQLITE_OS_WIN0 0
15687#endif
15688#if SQLITE_OS_KV0+1>1
15689# undef SQLITE_OS_OTHER0
15690# define SQLITE_OS_OTHER0 0
15691# undef SQLITE_OS_UNIX1
15692# define SQLITE_OS_UNIX1 0
15693# undef SQLITE_OS_WIN0
15694# define SQLITE_OS_WIN0 0
15695# define SQLITE_OMIT_LOAD_EXTENSION1 1
15696# define SQLITE_OMIT_WAL 1
15697# define SQLITE_OMIT_DEPRECATED1 1
15698# undef SQLITE_TEMP_STORE1
15699# define SQLITE_TEMP_STORE1 3 /* Always use memory for temporary storage */
15700# define SQLITE_DQS0 0
15701# define SQLITE_OMIT_SHARED_CACHE 1
15702# define SQLITE_OMIT_AUTOINIT 1
15703#endif
15704#if SQLITE_OS_UNIX1+1>1
15705# undef SQLITE_OS_KV0
15706# define SQLITE_OS_KV0 0
15707# undef SQLITE_OS_OTHER0
15708# define SQLITE_OS_OTHER0 0
15709# undef SQLITE_OS_WIN0
15710# define SQLITE_OS_WIN0 0
15711#endif
15712#if SQLITE_OS_WIN0+1>1
15713# undef SQLITE_OS_KV0
15714# define SQLITE_OS_KV0 0
15715# undef SQLITE_OS_OTHER0
15716# define SQLITE_OS_OTHER0 0
15717# undef SQLITE_OS_UNIX1
15718# define SQLITE_OS_UNIX1 0
15719#endif
15720
15721
15722#endif /* SQLITE_OS_SETUP_H */
15723
15724/************** End of os_setup.h ********************************************/
15725/************** Continuing where we left off in os.h *************************/
15726
15727/* If the SET_FULLSYNC macro is not defined above, then make it
15728** a no-op
15729*/
15730#ifndef SET_FULLSYNC
15731# define SET_FULLSYNC(x,y)
15732#endif
15733
15734/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
15735*/
15736#ifndef SQLITE_MAX_PATHLEN4096
15737# define SQLITE_MAX_PATHLEN4096 FILENAME_MAX4096
15738#endif
15739
15740/* Maximum number of symlinks that will be resolved while trying to
15741** expand a filename in xFullPathname() in the VFS.
15742*/
15743#ifndef SQLITE_MAX_SYMLINK200
15744# define SQLITE_MAX_SYMLINK200 200
15745#endif
15746
15747/*
15748** The default size of a disk sector
15749*/
15750#ifndef SQLITE_DEFAULT_SECTOR_SIZE4096
15751# define SQLITE_DEFAULT_SECTOR_SIZE4096 4096
15752#endif
15753
15754/*
15755** Temporary files are named starting with this prefix followed by 16 random
15756** alphanumeric characters, and no file extension. They are stored in the
15757** OS's standard temporary file directory, and are deleted prior to exit.
15758** If sqlite is being embedded in another program, you may wish to change the
15759** prefix to reflect your program's name, so that if your program exits
15760** prematurely, old temporary files can be easily identified. This can be done
15761** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15762**
15763** 2006-10-31: The default prefix used to be "sqlite_". But then
15764** Mcafee started using SQLite in their anti-virus product and it
15765** started putting files with the "sqlite" name in the c:/temp folder.
15766** This annoyed many windows users. Those users would then do a
15767** Google search for "sqlite", find the telephone numbers of the
15768** developers and call to wake them up at night and complain.
15769** For this reason, the default name prefix is changed to be "sqlite"
15770** spelled backwards. So the temp files are still identified, but
15771** anybody smart enough to figure out the code is also likely smart
15772** enough to know that calling the developer will not help get rid
15773** of the file.
15774*/
15775#ifndef SQLITE_TEMP_FILE_PREFIX"etilqs_"
15776# define SQLITE_TEMP_FILE_PREFIX"etilqs_" "etilqs_"
15777#endif
15778
15779/*
15780** The following values may be passed as the second argument to
15781** sqlite3OsLock(). The various locks exhibit the following semantics:
15782**
15783** SHARED: Any number of processes may hold a SHARED lock simultaneously.
15784** RESERVED: A single process may hold a RESERVED lock on a file at
15785** any time. Other processes may hold and obtain new SHARED locks.
15786** PENDING: A single process may hold a PENDING lock on a file at
15787** any one time. Existing SHARED locks may persist, but no new
15788** SHARED locks may be obtained by other processes.
15789** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
15790**
15791** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
15792** process that requests an EXCLUSIVE lock may actually obtain a PENDING
15793** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
15794** sqlite3OsLock().
15795*/
15796#define NO_LOCK0 0
15797#define SHARED_LOCK1 1
15798#define RESERVED_LOCK2 2
15799#define PENDING_LOCK3 3
15800#define EXCLUSIVE_LOCK4 4
15801
15802/*
15803** File Locking Notes: (Mostly about windows but also some info for Unix)
15804**
15805** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
15806** those functions are not available. So we use only LockFile() and
15807** UnlockFile().
15808**
15809** LockFile() prevents not just writing but also reading by other processes.
15810** A SHARED_LOCK is obtained by locking a single randomly-chosen
15811** byte out of a specific range of bytes. The lock byte is obtained at
15812** random so two separate readers can probably access the file at the
15813** same time, unless they are unlucky and choose the same lock byte.
15814** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
15815** There can only be one writer. A RESERVED_LOCK is obtained by locking
15816** a single byte of the file that is designated as the reserved lock byte.
15817** A PENDING_LOCK is obtained by locking a designated byte different from
15818** the RESERVED_LOCK byte.
15819**
15820** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
15821** which means we can use reader/writer locks. When reader/writer locks
15822** are used, the lock is placed on the same range of bytes that is used
15823** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
15824** will support two or more Win95 readers or two or more WinNT readers.
15825** But a single Win95 reader will lock out all WinNT readers and a single
15826** WinNT reader will lock out all other Win95 readers.
15827**
15828** The following #defines specify the range of bytes used for locking.
15829** SHARED_SIZE is the number of bytes available in the pool from which
15830** a random byte is selected for a shared lock. The pool of bytes for
15831** shared locks begins at SHARED_FIRST.
15832**
15833** The same locking strategy and
15834** byte ranges are used for Unix. This leaves open the possibility of having
15835** clients on win95, winNT, and unix all talking to the same shared file
15836** and all locking correctly. To do so would require that samba (or whatever
15837** tool is being used for file sharing) implements locks correctly between
15838** windows and unix. I'm guessing that isn't likely to happen, but by
15839** using the same locking range we are at least open to the possibility.
15840**
15841** Locking in windows is manditory. For this reason, we cannot store
15842** actual data in the bytes used for locking. The pager never allocates
15843** the pages involved in locking therefore. SHARED_SIZE is selected so
15844** that all locks will fit on a single page even at the minimum page size.
15845** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
15846** is set high so that we don't have to allocate an unused page except
15847** for very large databases. But one should test the page skipping logic
15848** by setting PENDING_BYTE low and running the entire regression suite.
15849**
15850** Changing the value of PENDING_BYTE results in a subtly incompatible
15851** file format. Depending on how it is changed, you might not notice
15852** the incompatibility right away, even running a full regression test.
15853** The default location of PENDING_BYTE is the first byte past the
15854** 1GB boundary.
15855**
15856*/
15857#ifdef SQLITE_OMIT_WSD
15858# define PENDING_BYTEsqlite3PendingByte (0x40000000)
15859#else
15860# define PENDING_BYTEsqlite3PendingByte sqlite3PendingByte
15861#endif
15862#define RESERVED_BYTE(sqlite3PendingByte+1) (PENDING_BYTEsqlite3PendingByte+1)
15863#define SHARED_FIRST(sqlite3PendingByte+2) (PENDING_BYTEsqlite3PendingByte+2)
15864#define SHARED_SIZE510 510
15865
15866/*
15867** Wrapper around OS specific sqlite3_os_init() function.
15868*/
15869SQLITE_PRIVATEstatic int sqlite3OsInit(void);
15870
15871/*
15872** Functions for accessing sqlite3_file methods
15873*/
15874SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file*);
15875SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15876SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15877SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file*, i64 size);
15878SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file*, int);
15879SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15880SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file*, int);
15881SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file*, int);
15882SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15883SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file*,int,void*);
15884SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15885#define SQLITE_FCNTL_DB_UNCHANGED0xca093fa0 0xca093fa0
15886SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id);
15887SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15888#ifndef SQLITE_OMIT_WAL
15889SQLITE_PRIVATEstatic int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15890SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15891SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id);
15892SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int);
15893#endif /* SQLITE_OMIT_WAL */
15894SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15895SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15896
15897
15898/*
15899** Functions for accessing sqlite3_vfs methods
15900*/
15901SQLITE_PRIVATEstatic int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15902SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15903SQLITE_PRIVATEstatic int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15904SQLITE_PRIVATEstatic int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15905#ifndef SQLITE_OMIT_LOAD_EXTENSION1
15906SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
15907SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15908SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
15909SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *, void *);
15910#endif /* SQLITE_OMIT_LOAD_EXTENSION */
15911SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15912SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *, int);
15913SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs*);
15914SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15915
15916/*
15917** Convenience functions for opening and closing files using
15918** sqlite3_malloc() to obtain space for the file-handle structure.
15919*/
15920SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15921SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *);
15922
15923#endif /* _SQLITE_OS_H_ */
15924
15925/************** End of os.h **************************************************/
15926/************** Continuing where we left off in sqliteInt.h ******************/
15927/************** Include pager.h in the middle of sqliteInt.h *****************/
15928/************** Begin file pager.h *******************************************/
15929/*
15930** 2001 September 15
15931**
15932** The author disclaims copyright to this source code. In place of
15933** a legal notice, here is a blessing:
15934**
15935** May you do good and not evil.
15936** May you find forgiveness for yourself and forgive others.
15937** May you share freely, never taking more than you give.
15938**
15939*************************************************************************
15940** This header file defines the interface that the sqlite page cache
15941** subsystem. The page cache subsystem reads and writes a file a page
15942** at a time and provides a journal for rollback.
15943*/
15944
15945#ifndef SQLITE_PAGER_H
15946#define SQLITE_PAGER_H
15947
15948/*
15949** Default maximum size for persistent journal files. A negative
15950** value means no limit. This value may be overridden using the
15951** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
15952*/
15953#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768
15954 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768 -1
15955#endif
15956
15957/*
15958** The type used to represent a page number. The first page in a file
15959** is called page 1. 0 is used to represent "not a page".
15960*/
15961typedef u32 Pgno;
15962
15963/*
15964** Each open file is managed by a separate instance of the "Pager" structure.
15965*/
15966typedef struct Pager Pager;
15967
15968/*
15969** Handle type for pages.
15970*/
15971typedef struct PgHdr DbPage;
15972
15973/*
15974** Page number PAGER_SJ_PGNO is never used in an SQLite database (it is
15975** reserved for working around a windows/posix incompatibility). It is
15976** used in the journal to signify that the remainder of the journal file
15977** is devoted to storing a super-journal name - there are no more pages to
15978** roll back. See comments for function writeSuperJournal() in pager.c
15979** for details.
15980*/
15981#define PAGER_SJ_PGNO_COMPUTED(x)((Pgno)((sqlite3PendingByte/((x)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((x)->pageSize))+1))
15982#define PAGER_SJ_PGNO(x)((x)->lckPgno) ((x)->lckPgno)
15983
15984/*
15985** Allowed values for the flags parameter to sqlite3PagerOpen().
15986**
15987** NOTE: These values must match the corresponding BTREE_ values in btree.h.
15988*/
15989#define PAGER_OMIT_JOURNAL0x0001 0x0001 /* Do not use a rollback journal */
15990#define PAGER_MEMORY0x0002 0x0002 /* In-memory database */
15991
15992/*
15993** Valid values for the second argument to sqlite3PagerLockingMode().
15994*/
15995#define PAGER_LOCKINGMODE_QUERY-1 -1
15996#define PAGER_LOCKINGMODE_NORMAL0 0
15997#define PAGER_LOCKINGMODE_EXCLUSIVE1 1
15998
15999/*
16000** Numeric constants that encode the journalmode.
16001**
16002** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
16003** are exposed in the API via the "PRAGMA journal_mode" command and
16004** therefore cannot be changed without a compatibility break.
16005*/
16006#define PAGER_JOURNALMODE_QUERY(-1) (-1) /* Query the value of journalmode */
16007#define PAGER_JOURNALMODE_DELETE0 0 /* Commit by deleting journal file */
16008#define PAGER_JOURNALMODE_PERSIST1 1 /* Commit by zeroing journal header */
16009#define PAGER_JOURNALMODE_OFF2 2 /* Journal omitted. */
16010#define PAGER_JOURNALMODE_TRUNCATE3 3 /* Commit by truncating journal */
16011#define PAGER_JOURNALMODE_MEMORY4 4 /* In-memory journal file */
16012#define PAGER_JOURNALMODE_WAL5 5 /* Use write-ahead logging */
16013
16014/*
16015** Flags that make up the mask passed to sqlite3PagerGet().
16016*/
16017#define PAGER_GET_NOCONTENT0x01 0x01 /* Do not load data from disk */
16018#define PAGER_GET_READONLY0x02 0x02 /* Read-only page is acceptable */
16019
16020/*
16021** Flags for sqlite3PagerSetFlags()
16022**
16023** Value constraints (enforced via assert()):
16024** PAGER_FULLFSYNC == SQLITE_FullFSync
16025** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
16026** PAGER_CACHE_SPILL == SQLITE_CacheSpill
16027*/
16028#define PAGER_SYNCHRONOUS_OFF0x01 0x01 /* PRAGMA synchronous=OFF */
16029#define PAGER_SYNCHRONOUS_NORMAL0x02 0x02 /* PRAGMA synchronous=NORMAL */
16030#define PAGER_SYNCHRONOUS_FULL0x03 0x03 /* PRAGMA synchronous=FULL */
16031#define PAGER_SYNCHRONOUS_EXTRA0x04 0x04 /* PRAGMA synchronous=EXTRA */
16032#define PAGER_SYNCHRONOUS_MASK0x07 0x07 /* Mask for four values above */
16033#define PAGER_FULLFSYNC0x08 0x08 /* PRAGMA fullfsync=ON */
16034#define PAGER_CKPT_FULLFSYNC0x10 0x10 /* PRAGMA checkpoint_fullfsync=ON */
16035#define PAGER_CACHESPILL0x20 0x20 /* PRAGMA cache_spill=ON */
16036#define PAGER_FLAGS_MASK0x38 0x38 /* All above except SYNCHRONOUS */
16037
16038/*
16039** The remainder of this file contains the declarations of the functions
16040** that make up the Pager sub-system API. See source code comments for
16041** a detailed description of each routine.
16042*/
16043
16044/* Open and close a Pager connection. */
16045SQLITE_PRIVATEstatic int sqlite3PagerOpen(
16046 sqlite3_vfs*,
16047 Pager **ppPager,
16048 const char*,
16049 int,
16050 int,
16051 int,
16052 void(*)(DbPage*)
16053);
16054SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3*);
16055SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
16056
16057/* Functions used to configure a Pager object. */
16058SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
16059SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager*, u32*, int);
16060SQLITE_PRIVATEstatic Pgno sqlite3PagerMaxPageCount(Pager*, Pgno);
16061SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager*, int);
16062SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager*, int);
16063SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
16064SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager*);
16065SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(Pager*,unsigned);
16066SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *, int);
16067SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *, int);
16068SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager*);
16069SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager*);
16070SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
16071SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
16072SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager*);
16073
16074/* Functions used to obtain and release page references. */
16075SQLITE_PRIVATEstatic int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
16076SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
16077SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage*);
16078SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage*);
16079SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage*);
16080SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage*);
16081
16082/* Operations on page references. */
16083SQLITE_PRIVATEstatic int sqlite3PagerWrite(DbPage*);
16084SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(DbPage*);
16085SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
16086SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage*);
16087SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *);
16088SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *);
16089
16090/* Functions used to manage pager transactions and savepoints. */
16091SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager*, int*);
16092SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager*, int exFlag, int);
16093SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int);
16094SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager*);
16095SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zSuper);
16096SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager*);
16097SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager*);
16098SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
16099SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
16100SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager);
16101
16102#ifndef SQLITE_OMIT_WAL
16103SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
16104SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager);
16105SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager);
16106SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
16107SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
16108# ifdef SQLITE_ENABLE_SNAPSHOT1
16109SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot);
16110SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot);
16111SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager);
16112SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
16113SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager);
16114# endif
16115#endif
16116
16117#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT)
16118SQLITE_PRIVATEstatic int sqlite3PagerWalWriteLock(Pager*, int)0;
16119SQLITE_PRIVATEstatic void sqlite3PagerWalDb(Pager*, sqlite3*);
16120#else
16121# define sqlite3PagerWalWriteLock(y,z)0 SQLITE_OK0
16122# define sqlite3PagerWalDb(x,y)
16123#endif
16124
16125#ifdef SQLITE_DIRECT_OVERFLOW_READ1
16126SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
16127#endif
16128
16129#ifdef SQLITE_ENABLE_ZIPVFS
16130SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager);
16131#endif
16132
16133/* Functions used to query pager state and configuration. */
16134SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager*);
16135SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager*);
16136#ifdef SQLITE_DEBUG
16137SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager*);
16138#endif
16139SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager*);
16140SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(const Pager*, int);
16141SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager*);
16142SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager*);
16143SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager*);
16144SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager*);
16145SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager*);
16146SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager*);
16147SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *, int, int, u64*);
16148SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager*);
16149SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *);
16150
16151/* Functions used to truncate the database file. */
16152SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager*,Pgno);
16153
16154SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage*, Pgno, u16);
16155
16156/* Functions to support testing and debugging. */
16157#if !defined(NDEBUG1) || defined(SQLITE_TEST)
16158SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage*);
16159SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage*);
16160#endif
16161#ifdef SQLITE_TEST
16162SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager*);
16163SQLITE_PRIVATEstatic void sqlite3PagerRefdump(Pager*);
16164 void disable_simulated_io_errors(void);
16165 void enable_simulated_io_errors(void);
16166#else
16167# define disable_simulated_io_errors()
16168# define enable_simulated_io_errors()
16169#endif
16170
16171#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
16172SQLITE_PRIVATEstatic int sqlite3PagerWalSystemErrno(Pager*);
16173#endif
16174
16175#endif /* SQLITE_PAGER_H */
16176
16177/************** End of pager.h ***********************************************/
16178/************** Continuing where we left off in sqliteInt.h ******************/
16179/************** Include btree.h in the middle of sqliteInt.h *****************/
16180/************** Begin file btree.h *******************************************/
16181/*
16182** 2001 September 15
16183**
16184** The author disclaims copyright to this source code. In place of
16185** a legal notice, here is a blessing:
16186**
16187** May you do good and not evil.
16188** May you find forgiveness for yourself and forgive others.
16189** May you share freely, never taking more than you give.
16190**
16191*************************************************************************
16192** This header file defines the interface that the sqlite B-Tree file
16193** subsystem. See comments in the source code for a detailed description
16194** of what each interface routine does.
16195*/
16196#ifndef SQLITE_BTREE_H
16197#define SQLITE_BTREE_H
16198
16199/* TODO: This definition is just included so other modules compile. It
16200** needs to be revisited.
16201*/
16202#define SQLITE_N_BTREE_META16 16
16203
16204/*
16205** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
16206** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
16207*/
16208#ifndef SQLITE_DEFAULT_AUTOVACUUM0
16209 #define SQLITE_DEFAULT_AUTOVACUUM0 0
16210#endif
16211
16212#define BTREE_AUTOVACUUM_NONE0 0 /* Do not do auto-vacuum */
16213#define BTREE_AUTOVACUUM_FULL1 1 /* Do full auto-vacuum */
16214#define BTREE_AUTOVACUUM_INCR2 2 /* Incremental vacuum */
16215
16216/*
16217** Forward declarations of structure
16218*/
16219typedef struct Btree Btree;
16220typedef struct BtCursor BtCursor;
16221typedef struct BtShared BtShared;
16222typedef struct BtreePayload BtreePayload;
16223
16224
16225SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
16226 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
16227 const char *zFilename, /* Name of database file to open */
16228 sqlite3 *db, /* Associated database connection */
16229 Btree **ppBtree, /* Return open Btree* here */
16230 int flags, /* Flags */
16231 int vfsFlags /* Flags passed through to VFS open */
16232);
16233
16234/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
16235** following values.
16236**
16237** NOTE: These values must match the corresponding PAGER_ values in
16238** pager.h.
16239*/
16240#define BTREE_OMIT_JOURNAL1 1 /* Do not create or use a rollback journal */
16241#define BTREE_MEMORY2 2 /* This is an in-memory DB */
16242#define BTREE_SINGLE4 4 /* The file contains at most 1 b-tree */
16243#define BTREE_UNORDERED8 8 /* Use of a hash implementation is OK */
16244
16245SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree*);
16246SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree*,int);
16247SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree*,int);
16248#if SQLITE_MAX_MMAP_SIZE20971520>0
16249SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
16250#endif
16251SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
16252SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
16253SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree*);
16254SQLITE_PRIVATEstatic Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
16255SQLITE_PRIVATEstatic Pgno sqlite3BtreeLastPage(Btree*);
16256SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree*,int);
16257SQLITE_PRIVATEstatic int sqlite3BtreeGetRequestedReserve(Btree*);
16258SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p);
16259SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *, int);
16260SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *);
16261SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree*,int,int*);
16262SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree*, const char*);
16263SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree*, int);
16264SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree*);
16265SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree*,int,int);
16266SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree*,int);
16267SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
16268SQLITE_PRIVATEstatic int sqlite3BtreeTxnState(Btree*);
16269SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree*);
16270
16271SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
16272SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *pBtree);
16273#ifndef SQLITE_OMIT_SHARED_CACHE
16274SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
16275#endif
16276
16277/* Savepoints are named, nestable SQL transactions mostly implemented */
16278/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */
16279SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *, int, int);
16280
16281/* "Checkpoint" only refers to WAL. See https://sqlite.org/wal.html#ckpt */
16282#ifndef SQLITE_OMIT_WAL
16283SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16284#endif
16285
16286SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *);
16287SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *);
16288SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *, Btree *);
16289
16290SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *);
16291
16292/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
16293** of the flags shown below.
16294**
16295** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
16296** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
16297** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
16298** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
16299** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
16300** indices.)
16301*/
16302#define BTREE_INTKEY1 1 /* Table has only 64-bit signed integer keys */
16303#define BTREE_BLOBKEY2 2 /* Table has keys only - no data */
16304
16305SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree*, int, int*);
16306SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree*, int, i64*);
16307SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor*);
16308SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree*, int, int);
16309
16310SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
16311SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
16312
16313SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p);
16314
16315/*
16316** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
16317** should be one of the following values. The integer values are assigned
16318** to constants so that the offset of the corresponding field in an
16319** SQLite database header may be found using the following formula:
16320**
16321** offset = 36 + (idx * 4)
16322**
16323** For example, the free-page-count field is located at byte offset 36 of
16324** the database file header. The incr-vacuum-flag field is located at
16325** byte offset 64 (== 36+4*7).
16326**
16327** The BTREE_DATA_VERSION value is not really a value stored in the header.
16328** It is a read-only number computed by the pager. But we merge it with
16329** the header value access routines since its access pattern is the same.
16330** Call it a "virtual meta value".
16331*/
16332#define BTREE_FREE_PAGE_COUNT0 0
16333#define BTREE_SCHEMA_VERSION1 1
16334#define BTREE_FILE_FORMAT2 2
16335#define BTREE_DEFAULT_CACHE_SIZE3 3
16336#define BTREE_LARGEST_ROOT_PAGE4 4
16337#define BTREE_TEXT_ENCODING5 5
16338#define BTREE_USER_VERSION6 6
16339#define BTREE_INCR_VACUUM7 7
16340#define BTREE_APPLICATION_ID8 8
16341#define BTREE_DATA_VERSION15 15 /* A virtual meta-value */
16342
16343/*
16344** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
16345** interface.
16346**
16347** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
16348**
16349** The first argument is an Expr* (which is guaranteed to be constant for
16350** the lifetime of the cursor) that defines constraints on which rows
16351** might be fetched with this cursor. The Expr* tree may contain
16352** TK_REGISTER nodes that refer to values stored in the array of registers
16353** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
16354** then the value of the node is the value in Mem[pExpr.iTable]. Any
16355** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
16356** column of the b-tree of the cursor. The Expr tree will not contain
16357** any function calls nor subqueries nor references to b-trees other than
16358** the cursor being hinted.
16359**
16360** The design of the _RANGE hint is aid b-tree implementations that try
16361** to prefetch content from remote machines - to provide those
16362** implementations with limits on what needs to be prefetched and thereby
16363** reduce network bandwidth.
16364**
16365** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
16366** standard SQLite. The other hints are provided for extensions that use
16367** the SQLite parser and code generator but substitute their own storage
16368** engine.
16369*/
16370#define BTREE_HINT_RANGE0 0 /* Range constraints on queries */
16371
16372/*
16373** Values that may be OR'd together to form the argument to the
16374** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
16375**
16376** The BTREE_BULKLOAD flag is set on index cursors when the index is going
16377** to be filled with content that is already in sorted order.
16378**
16379** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
16380** OP_SeekLE opcodes for a range search, but where the range of entries
16381** selected will all have the same key. In other words, the cursor will
16382** be used only for equality key searches.
16383**
16384*/
16385#define BTREE_BULKLOAD0x00000001 0x00000001 /* Used to full index in sorted order */
16386#define BTREE_SEEK_EQ0x00000002 0x00000002 /* EQ seeks only - no range seeks */
16387
16388/*
16389** Flags passed as the third argument to sqlite3BtreeCursor().
16390**
16391** For read-only cursors the wrFlag argument is always zero. For read-write
16392** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
16393** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
16394** only be used by SQLite for the following:
16395**
16396** * to seek to and then delete specific entries, and/or
16397**
16398** * to read values that will be used to create keys that other
16399** BTREE_FORDELETE cursors will seek to and delete.
16400**
16401** The BTREE_FORDELETE flag is an optimization hint. It is not used by
16402** by this, the native b-tree engine of SQLite, but it is available to
16403** alternative storage engines that might be substituted in place of this
16404** b-tree system. For alternative storage engines in which a delete of
16405** the main table row automatically deletes corresponding index rows,
16406** the FORDELETE flag hint allows those alternative storage engines to
16407** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
16408** and DELETE operations as no-ops, and any READ operation against a
16409** FORDELETE cursor may return a null row: 0x01 0x00.
16410*/
16411#define BTREE_WRCSR0x00000004 0x00000004 /* read-write cursor */
16412#define BTREE_FORDELETE0x00000008 0x00000008 /* Cursor is for seek/delete only */
16413
16414SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
16415 Btree*, /* BTree containing table to open */
16416 Pgno iTable, /* Index of root page */
16417 int wrFlag, /* 1 for writing. 0 for read-only */
16418 struct KeyInfo*, /* First argument to compare function */
16419 BtCursor *pCursor /* Space to write cursor structure */
16420);
16421SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void);
16422SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void);
16423#ifdef SQLITE_DEBUG
16424SQLITE_PRIVATEstatic int sqlite3BtreeClosesWithCursor(Btree*,BtCursor*);
16425#endif
16426SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor*);
16427SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
16428#ifdef SQLITE_ENABLE_CURSOR_HINTS
16429SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor*, int, ...);
16430#endif
16431
16432SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor*);
16433SQLITE_PRIVATEstatic int sqlite3BtreeTableMoveto(
16434 BtCursor*,
16435 i64 intKey,
16436 int bias,
16437 int *pRes
16438);
16439SQLITE_PRIVATEstatic int sqlite3BtreeIndexMoveto(
16440 BtCursor*,
16441 UnpackedRecord *pUnKey,
16442 int *pRes
16443);
16444SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor*);
16445SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor*, int*);
16446SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor*, u8 flags);
16447
16448/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
16449#define BTREE_SAVEPOSITION0x02 0x02 /* Leave cursor pointing at NEXT or PREV */
16450#define BTREE_AUXDELETE0x04 0x04 /* not the primary delete operation */
16451#define BTREE_APPEND0x08 0x08 /* Insert is likely an append */
16452#define BTREE_PREFORMAT0x80 0x80 /* Inserted data is a preformated cell */
16453
16454/* An instance of the BtreePayload object describes the content of a single
16455** entry in either an index or table btree.
16456**
16457** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
16458** an arbitrary key and no data. These btrees have pKey,nKey set to the
16459** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
16460** fields give an array of Mem objects that are a decomposition of the key.
16461** The nMem field might be zero, indicating that no decomposition is available.
16462**
16463** Table btrees (used for rowid tables) contain an integer rowid used as
16464** the key and passed in the nKey field. The pKey field is zero.
16465** pData,nData hold the content of the new entry. nZero extra zero bytes
16466** are appended to the end of the content when constructing the entry.
16467** The aMem,nMem fields are uninitialized for table btrees.
16468**
16469** Field usage summary:
16470**
16471** Table BTrees Index Btrees
16472**
16473** pKey always NULL encoded key
16474** nKey the ROWID length of pKey
16475** pData data not used
16476** aMem not used decomposed key value
16477** nMem not used entries in aMem
16478** nData length of pData not used
16479** nZero extra zeros after pData not used
16480**
16481** This object is used to pass information into sqlite3BtreeInsert(). The
16482** same information used to be passed as five separate parameters. But placing
16483** the information into this object helps to keep the interface more
16484** organized and understandable, and it also helps the resulting code to
16485** run a little faster by using fewer registers for parameter passing.
16486*/
16487struct BtreePayload {
16488 const void *pKey; /* Key content for indexes. NULL for tables */
16489 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
16490 const void *pData; /* Data for tables. */
16491 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
16492 u16 nMem; /* Number of aMem[] value. Might be zero */
16493 int nData; /* Size of pData. 0 if none. */
16494 int nZero; /* Extra zero data appended after pData,nData */
16495};
16496
16497SQLITE_PRIVATEstatic int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
16498 int flags, int seekResult);
16499SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor*, int *pRes);
16500SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor*, int *pRes);
16501SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor*, int flags);
16502SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor*);
16503SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor*, int flags);
16504SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor*);
16505SQLITE_PRIVATEstatic void sqlite3BtreeCursorPin(BtCursor*);
16506SQLITE_PRIVATEstatic void sqlite3BtreeCursorUnpin(BtCursor*);
16507SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor*);
16508SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
16509SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
16510SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor*);
16511SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
16512
16513SQLITE_PRIVATEstatic int sqlite3BtreeIntegrityCheck(
16514 sqlite3 *db, /* Database connection that is running the check */
16515 Btree *p, /* The btree to be checked */
16516 Pgno *aRoot, /* An array of root pages numbers for individual trees */
16517 sqlite3_value *aCnt, /* OUT: entry counts for each btree in aRoot[] */
16518 int nRoot, /* Number of entries in aRoot[] */
16519 int mxErr, /* Stop reporting errors after this many */
16520 int *pnErr, /* OUT: Write number of errors seen to this variable */
16521 char **pzOut /* OUT: Write the error message string here */
16522);
16523SQLITE_PRIVATEstatic struct Pager *sqlite3BtreePager(Btree*);
16524SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor*);
16525
16526#ifndef SQLITE_OMIT_INCRBLOB
16527SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
16528SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
16529SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *);
16530#endif
16531SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *);
16532SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
16533SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
16534SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *pBt);
16535SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void);
16536
16537#ifdef SQLITE_DEBUG
16538SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3BtreeSeekCount(Btree*)0;
16539#else
16540# define sqlite3BtreeSeekCount(X)0 0
16541#endif
16542
16543#ifndef NDEBUG1
16544SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor*);
16545#endif
16546SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor*);
16547
16548SQLITE_PRIVATEstatic int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
16549
16550#ifdef SQLITE_TEST
16551SQLITE_PRIVATEstatic int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
16552SQLITE_PRIVATEstatic void sqlite3BtreeCursorList(Btree*);
16553#endif
16554
16555#ifndef SQLITE_OMIT_WAL
16556SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
16557#endif
16558
16559SQLITE_PRIVATEstatic int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
16560
16561SQLITE_PRIVATEstatic void sqlite3BtreeClearCache(Btree*);
16562
16563/*
16564** If we are not using shared cache, then there is no need to
16565** use mutexes to access the BtShared structures. So make the
16566** Enter and Leave procedures no-ops.
16567*/
16568#ifndef SQLITE_OMIT_SHARED_CACHE
16569SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree*);
16570SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3*);
16571SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree*);
16572SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor*);
16573SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree*);
16574#else
16575# define sqlite3BtreeEnter(X)
16576# define sqlite3BtreeEnterAll(X)
16577# define sqlite3BtreeSharable(X) 0
16578# define sqlite3BtreeEnterCursor(X)
16579# define sqlite3BtreeConnectionCount(X) 1
16580#endif
16581
16582#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2
16583SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree*);
16584SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor*);
16585SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3*);
16586#ifndef NDEBUG1
16587 /* These routines are used inside assert() statements only. */
16588SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree*);
16589SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3*);
16590SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
16591#endif
16592#else
16593
16594# define sqlite3BtreeLeave(X)
16595# define sqlite3BtreeLeaveCursor(X)
16596# define sqlite3BtreeLeaveAll(X)
16597
16598# define sqlite3BtreeHoldsMutex(X) 1
16599# define sqlite3BtreeHoldsAllMutexes(X) 1
16600# define sqlite3SchemaMutexHeld(X,Y,Z) 1
16601#endif
16602
16603
16604#endif /* SQLITE_BTREE_H */
16605
16606/************** End of btree.h ***********************************************/
16607/************** Continuing where we left off in sqliteInt.h ******************/
16608/************** Include vdbe.h in the middle of sqliteInt.h ******************/
16609/************** Begin file vdbe.h ********************************************/
16610/*
16611** 2001 September 15
16612**
16613** The author disclaims copyright to this source code. In place of
16614** a legal notice, here is a blessing:
16615**
16616** May you do good and not evil.
16617** May you find forgiveness for yourself and forgive others.
16618** May you share freely, never taking more than you give.
16619**
16620*************************************************************************
16621** Header file for the Virtual DataBase Engine (VDBE)
16622**
16623** This header defines the interface to the virtual database engine
16624** or VDBE. The VDBE implements an abstract machine that runs a
16625** simple program to access and modify the underlying database.
16626*/
16627#ifndef SQLITE_VDBE_H
16628#define SQLITE_VDBE_H
16629/* #include <stdio.h> */
16630
16631/*
16632** A single VDBE is an opaque structure named "Vdbe". Only routines
16633** in the source file sqliteVdbe.c are allowed to see the insides
16634** of this structure.
16635*/
16636typedef struct Vdbe Vdbe;
16637
16638/*
16639** The names of the following types declared in vdbeInt.h are required
16640** for the VdbeOp definition.
16641*/
16642typedef struct sqlite3_value Mem;
16643typedef struct SubProgram SubProgram;
16644typedef struct SubrtnSig SubrtnSig;
16645
16646/*
16647** A signature for a reusable subroutine that materializes the RHS of
16648** an IN operator.
16649*/
16650struct SubrtnSig {
16651 int selId; /* SELECT-id for the SELECT statement on the RHS */
16652 char *zAff; /* Affinity of the overall IN expression */
16653 int iTable; /* Ephemeral table generated by the subroutine */
16654 int iAddr; /* Subroutine entry address */
16655 int regReturn; /* Register used to hold return address */
16656};
16657
16658/*
16659** A single instruction of the virtual machine has an opcode
16660** and as many as three operands. The instruction is recorded
16661** as an instance of the following structure:
16662*/
16663struct VdbeOp {
16664 u8 opcode; /* What operation to perform */
16665 signed char p4type; /* One of the P4_xxx constants for p4 */
16666 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16667 int p1; /* First operand */
16668 int p2; /* Second parameter (often the jump destination) */
16669 int p3; /* The third parameter */
16670 union p4union { /* fourth parameter */
16671 int i; /* Integer value if p4type==P4_INT32 */
16672 void *p; /* Generic pointer */
16673 char *z; /* Pointer to data for string (char array) types */
16674 i64 *pI64; /* Used when p4type is P4_INT64 */
16675 double *pReal; /* Used when p4type is P4_REAL */
16676 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
16677 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
16678 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
16679 Mem *pMem; /* Used when p4type is P4_MEM */
16680 VTable *pVtab; /* Used when p4type is P4_VTAB */
16681 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
16682 u32 *ai; /* Used when p4type is P4_INTARRAY */
16683 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
16684 Table *pTab; /* Used when p4type is P4_TABLE */
16685 SubrtnSig *pSubrtnSig; /* Used when p4type is P4_SUBRTNSIG */
16686#ifdef SQLITE_ENABLE_CURSOR_HINTS
16687 Expr *pExpr; /* Used when p4type is P4_EXPR */
16688#endif
16689 } p4;
16690#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
16691 char *zComment; /* Comment to improve readability */
16692#endif
16693#ifdef SQLITE_VDBE_COVERAGE
16694 u32 iSrcLine; /* Source-code line that generated this opcode
16695 ** with flags in the upper 8 bits */
16696#endif
16697#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
16698 u64 nExec;
16699 u64 nCycle;
16700#endif
16701};
16702typedef struct VdbeOp VdbeOp;
16703
16704
16705/*
16706** A sub-routine used to implement a trigger program.
16707*/
16708struct SubProgram {
16709 VdbeOp *aOp; /* Array of opcodes for sub-program */
16710 int nOp; /* Elements in aOp[] */
16711 int nMem; /* Number of memory cells required */
16712 int nCsr; /* Number of cursors required */
16713 u8 *aOnce; /* Array of OP_Once flags */
16714 void *token; /* id that may be used to recursive triggers */
16715 SubProgram *pNext; /* Next sub-program already visited */
16716};
16717
16718/*
16719** A smaller version of VdbeOp used for the VdbeAddOpList() function because
16720** it takes up less space.
16721*/
16722struct VdbeOpList {
16723 u8 opcode; /* What operation to perform */
16724 signed char p1; /* First operand */
16725 signed char p2; /* Second parameter (often the jump destination) */
16726 signed char p3; /* Third parameter */
16727};
16728typedef struct VdbeOpList VdbeOpList;
16729
16730/*
16731** Allowed values of VdbeOp.p4type
16732*/
16733#define P4_NOTUSED0 0 /* The P4 parameter is not used */
16734#define P4_TRANSIENT0 0 /* P4 is a pointer to a transient string */
16735#define P4_STATIC(-1) (-1) /* Pointer to a static string */
16736#define P4_COLLSEQ(-2) (-2) /* P4 is a pointer to a CollSeq structure */
16737#define P4_INT32(-3) (-3) /* P4 is a 32-bit signed integer */
16738#define P4_SUBPROGRAM(-4) (-4) /* P4 is a pointer to a SubProgram structure */
16739#define P4_TABLE(-5) (-5) /* P4 is a pointer to a Table structure */
16740/* Above do not own any resources. Must free those below */
16741#define P4_FREE_IF_LE(-6) (-6)
16742#define P4_DYNAMIC(-6) (-6) /* Pointer to memory from sqliteMalloc() */
16743#define P4_FUNCDEF(-7) (-7) /* P4 is a pointer to a FuncDef structure */
16744#define P4_KEYINFO(-8) (-8) /* P4 is a pointer to a KeyInfo structure */
16745#define P4_EXPR(-9) (-9) /* P4 is a pointer to an Expr tree */
16746#define P4_MEM(-10) (-10) /* P4 is a pointer to a Mem* structure */
16747#define P4_VTAB(-11) (-11) /* P4 is a pointer to an sqlite3_vtab structure */
16748#define P4_REAL(-12) (-12) /* P4 is a 64-bit floating point value */
16749#define P4_INT64(-13) (-13) /* P4 is a 64-bit signed integer */
16750#define P4_INTARRAY(-14) (-14) /* P4 is a vector of 32-bit integers */
16751#define P4_FUNCCTX(-15) (-15) /* P4 is a pointer to an sqlite3_context object */
16752#define P4_TABLEREF(-16) (-16) /* Like P4_TABLE, but reference counted */
16753#define P4_SUBRTNSIG(-17) (-17) /* P4 is a SubrtnSig pointer */
16754
16755/* Error message codes for OP_Halt */
16756#define P5_ConstraintNotNull1 1
16757#define P5_ConstraintUnique2 2
16758#define P5_ConstraintCheck3 3
16759#define P5_ConstraintFK4 4
16760
16761/*
16762** The Vdbe.aColName array contains 5n Mem structures, where n is the
16763** number of columns of data returned by the statement.
16764*/
16765#define COLNAME_NAME0 0
16766#define COLNAME_DECLTYPE1 1
16767#define COLNAME_DATABASE2 2
16768#define COLNAME_TABLE3 3
16769#define COLNAME_COLUMN4 4
16770#ifdef SQLITE_ENABLE_COLUMN_METADATA1
16771# define COLNAME_N5 5 /* Number of COLNAME_xxx symbols */
16772#else
16773# ifdef SQLITE_OMIT_DECLTYPE
16774# define COLNAME_N5 1 /* Store only the name */
16775# else
16776# define COLNAME_N5 2 /* Store the name and decltype */
16777# endif
16778#endif
16779
16780/*
16781** The following macro converts a label returned by sqlite3VdbeMakeLabel()
16782** into an index into the Parse.aLabel[] array that contains the resolved
16783** address of that label.
16784*/
16785#define ADDR(X)(~(X)) (~(X))
16786
16787/*
16788** The makefile scans the vdbe.c source file and creates the "opcodes.h"
16789** header file that defines a number for each opcode used by the VDBE.
16790*/
16791/************** Include opcodes.h in the middle of vdbe.h ********************/
16792/************** Begin file opcodes.h *****************************************/
16793/* Automatically generated. Do not edit */
16794/* See the tool/mkopcodeh.tcl script for details */
16795#define OP_Savepoint0 0
16796#define OP_AutoCommit1 1
16797#define OP_Transaction2 2
16798#define OP_Checkpoint3 3
16799#define OP_JournalMode4 4
16800#define OP_Vacuum5 5
16801#define OP_VFilter6 6 /* jump, synopsis: iplan=r[P3] zplan='P4' */
16802#define OP_VUpdate7 7 /* synopsis: data=r[P3@P2] */
16803#define OP_Init8 8 /* jump0, synopsis: Start at P2 */
16804#define OP_Goto9 9 /* jump */
16805#define OP_Gosub10 10 /* jump */
16806#define OP_InitCoroutine11 11 /* jump0 */
16807#define OP_Yield12 12 /* jump0 */
16808#define OP_MustBeInt13 13 /* jump0 */
16809#define OP_Jump14 14 /* jump */
16810#define OP_Once15 15 /* jump */
16811#define OP_If16 16 /* jump */
16812#define OP_IfNot17 17 /* jump */
16813#define OP_IsType18 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */
16814#define OP_Not19 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
16815#define OP_IfNullRow20 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
16816#define OP_SeekLT21 21 /* jump0, synopsis: key=r[P3@P4] */
16817#define OP_SeekLE22 22 /* jump0, synopsis: key=r[P3@P4] */
16818#define OP_SeekGE23 23 /* jump0, synopsis: key=r[P3@P4] */
16819#define OP_SeekGT24 24 /* jump0, synopsis: key=r[P3@P4] */
16820#define OP_IfNotOpen25 25 /* jump, synopsis: if( !csr[P1] ) goto P2 */
16821#define OP_IfNoHope26 26 /* jump, synopsis: key=r[P3@P4] */
16822#define OP_NoConflict27 27 /* jump, synopsis: key=r[P3@P4] */
16823#define OP_NotFound28 28 /* jump, synopsis: key=r[P3@P4] */
16824#define OP_Found29 29 /* jump, synopsis: key=r[P3@P4] */
16825#define OP_SeekRowid30 30 /* jump0, synopsis: intkey=r[P3] */
16826#define OP_NotExists31 31 /* jump, synopsis: intkey=r[P3] */
16827#define OP_Last32 32 /* jump0 */
16828#define OP_IfSizeBetween33 33 /* jump */
16829#define OP_SorterSort34 34 /* jump */
16830#define OP_Sort35 35 /* jump */
16831#define OP_Rewind36 36 /* jump0 */
16832#define OP_SorterNext37 37 /* jump */
16833#define OP_Prev38 38 /* jump */
16834#define OP_Next39 39 /* jump */
16835#define OP_IdxLE40 40 /* jump, synopsis: key=r[P3@P4] */
16836#define OP_IdxGT41 41 /* jump, synopsis: key=r[P3@P4] */
16837#define OP_IdxLT42 42 /* jump, synopsis: key=r[P3@P4] */
16838#define OP_Or43 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
16839#define OP_And44 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
16840#define OP_IdxGE45 45 /* jump, synopsis: key=r[P3@P4] */
16841#define OP_RowSetRead46 46 /* jump, synopsis: r[P3]=rowset(P1) */
16842#define OP_RowSetTest47 47 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
16843#define OP_Program48 48 /* jump0 */
16844#define OP_FkIfZero49 49 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
16845#define OP_IfPos50 50 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16846#define OP_IsNull51 51 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
16847#define OP_NotNull52 52 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
16848#define OP_Ne53 53 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
16849#define OP_Eq54 54 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
16850#define OP_Gt55 55 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
16851#define OP_Le56 56 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
16852#define OP_Lt57 57 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
16853#define OP_Ge58 58 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
16854#define OP_ElseEq59 59 /* jump, same as TK_ESCAPE */
16855#define OP_IfNotZero60 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16856#define OP_DecrJumpZero61 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16857#define OP_IncrVacuum62 62 /* jump */
16858#define OP_VNext63 63 /* jump */
16859#define OP_Filter64 64 /* jump, synopsis: if key(P3@P4) not in filter(P1) goto P2 */
16860#define OP_PureFunc65 65 /* synopsis: r[P3]=func(r[P2@NP]) */
16861#define OP_Function66 66 /* synopsis: r[P3]=func(r[P2@NP]) */
16862#define OP_Return67 67
16863#define OP_EndCoroutine68 68
16864#define OP_HaltIfNull69 69 /* synopsis: if r[P3]=null halt */
16865#define OP_Halt70 70
16866#define OP_Integer71 71 /* synopsis: r[P2]=P1 */
16867#define OP_Int6472 72 /* synopsis: r[P2]=P4 */
16868#define OP_String73 73 /* synopsis: r[P2]='P4' (len=P1) */
16869#define OP_BeginSubrtn74 74 /* synopsis: r[P2]=NULL */
16870#define OP_Null75 75 /* synopsis: r[P2..P3]=NULL */
16871#define OP_SoftNull76 76 /* synopsis: r[P1]=NULL */
16872#define OP_Blob77 77 /* synopsis: r[P2]=P4 (len=P1) */
16873#define OP_Variable78 78 /* synopsis: r[P2]=parameter(P1) */
16874#define OP_Move79 79 /* synopsis: r[P2@P3]=r[P1@P3] */
16875#define OP_Copy80 80 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
16876#define OP_SCopy81 81 /* synopsis: r[P2]=r[P1] */
16877#define OP_IntCopy82 82 /* synopsis: r[P2]=r[P1] */
16878#define OP_FkCheck83 83
16879#define OP_ResultRow84 84 /* synopsis: output=r[P1@P2] */
16880#define OP_CollSeq85 85
16881#define OP_AddImm86 86 /* synopsis: r[P1]=r[P1]+P2 */
16882#define OP_RealAffinity87 87
16883#define OP_Cast88 88 /* synopsis: affinity(r[P1]) */
16884#define OP_Permutation89 89
16885#define OP_Compare90 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16886#define OP_IsTrue91 91 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
16887#define OP_ZeroOrNull92 92 /* synopsis: r[P2] = 0 OR NULL */
16888#define OP_Offset93 93 /* synopsis: r[P3] = sqlite_offset(P1) */
16889#define OP_Column94 94 /* synopsis: r[P3]=PX cursor P1 column P2 */
16890#define OP_TypeCheck95 95 /* synopsis: typecheck(r[P1@P2]) */
16891#define OP_Affinity96 96 /* synopsis: affinity(r[P1@P2]) */
16892#define OP_MakeRecord97 97 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
16893#define OP_Count98 98 /* synopsis: r[P2]=count() */
16894#define OP_ReadCookie99 99
16895#define OP_SetCookie100 100
16896#define OP_ReopenIdx101 101 /* synopsis: root=P2 iDb=P3 */
16897#define OP_OpenRead102 102 /* synopsis: root=P2 iDb=P3 */
16898#define OP_BitAnd103 103 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
16899#define OP_BitOr104 104 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
16900#define OP_ShiftLeft105 105 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
16901#define OP_ShiftRight106 106 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
16902#define OP_Add107 107 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
16903#define OP_Subtract108 108 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16904#define OP_Multiply109 109 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
16905#define OP_Divide110 110 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
16906#define OP_Remainder111 111 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
16907#define OP_Concat112 112 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
16908#define OP_OpenWrite113 113 /* synopsis: root=P2 iDb=P3 */
16909#define OP_OpenDup114 114
16910#define OP_BitNot115 115 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
16911#define OP_OpenAutoindex116 116 /* synopsis: nColumn=P2 */
16912#define OP_OpenEphemeral117 117 /* synopsis: nColumn=P2 */
16913#define OP_String8118 118 /* same as TK_STRING, synopsis: r[P2]='P4' */
16914#define OP_SorterOpen119 119
16915#define OP_SequenceTest120 120 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
16916#define OP_OpenPseudo121 121 /* synopsis: P3 columns in r[P2] */
16917#define OP_Close122 122
16918#define OP_ColumnsUsed123 123
16919#define OP_SeekScan124 124 /* synopsis: Scan-ahead up to P1 rows */
16920#define OP_SeekHit125 125 /* synopsis: set P2<=seekHit<=P3 */
16921#define OP_Sequence126 126 /* synopsis: r[P2]=cursor[P1].ctr++ */
16922#define OP_NewRowid127 127 /* synopsis: r[P2]=rowid */
16923#define OP_Insert128 128 /* synopsis: intkey=r[P3] data=r[P2] */
16924#define OP_RowCell129 129
16925#define OP_Delete130 130
16926#define OP_ResetCount131 131
16927#define OP_SorterCompare132 132 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
16928#define OP_SorterData133 133 /* synopsis: r[P2]=data */
16929#define OP_RowData134 134 /* synopsis: r[P2]=data */
16930#define OP_Rowid135 135 /* synopsis: r[P2]=PX rowid of P1 */
16931#define OP_NullRow136 136
16932#define OP_SeekEnd137 137
16933#define OP_IdxInsert138 138 /* synopsis: key=r[P2] */
16934#define OP_SorterInsert139 139 /* synopsis: key=r[P2] */
16935#define OP_IdxDelete140 140 /* synopsis: key=r[P2@P3] */
16936#define OP_DeferredSeek141 141 /* synopsis: Move P3 to P1.rowid if needed */
16937#define OP_IdxRowid142 142 /* synopsis: r[P2]=rowid */
16938#define OP_FinishSeek143 143
16939#define OP_Destroy144 144
16940#define OP_Clear145 145
16941#define OP_ResetSorter146 146
16942#define OP_CreateBtree147 147 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
16943#define OP_SqlExec148 148
16944#define OP_ParseSchema149 149
16945#define OP_LoadAnalysis150 150
16946#define OP_DropTable151 151
16947#define OP_DropIndex152 152
16948#define OP_DropTrigger153 153
16949#define OP_Real154 154 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
16950#define OP_IntegrityCk155 155
16951#define OP_RowSetAdd156 156 /* synopsis: rowset(P1)=r[P2] */
16952#define OP_Param157 157
16953#define OP_FkCounter158 158 /* synopsis: fkctr[P1]+=P2 */
16954#define OP_MemMax159 159 /* synopsis: r[P1]=max(r[P1],r[P2]) */
16955#define OP_OffsetLimit160 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
16956#define OP_AggInverse161 161 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
16957#define OP_AggStep162 162 /* synopsis: accum=r[P3] step(r[P2@P5]) */
16958#define OP_AggStep1163 163 /* synopsis: accum=r[P3] step(r[P2@P5]) */
16959#define OP_AggValue164 164 /* synopsis: r[P3]=value N=P2 */
16960#define OP_AggFinal165 165 /* synopsis: accum=r[P1] N=P2 */
16961#define OP_Expire166 166
16962#define OP_CursorLock167 167
16963#define OP_CursorUnlock168 168
16964#define OP_TableLock169 169 /* synopsis: iDb=P1 root=P2 write=P3 */
16965#define OP_VBegin170 170
16966#define OP_VCreate171 171
16967#define OP_VDestroy172 172
16968#define OP_VOpen173 173
16969#define OP_VCheck174 174
16970#define OP_VInitIn175 175 /* synopsis: r[P2]=ValueList(P1,P3) */
16971#define OP_VColumn176 176 /* synopsis: r[P3]=vcolumn(P2) */
16972#define OP_VRename177 177
16973#define OP_Pagecount178 178
16974#define OP_MaxPgcnt179 179
16975#define OP_ClrSubtype180 180 /* synopsis: r[P1].subtype = 0 */
16976#define OP_GetSubtype181 181 /* synopsis: r[P2] = r[P1].subtype */
16977#define OP_SetSubtype182 182 /* synopsis: r[P2].subtype = r[P1] */
16978#define OP_FilterAdd183 183 /* synopsis: filter(P1) += key(P3@P4) */
16979#define OP_Trace184 184
16980#define OP_CursorHint185 185
16981#define OP_ReleaseReg186 186 /* synopsis: release r[P1@P2] mask P3 */
16982#define OP_Noop187 187
16983#define OP_Explain188 188
16984#define OP_Abortable189 189
16985
16986/* Properties such as "out2" or "jump" that are specified in
16987** comments following the "case" for each opcode in the vdbe.c
16988** are encoded into bitvectors as follows:
16989*/
16990#define OPFLG_JUMP0x01 0x01 /* jump: P2 holds jmp target */
16991#define OPFLG_IN10x02 0x02 /* in1: P1 is an input */
16992#define OPFLG_IN20x04 0x04 /* in2: P2 is an input */
16993#define OPFLG_IN30x08 0x08 /* in3: P3 is an input */
16994#define OPFLG_OUT20x10 0x10 /* out2: P2 is an output */
16995#define OPFLG_OUT30x20 0x20 /* out3: P3 is an output */
16996#define OPFLG_NCYCLE0x40 0x40 /* ncycle:Cycles count against P1 */
16997#define OPFLG_JUMP00x80 0x80 /* jump0: P2 might be zero */
16998#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,}
{\
16999/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
17000/* 8 */ 0x81, 0x01, 0x01, 0x81, 0x83, 0x83, 0x01, 0x01,\
17001/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0xc9, 0xc9, 0xc9,\
17002/* 24 */ 0xc9, 0x01, 0x49, 0x49, 0x49, 0x49, 0xc9, 0x49,\
17003/* 32 */ 0xc1, 0x01, 0x41, 0x41, 0xc1, 0x01, 0x41, 0x41,\
17004/* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
17005/* 48 */ 0x81, 0x01, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b,\
17006/* 56 */ 0x0b, 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x01, 0x41,\
17007/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
17008/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
17009/* 80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x02, 0x02,\
17010/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\
17011/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x40, 0x26,\
17012/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
17013/* 112 */ 0x26, 0x00, 0x40, 0x12, 0x40, 0x40, 0x10, 0x00,\
17014/* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\
17015/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,\
17016/* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
17017/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
17018/* 152 */ 0x00, 0x00, 0x10, 0x00, 0x06, 0x10, 0x00, 0x04,\
17019/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
17020/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x50,\
17021/* 176 */ 0x40, 0x00, 0x10, 0x10, 0x02, 0x12, 0x12, 0x00,\
17022/* 184 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
17023
17024/* The resolve3P2Values() routine is able to run faster if it knows
17025** the value of the largest JUMP opcode. The smaller the maximum
17026** JUMP opcode the better, so the mkopcodeh.tcl script that
17027** generated this include file strives to group all JUMP opcodes
17028** together near the beginning of the list.
17029*/
17030#define SQLITE_MX_JUMP_OPCODE64 64 /* Maximum JUMP opcode */
17031
17032/************** End of opcodes.h *********************************************/
17033/************** Continuing where we left off in vdbe.h ***********************/
17034
17035/*
17036** Additional non-public SQLITE_PREPARE_* flags
17037*/
17038#define SQLITE_PREPARE_SAVESQL0x80 0x80 /* Preserve SQL text */
17039#define SQLITE_PREPARE_MASK0x0f 0x0f /* Mask of public flags */
17040
17041/*
17042** Prototypes for the VDBE interface. See comments on the implementation
17043** for a description of what each of these routines does.
17044*/
17045SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse*);
17046SQLITE_PRIVATEstatic Parse *sqlite3VdbeParser(Vdbe*);
17047SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe*,int);
17048SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe*,int,int);
17049SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
17050SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe*,int);
17051SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe*,int,const char*);
17052SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
17053SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
17054SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
17055SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
17056SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
17057SQLITE_PRIVATEstatic int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
17058SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe*,int);
17059#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
17060SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
17061SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
17062#else
17063# define sqlite3VdbeVerifyNoMallocRequired(A,B)
17064# define sqlite3VdbeVerifyNoResultRow(A)
17065#endif
17066#if defined(SQLITE_DEBUG)
17067SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
17068SQLITE_PRIVATEstatic void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe*,int,int,int);
17069#else
17070# define sqlite3VdbeVerifyAbortable(A,B)
17071# define sqlite3VdbeNoJumpsOutsideSubrtn(A,B,C,D)
17072#endif
17073SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
17074#ifndef SQLITE_OMIT_EXPLAIN
17075SQLITE_PRIVATEstatic int sqlite3VdbeExplain(Parse*,u8,const char*,...);
17076SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse*);
17077SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse*);
17078# define ExplainQueryPlan(P)sqlite3VdbeExplain P sqlite3VdbeExplain P
17079# ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
17080# define ExplainQueryPlan2(V,P)(V = sqlite3VdbeExplain P) (V = sqlite3VdbeExplain P)
17081# else
17082# define ExplainQueryPlan2(V,P)(V = sqlite3VdbeExplain P) ExplainQueryPlan(P)sqlite3VdbeExplain P
17083# endif
17084# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P) sqlite3VdbeExplainPop(P)
17085# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) sqlite3VdbeExplainParent(P)
17086#else
17087# define ExplainQueryPlan(P)sqlite3VdbeExplain P
17088# define ExplainQueryPlan2(V,P)(V = sqlite3VdbeExplain P)
17089# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P)
17090# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) 0
17091# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
17092#endif
17093#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
17094SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char*,const char*);
17095#else
17096# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
17097#endif
17098SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
17099SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
17100SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
17101SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
17102SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
17103SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
17104SQLITE_PRIVATEstatic void sqlite3VdbeTypeofColumn(Vdbe*, int);
17105SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe*, int addr);
17106SQLITE_PRIVATEstatic void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
17107SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
17108SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
17109#ifdef SQLITE_DEBUG
17110SQLITE_PRIVATEstatic void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
17111#else
17112# define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
17113#endif
17114SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
17115SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
17116SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
17117SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe*, int);
17118SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
17119SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetLastOp(Vdbe*);
17120SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse*);
17121SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe*);
17122SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe*);
17123SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe*);
17124SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(Vdbe*,Parse*);
17125SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe*);
17126SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe*, int);
17127SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe*);
17128#ifdef SQLITE_DEBUG
17129SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *, int);
17130#endif
17131SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe*);
17132SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe*);
17133SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe*);
17134SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe*,int);
17135SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
17136SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe*);
17137SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe*);
17138SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe*);
17139SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
17140#ifdef SQLITE_ENABLE_NORMALIZE
17141SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
17142SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
17143#endif
17144SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe*,Vdbe*);
17145SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
17146SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
17147SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe*, int);
17148#ifndef SQLITE_OMIT_TRACE
17149SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(Vdbe*, const char*);
17150#endif
17151SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
17152SQLITE_PRIVATEstatic int sqlite3BlobCompare(const Mem*, const Mem*);
17153
17154SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
17155SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
17156SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
17157SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
17158
17159typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
17160SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
17161
17162SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
17163SQLITE_PRIVATEstatic int sqlite3VdbeHasSubProgram(Vdbe*);
17164
17165SQLITE_PRIVATEstatic void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val);
17166
17167SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context*);
17168#ifdef SQLITE_ENABLE_BYTECODE_VTAB
17169SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3*);
17170#endif
17171
17172/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
17173** each VDBE opcode.
17174**
17175** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
17176** comments in VDBE programs that show key decision points in the code
17177** generator.
17178*/
17179#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
17180SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe*, const char*, ...);
17181# define VdbeComment(X) sqlite3VdbeComment X
17182SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
17183# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
17184# ifdef SQLITE_ENABLE_MODULE_COMMENTS
17185# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
17186# else
17187# define VdbeModuleComment(X)
17188# endif
17189#else
17190# define VdbeComment(X)
17191# define VdbeNoopComment(X)
17192# define VdbeModuleComment(X)
17193#endif
17194
17195/*
17196** The VdbeCoverage macros are used to set a coverage testing point
17197** for VDBE branch instructions. The coverage testing points are line
17198** numbers in the sqlite3.c source file. VDBE branch coverage testing
17199** only works with an amalgamation build. That's ok since a VDBE branch
17200** coverage build designed for testing the test suite only. No application
17201** should ever ship with VDBE branch coverage measuring turned on.
17202**
17203** VdbeCoverage(v) // Mark the previously coded instruction
17204** // as a branch
17205**
17206** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
17207**
17208** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
17209**
17210** VdbeCoverageNeverTaken(v) // Previous branch is never taken
17211**
17212** VdbeCoverageNeverNull(v) // Previous three-way branch is only
17213** // taken on the first two ways. The
17214** // NULL option is not possible
17215**
17216** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
17217** // in distinguishing equal and not-equal.
17218**
17219** Every VDBE branch operation must be tagged with one of the macros above.
17220** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
17221** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
17222** routine in vdbe.c, alerting the developer to the missed tag.
17223**
17224** During testing, the test application will invoke
17225** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
17226** routine that is invoked as each bytecode branch is taken. The callback
17227** contains the sqlite3.c source line number of the VdbeCoverage macro and
17228** flags to indicate whether or not the branch was taken. The test application
17229** is responsible for keeping track of this and reporting byte-code branches
17230** that are never taken.
17231**
17232** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
17233** vdbe.c source file for additional information.
17234*/
17235#ifdef SQLITE_VDBE_COVERAGE
17236SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe*,int);
17237# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__17237)
17238# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__17238)
17239# define VdbeCoverageAlwaysTaken(v) \
17240 sqlite3VdbeSetLineNumber(v,__LINE__17240|0x5000000);
17241# define VdbeCoverageNeverTaken(v) \
17242 sqlite3VdbeSetLineNumber(v,__LINE__17242|0x6000000);
17243# define VdbeCoverageNeverNull(v) \
17244 sqlite3VdbeSetLineNumber(v,__LINE__17244|0x4000000);
17245# define VdbeCoverageNeverNullIf(v,x) \
17246 if(x)sqlite3VdbeSetLineNumber(v,__LINE__17246|0x4000000);
17247# define VdbeCoverageEqNe(v) \
17248 sqlite3VdbeSetLineNumber(v,__LINE__17248|0x8000000);
17249# define VDBE_OFFSET_LINENO(x)0 (__LINE__17249+x)
17250#else
17251# define VdbeCoverage(v)
17252# define VdbeCoverageIf(v,x)
17253# define VdbeCoverageAlwaysTaken(v)
17254# define VdbeCoverageNeverTaken(v)
17255# define VdbeCoverageNeverNull(v)
17256# define VdbeCoverageNeverNullIf(v,x)
17257# define VdbeCoverageEqNe(v)
17258# define VDBE_OFFSET_LINENO(x)0 0
17259#endif
17260
17261#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
17262SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
17263SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusRange(Vdbe*, int, int, int);
17264SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusCounters(Vdbe*, int, int, int);
17265#else
17266# define sqlite3VdbeScanStatus(a,b,c,d,e,f)
17267# define sqlite3VdbeScanStatusRange(a,b,c,d)
17268# define sqlite3VdbeScanStatusCounters(a,b,c,d)
17269#endif
17270
17271#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
17272SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
17273#endif
17274
17275#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
17276SQLITE_PRIVATEstatic int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr);
17277#endif
17278
17279#endif /* SQLITE_VDBE_H */
17280
17281/************** End of vdbe.h ************************************************/
17282/************** Continuing where we left off in sqliteInt.h ******************/
17283/************** Include pcache.h in the middle of sqliteInt.h ****************/
17284/************** Begin file pcache.h ******************************************/
17285/*
17286** 2008 August 05
17287**
17288** The author disclaims copyright to this source code. In place of
17289** a legal notice, here is a blessing:
17290**
17291** May you do good and not evil.
17292** May you find forgiveness for yourself and forgive others.
17293** May you share freely, never taking more than you give.
17294**
17295*************************************************************************
17296** This header file defines the interface that the sqlite page cache
17297** subsystem.
17298*/
17299
17300#ifndef _PCACHE_H_
17301
17302typedef struct PgHdr PgHdr;
17303typedef struct PCache PCache;
17304
17305/*
17306** Every page in the cache is controlled by an instance of the following
17307** structure.
17308*/
17309struct PgHdr {
17310 sqlite3_pcache_page *pPage; /* Pcache object page handle */
17311 void *pData; /* Page data */
17312 void *pExtra; /* Extra content */
17313 PCache *pCache; /* PRIVATE: Cache that owns this page */
17314 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
17315 Pager *pPager; /* The pager this page is part of */
17316 Pgno pgno; /* Page number for this page */
17317#ifdef SQLITE_CHECK_PAGES
17318 u32 pageHash; /* Hash of page content */
17319#endif
17320 u16 flags; /* PGHDR flags defined below */
17321
17322 /**********************************************************************
17323 ** Elements above, except pCache, are public. All that follow are
17324 ** private to pcache.c and should not be accessed by other modules.
17325 ** pCache is grouped with the public elements for efficiency.
17326 */
17327 i64 nRef; /* Number of users of this page */
17328 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
17329 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
17330 /* NB: pDirtyNext and pDirtyPrev are undefined if the
17331 ** PgHdr object is not dirty */
17332};
17333
17334/* Bit values for PgHdr.flags */
17335#define PGHDR_CLEAN0x001 0x001 /* Page not on the PCache.pDirty list */
17336#define PGHDR_DIRTY0x002 0x002 /* Page is on the PCache.pDirty list */
17337#define PGHDR_WRITEABLE0x004 0x004 /* Journaled and ready to modify */
17338#define PGHDR_NEED_SYNC0x008 0x008 /* Fsync the rollback journal before
17339 ** writing this page to the database */
17340#define PGHDR_DONT_WRITE0x010 0x010 /* Do not write content to disk */
17341#define PGHDR_MMAP0x020 0x020 /* This is an mmap page object */
17342
17343#define PGHDR_WAL_APPEND0x040 0x040 /* Appended to wal file */
17344
17345/* Initialize and shutdown the page cache subsystem */
17346SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void);
17347SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void);
17348
17349/* Page cache buffer management:
17350** These routines implement SQLITE_CONFIG_PAGECACHE.
17351*/
17352SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *, int sz, int n);
17353
17354/* Create a new pager cache.
17355** Under memory stress, invoke xStress to try to make pages clean.
17356** Only clean and unpinned pages can be reclaimed.
17357*/
17358SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
17359 int szPage, /* Size of every page */
17360 int szExtra, /* Extra space associated with each page */
17361 int bPurgeable, /* True if pages are on backing store */
17362 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
17363 void *pStress, /* Argument to xStress */
17364 PCache *pToInit /* Preallocated space for the PCache */
17365);
17366
17367/* Modify the page-size after the cache has been created. */
17368SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *, int);
17369
17370/* Return the size in bytes of a PCache object. Used to preallocate
17371** storage space.
17372*/
17373SQLITE_PRIVATEstatic int sqlite3PcacheSize(void);
17374
17375/* One release per successful fetch. Page is pinned until released.
17376** Reference counted.
17377*/
17378SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
17379SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
17380SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
17381SQLITE_PRIVATEstatic void sqlite3PcacheRelease(PgHdr*);
17382
17383SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
17384SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
17385SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
17386SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
17387SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache*);
17388
17389/* Change a page number. Used by incr-vacuum. */
17390SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr*, Pgno);
17391
17392/* Remove all pages with pgno>x. Reset the cache if x==0 */
17393SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache*, Pgno x);
17394
17395/* Get a list of all dirty pages in the cache, sorted by page number */
17396SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache*);
17397
17398/* Reset and close the cache object */
17399SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache*);
17400
17401/* Clear flags from pages of the page cache */
17402SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *);
17403
17404/* Discard the contents of the cache */
17405SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache*);
17406
17407/* Return the total number of outstanding page references */
17408SQLITE_PRIVATEstatic i64 sqlite3PcacheRefCount(PCache*);
17409
17410/* Increment the reference count of an existing page */
17411SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr*);
17412
17413SQLITE_PRIVATEstatic i64 sqlite3PcachePageRefcount(PgHdr*);
17414
17415/* Return the total number of pages stored in the cache */
17416SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache*);
17417
17418#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
17419/* Iterate through all dirty pages currently stored in the cache. This
17420** interface is only available if SQLITE_CHECK_PAGES is defined when the
17421** library is built.
17422*/
17423SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
17424#endif
17425
17426#if defined(SQLITE_DEBUG)
17427/* Check invariants on a PgHdr object */
17428SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr*);
17429#endif
17430
17431/* Set and get the suggested cache-size for the specified pager-cache.
17432**
17433** If no global maximum is configured, then the system attempts to limit
17434** the total number of pages cached by purgeable pager-caches to the sum
17435** of the suggested cache-sizes.
17436*/
17437SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *, int);
17438#ifdef SQLITE_TEST
17439SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *);
17440#endif
17441
17442/* Set or get the suggested spill-size for the specified pager-cache.
17443**
17444** The spill-size is the minimum number of pages in cache before the cache
17445** will attempt to spill dirty pages by calling xStress.
17446*/
17447SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *, int);
17448
17449/* Free up as much memory as possible from the page cache */
17450SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache*);
17451
17452#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
17453/* Try to return memory used by the pcache module to the main memory heap */
17454SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int);
17455#endif
17456
17457#ifdef SQLITE_TEST
17458SQLITE_PRIVATEstatic void sqlite3PcacheStats(int*,int*,int*,int*);
17459#endif
17460
17461SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void);
17462
17463/* Return the header size */
17464SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void);
17465SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void);
17466
17467/* Number of dirty pages as a percentage of the configured cache size */
17468SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache*);
17469
17470#ifdef SQLITE_DIRECT_OVERFLOW_READ1
17471SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache);
17472#endif
17473
17474#endif /* _PCACHE_H_ */
17475
17476/************** End of pcache.h **********************************************/
17477/************** Continuing where we left off in sqliteInt.h ******************/
17478/************** Include mutex.h in the middle of sqliteInt.h *****************/
17479/************** Begin file mutex.h *******************************************/
17480/*
17481** 2007 August 28
17482**
17483** The author disclaims copyright to this source code. In place of
17484** a legal notice, here is a blessing:
17485**
17486** May you do good and not evil.
17487** May you find forgiveness for yourself and forgive others.
17488** May you share freely, never taking more than you give.
17489**
17490*************************************************************************
17491**
17492** This file contains the common header for all mutex implementations.
17493** The sqliteInt.h header #includes this file so that it is available
17494** to all source files. We break it out in an effort to keep the code
17495** better organized.
17496**
17497** NOTE: source files should *not* #include this header file directly.
17498** Source files should #include the sqliteInt.h file and let that file
17499** include this one indirectly.
17500*/
17501
17502
17503/*
17504** Figure out what version of the code to use. The choices are
17505**
17506** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
17507** mutexes implementation cannot be overridden
17508** at start-time.
17509**
17510** SQLITE_MUTEX_NOOP For single-threaded applications. No
17511** mutual exclusion is provided. But this
17512** implementation can be overridden at
17513** start-time.
17514**
17515** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
17516**
17517** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
17518*/
17519#if !SQLITE_THREADSAFE2
17520# define SQLITE_MUTEX_OMIT
17521#endif
17522#if SQLITE_THREADSAFE2 && !defined(SQLITE_MUTEX_NOOP)
17523# if SQLITE_OS_UNIX1
17524# define SQLITE_MUTEX_PTHREADS
17525# elif SQLITE_OS_WIN0
17526# define SQLITE_MUTEX_W32
17527# else
17528# define SQLITE_MUTEX_NOOP
17529# endif
17530#endif
17531
17532#ifdef SQLITE_MUTEX_OMIT
17533/*
17534** If this is a no-op implementation, implement everything as macros.
17535*/
17536#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
17537#define sqlite3_mutex_free(X)
17538#define sqlite3_mutex_enter(X)
17539#define sqlite3_mutex_try(X) SQLITE_OK0
17540#define sqlite3_mutex_leave(X)
17541#define sqlite3_mutex_held(X) ((void)(X),1)
17542#define sqlite3_mutex_notheld(X) ((void)(X),1)
17543#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
17544#define sqlite3MutexInit() SQLITE_OK0
17545#define sqlite3MutexEnd()
17546#define MUTEX_LOGIC(X)X
17547#else
17548#define MUTEX_LOGIC(X)X X
17549SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
17550#endif /* defined(SQLITE_MUTEX_OMIT) */
17551
17552/************** End of mutex.h ***********************************************/
17553/************** Continuing where we left off in sqliteInt.h ******************/
17554
17555/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17556** synchronous setting to EXTRA. It is no longer supported.
17557*/
17558#ifdef SQLITE_EXTRA_DURABLE
17559# warning Use SQLITE_DEFAULT_SYNCHRONOUS2=3 instead of SQLITE_EXTRA_DURABLE
17560# define SQLITE_DEFAULT_SYNCHRONOUS2 3
17561#endif
17562
17563/*
17564** Default synchronous levels.
17565**
17566** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ
17567** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
17568**
17569** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
17570** OFF 1 0
17571** NORMAL 2 1
17572** FULL 3 2
17573** EXTRA 4 3
17574**
17575** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17576** In other words, the zero-based numbers are used for all external interfaces
17577** and the one-based values are used internally.
17578*/
17579#ifndef SQLITE_DEFAULT_SYNCHRONOUS2
17580# define SQLITE_DEFAULT_SYNCHRONOUS2 2
17581#endif
17582#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS1
17583# define SQLITE_DEFAULT_WAL_SYNCHRONOUS1 SQLITE_DEFAULT_SYNCHRONOUS2
17584#endif
17585
17586/*
17587** Each database file to be accessed by the system is an instance
17588** of the following structure. There are normally two of these structures
17589** in the sqlite.aDb[] array. aDb[0] is the main database file and
17590** aDb[1] is the database file used to hold temporary tables. Additional
17591** databases may be attached.
17592*/
17593struct Db {
17594 char *zDbSName; /* Name of this database. (schema name, not filename) */
17595 Btree *pBt; /* The B*Tree structure for this database file */
17596 u8 safety_level; /* How aggressive at syncing data to disk */
17597 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
17598 Schema *pSchema; /* Pointer to database schema (possibly shared) */
17599};
17600
17601/*
17602** An instance of the following structure stores a database schema.
17603**
17604** Most Schema objects are associated with a Btree. The exception is
17605** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17606** In shared cache mode, a single Schema object can be shared by multiple
17607** Btrees that refer to the same underlying BtShared object.
17608**
17609** Schema objects are automatically deallocated when the last Btree that
17610** references them is destroyed. The TEMP Schema is manually freed by
17611** sqlite3_close().
17612*
17613** A thread must be holding a mutex on the corresponding Btree in order
17614** to access Schema content. This implies that the thread must also be
17615** holding a mutex on the sqlite3 connection pointer that owns the Btree.
17616** For a TEMP Schema, only the connection mutex is required.
17617*/
17618struct Schema {
17619 int schema_cookie; /* Database schema version number for this file */
17620 int iGeneration; /* Generation counter. Incremented with each change */
17621 Hash tblHash; /* All tables indexed by name */
17622 Hash idxHash; /* All (named) indices indexed by name */
17623 Hash trigHash; /* All triggers indexed by name */
17624 Hash fkeyHash; /* All foreign keys by referenced table name */
17625 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
17626 u8 file_format; /* Schema format version for this file */
17627 u8 enc; /* Text encoding used by this database */
17628 u16 schemaFlags; /* Flags associated with this schema */
17629 int cache_size; /* Number of pages to use in the cache */
17630};
17631
17632/*
17633** These macros can be used to test, set, or clear bits in the
17634** Db.pSchema->flags field.
17635*/
17636#define DbHasProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17637#define DbHasAnyProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))!=0) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17638#define DbSetProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags|=(P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17639#define DbClearProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags&=~(P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17640
17641/*
17642** Allowed values for the DB.pSchema->flags field.
17643**
17644** The DB_SchemaLoaded flag is set after the database schema has been
17645** read into internal hash tables.
17646**
17647** DB_UnresetViews means that one or more views have column names that
17648** have been filled out. If the schema changes, these column names might
17649** changes and so the view will need to be reset.
17650*/
17651#define DB_SchemaLoaded0x0001 0x0001 /* The schema has been loaded */
17652#define DB_UnresetViews0x0002 0x0002 /* Some views have defined column names */
17653#define DB_ResetWanted0x0008 0x0008 /* Reset the schema when nSchemaLock==0 */
17654
17655/*
17656** The number of different kinds of things that can be limited
17657** using the sqlite3_limit() interface.
17658*/
17659#define SQLITE_N_LIMIT(11 +1) (SQLITE_LIMIT_WORKER_THREADS11+1)
17660
17661/*
17662** Lookaside malloc is a set of fixed-size buffers that can be used
17663** to satisfy small transient memory allocation requests for objects
17664** associated with a particular database connection. The use of
17665** lookaside malloc provides a significant performance enhancement
17666** (approx 10%) by avoiding numerous malloc/free requests while parsing
17667** SQL statements.
17668**
17669** The Lookaside structure holds configuration information about the
17670** lookaside malloc subsystem. Each available memory allocation in
17671** the lookaside subsystem is stored on a linked list of LookasideSlot
17672** objects.
17673**
17674** Lookaside allocations are only allowed for objects that are associated
17675** with a particular database connection. Hence, schema information cannot
17676** be stored in lookaside because in shared cache mode the schema information
17677** is shared by multiple database connections. Therefore, while parsing
17678** schema information, the Lookaside.bEnabled flag is cleared so that
17679** lookaside allocations are not used to construct the schema objects.
17680**
17681** New lookaside allocations are only allowed if bDisable==0. When
17682** bDisable is greater than zero, sz is set to zero which effectively
17683** disables lookaside without adding a new test for the bDisable flag
17684** in a performance-critical path. sz should be set by to szTrue whenever
17685** bDisable changes back to zero.
17686**
17687** Lookaside buffers are initially held on the pInit list. As they are
17688** used and freed, they are added back to the pFree list. New allocations
17689** come off of pFree first, then pInit as a fallback. This dual-list
17690** allows use to compute a high-water mark - the maximum number of allocations
17691** outstanding at any point in the past - by subtracting the number of
17692** allocations on the pInit list from the total number of allocations.
17693**
17694** Enhancement on 2019-12-12: Two-size-lookaside
17695** The default lookaside configuration is 100 slots of 1200 bytes each.
17696** The larger slot sizes are important for performance, but they waste
17697** a lot of space, as most lookaside allocations are less than 128 bytes.
17698** The two-size-lookaside enhancement breaks up the lookaside allocation
17699** into two pools: One of 128-byte slots and the other of the default size
17700** (1200-byte) slots. Allocations are filled from the small-pool first,
17701** failing over to the full-size pool if that does not work. Thus more
17702** lookaside slots are available while also using less memory.
17703** This enhancement can be omitted by compiling with
17704** SQLITE_OMIT_TWOSIZE_LOOKASIDE.
17705*/
17706struct Lookaside {
17707 u32 bDisable; /* Only operate the lookaside when zero */
17708 u16 sz; /* Size of each buffer in bytes */
17709 u16 szTrue; /* True value of sz, even if disabled */
17710 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
17711 u32 nSlot; /* Number of lookaside slots allocated */
17712 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
17713 LookasideSlot *pInit; /* List of buffers not previously used */
17714 LookasideSlot *pFree; /* List of available buffers */
17715#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17716 LookasideSlot *pSmallInit; /* List of small buffers not previously used */
17717 LookasideSlot *pSmallFree; /* List of available small buffers */
17718 void *pMiddle; /* First byte past end of full-size buffers and
17719 ** the first byte of LOOKASIDE_SMALL buffers */
17720#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
17721 void *pStart; /* First byte of available memory space */
17722 void *pEnd; /* First byte past end of available space */
17723 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17724};
17725struct LookasideSlot {
17726 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
17727};
17728
17729#define DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0 db->lookaside.bDisable++;db->lookaside.sz=0
17730#define EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
db->lookaside.bDisable--;\
17731 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17732
17733/* Size of the smaller allocations in two-size lookaside */
17734#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17735# define LOOKASIDE_SMALL128 0
17736#else
17737# define LOOKASIDE_SMALL128 128
17738#endif
17739
17740/*
17741** A hash table for built-in function definitions. (Application-defined
17742** functions use a regular table table from hash.h.)
17743**
17744** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
17745** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
17746** macro to compute a hash on the function name.
17747*/
17748#define SQLITE_FUNC_HASH_SZ23 23
17749struct FuncDefHash {
17750 FuncDef *a[SQLITE_FUNC_HASH_SZ23]; /* Hash table for functions */
17751};
17752#define SQLITE_FUNC_HASH(C,L)(((C)+(L))%23) (((C)+(L))%SQLITE_FUNC_HASH_SZ23)
17753
17754#if defined(SQLITE_USER_AUTHENTICATION)
17755# warning "The SQLITE_USER_AUTHENTICATION extension is deprecated. \
17756 See ext/userauth/user-auth.txt for details."
17757#endif
17758#ifdef SQLITE_USER_AUTHENTICATION
17759/*
17760** Information held in the "sqlite3" database connection object and used
17761** to manage user authentication.
17762*/
17763typedef struct sqlite3_userauth sqlite3_userauth;
17764struct sqlite3_userauth {
17765 u8 authLevel; /* Current authentication level */
17766 int nAuthPW; /* Size of the zAuthPW in bytes */
17767 char *zAuthPW; /* Password used to authenticate */
17768 char *zAuthUser; /* User name used to authenticate */
17769};
17770
17771/* Allowed values for sqlite3_userauth.authLevel */
17772#define UAUTH_Unknown 0 /* Authentication not yet checked */
17773#define UAUTH_Fail 1 /* User authentication failed */
17774#define UAUTH_User 2 /* Authenticated as a normal user */
17775#define UAUTH_Admin 3 /* Authenticated as an administrator */
17776
17777/* Functions used only by user authorization logic */
17778SQLITE_PRIVATEstatic int sqlite3UserAuthTable(const char*);
17779SQLITE_PRIVATEstatic int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
17780SQLITE_PRIVATEstatic void sqlite3UserAuthInit(sqlite3*);
17781SQLITE_PRIVATEstatic void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
17782
17783#endif /* SQLITE_USER_AUTHENTICATION */
17784
17785/*
17786** typedef for the authorization callback function.
17787*/
17788#ifdef SQLITE_USER_AUTHENTICATION
17789 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17790 const char*, const char*);
17791#else
17792 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17793 const char*);
17794#endif
17795
17796#ifndef SQLITE_OMIT_DEPRECATED1
17797/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
17798** in the style of sqlite3_trace()
17799*/
17800#define SQLITE_TRACE_LEGACY0 0x40 /* Use the legacy xTrace */
17801#define SQLITE_TRACE_XPROFILE0 0x80 /* Use the legacy xProfile */
17802#else
17803#define SQLITE_TRACE_LEGACY0 0
17804#define SQLITE_TRACE_XPROFILE0 0
17805#endif /* SQLITE_OMIT_DEPRECATED */
17806#define SQLITE_TRACE_NONLEGACY_MASK0x0f 0x0f /* Normal flags */
17807
17808/*
17809** Maximum number of sqlite3.aDb[] entries. This is the number of attached
17810** databases plus 2 for "main" and "temp".
17811*/
17812#define SQLITE_MAX_DB(10 +2) (SQLITE_MAX_ATTACHED10+2)
17813
17814/*
17815** Each database connection is an instance of the following structure.
17816*/
17817struct sqlite3 {
17818 sqlite3_vfs *pVfs; /* OS Interface */
17819 struct Vdbe *pVdbe; /* List of active virtual machines */
17820 CollSeq *pDfltColl; /* BINARY collseq for the database encoding */
17821 sqlite3_mutex *mutex; /* Connection mutex */
17822 Db *aDb; /* All backends */
17823 int nDb; /* Number of backends currently in use */
17824 u32 mDbFlags; /* flags recording internal state */
17825 u64 flags; /* flags settable by pragmas. See below */
17826 i64 lastRowid; /* ROWID of most recent insert (see above) */
17827 i64 szMmap; /* Default mmap_size setting */
17828 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17829 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
17830 int errCode; /* Most recent error code (SQLITE_*) */
17831 int errByteOffset; /* Byte offset of error in SQL statement */
17832 int errMask; /* & result codes with this before returning */
17833 int iSysErrno; /* Errno value from last system error */
17834 u32 dbOptFlags; /* Flags to enable/disable optimizations */
17835 u8 enc; /* Text encoding */
17836 u8 autoCommit; /* The auto-commit flag. */
17837 u8 temp_store; /* 1: file 2: memory 0: default */
17838 u8 mallocFailed; /* True if we have seen a malloc failure */
17839 u8 bBenignMalloc; /* Do not require OOMs if true */
17840 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17841 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
17842 u8 suppressErr; /* Do not issue error messages if true */
17843 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
17844 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
17845 u8 mTrace; /* zero or more SQLITE_TRACE flags */
17846 u8 noSharedCache; /* True if no shared-cache backends */
17847 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
17848 u8 eOpenState; /* Current condition of the connection */
17849 int nextPagesize; /* Pagesize after VACUUM if >0 */
17850 i64 nChange; /* Value returned by sqlite3_changes() */
17851 i64 nTotalChange; /* Value returned by sqlite3_total_changes() */
17852 int aLimit[SQLITE_N_LIMIT(11 +1)]; /* Limits */
17853 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
17854 struct sqlite3InitInfo { /* Information used during initialization */
17855 Pgno newTnum; /* Rootpage of table being initialized */
17856 u8 iDb; /* Which db file is being initialized */
17857 u8 busy; /* TRUE if currently initializing */
17858 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
17859 unsigned imposterTable : 1; /* Building an imposter table */
17860 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
17861 const char **azInit; /* "type", "name", and "tbl_name" columns */
17862 } init;
17863 int nVdbeActive; /* Number of VDBEs currently running */
17864 int nVdbeRead; /* Number of active VDBEs that read or write */
17865 int nVdbeWrite; /* Number of active VDBEs that read and write */
17866 int nVdbeExec; /* Number of nested calls to VdbeExec() */
17867 int nVDestroy; /* Number of active OP_VDestroy operations */
17868 int nExtension; /* Number of loaded extensions */
17869 void **aExtension; /* Array of shared library handles */
17870 union {
17871 void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */
17872 int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */
17873 } trace;
17874 void *pTraceArg; /* Argument to the trace function */
17875#ifndef SQLITE_OMIT_DEPRECATED1
17876 void (*xProfile)(void*,const char*,u64); /* Profiling function */
17877 void *pProfileArg; /* Argument to profile function */
17878#endif
17879 void *pCommitArg; /* Argument to xCommitCallback() */
17880 int (*xCommitCallback)(void*); /* Invoked at every commit. */
17881 void *pRollbackArg; /* Argument to xRollbackCallback() */
17882 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
17883 void *pUpdateArg;
17884 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
17885 void *pAutovacPagesArg; /* Client argument to autovac_pages */
17886 void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */
17887 unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32);
17888 Parse *pParse; /* Current parse */
17889#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
17890 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
17891 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
17892 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
17893 );
17894 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17895#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
17896#ifndef SQLITE_OMIT_WAL
17897 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
17898 void *pWalArg;
17899#endif
17900 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
17901 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
17902 void *pCollNeededArg;
17903 sqlite3_value *pErr; /* Most recent error message */
17904 union {
17905 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
17906 double notUsed1; /* Spacer */
17907 } u1;
17908 Lookaside lookaside; /* Lookaside malloc configuration */
17909#ifndef SQLITE_OMIT_AUTHORIZATION
17910 sqlite3_xauth xAuth; /* Access authorization function */
17911 void *pAuthArg; /* 1st argument to the access auth function */
17912#endif
17913#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
17914 int (*xProgress)(void *); /* The progress callback */
17915 void *pProgressArg; /* Argument to the progress callback */
17916 unsigned nProgressOps; /* Number of opcodes for progress callback */
17917#endif
17918#ifndef SQLITE_OMIT_VIRTUALTABLE
17919 int nVTrans; /* Allocated size of aVTrans */
17920 Hash aModule; /* populated by sqlite3_create_module() */
17921 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
17922 VTable **aVTrans; /* Virtual tables with open transactions */
17923 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
17924#endif
17925 Hash aFunc; /* Hash table of connection functions */
17926 Hash aCollSeq; /* All collating sequences */
17927 BusyHandler busyHandler; /* Busy callback */
17928 Db aDbStatic[2]; /* Static space for the 2 default backends */
17929 Savepoint *pSavepoint; /* List of active savepoints */
17930 int nAnalysisLimit; /* Number of index rows to ANALYZE */
17931 int busyTimeout; /* Busy handler timeout, in msec */
17932 int nSavepoint; /* Number of non-transaction savepoints */
17933 int nStatement; /* Number of nested statement-transactions */
17934 i64 nDeferredCons; /* Net deferred constraints this transaction. */
17935 i64 nDeferredImmCons; /* Net deferred immediate constraints */
17936 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
17937 DbClientData *pDbData; /* sqlite3_set_clientdata() content */
17938#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
17939 /* The following variables are all protected by the STATIC_MAIN
17940 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
17941 **
17942 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
17943 ** unlock so that it can proceed.
17944 **
17945 ** When X.pBlockingConnection==Y, that means that something that X tried
17946 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
17947 ** held by Y.
17948 */
17949 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
17950 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
17951 void *pUnlockArg; /* Argument to xUnlockNotify */
17952 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
17953 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
17954#endif
17955#ifdef SQLITE_USER_AUTHENTICATION
17956 sqlite3_userauth auth; /* User authentication information */
17957#endif
17958};
17959
17960/*
17961** A macro to discover the encoding of a database.
17962*/
17963#define SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) ((db)->aDb[0].pSchema->enc)
17964#define ENC(db)((db)->enc) ((db)->enc)
17965
17966/*
17967** A u64 constant where the lower 32 bits are all zeros. Only the
17968** upper 32 bits are included in the argument. Necessary because some
17969** C-compilers still do not accept LL integer literals.
17970*/
17971#define HI(X)((u64)(X)<<32) ((u64)(X)<<32)
17972
17973/*
17974** Possible values for the sqlite3.flags.
17975**
17976** Value constraints (enforced via assert()):
17977** SQLITE_FullFSync == PAGER_FULLFSYNC
17978** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
17979** SQLITE_CacheSpill == PAGER_CACHE_SPILL
17980*/
17981#define SQLITE_WriteSchema0x00000001 0x00000001 /* OK to update SQLITE_SCHEMA */
17982#define SQLITE_LegacyFileFmt0x00000002 0x00000002 /* Create new databases in format 1 */
17983#define SQLITE_FullColNames0x00000004 0x00000004 /* Show full column names on SELECT */
17984#define SQLITE_FullFSync0x00000008 0x00000008 /* Use full fsync on the backend */
17985#define SQLITE_CkptFullFSync0x00000010 0x00000010 /* Use full fsync for checkpoint */
17986#define SQLITE_CacheSpill0x00000020 0x00000020 /* OK to spill pager cache */
17987#define SQLITE_ShortColNames0x00000040 0x00000040 /* Show short columns names */
17988#define SQLITE_TrustedSchema0x00000080 0x00000080 /* Allow unsafe functions and
17989 ** vtabs in the schema definition */
17990#define SQLITE_NullCallback0x00000100 0x00000100 /* Invoke the callback once if the */
17991 /* result set is empty */
17992#define SQLITE_IgnoreChecks0x00000200 0x00000200 /* Do not enforce check constraints */
17993#define SQLITE_StmtScanStatus0x00000400 0x00000400 /* Enable stmt_scanstats() counters */
17994#define SQLITE_NoCkptOnClose0x00000800 0x00000800 /* No checkpoint on close()/DETACH */
17995#define SQLITE_ReverseOrder0x00001000 0x00001000 /* Reverse unordered SELECTs */
17996#define SQLITE_RecTriggers0x00002000 0x00002000 /* Enable recursive triggers */
17997#define SQLITE_ForeignKeys0x00004000 0x00004000 /* Enforce foreign key constraints */
17998#define SQLITE_AutoIndex0x00008000 0x00008000 /* Enable automatic indexes */
17999#define SQLITE_LoadExtension0x00010000 0x00010000 /* Enable load_extension */
18000#define SQLITE_LoadExtFunc0x00020000 0x00020000 /* Enable load_extension() SQL func */
18001#define SQLITE_EnableTrigger0x00040000 0x00040000 /* True to enable triggers */
18002#define SQLITE_DeferFKs0x00080000 0x00080000 /* Defer all FK constraints */
18003#define SQLITE_QueryOnly0x00100000 0x00100000 /* Disable database changes */
18004#define SQLITE_CellSizeCk0x00200000 0x00200000 /* Check btree cell sizes on load */
18005#define SQLITE_Fts3Tokenizer0x00400000 0x00400000 /* Enable fts3_tokenizer(2) */
18006#define SQLITE_EnableQPSG0x00800000 0x00800000 /* Query Planner Stability Guarantee*/
18007#define SQLITE_TriggerEQP0x01000000 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
18008#define SQLITE_ResetDatabase0x02000000 0x02000000 /* Reset the database */
18009#define SQLITE_LegacyAlter0x04000000 0x04000000 /* Legacy ALTER TABLE behaviour */
18010#define SQLITE_NoSchemaError0x08000000 0x08000000 /* Do not report schema parse errors*/
18011#define SQLITE_Defensive0x10000000 0x10000000 /* Input SQL is likely hostile */
18012#define SQLITE_DqsDDL0x20000000 0x20000000 /* dbl-quoted strings allowed in DDL*/
18013#define SQLITE_DqsDML0x40000000 0x40000000 /* dbl-quoted strings allowed in DML*/
18014#define SQLITE_EnableView0x80000000 0x80000000 /* Enable the use of views */
18015#define SQLITE_CountRows((u64)(0x00001)<<32) HI(0x00001)((u64)(0x00001)<<32) /* Count rows changed by INSERT, */
18016 /* DELETE, or UPDATE and return */
18017 /* the count using a callback. */
18018#define SQLITE_CorruptRdOnly((u64)(0x00002)<<32) HI(0x00002)((u64)(0x00002)<<32) /* Prohibit writes due to error */
18019#define SQLITE_ReadUncommit((u64)(0x00004)<<32) HI(0x00004)((u64)(0x00004)<<32) /* READ UNCOMMITTED in shared-cache */
18020#define SQLITE_FkNoAction((u64)(0x00008)<<32) HI(0x00008)((u64)(0x00008)<<32) /* Treat all FK as NO ACTION */
18021
18022/* Flags used only if debugging */
18023#ifdef SQLITE_DEBUG
18024#define SQLITE_SqlTrace HI(0x0100000)((u64)(0x0100000)<<32) /* Debug print SQL as it executes */
18025#define SQLITE_VdbeListing HI(0x0200000)((u64)(0x0200000)<<32) /* Debug listings of VDBE progs */
18026#define SQLITE_VdbeTrace HI(0x0400000)((u64)(0x0400000)<<32) /* True to trace VDBE execution */
18027#define SQLITE_VdbeAddopTrace HI(0x0800000)((u64)(0x0800000)<<32) /* Trace sqlite3VdbeAddOp() calls */
18028#define SQLITE_VdbeEQP HI(0x1000000)((u64)(0x1000000)<<32) /* Debug EXPLAIN QUERY PLAN */
18029#define SQLITE_ParserTrace HI(0x2000000)((u64)(0x2000000)<<32) /* PRAGMA parser_trace=ON */
18030#endif
18031
18032/*
18033** Allowed values for sqlite3.mDbFlags
18034*/
18035#define DBFLAG_SchemaChange0x0001 0x0001 /* Uncommitted Hash table changes */
18036#define DBFLAG_PreferBuiltin0x0002 0x0002 /* Preference to built-in funcs */
18037#define DBFLAG_Vacuum0x0004 0x0004 /* Currently in a VACUUM */
18038#define DBFLAG_VacuumInto0x0008 0x0008 /* Currently running VACUUM INTO */
18039#define DBFLAG_SchemaKnownOk0x0010 0x0010 /* Schema is known to be valid */
18040#define DBFLAG_InternalFunc0x0020 0x0020 /* Allow use of internal functions */
18041#define DBFLAG_EncodingFixed0x0040 0x0040 /* No longer possible to change enc. */
18042
18043/*
18044** Bits of the sqlite3.dbOptFlags field that are used by the
18045** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
18046** selectively disable various optimizations.
18047*/
18048#define SQLITE_QueryFlattener0x00000001 0x00000001 /* Query flattening */
18049#define SQLITE_WindowFunc0x00000002 0x00000002 /* Use xInverse for window functions */
18050#define SQLITE_GroupByOrder0x00000004 0x00000004 /* GROUPBY cover of ORDERBY */
18051#define SQLITE_FactorOutConst0x00000008 0x00000008 /* Constant factoring */
18052#define SQLITE_DistinctOpt0x00000010 0x00000010 /* DISTINCT using indexes */
18053#define SQLITE_CoverIdxScan0x00000020 0x00000020 /* Covering index scans */
18054#define SQLITE_OrderByIdxJoin0x00000040 0x00000040 /* ORDER BY of joins via index */
18055#define SQLITE_Transitive0x00000080 0x00000080 /* Transitive constraints */
18056#define SQLITE_OmitNoopJoin0x00000100 0x00000100 /* Omit unused tables in joins */
18057#define SQLITE_CountOfView0x00000200 0x00000200 /* The count-of-view optimization */
18058#define SQLITE_CursorHints0x00000400 0x00000400 /* Add OP_CursorHint opcodes */
18059#define SQLITE_Stat40x00000800 0x00000800 /* Use STAT4 data */
18060 /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */
18061#define SQLITE_PushDown0x00001000 0x00001000 /* WHERE-clause push-down opt */
18062#define SQLITE_SimplifyJoin0x00002000 0x00002000 /* Convert LEFT JOIN to JOIN */
18063#define SQLITE_SkipScan0x00004000 0x00004000 /* Skip-scans */
18064#define SQLITE_PropagateConst0x00008000 0x00008000 /* The constant propagation opt */
18065#define SQLITE_MinMaxOpt0x00010000 0x00010000 /* The min/max optimization */
18066#define SQLITE_SeekScan0x00020000 0x00020000 /* The OP_SeekScan optimization */
18067#define SQLITE_OmitOrderBy0x00040000 0x00040000 /* Omit pointless ORDER BY */
18068 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
18069#define SQLITE_BloomFilter0x00080000 0x00080000 /* Use a Bloom filter on searches */
18070#define SQLITE_BloomPulldown0x00100000 0x00100000 /* Run Bloom filters early */
18071#define SQLITE_BalancedMerge0x00200000 0x00200000 /* Balance multi-way merges */
18072#define SQLITE_ReleaseReg0x00400000 0x00400000 /* Use OP_ReleaseReg for testing */
18073#define SQLITE_FlttnUnionAll0x00800000 0x00800000 /* Disable the UNION ALL flattener */
18074 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
18075#define SQLITE_IndexedExpr0x01000000 0x01000000 /* Pull exprs from index when able */
18076#define SQLITE_Coroutines0x02000000 0x02000000 /* Co-routines for subqueries */
18077#define SQLITE_NullUnusedCols0x04000000 0x04000000 /* NULL unused columns in subqueries */
18078#define SQLITE_OnePass0x08000000 0x08000000 /* Single-pass DELETE and UPDATE */
18079#define SQLITE_OrderBySubq0x10000000 0x10000000 /* ORDER BY in subquery helps outer */
18080#define SQLITE_AllOpts0xffffffff 0xffffffff /* All optimizations */
18081
18082/*
18083** Macros for testing whether or not optimizations are enabled or disabled.
18084*/
18085#define OptimizationDisabled(db, mask)(((db)->dbOptFlags&(mask))!=0) (((db)->dbOptFlags&(mask))!=0)
18086#define OptimizationEnabled(db, mask)(((db)->dbOptFlags&(mask))==0) (((db)->dbOptFlags&(mask))==0)
18087
18088/*
18089** Return true if it OK to factor constant expressions into the initialization
18090** code. The argument is a Parse object for the code generator.
18091*/
18092#define ConstFactorOk(P)((P)->okConstFactor) ((P)->okConstFactor)
18093
18094/* Possible values for the sqlite3.eOpenState field.
18095** The numbers are randomly selected such that a minimum of three bits must
18096** change to convert any number to another or to zero
18097*/
18098#define SQLITE_STATE_OPEN0x76 0x76 /* Database is open */
18099#define SQLITE_STATE_CLOSED0xce 0xce /* Database is closed */
18100#define SQLITE_STATE_SICK0xba 0xba /* Error and awaiting close */
18101#define SQLITE_STATE_BUSY0x6d 0x6d /* Database currently in use */
18102#define SQLITE_STATE_ERROR0xd5 0xd5 /* An SQLITE_MISUSE error occurred */
18103#define SQLITE_STATE_ZOMBIE0xa7 0xa7 /* Close with last statement close */
18104
18105/*
18106** Each SQL function is defined by an instance of the following
18107** structure. For global built-in functions (ex: substr(), max(), count())
18108** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
18109** For per-connection application-defined functions, a pointer to this
18110** structure is held in the db->aHash hash table.
18111**
18112** The u.pHash field is used by the global built-ins. The u.pDestructor
18113** field is used by per-connection app-def functions.
18114*/
18115struct FuncDef {
18116 i8 nArg; /* Number of arguments. -1 means unlimited */
18117 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
18118 void *pUserData; /* User data parameter */
18119 FuncDef *pNext; /* Next function with same name */
18120 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
18121 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
18122 void (*xValue)(sqlite3_context*); /* Current agg value */
18123 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
18124 const char *zName; /* SQL name of the function. */
18125 union {
18126 FuncDef *pHash; /* Next with a different name but the same hash */
18127 FuncDestructor *pDestructor; /* Reference counted destructor function */
18128 } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */
18129};
18130
18131/*
18132** This structure encapsulates a user-function destructor callback (as
18133** configured using create_function_v2()) and a reference counter. When
18134** create_function_v2() is called to create a function with a destructor,
18135** a single object of this type is allocated. FuncDestructor.nRef is set to
18136** the number of FuncDef objects created (either 1 or 3, depending on whether
18137** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
18138** member of each of the new FuncDef objects is set to point to the allocated
18139** FuncDestructor.
18140**
18141** Thereafter, when one of the FuncDef objects is deleted, the reference
18142** count on this object is decremented. When it reaches 0, the destructor
18143** is invoked and the FuncDestructor structure freed.
18144*/
18145struct FuncDestructor {
18146 int nRef;
18147 void (*xDestroy)(void *);
18148 void *pUserData;
18149};
18150
18151/*
18152** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
18153** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
18154** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
18155** are assert() statements in the code to verify this.
18156**
18157** Value constraints (enforced via assert()):
18158** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
18159** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
18160** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
18161** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
18162** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG
18163** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
18164** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
18165** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
18166** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
18167**
18168** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the
18169** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is
18170** used internally and if set means that the function has side effects.
18171** SQLITE_INNOCUOUS is used by application code and means "not unsafe".
18172** See multiple instances of tag-20230109-1.
18173*/
18174#define SQLITE_FUNC_ENCMASK0x0003 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
18175#define SQLITE_FUNC_LIKE0x0004 0x0004 /* Candidate for the LIKE optimization */
18176#define SQLITE_FUNC_CASE0x0008 0x0008 /* Case-sensitive LIKE-type function */
18177#define SQLITE_FUNC_EPHEM0x0010 0x0010 /* Ephemeral. Delete with VDBE */
18178#define SQLITE_FUNC_NEEDCOLL0x0020 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
18179#define SQLITE_FUNC_LENGTH0x0040 0x0040 /* Built-in length() function */
18180#define SQLITE_FUNC_TYPEOF0x0080 0x0080 /* Built-in typeof() function */
18181#define SQLITE_FUNC_BYTELEN0x00c0 0x00c0 /* Built-in octet_length() function */
18182#define SQLITE_FUNC_COUNT0x0100 0x0100 /* Built-in count(*) aggregate */
18183/* 0x0200 -- available for reuse */
18184#define SQLITE_FUNC_UNLIKELY0x0400 0x0400 /* Built-in unlikely() function */
18185#define SQLITE_FUNC_CONSTANT0x0800 0x0800 /* Constant inputs give a constant output */
18186#define SQLITE_FUNC_MINMAX0x1000 0x1000 /* True for min() and max() aggregates */
18187#define SQLITE_FUNC_SLOCHNG0x2000 0x2000 /* "Slow Change". Value constant during a
18188 ** single query - might change over time */
18189#define SQLITE_FUNC_TEST0x4000 0x4000 /* Built-in testing functions */
18190#define SQLITE_FUNC_RUNONLY0x8000 0x8000 /* Cannot be used by valueFromFunction */
18191#define SQLITE_FUNC_WINDOW0x00010000 0x00010000 /* Built-in window-only function */
18192#define SQLITE_FUNC_INTERNAL0x00040000 0x00040000 /* For use by NestedParse() only */
18193#define SQLITE_FUNC_DIRECT0x00080000 0x00080000 /* Not for use in TRIGGERs or VIEWs */
18194/* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
18195#define SQLITE_FUNC_UNSAFE0x00200000 0x00200000 /* Function has side effects */
18196#define SQLITE_FUNC_INLINE0x00400000 0x00400000 /* Functions implemented in-line */
18197#define SQLITE_FUNC_BUILTIN0x00800000 0x00800000 /* This is a built-in function */
18198/* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
18199#define SQLITE_FUNC_ANYORDER0x08000000 0x08000000 /* count/min/max aggregate */
18200
18201/* Identifier numbers for each in-line function */
18202#define INLINEFUNC_coalesce0 0
18203#define INLINEFUNC_implies_nonnull_row1 1
18204#define INLINEFUNC_expr_implies_expr2 2
18205#define INLINEFUNC_expr_compare3 3
18206#define INLINEFUNC_affinity4 4
18207#define INLINEFUNC_iif5 5
18208#define INLINEFUNC_sqlite_offset6 6
18209#define INLINEFUNC_unlikely99 99 /* Default case */
18210
18211/*
18212** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
18213** used to create the initializers for the FuncDef structures.
18214**
18215** FUNCTION(zName, nArg, iArg, bNC, xFunc)
18216** Used to create a scalar function definition of a function zName
18217** implemented by C function xFunc that accepts nArg arguments. The
18218** value passed as iArg is cast to a (void*) and made available
18219** as the user-data (sqlite3_user_data()) for the function. If
18220** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
18221**
18222** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
18223** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
18224**
18225** SFUNCTION(zName, nArg, iArg, bNC, xFunc)
18226** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
18227** adds the SQLITE_DIRECTONLY flag.
18228**
18229** INLINE_FUNC(zName, nArg, iFuncId, mFlags)
18230** zName is the name of a function that is implemented by in-line
18231** byte code rather than by the usual callbacks. The iFuncId
18232** parameter determines the function id. The mFlags parameter is
18233** optional SQLITE_FUNC_ flags for this function.
18234**
18235** TEST_FUNC(zName, nArg, iFuncId, mFlags)
18236** zName is the name of a test-only function implemented by in-line
18237** byte code rather than by the usual callbacks. The iFuncId
18238** parameter determines the function id. The mFlags parameter is
18239** optional SQLITE_FUNC_ flags for this function.
18240**
18241** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
18242** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
18243** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
18244** and functions like sqlite_version() that can change, but not during
18245** a single query. The iArg is ignored. The user-data is always set
18246** to a NULL pointer. The bNC parameter is not used.
18247**
18248** MFUNCTION(zName, nArg, xPtr, xFunc)
18249** For math-library functions. xPtr is an arbitrary pointer.
18250**
18251** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
18252** Used for "pure" date/time functions, this macro is like DFUNCTION
18253** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
18254** ignored and the user-data for these functions is set to an
18255** arbitrary non-NULL pointer. The bNC parameter is not used.
18256**
18257** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
18258** Used to create an aggregate function definition implemented by
18259** the C functions xStep and xFinal. The first four parameters
18260** are interpreted in the same way as the first 4 parameters to
18261** FUNCTION().
18262**
18263** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
18264** Used to create an aggregate function definition implemented by
18265** the C functions xStep and xFinal. The first four parameters
18266** are interpreted in the same way as the first 4 parameters to
18267** FUNCTION().
18268**
18269** LIKEFUNC(zName, nArg, pArg, flags)
18270** Used to create a scalar function definition of a function zName
18271** that accepts nArg arguments and is implemented by a call to C
18272** function likeFunc. Argument pArg is cast to a (void *) and made
18273** available as the function user-data (sqlite3_user_data()). The
18274** FuncDef.flags variable is set to the value passed as the flags
18275** parameter.
18276*/
18277#define FUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000| 0x0800|1|(bNC*0x0020), ((void*)(long int)(
iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
18278 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18279 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
18280 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18281#define VFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|1|(bNC*0x0020), ((void*)(long int)(iArg)), 0
, xFunc, 0, 0, 0, "zName", {0} }
\
18282 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
18283 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18284#define SFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int
)(iArg)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
18285 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000|SQLITE_FUNC_UNSAFE0x00200000, \
18286 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18287#define MFUNCTION(zName, nArg, xPtr, xFunc){nArg, 0x00800000|0x0800|1, xPtr, 0, xFunc, 0, 0, 0, "zName",
{0} }
\
18288 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81, \
18289 xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
18290#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} }
\
18291 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_DETERMINISTIC0x000000800|SQLITE_FUNC_CONSTANT0x0800|\
18292 SQLITE_UTF81|((bUseCache)*SQLITE_FUNC_RUNONLY0x8000)|\
18293 ((bRS)*SQLITE_SUBTYPE0x000100000)|((bWS)*SQLITE_RESULT_SUBTYPE0x001000000), \
18294 SQLITE_INT_TO_PTR(iArg|((bJsonB)*JSON_BLOB))((void*)(long int)(iArg|((bJsonB)*0x08))),0,xFunc,0, 0, 0, #zName, {0} }
18295#define INLINE_FUNC(zName, nArg, iArg, mFlags){nArg, 0x00800000| 1|0x00400000|0x0800|(mFlags), ((void*)(long
int)(iArg)), 0, versionFunc, 0, 0, 0, "zName", {0} }
\
18296 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18297 SQLITE_UTF81|SQLITE_FUNC_INLINE0x00400000|SQLITE_FUNC_CONSTANT0x0800|(mFlags), \
18298 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, noopFuncversionFunc, 0, 0, 0, #zName, {0} }
18299#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} }
\
18300 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18301 SQLITE_UTF81|SQLITE_FUNC_INTERNAL0x00040000|SQLITE_FUNC_TEST0x4000| \
18302 SQLITE_FUNC_INLINE0x00400000|SQLITE_FUNC_CONSTANT0x0800|(mFlags), \
18303 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, noopFuncversionFunc, 0, 0, 0, #zName, {0} }
18304#define DFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000|0x2000|1, 0, 0, xFunc, 0, 0, 0, "zName", {0
} }
\
18305 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81, \
18306 0, 0, xFunc, 0, 0, 0, #zName, {0} }
18307#define PURE_DATE(zName, nArg, iArg, bNC, xFunc){nArg, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config
, 0, xFunc, 0, 0, 0, "zName", {0} }
\
18308 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18309 SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
18310 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
18311#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} }
\
18312 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18313 SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020)|extraFlags,\
18314 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
18315#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc){nArg, 0x00800000| 0x2000|1|(bNC*0x0020), pArg, 0, xFunc, 0, 0
, 0, "zName", }
\
18316 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18317 SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
18318 pArg, 0, xFunc, 0, 0, 0, #zName, }
18319#define LIKEFUNC(zName, nArg, arg, flags){nArg, 0x00800000|0x0800|1|flags, (void *)arg, 0, likeFunc, 0
, 0, 0, "zName", {0} }
\
18320 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|flags, \
18321 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
18322#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}}
\
18323 {nArg, SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|(nc*SQLITE_FUNC_NEEDCOLL0x0020)|f, \
18324 SQLITE_INT_TO_PTR(arg)((void*)(long int)(arg)), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
18325#define INTERNAL_FUNCTION(zName, nArg, xFunc){nArg, 0x00800000| 0x00040000|1|0x0800, 0, 0, xFunc, 0, 0, 0,
"zName", {0} }
\
18326 {nArg, SQLITE_FUNC_BUILTIN0x00800000|\
18327 SQLITE_FUNC_INTERNAL0x00040000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
18328 0, 0, xFunc, 0, 0, 0, #zName, {0} }
18329
18330
18331/*
18332** All current savepoints are stored in a linked list starting at
18333** sqlite3.pSavepoint. The first element in the list is the most recently
18334** opened savepoint. Savepoints are added to the list by the vdbe
18335** OP_Savepoint instruction.
18336*/
18337struct Savepoint {
18338 char *zName; /* Savepoint name (nul-terminated) */
18339 i64 nDeferredCons; /* Number of deferred fk violations */
18340 i64 nDeferredImmCons; /* Number of deferred imm fk. */
18341 Savepoint *pNext; /* Parent savepoint (if any) */
18342};
18343
18344/*
18345** The following are used as the second parameter to sqlite3Savepoint(),
18346** and as the P1 argument to the OP_Savepoint instruction.
18347*/
18348#define SAVEPOINT_BEGIN0 0
18349#define SAVEPOINT_RELEASE1 1
18350#define SAVEPOINT_ROLLBACK2 2
18351
18352
18353/*
18354** Each SQLite module (virtual table definition) is defined by an
18355** instance of the following structure, stored in the sqlite3.aModule
18356** hash table.
18357*/
18358struct Module {
18359 const sqlite3_module *pModule; /* Callback pointers */
18360 const char *zName; /* Name passed to create_module() */
18361 int nRefModule; /* Number of pointers to this object */
18362 void *pAux; /* pAux passed to create_module() */
18363 void (*xDestroy)(void *); /* Module destructor function */
18364 Table *pEpoTab; /* Eponymous table for this module */
18365};
18366
18367/*
18368** Information about each column of an SQL table is held in an instance
18369** of the Column structure, in the Table.aCol[] array.
18370**
18371** Definitions:
18372**
18373** "table column index" This is the index of the column in the
18374** Table.aCol[] array, and also the index of
18375** the column in the original CREATE TABLE stmt.
18376**
18377** "storage column index" This is the index of the column in the
18378** record BLOB generated by the OP_MakeRecord
18379** opcode. The storage column index is less than
18380** or equal to the table column index. It is
18381** equal if and only if there are no VIRTUAL
18382** columns to the left.
18383**
18384** Notes on zCnName:
18385** The zCnName field stores the name of the column, the datatype of the
18386** column, and the collating sequence for the column, in that order, all in
18387** a single allocation. Each string is 0x00 terminated. The datatype
18388** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the
18389** collating sequence name is only included if the COLFLAG_HASCOLL bit is
18390** set.
18391*/
18392struct Column {
18393 char *zCnName; /* Name of this column */
18394 unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */
18395 unsigned eCType :4; /* One of the standard types */
18396 char affinity; /* One of the SQLITE_AFF_... values */
18397 u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */
18398 u8 hName; /* Column name hash for faster lookup */
18399 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
18400 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
18401};
18402
18403/* Allowed values for Column.eCType.
18404**
18405** Values must match entries in the global constant arrays
18406** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more
18407** than the offset into these arrays for the corresponding name.
18408** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
18409*/
18410#define COLTYPE_CUSTOM0 0 /* Type appended to zName */
18411#define COLTYPE_ANY1 1
18412#define COLTYPE_BLOB2 2
18413#define COLTYPE_INT3 3
18414#define COLTYPE_INTEGER4 4
18415#define COLTYPE_REAL5 5
18416#define COLTYPE_TEXT6 6
18417#define SQLITE_N_STDTYPE6 6 /* Number of standard types */
18418
18419/* Allowed values for Column.colFlags.
18420**
18421** Constraints:
18422** TF_HasVirtual == COLFLAG_VIRTUAL
18423** TF_HasStored == COLFLAG_STORED
18424** TF_HasHidden == COLFLAG_HIDDEN
18425*/
18426#define COLFLAG_PRIMKEY0x0001 0x0001 /* Column is part of the primary key */
18427#define COLFLAG_HIDDEN0x0002 0x0002 /* A hidden column in a virtual table */
18428#define COLFLAG_HASTYPE0x0004 0x0004 /* Type name follows column name */
18429#define COLFLAG_UNIQUE0x0008 0x0008 /* Column def contains "UNIQUE" or "PK" */
18430#define COLFLAG_SORTERREF0x0010 0x0010 /* Use sorter-refs with this column */
18431#define COLFLAG_VIRTUAL0x0020 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */
18432#define COLFLAG_STORED0x0040 0x0040 /* GENERATED ALWAYS AS ... STORED */
18433#define COLFLAG_NOTAVAIL0x0080 0x0080 /* STORED column not yet calculated */
18434#define COLFLAG_BUSY0x0100 0x0100 /* Blocks recursion on GENERATED columns */
18435#define COLFLAG_HASCOLL0x0200 0x0200 /* Has collating sequence name in zCnName */
18436#define COLFLAG_NOEXPAND0x0400 0x0400 /* Omit this column when expanding "*" */
18437#define COLFLAG_GENERATED0x0060 0x0060 /* Combo: _STORED, _VIRTUAL */
18438#define COLFLAG_NOINSERT0x0062 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */
18439
18440/*
18441** A "Collating Sequence" is defined by an instance of the following
18442** structure. Conceptually, a collating sequence consists of a name and
18443** a comparison routine that defines the order of that sequence.
18444**
18445** If CollSeq.xCmp is NULL, it means that the
18446** collating sequence is undefined. Indices built on an undefined
18447** collating sequence may not be read or written.
18448*/
18449struct CollSeq {
18450 char *zName; /* Name of the collating sequence, UTF-8 encoded */
18451 u8 enc; /* Text encoding handled by xCmp() */
18452 void *pUser; /* First argument to xCmp() */
18453 int (*xCmp)(void*,int, const void*, int, const void*);
18454 void (*xDel)(void*); /* Destructor for pUser */
18455};
18456
18457/*
18458** A sort order can be either ASC or DESC.
18459*/
18460#define SQLITE_SO_ASC0 0 /* Sort in ascending order */
18461#define SQLITE_SO_DESC1 1 /* Sort in ascending order */
18462#define SQLITE_SO_UNDEFINED-1 -1 /* No sort order specified */
18463
18464/*
18465** Column affinity types.
18466**
18467** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
18468** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
18469** the speed a little by numbering the values consecutively.
18470**
18471** But rather than start with 0 or 1, we begin with 'A'. That way,
18472** when multiple affinity types are concatenated into a string and
18473** used as the P4 operand, they will be more readable.
18474**
18475** Note also that the numeric types are grouped together so that testing
18476** for a numeric type is a single comparison. And the BLOB type is first.
18477*/
18478#define SQLITE_AFF_NONE0x40 0x40 /* '@' */
18479#define SQLITE_AFF_BLOB0x41 0x41 /* 'A' */
18480#define SQLITE_AFF_TEXT0x42 0x42 /* 'B' */
18481#define SQLITE_AFF_NUMERIC0x43 0x43 /* 'C' */
18482#define SQLITE_AFF_INTEGER0x44 0x44 /* 'D' */
18483#define SQLITE_AFF_REAL0x45 0x45 /* 'E' */
18484#define SQLITE_AFF_FLEXNUM0x46 0x46 /* 'F' */
18485
18486#define sqlite3IsNumericAffinity(X)((X)>=0x43) ((X)>=SQLITE_AFF_NUMERIC0x43)
18487
18488/*
18489** The SQLITE_AFF_MASK values masks off the significant bits of an
18490** affinity value.
18491*/
18492#define SQLITE_AFF_MASK0x47 0x47
18493
18494/*
18495** Additional bit values that can be ORed with an affinity without
18496** changing the affinity.
18497**
18498** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
18499** It causes an assert() to fire if either operand to a comparison
18500** operator is NULL. It is added to certain comparison operators to
18501** prove that the operands are always NOT NULL.
18502*/
18503#define SQLITE_JUMPIFNULL0x10 0x10 /* jumps if either operand is NULL */
18504#define SQLITE_NULLEQ0x80 0x80 /* NULL=NULL */
18505#define SQLITE_NOTNULL0x90 0x90 /* Assert that operands are never NULL */
18506
18507/*
18508** An object of this type is created for each virtual table present in
18509** the database schema.
18510**
18511** If the database schema is shared, then there is one instance of this
18512** structure for each database connection (sqlite3*) that uses the shared
18513** schema. This is because each database connection requires its own unique
18514** instance of the sqlite3_vtab* handle used to access the virtual table
18515** implementation. sqlite3_vtab* handles can not be shared between
18516** database connections, even when the rest of the in-memory database
18517** schema is shared, as the implementation often stores the database
18518** connection handle passed to it via the xConnect() or xCreate() method
18519** during initialization internally. This database connection handle may
18520** then be used by the virtual table implementation to access real tables
18521** within the database. So that they appear as part of the callers
18522** transaction, these accesses need to be made via the same database
18523** connection as that used to execute SQL operations on the virtual table.
18524**
18525** All VTable objects that correspond to a single table in a shared
18526** database schema are initially stored in a linked-list pointed to by
18527** the Table.pVTable member variable of the corresponding Table object.
18528** When an sqlite3_prepare() operation is required to access the virtual
18529** table, it searches the list for the VTable that corresponds to the
18530** database connection doing the preparing so as to use the correct
18531** sqlite3_vtab* handle in the compiled query.
18532**
18533** When an in-memory Table object is deleted (for example when the
18534** schema is being reloaded for some reason), the VTable objects are not
18535** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
18536** immediately. Instead, they are moved from the Table.pVTable list to
18537** another linked list headed by the sqlite3.pDisconnect member of the
18538** corresponding sqlite3 structure. They are then deleted/xDisconnected
18539** next time a statement is prepared using said sqlite3*. This is done
18540** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
18541** Refer to comments above function sqlite3VtabUnlockList() for an
18542** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
18543** list without holding the corresponding sqlite3.mutex mutex.
18544**
18545** The memory for objects of this type is always allocated by
18546** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
18547** the first argument.
18548*/
18549struct VTable {
18550 sqlite3 *db; /* Database connection associated with this table */
18551 Module *pMod; /* Pointer to module implementation */
18552 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
18553 int nRef; /* Number of pointers to this structure */
18554 u8 bConstraint; /* True if constraints are supported */
18555 u8 bAllSchemas; /* True if might use any attached schema */
18556 u8 eVtabRisk; /* Riskiness of allowing hacker access */
18557 int iSavepoint; /* Depth of the SAVEPOINT stack */
18558 VTable *pNext; /* Next in linked list (see above) */
18559};
18560
18561/* Allowed values for VTable.eVtabRisk
18562*/
18563#define SQLITE_VTABRISK_Low0 0
18564#define SQLITE_VTABRISK_Normal1 1
18565#define SQLITE_VTABRISK_High2 2
18566
18567/*
18568** The schema for each SQL table, virtual table, and view is represented
18569** in memory by an instance of the following structure.
18570*/
18571struct Table {
18572 char *zName; /* Name of the table or view */
18573 Column *aCol; /* Information about each column */
18574 Index *pIndex; /* List of SQL indexes on this table. */
18575 char *zColAff; /* String defining the affinity of each column */
18576 ExprList *pCheck; /* All CHECK constraints */
18577 /* ... also used as column name list in a VIEW */
18578 Pgno tnum; /* Root BTree page for this table */
18579 u32 nTabRef; /* Number of pointers to this Table */
18580 u32 tabFlags; /* Mask of TF_* values */
18581 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
18582 i16 nCol; /* Number of columns in this table */
18583 i16 nNVCol; /* Number of columns that are not VIRTUAL */
18584 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18585 LogEst szTabRow; /* Estimated size of each table row in bytes */
18586#ifdef SQLITE_ENABLE_COSTMULT
18587 LogEst costMult; /* Cost multiplier for using this table */
18588#endif
18589 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
18590 u8 eTabType; /* 0: normal, 1: virtual, 2: view */
18591 union {
18592 struct { /* Used by ordinary tables: */
18593 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
18594 FKey *pFKey; /* Linked list of all foreign keys in this table */
18595 ExprList *pDfltList; /* DEFAULT clauses on various columns.
18596 ** Or the AS clause for generated columns. */
18597 } tab;
18598 struct { /* Used by views: */
18599 Select *pSelect; /* View definition */
18600 } view;
18601 struct { /* Used by virtual tables only: */
18602 int nArg; /* Number of arguments to the module */
18603 char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */
18604 VTable *p; /* List of VTable objects. */
18605 } vtab;
18606 } u;
18607 Trigger *pTrigger; /* List of triggers on this object */
18608 Schema *pSchema; /* Schema that contains this table */
18609};
18610
18611/*
18612** Allowed values for Table.tabFlags.
18613**
18614** TF_OOOHidden applies to tables or view that have hidden columns that are
18615** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18616** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18617** the TF_OOOHidden attribute would apply in this case. Such tables require
18618** special handling during INSERT processing. The "OOO" means "Out Of Order".
18619**
18620** Constraints:
18621**
18622** TF_HasVirtual == COLFLAG_VIRTUAL
18623** TF_HasStored == COLFLAG_STORED
18624** TF_HasHidden == COLFLAG_HIDDEN
18625*/
18626#define TF_Readonly0x00000001 0x00000001 /* Read-only system table */
18627#define TF_HasHidden0x00000002 0x00000002 /* Has one or more hidden columns */
18628#define TF_HasPrimaryKey0x00000004 0x00000004 /* Table has a primary key */
18629#define TF_Autoincrement0x00000008 0x00000008 /* Integer primary key is autoincrement */
18630#define TF_HasStat10x00000010 0x00000010 /* nRowLogEst set from sqlite_stat1 */
18631#define TF_HasVirtual0x00000020 0x00000020 /* Has one or more VIRTUAL columns */
18632#define TF_HasStored0x00000040 0x00000040 /* Has one or more STORED columns */
18633#define TF_HasGenerated0x00000060 0x00000060 /* Combo: HasVirtual + HasStored */
18634#define TF_WithoutRowid0x00000080 0x00000080 /* No rowid. PRIMARY KEY is the key */
18635#define TF_MaybeReanalyze0x00000100 0x00000100 /* Maybe run ANALYZE on this table */
18636#define TF_NoVisibleRowid0x00000200 0x00000200 /* No user-visible "rowid" column */
18637#define TF_OOOHidden0x00000400 0x00000400 /* Out-of-Order hidden columns */
18638#define TF_HasNotNull0x00000800 0x00000800 /* Contains NOT NULL constraints */
18639#define TF_Shadow0x00001000 0x00001000 /* True for a shadow table */
18640#define TF_HasStat40x00002000 0x00002000 /* STAT4 info available for this table */
18641#define TF_Ephemeral0x00004000 0x00004000 /* An ephemeral table */
18642#define TF_Eponymous0x00008000 0x00008000 /* An eponymous virtual table */
18643#define TF_Strict0x00010000 0x00010000 /* STRICT mode */
18644
18645/*
18646** Allowed values for Table.eTabType
18647*/
18648#define TABTYP_NORM0 0 /* Ordinary table */
18649#define TABTYP_VTAB1 1 /* Virtual table */
18650#define TABTYP_VIEW2 2 /* A view */
18651
18652#define IsView(X)((X)->eTabType==2) ((X)->eTabType==TABTYP_VIEW2)
18653#define IsOrdinaryTable(X)((X)->eTabType==0) ((X)->eTabType==TABTYP_NORM0)
18654
18655/*
18656** Test to see whether or not a table is a virtual table. This is
18657** done as a macro so that it will be optimized out when virtual
18658** table support is omitted from the build.
18659*/
18660#ifndef SQLITE_OMIT_VIRTUALTABLE
18661# define IsVirtual(X)((X)->eTabType==1) ((X)->eTabType==TABTYP_VTAB1)
18662# define ExprIsVtab(X)((X)->op==168 && (X)->y.pTab->eTabType==1) \
18663 ((X)->op==TK_COLUMN168 && (X)->y.pTab->eTabType==TABTYP_VTAB1)
18664#else
18665# define IsVirtual(X)((X)->eTabType==1) 0
18666# define ExprIsVtab(X)((X)->op==168 && (X)->y.pTab->eTabType==1) 0
18667#endif
18668
18669/*
18670** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
18671** only works for non-virtual tables (ordinary tables and views) and is
18672** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
18673** IsHiddenColumn() macro is general purpose.
18674*/
18675#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
18676# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
18677# define IsOrdinaryHiddenColumn(X)0 (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
18678#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
18679# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
18680# define IsOrdinaryHiddenColumn(X)0 0
18681#else
18682# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) 0
18683# define IsOrdinaryHiddenColumn(X)0 0
18684#endif
18685
18686
18687/* Does the table have a rowid */
18688#define HasRowid(X)(((X)->tabFlags & 0x00000080)==0) (((X)->tabFlags & TF_WithoutRowid0x00000080)==0)
18689#define VisibleRowid(X)(((X)->tabFlags & 0x00000200)==0) (((X)->tabFlags & TF_NoVisibleRowid0x00000200)==0)
18690
18691/* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18692** available. By default, this macro is false
18693*/
18694#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
18695# define ViewCanHaveRowid0 0
18696#else
18697# define ViewCanHaveRowid0 (sqlite3Config.mNoVisibleRowid==0)
18698#endif
18699
18700/*
18701** Each foreign key constraint is an instance of the following structure.
18702**
18703** A foreign key is associated with two tables. The "from" table is
18704** the table that contains the REFERENCES clause that creates the foreign
18705** key. The "to" table is the table that is named in the REFERENCES clause.
18706** Consider this example:
18707**
18708** CREATE TABLE ex1(
18709** a INTEGER PRIMARY KEY,
18710** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
18711** );
18712**
18713** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18714** Equivalent names:
18715**
18716** from-table == child-table
18717** to-table == parent-table
18718**
18719** Each REFERENCES clause generates an instance of the following structure
18720** which is attached to the from-table. The to-table need not exist when
18721** the from-table is created. The existence of the to-table is not checked.
18722**
18723** The list of all parents for child Table X is held at X.pFKey.
18724**
18725** A list of all children for a table named Z (which might not even exist)
18726** is held in Schema.fkeyHash with a hash key of Z.
18727*/
18728struct FKey {
18729 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
18730 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
18731 char *zTo; /* Name of table that the key points to (aka: Parent) */
18732 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
18733 FKey *pPrevTo; /* Previous with the same zTo */
18734 int nCol; /* Number of columns in this key */
18735 /* EV: R-30323-21917 */
18736 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
18737 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
18738 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
18739 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
18740 int iFrom; /* Index of column in pFrom */
18741 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
18742 } aCol[1]; /* One entry for each of nCol columns */
18743};
18744
18745/*
18746** SQLite supports many different ways to resolve a constraint
18747** error. ROLLBACK processing means that a constraint violation
18748** causes the operation in process to fail and for the current transaction
18749** to be rolled back. ABORT processing means the operation in process
18750** fails and any prior changes from that one operation are backed out,
18751** but the transaction is not rolled back. FAIL processing means that
18752** the operation in progress stops and returns an error code. But prior
18753** changes due to the same operation are not backed out and no rollback
18754** occurs. IGNORE means that the particular row that caused the constraint
18755** error is not inserted or updated. Processing continues and no error
18756** is returned. REPLACE means that preexisting database rows that caused
18757** a UNIQUE constraint violation are removed so that the new insert or
18758** update can proceed. Processing continues and no error is reported.
18759** UPDATE applies to insert operations only and means that the insert
18760** is omitted and the DO UPDATE clause of an upsert is run instead.
18761**
18762** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys.
18763** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
18764** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
18765** key is set to NULL. SETDFLT means that the foreign key is set
18766** to its default value. CASCADE means that a DELETE or UPDATE of the
18767** referenced table row is propagated into the row that holds the
18768** foreign key.
18769**
18770** The OE_Default value is a place holder that means to use whatever
18771** conflict resolution algorithm is required from context.
18772**
18773** The following symbolic values are used to record which type
18774** of conflict resolution action to take.
18775*/
18776#define OE_None0 0 /* There is no constraint to check */
18777#define OE_Rollback1 1 /* Fail the operation and rollback the transaction */
18778#define OE_Abort2 2 /* Back out changes but do no rollback transaction */
18779#define OE_Fail3 3 /* Stop the operation but leave all prior changes */
18780#define OE_Ignore4 4 /* Ignore the error. Do not do the INSERT or UPDATE */
18781#define OE_Replace5 5 /* Delete existing record, then do INSERT or UPDATE */
18782#define OE_Update6 6 /* Process as a DO UPDATE in an upsert */
18783#define OE_Restrict7 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
18784#define OE_SetNull8 8 /* Set the foreign key value to NULL */
18785#define OE_SetDflt9 9 /* Set the foreign key value to its default */
18786#define OE_Cascade10 10 /* Cascade the changes */
18787#define OE_Default11 11 /* Do whatever the default action is */
18788
18789
18790/*
18791** An instance of the following structure is passed as the first
18792** argument to sqlite3VdbeKeyCompare and is used to control the
18793** comparison of the two index keys.
18794**
18795** Note that aSortOrder[] and aColl[] have nField+1 slots. There
18796** are nField slots for the columns of an index then one extra slot
18797** for the rowid at the end.
18798*/
18799struct KeyInfo {
18800 u32 nRef; /* Number of references to this KeyInfo object */
18801 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18802 u16 nKeyField; /* Number of key columns in the index */
18803 u16 nAllField; /* Total columns, including key plus others */
18804 sqlite3 *db; /* The database connection */
18805 u8 *aSortFlags; /* Sort order for each column. */
18806 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
18807};
18808
18809/*
18810** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
18811*/
18812#define KEYINFO_ORDER_DESC0x01 0x01 /* DESC sort order */
18813#define KEYINFO_ORDER_BIGNULL0x02 0x02 /* NULL is larger than any other value */
18814
18815/*
18816** This object holds a record which has been parsed out into individual
18817** fields, for the purposes of doing a comparison.
18818**
18819** A record is an object that contains one or more fields of data.
18820** Records are used to store the content of a table row and to store
18821** the key of an index. A blob encoding of a record is created by
18822** the OP_MakeRecord opcode of the VDBE and is disassembled by the
18823** OP_Column opcode.
18824**
18825** An instance of this object serves as a "key" for doing a search on
18826** an index b+tree. The goal of the search is to find the entry that
18827** is closed to the key described by this object. This object might hold
18828** just a prefix of the key. The number of fields is given by
18829** pKeyInfo->nField.
18830**
18831** The r1 and r2 fields are the values to return if this key is less than
18832** or greater than a key in the btree, respectively. These are normally
18833** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18834** is in DESC order.
18835**
18836** The key comparison functions actually return default_rc when they find
18837** an equals comparison. default_rc can be -1, 0, or +1. If there are
18838** multiple entries in the b-tree with the same key (when only looking
18839** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18840** cause the search to find the last match, or +1 to cause the search to
18841** find the first match.
18842**
18843** The key comparison functions will set eqSeen to true if they ever
18844** get and equal results when comparing this structure to a b-tree record.
18845** When default_rc!=0, the search might end up on the record immediately
18846** before the first match or immediately after the last match. The
18847** eqSeen field will indicate whether or not an exact match exists in the
18848** b-tree.
18849*/
18850struct UnpackedRecord {
18851 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18852 Mem *aMem; /* Values */
18853 union {
18854 char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */
18855 i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */
18856 } u;
18857 int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */
18858 u16 nField; /* Number of entries in apMem[] */
18859 i8 default_rc; /* Comparison result if keys are equal */
18860 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
18861 i8 r1; /* Value to return if (lhs < rhs) */
18862 i8 r2; /* Value to return if (lhs > rhs) */
18863 u8 eqSeen; /* True if an equality comparison has been seen */
18864};
18865
18866
18867/*
18868** Each SQL index is represented in memory by an
18869** instance of the following structure.
18870**
18871** The columns of the table that are to be indexed are described
18872** by the aiColumn[] field of this structure. For example, suppose
18873** we have the following table and index:
18874**
18875** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
18876** CREATE INDEX Ex2 ON Ex1(c3,c1);
18877**
18878** In the Table structure describing Ex1, nCol==3 because there are
18879** three columns in the table. In the Index structure describing
18880** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
18881** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
18882** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
18883** The second column to be indexed (c1) has an index of 0 in
18884** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
18885**
18886** The Index.onError field determines whether or not the indexed columns
18887** must be unique and what to do if they are not. When Index.onError=OE_None,
18888** it means this is not a unique index. Otherwise it is a unique index
18889** and the value of Index.onError indicates which conflict resolution
18890** algorithm to employ when an attempt is made to insert a non-unique
18891** element.
18892**
18893** The colNotIdxed bitmask is used in combination with SrcItem.colUsed
18894** for a fast test to see if an index can serve as a covering index.
18895** colNotIdxed has a 1 bit for every column of the original table that
18896** is *not* available in the index. Thus the expression
18897** "colUsed & colNotIdxed" will be non-zero if the index is not a
18898** covering index. The most significant bit of of colNotIdxed will always
18899** be true (note-20221022-a). If a column beyond the 63rd column of the
18900** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18901** and we have to assume either that the index is not covering, or use
18902** an alternative (slower) algorithm to determine whether or not
18903** the index is covering.
18904**
18905** While parsing a CREATE TABLE or CREATE INDEX statement in order to
18906** generate VDBE code (as opposed to parsing one read from an sqlite_schema
18907** table as part of parsing an existing database schema), transient instances
18908** of this structure may be created. In this case the Index.tnum variable is
18909** used to store the address of a VDBE instruction, not a database page
18910** number (it cannot - the database page is not allocated until the VDBE
18911** program is executed). See convertToWithoutRowidTable() for details.
18912*/
18913struct Index {
18914 char *zName; /* Name of this index */
18915 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
18916 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
18917 Table *pTable; /* The SQL table being indexed */
18918 char *zColAff; /* String defining the affinity of each column */
18919 Index *pNext; /* The next index associated with the same table */
18920 Schema *pSchema; /* Schema containing this index */
18921 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
18922 const char **azColl; /* Array of collation sequence names for index */
18923 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
18924 ExprList *aColExpr; /* Column expressions */
18925 Pgno tnum; /* DB Page containing root of this index */
18926 LogEst szIdxRow; /* Estimated average row size in bytes */
18927 u16 nKeyCol; /* Number of columns forming the key */
18928 u16 nColumn; /* Number of columns stored in the index */
18929 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
18930 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
18931 unsigned bUnordered:1; /* Use this index for == or IN queries only */
18932 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
18933 unsigned isResized:1; /* True if resizeIndexObject() has been called */
18934 unsigned isCovering:1; /* True if this is a covering index */
18935 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18936 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
18937 unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
18938 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
18939 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
18940 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
18941 unsigned bHasExpr:1; /* Index contains an expression, either a literal
18942 ** expression, or a reference to a VIRTUAL column */
18943#ifdef SQLITE_ENABLE_STAT4
18944 int nSample; /* Number of elements in aSample[] */
18945 int mxSample; /* Number of slots allocated to aSample[] */
18946 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
18947 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
18948 IndexSample *aSample; /* Samples of the left-most key */
18949 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18950 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18951#endif
18952 Bitmask colNotIdxed; /* Unindexed columns in pTab */
18953};
18954
18955/*
18956** Allowed values for Index.idxType
18957*/
18958#define SQLITE_IDXTYPE_APPDEF0 0 /* Created using CREATE INDEX */
18959#define SQLITE_IDXTYPE_UNIQUE1 1 /* Implements a UNIQUE constraint */
18960#define SQLITE_IDXTYPE_PRIMARYKEY2 2 /* Is the PRIMARY KEY for the table */
18961#define SQLITE_IDXTYPE_IPK3 3 /* INTEGER PRIMARY KEY index */
18962
18963/* Return true if index X is a PRIMARY KEY index */
18964#define IsPrimaryKeyIndex(X)((X)->idxType==2) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY2)
18965
18966/* Return true if index X is a UNIQUE index */
18967#define IsUniqueIndex(X)((X)->onError!=0) ((X)->onError!=OE_None0)
18968
18969/* The Index.aiColumn[] values are normally positive integer. But
18970** there are some negative values that have special meaning:
18971*/
18972#define XN_ROWID(-1) (-1) /* Indexed column is the rowid */
18973#define XN_EXPR(-2) (-2) /* Indexed column is an expression */
18974
18975/*
18976** Each sample stored in the sqlite_stat4 table is represented in memory
18977** using a structure of this type. See documentation at the top of the
18978** analyze.c source file for additional information.
18979*/
18980struct IndexSample {
18981 void *p; /* Pointer to sampled record */
18982 int n; /* Size of record in bytes */
18983 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
18984 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
18985 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
18986};
18987
18988/*
18989** Possible values to use within the flags argument to sqlite3GetToken().
18990*/
18991#define SQLITE_TOKEN_QUOTED0x1 0x1 /* Token is a quoted identifier. */
18992#define SQLITE_TOKEN_KEYWORD0x2 0x2 /* Token is a keyword. */
18993
18994/*
18995** Each token coming out of the lexer is an instance of
18996** this structure. Tokens are also used as part of an expression.
18997**
18998** The memory that "z" points to is owned by other objects. Take care
18999** that the owner of the "z" string does not deallocate the string before
19000** the Token goes out of scope! Very often, the "z" points to some place
19001** in the middle of the Parse.zSql text. But it might also point to a
19002** static string.
19003*/
19004struct Token {
19005 const char *z; /* Text of the token. Not NULL-terminated! */
19006 unsigned int n; /* Number of characters in this token */
19007};
19008
19009/*
19010** An instance of this structure contains information needed to generate
19011** code for a SELECT that contains aggregate functions.
19012**
19013** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
19014** pointer to this structure. The Expr.iAgg field is the index in
19015** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
19016** code for that node.
19017**
19018** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
19019** original Select structure that describes the SELECT statement. These
19020** fields do not need to be freed when deallocating the AggInfo structure.
19021*/
19022struct AggInfo {
19023 u8 directMode; /* Direct rendering mode means take data directly
19024 ** from source tables rather than from accumulators */
19025 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
19026 ** than the source table */
19027 u16 nSortingColumn; /* Number of columns in the sorting index */
19028 int sortingIdx; /* Cursor number of the sorting index */
19029 int sortingIdxPTab; /* Cursor number of pseudo-table */
19030 int iFirstReg; /* First register in range for aCol[] and aFunc[] */
19031 ExprList *pGroupBy; /* The group by clause */
19032 struct AggInfo_col { /* For each column used in source tables */
19033 Table *pTab; /* Source table */
19034 Expr *pCExpr; /* The original expression */
19035 int iTable; /* Cursor number of the source table */
19036 i16 iColumn; /* Column number within the source table */
19037 i16 iSorterColumn; /* Column number in the sorting index */
19038 } *aCol;
19039 int nColumn; /* Number of used entries in aCol[] */
19040 int nAccumulator; /* Number of columns that show through to the output.
19041 ** Additional columns are used only as parameters to
19042 ** aggregate functions */
19043 struct AggInfo_func { /* For each aggregate function */
19044 Expr *pFExpr; /* Expression encoding the function */
19045 FuncDef *pFunc; /* The aggregate function implementation */
19046 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
19047 int iDistAddr; /* Address of OP_OpenEphemeral */
19048 int iOBTab; /* Ephemeral table to implement ORDER BY */
19049 u8 bOBPayload; /* iOBTab has payload columns separate from key */
19050 u8 bOBUnique; /* Enforce uniqueness on iOBTab keys */
19051 u8 bUseSubtype; /* Transfer subtype info through sorter */
19052 } *aFunc;
19053 int nFunc; /* Number of entries in aFunc[] */
19054 u32 selId; /* Select to which this AggInfo belongs */
19055#ifdef SQLITE_DEBUG
19056 Select *pSelect; /* SELECT statement that this AggInfo supports */
19057#endif
19058};
19059
19060/*
19061** Macros to compute aCol[] and aFunc[] register numbers.
19062**
19063** These macros should not be used prior to the call to
19064** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
19065** The assert()s that are part of this macro verify that constraint.
19066*/
19067#ifndef NDEBUG1
19068#define AggInfoColumnReg(A,I)((A)->iFirstReg+(I)) (assert((A)->iFirstReg)((void) (0)),(A)->iFirstReg+(I))
19069#define AggInfoFuncReg(A,I)((A)->iFirstReg+(A)->nColumn+(I)) \
19070 (assert((A)->iFirstReg)((void) (0)),(A)->iFirstReg+(A)->nColumn+(I))
19071#else
19072#define AggInfoColumnReg(A,I)((A)->iFirstReg+(I)) ((A)->iFirstReg+(I))
19073#define AggInfoFuncReg(A,I)((A)->iFirstReg+(A)->nColumn+(I)) \
19074 ((A)->iFirstReg+(A)->nColumn+(I))
19075#endif
19076
19077/*
19078** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
19079** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
19080** than 32767 we have to make it 32-bit. 16-bit is preferred because
19081** it uses less memory in the Expr object, which is a big memory user
19082** in systems with lots of prepared statements. And few applications
19083** need more than about 10 or 20 variables. But some extreme users want
19084** to have prepared statements with over 32766 variables, and for them
19085** the option is available (at compile-time).
19086*/
19087#if SQLITE_MAX_VARIABLE_NUMBER500000<32767
19088typedef i16 ynVar;
19089#else
19090typedef int ynVar;
19091#endif
19092
19093/*
19094** Each node of an expression in the parse tree is an instance
19095** of this structure.
19096**
19097** Expr.op is the opcode. The integer parser token codes are reused
19098** as opcodes here. For example, the parser defines TK_GE to be an integer
19099** code representing the ">=" operator. This same integer code is reused
19100** to represent the greater-than-or-equal-to operator in the expression
19101** tree.
19102**
19103** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
19104** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If
19105** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the
19106** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
19107** then Expr.u.zToken contains the name of the function.
19108**
19109** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
19110** binary operator. Either or both may be NULL.
19111**
19112** Expr.x.pList is a list of arguments if the expression is an SQL function,
19113** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
19114** Expr.x.pSelect is used if the expression is a sub-select or an expression of
19115** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
19116** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
19117** valid.
19118**
19119** An expression of the form ID or ID.ID refers to a column in a table.
19120** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
19121** the integer cursor number of a VDBE cursor pointing to that table and
19122** Expr.iColumn is the column number for the specific column. If the
19123** expression is used as a result in an aggregate SELECT, then the
19124** value is also stored in the Expr.iAgg column in the aggregate so that
19125** it can be accessed after all aggregates are computed.
19126**
19127** If the expression is an unbound variable marker (a question mark
19128** character '?' in the original SQL) then the Expr.iTable holds the index
19129** number for that variable.
19130**
19131** If the expression is a subquery then Expr.iColumn holds an integer
19132** register number containing the result of the subquery. If the
19133** subquery gives a constant result, then iTable is -1. If the subquery
19134** gives a different answer at different times during statement processing
19135** then iTable is the address of a subroutine that computes the subquery.
19136**
19137** If the Expr is of type OP_Column, and the table it is selecting from
19138** is a disk table or the "old.*" pseudo-table, then pTab points to the
19139** corresponding table definition.
19140**
19141** ALLOCATION NOTES:
19142**
19143** Expr objects can use a lot of memory space in database schema. To
19144** help reduce memory requirements, sometimes an Expr object will be
19145** truncated. And to reduce the number of memory allocations, sometimes
19146** two or more Expr objects will be stored in a single memory allocation,
19147** together with Expr.u.zToken strings.
19148**
19149** If the EP_Reduced and EP_TokenOnly flags are set when
19150** an Expr object is truncated. When EP_Reduced is set, then all
19151** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
19152** are contained within the same memory allocation. Note, however, that
19153** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
19154** allocated, regardless of whether or not EP_Reduced is set.
19155*/
19156struct Expr {
19157 u8 op; /* Operation performed by this node */
19158 char affExpr; /* affinity, or RAISE type */
19159 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
19160 ** TK_COLUMN: the value of p5 for OP_Column
19161 ** TK_AGG_FUNCTION: nesting depth
19162 ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */
19163#ifdef SQLITE_DEBUG
19164 u8 vvaFlags; /* Verification flags. */
19165#endif
19166 u32 flags; /* Various flags. EP_* See below */
19167 union {
19168 char *zToken; /* Token value. Zero terminated and dequoted */
19169 int iValue; /* Non-negative integer value if EP_IntValue */
19170 } u;
19171
19172 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
19173 ** space is allocated for the fields below this point. An attempt to
19174 ** access them will result in a segfault or malfunction.
19175 *********************************************************************/
19176
19177 Expr *pLeft; /* Left subnode */
19178 Expr *pRight; /* Right subnode */
19179 union {
19180 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
19181 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
19182 } x;
19183
19184 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
19185 ** space is allocated for the fields below this point. An attempt to
19186 ** access them will result in a segfault or malfunction.
19187 *********************************************************************/
19188
19189#if SQLITE_MAX_EXPR_DEPTH1000>0
19190 int nHeight; /* Height of the tree headed by this node */
19191#endif
19192 int iTable; /* TK_COLUMN: cursor number of table holding column
19193 ** TK_REGISTER: register number
19194 ** TK_TRIGGER: 1 -> new, 0 -> old
19195 ** EP_Unlikely: 134217728 times likelihood
19196 ** TK_IN: ephemeral table holding RHS
19197 ** TK_SELECT_COLUMN: Number of columns on the LHS
19198 ** TK_SELECT: 1st register of result vector */
19199 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
19200 ** TK_VARIABLE: variable number (always >= 1).
19201 ** TK_SELECT_COLUMN: column of the result vector */
19202 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
19203 union {
19204 int iJoin; /* If EP_OuterON or EP_InnerON, the right table */
19205 int iOfst; /* else: start of token from start of statement */
19206 } w;
19207 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
19208 union {
19209 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
19210 ** for a column of an index on an expression */
19211 Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
19212 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
19213 int iAddr; /* Subroutine entry address */
19214 int regReturn; /* Register used to hold return address */
19215 } sub;
19216 } y;
19217};
19218
19219/* The following are the meanings of bits in the Expr.flags field.
19220** Value restrictions:
19221**
19222** EP_Agg == NC_HasAgg == SF_HasAgg
19223** EP_Win == NC_HasWin
19224*/
19225#define EP_OuterON0x000001 0x000001 /* Originates in ON/USING clause of outer join */
19226#define EP_InnerON0x000002 0x000002 /* Originates in ON/USING of an inner join */
19227#define EP_Distinct0x000004 0x000004 /* Aggregate function with DISTINCT keyword */
19228#define EP_HasFunc0x000008 0x000008 /* Contains one or more functions of any kind */
19229#define EP_Agg0x000010 0x000010 /* Contains one or more aggregate functions */
19230#define EP_FixedCol0x000020 0x000020 /* TK_Column with a known fixed value */
19231#define EP_VarSelect0x000040 0x000040 /* pSelect is correlated, not constant */
19232#define EP_DblQuoted0x000080 0x000080 /* token.z was originally in "..." */
19233#define EP_InfixFunc0x000100 0x000100 /* True for an infix function: LIKE, GLOB, etc */
19234#define EP_Collate0x000200 0x000200 /* Tree contains a TK_COLLATE operator */
19235#define EP_Commuted0x000400 0x000400 /* Comparison operator has been commuted */
19236#define EP_IntValue0x000800 0x000800 /* Integer value contained in u.iValue */
19237#define EP_xIsSelect0x001000 0x001000 /* x.pSelect is valid (otherwise x.pList is) */
19238#define EP_Skip0x002000 0x002000 /* Operator does not contribute to affinity */
19239#define EP_Reduced0x004000 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
19240#define EP_Win0x008000 0x008000 /* Contains window functions */
19241#define EP_TokenOnly0x010000 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
19242#define EP_FullSize0x020000 0x020000 /* Expr structure must remain full sized */
19243#define EP_IfNullRow0x040000 0x040000 /* The TK_IF_NULL_ROW opcode */
19244#define EP_Unlikely0x080000 0x080000 /* unlikely() or likelihood() function */
19245#define EP_ConstFunc0x100000 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
19246#define EP_CanBeNull0x200000 0x200000 /* Can be null despite NOT NULL constraint */
19247#define EP_Subquery0x400000 0x400000 /* Tree contains a TK_SELECT operator */
19248#define EP_Leaf0x800000 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
19249#define EP_WinFunc0x1000000 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
19250#define EP_Subrtn0x2000000 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
19251#define EP_Quoted0x4000000 0x4000000 /* TK_ID was originally quoted */
19252#define EP_Static0x8000000 0x8000000 /* Held in memory not obtained from malloc() */
19253#define EP_IsTrue0x10000000 0x10000000 /* Always has boolean value of TRUE */
19254#define EP_IsFalse0x20000000 0x20000000 /* Always has boolean value of FALSE */
19255#define EP_FromDDL0x40000000 0x40000000 /* Originates from sqlite_schema */
19256#define EP_SubtArg0x80000000 0x80000000 /* Is argument to SQLITE_SUBTYPE function */
19257
19258/* The EP_Propagate mask is a set of properties that automatically propagate
19259** upwards into parent nodes.
19260*/
19261#define EP_Propagate(0x000200|0x400000|0x000008) (EP_Collate0x000200|EP_Subquery0x400000|EP_HasFunc0x000008)
19262
19263/* Macros can be used to test, set, or clear bits in the
19264** Expr.flags field.
19265*/
19266#define ExprHasProperty(E,P)(((E)->flags&(P))!=0) (((E)->flags&(P))!=0)
19267#define ExprHasAllProperty(E,P)(((E)->flags&(P))==(P)) (((E)->flags&(P))==(P))
19268#define ExprSetProperty(E,P)(E)->flags|=(P) (E)->flags|=(P)
19269#define ExprClearProperty(E,P)(E)->flags&=~(P) (E)->flags&=~(P)
19270#define ExprAlwaysTrue(E)(((E)->flags&(0x000001|0x10000000))==0x10000000) (((E)->flags&(EP_OuterON0x000001|EP_IsTrue0x10000000))==EP_IsTrue0x10000000)
19271#define ExprAlwaysFalse(E)(((E)->flags&(0x000001|0x20000000))==0x20000000) (((E)->flags&(EP_OuterON0x000001|EP_IsFalse0x20000000))==EP_IsFalse0x20000000)
19272#define ExprIsFullSize(E)(((E)->flags&(0x004000|0x010000))==0) (((E)->flags&(EP_Reduced0x004000|EP_TokenOnly0x010000))==0)
19273
19274/* Macros used to ensure that the correct members of unions are accessed
19275** in Expr.
19276*/
19277#define ExprUseUToken(E)(((E)->flags&0x000800)==0) (((E)->flags&EP_IntValue0x000800)==0)
19278#define ExprUseUValue(E)(((E)->flags&0x000800)!=0) (((E)->flags&EP_IntValue0x000800)!=0)
19279#define ExprUseWOfst(E)(((E)->flags&(0x000002|0x000001))==0) (((E)->flags&(EP_InnerON0x000002|EP_OuterON0x000001))==0)
19280#define ExprUseWJoin(E)(((E)->flags&(0x000002|0x000001))!=0) (((E)->flags&(EP_InnerON0x000002|EP_OuterON0x000001))!=0)
19281#define ExprUseXList(E)(((E)->flags&0x001000)==0) (((E)->flags&EP_xIsSelect0x001000)==0)
19282#define ExprUseXSelect(E)(((E)->flags&0x001000)!=0) (((E)->flags&EP_xIsSelect0x001000)!=0)
19283#define ExprUseYTab(E)(((E)->flags&(0x1000000|0x2000000))==0) (((E)->flags&(EP_WinFunc0x1000000|EP_Subrtn0x2000000))==0)
19284#define ExprUseYWin(E)(((E)->flags&0x1000000)!=0) (((E)->flags&EP_WinFunc0x1000000)!=0)
19285#define ExprUseYSub(E)(((E)->flags&0x2000000)!=0) (((E)->flags&EP_Subrtn0x2000000)!=0)
19286
19287/* Flags for use with Expr.vvaFlags
19288*/
19289#define EP_NoReduce0x01 0x01 /* Cannot EXPRDUP_REDUCE this Expr */
19290#define EP_Immutable0x02 0x02 /* Do not change this Expr node */
19291
19292/* The ExprSetVVAProperty() macro is used for Verification, Validation,
19293** and Accreditation only. It works like ExprSetProperty() during VVA
19294** processes but is a no-op for delivery.
19295*/
19296#ifdef SQLITE_DEBUG
19297# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
19298# define ExprHasVVAProperty(E,P)0 (((E)->vvaFlags&(P))!=0)
19299# define ExprClearVVAProperties(E) (E)->vvaFlags = 0
19300#else
19301# define ExprSetVVAProperty(E,P)
19302# define ExprHasVVAProperty(E,P)0 0
19303# define ExprClearVVAProperties(E)
19304#endif
19305
19306/*
19307** Macros to determine the number of bytes required by a normal Expr
19308** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
19309** and an Expr struct with the EP_TokenOnly flag set.
19310*/
19311#define EXPR_FULLSIZEsizeof(Expr) sizeof(Expr) /* Full size */
19312#define EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) offsetof(Expr,iTable)__builtin_offsetof(Expr, iTable) /* Common features */
19313#define EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) offsetof(Expr,pLeft)__builtin_offsetof(Expr, pLeft) /* Fewer features */
19314
19315/*
19316** Flags passed to the sqlite3ExprDup() function. See the header comment
19317** above sqlite3ExprDup() for details.
19318*/
19319#define EXPRDUP_REDUCE0x0001 0x0001 /* Used reduced-size Expr nodes */
19320
19321/*
19322** True if the expression passed as an argument was a function with
19323** an OVER() clause (a window function).
19324*/
19325#ifdef SQLITE_OMIT_WINDOWFUNC
19326# define IsWindowFunc(p)( ((((p))->flags&(0x1000000))!=0) && p->y.pWin
->eFrmType!=167 )
0
19327#else
19328# define IsWindowFunc(p)( ((((p))->flags&(0x1000000))!=0) && p->y.pWin
->eFrmType!=167 )
( \
19329 ExprHasProperty((p), EP_WinFunc)((((p))->flags&(0x1000000))!=0) && p->y.pWin->eFrmType!=TK_FILTER167 \
19330 )
19331#endif
19332
19333/*
19334** A list of expressions. Each expression may optionally have a
19335** name. An expr/name combination can be used in several ways, such
19336** as the list of "expr AS ID" fields following a "SELECT" or in the
19337** list of "ID = expr" items in an UPDATE. A list of expressions can
19338** also be used as the argument to a function, in which case the a.zName
19339** field is not used.
19340**
19341** In order to try to keep memory usage down, the Expr.a.zEName field
19342** is used for multiple purposes:
19343**
19344** eEName Usage
19345** ---------- -------------------------
19346** ENAME_NAME (1) the AS of result set column
19347** (2) COLUMN= of an UPDATE
19348**
19349** ENAME_TAB DB.TABLE.NAME used to resolve names
19350** of subqueries
19351**
19352** ENAME_SPAN Text of the original result set
19353** expression.
19354*/
19355struct ExprList {
19356 int nExpr; /* Number of expressions on the list */
19357 int nAlloc; /* Number of a[] slots allocated */
19358 struct ExprList_item { /* For each expression in the list */
19359 Expr *pExpr; /* The parse tree for this expression */
19360 char *zEName; /* Token associated with this expression */
19361 struct {
19362 u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */
19363 unsigned eEName :2; /* Meaning of zEName */
19364 unsigned done :1; /* Indicates when processing is finished */
19365 unsigned reusable :1; /* Constant expression is reusable */
19366 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
19367 unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */
19368 unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */
19369 unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */
19370 unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should
19371 ** not be expanded by "*" in parent queries */
19372 } fg;
19373 union {
19374 struct { /* Used by any ExprList other than Parse.pConsExpr */
19375 u16 iOrderByCol; /* For ORDER BY, column number in result set */
19376 u16 iAlias; /* Index into Parse.aAlias[] for zName */
19377 } x;
19378 int iConstExprReg; /* Register in which Expr value is cached. Used only
19379 ** by Parse.pConstExpr */
19380 } u;
19381 } a[1]; /* One slot for each expression in the list */
19382};
19383
19384/*
19385** Allowed values for Expr.a.eEName
19386*/
19387#define ENAME_NAME0 0 /* The AS clause of a result set */
19388#define ENAME_SPAN1 1 /* Complete text of the result set expression */
19389#define ENAME_TAB2 2 /* "DB.TABLE.NAME" for the result set */
19390#define ENAME_ROWID3 3 /* "DB.TABLE._rowid_" for * expansion of rowid */
19391
19392/*
19393** An instance of this structure can hold a simple list of identifiers,
19394** such as the list "a,b,c" in the following statements:
19395**
19396** INSERT INTO t(a,b,c) VALUES ...;
19397** CREATE INDEX idx ON t(a,b,c);
19398** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
19399**
19400** The IdList.a.idx field is used when the IdList represents the list of
19401** column names after a table name in an INSERT statement. In the statement
19402**
19403** INSERT INTO t(a,b,c) ...
19404**
19405** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
19406*/
19407struct IdList {
19408 int nId; /* Number of identifiers on the list */
19409 u8 eU4; /* Which element of a.u4 is valid */
19410 struct IdList_item {
19411 char *zName; /* Name of the identifier */
19412 union {
19413 int idx; /* Index in some Table.aCol[] of a column named zName */
19414 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
19415 } u4;
19416 } a[1];
19417};
19418
19419/*
19420** Allowed values for IdList.eType, which determines which value of the a.u4
19421** is valid.
19422*/
19423#define EU4_NONE0 0 /* Does not use IdList.a.u4 */
19424#define EU4_IDX1 1 /* Uses IdList.a.u4.idx */
19425#define EU4_EXPR2 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
19426
19427/*
19428** Details of the implementation of a subquery.
19429*/
19430struct Subquery {
19431 Select *pSelect; /* A SELECT statement used in place of a table name */
19432 int addrFillSub; /* Address of subroutine to initialize a subquery */
19433 int regReturn; /* Register holding return address of addrFillSub */
19434 int regResult; /* Registers holding results of a co-routine */
19435};
19436
19437/*
19438** The SrcItem object represents a single term in the FROM clause of a query.
19439** The SrcList object is mostly an array of SrcItems.
19440**
19441** The jointype starts out showing the join type between the current table
19442** and the next table on the list. The parser builds the list this way.
19443** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
19444** jointype expresses the join between the table and the previous table.
19445**
19446** In the colUsed field, the high-order bit (bit 63) is set if the table
19447** contains more than 63 columns and the 64-th or later column is used.
19448**
19449** Aggressive use of "union" helps keep the size of the object small. This
19450** has been shown to boost performance, in addition to saving memory.
19451** Access to union elements is gated by the following rules which should
19452** always be checked, either by an if-statement or by an assert().
19453**
19454** Field Only access if this is true
19455** --------------- -----------------------------------
19456** u1.zIndexedBy fg.isIndexedBy
19457** u1.pFuncArg fg.isTabFunc
19458** u1.nRow !fg.isTabFunc && !fg.isIndexedBy
19459**
19460** u2.pIBIndex fg.isIndexedBy
19461** u2.pCteUse fg.isCte
19462**
19463** u3.pOn !fg.isUsing
19464** u3.pUsing fg.isUsing
19465**
19466** u4.zDatabase !fg.fixedSchema && !fg.isSubquery
19467** u4.pSchema fg.fixedSchema
19468** u4.pSubq fg.isSubquery
19469**
19470** See also the sqlite3SrcListDelete() routine for assert() statements that
19471** check invariants on the fields of this object, especially the flags
19472** inside the fg struct.
19473*/
19474struct SrcItem {
19475 char *zName; /* Name of the table */
19476 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
19477 Table *pSTab; /* Table object for zName. Mnemonic: Srcitem-TABle */
19478 struct {
19479 u8 jointype; /* Type of join between this table and the previous */
19480 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
19481 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
19482 unsigned isSubquery :1; /* True if this term is a subquery */
19483 unsigned isTabFunc :1; /* True if table-valued-function syntax */
19484 unsigned isCorrelated :1; /* True if sub-query is correlated */
19485 unsigned isMaterialized:1; /* This is a materialized view */
19486 unsigned viaCoroutine :1; /* Implemented as a co-routine */
19487 unsigned isRecursive :1; /* True for recursive reference in WITH */
19488 unsigned fromDDL :1; /* Comes from sqlite_schema */
19489 unsigned isCte :1; /* This is a CTE */
19490 unsigned notCte :1; /* This item may not match a CTE */
19491 unsigned isUsing :1; /* u3.pUsing is valid */
19492 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19493 unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */
19494 unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
19495 unsigned rowidUsed :1; /* The ROWID of this table is referenced */
19496 unsigned fixedSchema :1; /* Uses u4.pSchema, not u4.zDatabase */
19497 unsigned hadSchema :1; /* Had u4.zDatabase before u4.pSchema */
19498 } fg;
19499 int iCursor; /* The VDBE cursor number used to access this table */
19500 Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
19501 union {
19502 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
19503 ExprList *pFuncArg; /* Arguments to table-valued-function */
19504 u32 nRow; /* Number of rows in a VALUES clause */
19505 } u1;
19506 union {
19507 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
19508 CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */
19509 } u2;
19510 union {
19511 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
19512 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
19513 } u3;
19514 union {
19515 Schema *pSchema; /* Schema to which this item is fixed */
19516 char *zDatabase; /* Name of database holding this table */
19517 Subquery *pSubq; /* Description of a subquery */
19518 } u4;
19519};
19520
19521/*
19522** The OnOrUsing object represents either an ON clause or a USING clause.
19523** It can never be both at the same time, but it can be neither.
19524*/
19525struct OnOrUsing {
19526 Expr *pOn; /* The ON clause of a join */
19527 IdList *pUsing; /* The USING clause of a join */
19528};
19529
19530/*
19531** This object represents one or more tables that are the source of
19532** content for an SQL statement. For example, a single SrcList object
19533** is used to hold the FROM clause of a SELECT statement. SrcList also
19534** represents the target tables for DELETE, INSERT, and UPDATE statements.
19535**
19536*/
19537struct SrcList {
19538 int nSrc; /* Number of tables or subqueries in the FROM clause */
19539 u32 nAlloc; /* Number of entries allocated in a[] below */
19540 SrcItem a[1]; /* One entry for each identifier on the list */
19541};
19542
19543/*
19544** Permitted values of the SrcList.a.jointype field
19545*/
19546#define JT_INNER0x01 0x01 /* Any kind of inner or cross join */
19547#define JT_CROSS0x02 0x02 /* Explicit use of the CROSS keyword */
19548#define JT_NATURAL0x04 0x04 /* True for a "natural" join */
19549#define JT_LEFT0x08 0x08 /* Left outer join */
19550#define JT_RIGHT0x10 0x10 /* Right outer join */
19551#define JT_OUTER0x20 0x20 /* The "OUTER" keyword is present */
19552#define JT_LTORJ0x40 0x40 /* One of the LEFT operands of a RIGHT JOIN
19553 ** Mnemonic: Left Table Of Right Join */
19554#define JT_ERROR0x80 0x80 /* unknown or unsupported join type */
19555
19556/*
19557** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
19558** and the WhereInfo.wctrlFlags member.
19559**
19560** Value constraints (enforced via assert()):
19561** WHERE_USE_LIMIT == SF_FixedLimit
19562*/
19563#define WHERE_ORDERBY_NORMAL0x0000 0x0000 /* No-op */
19564#define WHERE_ORDERBY_MIN0x0001 0x0001 /* ORDER BY processing for min() func */
19565#define WHERE_ORDERBY_MAX0x0002 0x0002 /* ORDER BY processing for max() func */
19566#define WHERE_ONEPASS_DESIRED0x0004 0x0004 /* Want to do one-pass UPDATE/DELETE */
19567#define WHERE_ONEPASS_MULTIROW0x0008 0x0008 /* ONEPASS is ok with multiple rows */
19568#define WHERE_DUPLICATES_OK0x0010 0x0010 /* Ok to return a row more than once */
19569#define WHERE_OR_SUBCLAUSE0x0020 0x0020 /* Processing a sub-WHERE as part of
19570 ** the OR optimization */
19571#define WHERE_GROUPBY0x0040 0x0040 /* pOrderBy is really a GROUP BY */
19572#define WHERE_DISTINCTBY0x0080 0x0080 /* pOrderby is really a DISTINCT clause */
19573#define WHERE_WANT_DISTINCT0x0100 0x0100 /* All output needs to be distinct */
19574#define WHERE_SORTBYGROUP0x0200 0x0200 /* Support sqlite3WhereIsSorted() */
19575#define WHERE_AGG_DISTINCT0x0400 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
19576#define WHERE_ORDERBY_LIMIT0x0800 0x0800 /* ORDERBY+LIMIT on the inner loop */
19577#define WHERE_RIGHT_JOIN0x1000 0x1000 /* Processing a RIGHT JOIN */
19578#define WHERE_KEEP_ALL_JOINS0x2000 0x2000 /* Do not do the omit-noop-join opt */
19579#define WHERE_USE_LIMIT0x4000 0x4000 /* Use the LIMIT in cost estimates */
19580 /* 0x8000 not currently used */
19581
19582/* Allowed return values from sqlite3WhereIsDistinct()
19583*/
19584#define WHERE_DISTINCT_NOOP0 0 /* DISTINCT keyword not used */
19585#define WHERE_DISTINCT_UNIQUE1 1 /* No duplicates */
19586#define WHERE_DISTINCT_ORDERED2 2 /* All duplicates are adjacent */
19587#define WHERE_DISTINCT_UNORDERED3 3 /* Duplicates are scattered */
19588
19589/*
19590** A NameContext defines a context in which to resolve table and column
19591** names. The context consists of a list of tables (the pSrcList) field and
19592** a list of named expression (pEList). The named expression list may
19593** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
19594** to the table being operated on by INSERT, UPDATE, or DELETE. The
19595** pEList corresponds to the result set of a SELECT and is NULL for
19596** other statements.
19597**
19598** NameContexts can be nested. When resolving names, the inner-most
19599** context is searched first. If no match is found, the next outer
19600** context is checked. If there is still no match, the next context
19601** is checked. This process continues until either a match is found
19602** or all contexts are check. When a match is found, the nRef member of
19603** the context containing the match is incremented.
19604**
19605** Each subquery gets a new NameContext. The pNext field points to the
19606** NameContext in the parent query. Thus the process of scanning the
19607** NameContext list corresponds to searching through successively outer
19608** subqueries looking for a match.
19609*/
19610struct NameContext {
19611 Parse *pParse; /* The parser */
19612 SrcList *pSrcList; /* One or more tables used to resolve names */
19613 union {
19614 ExprList *pEList; /* Optional list of result-set columns */
19615 AggInfo *pAggInfo; /* Information about aggregates at this level */
19616 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
19617 int iBaseReg; /* For TK_REGISTER when parsing RETURNING */
19618 } uNC;
19619 NameContext *pNext; /* Next outer name context. NULL for outermost */
19620 int nRef; /* Number of names resolved by this context */
19621 int nNcErr; /* Number of errors encountered while resolving names */
19622 int ncFlags; /* Zero or more NC_* flags defined below */
19623 u32 nNestedSelect; /* Number of nested selects using this NC */
19624 Select *pWinSelect; /* SELECT statement for any window functions */
19625};
19626
19627/*
19628** Allowed values for the NameContext, ncFlags field.
19629**
19630** Value constraints (all checked via assert()):
19631** NC_HasAgg == SF_HasAgg == EP_Agg
19632** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
19633** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER
19634** NC_HasWin == EP_Win
19635**
19636*/
19637#define NC_AllowAgg0x000001 0x000001 /* Aggregate functions are allowed here */
19638#define NC_PartIdx0x000002 0x000002 /* True if resolving a partial index WHERE */
19639#define NC_IsCheck0x000004 0x000004 /* True if resolving a CHECK constraint */
19640#define NC_GenCol0x000008 0x000008 /* True for a GENERATED ALWAYS AS clause */
19641#define NC_HasAgg0x000010 0x000010 /* One or more aggregate functions seen */
19642#define NC_IdxExpr0x000020 0x000020 /* True if resolving columns of CREATE INDEX */
19643#define NC_SelfRef0x00002e 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */
19644#define NC_Subquery0x000040 0x000040 /* A subquery has been seen */
19645#define NC_UEList0x000080 0x000080 /* True if uNC.pEList is used */
19646#define NC_UAggInfo0x000100 0x000100 /* True if uNC.pAggInfo is used */
19647#define NC_UUpsert0x000200 0x000200 /* True if uNC.pUpsert is used */
19648#define NC_UBaseReg0x000400 0x000400 /* True if uNC.iBaseReg is used */
19649#define NC_MinMaxAgg0x001000 0x001000 /* min/max aggregates seen. See note above */
19650/* 0x002000 // available for reuse */
19651#define NC_AllowWin0x004000 0x004000 /* Window functions are allowed here */
19652#define NC_HasWin0x008000 0x008000 /* One or more window functions seen */
19653#define NC_IsDDL0x010000 0x010000 /* Resolving names in a CREATE statement */
19654#define NC_InAggFunc0x020000 0x020000 /* True if analyzing arguments to an agg func */
19655#define NC_FromDDL0x040000 0x040000 /* SQL text comes from sqlite_schema */
19656#define NC_NoSelect0x080000 0x080000 /* Do not descend into sub-selects */
19657#define NC_Where0x100000 0x100000 /* Processing WHERE clause of a SELECT */
19658#define NC_OrderAgg0x8000000 0x8000000 /* Has an aggregate other than count/min/max */
19659
19660/*
19661** An instance of the following object describes a single ON CONFLICT
19662** clause in an upsert.
19663**
19664** The pUpsertTarget field is only set if the ON CONFLICT clause includes
19665** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19666** conflict-target clause.) The pUpsertTargetWhere is the optional
19667** WHERE clause used to identify partial unique indexes.
19668**
19669** pUpsertSet is the list of column=expr terms of the UPDATE statement.
19670** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
19671** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
19672** WHERE clause is omitted.
19673*/
19674struct Upsert {
19675 ExprList *pUpsertTarget; /* Optional description of conflict target */
19676 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
19677 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
19678 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
19679 Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
19680 u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
19681 u8 isDup; /* True if 2nd or later with same pUpsertIdx */
19682 /* Above this point is the parse tree for the ON CONFLICT clauses.
19683 ** The next group of fields stores intermediate data. */
19684 void *pToFree; /* Free memory when deleting the Upsert object */
19685 /* All fields above are owned by the Upsert object and must be freed
19686 ** when the Upsert is destroyed. The fields below are used to transfer
19687 ** information from the INSERT processing down into the UPDATE processing
19688 ** while generating code. The fields below are owned by the INSERT
19689 ** statement and will be freed by INSERT processing. */
19690 Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */
19691 SrcList *pUpsertSrc; /* Table to be updated */
19692 int regData; /* First register holding array of VALUES */
19693 int iDataCur; /* Index of the data cursor */
19694 int iIdxCur; /* Index of the first index cursor */
19695};
19696
19697/*
19698** An instance of the following structure contains all information
19699** needed to generate code for a single SELECT statement.
19700**
19701** See the header comment on the computeLimitRegisters() routine for a
19702** detailed description of the meaning of the iLimit and iOffset fields.
19703**
19704** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
19705** These addresses must be stored so that we can go back and fill in
19706** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
19707** the number of columns in P2 can be computed at the same time
19708** as the OP_OpenEphm instruction is coded because not
19709** enough information about the compound query is known at that point.
19710** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
19711** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
19712** sequences for the ORDER BY clause.
19713*/
19714struct Select {
19715 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
19716 LogEst nSelectRow; /* Estimated number of result rows */
19717 u32 selFlags; /* Various SF_* values */
19718 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
19719 u32 selId; /* Unique identifier number for this SELECT */
19720 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
19721 ExprList *pEList; /* The fields of the result */
19722 SrcList *pSrc; /* The FROM clause */
19723 Expr *pWhere; /* The WHERE clause */
19724 ExprList *pGroupBy; /* The GROUP BY clause */
19725 Expr *pHaving; /* The HAVING clause */
19726 ExprList *pOrderBy; /* The ORDER BY clause */
19727 Select *pPrior; /* Prior select in a compound select statement */
19728 Select *pNext; /* Next select to the left in a compound */
19729 Expr *pLimit; /* LIMIT expression. NULL means not used. */
19730 With *pWith; /* WITH clause attached to this select. Or NULL. */
19731#ifndef SQLITE_OMIT_WINDOWFUNC
19732 Window *pWin; /* List of window functions */
19733 Window *pWinDefn; /* List of named window definitions */
19734#endif
19735};
19736
19737/*
19738** Allowed values for Select.selFlags. The "SF" prefix stands for
19739** "Select Flag".
19740**
19741** Value constraints (all checked via assert())
19742** SF_HasAgg == NC_HasAgg
19743** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
19744** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER
19745** SF_FixedLimit == WHERE_USE_LIMIT
19746*/
19747#define SF_Distinct0x0000001 0x0000001 /* Output should be DISTINCT */
19748#define SF_All0x0000002 0x0000002 /* Includes the ALL keyword */
19749#define SF_Resolved0x0000004 0x0000004 /* Identifiers have been resolved */
19750#define SF_Aggregate0x0000008 0x0000008 /* Contains agg functions or a GROUP BY */
19751#define SF_HasAgg0x0000010 0x0000010 /* Contains aggregate functions */
19752#define SF_UsesEphemeral0x0000020 0x0000020 /* Uses the OpenEphemeral opcode */
19753#define SF_Expanded0x0000040 0x0000040 /* sqlite3SelectExpand() called on this */
19754#define SF_HasTypeInfo0x0000080 0x0000080 /* FROM subqueries have Table metadata */
19755#define SF_Compound0x0000100 0x0000100 /* Part of a compound query */
19756#define SF_Values0x0000200 0x0000200 /* Synthesized from VALUES clause */
19757#define SF_MultiValue0x0000400 0x0000400 /* Single VALUES term with multiple rows */
19758#define SF_NestedFrom0x0000800 0x0000800 /* Part of a parenthesized FROM clause */
19759#define SF_MinMaxAgg0x0001000 0x0001000 /* Aggregate containing min() or max() */
19760#define SF_Recursive0x0002000 0x0002000 /* The recursive part of a recursive CTE */
19761#define SF_FixedLimit0x0004000 0x0004000 /* nSelectRow set by a constant LIMIT */
19762#define SF_MaybeConvert0x0008000 0x0008000 /* Need convertCompoundSelectToSubquery() */
19763#define SF_Converted0x0010000 0x0010000 /* By convertCompoundSelectToSubquery() */
19764#define SF_IncludeHidden0x0020000 0x0020000 /* Include hidden columns in output */
19765#define SF_ComplexResult0x0040000 0x0040000 /* Result contains subquery or function */
19766#define SF_WhereBegin0x0080000 0x0080000 /* Really a WhereBegin() call. Debug Only */
19767#define SF_WinRewrite0x0100000 0x0100000 /* Window function rewrite accomplished */
19768#define SF_View0x0200000 0x0200000 /* SELECT statement is a view */
19769#define SF_NoopOrderBy0x0400000 0x0400000 /* ORDER BY is ignored for this query */
19770#define SF_UFSrcCheck0x0800000 0x0800000 /* Check pSrc as required by UPDATE...FROM */
19771#define SF_PushDown0x1000000 0x1000000 /* Modified by WHERE-clause push-down opt */
19772#define SF_MultiPart0x2000000 0x2000000 /* Has multiple incompatible PARTITIONs */
19773#define SF_CopyCte0x4000000 0x4000000 /* SELECT statement is a copy of a CTE */
19774#define SF_OrderByReqd0x8000000 0x8000000 /* The ORDER BY clause may not be omitted */
19775#define SF_UpdateFrom0x10000000 0x10000000 /* Query originates with UPDATE FROM */
19776#define SF_Correlated0x20000000 0x20000000 /* True if references the outer context */
19777
19778/* True if SrcItem X is a subquery that has SF_NestedFrom */
19779#define IsNestedFrom(X)((X)->fg.isSubquery && ((X)->u4.pSubq->pSelect
->selFlags&0x0000800)!=0)
\
19780 ((X)->fg.isSubquery && \
19781 ((X)->u4.pSubq->pSelect->selFlags&SF_NestedFrom0x0000800)!=0)
19782
19783/*
19784** The results of a SELECT can be distributed in several ways, as defined
19785** by one of the following macros. The "SRT" prefix means "SELECT Result
19786** Type".
19787**
19788** SRT_Union Store results as a key in a temporary index
19789** identified by pDest->iSDParm.
19790**
19791** SRT_Except Remove results from the temporary index pDest->iSDParm.
19792**
19793** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
19794** set is not empty.
19795**
19796** SRT_Discard Throw the results away. This is used by SELECT
19797** statements within triggers whose only purpose is
19798** the side-effects of functions.
19799**
19800** SRT_Output Generate a row of output (using the OP_ResultRow
19801** opcode) for each row in the result set.
19802**
19803** SRT_Mem Only valid if the result is a single column.
19804** Store the first column of the first result row
19805** in register pDest->iSDParm then abandon the rest
19806** of the query. This destination implies "LIMIT 1".
19807**
19808** SRT_Set The result must be a single column. Store each
19809** row of result as the key in table pDest->iSDParm.
19810** Apply the affinity pDest->affSdst before storing
19811** results. if pDest->iSDParm2 is positive, then it is
19812** a register holding a Bloom filter for the IN operator
19813** that should be populated in addition to the
19814** pDest->iSDParm table. This SRT is used to
19815** implement "IN (SELECT ...)".
19816**
19817** SRT_EphemTab Create an temporary table pDest->iSDParm and store
19818** the result there. The cursor is left open after
19819** returning. This is like SRT_Table except that
19820** this destination uses OP_OpenEphemeral to create
19821** the table first.
19822**
19823** SRT_Coroutine Generate a co-routine that returns a new row of
19824** results each time it is invoked. The entry point
19825** of the co-routine is stored in register pDest->iSDParm
19826** and the result row is stored in pDest->nDest registers
19827** starting with pDest->iSdst.
19828**
19829** SRT_Table Store results in temporary table pDest->iSDParm.
19830** SRT_Fifo This is like SRT_EphemTab except that the table
19831** is assumed to already be open. SRT_Fifo has
19832** the additional property of being able to ignore
19833** the ORDER BY clause.
19834**
19835** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
19836** But also use temporary table pDest->iSDParm+1 as
19837** a record of all prior results and ignore any duplicate
19838** rows. Name means: "Distinct Fifo".
19839**
19840** SRT_Queue Store results in priority queue pDest->iSDParm (really
19841** an index). Append a sequence number so that all entries
19842** are distinct.
19843**
19844** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
19845** the same record has never been stored before. The
19846** index at pDest->iSDParm+1 hold all prior stores.
19847**
19848** SRT_Upfrom Store results in the temporary table already opened by
19849** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
19850** table is an intkey table - in this case the first
19851** column returned by the SELECT is used as the integer
19852** key. If (pDest->iSDParm>0), then the table is an index
19853** table. (pDest->iSDParm) is the number of key columns in
19854** each index record in this case.
19855*/
19856#define SRT_Union1 1 /* Store result as keys in an index */
19857#define SRT_Except2 2 /* Remove result from a UNION index */
19858#define SRT_Exists3 3 /* Store 1 if the result is not empty */
19859#define SRT_Discard4 4 /* Do not save the results anywhere */
19860#define SRT_DistFifo5 5 /* Like SRT_Fifo, but unique results only */
19861#define SRT_DistQueue6 6 /* Like SRT_Queue, but unique results only */
19862
19863/* The DISTINCT clause is ignored for all of the above. Not that
19864** IgnorableDistinct() implies IgnorableOrderby() */
19865#define IgnorableDistinct(X)((X->eDest)<=6) ((X->eDest)<=SRT_DistQueue6)
19866
19867#define SRT_Queue7 7 /* Store result in an queue */
19868#define SRT_Fifo8 8 /* Store result as data with an automatic rowid */
19869
19870/* The ORDER BY clause is ignored for all of the above */
19871#define IgnorableOrderby(X)((X->eDest)<=8) ((X->eDest)<=SRT_Fifo8)
19872
19873#define SRT_Output9 9 /* Output each row of result */
19874#define SRT_Mem10 10 /* Store result in a memory cell */
19875#define SRT_Set11 11 /* Store results as keys in an index */
19876#define SRT_EphemTab12 12 /* Create transient tab and store like SRT_Table */
19877#define SRT_Coroutine13 13 /* Generate a single row of result */
19878#define SRT_Table14 14 /* Store result as data with an automatic rowid */
19879#define SRT_Upfrom15 15 /* Store result as data with rowid */
19880
19881/*
19882** An instance of this object describes where to put of the results of
19883** a SELECT statement.
19884*/
19885struct SelectDest {
19886 u8 eDest; /* How to dispose of the results. One of SRT_* above. */
19887 int iSDParm; /* A parameter used by the eDest disposal method */
19888 int iSDParm2; /* A second parameter for the eDest disposal method */
19889 int iSdst; /* Base register where results are written */
19890 int nSdst; /* Number of registers allocated */
19891 char *zAffSdst; /* Affinity used for SRT_Set */
19892 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
19893};
19894
19895/*
19896** During code generation of statements that do inserts into AUTOINCREMENT
19897** tables, the following information is attached to the Table.u.autoInc.p
19898** pointer of each autoincrement table to record some side information that
19899** the code generator needs. We have to keep per-table autoincrement
19900** information in case inserts are done within triggers. Triggers do not
19901** normally coordinate their activities, but we do need to coordinate the
19902** loading and saving of autoincrement information.
19903*/
19904struct AutoincInfo {
19905 AutoincInfo *pNext; /* Next info block in a list of them all */
19906 Table *pTab; /* Table this info block refers to */
19907 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
19908 int regCtr; /* Memory register holding the rowid counter */
19909};
19910
19911/*
19912** At least one instance of the following structure is created for each
19913** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
19914** statement. All such objects are stored in the linked list headed at
19915** Parse.pTriggerPrg and deleted once statement compilation has been
19916** completed.
19917**
19918** A Vdbe sub-program that implements the body and WHEN clause of trigger
19919** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
19920** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
19921** The Parse.pTriggerPrg list never contains two entries with the same
19922** values for both pTrigger and orconf.
19923**
19924** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
19925** accessed (or set to 0 for triggers fired as a result of INSERT
19926** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
19927** a mask of new.* columns used by the program.
19928*/
19929struct TriggerPrg {
19930 Trigger *pTrigger; /* Trigger this program was coded from */
19931 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
19932 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
19933 int orconf; /* Default ON CONFLICT policy */
19934 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
19935};
19936
19937/*
19938** The yDbMask datatype for the bitmask of all attached databases.
19939*/
19940#if SQLITE_MAX_ATTACHED10>30
19941 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED10+9)/8];
19942# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)[(I)/8]&(1<<((I)&7)))!=0)
19943# define DbMaskZero(M)((M)=0) memset((M),0,sizeof(M))
19944# define DbMaskSet(M,I)((M)|=(((yDbMask)1)<<(I))) (M)[(I)/8]|=(1<<((I)&7))
19945# define DbMaskAllZero(M)((M)==0) sqlite3DbMaskAllZero(M)
19946# define DbMaskNonZero(M)((M)!=0) (sqlite3DbMaskAllZero(M)==0)
19947#else
19948 typedef unsigned int yDbMask;
19949# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)&(((yDbMask)1)<<(I)))!=0)
19950# define DbMaskZero(M)((M)=0) ((M)=0)
19951# define DbMaskSet(M,I)((M)|=(((yDbMask)1)<<(I))) ((M)|=(((yDbMask)1)<<(I)))
19952# define DbMaskAllZero(M)((M)==0) ((M)==0)
19953# define DbMaskNonZero(M)((M)!=0) ((M)!=0)
19954#endif
19955
19956/*
19957** For each index X that has as one of its arguments either an expression
19958** or the name of a virtual generated column, and if X is in scope such that
19959** the value of the expression can simply be read from the index, then
19960** there is an instance of this object on the Parse.pIdxExpr list.
19961**
19962** During code generation, while generating code to evaluate expressions,
19963** this list is consulted and if a matching expression is found, the value
19964** is read from the index rather than being recomputed.
19965*/
19966struct IndexedExpr {
19967 Expr *pExpr; /* The expression contained in the index */
19968 int iDataCur; /* The data cursor associated with the index */
19969 int iIdxCur; /* The index cursor */
19970 int iIdxCol; /* The index column that contains value of pExpr */
19971 u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */
19972 u8 aff; /* Affinity of the pExpr expression */
19973 IndexedExpr *pIENext; /* Next in a list of all indexed expressions */
19974#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
19975 const char *zIdxName; /* Name of index, used only for bytecode comments */
19976#endif
19977};
19978
19979/*
19980** An instance of the ParseCleanup object specifies an operation that
19981** should be performed after parsing to deallocation resources obtained
19982** during the parse and which are no longer needed.
19983*/
19984struct ParseCleanup {
19985 ParseCleanup *pNext; /* Next cleanup task */
19986 void *pPtr; /* Pointer to object to deallocate */
19987 void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */
19988};
19989
19990/*
19991** An SQL parser context. A copy of this structure is passed through
19992** the parser and down into all the parser action routine in order to
19993** carry around information that is global to the entire parse.
19994**
19995** The structure is divided into two parts. When the parser and code
19996** generate call themselves recursively, the first part of the structure
19997** is constant but the second part is reset at the beginning and end of
19998** each recursion.
19999**
20000** The nTableLock and aTableLock variables are only used if the shared-cache
20001** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
20002** used to store the set of table-locks required by the statement being
20003** compiled. Function sqlite3TableLock() is used to add entries to the
20004** list.
20005*/
20006struct Parse {
20007 sqlite3 *db; /* The main database structure */
20008 char *zErrMsg; /* An error message */
20009 Vdbe *pVdbe; /* An engine for executing database bytecode */
20010 int rc; /* Return code from execution */
20011 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
20012 u8 checkSchema; /* Causes schema cookie check after an error */
20013 u8 nested; /* Number of nested calls to the parser/code generator */
20014 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
20015 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
20016 u8 mayAbort; /* True if statement may throw an ABORT exception */
20017 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
20018 u8 okConstFactor; /* OK to factor out constants */
20019 u8 disableLookaside; /* Number of times lookaside has been disabled */
20020 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20021 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
20022 u8 bHasWith; /* True if statement contains WITH */
20023 u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
20024#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
20025 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
20026#endif
20027#ifdef SQLITE_DEBUG
20028 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
20029#endif
20030 int nRangeReg; /* Size of the temporary register block */
20031 int iRangeReg; /* First register in temporary register block */
20032 int nErr; /* Number of errors seen */
20033 int nTab; /* Number of previously allocated VDBE cursors */
20034 int nMem; /* Number of memory cells used so far */
20035 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
20036 int iSelfTab; /* Table associated with an index on expr, or negative
20037 ** of the base register during check-constraint eval */
20038 int nLabel; /* The *negative* of the number of labels used */
20039 int nLabelAlloc; /* Number of slots in aLabel */
20040 int *aLabel; /* Space to hold the labels */
20041 ExprList *pConstExpr;/* Constant expressions */
20042 IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
20043 IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */
20044 Token constraintName;/* Name of the constraint currently being parsed */
20045 yDbMask writeMask; /* Start a write transaction on these databases */
20046 yDbMask cookieMask; /* Bitmask of schema verified databases */
20047 int regRowid; /* Register holding rowid of CREATE TABLE entry */
20048 int regRoot; /* Register holding root page number for new objects */
20049 int nMaxArg; /* Max args passed to user function by sub-program */
20050 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
20051#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
20052 u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
20053#endif
20054#ifndef SQLITE_OMIT_SHARED_CACHE
20055 int nTableLock; /* Number of locks in aTableLock */
20056 TableLock *aTableLock; /* Required table locks for shared-cache mode */
20057#endif
20058 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
20059 Parse *pToplevel; /* Parse structure for main program (or NULL) */
20060 Table *pTriggerTab; /* Table triggers are being coded for */
20061 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
20062 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
20063 union {
20064 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
20065 Returning *pReturning; /* The RETURNING clause */
20066 } u1;
20067 u32 oldmask; /* Mask of old.* columns referenced */
20068 u32 newmask; /* Mask of new.* columns referenced */
20069 LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
20070 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
20071 u8 bReturning; /* Coding a RETURNING trigger */
20072 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
20073 u8 disableTriggers; /* True to disable triggers */
20074
20075 /**************************************************************************
20076 ** Fields above must be initialized to zero. The fields that follow,
20077 ** down to the beginning of the recursive section, do not need to be
20078 ** initialized as they will be set before being used. The boundary is
20079 ** determined by offsetof(Parse,aTempReg).
20080 **************************************************************************/
20081
20082 int aTempReg[8]; /* Holding area for temporary registers */
20083 Parse *pOuterParse; /* Outer Parse object when nested */
20084 Token sNameToken; /* Token with unqualified schema object name */
20085
20086 /************************************************************************
20087 ** Above is constant between recursions. Below is reset before and after
20088 ** each recursion. The boundary between these two regions is determined
20089 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
20090 ** first field in the recursive region.
20091 ************************************************************************/
20092
20093 Token sLastToken; /* The last token parsed */
20094 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
20095 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
20096 u8 explain; /* True if the EXPLAIN flag is found on the query */
20097 u8 eParseMode; /* PARSE_MODE_XXX constant */
20098#ifndef SQLITE_OMIT_VIRTUALTABLE
20099 int nVtabLock; /* Number of virtual tables to lock */
20100#endif
20101 int nHeight; /* Expression tree height of current sub-select */
20102#ifndef SQLITE_OMIT_EXPLAIN
20103 int addrExplain; /* Address of current OP_Explain opcode */
20104#endif
20105 VList *pVList; /* Mapping between variable names and numbers */
20106 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
20107 const char *zTail; /* All SQL text past the last semicolon parsed */
20108 Table *pNewTable; /* A table being constructed by CREATE TABLE */
20109 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
20110 ** Also used to hold redundant UNIQUE constraints
20111 ** during a RENAME COLUMN */
20112 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
20113 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
20114#ifndef SQLITE_OMIT_VIRTUALTABLE
20115 Token sArg; /* Complete text of a module argument */
20116 Table **apVtabLock; /* Pointer to virtual tables needing locking */
20117#endif
20118 With *pWith; /* Current WITH clause, or NULL */
20119#ifndef SQLITE_OMIT_ALTERTABLE
20120 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
20121#endif
20122};
20123
20124/* Allowed values for Parse.eParseMode
20125*/
20126#define PARSE_MODE_NORMAL0 0
20127#define PARSE_MODE_DECLARE_VTAB1 1
20128#define PARSE_MODE_RENAME2 2
20129#define PARSE_MODE_UNMAP3 3
20130
20131/*
20132** Sizes and pointers of various parts of the Parse object.
20133*/
20134#define PARSE_HDR(X)(((char*)(X))+__builtin_offsetof(Parse, zErrMsg)) (((char*)(X))+offsetof(Parse,zErrMsg)__builtin_offsetof(Parse, zErrMsg))
20135#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*/
20136#define PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken) offsetof(Parse,sLastToken)__builtin_offsetof(Parse, sLastToken) /* Recursive part */
20137#define PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)) (sizeof(Parse)-PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Non-recursive part */
20138#define PARSE_TAIL(X)(((char*)(X))+__builtin_offsetof(Parse, sLastToken)) (((char*)(X))+PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Pointer to tail */
20139
20140/*
20141** Return true if currently inside an sqlite3_declare_vtab() call.
20142*/
20143#ifdef SQLITE_OMIT_VIRTUALTABLE
20144 #define IN_DECLARE_VTAB(pParse->eParseMode==1) 0
20145#else
20146 #define IN_DECLARE_VTAB(pParse->eParseMode==1) (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB1)
20147#endif
20148
20149#if defined(SQLITE_OMIT_ALTERTABLE)
20150 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) 0
20151#else
20152 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) (pParse->eParseMode>=PARSE_MODE_RENAME2)
20153#endif
20154
20155#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
20156 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) 0
20157#else
20158 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) (pParse->eParseMode!=PARSE_MODE_NORMAL0)
20159#endif
20160
20161/*
20162** An instance of the following structure can be declared on a stack and used
20163** to save the Parse.zAuthContext value so that it can be restored later.
20164*/
20165struct AuthContext {
20166 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
20167 Parse *pParse; /* The Parse structure */
20168};
20169
20170/*
20171** Bitfield flags for P5 value in various opcodes.
20172**
20173** Value constraints (enforced via assert()):
20174** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
20175** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
20176** OPFLAG_BULKCSR == BTREE_BULKLOAD
20177** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
20178** OPFLAG_FORDELETE == BTREE_FORDELETE
20179** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
20180** OPFLAG_AUXDELETE == BTREE_AUXDELETE
20181*/
20182#define OPFLAG_NCHANGE0x01 0x01 /* OP_Insert: Set to update db->nChange */
20183 /* Also used in P2 (not P5) of OP_Delete */
20184#define OPFLAG_NOCHNG0x01 0x01 /* OP_VColumn nochange for UPDATE */
20185#define OPFLAG_EPHEM0x01 0x01 /* OP_Column: Ephemeral output is ok */
20186#define OPFLAG_LASTROWID0x20 0x20 /* Set to update db->lastRowid */
20187#define OPFLAG_ISUPDATE0x04 0x04 /* This OP_Insert is an sql UPDATE */
20188#define OPFLAG_APPEND0x08 0x08 /* This is likely to be an append */
20189#define OPFLAG_USESEEKRESULT0x10 0x10 /* Try to avoid a seek in BtreeInsert() */
20190#define OPFLAG_ISNOOP0x40 0x40 /* OP_Delete does pre-update-hook only */
20191#define OPFLAG_LENGTHARG0x40 0x40 /* OP_Column only used for length() */
20192#define OPFLAG_TYPEOFARG0x80 0x80 /* OP_Column only used for typeof() */
20193#define OPFLAG_BYTELENARG0xc0 0xc0 /* OP_Column only for octet_length() */
20194#define OPFLAG_BULKCSR0x01 0x01 /* OP_Open** used to open bulk cursor */
20195#define OPFLAG_SEEKEQ0x02 0x02 /* OP_Open** cursor uses EQ seek only */
20196#define OPFLAG_FORDELETE0x08 0x08 /* OP_Open should use BTREE_FORDELETE */
20197#define OPFLAG_P2ISREG0x10 0x10 /* P2 to OP_Open** is a register number */
20198#define OPFLAG_PERMUTE0x01 0x01 /* OP_Compare: use the permutation */
20199#define OPFLAG_SAVEPOSITION0x02 0x02 /* OP_Delete/Insert: save cursor pos */
20200#define OPFLAG_AUXDELETE0x04 0x04 /* OP_Delete: index in a DELETE op */
20201#define OPFLAG_NOCHNG_MAGIC0x6d 0x6d /* OP_MakeRecord: serialtype 10 is ok */
20202#define OPFLAG_PREFORMAT0x80 0x80 /* OP_Insert uses preformatted cell */
20203
20204/*
20205** Each trigger present in the database schema is stored as an instance of
20206** struct Trigger.
20207**
20208** Pointers to instances of struct Trigger are stored in two ways.
20209** 1. In the "trigHash" hash table (part of the sqlite3* that represents the
20210** database). This allows Trigger structures to be retrieved by name.
20211** 2. All triggers associated with a single table form a linked list, using the
20212** pNext member of struct Trigger. A pointer to the first element of the
20213** linked list is stored as the "pTrigger" member of the associated
20214** struct Table.
20215**
20216** The "step_list" member points to the first element of a linked list
20217** containing the SQL statements specified as the trigger program.
20218*/
20219struct Trigger {
20220 char *zName; /* The name of the trigger */
20221 char *table; /* The table or view to which the trigger applies */
20222 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
20223 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
20224 u8 bReturning; /* This trigger implements a RETURNING clause */
20225 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
20226 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
20227 the <column-list> is stored here */
20228 Schema *pSchema; /* Schema containing the trigger */
20229 Schema *pTabSchema; /* Schema containing the table */
20230 TriggerStep *step_list; /* Link list of trigger program steps */
20231 Trigger *pNext; /* Next trigger associated with the table */
20232};
20233
20234/*
20235** A trigger is either a BEFORE or an AFTER trigger. The following constants
20236** determine which.
20237**
20238** If there are multiple triggers, you might of some BEFORE and some AFTER.
20239** In that cases, the constants below can be ORed together.
20240*/
20241#define TRIGGER_BEFORE1 1
20242#define TRIGGER_AFTER2 2
20243
20244/*
20245** An instance of struct TriggerStep is used to store a single SQL statement
20246** that is a part of a trigger-program.
20247**
20248** Instances of struct TriggerStep are stored in a singly linked list (linked
20249** using the "pNext" member) referenced by the "step_list" member of the
20250** associated struct Trigger instance. The first element of the linked list is
20251** the first step of the trigger-program.
20252**
20253** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
20254** "SELECT" statement. The meanings of the other members is determined by the
20255** value of "op" as follows:
20256**
20257** (op == TK_INSERT)
20258** orconf -> stores the ON CONFLICT algorithm
20259** pSelect -> The content to be inserted - either a SELECT statement or
20260** a VALUES clause.
20261** zTarget -> Dequoted name of the table to insert into.
20262** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
20263** statement, then this stores the column-names to be
20264** inserted into.
20265** pUpsert -> The ON CONFLICT clauses for an Upsert
20266**
20267** (op == TK_DELETE)
20268** zTarget -> Dequoted name of the table to delete from.
20269** pWhere -> The WHERE clause of the DELETE statement if one is specified.
20270** Otherwise NULL.
20271**
20272** (op == TK_UPDATE)
20273** zTarget -> Dequoted name of the table to update.
20274** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
20275** Otherwise NULL.
20276** pExprList -> A list of the columns to update and the expressions to update
20277** them to. See sqlite3Update() documentation of "pChanges"
20278** argument.
20279**
20280** (op == TK_SELECT)
20281** pSelect -> The SELECT statement
20282**
20283** (op == TK_RETURNING)
20284** pExprList -> The list of expressions that follow the RETURNING keyword.
20285**
20286*/
20287struct TriggerStep {
20288 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT,
20289 ** or TK_RETURNING */
20290 u8 orconf; /* OE_Rollback etc. */
20291 Trigger *pTrig; /* The trigger that this step is a part of */
20292 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
20293 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
20294 SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */
20295 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
20296 ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */
20297 IdList *pIdList; /* Column names for INSERT */
20298 Upsert *pUpsert; /* Upsert clauses on an INSERT */
20299 char *zSpan; /* Original SQL text of this command */
20300 TriggerStep *pNext; /* Next in the link-list */
20301 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
20302};
20303
20304/*
20305** Information about a RETURNING clause
20306*/
20307struct Returning {
20308 Parse *pParse; /* The parse that includes the RETURNING clause */
20309 ExprList *pReturnEL; /* List of expressions to return */
20310 Trigger retTrig; /* The transient trigger that implements RETURNING */
20311 TriggerStep retTStep; /* The trigger step */
20312 int iRetCur; /* Transient table holding RETURNING results */
20313 int nRetCol; /* Number of in pReturnEL after expansion */
20314 int iRetReg; /* Register array for holding a row of RETURNING */
20315 char zName[40]; /* Name of trigger: "sqlite_returning_%p" */
20316};
20317
20318/*
20319** An object used to accumulate the text of a string where we
20320** do not necessarily know how big the string will be in the end.
20321*/
20322struct sqlite3_str {
20323 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
20324 char *zText; /* The string collected so far */
20325 u32 nAlloc; /* Amount of space allocated in zText */
20326 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
20327 u32 nChar; /* Length of the string so far */
20328 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
20329 u8 printfFlags; /* SQLITE_PRINTF flags below */
20330};
20331#define SQLITE_PRINTF_INTERNAL0x01 0x01 /* Internal-use-only converters allowed */
20332#define SQLITE_PRINTF_SQLFUNC0x02 0x02 /* SQL function arguments to VXPrintf */
20333#define SQLITE_PRINTF_MALLOCED0x04 0x04 /* True if zText is allocated space */
20334
20335#define isMalloced(X)(((X)->printfFlags & 0x04)!=0) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED0x04)!=0)
20336
20337/*
20338** The following object is the header for an "RCStr" or "reference-counted
20339** string". An RCStr is passed around and used like any other char*
20340** that has been dynamically allocated. The important interface
20341** differences:
20342**
20343** 1. RCStr strings are reference counted. They are deallocated
20344** when the reference count reaches zero.
20345**
20346** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than
20347** sqlite3_free()
20348**
20349** 3. Make a (read-only) copy of a read-only RCStr string using
20350** sqlite3RCStrRef().
20351**
20352** "String" is in the name, but an RCStr object can also be used to hold
20353** binary data.
20354*/
20355struct RCStr {
20356 u64 nRCRef; /* Number of references */
20357 /* Total structure size should be a multiple of 8 bytes for alignment */
20358};
20359
20360/*
20361** A pointer to this structure is used to communicate information
20362** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
20363*/
20364typedef struct {
20365 sqlite3 *db; /* The database being initialized */
20366 char **pzErrMsg; /* Error message stored here */
20367 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
20368 int rc; /* Result code stored here */
20369 u32 mInitFlags; /* Flags controlling error messages */
20370 u32 nInitRow; /* Number of rows processed */
20371 Pgno mxPage; /* Maximum page number. 0 for no limit. */
20372} InitData;
20373
20374/*
20375** Allowed values for mInitFlags
20376*/
20377#define INITFLAG_AlterMask0x0003 0x0003 /* Types of ALTER */
20378#define INITFLAG_AlterRename0x0001 0x0001 /* Reparse after a RENAME */
20379#define INITFLAG_AlterDrop0x0002 0x0002 /* Reparse after a DROP COLUMN */
20380#define INITFLAG_AlterAdd0x0003 0x0003 /* Reparse after an ADD COLUMN */
20381
20382/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
20383** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20384** parameters are for temporary use during development, to help find
20385** optimal values for parameters in the query planner. The should not
20386** be used on trunk check-ins. They are a temporary mechanism available
20387** for transient development builds only.
20388**
20389** Tuning parameters are numbered starting with 1.
20390*/
20391#define SQLITE_NTUNE6 6 /* Should be zero for all trunk check-ins */
20392#ifdef SQLITE_DEBUG
20393# define Tuning(X)0 (sqlite3Config.aTune[(X)-1])
20394#else
20395# define Tuning(X)0 0
20396#endif
20397
20398/*
20399** Structure containing global configuration data for the SQLite library.
20400**
20401** This structure also contains some state information.
20402*/
20403struct Sqlite3Config {
20404 int bMemstat; /* True to enable memory status */
20405 u8 bCoreMutex; /* True to enable core mutexing */
20406 u8 bFullMutex; /* True to enable full mutexing */
20407 u8 bOpenUri; /* True to interpret filenames as URIs */
20408 u8 bUseCis; /* Use covering indices for full-scans */
20409 u8 bSmallMalloc; /* Avoid large memory allocations if true */
20410 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
20411#ifdef SQLITE_DEBUG
20412 u8 bJsonSelfcheck; /* Double-check JSON parsing */
20413#endif
20414 int mxStrlen; /* Maximum string length */
20415 int neverCorrupt; /* Database is always well-formed */
20416 int szLookaside; /* Default lookaside buffer size */
20417 int nLookaside; /* Default lookaside buffer count */
20418 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
20419 sqlite3_mem_methods m; /* Low-level memory allocation interface */
20420 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
20421 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
20422 void *pHeap; /* Heap storage space */
20423 int nHeap; /* Size of pHeap[] */
20424 int mnReq, mxReq; /* Min and max heap requests sizes */
20425 sqlite3_int64 szMmap; /* mmap() space per open file */
20426 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
20427 void *pPage; /* Page cache memory */
20428 int szPage; /* Size of each page in pPage[] */
20429 int nPage; /* Number of pages in pPage[] */
20430 int mxParserStack; /* maximum depth of the parser stack */
20431 int sharedCacheEnabled; /* true if shared-cache mode enabled */
20432 u32 szPma; /* Maximum Sorter PMA size */
20433 /* The above might be initialized to non-zero. The following need to always
20434 ** initially be zero, however. */
20435 int isInit; /* True after initialization has finished */
20436 int inProgress; /* True while initialization in progress */
20437 int isMutexInit; /* True after mutexes are initialized */
20438 int isMallocInit; /* True after malloc is initialized */
20439 int isPCacheInit; /* True after malloc is initialized */
20440 int nRefInitMutex; /* Number of users of pInitMutex */
20441 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
20442 void (*xLog)(void*,int,const char*); /* Function for logging */
20443 void *pLogArg; /* First argument to xLog() */
20444#ifdef SQLITE_ENABLE_SQLLOG
20445 void(*xSqllog)(void*,sqlite3*,const char*, int);
20446 void *pSqllogArg;
20447#endif
20448#ifdef SQLITE_VDBE_COVERAGE
20449 /* The following callback (if not NULL) is invoked on every VDBE branch
20450 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
20451 */
20452 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
20453 void *pVdbeBranchArg; /* 1st argument */
20454#endif
20455#ifndef SQLITE_OMIT_DESERIALIZE
20456 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
20457#endif
20458#ifndef SQLITE_UNTESTABLE
20459 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
20460#endif
20461#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
20462 u32 mNoVisibleRowid; /* TF_NoVisibleRowid if the ROWID_IN_VIEW
20463 ** feature is disabled. 0 if rowids can
20464 ** occur in views. */
20465#endif
20466 int bLocaltimeFault; /* True to fail localtime() calls */
20467 int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
20468 int iOnceResetThreshold; /* When to reset OP_Once counters */
20469 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
20470 unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */
20471 /* vvvv--- must be last ---vvv */
20472#ifdef SQLITE_DEBUG
20473 sqlite3_int64 aTune[SQLITE_NTUNE6]; /* Tuning parameters */
20474#endif
20475};
20476
20477/*
20478** This macro is used inside of assert() statements to indicate that
20479** the assert is only valid on a well-formed database. Instead of:
20480**
20481** assert( X );
20482**
20483** One writes:
20484**
20485** assert( X || CORRUPT_DB );
20486**
20487** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
20488** that the database is definitely corrupt, only that it might be corrupt.
20489** For most test cases, CORRUPT_DB is set to false using a special
20490** sqlite3_test_control(). This enables assert() statements to prove
20491** things that are always true for well-formed databases.
20492*/
20493#define CORRUPT_DB(sqlite3Config.neverCorrupt==0) (sqlite3Config.neverCorrupt==0)
20494
20495/*
20496** Context pointer passed down through the tree-walk.
20497*/
20498struct Walker {
20499 Parse *pParse; /* Parser context. */
20500 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
20501 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
20502 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
20503 int walkerDepth; /* Number of subqueries */
20504 u16 eCode; /* A small processing code */
20505 u16 mWFlags; /* Use-dependent flags */
20506 union { /* Extra data for callback */
20507 NameContext *pNC; /* Naming context */
20508 int n; /* A counter */
20509 int iCur; /* A cursor number */
20510 SrcList *pSrcList; /* FROM clause */
20511 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
20512 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
20513 int *aiCol; /* array of column indexes */
20514 struct IdxCover *pIdxCover; /* Check for index coverage */
20515 ExprList *pGroupBy; /* GROUP BY clause */
20516 Select *pSelect; /* HAVING to WHERE clause ctx */
20517 struct WindowRewrite *pRewrite; /* Window rewrite context */
20518 struct WhereConst *pConst; /* WHERE clause constants */
20519 struct RenameCtx *pRename; /* RENAME COLUMN context */
20520 struct Table *pTab; /* Table of generated column */
20521 struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
20522 SrcItem *pSrcItem; /* A single FROM clause item */
20523 DbFixer *pFix; /* See sqlite3FixSelect() */
20524 Mem *aMem; /* See sqlite3BtreeCursorHint() */
20525 } u;
20526};
20527
20528/*
20529** The following structure contains information used by the sqliteFix...
20530** routines as they walk the parse tree to make database references
20531** explicit.
20532*/
20533struct DbFixer {
20534 Parse *pParse; /* The parsing context. Error messages written here */
20535 Walker w; /* Walker object */
20536 Schema *pSchema; /* Fix items to this schema */
20537 u8 bTemp; /* True for TEMP schema entries */
20538 const char *zDb; /* Make sure all objects are contained in this database */
20539 const char *zType; /* Type of the container - used for error messages */
20540 const Token *pName; /* Name of the container - used for error messages */
20541};
20542
20543/* Forward declarations */
20544SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker*, Expr*);
20545SQLITE_PRIVATEstatic int sqlite3WalkExprNN(Walker*, Expr*);
20546SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker*, ExprList*);
20547SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker*, Select*);
20548SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker*, Select*);
20549SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker*, Select*);
20550SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker*, Expr*);
20551SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker*, Select*);
20552SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker*, Select*);
20553SQLITE_PRIVATEstatic int sqlite3WalkerDepthIncrease(Walker*,Select*);
20554SQLITE_PRIVATEstatic void sqlite3WalkerDepthDecrease(Walker*,Select*);
20555SQLITE_PRIVATEstatic void sqlite3WalkWinDefnDummyCallback(Walker*,Select*);
20556
20557#ifdef SQLITE_DEBUG
20558SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker*, Select*);
20559#endif
20560
20561#ifndef SQLITE_OMIT_CTE
20562SQLITE_PRIVATEstatic void sqlite3SelectPopWith(Walker*, Select*);
20563#else
20564# define sqlite3SelectPopWith 0
20565#endif
20566
20567/*
20568** Return code from the parse-tree walking primitives and their
20569** callbacks.
20570*/
20571#define WRC_Continue0 0 /* Continue down into children */
20572#define WRC_Prune1 1 /* Omit children but continue walking siblings */
20573#define WRC_Abort2 2 /* Abandon the tree walk */
20574
20575/*
20576** A single common table expression
20577*/
20578struct Cte {
20579 char *zName; /* Name of this CTE */
20580 ExprList *pCols; /* List of explicit column names, or NULL */
20581 Select *pSelect; /* The definition of this CTE */
20582 const char *zCteErr; /* Error message for circular references */
20583 CteUse *pUse; /* Usage information for this CTE */
20584 u8 eM10d; /* The MATERIALIZED flag */
20585};
20586
20587/*
20588** Allowed values for the materialized flag (eM10d):
20589*/
20590#define M10d_Yes0 0 /* AS MATERIALIZED */
20591#define M10d_Any1 1 /* Not specified. Query planner's choice */
20592#define M10d_No2 2 /* AS NOT MATERIALIZED */
20593
20594/*
20595** An instance of the With object represents a WITH clause containing
20596** one or more CTEs (common table expressions).
20597*/
20598struct With {
20599 int nCte; /* Number of CTEs in the WITH clause */
20600 int bView; /* Belongs to the outermost Select of a view */
20601 With *pOuter; /* Containing WITH clause, or NULL */
20602 Cte a[1]; /* For each CTE in the WITH clause.... */
20603};
20604
20605/*
20606** The Cte object is not guaranteed to persist for the entire duration
20607** of code generation. (The query flattener or other parser tree
20608** edits might delete it.) The following object records information
20609** about each Common Table Expression that must be preserved for the
20610** duration of the parse.
20611**
20612** The CteUse objects are freed using sqlite3ParserAddCleanup() rather
20613** than sqlite3SelectDelete(), which is what enables them to persist
20614** until the end of code generation.
20615*/
20616struct CteUse {
20617 int nUse; /* Number of users of this CTE */
20618 int addrM9e; /* Start of subroutine to compute materialization */
20619 int regRtn; /* Return address register for addrM9e subroutine */
20620 int iCur; /* Ephemeral table holding the materialization */
20621 LogEst nRowEst; /* Estimated number of rows in the table */
20622 u8 eM10d; /* The MATERIALIZED flag */
20623};
20624
20625
20626/* Client data associated with sqlite3_set_clientdata() and
20627** sqlite3_get_clientdata().
20628*/
20629struct DbClientData {
20630 DbClientData *pNext; /* Next in a linked list */
20631 void *pData; /* The data */
20632 void (*xDestructor)(void*); /* Destructor. Might be NULL */
20633 char zName[1]; /* Name of this client data. MUST BE LAST */
20634};
20635
20636#ifdef SQLITE_DEBUG
20637/*
20638** An instance of the TreeView object is used for printing the content of
20639** data structures on sqlite3DebugPrintf() using a tree-like view.
20640*/
20641struct TreeView {
20642 int iLevel; /* Which level of the tree we are on */
20643 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
20644};
20645#endif /* SQLITE_DEBUG */
20646
20647/*
20648** This object is used in various ways, most (but not all) related to window
20649** functions.
20650**
20651** (1) A single instance of this structure is attached to the
20652** the Expr.y.pWin field for each window function in an expression tree.
20653** This object holds the information contained in the OVER clause,
20654** plus additional fields used during code generation.
20655**
20656** (2) All window functions in a single SELECT form a linked-list
20657** attached to Select.pWin. The Window.pFunc and Window.pExpr
20658** fields point back to the expression that is the window function.
20659**
20660** (3) The terms of the WINDOW clause of a SELECT are instances of this
20661** object on a linked list attached to Select.pWinDefn.
20662**
20663** (4) For an aggregate function with a FILTER clause, an instance
20664** of this object is stored in Expr.y.pWin with eFrmType set to
20665** TK_FILTER. In this case the only field used is Window.pFilter.
20666**
20667** The uses (1) and (2) are really the same Window object that just happens
20668** to be accessible in two different ways. Use case (3) are separate objects.
20669*/
20670struct Window {
20671 char *zName; /* Name of window (may be NULL) */
20672 char *zBase; /* Name of base window for chaining (may be NULL) */
20673 ExprList *pPartition; /* PARTITION BY clause */
20674 ExprList *pOrderBy; /* ORDER BY clause */
20675 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
20676 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
20677 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
20678 u8 bImplicitFrame; /* True if frame was implicitly specified */
20679 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
20680 Expr *pStart; /* Expression for "<expr> PRECEDING" */
20681 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
20682 Window **ppThis; /* Pointer to this object in Select.pWin list */
20683 Window *pNextWin; /* Next window function belonging to this SELECT */
20684 Expr *pFilter; /* The FILTER expression */
20685 FuncDef *pWFunc; /* The function */
20686 int iEphCsr; /* Partition buffer or Peer buffer */
20687 int regAccum; /* Accumulator */
20688 int regResult; /* Interim result */
20689 int csrApp; /* Function cursor (used by min/max) */
20690 int regApp; /* Function register (also used by min/max) */
20691 int regPart; /* Array of registers for PARTITION BY values */
20692 Expr *pOwner; /* Expression object this window is attached to */
20693 int nBufferCol; /* Number of columns in buffer table */
20694 int iArgCol; /* Offset of first argument for this function */
20695 int regOne; /* Register containing constant value 1 */
20696 int regStartRowid;
20697 int regEndRowid;
20698 u8 bExprArgs; /* Defer evaluation of window function arguments
20699 ** due to the SQLITE_SUBTYPE flag */
20700};
20701
20702SQLITE_PRIVATEstatic Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow);
20703SQLITE_PRIVATEstatic void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal);
20704
20705#ifndef SQLITE_OMIT_WINDOWFUNC
20706SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3*, Window*);
20707SQLITE_PRIVATEstatic void sqlite3WindowUnlinkFromSelect(Window*);
20708SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p);
20709SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
20710SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse*, Expr*, Window*);
20711SQLITE_PRIVATEstatic void sqlite3WindowLink(Select *pSel, Window *pWin);
20712SQLITE_PRIVATEstatic int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int);
20713SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse*, Select*);
20714SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
20715SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse*, Select*);
20716SQLITE_PRIVATEstatic void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
20717SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
20718SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
20719SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void);
20720SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse*, Window*, Window*);
20721SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
20722#else
20723# define sqlite3WindowDelete(a,b)
20724# define sqlite3WindowFunctions()
20725# define sqlite3WindowAttach(a,b,c)
20726#endif
20727
20728/*
20729** Assuming zIn points to the first byte of a UTF-8 character,
20730** advance zIn to point to the first byte of the next UTF-8 character.
20731*/
20732#define SQLITE_SKIP_UTF8(zIn){ if( (*(zIn++))>=0xc0 ){ while( (*zIn & 0xc0)==0x80 )
{ zIn++; } } }
{ \
20733 if( (*(zIn++))>=0xc0 ){ \
20734 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
20735 } \
20736}
20737
20738/*
20739** The SQLITE_*_BKPT macros are substitutes for the error codes with
20740** the same name but without the _BKPT suffix. These macros invoke
20741** routines that report the line-number on which the error originated
20742** using sqlite3_log(). The routines also provide a convenient place
20743** to set a debugger breakpoint.
20744*/
20745SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType);
20746SQLITE_PRIVATEstatic int sqlite3CorruptError(int);
20747SQLITE_PRIVATEstatic int sqlite3MisuseError(int);
20748SQLITE_PRIVATEstatic int sqlite3CantopenError(int);
20749#define SQLITE_CORRUPT_BKPTsqlite3CorruptError(20749) sqlite3CorruptError(__LINE__20749)
20750#define SQLITE_MISUSE_BKPTsqlite3MisuseError(20750) sqlite3MisuseError(__LINE__20750)
20751#define SQLITE_CANTOPEN_BKPTsqlite3CantopenError(20751) sqlite3CantopenError(__LINE__20751)
20752#ifdef SQLITE_DEBUG
20753SQLITE_PRIVATEstatic int sqlite3NomemError(int);
20754SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int);
20755# define SQLITE_NOMEM_BKPT7 sqlite3NomemError(__LINE__20755)
20756# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) sqlite3IoerrnomemError(__LINE__20756)
20757#else
20758# define SQLITE_NOMEM_BKPT7 SQLITE_NOMEM7
20759# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) SQLITE_IOERR_NOMEM(10 | (12<<8))
20760#endif
20761#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
20762SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int,Pgno);
20763# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(20763) sqlite3CorruptPgnoError(__LINE__20763,(P))
20764#else
20765# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(20765) sqlite3CorruptError(__LINE__20765)
20766#endif
20767
20768/*
20769** FTS3 and FTS4 both require virtual table support
20770*/
20771#if defined(SQLITE_OMIT_VIRTUALTABLE)
20772# undef SQLITE_ENABLE_FTS31
20773# undef SQLITE_ENABLE_FTS41
20774#endif
20775
20776/*
20777** FTS4 is really an extension for FTS3. It is enabled using the
20778** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
20779** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
20780*/
20781#if defined(SQLITE_ENABLE_FTS41) && !defined(SQLITE_ENABLE_FTS31)
20782# define SQLITE_ENABLE_FTS31 1
20783#endif
20784
20785/*
20786** The following macros mimic the standard library functions toupper(),
20787** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
20788** sqlite versions only work for ASCII characters, regardless of locale.
20789*/
20790#ifdef SQLITE_ASCII1
20791# define sqlite3Toupper(x)((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
20792# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
20793# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
20794# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
20795# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
20796# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
20797# define sqlite3Tolower(x)(sqlite3UpperToLower[(unsigned char)(x)]) (sqlite3UpperToLower[(unsigned char)(x)])
20798# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
20799# define sqlite3JsonId1(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x42) (sqlite3CtypeMap[(unsigned char)(x)]&0x42)
20800# define sqlite3JsonId2(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x46) (sqlite3CtypeMap[(unsigned char)(x)]&0x46)
20801#else
20802# 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; }))
20803# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) isspace((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISspace)
20804# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) isalnum((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalnum)
20805# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) isalpha((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalpha)
20806# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) isdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISdigit)
20807# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) isxdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISxdigit)
20808# 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; }))
20809# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
20810# define sqlite3JsonId1(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x42) (sqlite3IsIdChar(x)&&(x)<'0')
20811# define sqlite3JsonId2(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x46) sqlite3IsIdChar(x)
20812#endif
20813SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8);
20814
20815/*
20816** Internal function prototypes
20817*/
20818SQLITE_PRIVATEstatic int sqlite3StrICmp(const char*,const char*);
20819SQLITE_PRIVATEstatic int sqlite3Strlen30(const char*);
20820#define sqlite3Strlen30NN(C)(strlen(C)&0x3fffffff) (strlen(C)&0x3fffffff)
20821SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column*,char*);
20822#define sqlite3StrNICmpsqlite3_strnicmp sqlite3_strnicmp
20823
20824SQLITE_PRIVATEstatic int sqlite3MallocInit(void);
20825SQLITE_PRIVATEstatic void sqlite3MallocEnd(void);
20826SQLITE_PRIVATEstatic void *sqlite3Malloc(u64);
20827SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64);
20828SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3*, u64);
20829SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3*, u64);
20830SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3*, u64);
20831SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3*,const char*);
20832SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
20833SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
20834SQLITE_PRIVATEstatic void *sqlite3Realloc(void*, u64);
20835SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
20836SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *, void *, u64);
20837SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3*, void*);
20838SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3*, void*);
20839SQLITE_PRIVATEstatic void sqlite3DbNNFreeNN(sqlite3*, void*);
20840SQLITE_PRIVATEstatic int sqlite3MallocSize(const void*);
20841SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3*, const void*);
20842SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int);
20843SQLITE_PRIVATEstatic void sqlite3PageFree(void*);
20844SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void);
20845#ifndef SQLITE_UNTESTABLE
20846SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
20847#endif
20848SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void);
20849
20850/*
20851** On systems with ample stack space and that support alloca(), make
20852** use of alloca() to obtain space for large automatic objects. By default,
20853** obtain space from malloc().
20854**
20855** The alloca() routine never returns NULL. This will cause code paths
20856** that deal with sqlite3StackAlloc() failures to be unreachable.
20857*/
20858#ifdef SQLITE_USE_ALLOCA
20859# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) alloca(N)__builtin_alloca (N)
20860# define sqlite3StackAllocRawNN(D,N)sqlite3DbMallocRawNN(D,N) alloca(N)__builtin_alloca (N)
20861# define sqlite3StackFree(D,P)sqlite3DbFree(D,P)
20862# define sqlite3StackFreeNN(D,P)sqlite3DbFreeNN(D,P)
20863#else
20864# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) sqlite3DbMallocRaw(D,N)
20865# define sqlite3StackAllocRawNN(D,N)sqlite3DbMallocRawNN(D,N) sqlite3DbMallocRawNN(D,N)
20866# define sqlite3StackFree(D,P)sqlite3DbFree(D,P) sqlite3DbFree(D,P)
20867# define sqlite3StackFreeNN(D,P)sqlite3DbFreeNN(D,P) sqlite3DbFreeNN(D,P)
20868#endif
20869
20870/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
20871** are, disable MEMSYS3
20872*/
20873#ifdef SQLITE_ENABLE_MEMSYS5
20874SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
20875#undef SQLITE_ENABLE_MEMSYS3
20876#endif
20877#ifdef SQLITE_ENABLE_MEMSYS3
20878SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
20879#endif
20880
20881
20882#ifndef SQLITE_MUTEX_OMIT
20883SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
20884SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void);
20885SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int);
20886SQLITE_PRIVATEstatic int sqlite3MutexInit(void);
20887SQLITE_PRIVATEstatic int sqlite3MutexEnd(void);
20888#endif
20889#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
20890SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void);
20891#else
20892# define sqlite3MemoryBarrier()
20893#endif
20894
20895SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int);
20896SQLITE_PRIVATEstatic void sqlite3StatusUp(int, int);
20897SQLITE_PRIVATEstatic void sqlite3StatusDown(int, int);
20898SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int, int);
20899SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3*,int*);
20900
20901/* Access to mutexes used by sqlite3_status() */
20902SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void);
20903SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void);
20904
20905#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
20906SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex*);
20907#else
20908# define sqlite3MutexWarnOnContention(x)
20909#endif
20910
20911#ifndef SQLITE_OMIT_FLOATING_POINT
20912# define EXP754(((u64)0x7ff)<<52) (((u64)0x7ff)<<52)
20913# define MAN754((((u64)1)<<52)-1) ((((u64)1)<<52)-1)
20914# 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)
20915# define IsOvfl(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
)
(((X)&EXP754(((u64)0x7ff)<<52))==EXP754(((u64)0x7ff)<<52))
20916SQLITE_PRIVATEstatic int sqlite3IsNaN(double);
20917SQLITE_PRIVATEstatic int sqlite3IsOverflow(double);
20918#else
20919# define IsNaN(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((X)&((((u64)1)<<52)-1))!=0)
0
20920# define sqlite3IsNaN(X) 0
20921# define sqlite3IsOVerflow(X) 0
20922#endif
20923
20924/*
20925** An instance of the following structure holds information about SQL
20926** functions arguments that are the parameters to the printf() function.
20927*/
20928struct PrintfArguments {
20929 int nArg; /* Total number of arguments */
20930 int nUsed; /* Number of arguments used so far */
20931 sqlite3_value **apArg; /* The argument values */
20932};
20933
20934/*
20935** An instance of this object receives the decoding of a floating point
20936** value into an approximate decimal representation.
20937*/
20938struct FpDecode {
20939 char sign; /* '+' or '-' */
20940 char isSpecial; /* 1: Infinity 2: NaN */
20941 int n; /* Significant digits in the decode */
20942 int iDP; /* Location of the decimal point */
20943 char *z; /* Start of significant digits */
20944 char zBuf[24]; /* Storage for significant digits */
20945};
20946
20947SQLITE_PRIVATEstatic void sqlite3FpDecode(FpDecode*,double,int,int);
20948SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3*,const char*, ...);
20949SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
20950#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
20951SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char*, ...);
20952#endif
20953#if defined(SQLITE_TEST)
20954SQLITE_PRIVATEstatic void *sqlite3TestTextToPtr(const char*);
20955#endif
20956
20957#if defined(SQLITE_DEBUG)
20958SQLITE_PRIVATEstatic void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...);
20959SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
20960SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
20961SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
20962SQLITE_PRIVATEstatic void sqlite3TreeViewBareIdList(TreeView*, const IdList*, const char*);
20963SQLITE_PRIVATEstatic void sqlite3TreeViewIdList(TreeView*, const IdList*, u8, const char*);
20964SQLITE_PRIVATEstatic void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8);
20965SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
20966SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
20967SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView*, const With*, u8);
20968SQLITE_PRIVATEstatic void sqlite3TreeViewUpsert(TreeView*, const Upsert*, u8);
20969#if TREETRACE_ENABLED0
20970SQLITE_PRIVATEstatic void sqlite3TreeViewDelete(const With*, const SrcList*, const Expr*,
20971 const ExprList*,const Expr*, const Trigger*);
20972SQLITE_PRIVATEstatic void sqlite3TreeViewInsert(const With*, const SrcList*,
20973 const IdList*, const Select*, const ExprList*,
20974 int, const Upsert*, const Trigger*);
20975SQLITE_PRIVATEstatic void sqlite3TreeViewUpdate(const With*, const SrcList*, const ExprList*,
20976 const Expr*, int, const ExprList*, const Expr*,
20977 const Upsert*, const Trigger*);
20978#endif
20979#ifndef SQLITE_OMIT_TRIGGER
20980SQLITE_PRIVATEstatic void sqlite3TreeViewTriggerStep(TreeView*, const TriggerStep*, u8, u8);
20981SQLITE_PRIVATEstatic void sqlite3TreeViewTrigger(TreeView*, const Trigger*, u8, u8);
20982#endif
20983#ifndef SQLITE_OMIT_WINDOWFUNC
20984SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
20985SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
20986#endif
20987SQLITE_PRIVATEstatic void sqlite3ShowExpr(const Expr*);
20988SQLITE_PRIVATEstatic void sqlite3ShowExprList(const ExprList*);
20989SQLITE_PRIVATEstatic void sqlite3ShowIdList(const IdList*);
20990SQLITE_PRIVATEstatic void sqlite3ShowSrcList(const SrcList*);
20991SQLITE_PRIVATEstatic void sqlite3ShowSelect(const Select*);
20992SQLITE_PRIVATEstatic void sqlite3ShowWith(const With*);
20993SQLITE_PRIVATEstatic void sqlite3ShowUpsert(const Upsert*);
20994#ifndef SQLITE_OMIT_TRIGGER
20995SQLITE_PRIVATEstatic void sqlite3ShowTriggerStep(const TriggerStep*);
20996SQLITE_PRIVATEstatic void sqlite3ShowTriggerStepList(const TriggerStep*);
20997SQLITE_PRIVATEstatic void sqlite3ShowTrigger(const Trigger*);
20998SQLITE_PRIVATEstatic void sqlite3ShowTriggerList(const Trigger*);
20999#endif
21000#ifndef SQLITE_OMIT_WINDOWFUNC
21001SQLITE_PRIVATEstatic void sqlite3ShowWindow(const Window*);
21002SQLITE_PRIVATEstatic void sqlite3ShowWinFunc(const Window*);
21003#endif
21004#endif
21005
21006SQLITE_PRIVATEstatic void sqlite3SetString(char **, sqlite3*, const char*);
21007SQLITE_PRIVATEstatic void sqlite3ProgressCheck(Parse*);
21008SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse*, const char*, ...);
21009SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3*,int);
21010SQLITE_PRIVATEstatic void sqlite3Dequote(char*);
21011SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr*);
21012SQLITE_PRIVATEstatic void sqlite3DequoteToken(Token*);
21013SQLITE_PRIVATEstatic void sqlite3DequoteNumber(Parse*, Expr*);
21014SQLITE_PRIVATEstatic void sqlite3TokenInit(Token*,char*);
21015SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char*, int);
21016SQLITE_PRIVATEstatic int sqlite3RunParser(Parse*, const char*);
21017SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse*);
21018SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse*);
21019SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse*,int);
21020SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse*,int);
21021SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse*,int,int);
21022SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse*);
21023SQLITE_PRIVATEstatic void sqlite3TouchRegister(Parse*,int);
21024#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
21025SQLITE_PRIVATEstatic int sqlite3FirstAvailableRegister(Parse*,int);
21026#endif
21027#ifdef SQLITE_DEBUG
21028SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse*,int,int);
21029#endif
21030SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
21031SQLITE_PRIVATEstatic Expr *sqlite3Expr(sqlite3*,int,const char*);
21032SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
21033SQLITE_PRIVATEstatic Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
21034SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
21035SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
21036SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr*);
21037SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
21038SQLITE_PRIVATEstatic void sqlite3ExprAddFunctionOrderBy(Parse*,Expr*,ExprList*);
21039SQLITE_PRIVATEstatic void sqlite3ExprOrderByAggregateError(Parse*,Expr*);
21040SQLITE_PRIVATEstatic void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
21041SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
21042SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3*, Expr*);
21043SQLITE_PRIVATEstatic void sqlite3ExprDeleteGeneric(sqlite3*,void*);
21044SQLITE_PRIVATEstatic int sqlite3ExprDeferredDelete(Parse*, Expr*);
21045SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
21046SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
21047SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
21048SQLITE_PRIVATEstatic Select *sqlite3ExprListToValues(Parse*, int, ExprList*);
21049SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList*,int,int);
21050SQLITE_PRIVATEstatic void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
21051SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
21052SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3*, ExprList*);
21053SQLITE_PRIVATEstatic void sqlite3ExprListDeleteGeneric(sqlite3*,void*);
21054SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList*);
21055SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index*);
21056SQLITE_PRIVATEstatic int sqlite3Init(sqlite3*, char**);
21057SQLITE_PRIVATEstatic int sqlite3InitCallback(void*, int, char**, char**);
21058SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3*, int, char**, u32);
21059SQLITE_PRIVATEstatic void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
21060#ifndef SQLITE_OMIT_VIRTUALTABLE
21061SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
21062#endif
21063SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3*);
21064SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3*,int);
21065SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3*);
21066SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3*);
21067SQLITE_PRIVATEstatic void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*);
21068SQLITE_PRIVATEstatic Expr *sqlite3ColumnExpr(Table*,Column*);
21069SQLITE_PRIVATEstatic void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl);
21070SQLITE_PRIVATEstatic const char *sqlite3ColumnColl(Column*);
21071SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3*,Table*);
21072SQLITE_PRIVATEstatic void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect);
21073SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
21074SQLITE_PRIVATEstatic void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
21075SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
21076SQLITE_PRIVATEstatic void sqlite3OpenSchemaTable(Parse *, int);
21077SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table*);
21078SQLITE_PRIVATEstatic i16 sqlite3TableColumnToIndex(Index*, i16);
21079#ifdef SQLITE_OMIT_GENERATED_COLUMNS
21080# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
21081# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
21082#else
21083SQLITE_PRIVATEstatic i16 sqlite3TableColumnToStorage(Table*, i16);
21084SQLITE_PRIVATEstatic i16 sqlite3StorageColumnToTable(Table*, i16);
21085#endif
21086SQLITE_PRIVATEstatic void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
21087#if SQLITE_ENABLE_HIDDEN_COLUMNS
21088SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table*, Column*);
21089#else
21090# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
21091#endif
21092SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse*,Token,Token);
21093SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse*, int);
21094SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
21095SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
21096SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
21097SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse*, Token*);
21098SQLITE_PRIVATEstatic void sqlite3AddGenerated(Parse*,Expr*,Token*);
21099SQLITE_PRIVATEstatic void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
21100SQLITE_PRIVATEstatic void sqlite3AddReturning(Parse*,ExprList*);
21101SQLITE_PRIVATEstatic int sqlite3ParseUri(const char*,const char*,unsigned int*,
21102 sqlite3_vfs**,char**,char **);
21103#define sqlite3CodecQueryParameters(A,B,C)0 0
21104SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
21105
21106#ifdef SQLITE_UNTESTABLE
21107# define sqlite3FaultSim(X) SQLITE_OK0
21108#else
21109SQLITE_PRIVATEstatic int sqlite3FaultSim(int);
21110#endif
21111
21112SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32);
21113SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec*, u32);
21114SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec*, u32);
21115SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec*, u32);
21116SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec*, u32, void*);
21117SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec*);
21118SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec*);
21119#ifndef SQLITE_UNTESTABLE
21120SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int,int*);
21121#endif
21122
21123SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3*);
21124SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void*);
21125SQLITE_PRIVATEstatic void sqlite3RowSetClear(void*);
21126SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet*, i64);
21127SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet*, int iBatch, i64);
21128SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet*, i64*);
21129
21130SQLITE_PRIVATEstatic void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
21131
21132#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
21133SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse*,Table*);
21134#else
21135# define sqlite3ViewGetColumnNames(A,B) 0
21136#endif
21137
21138#if SQLITE_MAX_ATTACHED10>30
21139SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask);
21140#endif
21141SQLITE_PRIVATEstatic void sqlite3DropTable(Parse*, SrcList*, int, int);
21142SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse*, Table*, int, int);
21143SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3*, Table*);
21144SQLITE_PRIVATEstatic void sqlite3DeleteTableGeneric(sqlite3*, void*);
21145SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3*, Index*);
21146#ifndef SQLITE_OMIT_AUTOINCREMENT
21147SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse);
21148SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse);
21149#else
21150# define sqlite3AutoincrementBegin(X)
21151# define sqlite3AutoincrementEnd(X)
21152#endif
21153SQLITE_PRIVATEstatic void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
21154#ifndef SQLITE_OMIT_GENERATED_COLUMNS
21155SQLITE_PRIVATEstatic void sqlite3ComputeGeneratedColumns(Parse*, int, Table*);
21156#endif
21157SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
21158SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
21159SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList*,const char*);
21160SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
21161SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2);
21162SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
21163SQLITE_PRIVATEstatic void sqlite3SubqueryDelete(sqlite3*,Subquery*);
21164SQLITE_PRIVATEstatic Select *sqlite3SubqueryDetach(sqlite3*,SrcItem*);
21165SQLITE_PRIVATEstatic int sqlite3SrcItemAttachSubquery(Parse*, SrcItem*, Select*, int);
21166SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
21167 Token*, Select*, OnOrUsing*);
21168SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
21169SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
21170SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *, SrcItem *);
21171SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
21172SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse*, SrcList*);
21173SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3*, IdList*);
21174SQLITE_PRIVATEstatic void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
21175SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3*, SrcList*);
21176SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
21177SQLITE_PRIVATEstatic void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
21178 Expr*, int, int, u8);
21179SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse*, SrcList*, int);
21180SQLITE_PRIVATEstatic int sqlite3Select(Parse*, Select*, SelectDest*);
21181SQLITE_PRIVATEstatic Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
21182 Expr*,ExprList*,u32,Expr*);
21183SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3*, Select*);
21184SQLITE_PRIVATEstatic void sqlite3SelectDeleteGeneric(sqlite3*,void*);
21185SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse*, SrcList*);
21186SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
21187SQLITE_PRIVATEstatic void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
21188#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) && !defined(SQLITE_OMIT_SUBQUERY)
21189SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
21190#endif
21191SQLITE_PRIVATEstatic void sqlite3CodeChangeCount(Vdbe*,int,const char*);
21192SQLITE_PRIVATEstatic void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
21193SQLITE_PRIVATEstatic void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
21194 Upsert*);
21195SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
21196 ExprList*,Select*,u16,int);
21197SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo*);
21198SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo*);
21199SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo*);
21200SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo*);
21201SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
21202SQLITE_PRIVATEstatic void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*);
21203SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo*);
21204SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo*);
21205SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo*);
21206SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo*, int*);
21207#define ONEPASS_OFF0 0 /* Use of ONEPASS not allowed */
21208#define ONEPASS_SINGLE1 1 /* ONEPASS valid for a single row update */
21209#define ONEPASS_MULTI2 2 /* ONEPASS is valid for multiple rows */
21210SQLITE_PRIVATEstatic int sqlite3WhereUsesDeferredSeek(WhereInfo*);
21211SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
21212SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
21213SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
21214SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse*, int, int, int);
21215SQLITE_PRIVATEstatic void sqlite3ExprToRegister(Expr *pExpr, int iReg);
21216SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse*, Expr*, int);
21217#ifndef SQLITE_OMIT_GENERATED_COLUMNS
21218SQLITE_PRIVATEstatic void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
21219#endif
21220SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse*, Expr*, int);
21221SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
21222SQLITE_PRIVATEstatic int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
21223SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
21224SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse*, Expr*, int);
21225SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
21226#define SQLITE_ECEL_DUP0x01 0x01 /* Deep, not shallow copies */
21227#define SQLITE_ECEL_FACTOR0x02 0x02 /* Factor out constant terms */
21228#define SQLITE_ECEL_REF0x04 0x04 /* Use ExprList.u.x.iOrderByCol */
21229#define SQLITE_ECEL_OMITREF0x08 0x08 /* Omit if ExprList.u.x.iOrderByCol */
21230SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
21231SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
21232SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
21233SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3*,const char*, const char*);
21234#define LOCATE_VIEW0x01 0x01
21235#define LOCATE_NOERR0x02 0x02
21236SQLITE_PRIVATEstatic Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
21237SQLITE_PRIVATEstatic const char *sqlite3PreferredTableName(const char*);
21238SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *);
21239SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
21240SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
21241SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
21242SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse*,Token*,Expr*);
21243SQLITE_PRIVATEstatic int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
21244SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3*, const Token*);
21245SQLITE_PRIVATEstatic int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int);
21246SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr*,Expr*,int);
21247SQLITE_PRIVATEstatic int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
21248SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
21249SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr*,int,int);
21250SQLITE_PRIVATEstatic void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
21251SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
21252SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
21253SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
21254SQLITE_PRIVATEstatic int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*);
21255SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse*);
21256#ifndef SQLITE_UNTESTABLE
21257SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void);
21258SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void);
21259#endif
21260SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3*,int);
21261SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse*, int);
21262SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
21263SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse*, int);
21264SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse*,int);
21265SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse*, int, Token*);
21266SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *);
21267SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
21268SQLITE_PRIVATEstatic u32 sqlite3IsTrueOrFalse(const char*);
21269SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr*);
21270SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr*);
21271SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Parse*,Expr*);
21272SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr*, u8);
21273SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
21274SQLITE_PRIVATEstatic int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int);
21275#ifdef SQLITE_ENABLE_CURSOR_HINTS
21276SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr*);
21277#endif
21278SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(const Expr*, int*, Parse*);
21279SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr*);
21280SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
21281SQLITE_PRIVATEstatic int sqlite3IsRowid(const char*);
21282SQLITE_PRIVATEstatic const char *sqlite3RowidAlias(Table *pTab);
21283SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
21284 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
21285SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
21286SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
21287SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse*,int);
21288SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
21289SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
21290 u8,u8,int,int*,int*,Upsert*);
21291#ifdef SQLITE_ENABLE_NULL_TRIM
21292SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe*,Table*);
21293#else
21294# define sqlite3SetMakeRecordP5(A,B)
21295#endif
21296SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
21297SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
21298SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse*, int, int);
21299SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse*);
21300SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse*);
21301SQLITE_PRIVATEstatic void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
21302SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(Parse*, int, Index*);
21303SQLITE_PRIVATEstatic void sqlite3RowidConstraint(Parse*, int, Table*);
21304SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3*,const Expr*,int);
21305SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int);
21306SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int);
21307SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3*,const IdList*);
21308SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3*,const Select*,int);
21309SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(int,const char*);
21310SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(FuncDef*,int);
21311SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21312SQLITE_PRIVATEstatic void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
21313SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void);
21314SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void);
21315SQLITE_PRIVATEstatic void sqlite3RegisterJsonFunctions(void);
21316SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
21317#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
21318SQLITE_PRIVATEstatic int sqlite3JsonTableFunctions(sqlite3*);
21319#endif
21320SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3*);
21321SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3*);
21322SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse*, int);
21323SQLITE_PRIVATEstatic With *sqlite3WithDup(sqlite3 *db, With *p);
21324
21325#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
21326SQLITE_PRIVATEstatic void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
21327#endif
21328
21329#ifndef SQLITE_OMIT_TRIGGER
21330SQLITE_PRIVATEstatic void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
21331 Expr*,int, int);
21332SQLITE_PRIVATEstatic void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
21333SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse*, SrcList*, int);
21334SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse*, Trigger*);
21335SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
21336SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *, Table *);
21337SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
21338 int, int, int);
21339SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
21340 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
21341SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
21342SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
21343 const char*,const char*);
21344SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
21345 Select*,u8,Upsert*,
21346 const char*,const char*);
21347SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*,
21348 Expr*, u8, const char*,const char*);
21349SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
21350 const char*,const char*);
21351SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3*, Trigger*);
21352SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
21353SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
21354SQLITE_PRIVATEstatic SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*);
21355# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) ((p)->pToplevel ? (p)->pToplevel : (p))
21356# define sqlite3IsToplevel(p)((p)->pToplevel==0) ((p)->pToplevel==0)
21357#else
21358# define sqlite3TriggersExist(B,C,D,E,F) 0
21359# define sqlite3DeleteTrigger(A,B)
21360# define sqlite3DropTriggerPtr(A,B)
21361# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
21362# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
21363# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
21364# define sqlite3TriggerList(X, Y) 0
21365# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) p
21366# define sqlite3IsToplevel(p)((p)->pToplevel==0) 1
21367# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
21368# define sqlite3TriggerStepSrc(A,B) 0
21369#endif
21370
21371SQLITE_PRIVATEstatic int sqlite3JoinType(Parse*, Token*, Token*, Token*);
21372SQLITE_PRIVATEstatic int sqlite3ColumnIndex(Table *pTab, const char *zCol);
21373SQLITE_PRIVATEstatic void sqlite3SrcItemColumnUsed(SrcItem*,int);
21374SQLITE_PRIVATEstatic void sqlite3SetJoinExpr(Expr*,int,u32);
21375SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
21376SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse*, int);
21377#ifndef SQLITE_OMIT_AUTHORIZATION
21378SQLITE_PRIVATEstatic void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
21379SQLITE_PRIVATEstatic int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
21380SQLITE_PRIVATEstatic void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
21381SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext*);
21382SQLITE_PRIVATEstatic int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
21383#else
21384# define sqlite3AuthRead(a,b,c,d)
21385# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK0
21386# define sqlite3AuthContextPush(a,b,c)
21387# define sqlite3AuthContextPop(a) ((void)(a))
21388#endif
21389SQLITE_PRIVATEstatic int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName);
21390SQLITE_PRIVATEstatic void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
21391SQLITE_PRIVATEstatic void sqlite3Detach(Parse*, Expr*);
21392SQLITE_PRIVATEstatic void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
21393SQLITE_PRIVATEstatic int sqlite3FixSrcList(DbFixer*, SrcList*);
21394SQLITE_PRIVATEstatic int sqlite3FixSelect(DbFixer*, Select*);
21395SQLITE_PRIVATEstatic int sqlite3FixExpr(DbFixer*, Expr*);
21396SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
21397
21398SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double,sqlite3_int64);
21399SQLITE_PRIVATEstatic i64 sqlite3RealToI64(double);
21400SQLITE_PRIVATEstatic int sqlite3Int64ToText(i64,char*);
21401SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double*, int, u8);
21402SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *, int*);
21403SQLITE_PRIVATEstatic int sqlite3GetUInt32(const char*, u32*);
21404SQLITE_PRIVATEstatic int sqlite3Atoi(const char*);
21405#ifndef SQLITE_OMIT_UTF16
21406SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *pData, int nByte, int nChar);
21407#endif
21408SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *pData, int nByte);
21409SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(const u8**);
21410SQLITE_PRIVATEstatic int sqlite3Utf8ReadLimited(const u8*, int, u32*);
21411SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64);
21412SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst,LogEst);
21413SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double);
21414SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst);
21415SQLITE_PRIVATEstatic VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
21416SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList*,int);
21417SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList*,const char*,int);
21418
21419/*
21420** Routines to read and write variable-length integers. These used to
21421** be defined locally, but now we use the varint routines in the util.c
21422** file.
21423*/
21424SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char*, u64);
21425SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *, u64 *);
21426SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *, u32 *);
21427SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v);
21428
21429/*
21430** The common case is for a varint to be a single byte. They following
21431** macros handle the common case without a procedure call, but then call
21432** the procedure for larger varints.
21433*/
21434#define getVarint32(A,B)(u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32(
(A),(u32 *)&(B)))
\
21435 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
21436#define getVarint32NR(A,B)B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(
B))
\
21437 B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B))
21438#define putVarint32(A,B)(u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1: sqlite3PutVarint
((A),(B)))
\
21439 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
21440 sqlite3PutVarint((A),(B)))
21441#define getVarintsqlite3GetVarint sqlite3GetVarint
21442#define putVarintsqlite3PutVarint sqlite3PutVarint
21443
21444
21445SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
21446SQLITE_PRIVATEstatic char *sqlite3TableAffinityStr(sqlite3*,const Table*);
21447SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe*, Table*, int);
21448SQLITE_PRIVATEstatic char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
21449SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);
21450SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(const Table*,int);
21451SQLITE_PRIVATEstatic char sqlite3ExprAffinity(const Expr *pExpr);
21452SQLITE_PRIVATEstatic int sqlite3ExprDataType(const Expr *pExpr);
21453SQLITE_PRIVATEstatic int sqlite3Atoi64(const char*, i64*, int, u8);
21454SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char*, i64*);
21455SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
21456SQLITE_PRIVATEstatic void sqlite3Error(sqlite3*,int);
21457SQLITE_PRIVATEstatic void sqlite3ErrorClear(sqlite3*);
21458SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3*,int);
21459#if !defined(SQLITE_OMIT_BLOB_LITERAL)
21460SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
21461#endif
21462SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h);
21463SQLITE_PRIVATEstatic int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
21464
21465#if defined(SQLITE_NEED_ERR_NAME)
21466SQLITE_PRIVATEstatic const char *sqlite3ErrName(int);
21467#endif
21468
21469#ifndef SQLITE_OMIT_DESERIALIZE
21470SQLITE_PRIVATEstatic int sqlite3MemdbInit(void);
21471SQLITE_PRIVATEstatic int sqlite3IsMemdb(const sqlite3_vfs*);
21472#else
21473# define sqlite3IsMemdb(X) 0
21474#endif
21475
21476SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int);
21477SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse);
21478SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
21479SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq*);
21480SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
21481SQLITE_PRIVATEstatic void sqlite3SetTextEncoding(sqlite3 *db, u8);
21482SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr);
21483SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr);
21484SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*);
21485SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int);
21486SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*);
21487SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr*);
21488SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
21489SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *, CollSeq *);
21490SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3*);
21491SQLITE_PRIVATEstatic int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
21492SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *, i64);
21493SQLITE_PRIVATEstatic int sqlite3AddInt64(i64*,i64);
21494SQLITE_PRIVATEstatic int sqlite3SubInt64(i64*,i64);
21495SQLITE_PRIVATEstatic int sqlite3MulInt64(i64*,i64);
21496SQLITE_PRIVATEstatic int sqlite3AbsInt32(int);
21497#ifdef SQLITE_ENABLE_8_3_NAMES
21498SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char*, char*);
21499#else
21500# define sqlite3FileSuffix3(X,Y)
21501#endif
21502SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z,u8);
21503
21504SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value*, u8);
21505SQLITE_PRIVATEstatic int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*));
21506SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value*, u8);
21507SQLITE_PRIVATEstatic void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
21508 void(*)(void*));
21509SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value*);
21510SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value*);
21511#ifndef SQLITE_UNTESTABLE
21512SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context*);
21513#endif
21514SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *);
21515#ifndef SQLITE_OMIT_UTF16
21516SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
21517#endif
21518SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **);
21519SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
21520#ifndef SQLITE_AMALGAMATION1
21521SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[];
21522SQLITE_PRIVATEstatic const char sqlite3StrBINARY[];
21523SQLITE_PRIVATEstatic const unsigned char sqlite3StdTypeLen[];
21524SQLITE_PRIVATEstatic const char sqlite3StdTypeAffinity[];
21525SQLITE_PRIVATEstatic const char *sqlite3StdType[];
21526SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[];
21527SQLITE_PRIVATEstatic const unsigned char *sqlite3aLTb;
21528SQLITE_PRIVATEstatic const unsigned char *sqlite3aEQb;
21529SQLITE_PRIVATEstatic const unsigned char *sqlite3aGTb;
21530SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[];
21531SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config;
21532SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
21533#ifndef SQLITE_OMIT_WSD
21534SQLITE_PRIVATEstatic int sqlite3PendingByte;
21535#endif
21536#endif /* SQLITE_AMALGAMATION */
21537#ifdef VDBE_PROFILE
21538SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt;
21539#endif
21540SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno);
21541SQLITE_PRIVATEstatic void sqlite3Reindex(Parse*, Token*, Token*);
21542SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void);
21543SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
21544SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
21545SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *, int *);
21546SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse*, const char*, ...);
21547SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3*, int);
21548SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
21549SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse*, Expr*);
21550SQLITE_PRIVATEstatic void sqlite3SelectPrep(Parse*, Select*, NameContext*);
21551SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse*, SrcItem*);
21552SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
21553SQLITE_PRIVATEstatic int sqlite3MatchEName(
21554 const struct ExprList_item*,
21555 const char*,
21556 const char*,
21557 const char*,
21558 int*
21559);
21560SQLITE_PRIVATEstatic Bitmask sqlite3ExprColUsed(Expr*);
21561SQLITE_PRIVATEstatic u8 sqlite3StrIHash(const char*);
21562SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(NameContext*, Expr*);
21563SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(NameContext*, ExprList*);
21564SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
21565SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
21566SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
21567SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
21568SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *, Token *);
21569SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
21570SQLITE_PRIVATEstatic void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*);
21571SQLITE_PRIVATEstatic const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*);
21572SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom);
21573SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse*, Expr*);
21574SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
21575SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
21576SQLITE_PRIVATEstatic char sqlite3AffinityType(const char*, Column*);
21577SQLITE_PRIVATEstatic void sqlite3Analyze(Parse*, Token*, Token*);
21578SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler*);
21579SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3*, Token*);
21580SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *, const char *);
21581SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3*,int iDB);
21582SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3*,Index*);
21583SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index*);
21584SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3*, int);
21585SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
21586SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *);
21587SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
21588SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
21589SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
21590SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo*);
21591SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
21592SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
21593SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
21594SQLITE_PRIVATEstatic const char *sqlite3SelectOpName(int);
21595SQLITE_PRIVATEstatic int sqlite3HasExplicitNulls(Parse*, ExprList*);
21596
21597#ifdef SQLITE_DEBUG
21598SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo*);
21599#endif
21600SQLITE_PRIVATEstatic int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
21601 void (*)(sqlite3_context*,int,sqlite3_value **),
21602 void (*)(sqlite3_context*,int,sqlite3_value **),
21603 void (*)(sqlite3_context*),
21604 void (*)(sqlite3_context*),
21605 void (*)(sqlite3_context*,int,sqlite3_value **),
21606 FuncDestructor *pDestructor
21607);
21608SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void*);
21609SQLITE_PRIVATEstatic void *sqlite3OomFault(sqlite3*);
21610SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3*);
21611SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3 *db, int);
21612SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *);
21613
21614SQLITE_PRIVATEstatic char *sqlite3RCStrRef(char*);
21615SQLITE_PRIVATEstatic void sqlite3RCStrUnref(void*);
21616SQLITE_PRIVATEstatic char *sqlite3RCStrNew(u64);
21617SQLITE_PRIVATEstatic char *sqlite3RCStrResize(char*,u64);
21618
21619SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
21620SQLITE_PRIVATEstatic int sqlite3StrAccumEnlarge(StrAccum*, i64);
21621SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum*);
21622SQLITE_PRIVATEstatic void sqlite3StrAccumSetError(StrAccum*, u8);
21623SQLITE_PRIVATEstatic void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
21624SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest*,int,int);
21625SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
21626SQLITE_PRIVATEstatic void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
21627SQLITE_PRIVATEstatic void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*);
21628
21629SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *);
21630SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
21631
21632#ifndef SQLITE_OMIT_SUBQUERY
21633SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse*, Expr*);
21634#else
21635# define sqlite3ExprCheckIN(x,y) SQLITE_OK0
21636#endif
21637
21638#ifdef SQLITE_ENABLE_STAT4
21639SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
21640 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
21641SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
21642SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord*);
21643SQLITE_PRIVATEstatic int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
21644SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
21645#endif
21646
21647/*
21648** The interface to the LEMON-generated parser
21649*/
21650#ifndef SQLITE_AMALGAMATION1
21651SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
21652SQLITE_PRIVATEstatic void sqlite3ParserFree(void*, void(*)(void*));
21653#endif
21654SQLITE_PRIVATEstatic void sqlite3Parser(void*, int, Token);
21655SQLITE_PRIVATEstatic int sqlite3ParserFallback(int);
21656#ifdef YYTRACKMAXSTACKDEPTH
21657SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void*);
21658#endif
21659
21660SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3*);
21661#ifndef SQLITE_OMIT_LOAD_EXTENSION1
21662SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3*);
21663#else
21664# define sqlite3CloseExtensions(X)
21665#endif
21666
21667#ifndef SQLITE_OMIT_SHARED_CACHE
21668SQLITE_PRIVATEstatic void sqlite3TableLock(Parse *, int, Pgno, u8, const char *);
21669#else
21670 #define sqlite3TableLock(v,w,x,y,z)
21671#endif
21672
21673#ifdef SQLITE_TEST
21674SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char*);
21675#endif
21676
21677#ifdef SQLITE_OMIT_VIRTUALTABLE
21678# define sqlite3VtabClear(D,T)
21679# define sqlite3VtabSync(X,Y) SQLITE_OK0
21680# define sqlite3VtabRollback(X)
21681# define sqlite3VtabCommit(X)
21682# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) 0
21683# define sqlite3VtabLock(X)
21684# define sqlite3VtabUnlock(X)
21685# define sqlite3VtabModuleUnref(D,X)
21686# define sqlite3VtabUnlockList(X)
21687# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK0
21688# define sqlite3GetVTable(X,Y) ((VTable*)0)
21689#else
21690SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table*);
21691SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
21692SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe*);
21693SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db);
21694SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db);
21695SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *);
21696SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *);
21697SQLITE_PRIVATEstatic void sqlite3VtabModuleUnref(sqlite3*,Module*);
21698SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3*);
21699SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *, int, int);
21700SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
21701SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3*, Table*);
21702SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
21703 sqlite3*,
21704 const char*,
21705 const sqlite3_module*,
21706 void*,
21707 void(*)(void*)
21708 );
21709# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ((db)->nVTrans>0 && (db)->aVTrans==0)
21710#endif
21711SQLITE_PRIVATEstatic int sqlite3ReadOnlyShadowTables(sqlite3 *db);
21712#ifndef SQLITE_OMIT_VIRTUALTABLE
21713SQLITE_PRIVATEstatic int sqlite3ShadowTableName(sqlite3 *db, const char *zName);
21714SQLITE_PRIVATEstatic int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*);
21715SQLITE_PRIVATEstatic void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*);
21716#else
21717# define sqlite3ShadowTableName(A,B) 0
21718# define sqlite3IsShadowTableOf(A,B,C) 0
21719# define sqlite3MarkAllShadowTablesOf(A,B)
21720#endif
21721SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse*,Module*);
21722SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
21723SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse*,Table*);
21724SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
21725SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse*, Token*);
21726SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse*);
21727SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse*, Token*);
21728SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
21729SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse*, Table*);
21730SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
21731SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *, VTable *);
21732
21733SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
21734SQLITE_PRIVATEstatic void sqlite3VtabUsesAllSchemas(Parse*);
21735SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
21736SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
21737SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
21738SQLITE_PRIVATEstatic void sqlite3ParseObjectInit(Parse*,sqlite3*);
21739SQLITE_PRIVATEstatic void sqlite3ParseObjectReset(Parse*);
21740SQLITE_PRIVATEstatic void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*);
21741#ifdef SQLITE_ENABLE_NORMALIZE
21742SQLITE_PRIVATEstatic char *sqlite3Normalize(Vdbe*, const char*);
21743#endif
21744SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe*);
21745SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
21746SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*);
21747SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*);
21748SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3*);
21749SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int);
21750#ifndef SQLITE_OMIT_WAL
21751SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
21752SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
21753#endif
21754#ifndef SQLITE_OMIT_CTE
21755SQLITE_PRIVATEstatic Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
21756SQLITE_PRIVATEstatic void sqlite3CteDelete(sqlite3*,Cte*);
21757SQLITE_PRIVATEstatic With *sqlite3WithAdd(Parse*,With*,Cte*);
21758SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3*,With*);
21759SQLITE_PRIVATEstatic void sqlite3WithDeleteGeneric(sqlite3*,void*);
21760SQLITE_PRIVATEstatic With *sqlite3WithPush(Parse*, With*, u8);
21761#else
21762# define sqlite3CteNew(P,T,E,S) ((void*)0)
21763# define sqlite3CteDelete(D,C)
21764# define sqlite3CteWithAdd(P,W,C) ((void*)0)
21765# define sqlite3WithDelete(x,y)
21766# define sqlite3WithPush(x,y,z) ((void*)0)
21767#endif
21768#ifndef SQLITE_OMIT_UPSERT
21769SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
21770SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3*,Upsert*);
21771SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
21772SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*,Upsert*);
21773SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
21774SQLITE_PRIVATEstatic Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
21775SQLITE_PRIVATEstatic int sqlite3UpsertNextIsIPK(Upsert*);
21776#else
21777#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0)
21778#define sqlite3UpsertDelete(x,y)
21779#define sqlite3UpsertDup(x,y) ((Upsert*)0)
21780#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0)
21781#define sqlite3UpsertNextIsIPK(x) 0
21782#endif
21783
21784
21785/* Declarations for functions in fkey.c. All of these are replaced by
21786** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
21787** key functionality is available. If OMIT_TRIGGER is defined but
21788** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
21789** this case foreign keys are parsed, but no other functionality is
21790** provided (enforcement of FK constraints requires the triggers sub-system).
21791*/
21792#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
21793SQLITE_PRIVATEstatic void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
21794SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse*, SrcList *, Table*);
21795SQLITE_PRIVATEstatic void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
21796SQLITE_PRIVATEstatic int sqlite3FkRequired(Parse*, Table*, int*, int);
21797SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(Parse*, Table*);
21798SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *);
21799SQLITE_PRIVATEstatic void sqlite3FkClearTriggerCache(sqlite3*,int);
21800#else
21801 #define sqlite3FkActions(a,b,c,d,e,f)
21802 #define sqlite3FkCheck(a,b,c,d,e,f)
21803 #define sqlite3FkDropTable(a,b,c)
21804 #define sqlite3FkOldmask(a,b) 0
21805 #define sqlite3FkRequired(a,b,c,d) 0
21806 #define sqlite3FkReferences(a) 0
21807 #define sqlite3FkClearTriggerCache(a,b)
21808#endif
21809#ifndef SQLITE_OMIT_FOREIGN_KEY
21810SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *, Table*);
21811SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
21812#else
21813 #define sqlite3FkDelete(a,b)
21814 #define sqlite3FkLocateIndex(a,b,c,d,e)
21815#endif
21816
21817
21818/*
21819** Available fault injectors. Should be numbered beginning with 0.
21820*/
21821#define SQLITE_FAULTINJECTOR_MALLOC0 0
21822#define SQLITE_FAULTINJECTOR_COUNT1 1
21823
21824/*
21825** The interface to the code in fault.c used for identifying "benign"
21826** malloc failures. This is only present if SQLITE_UNTESTABLE
21827** is not defined.
21828*/
21829#ifndef SQLITE_UNTESTABLE
21830SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void);
21831SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void);
21832#else
21833 #define sqlite3BeginBenignMalloc()
21834 #define sqlite3EndBenignMalloc()
21835#endif
21836
21837/*
21838** Allowed return values from sqlite3FindInIndex()
21839*/
21840#define IN_INDEX_ROWID1 1 /* Search the rowid of the table */
21841#define IN_INDEX_EPH2 2 /* Search an ephemeral b-tree */
21842#define IN_INDEX_INDEX_ASC3 3 /* Existing index ASCENDING */
21843#define IN_INDEX_INDEX_DESC4 4 /* Existing index DESCENDING */
21844#define IN_INDEX_NOOP5 5 /* No table available. Use comparisons */
21845/*
21846** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
21847*/
21848#define IN_INDEX_NOOP_OK0x0001 0x0001 /* OK to return IN_INDEX_NOOP */
21849#define IN_INDEX_MEMBERSHIP0x0002 0x0002 /* IN operator used for membership test */
21850#define IN_INDEX_LOOP0x0004 0x0004 /* IN operator used as a loop */
21851SQLITE_PRIVATEstatic int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
21852
21853SQLITE_PRIVATEstatic int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
21854SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *);
21855#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
21856 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
21857SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *);
21858#endif
21859
21860SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p);
21861SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *);
21862
21863SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
21864#if SQLITE_MAX_EXPR_DEPTH1000>0
21865SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(const Select *);
21866SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse*, int);
21867#else
21868 #define sqlite3SelectExprHeight(x) 0
21869 #define sqlite3ExprCheckHeight(x,y)
21870#endif
21871SQLITE_PRIVATEstatic void sqlite3ExprSetErrorOffset(Expr*,int);
21872
21873SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8*);
21874SQLITE_PRIVATEstatic void sqlite3Put4byte(u8*, u32);
21875
21876#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
21877SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
21878SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db);
21879SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db);
21880#else
21881 #define sqlite3ConnectionBlocked(x,y)
21882 #define sqlite3ConnectionUnlocked(x)
21883 #define sqlite3ConnectionClosed(x)
21884#endif
21885
21886#ifdef SQLITE_DEBUG
21887SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE*, char *);
21888#endif
21889#if defined(YYCOVERAGE)
21890SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE*);
21891#endif
21892
21893/*
21894** If the SQLITE_ENABLE IOTRACE exists then the global variable
21895** sqlite3IoTrace is a pointer to a printf-like routine used to
21896** print I/O tracing messages.
21897*/
21898#ifdef SQLITE_ENABLE_IOTRACE
21899# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
21900SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe*);
21901SQLITE_API SQLITE_EXTERNextern void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
21902#else
21903# define IOTRACE(A)
21904# define sqlite3VdbeIOTraceSql(X)
21905#endif
21906
21907/*
21908** These routines are available for the mem2.c debugging memory allocator
21909** only. They are used to verify that different "types" of memory
21910** allocations are properly tracked by the system.
21911**
21912** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
21913** the MEMTYPE_* macros defined below. The type must be a bitmask with
21914** a single bit set.
21915**
21916** sqlite3MemdebugHasType() returns true if any of the bits in its second
21917** argument match the type set by the previous sqlite3MemdebugSetType().
21918** sqlite3MemdebugHasType() is intended for use inside assert() statements.
21919**
21920** sqlite3MemdebugNoType() returns true if none of the bits in its second
21921** argument match the type set by the previous sqlite3MemdebugSetType().
21922**
21923** Perhaps the most important point is the difference between MEMTYPE_HEAP
21924** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
21925** it might have been allocated by lookaside, except the allocation was
21926** too large or lookaside was already full. It is important to verify
21927** that allocations that might have been satisfied by lookaside are not
21928** passed back to non-lookaside free() routines. Asserts such as the
21929** example above are placed on the non-lookaside free() routines to verify
21930** this constraint.
21931**
21932** All of this is no-op for a production build. It only comes into
21933** play when the SQLITE_MEMDEBUG compile-time option is used.
21934*/
21935#ifdef SQLITE_MEMDEBUG
21936SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void*,u8);
21937SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(const void*,u8)1;
21938SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(const void*,u8)1;
21939#else
21940# define sqlite3MemdebugSetType(X,Y) /* no-op */
21941# define sqlite3MemdebugHasType(X,Y)1 1
21942# define sqlite3MemdebugNoType(X,Y)1 1
21943#endif
21944#define MEMTYPE_HEAP0x01 0x01 /* General heap allocations */
21945#define MEMTYPE_LOOKASIDE0x02 0x02 /* Heap that might have been lookaside */
21946#define MEMTYPE_PCACHE0x04 0x04 /* Page cache allocations */
21947
21948/*
21949** Threading interface
21950*/
21951#if SQLITE_MAX_WORKER_THREADS8>0
21952SQLITE_PRIVATEstatic int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
21953SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread*, void**);
21954#endif
21955
21956#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
21957SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3*);
21958#endif
21959#if defined(SQLITE_ENABLE_DBSTAT_VTAB1) || defined(SQLITE_TEST)
21960SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3*);
21961#endif
21962
21963SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(const Expr *pExpr);
21964SQLITE_PRIVATEstatic int sqlite3ExprIsVector(const Expr *pExpr);
21965SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr*, int);
21966SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int);
21967SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse*, Expr*);
21968
21969#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
21970SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt);
21971#endif
21972
21973#if SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL)
21974SQLITE_PRIVATEstatic int sqlite3KvvfsInit(void);
21975#endif
21976
21977#if defined(VDBE_PROFILE) \
21978 || defined(SQLITE_PERFORMANCE_TRACE) \
21979 || defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
21980SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3Hwtime(void);
21981#endif
21982
21983#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
21984# define IS_STMT_SCANSTATUS(db)(db->flags & 0x00000400) (db->flags & SQLITE_StmtScanStatus0x00000400)
21985#else
21986# define IS_STMT_SCANSTATUS(db)(db->flags & 0x00000400) 0
21987#endif
21988
21989#endif /* SQLITEINT_H */
21990
21991/************** End of sqliteInt.h *******************************************/
21992/************** Begin file os_common.h ***************************************/
21993/*
21994** 2004 May 22
21995**
21996** The author disclaims copyright to this source code. In place of
21997** a legal notice, here is a blessing:
21998**
21999** May you do good and not evil.
22000** May you find forgiveness for yourself and forgive others.
22001** May you share freely, never taking more than you give.
22002**
22003******************************************************************************
22004**
22005** This file contains macros and a little bit of code that is common to
22006** all of the platform-specific files (os_*.c) and is #included into those
22007** files.
22008**
22009** This file should be #included by the os_*.c files only. It is not a
22010** general purpose header file.
22011*/
22012#ifndef _OS_COMMON_H_
22013#define _OS_COMMON_H_
22014
22015/*
22016** At least two bugs have slipped in because we changed the MEMORY_DEBUG
22017** macro to SQLITE_DEBUG and some older makefiles have not yet made the
22018** switch. The following code should catch this problem at compile-time.
22019*/
22020#ifdef MEMORY_DEBUG
22021# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22022#endif
22023
22024/*
22025** Macros for performance tracing. Normally turned off. Only works
22026** on i486 hardware.
22027*/
22028#ifdef SQLITE_PERFORMANCE_TRACE
22029
22030static sqlite_uint64 g_start;
22031static sqlite_uint64 g_elapsed;
22032#define TIMER_START g_start=sqlite3Hwtime()
22033#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
22034#define TIMER_ELAPSED((sqlite_uint64)0) g_elapsed
22035#else
22036#define TIMER_START
22037#define TIMER_END
22038#define TIMER_ELAPSED((sqlite_uint64)0) ((sqlite_uint64)0)
22039#endif
22040
22041/*
22042** If we compile with the SQLITE_TEST macro set, then the following block
22043** of code will give us the ability to simulate a disk I/O error. This
22044** is used for testing the I/O recovery logic.
22045*/
22046#if defined(SQLITE_TEST)
22047SQLITE_API extern int sqlite3_io_error_hit;
22048SQLITE_API extern int sqlite3_io_error_hardhit;
22049SQLITE_API extern int sqlite3_io_error_pending;
22050SQLITE_API extern int sqlite3_io_error_persist;
22051SQLITE_API extern int sqlite3_io_error_benign;
22052SQLITE_API extern int sqlite3_diskfull_pending;
22053SQLITE_API extern int sqlite3_diskfull;
22054#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
22055#define SimulateIOError(CODE) \
22056 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
22057 || sqlite3_io_error_pending-- == 1 ) \
22058 { local_ioerr(); CODE; }
22059static void local_ioerr(){
22060 IOTRACE(("IOERR\n"));
22061 sqlite3_io_error_hit++;
22062 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
22063}
22064#define SimulateDiskfullError(CODE) \
22065 if( sqlite3_diskfull_pending ){ \
22066 if( sqlite3_diskfull_pending == 1 ){ \
22067 local_ioerr(); \
22068 sqlite3_diskfull = 1; \
22069 sqlite3_io_error_hit = 1; \
22070 CODE; \
22071 }else{ \
22072 sqlite3_diskfull_pending--; \
22073 } \
22074 }
22075#else
22076#define SimulateIOErrorBenign(X)
22077#define SimulateIOError(A)
22078#define SimulateDiskfullError(A)
22079#endif /* defined(SQLITE_TEST) */
22080
22081/*
22082** When testing, keep a count of the number of open files.
22083*/
22084#if defined(SQLITE_TEST)
22085SQLITE_API extern int sqlite3_open_file_count;
22086#define OpenCounter(X) sqlite3_open_file_count+=(X)
22087#else
22088#define OpenCounter(X)
22089#endif /* defined(SQLITE_TEST) */
22090
22091#endif /* !defined(_OS_COMMON_H_) */
22092
22093/************** End of os_common.h *******************************************/
22094/************** Begin file ctime.c *******************************************/
22095/* DO NOT EDIT!
22096** This file is automatically generated by the script in the canonical
22097** SQLite source tree at tool/mkctimec.tcl.
22098**
22099** To modify this header, edit any of the various lists in that script
22100** which specify categories of generated conditionals in this file.
22101*/
22102
22103/*
22104** 2010 February 23
22105**
22106** The author disclaims copyright to this source code. In place of
22107** a legal notice, here is a blessing:
22108**
22109** May you do good and not evil.
22110** May you find forgiveness for yourself and forgive others.
22111** May you share freely, never taking more than you give.
22112**
22113*************************************************************************
22114**
22115** This file implements routines used to report what compile-time options
22116** SQLite was built with.
22117*/
22118#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
22119
22120/*
22121** Include the configuration header output by 'configure' if we're using the
22122** autoconf-based build
22123*/
22124#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
22125/* #include "sqlite_cfg.h" */
22126#define SQLITECONFIG_H 1
22127#endif
22128
22129/* These macros are provided to "stringify" the value of the define
22130** for those options in which the value is meaningful. */
22131#define CTIMEOPT_VAL_(opt)"opt" #opt
22132#define CTIMEOPT_VAL(opt)"opt" CTIMEOPT_VAL_(opt)"opt"
22133
22134/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
22135** option requires a separate macro because legal values contain a single
22136** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
22137#define CTIMEOPT_VAL2_(opt1,opt2)"opt1" "," "opt2" #opt1 "," #opt2
22138#define CTIMEOPT_VAL2(opt)CTIMEOPT_VAL2_ CTIMEOPT_VAL2_(opt)
22139/* #include "sqliteInt.h" */
22140
22141/*
22142** An array of names of all compile-time options. This array should
22143** be sorted A-Z.
22144**
22145** This array looks large, but in a typical installation actually uses
22146** only a handful of compile-time options, so most times this array is usually
22147** rather short and uses little memory space.
22148*/
22149static const char * const sqlite3azCompileOpt[] = {
22150
22151#ifdef SQLITE_32BIT_ROWID
22152 "32BIT_ROWID",
22153#endif
22154#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
22155 "4_BYTE_ALIGNED_MALLOC",
22156#endif
22157#ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN1
22158# if SQLITE_ALLOW_COVERING_INDEX_SCAN1 != 1
22159 "ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN)"1",
22160# endif
22161#endif
22162#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
22163 "ALLOW_ROWID_IN_VIEW",
22164#endif
22165#ifdef SQLITE_ALLOW_URI_AUTHORITY
22166 "ALLOW_URI_AUTHORITY",
22167#endif
22168#ifdef SQLITE_ATOMIC_INTRINSICS1
22169 "ATOMIC_INTRINSICS=" CTIMEOPT_VAL(SQLITE_ATOMIC_INTRINSICS)"1",
22170#endif
22171#ifdef SQLITE_BITMASK_TYPE
22172 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE)"SQLITE_BITMASK_TYPE",
22173#endif
22174#ifdef SQLITE_BUG_COMPATIBLE_20160819
22175 "BUG_COMPATIBLE_20160819",
22176#endif
22177#ifdef SQLITE_CASE_SENSITIVE_LIKE
22178 "CASE_SENSITIVE_LIKE",
22179#endif
22180#ifdef SQLITE_CHECK_PAGES
22181 "CHECK_PAGES",
22182#endif
22183#if defined(__clang__1) && defined(__clang_major__19)
22184 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__)"19" "."
22185 CTIMEOPT_VAL(__clang_minor__)"1" "."
22186 CTIMEOPT_VAL(__clang_patchlevel__)"2",
22187#elif defined(_MSC_VER)
22188 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER)"_MSC_VER",
22189#elif defined(__GNUC__4) && defined(__VERSION__"Clang 19.1.2")
22190 "COMPILER=gcc-" __VERSION__"Clang 19.1.2",
22191#endif
22192#ifdef SQLITE_COVERAGE_TEST
22193 "COVERAGE_TEST",
22194#endif
22195#ifdef SQLITE_DEBUG
22196 "DEBUG",
22197#endif
22198#ifdef SQLITE_DEFAULT_AUTOMATIC_INDEX
22199 "DEFAULT_AUTOMATIC_INDEX",
22200#endif
22201#ifdef SQLITE_DEFAULT_AUTOVACUUM0
22202 "DEFAULT_AUTOVACUUM",
22203#endif
22204#ifdef SQLITE_DEFAULT_CACHE_SIZE128
22205 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE)"128",
22206#endif
22207#ifdef SQLITE_DEFAULT_CKPTFULLFSYNC1
22208 "DEFAULT_CKPTFULLFSYNC",
22209#endif
22210#ifdef SQLITE_DEFAULT_FILE_FORMAT4
22211 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT)"4",
22212#endif
22213#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS0644
22214 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS)"0644",
22215#endif
22216#ifdef SQLITE_DEFAULT_FOREIGN_KEYS
22217 "DEFAULT_FOREIGN_KEYS",
22218#endif
22219#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768
22220 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT)"32768",
22221#endif
22222#ifdef SQLITE_DEFAULT_LOCKING_MODE
22223 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE)"SQLITE_DEFAULT_LOCKING_MODE",
22224#endif
22225#ifdef SQLITE_DEFAULT_LOOKASIDE1200,40
22226 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE)"1200" "," "40",
22227#endif
22228#ifdef SQLITE_DEFAULT_MEMSTATUS1
22229# if SQLITE_DEFAULT_MEMSTATUS1 != 1
22230 "DEFAULT_MEMSTATUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_MEMSTATUS)"1",
22231# endif
22232#endif
22233#ifdef SQLITE_DEFAULT_MMAP_SIZE0
22234 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE)"0",
22235#endif
22236#ifdef SQLITE_DEFAULT_PAGE_SIZE4096
22237 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE)"4096",
22238#endif
22239#ifdef SQLITE_DEFAULT_PCACHE_INITSZ20
22240 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ)"20",
22241#endif
22242#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
22243 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS)"0755",
22244#endif
22245#ifdef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
22246 "DEFAULT_RECURSIVE_TRIGGERS",
22247#endif
22248#ifdef SQLITE_DEFAULT_ROWEST
22249 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST)"SQLITE_DEFAULT_ROWEST",
22250#endif
22251#ifdef SQLITE_DEFAULT_SECTOR_SIZE4096
22252 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE)"4096",
22253#endif
22254#ifdef SQLITE_DEFAULT_SYNCHRONOUS2
22255 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS)"2",
22256#endif
22257#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
22258 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT)"1000",
22259#endif
22260#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS1
22261 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS)"1",
22262#endif
22263#ifdef SQLITE_DEFAULT_WORKER_THREADS0
22264 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS)"0",
22265#endif
22266#ifdef SQLITE_DIRECT_OVERFLOW_READ1
22267 "DIRECT_OVERFLOW_READ",
22268#endif
22269#ifdef SQLITE_DISABLE_DIRSYNC
22270 "DISABLE_DIRSYNC",
22271#endif
22272#ifdef SQLITE_DISABLE_FTS3_UNICODE
22273 "DISABLE_FTS3_UNICODE",
22274#endif
22275#ifdef SQLITE_DISABLE_FTS4_DEFERRED
22276 "DISABLE_FTS4_DEFERRED",
22277#endif
22278#ifdef SQLITE_DISABLE_INTRINSIC
22279 "DISABLE_INTRINSIC",
22280#endif
22281#ifdef SQLITE_DISABLE_LFS
22282 "DISABLE_LFS",
22283#endif
22284#ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
22285 "DISABLE_PAGECACHE_OVERFLOW_STATS",
22286#endif
22287#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
22288 "DISABLE_SKIPAHEAD_DISTINCT",
22289#endif
22290#ifdef SQLITE_DQS0
22291 "DQS=" CTIMEOPT_VAL(SQLITE_DQS)"0",
22292#endif
22293#ifdef SQLITE_ENABLE_8_3_NAMES
22294 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES)"SQLITE_ENABLE_8_3_NAMES",
22295#endif
22296#ifdef SQLITE_ENABLE_API_ARMOR1
22297 "ENABLE_API_ARMOR",
22298#endif
22299#ifdef SQLITE_ENABLE_ATOMIC_WRITE
22300 "ENABLE_ATOMIC_WRITE",
22301#endif
22302#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
22303 "ENABLE_BATCH_ATOMIC_WRITE",
22304#endif
22305#ifdef SQLITE_ENABLE_BYTECODE_VTAB
22306 "ENABLE_BYTECODE_VTAB",
22307#endif
22308#ifdef SQLITE_ENABLE_CEROD
22309 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD)"SQLITE_ENABLE_CEROD",
22310#endif
22311#ifdef SQLITE_ENABLE_COLUMN_METADATA1
22312 "ENABLE_COLUMN_METADATA",
22313#endif
22314#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
22315 "ENABLE_COLUMN_USED_MASK",
22316#endif
22317#ifdef SQLITE_ENABLE_COSTMULT
22318 "ENABLE_COSTMULT",
22319#endif
22320#ifdef SQLITE_ENABLE_CURSOR_HINTS
22321 "ENABLE_CURSOR_HINTS",
22322#endif
22323#ifdef SQLITE_ENABLE_DBPAGE_VTAB
22324 "ENABLE_DBPAGE_VTAB",
22325#endif
22326#ifdef SQLITE_ENABLE_DBSTAT_VTAB1
22327 "ENABLE_DBSTAT_VTAB",
22328#endif
22329#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
22330 "ENABLE_EXPENSIVE_ASSERT",
22331#endif
22332#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
22333 "ENABLE_EXPLAIN_COMMENTS",
22334#endif
22335#ifdef SQLITE_ENABLE_FTS31
22336 "ENABLE_FTS3",
22337#endif
22338#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS1
22339 "ENABLE_FTS3_PARENTHESIS",
22340#endif
22341#ifdef SQLITE_ENABLE_FTS3_TOKENIZER1
22342 "ENABLE_FTS3_TOKENIZER",
22343#endif
22344#ifdef SQLITE_ENABLE_FTS41
22345 "ENABLE_FTS4",
22346#endif
22347#ifdef SQLITE_ENABLE_FTS51
22348 "ENABLE_FTS5",
22349#endif
22350#ifdef SQLITE_ENABLE_GEOPOLY
22351 "ENABLE_GEOPOLY",
22352#endif
22353#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
22354 "ENABLE_HIDDEN_COLUMNS",
22355#endif
22356#ifdef SQLITE_ENABLE_ICU
22357 "ENABLE_ICU",
22358#endif
22359#ifdef SQLITE_ENABLE_IOTRACE
22360 "ENABLE_IOTRACE",
22361#endif
22362#ifdef SQLITE_ENABLE_LOAD_EXTENSION
22363 "ENABLE_LOAD_EXTENSION",
22364#endif
22365#ifdef SQLITE_ENABLE_LOCKING_STYLE0
22366 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE)"0",
22367#endif
22368#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
22369 "ENABLE_MATH_FUNCTIONS",
22370#endif
22371#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
22372 "ENABLE_MEMORY_MANAGEMENT",
22373#endif
22374#ifdef SQLITE_ENABLE_MEMSYS3
22375 "ENABLE_MEMSYS3",
22376#endif
22377#ifdef SQLITE_ENABLE_MEMSYS5
22378 "ENABLE_MEMSYS5",
22379#endif
22380#ifdef SQLITE_ENABLE_MULTIPLEX
22381 "ENABLE_MULTIPLEX",
22382#endif
22383#ifdef SQLITE_ENABLE_NORMALIZE
22384 "ENABLE_NORMALIZE",
22385#endif
22386#ifdef SQLITE_ENABLE_NULL_TRIM
22387 "ENABLE_NULL_TRIM",
22388#endif
22389#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
22390 "ENABLE_OFFSET_SQL_FUNC",
22391#endif
22392#ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES
22393 "ENABLE_ORDERED_SET_AGGREGATES",
22394#endif
22395#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
22396 "ENABLE_OVERSIZE_CELL_CHECK",
22397#endif
22398#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
22399 "ENABLE_PREUPDATE_HOOK",
22400#endif
22401#ifdef SQLITE_ENABLE_QPSG
22402 "ENABLE_QPSG",
22403#endif
22404#ifdef SQLITE_ENABLE_RBU
22405 "ENABLE_RBU",
22406#endif
22407#ifdef SQLITE_ENABLE_RTREE1
22408 "ENABLE_RTREE",
22409#endif
22410#ifdef SQLITE_ENABLE_SESSION1
22411 "ENABLE_SESSION",
22412#endif
22413#ifdef SQLITE_ENABLE_SNAPSHOT1
22414 "ENABLE_SNAPSHOT",
22415#endif
22416#ifdef SQLITE_ENABLE_SORTER_REFERENCES
22417 "ENABLE_SORTER_REFERENCES",
22418#endif
22419#ifdef SQLITE_ENABLE_SQLLOG
22420 "ENABLE_SQLLOG",
22421#endif
22422#ifdef SQLITE_ENABLE_STAT4
22423 "ENABLE_STAT4",
22424#endif
22425#ifdef SQLITE_ENABLE_STMTVTAB
22426 "ENABLE_STMTVTAB",
22427#endif
22428#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
22429 "ENABLE_STMT_SCANSTATUS",
22430#endif
22431#ifdef SQLITE_ENABLE_TREETRACE
22432 "ENABLE_TREETRACE",
22433#endif
22434#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
22435 "ENABLE_UNKNOWN_SQL_FUNCTION",
22436#endif
22437#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
22438 "ENABLE_UNLOCK_NOTIFY",
22439#endif
22440#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
22441 "ENABLE_UPDATE_DELETE_LIMIT",
22442#endif
22443#ifdef SQLITE_ENABLE_URI_00_ERROR
22444 "ENABLE_URI_00_ERROR",
22445#endif
22446#ifdef SQLITE_ENABLE_VFSTRACE
22447 "ENABLE_VFSTRACE",
22448#endif
22449#ifdef SQLITE_ENABLE_WHERETRACE
22450 "ENABLE_WHERETRACE",
22451#endif
22452#ifdef SQLITE_ENABLE_ZIPVFS
22453 "ENABLE_ZIPVFS",
22454#endif
22455#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
22456 "EXPLAIN_ESTIMATED_ROWS",
22457#endif
22458#ifdef SQLITE_EXTRA_AUTOEXT
22459 "EXTRA_AUTOEXT=" CTIMEOPT_VAL(SQLITE_EXTRA_AUTOEXT)"SQLITE_EXTRA_AUTOEXT",
22460#endif
22461#ifdef SQLITE_EXTRA_IFNULLROW
22462 "EXTRA_IFNULLROW",
22463#endif
22464#ifdef SQLITE_EXTRA_INIT
22465 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT)"SQLITE_EXTRA_INIT",
22466#endif
22467#ifdef SQLITE_EXTRA_SHUTDOWN
22468 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN)"SQLITE_EXTRA_SHUTDOWN",
22469#endif
22470#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH12
22471 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH)"12",
22472#endif
22473#ifdef SQLITE_FTS5_ENABLE_TEST_MI
22474 "FTS5_ENABLE_TEST_MI",
22475#endif
22476#ifdef SQLITE_FTS5_NO_WITHOUT_ROWID
22477 "FTS5_NO_WITHOUT_ROWID",
22478#endif
22479#if HAVE_ISNAN || SQLITE_HAVE_ISNAN1
22480 "HAVE_ISNAN",
22481#endif
22482#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
22483# if SQLITE_HOMEGROWN_RECURSIVE_MUTEX != 1
22484 "HOMEGROWN_RECURSIVE_MUTEX=" CTIMEOPT_VAL(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)"SQLITE_HOMEGROWN_RECURSIVE_MUTEX",
22485# endif
22486#endif
22487#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
22488 "IGNORE_AFP_LOCK_ERRORS",
22489#endif
22490#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
22491 "IGNORE_FLOCK_LOCK_ERRORS",
22492#endif
22493#ifdef SQLITE_INLINE_MEMCPY
22494 "INLINE_MEMCPY",
22495#endif
22496#ifdef SQLITE_INT64_TYPE
22497 "INT64_TYPE",
22498#endif
22499#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
22500 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX)"100",
22501#endif
22502#ifdef SQLITE_LEGACY_JSON_VALID
22503 "LEGACY_JSON_VALID",
22504#endif
22505#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
22506 "LIKE_DOESNT_MATCH_BLOBS",
22507#endif
22508#ifdef SQLITE_LOCK_TRACE
22509 "LOCK_TRACE",
22510#endif
22511#ifdef SQLITE_LOG_CACHE_SPILL
22512 "LOG_CACHE_SPILL",
22513#endif
22514#ifdef SQLITE_MALLOC_SOFT_LIMIT1024
22515 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT)"1024",
22516#endif
22517#ifdef SQLITE_MAX_ATTACHED10
22518 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED)"10",
22519#endif
22520#ifdef SQLITE_MAX_COLUMN2000
22521 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN)"2000",
22522#endif
22523#ifdef SQLITE_MAX_COMPOUND_SELECT500
22524 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT)"500",
22525#endif
22526#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
22527 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE)"8192",
22528#endif
22529#ifdef SQLITE_MAX_EXPR_DEPTH1000
22530 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH)"1000",
22531#endif
22532#ifdef SQLITE_MAX_FUNCTION_ARG127
22533 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG)"127",
22534#endif
22535#ifdef SQLITE_MAX_LENGTH2147483645
22536 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH)"2147483645",
22537#endif
22538#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
22539 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH)"50000",
22540#endif
22541#ifdef SQLITE_MAX_MEMORY0
22542 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY)"0",
22543#endif
22544#ifdef SQLITE_MAX_MMAP_SIZE20971520
22545 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE)"20971520",
22546#endif
22547#ifdef SQLITE_MAX_MMAP_SIZE_
22548 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_)"SQLITE_MAX_MMAP_SIZE_",
22549#endif
22550#ifdef SQLITE_MAX_PAGE_COUNT0xfffffffe
22551 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT)"0xfffffffe",
22552#endif
22553#ifdef SQLITE_MAX_PAGE_SIZE65536
22554 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE)"65536",
22555#endif
22556#ifdef SQLITE_MAX_SCHEMA_RETRY50
22557 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY)"50",
22558#endif
22559#ifdef SQLITE_MAX_SQL_LENGTH1000000000
22560 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH)"1000000000",
22561#endif
22562#ifdef SQLITE_MAX_TRIGGER_DEPTH1000
22563 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH)"1000",
22564#endif
22565#ifdef SQLITE_MAX_VARIABLE_NUMBER500000
22566 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER)"500000",
22567#endif
22568#ifdef SQLITE_MAX_VDBE_OP250000000
22569 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP)"250000000",
22570#endif
22571#ifdef SQLITE_MAX_WORKER_THREADS8
22572 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS)"8",
22573#endif
22574#ifdef SQLITE_MEMDEBUG
22575 "MEMDEBUG",
22576#endif
22577#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
22578 "MIXED_ENDIAN_64BIT_FLOAT",
22579#endif
22580#ifdef SQLITE_MMAP_READWRITE
22581 "MMAP_READWRITE",
22582#endif
22583#ifdef SQLITE_MUTEX_NOOP
22584 "MUTEX_NOOP",
22585#endif
22586#ifdef SQLITE_MUTEX_OMIT
22587 "MUTEX_OMIT",
22588#endif
22589#ifdef SQLITE_MUTEX_PTHREADS
22590 "MUTEX_PTHREADS",
22591#endif
22592#ifdef SQLITE_MUTEX_W32
22593 "MUTEX_W32",
22594#endif
22595#ifdef SQLITE_NEED_ERR_NAME
22596 "NEED_ERR_NAME",
22597#endif
22598#ifdef SQLITE_NO_SYNC
22599 "NO_SYNC",
22600#endif
22601#ifdef SQLITE_OMIT_ALTERTABLE
22602 "OMIT_ALTERTABLE",
22603#endif
22604#ifdef SQLITE_OMIT_ANALYZE
22605 "OMIT_ANALYZE",
22606#endif
22607#ifdef SQLITE_OMIT_ATTACH
22608 "OMIT_ATTACH",
22609#endif
22610#ifdef SQLITE_OMIT_AUTHORIZATION
22611 "OMIT_AUTHORIZATION",
22612#endif
22613#ifdef SQLITE_OMIT_AUTOINCREMENT
22614 "OMIT_AUTOINCREMENT",
22615#endif
22616#ifdef SQLITE_OMIT_AUTOINIT
22617 "OMIT_AUTOINIT",
22618#endif
22619#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
22620 "OMIT_AUTOMATIC_INDEX",
22621#endif
22622#ifdef SQLITE_OMIT_AUTORESET1
22623 "OMIT_AUTORESET",
22624#endif
22625#ifdef SQLITE_OMIT_AUTOVACUUM
22626 "OMIT_AUTOVACUUM",
22627#endif
22628#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
22629 "OMIT_BETWEEN_OPTIMIZATION",
22630#endif
22631#ifdef SQLITE_OMIT_BLOB_LITERAL
22632 "OMIT_BLOB_LITERAL",
22633#endif
22634#ifdef SQLITE_OMIT_CAST
22635 "OMIT_CAST",
22636#endif
22637#ifdef SQLITE_OMIT_CHECK
22638 "OMIT_CHECK",
22639#endif
22640#ifdef SQLITE_OMIT_COMPLETE
22641 "OMIT_COMPLETE",
22642#endif
22643#ifdef SQLITE_OMIT_COMPOUND_SELECT
22644 "OMIT_COMPOUND_SELECT",
22645#endif
22646#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
22647 "OMIT_CONFLICT_CLAUSE",
22648#endif
22649#ifdef SQLITE_OMIT_CTE
22650 "OMIT_CTE",
22651#endif
22652#if defined(SQLITE_OMIT_DATETIME_FUNCS) || defined(SQLITE_OMIT_FLOATING_POINT)
22653 "OMIT_DATETIME_FUNCS",
22654#endif
22655#ifdef SQLITE_OMIT_DECLTYPE
22656 "OMIT_DECLTYPE",
22657#endif
22658#ifdef SQLITE_OMIT_DEPRECATED1
22659 "OMIT_DEPRECATED",
22660#endif
22661#ifdef SQLITE_OMIT_DESERIALIZE
22662 "OMIT_DESERIALIZE",
22663#endif
22664#ifdef SQLITE_OMIT_DISKIO
22665 "OMIT_DISKIO",
22666#endif
22667#ifdef SQLITE_OMIT_EXPLAIN
22668 "OMIT_EXPLAIN",
22669#endif
22670#ifdef SQLITE_OMIT_FLAG_PRAGMAS
22671 "OMIT_FLAG_PRAGMAS",
22672#endif
22673#ifdef SQLITE_OMIT_FLOATING_POINT
22674 "OMIT_FLOATING_POINT",
22675#endif
22676#ifdef SQLITE_OMIT_FOREIGN_KEY
22677 "OMIT_FOREIGN_KEY",
22678#endif
22679#ifdef SQLITE_OMIT_GET_TABLE
22680 "OMIT_GET_TABLE",
22681#endif
22682#ifdef SQLITE_OMIT_HEX_INTEGER
22683 "OMIT_HEX_INTEGER",
22684#endif
22685#ifdef SQLITE_OMIT_INCRBLOB
22686 "OMIT_INCRBLOB",
22687#endif
22688#ifdef SQLITE_OMIT_INTEGRITY_CHECK
22689 "OMIT_INTEGRITY_CHECK",
22690#endif
22691#ifdef SQLITE_OMIT_INTROSPECTION_PRAGMAS
22692 "OMIT_INTROSPECTION_PRAGMAS",
22693#endif
22694#ifdef SQLITE_OMIT_JSON
22695 "OMIT_JSON",
22696#endif
22697#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
22698 "OMIT_LIKE_OPTIMIZATION",
22699#endif
22700#ifdef SQLITE_OMIT_LOAD_EXTENSION1
22701 "OMIT_LOAD_EXTENSION",
22702#endif
22703#ifdef SQLITE_OMIT_LOCALTIME
22704 "OMIT_LOCALTIME",
22705#endif
22706#ifdef SQLITE_OMIT_LOOKASIDE
22707 "OMIT_LOOKASIDE",
22708#endif
22709#ifdef SQLITE_OMIT_MEMORYDB
22710 "OMIT_MEMORYDB",
22711#endif
22712#ifdef SQLITE_OMIT_OR_OPTIMIZATION
22713 "OMIT_OR_OPTIMIZATION",
22714#endif
22715#ifdef SQLITE_OMIT_PAGER_PRAGMAS
22716 "OMIT_PAGER_PRAGMAS",
22717#endif
22718#ifdef SQLITE_OMIT_PARSER_TRACE
22719 "OMIT_PARSER_TRACE",
22720#endif
22721#ifdef SQLITE_OMIT_POPEN
22722 "OMIT_POPEN",
22723#endif
22724#ifdef SQLITE_OMIT_PRAGMA
22725 "OMIT_PRAGMA",
22726#endif
22727#ifdef SQLITE_OMIT_PROGRESS_CALLBACK1
22728 "OMIT_PROGRESS_CALLBACK",
22729#endif
22730#ifdef SQLITE_OMIT_QUICKBALANCE
22731 "OMIT_QUICKBALANCE",
22732#endif
22733#ifdef SQLITE_OMIT_REINDEX
22734 "OMIT_REINDEX",
22735#endif
22736#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
22737 "OMIT_SCHEMA_PRAGMAS",
22738#endif
22739#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
22740 "OMIT_SCHEMA_VERSION_PRAGMAS",
22741#endif
22742#ifdef SQLITE_OMIT_SEH
22743 "OMIT_SEH",
22744#endif
22745#ifdef SQLITE_OMIT_SHARED_CACHE
22746 "OMIT_SHARED_CACHE",
22747#endif
22748#ifdef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
22749 "OMIT_SHUTDOWN_DIRECTORIES",
22750#endif
22751#ifdef SQLITE_OMIT_SUBQUERY
22752 "OMIT_SUBQUERY",
22753#endif
22754#ifdef SQLITE_OMIT_TCL_VARIABLE
22755 "OMIT_TCL_VARIABLE",
22756#endif
22757#ifdef SQLITE_OMIT_TEMPDB
22758 "OMIT_TEMPDB",
22759#endif
22760#ifdef SQLITE_OMIT_TEST_CONTROL
22761 "OMIT_TEST_CONTROL",
22762#endif
22763#ifdef SQLITE_OMIT_TRACE
22764# if SQLITE_OMIT_TRACE != 1
22765 "OMIT_TRACE=" CTIMEOPT_VAL(SQLITE_OMIT_TRACE)"SQLITE_OMIT_TRACE",
22766# endif
22767#endif
22768#ifdef SQLITE_OMIT_TRIGGER
22769 "OMIT_TRIGGER",
22770#endif
22771#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
22772 "OMIT_TRUNCATE_OPTIMIZATION",
22773#endif
22774#ifdef SQLITE_OMIT_UTF16
22775 "OMIT_UTF16",
22776#endif
22777#ifdef SQLITE_OMIT_VACUUM
22778 "OMIT_VACUUM",
22779#endif
22780#ifdef SQLITE_OMIT_VIEW
22781 "OMIT_VIEW",
22782#endif
22783#ifdef SQLITE_OMIT_VIRTUALTABLE
22784 "OMIT_VIRTUALTABLE",
22785#endif
22786#ifdef SQLITE_OMIT_WAL
22787 "OMIT_WAL",
22788#endif
22789#ifdef SQLITE_OMIT_WSD
22790 "OMIT_WSD",
22791#endif
22792#ifdef SQLITE_OMIT_XFER_OPT
22793 "OMIT_XFER_OPT",
22794#endif
22795#ifdef SQLITE_PERFORMANCE_TRACE
22796 "PERFORMANCE_TRACE",
22797#endif
22798#ifdef SQLITE_POWERSAFE_OVERWRITE1
22799# if SQLITE_POWERSAFE_OVERWRITE1 != 1
22800 "POWERSAFE_OVERWRITE=" CTIMEOPT_VAL(SQLITE_POWERSAFE_OVERWRITE)"1",
22801# endif
22802#endif
22803#ifdef SQLITE_PREFER_PROXY_LOCKING
22804 "PREFER_PROXY_LOCKING",
22805#endif
22806#ifdef SQLITE_PROXY_DEBUG
22807 "PROXY_DEBUG",
22808#endif
22809#ifdef SQLITE_REVERSE_UNORDERED_SELECTS
22810 "REVERSE_UNORDERED_SELECTS",
22811#endif
22812#ifdef SQLITE_RTREE_INT_ONLY
22813 "RTREE_INT_ONLY",
22814#endif
22815#ifdef SQLITE_SECURE_DELETE
22816 "SECURE_DELETE",
22817#endif
22818#ifdef SQLITE_SMALL_STACK
22819 "SMALL_STACK",
22820#endif
22821#ifdef SQLITE_SORTER_PMASZ250
22822 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ)"250",
22823#endif
22824#ifdef SQLITE_SOUNDEX
22825 "SOUNDEX",
22826#endif
22827#ifdef SQLITE_STAT4_SAMPLES1
22828 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES)"1",
22829#endif
22830#ifdef SQLITE_STMTJRNL_SPILL131072
22831 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL)"131072",
22832#endif
22833#ifdef SQLITE_SUBSTR_COMPATIBILITY1
22834 "SUBSTR_COMPATIBILITY",
22835#endif
22836#if (!defined(SQLITE_WIN32_MALLOC) \
22837 && !defined(SQLITE_ZERO_MALLOC) \
22838 && !defined(SQLITE_MEMDEBUG) \
22839 ) || defined(SQLITE_SYSTEM_MALLOC1)
22840 "SYSTEM_MALLOC",
22841#endif
22842#ifdef SQLITE_TCL
22843 "TCL",
22844#endif
22845#ifdef SQLITE_TEMP_STORE1
22846 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE)"1",
22847#endif
22848#ifdef SQLITE_TEST
22849 "TEST",
22850#endif
22851#if defined(SQLITE_THREADSAFE2)
22852 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE)"2",
22853#elif defined(THREADSAFE)
22854 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE)"THREADSAFE",
22855#else
22856 "THREADSAFE=1",
22857#endif
22858#ifdef SQLITE_UNLINK_AFTER_CLOSE
22859 "UNLINK_AFTER_CLOSE",
22860#endif
22861#ifdef SQLITE_UNTESTABLE
22862 "UNTESTABLE",
22863#endif
22864#ifdef SQLITE_USER_AUTHENTICATION
22865 "USER_AUTHENTICATION",
22866#endif
22867#ifdef SQLITE_USE_ALLOCA
22868 "USE_ALLOCA",
22869#endif
22870#ifdef SQLITE_USE_FCNTL_TRACE
22871 "USE_FCNTL_TRACE",
22872#endif
22873#ifdef SQLITE_USE_URI1
22874 "USE_URI",
22875#endif
22876#ifdef SQLITE_VDBE_COVERAGE
22877 "VDBE_COVERAGE",
22878#endif
22879#ifdef SQLITE_WIN32_MALLOC
22880 "WIN32_MALLOC",
22881#endif
22882#ifdef SQLITE_ZERO_MALLOC
22883 "ZERO_MALLOC",
22884#endif
22885
22886} ;
22887
22888SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt){
22889 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
22890 return (const char**)sqlite3azCompileOpt;
22891}
22892
22893#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
22894
22895/************** End of ctime.c ***********************************************/
22896/************** Begin file global.c ******************************************/
22897/*
22898** 2008 June 13
22899**
22900** The author disclaims copyright to this source code. In place of
22901** a legal notice, here is a blessing:
22902**
22903** May you do good and not evil.
22904** May you find forgiveness for yourself and forgive others.
22905** May you share freely, never taking more than you give.
22906**
22907*************************************************************************
22908**
22909** This file contains definitions of global variables and constants.
22910*/
22911/* #include "sqliteInt.h" */
22912
22913/* An array to map all upper-case characters into their corresponding
22914** lower-case character.
22915**
22916** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22917** handle case conversions for the UTF character set since the tables
22918** involved are nearly as big or bigger than SQLite itself.
22919*/
22920SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[] = {
22921#ifdef SQLITE_ASCII1
22922 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
22923 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
22924 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
22925 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
22926 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
22927 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
22928 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
22929 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
22930 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
22931 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
22932 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
22933 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
22934 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
22935 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
22936 252,253,254,255,
22937#endif
22938#ifdef SQLITE_EBCDIC
22939 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
22940 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
22941 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
22942 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
22943 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
22944 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
22945 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
22946 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
22947 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
22948 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
22949 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
22950 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
22951 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
22952 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
22953 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
22954 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
22955#endif
22956/* All of the upper-to-lower conversion data is above. The following
22957** 18 integers are completely unrelated. They are appended to the
22958** sqlite3UpperToLower[] array to avoid UBSAN warnings. Here's what is
22959** going on:
22960**
22961** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented
22962** by invoking sqlite3MemCompare(A,B) which compares values A and B and
22963** returns negative, zero, or positive if A is less then, equal to, or
22964** greater than B, respectively. Then the true false results is found by
22965** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or
22966** sqlite3aGTb[opcode] depending on whether the result of compare(A,B)
22967** is negative, zero, or positive, where opcode is the specific opcode.
22968** The only works because the comparison opcodes are consecutive and in
22969** this order: NE EQ GT LE LT GE. Various assert()s throughout the code
22970** ensure that is the case.
22971**
22972** These elements must be appended to another array. Otherwise the
22973** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22974** be undefined behavior. That's goofy, but the C-standards people thought
22975** it was a good idea, so here we are.
22976*/
22977/* NE EQ GT LE LT GE */
22978 1, 0, 0, 1, 1, 0, /* aLTb[]: Use when compare(A,B) less than zero */
22979 0, 1, 0, 1, 0, 1, /* aEQb[]: Use when compare(A,B) equals zero */
22980 1, 0, 1, 0, 0, 1 /* aGTb[]: Use when compare(A,B) greater than zero*/
22981};
22982SQLITE_PRIVATEstatic const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne53];
22983SQLITE_PRIVATEstatic const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne53];
22984SQLITE_PRIVATEstatic const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne53];
22985
22986/*
22987** The following 256 byte lookup table is used to support SQLites built-in
22988** equivalents to the following standard library functions:
22989**
22990** isspace() 0x01
22991** isalpha() 0x02
22992** isdigit() 0x04
22993** isalnum() 0x06
22994** isxdigit() 0x08
22995** toupper() 0x20
22996** SQLite identifier character 0x40 $, _, or non-ascii
22997** Quote character 0x80
22998**
22999** Bit 0x20 is set if the mapped character requires translation to upper
23000** case. i.e. if the character is a lower-case ASCII character.
23001** If x is a lower-case ASCII character, then its upper-case equivalent
23002** is (x - 0x20). Therefore toupper() can be implemented as:
23003**
23004** (x & ~(map[x]&0x20))
23005**
23006** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
23007** array. tolower() is used more often than toupper() by SQLite.
23008**
23009** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
23010** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
23011** non-ASCII UTF character. Hence the test for whether or not a character is
23012** part of an identifier is 0x46.
23013*/
23014SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[256] = {
23015 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
23016 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
23017 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
23018 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
23019 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
23020 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
23021 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
23022 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
23023
23024 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
23025 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
23026 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
23027 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
23028 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
23029 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
23030 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
23031 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
23032
23033 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
23034 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
23035 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
23036 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
23037 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
23038 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
23039 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
23040 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
23041
23042 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
23043 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
23044 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
23045 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
23046 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
23047 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
23048 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
23049 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
23050};
23051
23052/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
23053** compatibility for legacy applications, the URI filename capability is
23054** disabled by default.
23055**
23056** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
23057** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
23058**
23059** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
23060** disabled. The default value may be changed by compiling with the
23061** SQLITE_USE_URI symbol defined.
23062*/
23063#ifndef SQLITE_USE_URI1
23064# define SQLITE_USE_URI1 0
23065#endif
23066
23067/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
23068** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
23069** that compile-time option is omitted.
23070*/
23071#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN1)
23072# define SQLITE_ALLOW_COVERING_INDEX_SCAN1 1
23073#else
23074# if !SQLITE_ALLOW_COVERING_INDEX_SCAN1
23075# error "Compile-time disabling of covering index scan using the\
23076 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
23077 Contact SQLite developers if this is a problem for you, and\
23078 delete this #error macro to continue with your build."
23079# endif
23080#endif
23081
23082/* The minimum PMA size is set to this value multiplied by the database
23083** page size in bytes.
23084*/
23085#ifndef SQLITE_SORTER_PMASZ250
23086# define SQLITE_SORTER_PMASZ250 250
23087#endif
23088
23089/* Statement journals spill to disk when their size exceeds the following
23090** threshold (in bytes). 0 means that statement journals are created and
23091** written to disk immediately (the default behavior for SQLite versions
23092** before 3.12.0). -1 means always keep the entire statement journal in
23093** memory. (The statement journal is also always held entirely in memory
23094** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
23095** setting.)
23096*/
23097#ifndef SQLITE_STMTJRNL_SPILL131072
23098# define SQLITE_STMTJRNL_SPILL131072 (64*1024)
23099#endif
23100
23101/*
23102** The default lookaside-configuration, the format "SZ,N". SZ is the
23103** number of bytes in each lookaside slot (should be a multiple of 8)
23104** and N is the number of slots. The lookaside-configuration can be
23105** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
23106** or at run-time for an individual database connection using
23107** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
23108**
23109** With the two-size-lookaside enhancement, less lookaside is required.
23110** The default configuration of 1200,40 actually provides 30 1200-byte slots
23111** and 93 128-byte slots, which is more lookaside than is available
23112** using the older 1200,100 configuration without two-size-lookaside.
23113*/
23114#ifndef SQLITE_DEFAULT_LOOKASIDE1200,40
23115# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
23116# define SQLITE_DEFAULT_LOOKASIDE1200,40 1200,100 /* 120KB of memory */
23117# else
23118# define SQLITE_DEFAULT_LOOKASIDE1200,40 1200,40 /* 48KB of memory */
23119# endif
23120#endif
23121
23122
23123/* The default maximum size of an in-memory database created using
23124** sqlite3_deserialize()
23125*/
23126#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824
23127# define SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824 1073741824
23128#endif
23129
23130/*
23131** The following singleton contains the global configuration for
23132** the SQLite library.
23133*/
23134SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config = {
23135 SQLITE_DEFAULT_MEMSTATUS1, /* bMemstat */
23136 1, /* bCoreMutex */
23137 SQLITE_THREADSAFE2==1, /* bFullMutex */
23138 SQLITE_USE_URI1, /* bOpenUri */
23139 SQLITE_ALLOW_COVERING_INDEX_SCAN1, /* bUseCis */
23140 0, /* bSmallMalloc */
23141 1, /* bExtraSchemaChecks */
23142#ifdef SQLITE_DEBUG
23143 0, /* bJsonSelfcheck */
23144#endif
23145 0x7ffffffe, /* mxStrlen */
23146 0, /* neverCorrupt */
23147 SQLITE_DEFAULT_LOOKASIDE1200,40, /* szLookaside, nLookaside */
23148 SQLITE_STMTJRNL_SPILL131072, /* nStmtSpill */
23149 {0,0,0,0,0,0,0,0}, /* m */
23150 {0,0,0,0,0,0,0,0,0}, /* mutex */
23151 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
23152 (void*)0, /* pHeap */
23153 0, /* nHeap */
23154 0, 0, /* mnHeap, mxHeap */
23155 SQLITE_DEFAULT_MMAP_SIZE0, /* szMmap */
23156 SQLITE_MAX_MMAP_SIZE20971520, /* mxMmap */
23157 (void*)0, /* pPage */
23158 0, /* szPage */
23159 SQLITE_DEFAULT_PCACHE_INITSZ20, /* nPage */
23160 0, /* mxParserStack */
23161 0, /* sharedCacheEnabled */
23162 SQLITE_SORTER_PMASZ250, /* szPma */
23163 /* All the rest should always be initialized to zero */
23164 0, /* isInit */
23165 0, /* inProgress */
23166 0, /* isMutexInit */
23167 0, /* isMallocInit */
23168 0, /* isPCacheInit */
23169 0, /* nRefInitMutex */
23170 0, /* pInitMutex */
23171 0, /* xLog */
23172 0, /* pLogArg */
23173#ifdef SQLITE_ENABLE_SQLLOG
23174 0, /* xSqllog */
23175 0, /* pSqllogArg */
23176#endif
23177#ifdef SQLITE_VDBE_COVERAGE
23178 0, /* xVdbeBranch */
23179 0, /* pVbeBranchArg */
23180#endif
23181#ifndef SQLITE_OMIT_DESERIALIZE
23182 SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824, /* mxMemdbSize */
23183#endif
23184#ifndef SQLITE_UNTESTABLE
23185 0, /* xTestCallback */
23186#endif
23187#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
23188 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
23189#endif
23190 0, /* bLocaltimeFault */
23191 0, /* xAltLocaltime */
23192 0x7ffffffe, /* iOnceResetThreshold */
23193 SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff, /* szSorterRef */
23194 0, /* iPrngSeed */
23195#ifdef SQLITE_DEBUG
23196 {0,0,0,0,0,0}, /* aTune */
23197#endif
23198};
23199
23200/*
23201** Hash table for global functions - functions common to all
23202** database connections. After initialization, this table is
23203** read-only.
23204*/
23205SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
23206
23207#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
23208/*
23209** Counter used for coverage testing. Does not come into play for
23210** release builds.
23211**
23212** Access to this global variable is not mutex protected. This might
23213** result in TSAN warnings. But as the variable does not exist in
23214** release builds, that should not be a concern.
23215*/
23216SQLITE_PRIVATEstatic unsigned int sqlite3CoverageCounter;
23217#endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */
23218
23219#ifdef VDBE_PROFILE
23220/*
23221** The following performance counter can be used in place of
23222** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
23223*/
23224SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt = 0;
23225#endif
23226
23227/*
23228** The value of the "pending" byte must be 0x40000000 (1 byte past the
23229** 1-gibabyte boundary) in a compatible database. SQLite never uses
23230** the database page that contains the pending byte. It never attempts
23231** to read or write that page. The pending byte page is set aside
23232** for use by the VFS layers as space for managing file locks.
23233**
23234** During testing, it is often desirable to move the pending byte to
23235** a different position in the file. This allows code that has to
23236** deal with the pending byte to run on files that are much smaller
23237** than 1 GiB. The sqlite3_test_control() interface can be used to
23238** move the pending byte.
23239**
23240** IMPORTANT: Changing the pending byte to any value other than
23241** 0x40000000 results in an incompatible database file format!
23242** Changing the pending byte during operation will result in undefined
23243** and incorrect behavior.
23244*/
23245#ifndef SQLITE_OMIT_WSD
23246SQLITE_PRIVATEstatic int sqlite3PendingByte = 0x40000000;
23247#endif
23248
23249/*
23250** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS.
23251*/
23252SQLITE_PRIVATEstatic u32 sqlite3TreeTrace = 0;
23253SQLITE_PRIVATEstatic u32 sqlite3WhereTrace = 0;
23254
23255/* #include "opcodes.h" */
23256/*
23257** Properties of opcodes. The OPFLG_INITIALIZER macro is
23258** created by mkopcodeh.awk during compilation. Data is obtained
23259** from the comments following the "case OP_xxxx:" statements in
23260** the vdbe.c file.
23261*/
23262SQLITE_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,}
;
23263
23264/*
23265** Name of the default collating sequence
23266*/
23267SQLITE_PRIVATEstatic const char sqlite3StrBINARY[] = "BINARY";
23268
23269/*
23270** Standard typenames. These names must match the COLTYPE_* definitions.
23271** Adjust the SQLITE_N_STDTYPE value if adding or removing entries.
23272**
23273** sqlite3StdType[] The actual names of the datatypes.
23274**
23275** sqlite3StdTypeLen[] The length (in bytes) of each entry
23276** in sqlite3StdType[].
23277**
23278** sqlite3StdTypeAffinity[] The affinity associated with each entry
23279** in sqlite3StdType[].
23280*/
23281SQLITE_PRIVATEstatic const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
23282SQLITE_PRIVATEstatic const char sqlite3StdTypeAffinity[] = {
23283 SQLITE_AFF_NUMERIC0x43,
23284 SQLITE_AFF_BLOB0x41,
23285 SQLITE_AFF_INTEGER0x44,
23286 SQLITE_AFF_INTEGER0x44,
23287 SQLITE_AFF_REAL0x45,
23288 SQLITE_AFF_TEXT0x42
23289};
23290SQLITE_PRIVATEstatic const char *sqlite3StdType[] = {
23291 "ANY",
23292 "BLOB",
23293 "INT",
23294 "INTEGER",
23295 "REAL",
23296 "TEXT"
23297};
23298
23299/************** End of global.c **********************************************/
23300/************** Begin file status.c ******************************************/
23301/*
23302** 2008 June 18
23303**
23304** The author disclaims copyright to this source code. In place of
23305** a legal notice, here is a blessing:
23306**
23307** May you do good and not evil.
23308** May you find forgiveness for yourself and forgive others.
23309** May you share freely, never taking more than you give.
23310**
23311*************************************************************************
23312**
23313** This module implements the sqlite3_status() interface and related
23314** functionality.
23315*/
23316/* #include "sqliteInt.h" */
23317/************** Include vdbeInt.h in the middle of status.c ******************/
23318/************** Begin file vdbeInt.h *****************************************/
23319/*
23320** 2003 September 6
23321**
23322** The author disclaims copyright to this source code. In place of
23323** a legal notice, here is a blessing:
23324**
23325** May you do good and not evil.
23326** May you find forgiveness for yourself and forgive others.
23327** May you share freely, never taking more than you give.
23328**
23329*************************************************************************
23330** This is the header file for information that is private to the
23331** VDBE. This information used to all be at the top of the single
23332** source code file "vdbe.c". When that file became too big (over
23333** 6000 lines long) it was split up into several smaller files and
23334** this header information was factored out.
23335*/
23336#ifndef SQLITE_VDBEINT_H
23337#define SQLITE_VDBEINT_H
23338
23339/*
23340** The maximum number of times that a statement will try to reparse
23341** itself before giving up and returning SQLITE_SCHEMA.
23342*/
23343#ifndef SQLITE_MAX_SCHEMA_RETRY50
23344# define SQLITE_MAX_SCHEMA_RETRY50 50
23345#endif
23346
23347/*
23348** VDBE_DISPLAY_P4 is true or false depending on whether or not the
23349** "explain" P4 display logic is enabled.
23350*/
23351#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG1) \
23352 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \
23353 || defined(SQLITE_ENABLE_BYTECODE_VTAB)
23354# define VDBE_DISPLAY_P41 1
23355#else
23356# define VDBE_DISPLAY_P41 0
23357#endif
23358
23359/*
23360** SQL is translated into a sequence of instructions to be
23361** executed by a virtual machine. Each instruction is an instance
23362** of the following structure.
23363*/
23364typedef struct VdbeOp Op;
23365
23366/*
23367** Boolean values
23368*/
23369typedef unsigned Bool;
23370
23371/* Opaque type used by code in vdbesort.c */
23372typedef struct VdbeSorter VdbeSorter;
23373
23374/* Elements of the linked list at Vdbe.pAuxData */
23375typedef struct AuxData AuxData;
23376
23377/* A cache of large TEXT or BLOB values in a VdbeCursor */
23378typedef struct VdbeTxtBlbCache VdbeTxtBlbCache;
23379
23380/* Types of VDBE cursors */
23381#define CURTYPE_BTREE0 0
23382#define CURTYPE_SORTER1 1
23383#define CURTYPE_VTAB2 2
23384#define CURTYPE_PSEUDO3 3
23385
23386/*
23387** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
23388**
23389** * A b-tree cursor
23390** - In the main database or in an ephemeral database
23391** - On either an index or a table
23392** * A sorter
23393** * A virtual table
23394** * A one-row "pseudotable" stored in a single register
23395*/
23396typedef struct VdbeCursor VdbeCursor;
23397struct VdbeCursor {
23398 u8 eCurType; /* One of the CURTYPE_* values above */
23399 i8 iDb; /* Index of cursor database in db->aDb[] */
23400 u8 nullRow; /* True if pointing to a row with no data */
23401 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
23402 u8 isTable; /* True for rowid tables. False for indexes */
23403#ifdef SQLITE_DEBUG
23404 u8 seekOp; /* Most recent seek operation on this cursor */
23405 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
23406#endif
23407 Bool isEphemeral:1; /* True for an ephemeral table */
23408 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23409 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
23410 Bool noReuse:1; /* OpenEphemeral may not reuse this cursor */
23411 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23412 u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */
23413 union { /* pBtx for isEphermeral. pAltMap otherwise */
23414 Btree *pBtx; /* Separate file holding temporary table */
23415 u32 *aAltMap; /* Mapping from table to index column numbers */
23416 } ub;
23417 i64 seqCount; /* Sequence counter */
23418
23419 /* Cached OP_Column parse information is only valid if cacheStatus matches
23420 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
23421 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
23422 ** the cache is out of date. */
23423 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
23424 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
23425 ** if there have been no prior seeks on the cursor. */
23426 /* seekResult does not distinguish between "no seeks have ever occurred
23427 ** on this cursor" and "the most recent seek was an exact match".
23428 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
23429
23430 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
23431 ** The fields that follow are uninitialized, and must be individually
23432 ** initialized prior to first use. */
23433 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
23434 union {
23435 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
23436 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
23437 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
23438 } uc;
23439 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
23440 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
23441 Pgno pgnoRoot; /* Root page of the open btree cursor */
23442 i16 nField; /* Number of fields in the header */
23443 u16 nHdrParsed; /* Number of header fields parsed so far */
23444 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
23445 u32 *aOffset; /* Pointer to aType[nField] */
23446 const u8 *aRow; /* Data for the current row, if all on one page */
23447 u32 payloadSize; /* Total number of bytes in the record */
23448 u32 szRow; /* Byte available in aRow */
23449#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
23450 u64 maskUsed; /* Mask of columns used by this cursor */
23451#endif
23452 VdbeTxtBlbCache *pCache; /* Cache of large TEXT or BLOB values */
23453
23454 /* 2*nField extra array elements allocated for aType[], beyond the one
23455 ** static element declared in the structure. nField total array slots for
23456 ** aType[] and nField+1 array slots for aOffset[] */
23457 u32 aType[1]; /* Type values record decode. MUST BE LAST */
23458};
23459
23460/* Return true if P is a null-only cursor
23461*/
23462#define IsNullCursor(P)((P)->eCurType==3 && (P)->nullRow && (P
)->seekResult==0)
\
23463 ((P)->eCurType==CURTYPE_PSEUDO3 && (P)->nullRow && (P)->seekResult==0)
23464
23465/*
23466** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
23467*/
23468#define CACHE_STALE0 0
23469
23470/*
23471** Large TEXT or BLOB values can be slow to load, so we want to avoid
23472** loading them more than once. For that reason, large TEXT and BLOB values
23473** can be stored in a cache defined by this object, and attached to the
23474** VdbeCursor using the pCache field.
23475*/
23476struct VdbeTxtBlbCache {
23477 char *pCValue; /* A RCStr buffer to hold the value */
23478 i64 iOffset; /* File offset of the row being cached */
23479 int iCol; /* Column for which the cache is valid */
23480 u32 cacheStatus; /* Vdbe.cacheCtr value */
23481 u32 colCacheCtr; /* Column cache counter */
23482};
23483
23484/*
23485** When a sub-program is executed (OP_Program), a structure of this type
23486** is allocated to store the current value of the program counter, as
23487** well as the current memory cell array and various other frame specific
23488** values stored in the Vdbe struct. When the sub-program is finished,
23489** these values are copied back to the Vdbe from the VdbeFrame structure,
23490** restoring the state of the VM to as it was before the sub-program
23491** began executing.
23492**
23493** The memory for a VdbeFrame object is allocated and managed by a memory
23494** cell in the parent (calling) frame. When the memory cell is deleted or
23495** overwritten, the VdbeFrame object is not freed immediately. Instead, it
23496** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
23497** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
23498** this instead of deleting the VdbeFrame immediately is to avoid recursive
23499** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
23500** child frame are released.
23501**
23502** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
23503** set to NULL if the currently executing frame is the main program.
23504*/
23505typedef struct VdbeFrame VdbeFrame;
23506struct VdbeFrame {
23507 Vdbe *v; /* VM this frame belongs to */
23508 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
23509 Op *aOp; /* Program instructions for parent frame */
23510 Mem *aMem; /* Array of memory cells for parent frame */
23511 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
23512 u8 *aOnce; /* Bitmask used by OP_Once */
23513 void *token; /* Copy of SubProgram.token */
23514 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
23515 AuxData *pAuxData; /* Linked list of auxdata allocations */
23516#if SQLITE_DEBUG
23517 u32 iFrameMagic; /* magic number for sanity checking */
23518#endif
23519 int nCursor; /* Number of entries in apCsr */
23520 int pc; /* Program Counter in parent (calling) frame */
23521 int nOp; /* Size of aOp array */
23522 int nMem; /* Number of entries in aMem */
23523 int nChildMem; /* Number of memory cells for child frame */
23524 int nChildCsr; /* Number of cursors for child frame */
23525 i64 nChange; /* Statement changes (Vdbe.nChange) */
23526 i64 nDbChange; /* Value of db->nChange */
23527};
23528
23529/* Magic number for sanity checking on VdbeFrame objects */
23530#define SQLITE_FRAME_MAGIC0x879fb71e 0x879fb71e
23531
23532/*
23533** Return a pointer to the array of registers allocated for use
23534** by a VdbeFrame.
23535*/
23536#define VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)])
23537
23538/*
23539** Internally, the vdbe manipulates nearly all SQL values as Mem
23540** structures. Each Mem struct may cache multiple representations (string,
23541** integer etc.) of the same value.
23542*/
23543struct sqlite3_value {
23544 union MemValue {
23545 double r; /* Real value used when MEM_Real is set in flags */
23546 i64 i; /* Integer value used when MEM_Int is set in flags */
23547 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
23548 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
23549 FuncDef *pDef; /* Used only when flags==MEM_Agg */
23550 } u;
23551 char *z; /* String or BLOB value */
23552 int n; /* Number of characters in string value, excluding '\0' */
23553 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
23554 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
23555 u8 eSubtype; /* Subtype for this value */
23556 /* ShallowCopy only needs to copy the information above */
23557 sqlite3 *db; /* The associated database connection */
23558 int szMalloc; /* Size of the zMalloc allocation */
23559 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
23560 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
23561 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
23562#ifdef SQLITE_DEBUG
23563 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
23564 u16 mScopyFlags; /* flags value immediately after the shallow copy */
23565#endif
23566};
23567
23568/*
23569** Size of struct Mem not including the Mem.zMalloc member or anything that
23570** follows.
23571*/
23572#define MEMCELLSIZE__builtin_offsetof(Mem, db) offsetof(Mem,db)__builtin_offsetof(Mem, db)
23573
23574/* One or more of the following flags are set to indicate the
23575** representations of the value stored in the Mem struct.
23576**
23577** * MEM_Null An SQL NULL value
23578**
23579** * MEM_Null|MEM_Zero An SQL NULL with the virtual table
23580** UPDATE no-change flag set
23581**
23582** * MEM_Null|MEM_Term| An SQL NULL, but also contains a
23583** MEM_Subtype pointer accessible using
23584** sqlite3_value_pointer().
23585**
23586** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
23587** to other NULLs even using the IS operator.
23588**
23589** * MEM_Str A string, stored in Mem.z with
23590** length Mem.n. Zero-terminated if
23591** MEM_Term is set. This flag is
23592** incompatible with MEM_Blob and
23593** MEM_Null, but can appear with MEM_Int,
23594** MEM_Real, and MEM_IntReal.
23595**
23596** * MEM_Blob A blob, stored in Mem.z length Mem.n.
23597** Incompatible with MEM_Str, MEM_Null,
23598** MEM_Int, MEM_Real, and MEM_IntReal.
23599**
23600** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus
23601** MEM.u.i extra 0x00 bytes at the end.
23602**
23603** * MEM_Int Integer stored in Mem.u.i.
23604**
23605** * MEM_Real Real stored in Mem.u.r.
23606**
23607** * MEM_IntReal Real stored as an integer in Mem.u.i.
23608**
23609** If the MEM_Null flag is set, then the value is an SQL NULL value.
23610** For a pointer type created using sqlite3_bind_pointer() or
23611** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
23612**
23613** If the MEM_Str flag is set then Mem.z points at a string representation.
23614** Usually this is encoded in the same unicode encoding as the main
23615** database (see below for exceptions). If the MEM_Term flag is also
23616** set, then the string is nul terminated. The MEM_Int and MEM_Real
23617** flags may coexist with the MEM_Str flag.
23618*/
23619#define MEM_Undefined0x0000 0x0000 /* Value is undefined */
23620#define MEM_Null0x0001 0x0001 /* Value is NULL (or a pointer) */
23621#define MEM_Str0x0002 0x0002 /* Value is a string */
23622#define MEM_Int0x0004 0x0004 /* Value is an integer */
23623#define MEM_Real0x0008 0x0008 /* Value is a real number */
23624#define MEM_Blob0x0010 0x0010 /* Value is a BLOB */
23625#define MEM_IntReal0x0020 0x0020 /* MEM_Int that stringifies like MEM_Real */
23626#define MEM_AffMask0x003f 0x003f /* Mask of affinity bits */
23627
23628/* Extra bits that modify the meanings of the core datatypes above
23629*/
23630#define MEM_FromBind0x0040 0x0040 /* Value originates from sqlite3_bind() */
23631 /* 0x0080 // Available */
23632#define MEM_Cleared0x0100 0x0100 /* NULL set by OP_Null, not from data */
23633#define MEM_Term0x0200 0x0200 /* String in Mem.z is zero terminated */
23634#define MEM_Zero0x0400 0x0400 /* Mem.i contains count of 0s appended to blob */
23635#define MEM_Subtype0x0800 0x0800 /* Mem.eSubtype is valid */
23636#define MEM_TypeMask0x0dbf 0x0dbf /* Mask of type bits */
23637
23638/* Bits that determine the storage for Mem.z for a string or blob or
23639** aggregate accumulator.
23640*/
23641#define MEM_Dyn0x1000 0x1000 /* Need to call Mem.xDel() on Mem.z */
23642#define MEM_Static0x2000 0x2000 /* Mem.z points to a static string */
23643#define MEM_Ephem0x4000 0x4000 /* Mem.z points to an ephemeral string */
23644#define MEM_Agg0x8000 0x8000 /* Mem.z points to an agg function context */
23645
23646/* Return TRUE if Mem X contains dynamically allocated content - anything
23647** that needs to be deallocated to avoid a leak.
23648*/
23649#define VdbeMemDynamic(X)(((X)->flags&(0x8000|0x1000))!=0) \
23650 (((X)->flags&(MEM_Agg0x8000|MEM_Dyn0x1000))!=0)
23651
23652/*
23653** Clear any existing type flags from a Mem and replace them with f
23654*/
23655#define MemSetTypeFlag(p, f)((p)->flags = ((p)->flags&~(0x0dbf|0x0400))|f) \
23656 ((p)->flags = ((p)->flags&~(MEM_TypeMask0x0dbf|MEM_Zero0x0400))|f)
23657
23658/*
23659** True if Mem X is a NULL-nochng type.
23660*/
23661#define MemNullNochng(X)(((X)->flags&0x0dbf)==(0x0001|0x0400) && (X)->
n==0 && (X)->u.nZero==0)
\
23662 (((X)->flags&MEM_TypeMask0x0dbf)==(MEM_Null0x0001|MEM_Zero0x0400) \
23663 && (X)->n==0 && (X)->u.nZero==0)
23664
23665/*
23666** Return true if a memory cell has been initialized and is valid.
23667** is for use inside assert() statements only.
23668**
23669** A Memory cell is initialized if at least one of the
23670** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits
23671** is set. It is "undefined" if all those bits are zero.
23672*/
23673#ifdef SQLITE_DEBUG
23674#define memIsValid(M) ((M)->flags & MEM_AffMask0x003f)!=0
23675#endif
23676
23677/*
23678** Each auxiliary data pointer stored by a user defined function
23679** implementation calling sqlite3_set_auxdata() is stored in an instance
23680** of this structure. All such structures associated with a single VM
23681** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
23682** when the VM is halted (if not before).
23683*/
23684struct AuxData {
23685 int iAuxOp; /* Instruction number of OP_Function opcode */
23686 int iAuxArg; /* Index of function argument. */
23687 void *pAux; /* Aux data pointer */
23688 void (*xDeleteAux)(void*); /* Destructor for the aux data */
23689 AuxData *pNextAux; /* Next element in list */
23690};
23691
23692/*
23693** The "context" argument for an installable function. A pointer to an
23694** instance of this structure is the first argument to the routines used
23695** implement the SQL functions.
23696**
23697** There is a typedef for this structure in sqlite.h. So all routines,
23698** even the public interface to SQLite, can use a pointer to this structure.
23699** But this file is the only place where the internal details of this
23700** structure are known.
23701**
23702** This structure is defined inside of vdbeInt.h because it uses substructures
23703** (Mem) which are only defined there.
23704*/
23705struct sqlite3_context {
23706 Mem *pOut; /* The return value is stored here */
23707 FuncDef *pFunc; /* Pointer to function information */
23708 Mem *pMem; /* Memory cell used to store aggregate context */
23709 Vdbe *pVdbe; /* The VM that owns this context */
23710 int iOp; /* Instruction number of OP_Function */
23711 int isError; /* Error code returned by the function. */
23712 u8 enc; /* Encoding to use for results */
23713 u8 skipFlag; /* Skip accumulator loading if true */
23714 u8 argc; /* Number of arguments */
23715 sqlite3_value *argv[1]; /* Argument set */
23716};
23717
23718/* A bitfield type for use inside of structures. Always follow with :N where
23719** N is the number of bits.
23720*/
23721typedef unsigned bft; /* Bit Field Type */
23722
23723/* The ScanStatus object holds a single value for the
23724** sqlite3_stmt_scanstatus() interface.
23725**
23726** aAddrRange[]:
23727** This array is used by ScanStatus elements associated with EQP
23728** notes that make an SQLITE_SCANSTAT_NCYCLE value available. It is
23729** an array of up to 3 ranges of VM addresses for which the Vdbe.anCycle[]
23730** values should be summed to calculate the NCYCLE value. Each pair of
23731** integer addresses is a start and end address (both inclusive) for a range
23732** instructions. A start value of 0 indicates an empty range.
23733*/
23734typedef struct ScanStatus ScanStatus;
23735struct ScanStatus {
23736 int addrExplain; /* OP_Explain for loop */
23737 int aAddrRange[6];
23738 int addrLoop; /* Address of "loops" counter */
23739 int addrVisit; /* Address of "rows visited" counter */
23740 int iSelectID; /* The "Select-ID" for this loop */
23741 LogEst nEst; /* Estimated output rows per loop */
23742 char *zName; /* Name of table or index */
23743};
23744
23745/* The DblquoteStr object holds the text of a double-quoted
23746** string for a prepared statement. A linked list of these objects
23747** is constructed during statement parsing and is held on Vdbe.pDblStr.
23748** When computing a normalized SQL statement for an SQL statement, that
23749** list is consulted for each double-quoted identifier to see if the
23750** identifier should really be a string literal.
23751*/
23752typedef struct DblquoteStr DblquoteStr;
23753struct DblquoteStr {
23754 DblquoteStr *pNextStr; /* Next string literal in the list */
23755 char z[8]; /* Dequoted value for the string */
23756};
23757
23758/*
23759** An instance of the virtual machine. This structure contains the complete
23760** state of the virtual machine.
23761**
23762** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
23763** is really a pointer to an instance of this structure.
23764*/
23765struct Vdbe {
23766 sqlite3 *db; /* The database connection that owns this statement */
23767 Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */
23768 Parse *pParse; /* Parsing context used to create this Vdbe */
23769 ynVar nVar; /* Number of entries in aVar[] */
23770 int nMem; /* Number of memory locations currently allocated */
23771 int nCursor; /* Number of slots in apCsr[] */
23772 u32 cacheCtr; /* VdbeCursor row cache generation counter */
23773 int pc; /* The program counter */
23774 int rc; /* Value to return */
23775 i64 nChange; /* Number of db changes made since last reset */
23776 int iStatement; /* Statement number (or 0 if has no opened stmt) */
23777 i64 iCurrentTime; /* Value of julianday('now') for this statement */
23778 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
23779 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
23780 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
23781 Mem *aMem; /* The memory locations */
23782 Mem **apArg; /* Arguments to currently executing user function */
23783 VdbeCursor **apCsr; /* One element of this array for each open cursor */
23784 Mem *aVar; /* Values for the OP_Variable opcode. */
23785
23786 /* When allocating a new Vdbe object, all of the fields below should be
23787 ** initialized to zero or NULL */
23788
23789 Op *aOp; /* Space to hold the virtual machine's program */
23790 int nOp; /* Number of instructions in the program */
23791 int nOpAlloc; /* Slots allocated for aOp[] */
23792 Mem *aColName; /* Column names to return */
23793 Mem *pResultRow; /* Current output row */
23794 char *zErrMsg; /* Error message written here */
23795 VList *pVList; /* Name of variables */
23796#ifndef SQLITE_OMIT_TRACE
23797 i64 startTime; /* Time when query started - used for profiling */
23798#endif
23799#ifdef SQLITE_DEBUG
23800 int rcApp; /* errcode set by sqlite3_result_error_code() */
23801 u32 nWrite; /* Number of write operations that have occurred */
23802#endif
23803 u16 nResColumn; /* Number of columns in one row of the result set */
23804 u16 nResAlloc; /* Column slots allocated to aColName[] */
23805 u8 errorAction; /* Recovery action to do in case of an error */
23806 u8 minWriteFileFormat; /* Minimum file format for writable database files */
23807 u8 prepFlags; /* SQLITE_PREPARE_* flags */
23808 u8 eVdbeState; /* On of the VDBE_*_STATE values */
23809 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
23810 bft explain:2; /* 0: normal, 1: EXPLAIN, 2: EXPLAIN QUERY PLAN */
23811 bft changeCntOn:1; /* True to update the change-counter */
23812 bft usesStmtJournal:1; /* True if uses a statement journal */
23813 bft readOnly:1; /* True for statements that do not write */
23814 bft bIsReader:1; /* True for statements that read */
23815 bft haveEqpOps:1; /* Bytecode supports EXPLAIN QUERY PLAN */
23816 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23817 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
23818 u32 aCounter[9]; /* Counters used by sqlite3_stmt_status() */
23819 char *zSql; /* Text of the SQL statement that generated this */
23820#ifdef SQLITE_ENABLE_NORMALIZE
23821 char *zNormSql; /* Normalization of the associated SQL statement */
23822 DblquoteStr *pDblStr; /* List of double-quoted string literals */
23823#endif
23824 void *pFree; /* Free this when deleting the vdbe */
23825 VdbeFrame *pFrame; /* Parent frame */
23826 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
23827 int nFrame; /* Number of frames in pFrame list */
23828 u32 expmask; /* Binding to these vars invalidates VM */
23829 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23830 AuxData *pAuxData; /* Linked list of auxdata allocations */
23831#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
23832 int nScan; /* Entries in aScan[] */
23833 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
23834#endif
23835};
23836
23837/*
23838** The following are allowed values for Vdbe.eVdbeState
23839*/
23840#define VDBE_INIT_STATE0 0 /* Prepared statement under construction */
23841#define VDBE_READY_STATE1 1 /* Ready to run but not yet started */
23842#define VDBE_RUN_STATE2 2 /* Run in progress */
23843#define VDBE_HALT_STATE3 3 /* Finished. Need reset() or finalize() */
23844
23845/*
23846** Structure used to store the context required by the
23847** sqlite3_preupdate_*() API functions.
23848*/
23849struct PreUpdate {
23850 Vdbe *v;
23851 VdbeCursor *pCsr; /* Cursor to read old values from */
23852 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
23853 u8 *aRecord; /* old.* database record */
23854 KeyInfo keyinfo;
23855 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
23856 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
23857 int iNewReg; /* Register for new.* values */
23858 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
23859 i64 iKey1; /* First key value passed to hook */
23860 i64 iKey2; /* Second key value passed to hook */
23861 Mem *aNew; /* Array of new.* values */
23862 Table *pTab; /* Schema object being updated */
23863 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
23864 sqlite3_value **apDflt; /* Array of default values, if required */
23865};
23866
23867/*
23868** An instance of this object is used to pass an vector of values into
23869** OP_VFilter, the xFilter method of a virtual table. The vector is the
23870** set of values on the right-hand side of an IN constraint.
23871**
23872** The value as passed into xFilter is an sqlite3_value with a "pointer"
23873** type, such as is generated by sqlite3_result_pointer() and read by
23874** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null
23875** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces
23876** know how to use this object to step through all the values in the
23877** right operand of the IN constraint.
23878*/
23879typedef struct ValueList ValueList;
23880struct ValueList {
23881 BtCursor *pCsr; /* An ephemeral table holding all values */
23882 sqlite3_value *pOut; /* Register to hold each decoded output value */
23883};
23884
23885/* Size of content associated with serial types that fit into a
23886** single-byte varint.
23887*/
23888#ifndef SQLITE_AMALGAMATION1
23889SQLITE_PRIVATEstatic const u8 sqlite3SmallTypeSizes[];
23890#endif
23891
23892/*
23893** Function prototypes
23894*/
23895SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe*, const char *, ...);
23896SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
23897SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursorNN(Vdbe*,VdbeCursor*);
23898void sqliteVdbePopStack(Vdbe*,int);
23899SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeHandleMovedCursor(VdbeCursor *p);
23900SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeFinishMoveto(VdbeCursor*);
23901SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor*);
23902SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32);
23903SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8);
23904#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
23905SQLITE_PRIVATEstatic u64 sqlite3FloatSwap(u64 in);
23906# define swapMixedEndianFloat(X) X = sqlite3FloatSwap(X)
23907#else
23908# define swapMixedEndianFloat(X)
23909#endif
23910SQLITE_PRIVATEstatic void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
23911SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
23912
23913int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
23914SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
23915SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
23916SQLITE_PRIVATEstatic int sqlite3VdbeExec(Vdbe*);
23917#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
23918SQLITE_PRIVATEstatic int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**);
23919SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayP4(sqlite3*,Op*);
23920#endif
23921#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
23922SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*);
23923#endif
23924#if !defined(SQLITE_OMIT_EXPLAIN)
23925SQLITE_PRIVATEstatic int sqlite3VdbeList(Vdbe*);
23926#endif
23927SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe*);
23928SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *, int);
23929SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem*);
23930SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem*, const Mem*);
23931SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
23932SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem*, Mem*);
23933SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem*);
23934SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*));
23935SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem*, i64);
23936#ifdef SQLITE_OMIT_FLOATING_POINT
23937# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
23938#else
23939SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem*, double);
23940#endif
23941SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
23942SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
23943SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem*);
23944#ifndef SQLITE_OMIT_INCRBLOB
23945SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem*,int);
23946#else
23947SQLITE_PRIVATEstatic int sqlite3VdbeMemSetZeroBlob(Mem*,int);
23948#endif
23949#ifdef SQLITE_DEBUG
23950SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem*);
23951#endif
23952SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem*);
23953SQLITE_PRIVATEstatic void sqlite3VdbeMemZeroTerminateIfAble(Mem*);
23954SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem*);
23955SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem*, u8, u8);
23956SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64,double);
23957SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(const Mem*);
23958SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem*);
23959SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem*);
23960SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem*, int ifNull);
23961SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem*);
23962SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem*);
23963SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem*);
23964SQLITE_PRIVATEstatic int sqlite3VdbeMemCast(Mem*,u8,u8);
23965SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
23966SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
23967SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p);
23968SQLITE_PRIVATEstatic void sqlite3VdbeMemReleaseMalloc(Mem*p);
23969SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
23970#ifndef SQLITE_OMIT_WINDOWFUNC
23971SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
23972#endif
23973#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB)
23974SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int);
23975#endif
23976SQLITE_PRIVATEstatic int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
23977SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
23978SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *, int);
23979#ifdef SQLITE_DEBUG
23980SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame*);
23981#endif
23982SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
23983SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
23984SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *);
23985#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
23986SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
23987 Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int);
23988#endif
23989SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p);
23990
23991SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
23992SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
23993SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
23994SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
23995SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
23996SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
23997SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
23998SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
23999
24000SQLITE_PRIVATEstatic void sqlite3VdbeValueListFree(void*);
24001
24002#ifdef SQLITE_DEBUG
24003SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
24004SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe*);
24005#else
24006# define sqlite3VdbeIncrWriteCounter(V,C)
24007# define sqlite3VdbeAssertAbortable(V)
24008#endif
24009
24010#if !defined(SQLITE_OMIT_SHARED_CACHE)
24011SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe*);
24012#else
24013# define sqlite3VdbeEnter(X)
24014#endif
24015
24016#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2>0
24017SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe*);
24018#else
24019# define sqlite3VdbeLeave(X)
24020#endif
24021
24022#ifdef SQLITE_DEBUG
24023SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
24024SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem*);
24025#endif
24026
24027#ifndef SQLITE_OMIT_FOREIGN_KEY
24028SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *, int);
24029#else
24030# define sqlite3VdbeCheckFk(p,i) 0
24031#endif
24032
24033#ifdef SQLITE_DEBUG
24034SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe*);
24035SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
24036#endif
24037#ifndef SQLITE_OMIT_UTF16
24038SQLITE_PRIVATEstatic int sqlite3VdbeMemTranslate(Mem*, u8);
24039SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem);
24040#endif
24041
24042#ifndef SQLITE_OMIT_INCRBLOB
24043SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *);
24044 #define ExpandBlob(P)(((P)->flags&0x0400)?sqlite3VdbeMemExpandBlob(P):0) (((P)->flags&MEM_Zero0x0400)?sqlite3VdbeMemExpandBlob(P):0)
24045#else
24046 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK0
24047 #define ExpandBlob(P)(((P)->flags&0x0400)?sqlite3VdbeMemExpandBlob(P):0) SQLITE_OK0
24048#endif
24049
24050#endif /* !defined(SQLITE_VDBEINT_H) */
24051
24052/************** End of vdbeInt.h *********************************************/
24053/************** Continuing where we left off in status.c *********************/
24054
24055/*
24056** Variables in which to record status information.
24057*/
24058#if SQLITE_PTRSIZE8>4
24059typedef sqlite3_int64 sqlite3StatValueType;
24060#else
24061typedef u32 sqlite3StatValueType;
24062#endif
24063typedef struct sqlite3StatType sqlite3StatType;
24064static SQLITE_WSD struct sqlite3StatType {
24065 sqlite3StatValueType nowValue[10]; /* Current value */
24066 sqlite3StatValueType mxValue[10]; /* Maximum value */
24067} sqlite3Stat = { {0,}, {0,} };
24068
24069/*
24070** Elements of sqlite3Stat[] are protected by either the memory allocator
24071** mutex, or by the pcache1 mutex. The following array determines which.
24072*/
24073static const char statMutex[] = {
24074 0, /* SQLITE_STATUS_MEMORY_USED */
24075 1, /* SQLITE_STATUS_PAGECACHE_USED */
24076 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
24077 0, /* SQLITE_STATUS_SCRATCH_USED */
24078 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
24079 0, /* SQLITE_STATUS_MALLOC_SIZE */
24080 0, /* SQLITE_STATUS_PARSER_STACK */
24081 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
24082 0, /* SQLITE_STATUS_SCRATCH_SIZE */
24083 0, /* SQLITE_STATUS_MALLOC_COUNT */
24084};
24085
24086
24087/* The "wsdStat" macro will resolve to the status information
24088** state vector. If writable static data is unsupported on the target,
24089** we have to locate the state vector at run-time. In the more common
24090** case where writable static data is supported, wsdStat can refer directly
24091** to the "sqlite3Stat" state vector declared above.
24092*/
24093#ifdef SQLITE_OMIT_WSD
24094# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)sqlite3Stat
24095# define wsdStatsqlite3Stat x[0]
24096#else
24097# define wsdStatInit
24098# define wsdStatsqlite3Stat sqlite3Stat
24099#endif
24100
24101/*
24102** Return the current value of a status parameter. The caller must
24103** be holding the appropriate mutex.
24104*/
24105SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int op){
24106 wsdStatInit;
24107 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24108 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24109 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24110 : sqlite3MallocMutex()) )((void) (0));
24111 return wsdStatsqlite3Stat.nowValue[op];
24112}
24113
24114/*
24115** Add N to the value of a status record. The caller must hold the
24116** appropriate mutex. (Locking is checked by assert()).
24117**
24118** The StatusUp() routine can accept positive or negative values for N.
24119** The value of N is added to the current status value and the high-water
24120** mark is adjusted if necessary.
24121**
24122** The StatusDown() routine lowers the current value by N. The highwater
24123** mark is unchanged. N must be non-negative for StatusDown().
24124*/
24125SQLITE_PRIVATEstatic void sqlite3StatusUp(int op, int N){
24126 wsdStatInit;
24127 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24128 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24129 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24130 : sqlite3MallocMutex()) )((void) (0));
24131 wsdStatsqlite3Stat.nowValue[op] += N;
24132 if( wsdStatsqlite3Stat.nowValue[op]>wsdStatsqlite3Stat.mxValue[op] ){
24133 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
24134 }
24135}
24136SQLITE_PRIVATEstatic void sqlite3StatusDown(int op, int N){
24137 wsdStatInit;
24138 assert( N>=0 )((void) (0));
24139 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24140 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24141 : sqlite3MallocMutex()) )((void) (0));
24142 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24143 wsdStatsqlite3Stat.nowValue[op] -= N;
24144}
24145
24146/*
24147** Adjust the highwater mark if necessary.
24148** The caller must hold the appropriate mutex.
24149*/
24150SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int op, int X){
24151 sqlite3StatValueType newValue;
24152 wsdStatInit;
24153 assert( X>=0 )((void) (0));
24154 newValue = (sqlite3StatValueType)X;
24155 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
24156 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
24157 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
24158 : sqlite3MallocMutex()) )((void) (0));
24159 assert( op==SQLITE_STATUS_MALLOC_SIZE((void) (0))
24160 || op==SQLITE_STATUS_PAGECACHE_SIZE((void) (0))
24161 || op==SQLITE_STATUS_PARSER_STACK )((void) (0));
24162 if( newValue>wsdStatsqlite3Stat.mxValue[op] ){
24163 wsdStatsqlite3Stat.mxValue[op] = newValue;
24164 }
24165}
24166
24167/*
24168** Query status information.
24169*/
24170SQLITE_API int sqlite3_status64(
24171 int op,
24172 sqlite3_int64 *pCurrent,
24173 sqlite3_int64 *pHighwater,
24174 int resetFlag
24175){
24176 sqlite3_mutex *pMutex;
24177 wsdStatInit;
24178 if( op<0 || op>=ArraySize(wsdStat.nowValue)((int)(sizeof(sqlite3Stat.nowValue)/sizeof(sqlite3Stat.nowValue
[0])))
){
24179 return SQLITE_MISUSE_BKPTsqlite3MisuseError(24179);
24180 }
24181#ifdef SQLITE_ENABLE_API_ARMOR1
24182 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(24182);
24183#endif
24184 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
24185 sqlite3_mutex_enter(pMutex);
24186 *pCurrent = wsdStatsqlite3Stat.nowValue[op];
24187 *pHighwater = wsdStatsqlite3Stat.mxValue[op];
24188 if( resetFlag ){
24189 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
24190 }
24191 sqlite3_mutex_leave(pMutex);
24192 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
24193 return SQLITE_OK0;
24194}
24195SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
24196 sqlite3_int64 iCur = 0, iHwtr = 0;
24197 int rc;
24198#ifdef SQLITE_ENABLE_API_ARMOR1
24199 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(24199);
24200#endif
24201 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
24202 if( rc==0 ){
24203 *pCurrent = (int)iCur;
24204 *pHighwater = (int)iHwtr;
24205 }
24206 return rc;
24207}
24208
24209/*
24210** Return the number of LookasideSlot elements on the linked list
24211*/
24212static u32 countLookasideSlots(LookasideSlot *p){
24213 u32 cnt = 0;
24214 while( p ){
24215 p = p->pNext;
24216 cnt++;
24217 }
24218 return cnt;
24219}
24220
24221/*
24222** Count the number of slots of lookaside memory that are outstanding
24223*/
24224SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
24225 u32 nInit = countLookasideSlots(db->lookaside.pInit);
24226 u32 nFree = countLookasideSlots(db->lookaside.pFree);
24227#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
24228 nInit += countLookasideSlots(db->lookaside.pSmallInit);
24229 nFree += countLookasideSlots(db->lookaside.pSmallFree);
24230#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
24231 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
24232 return db->lookaside.nSlot - (nInit+nFree);
24233}
24234
24235/*
24236** Query status information for a single database connection
24237*/
24238SQLITE_API int sqlite3_db_status(
24239 sqlite3 *db, /* The database connection whose status is desired */
24240 int op, /* Status verb */
24241 int *pCurrent, /* Write current value here */
24242 int *pHighwater, /* Write high-water mark here */
24243 int resetFlag /* Reset high-water mark if true */
24244){
24245 int rc = SQLITE_OK0; /* Return code */
24246#ifdef SQLITE_ENABLE_API_ARMOR1
24247 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
24248 return SQLITE_MISUSE_BKPTsqlite3MisuseError(24248);
24249 }
24250#endif
24251 sqlite3_mutex_enter(db->mutex);
24252 switch( op ){
24253 case SQLITE_DBSTATUS_LOOKASIDE_USED0: {
24254 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
24255 if( resetFlag ){
24256 LookasideSlot *p = db->lookaside.pFree;
24257 if( p ){
24258 while( p->pNext ) p = p->pNext;
24259 p->pNext = db->lookaside.pInit;
24260 db->lookaside.pInit = db->lookaside.pFree;
24261 db->lookaside.pFree = 0;
24262 }
24263#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
24264 p = db->lookaside.pSmallFree;
24265 if( p ){
24266 while( p->pNext ) p = p->pNext;
24267 p->pNext = db->lookaside.pSmallInit;
24268 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
24269 db->lookaside.pSmallFree = 0;
24270 }
24271#endif
24272 }
24273 break;
24274 }
24275
24276 case SQLITE_DBSTATUS_LOOKASIDE_HIT4:
24277 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5:
24278 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6: {
24279 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
24280 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
24281 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
24282 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 )((void) (0));
24283 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 )((void) (0));
24284 *pCurrent = 0;
24285 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4];
24286 if( resetFlag ){
24287 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4] = 0;
24288 }
24289 break;
24290 }
24291
24292 /*
24293 ** Return an approximation for the amount of memory currently used
24294 ** by all pagers associated with the given database connection. The
24295 ** highwater mark is meaningless and is returned as zero.
24296 */
24297 case SQLITE_DBSTATUS_CACHE_USED_SHARED11:
24298 case SQLITE_DBSTATUS_CACHE_USED1: {
24299 int totalUsed = 0;
24300 int i;
24301 sqlite3BtreeEnterAll(db);
24302 for(i=0; i<db->nDb; i++){
24303 Btree *pBt = db->aDb[i].pBt;
24304 if( pBt ){
24305 Pager *pPager = sqlite3BtreePager(pBt);
24306 int nByte = sqlite3PagerMemUsed(pPager);
24307 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED11 ){
24308 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
24309 }
24310 totalUsed += nByte;
24311 }
24312 }
24313 sqlite3BtreeLeaveAll(db);
24314 *pCurrent = totalUsed;
24315 *pHighwater = 0;
24316 break;
24317 }
24318
24319 /*
24320 ** *pCurrent gets an accurate estimate of the amount of memory used
24321 ** to store the schema for all databases (main, temp, and any ATTACHed
24322 ** databases. *pHighwater is set to zero.
24323 */
24324 case SQLITE_DBSTATUS_SCHEMA_USED2: {
24325 int i; /* Used to iterate through schemas */
24326 int nByte = 0; /* Used to accumulate return value */
24327
24328 sqlite3BtreeEnterAll(db);
24329 db->pnBytesFreed = &nByte;
24330 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd )((void) (0));
24331 db->lookaside.pEnd = db->lookaside.pStart;
24332 for(i=0; i<db->nDb; i++){
24333 Schema *pSchema = db->aDb[i].pSchema;
24334 if( ALWAYS(pSchema!=0)(pSchema!=0) ){
24335 HashElem *p;
24336
24337 nByte += sqlite3GlobalConfigsqlite3Config.m.xRoundup(sizeof(HashElem)) * (
24338 pSchema->tblHash.count
24339 + pSchema->trigHash.count
24340 + pSchema->idxHash.count
24341 + pSchema->fkeyHash.count
24342 );
24343 nByte += sqlite3_msize(pSchema->tblHash.ht);
24344 nByte += sqlite3_msize(pSchema->trigHash.ht);
24345 nByte += sqlite3_msize(pSchema->idxHash.ht);
24346 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
24347
24348 for(p=sqliteHashFirst(&pSchema->trigHash)((&pSchema->trigHash)->first); p; p=sqliteHashNext(p)((p)->next)){
24349 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)((p)->data));
24350 }
24351 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
24352 sqlite3DeleteTable(db, (Table *)sqliteHashData(p)((p)->data));
24353 }
24354 }
24355 }
24356 db->pnBytesFreed = 0;
24357 db->lookaside.pEnd = db->lookaside.pTrueEnd;
24358 sqlite3BtreeLeaveAll(db);
24359
24360 *pHighwater = 0;
24361 *pCurrent = nByte;
24362 break;
24363 }
24364
24365 /*
24366 ** *pCurrent gets an accurate estimate of the amount of memory used
24367 ** to store all prepared statements.
24368 ** *pHighwater is set to zero.
24369 */
24370 case SQLITE_DBSTATUS_STMT_USED3: {
24371 struct Vdbe *pVdbe; /* Used to iterate through VMs */
24372 int nByte = 0; /* Used to accumulate return value */
24373
24374 db->pnBytesFreed = &nByte;
24375 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd )((void) (0));
24376 db->lookaside.pEnd = db->lookaside.pStart;
24377 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
24378 sqlite3VdbeDelete(pVdbe);
24379 }
24380 db->lookaside.pEnd = db->lookaside.pTrueEnd;
24381 db->pnBytesFreed = 0;
24382
24383 *pHighwater = 0; /* IMP: R-64479-57858 */
24384 *pCurrent = nByte;
24385
24386 break;
24387 }
24388
24389 /*
24390 ** Set *pCurrent to the total cache hits or misses encountered by all
24391 ** pagers the database handle is connected to. *pHighwater is always set
24392 ** to zero.
24393 */
24394 case SQLITE_DBSTATUS_CACHE_SPILL12:
24395 op = SQLITE_DBSTATUS_CACHE_WRITE9+1;
24396 /* no break */ deliberate_fall_through__attribute__((fallthrough));
24397 case SQLITE_DBSTATUS_CACHE_HIT7:
24398 case SQLITE_DBSTATUS_CACHE_MISS8:
24399 case SQLITE_DBSTATUS_CACHE_WRITE9:{
24400 int i;
24401 u64 nRet = 0;
24402 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 )((void) (0));
24403 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 )((void) (0));
24404
24405 for(i=0; i<db->nDb; i++){
24406 if( db->aDb[i].pBt ){
24407 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
24408 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
24409 }
24410 }
24411 *pHighwater = 0; /* IMP: R-42420-56072 */
24412 /* IMP: R-54100-20147 */
24413 /* IMP: R-29431-39229 */
24414 *pCurrent = (int)nRet & 0x7fffffff;
24415 break;
24416 }
24417
24418 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
24419 ** key constraints. Set *pCurrent to zero if all foreign key constraints
24420 ** have been satisfied. The *pHighwater is always set to zero.
24421 */
24422 case SQLITE_DBSTATUS_DEFERRED_FKS10: {
24423 *pHighwater = 0; /* IMP: R-11967-56545 */
24424 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
24425 break;
24426 }
24427
24428 default: {
24429 rc = SQLITE_ERROR1;
24430 }
24431 }
24432 sqlite3_mutex_leave(db->mutex);
24433 return rc;
24434}
24435
24436/************** End of status.c **********************************************/
24437/************** Begin file date.c ********************************************/
24438/*
24439** 2003 October 31
24440**
24441** The author disclaims copyright to this source code. In place of
24442** a legal notice, here is a blessing:
24443**
24444** May you do good and not evil.
24445** May you find forgiveness for yourself and forgive others.
24446** May you share freely, never taking more than you give.
24447**
24448*************************************************************************
24449** This file contains the C functions that implement date and time
24450** functions for SQLite.
24451**
24452** There is only one exported symbol in this file - the function
24453** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
24454** All other code has file scope.
24455**
24456** SQLite processes all times and dates as julian day numbers. The
24457** dates and times are stored as the number of days since noon
24458** in Greenwich on November 24, 4714 B.C. according to the Gregorian
24459** calendar system.
24460**
24461** 1970-01-01 00:00:00 is JD 2440587.5
24462** 2000-01-01 00:00:00 is JD 2451544.5
24463**
24464** This implementation requires years to be expressed as a 4-digit number
24465** which means that only dates between 0000-01-01 and 9999-12-31 can
24466** be represented, even though julian day numbers allow a much wider
24467** range of dates.
24468**
24469** The Gregorian calendar system is used for all dates and times,
24470** even those that predate the Gregorian calendar. Historians usually
24471** use the julian calendar for dates prior to 1582-10-15 and for some
24472** dates afterwards, depending on locale. Beware of this difference.
24473**
24474** The conversion algorithms are implemented based on descriptions
24475** in the following text:
24476**
24477** Jean Meeus
24478** Astronomical Algorithms, 2nd Edition, 1998
24479** ISBN 0-943396-61-1
24480** Willmann-Bell, Inc
24481** Richmond, Virginia (USA)
24482*/
24483/* #include "sqliteInt.h" */
24484/* #include <stdlib.h> */
24485/* #include <assert.h> */
24486#include <time.h>
24487
24488#ifndef SQLITE_OMIT_DATETIME_FUNCS
24489
24490/*
24491** The MSVC CRT on Windows CE may not have a localtime() function.
24492** So declare a substitute. The substitute function itself is
24493** defined in "os_win.c".
24494*/
24495#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
24496 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
24497struct tm *__cdecl localtime(const time_t *);
24498#endif
24499
24500/*
24501** A structure for holding a single date and time.
24502*/
24503typedef struct DateTime DateTime;
24504struct DateTime {
24505 sqlite3_int64 iJD; /* The julian day number times 86400000 */
24506 int Y, M, D; /* Year, month, and day */
24507 int h, m; /* Hour and minutes */
24508 int tz; /* Timezone offset in minutes */
24509 double s; /* Seconds */
24510 char validJD; /* True (1) if iJD is valid */
24511 char validYMD; /* True (1) if Y,M,D are valid */
24512 char validHMS; /* True (1) if h,m,s are valid */
24513 char nFloor; /* Days to implement "floor" */
24514 unsigned rawS : 1; /* Raw numeric value stored in s */
24515 unsigned isError : 1; /* An overflow has occurred */
24516 unsigned useSubsec : 1; /* Display subsecond precision */
24517 unsigned isUtc : 1; /* Time is known to be UTC */
24518 unsigned isLocal : 1; /* Time is known to be localtime */
24519};
24520
24521
24522/*
24523** Convert zDate into one or more integers according to the conversion
24524** specifier zFormat.
24525**
24526** zFormat[] contains 4 characters for each integer converted, except for
24527** the last integer which is specified by three characters. The meaning
24528** of a four-character format specifiers ABCD is:
24529**
24530** A: number of digits to convert. Always "2" or "4".
24531** B: minimum value. Always "0" or "1".
24532** C: maximum value, decoded as:
24533** a: 12
24534** b: 14
24535** c: 24
24536** d: 31
24537** e: 59
24538** f: 9999
24539** D: the separator character, or \000 to indicate this is the
24540** last number to convert.
24541**
24542** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
24543** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
24544** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
24545** the 2-digit day which is the last integer in the set.
24546**
24547** The function returns the number of successful conversions.
24548*/
24549static int getDigits(const char *zDate, const char *zFormat, ...){
24550 /* The aMx[] array translates the 3rd character of each format
24551 ** spec into a max size: a b c d e f */
24552 static const u16 aMx[] = { 12, 14, 24, 31, 59, 14712 };
24553 va_list ap;
24554 int cnt = 0;
24555 char nextC;
24556 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
24557 do{
24558 char N = zFormat[0] - '0';
24559 char min = zFormat[1] - '0';
24560 int val = 0;
24561 u16 max;
24562
24563 assert( zFormat[2]>='a' && zFormat[2]<='f' )((void) (0));
24564 max = aMx[zFormat[2] - 'a'];
24565 nextC = zFormat[3];
24566 val = 0;
24567 while( N-- ){
24568 if( !sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
24569 goto end_getDigits;
24570 }
24571 val = val*10 + *zDate - '0';
24572 zDate++;
24573 }
24574 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
24575 goto end_getDigits;
24576 }
24577 *va_arg(ap,int*)__builtin_va_arg(ap, int*) = val;
24578 zDate++;
24579 cnt++;
24580 zFormat += 4;
24581 }while( nextC );
24582end_getDigits:
24583 va_end(ap)__builtin_va_end(ap);
24584 return cnt;
24585}
24586
24587/*
24588** Parse a timezone extension on the end of a date-time.
24589** The extension is of the form:
24590**
24591** (+/-)HH:MM
24592**
24593** Or the "zulu" notation:
24594**
24595** Z
24596**
24597** If the parse is successful, write the number of minutes
24598** of change in p->tz and return 0. If a parser error occurs,
24599** return non-zero.
24600**
24601** A missing specifier is not considered an error.
24602*/
24603static int parseTimezone(const char *zDate, DateTime *p){
24604 int sgn = 0;
24605 int nHr, nMn;
24606 int c;
24607 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
24608 p->tz = 0;
24609 c = *zDate;
24610 if( c=='-' ){
24611 sgn = -1;
24612 }else if( c=='+' ){
24613 sgn = +1;
24614 }else if( c=='Z' || c=='z' ){
24615 zDate++;
24616 p->isLocal = 0;
24617 p->isUtc = 1;
24618 goto zulu_time;
24619 }else{
24620 return c!=0;
24621 }
24622 zDate++;
24623 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
24624 return 1;
24625 }
24626 zDate += 5;
24627 p->tz = sgn*(nMn + nHr*60);
24628zulu_time:
24629 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
24630 return *zDate!=0;
24631}
24632
24633/*
24634** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
24635** The HH, MM, and SS must each be exactly 2 digits. The
24636** fractional seconds FFFF can be one or more digits.
24637**
24638** Return 1 if there is a parsing error and 0 on success.
24639*/
24640static int parseHhMmSs(const char *zDate, DateTime *p){
24641 int h, m, s;
24642 double ms = 0.0;
24643 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
24644 return 1;
24645 }
24646 zDate += 5;
24647 if( *zDate==':' ){
24648 zDate++;
24649 if( getDigits(zDate, "20e", &s)!=1 ){
24650 return 1;
24651 }
24652 zDate += 2;
24653 if( *zDate=='.' && sqlite3Isdigit(zDate[1])(sqlite3CtypeMap[(unsigned char)(zDate[1])]&0x04) ){
24654 double rScale = 1.0;
24655 zDate++;
24656 while( sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
24657 ms = ms*10.0 + *zDate - '0';
24658 rScale *= 10.0;
24659 zDate++;
24660 }
24661 ms /= rScale;
24662 }
24663 }else{
24664 s = 0;
24665 }
24666 p->validJD = 0;
24667 p->rawS = 0;
24668 p->validHMS = 1;
24669 p->h = h;
24670 p->m = m;
24671 p->s = s + ms;
24672 if( parseTimezone(zDate, p) ) return 1;
24673 return 0;
24674}
24675
24676/*
24677** Put the DateTime object into its error state.
24678*/
24679static void datetimeError(DateTime *p){
24680 memset(p, 0, sizeof(*p));
24681 p->isError = 1;
24682}
24683
24684/*
24685** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24686** that the YYYY-MM-DD is according to the Gregorian calendar.
24687**
24688** Reference: Meeus page 61
24689*/
24690static void computeJD(DateTime *p){
24691 int Y, M, D, A, B, X1, X2;
24692
24693 if( p->validJD ) return;
24694 if( p->validYMD ){
24695 Y = p->Y;
24696 M = p->M;
24697 D = p->D;
24698 }else{
24699 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
24700 M = 1;
24701 D = 1;
24702 }
24703 if( Y<-4713 || Y>9999 || p->rawS ){
24704 datetimeError(p);
24705 return;
24706 }
24707 if( M<=2 ){
24708 Y--;
24709 M += 12;
24710 }
24711 A = (Y+4800)/100;
24712 B = 38 - A + (A/4);
24713 X1 = 36525*(Y+4716)/100;
24714 X2 = 306001*(M+1)/10000;
24715 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
24716 p->validJD = 1;
24717 if( p->validHMS ){
24718 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
24719 if( p->tz ){
24720 p->iJD -= p->tz*60000;
24721 p->validYMD = 0;
24722 p->validHMS = 0;
24723 p->tz = 0;
24724 p->isUtc = 1;
24725 p->isLocal = 0;
24726 }
24727 }
24728}
24729
24730/*
24731** Given the YYYY-MM-DD information current in p, determine if there
24732** is day-of-month overflow and set nFloor to the number of days that
24733** would need to be subtracted from the date in order to bring the
24734** date back to the end of the month.
24735*/
24736static void computeFloor(DateTime *p){
24737 assert( p->validYMD || p->isError )((void) (0));
24738 assert( p->D>=0 && p->D<=31 )((void) (0));
24739 assert( p->M>=0 && p->M<=12 )((void) (0));
24740 if( p->D<=28 ){
24741 p->nFloor = 0;
24742 }else if( (1<<p->M) & 0x15aa ){
24743 p->nFloor = 0;
24744 }else if( p->M!=2 ){
24745 p->nFloor = (p->D==31);
24746 }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){
24747 p->nFloor = p->D - 28;
24748 }else{
24749 p->nFloor = p->D - 29;
24750 }
24751}
24752
24753/*
24754** Parse dates of the form
24755**
24756** YYYY-MM-DD HH:MM:SS.FFF
24757** YYYY-MM-DD HH:MM:SS
24758** YYYY-MM-DD HH:MM
24759** YYYY-MM-DD
24760**
24761** Write the result into the DateTime structure and return 0
24762** on success and 1 if the input string is not a well-formed
24763** date.
24764*/
24765static int parseYyyyMmDd(const char *zDate, DateTime *p){
24766 int Y, M, D, neg;
24767
24768 if( zDate[0]=='-' ){
24769 zDate++;
24770 neg = 1;
24771 }else{
24772 neg = 0;
24773 }
24774 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
24775 return 1;
24776 }
24777 zDate += 10;
24778 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) || 'T'==*(u8*)zDate ){ zDate++; }
24779 if( parseHhMmSs(zDate, p)==0 ){
24780 /* We got the time */
24781 }else if( *zDate==0 ){
24782 p->validHMS = 0;
24783 }else{
24784 return 1;
24785 }
24786 p->validJD = 0;
24787 p->validYMD = 1;
24788 p->Y = neg ? -Y : Y;
24789 p->M = M;
24790 p->D = D;
24791 computeFloor(p);
24792 if( p->tz ){
24793 computeJD(p);
24794 }
24795 return 0;
24796}
24797
24798
24799static void clearYMD_HMS_TZ(DateTime *p); /* Forward declaration */
24800
24801/*
24802** Set the time to the current time reported by the VFS.
24803**
24804** Return the number of errors.
24805*/
24806static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
24807 p->iJD = sqlite3StmtCurrentTime(context);
24808 if( p->iJD>0 ){
24809 p->validJD = 1;
24810 p->isUtc = 1;
24811 p->isLocal = 0;
24812 clearYMD_HMS_TZ(p);
24813 return 0;
24814 }else{
24815 return 1;
24816 }
24817}
24818
24819/*
24820** Input "r" is a numeric quantity which might be a julian day number,
24821** or the number of seconds since 1970. If the value if r is within
24822** range of a julian day number, install it as such and set validJD.
24823** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
24824*/
24825static void setRawDateNumber(DateTime *p, double r){
24826 p->s = r;
24827 p->rawS = 1;
24828 if( r>=0.0 && r<5373484.5 ){
24829 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
24830 p->validJD = 1;
24831 }
24832}
24833
24834/*
24835** Attempt to parse the given string into a julian day number. Return
24836** the number of errors.
24837**
24838** The following are acceptable forms for the input string:
24839**
24840** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
24841** DDDD.DD
24842** now
24843**
24844** In the first form, the +/-HH:MM is always optional. The fractional
24845** seconds extension (the ".FFF") is optional. The seconds portion
24846** (":SS.FFF") is option. The year and date can be omitted as long
24847** as there is a time string. The time string can be omitted as long
24848** as there is a year and date.
24849*/
24850static int parseDateOrTime(
24851 sqlite3_context *context,
24852 const char *zDate,
24853 DateTime *p
24854){
24855 double r;
24856 if( parseYyyyMmDd(zDate,p)==0 ){
24857 return 0;
24858 }else if( parseHhMmSs(zDate, p)==0 ){
24859 return 0;
24860 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
24861 return setDateTimeToCurrent(context, p);
24862 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF81)>0 ){
24863 setRawDateNumber(p, r);
24864 return 0;
24865 }else if( (sqlite3StrICmp(zDate,"subsec")==0
24866 || sqlite3StrICmp(zDate,"subsecond")==0)
24867 && sqlite3NotPureFunc(context) ){
24868 p->useSubsec = 1;
24869 return setDateTimeToCurrent(context, p);
24870 }
24871 return 1;
24872}
24873
24874/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24875** Multiplying this by 86400000 gives 464269060799999 as the maximum value
24876** for DateTime.iJD.
24877**
24878** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
24879** such a large integer literal, so we have to encode it.
24880*/
24881#define INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff) ((((i64)0x1a640)<<32)|0x1072fdff)
24882
24883/*
24884** Return TRUE if the given julian day number is within range.
24885**
24886** The input is the JulianDay times 86400000.
24887*/
24888static int validJulianDay(sqlite3_int64 iJD){
24889 return iJD>=0 && iJD<=INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff);
24890}
24891
24892/*
24893** Compute the Year, Month, and Day from the julian day number.
24894*/
24895static void computeYMD(DateTime *p){
24896 int Z, alpha, A, B, C, D, E, X1;
24897 if( p->validYMD ) return;
24898 if( !p->validJD ){
24899 p->Y = 2000;
24900 p->M = 1;
24901 p->D = 1;
24902 }else if( !validJulianDay(p->iJD) ){
24903 datetimeError(p);
24904 return;
24905 }else{
24906 Z = (int)((p->iJD + 43200000)/86400000);
24907 alpha = (int)((Z + 32044.75)/36524.25) - 52;
24908 A = Z + 1 + alpha - ((alpha+100)/4) + 25;
24909 B = A + 1524;
24910 C = (int)((B - 122.1)/365.25);
24911 D = (36525*(C&32767))/100;
24912 E = (int)((B-D)/30.6001);
24913 X1 = (int)(30.6001*E);
24914 p->D = B - D - X1;
24915 p->M = E<14 ? E-1 : E-13;
24916 p->Y = p->M>2 ? C - 4716 : C - 4715;
24917 }
24918 p->validYMD = 1;
24919}
24920
24921/*
24922** Compute the Hour, Minute, and Seconds from the julian day number.
24923*/
24924static void computeHMS(DateTime *p){
24925 int day_ms, day_min; /* milliseconds, minutes into the day */
24926 if( p->validHMS ) return;
24927 computeJD(p);
24928 day_ms = (int)((p->iJD + 43200000) % 86400000);
24929 p->s = (day_ms % 60000)/1000.0;
24930 day_min = day_ms/60000;
24931 p->m = day_min % 60;
24932 p->h = day_min / 60;
24933 p->rawS = 0;
24934 p->validHMS = 1;
24935}
24936
24937/*
24938** Compute both YMD and HMS
24939*/
24940static void computeYMD_HMS(DateTime *p){
24941 computeYMD(p);
24942 computeHMS(p);
24943}
24944
24945/*
24946** Clear the YMD and HMS and the TZ
24947*/
24948static void clearYMD_HMS_TZ(DateTime *p){
24949 p->validYMD = 0;
24950 p->validHMS = 0;
24951 p->tz = 0;
24952}
24953
24954#ifndef SQLITE_OMIT_LOCALTIME
24955/*
24956** On recent Windows platforms, the localtime_s() function is available
24957** as part of the "Secure CRT". It is essentially equivalent to
24958** localtime_r() available under most POSIX platforms, except that the
24959** order of the parameters is reversed.
24960**
24961** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24962**
24963** If the user has not indicated to use localtime_r() or localtime_s()
24964** already, check for an MSVC build environment that provides
24965** localtime_s().
24966*/
24967#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
24968 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
24969#undef HAVE_LOCALTIME_S
24970#define HAVE_LOCALTIME_S 1
24971#endif
24972
24973/*
24974** The following routine implements the rough equivalent of localtime_r()
24975** using whatever operating-system specific localtime facility that
24976** is available. This routine returns 0 on success and
24977** non-zero on any kind of error.
24978**
24979** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24980** routine will always fail. If bLocaltimeFault is nonzero and
24981** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is
24982** invoked in place of the OS-defined localtime() function.
24983**
24984** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24985** library function localtime_r() is used to assist in the calculation of
24986** local time.
24987*/
24988static int osLocaltime(time_t *t, struct tm *pTm){
24989 int rc;
24990#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
24991 struct tm *pX;
24992#if SQLITE_THREADSAFE2>0
24993 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
24994#endif
24995 sqlite3_mutex_enter(mutex);
24996 pX = localtime(t);
24997#ifndef SQLITE_UNTESTABLE
24998 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ){
24999 if( sqlite3GlobalConfigsqlite3Config.xAltLocaltime!=0
25000 && 0==sqlite3GlobalConfigsqlite3Config.xAltLocaltime((const void*)t,(void*)pTm)
25001 ){
25002 pX = pTm;
25003 }else{
25004 pX = 0;
25005 }
25006 }
25007#endif
25008 if( pX ) *pTm = *pX;
25009#if SQLITE_THREADSAFE2>0
25010 sqlite3_mutex_leave(mutex);
25011#endif
25012 rc = pX==0;
25013#else
25014#ifndef SQLITE_UNTESTABLE
25015 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ){
25016 if( sqlite3GlobalConfigsqlite3Config.xAltLocaltime!=0 ){
25017 return sqlite3GlobalConfigsqlite3Config.xAltLocaltime((const void*)t,(void*)pTm);
25018 }else{
25019 return 1;
25020 }
25021 }
25022#endif
25023#if HAVE_LOCALTIME_R
25024 rc = localtime_r(t, pTm)==0;
25025#else
25026 rc = localtime_s(pTm, t);
25027#endif /* HAVE_LOCALTIME_R */
25028#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
25029 return rc;
25030}
25031#endif /* SQLITE_OMIT_LOCALTIME */
25032
25033
25034#ifndef SQLITE_OMIT_LOCALTIME
25035/*
25036** Assuming the input DateTime is UTC, move it to its localtime equivalent.
25037*/
25038static int toLocaltime(
25039 DateTime *p, /* Date at which to calculate offset */
25040 sqlite3_context *pCtx /* Write error here if one occurs */
25041){
25042 time_t t;
25043 struct tm sLocal;
25044 int iYearDiff;
25045
25046 /* Initialize the contents of sLocal to avoid a compiler warning. */
25047 memset(&sLocal, 0, sizeof(sLocal));
25048
25049 computeJD(p);
25050 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
25051 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
25052 ){
25053 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
25054 ** works for years between 1970 and 2037. For dates outside this range,
25055 ** SQLite attempts to map the year into an equivalent year within this
25056 ** range, do the calculation, then map the year back.
25057 */
25058 DateTime x = *p;
25059 computeYMD_HMS(&x);
25060 iYearDiff = (2000 + x.Y%4) - x.Y;
25061 x.Y += iYearDiff;
25062 x.validJD = 0;
25063 computeJD(&x);
25064 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
25065 }else{
25066 iYearDiff = 0;
25067 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
25068 }
25069 if( osLocaltime(&t, &sLocal) ){
25070 sqlite3_result_error(pCtx, "local time unavailable", -1);
25071 return SQLITE_ERROR1;
25072 }
25073 p->Y = sLocal.tm_year + 1900 - iYearDiff;
25074 p->M = sLocal.tm_mon + 1;
25075 p->D = sLocal.tm_mday;
25076 p->h = sLocal.tm_hour;
25077 p->m = sLocal.tm_min;
25078 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
25079 p->validYMD = 1;
25080 p->validHMS = 1;
25081 p->validJD = 0;
25082 p->rawS = 0;
25083 p->tz = 0;
25084 p->isError = 0;
25085 return SQLITE_OK0;
25086}
25087#endif /* SQLITE_OMIT_LOCALTIME */
25088
25089/*
25090** The following table defines various date transformations of the form
25091**
25092** 'NNN days'
25093**
25094** Where NNN is an arbitrary floating-point number and "days" can be one
25095** of several units of time.
25096*/
25097static const struct {
25098 u8 nName; /* Length of the name */
25099 char zName[7]; /* Name of the transformation */
25100 float rLimit; /* Maximum NNN value for this transform */
25101 float rXform; /* Constant used for this transform */
25102} aXformType[] = {
25103 /* 0 */ { 6, "second", 4.6427e+14, 1.0 },
25104 /* 1 */ { 6, "minute", 7.7379e+12, 60.0 },
25105 /* 2 */ { 4, "hour", 1.2897e+11, 3600.0 },
25106 /* 3 */ { 3, "day", 5373485.0, 86400.0 },
25107 /* 4 */ { 5, "month", 176546.0, 2592000.0 },
25108 /* 5 */ { 4, "year", 14713.0, 31536000.0 },
25109};
25110
25111/*
25112** If the DateTime p is raw number, try to figure out if it is
25113** a julian day number of a unix timestamp. Set the p value
25114** appropriately.
25115*/
25116static void autoAdjustDate(DateTime *p){
25117 if( !p->rawS || p->validJD ){
25118 p->rawS = 0;
25119 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
25120 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
25121 ){
25122 double r = p->s*1000.0 + 210866760000000.0;
25123 clearYMD_HMS_TZ(p);
25124 p->iJD = (sqlite3_int64)(r + 0.5);
25125 p->validJD = 1;
25126 p->rawS = 0;
25127 }
25128}
25129
25130/*
25131** Process a modifier to a date-time stamp. The modifiers are
25132** as follows:
25133**
25134** NNN days
25135** NNN hours
25136** NNN minutes
25137** NNN.NNNN seconds
25138** NNN months
25139** NNN years
25140** +/-YYYY-MM-DD HH:MM:SS.SSS
25141** ceiling
25142** floor
25143** start of month
25144** start of year
25145** start of week
25146** start of day
25147** weekday N
25148** unixepoch
25149** auto
25150** localtime
25151** utc
25152** subsec
25153** subsecond
25154**
25155** Return 0 on success and 1 if there is any kind of error. If the error
25156** is in a system call (i.e. localtime()), then an error message is written
25157** to context pCtx. If the error is an unrecognized modifier, no error is
25158** written to pCtx.
25159*/
25160static int parseModifier(
25161 sqlite3_context *pCtx, /* Function context */
25162 const char *z, /* The text of the modifier */
25163 int n, /* Length of zMod in bytes */
25164 DateTime *p, /* The date/time value to be modified */
25165 int idx /* Parameter index of the modifier */
25166){
25167 int rc = 1;
25168 double r;
25169 switch(sqlite3UpperToLower[(u8)z[0]] ){
25170 case 'a': {
25171 /*
25172 ** auto
25173 **
25174 ** If rawS is available, then interpret as a julian day number, or
25175 ** a unix timestamp, depending on its magnitude.
25176 */
25177 if( sqlite3_stricmp(z, "auto")==0 ){
25178 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
25179 autoAdjustDate(p);
25180 rc = 0;
25181 }
25182 break;
25183 }
25184 case 'c': {
25185 /*
25186 ** ceiling
25187 **
25188 ** Resolve day-of-month overflow by rolling forward into the next
25189 ** month. As this is the default action, this modifier is really
25190 ** a no-op that is only included for symmetry. See "floor".
25191 */
25192 if( sqlite3_stricmp(z, "ceiling")==0 ){
25193 computeJD(p);
25194 clearYMD_HMS_TZ(p);
25195 rc = 0;
25196 p->nFloor = 0;
25197 }
25198 break;
25199 }
25200 case 'f': {
25201 /*
25202 ** floor
25203 **
25204 ** Resolve day-of-month overflow by rolling back to the end of the
25205 ** previous month.
25206 */
25207 if( sqlite3_stricmp(z, "floor")==0 ){
25208 computeJD(p);
25209 p->iJD -= p->nFloor*86400000;
25210 clearYMD_HMS_TZ(p);
25211 rc = 0;
25212 }
25213 break;
25214 }
25215 case 'j': {
25216 /*
25217 ** julianday
25218 **
25219 ** Always interpret the prior number as a julian-day value. If this
25220 ** is not the first modifier, or if the prior argument is not a numeric
25221 ** value in the allowed range of julian day numbers understood by
25222 ** SQLite (0..5373484.5) then the result will be NULL.
25223 */
25224 if( sqlite3_stricmp(z, "julianday")==0 ){
25225 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
25226 if( p->validJD && p->rawS ){
25227 rc = 0;
25228 p->rawS = 0;
25229 }
25230 }
25231 break;
25232 }
25233#ifndef SQLITE_OMIT_LOCALTIME
25234 case 'l': {
25235 /* localtime
25236 **
25237 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
25238 ** show local time.
25239 */
25240 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
25241 rc = p->isLocal ? SQLITE_OK0 : toLocaltime(p, pCtx);
25242 p->isUtc = 0;
25243 p->isLocal = 1;
25244 }
25245 break;
25246 }
25247#endif
25248 case 'u': {
25249 /*
25250 ** unixepoch
25251 **
25252 ** Treat the current value of p->s as the number of
25253 ** seconds since 1970. Convert to a real julian day number.
25254 */
25255 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
25256 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
25257 r = p->s*1000.0 + 210866760000000.0;
25258 if( r>=0.0 && r<464269060800000.0 ){
25259 clearYMD_HMS_TZ(p);
25260 p->iJD = (sqlite3_int64)(r + 0.5);
25261 p->validJD = 1;
25262 p->rawS = 0;
25263 rc = 0;
25264 }
25265 }
25266#ifndef SQLITE_OMIT_LOCALTIME
25267 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
25268 if( p->isUtc==0 ){
25269 i64 iOrigJD; /* Original localtime */
25270 i64 iGuess; /* Guess at the corresponding utc time */
25271 int cnt = 0; /* Safety to prevent infinite loop */
25272 i64 iErr; /* Guess is off by this much */
25273
25274 computeJD(p);
25275 iGuess = iOrigJD = p->iJD;
25276 iErr = 0;
25277 do{
25278 DateTime new;
25279 memset(&new, 0, sizeof(new));
25280 iGuess -= iErr;
25281 new.iJD = iGuess;
25282 new.validJD = 1;
25283 rc = toLocaltime(&new, pCtx);
25284 if( rc ) return rc;
25285 computeJD(&new);
25286 iErr = new.iJD - iOrigJD;
25287 }while( iErr && cnt++<3 );
25288 memset(p, 0, sizeof(*p));
25289 p->iJD = iGuess;
25290 p->validJD = 1;
25291 p->isUtc = 1;
25292 p->isLocal = 0;
25293 }
25294 rc = SQLITE_OK0;
25295 }
25296#endif
25297 break;
25298 }
25299 case 'w': {
25300 /*
25301 ** weekday N
25302 **
25303 ** Move the date to the same time on the next occurrence of
25304 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
25305 ** date is already on the appropriate weekday, this is a no-op.
25306 */
25307 if( sqlite3_strnicmp(z, "weekday ", 8)==0
25308 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF81)>0
25309 && r>=0.0 && r<7.0 && (n=(int)r)==r ){
25310 sqlite3_int64 Z;
25311 computeYMD_HMS(p);
25312 p->tz = 0;
25313 p->validJD = 0;
25314 computeJD(p);
25315 Z = ((p->iJD + 129600000)/86400000) % 7;
25316 if( Z>n ) Z -= 7;
25317 p->iJD += (n - Z)*86400000;
25318 clearYMD_HMS_TZ(p);
25319 rc = 0;
25320 }
25321 break;
25322 }
25323 case 's': {
25324 /*
25325 ** start of TTTTT
25326 **
25327 ** Move the date backwards to the beginning of the current day,
25328 ** or month or year.
25329 **
25330 ** subsecond
25331 ** subsec
25332 **
25333 ** Show subsecond precision in the output of datetime() and
25334 ** unixepoch() and strftime('%s').
25335 */
25336 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ){
25337 if( sqlite3_stricmp(z, "subsec")==0
25338 || sqlite3_stricmp(z, "subsecond")==0
25339 ){
25340 p->useSubsec = 1;
25341 rc = 0;
25342 }
25343 break;
25344 }
25345 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
25346 z += 9;
25347 computeYMD(p);
25348 p->validHMS = 1;
25349 p->h = p->m = 0;
25350 p->s = 0.0;
25351 p->rawS = 0;
25352 p->tz = 0;
25353 p->validJD = 0;
25354 if( sqlite3_stricmp(z,"month")==0 ){
25355 p->D = 1;
25356 rc = 0;
25357 }else if( sqlite3_stricmp(z,"year")==0 ){
25358 p->M = 1;
25359 p->D = 1;
25360 rc = 0;
25361 }else if( sqlite3_stricmp(z,"day")==0 ){
25362 rc = 0;
25363 }
25364 break;
25365 }
25366 case '+':
25367 case '-':
25368 case '0':
25369 case '1':
25370 case '2':
25371 case '3':
25372 case '4':
25373 case '5':
25374 case '6':
25375 case '7':
25376 case '8':
25377 case '9': {
25378 double rRounder;
25379 int i;
25380 int Y,M,D,h,m,x;
25381 const char *z2 = z;
25382 char z0 = z[0];
25383 for(n=1; z[n]; n++){
25384 if( z[n]==':' ) break;
25385 if( sqlite3Isspace(z[n])(sqlite3CtypeMap[(unsigned char)(z[n])]&0x01) ) break;
25386 if( z[n]=='-' ){
25387 if( n==5 && getDigits(&z[1], "40f", &Y)==1 ) break;
25388 if( n==6 && getDigits(&z[1], "50f", &Y)==1 ) break;
25389 }
25390 }
25391 if( sqlite3AtoF(z, &r, n, SQLITE_UTF81)<=0 ){
25392 assert( rc==1 )((void) (0));
25393 break;
25394 }
25395 if( z[n]=='-' ){
25396 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the
25397 ** specified number of years, months, and days. MM is limited to
25398 ** the range 0-11 and DD is limited to 0-30.
25399 */
25400 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */
25401 if( n==5 ){
25402 if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break;
25403 }else{
25404 assert( n==6 )((void) (0));
25405 if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break;
25406 z++;
25407 }
25408 if( M>=12 ) break; /* M range 0..11 */
25409 if( D>=31 ) break; /* D range 0..30 */
25410 computeYMD_HMS(p);
25411 p->validJD = 0;
25412 if( z0=='-' ){
25413 p->Y -= Y;
25414 p->M -= M;
25415 D = -D;
25416 }else{
25417 p->Y += Y;
25418 p->M += M;
25419 }
25420 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25421 p->Y += x;
25422 p->M -= x*12;
25423 computeFloor(p);
25424 computeJD(p);
25425 p->validHMS = 0;
25426 p->validYMD = 0;
25427 p->iJD += (i64)D*86400000;
25428 if( z[11]==0 ){
25429 rc = 0;
25430 break;
25431 }
25432 if( sqlite3Isspace(z[11])(sqlite3CtypeMap[(unsigned char)(z[11])]&0x01)
25433 && getDigits(&z[12], "20c:20e", &h, &m)==2
25434 ){
25435 z2 = &z[12];
25436 n = 2;
25437 }else{
25438 break;
25439 }
25440 }
25441 if( z2[n]==':' ){
25442 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
25443 ** specified number of hours, minutes, seconds, and fractional seconds
25444 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
25445 ** omitted.
25446 */
25447
25448 DateTime tx;
25449 sqlite3_int64 day;
25450 if( !sqlite3Isdigit(*z2)(sqlite3CtypeMap[(unsigned char)(*z2)]&0x04) ) z2++;
25451 memset(&tx, 0, sizeof(tx));
25452 if( parseHhMmSs(z2, &tx) ) break;
25453 computeJD(&tx);
25454 tx.iJD -= 43200000;
25455 day = tx.iJD/86400000;
25456 tx.iJD -= day*86400000;
25457 if( z0=='-' ) tx.iJD = -tx.iJD;
25458 computeJD(p);
25459 clearYMD_HMS_TZ(p);
25460 p->iJD += tx.iJD;
25461 rc = 0;
25462 break;
25463 }
25464
25465 /* If control reaches this point, it means the transformation is
25466 ** one of the forms like "+NNN days". */
25467 z += n;
25468 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
25469 n = sqlite3Strlen30(z);
25470 if( n<3 || n>10 ) break;
25471 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
25472 computeJD(p);
25473 assert( rc==1 )((void) (0));
25474 rRounder = r<0 ? -0.5 : +0.5;
25475 p->nFloor = 0;
25476 for(i=0; i<ArraySize(aXformType)((int)(sizeof(aXformType)/sizeof(aXformType[0]))); i++){
25477 if( aXformType[i].nName==n
25478 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
25479 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
25480 ){
25481 switch( i ){
25482 case 4: { /* Special processing to add months */
25483 assert( strcmp(aXformType[4].zName,"month")==0 )((void) (0));
25484 computeYMD_HMS(p);
25485 p->M += (int)r;
25486 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25487 p->Y += x;
25488 p->M -= x*12;
25489 computeFloor(p);
25490 p->validJD = 0;
25491 r -= (int)r;
25492 break;
25493 }
25494 case 5: { /* Special processing to add years */
25495 int y = (int)r;
25496 assert( strcmp(aXformType[5].zName,"year")==0 )((void) (0));
25497 computeYMD_HMS(p);
25498 assert( p->M>=0 && p->M<=12 )((void) (0));
25499 p->Y += y;
25500 computeFloor(p);
25501 p->validJD = 0;
25502 r -= (int)r;
25503 break;
25504 }
25505 }
25506 computeJD(p);
25507 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
25508 rc = 0;
25509 break;
25510 }
25511 }
25512 clearYMD_HMS_TZ(p);
25513 break;
25514 }
25515 default: {
25516 break;
25517 }
25518 }
25519 return rc;
25520}
25521
25522/*
25523** Process time function arguments. argv[0] is a date-time stamp.
25524** argv[1] and following are modifiers. Parse them all and write
25525** the resulting time into the DateTime structure p. Return 0
25526** on success and 1 if there are any errors.
25527**
25528** If there are zero parameters (if even argv[0] is undefined)
25529** then assume a default value of "now" for argv[0].
25530*/
25531static int isDate(
25532 sqlite3_context *context,
25533 int argc,
25534 sqlite3_value **argv,
25535 DateTime *p
25536){
25537 int i, n;
25538 const unsigned char *z;
25539 int eType;
25540 memset(p, 0, sizeof(*p));
25541 if( argc==0 ){
25542 if( !sqlite3NotPureFunc(context) ) return 1;
25543 return setDateTimeToCurrent(context, p);
25544 }
25545 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT2
25546 || eType==SQLITE_INTEGER1 ){
25547 setRawDateNumber(p, sqlite3_value_double(argv[0]));
25548 }else{
25549 z = sqlite3_value_text(argv[0]);
25550 if( !z || parseDateOrTime(context, (char*)z, p) ){
25551 return 1;
25552 }
25553 }
25554 for(i=1; i<argc; i++){
25555 z = sqlite3_value_text(argv[i]);
25556 n = sqlite3_value_bytes(argv[i]);
25557 if( z==0 || parseModifier(context, (char*)z, n, p, i) ) return 1;
25558 }
25559 computeJD(p);
25560 if( p->isError || !validJulianDay(p->iJD) ) return 1;
25561 if( argc==1 && p->validYMD && p->D>28 ){
25562 /* Make sure a YYYY-MM-DD is normalized.
25563 ** Example: 2023-02-31 -> 2023-03-03 */
25564 assert( p->validJD )((void) (0));
25565 p->validYMD = 0;
25566 }
25567 return 0;
25568}
25569
25570
25571/*
25572** The following routines implement the various date and time functions
25573** of SQLite.
25574*/
25575
25576/*
25577** julianday( TIMESTRING, MOD, MOD, ...)
25578**
25579** Return the julian day number of the date specified in the arguments
25580*/
25581static void juliandayFunc(
25582 sqlite3_context *context,
25583 int argc,
25584 sqlite3_value **argv
25585){
25586 DateTime x;
25587 if( isDate(context, argc, argv, &x)==0 ){
25588 computeJD(&x);
25589 sqlite3_result_double(context, x.iJD/86400000.0);
25590 }
25591}
25592
25593/*
25594** unixepoch( TIMESTRING, MOD, MOD, ...)
25595**
25596** Return the number of seconds (including fractional seconds) since
25597** the unix epoch of 1970-01-01 00:00:00 GMT.
25598*/
25599static void unixepochFunc(
25600 sqlite3_context *context,
25601 int argc,
25602 sqlite3_value **argv
25603){
25604 DateTime x;
25605 if( isDate(context, argc, argv, &x)==0 ){
25606 computeJD(&x);
25607 if( x.useSubsec ){
25608 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0);
25609 }else{
25610 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
25611 }
25612 }
25613}
25614
25615/*
25616** datetime( TIMESTRING, MOD, MOD, ...)
25617**
25618** Return YYYY-MM-DD HH:MM:SS
25619*/
25620static void datetimeFunc(
25621 sqlite3_context *context,
25622 int argc,
25623 sqlite3_value **argv
25624){
25625 DateTime x;
25626 if( isDate(context, argc, argv, &x)==0 ){
25627 int Y, s, n;
25628 char zBuf[32];
25629 computeYMD_HMS(&x);
25630 Y = x.Y;
25631 if( Y<0 ) Y = -Y;
25632 zBuf[1] = '0' + (Y/1000)%10;
25633 zBuf[2] = '0' + (Y/100)%10;
25634 zBuf[3] = '0' + (Y/10)%10;
25635 zBuf[4] = '0' + (Y)%10;
25636 zBuf[5] = '-';
25637 zBuf[6] = '0' + (x.M/10)%10;
25638 zBuf[7] = '0' + (x.M)%10;
25639 zBuf[8] = '-';
25640 zBuf[9] = '0' + (x.D/10)%10;
25641 zBuf[10] = '0' + (x.D)%10;
25642 zBuf[11] = ' ';
25643 zBuf[12] = '0' + (x.h/10)%10;
25644 zBuf[13] = '0' + (x.h)%10;
25645 zBuf[14] = ':';
25646 zBuf[15] = '0' + (x.m/10)%10;
25647 zBuf[16] = '0' + (x.m)%10;
25648 zBuf[17] = ':';
25649 if( x.useSubsec ){
25650 s = (int)(1000.0*x.s + 0.5);
25651 zBuf[18] = '0' + (s/10000)%10;
25652 zBuf[19] = '0' + (s/1000)%10;
25653 zBuf[20] = '.';
25654 zBuf[21] = '0' + (s/100)%10;
25655 zBuf[22] = '0' + (s/10)%10;
25656 zBuf[23] = '0' + (s)%10;
25657 zBuf[24] = 0;
25658 n = 24;
25659 }else{
25660 s = (int)x.s;
25661 zBuf[18] = '0' + (s/10)%10;
25662 zBuf[19] = '0' + (s)%10;
25663 zBuf[20] = 0;
25664 n = 20;
25665 }
25666 if( x.Y<0 ){
25667 zBuf[0] = '-';
25668 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25669 }else{
25670 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25671 }
25672 }
25673}
25674
25675/*
25676** time( TIMESTRING, MOD, MOD, ...)
25677**
25678** Return HH:MM:SS
25679*/
25680static void timeFunc(
25681 sqlite3_context *context,
25682 int argc,
25683 sqlite3_value **argv
25684){
25685 DateTime x;
25686 if( isDate(context, argc, argv, &x)==0 ){
25687 int s, n;
25688 char zBuf[16];
25689 computeHMS(&x);
25690 zBuf[0] = '0' + (x.h/10)%10;
25691 zBuf[1] = '0' + (x.h)%10;
25692 zBuf[2] = ':';
25693 zBuf[3] = '0' + (x.m/10)%10;
25694 zBuf[4] = '0' + (x.m)%10;
25695 zBuf[5] = ':';
25696 if( x.useSubsec ){
25697 s = (int)(1000.0*x.s + 0.5);
25698 zBuf[6] = '0' + (s/10000)%10;
25699 zBuf[7] = '0' + (s/1000)%10;
25700 zBuf[8] = '.';
25701 zBuf[9] = '0' + (s/100)%10;
25702 zBuf[10] = '0' + (s/10)%10;
25703 zBuf[11] = '0' + (s)%10;
25704 zBuf[12] = 0;
25705 n = 12;
25706 }else{
25707 s = (int)x.s;
25708 zBuf[6] = '0' + (s/10)%10;
25709 zBuf[7] = '0' + (s)%10;
25710 zBuf[8] = 0;
25711 n = 8;
25712 }
25713 sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25714 }
25715}
25716
25717/*
25718** date( TIMESTRING, MOD, MOD, ...)
25719**
25720** Return YYYY-MM-DD
25721*/
25722static void dateFunc(
25723 sqlite3_context *context,
25724 int argc,
25725 sqlite3_value **argv
25726){
25727 DateTime x;
25728 if( isDate(context, argc, argv, &x)==0 ){
25729 int Y;
25730 char zBuf[16];
25731 computeYMD(&x);
25732 Y = x.Y;
25733 if( Y<0 ) Y = -Y;
25734 zBuf[1] = '0' + (Y/1000)%10;
25735 zBuf[2] = '0' + (Y/100)%10;
25736 zBuf[3] = '0' + (Y/10)%10;
25737 zBuf[4] = '0' + (Y)%10;
25738 zBuf[5] = '-';
25739 zBuf[6] = '0' + (x.M/10)%10;
25740 zBuf[7] = '0' + (x.M)%10;
25741 zBuf[8] = '-';
25742 zBuf[9] = '0' + (x.D/10)%10;
25743 zBuf[10] = '0' + (x.D)%10;
25744 zBuf[11] = 0;
25745 if( x.Y<0 ){
25746 zBuf[0] = '-';
25747 sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25748 }else{
25749 sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
25750 }
25751 }
25752}
25753
25754/*
25755** Compute the number of days after the most recent January 1.
25756**
25757** In other words, compute the zero-based day number for the
25758** current year:
25759**
25760** Jan01 = 0, Jan02 = 1, ..., Jan31 = 30, Feb01 = 31, ...
25761** Dec31 = 364 or 365.
25762*/
25763static int daysAfterJan01(DateTime *pDate){
25764 DateTime jan01 = *pDate;
25765 assert( jan01.validYMD )((void) (0));
25766 assert( jan01.validHMS )((void) (0));
25767 assert( pDate->validJD )((void) (0));
25768 jan01.validJD = 0;
25769 jan01.M = 1;
25770 jan01.D = 1;
25771 computeJD(&jan01);
25772 return (int)((pDate->iJD-jan01.iJD+43200000)/86400000);
25773}
25774
25775/*
25776** Return the number of days after the most recent Monday.
25777**
25778** In other words, return the day of the week according
25779** to this code:
25780**
25781** 0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday.
25782*/
25783static int daysAfterMonday(DateTime *pDate){
25784 assert( pDate->validJD )((void) (0));
25785 return (int)((pDate->iJD+43200000)/86400000) % 7;
25786}
25787
25788/*
25789** Return the number of days after the most recent Sunday.
25790**
25791** In other words, return the day of the week according
25792** to this code:
25793**
25794** 0=Sunday, 1=Monday, 2=Tues, ..., 6=Saturday
25795*/
25796static int daysAfterSunday(DateTime *pDate){
25797 assert( pDate->validJD )((void) (0));
25798 return (int)((pDate->iJD+129600000)/86400000) % 7;
25799}
25800
25801/*
25802** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
25803**
25804** Return a string described by FORMAT. Conversions as follows:
25805**
25806** %d day of month 01-31
25807** %e day of month 1-31
25808** %f ** fractional seconds SS.SSS
25809** %F ISO date. YYYY-MM-DD
25810** %G ISO year corresponding to %V 0000-9999.
25811** %g 2-digit ISO year corresponding to %V 00-99
25812** %H hour 00-24
25813** %k hour 0-24 (leading zero converted to space)
25814** %I hour 01-12
25815** %j day of year 001-366
25816** %J ** julian day number
25817** %l hour 1-12 (leading zero converted to space)
25818** %m month 01-12
25819** %M minute 00-59
25820** %p "am" or "pm"
25821** %P "AM" or "PM"
25822** %R time as HH:MM
25823** %s seconds since 1970-01-01
25824** %S seconds 00-59
25825** %T time as HH:MM:SS
25826** %u day of week 1-7 Monday==1, Sunday==7
25827** %w day of week 0-6 Sunday==0, Monday==1
25828** %U week of year 00-53 (First Sunday is start of week 01)
25829** %V week of year 01-53 (First week containing Thursday is week 01)
25830** %W week of year 00-53 (First Monday is start of week 01)
25831** %Y year 0000-9999
25832** %% %
25833*/
25834static void strftimeFunc(
25835 sqlite3_context *context,
25836 int argc,
25837 sqlite3_value **argv
25838){
25839 DateTime x;
25840 size_t i,j;
25841 sqlite3 *db;
25842 const char *zFmt;
25843 sqlite3_str sRes;
25844
25845
25846 if( argc==0 ) return;
25847 zFmt = (const char*)sqlite3_value_text(argv[0]);
25848 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
25849 db = sqlite3_context_db_handle(context);
25850 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
25851
25852 computeJD(&x);
25853 computeYMD_HMS(&x);
25854 for(i=j=0; zFmt[i]; i++){
25855 char cf;
25856 if( zFmt[i]!='%' ) continue;
25857 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
25858 i++;
25859 j = i + 1;
25860 cf = zFmt[i];
25861 switch( cf ){
25862 case 'd': /* Fall thru */
25863 case 'e': {
25864 sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D);
25865 break;
25866 }
25867 case 'f': { /* Fractional seconds. (Non-standard) */
25868 double s = x.s;
25869 if( s>59.999 ) s = 59.999;
25870 sqlite3_str_appendf(&sRes, "%06.3f", s);
25871 break;
25872 }
25873 case 'F': {
25874 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D);
25875 break;
25876 }
25877 case 'G': /* Fall thru */
25878 case 'g': {
25879 DateTime y = x;
25880 assert( y.validJD )((void) (0));
25881 /* Move y so that it is the Thursday in the same week as x */
25882 y.iJD += (3 - daysAfterMonday(&x))*86400000;
25883 y.validYMD = 0;
25884 computeYMD(&y);
25885 if( cf=='g' ){
25886 sqlite3_str_appendf(&sRes, "%02d", y.Y%100);
25887 }else{
25888 sqlite3_str_appendf(&sRes, "%04d", y.Y);
25889 }
25890 break;
25891 }
25892 case 'H':
25893 case 'k': {
25894 sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h);
25895 break;
25896 }
25897 case 'I': /* Fall thru */
25898 case 'l': {
25899 int h = x.h;
25900 if( h>12 ) h -= 12;
25901 if( h==0 ) h = 12;
25902 sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h);
25903 break;
25904 }
25905 case 'j': { /* Day of year. Jan01==1, Jan02==2, and so forth */
25906 sqlite3_str_appendf(&sRes,"%03d",daysAfterJan01(&x)+1);
25907 break;
25908 }
25909 case 'J': { /* Julian day number. (Non-standard) */
25910 sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0);
25911 break;
25912 }
25913 case 'm': {
25914 sqlite3_str_appendf(&sRes,"%02d",x.M);
25915 break;
25916 }
25917 case 'M': {
25918 sqlite3_str_appendf(&sRes,"%02d",x.m);
25919 break;
25920 }
25921 case 'p': /* Fall thru */
25922 case 'P': {
25923 if( x.h>=12 ){
25924 sqlite3_str_append(&sRes, cf=='p' ? "PM" : "pm", 2);
25925 }else{
25926 sqlite3_str_append(&sRes, cf=='p' ? "AM" : "am", 2);
25927 }
25928 break;
25929 }
25930 case 'R': {
25931 sqlite3_str_appendf(&sRes, "%02d:%02d", x.h, x.m);
25932 break;
25933 }
25934 case 's': {
25935 if( x.useSubsec ){
25936 sqlite3_str_appendf(&sRes,"%.3f",
25937 (x.iJD - 21086676*(i64)10000000)/1000.0);
25938 }else{
25939 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
25940 sqlite3_str_appendf(&sRes,"%lld",iS);
25941 }
25942 break;
25943 }
25944 case 'S': {
25945 sqlite3_str_appendf(&sRes,"%02d",(int)x.s);
25946 break;
25947 }
25948 case 'T': {
25949 sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s);
25950 break;
25951 }
25952 case 'u': /* Day of week. 1 to 7. Monday==1, Sunday==7 */
25953 case 'w': { /* Day of week. 0 to 6. Sunday==0, Monday==1 */
25954 char c = (char)daysAfterSunday(&x) + '0';
25955 if( c=='0' && cf=='u' ) c = '7';
25956 sqlite3_str_appendchar(&sRes, 1, c);
25957 break;
25958 }
25959 case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */
25960 sqlite3_str_appendf(&sRes,"%02d",
25961 (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7);
25962 break;
25963 }
25964 case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */
25965 DateTime y = x;
25966 /* Adjust y so that is the Thursday in the same week as x */
25967 assert( y.validJD )((void) (0));
25968 y.iJD += (3 - daysAfterMonday(&x))*86400000;
25969 y.validYMD = 0;
25970 computeYMD(&y);
25971 sqlite3_str_appendf(&sRes,"%02d", daysAfterJan01(&y)/7+1);
25972 break;
25973 }
25974 case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */
25975 sqlite3_str_appendf(&sRes,"%02d",
25976 (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7);
25977 break;
25978 }
25979 case 'Y': {
25980 sqlite3_str_appendf(&sRes,"%04d",x.Y);
25981 break;
25982 }
25983 case '%': {
25984 sqlite3_str_appendchar(&sRes, 1, '%');
25985 break;
25986 }
25987 default: {
25988 sqlite3_str_reset(&sRes);
25989 return;
25990 }
25991 }
25992 }
25993 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
25994 sqlite3ResultStrAccum(context, &sRes);
25995}
25996
25997/*
25998** current_time()
25999**
26000** This function returns the same value as time('now').
26001*/
26002static void ctimeFunc(
26003 sqlite3_context *context,
26004 int NotUsed,
26005 sqlite3_value **NotUsed2
26006){
26007 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
26008 timeFunc(context, 0, 0);
26009}
26010
26011/*
26012** current_date()
26013**
26014** This function returns the same value as date('now').
26015*/
26016static void cdateFunc(
26017 sqlite3_context *context,
26018 int NotUsed,
26019 sqlite3_value **NotUsed2
26020){
26021 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
26022 dateFunc(context, 0, 0);
26023}
26024
26025/*
26026** timediff(DATE1, DATE2)
26027**
26028** Return the amount of time that must be added to DATE2 in order to
26029** convert it into DATE2. The time difference format is:
26030**
26031** +YYYY-MM-DD HH:MM:SS.SSS
26032**
26033** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
26034** date/time values A and B, the following invariant should hold:
26035**
26036** datetime(A) == (datetime(B, timediff(A,B))
26037**
26038** Both DATE arguments must be either a julian day number, or an
26039** ISO-8601 string. The unix timestamps are not supported by this
26040** routine.
26041*/
26042static void timediffFunc(
26043 sqlite3_context *context,
26044 int NotUsed1,
26045 sqlite3_value **argv
26046){
26047 char sign;
26048 int Y, M;
26049 DateTime d1, d2;
26050 sqlite3_str sRes;
26051 UNUSED_PARAMETER(NotUsed1)(void)(NotUsed1);
26052 if( isDate(context, 1, &argv[0], &d1) ) return;
26053 if( isDate(context, 1, &argv[1], &d2) ) return;
26054 computeYMD_HMS(&d1);
26055 computeYMD_HMS(&d2);
26056 if( d1.iJD>=d2.iJD ){
26057 sign = '+';
26058 Y = d1.Y - d2.Y;
26059 if( Y ){
26060 d2.Y = d1.Y;
26061 d2.validJD = 0;
26062 computeJD(&d2);
26063 }
26064 M = d1.M - d2.M;
26065 if( M<0 ){
26066 Y--;
26067 M += 12;
26068 }
26069 if( M!=0 ){
26070 d2.M = d1.M;
26071 d2.validJD = 0;
26072 computeJD(&d2);
26073 }
26074 while( d1.iJD<d2.iJD ){
26075 M--;
26076 if( M<0 ){
26077 M = 11;
26078 Y--;
26079 }
26080 d2.M--;
26081 if( d2.M<1 ){
26082 d2.M = 12;
26083 d2.Y--;
26084 }
26085 d2.validJD = 0;
26086 computeJD(&d2);
26087 }
26088 d1.iJD -= d2.iJD;
26089 d1.iJD += (u64)1486995408 * (u64)100000;
26090 }else /* d1<d2 */{
26091 sign = '-';
26092 Y = d2.Y - d1.Y;
26093 if( Y ){
26094 d2.Y = d1.Y;
26095 d2.validJD = 0;
26096 computeJD(&d2);
26097 }
26098 M = d2.M - d1.M;
26099 if( M<0 ){
26100 Y--;
26101 M += 12;
26102 }
26103 if( M!=0 ){
26104 d2.M = d1.M;
26105 d2.validJD = 0;
26106 computeJD(&d2);
26107 }
26108 while( d1.iJD>d2.iJD ){
26109 M--;
26110 if( M<0 ){
26111 M = 11;
26112 Y--;
26113 }
26114 d2.M++;
26115 if( d2.M>12 ){
26116 d2.M = 1;
26117 d2.Y++;
26118 }
26119 d2.validJD = 0;
26120 computeJD(&d2);
26121 }
26122 d1.iJD = d2.iJD - d1.iJD;
26123 d1.iJD += (u64)1486995408 * (u64)100000;
26124 }
26125 clearYMD_HMS_TZ(&d1);
26126 computeYMD_HMS(&d1);
26127 sqlite3StrAccumInit(&sRes, 0, 0, 0, 100);
26128 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f",
26129 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s);
26130 sqlite3ResultStrAccum(context, &sRes);
26131}
26132
26133
26134/*
26135** current_timestamp()
26136**
26137** This function returns the same value as datetime('now').
26138*/
26139static void ctimestampFunc(
26140 sqlite3_context *context,
26141 int NotUsed,
26142 sqlite3_value **NotUsed2
26143){
26144 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
26145 datetimeFunc(context, 0, 0);
26146}
26147#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
26148
26149#ifdef SQLITE_OMIT_DATETIME_FUNCS
26150/*
26151** If the library is compiled to omit the full-scale date and time
26152** handling (to get a smaller binary), the following minimal version
26153** of the functions current_time(), current_date() and current_timestamp()
26154** are included instead. This is to support column declarations that
26155** include "DEFAULT CURRENT_TIME" etc.
26156**
26157** This function uses the C-library functions time(), gmtime()
26158** and strftime(). The format string to pass to strftime() is supplied
26159** as the user-data for the function.
26160*/
26161static void currentTimeFunc(
26162 sqlite3_context *context,
26163 int argc,
26164 sqlite3_value **argv
26165){
26166 time_t t;
26167 char *zFormat = (char *)sqlite3_user_data(context);
26168 sqlite3_int64 iT;
26169 struct tm *pTm;
26170 struct tm sNow;
26171 char zBuf[20];
26172
26173 UNUSED_PARAMETER(argc)(void)(argc);
26174 UNUSED_PARAMETER(argv)(void)(argv);
26175
26176 iT = sqlite3StmtCurrentTime(context);
26177 if( iT<=0 ) return;
26178 t = iT/1000 - 10000*(sqlite3_int64)21086676;
26179#if HAVE_GMTIME_R
26180 pTm = gmtime_r(&t, &sNow);
26181#else
26182 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
26183 pTm = gmtime(&t);
26184 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
26185 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
26186#endif
26187 if( pTm ){
26188 strftime(zBuf, 20, zFormat, &sNow);
26189 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
26190 }
26191}
26192#endif
26193
26194#if !defined(SQLITE_OMIT_DATETIME_FUNCS) && defined(SQLITE_DEBUG)
26195/*
26196** datedebug(...)
26197**
26198** This routine returns JSON that describes the internal DateTime object.
26199** Used for debugging and testing only. Subject to change.
26200*/
26201static void datedebugFunc(
26202 sqlite3_context *context,
26203 int argc,
26204 sqlite3_value **argv
26205){
26206 DateTime x;
26207 if( isDate(context, argc, argv, &x)==0 ){
26208 char *zJson;
26209 zJson = sqlite3_mprintf(
26210 "{iJD:%lld,Y:%d,M:%d,D:%d,h:%d,m:%d,tz:%d,"
26211 "s:%.3f,validJD:%d,validYMS:%d,validHMS:%d,"
26212 "nFloor:%d,rawS:%d,isError:%d,useSubsec:%d,"
26213 "isUtc:%d,isLocal:%d}",
26214 x.iJD, x.Y, x.M, x.D, x.h, x.m, x.tz,
26215 x.s, x.validJD, x.validYMD, x.validHMS,
26216 x.nFloor, x.rawS, x.isError, x.useSubsec,
26217 x.isUtc, x.isLocal);
26218 sqlite3_result_text(context, zJson, -1, sqlite3_free);
26219 }
26220}
26221#endif /* !SQLITE_OMIT_DATETIME_FUNCS && SQLITE_DEBUG */
26222
26223
26224/*
26225** This function registered all of the above C functions as SQL
26226** functions. This should be the only routine in this file with
26227** external linkage.
26228*/
26229SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void){
26230 static FuncDef aDateTimeFuncs[] = {
26231#ifndef SQLITE_OMIT_DATETIME_FUNCS
26232 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, juliandayFunc, 0, 0, 0, "julianday", {0} }
,
26233 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, unixepochFunc, 0, 0, 0, "unixepoch", {0} }
,
26234 PURE_DATE(date, -1, 0, 0, dateFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, dateFunc, 0, 0, 0, "date", {0} }
,
26235 PURE_DATE(time, -1, 0, 0, timeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, timeFunc, 0, 0, 0, "time", {0} }
,
26236 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, datetimeFunc, 0, 0, 0, "datetime", {0} }
,
26237 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, strftimeFunc, 0, 0, 0, "strftime", {0} }
,
26238 PURE_DATE(timediff, 2, 0, 0, timediffFunc ){2, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, timediffFunc, 0, 0, 0, "timediff", {0} }
,
26239#ifdef SQLITE_DEBUG
26240 PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ){-1, 0x00800000| 0x2000|1|0x0800, (void*)&sqlite3Config, 0
, datedebugFunc, 0, 0, 0, "datedebug", {0} }
,
26241#endif
26242 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ){0, 0x00800000|0x2000|1, 0, 0, ctimeFunc, 0, 0, 0, "current_time"
, {0} }
,
26243 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc){0, 0x00800000|0x2000|1, 0, 0, ctimestampFunc, 0, 0, 0, "current_timestamp"
, {0} }
,
26244 DFUNCTION(current_date, 0, 0, 0, cdateFunc ){0, 0x00800000|0x2000|1, 0, 0, cdateFunc, 0, 0, 0, "current_date"
, {0} }
,
26245#else
26246 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", }
,
26247 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", }
,
26248 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", }
,
26249#endif
26250 };
26251 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs)((int)(sizeof(aDateTimeFuncs)/sizeof(aDateTimeFuncs[0]))));
26252}
26253
26254/************** End of date.c ************************************************/
26255/************** Begin file os.c **********************************************/
26256/*
26257** 2005 November 29
26258**
26259** The author disclaims copyright to this source code. In place of
26260** a legal notice, here is a blessing:
26261**
26262** May you do good and not evil.
26263** May you find forgiveness for yourself and forgive others.
26264** May you share freely, never taking more than you give.
26265**
26266******************************************************************************
26267**
26268** This file contains OS interface code that is common to all
26269** architectures.
26270*/
26271/* #include "sqliteInt.h" */
26272
26273/*
26274** If we compile with the SQLITE_TEST macro set, then the following block
26275** of code will give us the ability to simulate a disk I/O error. This
26276** is used for testing the I/O recovery logic.
26277*/
26278#if defined(SQLITE_TEST)
26279SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
26280SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
26281SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
26282SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
26283SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
26284SQLITE_API int sqlite3_diskfull_pending = 0;
26285SQLITE_API int sqlite3_diskfull = 0;
26286#endif /* defined(SQLITE_TEST) */
26287
26288/*
26289** When testing, also keep a count of the number of open files.
26290*/
26291#if defined(SQLITE_TEST)
26292SQLITE_API int sqlite3_open_file_count = 0;
26293#endif /* defined(SQLITE_TEST) */
26294
26295/*
26296** The default SQLite sqlite3_vfs implementations do not allocate
26297** memory (actually, os_unix.c allocates a small amount of memory
26298** from within OsOpen()), but some third-party implementations may.
26299** So we test the effects of a malloc() failing and the sqlite3OsXXX()
26300** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
26301**
26302** The following functions are instrumented for malloc() failure
26303** testing:
26304**
26305** sqlite3OsRead()
26306** sqlite3OsWrite()
26307** sqlite3OsSync()
26308** sqlite3OsFileSize()
26309** sqlite3OsLock()
26310** sqlite3OsCheckReservedLock()
26311** sqlite3OsFileControl()
26312** sqlite3OsShmMap()
26313** sqlite3OsOpen()
26314** sqlite3OsDelete()
26315** sqlite3OsAccess()
26316** sqlite3OsFullPathname()
26317**
26318*/
26319#if defined(SQLITE_TEST)
26320SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
26321 #define DO_OS_MALLOC_TEST(x) \
26322 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
26323 void *pTstAlloc = sqlite3Malloc(10); \
26324 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)); \
26325 sqlite3_free(pTstAlloc); \
26326 }
26327#else
26328 #define DO_OS_MALLOC_TEST(x)
26329#endif
26330
26331/*
26332** The following routines are convenience wrappers around methods
26333** of the sqlite3_file object. This is mostly just syntactic sugar. All
26334** of this would be completely automatic if SQLite were coded using
26335** C++ instead of plain old C.
26336*/
26337SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file *pId){
26338 if( pId->pMethods ){
26339 pId->pMethods->xClose(pId);
26340 pId->pMethods = 0;
26341 }
26342}
26343SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
26344 DO_OS_MALLOC_TEST(id);
26345 return id->pMethods->xRead(id, pBuf, amt, offset);
26346}
26347SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
26348 DO_OS_MALLOC_TEST(id);
26349 return id->pMethods->xWrite(id, pBuf, amt, offset);
26350}
26351SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file *id, i64 size){
26352 return id->pMethods->xTruncate(id, size);
26353}
26354SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file *id, int flags){
26355 DO_OS_MALLOC_TEST(id);
26356 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK0;
26357}
26358SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
26359 DO_OS_MALLOC_TEST(id);
26360 return id->pMethods->xFileSize(id, pSize);
26361}
26362SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file *id, int lockType){
26363 DO_OS_MALLOC_TEST(id);
26364 assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE )((void) (0));
26365 return id->pMethods->xLock(id, lockType);
26366}
26367SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file *id, int lockType){
26368 assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED )((void) (0));
26369 return id->pMethods->xUnlock(id, lockType);
26370}
26371SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
26372 DO_OS_MALLOC_TEST(id);
26373 return id->pMethods->xCheckReservedLock(id, pResOut);
26374}
26375
26376/*
26377** Use sqlite3OsFileControl() when we are doing something that might fail
26378** and we need to know about the failures. Use sqlite3OsFileControlHint()
26379** when simply tossing information over the wall to the VFS and we do not
26380** really care if the VFS receives and understands the information since it
26381** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
26382** routine has no return value since the return value would be meaningless.
26383*/
26384SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
26385 if( id->pMethods==0 ) return SQLITE_NOTFOUND12;
26386#ifdef SQLITE_TEST
26387 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO22
26388 && op!=SQLITE_FCNTL_LOCK_TIMEOUT34
26389 && op!=SQLITE_FCNTL_CKPT_DONE37
26390 && op!=SQLITE_FCNTL_CKPT_START39
26391 ){
26392 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
26393 ** is using a regular VFS, it is called after the corresponding
26394 ** transaction has been committed. Injecting a fault at this point
26395 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM
26396 ** but the transaction is committed anyway.
26397 **
26398 ** The core must call OsFileControl() though, not OsFileControlHint(),
26399 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
26400 ** means the commit really has failed and an error should be returned
26401 ** to the user.
26402 **
26403 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
26404 ** to the cksumvfs. Their return code is meaningless and is ignored
26405 ** by the SQLite core, so there is no point in simulating OOMs for them.
26406 */
26407 DO_OS_MALLOC_TEST(id);
26408 }
26409#endif
26410 return id->pMethods->xFileControl(id, op, pArg);
26411}
26412SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
26413 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
26414}
26415
26416SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id){
26417 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
26418 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE4096);
26419}
26420SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
26421 if( NEVER(id->pMethods==0)(id->pMethods==0) ) return 0;
26422 return id->pMethods->xDeviceCharacteristics(id);
26423}
26424#ifndef SQLITE_OMIT_WAL
26425SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
26426 return id->pMethods->xShmLock(id, offset, n, flags);
26427}
26428SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id){
26429 id->pMethods->xShmBarrier(id);
26430}
26431SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
26432 return id->pMethods->xShmUnmap(id, deleteFlag);
26433}
26434SQLITE_PRIVATEstatic int sqlite3OsShmMap(
26435 sqlite3_file *id, /* Database file handle */
26436 int iPage,
26437 int pgsz,
26438 int bExtend, /* True to extend file if necessary */
26439 void volatile **pp /* OUT: Pointer to mapping */
26440){
26441 DO_OS_MALLOC_TEST(id);
26442 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
26443}
26444#endif /* SQLITE_OMIT_WAL */
26445
26446#if SQLITE_MAX_MMAP_SIZE20971520>0
26447/* The real implementation of xFetch and xUnfetch */
26448SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
26449 DO_OS_MALLOC_TEST(id);
26450 return id->pMethods->xFetch(id, iOff, iAmt, pp);
26451}
26452SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
26453 return id->pMethods->xUnfetch(id, iOff, p);
26454}
26455#else
26456/* No-op stubs to use when memory-mapped I/O is disabled */
26457SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
26458 *pp = 0;
26459 return SQLITE_OK0;
26460}
26461SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
26462 return SQLITE_OK0;
26463}
26464#endif
26465
26466/*
26467** The next group of routines are convenience wrappers around the
26468** VFS methods.
26469*/
26470SQLITE_PRIVATEstatic int sqlite3OsOpen(
26471 sqlite3_vfs *pVfs,
26472 const char *zPath,
26473 sqlite3_file *pFile,
26474 int flags,
26475 int *pFlagsOut
26476){
26477 int rc;
26478 DO_OS_MALLOC_TEST(0);
26479 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
26480 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
26481 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
26482 ** reaching the VFS. */
26483 assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) )((void) (0));
26484 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
26485 assert( rc==SQLITE_OK || pFile->pMethods==0 )((void) (0));
26486 return rc;
26487}
26488SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
26489 DO_OS_MALLOC_TEST(0);
26490 assert( dirSync==0 || dirSync==1 )((void) (0));
26491 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK0;
26492}
26493SQLITE_PRIVATEstatic int sqlite3OsAccess(
26494 sqlite3_vfs *pVfs,
26495 const char *zPath,
26496 int flags,
26497 int *pResOut
26498){
26499 DO_OS_MALLOC_TEST(0);
26500 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
26501}
26502SQLITE_PRIVATEstatic int sqlite3OsFullPathname(
26503 sqlite3_vfs *pVfs,
26504 const char *zPath,
26505 int nPathOut,
26506 char *zPathOut
26507){
26508 DO_OS_MALLOC_TEST(0);
26509 zPathOut[0] = 0;
26510 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
26511}
26512#ifndef SQLITE_OMIT_LOAD_EXTENSION1
26513SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
26514 assert( zPath!=0 )((void) (0));
26515 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN )((void) (0)); /* tag-20210611-1 */
26516 return pVfs->xDlOpen(pVfs, zPath);
26517}
26518SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
26519 pVfs->xDlError(pVfs, nByte, zBufOut);
26520}
26521SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
26522 return pVfs->xDlSym(pVfs, pHdle, zSym);
26523}
26524SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
26525 pVfs->xDlClose(pVfs, pHandle);
26526}
26527#endif /* SQLITE_OMIT_LOAD_EXTENSION */
26528SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
26529 if( sqlite3Config.iPrngSeed ){
26530 memset(zBufOut, 0, nByte);
26531 if( ALWAYS(nByte>(signed)sizeof(unsigned))(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
26532 memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
26533 return SQLITE_OK0;
26534 }else{
26535 return pVfs->xRandomness(pVfs, nByte, zBufOut);
26536 }
26537
26538}
26539SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
26540 return pVfs->xSleep(pVfs, nMicro);
26541}
26542SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
26543 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
26544}
26545SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
26546 int rc;
26547 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
26548 ** method to get the current date and time if that method is available
26549 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
26550 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
26551 ** unavailable.
26552 */
26553 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
26554 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
26555 }else{
26556 double r;
26557 rc = pVfs->xCurrentTime(pVfs, &r);
26558 *pTimeOut = (sqlite3_int64)(r*86400000.0);
26559 }
26560 return rc;
26561}
26562
26563SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(
26564 sqlite3_vfs *pVfs,
26565 const char *zFile,
26566 sqlite3_file **ppFile,
26567 int flags,
26568 int *pOutFlags
26569){
26570 int rc;
26571 sqlite3_file *pFile;
26572 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
26573 if( pFile ){
26574 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
26575 if( rc!=SQLITE_OK0 ){
26576 sqlite3_free(pFile);
26577 *ppFile = 0;
26578 }else{
26579 *ppFile = pFile;
26580 }
26581 }else{
26582 *ppFile = 0;
26583 rc = SQLITE_NOMEM_BKPT7;
26584 }
26585 assert( *ppFile!=0 || rc!=SQLITE_OK )((void) (0));
26586 return rc;
26587}
26588SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *pFile){
26589 assert( pFile )((void) (0));
26590 sqlite3OsClose(pFile);
26591 sqlite3_free(pFile);
26592}
26593
26594/*
26595** This function is a wrapper around the OS specific implementation of
26596** sqlite3_os_init(). The purpose of the wrapper is to provide the
26597** ability to simulate a malloc failure, so that the handling of an
26598** error in sqlite3_os_init() by the upper layers can be tested.
26599*/
26600SQLITE_PRIVATEstatic int sqlite3OsInit(void){
26601 void *p = sqlite3_malloc(10);
26602 if( p==0 ) return SQLITE_NOMEM_BKPT7;
26603 sqlite3_free(p);
26604 return sqlite3_os_init();
26605}
26606
26607/*
26608** The list of all registered VFS implementations.
26609*/
26610static sqlite3_vfs * SQLITE_WSD vfsListvfsList = 0;
26611#define vfsListvfsList GLOBAL(sqlite3_vfs *, vfsList)vfsList
26612
26613/*
26614** Locate a VFS by name. If no name is given, simply return the
26615** first VFS on the list.
26616*/
26617SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
26618 sqlite3_vfs *pVfs = 0;
26619#if SQLITE_THREADSAFE2
26620 sqlite3_mutex *mutex;
26621#endif
26622#ifndef SQLITE_OMIT_AUTOINIT
26623 int rc = sqlite3_initialize();
26624 if( rc ) return 0;
26625#endif
26626#if SQLITE_THREADSAFE2
26627 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
26628#endif
26629 sqlite3_mutex_enter(mutex);
26630 for(pVfs = vfsListvfsList; pVfs; pVfs=pVfs->pNext){
26631 if( zVfs==0 ) break;
26632 if( strcmp(zVfs, pVfs->zName)==0 ) break;
26633 }
26634 sqlite3_mutex_leave(mutex);
26635 return pVfs;
26636}
26637
26638/*
26639** Unlink a VFS from the linked list
26640*/
26641static void vfsUnlink(sqlite3_vfs *pVfs){
26642 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )((void) (0));
26643 if( pVfs==0 ){
26644 /* No-op */
26645 }else if( vfsListvfsList==pVfs ){
26646 vfsListvfsList = pVfs->pNext;
26647 }else if( vfsListvfsList ){
26648 sqlite3_vfs *p = vfsListvfsList;
26649 while( p->pNext && p->pNext!=pVfs ){
26650 p = p->pNext;
26651 }
26652 if( p->pNext==pVfs ){
26653 p->pNext = pVfs->pNext;
26654 }
26655 }
26656}
26657
26658/*
26659** Register a VFS with the system. It is harmless to register the same
26660** VFS multiple times. The new VFS becomes the default if makeDflt is
26661** true.
26662*/
26663SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
26664 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
26665#ifndef SQLITE_OMIT_AUTOINIT
26666 int rc = sqlite3_initialize();
26667 if( rc ) return rc;
26668#endif
26669#ifdef SQLITE_ENABLE_API_ARMOR1
26670 if( pVfs==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(26670);
26671#endif
26672
26673 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )mutex = sqlite3MutexAlloc(2);
26674 sqlite3_mutex_enter(mutex);
26675 vfsUnlink(pVfs);
26676 if( makeDflt || vfsListvfsList==0 ){
26677 pVfs->pNext = vfsListvfsList;
26678 vfsListvfsList = pVfs;
26679 }else{
26680 pVfs->pNext = vfsListvfsList->pNext;
26681 vfsListvfsList->pNext = pVfs;
26682 }
26683 assert(vfsList)((void) (0));
26684 sqlite3_mutex_leave(mutex);
26685 return SQLITE_OK0;
26686}
26687
26688/*
26689** Unregister a VFS so that it is no longer accessible.
26690*/
26691SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
26692 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
26693#ifndef SQLITE_OMIT_AUTOINIT
26694 int rc = sqlite3_initialize();
26695 if( rc ) return rc;
26696#endif
26697 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )mutex = sqlite3MutexAlloc(2);
26698 sqlite3_mutex_enter(mutex);
26699 vfsUnlink(pVfs);
26700 sqlite3_mutex_leave(mutex);
26701 return SQLITE_OK0;
26702}
26703
26704/************** End of os.c **************************************************/
26705/************** Begin file fault.c *******************************************/
26706/*
26707** 2008 Jan 22
26708**
26709** The author disclaims copyright to this source code. In place of
26710** a legal notice, here is a blessing:
26711**
26712** May you do good and not evil.
26713** May you find forgiveness for yourself and forgive others.
26714** May you share freely, never taking more than you give.
26715**
26716*************************************************************************
26717**
26718** This file contains code to support the concept of "benign"
26719** malloc failures (when the xMalloc() or xRealloc() method of the
26720** sqlite3_mem_methods structure fails to allocate a block of memory
26721** and returns 0).
26722**
26723** Most malloc failures are non-benign. After they occur, SQLite
26724** abandons the current operation and returns an error code (usually
26725** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
26726** fatal. For example, if a malloc fails while resizing a hash table, this
26727** is completely recoverable simply by not carrying out the resize. The
26728** hash table will continue to function normally. So a malloc failure
26729** during a hash table resize is a benign fault.
26730*/
26731
26732/* #include "sqliteInt.h" */
26733
26734#ifndef SQLITE_UNTESTABLE
26735
26736/*
26737** Global variables.
26738*/
26739typedef struct BenignMallocHooks BenignMallocHooks;
26740static SQLITE_WSD struct BenignMallocHooks {
26741 void (*xBenignBegin)(void);
26742 void (*xBenignEnd)(void);
26743} sqlite3Hooks = { 0, 0 };
26744
26745/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
26746** structure. If writable static data is unsupported on the target,
26747** we have to locate the state vector at run-time. In the more common
26748** case where writable static data is supported, wsdHooks can refer directly
26749** to the "sqlite3Hooks" state vector declared above.
26750*/
26751#ifdef SQLITE_OMIT_WSD
26752# define wsdHooksInit \
26753 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)sqlite3Hooks
26754# define wsdHookssqlite3Hooks x[0]
26755#else
26756# define wsdHooksInit
26757# define wsdHookssqlite3Hooks sqlite3Hooks
26758#endif
26759
26760
26761/*
26762** Register hooks to call when sqlite3BeginBenignMalloc() and
26763** sqlite3EndBenignMalloc() are called, respectively.
26764*/
26765SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(
26766 void (*xBenignBegin)(void),
26767 void (*xBenignEnd)(void)
26768){
26769 wsdHooksInit;
26770 wsdHookssqlite3Hooks.xBenignBegin = xBenignBegin;
26771 wsdHookssqlite3Hooks.xBenignEnd = xBenignEnd;
26772}
26773
26774/*
26775** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
26776** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
26777** indicates that subsequent malloc failures are non-benign.
26778*/
26779SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void){
26780 wsdHooksInit;
26781 if( wsdHookssqlite3Hooks.xBenignBegin ){
26782 wsdHookssqlite3Hooks.xBenignBegin();
26783 }
26784}
26785SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void){
26786 wsdHooksInit;
26787 if( wsdHookssqlite3Hooks.xBenignEnd ){
26788 wsdHookssqlite3Hooks.xBenignEnd();
26789 }
26790}
26791
26792#endif /* #ifndef SQLITE_UNTESTABLE */
26793
26794/************** End of fault.c ***********************************************/
26795/************** Begin file mem0.c ********************************************/
26796/*
26797** 2008 October 28
26798**
26799** The author disclaims copyright to this source code. In place of
26800** a legal notice, here is a blessing:
26801**
26802** May you do good and not evil.
26803** May you find forgiveness for yourself and forgive others.
26804** May you share freely, never taking more than you give.
26805**
26806*************************************************************************
26807**
26808** This file contains a no-op memory allocation drivers for use when
26809** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
26810** here always fail. SQLite will not operate with these drivers. These
26811** are merely placeholders. Real drivers must be substituted using
26812** sqlite3_config() before SQLite will operate.
26813*/
26814/* #include "sqliteInt.h" */
26815
26816/*
26817** This version of the memory allocator is the default. It is
26818** used when no other memory allocator is specified using compile-time
26819** macros.
26820*/
26821#ifdef SQLITE_ZERO_MALLOC
26822
26823/*
26824** No-op versions of all memory allocation routines
26825*/
26826static void *sqlite3MemMalloc(int nByte){ return 0; }
26827static void sqlite3MemFree(void *pPrior){ return; }
26828static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
26829static int sqlite3MemSize(void *pPrior){ return 0; }
26830static int sqlite3MemRoundup(int n){ return n; }
26831static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK0; }
26832static void sqlite3MemShutdown(void *NotUsed){ return; }
26833
26834/*
26835** This routine is the only routine in this file with external linkage.
26836**
26837** Populate the low-level memory allocation function pointers in
26838** sqlite3GlobalConfig.m with pointers to the routines in this file.
26839*/
26840SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
26841 static const sqlite3_mem_methods defaultMethods = {
26842 sqlite3MemMalloc,
26843 sqlite3MemFree,
26844 sqlite3MemRealloc,
26845 sqlite3MemSize,
26846 sqlite3MemRoundup,
26847 sqlite3MemInit,
26848 sqlite3MemShutdown,
26849 0
26850 };
26851 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
26852}
26853
26854#endif /* SQLITE_ZERO_MALLOC */
26855
26856/************** End of mem0.c ************************************************/
26857/************** Begin file mem1.c ********************************************/
26858/*
26859** 2007 August 14
26860**
26861** The author disclaims copyright to this source code. In place of
26862** a legal notice, here is a blessing:
26863**
26864** May you do good and not evil.
26865** May you find forgiveness for yourself and forgive others.
26866** May you share freely, never taking more than you give.
26867**
26868*************************************************************************
26869**
26870** This file contains low-level memory allocation drivers for when
26871** SQLite will use the standard C-library malloc/realloc/free interface
26872** to obtain the memory it needs.
26873**
26874** This file contains implementations of the low-level memory allocation
26875** routines specified in the sqlite3_mem_methods object. The content of
26876** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
26877** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
26878** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
26879** default configuration is to use memory allocation routines in this
26880** file.
26881**
26882** C-preprocessor macro summary:
26883**
26884** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
26885** the malloc_usable_size() interface exists
26886** on the target platform. Or, this symbol
26887** can be set manually, if desired.
26888** If an equivalent interface exists by
26889** a different name, using a separate -D
26890** option to rename it.
26891**
26892** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
26893** memory allocator. Set this symbol to enable
26894** building on older macs.
26895**
26896** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
26897** _msize() on windows systems. This might
26898** be necessary when compiling for Delphi,
26899** for example.
26900*/
26901/* #include "sqliteInt.h" */
26902
26903/*
26904** This version of the memory allocator is the default. It is
26905** used when no other memory allocator is specified using compile-time
26906** macros.
26907*/
26908#ifdef SQLITE_SYSTEM_MALLOC1
26909#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
26910
26911/*
26912** Use the zone allocator available on apple products unless the
26913** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
26914*/
26915#include <sys/sysctl.h>
26916#include <malloc/malloc.h>
26917#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
26918#include <libkern/OSAtomic.h>
26919#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
26920static malloc_zone_t* _sqliteZone_;
26921#define SQLITE_MALLOC(x)malloc(x) malloc_zone_malloc(_sqliteZone_, (x))
26922#define SQLITE_FREE(x)free(x) malloc_zone_free(_sqliteZone_, (x));
26923#define SQLITE_REALLOC(x,y)realloc((x),(y)) malloc_zone_realloc(_sqliteZone_, (x), (y))
26924#define SQLITE_MALLOCSIZE(x) \
26925 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
26926
26927#else /* if not __APPLE__ */
26928
26929/*
26930** Use standard C library malloc and free on non-Apple systems.
26931** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
26932*/
26933#define SQLITE_MALLOC(x)malloc(x) malloc(x)
26934#define SQLITE_FREE(x)free(x) free(x)
26935#define SQLITE_REALLOC(x,y)realloc((x),(y)) realloc((x),(y))
26936
26937/*
26938** The malloc.h header file is needed for malloc_usable_size() function
26939** on some systems (e.g. Linux).
26940*/
26941#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
26942# define SQLITE_USE_MALLOC_H 1
26943# define SQLITE_USE_MALLOC_USABLE_SIZE 1
26944/*
26945** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
26946** use of _msize() is automatic, but can be disabled by compiling with
26947** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
26948** the malloc.h header file.
26949*/
26950#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
26951# define SQLITE_USE_MALLOC_H
26952# define SQLITE_USE_MSIZE
26953#endif
26954
26955/*
26956** Include the malloc.h header file, if necessary. Also set define macro
26957** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
26958** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
26959** The memory size function can always be overridden manually by defining
26960** the macro SQLITE_MALLOCSIZE to the desired function name.
26961*/
26962#if defined(SQLITE_USE_MALLOC_H)
26963# include <malloc.h>
26964# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
26965# if !defined(SQLITE_MALLOCSIZE)
26966# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
26967# endif
26968# elif defined(SQLITE_USE_MSIZE)
26969# if !defined(SQLITE_MALLOCSIZE)
26970# define SQLITE_MALLOCSIZE _msize
26971# endif
26972# endif
26973#endif /* defined(SQLITE_USE_MALLOC_H) */
26974
26975#endif /* __APPLE__ or not __APPLE__ */
26976
26977/*
26978** Like malloc(), but remember the size of the allocation
26979** so that we can find it later using sqlite3MemSize().
26980**
26981** For this low-level routine, we are guaranteed that nByte>0 because
26982** cases of nByte<=0 will be intercepted and dealt with by higher level
26983** routines.
26984*/
26985static void *sqlite3MemMalloc(int nByte){
26986#ifdef SQLITE_MALLOCSIZE
26987 void *p;
26988 testcase( ROUND8(nByte)==nByte );
26989 p = SQLITE_MALLOC( nByte )malloc(nByte);
26990 if( p==0 ){
26991 testcase( sqlite3GlobalConfig.xLog!=0 );
26992 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
26993 }
26994 return p;
26995#else
26996 sqlite3_int64 *p;
26997 assert( nByte>0 )((void) (0));
26998 testcase( ROUND8(nByte)!=nByte );
26999 p = SQLITE_MALLOC( nByte+8 )malloc(nByte+8);
27000 if( p ){
27001 p[0] = nByte;
27002 p++;
27003 }else{
27004 testcase( sqlite3GlobalConfig.xLog!=0 );
27005 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
27006 }
27007 return (void *)p;
27008#endif
27009}
27010
27011/*
27012** Like free() but works for allocations obtained from sqlite3MemMalloc()
27013** or sqlite3MemRealloc().
27014**
27015** For this low-level routine, we already know that pPrior!=0 since
27016** cases where pPrior==0 will have been intercepted and dealt with
27017** by higher-level routines.
27018*/
27019static void sqlite3MemFree(void *pPrior){
27020#ifdef SQLITE_MALLOCSIZE
27021 SQLITE_FREE(pPrior)free(pPrior);
27022#else
27023 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
27024 assert( pPrior!=0 )((void) (0));
27025 p--;
27026 SQLITE_FREE(p)free(p);
27027#endif
27028}
27029
27030/*
27031** Report the allocated size of a prior return from xMalloc()
27032** or xRealloc().
27033*/
27034static int sqlite3MemSize(void *pPrior){
27035#ifdef SQLITE_MALLOCSIZE
27036 assert( pPrior!=0 )((void) (0));
27037 return (int)SQLITE_MALLOCSIZE(pPrior);
27038#else
27039 sqlite3_int64 *p;
27040 assert( pPrior!=0 )((void) (0));
27041 p = (sqlite3_int64*)pPrior;
27042 p--;
27043 return (int)p[0];
27044#endif
27045}
27046
27047/*
27048** Like realloc(). Resize an allocation previously obtained from
27049** sqlite3MemMalloc().
27050**
27051** For this low-level interface, we know that pPrior!=0. Cases where
27052** pPrior==0 while have been intercepted by higher-level routine and
27053** redirected to xMalloc. Similarly, we know that nByte>0 because
27054** cases where nByte<=0 will have been intercepted by higher-level
27055** routines and redirected to xFree.
27056*/
27057static void *sqlite3MemRealloc(void *pPrior, int nByte){
27058#ifdef SQLITE_MALLOCSIZE
27059 void *p = SQLITE_REALLOC(pPrior, nByte)realloc((pPrior),(nByte));
27060 if( p==0 ){
27061 testcase( sqlite3GlobalConfig.xLog!=0 );
27062 sqlite3_log(SQLITE_NOMEM7,
27063 "failed memory resize %u to %u bytes",
27064 SQLITE_MALLOCSIZE(pPrior), nByte);
27065 }
27066 return p;
27067#else
27068 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
27069 assert( pPrior!=0 && nByte>0 )((void) (0));
27070 assert( nByte==ROUND8(nByte) )((void) (0)); /* EV: R-46199-30249 */
27071 p--;
27072 p = SQLITE_REALLOC(p, nByte+8 )realloc((p),(nByte+8));
27073 if( p ){
27074 p[0] = nByte;
27075 p++;
27076 }else{
27077 testcase( sqlite3GlobalConfig.xLog!=0 );
27078 sqlite3_log(SQLITE_NOMEM7,
27079 "failed memory resize %u to %u bytes",
27080 sqlite3MemSize(pPrior), nByte);
27081 }
27082 return (void*)p;
27083#endif
27084}
27085
27086/*
27087** Round up a request size to the next valid allocation size.
27088*/
27089static int sqlite3MemRoundup(int n){
27090 return ROUND8(n)(((n)+7)&~7);
27091}
27092
27093/*
27094** Initialize this module.
27095*/
27096static int sqlite3MemInit(void *NotUsed){
27097#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
27098 int cpuCount;
27099 size_t len;
27100 if( _sqliteZone_ ){
27101 return SQLITE_OK0;
27102 }
27103 len = sizeof(cpuCount);
27104 /* One usually wants to use hw.activecpu for MT decisions, but not here */
27105 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL((void*)0), 0);
27106 if( cpuCount>1 ){
27107 /* defer MT decisions to system malloc */
27108 _sqliteZone_ = malloc_default_zone();
27109 }else{
27110 /* only 1 core, use our own zone to contention over global locks,
27111 ** e.g. we have our own dedicated locks */
27112 _sqliteZone_ = malloc_create_zone(4096, 0);
27113 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
27114 }
27115#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
27116 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27117 return SQLITE_OK0;
27118}
27119
27120/*
27121** Deinitialize this module.
27122*/
27123static void sqlite3MemShutdown(void *NotUsed){
27124 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27125 return;
27126}
27127
27128/*
27129** This routine is the only routine in this file with external linkage.
27130**
27131** Populate the low-level memory allocation function pointers in
27132** sqlite3GlobalConfig.m with pointers to the routines in this file.
27133*/
27134SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
27135 static const sqlite3_mem_methods defaultMethods = {
27136 sqlite3MemMalloc,
27137 sqlite3MemFree,
27138 sqlite3MemRealloc,
27139 sqlite3MemSize,
27140 sqlite3MemRoundup,
27141 sqlite3MemInit,
27142 sqlite3MemShutdown,
27143 0
27144 };
27145 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
27146}
27147
27148#endif /* SQLITE_SYSTEM_MALLOC */
27149
27150/************** End of mem1.c ************************************************/
27151/************** Begin file mem2.c ********************************************/
27152/*
27153** 2007 August 15
27154**
27155** The author disclaims copyright to this source code. In place of
27156** a legal notice, here is a blessing:
27157**
27158** May you do good and not evil.
27159** May you find forgiveness for yourself and forgive others.
27160** May you share freely, never taking more than you give.
27161**
27162*************************************************************************
27163**
27164** This file contains low-level memory allocation drivers for when
27165** SQLite will use the standard C-library malloc/realloc/free interface
27166** to obtain the memory it needs while adding lots of additional debugging
27167** information to each allocation in order to help detect and fix memory
27168** leaks and memory usage errors.
27169**
27170** This file contains implementations of the low-level memory allocation
27171** routines specified in the sqlite3_mem_methods object.
27172*/
27173/* #include "sqliteInt.h" */
27174
27175/*
27176** This version of the memory allocator is used only if the
27177** SQLITE_MEMDEBUG macro is defined
27178*/
27179#ifdef SQLITE_MEMDEBUG
27180
27181/*
27182** The backtrace functionality is only available with GLIBC
27183*/
27184#ifdef __GLIBC__2
27185 extern int backtrace(void**,int);
27186 extern void backtrace_symbols_fd(void*const*,int,int);
27187#else
27188# define backtrace(A,B) 1
27189# define backtrace_symbols_fd(A,B,C)
27190#endif
27191/* #include <stdio.h> */
27192
27193/*
27194** Each memory allocation looks like this:
27195**
27196** ------------------------------------------------------------------------
27197** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
27198** ------------------------------------------------------------------------
27199**
27200** The application code sees only a pointer to the allocation. We have
27201** to back up from the allocation pointer to find the MemBlockHdr. The
27202** MemBlockHdr tells us the size of the allocation and the number of
27203** backtrace pointers. There is also a guard word at the end of the
27204** MemBlockHdr.
27205*/
27206struct MemBlockHdr {
27207 i64 iSize; /* Size of this allocation */
27208 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
27209 char nBacktrace; /* Number of backtraces on this alloc */
27210 char nBacktraceSlots; /* Available backtrace slots */
27211 u8 nTitle; /* Bytes of title; includes '\0' */
27212 u8 eType; /* Allocation type code */
27213 int iForeGuard; /* Guard word for sanity */
27214};
27215
27216/*
27217** Guard words
27218*/
27219#define FOREGUARD 0x80F5E153
27220#define REARGUARD 0xE4676B53
27221
27222/*
27223** Number of malloc size increments to track.
27224*/
27225#define NCSIZE 1000
27226
27227/*
27228** All of the static variables used by this module are collected
27229** into a single structure named "mem". This is to keep the
27230** static variables organized and to reduce namespace pollution
27231** when this module is combined with other in the amalgamation.
27232*/
27233static struct {
27234
27235 /*
27236 ** Mutex to control access to the memory allocation subsystem.
27237 */
27238 sqlite3_mutex *mutex;
27239
27240 /*
27241 ** Head and tail of a linked list of all outstanding allocations
27242 */
27243 struct MemBlockHdr *pFirst;
27244 struct MemBlockHdr *pLast;
27245
27246 /*
27247 ** The number of levels of backtrace to save in new allocations.
27248 */
27249 int nBacktrace;
27250 void (*xBacktrace)(int, int, void **);
27251
27252 /*
27253 ** Title text to insert in front of each block
27254 */
27255 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
27256 char zTitle[100]; /* The title text */
27257
27258 /*
27259 ** sqlite3MallocDisallow() increments the following counter.
27260 ** sqlite3MallocAllow() decrements it.
27261 */
27262 int disallow; /* Do not allow memory allocation */
27263
27264 /*
27265 ** Gather statistics on the sizes of memory allocations.
27266 ** nAlloc[i] is the number of allocation attempts of i*8
27267 ** bytes. i==NCSIZE is the number of allocation attempts for
27268 ** sizes more than NCSIZE*8 bytes.
27269 */
27270 int nAlloc[NCSIZE]; /* Total number of allocations */
27271 int nCurrent[NCSIZE]; /* Current number of allocations */
27272 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
27273
27274} mem;
27275
27276
27277/*
27278** Adjust memory usage statistics
27279*/
27280static void adjustStats(int iSize, int increment){
27281 int i = ROUND8(iSize)(((iSize)+7)&~7)/8;
27282 if( i>NCSIZE-1 ){
27283 i = NCSIZE - 1;
27284 }
27285 if( increment>0 ){
27286 mem.nAlloc[i]++;
27287 mem.nCurrent[i]++;
27288 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
27289 mem.mxCurrent[i] = mem.nCurrent[i];
27290 }
27291 }else{
27292 mem.nCurrent[i]--;
27293 assert( mem.nCurrent[i]>=0 )((void) (0));
27294 }
27295}
27296
27297/*
27298** Given an allocation, find the MemBlockHdr for that allocation.
27299**
27300** This routine checks the guards at either end of the allocation and
27301** if they are incorrect it asserts.
27302*/
27303static struct MemBlockHdr *sqlite3MemsysGetHeader(const void *pAllocation){
27304 struct MemBlockHdr *p;
27305 int *pInt;
27306 u8 *pU8;
27307 int nReserve;
27308
27309 p = (struct MemBlockHdr*)pAllocation;
27310 p--;
27311 assert( p->iForeGuard==(int)FOREGUARD )((void) (0));
27312 nReserve = ROUND8(p->iSize)(((p->iSize)+7)&~7);
27313 pInt = (int*)pAllocation;
27314 pU8 = (u8*)pAllocation;
27315 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD )((void) (0));
27316 /* This checks any of the "extra" bytes allocated due
27317 ** to rounding up to an 8 byte boundary to ensure
27318 ** they haven't been overwritten.
27319 */
27320 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 )((void) (0));
27321 return p;
27322}
27323
27324/*
27325** Return the number of bytes currently allocated at address p.
27326*/
27327static int sqlite3MemSize(void *p){
27328 struct MemBlockHdr *pHdr;
27329 if( !p ){
27330 return 0;
27331 }
27332 pHdr = sqlite3MemsysGetHeader(p);
27333 return (int)pHdr->iSize;
27334}
27335
27336/*
27337** Initialize the memory allocation subsystem.
27338*/
27339static int sqlite3MemInit(void *NotUsed){
27340 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27341 assert( (sizeof(struct MemBlockHdr)&7) == 0 )((void) (0));
27342 if( !sqlite3GlobalConfigsqlite3Config.bMemstat ){
27343 /* If memory status is enabled, then the malloc.c wrapper will already
27344 ** hold the STATIC_MEM mutex when the routines here are invoked. */
27345 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
27346 }
27347 return SQLITE_OK0;
27348}
27349
27350/*
27351** Deinitialize the memory allocation subsystem.
27352*/
27353static void sqlite3MemShutdown(void *NotUsed){
27354 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
27355 mem.mutex = 0;
27356}
27357
27358/*
27359** Round up a request size to the next valid allocation size.
27360*/
27361static int sqlite3MemRoundup(int n){
27362 return ROUND8(n)(((n)+7)&~7);
27363}
27364
27365/*
27366** Fill a buffer with pseudo-random bytes. This is used to preset
27367** the content of a new memory allocation to unpredictable values and
27368** to clear the content of a freed allocation to unpredictable values.
27369*/
27370static void randomFill(char *pBuf, int nByte){
27371 unsigned int x, y, r;
27372 x = SQLITE_PTR_TO_INT(pBuf)((int)(long int)(pBuf));
27373 y = nByte | 1;
27374 while( nByte >= 4 ){
27375 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
27376 y = y*1103515245 + 12345;
27377 r = x ^ y;
27378 *(int*)pBuf = r;
27379 pBuf += 4;
27380 nByte -= 4;
27381 }
27382 while( nByte-- > 0 ){
27383 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
27384 y = y*1103515245 + 12345;
27385 r = x ^ y;
27386 *(pBuf++) = r & 0xff;
27387 }
27388}
27389
27390/*
27391** Allocate nByte bytes of memory.
27392*/
27393static void *sqlite3MemMalloc(int nByte){
27394 struct MemBlockHdr *pHdr;
27395 void **pBt;
27396 char *z;
27397 int *pInt;
27398 void *p = 0;
27399 int totalSize;
27400 int nReserve;
27401 sqlite3_mutex_enter(mem.mutex);
27402 assert( mem.disallow==0 )((void) (0));
27403 nReserve = ROUND8(nByte)(((nByte)+7)&~7);
27404 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
27405 mem.nBacktrace*sizeof(void*) + mem.nTitle;
27406 p = malloc(totalSize);
27407 if( p ){
27408 z = p;
27409 pBt = (void**)&z[mem.nTitle];
27410 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
27411 pHdr->pNext = 0;
27412 pHdr->pPrev = mem.pLast;
27413 if( mem.pLast ){
27414 mem.pLast->pNext = pHdr;
27415 }else{
27416 mem.pFirst = pHdr;
27417 }
27418 mem.pLast = pHdr;
27419 pHdr->iForeGuard = FOREGUARD;
27420 pHdr->eType = MEMTYPE_HEAP0x01;
27421 pHdr->nBacktraceSlots = mem.nBacktrace;
27422 pHdr->nTitle = mem.nTitle;
27423 if( mem.nBacktrace ){
27424 void *aAddr[40];
27425 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
27426 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
27427 assert(pBt[0])((void) (0));
27428 if( mem.xBacktrace ){
27429 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
27430 }
27431 }else{
27432 pHdr->nBacktrace = 0;
27433 }
27434 if( mem.nTitle ){
27435 memcpy(z, mem.zTitle, mem.nTitle);
27436 }
27437 pHdr->iSize = nByte;
27438 adjustStats(nByte, +1);
27439 pInt = (int*)&pHdr[1];
27440 pInt[nReserve/sizeof(int)] = REARGUARD;
27441 randomFill((char*)pInt, nByte);
27442 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
27443 p = (void*)pInt;
27444 }
27445 sqlite3_mutex_leave(mem.mutex);
27446 return p;
27447}
27448
27449/*
27450** Free memory.
27451*/
27452static void sqlite3MemFree(void *pPrior){
27453 struct MemBlockHdr *pHdr;
27454 void **pBt;
27455 char *z;
27456 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0((void) (0))
27457 || mem.mutex!=0 )((void) (0));
27458 pHdr = sqlite3MemsysGetHeader(pPrior);
27459 pBt = (void**)pHdr;
27460 pBt -= pHdr->nBacktraceSlots;
27461 sqlite3_mutex_enter(mem.mutex);
27462 if( pHdr->pPrev ){
27463 assert( pHdr->pPrev->pNext==pHdr )((void) (0));
27464 pHdr->pPrev->pNext = pHdr->pNext;
27465 }else{
27466 assert( mem.pFirst==pHdr )((void) (0));
27467 mem.pFirst = pHdr->pNext;
27468 }
27469 if( pHdr->pNext ){
27470 assert( pHdr->pNext->pPrev==pHdr )((void) (0));
27471 pHdr->pNext->pPrev = pHdr->pPrev;
27472 }else{
27473 assert( mem.pLast==pHdr )((void) (0));
27474 mem.pLast = pHdr->pPrev;
27475 }
27476 z = (char*)pBt;
27477 z -= pHdr->nTitle;
27478 adjustStats((int)pHdr->iSize, -1);
27479 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
27480 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
27481 free(z);
27482 sqlite3_mutex_leave(mem.mutex);
27483}
27484
27485/*
27486** Change the size of an existing memory allocation.
27487**
27488** For this debugging implementation, we *always* make a copy of the
27489** allocation into a new place in memory. In this way, if the
27490** higher level code is using pointer to the old allocation, it is
27491** much more likely to break and we are much more liking to find
27492** the error.
27493*/
27494static void *sqlite3MemRealloc(void *pPrior, int nByte){
27495 struct MemBlockHdr *pOldHdr;
27496 void *pNew;
27497 assert( mem.disallow==0 )((void) (0));
27498 assert( (nByte & 7)==0 )((void) (0)); /* EV: R-46199-30249 */
27499 pOldHdr = sqlite3MemsysGetHeader(pPrior);
27500 pNew = sqlite3MemMalloc(nByte);
27501 if( pNew ){
27502 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
27503 if( nByte>pOldHdr->iSize ){
27504 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
27505 }
27506 sqlite3MemFree(pPrior);
27507 }
27508 return pNew;
27509}
27510
27511/*
27512** Populate the low-level memory allocation function pointers in
27513** sqlite3GlobalConfig.m with pointers to the routines in this file.
27514*/
27515SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
27516 static const sqlite3_mem_methods defaultMethods = {
27517 sqlite3MemMalloc,
27518 sqlite3MemFree,
27519 sqlite3MemRealloc,
27520 sqlite3MemSize,
27521 sqlite3MemRoundup,
27522 sqlite3MemInit,
27523 sqlite3MemShutdown,
27524 0
27525 };
27526 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
27527}
27528
27529/*
27530** Set the "type" of an allocation.
27531*/
27532SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void *p, u8 eType){
27533 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
27534 struct MemBlockHdr *pHdr;
27535 pHdr = sqlite3MemsysGetHeader(p);
27536 assert( pHdr->iForeGuard==FOREGUARD )((void) (0));
27537 pHdr->eType = eType;
27538 }
27539}
27540
27541/*
27542** Return TRUE if the mask of type in eType matches the type of the
27543** allocation p. Also return true if p==NULL.
27544**
27545** This routine is designed for use within an assert() statement, to
27546** verify the type of an allocation. For example:
27547**
27548** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
27549*/
27550SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(const void *p, u8 eType)1{
27551 int rc = 1;
27552 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
27553 struct MemBlockHdr *pHdr;
27554 pHdr = sqlite3MemsysGetHeader(p);
27555 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
27556 if( (pHdr->eType&eType)==0 ){
27557 rc = 0;
27558 }
27559 }
27560 return rc;
27561}
27562
27563/*
27564** Return TRUE if the mask of type in eType matches no bits of the type of the
27565** allocation p. Also return true if p==NULL.
27566**
27567** This routine is designed for use within an assert() statement, to
27568** verify the type of an allocation. For example:
27569**
27570** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
27571*/
27572SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(const void *p, u8 eType)1{
27573 int rc = 1;
27574 if( p && sqlite3GlobalConfigsqlite3Config.m.xFree==sqlite3MemFree ){
27575 struct MemBlockHdr *pHdr;
27576 pHdr = sqlite3MemsysGetHeader(p);
27577 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
27578 if( (pHdr->eType&eType)!=0 ){
27579 rc = 0;
27580 }
27581 }
27582 return rc;
27583}
27584
27585/*
27586** Set the number of backtrace levels kept for each allocation.
27587** A value of zero turns off backtracing. The number is always rounded
27588** up to a multiple of 2.
27589*/
27590SQLITE_PRIVATEstatic void sqlite3MemdebugBacktrace(int depth){
27591 if( depth<0 ){ depth = 0; }
27592 if( depth>20 ){ depth = 20; }
27593 depth = (depth+1)&0xfe;
27594 mem.nBacktrace = depth;
27595}
27596
27597SQLITE_PRIVATEstatic void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
27598 mem.xBacktrace = xBacktrace;
27599}
27600
27601/*
27602** Set the title string for subsequent allocations.
27603*/
27604SQLITE_PRIVATEstatic void sqlite3MemdebugSettitle(const char *zTitle){
27605 unsigned int n = sqlite3Strlen30(zTitle) + 1;
27606 sqlite3_mutex_enter(mem.mutex);
27607 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
27608 memcpy(mem.zTitle, zTitle, n);
27609 mem.zTitle[n] = 0;
27610 mem.nTitle = ROUND8(n)(((n)+7)&~7);
27611 sqlite3_mutex_leave(mem.mutex);
27612}
27613
27614SQLITE_PRIVATEstatic void sqlite3MemdebugSync(){
27615 struct MemBlockHdr *pHdr;
27616 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
27617 void **pBt = (void**)pHdr;
27618 pBt -= pHdr->nBacktraceSlots;
27619 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
27620 }
27621}
27622
27623/*
27624** Open the file indicated and write a log of all unfreed memory
27625** allocations into that log.
27626*/
27627SQLITE_PRIVATEstatic void sqlite3MemdebugDump(const char *zFilename){
27628 FILE *out;
27629 struct MemBlockHdr *pHdr;
27630 void **pBt;
27631 int i;
27632 out = fopen(zFilename, "w");
27633 if( out==0 ){
27634 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
27635 zFilename);
27636 return;
27637 }
27638 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
27639 char *z = (char*)pHdr;
27640 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
27641 fprintf(out, "**** %lld bytes at %p from %s ****\n",
27642 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
27643 if( pHdr->nBacktrace ){
27644 fflush(out);
27645 pBt = (void**)pHdr;
27646 pBt -= pHdr->nBacktraceSlots;
27647 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
27648 fprintf(out, "\n");
27649 }
27650 }
27651 fprintf(out, "COUNTS:\n");
27652 for(i=0; i<NCSIZE-1; i++){
27653 if( mem.nAlloc[i] ){
27654 fprintf(out, " %5d: %10d %10d %10d\n",
27655 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
27656 }
27657 }
27658 if( mem.nAlloc[NCSIZE-1] ){
27659 fprintf(out, " %5d: %10d %10d %10d\n",
27660 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
27661 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
27662 }
27663 fclose(out);
27664}
27665
27666/*
27667** Return the number of times sqlite3MemMalloc() has been called.
27668*/
27669SQLITE_PRIVATEstatic int sqlite3MemdebugMallocCount(){
27670 int i;
27671 int nTotal = 0;
27672 for(i=0; i<NCSIZE; i++){
27673 nTotal += mem.nAlloc[i];
27674 }
27675 return nTotal;
27676}
27677
27678
27679#endif /* SQLITE_MEMDEBUG */
27680
27681/************** End of mem2.c ************************************************/
27682/************** Begin file mem3.c ********************************************/
27683/*
27684** 2007 October 14
27685**
27686** The author disclaims copyright to this source code. In place of
27687** a legal notice, here is a blessing:
27688**
27689** May you do good and not evil.
27690** May you find forgiveness for yourself and forgive others.
27691** May you share freely, never taking more than you give.
27692**
27693*************************************************************************
27694** This file contains the C functions that implement a memory
27695** allocation subsystem for use by SQLite.
27696**
27697** This version of the memory allocation subsystem omits all
27698** use of malloc(). The SQLite user supplies a block of memory
27699** before calling sqlite3_initialize() from which allocations
27700** are made and returned by the xMalloc() and xRealloc()
27701** implementations. Once sqlite3_initialize() has been called,
27702** the amount of memory available to SQLite is fixed and cannot
27703** be changed.
27704**
27705** This version of the memory allocation subsystem is included
27706** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
27707*/
27708/* #include "sqliteInt.h" */
27709
27710/*
27711** This version of the memory allocator is only built into the library
27712** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
27713** mean that the library will use a memory-pool by default, just that
27714** it is available. The mempool allocator is activated by calling
27715** sqlite3_config().
27716*/
27717#ifdef SQLITE_ENABLE_MEMSYS3
27718
27719/*
27720** Maximum size (in Mem3Blocks) of a "small" chunk.
27721*/
27722#define MX_SMALL 10
27723
27724
27725/*
27726** Number of freelist hash slots
27727*/
27728#define N_HASH 61
27729
27730/*
27731** A memory allocation (also called a "chunk") consists of two or
27732** more blocks where each block is 8 bytes. The first 8 bytes are
27733** a header that is not returned to the user.
27734**
27735** A chunk is two or more blocks that is either checked out or
27736** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
27737** size of the allocation in blocks if the allocation is free.
27738** The u.hdr.size4x&1 bit is true if the chunk is checked out and
27739** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
27740** is true if the previous chunk is checked out and false if the
27741** previous chunk is free. The u.hdr.prevSize field is the size of
27742** the previous chunk in blocks if the previous chunk is on the
27743** freelist. If the previous chunk is checked out, then
27744** u.hdr.prevSize can be part of the data for that chunk and should
27745** not be read or written.
27746**
27747** We often identify a chunk by its index in mem3.aPool[]. When
27748** this is done, the chunk index refers to the second block of
27749** the chunk. In this way, the first chunk has an index of 1.
27750** A chunk index of 0 means "no such chunk" and is the equivalent
27751** of a NULL pointer.
27752**
27753** The second block of free chunks is of the form u.list. The
27754** two fields form a double-linked list of chunks of related sizes.
27755** Pointers to the head of the list are stored in mem3.aiSmall[]
27756** for smaller chunks and mem3.aiHash[] for larger chunks.
27757**
27758** The second block of a chunk is user data if the chunk is checked
27759** out. If a chunk is checked out, the user data may extend into
27760** the u.hdr.prevSize value of the following chunk.
27761*/
27762typedef struct Mem3Block Mem3Block;
27763struct Mem3Block {
27764 union {
27765 struct {
27766 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
27767 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
27768 } hdr;
27769 struct {
27770 u32 next; /* Index in mem3.aPool[] of next free chunk */
27771 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
27772 } list;
27773 } u;
27774};
27775
27776/*
27777** All of the static variables used by this module are collected
27778** into a single structure named "mem3". This is to keep the
27779** static variables organized and to reduce namespace pollution
27780** when this module is combined with other in the amalgamation.
27781*/
27782static SQLITE_WSD struct Mem3Global {
27783 /*
27784 ** Memory available for allocation. nPool is the size of the array
27785 ** (in Mem3Blocks) pointed to by aPool less 2.
27786 */
27787 u32 nPool;
27788 Mem3Block *aPool;
27789
27790 /*
27791 ** True if we are evaluating an out-of-memory callback.
27792 */
27793 int alarmBusy;
27794
27795 /*
27796 ** Mutex to control access to the memory allocation subsystem.
27797 */
27798 sqlite3_mutex *mutex;
27799
27800 /*
27801 ** The minimum amount of free space that we have seen.
27802 */
27803 u32 mnKeyBlk;
27804
27805 /*
27806 ** iKeyBlk is the index of the key chunk. Most new allocations
27807 ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks)
27808 ** of the current key chunk. iKeyBlk is 0 if there is no key chunk.
27809 ** The key chunk is not in either the aiHash[] or aiSmall[].
27810 */
27811 u32 iKeyBlk;
27812 u32 szKeyBlk;
27813
27814 /*
27815 ** Array of lists of free blocks according to the block size
27816 ** for smaller chunks, or a hash on the block size for larger
27817 ** chunks.
27818 */
27819 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
27820 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
27821} mem3 = { 97535575 };
27822
27823#define mem3 GLOBAL(struct Mem3Global, mem3)mem3
27824
27825/*
27826** Unlink the chunk at mem3.aPool[i] from list it is currently
27827** on. *pRoot is the list that i is a member of.
27828*/
27829static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
27830 u32 next = mem3.aPool[i].u.list.next;
27831 u32 prev = mem3.aPool[i].u.list.prev;
27832 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27833 if( prev==0 ){
27834 *pRoot = next;
27835 }else{
27836 mem3.aPool[prev].u.list.next = next;
27837 }
27838 if( next ){
27839 mem3.aPool[next].u.list.prev = prev;
27840 }
27841 mem3.aPool[i].u.list.next = 0;
27842 mem3.aPool[i].u.list.prev = 0;
27843}
27844
27845/*
27846** Unlink the chunk at index i from
27847** whatever list is currently a member of.
27848*/
27849static void memsys3Unlink(u32 i){
27850 u32 size, hash;
27851 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27852 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
27853 assert( i>=1 )((void) (0));
27854 size = mem3.aPool[i-1].u.hdr.size4x/4;
27855 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
27856 assert( size>=2 )((void) (0));
27857 if( size <= MX_SMALL ){
27858 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
27859 }else{
27860 hash = size % N_HASH;
27861 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
27862 }
27863}
27864
27865/*
27866** Link the chunk at mem3.aPool[i] so that is on the list rooted
27867** at *pRoot.
27868*/
27869static void memsys3LinkIntoList(u32 i, u32 *pRoot){
27870 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27871 mem3.aPool[i].u.list.next = *pRoot;
27872 mem3.aPool[i].u.list.prev = 0;
27873 if( *pRoot ){
27874 mem3.aPool[*pRoot].u.list.prev = i;
27875 }
27876 *pRoot = i;
27877}
27878
27879/*
27880** Link the chunk at index i into either the appropriate
27881** small chunk list, or into the large chunk hash table.
27882*/
27883static void memsys3Link(u32 i){
27884 u32 size, hash;
27885 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27886 assert( i>=1 )((void) (0));
27887 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
27888 size = mem3.aPool[i-1].u.hdr.size4x/4;
27889 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
27890 assert( size>=2 )((void) (0));
27891 if( size <= MX_SMALL ){
27892 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
27893 }else{
27894 hash = size % N_HASH;
27895 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
27896 }
27897}
27898
27899/*
27900** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
27901** will already be held (obtained by code in malloc.c) if
27902** sqlite3GlobalConfig.bMemStat is true.
27903*/
27904static void memsys3Enter(void){
27905 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 && mem3.mutex==0 ){
27906 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
27907 }
27908 sqlite3_mutex_enter(mem3.mutex);
27909}
27910static void memsys3Leave(void){
27911 sqlite3_mutex_leave(mem3.mutex);
27912}
27913
27914/*
27915** Called when we are unable to satisfy an allocation of nBytes.
27916*/
27917static void memsys3OutOfMemory(int nByte){
27918 if( !mem3.alarmBusy ){
27919 mem3.alarmBusy = 1;
27920 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27921 sqlite3_mutex_leave(mem3.mutex);
27922 sqlite3_release_memory(nByte);
27923 sqlite3_mutex_enter(mem3.mutex);
27924 mem3.alarmBusy = 0;
27925 }
27926}
27927
27928
27929/*
27930** Chunk i is a free chunk that has been unlinked. Adjust its
27931** size parameters for check-out and return a pointer to the
27932** user portion of the chunk.
27933*/
27934static void *memsys3Checkout(u32 i, u32 nBlock){
27935 u32 x;
27936 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27937 assert( i>=1 )((void) (0));
27938 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock )((void) (0));
27939 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock )((void) (0));
27940 x = mem3.aPool[i-1].u.hdr.size4x;
27941 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
27942 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
27943 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
27944 return &mem3.aPool[i];
27945}
27946
27947/*
27948** Carve a piece off of the end of the mem3.iKeyBlk free chunk.
27949** Return a pointer to the new allocation. Or, if the key chunk
27950** is not large enough, return 0.
27951*/
27952static void *memsys3FromKeyBlk(u32 nBlock){
27953 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
27954 assert( mem3.szKeyBlk>=nBlock )((void) (0));
27955 if( nBlock>=mem3.szKeyBlk-1 ){
27956 /* Use the entire key chunk */
27957 void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk);
27958 mem3.iKeyBlk = 0;
27959 mem3.szKeyBlk = 0;
27960 mem3.mnKeyBlk = 0;
27961 return p;
27962 }else{
27963 /* Split the key block. Return the tail. */
27964 u32 newi, x;
27965 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
27966 assert( newi > mem3.iKeyBlk+1 )((void) (0));
27967 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
27968 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
27969 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
27970 mem3.szKeyBlk -= nBlock;
27971 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
27972 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
27973 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
27974 if( mem3.szKeyBlk < mem3.mnKeyBlk ){
27975 mem3.mnKeyBlk = mem3.szKeyBlk;
27976 }
27977 return (void*)&mem3.aPool[newi];
27978 }
27979}
27980
27981/*
27982** *pRoot is the head of a list of free chunks of the same size
27983** or same size hash. In other words, *pRoot is an entry in either
27984** mem3.aiSmall[] or mem3.aiHash[].
27985**
27986** This routine examines all entries on the given list and tries
27987** to coalesce each entries with adjacent free chunks.
27988**
27989** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces
27990** the current mem3.iKeyBlk with the new larger chunk. In order for
27991** this mem3.iKeyBlk replacement to work, the key chunk must be
27992** linked into the hash tables. That is not the normal state of
27993** affairs, of course. The calling routine must link the key
27994** chunk before invoking this routine, then must unlink the (possibly
27995** changed) key chunk once this routine has finished.
27996*/
27997static void memsys3Merge(u32 *pRoot){
27998 u32 iNext, prev, size, i, x;
27999
28000 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28001 for(i=*pRoot; i>0; i=iNext){
28002 iNext = mem3.aPool[i].u.list.next;
28003 size = mem3.aPool[i-1].u.hdr.size4x;
28004 assert( (size&1)==0 )((void) (0));
28005 if( (size&2)==0 ){
28006 memsys3UnlinkFromList(i, pRoot);
28007 assert( i > mem3.aPool[i-1].u.hdr.prevSize )((void) (0));
28008 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
28009 if( prev==iNext ){
28010 iNext = mem3.aPool[prev].u.list.next;
28011 }
28012 memsys3Unlink(prev);
28013 size = i + size/4 - prev;
28014 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
28015 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
28016 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
28017 memsys3Link(prev);
28018 i = prev;
28019 }else{
28020 size /= 4;
28021 }
28022 if( size>mem3.szKeyBlk ){
28023 mem3.iKeyBlk = i;
28024 mem3.szKeyBlk = size;
28025 }
28026 }
28027}
28028
28029/*
28030** Return a block of memory of at least nBytes in size.
28031** Return NULL if unable.
28032**
28033** This function assumes that the necessary mutexes, if any, are
28034** already held by the caller. Hence "Unsafe".
28035*/
28036static void *memsys3MallocUnsafe(int nByte){
28037 u32 i;
28038 u32 nBlock;
28039 u32 toFree;
28040
28041 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28042 assert( sizeof(Mem3Block)==8 )((void) (0));
28043 if( nByte<=12 ){
28044 nBlock = 2;
28045 }else{
28046 nBlock = (nByte + 11)/8;
28047 }
28048 assert( nBlock>=2 )((void) (0));
28049
28050 /* STEP 1:
28051 ** Look for an entry of the correct size in either the small
28052 ** chunk table or in the large chunk hash table. This is
28053 ** successful most of the time (about 9 times out of 10).
28054 */
28055 if( nBlock <= MX_SMALL ){
28056 i = mem3.aiSmall[nBlock-2];
28057 if( i>0 ){
28058 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
28059 return memsys3Checkout(i, nBlock);
28060 }
28061 }else{
28062 int hash = nBlock % N_HASH;
28063 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
28064 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
28065 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
28066 return memsys3Checkout(i, nBlock);
28067 }
28068 }
28069 }
28070
28071 /* STEP 2:
28072 ** Try to satisfy the allocation by carving a piece off of the end
28073 ** of the key chunk. This step usually works if step 1 fails.
28074 */
28075 if( mem3.szKeyBlk>=nBlock ){
28076 return memsys3FromKeyBlk(nBlock);
28077 }
28078
28079
28080 /* STEP 3:
28081 ** Loop through the entire memory pool. Coalesce adjacent free
28082 ** chunks. Recompute the key chunk as the largest free chunk.
28083 ** Then try again to satisfy the allocation by carving a piece off
28084 ** of the end of the key chunk. This step happens very
28085 ** rarely (we hope!)
28086 */
28087 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
28088 memsys3OutOfMemory(toFree);
28089 if( mem3.iKeyBlk ){
28090 memsys3Link(mem3.iKeyBlk);
28091 mem3.iKeyBlk = 0;
28092 mem3.szKeyBlk = 0;
28093 }
28094 for(i=0; i<N_HASH; i++){
28095 memsys3Merge(&mem3.aiHash[i]);
28096 }
28097 for(i=0; i<MX_SMALL-1; i++){
28098 memsys3Merge(&mem3.aiSmall[i]);
28099 }
28100 if( mem3.szKeyBlk ){
28101 memsys3Unlink(mem3.iKeyBlk);
28102 if( mem3.szKeyBlk>=nBlock ){
28103 return memsys3FromKeyBlk(nBlock);
28104 }
28105 }
28106 }
28107
28108 /* If none of the above worked, then we fail. */
28109 return 0;
28110}
28111
28112/*
28113** Free an outstanding memory allocation.
28114**
28115** This function assumes that the necessary mutexes, if any, are
28116** already held by the caller. Hence "Unsafe".
28117*/
28118static void memsys3FreeUnsafe(void *pOld){
28119 Mem3Block *p = (Mem3Block*)pOld;
28120 int i;
28121 u32 size, x;
28122 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
28123 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] )((void) (0));
28124 i = p - mem3.aPool;
28125 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 )((void) (0));
28126 size = mem3.aPool[i-1].u.hdr.size4x/4;
28127 assert( i+size<=mem3.nPool+1 )((void) (0));
28128 mem3.aPool[i-1].u.hdr.size4x &= ~1;
28129 mem3.aPool[i+size-1].u.hdr.prevSize = size;
28130 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
28131 memsys3Link(i);
28132
28133 /* Try to expand the key using the newly freed chunk */
28134 if( mem3.iKeyBlk ){
28135 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
28136 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
28137 mem3.iKeyBlk -= size;
28138 mem3.szKeyBlk += size;
28139 memsys3Unlink(mem3.iKeyBlk);
28140 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
28141 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
28142 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
28143 }
28144 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
28145 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
28146 memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk);
28147 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
28148 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
28149 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
28150 }
28151 }
28152}
28153
28154/*
28155** Return the size of an outstanding allocation, in bytes. The
28156** size returned omits the 8-byte header overhead. This only
28157** works for chunks that are currently checked out.
28158*/
28159static int memsys3Size(void *p){
28160 Mem3Block *pBlock;
28161 assert( p!=0 )((void) (0));
28162 pBlock = (Mem3Block*)p;
28163 assert( (pBlock[-1].u.hdr.size4x&1)!=0 )((void) (0));
28164 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
28165}
28166
28167/*
28168** Round up a request size to the next valid allocation size.
28169*/
28170static int memsys3Roundup(int n){
28171 if( n<=12 ){
28172 return 12;
28173 }else{
28174 return ((n+11)&~7) - 4;
28175 }
28176}
28177
28178/*
28179** Allocate nBytes of memory.
28180*/
28181static void *memsys3Malloc(int nBytes){
28182 sqlite3_int64 *p;
28183 assert( nBytes>0 )((void) (0)); /* malloc.c filters out 0 byte requests */
28184 memsys3Enter();
28185 p = memsys3MallocUnsafe(nBytes);
28186 memsys3Leave();
28187 return (void*)p;
28188}
28189
28190/*
28191** Free memory.
28192*/
28193static void memsys3Free(void *pPrior){
28194 assert( pPrior )((void) (0));
28195 memsys3Enter();
28196 memsys3FreeUnsafe(pPrior);
28197 memsys3Leave();
28198}
28199
28200/*
28201** Change the size of an existing memory allocation
28202*/
28203static void *memsys3Realloc(void *pPrior, int nBytes){
28204 int nOld;
28205 void *p;
28206 if( pPrior==0 ){
28207 return sqlite3_malloc(nBytes);
28208 }
28209 if( nBytes<=0 ){
28210 sqlite3_free(pPrior);
28211 return 0;
28212 }
28213 nOld = memsys3Size(pPrior);
28214 if( nBytes<=nOld && nBytes>=nOld-128 ){
28215 return pPrior;
28216 }
28217 memsys3Enter();
28218 p = memsys3MallocUnsafe(nBytes);
28219 if( p ){
28220 if( nOld<nBytes ){
28221 memcpy(p, pPrior, nOld);
28222 }else{
28223 memcpy(p, pPrior, nBytes);
28224 }
28225 memsys3FreeUnsafe(pPrior);
28226 }
28227 memsys3Leave();
28228 return p;
28229}
28230
28231/*
28232** Initialize this module.
28233*/
28234static int memsys3Init(void *NotUsed){
28235 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
28236 if( !sqlite3GlobalConfigsqlite3Config.pHeap ){
28237 return SQLITE_ERROR1;
28238 }
28239
28240 /* Store a pointer to the memory block in global structure mem3. */
28241 assert( sizeof(Mem3Block)==8 )((void) (0));
28242 mem3.aPool = (Mem3Block *)sqlite3GlobalConfigsqlite3Config.pHeap;
28243 mem3.nPool = (sqlite3GlobalConfigsqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
28244
28245 /* Initialize the key block. */
28246 mem3.szKeyBlk = mem3.nPool;
28247 mem3.mnKeyBlk = mem3.szKeyBlk;
28248 mem3.iKeyBlk = 1;
28249 mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2;
28250 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
28251 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
28252
28253 return SQLITE_OK0;
28254}
28255
28256/*
28257** Deinitialize this module.
28258*/
28259static void memsys3Shutdown(void *NotUsed){
28260 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
28261 mem3.mutex = 0;
28262 return;
28263}
28264
28265
28266
28267/*
28268** Open the file indicated and write a log of all unfreed memory
28269** allocations into that log.
28270*/
28271SQLITE_PRIVATEstatic void sqlite3Memsys3Dump(const char *zFilename){
28272#ifdef SQLITE_DEBUG
28273 FILE *out;
28274 u32 i, j;
28275 u32 size;
28276 if( zFilename==0 || zFilename[0]==0 ){
28277 out = stdoutstdout;
28278 }else{
28279 out = fopen(zFilename, "w");
28280 if( out==0 ){
28281 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
28282 zFilename);
28283 return;
28284 }
28285 }
28286 memsys3Enter();
28287 fprintf(out, "CHUNKS:\n");
28288 for(i=1; i<=mem3.nPool; i+=size/4){
28289 size = mem3.aPool[i-1].u.hdr.size4x;
28290 if( size/4<=1 ){
28291 fprintf(out, "%p size error\n", &mem3.aPool[i]);
28292 assert( 0 )((void) (0));
28293 break;
28294 }
28295 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
28296 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
28297 assert( 0 )((void) (0));
28298 break;
28299 }
28300 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
28301 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
28302 assert( 0 )((void) (0));
28303 break;
28304 }
28305 if( size&1 ){
28306 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
28307 }else{
28308 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
28309 i==mem3.iKeyBlk ? " **key**" : "");
28310 }
28311 }
28312 for(i=0; i<MX_SMALL-1; i++){
28313 if( mem3.aiSmall[i]==0 ) continue;
28314 fprintf(out, "small(%2d):", i);
28315 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
28316 fprintf(out, " %p(%d)", &mem3.aPool[j],
28317 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
28318 }
28319 fprintf(out, "\n");
28320 }
28321 for(i=0; i<N_HASH; i++){
28322 if( mem3.aiHash[i]==0 ) continue;
28323 fprintf(out, "hash(%2d):", i);
28324 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
28325 fprintf(out, " %p(%d)", &mem3.aPool[j],
28326 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
28327 }
28328 fprintf(out, "\n");
28329 }
28330 fprintf(out, "key=%d\n", mem3.iKeyBlk);
28331 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
28332 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
28333 sqlite3_mutex_leave(mem3.mutex);
28334 if( out==stdoutstdout ){
28335 fflush(stdoutstdout);
28336 }else{
28337 fclose(out);
28338 }
28339#else
28340 UNUSED_PARAMETER(zFilename)(void)(zFilename);
28341#endif
28342}
28343
28344/*
28345** This routine is the only routine in this file with external
28346** linkage.
28347**
28348** Populate the low-level memory allocation function pointers in
28349** sqlite3GlobalConfig.m with pointers to the routines in this file. The
28350** arguments specify the block of memory to manage.
28351**
28352** This routine is only called by sqlite3_config(), and therefore
28353** is not required to be threadsafe (it is not).
28354*/
28355SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
28356 static const sqlite3_mem_methods mempoolMethods = {
28357 memsys3Malloc,
28358 memsys3Free,
28359 memsys3Realloc,
28360 memsys3Size,
28361 memsys3Roundup,
28362 memsys3Init,
28363 memsys3Shutdown,
28364 0
28365 };
28366 return &mempoolMethods;
28367}
28368
28369#endif /* SQLITE_ENABLE_MEMSYS3 */
28370
28371/************** End of mem3.c ************************************************/
28372/************** Begin file mem5.c ********************************************/
28373/*
28374** 2007 October 14
28375**
28376** The author disclaims copyright to this source code. In place of
28377** a legal notice, here is a blessing:
28378**
28379** May you do good and not evil.
28380** May you find forgiveness for yourself and forgive others.
28381** May you share freely, never taking more than you give.
28382**
28383*************************************************************************
28384** This file contains the C functions that implement a memory
28385** allocation subsystem for use by SQLite.
28386**
28387** This version of the memory allocation subsystem omits all
28388** use of malloc(). The application gives SQLite a block of memory
28389** before calling sqlite3_initialize() from which allocations
28390** are made and returned by the xMalloc() and xRealloc()
28391** implementations. Once sqlite3_initialize() has been called,
28392** the amount of memory available to SQLite is fixed and cannot
28393** be changed.
28394**
28395** This version of the memory allocation subsystem is included
28396** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
28397**
28398** This memory allocator uses the following algorithm:
28399**
28400** 1. All memory allocation sizes are rounded up to a power of 2.
28401**
28402** 2. If two adjacent free blocks are the halves of a larger block,
28403** then the two blocks are coalesced into the single larger block.
28404**
28405** 3. New memory is allocated from the first available free block.
28406**
28407** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
28408** Concerning Dynamic Storage Allocation". Journal of the Association for
28409** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
28410**
28411** Let n be the size of the largest allocation divided by the minimum
28412** allocation size (after rounding all sizes up to a power of 2.) Let M
28413** be the maximum amount of memory ever outstanding at one time. Let
28414** N be the total amount of memory available for allocation. Robson
28415** proved that this memory allocator will never breakdown due to
28416** fragmentation as long as the following constraint holds:
28417**
28418** N >= M*(1 + log2(n)/2) - n + 1
28419**
28420** The sqlite3_status() logic tracks the maximum values of n and M so
28421** that an application can, at any time, verify this constraint.
28422*/
28423/* #include "sqliteInt.h" */
28424
28425/*
28426** This version of the memory allocator is used only when
28427** SQLITE_ENABLE_MEMSYS5 is defined.
28428*/
28429#ifdef SQLITE_ENABLE_MEMSYS5
28430
28431/*
28432** A minimum allocation is an instance of the following structure.
28433** Larger allocations are an array of these structures where the
28434** size of the array is a power of 2.
28435**
28436** The size of this object must be a power of two. That fact is
28437** verified in memsys5Init().
28438*/
28439typedef struct Mem5Link Mem5Link;
28440struct Mem5Link {
28441 int next; /* Index of next free chunk */
28442 int prev; /* Index of previous free chunk */
28443};
28444
28445/*
28446** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
28447** mem5.szAtom is always at least 8 and 32-bit integers are used,
28448** it is not actually possible to reach this limit.
28449*/
28450#define LOGMAX 30
28451
28452/*
28453** Masks used for mem5.aCtrl[] elements.
28454*/
28455#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
28456#define CTRL_FREE 0x20 /* True if not checked out */
28457
28458/*
28459** All of the static variables used by this module are collected
28460** into a single structure named "mem5". This is to keep the
28461** static variables organized and to reduce namespace pollution
28462** when this module is combined with other in the amalgamation.
28463*/
28464static SQLITE_WSD struct Mem5Global {
28465 /*
28466 ** Memory available for allocation
28467 */
28468 int szAtom; /* Smallest possible allocation in bytes */
28469 int nBlock; /* Number of szAtom sized blocks in zPool */
28470 u8 *zPool; /* Memory available to be allocated */
28471
28472 /*
28473 ** Mutex to control access to the memory allocation subsystem.
28474 */
28475 sqlite3_mutex *mutex;
28476
28477#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28478 /*
28479 ** Performance statistics
28480 */
28481 u64 nAlloc; /* Total number of calls to malloc */
28482 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
28483 u64 totalExcess; /* Total internal fragmentation */
28484 u32 currentOut; /* Current checkout, including internal fragmentation */
28485 u32 currentCount; /* Current number of distinct checkouts */
28486 u32 maxOut; /* Maximum instantaneous currentOut */
28487 u32 maxCount; /* Maximum instantaneous currentCount */
28488 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
28489#endif
28490
28491 /*
28492 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
28493 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
28494 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
28495 */
28496 int aiFreelist[LOGMAX+1];
28497
28498 /*
28499 ** Space for tracking which blocks are checked out and the size
28500 ** of each block. One byte per block.
28501 */
28502 u8 *aCtrl;
28503
28504} mem5;
28505
28506/*
28507** Access the static variable through a macro for SQLITE_OMIT_WSD.
28508*/
28509#define mem5 GLOBAL(struct Mem5Global, mem5)mem5
28510
28511/*
28512** Assuming mem5.zPool is divided up into an array of Mem5Link
28513** structures, return a pointer to the idx-th such link.
28514*/
28515#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
28516
28517/*
28518** Unlink the chunk at mem5.aPool[i] from list it is currently
28519** on. It should be found on mem5.aiFreelist[iLogsize].
28520*/
28521static void memsys5Unlink(int i, int iLogsize){
28522 int next, prev;
28523 assert( i>=0 && i<mem5.nBlock )((void) (0));
28524 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
28525 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
28526
28527 next = MEM5LINK(i)->next;
28528 prev = MEM5LINK(i)->prev;
28529 if( prev<0 ){
28530 mem5.aiFreelist[iLogsize] = next;
28531 }else{
28532 MEM5LINK(prev)->next = next;
28533 }
28534 if( next>=0 ){
28535 MEM5LINK(next)->prev = prev;
28536 }
28537}
28538
28539/*
28540** Link the chunk at mem5.aPool[i] so that is on the iLogsize
28541** free list.
28542*/
28543static void memsys5Link(int i, int iLogsize){
28544 int x;
28545 assert( sqlite3_mutex_held(mem5.mutex) )((void) (0));
28546 assert( i>=0 && i<mem5.nBlock )((void) (0));
28547 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
28548 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
28549
28550 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
28551 MEM5LINK(i)->prev = -1;
28552 if( x>=0 ){
28553 assert( x<mem5.nBlock )((void) (0));
28554 MEM5LINK(x)->prev = i;
28555 }
28556 mem5.aiFreelist[iLogsize] = i;
28557}
28558
28559/*
28560** Obtain or release the mutex needed to access global data structures.
28561*/
28562static void memsys5Enter(void){
28563 sqlite3_mutex_enter(mem5.mutex);
28564}
28565static void memsys5Leave(void){
28566 sqlite3_mutex_leave(mem5.mutex);
28567}
28568
28569/*
28570** Return the size of an outstanding allocation, in bytes.
28571** This only works for chunks that are currently checked out.
28572*/
28573static int memsys5Size(void *p){
28574 int iSize, i;
28575 assert( p!=0 )((void) (0));
28576 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
28577 assert( i>=0 && i<mem5.nBlock )((void) (0));
28578 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
28579 return iSize;
28580}
28581
28582/*
28583** Return a block of memory of at least nBytes in size.
28584** Return NULL if unable. Return NULL if nBytes==0.
28585**
28586** The caller guarantees that nByte is positive.
28587**
28588** The caller has obtained a mutex prior to invoking this
28589** routine so there is never any chance that two or more
28590** threads can be in this routine at the same time.
28591*/
28592static void *memsys5MallocUnsafe(int nByte){
28593 int i; /* Index of a mem5.aPool[] slot */
28594 int iBin; /* Index into mem5.aiFreelist[] */
28595 int iFullSz; /* Size of allocation rounded up to power of 2 */
28596 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
28597
28598 /* nByte must be a positive */
28599 assert( nByte>0 )((void) (0));
28600
28601 /* No more than 1GiB per allocation */
28602 if( nByte > 0x40000000 ) return 0;
28603
28604#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28605 /* Keep track of the maximum allocation request. Even unfulfilled
28606 ** requests are counted */
28607 if( (u32)nByte>mem5.maxRequest ){
28608 mem5.maxRequest = nByte;
28609 }
28610#endif
28611
28612
28613 /* Round nByte up to the next valid power of two */
28614 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
28615
28616 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
28617 ** block. If not, then split a block of the next larger power of
28618 ** two in order to create a new free block of size iLogsize.
28619 */
28620 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
28621 if( iBin>LOGMAX ){
28622 testcase( sqlite3GlobalConfig.xLog!=0 );
28623 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes", nByte);
28624 return 0;
28625 }
28626 i = mem5.aiFreelist[iBin];
28627 memsys5Unlink(i, iBin);
28628 while( iBin>iLogsize ){
28629 int newSize;
28630
28631 iBin--;
28632 newSize = 1 << iBin;
28633 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
28634 memsys5Link(i+newSize, iBin);
28635 }
28636 mem5.aCtrl[i] = iLogsize;
28637
28638#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28639 /* Update allocator performance statistics. */
28640 mem5.nAlloc++;
28641 mem5.totalAlloc += iFullSz;
28642 mem5.totalExcess += iFullSz - nByte;
28643 mem5.currentCount++;
28644 mem5.currentOut += iFullSz;
28645 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
28646 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
28647#endif
28648
28649#ifdef SQLITE_DEBUG
28650 /* Make sure the allocated memory does not assume that it is set to zero
28651 ** or retains a value from a previous allocation */
28652 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
28653#endif
28654
28655 /* Return a pointer to the allocated memory. */
28656 return (void*)&mem5.zPool[i*mem5.szAtom];
28657}
28658
28659/*
28660** Free an outstanding memory allocation.
28661*/
28662static void memsys5FreeUnsafe(void *pOld){
28663 u32 size, iLogsize;
28664 int iBlock;
28665
28666 /* Set iBlock to the index of the block pointed to by pOld in
28667 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
28668 */
28669 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
28670
28671 /* Check that the pointer pOld points to a valid, non-free block. */
28672 assert( iBlock>=0 && iBlock<mem5.nBlock )((void) (0));
28673 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 )((void) (0));
28674 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 )((void) (0));
28675
28676 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
28677 size = 1<<iLogsize;
28678 assert( iBlock+size-1<(u32)mem5.nBlock )((void) (0));
28679
28680 mem5.aCtrl[iBlock] |= CTRL_FREE;
28681 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
28682
28683#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
28684 assert( mem5.currentCount>0 )((void) (0));
28685 assert( mem5.currentOut>=(size*mem5.szAtom) )((void) (0));
28686 mem5.currentCount--;
28687 mem5.currentOut -= size*mem5.szAtom;
28688 assert( mem5.currentOut>0 || mem5.currentCount==0 )((void) (0));
28689 assert( mem5.currentCount>0 || mem5.currentOut==0 )((void) (0));
28690#endif
28691
28692 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
28693 while( ALWAYS(iLogsize<LOGMAX)(iLogsize<LOGMAX) ){
28694 int iBuddy;
28695 if( (iBlock>>iLogsize) & 1 ){
28696 iBuddy = iBlock - size;
28697 assert( iBuddy>=0 )((void) (0));
28698 }else{
28699 iBuddy = iBlock + size;
28700 if( iBuddy>=mem5.nBlock ) break;
28701 }
28702 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
28703 memsys5Unlink(iBuddy, iLogsize);
28704 iLogsize++;
28705 if( iBuddy<iBlock ){
28706 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
28707 mem5.aCtrl[iBlock] = 0;
28708 iBlock = iBuddy;
28709 }else{
28710 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
28711 mem5.aCtrl[iBuddy] = 0;
28712 }
28713 size *= 2;
28714 }
28715
28716#ifdef SQLITE_DEBUG
28717 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
28718 ** not used after being freed */
28719 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
28720#endif
28721
28722 memsys5Link(iBlock, iLogsize);
28723}
28724
28725/*
28726** Allocate nBytes of memory.
28727*/
28728static void *memsys5Malloc(int nBytes){
28729 sqlite3_int64 *p = 0;
28730 if( nBytes>0 ){
28731 memsys5Enter();
28732 p = memsys5MallocUnsafe(nBytes);
28733 memsys5Leave();
28734 }
28735 return (void*)p;
28736}
28737
28738/*
28739** Free memory.
28740**
28741** The outer layer memory allocator prevents this routine from
28742** being called with pPrior==0.
28743*/
28744static void memsys5Free(void *pPrior){
28745 assert( pPrior!=0 )((void) (0));
28746 memsys5Enter();
28747 memsys5FreeUnsafe(pPrior);
28748 memsys5Leave();
28749}
28750
28751/*
28752** Change the size of an existing memory allocation.
28753**
28754** The outer layer memory allocator prevents this routine from
28755** being called with pPrior==0.
28756**
28757** nBytes is always a value obtained from a prior call to
28758** memsys5Round(). Hence nBytes is always a non-negative power
28759** of two. If nBytes==0 that means that an oversize allocation
28760** (an allocation larger than 0x40000000) was requested and this
28761** routine should return 0 without freeing pPrior.
28762*/
28763static void *memsys5Realloc(void *pPrior, int nBytes){
28764 int nOld;
28765 void *p;
28766 assert( pPrior!=0 )((void) (0));
28767 assert( (nBytes&(nBytes-1))==0 )((void) (0)); /* EV: R-46199-30249 */
28768 assert( nBytes>=0 )((void) (0));
28769 if( nBytes==0 ){
28770 return 0;
28771 }
28772 nOld = memsys5Size(pPrior);
28773 if( nBytes<=nOld ){
28774 return pPrior;
28775 }
28776 p = memsys5Malloc(nBytes);
28777 if( p ){
28778 memcpy(p, pPrior, nOld);
28779 memsys5Free(pPrior);
28780 }
28781 return p;
28782}
28783
28784/*
28785** Round up a request size to the next valid allocation size. If
28786** the allocation is too large to be handled by this allocation system,
28787** return 0.
28788**
28789** All allocations must be a power of two and must be expressed by a
28790** 32-bit signed integer. Hence the largest allocation is 0x40000000
28791** or 1073741824 bytes.
28792*/
28793static int memsys5Roundup(int n){
28794 int iFullSz;
28795 if( n<=mem5.szAtom*2 ){
28796 if( n<=mem5.szAtom ) return mem5.szAtom;
28797 return mem5.szAtom*2;
28798 }
28799 if( n>0x10000000 ){
28800 if( n>0x40000000 ) return 0;
28801 if( n>0x20000000 ) return 0x40000000;
28802 return 0x20000000;
28803 }
28804 for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4);
28805 if( (iFullSz/2)>=(i64)n ) return iFullSz/2;
28806 return iFullSz;
28807}
28808
28809/*
28810** Return the ceiling of the logarithm base 2 of iValue.
28811**
28812** Examples: memsys5Log(1) -> 0
28813** memsys5Log(2) -> 1
28814** memsys5Log(4) -> 2
28815** memsys5Log(5) -> 3
28816** memsys5Log(8) -> 3
28817** memsys5Log(9) -> 4
28818*/
28819static int memsys5Log(int iValue){
28820 int iLog;
28821 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
28822 return iLog;
28823}
28824
28825/*
28826** Initialize the memory allocator.
28827**
28828** This routine is not threadsafe. The caller must be holding a mutex
28829** to prevent multiple threads from entering at the same time.
28830*/
28831static int memsys5Init(void *NotUsed){
28832 int ii; /* Loop counter */
28833 int nByte; /* Number of bytes of memory available to this allocator */
28834 u8 *zByte; /* Memory usable by this allocator */
28835 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
28836 int iOffset; /* An offset into mem5.aCtrl[] */
28837
28838 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
28839
28840 /* For the purposes of this routine, disable the mutex */
28841 mem5.mutex = 0;
28842
28843 /* The size of a Mem5Link object must be a power of two. Verify that
28844 ** this is case.
28845 */
28846 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 )((void) (0));
28847
28848 nByte = sqlite3GlobalConfigsqlite3Config.nHeap;
28849 zByte = (u8*)sqlite3GlobalConfigsqlite3Config.pHeap;
28850 assert( zByte!=0 )((void) (0)); /* sqlite3_config() does not allow otherwise */
28851
28852 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
28853 nMinLog = memsys5Log(sqlite3GlobalConfigsqlite3Config.mnReq);
28854 mem5.szAtom = (1<<nMinLog);
28855 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
28856 mem5.szAtom = mem5.szAtom << 1;
28857 }
28858
28859 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
28860 mem5.zPool = zByte;
28861 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
28862
28863 for(ii=0; ii<=LOGMAX; ii++){
28864 mem5.aiFreelist[ii] = -1;
28865 }
28866
28867 iOffset = 0;
28868 for(ii=LOGMAX; ii>=0; ii--){
28869 int nAlloc = (1<<ii);
28870 if( (iOffset+nAlloc)<=mem5.nBlock ){
28871 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
28872 memsys5Link(iOffset, ii);
28873 iOffset += nAlloc;
28874 }
28875 assert((iOffset+nAlloc)>mem5.nBlock)((void) (0));
28876 }
28877
28878 /* If a mutex is required for normal operation, allocate one */
28879 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 ){
28880 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
28881 }
28882
28883 return SQLITE_OK0;
28884}
28885
28886/*
28887** Deinitialize this module.
28888*/
28889static void memsys5Shutdown(void *NotUsed){
28890 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
28891 mem5.mutex = 0;
28892 return;
28893}
28894
28895#ifdef SQLITE_TEST
28896/*
28897** Open the file indicated and write a log of all unfreed memory
28898** allocations into that log.
28899*/
28900SQLITE_PRIVATEstatic void sqlite3Memsys5Dump(const char *zFilename){
28901 FILE *out;
28902 int i, j, n;
28903 int nMinLog;
28904
28905 if( zFilename==0 || zFilename[0]==0 ){
28906 out = stdoutstdout;
28907 }else{
28908 out = fopen(zFilename, "w");
28909 if( out==0 ){
28910 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
28911 zFilename);
28912 return;
28913 }
28914 }
28915 memsys5Enter();
28916 nMinLog = memsys5Log(mem5.szAtom);
28917 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
28918 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
28919 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
28920 }
28921 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
28922 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
28923 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
28924 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
28925 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
28926 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
28927 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
28928 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
28929 memsys5Leave();
28930 if( out==stdoutstdout ){
28931 fflush(stdoutstdout);
28932 }else{
28933 fclose(out);
28934 }
28935}
28936#endif
28937
28938/*
28939** This routine is the only routine in this file with external
28940** linkage. It returns a pointer to a static sqlite3_mem_methods
28941** struct populated with the memsys5 methods.
28942*/
28943SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
28944 static const sqlite3_mem_methods memsys5Methods = {
28945 memsys5Malloc,
28946 memsys5Free,
28947 memsys5Realloc,
28948 memsys5Size,
28949 memsys5Roundup,
28950 memsys5Init,
28951 memsys5Shutdown,
28952 0
28953 };
28954 return &memsys5Methods;
28955}
28956
28957#endif /* SQLITE_ENABLE_MEMSYS5 */
28958
28959/************** End of mem5.c ************************************************/
28960/************** Begin file mutex.c *******************************************/
28961/*
28962** 2007 August 14
28963**
28964** The author disclaims copyright to this source code. In place of
28965** a legal notice, here is a blessing:
28966**
28967** May you do good and not evil.
28968** May you find forgiveness for yourself and forgive others.
28969** May you share freely, never taking more than you give.
28970**
28971*************************************************************************
28972** This file contains the C functions that implement mutexes.
28973**
28974** This file contains code that is common across all mutex implementations.
28975*/
28976/* #include "sqliteInt.h" */
28977
28978#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
28979/*
28980** For debugging purposes, record when the mutex subsystem is initialized
28981** and uninitialized so that we can assert() if there is an attempt to
28982** allocate a mutex while the system is uninitialized.
28983*/
28984static SQLITE_WSD int mutexIsInit = 0;
28985#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
28986
28987
28988#ifndef SQLITE_MUTEX_OMIT
28989
28990#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
28991/*
28992** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
28993** the implementation of a wrapper around the system default mutex
28994** implementation (sqlite3DefaultMutex()).
28995**
28996** Most calls are passed directly through to the underlying default
28997** mutex implementation. Except, if a mutex is configured by calling
28998** sqlite3MutexWarnOnContention() on it, then if contention is ever
28999** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
29000**
29001** This type of mutex is used as the database handle mutex when testing
29002** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
29003*/
29004
29005/*
29006** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
29007** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
29008** allocated by the system mutex implementation. Variable iType is usually set
29009** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
29010** or one of the static mutex identifiers. Or, if this is a recursive mutex
29011** that has been configured using sqlite3MutexWarnOnContention(), it is
29012** set to SQLITE_MUTEX_WARNONCONTENTION.
29013*/
29014typedef struct CheckMutex CheckMutex;
29015struct CheckMutex {
29016 int iType;
29017 sqlite3_mutex *mutex;
29018};
29019
29020#define SQLITE_MUTEX_WARNONCONTENTION (-1)
29021
29022/*
29023** Pointer to real mutex methods object used by the CheckMutex
29024** implementation. Set by checkMutexInit().
29025*/
29026static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
29027
29028#ifdef SQLITE_DEBUG
29029static int checkMutexHeld(sqlite3_mutex *p){
29030 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
29031}
29032static int checkMutexNotheld(sqlite3_mutex *p){
29033 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
29034}
29035#endif
29036
29037/*
29038** Initialize and deinitialize the mutex subsystem.
29039*/
29040static int checkMutexInit(void){
29041 pGlobalMutexMethods = sqlite3DefaultMutex();
29042 return SQLITE_OK0;
29043}
29044static int checkMutexEnd(void){
29045 pGlobalMutexMethods = 0;
29046 return SQLITE_OK0;
29047}
29048
29049/*
29050** Allocate a mutex.
29051*/
29052static sqlite3_mutex *checkMutexAlloc(int iType){
29053 static CheckMutex staticMutexes[] = {
29054 {2, 0}, {3, 0}, {4, 0}, {5, 0},
29055 {6, 0}, {7, 0}, {8, 0}, {9, 0},
29056 {10, 0}, {11, 0}, {12, 0}, {13, 0}
29057 };
29058 CheckMutex *p = 0;
29059
29060 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 )((void) (0));
29061 if( iType<2 ){
29062 p = sqlite3MallocZero(sizeof(CheckMutex));
29063 if( p==0 ) return 0;
29064 p->iType = iType;
29065 }else{
29066#ifdef SQLITE_ENABLE_API_ARMOR1
29067 if( iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
29068 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29068);
29069 return 0;
29070 }
29071#endif
29072 p = &staticMutexes[iType-2];
29073 }
29074
29075 if( p->mutex==0 ){
29076 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
29077 if( p->mutex==0 ){
29078 if( iType<2 ){
29079 sqlite3_free(p);
29080 }
29081 p = 0;
29082 }
29083 }
29084
29085 return (sqlite3_mutex*)p;
29086}
29087
29088/*
29089** Free a mutex.
29090*/
29091static void checkMutexFree(sqlite3_mutex *p){
29092 assert( SQLITE_MUTEX_RECURSIVE<2 )((void) (0));
29093 assert( SQLITE_MUTEX_FAST<2 )((void) (0));
29094 assert( SQLITE_MUTEX_WARNONCONTENTION<2 )((void) (0));
29095
29096#ifdef SQLITE_ENABLE_API_ARMOR1
29097 if( ((CheckMutex*)p)->iType<2 )
29098#endif
29099 {
29100 CheckMutex *pCheck = (CheckMutex*)p;
29101 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
29102 sqlite3_free(pCheck);
29103 }
29104#ifdef SQLITE_ENABLE_API_ARMOR1
29105 else{
29106 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29106);
29107 }
29108#endif
29109}
29110
29111/*
29112** Enter the mutex.
29113*/
29114static void checkMutexEnter(sqlite3_mutex *p){
29115 CheckMutex *pCheck = (CheckMutex*)p;
29116 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
29117 if( SQLITE_OK0==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
29118 return;
29119 }
29120 sqlite3_log(SQLITE_MISUSE21,
29121 "illegal multi-threaded access to database connection"
29122 );
29123 }
29124 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
29125}
29126
29127/*
29128** Enter the mutex (do not block).
29129*/
29130static int checkMutexTry(sqlite3_mutex *p){
29131 CheckMutex *pCheck = (CheckMutex*)p;
29132 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
29133}
29134
29135/*
29136** Leave the mutex.
29137*/
29138static void checkMutexLeave(sqlite3_mutex *p){
29139 CheckMutex *pCheck = (CheckMutex*)p;
29140 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
29141}
29142
29143sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
29144 static const sqlite3_mutex_methods sMutex = {
29145 checkMutexInit,
29146 checkMutexEnd,
29147 checkMutexAlloc,
29148 checkMutexFree,
29149 checkMutexEnter,
29150 checkMutexTry,
29151 checkMutexLeave,
29152#ifdef SQLITE_DEBUG
29153 checkMutexHeld,
29154 checkMutexNotheld
29155#else
29156 0,
29157 0
29158#endif
29159 };
29160 return &sMutex;
29161}
29162
29163/*
29164** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
29165** one on which there should be no contention.
29166*/
29167SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
29168 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc==checkMutexAlloc ){
29169 CheckMutex *pCheck = (CheckMutex*)p;
29170 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE )((void) (0));
29171 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
29172 }
29173}
29174#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
29175
29176/*
29177** Initialize the mutex system.
29178*/
29179SQLITE_PRIVATEstatic int sqlite3MutexInit(void){
29180 int rc = SQLITE_OK0;
29181 if( !sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc ){
29182 /* If the xMutexAlloc method has not been set, then the user did not
29183 ** install a mutex implementation via sqlite3_config() prior to
29184 ** sqlite3_initialize() being called. This block copies pointers to
29185 ** the default implementation into the sqlite3GlobalConfig structure.
29186 */
29187 sqlite3_mutex_methods const *pFrom;
29188 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfigsqlite3Config.mutex;
29189
29190 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
29191#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
29192 pFrom = multiThreadedCheckMutex();
29193#else
29194 pFrom = sqlite3DefaultMutex();
29195#endif
29196 }else{
29197 pFrom = sqlite3NoopMutex();
29198 }
29199 pTo->xMutexInit = pFrom->xMutexInit;
29200 pTo->xMutexEnd = pFrom->xMutexEnd;
29201 pTo->xMutexFree = pFrom->xMutexFree;
29202 pTo->xMutexEnter = pFrom->xMutexEnter;
29203 pTo->xMutexTry = pFrom->xMutexTry;
29204 pTo->xMutexLeave = pFrom->xMutexLeave;
29205 pTo->xMutexHeld = pFrom->xMutexHeld;
29206 pTo->xMutexNotheld = pFrom->xMutexNotheld;
29207 sqlite3MemoryBarrier();
29208 pTo->xMutexAlloc = pFrom->xMutexAlloc;
29209 }
29210 assert( sqlite3GlobalConfig.mutex.xMutexInit )((void) (0));
29211 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexInit();
29212
29213#ifdef SQLITE_DEBUG
29214 GLOBAL(int, mutexIsInit)mutexIsInit = 1;
29215#endif
29216
29217 sqlite3MemoryBarrier();
29218 return rc;
29219}
29220
29221/*
29222** Shutdown the mutex system. This call frees resources allocated by
29223** sqlite3MutexInit().
29224*/
29225SQLITE_PRIVATEstatic int sqlite3MutexEnd(void){
29226 int rc = SQLITE_OK0;
29227 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd ){
29228 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd();
29229 }
29230
29231#ifdef SQLITE_DEBUG
29232 GLOBAL(int, mutexIsInit)mutexIsInit = 0;
29233#endif
29234
29235 return rc;
29236}
29237
29238/*
29239** Retrieve a pointer to a static mutex or allocate a new dynamic one.
29240*/
29241SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
29242#ifndef SQLITE_OMIT_AUTOINIT
29243 if( id<=SQLITE_MUTEX_RECURSIVE1 && sqlite3_initialize() ) return 0;
29244 if( id>SQLITE_MUTEX_RECURSIVE1 && sqlite3MutexInit() ) return 0;
29245#endif
29246 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
29247 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
29248}
29249
29250SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int id){
29251 if( !sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
29252 return 0;
29253 }
29254 assert( GLOBAL(int, mutexIsInit) )((void) (0));
29255 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
29256 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
29257}
29258
29259/*
29260** Free a dynamic mutex.
29261*/
29262SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
29263 if( p ){
29264 assert( sqlite3GlobalConfig.mutex.xMutexFree )((void) (0));
29265 sqlite3GlobalConfigsqlite3Config.mutex.xMutexFree(p);
29266 }
29267}
29268
29269/*
29270** Obtain the mutex p. If some other thread already has the mutex, block
29271** until it can be obtained.
29272*/
29273SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
29274 if( p ){
29275 assert( sqlite3GlobalConfig.mutex.xMutexEnter )((void) (0));
29276 sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnter(p);
29277 }
29278}
29279
29280/*
29281** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
29282** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
29283*/
29284SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
29285 int rc = SQLITE_OK0;
29286 if( p ){
29287 assert( sqlite3GlobalConfig.mutex.xMutexTry )((void) (0));
29288 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexTry(p);
29289 }
29290 return rc;
29291}
29292
29293/*
29294** The sqlite3_mutex_leave() routine exits a mutex that was previously
29295** entered by the same thread. The behavior is undefined if the mutex
29296** is not currently entered. If a NULL pointer is passed as an argument
29297** this function is a no-op.
29298*/
29299SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
29300 if( p ){
29301 assert( sqlite3GlobalConfig.mutex.xMutexLeave )((void) (0));
29302 sqlite3GlobalConfigsqlite3Config.mutex.xMutexLeave(p);
29303 }
29304}
29305
29306#ifndef NDEBUG1
29307/*
29308** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29309** intended for use inside assert() statements.
29310**
29311** Because these routines raise false-positive alerts in TSAN, disable
29312** them (make them always return 1) when compiling with TSAN.
29313*/
29314SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
29315# if defined(__has_feature)0
29316# if __has_feature(thread_sanitizer)0
29317 p = 0;
29318# endif
29319# endif
29320 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld )((void) (0));
29321 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexHeld(p);
29322}
29323SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
29324# if defined(__has_feature)0
29325# if __has_feature(thread_sanitizer)0
29326 p = 0;
29327# endif
29328# endif
29329 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld )((void) (0));
29330 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexNotheld(p);
29331}
29332#endif /* NDEBUG */
29333
29334#endif /* !defined(SQLITE_MUTEX_OMIT) */
29335
29336/************** End of mutex.c ***********************************************/
29337/************** Begin file mutex_noop.c **************************************/
29338/*
29339** 2008 October 07
29340**
29341** The author disclaims copyright to this source code. In place of
29342** a legal notice, here is a blessing:
29343**
29344** May you do good and not evil.
29345** May you find forgiveness for yourself and forgive others.
29346** May you share freely, never taking more than you give.
29347**
29348*************************************************************************
29349** This file contains the C functions that implement mutexes.
29350**
29351** This implementation in this file does not provide any mutual
29352** exclusion and is thus suitable for use only in applications
29353** that use SQLite in a single thread. The routines defined
29354** here are place-holders. Applications can substitute working
29355** mutex routines at start-time using the
29356**
29357** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
29358**
29359** interface.
29360**
29361** If compiled with SQLITE_DEBUG, then additional logic is inserted
29362** that does error checking on mutexes to make sure they are being
29363** called correctly.
29364*/
29365/* #include "sqliteInt.h" */
29366
29367#ifndef SQLITE_MUTEX_OMIT
29368
29369#ifndef SQLITE_DEBUG
29370/*
29371** Stub routines for all mutex methods.
29372**
29373** This routines provide no mutual exclusion or error checking.
29374*/
29375static int noopMutexInit(void){ return SQLITE_OK0; }
29376static int noopMutexEnd(void){ return SQLITE_OK0; }
29377static sqlite3_mutex *noopMutexAlloc(int id){
29378 UNUSED_PARAMETER(id)(void)(id);
29379 return (sqlite3_mutex*)8;
29380}
29381static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
29382static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
29383static int noopMutexTry(sqlite3_mutex *p){
29384 UNUSED_PARAMETER(p)(void)(p);
29385 return SQLITE_OK0;
29386}
29387static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
29388
29389SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
29390 static const sqlite3_mutex_methods sMutex = {
29391 noopMutexInit,
29392 noopMutexEnd,
29393 noopMutexAlloc,
29394 noopMutexFree,
29395 noopMutexEnter,
29396 noopMutexTry,
29397 noopMutexLeave,
29398
29399 0,
29400 0,
29401 };
29402
29403 return &sMutex;
29404}
29405#endif /* !SQLITE_DEBUG */
29406
29407#ifdef SQLITE_DEBUG
29408/*
29409** In this implementation, error checking is provided for testing
29410** and debugging purposes. The mutexes still do not provide any
29411** mutual exclusion.
29412*/
29413
29414/*
29415** The mutex object
29416*/
29417typedef struct sqlite3_debug_mutex {
29418 int id; /* The mutex type */
29419 int cnt; /* Number of entries without a matching leave */
29420} sqlite3_debug_mutex;
29421
29422/*
29423** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29424** intended for use inside assert() statements.
29425*/
29426static int debugMutexHeld(sqlite3_mutex *pX){
29427 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29428 return p==0 || p->cnt>0;
29429}
29430static int debugMutexNotheld(sqlite3_mutex *pX){
29431 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29432 return p==0 || p->cnt==0;
29433}
29434
29435/*
29436** Initialize and deinitialize the mutex subsystem.
29437*/
29438static int debugMutexInit(void){ return SQLITE_OK0; }
29439static int debugMutexEnd(void){ return SQLITE_OK0; }
29440
29441/*
29442** The sqlite3_mutex_alloc() routine allocates a new
29443** mutex and returns a pointer to it. If it returns NULL
29444** that means that a mutex could not be allocated.
29445*/
29446static sqlite3_mutex *debugMutexAlloc(int id){
29447 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS313 - 1];
29448 sqlite3_debug_mutex *pNew = 0;
29449 switch( id ){
29450 case SQLITE_MUTEX_FAST0:
29451 case SQLITE_MUTEX_RECURSIVE1: {
29452 pNew = sqlite3Malloc(sizeof(*pNew));
29453 if( pNew ){
29454 pNew->id = id;
29455 pNew->cnt = 0;
29456 }
29457 break;
29458 }
29459 default: {
29460#ifdef SQLITE_ENABLE_API_ARMOR1
29461 if( id-2<0 || id-2>=ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
29462 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29462);
29463 return 0;
29464 }
29465#endif
29466 pNew = &aStatic[id-2];
29467 pNew->id = id;
29468 break;
29469 }
29470 }
29471 return (sqlite3_mutex*)pNew;
29472}
29473
29474/*
29475** This routine deallocates a previously allocated mutex.
29476*/
29477static void debugMutexFree(sqlite3_mutex *pX){
29478 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29479 assert( p->cnt==0 )((void) (0));
29480 if( p->id==SQLITE_MUTEX_RECURSIVE1 || p->id==SQLITE_MUTEX_FAST0 ){
29481 sqlite3_free(p);
29482 }else{
29483#ifdef SQLITE_ENABLE_API_ARMOR1
29484 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29484);
29485#endif
29486 }
29487}
29488
29489/*
29490** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
29491** to enter a mutex. If another thread is already within the mutex,
29492** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
29493** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
29494** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
29495** be entered multiple times by the same thread. In such cases the,
29496** mutex must be exited an equal number of times before another thread
29497** can enter. If the same thread tries to enter any other kind of mutex
29498** more than once, the behavior is undefined.
29499*/
29500static void debugMutexEnter(sqlite3_mutex *pX){
29501 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29502 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
29503 p->cnt++;
29504}
29505static int debugMutexTry(sqlite3_mutex *pX){
29506 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29507 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
29508 p->cnt++;
29509 return SQLITE_OK0;
29510}
29511
29512/*
29513** The sqlite3_mutex_leave() routine exits a mutex that was
29514** previously entered by the same thread. The behavior
29515** is undefined if the mutex is not currently entered or
29516** is not currently allocated. SQLite will never do either.
29517*/
29518static void debugMutexLeave(sqlite3_mutex *pX){
29519 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
29520 assert( debugMutexHeld(pX) )((void) (0));
29521 p->cnt--;
29522 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
29523}
29524
29525SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
29526 static const sqlite3_mutex_methods sMutex = {
29527 debugMutexInit,
29528 debugMutexEnd,
29529 debugMutexAlloc,
29530 debugMutexFree,
29531 debugMutexEnter,
29532 debugMutexTry,
29533 debugMutexLeave,
29534
29535 debugMutexHeld,
29536 debugMutexNotheld
29537 };
29538
29539 return &sMutex;
29540}
29541#endif /* SQLITE_DEBUG */
29542
29543/*
29544** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
29545** is used regardless of the run-time threadsafety setting.
29546*/
29547#ifdef SQLITE_MUTEX_NOOP
29548SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
29549 return sqlite3NoopMutex();
29550}
29551#endif /* defined(SQLITE_MUTEX_NOOP) */
29552#endif /* !defined(SQLITE_MUTEX_OMIT) */
29553
29554/************** End of mutex_noop.c ******************************************/
29555/************** Begin file mutex_unix.c **************************************/
29556/*
29557** 2007 August 28
29558**
29559** The author disclaims copyright to this source code. In place of
29560** a legal notice, here is a blessing:
29561**
29562** May you do good and not evil.
29563** May you find forgiveness for yourself and forgive others.
29564** May you share freely, never taking more than you give.
29565**
29566*************************************************************************
29567** This file contains the C functions that implement mutexes for pthreads
29568*/
29569/* #include "sqliteInt.h" */
29570
29571/*
29572** The code in this file is only used if we are compiling threadsafe
29573** under unix with pthreads.
29574**
29575** Note that this implementation requires a version of pthreads that
29576** supports recursive mutexes.
29577*/
29578#ifdef SQLITE_MUTEX_PTHREADS
29579
29580#include <pthread.h>
29581
29582/*
29583** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
29584** are necessary under two conditions: (1) Debug builds and (2) using
29585** home-grown mutexes. Encapsulate these conditions into a single #define.
29586*/
29587#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
29588# define SQLITE_MUTEX_NREF0 1
29589#else
29590# define SQLITE_MUTEX_NREF0 0
29591#endif
29592
29593/*
29594** Each recursive mutex is an instance of the following structure.
29595*/
29596struct sqlite3_mutex {
29597 pthread_mutex_t mutex; /* Mutex controlling the lock */
29598#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
29599 int id; /* Mutex type */
29600#endif
29601#if SQLITE_MUTEX_NREF0
29602 volatile int nRef; /* Number of entrances */
29603 volatile pthread_t owner; /* Thread that is within this mutex */
29604 int trace; /* True to trace changes */
29605#endif
29606};
29607#if SQLITE_MUTEX_NREF0
29608# define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
\
29609 {PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },id,0,(pthread_t)0,0}
29610#elif defined(SQLITE_ENABLE_API_ARMOR1)
29611# 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 }
29612#else
29613#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 } } } }
29614#endif
29615
29616/*
29617** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
29618** intended for use only inside assert() statements. On some platforms,
29619** there might be race conditions that can cause these routines to
29620** deliver incorrect results. In particular, if pthread_equal() is
29621** not an atomic operation, then these routines might delivery
29622** incorrect results. On most platforms, pthread_equal() is a
29623** comparison of two integers and is therefore atomic. But we are
29624** told that HPUX is not such a platform. If so, then these routines
29625** will not always work correctly on HPUX.
29626**
29627** On those platforms where pthread_equal() is not atomic, SQLite
29628** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
29629** make sure no assert() statements are evaluated and hence these
29630** routines are never called.
29631*/
29632#if !defined(NDEBUG1) || defined(SQLITE_DEBUG)
29633static int pthreadMutexHeld(sqlite3_mutex *p){
29634 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
29635}
29636static int pthreadMutexNotheld(sqlite3_mutex *p){
29637 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
29638}
29639#endif
29640
29641/*
29642** Try to provide a memory barrier operation, needed for initialization
29643** and also for the implementation of xShmBarrier in the VFS in cases
29644** where SQLite is compiled without mutexes.
29645*/
29646SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
29647#if defined(SQLITE_MEMORY_BARRIER)
29648 SQLITE_MEMORY_BARRIER;
29649#elif defined(__GNUC__4) && GCC_VERSION(4*1000000+2*1000+1)>=4001000
29650 __sync_synchronize();
29651#endif
29652}
29653
29654/*
29655** Initialize and deinitialize the mutex subsystem.
29656*/
29657static int pthreadMutexInit(void){ return SQLITE_OK0; }
29658static int pthreadMutexEnd(void){ return SQLITE_OK0; }
29659
29660/*
29661** The sqlite3_mutex_alloc() routine allocates a new
29662** mutex and returns a pointer to it. If it returns NULL
29663** that means that a mutex could not be allocated. SQLite
29664** will unwind its stack and return an error. The argument
29665** to sqlite3_mutex_alloc() is one of these integer constants:
29666**
29667** <ul>
29668** <li> SQLITE_MUTEX_FAST
29669** <li> SQLITE_MUTEX_RECURSIVE
29670** <li> SQLITE_MUTEX_STATIC_MAIN
29671** <li> SQLITE_MUTEX_STATIC_MEM
29672** <li> SQLITE_MUTEX_STATIC_OPEN
29673** <li> SQLITE_MUTEX_STATIC_PRNG
29674** <li> SQLITE_MUTEX_STATIC_LRU
29675** <li> SQLITE_MUTEX_STATIC_PMEM
29676** <li> SQLITE_MUTEX_STATIC_APP1
29677** <li> SQLITE_MUTEX_STATIC_APP2
29678** <li> SQLITE_MUTEX_STATIC_APP3
29679** <li> SQLITE_MUTEX_STATIC_VFS1
29680** <li> SQLITE_MUTEX_STATIC_VFS2
29681** <li> SQLITE_MUTEX_STATIC_VFS3
29682** </ul>
29683**
29684** The first two constants cause sqlite3_mutex_alloc() to create
29685** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
29686** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
29687** The mutex implementation does not need to make a distinction
29688** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
29689** not want to. But SQLite will only request a recursive mutex in
29690** cases where it really needs one. If a faster non-recursive mutex
29691** implementation is available on the host platform, the mutex subsystem
29692** might return such a mutex in response to SQLITE_MUTEX_FAST.
29693**
29694** The other allowed parameters to sqlite3_mutex_alloc() each return
29695** a pointer to a static preexisting mutex. Six static mutexes are
29696** used by the current version of SQLite. Future versions of SQLite
29697** may add additional static mutexes. Static mutexes are for internal
29698** use by SQLite only. Applications that use SQLite mutexes should
29699** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
29700** SQLITE_MUTEX_RECURSIVE.
29701**
29702** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
29703** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
29704** returns a different mutex on every call. But for the static
29705** mutex types, the same mutex is returned on every call that has
29706** the same type number.
29707*/
29708static sqlite3_mutex *pthreadMutexAlloc(int iType){
29709 static sqlite3_mutex staticMutexes[] = {
29710 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
2 }
,
29711 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
3 }
,
29712 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
4 }
,
29713 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
5 }
,
29714 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
6 }
,
29715 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
7 }
,
29716 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
8 }
,
29717 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
9 }
,
29718 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
10 }
,
29719 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
11 }
,
29720 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
12 }
,
29721 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
13 }
29722 };
29723 sqlite3_mutex *p;
29724 switch( iType ){
29725 case SQLITE_MUTEX_RECURSIVE1: {
29726 p = sqlite3MallocZero( sizeof(*p) );
29727 if( p ){
29728#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
29729 /* If recursive mutexes are not available, we will have to
29730 ** build our own. See below. */
29731 pthread_mutex_init(&p->mutex, 0);
29732#else
29733 /* Use a recursive mutex if it is available */
29734 pthread_mutexattr_t recursiveAttr;
29735 pthread_mutexattr_init(&recursiveAttr);
29736 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
29737 pthread_mutex_init(&p->mutex, &recursiveAttr);
29738 pthread_mutexattr_destroy(&recursiveAttr);
29739#endif
29740#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
29741 p->id = SQLITE_MUTEX_RECURSIVE1;
29742#endif
29743 }
29744 break;
29745 }
29746 case SQLITE_MUTEX_FAST0: {
29747 p = sqlite3MallocZero( sizeof(*p) );
29748 if( p ){
29749 pthread_mutex_init(&p->mutex, 0);
29750#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
29751 p->id = SQLITE_MUTEX_FAST0;
29752#endif
29753 }
29754 break;
29755 }
29756 default: {
29757#ifdef SQLITE_ENABLE_API_ARMOR1
29758 if( iType-2<0 || iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
29759 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29759);
29760 return 0;
29761 }
29762#endif
29763 p = &staticMutexes[iType-2];
29764 break;
29765 }
29766 }
29767#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR1)
29768 assert( p==0 || p->id==iType )((void) (0));
29769#endif
29770 return p;
29771}
29772
29773
29774/*
29775** This routine deallocates a previously
29776** allocated mutex. SQLite is careful to deallocate every
29777** mutex that it allocates.
29778*/
29779static void pthreadMutexFree(sqlite3_mutex *p){
29780 assert( p->nRef==0 )((void) (0));
29781#ifdef SQLITE_ENABLE_API_ARMOR1
29782 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 )
29783#endif
29784 {
29785 pthread_mutex_destroy(&p->mutex);
29786 sqlite3_free(p);
29787 }
29788#ifdef SQLITE_ENABLE_API_ARMOR1
29789 else{
29790 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(29790);
29791 }
29792#endif
29793}
29794
29795/*
29796** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
29797** to enter a mutex. If another thread is already within the mutex,
29798** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
29799** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
29800** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
29801** be entered multiple times by the same thread. In such cases the,
29802** mutex must be exited an equal number of times before another thread
29803** can enter. If the same thread tries to enter any other kind of mutex
29804** more than once, the behavior is undefined.
29805*/
29806static void pthreadMutexEnter(sqlite3_mutex *p){
29807 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
29808
29809#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
29810 /* If recursive mutexes are not available, then we have to grow
29811 ** our own. This implementation assumes that pthread_equal()
29812 ** is atomic - that it cannot be deceived into thinking self
29813 ** and p->owner are equal if p->owner changes between two values
29814 ** that are not equal to self while the comparison is taking place.
29815 ** This implementation also assumes a coherent cache - that
29816 ** separate processes cannot read different values from the same
29817 ** address at the same time. If either of these two conditions
29818 ** are not met, then the mutexes will fail and problems will result.
29819 */
29820 {
29821 pthread_t self = pthread_self();
29822 if( p->nRef>0 && pthread_equal(p->owner, self) ){
29823 p->nRef++;
29824 }else{
29825 pthread_mutex_lock(&p->mutex);
29826 assert( p->nRef==0 )((void) (0));
29827 p->owner = self;
29828 p->nRef = 1;
29829 }
29830 }
29831#else
29832 /* Use the built-in recursive mutexes if they are available.
29833 */
29834 pthread_mutex_lock(&p->mutex);
29835#if SQLITE_MUTEX_NREF0
29836 assert( p->nRef>0 || p->owner==0 )((void) (0));
29837 p->owner = pthread_self();
29838 p->nRef++;
29839#endif
29840#endif
29841
29842#ifdef SQLITE_DEBUG
29843 if( p->trace ){
29844 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
29845 }
29846#endif
29847}
29848static int pthreadMutexTry(sqlite3_mutex *p){
29849 int rc;
29850 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
29851
29852#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
29853 /* If recursive mutexes are not available, then we have to grow
29854 ** our own. This implementation assumes that pthread_equal()
29855 ** is atomic - that it cannot be deceived into thinking self
29856 ** and p->owner are equal if p->owner changes between two values
29857 ** that are not equal to self while the comparison is taking place.
29858 ** This implementation also assumes a coherent cache - that
29859 ** separate processes cannot read different values from the same
29860 ** address at the same time. If either of these two conditions
29861 ** are not met, then the mutexes will fail and problems will result.
29862 */
29863 {
29864 pthread_t self = pthread_self();
29865 if( p->nRef>0 && pthread_equal(p->owner, self) ){
29866 p->nRef++;
29867 rc = SQLITE_OK0;
29868 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
29869 assert( p->nRef==0 )((void) (0));
29870 p->owner = self;
29871 p->nRef = 1;
29872 rc = SQLITE_OK0;
29873 }else{
29874 rc = SQLITE_BUSY5;
29875 }
29876 }
29877#else
29878 /* Use the built-in recursive mutexes if they are available.
29879 */
29880 if( pthread_mutex_trylock(&p->mutex)==0 ){
29881#if SQLITE_MUTEX_NREF0
29882 p->owner = pthread_self();
29883 p->nRef++;
29884#endif
29885 rc = SQLITE_OK0;
29886 }else{
29887 rc = SQLITE_BUSY5;
29888 }
29889#endif
29890
29891#ifdef SQLITE_DEBUG
29892 if( rc==SQLITE_OK0 && p->trace ){
29893 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
29894 }
29895#endif
29896 return rc;
29897}
29898
29899/*
29900** The sqlite3_mutex_leave() routine exits a mutex that was
29901** previously entered by the same thread. The behavior
29902** is undefined if the mutex is not currently entered or
29903** is not currently allocated. SQLite will never do either.
29904*/
29905static void pthreadMutexLeave(sqlite3_mutex *p){
29906 assert( pthreadMutexHeld(p) )((void) (0));
29907#if SQLITE_MUTEX_NREF0
29908 p->nRef--;
29909 if( p->nRef==0 ) p->owner = 0;
29910#endif
29911 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
29912
29913#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
29914 if( p->nRef==0 ){
29915 pthread_mutex_unlock(&p->mutex);
29916 }
29917#else
29918 pthread_mutex_unlock(&p->mutex);
29919#endif
29920
29921#ifdef SQLITE_DEBUG
29922 if( p->trace ){
29923 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
29924 }
29925#endif
29926}
29927
29928SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
29929 static const sqlite3_mutex_methods sMutex = {
29930 pthreadMutexInit,
29931 pthreadMutexEnd,
29932 pthreadMutexAlloc,
29933 pthreadMutexFree,
29934 pthreadMutexEnter,
29935 pthreadMutexTry,
29936 pthreadMutexLeave,
29937#ifdef SQLITE_DEBUG
29938 pthreadMutexHeld,
29939 pthreadMutexNotheld
29940#else
29941 0,
29942 0
29943#endif
29944 };
29945
29946 return &sMutex;
29947}
29948
29949#endif /* SQLITE_MUTEX_PTHREADS */
29950
29951/************** End of mutex_unix.c ******************************************/
29952/************** Begin file mutex_w32.c ***************************************/
29953/*
29954** 2007 August 14
29955**
29956** The author disclaims copyright to this source code. In place of
29957** a legal notice, here is a blessing:
29958**
29959** May you do good and not evil.
29960** May you find forgiveness for yourself and forgive others.
29961** May you share freely, never taking more than you give.
29962**
29963*************************************************************************
29964** This file contains the C functions that implement mutexes for Win32.
29965*/
29966/* #include "sqliteInt.h" */
29967
29968#if SQLITE_OS_WIN0
29969/*
29970** Include code that is common to all os_*.c files
29971*/
29972/* #include "os_common.h" */
29973
29974/*
29975** Include the header file for the Windows VFS.
29976*/
29977/************** Include os_win.h in the middle of mutex_w32.c ****************/
29978/************** Begin file os_win.h ******************************************/
29979/*
29980** 2013 November 25
29981**
29982** The author disclaims copyright to this source code. In place of
29983** a legal notice, here is a blessing:
29984**
29985** May you do good and not evil.
29986** May you find forgiveness for yourself and forgive others.
29987** May you share freely, never taking more than you give.
29988**
29989******************************************************************************
29990**
29991** This file contains code that is specific to Windows.
29992*/
29993#ifndef SQLITE_OS_WIN_H
29994#define SQLITE_OS_WIN_H
29995
29996/*
29997** Include the primary Windows SDK header file.
29998*/
29999#include "windows.h"
30000
30001#ifdef __CYGWIN__
30002# include <sys/cygwin.h>
30003# include <errno(*__errno_location ()).h> /* amalgamator: dontcache */
30004#endif
30005
30006/*
30007** Determine if we are dealing with Windows NT.
30008**
30009** We ought to be able to determine if we are compiling for Windows 9x or
30010** Windows NT using the _WIN32_WINNT macro as follows:
30011**
30012** #if defined(_WIN32_WINNT)
30013** # define SQLITE_OS_WINNT 1
30014** #else
30015** # define SQLITE_OS_WINNT 0
30016** #endif
30017**
30018** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
30019** it ought to, so the above test does not work. We'll just assume that
30020** everything is Windows NT unless the programmer explicitly says otherwise
30021** by setting SQLITE_OS_WINNT to 0.
30022*/
30023#if SQLITE_OS_WIN0 && !defined(SQLITE_OS_WINNT)
30024# define SQLITE_OS_WINNT 1
30025#endif
30026
30027/*
30028** Determine if we are dealing with Windows CE - which has a much reduced
30029** API.
30030*/
30031#if defined(_WIN32_WCE)
30032# define SQLITE_OS_WINCE 1
30033#else
30034# define SQLITE_OS_WINCE 0
30035#endif
30036
30037/*
30038** Determine if we are dealing with WinRT, which provides only a subset of
30039** the full Win32 API.
30040*/
30041#if !defined(SQLITE_OS_WINRT)
30042# define SQLITE_OS_WINRT 0
30043#endif
30044
30045/*
30046** For WinCE, some API function parameters do not appear to be declared as
30047** volatile.
30048*/
30049#if SQLITE_OS_WINCE
30050# define SQLITE_WIN32_VOLATILE
30051#else
30052# define SQLITE_WIN32_VOLATILE volatile
30053#endif
30054
30055/*
30056** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
30057** functions are not available (e.g. those not using MSVC, Cygwin, etc).
30058*/
30059#if SQLITE_OS_WIN0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
30060 SQLITE_THREADSAFE2>0 && !defined(__CYGWIN__)
30061# define SQLITE_OS_WIN_THREADS 1
30062#else
30063# define SQLITE_OS_WIN_THREADS 0
30064#endif
30065
30066#endif /* SQLITE_OS_WIN_H */
30067
30068/************** End of os_win.h **********************************************/
30069/************** Continuing where we left off in mutex_w32.c ******************/
30070#endif
30071
30072/*
30073** The code in this file is only used if we are compiling multithreaded
30074** on a Win32 system.
30075*/
30076#ifdef SQLITE_MUTEX_W32
30077
30078/*
30079** Each recursive mutex is an instance of the following structure.
30080*/
30081struct sqlite3_mutex {
30082 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
30083 int id; /* Mutex type */
30084#ifdef SQLITE_DEBUG
30085 volatile int nRef; /* Number of entrances */
30086 volatile DWORD owner; /* Thread holding this mutex */
30087 volatile LONG trace; /* True to trace changes */
30088#endif
30089};
30090
30091/*
30092** These are the initializer values used when declaring a "static" mutex
30093** on Win32. It should be noted that all mutexes require initialization
30094** on the Win32 platform.
30095*/
30096#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
30097
30098#ifdef SQLITE_DEBUG
30099#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ SQLITE_W32_MUTEX_INITIALIZER, id, \
30100 0L, (DWORD)0, 0 }
30101#else
30102#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
id }
{ SQLITE_W32_MUTEX_INITIALIZER, id }
30103#endif
30104
30105#ifdef SQLITE_DEBUG
30106/*
30107** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
30108** intended for use only inside assert() statements.
30109*/
30110static int winMutexHeld(sqlite3_mutex *p){
30111 return p->nRef!=0 && p->owner==GetCurrentThreadId();
30112}
30113
30114static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
30115 return p->nRef==0 || p->owner!=tid;
30116}
30117
30118static int winMutexNotheld(sqlite3_mutex *p){
30119 DWORD tid = GetCurrentThreadId();
30120 return winMutexNotheld2(p, tid);
30121}
30122#endif
30123
30124/*
30125** Try to provide a memory barrier operation, needed for initialization
30126** and also for the xShmBarrier method of the VFS in cases when SQLite is
30127** compiled without mutexes (SQLITE_THREADSAFE=0).
30128*/
30129SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
30130#if defined(SQLITE_MEMORY_BARRIER)
30131 SQLITE_MEMORY_BARRIER;
30132#elif defined(__GNUC__4)
30133 __sync_synchronize();
30134#elif MSVC_VERSION0>=1400
30135 _ReadWriteBarrier();
30136#elif defined(MemoryBarrier)
30137 MemoryBarrier();
30138#endif
30139}
30140
30141/*
30142** Initialize and deinitialize the mutex subsystem.
30143*/
30144static sqlite3_mutex winMutex_staticMutexes[] = {
30145 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
2 }
,
30146 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
3 }
,
30147 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
4 }
,
30148 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
5 }
,
30149 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
6 }
,
30150 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
7 }
,
30151 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
8 }
,
30152 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
9 }
,
30153 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
10 }
,
30154 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
11 }
,
30155 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
12 }
,
30156 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },
13 }
30157};
30158
30159static int winMutex_isInit = 0;
30160static int winMutex_isNt = -1; /* <0 means "need to query" */
30161
30162/* As the winMutexInit() and winMutexEnd() functions are called as part
30163** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
30164** "interlocked" magic used here is probably not strictly necessary.
30165*/
30166static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
30167
30168SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
30169SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
30170
30171static int winMutexInit(void){
30172 /* The first to increment to 1 does actual initialization */
30173 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
30174 int i;
30175 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
30176#if SQLITE_OS_WINRT
30177 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
30178#else
30179 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
30180#endif
30181 }
30182 winMutex_isInit = 1;
30183 }else{
30184 /* Another thread is (in the process of) initializing the static
30185 ** mutexes */
30186 while( !winMutex_isInit ){
30187 sqlite3_win32_sleep(1);
30188 }
30189 }
30190 return SQLITE_OK0;
30191}
30192
30193static int winMutexEnd(void){
30194 /* The first to decrement to 0 does actual shutdown
30195 ** (which should be the last to shutdown.) */
30196 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
30197 if( winMutex_isInit==1 ){
30198 int i;
30199 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
30200 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
30201 }
30202 winMutex_isInit = 0;
30203 }
30204 }
30205 return SQLITE_OK0;
30206}
30207
30208/*
30209** The sqlite3_mutex_alloc() routine allocates a new
30210** mutex and returns a pointer to it. If it returns NULL
30211** that means that a mutex could not be allocated. SQLite
30212** will unwind its stack and return an error. The argument
30213** to sqlite3_mutex_alloc() is one of these integer constants:
30214**
30215** <ul>
30216** <li> SQLITE_MUTEX_FAST
30217** <li> SQLITE_MUTEX_RECURSIVE
30218** <li> SQLITE_MUTEX_STATIC_MAIN
30219** <li> SQLITE_MUTEX_STATIC_MEM
30220** <li> SQLITE_MUTEX_STATIC_OPEN
30221** <li> SQLITE_MUTEX_STATIC_PRNG
30222** <li> SQLITE_MUTEX_STATIC_LRU
30223** <li> SQLITE_MUTEX_STATIC_PMEM
30224** <li> SQLITE_MUTEX_STATIC_APP1
30225** <li> SQLITE_MUTEX_STATIC_APP2
30226** <li> SQLITE_MUTEX_STATIC_APP3
30227** <li> SQLITE_MUTEX_STATIC_VFS1
30228** <li> SQLITE_MUTEX_STATIC_VFS2
30229** <li> SQLITE_MUTEX_STATIC_VFS3
30230** </ul>
30231**
30232** The first two constants cause sqlite3_mutex_alloc() to create
30233** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
30234** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
30235** The mutex implementation does not need to make a distinction
30236** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
30237** not want to. But SQLite will only request a recursive mutex in
30238** cases where it really needs one. If a faster non-recursive mutex
30239** implementation is available on the host platform, the mutex subsystem
30240** might return such a mutex in response to SQLITE_MUTEX_FAST.
30241**
30242** The other allowed parameters to sqlite3_mutex_alloc() each return
30243** a pointer to a static preexisting mutex. Six static mutexes are
30244** used by the current version of SQLite. Future versions of SQLite
30245** may add additional static mutexes. Static mutexes are for internal
30246** use by SQLite only. Applications that use SQLite mutexes should
30247** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
30248** SQLITE_MUTEX_RECURSIVE.
30249**
30250** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
30251** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
30252** returns a different mutex on every call. But for the static
30253** mutex types, the same mutex is returned on every call that has
30254** the same type number.
30255*/
30256static sqlite3_mutex *winMutexAlloc(int iType){
30257 sqlite3_mutex *p;
30258
30259 switch( iType ){
30260 case SQLITE_MUTEX_FAST0:
30261 case SQLITE_MUTEX_RECURSIVE1: {
30262 p = sqlite3MallocZero( sizeof(*p) );
30263 if( p ){
30264 p->id = iType;
30265#ifdef SQLITE_DEBUG
30266#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
30267 p->trace = 1;
30268#endif
30269#endif
30270#if SQLITE_OS_WINRT
30271 InitializeCriticalSectionEx(&p->mutex, 0, 0);
30272#else
30273 InitializeCriticalSection(&p->mutex);
30274#endif
30275 }
30276 break;
30277 }
30278 default: {
30279#ifdef SQLITE_ENABLE_API_ARMOR1
30280 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
){
30281 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30281);
30282 return 0;
30283 }
30284#endif
30285 p = &winMutex_staticMutexes[iType-2];
30286#ifdef SQLITE_DEBUG
30287#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
30288 InterlockedCompareExchange(&p->trace, 1, 0);
30289#endif
30290#endif
30291 break;
30292 }
30293 }
30294 assert( p==0 || p->id==iType )((void) (0));
30295 return p;
30296}
30297
30298
30299/*
30300** This routine deallocates a previously
30301** allocated mutex. SQLite is careful to deallocate every
30302** mutex that it allocates.
30303*/
30304static void winMutexFree(sqlite3_mutex *p){
30305 assert( p )((void) (0));
30306 assert( p->nRef==0 && p->owner==0 )((void) (0));
30307 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 ){
30308 DeleteCriticalSection(&p->mutex);
30309 sqlite3_free(p);
30310 }else{
30311#ifdef SQLITE_ENABLE_API_ARMOR1
30312 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(30312);
30313#endif
30314 }
30315}
30316
30317/*
30318** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
30319** to enter a mutex. If another thread is already within the mutex,
30320** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
30321** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
30322** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
30323** be entered multiple times by the same thread. In such cases the,
30324** mutex must be exited an equal number of times before another thread
30325** can enter. If the same thread tries to enter any other kind of mutex
30326** more than once, the behavior is undefined.
30327*/
30328static void winMutexEnter(sqlite3_mutex *p){
30329#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
30330 DWORD tid = GetCurrentThreadId();
30331#endif
30332#ifdef SQLITE_DEBUG
30333 assert( p )((void) (0));
30334 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
30335#else
30336 assert( p )((void) (0));
30337#endif
30338 assert( winMutex_isInit==1 )((void) (0));
30339 EnterCriticalSection(&p->mutex);
30340#ifdef SQLITE_DEBUG
30341 assert( p->nRef>0 || p->owner==0 )((void) (0));
30342 p->owner = tid;
30343 p->nRef++;
30344 if( p->trace ){
30345 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
30346 tid, p->id, p, p->trace, p->nRef));
30347 }
30348#endif
30349}
30350
30351static int winMutexTry(sqlite3_mutex *p){
30352#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
30353 DWORD tid = GetCurrentThreadId();
30354#endif
30355 int rc = SQLITE_BUSY5;
30356 assert( p )((void) (0));
30357 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
30358 /*
30359 ** The sqlite3_mutex_try() routine is very rarely used, and when it
30360 ** is used it is merely an optimization. So it is OK for it to always
30361 ** fail.
30362 **
30363 ** The TryEnterCriticalSection() interface is only available on WinNT.
30364 ** And some windows compilers complain if you try to use it without
30365 ** first doing some #defines that prevent SQLite from building on Win98.
30366 ** For that reason, we will omit this optimization for now. See
30367 ** ticket #2685.
30368 */
30369#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
30370 assert( winMutex_isInit==1 )((void) (0));
30371 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 )((void) (0));
30372 if( winMutex_isNt<0 ){
30373 winMutex_isNt = sqlite3_win32_is_nt();
30374 }
30375 assert( winMutex_isNt==0 || winMutex_isNt==1 )((void) (0));
30376 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
30377#ifdef SQLITE_DEBUG
30378 p->owner = tid;
30379 p->nRef++;
30380#endif
30381 rc = SQLITE_OK0;
30382 }
30383#else
30384 UNUSED_PARAMETER(p)(void)(p);
30385#endif
30386#ifdef SQLITE_DEBUG
30387 if( p->trace ){
30388 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
30389 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
30390 }
30391#endif
30392 return rc;
30393}
30394
30395/*
30396** The sqlite3_mutex_leave() routine exits a mutex that was
30397** previously entered by the same thread. The behavior
30398** is undefined if the mutex is not currently entered or
30399** is not currently allocated. SQLite will never do either.
30400*/
30401static void winMutexLeave(sqlite3_mutex *p){
30402#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
30403 DWORD tid = GetCurrentThreadId();
30404#endif
30405 assert( p )((void) (0));
30406#ifdef SQLITE_DEBUG
30407 assert( p->nRef>0 )((void) (0));
30408 assert( p->owner==tid )((void) (0));
30409 p->nRef--;
30410 if( p->nRef==0 ) p->owner = 0;
30411 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
30412#endif
30413 assert( winMutex_isInit==1 )((void) (0));
30414 LeaveCriticalSection(&p->mutex);
30415#ifdef SQLITE_DEBUG
30416 if( p->trace ){
30417 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
30418 tid, p->id, p, p->trace, p->nRef));
30419 }
30420#endif
30421}
30422
30423SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
30424 static const sqlite3_mutex_methods sMutex = {
30425 winMutexInit,
30426 winMutexEnd,
30427 winMutexAlloc,
30428 winMutexFree,
30429 winMutexEnter,
30430 winMutexTry,
30431 winMutexLeave,
30432#ifdef SQLITE_DEBUG
30433 winMutexHeld,
30434 winMutexNotheld
30435#else
30436 0,
30437 0
30438#endif
30439 };
30440 return &sMutex;
30441}
30442
30443#endif /* SQLITE_MUTEX_W32 */
30444
30445/************** End of mutex_w32.c *******************************************/
30446/************** Begin file malloc.c ******************************************/
30447/*
30448** 2001 September 15
30449**
30450** The author disclaims copyright to this source code. In place of
30451** a legal notice, here is a blessing:
30452**
30453** May you do good and not evil.
30454** May you find forgiveness for yourself and forgive others.
30455** May you share freely, never taking more than you give.
30456**
30457*************************************************************************
30458**
30459** Memory allocation functions used throughout sqlite.
30460*/
30461/* #include "sqliteInt.h" */
30462/* #include <stdarg.h> */
30463
30464/*
30465** Attempt to release up to n bytes of non-essential memory currently
30466** held by SQLite. An example of non-essential memory is memory used to
30467** cache database pages that are not currently in use.
30468*/
30469SQLITE_API int sqlite3_release_memory(int n){
30470#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
30471 return sqlite3PcacheReleaseMemory(n);
30472#else
30473 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
30474 ** is a no-op returning zero if SQLite is not compiled with
30475 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
30476 UNUSED_PARAMETER(n)(void)(n);
30477 return 0;
30478#endif
30479}
30480
30481/*
30482** Default value of the hard heap limit. 0 means "no limit".
30483*/
30484#ifndef SQLITE_MAX_MEMORY0
30485# define SQLITE_MAX_MEMORY0 0
30486#endif
30487
30488/*
30489** State information local to the memory allocation subsystem.
30490*/
30491static SQLITE_WSD struct Mem0Global {
30492 sqlite3_mutex *mutex; /* Mutex to serialize access */
30493 sqlite3_int64 alarmThreshold; /* The soft heap limit */
30494 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
30495
30496 /*
30497 ** True if heap is nearly "full" where "full" is defined by the
30498 ** sqlite3_soft_heap_limit() setting.
30499 */
30500 int nearlyFull;
30501} mem0mem0 = { 0, SQLITE_MAX_MEMORY0, SQLITE_MAX_MEMORY0, 0 };
30502
30503#define mem0mem0 GLOBAL(struct Mem0Global, mem0)mem0
30504
30505/*
30506** Return the memory allocator mutex. sqlite3_status() needs it.
30507*/
30508SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void){
30509 return mem0mem0.mutex;
30510}
30511
30512#ifndef SQLITE_OMIT_DEPRECATED1
30513/*
30514** Deprecated external interface. It used to set an alarm callback
30515** that was invoked when memory usage grew too large. Now it is a
30516** no-op.
30517*/
30518SQLITE_API int sqlite3_memory_alarm(
30519 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
30520 void *pArg,
30521 sqlite3_int64 iThreshold
30522){
30523 (void)xCallback;
30524 (void)pArg;
30525 (void)iThreshold;
30526 return SQLITE_OK0;
30527}
30528#endif
30529
30530/*
30531** Set the soft heap-size limit for the library. An argument of
30532** zero disables the limit. A negative argument is a no-op used to
30533** obtain the return value.
30534**
30535** The return value is the value of the heap limit just before this
30536** interface was called.
30537**
30538** If the hard heap limit is enabled, then the soft heap limit cannot
30539** be disabled nor raised above the hard heap limit.
30540*/
30541SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
30542 sqlite3_int64 priorLimit;
30543 sqlite3_int64 excess;
30544 sqlite3_int64 nUsed;
30545#ifndef SQLITE_OMIT_AUTOINIT
30546 int rc = sqlite3_initialize();
30547 if( rc ) return -1;
30548#endif
30549 sqlite3_mutex_enter(mem0mem0.mutex);
30550 priorLimit = mem0mem0.alarmThreshold;
30551 if( n<0 ){
30552 sqlite3_mutex_leave(mem0mem0.mutex);
30553 return priorLimit;
30554 }
30555 if( mem0mem0.hardLimit>0 && (n>mem0mem0.hardLimit || n==0) ){
30556 n = mem0mem0.hardLimit;
30557 }
30558 mem0mem0.alarmThreshold = n;
30559 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
30560 AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed)__atomic_store_n((&mem0.nearlyFull),(n>0 && n<=
nUsed),0)
;
30561 sqlite3_mutex_leave(mem0mem0.mutex);
30562 excess = sqlite3_memory_used() - n;
30563 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
30564 return priorLimit;
30565}
30566SQLITE_API void sqlite3_soft_heap_limit(int n){
30567 if( n<0 ) n = 0;
30568 sqlite3_soft_heap_limit64(n);
30569}
30570
30571/*
30572** Set the hard heap-size limit for the library. An argument of zero
30573** disables the hard heap limit. A negative argument is a no-op used
30574** to obtain the return value without affecting the hard heap limit.
30575**
30576** The return value is the value of the hard heap limit just prior to
30577** calling this interface.
30578**
30579** Setting the hard heap limit will also activate the soft heap limit
30580** and constrain the soft heap limit to be no more than the hard heap
30581** limit.
30582*/
30583SQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
30584 sqlite3_int64 priorLimit;
30585#ifndef SQLITE_OMIT_AUTOINIT
30586 int rc = sqlite3_initialize();
30587 if( rc ) return -1;
30588#endif
30589 sqlite3_mutex_enter(mem0mem0.mutex);
30590 priorLimit = mem0mem0.hardLimit;
30591 if( n>=0 ){
30592 mem0mem0.hardLimit = n;
30593 if( n<mem0mem0.alarmThreshold || mem0mem0.alarmThreshold==0 ){
30594 mem0mem0.alarmThreshold = n;
30595 }
30596 }
30597 sqlite3_mutex_leave(mem0mem0.mutex);
30598 return priorLimit;
30599}
30600
30601
30602/*
30603** Initialize the memory allocation subsystem.
30604*/
30605SQLITE_PRIVATEstatic int sqlite3MallocInit(void){
30606 int rc;
30607 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ){
30608 sqlite3MemSetDefault();
30609 }
30610 mem0mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
30611 if( sqlite3GlobalConfigsqlite3Config.pPage==0 || sqlite3GlobalConfigsqlite3Config.szPage<512
30612 || sqlite3GlobalConfigsqlite3Config.nPage<=0 ){
30613 sqlite3GlobalConfigsqlite3Config.pPage = 0;
30614 sqlite3GlobalConfigsqlite3Config.szPage = 0;
30615 }
30616 rc = sqlite3GlobalConfigsqlite3Config.m.xInit(sqlite3GlobalConfigsqlite3Config.m.pAppData);
30617 if( rc!=SQLITE_OK0 ) memset(&mem0mem0, 0, sizeof(mem0mem0));
30618 return rc;
30619}
30620
30621/*
30622** Return true if the heap is currently under memory pressure - in other
30623** words if the amount of heap used is close to the limit set by
30624** sqlite3_soft_heap_limit().
30625*/
30626SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void){
30627 return AtomicLoad(&mem0.nearlyFull)__atomic_load_n((&mem0.nearlyFull),0);
30628}
30629
30630/*
30631** Deinitialize the memory allocation subsystem.
30632*/
30633SQLITE_PRIVATEstatic void sqlite3MallocEnd(void){
30634 if( sqlite3GlobalConfigsqlite3Config.m.xShutdown ){
30635 sqlite3GlobalConfigsqlite3Config.m.xShutdown(sqlite3GlobalConfigsqlite3Config.m.pAppData);
30636 }
30637 memset(&mem0mem0, 0, sizeof(mem0mem0));
30638}
30639
30640/*
30641** Return the amount of memory currently checked out.
30642*/
30643SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
30644 sqlite3_int64 res, mx;
30645 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, 0);
30646 return res;
30647}
30648
30649/*
30650** Return the maximum amount of memory that has ever been
30651** checked out since either the beginning of this process
30652** or since the most recent reset.
30653*/
30654SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
30655 sqlite3_int64 res, mx;
30656 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, resetFlag);
30657 return mx;
30658}
30659
30660/*
30661** Trigger the alarm
30662*/
30663static void sqlite3MallocAlarm(int nByte){
30664 if( mem0mem0.alarmThreshold<=0 ) return;
30665 sqlite3_mutex_leave(mem0mem0.mutex);
30666 sqlite3_release_memory(nByte);
30667 sqlite3_mutex_enter(mem0mem0.mutex);
30668}
30669
30670#ifdef SQLITE_DEBUG
30671/*
30672** This routine is called whenever an out-of-memory condition is seen,
30673** It's only purpose to to serve as a breakpoint for gdb or similar
30674** code debuggers when working on out-of-memory conditions, for example
30675** caused by PRAGMA hard_heap_limit=N.
30676*/
30677static SQLITE_NOINLINE__attribute__((noinline)) void test_oom_breakpoint(u64 n){
30678 static u64 nOomFault = 0;
30679 nOomFault += n;
30680 /* The assert() is never reached in a human lifetime. It is here mostly
30681 ** to prevent code optimizers from optimizing out this function. */
30682 assert( (nOomFault>>32) < 0xffffffff )((void) (0));
30683}
30684#else
30685# define test_oom_breakpoint(X) /* No-op for production builds */
30686#endif
30687
30688/*
30689** Do a memory allocation with statistics and alarms. Assume the
30690** lock is already held.
30691*/
30692static void mallocWithAlarm(int n, void **pp){
30693 void *p;
30694 int nFull;
30695 assert( sqlite3_mutex_held(mem0.mutex) )((void) (0));
30696 assert( n>0 )((void) (0));
30697
30698 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
30699 ** implementation of malloc_good_size(), which must be called in debug
30700 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
30701 ** or else a crash results. Hence, do not attempt to optimize out the
30702 ** following xRoundup() call. */
30703 nFull = sqlite3GlobalConfigsqlite3Config.m.xRoundup(n);
30704
30705 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, n);
30706 if( mem0mem0.alarmThreshold>0 ){
30707 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
30708 if( nUsed >= mem0mem0.alarmThreshold - nFull ){
30709 AtomicStore(&mem0.nearlyFull, 1)__atomic_store_n((&mem0.nearlyFull),(1),0);
30710 sqlite3MallocAlarm(nFull);
30711 if( mem0mem0.hardLimit ){
30712 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
30713 if( nUsed >= mem0mem0.hardLimit - nFull ){
30714 test_oom_breakpoint(1);
30715 *pp = 0;
30716 return;
30717 }
30718 }
30719 }else{
30720 AtomicStore(&mem0.nearlyFull, 0)__atomic_store_n((&mem0.nearlyFull),(0),0);
30721 }
30722 }
30723 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
30724#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
30725 if( p==0 && mem0mem0.alarmThreshold>0 ){
30726 sqlite3MallocAlarm(nFull);
30727 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
30728 }
30729#endif
30730 if( p ){
30731 nFull = sqlite3MallocSize(p);
30732 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nFull);
30733 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT9, 1);
30734 }
30735 *pp = p;
30736}
30737
30738/*
30739** Maximum size of any single memory allocation.
30740**
30741** This is not a limit on the total amount of memory used. This is
30742** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
30743**
30744** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
30745** This provides a 256-byte safety margin for defense against 32-bit
30746** signed integer overflow bugs when computing memory allocation sizes.
30747** Paranoid applications might want to reduce the maximum allocation size
30748** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
30749** or even smaller would be reasonable upper bounds on the size of a memory
30750** allocations for most applications.
30751*/
30752#ifndef SQLITE_MAX_ALLOCATION_SIZE2147483391
30753# define SQLITE_MAX_ALLOCATION_SIZE2147483391 2147483391
30754#endif
30755#if SQLITE_MAX_ALLOCATION_SIZE2147483391>2147483391
30756# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE2147483391 is 2147483391
30757#endif
30758
30759/*
30760** Allocate memory. This routine is like sqlite3_malloc() except that it
30761** assumes the memory subsystem has already been initialized.
30762*/
30763SQLITE_PRIVATEstatic void *sqlite3Malloc(u64 n){
30764 void *p;
30765 if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE2147483391 ){
30766 p = 0;
30767 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
30768 sqlite3_mutex_enter(mem0mem0.mutex);
30769 mallocWithAlarm((int)n, &p);
30770 sqlite3_mutex_leave(mem0mem0.mutex);
30771 }else{
30772 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc((int)n);
30773 }
30774 assert( EIGHT_BYTE_ALIGNMENT(p) )((void) (0)); /* IMP: R-11148-40995 */
30775 return p;
30776}
30777
30778/*
30779** This version of the memory allocation is for use by the application.
30780** First make sure the memory subsystem is initialized, then do the
30781** allocation.
30782*/
30783SQLITE_API void *sqlite3_malloc(int n){
30784#ifndef SQLITE_OMIT_AUTOINIT
30785 if( sqlite3_initialize() ) return 0;
30786#endif
30787 return n<=0 ? 0 : sqlite3Malloc(n);
30788}
30789SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
30790#ifndef SQLITE_OMIT_AUTOINIT
30791 if( sqlite3_initialize() ) return 0;
30792#endif
30793 return sqlite3Malloc(n);
30794}
30795
30796/*
30797** TRUE if p is a lookaside memory allocation from db
30798*/
30799#ifndef SQLITE_OMIT_LOOKASIDE
30800static int isLookaside(sqlite3 *db, const void *p){
30801 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd)(((uptr)(p)>=(uptr)(db->lookaside.pStart))&&((uptr
)(p)<(uptr)(db->lookaside.pTrueEnd)))
;
30802}
30803#else
30804#define isLookaside(A,B) 0
30805#endif
30806
30807/*
30808** Return the size of a memory allocation previously obtained from
30809** sqlite3Malloc() or sqlite3_malloc().
30810*/
30811SQLITE_PRIVATEstatic int sqlite3MallocSize(const void *p){
30812 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
30813 return sqlite3GlobalConfigsqlite3Config.m.xSize((void*)p);
30814}
30815static int lookasideMallocSize(sqlite3 *db, const void *p){
30816#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30817 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL128;
30818#else
30819 return db->lookaside.szTrue;
30820#endif
30821}
30822SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3 *db, const void *p){
30823 assert( p!=0 )((void) (0));
30824#ifdef SQLITE_DEBUG
30825 if( db==0 ){
30826 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
30827 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
30828 }else if( !isLookaside(db,p) ){
30829 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
30830 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
30831 }
30832#endif
30833 if( db ){
30834 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
30835#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30836 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
30837 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
30838 return LOOKASIDE_SMALL128;
30839 }
30840#endif
30841 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
30842 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
30843 return db->lookaside.szTrue;
30844 }
30845 }
30846 }
30847 return sqlite3GlobalConfigsqlite3Config.m.xSize((void*)p);
30848}
30849SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
30850 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
30851 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
30852 return p ? sqlite3GlobalConfigsqlite3Config.m.xSize(p) : 0;
30853}
30854
30855/*
30856** Free memory previously obtained from sqlite3Malloc().
30857*/
30858SQLITE_API void sqlite3_free(void *p){
30859 if( p==0 ) return; /* IMP: R-49053-54554 */
30860 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
30861 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
30862 if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
30863 sqlite3_mutex_enter(mem0mem0.mutex);
30864 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED0, sqlite3MallocSize(p));
30865 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT9, 1);
30866 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
30867 sqlite3_mutex_leave(mem0mem0.mutex);
30868 }else{
30869 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
30870 }
30871}
30872
30873/*
30874** Add the size of memory allocation "p" to the count in
30875** *db->pnBytesFreed.
30876*/
30877static SQLITE_NOINLINE__attribute__((noinline)) void measureAllocationSize(sqlite3 *db, void *p){
30878 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
30879}
30880
30881/*
30882** Free memory that might be associated with a particular database
30883** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
30884** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
30885*/
30886SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3 *db, void *p){
30887 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
30888 assert( p!=0 )((void) (0));
30889 if( db ){
30890 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
30891#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30892 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
30893 LookasideSlot *pBuf = (LookasideSlot*)p;
30894 assert( db->pnBytesFreed==0 )((void) (0));
30895#ifdef SQLITE_DEBUG
30896 memset(p, 0xaa, LOOKASIDE_SMALL128); /* Trash freed content */
30897#endif
30898 pBuf->pNext = db->lookaside.pSmallFree;
30899 db->lookaside.pSmallFree = pBuf;
30900 return;
30901 }
30902#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
30903 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
30904 LookasideSlot *pBuf = (LookasideSlot*)p;
30905 assert( db->pnBytesFreed==0 )((void) (0));
30906#ifdef SQLITE_DEBUG
30907 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
30908#endif
30909 pBuf->pNext = db->lookaside.pFree;
30910 db->lookaside.pFree = pBuf;
30911 return;
30912 }
30913 }
30914 if( db->pnBytesFreed ){
30915 measureAllocationSize(db, p);
30916 return;
30917 }
30918 }
30919 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
30920 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
30921 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) )((void) (0));
30922 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
30923 sqlite3_free(p);
30924}
30925SQLITE_PRIVATEstatic void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
30926 assert( db!=0 )((void) (0));
30927 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
30928 assert( p!=0 )((void) (0));
30929 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
30930#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
30931 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
30932 LookasideSlot *pBuf = (LookasideSlot*)p;
30933 assert( db->pnBytesFreed==0 )((void) (0));
30934#ifdef SQLITE_DEBUG
30935 memset(p, 0xaa, LOOKASIDE_SMALL128); /* Trash freed content */
30936#endif
30937 pBuf->pNext = db->lookaside.pSmallFree;
30938 db->lookaside.pSmallFree = pBuf;
30939 return;
30940 }
30941#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
30942 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
30943 LookasideSlot *pBuf = (LookasideSlot*)p;
30944 assert( db->pnBytesFreed==0 )((void) (0));
30945#ifdef SQLITE_DEBUG
30946 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
30947#endif
30948 pBuf->pNext = db->lookaside.pFree;
30949 db->lookaside.pFree = pBuf;
30950 return;
30951 }
30952 }
30953 if( db->pnBytesFreed ){
30954 measureAllocationSize(db, p);
30955 return;
30956 }
30957 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
30958 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
30959 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
30960 sqlite3_free(p);
30961}
30962SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3 *db, void *p){
30963 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
30964 if( p ) sqlite3DbFreeNN(db, p);
30965}
30966
30967/*
30968** Change the size of an existing memory allocation
30969*/
30970SQLITE_PRIVATEstatic void *sqlite3Realloc(void *pOld, u64 nBytes){
30971 int nOld, nNew, nDiff;
30972 void *pNew;
30973 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) )((void) (0));
30974 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) )((void) (0));
30975 if( pOld==0 ){
30976 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
30977 }
30978 if( nBytes==0 ){
30979 sqlite3_free(pOld); /* IMP: R-26507-47431 */
30980 return 0;
30981 }
30982 if( nBytes>=0x7fffff00 ){
30983 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
30984 return 0;
30985 }
30986 nOld = sqlite3MallocSize(pOld);
30987 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
30988 ** argument to xRealloc is always a value returned by a prior call to
30989 ** xRoundup. */
30990 nNew = sqlite3GlobalConfigsqlite3Config.m.xRoundup((int)nBytes);
30991 if( nOld==nNew ){
30992 pNew = pOld;
30993 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
30994 sqlite3_int64 nUsed;
30995 sqlite3_mutex_enter(mem0mem0.mutex);
30996 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, (int)nBytes);
30997 nDiff = nNew - nOld;
30998 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0)) >=
30999 mem0mem0.alarmThreshold-nDiff ){
31000 sqlite3MallocAlarm(nDiff);
31001 if( mem0mem0.hardLimit>0 && nUsed >= mem0mem0.hardLimit - nDiff ){
31002 sqlite3_mutex_leave(mem0mem0.mutex);
31003 test_oom_breakpoint(1);
31004 return 0;
31005 }
31006 }
31007 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
31008#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
31009 if( pNew==0 && mem0mem0.alarmThreshold>0 ){
31010 sqlite3MallocAlarm((int)nBytes);
31011 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
31012 }
31013#endif
31014 if( pNew ){
31015 nNew = sqlite3MallocSize(pNew);
31016 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nNew-nOld);
31017 }
31018 sqlite3_mutex_leave(mem0mem0.mutex);
31019 }else{
31020 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
31021 }
31022 assert( EIGHT_BYTE_ALIGNMENT(pNew) )((void) (0)); /* IMP: R-11148-40995 */
31023 return pNew;
31024}
31025
31026/*
31027** The public interface to sqlite3Realloc. Make sure that the memory
31028** subsystem is initialized prior to invoking sqliteRealloc.
31029*/
31030SQLITE_API void *sqlite3_realloc(void *pOld, int n){
31031#ifndef SQLITE_OMIT_AUTOINIT
31032 if( sqlite3_initialize() ) return 0;
31033#endif
31034 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
31035 return sqlite3Realloc(pOld, n);
31036}
31037SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
31038#ifndef SQLITE_OMIT_AUTOINIT
31039 if( sqlite3_initialize() ) return 0;
31040#endif
31041 return sqlite3Realloc(pOld, n);
31042}
31043
31044
31045/*
31046** Allocate and zero memory.
31047*/
31048SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64 n){
31049 void *p = sqlite3Malloc(n);
31050 if( p ){
31051 memset(p, 0, (size_t)n);
31052 }
31053 return p;
31054}
31055
31056/*
31057** Allocate and zero memory. If the allocation fails, make
31058** the mallocFailed flag in the connection pointer.
31059*/
31060SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
31061 void *p;
31062 testcase( db==0 );
31063 p = sqlite3DbMallocRaw(db, n);
31064 if( p ) memset(p, 0, (size_t)n);
31065 return p;
31066}
31067
31068
31069/* Finish the work of sqlite3DbMallocRawNN for the unusual and
31070** slower case when the allocation cannot be fulfilled using lookaside.
31071*/
31072static SQLITE_NOINLINE__attribute__((noinline)) void *dbMallocRawFinish(sqlite3 *db, u64 n){
31073 void *p;
31074 assert( db!=0 )((void) (0));
31075 p = sqlite3Malloc(n);
31076 if( !p ) sqlite3OomFault(db);
31077 sqlite3MemdebugSetType(p,
31078 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
31079 return p;
31080}
31081
31082/*
31083** Allocate memory, either lookaside (if possible) or heap.
31084** If the allocation fails, set the mallocFailed flag in
31085** the connection pointer.
31086**
31087** If db!=0 and db->mallocFailed is true (indicating a prior malloc
31088** failure on the same database connection) then always return 0.
31089** Hence for a particular database connection, once malloc starts
31090** failing, it fails consistently until mallocFailed is reset.
31091** This is an important assumption. There are many places in the
31092** code that do things like this:
31093**
31094** int *a = (int*)sqlite3DbMallocRaw(db, 100);
31095** int *b = (int*)sqlite3DbMallocRaw(db, 200);
31096** if( b ) a[10] = 9;
31097**
31098** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
31099** that all prior mallocs (ex: "a") worked too.
31100**
31101** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
31102** not a NULL pointer.
31103*/
31104SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
31105 void *p;
31106 if( db ) return sqlite3DbMallocRawNN(db, n);
31107 p = sqlite3Malloc(n);
31108 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
31109 return p;
31110}
31111SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
31112#ifndef SQLITE_OMIT_LOOKASIDE
31113 LookasideSlot *pBuf;
31114 assert( db!=0 )((void) (0));
31115 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31116 assert( db->pnBytesFreed==0 )((void) (0));
31117 if( n>db->lookaside.sz ){
31118 if( !db->lookaside.bDisable ){
31119 db->lookaside.anStat[1]++;
31120 }else if( db->mallocFailed ){
31121 return 0;
31122 }
31123 return dbMallocRawFinish(db, n);
31124 }
31125#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31126 if( n<=LOOKASIDE_SMALL128 ){
31127 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
31128 db->lookaside.pSmallFree = pBuf->pNext;
31129 db->lookaside.anStat[0]++;
31130 return (void*)pBuf;
31131 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
31132 db->lookaside.pSmallInit = pBuf->pNext;
31133 db->lookaside.anStat[0]++;
31134 return (void*)pBuf;
31135 }
31136 }
31137#endif
31138 if( (pBuf = db->lookaside.pFree)!=0 ){
31139 db->lookaside.pFree = pBuf->pNext;
31140 db->lookaside.anStat[0]++;
31141 return (void*)pBuf;
31142 }else if( (pBuf = db->lookaside.pInit)!=0 ){
31143 db->lookaside.pInit = pBuf->pNext;
31144 db->lookaside.anStat[0]++;
31145 return (void*)pBuf;
31146 }else{
31147 db->lookaside.anStat[2]++;
31148 }
31149#else
31150 assert( db!=0 )((void) (0));
31151 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31152 assert( db->pnBytesFreed==0 )((void) (0));
31153 if( db->mallocFailed ){
31154 return 0;
31155 }
31156#endif
31157 return dbMallocRawFinish(db, n);
31158}
31159
31160/* Forward declaration */
31161static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
31162
31163/*
31164** Resize the block of memory pointed to by p to n bytes. If the
31165** resize fails, set the mallocFailed flag in the connection object.
31166*/
31167SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
31168 assert( db!=0 )((void) (0));
31169 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
31170 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31171 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
31172#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
31173 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
31174 if( n<=LOOKASIDE_SMALL128 ) return p;
31175 }else
31176#endif
31177 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
31178 if( n<=db->lookaside.szTrue ) return p;
31179 }
31180 }
31181 return dbReallocFinish(db, p, n);
31182}
31183static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
31184 void *pNew = 0;
31185 assert( db!=0 )((void) (0));
31186 assert( p!=0 )((void) (0));
31187 if( db->mallocFailed==0 ){
31188 if( isLookaside(db, p) ){
31189 pNew = sqlite3DbMallocRawNN(db, n);
31190 if( pNew ){
31191 memcpy(pNew, p, lookasideMallocSize(db, p));
31192 sqlite3DbFree(db, p);
31193 }
31194 }else{
31195 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31196 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
31197 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
31198 pNew = sqlite3Realloc(p, n);
31199 if( !pNew ){
31200 sqlite3OomFault(db);
31201 }
31202 sqlite3MemdebugSetType(pNew,
31203 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
31204 }
31205 }
31206 return pNew;
31207}
31208
31209/*
31210** Attempt to reallocate p. If the reallocation fails, then free p
31211** and set the mallocFailed flag in the database connection.
31212*/
31213SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
31214 void *pNew;
31215 pNew = sqlite3DbRealloc(db, p, n);
31216 if( !pNew ){
31217 sqlite3DbFree(db, p);
31218 }
31219 return pNew;
31220}
31221
31222/*
31223** Make a copy of a string in memory obtained from sqliteMalloc(). These
31224** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
31225** is because when memory debugging is turned on, these two functions are
31226** called via macros that record the current file and line number in the
31227** ThreadData structure.
31228*/
31229SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3 *db, const char *z){
31230 char *zNew;
31231 size_t n;
31232 if( z==0 ){
31233 return 0;
31234 }
31235 n = strlen(z) + 1;
31236 zNew = sqlite3DbMallocRaw(db, n);
31237 if( zNew ){
31238 memcpy(zNew, z, n);
31239 }
31240 return zNew;
31241}
31242SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
31243 char *zNew;
31244 assert( db!=0 )((void) (0));
31245 assert( z!=0 || n==0 )((void) (0));
31246 assert( (n&0x7fffffff)==n )((void) (0));
31247 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
31248 if( zNew ){
31249 memcpy(zNew, z, (size_t)n);
31250 zNew[n] = 0;
31251 }
31252 return zNew;
31253}
31254
31255/*
31256** The text between zStart and zEnd represents a phrase within a larger
31257** SQL statement. Make a copy of this phrase in space obtained form
31258** sqlite3DbMalloc(). Omit leading and trailing whitespace.
31259*/
31260SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
31261 int n;
31262#ifdef SQLITE_DEBUG
31263 /* Because of the way the parser works, the span is guaranteed to contain
31264 ** at least one non-space character */
31265 for(n=0; sqlite3Isspace(zStart[n])(sqlite3CtypeMap[(unsigned char)(zStart[n])]&0x01); n++){ assert( &zStart[n]<zEnd )((void) (0)); }
31266#endif
31267 while( sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01) ) zStart++;
31268 n = (int)(zEnd - zStart);
31269 while( sqlite3Isspace(zStart[n-1])(sqlite3CtypeMap[(unsigned char)(zStart[n-1])]&0x01) ) n--;
31270 return sqlite3DbStrNDup(db, zStart, n);
31271}
31272
31273/*
31274** Free any prior content in *pz and replace it with a copy of zNew.
31275*/
31276SQLITE_PRIVATEstatic void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
31277 char *z = sqlite3DbStrDup(db, zNew);
31278 sqlite3DbFree(db, *pz);
31279 *pz = z;
31280}
31281
31282/*
31283** Call this routine to record the fact that an OOM (out-of-memory) error
31284** has happened. This routine will set db->mallocFailed, and also
31285** temporarily disable the lookaside memory allocator and interrupt
31286** any running VDBEs.
31287**
31288** Always return a NULL pointer so that this routine can be invoked using
31289**
31290** return sqlite3OomFault(db);
31291**
31292** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
31293** common case where no OOM occurs.
31294*/
31295SQLITE_PRIVATEstatic void *sqlite3OomFault(sqlite3 *db){
31296 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
31297 db->mallocFailed = 1;
31298 if( db->nVdbeExec>0 ){
31299 AtomicStore(&db->u1.isInterrupted, 1)__atomic_store_n((&db->u1.isInterrupted),(1),0);
31300 }
31301 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
31302 if( db->pParse ){
31303 Parse *pParse;
31304 sqlite3ErrorMsg(db->pParse, "out of memory");
31305 db->pParse->rc = SQLITE_NOMEM_BKPT7;
31306 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
31307 pParse->nErr++;
31308 pParse->rc = SQLITE_NOMEM7;
31309 }
31310 }
31311 }
31312 return 0;
31313}
31314
31315/*
31316** This routine reactivates the memory allocator and clears the
31317** db->mallocFailed flag as necessary.
31318**
31319** The memory allocator is not restarted if there are running
31320** VDBEs.
31321*/
31322SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3 *db){
31323 if( db->mallocFailed && db->nVdbeExec==0 ){
31324 db->mallocFailed = 0;
31325 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
31326 assert( db->lookaside.bDisable>0 )((void) (0));
31327 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
31328 }
31329}
31330
31331/*
31332** Take actions at the end of an API call to deal with error codes.
31333*/
31334static SQLITE_NOINLINE__attribute__((noinline)) int apiHandleError(sqlite3 *db, int rc){
31335 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
31336 sqlite3OomClear(db);
31337 sqlite3Error(db, SQLITE_NOMEM7);
31338 return SQLITE_NOMEM_BKPT7;
31339 }
31340 return rc & db->errMask;
31341}
31342
31343/*
31344** This function must be called before exiting any API function (i.e.
31345** returning control to the user) that has called sqlite3_malloc or
31346** sqlite3_realloc.
31347**
31348** The returned value is normally a copy of the second argument to this
31349** function. However, if a malloc() failure has occurred since the previous
31350** invocation SQLITE_NOMEM is returned instead.
31351**
31352** If an OOM as occurred, then the connection error-code (the value
31353** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
31354*/
31355SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3* db, int rc){
31356 /* If the db handle must hold the connection handle mutex here.
31357 ** Otherwise the read (and possible write) of db->mallocFailed
31358 ** is unsafe, as is the call to sqlite3Error().
31359 */
31360 assert( db!=0 )((void) (0));
31361 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
31362 if( db->mallocFailed || rc ){
31363 return apiHandleError(db, rc);
31364 }
31365 return 0;
31366}
31367
31368/************** End of malloc.c **********************************************/
31369/************** Begin file printf.c ******************************************/
31370/*
31371** The "printf" code that follows dates from the 1980's. It is in
31372** the public domain.
31373**
31374**************************************************************************
31375**
31376** This file contains code for a set of "printf"-like routines. These
31377** routines format strings much like the printf() from the standard C
31378** library, though the implementation here has enhancements to support
31379** SQLite.
31380*/
31381/* #include "sqliteInt.h" */
31382
31383/*
31384** Conversion types fall into various categories as defined by the
31385** following enumeration.
31386*/
31387#define etRADIX0 0 /* non-decimal integer types. %x %o */
31388#define etFLOAT1 1 /* Floating point. %f */
31389#define etEXP2 2 /* Exponentional notation. %e and %E */
31390#define etGENERIC3 3 /* Floating or exponential, depending on exponent. %g */
31391#define etSIZE4 4 /* Return number of characters processed so far. %n */
31392#define etSTRING5 5 /* Strings. %s */
31393#define etDYNSTRING6 6 /* Dynamically allocated strings. %z */
31394#define etPERCENT7 7 /* Percent symbol. %% */
31395#define etCHARX8 8 /* Characters. %c */
31396/* The rest are extensions, not normally found in printf() */
31397#define etSQLESCAPE9 9 /* Strings with '\'' doubled. %q */
31398#define etSQLESCAPE210 10 /* Strings with '\'' doubled and enclosed in '',
31399 NULL pointers replaced by SQL NULL. %Q */
31400#define etTOKEN11 11 /* a pointer to a Token structure */
31401#define etSRCITEM12 12 /* a pointer to a SrcItem */
31402#define etPOINTER13 13 /* The %p conversion */
31403#define etSQLESCAPE314 14 /* %w -> Strings with '\"' doubled */
31404#define etORDINAL15 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31405#define etDECIMAL16 16 /* %d or %u, but not %x, %o */
31406
31407#define etINVALID17 17 /* Any unrecognized conversion type */
31408
31409
31410/*
31411** An "etByte" is an 8-bit unsigned value.
31412*/
31413typedef unsigned char etByte;
31414
31415/*
31416** Each builtin conversion character (ex: the 'd' in "%d") is described
31417** by an instance of the following structure
31418*/
31419typedef struct et_info { /* Information about each format field */
31420 char fmttype; /* The format field code letter */
31421 etByte base; /* The base for radix conversion */
31422 etByte flags; /* One or more of FLAG_ constants below */
31423 etByte type; /* Conversion paradigm */
31424 etByte charset; /* Offset into aDigits[] of the digits string */
31425 etByte prefix; /* Offset into aPrefix[] of the prefix string */
31426} et_info;
31427
31428/*
31429** Allowed values for et_info.flags
31430*/
31431#define FLAG_SIGNED1 1 /* True if the value to convert is signed */
31432#define FLAG_STRING4 4 /* Allow infinite precision */
31433
31434
31435/*
31436** The following table is searched linearly, so it is good to put the
31437** most frequently used conversion types first.
31438*/
31439static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
31440static const char aPrefix[] = "-x0\000X0";
31441static const et_info fmtinfo[] = {
31442 { 'd', 10, 1, etDECIMAL16, 0, 0 },
31443 { 's', 0, 4, etSTRING5, 0, 0 },
31444 { 'g', 0, 1, etGENERIC3, 30, 0 },
31445 { 'z', 0, 4, etDYNSTRING6, 0, 0 },
31446 { 'q', 0, 4, etSQLESCAPE9, 0, 0 },
31447 { 'Q', 0, 4, etSQLESCAPE210, 0, 0 },
31448 { 'w', 0, 4, etSQLESCAPE314, 0, 0 },
31449 { 'c', 0, 0, etCHARX8, 0, 0 },
31450 { 'o', 8, 0, etRADIX0, 0, 2 },
31451 { 'u', 10, 0, etDECIMAL16, 0, 0 },
31452 { 'x', 16, 0, etRADIX0, 16, 1 },
31453 { 'X', 16, 0, etRADIX0, 0, 4 },
31454#ifndef SQLITE_OMIT_FLOATING_POINT
31455 { 'f', 0, 1, etFLOAT1, 0, 0 },
31456 { 'e', 0, 1, etEXP2, 30, 0 },
31457 { 'E', 0, 1, etEXP2, 14, 0 },
31458 { 'G', 0, 1, etGENERIC3, 14, 0 },
31459#endif
31460 { 'i', 10, 1, etDECIMAL16, 0, 0 },
31461 { 'n', 0, 0, etSIZE4, 0, 0 },
31462 { '%', 0, 0, etPERCENT7, 0, 0 },
31463 { 'p', 16, 0, etPOINTER13, 0, 1 },
31464
31465 /* All the rest are undocumented and are for internal use only */
31466 { 'T', 0, 0, etTOKEN11, 0, 0 },
31467 { 'S', 0, 0, etSRCITEM12, 0, 0 },
31468 { 'r', 10, 1, etORDINAL15, 0, 0 },
31469};
31470
31471/* Notes:
31472**
31473** %S Takes a pointer to SrcItem. Shows name or database.name
31474** %!S Like %S but prefer the zName over the zAlias
31475*/
31476
31477/*
31478** Set the StrAccum object to an error mode.
31479*/
31480SQLITE_PRIVATEstatic void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
31481 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG )((void) (0));
31482 p->accError = eError;
31483 if( p->mxAlloc ) sqlite3_str_reset(p);
31484 if( eError==SQLITE_TOOBIG18 ) sqlite3ErrorToParser(p->db, eError);
31485}
31486
31487/*
31488** Extra argument values from a PrintfArguments object
31489*/
31490static sqlite3_int64 getIntArg(PrintfArguments *p){
31491 if( p->nArg<=p->nUsed ) return 0;
31492 return sqlite3_value_int64(p->apArg[p->nUsed++]);
31493}
31494static double getDoubleArg(PrintfArguments *p){
31495 if( p->nArg<=p->nUsed ) return 0.0;
31496 return sqlite3_value_double(p->apArg[p->nUsed++]);
31497}
31498static char *getTextArg(PrintfArguments *p){
31499 if( p->nArg<=p->nUsed ) return 0;
31500 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
31501}
31502
31503/*
31504** Allocate memory for a temporary buffer needed for printf rendering.
31505**
31506** If the requested size of the temp buffer is larger than the size
31507** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
31508** Do the size check before the memory allocation to prevent rogue
31509** SQL from requesting large allocations using the precision or width
31510** field of the printf() function.
31511*/
31512static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
31513 char *z;
31514 if( pAccum->accError ) return 0;
31515 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
31516 sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG18);
31517 return 0;
31518 }
31519 z = sqlite3DbMallocRaw(pAccum->db, n);
31520 if( z==0 ){
31521 sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM7);
31522 }
31523 return z;
31524}
31525
31526/*
31527** On machines with a small stack size, you can redefine the
31528** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
31529*/
31530#ifndef SQLITE_PRINT_BUF_SIZE70
31531# define SQLITE_PRINT_BUF_SIZE70 70
31532#endif
31533#define etBUFSIZE70 SQLITE_PRINT_BUF_SIZE70 /* Size of the output buffer */
31534
31535/*
31536** Hard limit on the precision of floating-point conversions.
31537*/
31538#ifndef SQLITE_PRINTF_PRECISION_LIMIT
31539# define SQLITE_FP_PRECISION_LIMIT100000000 100000000
31540#endif
31541
31542/*
31543** Render a string given by "fmt" into the StrAccum object.
31544*/
31545SQLITE_API void sqlite3_str_vappendf(
31546 sqlite3_str *pAccum, /* Accumulate results here */
31547 const char *fmt, /* Format string */
31548 va_list ap /* arguments */
31549){
31550 int c; /* Next character in the format string */
31551 char *bufpt; /* Pointer to the conversion buffer */
31552 int precision; /* Precision of the current field */
31553 int length; /* Length of the field */
31554 int idx; /* A general purpose loop counter */
31555 int width; /* Width of the current field */
31556 etByte flag_leftjustify; /* True if "-" flag is present */
31557 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
31558 etByte flag_alternateform; /* True if "#" flag is present */
31559 etByte flag_altform2; /* True if "!" flag is present */
31560 etByte flag_zeropad; /* True if field width constant starts with zero */
31561 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
31562 etByte done; /* Loop termination flag */
31563 etByte cThousand; /* Thousands separator for %d and %u */
31564 etByte xtype = etINVALID17; /* Conversion paradigm */
31565 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
31566 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
31567 sqlite_uint64 longvalue; /* Value for integer types */
31568 double realvalue; /* Value for real types */
31569 const et_info *infop; /* Pointer to the appropriate info structure */
31570 char *zOut; /* Rendering buffer */
31571 int nOut; /* Size of the rendering buffer */
31572 char *zExtra = 0; /* Malloced memory used by some conversion */
31573 int exp, e2; /* exponent of real numbers */
31574 etByte flag_dp; /* True if decimal point should be shown */
31575 etByte flag_rtz; /* True if trailing zeros should be removed */
31576
31577 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
31578 char buf[etBUFSIZE70]; /* Conversion buffer */
31579
31580 /* pAccum never starts out with an empty buffer that was obtained from
31581 ** malloc(). This precondition is required by the mprintf("%z...")
31582 ** optimization. */
31583 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
31584
31585 bufpt = 0;
31586 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC0x02)!=0 ){
31587 pArgList = va_arg(ap, PrintfArguments*)__builtin_va_arg(ap, PrintfArguments*);
31588 bArgList = 1;
31589 }else{
31590 bArgList = 0;
31591 }
31592 for(; (c=(*fmt))!=0; ++fmt){
31593 if( c!='%' ){
31594 bufpt = (char *)fmt;
31595#if HAVE_STRCHRNUL
31596 fmt = strchrnul(fmt, '%');
31597#else
31598 do{ fmt++; }while( *fmt && *fmt != '%' );
31599#endif
31600 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
31601 if( *fmt==0 ) break;
31602 }
31603 if( (c=(*++fmt))==0 ){
31604 sqlite3_str_append(pAccum, "%", 1);
31605 break;
31606 }
31607 /* Find out what flags are present */
31608 flag_leftjustify = flag_prefix = cThousand =
31609 flag_alternateform = flag_altform2 = flag_zeropad = 0;
31610 done = 0;
31611 width = 0;
31612 flag_long = 0;
31613 precision = -1;
31614 do{
31615 switch( c ){
31616 case '-': flag_leftjustify = 1; break;
31617 case '+': flag_prefix = '+'; break;
31618 case ' ': flag_prefix = ' '; break;
31619 case '#': flag_alternateform = 1; break;
31620 case '!': flag_altform2 = 1; break;
31621 case '0': flag_zeropad = 1; break;
31622 case ',': cThousand = ','; break;
31623 default: done = 1; break;
31624 case 'l': {
31625 flag_long = 1;
31626 c = *++fmt;
31627 if( c=='l' ){
31628 c = *++fmt;
31629 flag_long = 2;
31630 }
31631 done = 1;
31632 break;
31633 }
31634 case '1': case '2': case '3': case '4': case '5':
31635 case '6': case '7': case '8': case '9': {
31636 unsigned wx = c - '0';
31637 while( (c = *++fmt)>='0' && c<='9' ){
31638 wx = wx*10 + c - '0';
31639 }
31640 testcase( wx>0x7fffffff );
31641 width = wx & 0x7fffffff;
31642#ifdef SQLITE_PRINTF_PRECISION_LIMIT
31643 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
31644 width = SQLITE_PRINTF_PRECISION_LIMIT;
31645 }
31646#endif
31647 if( c!='.' && c!='l' ){
31648 done = 1;
31649 }else{
31650 fmt--;
31651 }
31652 break;
31653 }
31654 case '*': {
31655 if( bArgList ){
31656 width = (int)getIntArg(pArgList);
31657 }else{
31658 width = va_arg(ap,int)__builtin_va_arg(ap, int);
31659 }
31660 if( width<0 ){
31661 flag_leftjustify = 1;
31662 width = width >= -2147483647 ? -width : 0;
31663 }
31664#ifdef SQLITE_PRINTF_PRECISION_LIMIT
31665 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
31666 width = SQLITE_PRINTF_PRECISION_LIMIT;
31667 }
31668#endif
31669 if( (c = fmt[1])!='.' && c!='l' ){
31670 c = *++fmt;
31671 done = 1;
31672 }
31673 break;
31674 }
31675 case '.': {
31676 c = *++fmt;
31677 if( c=='*' ){
31678 if( bArgList ){
31679 precision = (int)getIntArg(pArgList);
31680 }else{
31681 precision = va_arg(ap,int)__builtin_va_arg(ap, int);
31682 }
31683 if( precision<0 ){
31684 precision = precision >= -2147483647 ? -precision : -1;
31685 }
31686 c = *++fmt;
31687 }else{
31688 unsigned px = 0;
31689 while( c>='0' && c<='9' ){
31690 px = px*10 + c - '0';
31691 c = *++fmt;
31692 }
31693 testcase( px>0x7fffffff );
31694 precision = px & 0x7fffffff;
31695 }
31696#ifdef SQLITE_PRINTF_PRECISION_LIMIT
31697 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
31698 precision = SQLITE_PRINTF_PRECISION_LIMIT;
31699 }
31700#endif
31701 if( c=='l' ){
31702 --fmt;
31703 }else{
31704 done = 1;
31705 }
31706 break;
31707 }
31708 }
31709 }while( !done && (c=(*++fmt))!=0 );
31710
31711 /* Fetch the info entry for the field */
31712 infop = &fmtinfo[0];
31713 xtype = etINVALID17;
31714 for(idx=0; idx<ArraySize(fmtinfo)((int)(sizeof(fmtinfo)/sizeof(fmtinfo[0]))); idx++){
31715 if( c==fmtinfo[idx].fmttype ){
31716 infop = &fmtinfo[idx];
31717 xtype = infop->type;
31718 break;
31719 }
31720 }
31721
31722 /*
31723 ** At this point, variables are initialized as follows:
31724 **
31725 ** flag_alternateform TRUE if a '#' is present.
31726 ** flag_altform2 TRUE if a '!' is present.
31727 ** flag_prefix '+' or ' ' or zero
31728 ** flag_leftjustify TRUE if a '-' is present or if the
31729 ** field width was negative.
31730 ** flag_zeropad TRUE if the width began with 0.
31731 ** flag_long 1 for "l", 2 for "ll"
31732 ** width The specified field width. This is
31733 ** always non-negative. Zero is the default.
31734 ** precision The specified precision. The default
31735 ** is -1.
31736 ** xtype The class of the conversion.
31737 ** infop Pointer to the appropriate info struct.
31738 */
31739 assert( width>=0 )((void) (0));
31740 assert( precision>=(-1) )((void) (0));
31741 switch( xtype ){
31742 case etPOINTER13:
31743 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
31744 sizeof(char*)==sizeof(long int) ? 1 : 0;
31745 /* no break */ deliberate_fall_through__attribute__((fallthrough));
31746 case etORDINAL15:
31747 case etRADIX0:
31748 cThousand = 0;
31749 /* no break */ deliberate_fall_through__attribute__((fallthrough));
31750 case etDECIMAL16:
31751 if( infop->flags & FLAG_SIGNED1 ){
31752 i64 v;
31753 if( bArgList ){
31754 v = getIntArg(pArgList);
31755 }else if( flag_long ){
31756 if( flag_long==2 ){
31757 v = va_arg(ap,i64)__builtin_va_arg(ap, i64) ;
31758 }else{
31759 v = va_arg(ap,long int)__builtin_va_arg(ap, long int);
31760 }
31761 }else{
31762 v = va_arg(ap,int)__builtin_va_arg(ap, int);
31763 }
31764 if( v<0 ){
31765 testcase( v==SMALLEST_INT64 );
31766 testcase( v==(-1) );
31767 longvalue = ~v;
31768 longvalue++;
31769 prefix = '-';
31770 }else{
31771 longvalue = v;
31772 prefix = flag_prefix;
31773 }
31774 }else{
31775 if( bArgList ){
31776 longvalue = (u64)getIntArg(pArgList);
31777 }else if( flag_long ){
31778 if( flag_long==2 ){
31779 longvalue = va_arg(ap,u64)__builtin_va_arg(ap, u64);
31780 }else{
31781 longvalue = va_arg(ap,unsigned long int)__builtin_va_arg(ap, unsigned long int);
31782 }
31783 }else{
31784 longvalue = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
31785 }
31786 prefix = 0;
31787 }
31788 if( longvalue==0 ) flag_alternateform = 0;
31789 if( flag_zeropad && precision<width-(prefix!=0) ){
31790 precision = width-(prefix!=0);
31791 }
31792 if( precision<etBUFSIZE70-10-etBUFSIZE70/3 ){
31793 nOut = etBUFSIZE70;
31794 zOut = buf;
31795 }else{
31796 u64 n;
31797 n = (u64)precision + 10;
31798 if( cThousand ) n += precision/3;
31799 zOut = zExtra = printfTempBuf(pAccum, n);
31800 if( zOut==0 ) return;
31801 nOut = (int)n;
31802 }
31803 bufpt = &zOut[nOut-1];
31804 if( xtype==etORDINAL15 ){
31805 static const char zOrd[] = "thstndrd";
31806 int x = (int)(longvalue % 10);
31807 if( x>=4 || (longvalue/10)%10==1 ){
31808 x = 0;
31809 }
31810 *(--bufpt) = zOrd[x*2+1];
31811 *(--bufpt) = zOrd[x*2];
31812 }
31813 {
31814 const char *cset = &aDigits[infop->charset];
31815 u8 base = infop->base;
31816 do{ /* Convert to ascii */
31817 *(--bufpt) = cset[longvalue%base];
31818 longvalue = longvalue/base;
31819 }while( longvalue>0 );
31820 }
31821 length = (int)(&zOut[nOut-1]-bufpt);
31822 while( precision>length ){
31823 *(--bufpt) = '0'; /* Zero pad */
31824 length++;
31825 }
31826 if( cThousand ){
31827 int nn = (length - 1)/3; /* Number of "," to insert */
31828 int ix = (length - 1)%3 + 1;
31829 bufpt -= nn;
31830 for(idx=0; nn>0; idx++){
31831 bufpt[idx] = bufpt[idx+nn];
31832 ix--;
31833 if( ix==0 ){
31834 bufpt[++idx] = cThousand;
31835 nn--;
31836 ix = 3;
31837 }
31838 }
31839 }
31840 if( prefix ) *(--bufpt) = prefix; /* Add sign */
31841 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
31842 const char *pre;
31843 char x;
31844 pre = &aPrefix[infop->prefix];
31845 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
31846 }
31847 length = (int)(&zOut[nOut-1]-bufpt);
31848 break;
31849 case etFLOAT1:
31850 case etEXP2:
31851 case etGENERIC3: {
31852 FpDecode s;
31853 int iRound;
31854 int j;
31855
31856 if( bArgList ){
31857 realvalue = getDoubleArg(pArgList);
31858 }else{
31859 realvalue = va_arg(ap,double)__builtin_va_arg(ap, double);
31860 }
31861 if( precision<0 ) precision = 6; /* Set default precision */
31862#ifdef SQLITE_FP_PRECISION_LIMIT100000000
31863 if( precision>SQLITE_FP_PRECISION_LIMIT100000000 ){
31864 precision = SQLITE_FP_PRECISION_LIMIT100000000;
31865 }
31866#endif
31867 if( xtype==etFLOAT1 ){
31868 iRound = -precision;
31869 }else if( xtype==etGENERIC3 ){
31870 if( precision==0 ) precision = 1;
31871 iRound = precision;
31872 }else{
31873 iRound = precision+1;
31874 }
31875 sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16);
31876 if( s.isSpecial ){
31877 if( s.isSpecial==2 ){
31878 bufpt = flag_zeropad ? "null" : "NaN";
31879 length = sqlite3Strlen30(bufpt);
31880 break;
31881 }else if( flag_zeropad ){
31882 s.z[0] = '9';
31883 s.iDP = 1000;
31884 s.n = 1;
31885 }else{
31886 memcpy(buf, "-Inf", 5);
31887 bufpt = buf;
31888 if( s.sign=='-' ){
31889 /* no-op */
31890 }else if( flag_prefix ){
31891 buf[0] = flag_prefix;
31892 }else{
31893 bufpt++;
31894 }
31895 length = sqlite3Strlen30(bufpt);
31896 break;
31897 }
31898 }
31899 if( s.sign=='-' ){
31900 prefix = '-';
31901 }else{
31902 prefix = flag_prefix;
31903 }
31904
31905 exp = s.iDP-1;
31906
31907 /*
31908 ** If the field type is etGENERIC, then convert to either etEXP
31909 ** or etFLOAT, as appropriate.
31910 */
31911 if( xtype==etGENERIC3 ){
31912 assert( precision>0 )((void) (0));
31913 precision--;
31914 flag_rtz = !flag_alternateform;
31915 if( exp<-4 || exp>precision ){
31916 xtype = etEXP2;
31917 }else{
31918 precision = precision - exp;
31919 xtype = etFLOAT1;
31920 }
31921 }else{
31922 flag_rtz = flag_altform2;
31923 }
31924 if( xtype==etEXP2 ){
31925 e2 = 0;
31926 }else{
31927 e2 = s.iDP - 1;
31928 }
31929 bufpt = buf;
31930 {
31931 i64 szBufNeeded; /* Size of a temporary buffer needed */
31932 szBufNeeded = MAX(e2,0)((e2)>(0)?(e2):(0))+(i64)precision+(i64)width+15;
31933 if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
31934 if( szBufNeeded > etBUFSIZE70 ){
31935 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
31936 if( bufpt==0 ) return;
31937 }
31938 }
31939 zOut = bufpt;
31940 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
31941 /* The sign in front of the number */
31942 if( prefix ){
31943 *(bufpt++) = prefix;
31944 }
31945 /* Digits prior to the decimal point */
31946 j = 0;
31947 if( e2<0 ){
31948 *(bufpt++) = '0';
31949 }else{
31950 for(; e2>=0; e2--){
31951 *(bufpt++) = j<s.n ? s.z[j++] : '0';
31952 if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
31953 }
31954 }
31955 /* The decimal point */
31956 if( flag_dp ){
31957 *(bufpt++) = '.';
31958 }
31959 /* "0" digits after the decimal point but before the first
31960 ** significant digit of the number */
31961 for(e2++; e2<0 && precision>0; precision--, e2++){
31962 *(bufpt++) = '0';
31963 }
31964 /* Significant digits after the decimal point */
31965 while( (precision--)>0 ){
31966 *(bufpt++) = j<s.n ? s.z[j++] : '0';
31967 }
31968 /* Remove trailing zeros and the "." if no digits follow the "." */
31969 if( flag_rtz && flag_dp ){
31970 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
31971 assert( bufpt>zOut )((void) (0));
31972 if( bufpt[-1]=='.' ){
31973 if( flag_altform2 ){
31974 *(bufpt++) = '0';
31975 }else{
31976 *(--bufpt) = 0;
31977 }
31978 }
31979 }
31980 /* Add the "eNNN" suffix */
31981 if( xtype==etEXP2 ){
31982 exp = s.iDP - 1;
31983 *(bufpt++) = aDigits[infop->charset];
31984 if( exp<0 ){
31985 *(bufpt++) = '-'; exp = -exp;
31986 }else{
31987 *(bufpt++) = '+';
31988 }
31989 if( exp>=100 ){
31990 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
31991 exp %= 100;
31992 }
31993 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
31994 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
31995 }
31996 *bufpt = 0;
31997
31998 /* The converted number is in buf[] and zero terminated. Output it.
31999 ** Note that the number is in the usual order, not reversed as with
32000 ** integer conversions. */
32001 length = (int)(bufpt-zOut);
32002 bufpt = zOut;
32003
32004 /* Special case: Add leading zeros if the flag_zeropad flag is
32005 ** set and we are not left justified */
32006 if( flag_zeropad && !flag_leftjustify && length < width){
32007 int i;
32008 int nPad = width - length;
32009 for(i=width; i>=nPad; i--){
32010 bufpt[i] = bufpt[i-nPad];
32011 }
32012 i = prefix!=0;
32013 while( nPad-- ) bufpt[i++] = '0';
32014 length = width;
32015 }
32016 break;
32017 }
32018 case etSIZE4:
32019 if( !bArgList ){
32020 *(va_arg(ap,int*)__builtin_va_arg(ap, int*)) = pAccum->nChar;
32021 }
32022 length = width = 0;
32023 break;
32024 case etPERCENT7:
32025 buf[0] = '%';
32026 bufpt = buf;
32027 length = 1;
32028 break;
32029 case etCHARX8:
32030 if( bArgList ){
32031 bufpt = getTextArg(pArgList);
32032 length = 1;
32033 if( bufpt ){
32034 buf[0] = c = *(bufpt++);
32035 if( (c&0xc0)==0xc0 ){
32036 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
32037 buf[length++] = *(bufpt++);
32038 }
32039 }
32040 }else{
32041 buf[0] = 0;
32042 }
32043 }else{
32044 unsigned int ch = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
32045 if( ch<0x00080 ){
32046 buf[0] = ch & 0xff;
32047 length = 1;
32048 }else if( ch<0x00800 ){
32049 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
32050 buf[1] = 0x80 + (u8)(ch & 0x3f);
32051 length = 2;
32052 }else if( ch<0x10000 ){
32053 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
32054 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
32055 buf[2] = 0x80 + (u8)(ch & 0x3f);
32056 length = 3;
32057 }else{
32058 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
32059 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
32060 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
32061 buf[3] = 0x80 + (u8)(ch & 0x3f);
32062 length = 4;
32063 }
32064 }
32065 if( precision>1 ){
32066 i64 nPrior = 1;
32067 width -= precision-1;
32068 if( width>1 && !flag_leftjustify ){
32069 sqlite3_str_appendchar(pAccum, width-1, ' ');
32070 width = 0;
32071 }
32072 sqlite3_str_append(pAccum, buf, length);
32073 precision--;
32074 while( precision > 1 ){
32075 i64 nCopyBytes;
32076 if( nPrior > precision-1 ) nPrior = precision - 1;
32077 nCopyBytes = length*nPrior;
32078 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){
32079 sqlite3StrAccumEnlarge(pAccum, nCopyBytes);
32080 }
32081 if( pAccum->accError ) break;
32082 sqlite3_str_append(pAccum,
32083 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes);
32084 precision -= nPrior;
32085 nPrior *= 2;
32086 }
32087 }
32088 bufpt = buf;
32089 flag_altform2 = 1;
32090 goto adjust_width_for_utf8;
32091 case etSTRING5:
32092 case etDYNSTRING6:
32093 if( bArgList ){
32094 bufpt = getTextArg(pArgList);
32095 xtype = etSTRING5;
32096 }else{
32097 bufpt = va_arg(ap,char*)__builtin_va_arg(ap, char*);
32098 }
32099 if( bufpt==0 ){
32100 bufpt = "";
32101 }else if( xtype==etDYNSTRING6 ){
32102 if( pAccum->nChar==0
32103 && pAccum->mxAlloc
32104 && width==0
32105 && precision<0
32106 && pAccum->accError==0
32107 ){
32108 /* Special optimization for sqlite3_mprintf("%z..."):
32109 ** Extend an existing memory allocation rather than creating
32110 ** a new one. */
32111 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
32112 pAccum->zText = bufpt;
32113 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
32114 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
32115 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
32116 length = 0;
32117 break;
32118 }
32119 zExtra = bufpt;
32120 }
32121 if( precision>=0 ){
32122 if( flag_altform2 ){
32123 /* Set length to the number of bytes needed in order to display
32124 ** precision characters */
32125 unsigned char *z = (unsigned char*)bufpt;
32126 while( precision-- > 0 && z[0] ){
32127 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
32128 }
32129 length = (int)(z - (unsigned char*)bufpt);
32130 }else{
32131 for(length=0; length<precision && bufpt[length]; length++){}
32132 }
32133 }else{
32134 length = 0x7fffffff & (int)strlen(bufpt);
32135 }
32136 adjust_width_for_utf8:
32137 if( flag_altform2 && width>0 ){
32138 /* Adjust width to account for extra bytes in UTF-8 characters */
32139 int ii = length - 1;
32140 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
32141 }
32142 break;
32143 case etSQLESCAPE9: /* %q: Escape ' characters */
32144 case etSQLESCAPE210: /* %Q: Escape ' and enclose in '...' */
32145 case etSQLESCAPE314: { /* %w: Escape " characters */
32146 i64 i, j, k, n;
32147 int needQuote, isnull;
32148 char ch;
32149 char q = ((xtype==etSQLESCAPE314)?'"':'\''); /* Quote character */
32150 char *escarg;
32151
32152 if( bArgList ){
32153 escarg = getTextArg(pArgList);
32154 }else{
32155 escarg = va_arg(ap,char*)__builtin_va_arg(ap, char*);
32156 }
32157 isnull = escarg==0;
32158 if( isnull ) escarg = (xtype==etSQLESCAPE210 ? "NULL" : "(NULL)");
32159 /* For %q, %Q, and %w, the precision is the number of bytes (or
32160 ** characters if the ! flags is present) to use from the input.
32161 ** Because of the extra quoting characters inserted, the number
32162 ** of output characters may be larger than the precision.
32163 */
32164 k = precision;
32165 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
32166 if( ch==q ) n++;
32167 if( flag_altform2 && (ch&0xc0)==0xc0 ){
32168 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
32169 }
32170 }
32171 needQuote = !isnull && xtype==etSQLESCAPE210;
32172 n += i + 3;
32173 if( n>etBUFSIZE70 ){
32174 bufpt = zExtra = printfTempBuf(pAccum, n);
32175 if( bufpt==0 ) return;
32176 }else{
32177 bufpt = buf;
32178 }
32179 j = 0;
32180 if( needQuote ) bufpt[j++] = q;
32181 k = i;
32182 for(i=0; i<k; i++){
32183 bufpt[j++] = ch = escarg[i];
32184 if( ch==q ) bufpt[j++] = ch;
32185 }
32186 if( needQuote ) bufpt[j++] = q;
32187 bufpt[j] = 0;
32188 length = j;
32189 goto adjust_width_for_utf8;
32190 }
32191 case etTOKEN11: {
32192 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
32193 if( flag_alternateform ){
32194 /* %#T means an Expr pointer that uses Expr.u.zToken */
32195 Expr *pExpr = va_arg(ap,Expr*)__builtin_va_arg(ap, Expr*);
32196 if( ALWAYS(pExpr)(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue))(!(((pExpr)->flags&(0x000800))!=0)) ){
32197 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
32198 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
32199 }
32200 }else{
32201 /* %T means a Token pointer */
32202 Token *pToken = va_arg(ap, Token*)__builtin_va_arg(ap, Token*);
32203 assert( bArgList==0 )((void) (0));
32204 if( pToken && pToken->n ){
32205 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
32206 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
32207 }
32208 }
32209 length = width = 0;
32210 break;
32211 }
32212 case etSRCITEM12: {
32213 SrcItem *pItem;
32214 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
32215 pItem = va_arg(ap, SrcItem*)__builtin_va_arg(ap, SrcItem*);
32216 assert( bArgList==0 )((void) (0));
32217 if( pItem->zAlias && !flag_altform2 ){
32218 sqlite3_str_appendall(pAccum, pItem->zAlias);
32219 }else if( pItem->zName ){
32220 if( pItem->fg.fixedSchema==0
32221 && pItem->fg.isSubquery==0
32222 && pItem->u4.zDatabase!=0
32223 ){
32224 sqlite3_str_appendall(pAccum, pItem->u4.zDatabase);
32225 sqlite3_str_append(pAccum, ".", 1);
32226 }
32227 sqlite3_str_appendall(pAccum, pItem->zName);
32228 }else if( pItem->zAlias ){
32229 sqlite3_str_appendall(pAccum, pItem->zAlias);
32230 }else if( ALWAYS(pItem->fg.isSubquery)(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */
32231 Select *pSel = pItem->u4.pSubq->pSelect;
32232 assert( pSel!=0 )((void) (0));
32233 if( pSel->selFlags & SF_NestedFrom0x0000800 ){
32234 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
32235 }else if( pSel->selFlags & SF_MultiValue0x0000400 ){
32236 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy )((void) (0));
32237 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE",
32238 pItem->u1.nRow);
32239 }else{
32240 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
32241 }
32242 }
32243 length = width = 0;
32244 break;
32245 }
32246 default: {
32247 assert( xtype==etINVALID )((void) (0));
32248 return;
32249 }
32250 }/* End switch over the format type */
32251 /*
32252 ** The text of the conversion is pointed to by "bufpt" and is
32253 ** "length" characters long. The field width is "width". Do
32254 ** the output. Both length and width are in bytes, not characters,
32255 ** at this point. If the "!" flag was present on string conversions
32256 ** indicating that width and precision should be expressed in characters,
32257 ** then the values have been translated prior to reaching this point.
32258 */
32259 width -= length;
32260 if( width>0 ){
32261 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
32262 sqlite3_str_append(pAccum, bufpt, length);
32263 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
32264 }else{
32265 sqlite3_str_append(pAccum, bufpt, length);
32266 }
32267
32268 if( zExtra ){
32269 sqlite3DbFree(pAccum->db, zExtra);
32270 zExtra = 0;
32271 }
32272 }/* End for loop over the format string */
32273} /* End of function */
32274
32275
32276/*
32277** The z string points to the first character of a token that is
32278** associated with an error. If db does not already have an error
32279** byte offset recorded, try to compute the error byte offset for
32280** z and set the error byte offset in db.
32281*/
32282SQLITE_PRIVATEstatic void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
32283 const Parse *pParse;
32284 const char *zText;
32285 const char *zEnd;
32286 assert( z!=0 )((void) (0));
32287 if( NEVER(db==0)(db==0) ) return;
32288 if( db->errByteOffset!=(-2) ) return;
32289 pParse = db->pParse;
32290 if( NEVER(pParse==0)(pParse==0) ) return;
32291 zText =pParse->zTail;
32292 if( NEVER(zText==0)(zText==0) ) return;
32293 zEnd = &zText[strlen(zText)];
32294 if( SQLITE_WITHIN(z,zText,zEnd)(((uptr)(z)>=(uptr)(zText))&&((uptr)(z)<(uptr)(
zEnd)))
){
32295 db->errByteOffset = (int)(z-zText);
32296 }
32297}
32298
32299/*
32300** If pExpr has a byte offset for the start of a token, record that as
32301** as the error offset.
32302*/
32303SQLITE_PRIVATEstatic void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){
32304 while( pExpr
32305 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) || pExpr->w.iOfst<=0)
32306 ){
32307 pExpr = pExpr->pLeft;
32308 }
32309 if( pExpr==0 ) return;
32310 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(0x40000000))!=0) ) return;
32311 db->errByteOffset = pExpr->w.iOfst;
32312}
32313
32314/*
32315** Enlarge the memory allocation on a StrAccum object so that it is
32316** able to accept at least N more bytes of text.
32317**
32318** Return the number of bytes of text that StrAccum is able to accept
32319** after the attempted enlargement. The value returned might be zero.
32320*/
32321SQLITE_PRIVATEstatic int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){
32322 char *zNew;
32323 assert( p->nChar+N >= p->nAlloc )((void) (0)); /* Only called if really needed */
32324 if( p->accError ){
32325 testcase(p->accError==SQLITE_TOOBIG);
32326 testcase(p->accError==SQLITE_NOMEM);
32327 return 0;
32328 }
32329 if( p->mxAlloc==0 ){
32330 sqlite3StrAccumSetError(p, SQLITE_TOOBIG18);
32331 return p->nAlloc - p->nChar - 1;
32332 }else{
32333 char *zOld = isMalloced(p)(((p)->printfFlags & 0x04)!=0) ? p->zText : 0;
32334 i64 szNew = p->nChar + N + 1;
32335 if( szNew+p->nChar<=p->mxAlloc ){
32336 /* Force exponential buffer size growth as long as it does not overflow,
32337 ** to avoid having to call this routine too often */
32338 szNew += p->nChar;
32339 }
32340 if( szNew > p->mxAlloc ){
32341 sqlite3_str_reset(p);
32342 sqlite3StrAccumSetError(p, SQLITE_TOOBIG18);
32343 return 0;
32344 }else{
32345 p->nAlloc = (int)szNew;
32346 }
32347 if( p->db ){
32348 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
32349 }else{
32350 zNew = sqlite3Realloc(zOld, p->nAlloc);
32351 }
32352 if( zNew ){
32353 assert( p->zText!=0 || p->nChar==0 )((void) (0));
32354 if( !isMalloced(p)(((p)->printfFlags & 0x04)!=0) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
32355 p->zText = zNew;
32356 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
32357 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
32358 }else{
32359 sqlite3_str_reset(p);
32360 sqlite3StrAccumSetError(p, SQLITE_NOMEM7);
32361 return 0;
32362 }
32363 }
32364 assert( N>=0 && N<=0x7fffffff )((void) (0));
32365 return (int)N;
32366}
32367
32368/*
32369** Append N copies of character c to the given string buffer.
32370*/
32371SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
32372 testcase( p->nChar + (i64)N > 0x7fffffff );
32373 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
32374 return;
32375 }
32376 while( (N--)>0 ) p->zText[p->nChar++] = c;
32377}
32378
32379/*
32380** The StrAccum "p" is not large enough to accept N new bytes of z[].
32381** So enlarge if first, then do the append.
32382**
32383** This is a helper routine to sqlite3_str_append() that does special-case
32384** work (enlarging the buffer) using tail recursion, so that the
32385** sqlite3_str_append() routine can use fast calling semantics.
32386*/
32387static void SQLITE_NOINLINE__attribute__((noinline)) enlargeAndAppend(StrAccum *p, const char *z, int N){
32388 N = sqlite3StrAccumEnlarge(p, N);
32389 if( N>0 ){
32390 memcpy(&p->zText[p->nChar], z, N);
32391 p->nChar += N;
32392 }
32393}
32394
32395/*
32396** Append N bytes of text from z to the StrAccum object. Increase the
32397** size of the memory allocation for StrAccum if necessary.
32398*/
32399SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
32400 assert( z!=0 || N==0 )((void) (0));
32401 assert( p->zText!=0 || p->nChar==0 || p->accError )((void) (0));
32402 assert( N>=0 )((void) (0));
32403 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 )((void) (0));
32404 if( p->nChar+N >= p->nAlloc ){
32405 enlargeAndAppend(p,z,N);
32406 }else if( N ){
32407 assert( p->zText )((void) (0));
32408 p->nChar += N;
32409 memcpy(&p->zText[p->nChar-N], z, N);
32410 }
32411}
32412
32413/*
32414** Append the complete text of zero-terminated string z[] to the p string.
32415*/
32416SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
32417 sqlite3_str_append(p, z, sqlite3Strlen30(z));
32418}
32419
32420
32421/*
32422** Finish off a string by making sure it is zero-terminated.
32423** Return a pointer to the resulting string. Return a NULL
32424** pointer if any kind of error was encountered.
32425*/
32426static SQLITE_NOINLINE__attribute__((noinline)) char *strAccumFinishRealloc(StrAccum *p){
32427 char *zText;
32428 assert( p->mxAlloc>0 && !isMalloced(p) )((void) (0));
32429 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
32430 if( zText ){
32431 memcpy(zText, p->zText, p->nChar+1);
32432 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
32433 }else{
32434 sqlite3StrAccumSetError(p, SQLITE_NOMEM7);
32435 }
32436 p->zText = zText;
32437 return zText;
32438}
32439SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum *p){
32440 if( p->zText ){
32441 p->zText[p->nChar] = 0;
32442 if( p->mxAlloc>0 && !isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
32443 return strAccumFinishRealloc(p);
32444 }
32445 }
32446 return p->zText;
32447}
32448
32449/*
32450** Use the content of the StrAccum passed as the second argument
32451** as the result of an SQL function.
32452*/
32453SQLITE_PRIVATEstatic void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){
32454 if( p->accError ){
32455 sqlite3_result_error_code(pCtx, p->accError);
32456 sqlite3_str_reset(p);
32457 }else if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
32458 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
32459 }else{
32460 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
32461 sqlite3_str_reset(p);
32462 }
32463}
32464
32465/*
32466** This singleton is an sqlite3_str object that is returned if
32467** sqlite3_malloc() fails to provide space for a real one. This
32468** sqlite3_str object accepts no new text and always returns
32469** an SQLITE_NOMEM error.
32470*/
32471static sqlite3_str sqlite3OomStr = {
32472 0, 0, 0, 0, 0, SQLITE_NOMEM7, 0
32473};
32474
32475/* Finalize a string created using sqlite3_str_new().
32476*/
32477SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
32478 char *z;
32479 if( p!=0 && p!=&sqlite3OomStr ){
32480 z = sqlite3StrAccumFinish(p);
32481 sqlite3_free(p);
32482 }else{
32483 z = 0;
32484 }
32485 return z;
32486}
32487
32488/* Return any error code associated with p */
32489SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
32490 return p ? p->accError : SQLITE_NOMEM7;
32491}
32492
32493/* Return the current length of p in bytes */
32494SQLITE_API int sqlite3_str_length(sqlite3_str *p){
32495 return p ? p->nChar : 0;
32496}
32497
32498/* Return the current value for p */
32499SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
32500 if( p==0 || p->nChar==0 ) return 0;
32501 p->zText[p->nChar] = 0;
32502 return p->zText;
32503}
32504
32505/*
32506** Reset an StrAccum string. Reclaim all malloced memory.
32507*/
32508SQLITE_API void sqlite3_str_reset(StrAccum *p){
32509 if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
32510 sqlite3DbFree(p->db, p->zText);
32511 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED0x04;
32512 }
32513 p->nAlloc = 0;
32514 p->nChar = 0;
32515 p->zText = 0;
32516}
32517
32518/*
32519** Initialize a string accumulator.
32520**
32521** p: The accumulator to be initialized.
32522** db: Pointer to a database connection. May be NULL. Lookaside
32523** memory is used if not NULL. db->mallocFailed is set appropriately
32524** when not NULL.
32525** zBase: An initial buffer. May be NULL in which case the initial buffer
32526** is malloced.
32527** n: Size of zBase in bytes. If total space requirements never exceed
32528** n then no memory allocations ever occur.
32529** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
32530** allocations will ever occur.
32531*/
32532SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
32533 p->zText = zBase;
32534 p->db = db;
32535 p->nAlloc = n;
32536 p->mxAlloc = mx;
32537 p->nChar = 0;
32538 p->accError = 0;
32539 p->printfFlags = 0;
32540}
32541
32542/* Allocate and initialize a new dynamic string object */
32543SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
32544 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
32545 if( p ){
32546 sqlite3StrAccumInit(p, 0, 0, 0,
32547 db ? db->aLimit[SQLITE_LIMIT_LENGTH0] : SQLITE_MAX_LENGTH2147483645);
32548 }else{
32549 p = &sqlite3OomStr;
32550 }
32551 return p;
32552}
32553
32554/*
32555** Print into memory obtained from sqliteMalloc(). Use the internal
32556** %-conversion extensions.
32557*/
32558SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
32559 char *z;
32560 char zBase[SQLITE_PRINT_BUF_SIZE70];
32561 StrAccum acc;
32562 assert( db!=0 )((void) (0));
32563 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
32564 db->aLimit[SQLITE_LIMIT_LENGTH0]);
32565 acc.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
32566 sqlite3_str_vappendf(&acc, zFormat, ap);
32567 z = sqlite3StrAccumFinish(&acc);
32568 if( acc.accError==SQLITE_NOMEM7 ){
32569 sqlite3OomFault(db);
32570 }
32571 return z;
32572}
32573
32574/*
32575** Print into memory obtained from sqliteMalloc(). Use the internal
32576** %-conversion extensions.
32577*/
32578SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
32579 va_list ap;
32580 char *z;
32581 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
32582 z = sqlite3VMPrintf(db, zFormat, ap);
32583 va_end(ap)__builtin_va_end(ap);
32584 return z;
32585}
32586
32587/*
32588** Print into memory obtained from sqlite3_malloc(). Omit the internal
32589** %-conversion extensions.
32590*/
32591SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
32592 char *z;
32593 char zBase[SQLITE_PRINT_BUF_SIZE70];
32594 StrAccum acc;
32595
32596#ifdef SQLITE_ENABLE_API_ARMOR1
32597 if( zFormat==0 ){
32598 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(32598);
32599 return 0;
32600 }
32601#endif
32602#ifndef SQLITE_OMIT_AUTOINIT
32603 if( sqlite3_initialize() ) return 0;
32604#endif
32605 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH2147483645);
32606 sqlite3_str_vappendf(&acc, zFormat, ap);
32607 z = sqlite3StrAccumFinish(&acc);
32608 return z;
32609}
32610
32611/*
32612** Print into memory obtained from sqlite3_malloc()(). Omit the internal
32613** %-conversion extensions.
32614*/
32615SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
32616 va_list ap;
32617 char *z;
32618#ifndef SQLITE_OMIT_AUTOINIT
32619 if( sqlite3_initialize() ) return 0;
32620#endif
32621 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
32622 z = sqlite3_vmprintf(zFormat, ap);
32623 va_end(ap)__builtin_va_end(ap);
32624 return z;
32625}
32626
32627/*
32628** sqlite3_snprintf() works like snprintf() except that it ignores the
32629** current locale settings. This is important for SQLite because we
32630** are not able to use a "," as the decimal point in place of "." as
32631** specified by some locales.
32632**
32633** Oops: The first two arguments of sqlite3_snprintf() are backwards
32634** from the snprintf() standard. Unfortunately, it is too late to change
32635** this without breaking compatibility, so we just have to live with the
32636** mistake.
32637**
32638** sqlite3_vsnprintf() is the varargs version.
32639*/
32640SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
32641 StrAccum acc;
32642 if( n<=0 ) return zBuf;
32643#ifdef SQLITE_ENABLE_API_ARMOR1
32644 if( zBuf==0 || zFormat==0 ) {
32645 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(32645);
32646 if( zBuf ) zBuf[0] = 0;
32647 return zBuf;
32648 }
32649#endif
32650 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
32651 sqlite3_str_vappendf(&acc, zFormat, ap);
32652 zBuf[acc.nChar] = 0;
32653 return zBuf;
32654}
32655SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
32656 StrAccum acc;
32657 va_list ap;
32658 if( n<=0 ) return zBuf;
32659#ifdef SQLITE_ENABLE_API_ARMOR1
32660 if( zBuf==0 || zFormat==0 ) {
32661 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(32661);
32662 if( zBuf ) zBuf[0] = 0;
32663 return zBuf;
32664 }
32665#endif
32666 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
32667 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
32668 sqlite3_str_vappendf(&acc, zFormat, ap);
32669 va_end(ap)__builtin_va_end(ap);
32670 zBuf[acc.nChar] = 0;
32671 return zBuf;
32672}
32673
32674/*
32675** This is the routine that actually formats the sqlite3_log() message.
32676** We house it in a separate routine from sqlite3_log() to avoid using
32677** stack space on small-stack systems when logging is disabled.
32678**
32679** sqlite3_log() must render into a static buffer. It cannot dynamically
32680** allocate memory because it might be called while the memory allocator
32681** mutex is held.
32682**
32683** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
32684** certain format characters (%q) or for very large precisions or widths.
32685** Care must be taken that any sqlite3_log() calls that occur while the
32686** memory mutex is held do not use these mechanisms.
32687*/
32688static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
32689 StrAccum acc; /* String accumulator */
32690 char zMsg[SQLITE_PRINT_BUF_SIZE70*3]; /* Complete log message */
32691
32692 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
32693 sqlite3_str_vappendf(&acc, zFormat, ap);
32694 sqlite3GlobalConfigsqlite3Config.xLog(sqlite3GlobalConfigsqlite3Config.pLogArg, iErrCode,
32695 sqlite3StrAccumFinish(&acc));
32696}
32697
32698/*
32699** Format and write a message to the log if logging is enabled.
32700*/
32701SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
32702 va_list ap; /* Vararg list */
32703 if( sqlite3GlobalConfigsqlite3Config.xLog ){
32704 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
32705 renderLogMsg(iErrCode, zFormat, ap);
32706 va_end(ap)__builtin_va_end(ap);
32707 }
32708}
32709
32710#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
32711/*
32712** A version of printf() that understands %lld. Used for debugging.
32713** The printf() built into some versions of windows does not understand %lld
32714** and segfaults if you give it a long long int.
32715*/
32716SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char *zFormat, ...){
32717 va_list ap;
32718 StrAccum acc;
32719 char zBuf[SQLITE_PRINT_BUF_SIZE70*10];
32720 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
32721 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
32722 sqlite3_str_vappendf(&acc, zFormat, ap);
32723 va_end(ap)__builtin_va_end(ap);
32724 sqlite3StrAccumFinish(&acc);
32725#ifdef SQLITE_OS_TRACE_PROC
32726 {
32727 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
32728 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
32729 }
32730#else
32731 fprintf(stdoutstdout,"%s", zBuf);
32732 fflush(stdoutstdout);
32733#endif
32734}
32735#endif
32736
32737
32738/*
32739** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
32740** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
32741*/
32742SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
32743 va_list ap;
32744 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
32745 sqlite3_str_vappendf(p, zFormat, ap);
32746 va_end(ap)__builtin_va_end(ap);
32747}
32748
32749
32750/*****************************************************************************
32751** Reference counted string/blob storage
32752*****************************************************************************/
32753
32754/*
32755** Increase the reference count of the string by one.
32756**
32757** The input parameter is returned.
32758*/
32759SQLITE_PRIVATEstatic char *sqlite3RCStrRef(char *z){
32760 RCStr *p = (RCStr*)z;
32761 assert( p!=0 )((void) (0));
32762 p--;
32763 p->nRCRef++;
32764 return z;
32765}
32766
32767/*
32768** Decrease the reference count by one. Free the string when the
32769** reference count reaches zero.
32770*/
32771SQLITE_PRIVATEstatic void sqlite3RCStrUnref(void *z){
32772 RCStr *p = (RCStr*)z;
32773 assert( p!=0 )((void) (0));
32774 p--;
32775 assert( p->nRCRef>0 )((void) (0));
32776 if( p->nRCRef>=2 ){
32777 p->nRCRef--;
32778 }else{
32779 sqlite3_free(p);
32780 }
32781}
32782
32783/*
32784** Create a new string that is capable of holding N bytes of text, not counting
32785** the zero byte at the end. The string is uninitialized.
32786**
32787** The reference count is initially 1. Call sqlite3RCStrUnref() to free the
32788** newly allocated string.
32789**
32790** This routine returns 0 on an OOM.
32791*/
32792SQLITE_PRIVATEstatic char *sqlite3RCStrNew(u64 N){
32793 RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 );
32794 if( p==0 ) return 0;
32795 p->nRCRef = 1;
32796 return (char*)&p[1];
32797}
32798
32799/*
32800** Change the size of the string so that it is able to hold N bytes.
32801** The string might be reallocated, so return the new allocation.
32802*/
32803SQLITE_PRIVATEstatic char *sqlite3RCStrResize(char *z, u64 N){
32804 RCStr *p = (RCStr*)z;
32805 RCStr *pNew;
32806 assert( p!=0 )((void) (0));
32807 p--;
32808 assert( p->nRCRef==1 )((void) (0));
32809 pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1);
32810 if( pNew==0 ){
32811 sqlite3_free(p);
32812 return 0;
32813 }else{
32814 return (char*)&pNew[1];
32815 }
32816}
32817
32818/************** End of printf.c **********************************************/
32819/************** Begin file treeview.c ****************************************/
32820/*
32821** 2015-06-08
32822**
32823** The author disclaims copyright to this source code. In place of
32824** a legal notice, here is a blessing:
32825**
32826** May you do good and not evil.
32827** May you find forgiveness for yourself and forgive others.
32828** May you share freely, never taking more than you give.
32829**
32830*************************************************************************
32831**
32832** This file contains C code to implement the TreeView debugging routines.
32833** These routines print a parse tree to standard output for debugging and
32834** analysis.
32835**
32836** The interfaces in this file is only available when compiling
32837** with SQLITE_DEBUG.
32838*/
32839/* #include "sqliteInt.h" */
32840#ifdef SQLITE_DEBUG
32841
32842/*
32843** Add a new subitem to the tree. The moreToFollow flag indicates that this
32844** is not the last item in the tree.
32845*/
32846static void sqlite3TreeViewPush(TreeView **pp, u8 moreToFollow){
32847 TreeView *p = *pp;
32848 if( p==0 ){
32849 *pp = p = sqlite3_malloc64( sizeof(*p) );
32850 if( p==0 ) return;
32851 memset(p, 0, sizeof(*p));
32852 }else{
32853 p->iLevel++;
32854 }
32855 assert( moreToFollow==0 || moreToFollow==1 )((void) (0));
32856 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
32857}
32858
32859/*
32860** Finished with one layer of the tree
32861*/
32862static void sqlite3TreeViewPop(TreeView **pp){
32863 TreeView *p = *pp;
32864 if( p==0 ) return;
32865 p->iLevel--;
32866 if( p->iLevel<0 ){
32867 sqlite3_free(p);
32868 *pp = 0;
32869 }
32870}
32871
32872/*
32873** Generate a single line of output for the tree, with a prefix that contains
32874** all the appropriate tree lines
32875*/
32876SQLITE_PRIVATEstatic void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
32877 va_list ap;
32878 int i;
32879 StrAccum acc;
32880 char zBuf[1000];
32881 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
32882 if( p ){
32883 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
32884 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
32885 }
32886 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
32887 }
32888 if( zFormat!=0 ){
32889 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
32890 sqlite3_str_vappendf(&acc, zFormat, ap);
32891 va_end(ap)__builtin_va_end(ap);
32892 assert( acc.nChar>0 || acc.accError )((void) (0));
32893 sqlite3_str_append(&acc, "\n", 1);
32894 }
32895 sqlite3StrAccumFinish(&acc);
32896 fprintf(stdoutstdout,"%s", zBuf);
32897 fflush(stdoutstdout);
32898}
32899
32900/*
32901** Shorthand for starting a new tree item that consists of a single label
32902*/
32903static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
32904 sqlite3TreeViewPush(&p, moreFollows);
32905 sqlite3TreeViewLine(p, "%s", zLabel);
32906}
32907
32908/*
32909** Show a list of Column objects in tree format.
32910*/
32911SQLITE_PRIVATEstatic void sqlite3TreeViewColumnList(
32912 TreeView *pView,
32913 const Column *aCol,
32914 int nCol,
32915 u8 moreToFollow
32916){
32917 int i;
32918 sqlite3TreeViewPush(&pView, moreToFollow);
32919 sqlite3TreeViewLine(pView, "COLUMNS");
32920 for(i=0; i<nCol; i++){
32921 u16 flg = aCol[i].colFlags;
32922 int colMoreToFollow = i<(nCol - 1);
32923 sqlite3TreeViewPush(&pView, colMoreToFollow);
32924 sqlite3TreeViewLine(pView, 0);
32925 printf(" %s", aCol[i].zCnName);
32926 switch( aCol[i].eCType ){
32927 case COLTYPE_ANY1: printf(" ANY"); break;
32928 case COLTYPE_BLOB2: printf(" BLOB"); break;
32929 case COLTYPE_INT3: printf(" INT"); break;
32930 case COLTYPE_INTEGER4: printf(" INTEGER"); break;
32931 case COLTYPE_REAL5: printf(" REAL"); break;
32932 case COLTYPE_TEXT6: printf(" TEXT"); break;
32933 case COLTYPE_CUSTOM0: {
32934 if( flg & COLFLAG_HASTYPE0x0004 ){
32935 const char *z = aCol[i].zCnName;
32936 z += strlen(z)+1;
32937 printf(" X-%s", z);
32938 break;
32939 }
32940 }
32941 }
32942 if( flg & COLFLAG_PRIMKEY0x0001 ) printf(" PRIMARY KEY");
32943 if( flg & COLFLAG_HIDDEN0x0002 ) printf(" HIDDEN");
32944#ifdef COLFLAG_NOEXPAND0x0400
32945 if( flg & COLFLAG_NOEXPAND0x0400 ) printf(" NO-EXPAND");
32946#endif
32947 if( flg ) printf(" flags=%04x", flg);
32948 printf("\n");
32949 fflush(stdoutstdout);
32950 sqlite3TreeViewPop(&pView);
32951 }
32952 sqlite3TreeViewPop(&pView);
32953}
32954
32955/*
32956** Generate a human-readable description of a WITH clause.
32957*/
32958SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
32959 int i;
32960 if( pWith==0 ) return;
32961 if( pWith->nCte==0 ) return;
32962 if( pWith->pOuter ){
32963 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
32964 }else{
32965 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
32966 }
32967 if( pWith->nCte>0 ){
32968 sqlite3TreeViewPush(&pView, moreToFollow);
32969 for(i=0; i<pWith->nCte; i++){
32970 StrAccum x;
32971 char zLine[1000];
32972 const struct Cte *pCte = &pWith->a[i];
32973 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
32974 sqlite3_str_appendf(&x, "%s", pCte->zName);
32975 if( pCte->pCols && pCte->pCols->nExpr>0 ){
32976 char cSep = '(';
32977 int j;
32978 for(j=0; j<pCte->pCols->nExpr; j++){
32979 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
32980 cSep = ',';
32981 }
32982 sqlite3_str_appendf(&x, ")");
32983 }
32984 if( pCte->eM10d!=M10d_Any1 ){
32985 sqlite3_str_appendf(&x, " %sMATERIALIZED",
32986 pCte->eM10d==M10d_No2 ? "NOT " : "");
32987 }
32988 if( pCte->pUse ){
32989 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
32990 pCte->pUse->nUse);
32991 }
32992 sqlite3StrAccumFinish(&x);
32993 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
32994 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
32995 sqlite3TreeViewPop(&pView);
32996 }
32997 sqlite3TreeViewPop(&pView);
32998 }
32999}
33000
33001/*
33002** Generate a human-readable description of a SrcList object.
33003*/
33004SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
33005 int i;
33006 if( pSrc==0 ) return;
33007 for(i=0; i<pSrc->nSrc; i++){
33008 const SrcItem *pItem = &pSrc->a[i];
33009 StrAccum x;
33010 int n = 0;
33011 char zLine[1000];
33012 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
33013 x.printfFlags |= SQLITE_PRINTF_INTERNAL0x01;
33014 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
33015 if( pItem->pSTab ){
33016 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx%s",
33017 pItem->pSTab->zName, pItem->pSTab->nCol, pItem->pSTab,
33018 pItem->colUsed,
33019 pItem->fg.rowidUsed ? "+rowid" : "");
33020 }
33021 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))==(JT_LEFT0x08|JT_RIGHT0x10) ){
33022 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
33023 }else if( pItem->fg.jointype & JT_LEFT0x08 ){
33024 sqlite3_str_appendf(&x, " LEFT-JOIN");
33025 }else if( pItem->fg.jointype & JT_RIGHT0x10 ){
33026 sqlite3_str_appendf(&x, " RIGHT-JOIN");
33027 }else if( pItem->fg.jointype & JT_CROSS0x02 ){
33028 sqlite3_str_appendf(&x, " CROSS-JOIN");
33029 }
33030 if( pItem->fg.jointype & JT_LTORJ0x40 ){
33031 sqlite3_str_appendf(&x, " LTORJ");
33032 }
33033 if( pItem->fg.fromDDL ){
33034 sqlite3_str_appendf(&x, " DDL");
33035 }
33036 if( pItem->fg.isCte ){
33037 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
33038 }
33039 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
33040 sqlite3_str_appendf(&x, " ON");
33041 }
33042 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc");
33043 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated");
33044 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized");
33045 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine");
33046 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte");
33047 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom");
33048 if( pItem->fg.fixedSchema ) sqlite3_str_appendf(&x, " fixedSchema");
33049 if( pItem->fg.hadSchema ) sqlite3_str_appendf(&x, " hadSchema");
33050 if( pItem->fg.isSubquery ) sqlite3_str_appendf(&x, " isSubquery");
33051
33052 sqlite3StrAccumFinish(&x);
33053 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
33054 n = 0;
33055 if( pItem->fg.isSubquery ) n++;
33056 if( pItem->fg.isTabFunc ) n++;
33057 if( pItem->fg.isUsing ) n++;
33058 if( pItem->fg.isUsing ){
33059 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
33060 }
33061 if( pItem->fg.isSubquery ){
33062 assert( n==1 )((void) (0));
33063 if( pItem->pSTab ){
33064 Table *pTab = pItem->pSTab;
33065 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
33066 }
33067 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) )((void) (0));
33068 sqlite3TreeViewPush(&pView, 0);
33069 sqlite3TreeViewLine(pView, "SUBQUERY");
33070 sqlite3TreeViewPop(&pView);
33071 sqlite3TreeViewSelect(pView, pItem->u4.pSubq->pSelect, 0);
33072 }
33073 if( pItem->fg.isTabFunc ){
33074 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
33075 }
33076 sqlite3TreeViewPop(&pView);
33077 }
33078}
33079
33080/*
33081** Generate a human-readable description of a Select object.
33082*/
33083SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
33084 int n = 0;
33085 int cnt = 0;
33086 if( p==0 ){
33087 sqlite3TreeViewLine(pView, "nil-SELECT");
33088 return;
33089 }
33090 sqlite3TreeViewPush(&pView, moreToFollow);
33091 if( p->pWith ){
33092 sqlite3TreeViewWith(pView, p->pWith, 1);
33093 cnt = 1;
33094 sqlite3TreeViewPush(&pView, 1);
33095 }
33096 do{
33097 if( p->selFlags & SF_WhereBegin0x0080000 ){
33098 sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
33099 }else{
33100 sqlite3TreeViewLine(pView,
33101 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
33102 ((p->selFlags & SF_Distinct0x0000001) ? " DISTINCT" : ""),
33103 ((p->selFlags & SF_Aggregate0x0000008) ? " agg_flag" : ""),
33104 p->selId, p, p->selFlags,
33105 (int)p->nSelectRow
33106 );
33107 }
33108 if( cnt++ ) sqlite3TreeViewPop(&pView);
33109 if( p->pPrior ){
33110 n = 1000;
33111 }else{
33112 n = 0;
33113 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ) n++;
33114 if( p->pWhere ) n++;
33115 if( p->pGroupBy ) n++;
33116 if( p->pHaving ) n++;
33117 if( p->pOrderBy ) n++;
33118 if( p->pLimit ) n++;
33119#ifndef SQLITE_OMIT_WINDOWFUNC
33120 if( p->pWin ) n++;
33121 if( p->pWinDefn ) n++;
33122#endif
33123 }
33124 if( p->pEList ){
33125 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
33126 }
33127 n--;
33128#ifndef SQLITE_OMIT_WINDOWFUNC
33129 if( p->pWin ){
33130 Window *pX;
33131 sqlite3TreeViewPush(&pView, (n--)>0);
33132 sqlite3TreeViewLine(pView, "window-functions");
33133 for(pX=p->pWin; pX; pX=pX->pNextWin){
33134 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
33135 }
33136 sqlite3TreeViewPop(&pView);
33137 }
33138#endif
33139 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ){
33140 sqlite3TreeViewPush(&pView, (n--)>0);
33141 sqlite3TreeViewLine(pView, "FROM");
33142 sqlite3TreeViewSrcList(pView, p->pSrc);
33143 sqlite3TreeViewPop(&pView);
33144 }
33145 if( p->pWhere ){
33146 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
33147 sqlite3TreeViewExpr(pView, p->pWhere, 0);
33148 sqlite3TreeViewPop(&pView);
33149 }
33150 if( p->pGroupBy ){
33151 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
33152 }
33153 if( p->pHaving ){
33154 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
33155 sqlite3TreeViewExpr(pView, p->pHaving, 0);
33156 sqlite3TreeViewPop(&pView);
33157 }
33158#ifndef SQLITE_OMIT_WINDOWFUNC
33159 if( p->pWinDefn ){
33160 Window *pX;
33161 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
33162 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
33163 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
33164 }
33165 sqlite3TreeViewPop(&pView);
33166 }
33167#endif
33168 if( p->pOrderBy ){
33169 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
33170 }
33171 if( p->pLimit ){
33172 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
33173 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
33174 if( p->pLimit->pRight ){
33175 sqlite3TreeViewItem(pView, "OFFSET", 0);
33176 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
33177 sqlite3TreeViewPop(&pView);
33178 }
33179 sqlite3TreeViewPop(&pView);
33180 }
33181 if( p->pPrior ){
33182 const char *zOp = "UNION";
33183 switch( p->op ){
33184 case TK_ALL136: zOp = "UNION ALL"; break;
33185 case TK_INTERSECT138: zOp = "INTERSECT"; break;
33186 case TK_EXCEPT137: zOp = "EXCEPT"; break;
33187 }
33188 sqlite3TreeViewItem(pView, zOp, 1);
33189 }
33190 p = p->pPrior;
33191 }while( p!=0 );
33192 sqlite3TreeViewPop(&pView);
33193}
33194
33195#ifndef SQLITE_OMIT_WINDOWFUNC
33196/*
33197** Generate a description of starting or stopping bounds
33198*/
33199SQLITE_PRIVATEstatic void sqlite3TreeViewBound(
33200 TreeView *pView, /* View context */
33201 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
33202 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
33203 u8 moreToFollow /* True if more to follow */
33204){
33205 switch( eBound ){
33206 case TK_UNBOUNDED91: {
33207 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
33208 sqlite3TreeViewPop(&pView);
33209 break;
33210 }
33211 case TK_CURRENT86: {
33212 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
33213 sqlite3TreeViewPop(&pView);
33214 break;
33215 }
33216 case TK_PRECEDING89: {
33217 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
33218 sqlite3TreeViewExpr(pView, pExpr, 0);
33219 sqlite3TreeViewPop(&pView);
33220 break;
33221 }
33222 case TK_FOLLOWING87: {
33223 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
33224 sqlite3TreeViewExpr(pView, pExpr, 0);
33225 sqlite3TreeViewPop(&pView);
33226 break;
33227 }
33228 }
33229}
33230#endif /* SQLITE_OMIT_WINDOWFUNC */
33231
33232#ifndef SQLITE_OMIT_WINDOWFUNC
33233/*
33234** Generate a human-readable explanation for a Window object
33235*/
33236SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
33237 int nElement = 0;
33238 if( pWin==0 ) return;
33239 if( pWin->pFilter ){
33240 sqlite3TreeViewItem(pView, "FILTER", 1);
33241 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
33242 sqlite3TreeViewPop(&pView);
33243 if( pWin->eFrmType==TK_FILTER167 ) return;
33244 }
33245 sqlite3TreeViewPush(&pView, more);
33246 if( pWin->zName ){
33247 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
33248 }else{
33249 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
33250 }
33251 if( pWin->zBase ) nElement++;
33252 if( pWin->pOrderBy ) nElement++;
33253 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER167 ) nElement++;
33254 if( pWin->eExclude ) nElement++;
33255 if( pWin->zBase ){
33256 sqlite3TreeViewPush(&pView, (--nElement)>0);
33257 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
33258 sqlite3TreeViewPop(&pView);
33259 }
33260 if( pWin->pPartition ){
33261 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
33262 }
33263 if( pWin->pOrderBy ){
33264 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
33265 }
33266 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER167 ){
33267 char zBuf[30];
33268 const char *zFrmType = "ROWS";
33269 if( pWin->eFrmType==TK_RANGE90 ) zFrmType = "RANGE";
33270 if( pWin->eFrmType==TK_GROUPS93 ) zFrmType = "GROUPS";
33271 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
33272 pWin->bImplicitFrame ? " (implied)" : "");
33273 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
33274 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
33275 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
33276 sqlite3TreeViewPop(&pView);
33277 }
33278 if( pWin->eExclude ){
33279 char zBuf[30];
33280 const char *zExclude;
33281 switch( pWin->eExclude ){
33282 case TK_NO67: zExclude = "NO OTHERS"; break;
33283 case TK_CURRENT86: zExclude = "CURRENT ROW"; break;
33284 case TK_GROUP147: zExclude = "GROUP"; break;
33285 case TK_TIES95: zExclude = "TIES"; break;
33286 default:
33287 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
33288 zExclude = zBuf;
33289 break;
33290 }
33291 sqlite3TreeViewPush(&pView, 0);
33292 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
33293 sqlite3TreeViewPop(&pView);
33294 }
33295 sqlite3TreeViewPop(&pView);
33296}
33297#endif /* SQLITE_OMIT_WINDOWFUNC */
33298
33299#ifndef SQLITE_OMIT_WINDOWFUNC
33300/*
33301** Generate a human-readable explanation for a Window Function object
33302*/
33303SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
33304 if( pWin==0 ) return;
33305 sqlite3TreeViewPush(&pView, more);
33306 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
33307 pWin->pWFunc->zName, pWin->pWFunc->nArg);
33308 sqlite3TreeViewWindow(pView, pWin, 0);
33309 sqlite3TreeViewPop(&pView);
33310}
33311#endif /* SQLITE_OMIT_WINDOWFUNC */
33312
33313/*
33314** Generate a human-readable explanation of an expression tree.
33315*/
33316SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
33317 const char *zBinOp = 0; /* Binary operator */
33318 const char *zUniOp = 0; /* Unary operator */
33319 char zFlgs[200];
33320 sqlite3TreeViewPush(&pView, moreToFollow);
33321 if( pExpr==0 ){
33322 sqlite3TreeViewLine(pView, "nil");
33323 sqlite3TreeViewPop(&pView);
33324 return;
33325 }
33326 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){
33327 StrAccum x;
33328 sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0);
33329 sqlite3_str_appendf(&x, " fg.af=%x.%c",
33330 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
33331 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
33332 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
33333 }
33334 if( ExprHasProperty(pExpr, EP_InnerON)(((pExpr)->flags&(0x000002))!=0) ){
33335 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
33336 }
33337 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(0x40000000))!=0) ){
33338 sqlite3_str_appendf(&x, " DDL");
33339 }
33340 if( ExprHasVVAProperty(pExpr, EP_Immutable)0 ){
33341 sqlite3_str_appendf(&x, " IMMUTABLE");
33342 }
33343 if( pExpr->pAggInfo!=0 ){
33344 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg);
33345 }
33346 sqlite3StrAccumFinish(&x);
33347 }else{
33348 zFlgs[0] = 0;
33349 }
33350 switch( pExpr->op ){
33351 case TK_AGG_COLUMN170: {
33352 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
33353 pExpr->iTable, pExpr->iColumn, zFlgs);
33354 break;
33355 }
33356 case TK_COLUMN168: {
33357 if( pExpr->iTable<0 ){
33358 /* This only happens when coding check constraints */
33359 char zOp2[16];
33360 if( pExpr->op2 ){
33361 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
33362 }else{
33363 zOp2[0] = 0;
33364 }
33365 sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s",
33366 pExpr->iColumn, zFlgs, zOp2);
33367 }else{
33368 assert( ExprUseYTab(pExpr) )((void) (0));
33369 sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s",
33370 pExpr->iTable, pExpr->iColumn,
33371 pExpr->y.pTab, zFlgs);
33372 }
33373 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0) ){
33374 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33375 }
33376 break;
33377 }
33378 case TK_INTEGER156: {
33379 if( pExpr->flags & EP_IntValue0x000800 ){
33380 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
33381 }else{
33382 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
33383 }
33384 break;
33385 }
33386#ifndef SQLITE_OMIT_FLOATING_POINT
33387 case TK_FLOAT154: {
33388 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33389 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
33390 break;
33391 }
33392#endif
33393 case TK_STRING118: {
33394 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33395 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
33396 break;
33397 }
33398 case TK_NULL122: {
33399 sqlite3TreeViewLine(pView,"NULL");
33400 break;
33401 }
33402 case TK_TRUEFALSE171: {
33403 sqlite3TreeViewLine(pView,"%s%s",
33404 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs);
33405 break;
33406 }
33407#ifndef SQLITE_OMIT_BLOB_LITERAL
33408 case TK_BLOB155: {
33409 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33410 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
33411 break;
33412 }
33413#endif
33414 case TK_VARIABLE157: {
33415 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33416 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
33417 pExpr->u.zToken, pExpr->iColumn);
33418 break;
33419 }
33420 case TK_REGISTER176: {
33421 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
33422 break;
33423 }
33424 case TK_ID60: {
33425 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33426 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
33427 break;
33428 }
33429#ifndef SQLITE_OMIT_CAST
33430 case TK_CAST36: {
33431 /* Expressions of the form: CAST(pLeft AS token) */
33432 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33433 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
33434 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33435 break;
33436 }
33437#endif /* SQLITE_OMIT_CAST */
33438 case TK_LT57: zBinOp = "LT"; break;
33439 case TK_LE56: zBinOp = "LE"; break;
33440 case TK_GT55: zBinOp = "GT"; break;
33441 case TK_GE58: zBinOp = "GE"; break;
33442 case TK_NE53: zBinOp = "NE"; break;
33443 case TK_EQ54: zBinOp = "EQ"; break;
33444 case TK_IS45: zBinOp = "IS"; break;
33445 case TK_ISNOT46: zBinOp = "ISNOT"; break;
33446 case TK_AND44: zBinOp = "AND"; break;
33447 case TK_OR43: zBinOp = "OR"; break;
33448 case TK_PLUS107: zBinOp = "ADD"; break;
33449 case TK_STAR109: zBinOp = "MUL"; break;
33450 case TK_MINUS108: zBinOp = "SUB"; break;
33451 case TK_REM111: zBinOp = "REM"; break;
33452 case TK_BITAND103: zBinOp = "BITAND"; break;
33453 case TK_BITOR104: zBinOp = "BITOR"; break;
33454 case TK_SLASH110: zBinOp = "DIV"; break;
33455 case TK_LSHIFT105: zBinOp = "LSHIFT"; break;
33456 case TK_RSHIFT106: zBinOp = "RSHIFT"; break;
33457 case TK_CONCAT112: zBinOp = "CONCAT"; break;
33458 case TK_DOT142: zBinOp = "DOT"; break;
33459 case TK_LIMIT149: zBinOp = "LIMIT"; break;
33460
33461 case TK_UMINUS174: zUniOp = "UMINUS"; break;
33462 case TK_UPLUS173: zUniOp = "UPLUS"; break;
33463 case TK_BITNOT115: zUniOp = "BITNOT"; break;
33464 case TK_NOT19: zUniOp = "NOT"; break;
33465 case TK_ISNULL51: zUniOp = "ISNULL"; break;
33466 case TK_NOTNULL52: zUniOp = "NOTNULL"; break;
33467
33468 case TK_TRUTH175: {
33469 int x;
33470 const char *azOp[] = {
33471 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
33472 };
33473 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT )((void) (0));
33474 assert( pExpr->pRight )((void) (0));
33475 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op((void) (0))
33476 == TK_TRUEFALSE )((void) (0));
33477 x = (pExpr->op2==TK_ISNOT46)*2 + sqlite3ExprTruthValue(pExpr->pRight);
33478 zUniOp = azOp[x];
33479 break;
33480 }
33481
33482 case TK_SPAN181: {
33483 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33484 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
33485 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33486 break;
33487 }
33488
33489 case TK_COLLATE114: {
33490 /* COLLATE operators without the EP_Collate flag are intended to
33491 ** emulate collation associated with a table column. These show
33492 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
33493 ** operators that appear in the original SQL always have the
33494 ** EP_Collate bit set and appear in treeview output as just "COLLATE" */
33495 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33496 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
33497 !ExprHasProperty(pExpr, EP_Collate)(((pExpr)->flags&(0x000200))!=0) ? "SOFT-" : "",
33498 pExpr->u.zToken, zFlgs);
33499 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33500 break;
33501 }
33502
33503 case TK_AGG_FUNCTION169:
33504 case TK_FUNCTION172: {
33505 ExprList *pFarg; /* List of function arguments */
33506 Window *pWin;
33507 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(0x010000))!=0) ){
33508 pFarg = 0;
33509 pWin = 0;
33510 }else{
33511 assert( ExprUseXList(pExpr) )((void) (0));
33512 pFarg = pExpr->x.pList;
33513#ifndef SQLITE_OMIT_WINDOWFUNC
33514 pWin = IsWindowFunc(pExpr)( ((((pExpr))->flags&(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=167 )
? pExpr->y.pWin : 0;
33515#else
33516 pWin = 0;
33517#endif
33518 }
33519 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33520 if( pExpr->op==TK_AGG_FUNCTION169 ){
33521 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p",
33522 pExpr->op2, pExpr->u.zToken, zFlgs,
33523 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
33524 pExpr->iAgg, pExpr->pAggInfo);
33525 }else if( pExpr->op2!=0 ){
33526 const char *zOp2;
33527 char zBuf[8];
33528 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
33529 zOp2 = zBuf;
33530 if( pExpr->op2==NC_IsCheck0x000004 ) zOp2 = "NC_IsCheck";
33531 if( pExpr->op2==NC_IdxExpr0x000020 ) zOp2 = "NC_IdxExpr";
33532 if( pExpr->op2==NC_PartIdx0x000002 ) zOp2 = "NC_PartIdx";
33533 if( pExpr->op2==NC_GenCol0x000008 ) zOp2 = "NC_GenCol";
33534 sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s",
33535 pExpr->u.zToken, zFlgs, zOp2);
33536 }else{
33537 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
33538 }
33539 if( pFarg ){
33540 sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0);
33541 if( pExpr->pLeft ){
33542 Expr *pOB = pExpr->pLeft;
33543 assert( pOB->op==TK_ORDER )((void) (0));
33544 assert( ExprUseXList(pOB) )((void) (0));
33545 sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY");
33546 }
33547 }
33548#ifndef SQLITE_OMIT_WINDOWFUNC
33549 if( pWin ){
33550 sqlite3TreeViewWindow(pView, pWin, 0);
33551 }
33552#endif
33553 break;
33554 }
33555 case TK_ORDER146: {
33556 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY");
33557 break;
33558 }
33559#ifndef SQLITE_OMIT_SUBQUERY
33560 case TK_EXISTS20: {
33561 assert( ExprUseXSelect(pExpr) )((void) (0));
33562 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
33563 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
33564 break;
33565 }
33566 case TK_SELECT139: {
33567 assert( ExprUseXSelect(pExpr) )((void) (0));
33568 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
33569 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
33570 break;
33571 }
33572 case TK_IN50: {
33573 sqlite3_str *pStr = sqlite3_str_new(0);
33574 char *z;
33575 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
33576 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
33577 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
33578 sqlite3_str_appendf(pStr, " subrtn(%d,%d)",
33579 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
33580 }
33581 z = sqlite3_str_finish(pStr);
33582 sqlite3TreeViewLine(pView, z);
33583 sqlite3_free(z);
33584 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
33585 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
33586 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
33587 }else{
33588 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
33589 }
33590 break;
33591 }
33592#endif /* SQLITE_OMIT_SUBQUERY */
33593
33594 /*
33595 ** x BETWEEN y AND z
33596 **
33597 ** This is equivalent to
33598 **
33599 ** x>=y AND x<=z
33600 **
33601 ** X is stored in pExpr->pLeft.
33602 ** Y is stored in pExpr->pList->a[0].pExpr.
33603 ** Z is stored in pExpr->pList->a[1].pExpr.
33604 */
33605 case TK_BETWEEN49: {
33606 const Expr *pX, *pY, *pZ;
33607 pX = pExpr->pLeft;
33608 assert( ExprUseXList(pExpr) )((void) (0));
33609 assert( pExpr->x.pList->nExpr==2 )((void) (0));
33610 pY = pExpr->x.pList->a[0].pExpr;
33611 pZ = pExpr->x.pList->a[1].pExpr;
33612 sqlite3TreeViewLine(pView, "BETWEEN%s", zFlgs);
33613 sqlite3TreeViewExpr(pView, pX, 1);
33614 sqlite3TreeViewExpr(pView, pY, 1);
33615 sqlite3TreeViewExpr(pView, pZ, 0);
33616 break;
33617 }
33618 case TK_TRIGGER78: {
33619 /* If the opcode is TK_TRIGGER, then the expression is a reference
33620 ** to a column in the new.* or old.* pseudo-tables available to
33621 ** trigger programs. In this case Expr.iTable is set to 1 for the
33622 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
33623 ** is set to the column of the pseudo-table to read, or to -1 to
33624 ** read the rowid field.
33625 */
33626 sqlite3TreeViewLine(pView, "%s(%d)",
33627 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
33628 break;
33629 }
33630 case TK_CASE158: {
33631 sqlite3TreeViewLine(pView, "CASE");
33632 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
33633 assert( ExprUseXList(pExpr) )((void) (0));
33634 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
33635 break;
33636 }
33637#ifndef SQLITE_OMIT_TRIGGER
33638 case TK_RAISE72: {
33639 const char *zType = "unk";
33640 switch( pExpr->affExpr ){
33641 case OE_Rollback1: zType = "rollback"; break;
33642 case OE_Abort2: zType = "abort"; break;
33643 case OE_Fail3: zType = "fail"; break;
33644 case OE_Ignore4: zType = "ignore"; break;
33645 }
33646 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
33647 sqlite3TreeViewLine(pView, "RAISE %s", zType);
33648 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33649 break;
33650 }
33651#endif
33652 case TK_MATCH47: {
33653 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
33654 pExpr->iTable, pExpr->iColumn, zFlgs);
33655 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
33656 break;
33657 }
33658 case TK_VECTOR177: {
33659 char *z = sqlite3_mprintf("VECTOR%s",zFlgs);
33660 assert( ExprUseXList(pExpr) )((void) (0));
33661 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
33662 sqlite3_free(z);
33663 break;
33664 }
33665 case TK_SELECT_COLUMN178: {
33666 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
33667 pExpr->iColumn, pExpr->iTable-1,
33668 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
33669 assert( ExprUseXSelect(pExpr->pLeft) )((void) (0));
33670 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
33671 break;
33672 }
33673 case TK_IF_NULL_ROW179: {
33674 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
33675 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33676 break;
33677 }
33678 case TK_ERROR182: {
33679 Expr tmp;
33680 sqlite3TreeViewLine(pView, "ERROR");
33681 tmp = *pExpr;
33682 tmp.op = pExpr->op2;
33683 sqlite3TreeViewExpr(pView, &tmp, 0);
33684 break;
33685 }
33686 case TK_ROW76: {
33687 if( pExpr->iColumn<=0 ){
33688 sqlite3TreeViewLine(pView, "First FROM table rowid");
33689 }else{
33690 sqlite3TreeViewLine(pView, "First FROM table column %d",
33691 pExpr->iColumn-1);
33692 }
33693 break;
33694 }
33695 default: {
33696 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
33697 break;
33698 }
33699 }
33700 if( zBinOp ){
33701 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
33702 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
33703 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
33704 }else if( zUniOp ){
33705 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
33706 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
33707 }
33708 sqlite3TreeViewPop(&pView);
33709}
33710
33711
33712/*
33713** Generate a human-readable explanation of an expression list.
33714*/
33715SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(
33716 TreeView *pView,
33717 const ExprList *pList,
33718 const char *zLabel
33719){
33720 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
33721 if( pList==0 ){
33722 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
33723 }else{
33724 int i;
33725 sqlite3TreeViewLine(pView, "%s", zLabel);
33726 for(i=0; i<pList->nExpr; i++){
33727 int j = pList->a[i].u.x.iOrderByCol;
33728 u8 sortFlags = pList->a[i].fg.sortFlags;
33729 char *zName = pList->a[i].zEName;
33730 int moreToFollow = i<pList->nExpr - 1;
33731 if( j || zName || sortFlags ){
33732 sqlite3TreeViewPush(&pView, moreToFollow);
33733 moreToFollow = 0;
33734 sqlite3TreeViewLine(pView, 0);
33735 if( zName ){
33736 switch( pList->a[i].fg.eEName ){
33737 default:
33738 fprintf(stdoutstdout, "AS %s ", zName);
33739 break;
33740 case ENAME_TAB2:
33741 fprintf(stdoutstdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
33742 if( pList->a[i].fg.bUsed ) fprintf(stdoutstdout, "(used) ");
33743 if( pList->a[i].fg.bUsingTerm ) fprintf(stdoutstdout, "(USING-term) ");
33744 if( pList->a[i].fg.bNoExpand ) fprintf(stdoutstdout, "(NoExpand) ");
33745 break;
33746 case ENAME_SPAN1:
33747 fprintf(stdoutstdout, "SPAN(\"%s\") ", zName);
33748 break;
33749 }
33750 }
33751 if( j ){
33752 fprintf(stdoutstdout, "iOrderByCol=%d ", j);
33753 }
33754 if( sortFlags & KEYINFO_ORDER_DESC0x01 ){
33755 fprintf(stdoutstdout, "DESC ");
33756 }else if( sortFlags & KEYINFO_ORDER_BIGNULL0x02 ){
33757 fprintf(stdoutstdout, "NULLS-LAST");
33758 }
33759 fprintf(stdoutstdout, "\n");
33760 fflush(stdoutstdout);
33761 }
33762 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
33763 if( j || zName || sortFlags ){
33764 sqlite3TreeViewPop(&pView);
33765 }
33766 }
33767 }
33768}
33769SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(
33770 TreeView *pView,
33771 const ExprList *pList,
33772 u8 moreToFollow,
33773 const char *zLabel
33774){
33775 sqlite3TreeViewPush(&pView, moreToFollow);
33776 sqlite3TreeViewBareExprList(pView, pList, zLabel);
33777 sqlite3TreeViewPop(&pView);
33778}
33779
33780/*
33781** Generate a human-readable explanation of an id-list.
33782*/
33783SQLITE_PRIVATEstatic void sqlite3TreeViewBareIdList(
33784 TreeView *pView,
33785 const IdList *pList,
33786 const char *zLabel
33787){
33788 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
33789 if( pList==0 ){
33790 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
33791 }else{
33792 int i;
33793 sqlite3TreeViewLine(pView, "%s", zLabel);
33794 for(i=0; i<pList->nId; i++){
33795 char *zName = pList->a[i].zName;
33796 int moreToFollow = i<pList->nId - 1;
33797 if( zName==0 ) zName = "(null)";
33798 sqlite3TreeViewPush(&pView, moreToFollow);
33799 sqlite3TreeViewLine(pView, 0);
33800 if( pList->eU4==EU4_NONE0 ){
33801 fprintf(stdoutstdout, "%s\n", zName);
33802 }else if( pList->eU4==EU4_IDX1 ){
33803 fprintf(stdoutstdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
33804 }else{
33805 assert( pList->eU4==EU4_EXPR )((void) (0));
33806 if( pList->a[i].u4.pExpr==0 ){
33807 fprintf(stdoutstdout, "%s (pExpr=NULL)\n", zName);
33808 }else{
33809 fprintf(stdoutstdout, "%s\n", zName);
33810 sqlite3TreeViewPush(&pView, i<pList->nId-1);
33811 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
33812 sqlite3TreeViewPop(&pView);
33813 }
33814 }
33815 sqlite3TreeViewPop(&pView);
33816 }
33817 }
33818}
33819SQLITE_PRIVATEstatic void sqlite3TreeViewIdList(
33820 TreeView *pView,
33821 const IdList *pList,
33822 u8 moreToFollow,
33823 const char *zLabel
33824){
33825 sqlite3TreeViewPush(&pView, moreToFollow);
33826 sqlite3TreeViewBareIdList(pView, pList, zLabel);
33827 sqlite3TreeViewPop(&pView);
33828}
33829
33830/*
33831** Generate a human-readable explanation of a list of Upsert objects
33832*/
33833SQLITE_PRIVATEstatic void sqlite3TreeViewUpsert(
33834 TreeView *pView,
33835 const Upsert *pUpsert,
33836 u8 moreToFollow
33837){
33838 if( pUpsert==0 ) return;
33839 sqlite3TreeViewPush(&pView, moreToFollow);
33840 while( pUpsert ){
33841 int n;
33842 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
33843 sqlite3TreeViewLine(pView, "ON CONFLICT DO %s",
33844 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
33845 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
33846 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
33847 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
33848 if( pUpsert->pUpsertWhere ){
33849 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
33850 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
33851 sqlite3TreeViewPop(&pView);
33852 }
33853 sqlite3TreeViewPop(&pView);
33854 pUpsert = pUpsert->pNextUpsert;
33855 }
33856 sqlite3TreeViewPop(&pView);
33857}
33858
33859#if TREETRACE_ENABLED0
33860/*
33861** Generate a human-readable diagram of the data structure that go
33862** into generating an DELETE statement.
33863*/
33864SQLITE_PRIVATEstatic void sqlite3TreeViewDelete(
33865 const With *pWith,
33866 const SrcList *pTabList,
33867 const Expr *pWhere,
33868 const ExprList *pOrderBy,
33869 const Expr *pLimit,
33870 const Trigger *pTrigger
33871){
33872 int n = 0;
33873 TreeView *pView = 0;
33874 sqlite3TreeViewPush(&pView, 0);
33875 sqlite3TreeViewLine(pView, "DELETE");
33876 if( pWith ) n++;
33877 if( pTabList ) n++;
33878 if( pWhere ) n++;
33879 if( pOrderBy ) n++;
33880 if( pLimit ) n++;
33881 if( pTrigger ) n++;
33882 if( pWith ){
33883 sqlite3TreeViewPush(&pView, (--n)>0);
33884 sqlite3TreeViewWith(pView, pWith, 0);
33885 sqlite3TreeViewPop(&pView);
33886 }
33887 if( pTabList ){
33888 sqlite3TreeViewPush(&pView, (--n)>0);
33889 sqlite3TreeViewLine(pView, "FROM");
33890 sqlite3TreeViewSrcList(pView, pTabList);
33891 sqlite3TreeViewPop(&pView);
33892 }
33893 if( pWhere ){
33894 sqlite3TreeViewPush(&pView, (--n)>0);
33895 sqlite3TreeViewLine(pView, "WHERE");
33896 sqlite3TreeViewExpr(pView, pWhere, 0);
33897 sqlite3TreeViewPop(&pView);
33898 }
33899 if( pOrderBy ){
33900 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
33901 }
33902 if( pLimit ){
33903 sqlite3TreeViewPush(&pView, (--n)>0);
33904 sqlite3TreeViewLine(pView, "LIMIT");
33905 sqlite3TreeViewExpr(pView, pLimit, 0);
33906 sqlite3TreeViewPop(&pView);
33907 }
33908 if( pTrigger ){
33909 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
33910 }
33911 sqlite3TreeViewPop(&pView);
33912}
33913#endif /* TREETRACE_ENABLED */
33914
33915#if TREETRACE_ENABLED0
33916/*
33917** Generate a human-readable diagram of the data structure that go
33918** into generating an INSERT statement.
33919*/
33920SQLITE_PRIVATEstatic void sqlite3TreeViewInsert(
33921 const With *pWith,
33922 const SrcList *pTabList,
33923 const IdList *pColumnList,
33924 const Select *pSelect,
33925 const ExprList *pExprList,
33926 int onError,
33927 const Upsert *pUpsert,
33928 const Trigger *pTrigger
33929){
33930 TreeView *pView = 0;
33931 int n = 0;
33932 const char *zLabel = "INSERT";
33933 switch( onError ){
33934 case OE_Replace5: zLabel = "REPLACE"; break;
33935 case OE_Ignore4: zLabel = "INSERT OR IGNORE"; break;
33936 case OE_Rollback1: zLabel = "INSERT OR ROLLBACK"; break;
33937 case OE_Abort2: zLabel = "INSERT OR ABORT"; break;
33938 case OE_Fail3: zLabel = "INSERT OR FAIL"; break;
33939 }
33940 sqlite3TreeViewPush(&pView, 0);
33941 sqlite3TreeViewLine(pView, zLabel);
33942 if( pWith ) n++;
33943 if( pTabList ) n++;
33944 if( pColumnList ) n++;
33945 if( pSelect ) n++;
33946 if( pExprList ) n++;
33947 if( pUpsert ) n++;
33948 if( pTrigger ) n++;
33949 if( pWith ){
33950 sqlite3TreeViewPush(&pView, (--n)>0);
33951 sqlite3TreeViewWith(pView, pWith, 0);
33952 sqlite3TreeViewPop(&pView);
33953 }
33954 if( pTabList ){
33955 sqlite3TreeViewPush(&pView, (--n)>0);
33956 sqlite3TreeViewLine(pView, "INTO");
33957 sqlite3TreeViewSrcList(pView, pTabList);
33958 sqlite3TreeViewPop(&pView);
33959 }
33960 if( pColumnList ){
33961 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
33962 }
33963 if( pSelect ){
33964 sqlite3TreeViewPush(&pView, (--n)>0);
33965 sqlite3TreeViewLine(pView, "DATA-SOURCE");
33966 sqlite3TreeViewSelect(pView, pSelect, 0);
33967 sqlite3TreeViewPop(&pView);
33968 }
33969 if( pExprList ){
33970 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
33971 }
33972 if( pUpsert ){
33973 sqlite3TreeViewPush(&pView, (--n)>0);
33974 sqlite3TreeViewLine(pView, "UPSERT");
33975 sqlite3TreeViewUpsert(pView, pUpsert, 0);
33976 sqlite3TreeViewPop(&pView);
33977 }
33978 if( pTrigger ){
33979 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
33980 }
33981 sqlite3TreeViewPop(&pView);
33982}
33983#endif /* TREETRACE_ENABLED */
33984
33985#if TREETRACE_ENABLED0
33986/*
33987** Generate a human-readable diagram of the data structure that go
33988** into generating an UPDATE statement.
33989*/
33990SQLITE_PRIVATEstatic void sqlite3TreeViewUpdate(
33991 const With *pWith,
33992 const SrcList *pTabList,
33993 const ExprList *pChanges,
33994 const Expr *pWhere,
33995 int onError,
33996 const ExprList *pOrderBy,
33997 const Expr *pLimit,
33998 const Upsert *pUpsert,
33999 const Trigger *pTrigger
34000){
34001 int n = 0;
34002 TreeView *pView = 0;
34003 const char *zLabel = "UPDATE";
34004 switch( onError ){
34005 case OE_Replace5: zLabel = "UPDATE OR REPLACE"; break;
34006 case OE_Ignore4: zLabel = "UPDATE OR IGNORE"; break;
34007 case OE_Rollback1: zLabel = "UPDATE OR ROLLBACK"; break;
34008 case OE_Abort2: zLabel = "UPDATE OR ABORT"; break;
34009 case OE_Fail3: zLabel = "UPDATE OR FAIL"; break;
34010 }
34011 sqlite3TreeViewPush(&pView, 0);
34012 sqlite3TreeViewLine(pView, zLabel);
34013 if( pWith ) n++;
34014 if( pTabList ) n++;
34015 if( pChanges ) n++;
34016 if( pWhere ) n++;
34017 if( pOrderBy ) n++;
34018 if( pLimit ) n++;
34019 if( pUpsert ) n++;
34020 if( pTrigger ) n++;
34021 if( pWith ){
34022 sqlite3TreeViewPush(&pView, (--n)>0);
34023 sqlite3TreeViewWith(pView, pWith, 0);
34024 sqlite3TreeViewPop(&pView);
34025 }
34026 if( pTabList ){
34027 sqlite3TreeViewPush(&pView, (--n)>0);
34028 sqlite3TreeViewLine(pView, "FROM");
34029 sqlite3TreeViewSrcList(pView, pTabList);
34030 sqlite3TreeViewPop(&pView);
34031 }
34032 if( pChanges ){
34033 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
34034 }
34035 if( pWhere ){
34036 sqlite3TreeViewPush(&pView, (--n)>0);
34037 sqlite3TreeViewLine(pView, "WHERE");
34038 sqlite3TreeViewExpr(pView, pWhere, 0);
34039 sqlite3TreeViewPop(&pView);
34040 }
34041 if( pOrderBy ){
34042 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
34043 }
34044 if( pLimit ){
34045 sqlite3TreeViewPush(&pView, (--n)>0);
34046 sqlite3TreeViewLine(pView, "LIMIT");
34047 sqlite3TreeViewExpr(pView, pLimit, 0);
34048 sqlite3TreeViewPop(&pView);
34049 }
34050 if( pUpsert ){
34051 sqlite3TreeViewPush(&pView, (--n)>0);
34052 sqlite3TreeViewLine(pView, "UPSERT");
34053 sqlite3TreeViewUpsert(pView, pUpsert, 0);
34054 sqlite3TreeViewPop(&pView);
34055 }
34056 if( pTrigger ){
34057 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
34058 }
34059 sqlite3TreeViewPop(&pView);
34060}
34061#endif /* TREETRACE_ENABLED */
34062
34063#ifndef SQLITE_OMIT_TRIGGER
34064/*
34065** Show a human-readable graph of a TriggerStep
34066*/
34067SQLITE_PRIVATEstatic void sqlite3TreeViewTriggerStep(
34068 TreeView *pView,
34069 const TriggerStep *pStep,
34070 u8 moreToFollow,
34071 u8 showFullList
34072){
34073 int cnt = 0;
34074 if( pStep==0 ) return;
34075 sqlite3TreeViewPush(&pView,
34076 moreToFollow || (showFullList && pStep->pNext!=0));
34077 do{
34078 if( cnt++ && pStep->pNext==0 ){
34079 sqlite3TreeViewPop(&pView);
34080 sqlite3TreeViewPush(&pView, 0);
34081 }
34082 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
34083 }while( showFullList && (pStep = pStep->pNext)!=0 );
34084 sqlite3TreeViewPop(&pView);
34085}
34086
34087/*
34088** Show a human-readable graph of a Trigger
34089*/
34090SQLITE_PRIVATEstatic void sqlite3TreeViewTrigger(
34091 TreeView *pView,
34092 const Trigger *pTrigger,
34093 u8 moreToFollow,
34094 u8 showFullList
34095){
34096 int cnt = 0;
34097 if( pTrigger==0 ) return;
34098 sqlite3TreeViewPush(&pView,
34099 moreToFollow || (showFullList && pTrigger->pNext!=0));
34100 do{
34101 if( cnt++ && pTrigger->pNext==0 ){
34102 sqlite3TreeViewPop(&pView);
34103 sqlite3TreeViewPush(&pView, 0);
34104 }
34105 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
34106 sqlite3TreeViewPush(&pView, 0);
34107 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
34108 sqlite3TreeViewPop(&pView);
34109 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
34110 sqlite3TreeViewPop(&pView);
34111}
34112#endif /* SQLITE_OMIT_TRIGGER */
34113
34114
34115/*
34116** These simplified versions of the tree-view routines omit unnecessary
34117** parameters. These variants are intended to be used from a symbolic
34118** debugger, such as "gdb", during interactive debugging sessions.
34119**
34120** This routines are given external linkage so that they will always be
34121** accessible to the debugging, and to avoid warnings about unused
34122** functions. But these routines only exist in debugging builds, so they
34123** do not contaminate the interface.
34124*/
34125SQLITE_PRIVATEstatic void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
34126SQLITE_PRIVATEstatic void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
34127SQLITE_PRIVATEstatic void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
34128SQLITE_PRIVATEstatic void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
34129SQLITE_PRIVATEstatic void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); }
34130SQLITE_PRIVATEstatic void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); }
34131SQLITE_PRIVATEstatic void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); }
34132#ifndef SQLITE_OMIT_TRIGGER
34133SQLITE_PRIVATEstatic void sqlite3ShowTriggerStep(const TriggerStep *p){
34134 sqlite3TreeViewTriggerStep(0,p,0,0);
34135}
34136SQLITE_PRIVATEstatic void sqlite3ShowTriggerStepList(const TriggerStep *p){
34137 sqlite3TreeViewTriggerStep(0,p,0,1);
34138}
34139SQLITE_PRIVATEstatic void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); }
34140SQLITE_PRIVATEstatic void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);}
34141#endif
34142#ifndef SQLITE_OMIT_WINDOWFUNC
34143SQLITE_PRIVATEstatic void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); }
34144SQLITE_PRIVATEstatic void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); }
34145#endif
34146
34147#endif /* SQLITE_DEBUG */
34148
34149/************** End of treeview.c ********************************************/
34150/************** Begin file random.c ******************************************/
34151/*
34152** 2001 September 15
34153**
34154** The author disclaims copyright to this source code. In place of
34155** a legal notice, here is a blessing:
34156**
34157** May you do good and not evil.
34158** May you find forgiveness for yourself and forgive others.
34159** May you share freely, never taking more than you give.
34160**
34161*************************************************************************
34162** This file contains code to implement a pseudo-random number
34163** generator (PRNG) for SQLite.
34164**
34165** Random numbers are used by some of the database backends in order
34166** to generate random integer keys for tables or random filenames.
34167*/
34168/* #include "sqliteInt.h" */
34169
34170
34171/* All threads share a single random number generator.
34172** This structure is the current state of the generator.
34173*/
34174static SQLITE_WSD struct sqlite3PrngType {
34175 u32 s[16]; /* 64 bytes of chacha20 state */
34176 u8 out[64]; /* Output bytes */
34177 u8 n; /* Output bytes remaining */
34178} sqlite3Prng;
34179
34180
34181/* The RFC-7539 ChaCha20 block function
34182*/
34183#define ROTL(a,b)(((a) << (b)) | ((a) >> (32 - (b)))) (((a) << (b)) | ((a) >> (32 - (b))))
34184#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)))))
( \
34185 a += b, d ^= a, d = ROTL(d,16)(((d) << (16)) | ((d) >> (32 - (16)))), \
34186 c += d, b ^= c, b = ROTL(b,12)(((b) << (12)) | ((b) >> (32 - (12)))), \
34187 a += b, d ^= a, d = ROTL(d, 8)(((d) << (8)) | ((d) >> (32 - (8)))), \
34188 c += d, b ^= c, b = ROTL(b, 7)(((b) << (7)) | ((b) >> (32 - (7)))))
34189static void chacha_block(u32 *out, const u32 *in){
34190 int i;
34191 u32 x[16];
34192 memcpy(x, in, 64);
34193 for(i=0; i<10; i++){
34194 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)))))
;
34195 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)))))
;
34196 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)))))
;
34197 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)))))
;
34198 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)))))
;
34199 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)))))
;
34200 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)))))
;
34201 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)))))
;
34202 }
34203 for(i=0; i<16; i++) out[i] = x[i]+in[i];
34204}
34205
34206/*
34207** Return N random bytes.
34208*/
34209SQLITE_API void sqlite3_randomness(int N, void *pBuf){
34210 unsigned char *zBuf = pBuf;
34211
34212 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
34213 ** state vector. If writable static data is unsupported on the target,
34214 ** we have to locate the state vector at run-time. In the more common
34215 ** case where writable static data is supported, wsdPrng can refer directly
34216 ** to the "sqlite3Prng" state vector declared above.
34217 */
34218#ifdef SQLITE_OMIT_WSD
34219 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng;
34220# define wsdPrngsqlite3Prng p[0]
34221#else
34222# define wsdPrngsqlite3Prng sqlite3Prng
34223#endif
34224
34225#if SQLITE_THREADSAFE2
34226 sqlite3_mutex *mutex;
34227#endif
34228
34229#ifndef SQLITE_OMIT_AUTOINIT
34230 if( sqlite3_initialize() ) return;
34231#endif
34232
34233#if SQLITE_THREADSAFE2
34234 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG5);
34235#endif
34236
34237 sqlite3_mutex_enter(mutex);
34238 if( N<=0 || pBuf==0 ){
34239 wsdPrngsqlite3Prng.s[0] = 0;
34240 sqlite3_mutex_leave(mutex);
34241 return;
34242 }
34243
34244 /* Initialize the state of the random number generator once,
34245 ** the first time this routine is called.
34246 */
34247 if( wsdPrngsqlite3Prng.s[0]==0 ){
34248 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
34249 static const u32 chacha20_init[] = {
34250 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
34251 };
34252 memcpy(&wsdPrngsqlite3Prng.s[0], chacha20_init, 16);
34253 if( NEVER(pVfs==0)(pVfs==0) ){
34254 memset(&wsdPrngsqlite3Prng.s[4], 0, 44);
34255 }else{
34256 sqlite3OsRandomness(pVfs, 44, (char*)&wsdPrngsqlite3Prng.s[4]);
34257 }
34258 wsdPrngsqlite3Prng.s[15] = wsdPrngsqlite3Prng.s[12];
34259 wsdPrngsqlite3Prng.s[12] = 0;
34260 wsdPrngsqlite3Prng.n = 0;
34261 }
34262
34263 assert( N>0 )((void) (0));
34264 while( 1 /* exit by break */ ){
34265 if( N<=wsdPrngsqlite3Prng.n ){
34266 memcpy(zBuf, &wsdPrngsqlite3Prng.out[wsdPrngsqlite3Prng.n-N], N);
34267 wsdPrngsqlite3Prng.n -= N;
34268 break;
34269 }
34270 if( wsdPrngsqlite3Prng.n>0 ){
34271 memcpy(zBuf, wsdPrngsqlite3Prng.out, wsdPrngsqlite3Prng.n);
34272 N -= wsdPrngsqlite3Prng.n;
34273 zBuf += wsdPrngsqlite3Prng.n;
34274 }
34275 wsdPrngsqlite3Prng.s[12]++;
34276 chacha_block((u32*)wsdPrngsqlite3Prng.out, wsdPrngsqlite3Prng.s);
34277 wsdPrngsqlite3Prng.n = 64;
34278 }
34279 sqlite3_mutex_leave(mutex);
34280}
34281
34282#ifndef SQLITE_UNTESTABLE
34283/*
34284** For testing purposes, we sometimes want to preserve the state of
34285** PRNG and restore the PRNG to its saved state at a later time, or
34286** to reset the PRNG to its initial state. These routines accomplish
34287** those tasks.
34288**
34289** The sqlite3_test_control() interface calls these routines to
34290** control the PRNG.
34291*/
34292static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
34293SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void){
34294 memcpy(
34295 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
34296 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
34297 sizeof(sqlite3Prng)
34298 );
34299}
34300SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void){
34301 memcpy(
34302 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
34303 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
34304 sizeof(sqlite3Prng)
34305 );
34306}
34307#endif /* SQLITE_UNTESTABLE */
34308
34309/************** End of random.c **********************************************/
34310/************** Begin file threads.c *****************************************/
34311/*
34312** 2012 July 21
34313**
34314** The author disclaims copyright to this source code. In place of
34315** a legal notice, here is a blessing:
34316**
34317** May you do good and not evil.
34318** May you find forgiveness for yourself and forgive others.
34319** May you share freely, never taking more than you give.
34320**
34321******************************************************************************
34322**
34323** This file presents a simple cross-platform threading interface for
34324** use internally by SQLite.
34325**
34326** A "thread" can be created using sqlite3ThreadCreate(). This thread
34327** runs independently of its creator until it is joined using
34328** sqlite3ThreadJoin(), at which point it terminates.
34329**
34330** Threads do not have to be real. It could be that the work of the
34331** "thread" is done by the main thread at either the sqlite3ThreadCreate()
34332** or sqlite3ThreadJoin() call. This is, in fact, what happens in
34333** single threaded systems. Nothing in SQLite requires multiple threads.
34334** This interface exists so that applications that want to take advantage
34335** of multiple cores can do so, while also allowing applications to stay
34336** single-threaded if desired.
34337*/
34338/* #include "sqliteInt.h" */
34339#if SQLITE_OS_WIN0
34340/* # include "os_win.h" */
34341#endif
34342
34343#if SQLITE_MAX_WORKER_THREADS8>0
34344
34345/********************************* Unix Pthreads ****************************/
34346#if SQLITE_OS_UNIX1 && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE2>0
34347
34348#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
34349/* #include <pthread.h> */
34350
34351/* A running thread */
34352struct SQLiteThread {
34353 pthread_t tid; /* Thread ID */
34354 int done; /* Set to true when thread finishes */
34355 void *pOut; /* Result returned by the thread */
34356 void *(*xTask)(void*); /* The thread routine */
34357 void *pIn; /* Argument to the thread */
34358};
34359
34360/* Create a new thread */
34361SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
34362 SQLiteThread **ppThread, /* OUT: Write the thread object here */
34363 void *(*xTask)(void*), /* Routine to run in a separate thread */
34364 void *pIn /* Argument passed into xTask() */
34365){
34366 SQLiteThread *p;
34367 int rc;
34368
34369 assert( ppThread!=0 )((void) (0));
34370 assert( xTask!=0 )((void) (0));
34371 /* This routine is never used in single-threaded mode */
34372 assert( sqlite3GlobalConfig.bCoreMutex!=0 )((void) (0));
34373
34374 *ppThread = 0;
34375 p = sqlite3Malloc(sizeof(*p));
34376 if( p==0 ) return SQLITE_NOMEM_BKPT7;
34377 memset(p, 0, sizeof(*p));
34378 p->xTask = xTask;
34379 p->pIn = pIn;
34380 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
34381 ** function that returns SQLITE_ERROR when passed the argument 200, that
34382 ** forces worker threads to run sequentially and deterministically
34383 ** for testing purposes. */
34384 if( sqlite3FaultSim(200) ){
34385 rc = 1;
34386 }else{
34387 rc = pthread_create(&p->tid, 0, xTask, pIn);
34388 }
34389 if( rc ){
34390 p->done = 1;
34391 p->pOut = xTask(pIn);
34392 }
34393 *ppThread = p;
34394 return SQLITE_OK0;
34395}
34396
34397/* Get the results of the thread */
34398SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
34399 int rc;
34400
34401 assert( ppOut!=0 )((void) (0));
34402 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
34403 if( p->done ){
34404 *ppOut = p->pOut;
34405 rc = SQLITE_OK0;
34406 }else{
34407 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR1 : SQLITE_OK0;
34408 }
34409 sqlite3_free(p);
34410 return rc;
34411}
34412
34413#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
34414/******************************** End Unix Pthreads *************************/
34415
34416
34417/********************************* Win32 Threads ****************************/
34418#if SQLITE_OS_WIN_THREADS
34419
34420#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
34421#include <process.h>
34422
34423/* A running thread */
34424struct SQLiteThread {
34425 void *tid; /* The thread handle */
34426 unsigned id; /* The thread identifier */
34427 void *(*xTask)(void*); /* The routine to run as a thread */
34428 void *pIn; /* Argument to xTask */
34429 void *pResult; /* Result of xTask */
34430};
34431
34432/* Thread procedure Win32 compatibility shim */
34433static unsigned __stdcall sqlite3ThreadProc(
34434 void *pArg /* IN: Pointer to the SQLiteThread structure */
34435){
34436 SQLiteThread *p = (SQLiteThread *)pArg;
34437
34438 assert( p!=0 )((void) (0));
34439#if 0
34440 /*
34441 ** This assert appears to trigger spuriously on certain
34442 ** versions of Windows, possibly due to _beginthreadex()
34443 ** and/or CreateThread() not fully setting their thread
34444 ** ID parameter before starting the thread.
34445 */
34446 assert( p->id==GetCurrentThreadId() )((void) (0));
34447#endif
34448 assert( p->xTask!=0 )((void) (0));
34449 p->pResult = p->xTask(p->pIn);
34450
34451 _endthreadex(0);
34452 return 0; /* NOT REACHED */
34453}
34454
34455/* Create a new thread */
34456SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
34457 SQLiteThread **ppThread, /* OUT: Write the thread object here */
34458 void *(*xTask)(void*), /* Routine to run in a separate thread */
34459 void *pIn /* Argument passed into xTask() */
34460){
34461 SQLiteThread *p;
34462
34463 assert( ppThread!=0 )((void) (0));
34464 assert( xTask!=0 )((void) (0));
34465 *ppThread = 0;
34466 p = sqlite3Malloc(sizeof(*p));
34467 if( p==0 ) return SQLITE_NOMEM_BKPT7;
34468 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
34469 ** function that returns SQLITE_ERROR when passed the argument 200, that
34470 ** forces worker threads to run sequentially and deterministically
34471 ** (via the sqlite3FaultSim() term of the conditional) for testing
34472 ** purposes. */
34473 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 || sqlite3FaultSim(200) ){
34474 memset(p, 0, sizeof(*p));
34475 }else{
34476 p->xTask = xTask;
34477 p->pIn = pIn;
34478 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
34479 if( p->tid==0 ){
34480 memset(p, 0, sizeof(*p));
34481 }
34482 }
34483 if( p->xTask==0 ){
34484 p->id = GetCurrentThreadId();
34485 p->pResult = xTask(pIn);
34486 }
34487 *ppThread = p;
34488 return SQLITE_OK0;
34489}
34490
34491SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
34492
34493/* Get the results of the thread */
34494SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
34495 DWORD rc;
34496 BOOL bRc;
34497
34498 assert( ppOut!=0 )((void) (0));
34499 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
34500 if( p->xTask==0 ){
34501 /* assert( p->id==GetCurrentThreadId() ); */
34502 rc = WAIT_OBJECT_0;
34503 assert( p->tid==0 )((void) (0));
34504 }else{
34505 assert( p->id!=0 && p->id!=GetCurrentThreadId() )((void) (0));
34506 rc = sqlite3Win32Wait((HANDLE)p->tid);
34507 assert( rc!=WAIT_IO_COMPLETION )((void) (0));
34508 bRc = CloseHandle((HANDLE)p->tid);
34509 assert( bRc )((void) (0));
34510 }
34511 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
34512 sqlite3_free(p);
34513 return (rc==WAIT_OBJECT_0) ? SQLITE_OK0 : SQLITE_ERROR1;
34514}
34515
34516#endif /* SQLITE_OS_WIN_THREADS */
34517/******************************** End Win32 Threads *************************/
34518
34519
34520/********************************* Single-Threaded **************************/
34521#ifndef SQLITE_THREADS_IMPLEMENTED1
34522/*
34523** This implementation does not actually create a new thread. It does the
34524** work of the thread in the main thread, when either the thread is created
34525** or when it is joined
34526*/
34527
34528/* A running thread */
34529struct SQLiteThread {
34530 void *(*xTask)(void*); /* The routine to run as a thread */
34531 void *pIn; /* Argument to xTask */
34532 void *pResult; /* Result of xTask */
34533};
34534
34535/* Create a new thread */
34536SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
34537 SQLiteThread **ppThread, /* OUT: Write the thread object here */
34538 void *(*xTask)(void*), /* Routine to run in a separate thread */
34539 void *pIn /* Argument passed into xTask() */
34540){
34541 SQLiteThread *p;
34542
34543 assert( ppThread!=0 )((void) (0));
34544 assert( xTask!=0 )((void) (0));
34545 *ppThread = 0;
34546 p = sqlite3Malloc(sizeof(*p));
34547 if( p==0 ) return SQLITE_NOMEM_BKPT7;
34548 if( (SQLITE_PTR_TO_INT(p)((int)(long int)(p))/17)&1 ){
34549 p->xTask = xTask;
34550 p->pIn = pIn;
34551 }else{
34552 p->xTask = 0;
34553 p->pResult = xTask(pIn);
34554 }
34555 *ppThread = p;
34556 return SQLITE_OK0;
34557}
34558
34559/* Get the results of the thread */
34560SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
34561
34562 assert( ppOut!=0 )((void) (0));
34563 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
34564 if( p->xTask ){
34565 *ppOut = p->xTask(p->pIn);
34566 }else{
34567 *ppOut = p->pResult;
34568 }
34569 sqlite3_free(p);
34570
34571#if defined(SQLITE_TEST)
34572 {
34573 void *pTstAlloc = sqlite3Malloc(10);
34574 if (!pTstAlloc) return SQLITE_NOMEM_BKPT7;
34575 sqlite3_free(pTstAlloc);
34576 }
34577#endif
34578
34579 return SQLITE_OK0;
34580}
34581
34582#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
34583/****************************** End Single-Threaded *************************/
34584#endif /* SQLITE_MAX_WORKER_THREADS>0 */
34585
34586/************** End of threads.c *********************************************/
34587/************** Begin file utf.c *********************************************/
34588/*
34589** 2004 April 13
34590**
34591** The author disclaims copyright to this source code. In place of
34592** a legal notice, here is a blessing:
34593**
34594** May you do good and not evil.
34595** May you find forgiveness for yourself and forgive others.
34596** May you share freely, never taking more than you give.
34597**
34598*************************************************************************
34599** This file contains routines used to translate between UTF-8,
34600** UTF-16, UTF-16BE, and UTF-16LE.
34601**
34602** Notes on UTF-8:
34603**
34604** Byte-0 Byte-1 Byte-2 Byte-3 Value
34605** 0xxxxxxx 00000000 00000000 0xxxxxxx
34606** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
34607** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
34608** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
34609**
34610**
34611** Notes on UTF-16: (with wwww+1==uuuuu)
34612**
34613** Word-0 Word-1 Value
34614** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
34615** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
34616**
34617**
34618** BOM or Byte Order Mark:
34619** 0xff 0xfe little-endian utf-16 follows
34620** 0xfe 0xff big-endian utf-16 follows
34621**
34622*/
34623/* #include "sqliteInt.h" */
34624/* #include <assert.h> */
34625/* #include "vdbeInt.h" */
34626
34627#if !defined(SQLITE_AMALGAMATION1) && SQLITE_BYTEORDER1234==0
34628/*
34629** The following constant value is used by the SQLITE_BIGENDIAN and
34630** SQLITE_LITTLEENDIAN macros.
34631*/
34632SQLITE_PRIVATEstatic const int sqlite3one = 1;
34633#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
34634
34635/*
34636** This lookup table is used to help decode the first byte of
34637** a multi-byte UTF8 character.
34638*/
34639static const unsigned char sqlite3Utf8Trans1[] = {
34640 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34641 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
34642 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
34643 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
34644 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34645 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
34646 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34647 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
34648};
34649
34650
34651#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); } }
{ \
34652 if( c<0x00080 ){ \
34653 *zOut++ = (u8)(c&0xFF); \
34654 } \
34655 else if( c<0x00800 ){ \
34656 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
34657 *zOut++ = 0x80 + (u8)(c & 0x3F); \
34658 } \
34659 else if( c<0x10000 ){ \
34660 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
34661 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
34662 *zOut++ = 0x80 + (u8)(c & 0x3F); \
34663 }else{ \
34664 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
34665 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
34666 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
34667 *zOut++ = 0x80 + (u8)(c & 0x3F); \
34668 } \
34669}
34670
34671#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)); } }
{ \
34672 if( c<=0xFFFF ){ \
34673 *zOut++ = (u8)(c&0x00FF); \
34674 *zOut++ = (u8)((c>>8)&0x00FF); \
34675 }else{ \
34676 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34677 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34678 *zOut++ = (u8)(c&0x00FF); \
34679 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
34680 } \
34681}
34682
34683#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); } }
{ \
34684 if( c<=0xFFFF ){ \
34685 *zOut++ = (u8)((c>>8)&0x00FF); \
34686 *zOut++ = (u8)(c&0x00FF); \
34687 }else{ \
34688 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34689 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34690 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
34691 *zOut++ = (u8)(c&0x00FF); \
34692 } \
34693}
34694
34695/*
34696** Translate a single UTF-8 character. Return the unicode value.
34697**
34698** During translation, assume that the byte that zTerm points
34699** is a 0x00.
34700**
34701** Write a pointer to the next unread byte back into *pzNext.
34702**
34703** Notes On Invalid UTF-8:
34704**
34705** * This routine never allows a 7-bit character (0x00 through 0x7f) to
34706** be encoded as a multi-byte character. Any multi-byte character that
34707** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
34708**
34709** * This routine never allows a UTF16 surrogate value to be encoded.
34710** If a multi-byte character attempts to encode a value between
34711** 0xd800 and 0xe000 then it is rendered as 0xfffd.
34712**
34713** * Bytes in the range of 0x80 through 0xbf which occur as the first
34714** byte of a character are interpreted as single-byte characters
34715** and rendered as themselves even though they are technically
34716** invalid characters.
34717**
34718** * This routine accepts over-length UTF8 encodings
34719** for unicode values 0x80 and greater. It does not change over-length
34720** encodings to 0xfffd as some systems recommend.
34721*/
34722#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; } }
\
34723 c = *(zIn++); \
34724 if( c>=0xc0 ){ \
34725 c = sqlite3Utf8Trans1[c-0xc0]; \
34726 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \
34727 c = (c<<6) + (0x3f & *(zIn++)); \
34728 } \
34729 if( c<0x80 \
34730 || (c&0xFFFFF800)==0xD800 \
34731 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
34732 }
34733SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(
34734 const unsigned char **pz /* Pointer to string from which to read char */
34735){
34736 unsigned int c;
34737
34738 /* Same as READ_UTF8() above but without the zTerm parameter.
34739 ** For this routine, we assume the UTF8 string is always zero-terminated.
34740 */
34741 c = *((*pz)++);
34742 if( c>=0xc0 ){
34743 c = sqlite3Utf8Trans1[c-0xc0];
34744 while( (*(*pz) & 0xc0)==0x80 ){
34745 c = (c<<6) + (0x3f & *((*pz)++));
34746 }
34747 if( c<0x80
34748 || (c&0xFFFFF800)==0xD800
34749 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
34750 }
34751 return c;
34752}
34753
34754/*
34755** Read a single UTF8 character out of buffer z[], but reading no
34756** more than n characters from the buffer. z[] is not zero-terminated.
34757**
34758** Return the number of bytes used to construct the character.
34759**
34760** Invalid UTF8 might generate a strange result. No effort is made
34761** to detect invalid UTF8.
34762**
34763** At most 4 bytes will be read out of z[]. The return value will always
34764** be between 1 and 4.
34765*/
34766SQLITE_PRIVATEstatic int sqlite3Utf8ReadLimited(
34767 const u8 *z,
34768 int n,
34769 u32 *piOut
34770){
34771 u32 c;
34772 int i = 1;
34773 assert( n>0 )((void) (0));
34774 c = z[0];
34775 if( c>=0xc0 ){
34776 c = sqlite3Utf8Trans1[c-0xc0];
34777 if( n>4 ) n = 4;
34778 while( i<n && (z[i] & 0xc0)==0x80 ){
34779 c = (c<<6) + (0x3f & z[i]);
34780 i++;
34781 }
34782 }
34783 *piOut = c;
34784 return i;
34785}
34786
34787
34788/*
34789** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
34790** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
34791*/
34792/* #define TRANSLATE_TRACE 1 */
34793
34794#ifndef SQLITE_OMIT_UTF16
34795/*
34796** This routine transforms the internal text encoding used by pMem to
34797** desiredEnc. It is an error if the string is already of the desired
34798** encoding, or if *pMem does not contain a string value.
34799*/
34800SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
34801 sqlite3_int64 len; /* Maximum length of output string in bytes */
34802 unsigned char *zOut; /* Output buffer */
34803 unsigned char *zIn; /* Input iterator */
34804 unsigned char *zTerm; /* End of input */
34805 unsigned char *z; /* Output iterator */
34806 unsigned int c;
34807
34808 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
34809 assert( pMem->flags&MEM_Str )((void) (0));
34810 assert( pMem->enc!=desiredEnc )((void) (0));
34811 assert( pMem->enc!=0 )((void) (0));
34812 assert( pMem->n>=0 )((void) (0));
34813
34814#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
34815 {
34816 StrAccum acc;
34817 char zBuf[1000];
34818 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
34819 sqlite3VdbeMemPrettyPrint(pMem, &acc);
34820 fprintf(stderrstderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc));
34821 }
34822#endif
34823
34824 /* If the translation is between UTF-16 little and big endian, then
34825 ** all that is required is to swap the byte order. This case is handled
34826 ** differently from the others.
34827 */
34828 if( pMem->enc!=SQLITE_UTF81 && desiredEnc!=SQLITE_UTF81 ){
34829 u8 temp;
34830 int rc;
34831 rc = sqlite3VdbeMemMakeWriteable(pMem);
34832 if( rc!=SQLITE_OK0 ){
34833 assert( rc==SQLITE_NOMEM )((void) (0));
34834 return SQLITE_NOMEM_BKPT7;
34835 }
34836 zIn = (u8*)pMem->z;
34837 zTerm = &zIn[pMem->n&~1];
34838 while( zIn<zTerm ){
34839 temp = *zIn;
34840 *zIn = *(zIn+1);
34841 zIn++;
34842 *zIn++ = temp;
34843 }
34844 pMem->enc = desiredEnc;
34845 goto translate_out;
34846 }
34847
34848 /* Set len to the maximum number of bytes required in the output buffer. */
34849 if( desiredEnc==SQLITE_UTF81 ){
34850 /* When converting from UTF-16, the maximum growth results from
34851 ** translating a 2-byte character to a 4-byte UTF-8 character.
34852 ** A single byte is required for the output string
34853 ** nul-terminator.
34854 */
34855 pMem->n &= ~1;
34856 len = 2 * (sqlite3_int64)pMem->n + 1;
34857 }else{
34858 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
34859 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
34860 ** character. Two bytes are required in the output buffer for the
34861 ** nul-terminator.
34862 */
34863 len = 2 * (sqlite3_int64)pMem->n + 2;
34864 }
34865
34866 /* Set zIn to point at the start of the input buffer and zTerm to point 1
34867 ** byte past the end.
34868 **
34869 ** Variable zOut is set to point at the output buffer, space obtained
34870 ** from sqlite3_malloc().
34871 */
34872 zIn = (u8*)pMem->z;
34873 zTerm = &zIn[pMem->n];
34874 zOut = sqlite3DbMallocRaw(pMem->db, len);
34875 if( !zOut ){
34876 return SQLITE_NOMEM_BKPT7;
34877 }
34878 z = zOut;
34879
34880 if( pMem->enc==SQLITE_UTF81 ){
34881 if( desiredEnc==SQLITE_UTF16LE2 ){
34882 /* UTF-8 -> UTF-16 Little-endian */
34883 while( zIn<zTerm ){
34884 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; } }
;
34885 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)); } }
;
34886 }
34887 }else{
34888 assert( desiredEnc==SQLITE_UTF16BE )((void) (0));
34889 /* UTF-8 -> UTF-16 Big-endian */
34890 while( zIn<zTerm ){
34891 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; } }
;
34892 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); } }
;
34893 }
34894 }
34895 pMem->n = (int)(z - zOut);
34896 *z++ = 0;
34897 }else{
34898 assert( desiredEnc==SQLITE_UTF8 )((void) (0));
34899 if( pMem->enc==SQLITE_UTF16LE2 ){
34900 /* UTF-16 Little-endian -> UTF-8 */
34901 while( zIn<zTerm ){
34902 c = *(zIn++);
34903 c += (*(zIn++))<<8;
34904 if( c>=0xd800 && c<0xe000 ){
34905#ifdef SQLITE_REPLACE_INVALID_UTF
34906 if( c>=0xdc00 || zIn>=zTerm ){
34907 c = 0xfffd;
34908 }else{
34909 int c2 = *(zIn++);
34910 c2 += (*(zIn++))<<8;
34911 if( c2<0xdc00 || c2>=0xe000 ){
34912 zIn -= 2;
34913 c = 0xfffd;
34914 }else{
34915 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
34916 }
34917 }
34918#else
34919 if( zIn<zTerm ){
34920 int c2 = (*zIn++);
34921 c2 += ((*zIn++)<<8);
34922 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
34923 }
34924#endif
34925 }
34926 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); } }
;
34927 }
34928 }else{
34929 /* UTF-16 Big-endian -> UTF-8 */
34930 while( zIn<zTerm ){
34931 c = (*(zIn++))<<8;
34932 c += *(zIn++);
34933 if( c>=0xd800 && c<0xe000 ){
34934#ifdef SQLITE_REPLACE_INVALID_UTF
34935 if( c>=0xdc00 || zIn>=zTerm ){
34936 c = 0xfffd;
34937 }else{
34938 int c2 = (*(zIn++))<<8;
34939 c2 += *(zIn++);
34940 if( c2<0xdc00 || c2>=0xe000 ){
34941 zIn -= 2;
34942 c = 0xfffd;
34943 }else{
34944 c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000;
34945 }
34946 }
34947#else
34948 if( zIn<zTerm ){
34949 int c2 = ((*zIn++)<<8);
34950 c2 += (*zIn++);
34951 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);
34952 }
34953#endif
34954 }
34955 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); } }
;
34956 }
34957 }
34958 pMem->n = (int)(z - zOut);
34959 }
34960 *z = 0;
34961 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len )((void) (0));
34962
34963 c = MEM_Str0x0002|MEM_Term0x0200|(pMem->flags&(MEM_AffMask0x003f|MEM_Subtype0x0800));
34964 sqlite3VdbeMemRelease(pMem);
34965 pMem->flags = c;
34966 pMem->enc = desiredEnc;
34967 pMem->z = (char*)zOut;
34968 pMem->zMalloc = pMem->z;
34969 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
34970
34971translate_out:
34972#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
34973 {
34974 StrAccum acc;
34975 char zBuf[1000];
34976 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
34977 sqlite3VdbeMemPrettyPrint(pMem, &acc);
34978 fprintf(stderrstderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc));
34979 }
34980#endif
34981 return SQLITE_OK0;
34982}
34983#endif /* SQLITE_OMIT_UTF16 */
34984
34985#ifndef SQLITE_OMIT_UTF16
34986/*
34987** This routine checks for a byte-order mark at the beginning of the
34988** UTF-16 string stored in *pMem. If one is present, it is removed and
34989** the encoding of the Mem adjusted. This routine does not do any
34990** byte-swapping, it just sets Mem.enc appropriately.
34991**
34992** The allocation (static, dynamic etc.) and encoding of the Mem may be
34993** changed by this function.
34994*/
34995SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem){
34996 int rc = SQLITE_OK0;
34997 u8 bom = 0;
34998
34999 assert( pMem->n>=0 )((void) (0));
35000 if( pMem->n>1 ){
35001 u8 b1 = *(u8 *)pMem->z;
35002 u8 b2 = *(((u8 *)pMem->z) + 1);
35003 if( b1==0xFE && b2==0xFF ){
35004 bom = SQLITE_UTF16BE3;
35005 }
35006 if( b1==0xFF && b2==0xFE ){
35007 bom = SQLITE_UTF16LE2;
35008 }
35009 }
35010
35011 if( bom ){
35012 rc = sqlite3VdbeMemMakeWriteable(pMem);
35013 if( rc==SQLITE_OK0 ){
35014 pMem->n -= 2;
35015 memmove(pMem->z, &pMem->z[2], pMem->n);
35016 pMem->z[pMem->n] = '\0';
35017 pMem->z[pMem->n+1] = '\0';
35018 pMem->flags |= MEM_Term0x0200;
35019 pMem->enc = bom;
35020 }
35021 }
35022 return rc;
35023}
35024#endif /* SQLITE_OMIT_UTF16 */
35025
35026/*
35027** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
35028** return the number of unicode characters in pZ up to (but not including)
35029** the first 0x00 byte. If nByte is not less than zero, return the
35030** number of unicode characters in the first nByte of pZ (or up to
35031** the first 0x00, whichever comes first).
35032*/
35033SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *zIn, int nByte){
35034 int r = 0;
35035 const u8 *z = (const u8*)zIn;
35036 const u8 *zTerm;
35037 if( nByte>=0 ){
35038 zTerm = &z[nByte];
35039 }else{
35040 zTerm = (const u8*)(-1);
35041 }
35042 assert( z<=zTerm )((void) (0));
35043 while( *z!=0 && z<zTerm ){
35044 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
35045 r++;
35046 }
35047 return r;
35048}
35049
35050/* This test function is not currently used by the automated test-suite.
35051** Hence it is only available in debug builds.
35052*/
35053#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
35054/*
35055** Translate UTF-8 to UTF-8.
35056**
35057** This has the effect of making sure that the string is well-formed
35058** UTF-8. Miscoded characters are removed.
35059**
35060** The translation is done in-place and aborted if the output
35061** overruns the input.
35062*/
35063SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char *zIn){
35064 unsigned char *zOut = zIn;
35065 unsigned char *zStart = zIn;
35066 u32 c;
35067
35068 while( zIn[0] && zOut<=zIn ){
35069 c = sqlite3Utf8Read((const u8**)&zIn);
35070 if( c!=0xfffd ){
35071 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); } }
;
35072 }
35073 }
35074 *zOut = 0;
35075 return (int)(zOut - zStart);
35076}
35077#endif
35078
35079#ifndef SQLITE_OMIT_UTF16
35080/*
35081** Convert a UTF-16 string in the native encoding into a UTF-8 string.
35082** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
35083** be freed by the calling function.
35084**
35085** NULL is returned if there is an allocation error.
35086*/
35087SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
35088 Mem m;
35089 memset(&m, 0, sizeof(m));
35090 m.db = db;
35091 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
35092 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF81);
35093 if( db->mallocFailed ){
35094 sqlite3VdbeMemRelease(&m);
35095 m.z = 0;
35096 }
35097 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed )((void) (0));
35098 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed )((void) (0));
35099 assert( m.z || db->mallocFailed )((void) (0));
35100 return m.z;
35101}
35102
35103/*
35104** zIn is a UTF-16 encoded unicode string at least nByte bytes long.
35105** Return the number of bytes in the first nChar unicode characters
35106** in pZ. nChar must be non-negative. Surrogate pairs count as a single
35107** character.
35108*/
35109SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){
35110 int c;
35111 unsigned char const *z = zIn;
35112 unsigned char const *zEnd = &z[nByte-1];
35113 int n = 0;
35114
35115 if( SQLITE_UTF16NATIVE2==SQLITE_UTF16LE2 ) z++;
35116 while( n<nChar && ALWAYS(z<=zEnd)(z<=zEnd) ){
35117 c = z[0];
35118 z += 2;
35119 if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
35120 n++;
35121 }
35122 return (int)(z-(unsigned char const *)zIn)
35123 - (SQLITE_UTF16NATIVE2==SQLITE_UTF16LE2);
35124}
35125
35126#if defined(SQLITE_TEST)
35127/*
35128** This routine is called from the TCL test function "translate_selftest".
35129** It checks that the primitives for serializing and deserializing
35130** characters in each encoding are inverses of each other.
35131*/
35132SQLITE_PRIVATEstatic void sqlite3UtfSelfTest(void){
35133 unsigned int i, t;
35134 unsigned char zBuf[20];
35135 unsigned char *z;
35136 int n;
35137 unsigned int c;
35138
35139 for(i=0; i<0x00110000; i++){
35140 z = zBuf;
35141 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); } }
;
35142 n = (int)(z-zBuf);
35143 assert( n>0 && n<=4 )((void) (0));
35144 z[0] = 0;
35145 z = zBuf;
35146 c = sqlite3Utf8Read((const u8**)&z);
35147 t = i;
35148 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
35149 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
35150 assert( c==t )((void) (0));
35151 assert( (z-zBuf)==n )((void) (0));
35152 }
35153}
35154#endif /* SQLITE_TEST */
35155#endif /* SQLITE_OMIT_UTF16 */
35156
35157/************** End of utf.c *************************************************/
35158/************** Begin file util.c ********************************************/
35159/*
35160** 2001 September 15
35161**
35162** The author disclaims copyright to this source code. In place of
35163** a legal notice, here is a blessing:
35164**
35165** May you do good and not evil.
35166** May you find forgiveness for yourself and forgive others.
35167** May you share freely, never taking more than you give.
35168**
35169*************************************************************************
35170** Utility functions used throughout sqlite.
35171**
35172** This file contains functions for allocating memory, comparing
35173** strings, and stuff like that.
35174**
35175*/
35176/* #include "sqliteInt.h" */
35177/* #include <stdarg.h> */
35178#ifndef SQLITE_OMIT_FLOATING_POINT
35179#include <math.h>
35180#endif
35181
35182/*
35183** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
35184** or to bypass normal error detection during testing in order to let
35185** execute proceed further downstream.
35186**
35187** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
35188** sqlite3FaultSim() function only returns non-zero during testing.
35189**
35190** During testing, if the test harness has set a fault-sim callback using
35191** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
35192** each call to sqlite3FaultSim() is relayed to that application-supplied
35193** callback and the integer return value form the application-supplied
35194** callback is returned by sqlite3FaultSim().
35195**
35196** The integer argument to sqlite3FaultSim() is a code to identify which
35197** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
35198** should have a unique code. To prevent legacy testing applications from
35199** breaking, the codes should not be changed or reused.
35200*/
35201#ifndef SQLITE_UNTESTABLE
35202SQLITE_PRIVATEstatic int sqlite3FaultSim(int iTest){
35203 int (*xCallback)(int) = sqlite3GlobalConfigsqlite3Config.xTestCallback;
35204 return xCallback ? xCallback(iTest) : SQLITE_OK0;
35205}
35206#endif
35207
35208#ifndef SQLITE_OMIT_FLOATING_POINT
35209/*
35210** Return true if the floating point value is Not a Number (NaN).
35211**
35212** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
35213** Otherwise, we have our own implementation that works on most systems.
35214*/
35215SQLITE_PRIVATEstatic int sqlite3IsNaN(double x){
35216 int rc; /* The value return */
35217#if !SQLITE_HAVE_ISNAN1 && !HAVE_ISNAN
35218 u64 y;
35219 memcpy(&y,&x,sizeof(y));
35220 rc = IsNaN(y)(((y)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((y)&((((u64)1)<<52)-1))!=0)
;
35221#else
35222 rc = isnan(x)__builtin_isnan (x);
35223#endif /* HAVE_ISNAN */
35224 testcase( rc );
35225 return rc;
35226}
35227#endif /* SQLITE_OMIT_FLOATING_POINT */
35228
35229#ifndef SQLITE_OMIT_FLOATING_POINT
35230/*
35231** Return true if the floating point value is NaN or +Inf or -Inf.
35232*/
35233SQLITE_PRIVATEstatic int sqlite3IsOverflow(double x){
35234 int rc; /* The value return */
35235 u64 y;
35236 memcpy(&y,&x,sizeof(y));
35237 rc = IsOvfl(y)(((y)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
)
;
35238 return rc;
35239}
35240#endif /* SQLITE_OMIT_FLOATING_POINT */
35241
35242/*
35243** Compute a string length that is limited to what can be stored in
35244** lower 30 bits of a 32-bit signed integer.
35245**
35246** The value returned will never be negative. Nor will it ever be greater
35247** than the actual length of the string. For very long strings (greater
35248** than 1GiB) the value returned might be less than the true string length.
35249*/
35250SQLITE_PRIVATEstatic int sqlite3Strlen30(const char *z){
35251 if( z==0 ) return 0;
35252 return 0x3fffffff & (int)strlen(z);
35253}
35254
35255/*
35256** Return the declared type of a column. Or return zDflt if the column
35257** has no declared type.
35258**
35259** The column type is an extra string stored after the zero-terminator on
35260** the column name if and only if the COLFLAG_HASTYPE flag is set.
35261*/
35262SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column *pCol, char *zDflt){
35263 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
35264 return pCol->zCnName + strlen(pCol->zCnName) + 1;
35265 }else if( pCol->eCType ){
35266 assert( pCol->eCType<=SQLITE_N_STDTYPE )((void) (0));
35267 return (char*)sqlite3StdType[pCol->eCType-1];
35268 }else{
35269 return zDflt;
35270 }
35271}
35272
35273/*
35274** Helper function for sqlite3Error() - called rarely. Broken out into
35275** a separate routine to avoid unnecessary register saves on entry to
35276** sqlite3Error().
35277*/
35278static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ErrorFinish(sqlite3 *db, int err_code){
35279 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
35280 sqlite3SystemError(db, err_code);
35281}
35282
35283/*
35284** Set the current error code to err_code and clear any prior error message.
35285** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
35286** that would be appropriate.
35287*/
35288SQLITE_PRIVATEstatic void sqlite3Error(sqlite3 *db, int err_code){
35289 assert( db!=0 )((void) (0));
35290 db->errCode = err_code;
35291 if( err_code || db->pErr ){
35292 sqlite3ErrorFinish(db, err_code);
35293 }else{
35294 db->errByteOffset = -1;
35295 }
35296}
35297
35298/*
35299** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
35300** and error message.
35301*/
35302SQLITE_PRIVATEstatic void sqlite3ErrorClear(sqlite3 *db){
35303 assert( db!=0 )((void) (0));
35304 db->errCode = SQLITE_OK0;
35305 db->errByteOffset = -1;
35306 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
35307}
35308
35309/*
35310** Load the sqlite3.iSysErrno field if that is an appropriate thing
35311** to do based on the SQLite error code in rc.
35312*/
35313SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3 *db, int rc){
35314 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) return;
35315#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
35316 if( rc==SQLITE_IOERR_IN_PAGE(10 | (34<<8)) ){
35317 int ii;
35318 int iErr;
35319 sqlite3BtreeEnterAll(db);
35320 for(ii=0; ii<db->nDb; ii++){
35321 if( db->aDb[ii].pBt ){
35322 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt));
35323 if( iErr ){
35324 db->iSysErrno = iErr;
35325 }
35326 }
35327 }
35328 sqlite3BtreeLeaveAll(db);
35329 return;
35330 }
35331#endif
35332 rc &= 0xff;
35333 if( rc==SQLITE_CANTOPEN14 || rc==SQLITE_IOERR10 ){
35334 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
35335 }
35336}
35337
35338/*
35339** Set the most recent error code and error string for the sqlite
35340** handle "db". The error code is set to "err_code".
35341**
35342** If it is not NULL, string zFormat specifies the format of the
35343** error string. zFormat and any string tokens that follow it are
35344** assumed to be encoded in UTF-8.
35345**
35346** To clear the most recent error for sqlite handle "db", sqlite3Error
35347** should be called with err_code set to SQLITE_OK and zFormat set
35348** to NULL.
35349*/
35350SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
35351 assert( db!=0 )((void) (0));
35352 db->errCode = err_code;
35353 sqlite3SystemError(db, err_code);
35354 if( zFormat==0 ){
35355 sqlite3Error(db, err_code);
35356 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
35357 char *z;
35358 va_list ap;
35359 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
35360 z = sqlite3VMPrintf(db, zFormat, ap);
35361 va_end(ap)__builtin_va_end(ap);
35362 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
35363 }
35364}
35365
35366/*
35367** Check for interrupts and invoke progress callback.
35368*/
35369SQLITE_PRIVATEstatic void sqlite3ProgressCheck(Parse *p){
35370 sqlite3 *db = p->db;
35371 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
35372 p->nErr++;
35373 p->rc = SQLITE_INTERRUPT9;
35374 }
35375#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
35376 if( db->xProgress ){
35377 if( p->rc==SQLITE_INTERRUPT9 ){
35378 p->nProgressSteps = 0;
35379 }else if( (++p->nProgressSteps)>=db->nProgressOps ){
35380 if( db->xProgress(db->pProgressArg) ){
35381 p->nErr++;
35382 p->rc = SQLITE_INTERRUPT9;
35383 }
35384 p->nProgressSteps = 0;
35385 }
35386 }
35387#endif
35388}
35389
35390/*
35391** Add an error message to pParse->zErrMsg and increment pParse->nErr.
35392**
35393** This function should be used to report any error that occurs while
35394** compiling an SQL statement (i.e. within sqlite3_prepare()). The
35395** last thing the sqlite3_prepare() function does is copy the error
35396** stored by this function into the database handle using sqlite3Error().
35397** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
35398** during statement execution (sqlite3_step() etc.).
35399*/
35400SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
35401 char *zMsg;
35402 va_list ap;
35403 sqlite3 *db = pParse->db;
35404 assert( db!=0 )((void) (0));
35405 assert( db->pParse==pParse || db->pParse->pToplevel==pParse )((void) (0));
35406 db->errByteOffset = -2;
35407 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
35408 zMsg = sqlite3VMPrintf(db, zFormat, ap);
35409 va_end(ap)__builtin_va_end(ap);
35410 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
35411 if( db->suppressErr ){
35412 sqlite3DbFree(db, zMsg);
35413 if( db->mallocFailed ){
35414 pParse->nErr++;
35415 pParse->rc = SQLITE_NOMEM7;
35416 }
35417 }else{
35418 pParse->nErr++;
35419 sqlite3DbFree(db, pParse->zErrMsg);
35420 pParse->zErrMsg = zMsg;
35421 pParse->rc = SQLITE_ERROR1;
35422 pParse->pWith = 0;
35423 }
35424}
35425
35426/*
35427** If database connection db is currently parsing SQL, then transfer
35428** error code errCode to that parser if the parser has not already
35429** encountered some other kind of error.
35430*/
35431SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3 *db, int errCode){
35432 Parse *pParse;
35433 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
35434 pParse->rc = errCode;
35435 pParse->nErr++;
35436 return errCode;
35437}
35438
35439/*
35440** Convert an SQL-style quoted string into a normal string by removing
35441** the quote characters. The conversion is done in-place. If the
35442** input does not begin with a quote character, then this routine
35443** is a no-op.
35444**
35445** The input string must be zero-terminated. A new zero-terminator
35446** is added to the dequoted string.
35447**
35448** The return value is -1 if no dequoting occurs or the length of the
35449** dequoted string, exclusive of the zero terminator, if dequoting does
35450** occur.
35451**
35452** 2002-02-14: This routine is extended to remove MS-Access style
35453** brackets from around identifiers. For example: "[a-b-c]" becomes
35454** "a-b-c".
35455*/
35456SQLITE_PRIVATEstatic void sqlite3Dequote(char *z){
35457 char quote;
35458 int i, j;
35459 if( z==0 ) return;
35460 quote = z[0];
35461 if( !sqlite3Isquote(quote)(sqlite3CtypeMap[(unsigned char)(quote)]&0x80) ) return;
35462 if( quote=='[' ) quote = ']';
35463 for(i=1, j=0;; i++){
35464 assert( z[i] )((void) (0));
35465 if( z[i]==quote ){
35466 if( z[i+1]==quote ){
35467 z[j++] = quote;
35468 i++;
35469 }else{
35470 break;
35471 }
35472 }else{
35473 z[j++] = z[i];
35474 }
35475 }
35476 z[j] = 0;
35477}
35478SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr *p){
35479 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
35480 assert( sqlite3Isquote(p->u.zToken[0]) )((void) (0));
35481 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted0x4000000|EP_DblQuoted0x000080 : EP_Quoted0x4000000;
35482 sqlite3Dequote(p->u.zToken);
35483}
35484
35485/*
35486** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
35487** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those
35488** that contain '_' characters that must be removed before further processing.
35489*/
35490SQLITE_PRIVATEstatic void sqlite3DequoteNumber(Parse *pParse, Expr *p){
35491 assert( p!=0 || pParse->db->mallocFailed )((void) (0));
35492 if( p ){
35493 const char *pIn = p->u.zToken;
35494 char *pOut = p->u.zToken;
35495 int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X'));
35496 int iValue;
35497 assert( p->op==TK_QNUMBER )((void) (0));
35498 p->op = TK_INTEGER156;
35499 do {
35500 if( *pIn!=SQLITE_DIGIT_SEPARATOR'_' ){
35501 *pOut++ = *pIn;
35502 if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT154;
35503 }else{
35504 if( (bHex==0 && (!sqlite3Isdigit(pIn[-1])(sqlite3CtypeMap[(unsigned char)(pIn[-1])]&0x04) || !sqlite3Isdigit(pIn[1])(sqlite3CtypeMap[(unsigned char)(pIn[1])]&0x04)))
35505 || (bHex==1 && (!sqlite3Isxdigit(pIn[-1])(sqlite3CtypeMap[(unsigned char)(pIn[-1])]&0x08) || !sqlite3Isxdigit(pIn[1])(sqlite3CtypeMap[(unsigned char)(pIn[1])]&0x08)))
35506 ){
35507 sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken);
35508 }
35509 }
35510 }while( *pIn++ );
35511 if( bHex ) p->op = TK_INTEGER156;
35512
35513 /* tag-20240227-a: If after dequoting, the number is an integer that
35514 ** fits in 32 bits, then it must be converted into EP_IntValue. Other
35515 ** parts of the code expect this. See also tag-20240227-b. */
35516 if( p->op==TK_INTEGER156 && sqlite3GetInt32(p->u.zToken, &iValue) ){
35517 p->u.iValue = iValue;
35518 p->flags |= EP_IntValue0x000800;
35519 }
35520 }
35521}
35522
35523/*
35524** If the input token p is quoted, try to adjust the token to remove
35525** the quotes. This is not always possible:
35526**
35527** "abc" -> abc
35528** "ab""cd" -> (not possible because of the interior "")
35529**
35530** Remove the quotes if possible. This is a optimization. The overall
35531** system should still return the correct answer even if this routine
35532** is always a no-op.
35533*/
35534SQLITE_PRIVATEstatic void sqlite3DequoteToken(Token *p){
35535 unsigned int i;
35536 if( p->n<2 ) return;
35537 if( !sqlite3Isquote(p->z[0])(sqlite3CtypeMap[(unsigned char)(p->z[0])]&0x80) ) return;
35538 for(i=1; i<p->n-1; i++){
35539 if( sqlite3Isquote(p->z[i])(sqlite3CtypeMap[(unsigned char)(p->z[i])]&0x80) ) return;
35540 }
35541 p->n -= 2;
35542 p->z++;
35543}
35544
35545/*
35546** Generate a Token object from a string
35547*/
35548SQLITE_PRIVATEstatic void sqlite3TokenInit(Token *p, char *z){
35549 p->z = z;
35550 p->n = sqlite3Strlen30(z);
35551}
35552
35553/* Convenient short-hand */
35554#define UpperToLowersqlite3UpperToLower sqlite3UpperToLower
35555
35556/*
35557** Some systems have stricmp(). Others have strcasecmp(). Because
35558** there is no consistency, we will define our own.
35559**
35560** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
35561** sqlite3_strnicmp() APIs allow applications and extensions to compare
35562** the contents of two buffers containing UTF-8 strings in a
35563** case-independent fashion, using the same definition of "case
35564** independence" that SQLite uses internally when comparing identifiers.
35565*/
35566SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
35567 if( zLeft==0 ){
35568 return zRight ? -1 : 0;
35569 }else if( zRight==0 ){
35570 return 1;
35571 }
35572 return sqlite3StrICmp(zLeft, zRight);
35573}
35574SQLITE_PRIVATEstatic int sqlite3StrICmp(const char *zLeft, const char *zRight){
35575 unsigned char *a, *b;
35576 int c, x;
35577 a = (unsigned char *)zLeft;
35578 b = (unsigned char *)zRight;
35579 for(;;){
35580 c = *a;
35581 x = *b;
35582 if( c==x ){
35583 if( c==0 ) break;
35584 }else{
35585 c = (int)UpperToLowersqlite3UpperToLower[c] - (int)UpperToLowersqlite3UpperToLower[x];
35586 if( c ) break;
35587 }
35588 a++;
35589 b++;
35590 }
35591 return c;
35592}
35593SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
35594 register unsigned char *a, *b;
35595 if( zLeft==0 ){
35596 return zRight ? -1 : 0;
35597 }else if( zRight==0 ){
35598 return 1;
35599 }
35600 a = (unsigned char *)zLeft;
35601 b = (unsigned char *)zRight;
35602 while( N-- > 0 && *a!=0 && UpperToLowersqlite3UpperToLower[*a]==UpperToLowersqlite3UpperToLower[*b]){ a++; b++; }
35603 return N<0 ? 0 : UpperToLowersqlite3UpperToLower[*a] - UpperToLowersqlite3UpperToLower[*b];
35604}
35605
35606/*
35607** Compute an 8-bit hash on a string that is insensitive to case differences
35608*/
35609SQLITE_PRIVATEstatic u8 sqlite3StrIHash(const char *z){
35610 u8 h = 0;
35611 if( z==0 ) return 0;
35612 while( z[0] ){
35613 h += UpperToLowersqlite3UpperToLower[(unsigned char)z[0]];
35614 z++;
35615 }
35616 return h;
35617}
35618
35619/* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
35620**
35621** Reference:
35622** T. J. Dekker, "A Floating-Point Technique for Extending the
35623** Available Precision". 1971-07-26.
35624*/
35625static void dekkerMul2(volatile double *x, double y, double yy){
35626 /*
35627 ** The "volatile" keywords on parameter x[] and on local variables
35628 ** below are needed force intermediate results to be truncated to
35629 ** binary64 rather than be carried around in an extended-precision
35630 ** format. The truncation is necessary for the Dekker algorithm to
35631 ** work. Intel x86 floating point might omit the truncation without
35632 ** the use of volatile.
35633 */
35634 volatile double tx, ty, p, q, c, cc;
35635 double hx, hy;
35636 u64 m;
35637 memcpy(&m, (void*)&x[0], 8);
35638 m &= 0xfffffffffc000000LL;
35639 memcpy(&hx, &m, 8);
35640 tx = x[0] - hx;
35641 memcpy(&m, &y, 8);
35642 m &= 0xfffffffffc000000LL;
35643 memcpy(&hy, &m, 8);
35644 ty = y - hy;
35645 p = hx*hy;
35646 q = hx*ty + tx*hy;
35647 c = p+q;
35648 cc = p - c + q + tx*ty;
35649 cc = x[0]*yy + x[1]*y + cc;
35650 x[0] = c + cc;
35651 x[1] = c - x[0];
35652 x[1] += cc;
35653}
35654
35655/*
35656** The string z[] is an text representation of a real number.
35657** Convert this string to a double and write it into *pResult.
35658**
35659** The string z[] is length bytes in length (bytes, not characters) and
35660** uses the encoding enc. The string is not necessarily zero-terminated.
35661**
35662** Return TRUE if the result is a valid real number (or integer) and FALSE
35663** if the string is empty or contains extraneous text. More specifically
35664** return
35665** 1 => The input string is a pure integer
35666** 2 or more => The input has a decimal point or eNNN clause
35667** 0 or less => The input string is not a valid number
35668** -1 => Not a valid number, but has a valid prefix which
35669** includes a decimal point and/or an eNNN clause
35670**
35671** Valid numbers are in one of these formats:
35672**
35673** [+-]digits[E[+-]digits]
35674** [+-]digits.[digits][E[+-]digits]
35675** [+-].digits[E[+-]digits]
35676**
35677** Leading and trailing whitespace is ignored for the purpose of determining
35678** validity.
35679**
35680** If some prefix of the input string is a valid number, this routine
35681** returns FALSE but it still converts the prefix and writes the result
35682** into *pResult.
35683*/
35684#if defined(_MSC_VER)
35685#pragma warning(disable : 4756)
35686#endif
35687SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
35688#ifndef SQLITE_OMIT_FLOATING_POINT
35689 int incr;
35690 const char *zEnd;
35691 /* sign * significand * (10 ^ (esign * exponent)) */
35692 int sign = 1; /* sign of significand */
35693 u64 s = 0; /* significand */
35694 int d = 0; /* adjust exponent for shifting decimal point */
35695 int esign = 1; /* sign of exponent */
35696 int e = 0; /* exponent */
35697 int eValid = 1; /* True exponent is either not used or is well-formed */
35698 int nDigit = 0; /* Number of digits processed */
35699 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
35700 double rr[2];
35701 u64 s2;
35702
35703 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
35704 *pResult = 0.0; /* Default return value, in case of an error */
35705 if( length==0 ) return 0;
35706
35707 if( enc==SQLITE_UTF81 ){
35708 incr = 1;
35709 zEnd = z + length;
35710 }else{
35711 int i;
35712 incr = 2;
35713 length &= ~1;
35714 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
35715 testcase( enc==SQLITE_UTF16LE );
35716 testcase( enc==SQLITE_UTF16BE );
35717 for(i=3-enc; i<length && z[i]==0; i+=2){}
35718 if( i<length ) eType = -100;
35719 zEnd = &z[i^1];
35720 z += (enc&1);
35721 }
35722
35723 /* skip leading spaces */
35724 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
35725 if( z>=zEnd ) return 0;
35726
35727 /* get sign of significand */
35728 if( *z=='-' ){
35729 sign = -1;
35730 z+=incr;
35731 }else if( *z=='+' ){
35732 z+=incr;
35733 }
35734
35735 /* copy max significant digits to significand */
35736 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
35737 s = s*10 + (*z - '0');
35738 z+=incr; nDigit++;
35739 if( s>=((LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32))-9)/10) ){
35740 /* skip non-significant significand digits
35741 ** (increase exponent by d to shift decimal left) */
35742 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){ z+=incr; d++; }
35743 }
35744 }
35745 if( z>=zEnd ) goto do_atof_calc;
35746
35747 /* if decimal point is present */
35748 if( *z=='.' ){
35749 z+=incr;
35750 eType++;
35751 /* copy digits from after decimal to significand
35752 ** (decrease exponent by d to shift decimal right) */
35753 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
35754 if( s<((LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32))-9)/10) ){
35755 s = s*10 + (*z - '0');
35756 d--;
35757 nDigit++;
35758 }
35759 z+=incr;
35760 }
35761 }
35762 if( z>=zEnd ) goto do_atof_calc;
35763
35764 /* if exponent is present */
35765 if( *z=='e' || *z=='E' ){
35766 z+=incr;
35767 eValid = 0;
35768 eType++;
35769
35770 /* This branch is needed to avoid a (harmless) buffer overread. The
35771 ** special comment alerts the mutation tester that the correct answer
35772 ** is obtained even if the branch is omitted */
35773 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
35774
35775 /* get sign of exponent */
35776 if( *z=='-' ){
35777 esign = -1;
35778 z+=incr;
35779 }else if( *z=='+' ){
35780 z+=incr;
35781 }
35782 /* copy digits to exponent */
35783 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
35784 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
35785 z+=incr;
35786 eValid = 1;
35787 }
35788 }
35789
35790 /* skip trailing spaces */
35791 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
35792
35793do_atof_calc:
35794 /* Zero is a special case */
35795 if( s==0 ){
35796 *pResult = sign<0 ? -0.0 : +0.0;
35797 goto atof_return;
35798 }
35799
35800 /* adjust exponent by d, and update sign */
35801 e = (e*esign) + d;
35802
35803 /* Try to adjust the exponent to make it smaller */
35804 while( e>0 && s<(LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32))/10) ){
35805 s *= 10;
35806 e--;
35807 }
35808 while( e<0 && (s%10)==0 ){
35809 s /= 10;
35810 e++;
35811 }
35812
35813 rr[0] = (double)s;
35814 s2 = (u64)rr[0];
35815#if defined(_MSC_VER) && _MSC_VER<1700
35816 if( s2==0x8000000000000000LL ){ s2 = 2*(u64)(0.5*rr[0]); }
35817#endif
35818 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
35819 if( e>0 ){
35820 while( e>=100 ){
35821 e -= 100;
35822 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
35823 }
35824 while( e>=10 ){
35825 e -= 10;
35826 dekkerMul2(rr, 1.0e+10, 0.0);
35827 }
35828 while( e>=1 ){
35829 e -= 1;
35830 dekkerMul2(rr, 1.0e+01, 0.0);
35831 }
35832 }else{
35833 while( e<=-100 ){
35834 e += 100;
35835 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
35836 }
35837 while( e<=-10 ){
35838 e += 10;
35839 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
35840 }
35841 while( e<=-1 ){
35842 e += 1;
35843 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
35844 }
35845 }
35846 *pResult = rr[0]+rr[1];
35847 if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300;
35848 if( sign<0 ) *pResult = -*pResult;
35849 assert( !sqlite3IsNaN(*pResult) )((void) (0));
35850
35851atof_return:
35852 /* return true if number and no extra non-whitespace characters after */
35853 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
35854 return eType;
35855 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
35856 return -1;
35857 }else{
35858 return 0;
35859 }
35860#else
35861 return !sqlite3Atoi64(z, pResult, length, enc);
35862#endif /* SQLITE_OMIT_FLOATING_POINT */
35863}
35864#if defined(_MSC_VER)
35865#pragma warning(default : 4756)
35866#endif
35867
35868/*
35869** Render an signed 64-bit integer as text. Store the result in zOut[] and
35870** return the length of the string that was stored, in bytes. The value
35871** returned does not include the zero terminator at the end of the output
35872** string.
35873**
35874** The caller must ensure that zOut[] is at least 21 bytes in size.
35875*/
35876SQLITE_PRIVATEstatic int sqlite3Int64ToText(i64 v, char *zOut){
35877 int i;
35878 u64 x;
35879 char zTemp[22];
35880 if( v<0 ){
35881 x = (v==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))) ? ((u64)1)<<63 : (u64)-v;
35882 }else{
35883 x = v;
35884 }
35885 i = sizeof(zTemp)-2;
35886 zTemp[sizeof(zTemp)-1] = 0;
35887 while( 1 /*exit-by-break*/ ){
35888 zTemp[i] = (x%10) + '0';
35889 x = x/10;
35890 if( x==0 ) break;
35891 i--;
35892 };
35893 if( v<0 ) zTemp[--i] = '-';
35894 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i);
35895 return sizeof(zTemp)-1-i;
35896}
35897
35898/*
35899** Compare the 19-character string zNum against the text representation
35900** value 2^63: 9223372036854775808. Return negative, zero, or positive
35901** if zNum is less than, equal to, or greater than the string.
35902** Note that zNum must contain exactly 19 characters.
35903**
35904** Unlike memcmp() this routine is guaranteed to return the difference
35905** in the values of the last digit if the only difference is in the
35906** last digit. So, for example,
35907**
35908** compare2pow63("9223372036854775800", 1)
35909**
35910** will return -8.
35911*/
35912static int compare2pow63(const char *zNum, int incr){
35913 int c = 0;
35914 int i;
35915 /* 012345678901234567 */
35916 const char *pow63 = "922337203685477580";
35917 for(i=0; c==0 && i<18; i++){
35918 c = (zNum[i*incr]-pow63[i])*10;
35919 }
35920 if( c==0 ){
35921 c = zNum[18*incr] - '8';
35922 testcase( c==(-1) );
35923 testcase( c==0 );
35924 testcase( c==(+1) );
35925 }
35926 return c;
35927}
35928
35929/*
35930** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
35931** routine does *not* accept hexadecimal notation.
35932**
35933** Returns:
35934**
35935** -1 Not even a prefix of the input text looks like an integer
35936** 0 Successful transformation. Fits in a 64-bit signed integer.
35937** 1 Excess non-space text after the integer value
35938** 2 Integer too large for a 64-bit signed integer or is malformed
35939** 3 Special case of 9223372036854775808
35940**
35941** length is the number of bytes in the string (bytes, not characters).
35942** The string is not necessarily zero-terminated. The encoding is
35943** given by enc.
35944*/
35945SQLITE_PRIVATEstatic int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
35946 int incr;
35947 u64 u = 0;
35948 int neg = 0; /* assume positive */
35949 int i;
35950 int c = 0;
35951 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
35952 int rc; /* Baseline return code */
35953 const char *zStart;
35954 const char *zEnd = zNum + length;
35955 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
35956 if( enc==SQLITE_UTF81 ){
35957 incr = 1;
35958 }else{
35959 incr = 2;
35960 length &= ~1;
35961 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
35962 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
35963 nonNum = i<length;
35964 zEnd = &zNum[i^1];
35965 zNum += (enc&1);
35966 }
35967 while( zNum<zEnd && sqlite3Isspace(*zNum)(sqlite3CtypeMap[(unsigned char)(*zNum)]&0x01) ) zNum+=incr;
35968 if( zNum<zEnd ){
35969 if( *zNum=='-' ){
35970 neg = 1;
35971 zNum+=incr;
35972 }else if( *zNum=='+' ){
35973 zNum+=incr;
35974 }
35975 }
35976 zStart = zNum;
35977 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
35978 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
35979 u = u*10 + c - '0';
35980 }
35981 testcase( i==18*incr );
35982 testcase( i==19*incr );
35983 testcase( i==20*incr );
35984 if( u>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
35985 /* This test and assignment is needed only to suppress UB warnings
35986 ** from clang and -fsanitize=undefined. This test and assignment make
35987 ** the code a little larger and slower, and no harm comes from omitting
35988 ** them, but we must appease the undefined-behavior pharisees. */
35989 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
35990 }else if( neg ){
35991 *pNum = -(i64)u;
35992 }else{
35993 *pNum = (i64)u;
35994 }
35995 rc = 0;
35996 if( i==0 && zStart==zNum ){ /* No digits */
35997 rc = -1;
35998 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
35999 rc = 1;
36000 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
36001 int jj = i;
36002 do{
36003 if( !sqlite3Isspace(zNum[jj])(sqlite3CtypeMap[(unsigned char)(zNum[jj])]&0x01) ){
36004 rc = 1; /* Extra non-space text after the integer */
36005 break;
36006 }
36007 jj += incr;
36008 }while( &zNum[jj]<zEnd );
36009 }
36010 if( i<19*incr ){
36011 /* Less than 19 digits, so we know that it fits in 64 bits */
36012 assert( u<=LARGEST_INT64 )((void) (0));
36013 return rc;
36014 }else{
36015 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
36016 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
36017 if( c<0 ){
36018 /* zNum is less than 9223372036854775808 so it fits */
36019 assert( u<=LARGEST_INT64 )((void) (0));
36020 return rc;
36021 }else{
36022 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
36023 if( c>0 ){
36024 /* zNum is greater than 9223372036854775808 so it overflows */
36025 return 2;
36026 }else{
36027 /* zNum is exactly 9223372036854775808. Fits if negative. The
36028 ** special case 2 overflow if positive */
36029 assert( u-1==LARGEST_INT64 )((void) (0));
36030 return neg ? rc : 3;
36031 }
36032 }
36033 }
36034}
36035
36036/*
36037** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
36038** into a 64-bit signed integer. This routine accepts hexadecimal literals,
36039** whereas sqlite3Atoi64() does not.
36040**
36041** Returns:
36042**
36043** 0 Successful transformation. Fits in a 64-bit signed integer.
36044** 1 Excess text after the integer value
36045** 2 Integer too large for a 64-bit signed integer or is malformed
36046** 3 Special case of 9223372036854775808
36047*/
36048SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
36049#ifndef SQLITE_OMIT_HEX_INTEGER
36050 if( z[0]=='0'
36051 && (z[1]=='x' || z[1]=='X')
36052 ){
36053 u64 u = 0;
36054 int i, k;
36055 for(i=2; z[i]=='0'; i++){}
36056 for(k=i; sqlite3Isxdigit(z[k])(sqlite3CtypeMap[(unsigned char)(z[k])]&0x08); k++){
36057 u = u*16 + sqlite3HexToInt(z[k]);
36058 }
36059 memcpy(pOut, &u, 8);
36060 if( k-i>16 ) return 2;
36061 if( z[k]!=0 ) return 1;
36062 return 0;
36063 }else
36064#endif /* SQLITE_OMIT_HEX_INTEGER */
36065 {
36066 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789"));
36067 if( z[n] ) n++;
36068 return sqlite3Atoi64(z, pOut, n, SQLITE_UTF81);
36069 }
36070}
36071
36072/*
36073** If zNum represents an integer that will fit in 32-bits, then set
36074** *pValue to that integer and return true. Otherwise return false.
36075**
36076** This routine accepts both decimal and hexadecimal notation for integers.
36077**
36078** Any non-numeric characters that following zNum are ignored.
36079** This is different from sqlite3Atoi64() which requires the
36080** input number to be zero-terminated.
36081*/
36082SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *zNum, int *pValue){
36083 sqlite_int64 v = 0;
36084 int i, c;
36085 int neg = 0;
36086 if( zNum[0]=='-' ){
36087 neg = 1;
36088 zNum++;
36089 }else if( zNum[0]=='+' ){
36090 zNum++;
36091 }
36092#ifndef SQLITE_OMIT_HEX_INTEGER
36093 else if( zNum[0]=='0'
36094 && (zNum[1]=='x' || zNum[1]=='X')
36095 && sqlite3Isxdigit(zNum[2])(sqlite3CtypeMap[(unsigned char)(zNum[2])]&0x08)
36096 ){
36097 u32 u = 0;
36098 zNum += 2;
36099 while( zNum[0]=='0' ) zNum++;
36100 for(i=0; i<8 && sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08); i++){
36101 u = u*16 + sqlite3HexToInt(zNum[i]);
36102 }
36103 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08)==0 ){
36104 memcpy(pValue, &u, 4);
36105 return 1;
36106 }else{
36107 return 0;
36108 }
36109 }
36110#endif
36111 if( !sqlite3Isdigit(zNum[0])(sqlite3CtypeMap[(unsigned char)(zNum[0])]&0x04) ) return 0;
36112 while( zNum[0]=='0' ) zNum++;
36113 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
36114 v = v*10 + c;
36115 }
36116
36117 /* The longest decimal representation of a 32 bit integer is 10 digits:
36118 **
36119 ** 1234567890
36120 ** 2^31 -> 2147483648
36121 */
36122 testcase( i==10 );
36123 if( i>10 ){
36124 return 0;
36125 }
36126 testcase( v-neg==2147483647 );
36127 if( v-neg>2147483647 ){
36128 return 0;
36129 }
36130 if( neg ){
36131 v = -v;
36132 }
36133 *pValue = (int)v;
36134 return 1;
36135}
36136
36137/*
36138** Return a 32-bit integer value extracted from a string. If the
36139** string is not an integer, just return 0.
36140*/
36141SQLITE_PRIVATEstatic int sqlite3Atoi(const char *z){
36142 int x = 0;
36143 sqlite3GetInt32(z, &x);
36144 return x;
36145}
36146
36147/*
36148** Decode a floating-point value into an approximate decimal
36149** representation.
36150**
36151** If iRound<=0 then round to -iRound significant digits to the
36152** the left of the decimal point, or to a maximum of mxRound total
36153** significant digits.
36154**
36155** If iRound>0 round to min(iRound,mxRound) significant digits total.
36156**
36157** mxRound must be positive.
36158**
36159** The significant digits of the decimal representation are
36160** stored in p->z[] which is a often (but not always) a pointer
36161** into the middle of p->zBuf[]. There are p->n significant digits.
36162** The p->z[] array is *not* zero-terminated.
36163*/
36164SQLITE_PRIVATEstatic void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){
36165 int i;
36166 u64 v;
36167 int e, exp = 0;
36168 double rr[2];
36169
36170 p->isSpecial = 0;
36171 p->z = p->zBuf;
36172 assert( mxRound>0 )((void) (0));
36173
36174 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
36175 ** NaN. */
36176 if( r<0.0 ){
36177 p->sign = '-';
36178 r = -r;
36179 }else if( r==0.0 ){
36180 p->sign = '+';
36181 p->n = 1;
36182 p->iDP = 1;
36183 p->z = "0";
36184 return;
36185 }else{
36186 p->sign = '+';
36187 }
36188 memcpy(&v,&r,8);
36189 e = v>>52;
36190 if( (e&0x7ff)==0x7ff ){
36191 p->isSpecial = 1 + (v!=0x7ff0000000000000LL);
36192 p->n = 0;
36193 p->iDP = 0;
36194 return;
36195 }
36196
36197 /* Multiply r by powers of ten until it lands somewhere in between
36198 ** 1.0e+19 and 1.0e+17.
36199 **
36200 ** Use Dekker-style double-double computation to increase the
36201 ** precision.
36202 **
36203 ** The error terms on constants like 1.0e+100 computed using the
36204 ** decimal extension, for example as follows:
36205 **
36206 ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100)));
36207 */
36208 rr[0] = r;
36209 rr[1] = 0.0;
36210 if( rr[0]>9.223372036854774784e+18 ){
36211 while( rr[0]>9.223372036854774784e+118 ){
36212 exp += 100;
36213 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
36214 }
36215 while( rr[0]>9.223372036854774784e+28 ){
36216 exp += 10;
36217 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
36218 }
36219 while( rr[0]>9.223372036854774784e+18 ){
36220 exp += 1;
36221 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
36222 }
36223 }else{
36224 while( rr[0]<9.223372036854774784e-83 ){
36225 exp -= 100;
36226 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
36227 }
36228 while( rr[0]<9.223372036854774784e+07 ){
36229 exp -= 10;
36230 dekkerMul2(rr, 1.0e+10, 0.0);
36231 }
36232 while( rr[0]<9.22337203685477478e+17 ){
36233 exp -= 1;
36234 dekkerMul2(rr, 1.0e+01, 0.0);
36235 }
36236 }
36237 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1];
36238
36239 /* Extract significant digits. */
36240 i = sizeof(p->zBuf)-1;
36241 assert( v>0 )((void) (0));
36242 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; }
36243 assert( i>=0 && i<sizeof(p->zBuf)-1 )((void) (0));
36244 p->n = sizeof(p->zBuf) - 1 - i;
36245 assert( p->n>0 )((void) (0));
36246 assert( p->n<sizeof(p->zBuf) )((void) (0));
36247 p->iDP = p->n + exp;
36248 if( iRound<=0 ){
36249 iRound = p->iDP - iRound;
36250 if( iRound==0 && p->zBuf[i+1]>='5' ){
36251 iRound = 1;
36252 p->zBuf[i--] = '0';
36253 p->n++;
36254 p->iDP++;
36255 }
36256 }
36257 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){
36258 char *z = &p->zBuf[i+1];
36259 if( iRound>mxRound ) iRound = mxRound;
36260 p->n = iRound;
36261 if( z[iRound]>='5' ){
36262 int j = iRound-1;
36263 while( 1 /*exit-by-break*/ ){
36264 z[j]++;
36265 if( z[j]<='9' ) break;
36266 z[j] = '0';
36267 if( j==0 ){
36268 p->z[i--] = '1';
36269 p->n++;
36270 p->iDP++;
36271 break;
36272 }else{
36273 j--;
36274 }
36275 }
36276 }
36277 }
36278 p->z = &p->zBuf[i+1];
36279 assert( i+p->n < sizeof(p->zBuf) )((void) (0));
36280 while( ALWAYS(p->n>0)(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; }
36281}
36282
36283/*
36284** Try to convert z into an unsigned 32-bit integer. Return true on
36285** success and false if there is an error.
36286**
36287** Only decimal notation is accepted.
36288*/
36289SQLITE_PRIVATEstatic int sqlite3GetUInt32(const char *z, u32 *pI){
36290 u64 v = 0;
36291 int i;
36292 for(i=0; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){
36293 v = v*10 + z[i] - '0';
36294 if( v>4294967296LL ){ *pI = 0; return 0; }
36295 }
36296 if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
36297 *pI = (u32)v;
36298 return 1;
36299}
36300
36301/*
36302** The variable-length integer encoding is as follows:
36303**
36304** KEY:
36305** A = 0xxxxxxx 7 bits of data and one flag bit
36306** B = 1xxxxxxx 7 bits of data and one flag bit
36307** C = xxxxxxxx 8 bits of data
36308**
36309** 7 bits - A
36310** 14 bits - BA
36311** 21 bits - BBA
36312** 28 bits - BBBA
36313** 35 bits - BBBBA
36314** 42 bits - BBBBBA
36315** 49 bits - BBBBBBA
36316** 56 bits - BBBBBBBA
36317** 64 bits - BBBBBBBBC
36318*/
36319
36320/*
36321** Write a 64-bit variable-length integer to memory starting at p[0].
36322** The length of data write will be between 1 and 9 bytes. The number
36323** of bytes written is returned.
36324**
36325** A variable-length integer consists of the lower 7 bits of each byte
36326** for all bytes that have the 8th bit set and one byte with the 8th
36327** bit clear. Except, if we get to the 9th byte, it stores the full
36328** 8 bits and is the last byte.
36329*/
36330static int SQLITE_NOINLINE__attribute__((noinline)) putVarint64(unsigned char *p, u64 v){
36331 int i, j, n;
36332 u8 buf[10];
36333 if( v & (((u64)0xff000000)<<32) ){
36334 p[8] = (u8)v;
36335 v >>= 8;
36336 for(i=7; i>=0; i--){
36337 p[i] = (u8)((v & 0x7f) | 0x80);
36338 v >>= 7;
36339 }
36340 return 9;
36341 }
36342 n = 0;
36343 do{
36344 buf[n++] = (u8)((v & 0x7f) | 0x80);
36345 v >>= 7;
36346 }while( v!=0 );
36347 buf[0] &= 0x7f;
36348 assert( n<=9 )((void) (0));
36349 for(i=0, j=n-1; j>=0; j--, i++){
36350 p[i] = buf[j];
36351 }
36352 return n;
36353}
36354SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char *p, u64 v){
36355 if( v<=0x7f ){
36356 p[0] = v&0x7f;
36357 return 1;
36358 }
36359 if( v<=0x3fff ){
36360 p[0] = ((v>>7)&0x7f)|0x80;
36361 p[1] = v&0x7f;
36362 return 2;
36363 }
36364 return putVarint64(p,v);
36365}
36366
36367/*
36368** Bitmasks used by sqlite3GetVarint(). These precomputed constants
36369** are defined here rather than simply putting the constant expressions
36370** inline in order to work around bugs in the RVT compiler.
36371**
36372** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
36373**
36374** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
36375*/
36376#define SLOT_2_00x001fc07f 0x001fc07f
36377#define SLOT_4_2_00xf01fc07f 0xf01fc07f
36378
36379
36380/*
36381** Read a 64-bit variable-length integer from memory starting at p[0].
36382** Return the number of bytes read. The value is stored in *v.
36383*/
36384SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
36385 u32 a,b,s;
36386
36387 if( ((signed char*)p)[0]>=0 ){
36388 *v = *p;
36389 return 1;
36390 }
36391 if( ((signed char*)p)[1]>=0 ){
36392 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
36393 return 2;
36394 }
36395
36396 /* Verify that constants are precomputed correctly */
36397 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
36398 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
36399
36400 a = ((u32)p[0])<<14;
36401 b = p[1];
36402 p += 2;
36403 a |= *p;
36404 /* a: p0<<14 | p2 (unmasked) */
36405 if (!(a&0x80))
36406 {
36407 a &= SLOT_2_00x001fc07f;
36408 b &= 0x7f;
36409 b = b<<7;
36410 a |= b;
36411 *v = a;
36412 return 3;
36413 }
36414
36415 /* CSE1 from below */
36416 a &= SLOT_2_00x001fc07f;
36417 p++;
36418 b = b<<14;
36419 b |= *p;
36420 /* b: p1<<14 | p3 (unmasked) */
36421 if (!(b&0x80))
36422 {
36423 b &= SLOT_2_00x001fc07f;
36424 /* moved CSE1 up */
36425 /* a &= (0x7f<<14)|(0x7f); */
36426 a = a<<7;
36427 a |= b;
36428 *v = a;
36429 return 4;
36430 }
36431
36432 /* a: p0<<14 | p2 (masked) */
36433 /* b: p1<<14 | p3 (unmasked) */
36434 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
36435 /* moved CSE1 up */
36436 /* a &= (0x7f<<14)|(0x7f); */
36437 b &= SLOT_2_00x001fc07f;
36438 s = a;
36439 /* s: p0<<14 | p2 (masked) */
36440
36441 p++;
36442 a = a<<14;
36443 a |= *p;
36444 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
36445 if (!(a&0x80))
36446 {
36447 /* we can skip these cause they were (effectively) done above
36448 ** while calculating s */
36449 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
36450 /* b &= (0x7f<<14)|(0x7f); */
36451 b = b<<7;
36452 a |= b;
36453 s = s>>18;
36454 *v = ((u64)s)<<32 | a;
36455 return 5;
36456 }
36457
36458 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
36459 s = s<<7;
36460 s |= b;
36461 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
36462
36463 p++;
36464 b = b<<14;
36465 b |= *p;
36466 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
36467 if (!(b&0x80))
36468 {
36469 /* we can skip this cause it was (effectively) done above in calc'ing s */
36470 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
36471 a &= SLOT_2_00x001fc07f;
36472 a = a<<7;
36473 a |= b;
36474 s = s>>18;
36475 *v = ((u64)s)<<32 | a;
36476 return 6;
36477 }
36478
36479 p++;
36480 a = a<<14;
36481 a |= *p;
36482 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
36483 if (!(a&0x80))
36484 {
36485 a &= SLOT_4_2_00xf01fc07f;
36486 b &= SLOT_2_00x001fc07f;
36487 b = b<<7;
36488 a |= b;
36489 s = s>>11;
36490 *v = ((u64)s)<<32 | a;
36491 return 7;
36492 }
36493
36494 /* CSE2 from below */
36495 a &= SLOT_2_00x001fc07f;
36496 p++;
36497 b = b<<14;
36498 b |= *p;
36499 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
36500 if (!(b&0x80))
36501 {
36502 b &= SLOT_4_2_00xf01fc07f;
36503 /* moved CSE2 up */
36504 /* a &= (0x7f<<14)|(0x7f); */
36505 a = a<<7;
36506 a |= b;
36507 s = s>>4;
36508 *v = ((u64)s)<<32 | a;
36509 return 8;
36510 }
36511
36512 p++;
36513 a = a<<15;
36514 a |= *p;
36515 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
36516
36517 /* moved CSE2 up */
36518 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
36519 b &= SLOT_2_00x001fc07f;
36520 b = b<<8;
36521 a |= b;
36522
36523 s = s<<4;
36524 b = p[-4];
36525 b &= 0x7f;
36526 b = b>>3;
36527 s |= b;
36528
36529 *v = ((u64)s)<<32 | a;
36530
36531 return 9;
36532}
36533
36534/*
36535** Read a 32-bit variable-length integer from memory starting at p[0].
36536** Return the number of bytes read. The value is stored in *v.
36537**
36538** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
36539** integer, then set *v to 0xffffffff.
36540**
36541** A MACRO version, getVarint32, is provided which inlines the
36542** single-byte case. All code should use the MACRO version as
36543** this function assumes the single-byte case has already been handled.
36544*/
36545SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
36546 u64 v64;
36547 u8 n;
36548
36549 /* Assume that the single-byte case has already been handled by
36550 ** the getVarint32() macro */
36551 assert( (p[0] & 0x80)!=0 )((void) (0));
36552
36553 if( (p[1] & 0x80)==0 ){
36554 /* This is the two-byte case */
36555 *v = ((p[0]&0x7f)<<7) | p[1];
36556 return 2;
36557 }
36558 if( (p[2] & 0x80)==0 ){
36559 /* This is the three-byte case */
36560 *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2];
36561 return 3;
36562 }
36563 /* four or more bytes */
36564 n = sqlite3GetVarint(p, &v64);
36565 assert( n>3 && n<=9 )((void) (0));
36566 if( (v64 & SQLITE_MAX_U32((((u64)1)<<32)-1))!=v64 ){
36567 *v = 0xffffffff;
36568 }else{
36569 *v = (u32)v64;
36570 }
36571 return n;
36572}
36573
36574/*
36575** Return the number of bytes that will be needed to store the given
36576** 64-bit integer.
36577*/
36578SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v){
36579 int i;
36580 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 )((void) (0)); }
36581 return i;
36582}
36583
36584
36585/*
36586** Read or write a four-byte big-endian integer value.
36587*/
36588SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8 *p){
36589#if SQLITE_BYTEORDER1234==4321
36590 u32 x;
36591 memcpy(&x,p,4);
36592 return x;
36593#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
36594 u32 x;
36595 memcpy(&x,p,4);
36596 return __builtin_bswap32(x);
36597#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
36598 u32 x;
36599 memcpy(&x,p,4);
36600 return _byteswap_ulong(x);
36601#else
36602 testcase( p[0]&0x80 );
36603 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
36604#endif
36605}
36606SQLITE_PRIVATEstatic void sqlite3Put4byte(unsigned char *p, u32 v){
36607#if SQLITE_BYTEORDER1234==4321
36608 memcpy(p,&v,4);
36609#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
36610 u32 x = __builtin_bswap32(v);
36611 memcpy(p,&x,4);
36612#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
36613 u32 x = _byteswap_ulong(v);
36614 memcpy(p,&x,4);
36615#else
36616 p[0] = (u8)(v>>24);
36617 p[1] = (u8)(v>>16);
36618 p[2] = (u8)(v>>8);
36619 p[3] = (u8)v;
36620#endif
36621}
36622
36623
36624
36625/*
36626** Translate a single byte of Hex into an integer.
36627** This routine only works if h really is a valid hexadecimal
36628** character: 0..9a..fA..F
36629*/
36630SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h){
36631 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') )((void) (0));
36632#ifdef SQLITE_ASCII1
36633 h += 9*(1&(h>>6));
36634#endif
36635#ifdef SQLITE_EBCDIC
36636 h += 9*(1&~(h>>4));
36637#endif
36638 return (u8)(h & 0xf);
36639}
36640
36641#if !defined(SQLITE_OMIT_BLOB_LITERAL)
36642/*
36643** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
36644** value. Return a pointer to its binary value. Space to hold the
36645** binary value has been obtained from malloc and must be freed by
36646** the calling routine.
36647*/
36648SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
36649 char *zBlob;
36650 int i;
36651
36652 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
36653 n--;
36654 if( zBlob ){
36655 for(i=0; i<n; i+=2){
36656 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
36657 }
36658 zBlob[i/2] = 0;
36659 }
36660 return zBlob;
36661}
36662#endif /* !SQLITE_OMIT_BLOB_LITERAL */
36663
36664/*
36665** Log an error that is an API call on a connection pointer that should
36666** not have been used. The "type" of connection pointer is given as the
36667** argument. The zType is a word like "NULL" or "closed" or "invalid".
36668*/
36669static void logBadConnection(const char *zType){
36670 sqlite3_log(SQLITE_MISUSE21,
36671 "API call with %s database connection pointer",
36672 zType
36673 );
36674}
36675
36676/*
36677** Check to make sure we have a valid db pointer. This test is not
36678** foolproof but it does provide some measure of protection against
36679** misuse of the interface such as passing in db pointers that are
36680** NULL or which have been previously closed. If this routine returns
36681** 1 it means that the db pointer is valid and 0 if it should not be
36682** dereferenced for any reason. The calling function should invoke
36683** SQLITE_MISUSE immediately.
36684**
36685** sqlite3SafetyCheckOk() requires that the db pointer be valid for
36686** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
36687** open properly and is not fit for general use but which can be
36688** used as an argument to sqlite3_errmsg() or sqlite3_close().
36689*/
36690SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3 *db){
36691 u8 eOpenState;
36692 if( db==0 ){
36693 logBadConnection("NULL");
36694 return 0;
36695 }
36696 eOpenState = db->eOpenState;
36697 if( eOpenState!=SQLITE_STATE_OPEN0x76 ){
36698 if( sqlite3SafetyCheckSickOrOk(db) ){
36699 testcase( sqlite3GlobalConfig.xLog!=0 );
36700 logBadConnection("unopened");
36701 }
36702 return 0;
36703 }else{
36704 return 1;
36705 }
36706}
36707SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
36708 u8 eOpenState;
36709 eOpenState = db->eOpenState;
36710 if( eOpenState!=SQLITE_STATE_SICK0xba &&
36711 eOpenState!=SQLITE_STATE_OPEN0x76 &&
36712 eOpenState!=SQLITE_STATE_BUSY0x6d ){
36713 testcase( sqlite3GlobalConfig.xLog!=0 );
36714 logBadConnection("invalid");
36715 return 0;
36716 }else{
36717 return 1;
36718 }
36719}
36720
36721/*
36722** Attempt to add, subtract, or multiply the 64-bit signed value iB against
36723** the other 64-bit signed integer at *pA and store the result in *pA.
36724** Return 0 on success. Or if the operation would have resulted in an
36725** overflow, leave *pA unchanged and return 1.
36726*/
36727SQLITE_PRIVATEstatic int sqlite3AddInt64(i64 *pA, i64 iB){
36728#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
36729 return __builtin_add_overflow(*pA, iB, pA);
36730#else
36731 i64 iA = *pA;
36732 testcase( iA==0 ); testcase( iA==1 );
36733 testcase( iB==-1 ); testcase( iB==0 );
36734 if( iB>=0 ){
36735 testcase( iA>0 && LARGEST_INT64 - iA == iB );
36736 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
36737 if( iA>0 && LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) - iA < iB ) return 1;
36738 }else{
36739 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
36740 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
36741 if( iA<0 && -(iA + LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))) > iB + 1 ) return 1;
36742 }
36743 *pA += iB;
36744 return 0;
36745#endif
36746}
36747SQLITE_PRIVATEstatic int sqlite3SubInt64(i64 *pA, i64 iB){
36748#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
36749 return __builtin_sub_overflow(*pA, iB, pA);
36750#else
36751 testcase( iB==SMALLEST_INT64+1 );
36752 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
36753 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
36754 if( (*pA)>=0 ) return 1;
36755 *pA -= iB;
36756 return 0;
36757 }else{
36758 return sqlite3AddInt64(pA, -iB);
36759 }
36760#endif
36761}
36762SQLITE_PRIVATEstatic int sqlite3MulInt64(i64 *pA, i64 iB){
36763#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
36764 return __builtin_mul_overflow(*pA, iB, pA);
36765#else
36766 i64 iA = *pA;
36767 if( iB>0 ){
36768 if( iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/iB ) return 1;
36769 if( iA<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iB ) return 1;
36770 }else if( iB<0 ){
36771 if( iA>0 ){
36772 if( iB<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iA ) return 1;
36773 }else if( iA<0 ){
36774 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
36775 if( iA==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
36776 if( -iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/-iB ) return 1;
36777 }
36778 }
36779 *pA = iA*iB;
36780 return 0;
36781#endif
36782}
36783
36784/*
36785** Compute the absolute value of a 32-bit signed integer, of possible. Or
36786** if the integer has a value of -2147483648, return +2147483647
36787*/
36788SQLITE_PRIVATEstatic int sqlite3AbsInt32(int x){
36789 if( x>=0 ) return x;
36790 if( x==(int)0x80000000 ) return 0x7fffffff;
36791 return -x;
36792}
36793
36794#ifdef SQLITE_ENABLE_8_3_NAMES
36795/*
36796** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
36797** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
36798** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
36799** three characters, then shorten the suffix on z[] to be the last three
36800** characters of the original suffix.
36801**
36802** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
36803** do the suffix shortening regardless of URI parameter.
36804**
36805** Examples:
36806**
36807** test.db-journal => test.nal
36808** test.db-wal => test.wal
36809** test.db-shm => test.shm
36810** test.db-mj7f3319fa => test.9fa
36811*/
36812SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
36813#if SQLITE_ENABLE_8_3_NAMES<2
36814 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
36815#endif
36816 {
36817 int i, sz;
36818 sz = sqlite3Strlen30(z);
36819 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
36820 if( z[i]=='.' && ALWAYS(sz>i+4)(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
36821 }
36822}
36823#endif
36824
36825/*
36826** Find (an approximate) sum of two LogEst values. This computation is
36827** not a simple "+" operator because LogEst is stored as a logarithmic
36828** value.
36829**
36830*/
36831SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
36832 static const unsigned char x[] = {
36833 10, 10, /* 0,1 */
36834 9, 9, /* 2,3 */
36835 8, 8, /* 4,5 */
36836 7, 7, 7, /* 6,7,8 */
36837 6, 6, 6, /* 9,10,11 */
36838 5, 5, 5, /* 12-14 */
36839 4, 4, 4, 4, /* 15-18 */
36840 3, 3, 3, 3, 3, 3, /* 19-24 */
36841 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
36842 };
36843 if( a>=b ){
36844 if( a>b+49 ) return a;
36845 if( a>b+31 ) return a+1;
36846 return a+x[a-b];
36847 }else{
36848 if( b>a+49 ) return b;
36849 if( b>a+31 ) return b+1;
36850 return b+x[b-a];
36851 }
36852}
36853
36854/*
36855** Convert an integer into a LogEst. In other words, compute an
36856** approximation for 10*log2(x).
36857*/
36858SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64 x){
36859 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
36860 LogEst y = 40;
36861 if( x<8 ){
36862 if( x<2 ) return 0;
36863 while( x<8 ){ y -= 10; x <<= 1; }
36864 }else{
36865#if GCC_VERSION(4*1000000+2*1000+1)>=5004000
36866 int i = 60 - __builtin_clzll(x);
36867 y += i*10;
36868 x >>= i;
36869#else
36870 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
36871 while( x>15 ){ y += 10; x >>= 1; }
36872#endif
36873 }
36874 return a[x&7] + y - 10;
36875}
36876
36877/*
36878** Convert a double into a LogEst
36879** In other words, compute an approximation for 10*log2(x).
36880*/
36881SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double x){
36882 u64 a;
36883 LogEst e;
36884 assert( sizeof(x)==8 && sizeof(a)==8 )((void) (0));
36885 if( x<=1 ) return 0;
36886 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
36887 memcpy(&a, &x, 8);
36888 e = (a>>52) - 1022;
36889 return e*10;
36890}
36891
36892/*
36893** Convert a LogEst into an integer.
36894*/
36895SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst x){
36896 u64 n;
36897 n = x%10;
36898 x /= 10;
36899 if( n>=5 ) n -= 2;
36900 else if( n>=1 ) n -= 1;
36901 if( x>60 ) return (u64)LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
36902 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
36903}
36904
36905/*
36906** Add a new name/number pair to a VList. This might require that the
36907** VList object be reallocated, so return the new VList. If an OOM
36908** error occurs, the original VList returned and the
36909** db->mallocFailed flag is set.
36910**
36911** A VList is really just an array of integers. To destroy a VList,
36912** simply pass it to sqlite3DbFree().
36913**
36914** The first integer is the number of integers allocated for the whole
36915** VList. The second integer is the number of integers actually used.
36916** Each name/number pair is encoded by subsequent groups of 3 or more
36917** integers.
36918**
36919** Each name/number pair starts with two integers which are the numeric
36920** value for the pair and the size of the name/number pair, respectively.
36921** The text name overlays one or more following integers. The text name
36922** is always zero-terminated.
36923**
36924** Conceptually:
36925**
36926** struct VList {
36927** int nAlloc; // Number of allocated slots
36928** int nUsed; // Number of used slots
36929** struct VListEntry {
36930** int iValue; // Value for this entry
36931** int nSlot; // Slots used by this entry
36932** // ... variable name goes here
36933** } a[0];
36934** }
36935**
36936** During code generation, pointers to the variable names within the
36937** VList are taken. When that happens, nAlloc is set to zero as an
36938** indication that the VList may never again be enlarged, since the
36939** accompanying realloc() would invalidate the pointers.
36940*/
36941SQLITE_PRIVATEstatic VList *sqlite3VListAdd(
36942 sqlite3 *db, /* The database connection used for malloc() */
36943 VList *pIn, /* The input VList. Might be NULL */
36944 const char *zName, /* Name of symbol to add */
36945 int nName, /* Bytes of text in zName */
36946 int iVal /* Value to associate with zName */
36947){
36948 int nInt; /* number of sizeof(int) objects needed for zName */
36949 char *z; /* Pointer to where zName will be stored */
36950 int i; /* Index in pIn[] where zName is stored */
36951
36952 nInt = nName/4 + 3;
36953 assert( pIn==0 || pIn[0]>=3 )((void) (0)); /* Verify ok to add new elements */
36954 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
36955 /* Enlarge the allocation */
36956 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
36957 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
36958 if( pOut==0 ) return pIn;
36959 if( pIn==0 ) pOut[1] = 2;
36960 pIn = pOut;
36961 pIn[0] = nAlloc;
36962 }
36963 i = pIn[1];
36964 pIn[i] = iVal;
36965 pIn[i+1] = nInt;
36966 z = (char*)&pIn[i+2];
36967 pIn[1] = i+nInt;
36968 assert( pIn[1]<=pIn[0] )((void) (0));
36969 memcpy(z, zName, nName);
36970 z[nName] = 0;
36971 return pIn;
36972}
36973
36974/*
36975** Return a pointer to the name of a variable in the given VList that
36976** has the value iVal. Or return a NULL if there is no such variable in
36977** the list
36978*/
36979SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList *pIn, int iVal){
36980 int i, mx;
36981 if( pIn==0 ) return 0;
36982 mx = pIn[1];
36983 i = 2;
36984 do{
36985 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
36986 i += pIn[i+1];
36987 }while( i<mx );
36988 return 0;
36989}
36990
36991/*
36992** Return the number of the variable named zName, if it is in VList.
36993** or return 0 if there is no such variable.
36994*/
36995SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
36996 int i, mx;
36997 if( pIn==0 ) return 0;
36998 mx = pIn[1];
36999 i = 2;
37000 do{
37001 const char *z = (const char*)&pIn[i+2];
37002 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
37003 i += pIn[i+1];
37004 }while( i<mx );
37005 return 0;
37006}
37007
37008/************** End of util.c ************************************************/
37009/************** Begin file hash.c ********************************************/
37010/*
37011** 2001 September 22
37012**
37013** The author disclaims copyright to this source code. In place of
37014** a legal notice, here is a blessing:
37015**
37016** May you do good and not evil.
37017** May you find forgiveness for yourself and forgive others.
37018** May you share freely, never taking more than you give.
37019**
37020*************************************************************************
37021** This is the implementation of generic hash-tables
37022** used in SQLite.
37023*/
37024/* #include "sqliteInt.h" */
37025/* #include <assert.h> */
37026
37027/* Turn bulk memory into a hash table object by initializing the
37028** fields of the Hash structure.
37029**
37030** "pNew" is a pointer to the hash table that is to be initialized.
37031*/
37032SQLITE_PRIVATEstatic void sqlite3HashInit(Hash *pNew){
37033 assert( pNew!=0 )((void) (0));
37034 pNew->first = 0;
37035 pNew->count = 0;
37036 pNew->htsize = 0;
37037 pNew->ht = 0;
37038}
37039
37040/* Remove all entries from a hash table. Reclaim all memory.
37041** Call this routine to delete a hash table or to reset a hash table
37042** to the empty state.
37043*/
37044SQLITE_PRIVATEstatic void sqlite3HashClear(Hash *pH){
37045 HashElem *elem; /* For looping over all elements of the table */
37046
37047 assert( pH!=0 )((void) (0));
37048 elem = pH->first;
37049 pH->first = 0;
37050 sqlite3_free(pH->ht);
37051 pH->ht = 0;
37052 pH->htsize = 0;
37053 while( elem ){
37054 HashElem *next_elem = elem->next;
37055 sqlite3_free(elem);
37056 elem = next_elem;
37057 }
37058 pH->count = 0;
37059}
37060
37061/*
37062** The hashing function.
37063*/
37064static unsigned int strHash(const char *z){
37065 unsigned int h = 0;
37066 unsigned char c;
37067 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
37068 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
37069 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
37070 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
37071 h += sqlite3UpperToLower[c];
37072 h *= 0x9e3779b1;
37073 }
37074 return h;
37075}
37076
37077
37078/* Link pNew element into the hash table pH. If pEntry!=0 then also
37079** insert pNew into the pEntry hash bucket.
37080*/
37081static void insertElement(
37082 Hash *pH, /* The complete hash table */
37083 struct _ht *pEntry, /* The entry into which pNew is inserted */
37084 HashElem *pNew /* The element to be inserted */
37085){
37086 HashElem *pHead; /* First element already in pEntry */
37087 if( pEntry ){
37088 pHead = pEntry->count ? pEntry->chain : 0;
37089 pEntry->count++;
37090 pEntry->chain = pNew;
37091 }else{
37092 pHead = 0;
37093 }
37094 if( pHead ){
37095 pNew->next = pHead;
37096 pNew->prev = pHead->prev;
37097 if( pHead->prev ){ pHead->prev->next = pNew; }
37098 else { pH->first = pNew; }
37099 pHead->prev = pNew;
37100 }else{
37101 pNew->next = pH->first;
37102 if( pH->first ){ pH->first->prev = pNew; }
37103 pNew->prev = 0;
37104 pH->first = pNew;
37105 }
37106}
37107
37108
37109/* Resize the hash table so that it contains "new_size" buckets.
37110**
37111** The hash table might fail to resize if sqlite3_malloc() fails or
37112** if the new size is the same as the prior size.
37113** Return TRUE if the resize occurs and false if not.
37114*/
37115static int rehash(Hash *pH, unsigned int new_size){
37116 struct _ht *new_ht; /* The new hash table */
37117 HashElem *elem, *next_elem; /* For looping over existing elements */
37118
37119#if SQLITE_MALLOC_SOFT_LIMIT1024>0
37120 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT1024 ){
37121 new_size = SQLITE_MALLOC_SOFT_LIMIT1024/sizeof(struct _ht);
37122 }
37123 if( new_size==pH->htsize ) return 0;
37124#endif
37125
37126 /* The inability to allocates space for a larger hash table is
37127 ** a performance hit but it is not a fatal error. So mark the
37128 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
37129 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
37130 ** only zeroes the requested number of bytes whereas this module will
37131 ** use the actual amount of space allocated for the hash table (which
37132 ** may be larger than the requested amount).
37133 */
37134 sqlite3BeginBenignMalloc();
37135 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
37136 sqlite3EndBenignMalloc();
37137
37138 if( new_ht==0 ) return 0;
37139 sqlite3_free(pH->ht);
37140 pH->ht = new_ht;
37141 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
37142 memset(new_ht, 0, new_size*sizeof(struct _ht));
37143 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
37144 unsigned int h = strHash(elem->pKey) % new_size;
37145 next_elem = elem->next;
37146 insertElement(pH, &new_ht[h], elem);
37147 }
37148 return 1;
37149}
37150
37151/* This function (for internal use only) locates an element in an
37152** hash table that matches the given key. If no element is found,
37153** a pointer to a static null element with HashElem.data==0 is returned.
37154** If pH is not NULL, then the hash for this key is written to *pH.
37155*/
37156static HashElem *findElementWithHash(
37157 const Hash *pH, /* The pH to be searched */
37158 const char *pKey, /* The key we are searching for */
37159 unsigned int *pHash /* Write the hash value here */
37160){
37161 HashElem *elem; /* Used to loop thru the element list */
37162 unsigned int count; /* Number of elements left to test */
37163 unsigned int h; /* The computed hash */
37164 static HashElem nullElement = { 0, 0, 0, 0 };
37165
37166 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
37167 struct _ht *pEntry;
37168 h = strHash(pKey) % pH->htsize;
37169 pEntry = &pH->ht[h];
37170 elem = pEntry->chain;
37171 count = pEntry->count;
37172 }else{
37173 h = 0;
37174 elem = pH->first;
37175 count = pH->count;
37176 }
37177 if( pHash ) *pHash = h;
37178 while( count ){
37179 assert( elem!=0 )((void) (0));
37180 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
37181 return elem;
37182 }
37183 elem = elem->next;
37184 count--;
37185 }
37186 return &nullElement;
37187}
37188
37189/* Remove a single entry from the hash table given a pointer to that
37190** element and a hash on the element's key.
37191*/
37192static void removeElementGivenHash(
37193 Hash *pH, /* The pH containing "elem" */
37194 HashElem* elem, /* The element to be removed from the pH */
37195 unsigned int h /* Hash value for the element */
37196){
37197 struct _ht *pEntry;
37198 if( elem->prev ){
37199 elem->prev->next = elem->next;
37200 }else{
37201 pH->first = elem->next;
37202 }
37203 if( elem->next ){
37204 elem->next->prev = elem->prev;
37205 }
37206 if( pH->ht ){
37207 pEntry = &pH->ht[h];
37208 if( pEntry->chain==elem ){
37209 pEntry->chain = elem->next;
37210 }
37211 assert( pEntry->count>0 )((void) (0));
37212 pEntry->count--;
37213 }
37214 sqlite3_free( elem );
37215 pH->count--;
37216 if( pH->count==0 ){
37217 assert( pH->first==0 )((void) (0));
37218 assert( pH->count==0 )((void) (0));
37219 sqlite3HashClear(pH);
37220 }
37221}
37222
37223/* Attempt to locate an element of the hash table pH with a key
37224** that matches pKey. Return the data for this element if it is
37225** found, or NULL if there is no match.
37226*/
37227SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash *pH, const char *pKey){
37228 assert( pH!=0 )((void) (0));
37229 assert( pKey!=0 )((void) (0));
37230 return findElementWithHash(pH, pKey, 0)->data;
37231}
37232
37233/* Insert an element into the hash table pH. The key is pKey
37234** and the data is "data".
37235**
37236** If no element exists with a matching key, then a new
37237** element is created and NULL is returned.
37238**
37239** If another element already exists with the same key, then the
37240** new data replaces the old data and the old data is returned.
37241** The key is not copied in this instance. If a malloc fails, then
37242** the new data is returned and the hash table is unchanged.
37243**
37244** If the "data" parameter to this function is NULL, then the
37245** element corresponding to "key" is removed from the hash table.
37246*/
37247SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
37248 unsigned int h; /* the hash of the key modulo hash table size */
37249 HashElem *elem; /* Used to loop thru the element list */
37250 HashElem *new_elem; /* New element added to the pH */
37251
37252 assert( pH!=0 )((void) (0));
37253 assert( pKey!=0 )((void) (0));
37254 elem = findElementWithHash(pH,pKey,&h);
37255 if( elem->data ){
37256 void *old_data = elem->data;
37257 if( data==0 ){
37258 removeElementGivenHash(pH,elem,h);
37259 }else{
37260 elem->data = data;
37261 elem->pKey = pKey;
37262 }
37263 return old_data;
37264 }
37265 if( data==0 ) return 0;
37266 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
37267 if( new_elem==0 ) return data;
37268 new_elem->pKey = pKey;
37269 new_elem->data = data;
37270 pH->count++;
37271 if( pH->count>=10 && pH->count > 2*pH->htsize ){
37272 if( rehash(pH, pH->count*2) ){
37273 assert( pH->htsize>0 )((void) (0));
37274 h = strHash(pKey) % pH->htsize;
37275 }
37276 }
37277 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
37278 return 0;
37279}
37280
37281/************** End of hash.c ************************************************/
37282/************** Begin file opcodes.c *****************************************/
37283/* Automatically generated. Do not edit */
37284/* See the tool/mkopcodec.tcl script for details. */
37285#if !defined(SQLITE_OMIT_EXPLAIN) \
37286 || defined(VDBE_PROFILE) \
37287 || defined(SQLITE_DEBUG)
37288#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
37289# define OpHelp(X) "\0" X
37290#else
37291# define OpHelp(X)
37292#endif
37293SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int i){
37294 static const char *const azName[] = {
37295 /* 0 */ "Savepoint" OpHelp(""),
37296 /* 1 */ "AutoCommit" OpHelp(""),
37297 /* 2 */ "Transaction" OpHelp(""),
37298 /* 3 */ "Checkpoint" OpHelp(""),
37299 /* 4 */ "JournalMode" OpHelp(""),
37300 /* 5 */ "Vacuum" OpHelp(""),
37301 /* 6 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
37302 /* 7 */ "VUpdate" OpHelp("data=r[P3@P2]"),
37303 /* 8 */ "Init" OpHelp("Start at P2"),
37304 /* 9 */ "Goto" OpHelp(""),
37305 /* 10 */ "Gosub" OpHelp(""),
37306 /* 11 */ "InitCoroutine" OpHelp(""),
37307 /* 12 */ "Yield" OpHelp(""),
37308 /* 13 */ "MustBeInt" OpHelp(""),
37309 /* 14 */ "Jump" OpHelp(""),
37310 /* 15 */ "Once" OpHelp(""),
37311 /* 16 */ "If" OpHelp(""),
37312 /* 17 */ "IfNot" OpHelp(""),
37313 /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
37314 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
37315 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
37316 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
37317 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
37318 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
37319 /* 24 */ "SeekGT" OpHelp("key=r[P3@P4]"),
37320 /* 25 */ "IfNotOpen" OpHelp("if( !csr[P1] ) goto P2"),
37321 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
37322 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
37323 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
37324 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
37325 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
37326 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
37327 /* 32 */ "Last" OpHelp(""),
37328 /* 33 */ "IfSizeBetween" OpHelp(""),
37329 /* 34 */ "SorterSort" OpHelp(""),
37330 /* 35 */ "Sort" OpHelp(""),
37331 /* 36 */ "Rewind" OpHelp(""),
37332 /* 37 */ "SorterNext" OpHelp(""),
37333 /* 38 */ "Prev" OpHelp(""),
37334 /* 39 */ "Next" OpHelp(""),
37335 /* 40 */ "IdxLE" OpHelp("key=r[P3@P4]"),
37336 /* 41 */ "IdxGT" OpHelp("key=r[P3@P4]"),
37337 /* 42 */ "IdxLT" OpHelp("key=r[P3@P4]"),
37338 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
37339 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
37340 /* 45 */ "IdxGE" OpHelp("key=r[P3@P4]"),
37341 /* 46 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
37342 /* 47 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
37343 /* 48 */ "Program" OpHelp(""),
37344 /* 49 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
37345 /* 50 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
37346 /* 51 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
37347 /* 52 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
37348 /* 53 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
37349 /* 54 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
37350 /* 55 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
37351 /* 56 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
37352 /* 57 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
37353 /* 58 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
37354 /* 59 */ "ElseEq" OpHelp(""),
37355 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
37356 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
37357 /* 62 */ "IncrVacuum" OpHelp(""),
37358 /* 63 */ "VNext" OpHelp(""),
37359 /* 64 */ "Filter" OpHelp("if key(P3@P4) not in filter(P1) goto P2"),
37360 /* 65 */ "PureFunc" OpHelp("r[P3]=func(r[P2@NP])"),
37361 /* 66 */ "Function" OpHelp("r[P3]=func(r[P2@NP])"),
37362 /* 67 */ "Return" OpHelp(""),
37363 /* 68 */ "EndCoroutine" OpHelp(""),
37364 /* 69 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
37365 /* 70 */ "Halt" OpHelp(""),
37366 /* 71 */ "Integer" OpHelp("r[P2]=P1"),
37367 /* 72 */ "Int64" OpHelp("r[P2]=P4"),
37368 /* 73 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
37369 /* 74 */ "BeginSubrtn" OpHelp("r[P2]=NULL"),
37370 /* 75 */ "Null" OpHelp("r[P2..P3]=NULL"),
37371 /* 76 */ "SoftNull" OpHelp("r[P1]=NULL"),
37372 /* 77 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
37373 /* 78 */ "Variable" OpHelp("r[P2]=parameter(P1)"),
37374 /* 79 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
37375 /* 80 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
37376 /* 81 */ "SCopy" OpHelp("r[P2]=r[P1]"),
37377 /* 82 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
37378 /* 83 */ "FkCheck" OpHelp(""),
37379 /* 84 */ "ResultRow" OpHelp("output=r[P1@P2]"),
37380 /* 85 */ "CollSeq" OpHelp(""),
37381 /* 86 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
37382 /* 87 */ "RealAffinity" OpHelp(""),
37383 /* 88 */ "Cast" OpHelp("affinity(r[P1])"),
37384 /* 89 */ "Permutation" OpHelp(""),
37385 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
37386 /* 91 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
37387 /* 92 */ "ZeroOrNull" OpHelp("r[P2] = 0 OR NULL"),
37388 /* 93 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
37389 /* 94 */ "Column" OpHelp("r[P3]=PX cursor P1 column P2"),
37390 /* 95 */ "TypeCheck" OpHelp("typecheck(r[P1@P2])"),
37391 /* 96 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
37392 /* 97 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
37393 /* 98 */ "Count" OpHelp("r[P2]=count()"),
37394 /* 99 */ "ReadCookie" OpHelp(""),
37395 /* 100 */ "SetCookie" OpHelp(""),
37396 /* 101 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
37397 /* 102 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
37398 /* 103 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
37399 /* 104 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
37400 /* 105 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
37401 /* 106 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
37402 /* 107 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
37403 /* 108 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
37404 /* 109 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
37405 /* 110 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
37406 /* 111 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
37407 /* 112 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
37408 /* 113 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
37409 /* 114 */ "OpenDup" OpHelp(""),
37410 /* 115 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
37411 /* 116 */ "OpenAutoindex" OpHelp("nColumn=P2"),
37412 /* 117 */ "OpenEphemeral" OpHelp("nColumn=P2"),
37413 /* 118 */ "String8" OpHelp("r[P2]='P4'"),
37414 /* 119 */ "SorterOpen" OpHelp(""),
37415 /* 120 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
37416 /* 121 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
37417 /* 122 */ "Close" OpHelp(""),
37418 /* 123 */ "ColumnsUsed" OpHelp(""),
37419 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
37420 /* 125 */ "SeekHit" OpHelp("set P2<=seekHit<=P3"),
37421 /* 126 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
37422 /* 127 */ "NewRowid" OpHelp("r[P2]=rowid"),
37423 /* 128 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
37424 /* 129 */ "RowCell" OpHelp(""),
37425 /* 130 */ "Delete" OpHelp(""),
37426 /* 131 */ "ResetCount" OpHelp(""),
37427 /* 132 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
37428 /* 133 */ "SorterData" OpHelp("r[P2]=data"),
37429 /* 134 */ "RowData" OpHelp("r[P2]=data"),
37430 /* 135 */ "Rowid" OpHelp("r[P2]=PX rowid of P1"),
37431 /* 136 */ "NullRow" OpHelp(""),
37432 /* 137 */ "SeekEnd" OpHelp(""),
37433 /* 138 */ "IdxInsert" OpHelp("key=r[P2]"),
37434 /* 139 */ "SorterInsert" OpHelp("key=r[P2]"),
37435 /* 140 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
37436 /* 141 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
37437 /* 142 */ "IdxRowid" OpHelp("r[P2]=rowid"),
37438 /* 143 */ "FinishSeek" OpHelp(""),
37439 /* 144 */ "Destroy" OpHelp(""),
37440 /* 145 */ "Clear" OpHelp(""),
37441 /* 146 */ "ResetSorter" OpHelp(""),
37442 /* 147 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
37443 /* 148 */ "SqlExec" OpHelp(""),
37444 /* 149 */ "ParseSchema" OpHelp(""),
37445 /* 150 */ "LoadAnalysis" OpHelp(""),
37446 /* 151 */ "DropTable" OpHelp(""),
37447 /* 152 */ "DropIndex" OpHelp(""),
37448 /* 153 */ "DropTrigger" OpHelp(""),
37449 /* 154 */ "Real" OpHelp("r[P2]=P4"),
37450 /* 155 */ "IntegrityCk" OpHelp(""),
37451 /* 156 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
37452 /* 157 */ "Param" OpHelp(""),
37453 /* 158 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
37454 /* 159 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
37455 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
37456 /* 161 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
37457 /* 162 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
37458 /* 163 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
37459 /* 164 */ "AggValue" OpHelp("r[P3]=value N=P2"),
37460 /* 165 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
37461 /* 166 */ "Expire" OpHelp(""),
37462 /* 167 */ "CursorLock" OpHelp(""),
37463 /* 168 */ "CursorUnlock" OpHelp(""),
37464 /* 169 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
37465 /* 170 */ "VBegin" OpHelp(""),
37466 /* 171 */ "VCreate" OpHelp(""),
37467 /* 172 */ "VDestroy" OpHelp(""),
37468 /* 173 */ "VOpen" OpHelp(""),
37469 /* 174 */ "VCheck" OpHelp(""),
37470 /* 175 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"),
37471 /* 176 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
37472 /* 177 */ "VRename" OpHelp(""),
37473 /* 178 */ "Pagecount" OpHelp(""),
37474 /* 179 */ "MaxPgcnt" OpHelp(""),
37475 /* 180 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"),
37476 /* 181 */ "GetSubtype" OpHelp("r[P2] = r[P1].subtype"),
37477 /* 182 */ "SetSubtype" OpHelp("r[P2].subtype = r[P1]"),
37478 /* 183 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"),
37479 /* 184 */ "Trace" OpHelp(""),
37480 /* 185 */ "CursorHint" OpHelp(""),
37481 /* 186 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
37482 /* 187 */ "Noop" OpHelp(""),
37483 /* 188 */ "Explain" OpHelp(""),
37484 /* 189 */ "Abortable" OpHelp(""),
37485 };
37486 return azName[i];
37487}
37488#endif
37489
37490/************** End of opcodes.c *********************************************/
37491/************** Begin file os_kv.c *******************************************/
37492/*
37493** 2022-09-06
37494**
37495** The author disclaims copyright to this source code. In place of
37496** a legal notice, here is a blessing:
37497**
37498** May you do good and not evil.
37499** May you find forgiveness for yourself and forgive others.
37500** May you share freely, never taking more than you give.
37501**
37502******************************************************************************
37503**
37504** This file contains an experimental VFS layer that operates on a
37505** Key/Value storage engine where both keys and values must be pure
37506** text.
37507*/
37508/* #include <sqliteInt.h> */
37509#if SQLITE_OS_KV0 || (SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL))
37510
37511/*****************************************************************************
37512** Debugging logic
37513*/
37514
37515/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
37516#if 0
37517#define SQLITE_KV_TRACE(X) printf X
37518#else
37519#define SQLITE_KV_TRACE(X)
37520#endif
37521
37522/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
37523#if 0
37524#define SQLITE_KV_LOG(X) printf X
37525#else
37526#define SQLITE_KV_LOG(X)
37527#endif
37528
37529
37530/*
37531** Forward declaration of objects used by this VFS implementation
37532*/
37533typedef struct KVVfsFile KVVfsFile;
37534
37535/* A single open file. There are only two files represented by this
37536** VFS - the database and the rollback journal.
37537*/
37538struct KVVfsFile {
37539 sqlite3_file base; /* IO methods */
37540 const char *zClass; /* Storage class */
37541 int isJournal; /* True if this is a journal file */
37542 unsigned int nJrnl; /* Space allocated for aJrnl[] */
37543 char *aJrnl; /* Journal content */
37544 int szPage; /* Last known page size */
37545 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
37546 char *aData; /* Buffer to hold page data */
37547};
37548#define SQLITE_KVOS_SZ 133073
37549
37550/*
37551** Methods for KVVfsFile
37552*/
37553static int kvvfsClose(sqlite3_file*);
37554static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
37555static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
37556static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
37557static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
37558static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
37559static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
37560static int kvvfsSyncDb(sqlite3_file*, int flags);
37561static int kvvfsSyncJrnl(sqlite3_file*, int flags);
37562static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
37563static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
37564static int kvvfsLock(sqlite3_file*, int);
37565static int kvvfsUnlock(sqlite3_file*, int);
37566static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
37567static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
37568static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
37569static int kvvfsSectorSize(sqlite3_file*);
37570static int kvvfsDeviceCharacteristics(sqlite3_file*);
37571
37572/*
37573** Methods for sqlite3_vfs
37574*/
37575static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
37576static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
37577static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
37578static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
37579static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
37580static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
37581static int kvvfsSleep(sqlite3_vfs*, int microseconds);
37582static int kvvfsCurrentTime(sqlite3_vfs*, double*);
37583static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
37584
37585static sqlite3_vfs sqlite3OsKvvfsObject = {
37586 1, /* iVersion */
37587 sizeof(KVVfsFile), /* szOsFile */
37588 1024, /* mxPathname */
37589 0, /* pNext */
37590 "kvvfs", /* zName */
37591 0, /* pAppData */
37592 kvvfsOpen, /* xOpen */
37593 kvvfsDelete, /* xDelete */
37594 kvvfsAccess, /* xAccess */
37595 kvvfsFullPathname, /* xFullPathname */
37596 kvvfsDlOpen, /* xDlOpen */
37597 0, /* xDlError */
37598 0, /* xDlSym */
37599 0, /* xDlClose */
37600 kvvfsRandomness, /* xRandomness */
37601 kvvfsSleep, /* xSleep */
37602 kvvfsCurrentTime, /* xCurrentTime */
37603 0, /* xGetLastError */
37604 kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
37605};
37606
37607/* Methods for sqlite3_file objects referencing a database file
37608*/
37609static sqlite3_io_methods kvvfs_db_io_methods = {
37610 1, /* iVersion */
37611 kvvfsClose, /* xClose */
37612 kvvfsReadDb, /* xRead */
37613 kvvfsWriteDb, /* xWrite */
37614 kvvfsTruncateDb, /* xTruncate */
37615 kvvfsSyncDb, /* xSync */
37616 kvvfsFileSizeDb, /* xFileSize */
37617 kvvfsLock, /* xLock */
37618 kvvfsUnlock, /* xUnlock */
37619 kvvfsCheckReservedLock, /* xCheckReservedLock */
37620 kvvfsFileControlDb, /* xFileControl */
37621 kvvfsSectorSize, /* xSectorSize */
37622 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
37623 0, /* xShmMap */
37624 0, /* xShmLock */
37625 0, /* xShmBarrier */
37626 0, /* xShmUnmap */
37627 0, /* xFetch */
37628 0 /* xUnfetch */
37629};
37630
37631/* Methods for sqlite3_file objects referencing a rollback journal
37632*/
37633static sqlite3_io_methods kvvfs_jrnl_io_methods = {
37634 1, /* iVersion */
37635 kvvfsClose, /* xClose */
37636 kvvfsReadJrnl, /* xRead */
37637 kvvfsWriteJrnl, /* xWrite */
37638 kvvfsTruncateJrnl, /* xTruncate */
37639 kvvfsSyncJrnl, /* xSync */
37640 kvvfsFileSizeJrnl, /* xFileSize */
37641 kvvfsLock, /* xLock */
37642 kvvfsUnlock, /* xUnlock */
37643 kvvfsCheckReservedLock, /* xCheckReservedLock */
37644 kvvfsFileControlJrnl, /* xFileControl */
37645 kvvfsSectorSize, /* xSectorSize */
37646 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
37647 0, /* xShmMap */
37648 0, /* xShmLock */
37649 0, /* xShmBarrier */
37650 0, /* xShmUnmap */
37651 0, /* xFetch */
37652 0 /* xUnfetch */
37653};
37654
37655/****** Storage subsystem **************************************************/
37656#include <sys/types.h>
37657#include <sys/stat.h>
37658#include <unistd.h>
37659
37660/* Forward declarations for the low-level storage engine
37661*/
37662static int kvstorageWrite(const char*, const char *zKey, const char *zData);
37663static int kvstorageDelete(const char*, const char *zKey);
37664static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
37665#define KVSTORAGE_KEY_SZ 32
37666
37667/* Expand the key name with an appropriate prefix and put the result
37668** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
37669** KVSTORAGE_KEY_SZ bytes.
37670*/
37671static void kvstorageMakeKey(
37672 const char *zClass,
37673 const char *zKeyIn,
37674 char *zKeyOut
37675){
37676 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
37677}
37678
37679/* Write content into a key. zClass is the particular namespace of the
37680** underlying key/value store to use - either "local" or "session".
37681**
37682** Both zKey and zData are zero-terminated pure text strings.
37683**
37684** Return the number of errors.
37685*/
37686static int kvstorageWrite(
37687 const char *zClass,
37688 const char *zKey,
37689 const char *zData
37690){
37691 FILE *fd;
37692 char zXKey[KVSTORAGE_KEY_SZ];
37693 kvstorageMakeKey(zClass, zKey, zXKey);
37694 fd = fopen(zXKey, "wb");
37695 if( fd ){
37696 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
37697 (int)strlen(zData), zData,
37698 strlen(zData)>50 ? "..." : ""));
37699 fputs(zData, fd);
37700 fclose(fd);
37701 return 0;
37702 }else{
37703 return 1;
37704 }
37705}
37706
37707/* Delete a key (with its corresponding data) from the key/value
37708** namespace given by zClass. If the key does not previously exist,
37709** this routine is a no-op.
37710*/
37711static int kvstorageDelete(const char *zClass, const char *zKey){
37712 char zXKey[KVSTORAGE_KEY_SZ];
37713 kvstorageMakeKey(zClass, zKey, zXKey);
37714 unlink(zXKey);
37715 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
37716 return 0;
37717}
37718
37719/* Read the value associated with a zKey from the key/value namespace given
37720** by zClass and put the text data associated with that key in the first
37721** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
37722** enough to hold it all. The value put into zBuf must always be zero
37723** terminated, even if it gets truncated because nBuf is not large enough.
37724**
37725** Return the total number of bytes in the data, without truncation, and
37726** not counting the final zero terminator. Return -1 if the key does
37727** not exist.
37728**
37729** If nBuf<=0 then this routine simply returns the size of the data without
37730** actually reading it.
37731*/
37732static int kvstorageRead(
37733 const char *zClass,
37734 const char *zKey,
37735 char *zBuf,
37736 int nBuf
37737){
37738 FILE *fd;
37739 struct stat buf;
37740 char zXKey[KVSTORAGE_KEY_SZ];
37741 kvstorageMakeKey(zClass, zKey, zXKey);
37742 if( access(zXKey, R_OK4)!=0
37743 || stat(zXKey, &buf)!=0
37744 || !S_ISREG(buf.st_mode)((((buf.st_mode)) & 0170000) == (0100000))
37745 ){
37746 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37747 return -1;
37748 }
37749 if( nBuf<=0 ){
37750 return (int)buf.st_size;
37751 }else if( nBuf==1 ){
37752 zBuf[0] = 0;
37753 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
37754 (int)buf.st_size));
37755 return (int)buf.st_size;
37756 }
37757 if( nBuf > buf.st_size + 1 ){
37758 nBuf = buf.st_size + 1;
37759 }
37760 fd = fopen(zXKey, "rb");
37761 if( fd==0 ){
37762 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37763 return -1;
37764 }else{
37765 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
37766 fclose(fd);
37767 zBuf[n] = 0;
37768 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
37769 n, zBuf, n>50 ? "..." : ""));
37770 return (int)n;
37771 }
37772}
37773
37774/*
37775** An internal level of indirection which enables us to replace the
37776** kvvfs i/o methods with JavaScript implementations in WASM builds.
37777** Maintenance reminder: if this struct changes in any way, the JSON
37778** rendering of its structure must be updated in
37779** sqlite3_wasm_enum_json(). There are no binary compatibility
37780** concerns, so it does not need an iVersion member. This file is
37781** necessarily always compiled together with sqlite3_wasm_enum_json(),
37782** and JS code dynamically creates the mapping of members based on
37783** that JSON description.
37784*/
37785typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
37786struct sqlite3_kvvfs_methods {
37787 int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
37788 int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
37789 int (*xDelete)(const char *zClass, const char *zKey);
37790 const int nKeySize;
37791};
37792
37793/*
37794** This object holds the kvvfs I/O methods which may be swapped out
37795** for JavaScript-side implementations in WASM builds. In such builds
37796** it cannot be const, but in native builds it should be so that
37797** the compiler can hopefully optimize this level of indirection out.
37798** That said, kvvfs is intended primarily for use in WASM builds.
37799**
37800** Note that this is not explicitly flagged as static because the
37801** amalgamation build will tag it with SQLITE_PRIVATE.
37802*/
37803#ifndef SQLITE_WASM
37804const
37805#endif
37806SQLITE_PRIVATEstatic sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
37807kvstorageRead,
37808kvstorageWrite,
37809kvstorageDelete,
37810KVSTORAGE_KEY_SZ
37811};
37812
37813/****** Utility subroutines ************************************************/
37814
37815/*
37816** Encode binary into the text encoded used to persist on disk.
37817** The output text is stored in aOut[], which must be at least
37818** nData+1 bytes in length.
37819**
37820** Return the actual length of the encoded text, not counting the
37821** zero terminator at the end.
37822**
37823** Encoding format
37824** ---------------
37825**
37826** * Non-zero bytes are encoded as upper-case hexadecimal
37827**
37828** * A sequence of one or more zero-bytes that are not at the
37829** beginning of the buffer are encoded as a little-endian
37830** base-26 number using a..z. "a" means 0. "b" means 1,
37831** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
37832**
37833** * Because there is no overlap between the encoding characters
37834** of hexadecimal and base-26 numbers, it is always clear where
37835** one stops and the next begins.
37836*/
37837static int kvvfsEncode(const char *aData, int nData, char *aOut){
37838 int i, j;
37839 const unsigned char *a = (const unsigned char*)aData;
37840 for(i=j=0; i<nData; i++){
37841 unsigned char c = a[i];
37842 if( c!=0 ){
37843 aOut[j++] = "0123456789ABCDEF"[c>>4];
37844 aOut[j++] = "0123456789ABCDEF"[c&0xf];
37845 }else{
37846 /* A sequence of 1 or more zeros is stored as a little-endian
37847 ** base-26 number using a..z as the digits. So one zero is "b".
37848 ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
37849 ** and so forth.
37850 */
37851 int k;
37852 for(k=1; i+k<nData && a[i+k]==0; k++){}
37853 i += k-1;
37854 while( k>0 ){
37855 aOut[j++] = 'a'+(k%26);
37856 k /= 26;
37857 }
37858 }
37859 }
37860 aOut[j] = 0;
37861 return j;
37862}
37863
37864static const signed char kvvfsHexValue[256] = {
37865 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37866 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37867 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37868 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
37869 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37870 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37871 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37872 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37873
37874 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37875 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37876 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37877 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37878 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37879 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37880 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37881 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
37882};
37883
37884/*
37885** Decode the text encoding back to binary. The binary content is
37886** written into pOut, which must be at least nOut bytes in length.
37887**
37888** The return value is the number of bytes actually written into aOut[].
37889*/
37890static int kvvfsDecode(const char *a, char *aOut, int nOut){
37891 int i, j;
37892 int c;
37893 const unsigned char *aIn = (const unsigned char*)a;
37894 i = 0;
37895 j = 0;
37896 while( 1 ){
37897 c = kvvfsHexValue[aIn[i]];
37898 if( c<0 ){
37899 int n = 0;
37900 int mult = 1;
37901 c = aIn[i];
37902 if( c==0 ) break;
37903 while( c>='a' && c<='z' ){
37904 n += (c - 'a')*mult;
37905 mult *= 26;
37906 c = aIn[++i];
37907 }
37908 if( j+n>nOut ) return -1;
37909 memset(&aOut[j], 0, n);
37910 j += n;
37911 if( c==0 || mult==1 ) break; /* progress stalled if mult==1 */
37912 }else{
37913 aOut[j] = c<<4;
37914 c = kvvfsHexValue[aIn[++i]];
37915 if( c<0 ) break;
37916 aOut[j++] += c;
37917 i++;
37918 }
37919 }
37920 return j;
37921}
37922
37923/*
37924** Decode a complete journal file. Allocate space in pFile->aJrnl
37925** and store the decoding there. Or leave pFile->aJrnl set to NULL
37926** if an error is encountered.
37927**
37928** The first few characters of the text encoding will be a little-endian
37929** base-26 number (digits a..z) that is the total number of bytes
37930** in the decoded journal file image. This base-26 number is followed
37931** by a single space, then the encoding of the journal. The space
37932** separator is required to act as a terminator for the base-26 number.
37933*/
37934static void kvvfsDecodeJournal(
37935 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
37936 const char *zTxt, /* Text encoding. Zero-terminated */
37937 int nTxt /* Bytes in zTxt, excluding zero terminator */
37938){
37939 unsigned int n = 0;
37940 int c, i, mult;
37941 i = 0;
37942 mult = 1;
37943 while( (c = zTxt[i++])>='a' && c<='z' ){
37944 n += (zTxt[i] - 'a')*mult;
37945 mult *= 26;
37946 }
37947 sqlite3_free(pFile->aJrnl);
37948 pFile->aJrnl = sqlite3_malloc64( n );
37949 if( pFile->aJrnl==0 ){
37950 pFile->nJrnl = 0;
37951 return;
37952 }
37953 pFile->nJrnl = n;
37954 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
37955 if( n<pFile->nJrnl ){
37956 sqlite3_free(pFile->aJrnl);
37957 pFile->aJrnl = 0;
37958 pFile->nJrnl = 0;
37959 }
37960}
37961
37962/*
37963** Read or write the "sz" element, containing the database file size.
37964*/
37965static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
37966 char zData[50];
37967 zData[0] = 0;
37968 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
37969 return strtoll(zData, 0, 0);
37970}
37971static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
37972 char zData[50];
37973 sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
37974 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
37975}
37976
37977/****** sqlite3_io_methods methods ******************************************/
37978
37979/*
37980** Close an kvvfs-file.
37981*/
37982static int kvvfsClose(sqlite3_file *pProtoFile){
37983 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
37984
37985 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
37986 pFile->isJournal ? "journal" : "db"));
37987 sqlite3_free(pFile->aJrnl);
37988 sqlite3_free(pFile->aData);
37989 return SQLITE_OK0;
37990}
37991
37992/*
37993** Read from the -journal file.
37994*/
37995static int kvvfsReadJrnl(
37996 sqlite3_file *pProtoFile,
37997 void *zBuf,
37998 int iAmt,
37999 sqlite_int64 iOfst
38000){
38001 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38002 assert( pFile->isJournal )((void) (0));
38003 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38004 if( pFile->aJrnl==0 ){
38005 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
38006 char *aTxt;
38007 if( szTxt<=4 ){
38008 return SQLITE_IOERR10;
38009 }
38010 aTxt = sqlite3_malloc64( szTxt+1 );
38011 if( aTxt==0 ) return SQLITE_NOMEM7;
38012 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
38013 kvvfsDecodeJournal(pFile, aTxt, szTxt);
38014 sqlite3_free(aTxt);
38015 if( pFile->aJrnl==0 ) return SQLITE_IOERR10;
38016 }
38017 if( iOfst+iAmt>pFile->nJrnl ){
38018 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
38019 }
38020 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
38021 return SQLITE_OK0;
38022}
38023
38024/*
38025** Read from the database file.
38026*/
38027static int kvvfsReadDb(
38028 sqlite3_file *pProtoFile,
38029 void *zBuf,
38030 int iAmt,
38031 sqlite_int64 iOfst
38032){
38033 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38034 unsigned int pgno;
38035 int got, n;
38036 char zKey[30];
38037 char *aData = pFile->aData;
38038 assert( iOfst>=0 )((void) (0));
38039 assert( iAmt>=0 )((void) (0));
38040 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38041 if( iOfst+iAmt>=512 ){
38042 if( (iOfst % iAmt)!=0 ){
38043 return SQLITE_IOERR_READ(10 | (1<<8));
38044 }
38045 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
38046 return SQLITE_IOERR_READ(10 | (1<<8));
38047 }
38048 pFile->szPage = iAmt;
38049 pgno = 1 + iOfst/iAmt;
38050 }else{
38051 pgno = 1;
38052 }
38053 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
38054 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
38055 aData, SQLITE_KVOS_SZ-1);
38056 if( got<0 ){
38057 n = 0;
38058 }else{
38059 aData[got] = 0;
38060 if( iOfst+iAmt<512 ){
38061 int k = iOfst+iAmt;
38062 aData[k*2] = 0;
38063 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
38064 if( n>=iOfst+iAmt ){
38065 memcpy(zBuf, &aData[2000+iOfst], iAmt);
38066 n = iAmt;
38067 }else{
38068 n = 0;
38069 }
38070 }else{
38071 n = kvvfsDecode(aData, zBuf, iAmt);
38072 }
38073 }
38074 if( n<iAmt ){
38075 memset(zBuf+n, 0, iAmt-n);
38076 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
38077 }
38078 return SQLITE_OK0;
38079}
38080
38081
38082/*
38083** Write into the -journal file.
38084*/
38085static int kvvfsWriteJrnl(
38086 sqlite3_file *pProtoFile,
38087 const void *zBuf,
38088 int iAmt,
38089 sqlite_int64 iOfst
38090){
38091 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38092 sqlite3_int64 iEnd = iOfst+iAmt;
38093 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38094 if( iEnd>=0x10000000 ) return SQLITE_FULL13;
38095 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
38096 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
38097 if( aNew==0 ){
38098 return SQLITE_IOERR_NOMEM(10 | (12<<8));
38099 }
38100 pFile->aJrnl = aNew;
38101 if( pFile->nJrnl<iOfst ){
38102 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
38103 }
38104 pFile->nJrnl = iEnd;
38105 }
38106 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
38107 return SQLITE_OK0;
38108}
38109
38110/*
38111** Write into the database file.
38112*/
38113static int kvvfsWriteDb(
38114 sqlite3_file *pProtoFile,
38115 const void *zBuf,
38116 int iAmt,
38117 sqlite_int64 iOfst
38118){
38119 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38120 unsigned int pgno;
38121 char zKey[30];
38122 char *aData = pFile->aData;
38123 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38124 assert( iAmt>=512 && iAmt<=65536 )((void) (0));
38125 assert( (iAmt & (iAmt-1))==0 )((void) (0));
38126 assert( pFile->szPage<0 || pFile->szPage==iAmt )((void) (0));
38127 pFile->szPage = iAmt;
38128 pgno = 1 + iOfst/iAmt;
38129 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
38130 kvvfsEncode(zBuf, iAmt, aData);
38131 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
38132 return SQLITE_IOERR10;
38133 }
38134 if( iOfst+iAmt > pFile->szDb ){
38135 pFile->szDb = iOfst + iAmt;
38136 }
38137 return SQLITE_OK0;
38138}
38139
38140/*
38141** Truncate an kvvfs-file.
38142*/
38143static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
38144 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38145 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
38146 assert( size==0 )((void) (0));
38147 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
38148 sqlite3_free(pFile->aJrnl);
38149 pFile->aJrnl = 0;
38150 pFile->nJrnl = 0;
38151 return SQLITE_OK0;
38152}
38153static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
38154 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38155 if( pFile->szDb>size
38156 && pFile->szPage>0
38157 && (size % pFile->szPage)==0
38158 ){
38159 char zKey[50];
38160 unsigned int pgno, pgnoMax;
38161 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
38162 pgno = 1 + size/pFile->szPage;
38163 pgnoMax = 2 + pFile->szDb/pFile->szPage;
38164 while( pgno<=pgnoMax ){
38165 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
38166 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
38167 pgno++;
38168 }
38169 pFile->szDb = size;
38170 return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR10 : SQLITE_OK0;
38171 }
38172 return SQLITE_IOERR10;
38173}
38174
38175/*
38176** Sync an kvvfs-file.
38177*/
38178static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
38179 int i, n;
38180 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38181 char *zOut;
38182 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
38183 if( pFile->nJrnl<=0 ){
38184 return kvvfsTruncateJrnl(pProtoFile, 0);
38185 }
38186 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
38187 if( zOut==0 ){
38188 return SQLITE_IOERR_NOMEM(10 | (12<<8));
38189 }
38190 n = pFile->nJrnl;
38191 i = 0;
38192 do{
38193 zOut[i++] = 'a' + (n%26);
38194 n /= 26;
38195 }while( n>0 );
38196 zOut[i++] = ' ';
38197 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
38198 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
38199 sqlite3_free(zOut);
38200 return i ? SQLITE_IOERR10 : SQLITE_OK0;
38201}
38202static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
38203 return SQLITE_OK0;
38204}
38205
38206/*
38207** Return the current file-size of an kvvfs-file.
38208*/
38209static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
38210 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38211 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
38212 *pSize = pFile->nJrnl;
38213 return SQLITE_OK0;
38214}
38215static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
38216 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38217 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
38218 if( pFile->szDb>=0 ){
38219 *pSize = pFile->szDb;
38220 }else{
38221 *pSize = kvvfsReadFileSize(pFile);
38222 }
38223 return SQLITE_OK0;
38224}
38225
38226/*
38227** Lock an kvvfs-file.
38228*/
38229static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
38230 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38231 assert( !pFile->isJournal )((void) (0));
38232 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
38233
38234 if( eLock!=SQLITE_LOCK_NONE0 ){
38235 pFile->szDb = kvvfsReadFileSize(pFile);
38236 }
38237 return SQLITE_OK0;
38238}
38239
38240/*
38241** Unlock an kvvfs-file.
38242*/
38243static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
38244 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38245 assert( !pFile->isJournal )((void) (0));
38246 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
38247 if( eLock==SQLITE_LOCK_NONE0 ){
38248 pFile->szDb = -1;
38249 }
38250 return SQLITE_OK0;
38251}
38252
38253/*
38254** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
38255*/
38256static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
38257 SQLITE_KV_LOG(("xCheckReservedLock\n"));
38258 *pResOut = 0;
38259 return SQLITE_OK0;
38260}
38261
38262/*
38263** File control method. For custom operations on an kvvfs-file.
38264*/
38265static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
38266 SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
38267 return SQLITE_NOTFOUND12;
38268}
38269static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
38270 SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
38271 if( op==SQLITE_FCNTL_SYNC21 ){
38272 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
38273 int rc = SQLITE_OK0;
38274 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
38275 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
38276 rc = SQLITE_IOERR10;
38277 }
38278 return rc;
38279 }
38280 return SQLITE_NOTFOUND12;
38281}
38282
38283/*
38284** Return the sector-size in bytes for an kvvfs-file.
38285*/
38286static int kvvfsSectorSize(sqlite3_file *pFile){
38287 return 512;
38288}
38289
38290/*
38291** Return the device characteristic flags supported by an kvvfs-file.
38292*/
38293static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
38294 return 0;
38295}
38296
38297/****** sqlite3_vfs methods *************************************************/
38298
38299/*
38300** Open an kvvfs file handle.
38301*/
38302static int kvvfsOpen(
38303 sqlite3_vfs *pProtoVfs,
38304 const char *zName,
38305 sqlite3_file *pProtoFile,
38306 int flags,
38307 int *pOutFlags
38308){
38309 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
38310 if( zName==0 ) zName = "";
38311 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
38312 if( strcmp(zName, "local")==0
38313 || strcmp(zName, "session")==0
38314 ){
38315 pFile->isJournal = 0;
38316 pFile->base.pMethods = &kvvfs_db_io_methods;
38317 }else
38318 if( strcmp(zName, "local-journal")==0
38319 || strcmp(zName, "session-journal")==0
38320 ){
38321 pFile->isJournal = 1;
38322 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
38323 }else{
38324 return SQLITE_CANTOPEN14;
38325 }
38326 if( zName[0]=='s' ){
38327 pFile->zClass = "session";
38328 }else{
38329 pFile->zClass = "local";
38330 }
38331 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
38332 if( pFile->aData==0 ){
38333 return SQLITE_NOMEM7;
38334 }
38335 pFile->aJrnl = 0;
38336 pFile->nJrnl = 0;
38337 pFile->szPage = -1;
38338 pFile->szDb = -1;
38339 return SQLITE_OK0;
38340}
38341
38342/*
38343** Delete the file located at zPath. If the dirSync argument is true,
38344** ensure the file-system modifications are synced to disk before
38345** returning.
38346*/
38347static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
38348 if( strcmp(zPath, "local-journal")==0 ){
38349 sqlite3KvvfsMethods.xDelete("local", "jrnl");
38350 }else
38351 if( strcmp(zPath, "session-journal")==0 ){
38352 sqlite3KvvfsMethods.xDelete("session", "jrnl");
38353 }
38354 return SQLITE_OK0;
38355}
38356
38357/*
38358** Test for access permissions. Return true if the requested permission
38359** is available, or false otherwise.
38360*/
38361static int kvvfsAccess(
38362 sqlite3_vfs *pProtoVfs,
38363 const char *zPath,
38364 int flags,
38365 int *pResOut
38366){
38367 SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
38368 if( strcmp(zPath, "local-journal")==0 ){
38369 *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
38370 }else
38371 if( strcmp(zPath, "session-journal")==0 ){
38372 *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
38373 }else
38374 if( strcmp(zPath, "local")==0 ){
38375 *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
38376 }else
38377 if( strcmp(zPath, "session")==0 ){
38378 *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
38379 }else
38380 {
38381 *pResOut = 0;
38382 }
38383 SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
38384 return SQLITE_OK0;
38385}
38386
38387/*
38388** Populate buffer zOut with the full canonical pathname corresponding
38389** to the pathname in zPath. zOut is guaranteed to point to a buffer
38390** of at least (INST_MAX_PATHNAME+1) bytes.
38391*/
38392static int kvvfsFullPathname(
38393 sqlite3_vfs *pVfs,
38394 const char *zPath,
38395 int nOut,
38396 char *zOut
38397){
38398 size_t nPath;
38399#ifdef SQLITE_OS_KV_ALWAYS_LOCAL
38400 zPath = "local";
38401#endif
38402 nPath = strlen(zPath);
38403 SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
38404 if( nOut<nPath+1 ) nPath = nOut - 1;
38405 memcpy(zOut, zPath, nPath);
38406 zOut[nPath] = 0;
38407 return SQLITE_OK0;
38408}
38409
38410/*
38411** Open the dynamic library located at zPath and return a handle.
38412*/
38413static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
38414 return 0;
38415}
38416
38417/*
38418** Populate the buffer pointed to by zBufOut with nByte bytes of
38419** random data.
38420*/
38421static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
38422 memset(zBufOut, 0, nByte);
38423 return nByte;
38424}
38425
38426/*
38427** Sleep for nMicro microseconds. Return the number of microseconds
38428** actually slept.
38429*/
38430static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
38431 return SQLITE_OK0;
38432}
38433
38434/*
38435** Return the current time as a Julian Day number in *pTimeOut.
38436*/
38437static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
38438 sqlite3_int64 i = 0;
38439 int rc;
38440 rc = kvvfsCurrentTimeInt64(0, &i);
38441 *pTimeOut = i/86400000.0;
38442 return rc;
38443}
38444#include <sys/time.h>
38445static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
38446 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
38447 struct timeval sNow;
38448 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
38449 *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
38450 return SQLITE_OK0;
38451}
38452#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
38453
38454#if SQLITE_OS_KV0
38455/*
38456** This routine is called initialize the KV-vfs as the default VFS.
38457*/
38458SQLITE_API int sqlite3_os_init(void){
38459 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
38460}
38461SQLITE_API int sqlite3_os_end(void){
38462 return SQLITE_OK0;
38463}
38464#endif /* SQLITE_OS_KV */
38465
38466#if SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL)
38467SQLITE_PRIVATEstatic int sqlite3KvvfsInit(void){
38468 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
38469}
38470#endif
38471
38472/************** End of os_kv.c ***********************************************/
38473/************** Begin file os_unix.c *****************************************/
38474/*
38475** 2004 May 22
38476**
38477** The author disclaims copyright to this source code. In place of
38478** a legal notice, here is a blessing:
38479**
38480** May you do good and not evil.
38481** May you find forgiveness for yourself and forgive others.
38482** May you share freely, never taking more than you give.
38483**
38484******************************************************************************
38485**
38486** This file contains the VFS implementation for unix-like operating systems
38487** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
38488**
38489** There are actually several different VFS implementations in this file.
38490** The differences are in the way that file locking is done. The default
38491** implementation uses Posix Advisory Locks. Alternative implementations
38492** use flock(), dot-files, various proprietary locking schemas, or simply
38493** skip locking all together.
38494**
38495** This source file is organized into divisions where the logic for various
38496** subfunctions is contained within the appropriate division. PLEASE
38497** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
38498** in the correct division and should be clearly labelled.
38499**
38500** The layout of divisions is as follows:
38501**
38502** * General-purpose declarations and utility functions.
38503** * Unique file ID logic used by VxWorks.
38504** * Various locking primitive implementations (all except proxy locking):
38505** + for Posix Advisory Locks
38506** + for no-op locks
38507** + for dot-file locks
38508** + for flock() locking
38509** + for named semaphore locks (VxWorks only)
38510** + for AFP filesystem locks (MacOSX only)
38511** * sqlite3_file methods not associated with locking.
38512** * Definitions of sqlite3_io_methods objects for all locking
38513** methods plus "finder" functions for each locking method.
38514** * sqlite3_vfs method implementations.
38515** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
38516** * Definitions of sqlite3_vfs objects for all locking methods
38517** plus implementations of sqlite3_os_init() and sqlite3_os_end().
38518*/
38519/* #include "sqliteInt.h" */
38520#if SQLITE_OS_UNIX1 /* This file is used on unix only */
38521
38522/*
38523** There are various methods for file locking used for concurrency
38524** control:
38525**
38526** 1. POSIX locking (the default),
38527** 2. No locking,
38528** 3. Dot-file locking,
38529** 4. flock() locking,
38530** 5. AFP locking (OSX only),
38531** 6. Named POSIX semaphores (VXWorks only),
38532** 7. proxy locking. (OSX only)
38533**
38534** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
38535** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
38536** selection of the appropriate locking style based on the filesystem
38537** where the database is located.
38538*/
38539#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
38540# if defined(__APPLE__)
38541# define SQLITE_ENABLE_LOCKING_STYLE0 1
38542# else
38543# define SQLITE_ENABLE_LOCKING_STYLE0 0
38544# endif
38545#endif
38546
38547/* Use pread() and pwrite() if they are available */
38548#if defined(__APPLE__) || defined(__linux__1)
38549# define HAVE_PREAD1 1
38550# define HAVE_PWRITE1 1
38551#endif
38552#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
38553# undef USE_PREAD1
38554# define USE_PREAD64 1
38555#elif defined(HAVE_PREAD1) && defined(HAVE_PWRITE1)
38556# undef USE_PREAD64
38557# define USE_PREAD1 1
38558#endif
38559
38560/*
38561** standard include files.
38562*/
38563#include <sys/types.h> /* amalgamator: keep */
38564#include <sys/stat.h> /* amalgamator: keep */
38565#include <fcntl.h>
38566#include <sys/ioctl.h>
38567#include <unistd.h> /* amalgamator: keep */
38568/* #include <time.h> */
38569#include <sys/time.h> /* amalgamator: keep */
38570#include <errno(*__errno_location ()).h>
38571#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) \
38572 && !defined(SQLITE_WASI)
38573# include <sys/mman.h>
38574#endif
38575
38576#if SQLITE_ENABLE_LOCKING_STYLE0
38577/* # include <sys/ioctl.h> */
38578# include <sys/file.h>
38579# include <sys/param.h>
38580#endif /* SQLITE_ENABLE_LOCKING_STYLE */
38581
38582/*
38583** Try to determine if gethostuuid() is available based on standard
38584** macros. This might sometimes compute the wrong value for some
38585** obscure platforms. For those cases, simply compile with one of
38586** the following:
38587**
38588** -DHAVE_GETHOSTUUID=0
38589** -DHAVE_GETHOSTUUID=1
38590**
38591** None if this matters except when building on Apple products with
38592** -DSQLITE_ENABLE_LOCKING_STYLE.
38593*/
38594#ifndef HAVE_GETHOSTUUID0
38595# define HAVE_GETHOSTUUID0 0
38596# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
38597 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
38598# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
38599 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\
38600 && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0))
38601# undef HAVE_GETHOSTUUID0
38602# define HAVE_GETHOSTUUID0 1
38603# else
38604# warning "gethostuuid() is disabled."
38605# endif
38606# endif
38607#endif
38608
38609
38610#if OS_VXWORKS0
38611/* # include <sys/ioctl.h> */
38612# include <semaphore.h>
38613# include <limits.h>
38614#endif /* OS_VXWORKS */
38615
38616#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
38617# include <sys/mount.h>
38618#endif
38619
38620#ifdef HAVE_UTIME
38621# include <utime.h>
38622#endif
38623
38624/*
38625** Allowed values of unixFile.fsFlags
38626*/
38627#define SQLITE_FSFLAGS_IS_MSDOS0x1 0x1
38628
38629/*
38630** If we are to be thread-safe, include the pthreads header.
38631*/
38632#if SQLITE_THREADSAFE2
38633/* # include <pthread.h> */
38634#endif
38635
38636/*
38637** Default permissions when creating a new file
38638*/
38639#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS0644
38640# define SQLITE_DEFAULT_FILE_PERMISSIONS0644 0644
38641#endif
38642
38643/*
38644** Default permissions when creating auto proxy dir
38645*/
38646#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
38647# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755 0755
38648#endif
38649
38650/*
38651** Maximum supported path-length.
38652*/
38653#define MAX_PATHNAME512 512
38654
38655/*
38656** Maximum supported symbolic links
38657*/
38658#define SQLITE_MAX_SYMLINKS100 100
38659
38660/*
38661** Remove and stub certain info for WASI (WebAssembly System
38662** Interface) builds.
38663*/
38664#ifdef SQLITE_WASI
38665# undef HAVE_FCHMOD
38666# undef HAVE_FCHOWN1
38667# undef HAVE_MREMAP1
38668# define HAVE_MREMAP1 0
38669# ifndef SQLITE_DEFAULT_UNIX_VFS
38670# define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
38671 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
38672# endif
38673# ifndef F_RDLCK0
38674# define F_RDLCK0 0
38675# define F_WRLCK1 1
38676# define F_UNLCK2 2
38677# if __LONG_MAX == 0x7fffffffL
38678# define F_GETLK5 12
38679# define F_SETLK6 13
38680# define F_SETLKW7 14
38681# else
38682# define F_GETLK5 5
38683# define F_SETLK6 6
38684# define F_SETLKW7 7
38685# endif
38686# endif
38687#else /* !SQLITE_WASI */
38688# ifndef HAVE_FCHMOD
38689# define HAVE_FCHMOD
38690# endif
38691#endif /* SQLITE_WASI */
38692
38693#ifdef SQLITE_WASI
38694# define osGetpid(X)(pid_t)getpid() (pid_t)1
38695#else
38696/* Always cast the getpid() return type for compatibility with
38697** kernel modules in VxWorks. */
38698# define osGetpid(X)(pid_t)getpid() (pid_t)getpid()
38699#endif
38700
38701/*
38702** Only set the lastErrno if the error code is a real error and not
38703** a normal expected return code of SQLITE_BUSY or SQLITE_OK
38704*/
38705#define IS_LOCK_ERROR(x)((x != 0) && (x != 5)) ((x != SQLITE_OK0) && (x != SQLITE_BUSY5))
38706
38707/* Forward references */
38708typedef struct unixShm unixShm; /* Connection shared memory */
38709typedef struct unixShmNode unixShmNode; /* Shared memory instance */
38710typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
38711typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
38712
38713/*
38714** Sometimes, after a file handle is closed by SQLite, the file descriptor
38715** cannot be closed immediately. In these cases, instances of the following
38716** structure are used to store the file descriptor while waiting for an
38717** opportunity to either close or reuse it.
38718*/
38719struct UnixUnusedFd {
38720 int fd; /* File descriptor to close */
38721 int flags; /* Flags this file descriptor was opened with */
38722 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
38723};
38724
38725/*
38726** The unixFile structure is subclass of sqlite3_file specific to the unix
38727** VFS implementations.
38728*/
38729typedef struct unixFile unixFile;
38730struct unixFile {
38731 sqlite3_io_methods const *pMethod; /* Always the first entry */
38732 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
38733 unixInodeInfo *pInode; /* Info about locks on this inode */
38734 int h; /* The file descriptor */
38735 unsigned char eFileLock; /* The type of lock held on this fd */
38736 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
38737 int lastErrno; /* The unix errno from last I/O error */
38738 void *lockingContext; /* Locking style specific state */
38739 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
38740 const char *zPath; /* Name of the file */
38741 unixShm *pShm; /* Shared memory segment information */
38742 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
38743#if SQLITE_MAX_MMAP_SIZE20971520>0
38744 int nFetchOut; /* Number of outstanding xFetch refs */
38745 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
38746 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
38747 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
38748 void *pMapRegion; /* Memory mapped region */
38749#endif
38750 int sectorSize; /* Device sector size */
38751 int deviceCharacteristics; /* Precomputed device characteristics */
38752#if SQLITE_ENABLE_LOCKING_STYLE0
38753 int openFlags; /* The flags specified at open() */
38754#endif
38755#if SQLITE_ENABLE_LOCKING_STYLE0 || defined(__APPLE__)
38756 unsigned fsFlags; /* cached details from statfs() */
38757#endif
38758#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
38759 unsigned iBusyTimeout; /* Wait this many millisec on locks */
38760#endif
38761#if OS_VXWORKS0
38762 struct vxworksFileId *pId; /* Unique file ID */
38763#endif
38764#ifdef SQLITE_DEBUG
38765 /* The next group of variables are used to track whether or not the
38766 ** transaction counter in bytes 24-27 of database files are updated
38767 ** whenever any part of the database changes. An assertion fault will
38768 ** occur if a file is updated without also updating the transaction
38769 ** counter. This test is made to avoid new problems similar to the
38770 ** one described by ticket #3584.
38771 */
38772 unsigned char transCntrChng; /* True if the transaction counter changed */
38773 unsigned char dbUpdate; /* True if any part of database file changed */
38774 unsigned char inNormalWrite; /* True if in a normal write operation */
38775
38776#endif
38777
38778#ifdef SQLITE_TEST
38779 /* In test mode, increase the size of this structure a bit so that
38780 ** it is larger than the struct CrashFile defined in test6.c.
38781 */
38782 char aPadding[32];
38783#endif
38784};
38785
38786/* This variable holds the process id (pid) from when the xRandomness()
38787** method was called. If xOpen() is called from a different process id,
38788** indicating that a fork() has occurred, the PRNG will be reset.
38789*/
38790static pid_t randomnessPid = 0;
38791
38792/*
38793** Allowed values for the unixFile.ctrlFlags bitmask:
38794*/
38795#define UNIXFILE_EXCL0x01 0x01 /* Connections from one process only */
38796#define UNIXFILE_RDONLY0x02 0x02 /* Connection is read only */
38797#define UNIXFILE_PERSIST_WAL0x04 0x04 /* Persistent WAL mode */
38798#if !defined(SQLITE_DISABLE_DIRSYNC) && !defined(_AIX)
38799# define UNIXFILE_DIRSYNC0x08 0x08 /* Directory sync needed */
38800#else
38801# define UNIXFILE_DIRSYNC0x08 0x00
38802#endif
38803#define UNIXFILE_PSOW0x10 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
38804#define UNIXFILE_DELETE0x20 0x20 /* Delete on close */
38805#define UNIXFILE_URI0x40 0x40 /* Filename might have query parameters */
38806#define UNIXFILE_NOLOCK0x80 0x80 /* Do no file locking */
38807
38808/*
38809** Include code that is common to all os_*.c files
38810*/
38811/* #include "os_common.h" */
38812
38813/*
38814** Define various macros that are missing from some systems.
38815*/
38816#ifndef O_LARGEFILE0
38817# define O_LARGEFILE0 0
38818#endif
38819#ifdef SQLITE_DISABLE_LFS
38820# undef O_LARGEFILE0
38821# define O_LARGEFILE0 0
38822#endif
38823#ifndef O_NOFOLLOW0400000
38824# define O_NOFOLLOW0400000 0
38825#endif
38826#ifndef O_BINARY0
38827# define O_BINARY0 0
38828#endif
38829
38830/*
38831** The threadid macro resolves to the thread-id or to 0. Used for
38832** testing and debugging only.
38833*/
38834#if SQLITE_THREADSAFE2
38835#define threadidpthread_self() pthread_self()
38836#else
38837#define threadidpthread_self() 0
38838#endif
38839
38840/*
38841** HAVE_MREMAP defaults to true on Linux and false everywhere else.
38842*/
38843#if !defined(HAVE_MREMAP1)
38844# if defined(__linux__1) && defined(_GNU_SOURCE)
38845# define HAVE_MREMAP1 1
38846# else
38847# define HAVE_MREMAP1 0
38848# endif
38849#endif
38850
38851/*
38852** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
38853** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
38854*/
38855#ifdef __ANDROID__
38856# define lseek lseek64
38857#endif
38858
38859#ifdef __linux__1
38860/*
38861** Linux-specific IOCTL magic numbers used for controlling F2FS
38862*/
38863#define F2FS_IOCTL_MAGIC0xf5 0xf5
38864#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)))
38865#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)))
38866#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)))
38867#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)))
38868#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)))
38869#define F2FS_FEATURE_ATOMIC_WRITE0x0004 0x0004
38870#endif /* __linux__ */
38871
38872
38873/*
38874** Different Unix systems declare open() in different ways. Same use
38875** open(const char*,int,mode_t). Others use open(const char*,int,...).
38876** The difference is important when using a pointer to the function.
38877**
38878** The safest way to deal with the problem is to always use this wrapper
38879** which always has the same well-defined interface.
38880*/
38881static int posixOpen(const char *zFile, int flags, int mode){
38882 return open(zFile, flags, mode);
38883}
38884
38885/* Forward reference */
38886static int openDirectory(const char*, int*);
38887static int unixGetpagesize(void);
38888
38889/*
38890** Many system calls are accessed through pointer-to-functions so that
38891** they may be overridden at runtime to facilitate fault injection during
38892** testing and sandboxing. The following array holds the names and pointers
38893** to all overrideable system calls.
38894*/
38895static struct unix_syscall {
38896 const char *zName; /* Name of the system call */
38897 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
38898 sqlite3_syscall_ptr pDefault; /* Default value */
38899} aSyscall[] = {
38900 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
38901#define osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent) ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
38902
38903 { "close", (sqlite3_syscall_ptr)close, 0 },
38904#define osClose((int(*)(int))aSyscall[1].pCurrent) ((int(*)(int))aSyscall[1].pCurrent)
38905
38906 { "access", (sqlite3_syscall_ptr)access, 0 },
38907#define osAccess((int(*)(const char*,int))aSyscall[2].pCurrent) ((int(*)(const char*,int))aSyscall[2].pCurrent)
38908
38909 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
38910#define osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
38911
38912 { "stat", (sqlite3_syscall_ptr)stat, 0 },
38913#define osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
38914
38915/*
38916** The DJGPP compiler environment looks mostly like Unix, but it
38917** lacks the fcntl() system call. So redefine fcntl() to be something
38918** that always succeeds. This means that locking does not occur under
38919** DJGPP. But it is DOS - what did you expect?
38920*/
38921#ifdef __DJGPP__
38922 { "fstat", 0, 0 },
38923#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(a,b,c) 0
38924#else
38925 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
38926#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent) ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
38927#endif
38928
38929 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
38930#define osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent) ((int(*)(int,off_t))aSyscall[6].pCurrent)
38931
38932 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
38933#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) ((int(*)(int,int,...))aSyscall[7].pCurrent)
38934
38935 { "read", (sqlite3_syscall_ptr)read, 0 },
38936#define osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
38937
38938#if defined(USE_PREAD1) || SQLITE_ENABLE_LOCKING_STYLE0
38939 { "pread", (sqlite3_syscall_ptr)pread, 0 },
38940#else
38941 { "pread", (sqlite3_syscall_ptr)0, 0 },
38942#endif
38943#define osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
38944
38945#if defined(USE_PREAD64)
38946 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
38947#else
38948 { "pread64", (sqlite3_syscall_ptr)0, 0 },
38949#endif
38950#define osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
38951
38952 { "write", (sqlite3_syscall_ptr)write, 0 },
38953#define osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
38954
38955#if defined(USE_PREAD1) || SQLITE_ENABLE_LOCKING_STYLE0
38956 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
38957#else
38958 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
38959#endif
38960#define osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off_t))\
38961 aSyscall[12].pCurrent)
38962
38963#if defined(USE_PREAD64)
38964 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
38965#else
38966 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
38967#endif
38968#define osPwrite64((ssize_t(*)(int,const void*,size_t,off64_t)) aSyscall[13].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off64_t))\
38969 aSyscall[13].pCurrent)
38970
38971#if defined(HAVE_FCHMOD)
38972 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
38973#else
38974 { "fchmod", (sqlite3_syscall_ptr)0, 0 },
38975#endif
38976#define osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent) ((int(*)(int,mode_t))aSyscall[14].pCurrent)
38977
38978#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
38979 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
38980#else
38981 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
38982#endif
38983#define osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
38984
38985 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
38986#define osUnlink((int(*)(const char*))aSyscall[16].pCurrent) ((int(*)(const char*))aSyscall[16].pCurrent)
38987
38988 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
38989#define osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent) ((int(*)(const char*,int*))aSyscall[17].pCurrent)
38990
38991 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
38992#define osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent) ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
38993
38994 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
38995#define osRmdir((int(*)(const char*))aSyscall[19].pCurrent) ((int(*)(const char*))aSyscall[19].pCurrent)
38996
38997#if defined(HAVE_FCHOWN1)
38998 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
38999#else
39000 { "fchown", (sqlite3_syscall_ptr)0, 0 },
39001#endif
39002#define osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
39003
39004#if defined(HAVE_FCHOWN1)
39005 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
39006#else
39007 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
39008#endif
39009#define osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent) ((uid_t(*)(void))aSyscall[21].pCurrent)
39010
39011#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) \
39012 && !defined(SQLITE_WASI)
39013 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
39014#else
39015 { "mmap", (sqlite3_syscall_ptr)0, 0 },
39016#endif
39017#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)
39018
39019#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) \
39020 && !defined(SQLITE_WASI)
39021 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
39022#else
39023 { "munmap", (sqlite3_syscall_ptr)0, 0 },
39024#endif
39025#define osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent) ((int(*)(void*,size_t))aSyscall[23].pCurrent)
39026
39027#if HAVE_MREMAP1 && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
39028 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
39029#else
39030 { "mremap", (sqlite3_syscall_ptr)0, 0 },
39031#endif
39032#define osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
39033
39034#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
39035 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
39036#else
39037 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
39038#endif
39039#define osGetpagesize((int(*)(void))aSyscall[25].pCurrent) ((int(*)(void))aSyscall[25].pCurrent)
39040
39041#if defined(HAVE_READLINK1)
39042 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
39043#else
39044 { "readlink", (sqlite3_syscall_ptr)0, 0 },
39045#endif
39046#define osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
39047
39048#if defined(HAVE_LSTAT1)
39049 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
39050#else
39051 { "lstat", (sqlite3_syscall_ptr)0, 0 },
39052#endif
39053#define osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
39054
39055#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
39056# ifdef __ANDROID__
39057 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
39058#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
39059# else
39060 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
39061#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
39062# endif
39063#else
39064 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
39065#endif
39066
39067}; /* End of the overrideable system calls */
39068
39069
39070/*
39071** On some systems, calls to fchown() will trigger a message in a security
39072** log if they come from non-root processes. So avoid calling fchown() if
39073** we are not running as root.
39074*/
39075static int robustFchown(int fd, uid_t uid, gid_t gid){
39076#if defined(HAVE_FCHOWN1)
39077 return osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent)() ? 0 : osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)(fd,uid,gid);
39078#else
39079 return 0;
39080#endif
39081}
39082
39083/*
39084** This is the xSetSystemCall() method of sqlite3_vfs for all of the
39085** "unix" VFSes. Return SQLITE_OK upon successfully updating the
39086** system call pointer, or SQLITE_NOTFOUND if there is no configurable
39087** system call named zName.
39088*/
39089static int unixSetSystemCall(
39090 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
39091 const char *zName, /* Name of system call to override */
39092 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
39093){
39094 unsigned int i;
39095 int rc = SQLITE_NOTFOUND12;
39096
39097 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
39098 if( zName==0 ){
39099 /* If no zName is given, restore all system calls to their default
39100 ** settings and return NULL
39101 */
39102 rc = SQLITE_OK0;
39103 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
39104 if( aSyscall[i].pDefault ){
39105 aSyscall[i].pCurrent = aSyscall[i].pDefault;
39106 }
39107 }
39108 }else{
39109 /* If zName is specified, operate on only the one system call
39110 ** specified.
39111 */
39112 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
39113 if( strcmp(zName, aSyscall[i].zName)==0 ){
39114 if( aSyscall[i].pDefault==0 ){
39115 aSyscall[i].pDefault = aSyscall[i].pCurrent;
39116 }
39117 rc = SQLITE_OK0;
39118 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
39119 aSyscall[i].pCurrent = pNewFunc;
39120 break;
39121 }
39122 }
39123 }
39124 return rc;
39125}
39126
39127/*
39128** Return the value of a system call. Return NULL if zName is not a
39129** recognized system call name. NULL is also returned if the system call
39130** is currently undefined.
39131*/
39132static sqlite3_syscall_ptr unixGetSystemCall(
39133 sqlite3_vfs *pNotUsed,
39134 const char *zName
39135){
39136 unsigned int i;
39137
39138 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
39139 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
39140 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
39141 }
39142 return 0;
39143}
39144
39145/*
39146** Return the name of the first system call after zName. If zName==NULL
39147** then return the name of the first system call. Return NULL if zName
39148** is the last system call or if zName is not the name of a valid
39149** system call.
39150*/
39151static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
39152 int i = -1;
39153
39154 UNUSED_PARAMETER(p)(void)(p);
39155 if( zName ){
39156 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
39157 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
39158 }
39159 }
39160 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
39161 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
39162 }
39163 return 0;
39164}
39165
39166/*
39167** Do not accept any file descriptor less than this value, in order to avoid
39168** opening database file using file descriptors that are commonly used for
39169** standard input, output, and error.
39170*/
39171#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR3
39172# define SQLITE_MINIMUM_FILE_DESCRIPTOR3 3
39173#endif
39174
39175/*
39176** Invoke open(). Do so multiple times, until it either succeeds or
39177** fails for some reason other than EINTR.
39178**
39179** If the file creation mode "m" is 0 then set it to the default for
39180** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
39181** 0644) as modified by the system umask. If m is not 0, then
39182** make the file creation mode be exactly m ignoring the umask.
39183**
39184** The m parameter will be non-zero only when creating -wal, -journal,
39185** and -shm files. We want those files to have *exactly* the same
39186** permissions as their original database, unadulterated by the umask.
39187** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
39188** transaction crashes and leaves behind hot journals, then any
39189** process that is able to write to the database will also be able to
39190** recover the hot journals.
39191*/
39192static int robust_open(const char *z, int f, mode_t m){
39193 int fd;
39194 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS0644;
39195 while(1){
39196#if defined(O_CLOEXEC02000000)
39197 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f|O_CLOEXEC02000000,m2);
39198#else
39199 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f,m2);
39200#endif
39201 if( fd<0 ){
39202 if( errno(*__errno_location ())==EINTR4 ) continue;
39203 break;
39204 }
39205 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR3 ) break;
39206 if( (f & (O_EXCL0200|O_CREAT0100))==(O_EXCL0200|O_CREAT0100) ){
39207 (void)osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(z);
39208 }
39209 osClose((int(*)(int))aSyscall[1].pCurrent)(fd);
39210 sqlite3_log(SQLITE_WARNING28,
39211 "attempt to open \"%s\" as file descriptor %d", z, fd);
39212 fd = -1;
39213 if( osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)("/dev/null", O_RDONLY00, m)<0 ) break;
39214 }
39215 if( fd>=0 ){
39216 if( m!=0 ){
39217 struct stat statbuf;
39218 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf)==0
39219 && statbuf.st_size==0
39220 && (statbuf.st_mode&0777)!=m
39221 ){
39222 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(fd, m);
39223 }
39224 }
39225#if defined(FD_CLOEXEC1) && (!defined(O_CLOEXEC02000000) || O_CLOEXEC02000000==0)
39226 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_SETFD2, osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETFD1, 0) | FD_CLOEXEC1);
39227#endif
39228 }
39229 return fd;
39230}
39231
39232/*
39233** Helper functions to obtain and relinquish the global mutex. The
39234** global mutex is used to protect the unixInodeInfo and
39235** vxworksFileId objects used by this file, all of which may be
39236** shared by multiple threads.
39237**
39238** Function unixMutexHeld() is used to assert() that the global mutex
39239** is held when required. This function is only used as part of assert()
39240** statements. e.g.
39241**
39242** unixEnterMutex()
39243** assert( unixMutexHeld() );
39244** unixEnterLeave()
39245**
39246** To prevent deadlock, the global unixBigLock must must be acquired
39247** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
39248** OK to get the pLockMutex without holding unixBigLock first, but if
39249** that happens, the unixBigLock mutex must not be acquired until after
39250** pLockMutex is released.
39251**
39252** OK: enter(unixBigLock), enter(pLockInfo)
39253** OK: enter(unixBigLock)
39254** OK: enter(pLockInfo)
39255** ERROR: enter(pLockInfo), enter(unixBigLock)
39256*/
39257static sqlite3_mutex *unixBigLock = 0;
39258static void unixEnterMutex(void){
39259 assert( sqlite3_mutex_notheld(unixBigLock) )((void) (0)); /* Not a recursive mutex */
39260 sqlite3_mutex_enter(unixBigLock);
39261}
39262static void unixLeaveMutex(void){
39263 assert( sqlite3_mutex_held(unixBigLock) )((void) (0));
39264 sqlite3_mutex_leave(unixBigLock);
39265}
39266#ifdef SQLITE_DEBUG
39267static int unixMutexHeld(void) {
39268 return sqlite3_mutex_held(unixBigLock);
39269}
39270#endif
39271
39272
39273#ifdef SQLITE_HAVE_OS_TRACE
39274/*
39275** Helper function for printing out trace information from debugging
39276** binaries. This returns the string representation of the supplied
39277** integer lock-type.
39278*/
39279static const char *azFileLock(int eFileLock){
39280 switch( eFileLock ){
39281 case NO_LOCK0: return "NONE";
39282 case SHARED_LOCK1: return "SHARED";
39283 case RESERVED_LOCK2: return "RESERVED";
39284 case PENDING_LOCK3: return "PENDING";
39285 case EXCLUSIVE_LOCK4: return "EXCLUSIVE";
39286 }
39287 return "ERROR";
39288}
39289#endif
39290
39291#ifdef SQLITE_LOCK_TRACE
39292/*
39293** Print out information about all locking operations.
39294**
39295** This routine is used for troubleshooting locks on multithreaded
39296** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
39297** command-line option on the compiler. This code is normally
39298** turned off.
39299*/
39300static int lockTrace(int fd, int op, struct flock *p){
39301 char *zOpName, *zType;
39302 int s;
39303 int savedErrno;
39304 if( op==F_GETLK5 ){
39305 zOpName = "GETLK";
39306 }else if( op==F_SETLK6 ){
39307 zOpName = "SETLK";
39308 }else{
39309 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
39310 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
39311 return s;
39312 }
39313 if( p->l_type==F_RDLCK0 ){
39314 zType = "RDLCK";
39315 }else if( p->l_type==F_WRLCK1 ){
39316 zType = "WRLCK";
39317 }else if( p->l_type==F_UNLCK2 ){
39318 zType = "UNLCK";
39319 }else{
39320 assert( 0 )((void) (0));
39321 }
39322 assert( p->l_whence==SEEK_SET )((void) (0));
39323 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
39324 savedErrno = errno(*__errno_location ());
39325 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
39326 threadidpthread_self(), fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
39327 (int)p->l_pid, s);
39328 if( s==(-1) && op==F_SETLK6 && (p->l_type==F_RDLCK0 || p->l_type==F_WRLCK1) ){
39329 struct flock l2;
39330 l2 = *p;
39331 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETLK5, &l2);
39332 if( l2.l_type==F_RDLCK0 ){
39333 zType = "RDLCK";
39334 }else if( l2.l_type==F_WRLCK1 ){
39335 zType = "WRLCK";
39336 }else if( l2.l_type==F_UNLCK2 ){
39337 zType = "UNLCK";
39338 }else{
39339 assert( 0 )((void) (0));
39340 }
39341 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
39342 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
39343 }
39344 errno(*__errno_location ()) = savedErrno;
39345 return s;
39346}
39347#undef osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)
39348#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) lockTrace
39349#endif /* SQLITE_LOCK_TRACE */
39350
39351/*
39352** Retry ftruncate() calls that fail due to EINTR
39353**
39354** All calls to ftruncate() within this file should be made through
39355** this wrapper. On the Android platform, bypassing the logic below
39356** could lead to a corrupt database.
39357*/
39358static int robust_ftruncate(int h, sqlite3_int64 sz){
39359 int rc;
39360#ifdef __ANDROID__
39361 /* On Android, ftruncate() always uses 32-bit offsets, even if
39362 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
39363 ** truncate a file to any size larger than 2GiB. Silently ignore any
39364 ** such attempts. */
39365 if( sz>(sqlite3_int64)0x7FFFFFFF ){
39366 rc = SQLITE_OK0;
39367 }else
39368#endif
39369 do{ rc = osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent)(h,sz); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
39370 return rc;
39371}
39372
39373/*
39374** This routine translates a standard POSIX errno code into something
39375** useful to the clients of the sqlite3 functions. Specifically, it is
39376** intended to translate a variety of "try again" errors into SQLITE_BUSY
39377** and a variety of "please close the file descriptor NOW" errors into
39378** SQLITE_IOERR
39379**
39380** Errors during initialization of locks, or file system support for locks,
39381** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
39382*/
39383static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
39384 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||((void) (0))
39385 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||((void) (0))
39386 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||((void) (0))
39387 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) )((void) (0));
39388 switch (posixError) {
39389 case EACCES13:
39390 case EAGAIN11:
39391 case ETIMEDOUT110:
39392 case EBUSY16:
39393 case EINTR4:
39394 case ENOLCK37:
39395 /* random NFS retry error, unless during file system support
39396 * introspection, in which it actually means what it says */
39397 return SQLITE_BUSY5;
39398
39399 case EPERM1:
39400 return SQLITE_PERM3;
39401
39402 default:
39403 return sqliteIOErr;
39404 }
39405}
39406
39407
39408/******************************************************************************
39409****************** Begin Unique File ID Utility Used By VxWorks ***************
39410**
39411** On most versions of unix, we can get a unique ID for a file by concatenating
39412** the device number and the inode number. But this does not work on VxWorks.
39413** On VxWorks, a unique file id must be based on the canonical filename.
39414**
39415** A pointer to an instance of the following structure can be used as a
39416** unique file ID in VxWorks. Each instance of this structure contains
39417** a copy of the canonical filename. There is also a reference count.
39418** The structure is reclaimed when the number of pointers to it drops to
39419** zero.
39420**
39421** There are never very many files open at one time and lookups are not
39422** a performance-critical path, so it is sufficient to put these
39423** structures on a linked list.
39424*/
39425struct vxworksFileId {
39426 struct vxworksFileId *pNext; /* Next in a list of them all */
39427 int nRef; /* Number of references to this one */
39428 int nName; /* Length of the zCanonicalName[] string */
39429 char *zCanonicalName; /* Canonical filename */
39430};
39431
39432#if OS_VXWORKS0
39433/*
39434** All unique filenames are held on a linked list headed by this
39435** variable:
39436*/
39437static struct vxworksFileId *vxworksFileList = 0;
39438
39439/*
39440** Simplify a filename into its canonical form
39441** by making the following changes:
39442**
39443** * removing any trailing and duplicate /
39444** * convert /./ into just /
39445** * convert /A/../ where A is any simple name into just /
39446**
39447** Changes are made in-place. Return the new name length.
39448**
39449** The original filename is in z[0..n-1]. Return the number of
39450** characters in the simplified name.
39451*/
39452static int vxworksSimplifyName(char *z, int n){
39453 int i, j;
39454 while( n>1 && z[n-1]=='/' ){ n--; }
39455 for(i=j=0; i<n; i++){
39456 if( z[i]=='/' ){
39457 if( z[i+1]=='/' ) continue;
39458 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
39459 i += 1;
39460 continue;
39461 }
39462 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
39463 while( j>0 && z[j-1]!='/' ){ j--; }
39464 if( j>0 ){ j--; }
39465 i += 2;
39466 continue;
39467 }
39468 }
39469 z[j++] = z[i];
39470 }
39471 z[j] = 0;
39472 return j;
39473}
39474
39475/*
39476** Find a unique file ID for the given absolute pathname. Return
39477** a pointer to the vxworksFileId object. This pointer is the unique
39478** file ID.
39479**
39480** The nRef field of the vxworksFileId object is incremented before
39481** the object is returned. A new vxworksFileId object is created
39482** and added to the global list if necessary.
39483**
39484** If a memory allocation error occurs, return NULL.
39485*/
39486static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
39487 struct vxworksFileId *pNew; /* search key and new file ID */
39488 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
39489 int n; /* Length of zAbsoluteName string */
39490
39491 assert( zAbsoluteName[0]=='/' )((void) (0));
39492 n = (int)strlen(zAbsoluteName);
39493 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
39494 if( pNew==0 ) return 0;
39495 pNew->zCanonicalName = (char*)&pNew[1];
39496 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
39497 n = vxworksSimplifyName(pNew->zCanonicalName, n);
39498
39499 /* Search for an existing entry that matching the canonical name.
39500 ** If found, increment the reference count and return a pointer to
39501 ** the existing file ID.
39502 */
39503 unixEnterMutex();
39504 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
39505 if( pCandidate->nName==n
39506 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
39507 ){
39508 sqlite3_free(pNew);
39509 pCandidate->nRef++;
39510 unixLeaveMutex();
39511 return pCandidate;
39512 }
39513 }
39514
39515 /* No match was found. We will make a new file ID */
39516 pNew->nRef = 1;
39517 pNew->nName = n;
39518 pNew->pNext = vxworksFileList;
39519 vxworksFileList = pNew;
39520 unixLeaveMutex();
39521 return pNew;
39522}
39523
39524/*
39525** Decrement the reference count on a vxworksFileId object. Free
39526** the object when the reference count reaches zero.
39527*/
39528static void vxworksReleaseFileId(struct vxworksFileId *pId){
39529 unixEnterMutex();
39530 assert( pId->nRef>0 )((void) (0));
39531 pId->nRef--;
39532 if( pId->nRef==0 ){
39533 struct vxworksFileId **pp;
39534 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
39535 assert( *pp==pId )((void) (0));
39536 *pp = pId->pNext;
39537 sqlite3_free(pId);
39538 }
39539 unixLeaveMutex();
39540}
39541#endif /* OS_VXWORKS */
39542/*************** End of Unique File ID Utility Used By VxWorks ****************
39543******************************************************************************/
39544
39545
39546/******************************************************************************
39547*************************** Posix Advisory Locking ****************************
39548**
39549** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
39550** section 6.5.2.2 lines 483 through 490 specify that when a process
39551** sets or clears a lock, that operation overrides any prior locks set
39552** by the same process. It does not explicitly say so, but this implies
39553** that it overrides locks set by the same process using a different
39554** file descriptor. Consider this test case:
39555**
39556** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
39557** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
39558**
39559** Suppose ./file1 and ./file2 are really the same file (because
39560** one is a hard or symbolic link to the other) then if you set
39561** an exclusive lock on fd1, then try to get an exclusive lock
39562** on fd2, it works. I would have expected the second lock to
39563** fail since there was already a lock on the file due to fd1.
39564** But not so. Since both locks came from the same process, the
39565** second overrides the first, even though they were on different
39566** file descriptors opened on different file names.
39567**
39568** This means that we cannot use POSIX locks to synchronize file access
39569** among competing threads of the same process. POSIX locks will work fine
39570** to synchronize access for threads in separate processes, but not
39571** threads within the same process.
39572**
39573** To work around the problem, SQLite has to manage file locks internally
39574** on its own. Whenever a new database is opened, we have to find the
39575** specific inode of the database file (the inode is determined by the
39576** st_dev and st_ino fields of the stat structure that fstat() fills in)
39577** and check for locks already existing on that inode. When locks are
39578** created or removed, we have to look at our own internal record of the
39579** locks to see if another thread has previously set a lock on that same
39580** inode.
39581**
39582** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
39583** For VxWorks, we have to use the alternative unique ID system based on
39584** canonical filename and implemented in the previous division.)
39585**
39586** The sqlite3_file structure for POSIX is no longer just an integer file
39587** descriptor. It is now a structure that holds the integer file
39588** descriptor and a pointer to a structure that describes the internal
39589** locks on the corresponding inode. There is one locking structure
39590** per inode, so if the same inode is opened twice, both unixFile structures
39591** point to the same locking structure. The locking structure keeps
39592** a reference count (so we will know when to delete it) and a "cnt"
39593** field that tells us its internal lock status. cnt==0 means the
39594** file is unlocked. cnt==-1 means the file has an exclusive lock.
39595** cnt>0 means there are cnt shared locks on the file.
39596**
39597** Any attempt to lock or unlock a file first checks the locking
39598** structure. The fcntl() system call is only invoked to set a
39599** POSIX lock if the internal lock structure transitions between
39600** a locked and an unlocked state.
39601**
39602** But wait: there are yet more problems with POSIX advisory locks.
39603**
39604** If you close a file descriptor that points to a file that has locks,
39605** all locks on that file that are owned by the current process are
39606** released. To work around this problem, each unixInodeInfo object
39607** maintains a count of the number of pending locks on the inode.
39608** When an attempt is made to close an unixFile, if there are
39609** other unixFile open on the same inode that are holding locks, the call
39610** to close() the file descriptor is deferred until all of the locks clear.
39611** The unixInodeInfo structure keeps a list of file descriptors that need to
39612** be closed and that list is walked (and cleared) when the last lock
39613** clears.
39614**
39615** Yet another problem: LinuxThreads do not play well with posix locks.
39616**
39617** Many older versions of linux use the LinuxThreads library which is
39618** not posix compliant. Under LinuxThreads, a lock created by thread
39619** A cannot be modified or overridden by a different thread B.
39620** Only thread A can modify the lock. Locking behavior is correct
39621** if the application uses the newer Native Posix Thread Library (NPTL)
39622** on linux - with NPTL a lock created by thread A can override locks
39623** in thread B. But there is no way to know at compile-time which
39624** threading library is being used. So there is no way to know at
39625** compile-time whether or not thread A can override locks on thread B.
39626** One has to do a run-time check to discover the behavior of the
39627** current process.
39628**
39629** SQLite used to support LinuxThreads. But support for LinuxThreads
39630** was dropped beginning with version 3.7.0. SQLite will still work with
39631** LinuxThreads provided that (1) there is no more than one connection
39632** per database file in the same process and (2) database connections
39633** do not move across threads.
39634*/
39635
39636/*
39637** An instance of the following structure serves as the key used
39638** to locate a particular unixInodeInfo object.
39639*/
39640struct unixFileId {
39641 dev_t dev; /* Device number */
39642#if OS_VXWORKS0
39643 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
39644#else
39645 /* We are told that some versions of Android contain a bug that
39646 ** sizes ino_t at only 32-bits instead of 64-bits. (See
39647 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
39648 ** To work around this, always allocate 64-bits for the inode number.
39649 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
39650 ** but that should not be a big deal. */
39651 /* WAS: ino_t ino; */
39652 u64 ino; /* Inode number */
39653#endif
39654};
39655
39656/*
39657** An instance of the following structure is allocated for each open
39658** inode.
39659**
39660** A single inode can have multiple file descriptors, so each unixFile
39661** structure contains a pointer to an instance of this object and this
39662** object keeps a count of the number of unixFile pointing to it.
39663**
39664** Mutex rules:
39665**
39666** (1) Only the pLockMutex mutex must be held in order to read or write
39667** any of the locking fields:
39668** nShared, nLock, eFileLock, bProcessLock, pUnused
39669**
39670** (2) When nRef>0, then the following fields are unchanging and can
39671** be read (but not written) without holding any mutex:
39672** fileId, pLockMutex
39673**
39674** (3) With the exceptions above, all the fields may only be read
39675** or written while holding the global unixBigLock mutex.
39676**
39677** Deadlock prevention: The global unixBigLock mutex may not
39678** be acquired while holding the pLockMutex mutex. If both unixBigLock
39679** and pLockMutex are needed, then unixBigLock must be acquired first.
39680*/
39681struct unixInodeInfo {
39682 struct unixFileId fileId; /* The lookup key */
39683 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
39684 int nShared; /* Number of SHARED locks held */
39685 int nLock; /* Number of outstanding file locks */
39686 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
39687 unsigned char bProcessLock; /* An exclusive process lock is held */
39688 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
39689 int nRef; /* Number of pointers to this structure */
39690 unixShmNode *pShmNode; /* Shared memory associated with this inode */
39691 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
39692 unixInodeInfo *pPrev; /* .... doubly linked */
39693#if SQLITE_ENABLE_LOCKING_STYLE0
39694 unsigned long long sharedByte; /* for AFP simulated shared lock */
39695#endif
39696#if OS_VXWORKS0
39697 sem_t *pSem; /* Named POSIX semaphore */
39698 char aSemName[MAX_PATHNAME512+2]; /* Name of that semaphore */
39699#endif
39700};
39701
39702/*
39703** A lists of all unixInodeInfo objects.
39704**
39705** Must hold unixBigLock in order to read or write this variable.
39706*/
39707static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
39708
39709#ifdef SQLITE_DEBUG
39710/*
39711** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
39712** This routine is used only within assert() to help verify correct mutex
39713** usage.
39714*/
39715int unixFileMutexHeld(unixFile *pFile){
39716 assert( pFile->pInode )((void) (0));
39717 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
39718}
39719int unixFileMutexNotheld(unixFile *pFile){
39720 assert( pFile->pInode )((void) (0));
39721 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
39722}
39723#endif
39724
39725/*
39726**
39727** This function - unixLogErrorAtLine(), is only ever called via the macro
39728** unixLogError().
39729**
39730** It is invoked after an error occurs in an OS function and errno has been
39731** set. It logs a message using sqlite3_log() containing the current value of
39732** errno and, if possible, the human-readable equivalent from strerror() or
39733** strerror_r().
39734**
39735** The first argument passed to the macro should be the error code that
39736** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
39737** The two subsequent arguments should be the name of the OS function that
39738** failed (e.g. "unlink", "open") and the associated file-system path,
39739** if any.
39740*/
39741#define unixLogError(a,b,c)unixLogErrorAtLine(a,b,c,39741) unixLogErrorAtLine(a,b,c,__LINE__39741)
39742static int unixLogErrorAtLine(
39743 int errcode, /* SQLite error code */
39744 const char *zFunc, /* Name of OS function that failed */
39745 const char *zPath, /* File path associated with error */
39746 int iLine /* Source line number where error occurred */
39747){
39748 char *zErr; /* Message from strerror() or equivalent */
39749 int iErrno = errno(*__errno_location ()); /* Saved syscall error number */
39750
39751 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
39752 ** the strerror() function to obtain the human-readable error message
39753 ** equivalent to errno. Otherwise, use strerror_r().
39754 */
39755#if SQLITE_THREADSAFE2 && defined(HAVE_STRERROR_R)
39756 char aErr[80];
39757 memset(aErr, 0, sizeof(aErr));
39758 zErr = aErr;
39759
39760 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
39761 ** assume that the system provides the GNU version of strerror_r() that
39762 ** returns a pointer to a buffer containing the error message. That pointer
39763 ** may point to aErr[], or it may point to some static storage somewhere.
39764 ** Otherwise, assume that the system provides the POSIX version of
39765 ** strerror_r(), which always writes an error message into aErr[].
39766 **
39767 ** If the code incorrectly assumes that it is the POSIX version that is
39768 ** available, the error message will often be an empty string. Not a
39769 ** huge problem. Incorrectly concluding that the GNU version is available
39770 ** could lead to a segfault though.
39771 **
39772 ** Forum post 3f13857fa4062301 reports that the Android SDK may use
39773 ** int-type return, depending on its version.
39774 */
39775#if (defined(STRERROR_R_CHAR_P) || defined(__USE_GNU1)) \
39776 && !defined(ANDROID) && !defined(__ANDROID__)
39777 zErr =
39778# endif
39779 strerror_r(iErrno, aErr, sizeof(aErr)-1);
39780
39781#elif SQLITE_THREADSAFE2
39782 /* This is a threadsafe build, but strerror_r() is not available. */
39783 zErr = "";
39784#else
39785 /* Non-threadsafe build, use strerror(). */
39786 zErr = strerror(iErrno);
39787#endif
39788
39789 if( zPath==0 ) zPath = "";
39790 sqlite3_log(errcode,
39791 "os_unix.c:%d: (%d) %s(%s) - %s",
39792 iLine, iErrno, zFunc, zPath, zErr
39793 );
39794
39795 return errcode;
39796}
39797
39798/*
39799** Close a file descriptor.
39800**
39801** We assume that close() almost always works, since it is only in a
39802** very sick application or on a very sick platform that it might fail.
39803** If it does fail, simply leak the file descriptor, but do log the
39804** error.
39805**
39806** Note that it is not safe to retry close() after EINTR since the
39807** file descriptor might have already been reused by another thread.
39808** So we don't even try to recover from an EINTR. Just log the error
39809** and move on.
39810*/
39811static void robust_close(unixFile *pFile, int h, int lineno){
39812 if( osClose((int(*)(int))aSyscall[1].pCurrent)(h) ){
39813 unixLogErrorAtLine(SQLITE_IOERR_CLOSE(10 | (16<<8)), "close",
39814 pFile ? pFile->zPath : 0, lineno);
39815 }
39816}
39817
39818/*
39819** Set the pFile->lastErrno. Do this in a subroutine as that provides
39820** a convenient place to set a breakpoint.
39821*/
39822static void storeLastErrno(unixFile *pFile, int error){
39823 pFile->lastErrno = error;
39824}
39825
39826/*
39827** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
39828*/
39829static void closePendingFds(unixFile *pFile){
39830 unixInodeInfo *pInode = pFile->pInode;
39831 UnixUnusedFd *p;
39832 UnixUnusedFd *pNext;
39833 assert( unixFileMutexHeld(pFile) )((void) (0));
39834 for(p=pInode->pUnused; p; p=pNext){
39835 pNext = p->pNext;
39836 robust_close(pFile, p->fd, __LINE__39836);
39837 sqlite3_free(p);
39838 }
39839 pInode->pUnused = 0;
39840}
39841
39842/*
39843** Release a unixInodeInfo structure previously allocated by findInodeInfo().
39844**
39845** The global mutex must be held when this routine is called, but the mutex
39846** on the inode being deleted must NOT be held.
39847*/
39848static void releaseInodeInfo(unixFile *pFile){
39849 unixInodeInfo *pInode = pFile->pInode;
39850 assert( unixMutexHeld() )((void) (0));
39851 assert( unixFileMutexNotheld(pFile) )((void) (0));
39852 if( ALWAYS(pInode)(pInode) ){
39853 pInode->nRef--;
39854 if( pInode->nRef==0 ){
39855 assert( pInode->pShmNode==0 )((void) (0));
39856 sqlite3_mutex_enter(pInode->pLockMutex);
39857 closePendingFds(pFile);
39858 sqlite3_mutex_leave(pInode->pLockMutex);
39859 if( pInode->pPrev ){
39860 assert( pInode->pPrev->pNext==pInode )((void) (0));
39861 pInode->pPrev->pNext = pInode->pNext;
39862 }else{
39863 assert( inodeList==pInode )((void) (0));
39864 inodeList = pInode->pNext;
39865 }
39866 if( pInode->pNext ){
39867 assert( pInode->pNext->pPrev==pInode )((void) (0));
39868 pInode->pNext->pPrev = pInode->pPrev;
39869 }
39870 sqlite3_mutex_free(pInode->pLockMutex);
39871 sqlite3_free(pInode);
39872 }
39873 }
39874}
39875
39876/*
39877** Given a file descriptor, locate the unixInodeInfo object that
39878** describes that file descriptor. Create a new one if necessary. The
39879** return value might be uninitialized if an error occurs.
39880**
39881** The global mutex must held when calling this routine.
39882**
39883** Return an appropriate error code.
39884*/
39885static int findInodeInfo(
39886 unixFile *pFile, /* Unix file with file desc used in the key */
39887 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
39888){
39889 int rc; /* System call return code */
39890 int fd; /* The file descriptor for pFile */
39891 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
39892 struct stat statbuf; /* Low-level file information */
39893 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
39894
39895 assert( unixMutexHeld() )((void) (0));
39896
39897 /* Get low-level information about the file that we can used to
39898 ** create a unique name for the file.
39899 */
39900 fd = pFile->h;
39901 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
39902 if( rc!=0 ){
39903 storeLastErrno(pFile, errno(*__errno_location ()));
39904#if defined(EOVERFLOW75) && defined(SQLITE_DISABLE_LFS)
39905 if( pFile->lastErrno==EOVERFLOW75 ) return SQLITE_NOLFS22;
39906#endif
39907 return SQLITE_IOERR10;
39908 }
39909
39910#ifdef __APPLE__
39911 /* On OS X on an msdos filesystem, the inode number is reported
39912 ** incorrectly for zero-size files. See ticket #3260. To work
39913 ** around this problem (we consider it a bug in OS X, not SQLite)
39914 ** we always increase the file size to 1 by writing a single byte
39915 ** prior to accessing the inode number. The one byte written is
39916 ** an ASCII 'S' character which also happens to be the first byte
39917 ** in the header of every SQLite database. In this way, if there
39918 ** is a race condition such that another thread has already populated
39919 ** the first page of the database, no damage is done.
39920 */
39921 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS0x1)!=0 ){
39922 do{ rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, "S", 1); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
39923 if( rc!=1 ){
39924 storeLastErrno(pFile, errno(*__errno_location ()));
39925 return SQLITE_IOERR10;
39926 }
39927 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
39928 if( rc!=0 ){
39929 storeLastErrno(pFile, errno(*__errno_location ()));
39930 return SQLITE_IOERR10;
39931 }
39932 }
39933#endif
39934
39935 memset(&fileId, 0, sizeof(fileId));
39936 fileId.dev = statbuf.st_dev;
39937#if OS_VXWORKS0
39938 fileId.pId = pFile->pId;
39939#else
39940 fileId.ino = (u64)statbuf.st_ino;
39941#endif
39942 assert( unixMutexHeld() )((void) (0));
39943 pInode = inodeList;
39944 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
39945 pInode = pInode->pNext;
39946 }
39947 if( pInode==0 ){
39948 pInode = sqlite3_malloc64( sizeof(*pInode) );
39949 if( pInode==0 ){
39950 return SQLITE_NOMEM_BKPT7;
39951 }
39952 memset(pInode, 0, sizeof(*pInode));
39953 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
39954 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
39955 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
39956 if( pInode->pLockMutex==0 ){
39957 sqlite3_free(pInode);
39958 return SQLITE_NOMEM_BKPT7;
39959 }
39960 }
39961 pInode->nRef = 1;
39962 assert( unixMutexHeld() )((void) (0));
39963 pInode->pNext = inodeList;
39964 pInode->pPrev = 0;
39965 if( inodeList ) inodeList->pPrev = pInode;
39966 inodeList = pInode;
39967 }else{
39968 pInode->nRef++;
39969 }
39970 *ppInode = pInode;
39971 return SQLITE_OK0;
39972}
39973
39974/*
39975** Return TRUE if pFile has been renamed or unlinked since it was first opened.
39976*/
39977static int fileHasMoved(unixFile *pFile){
39978#if OS_VXWORKS0
39979 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
39980#else
39981 struct stat buf;
39982 return pFile->pInode!=0 &&
39983 (osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pFile->zPath, &buf)!=0
39984 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
39985#endif
39986}
39987
39988
39989/*
39990** Check a unixFile that is a database. Verify the following:
39991**
39992** (1) There is exactly one hard link on the file
39993** (2) The file is not a symbolic link
39994** (3) The file has not been renamed or unlinked
39995**
39996** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
39997*/
39998static void verifyDbFile(unixFile *pFile){
39999 struct stat buf;
40000 int rc;
40001
40002 /* These verifications occurs for the main database only */
40003 if( pFile->ctrlFlags & UNIXFILE_NOLOCK0x80 ) return;
40004
40005 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
40006 if( rc!=0 ){
40007 sqlite3_log(SQLITE_WARNING28, "cannot fstat db file %s", pFile->zPath);
40008 return;
40009 }
40010 if( buf.st_nlink==0 ){
40011 sqlite3_log(SQLITE_WARNING28, "file unlinked while open: %s", pFile->zPath);
40012 return;
40013 }
40014 if( buf.st_nlink>1 ){
40015 sqlite3_log(SQLITE_WARNING28, "multiple links to file: %s", pFile->zPath);
40016 return;
40017 }
40018 if( fileHasMoved(pFile) ){
40019 sqlite3_log(SQLITE_WARNING28, "file renamed while open: %s", pFile->zPath);
40020 return;
40021 }
40022}
40023
40024
40025/*
40026** This routine checks if there is a RESERVED lock held on the specified
40027** file by this or any other process. If such a lock is held, set *pResOut
40028** to a non-zero value otherwise *pResOut is set to zero. The return value
40029** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40030*/
40031static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
40032 int rc = SQLITE_OK0;
40033 int reserved = 0;
40034 unixFile *pFile = (unixFile*)id;
40035
40036 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40037
40038 assert( pFile )((void) (0));
40039 assert( pFile->eFileLock<=SHARED_LOCK )((void) (0));
40040 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
40041
40042 /* Check if a thread in this process holds such a lock */
40043 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
40044 reserved = 1;
40045 }
40046
40047 /* Otherwise see if some other process holds it.
40048 */
40049#ifndef __DJGPP__
40050 if( !reserved && !pFile->pInode->bProcessLock ){
40051 struct flock lock;
40052 lock.l_whence = SEEK_SET0;
40053 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
40054 lock.l_len = 1;
40055 lock.l_type = F_WRLCK1;
40056 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h, F_GETLK5, &lock) ){
40057 rc = SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8));
40058 storeLastErrno(pFile, errno(*__errno_location ()));
40059 } else if( lock.l_type!=F_UNLCK2 ){
40060 reserved = 1;
40061 }
40062 }
40063#endif
40064
40065 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
40066 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
40067
40068 *pResOut = reserved;
40069 return rc;
40070}
40071
40072/* Forward declaration*/
40073static int unixSleep(sqlite3_vfs*,int);
40074
40075/*
40076** Set a posix-advisory-lock.
40077**
40078** There are two versions of this routine. If compiled with
40079** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
40080** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
40081** value is set, then it is the number of milliseconds to wait before
40082** failing the lock. The iBusyTimeout value is always reset back to
40083** zero on each call.
40084**
40085** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
40086** attempt to set the lock.
40087*/
40088#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
40089# 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)
40090#else
40091static int osSetPosixAdvisoryLock(((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40092 int h, /* The file descriptor on which to take the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40093 struct flock *pLock, /* The description of the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40094 unixFile *pFile /* Structure holding timeout value */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
40095)((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
{
40096 int tm = pFile->iBusyTimeout;
40097 int rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
40098 while( rc<0 && tm>0 ){
40099 /* On systems that support some kind of blocking file lock with a timeout,
40100 ** make appropriate changes here to invoke that blocking file lock. On
40101 ** generic posix, however, there is no such API. So we simply try the
40102 ** lock once every millisecond until either the timeout expires, or until
40103 ** the lock is obtained. */
40104 unixSleep(0,1000);
40105 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
40106 tm--;
40107 }
40108 return rc;
40109}
40110#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
40111
40112
40113/*
40114** Attempt to set a system-lock on the file pFile. The lock is
40115** described by pLock.
40116**
40117** If the pFile was opened read/write from unix-excl, then the only lock
40118** ever obtained is an exclusive lock, and it is obtained exactly once
40119** the first time any lock is attempted. All subsequent system locking
40120** operations become no-ops. Locking operations still happen internally,
40121** in order to coordinate access between separate database connections
40122** within this process, but all of that is handled in memory and the
40123** operating system does not participate.
40124**
40125** This function is a pass-through to fcntl(F_SETLK) if pFile is using
40126** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
40127** and is read-only.
40128**
40129** Zero is returned if the call completes successfully, or -1 if a call
40130** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
40131*/
40132static int unixFileLock(unixFile *pFile, struct flock *pLock){
40133 int rc;
40134 unixInodeInfo *pInode = pFile->pInode;
40135 assert( pInode!=0 )((void) (0));
40136 assert( sqlite3_mutex_held(pInode->pLockMutex) )((void) (0));
40137 if( (pFile->ctrlFlags & (UNIXFILE_EXCL0x01|UNIXFILE_RDONLY0x02))==UNIXFILE_EXCL0x01 ){
40138 if( pInode->bProcessLock==0 ){
40139 struct flock lock;
40140 assert( pInode->nLock==0 )((void) (0));
40141 lock.l_whence = SEEK_SET0;
40142 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40143 lock.l_len = SHARED_SIZE510;
40144 lock.l_type = F_WRLCK1;
40145 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,&
lock)
;
40146 if( rc<0 ) return rc;
40147 pInode->bProcessLock = 1;
40148 pInode->nLock++;
40149 }else{
40150 rc = 0;
40151 }
40152 }else{
40153 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,pLock
)
;
40154 }
40155 return rc;
40156}
40157
40158/*
40159** Lock the file with the lock specified by parameter eFileLock - one
40160** of the following:
40161**
40162** (1) SHARED_LOCK
40163** (2) RESERVED_LOCK
40164** (3) PENDING_LOCK
40165** (4) EXCLUSIVE_LOCK
40166**
40167** Sometimes when requesting one lock state, additional lock states
40168** are inserted in between. The locking might fail on one of the later
40169** transitions leaving the lock state different from what it started but
40170** still short of its goal. The following chart shows the allowed
40171** transitions and the inserted intermediate states:
40172**
40173** UNLOCKED -> SHARED
40174** SHARED -> RESERVED
40175** SHARED -> EXCLUSIVE
40176** RESERVED -> (PENDING) -> EXCLUSIVE
40177** PENDING -> EXCLUSIVE
40178**
40179** This routine will only increase a lock. Use the sqlite3OsUnlock()
40180** routine to lower a locking level.
40181*/
40182static int unixLock(sqlite3_file *id, int eFileLock){
40183 /* The following describes the implementation of the various locks and
40184 ** lock transitions in terms of the POSIX advisory shared and exclusive
40185 ** lock primitives (called read-locks and write-locks below, to avoid
40186 ** confusion with SQLite lock names). The algorithms are complicated
40187 ** slightly in order to be compatible with Windows95 systems simultaneously
40188 ** accessing the same database file, in case that is ever required.
40189 **
40190 ** Symbols defined in os.h identify the 'pending byte' and the 'reserved
40191 ** byte', each single bytes at well known offsets, and the 'shared byte
40192 ** range', a range of 510 bytes at a well known offset.
40193 **
40194 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
40195 ** byte'. If this is successful, 'shared byte range' is read-locked
40196 ** and the lock on the 'pending byte' released. (Legacy note: When
40197 ** SQLite was first developed, Windows95 systems were still very common,
40198 ** and Windows95 lacks a shared-lock capability. So on Windows95, a
40199 ** single randomly selected by from the 'shared byte range' is locked.
40200 ** Windows95 is now pretty much extinct, but this work-around for the
40201 ** lack of shared-locks on Windows95 lives on, for backwards
40202 ** compatibility.)
40203 **
40204 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
40205 ** A RESERVED lock is implemented by grabbing a write-lock on the
40206 ** 'reserved byte'.
40207 **
40208 ** An EXCLUSIVE lock may only be requested after either a SHARED or
40209 ** RESERVED lock is held. An EXCLUSIVE lock is implemented by obtaining
40210 ** a write-lock on the entire 'shared byte range'. Since all other locks
40211 ** require a read-lock on one of the bytes within this range, this ensures
40212 ** that no other locks are held on the database.
40213 **
40214 ** If a process that holds a RESERVED lock requests an EXCLUSIVE, then
40215 ** a PENDING lock is obtained first. A PENDING lock is implemented by
40216 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
40217 ** SHARED locks can be obtained, but existing SHARED locks are allowed to
40218 ** persist. If the call to this function fails to obtain the EXCLUSIVE
40219 ** lock in this case, it holds the PENDING lock instead. The client may
40220 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
40221 ** locks have cleared.
40222 */
40223 int rc = SQLITE_OK0;
40224 unixFile *pFile = (unixFile*)id;
40225 unixInodeInfo *pInode;
40226 struct flock lock;
40227 int tErrno = 0;
40228
40229 assert( pFile )((void) (0));
40230 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
40231 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40232 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
40233 osGetpid(0)));
40234
40235 /* If there is already a lock of this type or more restrictive on the
40236 ** unixFile, do nothing. Don't use the end_lock: exit path, as
40237 ** unixEnterMutex() hasn't been called yet.
40238 */
40239 if( pFile->eFileLock>=eFileLock ){
40240 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
40241 azFileLock(eFileLock)));
40242 return SQLITE_OK0;
40243 }
40244
40245 /* Make sure the locking sequence is correct.
40246 ** (1) We never move from unlocked to anything higher than shared lock.
40247 ** (2) SQLite never explicitly requests a pending lock.
40248 ** (3) A shared lock is always held when a reserve lock is requested.
40249 */
40250 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
40251 assert( eFileLock!=PENDING_LOCK )((void) (0));
40252 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
40253
40254 /* This mutex is needed because pFile->pInode is shared across threads
40255 */
40256 pInode = pFile->pInode;
40257 sqlite3_mutex_enter(pInode->pLockMutex);
40258
40259 /* If some thread using this PID has a lock via a different unixFile*
40260 ** handle that precludes the requested lock, return BUSY.
40261 */
40262 if( (pFile->eFileLock!=pInode->eFileLock &&
40263 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
40264 ){
40265 rc = SQLITE_BUSY5;
40266 goto end_lock;
40267 }
40268
40269 /* If a SHARED lock is requested, and some thread using this PID already
40270 ** has a SHARED or RESERVED lock, then increment reference counts and
40271 ** return SQLITE_OK.
40272 */
40273 if( eFileLock==SHARED_LOCK1 &&
40274 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
40275 assert( eFileLock==SHARED_LOCK )((void) (0));
40276 assert( pFile->eFileLock==0 )((void) (0));
40277 assert( pInode->nShared>0 )((void) (0));
40278 pFile->eFileLock = SHARED_LOCK1;
40279 pInode->nShared++;
40280 pInode->nLock++;
40281 goto end_lock;
40282 }
40283
40284
40285 /* A PENDING lock is needed before acquiring a SHARED lock and before
40286 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
40287 ** be released.
40288 */
40289 lock.l_len = 1L;
40290 lock.l_whence = SEEK_SET0;
40291 if( eFileLock==SHARED_LOCK1
40292 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock==RESERVED_LOCK2)
40293 ){
40294 lock.l_type = (eFileLock==SHARED_LOCK1?F_RDLCK0:F_WRLCK1);
40295 lock.l_start = PENDING_BYTEsqlite3PendingByte;
40296 if( unixFileLock(pFile, &lock) ){
40297 tErrno = errno(*__errno_location ());
40298 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40299 if( rc!=SQLITE_BUSY5 ){
40300 storeLastErrno(pFile, tErrno);
40301 }
40302 goto end_lock;
40303 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
40304 pFile->eFileLock = PENDING_LOCK3;
40305 pInode->eFileLock = PENDING_LOCK3;
40306 }
40307 }
40308
40309
40310 /* If control gets to this point, then actually go ahead and make
40311 ** operating system calls for the specified lock.
40312 */
40313 if( eFileLock==SHARED_LOCK1 ){
40314 assert( pInode->nShared==0 )((void) (0));
40315 assert( pInode->eFileLock==0 )((void) (0));
40316 assert( rc==SQLITE_OK )((void) (0));
40317
40318 /* Now get the read-lock */
40319 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40320 lock.l_len = SHARED_SIZE510;
40321 if( unixFileLock(pFile, &lock) ){
40322 tErrno = errno(*__errno_location ());
40323 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40324 }
40325
40326 /* Drop the temporary PENDING lock */
40327 lock.l_start = PENDING_BYTEsqlite3PendingByte;
40328 lock.l_len = 1L;
40329 lock.l_type = F_UNLCK2;
40330 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK0 ){
40331 /* This could happen with a network mount */
40332 tErrno = errno(*__errno_location ());
40333 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40334 }
40335
40336 if( rc ){
40337 if( rc!=SQLITE_BUSY5 ){
40338 storeLastErrno(pFile, tErrno);
40339 }
40340 goto end_lock;
40341 }else{
40342 pFile->eFileLock = SHARED_LOCK1;
40343 pInode->nLock++;
40344 pInode->nShared = 1;
40345 }
40346 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
40347 /* We are trying for an exclusive lock but another thread in this
40348 ** same process is still holding a shared lock. */
40349 rc = SQLITE_BUSY5;
40350 }else{
40351 /* The request was for a RESERVED or EXCLUSIVE lock. It is
40352 ** assumed that there is a SHARED or greater lock on the file
40353 ** already.
40354 */
40355 assert( 0!=pFile->eFileLock )((void) (0));
40356 lock.l_type = F_WRLCK1;
40357
40358 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK )((void) (0));
40359 if( eFileLock==RESERVED_LOCK2 ){
40360 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
40361 lock.l_len = 1L;
40362 }else{
40363 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40364 lock.l_len = SHARED_SIZE510;
40365 }
40366
40367 if( unixFileLock(pFile, &lock) ){
40368 tErrno = errno(*__errno_location ());
40369 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40370 if( rc!=SQLITE_BUSY5 ){
40371 storeLastErrno(pFile, tErrno);
40372 }
40373 }
40374 }
40375
40376
40377#ifdef SQLITE_DEBUG
40378 /* Set up the transaction-counter change checking flags when
40379 ** transitioning from a SHARED to a RESERVED lock. The change
40380 ** from SHARED to RESERVED marks the beginning of a normal
40381 ** write operation (not a hot journal rollback).
40382 */
40383 if( rc==SQLITE_OK0
40384 && pFile->eFileLock<=SHARED_LOCK1
40385 && eFileLock==RESERVED_LOCK2
40386 ){
40387 pFile->transCntrChng = 0;
40388 pFile->dbUpdate = 0;
40389 pFile->inNormalWrite = 1;
40390 }
40391#endif
40392
40393 if( rc==SQLITE_OK0 ){
40394 pFile->eFileLock = eFileLock;
40395 pInode->eFileLock = eFileLock;
40396 }
40397
40398end_lock:
40399 sqlite3_mutex_leave(pInode->pLockMutex);
40400 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
40401 rc==SQLITE_OK ? "ok" : "failed"));
40402 return rc;
40403}
40404
40405/*
40406** Add the file descriptor used by file handle pFile to the corresponding
40407** pUnused list.
40408*/
40409static void setPendingFd(unixFile *pFile){
40410 unixInodeInfo *pInode = pFile->pInode;
40411 UnixUnusedFd *p = pFile->pPreallocatedUnused;
40412 assert( unixFileMutexHeld(pFile) )((void) (0));
40413 p->pNext = pInode->pUnused;
40414 pInode->pUnused = p;
40415 pFile->h = -1;
40416 pFile->pPreallocatedUnused = 0;
40417}
40418
40419/*
40420** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40421** must be either NO_LOCK or SHARED_LOCK.
40422**
40423** If the locking level of the file descriptor is already at or below
40424** the requested locking level, this routine is a no-op.
40425**
40426** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
40427** the byte range is divided into 2 parts and the first part is unlocked then
40428** set to a read lock, then the other part is simply unlocked. This works
40429** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
40430** remove the write lock on a region when a read lock is set.
40431*/
40432static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
40433 unixFile *pFile = (unixFile*)id;
40434 unixInodeInfo *pInode;
40435 struct flock lock;
40436 int rc = SQLITE_OK0;
40437
40438 assert( pFile )((void) (0));
40439 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
40440 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40441 osGetpid(0)));
40442
40443 assert( eFileLock<=SHARED_LOCK )((void) (0));
40444 if( pFile->eFileLock<=eFileLock ){
40445 return SQLITE_OK0;
40446 }
40447 pInode = pFile->pInode;
40448 sqlite3_mutex_enter(pInode->pLockMutex);
40449 assert( pInode->nShared!=0 )((void) (0));
40450 if( pFile->eFileLock>SHARED_LOCK1 ){
40451 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
40452
40453#ifdef SQLITE_DEBUG
40454 /* When reducing a lock such that other processes can start
40455 ** reading the database file again, make sure that the
40456 ** transaction counter was updated if any part of the database
40457 ** file changed. If the transaction counter is not updated,
40458 ** other connections to the same file might not realize that
40459 ** the file has changed and hence might not know to flush their
40460 ** cache. The use of a stale cache can lead to database corruption.
40461 */
40462 pFile->inNormalWrite = 0;
40463#endif
40464
40465 /* downgrading to a shared lock on NFS involves clearing the write lock
40466 ** before establishing the readlock - to avoid a race condition we downgrade
40467 ** the lock in 2 blocks, so that part of the range will be covered by a
40468 ** write lock until the rest is covered by a read lock:
40469 ** 1: [WWWWW]
40470 ** 2: [....W]
40471 ** 3: [RRRRW]
40472 ** 4: [RRRR.]
40473 */
40474 if( eFileLock==SHARED_LOCK1 ){
40475#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE0
40476 (void)handleNFSUnlock;
40477 assert( handleNFSUnlock==0 )((void) (0));
40478#endif
40479#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
40480 if( handleNFSUnlock ){
40481 int tErrno; /* Error code from system call errors */
40482 off_t divSize = SHARED_SIZE510 - 1;
40483
40484 lock.l_type = F_UNLCK2;
40485 lock.l_whence = SEEK_SET0;
40486 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40487 lock.l_len = divSize;
40488 if( unixFileLock(pFile, &lock)==(-1) ){
40489 tErrno = errno(*__errno_location ());
40490 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40491 storeLastErrno(pFile, tErrno);
40492 goto end_unlock;
40493 }
40494 lock.l_type = F_RDLCK0;
40495 lock.l_whence = SEEK_SET0;
40496 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40497 lock.l_len = divSize;
40498 if( unixFileLock(pFile, &lock)==(-1) ){
40499 tErrno = errno(*__errno_location ());
40500 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK(10 | (9<<8)));
40501 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
40502 storeLastErrno(pFile, tErrno);
40503 }
40504 goto end_unlock;
40505 }
40506 lock.l_type = F_UNLCK2;
40507 lock.l_whence = SEEK_SET0;
40508 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2)+divSize;
40509 lock.l_len = SHARED_SIZE510-divSize;
40510 if( unixFileLock(pFile, &lock)==(-1) ){
40511 tErrno = errno(*__errno_location ());
40512 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40513 storeLastErrno(pFile, tErrno);
40514 goto end_unlock;
40515 }
40516 }else
40517#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
40518 {
40519 lock.l_type = F_RDLCK0;
40520 lock.l_whence = SEEK_SET0;
40521 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
40522 lock.l_len = SHARED_SIZE510;
40523 if( unixFileLock(pFile, &lock) ){
40524 /* In theory, the call to unixFileLock() cannot fail because another
40525 ** process is holding an incompatible lock. If it does, this
40526 ** indicates that the other process is not following the locking
40527 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
40528 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
40529 ** an assert to fail). */
40530 rc = SQLITE_IOERR_RDLOCK(10 | (9<<8));
40531 storeLastErrno(pFile, errno(*__errno_location ()));
40532 goto end_unlock;
40533 }
40534 }
40535 }
40536 lock.l_type = F_UNLCK2;
40537 lock.l_whence = SEEK_SET0;
40538 lock.l_start = PENDING_BYTEsqlite3PendingByte;
40539 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE )((void) (0));
40540 if( unixFileLock(pFile, &lock)==0 ){
40541 pInode->eFileLock = SHARED_LOCK1;
40542 }else{
40543 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40544 storeLastErrno(pFile, errno(*__errno_location ()));
40545 goto end_unlock;
40546 }
40547 }
40548 if( eFileLock==NO_LOCK0 ){
40549 /* Decrement the shared lock counter. Release the lock using an
40550 ** OS call only when all threads in this same process have released
40551 ** the lock.
40552 */
40553 pInode->nShared--;
40554 if( pInode->nShared==0 ){
40555 lock.l_type = F_UNLCK2;
40556 lock.l_whence = SEEK_SET0;
40557 lock.l_start = lock.l_len = 0L;
40558 if( unixFileLock(pFile, &lock)==0 ){
40559 pInode->eFileLock = NO_LOCK0;
40560 }else{
40561 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40562 storeLastErrno(pFile, errno(*__errno_location ()));
40563 pInode->eFileLock = NO_LOCK0;
40564 pFile->eFileLock = NO_LOCK0;
40565 }
40566 }
40567
40568 /* Decrement the count of locks against this same file. When the
40569 ** count reaches zero, close any other file descriptors whose close
40570 ** was deferred because of outstanding locks.
40571 */
40572 pInode->nLock--;
40573 assert( pInode->nLock>=0 )((void) (0));
40574 if( pInode->nLock==0 ) closePendingFds(pFile);
40575 }
40576
40577end_unlock:
40578 sqlite3_mutex_leave(pInode->pLockMutex);
40579 if( rc==SQLITE_OK0 ){
40580 pFile->eFileLock = eFileLock;
40581 }
40582 return rc;
40583}
40584
40585/*
40586** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40587** must be either NO_LOCK or SHARED_LOCK.
40588**
40589** If the locking level of the file descriptor is already at or below
40590** the requested locking level, this routine is a no-op.
40591*/
40592static int unixUnlock(sqlite3_file *id, int eFileLock){
40593#if SQLITE_MAX_MMAP_SIZE20971520>0
40594 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 )((void) (0));
40595#endif
40596 return posixUnlock(id, eFileLock, 0);
40597}
40598
40599#if SQLITE_MAX_MMAP_SIZE20971520>0
40600static int unixMapfile(unixFile *pFd, i64 nByte);
40601static void unixUnmapfile(unixFile *pFd);
40602#endif
40603
40604/*
40605** This function performs the parts of the "close file" operation
40606** common to all locking schemes. It closes the directory and file
40607** handles, if they are valid, and sets all fields of the unixFile
40608** structure to 0.
40609**
40610** It is *not* necessary to hold the mutex when this routine is called,
40611** even on VxWorks. A mutex will be acquired on VxWorks by the
40612** vxworksReleaseFileId() routine.
40613*/
40614static int closeUnixFile(sqlite3_file *id){
40615 unixFile *pFile = (unixFile*)id;
40616#if SQLITE_MAX_MMAP_SIZE20971520>0
40617 unixUnmapfile(pFile);
40618#endif
40619 if( pFile->h>=0 ){
40620 robust_close(pFile, pFile->h, __LINE__40620);
40621 pFile->h = -1;
40622 }
40623#if OS_VXWORKS0
40624 if( pFile->pId ){
40625 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
40626 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->pId->zCanonicalName);
40627 }
40628 vxworksReleaseFileId(pFile->pId);
40629 pFile->pId = 0;
40630 }
40631#endif
40632#ifdef SQLITE_UNLINK_AFTER_CLOSE
40633 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
40634 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->zPath);
40635 sqlite3_free(*(char**)&pFile->zPath);
40636 pFile->zPath = 0;
40637 }
40638#endif
40639 OSTRACE(("CLOSE %-3d\n", pFile->h));
40640 OpenCounter(-1);
40641 sqlite3_free(pFile->pPreallocatedUnused);
40642 memset(pFile, 0, sizeof(unixFile));
40643 return SQLITE_OK0;
40644}
40645
40646/*
40647** Close a file.
40648*/
40649static int unixClose(sqlite3_file *id){
40650 int rc = SQLITE_OK0;
40651 unixFile *pFile = (unixFile *)id;
40652 unixInodeInfo *pInode = pFile->pInode;
40653
40654 assert( pInode!=0 )((void) (0));
40655 verifyDbFile(pFile);
40656 unixUnlock(id, NO_LOCK0);
40657 assert( unixFileMutexNotheld(pFile) )((void) (0));
40658 unixEnterMutex();
40659
40660 /* unixFile.pInode is always valid here. Otherwise, a different close
40661 ** routine (e.g. nolockClose()) would be called instead.
40662 */
40663 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 )((void) (0));
40664 sqlite3_mutex_enter(pInode->pLockMutex);
40665 if( pInode->nLock ){
40666 /* If there are outstanding locks, do not actually close the file just
40667 ** yet because that would clear those locks. Instead, add the file
40668 ** descriptor to pInode->pUnused list. It will be automatically closed
40669 ** when the last lock is cleared.
40670 */
40671 setPendingFd(pFile);
40672 }
40673 sqlite3_mutex_leave(pInode->pLockMutex);
40674 releaseInodeInfo(pFile);
40675 assert( pFile->pShm==0 )((void) (0));
40676 rc = closeUnixFile(id);
40677 unixLeaveMutex();
40678 return rc;
40679}
40680
40681/************** End of the posix advisory lock implementation *****************
40682******************************************************************************/
40683
40684/******************************************************************************
40685****************************** No-op Locking **********************************
40686**
40687** Of the various locking implementations available, this is by far the
40688** simplest: locking is ignored. No attempt is made to lock the database
40689** file for reading or writing.
40690**
40691** This locking mode is appropriate for use on read-only databases
40692** (ex: databases that are burned into CD-ROM, for example.) It can
40693** also be used if the application employs some external mechanism to
40694** prevent simultaneous access of the same database by two or more
40695** database connections. But there is a serious risk of database
40696** corruption if this locking mode is used in situations where multiple
40697** database connections are accessing the same database file at the same
40698** time and one or more of those connections are writing.
40699*/
40700
40701static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
40702 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
40703 *pResOut = 0;
40704 return SQLITE_OK0;
40705}
40706static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
40707 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
40708 return SQLITE_OK0;
40709}
40710static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
40711 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
40712 return SQLITE_OK0;
40713}
40714
40715/*
40716** Close the file.
40717*/
40718static int nolockClose(sqlite3_file *id) {
40719 return closeUnixFile(id);
40720}
40721
40722/******************* End of the no-op lock implementation *********************
40723******************************************************************************/
40724
40725/******************************************************************************
40726************************* Begin dot-file Locking ******************************
40727**
40728** The dotfile locking implementation uses the existence of separate lock
40729** files (really a directory) to control access to the database. This works
40730** on just about every filesystem imaginable. But there are serious downsides:
40731**
40732** (1) There is zero concurrency. A single reader blocks all other
40733** connections from reading or writing the database.
40734**
40735** (2) An application crash or power loss can leave stale lock files
40736** sitting around that need to be cleared manually.
40737**
40738** Nevertheless, a dotlock is an appropriate locking mode for use if no
40739** other locking strategy is available.
40740**
40741** Dotfile locking works by creating a subdirectory in the same directory as
40742** the database and with the same name but with a ".lock" extension added.
40743** The existence of a lock directory implies an EXCLUSIVE lock. All other
40744** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
40745*/
40746
40747/*
40748** The file suffix added to the data base filename in order to create the
40749** lock directory.
40750*/
40751#define DOTLOCK_SUFFIX".lock" ".lock"
40752
40753/*
40754** This routine checks if there is a RESERVED lock held on the specified
40755** file by this or any other process. If the caller holds a SHARED
40756** or greater lock when it is called, then it is assumed that no other
40757** client may hold RESERVED. Or, if the caller holds no lock, then it
40758** is assumed another client holds RESERVED if the lock-file exists.
40759*/
40760static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
40761 unixFile *pFile = (unixFile*)id;
40762 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40763
40764 if( pFile->eFileLock>=SHARED_LOCK1 ){
40765 *pResOut = 0;
40766 }else{
40767 *pResOut = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)((const char*)pFile->lockingContext, 0)==0;
40768 }
40769 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, 0, *pResOut));
40770 return SQLITE_OK0;
40771}
40772
40773/*
40774** Lock the file with the lock specified by parameter eFileLock - one
40775** of the following:
40776**
40777** (1) SHARED_LOCK
40778** (2) RESERVED_LOCK
40779** (3) PENDING_LOCK
40780** (4) EXCLUSIVE_LOCK
40781**
40782** Sometimes when requesting one lock state, additional lock states
40783** are inserted in between. The locking might fail on one of the later
40784** transitions leaving the lock state different from what it started but
40785** still short of its goal. The following chart shows the allowed
40786** transitions and the inserted intermediate states:
40787**
40788** UNLOCKED -> SHARED
40789** SHARED -> RESERVED
40790** SHARED -> (PENDING) -> EXCLUSIVE
40791** RESERVED -> (PENDING) -> EXCLUSIVE
40792** PENDING -> EXCLUSIVE
40793**
40794** This routine will only increase a lock. Use the sqlite3OsUnlock()
40795** routine to lower a locking level.
40796**
40797** With dotfile locking, we really only support state (4): EXCLUSIVE.
40798** But we track the other locking levels internally.
40799*/
40800static int dotlockLock(sqlite3_file *id, int eFileLock) {
40801 unixFile *pFile = (unixFile*)id;
40802 char *zLockFile = (char *)pFile->lockingContext;
40803 int rc = SQLITE_OK0;
40804
40805
40806 /* If we have any lock, then the lock file already exists. All we have
40807 ** to do is adjust our internal record of the lock level.
40808 */
40809 if( pFile->eFileLock > NO_LOCK0 ){
40810 pFile->eFileLock = eFileLock;
40811 /* Always update the timestamp on the old file */
40812#ifdef HAVE_UTIME
40813 utime(zLockFile, NULL((void*)0));
40814#else
40815 utimes(zLockFile, NULL((void*)0));
40816#endif
40817 return SQLITE_OK0;
40818 }
40819
40820 /* grab an exclusive lock */
40821 rc = osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(zLockFile, 0777);
40822 if( rc<0 ){
40823 /* failed to open/create the lock directory */
40824 int tErrno = errno(*__errno_location ());
40825 if( EEXIST17 == tErrno ){
40826 rc = SQLITE_BUSY5;
40827 } else {
40828 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
40829 if( rc!=SQLITE_BUSY5 ){
40830 storeLastErrno(pFile, tErrno);
40831 }
40832 }
40833 return rc;
40834 }
40835
40836 /* got it, set the type and return ok */
40837 pFile->eFileLock = eFileLock;
40838 return rc;
40839}
40840
40841/*
40842** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40843** must be either NO_LOCK or SHARED_LOCK.
40844**
40845** If the locking level of the file descriptor is already at or below
40846** the requested locking level, this routine is a no-op.
40847**
40848** When the locking level reaches NO_LOCK, delete the lock file.
40849*/
40850static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
40851 unixFile *pFile = (unixFile*)id;
40852 char *zLockFile = (char *)pFile->lockingContext;
40853 int rc;
40854
40855 assert( pFile )((void) (0));
40856 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
40857 pFile->eFileLock, osGetpid(0)));
40858 assert( eFileLock<=SHARED_LOCK )((void) (0));
40859
40860 /* no-op if possible */
40861 if( pFile->eFileLock==eFileLock ){
40862 return SQLITE_OK0;
40863 }
40864
40865 /* To downgrade to shared, simply update our internal notion of the
40866 ** lock state. No need to mess with the file on disk.
40867 */
40868 if( eFileLock==SHARED_LOCK1 ){
40869 pFile->eFileLock = SHARED_LOCK1;
40870 return SQLITE_OK0;
40871 }
40872
40873 /* To fully unlock the database, delete the lock file */
40874 assert( eFileLock==NO_LOCK )((void) (0));
40875 rc = osRmdir((int(*)(const char*))aSyscall[19].pCurrent)(zLockFile);
40876 if( rc<0 ){
40877 int tErrno = errno(*__errno_location ());
40878 if( tErrno==ENOENT2 ){
40879 rc = SQLITE_OK0;
40880 }else{
40881 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
40882 storeLastErrno(pFile, tErrno);
40883 }
40884 return rc;
40885 }
40886 pFile->eFileLock = NO_LOCK0;
40887 return SQLITE_OK0;
40888}
40889
40890/*
40891** Close a file. Make sure the lock has been released before closing.
40892*/
40893static int dotlockClose(sqlite3_file *id) {
40894 unixFile *pFile = (unixFile*)id;
40895 assert( id!=0 )((void) (0));
40896 dotlockUnlock(id, NO_LOCK0);
40897 sqlite3_free(pFile->lockingContext);
40898 return closeUnixFile(id);
40899}
40900/****************** End of the dot-file lock implementation *******************
40901******************************************************************************/
40902
40903/******************************************************************************
40904************************** Begin flock Locking ********************************
40905**
40906** Use the flock() system call to do file locking.
40907**
40908** flock() locking is like dot-file locking in that the various
40909** fine-grain locking levels supported by SQLite are collapsed into
40910** a single exclusive lock. In other words, SHARED, RESERVED, and
40911** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
40912** still works when you do this, but concurrency is reduced since
40913** only a single process can be reading the database at a time.
40914**
40915** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
40916*/
40917#if SQLITE_ENABLE_LOCKING_STYLE0
40918
40919/*
40920** Retry flock() calls that fail with EINTR
40921*/
40922#ifdef EINTR4
40923static int robust_flock(int fd, int op){
40924 int rc;
40925 do{ rc = flock(fd,op); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
40926 return rc;
40927}
40928#else
40929# define robust_flock(a,b) flock(a,b)
40930#endif
40931
40932
40933/*
40934** This routine checks if there is a RESERVED lock held on the specified
40935** file by this or any other process. If such a lock is held, set *pResOut
40936** to a non-zero value otherwise *pResOut is set to zero. The return value
40937** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40938*/
40939static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
40940#ifdef SQLITE_DEBUG
40941 unixFile *pFile = (unixFile*)id;
40942#else
40943 UNUSED_PARAMETER(id)(void)(id);
40944#endif
40945
40946 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
40947
40948 assert( pFile )((void) (0));
40949 assert( pFile->eFileLock<=SHARED_LOCK )((void) (0));
40950
40951 /* The flock VFS only ever takes exclusive locks (see function flockLock).
40952 ** Therefore, if this connection is holding any lock at all, no other
40953 ** connection may be holding a RESERVED lock. So set *pResOut to 0
40954 ** in this case.
40955 **
40956 ** Or, this connection may be holding no lock. In that case, set *pResOut to
40957 ** 0 as well. The caller will then attempt to take an EXCLUSIVE lock on the
40958 ** db in order to roll the hot journal back. If there is another connection
40959 ** holding a lock, that attempt will fail and an SQLITE_BUSY returned to
40960 ** the user. With other VFS, we try to avoid this, in order to allow a reader
40961 ** to proceed while a writer is preparing its transaction. But that won't
40962 ** work with the flock VFS - as it always takes EXCLUSIVE locks - so it is
40963 ** not a problem in this case. */
40964 *pResOut = 0;
40965
40966 return SQLITE_OK0;
40967}
40968
40969/*
40970** Lock the file with the lock specified by parameter eFileLock - one
40971** of the following:
40972**
40973** (1) SHARED_LOCK
40974** (2) RESERVED_LOCK
40975** (3) PENDING_LOCK
40976** (4) EXCLUSIVE_LOCK
40977**
40978** Sometimes when requesting one lock state, additional lock states
40979** are inserted in between. The locking might fail on one of the later
40980** transitions leaving the lock state different from what it started but
40981** still short of its goal. The following chart shows the allowed
40982** transitions and the inserted intermediate states:
40983**
40984** UNLOCKED -> SHARED
40985** SHARED -> RESERVED
40986** SHARED -> (PENDING) -> EXCLUSIVE
40987** RESERVED -> (PENDING) -> EXCLUSIVE
40988** PENDING -> EXCLUSIVE
40989**
40990** flock() only really support EXCLUSIVE locks. We track intermediate
40991** lock states in the sqlite3_file structure, but all locks SHARED or
40992** above are really EXCLUSIVE locks and exclude all other processes from
40993** access the file.
40994**
40995** This routine will only increase a lock. Use the sqlite3OsUnlock()
40996** routine to lower a locking level.
40997*/
40998static int flockLock(sqlite3_file *id, int eFileLock) {
40999 int rc = SQLITE_OK0;
41000 unixFile *pFile = (unixFile*)id;
41001
41002 assert( pFile )((void) (0));
41003
41004 /* if we already have a lock, it is exclusive.
41005 ** Just adjust level and punt on outta here. */
41006 if (pFile->eFileLock > NO_LOCK0) {
41007 pFile->eFileLock = eFileLock;
41008 return SQLITE_OK0;
41009 }
41010
41011 /* grab an exclusive lock */
41012
41013 if (robust_flock(pFile->h, LOCK_EX2 | LOCK_NB4)) {
41014 int tErrno = errno(*__errno_location ());
41015 /* didn't get, must be busy */
41016 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
41017 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
41018 storeLastErrno(pFile, tErrno);
41019 }
41020 } else {
41021 /* got it, set the type and return ok */
41022 pFile->eFileLock = eFileLock;
41023 }
41024 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
41025 rc==SQLITE_OK ? "ok" : "failed"));
41026#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
41027 if( (rc & 0xff) == SQLITE_IOERR10 ){
41028 rc = SQLITE_BUSY5;
41029 }
41030#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
41031 return rc;
41032}
41033
41034
41035/*
41036** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41037** must be either NO_LOCK or SHARED_LOCK.
41038**
41039** If the locking level of the file descriptor is already at or below
41040** the requested locking level, this routine is a no-op.
41041*/
41042static int flockUnlock(sqlite3_file *id, int eFileLock) {
41043 unixFile *pFile = (unixFile*)id;
41044
41045 assert( pFile )((void) (0));
41046 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
41047 pFile->eFileLock, osGetpid(0)));
41048 assert( eFileLock<=SHARED_LOCK )((void) (0));
41049
41050 /* no-op if possible */
41051 if( pFile->eFileLock==eFileLock ){
41052 return SQLITE_OK0;
41053 }
41054
41055 /* shared can just be set because we always have an exclusive */
41056 if (eFileLock==SHARED_LOCK1) {
41057 pFile->eFileLock = eFileLock;
41058 return SQLITE_OK0;
41059 }
41060
41061 /* no, really, unlock. */
41062 if( robust_flock(pFile->h, LOCK_UN8) ){
41063#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
41064 return SQLITE_OK0;
41065#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
41066 return SQLITE_IOERR_UNLOCK(10 | (8<<8));
41067 }else{
41068 pFile->eFileLock = NO_LOCK0;
41069 return SQLITE_OK0;
41070 }
41071}
41072
41073/*
41074** Close a file.
41075*/
41076static int flockClose(sqlite3_file *id) {
41077 assert( id!=0 )((void) (0));
41078 flockUnlock(id, NO_LOCK0);
41079 return closeUnixFile(id);
41080}
41081
41082#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
41083
41084/******************* End of the flock lock implementation *********************
41085******************************************************************************/
41086
41087/******************************************************************************
41088************************ Begin Named Semaphore Locking ************************
41089**
41090** Named semaphore locking is only supported on VxWorks.
41091**
41092** Semaphore locking is like dot-lock and flock in that it really only
41093** supports EXCLUSIVE locking. Only a single process can read or write
41094** the database file at a time. This reduces potential concurrency, but
41095** makes the lock implementation much easier.
41096*/
41097#if OS_VXWORKS0
41098
41099/*
41100** This routine checks if there is a RESERVED lock held on the specified
41101** file by this or any other process. If such a lock is held, set *pResOut
41102** to a non-zero value otherwise *pResOut is set to zero. The return value
41103** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41104*/
41105static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
41106 int rc = SQLITE_OK0;
41107 int reserved = 0;
41108 unixFile *pFile = (unixFile*)id;
41109
41110 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
41111
41112 assert( pFile )((void) (0));
41113
41114 /* Check if a thread in this process holds such a lock */
41115 if( pFile->eFileLock>SHARED_LOCK1 ){
41116 reserved = 1;
41117 }
41118
41119 /* Otherwise see if some other process holds it. */
41120 if( !reserved ){
41121 sem_t *pSem = pFile->pInode->pSem;
41122
41123 if( sem_trywait(pSem)==-1 ){
41124 int tErrno = errno(*__errno_location ());
41125 if( EAGAIN11 != tErrno ){
41126 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)));
41127 storeLastErrno(pFile, tErrno);
41128 } else {
41129 /* someone else has the lock when we are in NO_LOCK */
41130 reserved = (pFile->eFileLock < SHARED_LOCK1);
41131 }
41132 }else{
41133 /* we could have it if we want it */
41134 sem_post(pSem);
41135 }
41136 }
41137 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
41138
41139 *pResOut = reserved;
41140 return rc;
41141}
41142
41143/*
41144** Lock the file with the lock specified by parameter eFileLock - one
41145** of the following:
41146**
41147** (1) SHARED_LOCK
41148** (2) RESERVED_LOCK
41149** (3) PENDING_LOCK
41150** (4) EXCLUSIVE_LOCK
41151**
41152** Sometimes when requesting one lock state, additional lock states
41153** are inserted in between. The locking might fail on one of the later
41154** transitions leaving the lock state different from what it started but
41155** still short of its goal. The following chart shows the allowed
41156** transitions and the inserted intermediate states:
41157**
41158** UNLOCKED -> SHARED
41159** SHARED -> RESERVED
41160** SHARED -> (PENDING) -> EXCLUSIVE
41161** RESERVED -> (PENDING) -> EXCLUSIVE
41162** PENDING -> EXCLUSIVE
41163**
41164** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
41165** lock states in the sqlite3_file structure, but all locks SHARED or
41166** above are really EXCLUSIVE locks and exclude all other processes from
41167** access the file.
41168**
41169** This routine will only increase a lock. Use the sqlite3OsUnlock()
41170** routine to lower a locking level.
41171*/
41172static int semXLock(sqlite3_file *id, int eFileLock) {
41173 unixFile *pFile = (unixFile*)id;
41174 sem_t *pSem = pFile->pInode->pSem;
41175 int rc = SQLITE_OK0;
41176
41177 /* if we already have a lock, it is exclusive.
41178 ** Just adjust level and punt on outta here. */
41179 if (pFile->eFileLock > NO_LOCK0) {
41180 pFile->eFileLock = eFileLock;
41181 rc = SQLITE_OK0;
41182 goto sem_end_lock;
41183 }
41184
41185 /* lock semaphore now but bail out when already locked. */
41186 if( sem_trywait(pSem)==-1 ){
41187 rc = SQLITE_BUSY5;
41188 goto sem_end_lock;
41189 }
41190
41191 /* got it, set the type and return ok */
41192 pFile->eFileLock = eFileLock;
41193
41194 sem_end_lock:
41195 return rc;
41196}
41197
41198/*
41199** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41200** must be either NO_LOCK or SHARED_LOCK.
41201**
41202** If the locking level of the file descriptor is already at or below
41203** the requested locking level, this routine is a no-op.
41204*/
41205static int semXUnlock(sqlite3_file *id, int eFileLock) {
41206 unixFile *pFile = (unixFile*)id;
41207 sem_t *pSem = pFile->pInode->pSem;
41208
41209 assert( pFile )((void) (0));
41210 assert( pSem )((void) (0));
41211 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
41212 pFile->eFileLock, osGetpid(0)));
41213 assert( eFileLock<=SHARED_LOCK )((void) (0));
41214
41215 /* no-op if possible */
41216 if( pFile->eFileLock==eFileLock ){
41217 return SQLITE_OK0;
41218 }
41219
41220 /* shared can just be set because we always have an exclusive */
41221 if (eFileLock==SHARED_LOCK1) {
41222 pFile->eFileLock = eFileLock;
41223 return SQLITE_OK0;
41224 }
41225
41226 /* no, really unlock. */
41227 if ( sem_post(pSem)==-1 ) {
41228 int rc, tErrno = errno(*__errno_location ());
41229 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK(10 | (8<<8)));
41230 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
41231 storeLastErrno(pFile, tErrno);
41232 }
41233 return rc;
41234 }
41235 pFile->eFileLock = NO_LOCK0;
41236 return SQLITE_OK0;
41237}
41238
41239/*
41240 ** Close a file.
41241 */
41242static int semXClose(sqlite3_file *id) {
41243 if( id ){
41244 unixFile *pFile = (unixFile*)id;
41245 semXUnlock(id, NO_LOCK0);
41246 assert( pFile )((void) (0));
41247 assert( unixFileMutexNotheld(pFile) )((void) (0));
41248 unixEnterMutex();
41249 releaseInodeInfo(pFile);
41250 unixLeaveMutex();
41251 closeUnixFile(id);
41252 }
41253 return SQLITE_OK0;
41254}
41255
41256#endif /* OS_VXWORKS */
41257/*
41258** Named semaphore locking is only available on VxWorks.
41259**
41260*************** End of the named semaphore lock implementation ****************
41261******************************************************************************/
41262
41263
41264/******************************************************************************
41265*************************** Begin AFP Locking *********************************
41266**
41267** AFP is the Apple Filing Protocol. AFP is a network filesystem found
41268** on Apple Macintosh computers - both OS9 and OSX.
41269**
41270** Third-party implementations of AFP are available. But this code here
41271** only works on OSX.
41272*/
41273
41274#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
41275/*
41276** The afpLockingContext structure contains all afp lock specific state
41277*/
41278typedef struct afpLockingContext afpLockingContext;
41279struct afpLockingContext {
41280 int reserved;
41281 const char *dbPath; /* Name of the open file */
41282};
41283
41284struct ByteRangeLockPB2
41285{
41286 unsigned long long offset; /* offset to first byte to lock */
41287 unsigned long long length; /* nbr of bytes to lock */
41288 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
41289 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
41290 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
41291 int fd; /* file desc to assoc this lock with */
41292};
41293
41294#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)(((2U|1U) << (((0 +8)+8)+14)) | ((('z')) << (0 +8
)) | (((23)) << 0) | ((((sizeof(struct ByteRangeLockPB2
)))) << ((0 +8)+8)))
41295
41296/*
41297** This is a utility for setting or clearing a bit-range lock on an
41298** AFP filesystem.
41299**
41300** Return SQLITE_OK on success, SQLITE_BUSY on failure.
41301*/
41302static int afpSetLock(
41303 const char *path, /* Name of the file to be locked or unlocked */
41304 unixFile *pFile, /* Open file descriptor on path */
41305 unsigned long long offset, /* First byte to be locked */
41306 unsigned long long length, /* Number of bytes to lock */
41307 int setLockFlag /* True to set lock. False to clear lock */
41308){
41309 struct ByteRangeLockPB2 pb;
41310 int err;
41311
41312 pb.unLockFlag = setLockFlag ? 0 : 1;
41313 pb.startEndFlag = 0;
41314 pb.offset = offset;
41315 pb.length = length;
41316 pb.fd = pFile->h;
41317
41318 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
41319 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
41320 offset, length));
41321 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
41322 if ( err==-1 ) {
41323 int rc;
41324 int tErrno = errno(*__errno_location ());
41325 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
41326 path, tErrno, strerror(tErrno)));
41327#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
41328 rc = SQLITE_BUSY5;
41329#else
41330 rc = sqliteErrorFromPosixError(tErrno,
41331 setLockFlag ? SQLITE_IOERR_LOCK(10 | (15<<8)) : SQLITE_IOERR_UNLOCK(10 | (8<<8)));
41332#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
41333 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
41334 storeLastErrno(pFile, tErrno);
41335 }
41336 return rc;
41337 } else {
41338 return SQLITE_OK0;
41339 }
41340}
41341
41342/*
41343** This routine checks if there is a RESERVED lock held on the specified
41344** file by this or any other process. If such a lock is held, set *pResOut
41345** to a non-zero value otherwise *pResOut is set to zero. The return value
41346** is set to SQLITE_OK unless an I/O error occurs during lock checking.
41347*/
41348static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
41349 int rc = SQLITE_OK0;
41350 int reserved = 0;
41351 unixFile *pFile = (unixFile*)id;
41352 afpLockingContext *context;
41353
41354 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
41355
41356 assert( pFile )((void) (0));
41357 context = (afpLockingContext *) pFile->lockingContext;
41358 if( context->reserved ){
41359 *pResOut = 1;
41360 return SQLITE_OK0;
41361 }
41362 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
41363 /* Check if a thread in this process holds such a lock */
41364 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
41365 reserved = 1;
41366 }
41367
41368 /* Otherwise see if some other process holds it.
41369 */
41370 if( !reserved ){
41371 /* lock the RESERVED byte */
41372 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
41373 if( SQLITE_OK0==lrc ){
41374 /* if we succeeded in taking the reserved lock, unlock it to restore
41375 ** the original state */
41376 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
41377 } else {
41378 /* if we failed to get the lock then someone else must have it */
41379 reserved = 1;
41380 }
41381 if( IS_LOCK_ERROR(lrc)((lrc != 0) && (lrc != 5)) ){
41382 rc=lrc;
41383 }
41384 }
41385
41386 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
41387 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
41388
41389 *pResOut = reserved;
41390 return rc;
41391}
41392
41393/*
41394** Lock the file with the lock specified by parameter eFileLock - one
41395** of the following:
41396**
41397** (1) SHARED_LOCK
41398** (2) RESERVED_LOCK
41399** (3) PENDING_LOCK
41400** (4) EXCLUSIVE_LOCK
41401**
41402** Sometimes when requesting one lock state, additional lock states
41403** are inserted in between. The locking might fail on one of the later
41404** transitions leaving the lock state different from what it started but
41405** still short of its goal. The following chart shows the allowed
41406** transitions and the inserted intermediate states:
41407**
41408** UNLOCKED -> SHARED
41409** SHARED -> RESERVED
41410** SHARED -> (PENDING) -> EXCLUSIVE
41411** RESERVED -> (PENDING) -> EXCLUSIVE
41412** PENDING -> EXCLUSIVE
41413**
41414** This routine will only increase a lock. Use the sqlite3OsUnlock()
41415** routine to lower a locking level.
41416*/
41417static int afpLock(sqlite3_file *id, int eFileLock){
41418 int rc = SQLITE_OK0;
41419 unixFile *pFile = (unixFile*)id;
41420 unixInodeInfo *pInode = pFile->pInode;
41421 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41422
41423 assert( pFile )((void) (0));
41424 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
41425 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
41426 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
41427
41428 /* If there is already a lock of this type or more restrictive on the
41429 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
41430 ** unixEnterMutex() hasn't been called yet.
41431 */
41432 if( pFile->eFileLock>=eFileLock ){
41433 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
41434 azFileLock(eFileLock)));
41435 return SQLITE_OK0;
41436 }
41437
41438 /* Make sure the locking sequence is correct
41439 ** (1) We never move from unlocked to anything higher than shared lock.
41440 ** (2) SQLite never explicitly requests a pending lock.
41441 ** (3) A shared lock is always held when a reserve lock is requested.
41442 */
41443 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
41444 assert( eFileLock!=PENDING_LOCK )((void) (0));
41445 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
41446
41447 /* This mutex is needed because pFile->pInode is shared across threads
41448 */
41449 pInode = pFile->pInode;
41450 sqlite3_mutex_enter(pInode->pLockMutex);
41451
41452 /* If some thread using this PID has a lock via a different unixFile*
41453 ** handle that precludes the requested lock, return BUSY.
41454 */
41455 if( (pFile->eFileLock!=pInode->eFileLock &&
41456 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
41457 ){
41458 rc = SQLITE_BUSY5;
41459 goto afp_end_lock;
41460 }
41461
41462 /* If a SHARED lock is requested, and some thread using this PID already
41463 ** has a SHARED or RESERVED lock, then increment reference counts and
41464 ** return SQLITE_OK.
41465 */
41466 if( eFileLock==SHARED_LOCK1 &&
41467 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
41468 assert( eFileLock==SHARED_LOCK )((void) (0));
41469 assert( pFile->eFileLock==0 )((void) (0));
41470 assert( pInode->nShared>0 )((void) (0));
41471 pFile->eFileLock = SHARED_LOCK1;
41472 pInode->nShared++;
41473 pInode->nLock++;
41474 goto afp_end_lock;
41475 }
41476
41477 /* A PENDING lock is needed before acquiring a SHARED lock and before
41478 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
41479 ** be released.
41480 */
41481 if( eFileLock==SHARED_LOCK1
41482 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock<PENDING_LOCK3)
41483 ){
41484 int failed;
41485 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 1);
41486 if (failed) {
41487 rc = failed;
41488 goto afp_end_lock;
41489 }
41490 }
41491
41492 /* If control gets to this point, then actually go ahead and make
41493 ** operating system calls for the specified lock.
41494 */
41495 if( eFileLock==SHARED_LOCK1 ){
41496 int lrc1, lrc2, lrc1Errno = 0;
41497 long lk, mask;
41498
41499 assert( pInode->nShared==0 )((void) (0));
41500 assert( pInode->eFileLock==0 )((void) (0));
41501
41502 mask = (sizeof(long)==8) ? LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) : 0x7fffffff;
41503 /* Now get the read-lock SHARED_LOCK */
41504 /* note that the quality of the randomness doesn't matter that much */
41505 lk = random();
41506 pInode->sharedByte = (lk & mask)%(SHARED_SIZE510 - 1);
41507 lrc1 = afpSetLock(context->dbPath, pFile,
41508 SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte, 1, 1);
41509 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ){
41510 lrc1Errno = pFile->lastErrno;
41511 }
41512 /* Drop the temporary PENDING lock */
41513 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
41514
41515 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ) {
41516 storeLastErrno(pFile, lrc1Errno);
41517 rc = lrc1;
41518 goto afp_end_lock;
41519 } else if( IS_LOCK_ERROR(lrc2)((lrc2 != 0) && (lrc2 != 5)) ){
41520 rc = lrc2;
41521 goto afp_end_lock;
41522 } else if( lrc1 != SQLITE_OK0 ) {
41523 rc = lrc1;
41524 } else {
41525 pFile->eFileLock = SHARED_LOCK1;
41526 pInode->nLock++;
41527 pInode->nShared = 1;
41528 }
41529 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
41530 /* We are trying for an exclusive lock but another thread in this
41531 ** same process is still holding a shared lock. */
41532 rc = SQLITE_BUSY5;
41533 }else{
41534 /* The request was for a RESERVED or EXCLUSIVE lock. It is
41535 ** assumed that there is a SHARED or greater lock on the file
41536 ** already.
41537 */
41538 int failed = 0;
41539 assert( 0!=pFile->eFileLock )((void) (0));
41540 if (eFileLock >= RESERVED_LOCK2 && pFile->eFileLock < RESERVED_LOCK2) {
41541 /* Acquire a RESERVED lock */
41542 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
41543 if( !failed ){
41544 context->reserved = 1;
41545 }
41546 }
41547 if (!failed && eFileLock == EXCLUSIVE_LOCK4) {
41548 /* Acquire an EXCLUSIVE lock */
41549
41550 /* Remove the shared lock before trying the range. we'll need to
41551 ** reestablish the shared lock if we can't get the afpUnlock
41552 */
41553 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2) +
41554 pInode->sharedByte, 1, 0)) ){
41555 int failed2 = SQLITE_OK0;
41556 /* now attempt to get the exclusive lock range */
41557 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2),
41558 SHARED_SIZE510, 1);
41559 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
41560 SHARED_FIRST(sqlite3PendingByte+2) + pInode->sharedByte, 1, 1)) ){
41561 /* Can't reestablish the shared lock. Sqlite can't deal, this is
41562 ** a critical I/O error
41563 */
41564 rc = ((failed & 0xff) == SQLITE_IOERR10) ? failed2 :
41565 SQLITE_IOERR_LOCK(10 | (15<<8));
41566 goto afp_end_lock;
41567 }
41568 }else{
41569 rc = failed;
41570 }
41571 }
41572 if( failed ){
41573 rc = failed;
41574 }
41575 }
41576
41577 if( rc==SQLITE_OK0 ){
41578 pFile->eFileLock = eFileLock;
41579 pInode->eFileLock = eFileLock;
41580 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
41581 pFile->eFileLock = PENDING_LOCK3;
41582 pInode->eFileLock = PENDING_LOCK3;
41583 }
41584
41585afp_end_lock:
41586 sqlite3_mutex_leave(pInode->pLockMutex);
41587 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
41588 rc==SQLITE_OK ? "ok" : "failed"));
41589 return rc;
41590}
41591
41592/*
41593** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41594** must be either NO_LOCK or SHARED_LOCK.
41595**
41596** If the locking level of the file descriptor is already at or below
41597** the requested locking level, this routine is a no-op.
41598*/
41599static int afpUnlock(sqlite3_file *id, int eFileLock) {
41600 int rc = SQLITE_OK0;
41601 unixFile *pFile = (unixFile*)id;
41602 unixInodeInfo *pInode;
41603 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41604 int skipShared = 0;
41605
41606 assert( pFile )((void) (0));
41607 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
41608 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
41609 osGetpid(0)));
41610
41611 assert( eFileLock<=SHARED_LOCK )((void) (0));
41612 if( pFile->eFileLock<=eFileLock ){
41613 return SQLITE_OK0;
41614 }
41615 pInode = pFile->pInode;
41616 sqlite3_mutex_enter(pInode->pLockMutex);
41617 assert( pInode->nShared!=0 )((void) (0));
41618 if( pFile->eFileLock>SHARED_LOCK1 ){
41619 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
41620
41621#ifdef SQLITE_DEBUG
41622 /* When reducing a lock such that other processes can start
41623 ** reading the database file again, make sure that the
41624 ** transaction counter was updated if any part of the database
41625 ** file changed. If the transaction counter is not updated,
41626 ** other connections to the same file might not realize that
41627 ** the file has changed and hence might not know to flush their
41628 ** cache. The use of a stale cache can lead to database corruption.
41629 */
41630 assert( pFile->inNormalWrite==0((void) (0))
41631 || pFile->dbUpdate==0((void) (0))
41632 || pFile->transCntrChng==1 )((void) (0));
41633 pFile->inNormalWrite = 0;
41634#endif
41635
41636 if( pFile->eFileLock==EXCLUSIVE_LOCK4 ){
41637 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2), SHARED_SIZE510, 0);
41638 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1) ){
41639 /* only re-establish the shared lock if necessary */
41640 int sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
41641 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
41642 } else {
41643 skipShared = 1;
41644 }
41645 }
41646 if( rc==SQLITE_OK0 && pFile->eFileLock>=PENDING_LOCK3 ){
41647 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
41648 }
41649 if( rc==SQLITE_OK0 && pFile->eFileLock>=RESERVED_LOCK2 && context->reserved ){
41650 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
41651 if( !rc ){
41652 context->reserved = 0;
41653 }
41654 }
41655 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1)){
41656 pInode->eFileLock = SHARED_LOCK1;
41657 }
41658 }
41659 if( rc==SQLITE_OK0 && eFileLock==NO_LOCK0 ){
41660
41661 /* Decrement the shared lock counter. Release the lock using an
41662 ** OS call only when all threads in this same process have released
41663 ** the lock.
41664 */
41665 unsigned long long sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
41666 pInode->nShared--;
41667 if( pInode->nShared==0 ){
41668 if( !skipShared ){
41669 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
41670 }
41671 if( !rc ){
41672 pInode->eFileLock = NO_LOCK0;
41673 pFile->eFileLock = NO_LOCK0;
41674 }
41675 }
41676 if( rc==SQLITE_OK0 ){
41677 pInode->nLock--;
41678 assert( pInode->nLock>=0 )((void) (0));
41679 if( pInode->nLock==0 ) closePendingFds(pFile);
41680 }
41681 }
41682
41683 sqlite3_mutex_leave(pInode->pLockMutex);
41684 if( rc==SQLITE_OK0 ){
41685 pFile->eFileLock = eFileLock;
41686 }
41687 return rc;
41688}
41689
41690/*
41691** Close a file & cleanup AFP specific locking context
41692*/
41693static int afpClose(sqlite3_file *id) {
41694 int rc = SQLITE_OK0;
41695 unixFile *pFile = (unixFile*)id;
41696 assert( id!=0 )((void) (0));
41697 afpUnlock(id, NO_LOCK0);
41698 assert( unixFileMutexNotheld(pFile) )((void) (0));
41699 unixEnterMutex();
41700 if( pFile->pInode ){
41701 unixInodeInfo *pInode = pFile->pInode;
41702 sqlite3_mutex_enter(pInode->pLockMutex);
41703 if( pInode->nLock ){
41704 /* If there are outstanding locks, do not actually close the file just
41705 ** yet because that would clear those locks. Instead, add the file
41706 ** descriptor to pInode->aPending. It will be automatically closed when
41707 ** the last lock is cleared.
41708 */
41709 setPendingFd(pFile);
41710 }
41711 sqlite3_mutex_leave(pInode->pLockMutex);
41712 }
41713 releaseInodeInfo(pFile);
41714 sqlite3_free(pFile->lockingContext);
41715 rc = closeUnixFile(id);
41716 unixLeaveMutex();
41717 return rc;
41718}
41719
41720#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
41721/*
41722** The code above is the AFP lock implementation. The code is specific
41723** to MacOSX and does not work on other unix platforms. No alternative
41724** is available. If you don't compile for a mac, then the "unix-afp"
41725** VFS is not available.
41726**
41727********************* End of the AFP lock implementation **********************
41728******************************************************************************/
41729
41730/******************************************************************************
41731*************************** Begin NFS Locking ********************************/
41732
41733#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
41734/*
41735 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
41736 ** must be either NO_LOCK or SHARED_LOCK.
41737 **
41738 ** If the locking level of the file descriptor is already at or below
41739 ** the requested locking level, this routine is a no-op.
41740 */
41741static int nfsUnlock(sqlite3_file *id, int eFileLock){
41742 return posixUnlock(id, eFileLock, 1);
41743}
41744
41745#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
41746/*
41747** The code above is the NFS lock implementation. The code is specific
41748** to MacOSX and does not work on other unix platforms. No alternative
41749** is available.
41750**
41751********************* End of the NFS lock implementation **********************
41752******************************************************************************/
41753
41754/******************************************************************************
41755**************** Non-locking sqlite3_file methods *****************************
41756**
41757** The next division contains implementations for all methods of the
41758** sqlite3_file object other than the locking methods. The locking
41759** methods were defined in divisions above (one locking method per
41760** division). Those methods that are common to all locking modes
41761** are gather together into this division.
41762*/
41763
41764/*
41765** Seek to the offset passed as the second argument, then read cnt
41766** bytes into pBuf. Return the number of bytes actually read.
41767**
41768** To avoid stomping the errno value on a failed read the lastErrno value
41769** is set before returning.
41770*/
41771static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
41772 int got;
41773 int prior = 0;
41774#if (!defined(USE_PREAD1) && !defined(USE_PREAD64))
41775 i64 newOffset;
41776#endif
41777 TIMER_START;
41778 assert( cnt==(cnt&0x1ffff) )((void) (0));
41779 assert( id->h>2 )((void) (0));
41780 do{
41781#if defined(USE_PREAD1)
41782 got = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(id->h, pBuf, cnt, offset);
41783 SimulateIOError( got = -1 );
41784#elif defined(USE_PREAD64)
41785 got = osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)(id->h, pBuf, cnt, offset);
41786 SimulateIOError( got = -1 );
41787#else
41788 newOffset = lseek(id->h, offset, SEEK_SET0);
41789 SimulateIOError( newOffset = -1 );
41790 if( newOffset<0 ){
41791 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
41792 return -1;
41793 }
41794 got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(id->h, pBuf, cnt);
41795#endif
41796 if( got==cnt ) break;
41797 if( got<0 ){
41798 if( errno(*__errno_location ())==EINTR4 ){ got = 1; continue; }
41799 prior = 0;
41800 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
41801 break;
41802 }else if( got>0 ){
41803 cnt -= got;
41804 offset += got;
41805 prior += got;
41806 pBuf = (void*)(got + (char*)pBuf);
41807 }
41808 }while( got>0 );
41809 TIMER_END;
41810 OSTRACE(("READ %-3d %5d %7lld %llu\n",
41811 id->h, got+prior, offset-prior, TIMER_ELAPSED));
41812 return got+prior;
41813}
41814
41815/*
41816** Read data from a file into a buffer. Return SQLITE_OK if all
41817** bytes were read successfully and SQLITE_IOERR if anything goes
41818** wrong.
41819*/
41820static int unixRead(
41821 sqlite3_file *id,
41822 void *pBuf,
41823 int amt,
41824 sqlite3_int64 offset
41825){
41826 unixFile *pFile = (unixFile *)id;
41827 int got;
41828 assert( id )((void) (0));
41829 assert( offset>=0 )((void) (0));
41830 assert( amt>0 )((void) (0));
41831
41832 /* If this is a database file (not a journal, super-journal or temp
41833 ** file), the bytes in the locking range should never be read or written. */
41834#if 0
41835 assert( pFile->pPreallocatedUnused==0((void) (0))
41836 || offset>=PENDING_BYTE+512((void) (0))
41837 || offset+amt<=PENDING_BYTE((void) (0))
41838 )((void) (0));
41839#endif
41840
41841#if SQLITE_MAX_MMAP_SIZE20971520>0
41842 /* Deal with as much of this read request as possible by transferring
41843 ** data from the memory mapping using memcpy(). */
41844 if( offset<pFile->mmapSize ){
41845 if( offset+amt <= pFile->mmapSize ){
41846 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
41847 return SQLITE_OK0;
41848 }else{
41849 int nCopy = pFile->mmapSize - offset;
41850 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
41851 pBuf = &((u8 *)pBuf)[nCopy];
41852 amt -= nCopy;
41853 offset += nCopy;
41854 }
41855 }
41856#endif
41857
41858 got = seekAndRead(pFile, offset, pBuf, amt);
41859 if( got==amt ){
41860 return SQLITE_OK0;
41861 }else if( got<0 ){
41862 /* pFile->lastErrno has been set by seekAndRead().
41863 ** Usually we return SQLITE_IOERR_READ here, though for some
41864 ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The
41865 ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT
41866 ** prior to returning to the application by the sqlite3ApiExit()
41867 ** routine.
41868 */
41869 switch( pFile->lastErrno ){
41870 case ERANGE34:
41871 case EIO5:
41872#ifdef ENXIO6
41873 case ENXIO6:
41874#endif
41875#ifdef EDEVERR
41876 case EDEVERR:
41877#endif
41878 return SQLITE_IOERR_CORRUPTFS(10 | (33<<8));
41879 }
41880 return SQLITE_IOERR_READ(10 | (1<<8));
41881 }else{
41882 storeLastErrno(pFile, 0); /* not a system error */
41883 /* Unread parts of the buffer must be zero-filled */
41884 memset(&((char*)pBuf)[got], 0, amt-got);
41885 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
41886 }
41887}
41888
41889/*
41890** Attempt to seek the file-descriptor passed as the first argument to
41891** absolute offset iOff, then attempt to write nBuf bytes of data from
41892** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
41893** return the actual number of bytes written (which may be less than
41894** nBuf).
41895*/
41896static int seekAndWriteFd(
41897 int fd, /* File descriptor to write to */
41898 i64 iOff, /* File offset to begin writing at */
41899 const void *pBuf, /* Copy data from this buffer to the file */
41900 int nBuf, /* Size of buffer pBuf in bytes */
41901 int *piErrno /* OUT: Error number if error occurs */
41902){
41903 int rc = 0; /* Value returned by system call */
41904
41905 assert( nBuf==(nBuf&0x1ffff) )((void) (0));
41906 assert( fd>2 )((void) (0));
41907 assert( piErrno!=0 )((void) (0));
41908 nBuf &= 0x1ffff;
41909 TIMER_START;
41910
41911#if defined(USE_PREAD1)
41912 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 );
41913#elif defined(USE_PREAD64)
41914 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);
41915#else
41916 do{
41917 i64 iSeek = lseek(fd, iOff, SEEK_SET0);
41918 SimulateIOError( iSeek = -1 );
41919 if( iSeek<0 ){
41920 rc = -1;
41921 break;
41922 }
41923 rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, pBuf, nBuf);
41924 }while( rc<0 && errno(*__errno_location ())==EINTR4 );
41925#endif
41926
41927 TIMER_END;
41928 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
41929
41930 if( rc<0 ) *piErrno = errno(*__errno_location ());
41931 return rc;
41932}
41933
41934
41935/*
41936** Seek to the offset in id->offset then read cnt bytes into pBuf.
41937** Return the number of bytes actually read. Update the offset.
41938**
41939** To avoid stomping the errno value on a failed write the lastErrno value
41940** is set before returning.
41941*/
41942static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
41943 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
41944}
41945
41946
41947/*
41948** Write data from a buffer into a file. Return SQLITE_OK on success
41949** or some other error code on failure.
41950*/
41951static int unixWrite(
41952 sqlite3_file *id,
41953 const void *pBuf,
41954 int amt,
41955 sqlite3_int64 offset
41956){
41957 unixFile *pFile = (unixFile*)id;
41958 int wrote = 0;
41959 assert( id )((void) (0));
41960 assert( amt>0 )((void) (0));
41961
41962 /* If this is a database file (not a journal, super-journal or temp
41963 ** file), the bytes in the locking range should never be read or written. */
41964#if 0
41965 assert( pFile->pPreallocatedUnused==0((void) (0))
41966 || offset>=PENDING_BYTE+512((void) (0))
41967 || offset+amt<=PENDING_BYTE((void) (0))
41968 )((void) (0));
41969#endif
41970
41971#ifdef SQLITE_DEBUG
41972 /* If we are doing a normal write to a database file (as opposed to
41973 ** doing a hot-journal rollback or a write to some file other than a
41974 ** normal database file) then record the fact that the database
41975 ** has changed. If the transaction counter is modified, record that
41976 ** fact too.
41977 */
41978 if( pFile->inNormalWrite ){
41979 pFile->dbUpdate = 1; /* The database has been modified */
41980 if( offset<=24 && offset+amt>=27 ){
41981 int rc;
41982 char oldCntr[4];
41983 SimulateIOErrorBenign(1);
41984 rc = seekAndRead(pFile, 24, oldCntr, 4);
41985 SimulateIOErrorBenign(0);
41986 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
41987 pFile->transCntrChng = 1; /* The transaction counter has changed */
41988 }
41989 }
41990 }
41991#endif
41992
41993#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE20971520>0
41994 /* Deal with as much of this write request as possible by transferring
41995 ** data from the memory mapping using memcpy(). */
41996 if( offset<pFile->mmapSize ){
41997 if( offset+amt <= pFile->mmapSize ){
41998 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41999 return SQLITE_OK0;
42000 }else{
42001 int nCopy = pFile->mmapSize - offset;
42002 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
42003 pBuf = &((u8 *)pBuf)[nCopy];
42004 amt -= nCopy;
42005 offset += nCopy;
42006 }
42007 }
42008#endif
42009
42010 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
42011 amt -= wrote;
42012 offset += wrote;
42013 pBuf = &((char*)pBuf)[wrote];
42014 }
42015 SimulateIOError(( wrote=(-1), amt=1 ));
42016 SimulateDiskfullError(( wrote=0, amt=1 ));
42017
42018 if( amt>wrote ){
42019 if( wrote<0 && pFile->lastErrno!=ENOSPC28 ){
42020 /* lastErrno set by seekAndWrite */
42021 return SQLITE_IOERR_WRITE(10 | (3<<8));
42022 }else{
42023 storeLastErrno(pFile, 0); /* not a system error */
42024 return SQLITE_FULL13;
42025 }
42026 }
42027
42028 return SQLITE_OK0;
42029}
42030
42031#ifdef SQLITE_TEST
42032/*
42033** Count the number of fullsyncs and normal syncs. This is used to test
42034** that syncs and fullsyncs are occurring at the right times.
42035*/
42036SQLITE_API int sqlite3_sync_count = 0;
42037SQLITE_API int sqlite3_fullsync_count = 0;
42038#endif
42039
42040/*
42041** We do not trust systems to provide a working fdatasync(). Some do.
42042** Others do no. To be safe, we will stick with the (slightly slower)
42043** fsync(). If you know that your system does support fdatasync() correctly,
42044** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
42045*/
42046#if !defined(fdatasyncfsync) && !HAVE_FDATASYNC
42047# define fdatasyncfsync fsync
42048#endif
42049
42050/*
42051** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
42052** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
42053** only available on Mac OS X. But that could change.
42054*/
42055#ifdef F_FULLFSYNC
42056# define HAVE_FULLFSYNC0 1
42057#else
42058# define HAVE_FULLFSYNC0 0
42059#endif
42060
42061
42062/*
42063** The fsync() system call does not work as advertised on many
42064** unix systems. The following procedure is an attempt to make
42065** it work better.
42066**
42067** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
42068** for testing when we want to run through the test suite quickly.
42069** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
42070** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
42071** or power failure will likely corrupt the database file.
42072**
42073** SQLite sets the dataOnly flag if the size of the file is unchanged.
42074** The idea behind dataOnly is that it should only write the file content
42075** to disk, not the inode. We only set dataOnly if the file size is
42076** unchanged since the file size is part of the inode. However,
42077** Ted Ts'o tells us that fdatasync() will also write the inode if the
42078** file size has changed. The only real difference between fdatasync()
42079** and fsync(), Ted tells us, is that fdatasync() will not flush the
42080** inode if the mtime or owner or other inode attributes have changed.
42081** We only care about the file size, not the other file attributes, so
42082** as far as SQLite is concerned, an fdatasync() is always adequate.
42083** So, we always use fdatasync() if it is available, regardless of
42084** the value of the dataOnly flag.
42085*/
42086static int full_fsync(int fd, int fullSync, int dataOnly){
42087 int rc;
42088
42089 /* The following "ifdef/elif/else/" block has the same structure as
42090 ** the one below. It is replicated here solely to avoid cluttering
42091 ** up the real code with the UNUSED_PARAMETER() macros.
42092 */
42093#ifdef SQLITE_NO_SYNC
42094 UNUSED_PARAMETER(fd)(void)(fd);
42095 UNUSED_PARAMETER(fullSync)(void)(fullSync);
42096 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
42097#elif HAVE_FULLFSYNC0
42098 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
42099#else
42100 UNUSED_PARAMETER(fullSync)(void)(fullSync);
42101 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
42102#endif
42103
42104 /* Record the number of times that we do a normal fsync() and
42105 ** FULLSYNC. This is used during testing to verify that this procedure
42106 ** gets called with the correct arguments.
42107 */
42108#ifdef SQLITE_TEST
42109 if( fullSync ) sqlite3_fullsync_count++;
42110 sqlite3_sync_count++;
42111#endif
42112
42113 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
42114 ** no-op. But go ahead and call fstat() to validate the file
42115 ** descriptor as we need a method to provoke a failure during
42116 ** coverage testing.
42117 */
42118#ifdef SQLITE_NO_SYNC
42119 {
42120 struct stat buf;
42121 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &buf);
42122 }
42123#elif HAVE_FULLFSYNC0
42124 if( fullSync ){
42125 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_FULLFSYNC, 0);
42126 }else{
42127 rc = 1;
42128 }
42129 /* If the FULLFSYNC failed, fall back to attempting an fsync().
42130 ** It shouldn't be possible for fullfsync to fail on the local
42131 ** file system (on OSX), so failure indicates that FULLFSYNC
42132 ** isn't supported for this file system. So, attempt an fsync
42133 ** and (for now) ignore the overhead of a superfluous fcntl call.
42134 ** It'd be better to detect fullfsync support once and avoid
42135 ** the fcntl call every time sync is called.
42136 */
42137 if( rc ) rc = fsync(fd);
42138
42139#elif defined(__APPLE__)
42140 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
42141 ** so currently we default to the macro that redefines fdatasync to fsync
42142 */
42143 rc = fsync(fd);
42144#else
42145 rc = fdatasyncfsync(fd);
42146#if OS_VXWORKS0
42147 if( rc==-1 && errno(*__errno_location ())==ENOTSUP95 ){
42148 rc = fsync(fd);
42149 }
42150#endif /* OS_VXWORKS */
42151#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
42152
42153 if( OS_VXWORKS0 && rc!= -1 ){
42154 rc = 0;
42155 }
42156 return rc;
42157}
42158
42159/*
42160** Open a file descriptor to the directory containing file zFilename.
42161** If successful, *pFd is set to the opened file descriptor and
42162** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
42163** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
42164** value.
42165**
42166** The directory file descriptor is used for only one thing - to
42167** fsync() a directory to make sure file creation and deletion events
42168** are flushed to disk. Such fsyncs are not needed on newer
42169** journaling filesystems, but are required on older filesystems.
42170**
42171** This routine can be overridden using the xSetSysCall interface.
42172** The ability to override this routine was added in support of the
42173** chromium sandbox. Opening a directory is a security risk (we are
42174** told) so making it overrideable allows the chromium sandbox to
42175** replace this routine with a harmless no-op. To make this routine
42176** a no-op, replace it with a stub that returns SQLITE_OK but leaves
42177** *pFd set to a negative number.
42178**
42179** If SQLITE_OK is returned, the caller is responsible for closing
42180** the file descriptor *pFd using close().
42181*/
42182static int openDirectory(const char *zFilename, int *pFd){
42183 int ii;
42184 int fd = -1;
42185 char zDirname[MAX_PATHNAME512+1];
42186
42187 sqlite3_snprintf(MAX_PATHNAME512, zDirname, "%s", zFilename);
42188 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
42189 if( ii>0 ){
42190 zDirname[ii] = '\0';
42191 }else{
42192 if( zDirname[0]!='/' ) zDirname[0] = '.';
42193 zDirname[1] = 0;
42194 }
42195 fd = robust_open(zDirname, O_RDONLY00|O_BINARY0, 0);
42196 if( fd>=0 ){
42197 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
42198 }
42199 *pFd = fd;
42200 if( fd>=0 ) return SQLITE_OK0;
42201 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname)unixLogErrorAtLine(sqlite3CantopenError(42201),"openDirectory"
,zDirname,42201)
;
42202}
42203
42204/*
42205** Make sure all writes to a particular file are committed to disk.
42206**
42207** If dataOnly==0 then both the file itself and its metadata (file
42208** size, access time, etc) are synced. If dataOnly!=0 then only the
42209** file data is synced.
42210**
42211** Under Unix, also make sure that the directory entry for the file
42212** has been created by fsync-ing the directory that contains the file.
42213** If we do not do this and we encounter a power failure, the directory
42214** entry for the journal might not exist after we reboot. The next
42215** SQLite to access the file will not know that the journal exists (because
42216** the directory entry for the journal was never created) and the transaction
42217** will not roll back - possibly leading to database corruption.
42218*/
42219static int unixSync(sqlite3_file *id, int flags){
42220 int rc;
42221 unixFile *pFile = (unixFile*)id;
42222
42223 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY0x00010);
42224 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL0x00003;
42225
42226 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
42227 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
42228 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
42229 )((void) (0));
42230
42231 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
42232 ** line is to test that doing so does not cause any problems.
42233 */
42234 SimulateDiskfullError( return SQLITE_FULL );
42235
42236 assert( pFile )((void) (0));
42237 OSTRACE(("SYNC %-3d\n", pFile->h));
42238 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
42239 SimulateIOError( rc=1 );
42240 if( rc ){
42241 storeLastErrno(pFile, errno(*__errno_location ()));
42242 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath)unixLogErrorAtLine((10 | (4<<8)),"full_fsync",pFile->
zPath,42242)
;
42243 }
42244
42245 /* Also fsync the directory containing the file if the DIRSYNC flag
42246 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
42247 ** are unable to fsync a directory, so ignore errors on the fsync.
42248 */
42249 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC0x08 ){
42250 int dirfd;
42251 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
42252 HAVE_FULLFSYNC, isFullsync));
42253 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(pFile->zPath, &dirfd);
42254 if( rc==SQLITE_OK0 ){
42255 full_fsync(dirfd, 0, 0);
42256 robust_close(pFile, dirfd, __LINE__42256);
42257 }else{
42258 assert( rc==SQLITE_CANTOPEN )((void) (0));
42259 rc = SQLITE_OK0;
42260 }
42261 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC0x08;
42262 }
42263 return rc;
42264}
42265
42266/*
42267** Truncate an open file to a specified size
42268*/
42269static int unixTruncate(sqlite3_file *id, i64 nByte){
42270 unixFile *pFile = (unixFile *)id;
42271 int rc;
42272 assert( pFile )((void) (0));
42273 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
42274
42275 /* If the user has configured a chunk-size for this file, truncate the
42276 ** file so that it consists of an integer number of chunks (i.e. the
42277 ** actual file size after the operation may be larger than the requested
42278 ** size).
42279 */
42280 if( pFile->szChunk>0 ){
42281 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
42282 }
42283
42284 rc = robust_ftruncate(pFile->h, nByte);
42285 if( rc ){
42286 storeLastErrno(pFile, errno(*__errno_location ()));
42287 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,42287)
;
42288 }else{
42289#ifdef SQLITE_DEBUG
42290 /* If we are doing a normal write to a database file (as opposed to
42291 ** doing a hot-journal rollback or a write to some file other than a
42292 ** normal database file) and we truncate the file to zero length,
42293 ** that effectively updates the change counter. This might happen
42294 ** when restoring a database using the backup API from a zero-length
42295 ** source.
42296 */
42297 if( pFile->inNormalWrite && nByte==0 ){
42298 pFile->transCntrChng = 1;
42299 }
42300#endif
42301
42302#if SQLITE_MAX_MMAP_SIZE20971520>0
42303 /* If the file was just truncated to a size smaller than the currently
42304 ** mapped region, reduce the effective mapping size as well. SQLite will
42305 ** use read() and write() to access data beyond this point from now on.
42306 */
42307 if( nByte<pFile->mmapSize ){
42308 pFile->mmapSize = nByte;
42309 }
42310#endif
42311
42312 return SQLITE_OK0;
42313 }
42314}
42315
42316/*
42317** Determine the current size of a file in bytes
42318*/
42319static int unixFileSize(sqlite3_file *id, i64 *pSize){
42320 int rc;
42321 struct stat buf;
42322 assert( id )((void) (0));
42323 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(((unixFile*)id)->h, &buf);
42324 SimulateIOError( rc=1 );
42325 if( rc!=0 ){
42326 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
42327 return SQLITE_IOERR_FSTAT(10 | (7<<8));
42328 }
42329 *pSize = buf.st_size;
42330
42331 /* When opening a zero-size database, the findInodeInfo() procedure
42332 ** writes a single byte into that file in order to work around a bug
42333 ** in the OS-X msdos filesystem. In order to avoid problems with upper
42334 ** layers, we need to report this file size as zero even though it is
42335 ** really 1. Ticket #3260.
42336 */
42337 if( *pSize==1 ) *pSize = 0;
42338
42339
42340 return SQLITE_OK0;
42341}
42342
42343#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
42344/*
42345** Handler for proxy-locking file-control verbs. Defined below in the
42346** proxying locking division.
42347*/
42348static int proxyFileControl(sqlite3_file*,int,void*);
42349#endif
42350
42351/*
42352** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
42353** file-control operation. Enlarge the database to nBytes in size
42354** (rounded up to the next chunk-size). If the database is already
42355** nBytes or larger, this routine is a no-op.
42356*/
42357static int fcntlSizeHint(unixFile *pFile, i64 nByte){
42358 if( pFile->szChunk>0 ){
42359 i64 nSize; /* Required file size */
42360 struct stat buf; /* Used to hold return values of fstat() */
42361
42362 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf) ){
42363 return SQLITE_IOERR_FSTAT(10 | (7<<8));
42364 }
42365
42366 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
42367 if( nSize>(i64)buf.st_size ){
42368
42369#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
42370 /* The code below is handling the return value of osFallocate()
42371 ** correctly. posix_fallocate() is defined to "returns zero on success,
42372 ** or an error number on failure". See the manpage for details. */
42373 int err;
42374 do{
42375 err = osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)(pFile->h, buf.st_size, nSize-buf.st_size);
42376 }while( err==EINTR4 );
42377 if( err && err!=EINVAL22 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
42378#else
42379 /* If the OS does not have posix_fallocate(), fake it. Write a
42380 ** single byte to the last byte in each block that falls entirely
42381 ** within the extended region. Then, if required, a single byte
42382 ** at offset (nSize-1), to set the size of the file correctly.
42383 ** This is a similar technique to that used by glibc on systems
42384 ** that do not have a real fallocate() call.
42385 */
42386 int nBlk = buf.st_blksize; /* File-system block size */
42387 int nWrite = 0; /* Number of bytes written by seekAndWrite */
42388 i64 iWrite; /* Next offset to write to */
42389
42390 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
42391 assert( iWrite>=buf.st_size )((void) (0));
42392 assert( ((iWrite+1)%nBlk)==0 )((void) (0));
42393 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
42394 if( iWrite>=nSize ) iWrite = nSize - 1;
42395 nWrite = seekAndWrite(pFile, iWrite, "", 1);
42396 if( nWrite!=1 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
42397 }
42398#endif
42399 }
42400 }
42401
42402#if SQLITE_MAX_MMAP_SIZE20971520>0
42403 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
42404 int rc;
42405 if( pFile->szChunk<=0 ){
42406 if( robust_ftruncate(pFile->h, nByte) ){
42407 storeLastErrno(pFile, errno(*__errno_location ()));
42408 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,42408)
;
42409 }
42410 }
42411
42412 rc = unixMapfile(pFile, nByte);
42413 return rc;
42414 }
42415#endif
42416
42417 return SQLITE_OK0;
42418}
42419
42420/*
42421** If *pArg is initially negative then this is a query. Set *pArg to
42422** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
42423**
42424** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
42425*/
42426static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
42427 if( *pArg<0 ){
42428 *pArg = (pFile->ctrlFlags & mask)!=0;
42429 }else if( (*pArg)==0 ){
42430 pFile->ctrlFlags &= ~mask;
42431 }else{
42432 pFile->ctrlFlags |= mask;
42433 }
42434}
42435
42436/* Forward declaration */
42437static int unixGetTempname(int nBuf, char *zBuf);
42438#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)
42439 static int unixFcntlExternalReader(unixFile*, int*);
42440#endif
42441
42442/*
42443** Information and control of an open file handle.
42444*/
42445static int unixFileControl(sqlite3_file *id, int op, void *pArg){
42446 unixFile *pFile = (unixFile*)id;
42447 switch( op ){
42448#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
42449 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31: {
42450 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
);
42451 return rc ? SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) : SQLITE_OK0;
42452 }
42453 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32: {
42454 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
);
42455 return rc ? SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) : SQLITE_OK0;
42456 }
42457 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33: {
42458 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
);
42459 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) : SQLITE_OK0;
42460 }
42461#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
42462
42463 case SQLITE_FCNTL_LOCKSTATE1: {
42464 *(int*)pArg = pFile->eFileLock;
42465 return SQLITE_OK0;
42466 }
42467 case SQLITE_FCNTL_LAST_ERRNO4: {
42468 *(int*)pArg = pFile->lastErrno;
42469 return SQLITE_OK0;
42470 }
42471 case SQLITE_FCNTL_CHUNK_SIZE6: {
42472 pFile->szChunk = *(int *)pArg;
42473 return SQLITE_OK0;
42474 }
42475 case SQLITE_FCNTL_SIZE_HINT5: {
42476 int rc;
42477 SimulateIOErrorBenign(1);
42478 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
42479 SimulateIOErrorBenign(0);
42480 return rc;
42481 }
42482 case SQLITE_FCNTL_PERSIST_WAL10: {
42483 unixModeBit(pFile, UNIXFILE_PERSIST_WAL0x04, (int*)pArg);
42484 return SQLITE_OK0;
42485 }
42486 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
42487 unixModeBit(pFile, UNIXFILE_PSOW0x10, (int*)pArg);
42488 return SQLITE_OK0;
42489 }
42490 case SQLITE_FCNTL_VFSNAME12: {
42491 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
42492 return SQLITE_OK0;
42493 }
42494 case SQLITE_FCNTL_TEMPFILENAME16: {
42495 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
42496 if( zTFile ){
42497 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
42498 *(char**)pArg = zTFile;
42499 }
42500 return SQLITE_OK0;
42501 }
42502 case SQLITE_FCNTL_HAS_MOVED20: {
42503 *(int*)pArg = fileHasMoved(pFile);
42504 return SQLITE_OK0;
42505 }
42506#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42507 case SQLITE_FCNTL_LOCK_TIMEOUT34: {
42508 int iOld = pFile->iBusyTimeout;
42509#if SQLITE_ENABLE_SETLK_TIMEOUT==1
42510 pFile->iBusyTimeout = *(int*)pArg;
42511#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
42512 pFile->iBusyTimeout = !!(*(int*)pArg);
42513#else
42514# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
42515#endif
42516 *(int*)pArg = iOld;
42517 return SQLITE_OK0;
42518 }
42519#endif
42520#if SQLITE_MAX_MMAP_SIZE20971520>0
42521 case SQLITE_FCNTL_MMAP_SIZE18: {
42522 i64 newLimit = *(i64*)pArg;
42523 int rc = SQLITE_OK0;
42524 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
42525 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
42526 }
42527
42528 /* The value of newLimit may be eventually cast to (size_t) and passed
42529 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
42530 ** 64-bit type. */
42531 if( newLimit>0 && sizeof(size_t)<8 ){
42532 newLimit = (newLimit & 0x7FFFFFFF);
42533 }
42534
42535 *(i64*)pArg = pFile->mmapSizeMax;
42536 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
42537 pFile->mmapSizeMax = newLimit;
42538 if( pFile->mmapSize>0 ){
42539 unixUnmapfile(pFile);
42540 rc = unixMapfile(pFile, -1);
42541 }
42542 }
42543 return rc;
42544 }
42545#endif
42546#ifdef SQLITE_DEBUG
42547 /* The pager calls this method to signal that it has done
42548 ** a rollback and that the database is therefore unchanged and
42549 ** it hence it is OK for the transaction change counter to be
42550 ** unchanged.
42551 */
42552 case SQLITE_FCNTL_DB_UNCHANGED0xca093fa0: {
42553 ((unixFile*)id)->dbUpdate = 0;
42554 return SQLITE_OK0;
42555 }
42556#endif
42557#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
42558 case SQLITE_FCNTL_SET_LOCKPROXYFILE3:
42559 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
42560 return proxyFileControl(id,op,pArg);
42561 }
42562#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
42563
42564 case SQLITE_FCNTL_EXTERNAL_READER40: {
42565#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)
42566 return unixFcntlExternalReader((unixFile*)id, (int*)pArg);
42567#else
42568 *(int*)pArg = 0;
42569 return SQLITE_OK0;
42570#endif
42571 }
42572 }
42573 return SQLITE_NOTFOUND12;
42574}
42575
42576/*
42577** If pFd->sectorSize is non-zero when this function is called, it is a
42578** no-op. Otherwise, the values of pFd->sectorSize and
42579** pFd->deviceCharacteristics are set according to the file-system
42580** characteristics.
42581**
42582** There are two versions of this function. One for QNX and one for all
42583** other systems.
42584*/
42585#ifndef __QNXNTO__
42586static void setDeviceCharacteristics(unixFile *pFd){
42587 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 )((void) (0));
42588 if( pFd->sectorSize==0 ){
42589#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
42590 int res;
42591 u32 f = 0;
42592
42593 /* Check for support for F2FS atomic batch writes. */
42594 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
, &f);
42595 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE0x0004) ){
42596 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC0x00004000;
42597 }
42598#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
42599
42600 /* Set the POWERSAFE_OVERWRITE flag if requested. */
42601 if( pFd->ctrlFlags & UNIXFILE_PSOW0x10 ){
42602 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000;
42603 }
42604 pFd->deviceCharacteristics |= SQLITE_IOCAP_SUBPAGE_READ0x00008000;
42605
42606 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
42607 }
42608}
42609#else
42610#include <sys/dcmd_blk.h>
42611#include <sys/statvfs.h>
42612static void setDeviceCharacteristics(unixFile *pFile){
42613 if( pFile->sectorSize == 0 ){
42614 struct statvfs fsInfo;
42615
42616 /* Set defaults for non-supported filesystems */
42617 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
42618 pFile->deviceCharacteristics = 0;
42619 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
42620 return;
42621 }
42622
42623 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
42624 pFile->sectorSize = fsInfo.f_bsize;
42625 pFile->deviceCharacteristics =
42626 SQLITE_IOCAP_ATOMIC4K0x00000010 | /* All ram filesystem writes are atomic */
42627 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
42628 ** the write succeeds */
42629 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
42630 ** so it is ordered */
42631 0;
42632 }else if( strstr(fsInfo.f_basetype, "etfs") ){
42633 pFile->sectorSize = fsInfo.f_bsize;
42634 pFile->deviceCharacteristics =
42635 /* etfs cluster size writes are atomic */
42636 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) |
42637 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
42638 ** the write succeeds */
42639 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
42640 ** so it is ordered */
42641 0;
42642 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
42643 pFile->sectorSize = fsInfo.f_bsize;
42644 pFile->deviceCharacteristics =
42645 SQLITE_IOCAP_ATOMIC0x00000001 | /* All filesystem writes are atomic */
42646 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
42647 ** the write succeeds */
42648 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
42649 ** so it is ordered */
42650 0;
42651 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
42652 pFile->sectorSize = fsInfo.f_bsize;
42653 pFile->deviceCharacteristics =
42654 /* full bitset of atomics from max sector size and smaller */
42655 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2) |
42656 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
42657 ** so it is ordered */
42658 0;
42659 }else if( strstr(fsInfo.f_basetype, "dos") ){
42660 pFile->sectorSize = fsInfo.f_bsize;
42661 pFile->deviceCharacteristics =
42662 /* full bitset of atomics from max sector size and smaller */
42663 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2) |
42664 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
42665 ** so it is ordered */
42666 0;
42667 }else{
42668 pFile->deviceCharacteristics =
42669 SQLITE_IOCAP_ATOMIC5120x00000002 | /* blocks are atomic */
42670 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
42671 ** the write succeeds */
42672 0;
42673 }
42674 }
42675 /* Last chance verification. If the sector size isn't a multiple of 512
42676 ** then it isn't valid.*/
42677 if( pFile->sectorSize % 512 != 0 ){
42678 pFile->deviceCharacteristics = 0;
42679 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
42680 }
42681}
42682#endif
42683
42684/*
42685** Return the sector size in bytes of the underlying block device for
42686** the specified file. This is almost always 512 bytes, but may be
42687** larger for some devices.
42688**
42689** SQLite code assumes this function cannot fail. It also assumes that
42690** if two files are created in the same file-system directory (i.e.
42691** a database and its journal file) that the sector size will be the
42692** same for both.
42693*/
42694static int unixSectorSize(sqlite3_file *id){
42695 unixFile *pFd = (unixFile*)id;
42696 setDeviceCharacteristics(pFd);
42697 return pFd->sectorSize;
42698}
42699
42700/*
42701** Return the device characteristics for the file.
42702**
42703** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
42704** However, that choice is controversial since technically the underlying
42705** file system does not always provide powersafe overwrites. (In other
42706** words, after a power-loss event, parts of the file that were never
42707** written might end up being altered.) However, non-PSOW behavior is very,
42708** very rare. And asserting PSOW makes a large reduction in the amount
42709** of required I/O for journaling, since a lot of padding is eliminated.
42710** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
42711** available to turn it off and URI query parameter available to turn it off.
42712*/
42713static int unixDeviceCharacteristics(sqlite3_file *id){
42714 unixFile *pFd = (unixFile*)id;
42715 setDeviceCharacteristics(pFd);
42716 return pFd->deviceCharacteristics;
42717}
42718
42719#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
42720
42721/*
42722** Return the system page size.
42723**
42724** This function should not be called directly by other code in this file.
42725** Instead, it should be called via macro osGetpagesize().
42726*/
42727static int unixGetpagesize(void){
42728#if OS_VXWORKS0
42729 return 1024;
42730#elif defined(_BSD_SOURCE)
42731 return getpagesize();
42732#else
42733 return (int)sysconf(_SC_PAGESIZE_SC_PAGESIZE);
42734#endif
42735}
42736
42737#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
42738
42739#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)
42740
42741/*
42742** Object used to represent an shared memory buffer.
42743**
42744** When multiple threads all reference the same wal-index, each thread
42745** has its own unixShm object, but they all point to a single instance
42746** of this unixShmNode object. In other words, each wal-index is opened
42747** only once per process.
42748**
42749** Each unixShmNode object is connected to a single unixInodeInfo object.
42750** We could coalesce this object into unixInodeInfo, but that would mean
42751** every open file that does not use shared memory (in other words, most
42752** open files) would have to carry around this extra information. So
42753** the unixInodeInfo object contains a pointer to this unixShmNode object
42754** and the unixShmNode object is created only when needed.
42755**
42756** unixMutexHeld() must be true when creating or destroying
42757** this object or while reading or writing the following fields:
42758**
42759** nRef
42760**
42761** The following fields are read-only after the object is created:
42762**
42763** hShm
42764** zFilename
42765**
42766** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
42767** unixMutexHeld() is true when reading or writing any other field
42768** in this structure.
42769**
42770** aLock[SQLITE_SHM_NLOCK]:
42771** This array records the various locks held by clients on each of the
42772** SQLITE_SHM_NLOCK slots. If the aLock[] entry is set to 0, then no
42773** locks are held by the process on this slot. If it is set to -1, then
42774** some client holds an EXCLUSIVE lock on the locking slot. If the aLock[]
42775** value is set to a positive value, then it is the number of shared
42776** locks currently held on the slot.
42777**
42778** aMutex[SQLITE_SHM_NLOCK]:
42779** Normally, when SQLITE_ENABLE_SETLK_TIMEOUT is not defined, mutex
42780** pShmMutex is used to protect the aLock[] array and the right to
42781** call fcntl() on unixShmNode.hShm to obtain or release locks.
42782**
42783** If SQLITE_ENABLE_SETLK_TIMEOUT is defined though, we use an array
42784** of mutexes - one for each locking slot. To read or write locking
42785** slot aLock[iSlot], the caller must hold the corresponding mutex
42786** aMutex[iSlot]. Similarly, to call fcntl() to obtain or release a
42787** lock corresponding to slot iSlot, mutex aMutex[iSlot] must be held.
42788*/
42789struct unixShmNode {
42790 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
42791 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
42792 char *zFilename; /* Name of the mmapped file */
42793 int hShm; /* Open file descriptor */
42794 int szRegion; /* Size of shared-memory regions */
42795 u16 nRegion; /* Size of array apRegion */
42796 u8 isReadonly; /* True if read-only */
42797 u8 isUnlocked; /* True if no DMS lock held */
42798 char **apRegion; /* Array of mapped shared-memory regions */
42799 int nRef; /* Number of unixShm objects pointing to this */
42800 unixShm *pFirst; /* All unixShm objects pointing to this */
42801#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42802 sqlite3_mutex *aMutex[SQLITE_SHM_NLOCK8];
42803#endif
42804 int aLock[SQLITE_SHM_NLOCK8]; /* # shared locks on slot, -1==excl lock */
42805#ifdef SQLITE_DEBUG
42806 u8 nextShmId; /* Next available unixShm.id value */
42807#endif
42808};
42809
42810/*
42811** Structure used internally by this VFS to record the state of an
42812** open shared memory connection.
42813**
42814** The following fields are initialized when this object is created and
42815** are read-only thereafter:
42816**
42817** unixShm.pShmNode
42818** unixShm.id
42819**
42820** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
42821** be held while accessing any read/write fields.
42822*/
42823struct unixShm {
42824 unixShmNode *pShmNode; /* The underlying unixShmNode object */
42825 unixShm *pNext; /* Next unixShm with the same unixShmNode */
42826 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
42827 u8 id; /* Id of this connection within its unixShmNode */
42828 u16 sharedMask; /* Mask of shared locks held */
42829 u16 exclMask; /* Mask of exclusive locks held */
42830};
42831
42832/*
42833** Constants used for locking
42834*/
42835#define UNIX_SHM_BASE((22+8)*4) ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
42836#define UNIX_SHM_DMS(((22+8)*4)+8) (UNIX_SHM_BASE((22+8)*4)+SQLITE_SHM_NLOCK8) /* deadman switch */
42837
42838/*
42839** Use F_GETLK to check whether or not there are any readers with open
42840** wal-mode transactions in other processes on database file pFile. If
42841** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are
42842** such transactions, or 0 otherwise. If an error occurs, return an
42843** SQLite error code. The final value of *piOut is undefined in this
42844** case.
42845*/
42846static int unixFcntlExternalReader(unixFile *pFile, int *piOut){
42847 int rc = SQLITE_OK0;
42848 *piOut = 0;
42849 if( pFile->pShm){
42850 unixShmNode *pShmNode = pFile->pShm->pShmNode;
42851 struct flock f;
42852
42853 memset(&f, 0, sizeof(f));
42854 f.l_type = F_WRLCK1;
42855 f.l_whence = SEEK_SET0;
42856 f.l_start = UNIX_SHM_BASE((22+8)*4) + 3;
42857 f.l_len = SQLITE_SHM_NLOCK8 - 3;
42858
42859 sqlite3_mutex_enter(pShmNode->pShmMutex);
42860 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &f)<0 ){
42861 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
42862 }else{
42863 *piOut = (f.l_type!=F_UNLCK2);
42864 }
42865 sqlite3_mutex_leave(pShmNode->pShmMutex);
42866 }
42867
42868 return rc;
42869}
42870
42871
42872/*
42873** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
42874**
42875** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
42876** otherwise.
42877*/
42878static int unixShmSystemLock(
42879 unixFile *pFile, /* Open connection to the WAL file */
42880 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
42881 int ofst, /* First byte of the locking range */
42882 int n /* Number of bytes to lock */
42883){
42884 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
42885 struct flock f; /* The posix advisory locking structure */
42886 int rc = SQLITE_OK0; /* Result code form fcntl() */
42887
42888 pShmNode = pFile->pInode->pShmNode;
42889
42890 /* Assert that the parameters are within expected range and that the
42891 ** correct mutex or mutexes are held. */
42892 assert( pShmNode->nRef>=0 )((void) (0));
42893 assert( (ofst==UNIX_SHM_DMS && n==1)((void) (0))
42894 || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK))((void) (0))
42895 )((void) (0));
42896 if( ofst==UNIX_SHM_DMS(((22+8)*4)+8) ){
42897 assert( pShmNode->nRef>0 || unixMutexHeld() )((void) (0));
42898 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
42899 }else{
42900#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42901 int ii;
42902 for(ii=ofst-UNIX_SHM_BASE((22+8)*4); ii<ofst-UNIX_SHM_BASE((22+8)*4)+n; ii++){
42903 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) )((void) (0));
42904 }
42905#else
42906 assert( sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
42907 assert( pShmNode->nRef>0 )((void) (0));
42908#endif
42909 }
42910
42911 /* Shared locks never span more than one byte */
42912 assert( n==1 || lockType!=F_RDLCK )((void) (0));
42913
42914 /* Locks are within range */
42915 assert( n>=1 && n<=SQLITE_SHM_NLOCK )((void) (0));
42916 assert( ofst>=UNIX_SHM_BASE && ofst<=(UNIX_SHM_DMS+SQLITE_SHM_NLOCK) )((void) (0));
42917
42918 if( pShmNode->hShm>=0 ){
42919 int res;
42920 /* Initialize the locking parameters */
42921 f.l_type = lockType;
42922 f.l_whence = SEEK_SET0;
42923 f.l_start = ofst;
42924 f.l_len = n;
42925 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm
,6,&f)
;
42926 if( res==-1 ){
42927#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && SQLITE_ENABLE_SETLK_TIMEOUT==1
42928 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT(5 | (3<<8)) : SQLITE_BUSY5);
42929#else
42930 rc = SQLITE_BUSY5;
42931#endif
42932 }
42933 }
42934
42935 /* Do debug tracing */
42936#ifdef SQLITE_DEBUG
42937 OSTRACE(("SHM-LOCK "));
42938 if( rc==SQLITE_OK0 ){
42939 if( lockType==F_UNLCK2 ){
42940 OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1));
42941 }else if( lockType==F_RDLCK0 ){
42942 OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1));
42943 }else{
42944 assert( lockType==F_WRLCK )((void) (0));
42945 OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1));
42946 }
42947 }else{
42948 if( lockType==F_UNLCK2 ){
42949 OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1));
42950 }else if( lockType==F_RDLCK0 ){
42951 OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1));
42952 }else{
42953 assert( lockType==F_WRLCK )((void) (0));
42954 OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1));
42955 }
42956 }
42957#endif
42958
42959 return rc;
42960}
42961
42962/*
42963** Return the minimum number of 32KB shm regions that should be mapped at
42964** a time, assuming that each mapping must be an integer multiple of the
42965** current system page-size.
42966**
42967** Usually, this is 1. The exception seems to be systems that are configured
42968** to use 64KB pages - in this case each mapping must cover at least two
42969** shm regions.
42970*/
42971static int unixShmRegionPerMap(void){
42972 int shmsz = 32*1024; /* SHM region size */
42973 int pgsz = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)(); /* System page size */
42974 assert( ((pgsz-1)&pgsz)==0 )((void) (0)); /* Page size must be a power of 2 */
42975 if( pgsz<shmsz ) return 1;
42976 return pgsz/shmsz;
42977}
42978
42979/*
42980** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
42981**
42982** This is not a VFS shared-memory method; it is a utility function called
42983** by VFS shared-memory methods.
42984*/
42985static void unixShmPurge(unixFile *pFd){
42986 unixShmNode *p = pFd->pInode->pShmNode;
42987 assert( unixMutexHeld() )((void) (0));
42988 if( p && ALWAYS(p->nRef==0)(p->nRef==0) ){
42989 int nShmPerMap = unixShmRegionPerMap();
42990 int i;
42991 assert( p->pInode==pFd->pInode )((void) (0));
42992 sqlite3_mutex_free(p->pShmMutex);
42993#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42994 for(i=0; i<SQLITE_SHM_NLOCK8; i++){
42995 sqlite3_mutex_free(p->aMutex[i]);
42996 }
42997#endif
42998 for(i=0; i<p->nRegion; i+=nShmPerMap){
42999 if( p->hShm>=0 ){
43000 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(p->apRegion[i], p->szRegion);
43001 }else{
43002 sqlite3_free(p->apRegion[i]);
43003 }
43004 }
43005 sqlite3_free(p->apRegion);
43006 if( p->hShm>=0 ){
43007 robust_close(pFd, p->hShm, __LINE__43007);
43008 p->hShm = -1;
43009 }
43010 p->pInode->pShmNode = 0;
43011 sqlite3_free(p);
43012 }
43013}
43014
43015/*
43016** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
43017** take it now. Return SQLITE_OK if successful, or an SQLite error
43018** code otherwise.
43019**
43020** If the DMS cannot be locked because this is a readonly_shm=1
43021** connection and no other process already holds a lock, return
43022** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
43023*/
43024static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
43025 struct flock lock;
43026 int rc = SQLITE_OK0;
43027
43028 /* Use F_GETLK to determine the locks other processes are holding
43029 ** on the DMS byte. If it indicates that another process is holding
43030 ** a SHARED lock, then this process may also take a SHARED lock
43031 ** and proceed with opening the *-shm file.
43032 **
43033 ** Or, if no other process is holding any lock, then this process
43034 ** is the first to open it. In this case take an EXCLUSIVE lock on the
43035 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
43036 ** downgrade to a SHARED lock on the DMS byte.
43037 **
43038 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
43039 ** return SQLITE_BUSY to the caller (it will try again). An earlier
43040 ** version of this code attempted the SHARED lock at this point. But
43041 ** this introduced a subtle race condition: if the process holding
43042 ** EXCLUSIVE failed just before truncating the *-shm file, then this
43043 ** process might open and use the *-shm file without truncating it.
43044 ** And if the *-shm file has been corrupted by a power failure or
43045 ** system crash, the database itself may also become corrupt. */
43046 lock.l_whence = SEEK_SET0;
43047 lock.l_start = UNIX_SHM_DMS(((22+8)*4)+8);
43048 lock.l_len = 1;
43049 lock.l_type = F_WRLCK1;
43050 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &lock)!=0 ) {
43051 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
43052 }else if( lock.l_type==F_UNLCK2 ){
43053 if( pShmNode->isReadonly ){
43054 pShmNode->isUnlocked = 1;
43055 rc = SQLITE_READONLY_CANTINIT(8 | (5<<8));
43056 }else{
43057#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43058 /* Do not use a blocking lock here. If the lock cannot be obtained
43059 ** immediately, it means some other connection is truncating the
43060 ** *-shm file. And after it has done so, it will not release its
43061 ** lock, but only downgrade it to a shared lock. So no point in
43062 ** blocking here. The call below to obtain the shared DMS lock may
43063 ** use a blocking lock. */
43064 int iSaveTimeout = pDbFd->iBusyTimeout;
43065 pDbFd->iBusyTimeout = 0;
43066#endif
43067 rc = unixShmSystemLock(pDbFd, F_WRLCK1, UNIX_SHM_DMS(((22+8)*4)+8), 1);
43068#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43069 pDbFd->iBusyTimeout = iSaveTimeout;
43070#endif
43071 /* The first connection to attach must truncate the -shm file. We
43072 ** truncate to 3 bytes (an arbitrary small number, less than the
43073 ** -shm header size) rather than 0 as a system debugging aid, to
43074 ** help detect if a -shm file truncation is legitimate or is the work
43075 ** or a rogue process. */
43076 if( rc==SQLITE_OK0 && robust_ftruncate(pShmNode->hShm, 3) ){
43077 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename)unixLogErrorAtLine((10 | (18<<8)),"ftruncate",pShmNode->
zFilename,43077)
;
43078 }
43079 }
43080 }else if( lock.l_type==F_WRLCK1 ){
43081 rc = SQLITE_BUSY5;
43082 }
43083
43084 if( rc==SQLITE_OK0 ){
43085 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK )((void) (0));
43086 rc = unixShmSystemLock(pDbFd, F_RDLCK0, UNIX_SHM_DMS(((22+8)*4)+8), 1);
43087 }
43088 return rc;
43089}
43090
43091/*
43092** Open a shared-memory area associated with open database file pDbFd.
43093** This particular implementation uses mmapped files.
43094**
43095** The file used to implement shared-memory is in the same directory
43096** as the open database file and has the same name as the open database
43097** file with the "-shm" suffix added. For example, if the database file
43098** is "/home/user1/config.db" then the file that is created and mmapped
43099** for shared memory will be called "/home/user1/config.db-shm".
43100**
43101** Another approach to is to use files in /dev/shm or /dev/tmp or an
43102** some other tmpfs mount. But if a file in a different directory
43103** from the database file is used, then differing access permissions
43104** or a chroot() might cause two different processes on the same
43105** database to end up using different files for shared memory -
43106** meaning that their memory would not really be shared - resulting
43107** in database corruption. Nevertheless, this tmpfs file usage
43108** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
43109** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
43110** option results in an incompatible build of SQLite; builds of SQLite
43111** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
43112** same database file at the same time, database corruption will likely
43113** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
43114** "unsupported" and may go away in a future SQLite release.
43115**
43116** When opening a new shared-memory file, if no other instances of that
43117** file are currently open, in this process or in other processes, then
43118** the file must be truncated to zero length or have its header cleared.
43119**
43120** If the original database file (pDbFd) is using the "unix-excl" VFS
43121** that means that an exclusive lock is held on the database file and
43122** that no other processes are able to read or write the database. In
43123** that case, we do not really need shared memory. No shared memory
43124** file is created. The shared memory will be simulated with heap memory.
43125*/
43126static int unixOpenSharedMemory(unixFile *pDbFd){
43127 struct unixShm *p = 0; /* The connection to be opened */
43128 struct unixShmNode *pShmNode; /* The underlying mmapped file */
43129 int rc = SQLITE_OK0; /* Result code */
43130 unixInodeInfo *pInode; /* The inode of fd */
43131 char *zShm; /* Name of the file used for SHM */
43132 int nShmFilename; /* Size of the SHM filename in bytes */
43133
43134 /* Allocate space for the new unixShm object. */
43135 p = sqlite3_malloc64( sizeof(*p) );
43136 if( p==0 ) return SQLITE_NOMEM_BKPT7;
43137 memset(p, 0, sizeof(*p));
43138 assert( pDbFd->pShm==0 )((void) (0));
43139
43140 /* Check to see if a unixShmNode object already exists. Reuse an existing
43141 ** one if present. Create a new one if necessary.
43142 */
43143 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
43144 unixEnterMutex();
43145 pInode = pDbFd->pInode;
43146 pShmNode = pInode->pShmNode;
43147 if( pShmNode==0 ){
43148 struct stat sStat; /* fstat() info for database file */
43149#ifndef SQLITE_SHM_DIRECTORY
43150 const char *zBasePath = pDbFd->zPath;
43151#endif
43152
43153 /* Call fstat() to figure out the permissions on the database file. If
43154 ** a new *-shm file is created, an attempt will be made to create it
43155 ** with the same permissions.
43156 */
43157 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pDbFd->h, &sStat) ){
43158 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
43159 goto shm_open_err;
43160 }
43161
43162#ifdef SQLITE_SHM_DIRECTORY
43163 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
43164#else
43165 nShmFilename = 6 + (int)strlen(zBasePath);
43166#endif
43167 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
43168 if( pShmNode==0 ){
43169 rc = SQLITE_NOMEM_BKPT7;
43170 goto shm_open_err;
43171 }
43172 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
43173 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
43174#ifdef SQLITE_SHM_DIRECTORY
43175 sqlite3_snprintf(nShmFilename, zShm,
43176 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
43177 (u32)sStat.st_ino, (u32)sStat.st_dev);
43178#else
43179 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
43180 sqlite3FileSuffix3(pDbFd->zPath, zShm);
43181#endif
43182 pShmNode->hShm = -1;
43183 pDbFd->pInode->pShmNode = pShmNode;
43184 pShmNode->pInode = pDbFd->pInode;
43185 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
43186 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
43187 if( pShmNode->pShmMutex==0 ){
43188 rc = SQLITE_NOMEM_BKPT7;
43189 goto shm_open_err;
43190 }
43191#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43192 {
43193 int ii;
43194 for(ii=0; ii<SQLITE_SHM_NLOCK8; ii++){
43195 pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
43196 if( pShmNode->aMutex[ii]==0 ){
43197 rc = SQLITE_NOMEM_BKPT7;
43198 goto shm_open_err;
43199 }
43200 }
43201 }
43202#endif
43203 }
43204
43205 if( pInode->bProcessLock==0 ){
43206 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
43207 pShmNode->hShm = robust_open(zShm, O_RDWR02|O_CREAT0100|O_NOFOLLOW0400000,
43208 (sStat.st_mode&0777));
43209 }
43210 if( pShmNode->hShm<0 ){
43211 pShmNode->hShm = robust_open(zShm, O_RDONLY00|O_NOFOLLOW0400000,
43212 (sStat.st_mode&0777));
43213 if( pShmNode->hShm<0 ){
43214 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm)unixLogErrorAtLine(sqlite3CantopenError(43214),"open",zShm,43214
)
;
43215 goto shm_open_err;
43216 }
43217 pShmNode->isReadonly = 1;
43218 }
43219
43220 /* If this process is running as root, make sure that the SHM file
43221 ** is owned by the same user that owns the original database. Otherwise,
43222 ** the original owner will not be able to connect.
43223 */
43224 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
43225
43226 rc = unixLockSharedMemory(pDbFd, pShmNode);
43227 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
43228 }
43229 }
43230
43231 /* Make the new connection a child of the unixShmNode */
43232 p->pShmNode = pShmNode;
43233#ifdef SQLITE_DEBUG
43234 p->id = pShmNode->nextShmId++;
43235#endif
43236 pShmNode->nRef++;
43237 pDbFd->pShm = p;
43238 unixLeaveMutex();
43239
43240 /* The reference count on pShmNode has already been incremented under
43241 ** the cover of the unixEnterMutex() mutex and the pointer from the
43242 ** new (struct unixShm) object to the pShmNode has been set. All that is
43243 ** left to do is to link the new object into the linked list starting
43244 ** at pShmNode->pFirst. This must be done while holding the
43245 ** pShmNode->pShmMutex.
43246 */
43247 sqlite3_mutex_enter(pShmNode->pShmMutex);
43248 p->pNext = pShmNode->pFirst;
43249 pShmNode->pFirst = p;
43250 sqlite3_mutex_leave(pShmNode->pShmMutex);
43251 return rc;
43252
43253 /* Jump here on any error */
43254shm_open_err:
43255 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
43256 sqlite3_free(p);
43257 unixLeaveMutex();
43258 return rc;
43259}
43260
43261/*
43262** This function is called to obtain a pointer to region iRegion of the
43263** shared-memory associated with the database file fd. Shared-memory regions
43264** are numbered starting from zero. Each shared-memory region is szRegion
43265** bytes in size.
43266**
43267** If an error occurs, an error code is returned and *pp is set to NULL.
43268**
43269** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
43270** region has not been allocated (by any client, including one running in a
43271** separate process), then *pp is set to NULL and SQLITE_OK returned. If
43272** bExtend is non-zero and the requested shared-memory region has not yet
43273** been allocated, it is allocated by this function.
43274**
43275** If the shared-memory region has already been allocated or is allocated by
43276** this call as described above, then it is mapped into this processes
43277** address space (if it is not already), *pp is set to point to the mapped
43278** memory and SQLITE_OK returned.
43279*/
43280static int unixShmMap(
43281 sqlite3_file *fd, /* Handle open on database file */
43282 int iRegion, /* Region to retrieve */
43283 int szRegion, /* Size of regions */
43284 int bExtend, /* True to extend file if necessary */
43285 void volatile **pp /* OUT: Mapped memory */
43286){
43287 unixFile *pDbFd = (unixFile*)fd;
43288 unixShm *p;
43289 unixShmNode *pShmNode;
43290 int rc = SQLITE_OK0;
43291 int nShmPerMap = unixShmRegionPerMap();
43292 int nReqRegion;
43293
43294 /* If the shared-memory file has not yet been opened, open it now. */
43295 if( pDbFd->pShm==0 ){
43296 rc = unixOpenSharedMemory(pDbFd);
43297 if( rc!=SQLITE_OK0 ) return rc;
43298 }
43299
43300 p = pDbFd->pShm;
43301 pShmNode = p->pShmNode;
43302 sqlite3_mutex_enter(pShmNode->pShmMutex);
43303 if( pShmNode->isUnlocked ){
43304 rc = unixLockSharedMemory(pDbFd, pShmNode);
43305 if( rc!=SQLITE_OK0 ) goto shmpage_out;
43306 pShmNode->isUnlocked = 0;
43307 }
43308 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
43309 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
43310 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
43311 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
43312
43313 /* Minimum number of regions required to be mapped. */
43314 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
43315
43316 if( pShmNode->nRegion<nReqRegion ){
43317 char **apNew; /* New apRegion[] array */
43318 int nByte = nReqRegion*szRegion; /* Minimum required file size */
43319 struct stat sStat; /* Used by fstat() */
43320
43321 pShmNode->szRegion = szRegion;
43322
43323 if( pShmNode->hShm>=0 ){
43324 /* The requested region is not mapped into this processes address space.
43325 ** Check to see if it has been allocated (i.e. if the wal-index file is
43326 ** large enough to contain the requested region).
43327 */
43328 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pShmNode->hShm, &sStat) ){
43329 rc = SQLITE_IOERR_SHMSIZE(10 | (19<<8));
43330 goto shmpage_out;
43331 }
43332
43333 if( sStat.st_size<nByte ){
43334 /* The requested memory region does not exist. If bExtend is set to
43335 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
43336 */
43337 if( !bExtend ){
43338 goto shmpage_out;
43339 }
43340
43341 /* Alternatively, if bExtend is true, extend the file. Do this by
43342 ** writing a single byte to the end of each (OS) page being
43343 ** allocated or extended. Technically, we need only write to the
43344 ** last page in order to extend the file. But writing to all new
43345 ** pages forces the OS to allocate them immediately, which reduces
43346 ** the chances of SIGBUS while accessing the mapped region later on.
43347 */
43348 else{
43349 static const int pgsz = 4096;
43350 int iPg;
43351
43352 /* Write to the last byte of each newly allocated or extended page */
43353 assert( (nByte % pgsz)==0 )((void) (0));
43354 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
43355 int x = 0;
43356 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
43357 const char *zFile = pShmNode->zFilename;
43358 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile)unixLogErrorAtLine((10 | (19<<8)),"write",zFile,43358);
43359 goto shmpage_out;
43360 }
43361 }
43362 }
43363 }
43364 }
43365
43366 /* Map the requested memory region into this processes address space. */
43367 apNew = (char **)sqlite3_realloc(
43368 pShmNode->apRegion, nReqRegion*sizeof(char *)
43369 );
43370 if( !apNew ){
43371 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
43372 goto shmpage_out;
43373 }
43374 pShmNode->apRegion = apNew;
43375 while( pShmNode->nRegion<nReqRegion ){
43376 int nMap = szRegion*nShmPerMap;
43377 int i;
43378 void *pMem;
43379 if( pShmNode->hShm>=0 ){
43380 pMem = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nMap,
43381 pShmNode->isReadonly ? PROT_READ0x1 : PROT_READ0x1|PROT_WRITE0x2,
43382 MAP_SHARED0x01, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
43383 );
43384 if( pMem==MAP_FAILED((void *) -1) ){
43385 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename)unixLogErrorAtLine((10 | (21<<8)),"mmap",pShmNode->zFilename
,43385)
;
43386 goto shmpage_out;
43387 }
43388 }else{
43389 pMem = sqlite3_malloc64(nMap);
43390 if( pMem==0 ){
43391 rc = SQLITE_NOMEM_BKPT7;
43392 goto shmpage_out;
43393 }
43394 memset(pMem, 0, nMap);
43395 }
43396
43397 for(i=0; i<nShmPerMap; i++){
43398 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
43399 }
43400 pShmNode->nRegion += nShmPerMap;
43401 }
43402 }
43403
43404shmpage_out:
43405 if( pShmNode->nRegion>iRegion ){
43406 *pp = pShmNode->apRegion[iRegion];
43407 }else{
43408 *pp = 0;
43409 }
43410 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
43411 sqlite3_mutex_leave(pShmNode->pShmMutex);
43412 return rc;
43413}
43414
43415/*
43416** Check that the pShmNode->aLock[] array comports with the locking bitmasks
43417** held by each client. Return true if it does, or false otherwise. This
43418** is to be used in an assert(). e.g.
43419**
43420** assert( assertLockingArrayOk(pShmNode) );
43421*/
43422#ifdef SQLITE_DEBUG
43423static int assertLockingArrayOk(unixShmNode *pShmNode){
43424#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43425 return 1;
43426#else
43427 unixShm *pX;
43428 int aLock[SQLITE_SHM_NLOCK8];
43429
43430 memset(aLock, 0, sizeof(aLock));
43431 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
43432 int i;
43433 for(i=0; i<SQLITE_SHM_NLOCK8; i++){
43434 if( pX->exclMask & (1<<i) ){
43435 assert( aLock[i]==0 )((void) (0));
43436 aLock[i] = -1;
43437 }else if( pX->sharedMask & (1<<i) ){
43438 assert( aLock[i]>=0 )((void) (0));
43439 aLock[i]++;
43440 }
43441 }
43442 }
43443
43444 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) )((void) (0));
43445 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
43446#endif
43447}
43448#endif
43449
43450/*
43451** Change the lock state for a shared-memory segment.
43452**
43453** Note that the relationship between SHARED and EXCLUSIVE locks is a little
43454** different here than in posix. In xShmLock(), one can go from unlocked
43455** to shared and back or from unlocked to exclusive and back. But one may
43456** not go from shared to exclusive or from exclusive to shared.
43457*/
43458static int unixShmLock(
43459 sqlite3_file *fd, /* Database file holding the shared memory */
43460 int ofst, /* First lock to acquire or release */
43461 int n, /* Number of locks to acquire or release */
43462 int flags /* What to do with the lock */
43463){
43464 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
43465 unixShm *p; /* The shared memory being locked */
43466 unixShmNode *pShmNode; /* The underlying file iNode */
43467 int rc = SQLITE_OK0; /* Result code */
43468 u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */
43469 int *aLock;
43470
43471 p = pDbFd->pShm;
43472 if( p==0 ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
43473 pShmNode = p->pShmNode;
43474 if( NEVER(pShmNode==0)(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
43475 aLock = pShmNode->aLock;
43476
43477 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
43478 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
43479 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
43480 assert( n>=1 )((void) (0));
43481 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
43482 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
43483 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
43484 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
43485 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
43486 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
43487 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
43488
43489 /* Check that, if this to be a blocking lock, no locks that occur later
43490 ** in the following list than the lock being obtained are already held:
43491 **
43492 ** 1. Checkpointer lock (ofst==1).
43493 ** 2. Write lock (ofst==0).
43494 ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
43495 **
43496 ** In other words, if this is a blocking lock, none of the locks that
43497 ** occur later in the above list than the lock being obtained may be
43498 ** held.
43499 **
43500 ** It is not permitted to block on the RECOVER lock.
43501 */
43502#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43503 {
43504 u16 lockMask = (p->exclMask|p->sharedMask);
43505 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (((void) (0))
43506 (ofst!=2) /* not RECOVER */((void) (0))
43507 && (ofst!=1 || lockMask==0 || lockMask==2)((void) (0))
43508 && (ofst!=0 || lockMask<3)((void) (0))
43509 && (ofst<3 || lockMask<(1<<ofst))((void) (0))
43510 ))((void) (0));
43511 }
43512#endif
43513
43514 /* Check if there is any work to do. There are three cases:
43515 **
43516 ** a) An unlock operation where there are locks to unlock,
43517 ** b) An shared lock where the requested lock is not already held
43518 ** c) An exclusive lock where the requested lock is not already held
43519 **
43520 ** The SQLite core never requests an exclusive lock that it already holds.
43521 ** This is assert()ed below.
43522 */
43523 assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)((void) (0))
43524 || 0==(p->exclMask & mask)((void) (0))
43525 )((void) (0));
43526 if( ((flags & SQLITE_SHM_UNLOCK1) && ((p->exclMask|p->sharedMask) & mask))
43527 || (flags==(SQLITE_SHM_SHARED4|SQLITE_SHM_LOCK2) && 0==(p->sharedMask & mask))
43528 || (flags==(SQLITE_SHM_EXCLUSIVE8|SQLITE_SHM_LOCK2))
43529 ){
43530
43531 /* Take the required mutexes. In SETLK_TIMEOUT mode (blocking locks), if
43532 ** this is an attempt on an exclusive lock use sqlite3_mutex_try(). If any
43533 ** other thread is holding this mutex, then it is either holding or about
43534 ** to hold a lock exclusive to the one being requested, and we may
43535 ** therefore return SQLITE_BUSY to the caller.
43536 **
43537 ** Doing this prevents some deadlock scenarios. For example, thread 1 may
43538 ** be a checkpointer blocked waiting on the WRITER lock. And thread 2
43539 ** may be a normal SQL client upgrading to a write transaction. In this
43540 ** case thread 2 does a non-blocking request for the WRITER lock. But -
43541 ** if it were to use sqlite3_mutex_enter() then it would effectively
43542 ** become a (doomed) blocking request, as thread 2 would block until thread
43543 ** 1 obtained WRITER and released the mutex. Since thread 2 already holds
43544 ** a lock on a read-locking slot at this point, this breaks the
43545 ** anti-deadlock rules (see above). */
43546#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43547 int iMutex;
43548 for(iMutex=ofst; iMutex<ofst+n; iMutex++){
43549 if( flags==(SQLITE_SHM_LOCK2|SQLITE_SHM_EXCLUSIVE8) ){
43550 rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]);
43551 if( rc!=SQLITE_OK0 ) goto leave_shmnode_mutexes;
43552 }else{
43553 sqlite3_mutex_enter(pShmNode->aMutex[iMutex]);
43554 }
43555 }
43556#else
43557 sqlite3_mutex_enter(pShmNode->pShmMutex);
43558#endif
43559
43560 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
43561 if( flags & SQLITE_SHM_UNLOCK1 ){
43562 /* Case (a) - unlock. */
43563 int bUnlock = 1;
43564 assert( (p->exclMask & p->sharedMask)==0 )((void) (0));
43565 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask )((void) (0));
43566 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask )((void) (0));
43567
43568 /* If this is a SHARED lock being unlocked, it is possible that other
43569 ** clients within this process are holding the same SHARED lock. In
43570 ** this case, set bUnlock to 0 so that the posix lock is not removed
43571 ** from the file-descriptor below. */
43572 if( flags & SQLITE_SHM_SHARED4 ){
43573 assert( n==1 )((void) (0));
43574 assert( aLock[ofst]>=1 )((void) (0));
43575 if( aLock[ofst]>1 ){
43576 bUnlock = 0;
43577 aLock[ofst]--;
43578 p->sharedMask &= ~mask;
43579 }
43580 }
43581
43582 if( bUnlock ){
43583 rc = unixShmSystemLock(pDbFd, F_UNLCK2, ofst+UNIX_SHM_BASE((22+8)*4), n);
43584 if( rc==SQLITE_OK0 ){
43585 memset(&aLock[ofst], 0, sizeof(int)*n);
43586 p->sharedMask &= ~mask;
43587 p->exclMask &= ~mask;
43588 }
43589 }
43590 }else if( flags & SQLITE_SHM_SHARED4 ){
43591 /* Case (b) - a shared lock. */
43592
43593 if( aLock[ofst]<0 ){
43594 /* An exclusive lock is held by some other connection. BUSY. */
43595 rc = SQLITE_BUSY5;
43596 }else if( aLock[ofst]==0 ){
43597 rc = unixShmSystemLock(pDbFd, F_RDLCK0, ofst+UNIX_SHM_BASE((22+8)*4), n);
43598 }
43599
43600 /* Get the local shared locks */
43601 if( rc==SQLITE_OK0 ){
43602 p->sharedMask |= mask;
43603 aLock[ofst]++;
43604 }
43605 }else{
43606 /* Case (c) - an exclusive lock. */
43607 int ii;
43608
43609 assert( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) )((void) (0));
43610 assert( (p->sharedMask & mask)==0 )((void) (0));
43611 assert( (p->exclMask & mask)==0 )((void) (0));
43612
43613 /* Make sure no sibling connections hold locks that will block this
43614 ** lock. If any do, return SQLITE_BUSY right away. */
43615 for(ii=ofst; ii<ofst+n; ii++){
43616 if( aLock[ii] ){
43617 rc = SQLITE_BUSY5;
43618 break;
43619 }
43620 }
43621
43622 /* Get the exclusive locks at the system level. Then if successful
43623 ** also update the in-memory values. */
43624 if( rc==SQLITE_OK0 ){
43625 rc = unixShmSystemLock(pDbFd, F_WRLCK1, ofst+UNIX_SHM_BASE((22+8)*4), n);
43626 if( rc==SQLITE_OK0 ){
43627 p->exclMask |= mask;
43628 for(ii=ofst; ii<ofst+n; ii++){
43629 aLock[ii] = -1;
43630 }
43631 }
43632 }
43633 }
43634 assert( assertLockingArrayOk(pShmNode) )((void) (0));
43635 }
43636
43637 /* Drop the mutexes acquired above. */
43638#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43639 leave_shmnode_mutexes:
43640 for(iMutex--; iMutex>=ofst; iMutex--){
43641 sqlite3_mutex_leave(pShmNode->aMutex[iMutex]);
43642 }
43643#else
43644 sqlite3_mutex_leave(pShmNode->pShmMutex);
43645#endif
43646 }
43647
43648 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
43649 p->id, osGetpid(0), p->sharedMask, p->exclMask));
43650 return rc;
43651}
43652
43653/*
43654** Implement a memory barrier or memory fence on shared memory.
43655**
43656** All loads and stores begun before the barrier must complete before
43657** any load or store begun after the barrier.
43658*/
43659static void unixShmBarrier(
43660 sqlite3_file *fd /* Database file holding the shared memory */
43661){
43662 UNUSED_PARAMETER(fd)(void)(fd);
43663 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
43664 assert( fd->pMethods->xLock==nolockLock((void) (0))
43665 || unixFileMutexNotheld((unixFile*)fd)((void) (0))
43666 )((void) (0));
43667 unixEnterMutex(); /* Also mutex, for redundancy */
43668 unixLeaveMutex();
43669}
43670
43671/*
43672** Close a connection to shared-memory. Delete the underlying
43673** storage if deleteFlag is true.
43674**
43675** If there is no shared memory associated with the connection then this
43676** routine is a harmless no-op.
43677*/
43678static int unixShmUnmap(
43679 sqlite3_file *fd, /* The underlying database file */
43680 int deleteFlag /* Delete shared-memory if true */
43681){
43682 unixShm *p; /* The connection to be closed */
43683 unixShmNode *pShmNode; /* The underlying shared-memory file */
43684 unixShm **pp; /* For looping over sibling connections */
43685 unixFile *pDbFd; /* The underlying database file */
43686
43687 pDbFd = (unixFile*)fd;
43688 p = pDbFd->pShm;
43689 if( p==0 ) return SQLITE_OK0;
43690 pShmNode = p->pShmNode;
43691
43692 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
43693 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
43694
43695 /* Remove connection p from the set of connections associated
43696 ** with pShmNode */
43697 sqlite3_mutex_enter(pShmNode->pShmMutex);
43698 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
43699 *pp = p->pNext;
43700
43701 /* Free the connection p */
43702 sqlite3_free(p);
43703 pDbFd->pShm = 0;
43704 sqlite3_mutex_leave(pShmNode->pShmMutex);
43705
43706 /* If pShmNode->nRef has reached 0, then close the underlying
43707 ** shared-memory file, too */
43708 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
43709 unixEnterMutex();
43710 assert( pShmNode->nRef>0 )((void) (0));
43711 pShmNode->nRef--;
43712 if( pShmNode->nRef==0 ){
43713 if( deleteFlag && pShmNode->hShm>=0 ){
43714 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pShmNode->zFilename);
43715 }
43716 unixShmPurge(pDbFd);
43717 }
43718 unixLeaveMutex();
43719
43720 return SQLITE_OK0;
43721}
43722
43723
43724#else
43725# define unixShmMap 0
43726# define unixShmLock 0
43727# define unixShmBarrier 0
43728# define unixShmUnmap 0
43729#endif /* #ifndef SQLITE_OMIT_WAL */
43730
43731#if SQLITE_MAX_MMAP_SIZE20971520>0
43732/*
43733** If it is currently memory mapped, unmap file pFd.
43734*/
43735static void unixUnmapfile(unixFile *pFd){
43736 assert( pFd->nFetchOut==0 )((void) (0));
43737 if( pFd->pMapRegion ){
43738 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pFd->pMapRegion, pFd->mmapSizeActual);
43739 pFd->pMapRegion = 0;
43740 pFd->mmapSize = 0;
43741 pFd->mmapSizeActual = 0;
43742 }
43743}
43744
43745/*
43746** Attempt to set the size of the memory mapping maintained by file
43747** descriptor pFd to nNew bytes. Any existing mapping is discarded.
43748**
43749** If successful, this function sets the following variables:
43750**
43751** unixFile.pMapRegion
43752** unixFile.mmapSize
43753** unixFile.mmapSizeActual
43754**
43755** If unsuccessful, an error message is logged via sqlite3_log() and
43756** the three variables above are zeroed. In this case SQLite should
43757** continue accessing the database using the xRead() and xWrite()
43758** methods.
43759*/
43760static void unixRemapfile(
43761 unixFile *pFd, /* File descriptor object */
43762 i64 nNew /* Required mapping size */
43763){
43764 const char *zErr = "mmap";
43765 int h = pFd->h; /* File descriptor open on db file */
43766 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
43767 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
43768 u8 *pNew = 0; /* Location of new mapping */
43769 int flags = PROT_READ0x1; /* Flags to pass to mmap() */
43770
43771 assert( pFd->nFetchOut==0 )((void) (0));
43772 assert( nNew>pFd->mmapSize )((void) (0));
43773 assert( nNew<=pFd->mmapSizeMax )((void) (0));
43774 assert( nNew>0 )((void) (0));
43775 assert( pFd->mmapSizeActual>=pFd->mmapSize )((void) (0));
43776 assert( MAP_FAILED!=0 )((void) (0));
43777
43778#ifdef SQLITE_MMAP_READWRITE
43779 if( (pFd->ctrlFlags & UNIXFILE_RDONLY0x02)==0 ) flags |= PROT_WRITE0x2;
43780#endif
43781
43782 if( pOrig ){
43783#if HAVE_MREMAP1
43784 i64 nReuse = pFd->mmapSize;
43785#else
43786 const int szSyspage = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)();
43787 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
43788#endif
43789 u8 *pReq = &pOrig[nReuse];
43790
43791 /* Unmap any pages of the existing mapping that cannot be reused. */
43792 if( nReuse!=nOrig ){
43793 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pReq, nOrig-nReuse);
43794 }
43795
43796#if HAVE_MREMAP1
43797 pNew = osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
(pOrig, nReuse, nNew, MREMAP_MAYMOVE1);
43798 zErr = "mremap";
43799#else
43800 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(pReq, nNew-nReuse, flags, MAP_SHARED0x01, h, nReuse);
43801 if( pNew!=MAP_FAILED((void *) -1) ){
43802 if( pNew!=pReq ){
43803 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pNew, nNew - nReuse);
43804 pNew = 0;
43805 }else{
43806 pNew = pOrig;
43807 }
43808 }
43809#endif
43810
43811 /* The attempt to extend the existing mapping failed. Free it. */
43812 if( pNew==MAP_FAILED((void *) -1) || pNew==0 ){
43813 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pOrig, nReuse);
43814 }
43815 }
43816
43817 /* If pNew is still NULL, try to create an entirely new mapping. */
43818 if( pNew==0 ){
43819 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nNew, flags, MAP_SHARED0x01, h, 0);
43820 }
43821
43822 if( pNew==MAP_FAILED((void *) -1) ){
43823 pNew = 0;
43824 nNew = 0;
43825 unixLogError(SQLITE_OK, zErr, pFd->zPath)unixLogErrorAtLine(0,zErr,pFd->zPath,43825);
43826
43827 /* If the mmap() above failed, assume that all subsequent mmap() calls
43828 ** will probably fail too. Fall back to using xRead/xWrite exclusively
43829 ** in this case. */
43830 pFd->mmapSizeMax = 0;
43831 }
43832 pFd->pMapRegion = (void *)pNew;
43833 pFd->mmapSize = pFd->mmapSizeActual = nNew;
43834}
43835
43836/*
43837** Memory map or remap the file opened by file-descriptor pFd (if the file
43838** is already mapped, the existing mapping is replaced by the new). Or, if
43839** there already exists a mapping for this file, and there are still
43840** outstanding xFetch() references to it, this function is a no-op.
43841**
43842** If parameter nByte is non-negative, then it is the requested size of
43843** the mapping to create. Otherwise, if nByte is less than zero, then the
43844** requested size is the size of the file on disk. The actual size of the
43845** created mapping is either the requested size or the value configured
43846** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
43847**
43848** SQLITE_OK is returned if no error occurs (even if the mapping is not
43849** recreated as a result of outstanding references) or an SQLite error
43850** code otherwise.
43851*/
43852static int unixMapfile(unixFile *pFd, i64 nMap){
43853 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
43854 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
43855 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
43856
43857 if( nMap<0 ){
43858 struct stat statbuf; /* Low-level file information */
43859 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFd->h, &statbuf) ){
43860 return SQLITE_IOERR_FSTAT(10 | (7<<8));
43861 }
43862 nMap = statbuf.st_size;
43863 }
43864 if( nMap>pFd->mmapSizeMax ){
43865 nMap = pFd->mmapSizeMax;
43866 }
43867
43868 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
43869 if( nMap!=pFd->mmapSize ){
43870 unixRemapfile(pFd, nMap);
43871 }
43872
43873 return SQLITE_OK0;
43874}
43875#endif /* SQLITE_MAX_MMAP_SIZE>0 */
43876
43877/*
43878** If possible, return a pointer to a mapping of file fd starting at offset
43879** iOff. The mapping must be valid for at least nAmt bytes.
43880**
43881** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
43882** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
43883** Finally, if an error does occur, return an SQLite error code. The final
43884** value of *pp is undefined in this case.
43885**
43886** If this function does return a pointer, the caller must eventually
43887** release the reference by calling unixUnfetch().
43888*/
43889static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
43890#if SQLITE_MAX_MMAP_SIZE20971520>0
43891 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
43892#endif
43893 *pp = 0;
43894
43895#if SQLITE_MAX_MMAP_SIZE20971520>0
43896 if( pFd->mmapSizeMax>0 ){
43897 /* Ensure that there is always at least a 256 byte buffer of addressable
43898 ** memory following the returned page. If the database is corrupt,
43899 ** SQLite may overread the page slightly (in practice only a few bytes,
43900 ** but 256 is safe, round, number). */
43901 const int nEofBuffer = 256;
43902 if( pFd->pMapRegion==0 ){
43903 int rc = unixMapfile(pFd, -1);
43904 if( rc!=SQLITE_OK0 ) return rc;
43905 }
43906 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
43907 *pp = &((u8 *)pFd->pMapRegion)[iOff];
43908 pFd->nFetchOut++;
43909 }
43910 }
43911#endif
43912 return SQLITE_OK0;
43913}
43914
43915/*
43916** If the third argument is non-NULL, then this function releases a
43917** reference obtained by an earlier call to unixFetch(). The second
43918** argument passed to this function must be the same as the corresponding
43919** argument that was passed to the unixFetch() invocation.
43920**
43921** Or, if the third argument is NULL, then this function is being called
43922** to inform the VFS layer that, according to POSIX, any existing mapping
43923** may now be invalid and should be unmapped.
43924*/
43925static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
43926#if SQLITE_MAX_MMAP_SIZE20971520>0
43927 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
43928 UNUSED_PARAMETER(iOff)(void)(iOff);
43929
43930 /* If p==0 (unmap the entire file) then there must be no outstanding
43931 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
43932 ** then there must be at least one outstanding. */
43933 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
43934
43935 /* If p!=0, it must match the iOff value. */
43936 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
43937
43938 if( p ){
43939 pFd->nFetchOut--;
43940 }else{
43941 unixUnmapfile(pFd);
43942 }
43943
43944 assert( pFd->nFetchOut>=0 )((void) (0));
43945#else
43946 UNUSED_PARAMETER(fd)(void)(fd);
43947 UNUSED_PARAMETER(p)(void)(p);
43948 UNUSED_PARAMETER(iOff)(void)(iOff);
43949#endif
43950 return SQLITE_OK0;
43951}
43952
43953/*
43954** Here ends the implementation of all sqlite3_file methods.
43955**
43956********************** End sqlite3_file Methods *******************************
43957******************************************************************************/
43958
43959/*
43960** This division contains definitions of sqlite3_io_methods objects that
43961** implement various file locking strategies. It also contains definitions
43962** of "finder" functions. A finder-function is used to locate the appropriate
43963** sqlite3_io_methods object for a particular database file. The pAppData
43964** field of the sqlite3_vfs VFS objects are initialized to be pointers to
43965** the correct finder-function for that VFS.
43966**
43967** Most finder functions return a pointer to a fixed sqlite3_io_methods
43968** object. The only interesting finder-function is autolockIoFinder, which
43969** looks at the filesystem type and tries to guess the best locking
43970** strategy from that.
43971**
43972** For finder-function F, two objects are created:
43973**
43974** (1) The real finder-function named "FImpt()".
43975**
43976** (2) A constant pointer to this function named just "F".
43977**
43978**
43979** A pointer to the F pointer is used as the pAppData value for VFS
43980** objects. We have to do this instead of letting pAppData point
43981** directly at the finder-function since C90 rules prevent a void*
43982** from be cast into a function pointer.
43983**
43984**
43985** Each instance of this macro generates two objects:
43986**
43987** * A constant sqlite3_io_methods object call METHOD that has locking
43988** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
43989**
43990** * An I/O method finder function called FINDER that returns a pointer
43991** to the METHOD object in the previous bullet.
43992*/
43993#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;
\
43994static const sqlite3_io_methods METHOD = { \
43995 VERSION, /* iVersion */ \
43996 CLOSE, /* xClose */ \
43997 unixRead, /* xRead */ \
43998 unixWrite, /* xWrite */ \
43999 unixTruncate, /* xTruncate */ \
44000 unixSync, /* xSync */ \
44001 unixFileSize, /* xFileSize */ \
44002 LOCK, /* xLock */ \
44003 UNLOCK, /* xUnlock */ \
44004 CKLOCK, /* xCheckReservedLock */ \
44005 unixFileControl, /* xFileControl */ \
44006 unixSectorSize, /* xSectorSize */ \
44007 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
44008 SHMMAP, /* xShmMap */ \
44009 unixShmLock, /* xShmLock */ \
44010 unixShmBarrier, /* xShmBarrier */ \
44011 unixShmUnmap, /* xShmUnmap */ \
44012 unixFetch, /* xFetch */ \
44013 unixUnfetch, /* xUnfetch */ \
44014}; \
44015static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
44016 UNUSED_PARAMETER(z)(void)(z); UNUSED_PARAMETER(p)(void)(p); \
44017 return &METHOD; \
44018} \
44019static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
44020 = FINDER##Impl;
44021
44022/*
44023** Here are all of the sqlite3_io_methods objects for each of the
44024** locking strategies. Functions that return pointers to these methods
44025** are also created.
44026*/
44027IOMETHODS(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
;
44028 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
;
44029 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
;
44030 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
;
44031 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
;
44032 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
;
44033 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
;
44034 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
;
44035 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
;
44036)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
;
44037IOMETHODS(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
;
44038 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
;
44039 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
;
44040 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
;
44041 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
;
44042 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
;
44043 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
;
44044 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
;
44045 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
;
44046)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
;
44047IOMETHODS(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
;
44048 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
;
44049 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
;
44050 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
;
44051 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
;
44052 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
;
44053 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
;
44054 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
;
44055 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
;
44056)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
;
44057
44058#if SQLITE_ENABLE_LOCKING_STYLE0
44059IOMETHODS(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
;
44060 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
;
44061 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
;
44062 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
;
44063 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
;
44064 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
;
44065 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
;
44066 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
;
44067 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
;
44068)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
;
44069#endif
44070
44071#if OS_VXWORKS0
44072IOMETHODS(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
;
44073 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
;
44074 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
;
44075 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
;
44076 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
;
44077 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
;
44078 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
;
44079 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
;
44080 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
;
44081)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
;
44082#endif
44083
44084#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44085IOMETHODS(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
;
44086 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
;
44087 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
;
44088 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
;
44089 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
;
44090 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
;
44091 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
;
44092 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
;
44093 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
;
44094)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
;
44095#endif
44096
44097/*
44098** The proxy locking method is a "super-method" in the sense that it
44099** opens secondary file descriptors for the conch and lock files and
44100** it uses proxy, dot-file, AFP, and flock() locking methods on those
44101** secondary files. For this reason, the division that implements
44102** proxy locking is located much further down in the file. But we need
44103** to go ahead and define the sqlite3_io_methods and finder function
44104** for proxy locking here. So we forward declare the I/O methods.
44105*/
44106#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44107static int proxyClose(sqlite3_file*);
44108static int proxyLock(sqlite3_file*, int);
44109static int proxyUnlock(sqlite3_file*, int);
44110static int proxyCheckReservedLock(sqlite3_file*, int*);
44111IOMETHODS(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
;
44112 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
;
44113 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
;
44114 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
;
44115 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
;
44116 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
;
44117 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
;
44118 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
;
44119 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
;
44120)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
;
44121#endif
44122
44123/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
44124#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44125IOMETHODS(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
;
44126 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
;
44127 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
;
44128 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
;
44129 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
;
44130 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
;
44131 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
;
44132 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
;
44133 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
;
44134)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
;
44135#endif
44136
44137#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44138/*
44139** This "finder" function attempts to determine the best locking strategy
44140** for the database file "filePath". It then returns the sqlite3_io_methods
44141** object that implements that strategy.
44142**
44143** This is for MacOSX only.
44144*/
44145static const sqlite3_io_methods *autolockIoFinderImpl(
44146 const char *filePath, /* name of the database file */
44147 unixFile *pNew /* open file object for the database file */
44148){
44149 static const struct Mapping {
44150 const char *zFilesystem; /* Filesystem type name */
44151 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
44152 } aMap[] = {
44153 { "hfs", &posixIoMethods },
44154 { "ufs", &posixIoMethods },
44155 { "afpfs", &afpIoMethods },
44156 { "smbfs", &afpIoMethods },
44157 { "webdav", &nolockIoMethods },
44158 { 0, 0 }
44159 };
44160 int i;
44161 struct statfs fsInfo;
44162 struct flock lockInfo;
44163
44164 if( !filePath ){
44165 /* If filePath==NULL that means we are dealing with a transient file
44166 ** that does not need to be locked. */
44167 return &nolockIoMethods;
44168 }
44169 if( statfs(filePath, &fsInfo) != -1 ){
44170 if( fsInfo.f_flags & MNT_RDONLY ){
44171 return &nolockIoMethods;
44172 }
44173 for(i=0; aMap[i].zFilesystem; i++){
44174 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
44175 return aMap[i].pMethods;
44176 }
44177 }
44178 }
44179
44180 /* Default case. Handles, amongst others, "nfs".
44181 ** Test byte-range lock using fcntl(). If the call succeeds,
44182 ** assume that the file-system supports POSIX style locks.
44183 */
44184 lockInfo.l_len = 1;
44185 lockInfo.l_start = 0;
44186 lockInfo.l_whence = SEEK_SET0;
44187 lockInfo.l_type = F_RDLCK0;
44188 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
44189 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
44190 return &nfsIoMethods;
44191 } else {
44192 return &posixIoMethods;
44193 }
44194 }else{
44195 return &dotlockIoMethods;
44196 }
44197}
44198static const sqlite3_io_methods
44199 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
44200
44201#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
44202
44203#if OS_VXWORKS0
44204/*
44205** This "finder" function for VxWorks checks to see if posix advisory
44206** locking works. If it does, then that is what is used. If it does not
44207** work, then fallback to named semaphore locking.
44208*/
44209static const sqlite3_io_methods *vxworksIoFinderImpl(
44210 const char *filePath, /* name of the database file */
44211 unixFile *pNew /* the open file object */
44212){
44213 struct flock lockInfo;
44214
44215 if( !filePath ){
44216 /* If filePath==NULL that means we are dealing with a transient file
44217 ** that does not need to be locked. */
44218 return &nolockIoMethods;
44219 }
44220
44221 /* Test if fcntl() is supported and use POSIX style locks.
44222 ** Otherwise fall back to the named semaphore method.
44223 */
44224 lockInfo.l_len = 1;
44225 lockInfo.l_start = 0;
44226 lockInfo.l_whence = SEEK_SET0;
44227 lockInfo.l_type = F_RDLCK0;
44228 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
44229 return &posixIoMethods;
44230 }else{
44231 return &semIoMethods;
44232 }
44233}
44234static const sqlite3_io_methods
44235 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
44236
44237#endif /* OS_VXWORKS */
44238
44239/*
44240** An abstract type for a pointer to an IO method finder function:
44241*/
44242typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
44243
44244
44245/****************************************************************************
44246**************************** sqlite3_vfs methods ****************************
44247**
44248** This division contains the implementation of methods on the
44249** sqlite3_vfs object.
44250*/
44251
44252/*
44253** Initialize the contents of the unixFile structure pointed to by pId.
44254*/
44255static int fillInUnixFile(
44256 sqlite3_vfs *pVfs, /* Pointer to vfs object */
44257 int h, /* Open file descriptor of file being opened */
44258 sqlite3_file *pId, /* Write to the unixFile structure here */
44259 const char *zFilename, /* Name of the file being opened */
44260 int ctrlFlags /* Zero or more UNIXFILE_* values */
44261){
44262 const sqlite3_io_methods *pLockingStyle;
44263 unixFile *pNew = (unixFile *)pId;
44264 int rc = SQLITE_OK0;
44265
44266 assert( pNew->pInode==NULL )((void) (0));
44267
44268 /* No locking occurs in temporary files */
44269 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 )((void) (0));
44270
44271 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
44272 pNew->h = h;
44273 pNew->pVfs = pVfs;
44274 pNew->zPath = zFilename;
44275 pNew->ctrlFlags = (u8)ctrlFlags;
44276#if SQLITE_MAX_MMAP_SIZE20971520>0
44277 pNew->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
44278#endif
44279 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI0x40) ? zFilename : 0),
44280 "psow", SQLITE_POWERSAFE_OVERWRITE1) ){
44281 pNew->ctrlFlags |= UNIXFILE_PSOW0x10;
44282 }
44283 if( strcmp(pVfs->zName,"unix-excl")==0 ){
44284 pNew->ctrlFlags |= UNIXFILE_EXCL0x01;
44285 }
44286
44287#if OS_VXWORKS0
44288 pNew->pId = vxworksFindFileId(zFilename);
44289 if( pNew->pId==0 ){
44290 ctrlFlags |= UNIXFILE_NOLOCK0x80;
44291 rc = SQLITE_NOMEM_BKPT7;
44292 }
44293#endif
44294
44295 if( ctrlFlags & UNIXFILE_NOLOCK0x80 ){
44296 pLockingStyle = &nolockIoMethods;
44297 }else{
44298 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
44299#if SQLITE_ENABLE_LOCKING_STYLE0
44300 /* Cache zFilename in the locking context (AFP and dotlock override) for
44301 ** proxyLock activation is possible (remote proxy is based on db name)
44302 ** zFilename remains valid until file is closed, to support */
44303 pNew->lockingContext = (void*)zFilename;
44304#endif
44305 }
44306
44307 if( pLockingStyle == &posixIoMethods
44308#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
44309 || pLockingStyle == &nfsIoMethods
44310#endif
44311 ){
44312 unixEnterMutex();
44313 rc = findInodeInfo(pNew, &pNew->pInode);
44314 if( rc!=SQLITE_OK0 ){
44315 /* If an error occurred in findInodeInfo(), close the file descriptor
44316 ** immediately, before releasing the mutex. findInodeInfo() may fail
44317 ** in two scenarios:
44318 **
44319 ** (a) A call to fstat() failed.
44320 ** (b) A malloc failed.
44321 **
44322 ** Scenario (b) may only occur if the process is holding no other
44323 ** file descriptors open on the same file. If there were other file
44324 ** descriptors on this file, then no malloc would be required by
44325 ** findInodeInfo(). If this is the case, it is quite safe to close
44326 ** handle h - as it is guaranteed that no posix locks will be released
44327 ** by doing so.
44328 **
44329 ** If scenario (a) caused the error then things are not so safe. The
44330 ** implicit assumption here is that if fstat() fails, things are in
44331 ** such bad shape that dropping a lock or two doesn't matter much.
44332 */
44333 robust_close(pNew, h, __LINE__44333);
44334 h = -1;
44335 }
44336 unixLeaveMutex();
44337 }
44338
44339#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
44340 else if( pLockingStyle == &afpIoMethods ){
44341 /* AFP locking uses the file path so it needs to be included in
44342 ** the afpLockingContext.
44343 */
44344 afpLockingContext *pCtx;
44345 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
44346 if( pCtx==0 ){
44347 rc = SQLITE_NOMEM_BKPT7;
44348 }else{
44349 /* NB: zFilename exists and remains valid until the file is closed
44350 ** according to requirement F11141. So we do not need to make a
44351 ** copy of the filename. */
44352 pCtx->dbPath = zFilename;
44353 pCtx->reserved = 0;
44354 srandomdev();
44355 unixEnterMutex();
44356 rc = findInodeInfo(pNew, &pNew->pInode);
44357 if( rc!=SQLITE_OK0 ){
44358 sqlite3_free(pNew->lockingContext);
44359 robust_close(pNew, h, __LINE__44359);
44360 h = -1;
44361 }
44362 unixLeaveMutex();
44363 }
44364 }
44365#endif
44366
44367 else if( pLockingStyle == &dotlockIoMethods ){
44368 /* Dotfile locking uses the file path so it needs to be included in
44369 ** the dotlockLockingContext
44370 */
44371 char *zLockFile;
44372 int nFilename;
44373 assert( zFilename!=0 )((void) (0));
44374 nFilename = (int)strlen(zFilename) + 6;
44375 zLockFile = (char *)sqlite3_malloc64(nFilename);
44376 if( zLockFile==0 ){
44377 rc = SQLITE_NOMEM_BKPT7;
44378 }else{
44379 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX".lock", zFilename);
44380 }
44381 pNew->lockingContext = zLockFile;
44382 }
44383
44384#if OS_VXWORKS0
44385 else if( pLockingStyle == &semIoMethods ){
44386 /* Named semaphore locking uses the file path so it needs to be
44387 ** included in the semLockingContext
44388 */
44389 unixEnterMutex();
44390 rc = findInodeInfo(pNew, &pNew->pInode);
44391 if( (rc==SQLITE_OK0) && (pNew->pInode->pSem==NULL((void*)0)) ){
44392 char *zSemName = pNew->pInode->aSemName;
44393 int n;
44394 sqlite3_snprintf(MAX_PATHNAME512, zSemName, "/%s.sem",
44395 pNew->pId->zCanonicalName);
44396 for( n=1; zSemName[n]; n++ )
44397 if( zSemName[n]=='/' ) zSemName[n] = '_';
44398 pNew->pInode->pSem = sem_open(zSemName, O_CREAT0100, 0666, 1);
44399 if( pNew->pInode->pSem == SEM_FAILED ){
44400 rc = SQLITE_NOMEM_BKPT7;
44401 pNew->pInode->aSemName[0] = '\0';
44402 }
44403 }
44404 unixLeaveMutex();
44405 }
44406#endif
44407
44408 storeLastErrno(pNew, 0);
44409#if OS_VXWORKS0
44410 if( rc!=SQLITE_OK0 ){
44411 if( h>=0 ) robust_close(pNew, h, __LINE__44411);
44412 h = -1;
44413 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zFilename);
44414 pNew->ctrlFlags |= UNIXFILE_DELETE0x20;
44415 }
44416#endif
44417 if( rc!=SQLITE_OK0 ){
44418 if( h>=0 ) robust_close(pNew, h, __LINE__44418);
44419 }else{
44420 pId->pMethods = pLockingStyle;
44421 OpenCounter(+1);
44422 verifyDbFile(pNew);
44423 }
44424 return rc;
44425}
44426
44427/*
44428** Directories to consider for temp files.
44429*/
44430static const char *azTempDirs[] = {
44431 0,
44432 0,
44433 "/var/tmp",
44434 "/usr/tmp",
44435 "/tmp",
44436 "."
44437};
44438
44439/*
44440** Initialize first two members of azTempDirs[] array.
44441*/
44442static void unixTempFileInit(void){
44443 azTempDirs[0] = getenv("SQLITE_TMPDIR");
44444 azTempDirs[1] = getenv("TMPDIR");
44445}
44446
44447/*
44448** Return the name of a directory in which to put temporary files.
44449** If no suitable temporary file directory can be found, return NULL.
44450*/
44451static const char *unixTempFileDir(void){
44452 unsigned int i = 0;
44453 struct stat buf;
44454 const char *zDir = sqlite3_temp_directory;
44455
44456 while(1){
44457 if( zDir!=0
44458 && osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zDir, &buf)==0
44459 && S_ISDIR(buf.st_mode)((((buf.st_mode)) & 0170000) == (0040000))
44460 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zDir, 03)==0
44461 ){
44462 return zDir;
44463 }
44464 if( i>=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break;
44465 zDir = azTempDirs[i++];
44466 }
44467 return 0;
44468}
44469
44470/*
44471** Create a temporary file name in zBuf. zBuf must be allocated
44472** by the calling process and must be big enough to hold at least
44473** pVfs->mxPathname bytes.
44474*/
44475static int unixGetTempname(int nBuf, char *zBuf){
44476 const char *zDir;
44477 int iLimit = 0;
44478 int rc = SQLITE_OK0;
44479
44480 /* It's odd to simulate an io-error here, but really this is just
44481 ** using the io-error infrastructure to test that SQLite handles this
44482 ** function failing.
44483 */
44484 zBuf[0] = 0;
44485 SimulateIOError( return SQLITE_IOERR );
44486
44487 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
44488 zDir = unixTempFileDir();
44489 if( zDir==0 ){
44490 rc = SQLITE_IOERR_GETTEMPPATH(10 | (25<<8));
44491 }else{
44492 do{
44493 u64 r;
44494 sqlite3_randomness(sizeof(r), &r);
44495 assert( nBuf>2 )((void) (0));
44496 zBuf[nBuf-2] = 0;
44497 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"etilqs_""%llx%c",
44498 zDir, r, 0);
44499 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
44500 rc = SQLITE_ERROR1;
44501 break;
44502 }
44503 }while( osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zBuf,0)==0 );
44504 }
44505 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
44506 return rc;
44507}
44508
44509#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
44510/*
44511** Routine to transform a unixFile into a proxy-locking unixFile.
44512** Implementation in the proxy-lock division, but used by unixOpen()
44513** if SQLITE_PREFER_PROXY_LOCKING is defined.
44514*/
44515static int proxyTransformUnixFile(unixFile*, const char*);
44516#endif
44517
44518/*
44519** Search for an unused file descriptor that was opened on the database
44520** file (not a journal or super-journal file) identified by pathname
44521** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
44522** argument to this function.
44523**
44524** Such a file descriptor may exist if a database connection was closed
44525** but the associated file descriptor could not be closed because some
44526** other file descriptor open on the same file is holding a file-lock.
44527** Refer to comments in the unixClose() function and the lengthy comment
44528** describing "Posix Advisory Locking" at the start of this file for
44529** further details. Also, ticket #4018.
44530**
44531** If a suitable file descriptor is found, then it is returned. If no
44532** such file descriptor is located, -1 is returned.
44533*/
44534static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
44535 UnixUnusedFd *pUnused = 0;
44536
44537 /* Do not search for an unused file descriptor on vxworks. Not because
44538 ** vxworks would not benefit from the change (it might, we're not sure),
44539 ** but because no way to test it is currently available. It is better
44540 ** not to risk breaking vxworks support for the sake of such an obscure
44541 ** feature. */
44542#if !OS_VXWORKS0
44543 struct stat sStat; /* Results of stat() call */
44544
44545 unixEnterMutex();
44546
44547 /* A stat() call may fail for various reasons. If this happens, it is
44548 ** almost certain that an open() call on the same path will also fail.
44549 ** For this reason, if an error occurs in the stat() call here, it is
44550 ** ignored and -1 is returned. The caller will try to open a new file
44551 ** descriptor on the same path, fail, and return an error to SQLite.
44552 **
44553 ** Even if a subsequent open() call does succeed, the consequences of
44554 ** not searching for a reusable file descriptor are not dire. */
44555 if( inodeList!=0 && 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &sStat) ){
44556 unixInodeInfo *pInode;
44557
44558 pInode = inodeList;
44559 while( pInode && (pInode->fileId.dev!=sStat.st_dev
44560 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
44561 pInode = pInode->pNext;
44562 }
44563 if( pInode ){
44564 UnixUnusedFd **pp;
44565 assert( sqlite3_mutex_notheld(pInode->pLockMutex) )((void) (0));
44566 sqlite3_mutex_enter(pInode->pLockMutex);
44567 flags &= (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_READWRITE0x00000002);
44568 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
44569 pUnused = *pp;
44570 if( pUnused ){
44571 *pp = pUnused->pNext;
44572 }
44573 sqlite3_mutex_leave(pInode->pLockMutex);
44574 }
44575 }
44576 unixLeaveMutex();
44577#endif /* if !OS_VXWORKS */
44578 return pUnused;
44579}
44580
44581/*
44582** Find the mode, uid and gid of file zFile.
44583*/
44584static int getFileMode(
44585 const char *zFile, /* File name */
44586 mode_t *pMode, /* OUT: Permissions of zFile */
44587 uid_t *pUid, /* OUT: uid of zFile. */
44588 gid_t *pGid /* OUT: gid of zFile. */
44589){
44590 struct stat sStat; /* Output of stat() on database file */
44591 int rc = SQLITE_OK0;
44592 if( 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zFile, &sStat) ){
44593 *pMode = sStat.st_mode & 0777;
44594 *pUid = sStat.st_uid;
44595 *pGid = sStat.st_gid;
44596 }else{
44597 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
44598 }
44599 return rc;
44600}
44601
44602/*
44603** This function is called by unixOpen() to determine the unix permissions
44604** to create new files with. If no error occurs, then SQLITE_OK is returned
44605** and a value suitable for passing as the third argument to open(2) is
44606** written to *pMode. If an IO error occurs, an SQLite error code is
44607** returned and the value of *pMode is not modified.
44608**
44609** In most cases, this routine sets *pMode to 0, which will become
44610** an indication to robust_open() to create the file using
44611** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
44612** But if the file being opened is a WAL or regular journal file, then
44613** this function queries the file-system for the permissions on the
44614** corresponding database file and sets *pMode to this value. Whenever
44615** possible, WAL and journal files are created using the same permissions
44616** as the associated database file.
44617**
44618** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
44619** original filename is unavailable. But 8_3_NAMES is only used for
44620** FAT filesystems and permissions do not matter there, so just use
44621** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero.
44622*/
44623static int findCreateFileMode(
44624 const char *zPath, /* Path of file (possibly) being created */
44625 int flags, /* Flags passed as 4th argument to xOpen() */
44626 mode_t *pMode, /* OUT: Permissions to open file with */
44627 uid_t *pUid, /* OUT: uid to set on the file */
44628 gid_t *pGid /* OUT: gid to set on the file */
44629){
44630 int rc = SQLITE_OK0; /* Return Code */
44631 *pMode = 0;
44632 *pUid = 0;
44633 *pGid = 0;
44634 if( flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800) ){
44635 char zDb[MAX_PATHNAME512+1]; /* Database file path */
44636 int nDb; /* Number of valid bytes in zDb */
44637
44638 /* zPath is a path to a WAL or journal file. The following block derives
44639 ** the path to the associated database file from zPath. This block handles
44640 ** the following naming conventions:
44641 **
44642 ** "<path to db>-journal"
44643 ** "<path to db>-wal"
44644 ** "<path to db>-journalNN"
44645 ** "<path to db>-walNN"
44646 **
44647 ** where NN is a decimal number. The NN naming schemes are
44648 ** used by the test_multiplex.c module.
44649 **
44650 ** In normal operation, the journal file name will always contain
44651 ** a '-' character. However in 8+3 filename mode, or if a corrupt
44652 ** rollback journal specifies a super-journal with a goofy name, then
44653 ** the '-' might be missing or the '-' might be the first character in
44654 ** the filename. In that case, just return SQLITE_OK with *pMode==0.
44655 */
44656 nDb = sqlite3Strlen30(zPath) - 1;
44657 while( nDb>0 && zPath[nDb]!='.' ){
44658 if( zPath[nDb]=='-' ){
44659 memcpy(zDb, zPath, nDb);
44660 zDb[nDb] = '\0';
44661 rc = getFileMode(zDb, pMode, pUid, pGid);
44662 break;
44663 }
44664 nDb--;
44665 }
44666 }else if( flags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
44667 *pMode = 0600;
44668 }else if( flags & SQLITE_OPEN_URI0x00000040 ){
44669 /* If this is a main database file and the file was opened using a URI
44670 ** filename, check for the "modeof" parameter. If present, interpret
44671 ** its value as a filename and try to copy the mode, uid and gid from
44672 ** that file. */
44673 const char *z = sqlite3_uri_parameter(zPath, "modeof");
44674 if( z ){
44675 rc = getFileMode(z, pMode, pUid, pGid);
44676 }
44677 }
44678 return rc;
44679}
44680
44681/*
44682** Open the file zPath.
44683**
44684** Previously, the SQLite OS layer used three functions in place of this
44685** one:
44686**
44687** sqlite3OsOpenReadWrite();
44688** sqlite3OsOpenReadOnly();
44689** sqlite3OsOpenExclusive();
44690**
44691** These calls correspond to the following combinations of flags:
44692**
44693** ReadWrite() -> (READWRITE | CREATE)
44694** ReadOnly() -> (READONLY)
44695** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
44696**
44697** The old OpenExclusive() accepted a boolean argument - "delFlag". If
44698** true, the file was configured to be automatically deleted when the
44699** file handle closed. To achieve the same effect using this new
44700** interface, add the DELETEONCLOSE flag to those specified above for
44701** OpenExclusive().
44702*/
44703static int unixOpen(
44704 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
44705 const char *zPath, /* Pathname of file to be opened */
44706 sqlite3_file *pFile, /* The file descriptor to be filled in */
44707 int flags, /* Input flags to control the opening */
44708 int *pOutFlags /* Output flags returned to SQLite core */
44709){
44710 unixFile *p = (unixFile *)pFile;
44711 int fd = -1; /* File descriptor returned by open() */
44712 int openFlags = 0; /* Flags to pass to open() */
44713 int eType = flags&0x0FFF00; /* Type of file to open */
44714 int noLock; /* True to omit locking primitives */
44715 int rc = SQLITE_OK0; /* Function Return Code */
44716 int ctrlFlags = 0; /* UNIXFILE_* flags */
44717
44718 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
44719 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
44720 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
44721 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
44722 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
44723#if SQLITE_ENABLE_LOCKING_STYLE0
44724 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY0x00000020);
44725#endif
44726#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
44727 struct statfs fsInfo;
44728#endif
44729
44730 /* If creating a super- or main-file journal, this function will open
44731 ** a file-descriptor on the directory too. The first time unixSync()
44732 ** is called the directory file descriptor will be fsync()ed and close()d.
44733 */
44734 int isNewJrnl = (isCreate && (
44735 eType==SQLITE_OPEN_SUPER_JOURNAL0x00004000
44736 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
44737 || eType==SQLITE_OPEN_WAL0x00080000
44738 ));
44739
44740 /* If argument zPath is a NULL pointer, this function is required to open
44741 ** a temporary file. Use this buffer to store the file name in.
44742 */
44743 char zTmpname[MAX_PATHNAME512+2];
44744 const char *zName = zPath;
44745
44746 /* Check the following statements are true:
44747 **
44748 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
44749 ** (b) if CREATE is set, then READWRITE must also be set, and
44750 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
44751 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
44752 */
44753 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
44754 assert(isCreate==0 || isReadWrite)((void) (0));
44755 assert(isExclusive==0 || isCreate)((void) (0));
44756 assert(isDelete==0 || isCreate)((void) (0));
44757
44758 /* The main DB, main journal, WAL file and super-journal are never
44759 ** automatically deleted. Nor are they ever temporary files. */
44760 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
44761 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
44762 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL )((void) (0));
44763 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
44764
44765 /* Assert that the upper layer has set one of the "file-type" flags. */
44766 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
44767 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
44768 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL((void) (0))
44769 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
44770 )((void) (0));
44771
44772 /* Detect a pid change and reset the PRNG. There is a race condition
44773 ** here such that two or more threads all trying to open databases at
44774 ** the same instant might all reset the PRNG. But multiple resets
44775 ** are harmless.
44776 */
44777 if( randomnessPid!=osGetpid(0)(pid_t)getpid() ){
44778 randomnessPid = osGetpid(0)(pid_t)getpid();
44779 sqlite3_randomness(0,0);
44780 }
44781 memset(p, 0, sizeof(unixFile));
44782
44783#ifdef SQLITE_ASSERT_NO_FILES
44784 /* Applications that never read or write a persistent disk files */
44785 assert( zName==0 )((void) (0));
44786#endif
44787
44788 if( eType==SQLITE_OPEN_MAIN_DB0x00000100 ){
44789 UnixUnusedFd *pUnused;
44790 pUnused = findReusableFd(zName, flags);
44791 if( pUnused ){
44792 fd = pUnused->fd;
44793 }else{
44794 pUnused = sqlite3_malloc64(sizeof(*pUnused));
44795 if( !pUnused ){
44796 return SQLITE_NOMEM_BKPT7;
44797 }
44798 }
44799 p->pPreallocatedUnused = pUnused;
44800
44801 /* Database filenames are double-zero terminated if they are not
44802 ** URIs with parameters. Hence, they can always be passed into
44803 ** sqlite3_uri_parameter(). */
44804 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 )((void) (0));
44805
44806 }else if( !zName ){
44807 /* If zName is NULL, the upper layer is requesting a temp file. */
44808 assert(isDelete && !isNewJrnl)((void) (0));
44809 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
44810 if( rc!=SQLITE_OK0 ){
44811 return rc;
44812 }
44813 zName = zTmpname;
44814
44815 /* Generated temporary filenames are always double-zero terminated
44816 ** for use by sqlite3_uri_parameter(). */
44817 assert( zName[strlen(zName)+1]==0 )((void) (0));
44818 }
44819
44820 /* Determine the value of the flags parameter passed to POSIX function
44821 ** open(). These must be calculated even if open() is not called, as
44822 ** they may be stored as part of the file handle and used by the
44823 ** 'conch file' locking functions later on. */
44824 if( isReadonly ) openFlags |= O_RDONLY00;
44825 if( isReadWrite ) openFlags |= O_RDWR02;
44826 if( isCreate ) openFlags |= O_CREAT0100;
44827 if( isExclusive ) openFlags |= (O_EXCL0200|O_NOFOLLOW0400000);
44828 openFlags |= (O_LARGEFILE0|O_BINARY0|O_NOFOLLOW0400000);
44829
44830 if( fd<0 ){
44831 mode_t openMode; /* Permissions to create file with */
44832 uid_t uid; /* Userid for the file */
44833 gid_t gid; /* Groupid for the file */
44834 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
44835 if( rc!=SQLITE_OK0 ){
44836 assert( !p->pPreallocatedUnused )((void) (0));
44837 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
44838 return rc;
44839 }
44840 fd = robust_open(zName, openFlags, openMode);
44841 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
44842 assert( !isExclusive || (openFlags & O_CREAT)!=0 )((void) (0));
44843 if( fd<0 ){
44844 if( isNewJrnl && errno(*__errno_location ())==EACCES13 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zName, F_OK0) ){
44845 /* If unable to create a journal because the directory is not
44846 ** writable, change the error code to indicate that. */
44847 rc = SQLITE_READONLY_DIRECTORY(8 | (6<<8));
44848 }else if( errno(*__errno_location ())!=EISDIR21 && isReadWrite ){
44849 /* Failed to open the file for read/write access. Try read-only. */
44850 UnixUnusedFd *pReadonly = 0;
44851 flags &= ~(SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004);
44852 openFlags &= ~(O_RDWR02|O_CREAT0100);
44853 flags |= SQLITE_OPEN_READONLY0x00000001;
44854 openFlags |= O_RDONLY00;
44855 isReadonly = 1;
44856 pReadonly = findReusableFd(zName, flags);
44857 if( pReadonly ){
44858 fd = pReadonly->fd;
44859 sqlite3_free(pReadonly);
44860 }else{
44861 fd = robust_open(zName, openFlags, openMode);
44862 }
44863 }
44864 }
44865 if( fd<0 ){
44866 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName)unixLogErrorAtLine(sqlite3CantopenError(44866),"open",zName,44866
)
;
44867 if( rc==SQLITE_OK0 ) rc = rc2;
44868 goto open_finished;
44869 }
44870
44871 /* The owner of the rollback journal or WAL file should always be the
44872 ** same as the owner of the database file. Try to ensure that this is
44873 ** the case. The chown() system call will be a no-op if the current
44874 ** process lacks root privileges, be we should at least try. Without
44875 ** this step, if a root process opens a database file, it can leave
44876 ** behinds a journal/WAL that is owned by root and hence make the
44877 ** database inaccessible to unprivileged processes.
44878 **
44879 ** If openMode==0, then that means uid and gid are not set correctly
44880 ** (probably because SQLite is configured to use 8+3 filename mode) and
44881 ** in that case we do not want to attempt the chown().
44882 */
44883 if( openMode && (flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800))!=0 ){
44884 robustFchown(fd, uid, gid);
44885 }
44886 }
44887 assert( fd>=0 )((void) (0));
44888 if( pOutFlags ){
44889 *pOutFlags = flags;
44890 }
44891
44892 if( p->pPreallocatedUnused ){
44893 p->pPreallocatedUnused->fd = fd;
44894 p->pPreallocatedUnused->flags =
44895 flags & (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_READWRITE0x00000002);
44896 }
44897
44898 if( isDelete ){
44899#if OS_VXWORKS0
44900 zPath = zName;
44901#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
44902 zPath = sqlite3_mprintf("%s", zName);
44903 if( zPath==0 ){
44904 robust_close(p, fd, __LINE__44904);
44905 return SQLITE_NOMEM_BKPT7;
44906 }
44907#else
44908 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zName);
44909#endif
44910 }
44911#if SQLITE_ENABLE_LOCKING_STYLE0
44912 else{
44913 p->openFlags = openFlags;
44914 }
44915#endif
44916
44917#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
44918 if( fstatfs(fd, &fsInfo) == -1 ){
44919 storeLastErrno(p, errno(*__errno_location ()));
44920 robust_close(p, fd, __LINE__44920);
44921 return SQLITE_IOERR_ACCESS(10 | (13<<8));
44922 }
44923 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
44924 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
44925 }
44926 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
44927 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
44928 }
44929#endif
44930
44931 /* Set up appropriate ctrlFlags */
44932 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE0x20;
44933 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY0x02;
44934 noLock = eType!=SQLITE_OPEN_MAIN_DB0x00000100;
44935 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK0x80;
44936 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC0x08;
44937 if( flags & SQLITE_OPEN_URI0x00000040 ) ctrlFlags |= UNIXFILE_URI0x40;
44938
44939#if SQLITE_ENABLE_LOCKING_STYLE0
44940#if SQLITE_PREFER_PROXY_LOCKING
44941 isAutoProxy = 1;
44942#endif
44943 if( isAutoProxy && (zPath!=NULL((void*)0)) && (!noLock) && pVfs->xOpen ){
44944 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
44945 int useProxy = 0;
44946
44947 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
44948 ** never use proxy, NULL means use proxy for non-local files only. */
44949 if( envforce!=NULL((void*)0) ){
44950 useProxy = atoi(envforce)>0;
44951 }else{
44952 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
44953 }
44954 if( useProxy ){
44955 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
44956 if( rc==SQLITE_OK0 ){
44957 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
44958 if( rc!=SQLITE_OK0 ){
44959 /* Use unixClose to clean up the resources added in fillInUnixFile
44960 ** and clear all the structure's references. Specifically,
44961 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
44962 */
44963 unixClose(pFile);
44964 return rc;
44965 }
44966 }
44967 goto open_finished;
44968 }
44969 }
44970#endif
44971
44972 assert( zPath==0 || zPath[0]=='/'((void) (0))
44973 || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL((void) (0))
44974 )((void) (0));
44975 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
44976
44977open_finished:
44978 if( rc!=SQLITE_OK0 ){
44979 sqlite3_free(p->pPreallocatedUnused);
44980 }
44981 return rc;
44982}
44983
44984
44985/*
44986** Delete the file at zPath. If the dirSync argument is true, fsync()
44987** the directory after deleting the file.
44988*/
44989static int unixDelete(
44990 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
44991 const char *zPath, /* Name of file to be deleted */
44992 int dirSync /* If true, fsync() directory after deleting file */
44993){
44994 int rc = SQLITE_OK0;
44995 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
44996 SimulateIOError(return SQLITE_IOERR_DELETE);
44997 if( osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zPath)==(-1) ){
44998 if( errno(*__errno_location ())==ENOENT2
44999#if OS_VXWORKS0
45000 || osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath,0)!=0
45001#endif
45002 ){
45003 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8));
45004 }else{
45005 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath)unixLogErrorAtLine((10 | (10<<8)),"unlink",zPath,45005);
45006 }
45007 return rc;
45008 }
45009#ifndef SQLITE_DISABLE_DIRSYNC
45010 if( (dirSync & 1)!=0 ){
45011 int fd;
45012 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(zPath, &fd);
45013 if( rc==SQLITE_OK0 ){
45014 if( full_fsync(fd,0,0) ){
45015 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath)unixLogErrorAtLine((10 | (5<<8)),"fsync",zPath,45015);
45016 }
45017 robust_close(0, fd, __LINE__45017);
45018 }else{
45019 assert( rc==SQLITE_CANTOPEN )((void) (0));
45020 rc = SQLITE_OK0;
45021 }
45022 }
45023#endif
45024 return rc;
45025}
45026
45027/*
45028** Test the existence of or access permissions of file zPath. The
45029** test performed depends on the value of flags:
45030**
45031** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
45032** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
45033** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
45034**
45035** Otherwise return 0.
45036*/
45037static int unixAccess(
45038 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
45039 const char *zPath, /* Path of the file to examine */
45040 int flags, /* What do we want to learn about the zPath file? */
45041 int *pResOut /* Write result boolean here */
45042){
45043 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45044 SimulateIOError( return SQLITE_IOERR_ACCESS; );
45045 assert( pResOut!=0 )((void) (0));
45046
45047 /* The spec says there are three possible values for flags. But only
45048 ** two of them are actually used */
45049 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE )((void) (0));
45050
45051 if( flags==SQLITE_ACCESS_EXISTS0 ){
45052 struct stat buf;
45053 *pResOut = 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &buf) &&
45054 (!S_ISREG(buf.st_mode)((((buf.st_mode)) & 0170000) == (0100000)) || buf.st_size>0);
45055 }else{
45056 *pResOut = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath, W_OK2|R_OK4)==0;
45057 }
45058 return SQLITE_OK0;
45059}
45060
45061/*
45062** A pathname under construction
45063*/
45064typedef struct DbPath DbPath;
45065struct DbPath {
45066 int rc; /* Non-zero following any error */
45067 int nSymlink; /* Number of symlinks resolved */
45068 char *zOut; /* Write the pathname here */
45069 int nOut; /* Bytes of space available to zOut[] */
45070 int nUsed; /* Bytes of zOut[] currently being used */
45071};
45072
45073/* Forward reference */
45074static void appendAllPathElements(DbPath*,const char*);
45075
45076/*
45077** Append a single path element to the DbPath under construction
45078*/
45079static void appendOnePathElement(
45080 DbPath *pPath, /* Path under construction, to which to append zName */
45081 const char *zName, /* Name to append to pPath. Not zero-terminated */
45082 int nName /* Number of significant bytes in zName */
45083){
45084 assert( nName>0 )((void) (0));
45085 assert( zName!=0 )((void) (0));
45086 if( zName[0]=='.' ){
45087 if( nName==1 ) return;
45088 if( zName[1]=='.' && nName==2 ){
45089 if( pPath->nUsed>1 ){
45090 assert( pPath->zOut[0]=='/' )((void) (0));
45091 while( pPath->zOut[--pPath->nUsed]!='/' ){}
45092 }
45093 return;
45094 }
45095 }
45096 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
45097 pPath->rc = SQLITE_ERROR1;
45098 return;
45099 }
45100 pPath->zOut[pPath->nUsed++] = '/';
45101 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
45102 pPath->nUsed += nName;
45103#if defined(HAVE_READLINK1) && defined(HAVE_LSTAT1)
45104 if( pPath->rc==SQLITE_OK0 ){
45105 const char *zIn;
45106 struct stat buf;
45107 pPath->zOut[pPath->nUsed] = 0;
45108 zIn = pPath->zOut;
45109 if( osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)(zIn, &buf)!=0 ){
45110 if( errno(*__errno_location ())!=ENOENT2 ){
45111 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn)unixLogErrorAtLine(sqlite3CantopenError(45111),"lstat",zIn,45111
)
;
45112 }
45113 }else if( S_ISLNK(buf.st_mode)((((buf.st_mode)) & 0170000) == (0120000)) ){
45114 ssize_t got;
45115 char zLnk[SQLITE_MAX_PATHLEN4096+2];
45116 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK200 ){
45117 pPath->rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(45117);
45118 return;
45119 }
45120 got = osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)(zIn, zLnk, sizeof(zLnk)-2);
45121 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
45122 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn)unixLogErrorAtLine(sqlite3CantopenError(45122),"readlink",zIn
,45122)
;
45123 return;
45124 }
45125 zLnk[got] = 0;
45126 if( zLnk[0]=='/' ){
45127 pPath->nUsed = 0;
45128 }else{
45129 pPath->nUsed -= nName + 1;
45130 }
45131 appendAllPathElements(pPath, zLnk);
45132 }
45133 }
45134#endif
45135}
45136
45137/*
45138** Append all path elements in zPath to the DbPath under construction.
45139*/
45140static void appendAllPathElements(
45141 DbPath *pPath, /* Path under construction, to which to append zName */
45142 const char *zPath /* Path to append to pPath. Is zero-terminated */
45143){
45144 int i = 0;
45145 int j = 0;
45146 do{
45147 while( zPath[i] && zPath[i]!='/' ){ i++; }
45148 if( i>j ){
45149 appendOnePathElement(pPath, &zPath[j], i-j);
45150 }
45151 j = i+1;
45152 }while( zPath[i++] );
45153}
45154
45155/*
45156** Turn a relative pathname into a full pathname. The relative path
45157** is stored as a nul-terminated string in the buffer pointed to by
45158** zPath.
45159**
45160** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
45161** (in this case, MAX_PATHNAME bytes). The full-path is written to
45162** this buffer before returning.
45163*/
45164static int unixFullPathname(
45165 sqlite3_vfs *pVfs, /* Pointer to vfs object */
45166 const char *zPath, /* Possibly relative input path */
45167 int nOut, /* Size of output buffer in bytes */
45168 char *zOut /* Output buffer */
45169){
45170 DbPath path;
45171 UNUSED_PARAMETER(pVfs)(void)(pVfs);
45172 path.rc = 0;
45173 path.nUsed = 0;
45174 path.nSymlink = 0;
45175 path.nOut = nOut;
45176 path.zOut = zOut;
45177 if( zPath[0]!='/' ){
45178 char zPwd[SQLITE_MAX_PATHLEN4096+2];
45179 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent)(zPwd, sizeof(zPwd)-2)==0 ){
45180 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath)unixLogErrorAtLine(sqlite3CantopenError(45180),"getcwd",zPath
,45180)
;
45181 }
45182 appendAllPathElements(&path, zPwd);
45183 }
45184 appendAllPathElements(&path, zPath);
45185 zOut[path.nUsed] = 0;
45186 if( path.rc || path.nUsed<2 ) return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(45186);
45187 if( path.nSymlink ) return SQLITE_OK_SYMLINK(0 | (2<<8));
45188 return SQLITE_OK0;
45189}
45190
45191#ifndef SQLITE_OMIT_LOAD_EXTENSION1
45192/*
45193** Interfaces for opening a shared library, finding entry points
45194** within the shared library, and closing the shared library.
45195*/
45196#include <dlfcn.h>
45197static void *unixDlOpen0(sqlite3_vfs *NotUsed, const char *zFilename){
45198 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45199 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
45200}
45201
45202/*
45203** SQLite calls this function immediately after a call to unixDlSym() or
45204** unixDlOpen() fails (returns a null pointer). If a more detailed error
45205** message is available, it is written to zBufOut. If no error message
45206** is available, zBufOut is left unmodified and SQLite uses a default
45207** error message.
45208*/
45209static void unixDlError0(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
45210 const char *zErr;
45211 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45212 unixEnterMutex();
45213 zErr = dlerror();
45214 if( zErr ){
45215 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
45216 }
45217 unixLeaveMutex();
45218}
45219static void (*unixDlSym0(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
45220 /*
45221 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
45222 ** cast into a pointer to a function. And yet the library dlsym() routine
45223 ** returns a void* which is really a pointer to a function. So how do we
45224 ** use dlsym() with -pedantic-errors?
45225 **
45226 ** Variable x below is defined to be a pointer to a function taking
45227 ** parameters void* and const char* and returning a pointer to a function.
45228 ** We initialize x by assigning it a pointer to the dlsym() function.
45229 ** (That assignment requires a cast.) Then we call the function that
45230 ** x points to.
45231 **
45232 ** This work-around is unlikely to work correctly on any system where
45233 ** you really cannot cast a function pointer into void*. But then, on the
45234 ** other hand, dlsym() will not work on such a system either, so we have
45235 ** not really lost anything.
45236 */
45237 void (*(*x)(void*,const char*))(void);
45238 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45239 x = (void(*(*)(void*,const char*))(void))dlsym;
45240 return (*x)(p, zSym);
45241}
45242static void unixDlClose0(sqlite3_vfs *NotUsed, void *pHandle){
45243 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45244 dlclose(pHandle);
45245}
45246#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
45247 #define unixDlOpen0 0
45248 #define unixDlError0 0
45249 #define unixDlSym0 0
45250 #define unixDlClose0 0
45251#endif
45252
45253/*
45254** Write nBuf bytes of random data to the supplied buffer zBuf.
45255*/
45256static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
45257 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45258 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)))((void) (0));
45259
45260 /* We have to initialize zBuf to prevent valgrind from reporting
45261 ** errors. The reports issued by valgrind are incorrect - we would
45262 ** prefer that the randomness be increased by making use of the
45263 ** uninitialized space in zBuf - but valgrind errors tend to worry
45264 ** some users. Rather than argue, it seems easier just to initialize
45265 ** the whole array and silence valgrind, even if that means less randomness
45266 ** in the random seed.
45267 **
45268 ** When testing, initializing zBuf[] to zero is all we do. That means
45269 ** that we always use the same random number sequence. This makes the
45270 ** tests repeatable.
45271 */
45272 memset(zBuf, 0, nBuf);
45273 randomnessPid = osGetpid(0)(pid_t)getpid();
45274#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
45275 {
45276 int fd, got;
45277 fd = robust_open("/dev/urandom", O_RDONLY00, 0);
45278 if( fd<0 ){
45279 time_t t;
45280 time(&t);
45281 memcpy(zBuf, &t, sizeof(t));
45282 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
45283 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf )((void) (0));
45284 nBuf = sizeof(t) + sizeof(randomnessPid);
45285 }else{
45286 do{ got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(fd, zBuf, nBuf); }while( got<0 && errno(*__errno_location ())==EINTR4 );
45287 robust_close(0, fd, __LINE__45287);
45288 }
45289 }
45290#endif
45291 return nBuf;
45292}
45293
45294
45295/*
45296** Sleep for a little while. Return the amount of time slept.
45297** The argument is the number of microseconds we want to sleep.
45298** The return value is the number of microseconds of sleep actually
45299** requested from the underlying operating system, a number which
45300** might be greater than or equal to the argument, but not less
45301** than the argument.
45302*/
45303static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
45304#if !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP+0
45305 struct timespec sp;
45306 sp.tv_sec = microseconds / 1000000;
45307 sp.tv_nsec = (microseconds % 1000000) * 1000;
45308
45309 /* Almost all modern unix systems support nanosleep(). But if you are
45310 ** compiling for one of the rare exceptions, you can use
45311 ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
45312 ** usleep() is available) in order to bypass the use of nanosleep() */
45313 nanosleep(&sp, NULL((void*)0));
45314
45315 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45316 return microseconds;
45317#elif defined(HAVE_USLEEP1) && HAVE_USLEEP1
45318 if( microseconds>=1000000 ) sleep(microseconds/1000000);
45319 if( microseconds%1000000 ) usleep(microseconds%1000000);
45320 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45321 return microseconds;
45322#else
45323 int seconds = (microseconds+999999)/1000000;
45324 sleep(seconds);
45325 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45326 return seconds*1000000;
45327#endif
45328}
45329
45330/*
45331** The following variable, if set to a non-zero value, is interpreted as
45332** the number of seconds since 1970 and is used to set the result of
45333** sqlite3OsCurrentTime() during testing.
45334*/
45335#ifdef SQLITE_TEST
45336SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
45337#endif
45338
45339/*
45340** Find the current time (in Universal Coordinated Time). Write into *piNow
45341** the current time and date as a Julian Day number times 86_400_000. In
45342** other words, write into *piNow the number of milliseconds since the Julian
45343** epoch of noon in Greenwich on November 24, 4714 B.C according to the
45344** proleptic Gregorian calendar.
45345**
45346** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
45347** cannot be found.
45348*/
45349static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
45350 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
45351 int rc = SQLITE_OK0;
45352#if defined(NO_GETTOD)
45353 time_t t;
45354 time(&t);
45355 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
45356#elif OS_VXWORKS0
45357 struct timespec sNow;
45358 clock_gettime(CLOCK_REALTIME0, &sNow);
45359 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
45360#else
45361 struct timeval sNow;
45362 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
45363 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
45364#endif
45365
45366#ifdef SQLITE_TEST
45367 if( sqlite3_current_time ){
45368 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
45369 }
45370#endif
45371 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45372 return rc;
45373}
45374
45375#ifndef SQLITE_OMIT_DEPRECATED1
45376/*
45377** Find the current time (in Universal Coordinated Time). Write the
45378** current time and date as a Julian Day number into *prNow and
45379** return 0. Return 1 if the time and date cannot be found.
45380*/
45381static int unixCurrentTime0(sqlite3_vfs *NotUsed, double *prNow){
45382 sqlite3_int64 i = 0;
45383 int rc;
45384 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45385 rc = unixCurrentTimeInt64(0, &i);
45386 *prNow = i/86400000.0;
45387 return rc;
45388}
45389#else
45390# define unixCurrentTime0 0
45391#endif
45392
45393/*
45394** The xGetLastError() method is designed to return a better
45395** low-level error message when operating-system problems come up
45396** during SQLite operation. Only the integer return code is currently
45397** used.
45398*/
45399static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
45400 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
45401 UNUSED_PARAMETER(NotUsed2)(void)(NotUsed2);
45402 UNUSED_PARAMETER(NotUsed3)(void)(NotUsed3);
45403 return errno(*__errno_location ());
45404}
45405
45406
45407/*
45408************************ End of sqlite3_vfs methods ***************************
45409******************************************************************************/
45410
45411/******************************************************************************
45412************************** Begin Proxy Locking ********************************
45413**
45414** Proxy locking is a "uber-locking-method" in this sense: It uses the
45415** other locking methods on secondary lock files. Proxy locking is a
45416** meta-layer over top of the primitive locking implemented above. For
45417** this reason, the division that implements of proxy locking is deferred
45418** until late in the file (here) after all of the other I/O methods have
45419** been defined - so that the primitive locking methods are available
45420** as services to help with the implementation of proxy locking.
45421**
45422****
45423**
45424** The default locking schemes in SQLite use byte-range locks on the
45425** database file to coordinate safe, concurrent access by multiple readers
45426** and writers [http://sqlite.org/lockingv3.html]. The five file locking
45427** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
45428** as POSIX read & write locks over fixed set of locations (via fsctl),
45429** on AFP and SMB only exclusive byte-range locks are available via fsctl
45430** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
45431** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
45432** address in the shared range is taken for a SHARED lock, the entire
45433** shared range is taken for an EXCLUSIVE lock):
45434**
45435** PENDING_BYTE 0x40000000
45436** RESERVED_BYTE 0x40000001
45437** SHARED_RANGE 0x40000002 -> 0x40000200
45438**
45439** This works well on the local file system, but shows a nearly 100x
45440** slowdown in read performance on AFP because the AFP client disables
45441** the read cache when byte-range locks are present. Enabling the read
45442** cache exposes a cache coherency problem that is present on all OS X
45443** supported network file systems. NFS and AFP both observe the
45444** close-to-open semantics for ensuring cache coherency
45445** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
45446** address the requirements for concurrent database access by multiple
45447** readers and writers
45448** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
45449**
45450** To address the performance and cache coherency issues, proxy file locking
45451** changes the way database access is controlled by limiting access to a
45452** single host at a time and moving file locks off of the database file
45453** and onto a proxy file on the local file system.
45454**
45455**
45456** Using proxy locks
45457** -----------------
45458**
45459** C APIs
45460**
45461** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
45462** <proxy_path> | ":auto:");
45463** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
45464** &<proxy_path>);
45465**
45466**
45467** SQL pragmas
45468**
45469** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
45470** PRAGMA [database.]lock_proxy_file
45471**
45472** Specifying ":auto:" means that if there is a conch file with a matching
45473** host ID in it, the proxy path in the conch file will be used, otherwise
45474** a proxy path based on the user's temp dir
45475** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
45476** actual proxy file name is generated from the name and path of the
45477** database file. For example:
45478**
45479** For database path "/Users/me/foo.db"
45480** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
45481**
45482** Once a lock proxy is configured for a database connection, it can not
45483** be removed, however it may be switched to a different proxy path via
45484** the above APIs (assuming the conch file is not being held by another
45485** connection or process).
45486**
45487**
45488** How proxy locking works
45489** -----------------------
45490**
45491** Proxy file locking relies primarily on two new supporting files:
45492**
45493** * conch file to limit access to the database file to a single host
45494** at a time
45495**
45496** * proxy file to act as a proxy for the advisory locks normally
45497** taken on the database
45498**
45499** The conch file - to use a proxy file, sqlite must first "hold the conch"
45500** by taking an sqlite-style shared lock on the conch file, reading the
45501** contents and comparing the host's unique host ID (see below) and lock
45502** proxy path against the values stored in the conch. The conch file is
45503** stored in the same directory as the database file and the file name
45504** is patterned after the database file name as ".<databasename>-conch".
45505** If the conch file does not exist, or its contents do not match the
45506** host ID and/or proxy path, then the lock is escalated to an exclusive
45507** lock and the conch file contents is updated with the host ID and proxy
45508** path and the lock is downgraded to a shared lock again. If the conch
45509** is held by another process (with a shared lock), the exclusive lock
45510** will fail and SQLITE_BUSY is returned.
45511**
45512** The proxy file - a single-byte file used for all advisory file locks
45513** normally taken on the database file. This allows for safe sharing
45514** of the database file for multiple readers and writers on the same
45515** host (the conch ensures that they all use the same local lock file).
45516**
45517** Requesting the lock proxy does not immediately take the conch, it is
45518** only taken when the first request to lock database file is made.
45519** This matches the semantics of the traditional locking behavior, where
45520** opening a connection to a database file does not take a lock on it.
45521** The shared lock and an open file descriptor are maintained until
45522** the connection to the database is closed.
45523**
45524** The proxy file and the lock file are never deleted so they only need
45525** to be created the first time they are used.
45526**
45527** Configuration options
45528** ---------------------
45529**
45530** SQLITE_PREFER_PROXY_LOCKING
45531**
45532** Database files accessed on non-local file systems are
45533** automatically configured for proxy locking, lock files are
45534** named automatically using the same logic as
45535** PRAGMA lock_proxy_file=":auto:"
45536**
45537** SQLITE_PROXY_DEBUG
45538**
45539** Enables the logging of error messages during host id file
45540** retrieval and creation
45541**
45542** LOCKPROXYDIR
45543**
45544** Overrides the default directory used for lock proxy files that
45545** are named automatically via the ":auto:" setting
45546**
45547** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
45548**
45549** Permissions to use when creating a directory for storing the
45550** lock proxy files, only used when LOCKPROXYDIR is not set.
45551**
45552**
45553** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
45554** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
45555** force proxy locking to be used for every database file opened, and 0
45556** will force automatic proxy locking to be disabled for all database
45557** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
45558** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
45559*/
45560
45561/*
45562** Proxy locking is only available on MacOSX
45563*/
45564#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
45565
45566/*
45567** The proxyLockingContext has the path and file structures for the remote
45568** and local proxy files in it
45569*/
45570typedef struct proxyLockingContext proxyLockingContext;
45571struct proxyLockingContext {
45572 unixFile *conchFile; /* Open conch file */
45573 char *conchFilePath; /* Name of the conch file */
45574 unixFile *lockProxy; /* Open proxy lock file */
45575 char *lockProxyPath; /* Name of the proxy lock file */
45576 char *dbPath; /* Name of the open file */
45577 int conchHeld; /* 1 if the conch is held, -1 if lockless */
45578 int nFails; /* Number of conch taking failures */
45579 void *oldLockingContext; /* Original lockingcontext to restore on close */
45580 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
45581};
45582
45583/*
45584** The proxy lock file path for the database at dbPath is written into lPath,
45585** which must point to valid, writable memory large enough for a maxLen length
45586** file path.
45587*/
45588static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
45589 int len;
45590 int dbLen;
45591 int i;
45592
45593#ifdef LOCKPROXYDIR
45594 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
45595#else
45596# ifdef _CS_DARWIN_USER_TEMP_DIR
45597 {
45598 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
45599 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
45600 lPath, errno, osGetpid(0)));
45601 return SQLITE_IOERR_LOCK(10 | (15<<8));
45602 }
45603 len = strlcat(lPath, "sqliteplocks", maxLen);
45604 }
45605# else
45606 len = strlcpy(lPath, "/tmp/", maxLen);
45607# endif
45608#endif
45609
45610 if( lPath[len-1]!='/' ){
45611 len = strlcat(lPath, "/", maxLen);
45612 }
45613
45614 /* transform the db path to a unique cache name */
45615 dbLen = (int)strlen(dbPath);
45616 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
45617 char c = dbPath[i];
45618 lPath[i+len] = (c=='/')?'_':c;
45619 }
45620 lPath[i+len]='\0';
45621 strlcat(lPath, ":auto:", maxLen);
45622 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
45623 return SQLITE_OK0;
45624}
45625
45626/*
45627 ** Creates the lock file and any missing directories in lockPath
45628 */
45629static int proxyCreateLockPath(const char *lockPath){
45630 int i, len;
45631 char buf[MAXPATHLEN];
45632 int start = 0;
45633
45634 assert(lockPath!=NULL)((void) (0));
45635 /* try to create all the intermediate directories */
45636 len = (int)strlen(lockPath);
45637 buf[0] = lockPath[0];
45638 for( i=1; i<len; i++ ){
45639 if( lockPath[i] == '/' && (i - start > 0) ){
45640 /* only mkdir if leaf dir != "." or "/" or ".." */
45641 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
45642 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
45643 buf[i]='\0';
45644 if( osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755) ){
45645 int err=errno(*__errno_location ());
45646 if( err!=EEXIST17 ) {
45647 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
45648 "'%s' proxy lock path=%s pid=%d\n",
45649 buf, strerror(err), lockPath, osGetpid(0)));
45650 return err;
45651 }
45652 }
45653 }
45654 start=i+1;
45655 }
45656 buf[i] = lockPath[i];
45657 }
45658 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
45659 return 0;
45660}
45661
45662/*
45663** Create a new VFS file descriptor (stored in memory obtained from
45664** sqlite3_malloc) and open the file named "path" in the file descriptor.
45665**
45666** The caller is responsible not only for closing the file descriptor
45667** but also for freeing the memory associated with the file descriptor.
45668*/
45669static int proxyCreateUnixFile(
45670 const char *path, /* path for the new unixFile */
45671 unixFile **ppFile, /* unixFile created and returned by ref */
45672 int islockfile /* if non zero missing dirs will be created */
45673) {
45674 int fd = -1;
45675 unixFile *pNew;
45676 int rc = SQLITE_OK0;
45677 int openFlags = O_RDWR02 | O_CREAT0100 | O_NOFOLLOW0400000;
45678 sqlite3_vfs dummyVfs;
45679 int terrno = 0;
45680 UnixUnusedFd *pUnused = NULL((void*)0);
45681
45682 /* 1. first try to open/create the file
45683 ** 2. if that fails, and this is a lock file (not-conch), try creating
45684 ** the parent directories and then try again.
45685 ** 3. if that fails, try to open the file read-only
45686 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
45687 */
45688 pUnused = findReusableFd(path, openFlags);
45689 if( pUnused ){
45690 fd = pUnused->fd;
45691 }else{
45692 pUnused = sqlite3_malloc64(sizeof(*pUnused));
45693 if( !pUnused ){
45694 return SQLITE_NOMEM_BKPT7;
45695 }
45696 }
45697 if( fd<0 ){
45698 fd = robust_open(path, openFlags, 0);
45699 terrno = errno(*__errno_location ());
45700 if( fd<0 && errno(*__errno_location ())==ENOENT2 && islockfile ){
45701 if( proxyCreateLockPath(path) == SQLITE_OK0 ){
45702 fd = robust_open(path, openFlags, 0);
45703 }
45704 }
45705 }
45706 if( fd<0 ){
45707 openFlags = O_RDONLY00 | O_NOFOLLOW0400000;
45708 fd = robust_open(path, openFlags, 0);
45709 terrno = errno(*__errno_location ());
45710 }
45711 if( fd<0 ){
45712 if( islockfile ){
45713 return SQLITE_BUSY5;
45714 }
45715 switch (terrno) {
45716 case EACCES13:
45717 return SQLITE_PERM3;
45718 case EIO5:
45719 return SQLITE_IOERR_LOCK(10 | (15<<8)); /* even though it is the conch */
45720 default:
45721 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(45721);
45722 }
45723 }
45724
45725 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
45726 if( pNew==NULL((void*)0) ){
45727 rc = SQLITE_NOMEM_BKPT7;
45728 goto end_create_proxy;
45729 }
45730 memset(pNew, 0, sizeof(unixFile));
45731 pNew->openFlags = openFlags;
45732 memset(&dummyVfs, 0, sizeof(dummyVfs));
45733 dummyVfs.pAppData = (void*)&autolockIoFinder;
45734 dummyVfs.zName = "dummy";
45735 pUnused->fd = fd;
45736 pUnused->flags = openFlags;
45737 pNew->pPreallocatedUnused = pUnused;
45738
45739 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
45740 if( rc==SQLITE_OK0 ){
45741 *ppFile = pNew;
45742 return SQLITE_OK0;
45743 }
45744end_create_proxy:
45745 robust_close(pNew, fd, __LINE__45745);
45746 sqlite3_free(pNew);
45747 sqlite3_free(pUnused);
45748 return rc;
45749}
45750
45751#ifdef SQLITE_TEST
45752/* simulate multiple hosts by creating unique hostid file paths */
45753SQLITE_API int sqlite3_hostid_num = 0;
45754#endif
45755
45756#define PROXY_HOSTIDLEN 16 /* conch file host id length */
45757
45758#if HAVE_GETHOSTUUID0
45759/* Not always defined in the headers as it ought to be */
45760extern int gethostuuid(uuid_t id, const struct timespec *wait);
45761#endif
45762
45763/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
45764** bytes of writable memory.
45765*/
45766static int proxyGetHostID(unsigned char *pHostID, int *pError){
45767 assert(PROXY_HOSTIDLEN == sizeof(uuid_t))((void) (0));
45768 memset(pHostID, 0, PROXY_HOSTIDLEN);
45769#if HAVE_GETHOSTUUID0
45770 {
45771 struct timespec timeout = {1, 0}; /* 1 sec timeout */
45772 if( gethostuuid(pHostID, &timeout) ){
45773 int err = errno(*__errno_location ());
45774 if( pError ){
45775 *pError = err;
45776 }
45777 return SQLITE_IOERR10;
45778 }
45779 }
45780#else
45781 UNUSED_PARAMETER(pError)(void)(pError);
45782#endif
45783#ifdef SQLITE_TEST
45784 /* simulate multiple hosts by creating unique hostid file paths */
45785 if( sqlite3_hostid_num != 0){
45786 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
45787 }
45788#endif
45789
45790 return SQLITE_OK0;
45791}
45792
45793/* The conch file contains the header, host id and lock file path
45794 */
45795#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
45796#define PROXY_HEADERLEN 1 /* conch file header length */
45797#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
45798#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
45799
45800/*
45801** Takes an open conch file, copies the contents to a new path and then moves
45802** it back. The newly created file's file descriptor is assigned to the
45803** conch file structure and finally the original conch file descriptor is
45804** closed. Returns zero if successful.
45805*/
45806static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
45807 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45808 unixFile *conchFile = pCtx->conchFile;
45809 char tPath[MAXPATHLEN];
45810 char buf[PROXY_MAXCONCHLEN];
45811 char *cPath = pCtx->conchFilePath;
45812 size_t readLen = 0;
45813 size_t pathLen = 0;
45814 char errmsg[64] = "";
45815 int fd = -1;
45816 int rc = -1;
45817 UNUSED_PARAMETER(myHostID)(void)(myHostID);
45818
45819 /* create a new path by replace the trailing '-conch' with '-break' */
45820 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
45821 if( pathLen>MAXPATHLEN || pathLen<6 ||
45822 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
45823 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
45824 goto end_breaklock;
45825 }
45826 /* read the conch content */
45827 readLen = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
45828 if( readLen<PROXY_PATHINDEX ){
45829 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
45830 goto end_breaklock;
45831 }
45832 /* write it out to the temporary break file */
45833 fd = robust_open(tPath, (O_RDWR02|O_CREAT0100|O_EXCL0200|O_NOFOLLOW0400000), 0);
45834 if( fd<0 ){
45835 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno(*__errno_location ()));
45836 goto end_breaklock;
45837 }
45838 if( osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
(fd, buf, readLen, 0) != (ssize_t)readLen ){
45839 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno(*__errno_location ()));
45840 goto end_breaklock;
45841 }
45842 if( rename(tPath, cPath) ){
45843 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno(*__errno_location ()));
45844 goto end_breaklock;
45845 }
45846 rc = 0;
45847 fprintf(stderrstderr, "broke stale lock on %s\n", cPath);
45848 robust_close(pFile, conchFile->h, __LINE__45848);
45849 conchFile->h = fd;
45850 conchFile->openFlags = O_RDWR02 | O_CREAT0100;
45851
45852end_breaklock:
45853 if( rc ){
45854 if( fd>=0 ){
45855 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(tPath);
45856 robust_close(pFile, fd, __LINE__45856);
45857 }
45858 fprintf(stderrstderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
45859 }
45860 return rc;
45861}
45862
45863/* Take the requested lock on the conch file and break a stale lock if the
45864** host id matches.
45865*/
45866static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
45867 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45868 unixFile *conchFile = pCtx->conchFile;
45869 int rc = SQLITE_OK0;
45870 int nTries = 0;
45871 struct timespec conchModTime;
45872
45873 memset(&conchModTime, 0, sizeof(conchModTime));
45874 do {
45875 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45876 nTries ++;
45877 if( rc==SQLITE_BUSY5 ){
45878 /* If the lock failed (busy):
45879 * 1st try: get the mod time of the conch, wait 0.5s and try again.
45880 * 2nd try: fail if the mod time changed or host id is different, wait
45881 * 10 sec and try again
45882 * 3rd try: break the lock unless the mod time has changed.
45883 */
45884 struct stat buf;
45885 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(conchFile->h, &buf) ){
45886 storeLastErrno(pFile, errno(*__errno_location ()));
45887 return SQLITE_IOERR_LOCK(10 | (15<<8));
45888 }
45889
45890 if( nTries==1 ){
45891 conchModTime = buf.st_mtimespec;
45892 unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/
45893 continue;
45894 }
45895
45896 assert( nTries>1 )((void) (0));
45897 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
45898 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
45899 return SQLITE_BUSY5;
45900 }
45901
45902 if( nTries==2 ){
45903 char tBuf[PROXY_MAXCONCHLEN];
45904 int len = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
45905 if( len<0 ){
45906 storeLastErrno(pFile, errno(*__errno_location ()));
45907 return SQLITE_IOERR_LOCK(10 | (15<<8));
45908 }
45909 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
45910 /* don't break the lock if the host id doesn't match */
45911 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
45912 return SQLITE_BUSY5;
45913 }
45914 }else{
45915 /* don't break the lock on short read or a version mismatch */
45916 return SQLITE_BUSY5;
45917 }
45918 unixSleep(0,10000000); /* wait 10 sec and try the lock again */
45919 continue;
45920 }
45921
45922 assert( nTries==3 )((void) (0));
45923 if( 0==proxyBreakConchLock(pFile, myHostID) ){
45924 rc = SQLITE_OK0;
45925 if( lockType==EXCLUSIVE_LOCK4 ){
45926 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK1);
45927 }
45928 if( !rc ){
45929 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45930 }
45931 }
45932 }
45933 } while( rc==SQLITE_BUSY5 && nTries<3 );
45934
45935 return rc;
45936}
45937
45938/* Takes the conch by taking a shared lock and read the contents conch, if
45939** lockPath is non-NULL, the host ID and lock file path must match. A NULL
45940** lockPath means that the lockPath in the conch file will be used if the
45941** host IDs match, or a new lock path will be generated automatically
45942** and written to the conch file.
45943*/
45944static int proxyTakeConch(unixFile *pFile){
45945 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45946
45947 if( pCtx->conchHeld!=0 ){
45948 return SQLITE_OK0;
45949 }else{
45950 unixFile *conchFile = pCtx->conchFile;
45951 uuid_t myHostID;
45952 int pError = 0;
45953 char readBuf[PROXY_MAXCONCHLEN];
45954 char lockPath[MAXPATHLEN];
45955 char *tempLockPath = NULL((void*)0);
45956 int rc = SQLITE_OK0;
45957 int createConch = 0;
45958 int hostIdMatch = 0;
45959 int readLen = 0;
45960 int tryOldLockPath = 0;
45961 int forceNewLockPath = 0;
45962
45963 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
45964 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45965 osGetpid(0)));
45966
45967 rc = proxyGetHostID(myHostID, &pError);
45968 if( (rc&0xff)==SQLITE_IOERR10 ){
45969 storeLastErrno(pFile, pError);
45970 goto end_takeconch;
45971 }
45972 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK1);
45973 if( rc!=SQLITE_OK0 ){
45974 goto end_takeconch;
45975 }
45976 /* read the existing conch file */
45977 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
45978 if( readLen<0 ){
45979 /* I/O error: lastErrno set by seekAndRead */
45980 storeLastErrno(pFile, conchFile->lastErrno);
45981 rc = SQLITE_IOERR_READ(10 | (1<<8));
45982 goto end_takeconch;
45983 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
45984 readBuf[0]!=(char)PROXY_CONCHVERSION ){
45985 /* a short read or version format mismatch means we need to create a new
45986 ** conch file.
45987 */
45988 createConch = 1;
45989 }
45990 /* if the host id matches and the lock path already exists in the conch
45991 ** we'll try to use the path there, if we can't open that path, we'll
45992 ** retry with a new auto-generated path
45993 */
45994 do { /* in case we need to try again for an :auto: named lock file */
45995
45996 if( !createConch && !forceNewLockPath ){
45997 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
45998 PROXY_HOSTIDLEN);
45999 /* if the conch has data compare the contents */
46000 if( !pCtx->lockProxyPath ){
46001 /* for auto-named local lock file, just check the host ID and we'll
46002 ** use the local lock file path that's already in there
46003 */
46004 if( hostIdMatch ){
46005 size_t pathLen = (readLen - PROXY_PATHINDEX);
46006
46007 if( pathLen>=MAXPATHLEN ){
46008 pathLen=MAXPATHLEN-1;
46009 }
46010 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
46011 lockPath[pathLen] = 0;
46012 tempLockPath = lockPath;
46013 tryOldLockPath = 1;
46014 /* create a copy of the lock path if the conch is taken */
46015 goto end_takeconch;
46016 }
46017 }else if( hostIdMatch
46018 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
46019 readLen-PROXY_PATHINDEX)
46020 ){
46021 /* conch host and lock path match */
46022 goto end_takeconch;
46023 }
46024 }
46025
46026 /* if the conch isn't writable and doesn't match, we can't take it */
46027 if( (conchFile->openFlags&O_RDWR02) == 0 ){
46028 rc = SQLITE_BUSY5;
46029 goto end_takeconch;
46030 }
46031
46032 /* either the conch didn't match or we need to create a new one */
46033 if( !pCtx->lockProxyPath ){
46034 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
46035 tempLockPath = lockPath;
46036 /* create a copy of the lock path _only_ if the conch is taken */
46037 }
46038
46039 /* update conch with host and path (this will fail if other process
46040 ** has a shared lock already), if the host id matches, use the big
46041 ** stick.
46042 */
46043 futimes(conchFile->h, NULL((void*)0));
46044 if( hostIdMatch && !createConch ){
46045 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
46046 /* We are trying for an exclusive lock but another thread in this
46047 ** same process is still holding a shared lock. */
46048 rc = SQLITE_BUSY5;
46049 } else {
46050 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
46051 }
46052 }else{
46053 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
46054 }
46055 if( rc==SQLITE_OK0 ){
46056 char writeBuffer[PROXY_MAXCONCHLEN];
46057 int writeSize = 0;
46058
46059 writeBuffer[0] = (char)PROXY_CONCHVERSION;
46060 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
46061 if( pCtx->lockProxyPath!=NULL((void*)0) ){
46062 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
46063 MAXPATHLEN);
46064 }else{
46065 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
46066 }
46067 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
46068 robust_ftruncate(conchFile->h, writeSize);
46069 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
46070 full_fsync(conchFile->h,0,0);
46071 /* If we created a new conch file (not just updated the contents of a
46072 ** valid conch file), try to match the permissions of the database
46073 */
46074 if( rc==SQLITE_OK0 && createConch ){
46075 struct stat buf;
46076 int err = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
46077 if( err==0 ){
46078 mode_t cmode = buf.st_mode&(S_IRUSR0400|S_IWUSR0200 | S_IRGRP(0400 >> 3)|S_IWGRP(0200 >> 3) |
46079 S_IROTH((0400 >> 3) >> 3)|S_IWOTH((0200 >> 3) >> 3));
46080 /* try to match the database file R/W permissions, ignore failure */
46081#ifndef SQLITE_PROXY_DEBUG
46082 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
46083#else
46084 do{
46085 rc = osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
46086 }while( rc==(-1) && errno(*__errno_location ())==EINTR4 );
46087 if( rc!=0 ){
46088 int code = errno(*__errno_location ());
46089 fprintf(stderrstderr, "fchmod %o FAILED with %d %s\n",
46090 cmode, code, strerror(code));
46091 } else {
46092 fprintf(stderrstderr, "fchmod %o SUCCEDED\n",cmode);
46093 }
46094 }else{
46095 int code = errno(*__errno_location ());
46096 fprintf(stderrstderr, "STAT FAILED[%d] with %d %s\n",
46097 err, code, strerror(code));
46098#endif
46099 }
46100 }
46101 }
46102 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK1);
46103
46104 end_takeconch:
46105 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
46106 if( rc==SQLITE_OK0 && pFile->openFlags ){
46107 int fd;
46108 if( pFile->h>=0 ){
46109 robust_close(pFile, pFile->h, __LINE__46109);
46110 }
46111 pFile->h = -1;
46112 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
46113 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
46114 if( fd>=0 ){
46115 pFile->h = fd;
46116 }else{
46117 rc=SQLITE_CANTOPEN_BKPTsqlite3CantopenError(46117); /* SQLITE_BUSY? proxyTakeConch called
46118 during locking */
46119 }
46120 }
46121 if( rc==SQLITE_OK0 && !pCtx->lockProxy ){
46122 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
46123 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
46124 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 && tryOldLockPath ){
46125 /* we couldn't create the proxy lock file with the old lock file path
46126 ** so try again via auto-naming
46127 */
46128 forceNewLockPath = 1;
46129 tryOldLockPath = 0;
46130 continue; /* go back to the do {} while start point, try again */
46131 }
46132 }
46133 if( rc==SQLITE_OK0 ){
46134 /* Need to make a copy of path if we extracted the value
46135 ** from the conch file or the path was allocated on the stack
46136 */
46137 if( tempLockPath ){
46138 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
46139 if( !pCtx->lockProxyPath ){
46140 rc = SQLITE_NOMEM_BKPT7;
46141 }
46142 }
46143 }
46144 if( rc==SQLITE_OK0 ){
46145 pCtx->conchHeld = 1;
46146
46147 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
46148 afpLockingContext *afpCtx;
46149 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
46150 afpCtx->dbPath = pCtx->lockProxyPath;
46151 }
46152 } else {
46153 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
46154 }
46155 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
46156 rc==SQLITE_OK?"ok":"failed"));
46157 return rc;
46158 } while (1); /* in case we need to retry the :auto: lock file -
46159 ** we should never get here except via the 'continue' call. */
46160 }
46161}
46162
46163/*
46164** If pFile holds a lock on a conch file, then release that lock.
46165*/
46166static int proxyReleaseConch(unixFile *pFile){
46167 int rc = SQLITE_OK0; /* Subroutine return code */
46168 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
46169 unixFile *conchFile; /* Name of the conch file */
46170
46171 pCtx = (proxyLockingContext *)pFile->lockingContext;
46172 conchFile = pCtx->conchFile;
46173 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
46174 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
46175 osGetpid(0)));
46176 if( pCtx->conchHeld>0 ){
46177 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
46178 }
46179 pCtx->conchHeld = 0;
46180 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
46181 (rc==SQLITE_OK ? "ok" : "failed")));
46182 return rc;
46183}
46184
46185/*
46186** Given the name of a database file, compute the name of its conch file.
46187** Store the conch filename in memory obtained from sqlite3_malloc64().
46188** Make *pConchPath point to the new name. Return SQLITE_OK on success
46189** or SQLITE_NOMEM if unable to obtain memory.
46190**
46191** The caller is responsible for ensuring that the allocated memory
46192** space is eventually freed.
46193**
46194** *pConchPath is set to NULL if a memory allocation error occurs.
46195*/
46196static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
46197 int i; /* Loop counter */
46198 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
46199 char *conchPath; /* buffer in which to construct conch name */
46200
46201 /* Allocate space for the conch filename and initialize the name to
46202 ** the name of the original database file. */
46203 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
46204 if( conchPath==0 ){
46205 return SQLITE_NOMEM_BKPT7;
46206 }
46207 memcpy(conchPath, dbPath, len+1);
46208
46209 /* now insert a "." before the last / character */
46210 for( i=(len-1); i>=0; i-- ){
46211 if( conchPath[i]=='/' ){
46212 i++;
46213 break;
46214 }
46215 }
46216 conchPath[i]='.';
46217 while ( i<len ){
46218 conchPath[i+1]=dbPath[i];
46219 i++;
46220 }
46221
46222 /* append the "-conch" suffix to the file */
46223 memcpy(&conchPath[i+1], "-conch", 7);
46224 assert( (int)strlen(conchPath) == len+7 )((void) (0));
46225
46226 return SQLITE_OK0;
46227}
46228
46229
46230/* Takes a fully configured proxy locking-style unix file and switches
46231** the local lock file path
46232*/
46233static int switchLockProxyPath(unixFile *pFile, const char *path) {
46234 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46235 char *oldPath = pCtx->lockProxyPath;
46236 int rc = SQLITE_OK0;
46237
46238 if( pFile->eFileLock!=NO_LOCK0 ){
46239 return SQLITE_BUSY5;
46240 }
46241
46242 /* nothing to do if the path is NULL, :auto: or matches the existing path */
46243 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
46244 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
46245 return SQLITE_OK0;
46246 }else{
46247 unixFile *lockProxy = pCtx->lockProxy;
46248 pCtx->lockProxy=NULL((void*)0);
46249 pCtx->conchHeld = 0;
46250 if( lockProxy!=NULL((void*)0) ){
46251 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
46252 if( rc ) return rc;
46253 sqlite3_free(lockProxy);
46254 }
46255 sqlite3_free(oldPath);
46256 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
46257 }
46258
46259 return rc;
46260}
46261
46262/*
46263** pFile is a file that has been opened by a prior xOpen call. dbPath
46264** is a string buffer at least MAXPATHLEN+1 characters in size.
46265**
46266** This routine find the filename associated with pFile and writes it
46267** int dbPath.
46268*/
46269static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
46270#if defined(__APPLE__)
46271 if( pFile->pMethod == &afpIoMethods ){
46272 /* afp style keeps a reference to the db path in the filePath field
46273 ** of the struct */
46274 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
46275 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
46276 MAXPATHLEN);
46277 } else
46278#endif
46279 if( pFile->pMethod == &dotlockIoMethods ){
46280 /* dot lock style uses the locking context to store the dot lock
46281 ** file path */
46282 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX".lock");
46283 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
46284 }else{
46285 /* all other styles use the locking context to store the db file path */
46286 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
46287 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
46288 }
46289 return SQLITE_OK0;
46290}
46291
46292/*
46293** Takes an already filled in unix file and alters it so all file locking
46294** will be performed on the local proxy lock file. The following fields
46295** are preserved in the locking context so that they can be restored and
46296** the unix structure properly cleaned up at close time:
46297** ->lockingContext
46298** ->pMethod
46299*/
46300static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
46301 proxyLockingContext *pCtx;
46302 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
46303 char *lockPath=NULL((void*)0);
46304 int rc = SQLITE_OK0;
46305
46306 if( pFile->eFileLock!=NO_LOCK0 ){
46307 return SQLITE_BUSY5;
46308 }
46309 proxyGetDbPathForUnixFile(pFile, dbPath);
46310 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
46311 lockPath=NULL((void*)0);
46312 }else{
46313 lockPath=(char *)path;
46314 }
46315
46316 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
46317 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
46318
46319 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
46320 if( pCtx==0 ){
46321 return SQLITE_NOMEM_BKPT7;
46322 }
46323 memset(pCtx, 0, sizeof(*pCtx));
46324
46325 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
46326 if( rc==SQLITE_OK0 ){
46327 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
46328 if( rc==SQLITE_CANTOPEN14 && ((pFile->openFlags&O_RDWR02) == 0) ){
46329 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
46330 ** (c) the file system is read-only, then enable no-locking access.
46331 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
46332 ** that openFlags will have only one of O_RDONLY or O_RDWR.
46333 */
46334 struct statfs fsInfo;
46335 struct stat conchInfo;
46336 int goLockless = 0;
46337
46338 if( osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pCtx->conchFilePath, &conchInfo) == -1 ) {
46339 int err = errno(*__errno_location ());
46340 if( (err==ENOENT2) && (statfs(dbPath, &fsInfo) != -1) ){
46341 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
46342 }
46343 }
46344 if( goLockless ){
46345 pCtx->conchHeld = -1; /* read only FS/ lockless */
46346 rc = SQLITE_OK0;
46347 }
46348 }
46349 }
46350 if( rc==SQLITE_OK0 && lockPath ){
46351 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
46352 }
46353
46354 if( rc==SQLITE_OK0 ){
46355 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
46356 if( pCtx->dbPath==NULL((void*)0) ){
46357 rc = SQLITE_NOMEM_BKPT7;
46358 }
46359 }
46360 if( rc==SQLITE_OK0 ){
46361 /* all memory is allocated, proxys are created and assigned,
46362 ** switch the locking context and pMethod then return.
46363 */
46364 pCtx->oldLockingContext = pFile->lockingContext;
46365 pFile->lockingContext = pCtx;
46366 pCtx->pOldMethod = pFile->pMethod;
46367 pFile->pMethod = &proxyIoMethods;
46368 }else{
46369 if( pCtx->conchFile ){
46370 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
46371 sqlite3_free(pCtx->conchFile);
46372 }
46373 sqlite3DbFree(0, pCtx->lockProxyPath);
46374 sqlite3_free(pCtx->conchFilePath);
46375 sqlite3_free(pCtx);
46376 }
46377 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
46378 (rc==SQLITE_OK ? "ok" : "failed")));
46379 return rc;
46380}
46381
46382
46383/*
46384** This routine handles sqlite3_file_control() calls that are specific
46385** to proxy locking.
46386*/
46387static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
46388 switch( op ){
46389 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
46390 unixFile *pFile = (unixFile*)id;
46391 if( pFile->pMethod == &proxyIoMethods ){
46392 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46393 proxyTakeConch(pFile);
46394 if( pCtx->lockProxyPath ){
46395 *(const char **)pArg = pCtx->lockProxyPath;
46396 }else{
46397 *(const char **)pArg = ":auto: (not held)";
46398 }
46399 } else {
46400 *(const char **)pArg = NULL((void*)0);
46401 }
46402 return SQLITE_OK0;
46403 }
46404 case SQLITE_FCNTL_SET_LOCKPROXYFILE3: {
46405 unixFile *pFile = (unixFile*)id;
46406 int rc = SQLITE_OK0;
46407 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
46408 if( pArg==NULL((void*)0) || (const char *)pArg==0 ){
46409 if( isProxyStyle ){
46410 /* turn off proxy locking - not supported. If support is added for
46411 ** switching proxy locking mode off then it will need to fail if
46412 ** the journal mode is WAL mode.
46413 */
46414 rc = SQLITE_ERROR1 /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
46415 }else{
46416 /* turn off proxy locking - already off - NOOP */
46417 rc = SQLITE_OK0;
46418 }
46419 }else{
46420 const char *proxyPath = (const char *)pArg;
46421 if( isProxyStyle ){
46422 proxyLockingContext *pCtx =
46423 (proxyLockingContext*)pFile->lockingContext;
46424 if( !strcmp(pArg, ":auto:")
46425 || (pCtx->lockProxyPath &&
46426 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
46427 ){
46428 rc = SQLITE_OK0;
46429 }else{
46430 rc = switchLockProxyPath(pFile, proxyPath);
46431 }
46432 }else{
46433 /* turn on proxy file locking */
46434 rc = proxyTransformUnixFile(pFile, proxyPath);
46435 }
46436 }
46437 return rc;
46438 }
46439 default: {
46440 assert( 0 )((void) (0)); /* The call assures that only valid opcodes are sent */
46441 }
46442 }
46443 /*NOTREACHED*/ assert(0)((void) (0));
46444 return SQLITE_ERROR1;
46445}
46446
46447/*
46448** Within this division (the proxying locking implementation) the procedures
46449** above this point are all utilities. The lock-related methods of the
46450** proxy-locking sqlite3_io_method object follow.
46451*/
46452
46453
46454/*
46455** This routine checks if there is a RESERVED lock held on the specified
46456** file by this or any other process. If such a lock is held, set *pResOut
46457** to a non-zero value otherwise *pResOut is set to zero. The return value
46458** is set to SQLITE_OK unless an I/O error occurs during lock checking.
46459*/
46460static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
46461 unixFile *pFile = (unixFile*)id;
46462 int rc = proxyTakeConch(pFile);
46463 if( rc==SQLITE_OK0 ){
46464 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46465 if( pCtx->conchHeld>0 ){
46466 unixFile *proxy = pCtx->lockProxy;
46467 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
46468 }else{ /* conchHeld < 0 is lockless */
46469 pResOut=0;
46470 }
46471 }
46472 return rc;
46473}
46474
46475/*
46476** Lock the file with the lock specified by parameter eFileLock - one
46477** of the following:
46478**
46479** (1) SHARED_LOCK
46480** (2) RESERVED_LOCK
46481** (3) PENDING_LOCK
46482** (4) EXCLUSIVE_LOCK
46483**
46484** Sometimes when requesting one lock state, additional lock states
46485** are inserted in between. The locking might fail on one of the later
46486** transitions leaving the lock state different from what it started but
46487** still short of its goal. The following chart shows the allowed
46488** transitions and the inserted intermediate states:
46489**
46490** UNLOCKED -> SHARED
46491** SHARED -> RESERVED
46492** SHARED -> (PENDING) -> EXCLUSIVE
46493** RESERVED -> (PENDING) -> EXCLUSIVE
46494** PENDING -> EXCLUSIVE
46495**
46496** This routine will only increase a lock. Use the sqlite3OsUnlock()
46497** routine to lower a locking level.
46498*/
46499static int proxyLock(sqlite3_file *id, int eFileLock) {
46500 unixFile *pFile = (unixFile*)id;
46501 int rc = proxyTakeConch(pFile);
46502 if( rc==SQLITE_OK0 ){
46503 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46504 if( pCtx->conchHeld>0 ){
46505 unixFile *proxy = pCtx->lockProxy;
46506 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
46507 pFile->eFileLock = proxy->eFileLock;
46508 }else{
46509 /* conchHeld < 0 is lockless */
46510 }
46511 }
46512 return rc;
46513}
46514
46515
46516/*
46517** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
46518** must be either NO_LOCK or SHARED_LOCK.
46519**
46520** If the locking level of the file descriptor is already at or below
46521** the requested locking level, this routine is a no-op.
46522*/
46523static int proxyUnlock(sqlite3_file *id, int eFileLock) {
46524 unixFile *pFile = (unixFile*)id;
46525 int rc = proxyTakeConch(pFile);
46526 if( rc==SQLITE_OK0 ){
46527 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46528 if( pCtx->conchHeld>0 ){
46529 unixFile *proxy = pCtx->lockProxy;
46530 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
46531 pFile->eFileLock = proxy->eFileLock;
46532 }else{
46533 /* conchHeld < 0 is lockless */
46534 }
46535 }
46536 return rc;
46537}
46538
46539/*
46540** Close a file that uses proxy locks.
46541*/
46542static int proxyClose(sqlite3_file *id) {
46543 if( ALWAYS(id)(id) ){
46544 unixFile *pFile = (unixFile*)id;
46545 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46546 unixFile *lockProxy = pCtx->lockProxy;
46547 unixFile *conchFile = pCtx->conchFile;
46548 int rc = SQLITE_OK0;
46549
46550 if( lockProxy ){
46551 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK0);
46552 if( rc ) return rc;
46553 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
46554 if( rc ) return rc;
46555 sqlite3_free(lockProxy);
46556 pCtx->lockProxy = 0;
46557 }
46558 if( conchFile ){
46559 if( pCtx->conchHeld ){
46560 rc = proxyReleaseConch(pFile);
46561 if( rc ) return rc;
46562 }
46563 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
46564 if( rc ) return rc;
46565 sqlite3_free(conchFile);
46566 }
46567 sqlite3DbFree(0, pCtx->lockProxyPath);
46568 sqlite3_free(pCtx->conchFilePath);
46569 sqlite3DbFree(0, pCtx->dbPath);
46570 /* restore the original locking context and pMethod then close it */
46571 pFile->lockingContext = pCtx->oldLockingContext;
46572 pFile->pMethod = pCtx->pOldMethod;
46573 sqlite3_free(pCtx);
46574 return pFile->pMethod->xClose(id);
46575 }
46576 return SQLITE_OK0;
46577}
46578
46579
46580
46581#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
46582/*
46583** The proxy locking style is intended for use with AFP filesystems.
46584** And since AFP is only supported on MacOSX, the proxy locking is also
46585** restricted to MacOSX.
46586**
46587**
46588******************* End of the proxy lock implementation **********************
46589******************************************************************************/
46590
46591/*
46592** Initialize the operating system interface.
46593**
46594** This routine registers all VFS implementations for unix-like operating
46595** systems. This routine, and the sqlite3_os_end() routine that follows,
46596** should be the only routines in this file that are visible from other
46597** files.
46598**
46599** This routine is called once during SQLite initialization and by a
46600** single thread. The memory allocation and mutex subsystems have not
46601** necessarily been initialized when this routine is called, and so they
46602** should not be used.
46603*/
46604SQLITE_API int sqlite3_os_init(void){
46605 /*
46606 ** The following macro defines an initializer for an sqlite3_vfs object.
46607 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
46608 ** to the "finder" function. (pAppData is a pointer to a pointer because
46609 ** silly C90 rules prohibit a void* from being cast to a function pointer
46610 ** and so we have to go through the intermediate pointer to avoid problems
46611 ** when compiling with -pedantic-errors on GCC.)
46612 **
46613 ** The FINDER parameter to this macro is the name of the pointer to the
46614 ** finder-function. The finder-function returns a pointer to the
46615 ** sqlite_io_methods object that implements the desired locking
46616 ** behaviors. See the division above that contains the IOMETHODS
46617 ** macro for addition information on finder-functions.
46618 **
46619 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
46620 ** object. But the "autolockIoFinder" available on MacOSX does a little
46621 ** more than that; it looks at the filesystem type that hosts the
46622 ** database file and tries to choose an locking method appropriate for
46623 ** that filesystem time.
46624 */
46625 #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, }
{ \
46626 3, /* iVersion */ \
46627 sizeof(unixFile), /* szOsFile */ \
46628 MAX_PATHNAME512, /* mxPathname */ \
46629 0, /* pNext */ \
46630 VFSNAME, /* zName */ \
46631 (void*)&FINDER, /* pAppData */ \
46632 unixOpen, /* xOpen */ \
46633 unixDelete, /* xDelete */ \
46634 unixAccess, /* xAccess */ \
46635 unixFullPathname, /* xFullPathname */ \
46636 unixDlOpen0, /* xDlOpen */ \
46637 unixDlError0, /* xDlError */ \
46638 unixDlSym0, /* xDlSym */ \
46639 unixDlClose0, /* xDlClose */ \
46640 unixRandomness, /* xRandomness */ \
46641 unixSleep, /* xSleep */ \
46642 unixCurrentTime0, /* xCurrentTime */ \
46643 unixGetLastError, /* xGetLastError */ \
46644 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
46645 unixSetSystemCall, /* xSetSystemCall */ \
46646 unixGetSystemCall, /* xGetSystemCall */ \
46647 unixNextSystemCall, /* xNextSystemCall */ \
46648 }
46649
46650 /*
46651 ** All default VFSes for unix are contained in the following array.
46652 **
46653 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
46654 ** by the SQLite core when the VFS is registered. So the following
46655 ** array cannot be const.
46656 */
46657 static sqlite3_vfs aVfs[] = {
46658#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
46659 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, }
,
46660#elif OS_VXWORKS0
46661 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, }
,
46662#else
46663 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, }
,
46664#endif
46665 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, }
,
46666 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, }
,
46667 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, }
,
46668#if OS_VXWORKS0
46669 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, }
,
46670#endif
46671#if SQLITE_ENABLE_LOCKING_STYLE0 || OS_VXWORKS0
46672 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, }
,
46673#endif
46674#if SQLITE_ENABLE_LOCKING_STYLE0
46675 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, }
,
46676#endif
46677#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
46678 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, }
,
46679 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, }
,
46680 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, }
,
46681#endif
46682 };
46683 unsigned int i; /* Loop counter */
46684
46685 /* Double-check that the aSyscall[] array has been constructed
46686 ** correctly. See ticket [bb3a86e890c8e96ab] */
46687 assert( ArraySize(aSyscall)==29 )((void) (0));
46688
46689 /* Register all VFSes defined in the aVfs[] array */
46690 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
46691#ifdef SQLITE_DEFAULT_UNIX_VFS
46692 sqlite3_vfs_register(&aVfs[i],
46693 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
46694#else
46695 sqlite3_vfs_register(&aVfs[i], i==0);
46696#endif
46697 }
46698#ifdef SQLITE_OS_KV_OPTIONAL
46699 sqlite3KvvfsInit();
46700#endif
46701 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
46702
46703#ifndef SQLITE_OMIT_WAL
46704 /* Validate lock assumptions */
46705 assert( SQLITE_SHM_NLOCK==8 )((void) (0)); /* Number of available locks */
46706 assert( UNIX_SHM_BASE==120 )((void) (0)); /* Start of locking area */
46707 /* Locks:
46708 ** WRITE UNIX_SHM_BASE 120
46709 ** CKPT UNIX_SHM_BASE+1 121
46710 ** RECOVER UNIX_SHM_BASE+2 122
46711 ** READ-0 UNIX_SHM_BASE+3 123
46712 ** READ-1 UNIX_SHM_BASE+4 124
46713 ** READ-2 UNIX_SHM_BASE+5 125
46714 ** READ-3 UNIX_SHM_BASE+6 126
46715 ** READ-4 UNIX_SHM_BASE+7 127
46716 ** DMS UNIX_SHM_BASE+8 128
46717 */
46718 assert( UNIX_SHM_DMS==128 )((void) (0)); /* Byte offset of the deadman-switch */
46719#endif
46720
46721 /* Initialize temp file dir array. */
46722 unixTempFileInit();
46723
46724 return SQLITE_OK0;
46725}
46726
46727/*
46728** Shutdown the operating system interface.
46729**
46730** Some operating systems might need to do some cleanup in this routine,
46731** to release dynamically allocated objects. But not on unix.
46732** This routine is a no-op for unix.
46733*/
46734SQLITE_API int sqlite3_os_end(void){
46735 unixBigLock = 0;
46736 return SQLITE_OK0;
46737}
46738
46739#endif /* SQLITE_OS_UNIX */
46740
46741/************** End of os_unix.c *********************************************/
46742/************** Begin file os_win.c ******************************************/
46743/*
46744** 2004 May 22
46745**
46746** The author disclaims copyright to this source code. In place of
46747** a legal notice, here is a blessing:
46748**
46749** May you do good and not evil.
46750** May you find forgiveness for yourself and forgive others.
46751** May you share freely, never taking more than you give.
46752**
46753******************************************************************************
46754**
46755** This file contains code that is specific to Windows.
46756*/
46757/* #include "sqliteInt.h" */
46758#if SQLITE_OS_WIN0 /* This file is used for Windows only */
46759
46760/*
46761** Include code that is common to all os_*.c files
46762*/
46763/* #include "os_common.h" */
46764
46765/*
46766** Include the header file for the Windows VFS.
46767*/
46768/* #include "os_win.h" */
46769
46770/*
46771** Compiling and using WAL mode requires several APIs that are only
46772** available in Windows platforms based on the NT kernel.
46773*/
46774#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
46775# error "WAL mode requires support from the Windows NT kernel, compile\
46776 with SQLITE_OMIT_WAL."
46777#endif
46778
46779#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE20971520>0
46780# error "Memory mapped files require support from the Windows NT kernel,\
46781 compile with SQLITE_MAX_MMAP_SIZE=0."
46782#endif
46783
46784/*
46785** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
46786** based on the sub-platform)?
46787*/
46788#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
46789# define SQLITE_WIN32_HAS_ANSI
46790#endif
46791
46792/*
46793** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
46794** based on the sub-platform)?
46795*/
46796#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
46797 !defined(SQLITE_WIN32_NO_WIDE)
46798# define SQLITE_WIN32_HAS_WIDE
46799#endif
46800
46801/*
46802** Make sure at least one set of Win32 APIs is available.
46803*/
46804#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
46805# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
46806 must be defined."
46807#endif
46808
46809/*
46810** Define the required Windows SDK version constants if they are not
46811** already available.
46812*/
46813#ifndef NTDDI_WIN8
46814# define NTDDI_WIN8 0x06020000
46815#endif
46816
46817#ifndef NTDDI_WINBLUE
46818# define NTDDI_WINBLUE 0x06030000
46819#endif
46820
46821#ifndef NTDDI_WINTHRESHOLD
46822# define NTDDI_WINTHRESHOLD 0x06040000
46823#endif
46824
46825/*
46826** Check to see if the GetVersionEx[AW] functions are deprecated on the
46827** target system. GetVersionEx was first deprecated in Win8.1.
46828*/
46829#ifndef SQLITE_WIN32_GETVERSIONEX
46830# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
46831# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
46832# else
46833# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
46834# endif
46835#endif
46836
46837/*
46838** Check to see if the CreateFileMappingA function is supported on the
46839** target system. It is unavailable when using "mincore.lib" on Win10.
46840** When compiling for Windows 10, always assume "mincore.lib" is in use.
46841*/
46842#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
46843# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
46844# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
46845# else
46846# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
46847# endif
46848#endif
46849
46850/*
46851** This constant should already be defined (in the "WinDef.h" SDK file).
46852*/
46853#ifndef MAX_PATH
46854# define MAX_PATH (260)
46855#endif
46856
46857/*
46858** Maximum pathname length (in chars) for Win32. This should normally be
46859** MAX_PATH.
46860*/
46861#ifndef SQLITE_WIN32_MAX_PATH_CHARS
46862# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
46863#endif
46864
46865/*
46866** This constant should already be defined (in the "WinNT.h" SDK file).
46867*/
46868#ifndef UNICODE_STRING_MAX_CHARS
46869# define UNICODE_STRING_MAX_CHARS (32767)
46870#endif
46871
46872/*
46873** Maximum pathname length (in chars) for WinNT. This should normally be
46874** UNICODE_STRING_MAX_CHARS.
46875*/
46876#ifndef SQLITE_WINNT_MAX_PATH_CHARS
46877# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
46878#endif
46879
46880/*
46881** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
46882** characters, so we allocate 4 bytes per character assuming worst-case of
46883** 4-bytes-per-character for UTF8.
46884*/
46885#ifndef SQLITE_WIN32_MAX_PATH_BYTES
46886# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
46887#endif
46888
46889/*
46890** Maximum pathname length (in bytes) for WinNT. This should normally be
46891** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
46892*/
46893#ifndef SQLITE_WINNT_MAX_PATH_BYTES
46894# define SQLITE_WINNT_MAX_PATH_BYTES \
46895 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
46896#endif
46897
46898/*
46899** Maximum error message length (in chars) for WinRT.
46900*/
46901#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
46902# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
46903#endif
46904
46905/*
46906** Returns non-zero if the character should be treated as a directory
46907** separator.
46908*/
46909#ifndef winIsDirSep
46910# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
46911#endif
46912
46913/*
46914** This macro is used when a local variable is set to a value that is
46915** [sometimes] not used by the code (e.g. via conditional compilation).
46916*/
46917#ifndef UNUSED_VARIABLE_VALUE
46918# define UNUSED_VARIABLE_VALUE(x) (void)(x)
46919#endif
46920
46921/*
46922** Returns the character that should be used as the directory separator.
46923*/
46924#ifndef winGetDirSep
46925# define winGetDirSep() '\\'
46926#endif
46927
46928/*
46929** Do we need to manually define the Win32 file mapping APIs for use with WAL
46930** mode or memory mapped files (e.g. these APIs are available in the Windows
46931** CE SDK; however, they are not present in the header file)?
46932*/
46933#if SQLITE_WIN32_FILEMAPPING_API && \
46934 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
46935/*
46936** Two of the file mapping APIs are different under WinRT. Figure out which
46937** set we need.
46938*/
46939#if SQLITE_OS_WINRT
46940WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
46941 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
46942
46943WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
46944#else
46945#if defined(SQLITE_WIN32_HAS_ANSI)
46946WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
46947 DWORD, DWORD, DWORD, LPCSTR);
46948#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
46949
46950#if defined(SQLITE_WIN32_HAS_WIDE)
46951WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
46952 DWORD, DWORD, DWORD, LPCWSTR);
46953#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
46954
46955WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
46956#endif /* SQLITE_OS_WINRT */
46957
46958/*
46959** These file mapping APIs are common to both Win32 and WinRT.
46960*/
46961
46962WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
46963WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
46964#endif /* SQLITE_WIN32_FILEMAPPING_API */
46965
46966/*
46967** Some Microsoft compilers lack this definition.
46968*/
46969#ifndef INVALID_FILE_ATTRIBUTES
46970# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
46971#endif
46972
46973#ifndef FILE_FLAG_MASK
46974# define FILE_FLAG_MASK (0xFF3C0000)
46975#endif
46976
46977#ifndef FILE_ATTRIBUTE_MASK
46978# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
46979#endif
46980
46981#ifndef SQLITE_OMIT_WAL
46982/* Forward references to structures used for WAL */
46983typedef struct winShm winShm; /* A connection to shared-memory */
46984typedef struct winShmNode winShmNode; /* A region of shared-memory */
46985#endif
46986
46987/*
46988** WinCE lacks native support for file locking so we have to fake it
46989** with some code of our own.
46990*/
46991#if SQLITE_OS_WINCE
46992typedef struct winceLock {
46993 int nReaders; /* Number of reader locks obtained */
46994 BOOL bPending; /* Indicates a pending lock has been obtained */
46995 BOOL bReserved; /* Indicates a reserved lock has been obtained */
46996 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
46997} winceLock;
46998#endif
46999
47000/*
47001** The winFile structure is a subclass of sqlite3_file* specific to the win32
47002** portability layer.
47003*/
47004typedef struct winFile winFile;
47005struct winFile {
47006 const sqlite3_io_methods *pMethod; /*** Must be first ***/
47007 sqlite3_vfs *pVfs; /* The VFS used to open this file */
47008 HANDLE h; /* Handle for accessing the file */
47009 u8 locktype; /* Type of lock currently held on this file */
47010 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
47011 u8 ctrlFlags; /* Flags. See WINFILE_* below */
47012 DWORD lastErrno; /* The Windows errno from the last I/O error */
47013#ifndef SQLITE_OMIT_WAL
47014 winShm *pShm; /* Instance of shared memory on this file */
47015#endif
47016 const char *zPath; /* Full pathname of this file */
47017 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
47018#if SQLITE_OS_WINCE
47019 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
47020 HANDLE hMutex; /* Mutex used to control access to shared lock */
47021 HANDLE hShared; /* Shared memory segment used for locking */
47022 winceLock local; /* Locks obtained by this instance of winFile */
47023 winceLock *shared; /* Global shared lock memory for the file */
47024#endif
47025#if SQLITE_MAX_MMAP_SIZE20971520>0
47026 int nFetchOut; /* Number of outstanding xFetch references */
47027 HANDLE hMap; /* Handle for accessing memory mapping */
47028 void *pMapRegion; /* Area memory mapped */
47029 sqlite3_int64 mmapSize; /* Size of mapped region */
47030 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
47031#endif
47032};
47033
47034/*
47035** The winVfsAppData structure is used for the pAppData member for all of the
47036** Win32 VFS variants.
47037*/
47038typedef struct winVfsAppData winVfsAppData;
47039struct winVfsAppData {
47040 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
47041 void *pAppData; /* The extra pAppData, if any. */
47042 BOOL bNoLock; /* Non-zero if locking is disabled. */
47043};
47044
47045/*
47046** Allowed values for winFile.ctrlFlags
47047*/
47048#define WINFILE_RDONLY 0x02 /* Connection is read only */
47049#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
47050#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
47051
47052/*
47053 * The size of the buffer used by sqlite3_win32_write_debug().
47054 */
47055#ifndef SQLITE_WIN32_DBG_BUF_SIZE
47056# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
47057#endif
47058
47059/*
47060 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
47061 * various Win32 API heap functions instead of our own.
47062 */
47063#ifdef SQLITE_WIN32_MALLOC
47064
47065/*
47066 * If this is non-zero, an isolated heap will be created by the native Win32
47067 * allocator subsystem; otherwise, the default process heap will be used. This
47068 * setting has no effect when compiling for WinRT. By default, this is enabled
47069 * and an isolated heap will be created to store all allocated data.
47070 *
47071 ******************************************************************************
47072 * WARNING: It is important to note that when this setting is non-zero and the
47073 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
47074 * function), all data that was allocated using the isolated heap will
47075 * be freed immediately and any attempt to access any of that freed
47076 * data will almost certainly result in an immediate access violation.
47077 ******************************************************************************
47078 */
47079#ifndef SQLITE_WIN32_HEAP_CREATE
47080# define SQLITE_WIN32_HEAP_CREATE (TRUE)
47081#endif
47082
47083/*
47084 * This is the maximum possible initial size of the Win32-specific heap, in
47085 * bytes.
47086 */
47087#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
47088# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
47089#endif
47090
47091/*
47092 * This is the extra space for the initial size of the Win32-specific heap,
47093 * in bytes. This value may be zero.
47094 */
47095#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
47096# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
47097#endif
47098
47099/*
47100 * Calculate the maximum legal cache size, in pages, based on the maximum
47101 * possible initial heap size and the default page size, setting aside the
47102 * needed extra space.
47103 */
47104#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
47105# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
47106 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
47107 (SQLITE_DEFAULT_PAGE_SIZE4096))
47108#endif
47109
47110/*
47111 * This is cache size used in the calculation of the initial size of the
47112 * Win32-specific heap. It cannot be negative.
47113 */
47114#ifndef SQLITE_WIN32_CACHE_SIZE
47115# if SQLITE_DEFAULT_CACHE_SIZE128>=0
47116# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE128)
47117# else
47118# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE128))
47119# endif
47120#endif
47121
47122/*
47123 * Make sure that the calculated cache size, in pages, cannot cause the
47124 * initial size of the Win32-specific heap to exceed the maximum amount
47125 * of memory that can be specified in the call to HeapCreate.
47126 */
47127#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
47128# undef SQLITE_WIN32_CACHE_SIZE
47129# define SQLITE_WIN32_CACHE_SIZE (2000)
47130#endif
47131
47132/*
47133 * The initial size of the Win32-specific heap. This value may be zero.
47134 */
47135#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
47136# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
47137 (SQLITE_DEFAULT_PAGE_SIZE4096) + \
47138 (SQLITE_WIN32_HEAP_INIT_EXTRA))
47139#endif
47140
47141/*
47142 * The maximum size of the Win32-specific heap. This value may be zero.
47143 */
47144#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
47145# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
47146#endif
47147
47148/*
47149 * The extra flags to use in calls to the Win32 heap APIs. This value may be
47150 * zero for the default behavior.
47151 */
47152#ifndef SQLITE_WIN32_HEAP_FLAGS
47153# define SQLITE_WIN32_HEAP_FLAGS (0)
47154#endif
47155
47156
47157/*
47158** The winMemData structure stores information required by the Win32-specific
47159** sqlite3_mem_methods implementation.
47160*/
47161typedef struct winMemData winMemData;
47162struct winMemData {
47163#ifndef NDEBUG1
47164 u32 magic1; /* Magic number to detect structure corruption. */
47165#endif
47166 HANDLE hHeap; /* The handle to our heap. */
47167 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
47168#ifndef NDEBUG1
47169 u32 magic2; /* Magic number to detect structure corruption. */
47170#endif
47171};
47172
47173#ifndef NDEBUG1
47174#define WINMEM_MAGIC1 0x42b2830b
47175#define WINMEM_MAGIC2 0xbd4d7cf4
47176#endif
47177
47178static struct winMemData win_mem_data = {
47179#ifndef NDEBUG1
47180 WINMEM_MAGIC1,
47181#endif
47182 NULL((void*)0), FALSE
47183#ifndef NDEBUG1
47184 ,WINMEM_MAGIC2
47185#endif
47186};
47187
47188#ifndef NDEBUG1
47189#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )((void) (0))
47190#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )((void) (0))
47191#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
47192#else
47193#define winMemAssertMagic()
47194#endif
47195
47196#define winMemGetDataPtr() &win_mem_data
47197#define winMemGetHeap() win_mem_data.hHeap
47198#define winMemGetOwned() win_mem_data.bOwned
47199
47200static void *winMemMalloc(int nBytes);
47201static void winMemFree(void *pPrior);
47202static void *winMemRealloc(void *pPrior, int nBytes);
47203static int winMemSize(void *p);
47204static int winMemRoundup(int n);
47205static int winMemInit(void *pAppData);
47206static void winMemShutdown(void *pAppData);
47207
47208SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void);
47209#endif /* SQLITE_WIN32_MALLOC */
47210
47211/*
47212** The following variable is (normally) set once and never changes
47213** thereafter. It records whether the operating system is Win9x
47214** or WinNT.
47215**
47216** 0: Operating system unknown.
47217** 1: Operating system is Win9x.
47218** 2: Operating system is WinNT.
47219**
47220** In order to facilitate testing on a WinNT system, the test fixture
47221** can manually set this value to 1 to emulate Win98 behavior.
47222*/
47223#ifdef SQLITE_TEST
47224SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
47225#else
47226static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
47227#endif
47228
47229#ifndef SYSCALL
47230# define SYSCALL sqlite3_syscall_ptr
47231#endif
47232
47233/*
47234** This function is not available on Windows CE or WinRT.
47235 */
47236
47237#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
47238# define osAreFileApisANSI() 1
47239#endif
47240
47241/*
47242** Many system calls are accessed through pointer-to-functions so that
47243** they may be overridden at runtime to facilitate fault injection during
47244** testing and sandboxing. The following array holds the names and pointers
47245** to all overrideable system calls.
47246*/
47247static struct win_syscall {
47248 const char *zName; /* Name of the system call */
47249 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
47250 sqlite3_syscall_ptr pDefault; /* Default value */
47251} aSyscall[] = {
47252#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47253 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
47254#else
47255 { "AreFileApisANSI", (SYSCALL)0, 0 },
47256#endif
47257
47258#ifndef osAreFileApisANSI
47259#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
47260#endif
47261
47262#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
47263 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
47264#else
47265 { "CharLowerW", (SYSCALL)0, 0 },
47266#endif
47267
47268#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
47269
47270#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
47271 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
47272#else
47273 { "CharUpperW", (SYSCALL)0, 0 },
47274#endif
47275
47276#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
47277
47278 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
47279
47280#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
47281
47282#if defined(SQLITE_WIN32_HAS_ANSI)
47283 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
47284#else
47285 { "CreateFileA", (SYSCALL)0, 0 },
47286#endif
47287
47288#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
47289 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
47290
47291#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47292 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
47293#else
47294 { "CreateFileW", (SYSCALL)0, 0 },
47295#endif
47296
47297#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
47298 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
47299
47300#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
47301 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0) && \
47302 SQLITE_WIN32_CREATEFILEMAPPINGA
47303 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
47304#else
47305 { "CreateFileMappingA", (SYSCALL)0, 0 },
47306#endif
47307
47308#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
47309 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
47310
47311#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
47312 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0))
47313 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
47314#else
47315 { "CreateFileMappingW", (SYSCALL)0, 0 },
47316#endif
47317
47318#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
47319 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
47320
47321#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47322 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
47323#else
47324 { "CreateMutexW", (SYSCALL)0, 0 },
47325#endif
47326
47327#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
47328 LPCWSTR))aSyscall[8].pCurrent)
47329
47330#if defined(SQLITE_WIN32_HAS_ANSI)
47331 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
47332#else
47333 { "DeleteFileA", (SYSCALL)0, 0 },
47334#endif
47335
47336#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
47337
47338#if defined(SQLITE_WIN32_HAS_WIDE)
47339 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
47340#else
47341 { "DeleteFileW", (SYSCALL)0, 0 },
47342#endif
47343
47344#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
47345
47346#if SQLITE_OS_WINCE
47347 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
47348#else
47349 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
47350#endif
47351
47352#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
47353 LPFILETIME))aSyscall[11].pCurrent)
47354
47355#if SQLITE_OS_WINCE
47356 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
47357#else
47358 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
47359#endif
47360
47361#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
47362 LPSYSTEMTIME))aSyscall[12].pCurrent)
47363
47364 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
47365
47366#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
47367
47368#if defined(SQLITE_WIN32_HAS_ANSI)
47369 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
47370#else
47371 { "FormatMessageA", (SYSCALL)0, 0 },
47372#endif
47373
47374#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
47375 DWORD,va_list*))aSyscall[14].pCurrent)
47376
47377#if defined(SQLITE_WIN32_HAS_WIDE)
47378 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
47379#else
47380 { "FormatMessageW", (SYSCALL)0, 0 },
47381#endif
47382
47383#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
47384 DWORD,va_list*))aSyscall[15].pCurrent)
47385
47386#if !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47387 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
47388#else
47389 { "FreeLibrary", (SYSCALL)0, 0 },
47390#endif
47391
47392#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
47393
47394 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
47395
47396#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
47397
47398#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
47399 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
47400#else
47401 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
47402#endif
47403
47404#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
47405 LPDWORD))aSyscall[18].pCurrent)
47406
47407#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47408 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
47409#else
47410 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
47411#endif
47412
47413#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
47414 LPDWORD))aSyscall[19].pCurrent)
47415
47416#if defined(SQLITE_WIN32_HAS_ANSI)
47417 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
47418#else
47419 { "GetFileAttributesA", (SYSCALL)0, 0 },
47420#endif
47421
47422#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
47423
47424#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47425 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
47426#else
47427 { "GetFileAttributesW", (SYSCALL)0, 0 },
47428#endif
47429
47430#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
47431
47432#if defined(SQLITE_WIN32_HAS_WIDE)
47433 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
47434#else
47435 { "GetFileAttributesExW", (SYSCALL)0, 0 },
47436#endif
47437
47438#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
47439 LPVOID))aSyscall[22].pCurrent)
47440
47441#if !SQLITE_OS_WINRT
47442 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
47443#else
47444 { "GetFileSize", (SYSCALL)0, 0 },
47445#endif
47446
47447#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
47448
47449#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
47450 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
47451#else
47452 { "GetFullPathNameA", (SYSCALL)0, 0 },
47453#endif
47454
47455#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
47456 LPSTR*))aSyscall[24].pCurrent)
47457
47458#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47459 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
47460#else
47461 { "GetFullPathNameW", (SYSCALL)0, 0 },
47462#endif
47463
47464#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
47465 LPWSTR*))aSyscall[25].pCurrent)
47466
47467 { "GetLastError", (SYSCALL)GetLastError, 0 },
47468
47469#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
47470
47471#if !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47472#if SQLITE_OS_WINCE
47473 /* The GetProcAddressA() routine is only available on Windows CE. */
47474 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
47475#else
47476 /* All other Windows platforms expect GetProcAddress() to take
47477 ** an ANSI string regardless of the _UNICODE setting */
47478 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
47479#endif
47480#else
47481 { "GetProcAddressA", (SYSCALL)0, 0 },
47482#endif
47483
47484#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
47485 LPCSTR))aSyscall[27].pCurrent)
47486
47487#if !SQLITE_OS_WINRT
47488 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
47489#else
47490 { "GetSystemInfo", (SYSCALL)0, 0 },
47491#endif
47492
47493#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
47494
47495 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
47496
47497#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
47498
47499#if !SQLITE_OS_WINCE
47500 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
47501#else
47502 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
47503#endif
47504
47505#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
47506 LPFILETIME))aSyscall[30].pCurrent)
47507
47508#if defined(SQLITE_WIN32_HAS_ANSI)
47509 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
47510#else
47511 { "GetTempPathA", (SYSCALL)0, 0 },
47512#endif
47513
47514#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
47515
47516#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
47517 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
47518#else
47519 { "GetTempPathW", (SYSCALL)0, 0 },
47520#endif
47521
47522#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
47523
47524#if !SQLITE_OS_WINRT
47525 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
47526#else
47527 { "GetTickCount", (SYSCALL)0, 0 },
47528#endif
47529
47530#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
47531
47532#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
47533 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
47534#else
47535 { "GetVersionExA", (SYSCALL)0, 0 },
47536#endif
47537
47538#define osGetVersionExA ((BOOL(WINAPI*)( \
47539 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
47540
47541#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
47542 SQLITE_WIN32_GETVERSIONEX
47543 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
47544#else
47545 { "GetVersionExW", (SYSCALL)0, 0 },
47546#endif
47547
47548#define osGetVersionExW ((BOOL(WINAPI*)( \
47549 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
47550
47551 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
47552
47553#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
47554 SIZE_T))aSyscall[36].pCurrent)
47555
47556#if !SQLITE_OS_WINRT
47557 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
47558#else
47559 { "HeapCreate", (SYSCALL)0, 0 },
47560#endif
47561
47562#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
47563 SIZE_T))aSyscall[37].pCurrent)
47564
47565#if !SQLITE_OS_WINRT
47566 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
47567#else
47568 { "HeapDestroy", (SYSCALL)0, 0 },
47569#endif
47570
47571#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
47572
47573 { "HeapFree", (SYSCALL)HeapFree, 0 },
47574
47575#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
47576
47577 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
47578
47579#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
47580 SIZE_T))aSyscall[40].pCurrent)
47581
47582 { "HeapSize", (SYSCALL)HeapSize, 0 },
47583
47584#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
47585 LPCVOID))aSyscall[41].pCurrent)
47586
47587#if !SQLITE_OS_WINRT
47588 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
47589#else
47590 { "HeapValidate", (SYSCALL)0, 0 },
47591#endif
47592
47593#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
47594 LPCVOID))aSyscall[42].pCurrent)
47595
47596#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47597 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
47598#else
47599 { "HeapCompact", (SYSCALL)0, 0 },
47600#endif
47601
47602#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
47603
47604#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47605 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
47606#else
47607 { "LoadLibraryA", (SYSCALL)0, 0 },
47608#endif
47609
47610#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
47611
47612#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
47613 !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47614 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
47615#else
47616 { "LoadLibraryW", (SYSCALL)0, 0 },
47617#endif
47618
47619#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
47620
47621#if !SQLITE_OS_WINRT
47622 { "LocalFree", (SYSCALL)LocalFree, 0 },
47623#else
47624 { "LocalFree", (SYSCALL)0, 0 },
47625#endif
47626
47627#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
47628
47629#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47630 { "LockFile", (SYSCALL)LockFile, 0 },
47631#else
47632 { "LockFile", (SYSCALL)0, 0 },
47633#endif
47634
47635#ifndef osLockFile
47636#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
47637 DWORD))aSyscall[47].pCurrent)
47638#endif
47639
47640#if !SQLITE_OS_WINCE
47641 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
47642#else
47643 { "LockFileEx", (SYSCALL)0, 0 },
47644#endif
47645
47646#ifndef osLockFileEx
47647#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
47648 LPOVERLAPPED))aSyscall[48].pCurrent)
47649#endif
47650
47651#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
47652 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0))
47653 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
47654#else
47655 { "MapViewOfFile", (SYSCALL)0, 0 },
47656#endif
47657
47658#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
47659 SIZE_T))aSyscall[49].pCurrent)
47660
47661 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
47662
47663#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
47664 int))aSyscall[50].pCurrent)
47665
47666 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
47667
47668#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
47669 LARGE_INTEGER*))aSyscall[51].pCurrent)
47670
47671 { "ReadFile", (SYSCALL)ReadFile, 0 },
47672
47673#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
47674 LPOVERLAPPED))aSyscall[52].pCurrent)
47675
47676 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
47677
47678#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
47679
47680#if !SQLITE_OS_WINRT
47681 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
47682#else
47683 { "SetFilePointer", (SYSCALL)0, 0 },
47684#endif
47685
47686#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
47687 DWORD))aSyscall[54].pCurrent)
47688
47689#if !SQLITE_OS_WINRT
47690 { "Sleep", (SYSCALL)Sleep, 0 },
47691#else
47692 { "Sleep", (SYSCALL)0, 0 },
47693#endif
47694
47695#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
47696
47697 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
47698
47699#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
47700 LPFILETIME))aSyscall[56].pCurrent)
47701
47702#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
47703 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
47704#else
47705 { "UnlockFile", (SYSCALL)0, 0 },
47706#endif
47707
47708#ifndef osUnlockFile
47709#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
47710 DWORD))aSyscall[57].pCurrent)
47711#endif
47712
47713#if !SQLITE_OS_WINCE
47714 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
47715#else
47716 { "UnlockFileEx", (SYSCALL)0, 0 },
47717#endif
47718
47719#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
47720 LPOVERLAPPED))aSyscall[58].pCurrent)
47721
47722#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0
47723 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
47724#else
47725 { "UnmapViewOfFile", (SYSCALL)0, 0 },
47726#endif
47727
47728#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
47729
47730 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
47731
47732#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
47733 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
47734
47735 { "WriteFile", (SYSCALL)WriteFile, 0 },
47736
47737#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
47738 LPOVERLAPPED))aSyscall[61].pCurrent)
47739
47740#if SQLITE_OS_WINRT
47741 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
47742#else
47743 { "CreateEventExW", (SYSCALL)0, 0 },
47744#endif
47745
47746#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
47747 DWORD,DWORD))aSyscall[62].pCurrent)
47748
47749#if !SQLITE_OS_WINRT
47750 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
47751#else
47752 { "WaitForSingleObject", (SYSCALL)0, 0 },
47753#endif
47754
47755#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
47756 DWORD))aSyscall[63].pCurrent)
47757
47758#if !SQLITE_OS_WINCE
47759 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
47760#else
47761 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
47762#endif
47763
47764#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
47765 BOOL))aSyscall[64].pCurrent)
47766
47767#if SQLITE_OS_WINRT
47768 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
47769#else
47770 { "SetFilePointerEx", (SYSCALL)0, 0 },
47771#endif
47772
47773#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
47774 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
47775
47776#if SQLITE_OS_WINRT
47777 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
47778#else
47779 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
47780#endif
47781
47782#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
47783 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
47784
47785#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
47786 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
47787#else
47788 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
47789#endif
47790
47791#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
47792 SIZE_T))aSyscall[67].pCurrent)
47793
47794#if SQLITE_OS_WINRT
47795 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
47796#else
47797 { "CreateFile2", (SYSCALL)0, 0 },
47798#endif
47799
47800#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
47801 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
47802
47803#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
47804 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
47805#else
47806 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
47807#endif
47808
47809#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
47810 DWORD))aSyscall[69].pCurrent)
47811
47812#if SQLITE_OS_WINRT
47813 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
47814#else
47815 { "GetTickCount64", (SYSCALL)0, 0 },
47816#endif
47817
47818#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
47819
47820#if SQLITE_OS_WINRT
47821 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
47822#else
47823 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
47824#endif
47825
47826#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
47827 LPSYSTEM_INFO))aSyscall[71].pCurrent)
47828
47829#if defined(SQLITE_WIN32_HAS_ANSI)
47830 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
47831#else
47832 { "OutputDebugStringA", (SYSCALL)0, 0 },
47833#endif
47834
47835#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
47836
47837#if defined(SQLITE_WIN32_HAS_WIDE)
47838 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
47839#else
47840 { "OutputDebugStringW", (SYSCALL)0, 0 },
47841#endif
47842
47843#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
47844
47845 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
47846
47847#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
47848
47849#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE20971520>0)
47850 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
47851#else
47852 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
47853#endif
47854
47855#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
47856 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
47857
47858/*
47859** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
47860** is really just a macro that uses a compiler intrinsic (e.g. x64).
47861** So do not try to make this is into a redefinable interface.
47862*/
47863#if defined(InterlockedCompareExchange)
47864 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
47865
47866#define osInterlockedCompareExchange InterlockedCompareExchange
47867#else
47868 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
47869
47870#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
47871 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
47872#endif /* defined(InterlockedCompareExchange) */
47873
47874#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
47875 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
47876#else
47877 { "UuidCreate", (SYSCALL)0, 0 },
47878#endif
47879
47880#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
47881
47882#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
47883 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
47884#else
47885 { "UuidCreateSequential", (SYSCALL)0, 0 },
47886#endif
47887
47888#define osUuidCreateSequential \
47889 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
47890
47891#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE20971520>0
47892 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
47893#else
47894 { "FlushViewOfFile", (SYSCALL)0, 0 },
47895#endif
47896
47897#define osFlushViewOfFile \
47898 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
47899
47900}; /* End of the overrideable system calls */
47901
47902/*
47903** This is the xSetSystemCall() method of sqlite3_vfs for all of the
47904** "win32" VFSes. Return SQLITE_OK upon successfully updating the
47905** system call pointer, or SQLITE_NOTFOUND if there is no configurable
47906** system call named zName.
47907*/
47908static int winSetSystemCall(
47909 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
47910 const char *zName, /* Name of system call to override */
47911 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
47912){
47913 unsigned int i;
47914 int rc = SQLITE_NOTFOUND12;
47915
47916 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
47917 if( zName==0 ){
47918 /* If no zName is given, restore all system calls to their default
47919 ** settings and return NULL
47920 */
47921 rc = SQLITE_OK0;
47922 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
47923 if( aSyscall[i].pDefault ){
47924 aSyscall[i].pCurrent = aSyscall[i].pDefault;
47925 }
47926 }
47927 }else{
47928 /* If zName is specified, operate on only the one system call
47929 ** specified.
47930 */
47931 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
47932 if( strcmp(zName, aSyscall[i].zName)==0 ){
47933 if( aSyscall[i].pDefault==0 ){
47934 aSyscall[i].pDefault = aSyscall[i].pCurrent;
47935 }
47936 rc = SQLITE_OK0;
47937 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
47938 aSyscall[i].pCurrent = pNewFunc;
47939 break;
47940 }
47941 }
47942 }
47943 return rc;
47944}
47945
47946/*
47947** Return the value of a system call. Return NULL if zName is not a
47948** recognized system call name. NULL is also returned if the system call
47949** is currently undefined.
47950*/
47951static sqlite3_syscall_ptr winGetSystemCall(
47952 sqlite3_vfs *pNotUsed,
47953 const char *zName
47954){
47955 unsigned int i;
47956
47957 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
47958 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
47959 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
47960 }
47961 return 0;
47962}
47963
47964/*
47965** Return the name of the first system call after zName. If zName==NULL
47966** then return the name of the first system call. Return NULL if zName
47967** is the last system call or if zName is not the name of a valid
47968** system call.
47969*/
47970static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
47971 int i = -1;
47972
47973 UNUSED_PARAMETER(p)(void)(p);
47974 if( zName ){
47975 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
47976 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
47977 }
47978 }
47979 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
47980 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
47981 }
47982 return 0;
47983}
47984
47985#ifdef SQLITE_WIN32_MALLOC
47986/*
47987** If a Win32 native heap has been configured, this function will attempt to
47988** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
47989** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
47990** "pnLargest" argument, if non-zero, will be used to return the size of the
47991** largest committed free block in the heap, in bytes.
47992*/
47993SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
47994 int rc = SQLITE_OK0;
47995 UINT nLargest = 0;
47996 HANDLE hHeap;
47997
47998 winMemAssertMagic();
47999 hHeap = winMemGetHeap();
48000 assert( hHeap!=0 )((void) (0));
48001 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48002#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48003 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48004#endif
48005#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
48006 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
48007 DWORD lastErrno = osGetLastError();
48008 if( lastErrno==NO_ERROR ){
48009 sqlite3_log(SQLITE_NOMEM7, "failed to HeapCompact (no space), heap=%p",
48010 (void*)hHeap);
48011 rc = SQLITE_NOMEM_BKPT7;
48012 }else{
48013 sqlite3_log(SQLITE_ERROR1, "failed to HeapCompact (%lu), heap=%p",
48014 osGetLastError(), (void*)hHeap);
48015 rc = SQLITE_ERROR1;
48016 }
48017 }
48018#else
48019 sqlite3_log(SQLITE_NOTFOUND12, "failed to HeapCompact, heap=%p",
48020 (void*)hHeap);
48021 rc = SQLITE_NOTFOUND12;
48022#endif
48023 if( pnLargest ) *pnLargest = nLargest;
48024 return rc;
48025}
48026
48027/*
48028** If a Win32 native heap has been configured, this function will attempt to
48029** destroy and recreate it. If the Win32 native heap is not isolated and/or
48030** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
48031** be returned and no changes will be made to the Win32 native heap.
48032*/
48033SQLITE_API int sqlite3_win32_reset_heap(){
48034 int rc;
48035 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx; /* The main static mutex */
48036 MUTEX_LOGIC( sqlite3_mutex *pMem; )sqlite3_mutex *pMem; /* The memsys static mutex */
48037 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
48038 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )pMem = sqlite3MutexAlloc(3);
48039 sqlite3_mutex_enter(pMainMtx);
48040 sqlite3_mutex_enter(pMem);
48041 winMemAssertMagic();
48042 if( winMemGetHeap()!=NULL((void*)0) && winMemGetOwned() && sqlite3_memory_used()==0 ){
48043 /*
48044 ** At this point, there should be no outstanding memory allocations on
48045 ** the heap. Also, since both the main and memsys locks are currently
48046 ** being held by us, no other function (i.e. from another thread) should
48047 ** be able to even access the heap. Attempt to destroy and recreate our
48048 ** isolated Win32 native heap now.
48049 */
48050 assert( winMemGetHeap()!=NULL )((void) (0));
48051 assert( winMemGetOwned() )((void) (0));
48052 assert( sqlite3_memory_used()==0 )((void) (0));
48053 winMemShutdown(winMemGetDataPtr());
48054 assert( winMemGetHeap()==NULL )((void) (0));
48055 assert( !winMemGetOwned() )((void) (0));
48056 assert( sqlite3_memory_used()==0 )((void) (0));
48057 rc = winMemInit(winMemGetDataPtr());
48058 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL )((void) (0));
48059 assert( rc!=SQLITE_OK || winMemGetOwned() )((void) (0));
48060 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 )((void) (0));
48061 }else{
48062 /*
48063 ** The Win32 native heap cannot be modified because it may be in use.
48064 */
48065 rc = SQLITE_BUSY5;
48066 }
48067 sqlite3_mutex_leave(pMem);
48068 sqlite3_mutex_leave(pMainMtx);
48069 return rc;
48070}
48071#endif /* SQLITE_WIN32_MALLOC */
48072
48073/*
48074** This function outputs the specified (ANSI) string to the Win32 debugger
48075** (if available).
48076*/
48077
48078SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
48079 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
48080 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. */
48081 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
48082 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE )((void) (0));
48083#ifdef SQLITE_ENABLE_API_ARMOR1
48084 if( !zBuf ){
48085 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48085);
48086 return;
48087 }
48088#endif
48089#if defined(SQLITE_WIN32_HAS_ANSI)
48090 if( nMin>0 ){
48091 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
48092 memcpy(zDbgBuf, zBuf, nMin);
48093 osOutputDebugStringA(zDbgBuf);
48094 }else{
48095 osOutputDebugStringA(zBuf);
48096 }
48097#elif defined(SQLITE_WIN32_HAS_WIDE)
48098 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
48099 if ( osMultiByteToWideChar(
48100 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
48101 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
48102 return;
48103 }
48104 osOutputDebugStringW((LPCWSTR)zDbgBuf);
48105#else
48106 if( nMin>0 ){
48107 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
48108 memcpy(zDbgBuf, zBuf, nMin);
48109 fprintf(stderrstderr, "%s", zDbgBuf);
48110 }else{
48111 fprintf(stderrstderr, "%s", zBuf);
48112 }
48113#endif
48114}
48115
48116/*
48117** The following routine suspends the current thread for at least ms
48118** milliseconds. This is equivalent to the Win32 Sleep() interface.
48119*/
48120#if SQLITE_OS_WINRT
48121static HANDLE sleepObj = NULL((void*)0);
48122#endif
48123
48124SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
48125#if SQLITE_OS_WINRT
48126 if ( sleepObj==NULL((void*)0) ){
48127 sleepObj = osCreateEventExW(NULL((void*)0), NULL((void*)0), CREATE_EVENT_MANUAL_RESET,
48128 SYNCHRONIZE);
48129 }
48130 assert( sleepObj!=NULL )((void) (0));
48131 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
48132#else
48133 osSleep(milliseconds);
48134#endif
48135}
48136
48137#if SQLITE_MAX_WORKER_THREADS8>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
48138 SQLITE_THREADSAFE2>0
48139SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject){
48140 DWORD rc;
48141 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
48142 TRUE))==WAIT_IO_COMPLETION ){}
48143 return rc;
48144}
48145#endif
48146
48147/*
48148** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
48149** or WinCE. Return false (zero) for Win95, Win98, or WinME.
48150**
48151** Here is an interesting observation: Win95, Win98, and WinME lack
48152** the LockFileEx() API. But we can still statically link against that
48153** API as long as we don't call it when running Win95/98/ME. A call to
48154** this routine is used to determine if the host is Win95/98/ME or
48155** WinNT/2K/XP so that we will know whether or not we can safely call
48156** the LockFileEx() API.
48157*/
48158
48159#if !SQLITE_WIN32_GETVERSIONEX
48160# define osIsNT() (1)
48161#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
48162# define osIsNT() (1)
48163#elif !defined(SQLITE_WIN32_HAS_WIDE)
48164# define osIsNT() (0)
48165#else
48166# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
48167#endif
48168
48169/*
48170** This function determines if the machine is running a version of Windows
48171** based on the NT kernel.
48172*/
48173SQLITE_API int sqlite3_win32_is_nt(void){
48174#if SQLITE_OS_WINRT
48175 /*
48176 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
48177 ** kernel.
48178 */
48179 return 1;
48180#elif SQLITE_WIN32_GETVERSIONEX
48181 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
48182#if defined(SQLITE_WIN32_HAS_ANSI)
48183 OSVERSIONINFOA sInfo;
48184 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
48185 osGetVersionExA(&sInfo);
48186 osInterlockedCompareExchange(&sqlite3_os_type,
48187 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
48188#elif defined(SQLITE_WIN32_HAS_WIDE)
48189 OSVERSIONINFOW sInfo;
48190 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
48191 osGetVersionExW(&sInfo);
48192 osInterlockedCompareExchange(&sqlite3_os_type,
48193 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
48194#endif
48195 }
48196 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48197#elif SQLITE_TEST
48198 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48199#else
48200 /*
48201 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
48202 ** deprecated are always assumed to be based on the NT kernel.
48203 */
48204 return 1;
48205#endif
48206}
48207
48208#ifdef SQLITE_WIN32_MALLOC
48209/*
48210** Allocate nBytes of memory.
48211*/
48212static void *winMemMalloc(int nBytes){
48213 HANDLE hHeap;
48214 void *p;
48215
48216 winMemAssertMagic();
48217 hHeap = winMemGetHeap();
48218 assert( hHeap!=0 )((void) (0));
48219 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48220#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48221 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48222#endif
48223 assert( nBytes>=0 )((void) (0));
48224 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
48225 if( !p ){
48226 sqlite3_log(SQLITE_NOMEM7, "failed to HeapAlloc %u bytes (%lu), heap=%p",
48227 nBytes, osGetLastError(), (void*)hHeap);
48228 }
48229 return p;
48230}
48231
48232/*
48233** Free memory.
48234*/
48235static void winMemFree(void *pPrior){
48236 HANDLE hHeap;
48237
48238 winMemAssertMagic();
48239 hHeap = winMemGetHeap();
48240 assert( hHeap!=0 )((void) (0));
48241 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48242#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48243 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
48244#endif
48245 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
48246 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
48247 sqlite3_log(SQLITE_NOMEM7, "failed to HeapFree block %p (%lu), heap=%p",
48248 pPrior, osGetLastError(), (void*)hHeap);
48249 }
48250}
48251
48252/*
48253** Change the size of an existing memory allocation
48254*/
48255static void *winMemRealloc(void *pPrior, int nBytes){
48256 HANDLE hHeap;
48257 void *p;
48258
48259 winMemAssertMagic();
48260 hHeap = winMemGetHeap();
48261 assert( hHeap!=0 )((void) (0));
48262 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48263#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48264 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
48265#endif
48266 assert( nBytes>=0 )((void) (0));
48267 if( !pPrior ){
48268 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
48269 }else{
48270 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
48271 }
48272 if( !p ){
48273 sqlite3_log(SQLITE_NOMEM7, "failed to %s %u bytes (%lu), heap=%p",
48274 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
48275 (void*)hHeap);
48276 }
48277 return p;
48278}
48279
48280/*
48281** Return the size of an outstanding allocation, in bytes.
48282*/
48283static int winMemSize(void *p){
48284 HANDLE hHeap;
48285 SIZE_T n;
48286
48287 winMemAssertMagic();
48288 hHeap = winMemGetHeap();
48289 assert( hHeap!=0 )((void) (0));
48290 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48291#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48292 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) )((void) (0));
48293#endif
48294 if( !p ) return 0;
48295 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
48296 if( n==(SIZE_T)-1 ){
48297 sqlite3_log(SQLITE_NOMEM7, "failed to HeapSize block %p (%lu), heap=%p",
48298 p, osGetLastError(), (void*)hHeap);
48299 return 0;
48300 }
48301 return (int)n;
48302}
48303
48304/*
48305** Round up a request size to the next valid allocation size.
48306*/
48307static int winMemRoundup(int n){
48308 return n;
48309}
48310
48311/*
48312** Initialize this module.
48313*/
48314static int winMemInit(void *pAppData){
48315 winMemData *pWinMemData = (winMemData *)pAppData;
48316
48317 if( !pWinMemData ) return SQLITE_ERROR1;
48318 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
48319 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
48320
48321#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
48322 if( !pWinMemData->hHeap ){
48323 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
48324 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfigsqlite3Config.nHeap;
48325 if( dwMaximumSize==0 ){
48326 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
48327 }else if( dwInitialSize>dwMaximumSize ){
48328 dwInitialSize = dwMaximumSize;
48329 }
48330 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
48331 dwInitialSize, dwMaximumSize);
48332 if( !pWinMemData->hHeap ){
48333 sqlite3_log(SQLITE_NOMEM7,
48334 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
48335 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
48336 dwMaximumSize);
48337 return SQLITE_NOMEM_BKPT7;
48338 }
48339 pWinMemData->bOwned = TRUE;
48340 assert( pWinMemData->bOwned )((void) (0));
48341 }
48342#else
48343 pWinMemData->hHeap = osGetProcessHeap();
48344 if( !pWinMemData->hHeap ){
48345 sqlite3_log(SQLITE_NOMEM7,
48346 "failed to GetProcessHeap (%lu)", osGetLastError());
48347 return SQLITE_NOMEM_BKPT7;
48348 }
48349 pWinMemData->bOwned = FALSE;
48350 assert( !pWinMemData->bOwned )((void) (0));
48351#endif
48352 assert( pWinMemData->hHeap!=0 )((void) (0));
48353 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48354#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48355 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48356#endif
48357 return SQLITE_OK0;
48358}
48359
48360/*
48361** Deinitialize this module.
48362*/
48363static void winMemShutdown(void *pAppData){
48364 winMemData *pWinMemData = (winMemData *)pAppData;
48365
48366 if( !pWinMemData ) return;
48367 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
48368 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
48369
48370 if( pWinMemData->hHeap ){
48371 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
48372#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
48373 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
48374#endif
48375 if( pWinMemData->bOwned ){
48376 if( !osHeapDestroy(pWinMemData->hHeap) ){
48377 sqlite3_log(SQLITE_NOMEM7, "failed to HeapDestroy (%lu), heap=%p",
48378 osGetLastError(), (void*)pWinMemData->hHeap);
48379 }
48380 pWinMemData->bOwned = FALSE;
48381 }
48382 pWinMemData->hHeap = NULL((void*)0);
48383 }
48384}
48385
48386/*
48387** Populate the low-level memory allocation function pointers in
48388** sqlite3GlobalConfig.m with pointers to the routines in this file. The
48389** arguments specify the block of memory to manage.
48390**
48391** This routine is only called by sqlite3_config(), and therefore
48392** is not required to be threadsafe (it is not).
48393*/
48394SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void){
48395 static const sqlite3_mem_methods winMemMethods = {
48396 winMemMalloc,
48397 winMemFree,
48398 winMemRealloc,
48399 winMemSize,
48400 winMemRoundup,
48401 winMemInit,
48402 winMemShutdown,
48403 &win_mem_data
48404 };
48405 return &winMemMethods;
48406}
48407
48408SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
48409 sqlite3_config(SQLITE_CONFIG_MALLOC4, sqlite3MemGetWin32());
48410}
48411#endif /* SQLITE_WIN32_MALLOC */
48412
48413/*
48414** Convert a UTF-8 string to Microsoft Unicode.
48415**
48416** Space to hold the returned string is obtained from sqlite3_malloc().
48417*/
48418static LPWSTR winUtf8ToUnicode(const char *zText){
48419 int nChar;
48420 LPWSTR zWideText;
48421
48422 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL((void*)0), 0);
48423 if( nChar==0 ){
48424 return 0;
48425 }
48426 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
48427 if( zWideText==0 ){
48428 return 0;
48429 }
48430 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
48431 nChar);
48432 if( nChar==0 ){
48433 sqlite3_free(zWideText);
48434 zWideText = 0;
48435 }
48436 return zWideText;
48437}
48438
48439/*
48440** Convert a Microsoft Unicode string to UTF-8.
48441**
48442** Space to hold the returned string is obtained from sqlite3_malloc().
48443*/
48444static char *winUnicodeToUtf8(LPCWSTR zWideText){
48445 int nByte;
48446 char *zText;
48447
48448 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
48449 if( nByte == 0 ){
48450 return 0;
48451 }
48452 zText = sqlite3MallocZero( nByte );
48453 if( zText==0 ){
48454 return 0;
48455 }
48456 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
48457 0, 0);
48458 if( nByte == 0 ){
48459 sqlite3_free(zText);
48460 zText = 0;
48461 }
48462 return zText;
48463}
48464
48465/*
48466** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
48467** code page.
48468**
48469** Space to hold the returned string is obtained from sqlite3_malloc().
48470*/
48471static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
48472 int nByte;
48473 LPWSTR zMbcsText;
48474 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
48475
48476 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL((void*)0),
48477 0)*sizeof(WCHAR);
48478 if( nByte==0 ){
48479 return 0;
48480 }
48481 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
48482 if( zMbcsText==0 ){
48483 return 0;
48484 }
48485 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
48486 nByte);
48487 if( nByte==0 ){
48488 sqlite3_free(zMbcsText);
48489 zMbcsText = 0;
48490 }
48491 return zMbcsText;
48492}
48493
48494/*
48495** Convert a Microsoft Unicode string to a multi-byte character string,
48496** using the ANSI or OEM code page.
48497**
48498** Space to hold the returned string is obtained from sqlite3_malloc().
48499*/
48500static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
48501 int nByte;
48502 char *zText;
48503 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
48504
48505 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
48506 if( nByte == 0 ){
48507 return 0;
48508 }
48509 zText = sqlite3MallocZero( nByte );
48510 if( zText==0 ){
48511 return 0;
48512 }
48513 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
48514 nByte, 0, 0);
48515 if( nByte == 0 ){
48516 sqlite3_free(zText);
48517 zText = 0;
48518 }
48519 return zText;
48520}
48521
48522/*
48523** Convert a multi-byte character string to UTF-8.
48524**
48525** Space to hold the returned string is obtained from sqlite3_malloc().
48526*/
48527static char *winMbcsToUtf8(const char *zText, int useAnsi){
48528 char *zTextUtf8;
48529 LPWSTR zTmpWide;
48530
48531 zTmpWide = winMbcsToUnicode(zText, useAnsi);
48532 if( zTmpWide==0 ){
48533 return 0;
48534 }
48535 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
48536 sqlite3_free(zTmpWide);
48537 return zTextUtf8;
48538}
48539
48540/*
48541** Convert a UTF-8 string to a multi-byte character string.
48542**
48543** Space to hold the returned string is obtained from sqlite3_malloc().
48544*/
48545static char *winUtf8ToMbcs(const char *zText, int useAnsi){
48546 char *zTextMbcs;
48547 LPWSTR zTmpWide;
48548
48549 zTmpWide = winUtf8ToUnicode(zText);
48550 if( zTmpWide==0 ){
48551 return 0;
48552 }
48553 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
48554 sqlite3_free(zTmpWide);
48555 return zTextMbcs;
48556}
48557
48558/*
48559** This is a public wrapper for the winUtf8ToUnicode() function.
48560*/
48561SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
48562#ifdef SQLITE_ENABLE_API_ARMOR1
48563 if( !zText ){
48564 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48564);
48565 return 0;
48566 }
48567#endif
48568#ifndef SQLITE_OMIT_AUTOINIT
48569 if( sqlite3_initialize() ) return 0;
48570#endif
48571 return winUtf8ToUnicode(zText);
48572}
48573
48574/*
48575** This is a public wrapper for the winUnicodeToUtf8() function.
48576*/
48577SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
48578#ifdef SQLITE_ENABLE_API_ARMOR1
48579 if( !zWideText ){
48580 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48580);
48581 return 0;
48582 }
48583#endif
48584#ifndef SQLITE_OMIT_AUTOINIT
48585 if( sqlite3_initialize() ) return 0;
48586#endif
48587 return winUnicodeToUtf8(zWideText);
48588}
48589
48590/*
48591** This is a public wrapper for the winMbcsToUtf8() function.
48592*/
48593SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
48594#ifdef SQLITE_ENABLE_API_ARMOR1
48595 if( !zText ){
48596 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48596);
48597 return 0;
48598 }
48599#endif
48600#ifndef SQLITE_OMIT_AUTOINIT
48601 if( sqlite3_initialize() ) return 0;
48602#endif
48603 return winMbcsToUtf8(zText, osAreFileApisANSI());
48604}
48605
48606/*
48607** This is a public wrapper for the winMbcsToUtf8() function.
48608*/
48609SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
48610#ifdef SQLITE_ENABLE_API_ARMOR1
48611 if( !zText ){
48612 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48612);
48613 return 0;
48614 }
48615#endif
48616#ifndef SQLITE_OMIT_AUTOINIT
48617 if( sqlite3_initialize() ) return 0;
48618#endif
48619 return winMbcsToUtf8(zText, useAnsi);
48620}
48621
48622/*
48623** This is a public wrapper for the winUtf8ToMbcs() function.
48624*/
48625SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
48626#ifdef SQLITE_ENABLE_API_ARMOR1
48627 if( !zText ){
48628 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48628);
48629 return 0;
48630 }
48631#endif
48632#ifndef SQLITE_OMIT_AUTOINIT
48633 if( sqlite3_initialize() ) return 0;
48634#endif
48635 return winUtf8ToMbcs(zText, osAreFileApisANSI());
48636}
48637
48638/*
48639** This is a public wrapper for the winUtf8ToMbcs() function.
48640*/
48641SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
48642#ifdef SQLITE_ENABLE_API_ARMOR1
48643 if( !zText ){
48644 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(48644);
48645 return 0;
48646 }
48647#endif
48648#ifndef SQLITE_OMIT_AUTOINIT
48649 if( sqlite3_initialize() ) return 0;
48650#endif
48651 return winUtf8ToMbcs(zText, useAnsi);
48652}
48653
48654/*
48655** This function is the same as sqlite3_win32_set_directory (below); however,
48656** it accepts a UTF-8 string.
48657*/
48658SQLITE_API int sqlite3_win32_set_directory8(
48659 unsigned long type, /* Identifier for directory being set or reset */
48660 const char *zValue /* New value for directory being set or reset */
48661){
48662 char **ppDirectory = 0;
48663 int rc;
48664#ifndef SQLITE_OMIT_AUTOINIT
48665 rc = sqlite3_initialize();
48666 if( rc ) return rc;
48667#endif
48668 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
48669 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE1 ){
48670 ppDirectory = &sqlite3_data_directory;
48671 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 ){
48672 ppDirectory = &sqlite3_temp_directory;
48673 }
48674 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE((void) (0))
48675 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE((void) (0))
48676 )((void) (0));
48677 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) )((void) (0));
48678 if( ppDirectory ){
48679 char *zCopy = 0;
48680 if( zValue && zValue[0] ){
48681 zCopy = sqlite3_mprintf("%s", zValue);
48682 if ( zCopy==0 ){
48683 rc = SQLITE_NOMEM_BKPT7;
48684 goto set_directory8_done;
48685 }
48686 }
48687 sqlite3_free(*ppDirectory);
48688 *ppDirectory = zCopy;
48689 rc = SQLITE_OK0;
48690 }else{
48691 rc = SQLITE_ERROR1;
48692 }
48693set_directory8_done:
48694 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
48695 return rc;
48696}
48697
48698/*
48699** This function is the same as sqlite3_win32_set_directory (below); however,
48700** it accepts a UTF-16 string.
48701*/
48702SQLITE_API int sqlite3_win32_set_directory16(
48703 unsigned long type, /* Identifier for directory being set or reset */
48704 const void *zValue /* New value for directory being set or reset */
48705){
48706 int rc;
48707 char *zUtf8 = 0;
48708 if( zValue ){
48709 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
48710 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT7;
48711 }
48712 rc = sqlite3_win32_set_directory8(type, zUtf8);
48713 if( zUtf8 ) sqlite3_free(zUtf8);
48714 return rc;
48715}
48716
48717/*
48718** This function sets the data directory or the temporary directory based on
48719** the provided arguments. The type argument must be 1 in order to set the
48720** data directory or 2 in order to set the temporary directory. The zValue
48721** argument is the name of the directory to use. The return value will be
48722** SQLITE_OK if successful.
48723*/
48724SQLITE_API int sqlite3_win32_set_directory(
48725 unsigned long type, /* Identifier for directory being set or reset */
48726 void *zValue /* New value for directory being set or reset */
48727){
48728 return sqlite3_win32_set_directory16(type, zValue);
48729}
48730
48731/*
48732** The return value of winGetLastErrorMsg
48733** is zero if the error message fits in the buffer, or non-zero
48734** otherwise (if the message was truncated).
48735*/
48736static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
48737 /* FormatMessage returns 0 on failure. Otherwise it
48738 ** returns the number of TCHARs written to the output
48739 ** buffer, excluding the terminating null char.
48740 */
48741 DWORD dwLen = 0;
48742 char *zOut = 0;
48743
48744 if( osIsNT() ){
48745#if SQLITE_OS_WINRT
48746 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
48747 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
48748 FORMAT_MESSAGE_IGNORE_INSERTS,
48749 NULL((void*)0),
48750 lastErrno,
48751 0,
48752 zTempWide,
48753 SQLITE_WIN32_MAX_ERRMSG_CHARS,
48754 0);
48755#else
48756 LPWSTR zTempWide = NULL((void*)0);
48757 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
48758 FORMAT_MESSAGE_FROM_SYSTEM |
48759 FORMAT_MESSAGE_IGNORE_INSERTS,
48760 NULL((void*)0),
48761 lastErrno,
48762 0,
48763 (LPWSTR) &zTempWide,
48764 0,
48765 0);
48766#endif
48767 if( dwLen > 0 ){
48768 /* allocate a buffer and convert to UTF8 */
48769 sqlite3BeginBenignMalloc();
48770 zOut = winUnicodeToUtf8(zTempWide);
48771 sqlite3EndBenignMalloc();
48772#if !SQLITE_OS_WINRT
48773 /* free the system buffer allocated by FormatMessage */
48774 osLocalFree(zTempWide);
48775#endif
48776 }
48777 }
48778#ifdef SQLITE_WIN32_HAS_ANSI
48779 else{
48780 char *zTemp = NULL((void*)0);
48781 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
48782 FORMAT_MESSAGE_FROM_SYSTEM |
48783 FORMAT_MESSAGE_IGNORE_INSERTS,
48784 NULL((void*)0),
48785 lastErrno,
48786 0,
48787 (LPSTR) &zTemp,
48788 0,
48789 0);
48790 if( dwLen > 0 ){
48791 /* allocate a buffer and convert to UTF8 */
48792 sqlite3BeginBenignMalloc();
48793 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
48794 sqlite3EndBenignMalloc();
48795 /* free the system buffer allocated by FormatMessage */
48796 osLocalFree(zTemp);
48797 }
48798 }
48799#endif
48800 if( 0 == dwLen ){
48801 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
48802 }else{
48803 /* copy a maximum of nBuf chars to output buffer */
48804 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
48805 /* free the UTF8 buffer */
48806 sqlite3_free(zOut);
48807 }
48808 return 0;
48809}
48810
48811/*
48812**
48813** This function - winLogErrorAtLine() - is only ever called via the macro
48814** winLogError().
48815**
48816** This routine is invoked after an error occurs in an OS function.
48817** It logs a message using sqlite3_log() containing the current value of
48818** error code and, if possible, the human-readable equivalent from
48819** FormatMessage.
48820**
48821** The first argument passed to the macro should be the error code that
48822** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
48823** The two subsequent arguments should be the name of the OS function that
48824** failed and the associated file-system path, if any.
48825*/
48826#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__48826)
48827static int winLogErrorAtLine(
48828 int errcode, /* SQLite error code */
48829 DWORD lastErrno, /* Win32 last error */
48830 const char *zFunc, /* Name of OS function that failed */
48831 const char *zPath, /* File path associated with error */
48832 int iLine /* Source line number where error occurred */
48833){
48834 char zMsg[500]; /* Human readable error text */
48835 int i; /* Loop counter */
48836
48837 zMsg[0] = 0;
48838 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
48839 assert( errcode!=SQLITE_OK )((void) (0));
48840 if( zPath==0 ) zPath = "";
48841 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
48842 zMsg[i] = 0;
48843 sqlite3_log(errcode,
48844 "os_win.c:%d: (%lu) %s(%s) - %s",
48845 iLine, lastErrno, zFunc, zPath, zMsg
48846 );
48847
48848 return errcode;
48849}
48850
48851/*
48852** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
48853** will be retried following a locking error - probably caused by
48854** antivirus software. Also the initial delay before the first retry.
48855** The delay increases linearly with each retry.
48856*/
48857#ifndef SQLITE_WIN32_IOERR_RETRY
48858# define SQLITE_WIN32_IOERR_RETRY 10
48859#endif
48860#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
48861# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
48862#endif
48863static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
48864static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
48865
48866/*
48867** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
48868** error code obtained via GetLastError() is eligible to be retried. It
48869** must accept the error code DWORD as its only argument and should return
48870** non-zero if the error code is transient in nature and the operation
48871** responsible for generating the original error might succeed upon being
48872** retried. The argument to this macro should be a variable.
48873**
48874** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
48875** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
48876** returns zero. The "winIoerrCanRetry2" macro is completely optional and
48877** may be used to include additional error codes in the set that should
48878** result in the failing I/O operation being retried by the caller. If
48879** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
48880** identical to those of the "winIoerrCanRetry1" macro.
48881*/
48882#if !defined(winIoerrCanRetry1)
48883#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
48884 ((a)==ERROR_SHARING_VIOLATION) || \
48885 ((a)==ERROR_LOCK_VIOLATION) || \
48886 ((a)==ERROR_DEV_NOT_EXIST) || \
48887 ((a)==ERROR_NETNAME_DELETED) || \
48888 ((a)==ERROR_SEM_TIMEOUT) || \
48889 ((a)==ERROR_NETWORK_UNREACHABLE))
48890#endif
48891
48892/*
48893** If a ReadFile() or WriteFile() error occurs, invoke this routine
48894** to see if it should be retried. Return TRUE to retry. Return FALSE
48895** to give up with an error.
48896*/
48897static int winRetryIoerr(int *pnRetry, DWORD *pError){
48898 DWORD e = osGetLastError();
48899 if( *pnRetry>=winIoerrRetry ){
48900 if( pError ){
48901 *pError = e;
48902 }
48903 return 0;
48904 }
48905 if( winIoerrCanRetry1(e) ){
48906 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
48907 ++*pnRetry;
48908 return 1;
48909 }
48910#if defined(winIoerrCanRetry2)
48911 else if( winIoerrCanRetry2(e) ){
48912 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
48913 ++*pnRetry;
48914 return 1;
48915 }
48916#endif
48917 if( pError ){
48918 *pError = e;
48919 }
48920 return 0;
48921}
48922
48923/*
48924** Log a I/O error retry episode.
48925*/
48926static void winLogIoerr(int nRetry, int lineno){
48927 if( nRetry ){
48928 sqlite3_log(SQLITE_NOTICE27,
48929 "delayed %dms for lock/sharing conflict at line %d",
48930 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
48931 );
48932 }
48933}
48934
48935/*
48936** This #if does not rely on the SQLITE_OS_WINCE define because the
48937** corresponding section in "date.c" cannot use it.
48938*/
48939#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
48940 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
48941/*
48942** The MSVC CRT on Windows CE may not have a localtime() function.
48943** So define a substitute.
48944*/
48945/* # include <time.h> */
48946struct tm *__cdecl localtime(const time_t *t)
48947{
48948 static struct tm y;
48949 FILETIME uTm, lTm;
48950 SYSTEMTIME pTm;
48951 sqlite3_int64 t64;
48952 t64 = *t;
48953 t64 = (t64 + 11644473600)*10000000;
48954 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
48955 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
48956 osFileTimeToLocalFileTime(&uTm,&lTm);
48957 osFileTimeToSystemTime(&lTm,&pTm);
48958 y.tm_year = pTm.wYear - 1900;
48959 y.tm_mon = pTm.wMonth - 1;
48960 y.tm_wday = pTm.wDayOfWeek;
48961 y.tm_mday = pTm.wDay;
48962 y.tm_hour = pTm.wHour;
48963 y.tm_min = pTm.wMinute;
48964 y.tm_sec = pTm.wSecond;
48965 return &y;
48966}
48967#endif
48968
48969#if SQLITE_OS_WINCE
48970/*************************************************************************
48971** This section contains code for WinCE only.
48972*/
48973#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)__builtin_offsetof(winFile, h)]
48974
48975/*
48976** Acquire a lock on the handle h
48977*/
48978static void winceMutexAcquire(HANDLE h){
48979 DWORD dwErr;
48980 do {
48981 dwErr = osWaitForSingleObject(h, INFINITE);
48982 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
48983}
48984/*
48985** Release a lock acquired by winceMutexAcquire()
48986*/
48987#define winceMutexRelease(h) ReleaseMutex(h)
48988
48989/*
48990** Create the mutex and shared memory used for locking in the file
48991** descriptor pFile
48992*/
48993static int winceCreateLock(const char *zFilename, winFile *pFile){
48994 LPWSTR zTok;
48995 LPWSTR zName;
48996 DWORD lastErrno;
48997 BOOL bLogged = FALSE;
48998 BOOL bInit = TRUE;
48999
49000 zName = winUtf8ToUnicode(zFilename);
49001 if( zName==0 ){
49002 /* out of memory */
49003 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
49004 }
49005
49006 /* Initialize the local lockdata */
49007 memset(&pFile->local, 0, sizeof(pFile->local));
49008
49009 /* Replace the backslashes from the filename and lowercase it
49010 ** to derive a mutex name. */
49011 zTok = osCharLowerW(zName);
49012 for (;*zTok;zTok++){
49013 if (*zTok == '\\') *zTok = '_';
49014 }
49015
49016 /* Create/open the named mutex */
49017 pFile->hMutex = osCreateMutexW(NULL((void*)0), FALSE, zName);
49018 if (!pFile->hMutex){
49019 pFile->lastErrno = osGetLastError();
49020 sqlite3_free(zName);
49021 return winLogError(SQLITE_IOERR10, pFile->lastErrno,
49022 "winceCreateLock1", zFilename);
49023 }
49024
49025 /* Acquire the mutex before continuing */
49026 winceMutexAcquire(pFile->hMutex);
49027
49028 /* Since the names of named mutexes, semaphores, file mappings etc are
49029 ** case-sensitive, take advantage of that by uppercasing the mutex name
49030 ** and using that as the shared filemapping name.
49031 */
49032 osCharUpperW(zName);
49033 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL((void*)0),
49034 PAGE_READWRITE, 0, sizeof(winceLock),
49035 zName);
49036
49037 /* Set a flag that indicates we're the first to create the memory so it
49038 ** must be zero-initialized */
49039 lastErrno = osGetLastError();
49040 if (lastErrno == ERROR_ALREADY_EXISTS){
49041 bInit = FALSE;
49042 }
49043
49044 sqlite3_free(zName);
49045
49046 /* If we succeeded in making the shared memory handle, map it. */
49047 if( pFile->hShared ){
49048 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
49049 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
49050 /* If mapping failed, close the shared memory handle and erase it */
49051 if( !pFile->shared ){
49052 pFile->lastErrno = osGetLastError();
49053 winLogError(SQLITE_IOERR10, pFile->lastErrno,
49054 "winceCreateLock2", zFilename);
49055 bLogged = TRUE;
49056 osCloseHandle(pFile->hShared);
49057 pFile->hShared = NULL((void*)0);
49058 }
49059 }
49060
49061 /* If shared memory could not be created, then close the mutex and fail */
49062 if( pFile->hShared==NULL((void*)0) ){
49063 if( !bLogged ){
49064 pFile->lastErrno = lastErrno;
49065 winLogError(SQLITE_IOERR10, pFile->lastErrno,
49066 "winceCreateLock3", zFilename);
49067 bLogged = TRUE;
49068 }
49069 winceMutexRelease(pFile->hMutex);
49070 osCloseHandle(pFile->hMutex);
49071 pFile->hMutex = NULL((void*)0);
49072 return SQLITE_IOERR10;
49073 }
49074
49075 /* Initialize the shared memory if we're supposed to */
49076 if( bInit ){
49077 memset(pFile->shared, 0, sizeof(winceLock));
49078 }
49079
49080 winceMutexRelease(pFile->hMutex);
49081 return SQLITE_OK0;
49082}
49083
49084/*
49085** Destroy the part of winFile that deals with wince locks
49086*/
49087static void winceDestroyLock(winFile *pFile){
49088 if (pFile->hMutex){
49089 /* Acquire the mutex */
49090 winceMutexAcquire(pFile->hMutex);
49091
49092 /* The following blocks should probably assert in debug mode, but they
49093 are to cleanup in case any locks remained open */
49094 if (pFile->local.nReaders){
49095 pFile->shared->nReaders --;
49096 }
49097 if (pFile->local.bReserved){
49098 pFile->shared->bReserved = FALSE;
49099 }
49100 if (pFile->local.bPending){
49101 pFile->shared->bPending = FALSE;
49102 }
49103 if (pFile->local.bExclusive){
49104 pFile->shared->bExclusive = FALSE;
49105 }
49106
49107 /* De-reference and close our copy of the shared memory handle */
49108 osUnmapViewOfFile(pFile->shared);
49109 osCloseHandle(pFile->hShared);
49110
49111 /* Done with the mutex */
49112 winceMutexRelease(pFile->hMutex);
49113 osCloseHandle(pFile->hMutex);
49114 pFile->hMutex = NULL((void*)0);
49115 }
49116}
49117
49118/*
49119** An implementation of the LockFile() API of Windows for CE
49120*/
49121static BOOL winceLockFile(
49122 LPHANDLE phFile,
49123 DWORD dwFileOffsetLow,
49124 DWORD dwFileOffsetHigh,
49125 DWORD nNumberOfBytesToLockLow,
49126 DWORD nNumberOfBytesToLockHigh
49127){
49128 winFile *pFile = HANDLE_TO_WINFILE(phFile);
49129 BOOL bReturn = FALSE;
49130
49131 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
49132 UNUSED_PARAMETER(nNumberOfBytesToLockHigh)(void)(nNumberOfBytesToLockHigh);
49133
49134 if (!pFile->hMutex) return TRUE;
49135 winceMutexAcquire(pFile->hMutex);
49136
49137 /* Wanting an exclusive lock? */
49138 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)
49139 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE510){
49140 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
49141 pFile->shared->bExclusive = TRUE;
49142 pFile->local.bExclusive = TRUE;
49143 bReturn = TRUE;
49144 }
49145 }
49146
49147 /* Want a read-only lock? */
49148 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2) &&
49149 nNumberOfBytesToLockLow == 1){
49150 if (pFile->shared->bExclusive == 0){
49151 pFile->local.nReaders ++;
49152 if (pFile->local.nReaders == 1){
49153 pFile->shared->nReaders ++;
49154 }
49155 bReturn = TRUE;
49156 }
49157 }
49158
49159 /* Want a pending lock? */
49160 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
49161 && nNumberOfBytesToLockLow == 1){
49162 /* If no pending lock has been acquired, then acquire it */
49163 if (pFile->shared->bPending == 0) {
49164 pFile->shared->bPending = TRUE;
49165 pFile->local.bPending = TRUE;
49166 bReturn = TRUE;
49167 }
49168 }
49169
49170 /* Want a reserved lock? */
49171 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
49172 && nNumberOfBytesToLockLow == 1){
49173 if (pFile->shared->bReserved == 0) {
49174 pFile->shared->bReserved = TRUE;
49175 pFile->local.bReserved = TRUE;
49176 bReturn = TRUE;
49177 }
49178 }
49179
49180 winceMutexRelease(pFile->hMutex);
49181 return bReturn;
49182}
49183
49184/*
49185** An implementation of the UnlockFile API of Windows for CE
49186*/
49187static BOOL winceUnlockFile(
49188 LPHANDLE phFile,
49189 DWORD dwFileOffsetLow,
49190 DWORD dwFileOffsetHigh,
49191 DWORD nNumberOfBytesToUnlockLow,
49192 DWORD nNumberOfBytesToUnlockHigh
49193){
49194 winFile *pFile = HANDLE_TO_WINFILE(phFile);
49195 BOOL bReturn = FALSE;
49196
49197 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
49198 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh)(void)(nNumberOfBytesToUnlockHigh);
49199
49200 if (!pFile->hMutex) return TRUE;
49201 winceMutexAcquire(pFile->hMutex);
49202
49203 /* Releasing a reader lock or an exclusive lock */
49204 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)){
49205 /* Did we have an exclusive lock? */
49206 if (pFile->local.bExclusive){
49207 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE)((void) (0));
49208 pFile->local.bExclusive = FALSE;
49209 pFile->shared->bExclusive = FALSE;
49210 bReturn = TRUE;
49211 }
49212
49213 /* Did we just have a reader lock? */
49214 else if (pFile->local.nReaders){
49215 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE((void) (0))
49216 || nNumberOfBytesToUnlockLow == 1)((void) (0));
49217 pFile->local.nReaders --;
49218 if (pFile->local.nReaders == 0)
49219 {
49220 pFile->shared->nReaders --;
49221 }
49222 bReturn = TRUE;
49223 }
49224 }
49225
49226 /* Releasing a pending lock */
49227 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
49228 && nNumberOfBytesToUnlockLow == 1){
49229 if (pFile->local.bPending){
49230 pFile->local.bPending = FALSE;
49231 pFile->shared->bPending = FALSE;
49232 bReturn = TRUE;
49233 }
49234 }
49235 /* Releasing a reserved lock */
49236 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
49237 && nNumberOfBytesToUnlockLow == 1){
49238 if (pFile->local.bReserved) {
49239 pFile->local.bReserved = FALSE;
49240 pFile->shared->bReserved = FALSE;
49241 bReturn = TRUE;
49242 }
49243 }
49244
49245 winceMutexRelease(pFile->hMutex);
49246 return bReturn;
49247}
49248/*
49249** End of the special code for wince
49250*****************************************************************************/
49251#endif /* SQLITE_OS_WINCE */
49252
49253/*
49254** Lock a file region.
49255*/
49256static BOOL winLockFile(
49257 LPHANDLE phFile,
49258 DWORD flags,
49259 DWORD offsetLow,
49260 DWORD offsetHigh,
49261 DWORD numBytesLow,
49262 DWORD numBytesHigh
49263){
49264#if SQLITE_OS_WINCE
49265 /*
49266 ** NOTE: Windows CE is handled differently here due its lack of the Win32
49267 ** API LockFile.
49268 */
49269 return winceLockFile(phFile, offsetLow, offsetHigh,
49270 numBytesLow, numBytesHigh);
49271#else
49272 if( osIsNT() ){
49273 OVERLAPPED ovlp;
49274 memset(&ovlp, 0, sizeof(OVERLAPPED));
49275 ovlp.Offset = offsetLow;
49276 ovlp.OffsetHigh = offsetHigh;
49277 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
49278 }else{
49279 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49280 numBytesHigh);
49281 }
49282#endif
49283}
49284
49285/*
49286** Unlock a file region.
49287 */
49288static BOOL winUnlockFile(
49289 LPHANDLE phFile,
49290 DWORD offsetLow,
49291 DWORD offsetHigh,
49292 DWORD numBytesLow,
49293 DWORD numBytesHigh
49294){
49295#if SQLITE_OS_WINCE
49296 /*
49297 ** NOTE: Windows CE is handled differently here due its lack of the Win32
49298 ** API UnlockFile.
49299 */
49300 return winceUnlockFile(phFile, offsetLow, offsetHigh,
49301 numBytesLow, numBytesHigh);
49302#else
49303 if( osIsNT() ){
49304 OVERLAPPED ovlp;
49305 memset(&ovlp, 0, sizeof(OVERLAPPED));
49306 ovlp.Offset = offsetLow;
49307 ovlp.OffsetHigh = offsetHigh;
49308 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
49309 }else{
49310 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49311 numBytesHigh);
49312 }
49313#endif
49314}
49315
49316/*****************************************************************************
49317** The next group of routines implement the I/O methods specified
49318** by the sqlite3_io_methods object.
49319******************************************************************************/
49320
49321/*
49322** Some Microsoft compilers lack this definition.
49323*/
49324#ifndef INVALID_SET_FILE_POINTER
49325# define INVALID_SET_FILE_POINTER ((DWORD)-1)
49326#endif
49327
49328/*
49329** Move the current position of the file handle passed as the first
49330** argument to offset iOffset within the file. If successful, return 0.
49331** Otherwise, set pFile->lastErrno and return non-zero.
49332*/
49333static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49334#if !SQLITE_OS_WINRT
49335 LONG upperBits; /* Most sig. 32 bits of new offset */
49336 LONG lowerBits; /* Least sig. 32 bits of new offset */
49337 DWORD dwRet; /* Value returned by SetFilePointer() */
49338 DWORD lastErrno; /* Value returned by GetLastError() */
49339
49340 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
49341
49342 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
49343 lowerBits = (LONG)(iOffset & 0xffffffff);
49344
49345 /* API oddity: If successful, SetFilePointer() returns a dword
49346 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49347 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
49348 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
49349 ** whether an error has actually occurred, it is also necessary to call
49350 ** GetLastError().
49351 */
49352 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
49353
49354 if( (dwRet==INVALID_SET_FILE_POINTER
49355 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
49356 pFile->lastErrno = lastErrno;
49357 winLogError(SQLITE_IOERR_SEEK(10 | (22<<8)), pFile->lastErrno,
49358 "winSeekFile", pFile->zPath);
49359 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49360 return 1;
49361 }
49362
49363 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49364 return 0;
49365#else
49366 /*
49367 ** Same as above, except that this implementation works for WinRT.
49368 */
49369
49370 LARGE_INTEGER x; /* The new offset */
49371 BOOL bRet; /* Value returned by SetFilePointerEx() */
49372
49373 x.QuadPart = iOffset;
49374 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
49375
49376 if(!bRet){
49377 pFile->lastErrno = osGetLastError();
49378 winLogError(SQLITE_IOERR_SEEK(10 | (22<<8)), pFile->lastErrno,
49379 "winSeekFile", pFile->zPath);
49380 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49381 return 1;
49382 }
49383
49384 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49385 return 0;
49386#endif
49387}
49388
49389#if SQLITE_MAX_MMAP_SIZE20971520>0
49390/* Forward references to VFS helper methods used for memory mapped files */
49391static int winMapfile(winFile*, sqlite3_int64);
49392static int winUnmapfile(winFile*);
49393#endif
49394
49395/*
49396** Close a file.
49397**
49398** It is reported that an attempt to close a handle might sometimes
49399** fail. This is a very unreasonable result, but Windows is notorious
49400** for being unreasonable so I do not doubt that it might happen. If
49401** the close fails, we pause for 100 milliseconds and try again. As
49402** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
49403** giving up and returning an error.
49404*/
49405#define MX_CLOSE_ATTEMPT 3
49406static int winClose(sqlite3_file *id){
49407 int rc, cnt = 0;
49408 winFile *pFile = (winFile*)id;
49409
49410 assert( id!=0 )((void) (0));
49411#ifndef SQLITE_OMIT_WAL
49412 assert( pFile->pShm==0 )((void) (0));
49413#endif
49414 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE )((void) (0));
49415 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
49416 osGetCurrentProcessId(), pFile, pFile->h));
49417
49418#if SQLITE_MAX_MMAP_SIZE20971520>0
49419 winUnmapfile(pFile);
49420#endif
49421
49422 do{
49423 rc = osCloseHandle(pFile->h);
49424 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
49425 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
49426#if SQLITE_OS_WINCE
49427#define WINCE_DELETION_ATTEMPTS 3
49428 {
49429 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
49430 if( pAppData==NULL((void*)0) || !pAppData->bNoLock ){
49431 winceDestroyLock(pFile);
49432 }
49433 }
49434 if( pFile->zDeleteOnClose ){
49435 int cnt = 0;
49436 while(
49437 osDeleteFileW(pFile->zDeleteOnClose)==0
49438 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
49439 && cnt++ < WINCE_DELETION_ATTEMPTS
49440 ){
49441 sqlite3_win32_sleep(100); /* Wait a little before trying again */
49442 }
49443 sqlite3_free(pFile->zDeleteOnClose);
49444 }
49445#endif
49446 if( rc ){
49447 pFile->h = NULL((void*)0);
49448 }
49449 OpenCounter(-1);
49450 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
49451 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
49452 return rc ? SQLITE_OK0
49453 : winLogError(SQLITE_IOERR_CLOSE(10 | (16<<8)), osGetLastError(),
49454 "winClose", pFile->zPath);
49455}
49456
49457/*
49458** Read data from a file into a buffer. Return SQLITE_OK if all
49459** bytes were read successfully and SQLITE_IOERR if anything goes
49460** wrong.
49461*/
49462static int winRead(
49463 sqlite3_file *id, /* File to read from */
49464 void *pBuf, /* Write content into this buffer */
49465 int amt, /* Number of bytes to read */
49466 sqlite3_int64 offset /* Begin reading at this offset */
49467){
49468#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
49469 OVERLAPPED overlapped; /* The offset for ReadFile. */
49470#endif
49471 winFile *pFile = (winFile*)id; /* file handle */
49472 DWORD nRead; /* Number of bytes actually read from file */
49473 int nRetry = 0; /* Number of retrys */
49474
49475 assert( id!=0 )((void) (0));
49476 assert( amt>0 )((void) (0));
49477 assert( offset>=0 )((void) (0));
49478 SimulateIOError(return SQLITE_IOERR_READ);
49479 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
49480 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
49481 pFile->h, pBuf, amt, offset, pFile->locktype));
49482
49483#if SQLITE_MAX_MMAP_SIZE20971520>0
49484 /* Deal with as much of this read request as possible by transferring
49485 ** data from the memory mapping using memcpy(). */
49486 if( offset<pFile->mmapSize ){
49487 if( offset+amt <= pFile->mmapSize ){
49488 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
49489 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49490 osGetCurrentProcessId(), pFile, pFile->h));
49491 return SQLITE_OK0;
49492 }else{
49493 int nCopy = (int)(pFile->mmapSize - offset);
49494 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
49495 pBuf = &((u8 *)pBuf)[nCopy];
49496 amt -= nCopy;
49497 offset += nCopy;
49498 }
49499 }
49500#endif
49501
49502#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
49503 if( winSeekFile(pFile, offset) ){
49504 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
49505 osGetCurrentProcessId(), pFile, pFile->h));
49506 return SQLITE_FULL13;
49507 }
49508 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
49509#else
49510 memset(&overlapped, 0, sizeof(OVERLAPPED));
49511 overlapped.Offset = (LONG)(offset & 0xffffffff);
49512 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
49513 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
49514 osGetLastError()!=ERROR_HANDLE_EOF ){
49515#endif
49516 DWORD lastErrno;
49517 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
49518 pFile->lastErrno = lastErrno;
49519 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
49520 osGetCurrentProcessId(), pFile, pFile->h));
49521 return winLogError(SQLITE_IOERR_READ(10 | (1<<8)), pFile->lastErrno,
49522 "winRead", pFile->zPath);
49523 }
49524 winLogIoerr(nRetry, __LINE__49524);
49525 if( nRead<(DWORD)amt ){
49526 /* Unread parts of the buffer must be zero-filled */
49527 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
49528 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
49529 osGetCurrentProcessId(), pFile, pFile->h));
49530 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
49531 }
49532
49533 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49534 osGetCurrentProcessId(), pFile, pFile->h));
49535 return SQLITE_OK0;
49536}
49537
49538/*
49539** Write data from a buffer into a file. Return SQLITE_OK on success
49540** or some other error code on failure.
49541*/
49542static int winWrite(
49543 sqlite3_file *id, /* File to write into */
49544 const void *pBuf, /* The bytes to be written */
49545 int amt, /* Number of bytes to write */
49546 sqlite3_int64 offset /* Offset into the file to begin writing at */
49547){
49548 int rc = 0; /* True if error has occurred, else false */
49549 winFile *pFile = (winFile*)id; /* File handle */
49550 int nRetry = 0; /* Number of retries */
49551
49552 assert( amt>0 )((void) (0));
49553 assert( pFile )((void) (0));
49554 SimulateIOError(return SQLITE_IOERR_WRITE);
49555 SimulateDiskfullError(return SQLITE_FULL);
49556
49557 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
49558 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
49559 pFile->h, pBuf, amt, offset, pFile->locktype));
49560
49561#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE20971520>0
49562 /* Deal with as much of this write request as possible by transferring
49563 ** data from the memory mapping using memcpy(). */
49564 if( offset<pFile->mmapSize ){
49565 if( offset+amt <= pFile->mmapSize ){
49566 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
49567 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49568 osGetCurrentProcessId(), pFile, pFile->h));
49569 return SQLITE_OK0;
49570 }else{
49571 int nCopy = (int)(pFile->mmapSize - offset);
49572 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
49573 pBuf = &((u8 *)pBuf)[nCopy];
49574 amt -= nCopy;
49575 offset += nCopy;
49576 }
49577 }
49578#endif
49579
49580#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
49581 rc = winSeekFile(pFile, offset);
49582 if( rc==0 ){
49583#else
49584 {
49585#endif
49586#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
49587 OVERLAPPED overlapped; /* The offset for WriteFile. */
49588#endif
49589 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
49590 int nRem = amt; /* Number of bytes yet to be written */
49591 DWORD nWrite; /* Bytes written by each WriteFile() call */
49592 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
49593
49594#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
49595 memset(&overlapped, 0, sizeof(OVERLAPPED));
49596 overlapped.Offset = (LONG)(offset & 0xffffffff);
49597 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
49598#endif
49599
49600 while( nRem>0 ){
49601#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
49602 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
49603#else
49604 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
49605#endif
49606 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
49607 break;
49608 }
49609 assert( nWrite==0 || nWrite<=(DWORD)nRem )((void) (0));
49610 if( nWrite==0 || nWrite>(DWORD)nRem ){
49611 lastErrno = osGetLastError();
49612 break;
49613 }
49614#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
49615 offset += nWrite;
49616 overlapped.Offset = (LONG)(offset & 0xffffffff);
49617 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
49618#endif
49619 aRem += nWrite;
49620 nRem -= nWrite;
49621 }
49622 if( nRem>0 ){
49623 pFile->lastErrno = lastErrno;
49624 rc = 1;
49625 }
49626 }
49627
49628 if( rc ){
49629 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
49630 || ( pFile->lastErrno==ERROR_DISK_FULL )){
49631 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
49632 osGetCurrentProcessId(), pFile, pFile->h));
49633 return winLogError(SQLITE_FULL13, pFile->lastErrno,
49634 "winWrite1", pFile->zPath);
49635 }
49636 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
49637 osGetCurrentProcessId(), pFile, pFile->h));
49638 return winLogError(SQLITE_IOERR_WRITE(10 | (3<<8)), pFile->lastErrno,
49639 "winWrite2", pFile->zPath);
49640 }else{
49641 winLogIoerr(nRetry, __LINE__49641);
49642 }
49643 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49644 osGetCurrentProcessId(), pFile, pFile->h));
49645 return SQLITE_OK0;
49646}
49647
49648/*
49649** Truncate an open file to a specified size
49650*/
49651static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
49652 winFile *pFile = (winFile*)id; /* File handle object */
49653 int rc = SQLITE_OK0; /* Return code for this function */
49654 DWORD lastErrno;
49655#if SQLITE_MAX_MMAP_SIZE20971520>0
49656 sqlite3_int64 oldMmapSize;
49657 if( pFile->nFetchOut>0 ){
49658 /* File truncation is a no-op if there are outstanding memory mapped
49659 ** pages. This is because truncating the file means temporarily unmapping
49660 ** the file, and that might delete memory out from under existing cursors.
49661 **
49662 ** This can result in incremental vacuum not truncating the file,
49663 ** if there is an active read cursor when the incremental vacuum occurs.
49664 ** No real harm comes of this - the database file is not corrupted,
49665 ** though some folks might complain that the file is bigger than it
49666 ** needs to be.
49667 **
49668 ** The only feasible work-around is to defer the truncation until after
49669 ** all references to memory-mapped content are closed. That is doable,
49670 ** but involves adding a few branches in the common write code path which
49671 ** could slow down normal operations slightly. Hence, we have decided for
49672 ** now to simply make transactions a no-op if there are pending reads. We
49673 ** can maybe revisit this decision in the future.
49674 */
49675 return SQLITE_OK0;
49676 }
49677#endif
49678
49679 assert( pFile )((void) (0));
49680 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
49681 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
49682 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
49683
49684 /* If the user has configured a chunk-size for this file, truncate the
49685 ** file so that it consists of an integer number of chunks (i.e. the
49686 ** actual file size after the operation may be larger than the requested
49687 ** size).
49688 */
49689 if( pFile->szChunk>0 ){
49690 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
49691 }
49692
49693#if SQLITE_MAX_MMAP_SIZE20971520>0
49694 if( pFile->pMapRegion ){
49695 oldMmapSize = pFile->mmapSize;
49696 }else{
49697 oldMmapSize = 0;
49698 }
49699 winUnmapfile(pFile);
49700#endif
49701
49702 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
49703 if( winSeekFile(pFile, nByte) ){
49704 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
49705 "winTruncate1", pFile->zPath);
49706 }else if( 0==osSetEndOfFile(pFile->h) &&
49707 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
49708 pFile->lastErrno = lastErrno;
49709 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
49710 "winTruncate2", pFile->zPath);
49711 }
49712
49713#if SQLITE_MAX_MMAP_SIZE20971520>0
49714 if( rc==SQLITE_OK0 && oldMmapSize>0 ){
49715 if( oldMmapSize>nByte ){
49716 winMapfile(pFile, -1);
49717 }else{
49718 winMapfile(pFile, oldMmapSize);
49719 }
49720 }
49721#endif
49722
49723 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
49724 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
49725 return rc;
49726}
49727
49728#ifdef SQLITE_TEST
49729/*
49730** Count the number of fullsyncs and normal syncs. This is used to test
49731** that syncs and fullsyncs are occurring at the right times.
49732*/
49733SQLITE_API int sqlite3_sync_count = 0;
49734SQLITE_API int sqlite3_fullsync_count = 0;
49735#endif
49736
49737/*
49738** Make sure all writes to a particular file are committed to disk.
49739*/
49740static int winSync(sqlite3_file *id, int flags){
49741#ifndef SQLITE_NO_SYNC
49742 /*
49743 ** Used only when SQLITE_NO_SYNC is not defined.
49744 */
49745 BOOL rc;
49746#endif
49747#if !defined(NDEBUG1) || !defined(SQLITE_NO_SYNC) || \
49748 defined(SQLITE_HAVE_OS_TRACE)
49749 /*
49750 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
49751 ** OSTRACE() macros.
49752 */
49753 winFile *pFile = (winFile*)id;
49754#else
49755 UNUSED_PARAMETER(id)(void)(id);
49756#endif
49757
49758 assert( pFile )((void) (0));
49759 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
49760 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
49761 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
49762 )((void) (0));
49763
49764 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
49765 ** line is to test that doing so does not cause any problems.
49766 */
49767 SimulateDiskfullError( return SQLITE_FULL );
49768
49769 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
49770 osGetCurrentProcessId(), pFile, pFile->h, flags,
49771 pFile->locktype));
49772
49773#ifndef SQLITE_TEST
49774 UNUSED_PARAMETER(flags)(void)(flags);
49775#else
49776 if( (flags&0x0F)==SQLITE_SYNC_FULL0x00003 ){
49777 sqlite3_fullsync_count++;
49778 }
49779 sqlite3_sync_count++;
49780#endif
49781
49782 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
49783 ** no-op
49784 */
49785#ifdef SQLITE_NO_SYNC
49786 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49787 osGetCurrentProcessId(), pFile, pFile->h));
49788 return SQLITE_OK0;
49789#else
49790#if SQLITE_MAX_MMAP_SIZE20971520>0
49791 if( pFile->pMapRegion ){
49792 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
49793 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49794 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
49795 pFile, pFile->pMapRegion));
49796 }else{
49797 pFile->lastErrno = osGetLastError();
49798 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49799 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
49800 pFile, pFile->pMapRegion));
49801 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
49802 "winSync1", pFile->zPath);
49803 }
49804 }
49805#endif
49806 rc = osFlushFileBuffers(pFile->h);
49807 SimulateIOError( rc=FALSE );
49808 if( rc ){
49809 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49810 osGetCurrentProcessId(), pFile, pFile->h));
49811 return SQLITE_OK0;
49812 }else{
49813 pFile->lastErrno = osGetLastError();
49814 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
49815 osGetCurrentProcessId(), pFile, pFile->h));
49816 return winLogError(SQLITE_IOERR_FSYNC(10 | (4<<8)), pFile->lastErrno,
49817 "winSync2", pFile->zPath);
49818 }
49819#endif
49820}
49821
49822/*
49823** Determine the current size of a file in bytes
49824*/
49825static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
49826 winFile *pFile = (winFile*)id;
49827 int rc = SQLITE_OK0;
49828
49829 assert( id!=0 )((void) (0));
49830 assert( pSize!=0 )((void) (0));
49831 SimulateIOError(return SQLITE_IOERR_FSTAT);
49832 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
49833
49834#if SQLITE_OS_WINRT
49835 {
49836 FILE_STANDARD_INFO info;
49837 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
49838 &info, sizeof(info)) ){
49839 *pSize = info.EndOfFile.QuadPart;
49840 }else{
49841 pFile->lastErrno = osGetLastError();
49842 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
49843 "winFileSize", pFile->zPath);
49844 }
49845 }
49846#else
49847 {
49848 DWORD upperBits;
49849 DWORD lowerBits;
49850 DWORD lastErrno;
49851
49852 lowerBits = osGetFileSize(pFile->h, &upperBits);
49853 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
49854 if( (lowerBits == INVALID_FILE_SIZE)
49855 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
49856 pFile->lastErrno = lastErrno;
49857 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
49858 "winFileSize", pFile->zPath);
49859 }
49860 }
49861#endif
49862 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
49863 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
49864 return rc;
49865}
49866
49867/*
49868** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
49869*/
49870#ifndef LOCKFILE_FAIL_IMMEDIATELY
49871# define LOCKFILE_FAIL_IMMEDIATELY 1
49872#endif
49873
49874#ifndef LOCKFILE_EXCLUSIVE_LOCK
49875# define LOCKFILE_EXCLUSIVE_LOCK 2
49876#endif
49877
49878/*
49879** Historically, SQLite has used both the LockFile and LockFileEx functions.
49880** When the LockFile function was used, it was always expected to fail
49881** immediately if the lock could not be obtained. Also, it always expected to
49882** obtain an exclusive lock. These flags are used with the LockFileEx function
49883** and reflect those expectations; therefore, they should not be changed.
49884*/
49885#ifndef SQLITE_LOCKFILE_FLAGS
49886# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
49887 LOCKFILE_EXCLUSIVE_LOCK)
49888#endif
49889
49890/*
49891** Currently, SQLite never calls the LockFileEx function without wanting the
49892** call to fail immediately if the lock cannot be obtained.
49893*/
49894#ifndef SQLITE_LOCKFILEEX_FLAGS
49895# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
49896#endif
49897
49898/*
49899** Acquire a reader lock.
49900** Different API routines are called depending on whether or not this
49901** is Win9x or WinNT.
49902*/
49903static int winGetReadLock(winFile *pFile){
49904 int res;
49905 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49906 if( osIsNT() ){
49907#if SQLITE_OS_WINCE
49908 /*
49909 ** NOTE: Windows CE is handled differently here due its lack of the Win32
49910 ** API LockFileEx.
49911 */
49912 res = winceLockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, 1, 0);
49913#else
49914 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
49915 SHARED_SIZE510, 0);
49916#endif
49917 }
49918#ifdef SQLITE_WIN32_HAS_ANSI
49919 else{
49920 int lk;
49921 sqlite3_randomness(sizeof(lk), &lk);
49922 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE510 - 1));
49923 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49924 SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
49925 }
49926#endif
49927 if( res == 0 ){
49928 pFile->lastErrno = osGetLastError();
49929 /* No need to log a failure to lock */
49930 }
49931 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
49932 return res;
49933}
49934
49935/*
49936** Undo a readlock
49937*/
49938static int winUnlockReadLock(winFile *pFile){
49939 int res;
49940 DWORD lastErrno;
49941 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49942 if( osIsNT() ){
49943 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
49944 }
49945#ifdef SQLITE_WIN32_HAS_ANSI
49946 else{
49947 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
49948 }
49949#endif
49950 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
49951 pFile->lastErrno = lastErrno;
49952 winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), pFile->lastErrno,
49953 "winUnlockReadLock", pFile->zPath);
49954 }
49955 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
49956 return res;
49957}
49958
49959/*
49960** Lock the file with the lock specified by parameter locktype - one
49961** of the following:
49962**
49963** (1) SHARED_LOCK
49964** (2) RESERVED_LOCK
49965** (3) PENDING_LOCK
49966** (4) EXCLUSIVE_LOCK
49967**
49968** Sometimes when requesting one lock state, additional lock states
49969** are inserted in between. The locking might fail on one of the later
49970** transitions leaving the lock state different from what it started but
49971** still short of its goal. The following chart shows the allowed
49972** transitions and the inserted intermediate states:
49973**
49974** UNLOCKED -> SHARED
49975** SHARED -> RESERVED
49976** SHARED -> (PENDING) -> EXCLUSIVE
49977** RESERVED -> (PENDING) -> EXCLUSIVE
49978** PENDING -> EXCLUSIVE
49979**
49980** This routine will only increase a lock. The winUnlock() routine
49981** erases all locks at once and returns us immediately to locking level 0.
49982** It is not possible to lower the locking level one step at a time. You
49983** must go straight to locking level 0.
49984*/
49985static int winLock(sqlite3_file *id, int locktype){
49986 int rc = SQLITE_OK0; /* Return code from subroutines */
49987 int res = 1; /* Result of a Windows lock call */
49988 int newLocktype; /* Set pFile->locktype to this value before exiting */
49989 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
49990 winFile *pFile = (winFile*)id;
49991 DWORD lastErrno = NO_ERROR;
49992
49993 assert( id!=0 )((void) (0));
49994 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
49995 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49996
49997 /* If there is already a lock of this type or more restrictive on the
49998 ** OsFile, do nothing. Don't use the end_lock: exit path, as
49999 ** sqlite3OsEnterMutex() hasn't been called yet.
50000 */
50001 if( pFile->locktype>=locktype ){
50002 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
50003 return SQLITE_OK0;
50004 }
50005
50006 /* Do not allow any kind of write-lock on a read-only database
50007 */
50008 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK2 ){
50009 return SQLITE_IOERR_LOCK(10 | (15<<8));
50010 }
50011
50012 /* Make sure the locking sequence is correct
50013 */
50014 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK )((void) (0));
50015 assert( locktype!=PENDING_LOCK )((void) (0));
50016 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK )((void) (0));
50017
50018 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
50019 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
50020 ** the PENDING_LOCK byte is temporary.
50021 */
50022 newLocktype = pFile->locktype;
50023 if( pFile->locktype==NO_LOCK0
50024 || (locktype==EXCLUSIVE_LOCK4 && pFile->locktype<=RESERVED_LOCK2)
50025 ){
50026 int cnt = 3;
50027 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50028 PENDING_BYTEsqlite3PendingByte, 0, 1, 0))==0 ){
50029 /* Try 3 times to get the pending lock. This is needed to work
50030 ** around problems caused by indexing and/or anti-virus software on
50031 ** Windows systems.
50032 ** If you are using this code as a model for alternative VFSes, do not
50033 ** copy this retry logic. It is a hack intended for Windows only.
50034 */
50035 lastErrno = osGetLastError();
50036 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50037 pFile->h, cnt, res));
50038 if( lastErrno==ERROR_INVALID_HANDLE ){
50039 pFile->lastErrno = lastErrno;
50040 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
50041 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50042 pFile->h, cnt, sqlite3ErrName(rc)));
50043 return rc;
50044 }
50045 if( cnt ) sqlite3_win32_sleep(1);
50046 }
50047 gotPendingLock = res;
50048 if( !res ){
50049 lastErrno = osGetLastError();
50050 }
50051 }
50052
50053 /* Acquire a shared lock
50054 */
50055 if( locktype==SHARED_LOCK1 && res ){
50056 assert( pFile->locktype==NO_LOCK )((void) (0));
50057 res = winGetReadLock(pFile);
50058 if( res ){
50059 newLocktype = SHARED_LOCK1;
50060 }else{
50061 lastErrno = osGetLastError();
50062 }
50063 }
50064
50065 /* Acquire a RESERVED lock
50066 */
50067 if( locktype==RESERVED_LOCK2 && res ){
50068 assert( pFile->locktype==SHARED_LOCK )((void) (0));
50069 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
50070 if( res ){
50071 newLocktype = RESERVED_LOCK2;
50072 }else{
50073 lastErrno = osGetLastError();
50074 }
50075 }
50076
50077 /* Acquire a PENDING lock
50078 */
50079 if( locktype==EXCLUSIVE_LOCK4 && res ){
50080 newLocktype = PENDING_LOCK3;
50081 gotPendingLock = 0;
50082 }
50083
50084 /* Acquire an EXCLUSIVE lock
50085 */
50086 if( locktype==EXCLUSIVE_LOCK4 && res ){
50087 assert( pFile->locktype>=SHARED_LOCK )((void) (0));
50088 (void)winUnlockReadLock(pFile);
50089 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
50090 SHARED_SIZE510, 0);
50091 if( res ){
50092 newLocktype = EXCLUSIVE_LOCK4;
50093 }else{
50094 lastErrno = osGetLastError();
50095 winGetReadLock(pFile);
50096 }
50097 }
50098
50099 /* If we are holding a PENDING lock that ought to be released, then
50100 ** release it now.
50101 */
50102 if( gotPendingLock && locktype==SHARED_LOCK1 ){
50103 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
50104 }
50105
50106 /* Update the state of the lock has held in the file descriptor then
50107 ** return the appropriate result code.
50108 */
50109 if( res ){
50110 rc = SQLITE_OK0;
50111 }else{
50112 pFile->lastErrno = lastErrno;
50113 rc = SQLITE_BUSY5;
50114 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
50115 pFile->h, locktype, newLocktype));
50116 }
50117 pFile->locktype = (u8)newLocktype;
50118 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
50119 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50120 return rc;
50121}
50122
50123/*
50124** This routine checks if there is a RESERVED lock held on the specified
50125** file by this or any other process. If such a lock is held, return
50126** non-zero, otherwise zero.
50127*/
50128static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
50129 int res;
50130 winFile *pFile = (winFile*)id;
50131
50132 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
50133 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
50134
50135 assert( id!=0 )((void) (0));
50136 if( pFile->locktype>=RESERVED_LOCK2 ){
50137 res = 1;
50138 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
50139 }else{
50140 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE(sqlite3PendingByte+1),0,1,0);
50141 if( res ){
50142 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
50143 }
50144 res = !res;
50145 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
50146 }
50147 *pResOut = res;
50148 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
50149 pFile->h, pResOut, *pResOut));
50150 return SQLITE_OK0;
50151}
50152
50153/*
50154** Lower the locking level on file descriptor id to locktype. locktype
50155** must be either NO_LOCK or SHARED_LOCK.
50156**
50157** If the locking level of the file descriptor is already at or below
50158** the requested locking level, this routine is a no-op.
50159**
50160** It is not possible for this routine to fail if the second argument
50161** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
50162** might return SQLITE_IOERR;
50163*/
50164static int winUnlock(sqlite3_file *id, int locktype){
50165 int type;
50166 winFile *pFile = (winFile*)id;
50167 int rc = SQLITE_OK0;
50168 assert( pFile!=0 )((void) (0));
50169 assert( locktype<=SHARED_LOCK )((void) (0));
50170 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
50171 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50172 type = pFile->locktype;
50173 if( type>=EXCLUSIVE_LOCK4 ){
50174 winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
50175 if( locktype==SHARED_LOCK1 && !winGetReadLock(pFile) ){
50176 /* This should never happen. We should always be able to
50177 ** reacquire the read lock */
50178 rc = winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), osGetLastError(),
50179 "winUnlock", pFile->zPath);
50180 }
50181 }
50182 if( type>=RESERVED_LOCK2 ){
50183 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
50184 }
50185 if( locktype==NO_LOCK0 && type>=SHARED_LOCK1 ){
50186 winUnlockReadLock(pFile);
50187 }
50188 if( type>=PENDING_LOCK3 ){
50189 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
50190 }
50191 pFile->locktype = (u8)locktype;
50192 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
50193 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50194 return rc;
50195}
50196
50197/******************************************************************************
50198****************************** No-op Locking **********************************
50199**
50200** Of the various locking implementations available, this is by far the
50201** simplest: locking is ignored. No attempt is made to lock the database
50202** file for reading or writing.
50203**
50204** This locking mode is appropriate for use on read-only databases
50205** (ex: databases that are burned into CD-ROM, for example.) It can
50206** also be used if the application employs some external mechanism to
50207** prevent simultaneous access of the same database by two or more
50208** database connections. But there is a serious risk of database
50209** corruption if this locking mode is used in situations where multiple
50210** database connections are accessing the same database file at the same
50211** time and one or more of those connections are writing.
50212*/
50213
50214static int winNolockLock(sqlite3_file *id, int locktype){
50215 UNUSED_PARAMETER(id)(void)(id);
50216 UNUSED_PARAMETER(locktype)(void)(locktype);
50217 return SQLITE_OK0;
50218}
50219
50220static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
50221 UNUSED_PARAMETER(id)(void)(id);
50222 UNUSED_PARAMETER(pResOut)(void)(pResOut);
50223 return SQLITE_OK0;
50224}
50225
50226static int winNolockUnlock(sqlite3_file *id, int locktype){
50227 UNUSED_PARAMETER(id)(void)(id);
50228 UNUSED_PARAMETER(locktype)(void)(locktype);
50229 return SQLITE_OK0;
50230}
50231
50232/******************* End of the no-op lock implementation *********************
50233******************************************************************************/
50234
50235/*
50236** If *pArg is initially negative then this is a query. Set *pArg to
50237** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
50238**
50239** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
50240*/
50241static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
50242 if( *pArg<0 ){
50243 *pArg = (pFile->ctrlFlags & mask)!=0;
50244 }else if( (*pArg)==0 ){
50245 pFile->ctrlFlags &= ~mask;
50246 }else{
50247 pFile->ctrlFlags |= mask;
50248 }
50249}
50250
50251/* Forward references to VFS helper methods used for temporary files */
50252static int winGetTempname(sqlite3_vfs *, char **);
50253static int winIsDir(const void *);
50254static BOOL winIsLongPathPrefix(const char *);
50255static BOOL winIsDriveLetterAndColon(const char *);
50256
50257/*
50258** Control and query of the open file handle.
50259*/
50260static int winFileControl(sqlite3_file *id, int op, void *pArg){
50261 winFile *pFile = (winFile*)id;
50262 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
50263 switch( op ){
50264 case SQLITE_FCNTL_LOCKSTATE1: {
50265 *(int*)pArg = pFile->locktype;
50266 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50267 return SQLITE_OK0;
50268 }
50269 case SQLITE_FCNTL_LAST_ERRNO4: {
50270 *(int*)pArg = (int)pFile->lastErrno;
50271 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50272 return SQLITE_OK0;
50273 }
50274 case SQLITE_FCNTL_CHUNK_SIZE6: {
50275 pFile->szChunk = *(int *)pArg;
50276 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50277 return SQLITE_OK0;
50278 }
50279 case SQLITE_FCNTL_SIZE_HINT5: {
50280 if( pFile->szChunk>0 ){
50281 sqlite3_int64 oldSz;
50282 int rc = winFileSize(id, &oldSz);
50283 if( rc==SQLITE_OK0 ){
50284 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
50285 if( newSz>oldSz ){
50286 SimulateIOErrorBenign(1);
50287 rc = winTruncate(id, newSz);
50288 SimulateIOErrorBenign(0);
50289 }
50290 }
50291 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50292 return rc;
50293 }
50294 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50295 return SQLITE_OK0;
50296 }
50297 case SQLITE_FCNTL_PERSIST_WAL10: {
50298 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
50299 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50300 return SQLITE_OK0;
50301 }
50302 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
50303 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
50304 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50305 return SQLITE_OK0;
50306 }
50307 case SQLITE_FCNTL_VFSNAME12: {
50308 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
50309 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50310 return SQLITE_OK0;
50311 }
50312 case SQLITE_FCNTL_WIN32_AV_RETRY9: {
50313 int *a = (int*)pArg;
50314 if( a[0]>0 ){
50315 winIoerrRetry = a[0];
50316 }else{
50317 a[0] = winIoerrRetry;
50318 }
50319 if( a[1]>0 ){
50320 winIoerrRetryDelay = a[1];
50321 }else{
50322 a[1] = winIoerrRetryDelay;
50323 }
50324 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50325 return SQLITE_OK0;
50326 }
50327 case SQLITE_FCNTL_WIN32_GET_HANDLE29: {
50328 LPHANDLE phFile = (LPHANDLE)pArg;
50329 *phFile = pFile->h;
50330 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50331 return SQLITE_OK0;
50332 }
50333#ifdef SQLITE_TEST
50334 case SQLITE_FCNTL_WIN32_SET_HANDLE23: {
50335 LPHANDLE phFile = (LPHANDLE)pArg;
50336 HANDLE hOldFile = pFile->h;
50337 pFile->h = *phFile;
50338 *phFile = hOldFile;
50339 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
50340 hOldFile, pFile->h));
50341 return SQLITE_OK0;
50342 }
50343#endif
50344 case SQLITE_FCNTL_TEMPFILENAME16: {
50345 char *zTFile = 0;
50346 int rc = winGetTempname(pFile->pVfs, &zTFile);
50347 if( rc==SQLITE_OK0 ){
50348 *(char**)pArg = zTFile;
50349 }
50350 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50351 return rc;
50352 }
50353#if SQLITE_MAX_MMAP_SIZE20971520>0
50354 case SQLITE_FCNTL_MMAP_SIZE18: {
50355 i64 newLimit = *(i64*)pArg;
50356 int rc = SQLITE_OK0;
50357 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
50358 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
50359 }
50360
50361 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
50362 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
50363 ** least a 64-bit type. */
50364 if( newLimit>0 && sizeof(SIZE_T)<8 ){
50365 newLimit = (newLimit & 0x7FFFFFFF);
50366 }
50367
50368 *(i64*)pArg = pFile->mmapSizeMax;
50369 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
50370 pFile->mmapSizeMax = newLimit;
50371 if( pFile->mmapSize>0 ){
50372 winUnmapfile(pFile);
50373 rc = winMapfile(pFile, -1);
50374 }
50375 }
50376 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50377 return rc;
50378 }
50379#endif
50380 }
50381 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
50382 return SQLITE_NOTFOUND12;
50383}
50384
50385/*
50386** Return the sector size in bytes of the underlying block device for
50387** the specified file. This is almost always 512 bytes, but may be
50388** larger for some devices.
50389**
50390** SQLite code assumes this function cannot fail. It also assumes that
50391** if two files are created in the same file-system directory (i.e.
50392** a database and its journal file) that the sector size will be the
50393** same for both.
50394*/
50395static int winSectorSize(sqlite3_file *id){
50396 (void)id;
50397 return SQLITE_DEFAULT_SECTOR_SIZE4096;
50398}
50399
50400/*
50401** Return a vector of device characteristics.
50402*/
50403static int winDeviceCharacteristics(sqlite3_file *id){
50404 winFile *p = (winFile*)id;
50405 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 | SQLITE_IOCAP_SUBPAGE_READ0x00008000 |
50406 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000:0);
50407}
50408
50409/*
50410** Windows will only let you create file view mappings
50411** on allocation size granularity boundaries.
50412** During sqlite3_os_init() we do a GetSystemInfo()
50413** to get the granularity size.
50414*/
50415static SYSTEM_INFO winSysInfo;
50416
50417#ifndef SQLITE_OMIT_WAL
50418
50419/*
50420** Helper functions to obtain and relinquish the global mutex. The
50421** global mutex is used to protect the winLockInfo objects used by
50422** this file, all of which may be shared by multiple threads.
50423**
50424** Function winShmMutexHeld() is used to assert() that the global mutex
50425** is held when required. This function is only used as part of assert()
50426** statements. e.g.
50427**
50428** winShmEnterMutex()
50429** assert( winShmMutexHeld() );
50430** winShmLeaveMutex()
50431*/
50432static sqlite3_mutex *winBigLock = 0;
50433static void winShmEnterMutex(void){
50434 sqlite3_mutex_enter(winBigLock);
50435}
50436static void winShmLeaveMutex(void){
50437 sqlite3_mutex_leave(winBigLock);
50438}
50439#ifndef NDEBUG1
50440static int winShmMutexHeld(void) {
50441 return sqlite3_mutex_held(winBigLock);
50442}
50443#endif
50444
50445/*
50446** Object used to represent a single file opened and mmapped to provide
50447** shared memory. When multiple threads all reference the same
50448** log-summary, each thread has its own winFile object, but they all
50449** point to a single instance of this object. In other words, each
50450** log-summary is opened only once per process.
50451**
50452** winShmMutexHeld() must be true when creating or destroying
50453** this object or while reading or writing the following fields:
50454**
50455** nRef
50456** pNext
50457**
50458** The following fields are read-only after the object is created:
50459**
50460** fid
50461** zFilename
50462**
50463** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
50464** winShmMutexHeld() is true when reading or writing any other field
50465** in this structure.
50466**
50467*/
50468struct winShmNode {
50469 sqlite3_mutex *mutex; /* Mutex to access this object */
50470 char *zFilename; /* Name of the file */
50471 winFile hFile; /* File handle from winOpen */
50472
50473 int szRegion; /* Size of shared-memory regions */
50474 int nRegion; /* Size of array apRegion */
50475 u8 isReadonly; /* True if read-only */
50476 u8 isUnlocked; /* True if no DMS lock held */
50477
50478 struct ShmRegion {
50479 HANDLE hMap; /* File handle from CreateFileMapping */
50480 void *pMap;
50481 } *aRegion;
50482 DWORD lastErrno; /* The Windows errno from the last I/O error */
50483
50484 int nRef; /* Number of winShm objects pointing to this */
50485 winShm *pFirst; /* All winShm objects pointing to this */
50486 winShmNode *pNext; /* Next in list of all winShmNode objects */
50487#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50488 u8 nextShmId; /* Next available winShm.id value */
50489#endif
50490};
50491
50492/*
50493** A global array of all winShmNode objects.
50494**
50495** The winShmMutexHeld() must be true while reading or writing this list.
50496*/
50497static winShmNode *winShmNodeList = 0;
50498
50499/*
50500** Structure used internally by this VFS to record the state of an
50501** open shared memory connection.
50502**
50503** The following fields are initialized when this object is created and
50504** are read-only thereafter:
50505**
50506** winShm.pShmNode
50507** winShm.id
50508**
50509** All other fields are read/write. The winShm.pShmNode->mutex must be held
50510** while accessing any read/write fields.
50511*/
50512struct winShm {
50513 winShmNode *pShmNode; /* The underlying winShmNode object */
50514 winShm *pNext; /* Next winShm with the same winShmNode */
50515 u8 hasMutex; /* True if holding the winShmNode mutex */
50516 u16 sharedMask; /* Mask of shared locks held */
50517 u16 exclMask; /* Mask of exclusive locks held */
50518#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50519 u8 id; /* Id of this connection with its winShmNode */
50520#endif
50521};
50522
50523/*
50524** Constants used for locking
50525*/
50526#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
50527#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK8) /* deadman switch */
50528
50529/*
50530** Apply advisory locks for all n bytes beginning at ofst.
50531*/
50532#define WINSHM_UNLCK 1
50533#define WINSHM_RDLCK 2
50534#define WINSHM_WRLCK 3
50535static int winShmSystemLock(
50536 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
50537 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
50538 int ofst, /* Offset to first byte to be locked/unlocked */
50539 int nByte /* Number of bytes to lock or unlock */
50540){
50541 int rc = 0; /* Result code form Lock/UnlockFileEx() */
50542
50543 /* Access to the winShmNode object is serialized by the caller */
50544 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) )((void) (0));
50545
50546 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
50547 pFile->hFile.h, lockType, ofst, nByte));
50548
50549 /* Release/Acquire the system-level lock */
50550 if( lockType==WINSHM_UNLCK ){
50551 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
50552 }else{
50553 /* Initialize the locking parameters */
50554 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
50555 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
50556 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
50557 }
50558
50559 if( rc!= 0 ){
50560 rc = SQLITE_OK0;
50561 }else{
50562 pFile->lastErrno = osGetLastError();
50563 rc = SQLITE_BUSY5;
50564 }
50565
50566 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
50567 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
50568 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
50569
50570 return rc;
50571}
50572
50573/* Forward references to VFS methods */
50574static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
50575static int winDelete(sqlite3_vfs *,const char*,int);
50576
50577/*
50578** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
50579**
50580** This is not a VFS shared-memory method; it is a utility function called
50581** by VFS shared-memory methods.
50582*/
50583static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
50584 winShmNode **pp;
50585 winShmNode *p;
50586 assert( winShmMutexHeld() )((void) (0));
50587 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
50588 osGetCurrentProcessId(), deleteFlag));
50589 pp = &winShmNodeList;
50590 while( (p = *pp)!=0 ){
50591 if( p->nRef==0 ){
50592 int i;
50593 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
50594 for(i=0; i<p->nRegion; i++){
50595 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
50596 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
50597 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
50598 UNUSED_VARIABLE_VALUE(bRc);
50599 bRc = osCloseHandle(p->aRegion[i].hMap);
50600 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
50601 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
50602 UNUSED_VARIABLE_VALUE(bRc);
50603 }
50604 if( p->hFile.h!=NULL((void*)0) && p->hFile.h!=INVALID_HANDLE_VALUE ){
50605 SimulateIOErrorBenign(1);
50606 winClose((sqlite3_file *)&p->hFile);
50607 SimulateIOErrorBenign(0);
50608 }
50609 if( deleteFlag ){
50610 SimulateIOErrorBenign(1);
50611 sqlite3BeginBenignMalloc();
50612 winDelete(pVfs, p->zFilename, 0);
50613 sqlite3EndBenignMalloc();
50614 SimulateIOErrorBenign(0);
50615 }
50616 *pp = p->pNext;
50617 sqlite3_free(p->aRegion);
50618 sqlite3_free(p);
50619 }else{
50620 pp = &p->pNext;
50621 }
50622 }
50623}
50624
50625/*
50626** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
50627** take it now. Return SQLITE_OK if successful, or an SQLite error
50628** code otherwise.
50629**
50630** If the DMS cannot be locked because this is a readonly_shm=1
50631** connection and no other process already holds a lock, return
50632** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
50633*/
50634static int winLockSharedMemory(winShmNode *pShmNode){
50635 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
50636
50637 if( rc==SQLITE_OK0 ){
50638 if( pShmNode->isReadonly ){
50639 pShmNode->isUnlocked = 1;
50640 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50641 return SQLITE_READONLY_CANTINIT(8 | (5<<8));
50642 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
50643 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50644 return winLogError(SQLITE_IOERR_SHMOPEN(10 | (18<<8)), osGetLastError(),
50645 "winLockSharedMemory", pShmNode->zFilename);
50646 }
50647 }
50648
50649 if( rc==SQLITE_OK0 ){
50650 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50651 }
50652
50653 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
50654}
50655
50656/*
50657** Open the shared-memory area associated with database file pDbFd.
50658**
50659** When opening a new shared-memory file, if no other instances of that
50660** file are currently open, in this process or in other processes, then
50661** the file must be truncated to zero length or have its header cleared.
50662*/
50663static int winOpenSharedMemory(winFile *pDbFd){
50664 struct winShm *p; /* The connection to be opened */
50665 winShmNode *pShmNode = 0; /* The underlying mmapped file */
50666 int rc = SQLITE_OK0; /* Result code */
50667 winShmNode *pNew; /* Newly allocated winShmNode */
50668 int nName; /* Size of zName in bytes */
50669
50670 assert( pDbFd->pShm==0 )((void) (0)); /* Not previously opened */
50671
50672 /* Allocate space for the new sqlite3_shm object. Also speculatively
50673 ** allocate space for a new winShmNode and filename.
50674 */
50675 p = sqlite3MallocZero( sizeof(*p) );
50676 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
50677 nName = sqlite3Strlen30(pDbFd->zPath);
50678 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
50679 if( pNew==0 ){
50680 sqlite3_free(p);
50681 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
50682 }
50683 pNew->zFilename = (char*)&pNew[1];
50684 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
50685 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
50686
50687 /* Look to see if there is an existing winShmNode that can be used.
50688 ** If no matching winShmNode currently exists, create a new one.
50689 */
50690 winShmEnterMutex();
50691 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
50692 /* TBD need to come up with better match here. Perhaps
50693 ** use FILE_ID_BOTH_DIR_INFO Structure.
50694 */
50695 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
50696 }
50697 if( pShmNode ){
50698 sqlite3_free(pNew);
50699 }else{
50700 int inFlags = SQLITE_OPEN_WAL0x00080000;
50701 int outFlags = 0;
50702
50703 pShmNode = pNew;
50704 pNew = 0;
50705 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50706 pShmNode->pNext = winShmNodeList;
50707 winShmNodeList = pShmNode;
50708
50709 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
50710 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
50711 if( pShmNode->mutex==0 ){
50712 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
50713 goto shm_open_err;
50714 }
50715 }
50716
50717 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50718 inFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004;
50719 }else{
50720 inFlags |= SQLITE_OPEN_READONLY0x00000001;
50721 }
50722 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50723 (sqlite3_file*)&pShmNode->hFile,
50724 inFlags, &outFlags);
50725 if( rc!=SQLITE_OK0 ){
50726 rc = winLogError(rc, osGetLastError(), "winOpenShm",
50727 pShmNode->zFilename);
50728 goto shm_open_err;
50729 }
50730 if( outFlags==SQLITE_OPEN_READONLY0x00000001 ) pShmNode->isReadonly = 1;
50731
50732 rc = winLockSharedMemory(pShmNode);
50733 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
50734 }
50735
50736 /* Make the new connection a child of the winShmNode */
50737 p->pShmNode = pShmNode;
50738#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50739 p->id = pShmNode->nextShmId++;
50740#endif
50741 pShmNode->nRef++;
50742 pDbFd->pShm = p;
50743 winShmLeaveMutex();
50744
50745 /* The reference count on pShmNode has already been incremented under
50746 ** the cover of the winShmEnterMutex() mutex and the pointer from the
50747 ** new (struct winShm) object to the pShmNode has been set. All that is
50748 ** left to do is to link the new object into the linked list starting
50749 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50750 ** mutex.
50751 */
50752 sqlite3_mutex_enter(pShmNode->mutex);
50753 p->pNext = pShmNode->pFirst;
50754 pShmNode->pFirst = p;
50755 sqlite3_mutex_leave(pShmNode->mutex);
50756 return rc;
50757
50758 /* Jump here on any error */
50759shm_open_err:
50760 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50761 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50762 sqlite3_free(p);
50763 sqlite3_free(pNew);
50764 winShmLeaveMutex();
50765 return rc;
50766}
50767
50768/*
50769** Close a connection to shared-memory. Delete the underlying
50770** storage if deleteFlag is true.
50771*/
50772static int winShmUnmap(
50773 sqlite3_file *fd, /* Database holding shared memory */
50774 int deleteFlag /* Delete after closing if true */
50775){
50776 winFile *pDbFd; /* Database holding shared-memory */
50777 winShm *p; /* The connection to be closed */
50778 winShmNode *pShmNode; /* The underlying shared-memory file */
50779 winShm **pp; /* For looping over sibling connections */
50780
50781 pDbFd = (winFile*)fd;
50782 p = pDbFd->pShm;
50783 if( p==0 ) return SQLITE_OK0;
50784 pShmNode = p->pShmNode;
50785
50786 /* Remove connection p from the set of connections associated
50787 ** with pShmNode */
50788 sqlite3_mutex_enter(pShmNode->mutex);
50789 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50790 *pp = p->pNext;
50791
50792 /* Free the connection p */
50793 sqlite3_free(p);
50794 pDbFd->pShm = 0;
50795 sqlite3_mutex_leave(pShmNode->mutex);
50796
50797 /* If pShmNode->nRef has reached 0, then close the underlying
50798 ** shared-memory file, too */
50799 winShmEnterMutex();
50800 assert( pShmNode->nRef>0 )((void) (0));
50801 pShmNode->nRef--;
50802 if( pShmNode->nRef==0 ){
50803 winShmPurge(pDbFd->pVfs, deleteFlag);
50804 }
50805 winShmLeaveMutex();
50806
50807 return SQLITE_OK0;
50808}
50809
50810/*
50811** Change the lock state for a shared-memory segment.
50812*/
50813static int winShmLock(
50814 sqlite3_file *fd, /* Database file holding the shared memory */
50815 int ofst, /* First lock to acquire or release */
50816 int n, /* Number of locks to acquire or release */
50817 int flags /* What to do with the lock */
50818){
50819 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
50820 winShm *p = pDbFd->pShm; /* The shared memory being locked */
50821 winShm *pX; /* For looping over all siblings */
50822 winShmNode *pShmNode;
50823 int rc = SQLITE_OK0; /* Result code */
50824 u16 mask; /* Mask of locks to take or release */
50825
50826 if( p==0 ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
50827 pShmNode = p->pShmNode;
50828 if( NEVER(pShmNode==0)(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK(10 | (20<<8));
50829
50830 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
50831 assert( n>=1 )((void) (0));
50832 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
50833 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
50834 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
50835 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
50836 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
50837
50838 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
50839 assert( n>1 || mask==(1<<ofst) )((void) (0));
50840 sqlite3_mutex_enter(pShmNode->mutex);
50841 if( flags & SQLITE_SHM_UNLOCK1 ){
50842 u16 allMask = 0; /* Mask of locks held by siblings */
50843
50844 /* See if any siblings hold this same lock */
50845 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50846 if( pX==p ) continue;
50847 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 )((void) (0));
50848 allMask |= pX->sharedMask;
50849 }
50850
50851 /* Unlock the system-level locks */
50852 if( (mask & allMask)==0 ){
50853 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
50854 }else{
50855 rc = SQLITE_OK0;
50856 }
50857
50858 /* Undo the local locks */
50859 if( rc==SQLITE_OK0 ){
50860 p->exclMask &= ~mask;
50861 p->sharedMask &= ~mask;
50862 }
50863 }else if( flags & SQLITE_SHM_SHARED4 ){
50864 u16 allShared = 0; /* Union of locks held by connections other than "p" */
50865
50866 /* Find out which shared locks are already held by sibling connections.
50867 ** If any sibling already holds an exclusive lock, go ahead and return
50868 ** SQLITE_BUSY.
50869 */
50870 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50871 if( (pX->exclMask & mask)!=0 ){
50872 rc = SQLITE_BUSY5;
50873 break;
50874 }
50875 allShared |= pX->sharedMask;
50876 }
50877
50878 /* Get shared locks at the system level, if necessary */
50879 if( rc==SQLITE_OK0 ){
50880 if( (allShared & mask)==0 ){
50881 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
50882 }else{
50883 rc = SQLITE_OK0;
50884 }
50885 }
50886
50887 /* Get the local shared locks */
50888 if( rc==SQLITE_OK0 ){
50889 p->sharedMask |= mask;
50890 }
50891 }else{
50892 /* Make sure no sibling connections hold locks that will block this
50893 ** lock. If any do, return SQLITE_BUSY right away.
50894 */
50895 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50896 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
50897 rc = SQLITE_BUSY5;
50898 break;
50899 }
50900 }
50901
50902 /* Get the exclusive locks at the system level. Then if successful
50903 ** also mark the local connection as being locked.
50904 */
50905 if( rc==SQLITE_OK0 ){
50906 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
50907 if( rc==SQLITE_OK0 ){
50908 assert( (p->sharedMask & mask)==0 )((void) (0));
50909 p->exclMask |= mask;
50910 }
50911 }
50912 }
50913 sqlite3_mutex_leave(pShmNode->mutex);
50914 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
50915 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
50916 sqlite3ErrName(rc)));
50917 return rc;
50918}
50919
50920/*
50921** Implement a memory barrier or memory fence on shared memory.
50922**
50923** All loads and stores begun before the barrier must complete before
50924** any load or store begun after the barrier.
50925*/
50926static void winShmBarrier(
50927 sqlite3_file *fd /* Database holding the shared memory */
50928){
50929 UNUSED_PARAMETER(fd)(void)(fd);
50930 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
50931 winShmEnterMutex(); /* Also mutex, for redundancy */
50932 winShmLeaveMutex();
50933}
50934
50935/*
50936** This function is called to obtain a pointer to region iRegion of the
50937** shared-memory associated with the database file fd. Shared-memory regions
50938** are numbered starting from zero. Each shared-memory region is szRegion
50939** bytes in size.
50940**
50941** If an error occurs, an error code is returned and *pp is set to NULL.
50942**
50943** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
50944** region has not been allocated (by any client, including one running in a
50945** separate process), then *pp is set to NULL and SQLITE_OK returned. If
50946** isWrite is non-zero and the requested shared-memory region has not yet
50947** been allocated, it is allocated by this function.
50948**
50949** If the shared-memory region has already been allocated or is allocated by
50950** this call as described above, then it is mapped into this processes
50951** address space (if it is not already), *pp is set to point to the mapped
50952** memory and SQLITE_OK returned.
50953*/
50954static int winShmMap(
50955 sqlite3_file *fd, /* Handle open on database file */
50956 int iRegion, /* Region to retrieve */
50957 int szRegion, /* Size of regions */
50958 int isWrite, /* True to extend file if necessary */
50959 void volatile **pp /* OUT: Mapped memory */
50960){
50961 winFile *pDbFd = (winFile*)fd;
50962 winShm *pShm = pDbFd->pShm;
50963 winShmNode *pShmNode;
50964 DWORD protect = PAGE_READWRITE;
50965 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
50966 int rc = SQLITE_OK0;
50967
50968 if( !pShm ){
50969 rc = winOpenSharedMemory(pDbFd);
50970 if( rc!=SQLITE_OK0 ) return rc;
50971 pShm = pDbFd->pShm;
50972 assert( pShm!=0 )((void) (0));
50973 }
50974 pShmNode = pShm->pShmNode;
50975
50976 sqlite3_mutex_enter(pShmNode->mutex);
50977 if( pShmNode->isUnlocked ){
50978 rc = winLockSharedMemory(pShmNode);
50979 if( rc!=SQLITE_OK0 ) goto shmpage_out;
50980 pShmNode->isUnlocked = 0;
50981 }
50982 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
50983
50984 if( pShmNode->nRegion<=iRegion ){
50985 struct ShmRegion *apNew; /* New aRegion[] array */
50986 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
50987 sqlite3_int64 sz; /* Current size of wal-index file */
50988
50989 pShmNode->szRegion = szRegion;
50990
50991 /* The requested region is not mapped into this processes address space.
50992 ** Check to see if it has been allocated (i.e. if the wal-index file is
50993 ** large enough to contain the requested region).
50994 */
50995 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
50996 if( rc!=SQLITE_OK0 ){
50997 rc = winLogError(SQLITE_IOERR_SHMSIZE(10 | (19<<8)), osGetLastError(),
50998 "winShmMap1", pDbFd->zPath);
50999 goto shmpage_out;
51000 }
51001
51002 if( sz<nByte ){
51003 /* The requested memory region does not exist. If isWrite is set to
51004 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
51005 **
51006 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51007 ** the requested memory region.
51008 */
51009 if( !isWrite ) goto shmpage_out;
51010 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
51011 if( rc!=SQLITE_OK0 ){
51012 rc = winLogError(SQLITE_IOERR_SHMSIZE(10 | (19<<8)), osGetLastError(),
51013 "winShmMap2", pDbFd->zPath);
51014 goto shmpage_out;
51015 }
51016 }
51017
51018 /* Map the requested memory region into this processes address space. */
51019 apNew = (struct ShmRegion *)sqlite3_realloc64(
51020 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
51021 );
51022 if( !apNew ){
51023 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51024 goto shmpage_out;
51025 }
51026 pShmNode->aRegion = apNew;
51027
51028 if( pShmNode->isReadonly ){
51029 protect = PAGE_READONLY;
51030 flags = FILE_MAP_READ;
51031 }
51032
51033 while( pShmNode->nRegion<=iRegion ){
51034 HANDLE hMap = NULL((void*)0); /* file-mapping handle */
51035 void *pMap = 0; /* Mapped memory region */
51036
51037#if SQLITE_OS_WINRT
51038 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
51039 NULL((void*)0), protect, nByte, NULL((void*)0)
51040 );
51041#elif defined(SQLITE_WIN32_HAS_WIDE)
51042 hMap = osCreateFileMappingW(pShmNode->hFile.h,
51043 NULL((void*)0), protect, 0, nByte, NULL((void*)0)
51044 );
51045#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51046 hMap = osCreateFileMappingA(pShmNode->hFile.h,
51047 NULL((void*)0), protect, 0, nByte, NULL((void*)0)
51048 );
51049#endif
51050 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
51051 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
51052 hMap ? "ok" : "failed"));
51053 if( hMap ){
51054 int iOffset = pShmNode->nRegion*szRegion;
51055 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
51056#if SQLITE_OS_WINRT
51057 pMap = osMapViewOfFileFromApp(hMap, flags,
51058 iOffset - iOffsetShift, szRegion + iOffsetShift
51059 );
51060#else
51061 pMap = osMapViewOfFile(hMap, flags,
51062 0, iOffset - iOffsetShift, szRegion + iOffsetShift
51063 );
51064#endif
51065 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
51066 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
51067 szRegion, pMap ? "ok" : "failed"));
51068 }
51069 if( !pMap ){
51070 pShmNode->lastErrno = osGetLastError();
51071 rc = winLogError(SQLITE_IOERR_SHMMAP(10 | (21<<8)), pShmNode->lastErrno,
51072 "winShmMap3", pDbFd->zPath);
51073 if( hMap ) osCloseHandle(hMap);
51074 goto shmpage_out;
51075 }
51076
51077 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
51078 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
51079 pShmNode->nRegion++;
51080 }
51081 }
51082
51083shmpage_out:
51084 if( pShmNode->nRegion>iRegion ){
51085 int iOffset = iRegion*szRegion;
51086 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
51087 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51088 *pp = (void *)&p[iOffsetShift];
51089 }else{
51090 *pp = 0;
51091 }
51092 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
51093 sqlite3_mutex_leave(pShmNode->mutex);
51094 return rc;
51095}
51096
51097#else
51098# define winShmMap 0
51099# define winShmLock 0
51100# define winShmBarrier 0
51101# define winShmUnmap 0
51102#endif /* #ifndef SQLITE_OMIT_WAL */
51103
51104/*
51105** Cleans up the mapped region of the specified file, if any.
51106*/
51107#if SQLITE_MAX_MMAP_SIZE20971520>0
51108static int winUnmapfile(winFile *pFile){
51109 assert( pFile!=0 )((void) (0));
51110 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
51111 "mmapSize=%lld, mmapSizeMax=%lld\n",
51112 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
51113 pFile->mmapSize, pFile->mmapSizeMax));
51114 if( pFile->pMapRegion ){
51115 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
51116 pFile->lastErrno = osGetLastError();
51117 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
51118 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
51119 pFile->pMapRegion));
51120 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
51121 "winUnmapfile1", pFile->zPath);
51122 }
51123 pFile->pMapRegion = 0;
51124 pFile->mmapSize = 0;
51125 }
51126 if( pFile->hMap!=NULL((void*)0) ){
51127 if( !osCloseHandle(pFile->hMap) ){
51128 pFile->lastErrno = osGetLastError();
51129 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
51130 osGetCurrentProcessId(), pFile, pFile->hMap));
51131 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
51132 "winUnmapfile2", pFile->zPath);
51133 }
51134 pFile->hMap = NULL((void*)0);
51135 }
51136 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51137 osGetCurrentProcessId(), pFile));
51138 return SQLITE_OK0;
51139}
51140
51141/*
51142** Memory map or remap the file opened by file-descriptor pFd (if the file
51143** is already mapped, the existing mapping is replaced by the new). Or, if
51144** there already exists a mapping for this file, and there are still
51145** outstanding xFetch() references to it, this function is a no-op.
51146**
51147** If parameter nByte is non-negative, then it is the requested size of
51148** the mapping to create. Otherwise, if nByte is less than zero, then the
51149** requested size is the size of the file on disk. The actual size of the
51150** created mapping is either the requested size or the value configured
51151** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
51152**
51153** SQLITE_OK is returned if no error occurs (even if the mapping is not
51154** recreated as a result of outstanding references) or an SQLite error
51155** code otherwise.
51156*/
51157static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
51158 sqlite3_int64 nMap = nByte;
51159 int rc;
51160
51161 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
51162 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
51163 osGetCurrentProcessId(), pFd, nByte));
51164
51165 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
51166
51167 if( nMap<0 ){
51168 rc = winFileSize((sqlite3_file*)pFd, &nMap);
51169 if( rc ){
51170 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
51171 osGetCurrentProcessId(), pFd));
51172 return SQLITE_IOERR_FSTAT(10 | (7<<8));
51173 }
51174 }
51175 if( nMap>pFd->mmapSizeMax ){
51176 nMap = pFd->mmapSizeMax;
51177 }
51178 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
51179
51180 if( nMap==0 && pFd->mmapSize>0 ){
51181 winUnmapfile(pFd);
51182 }
51183 if( nMap!=pFd->mmapSize ){
51184 void *pNew = 0;
51185 DWORD protect = PAGE_READONLY;
51186 DWORD flags = FILE_MAP_READ;
51187
51188 winUnmapfile(pFd);
51189#ifdef SQLITE_MMAP_READWRITE
51190 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
51191 protect = PAGE_READWRITE;
51192 flags |= FILE_MAP_WRITE;
51193 }
51194#endif
51195#if SQLITE_OS_WINRT
51196 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL((void*)0), protect, nMap, NULL((void*)0));
51197#elif defined(SQLITE_WIN32_HAS_WIDE)
51198 pFd->hMap = osCreateFileMappingW(pFd->h, NULL((void*)0), protect,
51199 (DWORD)((nMap>>32) & 0xffffffff),
51200 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
51201#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51202 pFd->hMap = osCreateFileMappingA(pFd->h, NULL((void*)0), protect,
51203 (DWORD)((nMap>>32) & 0xffffffff),
51204 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
51205#endif
51206 if( pFd->hMap==NULL((void*)0) ){
51207 pFd->lastErrno = osGetLastError();
51208 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
51209 "winMapfile1", pFd->zPath);
51210 /* Log the error, but continue normal operation using xRead/xWrite */
51211 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
51212 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
51213 return SQLITE_OK0;
51214 }
51215 assert( (nMap % winSysInfo.dwPageSize)==0 )((void) (0));
51216 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff )((void) (0));
51217#if SQLITE_OS_WINRT
51218 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
51219#else
51220 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
51221#endif
51222 if( pNew==NULL((void*)0) ){
51223 osCloseHandle(pFd->hMap);
51224 pFd->hMap = NULL((void*)0);
51225 pFd->lastErrno = osGetLastError();
51226 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
51227 "winMapfile2", pFd->zPath);
51228 /* Log the error, but continue normal operation using xRead/xWrite */
51229 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
51230 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
51231 return SQLITE_OK0;
51232 }
51233 pFd->pMapRegion = pNew;
51234 pFd->mmapSize = nMap;
51235 }
51236
51237 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51238 osGetCurrentProcessId(), pFd));
51239 return SQLITE_OK0;
51240}
51241#endif /* SQLITE_MAX_MMAP_SIZE>0 */
51242
51243/*
51244** If possible, return a pointer to a mapping of file fd starting at offset
51245** iOff. The mapping must be valid for at least nAmt bytes.
51246**
51247** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
51248** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
51249** Finally, if an error does occur, return an SQLite error code. The final
51250** value of *pp is undefined in this case.
51251**
51252** If this function does return a pointer, the caller must eventually
51253** release the reference by calling winUnfetch().
51254*/
51255static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
51256#if SQLITE_MAX_MMAP_SIZE20971520>0
51257 winFile *pFd = (winFile*)fd; /* The underlying database file */
51258#endif
51259 *pp = 0;
51260
51261 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
51262 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
51263
51264#if SQLITE_MAX_MMAP_SIZE20971520>0
51265 if( pFd->mmapSizeMax>0 ){
51266 /* Ensure that there is always at least a 256 byte buffer of addressable
51267 ** memory following the returned page. If the database is corrupt,
51268 ** SQLite may overread the page slightly (in practice only a few bytes,
51269 ** but 256 is safe, round, number). */
51270 const int nEofBuffer = 256;
51271 if( pFd->pMapRegion==0 ){
51272 int rc = winMapfile(pFd, -1);
51273 if( rc!=SQLITE_OK0 ){
51274 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
51275 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
51276 return rc;
51277 }
51278 }
51279 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
51280 assert( pFd->pMapRegion!=0 )((void) (0));
51281 *pp = &((u8 *)pFd->pMapRegion)[iOff];
51282 pFd->nFetchOut++;
51283 }
51284 }
51285#endif
51286
51287 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
51288 osGetCurrentProcessId(), fd, pp, *pp));
51289 return SQLITE_OK0;
51290}
51291
51292/*
51293** If the third argument is non-NULL, then this function releases a
51294** reference obtained by an earlier call to winFetch(). The second
51295** argument passed to this function must be the same as the corresponding
51296** argument that was passed to the winFetch() invocation.
51297**
51298** Or, if the third argument is NULL, then this function is being called
51299** to inform the VFS layer that, according to POSIX, any existing mapping
51300** may now be invalid and should be unmapped.
51301*/
51302static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
51303#if SQLITE_MAX_MMAP_SIZE20971520>0
51304 winFile *pFd = (winFile*)fd; /* The underlying database file */
51305
51306 /* If p==0 (unmap the entire file) then there must be no outstanding
51307 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
51308 ** then there must be at least one outstanding. */
51309 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
51310
51311 /* If p!=0, it must match the iOff value. */
51312 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
51313
51314 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
51315 osGetCurrentProcessId(), pFd, iOff, p));
51316
51317 if( p ){
51318 pFd->nFetchOut--;
51319 }else{
51320 /* FIXME: If Windows truly always prevents truncating or deleting a
51321 ** file while a mapping is held, then the following winUnmapfile() call
51322 ** is unnecessary can be omitted - potentially improving
51323 ** performance. */
51324 winUnmapfile(pFd);
51325 }
51326
51327 assert( pFd->nFetchOut>=0 )((void) (0));
51328#endif
51329
51330 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51331 osGetCurrentProcessId(), fd));
51332 return SQLITE_OK0;
51333}
51334
51335/*
51336** Here ends the implementation of all sqlite3_file methods.
51337**
51338********************** End sqlite3_file Methods *******************************
51339******************************************************************************/
51340
51341/*
51342** This vector defines all the methods that can operate on an
51343** sqlite3_file for win32.
51344*/
51345static const sqlite3_io_methods winIoMethod = {
51346 3, /* iVersion */
51347 winClose, /* xClose */
51348 winRead, /* xRead */
51349 winWrite, /* xWrite */
51350 winTruncate, /* xTruncate */
51351 winSync, /* xSync */
51352 winFileSize, /* xFileSize */
51353 winLock, /* xLock */
51354 winUnlock, /* xUnlock */
51355 winCheckReservedLock, /* xCheckReservedLock */
51356 winFileControl, /* xFileControl */
51357 winSectorSize, /* xSectorSize */
51358 winDeviceCharacteristics, /* xDeviceCharacteristics */
51359 winShmMap, /* xShmMap */
51360 winShmLock, /* xShmLock */
51361 winShmBarrier, /* xShmBarrier */
51362 winShmUnmap, /* xShmUnmap */
51363 winFetch, /* xFetch */
51364 winUnfetch /* xUnfetch */
51365};
51366
51367/*
51368** This vector defines all the methods that can operate on an
51369** sqlite3_file for win32 without performing any locking.
51370*/
51371static const sqlite3_io_methods winIoNolockMethod = {
51372 3, /* iVersion */
51373 winClose, /* xClose */
51374 winRead, /* xRead */
51375 winWrite, /* xWrite */
51376 winTruncate, /* xTruncate */
51377 winSync, /* xSync */
51378 winFileSize, /* xFileSize */
51379 winNolockLock, /* xLock */
51380 winNolockUnlock, /* xUnlock */
51381 winNolockCheckReservedLock, /* xCheckReservedLock */
51382 winFileControl, /* xFileControl */
51383 winSectorSize, /* xSectorSize */
51384 winDeviceCharacteristics, /* xDeviceCharacteristics */
51385 winShmMap, /* xShmMap */
51386 winShmLock, /* xShmLock */
51387 winShmBarrier, /* xShmBarrier */
51388 winShmUnmap, /* xShmUnmap */
51389 winFetch, /* xFetch */
51390 winUnfetch /* xUnfetch */
51391};
51392
51393static winVfsAppData winAppData = {
51394 &winIoMethod, /* pMethod */
51395 0, /* pAppData */
51396 0 /* bNoLock */
51397};
51398
51399static winVfsAppData winNolockAppData = {
51400 &winIoNolockMethod, /* pMethod */
51401 0, /* pAppData */
51402 1 /* bNoLock */
51403};
51404
51405/****************************************************************************
51406**************************** sqlite3_vfs methods ****************************
51407**
51408** This division contains the implementation of methods on the
51409** sqlite3_vfs object.
51410*/
51411
51412#if defined(__CYGWIN__)
51413/*
51414** Convert a filename from whatever the underlying operating system
51415** supports for filenames into UTF-8. Space to hold the result is
51416** obtained from malloc and must be freed by the calling function.
51417*/
51418static char *winConvertToUtf8Filename(const void *zFilename){
51419 char *zConverted = 0;
51420 if( osIsNT() ){
51421 zConverted = winUnicodeToUtf8(zFilename);
51422 }
51423#ifdef SQLITE_WIN32_HAS_ANSI
51424 else{
51425 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
51426 }
51427#endif
51428 /* caller will handle out of memory */
51429 return zConverted;
51430}
51431#endif
51432
51433/*
51434** Convert a UTF-8 filename into whatever form the underlying
51435** operating system wants filenames in. Space to hold the result
51436** is obtained from malloc and must be freed by the calling
51437** function.
51438*/
51439static void *winConvertFromUtf8Filename(const char *zFilename){
51440 void *zConverted = 0;
51441 if( osIsNT() ){
51442 zConverted = winUtf8ToUnicode(zFilename);
51443 }
51444#ifdef SQLITE_WIN32_HAS_ANSI
51445 else{
51446 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51447 }
51448#endif
51449 /* caller will handle out of memory */
51450 return zConverted;
51451}
51452
51453/*
51454** This function returns non-zero if the specified UTF-8 string buffer
51455** ends with a directory separator character or one was successfully
51456** added to it.
51457*/
51458static int winMakeEndInDirSep(int nBuf, char *zBuf){
51459 if( zBuf ){
51460 int nLen = sqlite3Strlen30(zBuf);
51461 if( nLen>0 ){
51462 if( winIsDirSep(zBuf[nLen-1]) ){
51463 return 1;
51464 }else if( nLen+1<nBuf ){
51465 zBuf[nLen] = winGetDirSep();
51466 zBuf[nLen+1] = '\0';
51467 return 1;
51468 }
51469 }
51470 }
51471 return 0;
51472}
51473
51474/*
51475** If sqlite3_temp_directory is defined, take the mutex and return true.
51476**
51477** If sqlite3_temp_directory is NULL (undefined), omit the mutex and
51478** return false.
51479*/
51480static int winTempDirDefined(void){
51481 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
51482 if( sqlite3_temp_directory!=0 ) return 1;
51483 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
51484 return 0;
51485}
51486
51487/*
51488** Create a temporary file name and store the resulting pointer into pzBuf.
51489** The pointer returned in pzBuf must be freed via sqlite3_free().
51490*/
51491static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
51492 static char zChars[] =
51493 "abcdefghijklmnopqrstuvwxyz"
51494 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51495 "0123456789";
51496 size_t i, j;
51497 DWORD pid;
51498 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX"etilqs_");
51499 int nMax, nBuf, nDir, nLen;
51500 char *zBuf;
51501
51502 /* It's odd to simulate an io-error here, but really this is just
51503 ** using the io-error infrastructure to test that SQLite handles this
51504 ** function failing.
51505 */
51506 SimulateIOError( return SQLITE_IOERR );
51507
51508 /* Allocate a temporary buffer to store the fully qualified file
51509 ** name for the temporary file. If this fails, we cannot continue.
51510 */
51511 nMax = pVfs->mxPathname; nBuf = nMax + 2;
51512 zBuf = sqlite3MallocZero( nBuf );
51513 if( !zBuf ){
51514 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51515 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51516 }
51517
51518 /* Figure out the effective temporary directory. First, check if one
51519 ** has been explicitly set by the application; otherwise, use the one
51520 ** configured by the operating system.
51521 */
51522 nDir = nMax - (nPre + 15);
51523 assert( nDir>0 )((void) (0));
51524 if( winTempDirDefined() ){
51525 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
51526 if( nDirLen>0 ){
51527 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
51528 nDirLen++;
51529 }
51530 if( nDirLen>nDir ){
51531 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
51532 sqlite3_free(zBuf);
51533 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51534 return winLogError(SQLITE_ERROR1, 0, "winGetTempname1", 0);
51535 }
51536 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
51537 }
51538 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
51539 }
51540
51541#if defined(__CYGWIN__)
51542 else{
51543 static const char *azDirs[] = {
51544 0, /* getenv("SQLITE_TMPDIR") */
51545 0, /* getenv("TMPDIR") */
51546 0, /* getenv("TMP") */
51547 0, /* getenv("TEMP") */
51548 0, /* getenv("USERPROFILE") */
51549 "/var/tmp",
51550 "/usr/tmp",
51551 "/tmp",
51552 ".",
51553 0 /* List terminator */
51554 };
51555 unsigned int i;
51556 const char *zDir = 0;
51557
51558 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
51559 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
51560 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
51561 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
51562 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
51563 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
51564 void *zConverted;
51565 if( zDir==0 ) continue;
51566 /* If the path starts with a drive letter followed by the colon
51567 ** character, assume it is already a native Win32 path; otherwise,
51568 ** it must be converted to a native Win32 path via the Cygwin API
51569 ** prior to using it.
51570 */
51571 if( winIsDriveLetterAndColon(zDir) ){
51572 zConverted = winConvertFromUtf8Filename(zDir);
51573 if( !zConverted ){
51574 sqlite3_free(zBuf);
51575 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51576 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51577 }
51578 if( winIsDir(zConverted) ){
51579 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
51580 sqlite3_free(zConverted);
51581 break;
51582 }
51583 sqlite3_free(zConverted);
51584 }else{
51585 zConverted = sqlite3MallocZero( nMax+1 );
51586 if( !zConverted ){
51587 sqlite3_free(zBuf);
51588 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51589 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51590 }
51591 if( cygwin_conv_path(
51592 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
51593 zConverted, nMax+1)<0 ){
51594 sqlite3_free(zConverted);
51595 sqlite3_free(zBuf);
51596 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
51597 return winLogError(SQLITE_IOERR_CONVPATH(10 | (26<<8)), (DWORD)errno(*__errno_location ()),
51598 "winGetTempname2", zDir);
51599 }
51600 if( winIsDir(zConverted) ){
51601 /* At this point, we know the candidate directory exists and should
51602 ** be used. However, we may need to convert the string containing
51603 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
51604 */
51605 char *zUtf8 = winConvertToUtf8Filename(zConverted);
51606 if( !zUtf8 ){
51607 sqlite3_free(zConverted);
51608 sqlite3_free(zBuf);
51609 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51610 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51611 }
51612 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
51613 sqlite3_free(zUtf8);
51614 sqlite3_free(zConverted);
51615 break;
51616 }
51617 sqlite3_free(zConverted);
51618 }
51619 }
51620 }
51621#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
51622 else if( osIsNT() ){
51623 char *zMulti;
51624 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
51625 if( !zWidePath ){
51626 sqlite3_free(zBuf);
51627 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51628 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51629 }
51630 if( osGetTempPathW(nMax, zWidePath)==0 ){
51631 sqlite3_free(zWidePath);
51632 sqlite3_free(zBuf);
51633 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
51634 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
51635 "winGetTempname2", 0);
51636 }
51637 zMulti = winUnicodeToUtf8(zWidePath);
51638 if( zMulti ){
51639 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
51640 sqlite3_free(zMulti);
51641 sqlite3_free(zWidePath);
51642 }else{
51643 sqlite3_free(zWidePath);
51644 sqlite3_free(zBuf);
51645 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51646 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51647 }
51648 }
51649#ifdef SQLITE_WIN32_HAS_ANSI
51650 else{
51651 char *zUtf8;
51652 char *zMbcsPath = sqlite3MallocZero( nMax );
51653 if( !zMbcsPath ){
51654 sqlite3_free(zBuf);
51655 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51656 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51657 }
51658 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
51659 sqlite3_free(zBuf);
51660 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
51661 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
51662 "winGetTempname3", 0);
51663 }
51664 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
51665 if( zUtf8 ){
51666 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
51667 sqlite3_free(zUtf8);
51668 }else{
51669 sqlite3_free(zBuf);
51670 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51671 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51672 }
51673 }
51674#endif /* SQLITE_WIN32_HAS_ANSI */
51675#endif /* !SQLITE_OS_WINRT */
51676
51677 /*
51678 ** Check to make sure the temporary directory ends with an appropriate
51679 ** separator. If it does not and there is not enough space left to add
51680 ** one, fail.
51681 */
51682 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
51683 sqlite3_free(zBuf);
51684 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51685 return winLogError(SQLITE_ERROR1, 0, "winGetTempname4", 0);
51686 }
51687
51688 /*
51689 ** Check that the output buffer is large enough for the temporary file
51690 ** name in the following format:
51691 **
51692 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
51693 **
51694 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
51695 ** account for the space used by the 15 character random suffix and the
51696 ** two trailing NUL characters. The final directory separator character
51697 ** has already added if it was not already present.
51698 */
51699 nLen = sqlite3Strlen30(zBuf);
51700 if( (nLen + nPre + 17) > nBuf ){
51701 sqlite3_free(zBuf);
51702 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51703 return winLogError(SQLITE_ERROR1, 0, "winGetTempname5", 0);
51704 }
51705
51706 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX"etilqs_");
51707
51708 j = sqlite3Strlen30(zBuf);
51709 sqlite3_randomness(15, &zBuf[j]);
51710 pid = osGetCurrentProcessId();
51711 for(i=0; i<15; i++, j++){
51712 zBuf[j] += pid & 0xff;
51713 pid >>= 8;
51714 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
51715 }
51716 zBuf[j] = 0;
51717 zBuf[j+1] = 0;
51718 *pzBuf = zBuf;
51719
51720 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
51721 return SQLITE_OK0;
51722}
51723
51724/*
51725** Return TRUE if the named file is really a directory. Return false if
51726** it is something other than a directory, or if there is any kind of memory
51727** allocation failure.
51728*/
51729static int winIsDir(const void *zConverted){
51730 DWORD attr;
51731 int rc = 0;
51732 DWORD lastErrno;
51733
51734 if( osIsNT() ){
51735 int cnt = 0;
51736 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
51737 memset(&sAttrData, 0, sizeof(sAttrData));
51738 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
51739 GetFileExInfoStandard,
51740 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
51741 if( !rc ){
51742 return 0; /* Invalid name? */
51743 }
51744 attr = sAttrData.dwFileAttributes;
51745#if SQLITE_OS_WINCE==0
51746 }else{
51747 attr = osGetFileAttributesA((char*)zConverted);
51748#endif
51749 }
51750 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
51751}
51752
51753/* forward reference */
51754static int winAccess(
51755 sqlite3_vfs *pVfs, /* Not used on win32 */
51756 const char *zFilename, /* Name of file to check */
51757 int flags, /* Type of test to make on this file */
51758 int *pResOut /* OUT: Result */
51759);
51760
51761/*
51762** Open a file.
51763*/
51764static int winOpen(
51765 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
51766 const char *zName, /* Name of the file (UTF-8) */
51767 sqlite3_file *id, /* Write the SQLite file handle here */
51768 int flags, /* Open mode flags */
51769 int *pOutFlags /* Status return flags */
51770){
51771 HANDLE h;
51772 DWORD lastErrno = 0;
51773 DWORD dwDesiredAccess;
51774 DWORD dwShareMode;
51775 DWORD dwCreationDisposition;
51776 DWORD dwFlagsAndAttributes = 0;
51777#if SQLITE_OS_WINCE
51778 int isTemp = 0;
51779#endif
51780 winVfsAppData *pAppData;
51781 winFile *pFile = (winFile*)id;
51782 void *zConverted; /* Filename in OS encoding */
51783 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
51784 int cnt = 0;
51785
51786 /* If argument zPath is a NULL pointer, this function is required to open
51787 ** a temporary file. Use this buffer to store the file name in.
51788 */
51789 char *zTmpname = 0; /* For temporary filename, if necessary. */
51790
51791 int rc = SQLITE_OK0; /* Function Return Code */
51792#if !defined(NDEBUG1) || SQLITE_OS_WINCE
51793 int eType = flags&0x0FFF00; /* Type of file to open */
51794#endif
51795
51796 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
51797 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
51798 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
51799 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
51800 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
51801
51802#ifndef NDEBUG1
51803 int isOpenJournal = (isCreate && (
51804 eType==SQLITE_OPEN_SUPER_JOURNAL0x00004000
51805 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
51806 || eType==SQLITE_OPEN_WAL0x00080000
51807 ));
51808#endif
51809
51810 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
51811 zUtf8Name, id, flags, pOutFlags));
51812
51813 /* Check the following statements are true:
51814 **
51815 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
51816 ** (b) if CREATE is set, then READWRITE must also be set, and
51817 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
51818 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
51819 */
51820 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
51821 assert(isCreate==0 || isReadWrite)((void) (0));
51822 assert(isExclusive==0 || isCreate)((void) (0));
51823 assert(isDelete==0 || isCreate)((void) (0));
51824
51825 /* The main DB, main journal, WAL file and super-journal are never
51826 ** automatically deleted. Nor are they ever temporary files. */
51827 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
51828 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
51829 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL )((void) (0));
51830 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
51831
51832 /* Assert that the upper layer has set one of the "file-type" flags. */
51833 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
51834 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
51835 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL((void) (0))
51836 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
51837 )((void) (0));
51838
51839 assert( pFile!=0 )((void) (0));
51840 memset(pFile, 0, sizeof(winFile));
51841 pFile->h = INVALID_HANDLE_VALUE;
51842
51843#if SQLITE_OS_WINRT
51844 if( !zUtf8Name && !sqlite3_temp_directory ){
51845 sqlite3_log(SQLITE_ERROR1,
51846 "sqlite3_temp_directory variable should be set for WinRT");
51847 }
51848#endif
51849
51850 /* If the second argument to this function is NULL, generate a
51851 ** temporary file name to use
51852 */
51853 if( !zUtf8Name ){
51854 assert( isDelete && !isOpenJournal )((void) (0));
51855 rc = winGetTempname(pVfs, &zTmpname);
51856 if( rc!=SQLITE_OK0 ){
51857 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
51858 return rc;
51859 }
51860 zUtf8Name = zTmpname;
51861 }
51862
51863 /* Database filenames are double-zero terminated if they are not
51864 ** URIs with parameters. Hence, they can always be passed into
51865 ** sqlite3_uri_parameter().
51866 */
51867 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||((void) (0))
51868 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 )((void) (0));
51869
51870 /* Convert the filename to the system encoding. */
51871 zConverted = winConvertFromUtf8Filename(zUtf8Name);
51872 if( zConverted==0 ){
51873 sqlite3_free(zTmpname);
51874 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
51875 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
51876 }
51877
51878 if( winIsDir(zConverted) ){
51879 sqlite3_free(zConverted);
51880 sqlite3_free(zTmpname);
51881 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
51882 return SQLITE_CANTOPEN_ISDIR(14 | (2<<8));
51883 }
51884
51885 if( isReadWrite ){
51886 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
51887 }else{
51888 dwDesiredAccess = GENERIC_READ;
51889 }
51890
51891 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
51892 ** created. SQLite doesn't use it to indicate "exclusive access"
51893 ** as it is usually understood.
51894 */
51895 if( isExclusive ){
51896 /* Creates a new file, only if it does not already exist. */
51897 /* If the file exists, it fails. */
51898 dwCreationDisposition = CREATE_NEW;
51899 }else if( isCreate ){
51900 /* Open existing file, or create if it doesn't exist */
51901 dwCreationDisposition = OPEN_ALWAYS;
51902 }else{
51903 /* Opens a file, only if it exists. */
51904 dwCreationDisposition = OPEN_EXISTING;
51905 }
51906
51907 if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){
51908 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
51909 }else{
51910 dwShareMode = 0;
51911 }
51912
51913 if( isDelete ){
51914#if SQLITE_OS_WINCE
51915 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
51916 isTemp = 1;
51917#else
51918 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
51919 | FILE_ATTRIBUTE_HIDDEN
51920 | FILE_FLAG_DELETE_ON_CLOSE;
51921#endif
51922 }else{
51923 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
51924 }
51925 /* Reports from the internet are that performance is always
51926 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
51927#if SQLITE_OS_WINCE
51928 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
51929#endif
51930
51931 if( osIsNT() ){
51932#if SQLITE_OS_WINRT
51933 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
51934 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
51935 extendedParameters.dwFileAttributes =
51936 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
51937 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
51938 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
51939 extendedParameters.lpSecurityAttributes = NULL((void*)0);
51940 extendedParameters.hTemplateFile = NULL((void*)0);
51941 do{
51942 h = osCreateFile2((LPCWSTR)zConverted,
51943 dwDesiredAccess,
51944 dwShareMode,
51945 dwCreationDisposition,
51946 &extendedParameters);
51947 if( h!=INVALID_HANDLE_VALUE ) break;
51948 if( isReadWrite ){
51949 int rc2, isRO = 0;
51950 sqlite3BeginBenignMalloc();
51951 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ2, &isRO);
51952 sqlite3EndBenignMalloc();
51953 if( rc2==SQLITE_OK0 && isRO ) break;
51954 }
51955 }while( winRetryIoerr(&cnt, &lastErrno) );
51956#else
51957 do{
51958 h = osCreateFileW((LPCWSTR)zConverted,
51959 dwDesiredAccess,
51960 dwShareMode, NULL((void*)0),
51961 dwCreationDisposition,
51962 dwFlagsAndAttributes,
51963 NULL((void*)0));
51964 if( h!=INVALID_HANDLE_VALUE ) break;
51965 if( isReadWrite ){
51966 int rc2, isRO = 0;
51967 sqlite3BeginBenignMalloc();
51968 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ2, &isRO);
51969 sqlite3EndBenignMalloc();
51970 if( rc2==SQLITE_OK0 && isRO ) break;
51971 }
51972 }while( winRetryIoerr(&cnt, &lastErrno) );
51973#endif
51974 }
51975#ifdef SQLITE_WIN32_HAS_ANSI
51976 else{
51977 do{
51978 h = osCreateFileA((LPCSTR)zConverted,
51979 dwDesiredAccess,
51980 dwShareMode, NULL((void*)0),
51981 dwCreationDisposition,
51982 dwFlagsAndAttributes,
51983 NULL((void*)0));
51984 if( h!=INVALID_HANDLE_VALUE ) break;
51985 if( isReadWrite ){
51986 int rc2, isRO = 0;
51987 sqlite3BeginBenignMalloc();
51988 rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ2, &isRO);
51989 sqlite3EndBenignMalloc();
51990 if( rc2==SQLITE_OK0 && isRO ) break;
51991 }
51992 }while( winRetryIoerr(&cnt, &lastErrno) );
51993 }
51994#endif
51995 winLogIoerr(cnt, __LINE__51995);
51996
51997 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
51998 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
51999
52000 if( h==INVALID_HANDLE_VALUE ){
52001 sqlite3_free(zConverted);
52002 sqlite3_free(zTmpname);
52003 if( isReadWrite && !isExclusive ){
52004 return winOpen(pVfs, zName, id,
52005 ((flags|SQLITE_OPEN_READONLY0x00000001) &
52006 ~(SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002)),
52007 pOutFlags);
52008 }else{
52009 pFile->lastErrno = lastErrno;
52010 winLogError(SQLITE_CANTOPEN14, pFile->lastErrno, "winOpen", zUtf8Name);
52011 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(52011);
52012 }
52013 }
52014
52015 if( pOutFlags ){
52016 if( isReadWrite ){
52017 *pOutFlags = SQLITE_OPEN_READWRITE0x00000002;
52018 }else{
52019 *pOutFlags = SQLITE_OPEN_READONLY0x00000001;
52020 }
52021 }
52022
52023 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
52024 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
52025 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
52026
52027 pAppData = (winVfsAppData*)pVfs->pAppData;
52028
52029#if SQLITE_OS_WINCE
52030 {
52031 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB0x00000100
52032 && ((pAppData==NULL((void*)0)) || !pAppData->bNoLock)
52033 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK0
52034 ){
52035 osCloseHandle(h);
52036 sqlite3_free(zConverted);
52037 sqlite3_free(zTmpname);
52038 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
52039 return rc;
52040 }
52041 }
52042 if( isTemp ){
52043 pFile->zDeleteOnClose = zConverted;
52044 }else
52045#endif
52046 {
52047 sqlite3_free(zConverted);
52048 }
52049
52050 sqlite3_free(zTmpname);
52051 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
52052 pFile->pVfs = pVfs;
52053 pFile->h = h;
52054 if( isReadonly ){
52055 pFile->ctrlFlags |= WINFILE_RDONLY;
52056 }
52057 if( (flags & SQLITE_OPEN_MAIN_DB0x00000100)
52058 && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE1)
52059 ){
52060 pFile->ctrlFlags |= WINFILE_PSOW;
52061 }
52062 pFile->lastErrno = NO_ERROR;
52063 pFile->zPath = zName;
52064#if SQLITE_MAX_MMAP_SIZE20971520>0
52065 pFile->hMap = NULL((void*)0);
52066 pFile->pMapRegion = 0;
52067 pFile->mmapSize = 0;
52068 pFile->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
52069#endif
52070
52071 OpenCounter(+1);
52072 return rc;
52073}
52074
52075/*
52076** Delete the named file.
52077**
52078** Note that Windows does not allow a file to be deleted if some other
52079** process has it open. Sometimes a virus scanner or indexing program
52080** will open a journal file shortly after it is created in order to do
52081** whatever it does. While this other process is holding the
52082** file open, we will be unable to delete it. To work around this
52083** problem, we delay 100 milliseconds and try to delete again. Up
52084** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
52085** up and returning an error.
52086*/
52087static int winDelete(
52088 sqlite3_vfs *pVfs, /* Not used on win32 */
52089 const char *zFilename, /* Name of file to delete */
52090 int syncDir /* Not used on win32 */
52091){
52092 int cnt = 0;
52093 int rc;
52094 DWORD attr;
52095 DWORD lastErrno = 0;
52096 void *zConverted;
52097 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52098 UNUSED_PARAMETER(syncDir)(void)(syncDir);
52099
52100 SimulateIOError(return SQLITE_IOERR_DELETE);
52101 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
52102
52103 zConverted = winConvertFromUtf8Filename(zFilename);
52104 if( zConverted==0 ){
52105 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
52106 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52107 }
52108 if( osIsNT() ){
52109 do {
52110#if SQLITE_OS_WINRT
52111 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
52112 memset(&sAttrData, 0, sizeof(sAttrData));
52113 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
52114 &sAttrData) ){
52115 attr = sAttrData.dwFileAttributes;
52116 }else{
52117 lastErrno = osGetLastError();
52118 if( lastErrno==ERROR_FILE_NOT_FOUND
52119 || lastErrno==ERROR_PATH_NOT_FOUND ){
52120 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
52121 }else{
52122 rc = SQLITE_ERROR1;
52123 }
52124 break;
52125 }
52126#else
52127 attr = osGetFileAttributesW(zConverted);
52128#endif
52129 if ( attr==INVALID_FILE_ATTRIBUTES ){
52130 lastErrno = osGetLastError();
52131 if( lastErrno==ERROR_FILE_NOT_FOUND
52132 || lastErrno==ERROR_PATH_NOT_FOUND ){
52133 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
52134 }else{
52135 rc = SQLITE_ERROR1;
52136 }
52137 break;
52138 }
52139 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
52140 rc = SQLITE_ERROR1; /* Files only. */
52141 break;
52142 }
52143 if ( osDeleteFileW(zConverted) ){
52144 rc = SQLITE_OK0; /* Deleted OK. */
52145 break;
52146 }
52147 if ( !winRetryIoerr(&cnt, &lastErrno) ){
52148 rc = SQLITE_ERROR1; /* No more retries. */
52149 break;
52150 }
52151 } while(1);
52152 }
52153#ifdef SQLITE_WIN32_HAS_ANSI
52154 else{
52155 do {
52156 attr = osGetFileAttributesA(zConverted);
52157 if ( attr==INVALID_FILE_ATTRIBUTES ){
52158 lastErrno = osGetLastError();
52159 if( lastErrno==ERROR_FILE_NOT_FOUND
52160 || lastErrno==ERROR_PATH_NOT_FOUND ){
52161 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
52162 }else{
52163 rc = SQLITE_ERROR1;
52164 }
52165 break;
52166 }
52167 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
52168 rc = SQLITE_ERROR1; /* Files only. */
52169 break;
52170 }
52171 if ( osDeleteFileA(zConverted) ){
52172 rc = SQLITE_OK0; /* Deleted OK. */
52173 break;
52174 }
52175 if ( !winRetryIoerr(&cnt, &lastErrno) ){
52176 rc = SQLITE_ERROR1; /* No more retries. */
52177 break;
52178 }
52179 } while(1);
52180 }
52181#endif
52182 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) ){
52183 rc = winLogError(SQLITE_IOERR_DELETE(10 | (10<<8)), lastErrno, "winDelete", zFilename);
52184 }else{
52185 winLogIoerr(cnt, __LINE__52185);
52186 }
52187 sqlite3_free(zConverted);
52188 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
52189 return rc;
52190}
52191
52192/*
52193** Check the existence and status of a file.
52194*/
52195static int winAccess(
52196 sqlite3_vfs *pVfs, /* Not used on win32 */
52197 const char *zFilename, /* Name of file to check */
52198 int flags, /* Type of test to make on this file */
52199 int *pResOut /* OUT: Result */
52200){
52201 DWORD attr;
52202 int rc = 0;
52203 DWORD lastErrno = 0;
52204 void *zConverted;
52205 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52206
52207 SimulateIOError( return SQLITE_IOERR_ACCESS; );
52208 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
52209 zFilename, flags, pResOut));
52210
52211 if( zFilename==0 ){
52212 *pResOut = 0;
52213 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
52214 zFilename, pResOut, *pResOut));
52215 return SQLITE_OK0;
52216 }
52217
52218 zConverted = winConvertFromUtf8Filename(zFilename);
52219 if( zConverted==0 ){
52220 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
52221 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52222 }
52223 if( osIsNT() ){
52224 int cnt = 0;
52225 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
52226 memset(&sAttrData, 0, sizeof(sAttrData));
52227 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
52228 GetFileExInfoStandard,
52229 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
52230 if( rc ){
52231 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
52232 ** as if it does not exist.
52233 */
52234 if( flags==SQLITE_ACCESS_EXISTS0
52235 && sAttrData.nFileSizeHigh==0
52236 && sAttrData.nFileSizeLow==0 ){
52237 attr = INVALID_FILE_ATTRIBUTES;
52238 }else{
52239 attr = sAttrData.dwFileAttributes;
52240 }
52241 }else{
52242 winLogIoerr(cnt, __LINE__52242);
52243 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
52244 sqlite3_free(zConverted);
52245 return winLogError(SQLITE_IOERR_ACCESS(10 | (13<<8)), lastErrno, "winAccess",
52246 zFilename);
52247 }else{
52248 attr = INVALID_FILE_ATTRIBUTES;
52249 }
52250 }
52251 }
52252#ifdef SQLITE_WIN32_HAS_ANSI
52253 else{
52254 attr = osGetFileAttributesA((char*)zConverted);
52255 }
52256#endif
52257 sqlite3_free(zConverted);
52258 switch( flags ){
52259 case SQLITE_ACCESS_READ2:
52260 case SQLITE_ACCESS_EXISTS0:
52261 rc = attr!=INVALID_FILE_ATTRIBUTES;
52262 break;
52263 case SQLITE_ACCESS_READWRITE1:
52264 rc = attr!=INVALID_FILE_ATTRIBUTES &&
52265 (attr & FILE_ATTRIBUTE_READONLY)==0;
52266 break;
52267 default:
52268 assert(!"Invalid flags argument")((void) (0));
52269 }
52270 *pResOut = rc;
52271 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
52272 zFilename, pResOut, *pResOut));
52273 return SQLITE_OK0;
52274}
52275
52276/*
52277** Returns non-zero if the specified path name starts with the "long path"
52278** prefix.
52279*/
52280static BOOL winIsLongPathPrefix(
52281 const char *zPathname
52282){
52283 return ( zPathname[0]=='\\' && zPathname[1]=='\\'
52284 && zPathname[2]=='?' && zPathname[3]=='\\' );
52285}
52286
52287/*
52288** Returns non-zero if the specified path name starts with a drive letter
52289** followed by a colon character.
52290*/
52291static BOOL winIsDriveLetterAndColon(
52292 const char *zPathname
52293){
52294 return ( sqlite3Isalpha(zPathname[0])(sqlite3CtypeMap[(unsigned char)(zPathname[0])]&0x02) && zPathname[1]==':' );
52295}
52296
52297/*
52298** Returns non-zero if the specified path name should be used verbatim. If
52299** non-zero is returned from this function, the calling function must simply
52300** use the provided path name verbatim -OR- resolve it into a full path name
52301** using the GetFullPathName Win32 API function (if available).
52302*/
52303static BOOL winIsVerbatimPathname(
52304 const char *zPathname
52305){
52306 /*
52307 ** If the path name starts with a forward slash or a backslash, it is either
52308 ** a legal UNC name, a volume relative path, or an absolute path name in the
52309 ** "Unix" format on Windows. There is no easy way to differentiate between
52310 ** the final two cases; therefore, we return the safer return value of TRUE
52311 ** so that callers of this function will simply use it verbatim.
52312 */
52313 if ( winIsDirSep(zPathname[0]) ){
52314 return TRUE;
52315 }
52316
52317 /*
52318 ** If the path name starts with a letter and a colon it is either a volume
52319 ** relative path or an absolute path. Callers of this function must not
52320 ** attempt to treat it as a relative path name (i.e. they should simply use
52321 ** it verbatim).
52322 */
52323 if ( winIsDriveLetterAndColon(zPathname) ){
52324 return TRUE;
52325 }
52326
52327 /*
52328 ** If we get to this point, the path name should almost certainly be a purely
52329 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
52330 */
52331 return FALSE;
52332}
52333
52334/*
52335** Turn a relative pathname into a full pathname. Write the full
52336** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
52337** bytes in size.
52338*/
52339static int winFullPathnameNoMutex(
52340 sqlite3_vfs *pVfs, /* Pointer to vfs object */
52341 const char *zRelative, /* Possibly relative input path */
52342 int nFull, /* Size of output buffer in bytes */
52343 char *zFull /* Output buffer */
52344){
52345#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
52346 DWORD nByte;
52347 void *zConverted;
52348 char *zOut;
52349#endif
52350
52351 /* If this path name begins with "/X:" or "\\?\", where "X" is any
52352 ** alphabetic character, discard the initial "/" from the pathname.
52353 */
52354 if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
52355 || winIsLongPathPrefix(zRelative+1)) ){
52356 zRelative++;
52357 }
52358
52359#if defined(__CYGWIN__)
52360 SimulateIOError( return SQLITE_ERROR );
52361 UNUSED_PARAMETER(nFull)(void)(nFull);
52362 assert( nFull>=pVfs->mxPathname )((void) (0));
52363 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
52364 /*
52365 ** NOTE: We are dealing with a relative path name and the data
52366 ** directory has been set. Therefore, use it as the basis
52367 ** for converting the relative path name to an absolute
52368 ** one by prepending the data directory and a slash.
52369 */
52370 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52371 if( !zOut ){
52372 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52373 }
52374 if( cygwin_conv_path(
52375 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
52376 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
52377 sqlite3_free(zOut);
52378 return winLogError(SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)), (DWORD)errno(*__errno_location ()),
52379 "winFullPathname1", zRelative);
52380 }else{
52381 char *zUtf8 = winConvertToUtf8Filename(zOut);
52382 if( !zUtf8 ){
52383 sqlite3_free(zOut);
52384 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52385 }
52386 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
52387 sqlite3_data_directory, winGetDirSep(), zUtf8);
52388 sqlite3_free(zUtf8);
52389 sqlite3_free(zOut);
52390 }
52391 }else{
52392 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52393 if( !zOut ){
52394 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52395 }
52396 if( cygwin_conv_path(
52397 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
52398 zRelative, zOut, pVfs->mxPathname+1)<0 ){
52399 sqlite3_free(zOut);
52400 return winLogError(SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)), (DWORD)errno(*__errno_location ()),
52401 "winFullPathname2", zRelative);
52402 }else{
52403 char *zUtf8 = winConvertToUtf8Filename(zOut);
52404 if( !zUtf8 ){
52405 sqlite3_free(zOut);
52406 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52407 }
52408 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zUtf8);
52409 sqlite3_free(zUtf8);
52410 sqlite3_free(zOut);
52411 }
52412 }
52413 return SQLITE_OK0;
52414#endif
52415
52416#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
52417 SimulateIOError( return SQLITE_ERROR );
52418 /* WinCE has no concept of a relative pathname, or so I am told. */
52419 /* WinRT has no way to convert a relative path to an absolute one. */
52420 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
52421 /*
52422 ** NOTE: We are dealing with a relative path name and the data
52423 ** directory has been set. Therefore, use it as the basis
52424 ** for converting the relative path name to an absolute
52425 ** one by prepending the data directory and a backslash.
52426 */
52427 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
52428 sqlite3_data_directory, winGetDirSep(), zRelative);
52429 }else{
52430 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zRelative);
52431 }
52432 return SQLITE_OK0;
52433#endif
52434
52435#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
52436 /* It's odd to simulate an io-error here, but really this is just
52437 ** using the io-error infrastructure to test that SQLite handles this
52438 ** function failing. This function could fail if, for example, the
52439 ** current working directory has been unlinked.
52440 */
52441 SimulateIOError( return SQLITE_ERROR );
52442 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
52443 /*
52444 ** NOTE: We are dealing with a relative path name and the data
52445 ** directory has been set. Therefore, use it as the basis
52446 ** for converting the relative path name to an absolute
52447 ** one by prepending the data directory and a backslash.
52448 */
52449 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
52450 sqlite3_data_directory, winGetDirSep(), zRelative);
52451 return SQLITE_OK0;
52452 }
52453 zConverted = winConvertFromUtf8Filename(zRelative);
52454 if( zConverted==0 ){
52455 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52456 }
52457 if( osIsNT() ){
52458 LPWSTR zTemp;
52459 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
52460 if( nByte==0 ){
52461 sqlite3_free(zConverted);
52462 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
52463 "winFullPathname1", zRelative);
52464 }
52465 nByte += 3;
52466 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
52467 if( zTemp==0 ){
52468 sqlite3_free(zConverted);
52469 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52470 }
52471 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
52472 if( nByte==0 ){
52473 sqlite3_free(zConverted);
52474 sqlite3_free(zTemp);
52475 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
52476 "winFullPathname2", zRelative);
52477 }
52478 sqlite3_free(zConverted);
52479 zOut = winUnicodeToUtf8(zTemp);
52480 sqlite3_free(zTemp);
52481 }
52482#ifdef SQLITE_WIN32_HAS_ANSI
52483 else{
52484 char *zTemp;
52485 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
52486 if( nByte==0 ){
52487 sqlite3_free(zConverted);
52488 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
52489 "winFullPathname3", zRelative);
52490 }
52491 nByte += 3;
52492 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
52493 if( zTemp==0 ){
52494 sqlite3_free(zConverted);
52495 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52496 }
52497 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
52498 if( nByte==0 ){
52499 sqlite3_free(zConverted);
52500 sqlite3_free(zTemp);
52501 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
52502 "winFullPathname4", zRelative);
52503 }
52504 sqlite3_free(zConverted);
52505 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
52506 sqlite3_free(zTemp);
52507 }
52508#endif
52509 if( zOut ){
52510 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut);
52511 sqlite3_free(zOut);
52512 return SQLITE_OK0;
52513 }else{
52514 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
52515 }
52516#endif
52517}
52518static int winFullPathname(
52519 sqlite3_vfs *pVfs, /* Pointer to vfs object */
52520 const char *zRelative, /* Possibly relative input path */
52521 int nFull, /* Size of output buffer in bytes */
52522 char *zFull /* Output buffer */
52523){
52524 int rc;
52525 MUTEX_LOGIC( sqlite3_mutex *pMutex; )sqlite3_mutex *pMutex;
52526 MUTEX_LOGIC( pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); )pMutex = sqlite3MutexAlloc(11);
52527 sqlite3_mutex_enter(pMutex);
52528 rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
52529 sqlite3_mutex_leave(pMutex);
52530 return rc;
52531}
52532
52533#ifndef SQLITE_OMIT_LOAD_EXTENSION1
52534/*
52535** Interfaces for opening a shared library, finding entry points
52536** within the shared library, and closing the shared library.
52537*/
52538static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
52539 HANDLE h;
52540#if defined(__CYGWIN__)
52541 int nFull = pVfs->mxPathname+1;
52542 char *zFull = sqlite3MallocZero( nFull );
52543 void *zConverted = 0;
52544 if( zFull==0 ){
52545 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
52546 return 0;
52547 }
52548 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK0 ){
52549 sqlite3_free(zFull);
52550 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
52551 return 0;
52552 }
52553 zConverted = winConvertFromUtf8Filename(zFull);
52554 sqlite3_free(zFull);
52555#else
52556 void *zConverted = winConvertFromUtf8Filename(zFilename);
52557 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52558#endif
52559 if( zConverted==0 ){
52560 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
52561 return 0;
52562 }
52563 if( osIsNT() ){
52564#if SQLITE_OS_WINRT
52565 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
52566#else
52567 h = osLoadLibraryW((LPCWSTR)zConverted);
52568#endif
52569 }
52570#ifdef SQLITE_WIN32_HAS_ANSI
52571 else{
52572 h = osLoadLibraryA((char*)zConverted);
52573 }
52574#endif
52575 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
52576 sqlite3_free(zConverted);
52577 return (void*)h;
52578}
52579static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
52580 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52581 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
52582}
52583static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
52584 FARPROC proc;
52585 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52586 proc = osGetProcAddressA((HANDLE)pH, zSym);
52587 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
52588 (void*)pH, zSym, (void*)proc));
52589 return (void(*)(void))proc;
52590}
52591static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
52592 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52593 osFreeLibrary((HANDLE)pHandle);
52594 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
52595}
52596#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
52597 #define winDlOpen 0
52598 #define winDlError 0
52599 #define winDlSym 0
52600 #define winDlClose 0
52601#endif
52602
52603/* State information for the randomness gatherer. */
52604typedef struct EntropyGatherer EntropyGatherer;
52605struct EntropyGatherer {
52606 unsigned char *a; /* Gather entropy into this buffer */
52607 int na; /* Size of a[] in bytes */
52608 int i; /* XOR next input into a[i] */
52609 int nXor; /* Number of XOR operations done */
52610};
52611
52612#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
52613/* Mix sz bytes of entropy into p. */
52614static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
52615 int j, k;
52616 for(j=0, k=p->i; j<sz; j++){
52617 p->a[k++] ^= x[j];
52618 if( k>=p->na ) k = 0;
52619 }
52620 p->i = k;
52621 p->nXor += sz;
52622}
52623#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
52624
52625/*
52626** Write up to nBuf bytes of randomness into zBuf.
52627*/
52628static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
52629#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
52630 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52631 memset(zBuf, 0, nBuf);
52632 return nBuf;
52633#else
52634 EntropyGatherer e;
52635 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52636 memset(zBuf, 0, nBuf);
52637 e.a = (unsigned char*)zBuf;
52638 e.na = nBuf;
52639 e.nXor = 0;
52640 e.i = 0;
52641 {
52642 SYSTEMTIME x;
52643 osGetSystemTime(&x);
52644 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
52645 }
52646 {
52647 DWORD pid = osGetCurrentProcessId();
52648 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
52649 }
52650#if SQLITE_OS_WINRT
52651 {
52652 ULONGLONG cnt = osGetTickCount64();
52653 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
52654 }
52655#else
52656 {
52657 DWORD cnt = osGetTickCount();
52658 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
52659 }
52660#endif /* SQLITE_OS_WINRT */
52661 {
52662 LARGE_INTEGER i;
52663 osQueryPerformanceCounter(&i);
52664 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
52665 }
52666#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
52667 {
52668 UUID id;
52669 memset(&id, 0, sizeof(UUID));
52670 osUuidCreate(&id);
52671 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
52672 memset(&id, 0, sizeof(UUID));
52673 osUuidCreateSequential(&id);
52674 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
52675 }
52676#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
52677 return e.nXor>nBuf ? nBuf : e.nXor;
52678#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
52679}
52680
52681
52682/*
52683** Sleep for a little while. Return the amount of time slept.
52684*/
52685static int winSleep(sqlite3_vfs *pVfs, int microsec){
52686 sqlite3_win32_sleep((microsec+999)/1000);
52687 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52688 return ((microsec+999)/1000)*1000;
52689}
52690
52691/*
52692** The following variable, if set to a non-zero value, is interpreted as
52693** the number of seconds since 1970 and is used to set the result of
52694** sqlite3OsCurrentTime() during testing.
52695*/
52696#ifdef SQLITE_TEST
52697SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
52698#endif
52699
52700/*
52701** Find the current time (in Universal Coordinated Time). Write into *piNow
52702** the current time and date as a Julian Day number times 86_400_000. In
52703** other words, write into *piNow the number of milliseconds since the Julian
52704** epoch of noon in Greenwich on November 24, 4714 B.C according to the
52705** proleptic Gregorian calendar.
52706**
52707** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
52708** cannot be found.
52709*/
52710static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
52711 /* FILETIME structure is a 64-bit value representing the number of
52712 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
52713 */
52714 FILETIME ft;
52715 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
52716#ifdef SQLITE_TEST
52717 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
52718#endif
52719 /* 2^32 - to avoid use of LL and warnings in gcc */
52720 static const sqlite3_int64 max32BitValue =
52721 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
52722 (sqlite3_int64)294967296;
52723
52724#if SQLITE_OS_WINCE
52725 SYSTEMTIME time;
52726 osGetSystemTime(&time);
52727 /* if SystemTimeToFileTime() fails, it returns zero. */
52728 if (!osSystemTimeToFileTime(&time,&ft)){
52729 return SQLITE_ERROR1;
52730 }
52731#else
52732 osGetSystemTimeAsFileTime( &ft );
52733#endif
52734
52735 *piNow = winFiletimeEpoch +
52736 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
52737 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
52738
52739#ifdef SQLITE_TEST
52740 if( sqlite3_current_time ){
52741 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
52742 }
52743#endif
52744 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52745 return SQLITE_OK0;
52746}
52747
52748/*
52749** Find the current time (in Universal Coordinated Time). Write the
52750** current time and date as a Julian Day number into *prNow and
52751** return 0. Return 1 if the time and date cannot be found.
52752*/
52753static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
52754 int rc;
52755 sqlite3_int64 i;
52756 rc = winCurrentTimeInt64(pVfs, &i);
52757 if( !rc ){
52758 *prNow = i/86400000.0;
52759 }
52760 return rc;
52761}
52762
52763/*
52764** The idea is that this function works like a combination of
52765** GetLastError() and FormatMessage() on Windows (or errno and
52766** strerror_r() on Unix). After an error is returned by an OS
52767** function, SQLite calls this function with zBuf pointing to
52768** a buffer of nBuf bytes. The OS layer should populate the
52769** buffer with a nul-terminated UTF-8 encoded error message
52770** describing the last IO error to have occurred within the calling
52771** thread.
52772**
52773** If the error message is too large for the supplied buffer,
52774** it should be truncated. The return value of xGetLastError
52775** is zero if the error message fits in the buffer, or non-zero
52776** otherwise (if the message was truncated). If non-zero is returned,
52777** then it is not necessary to include the nul-terminator character
52778** in the output buffer.
52779**
52780** Not supplying an error message will have no adverse effect
52781** on SQLite. It is fine to have an implementation that never
52782** returns an error message:
52783**
52784** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
52785** assert(zBuf[0]=='\0');
52786** return 0;
52787** }
52788**
52789** However if an error message is supplied, it will be incorporated
52790** by sqlite into the error message available to the user using
52791** sqlite3_errmsg(), possibly making IO errors easier to debug.
52792*/
52793static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
52794 DWORD e = osGetLastError();
52795 UNUSED_PARAMETER(pVfs)(void)(pVfs);
52796 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
52797 return e;
52798}
52799
52800/*
52801** Initialize and deinitialize the operating system interface.
52802*/
52803SQLITE_API int sqlite3_os_init(void){
52804 static sqlite3_vfs winVfs = {
52805 3, /* iVersion */
52806 sizeof(winFile), /* szOsFile */
52807 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
52808 0, /* pNext */
52809 "win32", /* zName */
52810 &winAppData, /* pAppData */
52811 winOpen, /* xOpen */
52812 winDelete, /* xDelete */
52813 winAccess, /* xAccess */
52814 winFullPathname, /* xFullPathname */
52815 winDlOpen, /* xDlOpen */
52816 winDlError, /* xDlError */
52817 winDlSym, /* xDlSym */
52818 winDlClose, /* xDlClose */
52819 winRandomness, /* xRandomness */
52820 winSleep, /* xSleep */
52821 winCurrentTime, /* xCurrentTime */
52822 winGetLastError, /* xGetLastError */
52823 winCurrentTimeInt64, /* xCurrentTimeInt64 */
52824 winSetSystemCall, /* xSetSystemCall */
52825 winGetSystemCall, /* xGetSystemCall */
52826 winNextSystemCall, /* xNextSystemCall */
52827 };
52828#if defined(SQLITE_WIN32_HAS_WIDE)
52829 static sqlite3_vfs winLongPathVfs = {
52830 3, /* iVersion */
52831 sizeof(winFile), /* szOsFile */
52832 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
52833 0, /* pNext */
52834 "win32-longpath", /* zName */
52835 &winAppData, /* pAppData */
52836 winOpen, /* xOpen */
52837 winDelete, /* xDelete */
52838 winAccess, /* xAccess */
52839 winFullPathname, /* xFullPathname */
52840 winDlOpen, /* xDlOpen */
52841 winDlError, /* xDlError */
52842 winDlSym, /* xDlSym */
52843 winDlClose, /* xDlClose */
52844 winRandomness, /* xRandomness */
52845 winSleep, /* xSleep */
52846 winCurrentTime, /* xCurrentTime */
52847 winGetLastError, /* xGetLastError */
52848 winCurrentTimeInt64, /* xCurrentTimeInt64 */
52849 winSetSystemCall, /* xSetSystemCall */
52850 winGetSystemCall, /* xGetSystemCall */
52851 winNextSystemCall, /* xNextSystemCall */
52852 };
52853#endif
52854 static sqlite3_vfs winNolockVfs = {
52855 3, /* iVersion */
52856 sizeof(winFile), /* szOsFile */
52857 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
52858 0, /* pNext */
52859 "win32-none", /* zName */
52860 &winNolockAppData, /* pAppData */
52861 winOpen, /* xOpen */
52862 winDelete, /* xDelete */
52863 winAccess, /* xAccess */
52864 winFullPathname, /* xFullPathname */
52865 winDlOpen, /* xDlOpen */
52866 winDlError, /* xDlError */
52867 winDlSym, /* xDlSym */
52868 winDlClose, /* xDlClose */
52869 winRandomness, /* xRandomness */
52870 winSleep, /* xSleep */
52871 winCurrentTime, /* xCurrentTime */
52872 winGetLastError, /* xGetLastError */
52873 winCurrentTimeInt64, /* xCurrentTimeInt64 */
52874 winSetSystemCall, /* xSetSystemCall */
52875 winGetSystemCall, /* xGetSystemCall */
52876 winNextSystemCall, /* xNextSystemCall */
52877 };
52878#if defined(SQLITE_WIN32_HAS_WIDE)
52879 static sqlite3_vfs winLongPathNolockVfs = {
52880 3, /* iVersion */
52881 sizeof(winFile), /* szOsFile */
52882 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
52883 0, /* pNext */
52884 "win32-longpath-none", /* zName */
52885 &winNolockAppData, /* pAppData */
52886 winOpen, /* xOpen */
52887 winDelete, /* xDelete */
52888 winAccess, /* xAccess */
52889 winFullPathname, /* xFullPathname */
52890 winDlOpen, /* xDlOpen */
52891 winDlError, /* xDlError */
52892 winDlSym, /* xDlSym */
52893 winDlClose, /* xDlClose */
52894 winRandomness, /* xRandomness */
52895 winSleep, /* xSleep */
52896 winCurrentTime, /* xCurrentTime */
52897 winGetLastError, /* xGetLastError */
52898 winCurrentTimeInt64, /* xCurrentTimeInt64 */
52899 winSetSystemCall, /* xSetSystemCall */
52900 winGetSystemCall, /* xGetSystemCall */
52901 winNextSystemCall, /* xNextSystemCall */
52902 };
52903#endif
52904
52905 /* Double-check that the aSyscall[] array has been constructed
52906 ** correctly. See ticket [bb3a86e890c8e96ab] */
52907 assert( ArraySize(aSyscall)==80 )((void) (0));
52908
52909 /* get memory map allocation granularity */
52910 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
52911#if SQLITE_OS_WINRT
52912 osGetNativeSystemInfo(&winSysInfo);
52913#else
52914 osGetSystemInfo(&winSysInfo);
52915#endif
52916 assert( winSysInfo.dwAllocationGranularity>0 )((void) (0));
52917 assert( winSysInfo.dwPageSize>0 )((void) (0));
52918
52919 sqlite3_vfs_register(&winVfs, 1);
52920
52921#if defined(SQLITE_WIN32_HAS_WIDE)
52922 sqlite3_vfs_register(&winLongPathVfs, 0);
52923#endif
52924
52925 sqlite3_vfs_register(&winNolockVfs, 0);
52926
52927#if defined(SQLITE_WIN32_HAS_WIDE)
52928 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
52929#endif
52930
52931#ifndef SQLITE_OMIT_WAL
52932 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
52933#endif
52934
52935 return SQLITE_OK0;
52936}
52937
52938SQLITE_API int sqlite3_os_end(void){
52939#if SQLITE_OS_WINRT
52940 if( sleepObj!=NULL((void*)0) ){
52941 osCloseHandle(sleepObj);
52942 sleepObj = NULL((void*)0);
52943 }
52944#endif
52945
52946#ifndef SQLITE_OMIT_WAL
52947 winBigLock = 0;
52948#endif
52949
52950 return SQLITE_OK0;
52951}
52952
52953#endif /* SQLITE_OS_WIN */
52954
52955/************** End of os_win.c **********************************************/
52956/************** Begin file memdb.c *******************************************/
52957/*
52958** 2016-09-07
52959**
52960** The author disclaims copyright to this source code. In place of
52961** a legal notice, here is a blessing:
52962**
52963** May you do good and not evil.
52964** May you find forgiveness for yourself and forgive others.
52965** May you share freely, never taking more than you give.
52966**
52967******************************************************************************
52968**
52969** This file implements an in-memory VFS. A database is held as a contiguous
52970** block of memory.
52971**
52972** This file also implements interface sqlite3_serialize() and
52973** sqlite3_deserialize().
52974*/
52975/* #include "sqliteInt.h" */
52976#ifndef SQLITE_OMIT_DESERIALIZE
52977
52978/*
52979** Forward declaration of objects used by this utility
52980*/
52981typedef struct sqlite3_vfs MemVfs;
52982typedef struct MemFile MemFile;
52983typedef struct MemStore MemStore;
52984
52985/* Access to a lower-level VFS that (might) implement dynamic loading,
52986** access to randomness, etc.
52987*/
52988#define ORIGVFS(p)((sqlite3_vfs*)((p)->pAppData)) ((sqlite3_vfs*)((p)->pAppData))
52989
52990/* Storage for a memdb file.
52991**
52992** An memdb object can be shared or separate. Shared memdb objects can be
52993** used by more than one database connection. Mutexes are used by shared
52994** memdb objects to coordinate access. Separate memdb objects are only
52995** connected to a single database connection and do not require additional
52996** mutexes.
52997**
52998** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created
52999** using "file:/name?vfs=memdb". The first character of the name must be
53000** "/" or else the object will be a separate memdb object. All shared
53001** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order.
53002**
53003** Separate memdb objects are created using a name that does not begin
53004** with "/" or using sqlite3_deserialize().
53005**
53006** Access rules for shared MemStore objects:
53007**
53008** * .zFName is initialized when the object is created and afterwards
53009** is unchanged until the object is destroyed. So it can be accessed
53010** at any time as long as we know the object is not being destroyed,
53011** which means while either the SQLITE_MUTEX_STATIC_VFS1 or
53012** .pMutex is held or the object is not part of memdb_g.apMemStore[].
53013**
53014** * Can .pMutex can only be changed while holding the
53015** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part
53016** of memdb_g.apMemStore[].
53017**
53018** * Other fields can only be changed while holding the .pMutex mutex
53019** or when the .nRef is less than zero and the object is not part of
53020** memdb_g.apMemStore[].
53021**
53022** * The .aData pointer has the added requirement that it can can only
53023** be changed (for resizing) when nMmap is zero.
53024**
53025*/
53026struct MemStore {
53027 sqlite3_int64 sz; /* Size of the file */
53028 sqlite3_int64 szAlloc; /* Space allocated to aData */
53029 sqlite3_int64 szMax; /* Maximum allowed size of the file */
53030 unsigned char *aData; /* content of the file */
53031 sqlite3_mutex *pMutex; /* Used by shared stores only */
53032 int nMmap; /* Number of memory mapped pages */
53033 unsigned mFlags; /* Flags */
53034 int nRdLock; /* Number of readers */
53035 int nWrLock; /* Number of writers. (Always 0 or 1) */
53036 int nRef; /* Number of users of this MemStore */
53037 char *zFName; /* The filename for shared stores */
53038};
53039
53040/* An open file */
53041struct MemFile {
53042 sqlite3_file base; /* IO methods */
53043 MemStore *pStore; /* The storage */
53044 int eLock; /* Most recent lock against this file */
53045};
53046
53047/*
53048** File-scope variables for holding the memdb files that are accessible
53049** to multiple database connections in separate threads.
53050**
53051** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object.
53052*/
53053static struct MemFS {
53054 int nMemStore; /* Number of shared MemStore objects */
53055 MemStore **apMemStore; /* Array of all shared MemStore objects */
53056} memdb_g;
53057
53058/*
53059** Methods for MemFile
53060*/
53061static int memdbClose(sqlite3_file*);
53062static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
53063static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
53064static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
53065static int memdbSync(sqlite3_file*, int flags);
53066static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
53067static int memdbLock(sqlite3_file*, int);
53068static int memdbUnlock(sqlite3_file*, int);
53069/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
53070static int memdbFileControl(sqlite3_file*, int op, void *pArg);
53071/* static int memdbSectorSize(sqlite3_file*); // not used */
53072static int memdbDeviceCharacteristics(sqlite3_file*);
53073static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
53074static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
53075
53076/*
53077** Methods for MemVfs
53078*/
53079static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
53080/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
53081static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
53082static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
53083static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
53084static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
53085static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
53086static void memdbDlClose(sqlite3_vfs*, void*);
53087static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
53088static int memdbSleep(sqlite3_vfs*, int microseconds);
53089/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
53090static int memdbGetLastError(sqlite3_vfs*, int, char *);
53091static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
53092
53093static sqlite3_vfs memdb_vfs = {
53094 2, /* iVersion */
53095 0, /* szOsFile (set when registered) */
53096 1024, /* mxPathname */
53097 0, /* pNext */
53098 "memdb", /* zName */
53099 0, /* pAppData (set when registered) */
53100 memdbOpen, /* xOpen */
53101 0, /* memdbDelete, */ /* xDelete */
53102 memdbAccess, /* xAccess */
53103 memdbFullPathname, /* xFullPathname */
53104 memdbDlOpen, /* xDlOpen */
53105 memdbDlError, /* xDlError */
53106 memdbDlSym, /* xDlSym */
53107 memdbDlClose, /* xDlClose */
53108 memdbRandomness, /* xRandomness */
53109 memdbSleep, /* xSleep */
53110 0, /* memdbCurrentTime, */ /* xCurrentTime */
53111 memdbGetLastError, /* xGetLastError */
53112 memdbCurrentTimeInt64, /* xCurrentTimeInt64 */
53113 0, /* xSetSystemCall */
53114 0, /* xGetSystemCall */
53115 0, /* xNextSystemCall */
53116};
53117
53118static const sqlite3_io_methods memdb_io_methods = {
53119 3, /* iVersion */
53120 memdbClose, /* xClose */
53121 memdbRead, /* xRead */
53122 memdbWrite, /* xWrite */
53123 memdbTruncate, /* xTruncate */
53124 memdbSync, /* xSync */
53125 memdbFileSize, /* xFileSize */
53126 memdbLock, /* xLock */
53127 memdbUnlock, /* xUnlock */
53128 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
53129 memdbFileControl, /* xFileControl */
53130 0, /* memdbSectorSize,*/ /* xSectorSize */
53131 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
53132 0, /* xShmMap */
53133 0, /* xShmLock */
53134 0, /* xShmBarrier */
53135 0, /* xShmUnmap */
53136 memdbFetch, /* xFetch */
53137 memdbUnfetch /* xUnfetch */
53138};
53139
53140/*
53141** Enter/leave the mutex on a MemStore
53142*/
53143#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2==0
53144static void memdbEnter(MemStore *p){
53145 UNUSED_PARAMETER(p)(void)(p);
53146}
53147static void memdbLeave(MemStore *p){
53148 UNUSED_PARAMETER(p)(void)(p);
53149}
53150#else
53151static void memdbEnter(MemStore *p){
53152 sqlite3_mutex_enter(p->pMutex);
53153}
53154static void memdbLeave(MemStore *p){
53155 sqlite3_mutex_leave(p->pMutex);
53156}
53157#endif
53158
53159
53160
53161/*
53162** Close an memdb-file.
53163** Free the underlying MemStore object when its refcount drops to zero
53164** or less.
53165*/
53166static int memdbClose(sqlite3_file *pFile){
53167 MemStore *p = ((MemFile*)pFile)->pStore;
53168 if( p->zFName ){
53169 int i;
53170#ifndef SQLITE_MUTEX_OMIT
53171 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
53172#endif
53173 sqlite3_mutex_enter(pVfsMutex);
53174 for(i=0; ALWAYS(i<memdb_g.nMemStore)(i<memdb_g.nMemStore); i++){
53175 if( memdb_g.apMemStore[i]==p ){
53176 memdbEnter(p);
53177 if( p->nRef==1 ){
53178 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
53179 if( memdb_g.nMemStore==0 ){
53180 sqlite3_free(memdb_g.apMemStore);
53181 memdb_g.apMemStore = 0;
53182 }
53183 }
53184 break;
53185 }
53186 }
53187 sqlite3_mutex_leave(pVfsMutex);
53188 }else{
53189 memdbEnter(p);
53190 }
53191 p->nRef--;
53192 if( p->nRef<=0 ){
53193 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1 ){
53194 sqlite3_free(p->aData);
53195 }
53196 memdbLeave(p);
53197 sqlite3_mutex_free(p->pMutex);
53198 sqlite3_free(p);
53199 }else{
53200 memdbLeave(p);
53201 }
53202 return SQLITE_OK0;
53203}
53204
53205/*
53206** Read data from an memdb-file.
53207*/
53208static int memdbRead(
53209 sqlite3_file *pFile,
53210 void *zBuf,
53211 int iAmt,
53212 sqlite_int64 iOfst
53213){
53214 MemStore *p = ((MemFile*)pFile)->pStore;
53215 memdbEnter(p);
53216 if( iOfst+iAmt>p->sz ){
53217 memset(zBuf, 0, iAmt);
53218 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
53219 memdbLeave(p);
53220 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
53221 }
53222 memcpy(zBuf, p->aData+iOfst, iAmt);
53223 memdbLeave(p);
53224 return SQLITE_OK0;
53225}
53226
53227/*
53228** Try to enlarge the memory allocation to hold at least sz bytes
53229*/
53230static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){
53231 unsigned char *pNew;
53232 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)==0 || NEVER(p->nMmap>0)(p->nMmap>0) ){
53233 return SQLITE_FULL13;
53234 }
53235 if( newSz>p->szMax ){
53236 return SQLITE_FULL13;
53237 }
53238 newSz *= 2;
53239 if( newSz>p->szMax ) newSz = p->szMax;
53240 pNew = sqlite3Realloc(p->aData, newSz);
53241 if( pNew==0 ) return SQLITE_IOERR_NOMEM(10 | (12<<8));
53242 p->aData = pNew;
53243 p->szAlloc = newSz;
53244 return SQLITE_OK0;
53245}
53246
53247/*
53248** Write data to an memdb-file.
53249*/
53250static int memdbWrite(
53251 sqlite3_file *pFile,
53252 const void *z,
53253 int iAmt,
53254 sqlite_int64 iOfst
53255){
53256 MemStore *p = ((MemFile*)pFile)->pStore;
53257 memdbEnter(p);
53258 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY)(p->mFlags & 4) ){
53259 /* Can't happen: memdbLock() will return SQLITE_READONLY before
53260 ** reaching this point */
53261 memdbLeave(p);
53262 return SQLITE_IOERR_WRITE(10 | (3<<8));
53263 }
53264 if( iOfst+iAmt>p->sz ){
53265 int rc;
53266 if( iOfst+iAmt>p->szAlloc
53267 && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK0
53268 ){
53269 memdbLeave(p);
53270 return rc;
53271 }
53272 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
53273 p->sz = iOfst+iAmt;
53274 }
53275 memcpy(p->aData+iOfst, z, iAmt);
53276 memdbLeave(p);
53277 return SQLITE_OK0;
53278}
53279
53280/*
53281** Truncate an memdb-file.
53282**
53283** In rollback mode (which is always the case for memdb, as it does not
53284** support WAL mode) the truncate() method is only used to reduce
53285** the size of a file, never to increase the size.
53286*/
53287static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
53288 MemStore *p = ((MemFile*)pFile)->pStore;
53289 int rc = SQLITE_OK0;
53290 memdbEnter(p);
53291 if( size>p->sz ){
53292 /* This can only happen with a corrupt wal mode db */
53293 rc = SQLITE_CORRUPT11;
53294 }else{
53295 p->sz = size;
53296 }
53297 memdbLeave(p);
53298 return rc;
53299}
53300
53301/*
53302** Sync an memdb-file.
53303*/
53304static int memdbSync(sqlite3_file *pFile, int flags){
53305 UNUSED_PARAMETER(pFile)(void)(pFile);
53306 UNUSED_PARAMETER(flags)(void)(flags);
53307 return SQLITE_OK0;
53308}
53309
53310/*
53311** Return the current file-size of an memdb-file.
53312*/
53313static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
53314 MemStore *p = ((MemFile*)pFile)->pStore;
53315 memdbEnter(p);
53316 *pSize = p->sz;
53317 memdbLeave(p);
53318 return SQLITE_OK0;
53319}
53320
53321/*
53322** Lock an memdb-file.
53323*/
53324static int memdbLock(sqlite3_file *pFile, int eLock){
53325 MemFile *pThis = (MemFile*)pFile;
53326 MemStore *p = pThis->pStore;
53327 int rc = SQLITE_OK0;
53328 if( eLock<=pThis->eLock ) return SQLITE_OK0;
53329 memdbEnter(p);
53330
53331 assert( p->nWrLock==0 || p->nWrLock==1 )((void) (0));
53332 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 )((void) (0));
53333 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 )((void) (0));
53334
53335 if( eLock>SQLITE_LOCK_SHARED1 && (p->mFlags & SQLITE_DESERIALIZE_READONLY4) ){
53336 rc = SQLITE_READONLY8;
53337 }else{
53338 switch( eLock ){
53339 case SQLITE_LOCK_SHARED1: {
53340 assert( pThis->eLock==SQLITE_LOCK_NONE )((void) (0));
53341 if( p->nWrLock>0 ){
53342 rc = SQLITE_BUSY5;
53343 }else{
53344 p->nRdLock++;
53345 }
53346 break;
53347 };
53348
53349 case SQLITE_LOCK_RESERVED2:
53350 case SQLITE_LOCK_PENDING3: {
53351 assert( pThis->eLock>=SQLITE_LOCK_SHARED )((void) (0));
53352 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED)(pThis->eLock==1) ){
53353 if( p->nWrLock>0 ){
53354 rc = SQLITE_BUSY5;
53355 }else{
53356 p->nWrLock = 1;
53357 }
53358 }
53359 break;
53360 }
53361
53362 default: {
53363 assert( eLock==SQLITE_LOCK_EXCLUSIVE )((void) (0));
53364 assert( pThis->eLock>=SQLITE_LOCK_SHARED )((void) (0));
53365 if( p->nRdLock>1 ){
53366 rc = SQLITE_BUSY5;
53367 }else if( pThis->eLock==SQLITE_LOCK_SHARED1 ){
53368 p->nWrLock = 1;
53369 }
53370 break;
53371 }
53372 }
53373 }
53374 if( rc==SQLITE_OK0 ) pThis->eLock = eLock;
53375 memdbLeave(p);
53376 return rc;
53377}
53378
53379/*
53380** Unlock an memdb-file.
53381*/
53382static int memdbUnlock(sqlite3_file *pFile, int eLock){
53383 MemFile *pThis = (MemFile*)pFile;
53384 MemStore *p = pThis->pStore;
53385 if( eLock>=pThis->eLock ) return SQLITE_OK0;
53386 memdbEnter(p);
53387
53388 assert( eLock==SQLITE_LOCK_SHARED || eLock==SQLITE_LOCK_NONE )((void) (0));
53389 if( eLock==SQLITE_LOCK_SHARED1 ){
53390 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED)(pThis->eLock>1) ){
53391 p->nWrLock--;
53392 }
53393 }else{
53394 if( pThis->eLock>SQLITE_LOCK_SHARED1 ){
53395 p->nWrLock--;
53396 }
53397 p->nRdLock--;
53398 }
53399
53400 pThis->eLock = eLock;
53401 memdbLeave(p);
53402 return SQLITE_OK0;
53403}
53404
53405#if 0
53406/*
53407** This interface is only used for crash recovery, which does not
53408** occur on an in-memory database.
53409*/
53410static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
53411 *pResOut = 0;
53412 return SQLITE_OK0;
53413}
53414#endif
53415
53416
53417/*
53418** File control method. For custom operations on an memdb-file.
53419*/
53420static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
53421 MemStore *p = ((MemFile*)pFile)->pStore;
53422 int rc = SQLITE_NOTFOUND12;
53423 memdbEnter(p);
53424 if( op==SQLITE_FCNTL_VFSNAME12 ){
53425 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
53426 rc = SQLITE_OK0;
53427 }
53428 if( op==SQLITE_FCNTL_SIZE_LIMIT36 ){
53429 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
53430 if( iLimit<p->sz ){
53431 if( iLimit<0 ){
53432 iLimit = p->szMax;
53433 }else{
53434 iLimit = p->sz;
53435 }
53436 }
53437 p->szMax = iLimit;
53438 *(sqlite3_int64*)pArg = iLimit;
53439 rc = SQLITE_OK0;
53440 }
53441 memdbLeave(p);
53442 return rc;
53443}
53444
53445#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
53446/*
53447** Return the sector-size in bytes for an memdb-file.
53448*/
53449static int memdbSectorSize(sqlite3_file *pFile){
53450 return 1024;
53451}
53452#endif
53453
53454/*
53455** Return the device characteristic flags supported by an memdb-file.
53456*/
53457static int memdbDeviceCharacteristics(sqlite3_file *pFile){
53458 UNUSED_PARAMETER(pFile)(void)(pFile);
53459 return SQLITE_IOCAP_ATOMIC0x00000001 |
53460 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 |
53461 SQLITE_IOCAP_SAFE_APPEND0x00000200 |
53462 SQLITE_IOCAP_SEQUENTIAL0x00000400;
53463}
53464
53465/* Fetch a page of a memory-mapped file */
53466static int memdbFetch(
53467 sqlite3_file *pFile,
53468 sqlite3_int64 iOfst,
53469 int iAmt,
53470 void **pp
53471){
53472 MemStore *p = ((MemFile*)pFile)->pStore;
53473 memdbEnter(p);
53474 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)!=0 ){
53475 *pp = 0;
53476 }else{
53477 p->nMmap++;
53478 *pp = (void*)(p->aData + iOfst);
53479 }
53480 memdbLeave(p);
53481 return SQLITE_OK0;
53482}
53483
53484/* Release a memory-mapped page */
53485static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
53486 MemStore *p = ((MemFile*)pFile)->pStore;
53487 UNUSED_PARAMETER(iOfst)(void)(iOfst);
53488 UNUSED_PARAMETER(pPage)(void)(pPage);
53489 memdbEnter(p);
53490 p->nMmap--;
53491 memdbLeave(p);
53492 return SQLITE_OK0;
53493}
53494
53495/*
53496** Open an mem file handle.
53497*/
53498static int memdbOpen(
53499 sqlite3_vfs *pVfs,
53500 const char *zName,
53501 sqlite3_file *pFd,
53502 int flags,
53503 int *pOutFlags
53504){
53505 MemFile *pFile = (MemFile*)pFd;
53506 MemStore *p = 0;
53507 int szName;
53508 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53509
53510 memset(pFile, 0, sizeof(*pFile));
53511 szName = sqlite3Strlen30(zName);
53512 if( szName>1 && (zName[0]=='/' || zName[0]=='\\') ){
53513 int i;
53514#ifndef SQLITE_MUTEX_OMIT
53515 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
53516#endif
53517 sqlite3_mutex_enter(pVfsMutex);
53518 for(i=0; i<memdb_g.nMemStore; i++){
53519 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
53520 p = memdb_g.apMemStore[i];
53521 break;
53522 }
53523 }
53524 if( p==0 ){
53525 MemStore **apNew;
53526 p = sqlite3Malloc( sizeof(*p) + szName + 3 );
53527 if( p==0 ){
53528 sqlite3_mutex_leave(pVfsMutex);
53529 return SQLITE_NOMEM7;
53530 }
53531 apNew = sqlite3Realloc(memdb_g.apMemStore,
53532 sizeof(apNew[0])*(memdb_g.nMemStore+1) );
53533 if( apNew==0 ){
53534 sqlite3_free(p);
53535 sqlite3_mutex_leave(pVfsMutex);
53536 return SQLITE_NOMEM7;
53537 }
53538 apNew[memdb_g.nMemStore++] = p;
53539 memdb_g.apMemStore = apNew;
53540 memset(p, 0, sizeof(*p));
53541 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2|SQLITE_DESERIALIZE_FREEONCLOSE1;
53542 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
53543 p->zFName = (char*)&p[1];
53544 memcpy(p->zFName, zName, szName+1);
53545 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
53546 if( p->pMutex==0 ){
53547 memdb_g.nMemStore--;
53548 sqlite3_free(p);
53549 sqlite3_mutex_leave(pVfsMutex);
53550 return SQLITE_NOMEM7;
53551 }
53552 p->nRef = 1;
53553 memdbEnter(p);
53554 }else{
53555 memdbEnter(p);
53556 p->nRef++;
53557 }
53558 sqlite3_mutex_leave(pVfsMutex);
53559 }else{
53560 p = sqlite3Malloc( sizeof(*p) );
53561 if( p==0 ){
53562 return SQLITE_NOMEM7;
53563 }
53564 memset(p, 0, sizeof(*p));
53565 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2 | SQLITE_DESERIALIZE_FREEONCLOSE1;
53566 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
53567 }
53568 pFile->pStore = p;
53569 if( pOutFlags!=0 ){
53570 *pOutFlags = flags | SQLITE_OPEN_MEMORY0x00000080;
53571 }
53572 pFd->pMethods = &memdb_io_methods;
53573 memdbLeave(p);
53574 return SQLITE_OK0;
53575}
53576
53577#if 0 /* Only used to delete rollback journals, super-journals, and WAL
53578 ** files, none of which exist in memdb. So this routine is never used */
53579/*
53580** Delete the file located at zPath. If the dirSync argument is true,
53581** ensure the file-system modifications are synced to disk before
53582** returning.
53583*/
53584static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
53585 return SQLITE_IOERR_DELETE(10 | (10<<8));
53586}
53587#endif
53588
53589/*
53590** Test for access permissions. Return true if the requested permission
53591** is available, or false otherwise.
53592**
53593** With memdb, no files ever exist on disk. So always return false.
53594*/
53595static int memdbAccess(
53596 sqlite3_vfs *pVfs,
53597 const char *zPath,
53598 int flags,
53599 int *pResOut
53600){
53601 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53602 UNUSED_PARAMETER(zPath)(void)(zPath);
53603 UNUSED_PARAMETER(flags)(void)(flags);
53604 *pResOut = 0;
53605 return SQLITE_OK0;
53606}
53607
53608/*
53609** Populate buffer zOut with the full canonical pathname corresponding
53610** to the pathname in zPath. zOut is guaranteed to point to a buffer
53611** of at least (INST_MAX_PATHNAME+1) bytes.
53612*/
53613static int memdbFullPathname(
53614 sqlite3_vfs *pVfs,
53615 const char *zPath,
53616 int nOut,
53617 char *zOut
53618){
53619 UNUSED_PARAMETER(pVfs)(void)(pVfs);
53620 sqlite3_snprintf(nOut, zOut, "%s", zPath);
53621 return SQLITE_OK0;
53622}
53623
53624/*
53625** Open the dynamic library located at zPath and return a handle.
53626*/
53627static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
53628 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlOpen(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), zPath);
53629}
53630
53631/*
53632** Populate the buffer zErrMsg (size nByte bytes) with a human readable
53633** utf-8 string describing the most recent error encountered associated
53634** with dynamic libraries.
53635*/
53636static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
53637 ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlError(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nByte, zErrMsg);
53638}
53639
53640/*
53641** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
53642*/
53643static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
53644 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlSym(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), p, zSym);
53645}
53646
53647/*
53648** Close the dynamic library handle pHandle.
53649*/
53650static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
53651 ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xDlClose(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), pHandle);
53652}
53653
53654/*
53655** Populate the buffer pointed to by zBufOut with nByte bytes of
53656** random data.
53657*/
53658static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
53659 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xRandomness(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nByte, zBufOut);
53660}
53661
53662/*
53663** Sleep for nMicro microseconds. Return the number of microseconds
53664** actually slept.
53665*/
53666static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
53667 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xSleep(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), nMicro);
53668}
53669
53670#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
53671/*
53672** Return the current time as a Julian Day number in *pTimeOut.
53673*/
53674static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
53675 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xCurrentTime(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), pTimeOut);
53676}
53677#endif
53678
53679static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
53680 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xGetLastError(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), a, b);
53681}
53682static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
53683 return ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData))->xCurrentTimeInt64(ORIGVFS(pVfs)((sqlite3_vfs*)((pVfs)->pAppData)), p);
53684}
53685
53686/*
53687** Translate a database connection pointer and schema name into a
53688** MemFile pointer.
53689*/
53690static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
53691 MemFile *p = 0;
53692 MemStore *pStore;
53693 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER7, &p);
53694 if( rc ) return 0;
53695 if( p->base.pMethods!=&memdb_io_methods ) return 0;
53696 pStore = p->pStore;
53697 memdbEnter(pStore);
53698 if( pStore->zFName!=0 ) p = 0;
53699 memdbLeave(pStore);
53700 return p;
53701}
53702
53703/*
53704** Return the serialization of a database
53705*/
53706SQLITE_API unsigned char *sqlite3_serialize(
53707 sqlite3 *db, /* The database connection */
53708 const char *zSchema, /* Which database within the connection */
53709 sqlite3_int64 *piSize, /* Write size here, if not NULL */
53710 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
53711){
53712 MemFile *p;
53713 int iDb;
53714 Btree *pBt;
53715 sqlite3_int64 sz;
53716 int szPage = 0;
53717 sqlite3_stmt *pStmt = 0;
53718 unsigned char *pOut;
53719 char *zSql;
53720 int rc;
53721
53722#ifdef SQLITE_ENABLE_API_ARMOR1
53723 if( !sqlite3SafetyCheckOk(db) ){
53724 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(53724);
53725 return 0;
53726 }
53727#endif
53728
53729 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53730 p = memdbFromDbSchema(db, zSchema);
53731 iDb = sqlite3FindDbName(db, zSchema);
53732 if( piSize ) *piSize = -1;
53733 if( iDb<0 ) return 0;
53734 if( p ){
53735 MemStore *pStore = p->pStore;
53736 assert( pStore->pMutex==0 )((void) (0));
53737 if( piSize ) *piSize = pStore->sz;
53738 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
53739 pOut = pStore->aData;
53740 }else{
53741 pOut = sqlite3_malloc64( pStore->sz );
53742 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
53743 }
53744 return pOut;
53745 }
53746 pBt = db->aDb[iDb].pBt;
53747 if( pBt==0 ) return 0;
53748 szPage = sqlite3BtreeGetPageSize(pBt);
53749 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
53750 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM7;
53751 sqlite3_free(zSql);
53752 if( rc ) return 0;
53753 rc = sqlite3_step(pStmt);
53754 if( rc!=SQLITE_ROW100 ){
53755 pOut = 0;
53756 }else{
53757 sz = sqlite3_column_int64(pStmt, 0)*szPage;
53758 if( sz==0 ){
53759 sqlite3_reset(pStmt);
53760 sqlite3_exec(db, "BEGIN IMMEDIATE; COMMIT;", 0, 0, 0);
53761 rc = sqlite3_step(pStmt);
53762 if( rc==SQLITE_ROW100 ){
53763 sz = sqlite3_column_int64(pStmt, 0)*szPage;
53764 }
53765 }
53766 if( piSize ) *piSize = sz;
53767 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
53768 pOut = 0;
53769 }else{
53770 pOut = sqlite3_malloc64( sz );
53771 if( pOut ){
53772 int nPage = sqlite3_column_int(pStmt, 0);
53773 Pager *pPager = sqlite3BtreePager(pBt);
53774 int pgno;
53775 for(pgno=1; pgno<=nPage; pgno++){
53776 DbPage *pPage = 0;
53777 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
53778 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
53779 if( rc==SQLITE_OK0 ){
53780 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
53781 }else{
53782 memset(pTo, 0, szPage);
53783 }
53784 sqlite3PagerUnref(pPage);
53785 }
53786 }
53787 }
53788 }
53789 sqlite3_finalize(pStmt);
53790 return pOut;
53791}
53792
53793/* Convert zSchema to a MemDB and initialize its content.
53794*/
53795SQLITE_API int sqlite3_deserialize(
53796 sqlite3 *db, /* The database connection */
53797 const char *zSchema, /* Which DB to reopen with the deserialization */
53798 unsigned char *pData, /* The serialized database content */
53799 sqlite3_int64 szDb, /* Number bytes in the deserialization */
53800 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
53801 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
53802){
53803 MemFile *p;
53804 char *zSql;
53805 sqlite3_stmt *pStmt = 0;
53806 int rc;
53807 int iDb;
53808
53809#ifdef SQLITE_ENABLE_API_ARMOR1
53810 if( !sqlite3SafetyCheckOk(db) ){
53811 return SQLITE_MISUSE_BKPTsqlite3MisuseError(53811);
53812 }
53813 if( szDb<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(53813);
53814 if( szBuf<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(53814);
53815#endif
53816
53817 sqlite3_mutex_enter(db->mutex);
53818 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53819 iDb = sqlite3FindDbName(db, zSchema);
53820 testcase( iDb==1 );
53821 if( iDb<2 && iDb!=0 ){
53822 rc = SQLITE_ERROR1;
53823 goto end_deserialize;
53824 }
53825 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
53826 if( zSql==0 ){
53827 rc = SQLITE_NOMEM7;
53828 }else{
53829 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
53830 sqlite3_free(zSql);
53831 }
53832 if( rc ) goto end_deserialize;
53833 db->init.iDb = (u8)iDb;
53834 db->init.reopenMemdb = 1;
53835 rc = sqlite3_step(pStmt);
53836 db->init.reopenMemdb = 0;
53837 if( rc!=SQLITE_DONE101 ){
53838 rc = SQLITE_ERROR1;
53839 goto end_deserialize;
53840 }
53841 p = memdbFromDbSchema(db, zSchema);
53842 if( p==0 ){
53843 rc = SQLITE_ERROR1;
53844 }else{
53845 MemStore *pStore = p->pStore;
53846 pStore->aData = pData;
53847 pData = 0;
53848 pStore->sz = szDb;
53849 pStore->szAlloc = szBuf;
53850 pStore->szMax = szBuf;
53851 if( pStore->szMax<sqlite3GlobalConfigsqlite3Config.mxMemdbSize ){
53852 pStore->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
53853 }
53854 pStore->mFlags = mFlags;
53855 rc = SQLITE_OK0;
53856 }
53857
53858end_deserialize:
53859 sqlite3_finalize(pStmt);
53860 if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1)!=0 ){
53861 sqlite3_free(pData);
53862 }
53863 sqlite3_mutex_leave(db->mutex);
53864 return rc;
53865}
53866
53867/*
53868** Return true if the VFS is the memvfs.
53869*/
53870SQLITE_PRIVATEstatic int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
53871 return pVfs==&memdb_vfs;
53872}
53873
53874/*
53875** This routine is called when the extension is loaded.
53876** Register the new VFS.
53877*/
53878SQLITE_PRIVATEstatic int sqlite3MemdbInit(void){
53879 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
53880 unsigned int sz;
53881 if( NEVER(pLower==0)(pLower==0) ) return SQLITE_ERROR1;
53882 sz = pLower->szOsFile;
53883 memdb_vfs.pAppData = pLower;
53884 /* The following conditional can only be true when compiled for
53885 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave
53886 ** it in, to be safe, but it is marked as NO_TEST since there
53887 ** is no way to reach it under most builds. */
53888 if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/
53889 memdb_vfs.szOsFile = sz;
53890 return sqlite3_vfs_register(&memdb_vfs, 0);
53891}
53892#endif /* SQLITE_OMIT_DESERIALIZE */
53893
53894/************** End of memdb.c ***********************************************/
53895/************** Begin file bitvec.c ******************************************/
53896/*
53897** 2008 February 16
53898**
53899** The author disclaims copyright to this source code. In place of
53900** a legal notice, here is a blessing:
53901**
53902** May you do good and not evil.
53903** May you find forgiveness for yourself and forgive others.
53904** May you share freely, never taking more than you give.
53905**
53906*************************************************************************
53907** This file implements an object that represents a fixed-length
53908** bitmap. Bits are numbered starting with 1.
53909**
53910** A bitmap is used to record which pages of a database file have been
53911** journalled during a transaction, or which pages have the "dont-write"
53912** property. Usually only a few pages are meet either condition.
53913** So the bitmap is usually sparse and has low cardinality.
53914** But sometimes (for example when during a DROP of a large table) most
53915** or all of the pages in a database can get journalled. In those cases,
53916** the bitmap becomes dense with high cardinality. The algorithm needs
53917** to handle both cases well.
53918**
53919** The size of the bitmap is fixed when the object is created.
53920**
53921** All bits are clear when the bitmap is created. Individual bits
53922** may be set or cleared one at a time.
53923**
53924** Test operations are about 100 times more common that set operations.
53925** Clear operations are exceedingly rare. There are usually between
53926** 5 and 500 set operations per Bitvec object, though the number of sets can
53927** sometimes grow into tens of thousands or larger. The size of the
53928** Bitvec object is the number of pages in the database file at the
53929** start of a transaction, and is thus usually less than a few thousand,
53930** but can be as large as 2 billion for a really big database.
53931*/
53932/* #include "sqliteInt.h" */
53933
53934/* Size of the Bitvec structure in bytes. */
53935#define BITVEC_SZ512 512
53936
53937/* Round the union size down to the nearest pointer boundary, since that's how
53938** it will be aligned within the Bitvec struct. */
53939#define BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) \
53940 (((BITVEC_SZ512-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
53941
53942/* Type of the array "element" for the bitmap representation.
53943** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
53944** Setting this to the "natural word" size of your CPU may improve
53945** performance. */
53946#define BITVEC_TELEMu8 u8
53947/* Size, in bits, of the bitmap element. */
53948#define BITVEC_SZELEM8 8
53949/* Number of elements in a bitmap array. */
53950#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))
53951/* Number of bits in the bitmap array. */
53952#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)
53953
53954/* Number of u32 values in hash table. */
53955#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))
53956/* Maximum number of entries in hash table before
53957** sub-dividing and re-hashing. */
53958#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)
53959/* Hashing function for the aHash representation.
53960** Empirical testing showed that the *37 multiplier
53961** (an arbitrary prime)in the hash function provided
53962** no fewer collisions than the no-op *1. */
53963#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))
)
53964
53965#define BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(Bitvec *))
53966
53967
53968/*
53969** A bitmap is an instance of the following structure.
53970**
53971** This bitmap records the existence of zero or more bits
53972** with values between 1 and iSize, inclusive.
53973**
53974** There are three possible representations of the bitmap.
53975** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
53976** bitmap. The least significant bit is bit 1.
53977**
53978** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
53979** a hash table that will hold up to BITVEC_MXHASH distinct values.
53980**
53981** Otherwise, the value i is redirected into one of BITVEC_NPTR
53982** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
53983** handles up to iDivisor separate values of i. apSub[0] holds
53984** values between 1 and iDivisor. apSub[1] holds values between
53985** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
53986** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
53987** to hold deal with values between 1 and iDivisor.
53988*/
53989struct Bitvec {
53990 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
53991 u32 nSet; /* Number of bits that are set - only valid for aHash
53992 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
53993 ** this would be 125. */
53994 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
53995 /* Should >=0 for apSub element. */
53996 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
53997 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
53998 union {
53999 BITVEC_TELEMu8 aBitmap[BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
]; /* Bitmap representation */
54000 u32 aHash[BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
]; /* Hash table representation */
54001 Bitvec *apSub[BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
]; /* Recursive representation */
54002 } u;
54003};
54004
54005/*
54006** Create a new bitmap object able to handle bits between 0 and iSize,
54007** inclusive. Return a pointer to the new object. Return NULL if
54008** malloc fails.
54009*/
54010SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32 iSize){
54011 Bitvec *p;
54012 assert( sizeof(*p)==BITVEC_SZ )((void) (0));
54013 p = sqlite3MallocZero( sizeof(*p) );
54014 if( p ){
54015 p->iSize = iSize;
54016 }
54017 return p;
54018}
54019
54020/*
54021** Check to see if the i-th bit is set. Return true or false.
54022** If p is NULL (if the bitmap has not been created) or if
54023** i is out of range, then return false.
54024*/
54025SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
54026 assert( p!=0 )((void) (0));
54027 i--;
54028 if( i>=p->iSize ) return 0;
54029 while( p->iDivisor ){
54030 u32 bin = i/p->iDivisor;
54031 i = i%p->iDivisor;
54032 p = p->u.apSub[bin];
54033 if (!p) {
54034 return 0;
54035 }
54036 }
54037 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
54038 return (p->u.aBitmap[i/BITVEC_SZELEM8] & (1<<(i&(BITVEC_SZELEM8-1))))!=0;
54039 } else{
54040 u32 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
54041 while( p->u.aHash[h] ){
54042 if( p->u.aHash[h]==i ) return 1;
54043 h = (h+1) % BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
;
54044 }
54045 return 0;
54046 }
54047}
54048SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec *p, u32 i){
54049 return p!=0 && sqlite3BitvecTestNotNull(p,i);
54050}
54051
54052/*
54053** Set the i-th bit. Return 0 on success and an error code if
54054** anything goes wrong.
54055**
54056** This routine might cause sub-bitmaps to be allocated. Failing
54057** to get the memory needed to hold the sub-bitmap is the only
54058** that can go wrong with an insert, assuming p and i are valid.
54059**
54060** The calling function must ensure that p is a valid Bitvec object
54061** and that the value for "i" is within range of the Bitvec object.
54062** Otherwise the behavior is undefined.
54063*/
54064SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec *p, u32 i){
54065 u32 h;
54066 if( p==0 ) return SQLITE_OK0;
54067 assert( i>0 )((void) (0));
54068 assert( i<=p->iSize )((void) (0));
54069 i--;
54070 while((p->iSize > BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
) && p->iDivisor) {
54071 u32 bin = i/p->iDivisor;
54072 i = i%p->iDivisor;
54073 if( p->u.apSub[bin]==0 ){
54074 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
54075 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT7;
54076 }
54077 p = p->u.apSub[bin];
54078 }
54079 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
54080 p->u.aBitmap[i/BITVEC_SZELEM8] |= 1 << (i&(BITVEC_SZELEM8-1));
54081 return SQLITE_OK0;
54082 }
54083 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
54084 /* if there wasn't a hash collision, and this doesn't */
54085 /* completely fill the hash, then just add it without */
54086 /* worrying about sub-dividing and re-hashing. */
54087 if( !p->u.aHash[h] ){
54088 if (p->nSet<(BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
-1)) {
54089 goto bitvec_set_end;
54090 } else {
54091 goto bitvec_set_rehash;
54092 }
54093 }
54094 /* there was a collision, check to see if it's already */
54095 /* in hash, if not, try to find a spot for it */
54096 do {
54097 if( p->u.aHash[h]==i ) return SQLITE_OK0;
54098 h++;
54099 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
54100 } while( p->u.aHash[h] );
54101 /* we didn't find it in the hash. h points to the first */
54102 /* available free spot. check to see if this is going to */
54103 /* make our hash too "full". */
54104bitvec_set_rehash:
54105 if( p->nSet>=BITVEC_MXHASH(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u32))/2)
){
54106 unsigned int j;
54107 int rc;
54108 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash))sqlite3DbMallocRaw(0,sizeof(p->u.aHash));
54109 if( aiValues==0 ){
54110 return SQLITE_NOMEM_BKPT7;
54111 }else{
54112 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54113 memset(p->u.apSub, 0, sizeof(p->u.apSub));
54114 p->iDivisor = (p->iSize + BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
- 1)/BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
;
54115 rc = sqlite3BitvecSet(p, i);
54116 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
54117 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
54118 }
54119 sqlite3StackFree(0, aiValues)sqlite3DbFree(0,aiValues);
54120 return rc;
54121 }
54122 }
54123bitvec_set_end:
54124 p->nSet++;
54125 p->u.aHash[h] = i;
54126 return SQLITE_OK0;
54127}
54128
54129/*
54130** Clear the i-th bit.
54131**
54132** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
54133** that BitvecClear can use to rebuilt its hash table.
54134*/
54135SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
54136 if( p==0 ) return;
54137 assert( i>0 )((void) (0));
54138 i--;
54139 while( p->iDivisor ){
54140 u32 bin = i/p->iDivisor;
54141 i = i%p->iDivisor;
54142 p = p->u.apSub[bin];
54143 if (!p) {
54144 return;
54145 }
54146 }
54147 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
54148 p->u.aBitmap[i/BITVEC_SZELEM8] &= ~(1 << (i&(BITVEC_SZELEM8-1)));
54149 }else{
54150 unsigned int j;
54151 u32 *aiValues = pBuf;
54152 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54153 memset(p->u.aHash, 0, sizeof(p->u.aHash));
54154 p->nSet = 0;
54155 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
54156 if( aiValues[j] && aiValues[j]!=(i+1) ){
54157 u32 h = BITVEC_HASH(aiValues[j]-1)(((aiValues[j]-1)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*
))*sizeof(Bitvec*))/sizeof(u32)))
;
54158 p->nSet++;
54159 while( p->u.aHash[h] ){
54160 h++;
54161 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
54162 }
54163 p->u.aHash[h] = aiValues[j];
54164 }
54165 }
54166 }
54167}
54168
54169/*
54170** Destroy a bitmap object. Reclaim all memory used.
54171*/
54172SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec *p){
54173 if( p==0 ) return;
54174 if( p->iDivisor ){
54175 unsigned int i;
54176 for(i=0; i<BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
; i++){
54177 sqlite3BitvecDestroy(p->u.apSub[i]);
54178 }
54179 }
54180 sqlite3_free(p);
54181}
54182
54183/*
54184** Return the value of the iSize parameter specified when Bitvec *p
54185** was created.
54186*/
54187SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec *p){
54188 return p->iSize;
54189}
54190
54191#ifndef SQLITE_UNTESTABLE
54192/*
54193** Let V[] be an array of unsigned characters sufficient to hold
54194** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
54195** Then the following macros can be used to set, clear, or test
54196** individual bits within V.
54197*/
54198#define SETBIT(V,I)V[I>>3] |= (1<<(I&7)) V[I>>3] |= (1<<(I&7))
54199#define CLEARBIT(V,I)V[I>>3] &= ~(1<<(I&7)) V[I>>3] &= ~(1<<(I&7))
54200#define TESTBIT(V,I)(V[I>>3]&(1<<(I&7)))!=0 (V[I>>3]&(1<<(I&7)))!=0
54201
54202/*
54203** This routine runs an extensive test of the Bitvec code.
54204**
54205** The input is an array of integers that acts as a program
54206** to test the Bitvec. The integers are opcodes followed
54207** by 0, 1, or 3 operands, depending on the opcode. Another
54208** opcode follows immediately after the last operand.
54209**
54210** There are 6 opcodes numbered from 0 through 5. 0 is the
54211** "halt" opcode and causes the test to end.
54212**
54213** 0 Halt and return the number of errors
54214** 1 N S X Set N bits beginning with S and incrementing by X
54215** 2 N S X Clear N bits beginning with S and incrementing by X
54216** 3 N Set N randomly chosen bits
54217** 4 N Clear N randomly chosen bits
54218** 5 N S X Set N bits from S increment X in array only, not in bitvec
54219**
54220** The opcodes 1 through 4 perform set and clear operations are performed
54221** on both a Bitvec object and on a linear array of bits obtained from malloc.
54222** Opcode 5 works on the linear array only, not on the Bitvec.
54223** Opcode 5 is used to deliberately induce a fault in order to
54224** confirm that error detection works.
54225**
54226** At the conclusion of the test the linear array is compared
54227** against the Bitvec object. If there are any differences,
54228** an error is returned. If they are the same, zero is returned.
54229**
54230** If a memory allocation error occurs, return -1.
54231*/
54232SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int sz, int *aOp){
54233 Bitvec *pBitvec = 0;
54234 unsigned char *pV = 0;
54235 int rc = -1;
54236 int i, nx, pc, op;
54237 void *pTmpSpace;
54238
54239 /* Allocate the Bitvec to be tested and a linear array of
54240 ** bits to act as the reference */
54241 pBitvec = sqlite3BitvecCreate( sz );
54242 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
54243 pTmpSpace = sqlite3_malloc64(BITVEC_SZ512);
54244 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
54245
54246 /* NULL pBitvec tests */
54247 sqlite3BitvecSet(0, 1);
54248 sqlite3BitvecClear(0, 1, pTmpSpace);
54249
54250 /* Run the program */
54251 pc = i = 0;
54252 while( (op = aOp[pc])!=0 ){
54253 switch( op ){
54254 case 1:
54255 case 2:
54256 case 5: {
54257 nx = 4;
54258 i = aOp[pc+2] - 1;
54259 aOp[pc+2] += aOp[pc+3];
54260 break;
54261 }
54262 case 3:
54263 case 4:
54264 default: {
54265 nx = 2;
54266 sqlite3_randomness(sizeof(i), &i);
54267 break;
54268 }
54269 }
54270 if( (--aOp[pc+1]) > 0 ) nx = 0;
54271 pc += nx;
54272 i = (i & 0x7fffffff)%sz;
54273 if( (op & 1)!=0 ){
54274 SETBIT(pV, (i+1))pV[(i+1)>>3] |= (1<<((i+1)&7));
54275 if( op!=5 ){
54276 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
54277 }
54278 }else{
54279 CLEARBIT(pV, (i+1))pV[(i+1)>>3] &= ~(1<<((i+1)&7));
54280 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
54281 }
54282 }
54283
54284 /* Test to make sure the linear array exactly matches the
54285 ** Bitvec object. Start with the assumption that they do
54286 ** match (rc==0). Change rc to non-zero if a discrepancy
54287 ** is found.
54288 */
54289 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
54290 + sqlite3BitvecTest(pBitvec, 0)
54291 + (sqlite3BitvecSize(pBitvec) - sz);
54292 for(i=1; i<=sz; i++){
54293 if( (TESTBIT(pV,i)(pV[i>>3]&(1<<(i&7)))!=0)!=sqlite3BitvecTest(pBitvec,i) ){
54294 rc = i;
54295 break;
54296 }
54297 }
54298
54299 /* Free allocated structure */
54300bitvec_end:
54301 sqlite3_free(pTmpSpace);
54302 sqlite3_free(pV);
54303 sqlite3BitvecDestroy(pBitvec);
54304 return rc;
54305}
54306#endif /* SQLITE_UNTESTABLE */
54307
54308/************** End of bitvec.c **********************************************/
54309/************** Begin file pcache.c ******************************************/
54310/*
54311** 2008 August 05
54312**
54313** The author disclaims copyright to this source code. In place of
54314** a legal notice, here is a blessing:
54315**
54316** May you do good and not evil.
54317** May you find forgiveness for yourself and forgive others.
54318** May you share freely, never taking more than you give.
54319**
54320*************************************************************************
54321** This file implements that page cache.
54322*/
54323/* #include "sqliteInt.h" */
54324
54325/*
54326** A complete page cache is an instance of this structure. Every
54327** entry in the cache holds a single page of the database file. The
54328** btree layer only operates on the cached copy of the database pages.
54329**
54330** A page cache entry is "clean" if it exactly matches what is currently
54331** on disk. A page is "dirty" if it has been modified and needs to be
54332** persisted to disk.
54333**
54334** pDirty, pDirtyTail, pSynced:
54335** All dirty pages are linked into the doubly linked list using
54336** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
54337** such that p was added to the list more recently than p->pDirtyNext.
54338** PCache.pDirty points to the first (newest) element in the list and
54339** pDirtyTail to the last (oldest).
54340**
54341** The PCache.pSynced variable is used to optimize searching for a dirty
54342** page to eject from the cache mid-transaction. It is better to eject
54343** a page that does not require a journal sync than one that does.
54344** Therefore, pSynced is maintained so that it *almost* always points
54345** to either the oldest page in the pDirty/pDirtyTail list that has a
54346** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
54347** (so that the right page to eject can be found by following pDirtyPrev
54348** pointers).
54349*/
54350struct PCache {
54351 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
54352 PgHdr *pSynced; /* Last synced page in dirty page list */
54353 i64 nRefSum; /* Sum of ref counts over all pages */
54354 int szCache; /* Configured cache size */
54355 int szSpill; /* Size before spilling occurs */
54356 int szPage; /* Size of every page in this cache */
54357 int szExtra; /* Size of extra space for each page */
54358 u8 bPurgeable; /* True if pages are on backing store */
54359 u8 eCreate; /* eCreate value for for xFetch() */
54360 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
54361 void *pStress; /* Argument to xStress */
54362 sqlite3_pcache *pCache; /* Pluggable cache module */
54363};
54364
54365/********************************** Test and Debug Logic **********************/
54366/*
54367** Debug tracing macros. Enable by by changing the "0" to "1" and
54368** recompiling.
54369**
54370** When sqlite3PcacheTrace is 1, single line trace messages are issued.
54371** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
54372** is displayed for many operations, resulting in a lot of output.
54373*/
54374#if defined(SQLITE_DEBUG) && 0
54375 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
54376 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
54377# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
54378 static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){
54379 PgHdr *pPg;
54380 unsigned char *a;
54381 int j;
54382 if( pLower==0 ){
54383 printf("%3d: NULL\n", i);
54384 }else{
54385 pPg = (PgHdr*)pLower->pExtra;
54386 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
54387 a = (unsigned char *)pLower->pBuf;
54388 for(j=0; j<12; j++) printf("%02x", a[j]);
54389 printf(" ptr %p\n", pPg);
54390 }
54391 }
54392 static void pcacheDump(PCache *pCache){
54393 int N;
54394 int i;
54395 sqlite3_pcache_page *pLower;
54396
54397 if( sqlite3PcacheTrace<2 ) return;
54398 if( pCache->pCache==0 ) return;
54399 N = sqlite3PcachePagecount(pCache);
54400 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
54401 for(i=1; i<=N; i++){
54402 pLower = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, i, 0);
54403 pcachePageTrace(i, pLower);
54404 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
54405 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(pCache->pCache, pLower, 0);
54406 }
54407 }
54408 }
54409#else
54410# define pcacheTrace(X)
54411# define pcachePageTrace(PGNO, X)
54412# define pcacheDump(X)
54413#endif
54414
54415/*
54416** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
54417** This routine runs inside of assert() statements only.
54418*/
54419#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
54420static int pageOnDirtyList(PCache *pCache, PgHdr *pPg)1{
54421 PgHdr *p;
54422 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54423 if( p==pPg ) return 1;
54424 }
54425 return 0;
54426}
54427static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg)1{
54428 PgHdr *p;
54429 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54430 if( p==pPg ) return 0;
54431 }
54432 return 1;
54433}
54434#else
54435# define pageOnDirtyList(A,B)1 1
54436# define pageNotOnDirtyList(A,B)1 1
54437#endif
54438
54439/*
54440** Check invariants on a PgHdr entry. Return true if everything is OK.
54441** Return false if any invariant is violated.
54442**
54443** This routine is for use inside of assert() statements only. For
54444** example:
54445**
54446** assert( sqlite3PcachePageSanity(pPg) );
54447*/
54448#ifdef SQLITE_DEBUG
54449SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr *pPg){
54450 PCache *pCache;
54451 assert( pPg!=0 )((void) (0));
54452 assert( pPg->pgno>0 || pPg->pPager==0 )((void) (0)); /* Page number is 1 or more */
54453 pCache = pPg->pCache;
54454 assert( pCache!=0 )((void) (0)); /* Every page has an associated PCache */
54455 if( pPg->flags & PGHDR_CLEAN0x001 ){
54456 assert( (pPg->flags & PGHDR_DIRTY)==0 )((void) (0));/* Cannot be both CLEAN and DIRTY */
54457 assert( pageNotOnDirtyList(pCache, pPg) )((void) (0));/* CLEAN pages not on dirtylist */
54458 }else{
54459 assert( (pPg->flags & PGHDR_DIRTY)!=0 )((void) (0));/* If not CLEAN must be DIRTY */
54460 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg )((void) (0));
54461 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg )((void) (0));
54462 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg )((void) (0));
54463 assert( pageOnDirtyList(pCache, pPg) )((void) (0));
54464 }
54465 /* WRITEABLE pages must also be DIRTY */
54466 if( pPg->flags & PGHDR_WRITEABLE0x004 ){
54467 assert( pPg->flags & PGHDR_DIRTY )((void) (0)); /* WRITEABLE implies DIRTY */
54468 }
54469 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
54470 ** for example, when using the sqlite3PagerDontWrite() optimization:
54471 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
54472 ** (2) Page X moved to freelist, WRITEABLE is cleared
54473 ** (3) Page X reused, WRITEABLE is set again
54474 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
54475 ** in step 3, and page might be written into the database without first
54476 ** syncing the rollback journal, which might cause corruption on a power
54477 ** loss.
54478 **
54479 ** Another example is when the database page size is smaller than the
54480 ** disk sector size. When any page of a sector is journalled, all pages
54481 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
54482 ** in case they are later modified, since all pages in the same sector
54483 ** must be journalled and synced before any of those pages can be safely
54484 ** written.
54485 */
54486 return 1;
54487}
54488#endif /* SQLITE_DEBUG */
54489
54490
54491/********************************** Linked List Management ********************/
54492
54493/* Allowed values for second argument to pcacheManageDirtyList() */
54494#define PCACHE_DIRTYLIST_REMOVE1 1 /* Remove pPage from dirty list */
54495#define PCACHE_DIRTYLIST_ADD2 2 /* Add pPage to the dirty list */
54496#define PCACHE_DIRTYLIST_FRONT3 3 /* Move pPage to the front of the list */
54497
54498/*
54499** Manage pPage's participation on the dirty list. Bits of the addRemove
54500** argument determines what operation to do. The 0x01 bit means first
54501** remove pPage from the dirty list. The 0x02 means add pPage back to
54502** the dirty list. Doing both moves pPage to the front of the dirty list.
54503*/
54504static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
54505 PCache *p = pPage->pCache;
54506
54507 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
54508 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
54509 pPage->pgno));
54510 if( addRemove & PCACHE_DIRTYLIST_REMOVE1 ){
54511 assert( pPage->pDirtyNext || pPage==p->pDirtyTail )((void) (0));
54512 assert( pPage->pDirtyPrev || pPage==p->pDirty )((void) (0));
54513
54514 /* Update the PCache1.pSynced variable if necessary. */
54515 if( p->pSynced==pPage ){
54516 p->pSynced = pPage->pDirtyPrev;
54517 }
54518
54519 if( pPage->pDirtyNext ){
54520 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
54521 }else{
54522 assert( pPage==p->pDirtyTail )((void) (0));
54523 p->pDirtyTail = pPage->pDirtyPrev;
54524 }
54525 if( pPage->pDirtyPrev ){
54526 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
54527 }else{
54528 /* If there are now no dirty pages in the cache, set eCreate to 2.
54529 ** This is an optimization that allows sqlite3PcacheFetch() to skip
54530 ** searching for a dirty page to eject from the cache when it might
54531 ** otherwise have to. */
54532 assert( pPage==p->pDirty )((void) (0));
54533 p->pDirty = pPage->pDirtyNext;
54534 assert( p->bPurgeable || p->eCreate==2 )((void) (0));
54535 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
54536 assert( p->bPurgeable==0 || p->eCreate==1 )((void) (0));
54537 p->eCreate = 2;
54538 }
54539 }
54540 }
54541 if( addRemove & PCACHE_DIRTYLIST_ADD2 ){
54542 pPage->pDirtyPrev = 0;
54543 pPage->pDirtyNext = p->pDirty;
54544 if( pPage->pDirtyNext ){
54545 assert( pPage->pDirtyNext->pDirtyPrev==0 )((void) (0));
54546 pPage->pDirtyNext->pDirtyPrev = pPage;
54547 }else{
54548 p->pDirtyTail = pPage;
54549 if( p->bPurgeable ){
54550 assert( p->eCreate==2 )((void) (0));
54551 p->eCreate = 1;
54552 }
54553 }
54554 p->pDirty = pPage;
54555
54556 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
54557 ** pSynced to point to it. Checking the NEED_SYNC flag is an
54558 ** optimization, as if pSynced points to a page with the NEED_SYNC
54559 ** flag set sqlite3PcacheFetchStress() searches through all newer
54560 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
54561 if( !p->pSynced
54562 && 0==(pPage->flags&PGHDR_NEED_SYNC0x008) /*OPTIMIZATION-IF-FALSE*/
54563 ){
54564 p->pSynced = pPage;
54565 }
54566 }
54567 pcacheDump(p);
54568}
54569
54570/*
54571** Wrapper around the pluggable caches xUnpin method. If the cache is
54572** being used for an in-memory database, this function is a no-op.
54573*/
54574static void pcacheUnpin(PgHdr *p){
54575 if( p->pCache->bPurgeable ){
54576 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
54577 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
54578 pcacheDump(p->pCache);
54579 }
54580}
54581
54582/*
54583** Compute the number of pages of cache requested. p->szCache is the
54584** cache size requested by the "PRAGMA cache_size" statement.
54585*/
54586static int numberOfCachePages(PCache *p){
54587 if( p->szCache>=0 ){
54588 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
54589 ** suggested cache size is set to N. */
54590 return p->szCache;
54591 }else{
54592 i64 n;
54593 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
54594 ** number of cache pages is adjusted to be a number of pages that would
54595 ** use approximately abs(N*1024) bytes of memory based on the current
54596 ** page size. */
54597 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
54598 if( n>1000000000 ) n = 1000000000;
54599 return (int)n;
54600 }
54601}
54602
54603/*************************************************** General Interfaces ******
54604**
54605** Initialize and shutdown the page cache subsystem. Neither of these
54606** functions are threadsafe.
54607*/
54608SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void){
54609 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
54610 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
54611 ** built-in default page cache is used instead of the application defined
54612 ** page cache. */
54613 sqlite3PCacheSetDefault();
54614 assert( sqlite3GlobalConfig.pcache2.xInit!=0 )((void) (0));
54615 }
54616 return sqlite3GlobalConfigsqlite3Config.pcache2.xInit(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
54617}
54618SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void){
54619 if( sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown ){
54620 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
54621 sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
54622 }
54623}
54624
54625/*
54626** Return the size in bytes of a PCache object.
54627*/
54628SQLITE_PRIVATEstatic int sqlite3PcacheSize(void){ return sizeof(PCache); }
54629
54630/*
54631** Create a new PCache object. Storage space to hold the object
54632** has already been allocated and is passed in as the p pointer.
54633** The caller discovers how much space needs to be allocated by
54634** calling sqlite3PcacheSize().
54635**
54636** szExtra is some extra space allocated for each page. The first
54637** 8 bytes of the extra space will be zeroed as the page is allocated,
54638** but remaining content will be uninitialized. Though it is opaque
54639** to this module, the extra space really ends up being the MemPage
54640** structure in the pager.
54641*/
54642SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
54643 int szPage, /* Size of every page */
54644 int szExtra, /* Extra space associated with each page */
54645 int bPurgeable, /* True if pages are on backing store */
54646 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
54647 void *pStress, /* Argument to xStress */
54648 PCache *p /* Preallocated space for the PCache */
54649){
54650 memset(p, 0, sizeof(PCache));
54651 p->szPage = 1;
54652 p->szExtra = szExtra;
54653 assert( szExtra>=8 )((void) (0)); /* First 8 bytes will be zeroed */
54654 p->bPurgeable = bPurgeable;
54655 p->eCreate = 2;
54656 p->xStress = xStress;
54657 p->pStress = pStress;
54658 p->szCache = 100;
54659 p->szSpill = 1;
54660 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
54661 return sqlite3PcacheSetPageSize(p, szPage);
54662}
54663
54664/*
54665** Change the page size for PCache object. The caller must ensure that there
54666** are no outstanding page references when this function is called.
54667*/
54668SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
54669 assert( pCache->nRefSum==0 && pCache->pDirty==0 )((void) (0));
54670 if( pCache->szPage ){
54671 sqlite3_pcache *pNew;
54672 pNew = sqlite3GlobalConfigsqlite3Config.pcache2.xCreate(
54673 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7),
54674 pCache->bPurgeable
54675 );
54676 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
54677 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
54678 if( pCache->pCache ){
54679 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
54680 }
54681 pCache->pCache = pNew;
54682 pCache->szPage = szPage;
54683 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
54684 }
54685 return SQLITE_OK0;
54686}
54687
54688/*
54689** Try to obtain a page from the cache.
54690**
54691** This routine returns a pointer to an sqlite3_pcache_page object if
54692** such an object is already in cache, or if a new one is created.
54693** This routine returns a NULL pointer if the object was not in cache
54694** and could not be created.
54695**
54696** The createFlags should be 0 to check for existing pages and should
54697** be 3 (not 1, but 3) to try to create a new page.
54698**
54699** If the createFlag is 0, then NULL is always returned if the page
54700** is not already in the cache. If createFlag is 1, then a new page
54701** is created only if that can be done without spilling dirty pages
54702** and without exceeding the cache size limit.
54703**
54704** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
54705** initialize the sqlite3_pcache_page object and convert it into a
54706** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
54707** routines are split this way for performance reasons. When separated
54708** they can both (usually) operate without having to push values to
54709** the stack on entry and pop them back off on exit, which saves a
54710** lot of pushing and popping.
54711*/
54712SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(
54713 PCache *pCache, /* Obtain the page from this cache */
54714 Pgno pgno, /* Page number to obtain */
54715 int createFlag /* If true, create page if it does not exist already */
54716){
54717 int eCreate;
54718 sqlite3_pcache_page *pRes;
54719
54720 assert( pCache!=0 )((void) (0));
54721 assert( pCache->pCache!=0 )((void) (0));
54722 assert( createFlag==3 || createFlag==0 )((void) (0));
54723 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) )((void) (0));
54724
54725 /* eCreate defines what to do if the page does not exist.
54726 ** 0 Do not allocate a new page. (createFlag==0)
54727 ** 1 Allocate a new page if doing so is inexpensive.
54728 ** (createFlag==1 AND bPurgeable AND pDirty)
54729 ** 2 Allocate a new page even it doing so is difficult.
54730 ** (createFlag==1 AND !(bPurgeable AND pDirty)
54731 */
54732 eCreate = createFlag & pCache->eCreate;
54733 assert( eCreate==0 || eCreate==1 || eCreate==2 )((void) (0));
54734 assert( createFlag==0 || pCache->eCreate==eCreate )((void) (0));
54735 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) )((void) (0));
54736 pRes = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, eCreate);
54737 pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno,
54738 createFlag?" create":"",pRes));
54739 pcachePageTrace(pgno, pRes);
54740 return pRes;
54741}
54742
54743/*
54744** If the sqlite3PcacheFetch() routine is unable to allocate a new
54745** page because no clean pages are available for reuse and the cache
54746** size limit has been reached, then this routine can be invoked to
54747** try harder to allocate a page. This routine might invoke the stress
54748** callback to spill dirty pages to the journal. It will then try to
54749** allocate the new page and will only fail to allocate a new page on
54750** an OOM error.
54751**
54752** This routine should be invoked only after sqlite3PcacheFetch() fails.
54753*/
54754SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(
54755 PCache *pCache, /* Obtain the page from this cache */
54756 Pgno pgno, /* Page number to obtain */
54757 sqlite3_pcache_page **ppPage /* Write result here */
54758){
54759 PgHdr *pPg;
54760 if( pCache->eCreate==2 ) return 0;
54761
54762 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
54763 /* Find a dirty page to write-out and recycle. First try to find a
54764 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
54765 ** cleared), but if that is not possible settle for any other
54766 ** unreferenced dirty page.
54767 **
54768 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
54769 ** flag is currently referenced, then the following may leave pSynced
54770 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
54771 ** cleared). This is Ok, as pSynced is just an optimization. */
54772 for(pPg=pCache->pSynced;
54773 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC0x008));
54774 pPg=pPg->pDirtyPrev
54775 );
54776 pCache->pSynced = pPg;
54777 if( !pPg ){
54778 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
54779 }
54780 if( pPg ){
54781 int rc;
54782#ifdef SQLITE_LOG_CACHE_SPILL
54783 sqlite3_log(SQLITE_FULL13,
54784 "spill page %d making room for %d - cache used: %d/%d",
54785 pPg->pgno, pgno,
54786 sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache),
54787 numberOfCachePages(pCache));
54788#endif
54789 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
54790 rc = pCache->xStress(pCache->pStress, pPg);
54791 pcacheDump(pCache);
54792 if( rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 ){
54793 return rc;
54794 }
54795 }
54796 }
54797 *ppPage = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, 2);
54798 return *ppPage==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
54799}
54800
54801/*
54802** This is a helper routine for sqlite3PcacheFetchFinish()
54803**
54804** In the uncommon case where the page being fetched has not been
54805** initialized, this routine is invoked to do the initialization.
54806** This routine is broken out into a separate function since it
54807** requires extra stack manipulation that can be avoided in the common
54808** case.
54809*/
54810static SQLITE_NOINLINE__attribute__((noinline)) PgHdr *pcacheFetchFinishWithInit(
54811 PCache *pCache, /* Obtain the page from this cache */
54812 Pgno pgno, /* Page number obtained */
54813 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
54814){
54815 PgHdr *pPgHdr;
54816 assert( pPage!=0 )((void) (0));
54817 pPgHdr = (PgHdr*)pPage->pExtra;
54818 assert( pPgHdr->pPage==0 )((void) (0));
54819 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)__builtin_offsetof(PgHdr, pDirty));
54820 pPgHdr->pPage = pPage;
54821 pPgHdr->pData = pPage->pBuf;
54822 pPgHdr->pExtra = (void *)&pPgHdr[1];
54823 memset(pPgHdr->pExtra, 0, 8);
54824 assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) )((void) (0));
54825 pPgHdr->pCache = pCache;
54826 pPgHdr->pgno = pgno;
54827 pPgHdr->flags = PGHDR_CLEAN0x001;
54828 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
54829}
54830
54831/*
54832** This routine converts the sqlite3_pcache_page object returned by
54833** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
54834** must be called after sqlite3PcacheFetch() in order to get a usable
54835** result.
54836*/
54837SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(
54838 PCache *pCache, /* Obtain the page from this cache */
54839 Pgno pgno, /* Page number obtained */
54840 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
54841){
54842 PgHdr *pPgHdr;
54843
54844 assert( pPage!=0 )((void) (0));
54845 pPgHdr = (PgHdr *)pPage->pExtra;
54846
54847 if( !pPgHdr->pPage ){
54848 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
54849 }
54850 pCache->nRefSum++;
54851 pPgHdr->nRef++;
54852 assert( sqlite3PcachePageSanity(pPgHdr) )((void) (0));
54853 return pPgHdr;
54854}
54855
54856/*
54857** Decrement the reference count on a page. If the page is clean and the
54858** reference count drops to 0, then it is made eligible for recycling.
54859*/
54860SQLITE_PRIVATEstatic void SQLITE_NOINLINE__attribute__((noinline)) sqlite3PcacheRelease(PgHdr *p){
54861 assert( p->nRef>0 )((void) (0));
54862 p->pCache->nRefSum--;
54863 if( (--p->nRef)==0 ){
54864 if( p->flags&PGHDR_CLEAN0x001 ){
54865 pcacheUnpin(p);
54866 }else{
54867 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
54868 assert( sqlite3PcachePageSanity(p) )((void) (0));
54869 }
54870 }
54871}
54872
54873/*
54874** Increase the reference count of a supplied page by 1.
54875*/
54876SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr *p){
54877 assert(p->nRef>0)((void) (0));
54878 assert( sqlite3PcachePageSanity(p) )((void) (0));
54879 p->nRef++;
54880 p->pCache->nRefSum++;
54881}
54882
54883/*
54884** Drop a page from the cache. There must be exactly one reference to the
54885** page. This function deletes that reference, so after it returns the
54886** page pointed to by p is invalid.
54887*/
54888SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr *p){
54889 assert( p->nRef==1 )((void) (0));
54890 assert( sqlite3PcachePageSanity(p) )((void) (0));
54891 if( p->flags&PGHDR_DIRTY0x002 ){
54892 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
54893 }
54894 p->pCache->nRefSum--;
54895 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
54896}
54897
54898/*
54899** Make sure the page is marked as dirty. If it isn't dirty already,
54900** make it so.
54901*/
54902SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr *p){
54903 assert( p->nRef>0 )((void) (0));
54904 assert( sqlite3PcachePageSanity(p) )((void) (0));
54905 if( p->flags & (PGHDR_CLEAN0x001|PGHDR_DONT_WRITE0x010) ){ /*OPTIMIZATION-IF-FALSE*/
54906 p->flags &= ~PGHDR_DONT_WRITE0x010;
54907 if( p->flags & PGHDR_CLEAN0x001 ){
54908 p->flags ^= (PGHDR_DIRTY0x002|PGHDR_CLEAN0x001);
54909 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
54910 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY )((void) (0));
54911 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD2);
54912 assert( sqlite3PcachePageSanity(p) )((void) (0));
54913 }
54914 assert( sqlite3PcachePageSanity(p) )((void) (0));
54915 }
54916}
54917
54918/*
54919** Make sure the page is marked as clean. If it isn't clean already,
54920** make it so.
54921*/
54922SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr *p){
54923 assert( sqlite3PcachePageSanity(p) )((void) (0));
54924 assert( (p->flags & PGHDR_DIRTY)!=0 )((void) (0));
54925 assert( (p->flags & PGHDR_CLEAN)==0 )((void) (0));
54926 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
54927 p->flags &= ~(PGHDR_DIRTY0x002|PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
54928 p->flags |= PGHDR_CLEAN0x001;
54929 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
54930 assert( sqlite3PcachePageSanity(p) )((void) (0));
54931 if( p->nRef==0 ){
54932 pcacheUnpin(p);
54933 }
54934}
54935
54936/*
54937** Make every page in the cache clean.
54938*/
54939SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache *pCache){
54940 PgHdr *p;
54941 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
54942 while( (p = pCache->pDirty)!=0 ){
54943 sqlite3PcacheMakeClean(p);
54944 }
54945}
54946
54947/*
54948** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
54949*/
54950SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache *pCache){
54951 PgHdr *p;
54952 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
54953 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54954 p->flags &= ~(PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
54955 }
54956 pCache->pSynced = pCache->pDirtyTail;
54957}
54958
54959/*
54960** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
54961*/
54962SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *pCache){
54963 PgHdr *p;
54964 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54965 p->flags &= ~PGHDR_NEED_SYNC0x008;
54966 }
54967 pCache->pSynced = pCache->pDirtyTail;
54968}
54969
54970/*
54971** Change the page number of page p to newPgno.
54972*/
54973SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
54974 PCache *pCache = p->pCache;
54975 sqlite3_pcache_page *pOther;
54976 assert( p->nRef>0 )((void) (0));
54977 assert( newPgno>0 )((void) (0));
54978 assert( sqlite3PcachePageSanity(p) )((void) (0));
54979 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
54980 pOther = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, newPgno, 0);
54981 if( pOther ){
54982 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
54983 assert( pXPage->nRef==0 )((void) (0));
54984 pXPage->nRef++;
54985 pCache->nRefSum++;
54986 sqlite3PcacheDrop(pXPage);
54987 }
54988 sqlite3GlobalConfigsqlite3Config.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
54989 p->pgno = newPgno;
54990 if( (p->flags&PGHDR_DIRTY0x002) && (p->flags&PGHDR_NEED_SYNC0x008) ){
54991 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
54992 assert( sqlite3PcachePageSanity(p) )((void) (0));
54993 }
54994}
54995
54996/*
54997** Drop every cache entry whose page number is greater than "pgno". The
54998** caller must ensure that there are no outstanding references to any pages
54999** other than page 1 with a page number greater than pgno.
55000**
55001** If there is a reference to page 1 and the pgno parameter passed to this
55002** function is 0, then the data area associated with page 1 is zeroed, but
55003** the page object is not dropped.
55004*/
55005SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
55006 if( pCache->pCache ){
55007 PgHdr *p;
55008 PgHdr *pNext;
55009 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
55010 for(p=pCache->pDirty; p; p=pNext){
55011 pNext = p->pDirtyNext;
55012 /* This routine never gets call with a positive pgno except right
55013 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
55014 ** it must be that pgno==0.
55015 */
55016 assert( p->pgno>0 )((void) (0));
55017 if( p->pgno>pgno ){
55018 assert( p->flags&PGHDR_DIRTY )((void) (0));
55019 sqlite3PcacheMakeClean(p);
55020 }
55021 }
55022 if( pgno==0 && pCache->nRefSum ){
55023 sqlite3_pcache_page *pPage1;
55024 pPage1 = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache,1,0);
55025 if( ALWAYS(pPage1)(pPage1) ){ /* Page 1 is always available in cache, because
55026 ** pCache->nRefSum>0 */
55027 memset(pPage1->pBuf, 0, pCache->szPage);
55028 pgno = 1;
55029 }
55030 }
55031 sqlite3GlobalConfigsqlite3Config.pcache2.xTruncate(pCache->pCache, pgno+1);
55032 }
55033}
55034
55035/*
55036** Close a cache.
55037*/
55038SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache *pCache){
55039 assert( pCache->pCache!=0 )((void) (0));
55040 pcacheTrace(("%p.CLOSE\n",pCache));
55041 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
55042}
55043
55044/*
55045** Discard the contents of the cache.
55046*/
55047SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache *pCache){
55048 sqlite3PcacheTruncate(pCache, 0);
55049}
55050
55051/*
55052** Merge two lists of pages connected by pDirty and in pgno order.
55053** Do not bother fixing the pDirtyPrev pointers.
55054*/
55055static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
55056 PgHdr result, *pTail;
55057 pTail = &result;
55058 assert( pA!=0 && pB!=0 )((void) (0));
55059 for(;;){
55060 if( pA->pgno<pB->pgno ){
55061 pTail->pDirty = pA;
55062 pTail = pA;
55063 pA = pA->pDirty;
55064 if( pA==0 ){
55065 pTail->pDirty = pB;
55066 break;
55067 }
55068 }else{
55069 pTail->pDirty = pB;
55070 pTail = pB;
55071 pB = pB->pDirty;
55072 if( pB==0 ){
55073 pTail->pDirty = pA;
55074 break;
55075 }
55076 }
55077 }
55078 return result.pDirty;
55079}
55080
55081/*
55082** Sort the list of pages in ascending order by pgno. Pages are
55083** connected by pDirty pointers. The pDirtyPrev pointers are
55084** corrupted by this sort.
55085**
55086** Since there cannot be more than 2^31 distinct pages in a database,
55087** there cannot be more than 31 buckets required by the merge sorter.
55088** One extra bucket is added to catch overflow in case something
55089** ever changes to make the previous sentence incorrect.
55090*/
55091#define N_SORT_BUCKET32 32
55092static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
55093 PgHdr *a[N_SORT_BUCKET32], *p;
55094 int i;
55095 memset(a, 0, sizeof(a));
55096 while( pIn ){
55097 p = pIn;
55098 pIn = p->pDirty;
55099 p->pDirty = 0;
55100 for(i=0; ALWAYS(i<N_SORT_BUCKET-1)(i<32 -1); i++){
55101 if( a[i]==0 ){
55102 a[i] = p;
55103 break;
55104 }else{
55105 p = pcacheMergeDirtyList(a[i], p);
55106 a[i] = 0;
55107 }
55108 }
55109 if( NEVER(i==N_SORT_BUCKET-1)(i==32 -1) ){
55110 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
55111 ** the input list. But that is impossible.
55112 */
55113 a[i] = pcacheMergeDirtyList(a[i], p);
55114 }
55115 }
55116 p = a[0];
55117 for(i=1; i<N_SORT_BUCKET32; i++){
55118 if( a[i]==0 ) continue;
55119 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
55120 }
55121 return p;
55122}
55123
55124/*
55125** Return a list of all dirty pages in the cache, sorted by page number.
55126*/
55127SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
55128 PgHdr *p;
55129 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55130 p->pDirty = p->pDirtyNext;
55131 }
55132 return pcacheSortDirtyList(pCache->pDirty);
55133}
55134
55135/*
55136** Return the total number of references to all pages held by the cache.
55137**
55138** This is not the total number of pages referenced, but the sum of the
55139** reference count for all pages.
55140*/
55141SQLITE_PRIVATEstatic i64 sqlite3PcacheRefCount(PCache *pCache){
55142 return pCache->nRefSum;
55143}
55144
55145/*
55146** Return the number of references to the page supplied as an argument.
55147*/
55148SQLITE_PRIVATEstatic i64 sqlite3PcachePageRefcount(PgHdr *p){
55149 return p->nRef;
55150}
55151
55152/*
55153** Return the total number of pages in the cache.
55154*/
55155SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache *pCache){
55156 assert( pCache->pCache!=0 )((void) (0));
55157 return sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache);
55158}
55159
55160#ifdef SQLITE_TEST
55161/*
55162** Get the suggested cache-size value.
55163*/
55164SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *pCache){
55165 return numberOfCachePages(pCache);
55166}
55167#endif
55168
55169/*
55170** Set the suggested cache-size value.
55171*/
55172SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
55173 assert( pCache->pCache!=0 )((void) (0));
55174 pCache->szCache = mxPage;
55175 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pCache->pCache,
55176 numberOfCachePages(pCache));
55177}
55178
55179/*
55180** Set the suggested cache-spill value. Make no changes if if the
55181** argument is zero. Return the effective cache-spill size, which will
55182** be the larger of the szSpill and szCache.
55183*/
55184SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
55185 int res;
55186 assert( p->pCache!=0 )((void) (0));
55187 if( mxPage ){
55188 if( mxPage<0 ){
55189 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
55190 }
55191 p->szSpill = mxPage;
55192 }
55193 res = numberOfCachePages(p);
55194 if( res<p->szSpill ) res = p->szSpill;
55195 return res;
55196}
55197
55198/*
55199** Free up as much memory as possible from the page cache.
55200*/
55201SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache *pCache){
55202 assert( pCache->pCache!=0 )((void) (0));
55203 sqlite3GlobalConfigsqlite3Config.pcache2.xShrink(pCache->pCache);
55204}
55205
55206/*
55207** Return the size of the header added by this middleware layer
55208** in the page-cache hierarchy.
55209*/
55210SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7); }
55211
55212/*
55213** Return the number of dirty pages currently in the cache, as a percentage
55214** of the configured cache size.
55215*/
55216SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache *pCache){
55217 PgHdr *pDirty;
55218 int nDirty = 0;
55219 int nCache = numberOfCachePages(pCache);
55220 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
55221 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
55222}
55223
55224#ifdef SQLITE_DIRECT_OVERFLOW_READ1
55225/*
55226** Return true if there are one or more dirty pages in the cache. Else false.
55227*/
55228SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache){
55229 return (pCache->pDirty!=0);
55230}
55231#endif
55232
55233#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
55234/*
55235** For all dirty pages currently in the cache, invoke the specified
55236** callback. This is only used if the SQLITE_CHECK_PAGES macro is
55237** defined.
55238*/
55239SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
55240 PgHdr *pDirty;
55241 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
55242 xIter(pDirty);
55243 }
55244}
55245#endif
55246
55247/************** End of pcache.c **********************************************/
55248/************** Begin file pcache1.c *****************************************/
55249/*
55250** 2008 November 05
55251**
55252** The author disclaims copyright to this source code. In place of
55253** a legal notice, here is a blessing:
55254**
55255** May you do good and not evil.
55256** May you find forgiveness for yourself and forgive others.
55257** May you share freely, never taking more than you give.
55258**
55259*************************************************************************
55260**
55261** This file implements the default page cache implementation (the
55262** sqlite3_pcache interface). It also contains part of the implementation
55263** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
55264** If the default page cache implementation is overridden, then neither of
55265** these two features are available.
55266**
55267** A Page cache line looks like this:
55268**
55269** -------------------------------------------------------------
55270** | database page content | PgHdr1 | MemPage | PgHdr |
55271** -------------------------------------------------------------
55272**
55273** The database page content is up front (so that buffer overreads tend to
55274** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
55275** is the extension added by the btree.c module containing information such
55276** as the database page number and how that database page is used. PgHdr
55277** is added by the pcache.c layer and contains information used to keep track
55278** of which pages are "dirty". PgHdr1 is an extension added by this
55279** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
55280** PgHdr1 contains information needed to look up a page by its page number.
55281** The superclass sqlite3_pcache_page.pBuf points to the start of the
55282** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
55283**
55284** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
55285** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
55286** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
55287** size can vary according to architecture, compile-time options, and
55288** SQLite library version number.
55289**
55290** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
55291** was defined, then the page content would be held in a separate memory
55292** allocation from the PgHdr1. This was intended to avoid clownshoe memory
55293** allocations. However, the btree layer needs a small (16-byte) overrun
55294** area after the page content buffer. The header serves as that overrun
55295** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
55296** any possibility of a memory error.
55297**
55298** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
55299** with this module. Information is passed back and forth as PgHdr1 pointers.
55300**
55301** The pcache.c and pager.c modules deal pointers to PgHdr objects.
55302** The btree.c module deals with pointers to MemPage objects.
55303**
55304** SOURCE OF PAGE CACHE MEMORY:
55305**
55306** Memory for a page might come from any of three sources:
55307**
55308** (1) The general-purpose memory allocator - sqlite3Malloc()
55309** (2) Global page-cache memory provided using sqlite3_config() with
55310** SQLITE_CONFIG_PAGECACHE.
55311** (3) PCache-local bulk allocation.
55312**
55313** The third case is a chunk of heap memory (defaulting to 100 pages worth)
55314** that is allocated when the page cache is created. The size of the local
55315** bulk allocation can be adjusted using
55316**
55317** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
55318**
55319** If N is positive, then N pages worth of memory are allocated using a single
55320** sqlite3Malloc() call and that memory is used for the first N pages allocated.
55321** Or if N is negative, then -1024*N bytes of memory are allocated and used
55322** for as many pages as can be accommodated.
55323**
55324** Only one of (2) or (3) can be used. Once the memory available to (2) or
55325** (3) is exhausted, subsequent allocations fail over to the general-purpose
55326** memory allocator (1).
55327**
55328** Earlier versions of SQLite used only methods (1) and (2). But experiments
55329** show that method (3) with N==100 provides about a 5% performance boost for
55330** common workloads.
55331*/
55332/* #include "sqliteInt.h" */
55333
55334typedef struct PCache1 PCache1;
55335typedef struct PgHdr1 PgHdr1;
55336typedef struct PgFreeslot PgFreeslot;
55337typedef struct PGroup PGroup;
55338
55339/*
55340** Each cache entry is represented by an instance of the following
55341** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
55342** directly before this structure and is used to cache the page content.
55343**
55344** When reading a corrupt database file, it is possible that SQLite might
55345** read a few bytes (no more than 16 bytes) past the end of the page buffer.
55346** It will only read past the end of the page buffer, never write. This
55347** object is positioned immediately after the page buffer to serve as an
55348** overrun area, so that overreads are harmless.
55349**
55350** Variables isBulkLocal and isAnchor were once type "u8". That works,
55351** but causes a 2-byte gap in the structure for most architectures (since
55352** pointers must be either 4 or 8-byte aligned). As this structure is located
55353** in memory directly after the associated page data, if the database is
55354** corrupt, code at the b-tree layer may overread the page buffer and
55355** read part of this structure before the corruption is detected. This
55356** can cause a valgrind error if the uninitialized gap is accessed. Using u16
55357** ensures there is no such gap, and therefore no bytes of uninitialized
55358** memory in the structure.
55359**
55360** The pLruNext and pLruPrev pointers form a double-linked circular list
55361** of all pages that are unpinned. The PGroup.lru element (which should be
55362** the only element on the list with PgHdr1.isAnchor set to 1) forms the
55363** beginning and the end of the list.
55364*/
55365struct PgHdr1 {
55366 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
55367 unsigned int iKey; /* Key value (page number) */
55368 u16 isBulkLocal; /* This page from bulk local storage */
55369 u16 isAnchor; /* This is the PGroup.lru element */
55370 PgHdr1 *pNext; /* Next in hash table chain */
55371 PCache1 *pCache; /* Cache that currently owns this page */
55372 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
55373 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
55374 /* NB: pLruPrev is only valid if pLruNext!=0 */
55375};
55376
55377/*
55378** A page is pinned if it is not on the LRU list. To be "pinned" means
55379** that the page is in active use and must not be deallocated.
55380*/
55381#define PAGE_IS_PINNED(p)((p)->pLruNext==0) ((p)->pLruNext==0)
55382#define PAGE_IS_UNPINNED(p)((p)->pLruNext!=0) ((p)->pLruNext!=0)
55383
55384/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
55385** of one or more PCaches that are able to recycle each other's unpinned
55386** pages when they are under memory pressure. A PGroup is an instance of
55387** the following object.
55388**
55389** This page cache implementation works in one of two modes:
55390**
55391** (1) Every PCache is the sole member of its own PGroup. There is
55392** one PGroup per PCache.
55393**
55394** (2) There is a single global PGroup that all PCaches are a member
55395** of.
55396**
55397** Mode 1 uses more memory (since PCache instances are not able to rob
55398** unused pages from other PCaches) but it also operates without a mutex,
55399** and is therefore often faster. Mode 2 requires a mutex in order to be
55400** threadsafe, but recycles pages more efficiently.
55401**
55402** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
55403** PGroup which is the pcache1.grp global variable and its mutex is
55404** SQLITE_MUTEX_STATIC_LRU.
55405*/
55406struct PGroup {
55407 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
55408 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
55409 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
55410 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
55411 unsigned int nPurgeable; /* Number of purgeable pages allocated */
55412 PgHdr1 lru; /* The beginning and end of the LRU list */
55413};
55414
55415/* Each page cache is an instance of the following object. Every
55416** open database file (including each in-memory database and each
55417** temporary or transient database) has a single page cache which
55418** is an instance of this object.
55419**
55420** Pointers to structures of this type are cast and returned as
55421** opaque sqlite3_pcache* handles.
55422*/
55423struct PCache1 {
55424 /* Cache configuration parameters. Page size (szPage) and the purgeable
55425 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
55426 ** cache is created and are never changed thereafter. nMax may be
55427 ** modified at any time by a call to the pcache1Cachesize() method.
55428 ** The PGroup mutex must be held when accessing nMax.
55429 */
55430 PGroup *pGroup; /* PGroup this cache belongs to */
55431 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
55432 int szPage; /* Size of database content section */
55433 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
55434 int szAlloc; /* Total size of one pcache line */
55435 int bPurgeable; /* True if cache is purgeable */
55436 unsigned int nMin; /* Minimum number of pages reserved */
55437 unsigned int nMax; /* Configured "cache_size" value */
55438 unsigned int n90pct; /* nMax*9/10 */
55439 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
55440 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
55441
55442 /* Hash table of all pages. The following variables may only be accessed
55443 ** when the accessor is holding the PGroup mutex.
55444 */
55445 unsigned int nRecyclable; /* Number of pages in the LRU list */
55446 unsigned int nPage; /* Total number of pages in apHash */
55447 unsigned int nHash; /* Number of slots in apHash[] */
55448 PgHdr1 **apHash; /* Hash table for fast lookup by key */
55449 PgHdr1 *pFree; /* List of unused pcache-local pages */
55450 void *pBulk; /* Bulk memory used by pcache-local */
55451};
55452
55453/*
55454** Free slots in the allocator used to divide up the global page cache
55455** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
55456*/
55457struct PgFreeslot {
55458 PgFreeslot *pNext; /* Next free slot */
55459};
55460
55461/*
55462** Global data used by this cache.
55463*/
55464static SQLITE_WSD struct PCacheGlobal {
55465 PGroup grp; /* The global PGroup for mode (2) */
55466
55467 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
55468 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
55469 ** fixed at sqlite3_initialize() time and do not require mutex protection.
55470 ** The nFreeSlot and pFree values do require mutex protection.
55471 */
55472 int isInit; /* True if initialized */
55473 int separateCache; /* Use a new PGroup for each PCache */
55474 int nInitPage; /* Initial bulk allocation size */
55475 int szSlot; /* Size of each free slot */
55476 int nSlot; /* The number of pcache slots */
55477 int nReserve; /* Try to keep nFreeSlot above this */
55478 void *pStart, *pEnd; /* Bounds of global page cache memory */
55479 /* Above requires no mutex. Use mutex below for variable that follow. */
55480 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
55481 PgFreeslot *pFree; /* Free page blocks */
55482 int nFreeSlot; /* Number of unused pcache slots */
55483 /* The following value requires a mutex to change. We skip the mutex on
55484 ** reading because (1) most platforms read a 32-bit integer atomically and
55485 ** (2) even if an incorrect value is read, no great harm is done since this
55486 ** is really just an optimization. */
55487 int bUnderPressure; /* True if low on PAGECACHE memory */
55488} pcache1_g;
55489
55490/*
55491** All code in this file should access the global structure above via the
55492** alias "pcache1". This ensures that the WSD emulation is used when
55493** compiling for systems that do not support real WSD.
55494*/
55495#define pcache1(pcache1_g) (GLOBAL(struct PCacheGlobal, pcache1_g)pcache1_g)
55496
55497/*
55498** Macros to enter and leave the PCache LRU mutex.
55499*/
55500#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE2==0
55501# define pcache1EnterMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
55502# define pcache1LeaveMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
55503# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 0
55504#else
55505# define pcache1EnterMutex(X)((void) (0)) sqlite3_mutex_enter((X)->mutex)
55506# define pcache1LeaveMutex(X)((void) (0)) sqlite3_mutex_leave((X)->mutex)
55507# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 1
55508#endif
55509
55510/******************************************************************************/
55511/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
55512
55513
55514/*
55515** This function is called during initialization if a static buffer is
55516** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
55517** verb to sqlite3_config(). Parameter pBuf points to an allocation large
55518** enough to contain 'n' buffers of 'sz' bytes each.
55519**
55520** This routine is called from sqlite3_initialize() and so it is guaranteed
55521** to be serialized already. There is no need for further mutexing.
55522*/
55523SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
55524 if( pcache1(pcache1_g).isInit ){
55525 PgFreeslot *p;
55526 if( pBuf==0 ) sz = n = 0;
55527 if( n==0 ) sz = 0;
55528 sz = ROUNDDOWN8(sz)((sz)&~7);
55529 pcache1(pcache1_g).szSlot = sz;
55530 pcache1(pcache1_g).nSlot = pcache1(pcache1_g).nFreeSlot = n;
55531 pcache1(pcache1_g).nReserve = n>90 ? 10 : (n/10 + 1);
55532 pcache1(pcache1_g).pStart = pBuf;
55533 pcache1(pcache1_g).pFree = 0;
55534 pcache1(pcache1_g).bUnderPressure = 0;
55535 while( n-- ){
55536 p = (PgFreeslot*)pBuf;
55537 p->pNext = pcache1(pcache1_g).pFree;
55538 pcache1(pcache1_g).pFree = p;
55539 pBuf = (void*)&((char*)pBuf)[sz];
55540 }
55541 pcache1(pcache1_g).pEnd = pBuf;
55542 }
55543}
55544
55545/*
55546** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
55547** true if pCache->pFree ends up containing one or more free pages.
55548*/
55549static int pcache1InitBulk(PCache1 *pCache){
55550 i64 szBulk;
55551 char *zBulk;
55552 if( pcache1(pcache1_g).nInitPage==0 ) return 0;
55553 /* Do not bother with a bulk allocation if the cache size very small */
55554 if( pCache->nMax<3 ) return 0;
55555 sqlite3BeginBenignMalloc();
55556 if( pcache1(pcache1_g).nInitPage>0 ){
55557 szBulk = pCache->szAlloc * (i64)pcache1(pcache1_g).nInitPage;
55558 }else{
55559 szBulk = -1024 * (i64)pcache1(pcache1_g).nInitPage;
55560 }
55561 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
55562 szBulk = pCache->szAlloc*(i64)pCache->nMax;
55563 }
55564 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
55565 sqlite3EndBenignMalloc();
55566 if( zBulk ){
55567 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
55568 do{
55569 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
55570 pX->page.pBuf = zBulk;
55571 pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX))(((sizeof(*pX))+7)&~7);
55572 assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) )((void) (0));
55573 pX->isBulkLocal = 1;
55574 pX->isAnchor = 0;
55575 pX->pNext = pCache->pFree;
55576 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
55577 pCache->pFree = pX;
55578 zBulk += pCache->szAlloc;
55579 }while( --nBulk );
55580 }
55581 return pCache->pFree!=0;
55582}
55583
55584/*
55585** Malloc function used within this file to allocate space from the buffer
55586** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
55587** such buffer exists or there is no space left in it, this function falls
55588** back to sqlite3Malloc().
55589**
55590** Multiple threads can run this routine at the same time. Global variables
55591** in pcache1 need to be protected via mutex.
55592*/
55593static void *pcache1Alloc(int nByte){
55594 void *p = 0;
55595 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
55596 if( nByte<=pcache1(pcache1_g).szSlot ){
55597 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
55598 p = (PgHdr1 *)pcache1(pcache1_g).pFree;
55599 if( p ){
55600 pcache1(pcache1_g).pFree = pcache1(pcache1_g).pFree->pNext;
55601 pcache1(pcache1_g).nFreeSlot--;
55602 pcache1(pcache1_g).bUnderPressure = pcache1(pcache1_g).nFreeSlot<pcache1(pcache1_g).nReserve;
55603 assert( pcache1.nFreeSlot>=0 )((void) (0));
55604 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
55605 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED1, 1);
55606 }
55607 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
55608 }
55609 if( p==0 ){
55610 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
55611 ** it from sqlite3Malloc instead.
55612 */
55613 p = sqlite3Malloc(nByte);
55614#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
55615 if( p ){
55616 int sz = sqlite3MallocSize(p);
55617 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
55618 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
55619 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW2, sz);
55620 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
55621 }
55622#endif
55623 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
55624 }
55625 return p;
55626}
55627
55628/*
55629** Free an allocated buffer obtained from pcache1Alloc().
55630*/
55631static void pcache1Free(void *p){
55632 if( p==0 ) return;
55633 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd)(((uptr)(p)>=(uptr)((pcache1_g).pStart))&&((uptr)(
p)<(uptr)((pcache1_g).pEnd)))
){
55634 PgFreeslot *pSlot;
55635 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
55636 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED1, 1);
55637 pSlot = (PgFreeslot*)p;
55638 pSlot->pNext = pcache1(pcache1_g).pFree;
55639 pcache1(pcache1_g).pFree = pSlot;
55640 pcache1(pcache1_g).nFreeSlot++;
55641 pcache1(pcache1_g).bUnderPressure = pcache1(pcache1_g).nFreeSlot<pcache1(pcache1_g).nReserve;
55642 assert( pcache1.nFreeSlot<=pcache1.nSlot )((void) (0));
55643 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
55644 }else{
55645 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
55646 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
55647#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
55648 {
55649 int nFreed = 0;
55650 nFreed = sqlite3MallocSize(p);
55651 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
55652 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW2, nFreed);
55653 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
55654 }
55655#endif
55656 sqlite3_free(p);
55657 }
55658}
55659
55660#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
55661/*
55662** Return the size of a pcache allocation
55663*/
55664static int pcache1MemSize(void *p){
55665 if( p>=pcache1(pcache1_g).pStart && p<pcache1(pcache1_g).pEnd ){
55666 return pcache1(pcache1_g).szSlot;
55667 }else{
55668 int iSize;
55669 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
55670 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
55671 iSize = sqlite3MallocSize(p);
55672 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
55673 return iSize;
55674 }
55675}
55676#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
55677
55678/*
55679** Allocate a new page object initially associated with cache pCache.
55680*/
55681static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
55682 PgHdr1 *p = 0;
55683 void *pPg;
55684
55685 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
55686 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
55687 assert( pCache->pFree!=0 )((void) (0));
55688 p = pCache->pFree;
55689 pCache->pFree = p->pNext;
55690 p->pNext = 0;
55691 }else{
55692#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
55693 /* The group mutex must be released before pcache1Alloc() is called. This
55694 ** is because it might call sqlite3_release_memory(), which assumes that
55695 ** this mutex is not held. */
55696 assert( pcache1.separateCache==0 )((void) (0));
55697 assert( pCache->pGroup==&pcache1.grp )((void) (0));
55698 pcache1LeaveMutex(pCache->pGroup)((void) (0));
55699#endif
55700 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
55701 pPg = pcache1Alloc(pCache->szAlloc);
55702 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
55703#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
55704 pcache1EnterMutex(pCache->pGroup)((void) (0));
55705#endif
55706 if( pPg==0 ) return 0;
55707 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
55708 p->page.pBuf = pPg;
55709 p->page.pExtra = (u8*)p + ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7);
55710 assert( EIGHT_BYTE_ALIGNMENT( p->page.pExtra ) )((void) (0));
55711 p->isBulkLocal = 0;
55712 p->isAnchor = 0;
55713 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
55714 }
55715 (*pCache->pnPurgeable)++;
55716 return p;
55717}
55718
55719/*
55720** Free a page object allocated by pcache1AllocPage().
55721*/
55722static void pcache1FreePage(PgHdr1 *p){
55723 PCache1 *pCache;
55724 assert( p!=0 )((void) (0));
55725 pCache = p->pCache;
55726 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) )((void) (0));
55727 if( p->isBulkLocal ){
55728 p->pNext = pCache->pFree;
55729 pCache->pFree = p;
55730 }else{
55731 pcache1Free(p->page.pBuf);
55732 }
55733 (*pCache->pnPurgeable)--;
55734}
55735
55736/*
55737** Malloc function used by SQLite to obtain space from the buffer configured
55738** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
55739** exists, this function falls back to sqlite3Malloc().
55740*/
55741SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int sz){
55742 assert( sz<=65536+8 )((void) (0)); /* These allocations are never very large */
55743 return pcache1Alloc(sz);
55744}
55745
55746/*
55747** Free an allocated buffer obtained from sqlite3PageMalloc().
55748*/
55749SQLITE_PRIVATEstatic void sqlite3PageFree(void *p){
55750 pcache1Free(p);
55751}
55752
55753
55754/*
55755** Return true if it desirable to avoid allocating a new page cache
55756** entry.
55757**
55758** If memory was allocated specifically to the page cache using
55759** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
55760** it is desirable to avoid allocating a new page cache entry because
55761** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
55762** for all page cache needs and we should not need to spill the
55763** allocation onto the heap.
55764**
55765** Or, the heap is used for all page cache memory but the heap is
55766** under memory pressure, then again it is desirable to avoid
55767** allocating a new page cache entry in order to avoid stressing
55768** the heap even further.
55769*/
55770static int pcache1UnderMemoryPressure(PCache1 *pCache){
55771 if( pcache1(pcache1_g).nSlot && (pCache->szPage+pCache->szExtra)<=pcache1(pcache1_g).szSlot ){
55772 return pcache1(pcache1_g).bUnderPressure;
55773 }else{
55774 return sqlite3HeapNearlyFull();
55775 }
55776}
55777
55778/******************************************************************************/
55779/******** General Implementation Functions ************************************/
55780
55781/*
55782** This function is used to resize the hash table used by the cache passed
55783** as the first argument.
55784**
55785** The PCache mutex must be held when this function is called.
55786*/
55787static void pcache1ResizeHash(PCache1 *p){
55788 PgHdr1 **apNew;
55789 unsigned int nNew;
55790 unsigned int i;
55791
55792 assert( sqlite3_mutex_held(p->pGroup->mutex) )((void) (0));
55793
55794 nNew = p->nHash*2;
55795 if( nNew<256 ){
55796 nNew = 256;
55797 }
55798
55799 pcache1LeaveMutex(p->pGroup)((void) (0));
55800 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
55801 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
55802 if( p->nHash ){ sqlite3EndBenignMalloc(); }
55803 pcache1EnterMutex(p->pGroup)((void) (0));
55804 if( apNew ){
55805 for(i=0; i<p->nHash; i++){
55806 PgHdr1 *pPage;
55807 PgHdr1 *pNext = p->apHash[i];
55808 while( (pPage = pNext)!=0 ){
55809 unsigned int h = pPage->iKey % nNew;
55810 pNext = pPage->pNext;
55811 pPage->pNext = apNew[h];
55812 apNew[h] = pPage;
55813 }
55814 }
55815 sqlite3_free(p->apHash);
55816 p->apHash = apNew;
55817 p->nHash = nNew;
55818 }
55819}
55820
55821/*
55822** This function is used internally to remove the page pPage from the
55823** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
55824** LRU list, then this function is a no-op.
55825**
55826** The PGroup mutex must be held when this function is called.
55827*/
55828static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
55829 assert( pPage!=0 )((void) (0));
55830 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
55831 assert( pPage->pLruNext )((void) (0));
55832 assert( pPage->pLruPrev )((void) (0));
55833 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) )((void) (0));
55834 pPage->pLruPrev->pLruNext = pPage->pLruNext;
55835 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
55836 pPage->pLruNext = 0;
55837 /* pPage->pLruPrev = 0;
55838 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
55839 assert( pPage->isAnchor==0 )((void) (0));
55840 assert( pPage->pCache->pGroup->lru.isAnchor==1 )((void) (0));
55841 pPage->pCache->nRecyclable--;
55842 return pPage;
55843}
55844
55845
55846/*
55847** Remove the page supplied as an argument from the hash table
55848** (PCache1.apHash structure) that it is currently stored in.
55849** Also free the page if freePage is true.
55850**
55851** The PGroup mutex must be held when this function is called.
55852*/
55853static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
55854 unsigned int h;
55855 PCache1 *pCache = pPage->pCache;
55856 PgHdr1 **pp;
55857
55858 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
55859 h = pPage->iKey % pCache->nHash;
55860 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
55861 *pp = (*pp)->pNext;
55862
55863 pCache->nPage--;
55864 if( freeFlag ) pcache1FreePage(pPage);
55865}
55866
55867/*
55868** If there are currently more than nMaxPage pages allocated, try
55869** to recycle pages to reduce the number allocated to nMaxPage.
55870*/
55871static void pcache1EnforceMaxPage(PCache1 *pCache){
55872 PGroup *pGroup = pCache->pGroup;
55873 PgHdr1 *p;
55874 assert( sqlite3_mutex_held(pGroup->mutex) )((void) (0));
55875 while( pGroup->nPurgeable>pGroup->nMaxPage
55876 && (p=pGroup->lru.pLruPrev)->isAnchor==0
55877 ){
55878 assert( p->pCache->pGroup==pGroup )((void) (0));
55879 assert( PAGE_IS_UNPINNED(p) )((void) (0));
55880 pcache1PinPage(p);
55881 pcache1RemoveFromHash(p, 1);
55882 }
55883 if( pCache->nPage==0 && pCache->pBulk ){
55884 sqlite3_free(pCache->pBulk);
55885 pCache->pBulk = pCache->pFree = 0;
55886 }
55887}
55888
55889/*
55890** Discard all pages from cache pCache with a page number (key value)
55891** greater than or equal to iLimit. Any pinned pages that meet this
55892** criteria are unpinned before they are discarded.
55893**
55894** The PCache mutex must be held when this function is called.
55895*/
55896static void pcache1TruncateUnsafe(
55897 PCache1 *pCache, /* The cache to truncate */
55898 unsigned int iLimit /* Drop pages with this pgno or larger */
55899){
55900 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
55901 unsigned int h, iStop;
55902 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
55903 assert( pCache->iMaxKey >= iLimit )((void) (0));
55904 assert( pCache->nHash > 0 )((void) (0));
55905 if( pCache->iMaxKey - iLimit < pCache->nHash ){
55906 /* If we are just shaving the last few pages off the end of the
55907 ** cache, then there is no point in scanning the entire hash table.
55908 ** Only scan those hash slots that might contain pages that need to
55909 ** be removed. */
55910 h = iLimit % pCache->nHash;
55911 iStop = pCache->iMaxKey % pCache->nHash;
55912 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
55913 }else{
55914 /* This is the general case where many pages are being removed.
55915 ** It is necessary to scan the entire hash table */
55916 h = pCache->nHash/2;
55917 iStop = h - 1;
55918 }
55919 for(;;){
55920 PgHdr1 **pp;
55921 PgHdr1 *pPage;
55922 assert( h<pCache->nHash )((void) (0));
55923 pp = &pCache->apHash[h];
55924 while( (pPage = *pp)!=0 ){
55925 if( pPage->iKey>=iLimit ){
55926 pCache->nPage--;
55927 *pp = pPage->pNext;
55928 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ) pcache1PinPage(pPage);
55929 pcache1FreePage(pPage);
55930 }else{
55931 pp = &pPage->pNext;
55932 TESTONLY( if( nPage>=0 ) nPage++; )
55933 }
55934 }
55935 if( h==iStop ) break;
55936 h = (h+1) % pCache->nHash;
55937 }
55938 assert( nPage<0 || pCache->nPage==(unsigned)nPage )((void) (0));
55939}
55940
55941/******************************************************************************/
55942/******** sqlite3_pcache Methods **********************************************/
55943
55944/*
55945** Implementation of the sqlite3_pcache.xInit method.
55946*/
55947static int pcache1Init(void *NotUsed){
55948 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
55949 assert( pcache1.isInit==0 )((void) (0));
55950 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
55951
55952
55953 /*
55954 ** The pcache1.separateCache variable is true if each PCache has its own
55955 ** private PGroup (mode-1). pcache1.separateCache is false if the single
55956 ** PGroup in pcache1.grp is used for all page caches (mode-2).
55957 **
55958 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
55959 **
55960 ** * Use a unified cache in single-threaded applications that have
55961 ** configured a start-time buffer for use as page-cache memory using
55962 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
55963 ** pBuf argument.
55964 **
55965 ** * Otherwise use separate caches (mode-1)
55966 */
55967#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
55968 pcache1(pcache1_g).separateCache = 0;
55969#elif SQLITE_THREADSAFE2
55970 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0
55971 || sqlite3GlobalConfigsqlite3Config.bCoreMutex>0;
55972#else
55973 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0;
55974#endif
55975
55976#if SQLITE_THREADSAFE2
55977 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
55978 pcache1(pcache1_g).grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU6);
55979 pcache1(pcache1_g).mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM7);
55980 }
55981#endif
55982 if( pcache1(pcache1_g).separateCache
55983 && sqlite3GlobalConfigsqlite3Config.nPage!=0
55984 && sqlite3GlobalConfigsqlite3Config.pPage==0
55985 ){
55986 pcache1(pcache1_g).nInitPage = sqlite3GlobalConfigsqlite3Config.nPage;
55987 }else{
55988 pcache1(pcache1_g).nInitPage = 0;
55989 }
55990 pcache1(pcache1_g).grp.mxPinned = 10;
55991 pcache1(pcache1_g).isInit = 1;
55992 return SQLITE_OK0;
55993}
55994
55995/*
55996** Implementation of the sqlite3_pcache.xShutdown method.
55997** Note that the static mutex allocated in xInit does
55998** not need to be freed.
55999*/
56000static void pcache1Shutdown(void *NotUsed){
56001 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
56002 assert( pcache1.isInit!=0 )((void) (0));
56003 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
56004}
56005
56006/* forward declaration */
56007static void pcache1Destroy(sqlite3_pcache *p);
56008
56009/*
56010** Implementation of the sqlite3_pcache.xCreate method.
56011**
56012** Allocate a new cache.
56013*/
56014static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
56015 PCache1 *pCache; /* The newly created page cache */
56016 PGroup *pGroup; /* The group the new page cache will belong to */
56017 int sz; /* Bytes of memory required to allocate the new cache */
56018
56019 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 )((void) (0));
56020 assert( szExtra < 300 )((void) (0));
56021
56022 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1(pcache1_g).separateCache;
56023 pCache = (PCache1 *)sqlite3MallocZero(sz);
56024 if( pCache ){
56025 if( pcache1(pcache1_g).separateCache ){
56026 pGroup = (PGroup*)&pCache[1];
56027 pGroup->mxPinned = 10;
56028 }else{
56029 pGroup = &pcache1(pcache1_g).grp;
56030 }
56031 pcache1EnterMutex(pGroup)((void) (0));
56032 if( pGroup->lru.isAnchor==0 ){
56033 pGroup->lru.isAnchor = 1;
56034 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
56035 }
56036 pCache->pGroup = pGroup;
56037 pCache->szPage = szPage;
56038 pCache->szExtra = szExtra;
56039 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7);
56040 pCache->bPurgeable = (bPurgeable ? 1 : 0);
56041 pcache1ResizeHash(pCache);
56042 if( bPurgeable ){
56043 pCache->nMin = 10;
56044 pGroup->nMinPage += pCache->nMin;
56045 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56046 pCache->pnPurgeable = &pGroup->nPurgeable;
56047 }else{
56048 pCache->pnPurgeable = &pCache->nPurgeableDummy;
56049 }
56050 pcache1LeaveMutex(pGroup)((void) (0));
56051 if( pCache->nHash==0 ){
56052 pcache1Destroy((sqlite3_pcache*)pCache);
56053 pCache = 0;
56054 }
56055 }
56056 return (sqlite3_pcache *)pCache;
56057}
56058
56059/*
56060** Implementation of the sqlite3_pcache.xCachesize method.
56061**
56062** Configure the cache_size limit for a cache.
56063*/
56064static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
56065 PCache1 *pCache = (PCache1 *)p;
56066 u32 n;
56067 assert( nMax>=0 )((void) (0));
56068 if( pCache->bPurgeable ){
56069 PGroup *pGroup = pCache->pGroup;
56070 pcache1EnterMutex(pGroup)((void) (0));
56071 n = (u32)nMax;
56072 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
56073 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
56074 }
56075 pGroup->nMaxPage += (n - pCache->nMax);
56076 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56077 pCache->nMax = n;
56078 pCache->n90pct = pCache->nMax*9/10;
56079 pcache1EnforceMaxPage(pCache);
56080 pcache1LeaveMutex(pGroup)((void) (0));
56081 }
56082}
56083
56084/*
56085** Implementation of the sqlite3_pcache.xShrink method.
56086**
56087** Free up as much memory as possible.
56088*/
56089static void pcache1Shrink(sqlite3_pcache *p){
56090 PCache1 *pCache = (PCache1*)p;
56091 if( pCache->bPurgeable ){
56092 PGroup *pGroup = pCache->pGroup;
56093 unsigned int savedMaxPage;
56094 pcache1EnterMutex(pGroup)((void) (0));
56095 savedMaxPage = pGroup->nMaxPage;
56096 pGroup->nMaxPage = 0;
56097 pcache1EnforceMaxPage(pCache);
56098 pGroup->nMaxPage = savedMaxPage;
56099 pcache1LeaveMutex(pGroup)((void) (0));
56100 }
56101}
56102
56103/*
56104** Implementation of the sqlite3_pcache.xPagecount method.
56105*/
56106static int pcache1Pagecount(sqlite3_pcache *p){
56107 int n;
56108 PCache1 *pCache = (PCache1*)p;
56109 pcache1EnterMutex(pCache->pGroup)((void) (0));
56110 n = pCache->nPage;
56111 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56112 return n;
56113}
56114
56115
56116/*
56117** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
56118** in the header of the pcache1Fetch() procedure.
56119**
56120** This steps are broken out into a separate procedure because they are
56121** usually not needed, and by avoiding the stack initialization required
56122** for these steps, the main pcache1Fetch() procedure can run faster.
56123*/
56124static SQLITE_NOINLINE__attribute__((noinline)) PgHdr1 *pcache1FetchStage2(
56125 PCache1 *pCache,
56126 unsigned int iKey,
56127 int createFlag
56128){
56129 unsigned int nPinned;
56130 PGroup *pGroup = pCache->pGroup;
56131 PgHdr1 *pPage = 0;
56132
56133 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
56134 assert( pCache->nPage >= pCache->nRecyclable )((void) (0));
56135 nPinned = pCache->nPage - pCache->nRecyclable;
56136 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage )((void) (0));
56137 assert( pCache->n90pct == pCache->nMax*9/10 )((void) (0));
56138 if( createFlag==1 && (
56139 nPinned>=pGroup->mxPinned
56140 || nPinned>=pCache->n90pct
56141 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
56142 )){
56143 return 0;
56144 }
56145
56146 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
56147 assert( pCache->nHash>0 && pCache->apHash )((void) (0));
56148
56149 /* Step 4. Try to recycle a page. */
56150 if( pCache->bPurgeable
56151 && !pGroup->lru.pLruPrev->isAnchor
56152 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
56153 ){
56154 PCache1 *pOther;
56155 pPage = pGroup->lru.pLruPrev;
56156 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
56157 pcache1RemoveFromHash(pPage, 0);
56158 pcache1PinPage(pPage);
56159 pOther = pPage->pCache;
56160 if( pOther->szAlloc != pCache->szAlloc ){
56161 pcache1FreePage(pPage);
56162 pPage = 0;
56163 }else{
56164 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
56165 }
56166 }
56167
56168 /* Step 5. If a usable page buffer has still not been found,
56169 ** attempt to allocate a new one.
56170 */
56171 if( !pPage ){
56172 pPage = pcache1AllocPage(pCache, createFlag==1);
56173 }
56174
56175 if( pPage ){
56176 unsigned int h = iKey % pCache->nHash;
56177 pCache->nPage++;
56178 pPage->iKey = iKey;
56179 pPage->pNext = pCache->apHash[h];
56180 pPage->pCache = pCache;
56181 pPage->pLruNext = 0;
56182 /* pPage->pLruPrev = 0;
56183 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
56184 *(void **)pPage->page.pExtra = 0;
56185 pCache->apHash[h] = pPage;
56186 if( iKey>pCache->iMaxKey ){
56187 pCache->iMaxKey = iKey;
56188 }
56189 }
56190 return pPage;
56191}
56192
56193/*
56194** Implementation of the sqlite3_pcache.xFetch method.
56195**
56196** Fetch a page by key value.
56197**
56198** Whether or not a new page may be allocated by this function depends on
56199** the value of the createFlag argument. 0 means do not allocate a new
56200** page. 1 means allocate a new page if space is easily available. 2
56201** means to try really hard to allocate a new page.
56202**
56203** For a non-purgeable cache (a cache used as the storage for an in-memory
56204** database) there is really no difference between createFlag 1 and 2. So
56205** the calling function (pcache.c) will never have a createFlag of 1 on
56206** a non-purgeable cache.
56207**
56208** There are three different approaches to obtaining space for a page,
56209** depending on the value of parameter createFlag (which may be 0, 1 or 2).
56210**
56211** 1. Regardless of the value of createFlag, the cache is searched for a
56212** copy of the requested page. If one is found, it is returned.
56213**
56214** 2. If createFlag==0 and the page is not already in the cache, NULL is
56215** returned.
56216**
56217** 3. If createFlag is 1, and the page is not already in the cache, then
56218** return NULL (do not allocate a new page) if any of the following
56219** conditions are true:
56220**
56221** (a) the number of pages pinned by the cache is greater than
56222** PCache1.nMax, or
56223**
56224** (b) the number of pages pinned by the cache is greater than
56225** the sum of nMax for all purgeable caches, less the sum of
56226** nMin for all other purgeable caches, or
56227**
56228** 4. If none of the first three conditions apply and the cache is marked
56229** as purgeable, and if one of the following is true:
56230**
56231** (a) The number of pages allocated for the cache is already
56232** PCache1.nMax, or
56233**
56234** (b) The number of pages allocated for all purgeable caches is
56235** already equal to or greater than the sum of nMax for all
56236** purgeable caches,
56237**
56238** (c) The system is under memory pressure and wants to avoid
56239** unnecessary pages cache entry allocations
56240**
56241** then attempt to recycle a page from the LRU list. If it is the right
56242** size, return the recycled buffer. Otherwise, free the buffer and
56243** proceed to step 5.
56244**
56245** 5. Otherwise, allocate and return a new page buffer.
56246**
56247** There are two versions of this routine. pcache1FetchWithMutex() is
56248** the general case. pcache1FetchNoMutex() is a faster implementation for
56249** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
56250** invokes the appropriate routine.
56251*/
56252static PgHdr1 *pcache1FetchNoMutex(
56253 sqlite3_pcache *p,
56254 unsigned int iKey,
56255 int createFlag
56256){
56257 PCache1 *pCache = (PCache1 *)p;
56258 PgHdr1 *pPage = 0;
56259
56260 /* Step 1: Search the hash table for an existing entry. */
56261 pPage = pCache->apHash[iKey % pCache->nHash];
56262 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
56263
56264 /* Step 2: If the page was found in the hash table, then return it.
56265 ** If the page was not in the hash table and createFlag is 0, abort.
56266 ** Otherwise (page not in hash and createFlag!=0) continue with
56267 ** subsequent steps to try to create the page. */
56268 if( pPage ){
56269 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ){
56270 return pcache1PinPage(pPage);
56271 }else{
56272 return pPage;
56273 }
56274 }else if( createFlag ){
56275 /* Steps 3, 4, and 5 implemented by this subroutine */
56276 return pcache1FetchStage2(pCache, iKey, createFlag);
56277 }else{
56278 return 0;
56279 }
56280}
56281#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
56282static PgHdr1 *pcache1FetchWithMutex(
56283 sqlite3_pcache *p,
56284 unsigned int iKey,
56285 int createFlag
56286){
56287 PCache1 *pCache = (PCache1 *)p;
56288 PgHdr1 *pPage;
56289
56290 pcache1EnterMutex(pCache->pGroup)((void) (0));
56291 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
56292 assert( pPage==0 || pCache->iMaxKey>=iKey )((void) (0));
56293 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56294 return pPage;
56295}
56296#endif
56297static sqlite3_pcache_page *pcache1Fetch(
56298 sqlite3_pcache *p,
56299 unsigned int iKey,
56300 int createFlag
56301){
56302#if PCACHE1_MIGHT_USE_GROUP_MUTEX0 || defined(SQLITE_DEBUG)
56303 PCache1 *pCache = (PCache1 *)p;
56304#endif
56305
56306 assert( offsetof(PgHdr1,page)==0 )((void) (0));
56307 assert( pCache->bPurgeable || createFlag!=1 )((void) (0));
56308 assert( pCache->bPurgeable || pCache->nMin==0 )((void) (0));
56309 assert( pCache->bPurgeable==0 || pCache->nMin==10 )((void) (0));
56310 assert( pCache->nMin==0 || pCache->bPurgeable )((void) (0));
56311 assert( pCache->nHash>0 )((void) (0));
56312#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
56313 if( pCache->pGroup->mutex ){
56314 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
56315 }else
56316#endif
56317 {
56318 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
56319 }
56320}
56321
56322
56323/*
56324** Implementation of the sqlite3_pcache.xUnpin method.
56325**
56326** Mark a page as unpinned (eligible for asynchronous recycling).
56327*/
56328static void pcache1Unpin(
56329 sqlite3_pcache *p,
56330 sqlite3_pcache_page *pPg,
56331 int reuseUnlikely
56332){
56333 PCache1 *pCache = (PCache1 *)p;
56334 PgHdr1 *pPage = (PgHdr1 *)pPg;
56335 PGroup *pGroup = pCache->pGroup;
56336
56337 assert( pPage->pCache==pCache )((void) (0));
56338 pcache1EnterMutex(pGroup)((void) (0));
56339
56340 /* It is an error to call this function if the page is already
56341 ** part of the PGroup LRU list.
56342 */
56343 assert( pPage->pLruNext==0 )((void) (0));
56344 assert( PAGE_IS_PINNED(pPage) )((void) (0));
56345
56346 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
56347 pcache1RemoveFromHash(pPage, 1);
56348 }else{
56349 /* Add the page to the PGroup LRU list. */
56350 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
56351 pPage->pLruPrev = &pGroup->lru;
56352 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
56353 *ppFirst = pPage;
56354 pCache->nRecyclable++;
56355 }
56356
56357 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56358}
56359
56360/*
56361** Implementation of the sqlite3_pcache.xRekey method.
56362*/
56363static void pcache1Rekey(
56364 sqlite3_pcache *p,
56365 sqlite3_pcache_page *pPg,
56366 unsigned int iOld,
56367 unsigned int iNew
56368){
56369 PCache1 *pCache = (PCache1 *)p;
56370 PgHdr1 *pPage = (PgHdr1 *)pPg;
56371 PgHdr1 **pp;
56372 unsigned int hOld, hNew;
56373 assert( pPage->iKey==iOld )((void) (0));
56374 assert( pPage->pCache==pCache )((void) (0));
56375 assert( iOld!=iNew )((void) (0)); /* The page number really is changing */
56376
56377 pcache1EnterMutex(pCache->pGroup)((void) (0));
56378
56379 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage )((void) (0)); /* pPg really is iOld */
56380 hOld = iOld%pCache->nHash;
56381 pp = &pCache->apHash[hOld];
56382 while( (*pp)!=pPage ){
56383 pp = &(*pp)->pNext;
56384 }
56385 *pp = pPage->pNext;
56386
56387 assert( pcache1FetchNoMutex(p, iNew, 0)==0 )((void) (0)); /* iNew not in cache */
56388 hNew = iNew%pCache->nHash;
56389 pPage->iKey = iNew;
56390 pPage->pNext = pCache->apHash[hNew];
56391 pCache->apHash[hNew] = pPage;
56392 if( iNew>pCache->iMaxKey ){
56393 pCache->iMaxKey = iNew;
56394 }
56395
56396 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56397}
56398
56399/*
56400** Implementation of the sqlite3_pcache.xTruncate method.
56401**
56402** Discard all unpinned pages in the cache with a page number equal to
56403** or greater than parameter iLimit. Any pinned pages with a page number
56404** equal to or greater than iLimit are implicitly unpinned.
56405*/
56406static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
56407 PCache1 *pCache = (PCache1 *)p;
56408 pcache1EnterMutex(pCache->pGroup)((void) (0));
56409 if( iLimit<=pCache->iMaxKey ){
56410 pcache1TruncateUnsafe(pCache, iLimit);
56411 pCache->iMaxKey = iLimit-1;
56412 }
56413 pcache1LeaveMutex(pCache->pGroup)((void) (0));
56414}
56415
56416/*
56417** Implementation of the sqlite3_pcache.xDestroy method.
56418**
56419** Destroy a cache allocated using pcache1Create().
56420*/
56421static void pcache1Destroy(sqlite3_pcache *p){
56422 PCache1 *pCache = (PCache1 *)p;
56423 PGroup *pGroup = pCache->pGroup;
56424 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) )((void) (0));
56425 pcache1EnterMutex(pGroup)((void) (0));
56426 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
56427 assert( pGroup->nMaxPage >= pCache->nMax )((void) (0));
56428 pGroup->nMaxPage -= pCache->nMax;
56429 assert( pGroup->nMinPage >= pCache->nMin )((void) (0));
56430 pGroup->nMinPage -= pCache->nMin;
56431 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56432 pcache1EnforceMaxPage(pCache);
56433 pcache1LeaveMutex(pGroup)((void) (0));
56434 sqlite3_free(pCache->pBulk);
56435 sqlite3_free(pCache->apHash);
56436 sqlite3_free(pCache);
56437}
56438
56439/*
56440** This function is called during initialization (sqlite3_initialize()) to
56441** install the default pluggable cache module, assuming the user has not
56442** already provided an alternative.
56443*/
56444SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void){
56445 static const sqlite3_pcache_methods2 defaultMethods = {
56446 1, /* iVersion */
56447 0, /* pArg */
56448 pcache1Init, /* xInit */
56449 pcache1Shutdown, /* xShutdown */
56450 pcache1Create, /* xCreate */
56451 pcache1Cachesize, /* xCachesize */
56452 pcache1Pagecount, /* xPagecount */
56453 pcache1Fetch, /* xFetch */
56454 pcache1Unpin, /* xUnpin */
56455 pcache1Rekey, /* xRekey */
56456 pcache1Truncate, /* xTruncate */
56457 pcache1Destroy, /* xDestroy */
56458 pcache1Shrink /* xShrink */
56459 };
56460 sqlite3_config(SQLITE_CONFIG_PCACHE218, &defaultMethods);
56461}
56462
56463/*
56464** Return the size of the header on each page of this PCACHE implementation.
56465*/
56466SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7); }
56467
56468/*
56469** Return the global mutex used by this PCACHE implementation. The
56470** sqlite3_status() routine needs access to this mutex.
56471*/
56472SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void){
56473 return pcache1(pcache1_g).mutex;
56474}
56475
56476#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
56477/*
56478** This function is called to free superfluous dynamically allocated memory
56479** held by the pager system. Memory in use by any SQLite pager allocated
56480** by the current thread may be sqlite3_free()ed.
56481**
56482** nReq is the number of bytes of memory required. Once this much has
56483** been released, the function returns. The return value is the total number
56484** of bytes of memory released.
56485*/
56486SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int nReq){
56487 int nFree = 0;
56488 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
56489 assert( sqlite3_mutex_notheld(pcache1.mutex) )((void) (0));
56490 if( sqlite3GlobalConfigsqlite3Config.pPage==0 ){
56491 PgHdr1 *p;
56492 pcache1EnterMutex(&pcache1.grp)((void) (0));
56493 while( (nReq<0 || nFree<nReq)
56494 && (p=pcache1(pcache1_g).grp.lru.pLruPrev)!=0
56495 && p->isAnchor==0
56496 ){
56497 nFree += pcache1MemSize(p->page.pBuf);
56498 assert( PAGE_IS_UNPINNED(p) )((void) (0));
56499 pcache1PinPage(p);
56500 pcache1RemoveFromHash(p, 1);
56501 }
56502 pcache1LeaveMutex(&pcache1.grp)((void) (0));
56503 }
56504 return nFree;
56505}
56506#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
56507
56508#ifdef SQLITE_TEST
56509/*
56510** This function is used by test procedures to inspect the internal state
56511** of the global cache.
56512*/
56513SQLITE_PRIVATEstatic void sqlite3PcacheStats(
56514 int *pnCurrent, /* OUT: Total number of pages cached */
56515 int *pnMax, /* OUT: Global maximum cache size */
56516 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
56517 int *pnRecyclable /* OUT: Total number of pages available for recycling */
56518){
56519 PgHdr1 *p;
56520 int nRecyclable = 0;
56521 for(p=pcache1(pcache1_g).grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
56522 assert( PAGE_IS_UNPINNED(p) )((void) (0));
56523 nRecyclable++;
56524 }
56525 *pnCurrent = pcache1(pcache1_g).grp.nPurgeable;
56526 *pnMax = (int)pcache1(pcache1_g).grp.nMaxPage;
56527 *pnMin = (int)pcache1(pcache1_g).grp.nMinPage;
56528 *pnRecyclable = nRecyclable;
56529}
56530#endif
56531
56532/************** End of pcache1.c *********************************************/
56533/************** Begin file rowset.c ******************************************/
56534/*
56535** 2008 December 3
56536**
56537** The author disclaims copyright to this source code. In place of
56538** a legal notice, here is a blessing:
56539**
56540** May you do good and not evil.
56541** May you find forgiveness for yourself and forgive others.
56542** May you share freely, never taking more than you give.
56543**
56544*************************************************************************
56545**
56546** This module implements an object we call a "RowSet".
56547**
56548** The RowSet object is a collection of rowids. Rowids
56549** are inserted into the RowSet in an arbitrary order. Inserts
56550** can be intermixed with tests to see if a given rowid has been
56551** previously inserted into the RowSet.
56552**
56553** After all inserts are finished, it is possible to extract the
56554** elements of the RowSet in sorted order. Once this extraction
56555** process has started, no new elements may be inserted.
56556**
56557** Hence, the primitive operations for a RowSet are:
56558**
56559** CREATE
56560** INSERT
56561** TEST
56562** SMALLEST
56563** DESTROY
56564**
56565** The CREATE and DESTROY primitives are the constructor and destructor,
56566** obviously. The INSERT primitive adds a new element to the RowSet.
56567** TEST checks to see if an element is already in the RowSet. SMALLEST
56568** extracts the least value from the RowSet.
56569**
56570** The INSERT primitive might allocate additional memory. Memory is
56571** allocated in chunks so most INSERTs do no allocation. There is an
56572** upper bound on the size of allocated memory. No memory is freed
56573** until DESTROY.
56574**
56575** The TEST primitive includes a "batch" number. The TEST primitive
56576** will only see elements that were inserted before the last change
56577** in the batch number. In other words, if an INSERT occurs between
56578** two TESTs where the TESTs have the same batch number, then the
56579** value added by the INSERT will not be visible to the second TEST.
56580** The initial batch number is zero, so if the very first TEST contains
56581** a non-zero batch number, it will see all prior INSERTs.
56582**
56583** No INSERTs may occurs after a SMALLEST. An assertion will fail if
56584** that is attempted.
56585**
56586** The cost of an INSERT is roughly constant. (Sometimes new memory
56587** has to be allocated on an INSERT.) The cost of a TEST with a new
56588** batch number is O(NlogN) where N is the number of elements in the RowSet.
56589** The cost of a TEST using the same batch number is O(logN). The cost
56590** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
56591** primitives are constant time. The cost of DESTROY is O(N).
56592**
56593** TEST and SMALLEST may not be used by the same RowSet. This used to
56594** be possible, but the feature was not used, so it was removed in order
56595** to simplify the code.
56596*/
56597/* #include "sqliteInt.h" */
56598
56599
56600/*
56601** Target size for allocation chunks.
56602*/
56603#define ROWSET_ALLOCATION_SIZE1024 1024
56604
56605/*
56606** The number of rowset entries per allocation chunk.
56607*/
56608#define ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry)) \
56609 ((ROWSET_ALLOCATION_SIZE1024-8)/sizeof(struct RowSetEntry))
56610
56611/*
56612** Each entry in a RowSet is an instance of the following object.
56613**
56614** This same object is reused to store a linked list of trees of RowSetEntry
56615** objects. In that alternative use, pRight points to the next entry
56616** in the list, pLeft points to the tree, and v is unused. The
56617** RowSet.pForest value points to the head of this forest list.
56618*/
56619struct RowSetEntry {
56620 i64 v; /* ROWID value for this entry */
56621 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
56622 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
56623};
56624
56625/*
56626** RowSetEntry objects are allocated in large chunks (instances of the
56627** following structure) to reduce memory allocation overhead. The
56628** chunks are kept on a linked list so that they can be deallocated
56629** when the RowSet is destroyed.
56630*/
56631struct RowSetChunk {
56632 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
56633 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry))]; /* Allocated entries */
56634};
56635
56636/*
56637** A RowSet in an instance of the following structure.
56638**
56639** A typedef of this structure if found in sqliteInt.h.
56640*/
56641struct RowSet {
56642 struct RowSetChunk *pChunk; /* List of all chunk allocations */
56643 sqlite3 *db; /* The database connection */
56644 struct RowSetEntry *pEntry; /* List of entries using pRight */
56645 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
56646 struct RowSetEntry *pFresh; /* Source of new entry objects */
56647 struct RowSetEntry *pForest; /* List of binary trees of entries */
56648 u16 nFresh; /* Number of objects on pFresh */
56649 u16 rsFlags; /* Various flags */
56650 int iBatch; /* Current insert batch */
56651};
56652
56653/*
56654** Allowed values for RowSet.rsFlags
56655*/
56656#define ROWSET_SORTED0x01 0x01 /* True if RowSet.pEntry is sorted */
56657#define ROWSET_NEXT0x02 0x02 /* True if sqlite3RowSetNext() has been called */
56658
56659/*
56660** Allocate a RowSet object. Return NULL if a memory allocation
56661** error occurs.
56662*/
56663SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3 *db){
56664 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
56665 if( p ){
56666 int N = sqlite3DbMallocSize(db, p);
56667 p->pChunk = 0;
56668 p->db = db;
56669 p->pEntry = 0;
56670 p->pLast = 0;
56671 p->pForest = 0;
56672 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7) + (char*)p);
56673 p->nFresh = (u16)((N - ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7))/sizeof(struct RowSetEntry));
56674 p->rsFlags = ROWSET_SORTED0x01;
56675 p->iBatch = 0;
56676 }
56677 return p;
56678}
56679
56680/*
56681** Deallocate all chunks from a RowSet. This frees all memory that
56682** the RowSet has allocated over its lifetime. This routine is
56683** the destructor for the RowSet.
56684*/
56685SQLITE_PRIVATEstatic void sqlite3RowSetClear(void *pArg){
56686 RowSet *p = (RowSet*)pArg;
56687 struct RowSetChunk *pChunk, *pNextChunk;
56688 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
56689 pNextChunk = pChunk->pNextChunk;
56690 sqlite3DbFree(p->db, pChunk);
56691 }
56692 p->pChunk = 0;
56693 p->nFresh = 0;
56694 p->pEntry = 0;
56695 p->pLast = 0;
56696 p->pForest = 0;
56697 p->rsFlags = ROWSET_SORTED0x01;
56698}
56699
56700/*
56701** Deallocate all chunks from a RowSet. This frees all memory that
56702** the RowSet has allocated over its lifetime. This routine is
56703** the destructor for the RowSet.
56704*/
56705SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void *pArg){
56706 sqlite3RowSetClear(pArg);
56707 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
56708}
56709
56710/*
56711** Allocate a new RowSetEntry object that is associated with the
56712** given RowSet. Return a pointer to the new and completely uninitialized
56713** object.
56714**
56715** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
56716** routine returns NULL.
56717*/
56718static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
56719 assert( p!=0 )((void) (0));
56720 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
56721 /* We could allocate a fresh RowSetEntry each time one is needed, but it
56722 ** is more efficient to pull a preallocated entry from the pool */
56723 struct RowSetChunk *pNew;
56724 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
56725 if( pNew==0 ){
56726 return 0;
56727 }
56728 pNew->pNextChunk = p->pChunk;
56729 p->pChunk = pNew;
56730 p->pFresh = pNew->aEntry;
56731 p->nFresh = ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry));
56732 }
56733 p->nFresh--;
56734 return p->pFresh++;
56735}
56736
56737/*
56738** Insert a new value into a RowSet.
56739**
56740** The mallocFailed flag of the database connection is set if a
56741** memory allocation fails.
56742*/
56743SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet *p, i64 rowid){
56744 struct RowSetEntry *pEntry; /* The new entry */
56745 struct RowSetEntry *pLast; /* The last prior entry */
56746
56747 /* This routine is never called after sqlite3RowSetNext() */
56748 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 )((void) (0));
56749
56750 pEntry = rowSetEntryAlloc(p);
56751 if( pEntry==0 ) return;
56752 pEntry->v = rowid;
56753 pEntry->pRight = 0;
56754 pLast = p->pLast;
56755 if( pLast ){
56756 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
56757 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
56758 ** where possible */
56759 p->rsFlags &= ~ROWSET_SORTED0x01;
56760 }
56761 pLast->pRight = pEntry;
56762 }else{
56763 p->pEntry = pEntry;
56764 }
56765 p->pLast = pEntry;
56766}
56767
56768/*
56769** Merge two lists of RowSetEntry objects. Remove duplicates.
56770**
56771** The input lists are connected via pRight pointers and are
56772** assumed to each already be in sorted order.
56773*/
56774static struct RowSetEntry *rowSetEntryMerge(
56775 struct RowSetEntry *pA, /* First sorted list to be merged */
56776 struct RowSetEntry *pB /* Second sorted list to be merged */
56777){
56778 struct RowSetEntry head;
56779 struct RowSetEntry *pTail;
56780
56781 pTail = &head;
56782 assert( pA!=0 && pB!=0 )((void) (0));
56783 for(;;){
56784 assert( pA->pRight==0 || pA->v<=pA->pRight->v )((void) (0));
56785 assert( pB->pRight==0 || pB->v<=pB->pRight->v )((void) (0));
56786 if( pA->v<=pB->v ){
56787 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
56788 pA = pA->pRight;
56789 if( pA==0 ){
56790 pTail->pRight = pB;
56791 break;
56792 }
56793 }else{
56794 pTail = pTail->pRight = pB;
56795 pB = pB->pRight;
56796 if( pB==0 ){
56797 pTail->pRight = pA;
56798 break;
56799 }
56800 }
56801 }
56802 return head.pRight;
56803}
56804
56805/*
56806** Sort all elements on the list of RowSetEntry objects into order of
56807** increasing v.
56808*/
56809static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
56810 unsigned int i;
56811 struct RowSetEntry *pNext, *aBucket[40];
56812
56813 memset(aBucket, 0, sizeof(aBucket));
56814 while( pIn ){
56815 pNext = pIn->pRight;
56816 pIn->pRight = 0;
56817 for(i=0; aBucket[i]; i++){
56818 pIn = rowSetEntryMerge(aBucket[i], pIn);
56819 aBucket[i] = 0;
56820 }
56821 aBucket[i] = pIn;
56822 pIn = pNext;
56823 }
56824 pIn = aBucket[0];
56825 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
56826 if( aBucket[i]==0 ) continue;
56827 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
56828 }
56829 return pIn;
56830}
56831
56832
56833/*
56834** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
56835** Convert this tree into a linked list connected by the pRight pointers
56836** and return pointers to the first and last elements of the new list.
56837*/
56838static void rowSetTreeToList(
56839 struct RowSetEntry *pIn, /* Root of the input tree */
56840 struct RowSetEntry **ppFirst, /* Write head of the output list here */
56841 struct RowSetEntry **ppLast /* Write tail of the output list here */
56842){
56843 assert( pIn!=0 )((void) (0));
56844 if( pIn->pLeft ){
56845 struct RowSetEntry *p;
56846 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
56847 p->pRight = pIn;
56848 }else{
56849 *ppFirst = pIn;
56850 }
56851 if( pIn->pRight ){
56852 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
56853 }else{
56854 *ppLast = pIn;
56855 }
56856 assert( (*ppLast)->pRight==0 )((void) (0));
56857}
56858
56859
56860/*
56861** Convert a sorted list of elements (connected by pRight) into a binary
56862** tree with depth of iDepth. A depth of 1 means the tree contains a single
56863** node taken from the head of *ppList. A depth of 2 means a tree with
56864** three nodes. And so forth.
56865**
56866** Use as many entries from the input list as required and update the
56867** *ppList to point to the unused elements of the list. If the input
56868** list contains too few elements, then construct an incomplete tree
56869** and leave *ppList set to NULL.
56870**
56871** Return a pointer to the root of the constructed binary tree.
56872*/
56873static struct RowSetEntry *rowSetNDeepTree(
56874 struct RowSetEntry **ppList,
56875 int iDepth
56876){
56877 struct RowSetEntry *p; /* Root of the new tree */
56878 struct RowSetEntry *pLeft; /* Left subtree */
56879 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
56880 /* Prevent unnecessary deep recursion when we run out of entries */
56881 return 0;
56882 }
56883 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
56884 /* This branch causes a *balanced* tree to be generated. A valid tree
56885 ** is still generated without this branch, but the tree is wildly
56886 ** unbalanced and inefficient. */
56887 pLeft = rowSetNDeepTree(ppList, iDepth-1);
56888 p = *ppList;
56889 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
56890 /* It is safe to always return here, but the resulting tree
56891 ** would be unbalanced */
56892 return pLeft;
56893 }
56894 p->pLeft = pLeft;
56895 *ppList = p->pRight;
56896 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
56897 }else{
56898 p = *ppList;
56899 *ppList = p->pRight;
56900 p->pLeft = p->pRight = 0;
56901 }
56902 return p;
56903}
56904
56905/*
56906** Convert a sorted list of elements into a binary tree. Make the tree
56907** as deep as it needs to be in order to contain the entire list.
56908*/
56909static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
56910 int iDepth; /* Depth of the tree so far */
56911 struct RowSetEntry *p; /* Current tree root */
56912 struct RowSetEntry *pLeft; /* Left subtree */
56913
56914 assert( pList!=0 )((void) (0));
56915 p = pList;
56916 pList = p->pRight;
56917 p->pLeft = p->pRight = 0;
56918 for(iDepth=1; pList; iDepth++){
56919 pLeft = p;
56920 p = pList;
56921 pList = p->pRight;
56922 p->pLeft = pLeft;
56923 p->pRight = rowSetNDeepTree(&pList, iDepth);
56924 }
56925 return p;
56926}
56927
56928/*
56929** Extract the smallest element from the RowSet.
56930** Write the element into *pRowid. Return 1 on success. Return
56931** 0 if the RowSet is already empty.
56932**
56933** After this routine has been called, the sqlite3RowSetInsert()
56934** routine may not be called again.
56935**
56936** This routine may not be called after sqlite3RowSetTest() has
56937** been used. Older versions of RowSet allowed that, but as the
56938** capability was not used by the code generator, it was removed
56939** for code economy.
56940*/
56941SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
56942 assert( p!=0 )((void) (0));
56943 assert( p->pForest==0 )((void) (0)); /* Cannot be used with sqlite3RowSetText() */
56944
56945 /* Merge the forest into a single sorted list on first call */
56946 if( (p->rsFlags & ROWSET_NEXT0x02)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56947 if( (p->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56948 p->pEntry = rowSetEntrySort(p->pEntry);
56949 }
56950 p->rsFlags |= ROWSET_SORTED0x01|ROWSET_NEXT0x02;
56951 }
56952
56953 /* Return the next entry on the list */
56954 if( p->pEntry ){
56955 *pRowid = p->pEntry->v;
56956 p->pEntry = p->pEntry->pRight;
56957 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
56958 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
56959 sqlite3RowSetClear(p);
56960 }
56961 return 1;
56962 }else{
56963 return 0;
56964 }
56965}
56966
56967/*
56968** Check to see if element iRowid was inserted into the rowset as
56969** part of any insert batch prior to iBatch. Return 1 or 0.
56970**
56971** If this is the first test of a new batch and if there exist entries
56972** on pRowSet->pEntry, then sort those entries into the forest at
56973** pRowSet->pForest so that they can be tested.
56974*/
56975SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
56976 struct RowSetEntry *p, *pTree;
56977
56978 /* This routine is never called after sqlite3RowSetNext() */
56979 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 )((void) (0));
56980
56981 /* Sort entries into the forest on the first test of a new batch.
56982 ** To save unnecessary work, only do this when the batch number changes.
56983 */
56984 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
56985 p = pRowSet->pEntry;
56986 if( p ){
56987 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
56988 if( (pRowSet->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56989 /* Only sort the current set of entries if they need it */
56990 p = rowSetEntrySort(p);
56991 }
56992 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56993 ppPrevTree = &pTree->pRight;
56994 if( pTree->pLeft==0 ){
56995 pTree->pLeft = rowSetListToTree(p);
56996 break;
56997 }else{
56998 struct RowSetEntry *pAux, *pTail;
56999 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
57000 pTree->pLeft = 0;
57001 p = rowSetEntryMerge(pAux, p);
57002 }
57003 }
57004 if( pTree==0 ){
57005 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
57006 if( pTree ){
57007 pTree->v = 0;
57008 pTree->pRight = 0;
57009 pTree->pLeft = rowSetListToTree(p);
57010 }
57011 }
57012 pRowSet->pEntry = 0;
57013 pRowSet->pLast = 0;
57014 pRowSet->rsFlags |= ROWSET_SORTED0x01;
57015 }
57016 pRowSet->iBatch = iBatch;
57017 }
57018
57019 /* Test to see if the iRowid value appears anywhere in the forest.
57020 ** Return 1 if it does and 0 if not.
57021 */
57022 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
57023 p = pTree->pLeft;
57024 while( p ){
57025 if( p->v<iRowid ){
57026 p = p->pRight;
57027 }else if( p->v>iRowid ){
57028 p = p->pLeft;
57029 }else{
57030 return 1;
57031 }
57032 }
57033 }
57034 return 0;
57035}
57036
57037/************** End of rowset.c **********************************************/
57038/************** Begin file pager.c *******************************************/
57039/*
57040** 2001 September 15
57041**
57042** The author disclaims copyright to this source code. In place of
57043** a legal notice, here is a blessing:
57044**
57045** May you do good and not evil.
57046** May you find forgiveness for yourself and forgive others.
57047** May you share freely, never taking more than you give.
57048**
57049*************************************************************************
57050** This is the implementation of the page cache subsystem or "pager".
57051**
57052** The pager is used to access a database disk file. It implements
57053** atomic commit and rollback through the use of a journal file that
57054** is separate from the database file. The pager also implements file
57055** locking to prevent two processes from writing the same database
57056** file simultaneously, or one process from reading the database while
57057** another is writing.
57058*/
57059#ifndef SQLITE_OMIT_DISKIO
57060/* #include "sqliteInt.h" */
57061/************** Include wal.h in the middle of pager.c ***********************/
57062/************** Begin file wal.h *********************************************/
57063/*
57064** 2010 February 1
57065**
57066** The author disclaims copyright to this source code. In place of
57067** a legal notice, here is a blessing:
57068**
57069** May you do good and not evil.
57070** May you find forgiveness for yourself and forgive others.
57071** May you share freely, never taking more than you give.
57072**
57073*************************************************************************
57074** This header file defines the interface to the write-ahead logging
57075** system. Refer to the comments below and the header comment attached to
57076** the implementation of each function in log.c for further details.
57077*/
57078
57079#ifndef SQLITE_WAL_H
57080#define SQLITE_WAL_H
57081
57082/* #include "sqliteInt.h" */
57083
57084/* Macros for extracting appropriate sync flags for either transaction
57085** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
57086*/
57087#define WAL_SYNC_FLAGS(X)((X)&0x03) ((X)&0x03)
57088#define CKPT_SYNC_FLAGS(X)(((X)>>2)&0x03) (((X)>>2)&0x03)
57089
57090#ifdef SQLITE_OMIT_WAL
57091# define sqlite3WalOpen(x,y,z) 0
57092# define sqlite3WalLimit(x,y)
57093# define sqlite3WalClose(v,w,x,y,z) 0
57094# define sqlite3WalBeginReadTransaction(y,z) 0
57095# define sqlite3WalEndReadTransaction(z)
57096# define sqlite3WalDbsize(y) 0
57097# define sqlite3WalBeginWriteTransaction(y) 0
57098# define sqlite3WalEndWriteTransaction(x) 0
57099# define sqlite3WalUndo(x,y,z) 0
57100# define sqlite3WalSavepoint(y,z)
57101# define sqlite3WalSavepointUndo(y,z) 0
57102# define sqlite3WalFrames(u,v,w,x,y,z) 0
57103# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
57104# define sqlite3WalCallback(z) 0
57105# define sqlite3WalExclusiveMode(y,z) 0
57106# define sqlite3WalHeapMemory(z) 0
57107# define sqlite3WalFramesize(z) 0
57108# define sqlite3WalFindFrame(x,y,z) 0
57109# define sqlite3WalFile(x) 0
57110# undef SQLITE_USE_SEH
57111#else
57112
57113#define WAL_SAVEPOINT_NDATA4 4
57114
57115/* Connection to a write-ahead log (WAL) file.
57116** There is one object of this type for each pager.
57117*/
57118typedef struct Wal Wal;
57119
57120/* Open and close a connection to a write-ahead log. */
57121SQLITE_PRIVATEstatic int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
57122SQLITE_PRIVATEstatic int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
57123
57124/* Set the limiting size of a WAL file. */
57125SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal*, i64);
57126
57127/* Used by readers to open (lock) and close (unlock) a snapshot. A
57128** snapshot is like a read-transaction. It is the state of the database
57129** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
57130** preserves the current state even if the other threads or processes
57131** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
57132** transaction and releases the lock.
57133*/
57134SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
57135SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal);
57136
57137/* Read a page from the write-ahead log, if it is present. */
57138SQLITE_PRIVATEstatic int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
57139SQLITE_PRIVATEstatic int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
57140
57141/* If the WAL is not empty, return the size of the database. */
57142SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal);
57143
57144/* Obtain or release the WRITER lock. */
57145SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal);
57146SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal);
57147
57148/* Undo any frames written (but not committed) to the log */
57149SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
57150
57151/* Return an integer that records the current (uncommitted) write
57152** position in the WAL */
57153SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
57154
57155/* Move the write position of the WAL back to iFrame. Called in
57156** response to a ROLLBACK TO command. */
57157SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
57158
57159/* Write a frame or frames to the log. */
57160SQLITE_PRIVATEstatic int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
57161
57162/* Copy pages from the log to the database file */
57163SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
57164 Wal *pWal, /* Write-ahead log connection */
57165 sqlite3 *db, /* Check this handle's interrupt flag */
57166 int eMode, /* One of PASSIVE, FULL and RESTART */
57167 int (*xBusy)(void*), /* Function to call when busy */
57168 void *pBusyArg, /* Context argument for xBusyHandler */
57169 int sync_flags, /* Flags to sync db file with (or 0) */
57170 int nBuf, /* Size of buffer nBuf */
57171 u8 *zBuf, /* Temporary buffer to use */
57172 int *pnLog, /* OUT: Number of frames in WAL */
57173 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
57174);
57175
57176/* Return the value to pass to a sqlite3_wal_hook callback, the
57177** number of frames in the WAL at the point of the last commit since
57178** sqlite3WalCallback() was called. If no commits have occurred since
57179** the last call, then return 0.
57180*/
57181SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal);
57182
57183/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
57184** by the pager layer on the database file.
57185*/
57186SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op);
57187
57188/* Return true if the argument is non-NULL and the WAL module is using
57189** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
57190** WAL module is using shared-memory, return false.
57191*/
57192SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal);
57193
57194#ifdef SQLITE_ENABLE_SNAPSHOT1
57195SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
57196SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
57197SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal);
57198SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
57199SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal);
57200#endif
57201
57202#ifdef SQLITE_ENABLE_ZIPVFS
57203/* If the WAL file is not empty, return the number of bytes of content
57204** stored in each frame (i.e. the db page-size when the WAL was created).
57205*/
57206SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal);
57207#endif
57208
57209/* Return the sqlite3_file object for the WAL file */
57210SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal);
57211
57212#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
57213SQLITE_PRIVATEstatic int sqlite3WalWriteLock(Wal *pWal, int bLock);
57214SQLITE_PRIVATEstatic void sqlite3WalDb(Wal *pWal, sqlite3 *db);
57215#endif
57216
57217#ifdef SQLITE_USE_SEH
57218SQLITE_PRIVATEstatic int sqlite3WalSystemErrno(Wal*);
57219#endif
57220
57221#endif /* ifndef SQLITE_OMIT_WAL */
57222#endif /* SQLITE_WAL_H */
57223
57224/************** End of wal.h *************************************************/
57225/************** Continuing where we left off in pager.c **********************/
57226
57227
57228/******************* NOTES ON THE DESIGN OF THE PAGER ************************
57229**
57230** This comment block describes invariants that hold when using a rollback
57231** journal. These invariants do not apply for journal_mode=WAL,
57232** journal_mode=MEMORY, or journal_mode=OFF.
57233**
57234** Within this comment block, a page is deemed to have been synced
57235** automatically as soon as it is written when PRAGMA synchronous=OFF.
57236** Otherwise, the page is not synced until the xSync method of the VFS
57237** is called successfully on the file containing the page.
57238**
57239** Definition: A page of the database file is said to be "overwriteable" if
57240** one or more of the following are true about the page:
57241**
57242** (a) The original content of the page as it was at the beginning of
57243** the transaction has been written into the rollback journal and
57244** synced.
57245**
57246** (b) The page was a freelist leaf page at the start of the transaction.
57247**
57248** (c) The page number is greater than the largest page that existed in
57249** the database file at the start of the transaction.
57250**
57251** (1) A page of the database file is never overwritten unless one of the
57252** following are true:
57253**
57254** (a) The page and all other pages on the same sector are overwriteable.
57255**
57256** (b) The atomic page write optimization is enabled, and the entire
57257** transaction other than the update of the transaction sequence
57258** number consists of a single page change.
57259**
57260** (2) The content of a page written into the rollback journal exactly matches
57261** both the content in the database when the rollback journal was written
57262** and the content in the database at the beginning of the current
57263** transaction.
57264**
57265** (3) Writes to the database file are an integer multiple of the page size
57266** in length and are aligned on a page boundary.
57267**
57268** (4) Reads from the database file are either aligned on a page boundary and
57269** an integer multiple of the page size in length or are taken from the
57270** first 100 bytes of the database file.
57271**
57272** (5) All writes to the database file are synced prior to the rollback journal
57273** being deleted, truncated, or zeroed.
57274**
57275** (6) If a super-journal file is used, then all writes to the database file
57276** are synced prior to the super-journal being deleted.
57277**
57278** Definition: Two databases (or the same database at two points it time)
57279** are said to be "logically equivalent" if they give the same answer to
57280** all queries. Note in particular the content of freelist leaf
57281** pages can be changed arbitrarily without affecting the logical equivalence
57282** of the database.
57283**
57284** (7) At any time, if any subset, including the empty set and the total set,
57285** of the unsynced changes to a rollback journal are removed and the
57286** journal is rolled back, the resulting database file will be logically
57287** equivalent to the database file at the beginning of the transaction.
57288**
57289** (8) When a transaction is rolled back, the xTruncate method of the VFS
57290** is called to restore the database file to the same size it was at
57291** the beginning of the transaction. (In some VFSes, the xTruncate
57292** method is a no-op, but that does not change the fact the SQLite will
57293** invoke it.)
57294**
57295** (9) Whenever the database file is modified, at least one bit in the range
57296** of bytes from 24 through 39 inclusive will be changed prior to releasing
57297** the EXCLUSIVE lock, thus signaling other connections on the same
57298** database to flush their caches.
57299**
57300** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
57301** than one billion transactions.
57302**
57303** (11) A database file is well-formed at the beginning and at the conclusion
57304** of every transaction.
57305**
57306** (12) An EXCLUSIVE lock is held on the database file when writing to
57307** the database file.
57308**
57309** (13) A SHARED lock is held on the database file while reading any
57310** content out of the database file.
57311**
57312******************************************************************************/
57313
57314/*
57315** Macros for troubleshooting. Normally turned off
57316*/
57317#if 0
57318int sqlite3PagerTrace=1; /* True to enable tracing */
57319#define sqlite3DebugPrintf printf
57320#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
57321#else
57322#define PAGERTRACE(X)
57323#endif
57324
57325/*
57326** The following two macros are used within the PAGERTRACE() macros above
57327** to print out file-descriptors.
57328**
57329** PAGERID() takes a pointer to a Pager struct as its argument. The
57330** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
57331** struct as its argument.
57332*/
57333#define PAGERID(p)(((int)(long int)(p->fd))) (SQLITE_PTR_TO_INT(p->fd)((int)(long int)(p->fd)))
57334#define FILEHANDLEID(fd)(((int)(long int)(fd))) (SQLITE_PTR_TO_INT(fd)((int)(long int)(fd)))
57335
57336/*
57337** The Pager.eState variable stores the current 'state' of a pager. A
57338** pager may be in any one of the seven states shown in the following
57339** state diagram.
57340**
57341** OPEN <------+------+
57342** | | |
57343** V | |
57344** +---------> READER-------+ |
57345** | | |
57346** | V |
57347** |<-------WRITER_LOCKED------> ERROR
57348** | | ^
57349** | V |
57350** |<------WRITER_CACHEMOD-------->|
57351** | | |
57352** | V |
57353** |<-------WRITER_DBMOD---------->|
57354** | | |
57355** | V |
57356** +<------WRITER_FINISHED-------->+
57357**
57358**
57359** List of state transitions and the C [function] that performs each:
57360**
57361** OPEN -> READER [sqlite3PagerSharedLock]
57362** READER -> OPEN [pager_unlock]
57363**
57364** READER -> WRITER_LOCKED [sqlite3PagerBegin]
57365** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
57366** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
57367** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
57368** WRITER_*** -> READER [pager_end_transaction]
57369**
57370** WRITER_*** -> ERROR [pager_error]
57371** ERROR -> OPEN [pager_unlock]
57372**
57373**
57374** OPEN:
57375**
57376** The pager starts up in this state. Nothing is guaranteed in this
57377** state - the file may or may not be locked and the database size is
57378** unknown. The database may not be read or written.
57379**
57380** * No read or write transaction is active.
57381** * Any lock, or no lock at all, may be held on the database file.
57382** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
57383**
57384** READER:
57385**
57386** In this state all the requirements for reading the database in
57387** rollback (non-WAL) mode are met. Unless the pager is (or recently
57388** was) in exclusive-locking mode, a user-level read transaction is
57389** open. The database size is known in this state.
57390**
57391** A connection running with locking_mode=normal enters this state when
57392** it opens a read-transaction on the database and returns to state
57393** OPEN after the read-transaction is completed. However a connection
57394** running in locking_mode=exclusive (including temp databases) remains in
57395** this state even after the read-transaction is closed. The only way
57396** a locking_mode=exclusive connection can transition from READER to OPEN
57397** is via the ERROR state (see below).
57398**
57399** * A read transaction may be active (but a write-transaction cannot).
57400** * A SHARED or greater lock is held on the database file.
57401** * The dbSize variable may be trusted (even if a user-level read
57402** transaction is not active). The dbOrigSize and dbFileSize variables
57403** may not be trusted at this point.
57404** * If the database is a WAL database, then the WAL connection is open.
57405** * Even if a read-transaction is not open, it is guaranteed that
57406** there is no hot-journal in the file-system.
57407**
57408** WRITER_LOCKED:
57409**
57410** The pager moves to this state from READER when a write-transaction
57411** is first opened on the database. In WRITER_LOCKED state, all locks
57412** required to start a write-transaction are held, but no actual
57413** modifications to the cache or database have taken place.
57414**
57415** In rollback mode, a RESERVED or (if the transaction was opened with
57416** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
57417** moving to this state, but the journal file is not written to or opened
57418** to in this state. If the transaction is committed or rolled back while
57419** in WRITER_LOCKED state, all that is required is to unlock the database
57420** file.
57421**
57422** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
57423** If the connection is running with locking_mode=exclusive, an attempt
57424** is made to obtain an EXCLUSIVE lock on the database file.
57425**
57426** * A write transaction is active.
57427** * If the connection is open in rollback-mode, a RESERVED or greater
57428** lock is held on the database file.
57429** * If the connection is open in WAL-mode, a WAL write transaction
57430** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
57431** called).
57432** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
57433** * The contents of the pager cache have not been modified.
57434** * The journal file may or may not be open.
57435** * Nothing (not even the first header) has been written to the journal.
57436**
57437** WRITER_CACHEMOD:
57438**
57439** A pager moves from WRITER_LOCKED state to this state when a page is
57440** first modified by the upper layer. In rollback mode the journal file
57441** is opened (if it is not already open) and a header written to the
57442** start of it. The database file on disk has not been modified.
57443**
57444** * A write transaction is active.
57445** * A RESERVED or greater lock is held on the database file.
57446** * The journal file is open and the first header has been written
57447** to it, but the header has not been synced to disk.
57448** * The contents of the page cache have been modified.
57449**
57450** WRITER_DBMOD:
57451**
57452** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
57453** when it modifies the contents of the database file. WAL connections
57454** never enter this state (since they do not modify the database file,
57455** just the log file).
57456**
57457** * A write transaction is active.
57458** * An EXCLUSIVE or greater lock is held on the database file.
57459** * The journal file is open and the first header has been written
57460** and synced to disk.
57461** * The contents of the page cache have been modified (and possibly
57462** written to disk).
57463**
57464** WRITER_FINISHED:
57465**
57466** It is not possible for a WAL connection to enter this state.
57467**
57468** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
57469** state after the entire transaction has been successfully written into the
57470** database file. In this state the transaction may be committed simply
57471** by finalizing the journal file. Once in WRITER_FINISHED state, it is
57472** not possible to modify the database further. At this point, the upper
57473** layer must either commit or rollback the transaction.
57474**
57475** * A write transaction is active.
57476** * An EXCLUSIVE or greater lock is held on the database file.
57477** * All writing and syncing of journal and database data has finished.
57478** If no error occurred, all that remains is to finalize the journal to
57479** commit the transaction. If an error did occur, the caller will need
57480** to rollback the transaction.
57481**
57482** ERROR:
57483**
57484** The ERROR state is entered when an IO or disk-full error (including
57485** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
57486** difficult to be sure that the in-memory pager state (cache contents,
57487** db size etc.) are consistent with the contents of the file-system.
57488**
57489** Temporary pager files may enter the ERROR state, but in-memory pagers
57490** cannot.
57491**
57492** For example, if an IO error occurs while performing a rollback,
57493** the contents of the page-cache may be left in an inconsistent state.
57494** At this point it would be dangerous to change back to READER state
57495** (as usually happens after a rollback). Any subsequent readers might
57496** report database corruption (due to the inconsistent cache), and if
57497** they upgrade to writers, they may inadvertently corrupt the database
57498** file. To avoid this hazard, the pager switches into the ERROR state
57499** instead of READER following such an error.
57500**
57501** Once it has entered the ERROR state, any attempt to use the pager
57502** to read or write data returns an error. Eventually, once all
57503** outstanding transactions have been abandoned, the pager is able to
57504** transition back to OPEN state, discarding the contents of the
57505** page-cache and any other in-memory state at the same time. Everything
57506** is reloaded from disk (and, if necessary, hot-journal rollback performed)
57507** when a read-transaction is next opened on the pager (transitioning
57508** the pager into READER state). At that point the system has recovered
57509** from the error.
57510**
57511** Specifically, the pager jumps into the ERROR state if:
57512**
57513** 1. An error occurs while attempting a rollback. This happens in
57514** function sqlite3PagerRollback().
57515**
57516** 2. An error occurs while attempting to finalize a journal file
57517** following a commit in function sqlite3PagerCommitPhaseTwo().
57518**
57519** 3. An error occurs while attempting to write to the journal or
57520** database file in function pagerStress() in order to free up
57521** memory.
57522**
57523** In other cases, the error is returned to the b-tree layer. The b-tree
57524** layer then attempts a rollback operation. If the error condition
57525** persists, the pager enters the ERROR state via condition (1) above.
57526**
57527** Condition (3) is necessary because it can be triggered by a read-only
57528** statement executed within a transaction. In this case, if the error
57529** code were simply returned to the user, the b-tree layer would not
57530** automatically attempt a rollback, as it assumes that an error in a
57531** read-only statement cannot leave the pager in an internally inconsistent
57532** state.
57533**
57534** * The Pager.errCode variable is set to something other than SQLITE_OK.
57535** * There are one or more outstanding references to pages (after the
57536** last reference is dropped the pager should move back to OPEN state).
57537** * The pager is not an in-memory pager.
57538**
57539**
57540** Notes:
57541**
57542** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
57543** connection is open in WAL mode. A WAL connection is always in one
57544** of the first four states.
57545**
57546** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
57547** state. There are two exceptions: immediately after exclusive-mode has
57548** been turned on (and before any read or write transactions are
57549** executed), and when the pager is leaving the "error state".
57550**
57551** * See also: assert_pager_state().
57552*/
57553#define PAGER_OPEN0 0
57554#define PAGER_READER1 1
57555#define PAGER_WRITER_LOCKED2 2
57556#define PAGER_WRITER_CACHEMOD3 3
57557#define PAGER_WRITER_DBMOD4 4
57558#define PAGER_WRITER_FINISHED5 5
57559#define PAGER_ERROR6 6
57560
57561/*
57562** The Pager.eLock variable is almost always set to one of the
57563** following locking-states, according to the lock currently held on
57564** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
57565** This variable is kept up to date as locks are taken and released by
57566** the pagerLockDb() and pagerUnlockDb() wrappers.
57567**
57568** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
57569** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
57570** the operation was successful. In these circumstances pagerLockDb() and
57571** pagerUnlockDb() take a conservative approach - eLock is always updated
57572** when unlocking the file, and only updated when locking the file if the
57573** VFS call is successful. This way, the Pager.eLock variable may be set
57574** to a less exclusive (lower) value than the lock that is actually held
57575** at the system level, but it is never set to a more exclusive value.
57576**
57577** This is usually safe. If an xUnlock fails or appears to fail, there may
57578** be a few redundant xLock() calls or a lock may be held for longer than
57579** required, but nothing really goes wrong.
57580**
57581** The exception is when the database file is unlocked as the pager moves
57582** from ERROR to OPEN state. At this point there may be a hot-journal file
57583** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
57584** transition, by the same pager or any other). If the call to xUnlock()
57585** fails at this point and the pager is left holding an EXCLUSIVE lock, this
57586** can confuse the call to xCheckReservedLock() call made later as part
57587** of hot-journal detection.
57588**
57589** xCheckReservedLock() is defined as returning true "if there is a RESERVED
57590** lock held by this process or any others". So xCheckReservedLock may
57591** return true because the caller itself is holding an EXCLUSIVE lock (but
57592** doesn't know it because of a previous error in xUnlock). If this happens
57593** a hot-journal may be mistaken for a journal being created by an active
57594** transaction in another process, causing SQLite to read from the database
57595** without rolling it back.
57596**
57597** To work around this, if a call to xUnlock() fails when unlocking the
57598** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
57599** is only changed back to a real locking state after a successful call
57600** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
57601** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
57602** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
57603** lock on the database file before attempting to roll it back. See function
57604** PagerSharedLock() for more detail.
57605**
57606** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
57607** PAGER_OPEN state.
57608*/
57609#define UNKNOWN_LOCK(4 +1) (EXCLUSIVE_LOCK4+1)
57610
57611/*
57612** The maximum allowed sector size. 64KiB. If the xSectorsize() method
57613** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
57614** This could conceivably cause corruption following a power failure on
57615** such a system. This is currently an undocumented limit.
57616*/
57617#define MAX_SECTOR_SIZE0x10000 0x10000
57618
57619
57620/*
57621** An instance of the following structure is allocated for each active
57622** savepoint and statement transaction in the system. All such structures
57623** are stored in the Pager.aSavepoint[] array, which is allocated and
57624** resized using sqlite3Realloc().
57625**
57626** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
57627** set to 0. If a journal-header is written into the main journal while
57628** the savepoint is active, then iHdrOffset is set to the byte offset
57629** immediately following the last journal record written into the main
57630** journal before the journal-header. This is required during savepoint
57631** rollback (see pagerPlaybackSavepoint()).
57632*/
57633typedef struct PagerSavepoint PagerSavepoint;
57634struct PagerSavepoint {
57635 i64 iOffset; /* Starting offset in main journal */
57636 i64 iHdrOffset; /* See above */
57637 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
57638 Pgno nOrig; /* Original number of pages in file */
57639 Pgno iSubRec; /* Index of first record in sub-journal */
57640 int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */
57641#ifndef SQLITE_OMIT_WAL
57642 u32 aWalData[WAL_SAVEPOINT_NDATA4]; /* WAL savepoint context */
57643#endif
57644};
57645
57646/*
57647** Bits of the Pager.doNotSpill flag. See further description below.
57648*/
57649#define SPILLFLAG_OFF0x01 0x01 /* Never spill cache. Set via pragma */
57650#define SPILLFLAG_ROLLBACK0x02 0x02 /* Current rolling back, so do not spill */
57651#define SPILLFLAG_NOSYNC0x04 0x04 /* Spill is ok, but do not sync */
57652
57653/*
57654** An open page cache is an instance of struct Pager. A description of
57655** some of the more important member variables follows:
57656**
57657** eState
57658**
57659** The current 'state' of the pager object. See the comment and state
57660** diagram above for a description of the pager state.
57661**
57662** eLock
57663**
57664** For a real on-disk database, the current lock held on the database file -
57665** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
57666**
57667** For a temporary or in-memory database (neither of which require any
57668** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
57669** databases always have Pager.exclusiveMode==1, this tricks the pager
57670** logic into thinking that it already has all the locks it will ever
57671** need (and no reason to release them).
57672**
57673** In some (obscure) circumstances, this variable may also be set to
57674** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
57675** details.
57676**
57677** changeCountDone
57678**
57679** This boolean variable is used to make sure that the change-counter
57680** (the 4-byte header field at byte offset 24 of the database file) is
57681** not updated more often than necessary.
57682**
57683** It is set to true when the change-counter field is updated, which
57684** can only happen if an exclusive lock is held on the database file.
57685** It is cleared (set to false) whenever an exclusive lock is
57686** relinquished on the database file. Each time a transaction is committed,
57687** The changeCountDone flag is inspected. If it is true, the work of
57688** updating the change-counter is omitted for the current transaction.
57689**
57690** This mechanism means that when running in exclusive mode, a connection
57691** need only update the change-counter once, for the first transaction
57692** committed.
57693**
57694** setSuper
57695**
57696** When PagerCommitPhaseOne() is called to commit a transaction, it may
57697** (or may not) specify a super-journal name to be written into the
57698** journal file before it is synced to disk.
57699**
57700** Whether or not a journal file contains a super-journal pointer affects
57701** the way in which the journal file is finalized after the transaction is
57702** committed or rolled back when running in "journal_mode=PERSIST" mode.
57703** If a journal file does not contain a super-journal pointer, it is
57704** finalized by overwriting the first journal header with zeroes. If
57705** it does contain a super-journal pointer the journal file is finalized
57706** by truncating it to zero bytes, just as if the connection were
57707** running in "journal_mode=truncate" mode.
57708**
57709** Journal files that contain super-journal pointers cannot be finalized
57710** simply by overwriting the first journal-header with zeroes, as the
57711** super-journal pointer could interfere with hot-journal rollback of any
57712** subsequently interrupted transaction that reuses the journal file.
57713**
57714** The flag is cleared as soon as the journal file is finalized (either
57715** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
57716** journal file from being successfully finalized, the setSuper flag
57717** is cleared anyway (and the pager will move to ERROR state).
57718**
57719** doNotSpill
57720**
57721** This variables control the behavior of cache-spills (calls made by
57722** the pcache module to the pagerStress() routine to write cached data
57723** to the file-system in order to free up memory).
57724**
57725** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
57726** writing to the database from pagerStress() is disabled altogether.
57727** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
57728** comes up during savepoint rollback that requires the pcache module
57729** to allocate a new page to prevent the journal file from being written
57730** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
57731** case is a user preference.
57732**
57733** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
57734** pagerStress() is permitted, but syncing the journal file is not.
57735** This flag is set by sqlite3PagerWrite() when the file-system sector-size
57736** is larger than the database page-size in order to prevent a journal sync
57737** from happening in between the journalling of two pages on the same sector.
57738**
57739** subjInMemory
57740**
57741** This is a boolean variable. If true, then any required sub-journal
57742** is opened as an in-memory journal file. If false, then in-memory
57743** sub-journals are only used for in-memory pager files.
57744**
57745** This variable is updated by the upper layer each time a new
57746** write-transaction is opened.
57747**
57748** dbSize, dbOrigSize, dbFileSize
57749**
57750** Variable dbSize is set to the number of pages in the database file.
57751** It is valid in PAGER_READER and higher states (all states except for
57752** OPEN and ERROR).
57753**
57754** dbSize is set based on the size of the database file, which may be
57755** larger than the size of the database (the value stored at offset
57756** 28 of the database header by the btree). If the size of the file
57757** is not an integer multiple of the page-size, the value stored in
57758** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
57759** Except, any file that is greater than 0 bytes in size is considered
57760** to have at least one page. (i.e. a 1KB file with 2K page-size leads
57761** to dbSize==1).
57762**
57763** During a write-transaction, if pages with page-numbers greater than
57764** dbSize are modified in the cache, dbSize is updated accordingly.
57765** Similarly, if the database is truncated using PagerTruncateImage(),
57766** dbSize is updated.
57767**
57768** Variables dbOrigSize and dbFileSize are valid in states
57769** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
57770** variable at the start of the transaction. It is used during rollback,
57771** and to determine whether or not pages need to be journalled before
57772** being modified.
57773**
57774** Throughout a write-transaction, dbFileSize contains the size of
57775** the file on disk in pages. It is set to a copy of dbSize when the
57776** write-transaction is first opened, and updated when VFS calls are made
57777** to write or truncate the database file on disk.
57778**
57779** The only reason the dbFileSize variable is required is to suppress
57780** unnecessary calls to xTruncate() after committing a transaction. If,
57781** when a transaction is committed, the dbFileSize variable indicates
57782** that the database file is larger than the database image (Pager.dbSize),
57783** pager_truncate() is called. The pager_truncate() call uses xFilesize()
57784** to measure the database file on disk, and then truncates it if required.
57785** dbFileSize is not used when rolling back a transaction. In this case
57786** pager_truncate() is called unconditionally (which means there may be
57787** a call to xFilesize() that is not strictly required). In either case,
57788** pager_truncate() may cause the file to become smaller or larger.
57789**
57790** dbHintSize
57791**
57792** The dbHintSize variable is used to limit the number of calls made to
57793** the VFS xFileControl(FCNTL_SIZE_HINT) method.
57794**
57795** dbHintSize is set to a copy of the dbSize variable when a
57796** write-transaction is opened (at the same time as dbFileSize and
57797** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
57798** dbHintSize is increased to the number of pages that correspond to the
57799** size-hint passed to the method call. See pager_write_pagelist() for
57800** details.
57801**
57802** errCode
57803**
57804** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
57805** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
57806** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
57807** sub-codes.
57808**
57809** syncFlags, walSyncFlags
57810**
57811** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
57812** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
57813** and contains the flags used to sync the checkpoint operations in the
57814** lower two bits, and sync flags used for transaction commits in the WAL
57815** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
57816** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
57817** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
57818** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
57819** meaning that the 0x04 and 0x08 bits are both zero.
57820*/
57821struct Pager {
57822 sqlite3_vfs *pVfs; /* OS functions to use for IO */
57823 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
57824 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
57825 u8 useJournal; /* Use a rollback journal on this file */
57826 u8 noSync; /* Do not sync the journal if true */
57827 u8 fullSync; /* Do extra syncs of the journal for robustness */
57828 u8 extraSync; /* sync directory after journal delete */
57829 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
57830 u8 walSyncFlags; /* See description above */
57831 u8 tempFile; /* zFilename is a temporary or immutable file */
57832 u8 noLock; /* Do not lock (except in WAL mode) */
57833 u8 readOnly; /* True for a read-only database */
57834 u8 memDb; /* True to inhibit all file I/O */
57835 u8 memVfs; /* VFS-implemented memory database */
57836
57837 /**************************************************************************
57838 ** The following block contains those class members that change during
57839 ** routine operation. Class members not in this block are either fixed
57840 ** when the pager is first created or else only change when there is a
57841 ** significant mode change (such as changing the page_size, locking_mode,
57842 ** or the journal_mode). From another view, these class members describe
57843 ** the "state" of the pager, while other class members describe the
57844 ** "configuration" of the pager.
57845 */
57846 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
57847 u8 eLock; /* Current lock held on database file */
57848 u8 changeCountDone; /* Set after incrementing the change-counter */
57849 u8 setSuper; /* Super-jrnl name is written into jrnl */
57850 u8 doNotSpill; /* Do not spill the cache when non-zero */
57851 u8 subjInMemory; /* True to use in-memory sub-journals */
57852 u8 bUseFetch; /* True to use xFetch() */
57853 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
57854 Pgno dbSize; /* Number of pages in the database */
57855 Pgno dbOrigSize; /* dbSize before the current transaction */
57856 Pgno dbFileSize; /* Number of pages in the database file */
57857 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
57858 int errCode; /* One of several kinds of errors */
57859 int nRec; /* Pages journalled since last j-header written */
57860 u32 cksumInit; /* Quasi-random value added to every checksum */
57861 u32 nSubRec; /* Number of records written to sub-journal */
57862 Bitvec *pInJournal; /* One bit for each page in the database file */
57863 sqlite3_file *fd; /* File descriptor for database */
57864 sqlite3_file *jfd; /* File descriptor for main journal */
57865 sqlite3_file *sjfd; /* File descriptor for sub-journal */
57866 i64 journalOff; /* Current write offset in the journal file */
57867 i64 journalHdr; /* Byte offset to previous journal header */
57868 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
57869 PagerSavepoint *aSavepoint; /* Array of active savepoints */
57870 int nSavepoint; /* Number of elements in aSavepoint[] */
57871 u32 iDataVersion; /* Changes whenever database content changes */
57872 char dbFileVers[16]; /* Changes whenever database file changes */
57873
57874 int nMmapOut; /* Number of mmap pages currently outstanding */
57875 sqlite3_int64 szMmap; /* Desired maximum mmap size */
57876 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
57877 /*
57878 ** End of the routinely-changing class members
57879 ***************************************************************************/
57880
57881 u16 nExtra; /* Add this many bytes to each in-memory page */
57882 i16 nReserve; /* Number of unused bytes at end of each page */
57883 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
57884 u32 sectorSize; /* Assumed sector size during rollback */
57885 Pgno mxPgno; /* Maximum allowed size of the database */
57886 Pgno lckPgno; /* Page number for the locking page */
57887 i64 pageSize; /* Number of bytes in a page */
57888 i64 journalSizeLimit; /* Size limit for persistent journal files */
57889 char *zFilename; /* Name of the database file */
57890 char *zJournal; /* Name of the journal file */
57891 int (*xBusyHandler)(void*); /* Function to call when busy */
57892 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
57893 u32 aStat[4]; /* Total cache hits, misses, writes, spills */
57894#ifdef SQLITE_TEST
57895 int nRead; /* Database pages read */
57896#endif
57897 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
57898 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
57899 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
57900 PCache *pPCache; /* Pointer to page cache object */
57901#ifndef SQLITE_OMIT_WAL
57902 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
57903 char *zWal; /* File name for write-ahead log */
57904#endif
57905};
57906
57907/*
57908** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
57909** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
57910** or CACHE_WRITE to sqlite3_db_status().
57911*/
57912#define PAGER_STAT_HIT0 0
57913#define PAGER_STAT_MISS1 1
57914#define PAGER_STAT_WRITE2 2
57915#define PAGER_STAT_SPILL3 3
57916
57917/*
57918** The following global variables hold counters used for
57919** testing purposes only. These variables do not exist in
57920** a non-testing build. These variables are not thread-safe.
57921*/
57922#ifdef SQLITE_TEST
57923SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
57924SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
57925SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
57926# define PAGER_INCR(v) v++
57927#else
57928# define PAGER_INCR(v)
57929#endif
57930
57931
57932
57933/*
57934** Journal files begin with the following magic string. The data
57935** was obtained from /dev/random. It is used only as a sanity check.
57936**
57937** Since version 2.8.0, the journal format contains additional sanity
57938** checking information. If the power fails while the journal is being
57939** written, semi-random garbage data might appear in the journal
57940** file after power is restored. If an attempt is then made
57941** to roll the journal back, the database could be corrupted. The additional
57942** sanity checking data is an attempt to discover the garbage in the
57943** journal and ignore it.
57944**
57945** The sanity checking information for the new journal format consists
57946** of a 32-bit checksum on each page of data. The checksum covers both
57947** the page number and the pPager->pageSize bytes of data for the page.
57948** This cksum is initialized to a 32-bit random value that appears in the
57949** journal file right after the header. The random initializer is important,
57950** because garbage data that appears at the end of a journal is likely
57951** data that was once in other files that have now been deleted. If the
57952** garbage data came from an obsolete journal file, the checksums might
57953** be correct. But by initializing the checksum to random value which
57954** is different for every journal, we minimize that risk.
57955*/
57956static const unsigned char aJournalMagic[] = {
57957 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
57958};
57959
57960/*
57961** The size of the of each page record in the journal is given by
57962** the following macro.
57963*/
57964#define JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8) ((pPager->pageSize) + 8)
57965
57966/*
57967** The journal header size for this pager. This is usually the same
57968** size as a single disk sector. See also setSectorSize().
57969*/
57970#define JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) (pPager->sectorSize)
57971
57972/*
57973** The macro MEMDB is true if we are dealing with an in-memory database.
57974** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
57975** the value of MEMDB will be a constant and the compiler will optimize
57976** out code that would never execute.
57977*/
57978#ifdef SQLITE_OMIT_MEMORYDB
57979# define MEMDBpPager->memDb 0
57980#else
57981# define MEMDBpPager->memDb pPager->memDb
57982#endif
57983
57984/*
57985** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
57986** interfaces to access the database using memory-mapped I/O.
57987*/
57988#if SQLITE_MAX_MMAP_SIZE20971520>0
57989# define USEFETCH(x)((x)->bUseFetch) ((x)->bUseFetch)
57990#else
57991# define USEFETCH(x)((x)->bUseFetch) 0
57992#endif
57993
57994/*
57995** The argument to this macro is a file descriptor (type sqlite3_file*).
57996** Return 0 if it is not open, or non-zero (but not 1) if it is.
57997**
57998** This is so that expressions can be written as:
57999**
58000** if( isOpen(pPager->jfd) ){ ...
58001**
58002** instead of
58003**
58004** if( pPager->jfd->pMethods ){ ...
58005*/
58006#define isOpen(pFd)((pFd)->pMethods!=0) ((pFd)->pMethods!=0)
58007
58008#ifdef SQLITE_DIRECT_OVERFLOW_READ1
58009/*
58010** Return true if page pgno can be read directly from the database file
58011** by the b-tree layer. This is the case if:
58012**
58013** (1) the database file is open
58014** (2) the VFS for the database is able to do unaligned sub-page reads
58015** (3) there are no dirty pages in the cache, and
58016** (4) the desired page is not currently in the wal file.
58017*/
58018SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
58019 assert( pPager!=0 )((void) (0));
58020 assert( pPager->fd!=0 )((void) (0));
58021 if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */
58022 assert( pPager->fd->pMethods->xDeviceCharacteristics!=0 )((void) (0));
58023 if( (pPager->fd->pMethods->xDeviceCharacteristics(pPager->fd)
58024 & SQLITE_IOCAP_SUBPAGE_READ0x00008000)==0 ){
58025 return 0; /* Case (2) */
58026 }
58027 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */
58028#ifndef SQLITE_OMIT_WAL
58029 if( pPager->pWal ){
58030 u32 iRead = 0;
58031 (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
58032 return iRead==0; /* Condition (4) */
58033 }
58034#endif
58035 return 1;
58036}
58037#endif
58038
58039#ifndef SQLITE_OMIT_WAL
58040# define pagerUseWal(x)((x)->pWal!=0) ((x)->pWal!=0)
58041#else
58042# define pagerUseWal(x)((x)->pWal!=0) 0
58043# define pagerRollbackWal(x) 0
58044# define pagerWalFrames(v,w,x,y) 0
58045# define pagerOpenWalIfPresent(z) SQLITE_OK0
58046# define pagerBeginReadTransaction(z) SQLITE_OK0
58047#endif
58048
58049#ifndef NDEBUG1
58050/*
58051** Usage:
58052**
58053** assert( assert_pager_state(pPager) );
58054**
58055** This function runs many asserts to try to find inconsistencies in
58056** the internal state of the Pager object.
58057*/
58058static int assert_pager_state(Pager *p){
58059 Pager *pPager = p;
58060
58061 /* State must be valid. */
58062 assert( p->eState==PAGER_OPEN((void) (0))
58063 || p->eState==PAGER_READER((void) (0))
58064 || p->eState==PAGER_WRITER_LOCKED((void) (0))
58065 || p->eState==PAGER_WRITER_CACHEMOD((void) (0))
58066 || p->eState==PAGER_WRITER_DBMOD((void) (0))
58067 || p->eState==PAGER_WRITER_FINISHED((void) (0))
58068 || p->eState==PAGER_ERROR((void) (0))
58069 )((void) (0));
58070
58071 /* Regardless of the current state, a temp-file connection always behaves
58072 ** as if it has an exclusive lock on the database file. It never updates
58073 ** the change-counter field, so the changeCountDone flag is always set.
58074 */
58075 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK )((void) (0));
58076 assert( p->tempFile==0 || pPager->changeCountDone )((void) (0));
58077
58078 /* If the useJournal flag is clear, the journal-mode must be "OFF".
58079 ** And if the journal-mode is "OFF", the journal file must not be open.
58080 */
58081 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal )((void) (0));
58082 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) )((void) (0));
58083
58084 /* Check that MEMDB implies noSync. And an in-memory journal. Since
58085 ** this means an in-memory pager performs no IO at all, it cannot encounter
58086 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
58087 ** a journal file. (although the in-memory journal implementation may
58088 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
58089 ** is therefore not possible for an in-memory pager to enter the ERROR
58090 ** state.
58091 */
58092 if( MEMDBpPager->memDb ){
58093 assert( !isOpen(p->fd) )((void) (0));
58094 assert( p->noSync )((void) (0));
58095 assert( p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
58096 || p->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
58097 )((void) (0));
58098 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN )((void) (0));
58099 assert( pagerUseWal(p)==0 )((void) (0));
58100 }
58101
58102 /* If changeCountDone is set, a RESERVED lock or greater must be held
58103 ** on the file.
58104 */
58105 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK )((void) (0));
58106 assert( p->eLock!=PENDING_LOCK )((void) (0));
58107
58108 switch( p->eState ){
58109 case PAGER_OPEN0:
58110 assert( !MEMDB )((void) (0));
58111 assert( pPager->errCode==SQLITE_OK )((void) (0));
58112 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile )((void) (0));
58113 break;
58114
58115 case PAGER_READER1:
58116 assert( pPager->errCode==SQLITE_OK )((void) (0));
58117 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
58118 assert( p->eLock>=SHARED_LOCK )((void) (0));
58119 break;
58120
58121 case PAGER_WRITER_LOCKED2:
58122 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
58123 assert( pPager->errCode==SQLITE_OK )((void) (0));
58124 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
58125 assert( p->eLock>=RESERVED_LOCK )((void) (0));
58126 }
58127 assert( pPager->dbSize==pPager->dbOrigSize )((void) (0));
58128 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
58129 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
58130 assert( pPager->setSuper==0 )((void) (0));
58131 break;
58132
58133 case PAGER_WRITER_CACHEMOD3:
58134 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
58135 assert( pPager->errCode==SQLITE_OK )((void) (0));
58136 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
58137 /* It is possible that if journal_mode=wal here that neither the
58138 ** journal file nor the WAL file are open. This happens during
58139 ** a rollback transaction that switches from journal_mode=off
58140 ** to journal_mode=wal.
58141 */
58142 assert( p->eLock>=RESERVED_LOCK )((void) (0));
58143 assert( isOpen(p->jfd)((void) (0))
58144 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
58145 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
58146 )((void) (0));
58147 }
58148 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
58149 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
58150 break;
58151
58152 case PAGER_WRITER_DBMOD4:
58153 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
58154 assert( pPager->errCode==SQLITE_OK )((void) (0));
58155 assert( !pagerUseWal(pPager) )((void) (0));
58156 assert( p->eLock>=EXCLUSIVE_LOCK )((void) (0));
58157 assert( isOpen(p->jfd)((void) (0))
58158 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
58159 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
58160 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
58161 )((void) (0));
58162 assert( pPager->dbOrigSize<=pPager->dbHintSize )((void) (0));
58163 break;
58164
58165 case PAGER_WRITER_FINISHED5:
58166 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
58167 assert( pPager->errCode==SQLITE_OK )((void) (0));
58168 assert( !pagerUseWal(pPager) )((void) (0));
58169 assert( isOpen(p->jfd)((void) (0))
58170 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
58171 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
58172 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
58173 )((void) (0));
58174 break;
58175
58176 case PAGER_ERROR6:
58177 /* There must be at least one outstanding reference to the pager if
58178 ** in ERROR state. Otherwise the pager should have already dropped
58179 ** back to OPEN state.
58180 */
58181 assert( pPager->errCode!=SQLITE_OK )((void) (0));
58182 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile )((void) (0));
58183 break;
58184 }
58185
58186 return 1;
58187}
58188#endif /* ifndef NDEBUG */
58189
58190#ifdef SQLITE_DEBUG
58191/*
58192** Return a pointer to a human readable string in a static buffer
58193** containing the state of the Pager object passed as an argument. This
58194** is intended to be used within debuggers. For example, as an alternative
58195** to "print *pPager" in gdb:
58196**
58197** (gdb) printf "%s", print_pager_state(pPager)
58198**
58199** This routine has external linkage in order to suppress compiler warnings
58200** about an unused function. It is enclosed within SQLITE_DEBUG and so does
58201** not appear in normal builds.
58202*/
58203char *print_pager_state(Pager *p){
58204 static char zRet[1024];
58205
58206 sqlite3_snprintf(1024, zRet,
58207 "Filename: %s\n"
58208 "State: %s errCode=%d\n"
58209 "Lock: %s\n"
58210 "Locking mode: locking_mode=%s\n"
58211 "Journal mode: journal_mode=%s\n"
58212 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
58213 "Journal: journalOff=%lld journalHdr=%lld\n"
58214 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
58215 , p->zFilename
58216 , p->eState==PAGER_OPEN0 ? "OPEN" :
58217 p->eState==PAGER_READER1 ? "READER" :
58218 p->eState==PAGER_WRITER_LOCKED2 ? "WRITER_LOCKED" :
58219 p->eState==PAGER_WRITER_CACHEMOD3 ? "WRITER_CACHEMOD" :
58220 p->eState==PAGER_WRITER_DBMOD4 ? "WRITER_DBMOD" :
58221 p->eState==PAGER_WRITER_FINISHED5 ? "WRITER_FINISHED" :
58222 p->eState==PAGER_ERROR6 ? "ERROR" : "?error?"
58223 , (int)p->errCode
58224 , p->eLock==NO_LOCK0 ? "NO_LOCK" :
58225 p->eLock==RESERVED_LOCK2 ? "RESERVED" :
58226 p->eLock==EXCLUSIVE_LOCK4 ? "EXCLUSIVE" :
58227 p->eLock==SHARED_LOCK1 ? "SHARED" :
58228 p->eLock==UNKNOWN_LOCK(4 +1) ? "UNKNOWN" : "?error?"
58229 , p->exclusiveMode ? "exclusive" : "normal"
58230 , p->journalMode==PAGER_JOURNALMODE_MEMORY4 ? "memory" :
58231 p->journalMode==PAGER_JOURNALMODE_OFF2 ? "off" :
58232 p->journalMode==PAGER_JOURNALMODE_DELETE0 ? "delete" :
58233 p->journalMode==PAGER_JOURNALMODE_PERSIST1 ? "persist" :
58234 p->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ? "truncate" :
58235 p->journalMode==PAGER_JOURNALMODE_WAL5 ? "wal" : "?error?"
58236 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
58237 , p->journalOff, p->journalHdr
58238 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
58239 );
58240
58241 return zRet;
58242}
58243#endif
58244
58245/* Forward references to the various page getters */
58246static int getPageNormal(Pager*,Pgno,DbPage**,int);
58247static int getPageError(Pager*,Pgno,DbPage**,int);
58248#if SQLITE_MAX_MMAP_SIZE20971520>0
58249static int getPageMMap(Pager*,Pgno,DbPage**,int);
58250#endif
58251
58252/*
58253** Set the Pager.xGet method for the appropriate routine used to fetch
58254** content from the pager.
58255*/
58256static void setGetterMethod(Pager *pPager){
58257 if( pPager->errCode ){
58258 pPager->xGet = getPageError;
58259#if SQLITE_MAX_MMAP_SIZE20971520>0
58260 }else if( USEFETCH(pPager)((pPager)->bUseFetch) ){
58261 pPager->xGet = getPageMMap;
58262#endif /* SQLITE_MAX_MMAP_SIZE>0 */
58263 }else{
58264 pPager->xGet = getPageNormal;
58265 }
58266}
58267
58268/*
58269** Return true if it is necessary to write page *pPg into the sub-journal.
58270** A page needs to be written into the sub-journal if there exists one
58271** or more open savepoints for which:
58272**
58273** * The page-number is less than or equal to PagerSavepoint.nOrig, and
58274** * The bit corresponding to the page-number is not set in
58275** PagerSavepoint.pInSavepoint.
58276*/
58277static int subjRequiresPage(PgHdr *pPg){
58278 Pager *pPager = pPg->pPager;
58279 PagerSavepoint *p;
58280 Pgno pgno = pPg->pgno;
58281 int i;
58282 for(i=0; i<pPager->nSavepoint; i++){
58283 p = &pPager->aSavepoint[i];
58284 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
58285 for(i=i+1; i<pPager->nSavepoint; i++){
58286 pPager->aSavepoint[i].bTruncateOnRelease = 0;
58287 }
58288 return 1;
58289 }
58290 }
58291 return 0;
58292}
58293
58294#ifdef SQLITE_DEBUG
58295/*
58296** Return true if the page is already in the journal file.
58297*/
58298static int pageInJournal(Pager *pPager, PgHdr *pPg){
58299 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
58300}
58301#endif
58302
58303/*
58304** Read a 32-bit integer from the given file descriptor. Store the integer
58305** that is read in *pRes. Return SQLITE_OK if everything worked, or an
58306** error code is something goes wrong.
58307**
58308** All values are stored on disk as big-endian.
58309*/
58310static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
58311 unsigned char ac[4];
58312 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
58313 if( rc==SQLITE_OK0 ){
58314 *pRes = sqlite3Get4byte(ac);
58315 }
58316 return rc;
58317}
58318
58319/*
58320** Write a 32-bit integer into a string buffer in big-endian byte order.
58321*/
58322#define put32bits(A,B)sqlite3Put4byte((u8*)A,B) sqlite3Put4byte((u8*)A,B)
58323
58324
58325/*
58326** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
58327** on success or an error code is something goes wrong.
58328*/
58329static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
58330 char ac[4];
58331 put32bits(ac, val)sqlite3Put4byte((u8*)ac,val);
58332 return sqlite3OsWrite(fd, ac, 4, offset);
58333}
58334
58335/*
58336** Unlock the database file to level eLock, which must be either NO_LOCK
58337** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
58338** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
58339**
58340** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
58341** called, do not modify it. See the comment above the #define of
58342** UNKNOWN_LOCK for an explanation of this.
58343*/
58344static int pagerUnlockDb(Pager *pPager, int eLock){
58345 int rc = SQLITE_OK0;
58346
58347 assert( !pPager->exclusiveMode || pPager->eLock==eLock )((void) (0));
58348 assert( eLock==NO_LOCK || eLock==SHARED_LOCK )((void) (0));
58349 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 )((void) (0));
58350 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
58351 assert( pPager->eLock>=eLock )((void) (0));
58352 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsUnlock(pPager->fd, eLock);
58353 if( pPager->eLock!=UNKNOWN_LOCK(4 +1) ){
58354 pPager->eLock = (u8)eLock;
58355 }
58356 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
58357 }
58358 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
58359 return rc;
58360}
58361
58362/*
58363** Lock the database file to level eLock, which must be either SHARED_LOCK,
58364** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
58365** Pager.eLock variable to the new locking state.
58366**
58367** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
58368** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
58369** See the comment above the #define of UNKNOWN_LOCK for an explanation
58370** of this.
58371*/
58372static int pagerLockDb(Pager *pPager, int eLock){
58373 int rc = SQLITE_OK0;
58374
58375 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK )((void) (0));
58376 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK(4 +1) ){
58377 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsLock(pPager->fd, eLock);
58378 if( rc==SQLITE_OK0 && (pPager->eLock!=UNKNOWN_LOCK(4 +1)||eLock==EXCLUSIVE_LOCK4) ){
58379 pPager->eLock = (u8)eLock;
58380 IOTRACE(("LOCK %p %d\n", pPager, eLock))
58381 }
58382 }
58383 return rc;
58384}
58385
58386/*
58387** This function determines whether or not the atomic-write or
58388** atomic-batch-write optimizations can be used with this pager. The
58389** atomic-write optimization can be used if:
58390**
58391** (a) the value returned by OsDeviceCharacteristics() indicates that
58392** a database page may be written atomically, and
58393** (b) the value returned by OsSectorSize() is less than or equal
58394** to the page size.
58395**
58396** If it can be used, then the value returned is the size of the journal
58397** file when it contains rollback data for exactly one page.
58398**
58399** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
58400** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
58401** returned in this case.
58402**
58403** If neither optimization can be used, 0 is returned.
58404*/
58405static int jrnlBufferSize(Pager *pPager){
58406 assert( !MEMDB )((void) (0));
58407
58408#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
58409 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
58410 int dc; /* Device characteristics */
58411
58412 assert( isOpen(pPager->fd) )((void) (0));
58413 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
58414#else
58415 UNUSED_PARAMETER(pPager)(void)(pPager);
58416#endif
58417
58418#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
58419 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC0x00004000) ){
58420 return -1;
58421 }
58422#endif
58423
58424#ifdef SQLITE_ENABLE_ATOMIC_WRITE
58425 {
58426 int nSector = pPager->sectorSize;
58427 int szPage = pPager->pageSize;
58428
58429 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
58430 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
58431 if( 0==(dc&(SQLITE_IOCAP_ATOMIC0x00000001|(szPage>>8)) || nSector>szPage) ){
58432 return 0;
58433 }
58434 }
58435
58436 return JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8);
58437#endif
58438
58439 return 0;
58440}
58441
58442/*
58443** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
58444** on the cache using a hash function. This is used for testing
58445** and debugging only.
58446*/
58447#ifdef SQLITE_CHECK_PAGES
58448/*
58449** Return a 32-bit hash of the page data for pPage.
58450*/
58451static u32 pager_datahash(int nByte, unsigned char *pData)0{
58452 u32 hash = 0;
58453 int i;
58454 for(i=0; i<nByte; i++){
58455 hash = (hash*1039) + pData[i];
58456 }
58457 return hash;
58458}
58459static u32 pager_pagehash(PgHdr *pPage)0{
58460 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData)0;
58461}
58462static void pager_set_pagehash(PgHdr *pPage){
58463 pPage->pageHash = pager_pagehash(pPage)0;
58464}
58465
58466/*
58467** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
58468** is defined, and NDEBUG is not defined, an assert() statement checks
58469** that the page is either dirty or still matches the calculated page-hash.
58470*/
58471#define CHECK_PAGE(x) checkPage(x)
58472static void checkPage(PgHdr *pPg){
58473 Pager *pPager = pPg->pPager;
58474 assert( pPager->eState!=PAGER_ERROR )((void) (0));
58475 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) )((void) (0));
58476}
58477
58478#else
58479#define pager_datahash(X,Y)0 0
58480#define pager_pagehash(X)0 0
58481#define pager_set_pagehash(X)
58482#define CHECK_PAGE(x)
58483#endif /* SQLITE_CHECK_PAGES */
58484
58485/*
58486** When this is called the journal file for pager pPager must be open.
58487** This function attempts to read a super-journal file name from the
58488** end of the file and, if successful, copies it into memory supplied
58489** by the caller. See comments above writeSuperJournal() for the format
58490** used to store a super-journal file name at the end of a journal file.
58491**
58492** zSuper must point to a buffer of at least nSuper bytes allocated by
58493** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
58494** enough space to write the super-journal name). If the super-journal
58495** name in the journal is longer than nSuper bytes (including a
58496** nul-terminator), then this is handled as if no super-journal name
58497** were present in the journal.
58498**
58499** If a super-journal file name is present at the end of the journal
58500** file, then it is copied into the buffer pointed to by zSuper. A
58501** nul-terminator byte is appended to the buffer following the
58502** super-journal file name.
58503**
58504** If it is determined that no super-journal file name is present
58505** zSuper[0] is set to 0 and SQLITE_OK returned.
58506**
58507** If an error occurs while reading from the journal file, an SQLite
58508** error code is returned.
58509*/
58510static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
58511 int rc; /* Return code */
58512 u32 len; /* Length in bytes of super-journal name */
58513 i64 szJ; /* Total size in bytes of journal file pJrnl */
58514 u32 cksum; /* MJ checksum value read from journal */
58515 u32 u; /* Unsigned loop counter */
58516 unsigned char aMagic[8]; /* A buffer to hold the magic header */
58517 zSuper[0] = '\0';
58518
58519 if( SQLITE_OK0!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
58520 || szJ<16
58521 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-16, &len))
58522 || len>=nSuper
58523 || len>szJ-16
58524 || len==0
58525 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-12, &cksum))
58526 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
58527 || memcmp(aMagic, aJournalMagic, 8)
58528 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
58529 ){
58530 return rc;
58531 }
58532
58533 /* See if the checksum matches the super-journal name */
58534 for(u=0; u<len; u++){
58535 cksum -= zSuper[u];
58536 }
58537 if( cksum ){
58538 /* If the checksum doesn't add up, then one or more of the disk sectors
58539 ** containing the super-journal filename is corrupted. This means
58540 ** definitely roll back, so just return SQLITE_OK and report a (nul)
58541 ** super-journal filename.
58542 */
58543 len = 0;
58544 }
58545 zSuper[len] = '\0';
58546 zSuper[len+1] = '\0';
58547
58548 return SQLITE_OK0;
58549}
58550
58551/*
58552** Return the offset of the sector boundary at or immediately
58553** following the value in pPager->journalOff, assuming a sector
58554** size of pPager->sectorSize bytes.
58555**
58556** i.e for a sector size of 512:
58557**
58558** Pager.journalOff Return value
58559** ---------------------------------------
58560** 0 0
58561** 512 512
58562** 100 512
58563** 2000 2048
58564**
58565*/
58566static i64 journalHdrOffset(Pager *pPager){
58567 i64 offset = 0;
58568 i64 c = pPager->journalOff;
58569 if( c ){
58570 offset = ((c-1)/JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + 1) * JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
58571 }
58572 assert( offset%JOURNAL_HDR_SZ(pPager)==0 )((void) (0));
58573 assert( offset>=c )((void) (0));
58574 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) )((void) (0));
58575 return offset;
58576}
58577
58578/*
58579** The journal file must be open when this function is called.
58580**
58581** This function is a no-op if the journal file has not been written to
58582** within the current transaction (i.e. if Pager.journalOff==0).
58583**
58584** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
58585** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
58586** zero the 28-byte header at the start of the journal file. In either case,
58587** if the pager is not in no-sync mode, sync the journal file immediately
58588** after writing or truncating it.
58589**
58590** If Pager.journalSizeLimit is set to a positive, non-zero value, and
58591** following the truncation or zeroing described above the size of the
58592** journal file in bytes is larger than this value, then truncate the
58593** journal file to Pager.journalSizeLimit bytes. The journal file does
58594** not need to be synced following this operation.
58595**
58596** If an IO error occurs, abandon processing and return the IO error code.
58597** Otherwise, return SQLITE_OK.
58598*/
58599static int zeroJournalHdr(Pager *pPager, int doTruncate){
58600 int rc = SQLITE_OK0; /* Return code */
58601 assert( isOpen(pPager->jfd) )((void) (0));
58602 assert( !sqlite3JournalIsInMemory(pPager->jfd) )((void) (0));
58603 if( pPager->journalOff ){
58604 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
58605
58606 IOTRACE(("JZEROHDR %p\n", pPager))
58607 if( doTruncate || iLimit==0 ){
58608 rc = sqlite3OsTruncate(pPager->jfd, 0);
58609 }else{
58610 static const char zeroHdr[28] = {0};
58611 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
58612 }
58613 if( rc==SQLITE_OK0 && !pPager->noSync ){
58614 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY0x00010|pPager->syncFlags);
58615 }
58616
58617 /* At this point the transaction is committed but the write lock
58618 ** is still held on the file. If there is a size limit configured for
58619 ** the persistent journal and the journal file currently consumes more
58620 ** space than that limit allows for, truncate it now. There is no need
58621 ** to sync the file following this operation.
58622 */
58623 if( rc==SQLITE_OK0 && iLimit>0 ){
58624 i64 sz;
58625 rc = sqlite3OsFileSize(pPager->jfd, &sz);
58626 if( rc==SQLITE_OK0 && sz>iLimit ){
58627 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
58628 }
58629 }
58630 }
58631 return rc;
58632}
58633
58634/*
58635** The journal file must be open when this routine is called. A journal
58636** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
58637** current location.
58638**
58639** The format for the journal header is as follows:
58640** - 8 bytes: Magic identifying journal format.
58641** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
58642** - 4 bytes: Random number used for page hash.
58643** - 4 bytes: Initial database page count.
58644** - 4 bytes: Sector size used by the process that wrote this journal.
58645** - 4 bytes: Database page size.
58646**
58647** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
58648*/
58649static int writeJournalHdr(Pager *pPager){
58650 int rc = SQLITE_OK0; /* Return code */
58651 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
58652 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
58653 u32 nWrite; /* Bytes of header sector written */
58654 int ii; /* Loop counter */
58655
58656 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
58657
58658 if( nHeader>JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
58659 nHeader = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
58660 }
58661
58662 /* If there are active savepoints and any of them were created
58663 ** since the most recent journal header was written, update the
58664 ** PagerSavepoint.iHdrOffset fields now.
58665 */
58666 for(ii=0; ii<pPager->nSavepoint; ii++){
58667 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
58668 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
58669 }
58670 }
58671
58672 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
58673
58674 /*
58675 ** Write the nRec Field - the number of page records that follow this
58676 ** journal header. Normally, zero is written to this value at this time.
58677 ** After the records are added to the journal (and the journal synced,
58678 ** if in full-sync mode), the zero is overwritten with the true number
58679 ** of records (see syncJournal()).
58680 **
58681 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
58682 ** reading the journal this value tells SQLite to assume that the
58683 ** rest of the journal file contains valid page records. This assumption
58684 ** is dangerous, as if a failure occurred whilst writing to the journal
58685 ** file it may contain some garbage data. There are two scenarios
58686 ** where this risk can be ignored:
58687 **
58688 ** * When the pager is in no-sync mode. Corruption can follow a
58689 ** power failure in this case anyway.
58690 **
58691 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
58692 ** that garbage data is never appended to the journal file.
58693 */
58694 assert( isOpen(pPager->fd) || pPager->noSync )((void) (0));
58695 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY4)
58696 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND0x00000200)
58697 ){
58698 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
58699 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],0xffffffff
)
;
58700 }else{
58701 memset(zHeader, 0, sizeof(aJournalMagic)+4);
58702 }
58703
58704
58705
58706 /* The random check-hash initializer */
58707 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY4 ){
58708 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
58709 }
58710#ifdef SQLITE_DEBUG
58711 else{
58712 /* The Pager.cksumInit variable is usually randomized above to protect
58713 ** against there being existing records in the journal file. This is
58714 ** dangerous, as following a crash they may be mistaken for records
58715 ** written by the current transaction and rolled back into the database
58716 ** file, causing corruption. The following assert statements verify
58717 ** that this is not required in "journal_mode=memory" mode, as in that
58718 ** case the journal file is always 0 bytes in size at this point.
58719 ** It is advantageous to avoid the sqlite3_randomness() call if possible
58720 ** as it takes the global PRNG mutex. */
58721 i64 sz = 0;
58722 sqlite3OsFileSize(pPager->jfd, &sz);
58723 assert( sz==0 )((void) (0));
58724 assert( pPager->journalOff==journalHdrOffset(pPager) )((void) (0));
58725 assert( sqlite3JournalIsInMemory(pPager->jfd) )((void) (0));
58726 }
58727#endif
58728 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+4],pPager
->cksumInit)
;
58729
58730 /* The initial database size */
58731 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+8],pPager
->dbOrigSize)
;
58732 /* The assumed sector size for this process */
58733 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+12],pPager
->sectorSize)
;
58734
58735 /* The page size */
58736 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+16],pPager
->pageSize)
;
58737
58738 /* Initializing the tail of the buffer is not necessary. Everything
58739 ** works find if the following memset() is omitted. But initializing
58740 ** the memory prevents valgrind from complaining, so we are willing to
58741 ** take the performance hit.
58742 */
58743 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
58744 nHeader-(sizeof(aJournalMagic)+20));
58745
58746 /* In theory, it is only necessary to write the 28 bytes that the
58747 ** journal header consumes to the journal file here. Then increment the
58748 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
58749 ** record is written to the following sector (leaving a gap in the file
58750 ** that will be implicitly filled in by the OS).
58751 **
58752 ** However it has been discovered that on some systems this pattern can
58753 ** be significantly slower than contiguously writing data to the file,
58754 ** even if that means explicitly writing data to the block of
58755 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
58756 ** is done.
58757 **
58758 ** The loop is required here in case the sector-size is larger than the
58759 ** database page size. Since the zHeader buffer is only Pager.pageSize
58760 ** bytes in size, more than one call to sqlite3OsWrite() may be required
58761 ** to populate the entire journal header sector.
58762 */
58763 for(nWrite=0; rc==SQLITE_OK0&&nWrite<JOURNAL_HDR_SZ(pPager)(pPager->sectorSize); nWrite+=nHeader){
58764 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
58765 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
58766 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
58767 pPager->journalOff += nHeader;
58768 }
58769
58770 return rc;
58771}
58772
58773/*
58774** The journal file must be open when this is called. A journal header file
58775** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
58776** file. The current location in the journal file is given by
58777** pPager->journalOff. See comments above function writeJournalHdr() for
58778** a description of the journal header format.
58779**
58780** If the header is read successfully, *pNRec is set to the number of
58781** page records following this header and *pDbSize is set to the size of the
58782** database before the transaction began, in pages. Also, pPager->cksumInit
58783** is set to the value read from the journal header. SQLITE_OK is returned
58784** in this case.
58785**
58786** If the journal header file appears to be corrupted, SQLITE_DONE is
58787** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
58788** cannot be read from the journal file an error code is returned.
58789*/
58790static int readJournalHdr(
58791 Pager *pPager, /* Pager object */
58792 int isHot,
58793 i64 journalSize, /* Size of the open journal file in bytes */
58794 u32 *pNRec, /* OUT: Value read from the nRec field */
58795 u32 *pDbSize /* OUT: Value of original database size field */
58796){
58797 int rc; /* Return code */
58798 unsigned char aMagic[8]; /* A buffer to hold the magic header */
58799 i64 iHdrOff; /* Offset of journal header being read */
58800
58801 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
58802
58803 /* Advance Pager.journalOff to the start of the next sector. If the
58804 ** journal file is too small for there to be a header stored at this
58805 ** point, return SQLITE_DONE.
58806 */
58807 pPager->journalOff = journalHdrOffset(pPager);
58808 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) > journalSize ){
58809 return SQLITE_DONE101;
58810 }
58811 iHdrOff = pPager->journalOff;
58812
58813 /* Read in the first 8 bytes of the journal header. If they do not match
58814 ** the magic string found at the start of each journal header, return
58815 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
58816 ** proceed.
58817 */
58818 if( isHot || iHdrOff!=pPager->journalHdr ){
58819 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
58820 if( rc ){
58821 return rc;
58822 }
58823 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
58824 return SQLITE_DONE101;
58825 }
58826 }
58827
58828 /* Read the first three 32-bit fields of the journal header: The nRec
58829 ** field, the checksum-initializer and the database size at the start
58830 ** of the transaction. Return an error code if anything goes wrong.
58831 */
58832 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
58833 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
58834 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
58835 ){
58836 return rc;
58837 }
58838
58839 if( pPager->journalOff==0 ){
58840 u32 iPageSize; /* Page-size field of journal header */
58841 u32 iSectorSize; /* Sector-size field of journal header */
58842
58843 /* Read the page-size and sector-size journal header fields. */
58844 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
58845 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
58846 ){
58847 return rc;
58848 }
58849
58850 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
58851 ** journal header to zero. In this case, assume that the Pager.pageSize
58852 ** variable is already set to the correct page size.
58853 */
58854 if( iPageSize==0 ){
58855 iPageSize = pPager->pageSize;
58856 }
58857
58858 /* Check that the values read from the page-size and sector-size fields
58859 ** are within range. To be 'in range', both values need to be a power
58860 ** of two greater than or equal to 512 or 32, and not greater than their
58861 ** respective compile time maximum limits.
58862 */
58863 if( iPageSize<512 || iSectorSize<32
58864 || iPageSize>SQLITE_MAX_PAGE_SIZE65536 || iSectorSize>MAX_SECTOR_SIZE0x10000
58865 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
58866 ){
58867 /* If the either the page-size or sector-size in the journal-header is
58868 ** invalid, then the process that wrote the journal-header must have
58869 ** crashed before the header was synced. In this case stop reading
58870 ** the journal file here.
58871 */
58872 return SQLITE_DONE101;
58873 }
58874
58875 /* Update the page-size to match the value read from the journal.
58876 ** Use a testcase() macro to make sure that malloc failure within
58877 ** PagerSetPagesize() is tested.
58878 */
58879 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
58880 testcase( rc!=SQLITE_OK );
58881
58882 /* Update the assumed sector-size to match the value used by
58883 ** the process that created this journal. If this journal was
58884 ** created by a process other than this one, then this routine
58885 ** is being called from within pager_playback(). The local value
58886 ** of Pager.sectorSize is restored at the end of that routine.
58887 */
58888 pPager->sectorSize = iSectorSize;
58889 }
58890
58891 pPager->journalOff += JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
58892 return rc;
58893}
58894
58895
58896/*
58897** Write the supplied super-journal name into the journal file for pager
58898** pPager at the current location. The super-journal name must be the last
58899** thing written to a journal file. If the pager is in full-sync mode, the
58900** journal file descriptor is advanced to the next sector boundary before
58901** anything is written. The format is:
58902**
58903** + 4 bytes: PAGER_SJ_PGNO.
58904** + N bytes: super-journal filename in utf-8.
58905** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
58906** + 4 bytes: super-journal name checksum.
58907** + 8 bytes: aJournalMagic[].
58908**
58909** The super-journal page checksum is the sum of the bytes in the super-journal
58910** name, where each byte is interpreted as a signed 8-bit integer.
58911**
58912** If zSuper is a NULL pointer (occurs for a single database transaction),
58913** this call is a no-op.
58914*/
58915static int writeSuperJournal(Pager *pPager, const char *zSuper){
58916 int rc; /* Return code */
58917 int nSuper; /* Length of string zSuper */
58918 i64 iHdrOff; /* Offset of header in journal file */
58919 i64 jrnlSize; /* Size of journal file on disk */
58920 u32 cksum = 0; /* Checksum of string zSuper */
58921
58922 assert( pPager->setSuper==0 )((void) (0));
58923 assert( !pagerUseWal(pPager) )((void) (0));
58924
58925 if( !zSuper
58926 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY4
58927 || !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
58928 ){
58929 return SQLITE_OK0;
58930 }
58931 pPager->setSuper = 1;
58932 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
58933
58934 /* Calculate the length in bytes and the checksum of zSuper */
58935 for(nSuper=0; zSuper[nSuper]; nSuper++){
58936 cksum += zSuper[nSuper];
58937 }
58938
58939 /* If in full-sync mode, advance to the next disk sector before writing
58940 ** the super-journal name. This is in case the previous page written to
58941 ** the journal has already been synced.
58942 */
58943 if( pPager->fullSync ){
58944 pPager->journalOff = journalHdrOffset(pPager);
58945 }
58946 iHdrOff = pPager->journalOff;
58947
58948 /* Write the super-journal data to the end of the journal file. If
58949 ** an error occurs, return the error code to the caller.
58950 */
58951 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager)((pPager)->lckPgno))))
58952 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
58953 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
58954 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
58955 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
58956 iHdrOff+4+nSuper+8)))
58957 ){
58958 return rc;
58959 }
58960 pPager->journalOff += (nSuper+20);
58961
58962 /* If the pager is in persistent-journal mode, then the physical
58963 ** journal-file may extend past the end of the super-journal name
58964 ** and 8 bytes of magic data just written to the file. This is
58965 ** dangerous because the code to rollback a hot-journal file
58966 ** will not be able to find the super-journal name to determine
58967 ** whether or not the journal is hot.
58968 **
58969 ** Easiest thing to do in this scenario is to truncate the journal
58970 ** file to the required size.
58971 */
58972 if( SQLITE_OK0==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
58973 && jrnlSize>pPager->journalOff
58974 ){
58975 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
58976 }
58977 return rc;
58978}
58979
58980/*
58981** Discard the entire contents of the in-memory page-cache.
58982*/
58983static void pager_reset(Pager *pPager){
58984 pPager->iDataVersion++;
58985 sqlite3BackupRestart(pPager->pBackup);
58986 sqlite3PcacheClear(pPager->pPCache);
58987}
58988
58989/*
58990** Return the pPager->iDataVersion value
58991*/
58992SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager *pPager){
58993 return pPager->iDataVersion;
58994}
58995
58996/*
58997** Free all structures in the Pager.aSavepoint[] array and set both
58998** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
58999** if it is open and the pager is not in exclusive mode.
59000*/
59001static void releaseAllSavepoints(Pager *pPager){
59002 int ii; /* Iterator for looping through Pager.aSavepoint */
59003 for(ii=0; ii<pPager->nSavepoint; ii++){
59004 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
59005 }
59006 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
59007 sqlite3OsClose(pPager->sjfd);
59008 }
59009 sqlite3_free(pPager->aSavepoint);
59010 pPager->aSavepoint = 0;
59011 pPager->nSavepoint = 0;
59012 pPager->nSubRec = 0;
59013}
59014
59015/*
59016** Set the bit number pgno in the PagerSavepoint.pInSavepoint
59017** bitvecs of all open savepoints. Return SQLITE_OK if successful
59018** or SQLITE_NOMEM if a malloc failure occurs.
59019*/
59020static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
59021 int ii; /* Loop counter */
59022 int rc = SQLITE_OK0; /* Result code */
59023
59024 for(ii=0; ii<pPager->nSavepoint; ii++){
59025 PagerSavepoint *p = &pPager->aSavepoint[ii];
59026 if( pgno<=p->nOrig ){
59027 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
59028 testcase( rc==SQLITE_NOMEM );
59029 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
59030 }
59031 }
59032 return rc;
59033}
59034
59035/*
59036** This function is a no-op if the pager is in exclusive mode and not
59037** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
59038** state.
59039**
59040** If the pager is not in exclusive-access mode, the database file is
59041** completely unlocked. If the file is unlocked and the file-system does
59042** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
59043** closed (if it is open).
59044**
59045** If the pager is in ERROR state when this function is called, the
59046** contents of the pager cache are discarded before switching back to
59047** the OPEN state. Regardless of whether the pager is in exclusive-mode
59048** or not, any journal file left in the file-system will be treated
59049** as a hot-journal and rolled back the next time a read-transaction
59050** is opened (by this or by any other connection).
59051*/
59052static void pager_unlock(Pager *pPager){
59053
59054 assert( pPager->eState==PAGER_READER((void) (0))
59055 || pPager->eState==PAGER_OPEN((void) (0))
59056 || pPager->eState==PAGER_ERROR((void) (0))
59057 )((void) (0));
59058
59059 sqlite3BitvecDestroy(pPager->pInJournal);
59060 pPager->pInJournal = 0;
59061 releaseAllSavepoints(pPager);
59062
59063 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
59064 assert( !isOpen(pPager->jfd) )((void) (0));
59065 sqlite3WalEndReadTransaction(pPager->pWal);
59066 pPager->eState = PAGER_OPEN0;
59067 }else if( !pPager->exclusiveMode ){
59068 int rc; /* Error code returned by pagerUnlockDb() */
59069 int iDc = isOpen(pPager->fd)((pPager->fd)->pMethods!=0)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
59070
59071 /* If the operating system support deletion of open files, then
59072 ** close the journal file when dropping the database lock. Otherwise
59073 ** another connection with journal_mode=delete might delete the file
59074 ** out from under us.
59075 */
59076 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 )((void) (0));
59077 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 )((void) (0));
59078 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 )((void) (0));
59079 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 )((void) (0));
59080 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
59081 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
59082 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800)
59083 || 1!=(pPager->journalMode & 5)
59084 ){
59085 sqlite3OsClose(pPager->jfd);
59086 }
59087
59088 /* If the pager is in the ERROR state and the call to unlock the database
59089 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
59090 ** above the #define for UNKNOWN_LOCK for an explanation of why this
59091 ** is necessary.
59092 */
59093 rc = pagerUnlockDb(pPager, NO_LOCK0);
59094 if( rc!=SQLITE_OK0 && pPager->eState==PAGER_ERROR6 ){
59095 pPager->eLock = UNKNOWN_LOCK(4 +1);
59096 }
59097
59098 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
59099 ** without clearing the error code. This is intentional - the error
59100 ** code is cleared and the cache reset in the block below.
59101 */
59102 assert( pPager->errCode || pPager->eState!=PAGER_ERROR )((void) (0));
59103 pPager->eState = PAGER_OPEN0;
59104 }
59105
59106 /* If Pager.errCode is set, the contents of the pager cache cannot be
59107 ** trusted. Now that there are no outstanding references to the pager,
59108 ** it can safely move back to PAGER_OPEN state. This happens in both
59109 ** normal and exclusive-locking mode.
59110 */
59111 assert( pPager->errCode==SQLITE_OK || !MEMDB )((void) (0));
59112 if( pPager->errCode ){
59113 if( pPager->tempFile==0 ){
59114 pager_reset(pPager);
59115 pPager->changeCountDone = 0;
59116 pPager->eState = PAGER_OPEN0;
59117 }else{
59118 pPager->eState = (isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ? PAGER_OPEN0 : PAGER_READER1);
59119 }
59120 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
59121 pPager->errCode = SQLITE_OK0;
59122 setGetterMethod(pPager);
59123 }
59124
59125 pPager->journalOff = 0;
59126 pPager->journalHdr = 0;
59127 pPager->setSuper = 0;
59128}
59129
59130/*
59131** This function is called whenever an IOERR or FULL error that requires
59132** the pager to transition into the ERROR state may have occurred.
59133** The first argument is a pointer to the pager structure, the second
59134** the error-code about to be returned by a pager API function. The
59135** value returned is a copy of the second argument to this function.
59136**
59137** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
59138** IOERR sub-codes, the pager enters the ERROR state and the error code
59139** is stored in Pager.errCode. While the pager remains in the ERROR state,
59140** all major API calls on the Pager will immediately return Pager.errCode.
59141**
59142** The ERROR state indicates that the contents of the pager-cache
59143** cannot be trusted. This state can be cleared by completely discarding
59144** the contents of the pager-cache. If a transaction was active when
59145** the persistent error occurred, then the rollback journal may need
59146** to be replayed to restore the contents of the database file (as if
59147** it were a hot-journal).
59148*/
59149static int pager_error(Pager *pPager, int rc){
59150 int rc2 = rc & 0xff;
59151 assert( rc==SQLITE_OK || !MEMDB )((void) (0));
59152 assert(((void) (0))
59153 pPager->errCode==SQLITE_FULL ||((void) (0))
59154 pPager->errCode==SQLITE_OK ||((void) (0))
59155 (pPager->errCode & 0xff)==SQLITE_IOERR((void) (0))
59156 )((void) (0));
59157 if( rc2==SQLITE_FULL13 || rc2==SQLITE_IOERR10 ){
59158 pPager->errCode = rc;
59159 pPager->eState = PAGER_ERROR6;
59160 setGetterMethod(pPager);
59161 }
59162 return rc;
59163}
59164
59165static int pager_truncate(Pager *pPager, Pgno nPage);
59166
59167/*
59168** The write transaction open on pPager is being committed (bCommit==1)
59169** or rolled back (bCommit==0).
59170**
59171** Return TRUE if and only if all dirty pages should be flushed to disk.
59172**
59173** Rules:
59174**
59175** * For non-TEMP databases, always sync to disk. This is necessary
59176** for transactions to be durable.
59177**
59178** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
59179** file has been created already (via a spill on pagerStress()) and
59180** when the number of dirty pages in memory exceeds 25% of the total
59181** cache size.
59182*/
59183static int pagerFlushOnCommit(Pager *pPager, int bCommit){
59184 if( pPager->tempFile==0 ) return 1;
59185 if( !bCommit ) return 0;
59186 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ) return 0;
59187 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
59188}
59189
59190/*
59191** This routine ends a transaction. A transaction is usually ended by
59192** either a COMMIT or a ROLLBACK operation. This routine may be called
59193** after rollback of a hot-journal, or if an error occurs while opening
59194** the journal file or writing the very first journal-header of a
59195** database transaction.
59196**
59197** This routine is never called in PAGER_ERROR state. If it is called
59198** in PAGER_NONE or PAGER_SHARED state and the lock held is less
59199** exclusive than a RESERVED lock, it is a no-op.
59200**
59201** Otherwise, any active savepoints are released.
59202**
59203** If the journal file is open, then it is "finalized". Once a journal
59204** file has been finalized it is not possible to use it to roll back a
59205** transaction. Nor will it be considered to be a hot-journal by this
59206** or any other database connection. Exactly how a journal is finalized
59207** depends on whether or not the pager is running in exclusive mode and
59208** the current journal-mode (Pager.journalMode value), as follows:
59209**
59210** journalMode==MEMORY
59211** Journal file descriptor is simply closed. This destroys an
59212** in-memory journal.
59213**
59214** journalMode==TRUNCATE
59215** Journal file is truncated to zero bytes in size.
59216**
59217** journalMode==PERSIST
59218** The first 28 bytes of the journal file are zeroed. This invalidates
59219** the first journal header in the file, and hence the entire journal
59220** file. An invalid journal file cannot be rolled back.
59221**
59222** journalMode==DELETE
59223** The journal file is closed and deleted using sqlite3OsDelete().
59224**
59225** If the pager is running in exclusive mode, this method of finalizing
59226** the journal file is never used. Instead, if the journalMode is
59227** DELETE and the pager is in exclusive mode, the method described under
59228** journalMode==PERSIST is used instead.
59229**
59230** After the journal is finalized, the pager moves to PAGER_READER state.
59231** If running in non-exclusive rollback mode, the lock on the file is
59232** downgraded to a SHARED_LOCK.
59233**
59234** SQLITE_OK is returned if no error occurs. If an error occurs during
59235** any of the IO operations to finalize the journal file or unlock the
59236** database then the IO error code is returned to the user. If the
59237** operation to finalize the journal file fails, then the code still
59238** tries to unlock the database file if not in exclusive mode. If the
59239** unlock operation fails as well, then the first error code related
59240** to the first error encountered (the journal finalization one) is
59241** returned.
59242*/
59243static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
59244 int rc = SQLITE_OK0; /* Error code from journal finalization operation */
59245 int rc2 = SQLITE_OK0; /* Error code from db file unlock operation */
59246
59247 /* Do nothing if the pager does not have an open write transaction
59248 ** or at least a RESERVED lock. This function may be called when there
59249 ** is no write-transaction active but a RESERVED or greater lock is
59250 ** held under two circumstances:
59251 **
59252 ** 1. After a successful hot-journal rollback, it is called with
59253 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
59254 **
59255 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
59256 ** lock switches back to locking_mode=normal and then executes a
59257 ** read-transaction, this function is called with eState==PAGER_READER
59258 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
59259 */
59260 assert( assert_pager_state(pPager) )((void) (0));
59261 assert( pPager->eState!=PAGER_ERROR )((void) (0));
59262 if( pPager->eState<PAGER_WRITER_LOCKED2 && pPager->eLock<RESERVED_LOCK2 ){
59263 return SQLITE_OK0;
59264 }
59265
59266 releaseAllSavepoints(pPager);
59267 assert( isOpen(pPager->jfd) || pPager->pInJournal==0((void) (0))
59268 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
59269 )((void) (0));
59270 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
59271 assert( !pagerUseWal(pPager) )((void) (0));
59272
59273 /* Finalize the journal file. */
59274 if( sqlite3JournalIsInMemory(pPager->jfd) ){
59275 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
59276 sqlite3OsClose(pPager->jfd);
59277 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ){
59278 if( pPager->journalOff==0 ){
59279 rc = SQLITE_OK0;
59280 }else{
59281 rc = sqlite3OsTruncate(pPager->jfd, 0);
59282 if( rc==SQLITE_OK0 && pPager->fullSync ){
59283 /* Make sure the new file size is written into the inode right away.
59284 ** Otherwise the journal might resurrect following a power loss and
59285 ** cause the last transaction to roll back. See
59286 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
59287 */
59288 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
59289 }
59290 }
59291 pPager->journalOff = 0;
59292 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
59293 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL5)
59294 ){
59295 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
59296 pPager->journalOff = 0;
59297 }else{
59298 /* This branch may be executed with Pager.journalMode==MEMORY if
59299 ** a hot-journal was just rolled back. In this case the journal
59300 ** file should be closed and deleted. If this connection writes to
59301 ** the database file, it will do so using an in-memory journal.
59302 */
59303 int bDelete = !pPager->tempFile;
59304 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 )((void) (0));
59305 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE((void) (0))
59306 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
59307 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
59308 )((void) (0));
59309 sqlite3OsClose(pPager->jfd);
59310 if( bDelete ){
59311 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
59312 }
59313 }
59314 }
59315
59316#ifdef SQLITE_CHECK_PAGES
59317 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
59318 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
59319 PgHdr *p = sqlite3PagerLookup(pPager, 1);
59320 if( p ){
59321 p->pageHash = 0;
59322 sqlite3PagerUnrefNotNull(p);
59323 }
59324 }
59325#endif
59326
59327 sqlite3BitvecDestroy(pPager->pInJournal);
59328 pPager->pInJournal = 0;
59329 pPager->nRec = 0;
59330 if( rc==SQLITE_OK0 ){
59331 if( MEMDBpPager->memDb || pagerFlushOnCommit(pPager, bCommit) ){
59332 sqlite3PcacheCleanAll(pPager->pPCache);
59333 }else{
59334 sqlite3PcacheClearWritable(pPager->pPCache);
59335 }
59336 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
59337 }
59338
59339 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
59340 /* Drop the WAL write-lock, if any. Also, if the connection was in
59341 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
59342 ** lock held on the database file.
59343 */
59344 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
59345 assert( rc2==SQLITE_OK )((void) (0));
59346 }else if( rc==SQLITE_OK0 && bCommit && pPager->dbFileSize>pPager->dbSize ){
59347 /* This branch is taken when committing a transaction in rollback-journal
59348 ** mode if the database file on disk is larger than the database image.
59349 ** At this point the journal has been finalized and the transaction
59350 ** successfully committed, but the EXCLUSIVE lock is still held on the
59351 ** file. So it is safe to truncate the database file to its minimum
59352 ** required size. */
59353 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
59354 rc = pager_truncate(pPager, pPager->dbSize);
59355 }
59356
59357 if( rc==SQLITE_OK0 && bCommit ){
59358 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO22, 0);
59359 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
59360 }
59361
59362 if( !pPager->exclusiveMode
59363 && (!pagerUseWal(pPager)((pPager)->pWal!=0) || sqlite3WalExclusiveMode(pPager->pWal, 0))
59364 ){
59365 rc2 = pagerUnlockDb(pPager, SHARED_LOCK1);
59366 }
59367 pPager->eState = PAGER_READER1;
59368 pPager->setSuper = 0;
59369
59370 return (rc==SQLITE_OK0?rc2:rc);
59371}
59372
59373/* Forward reference */
59374static int pager_playback(Pager *pPager, int isHot);
59375
59376/*
59377** Execute a rollback if a transaction is active and unlock the
59378** database file.
59379**
59380** If the pager has already entered the ERROR state, do not attempt
59381** the rollback at this time. Instead, pager_unlock() is called. The
59382** call to pager_unlock() will discard all in-memory pages, unlock
59383** the database file and move the pager back to OPEN state. If this
59384** means that there is a hot-journal left in the file-system, the next
59385** connection to obtain a shared lock on the pager (which may be this one)
59386** will roll it back.
59387**
59388** If the pager has not already entered the ERROR state, but an IO or
59389** malloc error occurs during a rollback, then this will itself cause
59390** the pager to enter the ERROR state. Which will be cleared by the
59391** call to pager_unlock(), as described above.
59392*/
59393static void pagerUnlockAndRollback(Pager *pPager){
59394 if( pPager->eState!=PAGER_ERROR6 && pPager->eState!=PAGER_OPEN0 ){
59395 assert( assert_pager_state(pPager) )((void) (0));
59396 if( pPager->eState>=PAGER_WRITER_LOCKED2 ){
59397 sqlite3BeginBenignMalloc();
59398 sqlite3PagerRollback(pPager);
59399 sqlite3EndBenignMalloc();
59400 }else if( !pPager->exclusiveMode ){
59401 assert( pPager->eState==PAGER_READER )((void) (0));
59402 pager_end_transaction(pPager, 0, 0);
59403 }
59404 }else if( pPager->eState==PAGER_ERROR6
59405 && pPager->journalMode==PAGER_JOURNALMODE_MEMORY4
59406 && isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
59407 ){
59408 /* Special case for a ROLLBACK due to I/O error with an in-memory
59409 ** journal: We have to rollback immediately, before the journal is
59410 ** closed, because once it is closed, all content is forgotten. */
59411 int errCode = pPager->errCode;
59412 u8 eLock = pPager->eLock;
59413 pPager->eState = PAGER_OPEN0;
59414 pPager->errCode = SQLITE_OK0;
59415 pPager->eLock = EXCLUSIVE_LOCK4;
59416 pager_playback(pPager, 1);
59417 pPager->errCode = errCode;
59418 pPager->eLock = eLock;
59419 }
59420 pager_unlock(pPager);
59421}
59422
59423/*
59424** Parameter aData must point to a buffer of pPager->pageSize bytes
59425** of data. Compute and return a checksum based on the contents of the
59426** page of data and the current value of pPager->cksumInit.
59427**
59428** This is not a real checksum. It is really just the sum of the
59429** random initial value (pPager->cksumInit) and every 200th byte
59430** of the page data, starting with byte offset (pPager->pageSize%200).
59431** Each byte is interpreted as an 8-bit unsigned integer.
59432**
59433** Changing the formula used to compute this checksum results in an
59434** incompatible journal file format.
59435**
59436** If journal corruption occurs due to a power failure, the most likely
59437** scenario is that one end or the other of the record will be changed.
59438** It is much less likely that the two ends of the journal record will be
59439** correct and the middle be corrupt. Thus, this "checksum" scheme,
59440** though fast and simple, catches the mostly likely kind of corruption.
59441*/
59442static u32 pager_cksum(Pager *pPager, const u8 *aData){
59443 u32 cksum = pPager->cksumInit; /* Checksum value to return */
59444 int i = pPager->pageSize-200; /* Loop counter */
59445 while( i>0 ){
59446 cksum += aData[i];
59447 i -= 200;
59448 }
59449 return cksum;
59450}
59451
59452/*
59453** Read a single page from either the journal file (if isMainJrnl==1) or
59454** from the sub-journal (if isMainJrnl==0) and playback that page.
59455** The page begins at offset *pOffset into the file. The *pOffset
59456** value is increased to the start of the next page in the journal.
59457**
59458** The main rollback journal uses checksums - the statement journal does
59459** not.
59460**
59461** If the page number of the page record read from the (sub-)journal file
59462** is greater than the current value of Pager.dbSize, then playback is
59463** skipped and SQLITE_OK is returned.
59464**
59465** If pDone is not NULL, then it is a record of pages that have already
59466** been played back. If the page at *pOffset has already been played back
59467** (if the corresponding pDone bit is set) then skip the playback.
59468** Make sure the pDone bit corresponding to the *pOffset page is set
59469** prior to returning.
59470**
59471** If the page record is successfully read from the (sub-)journal file
59472** and played back, then SQLITE_OK is returned. If an IO error occurs
59473** while reading the record from the (sub-)journal file or while writing
59474** to the database file, then the IO error code is returned. If data
59475** is successfully read from the (sub-)journal file but appears to be
59476** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
59477** two circumstances:
59478**
59479** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
59480** * If the record is being rolled back from the main journal file
59481** and the checksum field does not match the record content.
59482**
59483** Neither of these two scenarios are possible during a savepoint rollback.
59484**
59485** If this is a savepoint rollback, then memory may have to be dynamically
59486** allocated by this function. If this is the case and an allocation fails,
59487** SQLITE_NOMEM is returned.
59488*/
59489static int pager_playback_one_page(
59490 Pager *pPager, /* The pager being played back */
59491 i64 *pOffset, /* Offset of record to playback */
59492 Bitvec *pDone, /* Bitvec of pages already played back */
59493 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
59494 int isSavepnt /* True for a savepoint rollback */
59495){
59496 int rc;
59497 PgHdr *pPg; /* An existing page in the cache */
59498 Pgno pgno; /* The page number of a page in journal */
59499 u32 cksum; /* Checksum used for sanity checking */
59500 char *aData; /* Temporary storage for the page */
59501 sqlite3_file *jfd; /* The file descriptor for the journal file */
59502 int isSynced; /* True if journal page is synced */
59503
59504 assert( (isMainJrnl&~1)==0 )((void) (0)); /* isMainJrnl is 0 or 1 */
59505 assert( (isSavepnt&~1)==0 )((void) (0)); /* isSavepnt is 0 or 1 */
59506 assert( isMainJrnl || pDone )((void) (0)); /* pDone always used on sub-journals */
59507 assert( isSavepnt || pDone==0 )((void) (0)); /* pDone never used on non-savepoint */
59508
59509 aData = pPager->pTmpSpace;
59510 assert( aData )((void) (0)); /* Temp storage must have already been allocated */
59511 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) )((void) (0));
59512
59513 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
59514 ** or savepoint rollback done at the request of the caller) or this is
59515 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
59516 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
59517 ** only reads from the main journal, not the sub-journal.
59518 */
59519 assert( pPager->eState>=PAGER_WRITER_CACHEMOD((void) (0))
59520 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)((void) (0))
59521 )((void) (0));
59522 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl )((void) (0));
59523
59524 /* Read the page number and page data from the journal or sub-journal
59525 ** file. Return an error code to the caller if an IO error occurs.
59526 */
59527 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
59528 rc = read32bits(jfd, *pOffset, &pgno);
59529 if( rc!=SQLITE_OK0 ) return rc;
59530 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
59531 if( rc!=SQLITE_OK0 ) return rc;
59532 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
59533
59534 /* Sanity checking on the page. This is more important that I originally
59535 ** thought. If a power failure occurs while the journal is being written,
59536 ** it could cause invalid data to be written into the journal. We need to
59537 ** detect this invalid data (with high probability) and ignore it.
59538 */
59539 if( pgno==0 || pgno==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
59540 assert( !isSavepnt )((void) (0));
59541 return SQLITE_DONE101;
59542 }
59543 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
59544 return SQLITE_OK0;
59545 }
59546 if( isMainJrnl ){
59547 rc = read32bits(jfd, (*pOffset)-4, &cksum);
59548 if( rc ) return rc;
59549 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
59550 return SQLITE_DONE101;
59551 }
59552 }
59553
59554 /* If this page has already been played back before during the current
59555 ** rollback, then don't bother to play it back again.
59556 */
59557 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK0 ){
59558 return rc;
59559 }
59560
59561 /* When playing back page 1, restore the nReserve setting
59562 */
59563 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
59564 pPager->nReserve = ((u8*)aData)[20];
59565 }
59566
59567 /* If the pager is in CACHEMOD state, then there must be a copy of this
59568 ** page in the pager cache. In this case just update the pager cache,
59569 ** not the database file. The page is left marked dirty in this case.
59570 **
59571 ** An exception to the above rule: If the database is in no-sync mode
59572 ** and a page is moved during an incremental vacuum then the page may
59573 ** not be in the pager cache. Later: if a malloc() or IO error occurs
59574 ** during a Movepage() call, then the page may not be in the cache
59575 ** either. So the condition described in the above paragraph is not
59576 ** assert()able.
59577 **
59578 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
59579 ** pager cache if it exists and the main file. The page is then marked
59580 ** not dirty. Since this code is only executed in PAGER_OPEN state for
59581 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
59582 ** if the pager is in OPEN state.
59583 **
59584 ** Ticket #1171: The statement journal might contain page content that is
59585 ** different from the page content at the start of the transaction.
59586 ** This occurs when a page is changed prior to the start of a statement
59587 ** then changed again within the statement. When rolling back such a
59588 ** statement we must not write to the original database unless we know
59589 ** for certain that original page contents are synced into the main rollback
59590 ** journal. Otherwise, a power loss might leave modified data in the
59591 ** database file without an entry in the rollback journal that can
59592 ** restore the database to its original form. Two conditions must be
59593 ** met before writing to the database files. (1) the database must be
59594 ** locked. (2) we know that the original page content is fully synced
59595 ** in the main journal either because the page is not in cache or else
59596 ** the page is marked as needSync==0.
59597 **
59598 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
59599 ** is possible to fail a statement on a database that does not yet exist.
59600 ** Do not attempt to write if database file has never been opened.
59601 */
59602 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
59603 pPg = 0;
59604 }else{
59605 pPg = sqlite3PagerLookup(pPager, pgno);
59606 }
59607 assert( pPg || !MEMDB )((void) (0));
59608 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile )((void) (0));
59609 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
59610 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
59611 (isMainJrnl?"main-journal":"sub-journal")
59612 ));
59613 if( isMainJrnl ){
59614 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
59615 }else{
59616 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC0x008));
59617 }
59618 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
59619 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
59620 && isSynced
59621 ){
59622 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
59623 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
59624 assert( !pagerUseWal(pPager) )((void) (0));
59625
59626 /* Write the data read from the journal back into the database file.
59627 ** This is usually safe even for an encrypted database - as the data
59628 ** was encrypted before it was written to the journal file. The exception
59629 ** is if the data was just read from an in-memory sub-journal. In that
59630 ** case it must be encrypted here before it is copied into the database
59631 ** file. */
59632 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
59633
59634 if( pgno>pPager->dbFileSize ){
59635 pPager->dbFileSize = pgno;
59636 }
59637 if( pPager->pBackup ){
59638 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
59639 }
59640 }else if( !isMainJrnl && pPg==0 ){
59641 /* If this is a rollback of a savepoint and data was not written to
59642 ** the database and the page is not in-memory, there is a potential
59643 ** problem. When the page is next fetched by the b-tree layer, it
59644 ** will be read from the database file, which may or may not be
59645 ** current.
59646 **
59647 ** There are a couple of different ways this can happen. All are quite
59648 ** obscure. When running in synchronous mode, this can only happen
59649 ** if the page is on the free-list at the start of the transaction, then
59650 ** populated, then moved using sqlite3PagerMovepage().
59651 **
59652 ** The solution is to add an in-memory page to the cache containing
59653 ** the data just read from the sub-journal. Mark the page as dirty
59654 ** and if the pager requires a journal-sync, then mark the page as
59655 ** requiring a journal-sync before it is written.
59656 */
59657 assert( isSavepnt )((void) (0));
59658 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 )((void) (0));
59659 pPager->doNotSpill |= SPILLFLAG_ROLLBACK0x02;
59660 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
59661 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 )((void) (0));
59662 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK0x02;
59663 if( rc!=SQLITE_OK0 ) return rc;
59664 sqlite3PcacheMakeDirty(pPg);
59665 }
59666 if( pPg ){
59667 /* No page should ever be explicitly rolled back that is in use, except
59668 ** for page 1 which is held in use in order to keep the lock on the
59669 ** database active. However such a page may be rolled back as a result
59670 ** of an internal error resulting in an automatic call to
59671 ** sqlite3PagerRollback().
59672 */
59673 void *pData;
59674 pData = pPg->pData;
59675 memcpy(pData, (u8*)aData, pPager->pageSize);
59676 pPager->xReiniter(pPg);
59677 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
59678 ** that call was dangerous and had no detectable benefit since the cache
59679 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
59680 ** has been removed. */
59681 pager_set_pagehash(pPg);
59682
59683 /* If this was page 1, then restore the value of Pager.dbFileVers.
59684 ** Do this before any decoding. */
59685 if( pgno==1 ){
59686 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
59687 }
59688 sqlite3PcacheRelease(pPg);
59689 }
59690 return rc;
59691}
59692
59693/*
59694** Parameter zSuper is the name of a super-journal file. A single journal
59695** file that referred to the super-journal file has just been rolled back.
59696** This routine checks if it is possible to delete the super-journal file,
59697** and does so if it is.
59698**
59699** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
59700** available for use within this function.
59701**
59702** When a super-journal file is created, it is populated with the names
59703** of all of its child journals, one after another, formatted as utf-8
59704** encoded text. The end of each child journal file is marked with a
59705** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
59706** file for a transaction involving two databases might be:
59707**
59708** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
59709**
59710** A super-journal file may only be deleted once all of its child
59711** journals have been rolled back.
59712**
59713** This function reads the contents of the super-journal file into
59714** memory and loops through each of the child journal names. For
59715** each child journal, it checks if:
59716**
59717** * if the child journal exists, and if so
59718** * if the child journal contains a reference to super-journal
59719** file zSuper
59720**
59721** If a child journal can be found that matches both of the criteria
59722** above, this function returns without doing anything. Otherwise, if
59723** no such child journal can be found, file zSuper is deleted from
59724** the file-system using sqlite3OsDelete().
59725**
59726** If an IO error within this function, an error code is returned. This
59727** function allocates memory by calling sqlite3Malloc(). If an allocation
59728** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
59729** occur, SQLITE_OK is returned.
59730**
59731** TODO: This function allocates a single block of memory to load
59732** the entire contents of the super-journal file. This could be
59733** a couple of kilobytes or so - potentially larger than the page
59734** size.
59735*/
59736static int pager_delsuper(Pager *pPager, const char *zSuper){
59737 sqlite3_vfs *pVfs = pPager->pVfs;
59738 int rc; /* Return code */
59739 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
59740 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
59741 char *zSuperJournal = 0; /* Contents of super-journal file */
59742 i64 nSuperJournal; /* Size of super-journal file */
59743 char *zJournal; /* Pointer to one journal within MJ file */
59744 char *zSuperPtr; /* Space to hold super-journal filename */
59745 char *zFree = 0; /* Free this buffer */
59746 int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
59747
59748 /* Allocate space for both the pJournal and pSuper file descriptors.
59749 ** If successful, open the super-journal file for reading.
59750 */
59751 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
59752 if( !pSuper ){
59753 rc = SQLITE_NOMEM_BKPT7;
59754 pJournal = 0;
59755 }else{
59756 const int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_SUPER_JOURNAL0x00004000);
59757 rc = sqlite3OsOpen(pVfs, zSuper, pSuper, flags, 0);
59758 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
59759 }
59760 if( rc!=SQLITE_OK0 ) goto delsuper_out;
59761
59762 /* Load the entire super-journal file into space obtained from
59763 ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain
59764 ** sufficient space (in zSuperPtr) to hold the names of super-journal
59765 ** files extracted from regular rollback-journals.
59766 */
59767 rc = sqlite3OsFileSize(pSuper, &nSuperJournal);
59768 if( rc!=SQLITE_OK0 ) goto delsuper_out;
59769 nSuperPtr = pVfs->mxPathname+1;
59770 zFree = sqlite3Malloc(4 + nSuperJournal + nSuperPtr + 2);
59771 if( !zFree ){
59772 rc = SQLITE_NOMEM_BKPT7;
59773 goto delsuper_out;
59774 }
59775 zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0;
59776 zSuperJournal = &zFree[4];
59777 zSuperPtr = &zSuperJournal[nSuperJournal+2];
59778 rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0);
59779 if( rc!=SQLITE_OK0 ) goto delsuper_out;
59780 zSuperJournal[nSuperJournal] = 0;
59781 zSuperJournal[nSuperJournal+1] = 0;
59782
59783 zJournal = zSuperJournal;
59784 while( (zJournal-zSuperJournal)<nSuperJournal ){
59785 int exists;
59786 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS0, &exists);
59787 if( rc!=SQLITE_OK0 ){
59788 goto delsuper_out;
59789 }
59790 if( exists ){
59791 /* One of the journals pointed to by the super-journal exists.
59792 ** Open it and check if it points at the super-journal. If
59793 ** so, return without deleting the super-journal file.
59794 ** NB: zJournal is really a MAIN_JOURNAL. But call it a
59795 ** SUPER_JOURNAL here so that the VFS will not send the zJournal
59796 ** name into sqlite3_database_file_object().
59797 */
59798 int c;
59799 int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_SUPER_JOURNAL0x00004000);
59800 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
59801 if( rc!=SQLITE_OK0 ){
59802 goto delsuper_out;
59803 }
59804
59805 rc = readSuperJournal(pJournal, zSuperPtr, nSuperPtr);
59806 sqlite3OsClose(pJournal);
59807 if( rc!=SQLITE_OK0 ){
59808 goto delsuper_out;
59809 }
59810
59811 c = zSuperPtr[0]!=0 && strcmp(zSuperPtr, zSuper)==0;
59812 if( c ){
59813 /* We have a match. Do not delete the super-journal file. */
59814 goto delsuper_out;
59815 }
59816 }
59817 zJournal += (sqlite3Strlen30(zJournal)+1);
59818 }
59819
59820 sqlite3OsClose(pSuper);
59821 rc = sqlite3OsDelete(pVfs, zSuper, 0);
59822
59823delsuper_out:
59824 sqlite3_free(zFree);
59825 if( pSuper ){
59826 sqlite3OsClose(pSuper);
59827 assert( !isOpen(pJournal) )((void) (0));
59828 sqlite3_free(pSuper);
59829 }
59830 return rc;
59831}
59832
59833
59834/*
59835** This function is used to change the actual size of the database
59836** file in the file-system. This only happens when committing a transaction,
59837** or rolling back a transaction (including rolling back a hot-journal).
59838**
59839** If the main database file is not open, or the pager is not in either
59840** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
59841** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
59842** If the file on disk is currently larger than nPage pages, then use the VFS
59843** xTruncate() method to truncate it.
59844**
59845** Or, it might be the case that the file on disk is smaller than
59846** nPage pages. Some operating system implementations can get confused if
59847** you try to truncate a file to some size that is larger than it
59848** currently is, so detect this case and write a single zero byte to
59849** the end of the new file instead.
59850**
59851** If successful, return SQLITE_OK. If an IO error occurs while modifying
59852** the database file, return the error code to the caller.
59853*/
59854static int pager_truncate(Pager *pPager, Pgno nPage){
59855 int rc = SQLITE_OK0;
59856 assert( pPager->eState!=PAGER_ERROR )((void) (0));
59857 assert( pPager->eState!=PAGER_READER )((void) (0));
59858 PAGERTRACE(("Truncate %d npage %u\n", PAGERID(pPager), nPage));
59859
59860
59861 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
59862 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
59863 ){
59864 i64 currentSize, newSize;
59865 int szPage = pPager->pageSize;
59866 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
59867 /* TODO: Is it safe to use Pager.dbFileSize here? */
59868 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
59869 newSize = szPage*(i64)nPage;
59870 if( rc==SQLITE_OK0 && currentSize!=newSize ){
59871 if( currentSize>newSize ){
59872 rc = sqlite3OsTruncate(pPager->fd, newSize);
59873 }else if( (currentSize+szPage)<=newSize ){
59874 char *pTmp = pPager->pTmpSpace;
59875 memset(pTmp, 0, szPage);
59876 testcase( (newSize-szPage) == currentSize );
59877 testcase( (newSize-szPage) > currentSize );
59878 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &newSize);
59879 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
59880 }
59881 if( rc==SQLITE_OK0 ){
59882 pPager->dbFileSize = nPage;
59883 }
59884 }
59885 }
59886 return rc;
59887}
59888
59889/*
59890** Return a sanitized version of the sector-size of OS file pFile. The
59891** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
59892*/
59893SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *pFile){
59894 int iRet = sqlite3OsSectorSize(pFile);
59895 if( iRet<32 ){
59896 iRet = 512;
59897 }else if( iRet>MAX_SECTOR_SIZE0x10000 ){
59898 assert( MAX_SECTOR_SIZE>=512 )((void) (0));
59899 iRet = MAX_SECTOR_SIZE0x10000;
59900 }
59901 return iRet;
59902}
59903
59904/*
59905** Set the value of the Pager.sectorSize variable for the given
59906** pager based on the value returned by the xSectorSize method
59907** of the open database file. The sector size will be used
59908** to determine the size and alignment of journal header and
59909** super-journal pointers within created journal files.
59910**
59911** For temporary files the effective sector size is always 512 bytes.
59912**
59913** Otherwise, for non-temporary files, the effective sector size is
59914** the value returned by the xSectorSize() method rounded up to 32 if
59915** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
59916** is greater than MAX_SECTOR_SIZE.
59917**
59918** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
59919** the effective sector size to its minimum value (512). The purpose of
59920** pPager->sectorSize is to define the "blast radius" of bytes that
59921** might change if a crash occurs while writing to a single byte in
59922** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
59923** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
59924** size. For backwards compatibility of the rollback journal file format,
59925** we cannot reduce the effective sector size below 512.
59926*/
59927static void setSectorSize(Pager *pPager){
59928 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
59929
59930 if( pPager->tempFile
59931 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
59932 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000)!=0
59933 ){
59934 /* Sector size doesn't matter for temporary files. Also, the file
59935 ** may not have been opened yet, in which case the OsSectorSize()
59936 ** call will segfault. */
59937 pPager->sectorSize = 512;
59938 }else{
59939 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
59940 }
59941}
59942
59943/*
59944** Playback the journal and thus restore the database file to
59945** the state it was in before we started making changes.
59946**
59947** The journal file format is as follows:
59948**
59949** (1) 8 byte prefix. A copy of aJournalMagic[].
59950** (2) 4 byte big-endian integer which is the number of valid page records
59951** in the journal. If this value is 0xffffffff, then compute the
59952** number of page records from the journal size.
59953** (3) 4 byte big-endian integer which is the initial value for the
59954** sanity checksum.
59955** (4) 4 byte integer which is the number of pages to truncate the
59956** database to during a rollback.
59957** (5) 4 byte big-endian integer which is the sector size. The header
59958** is this many bytes in size.
59959** (6) 4 byte big-endian integer which is the page size.
59960** (7) zero padding out to the next sector size.
59961** (8) Zero or more pages instances, each as follows:
59962** + 4 byte page number.
59963** + pPager->pageSize bytes of data.
59964** + 4 byte checksum
59965**
59966** When we speak of the journal header, we mean the first 7 items above.
59967** Each entry in the journal is an instance of the 8th item.
59968**
59969** Call the value from the second bullet "nRec". nRec is the number of
59970** valid page entries in the journal. In most cases, you can compute the
59971** value of nRec from the size of the journal file. But if a power
59972** failure occurred while the journal was being written, it could be the
59973** case that the size of the journal file had already been increased but
59974** the extra entries had not yet made it safely to disk. In such a case,
59975** the value of nRec computed from the file size would be too large. For
59976** that reason, we always use the nRec value in the header.
59977**
59978** If the nRec value is 0xffffffff it means that nRec should be computed
59979** from the file size. This value is used when the user selects the
59980** no-sync option for the journal. A power failure could lead to corruption
59981** in this case. But for things like temporary table (which will be
59982** deleted when the power is restored) we don't care.
59983**
59984** If the file opened as the journal file is not a well-formed
59985** journal file then all pages up to the first corrupted page are rolled
59986** back (or no pages if the journal header is corrupted). The journal file
59987** is then deleted and SQLITE_OK returned, just as if no corruption had
59988** been encountered.
59989**
59990** If an I/O or malloc() error occurs, the journal-file is not deleted
59991** and an error code is returned.
59992**
59993** The isHot parameter indicates that we are trying to rollback a journal
59994** that might be a hot journal. Or, it could be that the journal is
59995** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
59996** If the journal really is hot, reset the pager cache prior rolling
59997** back any content. If the journal is merely persistent, no reset is
59998** needed.
59999*/
60000static int pager_playback(Pager *pPager, int isHot){
60001 sqlite3_vfs *pVfs = pPager->pVfs;
60002 i64 szJ; /* Size of the journal file in bytes */
60003 u32 nRec; /* Number of Records in the journal */
60004 u32 u; /* Unsigned loop counter */
60005 Pgno mxPg = 0; /* Size of the original file in pages */
60006 int rc; /* Result code of a subroutine */
60007 int res = 1; /* Value returned by sqlite3OsAccess() */
60008 char *zSuper = 0; /* Name of super-journal file if any */
60009 int needPagerReset; /* True to reset page prior to first page rollback */
60010 int nPlayback = 0; /* Total number of pages restored from journal */
60011 u32 savedPageSize = pPager->pageSize;
60012
60013 /* Figure out how many records are in the journal. Abort early if
60014 ** the journal is empty.
60015 */
60016 assert( isOpen(pPager->jfd) )((void) (0));
60017 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
60018 if( rc!=SQLITE_OK0 ){
60019 goto end_playback;
60020 }
60021
60022 /* Read the super-journal name from the journal, if it is present.
60023 ** If a super-journal file name is specified, but the file is not
60024 ** present on disk, then the journal is not hot and does not need to be
60025 ** played back.
60026 **
60027 ** TODO: Technically the following is an error because it assumes that
60028 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
60029 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
60030 ** mxPathname is 512, which is the same as the minimum allowable value
60031 ** for pageSize.
60032 */
60033 zSuper = pPager->pTmpSpace;
60034 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
60035 if( rc==SQLITE_OK0 && zSuper[0] ){
60036 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS0, &res);
60037 }
60038 zSuper = 0;
60039 if( rc!=SQLITE_OK0 || !res ){
60040 goto end_playback;
60041 }
60042 pPager->journalOff = 0;
60043 needPagerReset = isHot;
60044
60045 /* This loop terminates either when a readJournalHdr() or
60046 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
60047 ** occurs.
60048 */
60049 while( 1 ){
60050 /* Read the next journal header from the journal file. If there are
60051 ** not enough bytes left in the journal file for a complete header, or
60052 ** it is corrupted, then a process must have failed while writing it.
60053 ** This indicates nothing more needs to be rolled back.
60054 */
60055 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
60056 if( rc!=SQLITE_OK0 ){
60057 if( rc==SQLITE_DONE101 ){
60058 rc = SQLITE_OK0;
60059 }
60060 goto end_playback;
60061 }
60062
60063 /* If nRec is 0xffffffff, then this journal was created by a process
60064 ** working in no-sync mode. This means that the rest of the journal
60065 ** file consists of pages, there are no more journal headers. Compute
60066 ** the value of nRec based on this assumption.
60067 */
60068 if( nRec==0xffffffff ){
60069 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) )((void) (0));
60070 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager)(pPager->sectorSize))/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
60071 }
60072
60073 /* If nRec is 0 and this rollback is of a transaction created by this
60074 ** process and if this is the final header in the journal, then it means
60075 ** that this part of the journal was being filled but has not yet been
60076 ** synced to disk. Compute the number of pages based on the remaining
60077 ** size of the file.
60078 **
60079 ** The third term of the test was added to fix ticket #2565.
60080 ** When rolling back a hot journal, nRec==0 always means that the next
60081 ** chunk of the journal contains zero pages to be rolled back. But
60082 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
60083 ** the journal, it means that the journal might contain additional
60084 ** pages that need to be rolled back and that the number of pages
60085 ** should be computed based on the journal file size.
60086 */
60087 if( nRec==0 && !isHot &&
60088 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff ){
60089 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
60090 }
60091
60092 /* If this is the first header read from the journal, truncate the
60093 ** database file back to its original size.
60094 */
60095 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
60096 rc = pager_truncate(pPager, mxPg);
60097 if( rc!=SQLITE_OK0 ){
60098 goto end_playback;
60099 }
60100 pPager->dbSize = mxPg;
60101 if( pPager->mxPgno<mxPg ){
60102 pPager->mxPgno = mxPg;
60103 }
60104 }
60105
60106 /* Copy original pages out of the journal and back into the
60107 ** database file and/or page cache.
60108 */
60109 for(u=0; u<nRec; u++){
60110 if( needPagerReset ){
60111 pager_reset(pPager);
60112 needPagerReset = 0;
60113 }
60114 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
60115 if( rc==SQLITE_OK0 ){
60116 nPlayback++;
60117 }else{
60118 if( rc==SQLITE_DONE101 ){
60119 pPager->journalOff = szJ;
60120 break;
60121 }else if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
60122 /* If the journal has been truncated, simply stop reading and
60123 ** processing the journal. This might happen if the journal was
60124 ** not completely written and synced prior to a crash. In that
60125 ** case, the database should have never been written in the
60126 ** first place so it is OK to simply abandon the rollback. */
60127 rc = SQLITE_OK0;
60128 goto end_playback;
60129 }else{
60130 /* If we are unable to rollback, quit and return the error
60131 ** code. This will cause the pager to enter the error state
60132 ** so that no further harm will be done. Perhaps the next
60133 ** process to come along will be able to rollback the database.
60134 */
60135 goto end_playback;
60136 }
60137 }
60138 }
60139 }
60140 /*NOTREACHED*/
60141 assert( 0 )((void) (0));
60142
60143end_playback:
60144 if( rc==SQLITE_OK0 ){
60145 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
60146 }
60147 /* Following a rollback, the database file should be back in its original
60148 ** state prior to the start of the transaction, so invoke the
60149 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
60150 ** assertion that the transaction counter was modified.
60151 */
60152#ifdef SQLITE_DEBUG
60153 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED0xca093fa0,0);
60154#endif
60155
60156 /* If this playback is happening automatically as a result of an IO or
60157 ** malloc error that occurred after the change-counter was updated but
60158 ** before the transaction was committed, then the change-counter
60159 ** modification may just have been reverted. If this happens in exclusive
60160 ** mode, then subsequent transactions performed by the connection will not
60161 ** update the change-counter at all. This may lead to cache inconsistency
60162 ** problems for other processes at some point in the future. So, just
60163 ** in case this has happened, clear the changeCountDone flag now.
60164 */
60165 pPager->changeCountDone = pPager->tempFile;
60166
60167 if( rc==SQLITE_OK0 ){
60168 /* Leave 4 bytes of space before the super-journal filename in memory.
60169 ** This is because it may end up being passed to sqlite3OsOpen(), in
60170 ** which case it requires 4 0x00 bytes in memory immediately before
60171 ** the filename. */
60172 zSuper = &pPager->pTmpSpace[4];
60173 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
60174 testcase( rc!=SQLITE_OK );
60175 }
60176 if( rc==SQLITE_OK0
60177 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
60178 ){
60179 rc = sqlite3PagerSync(pPager, 0);
60180 }
60181 if( rc==SQLITE_OK0 ){
60182 rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0);
60183 testcase( rc!=SQLITE_OK );
60184 }
60185 if( rc==SQLITE_OK0 && zSuper[0] && res ){
60186 /* If there was a super-journal and this routine will return success,
60187 ** see if it is possible to delete the super-journal.
60188 */
60189 assert( zSuper==&pPager->pTmpSpace[4] )((void) (0));
60190 memset(pPager->pTmpSpace, 0, 4);
60191 rc = pager_delsuper(pPager, zSuper);
60192 testcase( rc!=SQLITE_OK );
60193 }
60194 if( isHot && nPlayback ){
60195 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)), "recovered %d pages from %s",
60196 nPlayback, pPager->zJournal);
60197 }
60198
60199 /* The Pager.sectorSize variable may have been updated while rolling
60200 ** back a journal created by a process with a different sector size
60201 ** value. Reset it to the correct value for this process.
60202 */
60203 setSectorSize(pPager);
60204 return rc;
60205}
60206
60207
60208/*
60209** Read the content for page pPg out of the database file (or out of
60210** the WAL if that is where the most recent copy if found) into
60211** pPg->pData. A shared lock or greater must be held on the database
60212** file before this function is called.
60213**
60214** If page 1 is read, then the value of Pager.dbFileVers[] is set to
60215** the value read from the database file.
60216**
60217** If an IO error occurs, then the IO error is returned to the caller.
60218** Otherwise, SQLITE_OK is returned.
60219*/
60220static int readDbPage(PgHdr *pPg){
60221 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
60222 int rc = SQLITE_OK0; /* Return code */
60223
60224#ifndef SQLITE_OMIT_WAL
60225 u32 iFrame = 0; /* Frame of WAL containing pgno */
60226
60227 assert( pPager->eState>=PAGER_READER && !MEMDB )((void) (0));
60228 assert( isOpen(pPager->fd) )((void) (0));
60229
60230 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60231 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
60232 if( rc ) return rc;
60233 }
60234 if( iFrame ){
60235 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
60236 }else
60237#endif
60238 {
60239 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
60240 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
60241 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
60242 rc = SQLITE_OK0;
60243 }
60244 }
60245
60246 if( pPg->pgno==1 ){
60247 if( rc ){
60248 /* If the read is unsuccessful, set the dbFileVers[] to something
60249 ** that will never be a valid file version. dbFileVers[] is a copy
60250 ** of bytes 24..39 of the database. Bytes 28..31 should always be
60251 ** zero or the size of the database in page. Bytes 32..35 and 35..39
60252 ** should be page numbers which are never 0xffffffff. So filling
60253 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
60254 **
60255 ** For an encrypted database, the situation is more complex: bytes
60256 ** 24..39 of the database are white noise. But the probability of
60257 ** white noise equaling 16 bytes of 0xff is vanishingly small so
60258 ** we should still be ok.
60259 */
60260 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
60261 }else{
60262 u8 *dbFileVers = &((u8*)pPg->pData)[24];
60263 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
60264 }
60265 }
60266 PAGER_INCR(sqlite3_pager_readdb_count);
60267 PAGER_INCR(pPager->nRead);
60268 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
60269 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
60270 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
60271
60272 return rc;
60273}
60274
60275/*
60276** Update the value of the change-counter at offsets 24 and 92 in
60277** the header and the sqlite version number at offset 96.
60278**
60279** This is an unconditional update. See also the pager_incr_changecounter()
60280** routine which only updates the change-counter if the update is actually
60281** needed, as determined by the pPager->changeCountDone state variable.
60282*/
60283static void pager_write_changecounter(PgHdr *pPg){
60284 u32 change_counter;
60285 if( NEVER(pPg==0)(pPg==0) ) return;
60286
60287 /* Increment the value just read and write it back to byte 24. */
60288 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
60289 put32bits(((char*)pPg->pData)+24, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+24,change_counter
)
;
60290
60291 /* Also store the SQLite version number in bytes 96..99 and in
60292 ** bytes 92..95 store the change counter for which the version number
60293 ** is valid. */
60294 put32bits(((char*)pPg->pData)+92, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+92,change_counter
)
;
60295 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER)sqlite3Put4byte((u8*)((char*)pPg->pData)+96,3047001);
60296}
60297
60298#ifndef SQLITE_OMIT_WAL
60299/*
60300** This function is invoked once for each page that has already been
60301** written into the log file when a WAL transaction is rolled back.
60302** Parameter iPg is the page number of said page. The pCtx argument
60303** is actually a pointer to the Pager structure.
60304**
60305** If page iPg is present in the cache, and has no outstanding references,
60306** it is discarded. Otherwise, if there are one or more outstanding
60307** references, the page content is reloaded from the database. If the
60308** attempt to reload content from the database is required and fails,
60309** return an SQLite error code. Otherwise, SQLITE_OK.
60310*/
60311static int pagerUndoCallback(void *pCtx, Pgno iPg){
60312 int rc = SQLITE_OK0;
60313 Pager *pPager = (Pager *)pCtx;
60314 PgHdr *pPg;
60315
60316 assert( pagerUseWal(pPager) )((void) (0));
60317 pPg = sqlite3PagerLookup(pPager, iPg);
60318 if( pPg ){
60319 if( sqlite3PcachePageRefcount(pPg)==1 ){
60320 sqlite3PcacheDrop(pPg);
60321 }else{
60322 rc = readDbPage(pPg);
60323 if( rc==SQLITE_OK0 ){
60324 pPager->xReiniter(pPg);
60325 }
60326 sqlite3PagerUnrefNotNull(pPg);
60327 }
60328 }
60329
60330 /* Normally, if a transaction is rolled back, any backup processes are
60331 ** updated as data is copied out of the rollback journal and into the
60332 ** database. This is not generally possible with a WAL database, as
60333 ** rollback involves simply truncating the log file. Therefore, if one
60334 ** or more frames have already been written to the log (and therefore
60335 ** also copied into the backup databases) as part of this transaction,
60336 ** the backups must be restarted.
60337 */
60338 sqlite3BackupRestart(pPager->pBackup);
60339
60340 return rc;
60341}
60342
60343/*
60344** This function is called to rollback a transaction on a WAL database.
60345*/
60346static int pagerRollbackWal(Pager *pPager){
60347 int rc; /* Return Code */
60348 PgHdr *pList; /* List of dirty pages to revert */
60349
60350 /* For all pages in the cache that are currently dirty or have already
60351 ** been written (but not committed) to the log file, do one of the
60352 ** following:
60353 **
60354 ** + Discard the cached page (if refcount==0), or
60355 ** + Reload page content from the database (if refcount>0).
60356 */
60357 pPager->dbSize = pPager->dbOrigSize;
60358 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
60359 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60360 while( pList && rc==SQLITE_OK0 ){
60361 PgHdr *pNext = pList->pDirty;
60362 rc = pagerUndoCallback((void *)pPager, pList->pgno);
60363 pList = pNext;
60364 }
60365
60366 return rc;
60367}
60368
60369/*
60370** This function is a wrapper around sqlite3WalFrames(). As well as logging
60371** the contents of the list of pages headed by pList (connected by pDirty),
60372** this function notifies any active backup processes that the pages have
60373** changed.
60374**
60375** The list of pages passed into this routine is always sorted by page number.
60376** Hence, if page 1 appears anywhere on the list, it will be the first page.
60377*/
60378static int pagerWalFrames(
60379 Pager *pPager, /* Pager object */
60380 PgHdr *pList, /* List of frames to log */
60381 Pgno nTruncate, /* Database size after this commit */
60382 int isCommit /* True if this is a commit */
60383){
60384 int rc; /* Return code */
60385 int nList; /* Number of pages in pList */
60386 PgHdr *p; /* For looping over pages */
60387
60388 assert( pPager->pWal )((void) (0));
60389 assert( pList )((void) (0));
60390#ifdef SQLITE_DEBUG
60391 /* Verify that the page list is in ascending order */
60392 for(p=pList; p && p->pDirty; p=p->pDirty){
60393 assert( p->pgno < p->pDirty->pgno )((void) (0));
60394 }
60395#endif
60396
60397 assert( pList->pDirty==0 || isCommit )((void) (0));
60398 if( isCommit ){
60399 /* If a WAL transaction is being committed, there is no point in writing
60400 ** any pages with page numbers greater than nTruncate into the WAL file.
60401 ** They will never be read by any client. So remove them from the pDirty
60402 ** list here. */
60403 PgHdr **ppNext = &pList;
60404 nList = 0;
60405 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
60406 if( p->pgno<=nTruncate ){
60407 ppNext = &p->pDirty;
60408 nList++;
60409 }
60410 }
60411 assert( pList )((void) (0));
60412 }else{
60413 nList = 1;
60414 }
60415 pPager->aStat[PAGER_STAT_WRITE2] += nList;
60416
60417 if( pList->pgno==1 ) pager_write_changecounter(pList);
60418 rc = sqlite3WalFrames(pPager->pWal,
60419 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
60420 );
60421 if( rc==SQLITE_OK0 && pPager->pBackup ){
60422 for(p=pList; p; p=p->pDirty){
60423 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
60424 }
60425 }
60426
60427#ifdef SQLITE_CHECK_PAGES
60428 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60429 for(p=pList; p; p=p->pDirty){
60430 pager_set_pagehash(p);
60431 }
60432#endif
60433
60434 return rc;
60435}
60436
60437/*
60438** Begin a read transaction on the WAL.
60439**
60440** This routine used to be called "pagerOpenSnapshot()" because it essentially
60441** makes a snapshot of the database at the current point in time and preserves
60442** that snapshot for use by the reader in spite of concurrently changes by
60443** other writers or checkpointers.
60444*/
60445static int pagerBeginReadTransaction(Pager *pPager){
60446 int rc; /* Return code */
60447 int changed = 0; /* True if cache must be reset */
60448
60449 assert( pagerUseWal(pPager) )((void) (0));
60450 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
60451
60452 /* sqlite3WalEndReadTransaction() was not called for the previous
60453 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
60454 ** are in locking_mode=NORMAL and EndRead() was previously called,
60455 ** the duplicate call is harmless.
60456 */
60457 sqlite3WalEndReadTransaction(pPager->pWal);
60458
60459 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
60460 if( rc!=SQLITE_OK0 || changed ){
60461 pager_reset(pPager);
60462 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
60463 }
60464
60465 return rc;
60466}
60467#endif
60468
60469/*
60470** This function is called as part of the transition from PAGER_OPEN
60471** to PAGER_READER state to determine the size of the database file
60472** in pages (assuming the page size currently stored in Pager.pageSize).
60473**
60474** If no error occurs, SQLITE_OK is returned and the size of the database
60475** in pages is stored in *pnPage. Otherwise, an error code (perhaps
60476** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
60477*/
60478static int pagerPagecount(Pager *pPager, Pgno *pnPage){
60479 Pgno nPage; /* Value to return via *pnPage */
60480
60481 /* Query the WAL sub-system for the database size. The WalDbsize()
60482 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
60483 ** if the database size is not available. The database size is not
60484 ** available from the WAL sub-system if the log file is empty or
60485 ** contains no valid committed transactions.
60486 */
60487 assert( pPager->eState==PAGER_OPEN )((void) (0));
60488 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
60489 assert( isOpen(pPager->fd) )((void) (0));
60490 assert( pPager->tempFile==0 )((void) (0));
60491 nPage = sqlite3WalDbsize(pPager->pWal);
60492
60493 /* If the number of pages in the database is not available from the
60494 ** WAL sub-system, determine the page count based on the size of
60495 ** the database file. If the size of the database file is not an
60496 ** integer multiple of the page-size, round up the result.
60497 */
60498 if( nPage==0 && ALWAYS(isOpen(pPager->fd))(((pPager->fd)->pMethods!=0)) ){
60499 i64 n = 0; /* Size of db file in bytes */
60500 int rc = sqlite3OsFileSize(pPager->fd, &n);
60501 if( rc!=SQLITE_OK0 ){
60502 return rc;
60503 }
60504 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
60505 }
60506
60507 /* If the current number of pages in the file is greater than the
60508 ** configured maximum pager number, increase the allowed limit so
60509 ** that the file can be read.
60510 */
60511 if( nPage>pPager->mxPgno ){
60512 pPager->mxPgno = (Pgno)nPage;
60513 }
60514
60515 *pnPage = nPage;
60516 return SQLITE_OK0;
60517}
60518
60519#ifndef SQLITE_OMIT_WAL
60520/*
60521** Check if the *-wal file that corresponds to the database opened by pPager
60522** exists if the database is not empty, or verify that the *-wal file does
60523** not exist (by deleting it) if the database file is empty.
60524**
60525** If the database is not empty and the *-wal file exists, open the pager
60526** in WAL mode. If the database is empty or if no *-wal file exists and
60527** if no error occurs, make sure Pager.journalMode is not set to
60528** PAGER_JOURNALMODE_WAL.
60529**
60530** Return SQLITE_OK or an error code.
60531**
60532** The caller must hold a SHARED lock on the database file to call this
60533** function. Because an EXCLUSIVE lock on the db file is required to delete
60534** a WAL on a none-empty database, this ensures there is no race condition
60535** between the xAccess() below and an xDelete() being executed by some
60536** other connection.
60537*/
60538static int pagerOpenWalIfPresent(Pager *pPager){
60539 int rc = SQLITE_OK0;
60540 assert( pPager->eState==PAGER_OPEN )((void) (0));
60541 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
60542
60543 if( !pPager->tempFile ){
60544 int isWal; /* True if WAL file exists */
60545 rc = sqlite3OsAccess(
60546 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &isWal
60547 );
60548 if( rc==SQLITE_OK0 ){
60549 if( isWal ){
60550 Pgno nPage; /* Size of the database file */
60551
60552 rc = pagerPagecount(pPager, &nPage);
60553 if( rc ) return rc;
60554 if( nPage==0 ){
60555 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
60556 }else{
60557 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
60558 rc = sqlite3PagerOpenWal(pPager, 0);
60559 }
60560 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
60561 pPager->journalMode = PAGER_JOURNALMODE_DELETE0;
60562 }
60563 }
60564 }
60565 return rc;
60566}
60567#endif
60568
60569/*
60570** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
60571** the entire super-journal file. The case pSavepoint==NULL occurs when
60572** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
60573** savepoint.
60574**
60575** When pSavepoint is not NULL (meaning a non-transaction savepoint is
60576** being rolled back), then the rollback consists of up to three stages,
60577** performed in the order specified:
60578**
60579** * Pages are played back from the main journal starting at byte
60580** offset PagerSavepoint.iOffset and continuing to
60581** PagerSavepoint.iHdrOffset, or to the end of the main journal
60582** file if PagerSavepoint.iHdrOffset is zero.
60583**
60584** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
60585** back starting from the journal header immediately following
60586** PagerSavepoint.iHdrOffset to the end of the main journal file.
60587**
60588** * Pages are then played back from the sub-journal file, starting
60589** with the PagerSavepoint.iSubRec and continuing to the end of
60590** the journal file.
60591**
60592** Throughout the rollback process, each time a page is rolled back, the
60593** corresponding bit is set in a bitvec structure (variable pDone in the
60594** implementation below). This is used to ensure that a page is only
60595** rolled back the first time it is encountered in either journal.
60596**
60597** If pSavepoint is NULL, then pages are only played back from the main
60598** journal file. There is no need for a bitvec in this case.
60599**
60600** In either case, before playback commences the Pager.dbSize variable
60601** is reset to the value that it held at the start of the savepoint
60602** (or transaction). No page with a page-number greater than this value
60603** is played back. If one is encountered it is simply skipped.
60604*/
60605static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
60606 i64 szJ; /* Effective size of the main journal */
60607 i64 iHdrOff; /* End of first segment of main-journal records */
60608 int rc = SQLITE_OK0; /* Return code */
60609 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
60610
60611 assert( pPager->eState!=PAGER_ERROR )((void) (0));
60612 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
60613
60614 /* Allocate a bitvec to use to store the set of pages rolled back */
60615 if( pSavepoint ){
60616 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
60617 if( !pDone ){
60618 return SQLITE_NOMEM_BKPT7;
60619 }
60620 }
60621
60622 /* Set the database size back to the value it was before the savepoint
60623 ** being reverted was opened.
60624 */
60625 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
60626 pPager->changeCountDone = pPager->tempFile;
60627
60628 if( !pSavepoint && pagerUseWal(pPager)((pPager)->pWal!=0) ){
60629 return pagerRollbackWal(pPager);
60630 }
60631
60632 /* Use pPager->journalOff as the effective size of the main rollback
60633 ** journal. The actual file might be larger than this in
60634 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
60635 ** past pPager->journalOff is off-limits to us.
60636 */
60637 szJ = pPager->journalOff;
60638 assert( pagerUseWal(pPager)==0 || szJ==0 )((void) (0));
60639
60640 /* Begin by rolling back records from the main journal starting at
60641 ** PagerSavepoint.iOffset and continuing to the next journal header.
60642 ** There might be records in the main journal that have a page number
60643 ** greater than the current database size (pPager->dbSize) but those
60644 ** will be skipped automatically. Pages are added to pDone as they
60645 ** are played back.
60646 */
60647 if( pSavepoint && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
60648 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
60649 pPager->journalOff = pSavepoint->iOffset;
60650 while( rc==SQLITE_OK0 && pPager->journalOff<iHdrOff ){
60651 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
60652 }
60653 assert( rc!=SQLITE_DONE )((void) (0));
60654 }else{
60655 pPager->journalOff = 0;
60656 }
60657
60658 /* Continue rolling back records out of the main journal starting at
60659 ** the first journal header seen and continuing until the effective end
60660 ** of the main journal file. Continue to skip out-of-range pages and
60661 ** continue adding pages rolled back to pDone.
60662 */
60663 while( rc==SQLITE_OK0 && pPager->journalOff<szJ ){
60664 u32 ii; /* Loop counter */
60665 u32 nJRec = 0; /* Number of Journal Records */
60666 u32 dummy;
60667 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
60668 assert( rc!=SQLITE_DONE )((void) (0));
60669
60670 /*
60671 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
60672 ** test is related to ticket #2565. See the discussion in the
60673 ** pager_playback() function for additional information.
60674 */
60675 if( nJRec==0
60676 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff
60677 ){
60678 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
60679 }
60680 for(ii=0; rc==SQLITE_OK0 && ii<nJRec && pPager->journalOff<szJ; ii++){
60681 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
60682 }
60683 assert( rc!=SQLITE_DONE )((void) (0));
60684 }
60685 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ )((void) (0));
60686
60687 /* Finally, rollback pages from the sub-journal. Page that were
60688 ** previously rolled back out of the main journal (and are hence in pDone)
60689 ** will be skipped. Out-of-range pages are also skipped.
60690 */
60691 if( pSavepoint ){
60692 u32 ii; /* Loop counter */
60693 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
60694
60695 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
60696 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
60697 }
60698 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK0 && ii<pPager->nSubRec; ii++){
60699 assert( offset==(i64)ii*(4+pPager->pageSize) )((void) (0));
60700 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
60701 }
60702 assert( rc!=SQLITE_DONE )((void) (0));
60703 }
60704
60705 sqlite3BitvecDestroy(pDone);
60706 if( rc==SQLITE_OK0 ){
60707 pPager->journalOff = szJ;
60708 }
60709
60710 return rc;
60711}
60712
60713/*
60714** Change the maximum number of in-memory pages that are allowed
60715** before attempting to recycle clean and unused pages.
60716*/
60717SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
60718 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
60719}
60720
60721/*
60722** Change the maximum number of in-memory pages that are allowed
60723** before attempting to spill pages to journal.
60724*/
60725SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
60726 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
60727}
60728
60729/*
60730** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
60731*/
60732static void pagerFixMaplimit(Pager *pPager){
60733#if SQLITE_MAX_MMAP_SIZE20971520>0
60734 sqlite3_file *fd = pPager->fd;
60735 if( isOpen(fd)((fd)->pMethods!=0) && fd->pMethods->iVersion>=3 ){
60736 sqlite3_int64 sz;
60737 sz = pPager->szMmap;
60738 pPager->bUseFetch = (sz>0);
60739 setGetterMethod(pPager);
60740 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE18, &sz);
60741 }
60742#endif
60743}
60744
60745/*
60746** Change the maximum size of any memory mapping made of the database file.
60747*/
60748SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
60749 pPager->szMmap = szMmap;
60750 pagerFixMaplimit(pPager);
60751}
60752
60753/*
60754** Free as much memory as possible from the pager.
60755*/
60756SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager *pPager){
60757 sqlite3PcacheShrink(pPager->pPCache);
60758}
60759
60760/*
60761** Adjust settings of the pager to those specified in the pgFlags parameter.
60762**
60763** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
60764** of the database to damage due to OS crashes or power failures by
60765** changing the number of syncs()s when writing the journals.
60766** There are four levels:
60767**
60768** OFF sqlite3OsSync() is never called. This is the default
60769** for temporary and transient files.
60770**
60771** NORMAL The journal is synced once before writes begin on the
60772** database. This is normally adequate protection, but
60773** it is theoretically possible, though very unlikely,
60774** that an inopertune power failure could leave the journal
60775** in a state which would cause damage to the database
60776** when it is rolled back.
60777**
60778** FULL The journal is synced twice before writes begin on the
60779** database (with some additional information - the nRec field
60780** of the journal header - being written in between the two
60781** syncs). If we assume that writing a
60782** single disk sector is atomic, then this mode provides
60783** assurance that the journal will not be corrupted to the
60784** point of causing damage to the database during rollback.
60785**
60786** EXTRA This is like FULL except that is also syncs the directory
60787** that contains the rollback journal after the rollback
60788** journal is unlinked.
60789**
60790** The above is for a rollback-journal mode. For WAL mode, OFF continues
60791** to mean that no syncs ever occur. NORMAL means that the WAL is synced
60792** prior to the start of checkpoint and that the database file is synced
60793** at the conclusion of the checkpoint if the entire content of the WAL
60794** was written back into the database. But no sync operations occur for
60795** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
60796** file is synced following each commit operation, in addition to the
60797** syncs associated with NORMAL. There is no difference between FULL
60798** and EXTRA for WAL mode.
60799**
60800** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
60801** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
60802** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
60803** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
60804** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
60805** synchronous=FULL versus synchronous=NORMAL setting determines when
60806** the xSync primitive is called and is relevant to all platforms.
60807**
60808** Numeric values associated with these states are OFF==1, NORMAL=2,
60809** and FULL=3.
60810*/
60811SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(
60812 Pager *pPager, /* The pager to set safety level for */
60813 unsigned pgFlags /* Various flags */
60814){
60815 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK0x07;
60816 if( pPager->tempFile ){
60817 pPager->noSync = 1;
60818 pPager->fullSync = 0;
60819 pPager->extraSync = 0;
60820 }else{
60821 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF0x01 ?1:0;
60822 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL0x03 ?1:0;
60823 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA0x04 ?1:0;
60824 }
60825 if( pPager->noSync ){
60826 pPager->syncFlags = 0;
60827 }else if( pgFlags & PAGER_FULLFSYNC0x08 ){
60828 pPager->syncFlags = SQLITE_SYNC_FULL0x00003;
60829 }else{
60830 pPager->syncFlags = SQLITE_SYNC_NORMAL0x00002;
60831 }
60832 pPager->walSyncFlags = (pPager->syncFlags<<2);
60833 if( pPager->fullSync ){
60834 pPager->walSyncFlags |= pPager->syncFlags;
60835 }
60836 if( (pgFlags & PAGER_CKPT_FULLFSYNC0x10) && !pPager->noSync ){
60837 pPager->walSyncFlags |= (SQLITE_SYNC_FULL0x00003<<2);
60838 }
60839 if( pgFlags & PAGER_CACHESPILL0x20 ){
60840 pPager->doNotSpill &= ~SPILLFLAG_OFF0x01;
60841 }else{
60842 pPager->doNotSpill |= SPILLFLAG_OFF0x01;
60843 }
60844}
60845
60846/*
60847** The following global variable is incremented whenever the library
60848** attempts to open a temporary file. This information is used for
60849** testing and analysis only.
60850*/
60851#ifdef SQLITE_TEST
60852SQLITE_API int sqlite3_opentemp_count = 0;
60853#endif
60854
60855/*
60856** Open a temporary file.
60857**
60858** Write the file descriptor into *pFile. Return SQLITE_OK on success
60859** or some other error code if we fail. The OS will automatically
60860** delete the temporary file when it is closed.
60861**
60862** The flags passed to the VFS layer xOpen() call are those specified
60863** by parameter vfsFlags ORed with the following:
60864**
60865** SQLITE_OPEN_READWRITE
60866** SQLITE_OPEN_CREATE
60867** SQLITE_OPEN_EXCLUSIVE
60868** SQLITE_OPEN_DELETEONCLOSE
60869*/
60870static int pagerOpentemp(
60871 Pager *pPager, /* The pager object */
60872 sqlite3_file *pFile, /* Write the file descriptor here */
60873 int vfsFlags /* Flags passed through to the VFS */
60874){
60875 int rc; /* Return code */
60876
60877#ifdef SQLITE_TEST
60878 sqlite3_opentemp_count++; /* Used for testing and analysis only */
60879#endif
60880
60881 vfsFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
60882 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
60883 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
60884 assert( rc!=SQLITE_OK || isOpen(pFile) )((void) (0));
60885 return rc;
60886}
60887
60888/*
60889** Set the busy handler function.
60890**
60891** The pager invokes the busy-handler if sqlite3OsLock() returns
60892** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
60893** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
60894** lock. It does *not* invoke the busy handler when upgrading from
60895** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
60896** (which occurs during hot-journal rollback). Summary:
60897**
60898** Transition | Invokes xBusyHandler
60899** --------------------------------------------------------
60900** NO_LOCK -> SHARED_LOCK | Yes
60901** SHARED_LOCK -> RESERVED_LOCK | No
60902** SHARED_LOCK -> EXCLUSIVE_LOCK | No
60903** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
60904**
60905** If the busy-handler callback returns non-zero, the lock is
60906** retried. If it returns zero, then the SQLITE_BUSY error is
60907** returned to the caller of the pager API function.
60908*/
60909SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(
60910 Pager *pPager, /* Pager object */
60911 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
60912 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
60913){
60914 void **ap;
60915 pPager->xBusyHandler = xBusyHandler;
60916 pPager->pBusyHandlerArg = pBusyHandlerArg;
60917 ap = (void **)&pPager->xBusyHandler;
60918 assert( ((int(*)(void *))(ap[0]))==xBusyHandler )((void) (0));
60919 assert( ap[1]==pBusyHandlerArg )((void) (0));
60920 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER15, (void *)ap);
60921}
60922
60923/*
60924** Change the page size used by the Pager object. The new page size
60925** is passed in *pPageSize.
60926**
60927** If the pager is in the error state when this function is called, it
60928** is a no-op. The value returned is the error state error code (i.e.
60929** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
60930**
60931** Otherwise, if all of the following are true:
60932**
60933** * the new page size (value of *pPageSize) is valid (a power
60934** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
60935**
60936** * there are no outstanding page references, and
60937**
60938** * the database is either not an in-memory database or it is
60939** an in-memory database that currently consists of zero pages.
60940**
60941** then the pager object page size is set to *pPageSize.
60942**
60943** If the page size is changed, then this function uses sqlite3PagerMalloc()
60944** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
60945** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
60946** In all other cases, SQLITE_OK is returned.
60947**
60948** If the page size is not changed, either because one of the enumerated
60949** conditions above is not true, the pager was in error state when this
60950** function was called, or because the memory allocation attempt failed,
60951** then *pPageSize is set to the old, retained page size before returning.
60952*/
60953SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
60954 int rc = SQLITE_OK0;
60955
60956 /* It is not possible to do a full assert_pager_state() here, as this
60957 ** function may be called from within PagerOpen(), before the state
60958 ** of the Pager object is internally consistent.
60959 **
60960 ** At one point this function returned an error if the pager was in
60961 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
60962 ** there is at least one outstanding page reference, this function
60963 ** is a no-op for that case anyhow.
60964 */
60965
60966 u32 pageSize = *pPageSize;
60967 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) )((void) (0));
60968 if( (pPager->memDb==0 || pPager->dbSize==0)
60969 && sqlite3PcacheRefCount(pPager->pPCache)==0
60970 && pageSize && pageSize!=(u32)pPager->pageSize
60971 ){
60972 char *pNew = NULL((void*)0); /* New temp space */
60973 i64 nByte = 0;
60974
60975 if( pPager->eState>PAGER_OPEN0 && isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
60976 rc = sqlite3OsFileSize(pPager->fd, &nByte);
60977 }
60978 if( rc==SQLITE_OK0 ){
60979 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
60980 * cell header parser will never run off the end of the allocation */
60981 pNew = (char *)sqlite3PageMalloc(pageSize+8);
60982 if( !pNew ){
60983 rc = SQLITE_NOMEM_BKPT7;
60984 }else{
60985 memset(pNew+pageSize, 0, 8);
60986 }
60987 }
60988
60989 if( rc==SQLITE_OK0 ){
60990 pager_reset(pPager);
60991 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
60992 }
60993 if( rc==SQLITE_OK0 ){
60994 sqlite3PageFree(pPager->pTmpSpace);
60995 pPager->pTmpSpace = pNew;
60996 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
60997 pPager->pageSize = pageSize;
60998 pPager->lckPgno = (Pgno)(PENDING_BYTEsqlite3PendingByte/pageSize) + 1;
60999 }else{
61000 sqlite3PageFree(pNew);
61001 }
61002 }
61003
61004 *pPageSize = pPager->pageSize;
61005 if( rc==SQLITE_OK0 ){
61006 if( nReserve<0 ) nReserve = pPager->nReserve;
61007 assert( nReserve>=0 && nReserve<1000 )((void) (0));
61008 pPager->nReserve = (i16)nReserve;
61009 pagerFixMaplimit(pPager);
61010 }
61011 return rc;
61012}
61013
61014/*
61015** Return a pointer to the "temporary page" buffer held internally
61016** by the pager. This is a buffer that is big enough to hold the
61017** entire content of a database page. This buffer is used internally
61018** during rollback and will be overwritten whenever a rollback
61019** occurs. But other modules are free to use it too, as long as
61020** no rollbacks are happening.
61021*/
61022SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager *pPager){
61023 return pPager->pTmpSpace;
61024}
61025
61026/*
61027** Attempt to set the maximum database page count if mxPage is positive.
61028** Make no changes if mxPage is zero or negative. And never reduce the
61029** maximum page count below the current size of the database.
61030**
61031** Regardless of mxPage, return the current maximum page count.
61032*/
61033SQLITE_PRIVATEstatic Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){
61034 if( mxPage>0 ){
61035 pPager->mxPgno = mxPage;
61036 }
61037 assert( pPager->eState!=PAGER_OPEN )((void) (0)); /* Called only by OP_MaxPgcnt */
61038 /* assert( pPager->mxPgno>=pPager->dbSize ); */
61039 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
61040 ** less than the total number of valid pages in the database. But this
61041 ** may be less than Pager.dbSize, and so the assert() above is not valid */
61042 return pPager->mxPgno;
61043}
61044
61045/*
61046** The following set of routines are used to disable the simulated
61047** I/O error mechanism. These routines are used to avoid simulated
61048** errors in places where we do not care about errors.
61049**
61050** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
61051** and generate no code.
61052*/
61053#ifdef SQLITE_TEST
61054SQLITE_API extern int sqlite3_io_error_pending;
61055SQLITE_API extern int sqlite3_io_error_hit;
61056static int saved_cnt;
61057void disable_simulated_io_errors(void){
61058 saved_cnt = sqlite3_io_error_pending;
61059 sqlite3_io_error_pending = -1;
61060}
61061void enable_simulated_io_errors(void){
61062 sqlite3_io_error_pending = saved_cnt;
61063}
61064#else
61065# define disable_simulated_io_errors()
61066# define enable_simulated_io_errors()
61067#endif
61068
61069/*
61070** Read the first N bytes from the beginning of the file into memory
61071** that pDest points to.
61072**
61073** If the pager was opened on a transient file (zFilename==""), or
61074** opened on a file less than N bytes in size, the output buffer is
61075** zeroed and SQLITE_OK returned. The rationale for this is that this
61076** function is used to read database headers, and a new transient or
61077** zero sized database has a header than consists entirely of zeroes.
61078**
61079** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
61080** the error code is returned to the caller and the contents of the
61081** output buffer undefined.
61082*/
61083SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
61084 int rc = SQLITE_OK0;
61085 memset(pDest, 0, N);
61086 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
61087
61088 /* This routine is only called by btree immediately after creating
61089 ** the Pager object. There has not been an opportunity to transition
61090 ** to WAL mode yet.
61091 */
61092 assert( !pagerUseWal(pPager) )((void) (0));
61093
61094 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
61095 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
61096 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
61097 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
61098 rc = SQLITE_OK0;
61099 }
61100 }
61101 return rc;
61102}
61103
61104/*
61105** This function may only be called when a read-transaction is open on
61106** the pager. It returns the total number of pages in the database.
61107**
61108** However, if the file is between 1 and <page-size> bytes in size, then
61109** this is considered a 1 page file.
61110*/
61111SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
61112 assert( pPager->eState>=PAGER_READER )((void) (0));
61113 assert( pPager->eState!=PAGER_WRITER_FINISHED )((void) (0));
61114 *pnPage = (int)pPager->dbSize;
61115}
61116
61117
61118/*
61119** Try to obtain a lock of type locktype on the database file. If
61120** a similar or greater lock is already held, this function is a no-op
61121** (returning SQLITE_OK immediately).
61122**
61123** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
61124** the busy callback if the lock is currently not available. Repeat
61125** until the busy callback returns false or until the attempt to
61126** obtain the lock succeeds.
61127**
61128** Return SQLITE_OK on success and an error code if we cannot obtain
61129** the lock. If the lock is obtained successfully, set the Pager.state
61130** variable to locktype before returning.
61131*/
61132static int pager_wait_on_lock(Pager *pPager, int locktype){
61133 int rc; /* Return code */
61134
61135 /* Check that this is either a no-op (because the requested lock is
61136 ** already held), or one of the transitions that the busy-handler
61137 ** may be invoked during, according to the comment above
61138 ** sqlite3PagerSetBusyhandler().
61139 */
61140 assert( (pPager->eLock>=locktype)((void) (0))
61141 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)((void) (0))
61142 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)((void) (0))
61143 )((void) (0));
61144
61145 do {
61146 rc = pagerLockDb(pPager, locktype);
61147 }while( rc==SQLITE_BUSY5 && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
61148 return rc;
61149}
61150
61151/*
61152** Function assertTruncateConstraint(pPager) checks that one of the
61153** following is true for all dirty pages currently in the page-cache:
61154**
61155** a) The page number is less than or equal to the size of the
61156** current database image, in pages, OR
61157**
61158** b) if the page content were written at this time, it would not
61159** be necessary to write the current content out to the sub-journal.
61160**
61161** If the condition asserted by this function were not true, and the
61162** dirty page were to be discarded from the cache via the pagerStress()
61163** routine, pagerStress() would not write the current page content to
61164** the database file. If a savepoint transaction were rolled back after
61165** this happened, the correct behavior would be to restore the current
61166** content of the page. However, since this content is not present in either
61167** the database file or the portion of the rollback journal and
61168** sub-journal rolled back the content could not be restored and the
61169** database image would become corrupt. It is therefore fortunate that
61170** this circumstance cannot arise.
61171*/
61172#if defined(SQLITE_DEBUG)
61173static void assertTruncateConstraintCb(PgHdr *pPg){
61174 Pager *pPager = pPg->pPager;
61175 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
61176 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
61177 Pgno pgno = pPg->pgno;
61178 int i;
61179 for(i=0; i<pPg->pPager->nSavepoint; i++){
61180 PagerSavepoint *p = &pPager->aSavepoint[i];
61181 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) )((void) (0));
61182 }
61183 }
61184}
61185static void assertTruncateConstraint(Pager *pPager){
61186 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
61187}
61188#else
61189# define assertTruncateConstraint(pPager)
61190#endif
61191
61192/*
61193** Truncate the in-memory database file image to nPage pages. This
61194** function does not actually modify the database file on disk. It
61195** just sets the internal state of the pager object so that the
61196** truncation will be done when the current transaction is committed.
61197**
61198** This function is only called right before committing a transaction.
61199** Once this function has been called, the transaction must either be
61200** rolled back or committed. It is not safe to call this function and
61201** then continue writing to the database.
61202*/
61203SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
61204 assert( pPager->dbSize>=nPage || CORRUPT_DB )((void) (0));
61205 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
61206 pPager->dbSize = nPage;
61207
61208 /* At one point the code here called assertTruncateConstraint() to
61209 ** ensure that all pages being truncated away by this operation are,
61210 ** if one or more savepoints are open, present in the savepoint
61211 ** journal so that they can be restored if the savepoint is rolled
61212 ** back. This is no longer necessary as this function is now only
61213 ** called right before committing a transaction. So although the
61214 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
61215 ** they cannot be rolled back. So the assertTruncateConstraint() call
61216 ** is no longer correct. */
61217}
61218
61219
61220/*
61221** This function is called before attempting a hot-journal rollback. It
61222** syncs the journal file to disk, then sets pPager->journalHdr to the
61223** size of the journal file so that the pager_playback() routine knows
61224** that the entire journal file has been synced.
61225**
61226** Syncing a hot-journal to disk before attempting to roll it back ensures
61227** that if a power-failure occurs during the rollback, the process that
61228** attempts rollback following system recovery sees the same journal
61229** content as this process.
61230**
61231** If everything goes as planned, SQLITE_OK is returned. Otherwise,
61232** an SQLite error code.
61233*/
61234static int pagerSyncHotJournal(Pager *pPager){
61235 int rc = SQLITE_OK0;
61236 if( !pPager->noSync ){
61237 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL0x00002);
61238 }
61239 if( rc==SQLITE_OK0 ){
61240 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
61241 }
61242 return rc;
61243}
61244
61245#if SQLITE_MAX_MMAP_SIZE20971520>0
61246/*
61247** Obtain a reference to a memory mapped page object for page number pgno.
61248** The new object will use the pointer pData, obtained from xFetch().
61249** If successful, set *ppPage to point to the new page reference
61250** and return SQLITE_OK. Otherwise, return an SQLite error code and set
61251** *ppPage to zero.
61252**
61253** Page references obtained by calling this function should be released
61254** by calling pagerReleaseMapPage().
61255*/
61256static int pagerAcquireMapPage(
61257 Pager *pPager, /* Pager object */
61258 Pgno pgno, /* Page number */
61259 void *pData, /* xFetch()'d data for this page */
61260 PgHdr **ppPage /* OUT: Acquired page object */
61261){
61262 PgHdr *p; /* Memory mapped page to return */
61263
61264 if( pPager->pMmapFreelist ){
61265 *ppPage = p = pPager->pMmapFreelist;
61266 pPager->pMmapFreelist = p->pDirty;
61267 p->pDirty = 0;
61268 assert( pPager->nExtra>=8 )((void) (0));
61269 memset(p->pExtra, 0, 8);
61270 }else{
61271 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
61272 if( p==0 ){
61273 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
61274 return SQLITE_NOMEM_BKPT7;
61275 }
61276 p->pExtra = (void *)&p[1];
61277 assert( EIGHT_BYTE_ALIGNMENT( p->pExtra ) )((void) (0));
61278 p->flags = PGHDR_MMAP0x020;
61279 p->nRef = 1;
61280 p->pPager = pPager;
61281 }
61282
61283 assert( p->pExtra==(void *)&p[1] )((void) (0));
61284 assert( p->pPage==0 )((void) (0));
61285 assert( p->flags==PGHDR_MMAP )((void) (0));
61286 assert( p->pPager==pPager )((void) (0));
61287 assert( p->nRef==1 )((void) (0));
61288
61289 p->pgno = pgno;
61290 p->pData = pData;
61291 pPager->nMmapOut++;
61292
61293 return SQLITE_OK0;
61294}
61295#endif
61296
61297/*
61298** Release a reference to page pPg. pPg must have been returned by an
61299** earlier call to pagerAcquireMapPage().
61300*/
61301static void pagerReleaseMapPage(PgHdr *pPg){
61302 Pager *pPager = pPg->pPager;
61303 pPager->nMmapOut--;
61304 pPg->pDirty = pPager->pMmapFreelist;
61305 pPager->pMmapFreelist = pPg;
61306
61307 assert( pPager->fd->pMethods->iVersion>=3 )((void) (0));
61308 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
61309}
61310
61311/*
61312** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
61313*/
61314static void pagerFreeMapHdrs(Pager *pPager){
61315 PgHdr *p;
61316 PgHdr *pNext;
61317 for(p=pPager->pMmapFreelist; p; p=pNext){
61318 pNext = p->pDirty;
61319 sqlite3_free(p);
61320 }
61321}
61322
61323/* Verify that the database file has not be deleted or renamed out from
61324** under the pager. Return SQLITE_OK if the database is still where it ought
61325** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
61326** code from sqlite3OsAccess()) if the database has gone missing.
61327*/
61328static int databaseIsUnmoved(Pager *pPager){
61329 int bHasMoved = 0;
61330 int rc;
61331
61332 if( pPager->tempFile ) return SQLITE_OK0;
61333 if( pPager->dbSize==0 ) return SQLITE_OK0;
61334 assert( pPager->zFilename && pPager->zFilename[0] )((void) (0));
61335 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED20, &bHasMoved);
61336 if( rc==SQLITE_NOTFOUND12 ){
61337 /* If the HAS_MOVED file-control is unimplemented, assume that the file
61338 ** has not been moved. That is the historical behavior of SQLite: prior to
61339 ** version 3.8.3, it never checked */
61340 rc = SQLITE_OK0;
61341 }else if( rc==SQLITE_OK0 && bHasMoved ){
61342 rc = SQLITE_READONLY_DBMOVED(8 | (4<<8));
61343 }
61344 return rc;
61345}
61346
61347
61348/*
61349** Shutdown the page cache. Free all memory and close all files.
61350**
61351** If a transaction was in progress when this routine is called, that
61352** transaction is rolled back. All outstanding pages are invalidated
61353** and their memory is freed. Any attempt to use a page associated
61354** with this page cache after this function returns will likely
61355** result in a coredump.
61356**
61357** This function always succeeds. If a transaction is active an attempt
61358** is made to roll it back. If an error occurs during the rollback
61359** a hot journal may be left in the filesystem but no error is returned
61360** to the caller.
61361*/
61362SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
61363 u8 *pTmp = (u8*)pPager->pTmpSpace;
61364 assert( db || pagerUseWal(pPager)==0 )((void) (0));
61365 assert( assert_pager_state(pPager) )((void) (0));
61366 disable_simulated_io_errors();
61367 sqlite3BeginBenignMalloc();
61368 pagerFreeMapHdrs(pPager);
61369 /* pPager->errCode = 0; */
61370 pPager->exclusiveMode = 0;
61371#ifndef SQLITE_OMIT_WAL
61372 {
61373 u8 *a = 0;
61374 assert( db || pPager->pWal==0 )((void) (0));
61375 if( db && 0==(db->flags & SQLITE_NoCkptOnClose0x00000800)
61376 && SQLITE_OK0==databaseIsUnmoved(pPager)
61377 ){
61378 a = pTmp;
61379 }
61380 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
61381 pPager->pWal = 0;
61382 }
61383#endif
61384 pager_reset(pPager);
61385 if( MEMDBpPager->memDb ){
61386 pager_unlock(pPager);
61387 }else{
61388 /* If it is open, sync the journal file before calling UnlockAndRollback.
61389 ** If this is not done, then an unsynced portion of the open journal
61390 ** file may be played back into the database. If a power failure occurs
61391 ** while this is happening, the database could become corrupt.
61392 **
61393 ** If an error occurs while trying to sync the journal, shift the pager
61394 ** into the ERROR state. This causes UnlockAndRollback to unlock the
61395 ** database and close the journal file without attempting to roll it
61396 ** back or finalize it. The next database user will have to do hot-journal
61397 ** rollback before accessing the database file.
61398 */
61399 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
61400 pager_error(pPager, pagerSyncHotJournal(pPager));
61401 }
61402 pagerUnlockAndRollback(pPager);
61403 }
61404 sqlite3EndBenignMalloc();
61405 enable_simulated_io_errors();
61406 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
61407 IOTRACE(("CLOSE %p\n", pPager))
61408 sqlite3OsClose(pPager->jfd);
61409 sqlite3OsClose(pPager->fd);
61410 sqlite3PageFree(pTmp);
61411 sqlite3PcacheClose(pPager->pPCache);
61412 assert( !pPager->aSavepoint && !pPager->pInJournal )((void) (0));
61413 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) )((void) (0));
61414
61415 sqlite3_free(pPager);
61416 return SQLITE_OK0;
61417}
61418
61419#if !defined(NDEBUG1) || defined(SQLITE_TEST)
61420/*
61421** Return the page number for page pPg.
61422*/
61423SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage *pPg){
61424 return pPg->pgno;
61425}
61426#endif
61427
61428/*
61429** Increment the reference count for page pPg.
61430*/
61431SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage *pPg){
61432 sqlite3PcacheRef(pPg);
61433}
61434
61435/*
61436** Sync the journal. In other words, make sure all the pages that have
61437** been written to the journal have actually reached the surface of the
61438** disk and can be restored in the event of a hot-journal rollback.
61439**
61440** If the Pager.noSync flag is set, then this function is a no-op.
61441** Otherwise, the actions required depend on the journal-mode and the
61442** device characteristics of the file-system, as follows:
61443**
61444** * If the journal file is an in-memory journal file, no action need
61445** be taken.
61446**
61447** * Otherwise, if the device does not support the SAFE_APPEND property,
61448** then the nRec field of the most recently written journal header
61449** is updated to contain the number of journal records that have
61450** been written following it. If the pager is operating in full-sync
61451** mode, then the journal file is synced before this field is updated.
61452**
61453** * If the device does not support the SEQUENTIAL property, then
61454** journal file is synced.
61455**
61456** Or, in pseudo-code:
61457**
61458** if( NOT <in-memory journal> ){
61459** if( NOT SAFE_APPEND ){
61460** if( <full-sync mode> ) xSync(<journal file>);
61461** <update nRec field>
61462** }
61463** if( NOT SEQUENTIAL ) xSync(<journal file>);
61464** }
61465**
61466** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
61467** page currently held in memory before returning SQLITE_OK. If an IO
61468** error is encountered, then the IO error code is returned to the caller.
61469*/
61470static int syncJournal(Pager *pPager, int newHdr){
61471 int rc; /* Return code */
61472
61473 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
61474 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
61475 )((void) (0));
61476 assert( assert_pager_state(pPager) )((void) (0));
61477 assert( !pagerUseWal(pPager) )((void) (0));
61478
61479 rc = sqlite3PagerExclusiveLock(pPager);
61480 if( rc!=SQLITE_OK0 ) return rc;
61481
61482 if( !pPager->noSync ){
61483 assert( !pPager->tempFile )((void) (0));
61484 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY4 ){
61485 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
61486 assert( isOpen(pPager->jfd) )((void) (0));
61487
61488 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
61489 /* This block deals with an obscure problem. If the last connection
61490 ** that wrote to this database was operating in persistent-journal
61491 ** mode, then the journal file may at this point actually be larger
61492 ** than Pager.journalOff bytes. If the next thing in the journal
61493 ** file happens to be a journal-header (written as part of the
61494 ** previous connection's transaction), and a crash or power-failure
61495 ** occurs after nRec is updated but before this connection writes
61496 ** anything else to the journal file (or commits/rolls back its
61497 ** transaction), then SQLite may become confused when doing the
61498 ** hot-journal rollback following recovery. It may roll back all
61499 ** of this connections data, then proceed to rolling back the old,
61500 ** out-of-date data that follows it. Database corruption.
61501 **
61502 ** To work around this, if the journal file does appear to contain
61503 ** a valid header following Pager.journalOff, then write a 0x00
61504 ** byte to the start of it to prevent it from being recognized.
61505 **
61506 ** Variable iNextHdrOffset is set to the offset at which this
61507 ** problematic header will occur, if it exists. aMagic is used
61508 ** as a temporary buffer to inspect the first couple of bytes of
61509 ** the potential journal header.
61510 */
61511 i64 iNextHdrOffset;
61512 u8 aMagic[8];
61513 u8 zHeader[sizeof(aJournalMagic)+4];
61514
61515 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
61516 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],pPager
->nRec)
;
61517
61518 iNextHdrOffset = journalHdrOffset(pPager);
61519 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
61520 if( rc==SQLITE_OK0 && 0==memcmp(aMagic, aJournalMagic, 8) ){
61521 static const u8 zerobyte = 0;
61522 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
61523 }
61524 if( rc!=SQLITE_OK0 && rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
61525 return rc;
61526 }
61527
61528 /* Write the nRec value into the journal file header. If in
61529 ** full-synchronous mode, sync the journal first. This ensures that
61530 ** all data has really hit the disk before nRec is updated to mark
61531 ** it as a candidate for rollback.
61532 **
61533 ** This is not required if the persistent media supports the
61534 ** SAFE_APPEND property. Because in this case it is not possible
61535 ** for garbage data to be appended to the file, the nRec field
61536 ** is populated with 0xFFFFFFFF when the journal header is written
61537 ** and never needs to be updated.
61538 */
61539 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
61540 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
61541 IOTRACE(("JSYNC %p\n", pPager))
61542 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
61543 if( rc!=SQLITE_OK0 ) return rc;
61544 }
61545 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
61546 rc = sqlite3OsWrite(
61547 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
61548 );
61549 if( rc!=SQLITE_OK0 ) return rc;
61550 }
61551 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
61552 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
61553 IOTRACE(("JSYNC %p\n", pPager))
61554 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
61555 (pPager->syncFlags==SQLITE_SYNC_FULL0x00003?SQLITE_SYNC_DATAONLY0x00010:0)
61556 );
61557 if( rc!=SQLITE_OK0 ) return rc;
61558 }
61559
61560 pPager->journalHdr = pPager->journalOff;
61561 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
61562 pPager->nRec = 0;
61563 rc = writeJournalHdr(pPager);
61564 if( rc!=SQLITE_OK0 ) return rc;
61565 }
61566 }else{
61567 pPager->journalHdr = pPager->journalOff;
61568 }
61569 }
61570
61571 /* Unless the pager is in noSync mode, the journal file was just
61572 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
61573 ** all pages.
61574 */
61575 sqlite3PcacheClearSyncFlags(pPager->pPCache);
61576 pPager->eState = PAGER_WRITER_DBMOD4;
61577 assert( assert_pager_state(pPager) )((void) (0));
61578 return SQLITE_OK0;
61579}
61580
61581/*
61582** The argument is the first in a linked list of dirty pages connected
61583** by the PgHdr.pDirty pointer. This function writes each one of the
61584** in-memory pages in the list to the database file. The argument may
61585** be NULL, representing an empty list. In this case this function is
61586** a no-op.
61587**
61588** The pager must hold at least a RESERVED lock when this function
61589** is called. Before writing anything to the database file, this lock
61590** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
61591** SQLITE_BUSY is returned and no data is written to the database file.
61592**
61593** If the pager is a temp-file pager and the actual file-system file
61594** is not yet open, it is created and opened before any data is
61595** written out.
61596**
61597** Once the lock has been upgraded and, if necessary, the file opened,
61598** the pages are written out to the database file in list order. Writing
61599** a page is skipped if it meets either of the following criteria:
61600**
61601** * The page number is greater than Pager.dbSize, or
61602** * The PGHDR_DONT_WRITE flag is set on the page.
61603**
61604** If writing out a page causes the database file to grow, Pager.dbFileSize
61605** is updated accordingly. If page 1 is written out, then the value cached
61606** in Pager.dbFileVers[] is updated to match the new value stored in
61607** the database file.
61608**
61609** If everything is successful, SQLITE_OK is returned. If an IO error
61610** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
61611** be obtained, SQLITE_BUSY is returned.
61612*/
61613static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
61614 int rc = SQLITE_OK0; /* Return code */
61615
61616 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
61617 assert( !pagerUseWal(pPager) )((void) (0));
61618 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
61619 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
61620 assert( isOpen(pPager->fd) || pList->pDirty==0 )((void) (0));
61621
61622 /* If the file is a temp-file has not yet been opened, open it now. It
61623 ** is not possible for rc to be other than SQLITE_OK if this branch
61624 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
61625 */
61626 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
61627 assert( pPager->tempFile && rc==SQLITE_OK )((void) (0));
61628 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
61629 }
61630
61631 /* Before the first write, give the VFS a hint of what the final
61632 ** file size will be.
61633 */
61634 assert( rc!=SQLITE_OK || isOpen(pPager->fd) )((void) (0));
61635 if( rc==SQLITE_OK0
61636 && pPager->dbHintSize<pPager->dbSize
61637 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
61638 ){
61639 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
61640 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &szFile);
61641 pPager->dbHintSize = pPager->dbSize;
61642 }
61643
61644 while( rc==SQLITE_OK0 && pList ){
61645 Pgno pgno = pList->pgno;
61646
61647 /* If there are dirty pages in the page cache with page numbers greater
61648 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
61649 ** make the file smaller (presumably by auto-vacuum code). Do not write
61650 ** any such pages to the file.
61651 **
61652 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
61653 ** set (set by sqlite3PagerDontWrite()).
61654 */
61655 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE0x010) ){
61656 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
61657 char *pData; /* Data to write */
61658
61659 assert( (pList->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
61660 if( pList->pgno==1 ) pager_write_changecounter(pList);
61661
61662 pData = pList->pData;
61663
61664 /* Write out the page data. */
61665 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
61666
61667 /* If page 1 was just written, update Pager.dbFileVers to match
61668 ** the value now stored in the database file. If writing this
61669 ** page caused the database file to grow, update dbFileSize.
61670 */
61671 if( pgno==1 ){
61672 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
61673 }
61674 if( pgno>pPager->dbFileSize ){
61675 pPager->dbFileSize = pgno;
61676 }
61677 pPager->aStat[PAGER_STAT_WRITE2]++;
61678
61679 /* Update any backup objects copying the contents of this pager. */
61680 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
61681
61682 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
61683 PAGERID(pPager), pgno, pager_pagehash(pList)));
61684 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
61685 PAGER_INCR(sqlite3_pager_writedb_count);
61686 }else{
61687 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
61688 }
61689 pager_set_pagehash(pList);
61690 pList = pList->pDirty;
61691 }
61692
61693 return rc;
61694}
61695
61696/*
61697** Ensure that the sub-journal file is open. If it is already open, this
61698** function is a no-op.
61699**
61700** SQLITE_OK is returned if everything goes according to plan. An
61701** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
61702** fails.
61703*/
61704static int openSubJournal(Pager *pPager){
61705 int rc = SQLITE_OK0;
61706 if( !isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
61707 const int flags = SQLITE_OPEN_SUBJOURNAL0x00002000 | SQLITE_OPEN_READWRITE0x00000002
61708 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_EXCLUSIVE0x00000010
61709 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
61710 int nStmtSpill = sqlite3Config.nStmtSpill;
61711 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 || pPager->subjInMemory ){
61712 nStmtSpill = -1;
61713 }
61714 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
61715 }
61716 return rc;
61717}
61718
61719/*
61720** Append a record of the current state of page pPg to the sub-journal.
61721**
61722** If successful, set the bit corresponding to pPg->pgno in the bitvecs
61723** for all open savepoints before returning.
61724**
61725** This function returns SQLITE_OK if everything is successful, an IO
61726** error code if the attempt to write to the sub-journal fails, or
61727** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
61728** bitvec.
61729*/
61730static int subjournalPage(PgHdr *pPg){
61731 int rc = SQLITE_OK0;
61732 Pager *pPager = pPg->pPager;
61733 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
61734
61735 /* Open the sub-journal, if it has not already been opened */
61736 assert( pPager->useJournal )((void) (0));
61737 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) )((void) (0));
61738 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 )((void) (0));
61739 assert( pagerUseWal(pPager)((void) (0))
61740 || pageInJournal(pPager, pPg)((void) (0))
61741 || pPg->pgno>pPager->dbOrigSize((void) (0))
61742 )((void) (0));
61743 rc = openSubJournal(pPager);
61744
61745 /* If the sub-journal was opened successfully (or was already open),
61746 ** write the journal record into the file. */
61747 if( rc==SQLITE_OK0 ){
61748 void *pData = pPg->pData;
61749 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
61750 char *pData2;
61751 pData2 = pData;
61752 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
61753 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
61754 if( rc==SQLITE_OK0 ){
61755 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
61756 }
61757 }
61758 }
61759 if( rc==SQLITE_OK0 ){
61760 pPager->nSubRec++;
61761 assert( pPager->nSavepoint>0 )((void) (0));
61762 rc = addToSavepointBitvecs(pPager, pPg->pgno);
61763 }
61764 return rc;
61765}
61766static int subjournalPageIfRequired(PgHdr *pPg){
61767 if( subjRequiresPage(pPg) ){
61768 return subjournalPage(pPg);
61769 }else{
61770 return SQLITE_OK0;
61771 }
61772}
61773
61774/*
61775** This function is called by the pcache layer when it has reached some
61776** soft memory limit. The first argument is a pointer to a Pager object
61777** (cast as a void*). The pager is always 'purgeable' (not an in-memory
61778** database). The second argument is a reference to a page that is
61779** currently dirty but has no outstanding references. The page
61780** is always associated with the Pager object passed as the first
61781** argument.
61782**
61783** The job of this function is to make pPg clean by writing its contents
61784** out to the database file, if possible. This may involve syncing the
61785** journal file.
61786**
61787** If successful, sqlite3PcacheMakeClean() is called on the page and
61788** SQLITE_OK returned. If an IO error occurs while trying to make the
61789** page clean, the IO error code is returned. If the page cannot be
61790** made clean for some other reason, but no error occurs, then SQLITE_OK
61791** is returned by sqlite3PcacheMakeClean() is not called.
61792*/
61793static int pagerStress(void *p, PgHdr *pPg){
61794 Pager *pPager = (Pager *)p;
61795 int rc = SQLITE_OK0;
61796
61797 assert( pPg->pPager==pPager )((void) (0));
61798 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
61799
61800 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
61801 ** journal (and adding a new header) is not allowed. This occurs
61802 ** during calls to sqlite3PagerWrite() while trying to journal multiple
61803 ** pages belonging to the same sector.
61804 **
61805 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
61806 ** regardless of whether or not a sync is required. This is set during
61807 ** a rollback or by user request, respectively.
61808 **
61809 ** Spilling is also prohibited when in an error state since that could
61810 ** lead to database corruption. In the current implementation it
61811 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
61812 ** while in the error state, hence it is impossible for this routine to
61813 ** be called in the error state. Nevertheless, we include a NEVER()
61814 ** test for the error state as a safeguard against future changes.
61815 */
61816 if( NEVER(pPager->errCode)(pPager->errCode) ) return SQLITE_OK0;
61817 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
61818 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
61819 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
61820 if( pPager->doNotSpill
61821 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK0x02|SPILLFLAG_OFF0x01))!=0
61822 || (pPg->flags & PGHDR_NEED_SYNC0x008)!=0)
61823 ){
61824 return SQLITE_OK0;
61825 }
61826
61827 pPager->aStat[PAGER_STAT_SPILL3]++;
61828 pPg->pDirty = 0;
61829 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
61830 /* Write a single frame for this page to the log. */
61831 rc = subjournalPageIfRequired(pPg);
61832 if( rc==SQLITE_OK0 ){
61833 rc = pagerWalFrames(pPager, pPg, 0, 0);
61834 }
61835 }else{
61836
61837#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
61838 if( pPager->tempFile==0 ){
61839 rc = sqlite3JournalCreate(pPager->jfd);
61840 if( rc!=SQLITE_OK0 ) return pager_error(pPager, rc);
61841 }
61842#endif
61843
61844 /* Sync the journal file if required. */
61845 if( pPg->flags&PGHDR_NEED_SYNC0x008
61846 || pPager->eState==PAGER_WRITER_CACHEMOD3
61847 ){
61848 rc = syncJournal(pPager, 1);
61849 }
61850
61851 /* Write the contents of the page out to the database file. */
61852 if( rc==SQLITE_OK0 ){
61853 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
61854 rc = pager_write_pagelist(pPager, pPg);
61855 }
61856 }
61857
61858 /* Mark the page as clean. */
61859 if( rc==SQLITE_OK0 ){
61860 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
61861 sqlite3PcacheMakeClean(pPg);
61862 }
61863
61864 return pager_error(pPager, rc);
61865}
61866
61867/*
61868** Flush all unreferenced dirty pages to disk.
61869*/
61870SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager *pPager){
61871 int rc = pPager->errCode;
61872 if( !MEMDBpPager->memDb ){
61873 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
61874 assert( assert_pager_state(pPager) )((void) (0));
61875 while( rc==SQLITE_OK0 && pList ){
61876 PgHdr *pNext = pList->pDirty;
61877 if( pList->nRef==0 ){
61878 rc = pagerStress((void*)pPager, pList);
61879 }
61880 pList = pNext;
61881 }
61882 }
61883
61884 return rc;
61885}
61886
61887/*
61888** Allocate and initialize a new Pager object and put a pointer to it
61889** in *ppPager. The pager should eventually be freed by passing it
61890** to sqlite3PagerClose().
61891**
61892** The zFilename argument is the path to the database file to open.
61893** If zFilename is NULL then a randomly-named temporary file is created
61894** and used as the file to be cached. Temporary files are be deleted
61895** automatically when they are closed. If zFilename is ":memory:" then
61896** all information is held in cache. It is never written to disk.
61897** This can be used to implement an in-memory database.
61898**
61899** The nExtra parameter specifies the number of bytes of space allocated
61900** along with each page reference. This space is available to the user
61901** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
61902** first 8 bytes of this space are zeroed but the remainder is uninitialized.
61903** (The extra space is used by btree as the MemPage object.)
61904**
61905** The flags argument is used to specify properties that affect the
61906** operation of the pager. It should be passed some bitwise combination
61907** of the PAGER_* flags.
61908**
61909** The vfsFlags parameter is a bitmask to pass to the flags parameter
61910** of the xOpen() method of the supplied VFS when opening files.
61911**
61912** If the pager object is allocated and the specified file opened
61913** successfully, SQLITE_OK is returned and *ppPager set to point to
61914** the new pager object. If an error occurs, *ppPager is set to NULL
61915** and error code returned. This function may return SQLITE_NOMEM
61916** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
61917** various SQLITE_IO_XXX errors.
61918*/
61919SQLITE_PRIVATEstatic int sqlite3PagerOpen(
61920 sqlite3_vfs *pVfs, /* The virtual file system to use */
61921 Pager **ppPager, /* OUT: Return the Pager structure here */
61922 const char *zFilename, /* Name of the database file to open */
61923 int nExtra, /* Extra bytes append to each in-memory page */
61924 int flags, /* flags controlling this file */
61925 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
61926 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
61927){
61928 u8 *pPtr;
61929 Pager *pPager = 0; /* Pager object to allocate and return */
61930 int rc = SQLITE_OK0; /* Return code */
61931 int tempFile = 0; /* True for temp files (incl. in-memory files) */
61932 int memDb = 0; /* True if this is an in-memory file */
61933 int memJM = 0; /* Memory journal mode */
61934 int readOnly = 0; /* True if this is a read-only file */
61935 int journalFileSize; /* Bytes to allocate for each journal fd */
61936 char *zPathname = 0; /* Full path to database file */
61937 int nPathname = 0; /* Number of bytes in zPathname */
61938 int useJournal = (flags & PAGER_OMIT_JOURNAL0x0001)==0; /* False to omit journal */
61939 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
61940 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE4096; /* Default page size */
61941 const char *zUri = 0; /* URI args to copy */
61942 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
61943
61944 /* Figure out how much space is required for each journal file-handle
61945 ** (there are two of them, the main journal and the sub-journal). */
61946 journalFileSize = ROUND8(sqlite3JournalSize(pVfs))(((sqlite3JournalSize(pVfs))+7)&~7);
61947
61948 /* Set the output variable to NULL in case an error occurs. */
61949 *ppPager = 0;
61950
61951#ifndef SQLITE_OMIT_MEMORYDB
61952 if( flags & PAGER_MEMORY0x0002 ){
61953 memDb = 1;
61954 if( zFilename && zFilename[0] ){
61955 zPathname = sqlite3DbStrDup(0, zFilename);
61956 if( zPathname==0 ) return SQLITE_NOMEM_BKPT7;
61957 nPathname = sqlite3Strlen30(zPathname);
61958 zFilename = 0;
61959 }
61960 }
61961#endif
61962
61963 /* Compute and store the full pathname in an allocated buffer pointed
61964 ** to by zPathname, length nPathname. Or, if this is a temporary file,
61965 ** leave both nPathname and zPathname set to 0.
61966 */
61967 if( zFilename && zFilename[0] ){
61968 const char *z;
61969 nPathname = pVfs->mxPathname+1;
61970 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
61971 if( zPathname==0 ){
61972 return SQLITE_NOMEM_BKPT7;
61973 }
61974 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
61975 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
61976 if( rc!=SQLITE_OK0 ){
61977 if( rc==SQLITE_OK_SYMLINK(0 | (2<<8)) ){
61978 if( vfsFlags & SQLITE_OPEN_NOFOLLOW0x01000000 ){
61979 rc = SQLITE_CANTOPEN_SYMLINK(14 | (6<<8));
61980 }else{
61981 rc = SQLITE_OK0;
61982 }
61983 }
61984 }
61985 nPathname = sqlite3Strlen30(zPathname);
61986 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
61987 while( *z ){
61988 z += strlen(z)+1;
61989 z += strlen(z)+1;
61990 }
61991 nUriByte = (int)(&z[1] - zUri);
61992 assert( nUriByte>=1 )((void) (0));
61993 if( rc==SQLITE_OK0 && nPathname+8>pVfs->mxPathname ){
61994 /* This branch is taken when the journal path required by
61995 ** the database being opened will be more than pVfs->mxPathname
61996 ** bytes in length. This means the database cannot be opened,
61997 ** as it will not be possible to open the journal file or even
61998 ** check for a hot-journal before reading.
61999 */
62000 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(62000);
62001 }
62002 if( rc!=SQLITE_OK0 ){
62003 sqlite3DbFree(0, zPathname);
62004 return rc;
62005 }
62006 }
62007
62008 /* Allocate memory for the Pager structure, PCache object, the
62009 ** three file descriptors, the database file name and the journal
62010 ** file name. The layout in memory is as follows:
62011 **
62012 ** Pager object (sizeof(Pager) bytes)
62013 ** PCache object (sqlite3PcacheSize() bytes)
62014 ** Database file handle (pVfs->szOsFile bytes)
62015 ** Sub-journal file handle (journalFileSize bytes)
62016 ** Main journal file handle (journalFileSize bytes)
62017 ** Ptr back to the Pager (sizeof(Pager*) bytes)
62018 ** \0\0\0\0 database prefix (4 bytes)
62019 ** Database file name (nPathname+1 bytes)
62020 ** URI query parameters (nUriByte bytes)
62021 ** Journal filename (nPathname+8+1 bytes)
62022 ** WAL filename (nPathname+4+1 bytes)
62023 ** \0\0\0 terminator (3 bytes)
62024 **
62025 ** Some 3rd-party software, over which we have no control, depends on
62026 ** the specific order of the filenames and the \0 separators between them
62027 ** so that it can (for example) find the database filename given the WAL
62028 ** filename without using the sqlite3_filename_database() API. This is a
62029 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
62030 ** software is in widespread use, so we try to avoid changing the filename
62031 ** order and formatting if possible. In particular, the details of the
62032 ** filename format expected by 3rd-party software should be as follows:
62033 **
62034 ** - Main Database Path
62035 ** - \0
62036 ** - Multiple URI components consisting of:
62037 ** - Key
62038 ** - \0
62039 ** - Value
62040 ** - \0
62041 ** - \0
62042 ** - Journal Path
62043 ** - \0
62044 ** - WAL Path (zWALName)
62045 ** - \0
62046 **
62047 ** The sqlite3_create_filename() interface and the databaseFilename() utility
62048 ** that is used by sqlite3_filename_database() and kin also depend on the
62049 ** specific formatting and order of the various filenames, so if the format
62050 ** changes here, be sure to change it there as well.
62051 */
62052 assert( SQLITE_PTRSIZE==sizeof(Pager*) )((void) (0));
62053 pPtr = (u8 *)sqlite3MallocZero(
62054 ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7) + /* Pager structure */
62055 ROUND8(pcacheSize)(((pcacheSize)+7)&~7) + /* PCache object */
62056 ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7) + /* The main db file */
62057 journalFileSize * 2 + /* The two journal files */
62058 SQLITE_PTRSIZE8 + /* Space to hold a pointer */
62059 4 + /* Database prefix */
62060 nPathname + 1 + /* database filename */
62061 nUriByte + /* query parameters */
62062 nPathname + 8 + 1 + /* Journal filename */
62063#ifndef SQLITE_OMIT_WAL
62064 nPathname + 4 + 1 + /* WAL filename */
62065#endif
62066 3 /* Terminator */
62067 );
62068 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) )((void) (0));
62069 if( !pPtr ){
62070 sqlite3DbFree(0, zPathname);
62071 return SQLITE_NOMEM_BKPT7;
62072 }
62073 pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7);
62074 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize)(((pcacheSize)+7)&~7);
62075 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7);
62076 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
62077 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
62078 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) )((void) (0));
62079 memcpy(pPtr, &pPager, SQLITE_PTRSIZE8); pPtr += SQLITE_PTRSIZE8;
62080
62081 /* Fill in the Pager.zFilename and pPager.zQueryParam fields */
62082 pPtr += 4; /* Skip zero prefix */
62083 pPager->zFilename = (char*)pPtr;
62084 if( nPathname>0 ){
62085 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname + 1;
62086 if( zUri ){
62087 memcpy(pPtr, zUri, nUriByte); pPtr += nUriByte;
62088 }else{
62089 pPtr++;
62090 }
62091 }
62092
62093
62094 /* Fill in Pager.zJournal */
62095 if( nPathname>0 ){
62096 pPager->zJournal = (char*)pPtr;
62097 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
62098 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
62099#ifdef SQLITE_ENABLE_8_3_NAMES
62100 sqlite3FileSuffix3(zFilename,pPager->zJournal);
62101 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
62102#endif
62103 }else{
62104 pPager->zJournal = 0;
62105 }
62106
62107#ifndef SQLITE_OMIT_WAL
62108 /* Fill in Pager.zWal */
62109 if( nPathname>0 ){
62110 pPager->zWal = (char*)pPtr;
62111 memcpy(pPtr, zPathname, nPathname); pPtr += nPathname;
62112 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
62113#ifdef SQLITE_ENABLE_8_3_NAMES
62114 sqlite3FileSuffix3(zFilename, pPager->zWal);
62115 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
62116#endif
62117 }else{
62118 pPager->zWal = 0;
62119 }
62120#endif
62121 (void)pPtr; /* Suppress warning about unused pPtr value */
62122
62123 if( nPathname ) sqlite3DbFree(0, zPathname);
62124 pPager->pVfs = pVfs;
62125 pPager->vfsFlags = vfsFlags;
62126
62127 /* Open the pager file.
62128 */
62129 if( zFilename && zFilename[0] ){
62130 int fout = 0; /* VFS flags returned by xOpen() */
62131 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
62132 assert( !memDb )((void) (0));
62133 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY0x00000080)!=0;
62134 readOnly = (fout&SQLITE_OPEN_READONLY0x00000001)!=0;
62135
62136 /* If the file was successfully opened for read/write access,
62137 ** choose a default page size in case we have to create the
62138 ** database file. The default page size is the maximum of:
62139 **
62140 ** + SQLITE_DEFAULT_PAGE_SIZE,
62141 ** + The value returned by sqlite3OsSectorSize()
62142 ** + The largest page size that can be written atomically.
62143 */
62144 if( rc==SQLITE_OK0 ){
62145 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
62146 if( !readOnly ){
62147 setSectorSize(pPager);
62148 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE)((void) (0));
62149 if( szPageDflt<pPager->sectorSize ){
62150 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE8192 ){
62151 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE8192;
62152 }else{
62153 szPageDflt = (u32)pPager->sectorSize;
62154 }
62155 }
62156#ifdef SQLITE_ENABLE_ATOMIC_WRITE
62157 {
62158 int ii;
62159 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
62160 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
62161 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536)((void) (0));
62162 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE8192; ii=ii*2){
62163 if( iDc&(SQLITE_IOCAP_ATOMIC0x00000001|(ii>>8)) ){
62164 szPageDflt = ii;
62165 }
62166 }
62167 }
62168#endif
62169 }
62170 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
62171 if( (iDc & SQLITE_IOCAP_IMMUTABLE0x00002000)!=0
62172 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
62173 vfsFlags |= SQLITE_OPEN_READONLY0x00000001;
62174 goto act_like_temp_file;
62175 }
62176 }
62177 }else{
62178 /* If a temporary file is requested, it is not opened immediately.
62179 ** In this case we accept the default page size and delay actually
62180 ** opening the file until the first call to OsWrite().
62181 **
62182 ** This branch is also run for an in-memory database. An in-memory
62183 ** database is the same as a temp-file that is never written out to
62184 ** disk and uses an in-memory rollback journal.
62185 **
62186 ** This branch also runs for files marked as immutable.
62187 */
62188act_like_temp_file:
62189 tempFile = 1;
62190 pPager->eState = PAGER_READER1; /* Pretend we already have a lock */
62191 pPager->eLock = EXCLUSIVE_LOCK4; /* Pretend we are in EXCLUSIVE mode */
62192 pPager->noLock = 1; /* Do no locking */
62193 readOnly = (vfsFlags&SQLITE_OPEN_READONLY0x00000001);
62194 }
62195
62196 /* The following call to PagerSetPagesize() serves to set the value of
62197 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
62198 */
62199 if( rc==SQLITE_OK0 ){
62200 assert( pPager->memDb==0 )((void) (0));
62201 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
62202 testcase( rc!=SQLITE_OK );
62203 }
62204
62205 /* Initialize the PCache object. */
62206 if( rc==SQLITE_OK0 ){
62207 nExtra = ROUND8(nExtra)(((nExtra)+7)&~7);
62208 assert( nExtra>=8 && nExtra<1000 )((void) (0));
62209 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
62210 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
62211 }
62212
62213 /* If an error occurred above, free the Pager structure and close the file.
62214 */
62215 if( rc!=SQLITE_OK0 ){
62216 sqlite3OsClose(pPager->fd);
62217 sqlite3PageFree(pPager->pTmpSpace);
62218 sqlite3_free(pPager);
62219 return rc;
62220 }
62221
62222 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
62223 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
62224
62225 pPager->useJournal = (u8)useJournal;
62226 /* pPager->stmtOpen = 0; */
62227 /* pPager->stmtInUse = 0; */
62228 /* pPager->nRef = 0; */
62229 /* pPager->stmtSize = 0; */
62230 /* pPager->stmtJSize = 0; */
62231 /* pPager->nPage = 0; */
62232 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT0xfffffffe;
62233 /* pPager->state = PAGER_UNLOCK; */
62234 /* pPager->errMask = 0; */
62235 pPager->tempFile = (u8)tempFile;
62236 assert( tempFile==PAGER_LOCKINGMODE_NORMAL((void) (0))
62237 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
62238 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 )((void) (0));
62239 pPager->exclusiveMode = (u8)tempFile;
62240 pPager->changeCountDone = pPager->tempFile;
62241 pPager->memDb = (u8)memDb;
62242 pPager->readOnly = (u8)readOnly;
62243 assert( useJournal || pPager->tempFile )((void) (0));
62244 sqlite3PagerSetFlags(pPager, (SQLITE_DEFAULT_SYNCHRONOUS2+1)|PAGER_CACHESPILL0x20);
62245 /* pPager->pFirst = 0; */
62246 /* pPager->pFirstSynced = 0; */
62247 /* pPager->pLast = 0; */
62248 pPager->nExtra = (u16)nExtra;
62249 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT32768;
62250 assert( isOpen(pPager->fd) || tempFile )((void) (0));
62251 setSectorSize(pPager);
62252 if( !useJournal ){
62253 pPager->journalMode = PAGER_JOURNALMODE_OFF2;
62254 }else if( memDb || memJM ){
62255 pPager->journalMode = PAGER_JOURNALMODE_MEMORY4;
62256 }
62257 /* pPager->xBusyHandler = 0; */
62258 /* pPager->pBusyHandlerArg = 0; */
62259 pPager->xReiniter = xReinit;
62260 setGetterMethod(pPager);
62261 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
62262 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
62263
62264 *ppPager = pPager;
62265 return SQLITE_OK0;
62266}
62267
62268/*
62269** Return the sqlite3_file for the main database given the name
62270** of the corresponding WAL or Journal name as passed into
62271** xOpen.
62272*/
62273SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
62274 Pager *pPager;
62275 const char *p;
62276 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
62277 zName--;
62278 }
62279 p = zName - 4 - sizeof(Pager*);
62280 assert( EIGHT_BYTE_ALIGNMENT(p) )((void) (0));
62281 pPager = *(Pager**)p;
62282 return pPager->fd;
62283}
62284
62285
62286/*
62287** This function is called after transitioning from PAGER_UNLOCK to
62288** PAGER_SHARED state. It tests if there is a hot journal present in
62289** the file-system for the given pager. A hot journal is one that
62290** needs to be played back. According to this function, a hot-journal
62291** file exists if the following criteria are met:
62292**
62293** * The journal file exists in the file system, and
62294** * No process holds a RESERVED or greater lock on the database file, and
62295** * The database file itself is greater than 0 bytes in size, and
62296** * The first byte of the journal file exists and is not 0x00.
62297**
62298** If the current size of the database file is 0 but a journal file
62299** exists, that is probably an old journal left over from a prior
62300** database with the same name. In this case the journal file is
62301** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
62302** is returned.
62303**
62304** This routine does not check if there is a super-journal filename
62305** at the end of the file. If there is, and that super-journal file
62306** does not exist, then the journal file is not really hot. In this
62307** case this routine will return a false-positive. The pager_playback()
62308** routine will discover that the journal file is not really hot and
62309** will not roll it back.
62310**
62311** If a hot-journal file is found to exist, *pExists is set to 1 and
62312** SQLITE_OK returned. If no hot-journal file is present, *pExists is
62313** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
62314** to determine whether or not a hot-journal file exists, the IO error
62315** code is returned and the value of *pExists is undefined.
62316*/
62317static int hasHotJournal(Pager *pPager, int *pExists){
62318 sqlite3_vfs * const pVfs = pPager->pVfs;
62319 int rc = SQLITE_OK0; /* Return code */
62320 int exists = 1; /* True if a journal file is present */
62321 int jrnlOpen = !!isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0);
62322
62323 assert( pPager->useJournal )((void) (0));
62324 assert( isOpen(pPager->fd) )((void) (0));
62325 assert( pPager->eState==PAGER_OPEN )((void) (0));
62326
62327 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &((void) (0))
62328 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN((void) (0))
62329 ))((void) (0));
62330
62331 *pExists = 0;
62332 if( !jrnlOpen ){
62333 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &exists);
62334 }
62335 if( rc==SQLITE_OK0 && exists ){
62336 int locked = 0; /* True if some process holds a RESERVED lock */
62337
62338 /* Race condition here: Another process might have been holding the
62339 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
62340 ** call above, but then delete the journal and drop the lock before
62341 ** we get to the following sqlite3OsCheckReservedLock() call. If that
62342 ** is the case, this routine might think there is a hot journal when
62343 ** in fact there is none. This results in a false-positive which will
62344 ** be dealt with by the playback routine. Ticket #3883.
62345 */
62346 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
62347 if( rc==SQLITE_OK0 && !locked ){
62348 Pgno nPage; /* Number of pages in database file */
62349
62350 assert( pPager->tempFile==0 )((void) (0));
62351 rc = pagerPagecount(pPager, &nPage);
62352 if( rc==SQLITE_OK0 ){
62353 /* If the database is zero pages in size, that means that either (1) the
62354 ** journal is a remnant from a prior database with the same name where
62355 ** the database file but not the journal was deleted, or (2) the initial
62356 ** transaction that populates a new database is being rolled back.
62357 ** In either case, the journal file can be deleted. However, take care
62358 ** not to delete the journal file if it is already open due to
62359 ** journal_mode=PERSIST.
62360 */
62361 if( nPage==0 && !jrnlOpen ){
62362 sqlite3BeginBenignMalloc();
62363 if( pagerLockDb(pPager, RESERVED_LOCK2)==SQLITE_OK0 ){
62364 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
62365 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
62366 }
62367 sqlite3EndBenignMalloc();
62368 }else{
62369 /* The journal file exists and no other connection has a reserved
62370 ** or greater lock on the database file. Now check that there is
62371 ** at least one non-zero bytes at the start of the journal file.
62372 ** If there is, then we consider this journal to be hot. If not,
62373 ** it can be ignored.
62374 */
62375 if( !jrnlOpen ){
62376 int f = SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
62377 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
62378 }
62379 if( rc==SQLITE_OK0 ){
62380 u8 first = 0;
62381 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
62382 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
62383 rc = SQLITE_OK0;
62384 }
62385 if( !jrnlOpen ){
62386 sqlite3OsClose(pPager->jfd);
62387 }
62388 *pExists = (first!=0);
62389 }else if( rc==SQLITE_CANTOPEN14 ){
62390 /* If we cannot open the rollback journal file in order to see if
62391 ** it has a zero header, that might be due to an I/O error, or
62392 ** it might be due to the race condition described above and in
62393 ** ticket #3883. Either way, assume that the journal is hot.
62394 ** This might be a false positive. But if it is, then the
62395 ** automatic journal playback and recovery mechanism will deal
62396 ** with it under an EXCLUSIVE lock where we do not need to
62397 ** worry so much with race conditions.
62398 */
62399 *pExists = 1;
62400 rc = SQLITE_OK0;
62401 }
62402 }
62403 }
62404 }
62405 }
62406
62407 return rc;
62408}
62409
62410/*
62411** This function is called to obtain a shared lock on the database file.
62412** It is illegal to call sqlite3PagerGet() until after this function
62413** has been successfully called. If a shared-lock is already held when
62414** this function is called, it is a no-op.
62415**
62416** The following operations are also performed by this function.
62417**
62418** 1) If the pager is currently in PAGER_OPEN state (no lock held
62419** on the database file), then an attempt is made to obtain a
62420** SHARED lock on the database file. Immediately after obtaining
62421** the SHARED lock, the file-system is checked for a hot-journal,
62422** which is played back if present. Following any hot-journal
62423** rollback, the contents of the cache are validated by checking
62424** the 'change-counter' field of the database file header and
62425** discarded if they are found to be invalid.
62426**
62427** 2) If the pager is running in exclusive-mode, and there are currently
62428** no outstanding references to any pages, and is in the error state,
62429** then an attempt is made to clear the error state by discarding
62430** the contents of the page cache and rolling back any open journal
62431** file.
62432**
62433** If everything is successful, SQLITE_OK is returned. If an IO error
62434** occurs while locking the database, checking for a hot-journal file or
62435** rolling back a journal file, the IO error code is returned.
62436*/
62437SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager){
62438 int rc = SQLITE_OK0; /* Return code */
62439
62440 /* This routine is only called from b-tree and only when there are no
62441 ** outstanding pages. This implies that the pager state should either
62442 ** be OPEN or READER. READER is only possible if the pager is or was in
62443 ** exclusive access mode. */
62444 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 )((void) (0));
62445 assert( assert_pager_state(pPager) )((void) (0));
62446 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
62447 assert( pPager->errCode==SQLITE_OK )((void) (0));
62448
62449 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->eState==PAGER_OPEN0 ){
62450 int bHotJournal = 1; /* True if there exists a hot journal-file */
62451
62452 assert( !MEMDB )((void) (0));
62453 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
62454
62455 rc = pager_wait_on_lock(pPager, SHARED_LOCK1);
62456 if( rc!=SQLITE_OK0 ){
62457 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK )((void) (0));
62458 goto failed;
62459 }
62460
62461 /* If a journal file exists, and there is no RESERVED lock on the
62462 ** database file, then it either needs to be played back or deleted.
62463 */
62464 if( pPager->eLock<=SHARED_LOCK1 ){
62465 rc = hasHotJournal(pPager, &bHotJournal);
62466 }
62467 if( rc!=SQLITE_OK0 ){
62468 goto failed;
62469 }
62470 if( bHotJournal ){
62471 if( pPager->readOnly ){
62472 rc = SQLITE_READONLY_ROLLBACK(8 | (3<<8));
62473 goto failed;
62474 }
62475
62476 /* Get an EXCLUSIVE lock on the database file. At this point it is
62477 ** important that a RESERVED lock is not obtained on the way to the
62478 ** EXCLUSIVE lock. If it were, another process might open the
62479 ** database file, detect the RESERVED lock, and conclude that the
62480 ** database is safe to read while this process is still rolling the
62481 ** hot-journal back.
62482 **
62483 ** Because the intermediate RESERVED lock is not requested, any
62484 ** other process attempting to access the database file will get to
62485 ** this point in the code and fail to obtain its own EXCLUSIVE lock
62486 ** on the database file.
62487 **
62488 ** Unless the pager is in locking_mode=exclusive mode, the lock is
62489 ** downgraded to SHARED_LOCK before this function returns.
62490 */
62491 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
62492 if( rc!=SQLITE_OK0 ){
62493 goto failed;
62494 }
62495
62496 /* If it is not already open and the file exists on disk, open the
62497 ** journal for read/write access. Write access is required because
62498 ** in exclusive-access mode the file descriptor will be kept open
62499 ** and possibly used for a transaction later on. Also, write-access
62500 ** is usually required to finalize the journal in journal_mode=persist
62501 ** mode (and also for journal_mode=truncate on some systems).
62502 **
62503 ** If the journal does not exist, it usually means that some
62504 ** other connection managed to get in and roll it back before
62505 ** this connection obtained the exclusive lock above. Or, it
62506 ** may mean that the pager was in the error-state when this
62507 ** function was called and the journal file does not exist.
62508 */
62509 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
62510 sqlite3_vfs * const pVfs = pPager->pVfs;
62511 int bExists; /* True if journal file exists */
62512 rc = sqlite3OsAccess(
62513 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &bExists);
62514 if( rc==SQLITE_OK0 && bExists ){
62515 int fout = 0;
62516 int f = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
62517 assert( !pPager->tempFile )((void) (0));
62518 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
62519 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
62520 if( rc==SQLITE_OK0 && fout&SQLITE_OPEN_READONLY0x00000001 ){
62521 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(62521);
62522 sqlite3OsClose(pPager->jfd);
62523 }
62524 }
62525 }
62526
62527 /* Playback and delete the journal. Drop the database write
62528 ** lock and reacquire the read lock. Purge the cache before
62529 ** playing back the hot-journal so that we don't end up with
62530 ** an inconsistent cache. Sync the hot journal before playing
62531 ** it back since the process that crashed and left the hot journal
62532 ** probably did not sync it and we are required to always sync
62533 ** the journal before playing it back.
62534 */
62535 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
62536 assert( rc==SQLITE_OK )((void) (0));
62537 rc = pagerSyncHotJournal(pPager);
62538 if( rc==SQLITE_OK0 ){
62539 rc = pager_playback(pPager, !pPager->tempFile);
62540 pPager->eState = PAGER_OPEN0;
62541 }
62542 }else if( !pPager->exclusiveMode ){
62543 pagerUnlockDb(pPager, SHARED_LOCK1);
62544 }
62545
62546 if( rc!=SQLITE_OK0 ){
62547 /* This branch is taken if an error occurs while trying to open
62548 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
62549 ** pager_unlock() routine will be called before returning to unlock
62550 ** the file. If the unlock attempt fails, then Pager.eLock must be
62551 ** set to UNKNOWN_LOCK (see the comment above the #define for
62552 ** UNKNOWN_LOCK above for an explanation).
62553 **
62554 ** In order to get pager_unlock() to do this, set Pager.eState to
62555 ** PAGER_ERROR now. This is not actually counted as a transition
62556 ** to ERROR state in the state diagram at the top of this file,
62557 ** since we know that the same call to pager_unlock() will very
62558 ** shortly transition the pager object to the OPEN state. Calling
62559 ** assert_pager_state() would fail now, as it should not be possible
62560 ** to be in ERROR state when there are zero outstanding page
62561 ** references.
62562 */
62563 pager_error(pPager, rc);
62564 goto failed;
62565 }
62566
62567 assert( pPager->eState==PAGER_OPEN )((void) (0));
62568 assert( (pPager->eLock==SHARED_LOCK)((void) (0))
62569 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)((void) (0))
62570 )((void) (0));
62571 }
62572
62573 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
62574 /* The shared-lock has just been acquired then check to
62575 ** see if the database has been modified. If the database has changed,
62576 ** flush the cache. The hasHeldSharedLock flag prevents this from
62577 ** occurring on the very first access to a file, in order to save a
62578 ** single unnecessary sqlite3OsRead() call at the start-up.
62579 **
62580 ** Database changes are detected by looking at 15 bytes beginning
62581 ** at offset 24 into the file. The first 4 of these 16 bytes are
62582 ** a 32-bit counter that is incremented with each change. The
62583 ** other bytes change randomly with each file change when
62584 ** a codec is in use.
62585 **
62586 ** There is a vanishingly small chance that a change will not be
62587 ** detected. The chance of an undetected change is so small that
62588 ** it can be neglected.
62589 */
62590 char dbFileVers[sizeof(pPager->dbFileVers)];
62591
62592 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
62593 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
62594 if( rc!=SQLITE_OK0 ){
62595 if( rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
62596 goto failed;
62597 }
62598 memset(dbFileVers, 0, sizeof(dbFileVers));
62599 }
62600
62601 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
62602 pager_reset(pPager);
62603
62604 /* Unmap the database file. It is possible that external processes
62605 ** may have truncated the database file and then extended it back
62606 ** to its original size while this process was not holding a lock.
62607 ** In this case there may exist a Pager.pMap mapping that appears
62608 ** to be the right size but is not actually valid. Avoid this
62609 ** possibility by unmapping the db here. */
62610 if( USEFETCH(pPager)((pPager)->bUseFetch) ){
62611 sqlite3OsUnfetch(pPager->fd, 0, 0);
62612 }
62613 }
62614 }
62615
62616 /* If there is a WAL file in the file-system, open this database in WAL
62617 ** mode. Otherwise, the following function call is a no-op.
62618 */
62619 rc = pagerOpenWalIfPresent(pPager);
62620#ifndef SQLITE_OMIT_WAL
62621 assert( pPager->pWal==0 || rc==SQLITE_OK )((void) (0));
62622#endif
62623 }
62624
62625 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
62626 assert( rc==SQLITE_OK )((void) (0));
62627 rc = pagerBeginReadTransaction(pPager);
62628 }
62629
62630 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN0 && rc==SQLITE_OK0 ){
62631 rc = pagerPagecount(pPager, &pPager->dbSize);
62632 }
62633
62634 failed:
62635 if( rc!=SQLITE_OK0 ){
62636 assert( !MEMDB )((void) (0));
62637 pager_unlock(pPager);
62638 assert( pPager->eState==PAGER_OPEN )((void) (0));
62639 }else{
62640 pPager->eState = PAGER_READER1;
62641 pPager->hasHeldSharedLock = 1;
62642 }
62643 return rc;
62644}
62645
62646/*
62647** If the reference count has reached zero, rollback any active
62648** transaction and unlock the pager.
62649**
62650** Except, in locking_mode=EXCLUSIVE when there is nothing to in
62651** the rollback journal, the unlock is not performed and there is
62652** nothing to rollback, so this routine is a no-op.
62653*/
62654static void pagerUnlockIfUnused(Pager *pPager){
62655 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
62656 assert( pPager->nMmapOut==0 )((void) (0)); /* because page1 is never memory mapped */
62657 pagerUnlockAndRollback(pPager);
62658 }
62659}
62660
62661/*
62662** The page getter methods each try to acquire a reference to a
62663** page with page number pgno. If the requested reference is
62664** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
62665**
62666** There are different implementations of the getter method depending
62667** on the current state of the pager.
62668**
62669** getPageNormal() -- The normal getter
62670** getPageError() -- Used if the pager is in an error state
62671** getPageMmap() -- Used if memory-mapped I/O is enabled
62672**
62673** If the requested page is already in the cache, it is returned.
62674** Otherwise, a new page object is allocated and populated with data
62675** read from the database file. In some cases, the pcache module may
62676** choose not to allocate a new page object and may reuse an existing
62677** object with no outstanding references.
62678**
62679** The extra data appended to a page is always initialized to zeros the
62680** first time a page is loaded into memory. If the page requested is
62681** already in the cache when this function is called, then the extra
62682** data is left as it was when the page object was last used.
62683**
62684** If the database image is smaller than the requested page or if
62685** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
62686** requested page is not already stored in the cache, then no
62687** actual disk read occurs. In this case the memory image of the
62688** page is initialized to all zeros.
62689**
62690** If PAGER_GET_NOCONTENT is true, it means that we do not care about
62691** the contents of the page. This occurs in two scenarios:
62692**
62693** a) When reading a free-list leaf page from the database, and
62694**
62695** b) When a savepoint is being rolled back and we need to load
62696** a new page into the cache to be filled with the data read
62697** from the savepoint journal.
62698**
62699** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
62700** of being read from the database. Additionally, the bits corresponding
62701** to pgno in Pager.pInJournal (bitvec of pages already written to the
62702** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
62703** savepoints are set. This means if the page is made writable at any
62704** point in the future, using a call to sqlite3PagerWrite(), its contents
62705** will not be journaled. This saves IO.
62706**
62707** The acquisition might fail for several reasons. In all cases,
62708** an appropriate error code is returned and *ppPage is set to NULL.
62709**
62710** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
62711** to find a page in the in-memory cache first. If the page is not already
62712** in memory, this routine goes to disk to read it in whereas Lookup()
62713** just returns 0. This routine acquires a read-lock the first time it
62714** has to go to disk, and could also playback an old journal if necessary.
62715** Since Lookup() never goes to disk, it never has to deal with locks
62716** or journal files.
62717*/
62718static int getPageNormal(
62719 Pager *pPager, /* The pager open on the database file */
62720 Pgno pgno, /* Page number to fetch */
62721 DbPage **ppPage, /* Write a pointer to the page here */
62722 int flags /* PAGER_GET_XXX flags */
62723){
62724 int rc = SQLITE_OK0;
62725 PgHdr *pPg;
62726 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
62727 sqlite3_pcache_page *pBase;
62728
62729 assert( pPager->errCode==SQLITE_OK )((void) (0));
62730 assert( pPager->eState>=PAGER_READER )((void) (0));
62731 assert( assert_pager_state(pPager) )((void) (0));
62732 assert( pPager->hasHeldSharedLock==1 )((void) (0));
62733
62734 if( pgno==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(62734);
62735 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
62736 if( pBase==0 ){
62737 pPg = 0;
62738 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
62739 if( rc!=SQLITE_OK0 ) goto pager_acquire_err;
62740 if( pBase==0 ){
62741 rc = SQLITE_NOMEM_BKPT7;
62742 goto pager_acquire_err;
62743 }
62744 }
62745 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
62746 assert( pPg==(*ppPage) )((void) (0));
62747 assert( pPg->pgno==pgno )((void) (0));
62748 assert( pPg->pPager==pPager || pPg->pPager==0 )((void) (0));
62749
62750 noContent = (flags & PAGER_GET_NOCONTENT0x01)!=0;
62751 if( pPg->pPager && !noContent ){
62752 /* In this case the pcache already contains an initialized copy of
62753 ** the page. Return without further ado. */
62754 assert( pgno!=PAGER_SJ_PGNO(pPager) )((void) (0));
62755 pPager->aStat[PAGER_STAT_HIT0]++;
62756 return SQLITE_OK0;
62757
62758 }else{
62759 /* The pager cache has created a new page. Its content needs to
62760 ** be initialized. But first some error checks:
62761 **
62762 ** (*) obsolete. Was: maximum page number is 2^31
62763 ** (2) Never try to fetch the locking page
62764 */
62765 if( pgno==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
62766 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(62766);
62767 goto pager_acquire_err;
62768 }
62769
62770 pPg->pPager = pPager;
62771
62772 assert( !isOpen(pPager->fd) || !MEMDB )((void) (0));
62773 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) || pPager->dbSize<pgno || noContent ){
62774 if( pgno>pPager->mxPgno ){
62775 rc = SQLITE_FULL13;
62776 if( pgno<=pPager->dbSize ){
62777 sqlite3PcacheRelease(pPg);
62778 pPg = 0;
62779 }
62780 goto pager_acquire_err;
62781 }
62782 if( noContent ){
62783 /* Failure to set the bits in the InJournal bit-vectors is benign.
62784 ** It merely means that we might do some extra work to journal a
62785 ** page that does not need to be journaled. Nevertheless, be sure
62786 ** to test the case where a malloc error occurs while trying to set
62787 ** a bit in a bit vector.
62788 */
62789 sqlite3BeginBenignMalloc();
62790 if( pgno<=pPager->dbOrigSize ){
62791 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
62792 testcase( rc==SQLITE_NOMEM );
62793 }
62794 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
62795 testcase( rc==SQLITE_NOMEM );
62796 sqlite3EndBenignMalloc();
62797 }
62798 memset(pPg->pData, 0, pPager->pageSize);
62799 IOTRACE(("ZERO %p %d\n", pPager, pgno));
62800 }else{
62801 assert( pPg->pPager==pPager )((void) (0));
62802 pPager->aStat[PAGER_STAT_MISS1]++;
62803 rc = readDbPage(pPg);
62804 if( rc!=SQLITE_OK0 ){
62805 goto pager_acquire_err;
62806 }
62807 }
62808 pager_set_pagehash(pPg);
62809 }
62810 return SQLITE_OK0;
62811
62812pager_acquire_err:
62813 assert( rc!=SQLITE_OK )((void) (0));
62814 if( pPg ){
62815 sqlite3PcacheDrop(pPg);
62816 }
62817 pagerUnlockIfUnused(pPager);
62818 *ppPage = 0;
62819 return rc;
62820}
62821
62822#if SQLITE_MAX_MMAP_SIZE20971520>0
62823/* The page getter for when memory-mapped I/O is enabled */
62824static int getPageMMap(
62825 Pager *pPager, /* The pager open on the database file */
62826 Pgno pgno, /* Page number to fetch */
62827 DbPage **ppPage, /* Write a pointer to the page here */
62828 int flags /* PAGER_GET_XXX flags */
62829){
62830 int rc = SQLITE_OK0;
62831 PgHdr *pPg = 0;
62832 u32 iFrame = 0; /* Frame to read from WAL file */
62833
62834 /* It is acceptable to use a read-only (mmap) page for any page except
62835 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
62836 ** flag was specified by the caller. And so long as the db is not a
62837 ** temporary or in-memory database. */
62838 const int bMmapOk = (pgno>1
62839 && (pPager->eState==PAGER_READER1 || (flags & PAGER_GET_READONLY0x02))
62840 );
62841
62842 assert( USEFETCH(pPager) )((void) (0));
62843
62844 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
62845 ** allows the compiler optimizer to reuse the results of the "pgno>1"
62846 ** test in the previous statement, and avoid testing pgno==0 in the
62847 ** common case where pgno is large. */
62848 if( pgno<=1 && pgno==0 ){
62849 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(62849);
62850 }
62851 assert( pPager->eState>=PAGER_READER )((void) (0));
62852 assert( assert_pager_state(pPager) )((void) (0));
62853 assert( pPager->hasHeldSharedLock==1 )((void) (0));
62854 assert( pPager->errCode==SQLITE_OK )((void) (0));
62855
62856 if( bMmapOk && pagerUseWal(pPager)((pPager)->pWal!=0) ){
62857 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
62858 if( rc!=SQLITE_OK0 ){
62859 *ppPage = 0;
62860 return rc;
62861 }
62862 }
62863 if( bMmapOk && iFrame==0 ){
62864 void *pData = 0;
62865 rc = sqlite3OsFetch(pPager->fd,
62866 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
62867 );
62868 if( rc==SQLITE_OK0 && pData ){
62869 if( pPager->eState>PAGER_READER1 || pPager->tempFile ){
62870 pPg = sqlite3PagerLookup(pPager, pgno);
62871 }
62872 if( pPg==0 ){
62873 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
62874 }else{
62875 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
62876 }
62877 if( pPg ){
62878 assert( rc==SQLITE_OK )((void) (0));
62879 *ppPage = pPg;
62880 return SQLITE_OK0;
62881 }
62882 }
62883 if( rc!=SQLITE_OK0 ){
62884 *ppPage = 0;
62885 return rc;
62886 }
62887 }
62888 return getPageNormal(pPager, pgno, ppPage, flags);
62889}
62890#endif /* SQLITE_MAX_MMAP_SIZE>0 */
62891
62892/* The page getter method for when the pager is an error state */
62893static int getPageError(
62894 Pager *pPager, /* The pager open on the database file */
62895 Pgno pgno, /* Page number to fetch */
62896 DbPage **ppPage, /* Write a pointer to the page here */
62897 int flags /* PAGER_GET_XXX flags */
62898){
62899 UNUSED_PARAMETER(pgno)(void)(pgno);
62900 UNUSED_PARAMETER(flags)(void)(flags);
62901 assert( pPager->errCode!=SQLITE_OK )((void) (0));
62902 *ppPage = 0;
62903 return pPager->errCode;
62904}
62905
62906
62907/* Dispatch all page fetch requests to the appropriate getter method.
62908*/
62909SQLITE_PRIVATEstatic int sqlite3PagerGet(
62910 Pager *pPager, /* The pager open on the database file */
62911 Pgno pgno, /* Page number to fetch */
62912 DbPage **ppPage, /* Write a pointer to the page here */
62913 int flags /* PAGER_GET_XXX flags */
62914){
62915#if 0 /* Trace page fetch by setting to 1 */
62916 int rc;
62917 printf("PAGE %u\n", pgno);
62918 fflush(stdoutstdout);
62919 rc = pPager->xGet(pPager, pgno, ppPage, flags);
62920 if( rc ){
62921 printf("PAGE %u failed with 0x%02x\n", pgno, rc);
62922 fflush(stdoutstdout);
62923 }
62924 return rc;
62925#else
62926 /* Normal, high-speed version of sqlite3PagerGet() */
62927 return pPager->xGet(pPager, pgno, ppPage, flags);
62928#endif
62929}
62930
62931/*
62932** Acquire a page if it is already in the in-memory cache. Do
62933** not read the page from disk. Return a pointer to the page,
62934** or 0 if the page is not in cache.
62935**
62936** See also sqlite3PagerGet(). The difference between this routine
62937** and sqlite3PagerGet() is that _get() will go to the disk and read
62938** in the page if the page is not already in cache. This routine
62939** returns NULL if the page is not in cache or if a disk I/O error
62940** has ever happened.
62941*/
62942SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
62943 sqlite3_pcache_page *pPage;
62944 assert( pPager!=0 )((void) (0));
62945 assert( pgno!=0 )((void) (0));
62946 assert( pPager->pPCache!=0 )((void) (0));
62947 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
62948 assert( pPage==0 || pPager->hasHeldSharedLock )((void) (0));
62949 if( pPage==0 ) return 0;
62950 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
62951}
62952
62953/*
62954** Release a page reference.
62955**
62956** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be used
62957** if we know that the page being released is not the last reference to page1.
62958** The btree layer always holds page1 open until the end, so these first
62959** two routines can be used to release any page other than BtShared.pPage1.
62960** The assert() at tag-20230419-2 proves that this constraint is always
62961** honored.
62962**
62963** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
62964** checks the total number of outstanding pages and if the number of
62965** pages reaches zero it drops the database lock.
62966*/
62967SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage *pPg){
62968 TESTONLY( Pager *pPager = pPg->pPager; )
62969 assert( pPg!=0 )((void) (0));
62970 if( pPg->flags & PGHDR_MMAP0x020 ){
62971 assert( pPg->pgno!=1 )((void) (0)); /* Page1 is never memory mapped */
62972 pagerReleaseMapPage(pPg);
62973 }else{
62974 sqlite3PcacheRelease(pPg);
62975 }
62976 /* Do not use this routine to release the last reference to page1 */
62977 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 )((void) (0)); /* tag-20230419-2 */
62978}
62979SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage *pPg){
62980 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
62981}
62982SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage *pPg){
62983 Pager *pPager;
62984 assert( pPg!=0 )((void) (0));
62985 assert( pPg->pgno==1 )((void) (0));
62986 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0)); /* Page1 is never memory mapped */
62987 pPager = pPg->pPager;
62988 sqlite3PcacheRelease(pPg);
62989 pagerUnlockIfUnused(pPager);
62990}
62991
62992/*
62993** This function is called at the start of every write transaction.
62994** There must already be a RESERVED or EXCLUSIVE lock on the database
62995** file when this routine is called.
62996**
62997** Open the journal file for pager pPager and write a journal header
62998** to the start of it. If there are active savepoints, open the sub-journal
62999** as well. This function is only used when the journal file is being
63000** opened to write a rollback log for a transaction. It is not used
63001** when opening a hot journal file to roll it back.
63002**
63003** If the journal file is already open (as it may be in exclusive mode),
63004** then this function just writes a journal header to the start of the
63005** already open file.
63006**
63007** Whether or not the journal file is opened by this function, the
63008** Pager.pInJournal bitvec structure is allocated.
63009**
63010** Return SQLITE_OK if everything is successful. Otherwise, return
63011** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
63012** an IO error code if opening or writing the journal file fails.
63013*/
63014static int pager_open_journal(Pager *pPager){
63015 int rc = SQLITE_OK0; /* Return code */
63016 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
63017
63018 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
63019 assert( assert_pager_state(pPager) )((void) (0));
63020 assert( pPager->pInJournal==0 )((void) (0));
63021
63022 /* If already in the error state, this function is a no-op. But on
63023 ** the other hand, this routine is never called if we are already in
63024 ** an error state. */
63025 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
63026
63027 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
63028 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
63029 if( pPager->pInJournal==0 ){
63030 return SQLITE_NOMEM_BKPT7;
63031 }
63032
63033 /* Open the journal file if it is not already open. */
63034 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
63035 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 ){
63036 sqlite3MemJournalOpen(pPager->jfd);
63037 }else{
63038 int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004;
63039 int nSpill;
63040
63041 if( pPager->tempFile ){
63042 flags |= (SQLITE_OPEN_DELETEONCLOSE0x00000008|SQLITE_OPEN_TEMP_JOURNAL0x00001000);
63043 flags |= SQLITE_OPEN_EXCLUSIVE0x00000010;
63044 nSpill = sqlite3Config.nStmtSpill;
63045 }else{
63046 flags |= SQLITE_OPEN_MAIN_JOURNAL0x00000800;
63047 nSpill = jrnlBufferSize(pPager);
63048 }
63049
63050 /* Verify that the database still has the same name as it did when
63051 ** it was originally opened. */
63052 rc = databaseIsUnmoved(pPager);
63053 if( rc==SQLITE_OK0 ){
63054 rc = sqlite3JournalOpen (
63055 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
63056 );
63057 }
63058 }
63059 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
63060 }
63061
63062
63063 /* Write the first journal header to the journal file and open
63064 ** the sub-journal if necessary.
63065 */
63066 if( rc==SQLITE_OK0 ){
63067 /* TODO: Check if all of these are really required. */
63068 pPager->nRec = 0;
63069 pPager->journalOff = 0;
63070 pPager->setSuper = 0;
63071 pPager->journalHdr = 0;
63072 rc = writeJournalHdr(pPager);
63073 }
63074 }
63075
63076 if( rc!=SQLITE_OK0 ){
63077 sqlite3BitvecDestroy(pPager->pInJournal);
63078 pPager->pInJournal = 0;
63079 pPager->journalOff = 0;
63080 }else{
63081 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
63082 pPager->eState = PAGER_WRITER_CACHEMOD3;
63083 }
63084
63085 return rc;
63086}
63087
63088/*
63089** Begin a write-transaction on the specified pager object. If a
63090** write-transaction has already been opened, this function is a no-op.
63091**
63092** If the exFlag argument is false, then acquire at least a RESERVED
63093** lock on the database file. If exFlag is true, then acquire at least
63094** an EXCLUSIVE lock. If such a lock is already held, no locking
63095** functions need be called.
63096**
63097** If the subjInMemory argument is non-zero, then any sub-journal opened
63098** within this transaction will be opened as an in-memory file. This
63099** has no effect if the sub-journal is already opened (as it may be when
63100** running in exclusive mode) or if the transaction does not require a
63101** sub-journal. If the subjInMemory argument is zero, then any required
63102** sub-journal is implemented in-memory if pPager is an in-memory database,
63103** or using a temporary file otherwise.
63104*/
63105SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
63106 int rc = SQLITE_OK0;
63107
63108 if( pPager->errCode ) return pPager->errCode;
63109 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR )((void) (0));
63110 pPager->subjInMemory = (u8)subjInMemory;
63111
63112 if( pPager->eState==PAGER_READER1 ){
63113 assert( pPager->pInJournal==0 )((void) (0));
63114
63115 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
63116 /* If the pager is configured to use locking_mode=exclusive, and an
63117 ** exclusive lock on the database is not already held, obtain it now.
63118 */
63119 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
63120 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
63121 if( rc!=SQLITE_OK0 ){
63122 return rc;
63123 }
63124 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
63125 }
63126
63127 /* Grab the write lock on the log file. If successful, upgrade to
63128 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
63129 ** The busy-handler is not invoked if another connection already
63130 ** holds the write-lock. If possible, the upper layer will call it.
63131 */
63132 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
63133 }else{
63134 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
63135 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
63136 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
63137 ** lock, but not when obtaining the RESERVED lock.
63138 */
63139 rc = pagerLockDb(pPager, RESERVED_LOCK2);
63140 if( rc==SQLITE_OK0 && exFlag ){
63141 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
63142 }
63143 }
63144
63145 if( rc==SQLITE_OK0 ){
63146 /* Change to WRITER_LOCKED state.
63147 **
63148 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
63149 ** when it has an open transaction, but never to DBMOD or FINISHED.
63150 ** This is because in those states the code to roll back savepoint
63151 ** transactions may copy data from the sub-journal into the database
63152 ** file as well as into the page cache. Which would be incorrect in
63153 ** WAL mode.
63154 */
63155 pPager->eState = PAGER_WRITER_LOCKED2;
63156 pPager->dbHintSize = pPager->dbSize;
63157 pPager->dbFileSize = pPager->dbSize;
63158 pPager->dbOrigSize = pPager->dbSize;
63159 pPager->journalOff = 0;
63160 }
63161
63162 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER )((void) (0));
63163 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
63164 assert( assert_pager_state(pPager) )((void) (0));
63165 }
63166
63167 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
63168 return rc;
63169}
63170
63171/*
63172** Write page pPg onto the end of the rollback journal.
63173*/
63174static SQLITE_NOINLINE__attribute__((noinline)) int pagerAddPageToRollbackJournal(PgHdr *pPg){
63175 Pager *pPager = pPg->pPager;
63176 int rc;
63177 u32 cksum;
63178 char *pData2;
63179 i64 iOff = pPager->journalOff;
63180
63181 /* We should never write to the journal file the page that
63182 ** contains the database locks. The following assert verifies
63183 ** that we do not. */
63184 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) )((void) (0));
63185
63186 assert( pPager->journalHdr<=pPager->journalOff )((void) (0));
63187 pData2 = pPg->pData;
63188 cksum = pager_cksum(pPager, (u8*)pData2);
63189
63190 /* Even if an IO or diskfull error occurs while journalling the
63191 ** page in the block above, set the need-sync flag for the page.
63192 ** Otherwise, when the transaction is rolled back, the logic in
63193 ** playback_one_page() will think that the page needs to be restored
63194 ** in the database file. And if an IO error occurs while doing so,
63195 ** then corruption may follow.
63196 */
63197 pPg->flags |= PGHDR_NEED_SYNC0x008;
63198
63199 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
63200 if( rc!=SQLITE_OK0 ) return rc;
63201 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
63202 if( rc!=SQLITE_OK0 ) return rc;
63203 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
63204 if( rc!=SQLITE_OK0 ) return rc;
63205
63206 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
63207 pPager->journalOff, pPager->pageSize));
63208 PAGER_INCR(sqlite3_pager_writej_count);
63209 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
63210 PAGERID(pPager), pPg->pgno,
63211 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
63212
63213 pPager->journalOff += 8 + pPager->pageSize;
63214 pPager->nRec++;
63215 assert( pPager->pInJournal!=0 )((void) (0));
63216 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
63217 testcase( rc==SQLITE_NOMEM );
63218 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
63219 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
63220 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
63221 return rc;
63222}
63223
63224/*
63225** Mark a single data page as writeable. The page is written into the
63226** main journal or sub-journal as required. If the page is written into
63227** one of the journals, the corresponding bit is set in the
63228** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
63229** of any open savepoints as appropriate.
63230*/
63231static int pager_write(PgHdr *pPg){
63232 Pager *pPager = pPg->pPager;
63233 int rc = SQLITE_OK0;
63234
63235 /* This routine is not called unless a write-transaction has already
63236 ** been started. The journal file may or may not be open at this point.
63237 ** It is never called in the ERROR state.
63238 */
63239 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
63240 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
63241 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
63242 )((void) (0));
63243 assert( assert_pager_state(pPager) )((void) (0));
63244 assert( pPager->errCode==0 )((void) (0));
63245 assert( pPager->readOnly==0 )((void) (0));
63246 CHECK_PAGE(pPg);
63247
63248 /* The journal file needs to be opened. Higher level routines have already
63249 ** obtained the necessary locks to begin the write-transaction, but the
63250 ** rollback journal might not yet be open. Open it now if this is the case.
63251 **
63252 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
63253 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
63254 ** an error might occur and the pager would end up in WRITER_LOCKED state
63255 ** with pages marked as dirty in the cache.
63256 */
63257 if( pPager->eState==PAGER_WRITER_LOCKED2 ){
63258 rc = pager_open_journal(pPager);
63259 if( rc!=SQLITE_OK0 ) return rc;
63260 }
63261 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
63262 assert( assert_pager_state(pPager) )((void) (0));
63263
63264 /* Mark the page that is about to be modified as dirty. */
63265 sqlite3PcacheMakeDirty(pPg);
63266
63267 /* If a rollback journal is in use, them make sure the page that is about
63268 ** to change is in the rollback journal, or if the page is a new page off
63269 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
63270 */
63271 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) )((void) (0));
63272 if( pPager->pInJournal!=0
63273 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
63274 ){
63275 assert( pagerUseWal(pPager)==0 )((void) (0));
63276 if( pPg->pgno<=pPager->dbOrigSize ){
63277 rc = pagerAddPageToRollbackJournal(pPg);
63278 if( rc!=SQLITE_OK0 ){
63279 return rc;
63280 }
63281 }else{
63282 if( pPager->eState!=PAGER_WRITER_DBMOD4 ){
63283 pPg->flags |= PGHDR_NEED_SYNC0x008;
63284 }
63285 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
63286 PAGERID(pPager), pPg->pgno,
63287 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
63288 }
63289 }
63290
63291 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
63292 ** and before writing the page into the rollback journal. Wait until now,
63293 ** after the page has been successfully journalled, before setting the
63294 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
63295 */
63296 pPg->flags |= PGHDR_WRITEABLE0x004;
63297
63298 /* If the statement journal is open and the page is not in it,
63299 ** then write the page into the statement journal.
63300 */
63301 if( pPager->nSavepoint>0 ){
63302 rc = subjournalPageIfRequired(pPg);
63303 }
63304
63305 /* Update the database size and return. */
63306 if( pPager->dbSize<pPg->pgno ){
63307 pPager->dbSize = pPg->pgno;
63308 }
63309 return rc;
63310}
63311
63312/*
63313** This is a variant of sqlite3PagerWrite() that runs when the sector size
63314** is larger than the page size. SQLite makes the (reasonable) assumption that
63315** all bytes of a sector are written together by hardware. Hence, all bytes of
63316** a sector need to be journalled in case of a power loss in the middle of
63317** a write.
63318**
63319** Usually, the sector size is less than or equal to the page size, in which
63320** case pages can be individually written. This routine only runs in the
63321** exceptional case where the page size is smaller than the sector size.
63322*/
63323static SQLITE_NOINLINE__attribute__((noinline)) int pagerWriteLargeSector(PgHdr *pPg){
63324 int rc = SQLITE_OK0; /* Return code */
63325 Pgno nPageCount; /* Total number of pages in database file */
63326 Pgno pg1; /* First page of the sector pPg is located on. */
63327 int nPage = 0; /* Number of pages starting at pg1 to journal */
63328 int ii; /* Loop counter */
63329 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
63330 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
63331 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
63332
63333 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
63334 ** a journal header to be written between the pages journaled by
63335 ** this function.
63336 */
63337 assert( !MEMDB )((void) (0));
63338 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 )((void) (0));
63339 pPager->doNotSpill |= SPILLFLAG_NOSYNC0x04;
63340
63341 /* This trick assumes that both the page-size and sector-size are
63342 ** an integer power of 2. It sets variable pg1 to the identifier
63343 ** of the first page of the sector pPg is located on.
63344 */
63345 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
63346
63347 nPageCount = pPager->dbSize;
63348 if( pPg->pgno>nPageCount ){
63349 nPage = (pPg->pgno - pg1)+1;
63350 }else if( (pg1+nPagePerSector-1)>nPageCount ){
63351 nPage = nPageCount+1-pg1;
63352 }else{
63353 nPage = nPagePerSector;
63354 }
63355 assert(nPage>0)((void) (0));
63356 assert(pg1<=pPg->pgno)((void) (0));
63357 assert((pg1+nPage)>pPg->pgno)((void) (0));
63358
63359 for(ii=0; ii<nPage && rc==SQLITE_OK0; ii++){
63360 Pgno pg = pg1+ii;
63361 PgHdr *pPage;
63362 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
63363 if( pg!=PAGER_SJ_PGNO(pPager)((pPager)->lckPgno) ){
63364 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
63365 if( rc==SQLITE_OK0 ){
63366 rc = pager_write(pPage);
63367 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
63368 needSync = 1;
63369 }
63370 sqlite3PagerUnrefNotNull(pPage);
63371 }
63372 }
63373 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
63374 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
63375 needSync = 1;
63376 }
63377 sqlite3PagerUnrefNotNull(pPage);
63378 }
63379 }
63380
63381 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
63382 ** starting at pg1, then it needs to be set for all of them. Because
63383 ** writing to any of these nPage pages may damage the others, the
63384 ** journal file must contain sync()ed copies of all of them
63385 ** before any of them can be written out to the database file.
63386 */
63387 if( rc==SQLITE_OK0 && needSync ){
63388 assert( !MEMDB )((void) (0));
63389 for(ii=0; ii<nPage; ii++){
63390 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
63391 if( pPage ){
63392 pPage->flags |= PGHDR_NEED_SYNC0x008;
63393 sqlite3PagerUnrefNotNull(pPage);
63394 }
63395 }
63396 }
63397
63398 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 )((void) (0));
63399 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC0x04;
63400 return rc;
63401}
63402
63403/*
63404** Mark a data page as writeable. This routine must be called before
63405** making changes to a page. The caller must check the return value
63406** of this function and be careful not to change any page data unless
63407** this routine returns SQLITE_OK.
63408**
63409** The difference between this function and pager_write() is that this
63410** function also deals with the special case where 2 or more pages
63411** fit on a single disk sector. In this case all co-resident pages
63412** must have been written to the journal file before returning.
63413**
63414** If an error occurs, SQLITE_NOMEM or an IO error code is returned
63415** as appropriate. Otherwise, SQLITE_OK.
63416*/
63417SQLITE_PRIVATEstatic int sqlite3PagerWrite(PgHdr *pPg){
63418 Pager *pPager = pPg->pPager;
63419 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0));
63420 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
63421 assert( assert_pager_state(pPager) )((void) (0));
63422 if( (pPg->flags & PGHDR_WRITEABLE0x004)!=0 && pPager->dbSize>=pPg->pgno ){
63423 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
63424 return SQLITE_OK0;
63425 }else if( pPager->errCode ){
63426 return pPager->errCode;
63427 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
63428 assert( pPager->tempFile==0 )((void) (0));
63429 return pagerWriteLargeSector(pPg);
63430 }else{
63431 return pager_write(pPg);
63432 }
63433}
63434
63435/*
63436** Return TRUE if the page given in the argument was previously passed
63437** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
63438** to change the content of the page.
63439*/
63440#ifndef NDEBUG1
63441SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage *pPg){
63442 return pPg->flags & PGHDR_WRITEABLE0x004;
63443}
63444#endif
63445
63446/*
63447** A call to this routine tells the pager that it is not necessary to
63448** write the information on page pPg back to the disk, even though
63449** that page might be marked as dirty. This happens, for example, when
63450** the page has been added as a leaf of the freelist and so its
63451** content no longer matters.
63452**
63453** The overlying software layer calls this routine when all of the data
63454** on the given page is unused. The pager marks the page as clean so
63455** that it does not get written to disk.
63456**
63457** Tests show that this optimization can quadruple the speed of large
63458** DELETE operations.
63459**
63460** This optimization cannot be used with a temp-file, as the page may
63461** have been dirty at the start of the transaction. In that case, if
63462** memory pressure forces page pPg out of the cache, the data does need
63463** to be written out to disk so that it may be read back in if the
63464** current transaction is rolled back.
63465*/
63466SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(PgHdr *pPg){
63467 Pager *pPager = pPg->pPager;
63468 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY0x002) && pPager->nSavepoint==0 ){
63469 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
63470 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
63471 pPg->flags |= PGHDR_DONT_WRITE0x010;
63472 pPg->flags &= ~PGHDR_WRITEABLE0x004;
63473 testcase( pPg->flags & PGHDR_NEED_SYNC );
63474 pager_set_pagehash(pPg);
63475 }
63476}
63477
63478/*
63479** This routine is called to increment the value of the database file
63480** change-counter, stored as a 4-byte big-endian integer starting at
63481** byte offset 24 of the pager file. The secondary change counter at
63482** 92 is also updated, as is the SQLite version number at offset 96.
63483**
63484** But this only happens if the pPager->changeCountDone flag is false.
63485** To avoid excess churning of page 1, the update only happens once.
63486** See also the pager_write_changecounter() routine that does an
63487** unconditional update of the change counters.
63488**
63489** If the isDirectMode flag is zero, then this is done by calling
63490** sqlite3PagerWrite() on page 1, then modifying the contents of the
63491** page data. In this case the file will be updated when the current
63492** transaction is committed.
63493**
63494** The isDirectMode flag may only be non-zero if the library was compiled
63495** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
63496** if isDirect is non-zero, then the database file is updated directly
63497** by writing an updated version of page 1 using a call to the
63498** sqlite3OsWrite() function.
63499*/
63500static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
63501 int rc = SQLITE_OK0;
63502
63503 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
63504 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
63505 )((void) (0));
63506 assert( assert_pager_state(pPager) )((void) (0));
63507
63508 /* Declare and initialize constant integer 'isDirect'. If the
63509 ** atomic-write optimization is enabled in this build, then isDirect
63510 ** is initialized to the value passed as the isDirectMode parameter
63511 ** to this function. Otherwise, it is always set to zero.
63512 **
63513 ** The idea is that if the atomic-write optimization is not
63514 ** enabled at compile time, the compiler can omit the tests of
63515 ** 'isDirect' below, as well as the block enclosed in the
63516 ** "if( isDirect )" condition.
63517 */
63518#ifndef SQLITE_ENABLE_ATOMIC_WRITE
63519# define DIRECT_MODE0 0
63520 assert( isDirectMode==0 )((void) (0));
63521 UNUSED_PARAMETER(isDirectMode)(void)(isDirectMode);
63522#else
63523# define DIRECT_MODE0 isDirectMode
63524#endif
63525
63526 if( !pPager->changeCountDone && pPager->dbSize>0 ){
63527 PgHdr *pPgHdr; /* Reference to page 1 */
63528
63529 assert( !pPager->tempFile && isOpen(pPager->fd) )((void) (0));
63530
63531 /* Open page 1 of the file for writing. */
63532 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
63533 assert( pPgHdr==0 || rc==SQLITE_OK )((void) (0));
63534
63535 /* If page one was fetched successfully, and this function is not
63536 ** operating in direct-mode, make page 1 writable. When not in
63537 ** direct mode, page 1 is always held in cache and hence the PagerGet()
63538 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
63539 */
63540 if( !DIRECT_MODE0 && ALWAYS(rc==SQLITE_OK)(rc==0) ){
63541 rc = sqlite3PagerWrite(pPgHdr);
63542 }
63543
63544 if( rc==SQLITE_OK0 ){
63545 /* Actually do the update of the change counter */
63546 pager_write_changecounter(pPgHdr);
63547
63548 /* If running in direct mode, write the contents of page 1 to the file. */
63549 if( DIRECT_MODE0 ){
63550 const void *zBuf;
63551 assert( pPager->dbFileSize>0 )((void) (0));
63552 zBuf = pPgHdr->pData;
63553 if( rc==SQLITE_OK0 ){
63554 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
63555 pPager->aStat[PAGER_STAT_WRITE2]++;
63556 }
63557 if( rc==SQLITE_OK0 ){
63558 /* Update the pager's copy of the change-counter. Otherwise, the
63559 ** next time a read transaction is opened the cache will be
63560 ** flushed (as the change-counter values will not match). */
63561 const void *pCopy = (const void *)&((const char *)zBuf)[24];
63562 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
63563 pPager->changeCountDone = 1;
63564 }
63565 }else{
63566 pPager->changeCountDone = 1;
63567 }
63568 }
63569
63570 /* Release the page reference. */
63571 sqlite3PagerUnref(pPgHdr);
63572 }
63573 return rc;
63574}
63575
63576/*
63577** Sync the database file to disk. This is a no-op for in-memory databases
63578** or pages with the Pager.noSync flag set.
63579**
63580** If successful, or if called on a pager for which it is a no-op, this
63581** function returns SQLITE_OK. Otherwise, an IO error code is returned.
63582*/
63583SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zSuper){
63584 int rc = SQLITE_OK0;
63585 void *pArg = (void*)zSuper;
63586 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC21, pArg);
63587 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
63588 if( rc==SQLITE_OK0 && !pPager->noSync ){
63589 assert( !MEMDB )((void) (0));
63590 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
63591 }
63592 return rc;
63593}
63594
63595/*
63596** This function may only be called while a write-transaction is active in
63597** rollback. If the connection is in WAL mode, this call is a no-op.
63598** Otherwise, if the connection does not already have an EXCLUSIVE lock on
63599** the database file, an attempt is made to obtain one.
63600**
63601** If the EXCLUSIVE lock is already held or the attempt to obtain it is
63602** successful, or the connection is in WAL mode, SQLITE_OK is returned.
63603** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
63604** returned.
63605*/
63606SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager *pPager){
63607 int rc = pPager->errCode;
63608 assert( assert_pager_state(pPager) )((void) (0));
63609 if( rc==SQLITE_OK0 ){
63610 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
63611 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
63612 || pPager->eState==PAGER_WRITER_LOCKED((void) (0))
63613 )((void) (0));
63614 assert( assert_pager_state(pPager) )((void) (0));
63615 if( 0==pagerUseWal(pPager)((pPager)->pWal!=0) ){
63616 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
63617 }
63618 }
63619 return rc;
63620}
63621
63622/*
63623** Sync the database file for the pager pPager. zSuper points to the name
63624** of a super-journal file that should be written into the individual
63625** journal file. zSuper may be NULL, which is interpreted as no
63626** super-journal (a single database transaction).
63627**
63628** This routine ensures that:
63629**
63630** * The database file change-counter is updated,
63631** * the journal is synced (unless the atomic-write optimization is used),
63632** * all dirty pages are written to the database file,
63633** * the database file is truncated (if required), and
63634** * the database file synced.
63635**
63636** The only thing that remains to commit the transaction is to finalize
63637** (delete, truncate or zero the first part of) the journal file (or
63638** delete the super-journal file if specified).
63639**
63640** Note that if zSuper==NULL, this does not overwrite a previous value
63641** passed to an sqlite3PagerCommitPhaseOne() call.
63642**
63643** If the final parameter - noSync - is true, then the database file itself
63644** is not synced. The caller must call sqlite3PagerSync() directly to
63645** sync the database file before calling CommitPhaseTwo() to delete the
63646** journal file in this case.
63647*/
63648SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(
63649 Pager *pPager, /* Pager object */
63650 const char *zSuper, /* If not NULL, the super-journal name */
63651 int noSync /* True to omit the xSync on the db file */
63652){
63653 int rc = SQLITE_OK0; /* Return code */
63654
63655 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
63656 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
63657 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
63658 || pPager->eState==PAGER_ERROR((void) (0))
63659 )((void) (0));
63660 assert( assert_pager_state(pPager) )((void) (0));
63661
63662 /* If a prior error occurred, report that error again. */
63663 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
63664
63665 /* Provide the ability to easily simulate an I/O error during testing */
63666 if( sqlite3FaultSim(400) ) return SQLITE_IOERR10;
63667
63668 PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
63669 pPager->zFilename, zSuper, pPager->dbSize));
63670
63671 /* If no database changes have been made, return early. */
63672 if( pPager->eState<PAGER_WRITER_CACHEMOD3 ) return SQLITE_OK0;
63673
63674 assert( MEMDB==0 || pPager->tempFile )((void) (0));
63675 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
63676 if( 0==pagerFlushOnCommit(pPager, 1) ){
63677 /* If this is an in-memory db, or no pages have been written to, or this
63678 ** function has already been called, it is mostly a no-op. However, any
63679 ** backup in progress needs to be restarted. */
63680 sqlite3BackupRestart(pPager->pBackup);
63681 }else{
63682 PgHdr *pList;
63683 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
63684 PgHdr *pPageOne = 0;
63685 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63686 if( pList==0 ){
63687 /* Must have at least one page for the WAL commit flag.
63688 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
63689 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
63690 pList = pPageOne;
63691 pList->pDirty = 0;
63692 }
63693 assert( rc==SQLITE_OK )((void) (0));
63694 if( ALWAYS(pList)(pList) ){
63695 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
63696 }
63697 sqlite3PagerUnref(pPageOne);
63698 if( rc==SQLITE_OK0 ){
63699 sqlite3PcacheCleanAll(pPager->pPCache);
63700 }
63701 }else{
63702 /* The bBatch boolean is true if the batch-atomic-write commit method
63703 ** should be used. No rollback journal is created if batch-atomic-write
63704 ** is enabled.
63705 */
63706#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
63707 sqlite3_file *fd = pPager->fd;
63708 int bBatch0 = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
63709 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC0x00004000)
63710 && !pPager->noSync
63711 && sqlite3JournalIsInMemory(pPager->jfd);
63712#else
63713# define bBatch0 0
63714#endif
63715
63716#ifdef SQLITE_ENABLE_ATOMIC_WRITE
63717 /* The following block updates the change-counter. Exactly how it
63718 ** does this depends on whether or not the atomic-update optimization
63719 ** was enabled at compile time, and if this transaction meets the
63720 ** runtime criteria to use the operation:
63721 **
63722 ** * The file-system supports the atomic-write property for
63723 ** blocks of size page-size, and
63724 ** * This commit is not part of a multi-file transaction, and
63725 ** * Exactly one page has been modified and store in the journal file.
63726 **
63727 ** If the optimization was not enabled at compile time, then the
63728 ** pager_incr_changecounter() function is called to update the change
63729 ** counter in 'indirect-mode'. If the optimization is compiled in but
63730 ** is not applicable to this transaction, call sqlite3JournalCreate()
63731 ** to make sure the journal file has actually been created, then call
63732 ** pager_incr_changecounter() to update the change-counter in indirect
63733 ** mode.
63734 **
63735 ** Otherwise, if the optimization is both enabled and applicable,
63736 ** then call pager_incr_changecounter() to update the change-counter
63737 ** in 'direct' mode. In this case the journal file will never be
63738 ** created for this transaction.
63739 */
63740 if( bBatch0==0 ){
63741 PgHdr *pPg;
63742 assert( isOpen(pPager->jfd)((void) (0))
63743 || pPager->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
63744 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
63745 )((void) (0));
63746 if( !zSuper && isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
63747 && pPager->journalOff==jrnlBufferSize(pPager)
63748 && pPager->dbSize>=pPager->dbOrigSize
63749 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
63750 ){
63751 /* Update the db file change counter via the direct-write method. The
63752 ** following call will modify the in-memory representation of page 1
63753 ** to include the updated change counter and then write page 1
63754 ** directly to the database file. Because of the atomic-write
63755 ** property of the host file-system, this is safe.
63756 */
63757 rc = pager_incr_changecounter(pPager, 1);
63758 }else{
63759 rc = sqlite3JournalCreate(pPager->jfd);
63760 if( rc==SQLITE_OK0 ){
63761 rc = pager_incr_changecounter(pPager, 0);
63762 }
63763 }
63764 }
63765#else /* SQLITE_ENABLE_ATOMIC_WRITE */
63766#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
63767 if( zSuper ){
63768 rc = sqlite3JournalCreate(pPager->jfd);
63769 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
63770 assert( bBatch==0 )((void) (0));
63771 }
63772#endif
63773 rc = pager_incr_changecounter(pPager, 0);
63774#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
63775 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
63776
63777 /* Write the super-journal name into the journal file. If a
63778 ** super-journal file name has already been written to the journal file,
63779 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
63780 */
63781 rc = writeSuperJournal(pPager, zSuper);
63782 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
63783
63784 /* Sync the journal file and write all dirty pages to the database.
63785 ** If the atomic-update optimization is being used, this sync will not
63786 ** create the journal file or perform any real IO.
63787 **
63788 ** Because the change-counter page was just modified, unless the
63789 ** atomic-update optimization is used it is almost certain that the
63790 ** journal requires a sync here. However, in locking_mode=exclusive
63791 ** on a system under memory pressure it is just possible that this is
63792 ** not the case. In this case it is likely enough that the redundant
63793 ** xSync() call will be changed to a no-op by the OS anyhow.
63794 */
63795 rc = syncJournal(pPager, 0);
63796 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
63797
63798 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63799#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
63800 if( bBatch0 ){
63801 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31, 0);
63802 if( rc==SQLITE_OK0 ){
63803 rc = pager_write_pagelist(pPager, pList);
63804 if( rc==SQLITE_OK0 && pPager->dbSize>pPager->dbFileSize ){
63805 char *pTmp = pPager->pTmpSpace;
63806 int szPage = (int)pPager->pageSize;
63807 memset(pTmp, 0, szPage);
63808 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
63809 ((i64)pPager->dbSize*pPager->pageSize)-szPage);
63810 }
63811 if( rc==SQLITE_OK0 ){
63812 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32, 0);
63813 }
63814 if( rc!=SQLITE_OK0 ){
63815 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33, 0);
63816 }
63817 }
63818
63819 if( (rc&0xFF)==SQLITE_IOERR10 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
63820 rc = sqlite3JournalCreate(pPager->jfd);
63821 if( rc!=SQLITE_OK0 ){
63822 sqlite3OsClose(pPager->jfd);
63823 goto commit_phase_one_exit;
63824 }
63825 bBatch0 = 0;
63826 }else{
63827 sqlite3OsClose(pPager->jfd);
63828 }
63829 }
63830#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
63831
63832 if( bBatch0==0 ){
63833 rc = pager_write_pagelist(pPager, pList);
63834 }
63835 if( rc!=SQLITE_OK0 ){
63836 assert( rc!=SQLITE_IOERR_BLOCKED )((void) (0));
63837 goto commit_phase_one_exit;
63838 }
63839 sqlite3PcacheCleanAll(pPager->pPCache);
63840
63841 /* If the file on disk is smaller than the database image, use
63842 ** pager_truncate to grow the file here. This can happen if the database
63843 ** image was extended as part of the current transaction and then the
63844 ** last page in the db image moved to the free-list. In this case the
63845 ** last page is never written out to disk, leaving the database file
63846 ** undersized. Fix this now if it is the case. */
63847 if( pPager->dbSize>pPager->dbFileSize ){
63848 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager)((pPager)->lckPgno));
63849 assert( pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
63850 rc = pager_truncate(pPager, nNew);
63851 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
63852 }
63853
63854 /* Finally, sync the database file. */
63855 if( !noSync ){
63856 rc = sqlite3PagerSync(pPager, zSuper);
63857 }
63858 IOTRACE(("DBSYNC %p\n", pPager))
63859 }
63860 }
63861
63862commit_phase_one_exit:
63863 if( rc==SQLITE_OK0 && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
63864 pPager->eState = PAGER_WRITER_FINISHED5;
63865 }
63866 return rc;
63867}
63868
63869
63870/*
63871** When this function is called, the database file has been completely
63872** updated to reflect the changes made by the current transaction and
63873** synced to disk. The journal file still exists in the file-system
63874** though, and if a failure occurs at this point it will eventually
63875** be used as a hot-journal and the current transaction rolled back.
63876**
63877** This function finalizes the journal file, either by deleting,
63878** truncating or partially zeroing it, so that it cannot be used
63879** for hot-journal rollback. Once this is done the transaction is
63880** irrevocably committed.
63881**
63882** If an error occurs, an IO error code is returned and the pager
63883** moves into the error state. Otherwise, SQLITE_OK is returned.
63884*/
63885SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager *pPager){
63886 int rc = SQLITE_OK0; /* Return code */
63887
63888 /* This routine should not be called if a prior error has occurred.
63889 ** But if (due to a coding error elsewhere in the system) it does get
63890 ** called, just return the same error code without doing anything. */
63891 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
63892 pPager->iDataVersion++;
63893
63894 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
63895 || pPager->eState==PAGER_WRITER_FINISHED((void) (0))
63896 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)((void) (0))
63897 )((void) (0));
63898 assert( assert_pager_state(pPager) )((void) (0));
63899
63900 /* An optimization. If the database was not actually modified during
63901 ** this transaction, the pager is running in exclusive-mode and is
63902 ** using persistent journals, then this function is a no-op.
63903 **
63904 ** The start of the journal file currently contains a single journal
63905 ** header with the nRec field set to 0. If such a journal is used as
63906 ** a hot-journal during hot-journal rollback, 0 changes will be made
63907 ** to the database file. So there is no need to zero the journal
63908 ** header. Since the pager is in exclusive mode, there is no need
63909 ** to drop any locks either.
63910 */
63911 if( pPager->eState==PAGER_WRITER_LOCKED2
63912 && pPager->exclusiveMode
63913 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
63914 ){
63915 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff )((void) (0));
63916 pPager->eState = PAGER_READER1;
63917 return SQLITE_OK0;
63918 }
63919
63920 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
63921 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
63922 return pager_error(pPager, rc);
63923}
63924
63925/*
63926** If a write transaction is open, then all changes made within the
63927** transaction are reverted and the current write-transaction is closed.
63928** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
63929** state if an error occurs.
63930**
63931** If the pager is already in PAGER_ERROR state when this function is called,
63932** it returns Pager.errCode immediately. No work is performed in this case.
63933**
63934** Otherwise, in rollback mode, this function performs two functions:
63935**
63936** 1) It rolls back the journal file, restoring all database file and
63937** in-memory cache pages to the state they were in when the transaction
63938** was opened, and
63939**
63940** 2) It finalizes the journal file, so that it is not used for hot
63941** rollback at any point in the future.
63942**
63943** Finalization of the journal file (task 2) is only performed if the
63944** rollback is successful.
63945**
63946** In WAL mode, all cache-entries containing data modified within the
63947** current transaction are either expelled from the cache or reverted to
63948** their pre-transaction state by re-reading data from the database or
63949** WAL files. The WAL transaction is then closed.
63950*/
63951SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager *pPager){
63952 int rc = SQLITE_OK0; /* Return code */
63953 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
63954
63955 /* PagerRollback() is a no-op if called in READER or OPEN state. If
63956 ** the pager is already in the ERROR state, the rollback is not
63957 ** attempted here. Instead, the error code is returned to the caller.
63958 */
63959 assert( assert_pager_state(pPager) )((void) (0));
63960 if( pPager->eState==PAGER_ERROR6 ) return pPager->errCode;
63961 if( pPager->eState<=PAGER_READER1 ) return SQLITE_OK0;
63962
63963 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
63964 int rc2;
63965 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK2, -1);
63966 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
63967 if( rc==SQLITE_OK0 ) rc = rc2;
63968 }else if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) || pPager->eState==PAGER_WRITER_LOCKED2 ){
63969 int eState = pPager->eState;
63970 rc = pager_end_transaction(pPager, 0, 0);
63971 if( !MEMDBpPager->memDb && eState>PAGER_WRITER_LOCKED2 ){
63972 /* This can happen using journal_mode=off. Move the pager to the error
63973 ** state to indicate that the contents of the cache may not be trusted.
63974 ** Any active readers will get SQLITE_ABORT.
63975 */
63976 pPager->errCode = SQLITE_ABORT4;
63977 pPager->eState = PAGER_ERROR6;
63978 setGetterMethod(pPager);
63979 return rc;
63980 }
63981 }else{
63982 rc = pager_playback(pPager, 0);
63983 }
63984
63985 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK )((void) (0));
63986 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT((void) (0))
63987 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR((void) (0))
63988 || rc==SQLITE_CANTOPEN((void) (0))
63989 )((void) (0));
63990
63991 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
63992 ** cache. So call pager_error() on the way out to make any error persistent.
63993 */
63994 return pager_error(pPager, rc);
63995}
63996
63997/*
63998** Return TRUE if the database file is opened read-only. Return FALSE
63999** if the database is (in theory) writable.
64000*/
64001SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager *pPager){
64002 return pPager->readOnly;
64003}
64004
64005#ifdef SQLITE_DEBUG
64006/*
64007** Return the sum of the reference counts for all pages held by pPager.
64008*/
64009SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager *pPager){
64010 return sqlite3PcacheRefCount(pPager->pPCache);
64011}
64012#endif
64013
64014/*
64015** Return the approximate number of bytes of memory currently
64016** used by the pager and its associated cache.
64017*/
64018SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager *pPager){
64019 int perPageSize = pPager->pageSize + pPager->nExtra
64020 + (int)(sizeof(PgHdr) + 5*sizeof(void*));
64021 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
64022 + sqlite3MallocSize(pPager)
64023 + pPager->pageSize;
64024}
64025
64026/*
64027** Return the number of references to the specified page.
64028*/
64029SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage *pPage){
64030 return sqlite3PcachePageRefcount(pPage);
64031}
64032
64033#ifdef SQLITE_TEST
64034/*
64035** This routine is used for testing and analysis only.
64036*/
64037SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager *pPager){
64038 static int a[11];
64039 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
64040 a[1] = sqlite3PcachePagecount(pPager->pPCache);
64041 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
64042 a[3] = pPager->eState==PAGER_OPEN0 ? -1 : (int) pPager->dbSize;
64043 a[4] = pPager->eState;
64044 a[5] = pPager->errCode;
64045 a[6] = (int)pPager->aStat[PAGER_STAT_HIT0] & 0x7fffffff;
64046 a[7] = (int)pPager->aStat[PAGER_STAT_MISS1] & 0x7fffffff;
64047 a[8] = 0; /* Used to be pPager->nOvfl */
64048 a[9] = pPager->nRead;
64049 a[10] = (int)pPager->aStat[PAGER_STAT_WRITE2] & 0x7fffffff;
64050 return a;
64051}
64052#endif
64053
64054/*
64055** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
64056** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
64057** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
64058** it was added later.
64059**
64060** Before returning, *pnVal is incremented by the
64061** current cache hit or miss count, according to the value of eStat. If the
64062** reset parameter is non-zero, the cache hit or miss count is zeroed before
64063** returning.
64064*/
64065SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){
64066
64067 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT((void) (0))
64068 || eStat==SQLITE_DBSTATUS_CACHE_MISS((void) (0))
64069 || eStat==SQLITE_DBSTATUS_CACHE_WRITE((void) (0))
64070 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1((void) (0))
64071 )((void) (0));
64072
64073 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS )((void) (0));
64074 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE )((void) (0));
64075 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1((void) (0))
64076 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 )((void) (0));
64077
64078 eStat -= SQLITE_DBSTATUS_CACHE_HIT7;
64079 *pnVal += pPager->aStat[eStat];
64080 if( reset ){
64081 pPager->aStat[eStat] = 0;
64082 }
64083}
64084
64085/*
64086** Return true if this is an in-memory or temp-file backed pager.
64087*/
64088SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager *pPager){
64089 return pPager->tempFile || pPager->memVfs;
64090}
64091
64092/*
64093** Check that there are at least nSavepoint savepoints open. If there are
64094** currently less than nSavepoints open, then open one or more savepoints
64095** to make up the difference. If the number of savepoints is already
64096** equal to nSavepoint, then this function is a no-op.
64097**
64098** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
64099** occurs while opening the sub-journal file, then an IO error code is
64100** returned. Otherwise, SQLITE_OK.
64101*/
64102static SQLITE_NOINLINE__attribute__((noinline)) int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
64103 int rc = SQLITE_OK0; /* Return code */
64104 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
64105 int ii; /* Iterator variable */
64106 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
64107
64108 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
64109 assert( assert_pager_state(pPager) )((void) (0));
64110 assert( nSavepoint>nCurrent && pPager->useJournal )((void) (0));
64111
64112 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
64113 ** if the allocation fails. Otherwise, zero the new portion in case a
64114 ** malloc failure occurs while populating it in the for(...) loop below.
64115 */
64116 aNew = (PagerSavepoint *)sqlite3Realloc(
64117 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
64118 );
64119 if( !aNew ){
64120 return SQLITE_NOMEM_BKPT7;
64121 }
64122 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
64123 pPager->aSavepoint = aNew;
64124
64125 /* Populate the PagerSavepoint structures just allocated. */
64126 for(ii=nCurrent; ii<nSavepoint; ii++){
64127 aNew[ii].nOrig = pPager->dbSize;
64128 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalOff>0 ){
64129 aNew[ii].iOffset = pPager->journalOff;
64130 }else{
64131 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
64132 }
64133 aNew[ii].iSubRec = pPager->nSubRec;
64134 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
64135 aNew[ii].bTruncateOnRelease = 1;
64136 if( !aNew[ii].pInSavepoint ){
64137 return SQLITE_NOMEM_BKPT7;
64138 }
64139 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
64140 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
64141 }
64142 pPager->nSavepoint = ii+1;
64143 }
64144 assert( pPager->nSavepoint==nSavepoint )((void) (0));
64145 assertTruncateConstraint(pPager);
64146 return rc;
64147}
64148SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
64149 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
64150 assert( assert_pager_state(pPager) )((void) (0));
64151
64152 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
64153 return pagerOpenSavepoint(pPager, nSavepoint);
64154 }else{
64155 return SQLITE_OK0;
64156 }
64157}
64158
64159
64160/*
64161** This function is called to rollback or release (commit) a savepoint.
64162** The savepoint to release or rollback need not be the most recently
64163** created savepoint.
64164**
64165** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
64166** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
64167** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
64168** that have occurred since the specified savepoint was created.
64169**
64170** The savepoint to rollback or release is identified by parameter
64171** iSavepoint. A value of 0 means to operate on the outermost savepoint
64172** (the first created). A value of (Pager.nSavepoint-1) means operate
64173** on the most recently created savepoint. If iSavepoint is greater than
64174** (Pager.nSavepoint-1), then this function is a no-op.
64175**
64176** If a negative value is passed to this function, then the current
64177** transaction is rolled back. This is different to calling
64178** sqlite3PagerRollback() because this function does not terminate
64179** the transaction or unlock the database, it just restores the
64180** contents of the database to its original state.
64181**
64182** In any case, all savepoints with an index greater than iSavepoint
64183** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
64184** then savepoint iSavepoint is also destroyed.
64185**
64186** This function may return SQLITE_NOMEM if a memory allocation fails,
64187** or an IO error code if an IO error occurs while rolling back a
64188** savepoint. If no errors occur, SQLITE_OK is returned.
64189*/
64190SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
64191 int rc = pPager->errCode;
64192
64193#ifdef SQLITE_ENABLE_ZIPVFS
64194 if( op==SAVEPOINT_RELEASE1 ) rc = SQLITE_OK0;
64195#endif
64196
64197 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
64198 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK )((void) (0));
64199
64200 if( rc==SQLITE_OK0 && iSavepoint<pPager->nSavepoint ){
64201 int ii; /* Iterator variable */
64202 int nNew; /* Number of remaining savepoints after this op. */
64203
64204 /* Figure out how many savepoints will still be active after this
64205 ** operation. Store this value in nNew. Then free resources associated
64206 ** with any savepoints that are destroyed by this operation.
64207 */
64208 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE1 ) ? 0 : 1);
64209 for(ii=nNew; ii<pPager->nSavepoint; ii++){
64210 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
64211 }
64212 pPager->nSavepoint = nNew;
64213
64214 /* Truncate the sub-journal so that it only includes the parts
64215 ** that are still in use. */
64216 if( op==SAVEPOINT_RELEASE1 ){
64217 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
64218 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
64219 /* Only truncate if it is an in-memory sub-journal. */
64220 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
64221 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
64222 rc = sqlite3OsTruncate(pPager->sjfd, sz);
64223 assert( rc==SQLITE_OK )((void) (0));
64224 }
64225 pPager->nSubRec = pRel->iSubRec;
64226 }
64227 }
64228 /* Else this is a rollback operation, playback the specified savepoint.
64229 ** If this is a temp-file, it is possible that the journal file has
64230 ** not yet been opened. In this case there have been no changes to
64231 ** the database file, so the playback operation can be skipped.
64232 */
64233 else if( pagerUseWal(pPager)((pPager)->pWal!=0) || isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
64234 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
64235 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
64236 assert(rc!=SQLITE_DONE)((void) (0));
64237 }
64238
64239#ifdef SQLITE_ENABLE_ZIPVFS
64240 /* If the cache has been modified but the savepoint cannot be rolled
64241 ** back journal_mode=off, put the pager in the error state. This way,
64242 ** if the VFS used by this pager includes ZipVFS, the entire transaction
64243 ** can be rolled back at the ZipVFS level. */
64244 else if(
64245 pPager->journalMode==PAGER_JOURNALMODE_OFF2
64246 && pPager->eState>=PAGER_WRITER_CACHEMOD3
64247 ){
64248 pPager->errCode = SQLITE_ABORT4;
64249 pPager->eState = PAGER_ERROR6;
64250 setGetterMethod(pPager);
64251 }
64252#endif
64253 }
64254
64255 return rc;
64256}
64257
64258/*
64259** Return the full pathname of the database file.
64260**
64261** Except, if the pager is in-memory only, then return an empty string if
64262** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
64263** used to report the filename to the user, for compatibility with legacy
64264** behavior. But when the Btree needs to know the filename for matching to
64265** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
64266** participate in shared-cache.
64267**
64268** The return value to this routine is always safe to use with
64269** sqlite3_uri_parameter() and sqlite3_filename_database() and friends.
64270*/
64271SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){
64272 static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
64273 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
64274 return &zFake[4];
64275 }else{
64276 return pPager->zFilename;
64277 }
64278}
64279
64280/*
64281** Return the VFS structure for the pager.
64282*/
64283SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
64284 return pPager->pVfs;
64285}
64286
64287/*
64288** Return the file handle for the database file associated
64289** with the pager. This might return NULL if the file has
64290** not yet been opened.
64291*/
64292SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager *pPager){
64293 return pPager->fd;
64294}
64295
64296/*
64297** Return the file handle for the journal file (if it exists).
64298** This will be either the rollback journal or the WAL file.
64299*/
64300SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
64301#ifdef SQLITE_OMIT_WAL
64302 return pPager->jfd;
64303#else
64304 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
64305#endif
64306}
64307
64308/*
64309** Return the full pathname of the journal file.
64310*/
64311SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager *pPager){
64312 return pPager->zJournal;
64313}
64314
64315#ifndef SQLITE_OMIT_AUTOVACUUM
64316/*
64317** Move the page pPg to location pgno in the file.
64318**
64319** There must be no references to the page previously located at
64320** pgno (which we call pPgOld) though that page is allowed to be
64321** in cache. If the page previously located at pgno is not already
64322** in the rollback journal, it is not put there by by this routine.
64323**
64324** References to the page pPg remain valid. Updating any
64325** meta-data associated with pPg (i.e. data stored in the nExtra bytes
64326** allocated along with the page) is the responsibility of the caller.
64327**
64328** A transaction must be active when this routine is called. It used to be
64329** required that a statement transaction was not active, but this restriction
64330** has been removed (CREATE INDEX needs to move a page when a statement
64331** transaction is active).
64332**
64333** If the fourth argument, isCommit, is non-zero, then this page is being
64334** moved as part of a database reorganization just before the transaction
64335** is being committed. In this case, it is guaranteed that the database page
64336** pPg refers to will not be written to again within this transaction.
64337**
64338** This function may return SQLITE_NOMEM or an IO error code if an error
64339** occurs. Otherwise, it returns SQLITE_OK.
64340*/
64341SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
64342 PgHdr *pPgOld; /* The page being overwritten. */
64343 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
64344 int rc; /* Return code */
64345 Pgno origPgno; /* The original page number */
64346
64347 assert( pPg->nRef>0 )((void) (0));
64348 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
64349 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
64350 )((void) (0));
64351 assert( assert_pager_state(pPager) )((void) (0));
64352
64353 /* In order to be able to rollback, an in-memory database must journal
64354 ** the page we are moving from.
64355 */
64356 assert( pPager->tempFile || !MEMDB )((void) (0));
64357 if( pPager->tempFile ){
64358 rc = sqlite3PagerWrite(pPg);
64359 if( rc ) return rc;
64360 }
64361
64362 /* If the page being moved is dirty and has not been saved by the latest
64363 ** savepoint, then save the current contents of the page into the
64364 ** sub-journal now. This is required to handle the following scenario:
64365 **
64366 ** BEGIN;
64367 ** <journal page X, then modify it in memory>
64368 ** SAVEPOINT one;
64369 ** <Move page X to location Y>
64370 ** ROLLBACK TO one;
64371 **
64372 ** If page X were not written to the sub-journal here, it would not
64373 ** be possible to restore its contents when the "ROLLBACK TO one"
64374 ** statement were is processed.
64375 **
64376 ** subjournalPage() may need to allocate space to store pPg->pgno into
64377 ** one or more savepoint bitvecs. This is the reason this function
64378 ** may return SQLITE_NOMEM.
64379 */
64380 if( (pPg->flags & PGHDR_DIRTY0x002)!=0
64381 && SQLITE_OK0!=(rc = subjournalPageIfRequired(pPg))
64382 ){
64383 return rc;
64384 }
64385
64386 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
64387 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
64388 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
64389
64390 /* If the journal needs to be sync()ed before page pPg->pgno can
64391 ** be written to, store pPg->pgno in local variable needSyncPgno.
64392 **
64393 ** If the isCommit flag is set, there is no need to remember that
64394 ** the journal needs to be sync()ed before database page pPg->pgno
64395 ** can be written to. The caller has already promised not to write to it.
64396 */
64397 if( (pPg->flags&PGHDR_NEED_SYNC0x008) && !isCommit ){
64398 needSyncPgno = pPg->pgno;
64399 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||((void) (0))
64400 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize )((void) (0));
64401 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
64402 }
64403
64404 /* If the cache contains a page with page-number pgno, remove it
64405 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
64406 ** page pgno before the 'move' operation, it needs to be retained
64407 ** for the page moved there.
64408 */
64409 pPg->flags &= ~PGHDR_NEED_SYNC0x008;
64410 pPgOld = sqlite3PagerLookup(pPager, pgno);
64411 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB )((void) (0));
64412 if( pPgOld ){
64413 if( NEVER(pPgOld->nRef>1)(pPgOld->nRef>1) ){
64414 sqlite3PagerUnrefNotNull(pPgOld);
64415 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(64415);
64416 }
64417 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC0x008);
64418 if( pPager->tempFile ){
64419 /* Do not discard pages from an in-memory database since we might
64420 ** need to rollback later. Just move the page out of the way. */
64421 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
64422 }else{
64423 sqlite3PcacheDrop(pPgOld);
64424 }
64425 }
64426
64427 origPgno = pPg->pgno;
64428 sqlite3PcacheMove(pPg, pgno);
64429 sqlite3PcacheMakeDirty(pPg);
64430
64431 /* For an in-memory database, make sure the original page continues
64432 ** to exist, in case the transaction needs to roll back. Use pPgOld
64433 ** as the original page since it has already been allocated.
64434 */
64435 if( pPager->tempFile && pPgOld ){
64436 sqlite3PcacheMove(pPgOld, origPgno);
64437 sqlite3PagerUnrefNotNull(pPgOld);
64438 }
64439
64440 if( needSyncPgno ){
64441 /* If needSyncPgno is non-zero, then the journal file needs to be
64442 ** sync()ed before any data is written to database file page needSyncPgno.
64443 ** Currently, no such page exists in the page-cache and the
64444 ** "is journaled" bitvec flag has been set. This needs to be remedied by
64445 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
64446 ** flag.
64447 **
64448 ** If the attempt to load the page into the page-cache fails, (due
64449 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
64450 ** array. Otherwise, if the page is loaded and written again in
64451 ** this transaction, it may be written to the database file before
64452 ** it is synced into the journal file. This way, it may end up in
64453 ** the journal file twice, but that is not a problem.
64454 */
64455 PgHdr *pPgHdr;
64456 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
64457 if( rc!=SQLITE_OK0 ){
64458 if( needSyncPgno<=pPager->dbOrigSize ){
64459 assert( pPager->pTmpSpace!=0 )((void) (0));
64460 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
64461 }
64462 return rc;
64463 }
64464 pPgHdr->flags |= PGHDR_NEED_SYNC0x008;
64465 sqlite3PcacheMakeDirty(pPgHdr);
64466 sqlite3PagerUnrefNotNull(pPgHdr);
64467 }
64468
64469 return SQLITE_OK0;
64470}
64471#endif
64472
64473/*
64474** The page handle passed as the first argument refers to a dirty page
64475** with a page number other than iNew. This function changes the page's
64476** page number to iNew and sets the value of the PgHdr.flags field to
64477** the value passed as the third parameter.
64478*/
64479SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
64480 assert( pPg->pgno!=iNew )((void) (0));
64481 pPg->flags = flags;
64482 sqlite3PcacheMove(pPg, iNew);
64483}
64484
64485/*
64486** Return a pointer to the data for the specified page.
64487*/
64488SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *pPg){
64489 assert( pPg->nRef>0 || pPg->pPager->memDb )((void) (0));
64490 return pPg->pData;
64491}
64492
64493/*
64494** Return a pointer to the Pager.nExtra bytes of "extra" space
64495** allocated along with the specified page.
64496*/
64497SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *pPg){
64498 return pPg->pExtra;
64499}
64500
64501/*
64502** Get/set the locking-mode for this pager. Parameter eMode must be one
64503** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
64504** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
64505** the locking-mode is set to the value specified.
64506**
64507** The returned value is either PAGER_LOCKINGMODE_NORMAL or
64508** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
64509** locking-mode.
64510*/
64511SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *pPager, int eMode){
64512 assert( eMode==PAGER_LOCKINGMODE_QUERY((void) (0))
64513 || eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
64514 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
64515 assert( PAGER_LOCKINGMODE_QUERY<0 )((void) (0));
64516 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 )((void) (0));
64517 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) )((void) (0));
64518 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
64519 pPager->exclusiveMode = (u8)eMode;
64520 }
64521 return (int)pPager->exclusiveMode;
64522}
64523
64524/*
64525** Set the journal-mode for this pager. Parameter eMode must be one of:
64526**
64527** PAGER_JOURNALMODE_DELETE
64528** PAGER_JOURNALMODE_TRUNCATE
64529** PAGER_JOURNALMODE_PERSIST
64530** PAGER_JOURNALMODE_OFF
64531** PAGER_JOURNALMODE_MEMORY
64532** PAGER_JOURNALMODE_WAL
64533**
64534** The journalmode is set to the value specified if the change is allowed.
64535** The change may be disallowed for the following reasons:
64536**
64537** * An in-memory database can only have its journal_mode set to _OFF
64538** or _MEMORY.
64539**
64540** * Temporary databases cannot have _WAL journalmode.
64541**
64542** The returned indicate the current (possibly updated) journal-mode.
64543*/
64544SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
64545 u8 eOld = pPager->journalMode; /* Prior journalmode */
64546
64547 /* The eMode parameter is always valid */
64548 assert( eMode==PAGER_JOURNALMODE_DELETE /* 0 */((void) (0))
64549 || eMode==PAGER_JOURNALMODE_PERSIST /* 1 */((void) (0))
64550 || eMode==PAGER_JOURNALMODE_OFF /* 2 */((void) (0))
64551 || eMode==PAGER_JOURNALMODE_TRUNCATE /* 3 */((void) (0))
64552 || eMode==PAGER_JOURNALMODE_MEMORY /* 4 */((void) (0))
64553 || eMode==PAGER_JOURNALMODE_WAL /* 5 */ )((void) (0));
64554
64555 /* This routine is only called from the OP_JournalMode opcode, and
64556 ** the logic there will never allow a temporary file to be changed
64557 ** to WAL mode.
64558 */
64559 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL )((void) (0));
64560
64561 /* Do allow the journalmode of an in-memory database to be set to
64562 ** anything other than MEMORY or OFF
64563 */
64564 if( MEMDBpPager->memDb ){
64565 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF )((void) (0));
64566 if( eMode!=PAGER_JOURNALMODE_MEMORY4 && eMode!=PAGER_JOURNALMODE_OFF2 ){
64567 eMode = eOld;
64568 }
64569 }
64570
64571 if( eMode!=eOld ){
64572
64573 /* Change the journal mode. */
64574 assert( pPager->eState!=PAGER_ERROR )((void) (0));
64575 pPager->journalMode = (u8)eMode;
64576
64577 /* When transitioning from TRUNCATE or PERSIST to any other journal
64578 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
64579 ** delete the journal file.
64580 */
64581 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
64582 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
64583 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 )((void) (0));
64584 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 )((void) (0));
64585 assert( (PAGER_JOURNALMODE_OFF & 5)==0 )((void) (0));
64586 assert( (PAGER_JOURNALMODE_WAL & 5)==5 )((void) (0));
64587
64588 assert( isOpen(pPager->fd) || pPager->exclusiveMode )((void) (0));
64589 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
64590 /* In this case we would like to delete the journal file. If it is
64591 ** not possible, then that is not a problem. Deleting the journal file
64592 ** here is an optimization only.
64593 **
64594 ** Before deleting the journal file, obtain a RESERVED lock on the
64595 ** database file. This ensures that the journal file is not deleted
64596 ** while it is in use by some other client.
64597 */
64598 sqlite3OsClose(pPager->jfd);
64599 if( pPager->eLock>=RESERVED_LOCK2 ){
64600 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
64601 }else{
64602 int rc = SQLITE_OK0;
64603 int state = pPager->eState;
64604 assert( state==PAGER_OPEN || state==PAGER_READER )((void) (0));
64605 if( state==PAGER_OPEN0 ){
64606 rc = sqlite3PagerSharedLock(pPager);
64607 }
64608 if( pPager->eState==PAGER_READER1 ){
64609 assert( rc==SQLITE_OK )((void) (0));
64610 rc = pagerLockDb(pPager, RESERVED_LOCK2);
64611 }
64612 if( rc==SQLITE_OK0 ){
64613 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
64614 }
64615 if( rc==SQLITE_OK0 && state==PAGER_READER1 ){
64616 pagerUnlockDb(pPager, SHARED_LOCK1);
64617 }else if( state==PAGER_OPEN0 ){
64618 pager_unlock(pPager);
64619 }
64620 assert( state==pPager->eState )((void) (0));
64621 }
64622 }else if( eMode==PAGER_JOURNALMODE_OFF2 || eMode==PAGER_JOURNALMODE_MEMORY4 ){
64623 sqlite3OsClose(pPager->jfd);
64624 }
64625 }
64626
64627 /* Return the new journal mode */
64628 return (int)pPager->journalMode;
64629}
64630
64631/*
64632** Return the current journal mode.
64633*/
64634SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager *pPager){
64635 return (int)pPager->journalMode;
64636}
64637
64638/*
64639** Return TRUE if the pager is in a state where it is OK to change the
64640** journalmode. Journalmode changes can only happen when the database
64641** is unmodified.
64642*/
64643SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
64644 assert( assert_pager_state(pPager) )((void) (0));
64645 if( pPager->eState>=PAGER_WRITER_CACHEMOD3 ) return 0;
64646 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0)(((pPager->jfd)->pMethods!=0) && pPager->journalOff
>0)
) return 0;
64647 return 1;
64648}
64649
64650/*
64651** Get/set the size-limit used for persistent journal files.
64652**
64653** Setting the size limit to -1 means no limit is enforced.
64654** An attempt to set a limit smaller than -1 is a no-op.
64655*/
64656SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
64657 if( iLimit>=-1 ){
64658 pPager->journalSizeLimit = iLimit;
64659 sqlite3WalLimit(pPager->pWal, iLimit);
64660 }
64661 return pPager->journalSizeLimit;
64662}
64663
64664/*
64665** Return a pointer to the pPager->pBackup variable. The backup module
64666** in backup.c maintains the content of this variable. This module
64667** uses it opaquely as an argument to sqlite3BackupRestart() and
64668** sqlite3BackupUpdate() only.
64669*/
64670SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
64671 return &pPager->pBackup;
64672}
64673
64674#ifndef SQLITE_OMIT_VACUUM
64675/*
64676** Unless this is an in-memory or temporary database, clear the pager cache.
64677*/
64678SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager *pPager){
64679 assert( MEMDB==0 || pPager->tempFile )((void) (0));
64680 if( pPager->tempFile==0 ) pager_reset(pPager);
64681}
64682#endif
64683
64684
64685#ifndef SQLITE_OMIT_WAL
64686/*
64687** This function is called when the user invokes "PRAGMA wal_checkpoint",
64688** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
64689** or wal_blocking_checkpoint() API functions.
64690**
64691** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
64692*/
64693SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(
64694 Pager *pPager, /* Checkpoint on this pager */
64695 sqlite3 *db, /* Db handle used to check for interrupts */
64696 int eMode, /* Type of checkpoint */
64697 int *pnLog, /* OUT: Final number of frames in log */
64698 int *pnCkpt /* OUT: Final number of checkpointed frames */
64699){
64700 int rc = SQLITE_OK0;
64701 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
64702 /* This only happens when a database file is zero bytes in size opened and
64703 ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint()
64704 ** is invoked without any intervening transactions. We need to start
64705 ** a transaction to initialize pWal. The PRAGMA table_list statement is
64706 ** used for this since it starts transactions on every database file,
64707 ** including all ATTACHed databases. This seems expensive for a single
64708 ** sqlite3_wal_checkpoint() call, but it happens very rarely.
64709 ** https://sqlite.org/forum/forumpost/fd0f19d229156939
64710 */
64711 sqlite3_exec(db, "PRAGMA table_list",0,0,0);
64712 }
64713 if( pPager->pWal ){
64714 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
64715 (eMode==SQLITE_CHECKPOINT_PASSIVE0 ? 0 : pPager->xBusyHandler),
64716 pPager->pBusyHandlerArg,
64717 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
64718 pnLog, pnCkpt
64719 );
64720 }
64721 return rc;
64722}
64723
64724SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager){
64725 return sqlite3WalCallback(pPager->pWal);
64726}
64727
64728/*
64729** Return true if the underlying VFS for the given pager supports the
64730** primitives necessary for write-ahead logging.
64731*/
64732SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager){
64733 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
64734 if( pPager->noLock ) return 0;
64735 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
64736}
64737
64738/*
64739** Attempt to take an exclusive lock on the database file. If a PENDING lock
64740** is obtained instead, immediately release it.
64741*/
64742static int pagerExclusiveLock(Pager *pPager){
64743 int rc; /* Return code */
64744 u8 eOrigLock; /* Original lock */
64745
64746 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
64747 eOrigLock = pPager->eLock;
64748 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
64749 if( rc!=SQLITE_OK0 ){
64750 /* If the attempt to grab the exclusive lock failed, release the
64751 ** pending lock that may have been obtained instead. */
64752 pagerUnlockDb(pPager, eOrigLock);
64753 }
64754
64755 return rc;
64756}
64757
64758/*
64759** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
64760** exclusive-locking mode when this function is called, take an EXCLUSIVE
64761** lock on the database file and use heap-memory to store the wal-index
64762** in. Otherwise, use the normal shared-memory.
64763*/
64764static int pagerOpenWal(Pager *pPager){
64765 int rc = SQLITE_OK0;
64766
64767 assert( pPager->pWal==0 && pPager->tempFile==0 )((void) (0));
64768 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
64769
64770 /* If the pager is already in exclusive-mode, the WAL module will use
64771 ** heap-memory for the wal-index instead of the VFS shared-memory
64772 ** implementation. Take the exclusive lock now, before opening the WAL
64773 ** file, to make sure this is safe.
64774 */
64775 if( pPager->exclusiveMode ){
64776 rc = pagerExclusiveLock(pPager);
64777 }
64778
64779 /* Open the connection to the log file. If this operation fails,
64780 ** (e.g. due to malloc() failure), return an error code.
64781 */
64782 if( rc==SQLITE_OK0 ){
64783 rc = sqlite3WalOpen(pPager->pVfs,
64784 pPager->fd, pPager->zWal, pPager->exclusiveMode,
64785 pPager->journalSizeLimit, &pPager->pWal
64786 );
64787 }
64788 pagerFixMaplimit(pPager);
64789
64790 return rc;
64791}
64792
64793
64794/*
64795** The caller must be holding a SHARED lock on the database file to call
64796** this function.
64797**
64798** If the pager passed as the first argument is open on a real database
64799** file (not a temp file or an in-memory database), and the WAL file
64800** is not already open, make an attempt to open it now. If successful,
64801** return SQLITE_OK. If an error occurs or the VFS used by the pager does
64802** not support the xShmXXX() methods, return an error code. *pbOpen is
64803** not modified in either case.
64804**
64805** If the pager is open on a temp-file (or in-memory database), or if
64806** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
64807** without doing anything.
64808*/
64809SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(
64810 Pager *pPager, /* Pager object */
64811 int *pbOpen /* OUT: Set to true if call is a no-op */
64812){
64813 int rc = SQLITE_OK0; /* Return code */
64814
64815 assert( assert_pager_state(pPager) )((void) (0));
64816 assert( pPager->eState==PAGER_OPEN || pbOpen )((void) (0));
64817 assert( pPager->eState==PAGER_READER || !pbOpen )((void) (0));
64818 assert( pbOpen==0 || *pbOpen==0 )((void) (0));
64819 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) )((void) (0));
64820
64821 if( !pPager->tempFile && !pPager->pWal ){
64822 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN14;
64823
64824 /* Close any rollback journal previously open */
64825 sqlite3OsClose(pPager->jfd);
64826
64827 rc = pagerOpenWal(pPager);
64828 if( rc==SQLITE_OK0 ){
64829 pPager->journalMode = PAGER_JOURNALMODE_WAL5;
64830 pPager->eState = PAGER_OPEN0;
64831 }
64832 }else{
64833 *pbOpen = 1;
64834 }
64835
64836 return rc;
64837}
64838
64839/*
64840** This function is called to close the connection to the log file prior
64841** to switching from WAL to rollback mode.
64842**
64843** Before closing the log file, this function attempts to take an
64844** EXCLUSIVE lock on the database file. If this cannot be obtained, an
64845** error (SQLITE_BUSY) is returned and the log connection is not closed.
64846** If successful, the EXCLUSIVE lock is not released before returning.
64847*/
64848SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
64849 int rc = SQLITE_OK0;
64850
64851 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL )((void) (0));
64852
64853 /* If the log file is not already open, but does exist in the file-system,
64854 ** it may need to be checkpointed before the connection can switch to
64855 ** rollback mode. Open it now so this can happen.
64856 */
64857 if( !pPager->pWal ){
64858 int logexists = 0;
64859 rc = pagerLockDb(pPager, SHARED_LOCK1);
64860 if( rc==SQLITE_OK0 ){
64861 rc = sqlite3OsAccess(
64862 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &logexists
64863 );
64864 }
64865 if( rc==SQLITE_OK0 && logexists ){
64866 rc = pagerOpenWal(pPager);
64867 }
64868 }
64869
64870 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
64871 ** the database file, the log and log-summary files will be deleted.
64872 */
64873 if( rc==SQLITE_OK0 && pPager->pWal ){
64874 rc = pagerExclusiveLock(pPager);
64875 if( rc==SQLITE_OK0 ){
64876 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
64877 pPager->pageSize, (u8*)pPager->pTmpSpace);
64878 pPager->pWal = 0;
64879 pagerFixMaplimit(pPager);
64880 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
64881 }
64882 }
64883 return rc;
64884}
64885
64886#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
64887/*
64888** If pager pPager is a wal-mode database not in exclusive locking mode,
64889** invoke the sqlite3WalWriteLock() function on the associated Wal object
64890** with the same db and bLock parameters as were passed to this function.
64891** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
64892*/
64893SQLITE_PRIVATEstatic int sqlite3PagerWalWriteLock(Pager *pPager, int bLock)0{
64894 int rc = SQLITE_OK0;
64895 if( pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->exclusiveMode==0 ){
64896 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
64897 }
64898 return rc;
64899}
64900
64901/*
64902** Set the database handle used by the wal layer to determine if
64903** blocking locks are required.
64904*/
64905SQLITE_PRIVATEstatic void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
64906 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
64907 sqlite3WalDb(pPager->pWal, db);
64908 }
64909}
64910#endif
64911
64912#ifdef SQLITE_ENABLE_SNAPSHOT1
64913/*
64914** If this is a WAL database, obtain a snapshot handle for the snapshot
64915** currently open. Otherwise, return an error.
64916*/
64917SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
64918 int rc = SQLITE_ERROR1;
64919 if( pPager->pWal ){
64920 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
64921 }
64922 return rc;
64923}
64924
64925/*
64926** If this is a WAL database, store a pointer to pSnapshot. Next time a
64927** read transaction is opened, attempt to read from the snapshot it
64928** identifies. If this is not a WAL database, return an error.
64929*/
64930SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(
64931 Pager *pPager,
64932 sqlite3_snapshot *pSnapshot
64933){
64934 int rc = SQLITE_OK0;
64935 if( pPager->pWal ){
64936 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
64937 }else{
64938 rc = SQLITE_ERROR1;
64939 }
64940 return rc;
64941}
64942
64943/*
64944** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
64945** is not a WAL database, return an error.
64946*/
64947SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager){
64948 int rc;
64949 if( pPager->pWal ){
64950 rc = sqlite3WalSnapshotRecover(pPager->pWal);
64951 }else{
64952 rc = SQLITE_ERROR1;
64953 }
64954 return rc;
64955}
64956
64957/*
64958** The caller currently has a read transaction open on the database.
64959** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
64960** this function takes a SHARED lock on the CHECKPOINTER slot and then
64961** checks if the snapshot passed as the second argument is still
64962** available. If so, SQLITE_OK is returned.
64963**
64964** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
64965** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
64966** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
64967** lock is released before returning.
64968*/
64969SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
64970 int rc;
64971 if( pPager->pWal ){
64972 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
64973 }else{
64974 rc = SQLITE_ERROR1;
64975 }
64976 return rc;
64977}
64978
64979/*
64980** Release a lock obtained by an earlier successful call to
64981** sqlite3PagerSnapshotCheck().
64982*/
64983SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager){
64984 assert( pPager->pWal )((void) (0));
64985 sqlite3WalSnapshotUnlock(pPager->pWal);
64986}
64987
64988#endif /* SQLITE_ENABLE_SNAPSHOT */
64989#endif /* !SQLITE_OMIT_WAL */
64990
64991#ifdef SQLITE_ENABLE_ZIPVFS
64992/*
64993** A read-lock must be held on the pager when this function is called. If
64994** the pager is in WAL mode and the WAL file currently contains one or more
64995** frames, return the size in bytes of the page images stored within the
64996** WAL frames. Otherwise, if this is not a WAL database or the WAL file
64997** is empty, return 0.
64998*/
64999SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager){
65000 assert( pPager->eState>=PAGER_READER )((void) (0));
65001 return sqlite3WalFramesize(pPager->pWal);
65002}
65003#endif
65004
65005#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
65006SQLITE_PRIVATEstatic int sqlite3PagerWalSystemErrno(Pager *pPager){
65007 return sqlite3WalSystemErrno(pPager->pWal);
65008}
65009#endif
65010
65011#endif /* SQLITE_OMIT_DISKIO */
65012
65013/************** End of pager.c ***********************************************/
65014/************** Begin file wal.c *********************************************/
65015/*
65016** 2010 February 1
65017**
65018** The author disclaims copyright to this source code. In place of
65019** a legal notice, here is a blessing:
65020**
65021** May you do good and not evil.
65022** May you find forgiveness for yourself and forgive others.
65023** May you share freely, never taking more than you give.
65024**
65025*************************************************************************
65026**
65027** This file contains the implementation of a write-ahead log (WAL) used in
65028** "journal_mode=WAL" mode.
65029**
65030** WRITE-AHEAD LOG (WAL) FILE FORMAT
65031**
65032** A WAL file consists of a header followed by zero or more "frames".
65033** Each frame records the revised content of a single page from the
65034** database file. All changes to the database are recorded by writing
65035** frames into the WAL. Transactions commit when a frame is written that
65036** contains a commit marker. A single WAL can and usually does record
65037** multiple transactions. Periodically, the content of the WAL is
65038** transferred back into the database file in an operation called a
65039** "checkpoint".
65040**
65041** A single WAL file can be used multiple times. In other words, the
65042** WAL can fill up with frames and then be checkpointed and then new
65043** frames can overwrite the old ones. A WAL always grows from beginning
65044** toward the end. Checksums and counters attached to each frame are
65045** used to determine which frames within the WAL are valid and which
65046** are leftovers from prior checkpoints.
65047**
65048** The WAL header is 32 bytes in size and consists of the following eight
65049** big-endian 32-bit unsigned integer values:
65050**
65051** 0: Magic number. 0x377f0682 or 0x377f0683
65052** 4: File format version. Currently 3007000
65053** 8: Database page size. Example: 1024
65054** 12: Checkpoint sequence number
65055** 16: Salt-1, random integer incremented with each checkpoint
65056** 20: Salt-2, a different random integer changing with each ckpt
65057** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
65058** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
65059**
65060** Immediately following the wal-header are zero or more frames. Each
65061** frame consists of a 24-byte frame-header followed by <page-size> bytes
65062** of page data. The frame-header is six big-endian 32-bit unsigned
65063** integer values, as follows:
65064**
65065** 0: Page number.
65066** 4: For commit records, the size of the database image in pages
65067** after the commit. For all other records, zero.
65068** 8: Salt-1 (copied from the header)
65069** 12: Salt-2 (copied from the header)
65070** 16: Checksum-1.
65071** 20: Checksum-2.
65072**
65073** A frame is considered valid if and only if the following conditions are
65074** true:
65075**
65076** (1) The salt-1 and salt-2 values in the frame-header match
65077** salt values in the wal-header
65078**
65079** (2) The checksum values in the final 8 bytes of the frame-header
65080** exactly match the checksum computed consecutively on the
65081** WAL header and the first 8 bytes and the content of all frames
65082** up to and including the current frame.
65083**
65084** The checksum is computed using 32-bit big-endian integers if the
65085** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
65086** is computed using little-endian if the magic number is 0x377f0682.
65087** The checksum values are always stored in the frame header in a
65088** big-endian format regardless of which byte order is used to compute
65089** the checksum. The checksum is computed by interpreting the input as
65090** an even number of unsigned 32-bit integers: x[0] through x[N]. The
65091** algorithm used for the checksum is as follows:
65092**
65093** for i from 0 to n-1 step 2:
65094** s0 += x[i] + s1;
65095** s1 += x[i+1] + s0;
65096** endfor
65097**
65098** Note that s0 and s1 are both weighted checksums using fibonacci weights
65099** in reverse order (the largest fibonacci weight occurs on the first element
65100** of the sequence being summed.) The s1 value spans all 32-bit
65101** terms of the sequence whereas s0 omits the final term.
65102**
65103** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
65104** WAL is transferred into the database, then the database is VFS.xSync-ed.
65105** The VFS.xSync operations serve as write barriers - all writes launched
65106** before the xSync must complete before any write that launches after the
65107** xSync begins.
65108**
65109** After each checkpoint, the salt-1 value is incremented and the salt-2
65110** value is randomized. This prevents old and new frames in the WAL from
65111** being considered valid at the same time and being checkpointing together
65112** following a crash.
65113**
65114** READER ALGORITHM
65115**
65116** To read a page from the database (call it page number P), a reader
65117** first checks the WAL to see if it contains page P. If so, then the
65118** last valid instance of page P that is a followed by a commit frame
65119** or is a commit frame itself becomes the value read. If the WAL
65120** contains no copies of page P that are valid and which are a commit
65121** frame or are followed by a commit frame, then page P is read from
65122** the database file.
65123**
65124** To start a read transaction, the reader records the index of the last
65125** valid frame in the WAL. The reader uses this recorded "mxFrame" value
65126** for all subsequent read operations. New transactions can be appended
65127** to the WAL, but as long as the reader uses its original mxFrame value
65128** and ignores the newly appended content, it will see a consistent snapshot
65129** of the database from a single point in time. This technique allows
65130** multiple concurrent readers to view different versions of the database
65131** content simultaneously.
65132**
65133** The reader algorithm in the previous paragraphs works correctly, but
65134** because frames for page P can appear anywhere within the WAL, the
65135** reader has to scan the entire WAL looking for page P frames. If the
65136** WAL is large (multiple megabytes is typical) that scan can be slow,
65137** and read performance suffers. To overcome this problem, a separate
65138** data structure called the wal-index is maintained to expedite the
65139** search for frames of a particular page.
65140**
65141** WAL-INDEX FORMAT
65142**
65143** Conceptually, the wal-index is shared memory, though VFS implementations
65144** might choose to implement the wal-index using a mmapped file. Because
65145** the wal-index is shared memory, SQLite does not support journal_mode=WAL
65146** on a network filesystem. All users of the database must be able to
65147** share memory.
65148**
65149** In the default unix and windows implementation, the wal-index is a mmapped
65150** file whose name is the database name with a "-shm" suffix added. For that
65151** reason, the wal-index is sometimes called the "shm" file.
65152**
65153** The wal-index is transient. After a crash, the wal-index can (and should
65154** be) reconstructed from the original WAL file. In fact, the VFS is required
65155** to either truncate or zero the header of the wal-index when the last
65156** connection to it closes. Because the wal-index is transient, it can
65157** use an architecture-specific format; it does not have to be cross-platform.
65158** Hence, unlike the database and WAL file formats which store all values
65159** as big endian, the wal-index can store multi-byte values in the native
65160** byte order of the host computer.
65161**
65162** The purpose of the wal-index is to answer this question quickly: Given
65163** a page number P and a maximum frame index M, return the index of the
65164** last frame in the wal before frame M for page P in the WAL, or return
65165** NULL if there are no frames for page P in the WAL prior to M.
65166**
65167** The wal-index consists of a header region, followed by an one or
65168** more index blocks.
65169**
65170** The wal-index header contains the total number of frames within the WAL
65171** in the mxFrame field.
65172**
65173** Each index block except for the first contains information on
65174** HASHTABLE_NPAGE frames. The first index block contains information on
65175** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
65176** HASHTABLE_NPAGE are selected so that together the wal-index header and
65177** first index block are the same size as all other index blocks in the
65178** wal-index. The values are:
65179**
65180** HASHTABLE_NPAGE 4096
65181** HASHTABLE_NPAGE_ONE 4062
65182**
65183** Each index block contains two sections, a page-mapping that contains the
65184** database page number associated with each wal frame, and a hash-table
65185** that allows readers to query an index block for a specific page number.
65186** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
65187** for the first index block) 32-bit page numbers. The first entry in the
65188** first index-block contains the database page number corresponding to the
65189** first frame in the WAL file. The first entry in the second index block
65190** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
65191** the log, and so on.
65192**
65193** The last index block in a wal-index usually contains less than the full
65194** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
65195** depending on the contents of the WAL file. This does not change the
65196** allocated size of the page-mapping array - the page-mapping array merely
65197** contains unused entries.
65198**
65199** Even without using the hash table, the last frame for page P
65200** can be found by scanning the page-mapping sections of each index block
65201** starting with the last index block and moving toward the first, and
65202** within each index block, starting at the end and moving toward the
65203** beginning. The first entry that equals P corresponds to the frame
65204** holding the content for that page.
65205**
65206** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
65207** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
65208** hash table for each page number in the mapping section, so the hash
65209** table is never more than half full. The expected number of collisions
65210** prior to finding a match is 1. Each entry of the hash table is an
65211** 1-based index of an entry in the mapping section of the same
65212** index block. Let K be the 1-based index of the largest entry in
65213** the mapping section. (For index blocks other than the last, K will
65214** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
65215** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
65216** contain a value of 0.
65217**
65218** To look for page P in the hash table, first compute a hash iKey on
65219** P as follows:
65220**
65221** iKey = (P * 383) % HASHTABLE_NSLOT
65222**
65223** Then start scanning entries of the hash table, starting with iKey
65224** (wrapping around to the beginning when the end of the hash table is
65225** reached) until an unused hash slot is found. Let the first unused slot
65226** be at index iUnused. (iUnused might be less than iKey if there was
65227** wrap-around.) Because the hash table is never more than half full,
65228** the search is guaranteed to eventually hit an unused entry. Let
65229** iMax be the value between iKey and iUnused, closest to iUnused,
65230** where aHash[iMax]==P. If there is no iMax entry (if there exists
65231** no hash slot such that aHash[i]==p) then page P is not in the
65232** current index block. Otherwise the iMax-th mapping entry of the
65233** current index block corresponds to the last entry that references
65234** page P.
65235**
65236** A hash search begins with the last index block and moves toward the
65237** first index block, looking for entries corresponding to page P. On
65238** average, only two or three slots in each index block need to be
65239** examined in order to either find the last entry for page P, or to
65240** establish that no such entry exists in the block. Each index block
65241** holds over 4000 entries. So two or three index blocks are sufficient
65242** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
65243** comparisons (on average) suffice to either locate a frame in the
65244** WAL or to establish that the frame does not exist in the WAL. This
65245** is much faster than scanning the entire 10MB WAL.
65246**
65247** Note that entries are added in order of increasing K. Hence, one
65248** reader might be using some value K0 and a second reader that started
65249** at a later time (after additional transactions were added to the WAL
65250** and to the wal-index) might be using a different value K1, where K1>K0.
65251** Both readers can use the same hash table and mapping section to get
65252** the correct result. There may be entries in the hash table with
65253** K>K0 but to the first reader, those entries will appear to be unused
65254** slots in the hash table and so the first reader will get an answer as
65255** if no values greater than K0 had ever been inserted into the hash table
65256** in the first place - which is what reader one wants. Meanwhile, the
65257** second reader using K1 will see additional values that were inserted
65258** later, which is exactly what reader two wants.
65259**
65260** When a rollback occurs, the value of K is decreased. Hash table entries
65261** that correspond to frames greater than the new K value are removed
65262** from the hash table at this point.
65263*/
65264#ifndef SQLITE_OMIT_WAL
65265
65266/* #include "wal.h" */
65267
65268/*
65269** Trace output macros
65270*/
65271#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
65272SQLITE_PRIVATEstatic int sqlite3WalTrace = 0;
65273# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
65274#else
65275# define WALTRACE(X)
65276#endif
65277
65278/*
65279** The maximum (and only) versions of the wal and wal-index formats
65280** that may be interpreted by this version of SQLite.
65281**
65282** If a client begins recovering a WAL file and finds that (a) the checksum
65283** values in the wal-header are correct and (b) the version field is not
65284** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
65285**
65286** Similarly, if a client successfully reads a wal-index header (i.e. the
65287** checksum test is successful) and finds that the version field is not
65288** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
65289** returns SQLITE_CANTOPEN.
65290*/
65291#define WAL_MAX_VERSION3007000 3007000
65292#define WALINDEX_MAX_VERSION3007000 3007000
65293
65294/*
65295** Index numbers for various locking bytes. WAL_NREADER is the number
65296** of available reader locks and should be at least 3. The default
65297** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
65298**
65299** Technically, the various VFSes are free to implement these locks however
65300** they see fit. However, compatibility is encouraged so that VFSes can
65301** interoperate. The standard implementation used on both unix and windows
65302** is for the index number to indicate a byte offset into the
65303** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
65304** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
65305** should be 120) is the location in the shm file for the first locking
65306** byte.
65307*/
65308#define WAL_WRITE_LOCK0 0
65309#define WAL_ALL_BUT_WRITE1 1
65310#define WAL_CKPT_LOCK1 1
65311#define WAL_RECOVER_LOCK2 2
65312#define WAL_READ_LOCK(I)(3+(I)) (3+(I))
65313#define WAL_NREADER(8 -3) (SQLITE_SHM_NLOCK8-3)
65314
65315
65316/* Object declarations */
65317typedef struct WalIndexHdr WalIndexHdr;
65318typedef struct WalIterator WalIterator;
65319typedef struct WalCkptInfo WalCkptInfo;
65320
65321
65322/*
65323** The following object holds a copy of the wal-index header content.
65324**
65325** The actual header in the wal-index consists of two copies of this
65326** object followed by one instance of the WalCkptInfo object.
65327** For all versions of SQLite through 3.10.0 and probably beyond,
65328** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
65329** the total header size is 136 bytes.
65330**
65331** The szPage value can be any power of 2 between 512 and 32768, inclusive.
65332** Or it can be 1 to represent a 65536-byte page. The latter case was
65333** added in 3.7.1 when support for 64K pages was added.
65334*/
65335struct WalIndexHdr {
65336 u32 iVersion; /* Wal-index version */
65337 u32 unused; /* Unused (padding) field */
65338 u32 iChange; /* Counter incremented each transaction */
65339 u8 isInit; /* 1 when initialized */
65340 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
65341 u16 szPage; /* Database page size in bytes. 1==64K */
65342 u32 mxFrame; /* Index of last valid frame in the WAL */
65343 u32 nPage; /* Size of database in pages */
65344 u32 aFrameCksum[2]; /* Checksum of last frame in log */
65345 u32 aSalt[2]; /* Two salt values copied from WAL header */
65346 u32 aCksum[2]; /* Checksum over all prior fields */
65347};
65348
65349/*
65350** A copy of the following object occurs in the wal-index immediately
65351** following the second copy of the WalIndexHdr. This object stores
65352** information used by checkpoint.
65353**
65354** nBackfill is the number of frames in the WAL that have been written
65355** back into the database. (We call the act of moving content from WAL to
65356** database "backfilling".) The nBackfill number is never greater than
65357** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
65358** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
65359** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
65360** mxFrame back to zero when the WAL is reset.
65361**
65362** nBackfillAttempted is the largest value of nBackfill that a checkpoint
65363** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
65364** the nBackfillAttempted is set before any backfilling is done and the
65365** nBackfill is only set after all backfilling completes. So if a checkpoint
65366** crashes, nBackfillAttempted might be larger than nBackfill. The
65367** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
65368**
65369** The aLock[] field is a set of bytes used for locking. These bytes should
65370** never be read or written.
65371**
65372** There is one entry in aReadMark[] for each reader lock. If a reader
65373** holds read-lock K, then the value in aReadMark[K] is no greater than
65374** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
65375** for any aReadMark[] means that entry is unused. aReadMark[0] is
65376** a special case; its value is never used and it exists as a place-holder
65377** to avoid having to offset aReadMark[] indexes by one. Readers holding
65378** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
65379** directly from the database.
65380**
65381** The value of aReadMark[K] may only be changed by a thread that
65382** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
65383** aReadMark[K] cannot changed while there is a reader is using that mark
65384** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
65385**
65386** The checkpointer may only transfer frames from WAL to database where
65387** the frame numbers are less than or equal to every aReadMark[] that is
65388** in use (that is, every aReadMark[j] for which there is a corresponding
65389** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
65390** largest value and will increase an unused aReadMark[] to mxFrame if there
65391** is not already an aReadMark[] equal to mxFrame. The exception to the
65392** previous sentence is when nBackfill equals mxFrame (meaning that everything
65393** in the WAL has been backfilled into the database) then new readers
65394** will choose aReadMark[0] which has value 0 and hence such reader will
65395** get all their all content directly from the database file and ignore
65396** the WAL.
65397**
65398** Writers normally append new frames to the end of the WAL. However,
65399** if nBackfill equals mxFrame (meaning that all WAL content has been
65400** written back into the database) and if no readers are using the WAL
65401** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
65402** the writer will first "reset" the WAL back to the beginning and start
65403** writing new content beginning at frame 1.
65404**
65405** We assume that 32-bit loads are atomic and so no locks are needed in
65406** order to read from any aReadMark[] entries.
65407*/
65408struct WalCkptInfo {
65409 u32 nBackfill; /* Number of WAL frames backfilled into DB */
65410 u32 aReadMark[WAL_NREADER(8 -3)]; /* Reader marks */
65411 u8 aLock[SQLITE_SHM_NLOCK8]; /* Reserved space for locks */
65412 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
65413 u32 notUsed0; /* Available for future enhancements */
65414};
65415#define READMARK_NOT_USED0xffffffff 0xffffffff
65416
65417/*
65418** This is a schematic view of the complete 136-byte header of the
65419** wal-index file (also known as the -shm file):
65420**
65421** +-----------------------------+
65422** 0: | iVersion | \
65423** +-----------------------------+ |
65424** 4: | (unused padding) | |
65425** +-----------------------------+ |
65426** 8: | iChange | |
65427** +-------+-------+-------------+ |
65428** 12: | bInit | bBig | szPage | |
65429** +-------+-------+-------------+ |
65430** 16: | mxFrame | | First copy of the
65431** +-----------------------------+ | WalIndexHdr object
65432** 20: | nPage | |
65433** +-----------------------------+ |
65434** 24: | aFrameCksum | |
65435** | | |
65436** +-----------------------------+ |
65437** 32: | aSalt | |
65438** | | |
65439** +-----------------------------+ |
65440** 40: | aCksum | |
65441** | | /
65442** +-----------------------------+
65443** 48: | iVersion | \
65444** +-----------------------------+ |
65445** 52: | (unused padding) | |
65446** +-----------------------------+ |
65447** 56: | iChange | |
65448** +-------+-------+-------------+ |
65449** 60: | bInit | bBig | szPage | |
65450** +-------+-------+-------------+ | Second copy of the
65451** 64: | mxFrame | | WalIndexHdr
65452** +-----------------------------+ |
65453** 68: | nPage | |
65454** +-----------------------------+ |
65455** 72: | aFrameCksum | |
65456** | | |
65457** +-----------------------------+ |
65458** 80: | aSalt | |
65459** | | |
65460** +-----------------------------+ |
65461** 88: | aCksum | |
65462** | | /
65463** +-----------------------------+
65464** 96: | nBackfill |
65465** +-----------------------------+
65466** 100: | 5 read marks |
65467** | |
65468** | |
65469** | |
65470** | |
65471** +-------+-------+------+------+
65472** 120: | Write | Ckpt | Rcvr | Rd0 | \
65473** +-------+-------+------+------+ ) 8 lock bytes
65474** | Read1 | Read2 | Rd3 | Rd4 | /
65475** +-------+-------+------+------+
65476** 128: | nBackfillAttempted |
65477** +-----------------------------+
65478** 132: | (unused padding) |
65479** +-----------------------------+
65480*/
65481
65482/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
65483** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
65484** only support mandatory file-locks, we do not read or write data
65485** from the region of the file on which locks are applied.
65486*/
65487#define WALINDEX_LOCK_OFFSET(sizeof(WalIndexHdr)*2+__builtin_offsetof(WalCkptInfo, aLock)
)
(sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)__builtin_offsetof(WalCkptInfo, aLock))
65488#define WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
65489
65490/* Size of header before each frame in wal */
65491#define WAL_FRAME_HDRSIZE24 24
65492
65493/* Size of write ahead log header, including checksum. */
65494#define WAL_HDRSIZE32 32
65495
65496/* WAL magic value. Either this value, or the same value with the least
65497** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
65498** big-endian format in the first 4 bytes of a WAL file.
65499**
65500** If the LSB is set, then the checksums for each frame within the WAL
65501** file are calculated by treating all data as an array of 32-bit
65502** big-endian words. Otherwise, they are calculated by interpreting
65503** all data as 32-bit little-endian words.
65504*/
65505#define WAL_MAGIC0x377f0682 0x377f0682
65506
65507/*
65508** Return the offset of frame iFrame in the write-ahead log file,
65509** assuming a database page size of szPage bytes. The offset returned
65510** is to the start of the write-ahead log frame-header.
65511*/
65512#define walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) ( \
65513 WAL_HDRSIZE32 + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE24) \
65514)
65515
65516/*
65517** An open write-ahead log file is represented by an instance of the
65518** following object.
65519*/
65520struct Wal {
65521 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
65522 sqlite3_file *pDbFd; /* File handle for the database file */
65523 sqlite3_file *pWalFd; /* File handle for WAL file */
65524 u32 iCallback; /* Value to pass to log callback (or 0) */
65525 i64 mxWalSize; /* Truncate WAL to this size upon reset */
65526 int nWiData; /* Size of array apWiData */
65527 int szFirstBlock; /* Size of first block written to WAL file */
65528 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
65529 u32 szPage; /* Database page size */
65530 i16 readLock; /* Which read lock is being held. -1 for none */
65531 u8 syncFlags; /* Flags to use to sync header writes */
65532 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
65533 u8 writeLock; /* True if in a write transaction */
65534 u8 ckptLock; /* True if holding a checkpoint lock */
65535 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
65536 u8 truncateOnCommit; /* True to truncate WAL file on commit */
65537 u8 syncHeader; /* Fsync the WAL header if true */
65538 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
65539 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
65540 WalIndexHdr hdr; /* Wal-index header for current transaction */
65541 u32 minFrame; /* Ignore wal frames before this one */
65542 u32 iReCksum; /* On commit, recalculate checksums from here */
65543 const char *zWalName; /* Name of WAL file */
65544 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
65545#ifdef SQLITE_USE_SEH
65546 u32 lockMask; /* Mask of locks held */
65547 void *pFree; /* Pointer to sqlite3_free() if exception thrown */
65548 u32 *pWiValue; /* Value to write into apWiData[iWiPg] */
65549 int iWiPg; /* Write pWiValue into apWiData[iWiPg] */
65550 int iSysErrno; /* System error code following exception */
65551#endif
65552#ifdef SQLITE_DEBUG
65553 int nSehTry; /* Number of nested SEH_TRY{} blocks */
65554 u8 lockError; /* True if a locking error has occurred */
65555#endif
65556#ifdef SQLITE_ENABLE_SNAPSHOT1
65557 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
65558 int bGetSnapshot; /* Transaction opened for sqlite3_get_snapshot() */
65559#endif
65560#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
65561 sqlite3 *db;
65562#endif
65563};
65564
65565/*
65566** Candidate values for Wal.exclusiveMode.
65567*/
65568#define WAL_NORMAL_MODE0 0
65569#define WAL_EXCLUSIVE_MODE1 1
65570#define WAL_HEAPMEMORY_MODE2 2
65571
65572/*
65573** Possible values for WAL.readOnly
65574*/
65575#define WAL_RDWR0 0 /* Normal read/write connection */
65576#define WAL_RDONLY1 1 /* The WAL file is readonly */
65577#define WAL_SHM_RDONLY2 2 /* The SHM file is readonly */
65578
65579/*
65580** Each page of the wal-index mapping contains a hash-table made up of
65581** an array of HASHTABLE_NSLOT elements of the following type.
65582*/
65583typedef u16 ht_slot;
65584
65585/*
65586** This structure is used to implement an iterator that loops through
65587** all frames in the WAL in database page order. Where two or more frames
65588** correspond to the same database page, the iterator visits only the
65589** frame most recently written to the WAL (in other words, the frame with
65590** the largest index).
65591**
65592** The internals of this structure are only accessed by:
65593**
65594** walIteratorInit() - Create a new iterator,
65595** walIteratorNext() - Step an iterator,
65596** walIteratorFree() - Free an iterator.
65597**
65598** This functionality is used by the checkpoint code (see walCheckpoint()).
65599*/
65600struct WalIterator {
65601 u32 iPrior; /* Last result returned from the iterator */
65602 int nSegment; /* Number of entries in aSegment[] */
65603 struct WalSegment {
65604 int iNext; /* Next slot in aIndex[] not yet returned */
65605 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
65606 u32 *aPgno; /* Array of page numbers. */
65607 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
65608 int iZero; /* Frame number associated with aPgno[0] */
65609 } aSegment[1]; /* One for every 32KB page in the wal-index */
65610};
65611
65612/*
65613** Define the parameters of the hash tables in the wal-index file. There
65614** is a hash-table following every HASHTABLE_NPAGE page numbers in the
65615** wal-index.
65616**
65617** Changing any of these constants will alter the wal-index format and
65618** create incompatibilities.
65619*/
65620#define HASHTABLE_NPAGE4096 4096 /* Must be power of 2 */
65621#define HASHTABLE_HASH_1383 383 /* Should be prime */
65622#define HASHTABLE_NSLOT(4096*2) (HASHTABLE_NPAGE4096*2) /* Must be a power of 2 */
65623
65624/*
65625** The block of page numbers associated with the first hash-table in a
65626** wal-index is smaller than usual. This is so that there is a complete
65627** hash-table on each aligned 32KB page of the wal-index.
65628*/
65629#define HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
(HASHTABLE_NPAGE4096 - (WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)))
65630
65631/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
65632#define WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ) ( \
65633 sizeof(ht_slot)*HASHTABLE_NSLOT(4096*2) + HASHTABLE_NPAGE4096*sizeof(u32) \
65634)
65635
65636/*
65637** Structured Exception Handling (SEH) is a Windows-specific technique
65638** for catching exceptions raised while accessing memory-mapped files.
65639**
65640** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
65641** deal with system-level errors that arise during WAL -shm file processing.
65642** Without this compile-time option, any system-level faults that appear
65643** while accessing the memory-mapped -shm file will cause a process-wide
65644** signal to be deliver, which will more than likely cause the entire
65645** process to exit.
65646*/
65647#ifdef SQLITE_USE_SEH
65648#include <Windows.h>
65649
65650/* Beginning of a block of code in which an exception might occur */
65651# define SEH_TRY; __try { \
65652 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 )((void) (0)); \
65653 VVA_ONLY(pWal->nSehTry++);
65654
65655/* The end of a block of code in which an exception might occur */
65656# define SEH_EXCEPT(X); ((void) (0)); \
65657 VVA_ONLY(pWal->nSehTry--); \
65658 assert( pWal->nSehTry==0 )((void) (0)); \
65659 } __except( sehExceptionFilter(pWal, GetExceptionCode(), GetExceptionInformation() ) ){ X }
65660
65661/* Simulate a memory-mapping fault in the -shm file for testing purposes */
65662# define SEH_INJECT_FAULT((void) (0)); sehInjectFault(pWal)
65663
65664/*
65665** The second argument is the return value of GetExceptionCode() for the
65666** current exception. Return EXCEPTION_EXECUTE_HANDLER if the exception code
65667** indicates that the exception may have been caused by accessing the *-shm
65668** file mapping. Or EXCEPTION_CONTINUE_SEARCH otherwise.
65669*/
65670static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){
65671 VVA_ONLY(pWal->nSehTry--);
65672 if( eCode==EXCEPTION_IN_PAGE_ERROR ){
65673 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
65674 /* From MSDN: For this type of exception, the first element of the
65675 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
65676 ** was thrown while reading, 1 if while writing. The second element is
65677 ** the virtual address being accessed. The "third array element specifies
65678 ** the underlying NTSTATUS code that resulted in the exception". */
65679 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
65680 }
65681 return EXCEPTION_EXECUTE_HANDLER;
65682 }
65683 return EXCEPTION_CONTINUE_SEARCH;
65684}
65685
65686/*
65687** If one is configured, invoke the xTestCallback callback with 650 as
65688** the argument. If it returns true, throw the same exception that is
65689** thrown by the system if the *-shm file mapping is accessed after it
65690** has been invalidated.
65691*/
65692static void sehInjectFault(Wal *pWal){
65693 int res;
65694 assert( pWal->nSehTry>0 )((void) (0));
65695
65696 res = sqlite3FaultSim(650);
65697 if( res!=0 ){
65698 ULONG_PTR aArg[3];
65699 aArg[0] = 0;
65700 aArg[1] = 0;
65701 aArg[2] = (ULONG_PTR)res;
65702 RaiseException(EXCEPTION_IN_PAGE_ERROR, 0, 3, (const ULONG_PTR*)aArg);
65703 }
65704}
65705
65706/*
65707** There are two ways to use this macro. To set a pointer to be freed
65708** if an exception is thrown:
65709**
65710** SEH_FREE_ON_ERROR(0, pPtr);
65711**
65712** and to cancel the same:
65713**
65714** SEH_FREE_ON_ERROR(pPtr, 0);
65715**
65716** In the first case, there must not already be a pointer registered to
65717** be freed. In the second case, pPtr must be the registered pointer.
65718*/
65719#define SEH_FREE_ON_ERROR(X,Y) \
65720 assert( (X==0 || Y==0) && pWal->pFree==X )((void) (0)); pWal->pFree = Y
65721
65722/*
65723** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
65724** to be set to pValue if an exception is thrown:
65725**
65726** SEH_SET_ON_ERROR(iPg, pValue);
65727**
65728** and to cancel the same:
65729**
65730** SEH_SET_ON_ERROR(0, 0);
65731*/
65732#define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
65733
65734#else
65735# define SEH_TRY; VVA_ONLY(pWal->nSehTry++);
65736# define SEH_EXCEPT(X); ((void) (0)); VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 )((void) (0));
65737# define SEH_INJECT_FAULT((void) (0)); assert( pWal->nSehTry>0 )((void) (0));
65738# define SEH_FREE_ON_ERROR(X,Y)
65739# define SEH_SET_ON_ERROR(X,Y)
65740#endif /* ifdef SQLITE_USE_SEH */
65741
65742
65743/*
65744** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
65745** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
65746** numbered from zero.
65747**
65748** If the wal-index is currently smaller the iPage pages then the size
65749** of the wal-index might be increased, but only if it is safe to do
65750** so. It is safe to enlarge the wal-index if pWal->writeLock is true
65751** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
65752**
65753** Three possible result scenarios:
65754**
65755** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
65756** (2) rc>=SQLITE_ERROR and *ppPage==NULL
65757** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0
65758**
65759** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
65760*/
65761static SQLITE_NOINLINE__attribute__((noinline)) int walIndexPageRealloc(
65762 Wal *pWal, /* The WAL context */
65763 int iPage, /* The page we seek */
65764 volatile u32 **ppPage /* Write the page pointer here */
65765){
65766 int rc = SQLITE_OK0;
65767
65768 /* Enlarge the pWal->apWiData[] array if required */
65769 if( pWal->nWiData<=iPage ){
65770 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
65771 volatile u32 **apNew;
65772 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
65773 if( !apNew ){
65774 *ppPage = 0;
65775 return SQLITE_NOMEM_BKPT7;
65776 }
65777 memset((void*)&apNew[pWal->nWiData], 0,
65778 sizeof(u32*)*(iPage+1-pWal->nWiData));
65779 pWal->apWiData = apNew;
65780 pWal->nWiData = iPage+1;
65781 }
65782
65783 /* Request a pointer to the required page from the VFS */
65784 assert( pWal->apWiData[iPage]==0 )((void) (0));
65785 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 ){
65786 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
65787 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT7;
65788 }else{
65789 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ),
65790 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
65791 );
65792 assert( pWal->apWiData[iPage]!=0((void) (0))
65793 || rc!=SQLITE_OK((void) (0))
65794 || (pWal->writeLock==0 && iPage==0) )((void) (0));
65795 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
65796 if( rc==SQLITE_OK0 ){
65797 if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM7;
65798 }else if( (rc&0xff)==SQLITE_READONLY8 ){
65799 pWal->readOnly |= WAL_SHM_RDONLY2;
65800 if( rc==SQLITE_READONLY8 ){
65801 rc = SQLITE_OK0;
65802 }
65803 }
65804 }
65805
65806 *ppPage = pWal->apWiData[iPage];
65807 assert( iPage==0 || *ppPage || rc!=SQLITE_OK )((void) (0));
65808 return rc;
65809}
65810static int walIndexPage(
65811 Wal *pWal, /* The WAL context */
65812 int iPage, /* The page we seek */
65813 volatile u32 **ppPage /* Write the page pointer here */
65814){
65815 SEH_INJECT_FAULT((void) (0));;
65816 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
65817 return walIndexPageRealloc(pWal, iPage, ppPage);
65818 }
65819 return SQLITE_OK0;
65820}
65821
65822/*
65823** Return a pointer to the WalCkptInfo structure in the wal-index.
65824*/
65825static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
65826 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
65827 SEH_INJECT_FAULT((void) (0));;
65828 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
65829}
65830
65831/*
65832** Return a pointer to the WalIndexHdr structure in the wal-index.
65833*/
65834static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
65835 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
65836 SEH_INJECT_FAULT((void) (0));;
65837 return (volatile WalIndexHdr*)pWal->apWiData[0];
65838}
65839
65840/*
65841** The argument to this macro must be of type u32. On a little-endian
65842** architecture, it returns the u32 value that results from interpreting
65843** the 4 bytes as a big-endian value. On a big-endian architecture, it
65844** returns the value that would be produced by interpreting the 4 bytes
65845** of the input value as a little-endian integer.
65846*/
65847#define BYTESWAP32(x)( (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<
8) + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>
24) )
( \
65848 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
65849 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
65850)
65851
65852/*
65853** Generate or extend an 8 byte checksum based on the data in
65854** array aByte[] and the initial values of aIn[0] and aIn[1] (or
65855** initial values of 0 and 0 if aIn==NULL).
65856**
65857** The checksum is written back into aOut[] before returning.
65858**
65859** nByte must be a positive multiple of 8.
65860*/
65861static void walChecksumBytes(
65862 int nativeCksum, /* True for native byte-order, false for non-native */
65863 u8 *a, /* Content to be checksummed */
65864 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
65865 const u32 *aIn, /* Initial checksum value input */
65866 u32 *aOut /* OUT: Final checksum value output */
65867){
65868 u32 s1, s2;
65869 u32 *aData = (u32 *)a;
65870 u32 *aEnd = (u32 *)&a[nByte];
65871
65872 if( aIn ){
65873 s1 = aIn[0];
65874 s2 = aIn[1];
65875 }else{
65876 s1 = s2 = 0;
65877 }
65878
65879 assert( nByte>=8 )((void) (0));
65880 assert( (nByte&0x00000007)==0 )((void) (0));
65881 assert( nByte<=65536 )((void) (0));
65882 assert( nByte%4==0 )((void) (0));
65883
65884 if( !nativeCksum ){
65885 do {
65886 s1 += BYTESWAP32(aData[0])( (((aData[0])&0x000000FF)<<24) + (((aData[0])&
0x0000FF00)<<8) + (((aData[0])&0x00FF0000)>>8
) + (((aData[0])&0xFF000000)>>24) )
+ s2;
65887 s2 += BYTESWAP32(aData[1])( (((aData[1])&0x000000FF)<<24) + (((aData[1])&
0x0000FF00)<<8) + (((aData[1])&0x00FF0000)>>8
) + (((aData[1])&0xFF000000)>>24) )
+ s1;
65888 aData += 2;
65889 }while( aData<aEnd );
65890 }else if( nByte%64==0 ){
65891 do {
65892 s1 += *aData++ + s2;
65893 s2 += *aData++ + s1;
65894 s1 += *aData++ + s2;
65895 s2 += *aData++ + s1;
65896 s1 += *aData++ + s2;
65897 s2 += *aData++ + s1;
65898 s1 += *aData++ + s2;
65899 s2 += *aData++ + s1;
65900 s1 += *aData++ + s2;
65901 s2 += *aData++ + s1;
65902 s1 += *aData++ + s2;
65903 s2 += *aData++ + s1;
65904 s1 += *aData++ + s2;
65905 s2 += *aData++ + s1;
65906 s1 += *aData++ + s2;
65907 s2 += *aData++ + s1;
65908 }while( aData<aEnd );
65909 }else{
65910 do {
65911 s1 += *aData++ + s2;
65912 s2 += *aData++ + s1;
65913 }while( aData<aEnd );
65914 }
65915 assert( aData==aEnd )((void) (0));
65916
65917 aOut[0] = s1;
65918 aOut[1] = s2;
65919}
65920
65921/*
65922** If there is the possibility of concurrent access to the SHM file
65923** from multiple threads and/or processes, then do a memory barrier.
65924*/
65925static void walShmBarrier(Wal *pWal){
65926 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
65927 sqlite3OsShmBarrier(pWal->pDbFd);
65928 }
65929}
65930
65931/*
65932** Add the SQLITE_NO_TSAN as part of the return-type of a function
65933** definition as a hint that the function contains constructs that
65934** might give false-positive TSAN warnings.
65935**
65936** See tag-20200519-1.
65937*/
65938#if defined(__clang__1) && !defined(SQLITE_NO_TSAN__attribute__((no_sanitize_thread)))
65939# define SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) __attribute__((no_sanitize_thread))
65940#else
65941# define SQLITE_NO_TSAN__attribute__((no_sanitize_thread))
65942#endif
65943
65944/*
65945** Write the header information in pWal->hdr into the wal-index.
65946**
65947** The checksum on pWal->hdr is updated before it is written.
65948*/
65949static SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) void walIndexWriteHdr(Wal *pWal){
65950 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
65951 const int nCksum = offsetof(WalIndexHdr, aCksum)__builtin_offsetof(WalIndexHdr, aCksum);
65952
65953 assert( pWal->writeLock )((void) (0));
65954 pWal->hdr.isInit = 1;
65955 pWal->hdr.iVersion = WALINDEX_MAX_VERSION3007000;
65956 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
65957 /* Possible TSAN false-positive. See tag-20200519-1 */
65958 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65959 walShmBarrier(pWal);
65960 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65961}
65962
65963/*
65964** This function encodes a single frame header and writes it to a buffer
65965** supplied by the caller. A frame-header is made up of a series of
65966** 4-byte big-endian integers, as follows:
65967**
65968** 0: Page number.
65969** 4: For commit records, the size of the database image in pages
65970** after the commit. For all other records, zero.
65971** 8: Salt-1 (copied from the wal-header)
65972** 12: Salt-2 (copied from the wal-header)
65973** 16: Checksum-1.
65974** 20: Checksum-2.
65975*/
65976static void walEncodeFrame(
65977 Wal *pWal, /* The write-ahead log */
65978 u32 iPage, /* Database page number for frame */
65979 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
65980 u8 *aData, /* Pointer to page data */
65981 u8 *aFrame /* OUT: Write encoded frame here */
65982){
65983 int nativeCksum; /* True for native byte-order checksums */
65984 u32 *aCksum = pWal->hdr.aFrameCksum;
65985 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
65986 sqlite3Put4byte(&aFrame[0], iPage);
65987 sqlite3Put4byte(&aFrame[4], nTruncate);
65988 if( pWal->iReCksum==0 ){
65989 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
65990
65991 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
65992 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
65993 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65994
65995 sqlite3Put4byte(&aFrame[16], aCksum[0]);
65996 sqlite3Put4byte(&aFrame[20], aCksum[1]);
65997 }else{
65998 memset(&aFrame[8], 0, 16);
65999 }
66000}
66001
66002/*
66003** Check to see if the frame with header in aFrame[] and content
66004** in aData[] is valid. If it is a valid frame, fill *piPage and
66005** *pnTruncate and return true. Return if the frame is not valid.
66006*/
66007static int walDecodeFrame(
66008 Wal *pWal, /* The write-ahead log */
66009 u32 *piPage, /* OUT: Database page number for frame */
66010 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
66011 u8 *aData, /* Pointer to page data (for checksum) */
66012 u8 *aFrame /* Frame data */
66013){
66014 int nativeCksum; /* True for native byte-order checksums */
66015 u32 *aCksum = pWal->hdr.aFrameCksum;
66016 u32 pgno; /* Page number of the frame */
66017 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
66018
66019 /* A frame is only valid if the salt values in the frame-header
66020 ** match the salt values in the wal-header.
66021 */
66022 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
66023 return 0;
66024 }
66025
66026 /* A frame is only valid if the page number is greater than zero.
66027 */
66028 pgno = sqlite3Get4byte(&aFrame[0]);
66029 if( pgno==0 ){
66030 return 0;
66031 }
66032
66033 /* A frame is only valid if a checksum of the WAL header,
66034 ** all prior frames, the first 16 bytes of this frame-header,
66035 ** and the frame-data matches the checksum in the last 8
66036 ** bytes of this frame-header.
66037 */
66038 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
66039 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
66040 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
66041 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
66042 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
66043 ){
66044 /* Checksum failed. */
66045 return 0;
66046 }
66047
66048 /* If we reach this point, the frame is valid. Return the page number
66049 ** and the new database size.
66050 */
66051 *piPage = pgno;
66052 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
66053 return 1;
66054}
66055
66056
66057#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
66058/*
66059** Names of locks. This routine is used to provide debugging output and is not
66060** a part of an ordinary build.
66061*/
66062static const char *walLockName(int lockIdx){
66063 if( lockIdx==WAL_WRITE_LOCK0 ){
66064 return "WRITE-LOCK";
66065 }else if( lockIdx==WAL_CKPT_LOCK1 ){
66066 return "CKPT-LOCK";
66067 }else if( lockIdx==WAL_RECOVER_LOCK2 ){
66068 return "RECOVER-LOCK";
66069 }else{
66070 static char zName[15];
66071 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
66072 lockIdx-WAL_READ_LOCK(0)(3+(0)));
66073 return zName;
66074 }
66075}
66076#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
66077
66078
66079/*
66080** Set or release locks on the WAL. Locks are either shared or exclusive.
66081** A lock cannot be moved directly between shared and exclusive - it must go
66082** through the unlocked state first.
66083**
66084** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
66085*/
66086static int walLockShared(Wal *pWal, int lockIdx){
66087 int rc;
66088 if( pWal->exclusiveMode ) return SQLITE_OK0;
66089 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66090 SQLITE_SHM_LOCK2 | SQLITE_SHM_SHARED4);
66091 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
66092 walLockName(lockIdx), rc ? "failed" : "ok"));
66093 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66094#ifdef SQLITE_USE_SEH
66095 if( rc==SQLITE_OK0 ) pWal->lockMask |= (1 << lockIdx);
66096#endif
66097 return rc;
66098}
66099static void walUnlockShared(Wal *pWal, int lockIdx){
66100 if( pWal->exclusiveMode ) return;
66101 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66102 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_SHARED4);
66103#ifdef SQLITE_USE_SEH
66104 pWal->lockMask &= ~(1 << lockIdx);
66105#endif
66106 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
66107}
66108static int walLockExclusive(Wal *pWal, int lockIdx, int n){
66109 int rc;
66110 if( pWal->exclusiveMode ) return SQLITE_OK0;
66111 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66112 SQLITE_SHM_LOCK2 | SQLITE_SHM_EXCLUSIVE8);
66113 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
66114 walLockName(lockIdx), n, rc ? "failed" : "ok"));
66115 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66116#ifdef SQLITE_USE_SEH
66117 if( rc==SQLITE_OK0 ){
66118 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK8+lockIdx));
66119 }
66120#endif
66121 return rc;
66122}
66123static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
66124 if( pWal->exclusiveMode ) return;
66125 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66126 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_EXCLUSIVE8);
66127#ifdef SQLITE_USE_SEH
66128 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK8+lockIdx));
66129#endif
66130 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
66131 walLockName(lockIdx), n));
66132}
66133
66134/*
66135** Compute a hash on a page number. The resulting hash value must land
66136** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
66137** the hash to the next value in the event of a collision.
66138*/
66139static int walHash(u32 iPage){
66140 assert( iPage>0 )((void) (0));
66141 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 )((void) (0));
66142 return (iPage*HASHTABLE_HASH_1383) & (HASHTABLE_NSLOT(4096*2)-1);
66143}
66144static int walNextHash(int iPriorHash){
66145 return (iPriorHash+1)&(HASHTABLE_NSLOT(4096*2)-1);
66146}
66147
66148/*
66149** An instance of the WalHashLoc object is used to describe the location
66150** of a page hash table in the wal-index. This becomes the return value
66151** from walHashGet().
66152*/
66153typedef struct WalHashLoc WalHashLoc;
66154struct WalHashLoc {
66155 volatile ht_slot *aHash; /* Start of the wal-index hash table */
66156 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
66157 u32 iZero; /* One less than the frame number of first indexed*/
66158};
66159
66160/*
66161** Return pointers to the hash table and page number array stored on
66162** page iHash of the wal-index. The wal-index is broken into 32KB pages
66163** numbered starting from 0.
66164**
66165** Set output variable pLoc->aHash to point to the start of the hash table
66166** in the wal-index file. Set pLoc->iZero to one less than the frame
66167** number of the first frame indexed by this hash table. If a
66168** slot in the hash table is set to N, it refers to frame number
66169** (pLoc->iZero+N) in the log.
66170**
66171** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
66172** first frame indexed by the hash table, frame (pLoc->iZero).
66173*/
66174static int walHashGet(
66175 Wal *pWal, /* WAL handle */
66176 int iHash, /* Find the iHash'th table */
66177 WalHashLoc *pLoc /* OUT: Hash table location */
66178){
66179 int rc; /* Return code */
66180
66181 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
66182 assert( rc==SQLITE_OK || iHash>0 )((void) (0));
66183
66184 if( pLoc->aPgno ){
66185 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE4096];
66186 if( iHash==0 ){
66187 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)];
66188 pLoc->iZero = 0;
66189 }else{
66190 pLoc->iZero = HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+ (iHash-1)*HASHTABLE_NPAGE4096;
66191 }
66192 }else if( NEVER(rc==SQLITE_OK)(rc==0) ){
66193 rc = SQLITE_ERROR1;
66194 }
66195 return rc;
66196}
66197
66198/*
66199** Return the number of the wal-index page that contains the hash-table
66200** and page-number array that contain entries corresponding to WAL frame
66201** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
66202** are numbered starting from 0.
66203*/
66204static int walFramePage(u32 iFrame){
66205 int iHash = (iFrame+HASHTABLE_NPAGE4096-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
-1) / HASHTABLE_NPAGE4096;
66206 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)((void) (0))
66207 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)((void) (0))
66208 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))((void) (0))
66209 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)((void) (0))
66210 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))((void) (0))
66211 )((void) (0));
66212 assert( iHash>=0 )((void) (0));
66213 return iHash;
66214}
66215
66216/*
66217** Return the page number associated with frame iFrame in this WAL.
66218*/
66219static u32 walFramePgno(Wal *pWal, u32 iFrame){
66220 int iHash = walFramePage(iFrame);
66221 SEH_INJECT_FAULT((void) (0));;
66222 if( iHash==0 ){
66223 return pWal->apWiData[0][WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32) + iFrame - 1];
66224 }
66225 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
)%HASHTABLE_NPAGE4096];
66226}
66227
66228/*
66229** Remove entries from the hash table that point to WAL slots greater
66230** than pWal->hdr.mxFrame.
66231**
66232** This function is called whenever pWal->hdr.mxFrame is decreased due
66233** to a rollback or savepoint.
66234**
66235** At most only the hash table containing pWal->hdr.mxFrame needs to be
66236** updated. Any later hash tables will be automatically cleared when
66237** pWal->hdr.mxFrame advances to the point where those hash tables are
66238** actually needed.
66239*/
66240static void walCleanupHash(Wal *pWal){
66241 WalHashLoc sLoc; /* Hash table location */
66242 int iLimit = 0; /* Zero values greater than this */
66243 int nByte; /* Number of bytes to zero in aPgno[] */
66244 int i; /* Used to iterate through aHash[] */
66245
66246 assert( pWal->writeLock )((void) (0));
66247 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
66248 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
66249 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
66250
66251 if( pWal->hdr.mxFrame==0 ) return;
66252
66253 /* Obtain pointers to the hash-table and page-number array containing
66254 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
66255 ** that the page said hash-table and array reside on is already mapped.(1)
66256 */
66257 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) )((void) (0));
66258 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] )((void) (0));
66259 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
66260 if( NEVER(i)(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
66261
66262 /* Zero all hash-table entries that correspond to frame numbers greater
66263 ** than pWal->hdr.mxFrame.
66264 */
66265 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
66266 assert( iLimit>0 )((void) (0));
66267 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){
66268 if( sLoc.aHash[i]>iLimit ){
66269 sLoc.aHash[i] = 0;
66270 }
66271 }
66272
66273 /* Zero the entries in the aPgno array that correspond to frames with
66274 ** frame numbers greater than pWal->hdr.mxFrame.
66275 */
66276 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
66277 assert( nByte>=0 )((void) (0));
66278 memset((void *)&sLoc.aPgno[iLimit], 0, nByte);
66279
66280#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
66281 /* Verify that the every entry in the mapping region is still reachable
66282 ** via the hash table even after the cleanup.
66283 */
66284 if( iLimit ){
66285 int j; /* Loop counter */
66286 int iKey; /* Hash key */
66287 for(j=0; j<iLimit; j++){
66288 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
66289 if( sLoc.aHash[iKey]==j+1 ) break;
66290 }
66291 assert( sLoc.aHash[iKey]==j+1 )((void) (0));
66292 }
66293 }
66294#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
66295}
66296
66297
66298/*
66299** Set an entry in the wal-index that will map database page number
66300** pPage into WAL frame iFrame.
66301*/
66302static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
66303 int rc; /* Return code */
66304 WalHashLoc sLoc; /* Wal-index hash table location */
66305
66306 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
66307
66308 /* Assuming the wal-index file was successfully mapped, populate the
66309 ** page number array and hash table entry.
66310 */
66311 if( rc==SQLITE_OK0 ){
66312 int iKey; /* Hash table key */
66313 int idx; /* Value to write to hash-table slot */
66314 int nCollide; /* Number of hash collisions */
66315
66316 idx = iFrame - sLoc.iZero;
66317 assert( idx <= HASHTABLE_NSLOT/2 + 1 )((void) (0));
66318
66319 /* If this is the first entry to be added to this hash-table, zero the
66320 ** entire hash table and aPgno[] array before proceeding.
66321 */
66322 if( idx==1 ){
66323 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT(4096*2)] - (u8*)sLoc.aPgno);
66324 assert( nByte>=0 )((void) (0));
66325 memset((void*)sLoc.aPgno, 0, nByte);
66326 }
66327
66328 /* If the entry in aPgno[] is already set, then the previous writer
66329 ** must have exited unexpectedly in the middle of a transaction (after
66330 ** writing one or more dirty pages to the WAL to free up memory).
66331 ** Remove the remnants of that writers uncommitted transaction from
66332 ** the hash-table before writing any new entries.
66333 */
66334 if( sLoc.aPgno[idx-1] ){
66335 walCleanupHash(pWal);
66336 assert( !sLoc.aPgno[idx-1] )((void) (0));
66337 }
66338
66339 /* Write the aPgno[] array entry and the hash-table slot. */
66340 nCollide = idx;
66341 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
66342 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(66342);
66343 }
66344 sLoc.aPgno[idx-1] = iPage;
66345 AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx)__atomic_store_n((&sLoc.aHash[iKey]),((ht_slot)idx),0);
66346
66347#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
66348 /* Verify that the number of entries in the hash table exactly equals
66349 ** the number of entries in the mapping region.
66350 */
66351 {
66352 int i; /* Loop counter */
66353 int nEntry = 0; /* Number of entries in the hash table */
66354 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){ if( sLoc.aHash[i] ) nEntry++; }
66355 assert( nEntry==idx )((void) (0));
66356 }
66357
66358 /* Verify that the every entry in the mapping region is reachable
66359 ** via the hash table. This turns out to be a really, really expensive
66360 ** thing to check, so only do this occasionally - not on every
66361 ** iteration.
66362 */
66363 if( (idx&0x3ff)==0 ){
66364 int i; /* Loop counter */
66365 for(i=0; i<idx; i++){
66366 for(iKey=walHash(sLoc.aPgno[i]);
66367 sLoc.aHash[iKey];
66368 iKey=walNextHash(iKey)){
66369 if( sLoc.aHash[iKey]==i+1 ) break;
66370 }
66371 assert( sLoc.aHash[iKey]==i+1 )((void) (0));
66372 }
66373 }
66374#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
66375 }
66376
66377 return rc;
66378}
66379
66380
66381/*
66382** Recover the wal-index by reading the write-ahead log file.
66383**
66384** This routine first tries to establish an exclusive lock on the
66385** wal-index to prevent other threads/processes from doing anything
66386** with the WAL or wal-index while recovery is running. The
66387** WAL_RECOVER_LOCK is also held so that other threads will know
66388** that this thread is running recovery. If unable to establish
66389** the necessary locks, this routine returns SQLITE_BUSY.
66390*/
66391static int walIndexRecover(Wal *pWal){
66392 int rc; /* Return Code */
66393 i64 nSize; /* Size of log file */
66394 u32 aFrameCksum[2] = {0, 0};
66395 int iLock; /* Lock offset to lock for checkpoint */
66396
66397 /* Obtain an exclusive lock on all byte in the locking range not already
66398 ** locked by the caller. The caller is guaranteed to have locked the
66399 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
66400 ** If successful, the same bytes that are locked here are unlocked before
66401 ** this function returns.
66402 */
66403 assert( pWal->ckptLock==1 || pWal->ckptLock==0 )((void) (0));
66404 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 )((void) (0));
66405 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE )((void) (0));
66406 assert( pWal->writeLock )((void) (0));
66407 iLock = WAL_ALL_BUT_WRITE1 + pWal->ckptLock;
66408 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
66409 if( rc ){
66410 return rc;
66411 }
66412
66413 WALTRACE(("WAL%p: recovery begin...\n", pWal));
66414
66415 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
66416
66417 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
66418 if( rc!=SQLITE_OK0 ){
66419 goto recovery_error;
66420 }
66421
66422 if( nSize>WAL_HDRSIZE32 ){
66423 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
66424 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
66425 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
66426 int szFrame; /* Number of bytes in buffer aFrame[] */
66427 u8 *aData; /* Pointer to data part of aFrame buffer */
66428 int szPage; /* Page size according to the log */
66429 u32 magic; /* Magic value read from WAL header */
66430 u32 version; /* Magic value read from WAL header */
66431 int isValid; /* True if this frame is valid */
66432 u32 iPg; /* Current 32KB wal-index page */
66433 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
66434
66435 /* Read in the WAL header. */
66436 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
66437 if( rc!=SQLITE_OK0 ){
66438 goto recovery_error;
66439 }
66440
66441 /* If the database page size is not a power of two, or is greater than
66442 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
66443 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
66444 ** WAL file.
66445 */
66446 magic = sqlite3Get4byte(&aBuf[0]);
66447 szPage = sqlite3Get4byte(&aBuf[8]);
66448 if( (magic&0xFFFFFFFE)!=WAL_MAGIC0x377f0682
66449 || szPage&(szPage-1)
66450 || szPage>SQLITE_MAX_PAGE_SIZE65536
66451 || szPage<512
66452 ){
66453 goto finished;
66454 }
66455 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
66456 pWal->szPage = szPage;
66457 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
66458 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
66459
66460 /* Verify that the WAL header checksum is correct */
66461 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0,
66462 aBuf, WAL_HDRSIZE32-2*4, 0, pWal->hdr.aFrameCksum
66463 );
66464 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
66465 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
66466 ){
66467 goto finished;
66468 }
66469
66470 /* Verify that the version number on the WAL format is one that
66471 ** are able to understand */
66472 version = sqlite3Get4byte(&aBuf[4]);
66473 if( version!=WAL_MAX_VERSION3007000 ){
66474 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(66474);
66475 goto finished;
66476 }
66477
66478 /* Malloc a buffer to read frames into. */
66479 szFrame = szPage + WAL_FRAME_HDRSIZE24;
66480 aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
66481 SEH_FREE_ON_ERROR(0, aFrame);
66482 if( !aFrame ){
66483 rc = SQLITE_NOMEM_BKPT7;
66484 goto recovery_error;
66485 }
66486 aData = &aFrame[WAL_FRAME_HDRSIZE24];
66487 aPrivate = (u32*)&aData[szPage];
66488
66489 /* Read all frames from the log file. */
66490 iLastFrame = (nSize - WAL_HDRSIZE32) / szFrame;
66491 for(iPg=0; iPg<=(u32)walFramePage(iLastFrame); iPg++){
66492 u32 *aShare;
66493 u32 iFrame; /* Index of last frame read */
66494 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))
;
66495 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+(iPg-1)*HASHTABLE_NPAGE4096);
66496 u32 nHdr, nHdr32;
66497 rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare);
66498 assert( aShare!=0 || rc!=SQLITE_OK )((void) (0));
66499 if( aShare==0 ) break;
66500 SEH_SET_ON_ERROR(iPg, aShare);
66501 pWal->apWiData[iPg] = aPrivate;
66502
66503 for(iFrame=iFirst; iFrame<=iLast; iFrame++){
66504 i64 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) );
66505 u32 pgno; /* Database page number for frame */
66506 u32 nTruncate; /* dbsize field from frame header */
66507
66508 /* Read and decode the next log frame. */
66509 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
66510 if( rc!=SQLITE_OK0 ) break;
66511 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
66512 if( !isValid ) break;
66513 rc = walIndexAppend(pWal, iFrame, pgno);
66514 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) break;
66515
66516 /* If nTruncate is non-zero, this is a commit record. */
66517 if( nTruncate ){
66518 pWal->hdr.mxFrame = iFrame;
66519 pWal->hdr.nPage = nTruncate;
66520 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
66521 testcase( szPage<=32768 );
66522 testcase( szPage>=65536 );
66523 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
66524 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
66525 }
66526 }
66527 pWal->apWiData[iPg] = aShare;
66528 SEH_SET_ON_ERROR(0,0);
66529 nHdr = (iPg==0 ? WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) : 0);
66530 nHdr32 = nHdr / sizeof(u32);
66531#ifndef SQLITE_SAFER_WALINDEX_RECOVERY
66532 /* Memcpy() should work fine here, on all reasonable implementations.
66533 ** Technically, memcpy() might change the destination to some
66534 ** intermediate value before setting to the final value, and that might
66535 ** cause a concurrent reader to malfunction. Memcpy() is allowed to
66536 ** do that, according to the spec, but no memcpy() implementation that
66537 ** we know of actually does that, which is why we say that memcpy()
66538 ** is safe for this. Memcpy() is certainly a lot faster.
66539 */
66540 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) )-nHdr);
66541#else
66542 /* In the event that some platform is found for which memcpy()
66543 ** changes the destination to some intermediate value before
66544 ** setting the final value, this alternative copy routine is
66545 ** provided.
66546 */
66547 {
66548 int i;
66549 for(i=nHdr32; i<WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) )/sizeof(u32); i++){
66550 if( aShare[i]!=aPrivate[i] ){
66551 /* Atomic memory operations are not required here because if
66552 ** the value needs to be changed, that means it is not being
66553 ** accessed concurrently. */
66554 aShare[i] = aPrivate[i];
66555 }
66556 }
66557 }
66558#endif
66559 SEH_INJECT_FAULT((void) (0));;
66560 if( iFrame<=iLast ) break;
66561 }
66562
66563 SEH_FREE_ON_ERROR(aFrame, 0);
66564 sqlite3_free(aFrame);
66565 }
66566
66567finished:
66568 if( rc==SQLITE_OK0 ){
66569 volatile WalCkptInfo *pInfo;
66570 int i;
66571 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
66572 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
66573 walIndexWriteHdr(pWal);
66574
66575 /* Reset the checkpoint-header. This is safe because this thread is
66576 ** currently holding locks that exclude all other writers and
66577 ** checkpointers. Then set the values of read-mark slots 1 through N.
66578 */
66579 pInfo = walCkptInfo(pWal);
66580 pInfo->nBackfill = 0;
66581 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
66582 pInfo->aReadMark[0] = 0;
66583 for(i=1; i<WAL_NREADER(8 -3); i++){
66584 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
66585 if( rc==SQLITE_OK0 ){
66586 if( i==1 && pWal->hdr.mxFrame ){
66587 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
66588 }else{
66589 pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
66590 }
66591 SEH_INJECT_FAULT((void) (0));;
66592 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
66593 }else if( rc!=SQLITE_BUSY5 ){
66594 goto recovery_error;
66595 }
66596 }
66597
66598 /* If more than one frame was recovered from the log file, report an
66599 ** event via sqlite3_log(). This is to help with identifying performance
66600 ** problems caused by applications routinely shutting down without
66601 ** checkpointing the log file.
66602 */
66603 if( pWal->hdr.nPage ){
66604 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)),
66605 "recovered %d frames from WAL file %s",
66606 pWal->hdr.mxFrame, pWal->zWalName
66607 );
66608 }
66609 }
66610
66611recovery_error:
66612 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
66613 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
66614 return rc;
66615}
66616
66617/*
66618** Close an open wal-index.
66619*/
66620static void walIndexClose(Wal *pWal, int isDelete){
66621 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 || pWal->bShmUnreliable ){
66622 int i;
66623 for(i=0; i<pWal->nWiData; i++){
66624 sqlite3_free((void *)pWal->apWiData[i]);
66625 pWal->apWiData[i] = 0;
66626 }
66627 }
66628 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
66629 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
66630 }
66631}
66632
66633/*
66634** Open a connection to the WAL file zWalName. The database file must
66635** already be opened on connection pDbFd. The buffer that zWalName points
66636** to must remain valid for the lifetime of the returned Wal* handle.
66637**
66638** A SHARED lock should be held on the database file when this function
66639** is called. The purpose of this SHARED lock is to prevent any other
66640** client from unlinking the WAL or wal-index file. If another process
66641** were to do this just after this client opened one of these files, the
66642** system would be badly broken.
66643**
66644** If the log file is successfully opened, SQLITE_OK is returned and
66645** *ppWal is set to point to a new WAL handle. If an error occurs,
66646** an SQLite error code is returned and *ppWal is left unmodified.
66647*/
66648SQLITE_PRIVATEstatic int sqlite3WalOpen(
66649 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
66650 sqlite3_file *pDbFd, /* The open database file */
66651 const char *zWalName, /* Name of the WAL file */
66652 int bNoShm, /* True to run in heap-memory mode */
66653 i64 mxWalSize, /* Truncate WAL to this size on reset */
66654 Wal **ppWal /* OUT: Allocated Wal handle */
66655){
66656 int rc; /* Return Code */
66657 Wal *pRet; /* Object to allocate and return */
66658 int flags; /* Flags passed to OsOpen() */
66659
66660 assert( zWalName && zWalName[0] )((void) (0));
66661 assert( pDbFd )((void) (0));
66662
66663 /* Verify the values of various constants. Any changes to the values
66664 ** of these constants would result in an incompatible on-disk format
66665 ** for the -shm file. Any change that causes one of these asserts to
66666 ** fail is a backward compatibility problem, even if the change otherwise
66667 ** works.
66668 **
66669 ** This table also serves as a helpful cross-reference when trying to
66670 ** interpret hex dumps of the -shm file.
66671 */
66672 assert( 48 == sizeof(WalIndexHdr) )((void) (0));
66673 assert( 40 == sizeof(WalCkptInfo) )((void) (0));
66674 assert( 120 == WALINDEX_LOCK_OFFSET )((void) (0));
66675 assert( 136 == WALINDEX_HDR_SIZE )((void) (0));
66676 assert( 4096 == HASHTABLE_NPAGE )((void) (0));
66677 assert( 4062 == HASHTABLE_NPAGE_ONE )((void) (0));
66678 assert( 8192 == HASHTABLE_NSLOT )((void) (0));
66679 assert( 383 == HASHTABLE_HASH_1 )((void) (0));
66680 assert( 32768 == WALINDEX_PGSZ )((void) (0));
66681 assert( 8 == SQLITE_SHM_NLOCK )((void) (0));
66682 assert( 5 == WAL_NREADER )((void) (0));
66683 assert( 24 == WAL_FRAME_HDRSIZE )((void) (0));
66684 assert( 32 == WAL_HDRSIZE )((void) (0));
66685 assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK )((void) (0));
66686 assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK )((void) (0));
66687 assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK )((void) (0));
66688 assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) )((void) (0));
66689 assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) )((void) (0));
66690 assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) )((void) (0));
66691 assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) )((void) (0));
66692 assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) )((void) (0));
66693
66694 /* In the amalgamation, the os_unix.c and os_win.c source files come before
66695 ** this source file. Verify that the #defines of the locking byte offsets
66696 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
66697 ** For that matter, if the lock offset ever changes from its initial design
66698 ** value of 120, we need to know that so there is an assert() to check it.
66699 */
66700#ifdef WIN_SHM_BASE
66701 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
66702#endif
66703#ifdef UNIX_SHM_BASE((22+8)*4)
66704 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
66705#endif
66706
66707
66708 /* Allocate an instance of struct Wal to return. */
66709 *ppWal = 0;
66710 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
66711 if( !pRet ){
66712 return SQLITE_NOMEM_BKPT7;
66713 }
66714
66715 pRet->pVfs = pVfs;
66716 pRet->pWalFd = (sqlite3_file *)&pRet[1];
66717 pRet->pDbFd = pDbFd;
66718 pRet->readLock = -1;
66719 pRet->mxWalSize = mxWalSize;
66720 pRet->zWalName = zWalName;
66721 pRet->syncHeader = 1;
66722 pRet->padToSectorBoundary = 1;
66723 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE2: WAL_NORMAL_MODE0);
66724
66725 /* Open file handle on the write-ahead log file. */
66726 flags = (SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_WAL0x00080000);
66727 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
66728 if( rc==SQLITE_OK0 && flags&SQLITE_OPEN_READONLY0x00000001 ){
66729 pRet->readOnly = WAL_RDONLY1;
66730 }
66731
66732 if( rc!=SQLITE_OK0 ){
66733 walIndexClose(pRet, 0);
66734 sqlite3OsClose(pRet->pWalFd);
66735 sqlite3_free(pRet);
66736 }else{
66737 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
66738 if( iDC & SQLITE_IOCAP_SEQUENTIAL0x00000400 ){ pRet->syncHeader = 0; }
66739 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 ){
66740 pRet->padToSectorBoundary = 0;
66741 }
66742 *ppWal = pRet;
66743 WALTRACE(("WAL%d: opened\n", pRet));
66744 }
66745 return rc;
66746}
66747
66748/*
66749** Change the size to which the WAL file is truncated on each reset.
66750*/
66751SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal *pWal, i64 iLimit){
66752 if( pWal ) pWal->mxWalSize = iLimit;
66753}
66754
66755/*
66756** Find the smallest page number out of all pages held in the WAL that
66757** has not been returned by any prior invocation of this method on the
66758** same WalIterator object. Write into *piFrame the frame index where
66759** that page was last written into the WAL. Write into *piPage the page
66760** number.
66761**
66762** Return 0 on success. If there are no pages in the WAL with a page
66763** number larger than *piPage, then return 1.
66764*/
66765static int walIteratorNext(
66766 WalIterator *p, /* Iterator */
66767 u32 *piPage, /* OUT: The page number of the next page */
66768 u32 *piFrame /* OUT: Wal frame index of next page */
66769){
66770 u32 iMin; /* Result pgno must be greater than iMin */
66771 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
66772 int i; /* For looping through segments */
66773
66774 iMin = p->iPrior;
66775 assert( iMin<0xffffffff )((void) (0));
66776 for(i=p->nSegment-1; i>=0; i--){
66777 struct WalSegment *pSegment = &p->aSegment[i];
66778 while( pSegment->iNext<pSegment->nEntry ){
66779 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
66780 if( iPg>iMin ){
66781 if( iPg<iRet ){
66782 iRet = iPg;
66783 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
66784 }
66785 break;
66786 }
66787 pSegment->iNext++;
66788 }
66789 }
66790
66791 *piPage = p->iPrior = iRet;
66792 return (iRet==0xFFFFFFFF);
66793}
66794
66795/*
66796** This function merges two sorted lists into a single sorted list.
66797**
66798** aLeft[] and aRight[] are arrays of indices. The sort key is
66799** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
66800** is guaranteed for all J<K:
66801**
66802** aContent[aLeft[J]] < aContent[aLeft[K]]
66803** aContent[aRight[J]] < aContent[aRight[K]]
66804**
66805** This routine overwrites aRight[] with a new (probably longer) sequence
66806** of indices such that the aRight[] contains every index that appears in
66807** either aLeft[] or the old aRight[] and such that the second condition
66808** above is still met.
66809**
66810** The aContent[aLeft[X]] values will be unique for all X. And the
66811** aContent[aRight[X]] values will be unique too. But there might be
66812** one or more combinations of X and Y such that
66813**
66814** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
66815**
66816** When that happens, omit the aLeft[X] and use the aRight[Y] index.
66817*/
66818static void walMerge(
66819 const u32 *aContent, /* Pages in wal - keys for the sort */
66820 ht_slot *aLeft, /* IN: Left hand input list */
66821 int nLeft, /* IN: Elements in array *paLeft */
66822 ht_slot **paRight, /* IN/OUT: Right hand input list */
66823 int *pnRight, /* IN/OUT: Elements in *paRight */
66824 ht_slot *aTmp /* Temporary buffer */
66825){
66826 int iLeft = 0; /* Current index in aLeft */
66827 int iRight = 0; /* Current index in aRight */
66828 int iOut = 0; /* Current index in output buffer */
66829 int nRight = *pnRight;
66830 ht_slot *aRight = *paRight;
66831
66832 assert( nLeft>0 && nRight>0 )((void) (0));
66833 while( iRight<nRight || iLeft<nLeft ){
66834 ht_slot logpage;
66835 Pgno dbpage;
66836
66837 if( (iLeft<nLeft)
66838 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
66839 ){
66840 logpage = aLeft[iLeft++];
66841 }else{
66842 logpage = aRight[iRight++];
66843 }
66844 dbpage = aContent[logpage];
66845
66846 aTmp[iOut++] = logpage;
66847 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
66848
66849 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage )((void) (0));
66850 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage )((void) (0));
66851 }
66852
66853 *paRight = aLeft;
66854 *pnRight = iOut;
66855 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
66856}
66857
66858/*
66859** Sort the elements in list aList using aContent[] as the sort key.
66860** Remove elements with duplicate keys, preferring to keep the
66861** larger aList[] values.
66862**
66863** The aList[] entries are indices into aContent[]. The values in
66864** aList[] are to be sorted so that for all J<K:
66865**
66866** aContent[aList[J]] < aContent[aList[K]]
66867**
66868** For any X and Y such that
66869**
66870** aContent[aList[X]] == aContent[aList[Y]]
66871**
66872** Keep the larger of the two values aList[X] and aList[Y] and discard
66873** the smaller.
66874*/
66875static void walMergesort(
66876 const u32 *aContent, /* Pages in wal */
66877 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
66878 ht_slot *aList, /* IN/OUT: List to sort */
66879 int *pnList /* IN/OUT: Number of elements in aList[] */
66880){
66881 struct Sublist {
66882 int nList; /* Number of elements in aList */
66883 ht_slot *aList; /* Pointer to sub-list content */
66884 };
66885
66886 const int nList = *pnList; /* Size of input list */
66887 int nMerge = 0; /* Number of elements in list aMerge */
66888 ht_slot *aMerge = 0; /* List to be merged */
66889 int iList; /* Index into input list */
66890 u32 iSub = 0; /* Index into aSub array */
66891 struct Sublist aSub[13]; /* Array of sub-lists */
66892
66893 memset(aSub, 0, sizeof(aSub));
66894 assert( nList<=HASHTABLE_NPAGE && nList>0 )((void) (0));
66895 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) )((void) (0));
66896
66897 for(iList=0; iList<nList; iList++){
66898 nMerge = 1;
66899 aMerge = &aList[iList];
66900 for(iSub=0; iList & (1<<iSub); iSub++){
66901 struct Sublist *p;
66902 assert( iSub<ArraySize(aSub) )((void) (0));
66903 p = &aSub[iSub];
66904 assert( p->aList && p->nList<=(1<<iSub) )((void) (0));
66905 assert( p->aList==&aList[iList&~((2<<iSub)-1)] )((void) (0));
66906 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66907 }
66908 aSub[iSub].aList = aMerge;
66909 aSub[iSub].nList = nMerge;
66910 }
66911
66912 for(iSub++; iSub<ArraySize(aSub)((int)(sizeof(aSub)/sizeof(aSub[0]))); iSub++){
66913 if( nList & (1<<iSub) ){
66914 struct Sublist *p;
66915 assert( iSub<ArraySize(aSub) )((void) (0));
66916 p = &aSub[iSub];
66917 assert( p->nList<=(1<<iSub) )((void) (0));
66918 assert( p->aList==&aList[nList&~((2<<iSub)-1)] )((void) (0));
66919 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66920 }
66921 }
66922 assert( aMerge==aList )((void) (0));
66923 *pnList = nMerge;
66924
66925#ifdef SQLITE_DEBUG
66926 {
66927 int i;
66928 for(i=1; i<*pnList; i++){
66929 assert( aContent[aList[i]] > aContent[aList[i-1]] )((void) (0));
66930 }
66931 }
66932#endif
66933}
66934
66935/*
66936** Free an iterator allocated by walIteratorInit().
66937*/
66938static void walIteratorFree(WalIterator *p){
66939 sqlite3_free(p);
66940}
66941
66942/*
66943** Construct a WalInterator object that can be used to loop over all
66944** pages in the WAL following frame nBackfill in ascending order. Frames
66945** nBackfill or earlier may be included - excluding them is an optimization
66946** only. The caller must hold the checkpoint lock.
66947**
66948** On success, make *pp point to the newly allocated WalInterator object
66949** return SQLITE_OK. Otherwise, return an error code. If this routine
66950** returns an error, the value of *pp is undefined.
66951**
66952** The calling routine should invoke walIteratorFree() to destroy the
66953** WalIterator object when it has finished with it.
66954*/
66955static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
66956 WalIterator *p; /* Return value */
66957 int nSegment; /* Number of segments to merge */
66958 u32 iLast; /* Last frame in log */
66959 sqlite3_int64 nByte; /* Number of bytes to allocate */
66960 int i; /* Iterator variable */
66961 ht_slot *aTmp; /* Temp space used by merge-sort */
66962 int rc = SQLITE_OK0; /* Return Code */
66963
66964 /* This routine only runs while holding the checkpoint lock. And
66965 ** it only runs if there is actually content in the log (mxFrame>0).
66966 */
66967 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 )((void) (0));
66968 iLast = pWal->hdr.mxFrame;
66969
66970 /* Allocate space for the WalIterator object. */
66971 nSegment = walFramePage(iLast) + 1;
66972 nByte = sizeof(WalIterator)
66973 + (nSegment-1)*sizeof(struct WalSegment)
66974 + iLast*sizeof(ht_slot);
66975 p = (WalIterator *)sqlite3_malloc64(nByte
66976 + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE4096?HASHTABLE_NPAGE4096:iLast)
66977 );
66978 if( !p ){
66979 return SQLITE_NOMEM_BKPT7;
66980 }
66981 memset(p, 0, nByte);
66982 p->nSegment = nSegment;
66983 aTmp = (ht_slot*)&(((u8*)p)[nByte]);
66984 SEH_FREE_ON_ERROR(0, p);
66985 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK0 && i<nSegment; i++){
66986 WalHashLoc sLoc;
66987
66988 rc = walHashGet(pWal, i, &sLoc);
66989 if( rc==SQLITE_OK0 ){
66990 int j; /* Counter variable */
66991 int nEntry; /* Number of entries in this segment */
66992 ht_slot *aIndex; /* Sorted index for this segment */
66993
66994 if( (i+1)==nSegment ){
66995 nEntry = (int)(iLast - sLoc.iZero);
66996 }else{
66997 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
66998 }
66999 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
67000 sLoc.iZero++;
67001
67002 for(j=0; j<nEntry; j++){
67003 aIndex[j] = (ht_slot)j;
67004 }
67005 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
67006 p->aSegment[i].iZero = sLoc.iZero;
67007 p->aSegment[i].nEntry = nEntry;
67008 p->aSegment[i].aIndex = aIndex;
67009 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
67010 }
67011 }
67012 if( rc!=SQLITE_OK0 ){
67013 SEH_FREE_ON_ERROR(p, 0);
67014 walIteratorFree(p);
67015 p = 0;
67016 }
67017 *pp = p;
67018 return rc;
67019}
67020
67021#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67022
67023
67024/*
67025** Attempt to enable blocking locks that block for nMs ms. Return 1 if
67026** blocking locks are successfully enabled, or 0 otherwise.
67027*/
67028static int walEnableBlockingMs(Wal *pWal, int nMs)0{
67029 int rc = sqlite3OsFileControl(
67030 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT34, (void*)&nMs
67031 );
67032 return (rc==SQLITE_OK0);
67033}
67034
67035/*
67036** Attempt to enable blocking locks. Blocking locks are enabled only if (a)
67037** they are supported by the VFS, and (b) the database handle is configured
67038** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
67039** or 0 otherwise.
67040*/
67041static int walEnableBlocking(Wal *pWal)0{
67042 int res = 0;
67043 if( pWal->db ){
67044 int tmout = pWal->db->busyTimeout;
67045 if( tmout ){
67046 res = walEnableBlockingMs(pWal, tmout)0;
67047 }
67048 }
67049 return res;
67050}
67051
67052/*
67053** Disable blocking locks.
67054*/
67055static void walDisableBlocking(Wal *pWal){
67056 int tmout = 0;
67057 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT34, (void*)&tmout);
67058}
67059
67060/*
67061** If parameter bLock is true, attempt to enable blocking locks, take
67062** the WRITER lock, and then disable blocking locks. If blocking locks
67063** cannot be enabled, no attempt to obtain the WRITER lock is made. Return
67064** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not
67065** an error if blocking locks can not be enabled.
67066**
67067** If the bLock parameter is false and the WRITER lock is held, release it.
67068*/
67069SQLITE_PRIVATEstatic int sqlite3WalWriteLock(Wal *pWal, int bLock){
67070 int rc = SQLITE_OK0;
67071 assert( pWal->readLock<0 || bLock==0 )((void) (0));
67072 if( bLock ){
67073 assert( pWal->db )((void) (0));
67074 if( walEnableBlocking(pWal)0 ){
67075 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
67076 if( rc==SQLITE_OK0 ){
67077 pWal->writeLock = 1;
67078 }
67079 walDisableBlocking(pWal);
67080 }
67081 }else if( pWal->writeLock ){
67082 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
67083 pWal->writeLock = 0;
67084 }
67085 return rc;
67086}
67087
67088/*
67089** Set the database handle used to determine if blocking locks are required.
67090*/
67091SQLITE_PRIVATEstatic void sqlite3WalDb(Wal *pWal, sqlite3 *db){
67092 pWal->db = db;
67093}
67094
67095#else
67096# define walEnableBlocking(x)0 0
67097# define walDisableBlocking(x)
67098# define walEnableBlockingMs(pWal, ms)0 0
67099# define sqlite3WalDb(pWal, db)
67100#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */
67101
67102
67103/*
67104** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
67105** n. If the attempt fails and parameter xBusy is not NULL, then it is a
67106** busy-handler function. Invoke it and retry the lock until either the
67107** lock is successfully obtained or the busy-handler returns 0.
67108*/
67109static int walBusyLock(
67110 Wal *pWal, /* WAL connection */
67111 int (*xBusy)(void*), /* Function to call when busy */
67112 void *pBusyArg, /* Context argument for xBusyHandler */
67113 int lockIdx, /* Offset of first byte to lock */
67114 int n /* Number of bytes to lock */
67115){
67116 int rc;
67117 do {
67118 rc = walLockExclusive(pWal, lockIdx, n);
67119 }while( xBusy && rc==SQLITE_BUSY5 && xBusy(pBusyArg) );
67120#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67121 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
67122 walDisableBlocking(pWal);
67123 rc = SQLITE_BUSY5;
67124 }
67125#endif
67126 return rc;
67127}
67128
67129/*
67130** The cache of the wal-index header must be valid to call this function.
67131** Return the page-size in bytes used by the database.
67132*/
67133static int walPagesize(Wal *pWal){
67134 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
67135}
67136
67137/*
67138** The following is guaranteed when this function is called:
67139**
67140** a) the WRITER lock is held,
67141** b) the entire log file has been checkpointed, and
67142** c) any existing readers are reading exclusively from the database
67143** file - there are no readers that may attempt to read a frame from
67144** the log file.
67145**
67146** This function updates the shared-memory structures so that the next
67147** client to write to the database (which may be this one) does so by
67148** writing frames into the start of the log file.
67149**
67150** The value of parameter salt1 is used as the aSalt[1] value in the
67151** new wal-index header. It should be passed a pseudo-random value (i.e.
67152** one obtained from sqlite3_randomness()).
67153*/
67154static void walRestartHdr(Wal *pWal, u32 salt1){
67155 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
67156 int i; /* Loop counter */
67157 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
67158 pWal->nCkpt++;
67159 pWal->hdr.mxFrame = 0;
67160 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
67161 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
67162 walIndexWriteHdr(pWal);
67163 AtomicStore(&pInfo->nBackfill, 0)__atomic_store_n((&pInfo->nBackfill),(0),0);
67164 pInfo->nBackfillAttempted = 0;
67165 pInfo->aReadMark[1] = 0;
67166 for(i=2; i<WAL_NREADER(8 -3); i++) pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
67167 assert( pInfo->aReadMark[0]==0 )((void) (0));
67168}
67169
67170/*
67171** Copy as much content as we can from the WAL back into the database file
67172** in response to an sqlite3_wal_checkpoint() request or the equivalent.
67173**
67174** The amount of information copies from WAL to database might be limited
67175** by active readers. This routine will never overwrite a database page
67176** that a concurrent reader might be using.
67177**
67178** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
67179** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
67180** checkpoints are always run by a background thread or background
67181** process, foreground threads will never block on a lengthy fsync call.
67182**
67183** Fsync is called on the WAL before writing content out of the WAL and
67184** into the database. This ensures that if the new content is persistent
67185** in the WAL and can be recovered following a power-loss or hard reset.
67186**
67187** Fsync is also called on the database file if (and only if) the entire
67188** WAL content is copied into the database file. This second fsync makes
67189** it safe to delete the WAL since the new content will persist in the
67190** database file.
67191**
67192** This routine uses and updates the nBackfill field of the wal-index header.
67193** This is the only routine that will increase the value of nBackfill.
67194** (A WAL reset or recovery will revert nBackfill to zero, but not increase
67195** its value.)
67196**
67197** The caller must be holding sufficient locks to ensure that no other
67198** checkpoint is running (in any other thread or process) at the same
67199** time.
67200*/
67201static int walCheckpoint(
67202 Wal *pWal, /* Wal connection */
67203 sqlite3 *db, /* Check for interrupts on this handle */
67204 int eMode, /* One of PASSIVE, FULL or RESTART */
67205 int (*xBusy)(void*), /* Function to call when busy */
67206 void *pBusyArg, /* Context argument for xBusyHandler */
67207 int sync_flags, /* Flags for OsSync() (or 0) */
67208 u8 *zBuf /* Temporary buffer to use */
67209){
67210 int rc = SQLITE_OK0; /* Return code */
67211 int szPage; /* Database page-size */
67212 WalIterator *pIter = 0; /* Wal iterator context */
67213 u32 iDbpage = 0; /* Next database page to write */
67214 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
67215 u32 mxSafeFrame; /* Max frame that can be backfilled */
67216 u32 mxPage; /* Max database page to write */
67217 int i; /* Loop counter */
67218 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
67219
67220 szPage = walPagesize(pWal);
67221 testcase( szPage<=32768 );
67222 testcase( szPage>=65536 );
67223 pInfo = walCkptInfo(pWal);
67224 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
67225
67226 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
67227 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
67228 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
67229
67230 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
67231 ** safe to write into the database. Frames beyond mxSafeFrame might
67232 ** overwrite database pages that are in use by active readers and thus
67233 ** cannot be backfilled from the WAL.
67234 */
67235 mxSafeFrame = pWal->hdr.mxFrame;
67236 mxPage = pWal->hdr.nPage;
67237 for(i=1; i<WAL_NREADER(8 -3); i++){
67238 u32 y = AtomicLoad(pInfo->aReadMark+i)__atomic_load_n((pInfo->aReadMark+i),0); SEH_INJECT_FAULT((void) (0));;
67239 if( mxSafeFrame>y ){
67240 assert( y<=pWal->hdr.mxFrame )((void) (0));
67241 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i)(3+(i)), 1);
67242 if( rc==SQLITE_OK0 ){
67243 u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED0xffffffff);
67244 AtomicStore(pInfo->aReadMark+i, iMark)__atomic_store_n((pInfo->aReadMark+i),(iMark),0); SEH_INJECT_FAULT((void) (0));;
67245 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
67246 }else if( rc==SQLITE_BUSY5 ){
67247 mxSafeFrame = y;
67248 xBusy = 0;
67249 }else{
67250 goto walcheckpoint_out;
67251 }
67252 }
67253 }
67254
67255 /* Allocate the iterator */
67256 if( pInfo->nBackfill<mxSafeFrame ){
67257 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
67258 assert( rc==SQLITE_OK || pIter==0 )((void) (0));
67259 }
67260
67261 if( pIter
67262 && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0)(3+(0)),1))==SQLITE_OK0
67263 ){
67264 u32 nBackfill = pInfo->nBackfill;
67265 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT((void) (0));;
67266
67267 /* Sync the WAL to disk */
67268 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
67269
67270 /* If the database may grow as a result of this checkpoint, hint
67271 ** about the eventual size of the db file to the VFS layer.
67272 */
67273 if( rc==SQLITE_OK0 ){
67274 i64 nReq = ((i64)mxPage * szPage);
67275 i64 nSize; /* Current size of database file */
67276 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START39, 0);
67277 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
67278 if( rc==SQLITE_OK0 && nSize<nReq ){
67279 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
67280 /* If the size of the final database is larger than the current
67281 ** database plus the amount of data in the wal file, plus the
67282 ** maximum size of the pending-byte page (65536 bytes), then
67283 ** must be corruption somewhere. */
67284 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67284);
67285 }else{
67286 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT5,&nReq);
67287 }
67288 }
67289
67290 }
67291
67292 /* Iterate through the contents of the WAL, copying data to the db file */
67293 while( rc==SQLITE_OK0 && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
67294 i64 iOffset;
67295 assert( walFramePgno(pWal, iFrame)==iDbpage )((void) (0));
67296 SEH_INJECT_FAULT((void) (0));;
67297 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
67298 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT7 : SQLITE_INTERRUPT9;
67299 break;
67300 }
67301 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
67302 continue;
67303 }
67304 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
67305 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
67306 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
67307 if( rc!=SQLITE_OK0 ) break;
67308 iOffset = (iDbpage-1)*(i64)szPage;
67309 testcase( IS_BIG_INT(iOffset) );
67310 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
67311 if( rc!=SQLITE_OK0 ) break;
67312 }
67313 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE37, 0);
67314
67315 /* If work was actually accomplished... */
67316 if( rc==SQLITE_OK0 ){
67317 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
67318 i64 szDb = pWal->hdr.nPage*(i64)szPage;
67319 testcase( IS_BIG_INT(szDb) );
67320 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
67321 if( rc==SQLITE_OK0 ){
67322 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
67323 }
67324 }
67325 if( rc==SQLITE_OK0 ){
67326 AtomicStore(&pInfo->nBackfill, mxSafeFrame)__atomic_store_n((&pInfo->nBackfill),(mxSafeFrame),0); SEH_INJECT_FAULT((void) (0));;
67327 }
67328 }
67329
67330 /* Release the reader lock held while backfilling */
67331 walUnlockExclusive(pWal, WAL_READ_LOCK(0)(3+(0)), 1);
67332 }
67333
67334 if( rc==SQLITE_BUSY5 ){
67335 /* Reset the return code so as not to report a checkpoint failure
67336 ** just because there are active readers. */
67337 rc = SQLITE_OK0;
67338 }
67339 }
67340
67341 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
67342 ** entire wal file has been copied into the database file, then block
67343 ** until all readers have finished using the wal file. This ensures that
67344 ** the next process to write to the database restarts the wal file.
67345 */
67346 if( rc==SQLITE_OK0 && eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
67347 assert( pWal->writeLock )((void) (0));
67348 SEH_INJECT_FAULT((void) (0));;
67349 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
67350 rc = SQLITE_BUSY5;
67351 }else if( eMode>=SQLITE_CHECKPOINT_RESTART2 ){
67352 u32 salt1;
67353 sqlite3_randomness(4, &salt1);
67354 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
67355 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
67356 if( rc==SQLITE_OK0 ){
67357 if( eMode==SQLITE_CHECKPOINT_TRUNCATE3 ){
67358 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
67359 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
67360 ** truncates the log file to zero bytes just prior to a
67361 ** successful return.
67362 **
67363 ** In theory, it might be safe to do this without updating the
67364 ** wal-index header in shared memory, as all subsequent reader or
67365 ** writer clients should see that the entire log file has been
67366 ** checkpointed and behave accordingly. This seems unsafe though,
67367 ** as it would leave the system in a state where the contents of
67368 ** the wal-index header do not match the contents of the
67369 ** file-system. To avoid this, update the wal-index header to
67370 ** indicate that the log file contains zero valid frames. */
67371 walRestartHdr(pWal, salt1);
67372 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
67373 }
67374 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
67375 }
67376 }
67377 }
67378
67379 walcheckpoint_out:
67380 SEH_FREE_ON_ERROR(pIter, 0);
67381 walIteratorFree(pIter);
67382 return rc;
67383}
67384
67385/*
67386** If the WAL file is currently larger than nMax bytes in size, truncate
67387** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
67388*/
67389static void walLimitSize(Wal *pWal, i64 nMax){
67390 i64 sz;
67391 int rx;
67392 sqlite3BeginBenignMalloc();
67393 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
67394 if( rx==SQLITE_OK0 && (sz > nMax ) ){
67395 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
67396 }
67397 sqlite3EndBenignMalloc();
67398 if( rx ){
67399 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
67400 }
67401}
67402
67403#ifdef SQLITE_USE_SEH
67404/*
67405** This is the "standard" exception handler used in a few places to handle
67406** an exception thrown by reading from the *-shm mapping after it has become
67407** invalid in SQLITE_USE_SEH builds. It is used as follows:
67408**
67409** SEH_TRY { ... }
67410** SEH_EXCEPT( rc = walHandleException(pWal); )
67411**
67412** This function does three things:
67413**
67414** 1) Determines the locks that should be held, based on the contents of
67415** the Wal.readLock, Wal.writeLock and Wal.ckptLock variables. All other
67416** held locks are assumed to be transient locks that would have been
67417** released had the exception not been thrown and are dropped.
67418**
67419** 2) Frees the pointer at Wal.pFree, if any, using sqlite3_free().
67420**
67421** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
67422**
67423** 4) Returns SQLITE_IOERR.
67424*/
67425static int walHandleException(Wal *pWal){
67426 if( pWal->exclusiveMode==0 ){
67427 static const int S = 1;
67428 static const int E = (1<<SQLITE_SHM_NLOCK8);
67429 int ii;
67430 u32 mUnlock = pWal->lockMask & ~(
67431 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock))))
67432 | (pWal->writeLock ? (E << WAL_WRITE_LOCK0) : 0)
67433 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK1) : 0)
67434 );
67435 for(ii=0; ii<SQLITE_SHM_NLOCK8; ii++){
67436 if( (S<<ii) & mUnlock ) walUnlockShared(pWal, ii);
67437 if( (E<<ii) & mUnlock ) walUnlockExclusive(pWal, ii, 1);
67438 }
67439 }
67440 sqlite3_free(pWal->pFree);
67441 pWal->pFree = 0;
67442 if( pWal->pWiValue ){
67443 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
67444 pWal->pWiValue = 0;
67445 }
67446 return SQLITE_IOERR_IN_PAGE(10 | (34<<8));
67447}
67448
67449/*
67450** Assert that the Wal.lockMask mask, which indicates the locks held
67451** by the connection, is consistent with the Wal.readLock, Wal.writeLock
67452** and Wal.ckptLock variables. To be used as:
67453**
67454** assert( walAssertLockmask(pWal) );
67455*/
67456static int walAssertLockmask(Wal *pWal)1{
67457 if( pWal->exclusiveMode==0 ){
67458 static const int S = 1;
67459 static const int E = (1<<SQLITE_SHM_NLOCK8);
67460 u32 mExpect = (
67461 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock))))
67462 | (pWal->writeLock ? (E << WAL_WRITE_LOCK0) : 0)
67463 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK1) : 0)
67464#ifdef SQLITE_ENABLE_SNAPSHOT1
67465 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK1)) : 0)
67466#endif
67467 );
67468 assert( mExpect==pWal->lockMask )((void) (0));
67469 }
67470 return 1;
67471}
67472
67473/*
67474** Return and zero the "system error" field set when an
67475** EXCEPTION_IN_PAGE_ERROR exception is caught.
67476*/
67477SQLITE_PRIVATEstatic int sqlite3WalSystemErrno(Wal *pWal){
67478 int iRet = 0;
67479 if( pWal ){
67480 iRet = pWal->iSysErrno;
67481 pWal->iSysErrno = 0;
67482 }
67483 return iRet;
67484}
67485
67486#else
67487# define walAssertLockmask(x)1 1
67488#endif /* ifdef SQLITE_USE_SEH */
67489
67490/*
67491** Close a connection to a log file.
67492*/
67493SQLITE_PRIVATEstatic int sqlite3WalClose(
67494 Wal *pWal, /* Wal to close */
67495 sqlite3 *db, /* For interrupt flag */
67496 int sync_flags, /* Flags to pass to OsSync() (or 0) */
67497 int nBuf,
67498 u8 *zBuf /* Buffer of at least nBuf bytes */
67499){
67500 int rc = SQLITE_OK0;
67501 if( pWal ){
67502 int isDelete = 0; /* True to unlink wal and wal-index files */
67503
67504 assert( walAssertLockmask(pWal) )((void) (0));
67505
67506 /* If an EXCLUSIVE lock can be obtained on the database file (using the
67507 ** ordinary, rollback-mode locking methods, this guarantees that the
67508 ** connection associated with this log file is the only connection to
67509 ** the database. In this case checkpoint the database and unlink both
67510 ** the wal and wal-index files.
67511 **
67512 ** The EXCLUSIVE lock is not released before returning.
67513 */
67514 if( zBuf!=0
67515 && SQLITE_OK0==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE4))
67516 ){
67517 if( pWal->exclusiveMode==WAL_NORMAL_MODE0 ){
67518 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
67519 }
67520 rc = sqlite3WalCheckpoint(pWal, db,
67521 SQLITE_CHECKPOINT_PASSIVE0, 0, 0, sync_flags, nBuf, zBuf, 0, 0
67522 );
67523 if( rc==SQLITE_OK0 ){
67524 int bPersist = -1;
67525 sqlite3OsFileControlHint(
67526 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL10, &bPersist
67527 );
67528 if( bPersist!=1 ){
67529 /* Try to delete the WAL file if the checkpoint completed and
67530 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
67531 ** mode (!bPersist) */
67532 isDelete = 1;
67533 }else if( pWal->mxWalSize>=0 ){
67534 /* Try to truncate the WAL file to zero bytes if the checkpoint
67535 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
67536 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
67537 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
67538 ** to zero bytes as truncating to the journal_size_limit might
67539 ** leave a corrupt WAL file on disk. */
67540 walLimitSize(pWal, 0);
67541 }
67542 }
67543 }
67544
67545 walIndexClose(pWal, isDelete);
67546 sqlite3OsClose(pWal->pWalFd);
67547 if( isDelete ){
67548 sqlite3BeginBenignMalloc();
67549 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
67550 sqlite3EndBenignMalloc();
67551 }
67552 WALTRACE(("WAL%p: closed\n", pWal));
67553 sqlite3_free((void *)pWal->apWiData);
67554 sqlite3_free(pWal);
67555 }
67556 return rc;
67557}
67558
67559/*
67560** Try to read the wal-index header. Return 0 on success and 1 if
67561** there is a problem.
67562**
67563** The wal-index is in shared memory. Another thread or process might
67564** be writing the header at the same time this procedure is trying to
67565** read it, which might result in inconsistency. A dirty read is detected
67566** by verifying that both copies of the header are the same and also by
67567** a checksum on the header.
67568**
67569** If and only if the read is consistent and the header is different from
67570** pWal->hdr, then pWal->hdr is updated to the content of the new header
67571** and *pChanged is set to 1.
67572**
67573** If the checksum cannot be verified return non-zero. If the header
67574** is read successfully and the checksum verified, return zero.
67575*/
67576static SQLITE_NO_TSAN__attribute__((no_sanitize_thread)) int walIndexTryHdr(Wal *pWal, int *pChanged){
67577 u32 aCksum[2]; /* Checksum on the header content */
67578 WalIndexHdr h1, h2; /* Two copies of the header content */
67579 WalIndexHdr volatile *aHdr; /* Header in shared memory */
67580
67581 /* The first page of the wal-index must be mapped at this point. */
67582 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
67583
67584 /* Read the header. This might happen concurrently with a write to the
67585 ** same area of shared memory on a different CPU in a SMP,
67586 ** meaning it is possible that an inconsistent snapshot is read
67587 ** from the file. If this happens, return non-zero.
67588 **
67589 ** tag-20200519-1:
67590 ** There are two copies of the header at the beginning of the wal-index.
67591 ** When reading, read [0] first then [1]. Writes are in the reverse order.
67592 ** Memory barriers are used to prevent the compiler or the hardware from
67593 ** reordering the reads and writes. TSAN and similar tools can sometimes
67594 ** give false-positive warnings about these accesses because the tools do not
67595 ** account for the double-read and the memory barrier. The use of mutexes
67596 ** here would be problematic as the memory being accessed is potentially
67597 ** shared among multiple processes and not all mutex implementations work
67598 ** reliably in that environment.
67599 */
67600 aHdr = walIndexHdr(pWal);
67601 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
67602 walShmBarrier(pWal);
67603 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
67604
67605 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
67606 return 1; /* Dirty read */
67607 }
67608 if( h1.isInit==0 ){
67609 return 1; /* Malformed header - probably all zeros */
67610 }
67611 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
67612 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
67613 return 1; /* Checksum does not match */
67614 }
67615
67616 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
67617 *pChanged = 1;
67618 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
67619 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
67620 testcase( pWal->szPage<=32768 );
67621 testcase( pWal->szPage>=65536 );
67622 }
67623
67624 /* The header was successfully read. Return zero. */
67625 return 0;
67626}
67627
67628/*
67629** This is the value that walTryBeginRead returns when it needs to
67630** be retried.
67631*/
67632#define WAL_RETRY(-1) (-1)
67633
67634/*
67635** Read the wal-index header from the wal-index and into pWal->hdr.
67636** If the wal-header appears to be corrupt, try to reconstruct the
67637** wal-index from the WAL before returning.
67638**
67639** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
67640** changed by this operation. If pWal->hdr is unchanged, set *pChanged
67641** to 0.
67642**
67643** If the wal-index header is successfully read, return SQLITE_OK.
67644** Otherwise an SQLite error code.
67645*/
67646static int walIndexReadHdr(Wal *pWal, int *pChanged){
67647 int rc; /* Return code */
67648 int badHdr; /* True if a header read failed */
67649 volatile u32 *page0; /* Chunk of wal-index containing header */
67650
67651 /* Ensure that page 0 of the wal-index (the page that contains the
67652 ** wal-index header) is mapped. Return early if an error occurs here.
67653 */
67654 assert( pChanged )((void) (0));
67655 rc = walIndexPage(pWal, 0, &page0);
67656 if( rc!=SQLITE_OK0 ){
67657 assert( rc!=SQLITE_READONLY )((void) (0)); /* READONLY changed to OK in walIndexPage */
67658 if( rc==SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
67659 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
67660 ** was openable but is not writable, and this thread is unable to
67661 ** confirm that another write-capable connection has the shared-memory
67662 ** open, and hence the content of the shared-memory is unreliable,
67663 ** since the shared-memory might be inconsistent with the WAL file
67664 ** and there is no writer on hand to fix it. */
67665 assert( page0==0 )((void) (0));
67666 assert( pWal->writeLock==0 )((void) (0));
67667 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
67668 pWal->bShmUnreliable = 1;
67669 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE2;
67670 *pChanged = 1;
67671 }else{
67672 return rc; /* Any other non-OK return is just an error */
67673 }
67674 }else{
67675 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
67676 ** is zero, which prevents the SHM from growing */
67677 testcase( page0!=0 );
67678 }
67679 assert( page0!=0 || pWal->writeLock==0 )((void) (0));
67680
67681 /* If the first page of the wal-index has been mapped, try to read the
67682 ** wal-index header immediately, without holding any lock. This usually
67683 ** works, but may fail if the wal-index header is corrupt or currently
67684 ** being modified by another thread or process.
67685 */
67686 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
67687
67688 /* If the first attempt failed, it might have been due to a race
67689 ** with a writer. So get a WRITE lock and try again.
67690 */
67691 if( badHdr ){
67692 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY2) ){
67693 if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_WRITE_LOCK0)) ){
67694 walUnlockShared(pWal, WAL_WRITE_LOCK0);
67695 rc = SQLITE_READONLY_RECOVERY(8 | (1<<8));
67696 }
67697 }else{
67698 int bWriteLock = pWal->writeLock;
67699 if( bWriteLock
67700 || SQLITE_OK0==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1))
67701 ){
67702 pWal->writeLock = 1;
67703 if( SQLITE_OK0==(rc = walIndexPage(pWal, 0, &page0)) ){
67704 badHdr = walIndexTryHdr(pWal, pChanged);
67705 if( badHdr ){
67706 /* If the wal-index header is still malformed even while holding
67707 ** a WRITE lock, it can only mean that the header is corrupted and
67708 ** needs to be reconstructed. So run recovery to do exactly that.
67709 ** Disable blocking locks first. */
67710 walDisableBlocking(pWal);
67711 rc = walIndexRecover(pWal);
67712 *pChanged = 1;
67713 }
67714 }
67715 if( bWriteLock==0 ){
67716 pWal->writeLock = 0;
67717 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
67718 }
67719 }
67720 }
67721 }
67722
67723 /* If the header is read successfully, check the version number to make
67724 ** sure the wal-index was not constructed with some future format that
67725 ** this version of SQLite cannot understand.
67726 */
67727 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION3007000 ){
67728 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(67728);
67729 }
67730 if( pWal->bShmUnreliable ){
67731 if( rc!=SQLITE_OK0 ){
67732 walIndexClose(pWal, 0);
67733 pWal->bShmUnreliable = 0;
67734 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 )((void) (0));
67735 /* walIndexRecover() might have returned SHORT_READ if a concurrent
67736 ** writer truncated the WAL out from under it. If that happens, it
67737 ** indicates that a writer has fixed the SHM file for us, so retry */
67738 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ) rc = WAL_RETRY(-1);
67739 }
67740 pWal->exclusiveMode = WAL_NORMAL_MODE0;
67741 }
67742
67743 return rc;
67744}
67745
67746/*
67747** Open a transaction in a connection where the shared-memory is read-only
67748** and where we cannot verify that there is a separate write-capable connection
67749** on hand to keep the shared-memory up-to-date with the WAL file.
67750**
67751** This can happen, for example, when the shared-memory is implemented by
67752** memory-mapping a *-shm file, where a prior writer has shut down and
67753** left the *-shm file on disk, and now the present connection is trying
67754** to use that database but lacks write permission on the *-shm file.
67755** Other scenarios are also possible, depending on the VFS implementation.
67756**
67757** Precondition:
67758**
67759** The *-wal file has been read and an appropriate wal-index has been
67760** constructed in pWal->apWiData[] using heap memory instead of shared
67761** memory.
67762**
67763** If this function returns SQLITE_OK, then the read transaction has
67764** been successfully opened. In this case output variable (*pChanged)
67765** is set to true before returning if the caller should discard the
67766** contents of the page cache before proceeding. Or, if it returns
67767** WAL_RETRY, then the heap memory wal-index has been discarded and
67768** the caller should retry opening the read transaction from the
67769** beginning (including attempting to map the *-shm file).
67770**
67771** If an error occurs, an SQLite error code is returned.
67772*/
67773static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
67774 i64 szWal; /* Size of wal file on disk in bytes */
67775 i64 iOffset; /* Current offset when reading wal file */
67776 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
67777 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
67778 int szFrame; /* Number of bytes in buffer aFrame[] */
67779 u8 *aData; /* Pointer to data part of aFrame buffer */
67780 volatile void *pDummy; /* Dummy argument for xShmMap */
67781 int rc; /* Return code */
67782 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
67783
67784 assert( pWal->bShmUnreliable )((void) (0));
67785 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
67786 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
67787
67788 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
67789 ** writers from running a checkpoint, but does not stop them
67790 ** from running recovery. */
67791 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
67792 if( rc!=SQLITE_OK0 ){
67793 if( rc==SQLITE_BUSY5 ) rc = WAL_RETRY(-1);
67794 goto begin_unreliable_shm_out;
67795 }
67796 pWal->readLock = 0;
67797
67798 /* Check to see if a separate writer has attached to the shared-memory area,
67799 ** thus making the shared-memory "reliable" again. Do this by invoking
67800 ** the xShmMap() routine of the VFS and looking to see if the return
67801 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
67802 **
67803 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
67804 ** cause the heap-memory WAL-index to be discarded and the actual
67805 ** shared memory to be used in its place.
67806 **
67807 ** This step is important because, even though this connection is holding
67808 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
67809 ** have already checkpointed the WAL file and, while the current
67810 ** is active, wrap the WAL and start overwriting frames that this
67811 ** process wants to use.
67812 **
67813 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
67814 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
67815 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
67816 ** even if some external agent does a "chmod" to make the shared-memory
67817 ** writable by us, until sqlite3OsShmUnmap() has been called.
67818 ** This is a requirement on the VFS implementation.
67819 */
67820 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ), 0, &pDummy);
67821 assert( rc!=SQLITE_OK )((void) (0)); /* SQLITE_OK not possible for read-only connection */
67822 if( rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
67823 rc = (rc==SQLITE_READONLY8 ? WAL_RETRY(-1) : rc);
67824 goto begin_unreliable_shm_out;
67825 }
67826
67827 /* We reach this point only if the real shared-memory is still unreliable.
67828 ** Assume the in-memory WAL-index substitute is correct and load it
67829 ** into pWal->hdr.
67830 */
67831 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
67832
67833 /* Make sure some writer hasn't come in and changed the WAL file out
67834 ** from under us, then disconnected, while we were not looking.
67835 */
67836 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
67837 if( rc!=SQLITE_OK0 ){
67838 goto begin_unreliable_shm_out;
67839 }
67840 if( szWal<WAL_HDRSIZE32 ){
67841 /* If the wal file is too small to contain a wal-header and the
67842 ** wal-index header has mxFrame==0, then it must be safe to proceed
67843 ** reading the database file only. However, the page cache cannot
67844 ** be trusted, as a read/write connection may have connected, written
67845 ** the db, run a checkpoint, truncated the wal file and disconnected
67846 ** since this client's last read transaction. */
67847 *pChanged = 1;
67848 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK0 : WAL_RETRY(-1));
67849 goto begin_unreliable_shm_out;
67850 }
67851
67852 /* Check the salt keys at the start of the wal file still match. */
67853 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
67854 if( rc!=SQLITE_OK0 ){
67855 goto begin_unreliable_shm_out;
67856 }
67857 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
67858 /* Some writer has wrapped the WAL file while we were not looking.
67859 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
67860 ** rebuilt. */
67861 rc = WAL_RETRY(-1);
67862 goto begin_unreliable_shm_out;
67863 }
67864
67865 /* Allocate a buffer to read frames into */
67866 assert( (pWal->szPage & (pWal->szPage-1))==0 )((void) (0));
67867 assert( pWal->szPage>=512 && pWal->szPage<=65536 )((void) (0));
67868 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE24;
67869 aFrame = (u8 *)sqlite3_malloc64(szFrame);
67870 if( aFrame==0 ){
67871 rc = SQLITE_NOMEM_BKPT7;
67872 goto begin_unreliable_shm_out;
67873 }
67874 aData = &aFrame[WAL_FRAME_HDRSIZE24];
67875
67876 /* Check to see if a complete transaction has been appended to the
67877 ** wal file since the heap-memory wal-index was created. If so, the
67878 ** heap-memory wal-index is discarded and WAL_RETRY returned to
67879 ** the caller. */
67880 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
67881 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
67882 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage)( 32 + ((pWal->hdr.mxFrame+1)-1)*(i64)((pWal->szPage)+24
) )
;
67883 iOffset+szFrame<=szWal;
67884 iOffset+=szFrame
67885 ){
67886 u32 pgno; /* Database page number for frame */
67887 u32 nTruncate; /* dbsize field from frame header */
67888
67889 /* Read and decode the next log frame. */
67890 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
67891 if( rc!=SQLITE_OK0 ) break;
67892 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
67893
67894 /* If nTruncate is non-zero, then a complete transaction has been
67895 ** appended to this wal file. Set rc to WAL_RETRY and break out of
67896 ** the loop. */
67897 if( nTruncate ){
67898 rc = WAL_RETRY(-1);
67899 break;
67900 }
67901 }
67902 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
67903 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
67904
67905 begin_unreliable_shm_out:
67906 sqlite3_free(aFrame);
67907 if( rc!=SQLITE_OK0 ){
67908 int i;
67909 for(i=0; i<pWal->nWiData; i++){
67910 sqlite3_free((void*)pWal->apWiData[i]);
67911 pWal->apWiData[i] = 0;
67912 }
67913 pWal->bShmUnreliable = 0;
67914 sqlite3WalEndReadTransaction(pWal);
67915 *pChanged = 1;
67916 }
67917 return rc;
67918}
67919
67920/*
67921** The final argument passed to walTryBeginRead() is of type (int*). The
67922** caller should invoke walTryBeginRead as follows:
67923**
67924** int cnt = 0;
67925** do {
67926** rc = walTryBeginRead(..., &cnt);
67927** }while( rc==WAL_RETRY );
67928**
67929** The final value of "cnt" is of no use to the caller. It is used by
67930** the implementation of walTryBeginRead() as follows:
67931**
67932** + Each time walTryBeginRead() is called, it is incremented. Once
67933** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
67934** has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
67935** returns SQLITE_PROTOCOL.
67936**
67937** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed
67938** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS
67939** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case
67940** the next invocation of walTryBeginRead() may omit an expected call to
67941** sqlite3OsSleep(). There has already been a delay when the previous call
67942** waited on a lock.
67943*/
67944#define WAL_RETRY_PROTOCOL_LIMIT100 100
67945#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67946# define WAL_RETRY_BLOCKED_MASK0 0x10000000
67947#else
67948# define WAL_RETRY_BLOCKED_MASK0 0
67949#endif
67950
67951/*
67952** Attempt to start a read transaction. This might fail due to a race or
67953** other transient condition. When that happens, it returns WAL_RETRY to
67954** indicate to the caller that it is safe to retry immediately.
67955**
67956** On success return SQLITE_OK. On a permanent failure (such an
67957** I/O error or an SQLITE_BUSY because another process is running
67958** recovery) return a positive error code.
67959**
67960** The useWal parameter is true to force the use of the WAL and disable
67961** the case where the WAL is bypassed because it has been completely
67962** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
67963** to make a copy of the wal-index header into pWal->hdr. If the
67964** wal-index header has changed, *pChanged is set to 1 (as an indication
67965** to the caller that the local page cache is obsolete and needs to be
67966** flushed.) When useWal==1, the wal-index header is assumed to already
67967** be loaded and the pChanged parameter is unused.
67968**
67969** The caller must set the cnt parameter to the number of prior calls to
67970** this routine during the current read attempt that returned WAL_RETRY.
67971** This routine will start taking more aggressive measures to clear the
67972** race conditions after multiple WAL_RETRY returns, and after an excessive
67973** number of errors will ultimately return SQLITE_PROTOCOL. The
67974** SQLITE_PROTOCOL return indicates that some other process has gone rogue
67975** and is not honoring the locking protocol. There is a vanishingly small
67976** chance that SQLITE_PROTOCOL could be returned because of a run of really
67977** bad luck when there is lots of contention for the wal-index, but that
67978** possibility is so small that it can be safely neglected, we believe.
67979**
67980** On success, this routine obtains a read lock on
67981** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
67982** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
67983** that means the Wal does not hold any read lock. The reader must not
67984** access any database page that is modified by a WAL frame up to and
67985** including frame number aReadMark[pWal->readLock]. The reader will
67986** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
67987** Or if pWal->readLock==0, then the reader will ignore the WAL
67988** completely and get all content directly from the database file.
67989** If the useWal parameter is 1 then the WAL will never be ignored and
67990** this routine will always set pWal->readLock>0 on success.
67991** When the read transaction is completed, the caller must release the
67992** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
67993**
67994** This routine uses the nBackfill and aReadMark[] fields of the header
67995** to select a particular WAL_READ_LOCK() that strives to let the
67996** checkpoint process do as much work as possible. This routine might
67997** update values of the aReadMark[] array in the header, but if it does
67998** so it takes care to hold an exclusive lock on the corresponding
67999** WAL_READ_LOCK() while changing values.
68000*/
68001static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
68002 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
68003 u32 mxReadMark; /* Largest aReadMark[] value */
68004 int mxI; /* Index of largest aReadMark[] value */
68005 int i; /* Loop counter */
68006 int rc = SQLITE_OK0; /* Return code */
68007 u32 mxFrame; /* Wal frame to lock to */
68008#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68009 int nBlockTmout = 0;
68010#endif
68011
68012 assert( pWal->readLock<0 )((void) (0)); /* Not currently locked */
68013
68014 /* useWal may only be set for read/write connections */
68015 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 )((void) (0));
68016
68017 /* Take steps to avoid spinning forever if there is a protocol error.
68018 **
68019 ** Circumstances that cause a RETRY should only last for the briefest
68020 ** instances of time. No I/O or other system calls are done while the
68021 ** locks are held, so the locks should not be held for very long. But
68022 ** if we are unlucky, another process that is holding a lock might get
68023 ** paged out or take a page-fault that is time-consuming to resolve,
68024 ** during the few nanoseconds that it is holding the lock. In that case,
68025 ** it might take longer than normal for the lock to free.
68026 **
68027 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
68028 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
68029 ** is more of a scheduler yield than an actual delay. But on the 10th
68030 ** an subsequent retries, the delays start becoming longer and longer,
68031 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
68032 ** The total delay time before giving up is less than 10 seconds.
68033 */
68034 (*pCnt)++;
68035 if( *pCnt>5 ){
68036 int nDelay = 1; /* Pause time in microseconds */
68037 int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK0);
68038 if( cnt>WAL_RETRY_PROTOCOL_LIMIT100 ){
68039 VVA_ONLY( pWal->lockError = 1; )
68040 return SQLITE_PROTOCOL15;
68041 }
68042 if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
68043#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68044 /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor
68045 ** to block for locks for approximately nDelay us. This affects three
68046 ** locks: (a) the shared lock taken on the DMS slot in os_unix.c (if
68047 ** using os_unix.c), (b) the WRITER lock taken in walIndexReadHdr() if the
68048 ** first attempted read fails, and (c) the shared lock taken on the
68049 ** read-mark.
68050 **
68051 ** If the previous call failed due to an SQLITE_BUSY_TIMEOUT error,
68052 ** then sleep for the minimum of 1us. The previous call already provided
68053 ** an extra delay while it was blocking on the lock.
68054 */
68055 nBlockTmout = (nDelay+998) / 1000;
68056 if( !useWal && walEnableBlockingMs(pWal, nBlockTmout)0 ){
68057 if( *pCnt & WAL_RETRY_BLOCKED_MASK0 ) nDelay = 1;
68058 }
68059#endif
68060 sqlite3OsSleep(pWal->pVfs, nDelay);
68061 *pCnt &= ~WAL_RETRY_BLOCKED_MASK0;
68062 }
68063
68064 if( !useWal ){
68065 assert( rc==SQLITE_OK )((void) (0));
68066 if( pWal->bShmUnreliable==0 ){
68067 rc = walIndexReadHdr(pWal, pChanged);
68068 }
68069#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68070 walDisableBlocking(pWal);
68071 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
68072 rc = SQLITE_BUSY5;
68073 *pCnt |= WAL_RETRY_BLOCKED_MASK0;
68074 }
68075#endif
68076 if( rc==SQLITE_BUSY5 ){
68077 /* If there is not a recovery running in another thread or process
68078 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
68079 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
68080 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
68081 ** would be technically correct. But the race is benign since with
68082 ** WAL_RETRY this routine will be called again and will probably be
68083 ** right on the second iteration.
68084 */
68085 if( pWal->apWiData[0]==0 ){
68086 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
68087 ** We assume this is a transient condition, so return WAL_RETRY. The
68088 ** xShmMap() implementation used by the default unix and win32 VFS
68089 ** modules may return SQLITE_BUSY due to a race condition in the
68090 ** code that determines whether or not the shared-memory region
68091 ** must be zeroed before the requested page is returned.
68092 */
68093 rc = WAL_RETRY(-1);
68094 }else if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_RECOVER_LOCK2)) ){
68095 walUnlockShared(pWal, WAL_RECOVER_LOCK2);
68096 rc = WAL_RETRY(-1);
68097 }else if( rc==SQLITE_BUSY5 ){
68098 rc = SQLITE_BUSY_RECOVERY(5 | (1<<8));
68099 }
68100 }
68101 if( rc!=SQLITE_OK0 ){
68102 return rc;
68103 }
68104 else if( pWal->bShmUnreliable ){
68105 return walBeginShmUnreliable(pWal, pChanged);
68106 }
68107 }
68108
68109 assert( pWal->nWiData>0 )((void) (0));
68110 assert( pWal->apWiData[0]!=0 )((void) (0));
68111 pInfo = walCkptInfo(pWal);
68112 SEH_INJECT_FAULT((void) (0));;
68113 if( !useWal && AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0)==pWal->hdr.mxFrame
68114#ifdef SQLITE_ENABLE_SNAPSHOT1
68115 && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
68116#endif
68117 ){
68118 /* The WAL has been completely backfilled (or it is empty).
68119 ** and can be safely ignored.
68120 */
68121 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
68122 walShmBarrier(pWal);
68123 if( rc==SQLITE_OK0 ){
68124 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
68125 /* It is not safe to allow the reader to continue here if frames
68126 ** may have been appended to the log before READ_LOCK(0) was obtained.
68127 ** When holding READ_LOCK(0), the reader ignores the entire log file,
68128 ** which implies that the database file contains a trustworthy
68129 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
68130 ** happening, this is usually correct.
68131 **
68132 ** However, if frames have been appended to the log (or if the log
68133 ** is wrapped and written for that matter) before the READ_LOCK(0)
68134 ** is obtained, that is not necessarily true. A checkpointer may
68135 ** have started to backfill the appended frames but crashed before
68136 ** it finished. Leaving a corrupt image in the database file.
68137 */
68138 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
68139 return WAL_RETRY(-1);
68140 }
68141 pWal->readLock = 0;
68142 return SQLITE_OK0;
68143 }else if( rc!=SQLITE_BUSY5 ){
68144 return rc;
68145 }
68146 }
68147
68148 /* If we get this far, it means that the reader will want to use
68149 ** the WAL to get at content from recent commits. The job now is
68150 ** to select one of the aReadMark[] entries that is closest to
68151 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
68152 */
68153 mxReadMark = 0;
68154 mxI = 0;
68155 mxFrame = pWal->hdr.mxFrame;
68156#ifdef SQLITE_ENABLE_SNAPSHOT1
68157 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
68158 mxFrame = pWal->pSnapshot->mxFrame;
68159 }
68160#endif
68161 for(i=1; i<WAL_NREADER(8 -3); i++){
68162 u32 thisMark = AtomicLoad(pInfo->aReadMark+i)__atomic_load_n((pInfo->aReadMark+i),0); SEH_INJECT_FAULT((void) (0));;
68163 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
68164 assert( thisMark!=READMARK_NOT_USED )((void) (0));
68165 mxReadMark = thisMark;
68166 mxI = i;
68167 }
68168 }
68169 if( (pWal->readOnly & WAL_SHM_RDONLY2)==0
68170 && (mxReadMark<mxFrame || mxI==0)
68171 ){
68172 for(i=1; i<WAL_NREADER(8 -3); i++){
68173 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
68174 if( rc==SQLITE_OK0 ){
68175 AtomicStore(pInfo->aReadMark+i,mxFrame)__atomic_store_n((pInfo->aReadMark+i),(mxFrame),0);
68176 mxReadMark = mxFrame;
68177 mxI = i;
68178 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
68179 break;
68180 }else if( rc!=SQLITE_BUSY5 ){
68181 return rc;
68182 }
68183 }
68184 }
68185 if( mxI==0 ){
68186 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 )((void) (0));
68187 return rc==SQLITE_BUSY5 ? WAL_RETRY(-1) : SQLITE_READONLY_CANTINIT(8 | (5<<8));
68188 }
68189
68190 (void)walEnableBlockingMs(pWal, nBlockTmout)0;
68191 rc = walLockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
68192 walDisableBlocking(pWal);
68193 if( rc ){
68194#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68195 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ){
68196 *pCnt |= WAL_RETRY_BLOCKED_MASK0;
68197 }
68198#else
68199 assert( rc!=SQLITE_BUSY_TIMEOUT )((void) (0));
68200#endif
68201 assert( (rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT )((void) (0));
68202 return (rc&0xFF)==SQLITE_BUSY5 ? WAL_RETRY(-1) : rc;
68203 }
68204 /* Now that the read-lock has been obtained, check that neither the
68205 ** value in the aReadMark[] array or the contents of the wal-index
68206 ** header have changed.
68207 **
68208 ** It is necessary to check that the wal-index header did not change
68209 ** between the time it was read and when the shared-lock was obtained
68210 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
68211 ** that the log file may have been wrapped by a writer, or that frames
68212 ** that occur later in the log than pWal->hdr.mxFrame may have been
68213 ** copied into the database by a checkpointer. If either of these things
68214 ** happened, then reading the database with the current value of
68215 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
68216 ** instead.
68217 **
68218 ** Before checking that the live wal-index header has not changed
68219 ** since it was read, set Wal.minFrame to the first frame in the wal
68220 ** file that has not yet been checkpointed. This client will not need
68221 ** to read any frames earlier than minFrame from the wal file - they
68222 ** can be safely read directly from the database file.
68223 **
68224 ** Because a ShmBarrier() call is made between taking the copy of
68225 ** nBackfill and checking that the wal-header in shared-memory still
68226 ** matches the one cached in pWal->hdr, it is guaranteed that the
68227 ** checkpointer that set nBackfill was not working with a wal-index
68228 ** header newer than that cached in pWal->hdr. If it were, that could
68229 ** cause a problem. The checkpointer could omit to checkpoint
68230 ** a version of page X that lies before pWal->minFrame (call that version
68231 ** A) on the basis that there is a newer version (version B) of the same
68232 ** page later in the wal file. But if version B happens to like past
68233 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
68234 ** that it can read version A from the database file. However, since
68235 ** we can guarantee that the checkpointer that set nBackfill could not
68236 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
68237 */
68238 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0)+1; SEH_INJECT_FAULT((void) (0));;
68239 walShmBarrier(pWal);
68240 if( AtomicLoad(pInfo->aReadMark+mxI)__atomic_load_n((pInfo->aReadMark+mxI),0)!=mxReadMark
68241 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
68242 ){
68243 walUnlockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
68244 return WAL_RETRY(-1);
68245 }else{
68246 assert( mxReadMark<=pWal->hdr.mxFrame )((void) (0));
68247 pWal->readLock = (i16)mxI;
68248 }
68249 return rc;
68250}
68251
68252#ifdef SQLITE_ENABLE_SNAPSHOT1
68253/*
68254** This function does the work of sqlite3WalSnapshotRecover().
68255*/
68256static int walSnapshotRecover(
68257 Wal *pWal, /* WAL handle */
68258 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
68259 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
68260){
68261 int szPage = (int)pWal->szPage;
68262 int rc;
68263 i64 szDb; /* Size of db file in bytes */
68264
68265 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
68266 if( rc==SQLITE_OK0 ){
68267 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
68268 u32 i = pInfo->nBackfillAttempted;
68269 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill)__atomic_load_n((&pInfo->nBackfill),0); i--){
68270 WalHashLoc sLoc; /* Hash table location */
68271 u32 pgno; /* Page number in db file */
68272 i64 iDbOff; /* Offset of db file entry */
68273 i64 iWalOff; /* Offset of wal file entry */
68274
68275 rc = walHashGet(pWal, walFramePage(i), &sLoc);
68276 if( rc!=SQLITE_OK0 ) break;
68277 assert( i - sLoc.iZero - 1 >=0 )((void) (0));
68278 pgno = sLoc.aPgno[i-sLoc.iZero-1];
68279 iDbOff = (i64)(pgno-1) * szPage;
68280
68281 if( iDbOff+szPage<=szDb ){
68282 iWalOff = walFrameOffset(i, szPage)( 32 + ((i)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
68283 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
68284
68285 if( rc==SQLITE_OK0 ){
68286 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
68287 }
68288
68289 if( rc!=SQLITE_OK0 || 0==memcmp(pBuf1, pBuf2, szPage) ){
68290 break;
68291 }
68292 }
68293
68294 pInfo->nBackfillAttempted = i-1;
68295 }
68296 }
68297
68298 return rc;
68299}
68300
68301/*
68302** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
68303** variable so that older snapshots can be accessed. To do this, loop
68304** through all wal frames from nBackfillAttempted to (nBackfill+1),
68305** comparing their content to the corresponding page with the database
68306** file, if any. Set nBackfillAttempted to the frame number of the
68307** first frame for which the wal file content matches the db file.
68308**
68309** This is only really safe if the file-system is such that any page
68310** writes made by earlier checkpointers were atomic operations, which
68311** is not always true. It is also possible that nBackfillAttempted
68312** may be left set to a value larger than expected, if a wal frame
68313** contains content that duplicate of an earlier version of the same
68314** page.
68315**
68316** SQLITE_OK is returned if successful, or an SQLite error code if an
68317** error occurs. It is not an error if nBackfillAttempted cannot be
68318** decreased at all.
68319*/
68320SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal){
68321 int rc;
68322
68323 assert( pWal->readLock>=0 )((void) (0));
68324 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
68325 if( rc==SQLITE_OK0 ){
68326 void *pBuf1 = sqlite3_malloc(pWal->szPage);
68327 void *pBuf2 = sqlite3_malloc(pWal->szPage);
68328 if( pBuf1==0 || pBuf2==0 ){
68329 rc = SQLITE_NOMEM7;
68330 }else{
68331 pWal->ckptLock = 1;
68332 SEH_TRY; {
68333 rc = walSnapshotRecover(pWal, pBuf1, pBuf2);
68334 }
68335 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
68336 pWal->ckptLock = 0;
68337 }
68338
68339 sqlite3_free(pBuf1);
68340 sqlite3_free(pBuf2);
68341 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
68342 }
68343
68344 return rc;
68345}
68346#endif /* SQLITE_ENABLE_SNAPSHOT */
68347
68348/*
68349** This function does the work of sqlite3WalBeginReadTransaction() (see
68350** below). That function simply calls this one inside an SEH_TRY{...} block.
68351*/
68352static int walBeginReadTransaction(Wal *pWal, int *pChanged){
68353 int rc; /* Return code */
68354 int cnt = 0; /* Number of TryBeginRead attempts */
68355#ifdef SQLITE_ENABLE_SNAPSHOT1
68356 int ckptLock = 0;
68357 int bChanged = 0;
68358 WalIndexHdr *pSnapshot = pWal->pSnapshot;
68359#endif
68360
68361 assert( pWal->ckptLock==0 )((void) (0));
68362 assert( pWal->nSehTry>0 )((void) (0));
68363
68364#ifdef SQLITE_ENABLE_SNAPSHOT1
68365 if( pSnapshot ){
68366 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
68367 bChanged = 1;
68368 }
68369
68370 /* It is possible that there is a checkpointer thread running
68371 ** concurrent with this code. If this is the case, it may be that the
68372 ** checkpointer has already determined that it will checkpoint
68373 ** snapshot X, where X is later in the wal file than pSnapshot, but
68374 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
68375 ** its intent. To avoid the race condition this leads to, ensure that
68376 ** there is no checkpointer process by taking a shared CKPT lock
68377 ** before checking pInfo->nBackfillAttempted. */
68378 (void)walEnableBlocking(pWal)0;
68379 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
68380 walDisableBlocking(pWal);
68381
68382 if( rc!=SQLITE_OK0 ){
68383 return rc;
68384 }
68385 ckptLock = 1;
68386 }
68387#endif
68388
68389 do{
68390 rc = walTryBeginRead(pWal, pChanged, 0, &cnt);
68391 }while( rc==WAL_RETRY(-1) );
68392 testcase( (rc&0xff)==SQLITE_BUSY );
68393 testcase( (rc&0xff)==SQLITE_IOERR );
68394 testcase( rc==SQLITE_PROTOCOL );
68395 testcase( rc==SQLITE_OK );
68396
68397#ifdef SQLITE_ENABLE_SNAPSHOT1
68398 if( rc==SQLITE_OK0 ){
68399 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
68400 /* At this point the client has a lock on an aReadMark[] slot holding
68401 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
68402 ** is populated with the wal-index header corresponding to the head
68403 ** of the wal file. Verify that pSnapshot is still valid before
68404 ** continuing. Reasons why pSnapshot might no longer be valid:
68405 **
68406 ** (1) The WAL file has been reset since the snapshot was taken.
68407 ** In this case, the salt will have changed.
68408 **
68409 ** (2) A checkpoint as been attempted that wrote frames past
68410 ** pSnapshot->mxFrame into the database file. Note that the
68411 ** checkpoint need not have completed for this to cause problems.
68412 */
68413 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
68414
68415 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 )((void) (0));
68416 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame )((void) (0));
68417
68418 /* Check that the wal file has not been wrapped. Assuming that it has
68419 ** not, also check that no checkpointer has attempted to checkpoint any
68420 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
68421 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
68422 ** with *pSnapshot and set *pChanged as appropriate for opening the
68423 ** snapshot. */
68424 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
68425 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
68426 ){
68427 assert( pWal->readLock>0 )((void) (0));
68428 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
68429 *pChanged = bChanged;
68430 }else{
68431 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
68432 }
68433
68434 /* A client using a non-current snapshot may not ignore any frames
68435 ** from the start of the wal file. This is because, for a system
68436 ** where (minFrame < iSnapshot < maxFrame), a checkpointer may
68437 ** have omitted to checkpoint a frame earlier than minFrame in
68438 ** the file because there exists a frame after iSnapshot that
68439 ** is the same database page. */
68440 pWal->minFrame = 1;
68441
68442 if( rc!=SQLITE_OK0 ){
68443 sqlite3WalEndReadTransaction(pWal);
68444 }
68445 }
68446 }
68447
68448 /* Release the shared CKPT lock obtained above. */
68449 if( ckptLock ){
68450 assert( pSnapshot )((void) (0));
68451 walUnlockShared(pWal, WAL_CKPT_LOCK1);
68452 }
68453#endif
68454 return rc;
68455}
68456
68457/*
68458** Begin a read transaction on the database.
68459**
68460** This routine used to be called sqlite3OpenSnapshot() and with good reason:
68461** it takes a snapshot of the state of the WAL and wal-index for the current
68462** instant in time. The current thread will continue to use this snapshot.
68463** Other threads might append new content to the WAL and wal-index but
68464** that extra content is ignored by the current thread.
68465**
68466** If the database contents have changes since the previous read
68467** transaction, then *pChanged is set to 1 before returning. The
68468** Pager layer will use this to know that its cache is stale and
68469** needs to be flushed.
68470*/
68471SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
68472 int rc;
68473 SEH_TRY; {
68474 rc = walBeginReadTransaction(pWal, pChanged);
68475 }
68476 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
68477 return rc;
68478}
68479
68480/*
68481** Finish with a read transaction. All this does is release the
68482** read-lock.
68483*/
68484SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal){
68485 sqlite3WalEndWriteTransaction(pWal);
68486 if( pWal->readLock>=0 ){
68487 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
68488 pWal->readLock = -1;
68489 }
68490}
68491
68492/*
68493** Search the wal file for page pgno. If found, set *piRead to the frame that
68494** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
68495** to zero.
68496**
68497** Return SQLITE_OK if successful, or an error code if an error occurs. If an
68498** error does occur, the final value of *piRead is undefined.
68499*/
68500static int walFindFrame(
68501 Wal *pWal, /* WAL handle */
68502 Pgno pgno, /* Database page number to read data for */
68503 u32 *piRead /* OUT: Frame number (or zero) */
68504){
68505 u32 iRead = 0; /* If !=0, WAL frame to return data from */
68506 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
68507 int iHash; /* Used to loop through N hash tables */
68508 int iMinHash;
68509
68510 /* This routine is only be called from within a read transaction. */
68511 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
68512
68513 /* If the "last page" field of the wal-index header snapshot is 0, then
68514 ** no data will be read from the wal under any circumstances. Return early
68515 ** in this case as an optimization. Likewise, if pWal->readLock==0,
68516 ** then the WAL is ignored by the reader so return early, as if the
68517 ** WAL were empty.
68518 */
68519 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
68520 *piRead = 0;
68521 return SQLITE_OK0;
68522 }
68523
68524 /* Search the hash table or tables for an entry matching page number
68525 ** pgno. Each iteration of the following for() loop searches one
68526 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
68527 **
68528 ** This code might run concurrently to the code in walIndexAppend()
68529 ** that adds entries to the wal-index (and possibly to this hash
68530 ** table). This means the value just read from the hash
68531 ** slot (aHash[iKey]) may have been added before or after the
68532 ** current read transaction was opened. Values added after the
68533 ** read transaction was opened may have been written incorrectly -
68534 ** i.e. these slots may contain garbage data. However, we assume
68535 ** that any slots written before the current read transaction was
68536 ** opened remain unmodified.
68537 **
68538 ** For the reasons above, the if(...) condition featured in the inner
68539 ** loop of the following block is more stringent that would be required
68540 ** if we had exclusive access to the hash-table:
68541 **
68542 ** (aPgno[iFrame]==pgno):
68543 ** This condition filters out normal hash-table collisions.
68544 **
68545 ** (iFrame<=iLast):
68546 ** This condition filters out entries that were added to the hash
68547 ** table after the current read-transaction had started.
68548 */
68549 iMinHash = walFramePage(pWal->minFrame);
68550 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
68551 WalHashLoc sLoc; /* Hash table location */
68552 int iKey; /* Hash slot index */
68553 int nCollide; /* Number of hash collisions remaining */
68554 int rc; /* Error code */
68555 u32 iH;
68556
68557 rc = walHashGet(pWal, iHash, &sLoc);
68558 if( rc!=SQLITE_OK0 ){
68559 return rc;
68560 }
68561 nCollide = HASHTABLE_NSLOT(4096*2);
68562 iKey = walHash(pgno);
68563 SEH_INJECT_FAULT((void) (0));;
68564 while( (iH = AtomicLoad(&sLoc.aHash[iKey])__atomic_load_n((&sLoc.aHash[iKey]),0))!=0 ){
68565 u32 iFrame = iH + sLoc.iZero;
68566 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
68567 assert( iFrame>iRead || CORRUPT_DB )((void) (0));
68568 iRead = iFrame;
68569 }
68570 if( (nCollide--)==0 ){
68571 *piRead = 0;
68572 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(68572);
68573 }
68574 iKey = walNextHash(iKey);
68575 }
68576 if( iRead ) break;
68577 }
68578
68579#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
68580 /* If expensive assert() statements are available, do a linear search
68581 ** of the wal-index file content. Make sure the results agree with the
68582 ** result obtained using the hash indexes above. */
68583 {
68584 u32 iRead2 = 0;
68585 u32 iTest;
68586 assert( pWal->bShmUnreliable || pWal->minFrame>0 )((void) (0));
68587 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
68588 if( walFramePgno(pWal, iTest)==pgno ){
68589 iRead2 = iTest;
68590 break;
68591 }
68592 }
68593 assert( iRead==iRead2 )((void) (0));
68594 }
68595#endif
68596
68597 *piRead = iRead;
68598 return SQLITE_OK0;
68599}
68600
68601/*
68602** Search the wal file for page pgno. If found, set *piRead to the frame that
68603** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
68604** to zero.
68605**
68606** Return SQLITE_OK if successful, or an error code if an error occurs. If an
68607** error does occur, the final value of *piRead is undefined.
68608**
68609** The difference between this function and walFindFrame() is that this
68610** function wraps walFindFrame() in an SEH_TRY{...} block.
68611*/
68612SQLITE_PRIVATEstatic int sqlite3WalFindFrame(
68613 Wal *pWal, /* WAL handle */
68614 Pgno pgno, /* Database page number to read data for */
68615 u32 *piRead /* OUT: Frame number (or zero) */
68616){
68617 int rc;
68618 SEH_TRY; {
68619 rc = walFindFrame(pWal, pgno, piRead);
68620 }
68621 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
68622 return rc;
68623}
68624
68625/*
68626** Read the contents of frame iRead from the wal file into buffer pOut
68627** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
68628** error code otherwise.
68629*/
68630SQLITE_PRIVATEstatic int sqlite3WalReadFrame(
68631 Wal *pWal, /* WAL handle */
68632 u32 iRead, /* Frame to read */
68633 int nOut, /* Size of buffer pOut in bytes */
68634 u8 *pOut /* Buffer to write page data to */
68635){
68636 int sz;
68637 i64 iOffset;
68638 sz = pWal->hdr.szPage;
68639 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
68640 testcase( sz<=32768 );
68641 testcase( sz>=65536 );
68642 iOffset = walFrameOffset(iRead, sz)( 32 + ((iRead)-1)*(i64)((sz)+24) ) + WAL_FRAME_HDRSIZE24;
68643 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
68644 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
68645}
68646
68647/*
68648** Return the size of the database in pages (or zero, if unknown).
68649*/
68650SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal){
68651 if( pWal && ALWAYS(pWal->readLock>=0)(pWal->readLock>=0) ){
68652 return pWal->hdr.nPage;
68653 }
68654 return 0;
68655}
68656
68657
68658/*
68659** This function starts a write transaction on the WAL.
68660**
68661** A read transaction must have already been started by a prior call
68662** to sqlite3WalBeginReadTransaction().
68663**
68664** If another thread or process has written into the database since
68665** the read transaction was started, then it is not possible for this
68666** thread to write as doing so would cause a fork. So this routine
68667** returns SQLITE_BUSY in that case and no write transaction is started.
68668**
68669** There can only be a single writer active at a time.
68670*/
68671SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal){
68672 int rc;
68673
68674#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68675 /* If the write-lock is already held, then it was obtained before the
68676 ** read-transaction was even opened, making this call a no-op.
68677 ** Return early. */
68678 if( pWal->writeLock ){
68679 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) )((void) (0));
68680 return SQLITE_OK0;
68681 }
68682#endif
68683
68684 /* Cannot start a write transaction without first holding a read
68685 ** transaction. */
68686 assert( pWal->readLock>=0 )((void) (0));
68687 assert( pWal->writeLock==0 && pWal->iReCksum==0 )((void) (0));
68688
68689 if( pWal->readOnly ){
68690 return SQLITE_READONLY8;
68691 }
68692
68693 /* Only one writer allowed at a time. Get the write lock. Return
68694 ** SQLITE_BUSY if unable.
68695 */
68696 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
68697 if( rc ){
68698 return rc;
68699 }
68700 pWal->writeLock = 1;
68701
68702 /* If another connection has written to the database file since the
68703 ** time the read transaction on this connection was started, then
68704 ** the write is disallowed.
68705 */
68706 SEH_TRY; {
68707 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
68708 rc = SQLITE_BUSY_SNAPSHOT(5 | (2<<8));
68709 }
68710 }
68711 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
68712
68713 if( rc!=SQLITE_OK0 ){
68714 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
68715 pWal->writeLock = 0;
68716 }
68717 return rc;
68718}
68719
68720/*
68721** End a write transaction. The commit has already been done. This
68722** routine merely releases the lock.
68723*/
68724SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal){
68725 if( pWal->writeLock ){
68726 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
68727 pWal->writeLock = 0;
68728 pWal->iReCksum = 0;
68729 pWal->truncateOnCommit = 0;
68730 }
68731 return SQLITE_OK0;
68732}
68733
68734/*
68735** If any data has been written (but not committed) to the log file, this
68736** function moves the write-pointer back to the start of the transaction.
68737**
68738** Additionally, the callback function is invoked for each frame written
68739** to the WAL since the start of the transaction. If the callback returns
68740** other than SQLITE_OK, it is not invoked again and the error code is
68741** returned to the caller.
68742**
68743** Otherwise, if the callback function does not return an error, this
68744** function returns SQLITE_OK.
68745*/
68746SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
68747 int rc = SQLITE_OK0;
68748 if( ALWAYS(pWal->writeLock)(pWal->writeLock) ){
68749 Pgno iMax = pWal->hdr.mxFrame;
68750 Pgno iFrame;
68751
68752 SEH_TRY; {
68753 /* Restore the clients cache of the wal-index header to the state it
68754 ** was in before the client began writing to the database.
68755 */
68756 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
68757
68758 for(iFrame=pWal->hdr.mxFrame+1;
68759 ALWAYS(rc==SQLITE_OK)(rc==0) && iFrame<=iMax;
68760 iFrame++
68761 ){
68762 /* This call cannot fail. Unless the page for which the page number
68763 ** is passed as the second argument is (a) in the cache and
68764 ** (b) has an outstanding reference, then xUndo is either a no-op
68765 ** (if (a) is false) or simply expels the page from the cache (if (b)
68766 ** is false).
68767 **
68768 ** If the upper layer is doing a rollback, it is guaranteed that there
68769 ** are no outstanding references to any page other than page 1. And
68770 ** page 1 is never written to the log until the transaction is
68771 ** committed. As a result, the call to xUndo may not fail.
68772 */
68773 assert( walFramePgno(pWal, iFrame)!=1 )((void) (0));
68774 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
68775 }
68776 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
68777 }
68778 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
68779 }
68780 return rc;
68781}
68782
68783/*
68784** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
68785** values. This function populates the array with values required to
68786** "rollback" the write position of the WAL handle back to the current
68787** point in the event of a savepoint rollback (via WalSavepointUndo()).
68788*/
68789SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
68790 assert( pWal->writeLock )((void) (0));
68791 aWalData[0] = pWal->hdr.mxFrame;
68792 aWalData[1] = pWal->hdr.aFrameCksum[0];
68793 aWalData[2] = pWal->hdr.aFrameCksum[1];
68794 aWalData[3] = pWal->nCkpt;
68795}
68796
68797/*
68798** Move the write position of the WAL back to the point identified by
68799** the values in the aWalData[] array. aWalData must point to an array
68800** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
68801** by a call to WalSavepoint().
68802*/
68803SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
68804 int rc = SQLITE_OK0;
68805
68806 assert( pWal->writeLock )((void) (0));
68807 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame )((void) (0));
68808
68809 if( aWalData[3]!=pWal->nCkpt ){
68810 /* This savepoint was opened immediately after the write-transaction
68811 ** was started. Right after that, the writer decided to wrap around
68812 ** to the start of the log. Update the savepoint values to match.
68813 */
68814 aWalData[0] = 0;
68815 aWalData[3] = pWal->nCkpt;
68816 }
68817
68818 if( aWalData[0]<pWal->hdr.mxFrame ){
68819 pWal->hdr.mxFrame = aWalData[0];
68820 pWal->hdr.aFrameCksum[0] = aWalData[1];
68821 pWal->hdr.aFrameCksum[1] = aWalData[2];
68822 SEH_TRY; {
68823 walCleanupHash(pWal);
68824 }
68825 SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ); ((void) (0));
68826 }
68827
68828 return rc;
68829}
68830
68831/*
68832** This function is called just before writing a set of frames to the log
68833** file (see sqlite3WalFrames()). It checks to see if, instead of appending
68834** to the current log file, it is possible to overwrite the start of the
68835** existing log file with the new frames (i.e. "reset" the log). If so,
68836** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
68837** unchanged.
68838**
68839** SQLITE_OK is returned if no error is encountered (regardless of whether
68840** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
68841** if an error occurs.
68842*/
68843static int walRestartLog(Wal *pWal){
68844 int rc = SQLITE_OK0;
68845 int cnt;
68846
68847 if( pWal->readLock==0 ){
68848 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
68849 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
68850 if( pInfo->nBackfill>0 ){
68851 u32 salt1;
68852 sqlite3_randomness(4, &salt1);
68853 rc = walLockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
68854 if( rc==SQLITE_OK0 ){
68855 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
68856 ** readers are currently using the WAL), then the transactions
68857 ** frames will overwrite the start of the existing log. Update the
68858 ** wal-index header to reflect this.
68859 **
68860 ** In theory it would be Ok to update the cache of the header only
68861 ** at this point. But updating the actual wal-index header is also
68862 ** safe and means there is no special case for sqlite3WalUndo()
68863 ** to handle if this transaction is rolled back. */
68864 walRestartHdr(pWal, salt1);
68865 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
68866 }else if( rc!=SQLITE_BUSY5 ){
68867 return rc;
68868 }
68869 }
68870 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
68871 pWal->readLock = -1;
68872 cnt = 0;
68873 do{
68874 int notUsed;
68875 rc = walTryBeginRead(pWal, &notUsed, 1, &cnt);
68876 }while( rc==WAL_RETRY(-1) );
68877 assert( (rc&0xff)!=SQLITE_BUSY )((void) (0)); /* BUSY not possible when useWal==1 */
68878 testcase( (rc&0xff)==SQLITE_IOERR );
68879 testcase( rc==SQLITE_PROTOCOL );
68880 testcase( rc==SQLITE_OK );
68881 }
68882 return rc;
68883}
68884
68885/*
68886** Information about the current state of the WAL file and where
68887** the next fsync should occur - passed from sqlite3WalFrames() into
68888** walWriteToLog().
68889*/
68890typedef struct WalWriter {
68891 Wal *pWal; /* The complete WAL information */
68892 sqlite3_file *pFd; /* The WAL file to which we write */
68893 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
68894 int syncFlags; /* Flags for the fsync */
68895 int szPage; /* Size of one page */
68896} WalWriter;
68897
68898/*
68899** Write iAmt bytes of content into the WAL file beginning at iOffset.
68900** Do a sync when crossing the p->iSyncPoint boundary.
68901**
68902** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
68903** first write the part before iSyncPoint, then sync, then write the
68904** rest.
68905*/
68906static int walWriteToLog(
68907 WalWriter *p, /* WAL to write to */
68908 void *pContent, /* Content to be written */
68909 int iAmt, /* Number of bytes to write */
68910 sqlite3_int64 iOffset /* Start writing at this offset */
68911){
68912 int rc;
68913 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
68914 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
68915 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
68916 if( rc ) return rc;
68917 iOffset += iFirstAmt;
68918 iAmt -= iFirstAmt;
68919 pContent = (void*)(iFirstAmt + (char*)pContent);
68920 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 )((void) (0));
68921 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags)((p->syncFlags)&0x03));
68922 if( iAmt==0 || rc ) return rc;
68923 }
68924 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
68925 return rc;
68926}
68927
68928/*
68929** Write out a single frame of the WAL
68930*/
68931static int walWriteOneFrame(
68932 WalWriter *p, /* Where to write the frame */
68933 PgHdr *pPage, /* The page of the frame to be written */
68934 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
68935 sqlite3_int64 iOffset /* Byte offset at which to write */
68936){
68937 int rc; /* Result code from subfunctions */
68938 void *pData; /* Data actually written */
68939 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-header in */
68940 pData = pPage->pData;
68941 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
68942 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
68943 if( rc ) return rc;
68944 /* Write the page data */
68945 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
68946 return rc;
68947}
68948
68949/*
68950** This function is called as part of committing a transaction within which
68951** one or more frames have been overwritten. It updates the checksums for
68952** all frames written to the wal file by the current transaction starting
68953** with the earliest to have been overwritten.
68954**
68955** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
68956*/
68957static int walRewriteChecksums(Wal *pWal, u32 iLast){
68958 const int szPage = pWal->szPage;/* Database page size */
68959 int rc = SQLITE_OK0; /* Return code */
68960 u8 *aBuf; /* Buffer to load data from wal file into */
68961 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-headers in */
68962 u32 iRead; /* Next frame to read from wal file */
68963 i64 iCksumOff;
68964
68965 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE24);
68966 if( aBuf==0 ) return SQLITE_NOMEM_BKPT7;
68967
68968 /* Find the checksum values to use as input for the recalculating the
68969 ** first checksum. If the first frame is frame 1 (implying that the current
68970 ** transaction restarted the wal file), these values must be read from the
68971 ** wal-file header. Otherwise, read them from the frame header of the
68972 ** previous frame. */
68973 assert( pWal->iReCksum>0 )((void) (0));
68974 if( pWal->iReCksum==1 ){
68975 iCksumOff = 24;
68976 }else{
68977 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage)( 32 + ((pWal->iReCksum-1)-1)*(i64)((szPage)+24) ) + 16;
68978 }
68979 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
68980 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
68981 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
68982
68983 iRead = pWal->iReCksum;
68984 pWal->iReCksum = 0;
68985 for(; rc==SQLITE_OK0 && iRead<=iLast; iRead++){
68986 i64 iOff = walFrameOffset(iRead, szPage)( 32 + ((iRead)-1)*(i64)((szPage)+24) );
68987 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE24, iOff);
68988 if( rc==SQLITE_OK0 ){
68989 u32 iPgno, nDbSize;
68990 iPgno = sqlite3Get4byte(aBuf);
68991 nDbSize = sqlite3Get4byte(&aBuf[4]);
68992
68993 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE24], aFrame);
68994 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
68995 }
68996 }
68997
68998 sqlite3_free(aBuf);
68999 return rc;
69000}
69001
69002/*
69003** Write a set of frames to the log. The caller must hold the write-lock
69004** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
69005*/
69006static int walFrames(
69007 Wal *pWal, /* Wal handle to write to */
69008 int szPage, /* Database page-size in bytes */
69009 PgHdr *pList, /* List of dirty pages to write */
69010 Pgno nTruncate, /* Database size after this commit */
69011 int isCommit, /* True if this is a commit */
69012 int sync_flags /* Flags to pass to OsSync() (or 0) */
69013){
69014 int rc; /* Used to catch return codes */
69015 u32 iFrame; /* Next frame address */
69016 PgHdr *p; /* Iterator to run through pList with. */
69017 PgHdr *pLast = 0; /* Last frame in list */
69018 int nExtra = 0; /* Number of extra copies of last page */
69019 int szFrame; /* The size of a single frame */
69020 i64 iOffset; /* Next byte to write in WAL file */
69021 WalWriter w; /* The writer */
69022 u32 iFirst = 0; /* First frame that may be overwritten */
69023 WalIndexHdr *pLive; /* Pointer to shared header */
69024
69025 assert( pList )((void) (0));
69026 assert( pWal->writeLock )((void) (0));
69027
69028 /* If this frame set completes a transaction, then nTruncate>0. If
69029 ** nTruncate==0 then this frame set does not complete the transaction. */
69030 assert( (isCommit!=0)==(nTruncate!=0) )((void) (0));
69031
69032#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
69033 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
69034 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
69035 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
69036 }
69037#endif
69038
69039 pLive = (WalIndexHdr*)walIndexHdr(pWal);
69040 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
69041 iFirst = pLive->mxFrame+1;
69042 }
69043
69044 /* See if it is possible to write these frames into the start of the
69045 ** log file, instead of appending to it at pWal->hdr.mxFrame.
69046 */
69047 if( SQLITE_OK0!=(rc = walRestartLog(pWal)) ){
69048 return rc;
69049 }
69050
69051 /* If this is the first frame written into the log, write the WAL
69052 ** header to the start of the WAL file. See comments at the top of
69053 ** this source file for a description of the WAL header format.
69054 */
69055 iFrame = pWal->hdr.mxFrame;
69056 if( iFrame==0 ){
69057 u8 aWalHdr[WAL_HDRSIZE32]; /* Buffer to assemble wal-header in */
69058 u32 aCksum[2]; /* Checksum for wal-header */
69059
69060 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC0x377f0682 | SQLITE_BIGENDIAN0));
69061 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION3007000);
69062 sqlite3Put4byte(&aWalHdr[8], szPage);
69063 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
69064 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
69065 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
69066 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE32-2*4, 0, aCksum);
69067 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
69068 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
69069
69070 pWal->szPage = szPage;
69071 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN0;
69072 pWal->hdr.aFrameCksum[0] = aCksum[0];
69073 pWal->hdr.aFrameCksum[1] = aCksum[1];
69074 pWal->truncateOnCommit = 1;
69075
69076 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
69077 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
69078 if( rc!=SQLITE_OK0 ){
69079 return rc;
69080 }
69081
69082 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
69083 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
69084 ** an out-of-order write following a WAL restart could result in
69085 ** database corruption. See the ticket:
69086 **
69087 ** https://sqlite.org/src/info/ff5be73dee
69088 */
69089 if( pWal->syncHeader ){
69090 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
69091 if( rc ) return rc;
69092 }
69093 }
69094 if( (int)pWal->szPage!=szPage ){
69095 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69095); /* TH3 test case: cov1/corrupt155.test */
69096 }
69097
69098 /* Setup information needed to write frames into the WAL */
69099 w.pWal = pWal;
69100 w.pFd = pWal->pWalFd;
69101 w.iSyncPoint = 0;
69102 w.syncFlags = sync_flags;
69103 w.szPage = szPage;
69104 iOffset = walFrameOffset(iFrame+1, szPage)( 32 + ((iFrame+1)-1)*(i64)((szPage)+24) );
69105 szFrame = szPage + WAL_FRAME_HDRSIZE24;
69106
69107 /* Write all frames into the log file exactly once */
69108 for(p=pList; p; p=p->pDirty){
69109 int nDbSize; /* 0 normally. Positive == commit flag */
69110
69111 /* Check if this page has already been written into the wal file by
69112 ** the current transaction. If so, overwrite the existing frame and
69113 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
69114 ** checksums must be recomputed when the transaction is committed. */
69115 if( iFirst && (p->pDirty || isCommit==0) ){
69116 u32 iWrite = 0;
69117 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
69118 assert( rc==SQLITE_OK || iWrite==0 )((void) (0));
69119 if( iWrite>=iFirst ){
69120 i64 iOff = walFrameOffset(iWrite, szPage)( 32 + ((iWrite)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
69121 void *pData;
69122 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
69123 pWal->iReCksum = iWrite;
69124 }
69125 pData = p->pData;
69126 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
69127 if( rc ) return rc;
69128 p->flags &= ~PGHDR_WAL_APPEND0x040;
69129 continue;
69130 }
69131 }
69132
69133 iFrame++;
69134 assert( iOffset==walFrameOffset(iFrame, szPage) )((void) (0));
69135 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
69136 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
69137 if( rc ) return rc;
69138 pLast = p;
69139 iOffset += szFrame;
69140 p->flags |= PGHDR_WAL_APPEND0x040;
69141 }
69142
69143 /* Recalculate checksums within the wal file if required. */
69144 if( isCommit && pWal->iReCksum ){
69145 rc = walRewriteChecksums(pWal, iFrame);
69146 if( rc ) return rc;
69147 }
69148
69149 /* If this is the end of a transaction, then we might need to pad
69150 ** the transaction and/or sync the WAL file.
69151 **
69152 ** Padding and syncing only occur if this set of frames complete a
69153 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
69154 ** or synchronous==OFF, then no padding or syncing are needed.
69155 **
69156 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
69157 ** needed and only the sync is done. If padding is needed, then the
69158 ** final frame is repeated (with its commit mark) until the next sector
69159 ** boundary is crossed. Only the part of the WAL prior to the last
69160 ** sector boundary is synced; the part of the last frame that extends
69161 ** past the sector boundary is written after the sync.
69162 */
69163 if( isCommit && WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03)!=0 ){
69164 int bSync = 1;
69165 if( pWal->padToSectorBoundary ){
69166 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
69167 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
69168 bSync = (w.iSyncPoint==iOffset);
69169 testcase( bSync );
69170 while( iOffset<w.iSyncPoint ){
69171 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
69172 if( rc ) return rc;
69173 iOffset += szFrame;
69174 nExtra++;
69175 assert( pLast!=0 )((void) (0));
69176 }
69177 }
69178 if( bSync ){
69179 assert( rc==SQLITE_OK )((void) (0));
69180 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03));
69181 }
69182 }
69183
69184 /* If this frame set completes the first transaction in the WAL and
69185 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
69186 ** journal size limit, if possible.
69187 */
69188 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
69189 i64 sz = pWal->mxWalSize;
69190 if( walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) )>pWal->mxWalSize ){
69191 sz = walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) );
69192 }
69193 walLimitSize(pWal, sz);
69194 pWal->truncateOnCommit = 0;
69195 }
69196
69197 /* Append data to the wal-index. It is not necessary to lock the
69198 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
69199 ** guarantees that there are no other writers, and no data that may
69200 ** be in use by existing readers is being overwritten.
69201 */
69202 iFrame = pWal->hdr.mxFrame;
69203 for(p=pList; p && rc==SQLITE_OK0; p=p->pDirty){
69204 if( (p->flags & PGHDR_WAL_APPEND0x040)==0 ) continue;
69205 iFrame++;
69206 rc = walIndexAppend(pWal, iFrame, p->pgno);
69207 }
69208 assert( pLast!=0 || nExtra==0 )((void) (0));
69209 while( rc==SQLITE_OK0 && nExtra>0 ){
69210 iFrame++;
69211 nExtra--;
69212 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
69213 }
69214
69215 if( rc==SQLITE_OK0 ){
69216 /* Update the private copy of the header. */
69217 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
69218 testcase( szPage<=32768 );
69219 testcase( szPage>=65536 );
69220 pWal->hdr.mxFrame = iFrame;
69221 if( isCommit ){
69222 pWal->hdr.iChange++;
69223 pWal->hdr.nPage = nTruncate;
69224 }
69225 /* If this is a commit, update the wal-index header too. */
69226 if( isCommit ){
69227 walIndexWriteHdr(pWal);
69228 pWal->iCallback = iFrame;
69229 }
69230 }
69231
69232 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
69233 return rc;
69234}
69235
69236/*
69237** Write a set of frames to the log. The caller must hold the write-lock
69238** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
69239**
69240** The difference between this function and walFrames() is that this
69241** function wraps walFrames() in an SEH_TRY{...} block.
69242*/
69243SQLITE_PRIVATEstatic int sqlite3WalFrames(
69244 Wal *pWal, /* Wal handle to write to */
69245 int szPage, /* Database page-size in bytes */
69246 PgHdr *pList, /* List of dirty pages to write */
69247 Pgno nTruncate, /* Database size after this commit */
69248 int isCommit, /* True if this is a commit */
69249 int sync_flags /* Flags to pass to OsSync() (or 0) */
69250){
69251 int rc;
69252 SEH_TRY; {
69253 rc = walFrames(pWal, szPage, pList, nTruncate, isCommit, sync_flags);
69254 }
69255 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
69256 return rc;
69257}
69258
69259/*
69260** This routine is called to implement sqlite3_wal_checkpoint() and
69261** related interfaces.
69262**
69263** Obtain a CHECKPOINT lock and then backfill as much information as
69264** we can from WAL into the database.
69265**
69266** If parameter xBusy is not NULL, it is a pointer to a busy-handler
69267** callback. In this case this function runs a blocking checkpoint.
69268*/
69269SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
69270 Wal *pWal, /* Wal connection */
69271 sqlite3 *db, /* Check this handle's interrupt flag */
69272 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
69273 int (*xBusy)(void*), /* Function to call when busy */
69274 void *pBusyArg, /* Context argument for xBusyHandler */
69275 int sync_flags, /* Flags to sync db file with (or 0) */
69276 int nBuf, /* Size of temporary buffer */
69277 u8 *zBuf, /* Temporary buffer to use */
69278 int *pnLog, /* OUT: Number of frames in WAL */
69279 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
69280){
69281 int rc; /* Return code */
69282 int isChanged = 0; /* True if a new wal-index header is loaded */
69283 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
69284 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
69285
69286 assert( pWal->ckptLock==0 )((void) (0));
69287 assert( pWal->writeLock==0 )((void) (0));
69288
69289 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
69290 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
69291 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
69292
69293 if( pWal->readOnly ) return SQLITE_READONLY8;
69294 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
69295
69296 /* Enable blocking locks, if possible. */
69297 sqlite3WalDb(pWal, db);
69298 if( xBusy2 ) (void)walEnableBlocking(pWal)0;
69299
69300 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
69301 ** "checkpoint" lock on the database file.
69302 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
69303 ** checkpoint operation at the same time, the lock cannot be obtained and
69304 ** SQLITE_BUSY is returned.
69305 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
69306 ** it will not be invoked in this case.
69307 */
69308 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
69309 testcase( rc==SQLITE_BUSY );
69310 testcase( rc!=SQLITE_OK && xBusy2!=0 );
69311 if( rc==SQLITE_OK0 ){
69312 pWal->ckptLock = 1;
69313
69314 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
69315 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
69316 ** file.
69317 **
69318 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
69319 ** immediately, and a busy-handler is configured, it is invoked and the
69320 ** writer lock retried until either the busy-handler returns 0 or the
69321 ** lock is successfully obtained.
69322 */
69323 if( eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
69324 rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK0, 1);
69325 if( rc==SQLITE_OK0 ){
69326 pWal->writeLock = 1;
69327 }else if( rc==SQLITE_BUSY5 ){
69328 eMode2 = SQLITE_CHECKPOINT_PASSIVE0;
69329 xBusy2 = 0;
69330 rc = SQLITE_OK0;
69331 }
69332 }
69333 }
69334
69335
69336 /* Read the wal-index header. */
69337 SEH_TRY; {
69338 if( rc==SQLITE_OK0 ){
69339 /* For a passive checkpoint, do not re-enable blocking locks after
69340 ** reading the wal-index header. A passive checkpoint should not block
69341 ** or invoke the busy handler. The only lock such a checkpoint may
69342 ** attempt to obtain is a lock on a read-slot, and it should give up
69343 ** immediately and do a partial checkpoint if it cannot obtain it. */
69344 walDisableBlocking(pWal);
69345 rc = walIndexReadHdr(pWal, &isChanged);
69346 if( eMode2!=SQLITE_CHECKPOINT_PASSIVE0 ) (void)walEnableBlocking(pWal)0;
69347 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
69348 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
69349 }
69350 }
69351
69352 /* Copy data from the log to the database file. */
69353 if( rc==SQLITE_OK0 ){
69354 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
69355 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(69355);
69356 }else{
69357 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags,zBuf);
69358 }
69359
69360 /* If no error occurred, set the output variables. */
69361 if( rc==SQLITE_OK0 || rc==SQLITE_BUSY5 ){
69362 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
69363 SEH_INJECT_FAULT((void) (0));;
69364 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
69365 }
69366 }
69367 }
69368 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
69369
69370 if( isChanged ){
69371 /* If a new wal-index header was loaded before the checkpoint was
69372 ** performed, then the pager-cache associated with pWal is now
69373 ** out of date. So zero the cached wal-index header to ensure that
69374 ** next time the pager opens a snapshot on this database it knows that
69375 ** the cache needs to be reset.
69376 */
69377 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
69378 }
69379
69380 walDisableBlocking(pWal);
69381 sqlite3WalDb(pWal, 0);
69382
69383 /* Release the locks. */
69384 sqlite3WalEndWriteTransaction(pWal);
69385 if( pWal->ckptLock ){
69386 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
69387 pWal->ckptLock = 0;
69388 }
69389 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
69390#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
69391 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ) rc = SQLITE_BUSY5;
69392#endif
69393 return (rc==SQLITE_OK0 && eMode!=eMode2 ? SQLITE_BUSY5 : rc);
69394}
69395
69396/* Return the value to pass to a sqlite3_wal_hook callback, the
69397** number of frames in the WAL at the point of the last commit since
69398** sqlite3WalCallback() was called. If no commits have occurred since
69399** the last call, then return 0.
69400*/
69401SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal){
69402 u32 ret = 0;
69403 if( pWal ){
69404 ret = pWal->iCallback;
69405 pWal->iCallback = 0;
69406 }
69407 return (int)ret;
69408}
69409
69410/*
69411** This function is called to change the WAL subsystem into or out
69412** of locking_mode=EXCLUSIVE.
69413**
69414** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
69415** into locking_mode=NORMAL. This means that we must acquire a lock
69416** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
69417** or if the acquisition of the lock fails, then return 0. If the
69418** transition out of exclusive-mode is successful, return 1. This
69419** operation must occur while the pager is still holding the exclusive
69420** lock on the main database file.
69421**
69422** If op is one, then change from locking_mode=NORMAL into
69423** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
69424** be released. Return 1 if the transition is made and 0 if the
69425** WAL is already in exclusive-locking mode - meaning that this
69426** routine is a no-op. The pager must already hold the exclusive lock
69427** on the main database file before invoking this operation.
69428**
69429** If op is negative, then do a dry-run of the op==1 case but do
69430** not actually change anything. The pager uses this to see if it
69431** should acquire the database exclusive lock prior to invoking
69432** the op==1 case.
69433*/
69434SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op){
69435 int rc;
69436 assert( pWal->writeLock==0 )((void) (0));
69437 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 )((void) (0));
69438
69439 /* pWal->readLock is usually set, but might be -1 if there was a
69440 ** prior error while attempting to acquire are read-lock. This cannot
69441 ** happen if the connection is actually in exclusive mode (as no xShmLock
69442 ** locks are taken in this case). Nor should the pager attempt to
69443 ** upgrade to exclusive-mode following such an error.
69444 */
69445#ifndef SQLITE_USE_SEH
69446 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
69447#endif
69448 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) )((void) (0));
69449
69450 if( op==0 ){
69451 if( pWal->exclusiveMode!=WAL_NORMAL_MODE0 ){
69452 pWal->exclusiveMode = WAL_NORMAL_MODE0;
69453 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)))!=SQLITE_OK0 ){
69454 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
69455 }
69456 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
69457 }else{
69458 /* Already in locking_mode=NORMAL */
69459 rc = 0;
69460 }
69461 }else if( op>0 ){
69462 assert( pWal->exclusiveMode==WAL_NORMAL_MODE )((void) (0));
69463 assert( pWal->readLock>=0 )((void) (0));
69464 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
69465 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
69466 rc = 1;
69467 }else{
69468 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
69469 }
69470 return rc;
69471}
69472
69473/*
69474** Return true if the argument is non-NULL and the WAL module is using
69475** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
69476** WAL module is using shared-memory, return false.
69477*/
69478SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal){
69479 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 );
69480}
69481
69482#ifdef SQLITE_ENABLE_SNAPSHOT1
69483/* Create a snapshot object. The content of a snapshot is opaque to
69484** every other subsystem, so the WAL module can put whatever it needs
69485** in the object.
69486*/
69487SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
69488 int rc = SQLITE_OK0;
69489 WalIndexHdr *pRet;
69490 static const u32 aZero[4] = { 0, 0, 0, 0 };
69491
69492 assert( pWal->readLock>=0 && pWal->writeLock==0 )((void) (0));
69493
69494 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
69495 *ppSnapshot = 0;
69496 return SQLITE_ERROR1;
69497 }
69498 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
69499 if( pRet==0 ){
69500 rc = SQLITE_NOMEM_BKPT7;
69501 }else{
69502 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
69503 *ppSnapshot = (sqlite3_snapshot*)pRet;
69504 }
69505
69506 return rc;
69507}
69508
69509/* Try to open on pSnapshot when the next read-transaction starts
69510*/
69511SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(
69512 Wal *pWal,
69513 sqlite3_snapshot *pSnapshot
69514){
69515 if( pSnapshot && ((WalIndexHdr*)pSnapshot)->iVersion==0 ){
69516 /* iVersion==0 means that this is a call to sqlite3_snapshot_get(). In
69517 ** this case set the bGetSnapshot flag so that if the call to
69518 ** sqlite3_snapshot_get() is about to read transaction on this wal
69519 ** file, it does not take read-lock 0 if the wal file has been completely
69520 ** checkpointed. Taking read-lock 0 would work, but then it would be
69521 ** possible for a subsequent writer to destroy the snapshot even while
69522 ** this connection is holding its read-transaction open. This is contrary
69523 ** to user expectations, so we avoid it by not taking read-lock 0. */
69524 pWal->bGetSnapshot = 1;
69525 }else{
69526 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
69527 pWal->bGetSnapshot = 0;
69528 }
69529}
69530
69531/*
69532** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
69533** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
69534*/
69535SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
69536 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
69537 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
69538
69539 /* aSalt[0] is a copy of the value stored in the wal file header. It
69540 ** is incremented each time the wal file is restarted. */
69541 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
69542 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
69543 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
69544 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
69545 return 0;
69546}
69547
69548/*
69549** The caller currently has a read transaction open on the database.
69550** This function takes a SHARED lock on the CHECKPOINTER slot and then
69551** checks if the snapshot passed as the second argument is still
69552** available. If so, SQLITE_OK is returned.
69553**
69554** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
69555** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
69556** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
69557** lock is released before returning.
69558*/
69559SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
69560 int rc;
69561 SEH_TRY; {
69562 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
69563 if( rc==SQLITE_OK0 ){
69564 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
69565 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
69566 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
69567 ){
69568 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
69569 walUnlockShared(pWal, WAL_CKPT_LOCK1);
69570 }
69571 }
69572 }
69573 SEH_EXCEPT( rc = walHandleException(pWal); ); ((void) (0));
69574 return rc;
69575}
69576
69577/*
69578** Release a lock obtained by an earlier successful call to
69579** sqlite3WalSnapshotCheck().
69580*/
69581SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal){
69582 assert( pWal )((void) (0));
69583 walUnlockShared(pWal, WAL_CKPT_LOCK1);
69584}
69585
69586
69587#endif /* SQLITE_ENABLE_SNAPSHOT */
69588
69589#ifdef SQLITE_ENABLE_ZIPVFS
69590/*
69591** If the argument is not NULL, it points to a Wal object that holds a
69592** read-lock. This function returns the database page-size if it is known,
69593** or zero if it is not (or if pWal is NULL).
69594*/
69595SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal){
69596 assert( pWal==0 || pWal->readLock>=0 )((void) (0));
69597 return (pWal ? pWal->szPage : 0);
69598}
69599#endif
69600
69601/* Return the sqlite3_file object for the WAL file
69602*/
69603SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal){
69604 return pWal->pWalFd;
69605}
69606
69607#endif /* #ifndef SQLITE_OMIT_WAL */
69608
69609/************** End of wal.c *************************************************/
69610/************** Begin file btmutex.c *****************************************/
69611/*
69612** 2007 August 27
69613**
69614** The author disclaims copyright to this source code. In place of
69615** a legal notice, here is a blessing:
69616**
69617** May you do good and not evil.
69618** May you find forgiveness for yourself and forgive others.
69619** May you share freely, never taking more than you give.
69620**
69621*************************************************************************
69622**
69623** This file contains code used to implement mutexes on Btree objects.
69624** This code really belongs in btree.c. But btree.c is getting too
69625** big and we want to break it down some. This packaged seemed like
69626** a good breakout.
69627*/
69628/************** Include btreeInt.h in the middle of btmutex.c ****************/
69629/************** Begin file btreeInt.h ****************************************/
69630/*
69631** 2004 April 6
69632**
69633** The author disclaims copyright to this source code. In place of
69634** a legal notice, here is a blessing:
69635**
69636** May you do good and not evil.
69637** May you find forgiveness for yourself and forgive others.
69638** May you share freely, never taking more than you give.
69639**
69640*************************************************************************
69641** This file implements an external (disk-based) database using BTrees.
69642** For a detailed discussion of BTrees, refer to
69643**
69644** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
69645** "Sorting And Searching", pages 473-480. Addison-Wesley
69646** Publishing Company, Reading, Massachusetts.
69647**
69648** The basic idea is that each page of the file contains N database
69649** entries and N+1 pointers to subpages.
69650**
69651** ----------------------------------------------------------------
69652** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
69653** ----------------------------------------------------------------
69654**
69655** All of the keys on the page that Ptr(0) points to have values less
69656** than Key(0). All of the keys on page Ptr(1) and its subpages have
69657** values greater than Key(0) and less than Key(1). All of the keys
69658** on Ptr(N) and its subpages have values greater than Key(N-1). And
69659** so forth.
69660**
69661** Finding a particular key requires reading O(log(M)) pages from the
69662** disk where M is the number of entries in the tree.
69663**
69664** In this implementation, a single file can hold one or more separate
69665** BTrees. Each BTree is identified by the index of its root page. The
69666** key and data for any entry are combined to form the "payload". A
69667** fixed amount of payload can be carried directly on the database
69668** page. If the payload is larger than the preset amount then surplus
69669** bytes are stored on overflow pages. The payload for an entry
69670** and the preceding pointer are combined to form a "Cell". Each
69671** page has a small header which contains the Ptr(N) pointer and other
69672** information such as the size of key and data.
69673**
69674** FORMAT DETAILS
69675**
69676** The file is divided into pages. The first page is called page 1,
69677** the second is page 2, and so forth. A page number of zero indicates
69678** "no such page". The page size can be any power of 2 between 512 and 65536.
69679** Each page can be either a btree page, a freelist page, an overflow
69680** page, or a pointer-map page.
69681**
69682** The first page is always a btree page. The first 100 bytes of the first
69683** page contain a special header (the "file header") that describes the file.
69684** The format of the file header is as follows:
69685**
69686** OFFSET SIZE DESCRIPTION
69687** 0 16 Header string: "SQLite format 3\000"
69688** 16 2 Page size in bytes. (1 means 65536)
69689** 18 1 File format write version
69690** 19 1 File format read version
69691** 20 1 Bytes of unused space at the end of each page
69692** 21 1 Max embedded payload fraction (must be 64)
69693** 22 1 Min embedded payload fraction (must be 32)
69694** 23 1 Min leaf payload fraction (must be 32)
69695** 24 4 File change counter
69696** 28 4 The size of the database in pages
69697** 32 4 First freelist page
69698** 36 4 Number of freelist pages in the file
69699** 40 60 15 4-byte meta values passed to higher layers
69700**
69701** 40 4 Schema cookie
69702** 44 4 File format of schema layer
69703** 48 4 Size of page cache
69704** 52 4 Largest root-page (auto/incr_vacuum)
69705** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
69706** 60 4 User version
69707** 64 4 Incremental vacuum mode
69708** 68 4 Application-ID
69709** 72 20 unused
69710** 92 4 The version-valid-for number
69711** 96 4 SQLITE_VERSION_NUMBER
69712**
69713** All of the integer values are big-endian (most significant byte first).
69714**
69715** The file change counter is incremented when the database is changed
69716** This counter allows other processes to know when the file has changed
69717** and thus when they need to flush their cache.
69718**
69719** The max embedded payload fraction is the amount of the total usable
69720** space in a page that can be consumed by a single cell for standard
69721** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
69722** is to limit the maximum cell size so that at least 4 cells will fit
69723** on one page. Thus the default max embedded payload fraction is 64.
69724**
69725** If the payload for a cell is larger than the max payload, then extra
69726** payload is spilled to overflow pages. Once an overflow page is allocated,
69727** as many bytes as possible are moved into the overflow pages without letting
69728** the cell size drop below the min embedded payload fraction.
69729**
69730** The min leaf payload fraction is like the min embedded payload fraction
69731** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
69732** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
69733** not specified in the header.
69734**
69735** Each btree pages is divided into three sections: The header, the
69736** cell pointer array, and the cell content area. Page 1 also has a 100-byte
69737** file header that occurs before the page header.
69738**
69739** |----------------|
69740** | file header | 100 bytes. Page 1 only.
69741** |----------------|
69742** | page header | 8 bytes for leaves. 12 bytes for interior nodes
69743** |----------------|
69744** | cell pointer | | 2 bytes per cell. Sorted order.
69745** | array | | Grows downward
69746** | | v
69747** |----------------|
69748** | unallocated |
69749** | space |
69750** |----------------| ^ Grows upwards
69751** | cell content | | Arbitrary order interspersed with freeblocks.
69752** | area | | and free space fragments.
69753** |----------------|
69754**
69755** The page headers looks like this:
69756**
69757** OFFSET SIZE DESCRIPTION
69758** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
69759** 1 2 byte offset to the first freeblock
69760** 3 2 number of cells on this page
69761** 5 2 first byte of the cell content area
69762** 7 1 number of fragmented free bytes
69763** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
69764**
69765** The flags define the format of this btree page. The leaf flag means that
69766** this page has no children. The zerodata flag means that this page carries
69767** only keys and no data. The intkey flag means that the key is an integer
69768** which is stored in the key size entry of the cell header rather than in
69769** the payload area.
69770**
69771** The cell pointer array begins on the first byte after the page header.
69772** The cell pointer array contains zero or more 2-byte numbers which are
69773** offsets from the beginning of the page to the cell content in the cell
69774** content area. The cell pointers occur in sorted order. The system strives
69775** to keep free space after the last cell pointer so that new cells can
69776** be easily added without having to defragment the page.
69777**
69778** Cell content is stored at the very end of the page and grows toward the
69779** beginning of the page.
69780**
69781** Unused space within the cell content area is collected into a linked list of
69782** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
69783** to the first freeblock is given in the header. Freeblocks occur in
69784** increasing order. Because a freeblock must be at least 4 bytes in size,
69785** any group of 3 or fewer unused bytes in the cell content area cannot
69786** exist on the freeblock chain. A group of 3 or fewer free bytes is called
69787** a fragment. The total number of bytes in all fragments is recorded.
69788** in the page header at offset 7.
69789**
69790** SIZE DESCRIPTION
69791** 2 Byte offset of the next freeblock
69792** 2 Bytes in this freeblock
69793**
69794** Cells are of variable length. Cells are stored in the cell content area at
69795** the end of the page. Pointers to the cells are in the cell pointer array
69796** that immediately follows the page header. Cells is not necessarily
69797** contiguous or in order, but cell pointers are contiguous and in order.
69798**
69799** Cell content makes use of variable length integers. A variable
69800** length integer is 1 to 9 bytes where the lower 7 bits of each
69801** byte are used. The integer consists of all bytes that have bit 8 set and
69802** the first byte with bit 8 clear. The most significant byte of the integer
69803** appears first. A variable-length integer may not be more than 9 bytes long.
69804** As a special case, all 8 bits of the 9th byte are used as data. This
69805** allows a 64-bit integer to be encoded in 9 bytes.
69806**
69807** 0x00 becomes 0x00000000
69808** 0x7f becomes 0x0000007f
69809** 0x81 0x00 becomes 0x00000080
69810** 0x82 0x00 becomes 0x00000100
69811** 0x80 0x7f becomes 0x0000007f
69812** 0x81 0x91 0xd1 0xac 0x78 becomes 0x12345678
69813** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
69814**
69815** Variable length integers are used for rowids and to hold the number of
69816** bytes of key and data in a btree cell.
69817**
69818** The content of a cell looks like this:
69819**
69820** SIZE DESCRIPTION
69821** 4 Page number of the left child. Omitted if leaf flag is set.
69822** var Number of bytes of data. Omitted if the zerodata flag is set.
69823** var Number of bytes of key. Or the key itself if intkey flag is set.
69824** * Payload
69825** 4 First page of the overflow chain. Omitted if no overflow
69826**
69827** Overflow pages form a linked list. Each page except the last is completely
69828** filled with data (pagesize - 4 bytes). The last page can have as little
69829** as 1 byte of data.
69830**
69831** SIZE DESCRIPTION
69832** 4 Page number of next overflow page
69833** * Data
69834**
69835** Freelist pages come in two subtypes: trunk pages and leaf pages. The
69836** file header points to the first in a linked list of trunk page. Each trunk
69837** page points to multiple leaf pages. The content of a leaf page is
69838** unspecified. A trunk page looks like this:
69839**
69840** SIZE DESCRIPTION
69841** 4 Page number of next trunk page
69842** 4 Number of leaf pointers on this page
69843** * zero or more pages numbers of leaves
69844*/
69845/* #include "sqliteInt.h" */
69846
69847
69848/* The following value is the maximum cell size assuming a maximum page
69849** size give above.
69850*/
69851#define MX_CELL_SIZE(pBt)((int)(pBt->pageSize-8)) ((int)(pBt->pageSize-8))
69852
69853/* The maximum number of cells on a single page of the database. This
69854** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
69855** plus 2 bytes for the index to the cell in the page header). Such
69856** small cells will be rare, but they are possible.
69857*/
69858#define MX_CELL(pBt)((pBt->pageSize-8)/6) ((pBt->pageSize-8)/6)
69859
69860/* Forward declarations */
69861typedef struct MemPage MemPage;
69862typedef struct BtLock BtLock;
69863typedef struct CellInfo CellInfo;
69864
69865/*
69866** This is a magic string that appears at the beginning of every
69867** SQLite database in order to identify the file as a real database.
69868**
69869** You can change this value at compile-time by specifying a
69870** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
69871** header must be exactly 16 bytes including the zero-terminator so
69872** the string itself should be 15 characters long. If you change
69873** the header, then your custom library will not be able to read
69874** databases generated by the standard tools and the standard tools
69875** will not be able to read databases created by your custom library.
69876*/
69877#ifndef SQLITE_FILE_HEADER"SQLite format 3" /* 123456789 123456 */
69878# define SQLITE_FILE_HEADER"SQLite format 3" "SQLite format 3"
69879#endif
69880
69881/*
69882** Page type flags. An ORed combination of these flags appear as the
69883** first byte of on-disk image of every BTree page.
69884*/
69885#define PTF_INTKEY0x01 0x01
69886#define PTF_ZERODATA0x02 0x02
69887#define PTF_LEAFDATA0x04 0x04
69888#define PTF_LEAF0x08 0x08
69889
69890/*
69891** An instance of this object stores information about each a single database
69892** page that has been loaded into memory. The information in this object
69893** is derived from the raw on-disk page content.
69894**
69895** As each database page is loaded into memory, the pager allocates an
69896** instance of this object and zeros the first 8 bytes. (This is the
69897** "extra" information associated with each page of the pager.)
69898**
69899** Access to all fields of this structure is controlled by the mutex
69900** stored in MemPage.pBt->mutex.
69901*/
69902struct MemPage {
69903 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
69904 u8 intKey; /* True if table b-trees. False for index b-trees */
69905 u8 intKeyLeaf; /* True if the leaf of an intKey table */
69906 Pgno pgno; /* Page number for this page */
69907 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
69908 ** is allocated. All fields that follow must be initialized before use */
69909 u8 leaf; /* True if a leaf page */
69910 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
69911 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
69912 u8 max1bytePayload; /* min(maxLocal,127) */
69913 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
69914 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
69915 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
69916 u16 cellOffset; /* Index in aData of first cell pointer */
69917 int nFree; /* Number of free bytes on the page. -1 for unknown */
69918 u16 nCell; /* Number of cells on this page, local and ovfl */
69919 u16 maskPage; /* Mask for page offset */
69920 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
69921 ** non-overflow cell */
69922 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
69923 BtShared *pBt; /* Pointer to BtShared that this page is part of */
69924 u8 *aData; /* Pointer to disk image of the page data */
69925 u8 *aDataEnd; /* One byte past the end of the entire page - not just
69926 ** the usable space, the entire page. Used to prevent
69927 ** corruption-induced buffer overflow. */
69928 u8 *aCellIdx; /* The cell index area */
69929 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
69930 DbPage *pDbPage; /* Pager page handle */
69931 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
69932 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
69933};
69934
69935/*
69936** A linked list of the following structures is stored at BtShared.pLock.
69937** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
69938** is opened on the table with root page BtShared.iTable. Locks are removed
69939** from this list when a transaction is committed or rolled back, or when
69940** a btree handle is closed.
69941*/
69942struct BtLock {
69943 Btree *pBtree; /* Btree handle holding this lock */
69944 Pgno iTable; /* Root page of table */
69945 u8 eLock; /* READ_LOCK or WRITE_LOCK */
69946 BtLock *pNext; /* Next in BtShared.pLock list */
69947};
69948
69949/* Candidate values for BtLock.eLock */
69950#define READ_LOCK1 1
69951#define WRITE_LOCK2 2
69952
69953/* A Btree handle
69954**
69955** A database connection contains a pointer to an instance of
69956** this object for every database file that it has open. This structure
69957** is opaque to the database connection. The database connection cannot
69958** see the internals of this structure and only deals with pointers to
69959** this structure.
69960**
69961** For some database files, the same underlying database cache might be
69962** shared between multiple connections. In that case, each connection
69963** has it own instance of this object. But each instance of this object
69964** points to the same BtShared object. The database cache and the
69965** schema associated with the database file are all contained within
69966** the BtShared object.
69967**
69968** All fields in this structure are accessed under sqlite3.mutex.
69969** The pBt pointer itself may not be changed while there exists cursors
69970** in the referenced BtShared that point back to this Btree since those
69971** cursors have to go through this Btree to find their BtShared and
69972** they often do so without holding sqlite3.mutex.
69973*/
69974struct Btree {
69975 sqlite3 *db; /* The database connection holding this btree */
69976 BtShared *pBt; /* Sharable content of this btree */
69977 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
69978 u8 sharable; /* True if we can share pBt with another db */
69979 u8 locked; /* True if db currently has pBt locked */
69980 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
69981 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
69982 int nBackup; /* Number of backup operations reading this btree */
69983 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
69984 Btree *pNext; /* List of other sharable Btrees from the same db */
69985 Btree *pPrev; /* Back pointer of the same list */
69986#ifdef SQLITE_DEBUG
69987 u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */
69988#endif
69989#ifndef SQLITE_OMIT_SHARED_CACHE
69990 BtLock lock; /* Object used to lock page 1 */
69991#endif
69992};
69993
69994/*
69995** Btree.inTrans may take one of the following values.
69996**
69997** If the shared-data extension is enabled, there may be multiple users
69998** of the Btree structure. At most one of these may open a write transaction,
69999** but any number may have active read transactions.
70000**
70001** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and
70002** SQLITE_TXN_WRITE
70003*/
70004#define TRANS_NONE0 0
70005#define TRANS_READ1 1
70006#define TRANS_WRITE2 2
70007
70008#if TRANS_NONE0!=SQLITE_TXN_NONE0
70009# error wrong numeric code for no-transaction
70010#endif
70011#if TRANS_READ1!=SQLITE_TXN_READ1
70012# error wrong numeric code for read-transaction
70013#endif
70014#if TRANS_WRITE2!=SQLITE_TXN_WRITE2
70015# error wrong numeric code for write-transaction
70016#endif
70017
70018
70019/*
70020** An instance of this object represents a single database file.
70021**
70022** A single database file can be in use at the same time by two
70023** or more database connections. When two or more connections are
70024** sharing the same database file, each connection has it own
70025** private Btree object for the file and each of those Btrees points
70026** to this one BtShared object. BtShared.nRef is the number of
70027** connections currently sharing this database file.
70028**
70029** Fields in this structure are accessed under the BtShared.mutex
70030** mutex, except for nRef and pNext which are accessed under the
70031** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field
70032** may not be modified once it is initially set as long as nRef>0.
70033** The pSchema field may be set once under BtShared.mutex and
70034** thereafter is unchanged as long as nRef>0.
70035**
70036** isPending:
70037**
70038** If a BtShared client fails to obtain a write-lock on a database
70039** table (because there exists one or more read-locks on the table),
70040** the shared-cache enters 'pending-lock' state and isPending is
70041** set to true.
70042**
70043** The shared-cache leaves the 'pending lock' state when either of
70044** the following occur:
70045**
70046** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
70047** 2) The number of locks held by other connections drops to zero.
70048**
70049** while in the 'pending-lock' state, no connection may start a new
70050** transaction.
70051**
70052** This feature is included to help prevent writer-starvation.
70053*/
70054struct BtShared {
70055 Pager *pPager; /* The page cache */
70056 sqlite3 *db; /* Database connection currently using this Btree */
70057 BtCursor *pCursor; /* A list of all open cursors */
70058 MemPage *pPage1; /* First page of the database */
70059 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
70060#ifndef SQLITE_OMIT_AUTOVACUUM
70061 u8 autoVacuum; /* True if auto-vacuum is enabled */
70062 u8 incrVacuum; /* True if incr-vacuum is enabled */
70063 u8 bDoTruncate; /* True to truncate db on commit */
70064#endif
70065 u8 inTransaction; /* Transaction state */
70066 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
70067 u8 nReserveWanted; /* Desired number of extra bytes per page */
70068 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
70069 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
70070 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
70071 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
70072 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
70073 u32 pageSize; /* Total number of bytes on a page */
70074 u32 usableSize; /* Number of usable bytes on each page */
70075 int nTransaction; /* Number of open transactions (read + write) */
70076 u32 nPage; /* Number of pages in the database */
70077 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
70078 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
70079 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
70080 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
70081#ifndef SQLITE_OMIT_SHARED_CACHE
70082 int nRef; /* Number of references to this structure */
70083 BtShared *pNext; /* Next on a list of sharable BtShared structs */
70084 BtLock *pLock; /* List of locks held on this shared-btree struct */
70085 Btree *pWriter; /* Btree with currently open write transaction */
70086#endif
70087 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
70088 int nPreformatSize; /* Size of last cell written by TransferRow() */
70089};
70090
70091/*
70092** Allowed values for BtShared.btsFlags
70093*/
70094#define BTS_READ_ONLY0x0001 0x0001 /* Underlying file is readonly */
70095#define BTS_PAGESIZE_FIXED0x0002 0x0002 /* Page size can no longer be changed */
70096#define BTS_SECURE_DELETE0x0004 0x0004 /* PRAGMA secure_delete is enabled */
70097#define BTS_OVERWRITE0x0008 0x0008 /* Overwrite deleted content with zeros */
70098#define BTS_FAST_SECURE0x000c 0x000c /* Combination of the previous two */
70099#define BTS_INITIALLY_EMPTY0x0010 0x0010 /* Database was empty at trans start */
70100#define BTS_NO_WAL0x0020 0x0020 /* Do not open write-ahead-log files */
70101#define BTS_EXCLUSIVE0x0040 0x0040 /* pWriter has an exclusive lock */
70102#define BTS_PENDING0x0080 0x0080 /* Waiting for read-locks to clear */
70103
70104/*
70105** An instance of the following structure is used to hold information
70106** about a cell. The parseCellPtr() function fills in this structure
70107** based on information extract from the raw disk page.
70108*/
70109struct CellInfo {
70110 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
70111 u8 *pPayload; /* Pointer to the start of payload */
70112 u32 nPayload; /* Bytes of payload */
70113 u16 nLocal; /* Amount of payload held locally, not on overflow */
70114 u16 nSize; /* Size of the cell content on the main b-tree page */
70115};
70116
70117/*
70118** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
70119** this will be declared corrupt. This value is calculated based on a
70120** maximum database size of 2^31 pages a minimum fanout of 2 for a
70121** root-node and 3 for all other internal nodes.
70122**
70123** If a tree that appears to be taller than this is encountered, it is
70124** assumed that the database is corrupt.
70125*/
70126#define BTCURSOR_MAX_DEPTH20 20
70127
70128/*
70129** A cursor is a pointer to a particular entry within a particular
70130** b-tree within a database file.
70131**
70132** The entry is identified by its MemPage and the index in
70133** MemPage.aCell[] of the entry.
70134**
70135** A single database file can be shared by two more database connections,
70136** but cursors cannot be shared. Each cursor is associated with a
70137** particular database connection identified BtCursor.pBtree.db.
70138**
70139** Fields in this structure are accessed under the BtShared.mutex
70140** found at self->pBt->mutex.
70141**
70142** skipNext meaning:
70143** The meaning of skipNext depends on the value of eState:
70144**
70145** eState Meaning of skipNext
70146** VALID skipNext is meaningless and is ignored
70147** INVALID skipNext is meaningless and is ignored
70148** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
70149** sqlite3BtreePrevious() is no-op if skipNext<0.
70150** REQUIRESEEK restoreCursorPosition() restores the cursor to
70151** eState=SKIPNEXT if skipNext!=0
70152** FAULT skipNext holds the cursor fault error code.
70153*/
70154struct BtCursor {
70155 u8 eState; /* One of the CURSOR_XXX constants (see below) */
70156 u8 curFlags; /* zero or more BTCF_* flags defined below */
70157 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
70158 u8 hints; /* As configured by CursorSetHints() */
70159 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
70160 ** Error code if eState==CURSOR_FAULT */
70161 Btree *pBtree; /* The Btree to which this cursor belongs */
70162 Pgno *aOverflow; /* Cache of overflow page locations */
70163 void *pKey; /* Saved key that was cursor last known position */
70164 /* All fields above are zeroed when the cursor is allocated. See
70165 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
70166 ** initialized. */
70167#define BTCURSOR_FIRST_UNINITpBt pBt /* Name of first uninitialized field */
70168 BtShared *pBt; /* The BtShared this cursor points to */
70169 BtCursor *pNext; /* Forms a linked list of all cursors */
70170 CellInfo info; /* A parse of the cell we are pointing at */
70171 i64 nKey; /* Size of pKey, or last integer key */
70172 Pgno pgnoRoot; /* The root page of this tree */
70173 i8 iPage; /* Index of current page in apPage */
70174 u8 curIntKey; /* Value of apPage[0]->intKey */
70175 u16 ix; /* Current index for apPage[iPage] */
70176 u16 aiIdx[BTCURSOR_MAX_DEPTH20-1]; /* Current index in apPage[i] */
70177 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
70178 MemPage *pPage; /* Current page */
70179 MemPage *apPage[BTCURSOR_MAX_DEPTH20-1]; /* Stack of parents of current page */
70180};
70181
70182/*
70183** Legal values for BtCursor.curFlags
70184*/
70185#define BTCF_WriteFlag0x01 0x01 /* True if a write cursor */
70186#define BTCF_ValidNKey0x02 0x02 /* True if info.nKey is valid */
70187#define BTCF_ValidOvfl0x04 0x04 /* True if aOverflow is valid */
70188#define BTCF_AtLast0x08 0x08 /* Cursor is pointing to the last entry */
70189#define BTCF_Incrblob0x10 0x10 /* True if an incremental I/O handle */
70190#define BTCF_Multiple0x20 0x20 /* Maybe another cursor on the same btree */
70191#define BTCF_Pinned0x40 0x40 /* Cursor is busy and cannot be moved */
70192
70193/*
70194** Potential values for BtCursor.eState.
70195**
70196** CURSOR_INVALID:
70197** Cursor does not point to a valid entry. This can happen (for example)
70198** because the table is empty or because BtreeCursorFirst() has not been
70199** called.
70200**
70201** CURSOR_VALID:
70202** Cursor points to a valid entry. getPayload() etc. may be called.
70203**
70204** CURSOR_SKIPNEXT:
70205** Cursor is valid except that the Cursor.skipNext field is non-zero
70206** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
70207** operation should be a no-op.
70208**
70209** CURSOR_REQUIRESEEK:
70210** The table that this cursor was opened on still exists, but has been
70211** modified since the cursor was last used. The cursor position is saved
70212** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
70213** this state, restoreCursorPosition() can be called to attempt to
70214** seek the cursor to the saved position.
70215**
70216** CURSOR_FAULT:
70217** An unrecoverable error (an I/O error or a malloc failure) has occurred
70218** on a different connection that shares the BtShared cache with this
70219** cursor. The error has left the cache in an inconsistent state.
70220** Do nothing else with this cursor. Any attempt to use the cursor
70221** should return the error code stored in BtCursor.skipNext
70222*/
70223#define CURSOR_VALID0 0
70224#define CURSOR_INVALID1 1
70225#define CURSOR_SKIPNEXT2 2
70226#define CURSOR_REQUIRESEEK3 3
70227#define CURSOR_FAULT4 4
70228
70229/*
70230** The database page the PENDING_BYTE occupies. This page is never used.
70231*/
70232#define PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((pBt)->pageSize))+1))
70233
70234/*
70235** These macros define the location of the pointer-map entry for a
70236** database page. The first argument to each is the number of usable
70237** bytes on each page of the database (often 1024). The second is the
70238** page number to look up in the pointer map.
70239**
70240** PTRMAP_PAGENO returns the database page number of the pointer-map
70241** page that stores the required pointer. PTRMAP_PTROFFSET returns
70242** the offset of the requested map entry.
70243**
70244** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
70245** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
70246** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
70247** this test.
70248*/
70249#define PTRMAP_PAGENO(pBt, pgno)ptrmapPageno(pBt, pgno) ptrmapPageno(pBt, pgno)
70250#define PTRMAP_PTROFFSET(pgptrmap, pgno)(5*(pgno-pgptrmap-1)) (5*(pgno-pgptrmap-1))
70251#define PTRMAP_ISPAGE(pBt, pgno)(ptrmapPageno((pBt), (pgno))==(pgno)) (PTRMAP_PAGENO((pBt),(pgno))ptrmapPageno((pBt), (pgno))==(pgno))
70252
70253/*
70254** The pointer map is a lookup table that identifies the parent page for
70255** each child page in the database file. The parent page is the page that
70256** contains a pointer to the child. Every page in the database contains
70257** 0 or 1 parent pages. (In this context 'database page' refers
70258** to any page that is not part of the pointer map itself.) Each pointer map
70259** entry consists of a single byte 'type' and a 4 byte parent page number.
70260** The PTRMAP_XXX identifiers below are the valid types.
70261**
70262** The purpose of the pointer map is to facility moving pages from one
70263** position in the file to another as part of autovacuum. When a page
70264** is moved, the pointer in its parent must be updated to point to the
70265** new location. The pointer map is used to locate the parent page quickly.
70266**
70267** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
70268** used in this case.
70269**
70270** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
70271** is not used in this case.
70272**
70273** PTRMAP_OVERFLOW1: The database page is the first page in a list of
70274** overflow pages. The page number identifies the page that
70275** contains the cell with a pointer to this overflow page.
70276**
70277** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
70278** overflow pages. The page-number identifies the previous
70279** page in the overflow page list.
70280**
70281** PTRMAP_BTREE: The database page is a non-root btree page. The page number
70282** identifies the parent page in the btree.
70283*/
70284#define PTRMAP_ROOTPAGE1 1
70285#define PTRMAP_FREEPAGE2 2
70286#define PTRMAP_OVERFLOW13 3
70287#define PTRMAP_OVERFLOW24 4
70288#define PTRMAP_BTREE5 5
70289
70290/* A bunch of assert() statements to check the transaction state variables
70291** of handle p (type Btree*) are internally consistent.
70292*/
70293#define btreeIntegrity(p)((void) (0)); ((void) (0)); \
70294 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 )((void) (0)); \
70295 assert( p->pBt->inTransaction>=p->inTrans )((void) (0));
70296
70297
70298/*
70299** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
70300** if the database supports auto-vacuum or not. Because it is used
70301** within an expression that is an argument to another macro
70302** (sqliteMallocRaw), it is not possible to use conditional compilation.
70303** So, this macro is defined instead.
70304*/
70305#ifndef SQLITE_OMIT_AUTOVACUUM
70306#define ISAUTOVACUUM(pBt)(pBt->autoVacuum) (pBt->autoVacuum)
70307#else
70308#define ISAUTOVACUUM(pBt)(pBt->autoVacuum) 0
70309#endif
70310
70311
70312/*
70313** This structure is passed around through all the PRAGMA integrity_check
70314** checking routines in order to keep track of some global state information.
70315**
70316** The aRef[] array is allocated so that there is 1 bit for each page in
70317** the database. As the integrity-check proceeds, for each page used in
70318** the database the corresponding bit is set. This allows integrity-check to
70319** detect pages that are used twice and orphaned pages (both of which
70320** indicate corruption).
70321*/
70322typedef struct IntegrityCk IntegrityCk;
70323struct IntegrityCk {
70324 BtShared *pBt; /* The tree being checked out */
70325 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
70326 u8 *aPgRef; /* 1 bit per page in the db (see above) */
70327 Pgno nCkPage; /* Pages in the database. 0 for partial check */
70328 int mxErr; /* Stop accumulating errors when this reaches zero */
70329 int nErr; /* Number of messages written to zErrMsg so far */
70330 int rc; /* SQLITE_OK, SQLITE_NOMEM, or SQLITE_INTERRUPT */
70331 u32 nStep; /* Number of steps into the integrity_check process */
70332 const char *zPfx; /* Error message prefix */
70333 Pgno v0; /* Value for first %u substitution in zPfx (root page) */
70334 Pgno v1; /* Value for second %u substitution in zPfx (current pg) */
70335 int v2; /* Value for third %d substitution in zPfx */
70336 StrAccum errMsg; /* Accumulate the error message text here */
70337 u32 *heap; /* Min-heap used for analyzing cell coverage */
70338 sqlite3 *db; /* Database connection running the check */
70339 i64 nRow; /* Number of rows visited in current tree */
70340};
70341
70342/*
70343** Routines to read or write a two- and four-byte big-endian integer values.
70344*/
70345#define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
70346#define put2byte(p,v)((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
70347#define get4bytesqlite3Get4byte sqlite3Get4byte
70348#define put4bytesqlite3Put4byte sqlite3Put4byte
70349
70350/*
70351** get2byteAligned(), unlike get2byte(), requires that its argument point to a
70352** two-byte aligned address. get2byteAligned() is only used for accessing the
70353** cell addresses in a btree header.
70354*/
70355#if SQLITE_BYTEORDER1234==4321
70356# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) (*(u16*)(x))
70357#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4008000
70358# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) __builtin_bswap16(*(u16*)(x))
70359#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
70360# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) _byteswap_ushort(*(u16*)(x))
70361#else
70362# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
70363#endif
70364
70365/************** End of btreeInt.h ********************************************/
70366/************** Continuing where we left off in btmutex.c ********************/
70367#ifndef SQLITE_OMIT_SHARED_CACHE
70368#if SQLITE_THREADSAFE2
70369
70370/*
70371** Obtain the BtShared mutex associated with B-Tree handle p. Also,
70372** set BtShared.db to the database handle associated with p and the
70373** p->locked boolean to true.
70374*/
70375static void lockBtreeMutex(Btree *p){
70376 assert( p->locked==0 )((void) (0));
70377 assert( sqlite3_mutex_notheld(p->pBt->mutex) )((void) (0));
70378 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
70379
70380 sqlite3_mutex_enter(p->pBt->mutex);
70381 p->pBt->db = p->db;
70382 p->locked = 1;
70383}
70384
70385/*
70386** Release the BtShared mutex associated with B-Tree handle p and
70387** clear the p->locked boolean.
70388*/
70389static void SQLITE_NOINLINE__attribute__((noinline)) unlockBtreeMutex(Btree *p){
70390 BtShared *pBt = p->pBt;
70391 assert( p->locked==1 )((void) (0));
70392 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70393 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
70394 assert( p->db==pBt->db )((void) (0));
70395
70396 sqlite3_mutex_leave(pBt->mutex);
70397 p->locked = 0;
70398}
70399
70400/* Forward reference */
70401static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p);
70402
70403/*
70404** Enter a mutex on the given BTree object.
70405**
70406** If the object is not sharable, then no mutex is ever required
70407** and this routine is a no-op. The underlying mutex is non-recursive.
70408** But we keep a reference count in Btree.wantToLock so the behavior
70409** of this interface is recursive.
70410**
70411** To avoid deadlocks, multiple Btrees are locked in the same order
70412** by all database connections. The p->pNext is a list of other
70413** Btrees belonging to the same database connection as the p Btree
70414** which need to be locked after p. If we cannot get a lock on
70415** p, then first unlock all of the others on p->pNext, then wait
70416** for the lock to become available on p, then relock all of the
70417** subsequent Btrees that desire a lock.
70418*/
70419SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
70420 /* Some basic sanity checking on the Btree. The list of Btrees
70421 ** connected by pNext and pPrev should be in sorted order by
70422 ** Btree.pBt value. All elements of the list should belong to
70423 ** the same connection. Only shared Btrees are on the list. */
70424 assert( p->pNext==0 || p->pNext->pBt>p->pBt )((void) (0));
70425 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt )((void) (0));
70426 assert( p->pNext==0 || p->pNext->db==p->db )((void) (0));
70427 assert( p->pPrev==0 || p->pPrev->db==p->db )((void) (0));
70428 assert( p->sharable || (p->pNext==0 && p->pPrev==0) )((void) (0));
70429
70430 /* Check for locking consistency */
70431 assert( !p->locked || p->wantToLock>0 )((void) (0));
70432 assert( p->sharable || p->wantToLock==0 )((void) (0));
70433
70434 /* We should already hold a lock on the database connection */
70435 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
70436
70437 /* Unless the database is sharable and unlocked, then BtShared.db
70438 ** should already be set correctly. */
70439 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db )((void) (0));
70440
70441 if( !p->sharable ) return;
70442 p->wantToLock++;
70443 if( p->locked ) return;
70444 btreeLockCarefully(p);
70445}
70446
70447/* This is a helper function for sqlite3BtreeLock(). By moving
70448** complex, but seldom used logic, out of sqlite3BtreeLock() and
70449** into this routine, we avoid unnecessary stack pointer changes
70450** and thus help the sqlite3BtreeLock() routine to run much faster
70451** in the common case.
70452*/
70453static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p){
70454 Btree *pLater;
70455
70456 /* In most cases, we should be able to acquire the lock we
70457 ** want without having to go through the ascending lock
70458 ** procedure that follows. Just be sure not to block.
70459 */
70460 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK0 ){
70461 p->pBt->db = p->db;
70462 p->locked = 1;
70463 return;
70464 }
70465
70466 /* To avoid deadlock, first release all locks with a larger
70467 ** BtShared address. Then acquire our lock. Then reacquire
70468 ** the other BtShared locks that we used to hold in ascending
70469 ** order.
70470 */
70471 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
70472 assert( pLater->sharable )((void) (0));
70473 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt )((void) (0));
70474 assert( !pLater->locked || pLater->wantToLock>0 )((void) (0));
70475 if( pLater->locked ){
70476 unlockBtreeMutex(pLater);
70477 }
70478 }
70479 lockBtreeMutex(p);
70480 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
70481 if( pLater->wantToLock ){
70482 lockBtreeMutex(pLater);
70483 }
70484 }
70485}
70486
70487
70488/*
70489** Exit the recursive mutex on a Btree.
70490*/
70491SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree *p){
70492 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
70493 if( p->sharable ){
70494 assert( p->wantToLock>0 )((void) (0));
70495 p->wantToLock--;
70496 if( p->wantToLock==0 ){
70497 unlockBtreeMutex(p);
70498 }
70499 }
70500}
70501
70502#ifndef NDEBUG1
70503/*
70504** Return true if the BtShared mutex is held on the btree, or if the
70505** B-Tree is not marked as sharable.
70506**
70507** This routine is used only from within assert() statements.
70508*/
70509SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree *p){
70510 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 )((void) (0));
70511 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db )((void) (0));
70512 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
70513 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
70514
70515 return (p->sharable==0 || p->locked);
70516}
70517#endif
70518
70519
70520/*
70521** Enter the mutex on every Btree associated with a database
70522** connection. This is needed (for example) prior to parsing
70523** a statement since we will be comparing table and column names
70524** against all schemas and we do not want those schemas being
70525** reset out from under us.
70526**
70527** There is a corresponding leave-all procedures.
70528**
70529** Enter the mutexes in ascending order by BtShared pointer address
70530** to avoid the possibility of deadlock when two threads with
70531** two or more btrees in common both try to lock all their btrees
70532** at the same instant.
70533*/
70534static void SQLITE_NOINLINE__attribute__((noinline)) btreeEnterAll(sqlite3 *db){
70535 int i;
70536 int skipOk = 1;
70537 Btree *p;
70538 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
70539 for(i=0; i<db->nDb; i++){
70540 p = db->aDb[i].pBt;
70541 if( p && p->sharable ){
70542 sqlite3BtreeEnter(p);
70543 skipOk = 0;
70544 }
70545 }
70546 db->noSharedCache = skipOk;
70547}
70548SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
70549 if( db->noSharedCache==0 ) btreeEnterAll(db);
70550}
70551static void SQLITE_NOINLINE__attribute__((noinline)) btreeLeaveAll(sqlite3 *db){
70552 int i;
70553 Btree *p;
70554 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
70555 for(i=0; i<db->nDb; i++){
70556 p = db->aDb[i].pBt;
70557 if( p ) sqlite3BtreeLeave(p);
70558 }
70559}
70560SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3 *db){
70561 if( db->noSharedCache==0 ) btreeLeaveAll(db);
70562}
70563
70564#ifndef NDEBUG1
70565/*
70566** Return true if the current thread holds the database connection
70567** mutex and all required BtShared mutexes.
70568**
70569** This routine is used inside assert() statements only.
70570*/
70571SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
70572 int i;
70573 if( !sqlite3_mutex_held(db->mutex) ){
70574 return 0;
70575 }
70576 for(i=0; i<db->nDb; i++){
70577 Btree *p;
70578 p = db->aDb[i].pBt;
70579 if( p && p->sharable &&
70580 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
70581 return 0;
70582 }
70583 }
70584 return 1;
70585}
70586#endif /* NDEBUG */
70587
70588#ifndef NDEBUG1
70589/*
70590** Return true if the correct mutexes are held for accessing the
70591** db->aDb[iDb].pSchema structure. The mutexes required for schema
70592** access are:
70593**
70594** (1) The mutex on db
70595** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
70596**
70597** If pSchema is not NULL, then iDb is computed from pSchema and
70598** db using sqlite3SchemaToIndex().
70599*/
70600SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
70601 Btree *p;
70602 assert( db!=0 )((void) (0));
70603 if( db->pVfs==0 && db->nDb==0 ) return 1;
70604 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
70605 assert( iDb>=0 && iDb<db->nDb )((void) (0));
70606 if( !sqlite3_mutex_held(db->mutex) ) return 0;
70607 if( iDb==1 ) return 1;
70608 p = db->aDb[iDb].pBt;
70609 assert( p!=0 )((void) (0));
70610 return p->sharable==0 || p->locked==1;
70611}
70612#endif /* NDEBUG */
70613
70614#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
70615/*
70616** The following are special cases for mutex enter routines for use
70617** in single threaded applications that use shared cache. Except for
70618** these two routines, all mutex operations are no-ops in that case and
70619** are null #defines in btree.h.
70620**
70621** If shared cache is disabled, then all btree mutex routines, including
70622** the ones below, are no-ops and are null #defines in btree.h.
70623*/
70624
70625SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
70626 p->pBt->db = p->db;
70627}
70628SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
70629 int i;
70630 for(i=0; i<db->nDb; i++){
70631 Btree *p = db->aDb[i].pBt;
70632 if( p ){
70633 p->pBt->db = p->db;
70634 }
70635 }
70636}
70637#endif /* if SQLITE_THREADSAFE */
70638
70639#ifndef SQLITE_OMIT_INCRBLOB
70640/*
70641** Enter a mutex on a Btree given a cursor owned by that Btree.
70642**
70643** These entry points are used by incremental I/O only. Enter() is required
70644** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
70645** the build is threadsafe. Leave() is only required by threadsafe builds.
70646*/
70647SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor *pCur){
70648 sqlite3BtreeEnter(pCur->pBtree);
70649}
70650# if SQLITE_THREADSAFE2
70651SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor *pCur){
70652 sqlite3BtreeLeave(pCur->pBtree);
70653}
70654# endif
70655#endif /* ifndef SQLITE_OMIT_INCRBLOB */
70656
70657#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
70658
70659/************** End of btmutex.c *********************************************/
70660/************** Begin file btree.c *******************************************/
70661/*
70662** 2004 April 6
70663**
70664** The author disclaims copyright to this source code. In place of
70665** a legal notice, here is a blessing:
70666**
70667** May you do good and not evil.
70668** May you find forgiveness for yourself and forgive others.
70669** May you share freely, never taking more than you give.
70670**
70671*************************************************************************
70672** This file implements an external (disk-based) database using BTrees.
70673** See the header comment on "btreeInt.h" for additional information.
70674** Including a description of file format and an overview of operation.
70675*/
70676/* #include "btreeInt.h" */
70677
70678/*
70679** The header string that appears at the beginning of every
70680** SQLite database.
70681*/
70682static const char zMagicHeader[] = SQLITE_FILE_HEADER"SQLite format 3";
70683
70684/*
70685** Set this global variable to 1 to enable tracing using the TRACE
70686** macro.
70687*/
70688#if 0
70689int sqlite3BtreeTrace=1; /* True to enable tracing */
70690# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdoutstdout);}
70691#else
70692# define TRACE(X)
70693#endif
70694
70695/*
70696** Extract a 2-byte big-endian integer from an array of unsigned bytes.
70697** But if the value is zero, make it 65536.
70698**
70699** This routine is used to extract the "offset to cell content area" value
70700** from the header of a btree page. If the page size is 65536 and the page
70701** is empty, the offset should be 65536, but the 2-byte value stores zero.
70702** This routine makes the necessary adjustment to 65536.
70703*/
70704#define get2byteNotZero(X)(((((int)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1) (((((int)get2byte(X)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1)
70705
70706/*
70707** Values passed as the 5th argument to allocateBtreePage()
70708*/
70709#define BTALLOC_ANY0 0 /* Allocate any page */
70710#define BTALLOC_EXACT1 1 /* Allocate exact page if possible */
70711#define BTALLOC_LE2 2 /* Allocate any page <= the parameter */
70712
70713/*
70714** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
70715** defined, or 0 if it is. For example:
70716**
70717** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
70718*/
70719#ifndef SQLITE_OMIT_AUTOVACUUM
70720#define IfNotOmitAV(expr)(expr) (expr)
70721#else
70722#define IfNotOmitAV(expr)(expr) 0
70723#endif
70724
70725#ifndef SQLITE_OMIT_SHARED_CACHE
70726/*
70727** A list of BtShared objects that are eligible for participation
70728** in shared cache. This variable has file scope during normal builds,
70729** but the test harness needs to access it so we make it global for
70730** test builds.
70731**
70732** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
70733*/
70734#ifdef SQLITE_TEST
70735SQLITE_PRIVATEstatic BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
70736#else
70737static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
70738#endif
70739#endif /* SQLITE_OMIT_SHARED_CACHE */
70740
70741#ifndef SQLITE_OMIT_SHARED_CACHE
70742/*
70743** Enable or disable the shared pager and schema features.
70744**
70745** This routine has no effect on existing database connections.
70746** The shared cache setting effects only future calls to
70747** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
70748*/
70749SQLITE_API int sqlite3_enable_shared_cache(int enable){
70750 sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled = enable;
70751 return SQLITE_OK0;
70752}
70753#endif
70754
70755
70756
70757#ifdef SQLITE_OMIT_SHARED_CACHE
70758 /*
70759 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
70760 ** and clearAllSharedCacheTableLocks()
70761 ** manipulate entries in the BtShared.pLock linked list used to store
70762 ** shared-cache table level locks. If the library is compiled with the
70763 ** shared-cache feature disabled, then there is only ever one user
70764 ** of each BtShared structure and so this locking is not necessary.
70765 ** So define the lock related functions as no-ops.
70766 */
70767 #define querySharedCacheTableLock(a,b,c) SQLITE_OK0
70768 #define setSharedCacheTableLock(a,b,c) SQLITE_OK0
70769 #define clearAllSharedCacheTableLocks(a)
70770 #define downgradeAllSharedCacheTableLocks(a)
70771 #define hasSharedCacheTableLock(a,b,c,d) 1
70772 #define hasReadConflicts(a, b) 0
70773#endif
70774
70775#ifdef SQLITE_DEBUG
70776/*
70777** Return and reset the seek counter for a Btree object.
70778*/
70779SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt)0{
70780 u64 n = pBt->nSeek;
70781 pBt->nSeek = 0;
70782 return n;
70783}
70784#endif
70785
70786/*
70787** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
70788** (MemPage*) as an argument. The (MemPage*) must not be NULL.
70789**
70790** If SQLITE_DEBUG is not defined, then this macro is equivalent to
70791** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
70792** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
70793** with the page number and filename associated with the (MemPage*).
70794*/
70795#ifdef SQLITE_DEBUG
70796int corruptPageError(int lineno, MemPage *p){
70797 char *zMsg;
70798 sqlite3BeginBenignMalloc();
70799 zMsg = sqlite3_mprintf("database corruption page %u of %s",
70800 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
70801 );
70802 sqlite3EndBenignMalloc();
70803 if( zMsg ){
70804 sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
70805 }
70806 sqlite3_free(zMsg);
70807 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70807);
70808}
70809# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(70809) corruptPageError(__LINE__70809, pMemPage)
70810#else
70811# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(70811) SQLITE_CORRUPT_PGNO(pMemPage->pgno)sqlite3CorruptError(70811)
70812#endif
70813
70814/* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
70815** or if the lock tracking is disabled. This is always the value for
70816** release builds.
70817*/
70818#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
70819
70820#ifndef SQLITE_OMIT_SHARED_CACHE
70821
70822#if 0
70823/* ^---- Change to 1 and recompile to enable shared-lock tracing
70824** for debugging purposes.
70825**
70826** Print all shared-cache locks on a BtShared. Debugging use only.
70827*/
70828static void sharedLockTrace(
70829 BtShared *pBt,
70830 const char *zMsg,
70831 int iRoot,
70832 int eLockType
70833){
70834 BtLock *pLock;
70835 if( iRoot>0 ){
70836 printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK1?"R":"W");
70837 }else{
70838 printf("%s-%p:", zMsg, pBt);
70839 }
70840 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
70841 printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
70842 pLock->eLock==READ_LOCK1 ? "R" : "W");
70843 while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
70844 pLock = pLock->pNext;
70845 printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK1 ? "R" : "W");
70846 }
70847 }
70848 printf("\n");
70849 fflush(stdoutstdout);
70850}
70851#undef SHARED_LOCK_TRACE
70852#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) sharedLockTrace(X,MSG,TAB,TYPE)
70853#endif /* Shared-lock tracing */
70854
70855#ifdef SQLITE_DEBUG
70856/*
70857**** This function is only used as part of an assert() statement. ***
70858**
70859** Check to see if pBtree holds the required locks to read or write to the
70860** table with root page iRoot. Return 1 if it does and 0 if not.
70861**
70862** For example, when writing to a table with root-page iRoot via
70863** Btree connection pBtree:
70864**
70865** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
70866**
70867** When writing to an index that resides in a sharable database, the
70868** caller should have first obtained a lock specifying the root page of
70869** the corresponding table. This makes things a bit more complicated,
70870** as this module treats each table as a separate structure. To determine
70871** the table corresponding to the index being written, this
70872** function has to search through the database schema.
70873**
70874** Instead of a lock on the table/index rooted at page iRoot, the caller may
70875** hold a write-lock on the schema table (root page 1). This is also
70876** acceptable.
70877*/
70878static int hasSharedCacheTableLock(
70879 Btree *pBtree, /* Handle that must hold lock */
70880 Pgno iRoot, /* Root page of b-tree */
70881 int isIndex, /* True if iRoot is the root of an index b-tree */
70882 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
70883){
70884 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
70885 Pgno iTab = 0;
70886 BtLock *pLock;
70887
70888 /* If this database is not shareable, or if the client is reading
70889 ** and has the read-uncommitted flag set, then no lock is required.
70890 ** Return true immediately.
70891 */
70892 if( (pBtree->sharable==0)
70893 || (eLockType==READ_LOCK1 && (pBtree->db->flags & SQLITE_ReadUncommit((u64)(0x00004)<<32)))
70894 ){
70895 return 1;
70896 }
70897
70898 /* If the client is reading or writing an index and the schema is
70899 ** not loaded, then it is too difficult to actually check to see if
70900 ** the correct locks are held. So do not bother - just return true.
70901 ** This case does not come up very often anyhow.
70902 */
70903 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded0x0001)==0) ){
70904 return 1;
70905 }
70906
70907 /* Figure out the root-page that the lock should be held on. For table
70908 ** b-trees, this is just the root page of the b-tree being read or
70909 ** written. For index b-trees, it is the root page of the associated
70910 ** table. */
70911 if( isIndex ){
70912 HashElem *p;
70913 int bSeen = 0;
70914 for(p=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); p; p=sqliteHashNext(p)((p)->next)){
70915 Index *pIdx = (Index *)sqliteHashData(p)((p)->data);
70916 if( pIdx->tnum==iRoot ){
70917 if( bSeen ){
70918 /* Two or more indexes share the same root page. There must
70919 ** be imposter tables. So just return true. The assert is not
70920 ** useful in that case. */
70921 return 1;
70922 }
70923 iTab = pIdx->pTable->tnum;
70924 bSeen = 1;
70925 }
70926 }
70927 }else{
70928 iTab = iRoot;
70929 }
70930
70931 SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
70932
70933 /* Search for the required lock. Either a write-lock on root-page iTab, a
70934 ** write-lock on the schema table, or (if the client is reading) a
70935 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
70936 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
70937 if( pLock->pBtree==pBtree
70938 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK2 && pLock->iTable==1))
70939 && pLock->eLock>=eLockType
70940 ){
70941 return 1;
70942 }
70943 }
70944
70945 /* Failed to find the required lock. */
70946 return 0;
70947}
70948#endif /* SQLITE_DEBUG */
70949
70950#ifdef SQLITE_DEBUG
70951/*
70952**** This function may be used as part of assert() statements only. ****
70953**
70954** Return true if it would be illegal for pBtree to write into the
70955** table or index rooted at iRoot because other shared connections are
70956** simultaneously reading that same table or index.
70957**
70958** It is illegal for pBtree to write if some other Btree object that
70959** shares the same BtShared object is currently reading or writing
70960** the iRoot table. Except, if the other Btree object has the
70961** read-uncommitted flag set, then it is OK for the other object to
70962** have a read cursor.
70963**
70964** For example, before writing to any part of the table or index
70965** rooted at page iRoot, one should call:
70966**
70967** assert( !hasReadConflicts(pBtree, iRoot) );
70968*/
70969static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
70970 BtCursor *p;
70971 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
70972 if( p->pgnoRoot==iRoot
70973 && p->pBtree!=pBtree
70974 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit((u64)(0x00004)<<32))
70975 ){
70976 return 1;
70977 }
70978 }
70979 return 0;
70980}
70981#endif /* #ifdef SQLITE_DEBUG */
70982
70983/*
70984** Query to see if Btree handle p may obtain a lock of type eLock
70985** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
70986** SQLITE_OK if the lock may be obtained (by calling
70987** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
70988*/
70989static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
70990 BtShared *pBt = p->pBt;
70991 BtLock *pIter;
70992
70993 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
70994 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
70995 assert( p->db!=0 )((void) (0));
70996 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 )((void) (0));
70997
70998 /* If requesting a write-lock, then the Btree must have an open write
70999 ** transaction on this file. And, obviously, for this to be so there
71000 ** must be an open write transaction on the file itself.
71001 */
71002 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) )((void) (0));
71003 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE )((void) (0));
71004
71005 /* This routine is a no-op if the shared-cache is not enabled */
71006 if( !p->sharable ){
71007 return SQLITE_OK0;
71008 }
71009
71010 /* If some other connection is holding an exclusive lock, the
71011 ** requested lock may not be obtained.
71012 */
71013 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE0x0040)!=0 ){
71014 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
71015 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
71016 }
71017
71018 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
71019 /* The condition (pIter->eLock!=eLock) in the following if(...)
71020 ** statement is a simplification of:
71021 **
71022 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
71023 **
71024 ** since we know that if eLock==WRITE_LOCK, then no other connection
71025 ** may hold a WRITE_LOCK on any table in this file (since there can
71026 ** only be a single writer).
71027 */
71028 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK )((void) (0));
71029 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK)((void) (0));
71030 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
71031 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
71032 if( eLock==WRITE_LOCK2 ){
71033 assert( p==pBt->pWriter )((void) (0));
71034 pBt->btsFlags |= BTS_PENDING0x0080;
71035 }
71036 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
71037 }
71038 }
71039 return SQLITE_OK0;
71040}
71041#endif /* !SQLITE_OMIT_SHARED_CACHE */
71042
71043#ifndef SQLITE_OMIT_SHARED_CACHE
71044/*
71045** Add a lock on the table with root-page iTable to the shared-btree used
71046** by Btree handle p. Parameter eLock must be either READ_LOCK or
71047** WRITE_LOCK.
71048**
71049** This function assumes the following:
71050**
71051** (a) The specified Btree object p is connected to a sharable
71052** database (one with the BtShared.sharable flag set), and
71053**
71054** (b) No other Btree objects hold a lock that conflicts
71055** with the requested lock (i.e. querySharedCacheTableLock() has
71056** already been called and returned SQLITE_OK).
71057**
71058** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
71059** is returned if a malloc attempt fails.
71060*/
71061static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
71062 BtShared *pBt = p->pBt;
71063 BtLock *pLock = 0;
71064 BtLock *pIter;
71065
71066 SHARED_LOCK_TRACE(pBt,"setLock", iTable, eLock);
71067
71068 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
71069 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
71070 assert( p->db!=0 )((void) (0));
71071
71072 /* A connection with the read-uncommitted flag set will never try to
71073 ** obtain a read-lock using this function. The only read-lock obtained
71074 ** by a connection in read-uncommitted mode is on the sqlite_schema
71075 ** table, and that lock is obtained in BtreeBeginTrans(). */
71076 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK )((void) (0));
71077
71078 /* This function should only be called on a sharable b-tree after it
71079 ** has been determined that no other b-tree holds a conflicting lock. */
71080 assert( p->sharable )((void) (0));
71081 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) )((void) (0));
71082
71083 /* First search the list for an existing lock on this table. */
71084 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
71085 if( pIter->iTable==iTable && pIter->pBtree==p ){
71086 pLock = pIter;
71087 break;
71088 }
71089 }
71090
71091 /* If the above search did not find a BtLock struct associating Btree p
71092 ** with table iTable, allocate one and link it into the list.
71093 */
71094 if( !pLock ){
71095 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
71096 if( !pLock ){
71097 return SQLITE_NOMEM_BKPT7;
71098 }
71099 pLock->iTable = iTable;
71100 pLock->pBtree = p;
71101 pLock->pNext = pBt->pLock;
71102 pBt->pLock = pLock;
71103 }
71104
71105 /* Set the BtLock.eLock variable to the maximum of the current lock
71106 ** and the requested lock. This means if a write-lock was already held
71107 ** and a read-lock requested, we don't incorrectly downgrade the lock.
71108 */
71109 assert( WRITE_LOCK>READ_LOCK )((void) (0));
71110 if( eLock>pLock->eLock ){
71111 pLock->eLock = eLock;
71112 }
71113
71114 return SQLITE_OK0;
71115}
71116#endif /* !SQLITE_OMIT_SHARED_CACHE */
71117
71118#ifndef SQLITE_OMIT_SHARED_CACHE
71119/*
71120** Release all the table locks (locks obtained via calls to
71121** the setSharedCacheTableLock() procedure) held by Btree object p.
71122**
71123** This function assumes that Btree p has an open read or write
71124** transaction. If it does not, then the BTS_PENDING flag
71125** may be incorrectly cleared.
71126*/
71127static void clearAllSharedCacheTableLocks(Btree *p){
71128 BtShared *pBt = p->pBt;
71129 BtLock **ppIter = &pBt->pLock;
71130
71131 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
71132 assert( p->sharable || 0==*ppIter )((void) (0));
71133 assert( p->inTrans>0 )((void) (0));
71134
71135 SHARED_LOCK_TRACE(pBt, "clearAllLocks", 0, 0);
71136
71137 while( *ppIter ){
71138 BtLock *pLock = *ppIter;
71139 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree )((void) (0));
71140 assert( pLock->pBtree->inTrans>=pLock->eLock )((void) (0));
71141 if( pLock->pBtree==p ){
71142 *ppIter = pLock->pNext;
71143 assert( pLock->iTable!=1 || pLock==&p->lock )((void) (0));
71144 if( pLock->iTable!=1 ){
71145 sqlite3_free(pLock);
71146 }
71147 }else{
71148 ppIter = &pLock->pNext;
71149 }
71150 }
71151
71152 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter )((void) (0));
71153 if( pBt->pWriter==p ){
71154 pBt->pWriter = 0;
71155 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
71156 }else if( pBt->nTransaction==2 ){
71157 /* This function is called when Btree p is concluding its
71158 ** transaction. If there currently exists a writer, and p is not
71159 ** that writer, then the number of locks held by connections other
71160 ** than the writer must be about to drop to zero. In this case
71161 ** set the BTS_PENDING flag to 0.
71162 **
71163 ** If there is not currently a writer, then BTS_PENDING must
71164 ** be zero already. So this next line is harmless in that case.
71165 */
71166 pBt->btsFlags &= ~BTS_PENDING0x0080;
71167 }
71168}
71169
71170/*
71171** This function changes all write-locks held by Btree p into read-locks.
71172*/
71173static void downgradeAllSharedCacheTableLocks(Btree *p){
71174 BtShared *pBt = p->pBt;
71175
71176 SHARED_LOCK_TRACE(pBt, "downgradeLocks", 0, 0);
71177
71178 if( pBt->pWriter==p ){
71179 BtLock *pLock;
71180 pBt->pWriter = 0;
71181 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
71182 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
71183 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p )((void) (0));
71184 pLock->eLock = READ_LOCK1;
71185 }
71186 }
71187}
71188
71189#endif /* SQLITE_OMIT_SHARED_CACHE */
71190
71191static void releasePage(MemPage *pPage); /* Forward reference */
71192static void releasePageOne(MemPage *pPage); /* Forward reference */
71193static void releasePageNotNull(MemPage *pPage); /* Forward reference */
71194
71195/*
71196***** This routine is used inside of assert() only ****
71197**
71198** Verify that the cursor holds the mutex on its BtShared
71199*/
71200#ifdef SQLITE_DEBUG
71201static int cursorHoldsMutex(BtCursor *p){
71202 return sqlite3_mutex_held(p->pBt->mutex);
71203}
71204
71205/* Verify that the cursor and the BtShared agree about what is the current
71206** database connetion. This is important in shared-cache mode. If the database
71207** connection pointers get out-of-sync, it is possible for routines like
71208** btreeInitPage() to reference an stale connection pointer that references a
71209** a connection that has already closed. This routine is used inside assert()
71210** statements only and for the purpose of double-checking that the btree code
71211** does keep the database connection pointers up-to-date.
71212*/
71213static int cursorOwnsBtShared(BtCursor *p){
71214 assert( cursorHoldsMutex(p) )((void) (0));
71215 return (p->pBtree->db==p->pBt->db);
71216}
71217#endif
71218
71219/*
71220** Invalidate the overflow cache of the cursor passed as the first argument.
71221** on the shared btree structure pBt.
71222*/
71223#define invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04) (pCur->curFlags &= ~BTCF_ValidOvfl0x04)
71224
71225/*
71226** Invalidate the overflow page-list cache for all cursors opened
71227** on the shared btree structure pBt.
71228*/
71229static void invalidateAllOverflowCache(BtShared *pBt){
71230 BtCursor *p;
71231 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71232 for(p=pBt->pCursor; p; p=p->pNext){
71233 invalidateOverflowCache(p)(p->curFlags &= ~0x04);
71234 }
71235}
71236
71237#ifndef SQLITE_OMIT_INCRBLOB
71238/*
71239** This function is called before modifying the contents of a table
71240** to invalidate any incrblob cursors that are open on the
71241** row or one of the rows being modified.
71242**
71243** If argument isClearTable is true, then the entire contents of the
71244** table is about to be deleted. In this case invalidate all incrblob
71245** cursors open on any row within the table with root-page pgnoRoot.
71246**
71247** Otherwise, if argument isClearTable is false, then the row with
71248** rowid iRow is being replaced or deleted. In this case invalidate
71249** only those incrblob cursors open on that specific row.
71250*/
71251static void invalidateIncrblobCursors(
71252 Btree *pBtree, /* The database file to check */
71253 Pgno pgnoRoot, /* The table that might be changing */
71254 i64 iRow, /* The rowid that might be changing */
71255 int isClearTable /* True if all rows are being deleted */
71256){
71257 BtCursor *p;
71258 assert( pBtree->hasIncrblobCur )((void) (0));
71259 assert( sqlite3BtreeHoldsMutex(pBtree) )((void) (0));
71260 pBtree->hasIncrblobCur = 0;
71261 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71262 if( (p->curFlags & BTCF_Incrblob0x10)!=0 ){
71263 pBtree->hasIncrblobCur = 1;
71264 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
71265 p->eState = CURSOR_INVALID1;
71266 }
71267 }
71268 }
71269}
71270
71271#else
71272 /* Stub function when INCRBLOB is omitted */
71273 #define invalidateIncrblobCursors(w,x,y,z)
71274#endif /* SQLITE_OMIT_INCRBLOB */
71275
71276/*
71277** Set bit pgno of the BtShared.pHasContent bitvec. This is called
71278** when a page that previously contained data becomes a free-list leaf
71279** page.
71280**
71281** The BtShared.pHasContent bitvec exists to work around an obscure
71282** bug caused by the interaction of two useful IO optimizations surrounding
71283** free-list leaf pages:
71284**
71285** 1) When all data is deleted from a page and the page becomes
71286** a free-list leaf page, the page is not written to the database
71287** (as free-list leaf pages contain no meaningful data). Sometimes
71288** such a page is not even journalled (as it will not be modified,
71289** why bother journalling it?).
71290**
71291** 2) When a free-list leaf page is reused, its content is not read
71292** from the database or written to the journal file (why should it
71293** be, if it is not at all meaningful?).
71294**
71295** By themselves, these optimizations work fine and provide a handy
71296** performance boost to bulk delete or insert operations. However, if
71297** a page is moved to the free-list and then reused within the same
71298** transaction, a problem comes up. If the page is not journalled when
71299** it is moved to the free-list and it is also not journalled when it
71300** is extracted from the free-list and reused, then the original data
71301** may be lost. In the event of a rollback, it may not be possible
71302** to restore the database to its original configuration.
71303**
71304** The solution is the BtShared.pHasContent bitvec. Whenever a page is
71305** moved to become a free-list leaf page, the corresponding bit is
71306** set in the bitvec. Whenever a leaf page is extracted from the free-list,
71307** optimization 2 above is omitted if the corresponding bit is already
71308** set in BtShared.pHasContent. The contents of the bitvec are cleared
71309** at the end of every transaction.
71310*/
71311static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
71312 int rc = SQLITE_OK0;
71313 if( !pBt->pHasContent ){
71314 assert( pgno<=pBt->nPage )((void) (0));
71315 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
71316 if( !pBt->pHasContent ){
71317 rc = SQLITE_NOMEM_BKPT7;
71318 }
71319 }
71320 if( rc==SQLITE_OK0 && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
71321 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
71322 }
71323 return rc;
71324}
71325
71326/*
71327** Query the BtShared.pHasContent vector.
71328**
71329** This function is called when a free-list leaf page is removed from the
71330** free-list for reuse. It returns false if it is safe to retrieve the
71331** page from the pager layer with the 'no-content' flag set. True otherwise.
71332*/
71333static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
71334 Bitvec *p = pBt->pHasContent;
71335 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
71336}
71337
71338/*
71339** Clear (destroy) the BtShared.pHasContent bitvec. This should be
71340** invoked at the conclusion of each write-transaction.
71341*/
71342static void btreeClearHasContent(BtShared *pBt){
71343 sqlite3BitvecDestroy(pBt->pHasContent);
71344 pBt->pHasContent = 0;
71345}
71346
71347/*
71348** Release all of the apPage[] pages for a cursor.
71349*/
71350static void btreeReleaseAllCursorPages(BtCursor *pCur){
71351 int i;
71352 if( pCur->iPage>=0 ){
71353 for(i=0; i<pCur->iPage; i++){
71354 releasePageNotNull(pCur->apPage[i]);
71355 }
71356 releasePageNotNull(pCur->pPage);
71357 pCur->iPage = -1;
71358 }
71359}
71360
71361/*
71362** The cursor passed as the only argument must point to a valid entry
71363** when this function is called (i.e. have eState==CURSOR_VALID). This
71364** function saves the current cursor key in variables pCur->nKey and
71365** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
71366** code otherwise.
71367**
71368** If the cursor is open on an intkey table, then the integer key
71369** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
71370** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
71371** set to point to a malloced buffer pCur->nKey bytes in size containing
71372** the key.
71373*/
71374static int saveCursorKey(BtCursor *pCur){
71375 int rc = SQLITE_OK0;
71376 assert( CURSOR_VALID==pCur->eState )((void) (0));
71377 assert( 0==pCur->pKey )((void) (0));
71378 assert( cursorHoldsMutex(pCur) )((void) (0));
71379
71380 if( pCur->curIntKey ){
71381 /* Only the rowid is required for a table btree */
71382 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
71383 }else{
71384 /* For an index btree, save the complete key content. It is possible
71385 ** that the current key is corrupt. In that case, it is possible that
71386 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
71387 ** up to the size of 1 varint plus 1 8-byte value when the cursor
71388 ** position is restored. Hence the 17 bytes of padding allocated
71389 ** below. */
71390 void *pKey;
71391 pCur->nKey = sqlite3BtreePayloadSize(pCur);
71392 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
71393 if( pKey ){
71394 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
71395 if( rc==SQLITE_OK0 ){
71396 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
71397 pCur->pKey = pKey;
71398 }else{
71399 sqlite3_free(pKey);
71400 }
71401 }else{
71402 rc = SQLITE_NOMEM_BKPT7;
71403 }
71404 }
71405 assert( !pCur->curIntKey || !pCur->pKey )((void) (0));
71406 return rc;
71407}
71408
71409/*
71410** Save the current cursor position in the variables BtCursor.nKey
71411** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
71412**
71413** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
71414** prior to calling this routine.
71415*/
71416static int saveCursorPosition(BtCursor *pCur){
71417 int rc;
71418
71419 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState )((void) (0));
71420 assert( 0==pCur->pKey )((void) (0));
71421 assert( cursorHoldsMutex(pCur) )((void) (0));
71422
71423 if( pCur->curFlags & BTCF_Pinned0x40 ){
71424 return SQLITE_CONSTRAINT_PINNED(19 |(11<<8));
71425 }
71426 if( pCur->eState==CURSOR_SKIPNEXT2 ){
71427 pCur->eState = CURSOR_VALID0;
71428 }else{
71429 pCur->skipNext = 0;
71430 }
71431
71432 rc = saveCursorKey(pCur);
71433 if( rc==SQLITE_OK0 ){
71434 btreeReleaseAllCursorPages(pCur);
71435 pCur->eState = CURSOR_REQUIRESEEK3;
71436 }
71437
71438 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04|BTCF_AtLast0x08);
71439 return rc;
71440}
71441
71442/* Forward reference */
71443static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
71444
71445/*
71446** Save the positions of all cursors (except pExcept) that are open on
71447** the table with root-page iRoot. "Saving the cursor position" means that
71448** the location in the btree is remembered in such a way that it can be
71449** moved back to the same spot after the btree has been modified. This
71450** routine is called just before cursor pExcept is used to modify the
71451** table, for example in BtreeDelete() or BtreeInsert().
71452**
71453** If there are two or more cursors on the same btree, then all such
71454** cursors should have their BTCF_Multiple flag set. The btreeCursor()
71455** routine enforces that rule. This routine only needs to be called in
71456** the uncommon case when pExpect has the BTCF_Multiple flag set.
71457**
71458** If pExpect!=NULL and if no other cursors are found on the same root-page,
71459** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
71460** pointless call to this routine.
71461**
71462** Implementation note: This routine merely checks to see if any cursors
71463** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
71464** event that cursors are in need to being saved.
71465*/
71466static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
71467 BtCursor *p;
71468 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71469 assert( pExcept==0 || pExcept->pBt==pBt )((void) (0));
71470 for(p=pBt->pCursor; p; p=p->pNext){
71471 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
71472 }
71473 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
71474 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple0x20;
71475 return SQLITE_OK0;
71476}
71477
71478/* This helper routine to saveAllCursors does the actual work of saving
71479** the cursors if and when a cursor is found that actually requires saving.
71480** The common case is that no cursors need to be saved, so this routine is
71481** broken out from its caller to avoid unnecessary stack pointer movement.
71482*/
71483static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(
71484 BtCursor *p, /* The first cursor that needs saving */
71485 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
71486 BtCursor *pExcept /* Do not save this cursor */
71487){
71488 do{
71489 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
71490 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
71491 int rc = saveCursorPosition(p);
71492 if( SQLITE_OK0!=rc ){
71493 return rc;
71494 }
71495 }else{
71496 testcase( p->iPage>=0 );
71497 btreeReleaseAllCursorPages(p);
71498 }
71499 }
71500 p = p->pNext;
71501 }while( p );
71502 return SQLITE_OK0;
71503}
71504
71505/*
71506** Clear the current cursor position.
71507*/
71508SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *pCur){
71509 assert( cursorHoldsMutex(pCur) )((void) (0));
71510 sqlite3_free(pCur->pKey);
71511 pCur->pKey = 0;
71512 pCur->eState = CURSOR_INVALID1;
71513}
71514
71515/*
71516** In this version of BtreeMoveto, pKey is a packed index record
71517** such as is generated by the OP_MakeRecord opcode. Unpack the
71518** record and then call sqlite3BtreeIndexMoveto() to do the work.
71519*/
71520static int btreeMoveto(
71521 BtCursor *pCur, /* Cursor open on the btree to be searched */
71522 const void *pKey, /* Packed key if the btree is an index */
71523 i64 nKey, /* Integer key for tables. Size of pKey for indices */
71524 int bias, /* Bias search to the high end */
71525 int *pRes /* Write search results here */
71526){
71527 int rc; /* Status code */
71528 UnpackedRecord *pIdxKey; /* Unpacked index key */
71529
71530 if( pKey ){
71531 KeyInfo *pKeyInfo = pCur->pKeyInfo;
71532 assert( nKey==(i64)(int)nKey )((void) (0));
71533 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
71534 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT7;
71535 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
71536 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
71537 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71537);
71538 }else{
71539 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes);
71540 }
71541 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
71542 }else{
71543 pIdxKey = 0;
71544 rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes);
71545 }
71546 return rc;
71547}
71548
71549/*
71550** Restore the cursor to the position it was in (or as close to as possible)
71551** when saveCursorPosition() was called. Note that this call deletes the
71552** saved position info stored by saveCursorPosition(), so there can be
71553** at most one effective restoreCursorPosition() call after each
71554** saveCursorPosition().
71555*/
71556static int btreeRestoreCursorPosition(BtCursor *pCur){
71557 int rc;
71558 int skipNext = 0;
71559 assert( cursorOwnsBtShared(pCur) )((void) (0));
71560 assert( pCur->eState>=CURSOR_REQUIRESEEK )((void) (0));
71561 if( pCur->eState==CURSOR_FAULT4 ){
71562 return pCur->skipNext;
71563 }
71564 pCur->eState = CURSOR_INVALID1;
71565 if( sqlite3FaultSim(410) ){
71566 rc = SQLITE_IOERR10;
71567 }else{
71568 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
71569 }
71570 if( rc==SQLITE_OK0 ){
71571 sqlite3_free(pCur->pKey);
71572 pCur->pKey = 0;
71573 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID )((void) (0));
71574 if( skipNext ) pCur->skipNext = skipNext;
71575 if( pCur->skipNext && pCur->eState==CURSOR_VALID0 ){
71576 pCur->eState = CURSOR_SKIPNEXT2;
71577 }
71578 }
71579 return rc;
71580}
71581
71582#define restoreCursorPosition(p)(p->eState>=3 ? btreeRestoreCursorPosition(p) : 0) \
71583 (p->eState>=CURSOR_REQUIRESEEK3 ? \
71584 btreeRestoreCursorPosition(p) : \
71585 SQLITE_OK0)
71586
71587/*
71588** Determine whether or not a cursor has moved from the position where
71589** it was last placed, or has been invalidated for any other reason.
71590** Cursors can move when the row they are pointing at is deleted out
71591** from under them, for example. Cursor might also move if a btree
71592** is rebalanced.
71593**
71594** Calling this routine with a NULL cursor pointer returns false.
71595**
71596** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
71597** back to where it ought to be if this routine returns true.
71598*/
71599SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
71600 assert( EIGHT_BYTE_ALIGNMENT(pCur)((void) (0))
71601 || pCur==sqlite3BtreeFakeValidCursor() )((void) (0));
71602 assert( offsetof(BtCursor, eState)==0 )((void) (0));
71603 assert( sizeof(pCur->eState)==1 )((void) (0));
71604 return CURSOR_VALID0 != *(u8*)pCur;
71605}
71606
71607/*
71608** Return a pointer to a fake BtCursor object that will always answer
71609** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
71610** cursor returned must not be used with any other Btree interface.
71611*/
71612SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void){
71613 static u8 fakeCursor = CURSOR_VALID0;
71614 assert( offsetof(BtCursor, eState)==0 )((void) (0));
71615 return (BtCursor*)&fakeCursor;
71616}
71617
71618/*
71619** This routine restores a cursor back to its original position after it
71620** has been moved by some outside activity (such as a btree rebalance or
71621** a row having been deleted out from under the cursor).
71622**
71623** On success, the *pDifferentRow parameter is false if the cursor is left
71624** pointing at exactly the same row. *pDifferntRow is the row the cursor
71625** was pointing to has been deleted, forcing the cursor to point to some
71626** nearby row.
71627**
71628** This routine should only be called for a cursor that just returned
71629** TRUE from sqlite3BtreeCursorHasMoved().
71630*/
71631SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
71632 int rc;
71633
71634 assert( pCur!=0 )((void) (0));
71635 assert( pCur->eState!=CURSOR_VALID )((void) (0));
71636 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
71637 if( rc ){
71638 *pDifferentRow = 1;
71639 return rc;
71640 }
71641 if( pCur->eState!=CURSOR_VALID0 ){
71642 *pDifferentRow = 1;
71643 }else{
71644 *pDifferentRow = 0;
71645 }
71646 return SQLITE_OK0;
71647}
71648
71649#ifdef SQLITE_ENABLE_CURSOR_HINTS
71650/*
71651** Provide hints to the cursor. The particular hint given (and the type
71652** and number of the varargs parameters) is determined by the eHintType
71653** parameter. See the definitions of the BTREE_HINT_* macros for details.
71654*/
71655SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
71656 /* Used only by system that substitute their own storage engine */
71657#ifdef SQLITE_DEBUG
71658 if( ALWAYS(eHintType==BTREE_HINT_RANGE)(eHintType==0) ){
71659 va_list ap;
71660 Expr *pExpr;
71661 Walker w;
71662 memset(&w, 0, sizeof(w));
71663 w.xExprCallback = sqlite3CursorRangeHintExprCheck;
71664 va_start(ap, eHintType)__builtin_va_start(ap, eHintType);
71665 pExpr = va_arg(ap, Expr*)__builtin_va_arg(ap, Expr*);
71666 w.u.aMem = va_arg(ap, Mem*)__builtin_va_arg(ap, Mem*);
71667 va_end(ap)__builtin_va_end(ap);
71668 assert( pExpr!=0 )((void) (0));
71669 assert( w.u.aMem!=0 )((void) (0));
71670 sqlite3WalkExpr(&w, pExpr);
71671 }
71672#endif /* SQLITE_DEBUG */
71673}
71674#endif /* SQLITE_ENABLE_CURSOR_HINTS */
71675
71676
71677/*
71678** Provide flag hints to the cursor.
71679*/
71680SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
71681 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 )((void) (0));
71682 pCur->hints = x;
71683}
71684
71685
71686#ifndef SQLITE_OMIT_AUTOVACUUM
71687/*
71688** Given a page number of a regular database page, return the page
71689** number for the pointer-map page that contains the entry for the
71690** input page number.
71691**
71692** Return 0 (not a valid page) for pgno==1 since there is
71693** no pointer map associated with page 1. The integrity_check logic
71694** requires that ptrmapPageno(*,1)!=1.
71695*/
71696static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
71697 int nPagesPerMapPage;
71698 Pgno iPtrMap, ret;
71699 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71700 if( pgno<2 ) return 0;
71701 nPagesPerMapPage = (pBt->usableSize/5)+1;
71702 iPtrMap = (pgno-2)/nPagesPerMapPage;
71703 ret = (iPtrMap*nPagesPerMapPage) + 2;
71704 if( ret==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
71705 ret++;
71706 }
71707 return ret;
71708}
71709
71710/*
71711** Write an entry into the pointer map.
71712**
71713** This routine updates the pointer map entry for page number 'key'
71714** so that it maps to type 'eType' and parent page number 'pgno'.
71715**
71716** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
71717** a no-op. If an error occurs, the appropriate error code is written
71718** into *pRC.
71719*/
71720static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
71721 DbPage *pDbPage; /* The pointer map page */
71722 u8 *pPtrmap; /* The pointer map data */
71723 Pgno iPtrmap; /* The pointer map page number */
71724 int offset; /* Offset in pointer map page */
71725 int rc; /* Return code from subfunctions */
71726
71727 if( *pRC ) return;
71728
71729 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71730 /* The super-journal page number must never be used as a pointer map page */
71731 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) )((void) (0));
71732
71733 assert( pBt->autoVacuum )((void) (0));
71734 if( key==0 ){
71735 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71735);
71736 return;
71737 }
71738 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
71739 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
71740 if( rc!=SQLITE_OK0 ){
71741 *pRC = rc;
71742 return;
71743 }
71744 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
71745 /* The first byte of the extra data is the MemPage.isInit byte.
71746 ** If that byte is set, it means this page is also being used
71747 ** as a btree page. */
71748 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71748);
71749 goto ptrmap_exit;
71750 }
71751 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
71752 if( offset<0 ){
71753 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71753);
71754 goto ptrmap_exit;
71755 }
71756 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
71757 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
71758
71759 if( eType!=pPtrmap[offset] || get4bytesqlite3Get4byte(&pPtrmap[offset+1])!=parent ){
71760 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
71761 *pRC= rc = sqlite3PagerWrite(pDbPage);
71762 if( rc==SQLITE_OK0 ){
71763 pPtrmap[offset] = eType;
71764 put4bytesqlite3Put4byte(&pPtrmap[offset+1], parent);
71765 }
71766 }
71767
71768ptrmap_exit:
71769 sqlite3PagerUnref(pDbPage);
71770}
71771
71772/*
71773** Read an entry from the pointer map.
71774**
71775** This routine retrieves the pointer map entry for page 'key', writing
71776** the type and parent page number to *pEType and *pPgno respectively.
71777** An error code is returned if something goes wrong, otherwise SQLITE_OK.
71778*/
71779static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
71780 DbPage *pDbPage; /* The pointer map page */
71781 int iPtrmap; /* Pointer map page index */
71782 u8 *pPtrmap; /* Pointer map page data */
71783 int offset; /* Offset of entry in pointer map */
71784 int rc;
71785
71786 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71787
71788 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
71789 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
71790 if( rc!=0 ){
71791 return rc;
71792 }
71793 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
71794
71795 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
71796 if( offset<0 ){
71797 sqlite3PagerUnref(pDbPage);
71798 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(71798);
71799 }
71800 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
71801 assert( pEType!=0 )((void) (0));
71802 *pEType = pPtrmap[offset];
71803 if( pPgno ) *pPgno = get4bytesqlite3Get4byte(&pPtrmap[offset+1]);
71804
71805 sqlite3PagerUnref(pDbPage);
71806 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap)sqlite3CorruptError(71806);
71807 return SQLITE_OK0;
71808}
71809
71810#else /* if defined SQLITE_OMIT_AUTOVACUUM */
71811 #define ptrmapPut(w,x,y,z,rc)
71812 #define ptrmapGet(w,x,y,z) SQLITE_OK0
71813 #define ptrmapPutOvflPtr(x, y, z, rc)
71814#endif
71815
71816/*
71817** Given a btree page and a cell index (0 means the first cell on
71818** the page, 1 means the second cell, and so forth) return a pointer
71819** to the cell content.
71820**
71821** findCellPastPtr() does the same except it skips past the initial
71822** 4-byte child pointer found on interior pages, if there is one.
71823**
71824** This routine works only for pages that do not contain overflow cells.
71825*/
71826#define findCell(P,I)((P)->aData + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
71827 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
71828#define findCellPastPtr(P,I)((P)->aDataOfst + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
71829 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
71830
71831
71832/*
71833** This is common tail processing for btreeParseCellPtr() and
71834** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
71835** on a single B-tree page. Make necessary adjustments to the CellInfo
71836** structure.
71837*/
71838static SQLITE_NOINLINE__attribute__((noinline)) void btreeParseCellAdjustSizeForOverflow(
71839 MemPage *pPage, /* Page containing the cell */
71840 u8 *pCell, /* Pointer to the cell text. */
71841 CellInfo *pInfo /* Fill in this structure */
71842){
71843 /* If the payload will not fit completely on the local page, we have
71844 ** to decide how much to store locally and how much to spill onto
71845 ** overflow pages. The strategy is to minimize the amount of unused
71846 ** space on overflow pages while keeping the amount of local storage
71847 ** in between minLocal and maxLocal.
71848 **
71849 ** Warning: changing the way overflow payload is distributed in any
71850 ** way will result in an incompatible file format.
71851 */
71852 int minLocal; /* Minimum amount of payload held locally */
71853 int maxLocal; /* Maximum amount of payload held locally */
71854 int surplus; /* Overflow payload available for local storage */
71855
71856 minLocal = pPage->minLocal;
71857 maxLocal = pPage->maxLocal;
71858 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
71859 testcase( surplus==maxLocal );
71860 testcase( surplus==maxLocal+1 );
71861 if( surplus <= maxLocal ){
71862 pInfo->nLocal = (u16)surplus;
71863 }else{
71864 pInfo->nLocal = (u16)minLocal;
71865 }
71866 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
71867}
71868
71869/*
71870** Given a record with nPayload bytes of payload stored within btree
71871** page pPage, return the number of bytes of payload stored locally.
71872*/
71873static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
71874 int maxLocal; /* Maximum amount of payload held locally */
71875 maxLocal = pPage->maxLocal;
71876 if( nPayload<=maxLocal ){
71877 return nPayload;
71878 }else{
71879 int minLocal; /* Minimum amount of payload held locally */
71880 int surplus; /* Overflow payload available for local storage */
71881 minLocal = pPage->minLocal;
71882 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
71883 return ( surplus <= maxLocal ) ? surplus : minLocal;
71884 }
71885}
71886
71887/*
71888** The following routines are implementations of the MemPage.xParseCell()
71889** method.
71890**
71891** Parse a cell content block and fill in the CellInfo structure.
71892**
71893** btreeParseCellPtr() => table btree leaf nodes
71894** btreeParseCellNoPayload() => table btree internal nodes
71895** btreeParseCellPtrIndex() => index btree nodes
71896**
71897** There is also a wrapper function btreeParseCell() that works for
71898** all MemPage types and that references the cell by index rather than
71899** by pointer.
71900*/
71901static void btreeParseCellPtrNoPayload(
71902 MemPage *pPage, /* Page containing the cell */
71903 u8 *pCell, /* Pointer to the cell text. */
71904 CellInfo *pInfo /* Fill in this structure */
71905){
71906 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
71907 assert( pPage->leaf==0 )((void) (0));
71908 assert( pPage->childPtrSize==4 )((void) (0));
71909#ifndef SQLITE_DEBUG
71910 UNUSED_PARAMETER(pPage)(void)(pPage);
71911#endif
71912 pInfo->nSize = 4 + getVarintsqlite3GetVarint(&pCell[4], (u64*)&pInfo->nKey);
71913 pInfo->nPayload = 0;
71914 pInfo->nLocal = 0;
71915 pInfo->pPayload = 0;
71916 return;
71917}
71918static void btreeParseCellPtr(
71919 MemPage *pPage, /* Page containing the cell */
71920 u8 *pCell, /* Pointer to the cell text. */
71921 CellInfo *pInfo /* Fill in this structure */
71922){
71923 u8 *pIter; /* For scanning through pCell */
71924 u32 nPayload; /* Number of bytes of cell payload */
71925 u64 iKey; /* Extracted Key value */
71926
71927 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
71928 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
71929 assert( pPage->intKeyLeaf )((void) (0));
71930 assert( pPage->childPtrSize==0 )((void) (0));
71931 pIter = pCell;
71932
71933 /* The next block of code is equivalent to:
71934 **
71935 ** pIter += getVarint32(pIter, nPayload);
71936 **
71937 ** The code is inlined to avoid a function call.
71938 */
71939 nPayload = *pIter;
71940 if( nPayload>=0x80 ){
71941 u8 *pEnd = &pIter[8];
71942 nPayload &= 0x7f;
71943 do{
71944 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
71945 }while( (*pIter)>=0x80 && pIter<pEnd );
71946 }
71947 pIter++;
71948
71949 /* The next block of code is equivalent to:
71950 **
71951 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
71952 **
71953 ** The code is inlined and the loop is unrolled for performance.
71954 ** This routine is a high-runner.
71955 */
71956 iKey = *pIter;
71957 if( iKey>=0x80 ){
71958 u8 x;
71959 iKey = (iKey<<7) ^ (x = *++pIter);
71960 if( x>=0x80 ){
71961 iKey = (iKey<<7) ^ (x = *++pIter);
71962 if( x>=0x80 ){
71963 iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter);
71964 if( x>=0x80 ){
71965 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
71966 if( x>=0x80 ){
71967 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
71968 if( x>=0x80 ){
71969 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
71970 if( x>=0x80 ){
71971 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
71972 if( x>=0x80 ){
71973 iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter);
71974 }
71975 }
71976 }
71977 }
71978 }
71979 }else{
71980 iKey ^= 0x204000;
71981 }
71982 }else{
71983 iKey ^= 0x4000;
71984 }
71985 }
71986 pIter++;
71987
71988 pInfo->nKey = *(i64*)&iKey;
71989 pInfo->nPayload = nPayload;
71990 pInfo->pPayload = pIter;
71991 testcase( nPayload==pPage->maxLocal );
71992 testcase( nPayload==(u32)pPage->maxLocal+1 );
71993 if( nPayload<=pPage->maxLocal ){
71994 /* This is the (easy) common case where the entire payload fits
71995 ** on the local page. No overflow is required.
71996 */
71997 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71998 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71999 pInfo->nLocal = (u16)nPayload;
72000 }else{
72001 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72002 }
72003}
72004static void btreeParseCellPtrIndex(
72005 MemPage *pPage, /* Page containing the cell */
72006 u8 *pCell, /* Pointer to the cell text. */
72007 CellInfo *pInfo /* Fill in this structure */
72008){
72009 u8 *pIter; /* For scanning through pCell */
72010 u32 nPayload; /* Number of bytes of cell payload */
72011
72012 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72013 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
72014 assert( pPage->intKeyLeaf==0 )((void) (0));
72015 pIter = pCell + pPage->childPtrSize;
72016 nPayload = *pIter;
72017 if( nPayload>=0x80 ){
72018 u8 *pEnd = &pIter[8];
72019 nPayload &= 0x7f;
72020 do{
72021 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
72022 }while( *(pIter)>=0x80 && pIter<pEnd );
72023 }
72024 pIter++;
72025 pInfo->nKey = nPayload;
72026 pInfo->nPayload = nPayload;
72027 pInfo->pPayload = pIter;
72028 testcase( nPayload==pPage->maxLocal );
72029 testcase( nPayload==(u32)pPage->maxLocal+1 );
72030 if( nPayload<=pPage->maxLocal ){
72031 /* This is the (easy) common case where the entire payload fits
72032 ** on the local page. No overflow is required.
72033 */
72034 pInfo->nSize = nPayload + (u16)(pIter - pCell);
72035 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72036 pInfo->nLocal = (u16)nPayload;
72037 }else{
72038 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72039 }
72040}
72041static void btreeParseCell(
72042 MemPage *pPage, /* Page containing the cell */
72043 int iCell, /* The cell index. First cell is 0 */
72044 CellInfo *pInfo /* Fill in this structure */
72045){
72046 pPage->xParseCell(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
, pInfo);
72047}
72048
72049/*
72050** The following routines are implementations of the MemPage.xCellSize
72051** method.
72052**
72053** Compute the total number of bytes that a Cell needs in the cell
72054** data area of the btree-page. The return number includes the cell
72055** data header and the local payload, but not any overflow page or
72056** the space used by the cell pointer.
72057**
72058** cellSizePtrNoPayload() => table internal nodes
72059** cellSizePtrTableLeaf() => table leaf nodes
72060** cellSizePtr() => index internal nodes
72061** cellSizeIdxLeaf() => index leaf nodes
72062*/
72063static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
72064 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
72065 u8 *pEnd; /* End mark for a varint */
72066 u32 nSize; /* Size value to return */
72067
72068#ifdef SQLITE_DEBUG
72069 /* The value returned by this function should always be the same as
72070 ** the (CellInfo.nSize) value found by doing a full parse of the
72071 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
72072 ** this function verifies that this invariant is not violated. */
72073 CellInfo debuginfo;
72074 pPage->xParseCell(pPage, pCell, &debuginfo);
72075#endif
72076
72077 assert( pPage->childPtrSize==4 )((void) (0));
72078 nSize = *pIter;
72079 if( nSize>=0x80 ){
72080 pEnd = &pIter[8];
72081 nSize &= 0x7f;
72082 do{
72083 nSize = (nSize<<7) | (*++pIter & 0x7f);
72084 }while( *(pIter)>=0x80 && pIter<pEnd );
72085 }
72086 pIter++;
72087 testcase( nSize==pPage->maxLocal );
72088 testcase( nSize==(u32)pPage->maxLocal+1 );
72089 if( nSize<=pPage->maxLocal ){
72090 nSize += (u32)(pIter - pCell);
72091 assert( nSize>4 )((void) (0));
72092 }else{
72093 int minLocal = pPage->minLocal;
72094 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72095 testcase( nSize==pPage->maxLocal );
72096 testcase( nSize==(u32)pPage->maxLocal+1 );
72097 if( nSize>pPage->maxLocal ){
72098 nSize = minLocal;
72099 }
72100 nSize += 4 + (u16)(pIter - pCell);
72101 }
72102 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
72103 return (u16)nSize;
72104}
72105static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){
72106 u8 *pIter = pCell; /* For looping over bytes of pCell */
72107 u8 *pEnd; /* End mark for a varint */
72108 u32 nSize; /* Size value to return */
72109
72110#ifdef SQLITE_DEBUG
72111 /* The value returned by this function should always be the same as
72112 ** the (CellInfo.nSize) value found by doing a full parse of the
72113 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
72114 ** this function verifies that this invariant is not violated. */
72115 CellInfo debuginfo;
72116 pPage->xParseCell(pPage, pCell, &debuginfo);
72117#endif
72118
72119 assert( pPage->childPtrSize==0 )((void) (0));
72120 nSize = *pIter;
72121 if( nSize>=0x80 ){
72122 pEnd = &pIter[8];
72123 nSize &= 0x7f;
72124 do{
72125 nSize = (nSize<<7) | (*++pIter & 0x7f);
72126 }while( *(pIter)>=0x80 && pIter<pEnd );
72127 }
72128 pIter++;
72129 testcase( nSize==pPage->maxLocal );
72130 testcase( nSize==(u32)pPage->maxLocal+1 );
72131 if( nSize<=pPage->maxLocal ){
72132 nSize += (u32)(pIter - pCell);
72133 if( nSize<4 ) nSize = 4;
72134 }else{
72135 int minLocal = pPage->minLocal;
72136 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72137 testcase( nSize==pPage->maxLocal );
72138 testcase( nSize==(u32)pPage->maxLocal+1 );
72139 if( nSize>pPage->maxLocal ){
72140 nSize = minLocal;
72141 }
72142 nSize += 4 + (u16)(pIter - pCell);
72143 }
72144 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
72145 return (u16)nSize;
72146}
72147static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
72148 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
72149 u8 *pEnd; /* End mark for a varint */
72150
72151#ifdef SQLITE_DEBUG
72152 /* The value returned by this function should always be the same as
72153 ** the (CellInfo.nSize) value found by doing a full parse of the
72154 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
72155 ** this function verifies that this invariant is not violated. */
72156 CellInfo debuginfo;
72157 pPage->xParseCell(pPage, pCell, &debuginfo);
72158#else
72159 UNUSED_PARAMETER(pPage)(void)(pPage);
72160#endif
72161
72162 assert( pPage->childPtrSize==4 )((void) (0));
72163 pEnd = pIter + 9;
72164 while( (*pIter++)&0x80 && pIter<pEnd );
72165 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB )((void) (0));
72166 return (u16)(pIter - pCell);
72167}
72168static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
72169 u8 *pIter = pCell; /* For looping over bytes of pCell */
72170 u8 *pEnd; /* End mark for a varint */
72171 u32 nSize; /* Size value to return */
72172
72173#ifdef SQLITE_DEBUG
72174 /* The value returned by this function should always be the same as
72175 ** the (CellInfo.nSize) value found by doing a full parse of the
72176 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
72177 ** this function verifies that this invariant is not violated. */
72178 CellInfo debuginfo;
72179 pPage->xParseCell(pPage, pCell, &debuginfo);
72180#endif
72181
72182 nSize = *pIter;
72183 if( nSize>=0x80 ){
72184 pEnd = &pIter[8];
72185 nSize &= 0x7f;
72186 do{
72187 nSize = (nSize<<7) | (*++pIter & 0x7f);
72188 }while( *(pIter)>=0x80 && pIter<pEnd );
72189 }
72190 pIter++;
72191 /* pIter now points at the 64-bit integer key value, a variable length
72192 ** integer. The following block moves pIter to point at the first byte
72193 ** past the end of the key value. */
72194 if( (*pIter++)&0x80
72195 && (*pIter++)&0x80
72196 && (*pIter++)&0x80
72197 && (*pIter++)&0x80
72198 && (*pIter++)&0x80
72199 && (*pIter++)&0x80
72200 && (*pIter++)&0x80
72201 && (*pIter++)&0x80 ){ pIter++; }
72202 testcase( nSize==pPage->maxLocal );
72203 testcase( nSize==(u32)pPage->maxLocal+1 );
72204 if( nSize<=pPage->maxLocal ){
72205 nSize += (u32)(pIter - pCell);
72206 if( nSize<4 ) nSize = 4;
72207 }else{
72208 int minLocal = pPage->minLocal;
72209 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72210 testcase( nSize==pPage->maxLocal );
72211 testcase( nSize==(u32)pPage->maxLocal+1 );
72212 if( nSize>pPage->maxLocal ){
72213 nSize = minLocal;
72214 }
72215 nSize += 4 + (u16)(pIter - pCell);
72216 }
72217 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
72218 return (u16)nSize;
72219}
72220
72221
72222#ifdef SQLITE_DEBUG
72223/* This variation on cellSizePtr() is used inside of assert() statements
72224** only. */
72225static u16 cellSize(MemPage *pPage, int iCell){
72226 return pPage->xCellSize(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
);
72227}
72228#endif
72229
72230#ifndef SQLITE_OMIT_AUTOVACUUM
72231/*
72232** The cell pCell is currently part of page pSrc but will ultimately be part
72233** of pPage. (pSrc and pPage are often the same.) If pCell contains a
72234** pointer to an overflow page, insert an entry into the pointer-map for
72235** the overflow page that will be valid after pCell has been moved to pPage.
72236*/
72237static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
72238 CellInfo info;
72239 if( *pRC ) return;
72240 assert( pCell!=0 )((void) (0));
72241 pPage->xParseCell(pPage, pCell, &info);
72242 if( info.nLocal<info.nPayload ){
72243 Pgno ovfl;
72244 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal)(((uptr)(pCell)<(uptr)(pSrc->aDataEnd))&&((uptr
)(pCell+info.nLocal)>(uptr)(pSrc->aDataEnd)))
){
72245 testcase( pSrc!=pPage );
72246 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72246);
72247 return;
72248 }
72249 ovfl = get4bytesqlite3Get4byte(&pCell[info.nSize-4]);
72250 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, pRC);
72251 }
72252}
72253#endif
72254
72255
72256/*
72257** Defragment the page given. This routine reorganizes cells within the
72258** page so that there are no free-blocks on the free-block list.
72259**
72260** Parameter nMaxFrag is the maximum amount of fragmented space that may be
72261** present in the page after this routine returns.
72262**
72263** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
72264** b-tree page so that there are no freeblocks or fragment bytes, all
72265** unused bytes are contained in the unallocated space region, and all
72266** cells are packed tightly at the end of the page.
72267*/
72268static int defragmentPage(MemPage *pPage, int nMaxFrag){
72269 int i; /* Loop counter */
72270 int pc; /* Address of the i-th cell */
72271 int hdr; /* Offset to the page header */
72272 int size; /* Size of a cell */
72273 int usableSize; /* Number of usable bytes on a page */
72274 int cellOffset; /* Offset to the cell pointer array */
72275 int cbrk; /* Offset to the cell content area */
72276 int nCell; /* Number of cells on the page */
72277 unsigned char *data; /* The page data */
72278 unsigned char *temp; /* Temp area for cell content */
72279 unsigned char *src; /* Source of content */
72280 int iCellFirst; /* First allowable cell index */
72281 int iCellLast; /* Last possible cell index */
72282 int iCellStart; /* First cell offset in input */
72283
72284 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
72285 assert( pPage->pBt!=0 )((void) (0));
72286 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE )((void) (0));
72287 assert( pPage->nOverflow==0 )((void) (0));
72288 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72289 data = pPage->aData;
72290 hdr = pPage->hdrOffset;
72291 cellOffset = pPage->cellOffset;
72292 nCell = pPage->nCell;
72293 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB )((void) (0));
72294 iCellFirst = cellOffset + 2*nCell;
72295 usableSize = pPage->pBt->usableSize;
72296
72297 /* This block handles pages with two or fewer free blocks and nMaxFrag
72298 ** or fewer fragmented bytes. In this case it is faster to move the
72299 ** two (or one) blocks of cells using memmove() and add the required
72300 ** offsets to each pointer in the cell-pointer array than it is to
72301 ** reconstruct the entire page. */
72302 if( (int)data[hdr+7]<=nMaxFrag ){
72303 int iFree = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
72304 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72304);
72305 if( iFree ){
72306 int iFree2 = get2byte(&data[iFree])((&data[iFree])[0]<<8 | (&data[iFree])[1]);
72307 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72307);
72308 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
72309 u8 *pEnd = &data[cellOffset + nCell*2];
72310 u8 *pAddr;
72311 int sz2 = 0;
72312 int sz = get2byte(&data[iFree+2])((&data[iFree+2])[0]<<8 | (&data[iFree+2])[1]);
72313 int top = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
72314 if( top>=iFree ){
72315 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72315);
72316 }
72317 if( iFree2 ){
72318 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72318);
72319 sz2 = get2byte(&data[iFree2+2])((&data[iFree2+2])[0]<<8 | (&data[iFree2+2])[1]
)
;
72320 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72320);
72321 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
72322 sz += sz2;
72323 }else if( iFree+sz>usableSize ){
72324 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72324);
72325 }
72326
72327 cbrk = top+sz;
72328 assert( cbrk+(iFree-top) <= usableSize )((void) (0));
72329 memmove(&data[cbrk], &data[top], iFree-top);
72330 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
72331 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
72332 if( pc<iFree ){ put2byte(pAddr, pc+sz)((pAddr)[0] = (u8)((pc+sz)>>8), (pAddr)[1] = (u8)(pc+sz
))
; }
72333 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2)((pAddr)[0] = (u8)((pc+sz2)>>8), (pAddr)[1] = (u8)(pc+sz2
))
; }
72334 }
72335 goto defragment_out;
72336 }
72337 }
72338 }
72339
72340 cbrk = usableSize;
72341 iCellLast = usableSize - 4;
72342 iCellStart = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
72343 if( nCell>0 ){
72344 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
72345 memcpy(temp, data, usableSize);
72346 src = temp;
72347 for(i=0; i<nCell; i++){
72348 u8 *pAddr; /* The i-th cell pointer */
72349 pAddr = &data[cellOffset + i*2];
72350 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
72351 testcase( pc==iCellFirst );
72352 testcase( pc==iCellLast );
72353 /* These conditions have already been verified in btreeInitPage()
72354 ** if PRAGMA cell_size_check=ON.
72355 */
72356 if( pc>iCellLast ){
72357 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72357);
72358 }
72359 assert( pc>=0 && pc<=iCellLast )((void) (0));
72360 size = pPage->xCellSize(pPage, &src[pc]);
72361 cbrk -= size;
72362 if( cbrk<iCellStart || pc+size>usableSize ){
72363 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72363);
72364 }
72365 assert( cbrk+size<=usableSize && cbrk>=iCellStart )((void) (0));
72366 testcase( cbrk+size==usableSize );
72367 testcase( pc+size==usableSize );
72368 put2byte(pAddr, cbrk)((pAddr)[0] = (u8)((cbrk)>>8), (pAddr)[1] = (u8)(cbrk));
72369 memcpy(&data[cbrk], &src[pc], size);
72370 }
72371 }
72372 data[hdr+7] = 0;
72373
72374defragment_out:
72375 assert( pPage->nFree>=0 )((void) (0));
72376 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
72377 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72377);
72378 }
72379 assert( cbrk>=iCellFirst )((void) (0));
72380 put2byte(&data[hdr+5], cbrk)((&data[hdr+5])[0] = (u8)((cbrk)>>8), (&data[hdr
+5])[1] = (u8)(cbrk))
;
72381 data[hdr+1] = 0;
72382 data[hdr+2] = 0;
72383 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
72384 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
72385 return SQLITE_OK0;
72386}
72387
72388/*
72389** Search the free-list on page pPg for space to store a cell nByte bytes in
72390** size. If one can be found, return a pointer to the space and remove it
72391** from the free-list.
72392**
72393** If no suitable space can be found on the free-list, return NULL.
72394**
72395** This function may detect corruption within pPg. If corruption is
72396** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
72397**
72398** Slots on the free list that are between 1 and 3 bytes larger than nByte
72399** will be ignored if adding the extra space to the fragmentation count
72400** causes the fragmentation count to exceed 60.
72401*/
72402static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
72403 const int hdr = pPg->hdrOffset; /* Offset to page header */
72404 u8 * const aData = pPg->aData; /* Page data */
72405 int iAddr = hdr + 1; /* Address of ptr to pc */
72406 u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */
72407 int pc = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]); /* Address of a free slot */
72408 int x; /* Excess size of the slot */
72409 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
72410 int size; /* Size of the free slot */
72411
72412 assert( pc>0 )((void) (0));
72413 while( pc<=maxPC ){
72414 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
72415 ** freeblock form a big-endian integer which is the size of the freeblock
72416 ** in bytes, including the 4-byte header. */
72417 pTmp = &aData[pc+2];
72418 size = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
72419 if( (x = size - nByte)>=0 ){
72420 testcase( x==4 );
72421 testcase( x==3 );
72422 if( x<4 ){
72423 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
72424 ** number of bytes in fragments may not exceed 60. */
72425 if( aData[hdr+7]>57 ) return 0;
72426
72427 /* Remove the slot from the free-list. Update the number of
72428 ** fragmented bytes within the page. */
72429 memcpy(&aData[iAddr], &aData[pc], 2);
72430 aData[hdr+7] += (u8)x;
72431 return &aData[pc];
72432 }else if( x+pc > maxPC ){
72433 /* This slot extends off the end of the usable part of the page */
72434 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(72434);
72435 return 0;
72436 }else{
72437 /* The slot remains on the free-list. Reduce its size to account
72438 ** for the portion used by the new allocation. */
72439 put2byte(&aData[pc+2], x)((&aData[pc+2])[0] = (u8)((x)>>8), (&aData[pc+2
])[1] = (u8)(x))
;
72440 }
72441 return &aData[pc + x];
72442 }
72443 iAddr = pc;
72444 pTmp = &aData[pc];
72445 pc = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
72446 if( pc<=iAddr ){
72447 if( pc ){
72448 /* The next slot in the chain comes before the current slot */
72449 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(72449);
72450 }
72451 return 0;
72452 }
72453 }
72454 if( pc>maxPC+nByte-4 ){
72455 /* The free slot chain extends off the end of the page */
72456 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(72456);
72457 }
72458 return 0;
72459}
72460
72461/*
72462** Allocate nByte bytes of space from within the B-Tree page passed
72463** as the first argument. Write into *pIdx the index into pPage->aData[]
72464** of the first byte of allocated space. Return either SQLITE_OK or
72465** an error code (usually SQLITE_CORRUPT).
72466**
72467** The caller guarantees that there is sufficient space to make the
72468** allocation. This routine might need to defragment in order to bring
72469** all the space together, however. This routine will avoid using
72470** the first two bytes past the cell pointer area since presumably this
72471** allocation is being made in order to insert a new cell, so we will
72472** also end up needing a new cell pointer.
72473*/
72474static SQLITE_INLINE__attribute__((always_inline)) inline int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
72475 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
72476 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
72477 int top; /* First byte of cell content area */
72478 int rc = SQLITE_OK0; /* Integer return code */
72479 u8 *pTmp; /* Temp ptr into data[] */
72480 int gap; /* First byte of gap between cell pointers and cell content */
72481
72482 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
72483 assert( pPage->pBt )((void) (0));
72484 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72485 assert( nByte>=0 )((void) (0)); /* Minimum cell size is 4 */
72486 assert( pPage->nFree>=nByte )((void) (0));
72487 assert( pPage->nOverflow==0 )((void) (0));
72488 assert( nByte < (int)(pPage->pBt->usableSize-8) )((void) (0));
72489
72490 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf )((void) (0));
72491 gap = pPage->cellOffset + 2*pPage->nCell;
72492 assert( gap<=65536 )((void) (0));
72493 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
72494 ** and the reserved space is zero (the usual value for reserved space)
72495 ** then the cell content offset of an empty page wants to be 65536.
72496 ** However, that integer is too large to be stored in a 2-byte unsigned
72497 ** integer, so a value of 0 is used in its place. */
72498 pTmp = &data[hdr+5];
72499 top = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
72500 if( gap>top ){
72501 if( top==0 && pPage->pBt->usableSize==65536 ){
72502 top = 65536;
72503 }else{
72504 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72504);
72505 }
72506 }else if( top>(int)pPage->pBt->usableSize ){
72507 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72507);
72508 }
72509
72510 /* If there is enough space between gap and top for one more cell pointer,
72511 ** and if the freelist is not empty, then search the
72512 ** freelist looking for a slot big enough to satisfy the request.
72513 */
72514 testcase( gap+2==top );
72515 testcase( gap+1==top );
72516 testcase( gap==top );
72517 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
72518 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
72519 if( pSpace ){
72520 int g2;
72521 assert( pSpace+nByte<=data+pPage->pBt->usableSize )((void) (0));
72522 *pIdx = g2 = (int)(pSpace-data);
72523 if( g2<=gap ){
72524 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72524);
72525 }else{
72526 return SQLITE_OK0;
72527 }
72528 }else if( rc ){
72529 return rc;
72530 }
72531 }
72532
72533 /* The request could not be fulfilled using a freelist slot. Check
72534 ** to see if defragmentation is necessary.
72535 */
72536 testcase( gap+2+nByte==top );
72537 if( gap+2+nByte>top ){
72538 assert( pPage->nCell>0 || CORRUPT_DB )((void) (0));
72539 assert( pPage->nFree>=0 )((void) (0));
72540 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))((4)<(pPage->nFree - (2+nByte))?(4):(pPage->nFree - (
2+nByte)))
);
72541 if( rc ) return rc;
72542 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
72543 assert( gap+2+nByte<=top )((void) (0));
72544 }
72545
72546
72547 /* Allocate memory from the gap in between the cell pointer array
72548 ** and the cell content area. The btreeComputeFreeSpace() call has already
72549 ** validated the freelist. Given that the freelist is valid, there
72550 ** is no way that the allocation can extend off the end of the page.
72551 ** The assert() below verifies the previous sentence.
72552 */
72553 top -= nByte;
72554 put2byte(&data[hdr+5], top)((&data[hdr+5])[0] = (u8)((top)>>8), (&data[hdr
+5])[1] = (u8)(top))
;
72555 assert( top+nByte <= (int)pPage->pBt->usableSize )((void) (0));
72556 *pIdx = top;
72557 return SQLITE_OK0;
72558}
72559
72560/*
72561** Return a section of the pPage->aData to the freelist.
72562** The first byte of the new free block is pPage->aData[iStart]
72563** and the size of the block is iSize bytes.
72564**
72565** Adjacent freeblocks are coalesced.
72566**
72567** Even though the freeblock list was checked by btreeComputeFreeSpace(),
72568** that routine will not detect overlap between cells or freeblocks. Nor
72569** does it detect cells or freeblocks that encroach into the reserved bytes
72570** at the end of the page. So do additional corruption checks inside this
72571** routine and return SQLITE_CORRUPT if any problems are found.
72572*/
72573static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
72574 u16 iPtr; /* Address of ptr to next freeblock */
72575 u16 iFreeBlk; /* Address of the next freeblock */
72576 u8 hdr; /* Page header size. 0 or 100 */
72577 u8 nFrag = 0; /* Reduction in fragmentation */
72578 u16 iOrigSize = iSize; /* Original value of iSize */
72579 u16 x; /* Offset to cell content area */
72580 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
72581 unsigned char *data = pPage->aData; /* Page content */
72582 u8 *pTmp; /* Temporary ptr into data[] */
72583
72584 assert( pPage->pBt!=0 )((void) (0));
72585 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
72586 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize )((void) (0));
72587 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize )((void) (0));
72588 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72589 assert( iSize>=4 )((void) (0)); /* Minimum cell size is 4 */
72590 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 )((void) (0));
72591
72592 /* The list of freeblocks must be in ascending order. Find the
72593 ** spot on the list where iStart should be inserted.
72594 */
72595 hdr = pPage->hdrOffset;
72596 iPtr = hdr + 1;
72597 if( data[iPtr+1]==0 && data[iPtr]==0 ){
72598 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
72599 }else{
72600 while( (iFreeBlk = get2byte(&data[iPtr])((&data[iPtr])[0]<<8 | (&data[iPtr])[1]))<iStart ){
72601 if( iFreeBlk<=iPtr ){
72602 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
72603 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72603);
72604 }
72605 iPtr = iFreeBlk;
72606 }
72607 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
72608 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72608);
72609 }
72610 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB )((void) (0));
72611
72612 /* At this point:
72613 ** iFreeBlk: First freeblock after iStart, or zero if none
72614 ** iPtr: The address of a pointer to iFreeBlk
72615 **
72616 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
72617 */
72618 if( iFreeBlk && iEnd+3>=iFreeBlk ){
72619 nFrag = iFreeBlk - iEnd;
72620 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72620);
72621 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2])((&data[iFreeBlk+2])[0]<<8 | (&data[iFreeBlk+2]
)[1])
;
72622 if( iEnd > pPage->pBt->usableSize ){
72623 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72623);
72624 }
72625 iSize = iEnd - iStart;
72626 iFreeBlk = get2byte(&data[iFreeBlk])((&data[iFreeBlk])[0]<<8 | (&data[iFreeBlk])[1]
)
;
72627 }
72628
72629 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
72630 ** pointer in the page header) then check to see if iStart should be
72631 ** coalesced onto the end of iPtr.
72632 */
72633 if( iPtr>hdr+1 ){
72634 int iPtrEnd = iPtr + get2byte(&data[iPtr+2])((&data[iPtr+2])[0]<<8 | (&data[iPtr+2])[1]);
72635 if( iPtrEnd+3>=iStart ){
72636 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72636);
72637 nFrag += iStart - iPtrEnd;
72638 iSize = iEnd - iPtr;
72639 iStart = iPtr;
72640 }
72641 }
72642 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72642);
72643 data[hdr+7] -= nFrag;
72644 }
72645 pTmp = &data[hdr+5];
72646 x = get2byte(pTmp)((pTmp)[0]<<8 | (pTmp)[1]);
72647 if( pPage->pBt->btsFlags & BTS_FAST_SECURE0x000c ){
72648 /* Overwrite deleted information with zeros when the secure_delete
72649 ** option is enabled */
72650 memset(&data[iStart], 0, iSize);
72651 }
72652 if( iStart<=x ){
72653 /* The new freeblock is at the beginning of the cell content area,
72654 ** so just extend the cell content area rather than create another
72655 ** freelist entry */
72656 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72656);
72657 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72657);
72658 put2byte(&data[hdr+1], iFreeBlk)((&data[hdr+1])[0] = (u8)((iFreeBlk)>>8), (&data
[hdr+1])[1] = (u8)(iFreeBlk))
;
72659 put2byte(&data[hdr+5], iEnd)((&data[hdr+5])[0] = (u8)((iEnd)>>8), (&data[hdr
+5])[1] = (u8)(iEnd))
;
72660 }else{
72661 /* Insert the new freeblock into the freelist */
72662 put2byte(&data[iPtr], iStart)((&data[iPtr])[0] = (u8)((iStart)>>8), (&data[iPtr
])[1] = (u8)(iStart))
;
72663 put2byte(&data[iStart], iFreeBlk)((&data[iStart])[0] = (u8)((iFreeBlk)>>8), (&data
[iStart])[1] = (u8)(iFreeBlk))
;
72664 put2byte(&data[iStart+2], iSize)((&data[iStart+2])[0] = (u8)((iSize)>>8), (&data
[iStart+2])[1] = (u8)(iSize))
;
72665 }
72666 pPage->nFree += iOrigSize;
72667 return SQLITE_OK0;
72668}
72669
72670/*
72671** Decode the flags byte (the first byte of the header) for a page
72672** and initialize fields of the MemPage structure accordingly.
72673**
72674** Only the following combinations are supported. Anything different
72675** indicates a corrupt database files:
72676**
72677** PTF_ZERODATA (0x02, 2)
72678** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
72679** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
72680** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
72681*/
72682static int decodeFlags(MemPage *pPage, int flagByte){
72683 BtShared *pBt; /* A copy of pPage->pBt */
72684
72685 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) )((void) (0));
72686 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72687 pBt = pPage->pBt;
72688 pPage->max1bytePayload = pBt->max1bytePayload;
72689 if( flagByte>=(PTF_ZERODATA0x02 | PTF_LEAF0x08) ){
72690 pPage->childPtrSize = 0;
72691 pPage->leaf = 1;
72692 if( flagByte==(PTF_LEAFDATA0x04 | PTF_INTKEY0x01 | PTF_LEAF0x08) ){
72693 pPage->intKeyLeaf = 1;
72694 pPage->xCellSize = cellSizePtrTableLeaf;
72695 pPage->xParseCell = btreeParseCellPtr;
72696 pPage->intKey = 1;
72697 pPage->maxLocal = pBt->maxLeaf;
72698 pPage->minLocal = pBt->minLeaf;
72699 }else if( flagByte==(PTF_ZERODATA0x02 | PTF_LEAF0x08) ){
72700 pPage->intKey = 0;
72701 pPage->intKeyLeaf = 0;
72702 pPage->xCellSize = cellSizePtrIdxLeaf;
72703 pPage->xParseCell = btreeParseCellPtrIndex;
72704 pPage->maxLocal = pBt->maxLocal;
72705 pPage->minLocal = pBt->minLocal;
72706 }else{
72707 pPage->intKey = 0;
72708 pPage->intKeyLeaf = 0;
72709 pPage->xCellSize = cellSizePtrIdxLeaf;
72710 pPage->xParseCell = btreeParseCellPtrIndex;
72711 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72711);
72712 }
72713 }else{
72714 pPage->childPtrSize = 4;
72715 pPage->leaf = 0;
72716 if( flagByte==(PTF_ZERODATA0x02) ){
72717 pPage->intKey = 0;
72718 pPage->intKeyLeaf = 0;
72719 pPage->xCellSize = cellSizePtr;
72720 pPage->xParseCell = btreeParseCellPtrIndex;
72721 pPage->maxLocal = pBt->maxLocal;
72722 pPage->minLocal = pBt->minLocal;
72723 }else if( flagByte==(PTF_LEAFDATA0x04 | PTF_INTKEY0x01) ){
72724 pPage->intKeyLeaf = 0;
72725 pPage->xCellSize = cellSizePtrNoPayload;
72726 pPage->xParseCell = btreeParseCellPtrNoPayload;
72727 pPage->intKey = 1;
72728 pPage->maxLocal = pBt->maxLeaf;
72729 pPage->minLocal = pBt->minLeaf;
72730 }else{
72731 pPage->intKey = 0;
72732 pPage->intKeyLeaf = 0;
72733 pPage->xCellSize = cellSizePtr;
72734 pPage->xParseCell = btreeParseCellPtrIndex;
72735 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72735);
72736 }
72737 }
72738 return SQLITE_OK0;
72739}
72740
72741/*
72742** Compute the amount of freespace on the page. In other words, fill
72743** in the pPage->nFree field.
72744*/
72745static int btreeComputeFreeSpace(MemPage *pPage){
72746 int pc; /* Address of a freeblock within pPage->aData[] */
72747 u8 hdr; /* Offset to beginning of page header */
72748 u8 *data; /* Equal to pPage->aData */
72749 int usableSize; /* Amount of usable space on each page */
72750 int nFree; /* Number of unused bytes on the page */
72751 int top; /* First byte of the cell content area */
72752 int iCellFirst; /* First allowable cell or freeblock offset */
72753 int iCellLast; /* Last possible cell or freeblock offset */
72754
72755 assert( pPage->pBt!=0 )((void) (0));
72756 assert( pPage->pBt->db!=0 )((void) (0));
72757 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72758 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
72759 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
72760 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
72761 assert( pPage->isInit==1 )((void) (0));
72762 assert( pPage->nFree<0 )((void) (0));
72763
72764 usableSize = pPage->pBt->usableSize;
72765 hdr = pPage->hdrOffset;
72766 data = pPage->aData;
72767 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
72768 ** the start of the cell content area. A zero value for this integer is
72769 ** interpreted as 65536. */
72770 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
72771 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
72772 iCellLast = usableSize - 4;
72773
72774 /* Compute the total free space on the page
72775 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
72776 ** start of the first freeblock on the page, or is zero if there are no
72777 ** freeblocks. */
72778 pc = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
72779 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
72780 if( pc>0 ){
72781 u32 next, size;
72782 if( pc<top ){
72783 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
72784 ** always be at least one cell before the first freeblock.
72785 */
72786 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72786);
72787 }
72788 while( 1 ){
72789 if( pc>iCellLast ){
72790 /* Freeblock off the end of the page */
72791 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72791);
72792 }
72793 next = get2byte(&data[pc])((&data[pc])[0]<<8 | (&data[pc])[1]);
72794 size = get2byte(&data[pc+2])((&data[pc+2])[0]<<8 | (&data[pc+2])[1]);
72795 nFree = nFree + size;
72796 if( next<=pc+size+3 ) break;
72797 pc = next;
72798 }
72799 if( next>0 ){
72800 /* Freeblock not in ascending order */
72801 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72801);
72802 }
72803 if( pc+size>(unsigned int)usableSize ){
72804 /* Last freeblock extends past page end */
72805 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72805);
72806 }
72807 }
72808
72809 /* At this point, nFree contains the sum of the offset to the start
72810 ** of the cell-content area plus the number of free bytes within
72811 ** the cell-content area. If this is greater than the usable-size
72812 ** of the page, then the page must be corrupted. This check also
72813 ** serves to verify that the offset to the start of the cell-content
72814 ** area, according to the page header, lies within the page.
72815 */
72816 if( nFree>usableSize || nFree<iCellFirst ){
72817 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72817);
72818 }
72819 pPage->nFree = (u16)(nFree - iCellFirst);
72820 return SQLITE_OK0;
72821}
72822
72823/*
72824** Do additional sanity check after btreeInitPage() if
72825** PRAGMA cell_size_check=ON
72826*/
72827static SQLITE_NOINLINE__attribute__((noinline)) int btreeCellSizeCheck(MemPage *pPage){
72828 int iCellFirst; /* First allowable cell or freeblock offset */
72829 int iCellLast; /* Last possible cell or freeblock offset */
72830 int i; /* Index into the cell pointer array */
72831 int sz; /* Size of a cell */
72832 int pc; /* Address of a freeblock within pPage->aData[] */
72833 u8 *data; /* Equal to pPage->aData */
72834 int usableSize; /* Maximum usable space on the page */
72835 int cellOffset; /* Start of cell content area */
72836
72837 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
72838 usableSize = pPage->pBt->usableSize;
72839 iCellLast = usableSize - 4;
72840 data = pPage->aData;
72841 cellOffset = pPage->cellOffset;
72842 if( !pPage->leaf ) iCellLast--;
72843 for(i=0; i<pPage->nCell; i++){
72844 pc = get2byteAligned(&data[cellOffset+i*2])((&data[cellOffset+i*2])[0]<<8 | (&data[cellOffset
+i*2])[1])
;
72845 testcase( pc==iCellFirst );
72846 testcase( pc==iCellLast );
72847 if( pc<iCellFirst || pc>iCellLast ){
72848 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72848);
72849 }
72850 sz = pPage->xCellSize(pPage, &data[pc]);
72851 testcase( pc+sz==usableSize );
72852 if( pc+sz>usableSize ){
72853 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72853);
72854 }
72855 }
72856 return SQLITE_OK0;
72857}
72858
72859/*
72860** Initialize the auxiliary information for a disk block.
72861**
72862** Return SQLITE_OK on success. If we see that the page does
72863** not contain a well-formed database page, then return
72864** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
72865** guarantee that the page is well-formed. It only shows that
72866** we failed to detect any corruption.
72867*/
72868static int btreeInitPage(MemPage *pPage){
72869 u8 *data; /* Equal to pPage->aData */
72870 BtShared *pBt; /* The main btree structure */
72871
72872 assert( pPage->pBt!=0 )((void) (0));
72873 assert( pPage->pBt->db!=0 )((void) (0));
72874 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
72875 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
72876 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
72877 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
72878 assert( pPage->isInit==0 )((void) (0));
72879
72880 pBt = pPage->pBt;
72881 data = pPage->aData + pPage->hdrOffset;
72882 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
72883 ** the b-tree page type. */
72884 if( decodeFlags(pPage, data[0]) ){
72885 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72885);
72886 }
72887 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
72888 pPage->maskPage = (u16)(pBt->pageSize - 1);
72889 pPage->nOverflow = 0;
72890 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
72891 pPage->aCellIdx = data + pPage->childPtrSize + 8;
72892 pPage->aDataEnd = pPage->aData + pBt->pageSize;
72893 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
72894 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
72895 ** number of cells on the page. */
72896 pPage->nCell = get2byte(&data[3])((&data[3])[0]<<8 | (&data[3])[1]);
72897 if( pPage->nCell>MX_CELL(pBt)((pBt->pageSize-8)/6) ){
72898 /* To many cells for a single page. The page must be corrupt */
72899 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(72899);
72900 }
72901 testcase( pPage->nCell==MX_CELL(pBt) );
72902 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
72903 ** possible for a root page of a table that contains no rows) then the
72904 ** offset to the cell content area will equal the page size minus the
72905 ** bytes of reserved space. */
72906 assert( pPage->nCell>0((void) (0))
72907 || get2byteNotZero(&data[5])==(int)pBt->usableSize((void) (0))
72908 || CORRUPT_DB )((void) (0));
72909 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
72910 pPage->isInit = 1;
72911 if( pBt->db->flags & SQLITE_CellSizeCk0x00200000 ){
72912 return btreeCellSizeCheck(pPage);
72913 }
72914 return SQLITE_OK0;
72915}
72916
72917/*
72918** Set up a raw page so that it looks like a database page holding
72919** no entries.
72920*/
72921static void zeroPage(MemPage *pPage, int flags){
72922 unsigned char *data = pPage->aData;
72923 BtShared *pBt = pPage->pBt;
72924 u8 hdr = pPage->hdrOffset;
72925 u16 first;
72926
72927 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB )((void) (0));
72928 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
72929 assert( sqlite3PagerGetData(pPage->pDbPage) == data )((void) (0));
72930 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
72931 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72932 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
72933 memset(&data[hdr], 0, pBt->usableSize - hdr);
72934 }
72935 data[hdr] = (char)flags;
72936 first = hdr + ((flags&PTF_LEAF0x08)==0 ? 12 : 8);
72937 memset(&data[hdr+1], 0, 4);
72938 data[hdr+7] = 0;
72939 put2byte(&data[hdr+5], pBt->usableSize)((&data[hdr+5])[0] = (u8)((pBt->usableSize)>>8),
(&data[hdr+5])[1] = (u8)(pBt->usableSize))
;
72940 pPage->nFree = (u16)(pBt->usableSize - first);
72941 decodeFlags(pPage, flags);
72942 pPage->cellOffset = first;
72943 pPage->aDataEnd = &data[pBt->pageSize];
72944 pPage->aCellIdx = &data[first];
72945 pPage->aDataOfst = &data[pPage->childPtrSize];
72946 pPage->nOverflow = 0;
72947 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
72948 pPage->maskPage = (u16)(pBt->pageSize - 1);
72949 pPage->nCell = 0;
72950 pPage->isInit = 1;
72951}
72952
72953
72954/*
72955** Convert a DbPage obtained from the pager into a MemPage used by
72956** the btree layer.
72957*/
72958static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
72959 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
72960 if( pgno!=pPage->pgno ){
72961 pPage->aData = sqlite3PagerGetData(pDbPage);
72962 pPage->pDbPage = pDbPage;
72963 pPage->pBt = pBt;
72964 pPage->pgno = pgno;
72965 pPage->hdrOffset = pgno==1 ? 100 : 0;
72966 }
72967 assert( pPage->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
72968 return pPage;
72969}
72970
72971/*
72972** Get a page from the pager. Initialize the MemPage.pBt and
72973** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
72974**
72975** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
72976** about the content of the page at this time. So do not go to the disk
72977** to fetch the content. Just fill in the content with zeros for now.
72978** If in the future we call sqlite3PagerWrite() on this page, that
72979** means we have started to be concerned about content and the disk
72980** read should occur at that point.
72981*/
72982static int btreeGetPage(
72983 BtShared *pBt, /* The btree */
72984 Pgno pgno, /* Number of the page to fetch */
72985 MemPage **ppPage, /* Return the page in this parameter */
72986 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
72987){
72988 int rc;
72989 DbPage *pDbPage;
72990
72991 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY )((void) (0));
72992 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72993 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
72994 if( rc ) return rc;
72995 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
72996 return SQLITE_OK0;
72997}
72998
72999/*
73000** Retrieve a page from the pager cache. If the requested page is not
73001** already in the pager cache return NULL. Initialize the MemPage.pBt and
73002** MemPage.aData elements if needed.
73003*/
73004static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
73005 DbPage *pDbPage;
73006 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73007 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
73008 if( pDbPage ){
73009 return btreePageFromDbPage(pDbPage, pgno, pBt);
73010 }
73011 return 0;
73012}
73013
73014/*
73015** Return the size of the database file in pages. If there is any kind of
73016** error, return ((unsigned int)-1).
73017*/
73018static Pgno btreePagecount(BtShared *pBt){
73019 return pBt->nPage;
73020}
73021SQLITE_PRIVATEstatic Pgno sqlite3BtreeLastPage(Btree *p){
73022 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
73023 return btreePagecount(p->pBt);
73024}
73025
73026/*
73027** Get a page from the pager and initialize it.
73028*/
73029static int getAndInitPage(
73030 BtShared *pBt, /* The database file */
73031 Pgno pgno, /* Number of the page to get */
73032 MemPage **ppPage, /* Write the page pointer here */
73033 int bReadOnly /* True for a read-only page */
73034){
73035 int rc;
73036 DbPage *pDbPage;
73037 MemPage *pPage;
73038 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73039
73040 if( pgno>btreePagecount(pBt) ){
73041 *ppPage = 0;
73042 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(73042);
73043 }
73044 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
73045 if( rc ){
73046 *ppPage = 0;
73047 return rc;
73048 }
73049 pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
73050 if( pPage->isInit==0 ){
73051 btreePageFromDbPage(pDbPage, pgno, pBt);
73052 rc = btreeInitPage(pPage);
73053 if( rc!=SQLITE_OK0 ){
73054 releasePage(pPage);
73055 *ppPage = 0;
73056 return rc;
73057 }
73058 }
73059 assert( pPage->pgno==pgno || CORRUPT_DB )((void) (0));
73060 assert( pPage->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
73061 *ppPage = pPage;
73062 return SQLITE_OK0;
73063}
73064
73065/*
73066** Release a MemPage. This should be called once for each prior
73067** call to btreeGetPage.
73068**
73069** Page1 is a special case and must be released using releasePageOne().
73070*/
73071static void releasePageNotNull(MemPage *pPage){
73072 assert( pPage->aData )((void) (0));
73073 assert( pPage->pBt )((void) (0));
73074 assert( pPage->pDbPage!=0 )((void) (0));
73075 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
73076 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
73077 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73078 sqlite3PagerUnrefNotNull(pPage->pDbPage);
73079}
73080static void releasePage(MemPage *pPage){
73081 if( pPage ) releasePageNotNull(pPage);
73082}
73083static void releasePageOne(MemPage *pPage){
73084 assert( pPage!=0 )((void) (0));
73085 assert( pPage->aData )((void) (0));
73086 assert( pPage->pBt )((void) (0));
73087 assert( pPage->pDbPage!=0 )((void) (0));
73088 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
73089 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
73090 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73091 sqlite3PagerUnrefPageOne(pPage->pDbPage);
73092}
73093
73094/*
73095** Get an unused page.
73096**
73097** This works just like btreeGetPage() with the addition:
73098**
73099** * If the page is already in use for some other purpose, immediately
73100** release it and return an SQLITE_CURRUPT error.
73101** * Make sure the isInit flag is clear
73102*/
73103static int btreeGetUnusedPage(
73104 BtShared *pBt, /* The btree */
73105 Pgno pgno, /* Number of the page to fetch */
73106 MemPage **ppPage, /* Return the page in this parameter */
73107 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
73108){
73109 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
73110 if( rc==SQLITE_OK0 ){
73111 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
73112 releasePage(*ppPage);
73113 *ppPage = 0;
73114 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(73114);
73115 }
73116 (*ppPage)->isInit = 0;
73117 }else{
73118 *ppPage = 0;
73119 }
73120 return rc;
73121}
73122
73123
73124/*
73125** During a rollback, when the pager reloads information into the cache
73126** so that the cache is restored to its original state at the start of
73127** the transaction, for each page restored this routine is called.
73128**
73129** This routine needs to reset the extra data section at the end of the
73130** page to agree with the restored data.
73131*/
73132static void pageReinit(DbPage *pData){
73133 MemPage *pPage;
73134 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
73135 assert( sqlite3PagerPageRefcount(pData)>0 )((void) (0));
73136 if( pPage->isInit ){
73137 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
73138 pPage->isInit = 0;
73139 if( sqlite3PagerPageRefcount(pData)>1 ){
73140 /* pPage might not be a btree page; it might be an overflow page
73141 ** or ptrmap page or a free page. In those cases, the following
73142 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
73143 ** But no harm is done by this. And it is very important that
73144 ** btreeInitPage() be called on every btree page so we make
73145 ** the call for every page that comes in for re-initializing. */
73146 btreeInitPage(pPage);
73147 }
73148 }
73149}
73150
73151/*
73152** Invoke the busy handler for a btree.
73153*/
73154static int btreeInvokeBusyHandler(void *pArg){
73155 BtShared *pBt = (BtShared*)pArg;
73156 assert( pBt->db )((void) (0));
73157 assert( sqlite3_mutex_held(pBt->db->mutex) )((void) (0));
73158 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
73159}
73160
73161/*
73162** Open a database file.
73163**
73164** zFilename is the name of the database file. If zFilename is NULL
73165** then an ephemeral database is created. The ephemeral database might
73166** be exclusively in memory, or it might use a disk-based memory cache.
73167** Either way, the ephemeral database will be automatically deleted
73168** when sqlite3BtreeClose() is called.
73169**
73170** If zFilename is ":memory:" then an in-memory database is created
73171** that is automatically destroyed when it is closed.
73172**
73173** The "flags" parameter is a bitmask that might contain bits like
73174** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
73175**
73176** If the database is already opened in the same database connection
73177** and we are in shared cache mode, then the open will fail with an
73178** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
73179** objects in the same database connection since doing so will lead
73180** to problems with locking.
73181*/
73182SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
73183 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
73184 const char *zFilename, /* Name of the file containing the BTree database */
73185 sqlite3 *db, /* Associated database handle */
73186 Btree **ppBtree, /* Pointer to new Btree object written here */
73187 int flags, /* Options */
73188 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
73189){
73190 BtShared *pBt = 0; /* Shared part of btree structure */
73191 Btree *p; /* Handle to return */
73192 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
73193 int rc = SQLITE_OK0; /* Result code from this function */
73194 u8 nReserve; /* Byte of unused space on each page */
73195 unsigned char zDbHeader[100]; /* Database header content */
73196
73197 /* True if opening an ephemeral, temporary database */
73198 const int isTempDb = zFilename==0 || zFilename[0]==0;
73199
73200 /* Set the variable isMemdb to true for an in-memory database, or
73201 ** false for a file-based database.
73202 */
73203#ifdef SQLITE_OMIT_MEMORYDB
73204 const int isMemdb = 0;
73205#else
73206 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
73207 || (isTempDb && sqlite3TempInMemory(db))
73208 || (vfsFlags & SQLITE_OPEN_MEMORY0x00000080)!=0;
73209#endif
73210
73211 assert( db!=0 )((void) (0));
73212 assert( pVfs!=0 )((void) (0));
73213 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
73214 assert( (flags&0xff)==flags )((void) (0)); /* flags fit in 8 bits */
73215
73216 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
73217 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 )((void) (0));
73218
73219 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
73220 assert( (flags & BTREE_SINGLE)==0 || isTempDb )((void) (0));
73221
73222 if( isMemdb ){
73223 flags |= BTREE_MEMORY2;
73224 }
73225 if( (vfsFlags & SQLITE_OPEN_MAIN_DB0x00000100)!=0 && (isMemdb || isTempDb) ){
73226 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB0x00000100) | SQLITE_OPEN_TEMP_DB0x00000200;
73227 }
73228 p = sqlite3MallocZero(sizeof(Btree));
73229 if( !p ){
73230 return SQLITE_NOMEM_BKPT7;
73231 }
73232 p->inTrans = TRANS_NONE0;
73233 p->db = db;
73234#ifndef SQLITE_OMIT_SHARED_CACHE
73235 p->lock.pBtree = p;
73236 p->lock.iTable = 1;
73237#endif
73238
73239#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
73240 /*
73241 ** If this Btree is a candidate for shared cache, try to find an
73242 ** existing BtShared object that we can share with
73243 */
73244 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI0x00000040)!=0) ){
73245 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE0x00020000 ){
73246 int nFilename = sqlite3Strlen30(zFilename)+1;
73247 int nFullPathname = pVfs->mxPathname+1;
73248 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)((nFullPathname)>(nFilename)?(nFullPathname):(nFilename)));
73249 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
73250
73251 p->sharable = 1;
73252 if( !zFullPathname ){
73253 sqlite3_free(p);
73254 return SQLITE_NOMEM_BKPT7;
73255 }
73256 if( isMemdb ){
73257 memcpy(zFullPathname, zFilename, nFilename);
73258 }else{
73259 rc = sqlite3OsFullPathname(pVfs, zFilename,
73260 nFullPathname, zFullPathname);
73261 if( rc ){
73262 if( rc==SQLITE_OK_SYMLINK(0 | (2<<8)) ){
73263 rc = SQLITE_OK0;
73264 }else{
73265 sqlite3_free(zFullPathname);
73266 sqlite3_free(p);
73267 return rc;
73268 }
73269 }
73270 }
73271#if SQLITE_THREADSAFE2
73272 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN4);
73273 sqlite3_mutex_enter(mutexOpen);
73274 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
73275 sqlite3_mutex_enter(mutexShared);
73276#endif
73277 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
73278 assert( pBt->nRef>0 )((void) (0));
73279 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
73280 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
73281 int iDb;
73282 for(iDb=db->nDb-1; iDb>=0; iDb--){
73283 Btree *pExisting = db->aDb[iDb].pBt;
73284 if( pExisting && pExisting->pBt==pBt ){
73285 sqlite3_mutex_leave(mutexShared);
73286 sqlite3_mutex_leave(mutexOpen);
73287 sqlite3_free(zFullPathname);
73288 sqlite3_free(p);
73289 return SQLITE_CONSTRAINT19;
73290 }
73291 }
73292 p->pBt = pBt;
73293 pBt->nRef++;
73294 break;
73295 }
73296 }
73297 sqlite3_mutex_leave(mutexShared);
73298 sqlite3_free(zFullPathname);
73299 }
73300#ifdef SQLITE_DEBUG
73301 else{
73302 /* In debug mode, we mark all persistent databases as sharable
73303 ** even when they are not. This exercises the locking code and
73304 ** gives more opportunity for asserts(sqlite3_mutex_held())
73305 ** statements to find locking problems.
73306 */
73307 p->sharable = 1;
73308 }
73309#endif
73310 }
73311#endif
73312 if( pBt==0 ){
73313 /*
73314 ** The following asserts make sure that structures used by the btree are
73315 ** the right size. This is to guard against size changes that result
73316 ** when compiling on a different architecture.
73317 */
73318 assert( sizeof(i64)==8 )((void) (0));
73319 assert( sizeof(u64)==8 )((void) (0));
73320 assert( sizeof(u32)==4 )((void) (0));
73321 assert( sizeof(u16)==2 )((void) (0));
73322 assert( sizeof(Pgno)==4 )((void) (0));
73323
73324 /* Suppress false-positive compiler warning from PVS-Studio */
73325 memset(&zDbHeader[16], 0, 8);
73326
73327 pBt = sqlite3MallocZero( sizeof(*pBt) );
73328 if( pBt==0 ){
73329 rc = SQLITE_NOMEM_BKPT7;
73330 goto btree_open_out;
73331 }
73332 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
73333 sizeof(MemPage), flags, vfsFlags, pageReinit);
73334 if( rc==SQLITE_OK0 ){
73335 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
73336 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
73337 }
73338 if( rc!=SQLITE_OK0 ){
73339 goto btree_open_out;
73340 }
73341 pBt->openFlags = (u8)flags;
73342 pBt->db = db;
73343 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
73344 p->pBt = pBt;
73345
73346 pBt->pCursor = 0;
73347 pBt->pPage1 = 0;
73348 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY0x0001;
73349#if defined(SQLITE_SECURE_DELETE)
73350 pBt->btsFlags |= BTS_SECURE_DELETE0x0004;
73351#elif defined(SQLITE_FAST_SECURE_DELETE)
73352 pBt->btsFlags |= BTS_OVERWRITE0x0008;
73353#endif
73354 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
73355 ** determined by the 2-byte integer located at an offset of 16 bytes from
73356 ** the beginning of the database file. */
73357 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
73358 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE65536
73359 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
73360 pBt->pageSize = 0;
73361#ifndef SQLITE_OMIT_AUTOVACUUM
73362 /* If the magic name ":memory:" will create an in-memory database, then
73363 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
73364 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
73365 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
73366 ** regular file-name. In this case the auto-vacuum applies as per normal.
73367 */
73368 if( zFilename && !isMemdb ){
73369 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM0 ? 1 : 0);
73370 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM0==2 ? 1 : 0);
73371 }
73372#endif
73373 nReserve = 0;
73374 }else{
73375 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
73376 ** determined by the one-byte unsigned integer found at an offset of 20
73377 ** into the database file header. */
73378 nReserve = zDbHeader[20];
73379 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
73380#ifndef SQLITE_OMIT_AUTOVACUUM
73381 pBt->autoVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 4*4])?1:0);
73382 pBt->incrVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 7*4])?1:0);
73383#endif
73384 }
73385 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
73386 if( rc ) goto btree_open_out;
73387 pBt->usableSize = pBt->pageSize - nReserve;
73388 assert( (pBt->pageSize & 7)==0 )((void) (0)); /* 8-byte alignment of pageSize */
73389
73390#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
73391 /* Add the new BtShared object to the linked list sharable BtShareds.
73392 */
73393 pBt->nRef = 1;
73394 if( p->sharable ){
73395 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
73396 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)mutexShared = sqlite3MutexAlloc(2);
73397 if( SQLITE_THREADSAFE2 && sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
73398 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST0);
73399 if( pBt->mutex==0 ){
73400 rc = SQLITE_NOMEM_BKPT7;
73401 goto btree_open_out;
73402 }
73403 }
73404 sqlite3_mutex_enter(mutexShared);
73405 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
73406 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt;
73407 sqlite3_mutex_leave(mutexShared);
73408 }
73409#endif
73410 }
73411
73412#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
73413 /* If the new Btree uses a sharable pBtShared, then link the new
73414 ** Btree into the list of all sharable Btrees for the same connection.
73415 ** The list is kept in ascending order by pBt address.
73416 */
73417 if( p->sharable ){
73418 int i;
73419 Btree *pSib;
73420 for(i=0; i<db->nDb; i++){
73421 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
73422 while( pSib->pPrev ){ pSib = pSib->pPrev; }
73423 if( (uptr)p->pBt<(uptr)pSib->pBt ){
73424 p->pNext = pSib;
73425 p->pPrev = 0;
73426 pSib->pPrev = p;
73427 }else{
73428 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
73429 pSib = pSib->pNext;
73430 }
73431 p->pNext = pSib->pNext;
73432 p->pPrev = pSib;
73433 if( p->pNext ){
73434 p->pNext->pPrev = p;
73435 }
73436 pSib->pNext = p;
73437 }
73438 break;
73439 }
73440 }
73441 }
73442#endif
73443 *ppBtree = p;
73444
73445btree_open_out:
73446 if( rc!=SQLITE_OK0 ){
73447 if( pBt && pBt->pPager ){
73448 sqlite3PagerClose(pBt->pPager, 0);
73449 }
73450 sqlite3_free(pBt);
73451 sqlite3_free(p);
73452 *ppBtree = 0;
73453 }else{
73454 sqlite3_file *pFile;
73455
73456 /* If the B-Tree was successfully opened, set the pager-cache size to the
73457 ** default value. Except, when opening on an existing shared pager-cache,
73458 ** do not change the pager-cache size.
73459 */
73460 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
73461 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE128);
73462 }
73463
73464 pFile = sqlite3PagerFile(pBt->pPager);
73465 if( pFile->pMethods ){
73466 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB30, (void*)&pBt->db);
73467 }
73468 }
73469 if( mutexOpen ){
73470 assert( sqlite3_mutex_held(mutexOpen) )((void) (0));
73471 sqlite3_mutex_leave(mutexOpen);
73472 }
73473 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 )((void) (0));
73474 return rc;
73475}
73476
73477/*
73478** Decrement the BtShared.nRef counter. When it reaches zero,
73479** remove the BtShared structure from the sharing list. Return
73480** true if the BtShared.nRef counter reaches zero and return
73481** false if it is still positive.
73482*/
73483static int removeFromSharingList(BtShared *pBt){
73484#ifndef SQLITE_OMIT_SHARED_CACHE
73485 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx;
73486 BtShared *pList;
73487 int removed = 0;
73488
73489 assert( sqlite3_mutex_notheld(pBt->mutex) )((void) (0));
73490 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
73491 sqlite3_mutex_enter(pMainMtx);
73492 pBt->nRef--;
73493 if( pBt->nRef<=0 ){
73494 if( GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList==pBt ){
73495 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt->pNext;
73496 }else{
73497 pList = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
73498 while( ALWAYS(pList)(pList) && pList->pNext!=pBt ){
73499 pList=pList->pNext;
73500 }
73501 if( ALWAYS(pList)(pList) ){
73502 pList->pNext = pBt->pNext;
73503 }
73504 }
73505 if( SQLITE_THREADSAFE2 ){
73506 sqlite3_mutex_free(pBt->mutex);
73507 }
73508 removed = 1;
73509 }
73510 sqlite3_mutex_leave(pMainMtx);
73511 return removed;
73512#else
73513 return 1;
73514#endif
73515}
73516
73517/*
73518** Make sure pBt->pTmpSpace points to an allocation of
73519** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
73520** pointer.
73521*/
73522static SQLITE_NOINLINE__attribute__((noinline)) int allocateTempSpace(BtShared *pBt){
73523 assert( pBt!=0 )((void) (0));
73524 assert( pBt->pTmpSpace==0 )((void) (0));
73525 /* This routine is called only by btreeCursor() when allocating the
73526 ** first write cursor for the BtShared object */
73527 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 )((void) (0));
73528 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
73529 if( pBt->pTmpSpace==0 ){
73530 BtCursor *pCur = pBt->pCursor;
73531 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
73532 memset(pCur, 0, sizeof(*pCur));
73533 return SQLITE_NOMEM_BKPT7;
73534 }
73535
73536 /* One of the uses of pBt->pTmpSpace is to format cells before
73537 ** inserting them into a leaf page (function fillInCell()). If
73538 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
73539 ** by the various routines that manipulate binary cells. Which
73540 ** can mean that fillInCell() only initializes the first 2 or 3
73541 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
73542 ** it into a database page. This is not actually a problem, but it
73543 ** does cause a valgrind error when the 1 or 2 bytes of uninitialized
73544 ** data is passed to system call write(). So to avoid this error,
73545 ** zero the first 4 bytes of temp space here.
73546 **
73547 ** Also: Provide four bytes of initialized space before the
73548 ** beginning of pTmpSpace as an area available to prepend the
73549 ** left-child pointer to the beginning of a cell.
73550 */
73551 memset(pBt->pTmpSpace, 0, 8);
73552 pBt->pTmpSpace += 4;
73553 return SQLITE_OK0;
73554}
73555
73556/*
73557** Free the pBt->pTmpSpace allocation
73558*/
73559static void freeTempSpace(BtShared *pBt){
73560 if( pBt->pTmpSpace ){
73561 pBt->pTmpSpace -= 4;
73562 sqlite3PageFree(pBt->pTmpSpace);
73563 pBt->pTmpSpace = 0;
73564 }
73565}
73566
73567/*
73568** Close an open database and invalidate all cursors.
73569*/
73570SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree *p){
73571 BtShared *pBt = p->pBt;
73572
73573 /* Close all cursors opened via this handle. */
73574 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73575 sqlite3BtreeEnter(p);
73576
73577 /* Verify that no other cursors have this Btree open */
73578#ifdef SQLITE_DEBUG
73579 {
73580 BtCursor *pCur = pBt->pCursor;
73581 while( pCur ){
73582 BtCursor *pTmp = pCur;
73583 pCur = pCur->pNext;
73584 assert( pTmp->pBtree!=p )((void) (0));
73585
73586 }
73587 }
73588#endif
73589
73590 /* Rollback any active transaction and free the handle structure.
73591 ** The call to sqlite3BtreeRollback() drops any table-locks held by
73592 ** this handle.
73593 */
73594 sqlite3BtreeRollback(p, SQLITE_OK0, 0);
73595 sqlite3BtreeLeave(p);
73596
73597 /* If there are still other outstanding references to the shared-btree
73598 ** structure, return now. The remainder of this procedure cleans
73599 ** up the shared-btree.
73600 */
73601 assert( p->wantToLock==0 && p->locked==0 )((void) (0));
73602 if( !p->sharable || removeFromSharingList(pBt) ){
73603 /* The pBt is no longer on the sharing list, so we can access
73604 ** it without having to hold the mutex.
73605 **
73606 ** Clean out and delete the BtShared object.
73607 */
73608 assert( !pBt->pCursor )((void) (0));
73609 sqlite3PagerClose(pBt->pPager, p->db);
73610 if( pBt->xFreeSchema && pBt->pSchema ){
73611 pBt->xFreeSchema(pBt->pSchema);
73612 }
73613 sqlite3DbFree(0, pBt->pSchema);
73614 freeTempSpace(pBt);
73615 sqlite3_free(pBt);
73616 }
73617
73618#ifndef SQLITE_OMIT_SHARED_CACHE
73619 assert( p->wantToLock==0 )((void) (0));
73620 assert( p->locked==0 )((void) (0));
73621 if( p->pPrev ) p->pPrev->pNext = p->pNext;
73622 if( p->pNext ) p->pNext->pPrev = p->pPrev;
73623#endif
73624
73625 sqlite3_free(p);
73626 return SQLITE_OK0;
73627}
73628
73629/*
73630** Change the "soft" limit on the number of pages in the cache.
73631** Unused and unmodified pages will be recycled when the number of
73632** pages in the cache exceeds this soft limit. But the size of the
73633** cache is allowed to grow larger than this limit if it contains
73634** dirty pages or pages still in active use.
73635*/
73636SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
73637 BtShared *pBt = p->pBt;
73638 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73639 sqlite3BtreeEnter(p);
73640 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
73641 sqlite3BtreeLeave(p);
73642 return SQLITE_OK0;
73643}
73644
73645/*
73646** Change the "spill" limit on the number of pages in the cache.
73647** If the number of pages exceeds this limit during a write transaction,
73648** the pager might attempt to "spill" pages to the journal early in
73649** order to free up memory.
73650**
73651** The value returned is the current spill size. If zero is passed
73652** as an argument, no changes are made to the spill size setting, so
73653** using mxPage of 0 is a way to query the current spill size.
73654*/
73655SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
73656 BtShared *pBt = p->pBt;
73657 int res;
73658 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73659 sqlite3BtreeEnter(p);
73660 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
73661 sqlite3BtreeLeave(p);
73662 return res;
73663}
73664
73665#if SQLITE_MAX_MMAP_SIZE20971520>0
73666/*
73667** Change the limit on the amount of the database file that may be
73668** memory mapped.
73669*/
73670SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
73671 BtShared *pBt = p->pBt;
73672 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73673 sqlite3BtreeEnter(p);
73674 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
73675 sqlite3BtreeLeave(p);
73676 return SQLITE_OK0;
73677}
73678#endif /* SQLITE_MAX_MMAP_SIZE>0 */
73679
73680/*
73681** Change the way data is synced to disk in order to increase or decrease
73682** how well the database resists damage due to OS crashes and power
73683** failures. Level 1 is the same as asynchronous (no syncs() occur and
73684** there is a high probability of damage) Level 2 is the default. There
73685** is a very low but non-zero probability of damage. Level 3 reduces the
73686** probability of damage to near zero but with a write performance reduction.
73687*/
73688#ifndef SQLITE_OMIT_PAGER_PRAGMAS
73689SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(
73690 Btree *p, /* The btree to set the safety level on */
73691 unsigned pgFlags /* Various PAGER_* flags */
73692){
73693 BtShared *pBt = p->pBt;
73694 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73695 sqlite3BtreeEnter(p);
73696 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
73697 sqlite3BtreeLeave(p);
73698 return SQLITE_OK0;
73699}
73700#endif
73701
73702/*
73703** Change the default pages size and the number of reserved bytes per page.
73704** Or, if the page size has already been fixed, return SQLITE_READONLY
73705** without changing anything.
73706**
73707** The page size must be a power of 2 between 512 and 65536. If the page
73708** size supplied does not meet this constraint then the page size is not
73709** changed.
73710**
73711** Page sizes are constrained to be a power of two so that the region
73712** of the database file used for locking (beginning at PENDING_BYTE,
73713** the first byte past the 1GB boundary, 0x40000000) needs to occur
73714** at the beginning of a page.
73715**
73716** If parameter nReserve is less than zero, then the number of reserved
73717** bytes per page is left unchanged.
73718**
73719** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
73720** and autovacuum mode can no longer be changed.
73721*/
73722SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
73723 int rc = SQLITE_OK0;
73724 int x;
73725 BtShared *pBt = p->pBt;
73726 assert( nReserve>=0 && nReserve<=255 )((void) (0));
73727 sqlite3BtreeEnter(p);
73728 pBt->nReserveWanted = nReserve;
73729 x = pBt->pageSize - pBt->usableSize;
73730 if( nReserve<x ) nReserve = x;
73731 if( pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002 ){
73732 sqlite3BtreeLeave(p);
73733 return SQLITE_READONLY8;
73734 }
73735 assert( nReserve>=0 && nReserve<=255 )((void) (0));
73736 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE65536 &&
73737 ((pageSize-1)&pageSize)==0 ){
73738 assert( (pageSize & 7)==0 )((void) (0));
73739 assert( !pBt->pCursor )((void) (0));
73740 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
73741 pBt->pageSize = (u32)pageSize;
73742 freeTempSpace(pBt);
73743 }
73744 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
73745 pBt->usableSize = pBt->pageSize - (u16)nReserve;
73746 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
73747 sqlite3BtreeLeave(p);
73748 return rc;
73749}
73750
73751/*
73752** Return the currently defined page size
73753*/
73754SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree *p){
73755 return p->pBt->pageSize;
73756}
73757
73758/*
73759** This function is similar to sqlite3BtreeGetReserve(), except that it
73760** may only be called if it is guaranteed that the b-tree mutex is already
73761** held.
73762**
73763** This is useful in one special case in the backup API code where it is
73764** known that the shared b-tree mutex is held, but the mutex on the
73765** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
73766** were to be called, it might collide with some other operation on the
73767** database handle that owns *p, causing undefined behavior.
73768*/
73769SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p){
73770 int n;
73771 assert( sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
73772 n = p->pBt->pageSize - p->pBt->usableSize;
73773 return n;
73774}
73775
73776/*
73777** Return the number of bytes of space at the end of every page that
73778** are intentionally left unused. This is the "reserved" space that is
73779** sometimes used by extensions.
73780**
73781** The value returned is the larger of the current reserve size and
73782** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
73783** The amount of reserve can only grow - never shrink.
73784*/
73785SQLITE_PRIVATEstatic int sqlite3BtreeGetRequestedReserve(Btree *p){
73786 int n1, n2;
73787 sqlite3BtreeEnter(p);
73788 n1 = (int)p->pBt->nReserveWanted;
73789 n2 = sqlite3BtreeGetReserveNoMutex(p);
73790 sqlite3BtreeLeave(p);
73791 return n1>n2 ? n1 : n2;
73792}
73793
73794
73795/*
73796** Set the maximum page count for a database if mxPage is positive.
73797** No changes are made if mxPage is 0 or negative.
73798** Regardless of the value of mxPage, return the maximum page count.
73799*/
73800SQLITE_PRIVATEstatic Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
73801 Pgno n;
73802 sqlite3BtreeEnter(p);
73803 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
73804 sqlite3BtreeLeave(p);
73805 return n;
73806}
73807
73808/*
73809** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
73810**
73811** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
73812** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
73813** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
73814** newFlag==(-1) No changes
73815**
73816** This routine acts as a query if newFlag is less than zero
73817**
73818** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
73819** freelist leaf pages are not written back to the database. Thus in-page
73820** deleted content is cleared, but freelist deleted content is not.
73821**
73822** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
73823** that freelist leaf pages are written back into the database, increasing
73824** the amount of disk I/O.
73825*/
73826SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
73827 int b;
73828 if( p==0 ) return 0;
73829 sqlite3BtreeEnter(p);
73830 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 )((void) (0));
73831 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) )((void) (0));
73832 if( newFlag>=0 ){
73833 p->pBt->btsFlags &= ~BTS_FAST_SECURE0x000c;
73834 p->pBt->btsFlags |= BTS_SECURE_DELETE0x0004*newFlag;
73835 }
73836 b = (p->pBt->btsFlags & BTS_FAST_SECURE0x000c)/BTS_SECURE_DELETE0x0004;
73837 sqlite3BtreeLeave(p);
73838 return b;
73839}
73840
73841/*
73842** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
73843** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
73844** is disabled. The default value for the auto-vacuum property is
73845** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
73846*/
73847SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
73848#ifdef SQLITE_OMIT_AUTOVACUUM
73849 return SQLITE_READONLY8;
73850#else
73851 BtShared *pBt = p->pBt;
73852 int rc = SQLITE_OK0;
73853 u8 av = (u8)autoVacuum;
73854
73855 sqlite3BtreeEnter(p);
73856 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002)!=0 && (av ?1:0)!=pBt->autoVacuum ){
73857 rc = SQLITE_READONLY8;
73858 }else{
73859 pBt->autoVacuum = av ?1:0;
73860 pBt->incrVacuum = av==2 ?1:0;
73861 }
73862 sqlite3BtreeLeave(p);
73863 return rc;
73864#endif
73865}
73866
73867/*
73868** Return the value of the 'auto-vacuum' property. If auto-vacuum is
73869** enabled 1 is returned. Otherwise 0.
73870*/
73871SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *p){
73872#ifdef SQLITE_OMIT_AUTOVACUUM
73873 return BTREE_AUTOVACUUM_NONE0;
73874#else
73875 int rc;
73876 sqlite3BtreeEnter(p);
73877 rc = (
73878 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE0:
73879 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL1:
73880 BTREE_AUTOVACUUM_INCR2
73881 );
73882 sqlite3BtreeLeave(p);
73883 return rc;
73884#endif
73885}
73886
73887/*
73888** If the user has not set the safety-level for this database connection
73889** using "PRAGMA synchronous", and if the safety-level is not already
73890** set to the value passed to this function as the second parameter,
73891** set it so.
73892*/
73893#if SQLITE_DEFAULT_SYNCHRONOUS2!=SQLITE_DEFAULT_WAL_SYNCHRONOUS1 \
73894 && !defined(SQLITE_OMIT_WAL)
73895static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
73896 sqlite3 *db;
73897 Db *pDb;
73898 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
73899 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
73900 if( pDb->bSyncSet==0
73901 && pDb->safety_level!=safety_level
73902 && pDb!=&db->aDb[1]
73903 ){
73904 pDb->safety_level = safety_level;
73905 sqlite3PagerSetFlags(pBt->pPager,
73906 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38));
73907 }
73908 }
73909}
73910#else
73911# define setDefaultSyncFlag(pBt,safety_level)
73912#endif
73913
73914/* Forward declaration */
73915static int newDatabase(BtShared*);
73916
73917
73918/*
73919** Get a reference to pPage1 of the database file. This will
73920** also acquire a readlock on that file.
73921**
73922** SQLITE_OK is returned on success. If the file is not a
73923** well-formed database file, then SQLITE_CORRUPT is returned.
73924** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
73925** is returned if we run out of memory.
73926*/
73927static int lockBtree(BtShared *pBt){
73928 int rc; /* Result code from subfunctions */
73929 MemPage *pPage1; /* Page 1 of the database file */
73930 u32 nPage; /* Number of pages in the database */
73931 u32 nPageFile = 0; /* Number of pages in the database file */
73932
73933 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
73934 assert( pBt->pPage1==0 )((void) (0));
73935 rc = sqlite3PagerSharedLock(pBt->pPager);
73936 if( rc!=SQLITE_OK0 ) return rc;
73937 rc = btreeGetPage(pBt, 1, &pPage1, 0);
73938 if( rc!=SQLITE_OK0 ) return rc;
73939
73940 /* Do some checking to help insure the file we opened really is
73941 ** a valid database file.
73942 */
73943 nPage = get4bytesqlite3Get4byte(28+(u8*)pPage1->aData);
73944 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
73945 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
73946 nPage = nPageFile;
73947 }
73948 if( (pBt->db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
73949 nPage = 0;
73950 }
73951 if( nPage>0 ){
73952 u32 pageSize;
73953 u32 usableSize;
73954 u8 *page1 = pPage1->aData;
73955 rc = SQLITE_NOTADB26;
73956 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
73957 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
73958 ** 61 74 20 33 00. */
73959 if( memcmp(page1, zMagicHeader, 16)!=0 ){
73960 goto page1_init_failed;
73961 }
73962
73963#ifdef SQLITE_OMIT_WAL
73964 if( page1[18]>1 ){
73965 pBt->btsFlags |= BTS_READ_ONLY0x0001;
73966 }
73967 if( page1[19]>1 ){
73968 goto page1_init_failed;
73969 }
73970#else
73971 if( page1[18]>2 ){
73972 pBt->btsFlags |= BTS_READ_ONLY0x0001;
73973 }
73974 if( page1[19]>2 ){
73975 goto page1_init_failed;
73976 }
73977
73978 /* If the read version is set to 2, this database should be accessed
73979 ** in WAL mode. If the log is not already open, open it now. Then
73980 ** return SQLITE_OK and return without populating BtShared.pPage1.
73981 ** The caller detects this and calls this function again. This is
73982 ** required as the version of page 1 currently in the page1 buffer
73983 ** may not be the latest version - there may be a newer one in the log
73984 ** file.
73985 */
73986 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL0x0020)==0 ){
73987 int isOpen = 0;
73988 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
73989 if( rc!=SQLITE_OK0 ){
73990 goto page1_init_failed;
73991 }else{
73992 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS1+1);
73993 if( isOpen==0 ){
73994 releasePageOne(pPage1);
73995 return SQLITE_OK0;
73996 }
73997 }
73998 rc = SQLITE_NOTADB26;
73999 }else{
74000 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS2+1);
74001 }
74002#endif
74003
74004 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
74005 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
74006 **
74007 ** The original design allowed these amounts to vary, but as of
74008 ** version 3.6.0, we require them to be fixed.
74009 */
74010 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
74011 goto page1_init_failed;
74012 }
74013 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
74014 ** determined by the 2-byte integer located at an offset of 16 bytes from
74015 ** the beginning of the database file. */
74016 pageSize = (page1[16]<<8) | (page1[17]<<16);
74017 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
74018 ** between 512 and 65536 inclusive. */
74019 if( ((pageSize-1)&pageSize)!=0
74020 || pageSize>SQLITE_MAX_PAGE_SIZE65536
74021 || pageSize<=256
74022 ){
74023 goto page1_init_failed;
74024 }
74025 assert( (pageSize & 7)==0 )((void) (0));
74026 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
74027 ** integer at offset 20 is the number of bytes of space at the end of
74028 ** each page to reserve for extensions.
74029 **
74030 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
74031 ** determined by the one-byte unsigned integer found at an offset of 20
74032 ** into the database file header. */
74033 usableSize = pageSize - page1[20];
74034 if( (u32)pageSize!=pBt->pageSize ){
74035 /* After reading the first page of the database assuming a page size
74036 ** of BtShared.pageSize, we have discovered that the page-size is
74037 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
74038 ** zero and return SQLITE_OK. The caller will call this function
74039 ** again with the correct page-size.
74040 */
74041 releasePageOne(pPage1);
74042 pBt->usableSize = usableSize;
74043 pBt->pageSize = pageSize;
74044 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74045 freeTempSpace(pBt);
74046 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
74047 pageSize-usableSize);
74048 return rc;
74049 }
74050 if( nPage>nPageFile ){
74051 if( sqlite3WritableSchema(pBt->db)==0 ){
74052 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(74052);
74053 goto page1_init_failed;
74054 }else{
74055 nPage = nPageFile;
74056 }
74057 }
74058 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
74059 ** be less than 480. In other words, if the page size is 512, then the
74060 ** reserved space size cannot exceed 32. */
74061 if( usableSize<480 ){
74062 goto page1_init_failed;
74063 }
74064 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74065 pBt->pageSize = pageSize;
74066 pBt->usableSize = usableSize;
74067#ifndef SQLITE_OMIT_AUTOVACUUM
74068 pBt->autoVacuum = (get4bytesqlite3Get4byte(&page1[36 + 4*4])?1:0);
74069 pBt->incrVacuum = (get4bytesqlite3Get4byte(&page1[36 + 7*4])?1:0);
74070#endif
74071 }
74072
74073 /* maxLocal is the maximum amount of payload to store locally for
74074 ** a cell. Make sure it is small enough so that at least minFanout
74075 ** cells can will fit on one page. We assume a 10-byte page header.
74076 ** Besides the payload, the cell must store:
74077 ** 2-byte pointer to the cell
74078 ** 4-byte child pointer
74079 ** 9-byte nKey value
74080 ** 4-byte nData value
74081 ** 4-byte overflow page pointer
74082 ** So a cell consists of a 2-byte pointer, a header which is as much as
74083 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
74084 ** page pointer.
74085 */
74086 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
74087 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
74088 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
74089 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
74090 if( pBt->maxLocal>127 ){
74091 pBt->max1bytePayload = 127;
74092 }else{
74093 pBt->max1bytePayload = (u8)pBt->maxLocal;
74094 }
74095 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) )((void) (0));
74096 pBt->pPage1 = pPage1;
74097 pBt->nPage = nPage;
74098 return SQLITE_OK0;
74099
74100page1_init_failed:
74101 releasePageOne(pPage1);
74102 pBt->pPage1 = 0;
74103 return rc;
74104}
74105
74106#ifndef NDEBUG1
74107/*
74108** Return the number of cursors open on pBt. This is for use
74109** in assert() expressions, so it is only compiled if NDEBUG is not
74110** defined.
74111**
74112** Only write cursors are counted if wrOnly is true. If wrOnly is
74113** false then all cursors are counted.
74114**
74115** For the purposes of this routine, a cursor is any cursor that
74116** is capable of reading or writing to the database. Cursors that
74117** have been tripped into the CURSOR_FAULT state are not counted.
74118*/
74119static int countValidCursors(BtShared *pBt, int wrOnly){
74120 BtCursor *pCur;
74121 int r = 0;
74122 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
74123 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag0x01)!=0)
74124 && pCur->eState!=CURSOR_FAULT4 ) r++;
74125 }
74126 return r;
74127}
74128#endif
74129
74130/*
74131** If there are no outstanding cursors and we are not in the middle
74132** of a transaction but there is a read lock on the database, then
74133** this routine unrefs the first page of the database file which
74134** has the effect of releasing the read lock.
74135**
74136** If there is a transaction in progress, this routine is a no-op.
74137*/
74138static void unlockBtreeIfUnused(BtShared *pBt){
74139 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74140 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE )((void) (0));
74141 if( pBt->inTransaction==TRANS_NONE0 && pBt->pPage1!=0 ){
74142 MemPage *pPage1 = pBt->pPage1;
74143 assert( pPage1->aData )((void) (0));
74144 assert( sqlite3PagerRefcount(pBt->pPager)==1 )((void) (0));
74145 pBt->pPage1 = 0;
74146 releasePageOne(pPage1);
74147 }
74148}
74149
74150/*
74151** If pBt points to an empty file then convert that empty file
74152** into a new empty database by initializing the first page of
74153** the database.
74154*/
74155static int newDatabase(BtShared *pBt){
74156 MemPage *pP1;
74157 unsigned char *data;
74158 int rc;
74159
74160 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74161 if( pBt->nPage>0 ){
74162 return SQLITE_OK0;
74163 }
74164 pP1 = pBt->pPage1;
74165 assert( pP1!=0 )((void) (0));
74166 data = pP1->aData;
74167 rc = sqlite3PagerWrite(pP1->pDbPage);
74168 if( rc ) return rc;
74169 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
74170 assert( sizeof(zMagicHeader)==16 )((void) (0));
74171 data[16] = (u8)((pBt->pageSize>>8)&0xff);
74172 data[17] = (u8)((pBt->pageSize>>16)&0xff);
74173 data[18] = 1;
74174 data[19] = 1;
74175 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize)((void) (0));
74176 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
74177 data[21] = 64;
74178 data[22] = 32;
74179 data[23] = 32;
74180 memset(&data[24], 0, 100-24);
74181 zeroPage(pP1, PTF_INTKEY0x01|PTF_LEAF0x08|PTF_LEAFDATA0x04 );
74182 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
74183#ifndef SQLITE_OMIT_AUTOVACUUM
74184 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 )((void) (0));
74185 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 )((void) (0));
74186 put4bytesqlite3Put4byte(&data[36 + 4*4], pBt->autoVacuum);
74187 put4bytesqlite3Put4byte(&data[36 + 7*4], pBt->incrVacuum);
74188#endif
74189 pBt->nPage = 1;
74190 data[31] = 1;
74191 return SQLITE_OK0;
74192}
74193
74194/*
74195** Initialize the first page of the database file (creating a database
74196** consisting of a single page and no schema objects). Return SQLITE_OK
74197** if successful, or an SQLite error code otherwise.
74198*/
74199SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p){
74200 int rc;
74201 sqlite3BtreeEnter(p);
74202 p->pBt->nPage = 0;
74203 rc = newDatabase(p->pBt);
74204 sqlite3BtreeLeave(p);
74205 return rc;
74206}
74207
74208/*
74209** Attempt to start a new transaction. A write-transaction
74210** is started if the second argument is nonzero, otherwise a read-
74211** transaction. If the second argument is 2 or more and exclusive
74212** transaction is started, meaning that no other process is allowed
74213** to access the database. A preexisting transaction may not be
74214** upgraded to exclusive by calling this routine a second time - the
74215** exclusivity flag only works for a new transaction.
74216**
74217** A write-transaction must be started before attempting any
74218** changes to the database. None of the following routines
74219** will work unless a transaction is started first:
74220**
74221** sqlite3BtreeCreateTable()
74222** sqlite3BtreeCreateIndex()
74223** sqlite3BtreeClearTable()
74224** sqlite3BtreeDropTable()
74225** sqlite3BtreeInsert()
74226** sqlite3BtreeDelete()
74227** sqlite3BtreeUpdateMeta()
74228**
74229** If an initial attempt to acquire the lock fails because of lock contention
74230** and the database was previously unlocked, then invoke the busy handler
74231** if there is one. But if there was previously a read-lock, do not
74232** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
74233** returned when there is already a read-lock in order to avoid a deadlock.
74234**
74235** Suppose there are two processes A and B. A has a read lock and B has
74236** a reserved lock. B tries to promote to exclusive but is blocked because
74237** of A's read lock. A tries to promote to reserved but is blocked by B.
74238** One or the other of the two processes must give way or there can be
74239** no progress. By returning SQLITE_BUSY and not invoking the busy callback
74240** when A already has a read lock, we encourage A to give up and let B
74241** proceed.
74242*/
74243static SQLITE_NOINLINE__attribute__((noinline)) int btreeBeginTrans(
74244 Btree *p, /* The btree in which to start the transaction */
74245 int wrflag, /* True to start a write transaction */
74246 int *pSchemaVersion /* Put schema version number here, if not NULL */
74247){
74248 BtShared *pBt = p->pBt;
74249 Pager *pPager = pBt->pPager;
74250 int rc = SQLITE_OK0;
74251
74252 sqlite3BtreeEnter(p);
74253 btreeIntegrity(p)((void) (0)); ((void) (0));;
74254
74255 /* If the btree is already in a write-transaction, or it
74256 ** is already in a read-transaction and a read-transaction
74257 ** is requested, this is a no-op.
74258 */
74259 if( p->inTrans==TRANS_WRITE2 || (p->inTrans==TRANS_READ1 && !wrflag) ){
74260 goto trans_begun;
74261 }
74262 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 )((void) (0));
74263
74264 if( (p->db->flags & SQLITE_ResetDatabase0x02000000)
74265 && sqlite3PagerIsreadonly(pPager)==0
74266 ){
74267 pBt->btsFlags &= ~BTS_READ_ONLY0x0001;
74268 }
74269
74270 /* Write transactions are not possible on a read-only database */
74271 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 && wrflag ){
74272 rc = SQLITE_READONLY8;
74273 goto trans_begun;
74274 }
74275
74276#ifndef SQLITE_OMIT_SHARED_CACHE
74277 {
74278 sqlite3 *pBlock = 0;
74279 /* If another database handle has already opened a write transaction
74280 ** on this shared-btree structure and a second write transaction is
74281 ** requested, return SQLITE_LOCKED.
74282 */
74283 if( (wrflag && pBt->inTransaction==TRANS_WRITE2)
74284 || (pBt->btsFlags & BTS_PENDING0x0080)!=0
74285 ){
74286 pBlock = pBt->pWriter->db;
74287 }else if( wrflag>1 ){
74288 BtLock *pIter;
74289 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
74290 if( pIter->pBtree!=p ){
74291 pBlock = pIter->pBtree->db;
74292 break;
74293 }
74294 }
74295 }
74296 if( pBlock ){
74297 sqlite3ConnectionBlocked(p->db, pBlock);
74298 rc = SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
74299 goto trans_begun;
74300 }
74301 }
74302#endif
74303
74304 /* Any read-only or read-write transaction implies a read-lock on
74305 ** page 1. So if some other shared-cache client already has a write-lock
74306 ** on page 1, the transaction cannot be opened. */
74307 rc = querySharedCacheTableLock(p, SCHEMA_ROOT1, READ_LOCK1);
74308 if( SQLITE_OK0!=rc ) goto trans_begun;
74309
74310 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY0x0010;
74311 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY0x0010;
74312 do {
74313 sqlite3PagerWalDb(pPager, p->db);
74314
74315#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
74316 /* If transitioning from no transaction directly to a write transaction,
74317 ** block for the WRITER lock first if possible. */
74318 if( pBt->pPage1==0 && wrflag ){
74319 assert( pBt->inTransaction==TRANS_NONE )((void) (0));
74320 rc = sqlite3PagerWalWriteLock(pPager, 1)0;
74321 if( rc!=SQLITE_BUSY5 && rc!=SQLITE_OK0 ) break;
74322 }
74323#endif
74324
74325 /* Call lockBtree() until either pBt->pPage1 is populated or
74326 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
74327 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
74328 ** reading page 1 it discovers that the page-size of the database
74329 ** file is not pBt->pageSize. In this case lockBtree() will update
74330 ** pBt->pageSize to the page-size of the file on disk.
74331 */
74332 while( pBt->pPage1==0 && SQLITE_OK0==(rc = lockBtree(pBt)) );
74333
74334 if( rc==SQLITE_OK0 && wrflag ){
74335 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 ){
74336 rc = SQLITE_READONLY8;
74337 }else{
74338 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
74339 if( rc==SQLITE_OK0 ){
74340 rc = newDatabase(pBt);
74341 }else if( rc==SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) && pBt->inTransaction==TRANS_NONE0 ){
74342 /* if there was no transaction opened when this function was
74343 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
74344 ** code to SQLITE_BUSY. */
74345 rc = SQLITE_BUSY5;
74346 }
74347 }
74348 }
74349
74350 if( rc!=SQLITE_OK0 ){
74351 (void)sqlite3PagerWalWriteLock(pPager, 0)0;
74352 unlockBtreeIfUnused(pBt);
74353 }
74354 }while( (rc&0xFF)==SQLITE_BUSY5 && pBt->inTransaction==TRANS_NONE0 &&
74355 btreeInvokeBusyHandler(pBt) );
74356 sqlite3PagerWalDb(pPager, 0);
74357#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
74358 if( rc==SQLITE_BUSY_TIMEOUT(5 | (3<<8)) ) rc = SQLITE_BUSY5;
74359#endif
74360
74361 if( rc==SQLITE_OK0 ){
74362 if( p->inTrans==TRANS_NONE0 ){
74363 pBt->nTransaction++;
74364#ifndef SQLITE_OMIT_SHARED_CACHE
74365 if( p->sharable ){
74366 assert( p->lock.pBtree==p && p->lock.iTable==1 )((void) (0));
74367 p->lock.eLock = READ_LOCK1;
74368 p->lock.pNext = pBt->pLock;
74369 pBt->pLock = &p->lock;
74370 }
74371#endif
74372 }
74373 p->inTrans = (wrflag?TRANS_WRITE2:TRANS_READ1);
74374 if( p->inTrans>pBt->inTransaction ){
74375 pBt->inTransaction = p->inTrans;
74376 }
74377 if( wrflag ){
74378 MemPage *pPage1 = pBt->pPage1;
74379#ifndef SQLITE_OMIT_SHARED_CACHE
74380 assert( !pBt->pWriter )((void) (0));
74381 pBt->pWriter = p;
74382 pBt->btsFlags &= ~BTS_EXCLUSIVE0x0040;
74383 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE0x0040;
74384#endif
74385
74386 /* If the db-size header field is incorrect (as it may be if an old
74387 ** client has been writing the database file), update it now. Doing
74388 ** this sooner rather than later means the database size can safely
74389 ** re-read the database size from page 1 if a savepoint or transaction
74390 ** rollback occurs within the transaction.
74391 */
74392 if( pBt->nPage!=get4bytesqlite3Get4byte(&pPage1->aData[28]) ){
74393 rc = sqlite3PagerWrite(pPage1->pDbPage);
74394 if( rc==SQLITE_OK0 ){
74395 put4bytesqlite3Put4byte(&pPage1->aData[28], pBt->nPage);
74396 }
74397 }
74398 }
74399 }
74400
74401trans_begun:
74402 if( rc==SQLITE_OK0 ){
74403 if( pSchemaVersion ){
74404 *pSchemaVersion = get4bytesqlite3Get4byte(&pBt->pPage1->aData[40]);
74405 }
74406 if( wrflag ){
74407 /* This call makes sure that the pager has the correct number of
74408 ** open savepoints. If the second parameter is greater than 0 and
74409 ** the sub-journal is not already open, then it will be opened here.
74410 */
74411 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
74412 }
74413 }
74414
74415 btreeIntegrity(p)((void) (0)); ((void) (0));;
74416 sqlite3BtreeLeave(p);
74417 return rc;
74418}
74419SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
74420 BtShared *pBt;
74421 if( p->sharable
74422 || p->inTrans==TRANS_NONE0
74423 || (p->inTrans==TRANS_READ1 && wrflag!=0)
74424 ){
74425 return btreeBeginTrans(p,wrflag,pSchemaVersion);
74426 }
74427 pBt = p->pBt;
74428 if( pSchemaVersion ){
74429 *pSchemaVersion = get4bytesqlite3Get4byte(&pBt->pPage1->aData[40]);
74430 }
74431 if( wrflag ){
74432 /* This call makes sure that the pager has the correct number of
74433 ** open savepoints. If the second parameter is greater than 0 and
74434 ** the sub-journal is not already open, then it will be opened here.
74435 */
74436 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
74437 }else{
74438 return SQLITE_OK0;
74439 }
74440}
74441
74442#ifndef SQLITE_OMIT_AUTOVACUUM
74443
74444/*
74445** Set the pointer-map entries for all children of page pPage. Also, if
74446** pPage contains cells that point to overflow pages, set the pointer
74447** map entries for the overflow pages as well.
74448*/
74449static int setChildPtrmaps(MemPage *pPage){
74450 int i; /* Counter variable */
74451 int nCell; /* Number of cells in page pPage */
74452 int rc; /* Return code */
74453 BtShared *pBt = pPage->pBt;
74454 Pgno pgno = pPage->pgno;
74455
74456 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
74457 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
74458 if( rc!=SQLITE_OK0 ) return rc;
74459 nCell = pPage->nCell;
74460
74461 for(i=0; i<nCell; i++){
74462 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
74463
74464 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
74465
74466 if( !pPage->leaf ){
74467 Pgno childPgno = get4bytesqlite3Get4byte(pCell);
74468 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
74469 }
74470 }
74471
74472 if( !pPage->leaf ){
74473 Pgno childPgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
74474 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
74475 }
74476
74477 return rc;
74478}
74479
74480/*
74481** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
74482** that it points to iTo. Parameter eType describes the type of pointer to
74483** be modified, as follows:
74484**
74485** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
74486** page of pPage.
74487**
74488** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
74489** page pointed to by one of the cells on pPage.
74490**
74491** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
74492** overflow page in the list.
74493*/
74494static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
74495 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
74496 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
74497 if( eType==PTRMAP_OVERFLOW24 ){
74498 /* The pointer is always the first 4 bytes of the page in this case. */
74499 if( get4bytesqlite3Get4byte(pPage->aData)!=iFrom ){
74500 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(74500);
74501 }
74502 put4bytesqlite3Put4byte(pPage->aData, iTo);
74503 }else{
74504 int i;
74505 int nCell;
74506 int rc;
74507
74508 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
74509 if( rc ) return rc;
74510 nCell = pPage->nCell;
74511
74512 for(i=0; i<nCell; i++){
74513 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
74514 if( eType==PTRMAP_OVERFLOW13 ){
74515 CellInfo info;
74516 pPage->xParseCell(pPage, pCell, &info);
74517 if( info.nLocal<info.nPayload ){
74518 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
74519 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(74519);
74520 }
74521 if( iFrom==get4bytesqlite3Get4byte(pCell+info.nSize-4) ){
74522 put4bytesqlite3Put4byte(pCell+info.nSize-4, iTo);
74523 break;
74524 }
74525 }
74526 }else{
74527 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
74528 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(74528);
74529 }
74530 if( get4bytesqlite3Get4byte(pCell)==iFrom ){
74531 put4bytesqlite3Put4byte(pCell, iTo);
74532 break;
74533 }
74534 }
74535 }
74536
74537 if( i==nCell ){
74538 if( eType!=PTRMAP_BTREE5 ||
74539 get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
74540 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(74540);
74541 }
74542 put4bytesqlite3Put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
74543 }
74544 }
74545 return SQLITE_OK0;
74546}
74547
74548
74549/*
74550** Move the open database page pDbPage to location iFreePage in the
74551** database. The pDbPage reference remains valid.
74552**
74553** The isCommit flag indicates that there is no need to remember that
74554** the journal needs to be sync()ed before database page pDbPage->pgno
74555** can be written to. The caller has already promised not to write to that
74556** page.
74557*/
74558static int relocatePage(
74559 BtShared *pBt, /* Btree */
74560 MemPage *pDbPage, /* Open page to move */
74561 u8 eType, /* Pointer map 'type' entry for pDbPage */
74562 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
74563 Pgno iFreePage, /* The location to move pDbPage to */
74564 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
74565){
74566 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
74567 Pgno iDbPage = pDbPage->pgno;
74568 Pager *pPager = pBt->pPager;
74569 int rc;
74570
74571 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||((void) (0))
74572 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE )((void) (0));
74573 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74574 assert( pDbPage->pBt==pBt )((void) (0));
74575 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74575);
74576
74577 /* Move page iDbPage from its current location to page number iFreePage */
74578 TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n",
74579 iDbPage, iFreePage, iPtrPage, eType));
74580 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
74581 if( rc!=SQLITE_OK0 ){
74582 return rc;
74583 }
74584 pDbPage->pgno = iFreePage;
74585
74586 /* If pDbPage was a btree-page, then it may have child pages and/or cells
74587 ** that point to overflow pages. The pointer map entries for all these
74588 ** pages need to be changed.
74589 **
74590 ** If pDbPage is an overflow page, then the first 4 bytes may store a
74591 ** pointer to a subsequent overflow page. If this is the case, then
74592 ** the pointer map needs to be updated for the subsequent overflow page.
74593 */
74594 if( eType==PTRMAP_BTREE5 || eType==PTRMAP_ROOTPAGE1 ){
74595 rc = setChildPtrmaps(pDbPage);
74596 if( rc!=SQLITE_OK0 ){
74597 return rc;
74598 }
74599 }else{
74600 Pgno nextOvfl = get4bytesqlite3Get4byte(pDbPage->aData);
74601 if( nextOvfl!=0 ){
74602 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW24, iFreePage, &rc);
74603 if( rc!=SQLITE_OK0 ){
74604 return rc;
74605 }
74606 }
74607 }
74608
74609 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
74610 ** that it points at iFreePage. Also fix the pointer map entry for
74611 ** iPtrPage.
74612 */
74613 if( eType!=PTRMAP_ROOTPAGE1 ){
74614 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
74615 if( rc!=SQLITE_OK0 ){
74616 return rc;
74617 }
74618 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
74619 if( rc!=SQLITE_OK0 ){
74620 releasePage(pPtrPage);
74621 return rc;
74622 }
74623 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
74624 releasePage(pPtrPage);
74625 if( rc==SQLITE_OK0 ){
74626 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
74627 }
74628 }
74629 return rc;
74630}
74631
74632/* Forward declaration required by incrVacuumStep(). */
74633static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
74634
74635/*
74636** Perform a single step of an incremental-vacuum. If successful, return
74637** SQLITE_OK. If there is no work to do (and therefore no point in
74638** calling this function again), return SQLITE_DONE. Or, if an error
74639** occurs, return some other error code.
74640**
74641** More specifically, this function attempts to re-organize the database so
74642** that the last page of the file currently in use is no longer in use.
74643**
74644** Parameter nFin is the number of pages that this database would contain
74645** were this function called until it returns SQLITE_DONE.
74646**
74647** If the bCommit parameter is non-zero, this function assumes that the
74648** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
74649** or an error. bCommit is passed true for an auto-vacuum-on-commit
74650** operation, or false for an incremental vacuum.
74651*/
74652static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
74653 Pgno nFreeList; /* Number of pages still on the free-list */
74654 int rc;
74655
74656 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74657 assert( iLastPg>nFin )((void) (0));
74658
74659 if( !PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) && iLastPg!=PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
74660 u8 eType;
74661 Pgno iPtrPage;
74662
74663 nFreeList = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
74664 if( nFreeList==0 ){
74665 return SQLITE_DONE101;
74666 }
74667
74668 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
74669 if( rc!=SQLITE_OK0 ){
74670 return rc;
74671 }
74672 if( eType==PTRMAP_ROOTPAGE1 ){
74673 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74673);
74674 }
74675
74676 if( eType==PTRMAP_FREEPAGE2 ){
74677 if( bCommit==0 ){
74678 /* Remove the page from the files free-list. This is not required
74679 ** if bCommit is non-zero. In that case, the free-list will be
74680 ** truncated to zero after this function returns, so it doesn't
74681 ** matter if it still contains some garbage entries.
74682 */
74683 Pgno iFreePg;
74684 MemPage *pFreePg;
74685 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT1);
74686 if( rc!=SQLITE_OK0 ){
74687 return rc;
74688 }
74689 assert( iFreePg==iLastPg )((void) (0));
74690 releasePage(pFreePg);
74691 }
74692 } else {
74693 Pgno iFreePg; /* Index of free page to move pLastPg to */
74694 MemPage *pLastPg;
74695 u8 eMode = BTALLOC_ANY0; /* Mode parameter for allocateBtreePage() */
74696 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
74697
74698 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
74699 if( rc!=SQLITE_OK0 ){
74700 return rc;
74701 }
74702
74703 /* If bCommit is zero, this loop runs exactly once and page pLastPg
74704 ** is swapped with the first free page pulled off the free list.
74705 **
74706 ** On the other hand, if bCommit is greater than zero, then keep
74707 ** looping until a free-page located within the first nFin pages
74708 ** of the file is found.
74709 */
74710 if( bCommit==0 ){
74711 eMode = BTALLOC_LE2;
74712 iNear = nFin;
74713 }
74714 do {
74715 MemPage *pFreePg;
74716 Pgno dbSize = btreePagecount(pBt);
74717 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
74718 if( rc!=SQLITE_OK0 ){
74719 releasePage(pLastPg);
74720 return rc;
74721 }
74722 releasePage(pFreePg);
74723 if( iFreePg>dbSize ){
74724 releasePage(pLastPg);
74725 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74725);
74726 }
74727 }while( bCommit && iFreePg>nFin );
74728 assert( iFreePg<iLastPg )((void) (0));
74729
74730 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
74731 releasePage(pLastPg);
74732 if( rc!=SQLITE_OK0 ){
74733 return rc;
74734 }
74735 }
74736 }
74737
74738 if( bCommit==0 ){
74739 do {
74740 iLastPg--;
74741 }while( iLastPg==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) || PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) );
74742 pBt->bDoTruncate = 1;
74743 pBt->nPage = iLastPg;
74744 }
74745 return SQLITE_OK0;
74746}
74747
74748/*
74749** The database opened by the first argument is an auto-vacuum database
74750** nOrig pages in size containing nFree free pages. Return the expected
74751** size of the database in pages following an auto-vacuum operation.
74752*/
74753static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
74754 int nEntry; /* Number of entries on one ptrmap page */
74755 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
74756 Pgno nFin; /* Return value */
74757
74758 nEntry = pBt->usableSize/5;
74759 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)ptrmapPageno(pBt, nOrig)+nEntry)/nEntry;
74760 nFin = nOrig - nFree - nPtrmap;
74761 if( nOrig>PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) && nFin<PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
74762 nFin--;
74763 }
74764 while( PTRMAP_ISPAGE(pBt, nFin)(ptrmapPageno((pBt), (nFin))==(nFin)) || nFin==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
74765 nFin--;
74766 }
74767
74768 return nFin;
74769}
74770
74771/*
74772** A write-transaction must be opened before calling this function.
74773** It performs a single unit of work towards an incremental vacuum.
74774**
74775** If the incremental vacuum is finished after this function has run,
74776** SQLITE_DONE is returned. If it is not finished, but no error occurred,
74777** SQLITE_OK is returned. Otherwise an SQLite error code.
74778*/
74779SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *p){
74780 int rc;
74781 BtShared *pBt = p->pBt;
74782
74783 sqlite3BtreeEnter(p);
74784 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE )((void) (0));
74785 if( !pBt->autoVacuum ){
74786 rc = SQLITE_DONE101;
74787 }else{
74788 Pgno nOrig = btreePagecount(pBt);
74789 Pgno nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
74790 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
74791
74792 if( nOrig<nFin || nFree>=nOrig ){
74793 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(74793);
74794 }else if( nFree>0 ){
74795 rc = saveAllCursors(pBt, 0, 0);
74796 if( rc==SQLITE_OK0 ){
74797 invalidateAllOverflowCache(pBt);
74798 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
74799 }
74800 if( rc==SQLITE_OK0 ){
74801 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74802 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], pBt->nPage);
74803 }
74804 }else{
74805 rc = SQLITE_DONE101;
74806 }
74807 }
74808 sqlite3BtreeLeave(p);
74809 return rc;
74810}
74811
74812/*
74813** This routine is called prior to sqlite3PagerCommit when a transaction
74814** is committed for an auto-vacuum database.
74815*/
74816static int autoVacuumCommit(Btree *p){
74817 int rc = SQLITE_OK0;
74818 Pager *pPager;
74819 BtShared *pBt;
74820 sqlite3 *db;
74821 VVA_ONLY( int nRef );
74822
74823 assert( p!=0 )((void) (0));
74824 pBt = p->pBt;
74825 pPager = pBt->pPager;
74826 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
74827
74828 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
74829 invalidateAllOverflowCache(pBt);
74830 assert(pBt->autoVacuum)((void) (0));
74831 if( !pBt->incrVacuum ){
74832 Pgno nFin; /* Number of pages in database after autovacuuming */
74833 Pgno nFree; /* Number of pages on the freelist initially */
74834 Pgno nVac; /* Number of pages to vacuum */
74835 Pgno iFree; /* The next page to be freed */
74836 Pgno nOrig; /* Database size before freeing */
74837
74838 nOrig = btreePagecount(pBt);
74839 if( PTRMAP_ISPAGE(pBt, nOrig)(ptrmapPageno((pBt), (nOrig))==(nOrig)) || nOrig==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
74840 /* It is not possible to create a database for which the final page
74841 ** is either a pointer-map page or the pending-byte page. If one
74842 ** is encountered, this indicates corruption.
74843 */
74844 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74844);
74845 }
74846
74847 nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
74848 db = p->db;
74849 if( db->xAutovacPages ){
74850 int iDb;
74851 for(iDb=0; ALWAYS(iDb<db->nDb)(iDb<db->nDb); iDb++){
74852 if( db->aDb[iDb].pBt==p ) break;
74853 }
74854 nVac = db->xAutovacPages(
74855 db->pAutovacPagesArg,
74856 db->aDb[iDb].zDbSName,
74857 nOrig,
74858 nFree,
74859 pBt->pageSize
74860 );
74861 if( nVac>nFree ){
74862 nVac = nFree;
74863 }
74864 if( nVac==0 ){
74865 return SQLITE_OK0;
74866 }
74867 }else{
74868 nVac = nFree;
74869 }
74870 nFin = finalDbSize(pBt, nOrig, nVac);
74871 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(74871);
74872 if( nFin<nOrig ){
74873 rc = saveAllCursors(pBt, 0, 0);
74874 }
74875 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK0; iFree--){
74876 rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree);
74877 }
74878 if( (rc==SQLITE_DONE101 || rc==SQLITE_OK0) && nFree>0 ){
74879 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74880 if( nVac==nFree ){
74881 put4bytesqlite3Put4byte(&pBt->pPage1->aData[32], 0);
74882 put4bytesqlite3Put4byte(&pBt->pPage1->aData[36], 0);
74883 }
74884 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], nFin);
74885 pBt->bDoTruncate = 1;
74886 pBt->nPage = nFin;
74887 }
74888 if( rc!=SQLITE_OK0 ){
74889 sqlite3PagerRollback(pPager);
74890 }
74891 }
74892
74893 assert( nRef>=sqlite3PagerRefcount(pPager) )((void) (0));
74894 return rc;
74895}
74896
74897#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
74898# define setChildPtrmaps(x) SQLITE_OK0
74899#endif
74900
74901/*
74902** This routine does the first phase of a two-phase commit. This routine
74903** causes a rollback journal to be created (if it does not already exist)
74904** and populated with enough information so that if a power loss occurs
74905** the database can be restored to its original state by playing back
74906** the journal. Then the contents of the journal are flushed out to
74907** the disk. After the journal is safely on oxide, the changes to the
74908** database are written into the database file and flushed to oxide.
74909** At the end of this call, the rollback journal still exists on the
74910** disk and we are still holding all locks, so the transaction has not
74911** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
74912** commit process.
74913**
74914** This call is a no-op if no write-transaction is currently active on pBt.
74915**
74916** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
74917** the name of a super-journal file that should be written into the
74918** individual journal file, or is NULL, indicating no super-journal file
74919** (single database transaction).
74920**
74921** When this is called, the super-journal should already have been
74922** created, populated with this journal pointer and synced to disk.
74923**
74924** Once this is routine has returned, the only thing required to commit
74925** the write-transaction for this database file is to delete the journal.
74926*/
74927SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
74928 int rc = SQLITE_OK0;
74929 if( p->inTrans==TRANS_WRITE2 ){
74930 BtShared *pBt = p->pBt;
74931 sqlite3BtreeEnter(p);
74932#ifndef SQLITE_OMIT_AUTOVACUUM
74933 if( pBt->autoVacuum ){
74934 rc = autoVacuumCommit(p);
74935 if( rc!=SQLITE_OK0 ){
74936 sqlite3BtreeLeave(p);
74937 return rc;
74938 }
74939 }
74940 if( pBt->bDoTruncate ){
74941 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
74942 }
74943#endif
74944 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
74945 sqlite3BtreeLeave(p);
74946 }
74947 return rc;
74948}
74949
74950/*
74951** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
74952** at the conclusion of a transaction.
74953*/
74954static void btreeEndTransaction(Btree *p){
74955 BtShared *pBt = p->pBt;
74956 sqlite3 *db = p->db;
74957 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
74958
74959#ifndef SQLITE_OMIT_AUTOVACUUM
74960 pBt->bDoTruncate = 0;
74961#endif
74962 if( p->inTrans>TRANS_NONE0 && db->nVdbeRead>1 ){
74963 /* If there are other active statements that belong to this database
74964 ** handle, downgrade to a read-only transaction. The other statements
74965 ** may still be reading from the database. */
74966 downgradeAllSharedCacheTableLocks(p);
74967 p->inTrans = TRANS_READ1;
74968 }else{
74969 /* If the handle had any kind of transaction open, decrement the
74970 ** transaction count of the shared btree. If the transaction count
74971 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
74972 ** call below will unlock the pager. */
74973 if( p->inTrans!=TRANS_NONE0 ){
74974 clearAllSharedCacheTableLocks(p);
74975 pBt->nTransaction--;
74976 if( 0==pBt->nTransaction ){
74977 pBt->inTransaction = TRANS_NONE0;
74978 }
74979 }
74980
74981 /* Set the current transaction state to TRANS_NONE and unlock the
74982 ** pager if this call closed the only read or write transaction. */
74983 p->inTrans = TRANS_NONE0;
74984 unlockBtreeIfUnused(pBt);
74985 }
74986
74987 btreeIntegrity(p)((void) (0)); ((void) (0));;
74988}
74989
74990/*
74991** Commit the transaction currently in progress.
74992**
74993** This routine implements the second phase of a 2-phase commit. The
74994** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
74995** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
74996** routine did all the work of writing information out to disk and flushing the
74997** contents so that they are written onto the disk platter. All this
74998** routine has to do is delete or truncate or zero the header in the
74999** the rollback journal (which causes the transaction to commit) and
75000** drop locks.
75001**
75002** Normally, if an error occurs while the pager layer is attempting to
75003** finalize the underlying journal file, this function returns an error and
75004** the upper layer will attempt a rollback. However, if the second argument
75005** is non-zero then this b-tree transaction is part of a multi-file
75006** transaction. In this case, the transaction has already been committed
75007** (by deleting a super-journal file) and the caller will ignore this
75008** functions return code. So, even if an error occurs in the pager layer,
75009** reset the b-tree objects internal state to indicate that the write
75010** transaction has been closed. This is quite safe, as the pager will have
75011** transitioned to the error state.
75012**
75013** This will release the write lock on the database file. If there
75014** are no active cursors, it also releases the read lock.
75015*/
75016SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
75017
75018 if( p->inTrans==TRANS_NONE0 ) return SQLITE_OK0;
75019 sqlite3BtreeEnter(p);
75020 btreeIntegrity(p)((void) (0)); ((void) (0));;
75021
75022 /* If the handle has a write-transaction open, commit the shared-btrees
75023 ** transaction and set the shared state to TRANS_READ.
75024 */
75025 if( p->inTrans==TRANS_WRITE2 ){
75026 int rc;
75027 BtShared *pBt = p->pBt;
75028 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
75029 assert( pBt->nTransaction>0 )((void) (0));
75030 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
75031 if( rc!=SQLITE_OK0 && bCleanup==0 ){
75032 sqlite3BtreeLeave(p);
75033 return rc;
75034 }
75035 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
75036 pBt->inTransaction = TRANS_READ1;
75037 btreeClearHasContent(pBt);
75038 }
75039
75040 btreeEndTransaction(p);
75041 sqlite3BtreeLeave(p);
75042 return SQLITE_OK0;
75043}
75044
75045/*
75046** Do both phases of a commit.
75047*/
75048SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree *p){
75049 int rc;
75050 sqlite3BtreeEnter(p);
75051 rc = sqlite3BtreeCommitPhaseOne(p, 0);
75052 if( rc==SQLITE_OK0 ){
75053 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
75054 }
75055 sqlite3BtreeLeave(p);
75056 return rc;
75057}
75058
75059/*
75060** This routine sets the state to CURSOR_FAULT and the error
75061** code to errCode for every cursor on any BtShared that pBtree
75062** references. Or if the writeOnly flag is set to 1, then only
75063** trip write cursors and leave read cursors unchanged.
75064**
75065** Every cursor is a candidate to be tripped, including cursors
75066** that belong to other database connections that happen to be
75067** sharing the cache with pBtree.
75068**
75069** This routine gets called when a rollback occurs. If the writeOnly
75070** flag is true, then only write-cursors need be tripped - read-only
75071** cursors save their current positions so that they may continue
75072** following the rollback. Or, if writeOnly is false, all cursors are
75073** tripped. In general, writeOnly is false if the transaction being
75074** rolled back modified the database schema. In this case b-tree root
75075** pages may be moved or deleted from the database altogether, making
75076** it unsafe for read cursors to continue.
75077**
75078** If the writeOnly flag is true and an error is encountered while
75079** saving the current position of a read-only cursor, all cursors,
75080** including all read-cursors are tripped.
75081**
75082** SQLITE_OK is returned if successful, or if an error occurs while
75083** saving a cursor position, an SQLite error code.
75084*/
75085SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
75086 BtCursor *p;
75087 int rc = SQLITE_OK0;
75088
75089 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 )((void) (0));
75090 if( pBtree ){
75091 sqlite3BtreeEnter(pBtree);
75092 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
75093 if( writeOnly && (p->curFlags & BTCF_WriteFlag0x01)==0 ){
75094 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
75095 rc = saveCursorPosition(p);
75096 if( rc!=SQLITE_OK0 ){
75097 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
75098 break;
75099 }
75100 }
75101 }else{
75102 sqlite3BtreeClearCursor(p);
75103 p->eState = CURSOR_FAULT4;
75104 p->skipNext = errCode;
75105 }
75106 btreeReleaseAllCursorPages(p);
75107 }
75108 sqlite3BtreeLeave(pBtree);
75109 }
75110 return rc;
75111}
75112
75113/*
75114** Set the pBt->nPage field correctly, according to the current
75115** state of the database. Assume pBt->pPage1 is valid.
75116*/
75117static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
75118 int nPage = get4bytesqlite3Get4byte(&pPage1->aData[28]);
75119 testcase( nPage==0 );
75120 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
75121 testcase( pBt->nPage!=(u32)nPage );
75122 pBt->nPage = nPage;
75123}
75124
75125/*
75126** Rollback the transaction in progress.
75127**
75128** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
75129** Only write cursors are tripped if writeOnly is true but all cursors are
75130** tripped if writeOnly is false. Any attempt to use
75131** a tripped cursor will result in an error.
75132**
75133** This will release the write lock on the database file. If there
75134** are no active cursors, it also releases the read lock.
75135*/
75136SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
75137 int rc;
75138 BtShared *pBt = p->pBt;
75139 MemPage *pPage1;
75140
75141 assert( writeOnly==1 || writeOnly==0 )((void) (0));
75142 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK )((void) (0));
75143 sqlite3BtreeEnter(p);
75144 if( tripCode==SQLITE_OK0 ){
75145 rc = tripCode = saveAllCursors(pBt, 0, 0);
75146 if( rc ) writeOnly = 0;
75147 }else{
75148 rc = SQLITE_OK0;
75149 }
75150 if( tripCode ){
75151 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
75152 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) )((void) (0));
75153 if( rc2!=SQLITE_OK0 ) rc = rc2;
75154 }
75155 btreeIntegrity(p)((void) (0)); ((void) (0));;
75156
75157 if( p->inTrans==TRANS_WRITE2 ){
75158 int rc2;
75159
75160 assert( TRANS_WRITE==pBt->inTransaction )((void) (0));
75161 rc2 = sqlite3PagerRollback(pBt->pPager);
75162 if( rc2!=SQLITE_OK0 ){
75163 rc = rc2;
75164 }
75165
75166 /* The rollback may have destroyed the pPage1->aData value. So
75167 ** call btreeGetPage() on page 1 again to make
75168 ** sure pPage1->aData is set correctly. */
75169 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK0 ){
75170 btreeSetNPage(pBt, pPage1);
75171 releasePageOne(pPage1);
75172 }
75173 assert( countValidCursors(pBt, 1)==0 )((void) (0));
75174 pBt->inTransaction = TRANS_READ1;
75175 btreeClearHasContent(pBt);
75176 }
75177
75178 btreeEndTransaction(p);
75179 sqlite3BtreeLeave(p);
75180 return rc;
75181}
75182
75183/*
75184** Start a statement subtransaction. The subtransaction can be rolled
75185** back independently of the main transaction. You must start a transaction
75186** before starting a subtransaction. The subtransaction is ended automatically
75187** if the main transaction commits or rolls back.
75188**
75189** Statement subtransactions are used around individual SQL statements
75190** that are contained within a BEGIN...COMMIT block. If a constraint
75191** error occurs within the statement, the effect of that one statement
75192** can be rolled back without having to rollback the entire transaction.
75193**
75194** A statement sub-transaction is implemented as an anonymous savepoint. The
75195** value passed as the second parameter is the total number of savepoints,
75196** including the new anonymous savepoint, open on the B-Tree. i.e. if there
75197** are no active savepoints and no other statement-transactions open,
75198** iStatement is 1. This anonymous savepoint can be released or rolled back
75199** using the sqlite3BtreeSavepoint() function.
75200*/
75201SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
75202 int rc;
75203 BtShared *pBt = p->pBt;
75204 sqlite3BtreeEnter(p);
75205 assert( p->inTrans==TRANS_WRITE )((void) (0));
75206 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
75207 assert( iStatement>0 )((void) (0));
75208 assert( iStatement>p->db->nSavepoint )((void) (0));
75209 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
75210 /* At the pager level, a statement transaction is a savepoint with
75211 ** an index greater than all savepoints created explicitly using
75212 ** SQL statements. It is illegal to open, release or rollback any
75213 ** such savepoints while the statement transaction savepoint is active.
75214 */
75215 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
75216 sqlite3BtreeLeave(p);
75217 return rc;
75218}
75219
75220/*
75221** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
75222** or SAVEPOINT_RELEASE. This function either releases or rolls back the
75223** savepoint identified by parameter iSavepoint, depending on the value
75224** of op.
75225**
75226** Normally, iSavepoint is greater than or equal to zero. However, if op is
75227** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
75228** contents of the entire transaction are rolled back. This is different
75229** from a normal transaction rollback, as no locks are released and the
75230** transaction remains open.
75231*/
75232SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
75233 int rc = SQLITE_OK0;
75234 if( p && p->inTrans==TRANS_WRITE2 ){
75235 BtShared *pBt = p->pBt;
75236 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
75237 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) )((void) (0));
75238 sqlite3BtreeEnter(p);
75239 if( op==SAVEPOINT_ROLLBACK2 ){
75240 rc = saveAllCursors(pBt, 0, 0);
75241 }
75242 if( rc==SQLITE_OK0 ){
75243 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
75244 }
75245 if( rc==SQLITE_OK0 ){
75246 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY0x0010)!=0 ){
75247 pBt->nPage = 0;
75248 }
75249 rc = newDatabase(pBt);
75250 btreeSetNPage(pBt, pBt->pPage1);
75251
75252 /* pBt->nPage might be zero if the database was corrupt when
75253 ** the transaction was started. Otherwise, it must be at least 1. */
75254 assert( CORRUPT_DB || pBt->nPage>0 )((void) (0));
75255 }
75256 sqlite3BtreeLeave(p);
75257 }
75258 return rc;
75259}
75260
75261/*
75262** Create a new cursor for the BTree whose root is on the page
75263** iTable. If a read-only cursor is requested, it is assumed that
75264** the caller already has at least a read-only transaction open
75265** on the database already. If a write-cursor is requested, then
75266** the caller is assumed to have an open write transaction.
75267**
75268** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
75269** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
75270** can be used for reading or for writing if other conditions for writing
75271** are also met. These are the conditions that must be met in order
75272** for writing to be allowed:
75273**
75274** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
75275**
75276** 2: Other database connections that share the same pager cache
75277** but which are not in the READ_UNCOMMITTED state may not have
75278** cursors open with wrFlag==0 on the same table. Otherwise
75279** the changes made by this write cursor would be visible to
75280** the read cursors in the other database connection.
75281**
75282** 3: The database must be writable (not on read-only media)
75283**
75284** 4: There must be an active transaction.
75285**
75286** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
75287** is set. If FORDELETE is set, that is a hint to the implementation that
75288** this cursor will only be used to seek to and delete entries of an index
75289** as part of a larger DELETE statement. The FORDELETE hint is not used by
75290** this implementation. But in a hypothetical alternative storage engine
75291** in which index entries are automatically deleted when corresponding table
75292** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
75293** operations on this cursor can be no-ops and all READ operations can
75294** return a null row (2-bytes: 0x01 0x00).
75295**
75296** No checking is done to make sure that page iTable really is the
75297** root page of a b-tree. If it is not, then the cursor acquired
75298** will not work correctly.
75299**
75300** It is assumed that the sqlite3BtreeCursorZero() has been called
75301** on pCur to initialize the memory space prior to invoking this routine.
75302*/
75303static int btreeCursor(
75304 Btree *p, /* The btree */
75305 Pgno iTable, /* Root page of table to open */
75306 int wrFlag, /* 1 to write. 0 read-only */
75307 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
75308 BtCursor *pCur /* Space for new cursor */
75309){
75310 BtShared *pBt = p->pBt; /* Shared b-tree handle */
75311 BtCursor *pX; /* Looping over other all cursors */
75312
75313 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
75314 assert( wrFlag==0((void) (0))
75315 || wrFlag==BTREE_WRCSR((void) (0))
75316 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)((void) (0))
75317 )((void) (0));
75318
75319 /* The following assert statements verify that if this is a sharable
75320 ** b-tree database, the connection is holding the required table locks,
75321 ** and that no other connection has any open cursor that conflicts with
75322 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
75323 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))((void) (0))
75324 || iTable<1 )((void) (0));
75325 assert( wrFlag==0 || !hasReadConflicts(p, iTable) )((void) (0));
75326
75327 /* Assert that the caller has opened the required transaction. */
75328 assert( p->inTrans>TRANS_NONE )((void) (0));
75329 assert( wrFlag==0 || p->inTrans==TRANS_WRITE )((void) (0));
75330 assert( pBt->pPage1 && pBt->pPage1->aData )((void) (0));
75331 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
75332
75333 if( iTable<=1 ){
75334 if( iTable<1 ){
75335 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75335);
75336 }else if( btreePagecount(pBt)==0 ){
75337 assert( wrFlag==0 )((void) (0));
75338 iTable = 0;
75339 }
75340 }
75341
75342 /* Now that no other errors can occur, finish filling in the BtCursor
75343 ** variables and link the cursor into the BtShared list. */
75344 pCur->pgnoRoot = iTable;
75345 pCur->iPage = -1;
75346 pCur->pKeyInfo = pKeyInfo;
75347 pCur->pBtree = p;
75348 pCur->pBt = pBt;
75349 pCur->curFlags = 0;
75350 /* If there are two or more cursors on the same btree, then all such
75351 ** cursors *must* have the BTCF_Multiple flag set. */
75352 for(pX=pBt->pCursor; pX; pX=pX->pNext){
75353 if( pX->pgnoRoot==iTable ){
75354 pX->curFlags |= BTCF_Multiple0x20;
75355 pCur->curFlags = BTCF_Multiple0x20;
75356 }
75357 }
75358 pCur->eState = CURSOR_INVALID1;
75359 pCur->pNext = pBt->pCursor;
75360 pBt->pCursor = pCur;
75361 if( wrFlag ){
75362 pCur->curFlags |= BTCF_WriteFlag0x01;
75363 pCur->curPagerFlags = 0;
75364 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
75365 }else{
75366 pCur->curPagerFlags = PAGER_GET_READONLY0x02;
75367 }
75368 return SQLITE_OK0;
75369}
75370static int btreeCursorWithLock(
75371 Btree *p, /* The btree */
75372 Pgno iTable, /* Root page of table to open */
75373 int wrFlag, /* 1 to write. 0 read-only */
75374 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
75375 BtCursor *pCur /* Space for new cursor */
75376){
75377 int rc;
75378 sqlite3BtreeEnter(p);
75379 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
75380 sqlite3BtreeLeave(p);
75381 return rc;
75382}
75383SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
75384 Btree *p, /* The btree */
75385 Pgno iTable, /* Root page of table to open */
75386 int wrFlag, /* 1 to write. 0 read-only */
75387 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
75388 BtCursor *pCur /* Write new cursor here */
75389){
75390 if( p->sharable ){
75391 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
75392 }else{
75393 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
75394 }
75395}
75396
75397/*
75398** Return the size of a BtCursor object in bytes.
75399**
75400** This interfaces is needed so that users of cursors can preallocate
75401** sufficient storage to hold a cursor. The BtCursor object is opaque
75402** to users so they cannot do the sizeof() themselves - they must call
75403** this routine.
75404*/
75405SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void){
75406 return ROUND8(sizeof(BtCursor))(((sizeof(BtCursor))+7)&~7);
75407}
75408
75409#ifdef SQLITE_DEBUG
75410/*
75411** Return true if and only if the Btree object will be automatically
75412** closed with the BtCursor closes. This is used within assert() statements
75413** only.
75414*/
75415SQLITE_PRIVATEstatic int sqlite3BtreeClosesWithCursor(
75416 Btree *pBtree, /* the btree object */
75417 BtCursor *pCur /* Corresponding cursor */
75418){
75419 BtShared *pBt = pBtree->pBt;
75420 if( (pBt->openFlags & BTREE_SINGLE4)==0 ) return 0;
75421 if( pBt->pCursor!=pCur ) return 0;
75422 if( pCur->pNext!=0 ) return 0;
75423 if( pCur->pBtree!=pBtree ) return 0;
75424 return 1;
75425}
75426#endif
75427
75428/*
75429** Initialize memory that will be converted into a BtCursor object.
75430**
75431** The simple approach here would be to memset() the entire object
75432** to zero. But it turns out that the apPage[] and aiIdx[] arrays
75433** do not need to be zeroed and they are large, so we can save a lot
75434** of run-time by skipping the initialization of those elements.
75435*/
75436SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor *p){
75437 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)__builtin_offsetof(BtCursor, pBt));
75438}
75439
75440/*
75441** Close a cursor. The read lock on the database file is released
75442** when the last cursor is closed.
75443*/
75444SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor *pCur){
75445 Btree *pBtree = pCur->pBtree;
75446 if( pBtree ){
75447 BtShared *pBt = pCur->pBt;
75448 sqlite3BtreeEnter(pBtree);
75449 assert( pBt->pCursor!=0 )((void) (0));
75450 if( pBt->pCursor==pCur ){
75451 pBt->pCursor = pCur->pNext;
75452 }else{
75453 BtCursor *pPrev = pBt->pCursor;
75454 do{
75455 if( pPrev->pNext==pCur ){
75456 pPrev->pNext = pCur->pNext;
75457 break;
75458 }
75459 pPrev = pPrev->pNext;
75460 }while( ALWAYS(pPrev)(pPrev) );
75461 }
75462 btreeReleaseAllCursorPages(pCur);
75463 unlockBtreeIfUnused(pBt);
75464 sqlite3_free(pCur->aOverflow);
75465 sqlite3_free(pCur->pKey);
75466 if( (pBt->openFlags & BTREE_SINGLE4) && pBt->pCursor==0 ){
75467 /* Since the BtShared is not sharable, there is no need to
75468 ** worry about the missing sqlite3BtreeLeave() call here. */
75469 assert( pBtree->sharable==0 )((void) (0));
75470 sqlite3BtreeClose(pBtree);
75471 }else{
75472 sqlite3BtreeLeave(pBtree);
75473 }
75474 pCur->pBtree = 0;
75475 }
75476 return SQLITE_OK0;
75477}
75478
75479/*
75480** Make sure the BtCursor* given in the argument has a valid
75481** BtCursor.info structure. If it is not already valid, call
75482** btreeParseCell() to fill it in.
75483**
75484** BtCursor.info is a cache of the information in the current cell.
75485** Using this cache reduces the number of calls to btreeParseCell().
75486*/
75487#ifndef NDEBUG1
75488 static int cellInfoEqual(CellInfo *a, CellInfo *b){
75489 if( a->nKey!=b->nKey ) return 0;
75490 if( a->pPayload!=b->pPayload ) return 0;
75491 if( a->nPayload!=b->nPayload ) return 0;
75492 if( a->nLocal!=b->nLocal ) return 0;
75493 if( a->nSize!=b->nSize ) return 0;
75494 return 1;
75495 }
75496 static void assertCellInfo(BtCursor *pCur){
75497 CellInfo info;
75498 memset(&info, 0, sizeof(info));
75499 btreeParseCell(pCur->pPage, pCur->ix, &info);
75500 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) )((void) (0));
75501 }
75502#else
75503 #define assertCellInfo(x)
75504#endif
75505static SQLITE_NOINLINE__attribute__((noinline)) void getCellInfo(BtCursor *pCur){
75506 if( pCur->info.nSize==0 ){
75507 pCur->curFlags |= BTCF_ValidNKey0x02;
75508 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
75509 }else{
75510 assertCellInfo(pCur);
75511 }
75512}
75513
75514#ifndef NDEBUG1 /* The next routine used only within assert() statements */
75515/*
75516** Return true if the given BtCursor is valid. A valid cursor is one
75517** that is currently pointing to a row in a (non-empty) table.
75518** This is a verification routine is used only within assert() statements.
75519*/
75520SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor *pCur){
75521 return pCur && pCur->eState==CURSOR_VALID0;
75522}
75523#endif /* NDEBUG */
75524SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
75525 assert( pCur!=0 )((void) (0));
75526 return pCur->eState==CURSOR_VALID0;
75527}
75528
75529/*
75530** Return the value of the integer key or "rowid" for a table btree.
75531** This routine is only valid for a cursor that is pointing into a
75532** ordinary table btree. If the cursor points to an index btree or
75533** is invalid, the result of this routine is undefined.
75534*/
75535SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
75536 assert( cursorHoldsMutex(pCur) )((void) (0));
75537 assert( pCur->eState==CURSOR_VALID )((void) (0));
75538 assert( pCur->curIntKey )((void) (0));
75539 getCellInfo(pCur);
75540 return pCur->info.nKey;
75541}
75542
75543/*
75544** Pin or unpin a cursor.
75545*/
75546SQLITE_PRIVATEstatic void sqlite3BtreeCursorPin(BtCursor *pCur){
75547 assert( (pCur->curFlags & BTCF_Pinned)==0 )((void) (0));
75548 pCur->curFlags |= BTCF_Pinned0x40;
75549}
75550SQLITE_PRIVATEstatic void sqlite3BtreeCursorUnpin(BtCursor *pCur){
75551 assert( (pCur->curFlags & BTCF_Pinned)!=0 )((void) (0));
75552 pCur->curFlags &= ~BTCF_Pinned0x40;
75553}
75554
75555/*
75556** Return the offset into the database file for the start of the
75557** payload to which the cursor is pointing.
75558*/
75559SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor *pCur){
75560 assert( cursorHoldsMutex(pCur) )((void) (0));
75561 assert( pCur->eState==CURSOR_VALID )((void) (0));
75562 getCellInfo(pCur);
75563 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
75564 (i64)(pCur->info.pPayload - pCur->pPage->aData);
75565}
75566
75567/*
75568** Return the number of bytes of payload for the entry that pCur is
75569** currently pointing to. For table btrees, this will be the amount
75570** of data. For index btrees, this will be the size of the key.
75571**
75572** The caller must guarantee that the cursor is pointing to a non-NULL
75573** valid entry. In other words, the calling procedure must guarantee
75574** that the cursor has Cursor.eState==CURSOR_VALID.
75575*/
75576SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor *pCur){
75577 assert( cursorHoldsMutex(pCur) )((void) (0));
75578 assert( pCur->eState==CURSOR_VALID )((void) (0));
75579 getCellInfo(pCur);
75580 return pCur->info.nPayload;
75581}
75582
75583/*
75584** Return an upper bound on the size of any record for the table
75585** that the cursor is pointing into.
75586**
75587** This is an optimization. Everything will still work if this
75588** routine always returns 2147483647 (which is the largest record
75589** that SQLite can handle) or more. But returning a smaller value might
75590** prevent large memory allocations when trying to interpret a
75591** corrupt database.
75592**
75593** The current implementation merely returns the size of the underlying
75594** database file.
75595*/
75596SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
75597 assert( cursorHoldsMutex(pCur) )((void) (0));
75598 assert( pCur->eState==CURSOR_VALID )((void) (0));
75599 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
75600}
75601
75602/*
75603** Given the page number of an overflow page in the database (parameter
75604** ovfl), this function finds the page number of the next page in the
75605** linked list of overflow pages. If possible, it uses the auto-vacuum
75606** pointer-map data instead of reading the content of page ovfl to do so.
75607**
75608** If an error occurs an SQLite error code is returned. Otherwise:
75609**
75610** The page number of the next overflow page in the linked list is
75611** written to *pPgnoNext. If page ovfl is the last page in its linked
75612** list, *pPgnoNext is set to zero.
75613**
75614** If ppPage is not NULL, and a reference to the MemPage object corresponding
75615** to page number pOvfl was obtained, then *ppPage is set to point to that
75616** reference. It is the responsibility of the caller to call releasePage()
75617** on *ppPage to free the reference. In no reference was obtained (because
75618** the pointer-map was used to obtain the value for *pPgnoNext), then
75619** *ppPage is set to zero.
75620*/
75621static int getOverflowPage(
75622 BtShared *pBt, /* The database file */
75623 Pgno ovfl, /* Current overflow page number */
75624 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
75625 Pgno *pPgnoNext /* OUT: Next overflow page number */
75626){
75627 Pgno next = 0;
75628 MemPage *pPage = 0;
75629 int rc = SQLITE_OK0;
75630
75631 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
75632 assert(pPgnoNext)((void) (0));
75633
75634#ifndef SQLITE_OMIT_AUTOVACUUM
75635 /* Try to find the next page in the overflow list using the
75636 ** autovacuum pointer-map pages. Guess that the next page in
75637 ** the overflow list is page number (ovfl+1). If that guess turns
75638 ** out to be wrong, fall back to loading the data of page
75639 ** number ovfl to determine the next page number.
75640 */
75641 if( pBt->autoVacuum ){
75642 Pgno pgno;
75643 Pgno iGuess = ovfl+1;
75644 u8 eType;
75645
75646 while( PTRMAP_ISPAGE(pBt, iGuess)(ptrmapPageno((pBt), (iGuess))==(iGuess)) || iGuess==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
75647 iGuess++;
75648 }
75649
75650 if( iGuess<=btreePagecount(pBt) ){
75651 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
75652 if( rc==SQLITE_OK0 && eType==PTRMAP_OVERFLOW24 && pgno==ovfl ){
75653 next = iGuess;
75654 rc = SQLITE_DONE101;
75655 }
75656 }
75657 }
75658#endif
75659
75660 assert( next==0 || rc==SQLITE_DONE )((void) (0));
75661 if( rc==SQLITE_OK0 ){
75662 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY0x02 : 0);
75663 assert( rc==SQLITE_OK || pPage==0 )((void) (0));
75664 if( rc==SQLITE_OK0 ){
75665 next = get4bytesqlite3Get4byte(pPage->aData);
75666 }
75667 }
75668
75669 *pPgnoNext = next;
75670 if( ppPage ){
75671 *ppPage = pPage;
75672 }else{
75673 releasePage(pPage);
75674 }
75675 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
75676}
75677
75678/*
75679** Copy data from a buffer to a page, or from a page to a buffer.
75680**
75681** pPayload is a pointer to data stored on database page pDbPage.
75682** If argument eOp is false, then nByte bytes of data are copied
75683** from pPayload to the buffer pointed at by pBuf. If eOp is true,
75684** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
75685** of data are copied from the buffer pBuf to pPayload.
75686**
75687** SQLITE_OK is returned on success, otherwise an error code.
75688*/
75689static int copyPayload(
75690 void *pPayload, /* Pointer to page data */
75691 void *pBuf, /* Pointer to buffer */
75692 int nByte, /* Number of bytes to copy */
75693 int eOp, /* 0 -> copy from page, 1 -> copy to page */
75694 DbPage *pDbPage /* Page containing pPayload */
75695){
75696 if( eOp ){
75697 /* Copy data from buffer to page (a write operation) */
75698 int rc = sqlite3PagerWrite(pDbPage);
75699 if( rc!=SQLITE_OK0 ){
75700 return rc;
75701 }
75702 memcpy(pPayload, pBuf, nByte);
75703 }else{
75704 /* Copy data from page to buffer (a read operation) */
75705 memcpy(pBuf, pPayload, nByte);
75706 }
75707 return SQLITE_OK0;
75708}
75709
75710/*
75711** This function is used to read or overwrite payload information
75712** for the entry that the pCur cursor is pointing to. The eOp
75713** argument is interpreted as follows:
75714**
75715** 0: The operation is a read. Populate the overflow cache.
75716** 1: The operation is a write. Populate the overflow cache.
75717**
75718** A total of "amt" bytes are read or written beginning at "offset".
75719** Data is read to or from the buffer pBuf.
75720**
75721** The content being read or written might appear on the main page
75722** or be scattered out on multiple overflow pages.
75723**
75724** If the current cursor entry uses one or more overflow pages
75725** this function may allocate space for and lazily populate
75726** the overflow page-list cache array (BtCursor.aOverflow).
75727** Subsequent calls use this cache to make seeking to the supplied offset
75728** more efficient.
75729**
75730** Once an overflow page-list cache has been allocated, it must be
75731** invalidated if some other cursor writes to the same table, or if
75732** the cursor is moved to a different row. Additionally, in auto-vacuum
75733** mode, the following events may invalidate an overflow page-list cache.
75734**
75735** * An incremental vacuum,
75736** * A commit in auto_vacuum="full" mode,
75737** * Creating a table (may require moving an overflow page).
75738*/
75739static int accessPayload(
75740 BtCursor *pCur, /* Cursor pointing to entry to read from */
75741 u32 offset, /* Begin reading this far into payload */
75742 u32 amt, /* Read this many bytes */
75743 unsigned char *pBuf, /* Write the bytes into this buffer */
75744 int eOp /* zero to read. non-zero to write. */
75745){
75746 unsigned char *aPayload;
75747 int rc = SQLITE_OK0;
75748 int iIdx = 0;
75749 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
75750 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
75751#ifdef SQLITE_DIRECT_OVERFLOW_READ1
75752 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
75753#endif
75754
75755 assert( pPage )((void) (0));
75756 assert( eOp==0 || eOp==1 )((void) (0));
75757 assert( pCur->eState==CURSOR_VALID )((void) (0));
75758 if( pCur->ix>=pPage->nCell ){
75759 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75759);
75760 }
75761 assert( cursorHoldsMutex(pCur) )((void) (0));
75762
75763 getCellInfo(pCur);
75764 aPayload = pCur->info.pPayload;
75765 assert( offset+amt <= pCur->info.nPayload )((void) (0));
75766
75767 assert( aPayload > pPage->aData )((void) (0));
75768 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
75769 /* Trying to read or write past the end of the data is an error. The
75770 ** conditional above is really:
75771 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
75772 ** but is recast into its current form to avoid integer overflow problems
75773 */
75774 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75774);
75775 }
75776
75777 /* Check if data must be read/written to/from the btree page itself. */
75778 if( offset<pCur->info.nLocal ){
75779 int a = amt;
75780 if( a+offset>pCur->info.nLocal ){
75781 a = pCur->info.nLocal - offset;
75782 }
75783 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
75784 offset = 0;
75785 pBuf += a;
75786 amt -= a;
75787 }else{
75788 offset -= pCur->info.nLocal;
75789 }
75790
75791
75792 if( rc==SQLITE_OK0 && amt>0 ){
75793 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
75794 Pgno nextPage;
75795
75796 nextPage = get4bytesqlite3Get4byte(&aPayload[pCur->info.nLocal]);
75797
75798 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
75799 **
75800 ** The aOverflow[] array is sized at one entry for each overflow page
75801 ** in the overflow chain. The page number of the first overflow page is
75802 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
75803 ** means "not yet known" (the cache is lazily populated).
75804 */
75805 if( (pCur->curFlags & BTCF_ValidOvfl0x04)==0 ){
75806 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
75807 if( pCur->aOverflow==0
75808 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
75809 ){
75810 Pgno *aNew;
75811 if( sqlite3FaultSim(413) ){
75812 aNew = 0;
75813 }else{
75814 aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
75815 }
75816 if( aNew==0 ){
75817 return SQLITE_NOMEM_BKPT7;
75818 }else{
75819 pCur->aOverflow = aNew;
75820 }
75821 }
75822 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
75823 pCur->curFlags |= BTCF_ValidOvfl0x04;
75824 }else{
75825 /* Sanity check the validity of the overflow page cache */
75826 assert( pCur->aOverflow[0]==nextPage((void) (0))
75827 || pCur->aOverflow[0]==0((void) (0))
75828 || CORRUPT_DB )((void) (0));
75829 assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 )((void) (0));
75830
75831 /* If the overflow page-list cache has been allocated and the
75832 ** entry for the first required overflow page is valid, skip
75833 ** directly to it.
75834 */
75835 if( pCur->aOverflow[offset/ovflSize] ){
75836 iIdx = (offset/ovflSize);
75837 nextPage = pCur->aOverflow[iIdx];
75838 offset = (offset%ovflSize);
75839 }
75840 }
75841
75842 assert( rc==SQLITE_OK && amt>0 )((void) (0));
75843 while( nextPage ){
75844 /* If required, populate the overflow page-list cache. */
75845 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75845);
75846 assert( pCur->aOverflow[iIdx]==0((void) (0))
75847 || pCur->aOverflow[iIdx]==nextPage((void) (0))
75848 || CORRUPT_DB )((void) (0));
75849 pCur->aOverflow[iIdx] = nextPage;
75850
75851 if( offset>=ovflSize ){
75852 /* The only reason to read this page is to obtain the page
75853 ** number for the next page in the overflow chain. The page
75854 ** data is not required. So first try to lookup the overflow
75855 ** page-list cache, if any, then fall back to the getOverflowPage()
75856 ** function.
75857 */
75858 assert( pCur->curFlags & BTCF_ValidOvfl )((void) (0));
75859 assert( pCur->pBtree->db==pBt->db )((void) (0));
75860 if( pCur->aOverflow[iIdx+1] ){
75861 nextPage = pCur->aOverflow[iIdx+1];
75862 }else{
75863 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
75864 }
75865 offset -= ovflSize;
75866 }else{
75867 /* Need to read this page properly. It contains some of the
75868 ** range of data that is being read (eOp==0) or written (eOp!=0).
75869 */
75870 int a = amt;
75871 if( a + offset > ovflSize ){
75872 a = ovflSize - offset;
75873 }
75874
75875#ifdef SQLITE_DIRECT_OVERFLOW_READ1
75876 /* If all the following are true:
75877 **
75878 ** 1) this is a read operation, and
75879 ** 2) data is required from the start of this overflow page, and
75880 ** 3) there are no dirty pages in the page-cache
75881 ** 4) the database is file-backed, and
75882 ** 5) the page is not in the WAL file
75883 ** 6) at least 4 bytes have already been read into the output buffer
75884 **
75885 ** then data can be read directly from the database file into the
75886 ** output buffer, bypassing the page-cache altogether. This speeds
75887 ** up loading large records that span many overflow pages.
75888 */
75889 if( eOp==0 /* (1) */
75890 && offset==0 /* (2) */
75891 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
75892 && &pBuf[-4]>=pBufStart /* (6) */
75893 ){
75894 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
75895 u8 aSave[4];
75896 u8 *aWrite = &pBuf[-4];
75897 assert( aWrite>=pBufStart )((void) (0)); /* due to (6) */
75898 memcpy(aSave, aWrite, 4);
75899 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
75900 nextPage = get4bytesqlite3Get4byte(aWrite);
75901 memcpy(aWrite, aSave, 4);
75902 }else
75903#endif
75904
75905 {
75906 DbPage *pDbPage;
75907 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
75908 (eOp==0 ? PAGER_GET_READONLY0x02 : 0)
75909 );
75910 if( rc==SQLITE_OK0 ){
75911 aPayload = sqlite3PagerGetData(pDbPage);
75912 nextPage = get4bytesqlite3Get4byte(aPayload);
75913 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
75914 sqlite3PagerUnref(pDbPage);
75915 offset = 0;
75916 }
75917 }
75918 amt -= a;
75919 if( amt==0 ) return rc;
75920 pBuf += a;
75921 }
75922 if( rc ) break;
75923 iIdx++;
75924 }
75925 }
75926
75927 if( rc==SQLITE_OK0 && amt>0 ){
75928 /* Overflow chain ends prematurely */
75929 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(75929);
75930 }
75931 return rc;
75932}
75933
75934/*
75935** Read part of the payload for the row at which that cursor pCur is currently
75936** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
75937** begins at "offset".
75938**
75939** pCur can be pointing to either a table or an index b-tree.
75940** If pointing to a table btree, then the content section is read. If
75941** pCur is pointing to an index b-tree then the key section is read.
75942**
75943** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
75944** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
75945** cursor might be invalid or might need to be restored before being read.
75946**
75947** Return SQLITE_OK on success or an error code if anything goes
75948** wrong. An error is returned if "offset+amt" is larger than
75949** the available payload.
75950*/
75951SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
75952 assert( cursorHoldsMutex(pCur) )((void) (0));
75953 assert( pCur->eState==CURSOR_VALID )((void) (0));
75954 assert( pCur->iPage>=0 && pCur->pPage )((void) (0));
75955 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
75956}
75957
75958/*
75959** This variant of sqlite3BtreePayload() works even if the cursor has not
75960** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
75961** interface.
75962*/
75963#ifndef SQLITE_OMIT_INCRBLOB
75964static SQLITE_NOINLINE__attribute__((noinline)) int accessPayloadChecked(
75965 BtCursor *pCur,
75966 u32 offset,
75967 u32 amt,
75968 void *pBuf
75969){
75970 int rc;
75971 if ( pCur->eState==CURSOR_INVALID1 ){
75972 return SQLITE_ABORT4;
75973 }
75974 assert( cursorOwnsBtShared(pCur) )((void) (0));
75975 rc = btreeRestoreCursorPosition(pCur);
75976 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
75977}
75978SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
75979 if( pCur->eState==CURSOR_VALID0 ){
75980 assert( cursorOwnsBtShared(pCur) )((void) (0));
75981 return accessPayload(pCur, offset, amt, pBuf, 0);
75982 }else{
75983 return accessPayloadChecked(pCur, offset, amt, pBuf);
75984 }
75985}
75986#endif /* SQLITE_OMIT_INCRBLOB */
75987
75988/*
75989** Return a pointer to payload information from the entry that the
75990** pCur cursor is pointing to. The pointer is to the beginning of
75991** the key if index btrees (pPage->intKey==0) and is the data for
75992** table btrees (pPage->intKey==1). The number of bytes of available
75993** key/data is written into *pAmt. If *pAmt==0, then the value
75994** returned will not be a valid pointer.
75995**
75996** This routine is an optimization. It is common for the entire key
75997** and data to fit on the local page and for there to be no overflow
75998** pages. When that is so, this routine can be used to access the
75999** key and data without making a copy. If the key and/or data spills
76000** onto overflow pages, then accessPayload() must be used to reassemble
76001** the key/data and copy it into a preallocated buffer.
76002**
76003** The pointer returned by this routine looks directly into the cached
76004** page of the database. The data might change or move the next time
76005** any btree routine is called.
76006*/
76007static const void *fetchPayload(
76008 BtCursor *pCur, /* Cursor pointing to entry to read from */
76009 u32 *pAmt /* Write the number of available bytes here */
76010){
76011 int amt;
76012 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage)((void) (0));
76013 assert( pCur->eState==CURSOR_VALID )((void) (0));
76014 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76015 assert( cursorOwnsBtShared(pCur) )((void) (0));
76016 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB )((void) (0));
76017 assert( pCur->info.nSize>0 )((void) (0));
76018 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB )((void) (0));
76019 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB)((void) (0));
76020 amt = pCur->info.nLocal;
76021 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
76022 /* There is too little space on the page for the expected amount
76023 ** of local content. Database must be corrupt. */
76024 assert( CORRUPT_DB )((void) (0));
76025 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
)))
;
76026 }
76027 *pAmt = (u32)amt;
76028 return (void*)pCur->info.pPayload;
76029}
76030
76031
76032/*
76033** For the entry that cursor pCur is point to, return as
76034** many bytes of the key or data as are available on the local
76035** b-tree page. Write the number of available bytes into *pAmt.
76036**
76037** The pointer returned is ephemeral. The key/data may move
76038** or be destroyed on the next call to any Btree routine,
76039** including calls from other threads against the same cache.
76040** Hence, a mutex on the BtShared should be held prior to calling
76041** this routine.
76042**
76043** These routines is used to get quick access to key and data
76044** in the common case where no overflow pages are used.
76045*/
76046SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
76047 return fetchPayload(pCur, pAmt);
76048}
76049
76050
76051/*
76052** Move the cursor down to a new child page. The newPgno argument is the
76053** page number of the child page to move to.
76054**
76055** This function returns SQLITE_CORRUPT if the page-header flags field of
76056** the new child page does not match the flags field of the parent (i.e.
76057** if an intkey page appears to be the parent of a non-intkey page, or
76058** vice-versa).
76059*/
76060static int moveToChild(BtCursor *pCur, u32 newPgno){
76061 int rc;
76062 assert( cursorOwnsBtShared(pCur) )((void) (0));
76063 assert( pCur->eState==CURSOR_VALID )((void) (0));
76064 assert( pCur->iPage<BTCURSOR_MAX_DEPTH )((void) (0));
76065 assert( pCur->iPage>=0 )((void) (0));
76066 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH20-1) ){
76067 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76067);
76068 }
76069 pCur->info.nSize = 0;
76070 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
76071 pCur->aiIdx[pCur->iPage] = pCur->ix;
76072 pCur->apPage[pCur->iPage] = pCur->pPage;
76073 pCur->ix = 0;
76074 pCur->iPage++;
76075 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
76076 assert( pCur->pPage!=0 || rc!=SQLITE_OK )((void) (0));
76077 if( rc==SQLITE_OK0
76078 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76079 ){
76080 releasePage(pCur->pPage);
76081 rc = SQLITE_CORRUPT_PGNO(newPgno)sqlite3CorruptError(76081);
76082 }
76083 if( rc ){
76084 pCur->pPage = pCur->apPage[--pCur->iPage];
76085 }
76086 return rc;
76087}
76088
76089#ifdef SQLITE_DEBUG
76090/*
76091** Page pParent is an internal (non-leaf) tree page. This function
76092** asserts that page number iChild is the left-child if the iIdx'th
76093** cell in page pParent. Or, if iIdx is equal to the total number of
76094** cells in pParent, that page number iChild is the right-child of
76095** the page.
76096*/
76097static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
76098 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return; /* The conditions tested below might not be true
76099 ** in a corrupt database */
76100 assert( iIdx<=pParent->nCell )((void) (0));
76101 if( iIdx==pParent->nCell ){
76102 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild )((void) (0));
76103 }else{
76104 assert( get4byte(findCell(pParent, iIdx))==iChild )((void) (0));
76105 }
76106}
76107#else
76108# define assertParentIndex(x,y,z)
76109#endif
76110
76111/*
76112** Move the cursor up to the parent page.
76113**
76114** pCur->idx is set to the cell index that contains the pointer
76115** to the page we are coming from. If we are coming from the
76116** right-most child page then pCur->idx is set to one more than
76117** the largest cell index.
76118*/
76119static void moveToParent(BtCursor *pCur){
76120 MemPage *pLeaf;
76121 assert( cursorOwnsBtShared(pCur) )((void) (0));
76122 assert( pCur->eState==CURSOR_VALID )((void) (0));
76123 assert( pCur->iPage>0 )((void) (0));
76124 assert( pCur->pPage )((void) (0));
76125 assertParentIndex(
76126 pCur->apPage[pCur->iPage-1],
76127 pCur->aiIdx[pCur->iPage-1],
76128 pCur->pPage->pgno
76129 );
76130 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
76131 pCur->info.nSize = 0;
76132 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
76133 pCur->ix = pCur->aiIdx[pCur->iPage-1];
76134 pLeaf = pCur->pPage;
76135 pCur->pPage = pCur->apPage[--pCur->iPage];
76136 releasePageNotNull(pLeaf);
76137}
76138
76139/*
76140** Move the cursor to point to the root page of its b-tree structure.
76141**
76142** If the table has a virtual root page, then the cursor is moved to point
76143** to the virtual root page instead of the actual root page. A table has a
76144** virtual root page when the actual root page contains no cells and a
76145** single child page. This can only happen with the table rooted at page 1.
76146**
76147** If the b-tree structure is empty, the cursor state is set to
76148** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
76149** the cursor is set to point to the first cell located on the root
76150** (or virtual root) page and the cursor state is set to CURSOR_VALID.
76151**
76152** If this function returns successfully, it may be assumed that the
76153** page-header flags indicate that the [virtual] root-page is the expected
76154** kind of b-tree page (i.e. if when opening the cursor the caller did not
76155** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
76156** indicating a table b-tree, or if the caller did specify a KeyInfo
76157** structure the flags byte is set to 0x02 or 0x0A, indicating an index
76158** b-tree).
76159*/
76160static int moveToRoot(BtCursor *pCur){
76161 MemPage *pRoot;
76162 int rc = SQLITE_OK0;
76163
76164 assert( cursorOwnsBtShared(pCur) )((void) (0));
76165 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK )((void) (0));
76166 assert( CURSOR_VALID < CURSOR_REQUIRESEEK )((void) (0));
76167 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK )((void) (0));
76168 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 )((void) (0));
76169 assert( pCur->pgnoRoot>0 || pCur->iPage<0 )((void) (0));
76170
76171 if( pCur->iPage>=0 ){
76172 if( pCur->iPage ){
76173 releasePageNotNull(pCur->pPage);
76174 while( --pCur->iPage ){
76175 releasePageNotNull(pCur->apPage[pCur->iPage]);
76176 }
76177 pRoot = pCur->pPage = pCur->apPage[0];
76178 goto skip_init;
76179 }
76180 }else if( pCur->pgnoRoot==0 ){
76181 pCur->eState = CURSOR_INVALID1;
76182 return SQLITE_EMPTY16;
76183 }else{
76184 assert( pCur->iPage==(-1) )((void) (0));
76185 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
76186 if( pCur->eState==CURSOR_FAULT4 ){
76187 assert( pCur->skipNext!=SQLITE_OK )((void) (0));
76188 return pCur->skipNext;
76189 }
76190 sqlite3BtreeClearCursor(pCur);
76191 }
76192 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
76193 pCur->curPagerFlags);
76194 if( rc!=SQLITE_OK0 ){
76195 pCur->eState = CURSOR_INVALID1;
76196 return rc;
76197 }
76198 pCur->iPage = 0;
76199 pCur->curIntKey = pCur->pPage->intKey;
76200 }
76201 pRoot = pCur->pPage;
76202 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB )((void) (0));
76203
76204 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
76205 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
76206 ** NULL, the caller expects a table b-tree. If this is not the case,
76207 ** return an SQLITE_CORRUPT error.
76208 **
76209 ** Earlier versions of SQLite assumed that this test could not fail
76210 ** if the root page was already loaded when this function was called (i.e.
76211 ** if pCur->iPage>=0). But this is not so if the database is corrupted
76212 ** in such a way that page pRoot is linked into a second b-tree table
76213 ** (or the freelist). */
76214 assert( pRoot->intKey==1 || pRoot->intKey==0 )((void) (0));
76215 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
76216 return SQLITE_CORRUPT_PAGE(pCur->pPage)sqlite3CorruptError(76216);
76217 }
76218
76219skip_init:
76220 pCur->ix = 0;
76221 pCur->info.nSize = 0;
76222 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
76223
76224 if( pRoot->nCell>0 ){
76225 pCur->eState = CURSOR_VALID0;
76226 }else if( !pRoot->leaf ){
76227 Pgno subpage;
76228 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76228);
76229 subpage = get4bytesqlite3Get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
76230 pCur->eState = CURSOR_VALID0;
76231 rc = moveToChild(pCur, subpage);
76232 }else{
76233 pCur->eState = CURSOR_INVALID1;
76234 rc = SQLITE_EMPTY16;
76235 }
76236 return rc;
76237}
76238
76239/*
76240** Move the cursor down to the left-most leaf entry beneath the
76241** entry to which it is currently pointing.
76242**
76243** The left-most leaf is the one with the smallest key - the first
76244** in ascending order.
76245*/
76246static int moveToLeftmost(BtCursor *pCur){
76247 Pgno pgno;
76248 int rc = SQLITE_OK0;
76249 MemPage *pPage;
76250
76251 assert( cursorOwnsBtShared(pCur) )((void) (0));
76252 assert( pCur->eState==CURSOR_VALID )((void) (0));
76253 while( rc==SQLITE_OK0 && !(pPage = pCur->pPage)->leaf ){
76254 assert( pCur->ix<pPage->nCell )((void) (0));
76255 pgno = get4bytesqlite3Get4byte(findCell(pPage, pCur->ix)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(pCur->ix)])[0]<<8 | (&(pPage)->
aCellIdx[2*(pCur->ix)])[1])))
);
76256 rc = moveToChild(pCur, pgno);
76257 }
76258 return rc;
76259}
76260
76261/*
76262** Move the cursor down to the right-most leaf entry beneath the
76263** page to which it is currently pointing. Notice the difference
76264** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
76265** finds the left-most entry beneath the *entry* whereas moveToRightmost()
76266** finds the right-most entry beneath the *page*.
76267**
76268** The right-most entry is the one with the largest key - the last
76269** key in ascending order.
76270*/
76271static int moveToRightmost(BtCursor *pCur){
76272 Pgno pgno;
76273 int rc = SQLITE_OK0;
76274 MemPage *pPage = 0;
76275
76276 assert( cursorOwnsBtShared(pCur) )((void) (0));
76277 assert( pCur->eState==CURSOR_VALID )((void) (0));
76278 while( !(pPage = pCur->pPage)->leaf ){
76279 pgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
76280 pCur->ix = pPage->nCell;
76281 rc = moveToChild(pCur, pgno);
76282 if( rc ) return rc;
76283 }
76284 pCur->ix = pPage->nCell-1;
76285 assert( pCur->info.nSize==0 )((void) (0));
76286 assert( (pCur->curFlags & BTCF_ValidNKey)==0 )((void) (0));
76287 return SQLITE_OK0;
76288}
76289
76290/* Move the cursor to the first entry in the table. Return SQLITE_OK
76291** on success. Set *pRes to 0 if the cursor actually points to something
76292** or set *pRes to 1 if the table is empty.
76293*/
76294SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
76295 int rc;
76296
76297 assert( cursorOwnsBtShared(pCur) )((void) (0));
76298 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76299 rc = moveToRoot(pCur);
76300 if( rc==SQLITE_OK0 ){
76301 assert( pCur->pPage->nCell>0 )((void) (0));
76302 *pRes = 0;
76303 rc = moveToLeftmost(pCur);
76304 }else if( rc==SQLITE_EMPTY16 ){
76305 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) )((void) (0));
76306 *pRes = 1;
76307 rc = SQLITE_OK0;
76308 }
76309 return rc;
76310}
76311
76312#ifdef SQLITE_DEBUG
76313/* The cursors is CURSOR_VALID and has BTCF_AtLast set. Verify that
76314** this flags are true for a consistent database.
76315**
76316** This routine is is called from within assert() statements only.
76317** It is an internal verification routine and does not appear in production
76318** builds.
76319*/
76320static int cursorIsAtLastEntry(BtCursor *pCur){
76321 int ii;
76322 for(ii=0; ii<pCur->iPage; ii++){
76323 if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
76324 }
76325 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
76326}
76327#endif
76328
76329/* Move the cursor to the last entry in the table. Return SQLITE_OK
76330** on success. Set *pRes to 0 if the cursor actually points to something
76331** or set *pRes to 1 if the table is empty.
76332*/
76333static SQLITE_NOINLINE__attribute__((noinline)) int btreeLast(BtCursor *pCur, int *pRes){
76334 int rc = moveToRoot(pCur);
76335 if( rc==SQLITE_OK0 ){
76336 assert( pCur->eState==CURSOR_VALID )((void) (0));
76337 *pRes = 0;
76338 rc = moveToRightmost(pCur);
76339 if( rc==SQLITE_OK0 ){
76340 pCur->curFlags |= BTCF_AtLast0x08;
76341 }else{
76342 pCur->curFlags &= ~BTCF_AtLast0x08;
76343 }
76344 }else if( rc==SQLITE_EMPTY16 ){
76345 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
76346 *pRes = 1;
76347 rc = SQLITE_OK0;
76348 }
76349 return rc;
76350}
76351SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
76352 assert( cursorOwnsBtShared(pCur) )((void) (0));
76353 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76354
76355 /* If the cursor already points to the last entry, this is a no-op. */
76356 if( CURSOR_VALID0==pCur->eState && (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
76357 assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB )((void) (0));
76358 *pRes = 0;
76359 return SQLITE_OK0;
76360 }
76361 return btreeLast(pCur, pRes);
76362}
76363
76364/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
76365** table near the key intKey. Return a success code.
76366**
76367** If an exact match is not found, then the cursor is always
76368** left pointing at a leaf page which would hold the entry if it
76369** were present. The cursor might point to an entry that comes
76370** before or after the key.
76371**
76372** An integer is written into *pRes which is the result of
76373** comparing the key with the entry to which the cursor is
76374** pointing. The meaning of the integer written into
76375** *pRes is as follows:
76376**
76377** *pRes<0 The cursor is left pointing at an entry that
76378** is smaller than intKey or if the table is empty
76379** and the cursor is therefore left point to nothing.
76380**
76381** *pRes==0 The cursor is left pointing at an entry that
76382** exactly matches intKey.
76383**
76384** *pRes>0 The cursor is left pointing at an entry that
76385** is larger than intKey.
76386*/
76387SQLITE_PRIVATEstatic int sqlite3BtreeTableMoveto(
76388 BtCursor *pCur, /* The cursor to be moved */
76389 i64 intKey, /* The table key */
76390 int biasRight, /* If true, bias the search to the high end */
76391 int *pRes /* Write search results here */
76392){
76393 int rc;
76394
76395 assert( cursorOwnsBtShared(pCur) )((void) (0));
76396 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76397 assert( pRes )((void) (0));
76398 assert( pCur->pKeyInfo==0 )((void) (0));
76399 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 )((void) (0));
76400
76401 /* If the cursor is already positioned at the point we are trying
76402 ** to move to, then just return without doing any work */
76403 if( pCur->eState==CURSOR_VALID0 && (pCur->curFlags & BTCF_ValidNKey0x02)!=0 ){
76404 if( pCur->info.nKey==intKey ){
76405 *pRes = 0;
76406 return SQLITE_OK0;
76407 }
76408 if( pCur->info.nKey<intKey ){
76409 if( (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
76410 assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB )((void) (0));
76411 *pRes = -1;
76412 return SQLITE_OK0;
76413 }
76414 /* If the requested key is one more than the previous key, then
76415 ** try to get there using sqlite3BtreeNext() rather than a full
76416 ** binary search. This is an optimization only. The correct answer
76417 ** is still obtained without this case, only a little more slowly. */
76418 if( pCur->info.nKey+1==intKey ){
76419 *pRes = 0;
76420 rc = sqlite3BtreeNext(pCur, 0);
76421 if( rc==SQLITE_OK0 ){
76422 getCellInfo(pCur);
76423 if( pCur->info.nKey==intKey ){
76424 return SQLITE_OK0;
76425 }
76426 }else if( rc!=SQLITE_DONE101 ){
76427 return rc;
76428 }
76429 }
76430 }
76431 }
76432
76433#ifdef SQLITE_DEBUG
76434 pCur->pBtree->nSeek++; /* Performance measurement during testing */
76435#endif
76436
76437 rc = moveToRoot(pCur);
76438 if( rc ){
76439 if( rc==SQLITE_EMPTY16 ){
76440 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
76441 *pRes = -1;
76442 return SQLITE_OK0;
76443 }
76444 return rc;
76445 }
76446 assert( pCur->pPage )((void) (0));
76447 assert( pCur->pPage->isInit )((void) (0));
76448 assert( pCur->eState==CURSOR_VALID )((void) (0));
76449 assert( pCur->pPage->nCell > 0 )((void) (0));
76450 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey )((void) (0));
76451 assert( pCur->curIntKey )((void) (0));
76452
76453 for(;;){
76454 int lwr, upr, idx, c;
76455 Pgno chldPg;
76456 MemPage *pPage = pCur->pPage;
76457 u8 *pCell; /* Pointer to current cell in pPage */
76458
76459 /* pPage->nCell must be greater than zero. If this is the root-page
76460 ** the cursor would have been INVALID above and this for(;;) loop
76461 ** not run. If this is not the root-page, then the moveToChild() routine
76462 ** would have already detected db corruption. Similarly, pPage must
76463 ** be the right kind (index or table) of b-tree page. Otherwise
76464 ** a moveToChild() or moveToRoot() call would have detected corruption. */
76465 assert( pPage->nCell>0 )((void) (0));
76466 assert( pPage->intKey )((void) (0));
76467 lwr = 0;
76468 upr = pPage->nCell-1;
76469 assert( biasRight==0 || biasRight==1 )((void) (0));
76470 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
76471 for(;;){
76472 i64 nCellKey;
76473 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
76474 if( pPage->intKeyLeaf ){
76475 while( 0x80 <= *(pCell++) ){
76476 if( pCell>=pPage->aDataEnd ){
76477 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(76477);
76478 }
76479 }
76480 }
76481 getVarintsqlite3GetVarint(pCell, (u64*)&nCellKey);
76482 if( nCellKey<intKey ){
76483 lwr = idx+1;
76484 if( lwr>upr ){ c = -1; break; }
76485 }else if( nCellKey>intKey ){
76486 upr = idx-1;
76487 if( lwr>upr ){ c = +1; break; }
76488 }else{
76489 assert( nCellKey==intKey )((void) (0));
76490 pCur->ix = (u16)idx;
76491 if( !pPage->leaf ){
76492 lwr = idx;
76493 goto moveto_table_next_layer;
76494 }else{
76495 pCur->curFlags |= BTCF_ValidNKey0x02;
76496 pCur->info.nKey = nCellKey;
76497 pCur->info.nSize = 0;
76498 *pRes = 0;
76499 return SQLITE_OK0;
76500 }
76501 }
76502 assert( lwr+upr>=0 )((void) (0));
76503 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
76504 }
76505 assert( lwr==upr+1 || !pPage->leaf )((void) (0));
76506 assert( pPage->isInit )((void) (0));
76507 if( pPage->leaf ){
76508 assert( pCur->ix<pCur->pPage->nCell )((void) (0));
76509 pCur->ix = (u16)idx;
76510 *pRes = c;
76511 rc = SQLITE_OK0;
76512 goto moveto_table_finish;
76513 }
76514moveto_table_next_layer:
76515 if( lwr>=pPage->nCell ){
76516 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
76517 }else{
76518 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
76519 }
76520 pCur->ix = (u16)lwr;
76521 rc = moveToChild(pCur, chldPg);
76522 if( rc ) break;
76523 }
76524moveto_table_finish:
76525 pCur->info.nSize = 0;
76526 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
76527 return rc;
76528}
76529
76530/*
76531** Compare the "idx"-th cell on the page the cursor pCur is currently
76532** pointing to to pIdxKey using xRecordCompare. Return negative or
76533** zero if the cell is less than or equal pIdxKey. Return positive
76534** if unknown.
76535**
76536** Return value negative: Cell at pCur[idx] less than pIdxKey
76537**
76538** Return value is zero: Cell at pCur[idx] equals pIdxKey
76539**
76540** Return value positive: Nothing is known about the relationship
76541** of the cell at pCur[idx] and pIdxKey.
76542**
76543** This routine is part of an optimization. It is always safe to return
76544** a positive value as that will cause the optimization to be skipped.
76545*/
76546static int indexCellCompare(
76547 BtCursor *pCur,
76548 int idx,
76549 UnpackedRecord *pIdxKey,
76550 RecordCompare xRecordCompare
76551){
76552 MemPage *pPage = pCur->pPage;
76553 int c;
76554 int nCell; /* Size of the pCell cell in bytes */
76555 u8 *pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
76556
76557 nCell = pCell[0];
76558 if( nCell<=pPage->max1bytePayload ){
76559 /* This branch runs if the record-size field of the cell is a
76560 ** single byte varint and the record fits entirely on the main
76561 ** b-tree page. */
76562 testcase( pCell+nCell+1==pPage->aDataEnd );
76563 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
76564 }else if( !(pCell[1] & 0x80)
76565 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76566 ){
76567 /* The record-size field is a 2 byte varint and the record
76568 ** fits entirely on the main b-tree page. */
76569 testcase( pCell+nCell+2==pPage->aDataEnd );
76570 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
76571 }else{
76572 /* If the record extends into overflow pages, do not attempt
76573 ** the optimization. */
76574 c = 99;
76575 }
76576 return c;
76577}
76578
76579/*
76580** Return true (non-zero) if pCur is current pointing to the last
76581** page of a table.
76582*/
76583static int cursorOnLastPage(BtCursor *pCur){
76584 int i;
76585 assert( pCur->eState==CURSOR_VALID )((void) (0));
76586 for(i=0; i<pCur->iPage; i++){
76587 MemPage *pPage = pCur->apPage[i];
76588 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
76589 }
76590 return 1;
76591}
76592
76593/* Move the cursor so that it points to an entry in an index table
76594** near the key pIdxKey. Return a success code.
76595**
76596** If an exact match is not found, then the cursor is always
76597** left pointing at a leaf page which would hold the entry if it
76598** were present. The cursor might point to an entry that comes
76599** before or after the key.
76600**
76601** An integer is written into *pRes which is the result of
76602** comparing the key with the entry to which the cursor is
76603** pointing. The meaning of the integer written into
76604** *pRes is as follows:
76605**
76606** *pRes<0 The cursor is left pointing at an entry that
76607** is smaller than pIdxKey or if the table is empty
76608** and the cursor is therefore left point to nothing.
76609**
76610** *pRes==0 The cursor is left pointing at an entry that
76611** exactly matches pIdxKey.
76612**
76613** *pRes>0 The cursor is left pointing at an entry that
76614** is larger than pIdxKey.
76615**
76616** The pIdxKey->eqSeen field is set to 1 if there
76617** exists an entry in the table that exactly matches pIdxKey.
76618*/
76619SQLITE_PRIVATEstatic int sqlite3BtreeIndexMoveto(
76620 BtCursor *pCur, /* The cursor to be moved */
76621 UnpackedRecord *pIdxKey, /* Unpacked index key */
76622 int *pRes /* Write search results here */
76623){
76624 int rc;
76625 RecordCompare xRecordCompare;
76626
76627 assert( cursorOwnsBtShared(pCur) )((void) (0));
76628 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76629 assert( pRes )((void) (0));
76630 assert( pCur->pKeyInfo!=0 )((void) (0));
76631
76632#ifdef SQLITE_DEBUG
76633 pCur->pBtree->nSeek++; /* Performance measurement during testing */
76634#endif
76635
76636 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
76637 pIdxKey->errCode = 0;
76638 assert( pIdxKey->default_rc==1((void) (0))
76639 || pIdxKey->default_rc==0((void) (0))
76640 || pIdxKey->default_rc==-1((void) (0))
76641 )((void) (0));
76642
76643
76644 /* Check to see if we can skip a lot of work. Two cases:
76645 **
76646 ** (1) If the cursor is already pointing to the very last cell
76647 ** in the table and the pIdxKey search key is greater than or
76648 ** equal to that last cell, then no movement is required.
76649 **
76650 ** (2) If the cursor is on the last page of the table and the first
76651 ** cell on that last page is less than or equal to the pIdxKey
76652 ** search key, then we can start the search on the current page
76653 ** without needing to go back to root.
76654 */
76655 if( pCur->eState==CURSOR_VALID0
76656 && pCur->pPage->leaf
76657 && cursorOnLastPage(pCur)
76658 ){
76659 int c;
76660 if( pCur->ix==pCur->pPage->nCell-1
76661 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
76662 && pIdxKey->errCode==SQLITE_OK0
76663 ){
76664 *pRes = c;
76665 return SQLITE_OK0; /* Cursor already pointing at the correct spot */
76666 }
76667 if( pCur->iPage>0
76668 && indexCellCompare(pCur, 0, pIdxKey, xRecordCompare)<=0
76669 && pIdxKey->errCode==SQLITE_OK0
76670 ){
76671 pCur->curFlags &= ~(BTCF_ValidOvfl0x04|BTCF_AtLast0x08);
76672 if( !pCur->pPage->isInit ){
76673 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76673);
76674 }
76675 goto bypass_moveto_root; /* Start search on the current page */
76676 }
76677 pIdxKey->errCode = SQLITE_OK0;
76678 }
76679
76680 rc = moveToRoot(pCur);
76681 if( rc ){
76682 if( rc==SQLITE_EMPTY16 ){
76683 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
76684 *pRes = -1;
76685 return SQLITE_OK0;
76686 }
76687 return rc;
76688 }
76689
76690bypass_moveto_root:
76691 assert( pCur->pPage )((void) (0));
76692 assert( pCur->pPage->isInit )((void) (0));
76693 assert( pCur->eState==CURSOR_VALID )((void) (0));
76694 assert( pCur->pPage->nCell > 0 )((void) (0));
76695 assert( pCur->curIntKey==0 )((void) (0));
76696 assert( pIdxKey!=0 )((void) (0));
76697 for(;;){
76698 int lwr, upr, idx, c;
76699 Pgno chldPg;
76700 MemPage *pPage = pCur->pPage;
76701 u8 *pCell; /* Pointer to current cell in pPage */
76702
76703 /* pPage->nCell must be greater than zero. If this is the root-page
76704 ** the cursor would have been INVALID above and this for(;;) loop
76705 ** not run. If this is not the root-page, then the moveToChild() routine
76706 ** would have already detected db corruption. Similarly, pPage must
76707 ** be the right kind (index or table) of b-tree page. Otherwise
76708 ** a moveToChild() or moveToRoot() call would have detected corruption. */
76709 assert( pPage->nCell>0 )((void) (0));
76710 assert( pPage->intKey==0 )((void) (0));
76711 lwr = 0;
76712 upr = pPage->nCell-1;
76713 idx = upr>>1; /* idx = (lwr+upr)/2; */
76714 for(;;){
76715 int nCell; /* Size of the pCell cell in bytes */
76716 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
76717
76718 /* The maximum supported page-size is 65536 bytes. This means that
76719 ** the maximum number of record bytes stored on an index B-Tree
76720 ** page is less than 16384 bytes and may be stored as a 2-byte
76721 ** varint. This information is used to attempt to avoid parsing
76722 ** the entire cell by checking for the cases where the record is
76723 ** stored entirely within the b-tree page by inspecting the first
76724 ** 2 bytes of the cell.
76725 */
76726 nCell = pCell[0];
76727 if( nCell<=pPage->max1bytePayload ){
76728 /* This branch runs if the record-size field of the cell is a
76729 ** single byte varint and the record fits entirely on the main
76730 ** b-tree page. */
76731 testcase( pCell+nCell+1==pPage->aDataEnd );
76732 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
76733 }else if( !(pCell[1] & 0x80)
76734 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76735 ){
76736 /* The record-size field is a 2 byte varint and the record
76737 ** fits entirely on the main b-tree page. */
76738 testcase( pCell+nCell+2==pPage->aDataEnd );
76739 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
76740 }else{
76741 /* The record flows over onto one or more overflow pages. In
76742 ** this case the whole cell needs to be parsed, a buffer allocated
76743 ** and accessPayload() used to retrieve the record into the
76744 ** buffer before VdbeRecordCompare() can be called.
76745 **
76746 ** If the record is corrupt, the xRecordCompare routine may read
76747 ** up to two varints past the end of the buffer. An extra 18
76748 ** bytes of padding is allocated at the end of the buffer in
76749 ** case this happens. */
76750 void *pCellKey;
76751 u8 * const pCellBody = pCell - pPage->childPtrSize;
76752 const int nOverrun = 18; /* Size of the overrun padding */
76753 pPage->xParseCell(pPage, pCellBody, &pCur->info);
76754 nCell = (int)pCur->info.nKey;
76755 testcase( nCell<0 ); /* True if key size is 2^32 or more */
76756 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
76757 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
76758 testcase( nCell==2 ); /* Minimum legal index key size */
76759 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
76760 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(76760);
76761 goto moveto_index_finish;
76762 }
76763 pCellKey = sqlite3Malloc( nCell+nOverrun );
76764 if( pCellKey==0 ){
76765 rc = SQLITE_NOMEM_BKPT7;
76766 goto moveto_index_finish;
76767 }
76768 pCur->ix = (u16)idx;
76769 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
76770 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
76771 pCur->curFlags &= ~BTCF_ValidOvfl0x04;
76772 if( rc ){
76773 sqlite3_free(pCellKey);
76774 goto moveto_index_finish;
76775 }
76776 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
76777 sqlite3_free(pCellKey);
76778 }
76779 assert(((void) (0))
76780 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)((void) (0))
76781 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)((void) (0))
76782 )((void) (0));
76783 if( c<0 ){
76784 lwr = idx+1;
76785 }else if( c>0 ){
76786 upr = idx-1;
76787 }else{
76788 assert( c==0 )((void) (0));
76789 *pRes = 0;
76790 rc = SQLITE_OK0;
76791 pCur->ix = (u16)idx;
76792 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(76792);
76793 goto moveto_index_finish;
76794 }
76795 if( lwr>upr ) break;
76796 assert( lwr+upr>=0 )((void) (0));
76797 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
76798 }
76799 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) )((void) (0));
76800 assert( pPage->isInit )((void) (0));
76801 if( pPage->leaf ){
76802 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB )((void) (0));
76803 pCur->ix = (u16)idx;
76804 *pRes = c;
76805 rc = SQLITE_OK0;
76806 goto moveto_index_finish;
76807 }
76808 if( lwr>=pPage->nCell ){
76809 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
76810 }else{
76811 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
76812 }
76813
76814 /* This block is similar to an in-lined version of:
76815 **
76816 ** pCur->ix = (u16)lwr;
76817 ** rc = moveToChild(pCur, chldPg);
76818 ** if( rc ) break;
76819 */
76820 pCur->info.nSize = 0;
76821 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
76822 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH20-1) ){
76823 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76823);
76824 }
76825 pCur->aiIdx[pCur->iPage] = (u16)lwr;
76826 pCur->apPage[pCur->iPage] = pCur->pPage;
76827 pCur->ix = 0;
76828 pCur->iPage++;
76829 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
76830 if( rc==SQLITE_OK0
76831 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76832 ){
76833 releasePage(pCur->pPage);
76834 rc = SQLITE_CORRUPT_PGNO(chldPg)sqlite3CorruptError(76834);
76835 }
76836 if( rc ){
76837 pCur->pPage = pCur->apPage[--pCur->iPage];
76838 break;
76839 }
76840 /*
76841 ***** End of in-lined moveToChild() call */
76842 }
76843moveto_index_finish:
76844 pCur->info.nSize = 0;
76845 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
76846 return rc;
76847}
76848
76849
76850/*
76851** Return TRUE if the cursor is not pointing at an entry of the table.
76852**
76853** TRUE will be returned after a call to sqlite3BtreeNext() moves
76854** past the last entry in the table or sqlite3BtreePrev() moves past
76855** the first entry. TRUE is also returned if the table is empty.
76856*/
76857SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor *pCur){
76858 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
76859 ** have been deleted? This API will need to change to return an error code
76860 ** as well as the boolean result value.
76861 */
76862 return (CURSOR_VALID0!=pCur->eState);
76863}
76864
76865/*
76866** Return an estimate for the number of rows in the table that pCur is
76867** pointing to. Return a negative number if no estimate is currently
76868** available.
76869*/
76870SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
76871 i64 n;
76872 u8 i;
76873
76874 assert( cursorOwnsBtShared(pCur) )((void) (0));
76875 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
76876
76877 /* Currently this interface is only called by the OP_IfSizeBetween
76878 ** opcode and the OP_Count opcode with P3=1. In either case,
76879 ** the cursor will always be valid unless the btree is empty. */
76880 if( pCur->eState!=CURSOR_VALID0 ) return 0;
76881 if( NEVER(pCur->pPage->leaf==0)(pCur->pPage->leaf==0) ) return -1;
76882
76883 n = pCur->pPage->nCell;
76884 for(i=0; i<pCur->iPage; i++){
76885 n *= pCur->apPage[i]->nCell;
76886 }
76887 return n;
76888}
76889
76890/*
76891** Advance the cursor to the next entry in the database.
76892** Return value:
76893**
76894** SQLITE_OK success
76895** SQLITE_DONE cursor is already pointing at the last element
76896** otherwise some kind of error occurred
76897**
76898** The main entry point is sqlite3BtreeNext(). That routine is optimized
76899** for the common case of merely incrementing the cell counter BtCursor.aiIdx
76900** to the next cell on the current page. The (slower) btreeNext() helper
76901** routine is called when it is necessary to move to a different page or
76902** to restore the cursor.
76903**
76904** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
76905** cursor corresponds to an SQL index and this routine could have been
76906** skipped if the SQL index had been a unique index. The F argument
76907** is a hint to the implement. SQLite btree implementation does not use
76908** this hint, but COMDB2 does.
76909*/
76910static SQLITE_NOINLINE__attribute__((noinline)) int btreeNext(BtCursor *pCur){
76911 int rc;
76912 int idx;
76913 MemPage *pPage;
76914
76915 assert( cursorOwnsBtShared(pCur) )((void) (0));
76916 if( pCur->eState!=CURSOR_VALID0 ){
76917 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
76918 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
76919 if( rc!=SQLITE_OK0 ){
76920 return rc;
76921 }
76922 if( CURSOR_INVALID1==pCur->eState ){
76923 return SQLITE_DONE101;
76924 }
76925 if( pCur->eState==CURSOR_SKIPNEXT2 ){
76926 pCur->eState = CURSOR_VALID0;
76927 if( pCur->skipNext>0 ) return SQLITE_OK0;
76928 }
76929 }
76930
76931 pPage = pCur->pPage;
76932 idx = ++pCur->ix;
76933 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76934 if( !pPage->isInit ){
76935 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76935);
76936 }
76937
76938 if( idx>=pPage->nCell ){
76939 if( !pPage->leaf ){
76940 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
76941 if( rc ) return rc;
76942 return moveToLeftmost(pCur);
76943 }
76944 do{
76945 if( pCur->iPage==0 ){
76946 pCur->eState = CURSOR_INVALID1;
76947 return SQLITE_DONE101;
76948 }
76949 moveToParent(pCur);
76950 pPage = pCur->pPage;
76951 }while( pCur->ix>=pPage->nCell );
76952 if( pPage->intKey ){
76953 return sqlite3BtreeNext(pCur, 0);
76954 }else{
76955 return SQLITE_OK0;
76956 }
76957 }
76958 if( pPage->leaf ){
76959 return SQLITE_OK0;
76960 }else{
76961 return moveToLeftmost(pCur);
76962 }
76963}
76964SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor *pCur, int flags){
76965 MemPage *pPage;
76966 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
76967 assert( cursorOwnsBtShared(pCur) )((void) (0));
76968 assert( flags==0 || flags==1 )((void) (0));
76969 pCur->info.nSize = 0;
76970 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
76971 if( pCur->eState!=CURSOR_VALID0 ) return btreeNext(pCur);
76972 pPage = pCur->pPage;
76973 if( (++pCur->ix)>=pPage->nCell ){
76974 pCur->ix--;
76975 return btreeNext(pCur);
76976 }
76977 if( pPage->leaf ){
76978 return SQLITE_OK0;
76979 }else{
76980 return moveToLeftmost(pCur);
76981 }
76982}
76983
76984/*
76985** Step the cursor to the back to the previous entry in the database.
76986** Return values:
76987**
76988** SQLITE_OK success
76989** SQLITE_DONE the cursor is already on the first element of the table
76990** otherwise some kind of error occurred
76991**
76992** The main entry point is sqlite3BtreePrevious(). That routine is optimized
76993** for the common case of merely decrementing the cell counter BtCursor.aiIdx
76994** to the previous cell on the current page. The (slower) btreePrevious()
76995** helper routine is called when it is necessary to move to a different page
76996** or to restore the cursor.
76997**
76998** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
76999** the cursor corresponds to an SQL index and this routine could have been
77000** skipped if the SQL index had been a unique index. The F argument is a
77001** hint to the implement. The native SQLite btree implementation does not
77002** use this hint, but COMDB2 does.
77003*/
77004static SQLITE_NOINLINE__attribute__((noinline)) int btreePrevious(BtCursor *pCur){
77005 int rc;
77006 MemPage *pPage;
77007
77008 assert( cursorOwnsBtShared(pCur) )((void) (0));
77009 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 )((void) (0));
77010 assert( pCur->info.nSize==0 )((void) (0));
77011 if( pCur->eState!=CURSOR_VALID0 ){
77012 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
77013 if( rc!=SQLITE_OK0 ){
77014 return rc;
77015 }
77016 if( CURSOR_INVALID1==pCur->eState ){
77017 return SQLITE_DONE101;
77018 }
77019 if( CURSOR_SKIPNEXT2==pCur->eState ){
77020 pCur->eState = CURSOR_VALID0;
77021 if( pCur->skipNext<0 ) return SQLITE_OK0;
77022 }
77023 }
77024
77025 pPage = pCur->pPage;
77026 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
77027 if( !pPage->isInit ){
77028 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77028);
77029 }
77030 if( !pPage->leaf ){
77031 int idx = pCur->ix;
77032 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
));
77033 if( rc ) return rc;
77034 rc = moveToRightmost(pCur);
77035 }else{
77036 while( pCur->ix==0 ){
77037 if( pCur->iPage==0 ){
77038 pCur->eState = CURSOR_INVALID1;
77039 return SQLITE_DONE101;
77040 }
77041 moveToParent(pCur);
77042 }
77043 assert( pCur->info.nSize==0 )((void) (0));
77044 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 )((void) (0));
77045
77046 pCur->ix--;
77047 pPage = pCur->pPage;
77048 if( pPage->intKey && !pPage->leaf ){
77049 rc = sqlite3BtreePrevious(pCur, 0);
77050 }else{
77051 rc = SQLITE_OK0;
77052 }
77053 }
77054 return rc;
77055}
77056SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor *pCur, int flags){
77057 assert( cursorOwnsBtShared(pCur) )((void) (0));
77058 assert( flags==0 || flags==1 )((void) (0));
77059 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
77060 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidOvfl0x04|BTCF_ValidNKey0x02);
77061 pCur->info.nSize = 0;
77062 if( pCur->eState!=CURSOR_VALID0
77063 || pCur->ix==0
77064 || pCur->pPage->leaf==0
77065 ){
77066 return btreePrevious(pCur);
77067 }
77068 pCur->ix--;
77069 return SQLITE_OK0;
77070}
77071
77072/*
77073** Allocate a new page from the database file.
77074**
77075** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
77076** has already been called on the new page.) The new page has also
77077** been referenced and the calling routine is responsible for calling
77078** sqlite3PagerUnref() on the new page when it is done.
77079**
77080** SQLITE_OK is returned on success. Any other return value indicates
77081** an error. *ppPage is set to NULL in the event of an error.
77082**
77083** If the "nearby" parameter is not 0, then an effort is made to
77084** locate a page close to the page number "nearby". This can be used in an
77085** attempt to keep related pages close to each other in the database file,
77086** which in turn can make database access faster.
77087**
77088** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
77089** anywhere on the free-list, then it is guaranteed to be returned. If
77090** eMode is BTALLOC_LT then the page returned will be less than or equal
77091** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
77092** are no restrictions on which page is returned.
77093*/
77094static int allocateBtreePage(
77095 BtShared *pBt, /* The btree */
77096 MemPage **ppPage, /* Store pointer to the allocated page here */
77097 Pgno *pPgno, /* Store the page number here */
77098 Pgno nearby, /* Search for a page near this one */
77099 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
77100){
77101 MemPage *pPage1;
77102 int rc;
77103 u32 n; /* Number of pages on the freelist */
77104 u32 k; /* Number of leaves on the trunk of the freelist */
77105 MemPage *pTrunk = 0;
77106 MemPage *pPrevTrunk = 0;
77107 Pgno mxPage; /* Total size of the database file */
77108
77109 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
77110 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) )((void) (0));
77111 pPage1 = pBt->pPage1;
77112 mxPage = btreePagecount(pBt);
77113 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
77114 ** stores the total number of pages on the freelist. */
77115 n = get4bytesqlite3Get4byte(&pPage1->aData[36]);
77116 testcase( n==mxPage-1 );
77117 if( n>=mxPage ){
77118 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77118);
77119 }
77120 if( n>0 ){
77121 /* There are pages on the freelist. Reuse one of those pages. */
77122 Pgno iTrunk;
77123 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
77124 u32 nSearch = 0; /* Count of the number of search attempts */
77125
77126 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
77127 ** shows that the page 'nearby' is somewhere on the free-list, then
77128 ** the entire-list will be searched for that page.
77129 */
77130#ifndef SQLITE_OMIT_AUTOVACUUM
77131 if( eMode==BTALLOC_EXACT1 ){
77132 if( nearby<=mxPage ){
77133 u8 eType;
77134 assert( nearby>0 )((void) (0));
77135 assert( pBt->autoVacuum )((void) (0));
77136 rc = ptrmapGet(pBt, nearby, &eType, 0);
77137 if( rc ) return rc;
77138 if( eType==PTRMAP_FREEPAGE2 ){
77139 searchList = 1;
77140 }
77141 }
77142 }else if( eMode==BTALLOC_LE2 ){
77143 searchList = 1;
77144 }
77145#endif
77146
77147 /* Decrement the free-list count by 1. Set iTrunk to the index of the
77148 ** first free-list trunk page. iPrevTrunk is initially 1.
77149 */
77150 rc = sqlite3PagerWrite(pPage1->pDbPage);
77151 if( rc ) return rc;
77152 put4bytesqlite3Put4byte(&pPage1->aData[36], n-1);
77153
77154 /* The code within this loop is run only once if the 'searchList' variable
77155 ** is not true. Otherwise, it runs once for each trunk-page on the
77156 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
77157 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
77158 */
77159 do {
77160 pPrevTrunk = pTrunk;
77161 if( pPrevTrunk ){
77162 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
77163 ** is the page number of the next freelist trunk page in the list or
77164 ** zero if this is the last freelist trunk page. */
77165 iTrunk = get4bytesqlite3Get4byte(&pPrevTrunk->aData[0]);
77166 }else{
77167 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
77168 ** stores the page number of the first page of the freelist, or zero if
77169 ** the freelist is empty. */
77170 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
77171 }
77172 testcase( iTrunk==mxPage );
77173 if( iTrunk>mxPage || nSearch++ > n ){
77174 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1)sqlite3CorruptError(77174);
77175 }else{
77176 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
77177 }
77178 if( rc ){
77179 pTrunk = 0;
77180 goto end_allocate_page;
77181 }
77182 assert( pTrunk!=0 )((void) (0));
77183 assert( pTrunk->aData!=0 )((void) (0));
77184 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
77185 ** is the number of leaf page pointers to follow. */
77186 k = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
77187 if( k==0 && !searchList ){
77188 /* The trunk has no leaves and the list is not being searched.
77189 ** So extract the trunk page itself and use it as the newly
77190 ** allocated page */
77191 assert( pPrevTrunk==0 )((void) (0));
77192 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77193 if( rc ){
77194 goto end_allocate_page;
77195 }
77196 *pPgno = iTrunk;
77197 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
77198 *ppPage = pTrunk;
77199 pTrunk = 0;
77200 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
77201 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
77202 /* Value of k is out of range. Database corruption */
77203 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(77203);
77204 goto end_allocate_page;
77205#ifndef SQLITE_OMIT_AUTOVACUUM
77206 }else if( searchList
77207 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE2))
77208 ){
77209 /* The list is being searched and this trunk page is the page
77210 ** to allocate, regardless of whether it has leaves.
77211 */
77212 *pPgno = iTrunk;
77213 *ppPage = pTrunk;
77214 searchList = 0;
77215 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77216 if( rc ){
77217 goto end_allocate_page;
77218 }
77219 if( k==0 ){
77220 if( !pPrevTrunk ){
77221 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
77222 }else{
77223 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
77224 if( rc!=SQLITE_OK0 ){
77225 goto end_allocate_page;
77226 }
77227 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
77228 }
77229 }else{
77230 /* The trunk page is required by the caller but it contains
77231 ** pointers to free-list leaves. The first leaf becomes a trunk
77232 ** page in this case.
77233 */
77234 MemPage *pNewTrunk;
77235 Pgno iNewTrunk = get4bytesqlite3Get4byte(&pTrunk->aData[8]);
77236 if( iNewTrunk>mxPage ){
77237 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(77237);
77238 goto end_allocate_page;
77239 }
77240 testcase( iNewTrunk==mxPage );
77241 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
77242 if( rc!=SQLITE_OK0 ){
77243 goto end_allocate_page;
77244 }
77245 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
77246 if( rc!=SQLITE_OK0 ){
77247 releasePage(pNewTrunk);
77248 goto end_allocate_page;
77249 }
77250 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
77251 put4bytesqlite3Put4byte(&pNewTrunk->aData[4], k-1);
77252 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
77253 releasePage(pNewTrunk);
77254 if( !pPrevTrunk ){
77255 assert( sqlite3PagerIswriteable(pPage1->pDbPage) )((void) (0));
77256 put4bytesqlite3Put4byte(&pPage1->aData[32], iNewTrunk);
77257 }else{
77258 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
77259 if( rc ){
77260 goto end_allocate_page;
77261 }
77262 put4bytesqlite3Put4byte(&pPrevTrunk->aData[0], iNewTrunk);
77263 }
77264 }
77265 pTrunk = 0;
77266 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
77267#endif
77268 }else if( k>0 ){
77269 /* Extract a leaf from the trunk */
77270 u32 closest;
77271 Pgno iPage;
77272 unsigned char *aData = pTrunk->aData;
77273 if( nearby>0 ){
77274 u32 i;
77275 closest = 0;
77276 if( eMode==BTALLOC_LE2 ){
77277 for(i=0; i<k; i++){
77278 iPage = get4bytesqlite3Get4byte(&aData[8+i*4]);
77279 if( iPage<=nearby ){
77280 closest = i;
77281 break;
77282 }
77283 }
77284 }else{
77285 int dist;
77286 dist = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8]) - nearby);
77287 for(i=1; i<k; i++){
77288 int d2 = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8+i*4]) - nearby);
77289 if( d2<dist ){
77290 closest = i;
77291 dist = d2;
77292 }
77293 }
77294 }
77295 }else{
77296 closest = 0;
77297 }
77298
77299 iPage = get4bytesqlite3Get4byte(&aData[8+closest*4]);
77300 testcase( iPage==mxPage );
77301 if( iPage>mxPage || iPage<2 ){
77302 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(77302);
77303 goto end_allocate_page;
77304 }
77305 testcase( iPage==mxPage );
77306 if( !searchList
77307 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE2))
77308 ){
77309 int noContent;
77310 *pPgno = iPage;
77311 TRACE(("ALLOCATE: %u was leaf %u of %u on trunk %u"
77312 ": %u more free pages\n",
77313 *pPgno, closest+1, k, pTrunk->pgno, n-1));
77314 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77315 if( rc ) goto end_allocate_page;
77316 if( closest<k-1 ){
77317 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
77318 }
77319 put4bytesqlite3Put4byte(&aData[4], k-1);
77320 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT0x01 : 0;
77321 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
77322 if( rc==SQLITE_OK0 ){
77323 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
77324 if( rc!=SQLITE_OK0 ){
77325 releasePage(*ppPage);
77326 *ppPage = 0;
77327 }
77328 }
77329 searchList = 0;
77330 }
77331 }
77332 releasePage(pPrevTrunk);
77333 pPrevTrunk = 0;
77334 }while( searchList );
77335 }else{
77336 /* There are no pages on the freelist, so append a new page to the
77337 ** database image.
77338 **
77339 ** Normally, new pages allocated by this block can be requested from the
77340 ** pager layer with the 'no-content' flag set. This prevents the pager
77341 ** from trying to read the pages content from disk. However, if the
77342 ** current transaction has already run one or more incremental-vacuum
77343 ** steps, then the page we are about to allocate may contain content
77344 ** that is required in the event of a rollback. In this case, do
77345 ** not set the no-content flag. This causes the pager to load and journal
77346 ** the current page content before overwriting it.
77347 **
77348 ** Note that the pager will not actually attempt to load or journal
77349 ** content for any page that really does lie past the end of the database
77350 ** file on disk. So the effects of disabling the no-content optimization
77351 ** here are confined to those pages that lie between the end of the
77352 ** database image and the end of the database file.
77353 */
77354 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)(pBt->bDoTruncate))? PAGER_GET_NOCONTENT0x01:0;
77355
77356 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
77357 if( rc ) return rc;
77358 pBt->nPage++;
77359 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ) pBt->nPage++;
77360
77361#ifndef SQLITE_OMIT_AUTOVACUUM
77362 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage)(ptrmapPageno((pBt), (pBt->nPage))==(pBt->nPage)) ){
77363 /* If *pPgno refers to a pointer-map page, allocate two new pages
77364 ** at the end of the file instead of one. The first allocated page
77365 ** becomes a new pointer-map page, the second is used by the caller.
77366 */
77367 MemPage *pPg = 0;
77368 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
77369 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) )((void) (0));
77370 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
77371 if( rc==SQLITE_OK0 ){
77372 rc = sqlite3PagerWrite(pPg->pDbPage);
77373 releasePage(pPg);
77374 }
77375 if( rc ) return rc;
77376 pBt->nPage++;
77377 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){ pBt->nPage++; }
77378 }
77379#endif
77380 put4bytesqlite3Put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
77381 *pPgno = pBt->nPage;
77382
77383 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
77384 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
77385 if( rc ) return rc;
77386 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
77387 if( rc!=SQLITE_OK0 ){
77388 releasePage(*ppPage);
77389 *ppPage = 0;
77390 }
77391 TRACE(("ALLOCATE: %u from end of file\n", *pPgno));
77392 }
77393
77394 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
77395
77396end_allocate_page:
77397 releasePage(pTrunk);
77398 releasePage(pPrevTrunk);
77399 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 )((void) (0));
77400 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 )((void) (0));
77401 return rc;
77402}
77403
77404/*
77405** This function is used to add page iPage to the database file free-list.
77406** It is assumed that the page is not already a part of the free-list.
77407**
77408** The value passed as the second argument to this function is optional.
77409** If the caller happens to have a pointer to the MemPage object
77410** corresponding to page iPage handy, it may pass it as the second value.
77411** Otherwise, it may pass NULL.
77412**
77413** If a pointer to a MemPage object is passed as the second argument,
77414** its reference count is not altered by this function.
77415*/
77416static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
77417 MemPage *pTrunk = 0; /* Free-list trunk page */
77418 Pgno iTrunk = 0; /* Page number of free-list trunk page */
77419 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
77420 MemPage *pPage; /* Page being freed. May be NULL. */
77421 int rc; /* Return Code */
77422 u32 nFree; /* Initial number of pages on free-list */
77423
77424 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
77425 assert( CORRUPT_DB || iPage>1 )((void) (0));
77426 assert( !pMemPage || pMemPage->pgno==iPage )((void) (0));
77427
77428 if( iPage<2 || iPage>pBt->nPage ){
77429 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77429);
77430 }
77431 if( pMemPage ){
77432 pPage = pMemPage;
77433 sqlite3PagerRef(pPage->pDbPage);
77434 }else{
77435 pPage = btreePageLookup(pBt, iPage);
77436 }
77437
77438 /* Increment the free page count on pPage1 */
77439 rc = sqlite3PagerWrite(pPage1->pDbPage);
77440 if( rc ) goto freepage_out;
77441 nFree = get4bytesqlite3Get4byte(&pPage1->aData[36]);
77442 put4bytesqlite3Put4byte(&pPage1->aData[36], nFree+1);
77443
77444 if( pBt->btsFlags & BTS_SECURE_DELETE0x0004 ){
77445 /* If the secure_delete option is enabled, then
77446 ** always fully overwrite deleted information with zeros.
77447 */
77448 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
77449 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
77450 ){
77451 goto freepage_out;
77452 }
77453 memset(pPage->aData, 0, pPage->pBt->pageSize);
77454 }
77455
77456 /* If the database supports auto-vacuum, write an entry in the pointer-map
77457 ** to indicate that the page is free.
77458 */
77459 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
77460 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE2, 0, &rc);
77461 if( rc ) goto freepage_out;
77462 }
77463
77464 /* Now manipulate the actual database free-list structure. There are two
77465 ** possibilities. If the free-list is currently empty, or if the first
77466 ** trunk page in the free-list is full, then this page will become a
77467 ** new free-list trunk page. Otherwise, it will become a leaf of the
77468 ** first trunk page in the current free-list. This block tests if it
77469 ** is possible to add the page as a new free-list leaf.
77470 */
77471 if( nFree!=0 ){
77472 u32 nLeaf; /* Initial number of leaf cells on trunk page */
77473
77474 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
77475 if( iTrunk>btreePagecount(pBt) ){
77476 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(77476);
77477 goto freepage_out;
77478 }
77479 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
77480 if( rc!=SQLITE_OK0 ){
77481 goto freepage_out;
77482 }
77483
77484 nLeaf = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
77485 assert( pBt->usableSize>32 )((void) (0));
77486 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
77487 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(77487);
77488 goto freepage_out;
77489 }
77490 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
77491 /* In this case there is room on the trunk page to insert the page
77492 ** being freed as a new leaf.
77493 **
77494 ** Note that the trunk page is not really full until it contains
77495 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
77496 ** coded. But due to a coding error in versions of SQLite prior to
77497 ** 3.6.0, databases with freelist trunk pages holding more than
77498 ** usableSize/4 - 8 entries will be reported as corrupt. In order
77499 ** to maintain backwards compatibility with older versions of SQLite,
77500 ** we will continue to restrict the number of entries to usableSize/4 - 8
77501 ** for now. At some point in the future (once everyone has upgraded
77502 ** to 3.6.0 or later) we should consider fixing the conditional above
77503 ** to read "usableSize/4-2" instead of "usableSize/4-8".
77504 **
77505 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
77506 ** avoid using the last six entries in the freelist trunk page array in
77507 ** order that database files created by newer versions of SQLite can be
77508 ** read by older versions of SQLite.
77509 */
77510 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77511 if( rc==SQLITE_OK0 ){
77512 put4bytesqlite3Put4byte(&pTrunk->aData[4], nLeaf+1);
77513 put4bytesqlite3Put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
77514 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE0x0004)==0 ){
77515 sqlite3PagerDontWrite(pPage->pDbPage);
77516 }
77517 rc = btreeSetHasContent(pBt, iPage);
77518 }
77519 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
77520 goto freepage_out;
77521 }
77522 }
77523
77524 /* If control flows to this point, then it was not possible to add the
77525 ** the page being freed as a leaf page of the first trunk in the free-list.
77526 ** Possibly because the free-list is empty, or possibly because the
77527 ** first trunk in the free-list is full. Either way, the page being freed
77528 ** will become the new first trunk page in the free-list.
77529 */
77530 if( pPage==0 && SQLITE_OK0!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
77531 goto freepage_out;
77532 }
77533 rc = sqlite3PagerWrite(pPage->pDbPage);
77534 if( rc!=SQLITE_OK0 ){
77535 goto freepage_out;
77536 }
77537 put4bytesqlite3Put4byte(pPage->aData, iTrunk);
77538 put4bytesqlite3Put4byte(&pPage->aData[4], 0);
77539 put4bytesqlite3Put4byte(&pPage1->aData[32], iPage);
77540 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
77541
77542freepage_out:
77543 if( pPage ){
77544 pPage->isInit = 0;
77545 }
77546 releasePage(pPage);
77547 releasePage(pTrunk);
77548 return rc;
77549}
77550static void freePage(MemPage *pPage, int *pRC){
77551 if( (*pRC)==SQLITE_OK0 ){
77552 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
77553 }
77554}
77555
77556/*
77557** Free the overflow pages associated with the given Cell.
77558*/
77559static SQLITE_NOINLINE__attribute__((noinline)) int clearCellOverflow(
77560 MemPage *pPage, /* The page that contains the Cell */
77561 unsigned char *pCell, /* First byte of the Cell */
77562 CellInfo *pInfo /* Size information about the cell */
77563){
77564 BtShared *pBt;
77565 Pgno ovflPgno;
77566 int rc;
77567 int nOvfl;
77568 u32 ovflPageSize;
77569
77570 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
77571 assert( pInfo->nLocal!=pInfo->nPayload )((void) (0));
77572 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
77573 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
77574 if( pCell + pInfo->nSize > pPage->aDataEnd ){
77575 /* Cell extends past end of page */
77576 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(77576);
77577 }
77578 ovflPgno = get4bytesqlite3Get4byte(pCell + pInfo->nSize - 4);
77579 pBt = pPage->pBt;
77580 assert( pBt->usableSize > 4 )((void) (0));
77581 ovflPageSize = pBt->usableSize - 4;
77582 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
77583 assert( nOvfl>0 ||((void) (0))
77584 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)((void) (0))
77585 )((void) (0));
77586 while( nOvfl-- ){
77587 Pgno iNext = 0;
77588 MemPage *pOvfl = 0;
77589 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
77590 /* 0 is not a legal page number and page 1 cannot be an
77591 ** overflow page. Therefore if ovflPgno<2 or past the end of the
77592 ** file the database must be corrupt. */
77593 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(77593);
77594 }
77595 if( nOvfl ){
77596 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
77597 if( rc ) return rc;
77598 }
77599
77600 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
77601 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
77602 ){
77603 /* There is no reason any cursor should have an outstanding reference
77604 ** to an overflow page belonging to a cell that is being deleted/updated.
77605 ** So if there exists more than one reference to this page, then it
77606 ** must not really be an overflow page and the database must be corrupt.
77607 ** It is helpful to detect this before calling freePage2(), as
77608 ** freePage2() may zero the page contents if secure-delete mode is
77609 ** enabled. If this 'overflow' page happens to be a page that the
77610 ** caller is iterating through or using in some other way, this
77611 ** can be problematic.
77612 */
77613 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(77613);
77614 }else{
77615 rc = freePage2(pBt, pOvfl, ovflPgno);
77616 }
77617
77618 if( pOvfl ){
77619 sqlite3PagerUnref(pOvfl->pDbPage);
77620 }
77621 if( rc ) return rc;
77622 ovflPgno = iNext;
77623 }
77624 return SQLITE_OK0;
77625}
77626
77627/* Call xParseCell to compute the size of a cell. If the cell contains
77628** overflow, then invoke cellClearOverflow to clear out that overflow.
77629** Store the result code (SQLITE_OK or some error code) in rc.
77630**
77631** Implemented as macro to force inlining for performance.
77632*/
77633#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; }
\
77634 pPage->xParseCell(pPage, pCell, &sInfo); \
77635 if( sInfo.nLocal!=sInfo.nPayload ){ \
77636 rc = clearCellOverflow(pPage, pCell, &sInfo); \
77637 }else{ \
77638 rc = SQLITE_OK0; \
77639 }
77640
77641
77642/*
77643** Create the byte sequence used to represent a cell on page pPage
77644** and write that byte sequence into pCell[]. Overflow pages are
77645** allocated and filled in as necessary. The calling procedure
77646** is responsible for making sure sufficient space has been allocated
77647** for pCell[].
77648**
77649** Note that pCell does not necessary need to point to the pPage->aData
77650** area. pCell might point to some temporary storage. The cell will
77651** be constructed in this temporary area then copied into pPage->aData
77652** later.
77653*/
77654static int fillInCell(
77655 MemPage *pPage, /* The page that contains the cell */
77656 unsigned char *pCell, /* Complete text of the cell */
77657 const BtreePayload *pX, /* Payload with which to construct the cell */
77658 int *pnSize /* Write cell size here */
77659){
77660 int nPayload;
77661 const u8 *pSrc;
77662 int nSrc, n, rc, mn;
77663 int spaceLeft;
77664 MemPage *pToRelease;
77665 unsigned char *pPrior;
77666 unsigned char *pPayload;
77667 BtShared *pBt;
77668 Pgno pgnoOvfl;
77669 int nHeader;
77670
77671 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
77672
77673 /* pPage is not necessarily writeable since pCell might be auxiliary
77674 ** buffer space that is separate from the pPage buffer area */
77675 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]((void) (0))
77676 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
77677
77678 /* Fill in the header. */
77679 nHeader = pPage->childPtrSize;
77680 if( pPage->intKey ){
77681 nPayload = pX->nData + pX->nZero;
77682 pSrc = pX->pData;
77683 nSrc = pX->nData;
77684 assert( pPage->intKeyLeaf )((void) (0)); /* fillInCell() only called for leaves */
77685 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
77686 nHeader += putVarintsqlite3PutVarint(&pCell[nHeader], *(u64*)&pX->nKey);
77687 }else{
77688 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 )((void) (0));
77689 nSrc = nPayload = (int)pX->nKey;
77690 pSrc = pX->pKey;
77691 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
77692 }
77693
77694 /* Fill in the payload */
77695 pPayload = &pCell[nHeader];
77696 if( nPayload<=pPage->maxLocal ){
77697 /* This is the common case where everything fits on the btree page
77698 ** and no overflow pages are required. */
77699 n = nHeader + nPayload;
77700 testcase( n==3 );
77701 testcase( n==4 );
77702 if( n<4 ){
77703 n = 4;
77704 pPayload[nPayload] = 0;
77705 }
77706 *pnSize = n;
77707 assert( nSrc<=nPayload )((void) (0));
77708 testcase( nSrc<nPayload );
77709 memcpy(pPayload, pSrc, nSrc);
77710 memset(pPayload+nSrc, 0, nPayload-nSrc);
77711 return SQLITE_OK0;
77712 }
77713
77714 /* If we reach this point, it means that some of the content will need
77715 ** to spill onto overflow pages.
77716 */
77717 mn = pPage->minLocal;
77718 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
77719 testcase( n==pPage->maxLocal );
77720 testcase( n==pPage->maxLocal+1 );
77721 if( n > pPage->maxLocal ) n = mn;
77722 spaceLeft = n;
77723 *pnSize = n + nHeader + 4;
77724 pPrior = &pCell[nHeader+n];
77725 pToRelease = 0;
77726 pgnoOvfl = 0;
77727 pBt = pPage->pBt;
77728
77729 /* At this point variables should be set as follows:
77730 **
77731 ** nPayload Total payload size in bytes
77732 ** pPayload Begin writing payload here
77733 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
77734 ** that means content must spill into overflow pages.
77735 ** *pnSize Size of the local cell (not counting overflow pages)
77736 ** pPrior Where to write the pgno of the first overflow page
77737 **
77738 ** Use a call to btreeParseCellPtr() to verify that the values above
77739 ** were computed correctly.
77740 */
77741#ifdef SQLITE_DEBUG
77742 {
77743 CellInfo info;
77744 pPage->xParseCell(pPage, pCell, &info);
77745 assert( nHeader==(int)(info.pPayload - pCell) )((void) (0));
77746 assert( info.nKey==pX->nKey )((void) (0));
77747 assert( *pnSize == info.nSize )((void) (0));
77748 assert( spaceLeft == info.nLocal )((void) (0));
77749 }
77750#endif
77751
77752 /* Write the payload into the local Cell and any extra into overflow pages */
77753 while( 1 ){
77754 n = nPayload;
77755 if( n>spaceLeft ) n = spaceLeft;
77756
77757 /* If pToRelease is not zero than pPayload points into the data area
77758 ** of pToRelease. Make sure pToRelease is still writeable. */
77759 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
77760
77761 /* If pPayload is part of the data area of pPage, then make sure pPage
77762 ** is still writeable */
77763 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]((void) (0))
77764 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
77765
77766 if( nSrc>=n ){
77767 memcpy(pPayload, pSrc, n);
77768 }else if( nSrc>0 ){
77769 n = nSrc;
77770 memcpy(pPayload, pSrc, n);
77771 }else{
77772 memset(pPayload, 0, n);
77773 }
77774 nPayload -= n;
77775 if( nPayload<=0 ) break;
77776 pPayload += n;
77777 pSrc += n;
77778 nSrc -= n;
77779 spaceLeft -= n;
77780 if( spaceLeft==0 ){
77781 MemPage *pOvfl = 0;
77782#ifndef SQLITE_OMIT_AUTOVACUUM
77783 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
77784 if( pBt->autoVacuum ){
77785 do{
77786 pgnoOvfl++;
77787 } while(
77788 PTRMAP_ISPAGE(pBt, pgnoOvfl)(ptrmapPageno((pBt), (pgnoOvfl))==(pgnoOvfl)) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
77789 );
77790 }
77791#endif
77792 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
77793#ifndef SQLITE_OMIT_AUTOVACUUM
77794 /* If the database supports auto-vacuum, and the second or subsequent
77795 ** overflow page is being allocated, add an entry to the pointer-map
77796 ** for that page now.
77797 **
77798 ** If this is the first overflow page, then write a partial entry
77799 ** to the pointer-map. If we write nothing to this pointer-map slot,
77800 ** then the optimistic overflow chain processing in clearCell()
77801 ** may misinterpret the uninitialized values and delete the
77802 ** wrong pages from the database.
77803 */
77804 if( pBt->autoVacuum && rc==SQLITE_OK0 ){
77805 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW24:PTRMAP_OVERFLOW13);
77806 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
77807 if( rc ){
77808 releasePage(pOvfl);
77809 }
77810 }
77811#endif
77812 if( rc ){
77813 releasePage(pToRelease);
77814 return rc;
77815 }
77816
77817 /* If pToRelease is not zero than pPrior points into the data area
77818 ** of pToRelease. Make sure pToRelease is still writeable. */
77819 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
77820
77821 /* If pPrior is part of the data area of pPage, then make sure pPage
77822 ** is still writeable */
77823 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]((void) (0))
77824 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
77825
77826 put4bytesqlite3Put4byte(pPrior, pgnoOvfl);
77827 releasePage(pToRelease);
77828 pToRelease = pOvfl;
77829 pPrior = pOvfl->aData;
77830 put4bytesqlite3Put4byte(pPrior, 0);
77831 pPayload = &pOvfl->aData[4];
77832 spaceLeft = pBt->usableSize - 4;
77833 }
77834 }
77835 releasePage(pToRelease);
77836 return SQLITE_OK0;
77837}
77838
77839/*
77840** Remove the i-th cell from pPage. This routine effects pPage only.
77841** The cell content is not freed or deallocated. It is assumed that
77842** the cell content has been copied someplace else. This routine just
77843** removes the reference to the cell from pPage.
77844**
77845** "sz" must be the number of bytes in the cell.
77846*/
77847static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
77848 u32 pc; /* Offset to cell content of cell being deleted */
77849 u8 *data; /* pPage->aData */
77850 u8 *ptr; /* Used to move bytes around within data[] */
77851 int rc; /* The return code */
77852 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
77853
77854 if( *pRC ) return;
77855 assert( idx>=0 )((void) (0));
77856 assert( idx<pPage->nCell )((void) (0));
77857 assert( CORRUPT_DB || sz==cellSize(pPage, idx) )((void) (0));
77858 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
77859 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
77860 assert( pPage->nFree>=0 )((void) (0));
77861 data = pPage->aData;
77862 ptr = &pPage->aCellIdx[2*idx];
77863 assert( pPage->pBt->usableSize > (u32)(ptr-data) )((void) (0));
77864 pc = get2byte(ptr)((ptr)[0]<<8 | (ptr)[1]);
77865 hdr = pPage->hdrOffset;
77866 testcase( pc==(u32)get2byte(&data[hdr+5]) );
77867 testcase( pc+sz==pPage->pBt->usableSize );
77868 if( pc+sz > pPage->pBt->usableSize ){
77869 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(77869);
77870 return;
77871 }
77872 rc = freeSpace(pPage, pc, sz);
77873 if( rc ){
77874 *pRC = rc;
77875 return;
77876 }
77877 pPage->nCell--;
77878 if( pPage->nCell==0 ){
77879 memset(&data[hdr+1], 0, 4);
77880 data[hdr+7] = 0;
77881 put2byte(&data[hdr+5], pPage->pBt->usableSize)((&data[hdr+5])[0] = (u8)((pPage->pBt->usableSize)>>
8), (&data[hdr+5])[1] = (u8)(pPage->pBt->usableSize
))
;
77882 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
77883 - pPage->childPtrSize - 8;
77884 }else{
77885 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
77886 put2byte(&data[hdr+3], pPage->nCell)((&data[hdr+3])[0] = (u8)((pPage->nCell)>>8), (&
data[hdr+3])[1] = (u8)(pPage->nCell))
;
77887 pPage->nFree += 2;
77888 }
77889}
77890
77891/*
77892** Insert a new cell on pPage at cell index "i". pCell points to the
77893** content of the cell.
77894**
77895** If the cell content will fit on the page, then put it there. If it
77896** will not fit, then make a copy of the cell content into pTemp if
77897** pTemp is not null. Regardless of pTemp, allocate a new entry
77898** in pPage->apOvfl[] and make it point to the cell content (either
77899** in pTemp or the original pCell) and also record its index.
77900** Allocating a new entry in pPage->aCell[] implies that
77901** pPage->nOverflow is incremented.
77902**
77903** The insertCellFast() routine below works exactly the same as
77904** insertCell() except that it lacks the pTemp and iChild parameters
77905** which are assumed zero. Other than that, the two routines are the
77906** same.
77907**
77908** Fixes or enhancements to this routine should be reflected in
77909** insertCellFast()!
77910*/
77911static int insertCell(
77912 MemPage *pPage, /* Page into which we are copying */
77913 int i, /* New cell becomes the i-th cell of the page */
77914 u8 *pCell, /* Content of the new cell */
77915 int sz, /* Bytes of content in pCell */
77916 u8 *pTemp, /* Temp storage space for pCell, if needed */
77917 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
77918){
77919 int idx = 0; /* Where to write new cell content in data[] */
77920 int j; /* Loop counter */
77921 u8 *data; /* The content of the whole page */
77922 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77923
77924 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow )((void) (0));
77925 assert( MX_CELL(pPage->pBt)<=10921 )((void) (0));
77926 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB )((void) (0));
77927 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) )((void) (0));
77928 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) )((void) (0));
77929 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
77930 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB )((void) (0));
77931 assert( pPage->nFree>=0 )((void) (0));
77932 assert( iChild>0 )((void) (0));
77933 if( pPage->nOverflow || sz+2>pPage->nFree ){
77934 if( pTemp ){
77935 memcpy(pTemp, pCell, sz);
77936 pCell = pTemp;
77937 }
77938 put4bytesqlite3Put4byte(pCell, iChild);
77939 j = pPage->nOverflow++;
77940 /* Comparison against ArraySize-1 since we hold back one extra slot
77941 ** as a contingency. In other words, never need more than 3 overflow
77942 ** slots but 4 are allocated, just to be safe. */
77943 assert( j < ArraySize(pPage->apOvfl)-1 )((void) (0));
77944 pPage->apOvfl[j] = pCell;
77945 pPage->aiOvfl[j] = (u16)i;
77946
77947 /* When multiple overflows occur, they are always sequential and in
77948 ** sorted order. This invariants arise because multiple overflows can
77949 ** only occur when inserting divider cells into the parent page during
77950 ** balancing, and the dividers are adjacent and sorted.
77951 */
77952 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i )((void) (0)); /* Overflows in sorted order */
77953 assert( j==0 || i==pPage->aiOvfl[j-1]+1 )((void) (0)); /* Overflows are sequential */
77954 }else{
77955 int rc = sqlite3PagerWrite(pPage->pDbPage);
77956 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
77957 return rc;
77958 }
77959 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
77960 data = pPage->aData;
77961 assert( &data[pPage->cellOffset]==pPage->aCellIdx )((void) (0));
77962 rc = allocateSpace(pPage, sz, &idx);
77963 if( rc ){ return rc; }
77964 /* The allocateSpace() routine guarantees the following properties
77965 ** if it returns successfully */
77966 assert( idx >= 0 )((void) (0));
77967 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB )((void) (0));
77968 assert( idx+sz <= (int)pPage->pBt->usableSize )((void) (0));
77969 pPage->nFree -= (u16)(2 + sz);
77970 /* In a corrupt database where an entry in the cell index section of
77971 ** a btree page has a value of 3 or less, the pCell value might point
77972 ** as many as 4 bytes in front of the start of the aData buffer for
77973 ** the source page. Make sure this does not cause problems by not
77974 ** reading the first 4 bytes */
77975 memcpy(&data[idx+4], pCell+4, sz-4);
77976 put4bytesqlite3Put4byte(&data[idx], iChild);
77977 pIns = pPage->aCellIdx + i*2;
77978 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77979 put2byte(pIns, idx)((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
77980 pPage->nCell++;
77981 /* increment the cell count */
77982 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77983 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB )((void) (0));
77984#ifndef SQLITE_OMIT_AUTOVACUUM
77985 if( pPage->pBt->autoVacuum ){
77986 int rc2 = SQLITE_OK0;
77987 /* The cell may contain a pointer to an overflow page. If so, write
77988 ** the entry for the overflow page into the pointer map.
77989 */
77990 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
77991 if( rc2 ) return rc2;
77992 }
77993#endif
77994 }
77995 return SQLITE_OK0;
77996}
77997
77998/*
77999** This variant of insertCell() assumes that the pTemp and iChild
78000** parameters are both zero. Use this variant in sqlite3BtreeInsert()
78001** for performance improvement, and also so that this variant is only
78002** called from that one place, and is thus inlined, and thus runs must
78003** faster.
78004**
78005** Fixes or enhancements to this routine should be reflected into
78006** the insertCell() routine.
78007*/
78008static int insertCellFast(
78009 MemPage *pPage, /* Page into which we are copying */
78010 int i, /* New cell becomes the i-th cell of the page */
78011 u8 *pCell, /* Content of the new cell */
78012 int sz /* Bytes of content in pCell */
78013){
78014 int idx = 0; /* Where to write new cell content in data[] */
78015 int j; /* Loop counter */
78016 u8 *data; /* The content of the whole page */
78017 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
78018
78019 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow )((void) (0));
78020 assert( MX_CELL(pPage->pBt)<=10921 )((void) (0));
78021 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB )((void) (0));
78022 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) )((void) (0));
78023 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) )((void) (0));
78024 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78025 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB )((void) (0));
78026 assert( pPage->nFree>=0 )((void) (0));
78027 assert( pPage->nOverflow==0 )((void) (0));
78028 if( sz+2>pPage->nFree ){
78029 j = pPage->nOverflow++;
78030 /* Comparison against ArraySize-1 since we hold back one extra slot
78031 ** as a contingency. In other words, never need more than 3 overflow
78032 ** slots but 4 are allocated, just to be safe. */
78033 assert( j < ArraySize(pPage->apOvfl)-1 )((void) (0));
78034 pPage->apOvfl[j] = pCell;
78035 pPage->aiOvfl[j] = (u16)i;
78036
78037 /* When multiple overflows occur, they are always sequential and in
78038 ** sorted order. This invariants arise because multiple overflows can
78039 ** only occur when inserting divider cells into the parent page during
78040 ** balancing, and the dividers are adjacent and sorted.
78041 */
78042 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i )((void) (0)); /* Overflows in sorted order */
78043 assert( j==0 || i==pPage->aiOvfl[j-1]+1 )((void) (0)); /* Overflows are sequential */
78044 }else{
78045 int rc = sqlite3PagerWrite(pPage->pDbPage);
78046 if( rc!=SQLITE_OK0 ){
78047 return rc;
78048 }
78049 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
78050 data = pPage->aData;
78051 assert( &data[pPage->cellOffset]==pPage->aCellIdx )((void) (0));
78052 rc = allocateSpace(pPage, sz, &idx);
78053 if( rc ){ return rc; }
78054 /* The allocateSpace() routine guarantees the following properties
78055 ** if it returns successfully */
78056 assert( idx >= 0 )((void) (0));
78057 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB )((void) (0));
78058 assert( idx+sz <= (int)pPage->pBt->usableSize )((void) (0));
78059 pPage->nFree -= (u16)(2 + sz);
78060 memcpy(&data[idx], pCell, sz);
78061 pIns = pPage->aCellIdx + i*2;
78062 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
78063 put2byte(pIns, idx)((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
78064 pPage->nCell++;
78065 /* increment the cell count */
78066 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
78067 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB )((void) (0));
78068#ifndef SQLITE_OMIT_AUTOVACUUM
78069 if( pPage->pBt->autoVacuum ){
78070 int rc2 = SQLITE_OK0;
78071 /* The cell may contain a pointer to an overflow page. If so, write
78072 ** the entry for the overflow page into the pointer map.
78073 */
78074 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
78075 if( rc2 ) return rc2;
78076 }
78077#endif
78078 }
78079 return SQLITE_OK0;
78080}
78081
78082/*
78083** The following parameters determine how many adjacent pages get involved
78084** in a balancing operation. NN is the number of neighbors on either side
78085** of the page that participate in the balancing operation. NB is the
78086** total number of pages that participate, including the target page and
78087** NN neighbors on either side.
78088**
78089** The minimum value of NN is 1 (of course). Increasing NN above 1
78090** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
78091** in exchange for a larger degradation in INSERT and UPDATE performance.
78092** The value of NN appears to give the best results overall.
78093**
78094** (Later:) The description above makes it seem as if these values are
78095** tunable - as if you could change them and recompile and it would all work.
78096** But that is unlikely. NB has been 3 since the inception of SQLite and
78097** we have never tested any other value.
78098*/
78099#define NN1 1 /* Number of neighbors on either side of pPage */
78100#define NB3 3 /* (NN*2+1): Total pages involved in the balance */
78101
78102/*
78103** A CellArray object contains a cache of pointers and sizes for a
78104** consecutive sequence of cells that might be held on multiple pages.
78105**
78106** The cells in this array are the divider cell or cells from the pParent
78107** page plus up to three child pages. There are a total of nCell cells.
78108**
78109** pRef is a pointer to one of the pages that contributes cells. This is
78110** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
78111** which should be common to all pages that contribute cells to this array.
78112**
78113** apCell[] and szCell[] hold, respectively, pointers to the start of each
78114** cell and the size of each cell. Some of the apCell[] pointers might refer
78115** to overflow cells. In other words, some apCel[] pointers might not point
78116** to content area of the pages.
78117**
78118** A szCell[] of zero means the size of that cell has not yet been computed.
78119**
78120** The cells come from as many as four different pages:
78121**
78122** -----------
78123** | Parent |
78124** -----------
78125** / | \
78126** / | \
78127** --------- --------- ---------
78128** |Child-1| |Child-2| |Child-3|
78129** --------- --------- ---------
78130**
78131** The order of cells is in the array is for an index btree is:
78132**
78133** 1. All cells from Child-1 in order
78134** 2. The first divider cell from Parent
78135** 3. All cells from Child-2 in order
78136** 4. The second divider cell from Parent
78137** 5. All cells from Child-3 in order
78138**
78139** For a table-btree (with rowids) the items 2 and 4 are empty because
78140** content exists only in leaves and there are no divider cells.
78141**
78142** For an index btree, the apEnd[] array holds pointer to the end of page
78143** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
78144** respectively. The ixNx[] array holds the number of cells contained in
78145** each of these 5 stages, and all stages to the left. Hence:
78146**
78147** ixNx[0] = Number of cells in Child-1.
78148** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
78149** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
78150** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
78151** ixNx[4] = Total number of cells.
78152**
78153** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
78154** are used and they point to the leaf pages only, and the ixNx value are:
78155**
78156** ixNx[0] = Number of cells in Child-1.
78157** ixNx[1] = Number of cells in Child-1 and Child-2.
78158** ixNx[2] = Total number of cells.
78159**
78160** Sometimes when deleting, a child page can have zero cells. In those
78161** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
78162** entries, shift down. The end result is that each ixNx[] entry should
78163** be larger than the previous
78164*/
78165typedef struct CellArray CellArray;
78166struct CellArray {
78167 int nCell; /* Number of cells in apCell[] */
78168 MemPage *pRef; /* Reference page */
78169 u8 **apCell; /* All cells begin balanced */
78170 u16 *szCell; /* Local size of all cells in apCell[] */
78171 u8 *apEnd[NB3*2]; /* MemPage.aDataEnd values */
78172 int ixNx[NB3*2]; /* Index of at which we move to the next apEnd[] */
78173};
78174
78175/*
78176** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
78177** computed.
78178*/
78179static void populateCellCache(CellArray *p, int idx, int N){
78180 MemPage *pRef = p->pRef;
78181 u16 *szCell = p->szCell;
78182 assert( idx>=0 && idx+N<=p->nCell )((void) (0));
78183 while( N>0 ){
78184 assert( p->apCell[idx]!=0 )((void) (0));
78185 if( szCell[idx]==0 ){
78186 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
78187 }else{
78188 assert( CORRUPT_DB ||((void) (0))
78189 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) )((void) (0));
78190 }
78191 idx++;
78192 N--;
78193 }
78194}
78195
78196/*
78197** Return the size of the Nth element of the cell array
78198*/
78199static SQLITE_NOINLINE__attribute__((noinline)) u16 computeCellSize(CellArray *p, int N){
78200 assert( N>=0 && N<p->nCell )((void) (0));
78201 assert( p->szCell[N]==0 )((void) (0));
78202 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
78203 return p->szCell[N];
78204}
78205static u16 cachedCellSize(CellArray *p, int N){
78206 assert( N>=0 && N<p->nCell )((void) (0));
78207 if( p->szCell[N] ) return p->szCell[N];
78208 return computeCellSize(p, N);
78209}
78210
78211/*
78212** Array apCell[] contains pointers to nCell b-tree page cells. The
78213** szCell[] array contains the size in bytes of each cell. This function
78214** replaces the current contents of page pPg with the contents of the cell
78215** array.
78216**
78217** Some of the cells in apCell[] may currently be stored in pPg. This
78218** function works around problems caused by this by making a copy of any
78219** such cells before overwriting the page data.
78220**
78221** The MemPage.nFree field is invalidated by this function. It is the
78222** responsibility of the caller to set it correctly.
78223*/
78224static int rebuildPage(
78225 CellArray *pCArray, /* Content to be added to page pPg */
78226 int iFirst, /* First cell in pCArray to use */
78227 int nCell, /* Final number of cells on page */
78228 MemPage *pPg /* The page to be reconstructed */
78229){
78230 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
78231 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
78232 const int usableSize = pPg->pBt->usableSize;
78233 u8 * const pEnd = &aData[usableSize];
78234 int i = iFirst; /* Which cell to copy from pCArray*/
78235 u32 j; /* Start of cell content area */
78236 int iEnd = i+nCell; /* Loop terminator */
78237 u8 *pCellptr = pPg->aCellIdx;
78238 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
78239 u8 *pData;
78240 int k; /* Current slot in pCArray->apEnd[] */
78241 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
78242
78243 assert( nCell>0 )((void) (0));
78244 assert( i<iEnd )((void) (0));
78245 j = get2byte(&aData[hdr+5])((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1]);
78246 if( j>(u32)usableSize ){ j = 0; }
78247 memcpy(&pTmp[j], &aData[j], usableSize - j);
78248
78249 assert( pCArray->ixNx[NB*2-1]>i )((void) (0));
78250 for(k=0; pCArray->ixNx[k]<=i; k++){}
78251 pSrcEnd = pCArray->apEnd[k];
78252
78253 pData = pEnd;
78254 while( 1/*exit by break*/ ){
78255 u8 *pCell = pCArray->apCell[i];
78256 u16 sz = pCArray->szCell[i];
78257 assert( sz>0 )((void) (0));
78258 if( SQLITE_WITHIN(pCell,aData+j,pEnd)(((uptr)(pCell)>=(uptr)(aData+j))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
78259 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78259);
78260 pCell = &pTmp[pCell - aData];
78261 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
78262 && (uptr)(pCell)<(uptr)pSrcEnd
78263 ){
78264 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78264);
78265 }
78266
78267 pData -= sz;
78268 put2byte(pCellptr, (pData - aData))((pCellptr)[0] = (u8)(((pData - aData))>>8), (pCellptr)
[1] = (u8)((pData - aData)))
;
78269 pCellptr += 2;
78270 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78270);
78271 memmove(pData, pCell, sz);
78272 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB )((void) (0));
78273 i++;
78274 if( i>=iEnd ) break;
78275 if( pCArray->ixNx[k]<=i ){
78276 k++;
78277 pSrcEnd = pCArray->apEnd[k];
78278 }
78279 }
78280
78281 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78282 pPg->nCell = nCell;
78283 pPg->nOverflow = 0;
78284
78285 put2byte(&aData[hdr+1], 0)((&aData[hdr+1])[0] = (u8)((0)>>8), (&aData[hdr
+1])[1] = (u8)(0))
;
78286 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
78287 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
78288 aData[hdr+7] = 0x00;
78289 return SQLITE_OK0;
78290}
78291
78292/*
78293** The pCArray objects contains pointers to b-tree cells and the cell sizes.
78294** This function attempts to add the cells stored in the array to page pPg.
78295** If it cannot (because the page needs to be defragmented before the cells
78296** will fit), non-zero is returned. Otherwise, if the cells are added
78297** successfully, zero is returned.
78298**
78299** Argument pCellptr points to the first entry in the cell-pointer array
78300** (part of page pPg) to populate. After cell apCell[0] is written to the
78301** page body, a 16-bit offset is written to pCellptr. And so on, for each
78302** cell in the array. It is the responsibility of the caller to ensure
78303** that it is safe to overwrite this part of the cell-pointer array.
78304**
78305** When this function is called, *ppData points to the start of the
78306** content area on page pPg. If the size of the content area is extended,
78307** *ppData is updated to point to the new start of the content area
78308** before returning.
78309**
78310** Finally, argument pBegin points to the byte immediately following the
78311** end of the space required by this page for the cell-pointer area (for
78312** all cells - not just those inserted by the current call). If the content
78313** area must be extended to before this point in order to accommodate all
78314** cells in apCell[], then the cells do not fit and non-zero is returned.
78315*/
78316static int pageInsertArray(
78317 MemPage *pPg, /* Page to add cells to */
78318 u8 *pBegin, /* End of cell-pointer array */
78319 u8 **ppData, /* IN/OUT: Page content-area pointer */
78320 u8 *pCellptr, /* Pointer to cell-pointer area */
78321 int iFirst, /* Index of first cell to add */
78322 int nCell, /* Number of cells to add to pPg */
78323 CellArray *pCArray /* Array of cells */
78324){
78325 int i = iFirst; /* Loop counter - cell index to insert */
78326 u8 *aData = pPg->aData; /* Complete page */
78327 u8 *pData = *ppData; /* Content area. A subset of aData[] */
78328 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
78329 int k; /* Current slot in pCArray->apEnd[] */
78330 u8 *pEnd; /* Maximum extent of cell data */
78331 assert( CORRUPT_DB || pPg->hdrOffset==0 )((void) (0)); /* Never called on page 1 */
78332 if( iEnd<=iFirst ) return 0;
78333 assert( pCArray->ixNx[NB*2-1]>i )((void) (0));
78334 for(k=0; pCArray->ixNx[k]<=i ; k++){}
78335 pEnd = pCArray->apEnd[k];
78336 while( 1 /*Exit by break*/ ){
78337 int sz, rc;
78338 u8 *pSlot;
78339 assert( pCArray->szCell[i]!=0 )((void) (0));
78340 sz = pCArray->szCell[i];
78341 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
78342 if( (pData - pBegin)<sz ) return 1;
78343 pData -= sz;
78344 pSlot = pData;
78345 }
78346 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
78347 ** database. But they might for a corrupt database. Hence use memmove()
78348 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
78349 assert( (pSlot+sz)<=pCArray->apCell[i]((void) (0))
78350 || pSlot>=(pCArray->apCell[i]+sz)((void) (0))
78351 || CORRUPT_DB )((void) (0));
78352 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
78353 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
78354 ){
78355 assert( CORRUPT_DB )((void) (0));
78356 (void)SQLITE_CORRUPT_BKPTsqlite3CorruptError(78356);
78357 return 1;
78358 }
78359 memmove(pSlot, pCArray->apCell[i], sz);
78360 put2byte(pCellptr, (pSlot - aData))((pCellptr)[0] = (u8)(((pSlot - aData))>>8), (pCellptr)
[1] = (u8)((pSlot - aData)))
;
78361 pCellptr += 2;
78362 i++;
78363 if( i>=iEnd ) break;
78364 if( pCArray->ixNx[k]<=i ){
78365 k++;
78366 pEnd = pCArray->apEnd[k];
78367 }
78368 }
78369 *ppData = pData;
78370 return 0;
78371}
78372
78373/*
78374** The pCArray object contains pointers to b-tree cells and their sizes.
78375**
78376** This function adds the space associated with each cell in the array
78377** that is currently stored within the body of pPg to the pPg free-list.
78378** The cell-pointers and other fields of the page are not updated.
78379**
78380** This function returns the total number of cells added to the free-list.
78381*/
78382static int pageFreeArray(
78383 MemPage *pPg, /* Page to edit */
78384 int iFirst, /* First cell to delete */
78385 int nCell, /* Cells to delete */
78386 CellArray *pCArray /* Array of cells */
78387){
78388 u8 * const aData = pPg->aData;
78389 u8 * const pEnd = &aData[pPg->pBt->usableSize];
78390 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
78391 int nRet = 0;
78392 int i, j;
78393 int iEnd = iFirst + nCell;
78394 int nFree = 0;
78395 int aOfst[10];
78396 int aAfter[10];
78397
78398 for(i=iFirst; i<iEnd; i++){
78399 u8 *pCell = pCArray->apCell[i];
78400 if( SQLITE_WITHIN(pCell, pStart, pEnd)(((uptr)(pCell)>=(uptr)(pStart))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
78401 int sz;
78402 int iAfter;
78403 int iOfst;
78404 /* No need to use cachedCellSize() here. The sizes of all cells that
78405 ** are to be freed have already been computing while deciding which
78406 ** cells need freeing */
78407 sz = pCArray->szCell[i]; assert( sz>0 )((void) (0));
78408 iOfst = (u16)(pCell - aData);
78409 iAfter = iOfst+sz;
78410 for(j=0; j<nFree; j++){
78411 if( aOfst[j]==iAfter ){
78412 aOfst[j] = iOfst;
78413 break;
78414 }else if( aAfter[j]==iOfst ){
78415 aAfter[j] = iAfter;
78416 break;
78417 }
78418 }
78419 if( j>=nFree ){
78420 if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){
78421 for(j=0; j<nFree; j++){
78422 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
78423 }
78424 nFree = 0;
78425 }
78426 aOfst[nFree] = iOfst;
78427 aAfter[nFree] = iAfter;
78428 if( &aData[iAfter]>pEnd ) return 0;
78429 nFree++;
78430 }
78431 nRet++;
78432 }
78433 }
78434 for(j=0; j<nFree; j++){
78435 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
78436 }
78437 return nRet;
78438}
78439
78440/*
78441** pCArray contains pointers to and sizes of all cells in the page being
78442** balanced. The current page, pPg, has pPg->nCell cells starting with
78443** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
78444** starting at apCell[iNew].
78445**
78446** This routine makes the necessary adjustments to pPg so that it contains
78447** the correct cells after being balanced.
78448**
78449** The pPg->nFree field is invalid when this function returns. It is the
78450** responsibility of the caller to set it correctly.
78451*/
78452static int editPage(
78453 MemPage *pPg, /* Edit this page */
78454 int iOld, /* Index of first cell currently on page */
78455 int iNew, /* Index of new first cell on page */
78456 int nNew, /* Final number of cells on page */
78457 CellArray *pCArray /* Array of cells and sizes */
78458){
78459 u8 * const aData = pPg->aData;
78460 const int hdr = pPg->hdrOffset;
78461 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
78462 int nCell = pPg->nCell; /* Cells stored on pPg */
78463 u8 *pData;
78464 u8 *pCellptr;
78465 int i;
78466 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
78467 int iNewEnd = iNew + nNew;
78468
78469#ifdef SQLITE_DEBUG
78470 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
78471 memcpy(pTmp, aData, pPg->pBt->usableSize);
78472#endif
78473
78474 /* Remove cells from the start and end of the page */
78475 assert( nCell>=0 )((void) (0));
78476 if( iOld<iNew ){
78477 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
78478 if( NEVER(nShift>nCell)(nShift>nCell) ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78478);
78479 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
78480 nCell -= nShift;
78481 }
78482 if( iNewEnd < iOldEnd ){
78483 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
78484 assert( nCell>=nTail )((void) (0));
78485 nCell -= nTail;
78486 }
78487
78488 pData = &aData[get2byte(&aData[hdr+5])((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1])];
78489 if( pData<pBegin ) goto editpage_fail;
78490 if( NEVER(pData>pPg->aDataEnd)(pData>pPg->aDataEnd) ) goto editpage_fail;
78491
78492 /* Add cells to the start of the page */
78493 if( iNew<iOld ){
78494 int nAdd = MIN(nNew,iOld-iNew)((nNew)<(iOld-iNew)?(nNew):(iOld-iNew));
78495 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB )((void) (0));
78496 assert( nAdd>=0 )((void) (0));
78497 pCellptr = pPg->aCellIdx;
78498 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
78499 if( pageInsertArray(
78500 pPg, pBegin, &pData, pCellptr,
78501 iNew, nAdd, pCArray
78502 ) ) goto editpage_fail;
78503 nCell += nAdd;
78504 }
78505
78506 /* Add any overflow cells */
78507 for(i=0; i<pPg->nOverflow; i++){
78508 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
78509 if( iCell>=0 && iCell<nNew ){
78510 pCellptr = &pPg->aCellIdx[iCell * 2];
78511 if( nCell>iCell ){
78512 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
78513 }
78514 nCell++;
78515 cachedCellSize(pCArray, iCell+iNew);
78516 if( pageInsertArray(
78517 pPg, pBegin, &pData, pCellptr,
78518 iCell+iNew, 1, pCArray
78519 ) ) goto editpage_fail;
78520 }
78521 }
78522
78523 /* Append cells to the end of the page */
78524 assert( nCell>=0 )((void) (0));
78525 pCellptr = &pPg->aCellIdx[nCell*2];
78526 if( pageInsertArray(
78527 pPg, pBegin, &pData, pCellptr,
78528 iNew+nCell, nNew-nCell, pCArray
78529 ) ) goto editpage_fail;
78530
78531 pPg->nCell = nNew;
78532 pPg->nOverflow = 0;
78533
78534 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
78535 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
78536
78537#ifdef SQLITE_DEBUG
78538 for(i=0; i<nNew && !CORRUPT_DB(sqlite3Config.neverCorrupt==0); i++){
78539 u8 *pCell = pCArray->apCell[i+iNew];
78540 int iOff = get2byteAligned(&pPg->aCellIdx[i*2])((&pPg->aCellIdx[i*2])[0]<<8 | (&pPg->aCellIdx
[i*2])[1])
;
78541 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize])(((uptr)(pCell)>=(uptr)(aData))&&((uptr)(pCell)<
(uptr)(&aData[pPg->pBt->usableSize])))
){
78542 pCell = &pTmp[pCell - aData];
78543 }
78544 assert( 0==memcmp(pCell, &aData[iOff],((void) (0))
78545 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) )((void) (0));
78546 }
78547#endif
78548
78549 return SQLITE_OK0;
78550 editpage_fail:
78551 /* Unable to edit this page. Rebuild it from scratch instead. */
78552 if( nNew<1 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78552);
78553 populateCellCache(pCArray, iNew, nNew);
78554 return rebuildPage(pCArray, iNew, nNew, pPg);
78555}
78556
78557
78558#ifndef SQLITE_OMIT_QUICKBALANCE
78559/*
78560** This version of balance() handles the common special case where
78561** a new entry is being inserted on the extreme right-end of the
78562** tree, in other words, when the new entry will become the largest
78563** entry in the tree.
78564**
78565** Instead of trying to balance the 3 right-most leaf pages, just add
78566** a new page to the right-hand side and put the one new entry in
78567** that page. This leaves the right side of the tree somewhat
78568** unbalanced. But odds are that we will be inserting new entries
78569** at the end soon afterwards so the nearly empty page will quickly
78570** fill up. On average.
78571**
78572** pPage is the leaf page which is the right-most page in the tree.
78573** pParent is its parent. pPage must have a single overflow entry
78574** which is also the right-most entry on the page.
78575**
78576** The pSpace buffer is used to store a temporary copy of the divider
78577** cell that will be inserted into pParent. Such a cell consists of a 4
78578** byte page number followed by a variable length integer. In other
78579** words, at most 13 bytes. Hence the pSpace buffer must be at
78580** least 13 bytes in size.
78581*/
78582static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
78583 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
78584 MemPage *pNew; /* Newly allocated page */
78585 int rc; /* Return Code */
78586 Pgno pgnoNew; /* Page number of pNew */
78587
78588 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
78589 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
78590 assert( pPage->nOverflow==1 )((void) (0));
78591
78592 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(78592); /* dbfuzz001.test */
78593 assert( pPage->nFree>=0 )((void) (0));
78594 assert( pParent->nFree>=0 )((void) (0));
78595
78596 /* Allocate a new page. This page will become the right-sibling of
78597 ** pPage. Make the parent page writable, so that the new divider cell
78598 ** may be inserted. If both these operations are successful, proceed.
78599 */
78600 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
78601
78602 if( rc==SQLITE_OK0 ){
78603
78604 u8 *pOut = &pSpace[4];
78605 u8 *pCell = pPage->apOvfl[0];
78606 u16 szCell = pPage->xCellSize(pPage, pCell);
78607 u8 *pStop;
78608 CellArray b;
78609
78610 assert( sqlite3PagerIswriteable(pNew->pDbPage) )((void) (0));
78611 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) )((void) (0));
78612 zeroPage(pNew, PTF_INTKEY0x01|PTF_LEAFDATA0x04|PTF_LEAF0x08);
78613 b.nCell = 1;
78614 b.pRef = pPage;
78615 b.apCell = &pCell;
78616 b.szCell = &szCell;
78617 b.apEnd[0] = pPage->aDataEnd;
78618 b.ixNx[0] = 2;
78619 b.ixNx[NB3*2-1] = 0x7fffffff;
78620 rc = rebuildPage(&b, 0, 1, pNew);
78621 if( NEVER(rc)(rc) ){
78622 releasePage(pNew);
78623 return rc;
78624 }
78625 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
78626
78627 /* If this is an auto-vacuum database, update the pointer map
78628 ** with entries for the new page, and any pointer from the
78629 ** cell on the page to an overflow page. If either of these
78630 ** operations fails, the return code is set, but the contents
78631 ** of the parent page are still manipulated by the code below.
78632 ** That is Ok, at this point the parent page is guaranteed to
78633 ** be marked as dirty. Returning an error code will cause a
78634 ** rollback, undoing any changes made to the parent page.
78635 */
78636 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
78637 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE5, pParent->pgno, &rc);
78638 if( szCell>pNew->minLocal ){
78639 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
78640 }
78641 }
78642
78643 /* Create a divider cell to insert into pParent. The divider cell
78644 ** consists of a 4-byte page number (the page number of pPage) and
78645 ** a variable length key value (which must be the same value as the
78646 ** largest key on pPage).
78647 **
78648 ** To find the largest key value on pPage, first find the right-most
78649 ** cell on pPage. The first two fields of this cell are the
78650 ** record-length (a variable length integer at most 32-bits in size)
78651 ** and the key value (a variable length integer, may have any value).
78652 ** The first of the while(...) loops below skips over the record-length
78653 ** field. The second while(...) loop copies the key value from the
78654 ** cell on pPage into the pSpace buffer.
78655 */
78656 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])))
;
78657 pStop = &pCell[9];
78658 while( (*(pCell++)&0x80) && pCell<pStop );
78659 pStop = &pCell[9];
78660 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
78661
78662 /* Insert the new divider cell into pParent. */
78663 if( rc==SQLITE_OK0 ){
78664 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
78665 0, pPage->pgno);
78666 }
78667
78668 /* Set the right-child pointer of pParent to point to the new page. */
78669 put4bytesqlite3Put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
78670
78671 /* Release the reference to the new page. */
78672 releasePage(pNew);
78673 }
78674
78675 return rc;
78676}
78677#endif /* SQLITE_OMIT_QUICKBALANCE */
78678
78679#if 0
78680/*
78681** This function does not contribute anything to the operation of SQLite.
78682** it is sometimes activated temporarily while debugging code responsible
78683** for setting pointer-map entries.
78684*/
78685static int ptrmapCheckPages(MemPage **apPage, int nPage){
78686 int i, j;
78687 for(i=0; i<nPage; i++){
78688 Pgno n;
78689 u8 e;
78690 MemPage *pPage = apPage[i];
78691 BtShared *pBt = pPage->pBt;
78692 assert( pPage->isInit )((void) (0));
78693
78694 for(j=0; j<pPage->nCell; j++){
78695 CellInfo info;
78696 u8 *z;
78697
78698 z = findCell(pPage, j)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(j)])[0]<<8 | (&(pPage)->aCellIdx
[2*(j)])[1])))
;
78699 pPage->xParseCell(pPage, z, &info);
78700 if( info.nLocal<info.nPayload ){
78701 Pgno ovfl = get4bytesqlite3Get4byte(&z[info.nSize-4]);
78702 ptrmapGet(pBt, ovfl, &e, &n);
78703 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 )((void) (0));
78704 }
78705 if( !pPage->leaf ){
78706 Pgno child = get4bytesqlite3Get4byte(z);
78707 ptrmapGet(pBt, child, &e, &n);
78708 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
78709 }
78710 }
78711 if( !pPage->leaf ){
78712 Pgno child = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
78713 ptrmapGet(pBt, child, &e, &n);
78714 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
78715 }
78716 }
78717 return 1;
78718}
78719#endif
78720
78721/*
78722** This function is used to copy the contents of the b-tree node stored
78723** on page pFrom to page pTo. If page pFrom was not a leaf page, then
78724** the pointer-map entries for each child page are updated so that the
78725** parent page stored in the pointer map is page pTo. If pFrom contained
78726** any cells with overflow page pointers, then the corresponding pointer
78727** map entries are also updated so that the parent page is page pTo.
78728**
78729** If pFrom is currently carrying any overflow cells (entries in the
78730** MemPage.apOvfl[] array), they are not copied to pTo.
78731**
78732** Before returning, page pTo is reinitialized using btreeInitPage().
78733**
78734** The performance of this function is not critical. It is only used by
78735** the balance_shallower() and balance_deeper() procedures, neither of
78736** which are called often under normal circumstances.
78737*/
78738static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
78739 if( (*pRC)==SQLITE_OK0 ){
78740 BtShared * const pBt = pFrom->pBt;
78741 u8 * const aFrom = pFrom->aData;
78742 u8 * const aTo = pTo->aData;
78743 int const iFromHdr = pFrom->hdrOffset;
78744 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
78745 int rc;
78746 int iData;
78747
78748
78749 assert( pFrom->isInit )((void) (0));
78750 assert( pFrom->nFree>=iToHdr )((void) (0));
78751 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize )((void) (0));
78752
78753 /* Copy the b-tree node content from page pFrom to page pTo. */
78754 iData = get2byte(&aFrom[iFromHdr+5])((&aFrom[iFromHdr+5])[0]<<8 | (&aFrom[iFromHdr+
5])[1])
;
78755 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
78756 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
78757
78758 /* Reinitialize page pTo so that the contents of the MemPage structure
78759 ** match the new data. The initialization of pTo can actually fail under
78760 ** fairly obscure circumstances, even though it is a copy of initialized
78761 ** page pFrom.
78762 */
78763 pTo->isInit = 0;
78764 rc = btreeInitPage(pTo);
78765 if( rc==SQLITE_OK0 ) rc = btreeComputeFreeSpace(pTo);
78766 if( rc!=SQLITE_OK0 ){
78767 *pRC = rc;
78768 return;
78769 }
78770
78771 /* If this is an auto-vacuum database, update the pointer-map entries
78772 ** for any b-tree or overflow pages that pTo now contains the pointers to.
78773 */
78774 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
78775 *pRC = setChildPtrmaps(pTo);
78776 }
78777 }
78778}
78779
78780/*
78781** This routine redistributes cells on the iParentIdx'th child of pParent
78782** (hereafter "the page") and up to 2 siblings so that all pages have about the
78783** same amount of free space. Usually a single sibling on either side of the
78784** page are used in the balancing, though both siblings might come from one
78785** side if the page is the first or last child of its parent. If the page
78786** has fewer than 2 siblings (something which can only happen if the page
78787** is a root page or a child of a root page) then all available siblings
78788** participate in the balancing.
78789**
78790** The number of siblings of the page might be increased or decreased by
78791** one or two in an effort to keep pages nearly full but not over full.
78792**
78793** Note that when this routine is called, some of the cells on the page
78794** might not actually be stored in MemPage.aData[]. This can happen
78795** if the page is overfull. This routine ensures that all cells allocated
78796** to the page and its siblings fit into MemPage.aData[] before returning.
78797**
78798** In the course of balancing the page and its siblings, cells may be
78799** inserted into or removed from the parent page (pParent). Doing so
78800** may cause the parent page to become overfull or underfull. If this
78801** happens, it is the responsibility of the caller to invoke the correct
78802** balancing routine to fix this problem (see the balance() routine).
78803**
78804** If this routine fails for any reason, it might leave the database
78805** in a corrupted state. So if this routine fails, the database should
78806** be rolled back.
78807**
78808** The third argument to this function, aOvflSpace, is a pointer to a
78809** buffer big enough to hold one page. If while inserting cells into the parent
78810** page (pParent) the parent page becomes overfull, this buffer is
78811** used to store the parent's overflow cells. Because this function inserts
78812** a maximum of four divider cells into the parent page, and the maximum
78813** size of a cell stored within an internal node is always less than 1/4
78814** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
78815** enough for all overflow cells.
78816**
78817** If aOvflSpace is set to a null pointer, this function returns
78818** SQLITE_NOMEM.
78819*/
78820static int balance_nonroot(
78821 MemPage *pParent, /* Parent page of siblings being balanced */
78822 int iParentIdx, /* Index of "the page" in pParent */
78823 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
78824 int isRoot, /* True if pParent is a root-page */
78825 int bBulk /* True if this call is part of a bulk load */
78826){
78827 BtShared *pBt; /* The whole database */
78828 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
78829 int nNew = 0; /* Number of pages in apNew[] */
78830 int nOld; /* Number of pages in apOld[] */
78831 int i, j, k; /* Loop counters */
78832 int nxDiv; /* Next divider slot in pParent->aCell[] */
78833 int rc = SQLITE_OK0; /* The return code */
78834 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
78835 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
78836 int usableSpace; /* Bytes in pPage beyond the header */
78837 int pageFlags; /* Value of pPage->aData[0] */
78838 int iSpace1 = 0; /* First unused byte of aSpace1[] */
78839 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
78840 int szScratch; /* Size of scratch memory requested */
78841 MemPage *apOld[NB3]; /* pPage and up to two siblings */
78842 MemPage *apNew[NB3+2]; /* pPage and up to NB siblings after balancing */
78843 u8 *pRight; /* Location in parent of right-sibling pointer */
78844 u8 *apDiv[NB3-1]; /* Divider cells in pParent */
78845 int cntNew[NB3+2]; /* Index in b.paCell[] of cell after i-th page */
78846 int cntOld[NB3+2]; /* Old index in b.apCell[] */
78847 int szNew[NB3+2]; /* Combined size of cells placed on i-th page */
78848 u8 *aSpace1; /* Space for copies of dividers cells */
78849 Pgno pgno; /* Temp var to store a page number in */
78850 u8 abDone[NB3+2]; /* True after i'th new page is populated */
78851 Pgno aPgno[NB3+2]; /* Page numbers of new pages before shuffling */
78852 CellArray b; /* Parsed information on cells being balanced */
78853
78854 memset(abDone, 0, sizeof(abDone));
78855 assert( sizeof(b) - sizeof(b.ixNx) == offsetof(CellArray,ixNx) )((void) (0));
78856 memset(&b, 0, sizeof(b)-sizeof(b.ixNx[0]));
78857 b.ixNx[NB3*2-1] = 0x7fffffff;
78858 pBt = pParent->pBt;
78859 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
78860 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
78861
78862 /* At this point pParent may have at most one overflow cell. And if
78863 ** this overflow cell is present, it must be the cell with
78864 ** index iParentIdx. This scenario comes about when this function
78865 ** is called (indirectly) from sqlite3BtreeDelete().
78866 */
78867 assert( pParent->nOverflow==0 || pParent->nOverflow==1 )((void) (0));
78868 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx )((void) (0));
78869
78870 if( !aOvflSpace ){
78871 return SQLITE_NOMEM_BKPT7;
78872 }
78873 assert( pParent->nFree>=0 )((void) (0));
78874
78875 /* Find the sibling pages to balance. Also locate the cells in pParent
78876 ** that divide the siblings. An attempt is made to find NN siblings on
78877 ** either side of pPage. More siblings are taken from one side, however,
78878 ** if there are fewer than NN siblings on the other side. If pParent
78879 ** has NB or fewer children then all children of pParent are taken.
78880 **
78881 ** This loop also drops the divider cells from the parent page. This
78882 ** way, the remainder of the function does not have to deal with any
78883 ** overflow cells in the parent page, since if any existed they will
78884 ** have already been removed.
78885 */
78886 i = pParent->nOverflow + pParent->nCell;
78887 if( i<2 ){
78888 nxDiv = 0;
78889 }else{
78890 assert( bBulk==0 || bBulk==1 )((void) (0));
78891 if( iParentIdx==0 ){
78892 nxDiv = 0;
78893 }else if( iParentIdx==i ){
78894 nxDiv = i-2+bBulk;
78895 }else{
78896 nxDiv = iParentIdx-1;
78897 }
78898 i = 2-bBulk;
78899 }
78900 nOld = i+1;
78901 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
78902 pRight = &pParent->aData[pParent->hdrOffset+8];
78903 }else{
78904 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])))
;
78905 }
78906 pgno = get4bytesqlite3Get4byte(pRight);
78907 while( 1 ){
78908 if( rc==SQLITE_OK0 ){
78909 rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
78910 }
78911 if( rc ){
78912 memset(apOld, 0, (i+1)*sizeof(MemPage*));
78913 goto balance_cleanup;
78914 }
78915 if( apOld[i]->nFree<0 ){
78916 rc = btreeComputeFreeSpace(apOld[i]);
78917 if( rc ){
78918 memset(apOld, 0, (i)*sizeof(MemPage*));
78919 goto balance_cleanup;
78920 }
78921 }
78922 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl)((int)(sizeof(pParent->apOvfl)/sizeof(pParent->apOvfl[0
])))
;
78923 if( (i--)==0 ) break;
78924
78925 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
78926 apDiv[i] = pParent->apOvfl[0];
78927 pgno = get4bytesqlite3Get4byte(apDiv[i]);
78928 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78929 pParent->nOverflow = 0;
78930 }else{
78931 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])))
;
78932 pgno = get4bytesqlite3Get4byte(apDiv[i]);
78933 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78934
78935 /* Drop the cell from the parent page. apDiv[i] still points to
78936 ** the cell within the parent, even though it has been dropped.
78937 ** This is safe because dropping a cell only overwrites the first
78938 ** four bytes of it, and this function does not need the first
78939 ** four bytes of the divider cell. So the pointer is safe to use
78940 ** later on.
78941 **
78942 ** But not if we are in secure-delete mode. In secure-delete mode,
78943 ** the dropCell() routine will overwrite the entire cell with zeroes.
78944 ** In this case, temporarily copy the cell into the aOvflSpace[]
78945 ** buffer. It will be copied out again as soon as the aSpace[] buffer
78946 ** is allocated. */
78947 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
78948 int iOff;
78949
78950 /* If the following if() condition is not true, the db is corrupted.
78951 ** The call to dropCell() below will detect this. */
78952 iOff = SQLITE_PTR_TO_INT(apDiv[i])((int)(long int)(apDiv[i])) - SQLITE_PTR_TO_INT(pParent->aData)((int)(long int)(pParent->aData));
78953 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
78954 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
78955 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
78956 }
78957 }
78958 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
78959 }
78960 }
78961
78962 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
78963 ** alignment */
78964 nMaxCells = (nMaxCells + 3)&~3;
78965
78966 /*
78967 ** Allocate space for memory structures
78968 */
78969 szScratch =
78970 nMaxCells*sizeof(u8*) /* b.apCell */
78971 + nMaxCells*sizeof(u16) /* b.szCell */
78972 + pBt->pageSize; /* aSpace1 */
78973
78974 assert( szScratch<=7*(int)pBt->pageSize )((void) (0));
78975 b.apCell = sqlite3StackAllocRaw(0, szScratch )sqlite3DbMallocRaw(0,szScratch);
78976 if( b.apCell==0 ){
78977 rc = SQLITE_NOMEM_BKPT7;
78978 goto balance_cleanup;
78979 }
78980 b.szCell = (u16*)&b.apCell[nMaxCells];
78981 aSpace1 = (u8*)&b.szCell[nMaxCells];
78982 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) )((void) (0));
78983
78984 /*
78985 ** Load pointers to all cells on sibling pages and the divider cells
78986 ** into the local b.apCell[] array. Make copies of the divider cells
78987 ** into space obtained from aSpace1[]. The divider cells have already
78988 ** been removed from pParent.
78989 **
78990 ** If the siblings are on leaf pages, then the child pointers of the
78991 ** divider cells are stripped from the cells before they are copied
78992 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
78993 ** child pointers. If siblings are not leaves, then all cell in
78994 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
78995 ** are alike.
78996 **
78997 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
78998 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
78999 */
79000 b.pRef = apOld[0];
79001 leafCorrection = b.pRef->leaf*4;
79002 leafData = b.pRef->intKeyLeaf;
79003 for(i=0; i<nOld; i++){
79004 MemPage *pOld = apOld[i];
79005 int limit = pOld->nCell;
79006 u8 *aData = pOld->aData;
79007 u16 maskPage = pOld->maskPage;
79008 u8 *piCell = aData + pOld->cellOffset;
79009 u8 *piEnd;
79010 VVA_ONLY( int nCellAtStart = b.nCell; )
79011
79012 /* Verify that all sibling pages are of the same "type" (table-leaf,
79013 ** table-interior, index-leaf, or index-interior).
79014 */
79015 if( pOld->aData[0]!=apOld[0]->aData[0] ){
79016 rc = SQLITE_CORRUPT_PAGE(pOld)sqlite3CorruptError(79016);
79017 goto balance_cleanup;
79018 }
79019
79020 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
79021 ** contains overflow cells, include them in the b.apCell[] array
79022 ** in the correct spot.
79023 **
79024 ** Note that when there are multiple overflow cells, it is always the
79025 ** case that they are sequential and adjacent. This invariant arises
79026 ** because multiple overflows can only occurs when inserting divider
79027 ** cells into a parent on a prior balance, and divider cells are always
79028 ** adjacent and are inserted in order. There is an assert() tagged
79029 ** with "NOTE 1" in the overflow cell insertion loop to prove this
79030 ** invariant.
79031 **
79032 ** This must be done in advance. Once the balance starts, the cell
79033 ** offset section of the btree page will be overwritten and we will no
79034 ** long be able to find the cells if a pointer to each cell is not saved
79035 ** first.
79036 */
79037 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
79038 if( pOld->nOverflow>0 ){
79039 if( NEVER(limit<pOld->aiOvfl[0])(limit<pOld->aiOvfl[0]) ){
79040 rc = SQLITE_CORRUPT_PAGE(pOld)sqlite3CorruptError(79040);
79041 goto balance_cleanup;
79042 }
79043 limit = pOld->aiOvfl[0];
79044 for(j=0; j<limit; j++){
79045 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
79046 piCell += 2;
79047 b.nCell++;
79048 }
79049 for(k=0; k<pOld->nOverflow; k++){
79050 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] )((void) (0));/* NOTE 1 */
79051 b.apCell[b.nCell] = pOld->apOvfl[k];
79052 b.nCell++;
79053 }
79054 }
79055 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
79056 while( piCell<piEnd ){
79057 assert( b.nCell<nMaxCells )((void) (0));
79058 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
79059 piCell += 2;
79060 b.nCell++;
79061 }
79062 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) )((void) (0));
79063
79064 cntOld[i] = b.nCell;
79065 if( i<nOld-1 && !leafData){
79066 u16 sz = (u16)szNew[i];
79067 u8 *pTemp;
79068 assert( b.nCell<nMaxCells )((void) (0));
79069 b.szCell[b.nCell] = sz;
79070 pTemp = &aSpace1[iSpace1];
79071 iSpace1 += sz;
79072 assert( sz<=pBt->maxLocal+23 )((void) (0));
79073 assert( iSpace1 <= (int)pBt->pageSize )((void) (0));
79074 memcpy(pTemp, apDiv[i], sz);
79075 b.apCell[b.nCell] = pTemp+leafCorrection;
79076 assert( leafCorrection==0 || leafCorrection==4 )((void) (0));
79077 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
79078 if( !pOld->leaf ){
79079 assert( leafCorrection==0 )((void) (0));
79080 assert( pOld->hdrOffset==0 || CORRUPT_DB )((void) (0));
79081 /* The right pointer of the child page pOld becomes the left
79082 ** pointer of the divider cell */
79083 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
79084 }else{
79085 assert( leafCorrection==4 )((void) (0));
79086 while( b.szCell[b.nCell]<4 ){
79087 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
79088 ** does exist, pad it with 0x00 bytes. */
79089 assert( b.szCell[b.nCell]==3 || CORRUPT_DB )((void) (0));
79090 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB )((void) (0));
79091 aSpace1[iSpace1++] = 0x00;
79092 b.szCell[b.nCell]++;
79093 }
79094 }
79095 b.nCell++;
79096 }
79097 }
79098
79099 /*
79100 ** Figure out the number of pages needed to hold all b.nCell cells.
79101 ** Store this number in "k". Also compute szNew[] which is the total
79102 ** size of all cells on the i-th page and cntNew[] which is the index
79103 ** in b.apCell[] of the cell that divides page i from page i+1.
79104 ** cntNew[k] should equal b.nCell.
79105 **
79106 ** Values computed by this block:
79107 **
79108 ** k: The total number of sibling pages
79109 ** szNew[i]: Spaced used on the i-th sibling page.
79110 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
79111 ** the right of the i-th sibling page.
79112 ** usableSpace: Number of bytes of space available on each sibling.
79113 **
79114 */
79115 usableSpace = pBt->usableSize - 12 + leafCorrection;
79116 for(i=k=0; i<nOld; i++, k++){
79117 MemPage *p = apOld[i];
79118 b.apEnd[k] = p->aDataEnd;
79119 b.ixNx[k] = cntOld[i];
79120 if( k && b.ixNx[k]==b.ixNx[k-1] ){
79121 k--; /* Omit b.ixNx[] entry for child pages with no cells */
79122 }
79123 if( !leafData ){
79124 k++;
79125 b.apEnd[k] = pParent->aDataEnd;
79126 b.ixNx[k] = cntOld[i]+1;
79127 }
79128 assert( p->nFree>=0 )((void) (0));
79129 szNew[i] = usableSpace - p->nFree;
79130 for(j=0; j<p->nOverflow; j++){
79131 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
79132 }
79133 cntNew[i] = cntOld[i];
79134 }
79135 k = nOld;
79136 for(i=0; i<k; i++){
79137 int sz;
79138 while( szNew[i]>usableSpace ){
79139 if( i+1>=k ){
79140 k = i+2;
79141 if( k>NB3+2 ){ rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(79141); goto balance_cleanup; }
79142 szNew[k-1] = 0;
79143 cntNew[k-1] = b.nCell;
79144 }
79145 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
79146 szNew[i] -= sz;
79147 if( !leafData ){
79148 if( cntNew[i]<b.nCell ){
79149 sz = 2 + cachedCellSize(&b, cntNew[i]);
79150 }else{
79151 sz = 0;
79152 }
79153 }
79154 szNew[i+1] += sz;
79155 cntNew[i]--;
79156 }
79157 while( cntNew[i]<b.nCell ){
79158 sz = 2 + cachedCellSize(&b, cntNew[i]);
79159 if( szNew[i]+sz>usableSpace ) break;
79160 szNew[i] += sz;
79161 cntNew[i]++;
79162 if( !leafData ){
79163 if( cntNew[i]<b.nCell ){
79164 sz = 2 + cachedCellSize(&b, cntNew[i]);
79165 }else{
79166 sz = 0;
79167 }
79168 }
79169 szNew[i+1] -= sz;
79170 }
79171 if( cntNew[i]>=b.nCell ){
79172 k = i+1;
79173 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
79174 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(79174);
79175 goto balance_cleanup;
79176 }
79177 }
79178
79179 /*
79180 ** The packing computed by the previous block is biased toward the siblings
79181 ** on the left side (siblings with smaller keys). The left siblings are
79182 ** always nearly full, while the right-most sibling might be nearly empty.
79183 ** The next block of code attempts to adjust the packing of siblings to
79184 ** get a better balance.
79185 **
79186 ** This adjustment is more than an optimization. The packing above might
79187 ** be so out of balance as to be illegal. For example, the right-most
79188 ** sibling might be completely empty. This adjustment is not optional.
79189 */
79190 for(i=k-1; i>0; i--){
79191 int szRight = szNew[i]; /* Size of sibling on the right */
79192 int szLeft = szNew[i-1]; /* Size of sibling on the left */
79193 int r; /* Index of right-most cell in left sibling */
79194 int d; /* Index of first cell to the left of right sibling */
79195
79196 r = cntNew[i-1] - 1;
79197 d = r + 1 - leafData;
79198 (void)cachedCellSize(&b, d);
79199 do{
79200 int szR, szD;
79201 assert( d<nMaxCells )((void) (0));
79202 assert( r<nMaxCells )((void) (0));
79203 szR = cachedCellSize(&b, r);
79204 szD = b.szCell[d];
79205 if( szRight!=0
79206 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
79207 break;
79208 }
79209 szRight += szD + 2;
79210 szLeft -= szR + 2;
79211 cntNew[i-1] = r;
79212 r--;
79213 d--;
79214 }while( r>=0 );
79215 szNew[i] = szRight;
79216 szNew[i-1] = szLeft;
79217 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
79218 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(79218);
79219 goto balance_cleanup;
79220 }
79221 }
79222
79223 /* Sanity check: For a non-corrupt database file one of the following
79224 ** must be true:
79225 ** (1) We found one or more cells (cntNew[0])>0), or
79226 ** (2) pPage is a virtual root page. A virtual root page is when
79227 ** the real root page is page 1 and we are the only child of
79228 ** that page.
79229 */
79230 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB)((void) (0));
79231 TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n",
79232 apOld[0]->pgno, apOld[0]->nCell,
79233 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
79234 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
79235 ));
79236
79237 /*
79238 ** Allocate k new pages. Reuse old pages where possible.
79239 */
79240 pageFlags = apOld[0]->aData[0];
79241 for(i=0; i<k; i++){
79242 MemPage *pNew;
79243 if( i<nOld ){
79244 pNew = apNew[i] = apOld[i];
79245 apOld[i] = 0;
79246 rc = sqlite3PagerWrite(pNew->pDbPage);
79247 nNew++;
79248 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
79249 && rc==SQLITE_OK0
79250 ){
79251 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(79251);
79252 }
79253 if( rc ) goto balance_cleanup;
79254 }else{
79255 assert( i>0 )((void) (0));
79256 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
79257 if( rc ) goto balance_cleanup;
79258 zeroPage(pNew, pageFlags);
79259 apNew[i] = pNew;
79260 nNew++;
79261 cntOld[i] = b.nCell;
79262
79263 /* Set the pointer-map entry for the new sibling page. */
79264 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
79265 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE5, pParent->pgno, &rc);
79266 if( rc!=SQLITE_OK0 ){
79267 goto balance_cleanup;
79268 }
79269 }
79270 }
79271 }
79272
79273 /*
79274 ** Reassign page numbers so that the new pages are in ascending order.
79275 ** This helps to keep entries in the disk file in order so that a scan
79276 ** of the table is closer to a linear scan through the file. That in turn
79277 ** helps the operating system to deliver pages from the disk more rapidly.
79278 **
79279 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
79280 ** (5), that is not a performance concern.
79281 **
79282 ** When NB==3, this one optimization makes the database about 25% faster
79283 ** for large insertions and deletions.
79284 */
79285 for(i=0; i<nNew; i++){
79286 aPgno[i] = apNew[i]->pgno;
79287 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE )((void) (0));
79288 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY )((void) (0));
79289 }
79290 for(i=0; i<nNew-1; i++){
79291 int iB = i;
79292 for(j=i+1; j<nNew; j++){
79293 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
79294 }
79295
79296 /* If apNew[i] has a page number that is bigger than any of the
79297 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
79298 ** entry that has the smallest page number (which we know to be
79299 ** entry apNew[iB]).
79300 */
79301 if( iB!=i ){
79302 Pgno pgnoA = apNew[i]->pgno;
79303 Pgno pgnoB = apNew[iB]->pgno;
79304 Pgno pgnoTemp = (PENDING_BYTEsqlite3PendingByte/pBt->pageSize)+1;
79305 u16 fgA = apNew[i]->pDbPage->flags;
79306 u16 fgB = apNew[iB]->pDbPage->flags;
79307 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
79308 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
79309 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
79310 apNew[i]->pgno = pgnoB;
79311 apNew[iB]->pgno = pgnoA;
79312 }
79313 }
79314
79315 TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) "
79316 "%u(%u nc=%u) %u(%u nc=%u)\n",
79317 apNew[0]->pgno, szNew[0], cntNew[0],
79318 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
79319 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
79320 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
79321 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
79322 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
79323 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
79324 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
79325 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
79326 ));
79327
79328 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
79329 assert( nNew>=1 && nNew<=ArraySize(apNew) )((void) (0));
79330 assert( apNew[nNew-1]!=0 )((void) (0));
79331 put4bytesqlite3Put4byte(pRight, apNew[nNew-1]->pgno);
79332
79333 /* If the sibling pages are not leaves, ensure that the right-child pointer
79334 ** of the right-most new sibling page is set to the value that was
79335 ** originally in the same field of the right-most old sibling page. */
79336 if( (pageFlags & PTF_LEAF0x08)==0 && nOld!=nNew ){
79337 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
79338 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
79339 }
79340
79341 /* Make any required updates to pointer map entries associated with
79342 ** cells stored on sibling pages following the balance operation. Pointer
79343 ** map entries associated with divider cells are set by the insertCell()
79344 ** routine. The associated pointer map entries are:
79345 **
79346 ** a) if the cell contains a reference to an overflow chain, the
79347 ** entry associated with the first page in the overflow chain, and
79348 **
79349 ** b) if the sibling pages are not leaves, the child page associated
79350 ** with the cell.
79351 **
79352 ** If the sibling pages are not leaves, then the pointer map entry
79353 ** associated with the right-child of each sibling may also need to be
79354 ** updated. This happens below, after the sibling pages have been
79355 ** populated, not here.
79356 */
79357 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
79358 MemPage *pOld;
79359 MemPage *pNew = pOld = apNew[0];
79360 int cntOldNext = pNew->nCell + pNew->nOverflow;
79361 int iNew = 0;
79362 int iOld = 0;
79363
79364 for(i=0; i<b.nCell; i++){
79365 u8 *pCell = b.apCell[i];
79366 while( i==cntOldNext ){
79367 iOld++;
79368 assert( iOld<nNew || iOld<nOld )((void) (0));
79369 assert( iOld>=0 && iOld<NB )((void) (0));
79370 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
79371 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
79372 }
79373 if( i==cntNew[iNew] ){
79374 pNew = apNew[++iNew];
79375 if( !leafData ) continue;
79376 }
79377
79378 /* Cell pCell is destined for new sibling page pNew. Originally, it
79379 ** was either part of sibling page iOld (possibly an overflow cell),
79380 ** or else the divider cell to the left of sibling page iOld. So,
79381 ** if sibling page iOld had the same page number as pNew, and if
79382 ** pCell really was a part of sibling page iOld (not a divider or
79383 ** overflow cell), we can skip updating the pointer map entries. */
79384 if( iOld>=nNew
79385 || pNew->pgno!=aPgno[iOld]
79386 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)(((uptr)(pCell)>=(uptr)(pOld->aData))&&((uptr)(
pCell)<(uptr)(pOld->aDataEnd)))
79387 ){
79388 if( !leafCorrection ){
79389 ptrmapPut(pBt, get4bytesqlite3Get4byte(pCell), PTRMAP_BTREE5, pNew->pgno, &rc);
79390 }
79391 if( cachedCellSize(&b,i)>pNew->minLocal ){
79392 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
79393 }
79394 if( rc ) goto balance_cleanup;
79395 }
79396 }
79397 }
79398
79399 /* Insert new divider cells into pParent. */
79400 for(i=0; i<nNew-1; i++){
79401 u8 *pCell;
79402 u8 *pTemp;
79403 int sz;
79404 u8 *pSrcEnd;
79405 MemPage *pNew = apNew[i];
79406 j = cntNew[i];
79407
79408 assert( j<nMaxCells )((void) (0));
79409 assert( b.apCell[j]!=0 )((void) (0));
79410 pCell = b.apCell[j];
79411 sz = b.szCell[j] + leafCorrection;
79412 pTemp = &aOvflSpace[iOvflSpace];
79413 if( !pNew->leaf ){
79414 memcpy(&pNew->aData[8], pCell, 4);
79415 }else if( leafData ){
79416 /* If the tree is a leaf-data tree, and the siblings are leaves,
79417 ** then there is no divider cell in b.apCell[]. Instead, the divider
79418 ** cell consists of the integer key for the right-most cell of
79419 ** the sibling-page assembled above only.
79420 */
79421 CellInfo info;
79422 j--;
79423 pNew->xParseCell(pNew, b.apCell[j], &info);
79424 pCell = pTemp;
79425 sz = 4 + putVarintsqlite3PutVarint(&pCell[4], info.nKey);
79426 pTemp = 0;
79427 }else{
79428 pCell -= 4;
79429 /* Obscure case for non-leaf-data trees: If the cell at pCell was
79430 ** previously stored on a leaf node, and its reported size was 4
79431 ** bytes, then it may actually be smaller than this
79432 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
79433 ** any cell). But it is important to pass the correct size to
79434 ** insertCell(), so reparse the cell now.
79435 **
79436 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
79437 ** and WITHOUT ROWID tables with exactly one column which is the
79438 ** primary key.
79439 */
79440 if( b.szCell[j]==4 ){
79441 assert(leafCorrection==4)((void) (0));
79442 sz = pParent->xCellSize(pParent, pCell);
79443 }
79444 }
79445 iOvflSpace += sz;
79446 assert( sz<=pBt->maxLocal+23 )((void) (0));
79447 assert( iOvflSpace <= (int)pBt->pageSize )((void) (0));
79448 assert( b.ixNx[NB*2-1]>j )((void) (0));
79449 for(k=0; b.ixNx[k]<=j; k++){}
79450 pSrcEnd = b.apEnd[k];
79451 if( SQLITE_OVERFLOW(pSrcEnd, pCell, pCell+sz)(((uptr)(pCell)<(uptr)(pSrcEnd))&&((uptr)(pCell+sz
)>(uptr)(pSrcEnd)))
){
79452 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(79452);
79453 goto balance_cleanup;
79454 }
79455 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
79456 if( rc!=SQLITE_OK0 ) goto balance_cleanup;
79457 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
79458 }
79459
79460 /* Now update the actual sibling pages. The order in which they are updated
79461 ** is important, as this code needs to avoid disrupting any page from which
79462 ** cells may still to be read. In practice, this means:
79463 **
79464 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
79465 ** then it is not safe to update page apNew[iPg] until after
79466 ** the left-hand sibling apNew[iPg-1] has been updated.
79467 **
79468 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
79469 ** then it is not safe to update page apNew[iPg] until after
79470 ** the right-hand sibling apNew[iPg+1] has been updated.
79471 **
79472 ** If neither of the above apply, the page is safe to update.
79473 **
79474 ** The iPg value in the following loop starts at nNew-1 goes down
79475 ** to 0, then back up to nNew-1 again, thus making two passes over
79476 ** the pages. On the initial downward pass, only condition (1) above
79477 ** needs to be tested because (2) will always be true from the previous
79478 ** step. On the upward pass, both conditions are always true, so the
79479 ** upwards pass simply processes pages that were missed on the downward
79480 ** pass.
79481 */
79482 for(i=1-nNew; i<nNew; i++){
79483 int iPg = i<0 ? -i : i;
79484 assert( iPg>=0 && iPg<nNew )((void) (0));
79485 assert( iPg>=1 || i>=0 )((void) (0));
79486 assert( iPg<ArraySize(cntOld) )((void) (0));
79487 if( abDone[iPg] ) continue; /* Skip pages already processed */
79488 if( i>=0 /* On the upwards pass, or... */
79489 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
79490 ){
79491 int iNew;
79492 int iOld;
79493 int nNewCell;
79494
79495 /* Verify condition (1): If cells are moving left, update iPg
79496 ** only after iPg-1 has already been updated. */
79497 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] )((void) (0));
79498
79499 /* Verify condition (2): If cells are moving right, update iPg
79500 ** only after iPg+1 has already been updated. */
79501 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] )((void) (0));
79502
79503 if( iPg==0 ){
79504 iNew = iOld = 0;
79505 nNewCell = cntNew[0];
79506 }else{
79507 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
79508 iNew = cntNew[iPg-1] + !leafData;
79509 nNewCell = cntNew[iPg] - iNew;
79510 }
79511
79512 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
79513 if( rc ) goto balance_cleanup;
79514 abDone[iPg]++;
79515 apNew[iPg]->nFree = usableSpace-szNew[iPg];
79516 assert( apNew[iPg]->nOverflow==0 )((void) (0));
79517 assert( apNew[iPg]->nCell==nNewCell )((void) (0));
79518 }
79519 }
79520
79521 /* All pages have been processed exactly once */
79522 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 )((void) (0));
79523
79524 assert( nOld>0 )((void) (0));
79525 assert( nNew>0 )((void) (0));
79526
79527 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
79528 /* The root page of the b-tree now contains no cells. The only sibling
79529 ** page is the right-child of the parent. Copy the contents of the
79530 ** child page into the parent, decreasing the overall height of the
79531 ** b-tree structure by one. This is described as the "balance-shallower"
79532 ** sub-algorithm in some documentation.
79533 **
79534 ** If this is an auto-vacuum database, the call to copyNodeContent()
79535 ** sets all pointer-map entries corresponding to database image pages
79536 ** for which the pointer is stored within the content being copied.
79537 **
79538 ** It is critical that the child page be defragmented before being
79539 ** copied into the parent, because if the parent is page 1 then it will
79540 ** by smaller than the child due to the database header, and so all the
79541 ** free space needs to be up front.
79542 */
79543 assert( nNew==1 || CORRUPT_DB )((void) (0));
79544 rc = defragmentPage(apNew[0], -1);
79545 testcase( rc!=SQLITE_OK );
79546 assert( apNew[0]->nFree ==((void) (0))
79547 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset((void) (0))
79548 - apNew[0]->nCell*2)((void) (0))
79549 || rc!=SQLITE_OK((void) (0))
79550 )((void) (0));
79551 copyNodeContent(apNew[0], pParent, &rc);
79552 freePage(apNew[0], &rc);
79553 }else if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) && !leafCorrection ){
79554 /* Fix the pointer map entries associated with the right-child of each
79555 ** sibling page. All other pointer map entries have already been taken
79556 ** care of. */
79557 for(i=0; i<nNew; i++){
79558 u32 key = get4bytesqlite3Get4byte(&apNew[i]->aData[8]);
79559 ptrmapPut(pBt, key, PTRMAP_BTREE5, apNew[i]->pgno, &rc);
79560 }
79561 }
79562
79563 assert( pParent->isInit )((void) (0));
79564 TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n",
79565 nOld, nNew, b.nCell));
79566
79567 /* Free any old pages that were not reused as new pages.
79568 */
79569 for(i=nNew; i<nOld; i++){
79570 freePage(apOld[i], &rc);
79571 }
79572
79573#if 0
79574 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) && rc==SQLITE_OK0 && apNew[0]->isInit ){
79575 /* The ptrmapCheckPages() contains assert() statements that verify that
79576 ** all pointer map pages are set correctly. This is helpful while
79577 ** debugging. This is usually disabled because a corrupt database may
79578 ** cause an assert() statement to fail. */
79579 ptrmapCheckPages(apNew, nNew);
79580 ptrmapCheckPages(&pParent, 1);
79581 }
79582#endif
79583
79584 /*
79585 ** Cleanup before returning.
79586 */
79587balance_cleanup:
79588 sqlite3StackFree(0, b.apCell)sqlite3DbFree(0,b.apCell);
79589 for(i=0; i<nOld; i++){
79590 releasePage(apOld[i]);
79591 }
79592 for(i=0; i<nNew; i++){
79593 releasePage(apNew[i]);
79594 }
79595
79596 return rc;
79597}
79598
79599
79600/*
79601** This function is called when the root page of a b-tree structure is
79602** overfull (has one or more overflow pages).
79603**
79604** A new child page is allocated and the contents of the current root
79605** page, including overflow cells, are copied into the child. The root
79606** page is then overwritten to make it an empty page with the right-child
79607** pointer pointing to the new page.
79608**
79609** Before returning, all pointer-map entries corresponding to pages
79610** that the new child-page now contains pointers to are updated. The
79611** entry corresponding to the new right-child pointer of the root
79612** page is also updated.
79613**
79614** If successful, *ppChild is set to contain a reference to the child
79615** page and SQLITE_OK is returned. In this case the caller is required
79616** to call releasePage() on *ppChild exactly once. If an error occurs,
79617** an error code is returned and *ppChild is set to 0.
79618*/
79619static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
79620 int rc; /* Return value from subprocedures */
79621 MemPage *pChild = 0; /* Pointer to a new child page */
79622 Pgno pgnoChild = 0; /* Page number of the new child page */
79623 BtShared *pBt = pRoot->pBt; /* The BTree */
79624
79625 assert( pRoot->nOverflow>0 )((void) (0));
79626 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
79627
79628 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
79629 ** page that will become the new right-child of pPage. Copy the contents
79630 ** of the node stored on pRoot into the new child page.
79631 */
79632 rc = sqlite3PagerWrite(pRoot->pDbPage);
79633 if( rc==SQLITE_OK0 ){
79634 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
79635 copyNodeContent(pRoot, pChild, &rc);
79636 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) ){
79637 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE5, pRoot->pgno, &rc);
79638 }
79639 }
79640 if( rc ){
79641 *ppChild = 0;
79642 releasePage(pChild);
79643 return rc;
79644 }
79645 assert( sqlite3PagerIswriteable(pChild->pDbPage) )((void) (0));
79646 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
79647 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB )((void) (0));
79648
79649 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
79650
79651 /* Copy the overflow cells from pRoot to pChild */
79652 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
79653 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
79654 memcpy(pChild->apOvfl, pRoot->apOvfl,
79655 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
79656 pChild->nOverflow = pRoot->nOverflow;
79657
79658 /* Zero the contents of pRoot. Then install pChild as the right-child. */
79659 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF0x08);
79660 put4bytesqlite3Put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
79661
79662 *ppChild = pChild;
79663 return SQLITE_OK0;
79664}
79665
79666/*
79667** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
79668** on the same B-tree as pCur.
79669**
79670** This can occur if a database is corrupt with two or more SQL tables
79671** pointing to the same b-tree. If an insert occurs on one SQL table
79672** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
79673** table linked to the same b-tree. If the secondary insert causes a
79674** rebalance, that can change content out from under the cursor on the
79675** first SQL table, violating invariants on the first insert.
79676*/
79677static int anotherValidCursor(BtCursor *pCur){
79678 BtCursor *pOther;
79679 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
79680 if( pOther!=pCur
79681 && pOther->eState==CURSOR_VALID0
79682 && pOther->pPage==pCur->pPage
79683 ){
79684 return SQLITE_CORRUPT_PAGE(pCur->pPage)sqlite3CorruptError(79684);
79685 }
79686 }
79687 return SQLITE_OK0;
79688}
79689
79690/*
79691** The page that pCur currently points to has just been modified in
79692** some way. This function figures out if this modification means the
79693** tree needs to be balanced, and if so calls the appropriate balancing
79694** routine. Balancing routines are:
79695**
79696** balance_quick()
79697** balance_deeper()
79698** balance_nonroot()
79699*/
79700static int balance(BtCursor *pCur){
79701 int rc = SQLITE_OK0;
79702 u8 aBalanceQuickSpace[13];
79703 u8 *pFree = 0;
79704
79705 VVA_ONLY( int balance_quick_called = 0 );
79706 VVA_ONLY( int balance_deeper_called = 0 );
79707
79708 do {
79709 int iPage;
79710 MemPage *pPage = pCur->pPage;
79711
79712 if( NEVER(pPage->nFree<0)(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
79713 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
79714 /* No rebalance required as long as:
79715 ** (1) There are no overflow cells
79716 ** (2) The amount of free space on the page is less than 2/3rds of
79717 ** the total usable space on the page. */
79718 break;
79719 }else if( (iPage = pCur->iPage)==0 ){
79720 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK0 ){
79721 /* The root page of the b-tree is overfull. In this case call the
79722 ** balance_deeper() function to create a new child for the root-page
79723 ** and copy the current contents of the root-page to it. The
79724 ** next iteration of the do-loop will balance the child page.
79725 */
79726 assert( balance_deeper_called==0 )((void) (0));
79727 VVA_ONLY( balance_deeper_called++ );
79728 rc = balance_deeper(pPage, &pCur->apPage[1]);
79729 if( rc==SQLITE_OK0 ){
79730 pCur->iPage = 1;
79731 pCur->ix = 0;
79732 pCur->aiIdx[0] = 0;
79733 pCur->apPage[0] = pPage;
79734 pCur->pPage = pCur->apPage[1];
79735 assert( pCur->pPage->nOverflow )((void) (0));
79736 }
79737 }else{
79738 break;
79739 }
79740 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
79741 /* The page being written is not a root page, and there is currently
79742 ** more than one reference to it. This only happens if the page is one
79743 ** of its own ancestor pages. Corruption. */
79744 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(79744);
79745 }else{
79746 MemPage * const pParent = pCur->apPage[iPage-1];
79747 int const iIdx = pCur->aiIdx[iPage-1];
79748
79749 rc = sqlite3PagerWrite(pParent->pDbPage);
79750 if( rc==SQLITE_OK0 && pParent->nFree<0 ){
79751 rc = btreeComputeFreeSpace(pParent);
79752 }
79753 if( rc==SQLITE_OK0 ){
79754#ifndef SQLITE_OMIT_QUICKBALANCE
79755 if( pPage->intKeyLeaf
79756 && pPage->nOverflow==1
79757 && pPage->aiOvfl[0]==pPage->nCell
79758 && pParent->pgno!=1
79759 && pParent->nCell==iIdx
79760 ){
79761 /* Call balance_quick() to create a new sibling of pPage on which
79762 ** to store the overflow cell. balance_quick() inserts a new cell
79763 ** into pParent, which may cause pParent overflow. If this
79764 ** happens, the next iteration of the do-loop will balance pParent
79765 ** use either balance_nonroot() or balance_deeper(). Until this
79766 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
79767 ** buffer.
79768 **
79769 ** The purpose of the following assert() is to check that only a
79770 ** single call to balance_quick() is made for each call to this
79771 ** function. If this were not verified, a subtle bug involving reuse
79772 ** of the aBalanceQuickSpace[] might sneak in.
79773 */
79774 assert( balance_quick_called==0 )((void) (0));
79775 VVA_ONLY( balance_quick_called++ );
79776 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
79777 }else
79778#endif
79779 {
79780 /* In this case, call balance_nonroot() to redistribute cells
79781 ** between pPage and up to 2 of its sibling pages. This involves
79782 ** modifying the contents of pParent, which may cause pParent to
79783 ** become overfull or underfull. The next iteration of the do-loop
79784 ** will balance the parent page to correct this.
79785 **
79786 ** If the parent page becomes overfull, the overflow cell or cells
79787 ** are stored in the pSpace buffer allocated immediately below.
79788 ** A subsequent iteration of the do-loop will deal with this by
79789 ** calling balance_nonroot() (balance_deeper() may be called first,
79790 ** but it doesn't deal with overflow cells - just moves them to a
79791 ** different page). Once this subsequent call to balance_nonroot()
79792 ** has completed, it is safe to release the pSpace buffer used by
79793 ** the previous call, as the overflow cell data will have been
79794 ** copied either into the body of a database page or into the new
79795 ** pSpace buffer passed to the latter call to balance_nonroot().
79796 */
79797 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
79798 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
79799 pCur->hints&BTREE_BULKLOAD0x00000001);
79800 if( pFree ){
79801 /* If pFree is not NULL, it points to the pSpace buffer used
79802 ** by a previous call to balance_nonroot(). Its contents are
79803 ** now stored either on real database pages or within the
79804 ** new pSpace buffer, so it may be safely freed here. */
79805 sqlite3PageFree(pFree);
79806 }
79807
79808 /* The pSpace buffer will be freed after the next call to
79809 ** balance_nonroot(), or just before this function returns, whichever
79810 ** comes first. */
79811 pFree = pSpace;
79812 }
79813 }
79814
79815 pPage->nOverflow = 0;
79816
79817 /* The next iteration of the do-loop balances the parent page. */
79818 releasePage(pPage);
79819 pCur->iPage--;
79820 assert( pCur->iPage>=0 )((void) (0));
79821 pCur->pPage = pCur->apPage[pCur->iPage];
79822 }
79823 }while( rc==SQLITE_OK0 );
79824
79825 if( pFree ){
79826 sqlite3PageFree(pFree);
79827 }
79828 return rc;
79829}
79830
79831/* Overwrite content from pX into pDest. Only do the write if the
79832** content is different from what is already there.
79833*/
79834static int btreeOverwriteContent(
79835 MemPage *pPage, /* MemPage on which writing will occur */
79836 u8 *pDest, /* Pointer to the place to start writing */
79837 const BtreePayload *pX, /* Source of data to write */
79838 int iOffset, /* Offset of first byte to write */
79839 int iAmt /* Number of bytes to be written */
79840){
79841 int nData = pX->nData - iOffset;
79842 if( nData<=0 ){
79843 /* Overwriting with zeros */
79844 int i;
79845 for(i=0; i<iAmt && pDest[i]==0; i++){}
79846 if( i<iAmt ){
79847 int rc = sqlite3PagerWrite(pPage->pDbPage);
79848 if( rc ) return rc;
79849 memset(pDest + i, 0, iAmt - i);
79850 }
79851 }else{
79852 if( nData<iAmt ){
79853 /* Mixed read data and zeros at the end. Make a recursive call
79854 ** to write the zeros then fall through to write the real data */
79855 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
79856 iAmt-nData);
79857 if( rc ) return rc;
79858 iAmt = nData;
79859 }
79860 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
79861 int rc = sqlite3PagerWrite(pPage->pDbPage);
79862 if( rc ) return rc;
79863 /* In a corrupt database, it is possible for the source and destination
79864 ** buffers to overlap. This is harmless since the database is already
79865 ** corrupt but it does cause valgrind and ASAN warnings. So use
79866 ** memmove(). */
79867 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
79868 }
79869 }
79870 return SQLITE_OK0;
79871}
79872
79873/*
79874** Overwrite the cell that cursor pCur is pointing to with fresh content
79875** contained in pX. In this variant, pCur is pointing to an overflow
79876** cell.
79877*/
79878static SQLITE_NOINLINE__attribute__((noinline)) int btreeOverwriteOverflowCell(
79879 BtCursor *pCur, /* Cursor pointing to cell to overwrite */
79880 const BtreePayload *pX /* Content to write into the cell */
79881){
79882 int iOffset; /* Next byte of pX->pData to write */
79883 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79884 int rc; /* Return code */
79885 MemPage *pPage = pCur->pPage; /* Page being written */
79886 BtShared *pBt; /* Btree */
79887 Pgno ovflPgno; /* Next overflow page to write */
79888 u32 ovflPageSize; /* Size to write on overflow page */
79889
79890 assert( pCur->info.nLocal<nTotal )((void) (0)); /* pCur is an overflow cell */
79891
79892 /* Overwrite the local portion first */
79893 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79894 0, pCur->info.nLocal);
79895 if( rc ) return rc;
79896
79897 /* Now overwrite the overflow pages */
79898 iOffset = pCur->info.nLocal;
79899 assert( nTotal>=0 )((void) (0));
79900 assert( iOffset>=0 )((void) (0));
79901 ovflPgno = get4bytesqlite3Get4byte(pCur->info.pPayload + iOffset);
79902 pBt = pPage->pBt;
79903 ovflPageSize = pBt->usableSize - 4;
79904 do{
79905 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
79906 if( rc ) return rc;
79907 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
79908 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(79908);
79909 }else{
79910 if( iOffset+ovflPageSize<(u32)nTotal ){
79911 ovflPgno = get4bytesqlite3Get4byte(pPage->aData);
79912 }else{
79913 ovflPageSize = nTotal - iOffset;
79914 }
79915 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
79916 iOffset, ovflPageSize);
79917 }
79918 sqlite3PagerUnref(pPage->pDbPage);
79919 if( rc ) return rc;
79920 iOffset += ovflPageSize;
79921 }while( iOffset<nTotal );
79922 return SQLITE_OK0;
79923}
79924
79925/*
79926** Overwrite the cell that cursor pCur is pointing to with fresh content
79927** contained in pX.
79928*/
79929static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
79930 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79931 MemPage *pPage = pCur->pPage; /* Page being written */
79932
79933 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
79934 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
79935 ){
79936 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(79936);
79937 }
79938 if( pCur->info.nLocal==nTotal ){
79939 /* The entire cell is local */
79940 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79941 0, pCur->info.nLocal);
79942 }else{
79943 /* The cell contains overflow content */
79944 return btreeOverwriteOverflowCell(pCur, pX);
79945 }
79946}
79947
79948
79949/*
79950** Insert a new record into the BTree. The content of the new record
79951** is described by the pX object. The pCur cursor is used only to
79952** define what table the record should be inserted into, and is left
79953** pointing at a random location.
79954**
79955** For a table btree (used for rowid tables), only the pX.nKey value of
79956** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
79957** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
79958** hold the content of the row.
79959**
79960** For an index btree (used for indexes and WITHOUT ROWID tables), the
79961** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
79962** pX.pData,nData,nZero fields must be zero.
79963**
79964** If the seekResult parameter is non-zero, then a successful call to
79965** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
79966** been performed. In other words, if seekResult!=0 then the cursor
79967** is currently pointing to a cell that will be adjacent to the cell
79968** to be inserted. If seekResult<0 then pCur points to a cell that is
79969** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
79970** that is larger than (pKey,nKey).
79971**
79972** If seekResult==0, that means pCur is pointing at some unknown location.
79973** In that case, this routine must seek the cursor to the correct insertion
79974** point for (pKey,nKey) before doing the insertion. For index btrees,
79975** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
79976** key values and pX->aMem can be used instead of pX->pKey to avoid having
79977** to decode the key.
79978*/
79979SQLITE_PRIVATEstatic int sqlite3BtreeInsert(
79980 BtCursor *pCur, /* Insert data into the table of this cursor */
79981 const BtreePayload *pX, /* Content of the row to be inserted */
79982 int flags, /* True if this is likely an append */
79983 int seekResult /* Result of prior IndexMoveto() call */
79984){
79985 int rc;
79986 int loc = seekResult; /* -1: before desired location +1: after */
79987 int szNew = 0;
79988 int idx;
79989 MemPage *pPage;
79990 Btree *p = pCur->pBtree;
79991 unsigned char *oldCell;
79992 unsigned char *newCell = 0;
79993
79994 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags )((void) (0));
79995 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 )((void) (0));
79996
79997 /* Save the positions of any other cursors open on this table.
79998 **
79999 ** In some cases, the call to btreeMoveto() below is a no-op. For
80000 ** example, when inserting data into a table with auto-generated integer
80001 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
80002 ** integer key to use. It then calls this function to actually insert the
80003 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
80004 ** that the cursor is already where it needs to be and returns without
80005 ** doing any work. To avoid thwarting these optimizations, it is important
80006 ** not to clear the cursor here.
80007 */
80008 if( pCur->curFlags & BTCF_Multiple0x20 ){
19
Assuming the condition is false
20
Taking false branch
80009 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
80010 if( rc ) return rc;
80011 if( loc && pCur->iPage<0 ){
80012 /* This can only happen if the schema is corrupt such that there is more
80013 ** than one table or index with the same root page as used by the cursor.
80014 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
80015 ** the schema was loaded. This cannot be asserted though, as a user might
80016 ** set the flag, load the schema, and then unset the flag. */
80017 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot)sqlite3CorruptError(80017);
80018 }
80019 }
80020
80021 /* Ensure that the cursor is not in the CURSOR_FAULT state and that it
80022 ** points to a valid cell.
80023 */
80024 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
21
Assuming field 'eState' is < CURSOR_REQUIRESEEK
22
Taking false branch
80025 testcase( pCur->eState==CURSOR_REQUIRESEEK );
80026 testcase( pCur->eState==CURSOR_FAULT );
80027 rc = moveToRoot(pCur);
80028 if( rc && rc!=SQLITE_EMPTY16 ) return rc;
80029 }
80030
80031 assert( cursorOwnsBtShared(pCur) )((void) (0));
80032 assert( (pCur->curFlags & BTCF_WriteFlag)!=0((void) (0))
80033 && p->pBt->inTransaction==TRANS_WRITE((void) (0))
80034 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
80035 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
80036
80037 /* Assert that the caller has been consistent. If this cursor was opened
80038 ** expecting an index b-tree, then the caller should be inserting blob
80039 ** keys with no associated data. If the cursor was opened expecting an
80040 ** intkey table, the caller should be inserting integer keys with a
80041 ** blob of associated data. */
80042 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) )((void) (0));
80043
80044 if( pCur->pKeyInfo==0 ){
23
Assuming field 'pKeyInfo' is equal to null
24
Taking true branch
80045 assert( pX->pKey==0 )((void) (0));
80046 /* If this is an insert into a table b-tree, invalidate any incrblob
80047 ** cursors open on the row being replaced */
80048 if( p->hasIncrblobCur ){
25
Assuming field 'hasIncrblobCur' is 0
80049 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
80050 }
80051
80052 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
80053 ** to a row with the same key as the new entry being inserted.
80054 */
80055#ifdef SQLITE_DEBUG
80056 if( flags & BTREE_SAVEPOSITION0x02 ){
80057 assert( pCur->curFlags & BTCF_ValidNKey )((void) (0));
80058 assert( pX->nKey==pCur->info.nKey )((void) (0));
80059 assert( loc==0 )((void) (0));
80060 }
80061#endif
80062
80063 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
80064 ** that the cursor is not pointing to a row to be overwritten.
80065 ** So do a complete check.
80066 */
80067 if( (pCur->curFlags&BTCF_ValidNKey0x02)!=0 && pX->nKey==pCur->info.nKey ){
26
Assuming the condition is true
27
Assuming 'pX->nKey' is equal to 'pCur->info.nKey'
28
Taking true branch
80068 /* The cursor is pointing to the entry that is to be
80069 ** overwritten */
80070 assert( pX->nData>=0 && pX->nZero>=0 )((void) (0));
80071 if( pCur->info.nSize!=0
29
Assuming field 'nSize' is not equal to 0
80072 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
30
The left operand of '+' is a garbage value
80073 ){
80074 /* New entry is the same size as the old. Do an overwrite */
80075 return btreeOverwriteCell(pCur, pX);
80076 }
80077 assert( loc==0 )((void) (0));
80078 }else if( loc==0 ){
80079 /* The cursor is *not* pointing to the cell to be overwritten, nor
80080 ** to an adjacent cell. Move the cursor so that it is pointing either
80081 ** to the cell to be overwritten or an adjacent cell.
80082 */
80083 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
80084 (flags & BTREE_APPEND0x08)!=0, &loc);
80085 if( rc ) return rc;
80086 }
80087 }else{
80088 /* This is an index or a WITHOUT ROWID table */
80089
80090 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
80091 ** to a row with the same key as the new entry being inserted.
80092 */
80093 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 )((void) (0));
80094
80095 /* If the cursor is not already pointing either to the cell to be
80096 ** overwritten, or if a new cell is being inserted, if the cursor is
80097 ** not pointing to an immediately adjacent cell, then move the cursor
80098 ** so that it does.
80099 */
80100 if( loc==0 && (flags & BTREE_SAVEPOSITION0x02)==0 ){
80101 if( pX->nMem ){
80102 UnpackedRecord r;
80103 r.pKeyInfo = pCur->pKeyInfo;
80104 r.aMem = pX->aMem;
80105 r.nField = pX->nMem;
80106 r.default_rc = 0;
80107 r.eqSeen = 0;
80108 rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc);
80109 }else{
80110 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
80111 (flags & BTREE_APPEND0x08)!=0, &loc);
80112 }
80113 if( rc ) return rc;
80114 }
80115
80116 /* If the cursor is currently pointing to an entry to be overwritten
80117 ** and the new content is the same as as the old, then use the
80118 ** overwrite optimization.
80119 */
80120 if( loc==0 ){
80121 getCellInfo(pCur);
80122 if( pCur->info.nKey==pX->nKey ){
80123 BtreePayload x2;
80124 x2.pData = pX->pKey;
80125 x2.nData = pX->nKey;
80126 x2.nZero = 0;
80127 return btreeOverwriteCell(pCur, &x2);
80128 }
80129 }
80130 }
80131 assert( pCur->eState==CURSOR_VALID((void) (0))
80132 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB )((void) (0));
80133
80134 pPage = pCur->pPage;
80135 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) )((void) (0));
80136 assert( pPage->leaf || !pPage->intKey )((void) (0));
80137 if( pPage->nFree<0 ){
80138 if( NEVER(pCur->eState>CURSOR_INVALID)(pCur->eState>1) ){
80139 /* ^^^^^--- due to the moveToRoot() call above */
80140 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80140);
80141 }else{
80142 rc = btreeComputeFreeSpace(pPage);
80143 }
80144 if( rc ) return rc;
80145 }
80146
80147 TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n",
80148 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
80149 loc==0 ? "overwrite" : "new entry"));
80150 assert( pPage->isInit || CORRUPT_DB )((void) (0));
80151 newCell = p->pBt->pTmpSpace;
80152 assert( newCell!=0 )((void) (0));
80153 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT )((void) (0));
80154 if( flags & BTREE_PREFORMAT0x80 ){
80155 rc = SQLITE_OK0;
80156 szNew = p->pBt->nPreformatSize;
80157 if( szNew<4 ){
80158 szNew = 4;
80159 newCell[3] = 0;
80160 }
80161 if( ISAUTOVACUUM(p->pBt)(p->pBt->autoVacuum) && szNew>pPage->maxLocal ){
80162 CellInfo info;
80163 pPage->xParseCell(pPage, newCell, &info);
80164 if( info.nPayload!=info.nLocal ){
80165 Pgno ovfl = get4bytesqlite3Get4byte(&newCell[szNew-4]);
80166 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, &rc);
80167 if( NEVER(rc)(rc) ) goto end_insert;
80168 }
80169 }
80170 }else{
80171 rc = fillInCell(pPage, newCell, pX, &szNew);
80172 if( rc ) goto end_insert;
80173 }
80174 assert( szNew==pPage->xCellSize(pPage, newCell) )((void) (0));
80175 assert( szNew <= MX_CELL_SIZE(p->pBt) )((void) (0));
80176 idx = pCur->ix;
80177 pCur->info.nSize = 0;
80178 if( loc==0 ){
80179 CellInfo info;
80180 assert( idx>=0 )((void) (0));
80181 if( idx>=pPage->nCell ){
80182 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80182);
80183 }
80184 rc = sqlite3PagerWrite(pPage->pDbPage);
80185 if( rc ){
80186 goto end_insert;
80187 }
80188 oldCell = findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
;
80189 if( !pPage->leaf ){
80190 memcpy(newCell, oldCell, 4);
80191 }
80192 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; }
;
80193 testcase( pCur->curFlags & BTCF_ValidOvfl );
80194 invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04);
80195 if( info.nSize==szNew && info.nLocal==info.nPayload
80196 && (!ISAUTOVACUUM(p->pBt)(p->pBt->autoVacuum) || szNew<pPage->minLocal)
80197 ){
80198 /* Overwrite the old cell with the new if they are the same size.
80199 ** We could also try to do this if the old cell is smaller, then add
80200 ** the leftover space to the free list. But experiments show that
80201 ** doing that is no faster then skipping this optimization and just
80202 ** calling dropCell() and insertCell().
80203 **
80204 ** This optimization cannot be used on an autovacuum database if the
80205 ** new entry uses overflow pages, as the insertCell() call below is
80206 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
80207 assert( rc==SQLITE_OK )((void) (0)); /* clearCell never fails when nLocal==nPayload */
80208 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
80209 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80209);
80210 }
80211 if( oldCell+szNew > pPage->aDataEnd ){
80212 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80212);
80213 }
80214 memcpy(oldCell, newCell, szNew);
80215 return SQLITE_OK0;
80216 }
80217 dropCell(pPage, idx, info.nSize, &rc);
80218 if( rc ) goto end_insert;
80219 }else if( loc<0 && pPage->nCell>0 ){
80220 assert( pPage->leaf )((void) (0));
80221 idx = ++pCur->ix;
80222 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
80223 }else{
80224 assert( pPage->leaf )((void) (0));
80225 }
80226 rc = insertCellFast(pPage, idx, newCell, szNew);
80227 assert( pPage->nOverflow==0 || rc==SQLITE_OK )((void) (0));
80228 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 )((void) (0));
80229
80230 /* If no error has occurred and pPage has an overflow cell, call balance()
80231 ** to redistribute the cells within the tree. Since balance() may move
80232 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
80233 ** variables.
80234 **
80235 ** Previous versions of SQLite called moveToRoot() to move the cursor
80236 ** back to the root page as balance() used to invalidate the contents
80237 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
80238 ** set the cursor state to "invalid". This makes common insert operations
80239 ** slightly faster.
80240 **
80241 ** There is a subtle but important optimization here too. When inserting
80242 ** multiple records into an intkey b-tree using a single cursor (as can
80243 ** happen while processing an "INSERT INTO ... SELECT" statement), it
80244 ** is advantageous to leave the cursor pointing to the last entry in
80245 ** the b-tree if possible. If the cursor is left pointing to the last
80246 ** entry in the table, and the next row inserted has an integer key
80247 ** larger than the largest existing key, it is possible to insert the
80248 ** row without seeking the cursor. This can be a big performance boost.
80249 */
80250 if( pPage->nOverflow ){
80251 assert( rc==SQLITE_OK )((void) (0));
80252 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
80253 rc = balance(pCur);
80254
80255 /* Must make sure nOverflow is reset to zero even if the balance()
80256 ** fails. Internal data structure corruption will result otherwise.
80257 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
80258 ** from trying to save the current position of the cursor. */
80259 pCur->pPage->nOverflow = 0;
80260 pCur->eState = CURSOR_INVALID1;
80261 if( (flags & BTREE_SAVEPOSITION0x02) && rc==SQLITE_OK0 ){
80262 btreeReleaseAllCursorPages(pCur);
80263 if( pCur->pKeyInfo ){
80264 assert( pCur->pKey==0 )((void) (0));
80265 pCur->pKey = sqlite3Malloc( pX->nKey );
80266 if( pCur->pKey==0 ){
80267 rc = SQLITE_NOMEM7;
80268 }else{
80269 memcpy(pCur->pKey, pX->pKey, pX->nKey);
80270 }
80271 }
80272 pCur->eState = CURSOR_REQUIRESEEK3;
80273 pCur->nKey = pX->nKey;
80274 }
80275 }
80276 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 )((void) (0));
80277
80278end_insert:
80279 return rc;
80280}
80281
80282/*
80283** This function is used as part of copying the current row from cursor
80284** pSrc into cursor pDest. If the cursors are open on intkey tables, then
80285** parameter iKey is used as the rowid value when the record is copied
80286** into pDest. Otherwise, the record is copied verbatim.
80287**
80288** This function does not actually write the new value to cursor pDest.
80289** Instead, it creates and populates any required overflow pages and
80290** writes the data for the new cell into the BtShared.pTmpSpace buffer
80291** for the destination database. The size of the cell, in bytes, is left
80292** in BtShared.nPreformatSize. The caller completes the insertion by
80293** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
80294**
80295** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
80296*/
80297SQLITE_PRIVATEstatic int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
80298 BtShared *pBt = pDest->pBt;
80299 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
80300 const u8 *aIn; /* Pointer to next input buffer */
80301 u32 nIn; /* Size of input buffer aIn[] */
80302 u32 nRem; /* Bytes of data still to copy */
80303
80304 getCellInfo(pSrc);
80305 if( pSrc->info.nPayload<0x80 ){
80306 *(aOut++) = pSrc->info.nPayload;
80307 }else{
80308 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
80309 }
80310 if( pDest->pKeyInfo==0 ) aOut += putVarintsqlite3PutVarint(aOut, iKey);
80311 nIn = pSrc->info.nLocal;
80312 aIn = pSrc->info.pPayload;
80313 if( aIn+nIn>pSrc->pPage->aDataEnd ){
80314 return SQLITE_CORRUPT_PAGE(pSrc->pPage)sqlite3CorruptError(80314);
80315 }
80316 nRem = pSrc->info.nPayload;
80317 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
80318 memcpy(aOut, aIn, nIn);
80319 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
80320 return SQLITE_OK0;
80321 }else{
80322 int rc = SQLITE_OK0;
80323 Pager *pSrcPager = pSrc->pBt->pPager;
80324 u8 *pPgnoOut = 0;
80325 Pgno ovflIn = 0;
80326 DbPage *pPageIn = 0;
80327 MemPage *pPageOut = 0;
80328 u32 nOut; /* Size of output buffer aOut[] */
80329
80330 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80331 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
80332 if( nOut<pSrc->info.nPayload ){
80333 pPgnoOut = &aOut[nOut];
80334 pBt->nPreformatSize += 4;
80335 }
80336
80337 if( nRem>nIn ){
80338 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
80339 return SQLITE_CORRUPT_PAGE(pSrc->pPage)sqlite3CorruptError(80339);
80340 }
80341 ovflIn = get4bytesqlite3Get4byte(&pSrc->info.pPayload[nIn]);
80342 }
80343
80344 do {
80345 nRem -= nOut;
80346 do{
80347 assert( nOut>0 )((void) (0));
80348 if( nIn>0 ){
80349 int nCopy = MIN(nOut, nIn)((nOut)<(nIn)?(nOut):(nIn));
80350 memcpy(aOut, aIn, nCopy);
80351 nOut -= nCopy;
80352 nIn -= nCopy;
80353 aOut += nCopy;
80354 aIn += nCopy;
80355 }
80356 if( nOut>0 ){
80357 sqlite3PagerUnref(pPageIn);
80358 pPageIn = 0;
80359 rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY0x02);
80360 if( rc==SQLITE_OK0 ){
80361 aIn = (const u8*)sqlite3PagerGetData(pPageIn);
80362 ovflIn = get4bytesqlite3Get4byte(aIn);
80363 aIn += 4;
80364 nIn = pSrc->pBt->usableSize - 4;
80365 }
80366 }
80367 }while( rc==SQLITE_OK0 && nOut>0 );
80368
80369 if( rc==SQLITE_OK0 && nRem>0 && ALWAYS(pPgnoOut)(pPgnoOut) ){
80370 Pgno pgnoNew;
80371 MemPage *pNew = 0;
80372 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
80373 put4bytesqlite3Put4byte(pPgnoOut, pgnoNew);
80374 if( ISAUTOVACUUM(pBt)(pBt->autoVacuum) && pPageOut ){
80375 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW24, pPageOut->pgno, &rc);
80376 }
80377 releasePage(pPageOut);
80378 pPageOut = pNew;
80379 if( pPageOut ){
80380 pPgnoOut = pPageOut->aData;
80381 put4bytesqlite3Put4byte(pPgnoOut, 0);
80382 aOut = &pPgnoOut[4];
80383 nOut = MIN(pBt->usableSize - 4, nRem)((pBt->usableSize - 4)<(nRem)?(pBt->usableSize - 4):
(nRem))
;
80384 }
80385 }
80386 }while( nRem>0 && rc==SQLITE_OK0 );
80387
80388 releasePage(pPageOut);
80389 sqlite3PagerUnref(pPageIn);
80390 return rc;
80391 }
80392}
80393
80394/*
80395** Delete the entry that the cursor is pointing to.
80396**
80397** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
80398** the cursor is left pointing at an arbitrary location after the delete.
80399** But if that bit is set, then the cursor is left in a state such that
80400** the next call to BtreeNext() or BtreePrev() moves it to the same row
80401** as it would have been on if the call to BtreeDelete() had been omitted.
80402**
80403** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
80404** associated with a single table entry and its indexes. Only one of those
80405** deletes is considered the "primary" delete. The primary delete occurs
80406** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
80407** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
80408** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
80409** but which might be used by alternative storage engines.
80410*/
80411SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
80412 Btree *p = pCur->pBtree;
80413 BtShared *pBt = p->pBt;
80414 int rc; /* Return code */
80415 MemPage *pPage; /* Page to delete cell from */
80416 unsigned char *pCell; /* Pointer to cell to delete */
80417 int iCellIdx; /* Index of cell to delete */
80418 int iCellDepth; /* Depth of node containing pCell */
80419 CellInfo info; /* Size of the cell being deleted */
80420 u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */
80421
80422 assert( cursorOwnsBtShared(pCur) )((void) (0));
80423 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
80424 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
80425 assert( pCur->curFlags & BTCF_WriteFlag )((void) (0));
80426 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
80427 assert( !hasReadConflicts(p, pCur->pgnoRoot) )((void) (0));
80428 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 )((void) (0));
80429 if( pCur->eState!=CURSOR_VALID0 ){
80430 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
80431 rc = btreeRestoreCursorPosition(pCur);
80432 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID )((void) (0));
80433 if( rc || pCur->eState!=CURSOR_VALID0 ) return rc;
80434 }else{
80435 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot)sqlite3CorruptError(80435);
80436 }
80437 }
80438 assert( pCur->eState==CURSOR_VALID )((void) (0));
80439
80440 iCellDepth = pCur->iPage;
80441 iCellIdx = pCur->ix;
80442 pPage = pCur->pPage;
80443 if( pPage->nCell<=iCellIdx ){
80444 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80444);
80445 }
80446 pCell = findCell(pPage, iCellIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCellIdx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(iCellIdx)])[1])))
;
80447 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
80448 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80448);
80449 }
80450 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
80451 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80451);
80452 }
80453
80454 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
80455 ** be preserved following this delete operation. If the current delete
80456 ** will cause a b-tree rebalance, then this is done by saving the cursor
80457 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
80458 ** returning.
80459 **
80460 ** If the current delete will not cause a rebalance, then the cursor
80461 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
80462 ** before or after the deleted entry.
80463 **
80464 ** The bPreserve value records which path is required:
80465 **
80466 ** bPreserve==0 Not necessary to save the cursor position
80467 ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position
80468 ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT.
80469 */
80470 bPreserve = (flags & BTREE_SAVEPOSITION0x02)!=0;
80471 if( bPreserve ){
80472 if( !pPage->leaf
80473 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
80474 (int)(pBt->usableSize*2/3)
80475 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
80476 ){
80477 /* A b-tree rebalance will be required after deleting this entry.
80478 ** Save the cursor key. */
80479 rc = saveCursorKey(pCur);
80480 if( rc ) return rc;
80481 }else{
80482 bPreserve = 2;
80483 }
80484 }
80485
80486 /* If the page containing the entry to delete is not a leaf page, move
80487 ** the cursor to the largest entry in the tree that is smaller than
80488 ** the entry being deleted. This cell will replace the cell being deleted
80489 ** from the internal node. The 'previous' entry is used for this instead
80490 ** of the 'next' entry, as the previous entry is always a part of the
80491 ** sub-tree headed by the child page of the cell being deleted. This makes
80492 ** balancing the tree following the delete operation easier. */
80493 if( !pPage->leaf ){
80494 rc = sqlite3BtreePrevious(pCur, 0);
80495 assert( rc!=SQLITE_DONE )((void) (0));
80496 if( rc ) return rc;
80497 }
80498
80499 /* Save the positions of any other cursors open on this table before
80500 ** making any modifications. */
80501 if( pCur->curFlags & BTCF_Multiple0x20 ){
80502 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
80503 if( rc ) return rc;
80504 }
80505
80506 /* If this is a delete operation to remove a row from a table b-tree,
80507 ** invalidate any incrblob cursors open on the row being deleted. */
80508 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
80509 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
80510 }
80511
80512 /* Make the page containing the entry to be deleted writable. Then free any
80513 ** overflow pages associated with the entry and finally remove the cell
80514 ** itself from within the page. */
80515 rc = sqlite3PagerWrite(pPage->pDbPage);
80516 if( rc ) return rc;
80517 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; }
;
80518 dropCell(pPage, iCellIdx, info.nSize, &rc);
80519 if( rc ) return rc;
80520
80521 /* If the cell deleted was not located on a leaf page, then the cursor
80522 ** is currently pointing to the largest entry in the sub-tree headed
80523 ** by the child-page of the cell that was just deleted from an internal
80524 ** node. The cell from the leaf node needs to be moved to the internal
80525 ** node to replace the deleted cell. */
80526 if( !pPage->leaf ){
80527 MemPage *pLeaf = pCur->pPage;
80528 int nCell;
80529 Pgno n;
80530 unsigned char *pTmp;
80531
80532 if( pLeaf->nFree<0 ){
80533 rc = btreeComputeFreeSpace(pLeaf);
80534 if( rc ) return rc;
80535 }
80536 if( iCellDepth<pCur->iPage-1 ){
80537 n = pCur->apPage[iCellDepth+1]->pgno;
80538 }else{
80539 n = pCur->pPage->pgno;
80540 }
80541 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])))
;
80542 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf)sqlite3CorruptError(80542);
80543 nCell = pLeaf->xCellSize(pLeaf, pCell);
80544 assert( MX_CELL_SIZE(pBt) >= nCell )((void) (0));
80545 pTmp = pBt->pTmpSpace;
80546 assert( pTmp!=0 )((void) (0));
80547 rc = sqlite3PagerWrite(pLeaf->pDbPage);
80548 if( rc==SQLITE_OK0 ){
80549 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
80550 }
80551 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
80552 if( rc ) return rc;
80553 }
80554
80555 /* Balance the tree. If the entry deleted was located on a leaf page,
80556 ** then the cursor still points to that page. In this case the first
80557 ** call to balance() repairs the tree, and the if(...) condition is
80558 ** never true.
80559 **
80560 ** Otherwise, if the entry deleted was on an internal node page, then
80561 ** pCur is pointing to the leaf page from which a cell was removed to
80562 ** replace the cell deleted from the internal node. This is slightly
80563 ** tricky as the leaf node may be underfull, and the internal node may
80564 ** be either under or overfull. In this case run the balancing algorithm
80565 ** on the leaf node first. If the balance proceeds far enough up the
80566 ** tree that we can be sure that any problem in the internal node has
80567 ** been corrected, so be it. Otherwise, after balancing the leaf node,
80568 ** walk the cursor up the tree to the internal node and balance it as
80569 ** well. */
80570 assert( pCur->pPage->nOverflow==0 )((void) (0));
80571 assert( pCur->pPage->nFree>=0 )((void) (0));
80572 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
80573 /* Optimization: If the free space is less than 2/3rds of the page,
80574 ** then balance() will always be a no-op. No need to invoke it. */
80575 rc = SQLITE_OK0;
80576 }else{
80577 rc = balance(pCur);
80578 }
80579 if( rc==SQLITE_OK0 && pCur->iPage>iCellDepth ){
80580 releasePageNotNull(pCur->pPage);
80581 pCur->iPage--;
80582 while( pCur->iPage>iCellDepth ){
80583 releasePage(pCur->apPage[pCur->iPage--]);
80584 }
80585 pCur->pPage = pCur->apPage[pCur->iPage];
80586 rc = balance(pCur);
80587 }
80588
80589 if( rc==SQLITE_OK0 ){
80590 if( bPreserve>1 ){
80591 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) )((void) (0));
80592 assert( pPage==pCur->pPage || CORRUPT_DB )((void) (0));
80593 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell )((void) (0));
80594 pCur->eState = CURSOR_SKIPNEXT2;
80595 if( iCellIdx>=pPage->nCell ){
80596 pCur->skipNext = -1;
80597 pCur->ix = pPage->nCell-1;
80598 }else{
80599 pCur->skipNext = 1;
80600 }
80601 }else{
80602 rc = moveToRoot(pCur);
80603 if( bPreserve ){
80604 btreeReleaseAllCursorPages(pCur);
80605 pCur->eState = CURSOR_REQUIRESEEK3;
80606 }
80607 if( rc==SQLITE_EMPTY16 ) rc = SQLITE_OK0;
80608 }
80609 }
80610 return rc;
80611}
80612
80613/*
80614** Create a new BTree table. Write into *piTable the page
80615** number for the root page of the new table.
80616**
80617** The type of type is determined by the flags parameter. Only the
80618** following values of flags are currently in use. Other values for
80619** flags might not work:
80620**
80621** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
80622** BTREE_ZERODATA Used for SQL indices
80623*/
80624static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
80625 BtShared *pBt = p->pBt;
80626 MemPage *pRoot;
80627 Pgno pgnoRoot;
80628 int rc;
80629 int ptfFlags; /* Page-type flags for the root page of new table */
80630
80631 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
80632 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
80633 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
80634
80635#ifdef SQLITE_OMIT_AUTOVACUUM
80636 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
80637 if( rc ){
80638 return rc;
80639 }
80640#else
80641 if( pBt->autoVacuum ){
80642 Pgno pgnoMove; /* Move a page here to make room for the root-page */
80643 MemPage *pPageMove; /* The page to move to. */
80644
80645 /* Creating a new table may probably require moving an existing database
80646 ** to make room for the new tables root page. In case this page turns
80647 ** out to be an overflow page, delete all overflow page-map caches
80648 ** held by open cursors.
80649 */
80650 invalidateAllOverflowCache(pBt);
80651
80652 /* Read the value of meta[3] from the database to determine where the
80653 ** root page of the new table should go. meta[3] is the largest root-page
80654 ** created so far, so the new root-page is (meta[3]+1).
80655 */
80656 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &pgnoRoot);
80657 if( pgnoRoot>btreePagecount(pBt) ){
80658 return SQLITE_CORRUPT_PGNO(pgnoRoot)sqlite3CorruptError(80658);
80659 }
80660 pgnoRoot++;
80661
80662 /* The new root-page may not be allocated on a pointer-map page, or the
80663 ** PENDING_BYTE page.
80664 */
80665 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot)ptrmapPageno(pBt, pgnoRoot) ||
80666 pgnoRoot==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
80667 pgnoRoot++;
80668 }
80669 assert( pgnoRoot>=3 )((void) (0));
80670
80671 /* Allocate a page. The page that currently resides at pgnoRoot will
80672 ** be moved to the allocated page (unless the allocated page happens
80673 ** to reside at pgnoRoot).
80674 */
80675 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT1);
80676 if( rc!=SQLITE_OK0 ){
80677 return rc;
80678 }
80679
80680 if( pgnoMove!=pgnoRoot ){
80681 /* pgnoRoot is the page that will be used for the root-page of
80682 ** the new table (assuming an error did not occur). But we were
80683 ** allocated pgnoMove. If required (i.e. if it was not allocated
80684 ** by extending the file), the current page at position pgnoMove
80685 ** is already journaled.
80686 */
80687 u8 eType = 0;
80688 Pgno iPtrPage = 0;
80689
80690 /* Save the positions of any open cursors. This is required in
80691 ** case they are holding a reference to an xFetch reference
80692 ** corresponding to page pgnoRoot. */
80693 rc = saveAllCursors(pBt, 0, 0);
80694 releasePage(pPageMove);
80695 if( rc!=SQLITE_OK0 ){
80696 return rc;
80697 }
80698
80699 /* Move the page currently at pgnoRoot to pgnoMove. */
80700 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
80701 if( rc!=SQLITE_OK0 ){
80702 return rc;
80703 }
80704 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
80705 if( eType==PTRMAP_ROOTPAGE1 || eType==PTRMAP_FREEPAGE2 ){
80706 rc = SQLITE_CORRUPT_PGNO(pgnoRoot)sqlite3CorruptError(80706);
80707 }
80708 if( rc!=SQLITE_OK0 ){
80709 releasePage(pRoot);
80710 return rc;
80711 }
80712 assert( eType!=PTRMAP_ROOTPAGE )((void) (0));
80713 assert( eType!=PTRMAP_FREEPAGE )((void) (0));
80714 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
80715 releasePage(pRoot);
80716
80717 /* Obtain the page at pgnoRoot */
80718 if( rc!=SQLITE_OK0 ){
80719 return rc;
80720 }
80721 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
80722 if( rc!=SQLITE_OK0 ){
80723 return rc;
80724 }
80725 rc = sqlite3PagerWrite(pRoot->pDbPage);
80726 if( rc!=SQLITE_OK0 ){
80727 releasePage(pRoot);
80728 return rc;
80729 }
80730 }else{
80731 pRoot = pPageMove;
80732 }
80733
80734 /* Update the pointer-map and meta-data with the new root-page number. */
80735 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE1, 0, &rc);
80736 if( rc ){
80737 releasePage(pRoot);
80738 return rc;
80739 }
80740
80741 /* When the new root page was allocated, page 1 was made writable in
80742 ** order either to increase the database filesize, or to decrement the
80743 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
80744 */
80745 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) )((void) (0));
80746 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
80747 if( NEVER(rc)(rc) ){
80748 releasePage(pRoot);
80749 return rc;
80750 }
80751
80752 }else{
80753 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
80754 if( rc ) return rc;
80755 }
80756#endif
80757 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
80758 if( createTabFlags & BTREE_INTKEY1 ){
80759 ptfFlags = PTF_INTKEY0x01 | PTF_LEAFDATA0x04 | PTF_LEAF0x08;
80760 }else{
80761 ptfFlags = PTF_ZERODATA0x02 | PTF_LEAF0x08;
80762 }
80763 zeroPage(pRoot, ptfFlags);
80764 sqlite3PagerUnref(pRoot->pDbPage);
80765 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 )((void) (0));
80766 *piTable = pgnoRoot;
80767 return SQLITE_OK0;
80768}
80769SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
80770 int rc;
80771 sqlite3BtreeEnter(p);
80772 rc = btreeCreateTable(p, piTable, flags);
80773 sqlite3BtreeLeave(p);
80774 return rc;
80775}
80776
80777/*
80778** Erase the given database page and all its children. Return
80779** the page to the freelist.
80780*/
80781static int clearDatabasePage(
80782 BtShared *pBt, /* The BTree that contains the table */
80783 Pgno pgno, /* Page number to clear */
80784 int freePageFlag, /* Deallocate page if true */
80785 i64 *pnChange /* Add number of Cells freed to this counter */
80786){
80787 MemPage *pPage;
80788 int rc;
80789 unsigned char *pCell;
80790 int i;
80791 int hdr;
80792 CellInfo info;
80793
80794 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
80795 if( pgno>btreePagecount(pBt) ){
80796 return SQLITE_CORRUPT_PGNO(pgno)sqlite3CorruptError(80796);
80797 }
80798 rc = getAndInitPage(pBt, pgno, &pPage, 0);
80799 if( rc ) return rc;
80800 if( (pBt->openFlags & BTREE_SINGLE4)==0
80801 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
80802 ){
80803 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(80803);
80804 goto cleardatabasepage_out;
80805 }
80806 hdr = pPage->hdrOffset;
80807 for(i=0; i<pPage->nCell; i++){
80808 pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
80809 if( !pPage->leaf ){
80810 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(pCell), 1, pnChange);
80811 if( rc ) goto cleardatabasepage_out;
80812 }
80813 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; }
;
80814 if( rc ) goto cleardatabasepage_out;
80815 }
80816 if( !pPage->leaf ){
80817 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(&pPage->aData[hdr+8]), 1, pnChange);
80818 if( rc ) goto cleardatabasepage_out;
80819 if( pPage->intKey ) pnChange = 0;
80820 }
80821 if( pnChange ){
80822 testcase( !pPage->intKey );
80823 *pnChange += pPage->nCell;
80824 }
80825 if( freePageFlag ){
80826 freePage(pPage, &rc);
80827 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
80828 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF0x08);
80829 }
80830
80831cleardatabasepage_out:
80832 releasePage(pPage);
80833 return rc;
80834}
80835
80836/*
80837** Delete all information from a single table in the database. iTable is
80838** the page number of the root of the table. After this routine returns,
80839** the root page is empty, but still exists.
80840**
80841** This routine will fail with SQLITE_LOCKED if there are any open
80842** read cursors on the table. Open write cursors are moved to the
80843** root of the table.
80844**
80845** If pnChange is not NULL, then the integer value pointed to by pnChange
80846** is incremented by the number of entries in the table.
80847*/
80848SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
80849 int rc;
80850 BtShared *pBt = p->pBt;
80851 sqlite3BtreeEnter(p);
80852 assert( p->inTrans==TRANS_WRITE )((void) (0));
80853
80854 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
80855
80856 if( SQLITE_OK0==rc ){
80857 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
80858 ** is the root of a table b-tree - if it is not, the following call is
80859 ** a no-op). */
80860 if( p->hasIncrblobCur ){
80861 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
80862 }
80863 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
80864 }
80865 sqlite3BtreeLeave(p);
80866 return rc;
80867}
80868
80869/*
80870** Delete all information from the single table that pCur is open on.
80871**
80872** This routine only work for pCur on an ephemeral table.
80873*/
80874SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
80875 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
80876}
80877
80878/*
80879** Erase all information in a table and add the root of the table to
80880** the freelist. Except, the root of the principle table (the one on
80881** page 1) is never added to the freelist.
80882**
80883** This routine will fail with SQLITE_LOCKED if there are any open
80884** cursors on the table.
80885**
80886** If AUTOVACUUM is enabled and the page at iTable is not the last
80887** root page in the database file, then the last root page
80888** in the database file is moved into the slot formerly occupied by
80889** iTable and that last slot formerly occupied by the last root page
80890** is added to the freelist instead of iTable. In this say, all
80891** root pages are kept at the beginning of the database file, which
80892** is necessary for AUTOVACUUM to work right. *piMoved is set to the
80893** page number that used to be the last root page in the file before
80894** the move. If no page gets moved, *piMoved is set to 0.
80895** The last root page is recorded in meta[3] and the value of
80896** meta[3] is updated by this procedure.
80897*/
80898static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
80899 int rc;
80900 MemPage *pPage = 0;
80901 BtShared *pBt = p->pBt;
80902
80903 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
80904 assert( p->inTrans==TRANS_WRITE )((void) (0));
80905 assert( iTable>=2 )((void) (0));
80906 if( iTable>btreePagecount(pBt) ){
80907 return SQLITE_CORRUPT_PGNO(iTable)sqlite3CorruptError(80907);
80908 }
80909
80910 rc = sqlite3BtreeClearTable(p, iTable, 0);
80911 if( rc ) return rc;
80912 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
80913 if( NEVER(rc)(rc) ){
80914 releasePage(pPage);
80915 return rc;
80916 }
80917
80918 *piMoved = 0;
80919
80920#ifdef SQLITE_OMIT_AUTOVACUUM
80921 freePage(pPage, &rc);
80922 releasePage(pPage);
80923#else
80924 if( pBt->autoVacuum ){
80925 Pgno maxRootPgno;
80926 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &maxRootPgno);
80927
80928 if( iTable==maxRootPgno ){
80929 /* If the table being dropped is the table with the largest root-page
80930 ** number in the database, put the root page on the free list.
80931 */
80932 freePage(pPage, &rc);
80933 releasePage(pPage);
80934 if( rc!=SQLITE_OK0 ){
80935 return rc;
80936 }
80937 }else{
80938 /* The table being dropped does not have the largest root-page
80939 ** number in the database. So move the page that does into the
80940 ** gap left by the deleted root-page.
80941 */
80942 MemPage *pMove;
80943 releasePage(pPage);
80944 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
80945 if( rc!=SQLITE_OK0 ){
80946 return rc;
80947 }
80948 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE1, 0, iTable, 0);
80949 releasePage(pMove);
80950 if( rc!=SQLITE_OK0 ){
80951 return rc;
80952 }
80953 pMove = 0;
80954 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
80955 freePage(pMove, &rc);
80956 releasePage(pMove);
80957 if( rc!=SQLITE_OK0 ){
80958 return rc;
80959 }
80960 *piMoved = maxRootPgno;
80961 }
80962
80963 /* Set the new 'max-root-page' value in the database header. This
80964 ** is the old value less one, less one more if that happens to
80965 ** be a root-page number, less one again if that is the
80966 ** PENDING_BYTE_PAGE.
80967 */
80968 maxRootPgno--;
80969 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
80970 || PTRMAP_ISPAGE(pBt, maxRootPgno)(ptrmapPageno((pBt), (maxRootPgno))==(maxRootPgno)) ){
80971 maxRootPgno--;
80972 }
80973 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
80974
80975 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
80976 }else{
80977 freePage(pPage, &rc);
80978 releasePage(pPage);
80979 }
80980#endif
80981 return rc;
80982}
80983SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
80984 int rc;
80985 sqlite3BtreeEnter(p);
80986 rc = btreeDropTable(p, iTable, piMoved);
80987 sqlite3BtreeLeave(p);
80988 return rc;
80989}
80990
80991
80992/*
80993** This function may only be called if the b-tree connection already
80994** has a read or write transaction open on the database.
80995**
80996** Read the meta-information out of a database file. Meta[0]
80997** is the number of free pages currently in the database. Meta[1]
80998** through meta[15] are available for use by higher layers. Meta[0]
80999** is read-only, the others are read/write.
81000**
81001** The schema layer numbers meta values differently. At the schema
81002** layer (and the SetCookie and ReadCookie opcodes) the number of
81003** free pages is not visible. So Cookie[0] is the same as Meta[1].
81004**
81005** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
81006** of reading the value out of the header, it instead loads the "DataVersion"
81007** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
81008** database file. It is a number computed by the pager. But its access
81009** pattern is the same as header meta values, and so it is convenient to
81010** read it from this routine.
81011*/
81012SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
81013 BtShared *pBt = p->pBt;
81014
81015 sqlite3BtreeEnter(p);
81016 assert( p->inTrans>TRANS_NONE )((void) (0));
81017 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) )((void) (0));
81018 assert( pBt->pPage1 )((void) (0));
81019 assert( idx>=0 && idx<=15 )((void) (0));
81020
81021 if( idx==BTREE_DATA_VERSION15 ){
81022 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
81023 }else{
81024 *pMeta = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36 + idx*4]);
81025 }
81026
81027 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
81028 ** database, mark the database as read-only. */
81029#ifdef SQLITE_OMIT_AUTOVACUUM
81030 if( idx==BTREE_LARGEST_ROOT_PAGE4 && *pMeta>0 ){
81031 pBt->btsFlags |= BTS_READ_ONLY0x0001;
81032 }
81033#endif
81034
81035 sqlite3BtreeLeave(p);
81036}
81037
81038/*
81039** Write meta-information back into the database. Meta[0] is
81040** read-only and may not be written.
81041*/
81042SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
81043 BtShared *pBt = p->pBt;
81044 unsigned char *pP1;
81045 int rc;
81046 assert( idx>=1 && idx<=15 )((void) (0));
81047 sqlite3BtreeEnter(p);
81048 assert( p->inTrans==TRANS_WRITE )((void) (0));
81049 assert( pBt->pPage1!=0 )((void) (0));
81050 pP1 = pBt->pPage1->aData;
81051 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
81052 if( rc==SQLITE_OK0 ){
81053 put4bytesqlite3Put4byte(&pP1[36 + idx*4], iMeta);
81054#ifndef SQLITE_OMIT_AUTOVACUUM
81055 if( idx==BTREE_INCR_VACUUM7 ){
81056 assert( pBt->autoVacuum || iMeta==0 )((void) (0));
81057 assert( iMeta==0 || iMeta==1 )((void) (0));
81058 pBt->incrVacuum = (u8)iMeta;
81059 }
81060#endif
81061 }
81062 sqlite3BtreeLeave(p);
81063 return rc;
81064}
81065
81066/*
81067** The first argument, pCur, is a cursor opened on some b-tree. Count the
81068** number of entries in the b-tree and write the result to *pnEntry.
81069**
81070** SQLITE_OK is returned if the operation is successfully executed.
81071** Otherwise, if an error is encountered (i.e. an IO error or database
81072** corruption) an SQLite error code is returned.
81073*/
81074SQLITE_PRIVATEstatic int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
81075 i64 nEntry = 0; /* Value to return in *pnEntry */
81076 int rc; /* Return code */
81077
81078 rc = moveToRoot(pCur);
81079 if( rc==SQLITE_EMPTY16 ){
81080 *pnEntry = 0;
81081 return SQLITE_OK0;
81082 }
81083
81084 /* Unless an error occurs, the following loop runs one iteration for each
81085 ** page in the B-Tree structure (not including overflow pages).
81086 */
81087 while( rc==SQLITE_OK0 && !AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
81088 int iIdx; /* Index of child node in parent */
81089 MemPage *pPage; /* Current page of the b-tree */
81090
81091 /* If this is a leaf page or the tree is not an int-key tree, then
81092 ** this page contains countable entries. Increment the entry counter
81093 ** accordingly.
81094 */
81095 pPage = pCur->pPage;
81096 if( pPage->leaf || !pPage->intKey ){
81097 nEntry += pPage->nCell;
81098 }
81099
81100 /* pPage is a leaf node. This loop navigates the cursor so that it
81101 ** points to the first interior cell that it points to the parent of
81102 ** the next page in the tree that has not yet been visited. The
81103 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
81104 ** of the page, or to the number of cells in the page if the next page
81105 ** to visit is the right-child of its parent.
81106 **
81107 ** If all pages in the tree have been visited, return SQLITE_OK to the
81108 ** caller.
81109 */
81110 if( pPage->leaf ){
81111 do {
81112 if( pCur->iPage==0 ){
81113 /* All pages of the b-tree have been visited. Return successfully. */
81114 *pnEntry = nEntry;
81115 return moveToRoot(pCur);
81116 }
81117 moveToParent(pCur);
81118 }while ( pCur->ix>=pCur->pPage->nCell );
81119
81120 pCur->ix++;
81121 pPage = pCur->pPage;
81122 }
81123
81124 /* Descend to the child node of the cell that the cursor currently
81125 ** points at. This is the right-child if (iIdx==pPage->nCell).
81126 */
81127 iIdx = pCur->ix;
81128 if( iIdx==pPage->nCell ){
81129 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
81130 }else{
81131 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, iIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iIdx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iIdx)])[1])))
));
81132 }
81133 }
81134
81135 /* An error has occurred. Return an error code. */
81136 return rc;
81137}
81138
81139/*
81140** Return the pager associated with a BTree. This routine is used for
81141** testing and debugging only.
81142*/
81143SQLITE_PRIVATEstatic Pager *sqlite3BtreePager(Btree *p){
81144 return p->pBt->pPager;
81145}
81146
81147#ifndef SQLITE_OMIT_INTEGRITY_CHECK
81148/*
81149** Record an OOM error during integrity_check
81150*/
81151static void checkOom(IntegrityCk *pCheck){
81152 pCheck->rc = SQLITE_NOMEM7;
81153 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
81154 if( pCheck->nErr==0 ) pCheck->nErr++;
81155}
81156
81157/*
81158** Invoke the progress handler, if appropriate. Also check for an
81159** interrupt.
81160*/
81161static void checkProgress(IntegrityCk *pCheck){
81162 sqlite3 *db = pCheck->db;
81163 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
81164 pCheck->rc = SQLITE_INTERRUPT9;
81165 pCheck->nErr++;
81166 pCheck->mxErr = 0;
81167 }
81168#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
81169 if( db->xProgress ){
81170 assert( db->nProgressOps>0 )((void) (0));
81171 pCheck->nStep++;
81172 if( (pCheck->nStep % db->nProgressOps)==0
81173 && db->xProgress(db->pProgressArg)
81174 ){
81175 pCheck->rc = SQLITE_INTERRUPT9;
81176 pCheck->nErr++;
81177 pCheck->mxErr = 0;
81178 }
81179 }
81180#endif
81181}
81182
81183/*
81184** Append a message to the error message string.
81185*/
81186static void checkAppendMsg(
81187 IntegrityCk *pCheck,
81188 const char *zFormat,
81189 ...
81190){
81191 va_list ap;
81192 checkProgress(pCheck);
81193 if( !pCheck->mxErr ) return;
81194 pCheck->mxErr--;
81195 pCheck->nErr++;
81196 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
81197 if( pCheck->errMsg.nChar ){
81198 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
81199 }
81200 if( pCheck->zPfx ){
81201 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
81202 pCheck->v0, pCheck->v1, pCheck->v2);
81203 }
81204 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
81205 va_end(ap)__builtin_va_end(ap);
81206 if( pCheck->errMsg.accError==SQLITE_NOMEM7 ){
81207 checkOom(pCheck);
81208 }
81209}
81210#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
81211
81212#ifndef SQLITE_OMIT_INTEGRITY_CHECK
81213
81214/*
81215** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
81216** corresponds to page iPg is already set.
81217*/
81218static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
81219 assert( pCheck->aPgRef!=0 )((void) (0));
81220 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
81221 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
81222}
81223
81224/*
81225** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
81226*/
81227static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
81228 assert( pCheck->aPgRef!=0 )((void) (0));
81229 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
81230 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
81231}
81232
81233
81234/*
81235** Add 1 to the reference count for page iPage. If this is the second
81236** reference to the page, add an error message to pCheck->zErrMsg.
81237** Return 1 if there are 2 or more references to the page and 0 if
81238** if this is the first reference to the page.
81239**
81240** Also check that the page number is in bounds.
81241*/
81242static int checkRef(IntegrityCk *pCheck, Pgno iPage){
81243 if( iPage>pCheck->nCkPage || iPage==0 ){
81244 checkAppendMsg(pCheck, "invalid page number %u", iPage);
81245 return 1;
81246 }
81247 if( getPageReferenced(pCheck, iPage) ){
81248 checkAppendMsg(pCheck, "2nd reference to page %u", iPage);
81249 return 1;
81250 }
81251 setPageReferenced(pCheck, iPage);
81252 return 0;
81253}
81254
81255#ifndef SQLITE_OMIT_AUTOVACUUM
81256/*
81257** Check that the entry in the pointer-map for page iChild maps to
81258** page iParent, pointer type ptrType. If not, append an error message
81259** to pCheck.
81260*/
81261static void checkPtrmap(
81262 IntegrityCk *pCheck, /* Integrity check context */
81263 Pgno iChild, /* Child page number */
81264 u8 eType, /* Expected pointer map type */
81265 Pgno iParent /* Expected pointer map parent page number */
81266){
81267 int rc;
81268 u8 ePtrmapType;
81269 Pgno iPtrmapParent;
81270
81271 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
81272 if( rc!=SQLITE_OK0 ){
81273 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) checkOom(pCheck);
81274 checkAppendMsg(pCheck, "Failed to read ptrmap key=%u", iChild);
81275 return;
81276 }
81277
81278 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
81279 checkAppendMsg(pCheck,
81280 "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)",
81281 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
81282 }
81283}
81284#endif
81285
81286/*
81287** Check the integrity of the freelist or of an overflow page list.
81288** Verify that the number of pages on the list is N.
81289*/
81290static void checkList(
81291 IntegrityCk *pCheck, /* Integrity checking context */
81292 int isFreeList, /* True for a freelist. False for overflow page list */
81293 Pgno iPage, /* Page number for first page in the list */
81294 u32 N /* Expected number of pages in the list */
81295){
81296 int i;
81297 u32 expected = N;
81298 int nErrAtStart = pCheck->nErr;
81299 while( iPage!=0 && pCheck->mxErr ){
81300 DbPage *pOvflPage;
81301 unsigned char *pOvflData;
81302 if( checkRef(pCheck, iPage) ) break;
81303 N--;
81304 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
81305 checkAppendMsg(pCheck, "failed to get page %u", iPage);
81306 break;
81307 }
81308 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
81309 if( isFreeList ){
81310 u32 n = (u32)get4bytesqlite3Get4byte(&pOvflData[4]);
81311#ifndef SQLITE_OMIT_AUTOVACUUM
81312 if( pCheck->pBt->autoVacuum ){
81313 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE2, 0);
81314 }
81315#endif
81316 if( n>pCheck->pBt->usableSize/4-2 ){
81317 checkAppendMsg(pCheck,
81318 "freelist leaf count too big on page %u", iPage);
81319 N--;
81320 }else{
81321 for(i=0; i<(int)n; i++){
81322 Pgno iFreePage = get4bytesqlite3Get4byte(&pOvflData[8+i*4]);
81323#ifndef SQLITE_OMIT_AUTOVACUUM
81324 if( pCheck->pBt->autoVacuum ){
81325 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE2, 0);
81326 }
81327#endif
81328 checkRef(pCheck, iFreePage);
81329 }
81330 N -= n;
81331 }
81332 }
81333#ifndef SQLITE_OMIT_AUTOVACUUM
81334 else{
81335 /* If this database supports auto-vacuum and iPage is not the last
81336 ** page in this overflow list, check that the pointer-map entry for
81337 ** the following page matches iPage.
81338 */
81339 if( pCheck->pBt->autoVacuum && N>0 ){
81340 i = get4bytesqlite3Get4byte(pOvflData);
81341 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW24, iPage);
81342 }
81343 }
81344#endif
81345 iPage = get4bytesqlite3Get4byte(pOvflData);
81346 sqlite3PagerUnref(pOvflPage);
81347 }
81348 if( N && nErrAtStart==pCheck->nErr ){
81349 checkAppendMsg(pCheck,
81350 "%s is %u but should be %u",
81351 isFreeList ? "size" : "overflow list length",
81352 expected-N, expected);
81353 }
81354}
81355#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
81356
81357/*
81358** An implementation of a min-heap.
81359**
81360** aHeap[0] is the number of elements on the heap. aHeap[1] is the
81361** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
81362** and aHeap[N*2+1].
81363**
81364** The heap property is this: Every node is less than or equal to both
81365** of its daughter nodes. A consequence of the heap property is that the
81366** root node aHeap[1] is always the minimum value currently in the heap.
81367**
81368** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
81369** the heap, preserving the heap property. The btreeHeapPull() routine
81370** removes the root element from the heap (the minimum value in the heap)
81371** and then moves other nodes around as necessary to preserve the heap
81372** property.
81373**
81374** This heap is used for cell overlap and coverage testing. Each u32
81375** entry represents the span of a cell or freeblock on a btree page.
81376** The upper 16 bits are the index of the first byte of a range and the
81377** lower 16 bits are the index of the last byte of that range.
81378*/
81379static void btreeHeapInsert(u32 *aHeap, u32 x){
81380 u32 j, i;
81381 assert( aHeap!=0 )((void) (0));
81382 i = ++aHeap[0];
81383 aHeap[i] = x;
81384 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
81385 x = aHeap[j];
81386 aHeap[j] = aHeap[i];
81387 aHeap[i] = x;
81388 i = j;
81389 }
81390}
81391static int btreeHeapPull(u32 *aHeap, u32 *pOut){
81392 u32 j, i, x;
81393 if( (x = aHeap[0])==0 ) return 0;
81394 *pOut = aHeap[1];
81395 aHeap[1] = aHeap[x];
81396 aHeap[x] = 0xffffffff;
81397 aHeap[0]--;
81398 i = 1;
81399 while( (j = i*2)<=aHeap[0] ){
81400 if( aHeap[j]>aHeap[j+1] ) j++;
81401 if( aHeap[i]<aHeap[j] ) break;
81402 x = aHeap[i];
81403 aHeap[i] = aHeap[j];
81404 aHeap[j] = x;
81405 i = j;
81406 }
81407 return 1;
81408}
81409
81410#ifndef SQLITE_OMIT_INTEGRITY_CHECK
81411/*
81412** Do various sanity checks on a single page of a tree. Return
81413** the tree depth. Root pages return 0. Parents of root pages
81414** return 1, and so forth.
81415**
81416** These checks are done:
81417**
81418** 1. Make sure that cells and freeblocks do not overlap
81419** but combine to completely cover the page.
81420** 2. Make sure integer cell keys are in order.
81421** 3. Check the integrity of overflow pages.
81422** 4. Recursively call checkTreePage on all children.
81423** 5. Verify that the depth of all children is the same.
81424*/
81425static int checkTreePage(
81426 IntegrityCk *pCheck, /* Context for the sanity check */
81427 Pgno iPage, /* Page number of the page to check */
81428 i64 *piMinKey, /* Write minimum integer primary key here */
81429 i64 maxKey /* Error if integer primary key greater than this */
81430){
81431 MemPage *pPage = 0; /* The page being analyzed */
81432 int i; /* Loop counter */
81433 int rc; /* Result code from subroutine call */
81434 int depth = -1, d2; /* Depth of a subtree */
81435 int pgno; /* Page number */
81436 int nFrag; /* Number of fragmented bytes on the page */
81437 int hdr; /* Offset to the page header */
81438 int cellStart; /* Offset to the start of the cell pointer array */
81439 int nCell; /* Number of cells */
81440 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
81441 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
81442 ** False if IPK must be strictly less than maxKey */
81443 u8 *data; /* Page content */
81444 u8 *pCell; /* Cell content */
81445 u8 *pCellIdx; /* Next element of the cell pointer array */
81446 BtShared *pBt; /* The BtShared object that owns pPage */
81447 u32 pc; /* Address of a cell */
81448 u32 usableSize; /* Usable size of the page */
81449 u32 contentOffset; /* Offset to the start of the cell content area */
81450 u32 *heap = 0; /* Min-heap used for checking cell coverage */
81451 u32 x, prev = 0; /* Next and previous entry on the min-heap */
81452 const char *saved_zPfx = pCheck->zPfx;
81453 int saved_v1 = pCheck->v1;
81454 int saved_v2 = pCheck->v2;
81455 u8 savedIsInit = 0;
81456
81457 /* Check that the page exists
81458 */
81459 checkProgress(pCheck);
81460 if( pCheck->mxErr==0 ) goto end_of_check;
81461 pBt = pCheck->pBt;
81462 usableSize = pBt->usableSize;
81463 if( iPage==0 ) return 0;
81464 if( checkRef(pCheck, iPage) ) return 0;
81465 pCheck->zPfx = "Tree %u page %u: ";
81466 pCheck->v1 = iPage;
81467 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
81468 checkAppendMsg(pCheck,
81469 "unable to get the page. error code=%d", rc);
81470 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) pCheck->rc = SQLITE_NOMEM7;
81471 goto end_of_check;
81472 }
81473
81474 /* Clear MemPage.isInit to make sure the corruption detection code in
81475 ** btreeInitPage() is executed. */
81476 savedIsInit = pPage->isInit;
81477 pPage->isInit = 0;
81478 if( (rc = btreeInitPage(pPage))!=0 ){
81479 assert( rc==SQLITE_CORRUPT )((void) (0)); /* The only possible error from InitPage */
81480 checkAppendMsg(pCheck,
81481 "btreeInitPage() returns error code %d", rc);
81482 goto end_of_check;
81483 }
81484 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
81485 assert( rc==SQLITE_CORRUPT )((void) (0));
81486 checkAppendMsg(pCheck, "free space corruption", rc);
81487 goto end_of_check;
81488 }
81489 data = pPage->aData;
81490 hdr = pPage->hdrOffset;
81491
81492 /* Set up for cell analysis */
81493 pCheck->zPfx = "Tree %u page %u cell %u: ";
81494 contentOffset = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
81495 assert( contentOffset<=usableSize )((void) (0)); /* Enforced by btreeInitPage() */
81496
81497 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
81498 ** number of cells on the page. */
81499 nCell = get2byte(&data[hdr+3])((&data[hdr+3])[0]<<8 | (&data[hdr+3])[1]);
81500 assert( pPage->nCell==nCell )((void) (0));
81501 if( pPage->leaf || pPage->intKey==0 ){
81502 pCheck->nRow += nCell;
81503 }
81504
81505 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
81506 ** immediately follows the b-tree page header. */
81507 cellStart = hdr + 12 - 4*pPage->leaf;
81508 assert( pPage->aCellIdx==&data[cellStart] )((void) (0));
81509 pCellIdx = &data[cellStart + 2*(nCell-1)];
81510
81511 if( !pPage->leaf ){
81512 /* Analyze the right-child page of internal pages */
81513 pgno = get4bytesqlite3Get4byte(&data[hdr+8]);
81514#ifndef SQLITE_OMIT_AUTOVACUUM
81515 if( pBt->autoVacuum ){
81516 pCheck->zPfx = "Tree %u page %u right child: ";
81517 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
81518 }
81519#endif
81520 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
81521 keyCanBeEqual = 0;
81522 }else{
81523 /* For leaf pages, the coverage check will occur in the same loop
81524 ** as the other cell checks, so initialize the heap. */
81525 heap = pCheck->heap;
81526 heap[0] = 0;
81527 }
81528
81529 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
81530 ** integer offsets to the cell contents. */
81531 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
81532 CellInfo info;
81533
81534 /* Check cell size */
81535 pCheck->v2 = i;
81536 assert( pCellIdx==&data[cellStart + i*2] )((void) (0));
81537 pc = get2byteAligned(pCellIdx)((pCellIdx)[0]<<8 | (pCellIdx)[1]);
81538 pCellIdx -= 2;
81539 if( pc<contentOffset || pc>usableSize-4 ){
81540 checkAppendMsg(pCheck, "Offset %u out of range %u..%u",
81541 pc, contentOffset, usableSize-4);
81542 doCoverageCheck = 0;
81543 continue;
81544 }
81545 pCell = &data[pc];
81546 pPage->xParseCell(pPage, pCell, &info);
81547 if( pc+info.nSize>usableSize ){
81548 checkAppendMsg(pCheck, "Extends off end of page");
81549 doCoverageCheck = 0;
81550 continue;
81551 }
81552
81553 /* Check for integer primary key out of range */
81554 if( pPage->intKey ){
81555 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
81556 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
81557 }
81558 maxKey = info.nKey;
81559 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
81560 }
81561
81562 /* Check the content overflow list */
81563 if( info.nPayload>info.nLocal ){
81564 u32 nPage; /* Number of pages on the overflow chain */
81565 Pgno pgnoOvfl; /* First page of the overflow chain */
81566 assert( pc + info.nSize - 4 <= usableSize )((void) (0));
81567 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
81568 pgnoOvfl = get4bytesqlite3Get4byte(&pCell[info.nSize - 4]);
81569#ifndef SQLITE_OMIT_AUTOVACUUM
81570 if( pBt->autoVacuum ){
81571 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW13, iPage);
81572 }
81573#endif
81574 checkList(pCheck, 0, pgnoOvfl, nPage);
81575 }
81576
81577 if( !pPage->leaf ){
81578 /* Check sanity of left child page for internal pages */
81579 pgno = get4bytesqlite3Get4byte(pCell);
81580#ifndef SQLITE_OMIT_AUTOVACUUM
81581 if( pBt->autoVacuum ){
81582 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
81583 }
81584#endif
81585 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
81586 keyCanBeEqual = 0;
81587 if( d2!=depth ){
81588 checkAppendMsg(pCheck, "Child page depth differs");
81589 depth = d2;
81590 }
81591 }else{
81592 /* Populate the coverage-checking heap for leaf pages */
81593 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
81594 }
81595 }
81596 *piMinKey = maxKey;
81597
81598 /* Check for complete coverage of the page
81599 */
81600 pCheck->zPfx = 0;
81601 if( doCoverageCheck && pCheck->mxErr>0 ){
81602 /* For leaf pages, the min-heap has already been initialized and the
81603 ** cells have already been inserted. But for internal pages, that has
81604 ** not yet been done, so do it now */
81605 if( !pPage->leaf ){
81606 heap = pCheck->heap;
81607 heap[0] = 0;
81608 for(i=nCell-1; i>=0; i--){
81609 u32 size;
81610 pc = get2byteAligned(&data[cellStart+i*2])((&data[cellStart+i*2])[0]<<8 | (&data[cellStart
+i*2])[1])
;
81611 size = pPage->xCellSize(pPage, &data[pc]);
81612 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
81613 }
81614 }
81615 assert( heap!=0 )((void) (0));
81616 /* Add the freeblocks to the min-heap
81617 **
81618 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
81619 ** is the offset of the first freeblock, or zero if there are no
81620 ** freeblocks on the page.
81621 */
81622 i = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
81623 while( i>0 ){
81624 int size, j;
81625 assert( (u32)i<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
81626 size = get2byte(&data[i+2])((&data[i+2])[0]<<8 | (&data[i+2])[1]);
81627 assert( (u32)(i+size)<=usableSize )((void) (0)); /* due to btreeComputeFreeSpace() */
81628 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
81629 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
81630 ** big-endian integer which is the offset in the b-tree page of the next
81631 ** freeblock in the chain, or zero if the freeblock is the last on the
81632 ** chain. */
81633 j = get2byte(&data[i])((&data[i])[0]<<8 | (&data[i])[1]);
81634 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
81635 ** increasing offset. */
81636 assert( j==0 || j>i+size )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
81637 assert( (u32)j<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
81638 i = j;
81639 }
81640 /* Analyze the min-heap looking for overlap between cells and/or
81641 ** freeblocks, and counting the number of untracked bytes in nFrag.
81642 **
81643 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
81644 ** There is an implied first entry the covers the page header, the cell
81645 ** pointer index, and the gap between the cell pointer index and the start
81646 ** of cell content.
81647 **
81648 ** The loop below pulls entries from the min-heap in order and compares
81649 ** the start_address against the previous end_address. If there is an
81650 ** overlap, that means bytes are used multiple times. If there is a gap,
81651 ** that gap is added to the fragmentation count.
81652 */
81653 nFrag = 0;
81654 prev = contentOffset - 1; /* Implied first min-heap entry */
81655 while( btreeHeapPull(heap,&x) ){
81656 if( (prev&0xffff)>=(x>>16) ){
81657 checkAppendMsg(pCheck,
81658 "Multiple uses for byte %u of page %u", x>>16, iPage);
81659 break;
81660 }else{
81661 nFrag += (x>>16) - (prev&0xffff) - 1;
81662 prev = x;
81663 }
81664 }
81665 nFrag += usableSize - (prev&0xffff) - 1;
81666 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
81667 ** is stored in the fifth field of the b-tree page header.
81668 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
81669 ** number of fragmented free bytes within the cell content area.
81670 */
81671 if( heap[0]==0 && nFrag!=data[hdr+7] ){
81672 checkAppendMsg(pCheck,
81673 "Fragmentation of %u bytes reported as %u on page %u",
81674 nFrag, data[hdr+7], iPage);
81675 }
81676 }
81677
81678end_of_check:
81679 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
81680 releasePage(pPage);
81681 pCheck->zPfx = saved_zPfx;
81682 pCheck->v1 = saved_v1;
81683 pCheck->v2 = saved_v2;
81684 return depth+1;
81685}
81686#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
81687
81688#ifndef SQLITE_OMIT_INTEGRITY_CHECK
81689/*
81690** This routine does a complete check of the given BTree file. aRoot[] is
81691** an array of pages numbers were each page number is the root page of
81692** a table. nRoot is the number of entries in aRoot.
81693**
81694** A read-only or read-write transaction must be opened before calling
81695** this function.
81696**
81697** Write the number of error seen in *pnErr. Except for some memory
81698** allocation errors, an error message held in memory obtained from
81699** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
81700** returned. If a memory allocation error occurs, NULL is returned.
81701**
81702** If the first entry in aRoot[] is 0, that indicates that the list of
81703** root pages is incomplete. This is a "partial integrity-check". This
81704** happens when performing an integrity check on a single table. The
81705** zero is skipped, of course. But in addition, the freelist checks
81706** and the checks to make sure every page is referenced are also skipped,
81707** since obviously it is not possible to know which pages are covered by
81708** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
81709** checks are still performed.
81710*/
81711SQLITE_PRIVATEstatic int sqlite3BtreeIntegrityCheck(
81712 sqlite3 *db, /* Database connection that is running the check */
81713 Btree *p, /* The btree to be checked */
81714 Pgno *aRoot, /* An array of root pages numbers for individual trees */
81715 Mem *aCnt, /* Memory cells to write counts for each tree to */
81716 int nRoot, /* Number of entries in aRoot[] */
81717 int mxErr, /* Stop reporting errors after this many */
81718 int *pnErr, /* OUT: Write number of errors seen to this variable */
81719 char **pzOut /* OUT: Write the error message string here */
81720){
81721 Pgno i;
81722 IntegrityCk sCheck;
81723 BtShared *pBt = p->pBt;
81724 u64 savedDbFlags = pBt->db->flags;
81725 char zErr[100];
81726 int bPartial = 0; /* True if not checking all btrees */
81727 int bCkFreelist = 1; /* True to scan the freelist */
81728 VVA_ONLY( int nRef );
81729
81730 assert( nRoot>0 )((void) (0));
81731 assert( aCnt!=0 )((void) (0));
81732
81733 /* aRoot[0]==0 means this is a partial check */
81734 if( aRoot[0]==0 ){
81735 assert( nRoot>1 )((void) (0));
81736 bPartial = 1;
81737 if( aRoot[1]!=1 ) bCkFreelist = 0;
81738 }
81739
81740 sqlite3BtreeEnter(p);
81741 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE )((void) (0));
81742 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
81743 assert( nRef>=0 )((void) (0));
81744 memset(&sCheck, 0, sizeof(sCheck));
81745 sCheck.db = db;
81746 sCheck.pBt = pBt;
81747 sCheck.pPager = pBt->pPager;
81748 sCheck.nCkPage = btreePagecount(sCheck.pBt);
81749 sCheck.mxErr = mxErr;
81750 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH2147483645);
81751 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
81752 if( sCheck.nCkPage==0 ){
81753 goto integrity_ck_cleanup;
81754 }
81755
81756 sCheck.aPgRef = sqlite3MallocZero((sCheck.nCkPage / 8)+ 1);
81757 if( !sCheck.aPgRef ){
81758 checkOom(&sCheck);
81759 goto integrity_ck_cleanup;
81760 }
81761 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
81762 if( sCheck.heap==0 ){
81763 checkOom(&sCheck);
81764 goto integrity_ck_cleanup;
81765 }
81766
81767 i = PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1));
81768 if( i<=sCheck.nCkPage ) setPageReferenced(&sCheck, i);
81769
81770 /* Check the integrity of the freelist
81771 */
81772 if( bCkFreelist ){
81773 sCheck.zPfx = "Freelist: ";
81774 checkList(&sCheck, 1, get4bytesqlite3Get4byte(&pBt->pPage1->aData[32]),
81775 get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]));
81776 sCheck.zPfx = 0;
81777 }
81778
81779 /* Check all the tables.
81780 */
81781#ifndef SQLITE_OMIT_AUTOVACUUM
81782 if( !bPartial ){
81783 if( pBt->autoVacuum ){
81784 Pgno mx = 0;
81785 Pgno mxInHdr;
81786 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
81787 mxInHdr = get4bytesqlite3Get4byte(&pBt->pPage1->aData[52]);
81788 if( mx!=mxInHdr ){
81789 checkAppendMsg(&sCheck,
81790 "max rootpage (%u) disagrees with header (%u)",
81791 mx, mxInHdr
81792 );
81793 }
81794 }else if( get4bytesqlite3Get4byte(&pBt->pPage1->aData[64])!=0 ){
81795 checkAppendMsg(&sCheck,
81796 "incremental_vacuum enabled with a max rootpage of zero"
81797 );
81798 }
81799 }
81800#endif
81801 testcase( pBt->db->flags & SQLITE_CellSizeCk );
81802 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk0x00200000;
81803 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
81804 sCheck.nRow = 0;
81805 if( aRoot[i] ){
81806 i64 notUsed;
81807#ifndef SQLITE_OMIT_AUTOVACUUM
81808 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
81809 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE1, 0);
81810 }
81811#endif
81812 sCheck.v0 = aRoot[i];
81813 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
81814 }
81815 sqlite3MemSetArrayInt64(aCnt, i, sCheck.nRow);
81816 }
81817 pBt->db->flags = savedDbFlags;
81818
81819 /* Make sure every page in the file is referenced
81820 */
81821 if( !bPartial ){
81822 for(i=1; i<=sCheck.nCkPage && sCheck.mxErr; i++){
81823#ifdef SQLITE_OMIT_AUTOVACUUM
81824 if( getPageReferenced(&sCheck, i)==0 ){
81825 checkAppendMsg(&sCheck, "Page %u: never used", i);
81826 }
81827#else
81828 /* If the database supports auto-vacuum, make sure no tables contain
81829 ** references to pointer-map pages.
81830 */
81831 if( getPageReferenced(&sCheck, i)==0 &&
81832 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)!=i || !pBt->autoVacuum) ){
81833 checkAppendMsg(&sCheck, "Page %u: never used", i);
81834 }
81835 if( getPageReferenced(&sCheck, i)!=0 &&
81836 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)==i && pBt->autoVacuum) ){
81837 checkAppendMsg(&sCheck, "Page %u: pointer map referenced", i);
81838 }
81839#endif
81840 }
81841 }
81842
81843 /* Clean up and report errors.
81844 */
81845integrity_ck_cleanup:
81846 sqlite3PageFree(sCheck.heap);
81847 sqlite3_free(sCheck.aPgRef);
81848 *pnErr = sCheck.nErr;
81849 if( sCheck.nErr==0 ){
81850 sqlite3_str_reset(&sCheck.errMsg);
81851 *pzOut = 0;
81852 }else{
81853 *pzOut = sqlite3StrAccumFinish(&sCheck.errMsg);
81854 }
81855 /* Make sure this analysis did not leave any unref() pages. */
81856 assert( nRef==sqlite3PagerRefcount(pBt->pPager) )((void) (0));
81857 sqlite3BtreeLeave(p);
81858 return sCheck.rc;
81859}
81860#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
81861
81862/*
81863** Return the full pathname of the underlying database file. Return
81864** an empty string if the database is in-memory or a TEMP database.
81865**
81866** The pager filename is invariant as long as the pager is
81867** open so it is safe to access without the BtShared mutex.
81868*/
81869SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *p){
81870 assert( p->pBt->pPager!=0 )((void) (0));
81871 return sqlite3PagerFilename(p->pBt->pPager, 1);
81872}
81873
81874/*
81875** Return the pathname of the journal file for this database. The return
81876** value of this routine is the same regardless of whether the journal file
81877** has been created or not.
81878**
81879** The pager journal filename is invariant as long as the pager is
81880** open so it is safe to access without the BtShared mutex.
81881*/
81882SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *p){
81883 assert( p->pBt->pPager!=0 )((void) (0));
81884 return sqlite3PagerJournalname(p->pBt->pPager);
81885}
81886
81887/*
81888** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
81889** to describe the current transaction state of Btree p.
81890*/
81891SQLITE_PRIVATEstatic int sqlite3BtreeTxnState(Btree *p){
81892 assert( p==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
81893 return p ? p->inTrans : 0;
81894}
81895
81896#ifndef SQLITE_OMIT_WAL
81897/*
81898** Run a checkpoint on the Btree passed as the first argument.
81899**
81900** Return SQLITE_LOCKED if this or any other connection has an open
81901** transaction on the shared-cache the argument Btree is connected to.
81902**
81903** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
81904*/
81905SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
81906 int rc = SQLITE_OK0;
81907 if( p ){
81908 BtShared *pBt = p->pBt;
81909 sqlite3BtreeEnter(p);
81910 if( pBt->inTransaction!=TRANS_NONE0 ){
81911 rc = SQLITE_LOCKED6;
81912 }else{
81913 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
81914 }
81915 sqlite3BtreeLeave(p);
81916 }
81917 return rc;
81918}
81919#endif
81920
81921/*
81922** Return true if there is currently a backup running on Btree p.
81923*/
81924SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree *p){
81925 assert( p )((void) (0));
81926 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
81927 return p->nBackup!=0;
81928}
81929
81930/*
81931** This function returns a pointer to a blob of memory associated with
81932** a single shared-btree. The memory is used by client code for its own
81933** purposes (for example, to store a high-level schema associated with
81934** the shared-btree). The btree layer manages reference counting issues.
81935**
81936** The first time this is called on a shared-btree, nBytes bytes of memory
81937** are allocated, zeroed, and returned to the caller. For each subsequent
81938** call the nBytes parameter is ignored and a pointer to the same blob
81939** of memory returned.
81940**
81941** If the nBytes parameter is 0 and the blob of memory has not yet been
81942** allocated, a null pointer is returned. If the blob has already been
81943** allocated, it is returned as normal.
81944**
81945** Just before the shared-btree is closed, the function passed as the
81946** xFree argument when the memory allocation was made is invoked on the
81947** blob of allocated memory. The xFree function should not call sqlite3_free()
81948** on the memory, the btree layer does that.
81949*/
81950SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
81951 BtShared *pBt = p->pBt;
81952 sqlite3BtreeEnter(p);
81953 if( !pBt->pSchema && nBytes ){
81954 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
81955 pBt->xFreeSchema = xFree;
81956 }
81957 sqlite3BtreeLeave(p);
81958 return pBt->pSchema;
81959}
81960
81961/*
81962** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
81963** btree as the argument handle holds an exclusive lock on the
81964** sqlite_schema table. Otherwise SQLITE_OK.
81965*/
81966SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *p){
81967 int rc;
81968 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
81969 sqlite3BtreeEnter(p);
81970 rc = querySharedCacheTableLock(p, SCHEMA_ROOT1, READ_LOCK1);
81971 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE )((void) (0));
81972 sqlite3BtreeLeave(p);
81973 return rc;
81974}
81975
81976
81977#ifndef SQLITE_OMIT_SHARED_CACHE
81978/*
81979** Obtain a lock on the table whose root page is iTab. The
81980** lock is a write lock if isWritelock is true or a read lock
81981** if it is false.
81982*/
81983SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
81984 int rc = SQLITE_OK0;
81985 assert( p->inTrans!=TRANS_NONE )((void) (0));
81986 if( p->sharable ){
81987 u8 lockType = READ_LOCK1 + isWriteLock;
81988 assert( READ_LOCK+1==WRITE_LOCK )((void) (0));
81989 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
81990
81991 sqlite3BtreeEnter(p);
81992 rc = querySharedCacheTableLock(p, iTab, lockType);
81993 if( rc==SQLITE_OK0 ){
81994 rc = setSharedCacheTableLock(p, iTab, lockType);
81995 }
81996 sqlite3BtreeLeave(p);
81997 }
81998 return rc;
81999}
82000#endif
82001
82002#ifndef SQLITE_OMIT_INCRBLOB
82003/*
82004** Argument pCsr must be a cursor opened for writing on an
82005** INTKEY table currently pointing at a valid table entry.
82006** This function modifies the data stored as part of that entry.
82007**
82008** Only the data content may only be modified, it is not possible to
82009** change the length of the data stored. If this function is called with
82010** parameters that attempt to write past the end of the existing data,
82011** no modifications are made and SQLITE_CORRUPT is returned.
82012*/
82013SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
82014 int rc;
82015 assert( cursorOwnsBtShared(pCsr) )((void) (0));
82016 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) )((void) (0));
82017 assert( pCsr->curFlags & BTCF_Incrblob )((void) (0));
82018
82019 rc = restoreCursorPosition(pCsr)(pCsr->eState>=3 ? btreeRestoreCursorPosition(pCsr) : 0
)
;
82020 if( rc!=SQLITE_OK0 ){
82021 return rc;
82022 }
82023 assert( pCsr->eState!=CURSOR_REQUIRESEEK )((void) (0));
82024 if( pCsr->eState!=CURSOR_VALID0 ){
82025 return SQLITE_ABORT4;
82026 }
82027
82028 /* Save the positions of all other cursors open on this table. This is
82029 ** required in case any of them are holding references to an xFetch
82030 ** version of the b-tree page modified by the accessPayload call below.
82031 **
82032 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
82033 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
82034 ** saveAllCursors can only return SQLITE_OK.
82035 */
82036 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
82037 assert( rc==SQLITE_OK )((void) (0));
82038
82039 /* Check some assumptions:
82040 ** (a) the cursor is open for writing,
82041 ** (b) there is a read/write transaction open,
82042 ** (c) the connection holds a write-lock on the table (if required),
82043 ** (d) there are no conflicting read-locks, and
82044 ** (e) the cursor points at a valid row of an intKey table.
82045 */
82046 if( (pCsr->curFlags & BTCF_WriteFlag0x01)==0 ){
82047 return SQLITE_READONLY8;
82048 }
82049 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0((void) (0))
82050 && pCsr->pBt->inTransaction==TRANS_WRITE )((void) (0));
82051 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) )((void) (0));
82052 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) )((void) (0));
82053 assert( pCsr->pPage->intKey )((void) (0));
82054
82055 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
82056}
82057
82058/*
82059** Mark this cursor as an incremental blob cursor.
82060*/
82061SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
82062 pCur->curFlags |= BTCF_Incrblob0x10;
82063 pCur->pBtree->hasIncrblobCur = 1;
82064}
82065#endif
82066
82067/*
82068** Set both the "read version" (single byte at byte offset 18) and
82069** "write version" (single byte at byte offset 19) fields in the database
82070** header to iVersion.
82071*/
82072SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
82073 BtShared *pBt = pBtree->pBt;
82074 int rc; /* Return code */
82075
82076 assert( iVersion==1 || iVersion==2 )((void) (0));
82077
82078 /* If setting the version fields to 1, do not automatically open the
82079 ** WAL connection, even if the version fields are currently set to 2.
82080 */
82081 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
82082 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL0x0020;
82083
82084 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
82085 if( rc==SQLITE_OK0 ){
82086 u8 *aData = pBt->pPage1->aData;
82087 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
82088 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
82089 if( rc==SQLITE_OK0 ){
82090 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
82091 if( rc==SQLITE_OK0 ){
82092 aData[18] = (u8)iVersion;
82093 aData[19] = (u8)iVersion;
82094 }
82095 }
82096 }
82097 }
82098
82099 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
82100 return rc;
82101}
82102
82103/*
82104** Return true if the cursor has a hint specified. This routine is
82105** only used from within assert() statements
82106*/
82107SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
82108 return (pCsr->hints & mask)!=0;
82109}
82110
82111/*
82112** Return true if the given Btree is read-only.
82113*/
82114SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *p){
82115 return (p->pBt->btsFlags & BTS_READ_ONLY0x0001)!=0;
82116}
82117
82118/*
82119** Return the size of the header added to each page by this module.
82120*/
82121SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage))(((sizeof(MemPage))+7)&~7); }
82122
82123/*
82124** If no transaction is active and the database is not a temp-db, clear
82125** the in-memory pager cache.
82126*/
82127SQLITE_PRIVATEstatic void sqlite3BtreeClearCache(Btree *p){
82128 BtShared *pBt = p->pBt;
82129 if( pBt->inTransaction==TRANS_NONE0 ){
82130 sqlite3PagerClearCache(pBt->pPager);
82131 }
82132}
82133
82134#if !defined(SQLITE_OMIT_SHARED_CACHE)
82135/*
82136** Return true if the Btree passed as the only argument is sharable.
82137*/
82138SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree *p){
82139 return p->sharable;
82140}
82141
82142/*
82143** Return the number of connections to the BtShared object accessed by
82144** the Btree handle passed as the only argument. For private caches
82145** this is always 1. For shared caches it may be 1 or greater.
82146*/
82147SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree *p){
82148 testcase( p->sharable );
82149 return p->pBt->nRef;
82150}
82151#endif
82152
82153/************** End of btree.c ***********************************************/
82154/************** Begin file backup.c ******************************************/
82155/*
82156** 2009 January 28
82157**
82158** The author disclaims copyright to this source code. In place of
82159** a legal notice, here is a blessing:
82160**
82161** May you do good and not evil.
82162** May you find forgiveness for yourself and forgive others.
82163** May you share freely, never taking more than you give.
82164**
82165*************************************************************************
82166** This file contains the implementation of the sqlite3_backup_XXX()
82167** API functions and the related features.
82168*/
82169/* #include "sqliteInt.h" */
82170/* #include "btreeInt.h" */
82171
82172/*
82173** Structure allocated for each backup operation.
82174*/
82175struct sqlite3_backup {
82176 sqlite3* pDestDb; /* Destination database handle */
82177 Btree *pDest; /* Destination b-tree file */
82178 u32 iDestSchema; /* Original schema cookie in destination */
82179 int bDestLocked; /* True once a write-transaction is open on pDest */
82180
82181 Pgno iNext; /* Page number of the next source page to copy */
82182 sqlite3* pSrcDb; /* Source database handle */
82183 Btree *pSrc; /* Source b-tree file */
82184
82185 int rc; /* Backup process error code */
82186
82187 /* These two variables are set by every call to backup_step(). They are
82188 ** read by calls to backup_remaining() and backup_pagecount().
82189 */
82190 Pgno nRemaining; /* Number of pages left to copy */
82191 Pgno nPagecount; /* Total number of pages to copy */
82192
82193 int isAttached; /* True once backup has been registered with pager */
82194 sqlite3_backup *pNext; /* Next backup associated with source pager */
82195};
82196
82197/*
82198** THREAD SAFETY NOTES:
82199**
82200** Once it has been created using backup_init(), a single sqlite3_backup
82201** structure may be accessed via two groups of thread-safe entry points:
82202**
82203** * Via the sqlite3_backup_XXX() API function backup_step() and
82204** backup_finish(). Both these functions obtain the source database
82205** handle mutex and the mutex associated with the source BtShared
82206** structure, in that order.
82207**
82208** * Via the BackupUpdate() and BackupRestart() functions, which are
82209** invoked by the pager layer to report various state changes in
82210** the page cache associated with the source database. The mutex
82211** associated with the source database BtShared structure will always
82212** be held when either of these functions are invoked.
82213**
82214** The other sqlite3_backup_XXX() API functions, backup_remaining() and
82215** backup_pagecount() are not thread-safe functions. If they are called
82216** while some other thread is calling backup_step() or backup_finish(),
82217** the values returned may be invalid. There is no way for a call to
82218** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
82219** or backup_pagecount().
82220**
82221** Depending on the SQLite configuration, the database handles and/or
82222** the Btree objects may have their own mutexes that require locking.
82223** Non-sharable Btrees (in-memory databases for example), do not have
82224** associated mutexes.
82225*/
82226
82227/*
82228** Return a pointer corresponding to database zDb (i.e. "main", "temp")
82229** in connection handle pDb. If such a database cannot be found, return
82230** a NULL pointer and write an error message to pErrorDb.
82231**
82232** If the "temp" database is requested, it may need to be opened by this
82233** function. If an error occurs while doing so, return 0 and write an
82234** error message to pErrorDb.
82235*/
82236static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
82237 int i = sqlite3FindDbName(pDb, zDb);
82238
82239 if( i==1 ){
82240 Parse sParse;
82241 int rc = 0;
82242 sqlite3ParseObjectInit(&sParse,pDb);
82243 if( sqlite3OpenTempDatabase(&sParse) ){
82244 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
82245 rc = SQLITE_ERROR1;
82246 }
82247 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
82248 sqlite3ParseObjectReset(&sParse);
82249 if( rc ){
82250 return 0;
82251 }
82252 }
82253
82254 if( i<0 ){
82255 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR1, "unknown database %s", zDb);
82256 return 0;
82257 }
82258
82259 return pDb->aDb[i].pBt;
82260}
82261
82262/*
82263** Attempt to set the page size of the destination to match the page size
82264** of the source.
82265*/
82266static int setDestPgsz(sqlite3_backup *p){
82267 int rc;
82268 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
82269 return rc;
82270}
82271
82272/*
82273** Check that there is no open read-transaction on the b-tree passed as the
82274** second argument. If there is not, return SQLITE_OK. Otherwise, if there
82275** is an open read-transaction, return SQLITE_ERROR and leave an error
82276** message in database handle db.
82277*/
82278static int checkReadTransaction(sqlite3 *db, Btree *p){
82279 if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE0 ){
82280 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "destination database is in use");
82281 return SQLITE_ERROR1;
82282 }
82283 return SQLITE_OK0;
82284}
82285
82286/*
82287** Create an sqlite3_backup process to copy the contents of zSrcDb from
82288** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
82289** a pointer to the new sqlite3_backup object.
82290**
82291** If an error occurs, NULL is returned and an error code and error message
82292** stored in database handle pDestDb.
82293*/
82294SQLITE_API sqlite3_backup *sqlite3_backup_init(
82295 sqlite3* pDestDb, /* Database to write to */
82296 const char *zDestDb, /* Name of database within pDestDb */
82297 sqlite3* pSrcDb, /* Database connection to read from */
82298 const char *zSrcDb /* Name of database within pSrcDb */
82299){
82300 sqlite3_backup *p; /* Value to return */
82301
82302#ifdef SQLITE_ENABLE_API_ARMOR1
82303 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
82304 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(82304);
82305 return 0;
82306 }
82307#endif
82308
82309 /* Lock the source database handle. The destination database
82310 ** handle is not locked in this routine, but it is locked in
82311 ** sqlite3_backup_step(). The user is required to ensure that no
82312 ** other thread accesses the destination handle for the duration
82313 ** of the backup operation. Any attempt to use the destination
82314 ** database connection while a backup is in progress may cause
82315 ** a malfunction or a deadlock.
82316 */
82317 sqlite3_mutex_enter(pSrcDb->mutex);
82318 sqlite3_mutex_enter(pDestDb->mutex);
82319
82320 if( pSrcDb==pDestDb ){
82321 sqlite3ErrorWithMsg(
82322 pDestDb, SQLITE_ERROR1, "source and destination must be distinct"
82323 );
82324 p = 0;
82325 }else {
82326 /* Allocate space for a new sqlite3_backup object...
82327 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
82328 ** call to sqlite3_backup_init() and is destroyed by a call to
82329 ** sqlite3_backup_finish(). */
82330 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
82331 if( !p ){
82332 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT7);
82333 }
82334 }
82335
82336 /* If the allocation succeeded, populate the new object. */
82337 if( p ){
82338 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
82339 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
82340 p->pDestDb = pDestDb;
82341 p->pSrcDb = pSrcDb;
82342 p->iNext = 1;
82343 p->isAttached = 0;
82344
82345 if( 0==p->pSrc || 0==p->pDest
82346 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK0
82347 ){
82348 /* One (or both) of the named databases did not exist or an OOM
82349 ** error was hit. Or there is a transaction open on the destination
82350 ** database. The error has already been written into the pDestDb
82351 ** handle. All that is left to do here is free the sqlite3_backup
82352 ** structure. */
82353 sqlite3_free(p);
82354 p = 0;
82355 }
82356 }
82357 if( p ){
82358 p->pSrc->nBackup++;
82359 }
82360
82361 sqlite3_mutex_leave(pDestDb->mutex);
82362 sqlite3_mutex_leave(pSrcDb->mutex);
82363 return p;
82364}
82365
82366/*
82367** Argument rc is an SQLite error code. Return true if this error is
82368** considered fatal if encountered during a backup operation. All errors
82369** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
82370*/
82371static int isFatalError(int rc){
82372 return (rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 && ALWAYS(rc!=SQLITE_LOCKED)(rc!=6));
82373}
82374
82375/*
82376** Parameter zSrcData points to a buffer containing the data for
82377** page iSrcPg from the source database. Copy this data into the
82378** destination database.
82379*/
82380static int backupOnePage(
82381 sqlite3_backup *p, /* Backup handle */
82382 Pgno iSrcPg, /* Source database page to backup */
82383 const u8 *zSrcData, /* Source database page data */
82384 int bUpdate /* True for an update, false otherwise */
82385){
82386 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
82387 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
82388 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
82389 const int nCopy = MIN(nSrcPgsz, nDestPgsz)((nSrcPgsz)<(nDestPgsz)?(nSrcPgsz):(nDestPgsz));
82390 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
82391 int rc = SQLITE_OK0;
82392 i64 iOff;
82393
82394 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 )((void) (0));
82395 assert( p->bDestLocked )((void) (0));
82396 assert( !isFatalError(p->rc) )((void) (0));
82397 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) )((void) (0));
82398 assert( zSrcData )((void) (0));
82399 assert( nSrcPgsz==nDestPgsz || sqlite3PagerIsMemdb(pDestPager)==0 )((void) (0));
82400
82401 /* This loop runs once for each destination page spanned by the source
82402 ** page. For each iteration, variable iOff is set to the byte offset
82403 ** of the destination page.
82404 */
82405 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK0 && iOff<iEnd; iOff+=nDestPgsz){
82406 DbPage *pDestPg = 0;
82407 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
82408 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
) continue;
82409 if( SQLITE_OK0==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
82410 && SQLITE_OK0==(rc = sqlite3PagerWrite(pDestPg))
82411 ){
82412 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
82413 u8 *zDestData = sqlite3PagerGetData(pDestPg);
82414 u8 *zOut = &zDestData[iOff%nDestPgsz];
82415
82416 /* Copy the data from the source page into the destination page.
82417 ** Then clear the Btree layer MemPage.isInit flag. Both this module
82418 ** and the pager code use this trick (clearing the first byte
82419 ** of the page 'extra' space to invalidate the Btree layers
82420 ** cached parse of the page). MemPage.isInit is marked
82421 ** "MUST BE FIRST" for this purpose.
82422 */
82423 memcpy(zOut, zIn, nCopy);
82424 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
82425 if( iOff==0 && bUpdate==0 ){
82426 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
82427 }
82428 }
82429 sqlite3PagerUnref(pDestPg);
82430 }
82431
82432 return rc;
82433}
82434
82435/*
82436** If pFile is currently larger than iSize bytes, then truncate it to
82437** exactly iSize bytes. If pFile is not larger than iSize bytes, then
82438** this function is a no-op.
82439**
82440** Return SQLITE_OK if everything is successful, or an SQLite error
82441** code if an error occurs.
82442*/
82443static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
82444 i64 iCurrent;
82445 int rc = sqlite3OsFileSize(pFile, &iCurrent);
82446 if( rc==SQLITE_OK0 && iCurrent>iSize ){
82447 rc = sqlite3OsTruncate(pFile, iSize);
82448 }
82449 return rc;
82450}
82451
82452/*
82453** Register this backup object with the associated source pager for
82454** callbacks when pages are changed or the cache invalidated.
82455*/
82456static void attachBackupObject(sqlite3_backup *p){
82457 sqlite3_backup **pp;
82458 assert( sqlite3BtreeHoldsMutex(p->pSrc) )((void) (0));
82459 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
82460 p->pNext = *pp;
82461 *pp = p;
82462 p->isAttached = 1;
82463}
82464
82465/*
82466** Copy nPage pages from the source b-tree to the destination.
82467*/
82468SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
82469 int rc;
82470 int destMode; /* Destination journal mode */
82471 int pgszSrc = 0; /* Source page size */
82472 int pgszDest = 0; /* Destination page size */
82473
82474#ifdef SQLITE_ENABLE_API_ARMOR1
82475 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(82475);
82476#endif
82477 sqlite3_mutex_enter(p->pSrcDb->mutex);
82478 sqlite3BtreeEnter(p->pSrc);
82479 if( p->pDestDb ){
82480 sqlite3_mutex_enter(p->pDestDb->mutex);
82481 }
82482
82483 rc = p->rc;
82484 if( !isFatalError(rc) ){
82485 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
82486 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
82487 int ii; /* Iterator variable */
82488 int nSrcPage = -1; /* Size of source db in pages */
82489 int bCloseTrans = 0; /* True if src db requires unlocking */
82490
82491 /* If the source pager is currently in a write-transaction, return
82492 ** SQLITE_BUSY immediately.
82493 */
82494 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE2 ){
82495 rc = SQLITE_BUSY5;
82496 }else{
82497 rc = SQLITE_OK0;
82498 }
82499
82500 /* If there is no open read-transaction on the source database, open
82501 ** one now. If a transaction is opened here, then it will be closed
82502 ** before this function exits.
82503 */
82504 if( rc==SQLITE_OK0 && SQLITE_TXN_NONE0==sqlite3BtreeTxnState(p->pSrc) ){
82505 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
82506 bCloseTrans = 1;
82507 }
82508
82509 /* If the destination database has not yet been locked (i.e. if this
82510 ** is the first call to backup_step() for the current backup operation),
82511 ** try to set its page size to the same as the source database. This
82512 ** is especially important on ZipVFS systems, as in that case it is
82513 ** not possible to create a database file that uses one page size by
82514 ** writing to it with another. */
82515 if( p->bDestLocked==0 && rc==SQLITE_OK0 && setDestPgsz(p)==SQLITE_NOMEM7 ){
82516 rc = SQLITE_NOMEM7;
82517 }
82518
82519 /* Lock the destination database, if it is not locked already. */
82520 if( SQLITE_OK0==rc && p->bDestLocked==0
82521 && SQLITE_OK0==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
82522 (int*)&p->iDestSchema))
82523 ){
82524 p->bDestLocked = 1;
82525 }
82526
82527 /* Do not allow backup if the destination database is in WAL mode
82528 ** and the page sizes are different between source and destination */
82529 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
82530 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
82531 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
82532 if( SQLITE_OK0==rc
82533 && (destMode==PAGER_JOURNALMODE_WAL5 || sqlite3PagerIsMemdb(pDestPager))
82534 && pgszSrc!=pgszDest
82535 ){
82536 rc = SQLITE_READONLY8;
82537 }
82538
82539 /* Now that there is a read-lock on the source database, query the
82540 ** source pager for the number of pages in the database.
82541 */
82542 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
82543 assert( nSrcPage>=0 )((void) (0));
82544 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
82545 const Pgno iSrcPg = p->iNext; /* Source page number */
82546 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt)((Pgno)((sqlite3PendingByte/((p->pSrc->pBt)->pageSize
))+1))
){
82547 DbPage *pSrcPg; /* Source page object */
82548 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY0x02);
82549 if( rc==SQLITE_OK0 ){
82550 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
82551 sqlite3PagerUnref(pSrcPg);
82552 }
82553 }
82554 p->iNext++;
82555 }
82556 if( rc==SQLITE_OK0 ){
82557 p->nPagecount = nSrcPage;
82558 p->nRemaining = nSrcPage+1-p->iNext;
82559 if( p->iNext>(Pgno)nSrcPage ){
82560 rc = SQLITE_DONE101;
82561 }else if( !p->isAttached ){
82562 attachBackupObject(p);
82563 }
82564 }
82565
82566 /* Update the schema version field in the destination database. This
82567 ** is to make sure that the schema-version really does change in
82568 ** the case where the source and destination databases have the
82569 ** same schema version.
82570 */
82571 if( rc==SQLITE_DONE101 ){
82572 if( nSrcPage==0 ){
82573 rc = sqlite3BtreeNewDb(p->pDest);
82574 nSrcPage = 1;
82575 }
82576 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
82577 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
82578 }
82579 if( rc==SQLITE_OK0 ){
82580 if( p->pDestDb ){
82581 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
82582 }
82583 if( destMode==PAGER_JOURNALMODE_WAL5 ){
82584 rc = sqlite3BtreeSetVersion(p->pDest, 2);
82585 }
82586 }
82587 if( rc==SQLITE_OK0 ){
82588 int nDestTruncate;
82589 /* Set nDestTruncate to the final number of pages in the destination
82590 ** database. The complication here is that the destination page
82591 ** size may be different to the source page size.
82592 **
82593 ** If the source page size is smaller than the destination page size,
82594 ** round up. In this case the call to sqlite3OsTruncate() below will
82595 ** fix the size of the file. However it is important to call
82596 ** sqlite3PagerTruncateImage() here so that any pages in the
82597 ** destination file that lie beyond the nDestTruncate page mark are
82598 ** journalled by PagerCommitPhaseOne() before they are destroyed
82599 ** by the file truncation.
82600 */
82601 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) )((void) (0));
82602 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) )((void) (0));
82603 if( pgszSrc<pgszDest ){
82604 int ratio = pgszDest/pgszSrc;
82605 nDestTruncate = (nSrcPage+ratio-1)/ratio;
82606 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
82607 nDestTruncate--;
82608 }
82609 }else{
82610 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
82611 }
82612 assert( nDestTruncate>0 )((void) (0));
82613
82614 if( pgszSrc<pgszDest ){
82615 /* If the source page-size is smaller than the destination page-size,
82616 ** two extra things may need to happen:
82617 **
82618 ** * The destination may need to be truncated, and
82619 **
82620 ** * Data stored on the pages immediately following the
82621 ** pending-byte page in the source database may need to be
82622 ** copied into the destination database.
82623 */
82624 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
82625 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
82626 Pgno iPg;
82627 int nDstPage;
82628 i64 iOff;
82629 i64 iEnd;
82630
82631 assert( pFile )((void) (0));
82632 assert( nDestTruncate==0((void) (0))
82633 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (((void) (0))
82634 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)((void) (0))
82635 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest((void) (0))
82636 ))((void) (0));
82637
82638 /* This block ensures that all data required to recreate the original
82639 ** database has been stored in the journal for pDestPager and the
82640 ** journal synced to disk. So at this point we may safely modify
82641 ** the database file in any way, knowing that if a power failure
82642 ** occurs, the original database will be reconstructed from the
82643 ** journal file. */
82644 sqlite3PagerPagecount(pDestPager, &nDstPage);
82645 for(iPg=nDestTruncate; rc==SQLITE_OK0 && iPg<=(Pgno)nDstPage; iPg++){
82646 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
82647 DbPage *pPg;
82648 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
82649 if( rc==SQLITE_OK0 ){
82650 rc = sqlite3PagerWrite(pPg);
82651 sqlite3PagerUnref(pPg);
82652 }
82653 }
82654 }
82655 if( rc==SQLITE_OK0 ){
82656 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
82657 }
82658
82659 /* Write the extra pages and truncate the database file as required */
82660 iEnd = MIN(PENDING_BYTE + pgszDest, iSize)((sqlite3PendingByte + pgszDest)<(iSize)?(sqlite3PendingByte
+ pgszDest):(iSize))
;
82661 for(
82662 iOff=PENDING_BYTEsqlite3PendingByte+pgszSrc;
82663 rc==SQLITE_OK0 && iOff<iEnd;
82664 iOff+=pgszSrc
82665 ){
82666 PgHdr *pSrcPg = 0;
82667 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
82668 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
82669 if( rc==SQLITE_OK0 ){
82670 u8 *zData = sqlite3PagerGetData(pSrcPg);
82671 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
82672 }
82673 sqlite3PagerUnref(pSrcPg);
82674 }
82675 if( rc==SQLITE_OK0 ){
82676 rc = backupTruncateFile(pFile, iSize);
82677 }
82678
82679 /* Sync the database file to disk. */
82680 if( rc==SQLITE_OK0 ){
82681 rc = sqlite3PagerSync(pDestPager, 0);
82682 }
82683 }else{
82684 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
82685 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
82686 }
82687
82688 /* Finish committing the transaction to the destination database. */
82689 if( SQLITE_OK0==rc
82690 && SQLITE_OK0==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
82691 ){
82692 rc = SQLITE_DONE101;
82693 }
82694 }
82695 }
82696
82697 /* If bCloseTrans is true, then this function opened a read transaction
82698 ** on the source database. Close the read transaction here. There is
82699 ** no need to check the return values of the btree methods here, as
82700 ** "committing" a read-only transaction cannot fail.
82701 */
82702 if( bCloseTrans ){
82703 TESTONLY( int rc2 );
82704 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
82705 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
82706 assert( rc2==SQLITE_OK )((void) (0));
82707 }
82708
82709 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
82710 rc = SQLITE_NOMEM_BKPT7;
82711 }
82712 p->rc = rc;
82713 }
82714 if( p->pDestDb ){
82715 sqlite3_mutex_leave(p->pDestDb->mutex);
82716 }
82717 sqlite3BtreeLeave(p->pSrc);
82718 sqlite3_mutex_leave(p->pSrcDb->mutex);
82719 return rc;
82720}
82721
82722/*
82723** Release all resources associated with an sqlite3_backup* handle.
82724*/
82725SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
82726 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
82727 sqlite3 *pSrcDb; /* Source database connection */
82728 int rc; /* Value to return */
82729
82730 /* Enter the mutexes */
82731 if( p==0 ) return SQLITE_OK0;
82732 pSrcDb = p->pSrcDb;
82733 sqlite3_mutex_enter(pSrcDb->mutex);
82734 sqlite3BtreeEnter(p->pSrc);
82735 if( p->pDestDb ){
82736 sqlite3_mutex_enter(p->pDestDb->mutex);
82737 }
82738
82739 /* Detach this backup from the source pager. */
82740 if( p->pDestDb ){
82741 p->pSrc->nBackup--;
82742 }
82743 if( p->isAttached ){
82744 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
82745 assert( pp!=0 )((void) (0));
82746 while( *pp!=p ){
82747 pp = &(*pp)->pNext;
82748 assert( pp!=0 )((void) (0));
82749 }
82750 *pp = p->pNext;
82751 }
82752
82753 /* If a transaction is still open on the Btree, roll it back. */
82754 sqlite3BtreeRollback(p->pDest, SQLITE_OK0, 0);
82755
82756 /* Set the error code of the destination database handle. */
82757 rc = (p->rc==SQLITE_DONE101) ? SQLITE_OK0 : p->rc;
82758 if( p->pDestDb ){
82759 sqlite3Error(p->pDestDb, rc);
82760
82761 /* Exit the mutexes and free the backup context structure. */
82762 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
82763 }
82764 sqlite3BtreeLeave(p->pSrc);
82765 if( p->pDestDb ){
82766 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
82767 ** call to sqlite3_backup_init() and is destroyed by a call to
82768 ** sqlite3_backup_finish(). */
82769 sqlite3_free(p);
82770 }
82771 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
82772 return rc;
82773}
82774
82775/*
82776** Return the number of pages still to be backed up as of the most recent
82777** call to sqlite3_backup_step().
82778*/
82779SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
82780#ifdef SQLITE_ENABLE_API_ARMOR1
82781 if( p==0 ){
82782 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(82782);
82783 return 0;
82784 }
82785#endif
82786 return p->nRemaining;
82787}
82788
82789/*
82790** Return the total number of pages in the source database as of the most
82791** recent call to sqlite3_backup_step().
82792*/
82793SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
82794#ifdef SQLITE_ENABLE_API_ARMOR1
82795 if( p==0 ){
82796 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(82796);
82797 return 0;
82798 }
82799#endif
82800 return p->nPagecount;
82801}
82802
82803/*
82804** This function is called after the contents of page iPage of the
82805** source database have been modified. If page iPage has already been
82806** copied into the destination database, then the data written to the
82807** destination is now invalidated. The destination copy of iPage needs
82808** to be updated with the new data before the backup operation is
82809** complete.
82810**
82811** It is assumed that the mutex associated with the BtShared object
82812** corresponding to the source database is held when this function is
82813** called.
82814*/
82815static SQLITE_NOINLINE__attribute__((noinline)) void backupUpdate(
82816 sqlite3_backup *p,
82817 Pgno iPage,
82818 const u8 *aData
82819){
82820 assert( p!=0 )((void) (0));
82821 do{
82822 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
82823 if( !isFatalError(p->rc) && iPage<p->iNext ){
82824 /* The backup process p has already copied page iPage. But now it
82825 ** has been modified by a transaction on the source pager. Copy
82826 ** the new data into the backup.
82827 */
82828 int rc;
82829 assert( p->pDestDb )((void) (0));
82830 sqlite3_mutex_enter(p->pDestDb->mutex);
82831 rc = backupOnePage(p, iPage, aData, 1);
82832 sqlite3_mutex_leave(p->pDestDb->mutex);
82833 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED )((void) (0));
82834 if( rc!=SQLITE_OK0 ){
82835 p->rc = rc;
82836 }
82837 }
82838 }while( (p = p->pNext)!=0 );
82839}
82840SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
82841 if( pBackup ) backupUpdate(pBackup, iPage, aData);
82842}
82843
82844/*
82845** Restart the backup process. This is called when the pager layer
82846** detects that the database has been modified by an external database
82847** connection. In this case there is no way of knowing which of the
82848** pages that have been copied into the destination database are still
82849** valid and which are not, so the entire process needs to be restarted.
82850**
82851** It is assumed that the mutex associated with the BtShared object
82852** corresponding to the source database is held when this function is
82853** called.
82854*/
82855SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *pBackup){
82856 sqlite3_backup *p; /* Iterator variable */
82857 for(p=pBackup; p; p=p->pNext){
82858 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
82859 p->iNext = 1;
82860 }
82861}
82862
82863#ifndef SQLITE_OMIT_VACUUM
82864/*
82865** Copy the complete content of pBtFrom into pBtTo. A transaction
82866** must be active for both files.
82867**
82868** The size of file pTo may be reduced by this operation. If anything
82869** goes wrong, the transaction on pTo is rolled back. If successful, the
82870** transaction is committed before returning.
82871*/
82872SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
82873 int rc;
82874 sqlite3_file *pFd; /* File descriptor for database pTo */
82875 sqlite3_backup b;
82876 sqlite3BtreeEnter(pTo);
82877 sqlite3BtreeEnter(pFrom);
82878
82879 assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE )((void) (0));
82880 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
82881 if( pFd->pMethods ){
82882 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
82883 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE11, &nByte);
82884 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
82885 if( rc ) goto copy_finished;
82886 }
82887
82888 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
82889 ** to 0. This is used by the implementations of sqlite3_backup_step()
82890 ** and sqlite3_backup_finish() to detect that they are being called
82891 ** from this function, not directly by the user.
82892 */
82893 memset(&b, 0, sizeof(b));
82894 b.pSrcDb = pFrom->db;
82895 b.pSrc = pFrom;
82896 b.pDest = pTo;
82897 b.iNext = 1;
82898
82899 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
82900 ** file. By passing this as the number of pages to copy to
82901 ** sqlite3_backup_step(), we can guarantee that the copy finishes
82902 ** within a single call (unless an error occurs). The assert() statement
82903 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
82904 ** or an error code. */
82905 sqlite3_backup_step(&b, 0x7FFFFFFF);
82906 assert( b.rc!=SQLITE_OK )((void) (0));
82907
82908 rc = sqlite3_backup_finish(&b);
82909 if( rc==SQLITE_OK0 ){
82910 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED0x0002;
82911 }else{
82912 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
82913 }
82914
82915 assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE )((void) (0));
82916copy_finished:
82917 sqlite3BtreeLeave(pFrom);
82918 sqlite3BtreeLeave(pTo);
82919 return rc;
82920}
82921#endif /* SQLITE_OMIT_VACUUM */
82922
82923/************** End of backup.c **********************************************/
82924/************** Begin file vdbemem.c *****************************************/
82925/*
82926** 2004 May 26
82927**
82928** The author disclaims copyright to this source code. In place of
82929** a legal notice, here is a blessing:
82930**
82931** May you do good and not evil.
82932** May you find forgiveness for yourself and forgive others.
82933** May you share freely, never taking more than you give.
82934**
82935*************************************************************************
82936**
82937** This file contains code use to manipulate "Mem" structure. A "Mem"
82938** stores a single value in the VDBE. Mem is an opaque structure visible
82939** only within the VDBE. Interface routines refer to a Mem using the
82940** name sqlite_value
82941*/
82942/* #include "sqliteInt.h" */
82943/* #include "vdbeInt.h" */
82944
82945/* True if X is a power of two. 0 is considered a power of two here.
82946** In other words, return true if X has at most one bit set.
82947*/
82948#define ISPOWEROF2(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
82949
82950#ifdef SQLITE_DEBUG
82951/*
82952** Check invariants on a Mem object.
82953**
82954** This routine is intended for use inside of assert() statements, like
82955** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
82956*/
82957SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem *p){
82958 /* If MEM_Dyn is set then Mem.xDel!=0.
82959 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
82960 */
82961 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 )((void) (0));
82962
82963 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
82964 ** ensure that if Mem.szMalloc>0 then it is safe to do
82965 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
82966 ** That saves a few cycles in inner loops. */
82967 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 )((void) (0));
82968
82969 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
82970 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) )((void) (0));
82971
82972 if( p->flags & MEM_Null0x0001 ){
82973 /* Cannot be both MEM_Null and some other type */
82974 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 )((void) (0));
82975
82976 /* If MEM_Null is set, then either the value is a pure NULL (the usual
82977 ** case) or it is a pointer set using sqlite3_bind_pointer() or
82978 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
82979 ** set.
82980 */
82981 if( (p->flags & (MEM_Term0x0200|MEM_Subtype0x0800))==(MEM_Term0x0200|MEM_Subtype0x0800) ){
82982 /* This is a pointer type. There may be a flag to indicate what to
82983 ** do with the pointer. */
82984 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
82985 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
82986 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 )((void) (0));
82987
82988 /* No other bits set */
82989 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind((void) (0))
82990 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 )((void) (0));
82991 }else{
82992 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
82993 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
82994 }
82995 }else{
82996 /* The MEM_Cleared bit is only allowed on NULLs */
82997 assert( (p->flags & MEM_Cleared)==0 )((void) (0));
82998 }
82999
83000 /* The szMalloc field holds the correct memory allocation size */
83001 assert( p->szMalloc==0((void) (0))
83002 || (p->flags==MEM_Undefined((void) (0))
83003 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))((void) (0))
83004 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc))((void) (0));
83005
83006 /* If p holds a string or blob, the Mem.z must point to exactly
83007 ** one of the following:
83008 **
83009 ** (1) Memory in Mem.zMalloc and managed by the Mem object
83010 ** (2) Memory to be freed using Mem.xDel
83011 ** (3) An ephemeral string or blob
83012 ** (4) A static string or blob
83013 */
83014 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010)) && p->n>0 ){
83015 assert(((void) (0))
83016 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +((void) (0))
83017 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
83018 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
83019 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1((void) (0))
83020 )((void) (0));
83021 }
83022 return 1;
83023}
83024#endif
83025
83026/*
83027** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
83028** into a buffer.
83029*/
83030static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
83031 StrAccum acc;
83032 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
83033 assert( sz>22 )((void) (0));
83034 if( p->flags & MEM_Int0x0004 ){
83035#if GCC_VERSION(4*1000000+2*1000+1)>=7000000
83036 /* Work-around for GCC bug
83037 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
83038 i64 x;
83039 assert( (p->flags&MEM_Int)*2==sizeof(x) )((void) (0));
83040 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int0x0004)*2);
83041 p->n = sqlite3Int64ToText(x, zBuf);
83042#else
83043 p->n = sqlite3Int64ToText(p->u.i, zBuf);
83044#endif
83045 }else{
83046 sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
83047 sqlite3_str_appendf(&acc, "%!.15g",
83048 (p->flags & MEM_IntReal0x0020)!=0 ? (double)p->u.i : p->u.r);
83049 assert( acc.zText==zBuf && acc.mxAlloc<=0 )((void) (0));
83050 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
83051 p->n = acc.nChar;
83052 }
83053}
83054
83055#ifdef SQLITE_DEBUG
83056/*
83057** Validity checks on pMem. pMem holds a string.
83058**
83059** (1) Check that string value of pMem agrees with its integer or real value.
83060** (2) Check that the string is correctly zero terminated
83061**
83062** A single int or real value always converts to the same strings. But
83063** many different strings can be converted into the same int or real.
83064** If a table contains a numeric value and an index is based on the
83065** corresponding string value, then it is important that the string be
83066** derived from the numeric value, not the other way around, to ensure
83067** that the index and table are consistent. See ticket
83068** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
83069** an example.
83070**
83071** This routine looks at pMem to verify that if it has both a numeric
83072** representation and a string representation then the string rep has
83073** been derived from the numeric and not the other way around. It returns
83074** true if everything is ok and false if there is a problem.
83075**
83076** This routine is for use inside of assert() statements only.
83077*/
83078SQLITE_PRIVATEstatic int sqlite3VdbeMemValidStrRep(Mem *p){
83079 Mem tmp;
83080 char zBuf[100];
83081 char *z;
83082 int i, j, incr;
83083 if( (p->flags & MEM_Str0x0002)==0 ) return 1;
83084 if( p->db && p->db->mallocFailed ) return 1;
83085 if( p->flags & MEM_Term0x0200 ){
83086 /* Insure that the string is properly zero-terminated. Pay particular
83087 ** attention to the case where p->n is odd */
83088 if( p->szMalloc>0 && p->z==p->zMalloc ){
83089 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 )((void) (0));
83090 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 )((void) (0));
83091 }
83092 assert( p->z[p->n]==0 )((void) (0));
83093 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 )((void) (0));
83094 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 )((void) (0));
83095 }
83096 if( (p->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))==0 ) return 1;
83097 memcpy(&tmp, p, sizeof(tmp));
83098 vdbeMemRenderNum(sizeof(zBuf), zBuf, &tmp);
83099 z = p->z;
83100 i = j = 0;
83101 incr = 1;
83102 if( p->enc!=SQLITE_UTF81 ){
83103 incr = 2;
83104 if( p->enc==SQLITE_UTF16BE3 ) z++;
83105 }
83106 while( zBuf[j] ){
83107 if( zBuf[j++]!=z[i] ) return 0;
83108 i += incr;
83109 }
83110 return 1;
83111}
83112#endif /* SQLITE_DEBUG */
83113
83114/*
83115** If pMem is an object with a valid string representation, this routine
83116** ensures the internal encoding for the string representation is
83117** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
83118**
83119** If pMem is not a string object, or the encoding of the string
83120** representation is already stored using the requested encoding, then this
83121** routine is a no-op.
83122**
83123** SQLITE_OK is returned if the conversion is successful (or not required).
83124** SQLITE_NOMEM may be returned if a malloc() fails during conversion
83125** between formats.
83126*/
83127SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
83128#ifndef SQLITE_OMIT_UTF16
83129 int rc;
83130#endif
83131 assert( pMem!=0 )((void) (0));
83132 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83133 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE((void) (0))
83134 || desiredEnc==SQLITE_UTF16BE )((void) (0));
83135 if( !(pMem->flags&MEM_Str0x0002) ){
83136 pMem->enc = desiredEnc;
83137 return SQLITE_OK0;
83138 }
83139 if( pMem->enc==desiredEnc ){
83140 return SQLITE_OK0;
83141 }
83142 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83143#ifdef SQLITE_OMIT_UTF16
83144 return SQLITE_ERROR1;
83145#else
83146
83147 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
83148 ** then the encoding of the value may not have changed.
83149 */
83150 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
83151 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM)((void) (0));
83152 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc)((void) (0));
83153 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc)((void) (0));
83154 return rc;
83155#endif
83156}
83157
83158/*
83159** Make sure pMem->z points to a writable allocation of at least n bytes.
83160**
83161** If the bPreserve argument is true, then copy of the content of
83162** pMem->z into the new allocation. pMem must be either a string or
83163** blob if bPreserve is true. If bPreserve is false, any prior content
83164** in pMem->z is discarded.
83165*/
83166SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
83167 assert( sqlite3VdbeCheckMemInvariants(pMem) )((void) (0));
83168 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83169 testcase( pMem->db==0 );
83170
83171 /* If the bPreserve flag is set to true, then the memory cell must already
83172 ** contain a valid string or blob value. */
83173 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) )((void) (0));
83174 testcase( bPreserve && pMem->z==0 );
83175
83176 assert( pMem->szMalloc==0((void) (0))
83177 || (pMem->flags==MEM_Undefined((void) (0))
83178 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))((void) (0))
83179 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc))((void) (0));
83180 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
83181 if( pMem->db ){
83182 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
83183 }else{
83184 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
83185 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
83186 pMem->z = pMem->zMalloc;
83187 }
83188 bPreserve = 0;
83189 }else{
83190 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
83191 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
83192 }
83193 if( pMem->zMalloc==0 ){
83194 sqlite3VdbeMemSetNull(pMem);
83195 pMem->z = 0;
83196 pMem->szMalloc = 0;
83197 return SQLITE_NOMEM_BKPT7;
83198 }else{
83199 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
83200 }
83201
83202 if( bPreserve && pMem->z ){
83203 assert( pMem->z!=pMem->zMalloc )((void) (0));
83204 memcpy(pMem->zMalloc, pMem->z, pMem->n);
83205 }
83206 if( (pMem->flags&MEM_Dyn0x1000)!=0 ){
83207 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC )((void) (0));
83208 pMem->xDel((void *)(pMem->z));
83209 }
83210
83211 pMem->z = pMem->zMalloc;
83212 pMem->flags &= ~(MEM_Dyn0x1000|MEM_Ephem0x4000|MEM_Static0x2000);
83213 return SQLITE_OK0;
83214}
83215
83216/*
83217** Change the pMem->zMalloc allocation to be at least szNew bytes.
83218** If pMem->zMalloc already meets or exceeds the requested size, this
83219** routine is a no-op.
83220**
83221** Any prior string or blob content in the pMem object may be discarded.
83222** The pMem->xDel destructor is called, if it exists. Though MEM_Str
83223** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
83224** and MEM_Null values are preserved.
83225**
83226** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
83227** if unable to complete the resizing.
83228*/
83229SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
83230 assert( CORRUPT_DB || szNew>0 )((void) (0));
83231 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 )((void) (0));
83232 if( pMem->szMalloc<szNew ){
83233 return sqlite3VdbeMemGrow(pMem, szNew, 0);
83234 }
83235 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
83236 pMem->z = pMem->zMalloc;
83237 pMem->flags &= (MEM_Null0x0001|MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
83238 return SQLITE_OK0;
83239}
83240
83241/*
83242** If pMem is already a string, detect if it is a zero-terminated
83243** string, or make it into one if possible, and mark it as such.
83244**
83245** This is an optimization. Correct operation continues even if
83246** this routine is a no-op.
83247*/
83248SQLITE_PRIVATEstatic void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){
83249 if( (pMem->flags & (MEM_Str0x0002|MEM_Term0x0200|MEM_Ephem0x4000|MEM_Static0x2000))!=MEM_Str0x0002 ){
83250 /* pMem must be a string, and it cannot be an ephemeral or static string */
83251 return;
83252 }
83253 if( pMem->enc!=SQLITE_UTF81 ) return;
83254 if( NEVER(pMem->z==0)(pMem->z==0) ) return;
83255 if( pMem->flags & MEM_Dyn0x1000 ){
83256 if( pMem->xDel==sqlite3_free
83257 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
83258 ){
83259 pMem->z[pMem->n] = 0;
83260 pMem->flags |= MEM_Term0x0200;
83261 return;
83262 }
83263 if( pMem->xDel==sqlite3RCStrUnref ){
83264 /* Blindly assume that all RCStr objects are zero-terminated */
83265 pMem->flags |= MEM_Term0x0200;
83266 return;
83267 }
83268 }else if( pMem->szMalloc >= pMem->n+1 ){
83269 pMem->z[pMem->n] = 0;
83270 pMem->flags |= MEM_Term0x0200;
83271 return;
83272 }
83273}
83274
83275/*
83276** It is already known that pMem contains an unterminated string.
83277** Add the zero terminator.
83278**
83279** Three bytes of zero are added. In this way, there is guaranteed
83280** to be a double-zero byte at an even byte boundary in order to
83281** terminate a UTF16 string, even if the initial size of the buffer
83282** is an odd number of bytes.
83283*/
83284static SQLITE_NOINLINE__attribute__((noinline)) int vdbeMemAddTerminator(Mem *pMem){
83285 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
83286 return SQLITE_NOMEM_BKPT7;
83287 }
83288 pMem->z[pMem->n] = 0;
83289 pMem->z[pMem->n+1] = 0;
83290 pMem->z[pMem->n+2] = 0;
83291 pMem->flags |= MEM_Term0x0200;
83292 return SQLITE_OK0;
83293}
83294
83295/*
83296** Change pMem so that its MEM_Str or MEM_Blob value is stored in
83297** MEM.zMalloc, where it can be safely written.
83298**
83299** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
83300*/
83301SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem *pMem){
83302 assert( pMem!=0 )((void) (0));
83303 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83304 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83305 if( (pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 ){
83306 if( ExpandBlob(pMem)(((pMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pMem)
:0)
) return SQLITE_NOMEM7;
83307 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
83308 int rc = vdbeMemAddTerminator(pMem);
83309 if( rc ) return rc;
83310 }
83311 }
83312 pMem->flags &= ~MEM_Ephem0x4000;
83313#ifdef SQLITE_DEBUG
83314 pMem->pScopyFrom = 0;
83315#endif
83316
83317 return SQLITE_OK0;
83318}
83319
83320/*
83321** If the given Mem* has a zero-filled tail, turn it into an ordinary
83322** blob stored in dynamically allocated space.
83323*/
83324#ifndef SQLITE_OMIT_INCRBLOB
83325SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *pMem){
83326 int nByte;
83327 assert( pMem!=0 )((void) (0));
83328 assert( pMem->flags & MEM_Zero )((void) (0));
83329 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) )((void) (0));
83330 testcase( sqlite3_value_nochange(pMem) );
83331 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83332 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83333
83334 /* Set nByte to the number of bytes required to store the expanded blob. */
83335 nByte = pMem->n + pMem->u.nZero;
83336 if( nByte<=0 ){
83337 if( (pMem->flags & MEM_Blob0x0010)==0 ) return SQLITE_OK0;
83338 nByte = 1;
83339 }
83340 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
83341 return SQLITE_NOMEM_BKPT7;
83342 }
83343 assert( pMem->z!=0 )((void) (0));
83344 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte )((void) (0));
83345
83346 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
83347 pMem->n += pMem->u.nZero;
83348 pMem->flags &= ~(MEM_Zero0x0400|MEM_Term0x0200);
83349 return SQLITE_OK0;
83350}
83351#endif
83352
83353/*
83354** Make sure the given Mem is \u0000 terminated.
83355*/
83356SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem *pMem){
83357 assert( pMem!=0 )((void) (0));
83358 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83359 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
83360 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
83361 if( (pMem->flags & (MEM_Term0x0200|MEM_Str0x0002))!=MEM_Str0x0002 ){
83362 return SQLITE_OK0; /* Nothing to do */
83363 }else{
83364 return vdbeMemAddTerminator(pMem);
83365 }
83366}
83367
83368/*
83369** Add MEM_Str to the set of representations for the given Mem. This
83370** routine is only called if pMem is a number of some kind, not a NULL
83371** or a BLOB.
83372**
83373** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
83374** if bForce is true but are retained if bForce is false.
83375**
83376** A MEM_Null value will never be passed to this function. This function is
83377** used for converting values to text for returning to the user (i.e. via
83378** sqlite3_value_text()), or for ensuring that values to be used as btree
83379** keys are strings. In the former case a NULL pointer is returned the
83380** user and the latter is an internal programming error.
83381*/
83382SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
83383 const int nByte = 32;
83384
83385 assert( pMem!=0 )((void) (0));
83386 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83387 assert( !(pMem->flags&MEM_Zero) )((void) (0));
83388 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) )((void) (0));
83389 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
83390 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83391 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
83392
83393
83394 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
83395 pMem->enc = 0;
83396 return SQLITE_NOMEM_BKPT7;
83397 }
83398
83399 vdbeMemRenderNum(nByte, pMem->z, pMem);
83400 assert( pMem->z!=0 )((void) (0));
83401 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) )((void) (0));
83402 pMem->enc = SQLITE_UTF81;
83403 pMem->flags |= MEM_Str0x0002|MEM_Term0x0200;
83404 if( bForce ) pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
83405 sqlite3VdbeChangeEncoding(pMem, enc);
83406 return SQLITE_OK0;
83407}
83408
83409/*
83410** Memory cell pMem contains the context of an aggregate function.
83411** This routine calls the finalize method for that function. The
83412** result of the aggregate is stored back into pMem.
83413**
83414** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
83415** otherwise.
83416*/
83417SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
83418 sqlite3_context ctx;
83419 Mem t;
83420 assert( pFunc!=0 )((void) (0));
83421 assert( pMem!=0 )((void) (0));
83422 assert( pMem->db!=0 )((void) (0));
83423 assert( pFunc->xFinalize!=0 )((void) (0));
83424 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef )((void) (0));
83425 assert( sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83426 memset(&ctx, 0, sizeof(ctx));
83427 memset(&t, 0, sizeof(t));
83428 t.flags = MEM_Null0x0001;
83429 t.db = pMem->db;
83430 ctx.pOut = &t;
83431 ctx.pMem = pMem;
83432 ctx.pFunc = pFunc;
83433 ctx.enc = ENC(t.db)((t.db)->enc);
83434 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
83435 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
83436 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
83437 memcpy(pMem, &t, sizeof(t));
83438 return ctx.isError;
83439}
83440
83441/*
83442** Memory cell pAccum contains the context of an aggregate function.
83443** This routine calls the xValue method for that function and stores
83444** the results in memory cell pMem.
83445**
83446** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
83447** otherwise.
83448*/
83449#ifndef SQLITE_OMIT_WINDOWFUNC
83450SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
83451 sqlite3_context ctx;
83452 assert( pFunc!=0 )((void) (0));
83453 assert( pFunc->xValue!=0 )((void) (0));
83454 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef )((void) (0));
83455 assert( pAccum->db!=0 )((void) (0));
83456 assert( sqlite3_mutex_held(pAccum->db->mutex) )((void) (0));
83457 memset(&ctx, 0, sizeof(ctx));
83458 sqlite3VdbeMemSetNull(pOut);
83459 ctx.pOut = pOut;
83460 ctx.pMem = pAccum;
83461 ctx.pFunc = pFunc;
83462 ctx.enc = ENC(pAccum->db)((pAccum->db)->enc);
83463 pFunc->xValue(&ctx);
83464 return ctx.isError;
83465}
83466#endif /* SQLITE_OMIT_WINDOWFUNC */
83467
83468/*
83469** If the memory cell contains a value that must be freed by
83470** invoking the external callback in Mem.xDel, then this routine
83471** will free that value. It also sets Mem.flags to MEM_Null.
83472**
83473** This is a helper routine for sqlite3VdbeMemSetNull() and
83474** for sqlite3VdbeMemRelease(). Use those other routines as the
83475** entry point for releasing Mem resources.
83476*/
83477static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClearExternAndSetNull(Mem *p){
83478 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
83479 assert( VdbeMemDynamic(p) )((void) (0));
83480 if( p->flags&MEM_Agg0x8000 ){
83481 sqlite3VdbeMemFinalize(p, p->u.pDef);
83482 assert( (p->flags & MEM_Agg)==0 )((void) (0));
83483 testcase( p->flags & MEM_Dyn );
83484 }
83485 if( p->flags&MEM_Dyn0x1000 ){
83486 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 )((void) (0));
83487 p->xDel((void *)p->z);
83488 }
83489 p->flags = MEM_Null0x0001;
83490}
83491
83492/*
83493** Release memory held by the Mem p, both external memory cleared
83494** by p->xDel and memory in p->zMalloc.
83495**
83496** This is a helper routine invoked by sqlite3VdbeMemRelease() in
83497** the unusual case where there really is memory in p that needs
83498** to be freed.
83499*/
83500static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClear(Mem *p){
83501 if( VdbeMemDynamic(p)(((p)->flags&(0x8000|0x1000))!=0) ){
83502 vdbeMemClearExternAndSetNull(p);
83503 }
83504 if( p->szMalloc ){
83505 sqlite3DbFreeNN(p->db, p->zMalloc);
83506 p->szMalloc = 0;
83507 }
83508 p->z = 0;
83509}
83510
83511/*
83512** Release any memory resources held by the Mem. Both the memory that is
83513** free by Mem.xDel and the Mem.zMalloc allocation are freed.
83514**
83515** Use this routine prior to clean up prior to abandoning a Mem, or to
83516** reset a Mem back to its minimum memory utilization.
83517**
83518** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
83519** prior to inserting new content into the Mem.
83520*/
83521SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p){
83522 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
83523 if( VdbeMemDynamic(p)(((p)->flags&(0x8000|0x1000))!=0) || p->szMalloc ){
83524 vdbeMemClear(p);
83525 }
83526}
83527
83528/* Like sqlite3VdbeMemRelease() but faster for cases where we
83529** know in advance that the Mem is not MEM_Dyn or MEM_Agg.
83530*/
83531SQLITE_PRIVATEstatic void sqlite3VdbeMemReleaseMalloc(Mem *p){
83532 assert( !VdbeMemDynamic(p) )((void) (0));
83533 if( p->szMalloc ) vdbeMemClear(p);
83534}
83535
83536/*
83537** Return some kind of integer value which is the best we can do
83538** at representing the value that *pMem describes as an integer.
83539** If pMem is an integer, then the value is exact. If pMem is
83540** a floating-point then the value returned is the integer part.
83541** If pMem is a string or blob, then we make an attempt to convert
83542** it into an integer and return that. If pMem represents an
83543** an SQL-NULL value, return 0.
83544**
83545** If pMem represents a string value, its encoding might be changed.
83546*/
83547static SQLITE_NOINLINE__attribute__((noinline)) i64 memIntValue(const Mem *pMem){
83548 i64 value = 0;
83549 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
83550 return value;
83551}
83552SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(const Mem *pMem){
83553 int flags;
83554 assert( pMem!=0 )((void) (0));
83555 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83556 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
83557 flags = pMem->flags;
83558 if( flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
83559 testcase( flags & MEM_IntReal );
83560 return pMem->u.i;
83561 }else if( flags & MEM_Real0x0008 ){
83562 return sqlite3RealToI64(pMem->u.r);
83563 }else if( (flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && pMem->z!=0 ){
83564 return memIntValue(pMem);
83565 }else{
83566 return 0;
83567 }
83568}
83569
83570/*
83571** Return the best representation of pMem that we can get into a
83572** double. If pMem is already a double or an integer, return its
83573** value. If it is a string or blob, try to convert it to a double.
83574** If it is a NULL, return 0.0.
83575*/
83576static SQLITE_NOINLINE__attribute__((noinline)) double memRealValue(Mem *pMem){
83577 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
83578 double val = (double)0;
83579 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
83580 return val;
83581}
83582SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem *pMem){
83583 assert( pMem!=0 )((void) (0));
83584 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83585 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
83586 if( pMem->flags & MEM_Real0x0008 ){
83587 return pMem->u.r;
83588 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
83589 testcase( pMem->flags & MEM_IntReal );
83590 return (double)pMem->u.i;
83591 }else if( pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
83592 return memRealValue(pMem);
83593 }else{
83594 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
83595 return (double)0;
83596 }
83597}
83598
83599/*
83600** Return 1 if pMem represents true, and return 0 if pMem represents false.
83601** Return the value ifNull if pMem is NULL.
83602*/
83603SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
83604 testcase( pMem->flags & MEM_IntReal );
83605 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ) return pMem->u.i!=0;
83606 if( pMem->flags & MEM_Null0x0001 ) return ifNull;
83607 return sqlite3VdbeRealValue(pMem)!=0.0;
83608}
83609
83610/*
83611** The MEM structure is already a MEM_Real or MEM_IntReal. Try to
83612** make it a MEM_Int if we can.
83613*/
83614SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem *pMem){
83615 assert( pMem!=0 )((void) (0));
83616 assert( pMem->flags & (MEM_Real|MEM_IntReal) )((void) (0));
83617 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83618 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83619 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
83620
83621 if( pMem->flags & MEM_IntReal0x0020 ){
83622 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
83623 }else{
83624 i64 ix = sqlite3RealToI64(pMem->u.r);
83625
83626 /* Only mark the value as an integer if
83627 **
83628 ** (1) the round-trip conversion real->int->real is a no-op, and
83629 ** (2) The integer is neither the largest nor the smallest
83630 ** possible integer (ticket #3922)
83631 **
83632 ** The second and third terms in the following conditional enforces
83633 ** the second condition under the assumption that addition overflow causes
83634 ** values to wrap around.
83635 */
83636 if( pMem->u.r==ix && ix>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) && ix<LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
83637 pMem->u.i = ix;
83638 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
83639 }
83640 }
83641}
83642
83643/*
83644** Convert pMem to type integer. Invalidate any prior representations.
83645*/
83646SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem *pMem){
83647 assert( pMem!=0 )((void) (0));
83648 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83649 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83650 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
83651
83652 pMem->u.i = sqlite3VdbeIntValue(pMem);
83653 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
83654 return SQLITE_OK0;
83655}
83656
83657/*
83658** Convert pMem so that it is of type MEM_Real.
83659** Invalidate any prior representations.
83660*/
83661SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem *pMem){
83662 assert( pMem!=0 )((void) (0));
83663 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83664 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
83665
83666 pMem->u.r = sqlite3VdbeRealValue(pMem);
83667 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0008
)
;
83668 return SQLITE_OK0;
83669}
83670
83671/* Compare a floating point value to an integer. Return true if the two
83672** values are the same within the precision of the floating point value.
83673**
83674** This function assumes that i was obtained by assignment from r1.
83675**
83676** For some versions of GCC on 32-bit machines, if you do the more obvious
83677** comparison of "r1==(double)i" you sometimes get an answer of false even
83678** though the r1 and (double)i values are bit-for-bit the same.
83679*/
83680SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
83681 double r2 = (double)i;
83682 return r1==0.0
83683 || (memcmp(&r1, &r2, sizeof(r1))==0
83684 && i >= -2251799813685248LL && i < 2251799813685248LL);
83685}
83686
83687/* Convert a floating point value to its closest integer. Do so in
83688** a way that avoids 'outside the range of representable values' warnings
83689** from UBSAN.
83690*/
83691SQLITE_PRIVATEstatic i64 sqlite3RealToI64(double r){
83692 if( r<-9223372036854774784.0 ) return SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
83693 if( r>+9223372036854774784.0 ) return LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
83694 return (i64)r;
83695}
83696
83697/*
83698** Convert pMem so that it has type MEM_Real or MEM_Int.
83699** Invalidate any prior representations.
83700**
83701** Every effort is made to force the conversion, even if the input
83702** is a string that does not look completely like a number. Convert
83703** as much of the string as we can and ignore the rest.
83704*/
83705SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem *pMem){
83706 assert( pMem!=0 )((void) (0));
83707 testcase( pMem->flags & MEM_Int );
83708 testcase( pMem->flags & MEM_Real );
83709 testcase( pMem->flags & MEM_IntReal );
83710 testcase( pMem->flags & MEM_Null );
83711 if( (pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001))==0 ){
83712 int rc;
83713 sqlite3_int64 ix;
83714 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 )((void) (0));
83715 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
83716 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
83717 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
83718 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
83719 ){
83720 pMem->u.i = ix;
83721 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0004
)
;
83722 }else{
83723 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0008
)
;
83724 }
83725 }
83726 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 )((void) (0));
83727 pMem->flags &= ~(MEM_Str0x0002|MEM_Blob0x0010|MEM_Zero0x0400);
83728 return SQLITE_OK0;
83729}
83730
83731/*
83732** Cast the datatype of the value in pMem according to the affinity
83733** "aff". Casting is different from applying affinity in that a cast
83734** is forced. In other words, the value is converted into the desired
83735** affinity even if that results in loss of data. This routine is
83736** used (for example) to implement the SQL "cast()" operator.
83737*/
83738SQLITE_PRIVATEstatic int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
83739 if( pMem->flags & MEM_Null0x0001 ) return SQLITE_OK0;
83740 switch( aff ){
83741 case SQLITE_AFF_BLOB0x41: { /* Really a cast to BLOB */
83742 if( (pMem->flags & MEM_Blob0x0010)==0 ){
83743 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT0x42, encoding);
83744 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
83745 if( pMem->flags & MEM_Str0x0002 ) MemSetTypeFlag(pMem, MEM_Blob)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0010
)
;
83746 }else{
83747 pMem->flags &= ~(MEM_TypeMask0x0dbf&~MEM_Blob0x0010);
83748 }
83749 break;
83750 }
83751 case SQLITE_AFF_NUMERIC0x43: {
83752 sqlite3VdbeMemNumerify(pMem);
83753 break;
83754 }
83755 case SQLITE_AFF_INTEGER0x44: {
83756 sqlite3VdbeMemIntegerify(pMem);
83757 break;
83758 }
83759 case SQLITE_AFF_REAL0x45: {
83760 sqlite3VdbeMemRealify(pMem);
83761 break;
83762 }
83763 default: {
83764 int rc;
83765 assert( aff==SQLITE_AFF_TEXT )((void) (0));
83766 assert( MEM_Str==(MEM_Blob>>3) )((void) (0));
83767 pMem->flags |= (pMem->flags&MEM_Blob0x0010)>>3;
83768 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT0x42, encoding);
83769 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
83770 pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Blob0x0010|MEM_Zero0x0400);
83771 if( encoding!=SQLITE_UTF81 ) pMem->n &= ~1;
83772 rc = sqlite3VdbeChangeEncoding(pMem, encoding);
83773 if( rc ) return rc;
83774 sqlite3VdbeMemZeroTerminateIfAble(pMem);
83775 }
83776 }
83777 return SQLITE_OK0;
83778}
83779
83780/*
83781** Initialize bulk memory to be a consistent Mem object.
83782**
83783** The minimum amount of initialization feasible is performed.
83784*/
83785SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
83786 assert( (flags & ~MEM_TypeMask)==0 )((void) (0));
83787 pMem->flags = flags;
83788 pMem->db = db;
83789 pMem->szMalloc = 0;
83790}
83791
83792
83793/*
83794** Delete any previous value and set the value stored in *pMem to NULL.
83795**
83796** This routine calls the Mem.xDel destructor to dispose of values that
83797** require the destructor. But it preserves the Mem.zMalloc memory allocation.
83798** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
83799** routine to invoke the destructor and deallocates Mem.zMalloc.
83800**
83801** Use this routine to reset the Mem prior to insert a new value.
83802**
83803** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
83804*/
83805SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem *pMem){
83806 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x8000|0x1000))!=0) ){
83807 vdbeMemClearExternAndSetNull(pMem);
83808 }else{
83809 pMem->flags = MEM_Null0x0001;
83810 }
83811}
83812SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value *p){
83813 sqlite3VdbeMemSetNull((Mem*)p);
83814}
83815
83816/*
83817** Delete any previous value and set the value to be a BLOB of length
83818** n containing all zeros.
83819*/
83820#ifndef SQLITE_OMIT_INCRBLOB
83821SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
83822 sqlite3VdbeMemRelease(pMem);
83823 pMem->flags = MEM_Blob0x0010|MEM_Zero0x0400;
83824 pMem->n = 0;
83825 if( n<0 ) n = 0;
83826 pMem->u.nZero = n;
83827 pMem->enc = SQLITE_UTF81;
83828 pMem->z = 0;
83829}
83830#else
83831SQLITE_PRIVATEstatic int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
83832 int nByte = n>0?n:1;
83833 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
83834 return SQLITE_NOMEM_BKPT7;
83835 }
83836 assert( pMem->z!=0 )((void) (0));
83837 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte )((void) (0));
83838 memset(pMem->z, 0, nByte);
83839 pMem->n = n>0?n:0;
83840 pMem->flags = MEM_Blob0x0010;
83841 pMem->enc = SQLITE_UTF81;
83842 return SQLITE_OK0;
83843}
83844#endif
83845
83846/*
83847** The pMem is known to contain content that needs to be destroyed prior
83848** to a value change. So invoke the destructor, then set the value to
83849** a 64-bit integer.
83850*/
83851static SQLITE_NOINLINE__attribute__((noinline)) void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
83852 sqlite3VdbeMemSetNull(pMem);
83853 pMem->u.i = val;
83854 pMem->flags = MEM_Int0x0004;
83855}
83856
83857/*
83858** Delete any previous value and set the value stored in *pMem to val,
83859** manifest type INTEGER.
83860*/
83861SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
83862 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x8000|0x1000))!=0) ){
83863 vdbeReleaseAndSetInt64(pMem, val);
83864 }else{
83865 pMem->u.i = val;
83866 pMem->flags = MEM_Int0x0004;
83867 }
83868}
83869
83870/*
83871** Set the iIdx'th entry of array aMem[] to contain integer value val.
83872*/
83873SQLITE_PRIVATEstatic void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val){
83874 sqlite3VdbeMemSetInt64(&aMem[iIdx], val);
83875}
83876
83877/* A no-op destructor */
83878SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p)(void)(p); }
83879
83880/*
83881** Set the value stored in *pMem should already be a NULL.
83882** Also store a pointer to go with it.
83883*/
83884SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(
83885 Mem *pMem,
83886 void *pPtr,
83887 const char *zPType,
83888 void (*xDestructor)(void*)
83889){
83890 assert( pMem->flags==MEM_Null )((void) (0));
83891 vdbeMemClear(pMem);
83892 pMem->u.zPType = zPType ? zPType : "";
83893 pMem->z = pPtr;
83894 pMem->flags = MEM_Null0x0001|MEM_Dyn0x1000|MEM_Subtype0x0800|MEM_Term0x0200;
83895 pMem->eSubtype = 'p';
83896 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
83897}
83898
83899#ifndef SQLITE_OMIT_FLOATING_POINT
83900/*
83901** Delete any previous value and set the value stored in *pMem to val,
83902** manifest type REAL.
83903*/
83904SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
83905 sqlite3VdbeMemSetNull(pMem);
83906 if( !sqlite3IsNaN(val) ){
83907 pMem->u.r = val;
83908 pMem->flags = MEM_Real0x0008;
83909 }
83910}
83911#endif
83912
83913#ifdef SQLITE_DEBUG
83914/*
83915** Return true if the Mem holds a RowSet object. This routine is intended
83916** for use inside of assert() statements.
83917*/
83918SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem *pMem){
83919 return (pMem->flags&(MEM_Blob0x0010|MEM_Dyn0x1000))==(MEM_Blob0x0010|MEM_Dyn0x1000)
83920 && pMem->xDel==sqlite3RowSetDelete;
83921}
83922#endif
83923
83924/*
83925** Delete any previous value and set the value of pMem to be an
83926** empty boolean index.
83927**
83928** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
83929** error occurs.
83930*/
83931SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem *pMem){
83932 sqlite3 *db = pMem->db;
83933 RowSet *p;
83934 assert( db!=0 )((void) (0));
83935 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
83936 sqlite3VdbeMemRelease(pMem);
83937 p = sqlite3RowSetInit(db);
83938 if( p==0 ) return SQLITE_NOMEM7;
83939 pMem->z = (char*)p;
83940 pMem->flags = MEM_Blob0x0010|MEM_Dyn0x1000;
83941 pMem->xDel = sqlite3RowSetDelete;
83942 return SQLITE_OK0;
83943}
83944
83945/*
83946** Return true if the Mem object contains a TEXT or BLOB that is
83947** too large - whose size exceeds SQLITE_MAX_LENGTH.
83948*/
83949SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem *p){
83950 assert( p->db!=0 )((void) (0));
83951 if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
83952 int n = p->n;
83953 if( p->flags & MEM_Zero0x0400 ){
83954 n += p->u.nZero;
83955 }
83956 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH0];
83957 }
83958 return 0;
83959}
83960
83961#ifdef SQLITE_DEBUG
83962/*
83963** This routine prepares a memory cell for modification by breaking
83964** its link to a shallow copy and by marking any current shallow
83965** copies of this cell as invalid.
83966**
83967** This is used for testing and debugging only - to help ensure that shallow
83968** copies (created by OP_SCopy) are not misused.
83969*/
83970SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
83971 int i;
83972 Mem *pX;
83973 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
83974 if( pX->pScopyFrom==pMem ){
83975 u16 mFlags;
83976 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
83977 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",
83978 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
83979 }
83980 /* If pX is marked as a shallow copy of pMem, then try to verify that
83981 ** no significant changes have been made to pX since the OP_SCopy.
83982 ** A significant change would indicated a missed call to this
83983 ** function for pX. Minor changes, such as adding or removing a
83984 ** dual type, are allowed, as long as the underlying value is the
83985 ** same. */
83986 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
83987 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i )((void) (0));
83988
83989 /* pMem is the register that is changing. But also mark pX as
83990 ** undefined so that we can quickly detect the shallow-copy error */
83991 pX->flags = MEM_Undefined0x0000;
83992 pX->pScopyFrom = 0;
83993 }
83994 }
83995 pMem->pScopyFrom = 0;
83996}
83997#endif /* SQLITE_DEBUG */
83998
83999/*
84000** Make an shallow copy of pFrom into pTo. Prior contents of
84001** pTo are freed. The pFrom->z field is not duplicated. If
84002** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
84003** and flags gets srcType (either MEM_Ephem or MEM_Static).
84004*/
84005static SQLITE_NOINLINE__attribute__((noinline)) void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
84006 vdbeMemClearExternAndSetNull(pTo);
84007 assert( !VdbeMemDynamic(pTo) )((void) (0));
84008 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
84009}
84010SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
84011 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
84012 assert( pTo->db==pFrom->db )((void) (0));
84013 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x8000|0x1000))!=0) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
84014 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, db));
84015 if( (pFrom->flags&MEM_Static0x2000)==0 ){
84016 pTo->flags &= ~(MEM_Dyn0x1000|MEM_Static0x2000|MEM_Ephem0x4000);
84017 assert( srcType==MEM_Ephem || srcType==MEM_Static )((void) (0));
84018 pTo->flags |= srcType;
84019 }
84020}
84021
84022/*
84023** Make a full copy of pFrom into pTo. Prior contents of pTo are
84024** freed before the copy is made.
84025*/
84026SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
84027 int rc = SQLITE_OK0;
84028
84029 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
84030 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x8000|0x1000))!=0) ) vdbeMemClearExternAndSetNull(pTo);
84031 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, db));
84032 pTo->flags &= ~MEM_Dyn0x1000;
84033 if( pTo->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
84034 if( 0==(pFrom->flags&MEM_Static0x2000) ){
84035 pTo->flags |= MEM_Ephem0x4000;
84036 rc = sqlite3VdbeMemMakeWriteable(pTo);
84037 }
84038 }
84039
84040 return rc;
84041}
84042
84043/*
84044** Transfer the contents of pFrom to pTo. Any existing value in pTo is
84045** freed. If pFrom contains ephemeral data, a copy is made.
84046**
84047** pFrom contains an SQL NULL when this routine returns.
84048*/
84049SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
84050 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) )((void) (0));
84051 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) )((void) (0));
84052 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db )((void) (0));
84053
84054 sqlite3VdbeMemRelease(pTo);
84055 memcpy(pTo, pFrom, sizeof(Mem));
84056 pFrom->flags = MEM_Null0x0001;
84057 pFrom->szMalloc = 0;
84058}
84059
84060/*
84061** Change the value of a Mem to be a string or a BLOB.
84062**
84063** The memory management strategy depends on the value of the xDel
84064** parameter. If the value passed is SQLITE_TRANSIENT, then the
84065** string is copied into a (possibly existing) buffer managed by the
84066** Mem structure. Otherwise, any existing buffer is freed and the
84067** pointer copied.
84068**
84069** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
84070** size limit) then no memory allocation occurs. If the string can be
84071** stored without allocating memory, then it is. If a memory allocation
84072** is required to store the string, then value of pMem is unchanged. In
84073** either case, SQLITE_TOOBIG is returned.
84074**
84075** The "enc" parameter is the text encoding for the string, or zero
84076** to store a blob.
84077**
84078** If n is negative, then the string consists of all bytes up to but
84079** excluding the first zero character. The n parameter must be
84080** non-negative for blobs.
84081*/
84082SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(
84083 Mem *pMem, /* Memory cell to set to string value */
84084 const char *z, /* String pointer */
84085 i64 n, /* Bytes in string, or negative */
84086 u8 enc, /* Encoding of z. 0 for BLOBs */
84087 void (*xDel)(void*) /* Destructor function */
84088){
84089 i64 nByte = n; /* New value for pMem->n */
84090 int iLimit; /* Maximum allowed string or blob size */
84091 u16 flags; /* New value for pMem->flags */
84092
84093 assert( pMem!=0 )((void) (0));
84094 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
84095 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84096 assert( enc!=0 || n>=0 )((void) (0));
84097
84098 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
84099 if( !z ){
84100 sqlite3VdbeMemSetNull(pMem);
84101 return SQLITE_OK0;
84102 }
84103
84104 if( pMem->db ){
84105 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH0];
84106 }else{
84107 iLimit = SQLITE_MAX_LENGTH2147483645;
84108 }
84109 if( nByte<0 ){
84110 assert( enc!=0 )((void) (0));
84111 if( enc==SQLITE_UTF81 ){
84112 nByte = strlen(z);
84113 }else{
84114 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
84115 }
84116 flags= MEM_Str0x0002|MEM_Term0x0200;
84117 }else if( enc==0 ){
84118 flags = MEM_Blob0x0010;
84119 enc = SQLITE_UTF81;
84120 }else{
84121 flags = MEM_Str0x0002;
84122 }
84123 if( nByte>iLimit ){
84124 if( xDel && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
84125 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ){
84126 sqlite3DbFree(pMem->db, (void*)z);
84127 }else{
84128 xDel((void*)z);
84129 }
84130 }
84131 sqlite3VdbeMemSetNull(pMem);
84132 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG18);
84133 }
84134
84135 /* The following block sets the new values of Mem.z and Mem.xDel. It
84136 ** also sets a flag in local variable "flags" to indicate the memory
84137 ** management (one of MEM_Dyn or MEM_Static).
84138 */
84139 if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
84140 i64 nAlloc = nByte;
84141 if( flags&MEM_Term0x0200 ){
84142 nAlloc += (enc==SQLITE_UTF81?1:2);
84143 }
84144 testcase( nAlloc==0 );
84145 testcase( nAlloc==31 );
84146 testcase( nAlloc==32 );
84147 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)((nAlloc)>(32)?(nAlloc):(32))) ){
84148 return SQLITE_NOMEM_BKPT7;
84149 }
84150 memcpy(pMem->z, z, nAlloc);
84151 }else{
84152 sqlite3VdbeMemRelease(pMem);
84153 pMem->z = (char *)z;
84154 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear) ){
84155 pMem->zMalloc = pMem->z;
84156 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
84157 }else{
84158 pMem->xDel = xDel;
84159 flags |= ((xDel==SQLITE_STATIC((sqlite3_destructor_type)0))?MEM_Static0x2000:MEM_Dyn0x1000);
84160 }
84161 }
84162
84163 pMem->n = (int)(nByte & 0x7fffffff);
84164 pMem->flags = flags;
84165 pMem->enc = enc;
84166
84167#ifndef SQLITE_OMIT_UTF16
84168 if( enc>SQLITE_UTF81 && sqlite3VdbeMemHandleBom(pMem) ){
84169 return SQLITE_NOMEM_BKPT7;
84170 }
84171#endif
84172
84173
84174 return SQLITE_OK0;
84175}
84176
84177/*
84178** Move data out of a btree key or data field and into a Mem structure.
84179** The data is payload from the entry that pCur is currently pointing
84180** to. offset and amt determine what portion of the data or key to retrieve.
84181** The result is written into the pMem element.
84182**
84183** The pMem object must have been initialized. This routine will use
84184** pMem->zMalloc to hold the content from the btree, if possible. New
84185** pMem->zMalloc space will be allocated if necessary. The calling routine
84186** is responsible for making sure that the pMem object is eventually
84187** destroyed.
84188**
84189** If this routine fails for any reason (malloc returns NULL or unable
84190** to read from the disk) then the pMem is left in an inconsistent state.
84191*/
84192SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(
84193 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
84194 u32 offset, /* Offset from the start of data to return bytes from. */
84195 u32 amt, /* Number of bytes to return. */
84196 Mem *pMem /* OUT: Return data in this Mem structure. */
84197){
84198 int rc;
84199 pMem->flags = MEM_Null0x0001;
84200 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
84201 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(84201);
84202 }
84203 if( SQLITE_OK0==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
84204 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
84205 if( rc==SQLITE_OK0 ){
84206 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
84207 pMem->flags = MEM_Blob0x0010;
84208 pMem->n = (int)amt;
84209 }else{
84210 sqlite3VdbeMemRelease(pMem);
84211 }
84212 }
84213 return rc;
84214}
84215SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtreeZeroOffset(
84216 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
84217 u32 amt, /* Number of bytes to return. */
84218 Mem *pMem /* OUT: Return data in this Mem structure. */
84219){
84220 u32 available = 0; /* Number of bytes available on the local btree page */
84221 int rc = SQLITE_OK0; /* Return code */
84222
84223 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
84224 assert( !VdbeMemDynamic(pMem) )((void) (0));
84225
84226 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
84227 ** that both the BtShared and database handle mutexes are held. */
84228 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
84229 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
84230 assert( pMem->z!=0 )((void) (0));
84231
84232 if( amt<=available ){
84233 pMem->flags = MEM_Blob0x0010|MEM_Ephem0x4000;
84234 pMem->n = (int)amt;
84235 }else{
84236 rc = sqlite3VdbeMemFromBtree(pCur, 0, amt, pMem);
84237 }
84238
84239 return rc;
84240}
84241
84242/*
84243** The pVal argument is known to be a value other than NULL.
84244** Convert it into a string with encoding enc and return a pointer
84245** to a zero-terminated version of that string.
84246*/
84247static SQLITE_NOINLINE__attribute__((noinline)) const void *valueToText(sqlite3_value* pVal, u8 enc){
84248 assert( pVal!=0 )((void) (0));
84249 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
84250 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
84251 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
84252 assert( (pVal->flags & (MEM_Null))==0 )((void) (0));
84253 if( pVal->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
84254 if( ExpandBlob(pVal)(((pVal)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pVal)
:0)
) return 0;
84255 pVal->flags |= MEM_Str0x0002;
84256 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED8) ){
84257 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED8);
84258 }
84259 if( (enc & SQLITE_UTF16_ALIGNED8)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)((int)(long int)(pVal->z))) ){
84260 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 )((void) (0));
84261 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK0 ){
84262 return 0;
84263 }
84264 }
84265 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
84266 }else{
84267 sqlite3VdbeMemStringify(pVal, enc, 0);
84268 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) )((void) (0));
84269 }
84270 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0((void) (0))
84271 || pVal->db->mallocFailed )((void) (0));
84272 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED8) ){
84273 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
84274 return pVal->z;
84275 }else{
84276 return 0;
84277 }
84278}
84279
84280/* This function is only available internally, it is not part of the
84281** external API. It works in a similar way to sqlite3_value_text(),
84282** except the data returned is in the encoding specified by the second
84283** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
84284** SQLITE_UTF8.
84285**
84286** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
84287** If that is the case, then the result must be aligned on an even byte
84288** boundary.
84289*/
84290SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
84291 if( !pVal ) return 0;
84292 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
84293 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
84294 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
84295 if( (pVal->flags&(MEM_Str0x0002|MEM_Term0x0200))==(MEM_Str0x0002|MEM_Term0x0200) && pVal->enc==enc ){
84296 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
84297 return pVal->z;
84298 }
84299 if( pVal->flags&MEM_Null0x0001 ){
84300 return 0;
84301 }
84302 return valueToText(pVal, enc);
84303}
84304
84305/* Return true if sqlit3_value object pVal is a string or blob value
84306** that uses the destructor specified in the second argument.
84307**
84308** TODO: Maybe someday promote this interface into a published API so
84309** that third-party extensions can get access to it?
84310*/
84311SQLITE_PRIVATEstatic int sqlite3ValueIsOfClass(const sqlite3_value *pVal, void(*xFree)(void*)){
84312 if( ALWAYS(pVal!=0)(pVal!=0)
84313 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)((pVal->flags & (0x0002|0x0010))!=0)
84314 && (pVal->flags & MEM_Dyn0x1000)!=0
84315 && pVal->xDel==xFree
84316 ){
84317 return 1;
84318 }else{
84319 return 0;
84320 }
84321}
84322
84323/*
84324** Create a new sqlite3_value object.
84325*/
84326SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *db){
84327 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
84328 if( p ){
84329 p->flags = MEM_Null0x0001;
84330 p->db = db;
84331 }
84332 return p;
84333}
84334
84335/*
84336** Context object passed by sqlite3Stat4ProbeSetValue() through to
84337** valueNew(). See comments above valueNew() for details.
84338*/
84339struct ValueNewStat4Ctx {
84340 Parse *pParse;
84341 Index *pIdx;
84342 UnpackedRecord **ppRec;
84343 int iVal;
84344};
84345
84346/*
84347** Allocate and return a pointer to a new sqlite3_value object. If
84348** the second argument to this function is NULL, the object is allocated
84349** by calling sqlite3ValueNew().
84350**
84351** Otherwise, if the second argument is non-zero, then this function is
84352** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
84353** already been allocated, allocate the UnpackedRecord structure that
84354** that function will return to its caller here. Then return a pointer to
84355** an sqlite3_value within the UnpackedRecord.a[] array.
84356*/
84357static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
84358#ifdef SQLITE_ENABLE_STAT4
84359 if( p ){
84360 UnpackedRecord *pRec = p->ppRec[0];
84361
84362 if( pRec==0 ){
84363 Index *pIdx = p->pIdx; /* Index being probed */
84364 int nByte; /* Bytes of space to allocate */
84365 int i; /* Counter variable */
84366 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
84367
84368 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7);
84369 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
84370 if( pRec ){
84371 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
84372 if( pRec->pKeyInfo ){
84373 assert( pRec->pKeyInfo->nAllField==nCol )((void) (0));
84374 assert( pRec->pKeyInfo->enc==ENC(db) )((void) (0));
84375 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7));
84376 for(i=0; i<nCol; i++){
84377 pRec->aMem[i].flags = MEM_Null0x0001;
84378 pRec->aMem[i].db = db;
84379 }
84380 }else{
84381 sqlite3DbFreeNN(db, pRec);
84382 pRec = 0;
84383 }
84384 }
84385 if( pRec==0 ) return 0;
84386 p->ppRec[0] = pRec;
84387 }
84388
84389 pRec->nField = p->iVal+1;
84390 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
84391 return &pRec->aMem[p->iVal];
84392 }
84393#else
84394 UNUSED_PARAMETER(p)(void)(p);
84395#endif /* defined(SQLITE_ENABLE_STAT4) */
84396 return sqlite3ValueNew(db);
84397}
84398
84399/*
84400** The expression object indicated by the second argument is guaranteed
84401** to be a scalar SQL function. If
84402**
84403** * all function arguments are SQL literals,
84404** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
84405** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
84406**
84407** then this routine attempts to invoke the SQL function. Assuming no
84408** error occurs, output parameter (*ppVal) is set to point to a value
84409** object containing the result before returning SQLITE_OK.
84410**
84411** Affinity aff is applied to the result of the function before returning.
84412** If the result is a text value, the sqlite3_value object uses encoding
84413** enc.
84414**
84415** If the conditions above are not met, this function returns SQLITE_OK
84416** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
84417** NULL and an SQLite error code returned.
84418*/
84419#ifdef SQLITE_ENABLE_STAT4
84420static int valueFromFunction(0
84421 sqlite3 *db, /* The database connection */0
84422 const Expr *p, /* The expression to evaluate */0
84423 u8 enc, /* Encoding to use */0
84424 u8 aff, /* Affinity to use */0
84425 sqlite3_value **ppVal, /* Write the new value here */0
84426 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */0
84427)0{
84428 sqlite3_context ctx; /* Context object for function invocation */
84429 sqlite3_value **apVal = 0; /* Function arguments */
84430 int nVal = 0; /* Size of apVal[] array */
84431 FuncDef *pFunc = 0; /* Function definition */
84432 sqlite3_value *pVal = 0; /* New value */
84433 int rc = SQLITE_OK0; /* Return code */
84434 ExprList *pList = 0; /* Function arguments */
84435 int i; /* Iterator variable */
84436
84437 assert( pCtx!=0 )((void) (0));
84438 assert( (p->flags & EP_TokenOnly)==0 )((void) (0));
84439 assert( ExprUseXList(p) )((void) (0));
84440 pList = p->x.pList;
84441 if( pList ) nVal = pList->nExpr;
84442 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
84443 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
84444#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
84445 if( pFunc==0 ) return SQLITE_OK0;
84446#endif
84447 assert( pFunc )((void) (0));
84448 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000))==0
84449 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL0x0020|SQLITE_FUNC_RUNONLY0x8000))!=0
84450 ){
84451 return SQLITE_OK0;
84452 }
84453
84454 if( pList ){
84455 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
84456 if( apVal==0 ){
84457 rc = SQLITE_NOMEM_BKPT7;
84458 goto value_from_function_out;
84459 }
84460 for(i=0; i<nVal; i++){
84461 rc = sqlite3Stat4ValueFromExpr(pCtx->pParse, pList->a[i].pExpr, aff,
84462 &apVal[i]);
84463 if( apVal[i]==0 || rc!=SQLITE_OK0 ) goto value_from_function_out;
84464 }
84465 }
84466
84467 pVal = valueNew(db, pCtx);
84468 if( pVal==0 ){
84469 rc = SQLITE_NOMEM_BKPT7;
84470 goto value_from_function_out;
84471 }
84472
84473 memset(&ctx, 0, sizeof(ctx));
84474 ctx.pOut = pVal;
84475 ctx.pFunc = pFunc;
84476 ctx.enc = ENC(db)((db)->enc);
84477 pFunc->xSFunc(&ctx, nVal, apVal);
84478 if( ctx.isError ){
84479 rc = ctx.isError;
84480 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
84481 }else{
84482 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF81);
84483 assert( rc==SQLITE_OK )((void) (0));
84484 rc = sqlite3VdbeChangeEncoding(pVal, enc);
84485 if( NEVER(rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal))(rc==0 && sqlite3VdbeMemTooBig(pVal)) ){
84486 rc = SQLITE_TOOBIG18;
84487 pCtx->pParse->nErr++;
84488 }
84489 }
84490
84491 value_from_function_out:
84492 if( rc!=SQLITE_OK0 ){
84493 pVal = 0;
84494 pCtx->pParse->rc = rc;
84495 }
84496 if( apVal ){
84497 for(i=0; i<nVal; i++){
84498 sqlite3ValueFree(apVal[i]);
84499 }
84500 sqlite3DbFreeNN(db, apVal);
84501 }
84502
84503 *ppVal = pVal;
84504 return rc;
84505}
84506#else
84507# define valueFromFunction(a,b,c,d,e,f)0 SQLITE_OK0
84508#endif /* defined(SQLITE_ENABLE_STAT4) */
84509
84510/*
84511** Extract a value from the supplied expression in the manner described
84512** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
84513** using valueNew().
84514**
84515** If pCtx is NULL and an error occurs after the sqlite3_value object
84516** has been allocated, it is freed before returning. Or, if pCtx is not
84517** NULL, it is assumed that the caller will free any allocated object
84518** in all cases.
84519*/
84520static int valueFromExpr(
84521 sqlite3 *db, /* The database connection */
84522 const Expr *pExpr, /* The expression to evaluate */
84523 u8 enc, /* Encoding to use */
84524 u8 affinity, /* Affinity to use */
84525 sqlite3_value **ppVal, /* Write the new value here */
84526 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
84527){
84528 int op;
84529 char *zVal = 0;
84530 sqlite3_value *pVal = 0;
84531 int negInt = 1;
84532 const char *zNeg = "";
84533 int rc = SQLITE_OK0;
84534
84535 assert( pExpr!=0 )((void) (0));
84536 while( (op = pExpr->op)==TK_UPLUS173 || op==TK_SPAN181 ) pExpr = pExpr->pLeft;
84537 if( op==TK_REGISTER176 ) op = pExpr->op2;
84538
84539 /* Compressed expressions only appear when parsing the DEFAULT clause
84540 ** on a table column definition, and hence only when pCtx==0. This
84541 ** check ensures that an EP_TokenOnly expression is never passed down
84542 ** into valueFromFunction(). */
84543 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 )((void) (0));
84544
84545 if( op==TK_CAST36 ){
84546 u8 aff;
84547 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
84548 aff = sqlite3AffinityType(pExpr->u.zToken,0);
84549 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
84550 testcase( rc!=SQLITE_OK );
84551 if( *ppVal ){
84552#ifdef SQLITE_ENABLE_STAT4
84553 rc = ExpandBlob(*ppVal)(((*ppVal)->flags&0x0400)?sqlite3VdbeMemExpandBlob(*ppVal
):0)
;
84554#else
84555 /* zero-blobs only come from functions, not literal values. And
84556 ** functions are only processed under STAT4 */
84557 assert( (ppVal[0][0].flags & MEM_Zero)==0 )((void) (0));
84558#endif
84559 sqlite3VdbeMemCast(*ppVal, aff, enc);
84560 sqlite3ValueApplyAffinity(*ppVal, affinity, enc);
84561 }
84562 return rc;
84563 }
84564
84565 /* Handle negative integers in a single step. This is needed in the
84566 ** case when the value is -9223372036854775808. Except - do not do this
84567 ** for hexadecimal literals. */
84568 if( op==TK_UMINUS174 ){
84569 Expr *pLeft = pExpr->pLeft;
84570 if( (pLeft->op==TK_INTEGER156 || pLeft->op==TK_FLOAT154) ){
84571 if( ExprHasProperty(pLeft, EP_IntValue)(((pLeft)->flags&(0x000800))!=0)
84572 || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
84573 ){
84574 pExpr = pLeft;
84575 op = pExpr->op;
84576 negInt = -1;
84577 zNeg = "-";
84578 }
84579 }
84580 }
84581
84582 if( op==TK_STRING118 || op==TK_FLOAT154 || op==TK_INTEGER156 ){
84583 pVal = valueNew(db, pCtx);
84584 if( pVal==0 ) goto no_mem;
84585 if( ExprHasProperty(pExpr, EP_IntValue)(((pExpr)->flags&(0x000800))!=0) ){
84586 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
84587 }else{
84588 i64 iVal;
84589 if( op==TK_INTEGER156 && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
84590 sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
84591 }else{
84592 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
84593 if( zVal==0 ) goto no_mem;
84594 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
84595 }
84596 }
84597 if( affinity==SQLITE_AFF_BLOB0x41 ){
84598 if( op==TK_FLOAT154 ){
84599 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) )((void) (0));
84600 sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF81);
84601 pVal->flags = MEM_Real0x0008;
84602 }else if( op==TK_INTEGER156 ){
84603 /* This case is required by -9223372036854775808 and other strings
84604 ** that look like integers but cannot be handled by the
84605 ** sqlite3DecOrHexToI64() call above. */
84606 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC0x43, SQLITE_UTF81);
84607 }
84608 }else{
84609 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF81);
84610 }
84611 assert( (pVal->flags & MEM_IntReal)==0 )((void) (0));
84612 if( pVal->flags & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008) ){
84613 testcase( pVal->flags & MEM_Int );
84614 testcase( pVal->flags & MEM_Real );
84615 pVal->flags &= ~MEM_Str0x0002;
84616 }
84617 if( enc!=SQLITE_UTF81 ){
84618 rc = sqlite3VdbeChangeEncoding(pVal, enc);
84619 }
84620 }else if( op==TK_UMINUS174 ) {
84621 /* This branch happens for multiple negative signs. Ex: -(-5) */
84622 if( SQLITE_OK0==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
84623 && pVal!=0
84624 ){
84625 sqlite3VdbeMemNumerify(pVal);
84626 if( pVal->flags & MEM_Real0x0008 ){
84627 pVal->u.r = -pVal->u.r;
84628 }else if( pVal->u.i==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
84629#ifndef SQLITE_OMIT_FLOATING_POINT
84630 pVal->u.r = -(double)SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
84631#else
84632 pVal->u.r = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
84633#endif
84634 MemSetTypeFlag(pVal, MEM_Real)((pVal)->flags = ((pVal)->flags&~(0x0dbf|0x0400))|0x0008
)
;
84635 }else{
84636 pVal->u.i = -pVal->u.i;
84637 }
84638 sqlite3ValueApplyAffinity(pVal, affinity, enc);
84639 }
84640 }else if( op==TK_NULL122 ){
84641 pVal = valueNew(db, pCtx);
84642 if( pVal==0 ) goto no_mem;
84643 sqlite3VdbeMemSetNull(pVal);
84644 }
84645#ifndef SQLITE_OMIT_BLOB_LITERAL
84646 else if( op==TK_BLOB155 ){
84647 int nVal;
84648 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
84649 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
84650 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
84651 pVal = valueNew(db, pCtx);
84652 if( !pVal ) goto no_mem;
84653 zVal = &pExpr->u.zToken[2];
84654 nVal = sqlite3Strlen30(zVal)-1;
84655 assert( zVal[nVal]=='\'' )((void) (0));
84656 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
84657 0, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
84658 }
84659#endif
84660#ifdef SQLITE_ENABLE_STAT4
84661 else if( op==TK_FUNCTION172 && pCtx!=0 ){
84662 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx)0;
84663 }
84664#endif
84665 else if( op==TK_TRUEFALSE171 ){
84666 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
84667 pVal = valueNew(db, pCtx);
84668 if( pVal ){
84669 pVal->flags = MEM_Int0x0004;
84670 pVal->u.i = pExpr->u.zToken[4]==0;
84671 sqlite3ValueApplyAffinity(pVal, affinity, enc);
84672 }
84673 }
84674
84675 *ppVal = pVal;
84676 return rc;
84677
84678no_mem:
84679#ifdef SQLITE_ENABLE_STAT4
84680 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0)(pCtx->pParse->nErr==0) )
84681#endif
84682 sqlite3OomFault(db);
84683 sqlite3DbFree(db, zVal);
84684 assert( *ppVal==0 )((void) (0));
84685#ifdef SQLITE_ENABLE_STAT4
84686 if( pCtx==0 ) sqlite3ValueFree(pVal);
84687#else
84688 assert( pCtx==0 )((void) (0)); sqlite3ValueFree(pVal);
84689#endif
84690 return SQLITE_NOMEM_BKPT7;
84691}
84692
84693/*
84694** Create a new sqlite3_value object, containing the value of pExpr.
84695**
84696** This only works for very simple expressions that consist of one constant
84697** token (i.e. "5", "5.1", "'a string'"). If the expression can
84698** be converted directly into a value, then the value is allocated and
84699** a pointer written to *ppVal. The caller is responsible for deallocating
84700** the value by passing it to sqlite3ValueFree() later on. If the expression
84701** cannot be converted to a value, then *ppVal is set to NULL.
84702*/
84703SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(
84704 sqlite3 *db, /* The database connection */
84705 const Expr *pExpr, /* The expression to evaluate */
84706 u8 enc, /* Encoding to use */
84707 u8 affinity, /* Affinity to use */
84708 sqlite3_value **ppVal /* Write the new value here */
84709){
84710 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
84711}
84712
84713#ifdef SQLITE_ENABLE_STAT4
84714/*
84715** Attempt to extract a value from pExpr and use it to construct *ppVal.
84716**
84717** If pAlloc is not NULL, then an UnpackedRecord object is created for
84718** pAlloc if one does not exist and the new value is added to the
84719** UnpackedRecord object.
84720**
84721** A value is extracted in the following cases:
84722**
84723** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
84724**
84725** * The expression is a bound variable, and this is a reprepare, or
84726**
84727** * The expression is a literal value.
84728**
84729** On success, *ppVal is made to point to the extracted value. The caller
84730** is responsible for ensuring that the value is eventually freed.
84731*/
84732static int stat4ValueFromExpr(
84733 Parse *pParse, /* Parse context */
84734 Expr *pExpr, /* The expression to extract a value from */
84735 u8 affinity, /* Affinity to use */
84736 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
84737 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
84738){
84739 int rc = SQLITE_OK0;
84740 sqlite3_value *pVal = 0;
84741 sqlite3 *db = pParse->db;
84742
84743 /* Skip over any TK_COLLATE nodes */
84744 pExpr = sqlite3ExprSkipCollate(pExpr);
84745
84746 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE )((void) (0));
84747 if( !pExpr ){
84748 pVal = valueNew(db, pAlloc);
84749 if( pVal ){
84750 sqlite3VdbeMemSetNull((Mem*)pVal);
84751 }
84752 }else if( pExpr->op==TK_VARIABLE157 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
84753 Vdbe *v;
84754 int iBindVar = pExpr->iColumn;
84755 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
84756 if( (v = pParse->pReprepare)!=0 ){
84757 pVal = valueNew(db, pAlloc);
84758 if( pVal ){
84759 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
84760 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)((db)->enc));
84761 pVal->db = pParse->db;
84762 }
84763 }
84764 }else{
84765 rc = valueFromExpr(db, pExpr, ENC(db)((db)->enc), affinity, &pVal, pAlloc);
84766 }
84767
84768 assert( pVal==0 || pVal->db==db )((void) (0));
84769 *ppVal = pVal;
84770 return rc;
84771}
84772
84773/*
84774** This function is used to allocate and populate UnpackedRecord
84775** structures intended to be compared against sample index keys stored
84776** in the sqlite_stat4 table.
84777**
84778** A single call to this function populates zero or more fields of the
84779** record starting with field iVal (fields are numbered from left to
84780** right starting with 0). A single field is populated if:
84781**
84782** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
84783**
84784** * The expression is a bound variable, and this is a reprepare, or
84785**
84786** * The sqlite3ValueFromExpr() function is able to extract a value
84787** from the expression (i.e. the expression is a literal value).
84788**
84789** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
84790** vector components that match either of the two latter criteria listed
84791** above.
84792**
84793** Before any value is appended to the record, the affinity of the
84794** corresponding column within index pIdx is applied to it. Before
84795** this function returns, output parameter *pnExtract is set to the
84796** number of values appended to the record.
84797**
84798** When this function is called, *ppRec must either point to an object
84799** allocated by an earlier call to this function, or must be NULL. If it
84800** is NULL and a value can be successfully extracted, a new UnpackedRecord
84801** is allocated (and *ppRec set to point to it) before returning.
84802**
84803** Unless an error is encountered, SQLITE_OK is returned. It is not an
84804** error if a value cannot be extracted from pExpr. If an error does
84805** occur, an SQLite error code is returned.
84806*/
84807SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
84808 Parse *pParse, /* Parse context */
84809 Index *pIdx, /* Index being probed */
84810 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
84811 Expr *pExpr, /* The expression to extract a value from */
84812 int nElem, /* Maximum number of values to append */
84813 int iVal, /* Array element to populate */
84814 int *pnExtract /* OUT: Values appended to the record */
84815){
84816 int rc = SQLITE_OK0;
84817 int nExtract = 0;
84818
84819 if( pExpr==0 || pExpr->op!=TK_SELECT139 ){
84820 int i;
84821 struct ValueNewStat4Ctx alloc;
84822
84823 alloc.pParse = pParse;
84824 alloc.pIdx = pIdx;
84825 alloc.ppRec = ppRec;
84826
84827 for(i=0; i<nElem; i++){
84828 sqlite3_value *pVal = 0;
84829 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
84830 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
84831 alloc.iVal = iVal+i;
84832 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
84833 if( !pVal ) break;
84834 nExtract++;
84835 }
84836 }
84837
84838 *pnExtract = nExtract;
84839 return rc;
84840}
84841
84842/*
84843** Attempt to extract a value from expression pExpr using the methods
84844** as described for sqlite3Stat4ProbeSetValue() above.
84845**
84846** If successful, set *ppVal to point to a new value object and return
84847** SQLITE_OK. If no value can be extracted, but no other error occurs
84848** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
84849** does occur, return an SQLite error code. The final value of *ppVal
84850** is undefined in this case.
84851*/
84852SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(
84853 Parse *pParse, /* Parse context */
84854 Expr *pExpr, /* The expression to extract a value from */
84855 u8 affinity, /* Affinity to use */
84856 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
84857){
84858 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
84859}
84860
84861/*
84862** Extract the iCol-th column from the nRec-byte record in pRec. Write
84863** the column value into *ppVal. If *ppVal is initially NULL then a new
84864** sqlite3_value object is allocated.
84865**
84866** If *ppVal is initially NULL then the caller is responsible for
84867** ensuring that the value written into *ppVal is eventually freed.
84868*/
84869SQLITE_PRIVATEstatic int sqlite3Stat4Column(
84870 sqlite3 *db, /* Database handle */
84871 const void *pRec, /* Pointer to buffer containing record */
84872 int nRec, /* Size of buffer pRec in bytes */
84873 int iCol, /* Column to extract */
84874 sqlite3_value **ppVal /* OUT: Extracted value */
84875){
84876 u32 t = 0; /* a column type code */
84877 u32 nHdr; /* Size of the header in the record */
84878 u32 iHdr; /* Next unread header byte */
84879 i64 iField; /* Next unread data byte */
84880 u32 szField = 0; /* Size of the current data field */
84881 int i; /* Column index */
84882 u8 *a = (u8*)pRec; /* Typecast byte array */
84883 Mem *pMem = *ppVal; /* Write result into this Mem object */
84884
84885 assert( iCol>0 )((void) (0));
84886 iHdr = getVarint32(a, nHdr)(u8)((*(a)<(u8)0x80)?((nHdr)=(u32)*(a)),1:sqlite3GetVarint32
((a),(u32 *)&(nHdr)))
;
84887 if( nHdr>(u32)nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(84887);
84888 iField = nHdr;
84889 for(i=0; i<=iCol; i++){
84890 iHdr += getVarint32(&a[iHdr], t)(u8)((*(&a[iHdr])<(u8)0x80)?((t)=(u32)*(&a[iHdr]))
,1:sqlite3GetVarint32((&a[iHdr]),(u32 *)&(t)))
;
84891 testcase( iHdr==nHdr );
84892 testcase( iHdr==nHdr+1 );
84893 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(84893);
84894 szField = sqlite3VdbeSerialTypeLen(t);
84895 iField += szField;
84896 }
84897 testcase( iField==nRec );
84898 testcase( iField==nRec+1 );
84899 if( iField>nRec ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(84899);
84900 if( pMem==0 ){
84901 pMem = *ppVal = sqlite3ValueNew(db);
84902 if( pMem==0 ) return SQLITE_NOMEM_BKPT7;
84903 }
84904 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
84905 pMem->enc = ENC(db)((db)->enc);
84906 return SQLITE_OK0;
84907}
84908
84909/*
84910** Unless it is NULL, the argument must be an UnpackedRecord object returned
84911** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
84912** the object.
84913*/
84914SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
84915 if( pRec ){
84916 int i;
84917 int nCol = pRec->pKeyInfo->nAllField;
84918 Mem *aMem = pRec->aMem;
84919 sqlite3 *db = aMem[0].db;
84920 for(i=0; i<nCol; i++){
84921 sqlite3VdbeMemRelease(&aMem[i]);
84922 }
84923 sqlite3KeyInfoUnref(pRec->pKeyInfo);
84924 sqlite3DbFreeNN(db, pRec);
84925 }
84926}
84927#endif /* ifdef SQLITE_ENABLE_STAT4 */
84928
84929/*
84930** Change the string value of an sqlite3_value object
84931*/
84932SQLITE_PRIVATEstatic void sqlite3ValueSetStr(
84933 sqlite3_value *v, /* Value to be set */
84934 int n, /* Length of string z */
84935 const void *z, /* Text of the new string */
84936 u8 enc, /* Encoding to use */
84937 void (*xDel)(void*) /* Destructor for the string */
84938){
84939 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
84940}
84941
84942/*
84943** Free an sqlite3_value object
84944*/
84945SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value *v){
84946 if( !v ) return;
84947 sqlite3VdbeMemRelease((Mem *)v);
84948 sqlite3DbFreeNN(((Mem*)v)->db, v);
84949}
84950
84951/*
84952** The sqlite3ValueBytes() routine returns the number of bytes in the
84953** sqlite3_value object assuming that it uses the encoding "enc".
84954** The valueBytes() routine is a helper function.
84955*/
84956static SQLITE_NOINLINE__attribute__((noinline)) int valueBytes(sqlite3_value *pVal, u8 enc){
84957 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
84958}
84959SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
84960 Mem *p = (Mem*)pVal;
84961 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
84962 if( (p->flags & MEM_Str0x0002)!=0 && pVal->enc==enc ){
84963 return p->n;
84964 }
84965 if( (p->flags & MEM_Str0x0002)!=0 && enc!=SQLITE_UTF81 && pVal->enc!=SQLITE_UTF81 ){
84966 return p->n;
84967 }
84968 if( (p->flags & MEM_Blob0x0010)!=0 ){
84969 if( p->flags & MEM_Zero0x0400 ){
84970 return p->n + p->u.nZero;
84971 }else{
84972 return p->n;
84973 }
84974 }
84975 if( p->flags & MEM_Null0x0001 ) return 0;
84976 return valueBytes(pVal, enc);
84977}
84978
84979/************** End of vdbemem.c *********************************************/
84980/************** Begin file vdbeaux.c *****************************************/
84981/*
84982** 2003 September 6
84983**
84984** The author disclaims copyright to this source code. In place of
84985** a legal notice, here is a blessing:
84986**
84987** May you do good and not evil.
84988** May you find forgiveness for yourself and forgive others.
84989** May you share freely, never taking more than you give.
84990**
84991*************************************************************************
84992** This file contains code used for creating, destroying, and populating
84993** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
84994*/
84995/* #include "sqliteInt.h" */
84996/* #include "vdbeInt.h" */
84997
84998/* Forward references */
84999static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
85000static void vdbeFreeOpArray(sqlite3 *, Op *, int);
85001
85002/*
85003** Create a new virtual database engine.
85004*/
85005SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse *pParse){
85006 sqlite3 *db = pParse->db;
85007 Vdbe *p;
85008 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
85009 if( p==0 ) return 0;
85010 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)__builtin_offsetof(Vdbe, aOp));
85011 p->db = db;
85012 if( db->pVdbe ){
85013 db->pVdbe->ppVPrev = &p->pVNext;
85014 }
85015 p->pVNext = db->pVdbe;
85016 p->ppVPrev = &db->pVdbe;
85017 db->pVdbe = p;
85018 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
85019 p->pParse = pParse;
85020 pParse->pVdbe = p;
85021 assert( pParse->aLabel==0 )((void) (0));
85022 assert( pParse->nLabel==0 )((void) (0));
85023 assert( p->nOpAlloc==0 )((void) (0));
85024 assert( pParse->szOpAlloc==0 )((void) (0));
85025 sqlite3VdbeAddOp2(p, OP_Init8, 0, 1);
85026 return p;
85027}
85028
85029/*
85030** Return the Parse object that owns a Vdbe object.
85031*/
85032SQLITE_PRIVATEstatic Parse *sqlite3VdbeParser(Vdbe *p){
85033 return p->pParse;
85034}
85035
85036/*
85037** Change the error string stored in Vdbe.zErrMsg
85038*/
85039SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
85040 va_list ap;
85041 sqlite3DbFree(p->db, p->zErrMsg);
85042 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
85043 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
85044 va_end(ap)__builtin_va_end(ap);
85045}
85046
85047/*
85048** Remember the SQL string for a prepared statement.
85049*/
85050SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
85051 if( p==0 ) return;
85052 p->prepFlags = prepFlags;
85053 if( (prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ){
85054 p->expmask = 0;
85055 }
85056 assert( p->zSql==0 )((void) (0));
85057 p->zSql = sqlite3DbStrNDup(p->db, z, n);
85058}
85059
85060#ifdef SQLITE_ENABLE_NORMALIZE
85061/*
85062** Add a new element to the Vdbe->pDblStr list.
85063*/
85064SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
85065 if( p ){
85066 int n = sqlite3Strlen30(z);
85067 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
85068 sizeof(*pStr)+n+1-sizeof(pStr->z));
85069 if( pStr ){
85070 pStr->pNextStr = p->pDblStr;
85071 p->pDblStr = pStr;
85072 memcpy(pStr->z, z, n+1);
85073 }
85074 }
85075}
85076#endif
85077
85078#ifdef SQLITE_ENABLE_NORMALIZE
85079/*
85080** zId of length nId is a double-quoted identifier. Check to see if
85081** that identifier is really used as a string literal.
85082*/
85083SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(
85084 Vdbe *pVdbe, /* The prepared statement */
85085 const char *zId /* The double-quoted identifier, already dequoted */
85086){
85087 DblquoteStr *pStr;
85088 assert( zId!=0 )((void) (0));
85089 if( pVdbe->pDblStr==0 ) return 0;
85090 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
85091 if( strcmp(zId, pStr->z)==0 ) return 1;
85092 }
85093 return 0;
85094}
85095#endif
85096
85097/*
85098** Swap byte-code between two VDBE structures.
85099**
85100** This happens after pB was previously run and returned
85101** SQLITE_SCHEMA. The statement was then reprepared in pA.
85102** This routine transfers the new bytecode in pA over to pB
85103** so that pB can be run again. The old pB byte code is
85104** moved back to pA so that it will be cleaned up when pA is
85105** finalized.
85106*/
85107SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
85108 Vdbe tmp, *pTmp, **ppTmp;
85109 char *zTmp;
85110 assert( pA->db==pB->db )((void) (0));
85111 tmp = *pA;
85112 *pA = *pB;
85113 *pB = tmp;
85114 pTmp = pA->pVNext;
85115 pA->pVNext = pB->pVNext;
85116 pB->pVNext = pTmp;
85117 ppTmp = pA->ppVPrev;
85118 pA->ppVPrev = pB->ppVPrev;
85119 pB->ppVPrev = ppTmp;
85120 zTmp = pA->zSql;
85121 pA->zSql = pB->zSql;
85122 pB->zSql = zTmp;
85123#ifdef SQLITE_ENABLE_NORMALIZE
85124 zTmp = pA->zNormSql;
85125 pA->zNormSql = pB->zNormSql;
85126 pB->zNormSql = zTmp;
85127#endif
85128 pB->expmask = pA->expmask;
85129 pB->prepFlags = pA->prepFlags;
85130 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
85131 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE5]++;
85132}
85133
85134/*
85135** Resize the Vdbe.aOp array so that it is at least nOp elements larger
85136** than its current size. nOp is guaranteed to be less than or equal
85137** to 1024/sizeof(Op).
85138**
85139** If an out-of-memory error occurs while resizing the array, return
85140** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
85141** unchanged (this is so that any opcodes already allocated can be
85142** correctly deallocated along with the rest of the Vdbe).
85143*/
85144static int growOpArray(Vdbe *v, int nOp){
85145 VdbeOp *pNew;
85146 Parse *p = v->pParse;
85147
85148 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
85149 ** more frequent reallocs and hence provide more opportunities for
85150 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
85151 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
85152 ** by the minimum* amount required until the size reaches 512. Normal
85153 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
85154 ** size of the op array or add 1KB of space, whichever is smaller. */
85155#ifdef SQLITE_TEST_REALLOC_STRESS
85156 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
85157 : (sqlite3_int64)v->nOpAlloc+nOp);
85158#else
85159 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
85160 : (sqlite3_int64)(1024/sizeof(Op)));
85161 UNUSED_PARAMETER(nOp)(void)(nOp);
85162#endif
85163
85164 /* Ensure that the size of a VDBE does not grow too large */
85165 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP5] ){
85166 sqlite3OomFault(p->db);
85167 return SQLITE_NOMEM7;
85168 }
85169
85170 assert( nOp<=(int)(1024/sizeof(Op)) )((void) (0));
85171 assert( nNew>=(v->nOpAlloc+nOp) )((void) (0));
85172 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
85173 if( pNew ){
85174 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
85175 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
85176 v->aOp = pNew;
85177 }
85178 return (pNew ? SQLITE_OK0 : SQLITE_NOMEM_BKPT7);
85179}
85180
85181#ifdef SQLITE_DEBUG
85182/* This routine is just a convenient place to set a breakpoint that will
85183** fire after each opcode is inserted and displayed using
85184** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and
85185** pOp are available to make the breakpoint conditional.
85186**
85187** Other useful labels for breakpoints include:
85188** test_trace_breakpoint(pc,pOp)
85189** sqlite3CorruptError(lineno)
85190** sqlite3MisuseError(lineno)
85191** sqlite3CantopenError(lineno)
85192*/
85193static void test_addop_breakpoint(int pc, Op *pOp){
85194 static u64 n = 0;
85195 (void)pc;
85196 (void)pOp;
85197 n++;
85198 if( n==LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) ) abort(); /* so that n is used, preventing a warning */
85199}
85200#endif
85201
85202/*
85203** Slow paths for sqlite3VdbeAddOp3() and sqlite3VdbeAddOp4Int() for the
85204** unusual case when we need to increase the size of the Vdbe.aOp[] array
85205** before adding the new opcode.
85206*/
85207static SQLITE_NOINLINE__attribute__((noinline)) int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
85208 assert( p->nOpAlloc<=p->nOp )((void) (0));
85209 if( growOpArray(p, 1) ) return 1;
85210 assert( p->nOpAlloc>p->nOp )((void) (0));
85211 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
85212}
85213static SQLITE_NOINLINE__attribute__((noinline)) int addOp4IntSlow(
85214 Vdbe *p, /* Add the opcode to this VM */
85215 int op, /* The new opcode */
85216 int p1, /* The P1 operand */
85217 int p2, /* The P2 operand */
85218 int p3, /* The P3 operand */
85219 int p4 /* The P4 operand as an integer */
85220){
85221 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
85222 if( p->db->mallocFailed==0 ){
85223 VdbeOp *pOp = &p->aOp[addr];
85224 pOp->p4type = P4_INT32(-3);
85225 pOp->p4.i = p4;
85226 }
85227 return addr;
85228}
85229
85230
85231/*
85232** Add a new instruction to the list of instructions current in the
85233** VDBE. Return the address of the new instruction.
85234**
85235** Parameters:
85236**
85237** p Pointer to the VDBE
85238**
85239** op The opcode for this instruction
85240**
85241** p1, p2, p3, p4 Operands
85242*/
85243SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe *p, int op){
85244 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
85245}
85246SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
85247 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
85248}
85249SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
85250 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
85251}
85252SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
85253 int i;
85254 VdbeOp *pOp;
85255
85256 i = p->nOp;
85257 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
85258 assert( op>=0 && op<0xff )((void) (0));
85259 if( p->nOpAlloc<=i ){
85260 return growOp3(p, op, p1, p2, p3);
85261 }
85262 assert( p->aOp!=0 )((void) (0));
85263 p->nOp++;
85264 pOp = &p->aOp[i];
85265 assert( pOp!=0 )((void) (0));
85266 pOp->opcode = (u8)op;
85267 pOp->p5 = 0;
85268 pOp->p1 = p1;
85269 pOp->p2 = p2;
85270 pOp->p3 = p3;
85271 pOp->p4.p = 0;
85272 pOp->p4type = P4_NOTUSED0;
85273
85274 /* Replicate this logic in sqlite3VdbeAddOp4Int()
85275 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
85276#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
85277 pOp->zComment = 0;
85278#endif
85279#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
85280 pOp->nExec = 0;
85281 pOp->nCycle = 0;
85282#endif
85283#ifdef SQLITE_DEBUG
85284 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85285 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
85286 test_addop_breakpoint(i, &p->aOp[i]);
85287 }
85288#endif
85289#ifdef SQLITE_VDBE_COVERAGE
85290 pOp->iSrcLine = 0;
85291#endif
85292 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85293 ** Replicate in sqlite3VdbeAddOp4Int() */
85294
85295 return i;
85296}
85297SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(
85298 Vdbe *p, /* Add the opcode to this VM */
85299 int op, /* The new opcode */
85300 int p1, /* The P1 operand */
85301 int p2, /* The P2 operand */
85302 int p3, /* The P3 operand */
85303 int p4 /* The P4 operand as an integer */
85304){
85305 int i;
85306 VdbeOp *pOp;
85307
85308 i = p->nOp;
85309 if( p->nOpAlloc<=i ){
85310 return addOp4IntSlow(p, op, p1, p2, p3, p4);
85311 }
85312 p->nOp++;
85313 pOp = &p->aOp[i];
85314 assert( pOp!=0 )((void) (0));
85315 pOp->opcode = (u8)op;
85316 pOp->p5 = 0;
85317 pOp->p1 = p1;
85318 pOp->p2 = p2;
85319 pOp->p3 = p3;
85320 pOp->p4.i = p4;
85321 pOp->p4type = P4_INT32(-3);
85322
85323 /* Replicate this logic in sqlite3VdbeAddOp3()
85324 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */
85325#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
85326 pOp->zComment = 0;
85327#endif
85328#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
85329 pOp->nExec = 0;
85330 pOp->nCycle = 0;
85331#endif
85332#ifdef SQLITE_DEBUG
85333 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85334 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
85335 test_addop_breakpoint(i, &p->aOp[i]);
85336 }
85337#endif
85338#ifdef SQLITE_VDBE_COVERAGE
85339 pOp->iSrcLine = 0;
85340#endif
85341 /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85342 ** Replicate in sqlite3VdbeAddOp3() */
85343
85344 return i;
85345}
85346
85347/* Generate code for an unconditional jump to instruction iDest
85348*/
85349SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe *p, int iDest){
85350 return sqlite3VdbeAddOp3(p, OP_Goto9, 0, iDest, 0);
85351}
85352
85353/* Generate code to cause the string zStr to be loaded into
85354** register iDest
85355*/
85356SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
85357 return sqlite3VdbeAddOp4(p, OP_String8118, 0, iDest, 0, zStr, 0);
85358}
85359
85360/*
85361** Generate code that initializes multiple registers to string or integer
85362** constants. The registers begin with iDest and increase consecutively.
85363** One register is initialized for each characgter in zTypes[]. For each
85364** "s" character in zTypes[], the register is a string if the argument is
85365** not NULL, or OP_Null if the value is a null pointer. For each "i" character
85366** in zTypes[], the register is initialized to an integer.
85367**
85368** If the input string does not end with "X" then an OP_ResultRow instruction
85369** is generated for the values inserted.
85370*/
85371SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
85372 va_list ap;
85373 int i;
85374 char c;
85375 va_start(ap, zTypes)__builtin_va_start(ap, zTypes);
85376 for(i=0; (c = zTypes[i])!=0; i++){
85377 if( c=='s' ){
85378 const char *z = va_arg(ap, const char*)__builtin_va_arg(ap, const char*);
85379 sqlite3VdbeAddOp4(p, z==0 ? OP_Null75 : OP_String8118, 0, iDest+i, 0, z, 0);
85380 }else if( c=='i' ){
85381 sqlite3VdbeAddOp2(p, OP_Integer71, va_arg(ap, int)__builtin_va_arg(ap, int), iDest+i);
85382 }else{
85383 goto skip_op_resultrow;
85384 }
85385 }
85386 sqlite3VdbeAddOp2(p, OP_ResultRow84, iDest, i);
85387skip_op_resultrow:
85388 va_end(ap)__builtin_va_end(ap);
85389}
85390
85391/*
85392** Add an opcode that includes the p4 value as a pointer.
85393*/
85394SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(
85395 Vdbe *p, /* Add the opcode to this VM */
85396 int op, /* The new opcode */
85397 int p1, /* The P1 operand */
85398 int p2, /* The P2 operand */
85399 int p3, /* The P3 operand */
85400 const char *zP4, /* The P4 operand */
85401 int p4type /* P4 operand type */
85402){
85403 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
85404 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
85405 return addr;
85406}
85407
85408/*
85409** Add an OP_Function or OP_PureFunc opcode.
85410**
85411** The eCallCtx argument is information (typically taken from Expr.op2)
85412** that describes the calling context of the function. 0 means a general
85413** function call. NC_IsCheck means called by a check constraint,
85414** NC_IdxExpr means called as part of an index expression. NC_PartIdx
85415** means in the WHERE clause of a partial index. NC_GenCol means called
85416** while computing a generated column value. 0 is the usual case.
85417*/
85418SQLITE_PRIVATEstatic int sqlite3VdbeAddFunctionCall(
85419 Parse *pParse, /* Parsing context */
85420 int p1, /* Constant argument mask */
85421 int p2, /* First argument register */
85422 int p3, /* Register into which results are written */
85423 int nArg, /* Number of argument */
85424 const FuncDef *pFunc, /* The function to be invoked */
85425 int eCallCtx /* Calling context */
85426){
85427 Vdbe *v = pParse->pVdbe;
85428 int nByte;
85429 int addr;
85430 sqlite3_context *pCtx;
85431 assert( v )((void) (0));
85432 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
85433 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
85434 if( pCtx==0 ){
85435 assert( pParse->db->mallocFailed )((void) (0));
85436 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
85437 return 0;
85438 }
85439 pCtx->pOut = 0;
85440 pCtx->pFunc = (FuncDef*)pFunc;
85441 pCtx->pVdbe = 0;
85442 pCtx->isError = 0;
85443 pCtx->argc = nArg;
85444 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
85445 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc65 : OP_Function66,
85446 p1, p2, p3, (char*)pCtx, P4_FUNCCTX(-15));
85447 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef0x00002e);
85448 sqlite3MayAbort(pParse);
85449 return addr;
85450}
85451
85452/*
85453** Add an opcode that includes the p4 value with a P4_INT64 or
85454** P4_REAL type.
85455*/
85456SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(
85457 Vdbe *p, /* Add the opcode to this VM */
85458 int op, /* The new opcode */
85459 int p1, /* The P1 operand */
85460 int p2, /* The P2 operand */
85461 int p3, /* The P3 operand */
85462 const u8 *zP4, /* The P4 operand */
85463 int p4type /* P4 operand type */
85464){
85465 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
85466 if( p4copy ) memcpy(p4copy, zP4, 8);
85467 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
85468}
85469
85470#ifndef SQLITE_OMIT_EXPLAIN
85471/*
85472** Return the address of the current EXPLAIN QUERY PLAN baseline.
85473** 0 means "none".
85474*/
85475SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse *pParse){
85476 VdbeOp *pOp;
85477 if( pParse->addrExplain==0 ) return 0;
85478 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
85479 return pOp->p2;
85480}
85481
85482/*
85483** Set a debugger breakpoint on the following routine in order to
85484** monitor the EXPLAIN QUERY PLAN code generation.
85485*/
85486#if defined(SQLITE_DEBUG)
85487SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
85488 (void)z1;
85489 (void)z2;
85490}
85491#endif
85492
85493/*
85494** Add a new OP_Explain opcode.
85495**
85496** If the bPush flag is true, then make this opcode the parent for
85497** subsequent Explains until sqlite3VdbeExplainPop() is called.
85498*/
85499SQLITE_PRIVATEstatic int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
85500 int addr = 0;
85501#if !defined(SQLITE_DEBUG)
85502 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
85503 ** But omit them (for performance) during production builds */
85504 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) )
85505#endif
85506 {
85507 char *zMsg;
85508 Vdbe *v;
85509 va_list ap;
85510 int iThis;
85511 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
85512 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
85513 va_end(ap)__builtin_va_end(ap);
85514 v = pParse->pVdbe;
85515 iThis = v->nOp;
85516 addr = sqlite3VdbeAddOp4(v, OP_Explain188, iThis, pParse->addrExplain, 0,
85517 zMsg, P4_DYNAMIC(-6));
85518 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
85519 if( bPush){
85520 pParse->addrExplain = iThis;
85521 }
85522 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
85523 }
85524 return addr;
85525}
85526
85527/*
85528** Pop the EXPLAIN QUERY PLAN stack one level.
85529*/
85530SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse *pParse){
85531 sqlite3ExplainBreakpoint("POP", 0);
85532 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
85533}
85534#endif /* SQLITE_OMIT_EXPLAIN */
85535
85536/*
85537** Add an OP_ParseSchema opcode. This routine is broken out from
85538** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
85539** as having been used.
85540**
85541** The zWhere string must have been obtained from sqlite3_malloc().
85542** This routine will take ownership of the allocated memory.
85543*/
85544SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
85545 int j;
85546 sqlite3VdbeAddOp4(p, OP_ParseSchema149, iDb, 0, 0, zWhere, P4_DYNAMIC(-6));
85547 sqlite3VdbeChangeP5(p, p5);
85548 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
85549 sqlite3MayAbort(p->pParse);
85550}
85551
85552/* Insert the end of a co-routine
85553*/
85554SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
85555 sqlite3VdbeAddOp1(v, OP_EndCoroutine68, regYield);
85556
85557 /* Clear the temporary register cache, thereby ensuring that each
85558 ** co-routine has its own independent set of registers, because co-routines
85559 ** might expect their registers to be preserved across an OP_Yield, and
85560 ** that could cause problems if two or more co-routines are using the same
85561 ** temporary register.
85562 */
85563 v->pParse->nTempReg = 0;
85564 v->pParse->nRangeReg = 0;
85565}
85566
85567/*
85568** Create a new symbolic label for an instruction that has yet to be
85569** coded. The symbolic label is really just a negative number. The
85570** label can be used as the P2 value of an operation. Later, when
85571** the label is resolved to a specific address, the VDBE will scan
85572** through its operation list and change all values of P2 which match
85573** the label into the resolved address.
85574**
85575** The VDBE knows that a P2 value is a label because labels are
85576** always negative and P2 values are suppose to be non-negative.
85577** Hence, a negative P2 value is a label that has yet to be resolved.
85578** (Later:) This is only true for opcodes that have the OPFLG_JUMP
85579** property.
85580**
85581** Variable usage notes:
85582**
85583** Parse.aLabel[x] Stores the address that the x-th label resolves
85584** into. For testing (SQLITE_DEBUG), unresolved
85585** labels stores -1, but that is not required.
85586** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
85587** Parse.nLabel The *negative* of the number of labels that have
85588** been issued. The negative is stored because
85589** that gives a performance improvement over storing
85590** the equivalent positive value.
85591*/
85592SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse *pParse){
85593 return --pParse->nLabel;
85594}
85595
85596/*
85597** Resolve label "x" to be the address of the next instruction to
85598** be inserted. The parameter "x" must have been obtained from
85599** a prior call to sqlite3VdbeMakeLabel().
85600*/
85601static SQLITE_NOINLINE__attribute__((noinline)) void resizeResolveLabel(Parse *p, Vdbe *v, int j){
85602 int nNewSize = 10 - p->nLabel;
85603 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
85604 nNewSize*sizeof(p->aLabel[0]));
85605 if( p->aLabel==0 ){
85606 p->nLabelAlloc = 0;
85607 }else{
85608#ifdef SQLITE_DEBUG
85609 int i;
85610 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
85611#endif
85612 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
85613 sqlite3ProgressCheck(p);
85614 }
85615 p->nLabelAlloc = nNewSize;
85616 p->aLabel[j] = v->nOp;
85617 }
85618}
85619SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe *v, int x){
85620 Parse *p = v->pParse;
85621 int j = ADDR(x)(~(x));
85622 assert( v->eVdbeState==VDBE_INIT_STATE )((void) (0));
85623 assert( j<-p->nLabel )((void) (0));
85624 assert( j>=0 )((void) (0));
85625#ifdef SQLITE_DEBUG
85626 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85627 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
85628 }
85629#endif
85630 if( p->nLabelAlloc + p->nLabel < 0 ){
85631 resizeResolveLabel(p,v,j);
85632 }else{
85633 assert( p->aLabel[j]==(-1) )((void) (0)); /* Labels may only be resolved once */
85634 p->aLabel[j] = v->nOp;
85635 }
85636}
85637
85638/*
85639** Mark the VDBE as one that can only be run one time.
85640*/
85641SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe *p){
85642 sqlite3VdbeAddOp2(p, OP_Expire166, 1, 1);
85643}
85644
85645/*
85646** Mark the VDBE as one that can be run multiple times.
85647*/
85648SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe *p){
85649 int i;
85650 for(i=1; ALWAYS(i<p->nOp)(i<p->nOp); i++){
85651 if( ALWAYS(p->aOp[i].opcode==OP_Expire)(p->aOp[i].opcode==166) ){
85652 p->aOp[1].opcode = OP_Noop187;
85653 break;
85654 }
85655 }
85656}
85657
85658#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
85659
85660/*
85661** The following type and function are used to iterate through all opcodes
85662** in a Vdbe main program and each of the sub-programs (triggers) it may
85663** invoke directly or indirectly. It should be used as follows:
85664**
85665** Op *pOp;
85666** VdbeOpIter sIter;
85667**
85668** memset(&sIter, 0, sizeof(sIter));
85669** sIter.v = v; // v is of type Vdbe*
85670** while( (pOp = opIterNext(&sIter)) ){
85671** // Do something with pOp
85672** }
85673** sqlite3DbFree(v->db, sIter.apSub);
85674**
85675*/
85676typedef struct VdbeOpIter VdbeOpIter;
85677struct VdbeOpIter {
85678 Vdbe *v; /* Vdbe to iterate through the opcodes of */
85679 SubProgram **apSub; /* Array of subprograms */
85680 int nSub; /* Number of entries in apSub */
85681 int iAddr; /* Address of next instruction to return */
85682 int iSub; /* 0 = main program, 1 = first sub-program etc. */
85683};
85684static Op *opIterNext(VdbeOpIter *p){
85685 Vdbe *v = p->v;
85686 Op *pRet = 0;
85687 Op *aOp;
85688 int nOp;
85689
85690 if( p->iSub<=p->nSub ){
85691
85692 if( p->iSub==0 ){
85693 aOp = v->aOp;
85694 nOp = v->nOp;
85695 }else{
85696 aOp = p->apSub[p->iSub-1]->aOp;
85697 nOp = p->apSub[p->iSub-1]->nOp;
85698 }
85699 assert( p->iAddr<nOp )((void) (0));
85700
85701 pRet = &aOp[p->iAddr];
85702 p->iAddr++;
85703 if( p->iAddr==nOp ){
85704 p->iSub++;
85705 p->iAddr = 0;
85706 }
85707
85708 if( pRet->p4type==P4_SUBPROGRAM(-4) ){
85709 int nByte = (p->nSub+1)*sizeof(SubProgram*);
85710 int j;
85711 for(j=0; j<p->nSub; j++){
85712 if( p->apSub[j]==pRet->p4.pProgram ) break;
85713 }
85714 if( j==p->nSub ){
85715 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
85716 if( !p->apSub ){
85717 pRet = 0;
85718 }else{
85719 p->apSub[p->nSub++] = pRet->p4.pProgram;
85720 }
85721 }
85722 }
85723 }
85724
85725 return pRet;
85726}
85727
85728/*
85729** Check if the program stored in the VM associated with pParse may
85730** throw an ABORT exception (causing the statement, but not entire transaction
85731** to be rolled back). This condition is true if the main program or any
85732** sub-programs contains any of the following:
85733**
85734** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
85735** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
85736** * OP_Destroy
85737** * OP_VUpdate
85738** * OP_VCreate
85739** * OP_VRename
85740** * OP_FkCounter with P2==0 (immediate foreign key constraint)
85741** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
85742** (for CREATE TABLE AS SELECT ...)
85743**
85744** Then check that the value of Parse.mayAbort is true if an
85745** ABORT may be thrown, or false otherwise. Return true if it does
85746** match, or false otherwise. This function is intended to be used as
85747** part of an assert statement in the compiler. Similar to:
85748**
85749** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
85750*/
85751SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
85752 int hasAbort = 0;
85753 int hasFkCounter = 0;
85754 int hasCreateTable = 0;
85755 int hasCreateIndex = 0;
85756 int hasInitCoroutine = 0;
85757 Op *pOp;
85758 VdbeOpIter sIter;
85759
85760 if( v==0 ) return 0;
85761 memset(&sIter, 0, sizeof(sIter));
85762 sIter.v = v;
85763
85764 while( (pOp = opIterNext(&sIter))!=0 ){
85765 int opcode = pOp->opcode;
85766 if( opcode==OP_Destroy144 || opcode==OP_VUpdate7 || opcode==OP_VRename177
85767 || opcode==OP_VDestroy172
85768 || opcode==OP_VCreate171
85769 || opcode==OP_ParseSchema149
85770 || opcode==OP_Function66 || opcode==OP_PureFunc65
85771 || ((opcode==OP_Halt70 || opcode==OP_HaltIfNull69)
85772 && ((pOp->p1)!=SQLITE_OK0 && pOp->p2==OE_Abort2))
85773 ){
85774 hasAbort = 1;
85775 break;
85776 }
85777 if( opcode==OP_CreateBtree147 && pOp->p3==BTREE_INTKEY1 ) hasCreateTable = 1;
85778 if( mayAbort ){
85779 /* hasCreateIndex may also be set for some DELETE statements that use
85780 ** OP_Clear. So this routine may end up returning true in the case
85781 ** where a "DELETE FROM tbl" has a statement-journal but does not
85782 ** require one. This is not so bad - it is an inefficiency, not a bug. */
85783 if( opcode==OP_CreateBtree147 && pOp->p3==BTREE_BLOBKEY2 ) hasCreateIndex = 1;
85784 if( opcode==OP_Clear145 ) hasCreateIndex = 1;
85785 }
85786 if( opcode==OP_InitCoroutine11 ) hasInitCoroutine = 1;
85787#ifndef SQLITE_OMIT_FOREIGN_KEY
85788 if( opcode==OP_FkCounter158 && pOp->p1==0 && pOp->p2==1 ){
85789 hasFkCounter = 1;
85790 }
85791#endif
85792 }
85793 sqlite3DbFree(v->db, sIter.apSub);
85794
85795 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
85796 ** If malloc failed, then the while() loop above may not have iterated
85797 ** through all opcodes and hasAbort may be set incorrectly. Return
85798 ** true for this case to prevent the assert() in the callers frame
85799 ** from failing. */
85800 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
85801 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
85802 );
85803}
85804#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
85805
85806#ifdef SQLITE_DEBUG
85807/*
85808** Increment the nWrite counter in the VDBE if the cursor is not an
85809** ephemeral cursor, or if the cursor argument is NULL.
85810*/
85811SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
85812 if( pC==0
85813 || (pC->eCurType!=CURTYPE_SORTER1
85814 && pC->eCurType!=CURTYPE_PSEUDO3
85815 && !pC->isEphemeral)
85816 ){
85817 p->nWrite++;
85818 }
85819}
85820#endif
85821
85822#ifdef SQLITE_DEBUG
85823/*
85824** Assert if an Abort at this point in time might result in a corrupt
85825** database.
85826*/
85827SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe *p){
85828 assert( p->nWrite==0 || p->usesStmtJournal )((void) (0));
85829}
85830#endif
85831
85832/*
85833** This routine is called after all opcodes have been inserted. It loops
85834** through all the opcodes and fixes up some details.
85835**
85836** (1) For each jump instruction with a negative P2 value (a label)
85837** resolve the P2 value to an actual address.
85838**
85839** (2) Compute the maximum number of arguments used by any SQL function
85840** and store that value in *pMaxFuncArgs.
85841**
85842** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
85843** indicate what the prepared statement actually does.
85844**
85845** (4) (discontinued)
85846**
85847** (5) Reclaim the memory allocated for storing labels.
85848**
85849** This routine will only function correctly if the mkopcodeh.tcl generator
85850** script numbers the opcodes correctly. Changes to this routine must be
85851** coordinated with changes to mkopcodeh.tcl.
85852*/
85853static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
85854 int nMaxArgs = *pMaxFuncArgs;
85855 Op *pOp;
85856 Parse *pParse = p->pParse;
85857 int *aLabel = pParse->aLabel;
85858
85859 assert( pParse->db->mallocFailed==0 )((void) (0)); /* tag-20230419-1 */
85860 p->readOnly = 1;
85861 p->bIsReader = 0;
85862 pOp = &p->aOp[p->nOp-1];
85863 assert( p->aOp[0].opcode==OP_Init )((void) (0));
85864 while( 1 /* Loop terminates when it reaches the OP_Init opcode */ ){
85865 /* Only JUMP opcodes and the short list of special opcodes in the switch
85866 ** below need to be considered. The mkopcodeh.tcl generator script groups
85867 ** all these opcodes together near the front of the opcode list. Skip
85868 ** any opcode that does not need processing by virtual of the fact that
85869 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
85870 */
85871 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE64 ){
85872 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
85873 ** cases from this switch! */
85874 switch( pOp->opcode ){
85875 case OP_Transaction2: {
85876 if( pOp->p2!=0 ) p->readOnly = 0;
85877 /* no break */ deliberate_fall_through__attribute__((fallthrough));
85878 }
85879 case OP_AutoCommit1:
85880 case OP_Savepoint0: {
85881 p->bIsReader = 1;
85882 break;
85883 }
85884#ifndef SQLITE_OMIT_WAL
85885 case OP_Checkpoint3:
85886#endif
85887 case OP_Vacuum5:
85888 case OP_JournalMode4: {
85889 p->readOnly = 0;
85890 p->bIsReader = 1;
85891 break;
85892 }
85893 case OP_Init8: {
85894 assert( pOp->p2>=0 )((void) (0));
85895 goto resolve_p2_values_loop_exit;
85896 }
85897#ifndef SQLITE_OMIT_VIRTUALTABLE
85898 case OP_VUpdate7: {
85899 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
85900 break;
85901 }
85902 case OP_VFilter6: {
85903 int n;
85904 assert( (pOp - p->aOp) >= 3 )((void) (0));
85905 assert( pOp[-1].opcode==OP_Integer )((void) (0));
85906 n = pOp[-1].p1;
85907 if( n>nMaxArgs ) nMaxArgs = n;
85908 /* Fall through into the default case */
85909 /* no break */ deliberate_fall_through__attribute__((fallthrough));
85910 }
85911#endif
85912 default: {
85913 if( pOp->p2<0 ){
85914 /* The mkopcodeh.tcl script has so arranged things that the only
85915 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85916 ** have non-negative values for P2. */
85917 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 )((void) (0));
85918 assert( ADDR(pOp->p2)<-pParse->nLabel )((void) (0));
85919 assert( aLabel!=0 )((void) (0)); /* True because of tag-20230419-1 */
85920 pOp->p2 = aLabel[ADDR(pOp->p2)(~(pOp->p2))];
85921 }
85922
85923 /* OPFLG_JUMP opcodes never have P2==0, though OPFLG_JUMP0 opcodes
85924 ** might */
85925 assert( pOp->p2>0((void) (0))
85926 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 )((void) (0));
85927
85928 /* Jumps never go off the end of the bytecode array */
85929 assert( pOp->p2<p->nOp((void) (0))
85930 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 )((void) (0));
85931 break;
85932 }
85933 }
85934 /* The mkopcodeh.tcl script has so arranged things that the only
85935 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85936 ** have non-negative values for P2. */
85937 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0)((void) (0));
85938 }
85939 assert( pOp>p->aOp )((void) (0));
85940 pOp--;
85941 }
85942resolve_p2_values_loop_exit:
85943 if( aLabel ){
85944 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
85945 pParse->aLabel = 0;
85946 }
85947 pParse->nLabel = 0;
85948 *pMaxFuncArgs = nMaxArgs;
85949 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) )((void) (0));
85950}
85951
85952#ifdef SQLITE_DEBUG
85953/*
85954** Check to see if a subroutine contains a jump to a location outside of
85955** the subroutine. If a jump outside the subroutine is detected, add code
85956** that will cause the program to halt with an error message.
85957**
85958** The subroutine consists of opcodes between iFirst and iLast. Jumps to
85959** locations within the subroutine are acceptable. iRetReg is a register
85960** that contains the return address. Jumps to outside the range of iFirst
85961** through iLast are also acceptable as long as the jump destination is
85962** an OP_Return to iReturnAddr.
85963**
85964** A jump to an unresolved label means that the jump destination will be
85965** beyond the current address. That is normally a jump to an early
85966** termination and is consider acceptable.
85967**
85968** This routine only runs during debug builds. The purpose is (of course)
85969** to detect invalid escapes out of a subroutine. The OP_Halt opcode
85970** is generated rather than an assert() or other error, so that ".eqp full"
85971** will still work to show the original bytecode, to aid in debugging.
85972*/
85973SQLITE_PRIVATEstatic void sqlite3VdbeNoJumpsOutsideSubrtn(
85974 Vdbe *v, /* The byte-code program under construction */
85975 int iFirst, /* First opcode of the subroutine */
85976 int iLast, /* Last opcode of the subroutine */
85977 int iRetReg /* Subroutine return address register */
85978){
85979 VdbeOp *pOp;
85980 Parse *pParse;
85981 int i;
85982 sqlite3_str *pErr = 0;
85983 assert( v!=0 )((void) (0));
85984 pParse = v->pParse;
85985 assert( pParse!=0 )((void) (0));
85986 if( pParse->nErr ) return;
85987 assert( iLast>=iFirst )((void) (0));
85988 assert( iLast<v->nOp )((void) (0));
85989 pOp = &v->aOp[iFirst];
85990 for(i=iFirst; i<=iLast; i++, pOp++){
85991 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0x01)!=0 ){
85992 int iDest = pOp->p2; /* Jump destination */
85993 if( iDest==0 ) continue;
85994 if( pOp->opcode==OP_Gosub10 ) continue;
85995 if( pOp->p3==20230325 && pOp->opcode==OP_NotNull52 ){
85996 /* This is a deliberately taken illegal branch. tag-20230325-2 */
85997 continue;
85998 }
85999 if( iDest<0 ){
86000 int j = ADDR(iDest)(~(iDest));
86001 assert( j>=0 )((void) (0));
86002 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
86003 continue;
86004 }
86005 iDest = pParse->aLabel[j];
86006 }
86007 if( iDest<iFirst || iDest>iLast ){
86008 int j = iDest;
86009 for(; j<v->nOp; j++){
86010 VdbeOp *pX = &v->aOp[j];
86011 if( pX->opcode==OP_Return67 ){
86012 if( pX->p1==iRetReg ) break;
86013 continue;
86014 }
86015 if( pX->opcode==OP_Noop187 ) continue;
86016 if( pX->opcode==OP_Explain188 ) continue;
86017 if( pErr==0 ){
86018 pErr = sqlite3_str_new(0);
86019 }else{
86020 sqlite3_str_appendchar(pErr, 1, '\n');
86021 }
86022 sqlite3_str_appendf(pErr,
86023 "Opcode at %d jumps to %d which is outside the "
86024 "subroutine at %d..%d",
86025 i, iDest, iFirst, iLast);
86026 break;
86027 }
86028 }
86029 }
86030 }
86031 if( pErr ){
86032 char *zErr = sqlite3_str_finish(pErr);
86033 sqlite3VdbeAddOp4(v, OP_Halt70, SQLITE_INTERNAL2, OE_Abort2, 0, zErr, 0);
86034 sqlite3_free(zErr);
86035 sqlite3MayAbort(pParse);
86036 }
86037}
86038#endif /* SQLITE_DEBUG */
86039
86040/*
86041** Return the address of the next instruction to be inserted.
86042*/
86043SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe *p){
86044 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
86045 return p->nOp;
86046}
86047
86048/*
86049** Verify that at least N opcode slots are available in p without
86050** having to malloc for more space (except when compiled using
86051** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
86052** to verify that certain calls to sqlite3VdbeAddOpList() can never
86053** fail due to a OOM fault and hence that the return value from
86054** sqlite3VdbeAddOpList() will always be non-NULL.
86055*/
86056#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
86057SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
86058 assert( p->nOp + N <= p->nOpAlloc )((void) (0));
86059}
86060#endif
86061
86062/*
86063** Verify that the VM passed as the only argument does not contain
86064** an OP_ResultRow opcode. Fail an assert() if it does. This is used
86065** by code in pragma.c to ensure that the implementation of certain
86066** pragmas comports with the flags specified in the mkpragmatab.tcl
86067** script.
86068*/
86069#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
86070SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
86071 int i;
86072 for(i=0; i<p->nOp; i++){
86073 assert( p->aOp[i].opcode!=OP_ResultRow )((void) (0));
86074 }
86075}
86076#endif
86077
86078/*
86079** Generate code (a single OP_Abortable opcode) that will
86080** verify that the VDBE program can safely call Abort in the current
86081** context.
86082*/
86083#if defined(SQLITE_DEBUG)
86084SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
86085 if( onError==OE_Abort2 ) sqlite3VdbeAddOp0(p, OP_Abortable189);
86086}
86087#endif
86088
86089/*
86090** This function returns a pointer to the array of opcodes associated with
86091** the Vdbe passed as the first argument. It is the callers responsibility
86092** to arrange for the returned array to be eventually freed using the
86093** vdbeFreeOpArray() function.
86094**
86095** Before returning, *pnOp is set to the number of entries in the returned
86096** array. Also, *pnMaxArg is set to the larger of its current value and
86097** the number of entries in the Vdbe.apArg[] array required to execute the
86098** returned program.
86099*/
86100SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
86101 VdbeOp *aOp = p->aOp;
86102 assert( aOp && !p->db->mallocFailed )((void) (0));
86103
86104 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
86105 assert( DbMaskAllZero(p->btreeMask) )((void) (0));
86106
86107 resolveP2Values(p, pnMaxArg);
86108 *pnOp = p->nOp;
86109 p->aOp = 0;
86110 return aOp;
86111}
86112
86113/*
86114** Add a whole list of operations to the operation stack. Return a
86115** pointer to the first operation inserted.
86116**
86117** Non-zero P2 arguments to jump instructions are automatically adjusted
86118** so that the jump target is relative to the first operation inserted.
86119*/
86120SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(
86121 Vdbe *p, /* Add opcodes to the prepared statement */
86122 int nOp, /* Number of opcodes to add */
86123 VdbeOpList const *aOp, /* The opcodes to be added */
86124 int iLineno /* Source-file line number of first opcode */
86125){
86126 int i;
86127 VdbeOp *pOut, *pFirst;
86128 assert( nOp>0 )((void) (0));
86129 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
86130 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
86131 return 0;
86132 }
86133 pFirst = pOut = &p->aOp[p->nOp];
86134 for(i=0; i<nOp; i++, aOp++, pOut++){
86135 pOut->opcode = aOp->opcode;
86136 pOut->p1 = aOp->p1;
86137 pOut->p2 = aOp->p2;
86138 assert( aOp->p2>=0 )((void) (0));
86139 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP0x01)!=0 && aOp->p2>0 ){
86140 pOut->p2 += p->nOp;
86141 }
86142 pOut->p3 = aOp->p3;
86143 pOut->p4type = P4_NOTUSED0;
86144 pOut->p4.p = 0;
86145 pOut->p5 = 0;
86146#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
86147 pOut->zComment = 0;
86148#endif
86149#ifdef SQLITE_VDBE_COVERAGE
86150 pOut->iSrcLine = iLineno+i;
86151#else
86152 (void)iLineno;
86153#endif
86154#ifdef SQLITE_DEBUG
86155 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86156 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
86157 }
86158#endif
86159 }
86160 p->nOp += nOp;
86161 return pFirst;
86162}
86163
86164#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
86165/*
86166** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
86167*/
86168SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(
86169 Vdbe *p, /* VM to add scanstatus() to */
86170 int addrExplain, /* Address of OP_Explain (or 0) */
86171 int addrLoop, /* Address of loop counter */
86172 int addrVisit, /* Address of rows visited counter */
86173 LogEst nEst, /* Estimated number of output rows */
86174 const char *zName /* Name of table or index being scanned */
86175){
86176 if( IS_STMT_SCANSTATUS(p->db)(p->db->flags & 0x00000400) ){
86177 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
86178 ScanStatus *aNew;
86179 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
86180 if( aNew ){
86181 ScanStatus *pNew = &aNew[p->nScan++];
86182 memset(pNew, 0, sizeof(ScanStatus));
86183 pNew->addrExplain = addrExplain;
86184 pNew->addrLoop = addrLoop;
86185 pNew->addrVisit = addrVisit;
86186 pNew->nEst = nEst;
86187 pNew->zName = sqlite3DbStrDup(p->db, zName);
86188 p->aScan = aNew;
86189 }
86190 }
86191}
86192
86193/*
86194** Add the range of instructions from addrStart to addrEnd (inclusive) to
86195** the set of those corresponding to the sqlite3_stmt_scanstatus() counters
86196** associated with the OP_Explain instruction at addrExplain. The
86197** sum of the sqlite3Hwtime() values for each of these instructions
86198** will be returned for SQLITE_SCANSTAT_NCYCLE requests.
86199*/
86200SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusRange(
86201 Vdbe *p,
86202 int addrExplain,
86203 int addrStart,
86204 int addrEnd
86205){
86206 if( IS_STMT_SCANSTATUS(p->db)(p->db->flags & 0x00000400) ){
86207 ScanStatus *pScan = 0;
86208 int ii;
86209 for(ii=p->nScan-1; ii>=0; ii--){
86210 pScan = &p->aScan[ii];
86211 if( pScan->addrExplain==addrExplain ) break;
86212 pScan = 0;
86213 }
86214 if( pScan ){
86215 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
86216 for(ii=0; ii<ArraySize(pScan->aAddrRange)((int)(sizeof(pScan->aAddrRange)/sizeof(pScan->aAddrRange
[0])))
; ii+=2){
86217 if( pScan->aAddrRange[ii]==0 ){
86218 pScan->aAddrRange[ii] = addrStart;
86219 pScan->aAddrRange[ii+1] = addrEnd;
86220 break;
86221 }
86222 }
86223 }
86224 }
86225}
86226
86227/*
86228** Set the addresses for the SQLITE_SCANSTAT_NLOOP and SQLITE_SCANSTAT_NROW
86229** counters for the query element associated with the OP_Explain at
86230** addrExplain.
86231*/
86232SQLITE_PRIVATEstatic void sqlite3VdbeScanStatusCounters(
86233 Vdbe *p,
86234 int addrExplain,
86235 int addrLoop,
86236 int addrVisit
86237){
86238 if( IS_STMT_SCANSTATUS(p->db)(p->db->flags & 0x00000400) ){
86239 ScanStatus *pScan = 0;
86240 int ii;
86241 for(ii=p->nScan-1; ii>=0; ii--){
86242 pScan = &p->aScan[ii];
86243 if( pScan->addrExplain==addrExplain ) break;
86244 pScan = 0;
86245 }
86246 if( pScan ){
86247 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
86248 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
86249 }
86250 }
86251}
86252#endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */
86253
86254
86255/*
86256** Change the value of the opcode, or P1, P2, P3, or P5 operands
86257** for a specific instruction.
86258*/
86259SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
86260 assert( addr>=0 )((void) (0));
86261 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
86262}
86263SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
86264 assert( addr>=0 )((void) (0));
86265 sqlite3VdbeGetOp(p,addr)->p1 = val;
86266}
86267SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
86268 assert( addr>=0 || p->db->mallocFailed )((void) (0));
86269 sqlite3VdbeGetOp(p,addr)->p2 = val;
86270}
86271SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
86272 assert( addr>=0 )((void) (0));
86273 sqlite3VdbeGetOp(p,addr)->p3 = val;
86274}
86275SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
86276 assert( p->nOp>0 || p->db->mallocFailed )((void) (0));
86277 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
86278}
86279
86280/*
86281** If the previous opcode is an OP_Column that delivers results
86282** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
86283** opcode.
86284*/
86285SQLITE_PRIVATEstatic void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
86286 VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
86287 if( pOp->p3==iDest && pOp->opcode==OP_Column94 ){
86288 pOp->p5 |= OPFLAG_TYPEOFARG0x80;
86289 }
86290}
86291
86292/*
86293** Change the P2 operand of instruction addr so that it points to
86294** the address of the next instruction to be coded.
86295*/
86296SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe *p, int addr){
86297 sqlite3VdbeChangeP2(p, addr, p->nOp);
86298}
86299
86300/*
86301** Change the P2 operand of the jump instruction at addr so that
86302** the jump lands on the next opcode. Or if the jump instruction was
86303** the previous opcode (and is thus a no-op) then simply back up
86304** the next instruction counter by one slot so that the jump is
86305** overwritten by the next inserted opcode.
86306**
86307** This routine is an optimization of sqlite3VdbeJumpHere() that
86308** strives to omit useless byte-code like this:
86309**
86310** 7 Once 0 8 0
86311** 8 ...
86312*/
86313SQLITE_PRIVATEstatic void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
86314 if( addr==p->nOp-1 ){
86315 assert( p->aOp[addr].opcode==OP_Once((void) (0))
86316 || p->aOp[addr].opcode==OP_If((void) (0))
86317 || p->aOp[addr].opcode==OP_FkIfZero )((void) (0));
86318 assert( p->aOp[addr].p4type==0 )((void) (0));
86319#ifdef SQLITE_VDBE_COVERAGE
86320 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
86321#endif
86322 p->nOp--;
86323 }else{
86324 sqlite3VdbeChangeP2(p, addr, p->nOp);
86325 }
86326}
86327
86328
86329/*
86330** If the input FuncDef structure is ephemeral, then free it. If
86331** the FuncDef is not ephemeral, then do nothing.
86332*/
86333static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
86334 assert( db!=0 )((void) (0));
86335 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM0x0010)!=0 ){
86336 sqlite3DbNNFreeNN(db, pDef);
86337 }
86338}
86339
86340/*
86341** Delete a P4 value if necessary.
86342*/
86343static SQLITE_NOINLINE__attribute__((noinline)) void freeP4Mem(sqlite3 *db, Mem *p){
86344 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
86345 sqlite3DbNNFreeNN(db, p);
86346}
86347static SQLITE_NOINLINE__attribute__((noinline)) void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
86348 assert( db!=0 )((void) (0));
86349 freeEphemeralFunction(db, p->pFunc);
86350 sqlite3DbNNFreeNN(db, p);
86351}
86352static void freeP4(sqlite3 *db, int p4type, void *p4){
86353 assert( db )((void) (0));
86354 switch( p4type ){
86355 case P4_FUNCCTX(-15): {
86356 freeP4FuncCtx(db, (sqlite3_context*)p4);
86357 break;
86358 }
86359 case P4_REAL(-12):
86360 case P4_INT64(-13):
86361 case P4_DYNAMIC(-6):
86362 case P4_INTARRAY(-14): {
86363 if( p4 ) sqlite3DbNNFreeNN(db, p4);
86364 break;
86365 }
86366 case P4_KEYINFO(-8): {
86367 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
86368 break;
86369 }
86370#ifdef SQLITE_ENABLE_CURSOR_HINTS
86371 case P4_EXPR(-9): {
86372 sqlite3ExprDelete(db, (Expr*)p4);
86373 break;
86374 }
86375#endif
86376 case P4_FUNCDEF(-7): {
86377 freeEphemeralFunction(db, (FuncDef*)p4);
86378 break;
86379 }
86380 case P4_MEM(-10): {
86381 if( db->pnBytesFreed==0 ){
86382 sqlite3ValueFree((sqlite3_value*)p4);
86383 }else{
86384 freeP4Mem(db, (Mem*)p4);
86385 }
86386 break;
86387 }
86388 case P4_VTAB(-11) : {
86389 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
86390 break;
86391 }
86392 case P4_TABLEREF(-16): {
86393 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
86394 break;
86395 }
86396 case P4_SUBRTNSIG(-17): {
86397 SubrtnSig *pSig = (SubrtnSig*)p4;
86398 sqlite3DbFree(db, pSig->zAff);
86399 sqlite3DbFree(db, pSig);
86400 break;
86401 }
86402 }
86403}
86404
86405/*
86406** Free the space allocated for aOp and any p4 values allocated for the
86407** opcodes contained within. If aOp is not NULL it is assumed to contain
86408** nOp entries.
86409*/
86410static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
86411 assert( nOp>=0 )((void) (0));
86412 assert( db!=0 )((void) (0));
86413 if( aOp ){
86414 Op *pOp = &aOp[nOp-1];
86415 while(1){ /* Exit via break */
86416 if( pOp->p4type <= P4_FREE_IF_LE(-6) ) freeP4(db, pOp->p4type, pOp->p4.p);
86417#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
86418 sqlite3DbFree(db, pOp->zComment);
86419#endif
86420 if( pOp==aOp ) break;
86421 pOp--;
86422 }
86423 sqlite3DbNNFreeNN(db, aOp);
86424 }
86425}
86426
86427/*
86428** Link the SubProgram object passed as the second argument into the linked
86429** list at Vdbe.pSubProgram. This list is used to delete all sub-program
86430** objects when the VM is no longer required.
86431*/
86432SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
86433 p->pNext = pVdbe->pProgram;
86434 pVdbe->pProgram = p;
86435}
86436
86437/*
86438** Return true if the given Vdbe has any SubPrograms.
86439*/
86440SQLITE_PRIVATEstatic int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
86441 return pVdbe->pProgram!=0;
86442}
86443
86444/*
86445** Change the opcode at addr into OP_Noop
86446*/
86447SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
86448 VdbeOp *pOp;
86449 if( p->db->mallocFailed ) return 0;
86450 assert( addr>=0 && addr<p->nOp )((void) (0));
86451 pOp = &p->aOp[addr];
86452 freeP4(p->db, pOp->p4type, pOp->p4.p);
86453 pOp->p4type = P4_NOTUSED0;
86454 pOp->p4.z = 0;
86455 pOp->opcode = OP_Noop187;
86456 return 1;
86457}
86458
86459/*
86460** If the last opcode is "op" and it is not a jump destination,
86461** then remove it. Return true if and only if an opcode was removed.
86462*/
86463SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
86464 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
86465 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
86466 }else{
86467 return 0;
86468 }
86469}
86470
86471#ifdef SQLITE_DEBUG
86472/*
86473** Generate an OP_ReleaseReg opcode to indicate that a range of
86474** registers, except any identified by mask, are no longer in use.
86475*/
86476SQLITE_PRIVATEstatic void sqlite3VdbeReleaseRegisters(
86477 Parse *pParse, /* Parsing context */
86478 int iFirst, /* Index of first register to be released */
86479 int N, /* Number of registers to release */
86480 u32 mask, /* Mask of registers to NOT release */
86481 int bUndefine /* If true, mark registers as undefined */
86482){
86483 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg)(((pParse->db)->dbOptFlags&(0x00400000))!=0) ) return;
86484 assert( pParse->pVdbe )((void) (0));
86485 assert( iFirst>=1 )((void) (0));
86486 assert( iFirst+N-1<=pParse->nMem )((void) (0));
86487 if( N<=31 && mask!=0 ){
86488 while( N>0 && (mask&1)!=0 ){
86489 mask >>= 1;
86490 iFirst++;
86491 N--;
86492 }
86493 while( N>0 && N<=32 && (mask & MASKBIT32(N-1)(((unsigned int)1)<<(N-1)))!=0 ){
86494 mask &= ~MASKBIT32(N-1)(((unsigned int)1)<<(N-1));
86495 N--;
86496 }
86497 }
86498 if( N>0 ){
86499 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg186, iFirst, N, *(int*)&mask);
86500 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
86501 }
86502}
86503#endif /* SQLITE_DEBUG */
86504
86505/*
86506** Change the value of the P4 operand for a specific instruction.
86507** This routine is useful when a large program is loaded from a
86508** static array using sqlite3VdbeAddOpList but we want to make a
86509** few minor changes to the program.
86510**
86511** If n>=0 then the P4 operand is dynamic, meaning that a copy of
86512** the string is made into memory obtained from sqlite3_malloc().
86513** A value of n==0 means copy bytes of zP4 up to and including the
86514** first null byte. If n>0 then copy n+1 bytes of zP4.
86515**
86516** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
86517** to a string or structure that is guaranteed to exist for the lifetime of
86518** the Vdbe. In these cases we can just copy the pointer.
86519**
86520** If addr<0 then change P4 on the most recently inserted instruction.
86521*/
86522static void SQLITE_NOINLINE__attribute__((noinline)) vdbeChangeP4Full(
86523 Vdbe *p,
86524 Op *pOp,
86525 const char *zP4,
86526 int n
86527){
86528 if( pOp->p4type ){
86529 assert( pOp->p4type > P4_FREE_IF_LE )((void) (0));
86530 pOp->p4type = 0;
86531 pOp->p4.p = 0;
86532 }
86533 if( n<0 ){
86534 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
86535 }else{
86536 if( n==0 ) n = sqlite3Strlen30(zP4);
86537 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
86538 pOp->p4type = P4_DYNAMIC(-6);
86539 }
86540}
86541SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
86542 Op *pOp;
86543 sqlite3 *db;
86544 assert( p!=0 )((void) (0));
86545 db = p->db;
86546 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
86547 assert( p->aOp!=0 || db->mallocFailed )((void) (0));
86548 if( db->mallocFailed ){
86549 if( n!=P4_VTAB(-11) ) freeP4(db, n, (void*)*(char**)&zP4);
86550 return;
86551 }
86552 assert( p->nOp>0 )((void) (0));
86553 assert( addr<p->nOp )((void) (0));
86554 if( addr<0 ){
86555 addr = p->nOp - 1;
86556 }
86557 pOp = &p->aOp[addr];
86558 if( n>=0 || pOp->p4type ){
86559 vdbeChangeP4Full(p, pOp, zP4, n);
86560 return;
86561 }
86562 if( n==P4_INT32(-3) ){
86563 /* Note: this cast is safe, because the origin data point was an int
86564 ** that was cast to a (const char *). */
86565 pOp->p4.i = SQLITE_PTR_TO_INT(zP4)((int)(long int)(zP4));
86566 pOp->p4type = P4_INT32(-3);
86567 }else if( zP4!=0 ){
86568 assert( n<0 )((void) (0));
86569 pOp->p4.p = (void*)zP4;
86570 pOp->p4type = (signed char)n;
86571 if( n==P4_VTAB(-11) ) sqlite3VtabLock((VTable*)zP4);
86572 }
86573}
86574
86575/*
86576** Change the P4 operand of the most recently coded instruction
86577** to the value defined by the arguments. This is a high-speed
86578** version of sqlite3VdbeChangeP4().
86579**
86580** The P4 operand must not have been previously defined. And the new
86581** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
86582** those cases.
86583*/
86584SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
86585 VdbeOp *pOp;
86586 assert( n!=P4_INT32 && n!=P4_VTAB )((void) (0));
86587 assert( n<=0 )((void) (0));
86588 if( p->db->mallocFailed ){
86589 freeP4(p->db, n, pP4);
86590 }else{
86591 assert( pP4!=0 || n==P4_DYNAMIC )((void) (0));
86592 assert( p->nOp>0 )((void) (0));
86593 pOp = &p->aOp[p->nOp-1];
86594 assert( pOp->p4type==P4_NOTUSED )((void) (0));
86595 pOp->p4type = n;
86596 pOp->p4.p = pP4;
86597 }
86598}
86599
86600/*
86601** Set the P4 on the most recently added opcode to the KeyInfo for the
86602** index given.
86603*/
86604SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
86605 Vdbe *v = pParse->pVdbe;
86606 KeyInfo *pKeyInfo;
86607 assert( v!=0 )((void) (0));
86608 assert( pIdx!=0 )((void) (0));
86609 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
86610 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
86611}
86612
86613#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
86614/*
86615** Change the comment on the most recently coded instruction. Or
86616** insert a No-op and add the comment to that new instruction. This
86617** makes the code easier to read during debugging. None of this happens
86618** in a production build.
86619*/
86620static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
86621 assert( p->nOp>0 || p->aOp==0 )((void) (0));
86622 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 )((void) (0));
86623 if( p->nOp ){
86624 assert( p->aOp )((void) (0));
86625 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
86626 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
86627 }
86628}
86629SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
86630 va_list ap;
86631 if( p ){
86632 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
86633 vdbeVComment(p, zFormat, ap);
86634 va_end(ap)__builtin_va_end(ap);
86635 }
86636}
86637SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
86638 va_list ap;
86639 if( p ){
86640 sqlite3VdbeAddOp0(p, OP_Noop187);
86641 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
86642 vdbeVComment(p, zFormat, ap);
86643 va_end(ap)__builtin_va_end(ap);
86644 }
86645}
86646#endif /* NDEBUG */
86647
86648#ifdef SQLITE_VDBE_COVERAGE
86649/*
86650** Set the value if the iSrcLine field for the previously coded instruction.
86651*/
86652SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
86653 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
86654}
86655#endif /* SQLITE_VDBE_COVERAGE */
86656
86657/*
86658** Return the opcode for a given address. The address must be non-negative.
86659** See sqlite3VdbeGetLastOp() to get the most recently added opcode.
86660**
86661** If a memory allocation error has occurred prior to the calling of this
86662** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
86663** is readable but not writable, though it is cast to a writable value.
86664** The return of a dummy opcode allows the call to continue functioning
86665** after an OOM fault without having to check to see if the return from
86666** this routine is a valid pointer. But because the dummy.opcode is 0,
86667** dummy will never be written to. This is verified by code inspection and
86668** by running with Valgrind.
86669*/
86670SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
86671 /* C89 specifies that the constant "dummy" will be initialized to all
86672 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
86673 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
86674 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
86675 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed )((void) (0));
86676 if( p->db->mallocFailed ){
86677 return (VdbeOp*)&dummy;
86678 }else{
86679 return &p->aOp[addr];
86680 }
86681}
86682
86683/* Return the most recently added opcode
86684*/
86685SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetLastOp(Vdbe *p){
86686 return sqlite3VdbeGetOp(p, p->nOp - 1);
86687}
86688
86689#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
86690/*
86691** Return an integer value for one of the parameters to the opcode pOp
86692** determined by character c.
86693*/
86694static int translateP(char c, const Op *pOp){
86695 if( c=='1' ) return pOp->p1;
86696 if( c=='2' ) return pOp->p2;
86697 if( c=='3' ) return pOp->p3;
86698 if( c=='4' ) return pOp->p4.i;
86699 return pOp->p5;
86700}
86701
86702/*
86703** Compute a string for the "comment" field of a VDBE opcode listing.
86704**
86705** The Synopsis: field in comments in the vdbe.c source file gets converted
86706** to an extra string that is appended to the sqlite3OpcodeName(). In the
86707** absence of other comments, this synopsis becomes the comment on the opcode.
86708** Some translation occurs:
86709**
86710** "PX" -> "r[X]"
86711** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
86712** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
86713** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
86714*/
86715SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayComment(
86716 sqlite3 *db, /* Optional - Oom error reporting only */
86717 const Op *pOp, /* The opcode to be commented */
86718 const char *zP4 /* Previously obtained value for P4 */
86719){
86720 const char *zOpName;
86721 const char *zSynopsis;
86722 int nOpName;
86723 int ii;
86724 char zAlt[50];
86725 StrAccum x;
86726
86727 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH2147483645);
86728 zOpName = sqlite3OpcodeName(pOp->opcode);
86729 nOpName = sqlite3Strlen30(zOpName);
86730 if( zOpName[nOpName+1] ){
86731 int seenCom = 0;
86732 char c;
86733 zSynopsis = zOpName + nOpName + 1;
86734 if( strncmp(zSynopsis,"IF ",3)==0 ){
86735 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
86736 zSynopsis = zAlt;
86737 }
86738 for(ii=0; (c = zSynopsis[ii])!=0; ii++){
86739 if( c=='P' ){
86740 c = zSynopsis[++ii];
86741 if( c=='4' ){
86742 sqlite3_str_appendall(&x, zP4);
86743 }else if( c=='X' ){
86744 if( pOp->zComment && pOp->zComment[0] ){
86745 sqlite3_str_appendall(&x, pOp->zComment);
86746 seenCom = 1;
86747 break;
86748 }
86749 }else{
86750 int v1 = translateP(c, pOp);
86751 int v2;
86752 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
86753 ii += 3;
86754 v2 = translateP(zSynopsis[ii], pOp);
86755 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
86756 ii += 2;
86757 v2++;
86758 }
86759 if( v2<2 ){
86760 sqlite3_str_appendf(&x, "%d", v1);
86761 }else{
86762 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
86763 }
86764 }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
86765 sqlite3_context *pCtx = pOp->p4.pCtx;
86766 if( pOp->p4type!=P4_FUNCCTX(-15) || pCtx->argc==1 ){
86767 sqlite3_str_appendf(&x, "%d", v1);
86768 }else if( pCtx->argc>1 ){
86769 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
86770 }else if( x.accError==0 ){
86771 assert( x.nChar>2 )((void) (0));
86772 x.nChar -= 2;
86773 ii++;
86774 }
86775 ii += 3;
86776 }else{
86777 sqlite3_str_appendf(&x, "%d", v1);
86778 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
86779 ii += 4;
86780 }
86781 }
86782 }
86783 }else{
86784 sqlite3_str_appendchar(&x, 1, c);
86785 }
86786 }
86787 if( !seenCom && pOp->zComment ){
86788 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
86789 }
86790 }else if( pOp->zComment ){
86791 sqlite3_str_appendall(&x, pOp->zComment);
86792 }
86793 if( (x.accError & SQLITE_NOMEM7)!=0 && db!=0 ){
86794 sqlite3OomFault(db);
86795 }
86796 return sqlite3StrAccumFinish(&x);
86797}
86798#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */
86799
86800#if VDBE_DISPLAY_P41 && defined(SQLITE_ENABLE_CURSOR_HINTS)
86801/*
86802** Translate the P4.pExpr value for an OP_CursorHint opcode into text
86803** that can be displayed in the P4 column of EXPLAIN output.
86804*/
86805static void displayP4Expr(StrAccum *p, Expr *pExpr){
86806 const char *zOp = 0;
86807 switch( pExpr->op ){
86808 case TK_STRING118:
86809 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
86810 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
86811 break;
86812 case TK_INTEGER156:
86813 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
86814 break;
86815 case TK_NULL122:
86816 sqlite3_str_appendf(p, "NULL");
86817 break;
86818 case TK_REGISTER176: {
86819 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
86820 break;
86821 }
86822 case TK_COLUMN168: {
86823 if( pExpr->iColumn<0 ){
86824 sqlite3_str_appendf(p, "rowid");
86825 }else{
86826 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
86827 }
86828 break;
86829 }
86830 case TK_LT57: zOp = "LT"; break;
86831 case TK_LE56: zOp = "LE"; break;
86832 case TK_GT55: zOp = "GT"; break;
86833 case TK_GE58: zOp = "GE"; break;
86834 case TK_NE53: zOp = "NE"; break;
86835 case TK_EQ54: zOp = "EQ"; break;
86836 case TK_IS45: zOp = "IS"; break;
86837 case TK_ISNOT46: zOp = "ISNOT"; break;
86838 case TK_AND44: zOp = "AND"; break;
86839 case TK_OR43: zOp = "OR"; break;
86840 case TK_PLUS107: zOp = "ADD"; break;
86841 case TK_STAR109: zOp = "MUL"; break;
86842 case TK_MINUS108: zOp = "SUB"; break;
86843 case TK_REM111: zOp = "REM"; break;
86844 case TK_BITAND103: zOp = "BITAND"; break;
86845 case TK_BITOR104: zOp = "BITOR"; break;
86846 case TK_SLASH110: zOp = "DIV"; break;
86847 case TK_LSHIFT105: zOp = "LSHIFT"; break;
86848 case TK_RSHIFT106: zOp = "RSHIFT"; break;
86849 case TK_CONCAT112: zOp = "CONCAT"; break;
86850 case TK_UMINUS174: zOp = "MINUS"; break;
86851 case TK_UPLUS173: zOp = "PLUS"; break;
86852 case TK_BITNOT115: zOp = "BITNOT"; break;
86853 case TK_NOT19: zOp = "NOT"; break;
86854 case TK_ISNULL51: zOp = "ISNULL"; break;
86855 case TK_NOTNULL52: zOp = "NOTNULL"; break;
86856
86857 default:
86858 sqlite3_str_appendf(p, "%s", "expr");
86859 break;
86860 }
86861
86862 if( zOp ){
86863 sqlite3_str_appendf(p, "%s(", zOp);
86864 displayP4Expr(p, pExpr->pLeft);
86865 if( pExpr->pRight ){
86866 sqlite3_str_append(p, ",", 1);
86867 displayP4Expr(p, pExpr->pRight);
86868 }
86869 sqlite3_str_append(p, ")", 1);
86870 }
86871}
86872#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
86873
86874
86875#if VDBE_DISPLAY_P41
86876/*
86877** Compute a string that describes the P4 parameter for an opcode.
86878** Use zTemp for any required temporary buffer space.
86879*/
86880SQLITE_PRIVATEstatic char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
86881 char *zP4 = 0;
86882 StrAccum x;
86883
86884 sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH2147483645);
86885 switch( pOp->p4type ){
86886 case P4_KEYINFO(-8): {
86887 int j;
86888 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
86889 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
86890 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
86891 for(j=0; j<pKeyInfo->nKeyField; j++){
86892 CollSeq *pColl = pKeyInfo->aColl[j];
86893 const char *zColl = pColl ? pColl->zName : "";
86894 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
86895 sqlite3_str_appendf(&x, ",%s%s%s",
86896 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC0x01) ? "-" : "",
86897 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL0x02)? "N." : "",
86898 zColl);
86899 }
86900 sqlite3_str_append(&x, ")", 1);
86901 break;
86902 }
86903#ifdef SQLITE_ENABLE_CURSOR_HINTS
86904 case P4_EXPR(-9): {
86905 displayP4Expr(&x, pOp->p4.pExpr);
86906 break;
86907 }
86908#endif
86909 case P4_COLLSEQ(-2): {
86910 static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
86911 CollSeq *pColl = pOp->p4.pColl;
86912 assert( pColl->enc<4 )((void) (0));
86913 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
86914 encnames[pColl->enc]);
86915 break;
86916 }
86917 case P4_FUNCDEF(-7): {
86918 FuncDef *pDef = pOp->p4.pFunc;
86919 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
86920 break;
86921 }
86922 case P4_FUNCCTX(-15): {
86923 FuncDef *pDef = pOp->p4.pCtx->pFunc;
86924 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
86925 break;
86926 }
86927 case P4_INT64(-13): {
86928 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
86929 break;
86930 }
86931 case P4_INT32(-3): {
86932 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
86933 break;
86934 }
86935 case P4_REAL(-12): {
86936 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
86937 break;
86938 }
86939 case P4_MEM(-10): {
86940 Mem *pMem = pOp->p4.pMem;
86941 if( pMem->flags & MEM_Str0x0002 ){
86942 zP4 = pMem->z;
86943 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
86944 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
86945 }else if( pMem->flags & MEM_Real0x0008 ){
86946 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
86947 }else if( pMem->flags & MEM_Null0x0001 ){
86948 zP4 = "NULL";
86949 }else{
86950 assert( pMem->flags & MEM_Blob )((void) (0));
86951 zP4 = "(blob)";
86952 }
86953 break;
86954 }
86955#ifndef SQLITE_OMIT_VIRTUALTABLE
86956 case P4_VTAB(-11): {
86957 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
86958 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
86959 break;
86960 }
86961#endif
86962 case P4_INTARRAY(-14): {
86963 u32 i;
86964 u32 *ai = pOp->p4.ai;
86965 u32 n = ai[0]; /* The first element of an INTARRAY is always the
86966 ** count of the number of elements to follow */
86967 for(i=1; i<=n; i++){
86968 sqlite3_str_appendf(&x, "%c%u", (i==1 ? '[' : ','), ai[i]);
86969 }
86970 sqlite3_str_append(&x, "]", 1);
86971 break;
86972 }
86973 case P4_SUBPROGRAM(-4): {
86974 zP4 = "program";
86975 break;
86976 }
86977 case P4_TABLE(-5): {
86978 zP4 = pOp->p4.pTab->zName;
86979 break;
86980 }
86981 case P4_SUBRTNSIG(-17): {
86982 SubrtnSig *pSig = pOp->p4.pSubrtnSig;
86983 sqlite3_str_appendf(&x, "subrtnsig:%d,%s", pSig->selId, pSig->zAff);
86984 break;
86985 }
86986 default: {
86987 zP4 = pOp->p4.z;
86988 }
86989 }
86990 if( zP4 ) sqlite3_str_appendall(&x, zP4);
86991 if( (x.accError & SQLITE_NOMEM7)!=0 ){
86992 sqlite3OomFault(db);
86993 }
86994 return sqlite3StrAccumFinish(&x);
86995}
86996#endif /* VDBE_DISPLAY_P4 */
86997
86998/*
86999** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
87000**
87001** The prepared statements need to know in advance the complete set of
87002** attached databases that will be use. A mask of these databases
87003** is maintained in p->btreeMask. The p->lockMask value is the subset of
87004** p->btreeMask of databases that will require a lock.
87005*/
87006SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe *p, int i){
87007 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 )((void) (0));
87008 assert( i<(int)sizeof(p->btreeMask)*8 )((void) (0));
87009 DbMaskSet(p->btreeMask, i)((p->btreeMask)|=(((yDbMask)1)<<(i)));
87010 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
87011 DbMaskSet(p->lockMask, i)((p->lockMask)|=(((yDbMask)1)<<(i)));
87012 }
87013}
87014
87015#if !defined(SQLITE_OMIT_SHARED_CACHE)
87016/*
87017** If SQLite is compiled to support shared-cache mode and to be threadsafe,
87018** this routine obtains the mutex associated with each BtShared structure
87019** that may be accessed by the VM passed as an argument. In doing so it also
87020** sets the BtShared.db member of each of the BtShared structures, ensuring
87021** that the correct busy-handler callback is invoked if required.
87022**
87023** If SQLite is not threadsafe but does support shared-cache mode, then
87024** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
87025** of all of BtShared structures accessible via the database handle
87026** associated with the VM.
87027**
87028** If SQLite is not threadsafe and does not support shared-cache mode, this
87029** function is a no-op.
87030**
87031** The p->btreeMask field is a bitmask of all btrees that the prepared
87032** statement p will ever use. Let N be the number of bits in p->btreeMask
87033** corresponding to btrees that use shared cache. Then the runtime of
87034** this routine is N*N. But as N is rarely more than 1, this should not
87035** be a problem.
87036*/
87037SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe *p){
87038 int i;
87039 sqlite3 *db;
87040 Db *aDb;
87041 int nDb;
87042 if( DbMaskAllZero(p->lockMask)((p->lockMask)==0) ) return; /* The common case */
87043 db = p->db;
87044 aDb = db->aDb;
87045 nDb = db->nDb;
87046 for(i=0; i<nDb; i++){
87047 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
87048 sqlite3BtreeEnter(aDb[i].pBt);
87049 }
87050 }
87051}
87052#endif
87053
87054#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE2>0
87055/*
87056** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
87057*/
87058static SQLITE_NOINLINE__attribute__((noinline)) void vdbeLeave(Vdbe *p){
87059 int i;
87060 sqlite3 *db;
87061 Db *aDb;
87062 int nDb;
87063 db = p->db;
87064 aDb = db->aDb;
87065 nDb = db->nDb;
87066 for(i=0; i<nDb; i++){
87067 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
87068 sqlite3BtreeLeave(aDb[i].pBt);
87069 }
87070 }
87071}
87072SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe *p){
87073 if( DbMaskAllZero(p->lockMask)((p->lockMask)==0) ) return; /* The common case */
87074 vdbeLeave(p);
87075}
87076#endif
87077
87078#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
87079/*
87080** Print a single opcode. This routine is used for debugging only.
87081*/
87082SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
87083 char *zP4;
87084 char *zCom;
87085 sqlite3 dummyDb;
87086 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
87087 if( pOut==0 ) pOut = stdoutstdout;
87088 sqlite3BeginBenignMalloc();
87089 dummyDb.mallocFailed = 1;
87090 zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
87091#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
87092 zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
87093#else
87094 zCom = 0;
87095#endif
87096 /* NB: The sqlite3OpcodeName() function is implemented by code created
87097 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
87098 ** information from the vdbe.c source text */
87099 fprintf(pOut, zFormat1, pc,
87100 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
87101 zP4 ? zP4 : "", pOp->p5,
87102 zCom ? zCom : ""
87103 );
87104 fflush(pOut);
87105 sqlite3_free(zP4);
87106 sqlite3_free(zCom);
87107 sqlite3EndBenignMalloc();
87108}
87109#endif
87110
87111/*
87112** Initialize an array of N Mem element.
87113**
87114** This is a high-runner, so only those fields that really do need to
87115** be initialized are set. The Mem structure is organized so that
87116** the fields that get initialized are nearby and hopefully on the same
87117** cache line.
87118**
87119** Mem.flags = flags
87120** Mem.db = db
87121** Mem.szMalloc = 0
87122**
87123** All other fields of Mem can safely remain uninitialized for now. They
87124** will be initialized before use.
87125*/
87126static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
87127 if( N>0 ){
87128 do{
87129 p->flags = flags;
87130 p->db = db;
87131 p->szMalloc = 0;
87132#ifdef SQLITE_DEBUG
87133 p->pScopyFrom = 0;
87134#endif
87135 p++;
87136 }while( (--N)>0 );
87137 }
87138}
87139
87140/*
87141** Release auxiliary memory held in an array of N Mem elements.
87142**
87143** After this routine returns, all Mem elements in the array will still
87144** be valid. Those Mem elements that were not holding auxiliary resources
87145** will be unchanged. Mem elements which had something freed will be
87146** set to MEM_Undefined.
87147*/
87148static void releaseMemArray(Mem *p, int N){
87149 if( p && N ){
87150 Mem *pEnd = &p[N];
87151 sqlite3 *db = p->db;
87152 if( db->pnBytesFreed ){
87153 do{
87154 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
87155 }while( (++p)<pEnd );
87156 return;
87157 }
87158 do{
87159 assert( (&p[1])==pEnd || p[0].db==p[1].db )((void) (0));
87160 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
87161
87162 /* This block is really an inlined version of sqlite3VdbeMemRelease()
87163 ** that takes advantage of the fact that the memory cell value is
87164 ** being set to NULL after releasing any dynamic resources.
87165 **
87166 ** The justification for duplicating code is that according to
87167 ** callgrind, this causes a certain test case to hit the CPU 4.7
87168 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
87169 ** sqlite3MemRelease() were called from here. With -O2, this jumps
87170 ** to 6.6 percent. The test case is inserting 1000 rows into a table
87171 ** with no indexes using a single prepared INSERT statement, bind()
87172 ** and reset(). Inserts are grouped into a transaction.
87173 */
87174 testcase( p->flags & MEM_Agg );
87175 testcase( p->flags & MEM_Dyn );
87176 if( p->flags&(MEM_Agg0x8000|MEM_Dyn0x1000) ){
87177 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
87178 sqlite3VdbeMemRelease(p);
87179 p->flags = MEM_Undefined0x0000;
87180 }else if( p->szMalloc ){
87181 sqlite3DbNNFreeNN(db, p->zMalloc);
87182 p->szMalloc = 0;
87183 p->flags = MEM_Undefined0x0000;
87184 }
87185#ifdef SQLITE_DEBUG
87186 else{
87187 p->flags = MEM_Undefined0x0000;
87188 }
87189#endif
87190 }while( (++p)<pEnd );
87191 }
87192}
87193
87194#ifdef SQLITE_DEBUG
87195/*
87196** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
87197** and false if something is wrong.
87198**
87199** This routine is intended for use inside of assert() statements only.
87200*/
87201SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
87202 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC0x879fb71e ) return 0;
87203 return 1;
87204}
87205#endif
87206
87207
87208/*
87209** This is a destructor on a Mem object (which is really an sqlite3_value)
87210** that deletes the Frame object that is attached to it as a blob.
87211**
87212** This routine does not delete the Frame right away. It merely adds the
87213** frame to a list of frames to be deleted when the Vdbe halts.
87214*/
87215SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void *pArg){
87216 VdbeFrame *pFrame = (VdbeFrame*)pArg;
87217 assert( sqlite3VdbeFrameIsValid(pFrame) )((void) (0));
87218 pFrame->pParent = pFrame->v->pDelFrame;
87219 pFrame->v->pDelFrame = pFrame;
87220}
87221
87222#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
87223/*
87224** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
87225** QUERY PLAN output.
87226**
87227** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no
87228** more opcodes to be displayed.
87229*/
87230SQLITE_PRIVATEstatic int sqlite3VdbeNextOpcode(
87231 Vdbe *p, /* The statement being explained */
87232 Mem *pSub, /* Storage for keeping track of subprogram nesting */
87233 int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
87234 int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
87235 int *piAddr, /* OUT: Write index into (*paOp)[] here */
87236 Op **paOp /* OUT: Write the opcode array here */
87237){
87238 int nRow; /* Stop when row count reaches this */
87239 int nSub = 0; /* Number of sub-vdbes seen so far */
87240 SubProgram **apSub = 0; /* Array of sub-vdbes */
87241 int i; /* Next instruction address */
87242 int rc = SQLITE_OK0; /* Result code */
87243 Op *aOp = 0; /* Opcode array */
87244 int iPc; /* Rowid. Copy of value in *piPc */
87245
87246 /* When the number of output rows reaches nRow, that means the
87247 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
87248 ** nRow is the sum of the number of rows in the main program, plus
87249 ** the sum of the number of rows in all trigger subprograms encountered
87250 ** so far. The nRow value will increase as new trigger subprograms are
87251 ** encountered, but p->pc will eventually catch up to nRow.
87252 */
87253 nRow = p->nOp;
87254 if( pSub!=0 ){
87255 if( pSub->flags&MEM_Blob0x0010 ){
87256 /* pSub is initiallly NULL. It is initialized to a BLOB by
87257 ** the P4_SUBPROGRAM processing logic below */
87258 nSub = pSub->n/sizeof(Vdbe*);
87259 apSub = (SubProgram **)pSub->z;
87260 }
87261 for(i=0; i<nSub; i++){
87262 nRow += apSub[i]->nOp;
87263 }
87264 }
87265 iPc = *piPc;
87266 while(1){ /* Loop exits via break */
87267 i = iPc++;
87268 if( i>=nRow ){
87269 p->rc = SQLITE_OK0;
87270 rc = SQLITE_DONE101;
87271 break;
87272 }
87273 if( i<p->nOp ){
87274 /* The rowid is small enough that we are still in the
87275 ** main program. */
87276 aOp = p->aOp;
87277 }else{
87278 /* We are currently listing subprograms. Figure out which one and
87279 ** pick up the appropriate opcode. */
87280 int j;
87281 i -= p->nOp;
87282 assert( apSub!=0 )((void) (0));
87283 assert( nSub>0 )((void) (0));
87284 for(j=0; i>=apSub[j]->nOp; j++){
87285 i -= apSub[j]->nOp;
87286 assert( i<apSub[j]->nOp || j+1<nSub )((void) (0));
87287 }
87288 aOp = apSub[j]->aOp;
87289 }
87290
87291 /* When an OP_Program opcode is encounter (the only opcode that has
87292 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
87293 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
87294 ** has not already been seen.
87295 */
87296 if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM(-4) ){
87297 int nByte = (nSub+1)*sizeof(SubProgram*);
87298 int j;
87299 for(j=0; j<nSub; j++){
87300 if( apSub[j]==aOp[i].p4.pProgram ) break;
87301 }
87302 if( j==nSub ){
87303 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
87304 if( p->rc!=SQLITE_OK0 ){
87305 rc = SQLITE_ERROR1;
87306 break;
87307 }
87308 apSub = (SubProgram **)pSub->z;
87309 apSub[nSub++] = aOp[i].p4.pProgram;
87310 MemSetTypeFlag(pSub, MEM_Blob)((pSub)->flags = ((pSub)->flags&~(0x0dbf|0x0400))|0x0010
)
;
87311 pSub->n = nSub*sizeof(SubProgram*);
87312 nRow += aOp[i].p4.pProgram->nOp;
87313 }
87314 }
87315 if( eMode==0 ) break;
87316#ifdef SQLITE_ENABLE_BYTECODE_VTAB
87317 if( eMode==2 ){
87318 Op *pOp = aOp + i;
87319 if( pOp->opcode==OP_OpenRead102 ) break;
87320 if( pOp->opcode==OP_OpenWrite113 && (pOp->p5 & OPFLAG_P2ISREG0x10)==0 ) break;
87321 if( pOp->opcode==OP_ReopenIdx101 ) break;
87322 }else
87323#endif
87324 {
87325 assert( eMode==1 )((void) (0));
87326 if( aOp[i].opcode==OP_Explain188 ) break;
87327 if( aOp[i].opcode==OP_Init8 && iPc>1 ) break;
87328 }
87329 }
87330 *piPc = iPc;
87331 *piAddr = i;
87332 *paOp = aOp;
87333 return rc;
87334}
87335#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */
87336
87337
87338/*
87339** Delete a VdbeFrame object and its contents. VdbeFrame objects are
87340** allocated by the OP_Program opcode in sqlite3VdbeExec().
87341*/
87342SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame *p){
87343 int i;
87344 Mem *aMem = VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]);
87345 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
87346 assert( sqlite3VdbeFrameIsValid(p) )((void) (0));
87347 for(i=0; i<p->nChildCsr; i++){
87348 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
87349 }
87350 releaseMemArray(aMem, p->nChildMem);
87351 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
87352 sqlite3DbFree(p->v->db, p);
87353}
87354
87355#ifndef SQLITE_OMIT_EXPLAIN
87356/*
87357** Give a listing of the program in the virtual machine.
87358**
87359** The interface is the same as sqlite3VdbeExec(). But instead of
87360** running the code, it invokes the callback once for each instruction.
87361** This feature is used to implement "EXPLAIN".
87362**
87363** When p->explain==1, each instruction is listed. When
87364** p->explain==2, only OP_Explain instructions are listed and these
87365** are shown in a different format. p->explain==2 is used to implement
87366** EXPLAIN QUERY PLAN.
87367** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
87368** are also shown, so that the boundaries between the main program and
87369** each trigger are clear.
87370**
87371** When p->explain==1, first the main program is listed, then each of
87372** the trigger subprograms are listed one by one.
87373*/
87374SQLITE_PRIVATEstatic int sqlite3VdbeList(
87375 Vdbe *p /* The VDBE */
87376){
87377 Mem *pSub = 0; /* Memory cell hold array of subprogs */
87378 sqlite3 *db = p->db; /* The database connection */
87379 int i; /* Loop counter */
87380 int rc = SQLITE_OK0; /* Return code */
87381 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
87382 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP0x01000000)!=0);
87383 Op *aOp; /* Array of opcodes */
87384 Op *pOp; /* Current opcode */
87385
87386 assert( p->explain )((void) (0));
87387 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0));
87388 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM )((void) (0));
87389
87390 /* Even though this opcode does not use dynamic strings for
87391 ** the result, result columns may become dynamic if the user calls
87392 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
87393 */
87394 releaseMemArray(pMem, 8);
87395
87396 if( p->rc==SQLITE_NOMEM7 ){
87397 /* This happens if a malloc() inside a call to sqlite3_column_text() or
87398 ** sqlite3_column_text16() failed. */
87399 sqlite3OomFault(db);
87400 return SQLITE_ERROR1;
87401 }
87402
87403 if( bListSubprogs ){
87404 /* The first 8 memory cells are used for the result set. So we will
87405 ** commandeer the 9th cell to use as storage for an array of pointers
87406 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
87407 ** cells. */
87408 assert( p->nMem>9 )((void) (0));
87409 pSub = &p->aMem[9];
87410 }else{
87411 pSub = 0;
87412 }
87413
87414 /* Figure out which opcode is next to display */
87415 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
87416
87417 if( rc==SQLITE_OK0 ){
87418 pOp = aOp + i;
87419 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
87420 p->rc = SQLITE_INTERRUPT9;
87421 rc = SQLITE_ERROR1;
87422 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
87423 }else{
87424 char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
87425 if( p->explain==2 ){
87426 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
87427 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
87428 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
87429 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF81, sqlite3_free);
87430 assert( p->nResColumn==4 )((void) (0));
87431 }else{
87432 sqlite3VdbeMemSetInt64(pMem+0, i);
87433 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
87434 -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
87435 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
87436 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
87437 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
87438 /* pMem+5 for p4 is done last */
87439 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
87440#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
87441 {
87442 char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
87443 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF81, sqlite3_free);
87444 }
87445#else
87446 sqlite3VdbeMemSetNull(pMem+7);
87447#endif
87448 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF81, sqlite3_free);
87449 assert( p->nResColumn==8 )((void) (0));
87450 }
87451 p->pResultRow = pMem;
87452 if( db->mallocFailed ){
87453 p->rc = SQLITE_NOMEM7;
87454 rc = SQLITE_ERROR1;
87455 }else{
87456 p->rc = SQLITE_OK0;
87457 rc = SQLITE_ROW100;
87458 }
87459 }
87460 }
87461 return rc;
87462}
87463#endif /* SQLITE_OMIT_EXPLAIN */
87464
87465#ifdef SQLITE_DEBUG
87466/*
87467** Print the SQL that was used to generate a VDBE program.
87468*/
87469SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe *p){
87470 const char *z = 0;
87471 if( p->zSql ){
87472 z = p->zSql;
87473 }else if( p->nOp>=1 ){
87474 const VdbeOp *pOp = &p->aOp[0];
87475 if( pOp->opcode==OP_Init8 && pOp->p4.z!=0 ){
87476 z = pOp->p4.z;
87477 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
87478 }
87479 }
87480 if( z ) printf("SQL: [%s]\n", z);
87481}
87482#endif
87483
87484#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
87485/*
87486** Print an IOTRACE message showing SQL content.
87487*/
87488SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe *p){
87489 int nOp = p->nOp;
87490 VdbeOp *pOp;
87491 if( sqlite3IoTrace==0 ) return;
87492 if( nOp<1 ) return;
87493 pOp = &p->aOp[0];
87494 if( pOp->opcode==OP_Init8 && pOp->p4.z!=0 ){
87495 int i, j;
87496 char z[1000];
87497 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
87498 for(i=0; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
87499 for(j=0; z[i]; i++){
87500 if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ){
87501 if( z[i-1]!=' ' ){
87502 z[j++] = ' ';
87503 }
87504 }else{
87505 z[j++] = z[i];
87506 }
87507 }
87508 z[j] = 0;
87509 sqlite3IoTrace("SQL %s\n", z);
87510 }
87511}
87512#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
87513
87514/* An instance of this object describes bulk memory available for use
87515** by subcomponents of a prepared statement. Space is allocated out
87516** of a ReusableSpace object by the allocSpace() routine below.
87517*/
87518struct ReusableSpace {
87519 u8 *pSpace; /* Available memory */
87520 sqlite3_int64 nFree; /* Bytes of available memory */
87521 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
87522};
87523
87524/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
87525** from the ReusableSpace object. Return a pointer to the allocated
87526** memory on success. If insufficient memory is available in the
87527** ReusableSpace object, increase the ReusableSpace.nNeeded
87528** value by the amount needed and return NULL.
87529**
87530** If pBuf is not initially NULL, that means that the memory has already
87531** been allocated by a prior call to this routine, so just return a copy
87532** of pBuf and leave ReusableSpace unchanged.
87533**
87534** This allocator is employed to repurpose unused slots at the end of the
87535** opcode array of prepared state for other memory needs of the prepared
87536** statement.
87537*/
87538static void *allocSpace(
87539 struct ReusableSpace *p, /* Bulk memory available for allocation */
87540 void *pBuf, /* Pointer to a prior allocation */
87541 sqlite3_int64 nByte /* Bytes of memory needed. */
87542){
87543 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) )((void) (0));
87544 if( pBuf==0 ){
87545 nByte = ROUND8P(nByte)(nByte);
87546 if( nByte <= p->nFree ){
87547 p->nFree -= nByte;
87548 pBuf = &p->pSpace[p->nFree];
87549 }else{
87550 p->nNeeded += nByte;
87551 }
87552 }
87553 assert( EIGHT_BYTE_ALIGNMENT(pBuf) )((void) (0));
87554 return pBuf;
87555}
87556
87557/*
87558** Rewind the VDBE back to the beginning in preparation for
87559** running it.
87560*/
87561SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe *p){
87562#if defined(SQLITE_DEBUG)
87563 int i;
87564#endif
87565 assert( p!=0 )((void) (0));
87566 assert( p->eVdbeState==VDBE_INIT_STATE((void) (0))
87567 || p->eVdbeState==VDBE_READY_STATE((void) (0))
87568 || p->eVdbeState==VDBE_HALT_STATE )((void) (0));
87569
87570 /* There should be at least one opcode.
87571 */
87572 assert( p->nOp>0 )((void) (0));
87573
87574 p->eVdbeState = VDBE_READY_STATE1;
87575
87576#ifdef SQLITE_DEBUG
87577 for(i=0; i<p->nMem; i++){
87578 assert( p->aMem[i].db==p->db )((void) (0));
87579 }
87580#endif
87581 p->pc = -1;
87582 p->rc = SQLITE_OK0;
87583 p->errorAction = OE_Abort2;
87584 p->nChange = 0;
87585 p->cacheCtr = 1;
87586 p->minWriteFileFormat = 255;
87587 p->iStatement = 0;
87588 p->nFkConstraint = 0;
87589#ifdef VDBE_PROFILE
87590 for(i=0; i<p->nOp; i++){
87591 p->aOp[i].nExec = 0;
87592 p->aOp[i].nCycle = 0;
87593 }
87594#endif
87595}
87596
87597/*
87598** Prepare a virtual machine for execution for the first time after
87599** creating the virtual machine. This involves things such
87600** as allocating registers and initializing the program counter.
87601** After the VDBE has be prepped, it can be executed by one or more
87602** calls to sqlite3VdbeExec().
87603**
87604** This function may be called exactly once on each virtual machine.
87605** After this routine is called the VM has been "packaged" and is ready
87606** to run. After this routine is called, further calls to
87607** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
87608** the Vdbe from the Parse object that helped generate it so that the
87609** the Vdbe becomes an independent entity and the Parse object can be
87610** destroyed.
87611**
87612** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
87613** to its initial state after it has been run.
87614*/
87615SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(
87616 Vdbe *p, /* The VDBE */
87617 Parse *pParse /* Parsing context */
87618){
87619 sqlite3 *db; /* The database connection */
87620 int nVar; /* Number of parameters */
87621 int nMem; /* Number of VM memory registers */
87622 int nCursor; /* Number of cursors required */
87623 int nArg; /* Number of arguments in subprograms */
87624 int n; /* Loop counter */
87625 struct ReusableSpace x; /* Reusable bulk memory */
87626
87627 assert( p!=0 )((void) (0));
87628 assert( p->nOp>0 )((void) (0));
87629 assert( pParse!=0 )((void) (0));
87630 assert( p->eVdbeState==VDBE_INIT_STATE )((void) (0));
87631 assert( pParse==p->pParse )((void) (0));
87632 p->pVList = pParse->pVList;
87633 pParse->pVList = 0;
87634 db = p->db;
87635 assert( db->mallocFailed==0 )((void) (0));
87636 nVar = pParse->nVar;
87637 nMem = pParse->nMem;
87638 nCursor = pParse->nTab;
87639 nArg = pParse->nMaxArg;
87640
87641 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
87642 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
87643 ** space at the end of aMem[] for cursors 1 and greater.
87644 ** See also: allocateCursor().
87645 */
87646 nMem += nCursor;
87647 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
87648
87649 /* Figure out how much reusable memory is available at the end of the
87650 ** opcode array. This extra memory will be reallocated for other elements
87651 ** of the prepared statement.
87652 */
87653 n = ROUND8P(sizeof(Op)*p->nOp)(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
87654 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
87655 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) )((void) (0));
87656 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n)((pParse->szOpAlloc - n)&~7); /* Bytes of unused memory */
87657 assert( x.nFree>=0 )((void) (0));
87658 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) )((void) (0));
87659
87660 resolveP2Values(p, &nArg);
87661 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
87662 if( pParse->explain ){
87663 if( nMem<10 ) nMem = 10;
87664 p->explain = pParse->explain;
87665 p->nResColumn = 12 - 4*p->explain;
87666 }
87667 p->expired = 0;
87668
87669 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
87670 ** passes. On the first pass, we try to reuse unused memory at the
87671 ** end of the opcode array. If we are unable to satisfy all memory
87672 ** requirements by reusing the opcode array tail, then the second
87673 ** pass will fill in the remainder using a fresh memory allocation.
87674 **
87675 ** This two-pass approach that reuses as much memory as possible from
87676 ** the leftover memory at the end of the opcode array. This can significantly
87677 ** reduce the amount of memory held by a prepared statement.
87678 */
87679 x.nNeeded = 0;
87680 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
87681 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
87682 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
87683 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
87684 if( x.nNeeded ){
87685 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
87686 x.nFree = x.nNeeded;
87687 if( !db->mallocFailed ){
87688 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
87689 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
87690 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
87691 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
87692 }
87693 }
87694
87695 if( db->mallocFailed ){
87696 p->nVar = 0;
87697 p->nCursor = 0;
87698 p->nMem = 0;
87699 }else{
87700 p->nCursor = nCursor;
87701 p->nVar = (ynVar)nVar;
87702 initMemArray(p->aVar, nVar, db, MEM_Null0x0001);
87703 p->nMem = nMem;
87704 initMemArray(p->aMem, nMem, db, MEM_Undefined0x0000);
87705 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
87706 }
87707 sqlite3VdbeRewind(p);
87708}
87709
87710/*
87711** Close a VDBE cursor and release all the resources that cursor
87712** happens to hold.
87713*/
87714SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
87715 if( pCx ) sqlite3VdbeFreeCursorNN(p,pCx);
87716}
87717static SQLITE_NOINLINE__attribute__((noinline)) void freeCursorWithCache(Vdbe *p, VdbeCursor *pCx){
87718 VdbeTxtBlbCache *pCache = pCx->pCache;
87719 assert( pCx->colCache )((void) (0));
87720 pCx->colCache = 0;
87721 pCx->pCache = 0;
87722 if( pCache->pCValue ){
87723 sqlite3RCStrUnref(pCache->pCValue);
87724 pCache->pCValue = 0;
87725 }
87726 sqlite3DbFree(p->db, pCache);
87727 sqlite3VdbeFreeCursorNN(p, pCx);
87728}
87729SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursorNN(Vdbe *p, VdbeCursor *pCx){
87730 if( pCx->colCache ){
87731 freeCursorWithCache(p, pCx);
87732 return;
87733 }
87734 switch( pCx->eCurType ){
87735 case CURTYPE_SORTER1: {
87736 sqlite3VdbeSorterClose(p->db, pCx);
87737 break;
87738 }
87739 case CURTYPE_BTREE0: {
87740 assert( pCx->uc.pCursor!=0 )((void) (0));
87741 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
87742 break;
87743 }
87744#ifndef SQLITE_OMIT_VIRTUALTABLE
87745 case CURTYPE_VTAB2: {
87746 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
87747 const sqlite3_module *pModule = pVCur->pVtab->pModule;
87748 assert( pVCur->pVtab->nRef>0 )((void) (0));
87749 pVCur->pVtab->nRef--;
87750 pModule->xClose(pVCur);
87751 break;
87752 }
87753#endif
87754 }
87755}
87756
87757/*
87758** Close all cursors in the current frame.
87759*/
87760static void closeCursorsInFrame(Vdbe *p){
87761 int i;
87762 for(i=0; i<p->nCursor; i++){
87763 VdbeCursor *pC = p->apCsr[i];
87764 if( pC ){
87765 sqlite3VdbeFreeCursorNN(p, pC);
87766 p->apCsr[i] = 0;
87767 }
87768 }
87769}
87770
87771/*
87772** Copy the values stored in the VdbeFrame structure to its Vdbe. This
87773** is used, for example, when a trigger sub-program is halted to restore
87774** control to the main program.
87775*/
87776SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
87777 Vdbe *v = pFrame->v;
87778 closeCursorsInFrame(v);
87779 v->aOp = pFrame->aOp;
87780 v->nOp = pFrame->nOp;
87781 v->aMem = pFrame->aMem;
87782 v->nMem = pFrame->nMem;
87783 v->apCsr = pFrame->apCsr;
87784 v->nCursor = pFrame->nCursor;
87785 v->db->lastRowid = pFrame->lastRowid;
87786 v->nChange = pFrame->nChange;
87787 v->db->nChange = pFrame->nDbChange;
87788 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
87789 v->pAuxData = pFrame->pAuxData;
87790 pFrame->pAuxData = 0;
87791 return pFrame->pc;
87792}
87793
87794/*
87795** Close all cursors.
87796**
87797** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
87798** cell array. This is necessary as the memory cell array may contain
87799** pointers to VdbeFrame objects, which may in turn contain pointers to
87800** open cursors.
87801*/
87802static void closeAllCursors(Vdbe *p){
87803 if( p->pFrame ){
87804 VdbeFrame *pFrame;
87805 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
87806 sqlite3VdbeFrameRestore(pFrame);
87807 p->pFrame = 0;
87808 p->nFrame = 0;
87809 }
87810 assert( p->nFrame==0 )((void) (0));
87811 closeCursorsInFrame(p);
87812 releaseMemArray(p->aMem, p->nMem);
87813 while( p->pDelFrame ){
87814 VdbeFrame *pDel = p->pDelFrame;
87815 p->pDelFrame = pDel->pParent;
87816 sqlite3VdbeFrameDelete(pDel);
87817 }
87818
87819 /* Delete any auxdata allocations made by the VM */
87820 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
87821 assert( p->pAuxData==0 )((void) (0));
87822}
87823
87824/*
87825** Set the number of result columns that will be returned by this SQL
87826** statement. This is now set at compile time, rather than during
87827** execution of the vdbe program so that sqlite3_column_count() can
87828** be called on an SQL statement before sqlite3_step().
87829*/
87830SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
87831 int n;
87832 sqlite3 *db = p->db;
87833
87834 if( p->nResAlloc ){
87835 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N5);
87836 sqlite3DbFree(db, p->aColName);
87837 }
87838 n = nResColumn*COLNAME_N5;
87839 p->nResColumn = p->nResAlloc = (u16)nResColumn;
87840 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
87841 if( p->aColName==0 ) return;
87842 initMemArray(p->aColName, n, db, MEM_Null0x0001);
87843}
87844
87845/*
87846** Set the name of the idx'th column to be returned by the SQL statement.
87847** zName must be a pointer to a nul terminated string.
87848**
87849** This call must be made after a call to sqlite3VdbeSetNumCols().
87850**
87851** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
87852** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
87853** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
87854*/
87855SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(
87856 Vdbe *p, /* Vdbe being configured */
87857 int idx, /* Index of column zName applies to */
87858 int var, /* One of the COLNAME_* constants */
87859 const char *zName, /* Pointer to buffer containing name */
87860 void (*xDel)(void*) /* Memory management strategy for zName */
87861){
87862 int rc;
87863 Mem *pColName;
87864 assert( idx<p->nResAlloc )((void) (0));
87865 assert( var<COLNAME_N )((void) (0));
87866 if( p->db->mallocFailed ){
87867 assert( !zName || xDel!=SQLITE_DYNAMIC )((void) (0));
87868 return SQLITE_NOMEM_BKPT7;
87869 }
87870 assert( p->aColName!=0 )((void) (0));
87871 pColName = &(p->aColName[idx+var*p->nResAlloc]);
87872 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF81, xDel);
87873 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 )((void) (0));
87874 return rc;
87875}
87876
87877/*
87878** A read or write transaction may or may not be active on database handle
87879** db. If a transaction is active, commit it. If there is a
87880** write-transaction spanning more than one database file, this routine
87881** takes care of the super-journal trickery.
87882*/
87883static int vdbeCommit(sqlite3 *db, Vdbe *p){
87884 int i;
87885 int nTrans = 0; /* Number of databases with an active write-transaction
87886 ** that are candidates for a two-phase commit using a
87887 ** super-journal */
87888 int rc = SQLITE_OK0;
87889 int needXcommit = 0;
87890
87891#ifdef SQLITE_OMIT_VIRTUALTABLE
87892 /* With this option, sqlite3VtabSync() is defined to be simply
87893 ** SQLITE_OK so p is not used.
87894 */
87895 UNUSED_PARAMETER(p)(void)(p);
87896#endif
87897
87898 /* Before doing anything else, call the xSync() callback for any
87899 ** virtual module tables written in this transaction. This has to
87900 ** be done before determining whether a super-journal file is
87901 ** required, as an xSync() callback may add an attached database
87902 ** to the transaction.
87903 */
87904 rc = sqlite3VtabSync(db, p);
87905
87906 /* This loop determines (a) if the commit hook should be invoked and
87907 ** (b) how many database files have open write transactions, not
87908 ** including the temp database. (b) is important because if more than
87909 ** one database file has an open write transaction, a super-journal
87910 ** file is required for an atomic commit.
87911 */
87912 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
87913 Btree *pBt = db->aDb[i].pBt;
87914 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
87915 /* Whether or not a database might need a super-journal depends upon
87916 ** its journal mode (among other things). This matrix determines which
87917 ** journal modes use a super-journal and which do not */
87918 static const u8 aMJNeeded[] = {
87919 /* DELETE */ 1,
87920 /* PERSIST */ 1,
87921 /* OFF */ 0,
87922 /* TRUNCATE */ 1,
87923 /* MEMORY */ 0,
87924 /* WAL */ 0
87925 };
87926 Pager *pPager; /* Pager associated with pBt */
87927 needXcommit = 1;
87928 sqlite3BtreeEnter(pBt);
87929 pPager = sqlite3BtreePager(pBt);
87930 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF0x01
87931 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
87932 && sqlite3PagerIsMemdb(pPager)==0
87933 ){
87934 assert( i!=1 )((void) (0));
87935 nTrans++;
87936 }
87937 rc = sqlite3PagerExclusiveLock(pPager);
87938 sqlite3BtreeLeave(pBt);
87939 }
87940 }
87941 if( rc!=SQLITE_OK0 ){
87942 return rc;
87943 }
87944
87945 /* If there are any write-transactions at all, invoke the commit hook */
87946 if( needXcommit && db->xCommitCallback ){
87947 rc = db->xCommitCallback(db->pCommitArg);
87948 if( rc ){
87949 return SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8));
87950 }
87951 }
87952
87953 /* The simple case - no more than one database file (not counting the
87954 ** TEMP database) has a transaction active. There is no need for the
87955 ** super-journal.
87956 **
87957 ** If the return value of sqlite3BtreeGetFilename() is a zero length
87958 ** string, it means the main database is :memory: or a temp file. In
87959 ** that case we do not support atomic multi-file commits, so use the
87960 ** simple case then too.
87961 */
87962 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
87963 || nTrans<=1
87964 ){
87965 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
87966 Btree *pBt = db->aDb[i].pBt;
87967 if( pBt ){
87968 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
87969 }
87970 }
87971
87972 /* Do the commit only if all databases successfully complete phase 1.
87973 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
87974 ** IO error while deleting or truncating a journal file. It is unlikely,
87975 ** but could happen. In this case abandon processing and return the error.
87976 */
87977 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
87978 Btree *pBt = db->aDb[i].pBt;
87979 if( pBt ){
87980 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
87981 }
87982 }
87983 if( rc==SQLITE_OK0 ){
87984 sqlite3VtabCommit(db);
87985 }
87986 }
87987
87988 /* The complex case - There is a multi-file write-transaction active.
87989 ** This requires a super-journal file to ensure the transaction is
87990 ** committed atomically.
87991 */
87992#ifndef SQLITE_OMIT_DISKIO
87993 else{
87994 sqlite3_vfs *pVfs = db->pVfs;
87995 char *zSuper = 0; /* File-name for the super-journal */
87996 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
87997 sqlite3_file *pSuperJrnl = 0;
87998 i64 offset = 0;
87999 int res;
88000 int retryCount = 0;
88001 int nMainFile;
88002
88003 /* Select a super-journal file name */
88004 nMainFile = sqlite3Strlen30(zMainFile);
88005 zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
88006 if( zSuper==0 ) return SQLITE_NOMEM_BKPT7;
88007 zSuper += 4;
88008 do {
88009 u32 iRandom;
88010 if( retryCount ){
88011 if( retryCount>100 ){
88012 sqlite3_log(SQLITE_FULL13, "MJ delete: %s", zSuper);
88013 sqlite3OsDelete(pVfs, zSuper, 0);
88014 break;
88015 }else if( retryCount==1 ){
88016 sqlite3_log(SQLITE_FULL13, "MJ collide: %s", zSuper);
88017 }
88018 }
88019 retryCount++;
88020 sqlite3_randomness(sizeof(iRandom), &iRandom);
88021 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
88022 (iRandom>>8)&0xffffff, iRandom&0xff);
88023 /* The antipenultimate character of the super-journal name must
88024 ** be "9" to avoid name collisions when using 8+3 filenames. */
88025 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' )((void) (0));
88026 sqlite3FileSuffix3(zMainFile, zSuper);
88027 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS0, &res);
88028 }while( rc==SQLITE_OK0 && res );
88029 if( rc==SQLITE_OK0 ){
88030 /* Open the super-journal. */
88031 rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl,
88032 SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|
88033 SQLITE_OPEN_EXCLUSIVE0x00000010|SQLITE_OPEN_SUPER_JOURNAL0x00004000, 0
88034 );
88035 }
88036 if( rc!=SQLITE_OK0 ){
88037 sqlite3DbFree(db, zSuper-4);
88038 return rc;
88039 }
88040
88041 /* Write the name of each database file in the transaction into the new
88042 ** super-journal file. If an error occurs at this point close
88043 ** and delete the super-journal file. All the individual journal files
88044 ** still have 'null' as the super-journal pointer, so they will roll
88045 ** back independently if a failure occurs.
88046 */
88047 for(i=0; i<db->nDb; i++){
88048 Btree *pBt = db->aDb[i].pBt;
88049 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
88050 char const *zFile = sqlite3BtreeGetJournalname(pBt);
88051 if( zFile==0 ){
88052 continue; /* Ignore TEMP and :memory: databases */
88053 }
88054 assert( zFile[0]!=0 )((void) (0));
88055 rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset);
88056 offset += sqlite3Strlen30(zFile)+1;
88057 if( rc!=SQLITE_OK0 ){
88058 sqlite3OsCloseFree(pSuperJrnl);
88059 sqlite3OsDelete(pVfs, zSuper, 0);
88060 sqlite3DbFree(db, zSuper-4);
88061 return rc;
88062 }
88063 }
88064 }
88065
88066 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
88067 ** flag is set this is not required.
88068 */
88069 if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL0x00000400)
88070 && SQLITE_OK0!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL0x00002))
88071 ){
88072 sqlite3OsCloseFree(pSuperJrnl);
88073 sqlite3OsDelete(pVfs, zSuper, 0);
88074 sqlite3DbFree(db, zSuper-4);
88075 return rc;
88076 }
88077
88078 /* Sync all the db files involved in the transaction. The same call
88079 ** sets the super-journal pointer in each individual journal. If
88080 ** an error occurs here, do not delete the super-journal file.
88081 **
88082 ** If the error occurs during the first call to
88083 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
88084 ** super-journal file will be orphaned. But we cannot delete it,
88085 ** in case the super-journal file name was written into the journal
88086 ** file before the failure occurred.
88087 */
88088 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
88089 Btree *pBt = db->aDb[i].pBt;
88090 if( pBt ){
88091 rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper);
88092 }
88093 }
88094 sqlite3OsCloseFree(pSuperJrnl);
88095 assert( rc!=SQLITE_BUSY )((void) (0));
88096 if( rc!=SQLITE_OK0 ){
88097 sqlite3DbFree(db, zSuper-4);
88098 return rc;
88099 }
88100
88101 /* Delete the super-journal file. This commits the transaction. After
88102 ** doing this the directory is synced again before any individual
88103 ** transaction files are deleted.
88104 */
88105 rc = sqlite3OsDelete(pVfs, zSuper, 1);
88106 sqlite3DbFree(db, zSuper-4);
88107 zSuper = 0;
88108 if( rc ){
88109 return rc;
88110 }
88111
88112 /* All files and directories have already been synced, so the following
88113 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
88114 ** deleting or truncating journals. If something goes wrong while
88115 ** this is happening we don't really care. The integrity of the
88116 ** transaction is already guaranteed, but some stray 'cold' journals
88117 ** may be lying around. Returning an error code won't help matters.
88118 */
88119 disable_simulated_io_errors();
88120 sqlite3BeginBenignMalloc();
88121 for(i=0; i<db->nDb; i++){
88122 Btree *pBt = db->aDb[i].pBt;
88123 if( pBt ){
88124 sqlite3BtreeCommitPhaseTwo(pBt, 1);
88125 }
88126 }
88127 sqlite3EndBenignMalloc();
88128 enable_simulated_io_errors();
88129
88130 sqlite3VtabCommit(db);
88131 }
88132#endif
88133
88134 return rc;
88135}
88136
88137/*
88138** This routine checks that the sqlite3.nVdbeActive count variable
88139** matches the number of vdbe's in the list sqlite3.pVdbe that are
88140** currently active. An assertion fails if the two counts do not match.
88141** This is an internal self-check only - it is not an essential processing
88142** step.
88143**
88144** This is a no-op if NDEBUG is defined.
88145*/
88146#ifndef NDEBUG1
88147static void checkActiveVdbeCnt(sqlite3 *db){
88148 Vdbe *p;
88149 int cnt = 0;
88150 int nWrite = 0;
88151 int nRead = 0;
88152 p = db->pVdbe;
88153 while( p ){
88154 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
88155 cnt++;
88156 if( p->readOnly==0 ) nWrite++;
88157 if( p->bIsReader ) nRead++;
88158 }
88159 p = p->pVNext;
88160 }
88161 assert( cnt==db->nVdbeActive )((void) (0));
88162 assert( nWrite==db->nVdbeWrite )((void) (0));
88163 assert( nRead==db->nVdbeRead )((void) (0));
88164}
88165#else
88166#define checkActiveVdbeCnt(x)
88167#endif
88168
88169/*
88170** If the Vdbe passed as the first argument opened a statement-transaction,
88171** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
88172** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
88173** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
88174** statement transaction is committed.
88175**
88176** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
88177** Otherwise SQLITE_OK.
88178*/
88179static SQLITE_NOINLINE__attribute__((noinline)) int vdbeCloseStatement(Vdbe *p, int eOp){
88180 sqlite3 *const db = p->db;
88181 int rc = SQLITE_OK0;
88182 int i;
88183 const int iSavepoint = p->iStatement-1;
88184
88185 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE)((void) (0));
88186 assert( db->nStatement>0 )((void) (0));
88187 assert( p->iStatement==(db->nStatement+db->nSavepoint) )((void) (0));
88188
88189 for(i=0; i<db->nDb; i++){
88190 int rc2 = SQLITE_OK0;
88191 Btree *pBt = db->aDb[i].pBt;
88192 if( pBt ){
88193 if( eOp==SAVEPOINT_ROLLBACK2 ){
88194 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK2, iSavepoint);
88195 }
88196 if( rc2==SQLITE_OK0 ){
88197 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE1, iSavepoint);
88198 }
88199 if( rc==SQLITE_OK0 ){
88200 rc = rc2;
88201 }
88202 }
88203 }
88204 db->nStatement--;
88205 p->iStatement = 0;
88206
88207 if( rc==SQLITE_OK0 ){
88208 if( eOp==SAVEPOINT_ROLLBACK2 ){
88209 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK2, iSavepoint);
88210 }
88211 if( rc==SQLITE_OK0 ){
88212 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE1, iSavepoint);
88213 }
88214 }
88215
88216 /* If the statement transaction is being rolled back, also restore the
88217 ** database handles deferred constraint counter to the value it had when
88218 ** the statement transaction was opened. */
88219 if( eOp==SAVEPOINT_ROLLBACK2 ){
88220 db->nDeferredCons = p->nStmtDefCons;
88221 db->nDeferredImmCons = p->nStmtDefImmCons;
88222 }
88223 return rc;
88224}
88225SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
88226 if( p->db->nStatement && p->iStatement ){
88227 return vdbeCloseStatement(p, eOp);
88228 }
88229 return SQLITE_OK0;
88230}
88231
88232
88233/*
88234** This function is called when a transaction opened by the database
88235** handle associated with the VM passed as an argument is about to be
88236** committed. If there are outstanding deferred foreign key constraint
88237** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
88238**
88239** If there are outstanding FK violations and this function returns
88240** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
88241** and write an error message to it. Then return SQLITE_ERROR.
88242*/
88243#ifndef SQLITE_OMIT_FOREIGN_KEY
88244SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
88245 sqlite3 *db = p->db;
88246 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
88247 || (!deferred && p->nFkConstraint>0)
88248 ){
88249 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
88250 p->errorAction = OE_Abort2;
88251 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
88252 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ) return SQLITE_ERROR1;
88253 return SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
88254 }
88255 return SQLITE_OK0;
88256}
88257#endif
88258
88259/*
88260** This routine is called the when a VDBE tries to halt. If the VDBE
88261** has made changes and is in autocommit mode, then commit those
88262** changes. If a rollback is needed, then do the rollback.
88263**
88264** This routine is the only way to move the sqlite3eOpenState of a VM from
88265** SQLITE_STATE_RUN to SQLITE_STATE_HALT. It is harmless to
88266** call this on a VM that is in the SQLITE_STATE_HALT state.
88267**
88268** Return an error code. If the commit could not complete because of
88269** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
88270** means the close did not happen and needs to be repeated.
88271*/
88272SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe *p){
88273 int rc; /* Used to store transient return codes */
88274 sqlite3 *db = p->db;
88275
88276 /* This function contains the logic that determines if a statement or
88277 ** transaction will be committed or rolled back as a result of the
88278 ** execution of this virtual machine.
88279 **
88280 ** If any of the following errors occur:
88281 **
88282 ** SQLITE_NOMEM
88283 ** SQLITE_IOERR
88284 ** SQLITE_FULL
88285 ** SQLITE_INTERRUPT
88286 **
88287 ** Then the internal cache might have been left in an inconsistent
88288 ** state. We need to rollback the statement transaction, if there is
88289 ** one, or the complete transaction if there is no statement transaction.
88290 */
88291
88292 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0));
88293 if( db->mallocFailed ){
88294 p->rc = SQLITE_NOMEM_BKPT7;
88295 }
88296 closeAllCursors(p);
88297 checkActiveVdbeCnt(db);
88298
88299 /* No commit or rollback needed if the program never started or if the
88300 ** SQL statement does not read or write a database file. */
88301 if( p->bIsReader ){
88302 int mrc; /* Primary error code from p->rc */
88303 int eStatementOp = 0;
88304 int isSpecialError; /* Set to true if a 'special' error */
88305
88306 /* Lock all btrees used by the statement */
88307 sqlite3VdbeEnter(p);
88308
88309 /* Check for one of the special errors */
88310 if( p->rc ){
88311 mrc = p->rc & 0xff;
88312 isSpecialError = mrc==SQLITE_NOMEM7
88313 || mrc==SQLITE_IOERR10
88314 || mrc==SQLITE_INTERRUPT9
88315 || mrc==SQLITE_FULL13;
88316 }else{
88317 mrc = isSpecialError = 0;
88318 }
88319 if( isSpecialError ){
88320 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
88321 ** no rollback is necessary. Otherwise, at least a savepoint
88322 ** transaction must be rolled back to restore the database to a
88323 ** consistent state.
88324 **
88325 ** Even if the statement is read-only, it is important to perform
88326 ** a statement or transaction rollback operation. If the error
88327 ** occurred while writing to the journal, sub-journal or database
88328 ** file as part of an effort to free up cache space (see function
88329 ** pagerStress() in pager.c), the rollback is required to restore
88330 ** the pager to a consistent state.
88331 */
88332 if( !p->readOnly || mrc!=SQLITE_INTERRUPT9 ){
88333 if( (mrc==SQLITE_NOMEM7 || mrc==SQLITE_FULL13) && p->usesStmtJournal ){
88334 eStatementOp = SAVEPOINT_ROLLBACK2;
88335 }else{
88336 /* We are forced to roll back the active transaction. Before doing
88337 ** so, abort any other statements this handle currently has active.
88338 */
88339 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
88340 sqlite3CloseSavepoints(db);
88341 db->autoCommit = 1;
88342 p->nChange = 0;
88343 }
88344 }
88345 }
88346
88347 /* Check for immediate foreign key violations. */
88348 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
88349 (void)sqlite3VdbeCheckFk(p, 0);
88350 }
88351
88352 /* If the auto-commit flag is set and this is the only active writer
88353 ** VM, then we do either a commit or rollback of the current transaction.
88354 **
88355 ** Note: This block also runs if one of the special errors handled
88356 ** above has occurred.
88357 */
88358 if( !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
88359 && db->autoCommit
88360 && db->nVdbeWrite==(p->readOnly==0)
88361 ){
88362 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
88363 rc = sqlite3VdbeCheckFk(p, 1);
88364 if( rc!=SQLITE_OK0 ){
88365 if( NEVER(p->readOnly)(p->readOnly) ){
88366 sqlite3VdbeLeave(p);
88367 return SQLITE_ERROR1;
88368 }
88369 rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
88370 }else if( db->flags & SQLITE_CorruptRdOnly((u64)(0x00002)<<32) ){
88371 rc = SQLITE_CORRUPT11;
88372 db->flags &= ~SQLITE_CorruptRdOnly((u64)(0x00002)<<32);
88373 }else{
88374 /* The auto-commit flag is true, the vdbe program was successful
88375 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
88376 ** key constraints to hold up the transaction. This means a commit
88377 ** is required. */
88378 rc = vdbeCommit(db, p);
88379 }
88380 if( rc==SQLITE_BUSY5 && p->readOnly ){
88381 sqlite3VdbeLeave(p);
88382 return SQLITE_BUSY5;
88383 }else if( rc!=SQLITE_OK0 ){
88384 sqlite3SystemError(db, rc);
88385 p->rc = rc;
88386 sqlite3RollbackAll(db, SQLITE_OK0);
88387 p->nChange = 0;
88388 }else{
88389 db->nDeferredCons = 0;
88390 db->nDeferredImmCons = 0;
88391 db->flags &= ~(u64)SQLITE_DeferFKs0x00080000;
88392 sqlite3CommitInternalChanges(db);
88393 }
88394 }else if( p->rc==SQLITE_SCHEMA17 && db->nVdbeActive>1 ){
88395 p->nChange = 0;
88396 }else{
88397 sqlite3RollbackAll(db, SQLITE_OK0);
88398 p->nChange = 0;
88399 }
88400 db->nStatement = 0;
88401 }else if( eStatementOp==0 ){
88402 if( p->rc==SQLITE_OK0 || p->errorAction==OE_Fail3 ){
88403 eStatementOp = SAVEPOINT_RELEASE1;
88404 }else if( p->errorAction==OE_Abort2 ){
88405 eStatementOp = SAVEPOINT_ROLLBACK2;
88406 }else{
88407 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
88408 sqlite3CloseSavepoints(db);
88409 db->autoCommit = 1;
88410 p->nChange = 0;
88411 }
88412 }
88413
88414 /* If eStatementOp is non-zero, then a statement transaction needs to
88415 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
88416 ** do so. If this operation returns an error, and the current statement
88417 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
88418 ** current statement error code.
88419 */
88420 if( eStatementOp ){
88421 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
88422 if( rc ){
88423 if( p->rc==SQLITE_OK0 || (p->rc&0xff)==SQLITE_CONSTRAINT19 ){
88424 p->rc = rc;
88425 sqlite3DbFree(db, p->zErrMsg);
88426 p->zErrMsg = 0;
88427 }
88428 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
88429 sqlite3CloseSavepoints(db);
88430 db->autoCommit = 1;
88431 p->nChange = 0;
88432 }
88433 }
88434
88435 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
88436 ** has been rolled back, update the database connection change-counter.
88437 */
88438 if( p->changeCntOn ){
88439 if( eStatementOp!=SAVEPOINT_ROLLBACK2 ){
88440 sqlite3VdbeSetChanges(db, p->nChange);
88441 }else{
88442 sqlite3VdbeSetChanges(db, 0);
88443 }
88444 p->nChange = 0;
88445 }
88446
88447 /* Release the locks */
88448 sqlite3VdbeLeave(p);
88449 }
88450
88451 /* We have successfully halted and closed the VM. Record this fact. */
88452 db->nVdbeActive--;
88453 if( !p->readOnly ) db->nVdbeWrite--;
88454 if( p->bIsReader ) db->nVdbeRead--;
88455 assert( db->nVdbeActive>=db->nVdbeRead )((void) (0));
88456 assert( db->nVdbeRead>=db->nVdbeWrite )((void) (0));
88457 assert( db->nVdbeWrite>=0 )((void) (0));
88458 p->eVdbeState = VDBE_HALT_STATE3;
88459 checkActiveVdbeCnt(db);
88460 if( db->mallocFailed ){
88461 p->rc = SQLITE_NOMEM_BKPT7;
88462 }
88463
88464 /* If the auto-commit flag is set to true, then any locks that were held
88465 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
88466 ** to invoke any required unlock-notify callbacks.
88467 */
88468 if( db->autoCommit ){
88469 sqlite3ConnectionUnlocked(db);
88470 }
88471
88472 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 )((void) (0));
88473 return (p->rc==SQLITE_BUSY5 ? SQLITE_BUSY5 : SQLITE_OK0);
88474}
88475
88476
88477/*
88478** Each VDBE holds the result of the most recent sqlite3_step() call
88479** in p->rc. This routine sets that result back to SQLITE_OK.
88480*/
88481SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe *p){
88482 p->rc = SQLITE_OK0;
88483}
88484
88485/*
88486** Copy the error code and error message belonging to the VDBE passed
88487** as the first argument to its database handle (so that they will be
88488** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
88489**
88490** This function does not clear the VDBE error code or message, just
88491** copies them to the database handle.
88492*/
88493SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p){
88494 sqlite3 *db = p->db;
88495 int rc = p->rc;
88496 if( p->zErrMsg ){
88497 db->bBenignMalloc++;
88498 sqlite3BeginBenignMalloc();
88499 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
88500 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
88501 sqlite3EndBenignMalloc();
88502 db->bBenignMalloc--;
88503 }else if( db->pErr ){
88504 sqlite3ValueSetNull(db->pErr);
88505 }
88506 db->errCode = rc;
88507 db->errByteOffset = -1;
88508 return rc;
88509}
88510
88511#ifdef SQLITE_ENABLE_SQLLOG
88512/*
88513** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
88514** invoke it.
88515*/
88516static void vdbeInvokeSqllog(Vdbe *v){
88517 if( sqlite3GlobalConfigsqlite3Config.xSqllog && v->rc==SQLITE_OK0 && v->zSql && v->pc>=0 ){
88518 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
88519 assert( v->db->init.busy==0 )((void) (0));
88520 if( zExpanded ){
88521 sqlite3GlobalConfigsqlite3Config.xSqllog(
88522 sqlite3GlobalConfigsqlite3Config.pSqllogArg, v->db, zExpanded, 1
88523 );
88524 sqlite3DbFree(v->db, zExpanded);
88525 }
88526 }
88527}
88528#else
88529# define vdbeInvokeSqllog(x)
88530#endif
88531
88532/*
88533** Clean up a VDBE after execution but do not delete the VDBE just yet.
88534** Write any error messages into *pzErrMsg. Return the result code.
88535**
88536** After this routine is run, the VDBE should be ready to be executed
88537** again.
88538**
88539** To look at it another way, this routine resets the state of the
88540** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to
88541** VDBE_READY_STATE.
88542*/
88543SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe *p){
88544#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
88545 int i;
88546#endif
88547
88548 sqlite3 *db;
88549 db = p->db;
88550
88551 /* If the VM did not run to completion or if it encountered an
88552 ** error, then it might not have been halted properly. So halt
88553 ** it now.
88554 */
88555 if( p->eVdbeState==VDBE_RUN_STATE2 ) sqlite3VdbeHalt(p);
88556
88557 /* If the VDBE has been run even partially, then transfer the error code
88558 ** and error message from the VDBE into the main database structure. But
88559 ** if the VDBE has just been set to run but has not actually executed any
88560 ** instructions yet, leave the main database error information unchanged.
88561 */
88562 if( p->pc>=0 ){
88563 vdbeInvokeSqllog(p);
88564 if( db->pErr || p->zErrMsg ){
88565 sqlite3VdbeTransferError(p);
88566 }else{
88567 db->errCode = p->rc;
88568 }
88569 }
88570
88571 /* Reset register contents and reclaim error message memory.
88572 */
88573#ifdef SQLITE_DEBUG
88574 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
88575 ** Vdbe.aMem[] arrays have already been cleaned up. */
88576 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 )((void) (0));
88577 if( p->aMem ){
88578 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined )((void) (0));
88579 }
88580#endif
88581 if( p->zErrMsg ){
88582 sqlite3DbFree(db, p->zErrMsg);
88583 p->zErrMsg = 0;
88584 }
88585 p->pResultRow = 0;
88586#ifdef SQLITE_DEBUG
88587 p->nWrite = 0;
88588#endif
88589
88590 /* Save profiling information from this VDBE run.
88591 */
88592#ifdef VDBE_PROFILE
88593 {
88594 FILE *out = fopen("vdbe_profile.out", "a");
88595 if( out ){
88596 fprintf(out, "---- ");
88597 for(i=0; i<p->nOp; i++){
88598 fprintf(out, "%02x", p->aOp[i].opcode);
88599 }
88600 fprintf(out, "\n");
88601 if( p->zSql ){
88602 char c, pc = 0;
88603 fprintf(out, "-- ");
88604 for(i=0; (c = p->zSql[i])!=0; i++){
88605 if( pc=='\n' ) fprintf(out, "-- ");
88606 putc(c, out);
88607 pc = c;
88608 }
88609 if( pc!='\n' ) fprintf(out, "\n");
88610 }
88611 for(i=0; i<p->nOp; i++){
88612 char zHdr[100];
88613 i64 cnt = p->aOp[i].nExec;
88614 i64 cycles = p->aOp[i].nCycle;
88615 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
88616 cnt,
88617 cycles,
88618 cnt>0 ? cycles/cnt : 0
88619 );
88620 fprintf(out, "%s", zHdr);
88621 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
88622 }
88623 fclose(out);
88624 }
88625 }
88626#endif
88627 return p->rc & db->errMask;
88628}
88629
88630/*
88631** Clean up and delete a VDBE after execution. Return an integer which is
88632** the result code. Write any error message text into *pzErrMsg.
88633*/
88634SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe *p){
88635 int rc = SQLITE_OK0;
88636 assert( VDBE_RUN_STATE>VDBE_READY_STATE )((void) (0));
88637 assert( VDBE_HALT_STATE>VDBE_READY_STATE )((void) (0));
88638 assert( VDBE_INIT_STATE<VDBE_READY_STATE )((void) (0));
88639 if( p->eVdbeState>=VDBE_READY_STATE1 ){
88640 rc = sqlite3VdbeReset(p);
88641 assert( (rc & p->db->errMask)==rc )((void) (0));
88642 }
88643 sqlite3VdbeDelete(p);
88644 return rc;
88645}
88646
88647/*
88648** If parameter iOp is less than zero, then invoke the destructor for
88649** all auxiliary data pointers currently cached by the VM passed as
88650** the first argument.
88651**
88652** Or, if iOp is greater than or equal to zero, then the destructor is
88653** only invoked for those auxiliary data pointers created by the user
88654** function invoked by the OP_Function opcode at instruction iOp of
88655** VM pVdbe, and only then if:
88656**
88657** * the associated function parameter is the 32nd or later (counting
88658** from left to right), or
88659**
88660** * the corresponding bit in argument mask is clear (where the first
88661** function parameter corresponds to bit 0 etc.).
88662*/
88663SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
88664 while( *pp ){
88665 AuxData *pAux = *pp;
88666 if( (iOp<0)
88667 || (pAux->iAuxOp==iOp
88668 && pAux->iAuxArg>=0
88669 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg)(((unsigned int)1)<<(pAux->iAuxArg)))))
88670 ){
88671 testcase( pAux->iAuxArg==31 );
88672 if( pAux->xDeleteAux ){
88673 pAux->xDeleteAux(pAux->pAux);
88674 }
88675 *pp = pAux->pNextAux;
88676 sqlite3DbFree(db, pAux);
88677 }else{
88678 pp= &pAux->pNextAux;
88679 }
88680 }
88681}
88682
88683/*
88684** Free all memory associated with the Vdbe passed as the second argument,
88685** except for object itself, which is preserved.
88686**
88687** The difference between this function and sqlite3VdbeDelete() is that
88688** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
88689** the database connection and frees the object itself.
88690*/
88691static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
88692 SubProgram *pSub, *pNext;
88693 assert( db!=0 )((void) (0));
88694 assert( p->db==0 || p->db==db )((void) (0));
88695 if( p->aColName ){
88696 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N5);
88697 sqlite3DbNNFreeNN(db, p->aColName);
88698 }
88699 for(pSub=p->pProgram; pSub; pSub=pNext){
88700 pNext = pSub->pNext;
88701 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
88702 sqlite3DbFree(db, pSub);
88703 }
88704 if( p->eVdbeState!=VDBE_INIT_STATE0 ){
88705 releaseMemArray(p->aVar, p->nVar);
88706 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
88707 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
88708 }
88709 vdbeFreeOpArray(db, p->aOp, p->nOp);
88710 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
88711#ifdef SQLITE_ENABLE_NORMALIZE
88712 sqlite3DbFree(db, p->zNormSql);
88713 {
88714 DblquoteStr *pThis, *pNxt;
88715 for(pThis=p->pDblStr; pThis; pThis=pNxt){
88716 pNxt = pThis->pNextStr;
88717 sqlite3DbFree(db, pThis);
88718 }
88719 }
88720#endif
88721#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
88722 {
88723 int i;
88724 for(i=0; i<p->nScan; i++){
88725 sqlite3DbFree(db, p->aScan[i].zName);
88726 }
88727 sqlite3DbFree(db, p->aScan);
88728 }
88729#endif
88730}
88731
88732/*
88733** Delete an entire VDBE.
88734*/
88735SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe *p){
88736 sqlite3 *db;
88737
88738 assert( p!=0 )((void) (0));
88739 db = p->db;
88740 assert( db!=0 )((void) (0));
88741 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
88742 sqlite3VdbeClearObject(db, p);
88743 if( db->pnBytesFreed==0 ){
88744 assert( p->ppVPrev!=0 )((void) (0));
88745 *p->ppVPrev = p->pVNext;
88746 if( p->pVNext ){
88747 p->pVNext->ppVPrev = p->ppVPrev;
88748 }
88749 }
88750 sqlite3DbNNFreeNN(db, p);
88751}
88752
88753/*
88754** The cursor "p" has a pending seek operation that has not yet been
88755** carried out. Seek the cursor now. If an error occurs, return
88756** the appropriate error code.
88757*/
88758SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeFinishMoveto(VdbeCursor *p){
88759 int res, rc;
88760#ifdef SQLITE_TEST
88761 extern int sqlite3_search_count;
88762#endif
88763 assert( p->deferredMoveto )((void) (0));
88764 assert( p->isTable )((void) (0));
88765 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
88766 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
88767 if( rc ) return rc;
88768 if( res!=0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(88768);
88769#ifdef SQLITE_TEST
88770 sqlite3_search_count++;
88771#endif
88772 p->deferredMoveto = 0;
88773 p->cacheStatus = CACHE_STALE0;
88774 return SQLITE_OK0;
88775}
88776
88777/*
88778** Something has moved cursor "p" out of place. Maybe the row it was
88779** pointed to was deleted out from under it. Or maybe the btree was
88780** rebalanced. Whatever the cause, try to restore "p" to the place it
88781** is supposed to be pointing. If the row was deleted out from under the
88782** cursor, set the cursor to point to a NULL row.
88783*/
88784SQLITE_PRIVATEstatic int SQLITE_NOINLINE__attribute__((noinline)) sqlite3VdbeHandleMovedCursor(VdbeCursor *p){
88785 int isDifferentRow, rc;
88786 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
88787 assert( p->uc.pCursor!=0 )((void) (0));
88788 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) )((void) (0));
88789 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
88790 p->cacheStatus = CACHE_STALE0;
88791 if( isDifferentRow ) p->nullRow = 1;
88792 return rc;
88793}
88794
88795/*
88796** Check to ensure that the cursor is valid. Restore the cursor
88797** if need be. Return any I/O error from the restore operation.
88798*/
88799SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor *p){
88800 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) )((void) (0));
88801 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
88802 return sqlite3VdbeHandleMovedCursor(p);
88803 }
88804 return SQLITE_OK0;
88805}
88806
88807/*
88808** The following functions:
88809**
88810** sqlite3VdbeSerialType()
88811** sqlite3VdbeSerialTypeLen()
88812** sqlite3VdbeSerialLen()
88813** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
88814** sqlite3VdbeSerialGet()
88815**
88816** encapsulate the code that serializes values for storage in SQLite
88817** data and index records. Each serialized value consists of a
88818** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
88819** integer, stored as a varint.
88820**
88821** In an SQLite index record, the serial type is stored directly before
88822** the blob of data that it corresponds to. In a table record, all serial
88823** types are stored at the start of the record, and the blobs of data at
88824** the end. Hence these functions allow the caller to handle the
88825** serial-type and data blob separately.
88826**
88827** The following table describes the various storage classes for data:
88828**
88829** serial type bytes of data type
88830** -------------- --------------- ---------------
88831** 0 0 NULL
88832** 1 1 signed integer
88833** 2 2 signed integer
88834** 3 3 signed integer
88835** 4 4 signed integer
88836** 5 6 signed integer
88837** 6 8 signed integer
88838** 7 8 IEEE float
88839** 8 0 Integer constant 0
88840** 9 0 Integer constant 1
88841** 10,11 reserved for expansion
88842** N>=12 and even (N-12)/2 BLOB
88843** N>=13 and odd (N-13)/2 text
88844**
88845** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
88846** of SQLite will not understand those serial types.
88847*/
88848
88849#if 0 /* Inlined into the OP_MakeRecord opcode */
88850/*
88851** Return the serial-type for the value stored in pMem.
88852**
88853** This routine might convert a large MEM_IntReal value into MEM_Real.
88854**
88855** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
88856** opcode in the byte-code engine. But by moving this routine in-line, we
88857** can omit some redundant tests and make that opcode a lot faster. So
88858** this routine is now only used by the STAT3 logic and STAT3 support has
88859** ended. The code is kept here for historical reference only.
88860*/
88861SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
88862 int flags = pMem->flags;
88863 u32 n;
88864
88865 assert( pLen!=0 )((void) (0));
88866 if( flags&MEM_Null0x0001 ){
88867 *pLen = 0;
88868 return 0;
88869 }
88870 if( flags&(MEM_Int0x0004|MEM_IntReal0x0020) ){
88871 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
88872# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
88873 i64 i = pMem->u.i;
88874 u64 u;
88875 testcase( flags & MEM_Int );
88876 testcase( flags & MEM_IntReal );
88877 if( i<0 ){
88878 u = ~i;
88879 }else{
88880 u = i;
88881 }
88882 if( u<=127 ){
88883 if( (i&1)==i && file_format>=4 ){
88884 *pLen = 0;
88885 return 8+(u32)u;
88886 }else{
88887 *pLen = 1;
88888 return 1;
88889 }
88890 }
88891 if( u<=32767 ){ *pLen = 2; return 2; }
88892 if( u<=8388607 ){ *pLen = 3; return 3; }
88893 if( u<=2147483647 ){ *pLen = 4; return 4; }
88894 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
88895 *pLen = 8;
88896 if( flags&MEM_IntReal0x0020 ){
88897 /* If the value is IntReal and is going to take up 8 bytes to store
88898 ** as an integer, then we might as well make it an 8-byte floating
88899 ** point value */
88900 pMem->u.r = (double)pMem->u.i;
88901 pMem->flags &= ~MEM_IntReal0x0020;
88902 pMem->flags |= MEM_Real0x0008;
88903 return 7;
88904 }
88905 return 6;
88906 }
88907 if( flags&MEM_Real0x0008 ){
88908 *pLen = 8;
88909 return 7;
88910 }
88911 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) )((void) (0));
88912 assert( pMem->n>=0 )((void) (0));
88913 n = (u32)pMem->n;
88914 if( flags & MEM_Zero0x0400 ){
88915 n += pMem->u.nZero;
88916 }
88917 *pLen = n;
88918 return ((n*2) + 12 + ((flags&MEM_Str0x0002)!=0));
88919}
88920#endif /* inlined into OP_MakeRecord */
88921
88922/*
88923** The sizes for serial types less than 128
88924*/
88925SQLITE_PRIVATEstatic const u8 sqlite3SmallTypeSizes[128] = {
88926 /* 0 1 2 3 4 5 6 7 8 9 */
88927/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
88928/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
88929/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
88930/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
88931/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
88932/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
88933/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
88934/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
88935/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
88936/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
88937/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
88938/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
88939/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
88940};
88941
88942/*
88943** Return the length of the data corresponding to the supplied serial-type.
88944*/
88945SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
88946 if( serial_type>=128 ){
88947 return (serial_type-12)/2;
88948 }else{
88949 assert( serial_type<12((void) (0))
88950 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 )((void) (0));
88951 return sqlite3SmallTypeSizes[serial_type];
88952 }
88953}
88954SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
88955 assert( serial_type<128 )((void) (0));
88956 return sqlite3SmallTypeSizes[serial_type];
88957}
88958
88959/*
88960** If we are on an architecture with mixed-endian floating
88961** points (ex: ARM7) then swap the lower 4 bytes with the
88962** upper 4 bytes. Return the result.
88963**
88964** For most architectures, this is a no-op.
88965**
88966** (later): It is reported to me that the mixed-endian problem
88967** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
88968** that early versions of GCC stored the two words of a 64-bit
88969** float in the wrong order. And that error has been propagated
88970** ever since. The blame is not necessarily with GCC, though.
88971** GCC might have just copying the problem from a prior compiler.
88972** I am also told that newer versions of GCC that follow a different
88973** ABI get the byte order right.
88974**
88975** Developers using SQLite on an ARM7 should compile and run their
88976** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
88977** enabled, some asserts below will ensure that the byte order of
88978** floating point values is correct.
88979**
88980** (2007-08-30) Frank van Vugt has studied this problem closely
88981** and has send his findings to the SQLite developers. Frank
88982** writes that some Linux kernels offer floating point hardware
88983** emulation that uses only 32-bit mantissas instead of a full
88984** 48-bits as required by the IEEE standard. (This is the
88985** CONFIG_FPE_FASTFPE option.) On such systems, floating point
88986** byte swapping becomes very complicated. To avoid problems,
88987** the necessary byte swapping is carried out using a 64-bit integer
88988** rather than a 64-bit float. Frank assures us that the code here
88989** works for him. We, the developers, have no way to independently
88990** verify this, but Frank seems to know what he is talking about
88991** so we trust him.
88992*/
88993#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
88994SQLITE_PRIVATEstatic u64 sqlite3FloatSwap(u64 in){
88995 union {
88996 u64 r;
88997 u32 i[2];
88998 } u;
88999 u32 t;
89000
89001 u.r = in;
89002 t = u.i[0];
89003 u.i[0] = u.i[1];
89004 u.i[1] = t;
89005 return u.r;
89006}
89007#endif /* SQLITE_MIXED_ENDIAN_64BIT_FLOAT */
89008
89009
89010/* Input "x" is a sequence of unsigned characters that represent a
89011** big-endian integer. Return the equivalent native integer
89012*/
89013#define ONE_BYTE_INT(x)((i8)(x)[0]) ((i8)(x)[0])
89014#define TWO_BYTE_INT(x)(256*(i8)((x)[0])|(x)[1]) (256*(i8)((x)[0])|(x)[1])
89015#define THREE_BYTE_INT(x)(65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
89016#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])
89017#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])
89018
89019/*
89020** Deserialize the data blob pointed to by buf as serial type serial_type
89021** and store the result in pMem.
89022**
89023** This function is implemented as two separate routines for performance.
89024** The few cases that require local variables are broken out into a separate
89025** routine so that in most cases the overhead of moving the stack pointer
89026** is avoided.
89027*/
89028static void serialGet(
89029 const unsigned char *buf, /* Buffer to deserialize from */
89030 u32 serial_type, /* Serial type to deserialize */
89031 Mem *pMem /* Memory cell to write value into */
89032){
89033 u64 x = FOUR_BYTE_UINT(buf)(((u32)(buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
89034 u32 y = FOUR_BYTE_UINT(buf+4)(((u32)(buf+4)[0]<<24)|((buf+4)[1]<<16)|((buf+4)[
2]<<8)|(buf+4)[3])
;
89035 x = (x<<32) + y;
89036 if( serial_type==6 ){
89037 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
89038 ** twos-complement integer. */
89039 pMem->u.i = *(i64*)&x;
89040 pMem->flags = MEM_Int0x0004;
89041 testcase( pMem->u.i<0 );
89042 }else{
89043 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
89044 ** floating point number. */
89045#if !defined(NDEBUG1) && !defined(SQLITE_OMIT_FLOATING_POINT)
89046 /* Verify that integers and floating point values use the same
89047 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
89048 ** defined that 64-bit floating point values really are mixed
89049 ** endian.
89050 */
89051 static const u64 t1 = ((u64)0x3ff00000)<<32;
89052 static const double r1 = 1.0;
89053 u64 t2 = t1;
89054 swapMixedEndianFloat(t2);
89055 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 )((void) (0));
89056#endif
89057 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 )((void) (0));
89058 swapMixedEndianFloat(x);
89059 memcpy(&pMem->u.r, &x, sizeof(x));
89060 pMem->flags = IsNaN(x)(((x)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((x)&((((u64)1)<<52)-1))!=0)
? MEM_Null0x0001 : MEM_Real0x0008;
89061 }
89062}
89063static int serialGet7(
89064 const unsigned char *buf, /* Buffer to deserialize from */
89065 Mem *pMem /* Memory cell to write value into */
89066){
89067 u64 x = FOUR_BYTE_UINT(buf)(((u32)(buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
89068 u32 y = FOUR_BYTE_UINT(buf+4)(((u32)(buf+4)[0]<<24)|((buf+4)[1]<<16)|((buf+4)[
2]<<8)|(buf+4)[3])
;
89069 x = (x<<32) + y;
89070 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 )((void) (0));
89071 swapMixedEndianFloat(x);
89072 memcpy(&pMem->u.r, &x, sizeof(x));
89073 if( IsNaN(x)(((x)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((x)&((((u64)1)<<52)-1))!=0)
){
89074 pMem->flags = MEM_Null0x0001;
89075 return 1;
89076 }
89077 pMem->flags = MEM_Real0x0008;
89078 return 0;
89079}
89080SQLITE_PRIVATEstatic void sqlite3VdbeSerialGet(
89081 const unsigned char *buf, /* Buffer to deserialize from */
89082 u32 serial_type, /* Serial type to deserialize */
89083 Mem *pMem /* Memory cell to write value into */
89084){
89085 switch( serial_type ){
89086 case 10: { /* Internal use only: NULL with virtual table
89087 ** UPDATE no-change flag set */
89088 pMem->flags = MEM_Null0x0001|MEM_Zero0x0400;
89089 pMem->n = 0;
89090 pMem->u.nZero = 0;
89091 return;
89092 }
89093 case 11: /* Reserved for future use */
89094 case 0: { /* Null */
89095 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
89096 pMem->flags = MEM_Null0x0001;
89097 return;
89098 }
89099 case 1: {
89100 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
89101 ** integer. */
89102 pMem->u.i = ONE_BYTE_INT(buf)((i8)(buf)[0]);
89103 pMem->flags = MEM_Int0x0004;
89104 testcase( pMem->u.i<0 );
89105 return;
89106 }
89107 case 2: { /* 2-byte signed integer */
89108 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
89109 ** twos-complement integer. */
89110 pMem->u.i = TWO_BYTE_INT(buf)(256*(i8)((buf)[0])|(buf)[1]);
89111 pMem->flags = MEM_Int0x0004;
89112 testcase( pMem->u.i<0 );
89113 return;
89114 }
89115 case 3: { /* 3-byte signed integer */
89116 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
89117 ** twos-complement integer. */
89118 pMem->u.i = THREE_BYTE_INT(buf)(65536*(i8)((buf)[0])|((buf)[1]<<8)|(buf)[2]);
89119 pMem->flags = MEM_Int0x0004;
89120 testcase( pMem->u.i<0 );
89121 return;
89122 }
89123 case 4: { /* 4-byte signed integer */
89124 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
89125 ** twos-complement integer. */
89126 pMem->u.i = FOUR_BYTE_INT(buf)(16777216*(i8)((buf)[0])|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
89127#ifdef __HP_cc
89128 /* Work around a sign-extension bug in the HP compiler for HP/UX */
89129 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
89130#endif
89131 pMem->flags = MEM_Int0x0004;
89132 testcase( pMem->u.i<0 );
89133 return;
89134 }
89135 case 5: { /* 6-byte signed integer */
89136 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
89137 ** twos-complement integer. */
89138 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]);
89139 pMem->flags = MEM_Int0x0004;
89140 testcase( pMem->u.i<0 );
89141 return;
89142 }
89143 case 6: /* 8-byte signed integer */
89144 case 7: { /* IEEE floating point */
89145 /* These use local variables, so do them in a separate routine
89146 ** to avoid having to move the frame pointer in the common case */
89147 serialGet(buf,serial_type,pMem);
89148 return;
89149 }
89150 case 8: /* Integer 0 */
89151 case 9: { /* Integer 1 */
89152 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
89153 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
89154 pMem->u.i = serial_type-8;
89155 pMem->flags = MEM_Int0x0004;
89156 return;
89157 }
89158 default: {
89159 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
89160 ** length.
89161 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
89162 ** (N-13)/2 bytes in length. */
89163 static const u16 aFlag[] = { MEM_Blob0x0010|MEM_Ephem0x4000, MEM_Str0x0002|MEM_Ephem0x4000 };
89164 pMem->z = (char *)buf;
89165 pMem->n = (serial_type-12)/2;
89166 pMem->flags = aFlag[serial_type&1];
89167 return;
89168 }
89169 }
89170 return;
89171}
89172/*
89173** This routine is used to allocate sufficient space for an UnpackedRecord
89174** structure large enough to be used with sqlite3VdbeRecordUnpack() if
89175** the first argument is a pointer to KeyInfo structure pKeyInfo.
89176**
89177** The space is either allocated using sqlite3DbMallocRaw() or from within
89178** the unaligned buffer passed via the second and third arguments (presumably
89179** stack space). If the former, then *ppFree is set to a pointer that should
89180** be eventually freed by the caller using sqlite3DbFree(). Or, if the
89181** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
89182** before returning.
89183**
89184** If an OOM error occurs, NULL is returned.
89185*/
89186SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
89187 KeyInfo *pKeyInfo /* Description of the record */
89188){
89189 UnpackedRecord *p; /* Unpacked record to return */
89190 int nByte; /* Number of bytes required for *p */
89191 nByte = ROUND8P(sizeof(UnpackedRecord))(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
89192 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
89193 if( !p ) return 0;
89194 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))(sizeof(UnpackedRecord))];
89195 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
89196 p->pKeyInfo = pKeyInfo;
89197 p->nField = pKeyInfo->nKeyField + 1;
89198 return p;
89199}
89200
89201/*
89202** Given the nKey-byte encoding of a record in pKey[], populate the
89203** UnpackedRecord structure indicated by the fourth argument with the
89204** contents of the decoded record.
89205*/
89206SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(
89207 KeyInfo *pKeyInfo, /* Information about the record format */
89208 int nKey, /* Size of the binary record */
89209 const void *pKey, /* The binary record */
89210 UnpackedRecord *p /* Populate this structure before returning. */
89211){
89212 const unsigned char *aKey = (const unsigned char *)pKey;
89213 u32 d;
89214 u32 idx; /* Offset in aKey[] to read from */
89215 u16 u; /* Unsigned loop counter */
89216 u32 szHdr;
89217 Mem *pMem = p->aMem;
89218
89219 p->default_rc = 0;
89220 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
89221 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
89222 d = szHdr;
89223 u = 0;
89224 while( idx<szHdr && d<=(u32)nKey ){
89225 u32 serial_type;
89226
89227 idx += getVarint32(&aKey[idx], serial_type)(u8)((*(&aKey[idx])<(u8)0x80)?((serial_type)=(u32)*(&
aKey[idx])),1:sqlite3GetVarint32((&aKey[idx]),(u32 *)&
(serial_type)))
;
89228 pMem->enc = pKeyInfo->enc;
89229 pMem->db = pKeyInfo->db;
89230 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
89231 pMem->szMalloc = 0;
89232 pMem->z = 0;
89233 sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
89234 d += sqlite3VdbeSerialTypeLen(serial_type);
89235 pMem++;
89236 if( (++u)>=p->nField ) break;
89237 }
89238 if( d>(u32)nKey && u ){
89239 assert( CORRUPT_DB )((void) (0));
89240 /* In a corrupt record entry, the last pMem might have been set up using
89241 ** uninitialized memory. Overwrite its value with NULL, to prevent
89242 ** warnings from MSAN. */
89243 sqlite3VdbeMemSetNull(pMem-1);
89244 }
89245 assert( u<=pKeyInfo->nKeyField + 1 )((void) (0));
89246 p->nField = u;
89247}
89248
89249#ifdef SQLITE_DEBUG
89250/*
89251** This function compares two index or table record keys in the same way
89252** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
89253** this function deserializes and compares values using the
89254** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
89255** in assert() statements to ensure that the optimized code in
89256** sqlite3VdbeRecordCompare() returns results with these two primitives.
89257**
89258** Return true if the result of comparison is equivalent to desiredResult.
89259** Return false if there is a disagreement.
89260*/
89261static int vdbeRecordCompareDebug(
89262 int nKey1, const void *pKey1, /* Left key */
89263 const UnpackedRecord *pPKey2, /* Right key */
89264 int desiredResult /* Correct answer */
89265){
89266 u32 d1; /* Offset into aKey[] of next data element */
89267 u32 idx1; /* Offset into aKey[] of next header element */
89268 u32 szHdr1; /* Number of bytes in header */
89269 int i = 0;
89270 int rc = 0;
89271 const unsigned char *aKey1 = (const unsigned char *)pKey1;
89272 KeyInfo *pKeyInfo;
89273 Mem mem1;
89274
89275 pKeyInfo = pPKey2->pKeyInfo;
89276 if( pKeyInfo->db==0 ) return 1;
89277 mem1.enc = pKeyInfo->enc;
89278 mem1.db = pKeyInfo->db;
89279 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
89280 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
89281
89282 /* Compilers may complain that mem1.u.i is potentially uninitialized.
89283 ** We could initialize it, as shown here, to silence those complaints.
89284 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
89285 ** the unnecessary initialization has a measurable negative performance
89286 ** impact, since this routine is a very high runner. And so, we choose
89287 ** to ignore the compiler warnings and leave this variable uninitialized.
89288 */
89289 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
89290
89291 idx1 = getVarint32(aKey1, szHdr1)(u8)((*(aKey1)<(u8)0x80)?((szHdr1)=(u32)*(aKey1)),1:sqlite3GetVarint32
((aKey1),(u32 *)&(szHdr1)))
;
89292 if( szHdr1>98307 ) return SQLITE_CORRUPT11;
89293 d1 = szHdr1;
89294 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB )((void) (0));
89295 assert( pKeyInfo->aSortFlags!=0 )((void) (0));
89296 assert( pKeyInfo->nKeyField>0 )((void) (0));
89297 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
89298 do{
89299 u32 serial_type1;
89300
89301 /* Read the serial types for the next element in each key. */
89302 idx1 += getVarint32( aKey1+idx1, serial_type1 )(u8)((*(aKey1+idx1)<(u8)0x80)?((serial_type1)=(u32)*(aKey1
+idx1)),1:sqlite3GetVarint32((aKey1+idx1),(u32 *)&(serial_type1
)))
;
89303
89304 /* Verify that there is enough key space remaining to avoid
89305 ** a buffer overread. The "d1+serial_type1+2" subexpression will
89306 ** always be greater than or equal to the amount of required key space.
89307 ** Use that approximation to avoid the more expensive call to
89308 ** sqlite3VdbeSerialTypeLen() in the common case.
89309 */
89310 if( d1+(u64)serial_type1+2>(u64)nKey1
89311 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
89312 ){
89313 if( serial_type1>=1
89314 && serial_type1<=7
89315 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)<=(u64)nKey1+8
89316 && CORRUPT_DB(sqlite3Config.neverCorrupt==0)
89317 ){
89318 return 1; /* corrupt record not detected by
89319 ** sqlite3VdbeRecordCompareWithSkip(). Return true
89320 ** to avoid firing the assert() */
89321 }
89322 break;
89323 }
89324
89325 /* Extract the values to be compared.
89326 */
89327 sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
89328 d1 += sqlite3VdbeSerialTypeLen(serial_type1);
89329
89330 /* Do the comparison
89331 */
89332 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
89333 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
89334 if( rc!=0 ){
89335 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
89336 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL0x02)
89337 && ((mem1.flags & MEM_Null0x0001) || (pPKey2->aMem[i].flags & MEM_Null0x0001))
89338 ){
89339 rc = -rc;
89340 }
89341 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC0x01 ){
89342 rc = -rc; /* Invert the result for DESC sort order. */
89343 }
89344 goto debugCompareEnd;
89345 }
89346 i++;
89347 }while( idx1<szHdr1 && i<pPKey2->nField );
89348
89349 /* No memory allocation is ever used on mem1. Prove this using
89350 ** the following assert(). If the assert() fails, it indicates a
89351 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
89352 */
89353 assert( mem1.szMalloc==0 )((void) (0));
89354
89355 /* rc==0 here means that one of the keys ran out of fields and
89356 ** all the fields up to that point were equal. Return the default_rc
89357 ** value. */
89358 rc = pPKey2->default_rc;
89359
89360debugCompareEnd:
89361 if( desiredResult==0 && rc==0 ) return 1;
89362 if( desiredResult<0 && rc<0 ) return 1;
89363 if( desiredResult>0 && rc>0 ) return 1;
89364 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return 1;
89365 if( pKeyInfo->db->mallocFailed ) return 1;
89366 return 0;
89367}
89368#endif
89369
89370#ifdef SQLITE_DEBUG
89371/*
89372** Count the number of fields (a.k.a. columns) in the record given by
89373** pKey,nKey. The verify that this count is less than or equal to the
89374** limit given by pKeyInfo->nAllField.
89375**
89376** If this constraint is not satisfied, it means that the high-speed
89377** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
89378** not work correctly. If this assert() ever fires, it probably means
89379** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
89380** incorrectly.
89381*/
89382static void vdbeAssertFieldCountWithinLimits(
89383 int nKey, const void *pKey, /* The record to verify */
89384 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
89385){
89386 int nField = 0;
89387 u32 szHdr;
89388 u32 idx;
89389 u32 notUsed;
89390 const unsigned char *aKey = (const unsigned char*)pKey;
89391
89392 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return;
89393 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
89394 assert( nKey>=0 )((void) (0));
89395 assert( szHdr<=(u32)nKey )((void) (0));
89396 while( idx<szHdr ){
89397 idx += getVarint32(aKey+idx, notUsed)(u8)((*(aKey+idx)<(u8)0x80)?((notUsed)=(u32)*(aKey+idx)),1
:sqlite3GetVarint32((aKey+idx),(u32 *)&(notUsed)))
;
89398 nField++;
89399 }
89400 assert( nField <= pKeyInfo->nAllField )((void) (0));
89401}
89402#else
89403# define vdbeAssertFieldCountWithinLimits(A,B,C)
89404#endif
89405
89406/*
89407** Both *pMem1 and *pMem2 contain string values. Compare the two values
89408** using the collation sequence pColl. As usual, return a negative , zero
89409** or positive value if *pMem1 is less than, equal to or greater than
89410** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
89411*/
89412static int vdbeCompareMemString(
89413 const Mem *pMem1,
89414 const Mem *pMem2,
89415 const CollSeq *pColl,
89416 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
89417){
89418 if( pMem1->enc==pColl->enc ){
89419 /* The strings are already in the correct encoding. Call the
89420 ** comparison function directly */
89421 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
89422 }else{
89423 int rc;
89424 const void *v1, *v2;
89425 Mem c1;
89426 Mem c2;
89427 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null0x0001);
89428 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null0x0001);
89429 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem0x4000);
89430 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem0x4000);
89431 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
89432 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
89433 if( (v1==0 || v2==0) ){
89434 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT7;
89435 rc = 0;
89436 }else{
89437 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
89438 }
89439 sqlite3VdbeMemReleaseMalloc(&c1);
89440 sqlite3VdbeMemReleaseMalloc(&c2);
89441 return rc;
89442 }
89443}
89444
89445/*
89446** The input pBlob is guaranteed to be a Blob that is not marked
89447** with MEM_Zero. Return true if it could be a zero-blob.
89448*/
89449static int isAllZero(const char *z, int n){
89450 int i;
89451 for(i=0; i<n; i++){
89452 if( z[i] ) return 0;
89453 }
89454 return 1;
89455}
89456
89457/*
89458** Compare two blobs. Return negative, zero, or positive if the first
89459** is less than, equal to, or greater than the second, respectively.
89460** If one blob is a prefix of the other, then the shorter is the lessor.
89461*/
89462SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
89463 int c;
89464 int n1 = pB1->n;
89465 int n2 = pB2->n;
89466
89467 /* It is possible to have a Blob value that has some non-zero content
89468 ** followed by zero content. But that only comes up for Blobs formed
89469 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
89470 ** sqlite3MemCompare(). */
89471 assert( (pB1->flags & MEM_Zero)==0 || n1==0 )((void) (0));
89472 assert( (pB2->flags & MEM_Zero)==0 || n2==0 )((void) (0));
89473
89474 if( (pB1->flags|pB2->flags) & MEM_Zero0x0400 ){
89475 if( pB1->flags & pB2->flags & MEM_Zero0x0400 ){
89476 return pB1->u.nZero - pB2->u.nZero;
89477 }else if( pB1->flags & MEM_Zero0x0400 ){
89478 if( !isAllZero(pB2->z, pB2->n) ) return -1;
89479 return pB1->u.nZero - n2;
89480 }else{
89481 if( !isAllZero(pB1->z, pB1->n) ) return +1;
89482 return n1 - pB2->u.nZero;
89483 }
89484 }
89485 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
89486 if( c ) return c;
89487 return n1 - n2;
89488}
89489
89490/* The following two functions are used only within testcase() to prove
89491** test coverage. These functions do no exist for production builds.
89492** We must use separate SQLITE_NOINLINE functions here, since otherwise
89493** optimizer code movement causes gcov to become very confused.
89494*/
89495#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
89496static int SQLITE_NOINLINE__attribute__((noinline)) doubleLt(double a, double b){ return a<b; }
89497static int SQLITE_NOINLINE__attribute__((noinline)) doubleEq(double a, double b){ return a==b; }
89498#endif
89499
89500/*
89501** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
89502** number. Return negative, zero, or positive if the first (i64) is less than,
89503** equal to, or greater than the second (double).
89504*/
89505SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64 i, double r){
89506 if( sqlite3IsNaN(r) ){
89507 /* SQLite considers NaN to be a NULL. And all integer values are greater
89508 ** than NULL */
89509 return 1;
89510 }else{
89511 i64 y;
89512 if( r<-9223372036854775808.0 ) return +1;
89513 if( r>=9223372036854775808.0 ) return -1;
89514 y = (i64)r;
89515 if( i<y ) return -1;
89516 if( i>y ) return +1;
89517 testcase( doubleLt(((double)i),r) );
89518 testcase( doubleLt(r,((double)i)) );
89519 testcase( doubleEq(r,((double)i)) );
89520 return (((double)i)<r) ? -1 : (((double)i)>r);
89521 }
89522}
89523
89524/*
89525** Compare the values contained by the two memory cells, returning
89526** negative, zero or positive if pMem1 is less than, equal to, or greater
89527** than pMem2. Sorting order is NULL's first, followed by numbers (integers
89528** and reals) sorted numerically, followed by text ordered by the collating
89529** sequence pColl and finally blob's ordered by memcmp().
89530**
89531** Two NULL values are considered equal by this function.
89532*/
89533SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
89534 int f1, f2;
89535 int combined_flags;
89536
89537 f1 = pMem1->flags;
89538 f2 = pMem2->flags;
89539 combined_flags = f1|f2;
89540 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) )((void) (0));
89541
89542 /* If one value is NULL, it is less than the other. If both values
89543 ** are NULL, return 0.
89544 */
89545 if( combined_flags&MEM_Null0x0001 ){
89546 return (f2&MEM_Null0x0001) - (f1&MEM_Null0x0001);
89547 }
89548
89549 /* At least one of the two values is a number
89550 */
89551 if( combined_flags&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020) ){
89552 testcase( combined_flags & MEM_Int );
89553 testcase( combined_flags & MEM_Real );
89554 testcase( combined_flags & MEM_IntReal );
89555 if( (f1 & f2 & (MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
89556 testcase( f1 & f2 & MEM_Int );
89557 testcase( f1 & f2 & MEM_IntReal );
89558 if( pMem1->u.i < pMem2->u.i ) return -1;
89559 if( pMem1->u.i > pMem2->u.i ) return +1;
89560 return 0;
89561 }
89562 if( (f1 & f2 & MEM_Real0x0008)!=0 ){
89563 if( pMem1->u.r < pMem2->u.r ) return -1;
89564 if( pMem1->u.r > pMem2->u.r ) return +1;
89565 return 0;
89566 }
89567 if( (f1&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
89568 testcase( f1 & MEM_Int );
89569 testcase( f1 & MEM_IntReal );
89570 if( (f2&MEM_Real0x0008)!=0 ){
89571 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
89572 }else if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
89573 if( pMem1->u.i < pMem2->u.i ) return -1;
89574 if( pMem1->u.i > pMem2->u.i ) return +1;
89575 return 0;
89576 }else{
89577 return -1;
89578 }
89579 }
89580 if( (f1&MEM_Real0x0008)!=0 ){
89581 if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
89582 testcase( f2 & MEM_Int );
89583 testcase( f2 & MEM_IntReal );
89584 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
89585 }else{
89586 return -1;
89587 }
89588 }
89589 return +1;
89590 }
89591
89592 /* If one value is a string and the other is a blob, the string is less.
89593 ** If both are strings, compare using the collating functions.
89594 */
89595 if( combined_flags&MEM_Str0x0002 ){
89596 if( (f1 & MEM_Str0x0002)==0 ){
89597 return 1;
89598 }
89599 if( (f2 & MEM_Str0x0002)==0 ){
89600 return -1;
89601 }
89602
89603 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed )((void) (0));
89604 assert( pMem1->enc==SQLITE_UTF8 ||((void) (0))
89605 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE )((void) (0));
89606
89607 /* The collation sequence must be defined at this point, even if
89608 ** the user deletes the collation sequence after the vdbe program is
89609 ** compiled (this was not always the case).
89610 */
89611 assert( !pColl || pColl->xCmp )((void) (0));
89612
89613 if( pColl ){
89614 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
89615 }
89616 /* If a NULL pointer was passed as the collate function, fall through
89617 ** to the blob case and use memcmp(). */
89618 }
89619
89620 /* Both values must be blobs. Compare using memcmp(). */
89621 return sqlite3BlobCompare(pMem1, pMem2);
89622}
89623
89624
89625/*
89626** The first argument passed to this function is a serial-type that
89627** corresponds to an integer - all values between 1 and 9 inclusive
89628** except 7. The second points to a buffer containing an integer value
89629** serialized according to serial_type. This function deserializes
89630** and returns the value.
89631*/
89632static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
89633 u32 y;
89634 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) )((void) (0));
89635 switch( serial_type ){
89636 case 0:
89637 case 1:
89638 testcase( aKey[0]&0x80 );
89639 return ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
89640 case 2:
89641 testcase( aKey[0]&0x80 );
89642 return TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
89643 case 3:
89644 testcase( aKey[0]&0x80 );
89645 return THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
89646 case 4: {
89647 testcase( aKey[0]&0x80 );
89648 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
89649 return (i64)*(int*)&y;
89650 }
89651 case 5: {
89652 testcase( aKey[0]&0x80 );
89653 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]);
89654 }
89655 case 6: {
89656 u64 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
89657 testcase( aKey[0]&0x80 );
89658 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
89659 return (i64)*(i64*)&x;
89660 }
89661 }
89662
89663 return (serial_type - 8);
89664}
89665
89666/*
89667** This function compares the two table rows or index records
89668** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
89669** or positive integer if key1 is less than, equal to or
89670** greater than key2. The {nKey1, pKey1} key must be a blob
89671** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
89672** key must be a parsed key such as obtained from
89673** sqlite3VdbeParseRecord.
89674**
89675** If argument bSkip is non-zero, it is assumed that the caller has already
89676** determined that the first fields of the keys are equal.
89677**
89678** Key1 and Key2 do not have to contain the same number of fields. If all
89679** fields that appear in both keys are equal, then pPKey2->default_rc is
89680** returned.
89681**
89682** If database corruption is discovered, set pPKey2->errCode to
89683** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
89684** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
89685** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
89686*/
89687SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(
89688 int nKey1, const void *pKey1, /* Left key */
89689 UnpackedRecord *pPKey2, /* Right key */
89690 int bSkip /* If true, skip the first field */
89691){
89692 u32 d1; /* Offset into aKey[] of next data element */
89693 int i; /* Index of next field to compare */
89694 u32 szHdr1; /* Size of record header in bytes */
89695 u32 idx1; /* Offset of first type in header */
89696 int rc = 0; /* Return value */
89697 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
89698 KeyInfo *pKeyInfo;
89699 const unsigned char *aKey1 = (const unsigned char *)pKey1;
89700 Mem mem1;
89701
89702 /* If bSkip is true, then the caller has already determined that the first
89703 ** two elements in the keys are equal. Fix the various stack variables so
89704 ** that this routine begins comparing at the second field. */
89705 if( bSkip ){
89706 u32 s1 = aKey1[1];
89707 if( s1<0x80 ){
89708 idx1 = 2;
89709 }else{
89710 idx1 = 1 + sqlite3GetVarint32(&aKey1[1], &s1);
89711 }
89712 szHdr1 = aKey1[0];
89713 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
89714 i = 1;
89715 pRhs++;
89716 }else{
89717 if( (szHdr1 = aKey1[0])<0x80 ){
89718 idx1 = 1;
89719 }else{
89720 idx1 = sqlite3GetVarint32(aKey1, &szHdr1);
89721 }
89722 d1 = szHdr1;
89723 i = 0;
89724 }
89725 if( d1>(unsigned)nKey1 ){
89726 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(89726);
89727 return 0; /* Corruption */
89728 }
89729
89730 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
89731 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField((void) (0))
89732 || CORRUPT_DB )((void) (0));
89733 assert( pPKey2->pKeyInfo->aSortFlags!=0 )((void) (0));
89734 assert( pPKey2->pKeyInfo->nKeyField>0 )((void) (0));
89735 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
89736 while( 1 /*exit-by-break*/ ){
89737 u32 serial_type;
89738
89739 /* RHS is an integer */
89740 if( pRhs->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
89741 testcase( pRhs->flags & MEM_Int );
89742 testcase( pRhs->flags & MEM_IntReal );
89743 serial_type = aKey1[idx1];
89744 testcase( serial_type==12 );
89745 if( serial_type>=10 ){
89746 rc = serial_type==10 ? -1 : +1;
89747 }else if( serial_type==0 ){
89748 rc = -1;
89749 }else if( serial_type==7 ){
89750 serialGet7(&aKey1[d1], &mem1);
89751 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
89752 }else{
89753 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
89754 i64 rhs = pRhs->u.i;
89755 if( lhs<rhs ){
89756 rc = -1;
89757 }else if( lhs>rhs ){
89758 rc = +1;
89759 }
89760 }
89761 }
89762
89763 /* RHS is real */
89764 else if( pRhs->flags & MEM_Real0x0008 ){
89765 serial_type = aKey1[idx1];
89766 if( serial_type>=10 ){
89767 /* Serial types 12 or greater are strings and blobs (greater than
89768 ** numbers). Types 10 and 11 are currently "reserved for future
89769 ** use", so it doesn't really matter what the results of comparing
89770 ** them to numeric values are. */
89771 rc = serial_type==10 ? -1 : +1;
89772 }else if( serial_type==0 ){
89773 rc = -1;
89774 }else{
89775 if( serial_type==7 ){
89776 if( serialGet7(&aKey1[d1], &mem1) ){
89777 rc = -1; /* mem1 is a NaN */
89778 }else if( mem1.u.r<pRhs->u.r ){
89779 rc = -1;
89780 }else if( mem1.u.r>pRhs->u.r ){
89781 rc = +1;
89782 }else{
89783 assert( rc==0 )((void) (0));
89784 }
89785 }else{
89786 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
89787 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
89788 }
89789 }
89790 }
89791
89792 /* RHS is a string */
89793 else if( pRhs->flags & MEM_Str0x0002 ){
89794 getVarint32NR(&aKey1[idx1], serial_type)serial_type=(u32)*(&aKey1[idx1]);if(serial_type>=0x80)
sqlite3GetVarint32((&aKey1[idx1]),(u32*)&(serial_type
))
;
89795 testcase( serial_type==12 );
89796 if( serial_type<12 ){
89797 rc = -1;
89798 }else if( !(serial_type & 0x01) ){
89799 rc = +1;
89800 }else{
89801 mem1.n = (serial_type - 12) / 2;
89802 testcase( (d1+mem1.n)==(unsigned)nKey1 );
89803 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
89804 if( (d1+mem1.n) > (unsigned)nKey1
89805 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
89806 ){
89807 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(89807);
89808 return 0; /* Corruption */
89809 }else if( pKeyInfo->aColl[i] ){
89810 mem1.enc = pKeyInfo->enc;
89811 mem1.db = pKeyInfo->db;
89812 mem1.flags = MEM_Str0x0002;
89813 mem1.z = (char*)&aKey1[d1];
89814 rc = vdbeCompareMemString(
89815 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
89816 );
89817 }else{
89818 int nCmp = MIN(mem1.n, pRhs->n)((mem1.n)<(pRhs->n)?(mem1.n):(pRhs->n));
89819 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
89820 if( rc==0 ) rc = mem1.n - pRhs->n;
89821 }
89822 }
89823 }
89824
89825 /* RHS is a blob */
89826 else if( pRhs->flags & MEM_Blob0x0010 ){
89827 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 )((void) (0));
89828 getVarint32NR(&aKey1[idx1], serial_type)serial_type=(u32)*(&aKey1[idx1]);if(serial_type>=0x80)
sqlite3GetVarint32((&aKey1[idx1]),(u32*)&(serial_type
))
;
89829 testcase( serial_type==12 );
89830 if( serial_type<12 || (serial_type & 0x01) ){
89831 rc = -1;
89832 }else{
89833 int nStr = (serial_type - 12) / 2;
89834 testcase( (d1+nStr)==(unsigned)nKey1 );
89835 testcase( (d1+nStr+1)==(unsigned)nKey1 );
89836 if( (d1+nStr) > (unsigned)nKey1 ){
89837 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(89837);
89838 return 0; /* Corruption */
89839 }else if( pRhs->flags & MEM_Zero0x0400 ){
89840 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
89841 rc = 1;
89842 }else{
89843 rc = nStr - pRhs->u.nZero;
89844 }
89845 }else{
89846 int nCmp = MIN(nStr, pRhs->n)((nStr)<(pRhs->n)?(nStr):(pRhs->n));
89847 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
89848 if( rc==0 ) rc = nStr - pRhs->n;
89849 }
89850 }
89851 }
89852
89853 /* RHS is null */
89854 else{
89855 serial_type = aKey1[idx1];
89856 if( serial_type==0
89857 || serial_type==10
89858 || (serial_type==7 && serialGet7(&aKey1[d1], &mem1)!=0)
89859 ){
89860 assert( rc==0 )((void) (0));
89861 }else{
89862 rc = 1;
89863 }
89864 }
89865
89866 if( rc!=0 ){
89867 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
89868 if( sortFlags ){
89869 if( (sortFlags & KEYINFO_ORDER_BIGNULL0x02)==0
89870 || ((sortFlags & KEYINFO_ORDER_DESC0x01)
89871 !=(serial_type==0 || (pRhs->flags&MEM_Null0x0001)))
89872 ){
89873 rc = -rc;
89874 }
89875 }
89876 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) )((void) (0));
89877 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
89878 return rc;
89879 }
89880
89881 i++;
89882 if( i==pPKey2->nField ) break;
89883 pRhs++;
89884 d1 += sqlite3VdbeSerialTypeLen(serial_type);
89885 if( d1>(unsigned)nKey1 ) break;
89886 idx1 += sqlite3VarintLen(serial_type);
89887 if( idx1>=(unsigned)szHdr1 ){
89888 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(89888);
89889 return 0; /* Corrupt index */
89890 }
89891 }
89892
89893 /* No memory allocation is ever used on mem1. Prove this using
89894 ** the following assert(). If the assert() fails, it indicates a
89895 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
89896 assert( mem1.szMalloc==0 )((void) (0));
89897
89898 /* rc==0 here means that one or both of the keys ran out of fields and
89899 ** all the fields up to that point were equal. Return the default_rc
89900 ** value. */
89901 assert( CORRUPT_DB((void) (0))
89902 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)((void) (0))
89903 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
89904 )((void) (0));
89905 pPKey2->eqSeen = 1;
89906 return pPKey2->default_rc;
89907}
89908SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(
89909 int nKey1, const void *pKey1, /* Left key */
89910 UnpackedRecord *pPKey2 /* Right key */
89911){
89912 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
89913}
89914
89915
89916/*
89917** This function is an optimized version of sqlite3VdbeRecordCompare()
89918** that (a) the first field of pPKey2 is an integer, and (b) the
89919** size-of-header varint at the start of (pKey1/nKey1) fits in a single
89920** byte (i.e. is less than 128).
89921**
89922** To avoid concerns about buffer overreads, this routine is only used
89923** on schemas where the maximum valid header size is 63 bytes or less.
89924*/
89925static int vdbeRecordCompareInt(
89926 int nKey1, const void *pKey1, /* Left key */
89927 UnpackedRecord *pPKey2 /* Right key */
89928){
89929 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
89930 int serial_type = ((const u8*)pKey1)[1];
89931 int res;
89932 u32 y;
89933 u64 x;
89934 i64 v;
89935 i64 lhs;
89936
89937 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89938 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB )((void) (0));
89939 switch( serial_type ){
89940 case 1: { /* 1-byte signed integer */
89941 lhs = ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
89942 testcase( lhs<0 );
89943 break;
89944 }
89945 case 2: { /* 2-byte signed integer */
89946 lhs = TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
89947 testcase( lhs<0 );
89948 break;
89949 }
89950 case 3: { /* 3-byte signed integer */
89951 lhs = THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
89952 testcase( lhs<0 );
89953 break;
89954 }
89955 case 4: { /* 4-byte signed integer */
89956 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
89957 lhs = (i64)*(int*)&y;
89958 testcase( lhs<0 );
89959 break;
89960 }
89961 case 5: { /* 6-byte signed integer */
89962 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]);
89963 testcase( lhs<0 );
89964 break;
89965 }
89966 case 6: { /* 8-byte signed integer */
89967 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
89968 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
89969 lhs = *(i64*)&x;
89970 testcase( lhs<0 );
89971 break;
89972 }
89973 case 8:
89974 lhs = 0;
89975 break;
89976 case 9:
89977 lhs = 1;
89978 break;
89979
89980 /* This case could be removed without changing the results of running
89981 ** this code. Including it causes gcc to generate a faster switch
89982 ** statement (since the range of switch targets now starts at zero and
89983 ** is contiguous) but does not cause any duplicate code to be generated
89984 ** (as gcc is clever enough to combine the two like cases). Other
89985 ** compilers might be similar. */
89986 case 0: case 7:
89987 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
89988
89989 default:
89990 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
89991 }
89992
89993 assert( pPKey2->u.i == pPKey2->aMem[0].u.i )((void) (0));
89994 v = pPKey2->u.i;
89995 if( v>lhs ){
89996 res = pPKey2->r1;
89997 }else if( v<lhs ){
89998 res = pPKey2->r2;
89999 }else if( pPKey2->nField>1 ){
90000 /* The first fields of the two keys are equal. Compare the trailing
90001 ** fields. */
90002 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
90003 }else{
90004 /* The first fields of the two keys are equal and there are no trailing
90005 ** fields. Return pPKey2->default_rc in this case. */
90006 res = pPKey2->default_rc;
90007 pPKey2->eqSeen = 1;
90008 }
90009
90010 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) )((void) (0));
90011 return res;
90012}
90013
90014/*
90015** This function is an optimized version of sqlite3VdbeRecordCompare()
90016** that (a) the first field of pPKey2 is a string, that (b) the first field
90017** uses the collation sequence BINARY and (c) that the size-of-header varint
90018** at the start of (pKey1/nKey1) fits in a single byte.
90019*/
90020static int vdbeRecordCompareString(
90021 int nKey1, const void *pKey1, /* Left key */
90022 UnpackedRecord *pPKey2 /* Right key */
90023){
90024 const u8 *aKey1 = (const u8*)pKey1;
90025 int serial_type;
90026 int res;
90027
90028 assert( pPKey2->aMem[0].flags & MEM_Str )((void) (0));
90029 assert( pPKey2->aMem[0].n == pPKey2->n )((void) (0));
90030 assert( pPKey2->aMem[0].z == pPKey2->u.z )((void) (0));
90031 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
90032 serial_type = (signed char)(aKey1[1]);
90033
90034vrcs_restart:
90035 if( serial_type<12 ){
90036 if( serial_type<0 ){
90037 sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type);
90038 if( serial_type>=12 ) goto vrcs_restart;
90039 assert( CORRUPT_DB )((void) (0));
90040 }
90041 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
90042 }else if( !(serial_type & 0x01) ){
90043 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
90044 }else{
90045 int nCmp;
90046 int nStr;
90047 int szHdr = aKey1[0];
90048
90049 nStr = (serial_type-12) / 2;
90050 if( (szHdr + nStr) > nKey1 ){
90051 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(90051);
90052 return 0; /* Corruption */
90053 }
90054 nCmp = MIN( pPKey2->n, nStr )((pPKey2->n)<(nStr)?(pPKey2->n):(nStr));
90055 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
90056
90057 if( res>0 ){
90058 res = pPKey2->r2;
90059 }else if( res<0 ){
90060 res = pPKey2->r1;
90061 }else{
90062 res = nStr - pPKey2->n;
90063 if( res==0 ){
90064 if( pPKey2->nField>1 ){
90065 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
90066 }else{
90067 res = pPKey2->default_rc;
90068 pPKey2->eqSeen = 1;
90069 }
90070 }else if( res>0 ){
90071 res = pPKey2->r2;
90072 }else{
90073 res = pPKey2->r1;
90074 }
90075 }
90076 }
90077
90078 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)((void) (0))
90079 || CORRUPT_DB((void) (0))
90080 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
90081 )((void) (0));
90082 return res;
90083}
90084
90085/*
90086** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
90087** suitable for comparing serialized records to the unpacked record passed
90088** as the only argument.
90089*/
90090SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
90091 /* varintRecordCompareInt() and varintRecordCompareString() both assume
90092 ** that the size-of-header varint that occurs at the start of each record
90093 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
90094 ** also assumes that it is safe to overread a buffer by at least the
90095 ** maximum possible legal header size plus 8 bytes. Because there is
90096 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
90097 ** buffer passed to varintRecordCompareInt() this makes it convenient to
90098 ** limit the size of the header to 64 bytes in cases where the first field
90099 ** is an integer.
90100 **
90101 ** The easiest way to enforce this limit is to consider only records with
90102 ** 13 fields or less. If the first field is an integer, the maximum legal
90103 ** header size is (12*5 + 1 + 1) bytes. */
90104 if( p->pKeyInfo->nAllField<=13 ){
90105 int flags = p->aMem[0].flags;
90106 if( p->pKeyInfo->aSortFlags[0] ){
90107 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL0x02 ){
90108 return sqlite3VdbeRecordCompare;
90109 }
90110 p->r1 = 1;
90111 p->r2 = -1;
90112 }else{
90113 p->r1 = -1;
90114 p->r2 = 1;
90115 }
90116 if( (flags & MEM_Int0x0004) ){
90117 p->u.i = p->aMem[0].u.i;
90118 return vdbeRecordCompareInt;
90119 }
90120 testcase( flags & MEM_Real );
90121 testcase( flags & MEM_Null );
90122 testcase( flags & MEM_Blob );
90123 if( (flags & (MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001|MEM_Blob0x0010))==0
90124 && p->pKeyInfo->aColl[0]==0
90125 ){
90126 assert( flags & MEM_Str )((void) (0));
90127 p->u.z = p->aMem[0].z;
90128 p->n = p->aMem[0].n;
90129 return vdbeRecordCompareString;
90130 }
90131 }
90132
90133 return sqlite3VdbeRecordCompare;
90134}
90135
90136/*
90137** pCur points at an index entry created using the OP_MakeRecord opcode.
90138** Read the rowid (the last field in the record) and store it in *rowid.
90139** Return SQLITE_OK if everything works, or an error code otherwise.
90140**
90141** pCur might be pointing to text obtained from a corrupt database file.
90142** So the content cannot be trusted. Do appropriate checks on the content.
90143*/
90144SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
90145 i64 nCellKey = 0;
90146 int rc;
90147 u32 szHdr; /* Size of the header */
90148 u32 typeRowid; /* Serial type of the rowid */
90149 u32 lenRowid; /* Size of the rowid */
90150 Mem m, v;
90151
90152 /* Get the size of the index entry. Only indices entries of less
90153 ** than 2GiB are support - anything large must be database corruption.
90154 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
90155 ** this code can safely assume that nCellKey is 32-bits
90156 */
90157 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
90158 nCellKey = sqlite3BtreePayloadSize(pCur);
90159 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey )((void) (0));
90160
90161 /* Read in the complete content of the index entry */
90162 sqlite3VdbeMemInit(&m, db, 0);
90163 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
90164 if( rc ){
90165 return rc;
90166 }
90167
90168 /* The index entry must begin with a header size */
90169 getVarint32NR((u8*)m.z, szHdr)szHdr=(u32)*((u8*)m.z);if(szHdr>=0x80)sqlite3GetVarint32((
(u8*)m.z),(u32*)&(szHdr))
;
90170 testcase( szHdr==3 );
90171 testcase( szHdr==(u32)m.n );
90172 testcase( szHdr>0x7fffffff );
90173 assert( m.n>=0 )((void) (0));
90174 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n)(szHdr<3 || szHdr>(unsigned)m.n) ){
90175 goto idx_rowid_corruption;
90176 }
90177
90178 /* The last field of the index should be an integer - the ROWID.
90179 ** Verify that the last entry really is an integer. */
90180 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
))
;
90181 testcase( typeRowid==1 );
90182 testcase( typeRowid==2 );
90183 testcase( typeRowid==3 );
90184 testcase( typeRowid==4 );
90185 testcase( typeRowid==5 );
90186 testcase( typeRowid==6 );
90187 testcase( typeRowid==8 );
90188 testcase( typeRowid==9 );
90189 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7)(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
90190 goto idx_rowid_corruption;
90191 }
90192 lenRowid = sqlite3SmallTypeSizes[typeRowid];
90193 testcase( (u32)m.n==szHdr+lenRowid );
90194 if( unlikely((u32)m.n<szHdr+lenRowid)((u32)m.n<szHdr+lenRowid) ){
90195 goto idx_rowid_corruption;
90196 }
90197
90198 /* Fetch the integer off the end of the index record */
90199 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
90200 *rowid = v.u.i;
90201 sqlite3VdbeMemReleaseMalloc(&m);
90202 return SQLITE_OK0;
90203
90204 /* Jump here if database corruption is detected after m has been
90205 ** allocated. Free the m object and return SQLITE_CORRUPT. */
90206idx_rowid_corruption:
90207 testcase( m.szMalloc!=0 );
90208 sqlite3VdbeMemReleaseMalloc(&m);
90209 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(90209);
90210}
90211
90212/*
90213** Compare the key of the index entry that cursor pC is pointing to against
90214** the key string in pUnpacked. Write into *pRes a number
90215** that is negative, zero, or positive if pC is less than, equal to,
90216** or greater than pUnpacked. Return SQLITE_OK on success.
90217**
90218** pUnpacked is either created without a rowid or is truncated so that it
90219** omits the rowid at the end. The rowid at the end of the index entry
90220** is ignored as well. Hence, this routine only compares the prefixes
90221** of the keys prior to the final rowid, not the entire key.
90222*/
90223SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(
90224 sqlite3 *db, /* Database connection */
90225 VdbeCursor *pC, /* The cursor to compare against */
90226 UnpackedRecord *pUnpacked, /* Unpacked version of key */
90227 int *res /* Write the comparison result here */
90228){
90229 i64 nCellKey = 0;
90230 int rc;
90231 BtCursor *pCur;
90232 Mem m;
90233
90234 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
90235 pCur = pC->uc.pCursor;
90236 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
90237 nCellKey = sqlite3BtreePayloadSize(pCur);
90238 /* nCellKey will always be between 0 and 0xffffffff because of the way
90239 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
90240 if( nCellKey<=0 || nCellKey>0x7fffffff ){
90241 *res = 0;
90242 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(90242);
90243 }
90244 sqlite3VdbeMemInit(&m, db, 0);
90245 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
90246 if( rc ){
90247 return rc;
90248 }
90249 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
90250 sqlite3VdbeMemReleaseMalloc(&m);
90251 return SQLITE_OK0;
90252}
90253
90254/*
90255** This routine sets the value to be returned by subsequent calls to
90256** sqlite3_changes() on the database handle 'db'.
90257*/
90258SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){
90259 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
90260 db->nChange = nChange;
90261 db->nTotalChange += nChange;
90262}
90263
90264/*
90265** Set a flag in the vdbe to update the change counter when it is finalised
90266** or reset.
90267*/
90268SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe *v){
90269 v->changeCntOn = 1;
90270}
90271
90272/*
90273** Mark every prepared statement associated with a database connection
90274** as expired.
90275**
90276** An expired statement means that recompilation of the statement is
90277** recommend. Statements expire when things happen that make their
90278** programs obsolete. Removing user-defined functions or collating
90279** sequences, or changing an authorization function are the types of
90280** things that make prepared statements obsolete.
90281**
90282** If iCode is 1, then expiration is advisory. The statement should
90283** be reprepared before being restarted, but if it is already running
90284** it is allowed to run to completion.
90285**
90286** Internally, this function just sets the Vdbe.expired flag on all
90287** prepared statements. The flag is set to 1 for an immediate expiration
90288** and set to 2 for an advisory expiration.
90289*/
90290SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
90291 Vdbe *p;
90292 for(p = db->pVdbe; p; p=p->pVNext){
90293 p->expired = iCode+1;
90294 }
90295}
90296
90297/*
90298** Return the database associated with the Vdbe.
90299*/
90300SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe *v){
90301 return v->db;
90302}
90303
90304/*
90305** Return the SQLITE_PREPARE flags for a Vdbe.
90306*/
90307SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe *v){
90308 return v->prepFlags;
90309}
90310
90311/*
90312** Return a pointer to an sqlite3_value structure containing the value bound
90313** parameter iVar of VM v. Except, if the value is an SQL NULL, return
90314** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
90315** constants) to the value before returning it.
90316**
90317** The returned value must be freed by the caller using sqlite3ValueFree().
90318*/
90319SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
90320 assert( iVar>0 )((void) (0));
90321 if( v ){
90322 Mem *pMem = &v->aVar[iVar-1];
90323 assert( (v->db->flags & SQLITE_EnableQPSG)==0((void) (0))
90324 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 )((void) (0));
90325 if( 0==(pMem->flags & MEM_Null0x0001) ){
90326 sqlite3_value *pRet = sqlite3ValueNew(v->db);
90327 if( pRet ){
90328 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
90329 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF81);
90330 }
90331 return pRet;
90332 }
90333 }
90334 return 0;
90335}
90336
90337/*
90338** Configure SQL variable iVar so that binding a new value to it signals
90339** to sqlite3_reoptimize() that re-preparing the statement may result
90340** in a better query plan.
90341*/
90342SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
90343 assert( iVar>0 )((void) (0));
90344 assert( (v->db->flags & SQLITE_EnableQPSG)==0((void) (0))
90345 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 )((void) (0));
90346 if( iVar>=32 ){
90347 v->expmask |= 0x80000000;
90348 }else{
90349 v->expmask |= ((u32)1 << (iVar-1));
90350 }
90351}
90352
90353/*
90354** Cause a function to throw an error if it was call from OP_PureFunc
90355** rather than OP_Function.
90356**
90357** OP_PureFunc means that the function must be deterministic, and should
90358** throw an error if it is given inputs that would make it non-deterministic.
90359** This routine is invoked by date/time functions that use non-deterministic
90360** features such as 'now'.
90361*/
90362SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context *pCtx){
90363 const VdbeOp *pOp;
90364#ifdef SQLITE_ENABLE_STAT4
90365 if( pCtx->pVdbe==0 ) return 1;
90366#endif
90367 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
90368 if( pOp->opcode==OP_PureFunc65 ){
90369 const char *zContext;
90370 char *zMsg;
90371 if( pOp->p5 & NC_IsCheck0x000004 ){
90372 zContext = "a CHECK constraint";
90373 }else if( pOp->p5 & NC_GenCol0x000008 ){
90374 zContext = "a generated column";
90375 }else{
90376 zContext = "an index";
90377 }
90378 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
90379 pCtx->pFunc->zName, zContext);
90380 sqlite3_result_error(pCtx, zMsg, -1);
90381 sqlite3_free(zMsg);
90382 return 0;
90383 }
90384 return 1;
90385}
90386
90387#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
90388/*
90389** This Walker callback is used to help verify that calls to
90390** sqlite3BtreeCursorHint() with opcode BTREE_HINT_RANGE have
90391** byte-code register values correctly initialized.
90392*/
90393SQLITE_PRIVATEstatic int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){
90394 if( pExpr->op==TK_REGISTER176 ){
90395 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 )((void) (0));
90396 }
90397 return WRC_Continue0;
90398}
90399#endif /* SQLITE_ENABLE_CURSOR_HINTS && SQLITE_DEBUG */
90400
90401#ifndef SQLITE_OMIT_VIRTUALTABLE
90402/*
90403** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
90404** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
90405** in memory obtained from sqlite3DbMalloc).
90406*/
90407SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
90408 if( pVtab->zErrMsg ){
90409 sqlite3 *db = p->db;
90410 sqlite3DbFree(db, p->zErrMsg);
90411 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
90412 sqlite3_free(pVtab->zErrMsg);
90413 pVtab->zErrMsg = 0;
90414 }
90415}
90416#endif /* SQLITE_OMIT_VIRTUALTABLE */
90417
90418#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
90419
90420/*
90421** If the second argument is not NULL, release any allocations associated
90422** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
90423** structure itself, using sqlite3DbFree().
90424**
90425** This function is used to free UnpackedRecord structures allocated by
90426** the vdbeUnpackRecord() function found in vdbeapi.c.
90427*/
90428static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
90429 assert( db!=0 )((void) (0));
90430 if( p ){
90431 int i;
90432 for(i=0; i<nField; i++){
90433 Mem *pMem = &p->aMem[i];
90434 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
90435 }
90436 sqlite3DbNNFreeNN(db, p);
90437 }
90438}
90439#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
90440
90441#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
90442/*
90443** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
90444** then cursor passed as the second argument should point to the row about
90445** to be update or deleted. If the application calls sqlite3_preupdate_old(),
90446** the required value will be read from the row the cursor points to.
90447*/
90448SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
90449 Vdbe *v, /* Vdbe pre-update hook is invoked by */
90450 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
90451 int op, /* SQLITE_INSERT, UPDATE or DELETE */
90452 const char *zDb, /* Database name */
90453 Table *pTab, /* Modified table */
90454 i64 iKey1, /* Initial key value */
90455 int iReg, /* Register for new.* record */
90456 int iBlobWrite
90457){
90458 sqlite3 *db = v->db;
90459 i64 iKey2;
90460 PreUpdate preupdate;
90461 const char *zTbl = pTab->zName;
90462 static const u8 fakeSortOrder = 0;
90463#ifdef SQLITE_DEBUG
90464 int nRealCol;
90465 if( pTab->tabFlags & TF_WithoutRowid0x00000080 ){
90466 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
90467 }else if( pTab->tabFlags & TF_HasVirtual0x00000020 ){
90468 nRealCol = pTab->nNVCol;
90469 }else{
90470 nRealCol = pTab->nCol;
90471 }
90472#endif
90473
90474 assert( db->pPreUpdate==0 )((void) (0));
90475 memset(&preupdate, 0, sizeof(PreUpdate));
90476 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)==0 ){
90477 iKey1 = iKey2 = 0;
90478 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
90479 }else{
90480 if( op==SQLITE_UPDATE23 ){
90481 iKey2 = v->aMem[iReg].u.i;
90482 }else{
90483 iKey2 = iKey1;
90484 }
90485 }
90486
90487 assert( pCsr!=0 )((void) (0));
90488 assert( pCsr->eCurType==CURTYPE_BTREE )((void) (0));
90489 assert( pCsr->nField==nRealCol((void) (0))
90490 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)((void) (0))
90491 )((void) (0));
90492
90493 preupdate.v = v;
90494 preupdate.pCsr = pCsr;
90495 preupdate.op = op;
90496 preupdate.iNewReg = iReg;
90497 preupdate.keyinfo.db = db;
90498 preupdate.keyinfo.enc = ENC(db)((db)->enc);
90499 preupdate.keyinfo.nKeyField = pTab->nCol;
90500 preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
90501 preupdate.iKey1 = iKey1;
90502 preupdate.iKey2 = iKey2;
90503 preupdate.pTab = pTab;
90504 preupdate.iBlobWrite = iBlobWrite;
90505
90506 db->pPreUpdate = &preupdate;
90507 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
90508 db->pPreUpdate = 0;
90509 sqlite3DbFree(db, preupdate.aRecord);
90510 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
90511 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
90512 if( preupdate.aNew ){
90513 int i;
90514 for(i=0; i<pCsr->nField; i++){
90515 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
90516 }
90517 sqlite3DbNNFreeNN(db, preupdate.aNew);
90518 }
90519 if( preupdate.apDflt ){
90520 int i;
90521 for(i=0; i<pTab->nCol; i++){
90522 sqlite3ValueFree(preupdate.apDflt[i]);
90523 }
90524 sqlite3DbFree(db, preupdate.apDflt);
90525 }
90526}
90527#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
90528
90529/************** End of vdbeaux.c *********************************************/
90530/************** Begin file vdbeapi.c *****************************************/
90531/*
90532** 2004 May 26
90533**
90534** The author disclaims copyright to this source code. In place of
90535** a legal notice, here is a blessing:
90536**
90537** May you do good and not evil.
90538** May you find forgiveness for yourself and forgive others.
90539** May you share freely, never taking more than you give.
90540**
90541*************************************************************************
90542**
90543** This file contains code use to implement APIs that are part of the
90544** VDBE.
90545*/
90546/* #include "sqliteInt.h" */
90547/* #include "vdbeInt.h" */
90548/* #include "opcodes.h" */
90549
90550#ifndef SQLITE_OMIT_DEPRECATED1
90551/*
90552** Return TRUE (non-zero) of the statement supplied as an argument needs
90553** to be recompiled. A statement needs to be recompiled whenever the
90554** execution environment changes in a way that would alter the program
90555** that sqlite3_prepare() generates. For example, if new functions or
90556** collating sequences are registered or if an authorizer function is
90557** added or changed.
90558*/
90559SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
90560 Vdbe *p = (Vdbe*)pStmt;
90561 return p==0 || p->expired;
90562}
90563#endif
90564
90565/*
90566** Check on a Vdbe to make sure it has not been finalized. Log
90567** an error and return true if it has been finalized (or is otherwise
90568** invalid). Return false if it is ok.
90569*/
90570static int vdbeSafety(Vdbe *p){
90571 if( p->db==0 ){
90572 sqlite3_log(SQLITE_MISUSE21, "API called with finalized prepared statement");
90573 return 1;
90574 }else{
90575 return 0;
90576 }
90577}
90578static int vdbeSafetyNotNull(Vdbe *p){
90579 if( p==0 ){
90580 sqlite3_log(SQLITE_MISUSE21, "API called with NULL prepared statement");
90581 return 1;
90582 }else{
90583 return vdbeSafety(p);
90584 }
90585}
90586
90587#ifndef SQLITE_OMIT_TRACE
90588/*
90589** Invoke the profile callback. This routine is only called if we already
90590** know that the profile callback is defined and needs to be invoked.
90591*/
90592static SQLITE_NOINLINE__attribute__((noinline)) void invokeProfileCallback(sqlite3 *db, Vdbe *p){
90593 sqlite3_int64 iNow;
90594 sqlite3_int64 iElapse;
90595 assert( p->startTime>0 )((void) (0));
90596 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 )((void) (0));
90597 assert( db->init.busy==0 )((void) (0));
90598 assert( p->zSql!=0 )((void) (0));
90599 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
90600 iElapse = (iNow - p->startTime)*1000000;
90601#ifndef SQLITE_OMIT_DEPRECATED1
90602 if( db->xProfile ){
90603 db->xProfile(db->pProfileArg, p->zSql, iElapse);
90604 }
90605#endif
90606 if( db->mTrace & SQLITE_TRACE_PROFILE0x02 ){
90607 db->trace.xV2(SQLITE_TRACE_PROFILE0x02, db->pTraceArg, p, (void*)&iElapse);
90608 }
90609 p->startTime = 0;
90610}
90611/*
90612** The checkProfileCallback(DB,P) macro checks to see if a profile callback
90613** is needed, and it invokes the callback if it is needed.
90614*/
90615# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } \
90616 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
90617#else
90618# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } /*no-op*/
90619#endif
90620
90621/*
90622** The following routine destroys a virtual machine that is created by
90623** the sqlite3_compile() routine. The integer returned is an SQLITE_
90624** success/failure code that describes the result of executing the virtual
90625** machine.
90626**
90627** This routine sets the error code and string returned by
90628** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
90629*/
90630SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
90631 int rc;
90632 if( pStmt==0 ){
90633 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
90634 ** pointer is a harmless no-op. */
90635 rc = SQLITE_OK0;
90636 }else{
90637 Vdbe *v = (Vdbe*)pStmt;
90638 sqlite3 *db = v->db;
90639 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(90639);
90640 sqlite3_mutex_enter(db->mutex);
90641 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
90642 assert( v->eVdbeState>=VDBE_READY_STATE )((void) (0));
90643 rc = sqlite3VdbeReset(v);
90644 sqlite3VdbeDelete(v);
90645 rc = sqlite3ApiExit(db, rc);
90646 sqlite3LeaveMutexAndCloseZombie(db);
90647 }
90648 return rc;
90649}
90650
90651/*
90652** Terminate the current execution of an SQL statement and reset it
90653** back to its starting state so that it can be reused. A success code from
90654** the prior execution is returned.
90655**
90656** This routine sets the error code and string returned by
90657** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
90658*/
90659SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
90660 int rc;
90661 if( pStmt==0 ){
90662 rc = SQLITE_OK0;
90663 }else{
90664 Vdbe *v = (Vdbe*)pStmt;
90665 sqlite3 *db = v->db;
90666 sqlite3_mutex_enter(db->mutex);
90667 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
90668 rc = sqlite3VdbeReset(v);
90669 sqlite3VdbeRewind(v);
90670 assert( (rc & (db->errMask))==rc )((void) (0));
90671 rc = sqlite3ApiExit(db, rc);
90672 sqlite3_mutex_leave(db->mutex);
90673 }
90674 return rc;
90675}
90676
90677/*
90678** Set all the parameters in the compiled SQL statement to NULL.
90679*/
90680SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
90681 int i;
90682 int rc = SQLITE_OK0;
90683 Vdbe *p = (Vdbe*)pStmt;
90684#if SQLITE_THREADSAFE2
90685 sqlite3_mutex *mutex;
90686#endif
90687#ifdef SQLITE_ENABLE_API_ARMOR1
90688 if( pStmt==0 ){
90689 return SQLITE_MISUSE_BKPTsqlite3MisuseError(90689);
90690 }
90691#endif
90692#if SQLITE_THREADSAFE2
90693 mutex = p->db->mutex;
90694#endif
90695 sqlite3_mutex_enter(mutex);
90696 for(i=0; i<p->nVar; i++){
90697 sqlite3VdbeMemRelease(&p->aVar[i]);
90698 p->aVar[i].flags = MEM_Null0x0001;
90699 }
90700 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
90701 if( p->expmask ){
90702 p->expired = 1;
90703 }
90704 sqlite3_mutex_leave(mutex);
90705 return rc;
90706}
90707
90708
90709/**************************** sqlite3_value_ *******************************
90710** The following routines extract information from a Mem or sqlite3_value
90711** structure.
90712*/
90713SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
90714 Mem *p = (Mem*)pVal;
90715 if( p->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
90716 if( ExpandBlob(p)(((p)->flags&0x0400)?sqlite3VdbeMemExpandBlob(p):0)!=SQLITE_OK0 ){
90717 assert( p->flags==MEM_Null && p->z==0 )((void) (0));
90718 return 0;
90719 }
90720 p->flags |= MEM_Blob0x0010;
90721 return p->n ? p->z : 0;
90722 }else{
90723 return sqlite3_value_text(pVal);
90724 }
90725}
90726SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
90727 return sqlite3ValueBytes(pVal, SQLITE_UTF81);
90728}
90729SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
90730 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE2);
90731}
90732SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
90733 return sqlite3VdbeRealValue((Mem*)pVal);
90734}
90735SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
90736 return (int)sqlite3VdbeIntValue((Mem*)pVal);
90737}
90738SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
90739 return sqlite3VdbeIntValue((Mem*)pVal);
90740}
90741SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
90742 Mem *pMem = (Mem*)pVal;
90743 return ((pMem->flags & MEM_Subtype0x0800) ? pMem->eSubtype : 0);
90744}
90745SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
90746 Mem *p = (Mem*)pVal;
90747 if( (p->flags&(MEM_TypeMask0x0dbf|MEM_Term0x0200|MEM_Subtype0x0800)) ==
90748 (MEM_Null0x0001|MEM_Term0x0200|MEM_Subtype0x0800)
90749 && zPType!=0
90750 && p->eSubtype=='p'
90751 && strcmp(p->u.zPType, zPType)==0
90752 ){
90753 return (void*)p->z;
90754 }else{
90755 return 0;
90756 }
90757}
90758SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
90759 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF81);
90760}
90761#ifndef SQLITE_OMIT_UTF16
90762SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
90763 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE2);
90764}
90765SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
90766 return sqlite3ValueText(pVal, SQLITE_UTF16BE3);
90767}
90768SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
90769 return sqlite3ValueText(pVal, SQLITE_UTF16LE2);
90770}
90771#endif /* SQLITE_OMIT_UTF16 */
90772/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
90773** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
90774** point number string BLOB NULL
90775*/
90776SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
90777 static const u8 aType[] = {
90778 SQLITE_BLOB4, /* 0x00 (not possible) */
90779 SQLITE_NULL5, /* 0x01 NULL */
90780 SQLITE_TEXT3, /* 0x02 TEXT */
90781 SQLITE_NULL5, /* 0x03 (not possible) */
90782 SQLITE_INTEGER1, /* 0x04 INTEGER */
90783 SQLITE_NULL5, /* 0x05 (not possible) */
90784 SQLITE_INTEGER1, /* 0x06 INTEGER + TEXT */
90785 SQLITE_NULL5, /* 0x07 (not possible) */
90786 SQLITE_FLOAT2, /* 0x08 FLOAT */
90787 SQLITE_NULL5, /* 0x09 (not possible) */
90788 SQLITE_FLOAT2, /* 0x0a FLOAT + TEXT */
90789 SQLITE_NULL5, /* 0x0b (not possible) */
90790 SQLITE_INTEGER1, /* 0x0c (not possible) */
90791 SQLITE_NULL5, /* 0x0d (not possible) */
90792 SQLITE_INTEGER1, /* 0x0e (not possible) */
90793 SQLITE_NULL5, /* 0x0f (not possible) */
90794 SQLITE_BLOB4, /* 0x10 BLOB */
90795 SQLITE_NULL5, /* 0x11 (not possible) */
90796 SQLITE_TEXT3, /* 0x12 (not possible) */
90797 SQLITE_NULL5, /* 0x13 (not possible) */
90798 SQLITE_INTEGER1, /* 0x14 INTEGER + BLOB */
90799 SQLITE_NULL5, /* 0x15 (not possible) */
90800 SQLITE_INTEGER1, /* 0x16 (not possible) */
90801 SQLITE_NULL5, /* 0x17 (not possible) */
90802 SQLITE_FLOAT2, /* 0x18 FLOAT + BLOB */
90803 SQLITE_NULL5, /* 0x19 (not possible) */
90804 SQLITE_FLOAT2, /* 0x1a (not possible) */
90805 SQLITE_NULL5, /* 0x1b (not possible) */
90806 SQLITE_INTEGER1, /* 0x1c (not possible) */
90807 SQLITE_NULL5, /* 0x1d (not possible) */
90808 SQLITE_INTEGER1, /* 0x1e (not possible) */
90809 SQLITE_NULL5, /* 0x1f (not possible) */
90810 SQLITE_FLOAT2, /* 0x20 INTREAL */
90811 SQLITE_NULL5, /* 0x21 (not possible) */
90812 SQLITE_FLOAT2, /* 0x22 INTREAL + TEXT */
90813 SQLITE_NULL5, /* 0x23 (not possible) */
90814 SQLITE_FLOAT2, /* 0x24 (not possible) */
90815 SQLITE_NULL5, /* 0x25 (not possible) */
90816 SQLITE_FLOAT2, /* 0x26 (not possible) */
90817 SQLITE_NULL5, /* 0x27 (not possible) */
90818 SQLITE_FLOAT2, /* 0x28 (not possible) */
90819 SQLITE_NULL5, /* 0x29 (not possible) */
90820 SQLITE_FLOAT2, /* 0x2a (not possible) */
90821 SQLITE_NULL5, /* 0x2b (not possible) */
90822 SQLITE_FLOAT2, /* 0x2c (not possible) */
90823 SQLITE_NULL5, /* 0x2d (not possible) */
90824 SQLITE_FLOAT2, /* 0x2e (not possible) */
90825 SQLITE_NULL5, /* 0x2f (not possible) */
90826 SQLITE_BLOB4, /* 0x30 (not possible) */
90827 SQLITE_NULL5, /* 0x31 (not possible) */
90828 SQLITE_TEXT3, /* 0x32 (not possible) */
90829 SQLITE_NULL5, /* 0x33 (not possible) */
90830 SQLITE_FLOAT2, /* 0x34 (not possible) */
90831 SQLITE_NULL5, /* 0x35 (not possible) */
90832 SQLITE_FLOAT2, /* 0x36 (not possible) */
90833 SQLITE_NULL5, /* 0x37 (not possible) */
90834 SQLITE_FLOAT2, /* 0x38 (not possible) */
90835 SQLITE_NULL5, /* 0x39 (not possible) */
90836 SQLITE_FLOAT2, /* 0x3a (not possible) */
90837 SQLITE_NULL5, /* 0x3b (not possible) */
90838 SQLITE_FLOAT2, /* 0x3c (not possible) */
90839 SQLITE_NULL5, /* 0x3d (not possible) */
90840 SQLITE_FLOAT2, /* 0x3e (not possible) */
90841 SQLITE_NULL5, /* 0x3f (not possible) */
90842 };
90843#ifdef SQLITE_DEBUG
90844 {
90845 int eType = SQLITE_BLOB4;
90846 if( pVal->flags & MEM_Null0x0001 ){
90847 eType = SQLITE_NULL5;
90848 }else if( pVal->flags & (MEM_Real0x0008|MEM_IntReal0x0020) ){
90849 eType = SQLITE_FLOAT2;
90850 }else if( pVal->flags & MEM_Int0x0004 ){
90851 eType = SQLITE_INTEGER1;
90852 }else if( pVal->flags & MEM_Str0x0002 ){
90853 eType = SQLITE_TEXT3;
90854 }
90855 assert( eType == aType[pVal->flags&MEM_AffMask] )((void) (0));
90856 }
90857#endif
90858 return aType[pVal->flags&MEM_AffMask0x003f];
90859}
90860SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
90861 return pVal->enc;
90862}
90863
90864/* Return true if a parameter to xUpdate represents an unchanged column */
90865SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
90866 return (pVal->flags&(MEM_Null0x0001|MEM_Zero0x0400))==(MEM_Null0x0001|MEM_Zero0x0400);
90867}
90868
90869/* Return true if a parameter value originated from an sqlite3_bind() */
90870SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
90871 return (pVal->flags&MEM_FromBind0x0040)!=0;
90872}
90873
90874/* Make a copy of an sqlite3_value object
90875*/
90876SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
90877 sqlite3_value *pNew;
90878 if( pOrig==0 ) return 0;
90879 pNew = sqlite3_malloc( sizeof(*pNew) );
90880 if( pNew==0 ) return 0;
90881 memset(pNew, 0, sizeof(*pNew));
90882 memcpy(pNew, pOrig, MEMCELLSIZE__builtin_offsetof(Mem, db));
90883 pNew->flags &= ~MEM_Dyn0x1000;
90884 pNew->db = 0;
90885 if( pNew->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
90886 pNew->flags &= ~(MEM_Static0x2000|MEM_Dyn0x1000);
90887 pNew->flags |= MEM_Ephem0x4000;
90888 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK0 ){
90889 sqlite3ValueFree(pNew);
90890 pNew = 0;
90891 }
90892 }else if( pNew->flags & MEM_Null0x0001 ){
90893 /* Do not duplicate pointer values */
90894 pNew->flags &= ~(MEM_Term0x0200|MEM_Subtype0x0800);
90895 }
90896 return pNew;
90897}
90898
90899/* Destroy an sqlite3_value object previously obtained from
90900** sqlite3_value_dup().
90901*/
90902SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
90903 sqlite3ValueFree(pOld);
90904}
90905
90906
90907/**************************** sqlite3_result_ *******************************
90908** The following routines are used by user-defined functions to specify
90909** the function result.
90910**
90911** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
90912** result as a string or blob. Appropriate errors are set if the string/blob
90913** is too big or if an OOM occurs.
90914**
90915** The invokeValueDestructor(P,X) routine invokes destructor function X()
90916** on value P if P is not going to be used and need to be destroyed.
90917*/
90918static void setResultStrOrError(
90919 sqlite3_context *pCtx, /* Function context */
90920 const char *z, /* String pointer */
90921 int n, /* Bytes in string, or negative */
90922 u8 enc, /* Encoding of z. 0 for BLOBs */
90923 void (*xDel)(void*) /* Destructor function */
90924){
90925 Mem *pOut = pCtx->pOut;
90926 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel);
90927 if( rc ){
90928 if( rc==SQLITE_TOOBIG18 ){
90929 sqlite3_result_error_toobig(pCtx);
90930 }else{
90931 /* The only errors possible from sqlite3VdbeMemSetStr are
90932 ** SQLITE_TOOBIG and SQLITE_NOMEM */
90933 assert( rc==SQLITE_NOMEM )((void) (0));
90934 sqlite3_result_error_nomem(pCtx);
90935 }
90936 return;
90937 }
90938 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
90939 if( sqlite3VdbeMemTooBig(pOut) ){
90940 sqlite3_result_error_toobig(pCtx);
90941 }
90942}
90943static int invokeValueDestructor(
90944 const void *p, /* Value to destroy */
90945 void (*xDel)(void*), /* The destructor */
90946 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */
90947){
90948 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
90949 if( xDel==0 ){
90950 /* noop */
90951 }else if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
90952 /* noop */
90953 }else{
90954 xDel((void*)p);
90955 }
90956#ifdef SQLITE_ENABLE_API_ARMOR1
90957 if( pCtx!=0 ){
90958 sqlite3_result_error_toobig(pCtx);
90959 }
90960#else
90961 assert( pCtx!=0 )((void) (0));
90962 sqlite3_result_error_toobig(pCtx);
90963#endif
90964 return SQLITE_TOOBIG18;
90965}
90966SQLITE_API void sqlite3_result_blob(
90967 sqlite3_context *pCtx,
90968 const void *z,
90969 int n,
90970 void (*xDel)(void *)
90971){
90972#ifdef SQLITE_ENABLE_API_ARMOR1
90973 if( pCtx==0 || n<0 ){
90974 invokeValueDestructor(z, xDel, pCtx);
90975 return;
90976 }
90977#endif
90978 assert( n>=0 )((void) (0));
90979 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
90980 setResultStrOrError(pCtx, z, n, 0, xDel);
90981}
90982SQLITE_API void sqlite3_result_blob64(
90983 sqlite3_context *pCtx,
90984 const void *z,
90985 sqlite3_uint64 n,
90986 void (*xDel)(void *)
90987){
90988 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
90989#ifdef SQLITE_ENABLE_API_ARMOR1
90990 if( pCtx==0 ){
90991 invokeValueDestructor(z, xDel, 0);
90992 return;
90993 }
90994#endif
90995 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
90996 if( n>0x7fffffff ){
90997 (void)invokeValueDestructor(z, xDel, pCtx);
90998 }else{
90999 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
91000 }
91001}
91002SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
91003#ifdef SQLITE_ENABLE_API_ARMOR1
91004 if( pCtx==0 ) return;
91005#endif
91006 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91007 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
91008}
91009SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
91010#ifdef SQLITE_ENABLE_API_ARMOR1
91011 if( pCtx==0 ) return;
91012#endif
91013 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91014 pCtx->isError = SQLITE_ERROR1;
91015 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
91016}
91017#ifndef SQLITE_OMIT_UTF16
91018SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
91019#ifdef SQLITE_ENABLE_API_ARMOR1
91020 if( pCtx==0 ) return;
91021#endif
91022 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91023 pCtx->isError = SQLITE_ERROR1;
91024 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
91025}
91026#endif
91027SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
91028#ifdef SQLITE_ENABLE_API_ARMOR1
91029 if( pCtx==0 ) return;
91030#endif
91031 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91032 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
91033}
91034SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
91035#ifdef SQLITE_ENABLE_API_ARMOR1
91036 if( pCtx==0 ) return;
91037#endif
91038 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91039 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
91040}
91041SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
91042#ifdef SQLITE_ENABLE_API_ARMOR1
91043 if( pCtx==0 ) return;
91044#endif
91045 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91046 sqlite3VdbeMemSetNull(pCtx->pOut);
91047}
91048SQLITE_API void sqlite3_result_pointer(
91049 sqlite3_context *pCtx,
91050 void *pPtr,
91051 const char *zPType,
91052 void (*xDestructor)(void*)
91053){
91054 Mem *pOut;
91055#ifdef SQLITE_ENABLE_API_ARMOR1
91056 if( pCtx==0 ){
91057 invokeValueDestructor(pPtr, xDestructor, 0);
91058 return;
91059 }
91060#endif
91061 pOut = pCtx->pOut;
91062 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
91063 sqlite3VdbeMemRelease(pOut);
91064 pOut->flags = MEM_Null0x0001;
91065 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
91066}
91067SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
91068 Mem *pOut;
91069#ifdef SQLITE_ENABLE_API_ARMOR1
91070 if( pCtx==0 ) return;
91071#endif
91072#if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
91073 if( pCtx->pFunc!=0
91074 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE0x001000000)==0
91075 ){
91076 char zErr[200];
91077 sqlite3_snprintf(sizeof(zErr), zErr,
91078 "misuse of sqlite3_result_subtype() by %s()",
91079 pCtx->pFunc->zName);
91080 sqlite3_result_error(pCtx, zErr, -1);
91081 return;
91082 }
91083#endif /* SQLITE_STRICT_SUBTYPE */
91084 pOut = pCtx->pOut;
91085 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
91086 pOut->eSubtype = eSubtype & 0xff;
91087 pOut->flags |= MEM_Subtype0x0800;
91088}
91089SQLITE_API void sqlite3_result_text(
91090 sqlite3_context *pCtx,
91091 const char *z,
91092 int n,
91093 void (*xDel)(void *)
91094){
91095#ifdef SQLITE_ENABLE_API_ARMOR1
91096 if( pCtx==0 ){
91097 invokeValueDestructor(z, xDel, 0);
91098 return;
91099 }
91100#endif
91101 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91102 setResultStrOrError(pCtx, z, n, SQLITE_UTF81, xDel);
91103}
91104SQLITE_API void sqlite3_result_text64(
91105 sqlite3_context *pCtx,
91106 const char *z,
91107 sqlite3_uint64 n,
91108 void (*xDel)(void *),
91109 unsigned char enc
91110){
91111#ifdef SQLITE_ENABLE_API_ARMOR1
91112 if( pCtx==0 ){
91113 invokeValueDestructor(z, xDel, 0);
91114 return;
91115 }
91116#endif
91117 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91118 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
91119 if( enc!=SQLITE_UTF81 ){
91120 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
91121 n &= ~(u64)1;
91122 }
91123 if( n>0x7fffffff ){
91124 (void)invokeValueDestructor(z, xDel, pCtx);
91125 }else{
91126 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
91127 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
91128 }
91129}
91130#ifndef SQLITE_OMIT_UTF16
91131SQLITE_API void sqlite3_result_text16(
91132 sqlite3_context *pCtx,
91133 const void *z,
91134 int n,
91135 void (*xDel)(void *)
91136){
91137 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91138 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE2, xDel);
91139}
91140SQLITE_API void sqlite3_result_text16be(
91141 sqlite3_context *pCtx,
91142 const void *z,
91143 int n,
91144 void (*xDel)(void *)
91145){
91146 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91147 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE3, xDel);
91148}
91149SQLITE_API void sqlite3_result_text16le(
91150 sqlite3_context *pCtx,
91151 const void *z,
91152 int n,
91153 void (*xDel)(void *)
91154){
91155 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91156 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE2, xDel);
91157}
91158#endif /* SQLITE_OMIT_UTF16 */
91159SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
91160 Mem *pOut;
91161
91162#ifdef SQLITE_ENABLE_API_ARMOR1
91163 if( pCtx==0 ) return;
91164 if( pValue==0 ){
91165 sqlite3_result_null(pCtx);
91166 return;
91167 }
91168#endif
91169 pOut = pCtx->pOut;
91170 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91171 sqlite3VdbeMemCopy(pOut, pValue);
91172 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
91173 if( sqlite3VdbeMemTooBig(pOut) ){
91174 sqlite3_result_error_toobig(pCtx);
91175 }
91176}
91177SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
91178 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0);
91179}
91180SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
91181 Mem *pOut;
91182
91183#ifdef SQLITE_ENABLE_API_ARMOR1
91184 if( pCtx==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(91184);
91185#endif
91186 pOut = pCtx->pOut;
91187 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
91188 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
91189 sqlite3_result_error_toobig(pCtx);
91190 return SQLITE_TOOBIG18;
91191 }
91192#ifndef SQLITE_OMIT_INCRBLOB
91193 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
91194 return SQLITE_OK0;
91195#else
91196 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
91197#endif
91198}
91199SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
91200#ifdef SQLITE_ENABLE_API_ARMOR1
91201 if( pCtx==0 ) return;
91202#endif
91203 pCtx->isError = errCode ? errCode : -1;
91204#ifdef SQLITE_DEBUG
91205 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
91206#endif
91207 if( pCtx->pOut->flags & MEM_Null0x0001 ){
91208 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF81,
91209 SQLITE_STATIC((sqlite3_destructor_type)0));
91210 }
91211}
91212
91213/* Force an SQLITE_TOOBIG error. */
91214SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
91215#ifdef SQLITE_ENABLE_API_ARMOR1
91216 if( pCtx==0 ) return;
91217#endif
91218 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91219 pCtx->isError = SQLITE_TOOBIG18;
91220 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
91221 SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
91222}
91223
91224/* An SQLITE_NOMEM error. */
91225SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
91226#ifdef SQLITE_ENABLE_API_ARMOR1
91227 if( pCtx==0 ) return;
91228#endif
91229 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91230 sqlite3VdbeMemSetNull(pCtx->pOut);
91231 pCtx->isError = SQLITE_NOMEM_BKPT7;
91232 sqlite3OomFault(pCtx->pOut->db);
91233}
91234
91235#ifndef SQLITE_UNTESTABLE
91236/* Force the INT64 value currently stored as the result to be
91237** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
91238** test-control.
91239*/
91240SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context *pCtx){
91241 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91242 if( pCtx->pOut->flags & MEM_Int0x0004 ){
91243 pCtx->pOut->flags &= ~MEM_Int0x0004;
91244 pCtx->pOut->flags |= MEM_IntReal0x0020;
91245 }
91246}
91247#endif
91248
91249
91250/*
91251** This function is called after a transaction has been committed. It
91252** invokes callbacks registered with sqlite3_wal_hook() as required.
91253*/
91254static int doWalCallbacks(sqlite3 *db){
91255 int rc = SQLITE_OK0;
91256#ifndef SQLITE_OMIT_WAL
91257 int i;
91258 for(i=0; i<db->nDb; i++){
91259 Btree *pBt = db->aDb[i].pBt;
91260 if( pBt ){
91261 int nEntry;
91262 sqlite3BtreeEnter(pBt);
91263 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
91264 sqlite3BtreeLeave(pBt);
91265 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK0 ){
91266 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
91267 }
91268 }
91269 }
91270#endif
91271 return rc;
91272}
91273
91274
91275/*
91276** Execute the statement pStmt, either until a row of data is ready, the
91277** statement is completely executed or an error occurs.
91278**
91279** This routine implements the bulk of the logic behind the sqlite_step()
91280** API. The only thing omitted is the automatic recompile if a
91281** schema change has occurred. That detail is handled by the
91282** outer sqlite3_step() wrapper procedure.
91283*/
91284static int sqlite3Step(Vdbe *p){
91285 sqlite3 *db;
91286 int rc;
91287
91288 assert(p)((void) (0));
91289 db = p->db;
91290 if( p->eVdbeState!=VDBE_RUN_STATE2 ){
91291 restart_step:
91292 if( p->eVdbeState==VDBE_READY_STATE1 ){
91293 if( p->expired ){
91294 p->rc = SQLITE_SCHEMA17;
91295 rc = SQLITE_ERROR1;
91296 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ){
91297 /* If this statement was prepared using saved SQL and an
91298 ** error has occurred, then return the error code in p->rc to the
91299 ** caller. Set the error code in the database handle to the same
91300 ** value.
91301 */
91302 rc = sqlite3VdbeTransferError(p);
91303 }
91304 goto end_of_step;
91305 }
91306
91307 /* If there are no other statements currently running, then
91308 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
91309 ** from interrupting a statement that has not yet started.
91310 */
91311 if( db->nVdbeActive==0 ){
91312 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
91313 }
91314
91315 assert( db->nVdbeWrite>0 || db->autoCommit==0((void) (0))
91316 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)((void) (0))
91317 )((void) (0));
91318
91319#ifndef SQLITE_OMIT_TRACE
91320 if( (db->mTrace & (SQLITE_TRACE_PROFILE0x02|SQLITE_TRACE_XPROFILE0))!=0
91321 && !db->init.busy && p->zSql ){
91322 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
91323 }else{
91324 assert( p->startTime==0 )((void) (0));
91325 }
91326#endif
91327
91328 db->nVdbeActive++;
91329 if( p->readOnly==0 ) db->nVdbeWrite++;
91330 if( p->bIsReader ) db->nVdbeRead++;
91331 p->pc = 0;
91332 p->eVdbeState = VDBE_RUN_STATE2;
91333 }else
91334
91335 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE)(p->eVdbeState==3) ){
91336 /* We used to require that sqlite3_reset() be called before retrying
91337 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
91338 ** with version 3.7.0, we changed this so that sqlite3_reset() would
91339 ** be called automatically instead of throwing the SQLITE_MISUSE error.
91340 ** This "automatic-reset" change is not technically an incompatibility,
91341 ** since any application that receives an SQLITE_MISUSE is broken by
91342 ** definition.
91343 **
91344 ** Nevertheless, some published applications that were originally written
91345 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
91346 ** returns, and those were broken by the automatic-reset change. As a
91347 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
91348 ** legacy behavior of returning SQLITE_MISUSE for cases where the
91349 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
91350 ** or SQLITE_BUSY error.
91351 */
91352#ifdef SQLITE_OMIT_AUTORESET1
91353 if( (rc = p->rc&0xff)==SQLITE_BUSY5 || rc==SQLITE_LOCKED6 ){
91354 sqlite3_reset((sqlite3_stmt*)p);
91355 }else{
91356 return SQLITE_MISUSE_BKPTsqlite3MisuseError(91356);
91357 }
91358#else
91359 sqlite3_reset((sqlite3_stmt*)p);
91360#endif
91361 assert( p->eVdbeState==VDBE_READY_STATE )((void) (0));
91362 goto restart_step;
91363 }
91364 }
91365
91366#ifdef SQLITE_DEBUG
91367 p->rcApp = SQLITE_OK0;
91368#endif
91369#ifndef SQLITE_OMIT_EXPLAIN
91370 if( p->explain ){
91371 rc = sqlite3VdbeList(p);
91372 }else
91373#endif /* SQLITE_OMIT_EXPLAIN */
91374 {
91375 db->nVdbeExec++;
91376 rc = sqlite3VdbeExec(p);
91377 db->nVdbeExec--;
91378 }
91379
91380 if( rc==SQLITE_ROW100 ){
91381 assert( p->rc==SQLITE_OK )((void) (0));
91382 assert( db->mallocFailed==0 )((void) (0));
91383 db->errCode = SQLITE_ROW100;
91384 return SQLITE_ROW100;
91385 }else{
91386#ifndef SQLITE_OMIT_TRACE
91387 /* If the statement completed successfully, invoke the profile callback */
91388 checkProfileCallback(db, p)if( ((p)->startTime)>0 ){ invokeProfileCallback(db,p); };
91389#endif
91390 p->pResultRow = 0;
91391 if( rc==SQLITE_DONE101 && db->autoCommit ){
91392 assert( p->rc==SQLITE_OK )((void) (0));
91393 p->rc = doWalCallbacks(db);
91394 if( p->rc!=SQLITE_OK0 ){
91395 rc = SQLITE_ERROR1;
91396 }
91397 }else if( rc!=SQLITE_DONE101 && (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ){
91398 /* If this statement was prepared using saved SQL and an
91399 ** error has occurred, then return the error code in p->rc to the
91400 ** caller. Set the error code in the database handle to the same value.
91401 */
91402 rc = sqlite3VdbeTransferError(p);
91403 }
91404 }
91405
91406 db->errCode = rc;
91407 if( SQLITE_NOMEM7==sqlite3ApiExit(p->db, p->rc) ){
91408 p->rc = SQLITE_NOMEM_BKPT7;
91409 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0 ) rc = p->rc;
91410 }
91411end_of_step:
91412 /* There are only a limited number of result codes allowed from the
91413 ** statements prepared using the legacy sqlite3_prepare() interface */
91414 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0((void) (0))
91415 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR((void) (0))
91416 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE((void) (0))
91417 )((void) (0));
91418 return (rc&db->errMask);
91419}
91420
91421/*
91422** This is the top-level implementation of sqlite3_step(). Call
91423** sqlite3Step() to do most of the work. If a schema error occurs,
91424** call sqlite3Reprepare() and try again.
91425*/
91426SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
91427 int rc = SQLITE_OK0; /* Result from sqlite3Step() */
91428 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
91429 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
91430 sqlite3 *db; /* The database connection */
91431
91432 if( vdbeSafetyNotNull(v) ){
91433 return SQLITE_MISUSE_BKPTsqlite3MisuseError(91433);
91434 }
91435 db = v->db;
91436 sqlite3_mutex_enter(db->mutex);
91437 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA17
91438 && cnt++ < SQLITE_MAX_SCHEMA_RETRY50 ){
91439 int savedPc = v->pc;
91440 rc = sqlite3Reprepare(v);
91441 if( rc!=SQLITE_OK0 ){
91442 /* This case occurs after failing to recompile an sql statement.
91443 ** The error message from the SQL compiler has already been loaded
91444 ** into the database handle. This block copies the error message
91445 ** from the database handle into the statement and sets the statement
91446 ** program counter to 0 to ensure that when the statement is
91447 ** finalized or reset the parser error message is available via
91448 ** sqlite3_errmsg() and sqlite3_errcode().
91449 */
91450 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
91451 sqlite3DbFree(db, v->zErrMsg);
91452 if( !db->mallocFailed ){
91453 v->zErrMsg = sqlite3DbStrDup(db, zErr);
91454 v->rc = rc = sqlite3ApiExit(db, rc);
91455 } else {
91456 v->zErrMsg = 0;
91457 v->rc = rc = SQLITE_NOMEM_BKPT7;
91458 }
91459 break;
91460 }
91461 sqlite3_reset(pStmt);
91462 if( savedPc>=0 ){
91463 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and
91464 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has
91465 ** already been done once on a prior invocation that failed due to
91466 ** SQLITE_SCHEMA. tag-20220401a */
91467 v->minWriteFileFormat = 254;
91468 }
91469 assert( v->expired==0 )((void) (0));
91470 }
91471 sqlite3_mutex_leave(db->mutex);
91472 return rc;
91473}
91474
91475
91476/*
91477** Extract the user data from a sqlite3_context structure and return a
91478** pointer to it.
91479*/
91480SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
91481#ifdef SQLITE_ENABLE_API_ARMOR1
91482 if( p==0 ) return 0;
91483#endif
91484 assert( p && p->pFunc )((void) (0));
91485 return p->pFunc->pUserData;
91486}
91487
91488/*
91489** Extract the user data from a sqlite3_context structure and return a
91490** pointer to it.
91491**
91492** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
91493** returns a copy of the pointer to the database connection (the 1st
91494** parameter) of the sqlite3_create_function() and
91495** sqlite3_create_function16() routines that originally registered the
91496** application defined function.
91497*/
91498SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
91499#ifdef SQLITE_ENABLE_API_ARMOR1
91500 if( p==0 ) return 0;
91501#else
91502 assert( p && p->pOut )((void) (0));
91503#endif
91504 return p->pOut->db;
91505}
91506
91507/*
91508** If this routine is invoked from within an xColumn method of a virtual
91509** table, then it returns true if and only if the the call is during an
91510** UPDATE operation and the value of the column will not be modified
91511** by the UPDATE.
91512**
91513** If this routine is called from any context other than within the
91514** xColumn method of a virtual table, then the return value is meaningless
91515** and arbitrary.
91516**
91517** Virtual table implements might use this routine to optimize their
91518** performance by substituting a NULL result, or some other light-weight
91519** value, as a signal to the xUpdate routine that the column is unchanged.
91520*/
91521SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
91522#ifdef SQLITE_ENABLE_API_ARMOR1
91523 if( p==0 ) return 0;
91524#else
91525 assert( p )((void) (0));
91526#endif
91527 return sqlite3_value_nochange(p->pOut);
91528}
91529
91530/*
91531** The destructor function for a ValueList object. This needs to be
91532** a separate function, unknowable to the application, to ensure that
91533** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not
91534** preceded by activation of IN processing via sqlite3_vtab_int() do not
91535** try to access a fake ValueList object inserted by a hostile extension.
91536*/
91537SQLITE_PRIVATEstatic void sqlite3VdbeValueListFree(void *pToDelete){
91538 sqlite3_free(pToDelete);
91539}
91540
91541/*
91542** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
91543** sqlite3_vtab_in_next() (if bNext!=0).
91544*/
91545static int valueFromValueList(
91546 sqlite3_value *pVal, /* Pointer to the ValueList object */
91547 sqlite3_value **ppOut, /* Store the next value from the list here */
91548 int bNext /* 1 for _next(). 0 for _first() */
91549){
91550 int rc;
91551 ValueList *pRhs;
91552
91553 *ppOut = 0;
91554 if( pVal==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(91554);
91555 if( (pVal->flags & MEM_Dyn0x1000)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
91556 return SQLITE_ERROR1;
91557 }else{
91558 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==((void) (0))
91559 (MEM_Null|MEM_Term|MEM_Subtype) )((void) (0));
91560 assert( pVal->eSubtype=='p' )((void) (0));
91561 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 )((void) (0));
91562 pRhs = (ValueList*)pVal->z;
91563 }
91564 if( bNext ){
91565 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
91566 }else{
91567 int dummy = 0;
91568 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
91569 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) )((void) (0));
91570 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE101;
91571 }
91572 if( rc==SQLITE_OK0 ){
91573 u32 sz; /* Size of current row in bytes */
91574 Mem sMem; /* Raw content of current row */
91575 memset(&sMem, 0, sizeof(sMem));
91576 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
91577 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
91578 if( rc==SQLITE_OK0 ){
91579 u8 *zBuf = (u8*)sMem.z;
91580 u32 iSerial;
91581 sqlite3_value *pOut = pRhs->pOut;
91582 int iOff = 1 + getVarint32(&zBuf[1], iSerial)(u8)((*(&zBuf[1])<(u8)0x80)?((iSerial)=(u32)*(&zBuf
[1])),1:sqlite3GetVarint32((&zBuf[1]),(u32 *)&(iSerial
)))
;
91583 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut);
91584 pOut->enc = ENC(pOut->db)((pOut->db)->enc);
91585 if( (pOut->flags & MEM_Ephem0x4000)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
91586 rc = SQLITE_NOMEM7;
91587 }else{
91588 *ppOut = pOut;
91589 }
91590 }
91591 sqlite3VdbeMemRelease(&sMem);
91592 }
91593 return rc;
91594}
91595
91596/*
91597** Set the iterator value pVal to point to the first value in the set.
91598** Set (*ppOut) to point to this value before returning.
91599*/
91600SQLITE_API int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){
91601 return valueFromValueList(pVal, ppOut, 0);
91602}
91603
91604/*
91605** Set the iterator value pVal to point to the next value in the set.
91606** Set (*ppOut) to point to this value before returning.
91607*/
91608SQLITE_API int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){
91609 return valueFromValueList(pVal, ppOut, 1);
91610}
91611
91612/*
91613** Return the current time for a statement. If the current time
91614** is requested more than once within the same run of a single prepared
91615** statement, the exact same time is returned for each invocation regardless
91616** of the amount of time that elapses between invocations. In other words,
91617** the time returned is always the time of the first call.
91618*/
91619SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
91620 int rc;
91621#ifndef SQLITE_ENABLE_STAT4
91622 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
91623 assert( p->pVdbe!=0 )((void) (0));
91624#else
91625 sqlite3_int64 iTime = 0;
91626 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
91627#endif
91628 if( *piTime==0 ){
91629 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
91630 if( rc ) *piTime = 0;
91631 }
91632 return *piTime;
91633}
91634
91635/*
91636** Create a new aggregate context for p and return a pointer to
91637** its pMem->z element.
91638*/
91639static SQLITE_NOINLINE__attribute__((noinline)) void *createAggContext(sqlite3_context *p, int nByte){
91640 Mem *pMem = p->pMem;
91641 assert( (pMem->flags & MEM_Agg)==0 )((void) (0));
91642 if( nByte<=0 ){
91643 sqlite3VdbeMemSetNull(pMem);
91644 pMem->z = 0;
91645 }else{
91646 sqlite3VdbeMemClearAndResize(pMem, nByte);
91647 pMem->flags = MEM_Agg0x8000;
91648 pMem->u.pDef = p->pFunc;
91649 if( pMem->z ){
91650 memset(pMem->z, 0, nByte);
91651 }
91652 }
91653 return (void*)pMem->z;
91654}
91655
91656/*
91657** Allocate or return the aggregate context for a user function. A new
91658** context is allocated on the first call. Subsequent calls return the
91659** same context that was returned on prior calls.
91660*/
91661SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
91662 assert( p && p->pFunc && p->pFunc->xFinalize )((void) (0));
91663 assert( sqlite3_mutex_held(p->pOut->db->mutex) )((void) (0));
91664 testcase( nByte<0 );
91665 if( (p->pMem->flags & MEM_Agg0x8000)==0 ){
91666 return createAggContext(p, nByte);
91667 }else{
91668 return (void*)p->pMem->z;
91669 }
91670}
91671
91672/*
91673** Return the auxiliary data pointer, if any, for the iArg'th argument to
91674** the user-function defined by pCtx.
91675**
91676** The left-most argument is 0.
91677**
91678** Undocumented behavior: If iArg is negative then access a cache of
91679** auxiliary data pointers that is available to all functions within a
91680** single prepared statement. The iArg values must match.
91681*/
91682SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
91683 AuxData *pAuxData;
91684
91685#ifdef SQLITE_ENABLE_API_ARMOR1
91686 if( pCtx==0 ) return 0;
91687#endif
91688 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91689#if SQLITE_ENABLE_STAT4
91690 if( pCtx->pVdbe==0 ) return 0;
91691#else
91692 assert( pCtx->pVdbe!=0 )((void) (0));
91693#endif
91694 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
91695 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
91696 return pAuxData->pAux;
91697 }
91698 }
91699 return 0;
91700}
91701
91702/*
91703** Set the auxiliary data pointer and delete function, for the iArg'th
91704** argument to the user-function defined by pCtx. Any previous value is
91705** deleted by calling the delete function specified when it was set.
91706**
91707** The left-most argument is 0.
91708**
91709** Undocumented behavior: If iArg is negative then make the data available
91710** to all functions within the current prepared statement using iArg as an
91711** access code.
91712*/
91713SQLITE_API void sqlite3_set_auxdata(
91714 sqlite3_context *pCtx,
91715 int iArg,
91716 void *pAux,
91717 void (*xDelete)(void*)
91718){
91719 AuxData *pAuxData;
91720 Vdbe *pVdbe;
91721
91722#ifdef SQLITE_ENABLE_API_ARMOR1
91723 if( pCtx==0 ) return;
91724#endif
91725 pVdbe= pCtx->pVdbe;
91726 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
91727#ifdef SQLITE_ENABLE_STAT4
91728 if( pVdbe==0 ) goto failed;
91729#else
91730 assert( pVdbe!=0 )((void) (0));
91731#endif
91732
91733 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
91734 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
91735 break;
91736 }
91737 }
91738 if( pAuxData==0 ){
91739 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
91740 if( !pAuxData ) goto failed;
91741 pAuxData->iAuxOp = pCtx->iOp;
91742 pAuxData->iAuxArg = iArg;
91743 pAuxData->pNextAux = pVdbe->pAuxData;
91744 pVdbe->pAuxData = pAuxData;
91745 if( pCtx->isError==0 ) pCtx->isError = -1;
91746 }else if( pAuxData->xDeleteAux ){
91747 pAuxData->xDeleteAux(pAuxData->pAux);
91748 }
91749
91750 pAuxData->pAux = pAux;
91751 pAuxData->xDeleteAux = xDelete;
91752 return;
91753
91754failed:
91755 if( xDelete ){
91756 xDelete(pAux);
91757 }
91758}
91759
91760#ifndef SQLITE_OMIT_DEPRECATED1
91761/*
91762** Return the number of times the Step function of an aggregate has been
91763** called.
91764**
91765** This function is deprecated. Do not use it for new code. It is
91766** provide only to avoid breaking legacy code. New aggregate function
91767** implementations should keep their own counts within their aggregate
91768** context.
91769*/
91770SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
91771 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize )((void) (0));
91772 return p->pMem->n;
91773}
91774#endif
91775
91776/*
91777** Return the number of columns in the result set for the statement pStmt.
91778*/
91779SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
91780 Vdbe *pVm = (Vdbe *)pStmt;
91781 if( pVm==0 ) return 0;
91782 return pVm->nResColumn;
91783}
91784
91785/*
91786** Return the number of values available from the current row of the
91787** currently executing statement pStmt.
91788*/
91789SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
91790 Vdbe *pVm = (Vdbe *)pStmt;
91791 if( pVm==0 || pVm->pResultRow==0 ) return 0;
91792 return pVm->nResColumn;
91793}
91794
91795/*
91796** Return a pointer to static memory containing an SQL NULL value.
91797*/
91798static const Mem *columnNullValue(void){
91799 /* Even though the Mem structure contains an element
91800 ** of type i64, on certain architectures (x86) with certain compiler
91801 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
91802 ** instead of an 8-byte one. This all works fine, except that when
91803 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
91804 ** that a Mem structure is located on an 8-byte boundary. To prevent
91805 ** these assert()s from failing, when building with SQLITE_DEBUG defined
91806 ** using gcc, we force nullMem to be 8-byte aligned using the magical
91807 ** __attribute__((aligned(8))) macro. */
91808 static const Mem nullMem
91809#if defined(SQLITE_DEBUG) && defined(__GNUC__4)
91810 __attribute__((aligned(8)))
91811#endif
91812 = {
91813 /* .u = */ {0},
91814 /* .z = */ (char*)0,
91815 /* .n = */ (int)0,
91816 /* .flags = */ (u16)MEM_Null0x0001,
91817 /* .enc = */ (u8)0,
91818 /* .eSubtype = */ (u8)0,
91819 /* .db = */ (sqlite3*)0,
91820 /* .szMalloc = */ (int)0,
91821 /* .uTemp = */ (u32)0,
91822 /* .zMalloc = */ (char*)0,
91823 /* .xDel = */ (void(*)(void*))0,
91824#ifdef SQLITE_DEBUG
91825 /* .pScopyFrom = */ (Mem*)0,
91826 /* .mScopyFlags= */ 0,
91827#endif
91828 };
91829 return &nullMem;
91830}
91831
91832/*
91833** Check to see if column iCol of the given statement is valid. If
91834** it is, return a pointer to the Mem for the value of that column.
91835** If iCol is not valid, return a pointer to a Mem which has a value
91836** of NULL.
91837*/
91838static Mem *columnMem(sqlite3_stmt *pStmt, int i){
91839 Vdbe *pVm;
91840 Mem *pOut;
91841
91842 pVm = (Vdbe *)pStmt;
91843 if( pVm==0 ) return (Mem*)columnNullValue();
91844 assert( pVm->db )((void) (0));
91845 sqlite3_mutex_enter(pVm->db->mutex);
91846 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
91847 pOut = &pVm->pResultRow[i];
91848 }else{
91849 sqlite3Error(pVm->db, SQLITE_RANGE25);
91850 pOut = (Mem*)columnNullValue();
91851 }
91852 return pOut;
91853}
91854
91855/*
91856** This function is called after invoking an sqlite3_value_XXX function on a
91857** column value (i.e. a value returned by evaluating an SQL expression in the
91858** select list of a SELECT statement) that may cause a malloc() failure. If
91859** malloc() has failed, the threads mallocFailed flag is cleared and the result
91860** code of statement pStmt set to SQLITE_NOMEM.
91861**
91862** Specifically, this is called from within:
91863**
91864** sqlite3_column_int()
91865** sqlite3_column_int64()
91866** sqlite3_column_text()
91867** sqlite3_column_text16()
91868** sqlite3_column_real()
91869** sqlite3_column_bytes()
91870** sqlite3_column_bytes16()
91871** sqlite3_column_blob()
91872*/
91873static void columnMallocFailure(sqlite3_stmt *pStmt)
91874{
91875 /* If malloc() failed during an encoding conversion within an
91876 ** sqlite3_column_XXX API, then set the return code of the statement to
91877 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
91878 ** and _finalize() will return NOMEM.
91879 */
91880 Vdbe *p = (Vdbe *)pStmt;
91881 if( p ){
91882 assert( p->db!=0 )((void) (0));
91883 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
91884 p->rc = sqlite3ApiExit(p->db, p->rc);
91885 sqlite3_mutex_leave(p->db->mutex);
91886 }
91887}
91888
91889/**************************** sqlite3_column_ *******************************
91890** The following routines are used to access elements of the current row
91891** in the result set.
91892*/
91893SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
91894 const void *val;
91895 val = sqlite3_value_blob( columnMem(pStmt,i) );
91896 /* Even though there is no encoding conversion, value_blob() might
91897 ** need to call malloc() to expand the result of a zeroblob()
91898 ** expression.
91899 */
91900 columnMallocFailure(pStmt);
91901 return val;
91902}
91903SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
91904 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
91905 columnMallocFailure(pStmt);
91906 return val;
91907}
91908SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
91909 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
91910 columnMallocFailure(pStmt);
91911 return val;
91912}
91913SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
91914 double val = sqlite3_value_double( columnMem(pStmt,i) );
91915 columnMallocFailure(pStmt);
91916 return val;
91917}
91918SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
91919 int val = sqlite3_value_int( columnMem(pStmt,i) );
91920 columnMallocFailure(pStmt);
91921 return val;
91922}
91923SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
91924 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
91925 columnMallocFailure(pStmt);
91926 return val;
91927}
91928SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
91929 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
91930 columnMallocFailure(pStmt);
91931 return val;
91932}
91933SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
91934 Mem *pOut = columnMem(pStmt, i);
91935 if( pOut->flags&MEM_Static0x2000 ){
91936 pOut->flags &= ~MEM_Static0x2000;
91937 pOut->flags |= MEM_Ephem0x4000;
91938 }
91939 columnMallocFailure(pStmt);
91940 return (sqlite3_value *)pOut;
91941}
91942#ifndef SQLITE_OMIT_UTF16
91943SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
91944 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
91945 columnMallocFailure(pStmt);
91946 return val;
91947}
91948#endif /* SQLITE_OMIT_UTF16 */
91949SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
91950 int iType = sqlite3_value_type( columnMem(pStmt,i) );
91951 columnMallocFailure(pStmt);
91952 return iType;
91953}
91954
91955/*
91956** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN.
91957*/
91958static const char * const azExplainColNames8[] = {
91959 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */
91960 "id", "parent", "notused", "detail" /* EQP */
91961};
91962static const u16 azExplainColNames16data[] = {
91963 /* 0 */ 'a', 'd', 'd', 'r', 0,
91964 /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0,
91965 /* 12 */ 'p', '1', 0,
91966 /* 15 */ 'p', '2', 0,
91967 /* 18 */ 'p', '3', 0,
91968 /* 21 */ 'p', '4', 0,
91969 /* 24 */ 'p', '5', 0,
91970 /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0,
91971 /* 35 */ 'i', 'd', 0,
91972 /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0,
91973 /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0,
91974 /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0
91975};
91976static const u8 iExplainColNames16[] = {
91977 0, 5, 12, 15, 18, 21, 24, 27,
91978 35, 38, 45, 53
91979};
91980
91981/*
91982** Convert the N-th element of pStmt->pColName[] into a string using
91983** xFunc() then return that string. If N is out of range, return 0.
91984**
91985** There are up to 5 names for each column. useType determines which
91986** name is returned. Here are the names:
91987**
91988** 0 The column name as it should be displayed for output
91989** 1 The datatype name for the column
91990** 2 The name of the database that the column derives from
91991** 3 The name of the table that the column derives from
91992** 4 The name of the table column that the result column derives from
91993**
91994** If the result is not a simple column reference (if it is an expression
91995** or a constant) then useTypes 2, 3, and 4 return NULL.
91996*/
91997static const void *columnName(
91998 sqlite3_stmt *pStmt, /* The statement */
91999 int N, /* Which column to get the name for */
92000 int useUtf16, /* True to return the name as UTF16 */
92001 int useType /* What type of name */
92002){
92003 const void *ret;
92004 Vdbe *p;
92005 int n;
92006 sqlite3 *db;
92007#ifdef SQLITE_ENABLE_API_ARMOR1
92008 if( pStmt==0 ){
92009 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(92009);
92010 return 0;
92011 }
92012#endif
92013 if( N<0 ) return 0;
92014 ret = 0;
92015 p = (Vdbe *)pStmt;
92016 db = p->db;
92017 assert( db!=0 )((void) (0));
92018 sqlite3_mutex_enter(db->mutex);
92019
92020 if( p->explain ){
92021 if( useType>0 ) goto columnName_end;
92022 n = p->explain==1 ? 8 : 4;
92023 if( N>=n ) goto columnName_end;
92024 if( useUtf16 ){
92025 int i = iExplainColNames16[N + 8*p->explain - 8];
92026 ret = (void*)&azExplainColNames16data[i];
92027 }else{
92028 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
92029 }
92030 goto columnName_end;
92031 }
92032 n = p->nResColumn;
92033 if( N<n ){
92034 u8 prior_mallocFailed = db->mallocFailed;
92035 N += useType*n;
92036#ifndef SQLITE_OMIT_UTF16
92037 if( useUtf16 ){
92038 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
92039 }else
92040#endif
92041 {
92042 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
92043 }
92044 /* A malloc may have failed inside of the _text() call. If this
92045 ** is the case, clear the mallocFailed flag and return NULL.
92046 */
92047 assert( db->mallocFailed==0 || db->mallocFailed==1 )((void) (0));
92048 if( db->mallocFailed > prior_mallocFailed ){
92049 sqlite3OomClear(db);
92050 ret = 0;
92051 }
92052 }
92053columnName_end:
92054 sqlite3_mutex_leave(db->mutex);
92055 return ret;
92056}
92057
92058/*
92059** Return the name of the Nth column of the result set returned by SQL
92060** statement pStmt.
92061*/
92062SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
92063 return columnName(pStmt, N, 0, COLNAME_NAME0);
92064}
92065#ifndef SQLITE_OMIT_UTF16
92066SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
92067 return columnName(pStmt, N, 1, COLNAME_NAME0);
92068}
92069#endif
92070
92071/*
92072** Constraint: If you have ENABLE_COLUMN_METADATA then you must
92073** not define OMIT_DECLTYPE.
92074*/
92075#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA1)
92076# error "Must not define both SQLITE_OMIT_DECLTYPE \
92077 and SQLITE_ENABLE_COLUMN_METADATA"
92078#endif
92079
92080#ifndef SQLITE_OMIT_DECLTYPE
92081/*
92082** Return the column declaration type (if applicable) of the 'i'th column
92083** of the result set of SQL statement pStmt.
92084*/
92085SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
92086 return columnName(pStmt, N, 0, COLNAME_DECLTYPE1);
92087}
92088#ifndef SQLITE_OMIT_UTF16
92089SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
92090 return columnName(pStmt, N, 1, COLNAME_DECLTYPE1);
92091}
92092#endif /* SQLITE_OMIT_UTF16 */
92093#endif /* SQLITE_OMIT_DECLTYPE */
92094
92095#ifdef SQLITE_ENABLE_COLUMN_METADATA1
92096/*
92097** Return the name of the database from which a result column derives.
92098** NULL is returned if the result column is an expression or constant or
92099** anything else which is not an unambiguous reference to a database column.
92100*/
92101SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
92102 return columnName(pStmt, N, 0, COLNAME_DATABASE2);
92103}
92104#ifndef SQLITE_OMIT_UTF16
92105SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
92106 return columnName(pStmt, N, 1, COLNAME_DATABASE2);
92107}
92108#endif /* SQLITE_OMIT_UTF16 */
92109
92110/*
92111** Return the name of the table from which a result column derives.
92112** NULL is returned if the result column is an expression or constant or
92113** anything else which is not an unambiguous reference to a database column.
92114*/
92115SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
92116 return columnName(pStmt, N, 0, COLNAME_TABLE3);
92117}
92118#ifndef SQLITE_OMIT_UTF16
92119SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
92120 return columnName(pStmt, N, 1, COLNAME_TABLE3);
92121}
92122#endif /* SQLITE_OMIT_UTF16 */
92123
92124/*
92125** Return the name of the table column from which a result column derives.
92126** NULL is returned if the result column is an expression or constant or
92127** anything else which is not an unambiguous reference to a database column.
92128*/
92129SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
92130 return columnName(pStmt, N, 0, COLNAME_COLUMN4);
92131}
92132#ifndef SQLITE_OMIT_UTF16
92133SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
92134 return columnName(pStmt, N, 1, COLNAME_COLUMN4);
92135}
92136#endif /* SQLITE_OMIT_UTF16 */
92137#endif /* SQLITE_ENABLE_COLUMN_METADATA */
92138
92139
92140/******************************* sqlite3_bind_ ***************************
92141**
92142** Routines used to attach values to wildcards in a compiled SQL statement.
92143*/
92144/*
92145** Unbind the value bound to variable i in virtual machine p. This is the
92146** the same as binding a NULL value to the column. If the "i" parameter is
92147** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK.
92148**
92149** A successful evaluation of this routine acquires the mutex on p.
92150** the mutex is released if any kind of error occurs.
92151**
92152** The error code stored in database p->db is overwritten with the return
92153** value in any case.
92154**
92155** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK,
92156** that means all of the the following will be true:
92157**
92158** p!=0
92159** p->pVar!=0
92160** i>0
92161** i<=p->nVar
92162**
92163** An assert() is normally added after vdbeUnbind() to help static analyzers
92164** realize this.
92165*/
92166static int vdbeUnbind(Vdbe *p, unsigned int i){
92167 Mem *pVar;
92168 if( vdbeSafetyNotNull(p) ){
92169 return SQLITE_MISUSE_BKPTsqlite3MisuseError(92169);
92170 }
92171 sqlite3_mutex_enter(p->db->mutex);
92172 if( p->eVdbeState!=VDBE_READY_STATE1 ){
92173 sqlite3Error(p->db, SQLITE_MISUSE_BKPTsqlite3MisuseError(92173));
92174 sqlite3_mutex_leave(p->db->mutex);
92175 sqlite3_log(SQLITE_MISUSE21,
92176 "bind on a busy prepared statement: [%s]", p->zSql);
92177 return SQLITE_MISUSE_BKPTsqlite3MisuseError(92177);
92178 }
92179 if( i>=(unsigned int)p->nVar ){
92180 sqlite3Error(p->db, SQLITE_RANGE25);
92181 sqlite3_mutex_leave(p->db->mutex);
92182 return SQLITE_RANGE25;
92183 }
92184 pVar = &p->aVar[i];
92185 sqlite3VdbeMemRelease(pVar);
92186 pVar->flags = MEM_Null0x0001;
92187 p->db->errCode = SQLITE_OK0;
92188
92189 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
92190 ** binding a new value to this variable invalidates the current query plan.
92191 **
92192 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
92193 ** parameter in the WHERE clause might influence the choice of query plan
92194 ** for a statement, then the statement will be automatically recompiled,
92195 ** as if there had been a schema change, on the first sqlite3_step() call
92196 ** following any change to the bindings of that parameter.
92197 */
92198 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
92199 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
92200 p->expired = 1;
92201 }
92202 return SQLITE_OK0;
92203}
92204
92205/*
92206** Bind a text or BLOB value.
92207*/
92208static int bindText(
92209 sqlite3_stmt *pStmt, /* The statement to bind against */
92210 int i, /* Index of the parameter to bind */
92211 const void *zData, /* Pointer to the data to be bound */
92212 i64 nData, /* Number of bytes of data to be bound */
92213 void (*xDel)(void*), /* Destructor for the data */
92214 u8 encoding /* Encoding for the data */
92215){
92216 Vdbe *p = (Vdbe *)pStmt;
92217 Mem *pVar;
92218 int rc;
92219
92220 rc = vdbeUnbind(p, (u32)(i-1));
92221 if( rc==SQLITE_OK0 ){
92222 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
92223 if( zData!=0 ){
92224 pVar = &p->aVar[i-1];
92225 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
92226 if( rc==SQLITE_OK0 && encoding!=0 ){
92227 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)((p->db)->enc));
92228 }
92229 if( rc ){
92230 sqlite3Error(p->db, rc);
92231 rc = sqlite3ApiExit(p->db, rc);
92232 }
92233 }
92234 sqlite3_mutex_leave(p->db->mutex);
92235 }else if( xDel!=SQLITE_STATIC((sqlite3_destructor_type)0) && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
92236 xDel((void*)zData);
92237 }
92238 return rc;
92239}
92240
92241
92242/*
92243** Bind a blob value to an SQL statement variable.
92244*/
92245SQLITE_API int sqlite3_bind_blob(
92246 sqlite3_stmt *pStmt,
92247 int i,
92248 const void *zData,
92249 int nData,
92250 void (*xDel)(void*)
92251){
92252#ifdef SQLITE_ENABLE_API_ARMOR1
92253 if( nData<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(92253);
92254#endif
92255 return bindText(pStmt, i, zData, nData, xDel, 0);
92256}
92257SQLITE_API int sqlite3_bind_blob64(
92258 sqlite3_stmt *pStmt,
92259 int i,
92260 const void *zData,
92261 sqlite3_uint64 nData,
92262 void (*xDel)(void*)
92263){
92264 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
92265 return bindText(pStmt, i, zData, nData, xDel, 0);
92266}
92267SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
92268 int rc;
92269 Vdbe *p = (Vdbe *)pStmt;
92270 rc = vdbeUnbind(p, (u32)(i-1));
92271 if( rc==SQLITE_OK0 ){
92272 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
92273 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
92274 sqlite3_mutex_leave(p->db->mutex);
92275 }
92276 return rc;
92277}
92278SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
92279 return sqlite3_bind_int64(p, i, (i64)iValue);
92280}
92281SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
92282 int rc;
92283 Vdbe *p = (Vdbe *)pStmt;
92284 rc = vdbeUnbind(p, (u32)(i-1));
92285 if( rc==SQLITE_OK0 ){
92286 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
92287 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
92288 sqlite3_mutex_leave(p->db->mutex);
92289 }
92290 return rc;
92291}
92292SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
92293 int rc;
92294 Vdbe *p = (Vdbe*)pStmt;
92295 rc = vdbeUnbind(p, (u32)(i-1));
92296 if( rc==SQLITE_OK0 ){
92297 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
92298 sqlite3_mutex_leave(p->db->mutex);
92299 }
92300 return rc;
92301}
92302SQLITE_API int sqlite3_bind_pointer(
92303 sqlite3_stmt *pStmt,
92304 int i,
92305 void *pPtr,
92306 const char *zPTtype,
92307 void (*xDestructor)(void*)
92308){
92309 int rc;
92310 Vdbe *p = (Vdbe*)pStmt;
92311 rc = vdbeUnbind(p, (u32)(i-1));
92312 if( rc==SQLITE_OK0 ){
92313 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
92314 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
92315 sqlite3_mutex_leave(p->db->mutex);
92316 }else if( xDestructor ){
92317 xDestructor(pPtr);
92318 }
92319 return rc;
92320}
92321SQLITE_API int sqlite3_bind_text(
92322 sqlite3_stmt *pStmt,
92323 int i,
92324 const char *zData,
92325 int nData,
92326 void (*xDel)(void*)
92327){
92328 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF81);
92329}
92330SQLITE_API int sqlite3_bind_text64(
92331 sqlite3_stmt *pStmt,
92332 int i,
92333 const char *zData,
92334 sqlite3_uint64 nData,
92335 void (*xDel)(void*),
92336 unsigned char enc
92337){
92338 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
92339 if( enc!=SQLITE_UTF81 ){
92340 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
92341 nData &= ~(u16)1;
92342 }
92343 return bindText(pStmt, i, zData, nData, xDel, enc);
92344}
92345#ifndef SQLITE_OMIT_UTF16
92346SQLITE_API int sqlite3_bind_text16(
92347 sqlite3_stmt *pStmt,
92348 int i,
92349 const void *zData,
92350 int n,
92351 void (*xDel)(void*)
92352){
92353 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE2);
92354}
92355#endif /* SQLITE_OMIT_UTF16 */
92356SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
92357 int rc;
92358 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
92359 case SQLITE_INTEGER1: {
92360 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
92361 break;
92362 }
92363 case SQLITE_FLOAT2: {
92364 assert( pValue->flags & (MEM_Real|MEM_IntReal) )((void) (0));
92365 rc = sqlite3_bind_double(pStmt, i,
92366 (pValue->flags & MEM_Real0x0008) ? pValue->u.r : (double)pValue->u.i
92367 );
92368 break;
92369 }
92370 case SQLITE_BLOB4: {
92371 if( pValue->flags & MEM_Zero0x0400 ){
92372 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
92373 }else{
92374 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
92375 }
92376 break;
92377 }
92378 case SQLITE_TEXT3: {
92379 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
92380 pValue->enc);
92381 break;
92382 }
92383 default: {
92384 rc = sqlite3_bind_null(pStmt, i);
92385 break;
92386 }
92387 }
92388 return rc;
92389}
92390SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
92391 int rc;
92392 Vdbe *p = (Vdbe *)pStmt;
92393 rc = vdbeUnbind(p, (u32)(i-1));
92394 if( rc==SQLITE_OK0 ){
92395 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar )((void) (0)); /* tag-20240917-01 */
92396#ifndef SQLITE_OMIT_INCRBLOB
92397 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
92398#else
92399 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
92400#endif
92401 sqlite3_mutex_leave(p->db->mutex);
92402 }
92403 return rc;
92404}
92405SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
92406 int rc;
92407 Vdbe *p = (Vdbe *)pStmt;
92408#ifdef SQLITE_ENABLE_API_ARMOR1
92409 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(92409);
92410#endif
92411 sqlite3_mutex_enter(p->db->mutex);
92412 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
92413 rc = SQLITE_TOOBIG18;
92414 }else{
92415 assert( (n & 0x7FFFFFFF)==n )((void) (0));
92416 rc = sqlite3_bind_zeroblob(pStmt, i, n);
92417 }
92418 rc = sqlite3ApiExit(p->db, rc);
92419 sqlite3_mutex_leave(p->db->mutex);
92420 return rc;
92421}
92422
92423/*
92424** Return the number of wildcards that can be potentially bound to.
92425** This routine is added to support DBD::SQLite.
92426*/
92427SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
92428 Vdbe *p = (Vdbe*)pStmt;
92429 return p ? p->nVar : 0;
92430}
92431
92432/*
92433** Return the name of a wildcard parameter. Return NULL if the index
92434** is out of range or if the wildcard is unnamed.
92435**
92436** The result is always UTF-8.
92437*/
92438SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
92439 Vdbe *p = (Vdbe*)pStmt;
92440 if( p==0 ) return 0;
92441 return sqlite3VListNumToName(p->pVList, i);
92442}
92443
92444/*
92445** Given a wildcard parameter name, return the index of the variable
92446** with that name. If there is no variable with the given name,
92447** return 0.
92448*/
92449SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
92450 if( p==0 || zName==0 ) return 0;
92451 return sqlite3VListNameToNum(p->pVList, zName, nName);
92452}
92453SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
92454 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
92455}
92456
92457/*
92458** Transfer all bindings from the first statement over to the second.
92459*/
92460SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
92461 Vdbe *pFrom = (Vdbe*)pFromStmt;
92462 Vdbe *pTo = (Vdbe*)pToStmt;
92463 int i;
92464 assert( pTo->db==pFrom->db )((void) (0));
92465 assert( pTo->nVar==pFrom->nVar )((void) (0));
92466 sqlite3_mutex_enter(pTo->db->mutex);
92467 for(i=0; i<pFrom->nVar; i++){
92468 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
92469 }
92470 sqlite3_mutex_leave(pTo->db->mutex);
92471 return SQLITE_OK0;
92472}
92473
92474#ifndef SQLITE_OMIT_DEPRECATED1
92475/*
92476** Deprecated external interface. Internal/core SQLite code
92477** should call sqlite3TransferBindings.
92478**
92479** It is misuse to call this routine with statements from different
92480** database connections. But as this is a deprecated interface, we
92481** will not bother to check for that condition.
92482**
92483** If the two statements contain a different number of bindings, then
92484** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
92485** SQLITE_OK is returned.
92486*/
92487SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
92488 Vdbe *pFrom = (Vdbe*)pFromStmt;
92489 Vdbe *pTo = (Vdbe*)pToStmt;
92490 if( pFrom->nVar!=pTo->nVar ){
92491 return SQLITE_ERROR1;
92492 }
92493 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 )((void) (0));
92494 if( pTo->expmask ){
92495 pTo->expired = 1;
92496 }
92497 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 )((void) (0));
92498 if( pFrom->expmask ){
92499 pFrom->expired = 1;
92500 }
92501 return sqlite3TransferBindings(pFromStmt, pToStmt);
92502}
92503#endif
92504
92505/*
92506** Return the sqlite3* database handle to which the prepared statement given
92507** in the argument belongs. This is the same database handle that was
92508** the first argument to the sqlite3_prepare() that was used to create
92509** the statement in the first place.
92510*/
92511SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
92512 return pStmt ? ((Vdbe*)pStmt)->db : 0;
92513}
92514
92515/*
92516** Return true if the prepared statement is guaranteed to not modify the
92517** database.
92518*/
92519SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
92520 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
92521}
92522
92523/*
92524** Return 1 if the statement is an EXPLAIN and return 2 if the
92525** statement is an EXPLAIN QUERY PLAN
92526*/
92527SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
92528 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
92529}
92530
92531/*
92532** Set the explain mode for a statement.
92533*/
92534SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){
92535 Vdbe *v = (Vdbe*)pStmt;
92536 int rc;
92537#ifdef SQLITE_ENABLE_API_ARMOR1
92538 if( pStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(92538);
92539#endif
92540 sqlite3_mutex_enter(v->db->mutex);
92541 if( ((int)v->explain)==eMode ){
92542 rc = SQLITE_OK0;
92543 }else if( eMode<0 || eMode>2 ){
92544 rc = SQLITE_ERROR1;
92545 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ){
92546 rc = SQLITE_ERROR1;
92547 }else if( v->eVdbeState!=VDBE_READY_STATE1 ){
92548 rc = SQLITE_BUSY5;
92549 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
92550 /* No reprepare necessary */
92551 v->explain = eMode;
92552 rc = SQLITE_OK0;
92553 }else{
92554 v->explain = eMode;
92555 rc = sqlite3Reprepare(v);
92556 v->haveEqpOps = eMode==2;
92557 }
92558 if( v->explain ){
92559 v->nResColumn = 12 - 4*v->explain;
92560 }else{
92561 v->nResColumn = v->nResAlloc;
92562 }
92563 sqlite3_mutex_leave(v->db->mutex);
92564 return rc;
92565}
92566
92567/*
92568** Return true if the prepared statement is in need of being reset.
92569*/
92570SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
92571 Vdbe *v = (Vdbe*)pStmt;
92572 return v!=0 && v->eVdbeState==VDBE_RUN_STATE2;
92573}
92574
92575/*
92576** Return a pointer to the next prepared statement after pStmt associated
92577** with database connection pDb. If pStmt is NULL, return the first
92578** prepared statement for the database connection. Return NULL if there
92579** are no more.
92580*/
92581SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
92582 sqlite3_stmt *pNext;
92583#ifdef SQLITE_ENABLE_API_ARMOR1
92584 if( !sqlite3SafetyCheckOk(pDb) ){
92585 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(92585);
92586 return 0;
92587 }
92588#endif
92589 sqlite3_mutex_enter(pDb->mutex);
92590 if( pStmt==0 ){
92591 pNext = (sqlite3_stmt*)pDb->pVdbe;
92592 }else{
92593 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
92594 }
92595 sqlite3_mutex_leave(pDb->mutex);
92596 return pNext;
92597}
92598
92599/*
92600** Return the value of a status counter for a prepared statement
92601*/
92602SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
92603 Vdbe *pVdbe = (Vdbe*)pStmt;
92604 u32 v;
92605#ifdef SQLITE_ENABLE_API_ARMOR1
92606 if( !pStmt
92607 || (op!=SQLITE_STMTSTATUS_MEMUSED99 && (op<0||op>=ArraySize(pVdbe->aCounter)((int)(sizeof(pVdbe->aCounter)/sizeof(pVdbe->aCounter[0
])))
))
92608 ){
92609 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(92609);
92610 return 0;
92611 }
92612#endif
92613 if( op==SQLITE_STMTSTATUS_MEMUSED99 ){
92614 sqlite3 *db = pVdbe->db;
92615 sqlite3_mutex_enter(db->mutex);
92616 v = 0;
92617 db->pnBytesFreed = (int*)&v;
92618 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd )((void) (0));
92619 db->lookaside.pEnd = db->lookaside.pStart;
92620 sqlite3VdbeDelete(pVdbe);
92621 db->pnBytesFreed = 0;
92622 db->lookaside.pEnd = db->lookaside.pTrueEnd;
92623 sqlite3_mutex_leave(db->mutex);
92624 }else{
92625 v = pVdbe->aCounter[op];
92626 if( resetFlag ) pVdbe->aCounter[op] = 0;
92627 }
92628 return (int)v;
92629}
92630
92631/*
92632** Return the SQL associated with a prepared statement
92633*/
92634SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
92635 Vdbe *p = (Vdbe *)pStmt;
92636 return p ? p->zSql : 0;
92637}
92638
92639/*
92640** Return the SQL associated with a prepared statement with
92641** bound parameters expanded. Space to hold the returned string is
92642** obtained from sqlite3_malloc(). The caller is responsible for
92643** freeing the returned string by passing it to sqlite3_free().
92644**
92645** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
92646** expanded bound parameters.
92647*/
92648SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
92649#ifdef SQLITE_OMIT_TRACE
92650 return 0;
92651#else
92652 char *z = 0;
92653 const char *zSql = sqlite3_sql(pStmt);
92654 if( zSql ){
92655 Vdbe *p = (Vdbe *)pStmt;
92656 sqlite3_mutex_enter(p->db->mutex);
92657 z = sqlite3VdbeExpandSql(p, zSql);
92658 sqlite3_mutex_leave(p->db->mutex);
92659 }
92660 return z;
92661#endif
92662}
92663
92664#ifdef SQLITE_ENABLE_NORMALIZE
92665/*
92666** Return the normalized SQL associated with a prepared statement.
92667*/
92668SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
92669 Vdbe *p = (Vdbe *)pStmt;
92670 if( p==0 ) return 0;
92671 if( p->zNormSql==0 && ALWAYS(p->zSql!=0)(p->zSql!=0) ){
92672 sqlite3_mutex_enter(p->db->mutex);
92673 p->zNormSql = sqlite3Normalize(p, p->zSql);
92674 sqlite3_mutex_leave(p->db->mutex);
92675 }
92676 return p->zNormSql;
92677}
92678#endif /* SQLITE_ENABLE_NORMALIZE */
92679
92680#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
92681/*
92682** Allocate and populate an UnpackedRecord structure based on the serialized
92683** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
92684** if successful, or a NULL pointer if an OOM error is encountered.
92685*/
92686static UnpackedRecord *vdbeUnpackRecord(
92687 KeyInfo *pKeyInfo,
92688 int nKey,
92689 const void *pKey
92690){
92691 UnpackedRecord *pRet; /* Return value */
92692
92693 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
92694 if( pRet ){
92695 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
92696 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
92697 }
92698 return pRet;
92699}
92700
92701/*
92702** This function is called from within a pre-update callback to retrieve
92703** a field of the row currently being updated or deleted.
92704*/
92705SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
92706 PreUpdate *p;
92707 Mem *pMem;
92708 int rc = SQLITE_OK0;
92709
92710#ifdef SQLITE_ENABLE_API_ARMOR1
92711 if( db==0 || ppValue==0 ){
92712 return SQLITE_MISUSE_BKPTsqlite3MisuseError(92712);
92713 }
92714#endif
92715 p = db->pPreUpdate;
92716 /* Test that this call is being made from within an SQLITE_DELETE or
92717 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
92718 if( !p || p->op==SQLITE_INSERT18 ){
92719 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(92719);
92720 goto preupdate_old_out;
92721 }
92722 if( p->pPk ){
92723 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
92724 }
92725 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92726 rc = SQLITE_RANGE25;
92727 goto preupdate_old_out;
92728 }
92729
92730 /* If the old.* record has not yet been loaded into memory, do so now. */
92731 if( p->pUnpacked==0 ){
92732 u32 nRec;
92733 u8 *aRec;
92734
92735 assert( p->pCsr->eCurType==CURTYPE_BTREE )((void) (0));
92736 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
92737 aRec = sqlite3DbMallocRaw(db, nRec);
92738 if( !aRec ) goto preupdate_old_out;
92739 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
92740 if( rc==SQLITE_OK0 ){
92741 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
92742 if( !p->pUnpacked ) rc = SQLITE_NOMEM7;
92743 }
92744 if( rc!=SQLITE_OK0 ){
92745 sqlite3DbFree(db, aRec);
92746 goto preupdate_old_out;
92747 }
92748 p->aRecord = aRec;
92749 }
92750
92751 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
92752 if( iIdx==p->pTab->iPKey ){
92753 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
92754 }else if( iIdx>=p->pUnpacked->nField ){
92755 /* This occurs when the table has been extended using ALTER TABLE
92756 ** ADD COLUMN. The value to return is the default value of the column. */
92757 Column *pCol = &p->pTab->aCol[iIdx];
92758 if( pCol->iDflt>0 ){
92759 if( p->apDflt==0 ){
92760 int nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
92761 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
92762 if( p->apDflt==0 ) goto preupdate_old_out;
92763 }
92764 if( p->apDflt[iIdx]==0 ){
92765 sqlite3_value *pVal = 0;
92766 Expr *pDflt;
92767 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) )((void) (0));
92768 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
92769 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db)((db)->enc), pCol->affinity, &pVal);
92770 if( rc==SQLITE_OK0 && pVal==0 ){
92771 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(92771);
92772 }
92773 p->apDflt[iIdx] = pVal;
92774 }
92775 *ppValue = p->apDflt[iIdx];
92776 }else{
92777 *ppValue = (sqlite3_value *)columnNullValue();
92778 }
92779 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL0x45 ){
92780 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
92781 testcase( pMem->flags & MEM_Int );
92782 testcase( pMem->flags & MEM_IntReal );
92783 sqlite3VdbeMemRealify(pMem);
92784 }
92785 }
92786
92787 preupdate_old_out:
92788 sqlite3Error(db, rc);
92789 return sqlite3ApiExit(db, rc);
92790}
92791#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
92792
92793#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
92794/*
92795** This function is called from within a pre-update callback to retrieve
92796** the number of columns in the row being updated, deleted or inserted.
92797*/
92798SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
92799 PreUpdate *p;
92800#ifdef SQLITE_ENABLE_API_ARMOR1
92801 p = db!=0 ? db->pPreUpdate : 0;
92802#else
92803 p = db->pPreUpdate;
92804#endif
92805 return (p ? p->keyinfo.nKeyField : 0);
92806}
92807#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
92808
92809#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
92810/*
92811** This function is designed to be called from within a pre-update callback
92812** only. It returns zero if the change that caused the callback was made
92813** immediately by a user SQL statement. Or, if the change was made by a
92814** trigger program, it returns the number of trigger programs currently
92815** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
92816** top-level trigger etc.).
92817**
92818** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
92819** or SET DEFAULT action is considered a trigger.
92820*/
92821SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
92822 PreUpdate *p;
92823#ifdef SQLITE_ENABLE_API_ARMOR1
92824 p = db!=0 ? db->pPreUpdate : 0;
92825#else
92826 p = db->pPreUpdate;
92827#endif
92828 return (p ? p->v->nFrame : 0);
92829}
92830#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
92831
92832#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
92833/*
92834** This function is designed to be called from within a pre-update callback
92835** only.
92836*/
92837SQLITE_API int sqlite3_preupdate_blobwrite(sqlite3 *db){
92838 PreUpdate *p;
92839#ifdef SQLITE_ENABLE_API_ARMOR1
92840 p = db!=0 ? db->pPreUpdate : 0;
92841#else
92842 p = db->pPreUpdate;
92843#endif
92844 return (p ? p->iBlobWrite : -1);
92845}
92846#endif
92847
92848#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
92849/*
92850** This function is called from within a pre-update callback to retrieve
92851** a field of the row currently being updated or inserted.
92852*/
92853SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
92854 PreUpdate *p;
92855 int rc = SQLITE_OK0;
92856 Mem *pMem;
92857
92858#ifdef SQLITE_ENABLE_API_ARMOR1
92859 if( db==0 || ppValue==0 ){
92860 return SQLITE_MISUSE_BKPTsqlite3MisuseError(92860);
92861 }
92862#endif
92863 p = db->pPreUpdate;
92864 if( !p || p->op==SQLITE_DELETE9 ){
92865 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(92865);
92866 goto preupdate_new_out;
92867 }
92868 if( p->pPk && p->op!=SQLITE_UPDATE23 ){
92869 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
92870 }
92871 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92872 rc = SQLITE_RANGE25;
92873 goto preupdate_new_out;
92874 }
92875
92876 if( p->op==SQLITE_INSERT18 ){
92877 /* For an INSERT, memory cell p->iNewReg contains the serialized record
92878 ** that is being inserted. Deserialize it. */
92879 UnpackedRecord *pUnpack = p->pNewUnpacked;
92880 if( !pUnpack ){
92881 Mem *pData = &p->v->aMem[p->iNewReg];
92882 rc = ExpandBlob(pData)(((pData)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pData
):0)
;
92883 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
92884 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
92885 if( !pUnpack ){
92886 rc = SQLITE_NOMEM7;
92887 goto preupdate_new_out;
92888 }
92889 p->pNewUnpacked = pUnpack;
92890 }
92891 pMem = &pUnpack->aMem[iIdx];
92892 if( iIdx==p->pTab->iPKey ){
92893 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
92894 }else if( iIdx>=pUnpack->nField ){
92895 pMem = (sqlite3_value *)columnNullValue();
92896 }
92897 }else{
92898 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
92899 ** value. Make a copy of the cell contents and return a pointer to it.
92900 ** It is not safe to return a pointer to the memory cell itself as the
92901 ** caller may modify the value text encoding.
92902 */
92903 assert( p->op==SQLITE_UPDATE )((void) (0));
92904 if( !p->aNew ){
92905 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
92906 if( !p->aNew ){
92907 rc = SQLITE_NOMEM7;
92908 goto preupdate_new_out;
92909 }
92910 }
92911 assert( iIdx>=0 && iIdx<p->pCsr->nField )((void) (0));
92912 pMem = &p->aNew[iIdx];
92913 if( pMem->flags==0 ){
92914 if( iIdx==p->pTab->iPKey ){
92915 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
92916 }else{
92917 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
92918 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
92919 }
92920 }
92921 }
92922 *ppValue = pMem;
92923
92924 preupdate_new_out:
92925 sqlite3Error(db, rc);
92926 return sqlite3ApiExit(db, rc);
92927}
92928#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
92929
92930#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
92931/*
92932** Return status data for a single loop within query pStmt.
92933*/
92934SQLITE_API int sqlite3_stmt_scanstatus_v2(
92935 sqlite3_stmt *pStmt, /* Prepared statement being queried */
92936 int iScan, /* Index of loop to report on */
92937 int iScanStatusOp, /* Which metric to return */
92938 int flags,
92939 void *pOut /* OUT: Write the answer here */
92940){
92941 Vdbe *p = (Vdbe*)pStmt;
92942 VdbeOp *aOp;
92943 int nOp;
92944 ScanStatus *pScan = 0;
92945 int idx;
92946
92947#ifdef SQLITE_ENABLE_API_ARMOR1
92948 if( p==0 || pOut==0
92949 || iScanStatusOp<SQLITE_SCANSTAT_NLOOP0
92950 || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE7 ){
92951 return 1;
92952 }
92953#endif
92954 aOp = p->aOp;
92955 nOp = p->nOp;
92956 if( p->pFrame ){
92957 VdbeFrame *pFrame;
92958 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
92959 aOp = pFrame->aOp;
92960 nOp = pFrame->nOp;
92961 }
92962
92963 if( iScan<0 ){
92964 int ii;
92965 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE7 ){
92966 i64 res = 0;
92967 for(ii=0; ii<nOp; ii++){
92968 res += aOp[ii].nCycle;
92969 }
92970 *(i64*)pOut = res;
92971 return 0;
92972 }
92973 return 1;
92974 }
92975 if( flags & SQLITE_SCANSTAT_COMPLEX0x0001 ){
92976 idx = iScan;
92977 }else{
92978 /* If the COMPLEX flag is clear, then this function must ignore any
92979 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */
92980 for(idx=0; idx<p->nScan; idx++){
92981 pScan = &p->aScan[idx];
92982 if( pScan->zName ){
92983 iScan--;
92984 if( iScan<0 ) break;
92985 }
92986 }
92987 }
92988 if( idx>=p->nScan ) return 1;
92989 assert( pScan==0 || pScan==&p->aScan[idx] )((void) (0));
92990 pScan = &p->aScan[idx];
92991
92992 switch( iScanStatusOp ){
92993 case SQLITE_SCANSTAT_NLOOP0: {
92994 if( pScan->addrLoop>0 ){
92995 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
92996 }else{
92997 *(sqlite3_int64*)pOut = -1;
92998 }
92999 break;
93000 }
93001 case SQLITE_SCANSTAT_NVISIT1: {
93002 if( pScan->addrVisit>0 ){
93003 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
93004 }else{
93005 *(sqlite3_int64*)pOut = -1;
93006 }
93007 break;
93008 }
93009 case SQLITE_SCANSTAT_EST2: {
93010 double r = 1.0;
93011 LogEst x = pScan->nEst;
93012 while( x<100 ){
93013 x += 10;
93014 r *= 0.5;
93015 }
93016 *(double*)pOut = r*sqlite3LogEstToInt(x);
93017 break;
93018 }
93019 case SQLITE_SCANSTAT_NAME3: {
93020 *(const char**)pOut = pScan->zName;
93021 break;
93022 }
93023 case SQLITE_SCANSTAT_EXPLAIN4: {
93024 if( pScan->addrExplain ){
93025 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
93026 }else{
93027 *(const char**)pOut = 0;
93028 }
93029 break;
93030 }
93031 case SQLITE_SCANSTAT_SELECTID5: {
93032 if( pScan->addrExplain ){
93033 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
93034 }else{
93035 *(int*)pOut = -1;
93036 }
93037 break;
93038 }
93039 case SQLITE_SCANSTAT_PARENTID6: {
93040 if( pScan->addrExplain ){
93041 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
93042 }else{
93043 *(int*)pOut = -1;
93044 }
93045 break;
93046 }
93047 case SQLITE_SCANSTAT_NCYCLE7: {
93048 i64 res = 0;
93049 if( pScan->aAddrRange[0]==0 ){
93050 res = -1;
93051 }else{
93052 int ii;
93053 for(ii=0; ii<ArraySize(pScan->aAddrRange)((int)(sizeof(pScan->aAddrRange)/sizeof(pScan->aAddrRange
[0])))
; ii+=2){
93054 int iIns = pScan->aAddrRange[ii];
93055 int iEnd = pScan->aAddrRange[ii+1];
93056 if( iIns==0 ) break;
93057 if( iIns>0 ){
93058 while( iIns<=iEnd ){
93059 res += aOp[iIns].nCycle;
93060 iIns++;
93061 }
93062 }else{
93063 int iOp;
93064 for(iOp=0; iOp<nOp; iOp++){
93065 Op *pOp = &aOp[iOp];
93066 if( pOp->p1!=iEnd ) continue;
93067 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE0x40)==0 ){
93068 continue;
93069 }
93070 res += aOp[iOp].nCycle;
93071 }
93072 }
93073 }
93074 }
93075 *(i64*)pOut = res;
93076 break;
93077 }
93078 default: {
93079 return 1;
93080 }
93081 }
93082 return 0;
93083}
93084
93085/*
93086** Return status data for a single loop within query pStmt.
93087*/
93088SQLITE_API int sqlite3_stmt_scanstatus(
93089 sqlite3_stmt *pStmt, /* Prepared statement being queried */
93090 int iScan, /* Index of loop to report on */
93091 int iScanStatusOp, /* Which metric to return */
93092 void *pOut /* OUT: Write the answer here */
93093){
93094 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut);
93095}
93096
93097/*
93098** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
93099*/
93100SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
93101 Vdbe *p = (Vdbe*)pStmt;
93102 int ii;
93103 for(ii=0; p!=0 && ii<p->nOp; ii++){
93104 Op *pOp = &p->aOp[ii];
93105 pOp->nExec = 0;
93106 pOp->nCycle = 0;
93107 }
93108}
93109#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
93110
93111/************** End of vdbeapi.c *********************************************/
93112/************** Begin file vdbetrace.c ***************************************/
93113/*
93114** 2009 November 25
93115**
93116** The author disclaims copyright to this source code. In place of
93117** a legal notice, here is a blessing:
93118**
93119** May you do good and not evil.
93120** May you find forgiveness for yourself and forgive others.
93121** May you share freely, never taking more than you give.
93122**
93123*************************************************************************
93124**
93125** This file contains code used to insert the values of host parameters
93126** (aka "wildcards") into the SQL text output by sqlite3_trace().
93127**
93128** The Vdbe parse-tree explainer is also found here.
93129*/
93130/* #include "sqliteInt.h" */
93131/* #include "vdbeInt.h" */
93132
93133#ifndef SQLITE_OMIT_TRACE
93134
93135/*
93136** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
93137** bytes in this text up to but excluding the first character in
93138** a host parameter. If the text contains no host parameters, return
93139** the total number of bytes in the text.
93140*/
93141static int findNextHostParameter(const char *zSql, int *pnToken){
93142 int tokenType;
93143 int nTotal = 0;
93144 int n;
93145
93146 *pnToken = 0;
93147 while( zSql[0] ){
93148 n = sqlite3GetToken((u8*)zSql, &tokenType);
93149 assert( n>0 && tokenType!=TK_ILLEGAL )((void) (0));
93150 if( tokenType==TK_VARIABLE157 ){
93151 *pnToken = n;
93152 break;
93153 }
93154 nTotal += n;
93155 zSql += n;
93156 }
93157 return nTotal;
93158}
93159
93160/*
93161** This function returns a pointer to a nul-terminated string in memory
93162** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
93163** string contains a copy of zRawSql but with host parameters expanded to
93164** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
93165** then the returned string holds a copy of zRawSql with "-- " prepended
93166** to each line of text.
93167**
93168** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
93169** then long strings and blobs are truncated to that many bytes. This
93170** can be used to prevent unreasonably large trace strings when dealing
93171** with large (multi-megabyte) strings and blobs.
93172**
93173** The calling function is responsible for making sure the memory returned
93174** is eventually freed.
93175**
93176** ALGORITHM: Scan the input string looking for host parameters in any of
93177** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
93178** string literals, quoted identifier names, and comments. For text forms,
93179** the host parameter index is found by scanning the prepared
93180** statement for the corresponding OP_Variable opcode. Once the host
93181** parameter index is known, locate the value in p->aVar[]. Then render
93182** the value as a literal in place of the host parameter name.
93183*/
93184SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(
93185 Vdbe *p, /* The prepared statement being evaluated */
93186 const char *zRawSql /* Raw text of the SQL statement */
93187){
93188 sqlite3 *db; /* The database connection */
93189 int idx = 0; /* Index of a host parameter */
93190 int nextIndex = 1; /* Index of next ? host parameter */
93191 int n; /* Length of a token prefix */
93192 int nToken; /* Length of the parameter token */
93193 int i; /* Loop counter */
93194 Mem *pVar; /* Value of a host parameter */
93195 StrAccum out; /* Accumulate the output here */
93196#ifndef SQLITE_OMIT_UTF16
93197 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
93198#endif
93199
93200 db = p->db;
93201 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
93202 if( db->nVdbeExec>1 ){
93203 while( *zRawSql ){
93204 const char *zStart = zRawSql;
93205 while( *(zRawSql++)!='\n' && *zRawSql );
93206 sqlite3_str_append(&out, "-- ", 3);
93207 assert( (zRawSql - zStart) > 0 )((void) (0));
93208 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
93209 }
93210 }else if( p->nVar==0 ){
93211 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
93212 }else{
93213 while( zRawSql[0] ){
93214 n = findNextHostParameter(zRawSql, &nToken);
93215 assert( n>0 )((void) (0));
93216 sqlite3_str_append(&out, zRawSql, n);
93217 zRawSql += n;
93218 assert( zRawSql[0] || nToken==0 )((void) (0));
93219 if( nToken==0 ) break;
93220 if( zRawSql[0]=='?' ){
93221 if( nToken>1 ){
93222 assert( sqlite3Isdigit(zRawSql[1]) )((void) (0));
93223 sqlite3GetInt32(&zRawSql[1], &idx);
93224 }else{
93225 idx = nextIndex;
93226 }
93227 }else{
93228 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||((void) (0))
93229 zRawSql[0]=='@' || zRawSql[0]=='#' )((void) (0));
93230 testcase( zRawSql[0]==':' );
93231 testcase( zRawSql[0]=='$' );
93232 testcase( zRawSql[0]=='@' );
93233 testcase( zRawSql[0]=='#' );
93234 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
93235 assert( idx>0 )((void) (0));
93236 }
93237 zRawSql += nToken;
93238 nextIndex = MAX(idx + 1, nextIndex)((idx + 1)>(nextIndex)?(idx + 1):(nextIndex));
93239 assert( idx>0 && idx<=p->nVar )((void) (0));
93240 pVar = &p->aVar[idx-1];
93241 if( pVar->flags & MEM_Null0x0001 ){
93242 sqlite3_str_append(&out, "NULL", 4);
93243 }else if( pVar->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
93244 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
93245 }else if( pVar->flags & MEM_Real0x0008 ){
93246 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
93247 }else if( pVar->flags & MEM_Str0x0002 ){
93248 int nOut; /* Number of bytes of the string text to include in output */
93249#ifndef SQLITE_OMIT_UTF16
93250 u8 enc = ENC(db)((db)->enc);
93251 if( enc!=SQLITE_UTF81 ){
93252 memset(&utf8, 0, sizeof(utf8));
93253 utf8.db = db;
93254 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
93255 if( SQLITE_NOMEM7==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF81) ){
93256 out.accError = SQLITE_NOMEM7;
93257 out.nAlloc = 0;
93258 }
93259 pVar = &utf8;
93260 }
93261#endif
93262 nOut = pVar->n;
93263#ifdef SQLITE_TRACE_SIZE_LIMIT
93264 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
93265 nOut = SQLITE_TRACE_SIZE_LIMIT;
93266 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
93267 }
93268#endif
93269 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
93270#ifdef SQLITE_TRACE_SIZE_LIMIT
93271 if( nOut<pVar->n ){
93272 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
93273 }
93274#endif
93275#ifndef SQLITE_OMIT_UTF16
93276 if( enc!=SQLITE_UTF81 ) sqlite3VdbeMemRelease(&utf8);
93277#endif
93278 }else if( pVar->flags & MEM_Zero0x0400 ){
93279 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
93280 }else{
93281 int nOut; /* Number of bytes of the blob to include in output */
93282 assert( pVar->flags & MEM_Blob )((void) (0));
93283 sqlite3_str_append(&out, "x'", 2);
93284 nOut = pVar->n;
93285#ifdef SQLITE_TRACE_SIZE_LIMIT
93286 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
93287#endif
93288 for(i=0; i<nOut; i++){
93289 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
93290 }
93291 sqlite3_str_append(&out, "'", 1);
93292#ifdef SQLITE_TRACE_SIZE_LIMIT
93293 if( nOut<pVar->n ){
93294 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
93295 }
93296#endif
93297 }
93298 }
93299 }
93300 if( out.accError ) sqlite3_str_reset(&out);
93301 return sqlite3StrAccumFinish(&out);
93302}
93303
93304#endif /* #ifndef SQLITE_OMIT_TRACE */
93305
93306/************** End of vdbetrace.c *******************************************/
93307/************** Begin file vdbe.c ********************************************/
93308/*
93309** 2001 September 15
93310**
93311** The author disclaims copyright to this source code. In place of
93312** a legal notice, here is a blessing:
93313**
93314** May you do good and not evil.
93315** May you find forgiveness for yourself and forgive others.
93316** May you share freely, never taking more than you give.
93317**
93318*************************************************************************
93319** The code in this file implements the function that runs the
93320** bytecode of a prepared statement.
93321**
93322** Various scripts scan this source file in order to generate HTML
93323** documentation, headers files, or other derived files. The formatting
93324** of the code in this file is, therefore, important. See other comments
93325** in this file for details. If in doubt, do not deviate from existing
93326** commenting and indentation practices when changing or adding code.
93327*/
93328/* #include "sqliteInt.h" */
93329/* #include "vdbeInt.h" */
93330
93331/*
93332** High-resolution hardware timer used for debugging and testing only.
93333*/
93334#if defined(VDBE_PROFILE) \
93335 || defined(SQLITE_PERFORMANCE_TRACE) \
93336 || defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
93337/************** Include hwtime.h in the middle of vdbe.c *********************/
93338/************** Begin file hwtime.h ******************************************/
93339/*
93340** 2008 May 27
93341**
93342** The author disclaims copyright to this source code. In place of
93343** a legal notice, here is a blessing:
93344**
93345** May you do good and not evil.
93346** May you find forgiveness for yourself and forgive others.
93347** May you share freely, never taking more than you give.
93348**
93349******************************************************************************
93350**
93351** This file contains inline asm code for retrieving "high-performance"
93352** counters for x86 and x86_64 class CPUs.
93353*/
93354#ifndef SQLITE_HWTIME_H
93355#define SQLITE_HWTIME_H
93356
93357/*
93358** The following routine only works on Pentium-class (or newer) processors.
93359** It uses the RDTSC opcode to read the cycle count value out of the
93360** processor and returns that value. This can be used for high-res
93361** profiling.
93362*/
93363#if !defined(__STRICT_ANSI__) && \
93364 (defined(__GNUC__4) || defined(_MSC_VER)) && \
93365 (defined(i386) || defined(__i386__) || defined(_M_IX86))
93366
93367 #if defined(__GNUC__4)
93368
93369 __inline__ sqlite_uint64 sqlite3Hwtime(void){
93370 unsigned int lo, hi;
93371 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
93372 return (sqlite_uint64)hi << 32 | lo;
93373 }
93374
93375 #elif defined(_MSC_VER)
93376
93377 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
93378 __asm {
93379 rdtsc
93380 ret ; return value at EDX:EAX
93381 }
93382 }
93383
93384 #endif
93385
93386#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__4) && defined(__x86_64__1))
93387
93388 __inline__ sqlite_uint64 sqlite3Hwtime(void){
93389 unsigned int lo, hi;
93390 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
93391 return (sqlite_uint64)hi << 32 | lo;
93392 }
93393
93394#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__4) && defined(__ppc__))
93395
93396 __inline__ sqlite_uint64 sqlite3Hwtime(void){
93397 unsigned long long retval;
93398 unsigned long junk;
93399 __asm__ __volatile__ ("\n\
93400 1: mftbu %1\n\
93401 mftb %L0\n\
93402 mftbu %0\n\
93403 cmpw %0,%1\n\
93404 bne 1b"
93405 : "=r" (retval), "=r" (junk));
93406 return retval;
93407 }
93408
93409#else
93410
93411 /*
93412 ** asm() is needed for hardware timing support. Without asm(),
93413 ** disable the sqlite3Hwtime() routine.
93414 **
93415 ** sqlite3Hwtime() is only used for some obscure debugging
93416 ** and analysis configurations, not in any deliverable, so this
93417 ** should not be a great loss.
93418 */
93419SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
93420
93421#endif
93422
93423#endif /* !defined(SQLITE_HWTIME_H) */
93424
93425/************** End of hwtime.h **********************************************/
93426/************** Continuing where we left off in vdbe.c ***********************/
93427#endif
93428
93429/*
93430** Invoke this macro on memory cells just prior to changing the
93431** value of the cell. This macro verifies that shallow copies are
93432** not misused. A shallow copy of a string or blob just copies a
93433** pointer to the string or blob, not the content. If the original
93434** is changed while the copy is still in use, the string or blob might
93435** be changed out from under the copy. This macro verifies that nothing
93436** like that ever happens.
93437*/
93438#ifdef SQLITE_DEBUG
93439# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
93440#else
93441# define memAboutToChange(P,M)
93442#endif
93443
93444/*
93445** The following global variable is incremented every time a cursor
93446** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
93447** procedures use this information to make sure that indices are
93448** working correctly. This variable has no function other than to
93449** help verify the correct operation of the library.
93450*/
93451#ifdef SQLITE_TEST
93452SQLITE_API int sqlite3_search_count = 0;
93453#endif
93454
93455/*
93456** When this global variable is positive, it gets decremented once before
93457** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
93458** field of the sqlite3 structure is set in order to simulate an interrupt.
93459**
93460** This facility is used for testing purposes only. It does not function
93461** in an ordinary build.
93462*/
93463#ifdef SQLITE_TEST
93464SQLITE_API int sqlite3_interrupt_count = 0;
93465#endif
93466
93467/*
93468** The next global variable is incremented each type the OP_Sort opcode
93469** is executed. The test procedures use this information to make sure that
93470** sorting is occurring or not occurring at appropriate times. This variable
93471** has no function other than to help verify the correct operation of the
93472** library.
93473*/
93474#ifdef SQLITE_TEST
93475SQLITE_API int sqlite3_sort_count = 0;
93476#endif
93477
93478/*
93479** The next global variable records the size of the largest MEM_Blob
93480** or MEM_Str that has been used by a VDBE opcode. The test procedures
93481** use this information to make sure that the zero-blob functionality
93482** is working correctly. This variable has no function other than to
93483** help verify the correct operation of the library.
93484*/
93485#ifdef SQLITE_TEST
93486SQLITE_API int sqlite3_max_blobsize = 0;
93487static void updateMaxBlobsize(Mem *p){
93488 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && p->n>sqlite3_max_blobsize ){
93489 sqlite3_max_blobsize = p->n;
93490 }
93491}
93492#endif
93493
93494/*
93495** This macro evaluates to true if either the update hook or the preupdate
93496** hook are enabled for database connect DB.
93497*/
93498#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
93499# define HAS_UPDATE_HOOK(DB)((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
93500#else
93501# define HAS_UPDATE_HOOK(DB)((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) ((DB)->xUpdateCallback)
93502#endif
93503
93504/*
93505** The next global variable is incremented each time the OP_Found opcode
93506** is executed. This is used to test whether or not the foreign key
93507** operation implemented using OP_FkIsZero is working. This variable
93508** has no function other than to help verify the correct operation of the
93509** library.
93510*/
93511#ifdef SQLITE_TEST
93512SQLITE_API int sqlite3_found_count = 0;
93513#endif
93514
93515/*
93516** Test a register to see if it exceeds the current maximum blob size.
93517** If it does, record the new maximum blob size.
93518*/
93519#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
93520# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
93521#else
93522# define UPDATE_MAX_BLOBSIZE(P)
93523#endif
93524
93525#ifdef SQLITE_DEBUG
93526/* This routine provides a convenient place to set a breakpoint during
93527** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
93528** each opcode is printed. Variables "pc" (program counter) and pOp are
93529** available to add conditionals to the breakpoint. GDB example:
93530**
93531** break test_trace_breakpoint if pc=22
93532**
93533** Other useful labels for breakpoints include:
93534** test_addop_breakpoint(pc,pOp)
93535** sqlite3CorruptError(lineno)
93536** sqlite3MisuseError(lineno)
93537** sqlite3CantopenError(lineno)
93538*/
93539static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
93540 static u64 n = 0;
93541 (void)pc;
93542 (void)pOp;
93543 (void)v;
93544 n++;
93545 if( n==LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32)) ) abort(); /* So that n is used, preventing a warning */
93546}
93547#endif
93548
93549/*
93550** Invoke the VDBE coverage callback, if that callback is defined. This
93551** feature is used for test suite validation only and does not appear an
93552** production builds.
93553**
93554** M is the type of branch. I is the direction taken for this instance of
93555** the branch.
93556**
93557** M: 2 - two-way branch (I=0: fall-thru 1: jump )
93558** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
93559** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
93560**
93561** In other words, if M is 2, then I is either 0 (for fall-through) or
93562** 1 (for when the branch is taken). If M is 3, the I is 0 for an
93563** ordinary fall-through, I is 1 if the branch was taken, and I is 2
93564** if the result of comparison is NULL. For M=3, I=2 the jump may or
93565** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
93566** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
93567** depending on if the operands are less than, equal, or greater than.
93568**
93569** iSrcLine is the source code line (from the __LINE__ macro) that
93570** generated the VDBE instruction combined with flag bits. The source
93571** code line number is in the lower 24 bits of iSrcLine and the upper
93572** 8 bytes are flags. The lower three bits of the flags indicate
93573** values for I that should never occur. For example, if the branch is
93574** always taken, the flags should be 0x05 since the fall-through and
93575** alternate branch are never taken. If a branch is never taken then
93576** flags should be 0x06 since only the fall-through approach is allowed.
93577**
93578** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
93579** interested in equal or not-equal. In other words, I==0 and I==2
93580** should be treated as equivalent
93581**
93582** Since only a line number is retained, not the filename, this macro
93583** only works for amalgamation builds. But that is ok, since these macros
93584** should be no-ops except for special builds used to measure test coverage.
93585*/
93586#if !defined(SQLITE_VDBE_COVERAGE)
93587# define VdbeBranchTaken(I,M)
93588#else
93589# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
93590 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
93591 u8 mNever;
93592 assert( I<=2 )((void) (0)); /* 0: fall through, 1: taken, 2: alternate taken */
93593 assert( M<=4 )((void) (0)); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
93594 assert( I<M )((void) (0)); /* I can only be 2 if M is 3 or 4 */
93595 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
93596 I = 1<<I;
93597 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
93598 ** the flags indicate directions that the branch can never go. If
93599 ** a branch really does go in one of those directions, assert right
93600 ** away. */
93601 mNever = iSrcLine >> 24;
93602 assert( (I & mNever)==0 )((void) (0));
93603 if( sqlite3GlobalConfigsqlite3Config.xVdbeBranch==0 ) return; /*NO_TEST*/
93604 /* Invoke the branch coverage callback with three arguments:
93605 ** iSrcLine - the line number of the VdbeCoverage() macro, with
93606 ** flags removed.
93607 ** I - Mask of bits 0x07 indicating which cases are are
93608 ** fulfilled by this instance of the jump. 0x01 means
93609 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
93610 ** impossible cases (ex: if the comparison is never NULL)
93611 ** are filled in automatically so that the coverage
93612 ** measurement logic does not flag those impossible cases
93613 ** as missed coverage.
93614 ** M - Type of jump. Same as M argument above
93615 */
93616 I |= mNever;
93617 if( M==2 ) I |= 0x04;
93618 if( M==4 ){
93619 I |= 0x08;
93620 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
93621 }
93622 sqlite3GlobalConfigsqlite3Config.xVdbeBranch(sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg,
93623 iSrcLine&0xffffff, I, M);
93624 }
93625#endif
93626
93627/*
93628** An ephemeral string value (signified by the MEM_Ephem flag) contains
93629** a pointer to a dynamically allocated string where some other entity
93630** is responsible for deallocating that string. Because the register
93631** does not control the string, it might be deleted without the register
93632** knowing it.
93633**
93634** This routine converts an ephemeral string into a dynamically allocated
93635** string that the register itself controls. In other words, it
93636** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
93637*/
93638#define Deephemeralize(P)if( ((P)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(P) ){ goto no_mem;}
\
93639 if( ((P)->flags&MEM_Ephem0x4000)!=0 \
93640 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
93641
93642/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
93643#define isSorter(x)((x)->eCurType==1) ((x)->eCurType==CURTYPE_SORTER1)
93644
93645/*
93646** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
93647** if we run out of memory.
93648*/
93649static VdbeCursor *allocateCursor(
93650 Vdbe *p, /* The virtual machine */
93651 int iCur, /* Index of the new VdbeCursor */
93652 int nField, /* Number of fields in the table or index */
93653 u8 eCurType /* Type of the new cursor */
93654){
93655 /* Find the memory cell that will be used to store the blob of memory
93656 ** required for this VdbeCursor structure. It is convenient to use a
93657 ** vdbe memory cell to manage the memory allocation required for a
93658 ** VdbeCursor structure for the following reasons:
93659 **
93660 ** * Sometimes cursor numbers are used for a couple of different
93661 ** purposes in a vdbe program. The different uses might require
93662 ** different sized allocations. Memory cells provide growable
93663 ** allocations.
93664 **
93665 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
93666 ** be freed lazily via the sqlite3_release_memory() API. This
93667 ** minimizes the number of malloc calls made by the system.
93668 **
93669 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
93670 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
93671 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
93672 */
93673 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
93674
93675 int nByte;
93676 VdbeCursor *pCx = 0;
93677 nByte =
93678 ROUND8P(sizeof(VdbeCursor))(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
93679 (eCurType==CURTYPE_BTREE0?sqlite3BtreeCursorSize():0);
93680
93681 assert( iCur>=0 && iCur<p->nCursor )((void) (0));
93682 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
93683 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
93684 p->apCsr[iCur] = 0;
93685 }
93686
93687 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
93688 ** the pMem used to hold space for the cursor has enough storage available
93689 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
93690 ** to hold cursors, it is faster to in-line the logic. */
93691 assert( pMem->flags==MEM_Undefined )((void) (0));
93692 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
93693 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc )((void) (0));
93694 if( pMem->szMalloc<nByte ){
93695 if( pMem->szMalloc>0 ){
93696 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
93697 }
93698 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
93699 if( pMem->zMalloc==0 ){
93700 pMem->szMalloc = 0;
93701 return 0;
93702 }
93703 pMem->szMalloc = nByte;
93704 }
93705
93706 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
93707 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)__builtin_offsetof(VdbeCursor, pAltCursor));
93708 pCx->eCurType = eCurType;
93709 pCx->nField = nField;
93710 pCx->aOffset = &pCx->aType[nField];
93711 if( eCurType==CURTYPE_BTREE0 ){
93712 pCx->uc.pCursor = (BtCursor*)
93713 &pMem->z[ROUND8P(sizeof(VdbeCursor))(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
93714 sqlite3BtreeCursorZero(pCx->uc.pCursor);
93715 }
93716 return pCx;
93717}
93718
93719/*
93720** The string in pRec is known to look like an integer and to have a
93721** floating point value of rValue. Return true and set *piValue to the
93722** integer value if the string is in range to be an integer. Otherwise,
93723** return false.
93724*/
93725static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
93726 i64 iValue;
93727 iValue = sqlite3RealToI64(rValue);
93728 if( sqlite3RealSameAsInt(rValue,iValue) ){
93729 *piValue = iValue;
93730 return 1;
93731 }
93732 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
93733}
93734
93735/*
93736** Try to convert a value into a numeric representation if we can
93737** do so without loss of information. In other words, if the string
93738** looks like a number, convert it into a number. If it does not
93739** look like a number, leave it alone.
93740**
93741** If the bTryForInt flag is true, then extra effort is made to give
93742** an integer representation. Strings that look like floating point
93743** values but which have no fractional component (example: '48.00')
93744** will have a MEM_Int representation when bTryForInt is true.
93745**
93746** If bTryForInt is false, then if the input string contains a decimal
93747** point or exponential notation, the result is only MEM_Real, even
93748** if there is an exact integer representation of the quantity.
93749*/
93750static void applyNumericAffinity(Mem *pRec, int bTryForInt){
93751 double rValue;
93752 u8 enc = pRec->enc;
93753 int rc;
93754 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str )((void) (0));
93755 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
93756 if( rc<=0 ) return;
93757 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
93758 pRec->flags |= MEM_Int0x0004;
93759 }else{
93760 pRec->u.r = rValue;
93761 pRec->flags |= MEM_Real0x0008;
93762 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
93763 }
93764 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
93765 ** string representation after computing a numeric equivalent, because the
93766 ** string representation might not be the canonical representation for the
93767 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
93768 pRec->flags &= ~MEM_Str0x0002;
93769}
93770
93771/*
93772** Processing is determine by the affinity parameter:
93773**
93774** SQLITE_AFF_INTEGER:
93775** SQLITE_AFF_REAL:
93776** SQLITE_AFF_NUMERIC:
93777** Try to convert pRec to an integer representation or a
93778** floating-point representation if an integer representation
93779** is not possible. Note that the integer representation is
93780** always preferred, even if the affinity is REAL, because
93781** an integer representation is more space efficient on disk.
93782**
93783** SQLITE_AFF_FLEXNUM:
93784** If the value is text, then try to convert it into a number of
93785** some kind (integer or real) but do not make any other changes.
93786**
93787** SQLITE_AFF_TEXT:
93788** Convert pRec to a text representation.
93789**
93790** SQLITE_AFF_BLOB:
93791** SQLITE_AFF_NONE:
93792** No-op. pRec is unchanged.
93793*/
93794static void applyAffinity(
93795 Mem *pRec, /* The value to apply affinity to */
93796 char affinity, /* The affinity to be applied */
93797 u8 enc /* Use this text encoding */
93798){
93799 if( affinity>=SQLITE_AFF_NUMERIC0x43 ){
93800 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL((void) (0))
93801 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM )((void) (0));
93802 if( (pRec->flags & MEM_Int0x0004)==0 ){ /*OPTIMIZATION-IF-FALSE*/
93803 if( (pRec->flags & (MEM_Real0x0008|MEM_IntReal0x0020))==0 ){
93804 if( pRec->flags & MEM_Str0x0002 ) applyNumericAffinity(pRec,1);
93805 }else if( affinity<=SQLITE_AFF_REAL0x45 ){
93806 sqlite3VdbeIntegerAffinity(pRec);
93807 }
93808 }
93809 }else if( affinity==SQLITE_AFF_TEXT0x42 ){
93810 /* Only attempt the conversion to TEXT if there is an integer or real
93811 ** representation (blob and NULL do not get converted) but no string
93812 ** representation. It would be harmless to repeat the conversion if
93813 ** there is already a string rep, but it is pointless to waste those
93814 ** CPU cycles. */
93815 if( 0==(pRec->flags&MEM_Str0x0002) ){ /*OPTIMIZATION-IF-FALSE*/
93816 if( (pRec->flags&(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020)) ){
93817 testcase( pRec->flags & MEM_Int );
93818 testcase( pRec->flags & MEM_Real );
93819 testcase( pRec->flags & MEM_IntReal );
93820 sqlite3VdbeMemStringify(pRec, enc, 1);
93821 }
93822 }
93823 pRec->flags &= ~(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020);
93824 }
93825}
93826
93827/*
93828** Try to convert the type of a function argument or a result column
93829** into a numeric representation. Use either INTEGER or REAL whichever
93830** is appropriate. But only do the conversion if it is possible without
93831** loss of information and return the revised type of the argument.
93832*/
93833SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
93834 int eType = sqlite3_value_type(pVal);
93835 if( eType==SQLITE_TEXT3 ){
93836 Mem *pMem = (Mem*)pVal;
93837 applyNumericAffinity(pMem, 0);
93838 eType = sqlite3_value_type(pVal);
93839 }
93840 return eType;
93841}
93842
93843/*
93844** Exported version of applyAffinity(). This one works on sqlite3_value*,
93845** not the internal Mem* type.
93846*/
93847SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(
93848 sqlite3_value *pVal,
93849 u8 affinity,
93850 u8 enc
93851){
93852 applyAffinity((Mem *)pVal, affinity, enc);
93853}
93854
93855/*
93856** pMem currently only holds a string type (or maybe a BLOB that we can
93857** interpret as a string if we want to). Compute its corresponding
93858** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
93859** accordingly.
93860*/
93861static u16 SQLITE_NOINLINE__attribute__((noinline)) computeNumericType(Mem *pMem){
93862 int rc;
93863 sqlite3_int64 ix;
93864 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 )((void) (0));
93865 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 )((void) (0));
93866 if( ExpandBlob(pMem)(((pMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pMem)
:0)
){
93867 pMem->u.i = 0;
93868 return MEM_Int0x0004;
93869 }
93870 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
93871 if( rc<=0 ){
93872 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
93873 pMem->u.i = ix;
93874 return MEM_Int0x0004;
93875 }else{
93876 return MEM_Real0x0008;
93877 }
93878 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
93879 pMem->u.i = ix;
93880 return MEM_Int0x0004;
93881 }
93882 return MEM_Real0x0008;
93883}
93884
93885/*
93886** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
93887** none.
93888**
93889** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
93890** But it does set pMem->u.r and pMem->u.i appropriately.
93891*/
93892static u16 numericType(Mem *pMem){
93893 assert( (pMem->flags & MEM_Null)==0((void) (0))
93894 || pMem->db==0 || pMem->db->mallocFailed )((void) (0));
93895 if( pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001) ){
93896 testcase( pMem->flags & MEM_Int );
93897 testcase( pMem->flags & MEM_Real );
93898 testcase( pMem->flags & MEM_IntReal );
93899 return pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001);
93900 }
93901 assert( pMem->flags & (MEM_Str|MEM_Blob) )((void) (0));
93902 testcase( pMem->flags & MEM_Str );
93903 testcase( pMem->flags & MEM_Blob );
93904 return computeNumericType(pMem);
93905 return 0;
93906}
93907
93908#ifdef SQLITE_DEBUG
93909/*
93910** Write a nice string representation of the contents of cell pMem
93911** into buffer zBuf, length nBuf.
93912*/
93913SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
93914 int f = pMem->flags;
93915 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
93916 if( f&MEM_Blob0x0010 ){
93917 int i;
93918 char c;
93919 if( f & MEM_Dyn0x1000 ){
93920 c = 'z';
93921 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
93922 }else if( f & MEM_Static0x2000 ){
93923 c = 't';
93924 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
93925 }else if( f & MEM_Ephem0x4000 ){
93926 c = 'e';
93927 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
93928 }else{
93929 c = 's';
93930 }
93931 sqlite3_str_appendf(pStr, "%cx[", c);
93932 for(i=0; i<25 && i<pMem->n; i++){
93933 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
93934 }
93935 sqlite3_str_appendf(pStr, "|");
93936 for(i=0; i<25 && i<pMem->n; i++){
93937 char z = pMem->z[i];
93938 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
93939 }
93940 sqlite3_str_appendf(pStr,"]");
93941 if( f & MEM_Zero0x0400 ){
93942 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
93943 }
93944 }else if( f & MEM_Str0x0002 ){
93945 int j;
93946 u8 c;
93947 if( f & MEM_Dyn0x1000 ){
93948 c = 'z';
93949 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
93950 }else if( f & MEM_Static0x2000 ){
93951 c = 't';
93952 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
93953 }else if( f & MEM_Ephem0x4000 ){
93954 c = 'e';
93955 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
93956 }else{
93957 c = 's';
93958 }
93959 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
93960 for(j=0; j<25 && j<pMem->n; j++){
93961 c = pMem->z[j];
93962 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
93963 }
93964 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
93965 if( f & MEM_Term0x0200 ){
93966 sqlite3_str_appendf(pStr, "(0-term)");
93967 }
93968 }
93969}
93970#endif
93971
93972#ifdef SQLITE_DEBUG
93973/*
93974** Print the value of a register for tracing purposes:
93975*/
93976static void memTracePrint(Mem *p){
93977 if( p->flags & MEM_Undefined0x0000 ){
93978 printf(" undefined");
93979 }else if( p->flags & MEM_Null0x0001 ){
93980 printf(p->flags & MEM_Zero0x0400 ? " NULL-nochng" : " NULL");
93981 }else if( (p->flags & (MEM_Int0x0004|MEM_Str0x0002))==(MEM_Int0x0004|MEM_Str0x0002) ){
93982 printf(" si:%lld", p->u.i);
93983 }else if( (p->flags & (MEM_IntReal0x0020))!=0 ){
93984 printf(" ir:%lld", p->u.i);
93985 }else if( p->flags & MEM_Int0x0004 ){
93986 printf(" i:%lld", p->u.i);
93987#ifndef SQLITE_OMIT_FLOATING_POINT
93988 }else if( p->flags & MEM_Real0x0008 ){
93989 printf(" r:%.17g", p->u.r);
93990#endif
93991 }else if( sqlite3VdbeMemIsRowSet(p) ){
93992 printf(" (rowset)");
93993 }else{
93994 StrAccum acc;
93995 char zBuf[1000];
93996 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
93997 sqlite3VdbeMemPrettyPrint(p, &acc);
93998 printf(" %s", sqlite3StrAccumFinish(&acc));
93999 }
94000 if( p->flags & MEM_Subtype0x0800 ) printf(" subtype=0x%02x", p->eSubtype);
94001}
94002static void registerTrace(int iReg, Mem *p){
94003 printf("R[%d] = ", iReg);
94004 memTracePrint(p);
94005 if( p->pScopyFrom ){
94006 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
94007 }
94008 printf("\n");
94009 sqlite3VdbeCheckMemInvariants(p);
94010}
94011/**/ void sqlite3PrintMem(Mem *pMem){
94012 memTracePrint(pMem);
94013 printf("\n");
94014 fflush(stdoutstdout);
94015}
94016#endif
94017
94018#ifdef SQLITE_DEBUG
94019/*
94020** Show the values of all registers in the virtual machine. Used for
94021** interactive debugging.
94022*/
94023SQLITE_PRIVATEstatic void sqlite3VdbeRegisterDump(Vdbe *v){
94024 int i;
94025 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
94026}
94027#endif /* SQLITE_DEBUG */
94028
94029
94030#ifdef SQLITE_DEBUG
94031# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
94032#else
94033# define REGISTER_TRACE(R,M)
94034#endif
94035
94036#ifndef NDEBUG1
94037/*
94038** This function is only called from within an assert() expression. It
94039** checks that the sqlite3.nTransaction variable is correctly set to
94040** the number of non-transaction savepoints currently in the
94041** linked list starting at sqlite3.pSavepoint.
94042**
94043** Usage:
94044**
94045** assert( checkSavepointCount(db) );
94046*/
94047static int checkSavepointCount(sqlite3 *db){
94048 int n = 0;
94049 Savepoint *p;
94050 for(p=db->pSavepoint; p; p=p->pNext) n++;
94051 assert( n==(db->nSavepoint + db->isTransactionSavepoint) )((void) (0));
94052 return 1;
94053}
94054#endif
94055
94056/*
94057** Return the register of pOp->p2 after first preparing it to be
94058** overwritten with an integer value.
94059*/
94060static SQLITE_NOINLINE__attribute__((noinline)) Mem *out2PrereleaseWithClear(Mem *pOut){
94061 sqlite3VdbeMemSetNull(pOut);
94062 pOut->flags = MEM_Int0x0004;
94063 return pOut;
94064}
94065static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
94066 Mem *pOut;
94067 assert( pOp->p2>0 )((void) (0));
94068 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
94069 pOut = &p->aMem[pOp->p2];
94070 memAboutToChange(p, pOut);
94071 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x8000|0x1000))!=0) ){ /*OPTIMIZATION-IF-FALSE*/
94072 return out2PrereleaseWithClear(pOut);
94073 }else{
94074 pOut->flags = MEM_Int0x0004;
94075 return pOut;
94076 }
94077}
94078
94079/*
94080** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
94081** with pOp->p3. Return the hash.
94082*/
94083static u64 filterHash(const Mem *aMem, const Op *pOp){
94084 int i, mx;
94085 u64 h = 0;
94086
94087 assert( pOp->p4type==P4_INT32 )((void) (0));
94088 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
94089 const Mem *p = &aMem[i];
94090 if( p->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
94091 h += p->u.i;
94092 }else if( p->flags & MEM_Real0x0008 ){
94093 h += sqlite3VdbeIntValue(p);
94094 }else if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
94095 /* All strings have the same hash and all blobs have the same hash,
94096 ** though, at least, those hashes are different from each other and
94097 ** from NULL. */
94098 h += 4093 + (p->flags & (MEM_Str0x0002|MEM_Blob0x0010));
94099 }
94100 }
94101 return h;
94102}
94103
94104
94105/*
94106** For OP_Column, factor out the case where content is loaded from
94107** overflow pages, so that the code to implement this case is separate
94108** the common case where all content fits on the page. Factoring out
94109** the code reduces register pressure and helps the common case
94110** to run faster.
94111*/
94112static SQLITE_NOINLINE__attribute__((noinline)) int vdbeColumnFromOverflow(
94113 VdbeCursor *pC, /* The BTree cursor from which we are reading */
94114 int iCol, /* The column to read */
94115 int t, /* The serial-type code for the column value */
94116 i64 iOffset, /* Offset to the start of the content value */
94117 u32 cacheStatus, /* Current Vdbe.cacheCtr value */
94118 u32 colCacheCtr, /* Current value of the column cache counter */
94119 Mem *pDest /* Store the value into this register. */
94120){
94121 int rc;
94122 sqlite3 *db = pDest->db;
94123 int encoding = pDest->enc;
94124 int len = sqlite3VdbeSerialTypeLen(t);
94125 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
94126 if( len>db->aLimit[SQLITE_LIMIT_LENGTH0] ) return SQLITE_TOOBIG18;
94127 if( len > 4000 && pC->pKeyInfo==0 ){
94128 /* Cache large column values that are on overflow pages using
94129 ** an RCStr (reference counted string) so that if they are reloaded,
94130 ** that do not have to be copied a second time. The overhead of
94131 ** creating and managing the cache is such that this is only
94132 ** profitable for larger TEXT and BLOB values.
94133 **
94134 ** Only do this on table-btrees so that writes to index-btrees do not
94135 ** need to clear the cache. This buys performance in the common case
94136 ** in exchange for generality.
94137 */
94138 VdbeTxtBlbCache *pCache;
94139 char *pBuf;
94140 if( pC->colCache==0 ){
94141 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) );
94142 if( pC->pCache==0 ) return SQLITE_NOMEM7;
94143 pC->colCache = 1;
94144 }
94145 pCache = pC->pCache;
94146 if( pCache->pCValue==0
94147 || pCache->iCol!=iCol
94148 || pCache->cacheStatus!=cacheStatus
94149 || pCache->colCacheCtr!=colCacheCtr
94150 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor)
94151 ){
94152 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue);
94153 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 );
94154 if( pBuf==0 ) return SQLITE_NOMEM7;
94155 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf);
94156 if( rc ) return rc;
94157 pBuf[len] = 0;
94158 pBuf[len+1] = 0;
94159 pBuf[len+2] = 0;
94160 pCache->iCol = iCol;
94161 pCache->cacheStatus = cacheStatus;
94162 pCache->colCacheCtr = colCacheCtr;
94163 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor);
94164 }else{
94165 pBuf = pCache->pCValue;
94166 }
94167 assert( t>=12 )((void) (0));
94168 sqlite3RCStrRef(pBuf);
94169 if( t&1 ){
94170 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding,
94171 sqlite3RCStrUnref);
94172 pDest->flags |= MEM_Term0x0200;
94173 }else{
94174 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0,
94175 sqlite3RCStrUnref);
94176 }
94177 }else{
94178 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
94179 if( rc ) return rc;
94180 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
94181 if( (t&1)!=0 && encoding==SQLITE_UTF81 ){
94182 pDest->z[len] = 0;
94183 pDest->flags |= MEM_Term0x0200;
94184 }
94185 }
94186 pDest->flags &= ~MEM_Ephem0x4000;
94187 return rc;
94188}
94189
94190
94191/*
94192** Return the symbolic name for the data type of a pMem
94193*/
94194static const char *vdbeMemTypeName(Mem *pMem){
94195 static const char *azTypes[] = {
94196 /* SQLITE_INTEGER */ "INT",
94197 /* SQLITE_FLOAT */ "REAL",
94198 /* SQLITE_TEXT */ "TEXT",
94199 /* SQLITE_BLOB */ "BLOB",
94200 /* SQLITE_NULL */ "NULL"
94201 };
94202 return azTypes[sqlite3_value_type(pMem)-1];
94203}
94204
94205/*
94206** Execute as much of a VDBE program as we can.
94207** This is the core of sqlite3_step().
94208*/
94209SQLITE_PRIVATEstatic int sqlite3VdbeExec(
94210 Vdbe *p /* The VDBE */
94211){
94212 Op *aOp = p->aOp; /* Copy of p->aOp */
94213 Op *pOp = aOp; /* Current operation */
94214#ifdef SQLITE_DEBUG
94215 Op *pOrigOp; /* Value of pOp at the top of the loop */
94216 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
94217 u8 iCompareIsInit = 0; /* iCompare is initialized */
94218#endif
94219 int rc = SQLITE_OK0; /* Value to return */
94220 sqlite3 *db = p->db; /* The database */
94221 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
94222 u8 encoding = ENC(db)((db)->enc); /* The database encoding */
94223 int iCompare = 0; /* Result of last comparison */
94224 u64 nVmStep = 0; /* Number of virtual machine steps */
94225#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
94226 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
94227#endif
94228 Mem *aMem = p->aMem; /* Copy of p->aMem */
94229 Mem *pIn1 = 0; /* 1st input operand */
94230 Mem *pIn2 = 0; /* 2nd input operand */
94231 Mem *pIn3 = 0; /* 3rd input operand */
94232 Mem *pOut = 0; /* Output operand */
94233 u32 colCacheCtr = 0; /* Column cache counter */
94234#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1) || defined(VDBE_PROFILE)
94235 u64 *pnCycle = 0;
94236 int bStmtScanStatus = IS_STMT_SCANSTATUS(db)(db->flags & 0x00000400)!=0;
1
Assuming the condition is false
94237#endif
94238 /*** INSERT STACK UNION HERE ***/
94239
94240 assert( p->eVdbeState==VDBE_RUN_STATE )((void) (0)); /* sqlite3_step() verifies this */
94241 if( DbMaskNonZero(p->lockMask)((p->lockMask)!=0) ){
2
Assuming field 'lockMask' is equal to 0
3
Taking false branch
94242 sqlite3VdbeEnter(p);
94243 }
94244#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
94245 if( db->xProgress ){
94246 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP4];
94247 assert( 0 < db->nProgressOps )((void) (0));
94248 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
94249 }else{
94250 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
94251 }
94252#endif
94253 if( p->rc==SQLITE_NOMEM7 ){
4
Assuming field 'rc' is not equal to SQLITE_NOMEM
5
Taking false branch
94254 /* This happens if a malloc() inside a call to sqlite3_column_text() or
94255 ** sqlite3_column_text16() failed. */
94256 goto no_mem;
94257 }
94258 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY )((void) (0));
94259 testcase( p->rc!=SQLITE_OK );
94260 p->rc = SQLITE_OK0;
94261 assert( p->bIsReader || p->readOnly!=0 )((void) (0));
94262 p->iCurrentTime = 0;
94263 assert( p->explain==0 )((void) (0));
94264 db->busyHandler.nBusy = 0;
94265 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ) goto abort_due_to_interrupt;
6
Assuming the condition is false
7
Taking false branch
94266 sqlite3VdbeIOTraceSql(p);
94267#ifdef SQLITE_DEBUG
94268 sqlite3BeginBenignMalloc();
94269 if( p->pc==0
94270 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
94271 ){
94272 int i;
94273 int once = 1;
94274 sqlite3VdbePrintSql(p);
94275 if( p->db->flags & SQLITE_VdbeListing ){
94276 printf("VDBE Program Listing:\n");
94277 for(i=0; i<p->nOp; i++){
94278 sqlite3VdbePrintOp(stdoutstdout, i, &aOp[i]);
94279 }
94280 }
94281 if( p->db->flags & SQLITE_VdbeEQP ){
94282 for(i=0; i<p->nOp; i++){
94283 if( aOp[i].opcode==OP_Explain188 ){
94284 if( once ) printf("VDBE Query Plan:\n");
94285 printf("%s\n", aOp[i].p4.z);
94286 once = 0;
94287 }
94288 }
94289 }
94290 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
94291 }
94292 sqlite3EndBenignMalloc();
94293#endif
94294 for(pOp=&aOp[p->pc]; 1; pOp++){
8
Loop condition is true. Entering loop body
94295 /* Errors are detected by individual opcodes, with an immediate
94296 ** jumps to abort_due_to_error. */
94297 assert( rc==SQLITE_OK )((void) (0));
94298
94299 assert( pOp>=aOp && pOp<&aOp[p->nOp])((void) (0));
94300 nVmStep++;
94301
94302#if defined(VDBE_PROFILE)
94303 pOp->nExec++;
94304 pnCycle = &pOp->nCycle;
94305 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
94306#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
94307 if( bStmtScanStatus
8.1
'bStmtScanStatus' is 0
){
9
Taking false branch
94308 pOp->nExec++;
94309 pnCycle = &pOp->nCycle;
94310 *pnCycle -= sqlite3Hwtime();
94311 }
94312#endif
94313
94314 /* Only allow tracing if SQLITE_DEBUG is defined.
94315 */
94316#ifdef SQLITE_DEBUG
94317 if( db->flags & SQLITE_VdbeTrace ){
94318 sqlite3VdbePrintOp(stdoutstdout, (int)(pOp - aOp), pOp);
94319 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
94320 }
94321#endif
94322
94323
94324 /* Check to see if we need to simulate an interrupt. This only happens
94325 ** if we have a special test build.
94326 */
94327#ifdef SQLITE_TEST
94328 if( sqlite3_interrupt_count>0 ){
94329 sqlite3_interrupt_count--;
94330 if( sqlite3_interrupt_count==0 ){
94331 sqlite3_interrupt(db);
94332 }
94333 }
94334#endif
94335
94336 /* Sanity checking on other operands */
94337#ifdef SQLITE_DEBUG
94338 {
94339 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
94340 if( (opProperty & OPFLG_IN10x02)!=0 ){
94341 assert( pOp->p1>0 )((void) (0));
94342 assert( pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
94343 assert( memIsValid(&aMem[pOp->p1]) )((void) (0));
94344 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) )((void) (0));
94345 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
94346 }
94347 if( (opProperty & OPFLG_IN20x04)!=0 ){
94348 assert( pOp->p2>0 )((void) (0));
94349 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
94350 assert( memIsValid(&aMem[pOp->p2]) )((void) (0));
94351 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) )((void) (0));
94352 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
94353 }
94354 if( (opProperty & OPFLG_IN30x08)!=0 ){
94355 assert( pOp->p3>0 )((void) (0));
94356 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
94357 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
94358 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) )((void) (0));
94359 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
94360 }
94361 if( (opProperty & OPFLG_OUT20x10)!=0 ){
94362 assert( pOp->p2>0 )((void) (0));
94363 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
94364 memAboutToChange(p, &aMem[pOp->p2]);
94365 }
94366 if( (opProperty & OPFLG_OUT30x20)!=0 ){
94367 assert( pOp->p3>0 )((void) (0));
94368 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
94369 memAboutToChange(p, &aMem[pOp->p3]);
94370 }
94371 }
94372#endif
94373#ifdef SQLITE_DEBUG
94374 pOrigOp = pOp;
94375#endif
94376
94377 switch( pOp->opcode ){
10
Control jumps to 'case 138:' at line 99871
94378
94379/*****************************************************************************
94380** What follows is a massive switch statement where each case implements a
94381** separate instruction in the virtual machine. If we follow the usual
94382** indentation conventions, each case should be indented by 6 spaces. But
94383** that is a lot of wasted space on the left margin. So the code within
94384** the switch statement will break with convention and be flush-left. Another
94385** big comment (similar to this one) will mark the point in the code where
94386** we transition back to normal indentation.
94387**
94388** The formatting of each case is important. The makefile for SQLite
94389** generates two C files "opcodes.h" and "opcodes.c" by scanning this
94390** file looking for lines that begin with "case OP_". The opcodes.h files
94391** will be filled with #defines that give unique integer values to each
94392** opcode and the opcodes.c file is filled with an array of strings where
94393** each string is the symbolic name for the corresponding opcode. If the
94394** case statement is followed by a comment of the form "/# same as ... #/"
94395** that comment is used to determine the particular value of the opcode.
94396**
94397** Other keywords in the comment that follows each case are used to
94398** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
94399** Keywords include: in1, in2, in3, out2, out3. See
94400** the mkopcodeh.awk script for additional information.
94401**
94402** Documentation about VDBE opcodes is generated by scanning this file
94403** for lines of that contain "Opcode:". That line and all subsequent
94404** comment lines are used in the generation of the opcode.html documentation
94405** file.
94406**
94407** SUMMARY:
94408**
94409** Formatting is important to scripts that scan this file.
94410** Do not deviate from the formatting style currently in use.
94411**
94412*****************************************************************************/
94413
94414/* Opcode: Goto * P2 * * *
94415**
94416** An unconditional jump to address P2.
94417** The next instruction executed will be
94418** the one at index P2 from the beginning of
94419** the program.
94420**
94421** The P1 parameter is not actually used by this opcode. However, it
94422** is sometimes set to 1 instead of 0 as a hint to the command-line shell
94423** that this Goto is the bottom of a loop and that the lines from P2 down
94424** to the current line should be indented for EXPLAIN output.
94425*/
94426case OP_Goto9: { /* jump */
94427
94428#ifdef SQLITE_DEBUG
94429 /* In debugging mode, when the p5 flags is set on an OP_Goto, that
94430 ** means we should really jump back to the preceding OP_ReleaseReg
94431 ** instruction. */
94432 if( pOp->p5 ){
94433 assert( pOp->p2 < (int)(pOp - aOp) )((void) (0));
94434 assert( pOp->p2 > 1 )((void) (0));
94435 pOp = &aOp[pOp->p2 - 2];
94436 assert( pOp[1].opcode==OP_ReleaseReg )((void) (0));
94437 goto check_for_interrupt;
94438 }
94439#endif
94440
94441jump_to_p2_and_check_for_interrupt:
94442 pOp = &aOp[pOp->p2 - 1];
94443
94444 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
94445 ** OP_VNext, or OP_SorterNext) all jump here upon
94446 ** completion. Check to see if sqlite3_interrupt() has been called
94447 ** or if the progress callback needs to be invoked.
94448 **
94449 ** This code uses unstructured "goto" statements and does not look clean.
94450 ** But that is not due to sloppy coding habits. The code is written this
94451 ** way for performance, to avoid having to run the interrupt and progress
94452 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
94453 ** faster according to "valgrind --tool=cachegrind" */
94454check_for_interrupt:
94455 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ) goto abort_due_to_interrupt;
94456#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
94457 /* Call the progress callback if it is configured and the required number
94458 ** of VDBE ops have been executed (either since this invocation of
94459 ** sqlite3VdbeExec() or since last time the progress callback was called).
94460 ** If the progress callback returns non-zero, exit the virtual machine with
94461 ** a return code SQLITE_ABORT.
94462 */
94463 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
94464 assert( db->nProgressOps!=0 )((void) (0));
94465 nProgressLimit += db->nProgressOps;
94466 if( db->xProgress(db->pProgressArg) ){
94467 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
94468 rc = SQLITE_INTERRUPT9;
94469 goto abort_due_to_error;
94470 }
94471 }
94472#endif
94473
94474 break;
94475}
94476
94477/* Opcode: Gosub P1 P2 * * *
94478**
94479** Write the current address onto register P1
94480** and then jump to address P2.
94481*/
94482case OP_Gosub10: { /* jump */
94483 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
94484 pIn1 = &aMem[pOp->p1];
94485 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
94486 memAboutToChange(p, pIn1);
94487 pIn1->flags = MEM_Int0x0004;
94488 pIn1->u.i = (int)(pOp-aOp);
94489 REGISTER_TRACE(pOp->p1, pIn1);
94490 goto jump_to_p2_and_check_for_interrupt;
94491}
94492
94493/* Opcode: Return P1 P2 P3 * *
94494**
94495** Jump to the address stored in register P1. If P1 is a return address
94496** register, then this accomplishes a return from a subroutine.
94497**
94498** If P3 is 1, then the jump is only taken if register P1 holds an integer
94499** values, otherwise execution falls through to the next opcode, and the
94500** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
94501** integer or else an assert() is raised. P3 should be set to 1 when
94502** this opcode is used in combination with OP_BeginSubrtn, and set to 0
94503** otherwise.
94504**
94505** The value in register P1 is unchanged by this opcode.
94506**
94507** P2 is not used by the byte-code engine. However, if P2 is positive
94508** and also less than the current address, then the "EXPLAIN" output
94509** formatter in the CLI will indent all opcodes from the P2 opcode up
94510** to be not including the current Return. P2 should be the first opcode
94511** in the subroutine from which this opcode is returning. Thus the P2
94512** value is a byte-code indentation hint. See tag-20220407a in
94513** wherecode.c and shell.c.
94514*/
94515case OP_Return67: { /* in1 */
94516 pIn1 = &aMem[pOp->p1];
94517 if( pIn1->flags & MEM_Int0x0004 ){
94518 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
94519 pOp = &aOp[pIn1->u.i];
94520 }else if( ALWAYS(pOp->p3)(pOp->p3) ){
94521 VdbeBranchTaken(0, 2);
94522 }
94523 break;
94524}
94525
94526/* Opcode: InitCoroutine P1 P2 P3 * *
94527**
94528** Set up register P1 so that it will Yield to the coroutine
94529** located at address P3.
94530**
94531** If P2!=0 then the coroutine implementation immediately follows
94532** this opcode. So jump over the coroutine implementation to
94533** address P2.
94534**
94535** See also: EndCoroutine
94536*/
94537case OP_InitCoroutine11: { /* jump0 */
94538 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
94539 assert( pOp->p2>=0 && pOp->p2<p->nOp )((void) (0));
94540 assert( pOp->p3>=0 && pOp->p3<p->nOp )((void) (0));
94541 pOut = &aMem[pOp->p1];
94542 assert( !VdbeMemDynamic(pOut) )((void) (0));
94543 pOut->u.i = pOp->p3 - 1;
94544 pOut->flags = MEM_Int0x0004;
94545 if( pOp->p2==0 ) break;
94546
94547 /* Most jump operations do a goto to this spot in order to update
94548 ** the pOp pointer. */
94549jump_to_p2:
94550 assert( pOp->p2>0 )((void) (0)); /* There are never any jumps to instruction 0 */
94551 assert( pOp->p2<p->nOp )((void) (0)); /* Jumps must be in range */
94552 pOp = &aOp[pOp->p2 - 1];
94553 break;
94554}
94555
94556/* Opcode: EndCoroutine P1 * * * *
94557**
94558** The instruction at the address in register P1 is a Yield.
94559** Jump to the P2 parameter of that Yield.
94560** After the jump, the value register P1 is left with a value
94561** such that subsequent OP_Yields go back to the this same
94562** OP_EndCoroutine instruction.
94563**
94564** See also: InitCoroutine
94565*/
94566case OP_EndCoroutine68: { /* in1 */
94567 VdbeOp *pCaller;
94568 pIn1 = &aMem[pOp->p1];
94569 assert( pIn1->flags==MEM_Int )((void) (0));
94570 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp )((void) (0));
94571 pCaller = &aOp[pIn1->u.i];
94572 assert( pCaller->opcode==OP_Yield )((void) (0));
94573 assert( pCaller->p2>=0 && pCaller->p2<p->nOp )((void) (0));
94574 pIn1->u.i = (int)(pOp - p->aOp) - 1;
94575 pOp = &aOp[pCaller->p2 - 1];
94576 break;
94577}
94578
94579/* Opcode: Yield P1 P2 * * *
94580**
94581** Swap the program counter with the value in register P1. This
94582** has the effect of yielding to a coroutine.
94583**
94584** If the coroutine that is launched by this instruction ends with
94585** Yield or Return then continue to the next instruction. But if
94586** the coroutine launched by this instruction ends with
94587** EndCoroutine, then jump to P2 rather than continuing with the
94588** next instruction.
94589**
94590** See also: InitCoroutine
94591*/
94592case OP_Yield12: { /* in1, jump0 */
94593 int pcDest;
94594 pIn1 = &aMem[pOp->p1];
94595 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
94596 pIn1->flags = MEM_Int0x0004;
94597 pcDest = (int)pIn1->u.i;
94598 pIn1->u.i = (int)(pOp - aOp);
94599 REGISTER_TRACE(pOp->p1, pIn1);
94600 pOp = &aOp[pcDest];
94601 break;
94602}
94603
94604/* Opcode: HaltIfNull P1 P2 P3 P4 P5
94605** Synopsis: if r[P3]=null halt
94606**
94607** Check the value in register P3. If it is NULL then Halt using
94608** parameter P1, P2, and P4 as if this were a Halt instruction. If the
94609** value in register P3 is not NULL, then this routine is a no-op.
94610** The P5 parameter should be 1.
94611*/
94612case OP_HaltIfNull69: { /* in3 */
94613 pIn3 = &aMem[pOp->p3];
94614#ifdef SQLITE_DEBUG
94615 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
94616#endif
94617 if( (pIn3->flags & MEM_Null0x0001)==0 ) break;
94618 /* Fall through into OP_Halt */
94619 /* no break */ deliberate_fall_through__attribute__((fallthrough));
94620}
94621
94622/* Opcode: Halt P1 P2 P3 P4 P5
94623**
94624** Exit immediately. All open cursors, etc are closed
94625** automatically.
94626**
94627** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
94628** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
94629** For errors, it can be some other value. If P1!=0 then P2 will determine
94630** whether or not to rollback the current transaction. Do not rollback
94631** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
94632** then back out all changes that have occurred during this execution of the
94633** VDBE, but do not rollback the transaction.
94634**
94635** If P3 is not zero and P4 is NULL, then P3 is a register that holds the
94636** text of an error message.
94637**
94638** If P3 is zero and P4 is not null then the error message string is held
94639** in P4.
94640**
94641** P5 is a value between 1 and 4, inclusive, then the P4 error message
94642** string is modified as follows:
94643**
94644** 1: NOT NULL constraint failed: P4
94645** 2: UNIQUE constraint failed: P4
94646** 3: CHECK constraint failed: P4
94647** 4: FOREIGN KEY constraint failed: P4
94648**
94649** If P3 is zero and P5 is not zero and P4 is NULL, then everything after
94650** the ":" is omitted.
94651**
94652** There is an implied "Halt 0 0 0" instruction inserted at the very end of
94653** every program. So a jump past the last instruction of the program
94654** is the same as executing Halt.
94655*/
94656case OP_Halt70: {
94657 VdbeFrame *pFrame;
94658 int pcx;
94659
94660#ifdef SQLITE_DEBUG
94661 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
94662#endif
94663 assert( pOp->p4type==P4_NOTUSED((void) (0))
94664 || pOp->p4type==P4_STATIC((void) (0))
94665 || pOp->p4type==P4_DYNAMIC )((void) (0));
94666
94667 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates
94668 ** something is wrong with the code generator. Raise an assertion in order
94669 ** to bring this to the attention of fuzzers and other testing tools. */
94670 assert( pOp->p1!=SQLITE_INTERNAL )((void) (0));
94671
94672 if( p->pFrame && pOp->p1==SQLITE_OK0 ){
94673 /* Halt the sub-program. Return control to the parent frame. */
94674 pFrame = p->pFrame;
94675 p->pFrame = pFrame->pParent;
94676 p->nFrame--;
94677 sqlite3VdbeSetChanges(db, p->nChange);
94678 pcx = sqlite3VdbeFrameRestore(pFrame);
94679 if( pOp->p2==OE_Ignore4 ){
94680 /* Instruction pcx is the OP_Program that invoked the sub-program
94681 ** currently being halted. If the p2 instruction of this OP_Halt
94682 ** instruction is set to OE_Ignore, then the sub-program is throwing
94683 ** an IGNORE exception. In this case jump to the address specified
94684 ** as the p2 of the calling OP_Program. */
94685 pcx = p->aOp[pcx].p2-1;
94686 }
94687 aOp = p->aOp;
94688 aMem = p->aMem;
94689 pOp = &aOp[pcx];
94690 break;
94691 }
94692 p->rc = pOp->p1;
94693 p->errorAction = (u8)pOp->p2;
94694 assert( pOp->p5<=4 )((void) (0));
94695 if( p->rc ){
94696 if( pOp->p3>0 && pOp->p4type==P4_NOTUSED0 ){
94697 const char *zErr;
94698 assert( pOp->p3<=(p->nMem + 1 - p->nCursor) )((void) (0));
94699 zErr = sqlite3ValueText(&aMem[pOp->p3], SQLITE_UTF81);
94700 sqlite3VdbeError(p, "%s", zErr);
94701 }else if( pOp->p5 ){
94702 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
94703 "FOREIGN KEY" };
94704 testcase( pOp->p5==1 );
94705 testcase( pOp->p5==2 );
94706 testcase( pOp->p5==3 );
94707 testcase( pOp->p5==4 );
94708 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
94709 if( pOp->p4.z ){
94710 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
94711 }
94712 }else{
94713 sqlite3VdbeError(p, "%s", pOp->p4.z);
94714 }
94715 pcx = (int)(pOp - aOp);
94716 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
94717 }
94718 rc = sqlite3VdbeHalt(p);
94719 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR )((void) (0));
94720 if( rc==SQLITE_BUSY5 ){
94721 p->rc = SQLITE_BUSY5;
94722 }else{
94723 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT )((void) (0));
94724 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 )((void) (0));
94725 rc = p->rc ? SQLITE_ERROR1 : SQLITE_DONE101;
94726 }
94727 goto vdbe_return;
94728}
94729
94730/* Opcode: Integer P1 P2 * * *
94731** Synopsis: r[P2]=P1
94732**
94733** The 32-bit integer value P1 is written into register P2.
94734*/
94735case OP_Integer71: { /* out2 */
94736 pOut = out2Prerelease(p, pOp);
94737 pOut->u.i = pOp->p1;
94738 break;
94739}
94740
94741/* Opcode: Int64 * P2 * P4 *
94742** Synopsis: r[P2]=P4
94743**
94744** P4 is a pointer to a 64-bit integer value.
94745** Write that value into register P2.
94746*/
94747case OP_Int6472: { /* out2 */
94748 pOut = out2Prerelease(p, pOp);
94749 assert( pOp->p4.pI64!=0 )((void) (0));
94750 pOut->u.i = *pOp->p4.pI64;
94751 break;
94752}
94753
94754#ifndef SQLITE_OMIT_FLOATING_POINT
94755/* Opcode: Real * P2 * P4 *
94756** Synopsis: r[P2]=P4
94757**
94758** P4 is a pointer to a 64-bit floating point value.
94759** Write that value into register P2.
94760*/
94761case OP_Real154: { /* same as TK_FLOAT, out2 */
94762 pOut = out2Prerelease(p, pOp);
94763 pOut->flags = MEM_Real0x0008;
94764 assert( !sqlite3IsNaN(*pOp->p4.pReal) )((void) (0));
94765 pOut->u.r = *pOp->p4.pReal;
94766 break;
94767}
94768#endif
94769
94770/* Opcode: String8 * P2 * P4 *
94771** Synopsis: r[P2]='P4'
94772**
94773** P4 points to a nul terminated UTF-8 string. This opcode is transformed
94774** into a String opcode before it is executed for the first time. During
94775** this transformation, the length of string P4 is computed and stored
94776** as the P1 parameter.
94777*/
94778case OP_String8118: { /* same as TK_STRING, out2 */
94779 assert( pOp->p4.z!=0 )((void) (0));
94780 pOut = out2Prerelease(p, pOp);
94781 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
94782
94783#ifndef SQLITE_OMIT_UTF16
94784 if( encoding!=SQLITE_UTF81 ){
94785 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
94786 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG )((void) (0));
94787 if( rc ) goto too_big;
94788 if( SQLITE_OK0!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
94789 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z )((void) (0));
94790 assert( VdbeMemDynamic(pOut)==0 )((void) (0));
94791 pOut->szMalloc = 0;
94792 pOut->flags |= MEM_Static0x2000;
94793 if( pOp->p4type==P4_DYNAMIC(-6) ){
94794 sqlite3DbFree(db, pOp->p4.z);
94795 }
94796 pOp->p4type = P4_DYNAMIC(-6);
94797 pOp->p4.z = pOut->z;
94798 pOp->p1 = pOut->n;
94799 }
94800#endif
94801 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
94802 goto too_big;
94803 }
94804 pOp->opcode = OP_String73;
94805 assert( rc==SQLITE_OK )((void) (0));
94806 /* Fall through to the next case, OP_String */
94807 /* no break */ deliberate_fall_through__attribute__((fallthrough));
94808}
94809
94810/* Opcode: String P1 P2 P3 P4 P5
94811** Synopsis: r[P2]='P4' (len=P1)
94812**
94813** The string value P4 of length P1 (bytes) is stored in register P2.
94814**
94815** If P3 is not zero and the content of register P3 is equal to P5, then
94816** the datatype of the register P2 is converted to BLOB. The content is
94817** the same sequence of bytes, it is merely interpreted as a BLOB instead
94818** of a string, as if it had been CAST. In other words:
94819**
94820** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
94821*/
94822case OP_String73: { /* out2 */
94823 assert( pOp->p4.z!=0 )((void) (0));
94824 pOut = out2Prerelease(p, pOp);
94825 pOut->flags = MEM_Str0x0002|MEM_Static0x2000|MEM_Term0x0200;
94826 pOut->z = pOp->p4.z;
94827 pOut->n = pOp->p1;
94828 pOut->enc = encoding;
94829 UPDATE_MAX_BLOBSIZE(pOut);
94830#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
94831 if( pOp->p3>0 ){
94832 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
94833 pIn3 = &aMem[pOp->p3];
94834 assert( pIn3->flags & MEM_Int )((void) (0));
94835 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob0x0010|MEM_Static0x2000|MEM_Term0x0200;
94836 }
94837#endif
94838 break;
94839}
94840
94841/* Opcode: BeginSubrtn * P2 * * *
94842** Synopsis: r[P2]=NULL
94843**
94844** Mark the beginning of a subroutine that can be entered in-line
94845** or that can be called using OP_Gosub. The subroutine should
94846** be terminated by an OP_Return instruction that has a P1 operand that
94847** is the same as the P2 operand to this opcode and that has P3 set to 1.
94848** If the subroutine is entered in-line, then the OP_Return will simply
94849** fall through. But if the subroutine is entered using OP_Gosub, then
94850** the OP_Return will jump back to the first instruction after the OP_Gosub.
94851**
94852** This routine works by loading a NULL into the P2 register. When the
94853** return address register contains a NULL, the OP_Return instruction is
94854** a no-op that simply falls through to the next instruction (assuming that
94855** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
94856** entered in-line, then the OP_Return will cause in-line execution to
94857** continue. But if the subroutine is entered via OP_Gosub, then the
94858** OP_Return will cause a return to the address following the OP_Gosub.
94859**
94860** This opcode is identical to OP_Null. It has a different name
94861** only to make the byte code easier to read and verify.
94862*/
94863/* Opcode: Null P1 P2 P3 * *
94864** Synopsis: r[P2..P3]=NULL
94865**
94866** Write a NULL into registers P2. If P3 greater than P2, then also write
94867** NULL into register P3 and every register in between P2 and P3. If P3
94868** is less than P2 (typically P3 is zero) then only register P2 is
94869** set to NULL.
94870**
94871** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
94872** NULL values will not compare equal even if SQLITE_NULLEQ is set on
94873** OP_Ne or OP_Eq.
94874*/
94875case OP_BeginSubrtn74:
94876case OP_Null75: { /* out2 */
94877 int cnt;
94878 u16 nullFlag;
94879 pOut = out2Prerelease(p, pOp);
94880 cnt = pOp->p3-pOp->p2;
94881 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
94882 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null0x0001|MEM_Cleared0x0100) : MEM_Null0x0001;
94883 pOut->n = 0;
94884#ifdef SQLITE_DEBUG
94885 pOut->uTemp = 0;
94886#endif
94887 while( cnt>0 ){
94888 pOut++;
94889 memAboutToChange(p, pOut);
94890 sqlite3VdbeMemSetNull(pOut);
94891 pOut->flags = nullFlag;
94892 pOut->n = 0;
94893 cnt--;
94894 }
94895 break;
94896}
94897
94898/* Opcode: SoftNull P1 * * * *
94899** Synopsis: r[P1]=NULL
94900**
94901** Set register P1 to have the value NULL as seen by the OP_MakeRecord
94902** instruction, but do not free any string or blob memory associated with
94903** the register, so that if the value was a string or blob that was
94904** previously copied using OP_SCopy, the copies will continue to be valid.
94905*/
94906case OP_SoftNull76: {
94907 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
94908 pOut = &aMem[pOp->p1];
94909 pOut->flags = (pOut->flags&~(MEM_Undefined0x0000|MEM_AffMask0x003f))|MEM_Null0x0001;
94910 break;
94911}
94912
94913/* Opcode: Blob P1 P2 * P4 *
94914** Synopsis: r[P2]=P4 (len=P1)
94915**
94916** P4 points to a blob of data P1 bytes long. Store this
94917** blob in register P2. If P4 is a NULL pointer, then construct
94918** a zero-filled blob that is P1 bytes long in P2.
94919*/
94920case OP_Blob77: { /* out2 */
94921 assert( pOp->p1 <= SQLITE_MAX_LENGTH )((void) (0));
94922 pOut = out2Prerelease(p, pOp);
94923 if( pOp->p4.z==0 ){
94924 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
94925 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem;
94926 }else{
94927 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
94928 }
94929 pOut->enc = encoding;
94930 UPDATE_MAX_BLOBSIZE(pOut);
94931 break;
94932}
94933
94934/* Opcode: Variable P1 P2 * * *
94935** Synopsis: r[P2]=parameter(P1)
94936**
94937** Transfer the values of bound parameter P1 into register P2
94938*/
94939case OP_Variable78: { /* out2 */
94940 Mem *pVar; /* Value being transferred */
94941
94942 assert( pOp->p1>0 && pOp->p1<=p->nVar )((void) (0));
94943 pVar = &p->aVar[pOp->p1 - 1];
94944 if( sqlite3VdbeMemTooBig(pVar) ){
94945 goto too_big;
94946 }
94947 pOut = &aMem[pOp->p2];
94948 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x8000|0x1000))!=0) ) sqlite3VdbeMemSetNull(pOut);
94949 memcpy(pOut, pVar, MEMCELLSIZE__builtin_offsetof(Mem, db));
94950 pOut->flags &= ~(MEM_Dyn0x1000|MEM_Ephem0x4000);
94951 pOut->flags |= MEM_Static0x2000|MEM_FromBind0x0040;
94952 UPDATE_MAX_BLOBSIZE(pOut);
94953 break;
94954}
94955
94956/* Opcode: Move P1 P2 P3 * *
94957** Synopsis: r[P2@P3]=r[P1@P3]
94958**
94959** Move the P3 values in register P1..P1+P3-1 over into
94960** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
94961** left holding a NULL. It is an error for register ranges
94962** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
94963** for P3 to be less than 1.
94964*/
94965case OP_Move79: {
94966 int n; /* Number of registers left to copy */
94967 int p1; /* Register to copy from */
94968 int p2; /* Register to copy to */
94969
94970 n = pOp->p3;
94971 p1 = pOp->p1;
94972 p2 = pOp->p2;
94973 assert( n>0 && p1>0 && p2>0 )((void) (0));
94974 assert( p1+n<=p2 || p2+n<=p1 )((void) (0));
94975
94976 pIn1 = &aMem[p1];
94977 pOut = &aMem[p2];
94978 do{
94979 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
94980 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
94981 assert( memIsValid(pIn1) )((void) (0));
94982 memAboutToChange(p, pOut);
94983 sqlite3VdbeMemMove(pOut, pIn1);
94984#ifdef SQLITE_DEBUG
94985 pIn1->pScopyFrom = 0;
94986 { int i;
94987 for(i=1; i<p->nMem; i++){
94988 if( aMem[i].pScopyFrom==pIn1 ){
94989 aMem[i].pScopyFrom = pOut;
94990 }
94991 }
94992 }
94993#endif
94994 Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
94995 REGISTER_TRACE(p2++, pOut);
94996 pIn1++;
94997 pOut++;
94998 }while( --n );
94999 break;
95000}
95001
95002/* Opcode: Copy P1 P2 P3 * P5
95003** Synopsis: r[P2@P3+1]=r[P1@P3+1]
95004**
95005** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
95006**
95007** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
95008** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
95009** be merged. The 0x0001 bit is used by the query planner and does not
95010** come into play during query execution.
95011**
95012** This instruction makes a deep copy of the value. A duplicate
95013** is made of any string or blob constant. See also OP_SCopy.
95014*/
95015case OP_Copy80: {
95016 int n;
95017
95018 n = pOp->p3;
95019 pIn1 = &aMem[pOp->p1];
95020 pOut = &aMem[pOp->p2];
95021 assert( pOut!=pIn1 )((void) (0));
95022 while( 1 ){
95023 memAboutToChange(p, pOut);
95024 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x4000);
95025 Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
95026 if( (pOut->flags & MEM_Subtype0x0800)!=0 && (pOp->p5 & 0x0002)!=0 ){
95027 pOut->flags &= ~MEM_Subtype0x0800;
95028 }
95029#ifdef SQLITE_DEBUG
95030 pOut->pScopyFrom = 0;
95031#endif
95032 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
95033 if( (n--)==0 ) break;
95034 pOut++;
95035 pIn1++;
95036 }
95037 break;
95038}
95039
95040/* Opcode: SCopy P1 P2 * * *
95041** Synopsis: r[P2]=r[P1]
95042**
95043** Make a shallow copy of register P1 into register P2.
95044**
95045** This instruction makes a shallow copy of the value. If the value
95046** is a string or blob, then the copy is only a pointer to the
95047** original and hence if the original changes so will the copy.
95048** Worse, if the original is deallocated, the copy becomes invalid.
95049** Thus the program must guarantee that the original will not change
95050** during the lifetime of the copy. Use OP_Copy to make a complete
95051** copy.
95052*/
95053case OP_SCopy81: { /* out2 */
95054 pIn1 = &aMem[pOp->p1];
95055 pOut = &aMem[pOp->p2];
95056 assert( pOut!=pIn1 )((void) (0));
95057 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x4000);
95058#ifdef SQLITE_DEBUG
95059 pOut->pScopyFrom = pIn1;
95060 pOut->mScopyFlags = pIn1->flags;
95061#endif
95062 break;
95063}
95064
95065/* Opcode: IntCopy P1 P2 * * *
95066** Synopsis: r[P2]=r[P1]
95067**
95068** Transfer the integer value held in register P1 into register P2.
95069**
95070** This is an optimized version of SCopy that works only for integer
95071** values.
95072*/
95073case OP_IntCopy82: { /* out2 */
95074 pIn1 = &aMem[pOp->p1];
95075 assert( (pIn1->flags & MEM_Int)!=0 )((void) (0));
95076 pOut = &aMem[pOp->p2];
95077 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
95078 break;
95079}
95080
95081/* Opcode: FkCheck * * * * *
95082**
95083** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
95084** foreign key constraint violations. If there are no foreign key
95085** constraint violations, this is a no-op.
95086**
95087** FK constraint violations are also checked when the prepared statement
95088** exits. This opcode is used to raise foreign key constraint errors prior
95089** to returning results such as a row change count or the result of a
95090** RETURNING clause.
95091*/
95092case OP_FkCheck83: {
95093 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK0 ){
95094 goto abort_due_to_error;
95095 }
95096 break;
95097}
95098
95099/* Opcode: ResultRow P1 P2 * * *
95100** Synopsis: output=r[P1@P2]
95101**
95102** The registers P1 through P1+P2-1 contain a single row of
95103** results. This opcode causes the sqlite3_step() call to terminate
95104** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
95105** structure to provide access to the r(P1)..r(P1+P2-1) values as
95106** the result row.
95107*/
95108case OP_ResultRow84: {
95109 assert( p->nResColumn==pOp->p2 )((void) (0));
95110 assert( pOp->p1>0 || CORRUPT_DB )((void) (0));
95111 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
95112
95113 p->cacheCtr = (p->cacheCtr + 2)|1;
95114 p->pResultRow = &aMem[pOp->p1];
95115#ifdef SQLITE_DEBUG
95116 {
95117 Mem *pMem = p->pResultRow;
95118 int i;
95119 for(i=0; i<pOp->p2; i++){
95120 assert( memIsValid(&pMem[i]) )((void) (0));
95121 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
95122 /* The registers in the result will not be used again when the
95123 ** prepared statement restarts. This is because sqlite3_column()
95124 ** APIs might have caused type conversions of made other changes to
95125 ** the register values. Therefore, we can go ahead and break any
95126 ** OP_SCopy dependencies. */
95127 pMem[i].pScopyFrom = 0;
95128 }
95129 }
95130#endif
95131 if( db->mallocFailed ) goto no_mem;
95132 if( db->mTrace & SQLITE_TRACE_ROW0x04 ){
95133 db->trace.xV2(SQLITE_TRACE_ROW0x04, db->pTraceArg, p, 0);
95134 }
95135 p->pc = (int)(pOp - aOp) + 1;
95136 rc = SQLITE_ROW100;
95137 goto vdbe_return;
95138}
95139
95140/* Opcode: Concat P1 P2 P3 * *
95141** Synopsis: r[P3]=r[P2]+r[P1]
95142**
95143** Add the text in register P1 onto the end of the text in
95144** register P2 and store the result in register P3.
95145** If either the P1 or P2 text are NULL then store NULL in P3.
95146**
95147** P3 = P2 || P1
95148**
95149** It is illegal for P1 and P3 to be the same register. Sometimes,
95150** if P3 is the same register as P2, the implementation is able
95151** to avoid a memcpy().
95152*/
95153case OP_Concat112: { /* same as TK_CONCAT, in1, in2, out3 */
95154 i64 nByte; /* Total size of the output string or blob */
95155 u16 flags1; /* Initial flags for P1 */
95156 u16 flags2; /* Initial flags for P2 */
95157
95158 pIn1 = &aMem[pOp->p1];
95159 pIn2 = &aMem[pOp->p2];
95160 pOut = &aMem[pOp->p3];
95161 testcase( pOut==pIn2 );
95162 assert( pIn1!=pOut )((void) (0));
95163 flags1 = pIn1->flags;
95164 testcase( flags1 & MEM_Null );
95165 testcase( pIn2->flags & MEM_Null );
95166 if( (flags1 | pIn2->flags) & MEM_Null0x0001 ){
95167 sqlite3VdbeMemSetNull(pOut);
95168 break;
95169 }
95170 if( (flags1 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
95171 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
95172 flags1 = pIn1->flags & ~MEM_Str0x0002;
95173 }else if( (flags1 & MEM_Zero0x0400)!=0 ){
95174 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
95175 flags1 = pIn1->flags & ~MEM_Str0x0002;
95176 }
95177 flags2 = pIn2->flags;
95178 if( (flags2 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
95179 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
95180 flags2 = pIn2->flags & ~MEM_Str0x0002;
95181 }else if( (flags2 & MEM_Zero0x0400)!=0 ){
95182 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
95183 flags2 = pIn2->flags & ~MEM_Str0x0002;
95184 }
95185 nByte = pIn1->n + pIn2->n;
95186 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
95187 goto too_big;
95188 }
95189 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
95190 goto no_mem;
95191 }
95192 MemSetTypeFlag(pOut, MEM_Str)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0002
)
;
95193 if( pOut!=pIn2 ){
95194 memcpy(pOut->z, pIn2->z, pIn2->n);
95195 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) )((void) (0));
95196 pIn2->flags = flags2;
95197 }
95198 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
95199 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
95200 pIn1->flags = flags1;
95201 if( encoding>SQLITE_UTF81 ) nByte &= ~1;
95202 pOut->z[nByte]=0;
95203 pOut->z[nByte+1] = 0;
95204 pOut->flags |= MEM_Term0x0200;
95205 pOut->n = (int)nByte;
95206 pOut->enc = encoding;
95207 UPDATE_MAX_BLOBSIZE(pOut);
95208 break;
95209}
95210
95211/* Opcode: Add P1 P2 P3 * *
95212** Synopsis: r[P3]=r[P1]+r[P2]
95213**
95214** Add the value in register P1 to the value in register P2
95215** and store the result in register P3.
95216** If either input is NULL, the result is NULL.
95217*/
95218/* Opcode: Multiply P1 P2 P3 * *
95219** Synopsis: r[P3]=r[P1]*r[P2]
95220**
95221**
95222** Multiply the value in register P1 by the value in register P2
95223** and store the result in register P3.
95224** If either input is NULL, the result is NULL.
95225*/
95226/* Opcode: Subtract P1 P2 P3 * *
95227** Synopsis: r[P3]=r[P2]-r[P1]
95228**
95229** Subtract the value in register P1 from the value in register P2
95230** and store the result in register P3.
95231** If either input is NULL, the result is NULL.
95232*/
95233/* Opcode: Divide P1 P2 P3 * *
95234** Synopsis: r[P3]=r[P2]/r[P1]
95235**
95236** Divide the value in register P1 by the value in register P2
95237** and store the result in register P3 (P3=P2/P1). If the value in
95238** register P1 is zero, then the result is NULL. If either input is
95239** NULL, the result is NULL.
95240*/
95241/* Opcode: Remainder P1 P2 P3 * *
95242** Synopsis: r[P3]=r[P2]%r[P1]
95243**
95244** Compute the remainder after integer register P2 is divided by
95245** register P1 and store the result in register P3.
95246** If the value in register P1 is zero the result is NULL.
95247** If either operand is NULL, the result is NULL.
95248*/
95249case OP_Add107: /* same as TK_PLUS, in1, in2, out3 */
95250case OP_Subtract108: /* same as TK_MINUS, in1, in2, out3 */
95251case OP_Multiply109: /* same as TK_STAR, in1, in2, out3 */
95252case OP_Divide110: /* same as TK_SLASH, in1, in2, out3 */
95253case OP_Remainder111: { /* same as TK_REM, in1, in2, out3 */
95254 u16 type1; /* Numeric type of left operand */
95255 u16 type2; /* Numeric type of right operand */
95256 i64 iA; /* Integer value of left operand */
95257 i64 iB; /* Integer value of right operand */
95258 double rA; /* Real value of left operand */
95259 double rB; /* Real value of right operand */
95260
95261 pIn1 = &aMem[pOp->p1];
95262 type1 = pIn1->flags;
95263 pIn2 = &aMem[pOp->p2];
95264 type2 = pIn2->flags;
95265 pOut = &aMem[pOp->p3];
95266 if( (type1 & type2 & MEM_Int0x0004)!=0 ){
95267int_math:
95268 iA = pIn1->u.i;
95269 iB = pIn2->u.i;
95270 switch( pOp->opcode ){
95271 case OP_Add107: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
95272 case OP_Subtract108: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
95273 case OP_Multiply109: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
95274 case OP_Divide110: {
95275 if( iA==0 ) goto arithmetic_result_is_null;
95276 if( iA==-1 && iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) goto fp_math;
95277 iB /= iA;
95278 break;
95279 }
95280 default: {
95281 if( iA==0 ) goto arithmetic_result_is_null;
95282 if( iA==-1 ) iA = 1;
95283 iB %= iA;
95284 break;
95285 }
95286 }
95287 pOut->u.i = iB;
95288 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
95289 }else if( ((type1 | type2) & MEM_Null0x0001)!=0 ){
95290 goto arithmetic_result_is_null;
95291 }else{
95292 type1 = numericType(pIn1);
95293 type2 = numericType(pIn2);
95294 if( (type1 & type2 & MEM_Int0x0004)!=0 ) goto int_math;
95295fp_math:
95296 rA = sqlite3VdbeRealValue(pIn1);
95297 rB = sqlite3VdbeRealValue(pIn2);
95298 switch( pOp->opcode ){
95299 case OP_Add107: rB += rA; break;
95300 case OP_Subtract108: rB -= rA; break;
95301 case OP_Multiply109: rB *= rA; break;
95302 case OP_Divide110: {
95303 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
95304 if( rA==(double)0 ) goto arithmetic_result_is_null;
95305 rB /= rA;
95306 break;
95307 }
95308 default: {
95309 iA = sqlite3VdbeIntValue(pIn1);
95310 iB = sqlite3VdbeIntValue(pIn2);
95311 if( iA==0 ) goto arithmetic_result_is_null;
95312 if( iA==-1 ) iA = 1;
95313 rB = (double)(iB % iA);
95314 break;
95315 }
95316 }
95317#ifdef SQLITE_OMIT_FLOATING_POINT
95318 pOut->u.i = rB;
95319 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
95320#else
95321 if( sqlite3IsNaN(rB) ){
95322 goto arithmetic_result_is_null;
95323 }
95324 pOut->u.r = rB;
95325 MemSetTypeFlag(pOut, MEM_Real)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0008
)
;
95326#endif
95327 }
95328 break;
95329
95330arithmetic_result_is_null:
95331 sqlite3VdbeMemSetNull(pOut);
95332 break;
95333}
95334
95335/* Opcode: CollSeq P1 * * P4
95336**
95337** P4 is a pointer to a CollSeq object. If the next call to a user function
95338** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
95339** be returned. This is used by the built-in min(), max() and nullif()
95340** functions.
95341**
95342** If P1 is not zero, then it is a register that a subsequent min() or
95343** max() aggregate will set to 1 if the current row is not the minimum or
95344** maximum. The P1 register is initialized to 0 by this instruction.
95345**
95346** The interface used by the implementation of the aforementioned functions
95347** to retrieve the collation sequence set by this opcode is not available
95348** publicly. Only built-in functions have access to this feature.
95349*/
95350case OP_CollSeq85: {
95351 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
95352 if( pOp->p1 ){
95353 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
95354 }
95355 break;
95356}
95357
95358/* Opcode: BitAnd P1 P2 P3 * *
95359** Synopsis: r[P3]=r[P1]&r[P2]
95360**
95361** Take the bit-wise AND of the values in register P1 and P2 and
95362** store the result in register P3.
95363** If either input is NULL, the result is NULL.
95364*/
95365/* Opcode: BitOr P1 P2 P3 * *
95366** Synopsis: r[P3]=r[P1]|r[P2]
95367**
95368** Take the bit-wise OR of the values in register P1 and P2 and
95369** store the result in register P3.
95370** If either input is NULL, the result is NULL.
95371*/
95372/* Opcode: ShiftLeft P1 P2 P3 * *
95373** Synopsis: r[P3]=r[P2]<<r[P1]
95374**
95375** Shift the integer value in register P2 to the left by the
95376** number of bits specified by the integer in register P1.
95377** Store the result in register P3.
95378** If either input is NULL, the result is NULL.
95379*/
95380/* Opcode: ShiftRight P1 P2 P3 * *
95381** Synopsis: r[P3]=r[P2]>>r[P1]
95382**
95383** Shift the integer value in register P2 to the right by the
95384** number of bits specified by the integer in register P1.
95385** Store the result in register P3.
95386** If either input is NULL, the result is NULL.
95387*/
95388case OP_BitAnd103: /* same as TK_BITAND, in1, in2, out3 */
95389case OP_BitOr104: /* same as TK_BITOR, in1, in2, out3 */
95390case OP_ShiftLeft105: /* same as TK_LSHIFT, in1, in2, out3 */
95391case OP_ShiftRight106: { /* same as TK_RSHIFT, in1, in2, out3 */
95392 i64 iA;
95393 u64 uA;
95394 i64 iB;
95395 u8 op;
95396
95397 pIn1 = &aMem[pOp->p1];
95398 pIn2 = &aMem[pOp->p2];
95399 pOut = &aMem[pOp->p3];
95400 if( (pIn1->flags | pIn2->flags) & MEM_Null0x0001 ){
95401 sqlite3VdbeMemSetNull(pOut);
95402 break;
95403 }
95404 iA = sqlite3VdbeIntValue(pIn2);
95405 iB = sqlite3VdbeIntValue(pIn1);
95406 op = pOp->opcode;
95407 if( op==OP_BitAnd103 ){
95408 iA &= iB;
95409 }else if( op==OP_BitOr104 ){
95410 iA |= iB;
95411 }else if( iB!=0 ){
95412 assert( op==OP_ShiftRight || op==OP_ShiftLeft )((void) (0));
95413
95414 /* If shifting by a negative amount, shift in the other direction */
95415 if( iB<0 ){
95416 assert( OP_ShiftRight==OP_ShiftLeft+1 )((void) (0));
95417 op = 2*OP_ShiftLeft105 + 1 - op;
95418 iB = iB>(-64) ? -iB : 64;
95419 }
95420
95421 if( iB>=64 ){
95422 iA = (iA>=0 || op==OP_ShiftLeft105) ? 0 : -1;
95423 }else{
95424 memcpy(&uA, &iA, sizeof(uA));
95425 if( op==OP_ShiftLeft105 ){
95426 uA <<= iB;
95427 }else{
95428 uA >>= iB;
95429 /* Sign-extend on a right shift of a negative number */
95430 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
95431 }
95432 memcpy(&iA, &uA, sizeof(iA));
95433 }
95434 }
95435 pOut->u.i = iA;
95436 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
95437 break;
95438}
95439
95440/* Opcode: AddImm P1 P2 * * *
95441** Synopsis: r[P1]=r[P1]+P2
95442**
95443** Add the constant P2 to the value in register P1.
95444** The result is always an integer.
95445**
95446** To force any register to be an integer, just add 0.
95447*/
95448case OP_AddImm86: { /* in1 */
95449 pIn1 = &aMem[pOp->p1];
95450 memAboutToChange(p, pIn1);
95451 sqlite3VdbeMemIntegerify(pIn1);
95452 *(u64*)&pIn1->u.i += (u64)pOp->p2;
95453 break;
95454}
95455
95456/* Opcode: MustBeInt P1 P2 * * *
95457**
95458** Force the value in register P1 to be an integer. If the value
95459** in P1 is not an integer and cannot be converted into an integer
95460** without data loss, then jump immediately to P2, or if P2==0
95461** raise an SQLITE_MISMATCH exception.
95462*/
95463case OP_MustBeInt13: { /* jump0, in1 */
95464 pIn1 = &aMem[pOp->p1];
95465 if( (pIn1->flags & MEM_Int0x0004)==0 ){
95466 applyAffinity(pIn1, SQLITE_AFF_NUMERIC0x43, encoding);
95467 if( (pIn1->flags & MEM_Int0x0004)==0 ){
95468 VdbeBranchTaken(1, 2);
95469 if( pOp->p2==0 ){
95470 rc = SQLITE_MISMATCH20;
95471 goto abort_due_to_error;
95472 }else{
95473 goto jump_to_p2;
95474 }
95475 }
95476 }
95477 VdbeBranchTaken(0, 2);
95478 MemSetTypeFlag(pIn1, MEM_Int)((pIn1)->flags = ((pIn1)->flags&~(0x0dbf|0x0400))|0x0004
)
;
95479 break;
95480}
95481
95482#ifndef SQLITE_OMIT_FLOATING_POINT
95483/* Opcode: RealAffinity P1 * * * *
95484**
95485** If register P1 holds an integer convert it to a real value.
95486**
95487** This opcode is used when extracting information from a column that
95488** has REAL affinity. Such column values may still be stored as
95489** integers, for space efficiency, but after extraction we want them
95490** to have only a real value.
95491*/
95492case OP_RealAffinity87: { /* in1 */
95493 pIn1 = &aMem[pOp->p1];
95494 if( pIn1->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
95495 testcase( pIn1->flags & MEM_Int );
95496 testcase( pIn1->flags & MEM_IntReal );
95497 sqlite3VdbeMemRealify(pIn1);
95498 REGISTER_TRACE(pOp->p1, pIn1);
95499 }
95500 break;
95501}
95502#endif
95503
95504#if !defined(SQLITE_OMIT_CAST) || !defined(SQLITE_OMIT_ANALYZE)
95505/* Opcode: Cast P1 P2 * * *
95506** Synopsis: affinity(r[P1])
95507**
95508** Force the value in register P1 to be the type defined by P2.
95509**
95510** <ul>
95511** <li> P2=='A' &rarr; BLOB
95512** <li> P2=='B' &rarr; TEXT
95513** <li> P2=='C' &rarr; NUMERIC
95514** <li> P2=='D' &rarr; INTEGER
95515** <li> P2=='E' &rarr; REAL
95516** </ul>
95517**
95518** A NULL value is not changed by this routine. It remains NULL.
95519*/
95520case OP_Cast88: { /* in1 */
95521 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL )((void) (0));
95522 testcase( pOp->p2==SQLITE_AFF_TEXT );
95523 testcase( pOp->p2==SQLITE_AFF_BLOB );
95524 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
95525 testcase( pOp->p2==SQLITE_AFF_INTEGER );
95526 testcase( pOp->p2==SQLITE_AFF_REAL );
95527 pIn1 = &aMem[pOp->p1];
95528 memAboutToChange(p, pIn1);
95529 rc = ExpandBlob(pIn1)(((pIn1)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn1)
:0)
;
95530 if( rc ) goto abort_due_to_error;
95531 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
95532 if( rc ) goto abort_due_to_error;
95533 UPDATE_MAX_BLOBSIZE(pIn1);
95534 REGISTER_TRACE(pOp->p1, pIn1);
95535 break;
95536}
95537#endif /* SQLITE_OMIT_CAST */
95538
95539/* Opcode: Eq P1 P2 P3 P4 P5
95540** Synopsis: IF r[P3]==r[P1]
95541**
95542** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
95543** jump to address P2.
95544**
95545** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
95546** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
95547** to coerce both inputs according to this affinity before the
95548** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
95549** affinity is used. Note that the affinity conversions are stored
95550** back into the input registers P1 and P3. So this opcode can cause
95551** persistent changes to registers P1 and P3.
95552**
95553** Once any conversions have taken place, and neither value is NULL,
95554** the values are compared. If both values are blobs then memcmp() is
95555** used to determine the results of the comparison. If both values
95556** are text, then the appropriate collating function specified in
95557** P4 is used to do the comparison. If P4 is not specified then
95558** memcmp() is used to compare text string. If both values are
95559** numeric, then a numeric comparison is used. If the two values
95560** are of different types, then numbers are considered less than
95561** strings and strings are considered less than blobs.
95562**
95563** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
95564** true or false and is never NULL. If both operands are NULL then the result
95565** of comparison is true. If either operand is NULL then the result is false.
95566** If neither operand is NULL the result is the same as it would be if
95567** the SQLITE_NULLEQ flag were omitted from P5.
95568**
95569** This opcode saves the result of comparison for use by the new
95570** OP_Jump opcode.
95571*/
95572/* Opcode: Ne P1 P2 P3 P4 P5
95573** Synopsis: IF r[P3]!=r[P1]
95574**
95575** This works just like the Eq opcode except that the jump is taken if
95576** the operands in registers P1 and P3 are not equal. See the Eq opcode for
95577** additional information.
95578*/
95579/* Opcode: Lt P1 P2 P3 P4 P5
95580** Synopsis: IF r[P3]<r[P1]
95581**
95582** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
95583** jump to address P2.
95584**
95585** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
95586** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
95587** bit is clear then fall through if either operand is NULL.
95588**
95589** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
95590** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
95591** to coerce both inputs according to this affinity before the
95592** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
95593** affinity is used. Note that the affinity conversions are stored
95594** back into the input registers P1 and P3. So this opcode can cause
95595** persistent changes to registers P1 and P3.
95596**
95597** Once any conversions have taken place, and neither value is NULL,
95598** the values are compared. If both values are blobs then memcmp() is
95599** used to determine the results of the comparison. If both values
95600** are text, then the appropriate collating function specified in
95601** P4 is used to do the comparison. If P4 is not specified then
95602** memcmp() is used to compare text string. If both values are
95603** numeric, then a numeric comparison is used. If the two values
95604** are of different types, then numbers are considered less than
95605** strings and strings are considered less than blobs.
95606**
95607** This opcode saves the result of comparison for use by the new
95608** OP_Jump opcode.
95609*/
95610/* Opcode: Le P1 P2 P3 P4 P5
95611** Synopsis: IF r[P3]<=r[P1]
95612**
95613** This works just like the Lt opcode except that the jump is taken if
95614** the content of register P3 is less than or equal to the content of
95615** register P1. See the Lt opcode for additional information.
95616*/
95617/* Opcode: Gt P1 P2 P3 P4 P5
95618** Synopsis: IF r[P3]>r[P1]
95619**
95620** This works just like the Lt opcode except that the jump is taken if
95621** the content of register P3 is greater than the content of
95622** register P1. See the Lt opcode for additional information.
95623*/
95624/* Opcode: Ge P1 P2 P3 P4 P5
95625** Synopsis: IF r[P3]>=r[P1]
95626**
95627** This works just like the Lt opcode except that the jump is taken if
95628** the content of register P3 is greater than or equal to the content of
95629** register P1. See the Lt opcode for additional information.
95630*/
95631case OP_Eq54: /* same as TK_EQ, jump, in1, in3 */
95632case OP_Ne53: /* same as TK_NE, jump, in1, in3 */
95633case OP_Lt57: /* same as TK_LT, jump, in1, in3 */
95634case OP_Le56: /* same as TK_LE, jump, in1, in3 */
95635case OP_Gt55: /* same as TK_GT, jump, in1, in3 */
95636case OP_Ge58: { /* same as TK_GE, jump, in1, in3 */
95637 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
95638 char affinity; /* Affinity to use for comparison */
95639 u16 flags1; /* Copy of initial value of pIn1->flags */
95640 u16 flags3; /* Copy of initial value of pIn3->flags */
95641
95642 pIn1 = &aMem[pOp->p1];
95643 pIn3 = &aMem[pOp->p3];
95644 flags1 = pIn1->flags;
95645 flags3 = pIn3->flags;
95646 if( (flags1 & flags3 & MEM_Int0x0004)!=0 ){
95647 /* Common case of comparison of two integers */
95648 if( pIn3->u.i > pIn1->u.i ){
95649 if( sqlite3aGTb[pOp->opcode] ){
95650 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95651 goto jump_to_p2;
95652 }
95653 iCompare = +1;
95654 VVA_ONLY( iCompareIsInit = 1; )
95655 }else if( pIn3->u.i < pIn1->u.i ){
95656 if( sqlite3aLTb[pOp->opcode] ){
95657 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95658 goto jump_to_p2;
95659 }
95660 iCompare = -1;
95661 VVA_ONLY( iCompareIsInit = 1; )
95662 }else{
95663 if( sqlite3aEQb[pOp->opcode] ){
95664 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95665 goto jump_to_p2;
95666 }
95667 iCompare = 0;
95668 VVA_ONLY( iCompareIsInit = 1; )
95669 }
95670 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95671 break;
95672 }
95673 if( (flags1 | flags3)&MEM_Null0x0001 ){
95674 /* One or both operands are NULL */
95675 if( pOp->p5 & SQLITE_NULLEQ0x80 ){
95676 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
95677 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
95678 ** or not both operands are null.
95679 */
95680 assert( (flags1 & MEM_Cleared)==0 )((void) (0));
95681 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB )((void) (0));
95682 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
95683 if( (flags1&flags3&MEM_Null0x0001)!=0
95684 && (flags3&MEM_Cleared0x0100)==0
95685 ){
95686 res = 0; /* Operands are equal */
95687 }else{
95688 res = ((flags3 & MEM_Null0x0001) ? -1 : +1); /* Operands are not equal */
95689 }
95690 }else{
95691 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
95692 ** then the result is always NULL.
95693 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
95694 */
95695 VdbeBranchTaken(2,3);
95696 if( pOp->p5 & SQLITE_JUMPIFNULL0x10 ){
95697 goto jump_to_p2;
95698 }
95699 iCompare = 1; /* Operands are not equal */
95700 VVA_ONLY( iCompareIsInit = 1; )
95701 break;
95702 }
95703 }else{
95704 /* Neither operand is NULL and we couldn't do the special high-speed
95705 ** integer comparison case. So do a general-case comparison. */
95706 affinity = pOp->p5 & SQLITE_AFF_MASK0x47;
95707 if( affinity>=SQLITE_AFF_NUMERIC0x43 ){
95708 if( (flags1 | flags3)&MEM_Str0x0002 ){
95709 if( (flags1 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
95710 applyNumericAffinity(pIn1,0);
95711 assert( flags3==pIn3->flags || CORRUPT_DB )((void) (0));
95712 flags3 = pIn3->flags;
95713 }
95714 if( (flags3 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
95715 applyNumericAffinity(pIn3,0);
95716 }
95717 }
95718 }else if( affinity==SQLITE_AFF_TEXT0x42 && ((flags1 | flags3) & MEM_Str0x0002)!=0 ){
95719 if( (flags1 & MEM_Str0x0002)!=0 ){
95720 pIn1->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
95721 }else if( (flags1&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
95722 testcase( pIn1->flags & MEM_Int );
95723 testcase( pIn1->flags & MEM_Real );
95724 testcase( pIn1->flags & MEM_IntReal );
95725 sqlite3VdbeMemStringify(pIn1, encoding, 1);
95726 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
95727 flags1 = (pIn1->flags & ~MEM_TypeMask0x0dbf) | (flags1 & MEM_TypeMask0x0dbf);
95728 if( NEVER(pIn1==pIn3)(pIn1==pIn3) ) flags3 = flags1 | MEM_Str0x0002;
95729 }
95730 if( (flags3 & MEM_Str0x0002)!=0 ){
95731 pIn3->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
95732 }else if( (flags3&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
95733 testcase( pIn3->flags & MEM_Int );
95734 testcase( pIn3->flags & MEM_Real );
95735 testcase( pIn3->flags & MEM_IntReal );
95736 sqlite3VdbeMemStringify(pIn3, encoding, 1);
95737 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
95738 flags3 = (pIn3->flags & ~MEM_TypeMask0x0dbf) | (flags3 & MEM_TypeMask0x0dbf);
95739 }
95740 }
95741 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 )((void) (0));
95742 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
95743 }
95744
95745 /* At this point, res is negative, zero, or positive if reg[P1] is
95746 ** less than, equal to, or greater than reg[P3], respectively. Compute
95747 ** the answer to this operator in res2, depending on what the comparison
95748 ** operator actually is. The next block of code depends on the fact
95749 ** that the 6 comparison operators are consecutive integers in this
95750 ** order: NE, EQ, GT, LE, LT, GE */
95751 assert( OP_Eq==OP_Ne+1 )((void) (0)); assert( OP_Gt==OP_Ne+2 )((void) (0)); assert( OP_Le==OP_Ne+3 )((void) (0));
95752 assert( OP_Lt==OP_Ne+4 )((void) (0)); assert( OP_Ge==OP_Ne+5 )((void) (0));
95753 if( res<0 ){
95754 res2 = sqlite3aLTb[pOp->opcode];
95755 }else if( res==0 ){
95756 res2 = sqlite3aEQb[pOp->opcode];
95757 }else{
95758 res2 = sqlite3aGTb[pOp->opcode];
95759 }
95760 iCompare = res;
95761 VVA_ONLY( iCompareIsInit = 1; )
95762
95763 /* Undo any changes made by applyAffinity() to the input registers. */
95764 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) )((void) (0));
95765 pIn3->flags = flags3;
95766 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
95767 pIn1->flags = flags1;
95768
95769 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95770 if( res2 ){
95771 goto jump_to_p2;
95772 }
95773 break;
95774}
95775
95776/* Opcode: ElseEq * P2 * * *
95777**
95778** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
95779** can be zero or more OP_ReleaseReg opcodes intervening, but no other
95780** opcodes are allowed to occur between this instruction and the previous
95781** OP_Lt or OP_Gt.
95782**
95783** If the result of an OP_Eq comparison on the same two operands as
95784** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If
95785** the result of an OP_Eq comparison on the two previous operands
95786** would have been false or NULL, then fall through.
95787*/
95788case OP_ElseEq59: { /* same as TK_ESCAPE, jump */
95789
95790#ifdef SQLITE_DEBUG
95791 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
95792 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
95793 int iAddr;
95794 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0)(iAddr>=0); iAddr--){
95795 if( aOp[iAddr].opcode==OP_ReleaseReg186 ) continue;
95796 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt )((void) (0));
95797 break;
95798 }
95799#endif /* SQLITE_DEBUG */
95800 assert( iCompareIsInit )((void) (0));
95801 VdbeBranchTaken(iCompare==0, 2);
95802 if( iCompare==0 ) goto jump_to_p2;
95803 break;
95804}
95805
95806
95807/* Opcode: Permutation * * * P4 *
95808**
95809** Set the permutation used by the OP_Compare operator in the next
95810** instruction. The permutation is stored in the P4 operand.
95811**
95812** The permutation is only valid for the next opcode which must be
95813** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
95814**
95815** The first integer in the P4 integer array is the length of the array
95816** and does not become part of the permutation.
95817*/
95818case OP_Permutation89: {
95819 assert( pOp->p4type==P4_INTARRAY )((void) (0));
95820 assert( pOp->p4.ai )((void) (0));
95821 assert( pOp[1].opcode==OP_Compare )((void) (0));
95822 assert( pOp[1].p5 & OPFLAG_PERMUTE )((void) (0));
95823 break;
95824}
95825
95826/* Opcode: Compare P1 P2 P3 P4 P5
95827** Synopsis: r[P1@P3] <-> r[P2@P3]
95828**
95829** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
95830** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
95831** the comparison for use by the next OP_Jump instruct.
95832**
95833** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
95834** determined by the most recent OP_Permutation operator. If the
95835** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
95836** order.
95837**
95838** P4 is a KeyInfo structure that defines collating sequences and sort
95839** orders for the comparison. The permutation applies to registers
95840** only. The KeyInfo elements are used sequentially.
95841**
95842** The comparison is a sort comparison, so NULLs compare equal,
95843** NULLs are less than numbers, numbers are less than strings,
95844** and strings are less than blobs.
95845**
95846** This opcode must be immediately followed by an OP_Jump opcode.
95847*/
95848case OP_Compare90: {
95849 int n;
95850 int i;
95851 int p1;
95852 int p2;
95853 const KeyInfo *pKeyInfo;
95854 u32 idx;
95855 CollSeq *pColl; /* Collating sequence to use on this term */
95856 int bRev; /* True for DESCENDING sort order */
95857 u32 *aPermute; /* The permutation */
95858
95859 if( (pOp->p5 & OPFLAG_PERMUTE0x01)==0 ){
95860 aPermute = 0;
95861 }else{
95862 assert( pOp>aOp )((void) (0));
95863 assert( pOp[-1].opcode==OP_Permutation )((void) (0));
95864 assert( pOp[-1].p4type==P4_INTARRAY )((void) (0));
95865 aPermute = pOp[-1].p4.ai + 1;
95866 assert( aPermute!=0 )((void) (0));
95867 }
95868 n = pOp->p3;
95869 pKeyInfo = pOp->p4.pKeyInfo;
95870 assert( n>0 )((void) (0));
95871 assert( pKeyInfo!=0 )((void) (0));
95872 p1 = pOp->p1;
95873 p2 = pOp->p2;
95874#ifdef SQLITE_DEBUG
95875 if( aPermute ){
95876 int k, mx = 0;
95877 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
95878 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
95879 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
95880 }else{
95881 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
95882 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
95883 }
95884#endif /* SQLITE_DEBUG */
95885 for(i=0; i<n; i++){
95886 idx = aPermute ? aPermute[i] : (u32)i;
95887 assert( memIsValid(&aMem[p1+idx]) )((void) (0));
95888 assert( memIsValid(&aMem[p2+idx]) )((void) (0));
95889 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
95890 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
95891 assert( i<pKeyInfo->nKeyField )((void) (0));
95892 pColl = pKeyInfo->aColl[i];
95893 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC0x01);
95894 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
95895 VVA_ONLY( iCompareIsInit = 1; )
95896 if( iCompare ){
95897 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL0x02)
95898 && ((aMem[p1+idx].flags & MEM_Null0x0001) || (aMem[p2+idx].flags & MEM_Null0x0001))
95899 ){
95900 iCompare = -iCompare;
95901 }
95902 if( bRev ) iCompare = -iCompare;
95903 break;
95904 }
95905 }
95906 assert( pOp[1].opcode==OP_Jump )((void) (0));
95907 break;
95908}
95909
95910/* Opcode: Jump P1 P2 P3 * *
95911**
95912** Jump to the instruction at address P1, P2, or P3 depending on whether
95913** in the most recent OP_Compare instruction the P1 vector was less than,
95914** equal to, or greater than the P2 vector, respectively.
95915**
95916** This opcode must immediately follow an OP_Compare opcode.
95917*/
95918case OP_Jump14: { /* jump */
95919 assert( pOp>aOp && pOp[-1].opcode==OP_Compare )((void) (0));
95920 assert( iCompareIsInit )((void) (0));
95921 if( iCompare<0 ){
95922 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
95923 }else if( iCompare==0 ){
95924 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
95925 }else{
95926 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
95927 }
95928 break;
95929}
95930
95931/* Opcode: And P1 P2 P3 * *
95932** Synopsis: r[P3]=(r[P1] && r[P2])
95933**
95934** Take the logical AND of the values in registers P1 and P2 and
95935** write the result into register P3.
95936**
95937** If either P1 or P2 is 0 (false) then the result is 0 even if
95938** the other input is NULL. A NULL and true or two NULLs give
95939** a NULL output.
95940*/
95941/* Opcode: Or P1 P2 P3 * *
95942** Synopsis: r[P3]=(r[P1] || r[P2])
95943**
95944** Take the logical OR of the values in register P1 and P2 and
95945** store the answer in register P3.
95946**
95947** If either P1 or P2 is nonzero (true) then the result is 1 (true)
95948** even if the other input is NULL. A NULL and false or two NULLs
95949** give a NULL output.
95950*/
95951case OP_And44: /* same as TK_AND, in1, in2, out3 */
95952case OP_Or43: { /* same as TK_OR, in1, in2, out3 */
95953 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
95954 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
95955
95956 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
95957 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
95958 if( pOp->opcode==OP_And44 ){
95959 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
95960 v1 = and_logic[v1*3+v2];
95961 }else{
95962 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
95963 v1 = or_logic[v1*3+v2];
95964 }
95965 pOut = &aMem[pOp->p3];
95966 if( v1==2 ){
95967 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0001
)
;
95968 }else{
95969 pOut->u.i = v1;
95970 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0004
)
;
95971 }
95972 break;
95973}
95974
95975/* Opcode: IsTrue P1 P2 P3 P4 *
95976** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
95977**
95978** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
95979** IS NOT FALSE operators.
95980**
95981** Interpret the value in register P1 as a boolean value. Store that
95982** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
95983** NULL, then the P3 is stored in register P2. Invert the answer if P4
95984** is 1.
95985**
95986** The logic is summarized like this:
95987**
95988** <ul>
95989** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
95990** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
95991** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
95992** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
95993** </ul>
95994*/
95995case OP_IsTrue91: { /* in1, out2 */
95996 assert( pOp->p4type==P4_INT32 )((void) (0));
95997 assert( pOp->p4.i==0 || pOp->p4.i==1 )((void) (0));
95998 assert( pOp->p3==0 || pOp->p3==1 )((void) (0));
95999 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
96000 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
96001 break;
96002}
96003
96004/* Opcode: Not P1 P2 * * *
96005** Synopsis: r[P2]= !r[P1]
96006**
96007** Interpret the value in register P1 as a boolean value. Store the
96008** boolean complement in register P2. If the value in register P1 is
96009** NULL, then a NULL is stored in P2.
96010*/
96011case OP_Not19: { /* same as TK_NOT, in1, out2 */
96012 pIn1 = &aMem[pOp->p1];
96013 pOut = &aMem[pOp->p2];
96014 if( (pIn1->flags & MEM_Null0x0001)==0 ){
96015 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
96016 }else{
96017 sqlite3VdbeMemSetNull(pOut);
96018 }
96019 break;
96020}
96021
96022/* Opcode: BitNot P1 P2 * * *
96023** Synopsis: r[P2]= ~r[P1]
96024**
96025** Interpret the content of register P1 as an integer. Store the
96026** ones-complement of the P1 value into register P2. If P1 holds
96027** a NULL then store a NULL in P2.
96028*/
96029case OP_BitNot115: { /* same as TK_BITNOT, in1, out2 */
96030 pIn1 = &aMem[pOp->p1];
96031 pOut = &aMem[pOp->p2];
96032 sqlite3VdbeMemSetNull(pOut);
96033 if( (pIn1->flags & MEM_Null0x0001)==0 ){
96034 pOut->flags = MEM_Int0x0004;
96035 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
96036 }
96037 break;
96038}
96039
96040/* Opcode: Once P1 P2 * * *
96041**
96042** Fall through to the next instruction the first time this opcode is
96043** encountered on each invocation of the byte-code program. Jump to P2
96044** on the second and all subsequent encounters during the same invocation.
96045**
96046** Top-level programs determine first invocation by comparing the P1
96047** operand against the P1 operand on the OP_Init opcode at the beginning
96048** of the program. If the P1 values differ, then fall through and make
96049** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
96050** the same then take the jump.
96051**
96052** For subprograms, there is a bitmask in the VdbeFrame that determines
96053** whether or not the jump should be taken. The bitmask is necessary
96054** because the self-altering code trick does not work for recursive
96055** triggers.
96056*/
96057case OP_Once15: { /* jump */
96058 u32 iAddr; /* Address of this instruction */
96059 assert( p->aOp[0].opcode==OP_Init )((void) (0));
96060 if( p->pFrame ){
96061 iAddr = (int)(pOp - p->aOp);
96062 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
96063 VdbeBranchTaken(1, 2);
96064 goto jump_to_p2;
96065 }
96066 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
96067 }else{
96068 if( p->aOp[0].p1==pOp->p1 ){
96069 VdbeBranchTaken(1, 2);
96070 goto jump_to_p2;
96071 }
96072 }
96073 VdbeBranchTaken(0, 2);
96074 pOp->p1 = p->aOp[0].p1;
96075 break;
96076}
96077
96078/* Opcode: If P1 P2 P3 * *
96079**
96080** Jump to P2 if the value in register P1 is true. The value
96081** is considered true if it is numeric and non-zero. If the value
96082** in P1 is NULL then take the jump if and only if P3 is non-zero.
96083*/
96084case OP_If16: { /* jump, in1 */
96085 int c;
96086 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
96087 VdbeBranchTaken(c!=0, 2);
96088 if( c ) goto jump_to_p2;
96089 break;
96090}
96091
96092/* Opcode: IfNot P1 P2 P3 * *
96093**
96094** Jump to P2 if the value in register P1 is False. The value
96095** is considered false if it has a numeric value of zero. If the value
96096** in P1 is NULL then take the jump if and only if P3 is non-zero.
96097*/
96098case OP_IfNot17: { /* jump, in1 */
96099 int c;
96100 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
96101 VdbeBranchTaken(c!=0, 2);
96102 if( c ) goto jump_to_p2;
96103 break;
96104}
96105
96106/* Opcode: IsNull P1 P2 * * *
96107** Synopsis: if r[P1]==NULL goto P2
96108**
96109** Jump to P2 if the value in register P1 is NULL.
96110*/
96111case OP_IsNull51: { /* same as TK_ISNULL, jump, in1 */
96112 pIn1 = &aMem[pOp->p1];
96113 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
96114 if( (pIn1->flags & MEM_Null0x0001)!=0 ){
96115 goto jump_to_p2;
96116 }
96117 break;
96118}
96119
96120/* Opcode: IsType P1 P2 P3 P4 P5
96121** Synopsis: if typeof(P1.P3) in P5 goto P2
96122**
96123** Jump to P2 if the type of a column in a btree is one of the types specified
96124** by the P5 bitmask.
96125**
96126** P1 is normally a cursor on a btree for which the row decode cache is
96127** valid through at least column P3. In other words, there should have been
96128** a prior OP_Column for column P3 or greater. If the cursor is not valid,
96129** then this opcode might give spurious results.
96130** The the btree row has fewer than P3 columns, then use P4 as the
96131** datatype.
96132**
96133** If P1 is -1, then P3 is a register number and the datatype is taken
96134** from the value in that register.
96135**
96136** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
96137** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
96138** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
96139**
96140** WARNING: This opcode does not reliably distinguish between NULL and REAL
96141** when P1>=0. If the database contains a NaN value, this opcode will think
96142** that the datatype is REAL when it should be NULL. When P1<0 and the value
96143** is already stored in register P3, then this opcode does reliably
96144** distinguish between NULL and REAL. The problem only arises then P1>=0.
96145**
96146** Take the jump to address P2 if and only if the datatype of the
96147** value determined by P1 and P3 corresponds to one of the bits in the
96148** P5 bitmask.
96149**
96150*/
96151case OP_IsType18: { /* jump */
96152 VdbeCursor *pC;
96153 u16 typeMask;
96154 u32 serialType;
96155
96156 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor )((void) (0));
96157 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) )((void) (0));
96158 if( pOp->p1>=0 ){
96159 pC = p->apCsr[pOp->p1];
96160 assert( pC!=0 )((void) (0));
96161 assert( pOp->p3>=0 )((void) (0));
96162 if( pOp->p3<pC->nHdrParsed ){
96163 serialType = pC->aType[pOp->p3];
96164 if( serialType>=12 ){
96165 if( serialType&1 ){
96166 typeMask = 0x04; /* SQLITE_TEXT */
96167 }else{
96168 typeMask = 0x08; /* SQLITE_BLOB */
96169 }
96170 }else{
96171 static const unsigned char aMask[] = {
96172 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
96173 0x01, 0x01, 0x10, 0x10
96174 };
96175 testcase( serialType==0 );
96176 testcase( serialType==1 );
96177 testcase( serialType==2 );
96178 testcase( serialType==3 );
96179 testcase( serialType==4 );
96180 testcase( serialType==5 );
96181 testcase( serialType==6 );
96182 testcase( serialType==7 );
96183 testcase( serialType==8 );
96184 testcase( serialType==9 );
96185 testcase( serialType==10 );
96186 testcase( serialType==11 );
96187 typeMask = aMask[serialType];
96188 }
96189 }else{
96190 typeMask = 1 << (pOp->p4.i - 1);
96191 testcase( typeMask==0x01 );
96192 testcase( typeMask==0x02 );
96193 testcase( typeMask==0x04 );
96194 testcase( typeMask==0x08 );
96195 testcase( typeMask==0x10 );
96196 }
96197 }else{
96198 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
96199 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
96200 testcase( typeMask==0x01 );
96201 testcase( typeMask==0x02 );
96202 testcase( typeMask==0x04 );
96203 testcase( typeMask==0x08 );
96204 testcase( typeMask==0x10 );
96205 }
96206 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
96207 if( typeMask & pOp->p5 ){
96208 goto jump_to_p2;
96209 }
96210 break;
96211}
96212
96213/* Opcode: ZeroOrNull P1 P2 P3 * *
96214** Synopsis: r[P2] = 0 OR NULL
96215**
96216** If both registers P1 and P3 are NOT NULL, then store a zero in
96217** register P2. If either registers P1 or P3 are NULL then put
96218** a NULL in register P2.
96219*/
96220case OP_ZeroOrNull92: { /* in1, in2, out2, in3 */
96221 if( (aMem[pOp->p1].flags & MEM_Null0x0001)!=0
96222 || (aMem[pOp->p3].flags & MEM_Null0x0001)!=0
96223 ){
96224 sqlite3VdbeMemSetNull(aMem + pOp->p2);
96225 }else{
96226 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
96227 }
96228 break;
96229}
96230
96231/* Opcode: NotNull P1 P2 * * *
96232** Synopsis: if r[P1]!=NULL goto P2
96233**
96234** Jump to P2 if the value in register P1 is not NULL.
96235*/
96236case OP_NotNull52: { /* same as TK_NOTNULL, jump, in1 */
96237 pIn1 = &aMem[pOp->p1];
96238 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
96239 if( (pIn1->flags & MEM_Null0x0001)==0 ){
96240 goto jump_to_p2;
96241 }
96242 break;
96243}
96244
96245/* Opcode: IfNullRow P1 P2 P3 * *
96246** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
96247**
96248** Check the cursor P1 to see if it is currently pointing at a NULL row.
96249** If it is, then set register P3 to NULL and jump immediately to P2.
96250** If P1 is not on a NULL row, then fall through without making any
96251** changes.
96252**
96253** If P1 is not an open cursor, then this opcode is a no-op.
96254*/
96255case OP_IfNullRow20: { /* jump */
96256 VdbeCursor *pC;
96257 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
96258 pC = p->apCsr[pOp->p1];
96259 if( pC && pC->nullRow ){
96260 sqlite3VdbeMemSetNull(aMem + pOp->p3);
96261 goto jump_to_p2;
96262 }
96263 break;
96264}
96265
96266#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
96267/* Opcode: Offset P1 P2 P3 * *
96268** Synopsis: r[P3] = sqlite_offset(P1)
96269**
96270** Store in register r[P3] the byte offset into the database file that is the
96271** start of the payload for the record at which that cursor P1 is currently
96272** pointing.
96273**
96274** P2 is the column number for the argument to the sqlite_offset() function.
96275** This opcode does not use P2 itself, but the P2 value is used by the
96276** code generator. The P1, P2, and P3 operands to this opcode are the
96277** same as for OP_Column.
96278**
96279** This opcode is only available if SQLite is compiled with the
96280** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
96281*/
96282case OP_Offset93: { /* out3 */
96283 VdbeCursor *pC; /* The VDBE cursor */
96284 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
96285 pC = p->apCsr[pOp->p1];
96286 pOut = &p->aMem[pOp->p3];
96287 if( pC==0 || pC->eCurType!=CURTYPE_BTREE0 ){
96288 sqlite3VdbeMemSetNull(pOut);
96289 }else{
96290 if( pC->deferredMoveto ){
96291 rc = sqlite3VdbeFinishMoveto(pC);
96292 if( rc ) goto abort_due_to_error;
96293 }
96294 if( sqlite3BtreeEof(pC->uc.pCursor) ){
96295 sqlite3VdbeMemSetNull(pOut);
96296 }else{
96297 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
96298 }
96299 }
96300 break;
96301}
96302#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
96303
96304/* Opcode: Column P1 P2 P3 P4 P5
96305** Synopsis: r[P3]=PX cursor P1 column P2
96306**
96307** Interpret the data that cursor P1 points to as a structure built using
96308** the MakeRecord instruction. (See the MakeRecord opcode for additional
96309** information about the format of the data.) Extract the P2-th column
96310** from this record. If there are less than (P2+1)
96311** values in the record, extract a NULL.
96312**
96313** The value extracted is stored in register P3.
96314**
96315** If the record contains fewer than P2 fields, then extract a NULL. Or,
96316** if the P4 argument is a P4_MEM use the value of the P4 argument as
96317** the result.
96318**
96319** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
96320** to only be used by the length() function or the equivalent. The content
96321** of large blobs is not loaded, thus saving CPU cycles. If the
96322** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
96323** typeof() function or the IS NULL or IS NOT NULL operators or the
96324** equivalent. In this case, all content loading can be omitted.
96325*/
96326case OP_Column94: { /* ncycle */
96327 u32 p2; /* column number to retrieve */
96328 VdbeCursor *pC; /* The VDBE cursor */
96329 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
96330 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
96331 int len; /* The length of the serialized data for the column */
96332 int i; /* Loop counter */
96333 Mem *pDest; /* Where to write the extracted value */
96334 Mem sMem; /* For storing the record being decoded */
96335 const u8 *zData; /* Part of the record being decoded */
96336 const u8 *zHdr; /* Next unparsed byte of the header */
96337 const u8 *zEndHdr; /* Pointer to first byte after the header */
96338 u64 offset64; /* 64-bit offset */
96339 u32 t; /* A type code from the record header */
96340 Mem *pReg; /* PseudoTable input register */
96341
96342 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
96343 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
96344 pC = p->apCsr[pOp->p1];
96345 p2 = (u32)pOp->p2;
96346
96347op_column_restart:
96348 assert( pC!=0 )((void) (0));
96349 assert( p2<(u32)pC->nField((void) (0))
96350 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) )((void) (0));
96351 aOffset = pC->aOffset;
96352 assert( aOffset==pC->aType+pC->nField )((void) (0));
96353 assert( pC->eCurType!=CURTYPE_VTAB )((void) (0));
96354 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
96355 assert( pC->eCurType!=CURTYPE_SORTER )((void) (0));
96356
96357 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
96358 if( pC->nullRow ){
96359 if( pC->eCurType==CURTYPE_PSEUDO3 && pC->seekResult>0 ){
96360 /* For the special case of as pseudo-cursor, the seekResult field
96361 ** identifies the register that holds the record */
96362 pReg = &aMem[pC->seekResult];
96363 assert( pReg->flags & MEM_Blob )((void) (0));
96364 assert( memIsValid(pReg) )((void) (0));
96365 pC->payloadSize = pC->szRow = pReg->n;
96366 pC->aRow = (u8*)pReg->z;
96367 }else{
96368 pDest = &aMem[pOp->p3];
96369 memAboutToChange(p, pDest);
96370 sqlite3VdbeMemSetNull(pDest);
96371 goto op_column_out;
96372 }
96373 }else{
96374 pCrsr = pC->uc.pCursor;
96375 if( pC->deferredMoveto ){
96376 u32 iMap;
96377 assert( !pC->isEphemeral )((void) (0));
96378 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
96379 pC = pC->pAltCursor;
96380 p2 = iMap - 1;
96381 goto op_column_restart;
96382 }
96383 rc = sqlite3VdbeFinishMoveto(pC);
96384 if( rc ) goto abort_due_to_error;
96385 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){
96386 rc = sqlite3VdbeHandleMovedCursor(pC);
96387 if( rc ) goto abort_due_to_error;
96388 goto op_column_restart;
96389 }
96390 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
96391 assert( pCrsr )((void) (0));
96392 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
96393 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
96394 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
96395 assert( pC->szRow<=pC->payloadSize )((void) (0));
96396 assert( pC->szRow<=65536 )((void) (0)); /* Maximum page size is 64KiB */
96397 }
96398 pC->cacheStatus = p->cacheCtr;
96399 if( (aOffset[0] = pC->aRow[0])<0x80 ){
96400 pC->iHdrOffset = 1;
96401 }else{
96402 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
96403 }
96404 pC->nHdrParsed = 0;
96405
96406 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
96407 /* pC->aRow does not have to hold the entire row, but it does at least
96408 ** need to cover the header of the record. If pC->aRow does not contain
96409 ** the complete header, then set it to zero, forcing the header to be
96410 ** dynamically allocated. */
96411 pC->aRow = 0;
96412 pC->szRow = 0;
96413
96414 /* Make sure a corrupt database has not given us an oversize header.
96415 ** Do this now to avoid an oversize memory allocation.
96416 **
96417 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
96418 ** types use so much data space that there can only be 4096 and 32 of
96419 ** them, respectively. So the maximum header length results from a
96420 ** 3-byte type for each of the maximum of 32768 columns plus three
96421 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
96422 */
96423 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
96424 goto op_column_corrupt;
96425 }
96426 }else{
96427 /* This is an optimization. By skipping over the first few tests
96428 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
96429 ** measurable performance gain.
96430 **
96431 ** This branch is taken even if aOffset[0]==0. Such a record is never
96432 ** generated by SQLite, and could be considered corruption, but we
96433 ** accept it for historical reasons. When aOffset[0]==0, the code this
96434 ** branch jumps to reads past the end of the record, but never more
96435 ** than a few bytes. Even if the record occurs at the end of the page
96436 ** content area, the "page header" comes after the page content and so
96437 ** this overread is harmless. Similar overreads can occur for a corrupt
96438 ** database file.
96439 */
96440 zData = pC->aRow;
96441 assert( pC->nHdrParsed<=p2 )((void) (0)); /* Conditional skipped */
96442 testcase( aOffset[0]==0 );
96443 goto op_column_read_header;
96444 }
96445 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
96446 rc = sqlite3VdbeHandleMovedCursor(pC);
96447 if( rc ) goto abort_due_to_error;
96448 goto op_column_restart;
96449 }
96450
96451 /* Make sure at least the first p2+1 entries of the header have been
96452 ** parsed and valid information is in aOffset[] and pC->aType[].
96453 */
96454 if( pC->nHdrParsed<=p2 ){
96455 /* If there is more header available for parsing in the record, try
96456 ** to extract additional fields up through the p2+1-th field
96457 */
96458 if( pC->iHdrOffset<aOffset[0] ){
96459 /* Make sure zData points to enough of the record to cover the header. */
96460 if( pC->aRow==0 ){
96461 memset(&sMem, 0, sizeof(sMem));
96462 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
96463 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
96464 zData = (u8*)sMem.z;
96465 }else{
96466 zData = pC->aRow;
96467 }
96468
96469 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
96470 op_column_read_header:
96471 i = pC->nHdrParsed;
96472 offset64 = aOffset[i];
96473 zHdr = zData + pC->iHdrOffset;
96474 zEndHdr = zData + aOffset[0];
96475 testcase( zHdr>=zEndHdr );
96476 do{
96477 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
96478 zHdr++;
96479 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
96480 }else{
96481 zHdr += sqlite3GetVarint32(zHdr, &t);
96482 pC->aType[i] = t;
96483 offset64 += sqlite3VdbeSerialTypeLen(t);
96484 }
96485 aOffset[++i] = (u32)(offset64 & 0xffffffff);
96486 }while( (u32)i<=p2 && zHdr<zEndHdr );
96487
96488 /* The record is corrupt if any of the following are true:
96489 ** (1) the bytes of the header extend past the declared header size
96490 ** (2) the entire header was used but not all data was used
96491 ** (3) the end of the data extends beyond the end of the record.
96492 */
96493 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
96494 || (offset64 > pC->payloadSize)
96495 ){
96496 if( aOffset[0]==0 ){
96497 i = 0;
96498 zHdr = zEndHdr;
96499 }else{
96500 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
96501 goto op_column_corrupt;
96502 }
96503 }
96504
96505 pC->nHdrParsed = i;
96506 pC->iHdrOffset = (u32)(zHdr - zData);
96507 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
96508 }else{
96509 t = 0;
96510 }
96511
96512 /* If after trying to extract new entries from the header, nHdrParsed is
96513 ** still not up to p2, that means that the record has fewer than p2
96514 ** columns. So the result will be either the default value or a NULL.
96515 */
96516 if( pC->nHdrParsed<=p2 ){
96517 pDest = &aMem[pOp->p3];
96518 memAboutToChange(p, pDest);
96519 if( pOp->p4type==P4_MEM(-10) ){
96520 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static0x2000);
96521 }else{
96522 sqlite3VdbeMemSetNull(pDest);
96523 }
96524 goto op_column_out;
96525 }
96526 }else{
96527 t = pC->aType[p2];
96528 }
96529
96530 /* Extract the content for the p2+1-th column. Control can only
96531 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
96532 ** all valid.
96533 */
96534 assert( p2<pC->nHdrParsed )((void) (0));
96535 assert( rc==SQLITE_OK )((void) (0));
96536 pDest = &aMem[pOp->p3];
96537 memAboutToChange(p, pDest);
96538 assert( sqlite3VdbeCheckMemInvariants(pDest) )((void) (0));
96539 if( VdbeMemDynamic(pDest)(((pDest)->flags&(0x8000|0x1000))!=0) ){
96540 sqlite3VdbeMemSetNull(pDest);
96541 }
96542 assert( t==pC->aType[p2] )((void) (0));
96543 if( pC->szRow>=aOffset[p2+1] ){
96544 /* This is the common case where the desired content fits on the original
96545 ** page - where the content is not on an overflow page */
96546 zData = pC->aRow + aOffset[p2];
96547 if( t<12 ){
96548 sqlite3VdbeSerialGet(zData, t, pDest);
96549 }else{
96550 /* If the column value is a string, we need a persistent value, not
96551 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
96552 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
96553 */
96554 static const u16 aFlag[] = { MEM_Blob0x0010, MEM_Str0x0002|MEM_Term0x0200 };
96555 pDest->n = len = (t-12)/2;
96556 pDest->enc = encoding;
96557 if( pDest->szMalloc < len+2 ){
96558 if( len>db->aLimit[SQLITE_LIMIT_LENGTH0] ) goto too_big;
96559 pDest->flags = MEM_Null0x0001;
96560 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
96561 }else{
96562 pDest->z = pDest->zMalloc;
96563 }
96564 memcpy(pDest->z, zData, len);
96565 pDest->z[len] = 0;
96566 pDest->z[len+1] = 0;
96567 pDest->flags = aFlag[t&1];
96568 }
96569 }else{
96570 u8 p5;
96571 pDest->enc = encoding;
96572 assert( pDest->db==db )((void) (0));
96573 /* This branch happens only when content is on overflow pages */
96574 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG0xc0))!=0
96575 && (p5==OPFLAG_TYPEOFARG0x80
96576 || (t>=12 && ((t&1)==0 || p5==OPFLAG_BYTELENARG0xc0))
96577 )
96578 )
96579 || sqlite3VdbeSerialTypeLen(t)==0
96580 ){
96581 /* Content is irrelevant for
96582 ** 1. the typeof() function,
96583 ** 2. the length(X) function if X is a blob, and
96584 ** 3. if the content length is zero.
96585 ** So we might as well use bogus content rather than reading
96586 ** content from disk.
96587 **
96588 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
96589 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
96590 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
96591 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
96592 ** and it begins with a bunch of zeros.
96593 */
96594 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
96595 }else{
96596 rc = vdbeColumnFromOverflow(pC, p2, t, aOffset[p2],
96597 p->cacheCtr, colCacheCtr, pDest);
96598 if( rc ){
96599 if( rc==SQLITE_NOMEM7 ) goto no_mem;
96600 if( rc==SQLITE_TOOBIG18 ) goto too_big;
96601 goto abort_due_to_error;
96602 }
96603 }
96604 }
96605
96606op_column_out:
96607 UPDATE_MAX_BLOBSIZE(pDest);
96608 REGISTER_TRACE(pOp->p3, pDest);
96609 break;
96610
96611op_column_corrupt:
96612 if( aOp[0].p3>0 ){
96613 pOp = &aOp[aOp[0].p3-1];
96614 break;
96615 }else{
96616 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(96616);
96617 goto abort_due_to_error;
96618 }
96619}
96620
96621/* Opcode: TypeCheck P1 P2 P3 P4 *
96622** Synopsis: typecheck(r[P1@P2])
96623**
96624** Apply affinities to the range of P2 registers beginning with P1.
96625** Take the affinities from the Table object in P4. If any value
96626** cannot be coerced into the correct type, then raise an error.
96627**
96628** This opcode is similar to OP_Affinity except that this opcode
96629** forces the register type to the Table column type. This is used
96630** to implement "strict affinity".
96631**
96632** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
96633** is zero. When P3 is non-zero, no type checking occurs for
96634** static generated columns. Virtual columns are computed at query time
96635** and so they are never checked.
96636**
96637** Preconditions:
96638**
96639** <ul>
96640** <li> P2 should be the number of non-virtual columns in the
96641** table of P4.
96642** <li> Table P4 should be a STRICT table.
96643** </ul>
96644**
96645** If any precondition is false, an assertion fault occurs.
96646*/
96647case OP_TypeCheck95: {
96648 Table *pTab;
96649 Column *aCol;
96650 int i;
96651
96652 assert( pOp->p4type==P4_TABLE )((void) (0));
96653 pTab = pOp->p4.pTab;
96654 assert( pTab->tabFlags & TF_Strict )((void) (0));
96655 assert( pTab->nNVCol==pOp->p2 )((void) (0));
96656 aCol = pTab->aCol;
96657 pIn1 = &aMem[pOp->p1];
96658 for(i=0; i<pTab->nCol; i++){
96659 if( aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
96660 if( aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) continue;
96661 if( pOp->p3 ){ pIn1++; continue; }
96662 }
96663 assert( pIn1 < &aMem[pOp->p1+pOp->p2] )((void) (0));
96664 applyAffinity(pIn1, aCol[i].affinity, encoding);
96665 if( (pIn1->flags & MEM_Null0x0001)==0 ){
96666 switch( aCol[i].eCType ){
96667 case COLTYPE_BLOB2: {
96668 if( (pIn1->flags & MEM_Blob0x0010)==0 ) goto vdbe_type_error;
96669 break;
96670 }
96671 case COLTYPE_INTEGER4:
96672 case COLTYPE_INT3: {
96673 if( (pIn1->flags & MEM_Int0x0004)==0 ) goto vdbe_type_error;
96674 break;
96675 }
96676 case COLTYPE_TEXT6: {
96677 if( (pIn1->flags & MEM_Str0x0002)==0 ) goto vdbe_type_error;
96678 break;
96679 }
96680 case COLTYPE_REAL5: {
96681 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
96682 assert( (pIn1->flags & MEM_IntReal)==0 )((void) (0));
96683 if( pIn1->flags & MEM_Int0x0004 ){
96684 /* When applying REAL affinity, if the result is still an MEM_Int
96685 ** that will fit in 6 bytes, then change the type to MEM_IntReal
96686 ** so that we keep the high-resolution integer value but know that
96687 ** the type really wants to be REAL. */
96688 testcase( pIn1->u.i==140737488355328LL );
96689 testcase( pIn1->u.i==140737488355327LL );
96690 testcase( pIn1->u.i==-140737488355328LL );
96691 testcase( pIn1->u.i==-140737488355329LL );
96692 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
96693 pIn1->flags |= MEM_IntReal0x0020;
96694 pIn1->flags &= ~MEM_Int0x0004;
96695 }else{
96696 pIn1->u.r = (double)pIn1->u.i;
96697 pIn1->flags |= MEM_Real0x0008;
96698 pIn1->flags &= ~MEM_Int0x0004;
96699 }
96700 }else if( (pIn1->flags & (MEM_Real0x0008|MEM_IntReal0x0020))==0 ){
96701 goto vdbe_type_error;
96702 }
96703 break;
96704 }
96705 default: {
96706 /* COLTYPE_ANY. Accept anything. */
96707 break;
96708 }
96709 }
96710 }
96711 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
96712 pIn1++;
96713 }
96714 assert( pIn1 == &aMem[pOp->p1+pOp->p2] )((void) (0));
96715 break;
96716
96717vdbe_type_error:
96718 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s",
96719 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
96720 pTab->zName, aCol[i].zCnName);
96721 rc = SQLITE_CONSTRAINT_DATATYPE(19 |(12<<8));
96722 goto abort_due_to_error;
96723}
96724
96725/* Opcode: Affinity P1 P2 * P4 *
96726** Synopsis: affinity(r[P1@P2])
96727**
96728** Apply affinities to a range of P2 registers starting with P1.
96729**
96730** P4 is a string that is P2 characters long. The N-th character of the
96731** string indicates the column affinity that should be used for the N-th
96732** memory cell in the range.
96733*/
96734case OP_Affinity96: {
96735 const char *zAffinity; /* The affinity to be applied */
96736
96737 zAffinity = pOp->p4.z;
96738 assert( zAffinity!=0 )((void) (0));
96739 assert( pOp->p2>0 )((void) (0));
96740 assert( zAffinity[pOp->p2]==0 )((void) (0));
96741 pIn1 = &aMem[pOp->p1];
96742 while( 1 /*exit-by-break*/ ){
96743 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
96744 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) )((void) (0));
96745 applyAffinity(pIn1, zAffinity[0], encoding);
96746 if( zAffinity[0]==SQLITE_AFF_REAL0x45 && (pIn1->flags & MEM_Int0x0004)!=0 ){
96747 /* When applying REAL affinity, if the result is still an MEM_Int
96748 ** that will fit in 6 bytes, then change the type to MEM_IntReal
96749 ** so that we keep the high-resolution integer value but know that
96750 ** the type really wants to be REAL. */
96751 testcase( pIn1->u.i==140737488355328LL );
96752 testcase( pIn1->u.i==140737488355327LL );
96753 testcase( pIn1->u.i==-140737488355328LL );
96754 testcase( pIn1->u.i==-140737488355329LL );
96755 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
96756 pIn1->flags |= MEM_IntReal0x0020;
96757 pIn1->flags &= ~MEM_Int0x0004;
96758 }else{
96759 pIn1->u.r = (double)pIn1->u.i;
96760 pIn1->flags |= MEM_Real0x0008;
96761 pIn1->flags &= ~(MEM_Int0x0004|MEM_Str0x0002);
96762 }
96763 }
96764 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
96765 zAffinity++;
96766 if( zAffinity[0]==0 ) break;
96767 pIn1++;
96768 }
96769 break;
96770}
96771
96772/* Opcode: MakeRecord P1 P2 P3 P4 *
96773** Synopsis: r[P3]=mkrec(r[P1@P2])
96774**
96775** Convert P2 registers beginning with P1 into the [record format]
96776** use as a data record in a database table or as a key
96777** in an index. The OP_Column opcode can decode the record later.
96778**
96779** P4 may be a string that is P2 characters long. The N-th character of the
96780** string indicates the column affinity that should be used for the N-th
96781** field of the index key.
96782**
96783** The mapping from character to affinity is given by the SQLITE_AFF_
96784** macros defined in sqliteInt.h.
96785**
96786** If P4 is NULL then all index fields have the affinity BLOB.
96787**
96788** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
96789** compile-time option is enabled:
96790**
96791** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
96792** of the right-most table that can be null-trimmed.
96793**
96794** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
96795** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
96796** accept no-change records with serial_type 10. This value is
96797** only used inside an assert() and does not affect the end result.
96798*/
96799case OP_MakeRecord97: {
96800 Mem *pRec; /* The new record */
96801 u64 nData; /* Number of bytes of data space */
96802 int nHdr; /* Number of bytes of header space */
96803 i64 nByte; /* Data space required for this record */
96804 i64 nZero; /* Number of zero bytes at the end of the record */
96805 int nVarint; /* Number of bytes in a varint */
96806 u32 serial_type; /* Type field */
96807 Mem *pData0; /* First field to be combined into the record */
96808 Mem *pLast; /* Last field of the record */
96809 int nField; /* Number of fields in the record */
96810 char *zAffinity; /* The affinity string for the record */
96811 u32 len; /* Length of a field */
96812 u8 *zHdr; /* Where to write next byte of the header */
96813 u8 *zPayload; /* Where to write next byte of the payload */
96814
96815 /* Assuming the record contains N fields, the record format looks
96816 ** like this:
96817 **
96818 ** ------------------------------------------------------------------------
96819 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
96820 ** ------------------------------------------------------------------------
96821 **
96822 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
96823 ** and so forth.
96824 **
96825 ** Each type field is a varint representing the serial type of the
96826 ** corresponding data element (see sqlite3VdbeSerialType()). The
96827 ** hdr-size field is also a varint which is the offset from the beginning
96828 ** of the record to data0.
96829 */
96830 nData = 0; /* Number of bytes of data space */
96831 nHdr = 0; /* Number of bytes of header space */
96832 nZero = 0; /* Number of zero bytes at the end of the record */
96833 nField = pOp->p1;
96834 zAffinity = pOp->p4.z;
96835 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
96836 pData0 = &aMem[nField];
96837 nField = pOp->p2;
96838 pLast = &pData0[nField-1];
96839
96840 /* Identify the output register */
96841 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 )((void) (0));
96842 pOut = &aMem[pOp->p3];
96843 memAboutToChange(p, pOut);
96844
96845 /* Apply the requested affinity to all inputs
96846 */
96847 assert( pData0<=pLast )((void) (0));
96848 if( zAffinity ){
96849 pRec = pData0;
96850 do{
96851 applyAffinity(pRec, zAffinity[0], encoding);
96852 if( zAffinity[0]==SQLITE_AFF_REAL0x45 && (pRec->flags & MEM_Int0x0004) ){
96853 pRec->flags |= MEM_IntReal0x0020;
96854 pRec->flags &= ~(MEM_Int0x0004);
96855 }
96856 REGISTER_TRACE((int)(pRec-aMem), pRec);
96857 zAffinity++;
96858 pRec++;
96859 assert( zAffinity[0]==0 || pRec<=pLast )((void) (0));
96860 }while( zAffinity[0] );
96861 }
96862
96863#ifdef SQLITE_ENABLE_NULL_TRIM
96864 /* NULLs can be safely trimmed from the end of the record, as long as
96865 ** as the schema format is 2 or more and none of the omitted columns
96866 ** have a non-NULL default value. Also, the record must be left with
96867 ** at least one field. If P5>0 then it will be one more than the
96868 ** index of the right-most column with a non-NULL default value */
96869 if( pOp->p5 ){
96870 while( (pLast->flags & MEM_Null0x0001)!=0 && nField>pOp->p5 ){
96871 pLast--;
96872 nField--;
96873 }
96874 }
96875#endif
96876
96877 /* Loop through the elements that will make up the record to figure
96878 ** out how much space is required for the new record. After this loop,
96879 ** the Mem.uTemp field of each term should hold the serial-type that will
96880 ** be used for that term in the generated record:
96881 **
96882 ** Mem.uTemp value type
96883 ** --------------- ---------------
96884 ** 0 NULL
96885 ** 1 1-byte signed integer
96886 ** 2 2-byte signed integer
96887 ** 3 3-byte signed integer
96888 ** 4 4-byte signed integer
96889 ** 5 6-byte signed integer
96890 ** 6 8-byte signed integer
96891 ** 7 IEEE float
96892 ** 8 Integer constant 0
96893 ** 9 Integer constant 1
96894 ** 10,11 reserved for expansion
96895 ** N>=12 and even BLOB
96896 ** N>=13 and odd text
96897 **
96898 ** The following additional values are computed:
96899 ** nHdr Number of bytes needed for the record header
96900 ** nData Number of bytes of data space needed for the record
96901 ** nZero Zero bytes at the end of the record
96902 */
96903 pRec = pLast;
96904 do{
96905 assert( memIsValid(pRec) )((void) (0));
96906 if( pRec->flags & MEM_Null0x0001 ){
96907 if( pRec->flags & MEM_Zero0x0400 ){
96908 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
96909 ** table methods that never invoke sqlite3_result_xxxxx() while
96910 ** computing an unchanging column value in an UPDATE statement.
96911 ** Give such values a special internal-use-only serial-type of 10
96912 ** so that they can be passed through to xUpdate and have
96913 ** a true sqlite3_value_nochange(). */
96914#ifndef SQLITE_ENABLE_NULL_TRIM
96915 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB )((void) (0));
96916#endif
96917 pRec->uTemp = 10;
96918 }else{
96919 pRec->uTemp = 0;
96920 }
96921 nHdr++;
96922 }else if( pRec->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
96923 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
96924 i64 i = pRec->u.i;
96925 u64 uu;
96926 testcase( pRec->flags & MEM_Int );
96927 testcase( pRec->flags & MEM_IntReal );
96928 if( i<0 ){
96929 uu = ~i;
96930 }else{
96931 uu = i;
96932 }
96933 nHdr++;
96934 testcase( uu==127 ); testcase( uu==128 );
96935 testcase( uu==32767 ); testcase( uu==32768 );
96936 testcase( uu==8388607 ); testcase( uu==8388608 );
96937 testcase( uu==2147483647 ); testcase( uu==2147483648LL );
96938 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
96939 if( uu<=127 ){
96940 if( (i&1)==i && p->minWriteFileFormat>=4 ){
96941 pRec->uTemp = 8+(u32)uu;
96942 }else{
96943 nData++;
96944 pRec->uTemp = 1;
96945 }
96946 }else if( uu<=32767 ){
96947 nData += 2;
96948 pRec->uTemp = 2;
96949 }else if( uu<=8388607 ){
96950 nData += 3;
96951 pRec->uTemp = 3;
96952 }else if( uu<=2147483647 ){
96953 nData += 4;
96954 pRec->uTemp = 4;
96955 }else if( uu<=140737488355327LL ){
96956 nData += 6;
96957 pRec->uTemp = 5;
96958 }else{
96959 nData += 8;
96960 if( pRec->flags & MEM_IntReal0x0020 ){
96961 /* If the value is IntReal and is going to take up 8 bytes to store
96962 ** as an integer, then we might as well make it an 8-byte floating
96963 ** point value */
96964 pRec->u.r = (double)pRec->u.i;
96965 pRec->flags &= ~MEM_IntReal0x0020;
96966 pRec->flags |= MEM_Real0x0008;
96967 pRec->uTemp = 7;
96968 }else{
96969 pRec->uTemp = 6;
96970 }
96971 }
96972 }else if( pRec->flags & MEM_Real0x0008 ){
96973 nHdr++;
96974 nData += 8;
96975 pRec->uTemp = 7;
96976 }else{
96977 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) )((void) (0));
96978 assert( pRec->n>=0 )((void) (0));
96979 len = (u32)pRec->n;
96980 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str0x0002)!=0);
96981 if( pRec->flags & MEM_Zero0x0400 ){
96982 serial_type += pRec->u.nZero*2;
96983 if( nData ){
96984 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
96985 len += pRec->u.nZero;
96986 }else{
96987 nZero += pRec->u.nZero;
96988 }
96989 }
96990 nData += len;
96991 nHdr += sqlite3VarintLen(serial_type);
96992 pRec->uTemp = serial_type;
96993 }
96994 if( pRec==pData0 ) break;
96995 pRec--;
96996 }while(1);
96997
96998 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
96999 ** which determines the total number of bytes in the header. The varint
97000 ** value is the size of the header in bytes including the size varint
97001 ** itself. */
97002 testcase( nHdr==126 );
97003 testcase( nHdr==127 );
97004 if( nHdr<=126 ){
97005 /* The common case */
97006 nHdr += 1;
97007 }else{
97008 /* Rare case of a really large header */
97009 nVarint = sqlite3VarintLen(nHdr);
97010 nHdr += nVarint;
97011 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
97012 }
97013 nByte = nHdr+nData;
97014
97015 /* Make sure the output register has a buffer large enough to store
97016 ** the new record. The output register (pOp->p3) is not allowed to
97017 ** be one of the input registers (because the following call to
97018 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
97019 */
97020 if( nByte+nZero<=pOut->szMalloc ){
97021 /* The output register is already large enough to hold the record.
97022 ** No error checks or buffer enlargement is required */
97023 pOut->z = pOut->zMalloc;
97024 }else{
97025 /* Need to make sure that the output is not too big and then enlarge
97026 ** the output register to hold the full result */
97027 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
97028 goto too_big;
97029 }
97030 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
97031 goto no_mem;
97032 }
97033 }
97034 pOut->n = (int)nByte;
97035 pOut->flags = MEM_Blob0x0010;
97036 if( nZero ){
97037 pOut->u.nZero = nZero;
97038 pOut->flags |= MEM_Zero0x0400;
97039 }
97040 UPDATE_MAX_BLOBSIZE(pOut);
97041 zHdr = (u8 *)pOut->z;
97042 zPayload = zHdr + nHdr;
97043
97044 /* Write the record */
97045 if( nHdr<0x80 ){
97046 *(zHdr++) = nHdr;
97047 }else{
97048 zHdr += sqlite3PutVarint(zHdr,nHdr);
97049 }
97050 assert( pData0<=pLast )((void) (0));
97051 pRec = pData0;
97052 while( 1 /*exit-by-break*/ ){
97053 serial_type = pRec->uTemp;
97054 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
97055 ** additional varints, one per column.
97056 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
97057 ** immediately follow the header. */
97058 if( serial_type<=7 ){
97059 *(zHdr++) = serial_type;
97060 if( serial_type==0 ){
97061 /* NULL value. No change in zPayload */
97062 }else{
97063 u64 v;
97064 if( serial_type==7 ){
97065 assert( sizeof(v)==sizeof(pRec->u.r) )((void) (0));
97066 memcpy(&v, &pRec->u.r, sizeof(v));
97067 swapMixedEndianFloat(v);
97068 }else{
97069 v = pRec->u.i;
97070 }
97071 len = sqlite3SmallTypeSizes[serial_type];
97072 assert( len>=1 && len<=8 && len!=5 && len!=7 )((void) (0));
97073 switch( len ){
97074 default: zPayload[7] = (u8)(v&0xff); v >>= 8;
97075 zPayload[6] = (u8)(v&0xff); v >>= 8;
97076 /* no break */ deliberate_fall_through__attribute__((fallthrough));
97077 case 6: zPayload[5] = (u8)(v&0xff); v >>= 8;
97078 zPayload[4] = (u8)(v&0xff); v >>= 8;
97079 /* no break */ deliberate_fall_through__attribute__((fallthrough));
97080 case 4: zPayload[3] = (u8)(v&0xff); v >>= 8;
97081 /* no break */ deliberate_fall_through__attribute__((fallthrough));
97082 case 3: zPayload[2] = (u8)(v&0xff); v >>= 8;
97083 /* no break */ deliberate_fall_through__attribute__((fallthrough));
97084 case 2: zPayload[1] = (u8)(v&0xff); v >>= 8;
97085 /* no break */ deliberate_fall_through__attribute__((fallthrough));
97086 case 1: zPayload[0] = (u8)(v&0xff);
97087 }
97088 zPayload += len;
97089 }
97090 }else if( serial_type<0x80 ){
97091 *(zHdr++) = serial_type;
97092 if( serial_type>=14 && pRec->n>0 ){
97093 assert( pRec->z!=0 )((void) (0));
97094 memcpy(zPayload, pRec->z, pRec->n);
97095 zPayload += pRec->n;
97096 }
97097 }else{
97098 zHdr += sqlite3PutVarint(zHdr, serial_type);
97099 if( pRec->n ){
97100 assert( pRec->z!=0 )((void) (0));
97101 memcpy(zPayload, pRec->z, pRec->n);
97102 zPayload += pRec->n;
97103 }
97104 }
97105 if( pRec==pLast ) break;
97106 pRec++;
97107 }
97108 assert( nHdr==(int)(zHdr - (u8*)pOut->z) )((void) (0));
97109 assert( nByte==(int)(zPayload - (u8*)pOut->z) )((void) (0));
97110
97111 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
97112 REGISTER_TRACE(pOp->p3, pOut);
97113 break;
97114}
97115
97116/* Opcode: Count P1 P2 P3 * *
97117** Synopsis: r[P2]=count()
97118**
97119** Store the number of entries (an integer value) in the table or index
97120** opened by cursor P1 in register P2.
97121**
97122** If P3==0, then an exact count is obtained, which involves visiting
97123** every btree page of the table. But if P3 is non-zero, an estimate
97124** is returned based on the current cursor position.
97125*/
97126case OP_Count98: { /* out2 */
97127 i64 nEntry;
97128 BtCursor *pCrsr;
97129
97130 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE )((void) (0));
97131 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
97132 assert( pCrsr )((void) (0));
97133 if( pOp->p3 ){
97134 nEntry = sqlite3BtreeRowCountEst(pCrsr);
97135 }else{
97136 nEntry = 0; /* Not needed. Only used to silence a warning. */
97137 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
97138 if( rc ) goto abort_due_to_error;
97139 }
97140 pOut = out2Prerelease(p, pOp);
97141 pOut->u.i = nEntry;
97142 goto check_for_interrupt;
97143}
97144
97145/* Opcode: Savepoint P1 * * P4 *
97146**
97147** Open, release or rollback the savepoint named by parameter P4, depending
97148** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
97149** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
97150** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
97151*/
97152case OP_Savepoint0: {
97153 int p1; /* Value of P1 operand */
97154 char *zName; /* Name of savepoint */
97155 int nName;
97156 Savepoint *pNew;
97157 Savepoint *pSavepoint;
97158 Savepoint *pTmp;
97159 int iSavepoint;
97160 int ii;
97161
97162 p1 = pOp->p1;
97163 zName = pOp->p4.z;
97164
97165 /* Assert that the p1 parameter is valid. Also that if there is no open
97166 ** transaction, then there cannot be any savepoints.
97167 */
97168 assert( db->pSavepoint==0 || db->autoCommit==0 )((void) (0));
97169 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK )((void) (0));
97170 assert( db->pSavepoint || db->isTransactionSavepoint==0 )((void) (0));
97171 assert( checkSavepointCount(db) )((void) (0));
97172 assert( p->bIsReader )((void) (0));
97173
97174 if( p1==SAVEPOINT_BEGIN0 ){
97175 if( db->nVdbeWrite>0 ){
97176 /* A new savepoint cannot be created if there are active write
97177 ** statements (i.e. open read/write incremental blob handles).
97178 */
97179 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
97180 rc = SQLITE_BUSY5;
97181 }else{
97182 nName = sqlite3Strlen30(zName);
97183
97184#ifndef SQLITE_OMIT_VIRTUALTABLE
97185 /* This call is Ok even if this savepoint is actually a transaction
97186 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
97187 ** If this is a transaction savepoint being opened, it is guaranteed
97188 ** that the db->aVTrans[] array is empty. */
97189 assert( db->autoCommit==0 || db->nVTrans==0 )((void) (0));
97190 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0,
97191 db->nStatement+db->nSavepoint);
97192 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
97193#endif
97194
97195 /* Create a new savepoint structure. */
97196 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
97197 if( pNew ){
97198 pNew->zName = (char *)&pNew[1];
97199 memcpy(pNew->zName, zName, nName+1);
97200
97201 /* If there is no open transaction, then mark this as a special
97202 ** "transaction savepoint". */
97203 if( db->autoCommit ){
97204 db->autoCommit = 0;
97205 db->isTransactionSavepoint = 1;
97206 }else{
97207 db->nSavepoint++;
97208 }
97209
97210 /* Link the new savepoint into the database handle's list. */
97211 pNew->pNext = db->pSavepoint;
97212 db->pSavepoint = pNew;
97213 pNew->nDeferredCons = db->nDeferredCons;
97214 pNew->nDeferredImmCons = db->nDeferredImmCons;
97215 }
97216 }
97217 }else{
97218 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK )((void) (0));
97219 iSavepoint = 0;
97220
97221 /* Find the named savepoint. If there is no such savepoint, then an
97222 ** an error is returned to the user. */
97223 for(
97224 pSavepoint = db->pSavepoint;
97225 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
97226 pSavepoint = pSavepoint->pNext
97227 ){
97228 iSavepoint++;
97229 }
97230 if( !pSavepoint ){
97231 sqlite3VdbeError(p, "no such savepoint: %s", zName);
97232 rc = SQLITE_ERROR1;
97233 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE1 ){
97234 /* It is not possible to release (commit) a savepoint if there are
97235 ** active write statements.
97236 */
97237 sqlite3VdbeError(p, "cannot release savepoint - "
97238 "SQL statements in progress");
97239 rc = SQLITE_BUSY5;
97240 }else{
97241
97242 /* Determine whether or not this is a transaction savepoint. If so,
97243 ** and this is a RELEASE command, then the current transaction
97244 ** is committed.
97245 */
97246 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
97247 if( isTransaction && p1==SAVEPOINT_RELEASE1 ){
97248 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
97249 goto vdbe_return;
97250 }
97251 db->autoCommit = 1;
97252 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
97253 p->pc = (int)(pOp - aOp);
97254 db->autoCommit = 0;
97255 p->rc = rc = SQLITE_BUSY5;
97256 goto vdbe_return;
97257 }
97258 rc = p->rc;
97259 if( rc ){
97260 db->autoCommit = 0;
97261 }else{
97262 db->isTransactionSavepoint = 0;
97263 }
97264 }else{
97265 int isSchemaChange;
97266 iSavepoint = db->nSavepoint - iSavepoint - 1;
97267 if( p1==SAVEPOINT_ROLLBACK2 ){
97268 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0;
97269 for(ii=0; ii<db->nDb; ii++){
97270 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
97271 SQLITE_ABORT_ROLLBACK(4 | (2<<8)),
97272 isSchemaChange==0);
97273 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
97274 }
97275 }else{
97276 assert( p1==SAVEPOINT_RELEASE )((void) (0));
97277 isSchemaChange = 0;
97278 }
97279 for(ii=0; ii<db->nDb; ii++){
97280 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
97281 if( rc!=SQLITE_OK0 ){
97282 goto abort_due_to_error;
97283 }
97284 }
97285 if( isSchemaChange ){
97286 sqlite3ExpirePreparedStatements(db, 0);
97287 sqlite3ResetAllSchemasOfConnection(db);
97288 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
97289 }
97290 }
97291 if( rc ) goto abort_due_to_error;
97292
97293 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
97294 ** savepoints nested inside of the savepoint being operated on. */
97295 while( db->pSavepoint!=pSavepoint ){
97296 pTmp = db->pSavepoint;
97297 db->pSavepoint = pTmp->pNext;
97298 sqlite3DbFree(db, pTmp);
97299 db->nSavepoint--;
97300 }
97301
97302 /* If it is a RELEASE, then destroy the savepoint being operated on
97303 ** too. If it is a ROLLBACK TO, then set the number of deferred
97304 ** constraint violations present in the database to the value stored
97305 ** when the savepoint was created. */
97306 if( p1==SAVEPOINT_RELEASE1 ){
97307 assert( pSavepoint==db->pSavepoint )((void) (0));
97308 db->pSavepoint = pSavepoint->pNext;
97309 sqlite3DbFree(db, pSavepoint);
97310 if( !isTransaction ){
97311 db->nSavepoint--;
97312 }
97313 }else{
97314 assert( p1==SAVEPOINT_ROLLBACK )((void) (0));
97315 db->nDeferredCons = pSavepoint->nDeferredCons;
97316 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
97317 }
97318
97319 if( !isTransaction || p1==SAVEPOINT_ROLLBACK2 ){
97320 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
97321 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
97322 }
97323 }
97324 }
97325 if( rc ) goto abort_due_to_error;
97326 if( p->eVdbeState==VDBE_HALT_STATE3 ){
97327 rc = SQLITE_DONE101;
97328 goto vdbe_return;
97329 }
97330 break;
97331}
97332
97333/* Opcode: AutoCommit P1 P2 * * *
97334**
97335** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
97336** back any currently active btree transactions. If there are any active
97337** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
97338** there are active writing VMs or active VMs that use shared cache.
97339**
97340** This instruction causes the VM to halt.
97341*/
97342case OP_AutoCommit1: {
97343 int desiredAutoCommit;
97344 int iRollback;
97345
97346 desiredAutoCommit = pOp->p1;
97347 iRollback = pOp->p2;
97348 assert( desiredAutoCommit==1 || desiredAutoCommit==0 )((void) (0));
97349 assert( desiredAutoCommit==1 || iRollback==0 )((void) (0));
97350 assert( db->nVdbeActive>0 )((void) (0)); /* At least this one VM is active */
97351 assert( p->bIsReader )((void) (0));
97352
97353 if( desiredAutoCommit!=db->autoCommit ){
97354 if( iRollback ){
97355 assert( desiredAutoCommit==1 )((void) (0));
97356 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
97357 db->autoCommit = 1;
97358 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
97359 /* If this instruction implements a COMMIT and other VMs are writing
97360 ** return an error indicating that the other VMs must complete first.
97361 */
97362 sqlite3VdbeError(p, "cannot commit transaction - "
97363 "SQL statements in progress");
97364 rc = SQLITE_BUSY5;
97365 goto abort_due_to_error;
97366 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
97367 goto vdbe_return;
97368 }else{
97369 db->autoCommit = (u8)desiredAutoCommit;
97370 }
97371 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
97372 p->pc = (int)(pOp - aOp);
97373 db->autoCommit = (u8)(1-desiredAutoCommit);
97374 p->rc = rc = SQLITE_BUSY5;
97375 goto vdbe_return;
97376 }
97377 sqlite3CloseSavepoints(db);
97378 if( p->rc==SQLITE_OK0 ){
97379 rc = SQLITE_DONE101;
97380 }else{
97381 rc = SQLITE_ERROR1;
97382 }
97383 goto vdbe_return;
97384 }else{
97385 sqlite3VdbeError(p,
97386 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
97387 (iRollback)?"cannot rollback - no transaction is active":
97388 "cannot commit - no transaction is active"));
97389
97390 rc = SQLITE_ERROR1;
97391 goto abort_due_to_error;
97392 }
97393 /*NOTREACHED*/ assert(0)((void) (0));
97394}
97395
97396/* Opcode: Transaction P1 P2 P3 P4 P5
97397**
97398** Begin a transaction on database P1 if a transaction is not already
97399** active.
97400** If P2 is non-zero, then a write-transaction is started, or if a
97401** read-transaction is already active, it is upgraded to a write-transaction.
97402** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
97403** then an exclusive transaction is started.
97404**
97405** P1 is the index of the database file on which the transaction is
97406** started. Index 0 is the main database file and index 1 is the
97407** file used for temporary tables. Indices of 2 or more are used for
97408** attached databases.
97409**
97410** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
97411** true (this flag is set if the Vdbe may modify more than one row and may
97412** throw an ABORT exception), a statement transaction may also be opened.
97413** More specifically, a statement transaction is opened iff the database
97414** connection is currently not in autocommit mode, or if there are other
97415** active statements. A statement transaction allows the changes made by this
97416** VDBE to be rolled back after an error without having to roll back the
97417** entire transaction. If no error is encountered, the statement transaction
97418** will automatically commit when the VDBE halts.
97419**
97420** If P5!=0 then this opcode also checks the schema cookie against P3
97421** and the schema generation counter against P4.
97422** The cookie changes its value whenever the database schema changes.
97423** This operation is used to detect when that the cookie has changed
97424** and that the current process needs to reread the schema. If the schema
97425** cookie in P3 differs from the schema cookie in the database header or
97426** if the schema generation counter in P4 differs from the current
97427** generation counter, then an SQLITE_SCHEMA error is raised and execution
97428** halts. The sqlite3_step() wrapper function might then reprepare the
97429** statement and rerun it from the beginning.
97430*/
97431case OP_Transaction2: {
97432 Btree *pBt;
97433 Db *pDb;
97434 int iMeta = 0;
97435
97436 assert( p->bIsReader )((void) (0));
97437 assert( p->readOnly==0 || pOp->p2==0 )((void) (0));
97438 assert( pOp->p2>=0 && pOp->p2<=2 )((void) (0));
97439 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
97440 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
97441 assert( rc==SQLITE_OK )((void) (0));
97442 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly0x00100000|SQLITE_CorruptRdOnly((u64)(0x00002)<<32)))!=0 ){
97443 if( db->flags & SQLITE_QueryOnly0x00100000 ){
97444 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
97445 rc = SQLITE_READONLY8;
97446 }else{
97447 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
97448 ** transaction */
97449 rc = SQLITE_CORRUPT11;
97450 }
97451 goto abort_due_to_error;
97452 }
97453 pDb = &db->aDb[pOp->p1];
97454 pBt = pDb->pBt;
97455
97456 if( pBt ){
97457 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
97458 testcase( rc==SQLITE_BUSY_SNAPSHOT );
97459 testcase( rc==SQLITE_BUSY_RECOVERY );
97460 if( rc!=SQLITE_OK0 ){
97461 if( (rc&0xff)==SQLITE_BUSY5 ){
97462 p->pc = (int)(pOp - aOp);
97463 p->rc = rc;
97464 goto vdbe_return;
97465 }
97466 goto abort_due_to_error;
97467 }
97468
97469 if( p->usesStmtJournal
97470 && pOp->p2
97471 && (db->autoCommit==0 || db->nVdbeRead>1)
97472 ){
97473 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE )((void) (0));
97474 if( p->iStatement==0 ){
97475 assert( db->nStatement>=0 && db->nSavepoint>=0 )((void) (0));
97476 db->nStatement++;
97477 p->iStatement = db->nSavepoint + db->nStatement;
97478 }
97479
97480 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0, p->iStatement-1);
97481 if( rc==SQLITE_OK0 ){
97482 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
97483 }
97484
97485 /* Store the current value of the database handles deferred constraint
97486 ** counter. If the statement transaction needs to be rolled back,
97487 ** the value of this counter needs to be restored too. */
97488 p->nStmtDefCons = db->nDeferredCons;
97489 p->nStmtDefImmCons = db->nDeferredImmCons;
97490 }
97491 }
97492 assert( pOp->p5==0 || pOp->p4type==P4_INT32 )((void) (0));
97493 if( rc==SQLITE_OK0
97494 && pOp->p5
97495 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
97496 ){
97497 /*
97498 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
97499 ** version is checked to ensure that the schema has not changed since the
97500 ** SQL statement was prepared.
97501 */
97502 sqlite3DbFree(db, p->zErrMsg);
97503 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
97504 /* If the schema-cookie from the database file matches the cookie
97505 ** stored with the in-memory representation of the schema, do
97506 ** not reload the schema from the database file.
97507 **
97508 ** If virtual-tables are in use, this is not just an optimization.
97509 ** Often, v-tables store their data in other SQLite tables, which
97510 ** are queried from within xNext() and other v-table methods using
97511 ** prepared queries. If such a query is out-of-date, we do not want to
97512 ** discard the database schema, as the user code implementing the
97513 ** v-table would have to be ready for the sqlite3_vtab structure itself
97514 ** to be invalidated whenever sqlite3_step() is called from within
97515 ** a v-table method.
97516 */
97517 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
97518 sqlite3ResetOneSchema(db, pOp->p1);
97519 }
97520 p->expired = 1;
97521 rc = SQLITE_SCHEMA17;
97522
97523 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
97524 ** from being modified in sqlite3VdbeHalt(). If this statement is
97525 ** reprepared, changeCntOn will be set again. */
97526 p->changeCntOn = 0;
97527 }
97528 if( rc ) goto abort_due_to_error;
97529 break;
97530}
97531
97532/* Opcode: ReadCookie P1 P2 P3 * *
97533**
97534** Read cookie number P3 from database P1 and write it into register P2.
97535** P3==1 is the schema version. P3==2 is the database format.
97536** P3==3 is the recommended pager cache size, and so forth. P1==0 is
97537** the main database file and P1==1 is the database file used to store
97538** temporary tables.
97539**
97540** There must be a read-lock on the database (either a transaction
97541** must be started or there must be an open cursor) before
97542** executing this instruction.
97543*/
97544case OP_ReadCookie99: { /* out2 */
97545 int iMeta;
97546 int iDb;
97547 int iCookie;
97548
97549 assert( p->bIsReader )((void) (0));
97550 iDb = pOp->p1;
97551 iCookie = pOp->p3;
97552 assert( pOp->p3<SQLITE_N_BTREE_META )((void) (0));
97553 assert( iDb>=0 && iDb<db->nDb )((void) (0));
97554 assert( db->aDb[iDb].pBt!=0 )((void) (0));
97555 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
97556
97557 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
97558 pOut = out2Prerelease(p, pOp);
97559 pOut->u.i = iMeta;
97560 break;
97561}
97562
97563/* Opcode: SetCookie P1 P2 P3 * P5
97564**
97565** Write the integer value P3 into cookie number P2 of database P1.
97566** P2==1 is the schema version. P2==2 is the database format.
97567** P2==3 is the recommended pager cache
97568** size, and so forth. P1==0 is the main database file and P1==1 is the
97569** database file used to store temporary tables.
97570**
97571** A transaction must be started before executing this opcode.
97572**
97573** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
97574** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
97575** has P5 set to 1, so that the internal schema version will be different
97576** from the database schema version, resulting in a schema reset.
97577*/
97578case OP_SetCookie100: {
97579 Db *pDb;
97580
97581 sqlite3VdbeIncrWriteCounter(p, 0);
97582 assert( pOp->p2<SQLITE_N_BTREE_META )((void) (0));
97583 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
97584 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
97585 assert( p->readOnly==0 )((void) (0));
97586 pDb = &db->aDb[pOp->p1];
97587 assert( pDb->pBt!=0 )((void) (0));
97588 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) )((void) (0));
97589 /* See note about index shifting on OP_ReadCookie */
97590 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
97591 if( pOp->p2==BTREE_SCHEMA_VERSION1 ){
97592 /* When the schema cookie changes, record the new cookie internally */
97593 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
97594 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
97595 sqlite3FkClearTriggerCache(db, pOp->p1);
97596 }else if( pOp->p2==BTREE_FILE_FORMAT2 ){
97597 /* Record changes in the file format */
97598 pDb->pSchema->file_format = pOp->p3;
97599 }
97600 if( pOp->p1==1 ){
97601 /* Invalidate all prepared statements whenever the TEMP database
97602 ** schema is changed. Ticket #1644 */
97603 sqlite3ExpirePreparedStatements(db, 0);
97604 p->expired = 0;
97605 }
97606 if( rc ) goto abort_due_to_error;
97607 break;
97608}
97609
97610/* Opcode: OpenRead P1 P2 P3 P4 P5
97611** Synopsis: root=P2 iDb=P3
97612**
97613** Open a read-only cursor for the database table whose root page is
97614** P2 in a database file. The database file is determined by P3.
97615** P3==0 means the main database, P3==1 means the database used for
97616** temporary tables, and P3>1 means used the corresponding attached
97617** database. Give the new cursor an identifier of P1. The P1
97618** values need not be contiguous but all P1 values should be small integers.
97619** It is an error for P1 to be negative.
97620**
97621** Allowed P5 bits:
97622** <ul>
97623** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
97624** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
97625** of OP_SeekLE/OP_IdxLT)
97626** </ul>
97627**
97628** The P4 value may be either an integer (P4_INT32) or a pointer to
97629** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
97630** object, then table being opened must be an [index b-tree] where the
97631** KeyInfo object defines the content and collating
97632** sequence of that index b-tree. Otherwise, if P4 is an integer
97633** value, then the table being opened must be a [table b-tree] with a
97634** number of columns no less than the value of P4.
97635**
97636** See also: OpenWrite, ReopenIdx
97637*/
97638/* Opcode: ReopenIdx P1 P2 P3 P4 P5
97639** Synopsis: root=P2 iDb=P3
97640**
97641** The ReopenIdx opcode works like OP_OpenRead except that it first
97642** checks to see if the cursor on P1 is already open on the same
97643** b-tree and if it is this opcode becomes a no-op. In other words,
97644** if the cursor is already open, do not reopen it.
97645**
97646** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
97647** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
97648** be the same as every other ReopenIdx or OpenRead for the same cursor
97649** number.
97650**
97651** Allowed P5 bits:
97652** <ul>
97653** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
97654** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
97655** of OP_SeekLE/OP_IdxLT)
97656** </ul>
97657**
97658** See also: OP_OpenRead, OP_OpenWrite
97659*/
97660/* Opcode: OpenWrite P1 P2 P3 P4 P5
97661** Synopsis: root=P2 iDb=P3
97662**
97663** Open a read/write cursor named P1 on the table or index whose root
97664** page is P2 (or whose root page is held in register P2 if the
97665** OPFLAG_P2ISREG bit is set in P5 - see below).
97666**
97667** The P4 value may be either an integer (P4_INT32) or a pointer to
97668** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
97669** object, then table being opened must be an [index b-tree] where the
97670** KeyInfo object defines the content and collating
97671** sequence of that index b-tree. Otherwise, if P4 is an integer
97672** value, then the table being opened must be a [table b-tree] with a
97673** number of columns no less than the value of P4.
97674**
97675** Allowed P5 bits:
97676** <ul>
97677** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
97678** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
97679** of OP_SeekLE/OP_IdxLT)
97680** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
97681** and subsequently delete entries in an index btree. This is a
97682** hint to the storage engine that the storage engine is allowed to
97683** ignore. The hint is not used by the official SQLite b*tree storage
97684** engine, but is used by COMDB2.
97685** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
97686** as the root page, not the value of P2 itself.
97687** </ul>
97688**
97689** This instruction works like OpenRead except that it opens the cursor
97690** in read/write mode.
97691**
97692** See also: OP_OpenRead, OP_ReopenIdx
97693*/
97694case OP_ReopenIdx101: { /* ncycle */
97695 int nField;
97696 KeyInfo *pKeyInfo;
97697 u32 p2;
97698 int iDb;
97699 int wrFlag;
97700 Btree *pX;
97701 VdbeCursor *pCur;
97702 Db *pDb;
97703
97704 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
97705 assert( pOp->p4type==P4_KEYINFO )((void) (0));
97706 pCur = p->apCsr[pOp->p1];
97707 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
97708 assert( pCur->iDb==pOp->p3 )((void) (0)); /* Guaranteed by the code generator */
97709 assert( pCur->eCurType==CURTYPE_BTREE )((void) (0));
97710 sqlite3BtreeClearCursor(pCur->uc.pCursor);
97711 goto open_cursor_set_hints;
97712 }
97713 /* If the cursor is not currently open or is open on a different
97714 ** index, then fall through into OP_OpenRead to force a reopen */
97715case OP_OpenRead102: /* ncycle */
97716case OP_OpenWrite113:
97717
97718 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
97719 assert( p->bIsReader )((void) (0));
97720 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx((void) (0))
97721 || p->readOnly==0 )((void) (0));
97722
97723 if( p->expired==1 ){
97724 rc = SQLITE_ABORT_ROLLBACK(4 | (2<<8));
97725 goto abort_due_to_error;
97726 }
97727
97728 nField = 0;
97729 pKeyInfo = 0;
97730 p2 = (u32)pOp->p2;
97731 iDb = pOp->p3;
97732 assert( iDb>=0 && iDb<db->nDb )((void) (0));
97733 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
97734 pDb = &db->aDb[iDb];
97735 pX = pDb->pBt;
97736 assert( pX!=0 )((void) (0));
97737 if( pOp->opcode==OP_OpenWrite113 ){
97738 assert( OPFLAG_FORDELETE==BTREE_FORDELETE )((void) (0));
97739 wrFlag = BTREE_WRCSR0x00000004 | (pOp->p5 & OPFLAG_FORDELETE0x08);
97740 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
97741 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
97742 p->minWriteFileFormat = pDb->pSchema->file_format;
97743 }
97744 if( pOp->p5 & OPFLAG_P2ISREG0x10 ){
97745 assert( p2>0 )((void) (0));
97746 assert( p2<=(u32)(p->nMem+1 - p->nCursor) )((void) (0));
97747 pIn2 = &aMem[p2];
97748 assert( memIsValid(pIn2) )((void) (0));
97749 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
97750 sqlite3VdbeMemIntegerify(pIn2);
97751 p2 = (int)pIn2->u.i;
97752 /* The p2 value always comes from a prior OP_CreateBtree opcode and
97753 ** that opcode will always set the p2 value to 2 or more or else fail.
97754 ** If there were a failure, the prepared statement would have halted
97755 ** before reaching this instruction. */
97756 assert( p2>=2 )((void) (0));
97757 }
97758 }else{
97759 wrFlag = 0;
97760 assert( (pOp->p5 & OPFLAG_P2ISREG)==0 )((void) (0));
97761 }
97762 if( pOp->p4type==P4_KEYINFO(-8) ){
97763 pKeyInfo = pOp->p4.pKeyInfo;
97764 assert( pKeyInfo->enc==ENC(db) )((void) (0));
97765 assert( pKeyInfo->db==db )((void) (0));
97766 nField = pKeyInfo->nAllField;
97767 }else if( pOp->p4type==P4_INT32(-3) ){
97768 nField = pOp->p4.i;
97769 }
97770 assert( pOp->p1>=0 )((void) (0));
97771 assert( nField>=0 )((void) (0));
97772 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
97773 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE0);
97774 if( pCur==0 ) goto no_mem;
97775 pCur->iDb = iDb;
97776 pCur->nullRow = 1;
97777 pCur->isOrdered = 1;
97778 pCur->pgnoRoot = p2;
97779#ifdef SQLITE_DEBUG
97780 pCur->wrFlag = wrFlag;
97781#endif
97782 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
97783 pCur->pKeyInfo = pKeyInfo;
97784 /* Set the VdbeCursor.isTable variable. Previous versions of
97785 ** SQLite used to check if the root-page flags were sane at this point
97786 ** and report database corruption if they were not, but this check has
97787 ** since moved into the btree layer. */
97788 pCur->isTable = pOp->p4type!=P4_KEYINFO(-8);
97789
97790open_cursor_set_hints:
97791 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD )((void) (0));
97792 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ )((void) (0));
97793 testcase( pOp->p5 & OPFLAG_BULKCSR );
97794 testcase( pOp->p2 & OPFLAG_SEEKEQ );
97795 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
97796 (pOp->p5 & (OPFLAG_BULKCSR0x01|OPFLAG_SEEKEQ0x02)));
97797 if( rc ) goto abort_due_to_error;
97798 break;
97799}
97800
97801/* Opcode: OpenDup P1 P2 * * *
97802**
97803** Open a new cursor P1 that points to the same ephemeral table as
97804** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
97805** opcode. Only ephemeral cursors may be duplicated.
97806**
97807** Duplicate ephemeral cursors are used for self-joins of materialized views.
97808*/
97809case OP_OpenDup114: { /* ncycle */
97810 VdbeCursor *pOrig; /* The original cursor to be duplicated */
97811 VdbeCursor *pCx; /* The new cursor */
97812
97813 pOrig = p->apCsr[pOp->p2];
97814 assert( pOrig )((void) (0));
97815 assert( pOrig->isEphemeral )((void) (0)); /* Only ephemeral cursors can be duplicated */
97816
97817 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE0);
97818 if( pCx==0 ) goto no_mem;
97819 pCx->nullRow = 1;
97820 pCx->isEphemeral = 1;
97821 pCx->pKeyInfo = pOrig->pKeyInfo;
97822 pCx->isTable = pOrig->isTable;
97823 pCx->pgnoRoot = pOrig->pgnoRoot;
97824 pCx->isOrdered = pOrig->isOrdered;
97825 pCx->ub.pBtx = pOrig->ub.pBtx;
97826 pCx->noReuse = 1;
97827 pOrig->noReuse = 1;
97828 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
97829 pCx->pKeyInfo, pCx->uc.pCursor);
97830 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
97831 ** opened for a database. Since there is already an open cursor when this
97832 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
97833 assert( rc==SQLITE_OK )((void) (0));
97834 break;
97835}
97836
97837
97838/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
97839** Synopsis: nColumn=P2
97840**
97841** Open a new cursor P1 to a transient table.
97842** The cursor is always opened read/write even if
97843** the main database is read-only. The ephemeral
97844** table is deleted automatically when the cursor is closed.
97845**
97846** If the cursor P1 is already opened on an ephemeral table, the table
97847** is cleared (all content is erased).
97848**
97849** P2 is the number of columns in the ephemeral table.
97850** The cursor points to a BTree table if P4==0 and to a BTree index
97851** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
97852** that defines the format of keys in the index.
97853**
97854** The P5 parameter can be a mask of the BTREE_* flags defined
97855** in btree.h. These flags control aspects of the operation of
97856** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
97857** added automatically.
97858**
97859** If P3 is positive, then reg[P3] is modified slightly so that it
97860** can be used as zero-length data for OP_Insert. This is an optimization
97861** that avoids an extra OP_Blob opcode to initialize that register.
97862*/
97863/* Opcode: OpenAutoindex P1 P2 * P4 *
97864** Synopsis: nColumn=P2
97865**
97866** This opcode works the same as OP_OpenEphemeral. It has a
97867** different name to distinguish its use. Tables created using
97868** by this opcode will be used for automatically created transient
97869** indices in joins.
97870*/
97871case OP_OpenAutoindex116: /* ncycle */
97872case OP_OpenEphemeral117: { /* ncycle */
97873 VdbeCursor *pCx;
97874 KeyInfo *pKeyInfo;
97875
97876 static const int vfsFlags =
97877 SQLITE_OPEN_READWRITE0x00000002 |
97878 SQLITE_OPEN_CREATE0x00000004 |
97879 SQLITE_OPEN_EXCLUSIVE0x00000010 |
97880 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
97881 SQLITE_OPEN_TRANSIENT_DB0x00000400;
97882 assert( pOp->p1>=0 )((void) (0));
97883 assert( pOp->p2>=0 )((void) (0));
97884 if( pOp->p3>0 ){
97885 /* Make register reg[P3] into a value that can be used as the data
97886 ** form sqlite3BtreeInsert() where the length of the data is zero. */
97887 assert( pOp->p2==0 )((void) (0)); /* Only used when number of columns is zero */
97888 assert( pOp->opcode==OP_OpenEphemeral )((void) (0));
97889 assert( aMem[pOp->p3].flags & MEM_Null )((void) (0));
97890 aMem[pOp->p3].n = 0;
97891 aMem[pOp->p3].z = "";
97892 }
97893 pCx = p->apCsr[pOp->p1];
97894 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField)(pOp->p2<=pCx->nField) ){
97895 /* If the ephemeral table is already open and has no duplicates from
97896 ** OP_OpenDup, then erase all existing content so that the table is
97897 ** empty again, rather than creating a new table. */
97898 assert( pCx->isEphemeral )((void) (0));
97899 pCx->seqCount = 0;
97900 pCx->cacheStatus = CACHE_STALE0;
97901 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
97902 }else{
97903 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE0);
97904 if( pCx==0 ) goto no_mem;
97905 pCx->isEphemeral = 1;
97906 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
97907 BTREE_OMIT_JOURNAL1 | BTREE_SINGLE4 | pOp->p5,
97908 vfsFlags);
97909 if( rc==SQLITE_OK0 ){
97910 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
97911 if( rc==SQLITE_OK0 ){
97912 /* If a transient index is required, create it by calling
97913 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
97914 ** opening it. If a transient table is required, just use the
97915 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
97916 */
97917 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
97918 assert( pOp->p4type==P4_KEYINFO )((void) (0));
97919 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
97920 BTREE_BLOBKEY2 | pOp->p5);
97921 if( rc==SQLITE_OK0 ){
97922 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 )((void) (0));
97923 assert( pKeyInfo->db==db )((void) (0));
97924 assert( pKeyInfo->enc==ENC(db) )((void) (0));
97925 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
97926 pKeyInfo, pCx->uc.pCursor);
97927 }
97928 pCx->isTable = 0;
97929 }else{
97930 pCx->pgnoRoot = SCHEMA_ROOT1;
97931 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT1, BTREE_WRCSR0x00000004,
97932 0, pCx->uc.pCursor);
97933 pCx->isTable = 1;
97934 }
97935 }
97936 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED8);
97937 if( rc ){
97938 assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) )((void) (0));
97939 sqlite3BtreeClose(pCx->ub.pBtx);
97940 }else{
97941 assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) )((void) (0));
97942 }
97943 }
97944 }
97945 if( rc ) goto abort_due_to_error;
97946 pCx->nullRow = 1;
97947 break;
97948}
97949
97950/* Opcode: SorterOpen P1 P2 P3 P4 *
97951**
97952** This opcode works like OP_OpenEphemeral except that it opens
97953** a transient index that is specifically designed to sort large
97954** tables using an external merge-sort algorithm.
97955**
97956** If argument P3 is non-zero, then it indicates that the sorter may
97957** assume that a stable sort considering the first P3 fields of each
97958** key is sufficient to produce the required results.
97959*/
97960case OP_SorterOpen119: {
97961 VdbeCursor *pCx;
97962
97963 assert( pOp->p1>=0 )((void) (0));
97964 assert( pOp->p2>=0 )((void) (0));
97965 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER1);
97966 if( pCx==0 ) goto no_mem;
97967 pCx->pKeyInfo = pOp->p4.pKeyInfo;
97968 assert( pCx->pKeyInfo->db==db )((void) (0));
97969 assert( pCx->pKeyInfo->enc==ENC(db) )((void) (0));
97970 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
97971 if( rc ) goto abort_due_to_error;
97972 break;
97973}
97974
97975/* Opcode: SequenceTest P1 P2 * * *
97976** Synopsis: if( cursor[P1].ctr++ ) pc = P2
97977**
97978** P1 is a sorter cursor. If the sequence counter is currently zero, jump
97979** to P2. Regardless of whether or not the jump is taken, increment the
97980** the sequence value.
97981*/
97982case OP_SequenceTest120: {
97983 VdbeCursor *pC;
97984 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
97985 pC = p->apCsr[pOp->p1];
97986 assert( isSorter(pC) )((void) (0));
97987 if( (pC->seqCount++)==0 ){
97988 goto jump_to_p2;
97989 }
97990 break;
97991}
97992
97993/* Opcode: OpenPseudo P1 P2 P3 * *
97994** Synopsis: P3 columns in r[P2]
97995**
97996** Open a new cursor that points to a fake table that contains a single
97997** row of data. The content of that one row is the content of memory
97998** register P2. In other words, cursor P1 becomes an alias for the
97999** MEM_Blob content contained in register P2.
98000**
98001** A pseudo-table created by this opcode is used to hold a single
98002** row output from the sorter so that the row can be decomposed into
98003** individual columns using the OP_Column opcode. The OP_Column opcode
98004** is the only cursor opcode that works with a pseudo-table.
98005**
98006** P3 is the number of fields in the records that will be stored by
98007** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
98008** will return NULL for every column.
98009*/
98010case OP_OpenPseudo121: {
98011 VdbeCursor *pCx;
98012
98013 assert( pOp->p1>=0 )((void) (0));
98014 assert( pOp->p3>=0 )((void) (0));
98015 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO3);
98016 if( pCx==0 ) goto no_mem;
98017 pCx->nullRow = 1;
98018 pCx->seekResult = pOp->p2;
98019 pCx->isTable = 1;
98020 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
98021 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
98022 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
98023 ** which is a performance optimization */
98024 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
98025 assert( pOp->p5==0 )((void) (0));
98026 break;
98027}
98028
98029/* Opcode: Close P1 * * * *
98030**
98031** Close a cursor previously opened as P1. If P1 is not
98032** currently open, this instruction is a no-op.
98033*/
98034case OP_Close122: { /* ncycle */
98035 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98036 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
98037 p->apCsr[pOp->p1] = 0;
98038 break;
98039}
98040
98041#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
98042/* Opcode: ColumnsUsed P1 * * P4 *
98043**
98044** This opcode (which only exists if SQLite was compiled with
98045** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
98046** table or index for cursor P1 are used. P4 is a 64-bit integer
98047** (P4_INT64) in which the first 63 bits are one for each of the
98048** first 63 columns of the table or index that are actually used
98049** by the cursor. The high-order bit is set if any column after
98050** the 64th is used.
98051*/
98052case OP_ColumnsUsed123: {
98053 VdbeCursor *pC;
98054 pC = p->apCsr[pOp->p1];
98055 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
98056 pC->maskUsed = *(u64*)pOp->p4.pI64;
98057 break;
98058}
98059#endif
98060
98061/* Opcode: SeekGE P1 P2 P3 P4 *
98062** Synopsis: key=r[P3@P4]
98063**
98064** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
98065** use the value in register P3 as the key. If cursor P1 refers
98066** to an SQL index, then P3 is the first in an array of P4 registers
98067** that are used as an unpacked index key.
98068**
98069** Reposition cursor P1 so that it points to the smallest entry that
98070** is greater than or equal to the key value. If there are no records
98071** greater than or equal to the key and P2 is not zero, then jump to P2.
98072**
98073** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
98074** opcode will either land on a record that exactly matches the key, or
98075** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
98076** this opcode must be followed by an IdxLE opcode with the same arguments.
98077** The IdxGT opcode will be skipped if this opcode succeeds, but the
98078** IdxGT opcode will be used on subsequent loop iterations. The
98079** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
98080** is an equality search.
98081**
98082** This opcode leaves the cursor configured to move in forward order,
98083** from the beginning toward the end. In other words, the cursor is
98084** configured to use Next, not Prev.
98085**
98086** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
98087*/
98088/* Opcode: SeekGT P1 P2 P3 P4 *
98089** Synopsis: key=r[P3@P4]
98090**
98091** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
98092** use the value in register P3 as a key. If cursor P1 refers
98093** to an SQL index, then P3 is the first in an array of P4 registers
98094** that are used as an unpacked index key.
98095**
98096** Reposition cursor P1 so that it points to the smallest entry that
98097** is greater than the key value. If there are no records greater than
98098** the key and P2 is not zero, then jump to P2.
98099**
98100** This opcode leaves the cursor configured to move in forward order,
98101** from the beginning toward the end. In other words, the cursor is
98102** configured to use Next, not Prev.
98103**
98104** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
98105*/
98106/* Opcode: SeekLT P1 P2 P3 P4 *
98107** Synopsis: key=r[P3@P4]
98108**
98109** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
98110** use the value in register P3 as a key. If cursor P1 refers
98111** to an SQL index, then P3 is the first in an array of P4 registers
98112** that are used as an unpacked index key.
98113**
98114** Reposition cursor P1 so that it points to the largest entry that
98115** is less than the key value. If there are no records less than
98116** the key and P2 is not zero, then jump to P2.
98117**
98118** This opcode leaves the cursor configured to move in reverse order,
98119** from the end toward the beginning. In other words, the cursor is
98120** configured to use Prev, not Next.
98121**
98122** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
98123*/
98124/* Opcode: SeekLE P1 P2 P3 P4 *
98125** Synopsis: key=r[P3@P4]
98126**
98127** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
98128** use the value in register P3 as a key. If cursor P1 refers
98129** to an SQL index, then P3 is the first in an array of P4 registers
98130** that are used as an unpacked index key.
98131**
98132** Reposition cursor P1 so that it points to the largest entry that
98133** is less than or equal to the key value. If there are no records
98134** less than or equal to the key and P2 is not zero, then jump to P2.
98135**
98136** This opcode leaves the cursor configured to move in reverse order,
98137** from the end toward the beginning. In other words, the cursor is
98138** configured to use Prev, not Next.
98139**
98140** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
98141** opcode will either land on a record that exactly matches the key, or
98142** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
98143** this opcode must be followed by an IdxLE opcode with the same arguments.
98144** The IdxGE opcode will be skipped if this opcode succeeds, but the
98145** IdxGE opcode will be used on subsequent loop iterations. The
98146** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
98147** is an equality search.
98148**
98149** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
98150*/
98151case OP_SeekLT21: /* jump0, in3, group, ncycle */
98152case OP_SeekLE22: /* jump0, in3, group, ncycle */
98153case OP_SeekGE23: /* jump0, in3, group, ncycle */
98154case OP_SeekGT24: { /* jump0, in3, group, ncycle */
98155 int res; /* Comparison result */
98156 int oc; /* Opcode */
98157 VdbeCursor *pC; /* The cursor to seek */
98158 UnpackedRecord r; /* The key to seek for */
98159 int nField; /* Number of columns or fields in the key */
98160 i64 iKey; /* The rowid we are to seek to */
98161 int eqOnly; /* Only interested in == results */
98162
98163 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98164 assert( pOp->p2!=0 )((void) (0));
98165 pC = p->apCsr[pOp->p1];
98166 assert( pC!=0 )((void) (0));
98167 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
98168 assert( OP_SeekLE == OP_SeekLT+1 )((void) (0));
98169 assert( OP_SeekGE == OP_SeekLT+2 )((void) (0));
98170 assert( OP_SeekGT == OP_SeekLT+3 )((void) (0));
98171 assert( pC->isOrdered )((void) (0));
98172 assert( pC->uc.pCursor!=0 )((void) (0));
98173 oc = pOp->opcode;
98174 eqOnly = 0;
98175 pC->nullRow = 0;
98176#ifdef SQLITE_DEBUG
98177 pC->seekOp = pOp->opcode;
98178#endif
98179
98180 pC->deferredMoveto = 0;
98181 pC->cacheStatus = CACHE_STALE0;
98182 if( pC->isTable ){
98183 u16 flags3, newType;
98184 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
98185 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0((void) (0))
98186 || CORRUPT_DB )((void) (0));
98187
98188 /* The input value in P3 might be of any type: integer, real, string,
98189 ** blob, or NULL. But it needs to be an integer before we can do
98190 ** the seek, so convert it. */
98191 pIn3 = &aMem[pOp->p3];
98192 flags3 = pIn3->flags;
98193 if( (flags3 & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Str0x0002))==MEM_Str0x0002 ){
98194 applyNumericAffinity(pIn3, 0);
98195 }
98196 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
98197 newType = pIn3->flags; /* Record the type after applying numeric affinity */
98198 pIn3->flags = flags3; /* But convert the type back to its original */
98199
98200 /* If the P3 value could not be converted into an integer without
98201 ** loss of information, then special processing is required... */
98202 if( (newType & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
98203 int c;
98204 if( (newType & MEM_Real0x0008)==0 ){
98205 if( (newType & MEM_Null0x0001) || oc>=OP_SeekGE23 ){
98206 VdbeBranchTaken(1,2);
98207 goto jump_to_p2;
98208 }else{
98209 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
98210 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
98211 goto seek_not_found;
98212 }
98213 }
98214 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
98215
98216 /* If the approximation iKey is larger than the actual real search
98217 ** term, substitute >= for > and < for <=. e.g. if the search term
98218 ** is 4.9 and the integer approximation 5:
98219 **
98220 ** (x > 4.9) -> (x >= 5)
98221 ** (x <= 4.9) -> (x < 5)
98222 */
98223 if( c>0 ){
98224 assert( OP_SeekGE==(OP_SeekGT-1) )((void) (0));
98225 assert( OP_SeekLT==(OP_SeekLE-1) )((void) (0));
98226 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) )((void) (0));
98227 if( (oc & 0x0001)==(OP_SeekGT24 & 0x0001) ) oc--;
98228 }
98229
98230 /* If the approximation iKey is smaller than the actual real search
98231 ** term, substitute <= for < and > for >=. */
98232 else if( c<0 ){
98233 assert( OP_SeekLE==(OP_SeekLT+1) )((void) (0));
98234 assert( OP_SeekGT==(OP_SeekGE+1) )((void) (0));
98235 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) )((void) (0));
98236 if( (oc & 0x0001)==(OP_SeekLT21 & 0x0001) ) oc++;
98237 }
98238 }
98239 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
98240 pC->movetoTarget = iKey; /* Used by OP_Delete */
98241 if( rc!=SQLITE_OK0 ){
98242 goto abort_due_to_error;
98243 }
98244 }else{
98245 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
98246 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
98247 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
98248 ** with the same key.
98249 */
98250 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ0x00000002) ){
98251 eqOnly = 1;
98252 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE )((void) (0));
98253 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
98254 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT )((void) (0));
98255 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT )((void) (0));
98256 assert( pOp[1].p1==pOp[0].p1 )((void) (0));
98257 assert( pOp[1].p2==pOp[0].p2 )((void) (0));
98258 assert( pOp[1].p3==pOp[0].p3 )((void) (0));
98259 assert( pOp[1].p4.i==pOp[0].p4.i )((void) (0));
98260 }
98261
98262 nField = pOp->p4.i;
98263 assert( pOp->p4type==P4_INT32 )((void) (0));
98264 assert( nField>0 )((void) (0));
98265 r.pKeyInfo = pC->pKeyInfo;
98266 r.nField = (u16)nField;
98267
98268 /* The next line of code computes as follows, only faster:
98269 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
98270 ** r.default_rc = -1;
98271 ** }else{
98272 ** r.default_rc = +1;
98273 ** }
98274 */
98275 r.default_rc = ((1 & (oc - OP_SeekLT21)) ? -1 : +1);
98276 assert( oc!=OP_SeekGT || r.default_rc==-1 )((void) (0));
98277 assert( oc!=OP_SeekLE || r.default_rc==-1 )((void) (0));
98278 assert( oc!=OP_SeekGE || r.default_rc==+1 )((void) (0));
98279 assert( oc!=OP_SeekLT || r.default_rc==+1 )((void) (0));
98280
98281 r.aMem = &aMem[pOp->p3];
98282#ifdef SQLITE_DEBUG
98283 {
98284 int i;
98285 for(i=0; i<r.nField; i++){
98286 assert( memIsValid(&r.aMem[i]) )((void) (0));
98287 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
98288 }
98289 }
98290#endif
98291 r.eqSeen = 0;
98292 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
98293 if( rc!=SQLITE_OK0 ){
98294 goto abort_due_to_error;
98295 }
98296 if( eqOnly && r.eqSeen==0 ){
98297 assert( res!=0 )((void) (0));
98298 goto seek_not_found;
98299 }
98300 }
98301#ifdef SQLITE_TEST
98302 sqlite3_search_count++;
98303#endif
98304 if( oc>=OP_SeekGE23 ){ assert( oc==OP_SeekGE || oc==OP_SeekGT )((void) (0));
98305 if( res<0 || (res==0 && oc==OP_SeekGT24) ){
98306 res = 0;
98307 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
98308 if( rc!=SQLITE_OK0 ){
98309 if( rc==SQLITE_DONE101 ){
98310 rc = SQLITE_OK0;
98311 res = 1;
98312 }else{
98313 goto abort_due_to_error;
98314 }
98315 }
98316 }else{
98317 res = 0;
98318 }
98319 }else{
98320 assert( oc==OP_SeekLT || oc==OP_SeekLE )((void) (0));
98321 if( res>0 || (res==0 && oc==OP_SeekLT21) ){
98322 res = 0;
98323 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
98324 if( rc!=SQLITE_OK0 ){
98325 if( rc==SQLITE_DONE101 ){
98326 rc = SQLITE_OK0;
98327 res = 1;
98328 }else{
98329 goto abort_due_to_error;
98330 }
98331 }
98332 }else{
98333 /* res might be negative because the table is empty. Check to
98334 ** see if this is the case.
98335 */
98336 res = sqlite3BtreeEof(pC->uc.pCursor);
98337 }
98338 }
98339seek_not_found:
98340 assert( pOp->p2>0 )((void) (0));
98341 VdbeBranchTaken(res!=0,2);
98342 if( res ){
98343 goto jump_to_p2;
98344 }else if( eqOnly ){
98345 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
98346 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
98347 }
98348 break;
98349}
98350
98351
98352/* Opcode: SeekScan P1 P2 * * P5
98353** Synopsis: Scan-ahead up to P1 rows
98354**
98355** This opcode is a prefix opcode to OP_SeekGE. In other words, this
98356** opcode must be immediately followed by OP_SeekGE. This constraint is
98357** checked by assert() statements.
98358**
98359** This opcode uses the P1 through P4 operands of the subsequent
98360** OP_SeekGE. In the text that follows, the operands of the subsequent
98361** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
98362** the P1, P2 and P5 operands of this opcode are also used, and are called
98363** This.P1, This.P2 and This.P5.
98364**
98365** This opcode helps to optimize IN operators on a multi-column index
98366** where the IN operator is on the later terms of the index by avoiding
98367** unnecessary seeks on the btree, substituting steps to the next row
98368** of the b-tree instead. A correct answer is obtained if this opcode
98369** is omitted or is a no-op.
98370**
98371** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
98372** is the desired entry that we want the cursor SeekGE.P1 to be pointing
98373** to. Call this SeekGE.P3/P4 row the "target".
98374**
98375** If the SeekGE.P1 cursor is not currently pointing to a valid row,
98376** then this opcode is a no-op and control passes through into the OP_SeekGE.
98377**
98378** If the SeekGE.P1 cursor is pointing to a valid row, then that row
98379** might be the target row, or it might be near and slightly before the
98380** target row, or it might be after the target row. If the cursor is
98381** currently before the target row, then this opcode attempts to position
98382** the cursor on or after the target row by invoking sqlite3BtreeStep()
98383** on the cursor between 1 and This.P1 times.
98384**
98385** The This.P5 parameter is a flag that indicates what to do if the
98386** cursor ends up pointing at a valid row that is past the target
98387** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
98388** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
98389** case occurs when there are no inequality constraints to the right of
98390** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case
98391** occurs when there are inequality constraints to the right of the IN
98392** operator. In that case, the This.P2 will point either directly to or
98393** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
98394** loop terminate.
98395**
98396** Possible outcomes from this opcode:<ol>
98397**
98398** <li> If the cursor is initially not pointed to any valid row, then
98399** fall through into the subsequent OP_SeekGE opcode.
98400**
98401** <li> If the cursor is left pointing to a row that is before the target
98402** row, even after making as many as This.P1 calls to
98403** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
98404**
98405** <li> If the cursor is left pointing at the target row, either because it
98406** was at the target row to begin with or because one or more
98407** sqlite3BtreeNext() calls moved the cursor to the target row,
98408** then jump to This.P2..,
98409**
98410** <li> If the cursor started out before the target row and a call to
98411** to sqlite3BtreeNext() moved the cursor off the end of the index
98412** (indicating that the target row definitely does not exist in the
98413** btree) then jump to SeekGE.P2, ending the loop.
98414**
98415** <li> If the cursor ends up on a valid row that is past the target row
98416** (indicating that the target row does not exist in the btree) then
98417** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
98418** </ol>
98419*/
98420case OP_SeekScan124: { /* ncycle */
98421 VdbeCursor *pC;
98422 int res;
98423 int nStep;
98424 UnpackedRecord r;
98425
98426 assert( pOp[1].opcode==OP_SeekGE )((void) (0));
98427
98428 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
98429 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
98430 ** opcode past the OP_SeekGE itself. */
98431 assert( pOp->p2>=(int)(pOp-aOp)+2 )((void) (0));
98432#ifdef SQLITE_DEBUG
98433 if( pOp->p5==0 ){
98434 /* There are no inequality constraints following the IN constraint. */
98435 assert( pOp[1].p1==aOp[pOp->p2-1].p1 )((void) (0));
98436 assert( pOp[1].p2==aOp[pOp->p2-1].p2 )((void) (0));
98437 assert( pOp[1].p3==aOp[pOp->p2-1].p3 )((void) (0));
98438 assert( aOp[pOp->p2-1].opcode==OP_IdxGT((void) (0))
98439 || aOp[pOp->p2-1].opcode==OP_IdxGE )((void) (0));
98440 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
98441 }else{
98442 /* There are inequality constraints. */
98443 assert( pOp->p2==(int)(pOp-aOp)+2 )((void) (0));
98444 assert( aOp[pOp->p2-1].opcode==OP_SeekGE )((void) (0));
98445 }
98446#endif
98447
98448 assert( pOp->p1>0 )((void) (0));
98449 pC = p->apCsr[pOp[1].p1];
98450 assert( pC!=0 )((void) (0));
98451 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
98452 assert( !pC->isTable )((void) (0));
98453 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
98454#ifdef SQLITE_DEBUG
98455 if( db->flags&SQLITE_VdbeTrace ){
98456 printf("... cursor not valid - fall through\n");
98457 }
98458#endif
98459 break;
98460 }
98461 nStep = pOp->p1;
98462 assert( nStep>=1 )((void) (0));
98463 r.pKeyInfo = pC->pKeyInfo;
98464 r.nField = (u16)pOp[1].p4.i;
98465 r.default_rc = 0;
98466 r.aMem = &aMem[pOp[1].p3];
98467#ifdef SQLITE_DEBUG
98468 {
98469 int i;
98470 for(i=0; i<r.nField; i++){
98471 assert( memIsValid(&r.aMem[i]) )((void) (0));
98472 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
98473 }
98474 }
98475#endif
98476 res = 0; /* Not needed. Only used to silence a warning. */
98477 while(1){
98478 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
98479 if( rc ) goto abort_due_to_error;
98480 if( res>0 && pOp->p5==0 ){
98481 seekscan_search_fail:
98482 /* Jump to SeekGE.P2, ending the loop */
98483#ifdef SQLITE_DEBUG
98484 if( db->flags&SQLITE_VdbeTrace ){
98485 printf("... %d steps and then skip\n", pOp->p1 - nStep);
98486 }
98487#endif
98488 VdbeBranchTaken(1,3);
98489 pOp++;
98490 goto jump_to_p2;
98491 }
98492 if( res>=0 ){
98493 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
98494#ifdef SQLITE_DEBUG
98495 if( db->flags&SQLITE_VdbeTrace ){
98496 printf("... %d steps and then success\n", pOp->p1 - nStep);
98497 }
98498#endif
98499 VdbeBranchTaken(2,3);
98500 goto jump_to_p2;
98501 break;
98502 }
98503 if( nStep<=0 ){
98504#ifdef SQLITE_DEBUG
98505 if( db->flags&SQLITE_VdbeTrace ){
98506 printf("... fall through after %d steps\n", pOp->p1);
98507 }
98508#endif
98509 VdbeBranchTaken(0,3);
98510 break;
98511 }
98512 nStep--;
98513 pC->cacheStatus = CACHE_STALE0;
98514 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
98515 if( rc ){
98516 if( rc==SQLITE_DONE101 ){
98517 rc = SQLITE_OK0;
98518 goto seekscan_search_fail;
98519 }else{
98520 goto abort_due_to_error;
98521 }
98522 }
98523 }
98524
98525 break;
98526}
98527
98528
98529/* Opcode: SeekHit P1 P2 P3 * *
98530** Synopsis: set P2<=seekHit<=P3
98531**
98532** Increase or decrease the seekHit value for cursor P1, if necessary,
98533** so that it is no less than P2 and no greater than P3.
98534**
98535** The seekHit integer represents the maximum of terms in an index for which
98536** there is known to be at least one match. If the seekHit value is smaller
98537** than the total number of equality terms in an index lookup, then the
98538** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
98539** early, thus saving work. This is part of the IN-early-out optimization.
98540**
98541** P1 must be a valid b-tree cursor.
98542*/
98543case OP_SeekHit125: { /* ncycle */
98544 VdbeCursor *pC;
98545 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98546 pC = p->apCsr[pOp->p1];
98547 assert( pC!=0 )((void) (0));
98548 assert( pOp->p3>=pOp->p2 )((void) (0));
98549 if( pC->seekHit<pOp->p2 ){
98550#ifdef SQLITE_DEBUG
98551 if( db->flags&SQLITE_VdbeTrace ){
98552 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
98553 }
98554#endif
98555 pC->seekHit = pOp->p2;
98556 }else if( pC->seekHit>pOp->p3 ){
98557#ifdef SQLITE_DEBUG
98558 if( db->flags&SQLITE_VdbeTrace ){
98559 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
98560 }
98561#endif
98562 pC->seekHit = pOp->p3;
98563 }
98564 break;
98565}
98566
98567/* Opcode: IfNotOpen P1 P2 * * *
98568** Synopsis: if( !csr[P1] ) goto P2
98569**
98570** If cursor P1 is not open or if P1 is set to a NULL row using the
98571** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
98572*/
98573case OP_IfNotOpen25: { /* jump */
98574 VdbeCursor *pCur;
98575
98576 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98577 pCur = p->apCsr[pOp->p1];
98578 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
98579 if( pCur==0 || pCur->nullRow ){
98580 goto jump_to_p2_and_check_for_interrupt;
98581 }
98582 break;
98583}
98584
98585/* Opcode: Found P1 P2 P3 P4 *
98586** Synopsis: key=r[P3@P4]
98587**
98588** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
98589** P4>0 then register P3 is the first of P4 registers that form an unpacked
98590** record.
98591**
98592** Cursor P1 is on an index btree. If the record identified by P3 and P4
98593** is a prefix of any entry in P1 then a jump is made to P2 and
98594** P1 is left pointing at the matching entry.
98595**
98596** This operation leaves the cursor in a state where it can be
98597** advanced in the forward direction. The Next instruction will work,
98598** but not the Prev instruction.
98599**
98600** See also: NotFound, NoConflict, NotExists. SeekGe
98601*/
98602/* Opcode: NotFound P1 P2 P3 P4 *
98603** Synopsis: key=r[P3@P4]
98604**
98605** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
98606** P4>0 then register P3 is the first of P4 registers that form an unpacked
98607** record.
98608**
98609** Cursor P1 is on an index btree. If the record identified by P3 and P4
98610** is not the prefix of any entry in P1 then a jump is made to P2. If P1
98611** does contain an entry whose prefix matches the P3/P4 record then control
98612** falls through to the next instruction and P1 is left pointing at the
98613** matching entry.
98614**
98615** This operation leaves the cursor in a state where it cannot be
98616** advanced in either direction. In other words, the Next and Prev
98617** opcodes do not work after this operation.
98618**
98619** See also: Found, NotExists, NoConflict, IfNoHope
98620*/
98621/* Opcode: IfNoHope P1 P2 P3 P4 *
98622** Synopsis: key=r[P3@P4]
98623**
98624** Register P3 is the first of P4 registers that form an unpacked
98625** record. Cursor P1 is an index btree. P2 is a jump destination.
98626** In other words, the operands to this opcode are the same as the
98627** operands to OP_NotFound and OP_IdxGT.
98628**
98629** This opcode is an optimization attempt only. If this opcode always
98630** falls through, the correct answer is still obtained, but extra work
98631** is performed.
98632**
98633** A value of N in the seekHit flag of cursor P1 means that there exists
98634** a key P3:N that will match some record in the index. We want to know
98635** if it is possible for a record P3:P4 to match some record in the
98636** index. If it is not possible, we can skip some work. So if seekHit
98637** is less than P4, attempt to find out if a match is possible by running
98638** OP_NotFound.
98639**
98640** This opcode is used in IN clause processing for a multi-column key.
98641** If an IN clause is attached to an element of the key other than the
98642** left-most element, and if there are no matches on the most recent
98643** seek over the whole key, then it might be that one of the key element
98644** to the left is prohibiting a match, and hence there is "no hope" of
98645** any match regardless of how many IN clause elements are checked.
98646** In such a case, we abandon the IN clause search early, using this
98647** opcode. The opcode name comes from the fact that the
98648** jump is taken if there is "no hope" of achieving a match.
98649**
98650** See also: NotFound, SeekHit
98651*/
98652/* Opcode: NoConflict P1 P2 P3 P4 *
98653** Synopsis: key=r[P3@P4]
98654**
98655** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
98656** P4>0 then register P3 is the first of P4 registers that form an unpacked
98657** record.
98658**
98659** Cursor P1 is on an index btree. If the record identified by P3 and P4
98660** contains any NULL value, jump immediately to P2. If all terms of the
98661** record are not-NULL then a check is done to determine if any row in the
98662** P1 index btree has a matching key prefix. If there are no matches, jump
98663** immediately to P2. If there is a match, fall through and leave the P1
98664** cursor pointing to the matching row.
98665**
98666** This opcode is similar to OP_NotFound with the exceptions that the
98667** branch is always taken if any part of the search key input is NULL.
98668**
98669** This operation leaves the cursor in a state where it cannot be
98670** advanced in either direction. In other words, the Next and Prev
98671** opcodes do not work after this operation.
98672**
98673** See also: NotFound, Found, NotExists
98674*/
98675case OP_IfNoHope26: { /* jump, in3, ncycle */
98676 VdbeCursor *pC;
98677 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98678 pC = p->apCsr[pOp->p1];
98679 assert( pC!=0 )((void) (0));
98680#ifdef SQLITE_DEBUG
98681 if( db->flags&SQLITE_VdbeTrace ){
98682 printf("seekHit is %d\n", pC->seekHit);
98683 }
98684#endif
98685 if( pC->seekHit>=pOp->p4.i ) break;
98686 /* Fall through into OP_NotFound */
98687 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98688}
98689case OP_NoConflict27: /* jump, in3, ncycle */
98690case OP_NotFound28: /* jump, in3, ncycle */
98691case OP_Found29: { /* jump, in3, ncycle */
98692 int alreadyExists;
98693 int ii;
98694 VdbeCursor *pC;
98695 UnpackedRecord *pIdxKey;
98696 UnpackedRecord r;
98697
98698#ifdef SQLITE_TEST
98699 if( pOp->opcode!=OP_NoConflict27 ) sqlite3_found_count++;
98700#endif
98701
98702 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98703 assert( pOp->p4type==P4_INT32 )((void) (0));
98704 pC = p->apCsr[pOp->p1];
98705 assert( pC!=0 )((void) (0));
98706#ifdef SQLITE_DEBUG
98707 pC->seekOp = pOp->opcode;
98708#endif
98709 r.aMem = &aMem[pOp->p3];
98710 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
98711 assert( pC->uc.pCursor!=0 )((void) (0));
98712 assert( pC->isTable==0 )((void) (0));
98713 r.nField = (u16)pOp->p4.i;
98714 if( r.nField>0 ){
98715 /* Key values in an array of registers */
98716 r.pKeyInfo = pC->pKeyInfo;
98717 r.default_rc = 0;
98718#ifdef SQLITE_DEBUG
98719 (void)sqlite3FaultSim(50); /* For use by --counter in TH3 */
98720 for(ii=0; ii<r.nField; ii++){
98721 assert( memIsValid(&r.aMem[ii]) )((void) (0));
98722 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 )((void) (0));
98723 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
98724 }
98725#endif
98726 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
98727 }else{
98728 /* Composite key generated by OP_MakeRecord */
98729 assert( r.aMem->flags & MEM_Blob )((void) (0));
98730 assert( pOp->opcode!=OP_NoConflict )((void) (0));
98731 rc = ExpandBlob(r.aMem)(((r.aMem)->flags&0x0400)?sqlite3VdbeMemExpandBlob(r.aMem
):0)
;
98732 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
98733 if( rc ) goto no_mem;
98734 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
98735 if( pIdxKey==0 ) goto no_mem;
98736 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
98737 pIdxKey->default_rc = 0;
98738 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
98739 sqlite3DbFreeNN(db, pIdxKey);
98740 }
98741 if( rc!=SQLITE_OK0 ){
98742 goto abort_due_to_error;
98743 }
98744 alreadyExists = (pC->seekResult==0);
98745 pC->nullRow = 1-alreadyExists;
98746 pC->deferredMoveto = 0;
98747 pC->cacheStatus = CACHE_STALE0;
98748 if( pOp->opcode==OP_Found29 ){
98749 VdbeBranchTaken(alreadyExists!=0,2);
98750 if( alreadyExists ) goto jump_to_p2;
98751 }else{
98752 if( !alreadyExists ){
98753 VdbeBranchTaken(1,2);
98754 goto jump_to_p2;
98755 }
98756 if( pOp->opcode==OP_NoConflict27 ){
98757 /* For the OP_NoConflict opcode, take the jump if any of the
98758 ** input fields are NULL, since any key with a NULL will not
98759 ** conflict */
98760 for(ii=0; ii<r.nField; ii++){
98761 if( r.aMem[ii].flags & MEM_Null0x0001 ){
98762 VdbeBranchTaken(1,2);
98763 goto jump_to_p2;
98764 }
98765 }
98766 }
98767 VdbeBranchTaken(0,2);
98768 if( pOp->opcode==OP_IfNoHope26 ){
98769 pC->seekHit = pOp->p4.i;
98770 }
98771 }
98772 break;
98773}
98774
98775/* Opcode: SeekRowid P1 P2 P3 * *
98776** Synopsis: intkey=r[P3]
98777**
98778** P1 is the index of a cursor open on an SQL table btree (with integer
98779** keys). If register P3 does not contain an integer or if P1 does not
98780** contain a record with rowid P3 then jump immediately to P2.
98781** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
98782** a record with rowid P3 then
98783** leave the cursor pointing at that record and fall through to the next
98784** instruction.
98785**
98786** The OP_NotExists opcode performs the same operation, but with OP_NotExists
98787** the P3 register must be guaranteed to contain an integer value. With this
98788** opcode, register P3 might not contain an integer.
98789**
98790** The OP_NotFound opcode performs the same operation on index btrees
98791** (with arbitrary multi-value keys).
98792**
98793** This opcode leaves the cursor in a state where it cannot be advanced
98794** in either direction. In other words, the Next and Prev opcodes will
98795** not work following this opcode.
98796**
98797** See also: Found, NotFound, NoConflict, SeekRowid
98798*/
98799/* Opcode: NotExists P1 P2 P3 * *
98800** Synopsis: intkey=r[P3]
98801**
98802** P1 is the index of a cursor open on an SQL table btree (with integer
98803** keys). P3 is an integer rowid. If P1 does not contain a record with
98804** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
98805** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
98806** leave the cursor pointing at that record and fall through to the next
98807** instruction.
98808**
98809** The OP_SeekRowid opcode performs the same operation but also allows the
98810** P3 register to contain a non-integer value, in which case the jump is
98811** always taken. This opcode requires that P3 always contain an integer.
98812**
98813** The OP_NotFound opcode performs the same operation on index btrees
98814** (with arbitrary multi-value keys).
98815**
98816** This opcode leaves the cursor in a state where it cannot be advanced
98817** in either direction. In other words, the Next and Prev opcodes will
98818** not work following this opcode.
98819**
98820** See also: Found, NotFound, NoConflict, SeekRowid
98821*/
98822case OP_SeekRowid30: { /* jump0, in3, ncycle */
98823 VdbeCursor *pC;
98824 BtCursor *pCrsr;
98825 int res;
98826 u64 iKey;
98827
98828 pIn3 = &aMem[pOp->p3];
98829 testcase( pIn3->flags & MEM_Int );
98830 testcase( pIn3->flags & MEM_IntReal );
98831 testcase( pIn3->flags & MEM_Real );
98832 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
98833 if( (pIn3->flags & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
98834 /* If pIn3->u.i does not contain an integer, compute iKey as the
98835 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
98836 ** into an integer without loss of information. Take care to avoid
98837 ** changing the datatype of pIn3, however, as it is used by other
98838 ** parts of the prepared statement. */
98839 Mem x = pIn3[0];
98840 applyAffinity(&x, SQLITE_AFF_NUMERIC0x43, encoding);
98841 if( (x.flags & MEM_Int0x0004)==0 ) goto jump_to_p2;
98842 iKey = x.u.i;
98843 goto notExistsWithKey;
98844 }
98845 /* Fall through into OP_NotExists */
98846 /* no break */ deliberate_fall_through__attribute__((fallthrough));
98847case OP_NotExists31: /* jump, in3, ncycle */
98848 pIn3 = &aMem[pOp->p3];
98849 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid )((void) (0));
98850 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98851 iKey = pIn3->u.i;
98852notExistsWithKey:
98853 pC = p->apCsr[pOp->p1];
98854 assert( pC!=0 )((void) (0));
98855#ifdef SQLITE_DEBUG
98856 if( pOp->opcode==OP_SeekRowid30 ) pC->seekOp = OP_SeekRowid30;
98857#endif
98858 assert( pC->isTable )((void) (0));
98859 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
98860 pCrsr = pC->uc.pCursor;
98861 assert( pCrsr!=0 )((void) (0));
98862 res = 0;
98863 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res);
98864 assert( rc==SQLITE_OK || res==0 )((void) (0));
98865 pC->movetoTarget = iKey; /* Used by OP_Delete */
98866 pC->nullRow = 0;
98867 pC->cacheStatus = CACHE_STALE0;
98868 pC->deferredMoveto = 0;
98869 VdbeBranchTaken(res!=0,2);
98870 pC->seekResult = res;
98871 if( res!=0 ){
98872 assert( rc==SQLITE_OK )((void) (0));
98873 if( pOp->p2==0 ){
98874 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(98874);
98875 }else{
98876 goto jump_to_p2;
98877 }
98878 }
98879 if( rc ) goto abort_due_to_error;
98880 break;
98881}
98882
98883/* Opcode: Sequence P1 P2 * * *
98884** Synopsis: r[P2]=cursor[P1].ctr++
98885**
98886** Find the next available sequence number for cursor P1.
98887** Write the sequence number into register P2.
98888** The sequence number on the cursor is incremented after this
98889** instruction.
98890*/
98891case OP_Sequence126: { /* out2 */
98892 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98893 assert( p->apCsr[pOp->p1]!=0 )((void) (0));
98894 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB )((void) (0));
98895 pOut = out2Prerelease(p, pOp);
98896 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
98897 break;
98898}
98899
98900
98901/* Opcode: NewRowid P1 P2 P3 * *
98902** Synopsis: r[P2]=rowid
98903**
98904** Get a new integer record number (a.k.a "rowid") used as the key to a table.
98905** The record number is not previously used as a key in the database
98906** table that cursor P1 points to. The new record number is written
98907** written to register P2.
98908**
98909** If P3>0 then P3 is a register in the root frame of this VDBE that holds
98910** the largest previously generated record number. No new record numbers are
98911** allowed to be less than this value. When this value reaches its maximum,
98912** an SQLITE_FULL error is generated. The P3 register is updated with the '
98913** generated record number. This P3 mechanism is used to help implement the
98914** AUTOINCREMENT feature.
98915*/
98916case OP_NewRowid127: { /* out2 */
98917 i64 v; /* The new rowid */
98918 VdbeCursor *pC; /* Cursor of table to get the new rowid */
98919 int res; /* Result of an sqlite3BtreeLast() */
98920 int cnt; /* Counter to limit the number of searches */
98921#ifndef SQLITE_OMIT_AUTOINCREMENT
98922 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
98923 VdbeFrame *pFrame; /* Root frame of VDBE */
98924#endif
98925
98926 v = 0;
98927 res = 0;
98928 pOut = out2Prerelease(p, pOp);
98929 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
98930 pC = p->apCsr[pOp->p1];
98931 assert( pC!=0 )((void) (0));
98932 assert( pC->isTable )((void) (0));
98933 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
98934 assert( pC->uc.pCursor!=0 )((void) (0));
98935 {
98936 /* The next rowid or record number (different terms for the same
98937 ** thing) is obtained in a two-step algorithm.
98938 **
98939 ** First we attempt to find the largest existing rowid and add one
98940 ** to that. But if the largest existing rowid is already the maximum
98941 ** positive integer, we have to fall through to the second
98942 ** probabilistic algorithm
98943 **
98944 ** The second algorithm is to select a rowid at random and see if
98945 ** it already exists in the table. If it does not exist, we have
98946 ** succeeded. If the random rowid does exist, we select a new one
98947 ** and try again, up to 100 times.
98948 */
98949 assert( pC->isTable )((void) (0));
98950
98951#ifdef SQLITE_32BIT_ROWID
98952# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 0x7fffffff
98953#else
98954 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
98955 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
98956 ** to provide the constant while making all compilers happy.
98957 */
98958# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
98959#endif
98960
98961 if( !pC->useRandomRowid ){
98962 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
98963 if( rc!=SQLITE_OK0 ){
98964 goto abort_due_to_error;
98965 }
98966 if( res ){
98967 v = 1; /* IMP: R-61914-48074 */
98968 }else{
98969 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) )((void) (0));
98970 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98971 if( v>=MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) ){
98972 pC->useRandomRowid = 1;
98973 }else{
98974 v++; /* IMP: R-29538-34987 */
98975 }
98976 }
98977 }
98978
98979#ifndef SQLITE_OMIT_AUTOINCREMENT
98980 if( pOp->p3 ){
98981 /* Assert that P3 is a valid memory cell. */
98982 assert( pOp->p3>0 )((void) (0));
98983 if( p->pFrame ){
98984 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
98985 /* Assert that P3 is a valid memory cell. */
98986 assert( pOp->p3<=pFrame->nMem )((void) (0));
98987 pMem = &pFrame->aMem[pOp->p3];
98988 }else{
98989 /* Assert that P3 is a valid memory cell. */
98990 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
98991 pMem = &aMem[pOp->p3];
98992 memAboutToChange(p, pMem);
98993 }
98994 assert( memIsValid(pMem) )((void) (0));
98995
98996 REGISTER_TRACE(pOp->p3, pMem);
98997 sqlite3VdbeMemIntegerify(pMem);
98998 assert( (pMem->flags & MEM_Int)!=0 )((void) (0)); /* mem(P3) holds an integer */
98999 if( pMem->u.i==MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) || pC->useRandomRowid ){
99000 rc = SQLITE_FULL13; /* IMP: R-17817-00630 */
99001 goto abort_due_to_error;
99002 }
99003 if( v<pMem->u.i+1 ){
99004 v = pMem->u.i + 1;
99005 }
99006 pMem->u.i = v;
99007 }
99008#endif
99009 if( pC->useRandomRowid ){
99010 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
99011 ** largest possible integer (9223372036854775807) then the database
99012 ** engine starts picking positive candidate ROWIDs at random until
99013 ** it finds one that is not previously used. */
99014 assert( pOp->p3==0 )((void) (0)); /* We cannot be in random rowid mode if this is
99015 ** an AUTOINCREMENT table. */
99016 cnt = 0;
99017 do{
99018 sqlite3_randomness(sizeof(v), &v);
99019 v &= (MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )>>1); v++; /* Ensure that v is greater than zero */
99020 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
99021 0, &res))==SQLITE_OK0)
99022 && (res==0)
99023 && (++cnt<100));
99024 if( rc ) goto abort_due_to_error;
99025 if( res==0 ){
99026 rc = SQLITE_FULL13; /* IMP: R-38219-53002 */
99027 goto abort_due_to_error;
99028 }
99029 assert( v>0 )((void) (0)); /* EV: R-40812-03570 */
99030 }
99031 pC->deferredMoveto = 0;
99032 pC->cacheStatus = CACHE_STALE0;
99033 }
99034 pOut->u.i = v;
99035 break;
99036}
99037
99038/* Opcode: Insert P1 P2 P3 P4 P5
99039** Synopsis: intkey=r[P3] data=r[P2]
99040**
99041** Write an entry into the table of cursor P1. A new entry is
99042** created if it doesn't already exist or the data for an existing
99043** entry is overwritten. The data is the value MEM_Blob stored in register
99044** number P2. The key is stored in register P3. The key must
99045** be a MEM_Int.
99046**
99047** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
99048** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
99049** then rowid is stored for subsequent return by the
99050** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
99051**
99052** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
99053** run faster by avoiding an unnecessary seek on cursor P1. However,
99054** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
99055** seeks on the cursor or if the most recent seek used a key equal to P3.
99056**
99057** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
99058** UPDATE operation. Otherwise (if the flag is clear) then this opcode
99059** is part of an INSERT operation. The difference is only important to
99060** the update hook.
99061**
99062** Parameter P4 may point to a Table structure, or may be NULL. If it is
99063** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
99064** following a successful insert.
99065**
99066** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
99067** allocated, then ownership of P2 is transferred to the pseudo-cursor
99068** and register P2 becomes ephemeral. If the cursor is changed, the
99069** value of register P2 will then change. Make sure this does not
99070** cause any problems.)
99071**
99072** This instruction only works on tables. The equivalent instruction
99073** for indices is OP_IdxInsert.
99074*/
99075case OP_Insert128: {
99076 Mem *pData; /* MEM cell holding data for the record to be inserted */
99077 Mem *pKey; /* MEM cell holding key for the record */
99078 VdbeCursor *pC; /* Cursor to table into which insert is written */
99079 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
99080 const char *zDb; /* database name - used by the update hook */
99081 Table *pTab; /* Table structure - used by update and pre-update hooks */
99082 BtreePayload x; /* Payload to be inserted */
99083
99084 pData = &aMem[pOp->p2];
99085 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99086 assert( memIsValid(pData) )((void) (0));
99087 pC = p->apCsr[pOp->p1];
99088 assert( pC!=0 )((void) (0));
99089 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99090 assert( pC->deferredMoveto==0 )((void) (0));
99091 assert( pC->uc.pCursor!=0 )((void) (0));
99092 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable )((void) (0));
99093 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC )((void) (0));
99094 REGISTER_TRACE(pOp->p2, pData);
99095 sqlite3VdbeIncrWriteCounter(p, pC);
99096
99097 pKey = &aMem[pOp->p3];
99098 assert( pKey->flags & MEM_Int )((void) (0));
99099 assert( memIsValid(pKey) )((void) (0));
99100 REGISTER_TRACE(pOp->p3, pKey);
99101 x.nKey = pKey->u.i;
99102
99103 if( pOp->p4type==P4_TABLE(-5) && HAS_UPDATE_HOOK(db)((db)->xPreUpdateCallback||(db)->xUpdateCallback) ){
99104 assert( pC->iDb>=0 )((void) (0));
99105 zDb = db->aDb[pC->iDb].zDbSName;
99106 pTab = pOp->p4.pTab;
99107 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) )((void) (0));
99108 }else{
99109 pTab = 0;
99110 zDb = 0;
99111 }
99112
99113#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
99114 /* Invoke the pre-update hook, if any */
99115 if( pTab ){
99116 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE0x04) ){
99117 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT18,zDb,pTab,x.nKey,pOp->p2,-1);
99118 }
99119 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
99120 /* Prevent post-update hook from running in cases when it should not */
99121 pTab = 0;
99122 }
99123 }
99124 if( pOp->p5 & OPFLAG_ISNOOP0x40 ) break;
99125#endif
99126
99127 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 )((void) (0));
99128 if( pOp->p5 & OPFLAG_NCHANGE0x01 ){
99129 p->nChange++;
99130 if( pOp->p5 & OPFLAG_LASTROWID0x20 ) db->lastRowid = x.nKey;
99131 }
99132 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 )((void) (0));
99133 x.pData = pData->z;
99134 x.nData = pData->n;
99135 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0);
99136 if( pData->flags & MEM_Zero0x0400 ){
99137 x.nZero = pData->u.nZero;
99138 }else{
99139 x.nZero = 0;
99140 }
99141 x.pKey = 0;
99142 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT )((void) (0));
99143 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
99144 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02|OPFLAG_PREFORMAT0x80)),
99145 seekResult
99146 );
99147 pC->deferredMoveto = 0;
99148 pC->cacheStatus = CACHE_STALE0;
99149 colCacheCtr++;
99150
99151 /* Invoke the update-hook if required. */
99152 if( rc ) goto abort_due_to_error;
99153 if( pTab ){
99154 assert( db->xUpdateCallback!=0 )((void) (0));
99155 assert( pTab->aCol!=0 )((void) (0));
99156 db->xUpdateCallback(db->pUpdateArg,
99157 (pOp->p5 & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_INSERT18,
99158 zDb, pTab->zName, x.nKey);
99159 }
99160 break;
99161}
99162
99163/* Opcode: RowCell P1 P2 P3 * *
99164**
99165** P1 and P2 are both open cursors. Both must be opened on the same type
99166** of table - intkey or index. This opcode is used as part of copying
99167** the current row from P2 into P1. If the cursors are opened on intkey
99168** tables, register P3 contains the rowid to use with the new record in
99169** P1. If they are opened on index tables, P3 is not used.
99170**
99171** This opcode must be followed by either an Insert or InsertIdx opcode
99172** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
99173*/
99174case OP_RowCell129: {
99175 VdbeCursor *pDest; /* Cursor to write to */
99176 VdbeCursor *pSrc; /* Cursor to read from */
99177 i64 iKey; /* Rowid value to insert with */
99178 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert )((void) (0));
99179 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 )((void) (0));
99180 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 )((void) (0));
99181 assert( pOp[1].p5 & OPFLAG_PREFORMAT )((void) (0));
99182 pDest = p->apCsr[pOp->p1];
99183 pSrc = p->apCsr[pOp->p2];
99184 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
99185 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
99186 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
99187 break;
99188};
99189
99190/* Opcode: Delete P1 P2 P3 P4 P5
99191**
99192** Delete the record at which the P1 cursor is currently pointing.
99193**
99194** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
99195** the cursor will be left pointing at either the next or the previous
99196** record in the table. If it is left pointing at the next record, then
99197** the next Next instruction will be a no-op. As a result, in this case
99198** it is ok to delete a record from within a Next loop. If
99199** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
99200** left in an undefined state.
99201**
99202** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
99203** delete is one of several associated with deleting a table row and
99204** all its associated index entries. Exactly one of those deletes is
99205** the "primary" delete. The others are all on OPFLAG_FORDELETE
99206** cursors or else are marked with the AUXDELETE flag.
99207**
99208** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then
99209** the row change count is incremented (otherwise not).
99210**
99211** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the
99212** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
99213** This happens when the OP_Delete is to be shortly followed by an OP_Insert
99214** with the same key, causing the btree entry to be overwritten.
99215**
99216** P1 must not be pseudo-table. It has to be a real table with
99217** multiple rows.
99218**
99219** If P4 is not NULL then it points to a Table object. In this case either
99220** the update or pre-update hook, or both, may be invoked. The P1 cursor must
99221** have been positioned using OP_NotFound prior to invoking this opcode in
99222** this case. Specifically, if one is configured, the pre-update hook is
99223** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
99224** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
99225**
99226** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
99227** of the memory cell that contains the value that the rowid of the row will
99228** be set to by the update.
99229*/
99230case OP_Delete130: {
99231 VdbeCursor *pC;
99232 const char *zDb;
99233 Table *pTab;
99234 int opflags;
99235
99236 opflags = pOp->p2;
99237 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99238 pC = p->apCsr[pOp->p1];
99239 assert( pC!=0 )((void) (0));
99240 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99241 assert( pC->uc.pCursor!=0 )((void) (0));
99242 assert( pC->deferredMoveto==0 )((void) (0));
99243 sqlite3VdbeIncrWriteCounter(p, pC);
99244
99245#ifdef SQLITE_DEBUG
99246 if( pOp->p4type==P4_TABLE(-5)
99247 && HasRowid(pOp->p4.pTab)(((pOp->p4.pTab)->tabFlags & 0x00000080)==0)
99248 && pOp->p5==0
99249 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
99250 ){
99251 /* If p5 is zero, the seek operation that positioned the cursor prior to
99252 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
99253 ** the row that is being deleted */
99254 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99255 assert( CORRUPT_DB || pC->movetoTarget==iKey )((void) (0));
99256 }
99257#endif
99258
99259 /* If the update-hook or pre-update-hook will be invoked, set zDb to
99260 ** the name of the db to pass as to it. Also set local pTab to a copy
99261 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
99262 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
99263 ** VdbeCursor.movetoTarget to the current rowid. */
99264 if( pOp->p4type==P4_TABLE(-5) && HAS_UPDATE_HOOK(db)((db)->xPreUpdateCallback||(db)->xUpdateCallback) ){
99265 assert( pC->iDb>=0 )((void) (0));
99266 assert( pOp->p4.pTab!=0 )((void) (0));
99267 zDb = db->aDb[pC->iDb].zDbSName;
99268 pTab = pOp->p4.pTab;
99269 if( (pOp->p5 & OPFLAG_SAVEPOSITION0x02)!=0 && pC->isTable ){
99270 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99271 }
99272 }else{
99273 zDb = 0;
99274 pTab = 0;
99275 }
99276
99277#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
99278 /* Invoke the pre-update-hook if required. */
99279 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab )((void) (0));
99280 if( db->xPreUpdateCallback && pTab ){
99281 assert( !(opflags & OPFLAG_ISUPDATE)((void) (0))
99282 || HasRowid(pTab)==0((void) (0))
99283 || (aMem[pOp->p3].flags & MEM_Int)((void) (0))
99284 )((void) (0));
99285 sqlite3VdbePreUpdateHook(p, pC,
99286 (opflags & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_DELETE9,
99287 zDb, pTab, pC->movetoTarget,
99288 pOp->p3, -1
99289 );
99290 }
99291 if( opflags & OPFLAG_ISNOOP0x40 ) break;
99292#endif
99293
99294 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
99295 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 )((void) (0));
99296 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION )((void) (0));
99297 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE )((void) (0));
99298
99299#ifdef SQLITE_DEBUG
99300 if( p->pFrame==0 ){
99301 if( pC->isEphemeral==0
99302 && (pOp->p5 & OPFLAG_AUXDELETE0x04)==0
99303 && (pC->wrFlag & OPFLAG_FORDELETE0x08)==0
99304 ){
99305 nExtraDelete++;
99306 }
99307 if( pOp->p2 & OPFLAG_NCHANGE0x01 ){
99308 nExtraDelete--;
99309 }
99310 }
99311#endif
99312
99313 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
99314 pC->cacheStatus = CACHE_STALE0;
99315 colCacheCtr++;
99316 pC->seekResult = 0;
99317 if( rc ) goto abort_due_to_error;
99318
99319 /* Invoke the update-hook if required. */
99320 if( opflags & OPFLAG_NCHANGE0x01 ){
99321 p->nChange++;
99322 if( db->xUpdateCallback && ALWAYS(pTab!=0)(pTab!=0) && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
99323 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE9, zDb, pTab->zName,
99324 pC->movetoTarget);
99325 assert( pC->iDb>=0 )((void) (0));
99326 }
99327 }
99328
99329 break;
99330}
99331/* Opcode: ResetCount * * * * *
99332**
99333** The value of the change counter is copied to the database handle
99334** change counter (returned by subsequent calls to sqlite3_changes()).
99335** Then the VMs internal change counter resets to 0.
99336** This is used by trigger programs.
99337*/
99338case OP_ResetCount131: {
99339 sqlite3VdbeSetChanges(db, p->nChange);
99340 p->nChange = 0;
99341 break;
99342}
99343
99344/* Opcode: SorterCompare P1 P2 P3 P4
99345** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
99346**
99347** P1 is a sorter cursor. This instruction compares a prefix of the
99348** record blob in register P3 against a prefix of the entry that
99349** the sorter cursor currently points to. Only the first P4 fields
99350** of r[P3] and the sorter record are compared.
99351**
99352** If either P3 or the sorter contains a NULL in one of their significant
99353** fields (not counting the P4 fields at the end which are ignored) then
99354** the comparison is assumed to be equal.
99355**
99356** Fall through to next instruction if the two records compare equal to
99357** each other. Jump to P2 if they are different.
99358*/
99359case OP_SorterCompare132: {
99360 VdbeCursor *pC;
99361 int res;
99362 int nKeyCol;
99363
99364 pC = p->apCsr[pOp->p1];
99365 assert( isSorter(pC) )((void) (0));
99366 assert( pOp->p4type==P4_INT32 )((void) (0));
99367 pIn3 = &aMem[pOp->p3];
99368 nKeyCol = pOp->p4.i;
99369 res = 0;
99370 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
99371 VdbeBranchTaken(res!=0,2);
99372 if( rc ) goto abort_due_to_error;
99373 if( res ) goto jump_to_p2;
99374 break;
99375};
99376
99377/* Opcode: SorterData P1 P2 P3 * *
99378** Synopsis: r[P2]=data
99379**
99380** Write into register P2 the current sorter data for sorter cursor P1.
99381** Then clear the column header cache on cursor P3.
99382**
99383** This opcode is normally used to move a record out of the sorter and into
99384** a register that is the source for a pseudo-table cursor created using
99385** OpenPseudo. That pseudo-table cursor is the one that is identified by
99386** parameter P3. Clearing the P3 column cache as part of this opcode saves
99387** us from having to issue a separate NullRow instruction to clear that cache.
99388*/
99389case OP_SorterData133: { /* ncycle */
99390 VdbeCursor *pC;
99391
99392 pOut = &aMem[pOp->p2];
99393 pC = p->apCsr[pOp->p1];
99394 assert( isSorter(pC) )((void) (0));
99395 rc = sqlite3VdbeSorterRowkey(pC, pOut);
99396 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) )((void) (0));
99397 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99398 if( rc ) goto abort_due_to_error;
99399 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE0;
99400 break;
99401}
99402
99403/* Opcode: RowData P1 P2 P3 * *
99404** Synopsis: r[P2]=data
99405**
99406** Write into register P2 the complete row content for the row at
99407** which cursor P1 is currently pointing.
99408** There is no interpretation of the data.
99409** It is just copied onto the P2 register exactly as
99410** it is found in the database file.
99411**
99412** If cursor P1 is an index, then the content is the key of the row.
99413** If cursor P2 is a table, then the content extracted is the data.
99414**
99415** If the P1 cursor must be pointing to a valid row (not a NULL row)
99416** of a real table, not a pseudo-table.
99417**
99418** If P3!=0 then this opcode is allowed to make an ephemeral pointer
99419** into the database page. That means that the content of the output
99420** register will be invalidated as soon as the cursor moves - including
99421** moves caused by other cursors that "save" the current cursors
99422** position in order that they can write to the same table. If P3==0
99423** then a copy of the data is made into memory. P3!=0 is faster, but
99424** P3==0 is safer.
99425**
99426** If P3!=0 then the content of the P2 register is unsuitable for use
99427** in OP_Result and any OP_Result will invalidate the P2 register content.
99428** The P2 register content is invalidated by opcodes like OP_Function or
99429** by any use of another cursor pointing to the same table.
99430*/
99431case OP_RowData134: {
99432 VdbeCursor *pC;
99433 BtCursor *pCrsr;
99434 u32 n;
99435
99436 pOut = out2Prerelease(p, pOp);
99437
99438 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99439 pC = p->apCsr[pOp->p1];
99440 assert( pC!=0 )((void) (0));
99441 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99442 assert( isSorter(pC)==0 )((void) (0));
99443 assert( pC->nullRow==0 )((void) (0));
99444 assert( pC->uc.pCursor!=0 )((void) (0));
99445 pCrsr = pC->uc.pCursor;
99446
99447 /* The OP_RowData opcodes always follow OP_NotExists or
99448 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
99449 ** that might invalidate the cursor.
99450 ** If this where not the case, on of the following assert()s
99451 ** would fail. Should this ever change (because of changes in the code
99452 ** generator) then the fix would be to insert a call to
99453 ** sqlite3VdbeCursorMoveto().
99454 */
99455 assert( pC->deferredMoveto==0 )((void) (0));
99456 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
99457
99458 n = sqlite3BtreePayloadSize(pCrsr);
99459 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH0] ){
99460 goto too_big;
99461 }
99462 testcase( n==0 );
99463 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
99464 if( rc ) goto abort_due_to_error;
99465 if( !pOp->p3 ) Deephemeralize(pOut)if( ((pOut)->flags&0x4000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
99466 UPDATE_MAX_BLOBSIZE(pOut);
99467 REGISTER_TRACE(pOp->p2, pOut);
99468 break;
99469}
99470
99471/* Opcode: Rowid P1 P2 * * *
99472** Synopsis: r[P2]=PX rowid of P1
99473**
99474** Store in register P2 an integer which is the key of the table entry that
99475** P1 is currently point to.
99476**
99477** P1 can be either an ordinary table or a virtual table. There used to
99478** be a separate OP_VRowid opcode for use with virtual tables, but this
99479** one opcode now works for both table types.
99480*/
99481case OP_Rowid135: { /* out2, ncycle */
99482 VdbeCursor *pC;
99483 i64 v;
99484 sqlite3_vtab *pVtab;
99485 const sqlite3_module *pModule;
99486
99487 pOut = out2Prerelease(p, pOp);
99488 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99489 pC = p->apCsr[pOp->p1];
99490 assert( pC!=0 )((void) (0));
99491 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
99492 if( pC->nullRow ){
99493 pOut->flags = MEM_Null0x0001;
99494 break;
99495 }else if( pC->deferredMoveto ){
99496 v = pC->movetoTarget;
99497#ifndef SQLITE_OMIT_VIRTUALTABLE
99498 }else if( pC->eCurType==CURTYPE_VTAB2 ){
99499 assert( pC->uc.pVCur!=0 )((void) (0));
99500 pVtab = pC->uc.pVCur->pVtab;
99501 pModule = pVtab->pModule;
99502 assert( pModule->xRowid )((void) (0));
99503 rc = pModule->xRowid(pC->uc.pVCur, &v);
99504 sqlite3VtabImportErrmsg(p, pVtab);
99505 if( rc ) goto abort_due_to_error;
99506#endif /* SQLITE_OMIT_VIRTUALTABLE */
99507 }else{
99508 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99509 assert( pC->uc.pCursor!=0 )((void) (0));
99510 rc = sqlite3VdbeCursorRestore(pC);
99511 if( rc ) goto abort_due_to_error;
99512 if( pC->nullRow ){
99513 pOut->flags = MEM_Null0x0001;
99514 break;
99515 }
99516 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99517 }
99518 pOut->u.i = v;
99519 break;
99520}
99521
99522/* Opcode: NullRow P1 * * * *
99523**
99524** Move the cursor P1 to a null row. Any OP_Column operations
99525** that occur while the cursor is on the null row will always
99526** write a NULL.
99527**
99528** If cursor P1 is not previously opened, open it now to a special
99529** pseudo-cursor that always returns NULL for every column.
99530*/
99531case OP_NullRow136: {
99532 VdbeCursor *pC;
99533
99534 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99535 pC = p->apCsr[pOp->p1];
99536 if( pC==0 ){
99537 /* If the cursor is not already open, create a special kind of
99538 ** pseudo-cursor that always gives null rows. */
99539 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO3);
99540 if( pC==0 ) goto no_mem;
99541 pC->seekResult = 0;
99542 pC->isTable = 1;
99543 pC->noReuse = 1;
99544 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
99545 }
99546 pC->nullRow = 1;
99547 pC->cacheStatus = CACHE_STALE0;
99548 if( pC->eCurType==CURTYPE_BTREE0 ){
99549 assert( pC->uc.pCursor!=0 )((void) (0));
99550 sqlite3BtreeClearCursor(pC->uc.pCursor);
99551 }
99552#ifdef SQLITE_DEBUG
99553 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow136;
99554#endif
99555 break;
99556}
99557
99558/* Opcode: SeekEnd P1 * * * *
99559**
99560** Position cursor P1 at the end of the btree for the purpose of
99561** appending a new entry onto the btree.
99562**
99563** It is assumed that the cursor is used only for appending and so
99564** if the cursor is valid, then the cursor must already be pointing
99565** at the end of the btree and so no changes are made to
99566** the cursor.
99567*/
99568/* Opcode: Last P1 P2 * * *
99569**
99570** The next use of the Rowid or Column or Prev instruction for P1
99571** will refer to the last entry in the database table or index.
99572** If the table or index is empty and P2>0, then jump immediately to P2.
99573** If P2 is 0 or if the table or index is not empty, fall through
99574** to the following instruction.
99575**
99576** This opcode leaves the cursor configured to move in reverse order,
99577** from the end toward the beginning. In other words, the cursor is
99578** configured to use Prev, not Next.
99579*/
99580case OP_SeekEnd137: /* ncycle */
99581case OP_Last32: { /* jump0, ncycle */
99582 VdbeCursor *pC;
99583 BtCursor *pCrsr;
99584 int res;
99585
99586 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99587 pC = p->apCsr[pOp->p1];
99588 assert( pC!=0 )((void) (0));
99589 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99590 pCrsr = pC->uc.pCursor;
99591 res = 0;
99592 assert( pCrsr!=0 )((void) (0));
99593#ifdef SQLITE_DEBUG
99594 pC->seekOp = pOp->opcode;
99595#endif
99596 if( pOp->opcode==OP_SeekEnd137 ){
99597 assert( pOp->p2==0 )((void) (0));
99598 pC->seekResult = -1;
99599 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
99600 break;
99601 }
99602 }
99603 rc = sqlite3BtreeLast(pCrsr, &res);
99604 pC->nullRow = (u8)res;
99605 pC->deferredMoveto = 0;
99606 pC->cacheStatus = CACHE_STALE0;
99607 if( rc ) goto abort_due_to_error;
99608 if( pOp->p2>0 ){
99609 VdbeBranchTaken(res!=0,2);
99610 if( res ) goto jump_to_p2;
99611 }
99612 break;
99613}
99614
99615/* Opcode: IfSizeBetween P1 P2 P3 P4 *
99616**
99617** Let N be the approximate number of rows in the table or index
99618** with cursor P1 and let X be 10*log2(N) if N is positive or -1
99619** if N is zero.
99620**
99621** Jump to P2 if X is in between P3 and P4, inclusive.
99622*/
99623case OP_IfSizeBetween33: { /* jump */
99624 VdbeCursor *pC;
99625 BtCursor *pCrsr;
99626 int res;
99627 i64 sz;
99628
99629 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99630 assert( pOp->p4type==P4_INT32 )((void) (0));
99631 assert( pOp->p3>=-1 && pOp->p3<=640*2 )((void) (0));
99632 assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 )((void) (0));
99633 pC = p->apCsr[pOp->p1];
99634 assert( pC!=0 )((void) (0));
99635 pCrsr = pC->uc.pCursor;
99636 assert( pCrsr )((void) (0));
99637 rc = sqlite3BtreeFirst(pCrsr, &res);
99638 if( rc ) goto abort_due_to_error;
99639 if( res!=0 ){
99640 sz = -1; /* -Infinity encoding */
99641 }else{
99642 sz = sqlite3BtreeRowCountEst(pCrsr);
99643 assert( sz>0 )((void) (0));
99644 sz = sqlite3LogEst((u64)sz);
99645 }
99646 res = sz>=pOp->p3 && sz<=pOp->p4.i;
99647 VdbeBranchTaken(res!=0,2);
99648 if( res ) goto jump_to_p2;
99649 break;
99650}
99651
99652
99653/* Opcode: SorterSort P1 P2 * * *
99654**
99655** After all records have been inserted into the Sorter object
99656** identified by P1, invoke this opcode to actually do the sorting.
99657** Jump to P2 if there are no records to be sorted.
99658**
99659** This opcode is an alias for OP_Sort and OP_Rewind that is used
99660** for Sorter objects.
99661*/
99662/* Opcode: Sort P1 P2 * * *
99663**
99664** This opcode does exactly the same thing as OP_Rewind except that
99665** it increments an undocumented global variable used for testing.
99666**
99667** Sorting is accomplished by writing records into a sorting index,
99668** then rewinding that index and playing it back from beginning to
99669** end. We use the OP_Sort opcode instead of OP_Rewind to do the
99670** rewinding so that the global variable will be incremented and
99671** regression tests can determine whether or not the optimizer is
99672** correctly optimizing out sorts.
99673*/
99674case OP_SorterSort34: /* jump ncycle */
99675case OP_Sort35: { /* jump ncycle */
99676#ifdef SQLITE_TEST
99677 sqlite3_sort_count++;
99678 sqlite3_search_count--;
99679#endif
99680 p->aCounter[SQLITE_STMTSTATUS_SORT2]++;
99681 /* Fall through into OP_Rewind */
99682 /* no break */ deliberate_fall_through__attribute__((fallthrough));
99683}
99684/* Opcode: Rewind P1 P2 * * *
99685**
99686** The next use of the Rowid or Column or Next instruction for P1
99687** will refer to the first entry in the database table or index.
99688** If the table or index is empty, jump immediately to P2.
99689** If the table or index is not empty, fall through to the following
99690** instruction.
99691**
99692** If P2 is zero, that is an assertion that the P1 table is never
99693** empty and hence the jump will never be taken.
99694**
99695** This opcode leaves the cursor configured to move in forward order,
99696** from the beginning toward the end. In other words, the cursor is
99697** configured to use Next, not Prev.
99698*/
99699case OP_Rewind36: { /* jump0, ncycle */
99700 VdbeCursor *pC;
99701 BtCursor *pCrsr;
99702 int res;
99703
99704 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99705 assert( pOp->p5==0 )((void) (0));
99706 assert( pOp->p2>=0 && pOp->p2<p->nOp )((void) (0));
99707
99708 pC = p->apCsr[pOp->p1];
99709 assert( pC!=0 )((void) (0));
99710 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) )((void) (0));
99711 res = 1;
99712#ifdef SQLITE_DEBUG
99713 pC->seekOp = OP_Rewind36;
99714#endif
99715 if( isSorter(pC)((pC)->eCurType==1) ){
99716 rc = sqlite3VdbeSorterRewind(pC, &res);
99717 }else{
99718 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99719 pCrsr = pC->uc.pCursor;
99720 assert( pCrsr )((void) (0));
99721 rc = sqlite3BtreeFirst(pCrsr, &res);
99722 pC->deferredMoveto = 0;
99723 pC->cacheStatus = CACHE_STALE0;
99724 }
99725 if( rc ) goto abort_due_to_error;
99726 pC->nullRow = (u8)res;
99727 if( pOp->p2>0 ){
99728 VdbeBranchTaken(res!=0,2);
99729 if( res ) goto jump_to_p2;
99730 }
99731 break;
99732}
99733
99734/* Opcode: Next P1 P2 P3 * P5
99735**
99736** Advance cursor P1 so that it points to the next key/data pair in its
99737** table or index. If there are no more key/value pairs then fall through
99738** to the following instruction. But if the cursor advance was successful,
99739** jump immediately to P2.
99740**
99741** The Next opcode is only valid following an SeekGT, SeekGE, or
99742** OP_Rewind opcode used to position the cursor. Next is not allowed
99743** to follow SeekLT, SeekLE, or OP_Last.
99744**
99745** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
99746** been opened prior to this opcode or the program will segfault.
99747**
99748** The P3 value is a hint to the btree implementation. If P3==1, that
99749** means P1 is an SQL index and that this instruction could have been
99750** omitted if that index had been unique. P3 is usually 0. P3 is
99751** always either 0 or 1.
99752**
99753** If P5 is positive and the jump is taken, then event counter
99754** number P5-1 in the prepared statement is incremented.
99755**
99756** See also: Prev
99757*/
99758/* Opcode: Prev P1 P2 P3 * P5
99759**
99760** Back up cursor P1 so that it points to the previous key/data pair in its
99761** table or index. If there is no previous key/value pairs then fall through
99762** to the following instruction. But if the cursor backup was successful,
99763** jump immediately to P2.
99764**
99765**
99766** The Prev opcode is only valid following an SeekLT, SeekLE, or
99767** OP_Last opcode used to position the cursor. Prev is not allowed
99768** to follow SeekGT, SeekGE, or OP_Rewind.
99769**
99770** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
99771** not open then the behavior is undefined.
99772**
99773** The P3 value is a hint to the btree implementation. If P3==1, that
99774** means P1 is an SQL index and that this instruction could have been
99775** omitted if that index had been unique. P3 is usually 0. P3 is
99776** always either 0 or 1.
99777**
99778** If P5 is positive and the jump is taken, then event counter
99779** number P5-1 in the prepared statement is incremented.
99780*/
99781/* Opcode: SorterNext P1 P2 * * P5
99782**
99783** This opcode works just like OP_Next except that P1 must be a
99784** sorter object for which the OP_SorterSort opcode has been
99785** invoked. This opcode advances the cursor to the next sorted
99786** record, or jumps to P2 if there are no more sorted records.
99787*/
99788case OP_SorterNext37: { /* jump */
99789 VdbeCursor *pC;
99790
99791 pC = p->apCsr[pOp->p1];
99792 assert( isSorter(pC) )((void) (0));
99793 rc = sqlite3VdbeSorterNext(db, pC);
99794 goto next_tail;
99795
99796case OP_Prev38: /* jump, ncycle */
99797 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99798 assert( pOp->p5==0((void) (0))
99799 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP((void) (0))
99800 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX)((void) (0));
99801 pC = p->apCsr[pOp->p1];
99802 assert( pC!=0 )((void) (0));
99803 assert( pC->deferredMoveto==0 )((void) (0));
99804 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99805 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE((void) (0))
99806 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope((void) (0))
99807 || pC->seekOp==OP_NullRow)((void) (0));
99808 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
99809 goto next_tail;
99810
99811case OP_Next39: /* jump, ncycle */
99812 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99813 assert( pOp->p5==0((void) (0))
99814 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP((void) (0))
99815 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX)((void) (0));
99816 pC = p->apCsr[pOp->p1];
99817 assert( pC!=0 )((void) (0));
99818 assert( pC->deferredMoveto==0 )((void) (0));
99819 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99820 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE((void) (0))
99821 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found((void) (0))
99822 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid((void) (0))
99823 || pC->seekOp==OP_IfNoHope)((void) (0));
99824 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
99825
99826next_tail:
99827 pC->cacheStatus = CACHE_STALE0;
99828 VdbeBranchTaken(rc==SQLITE_OK,2);
99829 if( rc==SQLITE_OK0 ){
99830 pC->nullRow = 0;
99831 p->aCounter[pOp->p5]++;
99832#ifdef SQLITE_TEST
99833 sqlite3_search_count++;
99834#endif
99835 goto jump_to_p2_and_check_for_interrupt;
99836 }
99837 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
99838 rc = SQLITE_OK0;
99839 pC->nullRow = 1;
99840 goto check_for_interrupt;
99841}
99842
99843/* Opcode: IdxInsert P1 P2 P3 P4 P5
99844** Synopsis: key=r[P2]
99845**
99846** Register P2 holds an SQL index key made using the
99847** MakeRecord instructions. This opcode writes that key
99848** into the index P1. Data for the entry is nil.
99849**
99850** If P4 is not zero, then it is the number of values in the unpacked
99851** key of reg(P2). In that case, P3 is the index of the first register
99852** for the unpacked key. The availability of the unpacked key can sometimes
99853** be an optimization.
99854**
99855** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
99856** that this insert is likely to be an append.
99857**
99858** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
99859** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
99860** then the change counter is unchanged.
99861**
99862** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
99863** run faster by avoiding an unnecessary seek on cursor P1. However,
99864** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
99865** seeks on the cursor or if the most recent seek used a key equivalent
99866** to P2.
99867**
99868** This instruction only works for indices. The equivalent instruction
99869** for tables is OP_Insert.
99870*/
99871case OP_IdxInsert138: { /* in2 */
99872 VdbeCursor *pC;
99873 BtreePayload x;
99874
99875 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99876 pC = p->apCsr[pOp->p1];
99877 sqlite3VdbeIncrWriteCounter(p, pC);
99878 assert( pC!=0 )((void) (0));
99879 assert( !isSorter(pC) )((void) (0));
99880 pIn2 = &aMem[pOp->p2];
99881 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) )((void) (0));
99882 if( pOp->p5 & OPFLAG_NCHANGE0x01 ) p->nChange++;
11
Assuming the condition is false
12
Taking false branch
99883 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99884 assert( pC->isTable==0 )((void) (0));
99885 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
13
Assuming the condition is false
14
'?' condition is false
99886 if( rc
14.1
'rc' is 0
) goto abort_due_to_error;
15
Taking false branch
99887 x.nKey = pIn2->n;
99888 x.pKey = pIn2->z;
99889 x.aMem = aMem + pOp->p3;
99890 x.nMem = (u16)pOp->p4.i;
99891 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
18
Calling 'sqlite3BtreeInsert'
99892 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02|OPFLAG_PREFORMAT0x80)),
99893 ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0)
16
Assuming the condition is false
17
'?' condition is false
99894 );
99895 assert( pC->deferredMoveto==0 )((void) (0));
99896 pC->cacheStatus = CACHE_STALE0;
99897 if( rc) goto abort_due_to_error;
99898 break;
99899}
99900
99901/* Opcode: SorterInsert P1 P2 * * *
99902** Synopsis: key=r[P2]
99903**
99904** Register P2 holds an SQL index key made using the
99905** MakeRecord instructions. This opcode writes that key
99906** into the sorter P1. Data for the entry is nil.
99907*/
99908case OP_SorterInsert139: { /* in2 */
99909 VdbeCursor *pC;
99910
99911 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99912 pC = p->apCsr[pOp->p1];
99913 sqlite3VdbeIncrWriteCounter(p, pC);
99914 assert( pC!=0 )((void) (0));
99915 assert( isSorter(pC) )((void) (0));
99916 pIn2 = &aMem[pOp->p2];
99917 assert( pIn2->flags & MEM_Blob )((void) (0));
99918 assert( pC->isTable==0 )((void) (0));
99919 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x0400)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
99920 if( rc ) goto abort_due_to_error;
99921 rc = sqlite3VdbeSorterWrite(pC, pIn2);
99922 if( rc) goto abort_due_to_error;
99923 break;
99924}
99925
99926/* Opcode: IdxDelete P1 P2 P3 * P5
99927** Synopsis: key=r[P2@P3]
99928**
99929** The content of P3 registers starting at register P2 form
99930** an unpacked index key. This opcode removes that entry from the
99931** index opened by cursor P1.
99932**
99933** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
99934** if no matching index entry is found. This happens when running
99935** an UPDATE or DELETE statement and the index entry to be updated
99936** or deleted is not found. For some uses of IdxDelete
99937** (example: the EXCEPT operator) it does not matter that no matching
99938** entry is found. For those cases, P5 is zero. Also, do not raise
99939** this (self-correcting and non-critical) error if in writable_schema mode.
99940*/
99941case OP_IdxDelete140: {
99942 VdbeCursor *pC;
99943 BtCursor *pCrsr;
99944 int res;
99945 UnpackedRecord r;
99946
99947 assert( pOp->p3>0 )((void) (0));
99948 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
99949 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
99950 pC = p->apCsr[pOp->p1];
99951 assert( pC!=0 )((void) (0));
99952 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
99953 sqlite3VdbeIncrWriteCounter(p, pC);
99954 pCrsr = pC->uc.pCursor;
99955 assert( pCrsr!=0 )((void) (0));
99956 r.pKeyInfo = pC->pKeyInfo;
99957 r.nField = (u16)pOp->p3;
99958 r.default_rc = 0;
99959 r.aMem = &aMem[pOp->p2];
99960 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
99961 if( rc ) goto abort_due_to_error;
99962 if( res==0 ){
99963 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE0x04);
99964 if( rc ) goto abort_due_to_error;
99965 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
99966 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX(11 | (3<<8)), __LINE__99966, "index corruption");
99967 goto abort_due_to_error;
99968 }
99969 assert( pC->deferredMoveto==0 )((void) (0));
99970 pC->cacheStatus = CACHE_STALE0;
99971 pC->seekResult = 0;
99972 break;
99973}
99974
99975/* Opcode: DeferredSeek P1 * P3 P4 *
99976** Synopsis: Move P3 to P1.rowid if needed
99977**
99978** P1 is an open index cursor and P3 is a cursor on the corresponding
99979** table. This opcode does a deferred seek of the P3 table cursor
99980** to the row that corresponds to the current row of P1.
99981**
99982** This is a deferred seek. Nothing actually happens until
99983** the cursor is used to read a record. That way, if no reads
99984** occur, no unnecessary I/O happens.
99985**
99986** P4 may be an array of integers (type P4_INTARRAY) containing
99987** one entry for each column in the P3 table. If array entry a(i)
99988** is non-zero, then reading column a(i)-1 from cursor P3 is
99989** equivalent to performing the deferred seek and then reading column i
99990** from P1. This information is stored in P3 and used to redirect
99991** reads against P3 over to P1, thus possibly avoiding the need to
99992** seek and read cursor P3.
99993*/
99994/* Opcode: IdxRowid P1 P2 * * *
99995** Synopsis: r[P2]=rowid
99996**
99997** Write into register P2 an integer which is the last entry in the record at
99998** the end of the index key pointed to by cursor P1. This integer should be
99999** the rowid of the table entry to which this index entry points.
100000**
100001** See also: Rowid, MakeRecord.
100002*/
100003case OP_DeferredSeek141: /* ncycle */
100004case OP_IdxRowid142: { /* out2, ncycle */
100005 VdbeCursor *pC; /* The P1 index cursor */
100006 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
100007 i64 rowid; /* Rowid that P1 current points to */
100008
100009 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100010 pC = p->apCsr[pOp->p1];
100011 assert( pC!=0 )((void) (0));
100012 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) )((void) (0));
100013 assert( pC->uc.pCursor!=0 )((void) (0));
100014 assert( pC->isTable==0 || IsNullCursor(pC) )((void) (0));
100015 assert( pC->deferredMoveto==0 )((void) (0));
100016 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid )((void) (0));
100017
100018 /* The IdxRowid and Seek opcodes are combined because of the commonality
100019 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
100020 rc = sqlite3VdbeCursorRestore(pC);
100021
100022 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
100023 ** since it was last positioned and an error (e.g. OOM or an IO error)
100024 ** occurs while trying to reposition it. */
100025 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
100026
100027 if( !pC->nullRow ){
100028 rowid = 0; /* Not needed. Only used to silence a warning. */
100029 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
100030 if( rc!=SQLITE_OK0 ){
100031 goto abort_due_to_error;
100032 }
100033 if( pOp->opcode==OP_DeferredSeek141 ){
100034 assert( pOp->p3>=0 && pOp->p3<p->nCursor )((void) (0));
100035 pTabCur = p->apCsr[pOp->p3];
100036 assert( pTabCur!=0 )((void) (0));
100037 assert( pTabCur->eCurType==CURTYPE_BTREE )((void) (0));
100038 assert( pTabCur->uc.pCursor!=0 )((void) (0));
100039 assert( pTabCur->isTable )((void) (0));
100040 pTabCur->nullRow = 0;
100041 pTabCur->movetoTarget = rowid;
100042 pTabCur->deferredMoveto = 1;
100043 pTabCur->cacheStatus = CACHE_STALE0;
100044 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 )((void) (0));
100045 assert( !pTabCur->isEphemeral )((void) (0));
100046 pTabCur->ub.aAltMap = pOp->p4.ai;
100047 assert( !pC->isEphemeral )((void) (0));
100048 pTabCur->pAltCursor = pC;
100049 }else{
100050 pOut = out2Prerelease(p, pOp);
100051 pOut->u.i = rowid;
100052 }
100053 }else{
100054 assert( pOp->opcode==OP_IdxRowid )((void) (0));
100055 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
100056 }
100057 break;
100058}
100059
100060/* Opcode: FinishSeek P1 * * * *
100061**
100062** If cursor P1 was previously moved via OP_DeferredSeek, complete that
100063** seek operation now, without further delay. If the cursor seek has
100064** already occurred, this instruction is a no-op.
100065*/
100066case OP_FinishSeek143: { /* ncycle */
100067 VdbeCursor *pC; /* The P1 index cursor */
100068
100069 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100070 pC = p->apCsr[pOp->p1];
100071 if( pC->deferredMoveto ){
100072 rc = sqlite3VdbeFinishMoveto(pC);
100073 if( rc ) goto abort_due_to_error;
100074 }
100075 break;
100076}
100077
100078/* Opcode: IdxGE P1 P2 P3 P4 *
100079** Synopsis: key=r[P3@P4]
100080**
100081** The P4 register values beginning with P3 form an unpacked index
100082** key that omits the PRIMARY KEY. Compare this key value against the index
100083** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
100084** fields at the end.
100085**
100086** If the P1 index entry is greater than or equal to the key value
100087** then jump to P2. Otherwise fall through to the next instruction.
100088*/
100089/* Opcode: IdxGT P1 P2 P3 P4 *
100090** Synopsis: key=r[P3@P4]
100091**
100092** The P4 register values beginning with P3 form an unpacked index
100093** key that omits the PRIMARY KEY. Compare this key value against the index
100094** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
100095** fields at the end.
100096**
100097** If the P1 index entry is greater than the key value
100098** then jump to P2. Otherwise fall through to the next instruction.
100099*/
100100/* Opcode: IdxLT P1 P2 P3 P4 *
100101** Synopsis: key=r[P3@P4]
100102**
100103** The P4 register values beginning with P3 form an unpacked index
100104** key that omits the PRIMARY KEY or ROWID. Compare this key value against
100105** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
100106** ROWID on the P1 index.
100107**
100108** If the P1 index entry is less than the key value then jump to P2.
100109** Otherwise fall through to the next instruction.
100110*/
100111/* Opcode: IdxLE P1 P2 P3 P4 *
100112** Synopsis: key=r[P3@P4]
100113**
100114** The P4 register values beginning with P3 form an unpacked index
100115** key that omits the PRIMARY KEY or ROWID. Compare this key value against
100116** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
100117** ROWID on the P1 index.
100118**
100119** If the P1 index entry is less than or equal to the key value then jump
100120** to P2. Otherwise fall through to the next instruction.
100121*/
100122case OP_IdxLE40: /* jump, ncycle */
100123case OP_IdxGT41: /* jump, ncycle */
100124case OP_IdxLT42: /* jump, ncycle */
100125case OP_IdxGE45: { /* jump, ncycle */
100126 VdbeCursor *pC;
100127 int res;
100128 UnpackedRecord r;
100129
100130 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100131 pC = p->apCsr[pOp->p1];
100132 assert( pC!=0 )((void) (0));
100133 assert( pC->isOrdered )((void) (0));
100134 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100135 assert( pC->uc.pCursor!=0)((void) (0));
100136 assert( pC->deferredMoveto==0 )((void) (0));
100137 assert( pOp->p4type==P4_INT32 )((void) (0));
100138 r.pKeyInfo = pC->pKeyInfo;
100139 r.nField = (u16)pOp->p4.i;
100140 if( pOp->opcode<OP_IdxLT42 ){
100141 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT )((void) (0));
100142 r.default_rc = -1;
100143 }else{
100144 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT )((void) (0));
100145 r.default_rc = 0;
100146 }
100147 r.aMem = &aMem[pOp->p3];
100148#ifdef SQLITE_DEBUG
100149 {
100150 int i;
100151 for(i=0; i<r.nField; i++){
100152 assert( memIsValid(&r.aMem[i]) )((void) (0));
100153 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
100154 }
100155 }
100156#endif
100157
100158 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
100159 {
100160 i64 nCellKey = 0;
100161 BtCursor *pCur;
100162 Mem m;
100163
100164 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100165 pCur = pC->uc.pCursor;
100166 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
100167 nCellKey = sqlite3BtreePayloadSize(pCur);
100168 /* nCellKey will always be between 0 and 0xffffffff because of the way
100169 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
100170 if( nCellKey<=0 || nCellKey>0x7fffffff ){
100171 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(100171);
100172 goto abort_due_to_error;
100173 }
100174 sqlite3VdbeMemInit(&m, db, 0);
100175 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
100176 if( rc ) goto abort_due_to_error;
100177 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
100178 sqlite3VdbeMemReleaseMalloc(&m);
100179 }
100180 /* End of inlined sqlite3VdbeIdxKeyCompare() */
100181
100182 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) )((void) (0));
100183 if( (pOp->opcode&1)==(OP_IdxLT42&1) ){
100184 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT )((void) (0));
100185 res = -res;
100186 }else{
100187 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT )((void) (0));
100188 res++;
100189 }
100190 VdbeBranchTaken(res>0,2);
100191 assert( rc==SQLITE_OK )((void) (0));
100192 if( res>0 ) goto jump_to_p2;
100193 break;
100194}
100195
100196/* Opcode: Destroy P1 P2 P3 * *
100197**
100198** Delete an entire database table or index whose root page in the database
100199** file is given by P1.
100200**
100201** The table being destroyed is in the main database file if P3==0. If
100202** P3==1 then the table to be destroyed is in the auxiliary database file
100203** that is used to store tables create using CREATE TEMPORARY TABLE.
100204**
100205** If AUTOVACUUM is enabled then it is possible that another root page
100206** might be moved into the newly deleted root page in order to keep all
100207** root pages contiguous at the beginning of the database. The former
100208** value of the root page that moved - its value before the move occurred -
100209** is stored in register P2. If no page movement was required (because the
100210** table being dropped was already the last one in the database) then a
100211** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
100212** is stored in register P2.
100213**
100214** This opcode throws an error if there are any active reader VMs when
100215** it is invoked. This is done to avoid the difficulty associated with
100216** updating existing cursors when a root page is moved in an AUTOVACUUM
100217** database. This error is thrown even if the database is not an AUTOVACUUM
100218** db in order to avoid introducing an incompatibility between autovacuum
100219** and non-autovacuum modes.
100220**
100221** See also: Clear
100222*/
100223case OP_Destroy144: { /* out2 */
100224 int iMoved;
100225 int iDb;
100226
100227 sqlite3VdbeIncrWriteCounter(p, 0);
100228 assert( p->readOnly==0 )((void) (0));
100229 assert( pOp->p1>1 )((void) (0));
100230 pOut = out2Prerelease(p, pOp);
100231 pOut->flags = MEM_Null0x0001;
100232 if( db->nVdbeRead > db->nVDestroy+1 ){
100233 rc = SQLITE_LOCKED6;
100234 p->errorAction = OE_Abort2;
100235 goto abort_due_to_error;
100236 }else{
100237 iDb = pOp->p3;
100238 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
100239 iMoved = 0; /* Not needed. Only to silence a warning. */
100240 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
100241 pOut->flags = MEM_Int0x0004;
100242 pOut->u.i = iMoved;
100243 if( rc ) goto abort_due_to_error;
100244#ifndef SQLITE_OMIT_AUTOVACUUM
100245 if( iMoved!=0 ){
100246 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
100247 /* All OP_Destroy operations occur on the same btree */
100248 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 )((void) (0));
100249 resetSchemaOnFault = iDb+1;
100250 }
100251#endif
100252 }
100253 break;
100254}
100255
100256/* Opcode: Clear P1 P2 P3
100257**
100258** Delete all contents of the database table or index whose root page
100259** in the database file is given by P1. But, unlike Destroy, do not
100260** remove the table or index from the database file.
100261**
100262** The table being cleared is in the main database file if P2==0. If
100263** P2==1 then the table to be cleared is in the auxiliary database file
100264** that is used to store tables create using CREATE TEMPORARY TABLE.
100265**
100266** If the P3 value is non-zero, then the row change count is incremented
100267** by the number of rows in the table being cleared. If P3 is greater
100268** than zero, then the value stored in register P3 is also incremented
100269** by the number of rows in the table being cleared.
100270**
100271** See also: Destroy
100272*/
100273case OP_Clear145: {
100274 i64 nChange;
100275
100276 sqlite3VdbeIncrWriteCounter(p, 0);
100277 nChange = 0;
100278 assert( p->readOnly==0 )((void) (0));
100279 assert( DbMaskTest(p->btreeMask, pOp->p2) )((void) (0));
100280 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
100281 if( pOp->p3 ){
100282 p->nChange += nChange;
100283 if( pOp->p3>0 ){
100284 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
100285 memAboutToChange(p, &aMem[pOp->p3]);
100286 aMem[pOp->p3].u.i += nChange;
100287 }
100288 }
100289 if( rc ) goto abort_due_to_error;
100290 break;
100291}
100292
100293/* Opcode: ResetSorter P1 * * * *
100294**
100295** Delete all contents from the ephemeral table or sorter
100296** that is open on cursor P1.
100297**
100298** This opcode only works for cursors used for sorting and
100299** opened with OP_OpenEphemeral or OP_SorterOpen.
100300*/
100301case OP_ResetSorter146: {
100302 VdbeCursor *pC;
100303
100304 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
100305 pC = p->apCsr[pOp->p1];
100306 assert( pC!=0 )((void) (0));
100307 if( isSorter(pC)((pC)->eCurType==1) ){
100308 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
100309 }else{
100310 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
100311 assert( pC->isEphemeral )((void) (0));
100312 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
100313 if( rc ) goto abort_due_to_error;
100314 }
100315 break;
100316}
100317
100318/* Opcode: CreateBtree P1 P2 P3 * *
100319** Synopsis: r[P2]=root iDb=P1 flags=P3
100320**
100321** Allocate a new b-tree in the main database file if P1==0 or in the
100322** TEMP database file if P1==1 or in an attached database if
100323** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
100324** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
100325** The root page number of the new b-tree is stored in register P2.
100326*/
100327case OP_CreateBtree147: { /* out2 */
100328 Pgno pgno;
100329 Db *pDb;
100330
100331 sqlite3VdbeIncrWriteCounter(p, 0);
100332 pOut = out2Prerelease(p, pOp);
100333 pgno = 0;
100334 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY )((void) (0));
100335 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
100336 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
100337 assert( p->readOnly==0 )((void) (0));
100338 pDb = &db->aDb[pOp->p1];
100339 assert( pDb->pBt!=0 )((void) (0));
100340 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
100341 if( rc ) goto abort_due_to_error;
100342 pOut->u.i = pgno;
100343 break;
100344}
100345
100346/* Opcode: SqlExec P1 P2 * P4 *
100347**
100348** Run the SQL statement or statements specified in the P4 string.
100349**
100350** The P1 parameter is a bitmask of options:
100351**
100352** 0x0001 Disable Auth and Trace callbacks while the statements
100353** in P4 are running.
100354**
100355** 0x0002 Set db->nAnalysisLimit to P2 while the statements in
100356** P4 are running.
100357**
100358*/
100359case OP_SqlExec148: {
100360 char *zErr;
100361#ifndef SQLITE_OMIT_AUTHORIZATION
100362 sqlite3_xauth xAuth;
100363#endif
100364 u8 mTrace;
100365 int savedAnalysisLimit;
100366
100367 sqlite3VdbeIncrWriteCounter(p, 0);
100368 db->nSqlExec++;
100369 zErr = 0;
100370#ifndef SQLITE_OMIT_AUTHORIZATION
100371 xAuth = db->xAuth;
100372#endif
100373 mTrace = db->mTrace;
100374 savedAnalysisLimit = db->nAnalysisLimit;
100375 if( pOp->p1 & 0x0001 ){
100376#ifndef SQLITE_OMIT_AUTHORIZATION
100377 db->xAuth = 0;
100378#endif
100379 db->mTrace = 0;
100380 }
100381 if( pOp->p1 & 0x0002 ){
100382 db->nAnalysisLimit = pOp->p2;
100383 }
100384 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
100385 db->nSqlExec--;
100386#ifndef SQLITE_OMIT_AUTHORIZATION
100387 db->xAuth = xAuth;
100388#endif
100389 db->mTrace = mTrace;
100390 db->nAnalysisLimit = savedAnalysisLimit;
100391 if( zErr || rc ){
100392 sqlite3VdbeError(p, "%s", zErr);
100393 sqlite3_free(zErr);
100394 if( rc==SQLITE_NOMEM7 ) goto no_mem;
100395 goto abort_due_to_error;
100396 }
100397 break;
100398}
100399
100400/* Opcode: ParseSchema P1 * * P4 *
100401**
100402** Read and parse all entries from the schema table of database P1
100403** that match the WHERE clause P4. If P4 is a NULL pointer, then the
100404** entire schema for P1 is reparsed.
100405**
100406** This opcode invokes the parser to create a new virtual machine,
100407** then runs the new virtual machine. It is thus a re-entrant opcode.
100408*/
100409case OP_ParseSchema149: {
100410 int iDb;
100411 const char *zSchema;
100412 char *zSql;
100413 InitData initData;
100414
100415 /* Any prepared statement that invokes this opcode will hold mutexes
100416 ** on every btree. This is a prerequisite for invoking
100417 ** sqlite3InitCallback().
100418 */
100419#ifdef SQLITE_DEBUG
100420 for(iDb=0; iDb<db->nDb; iDb++){
100421 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
100422 }
100423#endif
100424
100425 iDb = pOp->p1;
100426 assert( iDb>=0 && iDb<db->nDb )((void) (0));
100427 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)((void) (0))
100428 || db->mallocFailed((void) (0))
100429 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) )((void) (0));
100430
100431#ifndef SQLITE_OMIT_ALTERTABLE
100432 if( pOp->p4.z==0 ){
100433 sqlite3SchemaClear(db->aDb[iDb].pSchema);
100434 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
100435 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
100436 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
100437 p->expired = 0;
100438 }else
100439#endif
100440 {
100441 zSchema = LEGACY_SCHEMA_TABLE"sqlite_master";
100442 initData.db = db;
100443 initData.iDb = iDb;
100444 initData.pzErrMsg = &p->zErrMsg;
100445 initData.mInitFlags = 0;
100446 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
100447 zSql = sqlite3MPrintf(db,
100448 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
100449 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
100450 if( zSql==0 ){
100451 rc = SQLITE_NOMEM_BKPT7;
100452 }else{
100453 assert( db->init.busy==0 )((void) (0));
100454 db->init.busy = 1;
100455 initData.rc = SQLITE_OK0;
100456 initData.nInitRow = 0;
100457 assert( !db->mallocFailed )((void) (0));
100458 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
100459 if( rc==SQLITE_OK0 ) rc = initData.rc;
100460 if( rc==SQLITE_OK0 && initData.nInitRow==0 ){
100461 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
100462 ** at least one SQL statement. Any less than that indicates that
100463 ** the sqlite_schema table is corrupt. */
100464 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(100464);
100465 }
100466 sqlite3DbFreeNN(db, zSql);
100467 db->init.busy = 0;
100468 }
100469 }
100470 if( rc ){
100471 sqlite3ResetAllSchemasOfConnection(db);
100472 if( rc==SQLITE_NOMEM7 ){
100473 goto no_mem;
100474 }
100475 goto abort_due_to_error;
100476 }
100477 break;
100478}
100479
100480#if !defined(SQLITE_OMIT_ANALYZE)
100481/* Opcode: LoadAnalysis P1 * * * *
100482**
100483** Read the sqlite_stat1 table for database P1 and load the content
100484** of that table into the internal index hash table. This will cause
100485** the analysis to be used when preparing all subsequent queries.
100486*/
100487case OP_LoadAnalysis150: {
100488 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
100489 rc = sqlite3AnalysisLoad(db, pOp->p1);
100490 if( rc ) goto abort_due_to_error;
100491 break;
100492}
100493#endif /* !defined(SQLITE_OMIT_ANALYZE) */
100494
100495/* Opcode: DropTable P1 * * P4 *
100496**
100497** Remove the internal (in-memory) data structures that describe
100498** the table named P4 in database P1. This is called after a table
100499** is dropped from disk (using the Destroy opcode) in order to keep
100500** the internal representation of the
100501** schema consistent with what is on disk.
100502*/
100503case OP_DropTable151: {
100504 sqlite3VdbeIncrWriteCounter(p, 0);
100505 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
100506 break;
100507}
100508
100509/* Opcode: DropIndex P1 * * P4 *
100510**
100511** Remove the internal (in-memory) data structures that describe
100512** the index named P4 in database P1. This is called after an index
100513** is dropped from disk (using the Destroy opcode)
100514** in order to keep the internal representation of the
100515** schema consistent with what is on disk.
100516*/
100517case OP_DropIndex152: {
100518 sqlite3VdbeIncrWriteCounter(p, 0);
100519 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
100520 break;
100521}
100522
100523/* Opcode: DropTrigger P1 * * P4 *
100524**
100525** Remove the internal (in-memory) data structures that describe
100526** the trigger named P4 in database P1. This is called after a trigger
100527** is dropped from disk (using the Destroy opcode) in order to keep
100528** the internal representation of the
100529** schema consistent with what is on disk.
100530*/
100531case OP_DropTrigger153: {
100532 sqlite3VdbeIncrWriteCounter(p, 0);
100533 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
100534 break;
100535}
100536
100537
100538#ifndef SQLITE_OMIT_INTEGRITY_CHECK
100539/* Opcode: IntegrityCk P1 P2 P3 P4 P5
100540**
100541** Do an analysis of the currently open database. Store in
100542** register (P1+1) the text of an error message describing any problems.
100543** If no problems are found, store a NULL in register (P1+1).
100544**
100545** The register (P1) contains one less than the maximum number of allowed
100546** errors. At most reg(P1) errors will be reported.
100547** In other words, the analysis stops as soon as reg(P1) errors are
100548** seen. Reg(P1) is updated with the number of errors remaining.
100549**
100550** The root page numbers of all tables in the database are integers
100551** stored in P4_INTARRAY argument.
100552**
100553** If P5 is not zero, the check is done on the auxiliary database
100554** file, not the main database file.
100555**
100556** This opcode is used to implement the integrity_check pragma.
100557*/
100558case OP_IntegrityCk155: {
100559 int nRoot; /* Number of tables to check. (Number of root pages.) */
100560 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
100561 int nErr; /* Number of errors reported */
100562 char *z; /* Text of the error report */
100563 Mem *pnErr; /* Register keeping track of errors remaining */
100564
100565 assert( p->bIsReader )((void) (0));
100566 assert( pOp->p4type==P4_INTARRAY )((void) (0));
100567 nRoot = pOp->p2;
100568 aRoot = pOp->p4.ai;
100569 assert( nRoot>0 )((void) (0));
100570 assert( aRoot!=0 )((void) (0));
100571 assert( aRoot[0]==(Pgno)nRoot )((void) (0));
100572 assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) )((void) (0));
100573 pnErr = &aMem[pOp->p1];
100574 assert( (pnErr->flags & MEM_Int)!=0 )((void) (0));
100575 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
100576 pIn1 = &aMem[pOp->p1+1];
100577 assert( pOp->p5<db->nDb )((void) (0));
100578 assert( DbMaskTest(p->btreeMask, pOp->p5) )((void) (0));
100579 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1],
100580 &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z);
100581 sqlite3VdbeMemSetNull(pIn1);
100582 if( nErr==0 ){
100583 assert( z==0 )((void) (0));
100584 }else if( rc ){
100585 sqlite3_free(z);
100586 goto abort_due_to_error;
100587 }else{
100588 pnErr->u.i -= nErr-1;
100589 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF81, sqlite3_free);
100590 }
100591 UPDATE_MAX_BLOBSIZE(pIn1);
100592 sqlite3VdbeChangeEncoding(pIn1, encoding);
100593 goto check_for_interrupt;
100594}
100595#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
100596
100597/* Opcode: RowSetAdd P1 P2 * * *
100598** Synopsis: rowset(P1)=r[P2]
100599**
100600** Insert the integer value held by register P2 into a RowSet object
100601** held in register P1.
100602**
100603** An assertion fails if P2 is not an integer.
100604*/
100605case OP_RowSetAdd156: { /* in1, in2 */
100606 pIn1 = &aMem[pOp->p1];
100607 pIn2 = &aMem[pOp->p2];
100608 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
100609 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
100610 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
100611 }
100612 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
100613 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
100614 break;
100615}
100616
100617/* Opcode: RowSetRead P1 P2 P3 * *
100618** Synopsis: r[P3]=rowset(P1)
100619**
100620** Extract the smallest value from the RowSet object in P1
100621** and put that value into register P3.
100622** Or, if RowSet object P1 is initially empty, leave P3
100623** unchanged and jump to instruction P2.
100624*/
100625case OP_RowSetRead46: { /* jump, in1, out3 */
100626 i64 val;
100627
100628 pIn1 = &aMem[pOp->p1];
100629 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
100630 if( (pIn1->flags & MEM_Blob0x0010)==0
100631 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
100632 ){
100633 /* The boolean index is empty */
100634 sqlite3VdbeMemSetNull(pIn1);
100635 VdbeBranchTaken(1,2);
100636 goto jump_to_p2_and_check_for_interrupt;
100637 }else{
100638 /* A value was pulled from the index */
100639 VdbeBranchTaken(0,2);
100640 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
100641 }
100642 goto check_for_interrupt;
100643}
100644
100645/* Opcode: RowSetTest P1 P2 P3 P4
100646** Synopsis: if r[P3] in rowset(P1) goto P2
100647**
100648** Register P3 is assumed to hold a 64-bit integer value. If register P1
100649** contains a RowSet object and that RowSet object contains
100650** the value held in P3, jump to register P2. Otherwise, insert the
100651** integer in P3 into the RowSet and continue on to the
100652** next opcode.
100653**
100654** The RowSet object is optimized for the case where sets of integers
100655** are inserted in distinct phases, which each set contains no duplicates.
100656** Each set is identified by a unique P4 value. The first set
100657** must have P4==0, the final set must have P4==-1, and for all other sets
100658** must have P4>0.
100659**
100660** This allows optimizations: (a) when P4==0 there is no need to test
100661** the RowSet object for P3, as it is guaranteed not to contain it,
100662** (b) when P4==-1 there is no need to insert the value, as it will
100663** never be tested for, and (c) when a value that is part of set X is
100664** inserted, there is no need to search to see if the same value was
100665** previously inserted as part of set X (only if it was previously
100666** inserted as part of some other set).
100667*/
100668case OP_RowSetTest47: { /* jump, in1, in3 */
100669 int iSet;
100670 int exists;
100671
100672 pIn1 = &aMem[pOp->p1];
100673 pIn3 = &aMem[pOp->p3];
100674 iSet = pOp->p4.i;
100675 assert( pIn3->flags&MEM_Int )((void) (0));
100676
100677 /* If there is anything other than a rowset object in memory cell P1,
100678 ** delete it now and initialize P1 with an empty rowset
100679 */
100680 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
100681 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
100682 }
100683 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
100684 assert( pOp->p4type==P4_INT32 )((void) (0));
100685 assert( iSet==-1 || iSet>=0 )((void) (0));
100686 if( iSet ){
100687 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
100688 VdbeBranchTaken(exists!=0,2);
100689 if( exists ) goto jump_to_p2;
100690 }
100691 if( iSet>=0 ){
100692 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
100693 }
100694 break;
100695}
100696
100697
100698#ifndef SQLITE_OMIT_TRIGGER
100699
100700/* Opcode: Program P1 P2 P3 P4 P5
100701**
100702** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
100703**
100704** P1 contains the address of the memory cell that contains the first memory
100705** cell in an array of values used as arguments to the sub-program. P2
100706** contains the address to jump to if the sub-program throws an IGNORE
100707** exception using the RAISE() function. P2 might be zero, if there is
100708** no possibility that an IGNORE exception will be raised.
100709** Register P3 contains the address
100710** of a memory cell in this (the parent) VM that is used to allocate the
100711** memory required by the sub-vdbe at runtime.
100712**
100713** P4 is a pointer to the VM containing the trigger program.
100714**
100715** If P5 is non-zero, then recursive program invocation is enabled.
100716*/
100717case OP_Program48: { /* jump0 */
100718 int nMem; /* Number of memory registers for sub-program */
100719 int nByte; /* Bytes of runtime space required for sub-program */
100720 Mem *pRt; /* Register to allocate runtime space */
100721 Mem *pMem; /* Used to iterate through memory cells */
100722 Mem *pEnd; /* Last memory cell in new array */
100723 VdbeFrame *pFrame; /* New vdbe frame to execute in */
100724 SubProgram *pProgram; /* Sub-program to execute */
100725 void *t; /* Token identifying trigger */
100726
100727 pProgram = pOp->p4.pProgram;
100728 pRt = &aMem[pOp->p3];
100729 assert( pProgram->nOp>0 )((void) (0));
100730
100731 /* If the p5 flag is clear, then recursive invocation of triggers is
100732 ** disabled for backwards compatibility (p5 is set if this sub-program
100733 ** is really a trigger, not a foreign key action, and the flag set
100734 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
100735 **
100736 ** It is recursive invocation of triggers, at the SQL level, that is
100737 ** disabled. In some cases a single trigger may generate more than one
100738 ** SubProgram (if the trigger may be executed with more than one different
100739 ** ON CONFLICT algorithm). SubProgram structures associated with a
100740 ** single trigger all have the same value for the SubProgram.token
100741 ** variable. */
100742 if( pOp->p5 ){
100743 t = pProgram->token;
100744 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
100745 if( pFrame ) break;
100746 }
100747
100748 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH10] ){
100749 rc = SQLITE_ERROR1;
100750 sqlite3VdbeError(p, "too many levels of trigger recursion");
100751 goto abort_due_to_error;
100752 }
100753
100754 /* Register pRt is used to store the memory required to save the state
100755 ** of the current program, and the memory required at runtime to execute
100756 ** the trigger program. If this trigger has been fired before, then pRt
100757 ** is already allocated. Otherwise, it must be initialized. */
100758 if( (pRt->flags&MEM_Blob0x0010)==0 ){
100759 /* SubProgram.nMem is set to the number of memory cells used by the
100760 ** program stored in SubProgram.aOp. As well as these, one memory
100761 ** cell is required for each cursor used by the program. Set local
100762 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
100763 */
100764 nMem = pProgram->nMem + pProgram->nCsr;
100765 assert( nMem>0 )((void) (0));
100766 if( pProgram->nCsr==0 ) nMem++;
100767 nByte = ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)
100768 + nMem * sizeof(Mem)
100769 + pProgram->nCsr * sizeof(VdbeCursor*)
100770 + (pProgram->nOp + 7)/8;
100771 pFrame = sqlite3DbMallocZero(db, nByte);
100772 if( !pFrame ){
100773 goto no_mem;
100774 }
100775 sqlite3VdbeMemRelease(pRt);
100776 pRt->flags = MEM_Blob0x0010|MEM_Dyn0x1000;
100777 pRt->z = (char*)pFrame;
100778 pRt->n = nByte;
100779 pRt->xDel = sqlite3VdbeFrameMemDel;
100780
100781 pFrame->v = p;
100782 pFrame->nChildMem = nMem;
100783 pFrame->nChildCsr = pProgram->nCsr;
100784 pFrame->pc = (int)(pOp - aOp);
100785 pFrame->aMem = p->aMem;
100786 pFrame->nMem = p->nMem;
100787 pFrame->apCsr = p->apCsr;
100788 pFrame->nCursor = p->nCursor;
100789 pFrame->aOp = p->aOp;
100790 pFrame->nOp = p->nOp;
100791 pFrame->token = pProgram->token;
100792#ifdef SQLITE_DEBUG
100793 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC0x879fb71e;
100794#endif
100795
100796 pEnd = &VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
[pFrame->nChildMem];
100797 for(pMem=VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
; pMem!=pEnd; pMem++){
100798 pMem->flags = MEM_Undefined0x0000;
100799 pMem->db = db;
100800 }
100801 }else{
100802 pFrame = (VdbeFrame*)pRt->z;
100803 assert( pRt->xDel==sqlite3VdbeFrameMemDel )((void) (0));
100804 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem((void) (0))
100805 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) )((void) (0));
100806 assert( pProgram->nCsr==pFrame->nChildCsr )((void) (0));
100807 assert( (int)(pOp - aOp)==pFrame->pc )((void) (0));
100808 }
100809
100810 p->nFrame++;
100811 pFrame->pParent = p->pFrame;
100812 pFrame->lastRowid = db->lastRowid;
100813 pFrame->nChange = p->nChange;
100814 pFrame->nDbChange = p->db->nChange;
100815 assert( pFrame->pAuxData==0 )((void) (0));
100816 pFrame->pAuxData = p->pAuxData;
100817 p->pAuxData = 0;
100818 p->nChange = 0;
100819 p->pFrame = pFrame;
100820 p->aMem = aMem = VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
;
100821 p->nMem = pFrame->nChildMem;
100822 p->nCursor = (u16)pFrame->nChildCsr;
100823 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
100824 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
100825 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
100826 p->aOp = aOp = pProgram->aOp;
100827 p->nOp = pProgram->nOp;
100828#ifdef SQLITE_DEBUG
100829 /* Verify that second and subsequent executions of the same trigger do not
100830 ** try to reuse register values from the first use. */
100831 {
100832 int i;
100833 for(i=0; i<p->nMem; i++){
100834 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
100835 MemSetTypeFlag(&aMem[i], MEM_Undefined)((&aMem[i])->flags = ((&aMem[i])->flags&~(0x0dbf
|0x0400))|0x0000)
; /* Fault if this reg is reused */
100836 }
100837 }
100838#endif
100839 pOp = &aOp[-1];
100840 goto check_for_interrupt;
100841}
100842
100843/* Opcode: Param P1 P2 * * *
100844**
100845** This opcode is only ever present in sub-programs called via the
100846** OP_Program instruction. Copy a value currently stored in a memory
100847** cell of the calling (parent) frame to cell P2 in the current frames
100848** address space. This is used by trigger programs to access the new.*
100849** and old.* values.
100850**
100851** The address of the cell in the parent frame is determined by adding
100852** the value of the P1 argument to the value of the P1 argument to the
100853** calling OP_Program instruction.
100854*/
100855case OP_Param157: { /* out2 */
100856 VdbeFrame *pFrame;
100857 Mem *pIn;
100858 pOut = out2Prerelease(p, pOp);
100859 pFrame = p->pFrame;
100860 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
100861 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem0x4000);
100862 break;
100863}
100864
100865#endif /* #ifndef SQLITE_OMIT_TRIGGER */
100866
100867#ifndef SQLITE_OMIT_FOREIGN_KEY
100868/* Opcode: FkCounter P1 P2 * * *
100869** Synopsis: fkctr[P1]+=P2
100870**
100871** Increment a "constraint counter" by P2 (P2 may be negative or positive).
100872** If P1 is non-zero, the database constraint counter is incremented
100873** (deferred foreign key constraints). Otherwise, if P1 is zero, the
100874** statement counter is incremented (immediate foreign key constraints).
100875*/
100876case OP_FkCounter158: {
100877 if( db->flags & SQLITE_DeferFKs0x00080000 ){
100878 db->nDeferredImmCons += pOp->p2;
100879 }else if( pOp->p1 ){
100880 db->nDeferredCons += pOp->p2;
100881 }else{
100882 p->nFkConstraint += pOp->p2;
100883 }
100884 break;
100885}
100886
100887/* Opcode: FkIfZero P1 P2 * * *
100888** Synopsis: if fkctr[P1]==0 goto P2
100889**
100890** This opcode tests if a foreign key constraint-counter is currently zero.
100891** If so, jump to instruction P2. Otherwise, fall through to the next
100892** instruction.
100893**
100894** If P1 is non-zero, then the jump is taken if the database constraint-counter
100895** is zero (the one that counts deferred constraint violations). If P1 is
100896** zero, the jump is taken if the statement constraint-counter is zero
100897** (immediate foreign key constraint violations).
100898*/
100899case OP_FkIfZero49: { /* jump */
100900 if( pOp->p1 ){
100901 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
100902 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
100903 }else{
100904 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
100905 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
100906 }
100907 break;
100908}
100909#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
100910
100911#ifndef SQLITE_OMIT_AUTOINCREMENT
100912/* Opcode: MemMax P1 P2 * * *
100913** Synopsis: r[P1]=max(r[P1],r[P2])
100914**
100915** P1 is a register in the root frame of this VM (the root frame is
100916** different from the current frame if this instruction is being executed
100917** within a sub-program). Set the value of register P1 to the maximum of
100918** its current value and the value in register P2.
100919**
100920** This instruction throws an error if the memory cell is not initially
100921** an integer.
100922*/
100923case OP_MemMax159: { /* in2 */
100924 VdbeFrame *pFrame;
100925 if( p->pFrame ){
100926 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
100927 pIn1 = &pFrame->aMem[pOp->p1];
100928 }else{
100929 pIn1 = &aMem[pOp->p1];
100930 }
100931 assert( memIsValid(pIn1) )((void) (0));
100932 sqlite3VdbeMemIntegerify(pIn1);
100933 pIn2 = &aMem[pOp->p2];
100934 sqlite3VdbeMemIntegerify(pIn2);
100935 if( pIn1->u.i<pIn2->u.i){
100936 pIn1->u.i = pIn2->u.i;
100937 }
100938 break;
100939}
100940#endif /* SQLITE_OMIT_AUTOINCREMENT */
100941
100942/* Opcode: IfPos P1 P2 P3 * *
100943** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
100944**
100945** Register P1 must contain an integer.
100946** If the value of register P1 is 1 or greater, subtract P3 from the
100947** value in P1 and jump to P2.
100948**
100949** If the initial value of register P1 is less than 1, then the
100950** value is unchanged and control passes through to the next instruction.
100951*/
100952case OP_IfPos50: { /* jump, in1 */
100953 pIn1 = &aMem[pOp->p1];
100954 assert( pIn1->flags&MEM_Int )((void) (0));
100955 VdbeBranchTaken( pIn1->u.i>0, 2);
100956 if( pIn1->u.i>0 ){
100957 pIn1->u.i -= pOp->p3;
100958 goto jump_to_p2;
100959 }
100960 break;
100961}
100962
100963/* Opcode: OffsetLimit P1 P2 P3 * *
100964** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
100965**
100966** This opcode performs a commonly used computation associated with
100967** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
100968** holds the offset counter. The opcode computes the combined value
100969** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
100970** value computed is the total number of rows that will need to be
100971** visited in order to complete the query.
100972**
100973** If r[P3] is zero or negative, that means there is no OFFSET
100974** and r[P2] is set to be the value of the LIMIT, r[P1].
100975**
100976** if r[P1] is zero or negative, that means there is no LIMIT
100977** and r[P2] is set to -1.
100978**
100979** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
100980*/
100981case OP_OffsetLimit160: { /* in1, out2, in3 */
100982 i64 x;
100983 pIn1 = &aMem[pOp->p1];
100984 pIn3 = &aMem[pOp->p3];
100985 pOut = out2Prerelease(p, pOp);
100986 assert( pIn1->flags & MEM_Int )((void) (0));
100987 assert( pIn3->flags & MEM_Int )((void) (0));
100988 x = pIn1->u.i;
100989 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
100990 /* If the LIMIT is less than or equal to zero, loop forever. This
100991 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
100992 ** also loop forever. This is undocumented. In fact, one could argue
100993 ** that the loop should terminate. But assuming 1 billion iterations
100994 ** per second (far exceeding the capabilities of any current hardware)
100995 ** it would take nearly 300 years to actually reach the limit. So
100996 ** looping forever is a reasonable approximation. */
100997 pOut->u.i = -1;
100998 }else{
100999 pOut->u.i = x;
101000 }
101001 break;
101002}
101003
101004/* Opcode: IfNotZero P1 P2 * * *
101005** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
101006**
101007** Register P1 must contain an integer. If the content of register P1 is
101008** initially greater than zero, then decrement the value in register P1.
101009** If it is non-zero (negative or positive) and then also jump to P2.
101010** If register P1 is initially zero, leave it unchanged and fall through.
101011*/
101012case OP_IfNotZero60: { /* jump, in1 */
101013 pIn1 = &aMem[pOp->p1];
101014 assert( pIn1->flags&MEM_Int )((void) (0));
101015 VdbeBranchTaken(pIn1->u.i<0, 2);
101016 if( pIn1->u.i ){
101017 if( pIn1->u.i>0 ) pIn1->u.i--;
101018 goto jump_to_p2;
101019 }
101020 break;
101021}
101022
101023/* Opcode: DecrJumpZero P1 P2 * * *
101024** Synopsis: if (--r[P1])==0 goto P2
101025**
101026** Register P1 must hold an integer. Decrement the value in P1
101027** and jump to P2 if the new value is exactly zero.
101028*/
101029case OP_DecrJumpZero61: { /* jump, in1 */
101030 pIn1 = &aMem[pOp->p1];
101031 assert( pIn1->flags&MEM_Int )((void) (0));
101032 if( pIn1->u.i>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) pIn1->u.i--;
101033 VdbeBranchTaken(pIn1->u.i==0, 2);
101034 if( pIn1->u.i==0 ) goto jump_to_p2;
101035 break;
101036}
101037
101038
101039/* Opcode: AggStep * P2 P3 P4 P5
101040** Synopsis: accum=r[P3] step(r[P2@P5])
101041**
101042** Execute the xStep function for an aggregate.
101043** The function has P5 arguments. P4 is a pointer to the
101044** FuncDef structure that specifies the function. Register P3 is the
101045** accumulator.
101046**
101047** The P5 arguments are taken from register P2 and its
101048** successors.
101049*/
101050/* Opcode: AggInverse * P2 P3 P4 P5
101051** Synopsis: accum=r[P3] inverse(r[P2@P5])
101052**
101053** Execute the xInverse function for an aggregate.
101054** The function has P5 arguments. P4 is a pointer to the
101055** FuncDef structure that specifies the function. Register P3 is the
101056** accumulator.
101057**
101058** The P5 arguments are taken from register P2 and its
101059** successors.
101060*/
101061/* Opcode: AggStep1 P1 P2 P3 P4 P5
101062** Synopsis: accum=r[P3] step(r[P2@P5])
101063**
101064** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
101065** aggregate. The function has P5 arguments. P4 is a pointer to the
101066** FuncDef structure that specifies the function. Register P3 is the
101067** accumulator.
101068**
101069** The P5 arguments are taken from register P2 and its
101070** successors.
101071**
101072** This opcode is initially coded as OP_AggStep0. On first evaluation,
101073** the FuncDef stored in P4 is converted into an sqlite3_context and
101074** the opcode is changed. In this way, the initialization of the
101075** sqlite3_context only happens once, instead of on each call to the
101076** step function.
101077*/
101078case OP_AggInverse161:
101079case OP_AggStep162: {
101080 int n;
101081 sqlite3_context *pCtx;
101082 u64 nAlloc;
101083
101084 assert( pOp->p4type==P4_FUNCDEF )((void) (0));
101085 n = pOp->p5;
101086 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
101087 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) )((void) (0));
101088 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n )((void) (0));
101089
101090 /* Allocate space for (a) the context object and (n-1) extra pointers
101091 ** to append to the sqlite3_context.argv[1] array, and (b) a memory
101092 ** cell in which to store the accumulation. Be careful that the memory
101093 ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits.
101094 **
101095 ** Note: We could avoid this by using a regular memory cell from aMem[] for
101096 ** the accumulator, instead of allocating one here. */
101097 nAlloc = ROUND8P( sizeof(pCtx[0]) + (n-1)*sizeof(sqlite3_value*) )(sizeof(pCtx[0]) + (n-1)*sizeof(sqlite3_value*));
101098 pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem));
101099 if( pCtx==0 ) goto no_mem;
101100 pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc);
101101 assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) )((void) (0));
101102
101103 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null0x0001);
101104 pCtx->pMem = 0;
101105 pCtx->pFunc = pOp->p4.pFunc;
101106 pCtx->iOp = (int)(pOp - aOp);
101107 pCtx->pVdbe = p;
101108 pCtx->skipFlag = 0;
101109 pCtx->isError = 0;
101110 pCtx->enc = encoding;
101111 pCtx->argc = n;
101112 pOp->p4type = P4_FUNCCTX(-15);
101113 pOp->p4.pCtx = pCtx;
101114
101115 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
101116 assert( pOp->p1==(pOp->opcode==OP_AggInverse) )((void) (0));
101117
101118 pOp->opcode = OP_AggStep1163;
101119 /* Fall through into OP_AggStep */
101120 /* no break */ deliberate_fall_through__attribute__((fallthrough));
101121}
101122case OP_AggStep1163: {
101123 int i;
101124 sqlite3_context *pCtx;
101125 Mem *pMem;
101126
101127 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
101128 pCtx = pOp->p4.pCtx;
101129 pMem = &aMem[pOp->p3];
101130
101131#ifdef SQLITE_DEBUG
101132 if( pOp->p1 ){
101133 /* This is an OP_AggInverse call. Verify that xStep has always
101134 ** been called at least once prior to any xInverse call. */
101135 assert( pMem->uTemp==0x1122e0e3 )((void) (0));
101136 }else{
101137 /* This is an OP_AggStep call. Mark it as such. */
101138 pMem->uTemp = 0x1122e0e3;
101139 }
101140#endif
101141
101142 /* If this function is inside of a trigger, the register array in aMem[]
101143 ** might change from one evaluation to the next. The next block of code
101144 ** checks to see if the register array has changed, and if so it
101145 ** reinitializes the relevant parts of the sqlite3_context object */
101146 if( pCtx->pMem != pMem ){
101147 pCtx->pMem = pMem;
101148 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
101149 }
101150
101151#ifdef SQLITE_DEBUG
101152 for(i=0; i<pCtx->argc; i++){
101153 assert( memIsValid(pCtx->argv[i]) )((void) (0));
101154 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
101155 }
101156#endif
101157
101158 pMem->n++;
101159 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
101160 assert( pCtx->isError==0 )((void) (0));
101161 assert( pCtx->skipFlag==0 )((void) (0));
101162#ifndef SQLITE_OMIT_WINDOWFUNC
101163 if( pOp->p1 ){
101164 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
101165 }else
101166#endif
101167 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
101168
101169 if( pCtx->isError ){
101170 if( pCtx->isError>0 ){
101171 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
101172 rc = pCtx->isError;
101173 }
101174 if( pCtx->skipFlag ){
101175 assert( pOp[-1].opcode==OP_CollSeq )((void) (0));
101176 i = pOp[-1].p1;
101177 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
101178 pCtx->skipFlag = 0;
101179 }
101180 sqlite3VdbeMemRelease(pCtx->pOut);
101181 pCtx->pOut->flags = MEM_Null0x0001;
101182 pCtx->isError = 0;
101183 if( rc ) goto abort_due_to_error;
101184 }
101185 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
101186 assert( pCtx->skipFlag==0 )((void) (0));
101187 break;
101188}
101189
101190/* Opcode: AggFinal P1 P2 * P4 *
101191** Synopsis: accum=r[P1] N=P2
101192**
101193** P1 is the memory location that is the accumulator for an aggregate
101194** or window function. Execute the finalizer function
101195** for an aggregate and store the result in P1.
101196**
101197** P2 is the number of arguments that the step function takes and
101198** P4 is a pointer to the FuncDef for this function. The P2
101199** argument is not used by this opcode. It is only there to disambiguate
101200** functions that can take varying numbers of arguments. The
101201** P4 argument is only needed for the case where
101202** the step function was not previously called.
101203*/
101204/* Opcode: AggValue * P2 P3 P4 *
101205** Synopsis: r[P3]=value N=P2
101206**
101207** Invoke the xValue() function and store the result in register P3.
101208**
101209** P2 is the number of arguments that the step function takes and
101210** P4 is a pointer to the FuncDef for this function. The P2
101211** argument is not used by this opcode. It is only there to disambiguate
101212** functions that can take varying numbers of arguments. The
101213** P4 argument is only needed for the case where
101214** the step function was not previously called.
101215*/
101216case OP_AggValue164:
101217case OP_AggFinal165: {
101218 Mem *pMem;
101219 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
101220 assert( pOp->p3==0 || pOp->opcode==OP_AggValue )((void) (0));
101221 pMem = &aMem[pOp->p1];
101222 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 )((void) (0));
101223#ifndef SQLITE_OMIT_WINDOWFUNC
101224 if( pOp->p3 ){
101225 memAboutToChange(p, &aMem[pOp->p3]);
101226 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
101227 pMem = &aMem[pOp->p3];
101228 }else
101229#endif
101230 {
101231 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
101232 }
101233
101234 if( rc ){
101235 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
101236 goto abort_due_to_error;
101237 }
101238 sqlite3VdbeChangeEncoding(pMem, encoding);
101239 UPDATE_MAX_BLOBSIZE(pMem);
101240 REGISTER_TRACE((int)(pMem-aMem), pMem);
101241 break;
101242}
101243
101244#ifndef SQLITE_OMIT_WAL
101245/* Opcode: Checkpoint P1 P2 P3 * *
101246**
101247** Checkpoint database P1. This is a no-op if P1 is not currently in
101248** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
101249** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
101250** SQLITE_BUSY or not, respectively. Write the number of pages in the
101251** WAL after the checkpoint into mem[P3+1] and the number of pages
101252** in the WAL that have been checkpointed after the checkpoint
101253** completes into mem[P3+2]. However on an error, mem[P3+1] and
101254** mem[P3+2] are initialized to -1.
101255*/
101256case OP_Checkpoint3: {
101257 int i; /* Loop counter */
101258 int aRes[3]; /* Results */
101259 Mem *pMem; /* Write results here */
101260
101261 assert( p->readOnly==0 )((void) (0));
101262 aRes[0] = 0;
101263 aRes[1] = aRes[2] = -1;
101264 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE((void) (0))
101265 || pOp->p2==SQLITE_CHECKPOINT_FULL((void) (0))
101266 || pOp->p2==SQLITE_CHECKPOINT_RESTART((void) (0))
101267 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE((void) (0))
101268 )((void) (0));
101269 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
101270 if( rc ){
101271 if( rc!=SQLITE_BUSY5 ) goto abort_due_to_error;
101272 rc = SQLITE_OK0;
101273 aRes[0] = 1;
101274 }
101275 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
101276 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
101277 }
101278 break;
101279};
101280#endif
101281
101282#ifndef SQLITE_OMIT_PRAGMA
101283/* Opcode: JournalMode P1 P2 P3 * *
101284**
101285** Change the journal mode of database P1 to P3. P3 must be one of the
101286** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
101287** modes (delete, truncate, persist, off and memory), this is a simple
101288** operation. No IO is required.
101289**
101290** If changing into or out of WAL mode the procedure is more complicated.
101291**
101292** Write a string containing the final journal-mode to register P2.
101293*/
101294case OP_JournalMode4: { /* out2 */
101295 Btree *pBt; /* Btree to change journal mode of */
101296 Pager *pPager; /* Pager associated with pBt */
101297 int eNew; /* New journal mode */
101298 int eOld; /* The old journal mode */
101299#ifndef SQLITE_OMIT_WAL
101300 const char *zFilename; /* Name of database file for pPager */
101301#endif
101302
101303 pOut = out2Prerelease(p, pOp);
101304 eNew = pOp->p3;
101305 assert( eNew==PAGER_JOURNALMODE_DELETE((void) (0))
101306 || eNew==PAGER_JOURNALMODE_TRUNCATE((void) (0))
101307 || eNew==PAGER_JOURNALMODE_PERSIST((void) (0))
101308 || eNew==PAGER_JOURNALMODE_OFF((void) (0))
101309 || eNew==PAGER_JOURNALMODE_MEMORY((void) (0))
101310 || eNew==PAGER_JOURNALMODE_WAL((void) (0))
101311 || eNew==PAGER_JOURNALMODE_QUERY((void) (0))
101312 )((void) (0));
101313 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
101314 assert( p->readOnly==0 )((void) (0));
101315
101316 pBt = db->aDb[pOp->p1].pBt;
101317 pPager = sqlite3BtreePager(pBt);
101318 eOld = sqlite3PagerGetJournalMode(pPager);
101319 if( eNew==PAGER_JOURNALMODE_QUERY(-1) ) eNew = eOld;
101320 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
101321 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
101322
101323#ifndef SQLITE_OMIT_WAL
101324 zFilename = sqlite3PagerFilename(pPager, 1);
101325
101326 /* Do not allow a transition to journal_mode=WAL for a database
101327 ** in temporary storage or if the VFS does not support shared memory
101328 */
101329 if( eNew==PAGER_JOURNALMODE_WAL5
101330 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
101331 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
101332 ){
101333 eNew = eOld;
101334 }
101335
101336 if( (eNew!=eOld)
101337 && (eOld==PAGER_JOURNALMODE_WAL5 || eNew==PAGER_JOURNALMODE_WAL5)
101338 ){
101339 if( !db->autoCommit || db->nVdbeRead>1 ){
101340 rc = SQLITE_ERROR1;
101341 sqlite3VdbeError(p,
101342 "cannot change %s wal mode from within a transaction",
101343 (eNew==PAGER_JOURNALMODE_WAL5 ? "into" : "out of")
101344 );
101345 goto abort_due_to_error;
101346 }else{
101347
101348 if( eOld==PAGER_JOURNALMODE_WAL5 ){
101349 /* If leaving WAL mode, close the log file. If successful, the call
101350 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
101351 ** file. An EXCLUSIVE lock may still be held on the database file
101352 ** after a successful return.
101353 */
101354 rc = sqlite3PagerCloseWal(pPager, db);
101355 if( rc==SQLITE_OK0 ){
101356 sqlite3PagerSetJournalMode(pPager, eNew);
101357 }
101358 }else if( eOld==PAGER_JOURNALMODE_MEMORY4 ){
101359 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
101360 ** as an intermediate */
101361 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF2);
101362 }
101363
101364 /* Open a transaction on the database file. Regardless of the journal
101365 ** mode, this transaction always uses a rollback journal.
101366 */
101367 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE )((void) (0));
101368 if( rc==SQLITE_OK0 ){
101369 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL5 ? 2 : 1));
101370 }
101371 }
101372 }
101373#endif /* ifndef SQLITE_OMIT_WAL */
101374
101375 if( rc ) eNew = eOld;
101376 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
101377
101378 pOut->flags = MEM_Str0x0002|MEM_Static0x2000|MEM_Term0x0200;
101379 pOut->z = (char *)sqlite3JournalModename(eNew);
101380 pOut->n = sqlite3Strlen30(pOut->z);
101381 pOut->enc = SQLITE_UTF81;
101382 sqlite3VdbeChangeEncoding(pOut, encoding);
101383 if( rc ) goto abort_due_to_error;
101384 break;
101385};
101386#endif /* SQLITE_OMIT_PRAGMA */
101387
101388#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
101389/* Opcode: Vacuum P1 P2 * * *
101390**
101391** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
101392** for an attached database. The "temp" database may not be vacuumed.
101393**
101394** If P2 is not zero, then it is a register holding a string which is
101395** the file into which the result of vacuum should be written. When
101396** P2 is zero, the vacuum overwrites the original database.
101397*/
101398case OP_Vacuum5: {
101399 assert( p->readOnly==0 )((void) (0));
101400 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
101401 pOp->p2 ? &aMem[pOp->p2] : 0);
101402 if( rc ) goto abort_due_to_error;
101403 break;
101404}
101405#endif
101406
101407#if !defined(SQLITE_OMIT_AUTOVACUUM)
101408/* Opcode: IncrVacuum P1 P2 * * *
101409**
101410** Perform a single step of the incremental vacuum procedure on
101411** the P1 database. If the vacuum has finished, jump to instruction
101412** P2. Otherwise, fall through to the next instruction.
101413*/
101414case OP_IncrVacuum62: { /* jump */
101415 Btree *pBt;
101416
101417 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
101418 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
101419 assert( p->readOnly==0 )((void) (0));
101420 pBt = db->aDb[pOp->p1].pBt;
101421 rc = sqlite3BtreeIncrVacuum(pBt);
101422 VdbeBranchTaken(rc==SQLITE_DONE,2);
101423 if( rc ){
101424 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
101425 rc = SQLITE_OK0;
101426 goto jump_to_p2;
101427 }
101428 break;
101429}
101430#endif
101431
101432/* Opcode: Expire P1 P2 * * *
101433**
101434** Cause precompiled statements to expire. When an expired statement
101435** is executed using sqlite3_step() it will either automatically
101436** reprepare itself (if it was originally created using sqlite3_prepare_v2())
101437** or it will fail with SQLITE_SCHEMA.
101438**
101439** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
101440** then only the currently executing statement is expired.
101441**
101442** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
101443** then running SQL statements are allowed to continue to run to completion.
101444** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
101445** that might help the statement run faster but which does not affect the
101446** correctness of operation.
101447*/
101448case OP_Expire166: {
101449 assert( pOp->p2==0 || pOp->p2==1 )((void) (0));
101450 if( !pOp->p1 ){
101451 sqlite3ExpirePreparedStatements(db, pOp->p2);
101452 }else{
101453 p->expired = pOp->p2+1;
101454 }
101455 break;
101456}
101457
101458/* Opcode: CursorLock P1 * * * *
101459**
101460** Lock the btree to which cursor P1 is pointing so that the btree cannot be
101461** written by an other cursor.
101462*/
101463case OP_CursorLock167: {
101464 VdbeCursor *pC;
101465 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
101466 pC = p->apCsr[pOp->p1];
101467 assert( pC!=0 )((void) (0));
101468 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
101469 sqlite3BtreeCursorPin(pC->uc.pCursor);
101470 break;
101471}
101472
101473/* Opcode: CursorUnlock P1 * * * *
101474**
101475** Unlock the btree to which cursor P1 is pointing so that it can be
101476** written by other cursors.
101477*/
101478case OP_CursorUnlock168: {
101479 VdbeCursor *pC;
101480 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
101481 pC = p->apCsr[pOp->p1];
101482 assert( pC!=0 )((void) (0));
101483 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
101484 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
101485 break;
101486}
101487
101488#ifndef SQLITE_OMIT_SHARED_CACHE
101489/* Opcode: TableLock P1 P2 P3 P4 *
101490** Synopsis: iDb=P1 root=P2 write=P3
101491**
101492** Obtain a lock on a particular table. This instruction is only used when
101493** the shared-cache feature is enabled.
101494**
101495** P1 is the index of the database in sqlite3.aDb[] of the database
101496** on which the lock is acquired. A readlock is obtained if P3==0 or
101497** a write lock if P3==1.
101498**
101499** P2 contains the root-page of the table to lock.
101500**
101501** P4 contains a pointer to the name of the table being locked. This is only
101502** used to generate an error message if the lock cannot be obtained.
101503*/
101504case OP_TableLock169: {
101505 u8 isWriteLock = (u8)pOp->p3;
101506 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit((u64)(0x00004)<<32)) ){
101507 int p1 = pOp->p1;
101508 assert( p1>=0 && p1<db->nDb )((void) (0));
101509 assert( DbMaskTest(p->btreeMask, p1) )((void) (0));
101510 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
101511 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
101512 if( rc ){
101513 if( (rc&0xFF)==SQLITE_LOCKED6 ){
101514 const char *z = pOp->p4.z;
101515 sqlite3VdbeError(p, "database table is locked: %s", z);
101516 }
101517 goto abort_due_to_error;
101518 }
101519 }
101520 break;
101521}
101522#endif /* SQLITE_OMIT_SHARED_CACHE */
101523
101524#ifndef SQLITE_OMIT_VIRTUALTABLE
101525/* Opcode: VBegin * * * P4 *
101526**
101527** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
101528** xBegin method for that table.
101529**
101530** Also, whether or not P4 is set, check that this is not being called from
101531** within a callback to a virtual table xSync() method. If it is, the error
101532** code will be set to SQLITE_LOCKED.
101533*/
101534case OP_VBegin170: {
101535 VTable *pVTab;
101536 pVTab = pOp->p4.pVtab;
101537 rc = sqlite3VtabBegin(db, pVTab);
101538 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
101539 if( rc ) goto abort_due_to_error;
101540 break;
101541}
101542#endif /* SQLITE_OMIT_VIRTUALTABLE */
101543
101544#ifndef SQLITE_OMIT_VIRTUALTABLE
101545/* Opcode: VCreate P1 P2 * * *
101546**
101547** P2 is a register that holds the name of a virtual table in database
101548** P1. Call the xCreate method for that table.
101549*/
101550case OP_VCreate171: {
101551 Mem sMem; /* For storing the record being decoded */
101552 const char *zTab; /* Name of the virtual table */
101553
101554 memset(&sMem, 0, sizeof(sMem));
101555 sMem.db = db;
101556 /* Because P2 is always a static string, it is impossible for the
101557 ** sqlite3VdbeMemCopy() to fail */
101558 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 )((void) (0));
101559 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 )((void) (0));
101560 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
101561 assert( rc==SQLITE_OK )((void) (0));
101562 zTab = (const char*)sqlite3_value_text(&sMem);
101563 assert( zTab || db->mallocFailed )((void) (0));
101564 if( zTab ){
101565 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
101566 }
101567 sqlite3VdbeMemRelease(&sMem);
101568 if( rc ) goto abort_due_to_error;
101569 break;
101570}
101571#endif /* SQLITE_OMIT_VIRTUALTABLE */
101572
101573#ifndef SQLITE_OMIT_VIRTUALTABLE
101574/* Opcode: VDestroy P1 * * P4 *
101575**
101576** P4 is the name of a virtual table in database P1. Call the xDestroy method
101577** of that table.
101578*/
101579case OP_VDestroy172: {
101580 db->nVDestroy++;
101581 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
101582 db->nVDestroy--;
101583 assert( p->errorAction==OE_Abort && p->usesStmtJournal )((void) (0));
101584 if( rc ) goto abort_due_to_error;
101585 break;
101586}
101587#endif /* SQLITE_OMIT_VIRTUALTABLE */
101588
101589#ifndef SQLITE_OMIT_VIRTUALTABLE
101590/* Opcode: VOpen P1 * * P4 *
101591**
101592** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
101593** P1 is a cursor number. This opcode opens a cursor to the virtual
101594** table and stores that cursor in P1.
101595*/
101596case OP_VOpen173: { /* ncycle */
101597 VdbeCursor *pCur;
101598 sqlite3_vtab_cursor *pVCur;
101599 sqlite3_vtab *pVtab;
101600 const sqlite3_module *pModule;
101601
101602 assert( p->bIsReader )((void) (0));
101603 pCur = 0;
101604 pVCur = 0;
101605 pVtab = pOp->p4.pVtab->pVtab;
101606 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
101607 rc = SQLITE_LOCKED6;
101608 goto abort_due_to_error;
101609 }
101610 pModule = pVtab->pModule;
101611 rc = pModule->xOpen(pVtab, &pVCur);
101612 sqlite3VtabImportErrmsg(p, pVtab);
101613 if( rc ) goto abort_due_to_error;
101614
101615 /* Initialize sqlite3_vtab_cursor base class */
101616 pVCur->pVtab = pVtab;
101617
101618 /* Initialize vdbe cursor object */
101619 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB2);
101620 if( pCur ){
101621 pCur->uc.pVCur = pVCur;
101622 pVtab->nRef++;
101623 }else{
101624 assert( db->mallocFailed )((void) (0));
101625 pModule->xClose(pVCur);
101626 goto no_mem;
101627 }
101628 break;
101629}
101630#endif /* SQLITE_OMIT_VIRTUALTABLE */
101631
101632#ifndef SQLITE_OMIT_VIRTUALTABLE
101633/* Opcode: VCheck P1 P2 P3 P4 *
101634**
101635** P4 is a pointer to a Table object that is a virtual table in schema P1
101636** that supports the xIntegrity() method. This opcode runs the xIntegrity()
101637** method for that virtual table, using P3 as the integer argument. If
101638** an error is reported back, the table name is prepended to the error
101639** message and that message is stored in P2. If no errors are seen,
101640** register P2 is set to NULL.
101641*/
101642case OP_VCheck174: { /* out2 */
101643 Table *pTab;
101644 sqlite3_vtab *pVtab;
101645 const sqlite3_module *pModule;
101646 char *zErr = 0;
101647
101648 pOut = &aMem[pOp->p2];
101649 sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */
101650 assert( pOp->p4type==P4_TABLEREF )((void) (0));
101651 pTab = pOp->p4.pTab;
101652 assert( pTab!=0 )((void) (0));
101653 assert( pTab->nTabRef>0 )((void) (0));
101654 assert( IsVirtual(pTab) )((void) (0));
101655 if( pTab->u.vtab.p==0 ) break;
101656 pVtab = pTab->u.vtab.p->pVtab;
101657 assert( pVtab!=0 )((void) (0));
101658 pModule = pVtab->pModule;
101659 assert( pModule!=0 )((void) (0));
101660 assert( pModule->iVersion>=4 )((void) (0));
101661 assert( pModule->xIntegrity!=0 )((void) (0));
101662 sqlite3VtabLock(pTab->u.vtab.p);
101663 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
101664 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName,
101665 pOp->p3, &zErr);
101666 sqlite3VtabUnlock(pTab->u.vtab.p);
101667 if( rc ){
101668 sqlite3_free(zErr);
101669 goto abort_due_to_error;
101670 }
101671 if( zErr ){
101672 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF81, sqlite3_free);
101673 }
101674 break;
101675}
101676#endif /* SQLITE_OMIT_VIRTUALTABLE */
101677
101678#ifndef SQLITE_OMIT_VIRTUALTABLE
101679/* Opcode: VInitIn P1 P2 P3 * *
101680** Synopsis: r[P2]=ValueList(P1,P3)
101681**
101682** Set register P2 to be a pointer to a ValueList object for cursor P1
101683** with cache register P3 and output register P3+1. This ValueList object
101684** can be used as the first argument to sqlite3_vtab_in_first() and
101685** sqlite3_vtab_in_next() to extract all of the values stored in the P1
101686** cursor. Register P3 is used to hold the values returned by
101687** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
101688*/
101689case OP_VInitIn175: { /* out2, ncycle */
101690 VdbeCursor *pC; /* The cursor containing the RHS values */
101691 ValueList *pRhs; /* New ValueList object to put in reg[P2] */
101692
101693 pC = p->apCsr[pOp->p1];
101694 pRhs = sqlite3_malloc64( sizeof(*pRhs) );
101695 if( pRhs==0 ) goto no_mem;
101696 pRhs->pCsr = pC->uc.pCursor;
101697 pRhs->pOut = &aMem[pOp->p3];
101698 pOut = out2Prerelease(p, pOp);
101699 pOut->flags = MEM_Null0x0001;
101700 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3VdbeValueListFree);
101701 break;
101702}
101703#endif /* SQLITE_OMIT_VIRTUALTABLE */
101704
101705
101706#ifndef SQLITE_OMIT_VIRTUALTABLE
101707/* Opcode: VFilter P1 P2 P3 P4 *
101708** Synopsis: iplan=r[P3] zplan='P4'
101709**
101710** P1 is a cursor opened using VOpen. P2 is an address to jump to if
101711** the filtered result set is empty.
101712**
101713** P4 is either NULL or a string that was generated by the xBestIndex
101714** method of the module. The interpretation of the P4 string is left
101715** to the module implementation.
101716**
101717** This opcode invokes the xFilter method on the virtual table specified
101718** by P1. The integer query plan parameter to xFilter is stored in register
101719** P3. Register P3+1 stores the argc parameter to be passed to the
101720** xFilter method. Registers P3+2..P3+1+argc are the argc
101721** additional parameters which are passed to
101722** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
101723**
101724** A jump is made to P2 if the result set after filtering would be empty.
101725*/
101726case OP_VFilter6: { /* jump, ncycle */
101727 int nArg;
101728 int iQuery;
101729 const sqlite3_module *pModule;
101730 Mem *pQuery;
101731 Mem *pArgc;
101732 sqlite3_vtab_cursor *pVCur;
101733 sqlite3_vtab *pVtab;
101734 VdbeCursor *pCur;
101735 int res;
101736 int i;
101737 Mem **apArg;
101738
101739 pQuery = &aMem[pOp->p3];
101740 pArgc = &pQuery[1];
101741 pCur = p->apCsr[pOp->p1];
101742 assert( memIsValid(pQuery) )((void) (0));
101743 REGISTER_TRACE(pOp->p3, pQuery);
101744 assert( pCur!=0 )((void) (0));
101745 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
101746 pVCur = pCur->uc.pVCur;
101747 pVtab = pVCur->pVtab;
101748 pModule = pVtab->pModule;
101749
101750 /* Grab the index number and argc parameters */
101751 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int )((void) (0));
101752 nArg = (int)pArgc->u.i;
101753 iQuery = (int)pQuery->u.i;
101754
101755 /* Invoke the xFilter method */
101756 apArg = p->apArg;
101757 for(i = 0; i<nArg; i++){
101758 apArg[i] = &pArgc[i+1];
101759 }
101760 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
101761 sqlite3VtabImportErrmsg(p, pVtab);
101762 if( rc ) goto abort_due_to_error;
101763 res = pModule->xEof(pVCur);
101764 pCur->nullRow = 0;
101765 VdbeBranchTaken(res!=0,2);
101766 if( res ) goto jump_to_p2;
101767 break;
101768}
101769#endif /* SQLITE_OMIT_VIRTUALTABLE */
101770
101771#ifndef SQLITE_OMIT_VIRTUALTABLE
101772/* Opcode: VColumn P1 P2 P3 * P5
101773** Synopsis: r[P3]=vcolumn(P2)
101774**
101775** Store in register P3 the value of the P2-th column of
101776** the current row of the virtual-table of cursor P1.
101777**
101778** If the VColumn opcode is being used to fetch the value of
101779** an unchanging column during an UPDATE operation, then the P5
101780** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
101781** function to return true inside the xColumn method of the virtual
101782** table implementation. The P5 column might also contain other
101783** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
101784** unused by OP_VColumn.
101785*/
101786case OP_VColumn176: { /* ncycle */
101787 sqlite3_vtab *pVtab;
101788 const sqlite3_module *pModule;
101789 Mem *pDest;
101790 sqlite3_context sContext;
101791 FuncDef nullFunc;
101792
101793 VdbeCursor *pCur = p->apCsr[pOp->p1];
101794 assert( pCur!=0 )((void) (0));
101795 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
101796 pDest = &aMem[pOp->p3];
101797 memAboutToChange(p, pDest);
101798 if( pCur->nullRow ){
101799 sqlite3VdbeMemSetNull(pDest);
101800 break;
101801 }
101802 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
101803 pVtab = pCur->uc.pVCur->pVtab;
101804 pModule = pVtab->pModule;
101805 assert( pModule->xColumn )((void) (0));
101806 memset(&sContext, 0, sizeof(sContext));
101807 sContext.pOut = pDest;
101808 sContext.enc = encoding;
101809 nullFunc.pUserData = 0;
101810 nullFunc.funcFlags = SQLITE_RESULT_SUBTYPE0x001000000;
101811 sContext.pFunc = &nullFunc;
101812 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 )((void) (0));
101813 if( pOp->p5 & OPFLAG_NOCHNG0x01 ){
101814 sqlite3VdbeMemSetNull(pDest);
101815 pDest->flags = MEM_Null0x0001|MEM_Zero0x0400;
101816 pDest->u.nZero = 0;
101817 }else{
101818 MemSetTypeFlag(pDest, MEM_Null)((pDest)->flags = ((pDest)->flags&~(0x0dbf|0x0400))
|0x0001)
;
101819 }
101820 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
101821 sqlite3VtabImportErrmsg(p, pVtab);
101822 if( sContext.isError>0 ){
101823 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
101824 rc = sContext.isError;
101825 }
101826 sqlite3VdbeChangeEncoding(pDest, encoding);
101827 REGISTER_TRACE(pOp->p3, pDest);
101828 UPDATE_MAX_BLOBSIZE(pDest);
101829
101830 if( rc ) goto abort_due_to_error;
101831 break;
101832}
101833#endif /* SQLITE_OMIT_VIRTUALTABLE */
101834
101835#ifndef SQLITE_OMIT_VIRTUALTABLE
101836/* Opcode: VNext P1 P2 * * *
101837**
101838** Advance virtual table P1 to the next row in its result set and
101839** jump to instruction P2. Or, if the virtual table has reached
101840** the end of its result set, then fall through to the next instruction.
101841*/
101842case OP_VNext63: { /* jump, ncycle */
101843 sqlite3_vtab *pVtab;
101844 const sqlite3_module *pModule;
101845 int res;
101846 VdbeCursor *pCur;
101847
101848 pCur = p->apCsr[pOp->p1];
101849 assert( pCur!=0 )((void) (0));
101850 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
101851 if( pCur->nullRow ){
101852 break;
101853 }
101854 pVtab = pCur->uc.pVCur->pVtab;
101855 pModule = pVtab->pModule;
101856 assert( pModule->xNext )((void) (0));
101857
101858 /* Invoke the xNext() method of the module. There is no way for the
101859 ** underlying implementation to return an error if one occurs during
101860 ** xNext(). Instead, if an error occurs, true is returned (indicating that
101861 ** data is available) and the error code returned when xColumn or
101862 ** some other method is next invoked on the save virtual table cursor.
101863 */
101864 rc = pModule->xNext(pCur->uc.pVCur);
101865 sqlite3VtabImportErrmsg(p, pVtab);
101866 if( rc ) goto abort_due_to_error;
101867 res = pModule->xEof(pCur->uc.pVCur);
101868 VdbeBranchTaken(!res,2);
101869 if( !res ){
101870 /* If there is data, jump to P2 */
101871 goto jump_to_p2_and_check_for_interrupt;
101872 }
101873 goto check_for_interrupt;
101874}
101875#endif /* SQLITE_OMIT_VIRTUALTABLE */
101876
101877#ifndef SQLITE_OMIT_VIRTUALTABLE
101878/* Opcode: VRename P1 * * P4 *
101879**
101880** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
101881** This opcode invokes the corresponding xRename method. The value
101882** in register P1 is passed as the zName argument to the xRename method.
101883*/
101884case OP_VRename177: {
101885 sqlite3_vtab *pVtab;
101886 Mem *pName;
101887 int isLegacy;
101888
101889 isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
101890 db->flags |= SQLITE_LegacyAlter0x04000000;
101891 pVtab = pOp->p4.pVtab->pVtab;
101892 pName = &aMem[pOp->p1];
101893 assert( pVtab->pModule->xRename )((void) (0));
101894 assert( memIsValid(pName) )((void) (0));
101895 assert( p->readOnly==0 )((void) (0));
101896 REGISTER_TRACE(pOp->p1, pName);
101897 assert( pName->flags & MEM_Str )((void) (0));
101898 testcase( pName->enc==SQLITE_UTF8 );
101899 testcase( pName->enc==SQLITE_UTF16BE );
101900 testcase( pName->enc==SQLITE_UTF16LE );
101901 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF81);
101902 if( rc ) goto abort_due_to_error;
101903 rc = pVtab->pModule->xRename(pVtab, pName->z);
101904 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter0x04000000;
101905 sqlite3VtabImportErrmsg(p, pVtab);
101906 p->expired = 0;
101907 if( rc ) goto abort_due_to_error;
101908 break;
101909}
101910#endif
101911
101912#ifndef SQLITE_OMIT_VIRTUALTABLE
101913/* Opcode: VUpdate P1 P2 P3 P4 P5
101914** Synopsis: data=r[P3@P2]
101915**
101916** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
101917** This opcode invokes the corresponding xUpdate method. P2 values
101918** are contiguous memory cells starting at P3 to pass to the xUpdate
101919** invocation. The value in register (P3+P2-1) corresponds to the
101920** p2th element of the argv array passed to xUpdate.
101921**
101922** The xUpdate method will do a DELETE or an INSERT or both.
101923** The argv[0] element (which corresponds to memory cell P3)
101924** is the rowid of a row to delete. If argv[0] is NULL then no
101925** deletion occurs. The argv[1] element is the rowid of the new
101926** row. This can be NULL to have the virtual table select the new
101927** rowid for itself. The subsequent elements in the array are
101928** the values of columns in the new row.
101929**
101930** If P2==1 then no insert is performed. argv[0] is the rowid of
101931** a row to delete.
101932**
101933** P1 is a boolean flag. If it is set to true and the xUpdate call
101934** is successful, then the value returned by sqlite3_last_insert_rowid()
101935** is set to the value of the rowid for the row just inserted.
101936**
101937** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
101938** apply in the case of a constraint failure on an insert or update.
101939*/
101940case OP_VUpdate7: {
101941 sqlite3_vtab *pVtab;
101942 const sqlite3_module *pModule;
101943 int nArg;
101944 int i;
101945 sqlite_int64 rowid = 0;
101946 Mem **apArg;
101947 Mem *pX;
101948
101949 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback((void) (0))
101950 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace((void) (0))
101951 )((void) (0));
101952 assert( p->readOnly==0 )((void) (0));
101953 if( db->mallocFailed ) goto no_mem;
101954 sqlite3VdbeIncrWriteCounter(p, 0);
101955 pVtab = pOp->p4.pVtab->pVtab;
101956 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
101957 rc = SQLITE_LOCKED6;
101958 goto abort_due_to_error;
101959 }
101960 pModule = pVtab->pModule;
101961 nArg = pOp->p2;
101962 assert( pOp->p4type==P4_VTAB )((void) (0));
101963 if( ALWAYS(pModule->xUpdate)(pModule->xUpdate) ){
101964 u8 vtabOnConflict = db->vtabOnConflict;
101965 apArg = p->apArg;
101966 pX = &aMem[pOp->p3];
101967 for(i=0; i<nArg; i++){
101968 assert( memIsValid(pX) )((void) (0));
101969 memAboutToChange(p, pX);
101970 apArg[i] = pX;
101971 pX++;
101972 }
101973 db->vtabOnConflict = pOp->p5;
101974 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
101975 db->vtabOnConflict = vtabOnConflict;
101976 sqlite3VtabImportErrmsg(p, pVtab);
101977 if( rc==SQLITE_OK0 && pOp->p1 ){
101978 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) )((void) (0));
101979 db->lastRowid = rowid;
101980 }
101981 if( (rc&0xff)==SQLITE_CONSTRAINT19 && pOp->p4.pVtab->bConstraint ){
101982 if( pOp->p5==OE_Ignore4 ){
101983 rc = SQLITE_OK0;
101984 }else{
101985 p->errorAction = ((pOp->p5==OE_Replace5) ? OE_Abort2 : pOp->p5);
101986 }
101987 }else{
101988 p->nChange++;
101989 }
101990 if( rc ) goto abort_due_to_error;
101991 }
101992 break;
101993}
101994#endif /* SQLITE_OMIT_VIRTUALTABLE */
101995
101996#ifndef SQLITE_OMIT_PAGER_PRAGMAS
101997/* Opcode: Pagecount P1 P2 * * *
101998**
101999** Write the current number of pages in database P1 to memory cell P2.
102000*/
102001case OP_Pagecount178: { /* out2 */
102002 pOut = out2Prerelease(p, pOp);
102003 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
102004 break;
102005}
102006#endif
102007
102008
102009#ifndef SQLITE_OMIT_PAGER_PRAGMAS
102010/* Opcode: MaxPgcnt P1 P2 P3 * *
102011**
102012** Try to set the maximum page count for database P1 to the value in P3.
102013** Do not let the maximum page count fall below the current page count and
102014** do not change the maximum page count value if P3==0.
102015**
102016** Store the maximum page count after the change in register P2.
102017*/
102018case OP_MaxPgcnt179: { /* out2 */
102019 unsigned int newMax;
102020 Btree *pBt;
102021
102022 pOut = out2Prerelease(p, pOp);
102023 pBt = db->aDb[pOp->p1].pBt;
102024 newMax = 0;
102025 if( pOp->p3 ){
102026 newMax = sqlite3BtreeLastPage(pBt);
102027 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
102028 }
102029 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
102030 break;
102031}
102032#endif
102033
102034/* Opcode: Function P1 P2 P3 P4 *
102035** Synopsis: r[P3]=func(r[P2@NP])
102036**
102037** Invoke a user function (P4 is a pointer to an sqlite3_context object that
102038** contains a pointer to the function to be run) with arguments taken
102039** from register P2 and successors. The number of arguments is in
102040** the sqlite3_context object that P4 points to.
102041** The result of the function is stored
102042** in register P3. Register P3 must not be one of the function inputs.
102043**
102044** P1 is a 32-bit bitmask indicating whether or not each argument to the
102045** function was determined to be constant at compile time. If the first
102046** argument was constant then bit 0 of P1 is set. This is used to determine
102047** whether meta data associated with a user function argument using the
102048** sqlite3_set_auxdata() API may be safely retained until the next
102049** invocation of this opcode.
102050**
102051** See also: AggStep, AggFinal, PureFunc
102052*/
102053/* Opcode: PureFunc P1 P2 P3 P4 *
102054** Synopsis: r[P3]=func(r[P2@NP])
102055**
102056** Invoke a user function (P4 is a pointer to an sqlite3_context object that
102057** contains a pointer to the function to be run) with arguments taken
102058** from register P2 and successors. The number of arguments is in
102059** the sqlite3_context object that P4 points to.
102060** The result of the function is stored
102061** in register P3. Register P3 must not be one of the function inputs.
102062**
102063** P1 is a 32-bit bitmask indicating whether or not each argument to the
102064** function was determined to be constant at compile time. If the first
102065** argument was constant then bit 0 of P1 is set. This is used to determine
102066** whether meta data associated with a user function argument using the
102067** sqlite3_set_auxdata() API may be safely retained until the next
102068** invocation of this opcode.
102069**
102070** This opcode works exactly like OP_Function. The only difference is in
102071** its name. This opcode is used in places where the function must be
102072** purely non-deterministic. Some built-in date/time functions can be
102073** either deterministic of non-deterministic, depending on their arguments.
102074** When those function are used in a non-deterministic way, they will check
102075** to see if they were called using OP_PureFunc instead of OP_Function, and
102076** if they were, they throw an error.
102077**
102078** See also: AggStep, AggFinal, Function
102079*/
102080case OP_PureFunc65: /* group */
102081case OP_Function66: { /* group */
102082 int i;
102083 sqlite3_context *pCtx;
102084
102085 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
102086 pCtx = pOp->p4.pCtx;
102087
102088 /* If this function is inside of a trigger, the register array in aMem[]
102089 ** might change from one evaluation to the next. The next block of code
102090 ** checks to see if the register array has changed, and if so it
102091 ** reinitializes the relevant parts of the sqlite3_context object */
102092 pOut = &aMem[pOp->p3];
102093 if( pCtx->pOut != pOut ){
102094 pCtx->pVdbe = p;
102095 pCtx->pOut = pOut;
102096 pCtx->enc = encoding;
102097 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
102098 }
102099 assert( pCtx->pVdbe==p )((void) (0));
102100
102101 memAboutToChange(p, pOut);
102102#ifdef SQLITE_DEBUG
102103 for(i=0; i<pCtx->argc; i++){
102104 assert( memIsValid(pCtx->argv[i]) )((void) (0));
102105 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
102106 }
102107#endif
102108 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0001
)
;
102109 assert( pCtx->isError==0 )((void) (0));
102110 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
102111
102112 /* If the function returned an error, throw an exception */
102113 if( pCtx->isError ){
102114 if( pCtx->isError>0 ){
102115 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
102116 rc = pCtx->isError;
102117 }
102118 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
102119 pCtx->isError = 0;
102120 if( rc ) goto abort_due_to_error;
102121 }
102122
102123 assert( (pOut->flags&MEM_Str)==0((void) (0))
102124 || pOut->enc==encoding((void) (0))
102125 || db->mallocFailed )((void) (0));
102126 assert( !sqlite3VdbeMemTooBig(pOut) )((void) (0));
102127
102128 REGISTER_TRACE(pOp->p3, pOut);
102129 UPDATE_MAX_BLOBSIZE(pOut);
102130 break;
102131}
102132
102133/* Opcode: ClrSubtype P1 * * * *
102134** Synopsis: r[P1].subtype = 0
102135**
102136** Clear the subtype from register P1.
102137*/
102138case OP_ClrSubtype180: { /* in1 */
102139 pIn1 = &aMem[pOp->p1];
102140 pIn1->flags &= ~MEM_Subtype0x0800;
102141 break;
102142}
102143
102144/* Opcode: GetSubtype P1 P2 * * *
102145** Synopsis: r[P2] = r[P1].subtype
102146**
102147** Extract the subtype value from register P1 and write that subtype
102148** into register P2. If P1 has no subtype, then P1 gets a NULL.
102149*/
102150case OP_GetSubtype181: { /* in1 out2 */
102151 pIn1 = &aMem[pOp->p1];
102152 pOut = &aMem[pOp->p2];
102153 if( pIn1->flags & MEM_Subtype0x0800 ){
102154 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype);
102155 }else{
102156 sqlite3VdbeMemSetNull(pOut);
102157 }
102158 break;
102159}
102160
102161/* Opcode: SetSubtype P1 P2 * * *
102162** Synopsis: r[P2].subtype = r[P1]
102163**
102164** Set the subtype value of register P2 to the integer from register P1.
102165** If P1 is NULL, clear the subtype from p2.
102166*/
102167case OP_SetSubtype182: { /* in1 out2 */
102168 pIn1 = &aMem[pOp->p1];
102169 pOut = &aMem[pOp->p2];
102170 if( pIn1->flags & MEM_Null0x0001 ){
102171 pOut->flags &= ~MEM_Subtype0x0800;
102172 }else{
102173 assert( pIn1->flags & MEM_Int )((void) (0));
102174 pOut->flags |= MEM_Subtype0x0800;
102175 pOut->eSubtype = (u8)(pIn1->u.i & 0xff);
102176 }
102177 break;
102178}
102179
102180/* Opcode: FilterAdd P1 * P3 P4 *
102181** Synopsis: filter(P1) += key(P3@P4)
102182**
102183** Compute a hash on the P4 registers starting with r[P3] and
102184** add that hash to the bloom filter contained in r[P1].
102185*/
102186case OP_FilterAdd183: {
102187 u64 h;
102188
102189 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
102190 pIn1 = &aMem[pOp->p1];
102191 assert( pIn1->flags & MEM_Blob )((void) (0));
102192 assert( pIn1->n>0 )((void) (0));
102193 h = filterHash(aMem, pOp);
102194#ifdef SQLITE_DEBUG
102195 if( db->flags&SQLITE_VdbeTrace ){
102196 int ii;
102197 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
102198 registerTrace(ii, &aMem[ii]);
102199 }
102200 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
102201 }
102202#endif
102203 h %= (pIn1->n*8);
102204 pIn1->z[h/8] |= 1<<(h&7);
102205 break;
102206}
102207
102208/* Opcode: Filter P1 P2 P3 P4 *
102209** Synopsis: if key(P3@P4) not in filter(P1) goto P2
102210**
102211** Compute a hash on the key contained in the P4 registers starting
102212** with r[P3]. Check to see if that hash is found in the
102213** bloom filter hosted by register P1. If it is not present then
102214** maybe jump to P2. Otherwise fall through.
102215**
102216** False negatives are harmless. It is always safe to fall through,
102217** even if the value is in the bloom filter. A false negative causes
102218** more CPU cycles to be used, but it should still yield the correct
102219** answer. However, an incorrect answer may well arise from a
102220** false positive - if the jump is taken when it should fall through.
102221*/
102222case OP_Filter64: { /* jump */
102223 u64 h;
102224
102225 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
102226 pIn1 = &aMem[pOp->p1];
102227 assert( (pIn1->flags & MEM_Blob)!=0 )((void) (0));
102228 assert( pIn1->n >= 1 )((void) (0));
102229 h = filterHash(aMem, pOp);
102230#ifdef SQLITE_DEBUG
102231 if( db->flags&SQLITE_VdbeTrace ){
102232 int ii;
102233 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
102234 registerTrace(ii, &aMem[ii]);
102235 }
102236 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
102237 }
102238#endif
102239 h %= (pIn1->n*8);
102240 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
102241 VdbeBranchTaken(1, 2);
102242 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT8]++;
102243 goto jump_to_p2;
102244 }else{
102245 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS7]++;
102246 VdbeBranchTaken(0, 2);
102247 }
102248 break;
102249}
102250
102251/* Opcode: Trace P1 P2 * P4 *
102252**
102253** Write P4 on the statement trace output if statement tracing is
102254** enabled.
102255**
102256** Operand P1 must be 0x7fffffff and P2 must positive.
102257*/
102258/* Opcode: Init P1 P2 P3 P4 *
102259** Synopsis: Start at P2
102260**
102261** Programs contain a single instance of this opcode as the very first
102262** opcode.
102263**
102264** If tracing is enabled (by the sqlite3_trace()) interface, then
102265** the UTF-8 string contained in P4 is emitted on the trace callback.
102266** Or if P4 is blank, use the string returned by sqlite3_sql().
102267**
102268** If P2 is not zero, jump to instruction P2.
102269**
102270** Increment the value of P1 so that OP_Once opcodes will jump the
102271** first time they are evaluated for this run.
102272**
102273** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
102274** error is encountered.
102275*/
102276case OP_Trace184:
102277case OP_Init8: { /* jump0 */
102278 int i;
102279#ifndef SQLITE_OMIT_TRACE
102280 char *zTrace;
102281#endif
102282
102283 /* If the P4 argument is not NULL, then it must be an SQL comment string.
102284 ** The "--" string is broken up to prevent false-positives with srcck1.c.
102285 **
102286 ** This assert() provides evidence for:
102287 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
102288 ** would have been returned by the legacy sqlite3_trace() interface by
102289 ** using the X argument when X begins with "--" and invoking
102290 ** sqlite3_expanded_sql(P) otherwise.
102291 */
102292 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 )((void) (0));
102293
102294 /* OP_Init is always instruction 0 */
102295 assert( pOp==p->aOp || pOp->opcode==OP_Trace )((void) (0));
102296
102297#ifndef SQLITE_OMIT_TRACE
102298 if( (db->mTrace & (SQLITE_TRACE_STMT0x01|SQLITE_TRACE_LEGACY0))!=0
102299 && p->minWriteFileFormat!=254 /* tag-20220401a */
102300 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
102301 ){
102302#ifndef SQLITE_OMIT_DEPRECATED1
102303 if( db->mTrace & SQLITE_TRACE_LEGACY0 ){
102304 char *z = sqlite3VdbeExpandSql(p, zTrace);
102305 db->trace.xLegacy(db->pTraceArg, z);
102306 sqlite3_free(z);
102307 }else
102308#endif
102309 if( db->nVdbeExec>1 ){
102310 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
102311 (void)db->trace.xV2(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, z);
102312 sqlite3DbFree(db, z);
102313 }else{
102314 (void)db->trace.xV2(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, zTrace);
102315 }
102316 }
102317#ifdef SQLITE_USE_FCNTL_TRACE
102318 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
102319 if( zTrace ){
102320 int j;
102321 for(j=0; j<db->nDb; j++){
102322 if( DbMaskTest(p->btreeMask, j)(((p->btreeMask)&(((yDbMask)1)<<(j)))!=0)==0 ) continue;
102323 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE19, zTrace);
102324 }
102325 }
102326#endif /* SQLITE_USE_FCNTL_TRACE */
102327#ifdef SQLITE_DEBUG
102328 if( (db->flags & SQLITE_SqlTrace)!=0
102329 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
102330 ){
102331 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
102332 }
102333#endif /* SQLITE_DEBUG */
102334#endif /* SQLITE_OMIT_TRACE */
102335 assert( pOp->p2>0 )((void) (0));
102336 if( pOp->p1>=sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold ){
102337 if( pOp->opcode==OP_Trace184 ) break;
102338 for(i=1; i<p->nOp; i++){
102339 if( p->aOp[i].opcode==OP_Once15 ) p->aOp[i].p1 = 0;
102340 }
102341 pOp->p1 = 0;
102342 }
102343 pOp->p1++;
102344 p->aCounter[SQLITE_STMTSTATUS_RUN6]++;
102345 goto jump_to_p2;
102346}
102347
102348#ifdef SQLITE_ENABLE_CURSOR_HINTS
102349/* Opcode: CursorHint P1 * * P4 *
102350**
102351** Provide a hint to cursor P1 that it only needs to return rows that
102352** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
102353** to values currently held in registers. TK_COLUMN terms in the P4
102354** expression refer to columns in the b-tree to which cursor P1 is pointing.
102355*/
102356case OP_CursorHint185: {
102357 VdbeCursor *pC;
102358
102359 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
102360 assert( pOp->p4type==P4_EXPR )((void) (0));
102361 pC = p->apCsr[pOp->p1];
102362 if( pC ){
102363 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
102364 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE0,
102365 pOp->p4.pExpr, aMem);
102366 }
102367 break;
102368}
102369#endif /* SQLITE_ENABLE_CURSOR_HINTS */
102370
102371#ifdef SQLITE_DEBUG
102372/* Opcode: Abortable * * * * *
102373**
102374** Verify that an Abort can happen. Assert if an Abort at this point
102375** might cause database corruption. This opcode only appears in debugging
102376** builds.
102377**
102378** An Abort is safe if either there have been no writes, or if there is
102379** an active statement journal.
102380*/
102381case OP_Abortable189: {
102382 sqlite3VdbeAssertAbortable(p);
102383 break;
102384}
102385#endif
102386
102387#ifdef SQLITE_DEBUG
102388/* Opcode: ReleaseReg P1 P2 P3 * P5
102389** Synopsis: release r[P1@P2] mask P3
102390**
102391** Release registers from service. Any content that was in the
102392** the registers is unreliable after this opcode completes.
102393**
102394** The registers released will be the P2 registers starting at P1,
102395** except if bit ii of P3 set, then do not release register P1+ii.
102396** In other words, P3 is a mask of registers to preserve.
102397**
102398** Releasing a register clears the Mem.pScopyFrom pointer. That means
102399** that if the content of the released register was set using OP_SCopy,
102400** a change to the value of the source register for the OP_SCopy will no longer
102401** generate an assertion fault in sqlite3VdbeMemAboutToChange().
102402**
102403** If P5 is set, then all released registers have their type set
102404** to MEM_Undefined so that any subsequent attempt to read the released
102405** register (before it is reinitialized) will generate an assertion fault.
102406**
102407** P5 ought to be set on every call to this opcode.
102408** However, there are places in the code generator will release registers
102409** before their are used, under the (valid) assumption that the registers
102410** will not be reallocated for some other purpose before they are used and
102411** hence are safe to release.
102412**
102413** This opcode is only available in testing and debugging builds. It is
102414** not generated for release builds. The purpose of this opcode is to help
102415** validate the generated bytecode. This opcode does not actually contribute
102416** to computing an answer.
102417*/
102418case OP_ReleaseReg186: {
102419 Mem *pMem;
102420 int i;
102421 u32 constMask;
102422 assert( pOp->p1>0 )((void) (0));
102423 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
102424 pMem = &aMem[pOp->p1];
102425 constMask = pOp->p3;
102426 for(i=0; i<pOp->p2; i++, pMem++){
102427 if( i>=32 || (constMask & MASKBIT32(i)(((unsigned int)1)<<(i)))==0 ){
102428 pMem->pScopyFrom = 0;
102429 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined)((pMem)->flags = ((pMem)->flags&~(0x0dbf|0x0400))|0x0000
)
;
102430 }
102431 }
102432 break;
102433}
102434#endif
102435
102436/* Opcode: Noop * * * * *
102437**
102438** Do nothing. Continue downward to the next opcode.
102439*/
102440/* Opcode: Explain P1 P2 P3 P4 *
102441**
102442** This is the same as OP_Noop during normal query execution. The
102443** purpose of this opcode is to hold information about the query
102444** plan for the purpose of EXPLAIN QUERY PLAN output.
102445**
102446** The P4 value is human-readable text that describes the query plan
102447** element. Something like "SCAN t1" or "SEARCH t2 USING INDEX t2x1".
102448**
102449** The P1 value is the ID of the current element and P2 is the parent
102450** element for the case of nested query plan elements. If P2 is zero
102451** then this element is a top-level element.
102452**
102453** For loop elements, P3 is the estimated code of each invocation of this
102454** element.
102455**
102456** As with all opcodes, the meanings of the parameters for OP_Explain
102457** are subject to change from one release to the next. Applications
102458** should not attempt to interpret or use any of the information
102459** contained in the OP_Explain opcode. The information provided by this
102460** opcode is intended for testing and debugging use only.
102461*/
102462default: { /* This is really OP_Noop, OP_Explain */
102463 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain )((void) (0));
102464
102465 break;
102466}
102467
102468/*****************************************************************************
102469** The cases of the switch statement above this line should all be indented
102470** by 6 spaces. But the left-most 6 spaces have been removed to improve the
102471** readability. From this point on down, the normal indentation rules are
102472** restored.
102473*****************************************************************************/
102474 }
102475
102476#if defined(VDBE_PROFILE)
102477 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
102478 pnCycle = 0;
102479#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
102480 if( pnCycle ){
102481 *pnCycle += sqlite3Hwtime();
102482 pnCycle = 0;
102483 }
102484#endif
102485
102486 /* The following code adds nothing to the actual functionality
102487 ** of the program. It is only here for testing and debugging.
102488 ** On the other hand, it does burn CPU cycles every time through
102489 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
102490 */
102491#ifndef NDEBUG1
102492 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] )((void) (0));
102493
102494#ifdef SQLITE_DEBUG
102495 if( db->flags & SQLITE_VdbeTrace ){
102496 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
102497 if( rc!=0 ) printf("rc=%d\n",rc);
102498 if( opProperty & (OPFLG_OUT20x10) ){
102499 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
102500 }
102501 if( opProperty & OPFLG_OUT30x20 ){
102502 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
102503 }
102504 if( opProperty==0xff ){
102505 /* Never happens. This code exists to avoid a harmless linkage
102506 ** warning about sqlite3VdbeRegisterDump() being defined but not
102507 ** used. */
102508 sqlite3VdbeRegisterDump(p);
102509 }
102510 }
102511#endif /* SQLITE_DEBUG */
102512#endif /* NDEBUG */
102513 } /* The end of the for(;;) loop the loops through opcodes */
102514
102515 /* If we reach this point, it means that execution is finished with
102516 ** an error of some kind.
102517 */
102518abort_due_to_error:
102519 if( db->mallocFailed ){
102520 rc = SQLITE_NOMEM_BKPT7;
102521 }else if( rc==SQLITE_IOERR_CORRUPTFS(10 | (33<<8)) ){
102522 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(102522);
102523 }
102524 assert( rc )((void) (0));
102525#ifdef SQLITE_DEBUG
102526 if( db->flags & SQLITE_VdbeTrace ){
102527 const char *zTrace = p->zSql;
102528 if( zTrace==0 ){
102529 if( aOp[0].opcode==OP_Trace184 ){
102530 zTrace = aOp[0].p4.z;
102531 }
102532 if( zTrace==0 ) zTrace = "???";
102533 }
102534 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
102535 }
102536#endif
102537 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
102538 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
102539 }
102540 p->rc = rc;
102541 sqlite3SystemError(db, rc);
102542 testcase( sqlite3GlobalConfig.xLog!=0 );
102543 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
102544 (int)(pOp - aOp), p->zSql, p->zErrMsg);
102545 if( p->eVdbeState==VDBE_RUN_STATE2 ) sqlite3VdbeHalt(p);
102546 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) sqlite3OomFault(db);
102547 if( rc==SQLITE_CORRUPT11 && db->autoCommit==0 ){
102548 db->flags |= SQLITE_CorruptRdOnly((u64)(0x00002)<<32);
102549 }
102550 rc = SQLITE_ERROR1;
102551 if( resetSchemaOnFault>0 ){
102552 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
102553 }
102554
102555 /* This is the only way out of this procedure. We have to
102556 ** release the mutexes on btrees that were acquired at the
102557 ** top. */
102558vdbe_return:
102559#if defined(VDBE_PROFILE)
102560 if( pnCycle ){
102561 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
102562 pnCycle = 0;
102563 }
102564#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
102565 if( pnCycle ){
102566 *pnCycle += sqlite3Hwtime();
102567 pnCycle = 0;
102568 }
102569#endif
102570
102571#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
102572 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
102573 nProgressLimit += db->nProgressOps;
102574 if( db->xProgress(db->pProgressArg) ){
102575 nProgressLimit = LARGEST_UINT64(0xffffffff|(((u64)0xffffffff)<<32));
102576 rc = SQLITE_INTERRUPT9;
102577 goto abort_due_to_error;
102578 }
102579 }
102580#endif
102581 p->aCounter[SQLITE_STMTSTATUS_VM_STEP4] += (int)nVmStep;
102582 if( DbMaskNonZero(p->lockMask)((p->lockMask)!=0) ){
102583 sqlite3VdbeLeave(p);
102584 }
102585 assert( rc!=SQLITE_OK || nExtraDelete==0((void) (0))
102586 || sqlite3_strlike("DELETE%",p->zSql,0)!=0((void) (0))
102587 )((void) (0));
102588 return rc;
102589
102590 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
102591 ** is encountered.
102592 */
102593too_big:
102594 sqlite3VdbeError(p, "string or blob too big");
102595 rc = SQLITE_TOOBIG18;
102596 goto abort_due_to_error;
102597
102598 /* Jump to here if a malloc() fails.
102599 */
102600no_mem:
102601 sqlite3OomFault(db);
102602 sqlite3VdbeError(p, "out of memory");
102603 rc = SQLITE_NOMEM_BKPT7;
102604 goto abort_due_to_error;
102605
102606 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
102607 ** flag.
102608 */
102609abort_due_to_interrupt:
102610 assert( AtomicLoad(&db->u1.isInterrupted) )((void) (0));
102611 rc = SQLITE_INTERRUPT9;
102612 goto abort_due_to_error;
102613}
102614
102615
102616/************** End of vdbe.c ************************************************/
102617/************** Begin file vdbeblob.c ****************************************/
102618/*
102619** 2007 May 1
102620**
102621** The author disclaims copyright to this source code. In place of
102622** a legal notice, here is a blessing:
102623**
102624** May you do good and not evil.
102625** May you find forgiveness for yourself and forgive others.
102626** May you share freely, never taking more than you give.
102627**
102628*************************************************************************
102629**
102630** This file contains code used to implement incremental BLOB I/O.
102631*/
102632
102633/* #include "sqliteInt.h" */
102634/* #include "vdbeInt.h" */
102635
102636#ifndef SQLITE_OMIT_INCRBLOB
102637
102638/*
102639** Valid sqlite3_blob* handles point to Incrblob structures.
102640*/
102641typedef struct Incrblob Incrblob;
102642struct Incrblob {
102643 int nByte; /* Size of open blob, in bytes */
102644 int iOffset; /* Byte offset of blob in cursor data */
102645 u16 iCol; /* Table column this handle is open on */
102646 BtCursor *pCsr; /* Cursor pointing at blob row */
102647 sqlite3_stmt *pStmt; /* Statement holding cursor open */
102648 sqlite3 *db; /* The associated database */
102649 char *zDb; /* Database name */
102650 Table *pTab; /* Table object */
102651};
102652
102653
102654/*
102655** This function is used by both blob_open() and blob_reopen(). It seeks
102656** the b-tree cursor associated with blob handle p to point to row iRow.
102657** If successful, SQLITE_OK is returned and subsequent calls to
102658** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
102659**
102660** If an error occurs, or if the specified row does not exist or does not
102661** contain a value of type TEXT or BLOB in the column nominated when the
102662** blob handle was opened, then an error code is returned and *pzErr may
102663** be set to point to a buffer containing an error message. It is the
102664** responsibility of the caller to free the error message buffer using
102665** sqlite3DbFree().
102666**
102667** If an error does occur, then the b-tree cursor is closed. All subsequent
102668** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
102669** immediately return SQLITE_ABORT.
102670*/
102671static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
102672 int rc; /* Error code */
102673 char *zErr = 0; /* Error message */
102674 Vdbe *v = (Vdbe *)p->pStmt;
102675
102676 /* Set the value of register r[1] in the SQL statement to integer iRow.
102677 ** This is done directly as a performance optimization
102678 */
102679 sqlite3VdbeMemSetInt64(&v->aMem[1], iRow);
102680
102681 /* If the statement has been run before (and is paused at the OP_ResultRow)
102682 ** then back it up to the point where it does the OP_NotExists. This could
102683 ** have been down with an extra OP_Goto, but simply setting the program
102684 ** counter is faster. */
102685 if( v->pc>4 ){
102686 v->pc = 4;
102687 assert( v->aOp[v->pc].opcode==OP_NotExists )((void) (0));
102688 rc = sqlite3VdbeExec(v);
102689 }else{
102690 rc = sqlite3_step(p->pStmt);
102691 }
102692 if( rc==SQLITE_ROW100 ){
102693 VdbeCursor *pC = v->apCsr[0];
102694 u32 type;
102695 assert( pC!=0 )((void) (0));
102696 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
102697 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
102698 testcase( pC->nHdrParsed==p->iCol );
102699 testcase( pC->nHdrParsed==p->iCol+1 );
102700 if( type<12 ){
102701 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
102702 type==0?"null": type==7?"real": "integer"
102703 );
102704 rc = SQLITE_ERROR1;
102705 sqlite3_finalize(p->pStmt);
102706 p->pStmt = 0;
102707 }else{
102708 p->iOffset = pC->aType[p->iCol + pC->nField];
102709 p->nByte = sqlite3VdbeSerialTypeLen(type);
102710 p->pCsr = pC->uc.pCursor;
102711 sqlite3BtreeIncrblobCursor(p->pCsr);
102712 }
102713 }
102714
102715 if( rc==SQLITE_ROW100 ){
102716 rc = SQLITE_OK0;
102717 }else if( p->pStmt ){
102718 rc = sqlite3_finalize(p->pStmt);
102719 p->pStmt = 0;
102720 if( rc==SQLITE_OK0 ){
102721 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
102722 rc = SQLITE_ERROR1;
102723 }else{
102724 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
102725 }
102726 }
102727
102728 assert( rc!=SQLITE_OK || zErr==0 )((void) (0));
102729 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE )((void) (0));
102730
102731 *pzErr = zErr;
102732 return rc;
102733}
102734
102735/*
102736** Open a blob handle.
102737*/
102738SQLITE_API int sqlite3_blob_open(
102739 sqlite3* db, /* The database connection */
102740 const char *zDb, /* The attached database containing the blob */
102741 const char *zTable, /* The table containing the blob */
102742 const char *zColumn, /* The column containing the blob */
102743 sqlite_int64 iRow, /* The row containing the glob */
102744 int wrFlag, /* True -> read/write access, false -> read-only */
102745 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
102746){
102747 int nAttempt = 0;
102748 int iCol; /* Index of zColumn in row-record */
102749 int rc = SQLITE_OK0;
102750 char *zErr = 0;
102751 Table *pTab;
102752 Incrblob *pBlob = 0;
102753 Parse sParse;
102754
102755#ifdef SQLITE_ENABLE_API_ARMOR1
102756 if( ppBlob==0 ){
102757 return SQLITE_MISUSE_BKPTsqlite3MisuseError(102757);
102758 }
102759#endif
102760 *ppBlob = 0;
102761#ifdef SQLITE_ENABLE_API_ARMOR1
102762 if( !sqlite3SafetyCheckOk(db) || zTable==0 || zColumn==0 ){
102763 return SQLITE_MISUSE_BKPTsqlite3MisuseError(102763);
102764 }
102765#endif
102766 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
102767
102768 sqlite3_mutex_enter(db->mutex);
102769
102770 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
102771 while(1){
102772 sqlite3ParseObjectInit(&sParse,db);
102773 if( !pBlob ) goto blob_open_out;
102774 sqlite3DbFree(db, zErr);
102775 zErr = 0;
102776
102777 sqlite3BtreeEnterAll(db);
102778 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
102779 if( pTab && IsVirtual(pTab)((pTab)->eTabType==1) ){
102780 pTab = 0;
102781 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
102782 }
102783 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
102784 pTab = 0;
102785 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
102786 }
102787 if( pTab && (pTab->tabFlags&TF_HasGenerated0x00000060)!=0 ){
102788 pTab = 0;
102789 sqlite3ErrorMsg(&sParse, "cannot open table with generated columns: %s",
102790 zTable);
102791 }
102792#ifndef SQLITE_OMIT_VIEW
102793 if( pTab && IsView(pTab)((pTab)->eTabType==2) ){
102794 pTab = 0;
102795 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
102796 }
102797#endif
102798 if( !pTab ){
102799 if( sParse.zErrMsg ){
102800 sqlite3DbFree(db, zErr);
102801 zErr = sParse.zErrMsg;
102802 sParse.zErrMsg = 0;
102803 }
102804 rc = SQLITE_ERROR1;
102805 sqlite3BtreeLeaveAll(db);
102806 goto blob_open_out;
102807 }
102808 pBlob->pTab = pTab;
102809 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
102810
102811 /* Now search pTab for the exact column. */
102812 for(iCol=0; iCol<pTab->nCol; iCol++) {
102813 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
102814 break;
102815 }
102816 }
102817 if( iCol==pTab->nCol ){
102818 sqlite3DbFree(db, zErr);
102819 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
102820 rc = SQLITE_ERROR1;
102821 sqlite3BtreeLeaveAll(db);
102822 goto blob_open_out;
102823 }
102824
102825 /* If the value is being opened for writing, check that the
102826 ** column is not indexed, and that it is not part of a foreign key.
102827 */
102828 if( wrFlag ){
102829 const char *zFault = 0;
102830 Index *pIdx;
102831#ifndef SQLITE_OMIT_FOREIGN_KEY
102832 if( db->flags&SQLITE_ForeignKeys0x00004000 ){
102833 /* Check that the column is not part of an FK child key definition. It
102834 ** is not necessary to check if it is part of a parent key, as parent
102835 ** key columns must be indexed. The check below will pick up this
102836 ** case. */
102837 FKey *pFKey;
102838 assert( IsOrdinaryTable(pTab) )((void) (0));
102839 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
102840 int j;
102841 for(j=0; j<pFKey->nCol; j++){
102842 if( pFKey->aCol[j].iFrom==iCol ){
102843 zFault = "foreign key";
102844 }
102845 }
102846 }
102847 }
102848#endif
102849 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102850 int j;
102851 for(j=0; j<pIdx->nKeyCol; j++){
102852 /* FIXME: Be smarter about indexes that use expressions */
102853 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR(-2) ){
102854 zFault = "indexed";
102855 }
102856 }
102857 }
102858 if( zFault ){
102859 sqlite3DbFree(db, zErr);
102860 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
102861 rc = SQLITE_ERROR1;
102862 sqlite3BtreeLeaveAll(db);
102863 goto blob_open_out;
102864 }
102865 }
102866
102867 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
102868 assert( pBlob->pStmt || db->mallocFailed )((void) (0));
102869 if( pBlob->pStmt ){
102870
102871 /* This VDBE program seeks a btree cursor to the identified
102872 ** db/table/row entry. The reason for using a vdbe program instead
102873 ** of writing code to use the b-tree layer directly is that the
102874 ** vdbe program will take advantage of the various transaction,
102875 ** locking and error handling infrastructure built into the vdbe.
102876 **
102877 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
102878 ** Code external to the Vdbe then "borrows" the b-tree cursor and
102879 ** uses it to implement the blob_read(), blob_write() and
102880 ** blob_bytes() functions.
102881 **
102882 ** The sqlite3_blob_close() function finalizes the vdbe program,
102883 ** which closes the b-tree cursor and (possibly) commits the
102884 ** transaction.
102885 */
102886 static const int iLn = VDBE_OFFSET_LINENO(2)0;
102887 static const VdbeOpList openBlob[] = {
102888 {OP_TableLock169, 0, 0, 0}, /* 0: Acquire a read or write lock */
102889 {OP_OpenRead102, 0, 0, 0}, /* 1: Open a cursor */
102890 /* blobSeekToRow() will initialize r[1] to the desired rowid */
102891 {OP_NotExists31, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
102892 {OP_Column94, 0, 0, 1}, /* 3 */
102893 {OP_ResultRow84, 1, 0, 0}, /* 4 */
102894 {OP_Halt70, 0, 0, 0}, /* 5 */
102895 };
102896 Vdbe *v = (Vdbe *)pBlob->pStmt;
102897 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102898 VdbeOp *aOp;
102899
102900 sqlite3VdbeAddOp4Int(v, OP_Transaction2, iDb, wrFlag,
102901 pTab->pSchema->schema_cookie,
102902 pTab->pSchema->iGeneration);
102903 sqlite3VdbeChangeP5(v, 1);
102904 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed )((void) (0));
102905 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob)((int)(sizeof(openBlob)/sizeof(openBlob[0]))), openBlob, iLn);
102906
102907 /* Make sure a mutex is held on the table to be accessed */
102908 sqlite3VdbeUsesBtree(v, iDb);
102909
102910 if( db->mallocFailed==0 ){
102911 assert( aOp!=0 )((void) (0));
102912 /* Configure the OP_TableLock instruction */
102913#ifdef SQLITE_OMIT_SHARED_CACHE
102914 aOp[0].opcode = OP_Noop187;
102915#else
102916 aOp[0].p1 = iDb;
102917 aOp[0].p2 = pTab->tnum;
102918 aOp[0].p3 = wrFlag;
102919 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT0);
102920 }
102921 if( db->mallocFailed==0 ){
102922#endif
102923
102924 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
102925 ** parameter of the other to pTab->tnum. */
102926 if( wrFlag ) aOp[1].opcode = OP_OpenWrite113;
102927 aOp[1].p2 = pTab->tnum;
102928 aOp[1].p3 = iDb;
102929
102930 /* Configure the number of columns. Configure the cursor to
102931 ** think that the table has one more column than it really
102932 ** does. An OP_Column to retrieve this imaginary column will
102933 ** always return an SQL NULL. This is useful because it means
102934 ** we can invoke OP_Column to fill in the vdbe cursors type
102935 ** and offset cache without causing any IO.
102936 */
102937 aOp[1].p4type = P4_INT32(-3);
102938 aOp[1].p4.i = pTab->nCol+1;
102939 aOp[3].p2 = pTab->nCol;
102940
102941 sParse.nVar = 0;
102942 sParse.nMem = 1;
102943 sParse.nTab = 1;
102944 sqlite3VdbeMakeReady(v, &sParse);
102945 }
102946 }
102947
102948 pBlob->iCol = iCol;
102949 pBlob->db = db;
102950 sqlite3BtreeLeaveAll(db);
102951 if( db->mallocFailed ){
102952 goto blob_open_out;
102953 }
102954 rc = blobSeekToRow(pBlob, iRow, &zErr);
102955 if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY50 || rc!=SQLITE_SCHEMA17 ) break;
102956 sqlite3ParseObjectReset(&sParse);
102957 }
102958
102959blob_open_out:
102960 if( rc==SQLITE_OK0 && db->mallocFailed==0 ){
102961 *ppBlob = (sqlite3_blob *)pBlob;
102962 }else{
102963 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
102964 sqlite3DbFree(db, pBlob);
102965 }
102966 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr);
102967 sqlite3DbFree(db, zErr);
102968 sqlite3ParseObjectReset(&sParse);
102969 rc = sqlite3ApiExit(db, rc);
102970 sqlite3_mutex_leave(db->mutex);
102971 return rc;
102972}
102973
102974/*
102975** Close a blob handle that was previously created using
102976** sqlite3_blob_open().
102977*/
102978SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
102979 Incrblob *p = (Incrblob *)pBlob;
102980 int rc;
102981 sqlite3 *db;
102982
102983 if( p ){
102984 sqlite3_stmt *pStmt = p->pStmt;
102985 db = p->db;
102986 sqlite3_mutex_enter(db->mutex);
102987 sqlite3DbFree(db, p);
102988 sqlite3_mutex_leave(db->mutex);
102989 rc = sqlite3_finalize(pStmt);
102990 }else{
102991 rc = SQLITE_OK0;
102992 }
102993 return rc;
102994}
102995
102996/*
102997** Perform a read or write operation on a blob
102998*/
102999static int blobReadWrite(
103000 sqlite3_blob *pBlob,
103001 void *z,
103002 int n,
103003 int iOffset,
103004 int (*xCall)(BtCursor*, u32, u32, void*)
103005){
103006 int rc;
103007 Incrblob *p = (Incrblob *)pBlob;
103008 Vdbe *v;
103009 sqlite3 *db;
103010
103011 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(103011);
103012 db = p->db;
103013 sqlite3_mutex_enter(db->mutex);
103014 v = (Vdbe*)p->pStmt;
103015
103016 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
103017 /* Request is out of range. Return a transient error. */
103018 rc = SQLITE_ERROR1;
103019 }else if( v==0 ){
103020 /* If there is no statement handle, then the blob-handle has
103021 ** already been invalidated. Return SQLITE_ABORT in this case.
103022 */
103023 rc = SQLITE_ABORT4;
103024 }else{
103025 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
103026 ** returned, clean-up the statement handle.
103027 */
103028 assert( db == v->db )((void) (0));
103029 sqlite3BtreeEnterCursor(p->pCsr);
103030
103031#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
103032 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
103033 /* If a pre-update hook is registered and this is a write cursor,
103034 ** invoke it here.
103035 **
103036 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
103037 ** operation should really be an SQLITE_UPDATE. This is probably
103038 ** incorrect, but is convenient because at this point the new.* values
103039 ** are not easily obtainable. And for the sessions module, an
103040 ** SQLITE_UPDATE where the PK columns do not change is handled in the
103041 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
103042 ** slightly more efficient). Since you cannot write to a PK column
103043 ** using the incremental-blob API, this works. For the sessions module
103044 ** anyhow.
103045 */
103046 sqlite3_int64 iKey;
103047 iKey = sqlite3BtreeIntegerKey(p->pCsr);
103048 assert( v->apCsr[0]!=0 )((void) (0));
103049 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE )((void) (0));
103050 sqlite3VdbePreUpdateHook(
103051 v, v->apCsr[0], SQLITE_DELETE9, p->zDb, p->pTab, iKey, -1, p->iCol
103052 );
103053 }
103054#endif
103055
103056 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
103057 sqlite3BtreeLeaveCursor(p->pCsr);
103058 if( rc==SQLITE_ABORT4 ){
103059 sqlite3VdbeFinalize(v);
103060 p->pStmt = 0;
103061 }else{
103062 v->rc = rc;
103063 }
103064 }
103065 sqlite3Error(db, rc);
103066 rc = sqlite3ApiExit(db, rc);
103067 sqlite3_mutex_leave(db->mutex);
103068 return rc;
103069}
103070
103071/*
103072** Read data from a blob handle.
103073*/
103074SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
103075 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
103076}
103077
103078/*
103079** Write data to a blob handle.
103080*/
103081SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
103082 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
103083}
103084
103085/*
103086** Query a blob handle for the size of the data.
103087**
103088** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
103089** so no mutex is required for access.
103090*/
103091SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
103092 Incrblob *p = (Incrblob *)pBlob;
103093 return (p && p->pStmt) ? p->nByte : 0;
103094}
103095
103096/*
103097** Move an existing blob handle to point to a different row of the same
103098** database table.
103099**
103100** If an error occurs, or if the specified row does not exist or does not
103101** contain a blob or text value, then an error code is returned and the
103102** database handle error code and message set. If this happens, then all
103103** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
103104** immediately return SQLITE_ABORT.
103105*/
103106SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
103107 int rc;
103108 Incrblob *p = (Incrblob *)pBlob;
103109 sqlite3 *db;
103110
103111 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(103111);
103112 db = p->db;
103113 sqlite3_mutex_enter(db->mutex);
103114
103115 if( p->pStmt==0 ){
103116 /* If there is no statement handle, then the blob-handle has
103117 ** already been invalidated. Return SQLITE_ABORT in this case.
103118 */
103119 rc = SQLITE_ABORT4;
103120 }else{
103121 char *zErr;
103122 ((Vdbe*)p->pStmt)->rc = SQLITE_OK0;
103123 rc = blobSeekToRow(p, iRow, &zErr);
103124 if( rc!=SQLITE_OK0 ){
103125 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr);
103126 sqlite3DbFree(db, zErr);
103127 }
103128 assert( rc!=SQLITE_SCHEMA )((void) (0));
103129 }
103130
103131 rc = sqlite3ApiExit(db, rc);
103132 assert( rc==SQLITE_OK || p->pStmt==0 )((void) (0));
103133 sqlite3_mutex_leave(db->mutex);
103134 return rc;
103135}
103136
103137#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
103138
103139/************** End of vdbeblob.c ********************************************/
103140/************** Begin file vdbesort.c ****************************************/
103141/*
103142** 2011-07-09
103143**
103144** The author disclaims copyright to this source code. In place of
103145** a legal notice, here is a blessing:
103146**
103147** May you do good and not evil.
103148** May you find forgiveness for yourself and forgive others.
103149** May you share freely, never taking more than you give.
103150**
103151*************************************************************************
103152** This file contains code for the VdbeSorter object, used in concert with
103153** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
103154** or by SELECT statements with ORDER BY clauses that cannot be satisfied
103155** using indexes and without LIMIT clauses.
103156**
103157** The VdbeSorter object implements a multi-threaded external merge sort
103158** algorithm that is efficient even if the number of elements being sorted
103159** exceeds the available memory.
103160**
103161** Here is the (internal, non-API) interface between this module and the
103162** rest of the SQLite system:
103163**
103164** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
103165**
103166** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
103167** object. The row is a binary blob in the
103168** OP_MakeRecord format that contains both
103169** the ORDER BY key columns and result columns
103170** in the case of a SELECT w/ ORDER BY, or
103171** the complete record for an index entry
103172** in the case of a CREATE INDEX.
103173**
103174** sqlite3VdbeSorterRewind() Sort all content previously added.
103175** Position the read cursor on the
103176** first sorted element.
103177**
103178** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
103179** element.
103180**
103181** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
103182** row currently under the read cursor.
103183**
103184** sqlite3VdbeSorterCompare() Compare the binary blob for the row
103185** currently under the read cursor against
103186** another binary blob X and report if
103187** X is strictly less than the read cursor.
103188** Used to enforce uniqueness in a
103189** CREATE UNIQUE INDEX statement.
103190**
103191** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
103192** all resources.
103193**
103194** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
103195** is like Close() followed by Init() only
103196** much faster.
103197**
103198** The interfaces above must be called in a particular order. Write() can
103199** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
103200** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
103201**
103202** Init()
103203** for each record: Write()
103204** Rewind()
103205** Rowkey()/Compare()
103206** Next()
103207** Close()
103208**
103209** Algorithm:
103210**
103211** Records passed to the sorter via calls to Write() are initially held
103212** unsorted in main memory. Assuming the amount of memory used never exceeds
103213** a threshold, when Rewind() is called the set of records is sorted using
103214** an in-memory merge sort. In this case, no temporary files are required
103215** and subsequent calls to Rowkey(), Next() and Compare() read records
103216** directly from main memory.
103217**
103218** If the amount of space used to store records in main memory exceeds the
103219** threshold, then the set of records currently in memory are sorted and
103220** written to a temporary file in "Packed Memory Array" (PMA) format.
103221** A PMA created at this point is known as a "level-0 PMA". Higher levels
103222** of PMAs may be created by merging existing PMAs together - for example
103223** merging two or more level-0 PMAs together creates a level-1 PMA.
103224**
103225** The threshold for the amount of main memory to use before flushing
103226** records to a PMA is roughly the same as the limit configured for the
103227** page-cache of the main database. Specifically, the threshold is set to
103228** the value returned by "PRAGMA main.page_size" multiplied by
103229** that returned by "PRAGMA main.cache_size", in bytes.
103230**
103231** If the sorter is running in single-threaded mode, then all PMAs generated
103232** are appended to a single temporary file. Or, if the sorter is running in
103233** multi-threaded mode then up to (N+1) temporary files may be opened, where
103234** N is the configured number of worker threads. In this case, instead of
103235** sorting the records and writing the PMA to a temporary file itself, the
103236** calling thread usually launches a worker thread to do so. Except, if
103237** there are already N worker threads running, the main thread does the work
103238** itself.
103239**
103240** The sorter is running in multi-threaded mode if (a) the library was built
103241** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
103242** than zero, and (b) worker threads have been enabled at runtime by calling
103243** "PRAGMA threads=N" with some value of N greater than 0.
103244**
103245** When Rewind() is called, any data remaining in memory is flushed to a
103246** final PMA. So at this point the data is stored in some number of sorted
103247** PMAs within temporary files on disk.
103248**
103249** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
103250** sorter is running in single-threaded mode, then these PMAs are merged
103251** incrementally as keys are retrieved from the sorter by the VDBE. The
103252** MergeEngine object, described in further detail below, performs this
103253** merge.
103254**
103255** Or, if running in multi-threaded mode, then a background thread is
103256** launched to merge the existing PMAs. Once the background thread has
103257** merged T bytes of data into a single sorted PMA, the main thread
103258** begins reading keys from that PMA while the background thread proceeds
103259** with merging the next T bytes of data. And so on.
103260**
103261** Parameter T is set to half the value of the memory threshold used
103262** by Write() above to determine when to create a new PMA.
103263**
103264** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
103265** Rewind() is called, then a hierarchy of incremental-merges is used.
103266** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
103267** disk are merged together. Then T bytes of data from the second set, and
103268** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
103269** PMAs at a time. This done is to improve locality.
103270**
103271** If running in multi-threaded mode and there are more than
103272** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
103273** than one background thread may be created. Specifically, there may be
103274** one background thread for each temporary file on disk, and one background
103275** thread to merge the output of each of the others to a single PMA for
103276** the main thread to read from.
103277*/
103278/* #include "sqliteInt.h" */
103279/* #include "vdbeInt.h" */
103280
103281/*
103282** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
103283** messages to stderr that may be helpful in understanding the performance
103284** characteristics of the sorter in multi-threaded mode.
103285*/
103286#if 0
103287# define SQLITE_DEBUG_SORTER_THREADS 1
103288#endif
103289
103290/*
103291** Hard-coded maximum amount of data to accumulate in memory before flushing
103292** to a level 0 PMA. The purpose of this limit is to prevent various integer
103293** overflows. 512MiB.
103294*/
103295#define SQLITE_MAX_PMASZ(1<<29) (1<<29)
103296
103297/*
103298** Private objects used by the sorter
103299*/
103300typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
103301typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
103302typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
103303typedef struct SorterRecord SorterRecord; /* A record being sorted */
103304typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
103305typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
103306typedef struct SorterList SorterList; /* In-memory list of records */
103307typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
103308
103309/*
103310** A container for a temp file handle and the current amount of data
103311** stored in the file.
103312*/
103313struct SorterFile {
103314 sqlite3_file *pFd; /* File handle */
103315 i64 iEof; /* Bytes of data stored in pFd */
103316};
103317
103318/*
103319** An in-memory list of objects to be sorted.
103320**
103321** If aMemory==0 then each object is allocated separately and the objects
103322** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
103323** are stored in the aMemory[] bulk memory, one right after the other, and
103324** are connected using SorterRecord.u.iNext.
103325*/
103326struct SorterList {
103327 SorterRecord *pList; /* Linked list of records */
103328 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
103329 i64 szPMA; /* Size of pList as PMA in bytes */
103330};
103331
103332/*
103333** The MergeEngine object is used to combine two or more smaller PMAs into
103334** one big PMA using a merge operation. Separate PMAs all need to be
103335** combined into one big PMA in order to be able to step through the sorted
103336** records in order.
103337**
103338** The aReadr[] array contains a PmaReader object for each of the PMAs being
103339** merged. An aReadr[] object either points to a valid key or else is at EOF.
103340** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
103341** For the purposes of the paragraphs below, we assume that the array is
103342** actually N elements in size, where N is the smallest power of 2 greater
103343** to or equal to the number of PMAs being merged. The extra aReadr[] elements
103344** are treated as if they are empty (always at EOF).
103345**
103346** The aTree[] array is also N elements in size. The value of N is stored in
103347** the MergeEngine.nTree variable.
103348**
103349** The final (N/2) elements of aTree[] contain the results of comparing
103350** pairs of PMA keys together. Element i contains the result of
103351** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
103352** aTree element is set to the index of it.
103353**
103354** For the purposes of this comparison, EOF is considered greater than any
103355** other key value. If the keys are equal (only possible with two EOF
103356** values), it doesn't matter which index is stored.
103357**
103358** The (N/4) elements of aTree[] that precede the final (N/2) described
103359** above contains the index of the smallest of each block of 4 PmaReaders
103360** And so on. So that aTree[1] contains the index of the PmaReader that
103361** currently points to the smallest key value. aTree[0] is unused.
103362**
103363** Example:
103364**
103365** aReadr[0] -> Banana
103366** aReadr[1] -> Feijoa
103367** aReadr[2] -> Elderberry
103368** aReadr[3] -> Currant
103369** aReadr[4] -> Grapefruit
103370** aReadr[5] -> Apple
103371** aReadr[6] -> Durian
103372** aReadr[7] -> EOF
103373**
103374** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
103375**
103376** The current element is "Apple" (the value of the key indicated by
103377** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
103378** be advanced to the next key in its segment. Say the next key is
103379** "Eggplant":
103380**
103381** aReadr[5] -> Eggplant
103382**
103383** The contents of aTree[] are updated first by comparing the new PmaReader
103384** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
103385** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
103386** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
103387** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
103388** so the value written into element 1 of the array is 0. As follows:
103389**
103390** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
103391**
103392** In other words, each time we advance to the next sorter element, log2(N)
103393** key comparison operations are required, where N is the number of segments
103394** being merged (rounded up to the next power of 2).
103395*/
103396struct MergeEngine {
103397 int nTree; /* Used size of aTree/aReadr (power of 2) */
103398 SortSubtask *pTask; /* Used by this thread only */
103399 int *aTree; /* Current state of incremental merge */
103400 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
103401};
103402
103403/*
103404** This object represents a single thread of control in a sort operation.
103405** Exactly VdbeSorter.nTask instances of this object are allocated
103406** as part of each VdbeSorter object. Instances are never allocated any
103407** other way. VdbeSorter.nTask is set to the number of worker threads allowed
103408** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
103409** single-threaded operation, there is exactly one instance of this object
103410** and for multi-threaded operation there are two or more instances.
103411**
103412** Essentially, this structure contains all those fields of the VdbeSorter
103413** structure for which each thread requires a separate instance. For example,
103414** each thread requeries its own UnpackedRecord object to unpack records in
103415** as part of comparison operations.
103416**
103417** Before a background thread is launched, variable bDone is set to 0. Then,
103418** right before it exits, the thread itself sets bDone to 1. This is used for
103419** two purposes:
103420**
103421** 1. When flushing the contents of memory to a level-0 PMA on disk, to
103422** attempt to select a SortSubtask for which there is not already an
103423** active background thread (since doing so causes the main thread
103424** to block until it finishes).
103425**
103426** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
103427** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
103428** block provoke debugging output.
103429**
103430** In both cases, the effects of the main thread seeing (bDone==0) even
103431** after the thread has finished are not dire. So we don't worry about
103432** memory barriers and such here.
103433*/
103434typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
103435struct SortSubtask {
103436 SQLiteThread *pThread; /* Background thread, if any */
103437 int bDone; /* Set if thread is finished but not joined */
103438 int nPMA; /* Number of PMAs currently in file */
103439 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
103440 UnpackedRecord *pUnpacked; /* Space to unpack a record */
103441 SorterList list; /* List for thread to write to a PMA */
103442 SorterCompare xCompare; /* Compare function to use */
103443 SorterFile file; /* Temp file for level-0 PMAs */
103444 SorterFile file2; /* Space for other PMAs */
103445};
103446
103447
103448/*
103449** Main sorter structure. A single instance of this is allocated for each
103450** sorter cursor created by the VDBE.
103451**
103452** mxKeysize:
103453** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
103454** this variable is updated so as to be set to the size on disk of the
103455** largest record in the sorter.
103456*/
103457struct VdbeSorter {
103458 int mnPmaSize; /* Minimum PMA size, in bytes */
103459 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
103460 int mxKeysize; /* Largest serialized key seen so far */
103461 int pgsz; /* Main database page size */
103462 PmaReader *pReader; /* Readr data from here after Rewind() */
103463 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
103464 sqlite3 *db; /* Database connection */
103465 KeyInfo *pKeyInfo; /* How to compare records */
103466 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
103467 SorterList list; /* List of in-memory records */
103468 int iMemory; /* Offset of free space in list.aMemory */
103469 int nMemory; /* Size of list.aMemory allocation in bytes */
103470 u8 bUsePMA; /* True if one or more PMAs created */
103471 u8 bUseThreads; /* True to use background threads */
103472 u8 iPrev; /* Previous thread used to flush PMA */
103473 u8 nTask; /* Size of aTask[] array */
103474 u8 typeMask;
103475 SortSubtask aTask[1]; /* One or more subtasks */
103476};
103477
103478#define SORTER_TYPE_INTEGER0x01 0x01
103479#define SORTER_TYPE_TEXT0x02 0x02
103480
103481/*
103482** An instance of the following object is used to read records out of a
103483** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
103484** aKey might point into aMap or into aBuffer. If neither of those locations
103485** contain a contiguous representation of the key, then aAlloc is allocated
103486** and the key is copied into aAlloc and aKey is made to point to aAlloc.
103487**
103488** pFd==0 at EOF.
103489*/
103490struct PmaReader {
103491 i64 iReadOff; /* Current read offset */
103492 i64 iEof; /* 1 byte past EOF for this PmaReader */
103493 int nAlloc; /* Bytes of space at aAlloc */
103494 int nKey; /* Number of bytes in key */
103495 sqlite3_file *pFd; /* File handle we are reading from */
103496 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
103497 u8 *aKey; /* Pointer to current key */
103498 u8 *aBuffer; /* Current read buffer */
103499 int nBuffer; /* Size of read buffer in bytes */
103500 u8 *aMap; /* Pointer to mapping of entire file */
103501 IncrMerger *pIncr; /* Incremental merger */
103502};
103503
103504/*
103505** Normally, a PmaReader object iterates through an existing PMA stored
103506** within a temp file. However, if the PmaReader.pIncr variable points to
103507** an object of the following type, it may be used to iterate/merge through
103508** multiple PMAs simultaneously.
103509**
103510** There are two types of IncrMerger object - single (bUseThread==0) and
103511** multi-threaded (bUseThread==1).
103512**
103513** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
103514** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
103515** size. When the IncrMerger is initialized, it reads enough data from
103516** pMerger to populate aFile[0]. It then sets variables within the
103517** corresponding PmaReader object to read from that file and kicks off
103518** a background thread to populate aFile[1] with the next mxSz bytes of
103519** sorted record data from pMerger.
103520**
103521** When the PmaReader reaches the end of aFile[0], it blocks until the
103522** background thread has finished populating aFile[1]. It then exchanges
103523** the contents of the aFile[0] and aFile[1] variables within this structure,
103524** sets the PmaReader fields to read from the new aFile[0] and kicks off
103525** another background thread to populate the new aFile[1]. And so on, until
103526** the contents of pMerger are exhausted.
103527**
103528** A single-threaded IncrMerger does not open any temporary files of its
103529** own. Instead, it has exclusive access to mxSz bytes of space beginning
103530** at offset iStartOff of file pTask->file2. And instead of using a
103531** background thread to prepare data for the PmaReader, with a single
103532** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
103533** keys from pMerger by the calling thread whenever the PmaReader runs out
103534** of data.
103535*/
103536struct IncrMerger {
103537 SortSubtask *pTask; /* Task that owns this merger */
103538 MergeEngine *pMerger; /* Merge engine thread reads data from */
103539 i64 iStartOff; /* Offset to start writing file at */
103540 int mxSz; /* Maximum bytes of data to store */
103541 int bEof; /* Set to true when merge is finished */
103542 int bUseThread; /* True to use a bg thread for this object */
103543 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
103544};
103545
103546/*
103547** An instance of this object is used for writing a PMA.
103548**
103549** The PMA is written one record at a time. Each record is of an arbitrary
103550** size. But I/O is more efficient if it occurs in page-sized blocks where
103551** each block is aligned on a page boundary. This object caches writes to
103552** the PMA so that aligned, page-size blocks are written.
103553*/
103554struct PmaWriter {
103555 int eFWErr; /* Non-zero if in an error state */
103556 u8 *aBuffer; /* Pointer to write buffer */
103557 int nBuffer; /* Size of write buffer in bytes */
103558 int iBufStart; /* First byte of buffer to write */
103559 int iBufEnd; /* Last byte of buffer to write */
103560 i64 iWriteOff; /* Offset of start of buffer in file */
103561 sqlite3_file *pFd; /* File handle to write to */
103562};
103563
103564/*
103565** This object is the header on a single record while that record is being
103566** held in memory and prior to being written out as part of a PMA.
103567**
103568** How the linked list is connected depends on how memory is being managed
103569** by this module. If using a separate allocation for each in-memory record
103570** (VdbeSorter.list.aMemory==0), then the list is always connected using the
103571** SorterRecord.u.pNext pointers.
103572**
103573** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
103574** then while records are being accumulated the list is linked using the
103575** SorterRecord.u.iNext offset. This is because the aMemory[] array may
103576** be sqlite3Realloc()ed while records are being accumulated. Once the VM
103577** has finished passing records to the sorter, or when the in-memory buffer
103578** is full, the list is sorted. As part of the sorting process, it is
103579** converted to use the SorterRecord.u.pNext pointers. See function
103580** vdbeSorterSort() for details.
103581*/
103582struct SorterRecord {
103583 int nVal; /* Size of the record in bytes */
103584 union {
103585 SorterRecord *pNext; /* Pointer to next record in list */
103586 int iNext; /* Offset within aMemory of next record */
103587 } u;
103588 /* The data for the record immediately follows this header */
103589};
103590
103591/* Return a pointer to the buffer containing the record data for SorterRecord
103592** object p. Should be used as if:
103593**
103594** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
103595*/
103596#define SRVAL(p)((void*)((SorterRecord*)(p) + 1)) ((void*)((SorterRecord*)(p) + 1))
103597
103598
103599/* Maximum number of PMAs that a single MergeEngine can merge */
103600#define SORTER_MAX_MERGE_COUNT16 16
103601
103602static int vdbeIncrSwap(IncrMerger*);
103603static void vdbeIncrFree(IncrMerger *);
103604
103605/*
103606** Free all memory belonging to the PmaReader object passed as the
103607** argument. All structure fields are set to zero before returning.
103608*/
103609static void vdbePmaReaderClear(PmaReader *pReadr){
103610 sqlite3_free(pReadr->aAlloc);
103611 sqlite3_free(pReadr->aBuffer);
103612 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
103613 vdbeIncrFree(pReadr->pIncr);
103614 memset(pReadr, 0, sizeof(PmaReader));
103615}
103616
103617/*
103618** Read the next nByte bytes of data from the PMA p.
103619** If successful, set *ppOut to point to a buffer containing the data
103620** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
103621** error code.
103622**
103623** The buffer returned in *ppOut is only valid until the
103624** next call to this function.
103625*/
103626static int vdbePmaReadBlob(
103627 PmaReader *p, /* PmaReader from which to take the blob */
103628 int nByte, /* Bytes of data to read */
103629 u8 **ppOut /* OUT: Pointer to buffer containing data */
103630){
103631 int iBuf; /* Offset within buffer to read from */
103632 int nAvail; /* Bytes of data available in buffer */
103633
103634 if( p->aMap ){
103635 *ppOut = &p->aMap[p->iReadOff];
103636 p->iReadOff += nByte;
103637 return SQLITE_OK0;
103638 }
103639
103640 assert( p->aBuffer )((void) (0));
103641
103642 /* If there is no more data to be read from the buffer, read the next
103643 ** p->nBuffer bytes of data from the file into it. Or, if there are less
103644 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
103645 iBuf = p->iReadOff % p->nBuffer;
103646 if( iBuf==0 ){
103647 int nRead; /* Bytes to read from disk */
103648 int rc; /* sqlite3OsRead() return code */
103649
103650 /* Determine how many bytes of data to read. */
103651 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
103652 nRead = p->nBuffer;
103653 }else{
103654 nRead = (int)(p->iEof - p->iReadOff);
103655 }
103656 assert( nRead>0 )((void) (0));
103657
103658 /* Readr data from the file. Return early if an error occurs. */
103659 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
103660 assert( rc!=SQLITE_IOERR_SHORT_READ )((void) (0));
103661 if( rc!=SQLITE_OK0 ) return rc;
103662 }
103663 nAvail = p->nBuffer - iBuf;
103664
103665 if( nByte<=nAvail ){
103666 /* The requested data is available in the in-memory buffer. In this
103667 ** case there is no need to make a copy of the data, just return a
103668 ** pointer into the buffer to the caller. */
103669 *ppOut = &p->aBuffer[iBuf];
103670 p->iReadOff += nByte;
103671 }else{
103672 /* The requested data is not all available in the in-memory buffer.
103673 ** In this case, allocate space at p->aAlloc[] to copy the requested
103674 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
103675 int nRem; /* Bytes remaining to copy */
103676
103677 /* Extend the p->aAlloc[] allocation if required. */
103678 if( p->nAlloc<nByte ){
103679 u8 *aNew;
103680 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc)((128)>(2*(sqlite3_int64)p->nAlloc)?(128):(2*(sqlite3_int64
)p->nAlloc))
;
103681 while( nByte>nNew ) nNew = nNew*2;
103682 aNew = sqlite3Realloc(p->aAlloc, nNew);
103683 if( !aNew ) return SQLITE_NOMEM_BKPT7;
103684 p->nAlloc = nNew;
103685 p->aAlloc = aNew;
103686 }
103687
103688 /* Copy as much data as is available in the buffer into the start of
103689 ** p->aAlloc[]. */
103690 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
103691 p->iReadOff += nAvail;
103692 nRem = nByte - nAvail;
103693
103694 /* The following loop copies up to p->nBuffer bytes per iteration into
103695 ** the p->aAlloc[] buffer. */
103696 while( nRem>0 ){
103697 int rc; /* vdbePmaReadBlob() return code */
103698 int nCopy; /* Number of bytes to copy */
103699 u8 *aNext = 0; /* Pointer to buffer to copy data from */
103700
103701 nCopy = nRem;
103702 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
103703 rc = vdbePmaReadBlob(p, nCopy, &aNext);
103704 if( rc!=SQLITE_OK0 ) return rc;
103705 assert( aNext!=p->aAlloc )((void) (0));
103706 assert( aNext!=0 )((void) (0));
103707 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
103708 nRem -= nCopy;
103709 }
103710
103711 *ppOut = p->aAlloc;
103712 }
103713
103714 return SQLITE_OK0;
103715}
103716
103717/*
103718** Read a varint from the stream of data accessed by p. Set *pnOut to
103719** the value read.
103720*/
103721static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
103722 int iBuf;
103723
103724 if( p->aMap ){
103725 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
103726 }else{
103727 iBuf = p->iReadOff % p->nBuffer;
103728 if( iBuf && (p->nBuffer-iBuf)>=9 ){
103729 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
103730 }else{
103731 u8 aVarint[16], *a;
103732 int i = 0, rc;
103733 do{
103734 rc = vdbePmaReadBlob(p, 1, &a);
103735 if( rc ) return rc;
103736 aVarint[(i++)&0xf] = a[0];
103737 }while( (a[0]&0x80)!=0 );
103738 sqlite3GetVarint(aVarint, pnOut);
103739 }
103740 }
103741
103742 return SQLITE_OK0;
103743}
103744
103745/*
103746** Attempt to memory map file pFile. If successful, set *pp to point to the
103747** new mapping and return SQLITE_OK. If the mapping is not attempted
103748** (because the file is too large or the VFS layer is configured not to use
103749** mmap), return SQLITE_OK and set *pp to NULL.
103750**
103751** Or, if an error occurs, return an SQLite error code. The final value of
103752** *pp is undefined in this case.
103753*/
103754static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
103755 int rc = SQLITE_OK0;
103756 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
103757 sqlite3_file *pFd = pFile->pFd;
103758 if( pFd->pMethods->iVersion>=3 ){
103759 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
103760 testcase( rc!=SQLITE_OK );
103761 }
103762 }
103763 return rc;
103764}
103765
103766/*
103767** Attach PmaReader pReadr to file pFile (if it is not already attached to
103768** that file) and seek it to offset iOff within the file. Return SQLITE_OK
103769** if successful, or an SQLite error code if an error occurs.
103770*/
103771static int vdbePmaReaderSeek(
103772 SortSubtask *pTask, /* Task context */
103773 PmaReader *pReadr, /* Reader whose cursor is to be moved */
103774 SorterFile *pFile, /* Sorter file to read from */
103775 i64 iOff /* Offset in pFile */
103776){
103777 int rc = SQLITE_OK0;
103778
103779 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 )((void) (0));
103780
103781 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ(10 | (1<<8));
103782 if( pReadr->aMap ){
103783 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
103784 pReadr->aMap = 0;
103785 }
103786 pReadr->iReadOff = iOff;
103787 pReadr->iEof = pFile->iEof;
103788 pReadr->pFd = pFile->pFd;
103789
103790 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
103791 if( rc==SQLITE_OK0 && pReadr->aMap==0 ){
103792 int pgsz = pTask->pSorter->pgsz;
103793 int iBuf = pReadr->iReadOff % pgsz;
103794 if( pReadr->aBuffer==0 ){
103795 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
103796 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT7;
103797 pReadr->nBuffer = pgsz;
103798 }
103799 if( rc==SQLITE_OK0 && iBuf ){
103800 int nRead = pgsz - iBuf;
103801 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
103802 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
103803 }
103804 rc = sqlite3OsRead(
103805 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
103806 );
103807 testcase( rc!=SQLITE_OK );
103808 }
103809 }
103810
103811 return rc;
103812}
103813
103814/*
103815** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
103816** no error occurs, or an SQLite error code if one does.
103817*/
103818static int vdbePmaReaderNext(PmaReader *pReadr){
103819 int rc = SQLITE_OK0; /* Return Code */
103820 u64 nRec = 0; /* Size of record in bytes */
103821
103822
103823 if( pReadr->iReadOff>=pReadr->iEof ){
103824 IncrMerger *pIncr = pReadr->pIncr;
103825 int bEof = 1;
103826 if( pIncr ){
103827 rc = vdbeIncrSwap(pIncr);
103828 if( rc==SQLITE_OK0 && pIncr->bEof==0 ){
103829 rc = vdbePmaReaderSeek(
103830 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
103831 );
103832 bEof = 0;
103833 }
103834 }
103835
103836 if( bEof ){
103837 /* This is an EOF condition */
103838 vdbePmaReaderClear(pReadr);
103839 testcase( rc!=SQLITE_OK );
103840 return rc;
103841 }
103842 }
103843
103844 if( rc==SQLITE_OK0 ){
103845 rc = vdbePmaReadVarint(pReadr, &nRec);
103846 }
103847 if( rc==SQLITE_OK0 ){
103848 pReadr->nKey = (int)nRec;
103849 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
103850 testcase( rc!=SQLITE_OK );
103851 }
103852
103853 return rc;
103854}
103855
103856/*
103857** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
103858** starting at offset iStart and ending at offset iEof-1. This function
103859** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
103860** PMA is empty).
103861**
103862** If the pnByte parameter is NULL, then it is assumed that the file
103863** contains a single PMA, and that that PMA omits the initial length varint.
103864*/
103865static int vdbePmaReaderInit(
103866 SortSubtask *pTask, /* Task context */
103867 SorterFile *pFile, /* Sorter file to read from */
103868 i64 iStart, /* Start offset in pFile */
103869 PmaReader *pReadr, /* PmaReader to populate */
103870 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
103871){
103872 int rc;
103873
103874 assert( pFile->iEof>iStart )((void) (0));
103875 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 )((void) (0));
103876 assert( pReadr->aBuffer==0 )((void) (0));
103877 assert( pReadr->aMap==0 )((void) (0));
103878
103879 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
103880 if( rc==SQLITE_OK0 ){
103881 u64 nByte = 0; /* Size of PMA in bytes */
103882 rc = vdbePmaReadVarint(pReadr, &nByte);
103883 pReadr->iEof = pReadr->iReadOff + nByte;
103884 *pnByte += nByte;
103885 }
103886
103887 if( rc==SQLITE_OK0 ){
103888 rc = vdbePmaReaderNext(pReadr);
103889 }
103890 return rc;
103891}
103892
103893/*
103894** A version of vdbeSorterCompare() that assumes that it has already been
103895** determined that the first field of key1 is equal to the first field of
103896** key2.
103897*/
103898static int vdbeSorterCompareTail(
103899 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
103900 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103901 const void *pKey1, int nKey1, /* Left side of comparison */
103902 const void *pKey2, int nKey2 /* Right side of comparison */
103903){
103904 UnpackedRecord *r2 = pTask->pUnpacked;
103905 if( *pbKey2Cached==0 ){
103906 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
103907 *pbKey2Cached = 1;
103908 }
103909 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
103910}
103911
103912/*
103913** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
103914** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
103915** used by the comparison. Return the result of the comparison.
103916**
103917** If IN/OUT parameter *pbKey2Cached is true when this function is called,
103918** it is assumed that (pTask->pUnpacked) contains the unpacked version
103919** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
103920** version of key2 and *pbKey2Cached set to true before returning.
103921**
103922** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
103923** to SQLITE_NOMEM.
103924*/
103925static int vdbeSorterCompare(
103926 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
103927 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103928 const void *pKey1, int nKey1, /* Left side of comparison */
103929 const void *pKey2, int nKey2 /* Right side of comparison */
103930){
103931 UnpackedRecord *r2 = pTask->pUnpacked;
103932 if( !*pbKey2Cached ){
103933 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
103934 *pbKey2Cached = 1;
103935 }
103936 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
103937}
103938
103939/*
103940** A specially optimized version of vdbeSorterCompare() that assumes that
103941** the first field of each key is a TEXT value and that the collation
103942** sequence to compare them with is BINARY.
103943*/
103944static int vdbeSorterCompareText(
103945 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
103946 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103947 const void *pKey1, int nKey1, /* Left side of comparison */
103948 const void *pKey2, int nKey2 /* Right side of comparison */
103949){
103950 const u8 * const p1 = (const u8 * const)pKey1;
103951 const u8 * const p2 = (const u8 * const)pKey2;
103952 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
103953 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
103954
103955 int n1;
103956 int n2;
103957 int res;
103958
103959 getVarint32NR(&p1[1], n1)n1=(u32)*(&p1[1]);if(n1>=0x80)sqlite3GetVarint32((&
p1[1]),(u32*)&(n1))
;
103960 getVarint32NR(&p2[1], n2)n2=(u32)*(&p2[1]);if(n2>=0x80)sqlite3GetVarint32((&
p2[1]),(u32*)&(n2))
;
103961 res = memcmp(v1, v2, (MIN(n1, n2)((n1)<(n2)?(n1):(n2)) - 13)/2);
103962 if( res==0 ){
103963 res = n1 - n2;
103964 }
103965
103966 if( res==0 ){
103967 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
103968 res = vdbeSorterCompareTail(
103969 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
103970 );
103971 }
103972 }else{
103973 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) )((void) (0));
103974 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
103975 res = res * -1;
103976 }
103977 }
103978
103979 return res;
103980}
103981
103982/*
103983** A specially optimized version of vdbeSorterCompare() that assumes that
103984** the first field of each key is an INTEGER value.
103985*/
103986static int vdbeSorterCompareInt(
103987 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
103988 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103989 const void *pKey1, int nKey1, /* Left side of comparison */
103990 const void *pKey2, int nKey2 /* Right side of comparison */
103991){
103992 const u8 * const p1 = (const u8 * const)pKey1;
103993 const u8 * const p2 = (const u8 * const)pKey2;
103994 const int s1 = p1[1]; /* Left hand serial type */
103995 const int s2 = p2[1]; /* Right hand serial type */
103996 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
103997 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
103998 int res; /* Return value */
103999
104000 assert( (s1>0 && s1<7) || s1==8 || s1==9 )((void) (0));
104001 assert( (s2>0 && s2<7) || s2==8 || s2==9 )((void) (0));
104002
104003 if( s1==s2 ){
104004 /* The two values have the same sign. Compare using memcmp(). */
104005 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
104006 const u8 n = aLen[s1];
104007 int i;
104008 res = 0;
104009 for(i=0; i<n; i++){
104010 if( (res = v1[i] - v2[i])!=0 ){
104011 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
104012 res = v1[0] & 0x80 ? -1 : +1;
104013 }
104014 break;
104015 }
104016 }
104017 }else if( s1>7 && s2>7 ){
104018 res = s1 - s2;
104019 }else{
104020 if( s2>7 ){
104021 res = +1;
104022 }else if( s1>7 ){
104023 res = -1;
104024 }else{
104025 res = s1 - s2;
104026 }
104027 assert( res!=0 )((void) (0));
104028
104029 if( res>0 ){
104030 if( *v1 & 0x80 ) res = -1;
104031 }else{
104032 if( *v2 & 0x80 ) res = +1;
104033 }
104034 }
104035
104036 if( res==0 ){
104037 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
104038 res = vdbeSorterCompareTail(
104039 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
104040 );
104041 }
104042 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
104043 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) )((void) (0));
104044 res = res * -1;
104045 }
104046
104047 return res;
104048}
104049
104050/*
104051** Initialize the temporary index cursor just opened as a sorter cursor.
104052**
104053** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
104054** to determine the number of fields that should be compared from the
104055** records being sorted. However, if the value passed as argument nField
104056** is non-zero and the sorter is able to guarantee a stable sort, nField
104057** is used instead. This is used when sorting records for a CREATE INDEX
104058** statement. In this case, keys are always delivered to the sorter in
104059** order of the primary key, which happens to be make up the final part
104060** of the records being sorted. So if the sort is stable, there is never
104061** any reason to compare PK fields and they can be ignored for a small
104062** performance boost.
104063**
104064** The sorter can guarantee a stable sort when running in single-threaded
104065** mode, but not in multi-threaded mode.
104066**
104067** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
104068*/
104069SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(
104070 sqlite3 *db, /* Database connection (for malloc()) */
104071 int nField, /* Number of key fields in each record */
104072 VdbeCursor *pCsr /* Cursor that holds the new sorter */
104073){
104074 int pgsz; /* Page size of main database */
104075 int i; /* Used to iterate through aTask[] */
104076 VdbeSorter *pSorter; /* The new sorter */
104077 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
104078 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
104079 int sz; /* Size of pSorter in bytes */
104080 int rc = SQLITE_OK0;
104081#if SQLITE_MAX_WORKER_THREADS8==0
104082# define nWorker 0
104083#else
104084 int nWorker;
104085#endif
104086
104087 /* Initialize the upper limit on the number of worker threads */
104088#if SQLITE_MAX_WORKER_THREADS8>0
104089 if( sqlite3TempInMemory(db) || sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
104090 nWorker = 0;
104091 }else{
104092 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS11];
104093 }
104094#endif
104095
104096 /* Do not allow the total number of threads (main thread + all workers)
104097 ** to exceed the maximum merge count */
104098#if SQLITE_MAX_WORKER_THREADS8>=SORTER_MAX_MERGE_COUNT16
104099 if( nWorker>=SORTER_MAX_MERGE_COUNT16 ){
104100 nWorker = SORTER_MAX_MERGE_COUNT16-1;
104101 }
104102#endif
104103
104104 assert( pCsr->pKeyInfo )((void) (0));
104105 assert( !pCsr->isEphemeral )((void) (0));
104106 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
104107 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
104108 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
104109
104110 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
104111 pCsr->uc.pSorter = pSorter;
104112 if( pSorter==0 ){
104113 rc = SQLITE_NOMEM_BKPT7;
104114 }else{
104115 Btree *pBt = db->aDb[0].pBt;
104116 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
104117 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
104118 pKeyInfo->db = 0;
104119 if( nField && nWorker==0 ){
104120 pKeyInfo->nKeyField = nField;
104121 }
104122 sqlite3BtreeEnter(pBt);
104123 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
104124 sqlite3BtreeLeave(pBt);
104125 pSorter->nTask = nWorker + 1;
104126 pSorter->iPrev = (u8)(nWorker - 1);
104127 pSorter->bUseThreads = (pSorter->nTask>1);
104128 pSorter->db = db;
104129 for(i=0; i<pSorter->nTask; i++){
104130 SortSubtask *pTask = &pSorter->aTask[i];
104131 pTask->pSorter = pSorter;
104132 }
104133
104134 if( !sqlite3TempInMemory(db) ){
104135 i64 mxCache; /* Cache size in bytes*/
104136 u32 szPma = sqlite3GlobalConfigsqlite3Config.szPma;
104137 pSorter->mnPmaSize = szPma * pgsz;
104138
104139 mxCache = db->aDb[0].pSchema->cache_size;
104140 if( mxCache<0 ){
104141 /* A negative cache-size value C indicates that the cache is abs(C)
104142 ** KiB in size. */
104143 mxCache = mxCache * -1024;
104144 }else{
104145 mxCache = mxCache * pgsz;
104146 }
104147 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ)((mxCache)<((1<<29))?(mxCache):((1<<29)));
104148 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache)((pSorter->mnPmaSize)>((int)mxCache)?(pSorter->mnPmaSize
):((int)mxCache))
;
104149
104150 /* Avoid large memory allocations if the application has requested
104151 ** SQLITE_CONFIG_SMALL_MALLOC. */
104152 if( sqlite3GlobalConfigsqlite3Config.bSmallMalloc==0 ){
104153 assert( pSorter->iMemory==0 )((void) (0));
104154 pSorter->nMemory = pgsz;
104155 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
104156 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT7;
104157 }
104158 }
104159
104160 if( pKeyInfo->nAllField<13
104161 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
104162 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL0x02)==0
104163 ){
104164 pSorter->typeMask = SORTER_TYPE_INTEGER0x01 | SORTER_TYPE_TEXT0x02;
104165 }
104166 }
104167
104168 return rc;
104169}
104170#undef nWorker /* Defined at the top of this function */
104171
104172/*
104173** Free the list of sorted records starting at pRecord.
104174*/
104175static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
104176 SorterRecord *p;
104177 SorterRecord *pNext;
104178 for(p=pRecord; p; p=pNext){
104179 pNext = p->u.pNext;
104180 sqlite3DbFree(db, p);
104181 }
104182}
104183
104184/*
104185** Free all resources owned by the object indicated by argument pTask. All
104186** fields of *pTask are zeroed before returning.
104187*/
104188static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
104189 sqlite3DbFree(db, pTask->pUnpacked);
104190#if SQLITE_MAX_WORKER_THREADS8>0
104191 /* pTask->list.aMemory can only be non-zero if it was handed memory
104192 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
104193 if( pTask->list.aMemory ){
104194 sqlite3_free(pTask->list.aMemory);
104195 }else
104196#endif
104197 {
104198 assert( pTask->list.aMemory==0 )((void) (0));
104199 vdbeSorterRecordFree(0, pTask->list.pList);
104200 }
104201 if( pTask->file.pFd ){
104202 sqlite3OsCloseFree(pTask->file.pFd);
104203 }
104204 if( pTask->file2.pFd ){
104205 sqlite3OsCloseFree(pTask->file2.pFd);
104206 }
104207 memset(pTask, 0, sizeof(SortSubtask));
104208}
104209
104210#ifdef SQLITE_DEBUG_SORTER_THREADS
104211static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
104212 i64 t;
104213 int iTask = (pTask - pTask->pSorter->aTask);
104214 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
104215 fprintf(stderrstderr, "%lld:%d %s\n", t, iTask, zEvent);
104216}
104217static void vdbeSorterRewindDebug(const char *zEvent){
104218 i64 t = 0;
104219 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
104220 if( ALWAYS(pVfs)(pVfs) ) sqlite3OsCurrentTimeInt64(pVfs, &t);
104221 fprintf(stderrstderr, "%lld:X %s\n", t, zEvent);
104222}
104223static void vdbeSorterPopulateDebug(
104224 SortSubtask *pTask,
104225 const char *zEvent
104226){
104227 i64 t;
104228 int iTask = (pTask - pTask->pSorter->aTask);
104229 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
104230 fprintf(stderrstderr, "%lld:bg%d %s\n", t, iTask, zEvent);
104231}
104232static void vdbeSorterBlockDebug(
104233 SortSubtask *pTask,
104234 int bBlocked,
104235 const char *zEvent
104236){
104237 if( bBlocked ){
104238 i64 t;
104239 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
104240 fprintf(stderrstderr, "%lld:main %s\n", t, zEvent);
104241 }
104242}
104243#else
104244# define vdbeSorterWorkDebug(x,y)
104245# define vdbeSorterRewindDebug(y)
104246# define vdbeSorterPopulateDebug(x,y)
104247# define vdbeSorterBlockDebug(x,y,z)
104248#endif
104249
104250#if SQLITE_MAX_WORKER_THREADS8>0
104251/*
104252** Join thread pTask->thread.
104253*/
104254static int vdbeSorterJoinThread(SortSubtask *pTask){
104255 int rc = SQLITE_OK0;
104256 if( pTask->pThread ){
104257#ifdef SQLITE_DEBUG_SORTER_THREADS
104258 int bDone = pTask->bDone;
104259#endif
104260 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR)((void*)(long int)(1));
104261 vdbeSorterBlockDebug(pTask, !bDone, "enter");
104262 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
104263 vdbeSorterBlockDebug(pTask, !bDone, "exit");
104264 rc = SQLITE_PTR_TO_INT(pRet)((int)(long int)(pRet));
104265 assert( pTask->bDone==1 )((void) (0));
104266 pTask->bDone = 0;
104267 pTask->pThread = 0;
104268 }
104269 return rc;
104270}
104271
104272/*
104273** Launch a background thread to run xTask(pIn).
104274*/
104275static int vdbeSorterCreateThread(
104276 SortSubtask *pTask, /* Thread will use this task object */
104277 void *(*xTask)(void*), /* Routine to run in a separate thread */
104278 void *pIn /* Argument passed into xTask() */
104279){
104280 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
104281 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
104282}
104283
104284/*
104285** Join all outstanding threads launched by SorterWrite() to create
104286** level-0 PMAs.
104287*/
104288static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
104289 int rc = rcin;
104290 int i;
104291
104292 /* This function is always called by the main user thread.
104293 **
104294 ** If this function is being called after SorterRewind() has been called,
104295 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
104296 ** is currently attempt to join one of the other threads. To avoid a race
104297 ** condition where this thread also attempts to join the same object, join
104298 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
104299 for(i=pSorter->nTask-1; i>=0; i--){
104300 SortSubtask *pTask = &pSorter->aTask[i];
104301 int rc2 = vdbeSorterJoinThread(pTask);
104302 if( rc==SQLITE_OK0 ) rc = rc2;
104303 }
104304 return rc;
104305}
104306#else
104307# define vdbeSorterJoinAll(x,rcin) (rcin)
104308# define vdbeSorterJoinThread(pTask) SQLITE_OK0
104309#endif
104310
104311/*
104312** Allocate a new MergeEngine object capable of handling up to
104313** nReader PmaReader inputs.
104314**
104315** nReader is automatically rounded up to the next power of two.
104316** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
104317*/
104318static MergeEngine *vdbeMergeEngineNew(int nReader){
104319 int N = 2; /* Smallest power of two >= nReader */
104320 int nByte; /* Total bytes of space to allocate */
104321 MergeEngine *pNew; /* Pointer to allocated object to return */
104322
104323 assert( nReader<=SORTER_MAX_MERGE_COUNT )((void) (0));
104324
104325 while( N<nReader ) N += N;
104326 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
104327
104328 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
104329 if( pNew ){
104330 pNew->nTree = N;
104331 pNew->pTask = 0;
104332 pNew->aReadr = (PmaReader*)&pNew[1];
104333 pNew->aTree = (int*)&pNew->aReadr[N];
104334 }
104335 return pNew;
104336}
104337
104338/*
104339** Free the MergeEngine object passed as the only argument.
104340*/
104341static void vdbeMergeEngineFree(MergeEngine *pMerger){
104342 int i;
104343 if( pMerger ){
104344 for(i=0; i<pMerger->nTree; i++){
104345 vdbePmaReaderClear(&pMerger->aReadr[i]);
104346 }
104347 }
104348 sqlite3_free(pMerger);
104349}
104350
104351/*
104352** Free all resources associated with the IncrMerger object indicated by
104353** the first argument.
104354*/
104355static void vdbeIncrFree(IncrMerger *pIncr){
104356 if( pIncr ){
104357#if SQLITE_MAX_WORKER_THREADS8>0
104358 if( pIncr->bUseThread ){
104359 vdbeSorterJoinThread(pIncr->pTask);
104360 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
104361 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
104362 }
104363#endif
104364 vdbeMergeEngineFree(pIncr->pMerger);
104365 sqlite3_free(pIncr);
104366 }
104367}
104368
104369/*
104370** Reset a sorting cursor back to its original empty state.
104371*/
104372SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
104373 int i;
104374 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK0);
104375 assert( pSorter->bUseThreads || pSorter->pReader==0 )((void) (0));
104376#if SQLITE_MAX_WORKER_THREADS8>0
104377 if( pSorter->pReader ){
104378 vdbePmaReaderClear(pSorter->pReader);
104379 sqlite3DbFree(db, pSorter->pReader);
104380 pSorter->pReader = 0;
104381 }
104382#endif
104383 vdbeMergeEngineFree(pSorter->pMerger);
104384 pSorter->pMerger = 0;
104385 for(i=0; i<pSorter->nTask; i++){
104386 SortSubtask *pTask = &pSorter->aTask[i];
104387 vdbeSortSubtaskCleanup(db, pTask);
104388 pTask->pSorter = pSorter;
104389 }
104390 if( pSorter->list.aMemory==0 ){
104391 vdbeSorterRecordFree(0, pSorter->list.pList);
104392 }
104393 pSorter->list.pList = 0;
104394 pSorter->list.szPMA = 0;
104395 pSorter->bUsePMA = 0;
104396 pSorter->iMemory = 0;
104397 pSorter->mxKeysize = 0;
104398 sqlite3DbFree(db, pSorter->pUnpacked);
104399 pSorter->pUnpacked = 0;
104400}
104401
104402/*
104403** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
104404*/
104405SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
104406 VdbeSorter *pSorter;
104407 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
104408 pSorter = pCsr->uc.pSorter;
104409 if( pSorter ){
104410 sqlite3VdbeSorterReset(db, pSorter);
104411 sqlite3_free(pSorter->list.aMemory);
104412 sqlite3DbFree(db, pSorter);
104413 pCsr->uc.pSorter = 0;
104414 }
104415}
104416
104417#if SQLITE_MAX_MMAP_SIZE20971520>0
104418/*
104419** The first argument is a file-handle open on a temporary file. The file
104420** is guaranteed to be nByte bytes or smaller in size. This function
104421** attempts to extend the file to nByte bytes in size and to ensure that
104422** the VFS has memory mapped it.
104423**
104424** Whether or not the file does end up memory mapped of course depends on
104425** the specific VFS implementation.
104426*/
104427static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
104428 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
104429 void *p = 0;
104430 int chunksize = 4*1024;
104431 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE6, &chunksize);
104432 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT5, &nByte);
104433 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
104434 if( p ) sqlite3OsUnfetch(pFd, 0, p);
104435 }
104436}
104437#else
104438# define vdbeSorterExtendFile(x,y,z)
104439#endif
104440
104441/*
104442** Allocate space for a file-handle and open a temporary file. If successful,
104443** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
104444** Otherwise, set *ppFd to 0 and return an SQLite error code.
104445*/
104446static int vdbeSorterOpenTempFile(
104447 sqlite3 *db, /* Database handle doing sort */
104448 i64 nExtend, /* Attempt to extend file to this size */
104449 sqlite3_file **ppFd
104450){
104451 int rc;
104452 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS(10 | (13<<8));
104453 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
104454 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
104455 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
104456 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008, &rc
104457 );
104458 if( rc==SQLITE_OK0 ){
104459 i64 max = SQLITE_MAX_MMAP_SIZE20971520;
104460 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE18, (void*)&max);
104461 if( nExtend>0 ){
104462 vdbeSorterExtendFile(db, *ppFd, nExtend);
104463 }
104464 }
104465 return rc;
104466}
104467
104468/*
104469** If it has not already been allocated, allocate the UnpackedRecord
104470** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
104471** if no allocation was required), or SQLITE_NOMEM otherwise.
104472*/
104473static int vdbeSortAllocUnpacked(SortSubtask *pTask){
104474 if( pTask->pUnpacked==0 ){
104475 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
104476 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT7;
104477 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
104478 pTask->pUnpacked->errCode = 0;
104479 }
104480 return SQLITE_OK0;
104481}
104482
104483
104484/*
104485** Merge the two sorted lists p1 and p2 into a single list.
104486*/
104487static SorterRecord *vdbeSorterMerge(
104488 SortSubtask *pTask, /* Calling thread context */
104489 SorterRecord *p1, /* First list to merge */
104490 SorterRecord *p2 /* Second list to merge */
104491){
104492 SorterRecord *pFinal = 0;
104493 SorterRecord **pp = &pFinal;
104494 int bCached = 0;
104495
104496 assert( p1!=0 && p2!=0 )((void) (0));
104497 for(;;){
104498 int res;
104499 res = pTask->xCompare(
104500 pTask, &bCached, SRVAL(p1)((void*)((SorterRecord*)(p1) + 1)), p1->nVal, SRVAL(p2)((void*)((SorterRecord*)(p2) + 1)), p2->nVal
104501 );
104502
104503 if( res<=0 ){
104504 *pp = p1;
104505 pp = &p1->u.pNext;
104506 p1 = p1->u.pNext;
104507 if( p1==0 ){
104508 *pp = p2;
104509 break;
104510 }
104511 }else{
104512 *pp = p2;
104513 pp = &p2->u.pNext;
104514 p2 = p2->u.pNext;
104515 bCached = 0;
104516 if( p2==0 ){
104517 *pp = p1;
104518 break;
104519 }
104520 }
104521 }
104522 return pFinal;
104523}
104524
104525/*
104526** Return the SorterCompare function to compare values collected by the
104527** sorter object passed as the only argument.
104528*/
104529static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
104530 if( p->typeMask==SORTER_TYPE_INTEGER0x01 ){
104531 return vdbeSorterCompareInt;
104532 }else if( p->typeMask==SORTER_TYPE_TEXT0x02 ){
104533 return vdbeSorterCompareText;
104534 }
104535 return vdbeSorterCompare;
104536}
104537
104538/*
104539** Sort the linked list of records headed at pTask->pList. Return
104540** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
104541** an error occurs.
104542*/
104543static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
104544 int i;
104545 SorterRecord *p;
104546 int rc;
104547 SorterRecord *aSlot[64];
104548
104549 rc = vdbeSortAllocUnpacked(pTask);
104550 if( rc!=SQLITE_OK0 ) return rc;
104551
104552 p = pList->pList;
104553 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
104554 memset(aSlot, 0, sizeof(aSlot));
104555
104556 while( p ){
104557 SorterRecord *pNext;
104558 if( pList->aMemory ){
104559 if( (u8*)p==pList->aMemory ){
104560 pNext = 0;
104561 }else{
104562 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) )((void) (0));
104563 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
104564 }
104565 }else{
104566 pNext = p->u.pNext;
104567 }
104568
104569 p->u.pNext = 0;
104570 for(i=0; aSlot[i]; i++){
104571 p = vdbeSorterMerge(pTask, p, aSlot[i]);
104572 aSlot[i] = 0;
104573 }
104574 aSlot[i] = p;
104575 p = pNext;
104576 }
104577
104578 p = 0;
104579 for(i=0; i<ArraySize(aSlot)((int)(sizeof(aSlot)/sizeof(aSlot[0]))); i++){
104580 if( aSlot[i]==0 ) continue;
104581 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
104582 }
104583 pList->pList = p;
104584
104585 assert( pTask->pUnpacked->errCode==SQLITE_OK((void) (0))
104586 || pTask->pUnpacked->errCode==SQLITE_NOMEM((void) (0))
104587 )((void) (0));
104588 return pTask->pUnpacked->errCode;
104589}
104590
104591/*
104592** Initialize a PMA-writer object.
104593*/
104594static void vdbePmaWriterInit(
104595 sqlite3_file *pFd, /* File handle to write to */
104596 PmaWriter *p, /* Object to populate */
104597 int nBuf, /* Buffer size */
104598 i64 iStart /* Offset of pFd to begin writing at */
104599){
104600 memset(p, 0, sizeof(PmaWriter));
104601 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
104602 if( !p->aBuffer ){
104603 p->eFWErr = SQLITE_NOMEM_BKPT7;
104604 }else{
104605 p->iBufEnd = p->iBufStart = (iStart % nBuf);
104606 p->iWriteOff = iStart - p->iBufStart;
104607 p->nBuffer = nBuf;
104608 p->pFd = pFd;
104609 }
104610}
104611
104612/*
104613** Write nData bytes of data to the PMA. Return SQLITE_OK
104614** if successful, or an SQLite error code if an error occurs.
104615*/
104616static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
104617 int nRem = nData;
104618 while( nRem>0 && p->eFWErr==0 ){
104619 int nCopy = nRem;
104620 if( nCopy>(p->nBuffer - p->iBufEnd) ){
104621 nCopy = p->nBuffer - p->iBufEnd;
104622 }
104623
104624 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
104625 p->iBufEnd += nCopy;
104626 if( p->iBufEnd==p->nBuffer ){
104627 p->eFWErr = sqlite3OsWrite(p->pFd,
104628 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
104629 p->iWriteOff + p->iBufStart
104630 );
104631 p->iBufStart = p->iBufEnd = 0;
104632 p->iWriteOff += p->nBuffer;
104633 }
104634 assert( p->iBufEnd<p->nBuffer )((void) (0));
104635
104636 nRem -= nCopy;
104637 }
104638}
104639
104640/*
104641** Flush any buffered data to disk and clean up the PMA-writer object.
104642** The results of using the PMA-writer after this call are undefined.
104643** Return SQLITE_OK if flushing the buffered data succeeds or is not
104644** required. Otherwise, return an SQLite error code.
104645**
104646** Before returning, set *piEof to the offset immediately following the
104647** last byte written to the file.
104648*/
104649static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
104650 int rc;
104651 if( p->eFWErr==0 && ALWAYS(p->aBuffer)(p->aBuffer) && p->iBufEnd>p->iBufStart ){
104652 p->eFWErr = sqlite3OsWrite(p->pFd,
104653 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
104654 p->iWriteOff + p->iBufStart
104655 );
104656 }
104657 *piEof = (p->iWriteOff + p->iBufEnd);
104658 sqlite3_free(p->aBuffer);
104659 rc = p->eFWErr;
104660 memset(p, 0, sizeof(PmaWriter));
104661 return rc;
104662}
104663
104664/*
104665** Write value iVal encoded as a varint to the PMA. Return
104666** SQLITE_OK if successful, or an SQLite error code if an error occurs.
104667*/
104668static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
104669 int nByte;
104670 u8 aByte[10];
104671 nByte = sqlite3PutVarint(aByte, iVal);
104672 vdbePmaWriteBlob(p, aByte, nByte);
104673}
104674
104675/*
104676** Write the current contents of in-memory linked-list pList to a level-0
104677** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
104678** successful, or an SQLite error code otherwise.
104679**
104680** The format of a PMA is:
104681**
104682** * A varint. This varint contains the total number of bytes of content
104683** in the PMA (not including the varint itself).
104684**
104685** * One or more records packed end-to-end in order of ascending keys.
104686** Each record consists of a varint followed by a blob of data (the
104687** key). The varint is the number of bytes in the blob of data.
104688*/
104689static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
104690 sqlite3 *db = pTask->pSorter->db;
104691 int rc = SQLITE_OK0; /* Return code */
104692 PmaWriter writer; /* Object used to write to the file */
104693
104694#ifdef SQLITE_DEBUG
104695 /* Set iSz to the expected size of file pTask->file after writing the PMA.
104696 ** This is used by an assert() statement at the end of this function. */
104697 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
104698#endif
104699
104700 vdbeSorterWorkDebug(pTask, "enter");
104701 memset(&writer, 0, sizeof(PmaWriter));
104702 assert( pList->szPMA>0 )((void) (0));
104703
104704 /* If the first temporary PMA file has not been opened, open it now. */
104705 if( pTask->file.pFd==0 ){
104706 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
104707 assert( rc!=SQLITE_OK || pTask->file.pFd )((void) (0));
104708 assert( pTask->file.iEof==0 )((void) (0));
104709 assert( pTask->nPMA==0 )((void) (0));
104710 }
104711
104712 /* Try to get the file to memory map */
104713 if( rc==SQLITE_OK0 ){
104714 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
104715 }
104716
104717 /* Sort the list */
104718 if( rc==SQLITE_OK0 ){
104719 rc = vdbeSorterSort(pTask, pList);
104720 }
104721
104722 if( rc==SQLITE_OK0 ){
104723 SorterRecord *p;
104724 SorterRecord *pNext = 0;
104725
104726 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
104727 pTask->file.iEof);
104728 pTask->nPMA++;
104729 vdbePmaWriteVarint(&writer, pList->szPMA);
104730 for(p=pList->pList; p; p=pNext){
104731 pNext = p->u.pNext;
104732 vdbePmaWriteVarint(&writer, p->nVal);
104733 vdbePmaWriteBlob(&writer, SRVAL(p)((void*)((SorterRecord*)(p) + 1)), p->nVal);
104734 if( pList->aMemory==0 ) sqlite3_free(p);
104735 }
104736 pList->pList = p;
104737 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
104738 }
104739
104740 vdbeSorterWorkDebug(pTask, "exit");
104741 assert( rc!=SQLITE_OK || pList->pList==0 )((void) (0));
104742 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz )((void) (0));
104743 return rc;
104744}
104745
104746/*
104747** Advance the MergeEngine to its next entry.
104748** Set *pbEof to true there is no next entry because
104749** the MergeEngine has reached the end of all its inputs.
104750**
104751** Return SQLITE_OK if successful or an error code if an error occurs.
104752*/
104753static int vdbeMergeEngineStep(
104754 MergeEngine *pMerger, /* The merge engine to advance to the next row */
104755 int *pbEof /* Set TRUE at EOF. Set false for more content */
104756){
104757 int rc;
104758 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
104759 SortSubtask *pTask = pMerger->pTask;
104760
104761 /* Advance the current PmaReader */
104762 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
104763
104764 /* Update contents of aTree[] */
104765 if( rc==SQLITE_OK0 ){
104766 int i; /* Index of aTree[] to recalculate */
104767 PmaReader *pReadr1; /* First PmaReader to compare */
104768 PmaReader *pReadr2; /* Second PmaReader to compare */
104769 int bCached = 0;
104770
104771 /* Find the first two PmaReaders to compare. The one that was just
104772 ** advanced (iPrev) and the one next to it in the array. */
104773 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
104774 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
104775
104776 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
104777 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
104778 int iRes;
104779 if( pReadr1->pFd==0 ){
104780 iRes = +1;
104781 }else if( pReadr2->pFd==0 ){
104782 iRes = -1;
104783 }else{
104784 iRes = pTask->xCompare(pTask, &bCached,
104785 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
104786 );
104787 }
104788
104789 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
104790 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
104791 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
104792 ** pKey2 to point to the record belonging to pReadr2.
104793 **
104794 ** Alternatively, if pReadr2 contains the smaller of the two values,
104795 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
104796 ** was actually called above, then pTask->pUnpacked now contains
104797 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
104798 ** vdbeSorterCompare() from decoding pReadr2 again.
104799 **
104800 ** If the two values were equal, then the value from the oldest
104801 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
104802 ** is sorted from oldest to newest, so pReadr1 contains older values
104803 ** than pReadr2 iff (pReadr1<pReadr2). */
104804 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
104805 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
104806 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
104807 bCached = 0;
104808 }else{
104809 if( pReadr1->pFd ) bCached = 0;
104810 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
104811 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
104812 }
104813 }
104814 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
104815 }
104816
104817 return (rc==SQLITE_OK0 ? pTask->pUnpacked->errCode : rc);
104818}
104819
104820#if SQLITE_MAX_WORKER_THREADS8>0
104821/*
104822** The main routine for background threads that write level-0 PMAs.
104823*/
104824static void *vdbeSorterFlushThread(void *pCtx){
104825 SortSubtask *pTask = (SortSubtask*)pCtx;
104826 int rc; /* Return code */
104827 assert( pTask->bDone==0 )((void) (0));
104828 rc = vdbeSorterListToPMA(pTask, &pTask->list);
104829 pTask->bDone = 1;
104830 return SQLITE_INT_TO_PTR(rc)((void*)(long int)(rc));
104831}
104832#endif /* SQLITE_MAX_WORKER_THREADS>0 */
104833
104834/*
104835** Flush the current contents of VdbeSorter.list to a new PMA, possibly
104836** using a background thread.
104837*/
104838static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
104839#if SQLITE_MAX_WORKER_THREADS8==0
104840 pSorter->bUsePMA = 1;
104841 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
104842#else
104843 int rc = SQLITE_OK0;
104844 int i;
104845 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
104846 int nWorker = (pSorter->nTask-1);
104847
104848 /* Set the flag to indicate that at least one PMA has been written.
104849 ** Or will be, anyhow. */
104850 pSorter->bUsePMA = 1;
104851
104852 /* Select a sub-task to sort and flush the current list of in-memory
104853 ** records to disk. If the sorter is running in multi-threaded mode,
104854 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
104855 ** the background thread from a sub-tasks previous turn is still running,
104856 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
104857 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
104858 ** sub-tasks are preferred as they use background threads - the final
104859 ** sub-task uses the main thread. */
104860 for(i=0; i<nWorker; i++){
104861 int iTest = (pSorter->iPrev + i + 1) % nWorker;
104862 pTask = &pSorter->aTask[iTest];
104863 if( pTask->bDone ){
104864 rc = vdbeSorterJoinThread(pTask);
104865 }
104866 if( rc!=SQLITE_OK0 || pTask->pThread==0 ) break;
104867 }
104868
104869 if( rc==SQLITE_OK0 ){
104870 if( i==nWorker ){
104871 /* Use the foreground thread for this operation */
104872 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
104873 }else{
104874 /* Launch a background thread for this operation */
104875 u8 *aMem;
104876 void *pCtx;
104877
104878 assert( pTask!=0 )((void) (0));
104879 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
104880 assert( pTask->list.pList==0 )((void) (0));
104881 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 )((void) (0));
104882
104883 aMem = pTask->list.aMemory;
104884 pCtx = (void*)pTask;
104885 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
104886 pTask->list = pSorter->list;
104887 pSorter->list.pList = 0;
104888 pSorter->list.szPMA = 0;
104889 if( aMem ){
104890 pSorter->list.aMemory = aMem;
104891 pSorter->nMemory = sqlite3MallocSize(aMem);
104892 }else if( pSorter->list.aMemory ){
104893 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
104894 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT7;
104895 }
104896
104897 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
104898 }
104899 }
104900
104901 return rc;
104902#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
104903}
104904
104905/*
104906** Add a record to the sorter.
104907*/
104908SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(
104909 const VdbeCursor *pCsr, /* Sorter cursor */
104910 Mem *pVal /* Memory cell containing record */
104911){
104912 VdbeSorter *pSorter;
104913 int rc = SQLITE_OK0; /* Return Code */
104914 SorterRecord *pNew; /* New list element */
104915 int bFlush; /* True to flush contents of memory to PMA */
104916 i64 nReq; /* Bytes of memory required */
104917 i64 nPMA; /* Bytes of PMA space required */
104918 int t; /* serial type of first record field */
104919
104920 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
104921 pSorter = pCsr->uc.pSorter;
104922 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))
;
104923 if( t>0 && t<10 && t!=7 ){
104924 pSorter->typeMask &= SORTER_TYPE_INTEGER0x01;
104925 }else if( t>10 && (t & 0x01) ){
104926 pSorter->typeMask &= SORTER_TYPE_TEXT0x02;
104927 }else{
104928 pSorter->typeMask = 0;
104929 }
104930
104931 assert( pSorter )((void) (0));
104932
104933 /* Figure out whether or not the current contents of memory should be
104934 ** flushed to a PMA before continuing. If so, do so.
104935 **
104936 ** If using the single large allocation mode (pSorter->aMemory!=0), then
104937 ** flush the contents of memory to a new PMA if (a) at least one value is
104938 ** already in memory and (b) the new value will not fit in memory.
104939 **
104940 ** Or, if using separate allocations for each record, flush the contents
104941 ** of memory to a PMA if either of the following are true:
104942 **
104943 ** * The total memory allocated for the in-memory list is greater
104944 ** than (page-size * cache-size), or
104945 **
104946 ** * The total memory allocated for the in-memory list is greater
104947 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
104948 */
104949 nReq = pVal->n + sizeof(SorterRecord);
104950 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
104951 if( pSorter->mxPmaSize ){
104952 if( pSorter->list.aMemory ){
104953 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
104954 }else{
104955 bFlush = (
104956 (pSorter->list.szPMA > pSorter->mxPmaSize)
104957 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
104958 );
104959 }
104960 if( bFlush ){
104961 rc = vdbeSorterFlushPMA(pSorter);
104962 pSorter->list.szPMA = 0;
104963 pSorter->iMemory = 0;
104964 assert( rc!=SQLITE_OK || pSorter->list.pList==0 )((void) (0));
104965 }
104966 }
104967
104968 pSorter->list.szPMA += nPMA;
104969 if( nPMA>pSorter->mxKeysize ){
104970 pSorter->mxKeysize = nPMA;
104971 }
104972
104973 if( pSorter->list.aMemory ){
104974 int nMin = pSorter->iMemory + nReq;
104975
104976 if( nMin>pSorter->nMemory ){
104977 u8 *aNew;
104978 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
104979 int iListOff = -1;
104980 if( pSorter->list.pList ){
104981 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
104982 }
104983 while( nNew < nMin ) nNew = nNew*2;
104984 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
104985 if( nNew < nMin ) nNew = nMin;
104986 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
104987 if( !aNew ) return SQLITE_NOMEM_BKPT7;
104988 if( iListOff>=0 ){
104989 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
104990 }
104991 pSorter->list.aMemory = aNew;
104992 pSorter->nMemory = nNew;
104993 }
104994
104995 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
104996 pSorter->iMemory += ROUND8(nReq)(((nReq)+7)&~7);
104997 if( pSorter->list.pList ){
104998 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
104999 }
105000 }else{
105001 pNew = (SorterRecord *)sqlite3Malloc(nReq);
105002 if( pNew==0 ){
105003 return SQLITE_NOMEM_BKPT7;
105004 }
105005 pNew->u.pNext = pSorter->list.pList;
105006 }
105007
105008 memcpy(SRVAL(pNew)((void*)((SorterRecord*)(pNew) + 1)), pVal->z, pVal->n);
105009 pNew->nVal = pVal->n;
105010 pSorter->list.pList = pNew;
105011
105012 return rc;
105013}
105014
105015/*
105016** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
105017** of the data stored in aFile[1] is the same as that used by regular PMAs,
105018** except that the number-of-bytes varint is omitted from the start.
105019*/
105020static int vdbeIncrPopulate(IncrMerger *pIncr){
105021 int rc = SQLITE_OK0;
105022 int rc2;
105023 i64 iStart = pIncr->iStartOff;
105024 SorterFile *pOut = &pIncr->aFile[1];
105025 SortSubtask *pTask = pIncr->pTask;
105026 MergeEngine *pMerger = pIncr->pMerger;
105027 PmaWriter writer;
105028 assert( pIncr->bEof==0 )((void) (0));
105029
105030 vdbeSorterPopulateDebug(pTask, "enter");
105031
105032 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
105033 while( rc==SQLITE_OK0 ){
105034 int dummy;
105035 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
105036 int nKey = pReader->nKey;
105037 i64 iEof = writer.iWriteOff + writer.iBufEnd;
105038
105039 /* Check if the output file is full or if the input has been exhausted.
105040 ** In either case exit the loop. */
105041 if( pReader->pFd==0 ) break;
105042 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
105043
105044 /* Write the next key to the output. */
105045 vdbePmaWriteVarint(&writer, nKey);
105046 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
105047 assert( pIncr->pMerger->pTask==pTask )((void) (0));
105048 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
105049 }
105050
105051 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
105052 if( rc==SQLITE_OK0 ) rc = rc2;
105053 vdbeSorterPopulateDebug(pTask, "exit");
105054 return rc;
105055}
105056
105057#if SQLITE_MAX_WORKER_THREADS8>0
105058/*
105059** The main routine for background threads that populate aFile[1] of
105060** multi-threaded IncrMerger objects.
105061*/
105062static void *vdbeIncrPopulateThread(void *pCtx){
105063 IncrMerger *pIncr = (IncrMerger*)pCtx;
105064 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) )((void*)(long int)(vdbeIncrPopulate(pIncr)));
105065 pIncr->pTask->bDone = 1;
105066 return pRet;
105067}
105068
105069/*
105070** Launch a background thread to populate aFile[1] of pIncr.
105071*/
105072static int vdbeIncrBgPopulate(IncrMerger *pIncr){
105073 void *p = (void*)pIncr;
105074 assert( pIncr->bUseThread )((void) (0));
105075 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
105076}
105077#endif
105078
105079/*
105080** This function is called when the PmaReader corresponding to pIncr has
105081** finished reading the contents of aFile[0]. Its purpose is to "refill"
105082** aFile[0] such that the PmaReader should start rereading it from the
105083** beginning.
105084**
105085** For single-threaded objects, this is accomplished by literally reading
105086** keys from pIncr->pMerger and repopulating aFile[0].
105087**
105088** For multi-threaded objects, all that is required is to wait until the
105089** background thread is finished (if it is not already) and then swap
105090** aFile[0] and aFile[1] in place. If the contents of pMerger have not
105091** been exhausted, this function also launches a new background thread
105092** to populate the new aFile[1].
105093**
105094** SQLITE_OK is returned on success, or an SQLite error code otherwise.
105095*/
105096static int vdbeIncrSwap(IncrMerger *pIncr){
105097 int rc = SQLITE_OK0;
105098
105099#if SQLITE_MAX_WORKER_THREADS8>0
105100 if( pIncr->bUseThread ){
105101 rc = vdbeSorterJoinThread(pIncr->pTask);
105102
105103 if( rc==SQLITE_OK0 ){
105104 SorterFile f0 = pIncr->aFile[0];
105105 pIncr->aFile[0] = pIncr->aFile[1];
105106 pIncr->aFile[1] = f0;
105107 }
105108
105109 if( rc==SQLITE_OK0 ){
105110 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
105111 pIncr->bEof = 1;
105112 }else{
105113 rc = vdbeIncrBgPopulate(pIncr);
105114 }
105115 }
105116 }else
105117#endif
105118 {
105119 rc = vdbeIncrPopulate(pIncr);
105120 pIncr->aFile[0] = pIncr->aFile[1];
105121 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
105122 pIncr->bEof = 1;
105123 }
105124 }
105125
105126 return rc;
105127}
105128
105129/*
105130** Allocate and return a new IncrMerger object to read data from pMerger.
105131**
105132** If an OOM condition is encountered, return NULL. In this case free the
105133** pMerger argument before returning.
105134*/
105135static int vdbeIncrMergerNew(
105136 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
105137 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
105138 IncrMerger **ppOut /* Write the new IncrMerger here */
105139){
105140 int rc = SQLITE_OK0;
105141 IncrMerger *pIncr = *ppOut = (IncrMerger*)
105142 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
105143 if( pIncr ){
105144 pIncr->pMerger = pMerger;
105145 pIncr->pTask = pTask;
105146 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))
;
105147 pTask->file2.iEof += pIncr->mxSz;
105148 }else{
105149 vdbeMergeEngineFree(pMerger);
105150 rc = SQLITE_NOMEM_BKPT7;
105151 }
105152 assert( *ppOut!=0 || rc!=SQLITE_OK )((void) (0));
105153 return rc;
105154}
105155
105156#if SQLITE_MAX_WORKER_THREADS8>0
105157/*
105158** Set the "use-threads" flag on object pIncr.
105159*/
105160static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
105161 pIncr->bUseThread = 1;
105162 pIncr->pTask->file2.iEof -= pIncr->mxSz;
105163}
105164#endif /* SQLITE_MAX_WORKER_THREADS>0 */
105165
105166
105167
105168/*
105169** Recompute pMerger->aTree[iOut] by comparing the next keys on the
105170** two PmaReaders that feed that entry. Neither of the PmaReaders
105171** are advanced. This routine merely does the comparison.
105172*/
105173static void vdbeMergeEngineCompare(
105174 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
105175 int iOut /* Store the result in pMerger->aTree[iOut] */
105176){
105177 int i1;
105178 int i2;
105179 int iRes;
105180 PmaReader *p1;
105181 PmaReader *p2;
105182
105183 assert( iOut<pMerger->nTree && iOut>0 )((void) (0));
105184
105185 if( iOut>=(pMerger->nTree/2) ){
105186 i1 = (iOut - pMerger->nTree/2) * 2;
105187 i2 = i1 + 1;
105188 }else{
105189 i1 = pMerger->aTree[iOut*2];
105190 i2 = pMerger->aTree[iOut*2+1];
105191 }
105192
105193 p1 = &pMerger->aReadr[i1];
105194 p2 = &pMerger->aReadr[i2];
105195
105196 if( p1->pFd==0 ){
105197 iRes = i2;
105198 }else if( p2->pFd==0 ){
105199 iRes = i1;
105200 }else{
105201 SortSubtask *pTask = pMerger->pTask;
105202 int bCached = 0;
105203 int res;
105204 assert( pTask->pUnpacked!=0 )((void) (0)); /* from vdbeSortSubtaskMain() */
105205 res = pTask->xCompare(
105206 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
105207 );
105208 if( res<=0 ){
105209 iRes = i1;
105210 }else{
105211 iRes = i2;
105212 }
105213 }
105214
105215 pMerger->aTree[iOut] = iRes;
105216}
105217
105218/*
105219** Allowed values for the eMode parameter to vdbeMergeEngineInit()
105220** and vdbePmaReaderIncrMergeInit().
105221**
105222** Only INCRINIT_NORMAL is valid in single-threaded builds (when
105223** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
105224** when there exists one or more separate worker threads.
105225*/
105226#define INCRINIT_NORMAL0 0
105227#define INCRINIT_TASK1 1
105228#define INCRINIT_ROOT2 2
105229
105230/*
105231** Forward reference required as the vdbeIncrMergeInit() and
105232** vdbePmaReaderIncrInit() routines are called mutually recursively when
105233** building a merge tree.
105234*/
105235static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
105236
105237/*
105238** Initialize the MergeEngine object passed as the second argument. Once this
105239** function returns, the first key of merged data may be read from the
105240** MergeEngine object in the usual fashion.
105241**
105242** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
105243** objects attached to the PmaReader objects that the merger reads from have
105244** already been populated, but that they have not yet populated aFile[0] and
105245** set the PmaReader objects up to read from it. In this case all that is
105246** required is to call vdbePmaReaderNext() on each PmaReader to point it at
105247** its first key.
105248**
105249** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
105250** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
105251** to pMerger.
105252**
105253** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
105254*/
105255static int vdbeMergeEngineInit(
105256 SortSubtask *pTask, /* Thread that will run pMerger */
105257 MergeEngine *pMerger, /* MergeEngine to initialize */
105258 int eMode /* One of the INCRINIT_XXX constants */
105259){
105260 int rc = SQLITE_OK0; /* Return code */
105261 int i; /* For looping over PmaReader objects */
105262 int nTree; /* Number of subtrees to merge */
105263
105264 /* Failure to allocate the merge would have been detected prior to
105265 ** invoking this routine */
105266 assert( pMerger!=0 )((void) (0));
105267
105268 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
105269 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
105270
105271 /* Verify that the MergeEngine is assigned to a single thread */
105272 assert( pMerger->pTask==0 )((void) (0));
105273 pMerger->pTask = pTask;
105274
105275 nTree = pMerger->nTree;
105276 for(i=0; i<nTree; i++){
105277 if( SQLITE_MAX_WORKER_THREADS8>0 && eMode==INCRINIT_ROOT2 ){
105278 /* PmaReaders should be normally initialized in order, as if they are
105279 ** reading from the same temp file this makes for more linear file IO.
105280 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
105281 ** in use it will block the vdbePmaReaderNext() call while it uses
105282 ** the main thread to fill its buffer. So calling PmaReaderNext()
105283 ** on this PmaReader before any of the multi-threaded PmaReaders takes
105284 ** better advantage of multi-processor hardware. */
105285 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
105286 }else{
105287 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL0);
105288 }
105289 if( rc!=SQLITE_OK0 ) return rc;
105290 }
105291
105292 for(i=pMerger->nTree-1; i>0; i--){
105293 vdbeMergeEngineCompare(pMerger, i);
105294 }
105295 return pTask->pUnpacked->errCode;
105296}
105297
105298/*
105299** The PmaReader passed as the first argument is guaranteed to be an
105300** incremental-reader (pReadr->pIncr!=0). This function serves to open
105301** and/or initialize the temp file related fields of the IncrMerge
105302** object at (pReadr->pIncr).
105303**
105304** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
105305** in the sub-tree headed by pReadr are also initialized. Data is then
105306** loaded into the buffers belonging to pReadr and it is set to point to
105307** the first key in its range.
105308**
105309** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
105310** to be a multi-threaded PmaReader and this function is being called in a
105311** background thread. In this case all PmaReaders in the sub-tree are
105312** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
105313** pReadr is populated. However, pReadr itself is not set up to point
105314** to its first key. A call to vdbePmaReaderNext() is still required to do
105315** that.
105316**
105317** The reason this function does not call vdbePmaReaderNext() immediately
105318** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
105319** to block on thread (pTask->thread) before accessing aFile[1]. But, since
105320** this entire function is being run by thread (pTask->thread), that will
105321** lead to the current background thread attempting to join itself.
105322**
105323** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
105324** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
105325** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
105326** In this case vdbePmaReaderNext() is called on all child PmaReaders and
105327** the current PmaReader set to point to the first key in its range.
105328**
105329** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
105330*/
105331static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
105332 int rc = SQLITE_OK0;
105333 IncrMerger *pIncr = pReadr->pIncr;
105334 SortSubtask *pTask = pIncr->pTask;
105335 sqlite3 *db = pTask->pSorter->db;
105336
105337 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
105338 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
105339
105340 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
105341
105342 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
105343 ** requires two temp files to itself, whereas a single-threaded object
105344 ** only requires a region of pTask->file2. */
105345 if( rc==SQLITE_OK0 ){
105346 int mxSz = pIncr->mxSz;
105347#if SQLITE_MAX_WORKER_THREADS8>0
105348 if( pIncr->bUseThread ){
105349 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
105350 if( rc==SQLITE_OK0 ){
105351 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
105352 }
105353 }else
105354#endif
105355 /*if( !pIncr->bUseThread )*/{
105356 if( pTask->file2.pFd==0 ){
105357 assert( pTask->file2.iEof>0 )((void) (0));
105358 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
105359 pTask->file2.iEof = 0;
105360 }
105361 if( rc==SQLITE_OK0 ){
105362 pIncr->aFile[1].pFd = pTask->file2.pFd;
105363 pIncr->iStartOff = pTask->file2.iEof;
105364 pTask->file2.iEof += mxSz;
105365 }
105366 }
105367 }
105368
105369#if SQLITE_MAX_WORKER_THREADS8>0
105370 if( rc==SQLITE_OK0 && pIncr->bUseThread ){
105371 /* Use the current thread to populate aFile[1], even though this
105372 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
105373 ** then this function is already running in background thread
105374 ** pIncr->pTask->thread.
105375 **
105376 ** If this is the INCRINIT_ROOT object, then it is running in the
105377 ** main VDBE thread. But that is Ok, as that thread cannot return
105378 ** control to the VDBE or proceed with anything useful until the
105379 ** first results are ready from this merger object anyway.
105380 */
105381 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK )((void) (0));
105382 rc = vdbeIncrPopulate(pIncr);
105383 }
105384#endif
105385
105386 if( rc==SQLITE_OK0 && (SQLITE_MAX_WORKER_THREADS8==0 || eMode!=INCRINIT_TASK1) ){
105387 rc = vdbePmaReaderNext(pReadr);
105388 }
105389
105390 return rc;
105391}
105392
105393#if SQLITE_MAX_WORKER_THREADS8>0
105394/*
105395** The main routine for vdbePmaReaderIncrMergeInit() operations run in
105396** background threads.
105397*/
105398static void *vdbePmaReaderBgIncrInit(void *pCtx){
105399 PmaReader *pReader = (PmaReader*)pCtx;
105400 void *pRet = SQLITE_INT_TO_PTR(((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
105401 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
105402 )((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)));
105403 pReader->pIncr->pTask->bDone = 1;
105404 return pRet;
105405}
105406#endif
105407
105408/*
105409** If the PmaReader passed as the first argument is not an incremental-reader
105410** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
105411** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
105412** this routine to initialize the incremental merge.
105413**
105414** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
105415** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
105416** Or, if the IncrMerger is single threaded, the same function is called
105417** using the current thread.
105418*/
105419static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
105420 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
105421 int rc = SQLITE_OK0; /* Return code */
105422 if( pIncr ){
105423#if SQLITE_MAX_WORKER_THREADS8>0
105424 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK )((void) (0));
105425 if( pIncr->bUseThread ){
105426 void *pCtx = (void*)pReadr;
105427 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
105428 }else
105429#endif
105430 {
105431 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
105432 }
105433 }
105434 return rc;
105435}
105436
105437/*
105438** Allocate a new MergeEngine object to merge the contents of nPMA level-0
105439** PMAs from pTask->file. If no error occurs, set *ppOut to point to
105440** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
105441** to NULL and return an SQLite error code.
105442**
105443** When this function is called, *piOffset is set to the offset of the
105444** first PMA to read from pTask->file. Assuming no error occurs, it is
105445** set to the offset immediately following the last byte of the last
105446** PMA before returning. If an error does occur, then the final value of
105447** *piOffset is undefined.
105448*/
105449static int vdbeMergeEngineLevel0(
105450 SortSubtask *pTask, /* Sorter task to read from */
105451 int nPMA, /* Number of PMAs to read */
105452 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
105453 MergeEngine **ppOut /* OUT: New merge-engine */
105454){
105455 MergeEngine *pNew; /* Merge engine to return */
105456 i64 iOff = *piOffset;
105457 int i;
105458 int rc = SQLITE_OK0;
105459
105460 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
105461 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT7;
105462
105463 for(i=0; i<nPMA && rc==SQLITE_OK0; i++){
105464 i64 nDummy = 0;
105465 PmaReader *pReadr = &pNew->aReadr[i];
105466 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
105467 iOff = pReadr->iEof;
105468 }
105469
105470 if( rc!=SQLITE_OK0 ){
105471 vdbeMergeEngineFree(pNew);
105472 *ppOut = 0;
105473 }
105474 *piOffset = iOff;
105475 return rc;
105476}
105477
105478/*
105479** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
105480** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
105481**
105482** i.e.
105483**
105484** nPMA<=16 -> TreeDepth() == 0
105485** nPMA<=256 -> TreeDepth() == 1
105486** nPMA<=65536 -> TreeDepth() == 2
105487*/
105488static int vdbeSorterTreeDepth(int nPMA){
105489 int nDepth = 0;
105490 i64 nDiv = SORTER_MAX_MERGE_COUNT16;
105491 while( nDiv < (i64)nPMA ){
105492 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
105493 nDepth++;
105494 }
105495 return nDepth;
105496}
105497
105498/*
105499** pRoot is the root of an incremental merge-tree with depth nDepth (according
105500** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
105501** tree, counting from zero. This function adds pLeaf to the tree.
105502**
105503** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
105504** code is returned and pLeaf is freed.
105505*/
105506static int vdbeSorterAddToTree(
105507 SortSubtask *pTask, /* Task context */
105508 int nDepth, /* Depth of tree according to TreeDepth() */
105509 int iSeq, /* Sequence number of leaf within tree */
105510 MergeEngine *pRoot, /* Root of tree */
105511 MergeEngine *pLeaf /* Leaf to add to tree */
105512){
105513 int rc = SQLITE_OK0;
105514 int nDiv = 1;
105515 int i;
105516 MergeEngine *p = pRoot;
105517 IncrMerger *pIncr;
105518
105519 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
105520
105521 for(i=1; i<nDepth; i++){
105522 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
105523 }
105524
105525 for(i=1; i<nDepth && rc==SQLITE_OK0; i++){
105526 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT16;
105527 PmaReader *pReadr = &p->aReadr[iIter];
105528
105529 if( pReadr->pIncr==0 ){
105530 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
105531 if( pNew==0 ){
105532 rc = SQLITE_NOMEM_BKPT7;
105533 }else{
105534 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
105535 }
105536 }
105537 if( rc==SQLITE_OK0 ){
105538 p = pReadr->pIncr->pMerger;
105539 nDiv = nDiv / SORTER_MAX_MERGE_COUNT16;
105540 }
105541 }
105542
105543 if( rc==SQLITE_OK0 ){
105544 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT16].pIncr = pIncr;
105545 }else{
105546 vdbeIncrFree(pIncr);
105547 }
105548 return rc;
105549}
105550
105551/*
105552** This function is called as part of a SorterRewind() operation on a sorter
105553** that has already written two or more level-0 PMAs to one or more temp
105554** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
105555** can be used to incrementally merge all PMAs on disk.
105556**
105557** If successful, SQLITE_OK is returned and *ppOut set to point to the
105558** MergeEngine object at the root of the tree before returning. Or, if an
105559** error occurs, an SQLite error code is returned and the final value
105560** of *ppOut is undefined.
105561*/
105562static int vdbeSorterMergeTreeBuild(
105563 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
105564 MergeEngine **ppOut /* Write the MergeEngine here */
105565){
105566 MergeEngine *pMain = 0;
105567 int rc = SQLITE_OK0;
105568 int iTask;
105569
105570#if SQLITE_MAX_WORKER_THREADS8>0
105571 /* If the sorter uses more than one task, then create the top-level
105572 ** MergeEngine here. This MergeEngine will read data from exactly
105573 ** one PmaReader per sub-task. */
105574 assert( pSorter->bUseThreads || pSorter->nTask==1 )((void) (0));
105575 if( pSorter->nTask>1 ){
105576 pMain = vdbeMergeEngineNew(pSorter->nTask);
105577 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT7;
105578 }
105579#endif
105580
105581 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
105582 SortSubtask *pTask = &pSorter->aTask[iTask];
105583 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 )((void) (0));
105584 if( SQLITE_MAX_WORKER_THREADS8==0 || pTask->nPMA ){
105585 MergeEngine *pRoot = 0; /* Root node of tree for this task */
105586 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
105587 i64 iReadOff = 0;
105588
105589 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT16 ){
105590 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
105591 }else{
105592 int i;
105593 int iSeq = 0;
105594 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
105595 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT7;
105596 for(i=0; i<pTask->nPMA && rc==SQLITE_OK0; i += SORTER_MAX_MERGE_COUNT16){
105597 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
105598 int nReader; /* Number of level-0 PMAs to merge */
105599
105600 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT)((pTask->nPMA - i)<(16)?(pTask->nPMA - i):(16));
105601 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
105602 if( rc==SQLITE_OK0 ){
105603 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
105604 }
105605 }
105606 }
105607
105608 if( rc==SQLITE_OK0 ){
105609#if SQLITE_MAX_WORKER_THREADS8>0
105610 if( pMain!=0 ){
105611 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
105612 }else
105613#endif
105614 {
105615 assert( pMain==0 )((void) (0));
105616 pMain = pRoot;
105617 }
105618 }else{
105619 vdbeMergeEngineFree(pRoot);
105620 }
105621 }
105622 }
105623
105624 if( rc!=SQLITE_OK0 ){
105625 vdbeMergeEngineFree(pMain);
105626 pMain = 0;
105627 }
105628 *ppOut = pMain;
105629 return rc;
105630}
105631
105632/*
105633** This function is called as part of an sqlite3VdbeSorterRewind() operation
105634** on a sorter that has written two or more PMAs to temporary files. It sets
105635** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
105636** (for multi-threaded sorters) so that it can be used to iterate through
105637** all records stored in the sorter.
105638**
105639** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
105640*/
105641static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
105642 int rc; /* Return code */
105643 SortSubtask *pTask0 = &pSorter->aTask[0];
105644 MergeEngine *pMain = 0;
105645#if SQLITE_MAX_WORKER_THREADS8
105646 sqlite3 *db = pTask0->pSorter->db;
105647 int i;
105648 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
105649 for(i=0; i<pSorter->nTask; i++){
105650 pSorter->aTask[i].xCompare = xCompare;
105651 }
105652#endif
105653
105654 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
105655 if( rc==SQLITE_OK0 ){
105656#if SQLITE_MAX_WORKER_THREADS8
105657 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 )((void) (0));
105658 if( pSorter->bUseThreads ){
105659 int iTask;
105660 PmaReader *pReadr = 0;
105661 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
105662 rc = vdbeSortAllocUnpacked(pLast);
105663 if( rc==SQLITE_OK0 ){
105664 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
105665 pSorter->pReader = pReadr;
105666 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT7;
105667 }
105668 if( rc==SQLITE_OK0 ){
105669 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
105670 if( rc==SQLITE_OK0 ){
105671 vdbeIncrMergerSetThreads(pReadr->pIncr);
105672 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
105673 IncrMerger *pIncr;
105674 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
105675 vdbeIncrMergerSetThreads(pIncr);
105676 assert( pIncr->pTask!=pLast )((void) (0));
105677 }
105678 }
105679 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
105680 /* Check that:
105681 **
105682 ** a) The incremental merge object is configured to use the
105683 ** right task, and
105684 ** b) If it is using task (nTask-1), it is configured to run
105685 ** in single-threaded mode. This is important, as the
105686 ** root merge (INCRINIT_ROOT) will be using the same task
105687 ** object.
105688 */
105689 PmaReader *p = &pMain->aReadr[iTask];
105690 assert( p->pIncr==0 || (((void) (0))
105691 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */((void) (0))
105692 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */((void) (0))
105693 ))((void) (0));
105694 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK1);
105695 }
105696 }
105697 pMain = 0;
105698 }
105699 if( rc==SQLITE_OK0 ){
105700 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT2);
105701 }
105702 }else
105703#endif
105704 {
105705 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL0);
105706 pSorter->pMerger = pMain;
105707 pMain = 0;
105708 }
105709 }
105710
105711 if( rc!=SQLITE_OK0 ){
105712 vdbeMergeEngineFree(pMain);
105713 }
105714 return rc;
105715}
105716
105717
105718/*
105719** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
105720** this function is called to prepare for iterating through the records
105721** in sorted order.
105722*/
105723SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
105724 VdbeSorter *pSorter;
105725 int rc = SQLITE_OK0; /* Return code */
105726
105727 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105728 pSorter = pCsr->uc.pSorter;
105729 assert( pSorter )((void) (0));
105730
105731 /* If no data has been written to disk, then do not do so now. Instead,
105732 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
105733 ** from the in-memory list. */
105734 if( pSorter->bUsePMA==0 ){
105735 if( pSorter->list.pList ){
105736 *pbEof = 0;
105737 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
105738 }else{
105739 *pbEof = 1;
105740 }
105741 return rc;
105742 }
105743
105744 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
105745 ** function flushes the contents of memory to disk, it immediately always
105746 ** creates a new list consisting of a single key immediately afterwards.
105747 ** So the list is never empty at this point. */
105748 assert( pSorter->list.pList )((void) (0));
105749 rc = vdbeSorterFlushPMA(pSorter);
105750
105751 /* Join all threads */
105752 rc = vdbeSorterJoinAll(pSorter, rc);
105753
105754 vdbeSorterRewindDebug("rewind");
105755
105756 /* Assuming no errors have occurred, set up a merger structure to
105757 ** incrementally read and merge all remaining PMAs. */
105758 assert( pSorter->pReader==0 )((void) (0));
105759 if( rc==SQLITE_OK0 ){
105760 rc = vdbeSorterSetupMerge(pSorter);
105761 *pbEof = 0;
105762 }
105763
105764 vdbeSorterRewindDebug("rewinddone");
105765 return rc;
105766}
105767
105768/*
105769** Advance to the next element in the sorter. Return value:
105770**
105771** SQLITE_OK success
105772** SQLITE_DONE end of data
105773** otherwise some kind of error.
105774*/
105775SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
105776 VdbeSorter *pSorter;
105777 int rc; /* Return code */
105778
105779 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105780 pSorter = pCsr->uc.pSorter;
105781 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) )((void) (0));
105782 if( pSorter->bUsePMA ){
105783 assert( pSorter->pReader==0 || pSorter->pMerger==0 )((void) (0));
105784 assert( pSorter->bUseThreads==0 || pSorter->pReader )((void) (0));
105785 assert( pSorter->bUseThreads==1 || pSorter->pMerger )((void) (0));
105786#if SQLITE_MAX_WORKER_THREADS8>0
105787 if( pSorter->bUseThreads ){
105788 rc = vdbePmaReaderNext(pSorter->pReader);
105789 if( rc==SQLITE_OK0 && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE101;
105790 }else
105791#endif
105792 /*if( !pSorter->bUseThreads )*/ {
105793 int res = 0;
105794 assert( pSorter->pMerger!=0 )((void) (0));
105795 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) )((void) (0));
105796 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
105797 if( rc==SQLITE_OK0 && res ) rc = SQLITE_DONE101;
105798 }
105799 }else{
105800 SorterRecord *pFree = pSorter->list.pList;
105801 pSorter->list.pList = pFree->u.pNext;
105802 pFree->u.pNext = 0;
105803 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
105804 rc = pSorter->list.pList ? SQLITE_OK0 : SQLITE_DONE101;
105805 }
105806 return rc;
105807}
105808
105809/*
105810** Return a pointer to a buffer owned by the sorter that contains the
105811** current key.
105812*/
105813static void *vdbeSorterRowkey(
105814 const VdbeSorter *pSorter, /* Sorter object */
105815 int *pnKey /* OUT: Size of current key in bytes */
105816){
105817 void *pKey;
105818 if( pSorter->bUsePMA ){
105819 PmaReader *pReader;
105820#if SQLITE_MAX_WORKER_THREADS8>0
105821 if( pSorter->bUseThreads ){
105822 pReader = pSorter->pReader;
105823 }else
105824#endif
105825 /*if( !pSorter->bUseThreads )*/{
105826 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
105827 }
105828 *pnKey = pReader->nKey;
105829 pKey = pReader->aKey;
105830 }else{
105831 *pnKey = pSorter->list.pList->nVal;
105832 pKey = SRVAL(pSorter->list.pList)((void*)((SorterRecord*)(pSorter->list.pList) + 1));
105833 }
105834 return pKey;
105835}
105836
105837/*
105838** Copy the current sorter key into the memory cell pOut.
105839*/
105840SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
105841 VdbeSorter *pSorter;
105842 void *pKey; int nKey; /* Sorter key to copy into pOut */
105843
105844 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105845 pSorter = pCsr->uc.pSorter;
105846 pKey = vdbeSorterRowkey(pSorter, &nKey);
105847 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
105848 return SQLITE_NOMEM_BKPT7;
105849 }
105850 pOut->n = nKey;
105851 MemSetTypeFlag(pOut, MEM_Blob)((pOut)->flags = ((pOut)->flags&~(0x0dbf|0x0400))|0x0010
)
;
105852 memcpy(pOut->z, pKey, nKey);
105853
105854 return SQLITE_OK0;
105855}
105856
105857/*
105858** Compare the key in memory cell pVal with the key that the sorter cursor
105859** passed as the first argument currently points to. For the purposes of
105860** the comparison, ignore the rowid field at the end of each record.
105861**
105862** If the sorter cursor key contains any NULL values, consider it to be
105863** less than pVal. Even if pVal also contains NULL values.
105864**
105865** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
105866** Otherwise, set *pRes to a negative, zero or positive value if the
105867** key in pVal is smaller than, equal to or larger than the current sorter
105868** key.
105869**
105870** This routine forms the core of the OP_SorterCompare opcode, which in
105871** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
105872*/
105873SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(
105874 const VdbeCursor *pCsr, /* Sorter cursor */
105875 Mem *pVal, /* Value to compare to current sorter key */
105876 int nKeyCol, /* Compare this many columns */
105877 int *pRes /* OUT: Result of comparison */
105878){
105879 VdbeSorter *pSorter;
105880 UnpackedRecord *r2;
105881 KeyInfo *pKeyInfo;
105882 int i;
105883 void *pKey; int nKey; /* Sorter key to compare pVal with */
105884
105885 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
105886 pSorter = pCsr->uc.pSorter;
105887 r2 = pSorter->pUnpacked;
105888 pKeyInfo = pCsr->pKeyInfo;
105889 if( r2==0 ){
105890 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
105891 if( r2==0 ) return SQLITE_NOMEM_BKPT7;
105892 r2->nField = nKeyCol;
105893 }
105894 assert( r2->nField==nKeyCol )((void) (0));
105895
105896 pKey = vdbeSorterRowkey(pSorter, &nKey);
105897 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
105898 for(i=0; i<nKeyCol; i++){
105899 if( r2->aMem[i].flags & MEM_Null0x0001 ){
105900 *pRes = -1;
105901 return SQLITE_OK0;
105902 }
105903 }
105904
105905 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
105906 return SQLITE_OK0;
105907}
105908
105909/************** End of vdbesort.c ********************************************/
105910/************** Begin file vdbevtab.c ****************************************/
105911/*
105912** 2020-03-23
105913**
105914** The author disclaims copyright to this source code. In place of
105915** a legal notice, here is a blessing:
105916**
105917** May you do good and not evil.
105918** May you find forgiveness for yourself and forgive others.
105919** May you share freely, never taking more than you give.
105920**
105921*************************************************************************
105922**
105923** This file implements virtual-tables for examining the bytecode content
105924** of a prepared statement.
105925*/
105926/* #include "sqliteInt.h" */
105927#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
105928/* #include "vdbeInt.h" */
105929
105930/* An instance of the bytecode() table-valued function.
105931*/
105932typedef struct bytecodevtab bytecodevtab;
105933struct bytecodevtab {
105934 sqlite3_vtab base; /* Base class - must be first */
105935 sqlite3 *db; /* Database connection */
105936 int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
105937};
105938
105939/* A cursor for scanning through the bytecode
105940*/
105941typedef struct bytecodevtab_cursor bytecodevtab_cursor;
105942struct bytecodevtab_cursor {
105943 sqlite3_vtab_cursor base; /* Base class - must be first */
105944 sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */
105945 int iRowid; /* The rowid of the output table */
105946 int iAddr; /* Address */
105947 int needFinalize; /* Cursors owns pStmt and must finalize it */
105948 int showSubprograms; /* Provide a listing of subprograms */
105949 Op *aOp; /* Operand array */
105950 char *zP4; /* Rendered P4 value */
105951 const char *zType; /* tables_used.type */
105952 const char *zSchema; /* tables_used.schema */
105953 const char *zName; /* tables_used.name */
105954 Mem sub; /* Subprograms */
105955};
105956
105957/*
105958** Create a new bytecode() table-valued function.
105959*/
105960static int bytecodevtabConnect(
105961 sqlite3 *db,
105962 void *pAux,
105963 int argc, const char *const*argv,
105964 sqlite3_vtab **ppVtab,
105965 char **pzErr
105966){
105967 bytecodevtab *pNew;
105968 int rc;
105969 int isTabUsed = pAux!=0;
105970 const char *azSchema[2] = {
105971 /* bytecode() schema */
105972 "CREATE TABLE x("
105973 "addr INT,"
105974 "opcode TEXT,"
105975 "p1 INT,"
105976 "p2 INT,"
105977 "p3 INT,"
105978 "p4 TEXT,"
105979 "p5 INT,"
105980 "comment TEXT,"
105981 "subprog TEXT,"
105982 "nexec INT,"
105983 "ncycle INT,"
105984 "stmt HIDDEN"
105985 ");",
105986
105987 /* Tables_used() schema */
105988 "CREATE TABLE x("
105989 "type TEXT,"
105990 "schema TEXT,"
105991 "name TEXT,"
105992 "wr INT,"
105993 "subprog TEXT,"
105994 "stmt HIDDEN"
105995 ");"
105996 };
105997
105998 (void)argc;
105999 (void)argv;
106000 (void)pzErr;
106001 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
106002 if( rc==SQLITE_OK0 ){
106003 pNew = sqlite3_malloc( sizeof(*pNew) );
106004 *ppVtab = (sqlite3_vtab*)pNew;
106005 if( pNew==0 ) return SQLITE_NOMEM7;
106006 memset(pNew, 0, sizeof(*pNew));
106007 pNew->db = db;
106008 pNew->bTablesUsed = isTabUsed*2;
106009 }
106010 return rc;
106011}
106012
106013/*
106014** This method is the destructor for bytecodevtab objects.
106015*/
106016static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
106017 bytecodevtab *p = (bytecodevtab*)pVtab;
106018 sqlite3_free(p);
106019 return SQLITE_OK0;
106020}
106021
106022/*
106023** Constructor for a new bytecodevtab_cursor object.
106024*/
106025static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
106026 bytecodevtab *pVTab = (bytecodevtab*)p;
106027 bytecodevtab_cursor *pCur;
106028 pCur = sqlite3_malloc( sizeof(*pCur) );
106029 if( pCur==0 ) return SQLITE_NOMEM7;
106030 memset(pCur, 0, sizeof(*pCur));
106031 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
106032 *ppCursor = &pCur->base;
106033 return SQLITE_OK0;
106034}
106035
106036/*
106037** Clear all internal content from a bytecodevtab cursor.
106038*/
106039static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
106040 sqlite3_free(pCur->zP4);
106041 pCur->zP4 = 0;
106042 sqlite3VdbeMemRelease(&pCur->sub);
106043 sqlite3VdbeMemSetNull(&pCur->sub);
106044 if( pCur->needFinalize ){
106045 sqlite3_finalize(pCur->pStmt);
106046 }
106047 pCur->pStmt = 0;
106048 pCur->needFinalize = 0;
106049 pCur->zType = 0;
106050 pCur->zSchema = 0;
106051 pCur->zName = 0;
106052}
106053
106054/*
106055** Destructor for a bytecodevtab_cursor.
106056*/
106057static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
106058 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
106059 bytecodevtabCursorClear(pCur);
106060 sqlite3_free(pCur);
106061 return SQLITE_OK0;
106062}
106063
106064
106065/*
106066** Advance a bytecodevtab_cursor to its next row of output.
106067*/
106068static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
106069 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
106070 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
106071 int rc;
106072 if( pCur->zP4 ){
106073 sqlite3_free(pCur->zP4);
106074 pCur->zP4 = 0;
106075 }
106076 if( pCur->zName ){
106077 pCur->zName = 0;
106078 pCur->zType = 0;
106079 pCur->zSchema = 0;
106080 }
106081 rc = sqlite3VdbeNextOpcode(
106082 (Vdbe*)pCur->pStmt,
106083 pCur->showSubprograms ? &pCur->sub : 0,
106084 pTab->bTablesUsed,
106085 &pCur->iRowid,
106086 &pCur->iAddr,
106087 &pCur->aOp);
106088 if( rc!=SQLITE_OK0 ){
106089 sqlite3VdbeMemSetNull(&pCur->sub);
106090 pCur->aOp = 0;
106091 }
106092 return SQLITE_OK0;
106093}
106094
106095/*
106096** Return TRUE if the cursor has been moved off of the last
106097** row of output.
106098*/
106099static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
106100 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
106101 return pCur->aOp==0;
106102}
106103
106104/*
106105** Return values of columns for the row at which the bytecodevtab_cursor
106106** is currently pointing.
106107*/
106108static int bytecodevtabColumn(
106109 sqlite3_vtab_cursor *cur, /* The cursor */
106110 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
106111 int i /* Which column to return */
106112){
106113 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
106114 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
106115 Op *pOp = pCur->aOp + pCur->iAddr;
106116 if( pVTab->bTablesUsed ){
106117 if( i==4 ){
106118 i = 8;
106119 }else{
106120 if( i<=2 && pCur->zType==0 ){
106121 Schema *pSchema;
106122 HashElem *k;
106123 int iDb = pOp->p3;
106124 Pgno iRoot = (Pgno)pOp->p2;
106125 sqlite3 *db = pVTab->db;
106126 pSchema = db->aDb[iDb].pSchema;
106127 pCur->zSchema = db->aDb[iDb].zDbSName;
106128 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
106129 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
106130 if( !IsVirtual(pTab)((pTab)->eTabType==1) && pTab->tnum==iRoot ){
106131 pCur->zName = pTab->zName;
106132 pCur->zType = "table";
106133 break;
106134 }
106135 }
106136 if( pCur->zName==0 ){
106137 for(k=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); k; k=sqliteHashNext(k)((k)->next)){
106138 Index *pIdx = (Index*)sqliteHashData(k)((k)->data);
106139 if( pIdx->tnum==iRoot ){
106140 pCur->zName = pIdx->zName;
106141 pCur->zType = "index";
106142 }
106143 }
106144 }
106145 }
106146 i += 20;
106147 }
106148 }
106149 switch( i ){
106150 case 0: /* addr */
106151 sqlite3_result_int(ctx, pCur->iAddr);
106152 break;
106153 case 1: /* opcode */
106154 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
106155 -1, SQLITE_STATIC((sqlite3_destructor_type)0));
106156 break;
106157 case 2: /* p1 */
106158 sqlite3_result_int(ctx, pOp->p1);
106159 break;
106160 case 3: /* p2 */
106161 sqlite3_result_int(ctx, pOp->p2);
106162 break;
106163 case 4: /* p3 */
106164 sqlite3_result_int(ctx, pOp->p3);
106165 break;
106166 case 5: /* p4 */
106167 case 7: /* comment */
106168 if( pCur->zP4==0 ){
106169 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
106170 }
106171 if( i==5 ){
106172 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
106173 }else{
106174#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
106175 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
106176 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
106177#endif
106178 }
106179 break;
106180 case 6: /* p5 */
106181 sqlite3_result_int(ctx, pOp->p5);
106182 break;
106183 case 8: { /* subprog */
106184 Op *aOp = pCur->aOp;
106185 assert( aOp[0].opcode==OP_Init )((void) (0));
106186 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 )((void) (0));
106187 if( pCur->iRowid==pCur->iAddr+1 ){
106188 break; /* Result is NULL for the main program */
106189 }else if( aOp[0].p4.z!=0 ){
106190 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
106191 }else{
106192 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
106193 }
106194 break;
106195 }
106196
106197#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
106198 case 9: /* nexec */
106199 sqlite3_result_int64(ctx, pOp->nExec);
106200 break;
106201 case 10: /* ncycle */
106202 sqlite3_result_int64(ctx, pOp->nCycle);
106203 break;
106204#else
106205 case 9: /* nexec */
106206 case 10: /* ncycle */
106207 sqlite3_result_int(ctx, 0);
106208 break;
106209#endif
106210
106211 case 20: /* tables_used.type */
106212 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
106213 break;
106214 case 21: /* tables_used.schema */
106215 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
106216 break;
106217 case 22: /* tables_used.name */
106218 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
106219 break;
106220 case 23: /* tables_used.wr */
106221 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite113);
106222 break;
106223 }
106224 return SQLITE_OK0;
106225}
106226
106227/*
106228** Return the rowid for the current row. In this implementation, the
106229** rowid is the same as the output value.
106230*/
106231static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
106232 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
106233 *pRowid = pCur->iRowid;
106234 return SQLITE_OK0;
106235}
106236
106237/*
106238** Initialize a cursor.
106239**
106240** idxNum==0 means show all subprograms
106241** idxNum==1 means show only the main bytecode and omit subprograms.
106242*/
106243static int bytecodevtabFilter(
106244 sqlite3_vtab_cursor *pVtabCursor,
106245 int idxNum, const char *idxStr,
106246 int argc, sqlite3_value **argv
106247){
106248 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
106249 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
106250 int rc = SQLITE_OK0;
106251 (void)idxStr;
106252
106253 bytecodevtabCursorClear(pCur);
106254 pCur->iRowid = 0;
106255 pCur->iAddr = 0;
106256 pCur->showSubprograms = idxNum==0;
106257 assert( argc==1 )((void) (0));
106258 if( sqlite3_value_type(argv[0])==SQLITE_TEXT3 ){
106259 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
106260 if( zSql==0 ){
106261 rc = SQLITE_NOMEM7;
106262 }else{
106263 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
106264 pCur->needFinalize = 1;
106265 }
106266 }else{
106267 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
106268 }
106269 if( pCur->pStmt==0 ){
106270 pVTab->base.zErrMsg = sqlite3_mprintf(
106271 "argument to %s() is not a valid SQL statement",
106272 pVTab->bTablesUsed ? "tables_used" : "bytecode"
106273 );
106274 rc = SQLITE_ERROR1;
106275 }else{
106276 bytecodevtabNext(pVtabCursor);
106277 }
106278 return rc;
106279}
106280
106281/*
106282** We must have a single stmt=? constraint that will be passed through
106283** into the xFilter method. If there is no valid stmt=? constraint,
106284** then return an SQLITE_CONSTRAINT error.
106285*/
106286static int bytecodevtabBestIndex(
106287 sqlite3_vtab *tab,
106288 sqlite3_index_info *pIdxInfo
106289){
106290 int i;
106291 int rc = SQLITE_CONSTRAINT19;
106292 struct sqlite3_index_constraint *p;
106293 bytecodevtab *pVTab = (bytecodevtab*)tab;
106294 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
106295 pIdxInfo->estimatedCost = (double)100;
106296 pIdxInfo->estimatedRows = 100;
106297 pIdxInfo->idxNum = 0;
106298 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
106299 if( p->usable==0 ) continue;
106300 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && p->iColumn==iBaseCol+1 ){
106301 rc = SQLITE_OK0;
106302 pIdxInfo->aConstraintUsage[i].omit = 1;
106303 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
106304 }
106305 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL71 && p->iColumn==iBaseCol ){
106306 pIdxInfo->aConstraintUsage[i].omit = 1;
106307 pIdxInfo->idxNum = 1;
106308 }
106309 }
106310 return rc;
106311}
106312
106313/*
106314** This following structure defines all the methods for the
106315** virtual table.
106316*/
106317static sqlite3_module bytecodevtabModule = {
106318 /* iVersion */ 0,
106319 /* xCreate */ 0,
106320 /* xConnect */ bytecodevtabConnect,
106321 /* xBestIndex */ bytecodevtabBestIndex,
106322 /* xDisconnect */ bytecodevtabDisconnect,
106323 /* xDestroy */ 0,
106324 /* xOpen */ bytecodevtabOpen,
106325 /* xClose */ bytecodevtabClose,
106326 /* xFilter */ bytecodevtabFilter,
106327 /* xNext */ bytecodevtabNext,
106328 /* xEof */ bytecodevtabEof,
106329 /* xColumn */ bytecodevtabColumn,
106330 /* xRowid */ bytecodevtabRowid,
106331 /* xUpdate */ 0,
106332 /* xBegin */ 0,
106333 /* xSync */ 0,
106334 /* xCommit */ 0,
106335 /* xRollback */ 0,
106336 /* xFindMethod */ 0,
106337 /* xRename */ 0,
106338 /* xSavepoint */ 0,
106339 /* xRelease */ 0,
106340 /* xRollbackTo */ 0,
106341 /* xShadowName */ 0,
106342 /* xIntegrity */ 0
106343};
106344
106345
106346SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
106347 int rc;
106348 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
106349 if( rc==SQLITE_OK0 ){
106350 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
106351 }
106352 return rc;
106353}
106354#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
106355SQLITE_PRIVATEstatic int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK0; }
106356#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
106357
106358/************** End of vdbevtab.c ********************************************/
106359/************** Begin file memjournal.c **************************************/
106360/*
106361** 2008 October 7
106362**
106363** The author disclaims copyright to this source code. In place of
106364** a legal notice, here is a blessing:
106365**
106366** May you do good and not evil.
106367** May you find forgiveness for yourself and forgive others.
106368** May you share freely, never taking more than you give.
106369**
106370*************************************************************************
106371**
106372** This file contains code use to implement an in-memory rollback journal.
106373** The in-memory rollback journal is used to journal transactions for
106374** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
106375**
106376** Update: The in-memory journal is also used to temporarily cache
106377** smaller journals that are not critical for power-loss recovery.
106378** For example, statement journals that are not too big will be held
106379** entirely in memory, thus reducing the number of file I/O calls, and
106380** more importantly, reducing temporary file creation events. If these
106381** journals become too large for memory, they are spilled to disk. But
106382** in the common case, they are usually small and no file I/O needs to
106383** occur.
106384*/
106385/* #include "sqliteInt.h" */
106386
106387/* Forward references to internal structures */
106388typedef struct MemJournal MemJournal;
106389typedef struct FilePoint FilePoint;
106390typedef struct FileChunk FileChunk;
106391
106392/*
106393** The rollback journal is composed of a linked list of these structures.
106394**
106395** The zChunk array is always at least 8 bytes in size - usually much more.
106396** Its actual size is stored in the MemJournal.nChunkSize variable.
106397*/
106398struct FileChunk {
106399 FileChunk *pNext; /* Next chunk in the journal */
106400 u8 zChunk[8]; /* Content of this chunk */
106401};
106402
106403/*
106404** By default, allocate this many bytes of memory for each FileChunk object.
106405*/
106406#define MEMJOURNAL_DFLT_FILECHUNKSIZE1024 1024
106407
106408/*
106409** For chunk size nChunkSize, return the number of bytes that should
106410** be allocated for each FileChunk structure.
106411*/
106412#define fileChunkSize(nChunkSize)(sizeof(FileChunk) + ((nChunkSize)-8)) (sizeof(FileChunk) + ((nChunkSize)-8))
106413
106414/*
106415** An instance of this object serves as a cursor into the rollback journal.
106416** The cursor can be either for reading or writing.
106417*/
106418struct FilePoint {
106419 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
106420 FileChunk *pChunk; /* Specific chunk into which cursor points */
106421};
106422
106423/*
106424** This structure is a subclass of sqlite3_file. Each open memory-journal
106425** is an instance of this class.
106426*/
106427struct MemJournal {
106428 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
106429 int nChunkSize; /* In-memory chunk-size */
106430
106431 int nSpill; /* Bytes of data before flushing */
106432 FileChunk *pFirst; /* Head of in-memory chunk-list */
106433 FilePoint endpoint; /* Pointer to the end of the file */
106434 FilePoint readpoint; /* Pointer to the end of the last xRead() */
106435
106436 int flags; /* xOpen flags */
106437 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
106438 const char *zJournal; /* Name of the journal file */
106439};
106440
106441/*
106442** Read data from the in-memory journal file. This is the implementation
106443** of the sqlite3_vfs.xRead method.
106444*/
106445static int memjrnlRead(
106446 sqlite3_file *pJfd, /* The journal file from which to read */
106447 void *zBuf, /* Put the results here */
106448 int iAmt, /* Number of bytes to read */
106449 sqlite_int64 iOfst /* Begin reading at this offset */
106450){
106451 MemJournal *p = (MemJournal *)pJfd;
106452 u8 *zOut = zBuf;
106453 int nRead = iAmt;
106454 int iChunkOffset;
106455 FileChunk *pChunk;
106456
106457 if( (iAmt+iOfst)>p->endpoint.iOffset ){
106458 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
106459 }
106460 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 )((void) (0));
106461 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
106462 sqlite3_int64 iOff = 0;
106463 for(pChunk=p->pFirst;
106464 ALWAYS(pChunk)(pChunk) && (iOff+p->nChunkSize)<=iOfst;
106465 pChunk=pChunk->pNext
106466 ){
106467 iOff += p->nChunkSize;
106468 }
106469 }else{
106470 pChunk = p->readpoint.pChunk;
106471 assert( pChunk!=0 )((void) (0));
106472 }
106473
106474 iChunkOffset = (int)(iOfst%p->nChunkSize);
106475 do {
106476 int iSpace = p->nChunkSize - iChunkOffset;
106477 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset))((nRead)<((p->nChunkSize - iChunkOffset))?(nRead):((p->
nChunkSize - iChunkOffset)))
;
106478 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
106479 zOut += nCopy;
106480 nRead -= iSpace;
106481 iChunkOffset = 0;
106482 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
106483 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
106484 p->readpoint.pChunk = pChunk;
106485
106486 return SQLITE_OK0;
106487}
106488
106489/*
106490** Free the list of FileChunk structures headed at MemJournal.pFirst.
106491*/
106492static void memjrnlFreeChunks(FileChunk *pFirst){
106493 FileChunk *pIter;
106494 FileChunk *pNext;
106495 for(pIter=pFirst; pIter; pIter=pNext){
106496 pNext = pIter->pNext;
106497 sqlite3_free(pIter);
106498 }
106499}
106500
106501/*
106502** Flush the contents of memory to a real file on disk.
106503*/
106504static int memjrnlCreateFile(MemJournal *p){
106505 int rc;
106506 sqlite3_file *pReal = (sqlite3_file*)p;
106507 MemJournal copy = *p;
106508
106509 memset(p, 0, sizeof(MemJournal));
106510 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
106511 if( rc==SQLITE_OK0 ){
106512 int nChunk = copy.nChunkSize;
106513 i64 iOff = 0;
106514 FileChunk *pIter;
106515 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
106516 if( iOff + nChunk > copy.endpoint.iOffset ){
106517 nChunk = copy.endpoint.iOffset - iOff;
106518 }
106519 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
106520 if( rc ) break;
106521 iOff += nChunk;
106522 }
106523 if( rc==SQLITE_OK0 ){
106524 /* No error has occurred. Free the in-memory buffers. */
106525 memjrnlFreeChunks(copy.pFirst);
106526 }
106527 }
106528 if( rc!=SQLITE_OK0 ){
106529 /* If an error occurred while creating or writing to the file, restore
106530 ** the original before returning. This way, SQLite uses the in-memory
106531 ** journal data to roll back changes made to the internal page-cache
106532 ** before this function was called. */
106533 sqlite3OsClose(pReal);
106534 *p = copy;
106535 }
106536 return rc;
106537}
106538
106539
106540/* Forward reference */
106541static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size);
106542
106543/*
106544** Write data to the file.
106545*/
106546static int memjrnlWrite(
106547 sqlite3_file *pJfd, /* The journal file into which to write */
106548 const void *zBuf, /* Take data to be written from here */
106549 int iAmt, /* Number of bytes to write */
106550 sqlite_int64 iOfst /* Begin writing at this offset into the file */
106551){
106552 MemJournal *p = (MemJournal *)pJfd;
106553 int nWrite = iAmt;
106554 u8 *zWrite = (u8 *)zBuf;
106555
106556 /* If the file should be created now, create it and write the new data
106557 ** into the file on disk. */
106558 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
106559 int rc = memjrnlCreateFile(p);
106560 if( rc==SQLITE_OK0 ){
106561 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
106562 }
106563 return rc;
106564 }
106565
106566 /* If the contents of this write should be stored in memory */
106567 else{
106568 /* An in-memory journal file should only ever be appended to. Random
106569 ** access writes are not required. The only exception to this is when
106570 ** the in-memory journal is being used by a connection using the
106571 ** atomic-write optimization. In this case the first 28 bytes of the
106572 ** journal file may be written as part of committing the transaction. */
106573 assert( iOfst<=p->endpoint.iOffset )((void) (0));
106574 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
106575 memjrnlTruncate(pJfd, iOfst);
106576 }
106577 if( iOfst==0 && p->pFirst ){
106578 assert( p->nChunkSize>iAmt )((void) (0));
106579 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
106580 }else{
106581 while( nWrite>0 ){
106582 FileChunk *pChunk = p->endpoint.pChunk;
106583 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
106584 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset)((nWrite)<(p->nChunkSize - iChunkOffset)?(nWrite):(p->
nChunkSize - iChunkOffset))
;
106585
106586 assert( pChunk!=0 || iChunkOffset==0 )((void) (0));
106587 if( iChunkOffset==0 ){
106588 /* New chunk is required to extend the file. */
106589 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)(sizeof(FileChunk) + ((p->nChunkSize)-8)));
106590 if( !pNew ){
106591 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
106592 }
106593 pNew->pNext = 0;
106594 if( pChunk ){
106595 assert( p->pFirst )((void) (0));
106596 pChunk->pNext = pNew;
106597 }else{
106598 assert( !p->pFirst )((void) (0));
106599 p->pFirst = pNew;
106600 }
106601 pChunk = p->endpoint.pChunk = pNew;
106602 }
106603
106604 assert( pChunk!=0 )((void) (0));
106605 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
106606 zWrite += iSpace;
106607 nWrite -= iSpace;
106608 p->endpoint.iOffset += iSpace;
106609 }
106610 }
106611 }
106612
106613 return SQLITE_OK0;
106614}
106615
106616/*
106617** Truncate the in-memory file.
106618*/
106619static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
106620 MemJournal *p = (MemJournal *)pJfd;
106621 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 )((void) (0));
106622 if( size<p->endpoint.iOffset ){
106623 FileChunk *pIter = 0;
106624 if( size==0 ){
106625 memjrnlFreeChunks(p->pFirst);
106626 p->pFirst = 0;
106627 }else{
106628 i64 iOff = p->nChunkSize;
106629 for(pIter=p->pFirst; ALWAYS(pIter)(pIter) && iOff<size; pIter=pIter->pNext){
106630 iOff += p->nChunkSize;
106631 }
106632 if( ALWAYS(pIter)(pIter) ){
106633 memjrnlFreeChunks(pIter->pNext);
106634 pIter->pNext = 0;
106635 }
106636 }
106637
106638 p->endpoint.pChunk = pIter;
106639 p->endpoint.iOffset = size;
106640 p->readpoint.pChunk = 0;
106641 p->readpoint.iOffset = 0;
106642 }
106643 return SQLITE_OK0;
106644}
106645
106646/*
106647** Close the file.
106648*/
106649static int memjrnlClose(sqlite3_file *pJfd){
106650 MemJournal *p = (MemJournal *)pJfd;
106651 memjrnlFreeChunks(p->pFirst);
106652 return SQLITE_OK0;
106653}
106654
106655/*
106656** Sync the file.
106657**
106658** If the real file has been created, call its xSync method. Otherwise,
106659** syncing an in-memory journal is a no-op.
106660*/
106661static int memjrnlSync(sqlite3_file *pJfd, int flags){
106662 UNUSED_PARAMETER2(pJfd, flags)(void)(pJfd),(void)(flags);
106663 return SQLITE_OK0;
106664}
106665
106666/*
106667** Query the size of the file in bytes.
106668*/
106669static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
106670 MemJournal *p = (MemJournal *)pJfd;
106671 *pSize = (sqlite_int64) p->endpoint.iOffset;
106672 return SQLITE_OK0;
106673}
106674
106675/*
106676** Table of methods for MemJournal sqlite3_file object.
106677*/
106678static const struct sqlite3_io_methods MemJournalMethods = {
106679 1, /* iVersion */
106680 memjrnlClose, /* xClose */
106681 memjrnlRead, /* xRead */
106682 memjrnlWrite, /* xWrite */
106683 memjrnlTruncate, /* xTruncate */
106684 memjrnlSync, /* xSync */
106685 memjrnlFileSize, /* xFileSize */
106686 0, /* xLock */
106687 0, /* xUnlock */
106688 0, /* xCheckReservedLock */
106689 0, /* xFileControl */
106690 0, /* xSectorSize */
106691 0, /* xDeviceCharacteristics */
106692 0, /* xShmMap */
106693 0, /* xShmLock */
106694 0, /* xShmBarrier */
106695 0, /* xShmUnmap */
106696 0, /* xFetch */
106697 0 /* xUnfetch */
106698};
106699
106700/*
106701** Open a journal file.
106702**
106703** The behaviour of the journal file depends on the value of parameter
106704** nSpill. If nSpill is 0, then the journal file is always create and
106705** accessed using the underlying VFS. If nSpill is less than zero, then
106706** all content is always stored in main-memory. Finally, if nSpill is a
106707** positive value, then the journal file is initially created in-memory
106708** but may be flushed to disk later on. In this case the journal file is
106709** flushed to disk either when it grows larger than nSpill bytes in size,
106710** or when sqlite3JournalCreate() is called.
106711*/
106712SQLITE_PRIVATEstatic int sqlite3JournalOpen(
106713 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
106714 const char *zName, /* Name of the journal file */
106715 sqlite3_file *pJfd, /* Preallocated, blank file handle */
106716 int flags, /* Opening flags */
106717 int nSpill /* Bytes buffered before opening the file */
106718){
106719 MemJournal *p = (MemJournal*)pJfd;
106720
106721 assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) )((void) (0));
106722
106723 /* Zero the file-handle object. If nSpill was passed zero, initialize
106724 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
106725 ** case none of the code in this module is executed as a result of calls
106726 ** made on the journal file-handle. */
106727 memset(p, 0, sizeof(MemJournal));
106728 if( nSpill==0 ){
106729 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
106730 }
106731
106732 if( nSpill>0 ){
106733 p->nChunkSize = nSpill;
106734 }else{
106735 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE1024 - sizeof(FileChunk);
106736 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) )((void) (0));
106737 }
106738
106739 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
106740 p->nSpill = nSpill;
106741 p->flags = flags;
106742 p->zJournal = zName;
106743 p->pVfs = pVfs;
106744 return SQLITE_OK0;
106745}
106746
106747/*
106748** Open an in-memory journal file.
106749*/
106750SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *pJfd){
106751 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
106752}
106753
106754#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
106755 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
106756/*
106757** If the argument p points to a MemJournal structure that is not an
106758** in-memory-only journal file (i.e. is one that was opened with a +ve
106759** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
106760** file has not yet been created, create it now.
106761*/
106762SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *pJfd){
106763 int rc = SQLITE_OK0;
106764 MemJournal *p = (MemJournal*)pJfd;
106765 if( pJfd->pMethods==&MemJournalMethods && (
106766#ifdef SQLITE_ENABLE_ATOMIC_WRITE
106767 p->nSpill>0
106768#else
106769 /* While this appears to not be possible without ATOMIC_WRITE, the
106770 ** paths are complex, so it seems prudent to leave the test in as
106771 ** a NEVER(), in case our analysis is subtly flawed. */
106772 NEVER(p->nSpill>0)(p->nSpill>0)
106773#endif
106774#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
106775 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL0x00000800)
106776#endif
106777 )){
106778 rc = memjrnlCreateFile(p);
106779 }
106780 return rc;
106781}
106782#endif
106783
106784/*
106785** The file-handle passed as the only argument is open on a journal file.
106786** Return true if this "journal file" is currently stored in heap memory,
106787** or false otherwise.
106788*/
106789SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p){
106790 return p->pMethods==&MemJournalMethods;
106791}
106792
106793/*
106794** Return the number of bytes required to store a JournalFile that uses vfs
106795** pVfs to create the underlying on-disk files.
106796*/
106797SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *pVfs){
106798 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal))((pVfs->szOsFile)>((int)sizeof(MemJournal))?(pVfs->szOsFile
):((int)sizeof(MemJournal)))
;
106799}
106800
106801/************** End of memjournal.c ******************************************/
106802/************** Begin file walker.c ******************************************/
106803/*
106804** 2008 August 16
106805**
106806** The author disclaims copyright to this source code. In place of
106807** a legal notice, here is a blessing:
106808**
106809** May you do good and not evil.
106810** May you find forgiveness for yourself and forgive others.
106811** May you share freely, never taking more than you give.
106812**
106813*************************************************************************
106814** This file contains routines used for walking the parser tree for
106815** an SQL statement.
106816*/
106817/* #include "sqliteInt.h" */
106818/* #include <stdlib.h> */
106819/* #include <string.h> */
106820
106821
106822#if !defined(SQLITE_OMIT_WINDOWFUNC)
106823/*
106824** Walk all expressions linked into the list of Window objects passed
106825** as the second argument.
106826*/
106827static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){
106828 Window *pWin;
106829 for(pWin=pList; pWin; pWin=pWin->pNextWin){
106830 int rc;
106831 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
106832 if( rc ) return WRC_Abort2;
106833 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
106834 if( rc ) return WRC_Abort2;
106835 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
106836 if( rc ) return WRC_Abort2;
106837 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
106838 if( rc ) return WRC_Abort2;
106839 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
106840 if( rc ) return WRC_Abort2;
106841 if( bOneOnly ) break;
106842 }
106843 return WRC_Continue0;
106844}
106845#endif
106846
106847/*
106848** Walk an expression tree. Invoke the callback once for each node
106849** of the expression, while descending. (In other words, the callback
106850** is invoked before visiting children.)
106851**
106852** The return value from the callback should be one of the WRC_*
106853** constants to specify how to proceed with the walk.
106854**
106855** WRC_Continue Continue descending down the tree.
106856**
106857** WRC_Prune Do not descend into child nodes, but allow
106858** the walk to continue with sibling nodes.
106859**
106860** WRC_Abort Do no more callbacks. Unwind the stack and
106861** return from the top-level walk call.
106862**
106863** The return value from this routine is WRC_Abort to abandon the tree walk
106864** and WRC_Continue to continue.
106865*/
106866SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3WalkExprNN(Walker *pWalker, Expr *pExpr){
106867 int rc;
106868 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
106869 testcase( ExprHasProperty(pExpr, EP_Reduced) );
106870 while(1){
106871 rc = pWalker->xExprCallback(pWalker, pExpr);
106872 if( rc ) return rc & WRC_Abort2;
106873 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&((0x010000|0x800000)))!=0) ){
106874 assert( pExpr->x.pList==0 || pExpr->pRight==0 )((void) (0));
106875 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){
106876 return WRC_Abort2;
106877 }
106878 if( pExpr->pRight ){
106879 assert( !ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
106880 pExpr = pExpr->pRight;
106881 continue;
106882 }else if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
106883 assert( !ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
106884 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort2;
106885 }else{
106886 if( pExpr->x.pList ){
106887 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort2;
106888 }
106889#ifndef SQLITE_OMIT_WINDOWFUNC
106890 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
106891 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort2;
106892 }
106893#endif
106894 }
106895 }
106896 break;
106897 }
106898 return WRC_Continue0;
106899}
106900SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
106901 return pExpr ? sqlite3WalkExprNN(pWalker,pExpr) : WRC_Continue0;
106902}
106903
106904/*
106905** Call sqlite3WalkExpr() for every expression in list p or until
106906** an abort request is seen.
106907*/
106908SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
106909 int i;
106910 struct ExprList_item *pItem;
106911 if( p ){
106912 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
106913 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort2;
106914 }
106915 }
106916 return WRC_Continue0;
106917}
106918
106919/*
106920** This is a no-op callback for Walker->xSelectCallback2. If this
106921** callback is set, then the Select->pWinDefn list is traversed.
106922*/
106923SQLITE_PRIVATEstatic void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){
106924 UNUSED_PARAMETER(pWalker)(void)(pWalker);
106925 UNUSED_PARAMETER(p)(void)(p);
106926 /* No-op */
106927}
106928
106929/*
106930** Walk all expressions associated with SELECT statement p. Do
106931** not invoke the SELECT callback on p, but do (of course) invoke
106932** any expr callbacks and SELECT callbacks that come from subqueries.
106933** Return WRC_Abort or WRC_Continue.
106934*/
106935SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
106936 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort2;
106937 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort2;
106938 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort2;
106939 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort2;
106940 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort2;
106941 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort2;
106942#if !defined(SQLITE_OMIT_WINDOWFUNC)
106943 if( p->pWinDefn ){
106944 Parse *pParse;
106945 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
106946 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT(pParse->eParseMode>=2))
106947#ifndef SQLITE_OMIT_CTE
106948 || pWalker->xSelectCallback2==sqlite3SelectPopWith
106949#endif
106950 ){
106951 /* The following may return WRC_Abort if there are unresolvable
106952 ** symbols (e.g. a table that does not exist) in a window definition. */
106953 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
106954 return rc;
106955 }
106956 }
106957#endif
106958 return WRC_Continue0;
106959}
106960
106961/*
106962** Walk the parse trees associated with all subqueries in the
106963** FROM clause of SELECT statement p. Do not invoke the select
106964** callback on p, but do invoke it on each FROM clause subquery
106965** and on any subqueries further down in the tree. Return
106966** WRC_Abort or WRC_Continue;
106967*/
106968SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
106969 SrcList *pSrc;
106970 int i;
106971 SrcItem *pItem;
106972
106973 pSrc = p->pSrc;
106974 if( ALWAYS(pSrc)(pSrc) ){
106975 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
106976 if( pItem->fg.isSubquery
106977 && sqlite3WalkSelect(pWalker, pItem->u4.pSubq->pSelect)
106978 ){
106979 return WRC_Abort2;
106980 }
106981 if( pItem->fg.isTabFunc
106982 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
106983 ){
106984 return WRC_Abort2;
106985 }
106986 }
106987 }
106988 return WRC_Continue0;
106989}
106990
106991/*
106992** Call sqlite3WalkExpr() for every expression in Select statement p.
106993** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
106994** on the compound select chain, p->pPrior.
106995**
106996** If it is not NULL, the xSelectCallback() callback is invoked before
106997** the walk of the expressions and FROM clause. The xSelectCallback2()
106998** method is invoked following the walk of the expressions and FROM clause,
106999** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
107000** and if the expressions and FROM clause both return WRC_Continue;
107001**
107002** Return WRC_Continue under normal conditions. Return WRC_Abort if
107003** there is an abort request.
107004**
107005** If the Walker does not have an xSelectCallback() then this routine
107006** is a no-op returning WRC_Continue.
107007*/
107008SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker *pWalker, Select *p){
107009 int rc;
107010 if( p==0 ) return WRC_Continue0;
107011 if( pWalker->xSelectCallback==0 ) return WRC_Continue0;
107012 do{
107013 rc = pWalker->xSelectCallback(pWalker, p);
107014 if( rc ) return rc & WRC_Abort2;
107015 if( sqlite3WalkSelectExpr(pWalker, p)
107016 || sqlite3WalkSelectFrom(pWalker, p)
107017 ){
107018 return WRC_Abort2;
107019 }
107020 if( pWalker->xSelectCallback2 ){
107021 pWalker->xSelectCallback2(pWalker, p);
107022 }
107023 p = p->pPrior;
107024 }while( p!=0 );
107025 return WRC_Continue0;
107026}
107027
107028/* Increase the walkerDepth when entering a subquery, and
107029** decrease when leaving the subquery.
107030*/
107031SQLITE_PRIVATEstatic int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){
107032 UNUSED_PARAMETER(pSelect)(void)(pSelect);
107033 pWalker->walkerDepth++;
107034 return WRC_Continue0;
107035}
107036SQLITE_PRIVATEstatic void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){
107037 UNUSED_PARAMETER(pSelect)(void)(pSelect);
107038 pWalker->walkerDepth--;
107039}
107040
107041
107042/*
107043** No-op routine for the parse-tree walker.
107044**
107045** When this routine is the Walker.xExprCallback then expression trees
107046** are walked without any actions being taken at each node. Presumably,
107047** when this routine is used for Walker.xExprCallback then
107048** Walker.xSelectCallback is set to do something useful for every
107049** subquery in the parser tree.
107050*/
107051SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
107052 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
107053 return WRC_Continue0;
107054}
107055
107056/*
107057** No-op routine for the parse-tree walker for SELECT statements.
107058** subquery in the parser tree.
107059*/
107060SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
107061 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
107062 return WRC_Continue0;
107063}
107064
107065/************** End of walker.c **********************************************/
107066/************** Begin file resolve.c *****************************************/
107067/*
107068** 2008 August 18
107069**
107070** The author disclaims copyright to this source code. In place of
107071** a legal notice, here is a blessing:
107072**
107073** May you do good and not evil.
107074** May you find forgiveness for yourself and forgive others.
107075** May you share freely, never taking more than you give.
107076**
107077*************************************************************************
107078**
107079** This file contains routines used for walking the parser tree and
107080** resolve all identifiers by associating them with a particular
107081** table and column.
107082*/
107083/* #include "sqliteInt.h" */
107084
107085/*
107086** Magic table number to mean the EXCLUDED table in an UPSERT statement.
107087*/
107088#define EXCLUDED_TABLE_NUMBER2 2
107089
107090/*
107091** Walk the expression tree pExpr and increase the aggregate function
107092** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
107093** This needs to occur when copying a TK_AGG_FUNCTION node from an
107094** outer query into an inner subquery.
107095**
107096** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
107097** is a helper function - a callback for the tree walker.
107098**
107099** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
107100*/
107101static int incrAggDepth(Walker *pWalker, Expr *pExpr){
107102 if( pExpr->op==TK_AGG_FUNCTION169 ) pExpr->op2 += pWalker->u.n;
107103 return WRC_Continue0;
107104}
107105static void incrAggFunctionDepth(Expr *pExpr, int N){
107106 if( N>0 ){
107107 Walker w;
107108 memset(&w, 0, sizeof(w));
107109 w.xExprCallback = incrAggDepth;
107110 w.u.n = N;
107111 sqlite3WalkExpr(&w, pExpr);
107112 }
107113}
107114
107115/*
107116** Turn the pExpr expression into an alias for the iCol-th column of the
107117** result set in pEList.
107118**
107119** If the reference is followed by a COLLATE operator, then make sure
107120** the COLLATE operator is preserved. For example:
107121**
107122** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
107123**
107124** Should be transformed into:
107125**
107126** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
107127**
107128** The nSubquery parameter specifies how many levels of subquery the
107129** alias is removed from the original expression. The usual value is
107130** zero but it might be more if the alias is contained within a subquery
107131** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
107132** structures must be increased by the nSubquery amount.
107133*/
107134static void resolveAlias(
107135 Parse *pParse, /* Parsing context */
107136 ExprList *pEList, /* A result set */
107137 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
107138 Expr *pExpr, /* Transform this into an alias to the result set */
107139 int nSubquery /* Number of subqueries that the label is moving */
107140){
107141 Expr *pOrig; /* The iCol-th column of the result set */
107142 Expr *pDup; /* Copy of pOrig */
107143 sqlite3 *db; /* The database connection */
107144
107145 assert( iCol>=0 && iCol<pEList->nExpr )((void) (0));
107146 pOrig = pEList->a[iCol].pExpr;
107147 assert( pOrig!=0 )((void) (0));
107148 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) )((void) (0));
107149 if( pExpr->pAggInfo ) return;
107150 db = pParse->db;
107151 pDup = sqlite3ExprDup(db, pOrig, 0);
107152 if( db->mallocFailed ){
107153 sqlite3ExprDelete(db, pDup);
107154 pDup = 0;
107155 }else{
107156 Expr temp;
107157 incrAggFunctionDepth(pDup, nSubquery);
107158 if( pExpr->op==TK_COLLATE114 ){
107159 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
107160 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
107161 }
107162 memcpy(&temp, pDup, sizeof(Expr));
107163 memcpy(pDup, pExpr, sizeof(Expr));
107164 memcpy(pExpr, &temp, sizeof(Expr));
107165 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
107166 if( ALWAYS(pExpr->y.pWin!=0)(pExpr->y.pWin!=0) ){
107167 pExpr->y.pWin->pOwner = pExpr;
107168 }
107169 }
107170 sqlite3ExprDeferredDelete(pParse, pDup);
107171 }
107172}
107173
107174/*
107175** Subqueries store the original database, table and column names for their
107176** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN",
107177** and mark the expression-list item by setting ExprList.a[].fg.eEName
107178** to ENAME_TAB.
107179**
107180** Check to see if the zSpan/eEName of the expression-list item passed to this
107181** routine matches the zDb, zTab, and zCol. If any of zDb, zTab, and zCol are
107182** NULL then those fields will match anything. Return true if there is a match,
107183** or false otherwise.
107184**
107185** SF_NestedFrom subqueries also store an entry for the implicit rowid (or
107186** _rowid_, or oid) column by setting ExprList.a[].fg.eEName to ENAME_ROWID,
107187** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
107188** argument matches if zCol is a rowid alias. If it is not NULL, (*pbRowid)
107189** is set to 1 if there is this kind of match.
107190*/
107191SQLITE_PRIVATEstatic int sqlite3MatchEName(
107192 const struct ExprList_item *pItem,
107193 const char *zCol,
107194 const char *zTab,
107195 const char *zDb,
107196 int *pbRowid
107197){
107198 int n;
107199 const char *zSpan;
107200 int eEName = pItem->fg.eEName;
107201 if( eEName!=ENAME_TAB2 && (eEName!=ENAME_ROWID3 || NEVER(pbRowid==0)(pbRowid==0)) ){
107202 return 0;
107203 }
107204 assert( pbRowid==0 || *pbRowid==0 )((void) (0));
107205 zSpan = pItem->zEName;
107206 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
107207 if( zDb && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
107208 return 0;
107209 }
107210 zSpan += n+1;
107211 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
107212 if( zTab && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
107213 return 0;
107214 }
107215 zSpan += n+1;
107216 if( zCol ){
107217 if( eEName==ENAME_TAB2 && sqlite3StrICmp(zSpan, zCol)!=0 ) return 0;
107218 if( eEName==ENAME_ROWID3 && sqlite3IsRowid(zCol)==0 ) return 0;
107219 }
107220 if( eEName==ENAME_ROWID3 ) *pbRowid = 1;
107221 return 1;
107222}
107223
107224/*
107225** Return TRUE if the double-quoted string mis-feature should be supported.
107226*/
107227static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
107228 if( db->init.busy ) return 1; /* Always support for legacy schemas */
107229 if( pTopNC->ncFlags & NC_IsDDL0x010000 ){
107230 /* Currently parsing a DDL statement */
107231 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML0x40000000)!=0 ){
107232 return 1;
107233 }
107234 return (db->flags & SQLITE_DqsDDL0x20000000)!=0;
107235 }else{
107236 /* Currently parsing a DML statement */
107237 return (db->flags & SQLITE_DqsDML0x40000000)!=0;
107238 }
107239}
107240
107241/*
107242** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
107243** return the appropriate colUsed mask.
107244*/
107245SQLITE_PRIVATEstatic Bitmask sqlite3ExprColUsed(Expr *pExpr){
107246 int n;
107247 Table *pExTab;
107248
107249 n = pExpr->iColumn;
107250 assert( ExprUseYTab(pExpr) )((void) (0));
107251 pExTab = pExpr->y.pTab;
107252 assert( pExTab!=0 )((void) (0));
107253 assert( n < pExTab->nCol )((void) (0));
107254 if( (pExTab->tabFlags & TF_HasGenerated0x00000060)!=0
107255 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED0x0060)!=0
107256 ){
107257 testcase( pExTab->nCol==BMS-1 );
107258 testcase( pExTab->nCol==BMS );
107259 return pExTab->nCol>=BMS((int)(sizeof(Bitmask)*8)) ? ALLBITS((Bitmask)-1) : MASKBIT(pExTab->nCol)(((Bitmask)1)<<(pExTab->nCol))-1;
107260 }else{
107261 testcase( n==BMS-1 );
107262 testcase( n==BMS );
107263 if( n>=BMS((int)(sizeof(Bitmask)*8)) ) n = BMS((int)(sizeof(Bitmask)*8))-1;
107264 return ((Bitmask)1)<<n;
107265 }
107266}
107267
107268/*
107269** Create a new expression term for the column specified by pMatch and
107270** iColumn. Append this new expression term to the FULL JOIN Match set
107271** in *ppList. Create a new *ppList if this is the first term in the
107272** set.
107273*/
107274static void extendFJMatch(
107275 Parse *pParse, /* Parsing context */
107276 ExprList **ppList, /* ExprList to extend */
107277 SrcItem *pMatch, /* Source table containing the column */
107278 i16 iColumn /* The column number */
107279){
107280 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN168, 0, 0);
107281 if( pNew ){
107282 pNew->iTable = pMatch->iCursor;
107283 pNew->iColumn = iColumn;
107284 pNew->y.pTab = pMatch->pSTab;
107285 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 )((void) (0));
107286 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(0x200000);
107287 *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew);
107288 }
107289}
107290
107291/*
107292** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
107293*/
107294static SQLITE_NOINLINE__attribute__((noinline)) int isValidSchemaTableName(
107295 const char *zTab, /* Name as it appears in the SQL */
107296 Table *pTab, /* The schema table we are trying to match */
107297 const char *zDb /* non-NULL if a database qualifier is present */
107298){
107299 const char *zLegacy;
107300 assert( pTab!=0 )((void) (0));
107301 assert( pTab->tnum==1 )((void) (0));
107302 if( sqlite3StrNICmpsqlite3_strnicmp(zTab, "sqlite_", 7)!=0 ) return 0;
107303 zLegacy = pTab->zName;
107304 if( strcmp(zLegacy+7, &LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master"[7])==0 ){
107305 if( sqlite3StrICmp(zTab+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0 ){
107306 return 1;
107307 }
107308 if( zDb==0 ) return 0;
107309 if( sqlite3StrICmp(zTab+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0 ) return 1;
107310 if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ) return 1;
107311 }else{
107312 if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ) return 1;
107313 }
107314 return 0;
107315}
107316
107317/*
107318** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
107319** that name in the set of source tables in pSrcList and make the pExpr
107320** expression node refer back to that source column. The following changes
107321** are made to pExpr:
107322**
107323** pExpr->iDb Set the index in db->aDb[] of the database X
107324** (even if X is implied).
107325** pExpr->iTable Set to the cursor number for the table obtained
107326** from pSrcList.
107327** pExpr->y.pTab Points to the Table structure of X.Y (even if
107328** X and/or Y are implied.)
107329** pExpr->iColumn Set to the column number within the table.
107330** pExpr->op Set to TK_COLUMN.
107331** pExpr->pLeft Any expression this points to is deleted
107332** pExpr->pRight Any expression this points to is deleted.
107333**
107334** The zDb variable is the name of the database (the "X"). This value may be
107335** NULL meaning that name is of the form Y.Z or Z. Any available database
107336** can be used. The zTable variable is the name of the table (the "Y"). This
107337** value can be NULL if zDb is also NULL. If zTable is NULL it
107338** means that the form of the name is Z and that columns from any table
107339** can be used.
107340**
107341** If the name cannot be resolved unambiguously, leave an error message
107342** in pParse and return WRC_Abort. Return WRC_Prune on success.
107343*/
107344static int lookupName(
107345 Parse *pParse, /* The parsing context */
107346 const char *zDb, /* Name of the database containing table, or NULL */
107347 const char *zTab, /* Name of table containing column, or NULL */
107348 const Expr *pRight, /* Name of the column. */
107349 NameContext *pNC, /* The name context used to resolve the name */
107350 Expr *pExpr /* Make this EXPR node point to the selected column */
107351){
107352 int i, j; /* Loop counters */
107353 int cnt = 0; /* Number of matching column names */
107354 int cntTab = 0; /* Number of potential "rowid" matches */
107355 int nSubquery = 0; /* How many levels of subquery */
107356 sqlite3 *db = pParse->db; /* The database connection */
107357 SrcItem *pItem; /* Use for looping over pSrcList items */
107358 SrcItem *pMatch = 0; /* The matching pSrcList item */
107359 NameContext *pTopNC = pNC; /* First namecontext in the list */
107360 Schema *pSchema = 0; /* Schema of the expression */
107361 int eNewExprOp = TK_COLUMN168; /* New value for pExpr->op on success */
107362 Table *pTab = 0; /* Table holding the row */
107363 Column *pCol; /* A column of pTab */
107364 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
107365 const char *zCol = pRight->u.zToken;
107366
107367 assert( pNC )((void) (0)); /* the name context cannot be NULL. */
107368 assert( zCol )((void) (0)); /* The Z in X.Y.Z cannot be NULL */
107369 assert( zDb==0 || zTab!=0 )((void) (0));
107370 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
107371
107372 /* Initialize the node to no-match */
107373 pExpr->iTable = -1;
107374 ExprSetVVAProperty(pExpr, EP_NoReduce);
107375
107376 /* Translate the schema name in zDb into a pointer to the corresponding
107377 ** schema. If not found, pSchema will remain NULL and nothing will match
107378 ** resulting in an appropriate error message toward the end of this routine
107379 */
107380 if( zDb ){
107381 testcase( pNC->ncFlags & NC_PartIdx );
107382 testcase( pNC->ncFlags & NC_IsCheck );
107383 if( (pNC->ncFlags & (NC_PartIdx0x000002|NC_IsCheck0x000004))!=0 ){
107384 /* Silently ignore database qualifiers inside CHECK constraints and
107385 ** partial indices. Do not raise errors because that might break
107386 ** legacy and because it does not hurt anything to just ignore the
107387 ** database name. */
107388 zDb = 0;
107389 }else{
107390 for(i=0; i<db->nDb; i++){
107391 assert( db->aDb[i].zDbSName )((void) (0));
107392 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
107393 pSchema = db->aDb[i].pSchema;
107394 break;
107395 }
107396 }
107397 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
107398 /* This branch is taken when the main database has been renamed
107399 ** using SQLITE_DBCONFIG_MAINDBNAME. */
107400 pSchema = db->aDb[0].pSchema;
107401 zDb = db->aDb[0].zDbSName;
107402 }
107403 }
107404 }
107405
107406 /* Start at the inner-most context and move outward until a match is found */
107407 assert( pNC && cnt==0 )((void) (0));
107408 do{
107409 ExprList *pEList;
107410 SrcList *pSrcList = pNC->pSrcList;
107411
107412 if( pSrcList ){
107413 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
107414 u8 hCol;
107415 pTab = pItem->pSTab;
107416 assert( pTab!=0 && pTab->zName!=0 )((void) (0));
107417 assert( pTab->nCol>0 || pParse->nErr )((void) (0));
107418 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem))((void) (0));
107419 if( pItem->fg.isNestedFrom ){
107420 /* In this case, pItem is a subquery that has been formed from a
107421 ** parenthesized subset of the FROM clause terms. Example:
107422 ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ...
107423 ** \_________________________/
107424 ** This pItem -------------^
107425 */
107426 int hit = 0;
107427 Select *pSel;
107428 assert( pItem->fg.isSubquery )((void) (0));
107429 assert( pItem->u4.pSubq!=0 )((void) (0));
107430 pSel = pItem->u4.pSubq->pSelect;
107431 assert( pSel!=0 )((void) (0));
107432 pEList = pSel->pEList;
107433 assert( pEList!=0 )((void) (0));
107434 assert( pEList->nExpr==pTab->nCol )((void) (0));
107435 for(j=0; j<pEList->nExpr; j++){
107436 int bRowid = 0; /* True if possible rowid match */
107437 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){
107438 continue;
107439 }
107440 if( bRowid==0 ){
107441 if( cnt>0 ){
107442 if( pItem->fg.isUsing==0
107443 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107444 ){
107445 /* Two or more tables have the same column name which is
107446 ** not joined by USING. This is an error. Signal as much
107447 ** by clearing pFJMatch and letting cnt go above 1. */
107448 sqlite3ExprListDelete(db, pFJMatch);
107449 pFJMatch = 0;
107450 }else
107451 if( (pItem->fg.jointype & JT_RIGHT0x10)==0 ){
107452 /* An INNER or LEFT JOIN. Use the left-most table */
107453 continue;
107454 }else
107455 if( (pItem->fg.jointype & JT_LEFT0x08)==0 ){
107456 /* A RIGHT JOIN. Use the right-most table */
107457 cnt = 0;
107458 sqlite3ExprListDelete(db, pFJMatch);
107459 pFJMatch = 0;
107460 }else{
107461 /* For a FULL JOIN, we must construct a coalesce() func */
107462 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107463 }
107464 }
107465 cnt++;
107466 hit = 1;
107467 }else if( cnt>0 ){
107468 /* This is a potential rowid match, but there has already been
107469 ** a real match found. So this can be ignored. */
107470 continue;
107471 }
107472 cntTab++;
107473 pMatch = pItem;
107474 pExpr->iColumn = j;
107475 pEList->a[j].fg.bUsed = 1;
107476
107477 /* rowid cannot be part of a USING clause - assert() this. */
107478 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 )((void) (0));
107479 if( pEList->a[j].fg.bUsingTerm ) break;
107480 }
107481 if( hit || zTab==0 ) continue;
107482 }
107483 assert( zDb==0 || zTab!=0 )((void) (0));
107484 if( zTab ){
107485 if( zDb ){
107486 if( pTab->pSchema!=pSchema ) continue;
107487 if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;
107488 }
107489 if( pItem->zAlias!=0 ){
107490 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
107491 continue;
107492 }
107493 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
107494 if( pTab->tnum!=1 ) continue;
107495 if( !isValidSchemaTableName(zTab, pTab, zDb) ) continue;
107496 }
107497 assert( ExprUseYTab(pExpr) )((void) (0));
107498 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zAlias ){
107499 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
107500 }
107501 }
107502 hCol = sqlite3StrIHash(zCol);
107503 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
107504 if( pCol->hName==hCol
107505 && sqlite3StrICmp(pCol->zCnName, zCol)==0
107506 ){
107507 if( cnt>0 ){
107508 if( pItem->fg.isUsing==0
107509 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107510 ){
107511 /* Two or more tables have the same column name which is
107512 ** not joined by USING. This is an error. Signal as much
107513 ** by clearing pFJMatch and letting cnt go above 1. */
107514 sqlite3ExprListDelete(db, pFJMatch);
107515 pFJMatch = 0;
107516 }else
107517 if( (pItem->fg.jointype & JT_RIGHT0x10)==0 ){
107518 /* An INNER or LEFT JOIN. Use the left-most table */
107519 continue;
107520 }else
107521 if( (pItem->fg.jointype & JT_LEFT0x08)==0 ){
107522 /* A RIGHT JOIN. Use the right-most table */
107523 cnt = 0;
107524 sqlite3ExprListDelete(db, pFJMatch);
107525 pFJMatch = 0;
107526 }else{
107527 /* For a FULL JOIN, we must construct a coalesce() func */
107528 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107529 }
107530 }
107531 cnt++;
107532 pMatch = pItem;
107533 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
107534 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
107535 if( pItem->fg.isNestedFrom ){
107536 sqlite3SrcItemColumnUsed(pItem, j);
107537 }
107538 break;
107539 }
107540 }
107541 if( 0==cnt && VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) ){
107542 /* pTab is a potential ROWID match. Keep track of it and match
107543 ** the ROWID later if that seems appropriate. (Search for "cntTab"
107544 ** to find related code.) Only allow a ROWID match if there is
107545 ** a single ROWID match candidate.
107546 */
107547#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
107548 /* In SQLITE_ALLOW_ROWID_IN_VIEW mode, allow a ROWID match
107549 ** if there is a single VIEW candidate or if there is a single
107550 ** non-VIEW candidate plus multiple VIEW candidates. In other
107551 ** words non-VIEW candidate terms take precedence over VIEWs.
107552 */
107553 if( cntTab==0
107554 || (cntTab==1
107555 && pMatch!=0
107556 && ALWAYS(pMatch->pSTab!=0)(pMatch->pSTab!=0)
107557 && (pMatch->pSTab->tabFlags & TF_Ephemeral0x00004000)!=0
107558 && (pTab->tabFlags & TF_Ephemeral0x00004000)==0)
107559 ){
107560 cntTab = 1;
107561 pMatch = pItem;
107562 }else{
107563 cntTab++;
107564 }
107565#else
107566 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
107567 ** simpler since we require exactly one candidate, which will
107568 ** always be a non-VIEW
107569 */
107570 cntTab++;
107571 pMatch = pItem;
107572#endif
107573 }
107574 }
107575 if( pMatch ){
107576 pExpr->iTable = pMatch->iCursor;
107577 assert( ExprUseYTab(pExpr) )((void) (0));
107578 pExpr->y.pTab = pMatch->pSTab;
107579 if( (pMatch->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40))!=0 ){
107580 ExprSetProperty(pExpr, EP_CanBeNull)(pExpr)->flags|=(0x200000);
107581 }
107582 pSchema = pExpr->y.pTab->pSchema;
107583 }
107584 } /* if( pSrcList ) */
107585
107586#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
107587 /* If we have not already resolved the name, then maybe
107588 ** it is a new.* or old.* trigger argument reference. Or
107589 ** maybe it is an excluded.* from an upsert. Or maybe it is
107590 ** a reference in the RETURNING clause to a table being modified.
107591 */
107592 if( cnt==0 && zDb==0 ){
107593 pTab = 0;
107594#ifndef SQLITE_OMIT_TRIGGER
107595 if( pParse->pTriggerTab!=0 ){
107596 int op = pParse->eTriggerOp;
107597 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT )((void) (0));
107598 if( pParse->bReturning ){
107599 if( (pNC->ncFlags & NC_UBaseReg0x000400)!=0
107600 && ALWAYS(zTab==0(zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName
)==0 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0
))
107601 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0(zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName
)==0 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0
))
107602 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))(zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName
)==0 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0
))
107603 ){
107604 pExpr->iTable = op!=TK_DELETE129;
107605 pTab = pParse->pTriggerTab;
107606 }
107607 }else if( op!=TK_DELETE129 && zTab && sqlite3StrICmp("new",zTab) == 0 ){
107608 pExpr->iTable = 1;
107609 pTab = pParse->pTriggerTab;
107610 }else if( op!=TK_INSERT128 && zTab && sqlite3StrICmp("old",zTab)==0 ){
107611 pExpr->iTable = 0;
107612 pTab = pParse->pTriggerTab;
107613 }
107614 }
107615#endif /* SQLITE_OMIT_TRIGGER */
107616#ifndef SQLITE_OMIT_UPSERT
107617 if( (pNC->ncFlags & NC_UUpsert0x000200)!=0 && zTab!=0 ){
107618 Upsert *pUpsert = pNC->uNC.pUpsert;
107619 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
107620 pTab = pUpsert->pUpsertSrc->a[0].pSTab;
107621 pExpr->iTable = EXCLUDED_TABLE_NUMBER2;
107622 }
107623 }
107624#endif /* SQLITE_OMIT_UPSERT */
107625
107626 if( pTab ){
107627 int iCol;
107628 u8 hCol = sqlite3StrIHash(zCol);
107629 pSchema = pTab->pSchema;
107630 cntTab++;
107631 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
107632 if( pCol->hName==hCol
107633 && sqlite3StrICmp(pCol->zCnName, zCol)==0
107634 ){
107635 if( iCol==pTab->iPKey ){
107636 iCol = -1;
107637 }
107638 break;
107639 }
107640 }
107641 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) ){
107642 /* IMP: R-51414-32910 */
107643 iCol = -1;
107644 }
107645 if( iCol<pTab->nCol ){
107646 cnt++;
107647 pMatch = 0;
107648#ifndef SQLITE_OMIT_UPSERT
107649 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER2 ){
107650 testcase( iCol==(-1) );
107651 assert( ExprUseYTab(pExpr) )((void) (0));
107652 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
107653 pExpr->iColumn = iCol;
107654 pExpr->y.pTab = pTab;
107655 eNewExprOp = TK_COLUMN168;
107656 }else{
107657 pExpr->iTable = pNC->uNC.pUpsert->regData +
107658 sqlite3TableColumnToStorage(pTab, iCol);
107659 eNewExprOp = TK_REGISTER176;
107660 }
107661 }else
107662#endif /* SQLITE_OMIT_UPSERT */
107663 {
107664 assert( ExprUseYTab(pExpr) )((void) (0));
107665 pExpr->y.pTab = pTab;
107666 if( pParse->bReturning ){
107667 eNewExprOp = TK_REGISTER176;
107668 pExpr->op2 = TK_COLUMN168;
107669 pExpr->iColumn = iCol;
107670 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
107671 sqlite3TableColumnToStorage(pTab, iCol) + 1;
107672 }else{
107673 pExpr->iColumn = (i16)iCol;
107674 eNewExprOp = TK_TRIGGER78;
107675#ifndef SQLITE_OMIT_TRIGGER
107676 if( iCol<0 ){
107677 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
107678 }else if( pExpr->iTable==0 ){
107679 testcase( iCol==31 );
107680 testcase( iCol==32 );
107681 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
107682 }else{
107683 testcase( iCol==31 );
107684 testcase( iCol==32 );
107685 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
107686 }
107687#endif /* SQLITE_OMIT_TRIGGER */
107688 }
107689 }
107690 }
107691 }
107692 }
107693#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
107694
107695 /*
107696 ** Perhaps the name is a reference to the ROWID
107697 */
107698 if( cnt==0
107699 && cntTab>=1
107700 && pMatch
107701 && (pNC->ncFlags & (NC_IdxExpr0x000020|NC_GenCol0x000008))==0
107702 && sqlite3IsRowid(zCol)
107703 && ALWAYS(VisibleRowid(pMatch->pSTab) || pMatch->fg.isNestedFrom)((((pMatch->pSTab)->tabFlags & 0x00000200)==0) || pMatch
->fg.isNestedFrom)
107704 ){
107705 cnt = cntTab;
107706#if SQLITE_ALLOW_ROWID_IN_VIEW+0==2
107707 if( pMatch->pSTab!=0 && IsView(pMatch->pSTab)((pMatch->pSTab)->eTabType==2) ){
107708 eNewExprOp = TK_NULL122;
107709 }
107710#endif
107711 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
107712 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
107713 }
107714
107715 /*
107716 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
107717 ** might refer to an result-set alias. This happens, for example, when
107718 ** we are resolving names in the WHERE clause of the following command:
107719 **
107720 ** SELECT a+b AS x FROM table WHERE x<10;
107721 **
107722 ** In cases like this, replace pExpr with a copy of the expression that
107723 ** forms the result set entry ("a+b" in the example) and return immediately.
107724 ** Note that the expression in the result set should have already been
107725 ** resolved by the time the WHERE clause is resolved.
107726 **
107727 ** The ability to use an output result-set column in the WHERE, GROUP BY,
107728 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
107729 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
107730 ** is supported for backwards compatibility only. Hence, we issue a warning
107731 ** on sqlite3_log() whenever the capability is used.
107732 */
107733 if( cnt==0
107734 && (pNC->ncFlags & NC_UEList0x000080)!=0
107735 && zTab==0
107736 ){
107737 pEList = pNC->uNC.pEList;
107738 assert( pEList!=0 )((void) (0));
107739 for(j=0; j<pEList->nExpr; j++){
107740 char *zAs = pEList->a[j].zEName;
107741 if( pEList->a[j].fg.eEName==ENAME_NAME0
107742 && sqlite3_stricmp(zAs, zCol)==0
107743 ){
107744 Expr *pOrig;
107745 assert( pExpr->pLeft==0 && pExpr->pRight==0 )((void) (0));
107746 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 )((void) (0));
107747 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 )((void) (0));
107748 pOrig = pEList->a[j].pExpr;
107749 if( (pNC->ncFlags&NC_AllowAgg0x000001)==0 && ExprHasProperty(pOrig, EP_Agg)(((pOrig)->flags&(0x000010))!=0) ){
107750 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
107751 return WRC_Abort2;
107752 }
107753 if( ExprHasProperty(pOrig, EP_Win)(((pOrig)->flags&(0x008000))!=0)
107754 && ((pNC->ncFlags&NC_AllowWin0x004000)==0 || pNC!=pTopNC )
107755 ){
107756 sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
107757 return WRC_Abort2;
107758 }
107759 if( sqlite3ExprVectorSize(pOrig)!=1 ){
107760 sqlite3ErrorMsg(pParse, "row value misused");
107761 return WRC_Abort2;
107762 }
107763 resolveAlias(pParse, pEList, j, pExpr, nSubquery);
107764 cnt = 1;
107765 pMatch = 0;
107766 assert( zTab==0 && zDb==0 )((void) (0));
107767 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
107768 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
107769 }
107770 goto lookupname_end;
107771 }
107772 }
107773 }
107774
107775 /* Advance to the next name context. The loop will exit when either
107776 ** we have a match (cnt>0) or when we run out of name contexts.
107777 */
107778 if( cnt ) break;
107779 pNC = pNC->pNext;
107780 nSubquery++;
107781 }while( pNC );
107782
107783
107784 /*
107785 ** If X and Y are NULL (in other words if only the column name Z is
107786 ** supplied) and the value of Z is enclosed in double-quotes, then
107787 ** Z is a string literal if it doesn't match any column names. In that
107788 ** case, we need to return right away and not make any changes to
107789 ** pExpr.
107790 **
107791 ** Because no reference was made to outer contexts, the pNC->nRef
107792 ** fields are not changed in any context.
107793 */
107794 if( cnt==0 && zTab==0 ){
107795 assert( pExpr->op==TK_ID )((void) (0));
107796 if( ExprHasProperty(pExpr,EP_DblQuoted)(((pExpr)->flags&(0x000080))!=0)
107797 && areDoubleQuotedStringsEnabled(db, pTopNC)
107798 ){
107799 /* If a double-quoted identifier does not match any known column name,
107800 ** then treat it as a string.
107801 **
107802 ** This hack was added in the early days of SQLite in a misguided attempt
107803 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
107804 ** I now sorely regret putting in this hack. The effect of this hack is
107805 ** that misspelled identifier names are silently converted into strings
107806 ** rather than causing an error, to the frustration of countless
107807 ** programmers. To all those frustrated programmers, my apologies.
107808 **
107809 ** Someday, I hope to get rid of this hack. Unfortunately there is
107810 ** a huge amount of legacy SQL that uses it. So for now, we just
107811 ** issue a warning.
107812 */
107813 sqlite3_log(SQLITE_WARNING28,
107814 "double-quoted string literal: \"%w\"", zCol);
107815#ifdef SQLITE_ENABLE_NORMALIZE
107816 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
107817#endif
107818 pExpr->op = TK_STRING118;
107819 memset(&pExpr->y, 0, sizeof(pExpr->y));
107820 return WRC_Prune1;
107821 }
107822 if( sqlite3ExprIdToTrueFalse(pExpr) ){
107823 return WRC_Prune1;
107824 }
107825 }
107826
107827 /*
107828 ** cnt==0 means there was not match.
107829 ** cnt>1 means there were two or more matches.
107830 **
107831 ** cnt==0 is always an error. cnt>1 is often an error, but might
107832 ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
107833 */
107834 assert( pFJMatch==0 || cnt>0 )((void) (0));
107835 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) )((void) (0));
107836 if( cnt!=1 ){
107837 const char *zErr;
107838 if( pFJMatch ){
107839 if( pFJMatch->nExpr==cnt-1 ){
107840 if( ExprHasProperty(pExpr,EP_Leaf)(((pExpr)->flags&(0x800000))!=0) ){
107841 ExprClearProperty(pExpr,EP_Leaf)(pExpr)->flags&=~(0x800000);
107842 }else{
107843 sqlite3ExprDelete(db, pExpr->pLeft);
107844 pExpr->pLeft = 0;
107845 sqlite3ExprDelete(db, pExpr->pRight);
107846 pExpr->pRight = 0;
107847 }
107848 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107849 pExpr->op = TK_FUNCTION172;
107850 pExpr->u.zToken = "coalesce";
107851 pExpr->x.pList = pFJMatch;
107852 cnt = 1;
107853 goto lookupname_end;
107854 }else{
107855 sqlite3ExprListDelete(db, pFJMatch);
107856 pFJMatch = 0;
107857 }
107858 }
107859 zErr = cnt==0 ? "no such column" : "ambiguous column name";
107860 if( zDb ){
107861 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
107862 }else if( zTab ){
107863 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
107864 }else if( cnt==0 && ExprHasProperty(pRight,EP_DblQuoted)(((pRight)->flags&(0x000080))!=0) ){
107865 sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"
107866 " string literal in single-quotes?",
107867 zErr, zCol);
107868 }else{
107869 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
107870 }
107871 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107872 pParse->checkSchema = 1;
107873 pTopNC->nNcErr++;
107874 eNewExprOp = TK_NULL122;
107875 }
107876 assert( pFJMatch==0 )((void) (0));
107877
107878 /* Remove all substructure from pExpr */
107879 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&((0x010000|0x800000)))!=0) ){
107880 sqlite3ExprDelete(db, pExpr->pLeft);
107881 pExpr->pLeft = 0;
107882 sqlite3ExprDelete(db, pExpr->pRight);
107883 pExpr->pRight = 0;
107884 ExprSetProperty(pExpr, EP_Leaf)(pExpr)->flags|=(0x800000);
107885 }
107886
107887 /* If a column from a table in pSrcList is referenced, then record
107888 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
107889 ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
107890 ** set if the 63rd or any subsequent column is used.
107891 **
107892 ** The colUsed mask is an optimization used to help determine if an
107893 ** index is a covering index. The correct answer is still obtained
107894 ** if the mask contains extra set bits. However, it is important to
107895 ** avoid setting bits beyond the maximum column number of the table.
107896 ** (See ticket [b92e5e8ec2cdbaa1]).
107897 **
107898 ** If a generated column is referenced, set bits for every column
107899 ** of the table.
107900 */
107901 if( pMatch ){
107902 if( pExpr->iColumn>=0 ){
107903 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
107904 }else{
107905 pMatch->fg.rowidUsed = 1;
107906 }
107907 }
107908
107909 pExpr->op = eNewExprOp;
107910lookupname_end:
107911 if( cnt==1 ){
107912 assert( pNC!=0 )((void) (0));
107913#ifndef SQLITE_OMIT_AUTHORIZATION
107914 if( pParse->db->xAuth
107915 && (pExpr->op==TK_COLUMN168 || pExpr->op==TK_TRIGGER78)
107916 ){
107917 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
107918 }
107919#endif
107920 /* Increment the nRef value on all name contexts from TopNC up to
107921 ** the point where the name matched. */
107922 for(;;){
107923 assert( pTopNC!=0 )((void) (0));
107924 pTopNC->nRef++;
107925 if( pTopNC==pNC ) break;
107926 pTopNC = pTopNC->pNext;
107927 }
107928 return WRC_Prune1;
107929 } else {
107930 return WRC_Abort2;
107931 }
107932}
107933
107934/*
107935** Allocate and return a pointer to an expression to load the column iCol
107936** from datasource iSrc in SrcList pSrc.
107937*/
107938SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
107939 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN168, 0, 0);
107940 if( p ){
107941 SrcItem *pItem = &pSrc->a[iSrc];
107942 Table *pTab;
107943 assert( ExprUseYTab(p) )((void) (0));
107944 pTab = p->y.pTab = pItem->pSTab;
107945 p->iTable = pItem->iCursor;
107946 if( p->y.pTab->iPKey==iCol ){
107947 p->iColumn = -1;
107948 }else{
107949 p->iColumn = (ynVar)iCol;
107950 if( (pTab->tabFlags & TF_HasGenerated0x00000060)!=0
107951 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED0x0060)!=0
107952 ){
107953 testcase( pTab->nCol==63 );
107954 testcase( pTab->nCol==64 );
107955 pItem->colUsed = pTab->nCol>=64 ? ALLBITS((Bitmask)-1) : MASKBIT(pTab->nCol)(((Bitmask)1)<<(pTab->nCol))-1;
107956 }else{
107957 testcase( iCol==BMS );
107958 testcase( iCol==BMS-1 );
107959 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS((int)(sizeof(Bitmask)*8)) ? BMS((int)(sizeof(Bitmask)*8))-1 : iCol);
107960 }
107961 }
107962 }
107963 return p;
107964}
107965
107966/*
107967** Report an error that an expression is not valid for some set of
107968** pNC->ncFlags values determined by validMask.
107969**
107970** static void notValid(
107971** Parse *pParse, // Leave error message here
107972** NameContext *pNC, // The name context
107973** const char *zMsg, // Type of error
107974** int validMask, // Set of contexts for which prohibited
107975** Expr *pExpr // Invalidate this expression on error
107976** ){...}
107977**
107978** As an optimization, since the conditional is almost always false
107979** (because errors are rare), the conditional is moved outside of the
107980** function call using a macro.
107981*/
107982static void notValidImpl(
107983 Parse *pParse, /* Leave error message here */
107984 NameContext *pNC, /* The name context */
107985 const char *zMsg, /* Type of error */
107986 Expr *pExpr, /* Invalidate this expression on error */
107987 Expr *pError /* Associate error with this expression */
107988){
107989 const char *zIn = "partial index WHERE clauses";
107990 if( pNC->ncFlags & NC_IdxExpr0x000020 ) zIn = "index expressions";
107991#ifndef SQLITE_OMIT_CHECK
107992 else if( pNC->ncFlags & NC_IsCheck0x000004 ) zIn = "CHECK constraints";
107993#endif
107994#ifndef SQLITE_OMIT_GENERATED_COLUMNS
107995 else if( pNC->ncFlags & NC_GenCol0x000008 ) zIn = "generated columns";
107996#endif
107997 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
107998 if( pExpr ) pExpr->op = TK_NULL122;
107999 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
108000}
108001#define sqlite3ResolveNotValid(P,N,M,X,E,R)((void) (0)); if( ((N)->ncFlags & (X))!=0 ) notValidImpl
(P,N,M,E,R);
\
108002 assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 )((void) (0)); \
108003 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
108004
108005/*
108006** Expression p should encode a floating point value between 1.0 and 0.0.
108007** Return 1024 times this value. Or return -1 if p is not a floating point
108008** value between 1.0 and 0.0.
108009*/
108010static int exprProbability(Expr *p){
108011 double r = -1.0;
108012 if( p->op!=TK_FLOAT154 ) return -1;
108013 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
108014 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF81);
108015 assert( r>=0.0 )((void) (0));
108016 if( r>1.0 ) return -1;
108017 return (int)(r*134217728.0);
108018}
108019
108020/*
108021** This routine is callback for sqlite3WalkExpr().
108022**
108023** Resolve symbolic names into TK_COLUMN operators for the current
108024** node in the expression tree. Return 0 to continue the search down
108025** the tree or 2 to abort the tree walk.
108026**
108027** This routine also does error checking and name resolution for
108028** function names. The operator for aggregate functions is changed
108029** to TK_AGG_FUNCTION.
108030*/
108031static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108032 NameContext *pNC;
108033 Parse *pParse;
108034
108035 pNC = pWalker->u.pNC;
108036 assert( pNC!=0 )((void) (0));
108037 pParse = pNC->pParse;
108038 assert( pParse==pWalker->pParse )((void) (0));
108039
108040#ifndef NDEBUG1
108041 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
108042 SrcList *pSrcList = pNC->pSrcList;
108043 int i;
108044 for(i=0; i<pNC->pSrcList->nSrc; i++){
108045 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab)((void) (0));
108046 }
108047 }
108048#endif
108049 switch( pExpr->op ){
108050
108051 /* The special operator TK_ROW means use the rowid for the first
108052 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
108053 ** clause processing on UPDATE and DELETE statements, and by
108054 ** UPDATE ... FROM statement processing.
108055 */
108056 case TK_ROW76: {
108057 SrcList *pSrcList = pNC->pSrcList;
108058 SrcItem *pItem;
108059 assert( pSrcList && pSrcList->nSrc>=1 )((void) (0));
108060 pItem = pSrcList->a;
108061 pExpr->op = TK_COLUMN168;
108062 assert( ExprUseYTab(pExpr) )((void) (0));
108063 pExpr->y.pTab = pItem->pSTab;
108064 pExpr->iTable = pItem->iCursor;
108065 pExpr->iColumn--;
108066 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
108067 break;
108068 }
108069
108070 /* An optimization: Attempt to convert
108071 **
108072 ** "expr IS NOT NULL" --> "TRUE"
108073 ** "expr IS NULL" --> "FALSE"
108074 **
108075 ** if we can prove that "expr" is never NULL. Call this the
108076 ** "NOT NULL strength reduction optimization".
108077 **
108078 ** If this optimization occurs, also restore the NameContext ref-counts
108079 ** to the state they where in before the "column" LHS expression was
108080 ** resolved. This prevents "column" from being counted as having been
108081 ** referenced, which might prevent a SELECT from being erroneously
108082 ** marked as correlated.
108083 **
108084 ** 2024-03-28: Beware of aggregates. A bare column of aggregated table
108085 ** can still evaluate to NULL even though it is marked as NOT NULL.
108086 ** Example:
108087 **
108088 ** CREATE TABLE t1(a INT NOT NULL);
108089 ** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1;
108090 **
108091 ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized
108092 ** here because at the time this case is hit, we do not yet know whether
108093 ** or not t1 is being aggregated. We have to assume the worst and omit
108094 ** the optimization. The only time it is safe to apply this optimization
108095 ** is within the WHERE clause.
108096 */
108097 case TK_NOTNULL52:
108098 case TK_ISNULL51: {
108099 int anRef[8];
108100 NameContext *p;
108101 int i;
108102 for(i=0, p=pNC; p && i<ArraySize(anRef)((int)(sizeof(anRef)/sizeof(anRef[0]))); p=p->pNext, i++){
108103 anRef[i] = p->nRef;
108104 }
108105 sqlite3WalkExpr(pWalker, pExpr->pLeft);
108106 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return WRC_Prune1;
108107 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
108108 /* The expression can be NULL. So the optimization does not apply */
108109 return WRC_Prune1;
108110 }
108111
108112 for(i=0, p=pNC; p; p=p->pNext, i++){
108113 if( (p->ncFlags & NC_Where0x100000)==0 ){
108114 return WRC_Prune1; /* Not in a WHERE clause. Unsafe to optimize. */
108115 }
108116 }
108117 testcase( ExprHasProperty(pExpr, EP_OuterON) );
108118 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
108119#if TREETRACE_ENABLED0
108120 if( sqlite3TreeTrace & 0x80000 ){
108121 sqlite3DebugPrintf(
108122 "NOT NULL strength reduction converts the following to %d:\n",
108123 pExpr->op==TK_NOTNULL52
108124 );
108125 sqlite3ShowExpr(pExpr);
108126 }
108127#endif /* TREETRACE_ENABLED */
108128 pExpr->u.iValue = (pExpr->op==TK_NOTNULL52);
108129 pExpr->flags |= EP_IntValue0x000800;
108130 pExpr->op = TK_INTEGER156;
108131 for(i=0, p=pNC; p && i<ArraySize(anRef)((int)(sizeof(anRef)/sizeof(anRef[0]))); p=p->pNext, i++){
108132 p->nRef = anRef[i];
108133 }
108134 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
108135 pExpr->pLeft = 0;
108136 return WRC_Prune1;
108137 }
108138
108139 /* A column name: ID
108140 ** Or table name and column name: ID.ID
108141 ** Or a database, table and column: ID.ID.ID
108142 **
108143 ** The TK_ID and TK_OUT cases are combined so that there will only
108144 ** be one call to lookupName(). Then the compiler will in-line
108145 ** lookupName() for a size reduction and performance increase.
108146 */
108147 case TK_ID60:
108148 case TK_DOT142: {
108149 const char *zTable;
108150 const char *zDb;
108151 Expr *pRight;
108152
108153 if( pExpr->op==TK_ID60 ){
108154 zDb = 0;
108155 zTable = 0;
108156 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
108157 pRight = pExpr;
108158 }else{
108159 Expr *pLeft = pExpr->pLeft;
108160 testcase( pNC->ncFlags & NC_IdxExpr );
108161 testcase( pNC->ncFlags & NC_GenCol );
108162 sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000008
))!=0 ) notValidImpl(pParse,pNC,"the \".\" operator",0,pExpr)
;
108163 NC_IdxExpr|NC_GenCol, 0, pExpr)((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000008
))!=0 ) notValidImpl(pParse,pNC,"the \".\" operator",0,pExpr)
;
;
108164 pRight = pExpr->pRight;
108165 if( pRight->op==TK_ID60 ){
108166 zDb = 0;
108167 }else{
108168 assert( pRight->op==TK_DOT )((void) (0));
108169 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
108170 zDb = pLeft->u.zToken;
108171 pLeft = pRight->pLeft;
108172 pRight = pRight->pRight;
108173 }
108174 assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) )((void) (0));
108175 zTable = pLeft->u.zToken;
108176 assert( ExprUseYTab(pExpr) )((void) (0));
108177 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108178 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
108179 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
108180 }
108181 }
108182 return lookupName(pParse, zDb, zTable, pRight, pNC, pExpr);
108183 }
108184
108185 /* Resolve function names
108186 */
108187 case TK_FUNCTION172: {
108188 ExprList *pList; /* The argument list */
108189 int n; /* Number of arguments */
108190 int no_such_func = 0; /* True if no such function exists */
108191 int wrong_num_args = 0; /* True if wrong number of arguments */
108192 int is_agg = 0; /* True if is an aggregate function */
108193 const char *zId; /* The function name. */
108194 FuncDef *pDef; /* Information about the function */
108195 u8 enc = ENC(pParse->db)((pParse->db)->enc); /* The database encoding */
108196 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg0x000001 | NC_AllowWin0x004000));
108197#ifndef SQLITE_OMIT_WINDOWFUNC
108198 Window *pWin = (IsWindowFunc(pExpr)( ((((pExpr))->flags&(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=167 )
? pExpr->y.pWin : 0);
108199#endif
108200 assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) )((void) (0));
108201 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER )((void) (0));
108202 pList = pExpr->x.pList;
108203 n = pList ? pList->nExpr : 0;
108204 zId = pExpr->u.zToken;
108205 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
108206 if( pDef==0 ){
108207 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
108208 if( pDef==0 ){
108209 no_such_func = 1;
108210 }else{
108211 wrong_num_args = 1;
108212 }
108213 }else{
108214 is_agg = pDef->xFinalize!=0;
108215 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY0x0400 ){
108216 ExprSetProperty(pExpr, EP_Unlikely)(pExpr)->flags|=(0x080000);
108217 if( n==2 ){
108218 pExpr->iTable = exprProbability(pList->a[1].pExpr);
108219 if( pExpr->iTable<0 ){
108220 sqlite3ErrorMsg(pParse,
108221 "second argument to %#T() must be a "
108222 "constant between 0.0 and 1.0", pExpr);
108223 pNC->nNcErr++;
108224 }
108225 }else{
108226 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
108227 ** equivalent to likelihood(X, 0.0625).
108228 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
108229 ** short-hand for likelihood(X,0.0625).
108230 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
108231 ** for likelihood(X,0.9375).
108232 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
108233 ** to likelihood(X,0.9375). */
108234 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
108235 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
108236 }
108237 }
108238#ifndef SQLITE_OMIT_AUTHORIZATION
108239 {
108240 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION31, 0,pDef->zName,0);
108241 if( auth!=SQLITE_OK0 ){
108242 if( auth==SQLITE_DENY1 ){
108243 sqlite3ErrorMsg(pParse, "not authorized to use function: %#T",
108244 pExpr);
108245 pNC->nNcErr++;
108246 }
108247 pExpr->op = TK_NULL122;
108248 return WRC_Prune1;
108249 }
108250 }
108251#endif
108252
108253 /* If the function may call sqlite3_value_subtype(), then set the
108254 ** EP_SubtArg flag on all of its argument expressions. This prevents
108255 ** where.c from replacing the expression with a value read from an
108256 ** index on the same expression, which will not have the correct
108257 ** subtype. Also set the flag if the function expression itself is
108258 ** an EP_SubtArg expression. In this case subtypes are required as
108259 ** the function may return a value with a subtype back to its
108260 ** caller using sqlite3_result_value(). */
108261 if( (pDef->funcFlags & SQLITE_SUBTYPE0x000100000)
108262 || ExprHasProperty(pExpr, EP_SubtArg)(((pExpr)->flags&(0x80000000))!=0)
108263 ){
108264 int ii;
108265 for(ii=0; ii<n; ii++){
108266 ExprSetProperty(pList->a[ii].pExpr, EP_SubtArg)(pList->a[ii].pExpr)->flags|=(0x80000000);
108267 }
108268 }
108269
108270 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000) ){
108271 /* For the purposes of the EP_ConstFunc flag, date and time
108272 ** functions and other functions that change slowly are considered
108273 ** constant because they are constant for the duration of one query.
108274 ** This allows them to be factored out of inner loops. */
108275 ExprSetProperty(pExpr,EP_ConstFunc)(pExpr)->flags|=(0x100000);
108276 }
108277 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT0x0800)==0 ){
108278 /* Clearly non-deterministic functions like random(), but also
108279 ** date/time functions that use 'now', and other functions like
108280 ** sqlite_version() that might change over time cannot be used
108281 ** in an index or generated column. Curiously, they can be used
108282 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
108283 ** all this. */
108284 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",((void) (0)); if( ((pNC)->ncFlags & (0x000020|0x000002
|0x000008))!=0 ) notValidImpl(pParse,pNC,"non-deterministic functions"
,0,pExpr);
108285 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);
;
108286 }else{
108287 assert( (NC_SelfRef & 0xff)==NC_SelfRef )((void) (0)); /* Must fit in 8 bits */
108288 pExpr->op2 = pNC->ncFlags & NC_SelfRef0x00002e;
108289 if( pNC->ncFlags & NC_FromDDL0x040000 ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(0x40000000);
108290 }
108291 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
108292 && pParse->nested==0
108293 && (pParse->db->mDbFlags & DBFLAG_InternalFunc0x0020)==0
108294 ){
108295 /* Internal-use-only functions are disallowed unless the
108296 ** SQL is being compiled using sqlite3NestedParse() or
108297 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
108298 ** used to activate internal functions for testing purposes */
108299 no_such_func = 1;
108300 pDef = 0;
108301 }else
108302 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT0x00080000|SQLITE_FUNC_UNSAFE0x00200000))!=0
108303 && !IN_RENAME_OBJECT(pParse->eParseMode>=2)
108304 ){
108305 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
108306 }
108307 }
108308
108309 if( 0==IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108310#ifndef SQLITE_OMIT_WINDOWFUNC
108311 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)((void) (0))
108312 || (pDef->xValue==0 && pDef->xInverse==0)((void) (0))
108313 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)((void) (0))
108314 )((void) (0));
108315 if( pDef && pDef->xValue==0 && pWin ){
108316 sqlite3ErrorMsg(pParse,
108317 "%#T() may not be used as a window function", pExpr
108318 );
108319 pNC->nNcErr++;
108320 }else if(
108321 (is_agg && (pNC->ncFlags & NC_AllowAgg0x000001)==0)
108322 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW0x00010000) && !pWin)
108323 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin0x004000)==0)
108324 ){
108325 const char *zType;
108326 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW0x00010000) || pWin ){
108327 zType = "window";
108328 }else{
108329 zType = "aggregate";
108330 }
108331 sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr);
108332 pNC->nNcErr++;
108333 is_agg = 0;
108334 }
108335#else
108336 if( (is_agg && (pNC->ncFlags & NC_AllowAgg0x000001)==0) ){
108337 sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr);
108338 pNC->nNcErr++;
108339 is_agg = 0;
108340 }
108341#endif
108342 else if( no_such_func && pParse->db->init.busy==0
108343#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
108344 && pParse->explain==0
108345#endif
108346 ){
108347 sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr);
108348 pNC->nNcErr++;
108349 }else if( wrong_num_args ){
108350 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()",
108351 pExpr);
108352 pNC->nNcErr++;
108353 }
108354#ifndef SQLITE_OMIT_WINDOWFUNC
108355 else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
108356 sqlite3ErrorMsg(pParse,
108357 "FILTER may not be used with non-aggregate %#T()",
108358 pExpr
108359 );
108360 pNC->nNcErr++;
108361 }
108362#endif
108363 else if( is_agg==0 && pExpr->pLeft ){
108364 sqlite3ExprOrderByAggregateError(pParse, pExpr);
108365 pNC->nNcErr++;
108366 }
108367 if( is_agg ){
108368 /* Window functions may not be arguments of aggregate functions.
108369 ** Or arguments of other window functions. But aggregate functions
108370 ** may be arguments for window functions. */
108371#ifndef SQLITE_OMIT_WINDOWFUNC
108372 pNC->ncFlags &= ~(NC_AllowWin0x004000 | (!pWin ? NC_AllowAgg0x000001 : 0));
108373#else
108374 pNC->ncFlags &= ~NC_AllowAgg0x000001;
108375#endif
108376 }
108377 }
108378 else if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) || pExpr->pLeft ){
108379 is_agg = 1;
108380 }
108381 sqlite3WalkExprList(pWalker, pList);
108382 if( is_agg ){
108383 if( pExpr->pLeft ){
108384 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
108385 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
108386 sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList);
108387 }
108388#ifndef SQLITE_OMIT_WINDOWFUNC
108389 if( pWin && pParse->nErr==0 ){
108390 Select *pSel = pNC->pWinSelect;
108391 assert( ExprUseYWin(pExpr) && pWin==pExpr->y.pWin )((void) (0));
108392 if( IN_RENAME_OBJECT(pParse->eParseMode>=2)==0 ){
108393 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
108394 if( pParse->db->mallocFailed ) break;
108395 }
108396 sqlite3WalkExprList(pWalker, pWin->pPartition);
108397 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
108398 sqlite3WalkExpr(pWalker, pWin->pFilter);
108399 sqlite3WindowLink(pSel, pWin);
108400 pNC->ncFlags |= NC_HasWin0x008000;
108401 }else
108402#endif /* SQLITE_OMIT_WINDOWFUNC */
108403 {
108404 NameContext *pNC2; /* For looping up thru outer contexts */
108405 pExpr->op = TK_AGG_FUNCTION169;
108406 pExpr->op2 = 0;
108407#ifndef SQLITE_OMIT_WINDOWFUNC
108408 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
108409 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
108410 }
108411#endif
108412 pNC2 = pNC;
108413 while( pNC2
108414 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
108415 ){
108416 pExpr->op2 += (1 + pNC2->nNestedSelect);
108417 pNC2 = pNC2->pNext;
108418 }
108419 assert( pDef!=0 || IN_RENAME_OBJECT )((void) (0));
108420 if( pNC2 && pDef ){
108421 pExpr->op2 += pNC2->nNestedSelect;
108422 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg )((void) (0));
108423 assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg )((void) (0));
108424 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
108425 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
108426 pNC2->ncFlags |= NC_HasAgg0x000010
108427 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER0x08000000)
108428 & (SQLITE_FUNC_MINMAX0x1000|SQLITE_FUNC_ANYORDER0x08000000));
108429 }
108430 }
108431 pNC->ncFlags |= savedAllowFlags;
108432 }
108433 /* FIX ME: Compute pExpr->affinity based on the expected return
108434 ** type of the function
108435 */
108436 return WRC_Prune1;
108437 }
108438#ifndef SQLITE_OMIT_SUBQUERY
108439 case TK_SELECT139:
108440 case TK_EXISTS20: testcase( pExpr->op==TK_EXISTS );
108441#endif
108442 case TK_IN50: {
108443 testcase( pExpr->op==TK_IN );
108444 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
108445 int nRef = pNC->nRef;
108446 testcase( pNC->ncFlags & NC_IsCheck );
108447 testcase( pNC->ncFlags & NC_PartIdx );
108448 testcase( pNC->ncFlags & NC_IdxExpr );
108449 testcase( pNC->ncFlags & NC_GenCol );
108450 assert( pExpr->x.pSelect )((void) (0));
108451 if( pNC->ncFlags & NC_SelfRef0x00002e ){
108452 notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr);
108453 }else{
108454 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
108455 }
108456 assert( pNC->nRef>=nRef )((void) (0));
108457 if( nRef!=pNC->nRef ){
108458 ExprSetProperty(pExpr, EP_VarSelect)(pExpr)->flags|=(0x000040);
108459 pExpr->x.pSelect->selFlags |= SF_Correlated0x20000000;
108460 }
108461 pNC->ncFlags |= NC_Subquery0x000040;
108462 }
108463 break;
108464 }
108465 case TK_VARIABLE157: {
108466 testcase( pNC->ncFlags & NC_IsCheck );
108467 testcase( pNC->ncFlags & NC_PartIdx );
108468 testcase( pNC->ncFlags & NC_IdxExpr );
108469 testcase( pNC->ncFlags & NC_GenCol );
108470 sqlite3ResolveNotValid(pParse, pNC, "parameters",((void) (0)); if( ((pNC)->ncFlags & (0x000004|0x000002
|0x000020|0x000008))!=0 ) notValidImpl(pParse,pNC,"parameters"
,pExpr,pExpr);
108471 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);
;
108472 break;
108473 }
108474 case TK_IS45:
108475 case TK_ISNOT46: {
108476 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
108477 assert( !ExprHasProperty(pExpr, EP_Reduced) )((void) (0));
108478 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
108479 ** and "x IS NOT FALSE". */
108480 if( ALWAYS(pRight)(pRight) && (pRight->op==TK_ID60 || pRight->op==TK_TRUEFALSE171) ){
108481 int rc = resolveExprStep(pWalker, pRight);
108482 if( rc==WRC_Abort2 ) return WRC_Abort2;
108483 if( pRight->op==TK_TRUEFALSE171 ){
108484 pExpr->op2 = pExpr->op;
108485 pExpr->op = TK_TRUTH175;
108486 return WRC_Continue0;
108487 }
108488 }
108489 /* no break */ deliberate_fall_through__attribute__((fallthrough));
108490 }
108491 case TK_BETWEEN49:
108492 case TK_EQ54:
108493 case TK_NE53:
108494 case TK_LT57:
108495 case TK_LE56:
108496 case TK_GT55:
108497 case TK_GE58: {
108498 int nLeft, nRight;
108499 if( pParse->db->mallocFailed ) break;
108500 assert( pExpr->pLeft!=0 )((void) (0));
108501 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
108502 if( pExpr->op==TK_BETWEEN49 ){
108503 assert( ExprUseXList(pExpr) )((void) (0));
108504 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
108505 if( nRight==nLeft ){
108506 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
108507 }
108508 }else{
108509 assert( pExpr->pRight!=0 )((void) (0));
108510 nRight = sqlite3ExprVectorSize(pExpr->pRight);
108511 }
108512 if( nLeft!=nRight ){
108513 testcase( pExpr->op==TK_EQ );
108514 testcase( pExpr->op==TK_NE );
108515 testcase( pExpr->op==TK_LT );
108516 testcase( pExpr->op==TK_LE );
108517 testcase( pExpr->op==TK_GT );
108518 testcase( pExpr->op==TK_GE );
108519 testcase( pExpr->op==TK_IS );
108520 testcase( pExpr->op==TK_ISNOT );
108521 testcase( pExpr->op==TK_BETWEEN );
108522 sqlite3ErrorMsg(pParse, "row value misused");
108523 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
108524 }
108525 break;
108526 }
108527 }
108528 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
108529 return pParse->nErr ? WRC_Abort2 : WRC_Continue0;
108530}
108531
108532/*
108533** pEList is a list of expressions which are really the result set of the
108534** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
108535** This routine checks to see if pE is a simple identifier which corresponds
108536** to the AS-name of one of the terms of the expression list. If it is,
108537** this routine return an integer between 1 and N where N is the number of
108538** elements in pEList, corresponding to the matching entry. If there is
108539** no match, or if pE is not a simple identifier, then this routine
108540** return 0.
108541**
108542** pEList has been resolved. pE has not.
108543*/
108544static int resolveAsName(
108545 Parse *pParse, /* Parsing context for error messages */
108546 ExprList *pEList, /* List of expressions to scan */
108547 Expr *pE /* Expression we are trying to match */
108548){
108549 int i; /* Loop counter */
108550
108551 UNUSED_PARAMETER(pParse)(void)(pParse);
108552
108553 if( pE->op==TK_ID60 ){
108554 const char *zCol;
108555 assert( !ExprHasProperty(pE, EP_IntValue) )((void) (0));
108556 zCol = pE->u.zToken;
108557 for(i=0; i<pEList->nExpr; i++){
108558 if( pEList->a[i].fg.eEName==ENAME_NAME0
108559 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
108560 ){
108561 return i+1;
108562 }
108563 }
108564 }
108565 return 0;
108566}
108567
108568/*
108569** pE is a pointer to an expression which is a single term in the
108570** ORDER BY of a compound SELECT. The expression has not been
108571** name resolved.
108572**
108573** At the point this routine is called, we already know that the
108574** ORDER BY term is not an integer index into the result set. That
108575** case is handled by the calling routine.
108576**
108577** Attempt to match pE against result set columns in the left-most
108578** SELECT statement. Return the index i of the matching column,
108579** as an indication to the caller that it should sort by the i-th column.
108580** The left-most column is 1. In other words, the value returned is the
108581** same integer value that would be used in the SQL statement to indicate
108582** the column.
108583**
108584** If there is no match, return 0. Return -1 if an error occurs.
108585*/
108586static int resolveOrderByTermToExprList(
108587 Parse *pParse, /* Parsing context for error messages */
108588 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
108589 Expr *pE /* The specific ORDER BY term */
108590){
108591 int i; /* Loop counter */
108592 ExprList *pEList; /* The columns of the result set */
108593 NameContext nc; /* Name context for resolving pE */
108594 sqlite3 *db; /* Database connection */
108595 int rc; /* Return code from subprocedures */
108596 u8 savedSuppErr; /* Saved value of db->suppressErr */
108597
108598 assert( sqlite3ExprIsInteger(pE, &i, 0)==0 )((void) (0));
108599 pEList = pSelect->pEList;
108600
108601 /* Resolve all names in the ORDER BY term expression
108602 */
108603 memset(&nc, 0, sizeof(nc));
108604 nc.pParse = pParse;
108605 nc.pSrcList = pSelect->pSrc;
108606 nc.uNC.pEList = pEList;
108607 nc.ncFlags = NC_AllowAgg0x000001|NC_UEList0x000080|NC_NoSelect0x080000;
108608 nc.nNcErr = 0;
108609 db = pParse->db;
108610 savedSuppErr = db->suppressErr;
108611 db->suppressErr = 1;
108612 rc = sqlite3ResolveExprNames(&nc, pE);
108613 db->suppressErr = savedSuppErr;
108614 if( rc ) return 0;
108615
108616 /* Try to match the ORDER BY expression against an expression
108617 ** in the result set. Return an 1-based index of the matching
108618 ** result-set entry.
108619 */
108620 for(i=0; i<pEList->nExpr; i++){
108621 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
108622 return i+1;
108623 }
108624 }
108625
108626 /* If no match, return 0. */
108627 return 0;
108628}
108629
108630/*
108631** Generate an ORDER BY or GROUP BY term out-of-range error.
108632*/
108633static void resolveOutOfRangeError(
108634 Parse *pParse, /* The error context into which to write the error */
108635 const char *zType, /* "ORDER" or "GROUP" */
108636 int i, /* The index (1-based) of the term out of range */
108637 int mx, /* Largest permissible value of i */
108638 Expr *pError /* Associate the error with the expression */
108639){
108640 sqlite3ErrorMsg(pParse,
108641 "%r %s BY term out of range - should be "
108642 "between 1 and %d", i, zType, mx);
108643 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
108644}
108645
108646/*
108647** Analyze the ORDER BY clause in a compound SELECT statement. Modify
108648** each term of the ORDER BY clause is a constant integer between 1
108649** and N where N is the number of columns in the compound SELECT.
108650**
108651** ORDER BY terms that are already an integer between 1 and N are
108652** unmodified. ORDER BY terms that are integers outside the range of
108653** 1 through N generate an error. ORDER BY terms that are expressions
108654** are matched against result set expressions of compound SELECT
108655** beginning with the left-most SELECT and working toward the right.
108656** At the first match, the ORDER BY expression is transformed into
108657** the integer column number.
108658**
108659** Return the number of errors seen.
108660*/
108661static int resolveCompoundOrderBy(
108662 Parse *pParse, /* Parsing context. Leave error messages here */
108663 Select *pSelect /* The SELECT statement containing the ORDER BY */
108664){
108665 int i;
108666 ExprList *pOrderBy;
108667 ExprList *pEList;
108668 sqlite3 *db;
108669 int moreToDo = 1;
108670
108671 pOrderBy = pSelect->pOrderBy;
108672 if( pOrderBy==0 ) return 0;
108673 db = pParse->db;
108674 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
108675 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
108676 return 1;
108677 }
108678 for(i=0; i<pOrderBy->nExpr; i++){
108679 pOrderBy->a[i].fg.done = 0;
108680 }
108681 pSelect->pNext = 0;
108682 while( pSelect->pPrior ){
108683 pSelect->pPrior->pNext = pSelect;
108684 pSelect = pSelect->pPrior;
108685 }
108686 while( pSelect && moreToDo ){
108687 struct ExprList_item *pItem;
108688 moreToDo = 0;
108689 pEList = pSelect->pEList;
108690 assert( pEList!=0 )((void) (0));
108691 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108692 int iCol = -1;
108693 Expr *pE, *pDup;
108694 if( pItem->fg.done ) continue;
108695 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
108696 if( NEVER(pE==0)(pE==0) ) continue;
108697 if( sqlite3ExprIsInteger(pE, &iCol, 0) ){
108698 if( iCol<=0 || iCol>pEList->nExpr ){
108699 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
108700 return 1;
108701 }
108702 }else{
108703 iCol = resolveAsName(pParse, pEList, pE);
108704 if( iCol==0 ){
108705 /* Now test if expression pE matches one of the values returned
108706 ** by pSelect. In the usual case this is done by duplicating the
108707 ** expression, resolving any symbols in it, and then comparing
108708 ** it against each expression returned by the SELECT statement.
108709 ** Once the comparisons are finished, the duplicate expression
108710 ** is deleted.
108711 **
108712 ** If this is running as part of an ALTER TABLE operation and
108713 ** the symbols resolve successfully, also resolve the symbols in the
108714 ** actual expression. This allows the code in alter.c to modify
108715 ** column references within the ORDER BY expression as required. */
108716 pDup = sqlite3ExprDup(db, pE, 0);
108717 if( !db->mallocFailed ){
108718 assert(pDup)((void) (0));
108719 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
108720 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && iCol>0 ){
108721 resolveOrderByTermToExprList(pParse, pSelect, pE);
108722 }
108723 }
108724 sqlite3ExprDelete(db, pDup);
108725 }
108726 }
108727 if( iCol>0 ){
108728 /* Convert the ORDER BY term into an integer column number iCol,
108729 ** taking care to preserve the COLLATE clause if it exists. */
108730 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108731 Expr *pNew = sqlite3Expr(db, TK_INTEGER156, 0);
108732 if( pNew==0 ) return 1;
108733 pNew->flags |= EP_IntValue0x000800;
108734 pNew->u.iValue = iCol;
108735 if( pItem->pExpr==pE ){
108736 pItem->pExpr = pNew;
108737 }else{
108738 Expr *pParent = pItem->pExpr;
108739 assert( pParent->op==TK_COLLATE )((void) (0));
108740 while( pParent->pLeft->op==TK_COLLATE114 ) pParent = pParent->pLeft;
108741 assert( pParent->pLeft==pE )((void) (0));
108742 pParent->pLeft = pNew;
108743 }
108744 sqlite3ExprDelete(db, pE);
108745 pItem->u.x.iOrderByCol = (u16)iCol;
108746 }
108747 pItem->fg.done = 1;
108748 }else{
108749 moreToDo = 1;
108750 }
108751 }
108752 pSelect = pSelect->pNext;
108753 }
108754 for(i=0; i<pOrderBy->nExpr; i++){
108755 if( pOrderBy->a[i].fg.done==0 ){
108756 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
108757 "column in the result set", i+1);
108758 return 1;
108759 }
108760 }
108761 return 0;
108762}
108763
108764/*
108765** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
108766** the SELECT statement pSelect. If any term is reference to a
108767** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
108768** field) then convert that term into a copy of the corresponding result set
108769** column.
108770**
108771** If any errors are detected, add an error message to pParse and
108772** return non-zero. Return zero if no errors are seen.
108773*/
108774SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(
108775 Parse *pParse, /* Parsing context. Leave error messages here */
108776 Select *pSelect, /* The SELECT statement containing the clause */
108777 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
108778 const char *zType /* "ORDER" or "GROUP" */
108779){
108780 int i;
108781 sqlite3 *db = pParse->db;
108782 ExprList *pEList;
108783 struct ExprList_item *pItem;
108784
108785 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return 0;
108786 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
108787 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
108788 return 1;
108789 }
108790 pEList = pSelect->pEList;
108791 assert( pEList!=0 )((void) (0)); /* sqlite3SelectNew() guarantees this */
108792 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108793 if( pItem->u.x.iOrderByCol ){
108794 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
108795 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
108796 return 1;
108797 }
108798 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
108799 }
108800 }
108801 return 0;
108802}
108803
108804#ifndef SQLITE_OMIT_WINDOWFUNC
108805/*
108806** Walker callback for windowRemoveExprFromSelect().
108807*/
108808static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
108809 UNUSED_PARAMETER(pWalker)(void)(pWalker);
108810 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
108811 Window *pWin = pExpr->y.pWin;
108812 sqlite3WindowUnlinkFromSelect(pWin);
108813 }
108814 return WRC_Continue0;
108815}
108816
108817/*
108818** Remove any Window objects owned by the expression pExpr from the
108819** Select.pWin list of Select object pSelect.
108820*/
108821static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
108822 if( pSelect->pWin ){
108823 Walker sWalker;
108824 memset(&sWalker, 0, sizeof(Walker));
108825 sWalker.xExprCallback = resolveRemoveWindowsCb;
108826 sWalker.u.pSelect = pSelect;
108827 sqlite3WalkExpr(&sWalker, pExpr);
108828 }
108829}
108830#else
108831# define windowRemoveExprFromSelect(a, b)
108832#endif /* SQLITE_OMIT_WINDOWFUNC */
108833
108834/*
108835** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
108836** The Name context of the SELECT statement is pNC. zType is either
108837** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
108838**
108839** This routine resolves each term of the clause into an expression.
108840** If the order-by term is an integer I between 1 and N (where N is the
108841** number of columns in the result set of the SELECT) then the expression
108842** in the resolution is a copy of the I-th result-set expression. If
108843** the order-by term is an identifier that corresponds to the AS-name of
108844** a result-set expression, then the term resolves to a copy of the
108845** result-set expression. Otherwise, the expression is resolved in
108846** the usual way - using sqlite3ResolveExprNames().
108847**
108848** This routine returns the number of errors. If errors occur, then
108849** an appropriate error message might be left in pParse. (OOM errors
108850** excepted.)
108851*/
108852static int resolveOrderGroupBy(
108853 NameContext *pNC, /* The name context of the SELECT statement */
108854 Select *pSelect, /* The SELECT statement holding pOrderBy */
108855 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
108856 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
108857){
108858 int i, j; /* Loop counters */
108859 int iCol; /* Column number */
108860 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
108861 Parse *pParse; /* Parsing context */
108862 int nResult; /* Number of terms in the result set */
108863
108864 assert( pOrderBy!=0 )((void) (0));
108865 nResult = pSelect->pEList->nExpr;
108866 pParse = pNC->pParse;
108867 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108868 Expr *pE = pItem->pExpr;
108869 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
108870 if( NEVER(pE2==0)(pE2==0) ) continue;
108871 if( zType[0]!='G' ){
108872 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
108873 if( iCol>0 ){
108874 /* If an AS-name match is found, mark this ORDER BY column as being
108875 ** a copy of the iCol-th result-set column. The subsequent call to
108876 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
108877 ** copy of the iCol-th result-set expression. */
108878 pItem->u.x.iOrderByCol = (u16)iCol;
108879 continue;
108880 }
108881 }
108882 if( sqlite3ExprIsInteger(pE2, &iCol, 0) ){
108883 /* The ORDER BY term is an integer constant. Again, set the column
108884 ** number so that sqlite3ResolveOrderGroupBy() will convert the
108885 ** order-by term to a copy of the result-set expression */
108886 if( iCol<1 || iCol>0xffff ){
108887 resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
108888 return 1;
108889 }
108890 pItem->u.x.iOrderByCol = (u16)iCol;
108891 continue;
108892 }
108893
108894 /* Otherwise, treat the ORDER BY term as an ordinary expression */
108895 pItem->u.x.iOrderByCol = 0;
108896 if( sqlite3ResolveExprNames(pNC, pE) ){
108897 return 1;
108898 }
108899 for(j=0; j<pSelect->pEList->nExpr; j++){
108900 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
108901 /* Since this expression is being changed into a reference
108902 ** to an identical expression in the result set, remove all Window
108903 ** objects belonging to the expression from the Select.pWin list. */
108904 windowRemoveExprFromSelect(pSelect, pE);
108905 pItem->u.x.iOrderByCol = j+1;
108906 }
108907 }
108908 }
108909 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
108910}
108911
108912/*
108913** Resolve names in the SELECT statement p and all of its descendants.
108914*/
108915static int resolveSelectStep(Walker *pWalker, Select *p){
108916 NameContext *pOuterNC; /* Context that contains this SELECT */
108917 NameContext sNC; /* Name context of this SELECT */
108918 int isCompound; /* True if p is a compound select */
108919 int nCompound; /* Number of compound terms processed so far */
108920 Parse *pParse; /* Parsing context */
108921 int i; /* Loop counter */
108922 ExprList *pGroupBy; /* The GROUP BY clause */
108923 Select *pLeftmost; /* Left-most of SELECT of a compound */
108924 sqlite3 *db; /* Database connection */
108925
108926
108927 assert( p!=0 )((void) (0));
108928 if( p->selFlags & SF_Resolved0x0000004 ){
108929 return WRC_Prune1;
108930 }
108931 pOuterNC = pWalker->u.pNC;
108932 pParse = pWalker->pParse;
108933 db = pParse->db;
108934
108935 /* Normally sqlite3SelectExpand() will be called first and will have
108936 ** already expanded this SELECT. However, if this is a subquery within
108937 ** an expression, sqlite3ResolveExprNames() will be called without a
108938 ** prior call to sqlite3SelectExpand(). When that happens, let
108939 ** sqlite3SelectPrep() do all of the processing for this SELECT.
108940 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
108941 ** this routine in the correct order.
108942 */
108943 if( (p->selFlags & SF_Expanded0x0000040)==0 ){
108944 sqlite3SelectPrep(pParse, p, pOuterNC);
108945 return pParse->nErr ? WRC_Abort2 : WRC_Prune1;
108946 }
108947
108948 isCompound = p->pPrior!=0;
108949 nCompound = 0;
108950 pLeftmost = p;
108951 while( p ){
108952 assert( (p->selFlags & SF_Expanded)!=0 )((void) (0));
108953 assert( (p->selFlags & SF_Resolved)==0 )((void) (0));
108954 p->selFlags |= SF_Resolved0x0000004;
108955
108956 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
108957 ** are not allowed to refer to any names, so pass an empty NameContext.
108958 */
108959 memset(&sNC, 0, sizeof(sNC));
108960 sNC.pParse = pParse;
108961 sNC.pWinSelect = p;
108962 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
108963 return WRC_Abort2;
108964 }
108965
108966 /* If the SF_Converted flags is set, then this Select object was
108967 ** was created by the convertCompoundSelectToSubquery() function.
108968 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
108969 ** as if it were part of the sub-query, not the parent. This block
108970 ** moves the pOrderBy down to the sub-query. It will be moved back
108971 ** after the names have been resolved. */
108972 if( p->selFlags & SF_Converted0x0010000 ){
108973 Select *pSub;
108974 assert( p->pSrc->a[0].fg.isSubquery )((void) (0));
108975 assert( p->pSrc->a[0].u4.pSubq!=0 )((void) (0));
108976 pSub = p->pSrc->a[0].u4.pSubq->pSelect;
108977 assert( pSub!=0 )((void) (0));
108978 assert( p->pSrc->nSrc==1 && p->pOrderBy )((void) (0));
108979 assert( pSub->pPrior && pSub->pOrderBy==0 )((void) (0));
108980 pSub->pOrderBy = p->pOrderBy;
108981 p->pOrderBy = 0;
108982 }
108983
108984 /* Recursively resolve names in all subqueries in the FROM clause
108985 */
108986 if( pOuterNC ) pOuterNC->nNestedSelect++;
108987 for(i=0; i<p->pSrc->nSrc; i++){
108988 SrcItem *pItem = &p->pSrc->a[i];
108989 assert( pItem->zName!=0((void) (0))
108990 || pItem->fg.isSubquery )((void) (0)); /* Test of tag-20240424-1*/
108991 if( pItem->fg.isSubquery
108992 && (pItem->u4.pSubq->pSelect->selFlags & SF_Resolved0x0000004)==0
108993 ){
108994 int nRef = pOuterNC ? pOuterNC->nRef : 0;
108995 const char *zSavedContext = pParse->zAuthContext;
108996
108997 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
108998 sqlite3ResolveSelectNames(pParse, pItem->u4.pSubq->pSelect, pOuterNC);
108999 pParse->zAuthContext = zSavedContext;
109000 if( pParse->nErr ) return WRC_Abort2;
109001 assert( db->mallocFailed==0 )((void) (0));
109002
109003 /* If the number of references to the outer context changed when
109004 ** expressions in the sub-select were resolved, the sub-select
109005 ** is correlated. It is not required to check the refcount on any
109006 ** but the innermost outer context object, as lookupName() increments
109007 ** the refcount on all contexts between the current one and the
109008 ** context containing the column when it resolves a name. */
109009 if( pOuterNC ){
109010 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef )((void) (0));
109011 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
109012 }
109013 }
109014 }
109015 if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0)(pOuterNC->nNestedSelect>0) ){
109016 pOuterNC->nNestedSelect--;
109017 }
109018
109019 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
109020 ** resolve the result-set expression list.
109021 */
109022 sNC.ncFlags = NC_AllowAgg0x000001|NC_AllowWin0x004000;
109023 sNC.pSrcList = p->pSrc;
109024 sNC.pNext = pOuterNC;
109025
109026 /* Resolve names in the result set. */
109027 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort2;
109028 sNC.ncFlags &= ~NC_AllowWin0x004000;
109029
109030 /* If there are no aggregate functions in the result-set, and no GROUP BY
109031 ** expression, do not allow aggregates in any of the other expressions.
109032 */
109033 assert( (p->selFlags & SF_Aggregate)==0 )((void) (0));
109034 pGroupBy = p->pGroupBy;
109035 if( pGroupBy || (sNC.ncFlags & NC_HasAgg0x000010)!=0 ){
109036 assert( NC_MinMaxAgg==SF_MinMaxAgg )((void) (0));
109037 assert( NC_OrderAgg==SF_OrderByReqd )((void) (0));
109038 p->selFlags |= SF_Aggregate0x0000008 | (sNC.ncFlags&(NC_MinMaxAgg0x001000|NC_OrderAgg0x8000000));
109039 }else{
109040 sNC.ncFlags &= ~NC_AllowAgg0x000001;
109041 }
109042
109043 /* Add the output column list to the name-context before parsing the
109044 ** other expressions in the SELECT statement. This is so that
109045 ** expressions in the WHERE clause (etc.) can refer to expressions by
109046 ** aliases in the result set.
109047 **
109048 ** Minor point: If this is the case, then the expression will be
109049 ** re-evaluated for each reference to it.
109050 */
109051 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 )((void) (0));
109052 sNC.uNC.pEList = p->pEList;
109053 sNC.ncFlags |= NC_UEList0x000080;
109054 if( p->pHaving ){
109055 if( (p->selFlags & SF_Aggregate0x0000008)==0 ){
109056 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
109057 return WRC_Abort2;
109058 }
109059 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort2;
109060 }
109061 sNC.ncFlags |= NC_Where0x100000;
109062 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort2;
109063 sNC.ncFlags &= ~NC_Where0x100000;
109064
109065 /* Resolve names in table-valued-function arguments */
109066 for(i=0; i<p->pSrc->nSrc; i++){
109067 SrcItem *pItem = &p->pSrc->a[i];
109068 if( pItem->fg.isTabFunc
109069 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
109070 ){
109071 return WRC_Abort2;
109072 }
109073 }
109074
109075#ifndef SQLITE_OMIT_WINDOWFUNC
109076 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109077 Window *pWin;
109078 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
109079 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
109080 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
109081 ){
109082 return WRC_Abort2;
109083 }
109084 }
109085 }
109086#endif
109087
109088 /* The ORDER BY and GROUP BY clauses may not refer to terms in
109089 ** outer queries
109090 */
109091 sNC.pNext = 0;
109092 sNC.ncFlags |= NC_AllowAgg0x000001|NC_AllowWin0x004000;
109093
109094 /* If this is a converted compound query, move the ORDER BY clause from
109095 ** the sub-query back to the parent query. At this point each term
109096 ** within the ORDER BY clause has been transformed to an integer value.
109097 ** These integers will be replaced by copies of the corresponding result
109098 ** set expressions by the call to resolveOrderGroupBy() below. */
109099 if( p->selFlags & SF_Converted0x0010000 ){
109100 Select *pSub;
109101 assert( p->pSrc->a[0].fg.isSubquery )((void) (0));
109102 pSub = p->pSrc->a[0].u4.pSubq->pSelect;
109103 assert( pSub!=0 )((void) (0));
109104 p->pOrderBy = pSub->pOrderBy;
109105 pSub->pOrderBy = 0;
109106 }
109107
109108 /* Process the ORDER BY clause for singleton SELECT statements.
109109 ** The ORDER BY clause for compounds SELECT statements is handled
109110 ** below, after all of the result-sets for all of the elements of
109111 ** the compound have been resolved.
109112 **
109113 ** If there is an ORDER BY clause on a term of a compound-select other
109114 ** than the right-most term, then that is a syntax error. But the error
109115 ** is not detected until much later, and so we need to go ahead and
109116 ** resolve those symbols on the incorrect ORDER BY for consistency.
109117 */
109118 if( p->pOrderBy!=0
109119 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
109120 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
109121 ){
109122 return WRC_Abort2;
109123 }
109124 if( db->mallocFailed ){
109125 return WRC_Abort2;
109126 }
109127 sNC.ncFlags &= ~NC_AllowWin0x004000;
109128
109129 /* Resolve the GROUP BY clause. At the same time, make sure
109130 ** the GROUP BY clause does not contain aggregate functions.
109131 */
109132 if( pGroupBy ){
109133 struct ExprList_item *pItem;
109134
109135 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
109136 return WRC_Abort2;
109137 }
109138 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
109139 if( ExprHasProperty(pItem->pExpr, EP_Agg)(((pItem->pExpr)->flags&(0x000010))!=0) ){
109140 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
109141 "the GROUP BY clause");
109142 return WRC_Abort2;
109143 }
109144 }
109145 }
109146
109147 /* If this is part of a compound SELECT, check that it has the right
109148 ** number of expressions in the select list. */
109149 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
109150 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
109151 return WRC_Abort2;
109152 }
109153
109154 /* Advance to the next term of the compound
109155 */
109156 p = p->pPrior;
109157 nCompound++;
109158 }
109159
109160 /* Resolve the ORDER BY on a compound SELECT after all terms of
109161 ** the compound have been resolved.
109162 */
109163 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
109164 return WRC_Abort2;
109165 }
109166
109167 return WRC_Prune1;
109168}
109169
109170/*
109171** This routine walks an expression tree and resolves references to
109172** table columns and result-set columns. At the same time, do error
109173** checking on function usage and set a flag if any aggregate functions
109174** are seen.
109175**
109176** To resolve table columns references we look for nodes (or subtrees) of the
109177** form X.Y.Z or Y.Z or just Z where
109178**
109179** X: The name of a database. Ex: "main" or "temp" or
109180** the symbolic name assigned to an ATTACH-ed database.
109181**
109182** Y: The name of a table in a FROM clause. Or in a trigger
109183** one of the special names "old" or "new".
109184**
109185** Z: The name of a column in table Y.
109186**
109187** The node at the root of the subtree is modified as follows:
109188**
109189** Expr.op Changed to TK_COLUMN
109190** Expr.pTab Points to the Table object for X.Y
109191** Expr.iColumn The column index in X.Y. -1 for the rowid.
109192** Expr.iTable The VDBE cursor number for X.Y
109193**
109194**
109195** To resolve result-set references, look for expression nodes of the
109196** form Z (with no X and Y prefix) where the Z matches the right-hand
109197** size of an AS clause in the result-set of a SELECT. The Z expression
109198** is replaced by a copy of the left-hand side of the result-set expression.
109199** Table-name and function resolution occurs on the substituted expression
109200** tree. For example, in:
109201**
109202** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
109203**
109204** The "x" term of the order by is replaced by "a+b" to render:
109205**
109206** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
109207**
109208** Function calls are checked to make sure that the function is
109209** defined and that the correct number of arguments are specified.
109210** If the function is an aggregate function, then the NC_HasAgg flag is
109211** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
109212** If an expression contains aggregate functions then the EP_Agg
109213** property on the expression is set.
109214**
109215** An error message is left in pParse if anything is amiss. The number
109216** if errors is returned.
109217*/
109218SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(
109219 NameContext *pNC, /* Namespace to resolve expressions in. */
109220 Expr *pExpr /* The expression to be analyzed. */
109221){
109222 int savedHasAgg;
109223 Walker w;
109224
109225 if( pExpr==0 ) return SQLITE_OK0;
109226 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
109227 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
109228 w.pParse = pNC->pParse;
109229 w.xExprCallback = resolveExprStep;
109230 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect0x080000) ? 0 : resolveSelectStep;
109231 w.xSelectCallback2 = 0;
109232 w.u.pNC = pNC;
109233#if SQLITE_MAX_EXPR_DEPTH1000>0
109234 w.pParse->nHeight += pExpr->nHeight;
109235 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
109236 return SQLITE_ERROR1;
109237 }
109238#endif
109239 assert( pExpr!=0 )((void) (0));
109240 sqlite3WalkExprNN(&w, pExpr);
109241#if SQLITE_MAX_EXPR_DEPTH1000>0
109242 w.pParse->nHeight -= pExpr->nHeight;
109243#endif
109244 assert( EP_Agg==NC_HasAgg )((void) (0));
109245 assert( EP_Win==NC_HasWin )((void) (0));
109246 testcase( pNC->ncFlags & NC_HasAgg );
109247 testcase( pNC->ncFlags & NC_HasWin );
109248 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(pNC->ncFlags & (0x000010|0x008000)
)
;
109249 pNC->ncFlags |= savedHasAgg;
109250 return pNC->nNcErr>0 || w.pParse->nErr>0;
109251}
109252
109253/*
109254** Resolve all names for all expression in an expression list. This is
109255** just like sqlite3ResolveExprNames() except that it works for an expression
109256** list rather than a single expression.
109257**
109258** The return value is SQLITE_OK (0) for success or SQLITE_ERROR (1) for a
109259** failure.
109260*/
109261SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(
109262 NameContext *pNC, /* Namespace to resolve expressions in. */
109263 ExprList *pList /* The expression list to be analyzed. */
109264){
109265 int i;
109266 int savedHasAgg = 0;
109267 Walker w;
109268 if( pList==0 ) return SQLITE_OK0;
109269 w.pParse = pNC->pParse;
109270 w.xExprCallback = resolveExprStep;
109271 w.xSelectCallback = resolveSelectStep;
109272 w.xSelectCallback2 = 0;
109273 w.u.pNC = pNC;
109274 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
109275 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
109276 for(i=0; i<pList->nExpr; i++){
109277 Expr *pExpr = pList->a[i].pExpr;
109278 if( pExpr==0 ) continue;
109279#if SQLITE_MAX_EXPR_DEPTH1000>0
109280 w.pParse->nHeight += pExpr->nHeight;
109281 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
109282 return SQLITE_ERROR1;
109283 }
109284#endif
109285 sqlite3WalkExprNN(&w, pExpr);
109286#if SQLITE_MAX_EXPR_DEPTH1000>0
109287 w.pParse->nHeight -= pExpr->nHeight;
109288#endif
109289 assert( EP_Agg==NC_HasAgg )((void) (0));
109290 assert( EP_Win==NC_HasWin )((void) (0));
109291 testcase( pNC->ncFlags & NC_HasAgg );
109292 testcase( pNC->ncFlags & NC_HasWin );
109293 if( pNC->ncFlags & (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000) ){
109294 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(pNC->ncFlags & (0x000010|0x008000)
)
;
109295 savedHasAgg |= pNC->ncFlags &
109296 (NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
109297 pNC->ncFlags &= ~(NC_HasAgg0x000010|NC_MinMaxAgg0x001000|NC_HasWin0x008000|NC_OrderAgg0x8000000);
109298 }
109299 if( w.pParse->nErr>0 ) return SQLITE_ERROR1;
109300 }
109301 pNC->ncFlags |= savedHasAgg;
109302 return SQLITE_OK0;
109303}
109304
109305/*
109306** Resolve all names in all expressions of a SELECT and in all
109307** descendants of the SELECT, including compounds off of p->pPrior,
109308** subqueries in expressions, and subqueries used as FROM clause
109309** terms.
109310**
109311** See sqlite3ResolveExprNames() for a description of the kinds of
109312** transformations that occur.
109313**
109314** All SELECT statements should have been expanded using
109315** sqlite3SelectExpand() prior to invoking this routine.
109316*/
109317SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(
109318 Parse *pParse, /* The parser context */
109319 Select *p, /* The SELECT statement being coded. */
109320 NameContext *pOuterNC /* Name context for parent SELECT statement */
109321){
109322 Walker w;
109323
109324 assert( p!=0 )((void) (0));
109325 w.xExprCallback = resolveExprStep;
109326 w.xSelectCallback = resolveSelectStep;
109327 w.xSelectCallback2 = 0;
109328 w.pParse = pParse;
109329 w.u.pNC = pOuterNC;
109330 sqlite3WalkSelect(&w, p);
109331}
109332
109333/*
109334** Resolve names in expressions that can only reference a single table
109335** or which cannot reference any tables at all. Examples:
109336**
109337** "type" flag
109338** ------------
109339** (1) CHECK constraints NC_IsCheck
109340** (2) WHERE clauses on partial indices NC_PartIdx
109341** (3) Expressions in indexes on expressions NC_IdxExpr
109342** (4) Expression arguments to VACUUM INTO. 0
109343** (5) GENERATED ALWAYS as expressions NC_GenCol
109344**
109345** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
109346** nodes of the expression is set to -1 and the Expr.iColumn value is
109347** set to the column number. In case (4), TK_COLUMN nodes cause an error.
109348**
109349** Any errors cause an error message to be set in pParse.
109350*/
109351SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(
109352 Parse *pParse, /* Parsing context */
109353 Table *pTab, /* The table being referenced, or NULL */
109354 int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
109355 Expr *pExpr, /* Expression to resolve. May be NULL. */
109356 ExprList *pList /* Expression list to resolve. May be NULL. */
109357){
109358 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
109359 NameContext sNC; /* Name context for pParse->pNewTable */
109360 int rc;
109361
109362 assert( type==0 || pTab!=0 )((void) (0));
109363 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr((void) (0))
109364 || type==NC_GenCol || pTab==0 )((void) (0));
109365 memset(&sNC, 0, sizeof(sNC));
109366 memset(&sSrc, 0, sizeof(sSrc));
109367 if( pTab ){
109368 sSrc.nSrc = 1;
109369 sSrc.a[0].zName = pTab->zName;
109370 sSrc.a[0].pSTab = pTab;
109371 sSrc.a[0].iCursor = -1;
109372 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
109373 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
109374 ** schema elements */
109375 type |= NC_FromDDL0x040000;
109376 }
109377 }
109378 sNC.pParse = pParse;
109379 sNC.pSrcList = &sSrc;
109380 sNC.ncFlags = type | NC_IsDDL0x010000;
109381 if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK0 ) return rc;
109382 if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
109383 return rc;
109384}
109385
109386/************** End of resolve.c *********************************************/
109387/************** Begin file expr.c ********************************************/
109388/*
109389** 2001 September 15
109390**
109391** The author disclaims copyright to this source code. In place of
109392** a legal notice, here is a blessing:
109393**
109394** May you do good and not evil.
109395** May you find forgiveness for yourself and forgive others.
109396** May you share freely, never taking more than you give.
109397**
109398*************************************************************************
109399** This file contains routines used for analyzing expressions and
109400** for generating VDBE code that evaluates expressions in SQLite.
109401*/
109402/* #include "sqliteInt.h" */
109403
109404/* Forward declarations */
109405static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
109406static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
109407
109408/*
109409** Return the affinity character for a single column of a table.
109410*/
109411SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(const Table *pTab, int iCol){
109412 if( iCol<0 || NEVER(iCol>=pTab->nCol)(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER0x44;
109413 return pTab->aCol[iCol].affinity;
109414}
109415
109416/*
109417** Return the 'affinity' of the expression pExpr if any.
109418**
109419** If pExpr is a column, a reference to a column via an 'AS' alias,
109420** or a sub-select with a column as the return value, then the
109421** affinity of that column is returned. Otherwise, 0x00 is returned,
109422** indicating no affinity for the expression.
109423**
109424** i.e. the WHERE clause expressions in the following statements all
109425** have an affinity:
109426**
109427** CREATE TABLE t1(a);
109428** SELECT * FROM t1 WHERE a;
109429** SELECT a AS b FROM t1 WHERE b;
109430** SELECT * FROM t1 WHERE (select a from t1);
109431*/
109432SQLITE_PRIVATEstatic char sqlite3ExprAffinity(const Expr *pExpr){
109433 int op;
109434 op = pExpr->op;
109435 while( 1 /* exit-by-break */ ){
109436 if( op==TK_COLUMN168 || (op==TK_AGG_COLUMN170 && pExpr->y.pTab!=0) ){
109437 assert( ExprUseYTab(pExpr) )((void) (0));
109438 assert( pExpr->y.pTab!=0 )((void) (0));
109439 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
109440 }
109441 if( op==TK_SELECT139 ){
109442 assert( ExprUseXSelect(pExpr) )((void) (0));
109443 assert( pExpr->x.pSelect!=0 )((void) (0));
109444 assert( pExpr->x.pSelect->pEList!=0 )((void) (0));
109445 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 )((void) (0));
109446 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
109447 }
109448#ifndef SQLITE_OMIT_CAST
109449 if( op==TK_CAST36 ){
109450 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
109451 return sqlite3AffinityType(pExpr->u.zToken, 0);
109452 }
109453#endif
109454 if( op==TK_SELECT_COLUMN178 ){
109455 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) )((void) (0));
109456 assert( pExpr->iColumn < pExpr->iTable )((void) (0));
109457 assert( pExpr->iColumn >= 0 )((void) (0));
109458 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr )((void) (0));
109459 return sqlite3ExprAffinity(
109460 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
109461 );
109462 }
109463 if( op==TK_VECTOR177 ){
109464 assert( ExprUseXList(pExpr) )((void) (0));
109465 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
109466 }
109467 if( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow)(((pExpr)->flags&(0x002000|0x040000))!=0) ){
109468 assert( pExpr->op==TK_COLLATE((void) (0))
109469 || pExpr->op==TK_IF_NULL_ROW((void) (0))
109470 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) )((void) (0));
109471 pExpr = pExpr->pLeft;
109472 op = pExpr->op;
109473 continue;
109474 }
109475 if( op!=TK_REGISTER176 ) break;
109476 op = pExpr->op2;
109477 if( NEVER( op==TK_REGISTER )(op==176) ) break;
109478 }
109479 return pExpr->affExpr;
109480}
109481
109482/*
109483** Make a guess at all the possible datatypes of the result that could
109484** be returned by an expression. Return a bitmask indicating the answer:
109485**
109486** 0x01 Numeric
109487** 0x02 Text
109488** 0x04 Blob
109489**
109490** If the expression must return NULL, then 0x00 is returned.
109491*/
109492SQLITE_PRIVATEstatic int sqlite3ExprDataType(const Expr *pExpr){
109493 while( pExpr ){
109494 switch( pExpr->op ){
109495 case TK_COLLATE114:
109496 case TK_IF_NULL_ROW179:
109497 case TK_UPLUS173: {
109498 pExpr = pExpr->pLeft;
109499 break;
109500 }
109501 case TK_NULL122: {
109502 pExpr = 0;
109503 break;
109504 }
109505 case TK_STRING118: {
109506 return 0x02;
109507 }
109508 case TK_BLOB155: {
109509 return 0x04;
109510 }
109511 case TK_CONCAT112: {
109512 return 0x06;
109513 }
109514 case TK_VARIABLE157:
109515 case TK_AGG_FUNCTION169:
109516 case TK_FUNCTION172: {
109517 return 0x07;
109518 }
109519 case TK_COLUMN168:
109520 case TK_AGG_COLUMN170:
109521 case TK_SELECT139:
109522 case TK_CAST36:
109523 case TK_SELECT_COLUMN178:
109524 case TK_VECTOR177: {
109525 int aff = sqlite3ExprAffinity(pExpr);
109526 if( aff>=SQLITE_AFF_NUMERIC0x43 ) return 0x05;
109527 if( aff==SQLITE_AFF_TEXT0x42 ) return 0x06;
109528 return 0x07;
109529 }
109530 case TK_CASE158: {
109531 int res = 0;
109532 int ii;
109533 ExprList *pList = pExpr->x.pList;
109534 assert( ExprUseXList(pExpr) && pList!=0 )((void) (0));
109535 assert( pList->nExpr > 0)((void) (0));
109536 for(ii=1; ii<pList->nExpr; ii+=2){
109537 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
109538 }
109539 if( pList->nExpr % 2 ){
109540 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
109541 }
109542 return res;
109543 }
109544 default: {
109545 return 0x01;
109546 }
109547 } /* End of switch(op) */
109548 } /* End of while(pExpr) */
109549 return 0x00;
109550}
109551
109552/*
109553** Set the collating sequence for expression pExpr to be the collating
109554** sequence named by pToken. Return a pointer to a new Expr node that
109555** implements the COLLATE operator.
109556**
109557** If a memory allocation error occurs, that fact is recorded in pParse->db
109558** and the pExpr parameter is returned unchanged.
109559*/
109560SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(
109561 const Parse *pParse, /* Parsing context */
109562 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
109563 const Token *pCollName, /* Name of collating sequence */
109564 int dequote /* True to dequote pCollName */
109565){
109566 if( pCollName->n>0 ){
109567 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE114, pCollName, dequote);
109568 if( pNew ){
109569 pNew->pLeft = pExpr;
109570 pNew->flags |= EP_Collate0x000200|EP_Skip0x002000;
109571 pExpr = pNew;
109572 }
109573 }
109574 return pExpr;
109575}
109576SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(
109577 const Parse *pParse, /* Parsing context */
109578 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
109579 const char *zC /* The collating sequence name */
109580){
109581 Token s;
109582 assert( zC!=0 )((void) (0));
109583 sqlite3TokenInit(&s, (char*)zC);
109584 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
109585}
109586
109587/*
109588** Skip over any TK_COLLATE operators.
109589*/
109590SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr *pExpr){
109591 while( pExpr && ExprHasProperty(pExpr, EP_Skip)(((pExpr)->flags&(0x002000))!=0) ){
109592 assert( pExpr->op==TK_COLLATE )((void) (0));
109593 pExpr = pExpr->pLeft;
109594 }
109595 return pExpr;
109596}
109597
109598/*
109599** Skip over any TK_COLLATE operators and/or any unlikely()
109600** or likelihood() or likely() functions at the root of an
109601** expression.
109602*/
109603SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
109604 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely)(((pExpr)->flags&(0x002000|0x080000))!=0) ){
109605 if( ExprHasProperty(pExpr, EP_Unlikely)(((pExpr)->flags&(0x080000))!=0) ){
109606 assert( ExprUseXList(pExpr) )((void) (0));
109607 assert( pExpr->x.pList->nExpr>0 )((void) (0));
109608 assert( pExpr->op==TK_FUNCTION )((void) (0));
109609 pExpr = pExpr->x.pList->a[0].pExpr;
109610 }else if( pExpr->op==TK_COLLATE114 ){
109611 pExpr = pExpr->pLeft;
109612 }else{
109613 break;
109614 }
109615 }
109616 return pExpr;
109617}
109618
109619/*
109620** Return the collation sequence for the expression pExpr. If
109621** there is no defined collating sequence, return NULL.
109622**
109623** See also: sqlite3ExprNNCollSeq()
109624**
109625** The sqlite3ExprNNCollSeq() works the same exact that it returns the
109626** default collation if pExpr has no defined collation.
109627**
109628** The collating sequence might be determined by a COLLATE operator
109629** or by the presence of a column with a defined collating sequence.
109630** COLLATE operators take first precedence. Left operands take
109631** precedence over right operands.
109632*/
109633SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
109634 sqlite3 *db = pParse->db;
109635 CollSeq *pColl = 0;
109636 const Expr *p = pExpr;
109637 while( p ){
109638 int op = p->op;
109639 if( op==TK_REGISTER176 ) op = p->op2;
109640 if( (op==TK_AGG_COLUMN170 && p->y.pTab!=0)
109641 || op==TK_COLUMN168 || op==TK_TRIGGER78
109642 ){
109643 int j;
109644 assert( ExprUseYTab(p) )((void) (0));
109645 assert( p->y.pTab!=0 )((void) (0));
109646 if( (j = p->iColumn)>=0 ){
109647 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
109648 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
109649 }
109650 break;
109651 }
109652 if( op==TK_CAST36 || op==TK_UPLUS173 ){
109653 p = p->pLeft;
109654 continue;
109655 }
109656 if( op==TK_VECTOR177 ){
109657 assert( ExprUseXList(p) )((void) (0));
109658 p = p->x.pList->a[0].pExpr;
109659 continue;
109660 }
109661 if( op==TK_COLLATE114 ){
109662 assert( !ExprHasProperty(p, EP_IntValue) )((void) (0));
109663 pColl = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), 0, p->u.zToken);
109664 break;
109665 }
109666 if( p->flags & EP_Collate0x000200 ){
109667 if( p->pLeft && (p->pLeft->flags & EP_Collate0x000200)!=0 ){
109668 p = p->pLeft;
109669 }else{
109670 Expr *pNext = p->pRight;
109671 /* The Expr.x union is never used at the same time as Expr.pRight */
109672 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 )((void) (0));
109673 if( ExprUseXList(p)(((p)->flags&0x001000)==0) && p->x.pList!=0 && !db->mallocFailed ){
109674 int i;
109675 for(i=0; i<p->x.pList->nExpr; i++){
109676 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate)(((p->x.pList->a[i].pExpr)->flags&(0x000200))!=0
)
){
109677 pNext = p->x.pList->a[i].pExpr;
109678 break;
109679 }
109680 }
109681 }
109682 p = pNext;
109683 }
109684 }else{
109685 break;
109686 }
109687 }
109688 if( sqlite3CheckCollSeq(pParse, pColl) ){
109689 pColl = 0;
109690 }
109691 return pColl;
109692}
109693
109694/*
109695** Return the collation sequence for the expression pExpr. If
109696** there is no defined collating sequence, return a pointer to the
109697** default collation sequence.
109698**
109699** See also: sqlite3ExprCollSeq()
109700**
109701** The sqlite3ExprCollSeq() routine works the same except that it
109702** returns NULL if there is no defined collation.
109703*/
109704SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){
109705 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
109706 if( p==0 ) p = pParse->db->pDfltColl;
109707 assert( p!=0 )((void) (0));
109708 return p;
109709}
109710
109711/*
109712** Return TRUE if the two expressions have equivalent collating sequences.
109713*/
109714SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){
109715 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
109716 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
109717 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
109718}
109719
109720/*
109721** pExpr is an operand of a comparison operator. aff2 is the
109722** type affinity of the other operand. This routine returns the
109723** type affinity that should be used for the comparison operator.
109724*/
109725SQLITE_PRIVATEstatic char sqlite3CompareAffinity(const Expr *pExpr, char aff2){
109726 char aff1 = sqlite3ExprAffinity(pExpr);
109727 if( aff1>SQLITE_AFF_NONE0x40 && aff2>SQLITE_AFF_NONE0x40 ){
109728 /* Both sides of the comparison are columns. If one has numeric
109729 ** affinity, use that. Otherwise use no affinity.
109730 */
109731 if( sqlite3IsNumericAffinity(aff1)((aff1)>=0x43) || sqlite3IsNumericAffinity(aff2)((aff2)>=0x43) ){
109732 return SQLITE_AFF_NUMERIC0x43;
109733 }else{
109734 return SQLITE_AFF_BLOB0x41;
109735 }
109736 }else{
109737 /* One side is a column, the other is not. Use the columns affinity. */
109738 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE )((void) (0));
109739 return (aff1<=SQLITE_AFF_NONE0x40 ? aff2 : aff1) | SQLITE_AFF_NONE0x40;
109740 }
109741}
109742
109743/*
109744** pExpr is a comparison operator. Return the type affinity that should
109745** be applied to both operands prior to doing the comparison.
109746*/
109747static char comparisonAffinity(const Expr *pExpr){
109748 char aff;
109749 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||((void) (0))
109750 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||((void) (0))
109751 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT )((void) (0));
109752 assert( pExpr->pLeft )((void) (0));
109753 aff = sqlite3ExprAffinity(pExpr->pLeft);
109754 if( pExpr->pRight ){
109755 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
109756 }else if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
109757 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
109758 }else if( aff==0 ){
109759 aff = SQLITE_AFF_BLOB0x41;
109760 }
109761 return aff;
109762}
109763
109764/*
109765** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
109766** idx_affinity is the affinity of an indexed column. Return true
109767** if the index with affinity idx_affinity may be used to implement
109768** the comparison in pExpr.
109769*/
109770SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){
109771 char aff = comparisonAffinity(pExpr);
109772 if( aff<SQLITE_AFF_TEXT0x42 ){
109773 return 1;
109774 }
109775 if( aff==SQLITE_AFF_TEXT0x42 ){
109776 return idx_affinity==SQLITE_AFF_TEXT0x42;
109777 }
109778 return sqlite3IsNumericAffinity(idx_affinity)((idx_affinity)>=0x43);
109779}
109780
109781/*
109782** Return the P5 value that should be used for a binary comparison
109783** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
109784*/
109785static u8 binaryCompareP5(
109786 const Expr *pExpr1, /* Left operand */
109787 const Expr *pExpr2, /* Right operand */
109788 int jumpIfNull /* Extra flags added to P5 */
109789){
109790 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
109791 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
109792 return aff;
109793}
109794
109795/*
109796** Return a pointer to the collation sequence that should be used by
109797** a binary comparison operator comparing pLeft and pRight.
109798**
109799** If the left hand expression has a collating sequence type, then it is
109800** used. Otherwise the collation sequence for the right hand expression
109801** is used, or the default (BINARY) if neither expression has a collating
109802** type.
109803**
109804** Argument pRight (but not pLeft) may be a null pointer. In this case,
109805** it is not considered.
109806*/
109807SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(
109808 Parse *pParse,
109809 const Expr *pLeft,
109810 const Expr *pRight
109811){
109812 CollSeq *pColl;
109813 assert( pLeft )((void) (0));
109814 if( pLeft->flags & EP_Collate0x000200 ){
109815 pColl = sqlite3ExprCollSeq(pParse, pLeft);
109816 }else if( pRight && (pRight->flags & EP_Collate0x000200)!=0 ){
109817 pColl = sqlite3ExprCollSeq(pParse, pRight);
109818 }else{
109819 pColl = sqlite3ExprCollSeq(pParse, pLeft);
109820 if( !pColl ){
109821 pColl = sqlite3ExprCollSeq(pParse, pRight);
109822 }
109823 }
109824 return pColl;
109825}
109826
109827/* Expression p is a comparison operator. Return a collation sequence
109828** appropriate for the comparison operator.
109829**
109830** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
109831** However, if the OP_Commuted flag is set, then the order of the operands
109832** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
109833** correct collating sequence is found.
109834*/
109835SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){
109836 if( ExprHasProperty(p, EP_Commuted)(((p)->flags&(0x000400))!=0) ){
109837 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
109838 }else{
109839 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
109840 }
109841}
109842
109843/*
109844** Generate code for a comparison operator.
109845*/
109846static int codeCompare(
109847 Parse *pParse, /* The parsing (and code generating) context */
109848 Expr *pLeft, /* The left operand */
109849 Expr *pRight, /* The right operand */
109850 int opcode, /* The comparison opcode */
109851 int in1, int in2, /* Register holding operands */
109852 int dest, /* Jump here if true. */
109853 int jumpIfNull, /* If true, jump if either operand is NULL */
109854 int isCommuted /* The comparison has been commuted */
109855){
109856 int p5;
109857 int addr;
109858 CollSeq *p4;
109859
109860 if( pParse->nErr ) return 0;
109861 if( isCommuted ){
109862 p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft);
109863 }else{
109864 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
109865 }
109866 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
109867 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
109868 (void*)p4, P4_COLLSEQ(-2));
109869 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
109870 return addr;
109871}
109872
109873/*
109874** Return true if expression pExpr is a vector, or false otherwise.
109875**
109876** A vector is defined as any expression that results in two or more
109877** columns of result. Every TK_VECTOR node is an vector because the
109878** parser will not generate a TK_VECTOR with fewer than two entries.
109879** But a TK_SELECT might be either a vector or a scalar. It is only
109880** considered a vector if it has two or more result columns.
109881*/
109882SQLITE_PRIVATEstatic int sqlite3ExprIsVector(const Expr *pExpr){
109883 return sqlite3ExprVectorSize(pExpr)>1;
109884}
109885
109886/*
109887** If the expression passed as the only argument is of type TK_VECTOR
109888** return the number of expressions in the vector. Or, if the expression
109889** is a sub-select, return the number of columns in the sub-select. For
109890** any other type of expression, return 1.
109891*/
109892SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(const Expr *pExpr){
109893 u8 op = pExpr->op;
109894 if( op==TK_REGISTER176 ) op = pExpr->op2;
109895 if( op==TK_VECTOR177 ){
109896 assert( ExprUseXList(pExpr) )((void) (0));
109897 return pExpr->x.pList->nExpr;
109898 }else if( op==TK_SELECT139 ){
109899 assert( ExprUseXSelect(pExpr) )((void) (0));
109900 return pExpr->x.pSelect->pEList->nExpr;
109901 }else{
109902 return 1;
109903 }
109904}
109905
109906/*
109907** Return a pointer to a subexpression of pVector that is the i-th
109908** column of the vector (numbered starting with 0). The caller must
109909** ensure that i is within range.
109910**
109911** If pVector is really a scalar (and "scalar" here includes subqueries
109912** that return a single column!) then return pVector unmodified.
109913**
109914** pVector retains ownership of the returned subexpression.
109915**
109916** If the vector is a (SELECT ...) then the expression returned is
109917** just the expression for the i-th term of the result set, and may
109918** not be ready for evaluation because the table cursor has not yet
109919** been positioned.
109920*/
109921SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
109922 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR )((void) (0));
109923 if( sqlite3ExprIsVector(pVector) ){
109924 assert( pVector->op2==0 || pVector->op==TK_REGISTER )((void) (0));
109925 if( pVector->op==TK_SELECT139 || pVector->op2==TK_SELECT139 ){
109926 assert( ExprUseXSelect(pVector) )((void) (0));
109927 return pVector->x.pSelect->pEList->a[i].pExpr;
109928 }else{
109929 assert( ExprUseXList(pVector) )((void) (0));
109930 return pVector->x.pList->a[i].pExpr;
109931 }
109932 }
109933 return pVector;
109934}
109935
109936/*
109937** Compute and return a new Expr object which when passed to
109938** sqlite3ExprCode() will generate all necessary code to compute
109939** the iField-th column of the vector expression pVector.
109940**
109941** It is ok for pVector to be a scalar (as long as iField==0).
109942** In that case, this routine works like sqlite3ExprDup().
109943**
109944** The caller owns the returned Expr object and is responsible for
109945** ensuring that the returned value eventually gets freed.
109946**
109947** The caller retains ownership of pVector. If pVector is a TK_SELECT,
109948** then the returned object will reference pVector and so pVector must remain
109949** valid for the life of the returned object. If pVector is a TK_VECTOR
109950** or a scalar expression, then it can be deleted as soon as this routine
109951** returns.
109952**
109953** A trick to cause a TK_SELECT pVector to be deleted together with
109954** the returned Expr object is to attach the pVector to the pRight field
109955** of the returned TK_SELECT_COLUMN Expr object.
109956*/
109957SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(
109958 Parse *pParse, /* Parsing context */
109959 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
109960 int iField, /* Which column of the vector to return */
109961 int nField /* Total number of columns in the vector */
109962){
109963 Expr *pRet;
109964 if( pVector->op==TK_SELECT139 ){
109965 assert( ExprUseXSelect(pVector) )((void) (0));
109966 /* The TK_SELECT_COLUMN Expr node:
109967 **
109968 ** pLeft: pVector containing TK_SELECT. Not deleted.
109969 ** pRight: not used. But recursively deleted.
109970 ** iColumn: Index of a column in pVector
109971 ** iTable: 0 or the number of columns on the LHS of an assignment
109972 ** pLeft->iTable: First in an array of register holding result, or 0
109973 ** if the result is not yet computed.
109974 **
109975 ** sqlite3ExprDelete() specifically skips the recursive delete of
109976 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
109977 ** can be attached to pRight to cause this node to take ownership of
109978 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
109979 ** with the same pLeft pointer to the pVector, but only one of them
109980 ** will own the pVector.
109981 */
109982 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN178, 0, 0);
109983 if( pRet ){
109984 ExprSetProperty(pRet, EP_FullSize)(pRet)->flags|=(0x020000);
109985 pRet->iTable = nField;
109986 pRet->iColumn = iField;
109987 pRet->pLeft = pVector;
109988 }
109989 }else{
109990 if( pVector->op==TK_VECTOR177 ){
109991 Expr **ppVector;
109992 assert( ExprUseXList(pVector) )((void) (0));
109993 ppVector = &pVector->x.pList->a[iField].pExpr;
109994 pVector = *ppVector;
109995 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109996 /* This must be a vector UPDATE inside a trigger */
109997 *ppVector = 0;
109998 return pVector;
109999 }
110000 }
110001 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
110002 }
110003 return pRet;
110004}
110005
110006/*
110007** If expression pExpr is of type TK_SELECT, generate code to evaluate
110008** it. Return the register in which the result is stored (or, if the
110009** sub-select returns more than one column, the first in an array
110010** of registers in which the result is stored).
110011**
110012** If pExpr is not a TK_SELECT expression, return 0.
110013*/
110014static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
110015 int reg = 0;
110016#ifndef SQLITE_OMIT_SUBQUERY
110017 if( pExpr->op==TK_SELECT139 ){
110018 reg = sqlite3CodeSubselect(pParse, pExpr);
110019 }
110020#endif
110021 return reg;
110022}
110023
110024/*
110025** Argument pVector points to a vector expression - either a TK_VECTOR
110026** or TK_SELECT that returns more than one column. This function returns
110027** the register number of a register that contains the value of
110028** element iField of the vector.
110029**
110030** If pVector is a TK_SELECT expression, then code for it must have
110031** already been generated using the exprCodeSubselect() routine. In this
110032** case parameter regSelect should be the first in an array of registers
110033** containing the results of the sub-select.
110034**
110035** If pVector is of type TK_VECTOR, then code for the requested field
110036** is generated. In this case (*pRegFree) may be set to the number of
110037** a temporary register to be freed by the caller before returning.
110038**
110039** Before returning, output parameter (*ppExpr) is set to point to the
110040** Expr object corresponding to element iElem of the vector.
110041*/
110042static int exprVectorRegister(
110043 Parse *pParse, /* Parse context */
110044 Expr *pVector, /* Vector to extract element from */
110045 int iField, /* Field to extract from pVector */
110046 int regSelect, /* First in array of registers */
110047 Expr **ppExpr, /* OUT: Expression element */
110048 int *pRegFree /* OUT: Temp register to free */
110049){
110050 u8 op = pVector->op;
110051 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR )((void) (0));
110052 if( op==TK_REGISTER176 ){
110053 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
110054 return pVector->iTable+iField;
110055 }
110056 if( op==TK_SELECT139 ){
110057 assert( ExprUseXSelect(pVector) )((void) (0));
110058 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
110059 return regSelect+iField;
110060 }
110061 if( op==TK_VECTOR177 ){
110062 assert( ExprUseXList(pVector) )((void) (0));
110063 *ppExpr = pVector->x.pList->a[iField].pExpr;
110064 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
110065 }
110066 return 0;
110067}
110068
110069/*
110070** Expression pExpr is a comparison between two vector values. Compute
110071** the result of the comparison (1, 0, or NULL) and write that
110072** result into register dest.
110073**
110074** The caller must satisfy the following preconditions:
110075**
110076** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
110077** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
110078** otherwise: op==pExpr->op and p5==0
110079*/
110080static void codeVectorCompare(
110081 Parse *pParse, /* Code generator context */
110082 Expr *pExpr, /* The comparison operation */
110083 int dest, /* Write results into this register */
110084 u8 op, /* Comparison operator */
110085 u8 p5 /* SQLITE_NULLEQ or zero */
110086){
110087 Vdbe *v = pParse->pVdbe;
110088 Expr *pLeft = pExpr->pLeft;
110089 Expr *pRight = pExpr->pRight;
110090 int nLeft = sqlite3ExprVectorSize(pLeft);
110091 int i;
110092 int regLeft = 0;
110093 int regRight = 0;
110094 u8 opx = op;
110095 int addrCmp = 0;
110096 int addrDone = sqlite3VdbeMakeLabel(pParse);
110097 int isCommuted = ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0);
110098
110099 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
110100 if( pParse->nErr ) return;
110101 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
110102 sqlite3ErrorMsg(pParse, "row value misused");
110103 return;
110104 }
110105 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE((void) (0))
110106 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT((void) (0))
110107 || pExpr->op==TK_LT || pExpr->op==TK_GT((void) (0))
110108 || pExpr->op==TK_LE || pExpr->op==TK_GE((void) (0))
110109 )((void) (0));
110110 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)((void) (0))
110111 || (pExpr->op==TK_ISNOT && op==TK_NE) )((void) (0));
110112 assert( p5==0 || pExpr->op!=op )((void) (0));
110113 assert( p5==SQLITE_NULLEQ || pExpr->op==op )((void) (0));
110114
110115 if( op==TK_LE56 ) opx = TK_LT57;
110116 if( op==TK_GE58 ) opx = TK_GT55;
110117 if( op==TK_NE53 ) opx = TK_EQ54;
110118
110119 regLeft = exprCodeSubselect(pParse, pLeft);
110120 regRight = exprCodeSubselect(pParse, pRight);
110121
110122 sqlite3VdbeAddOp2(v, OP_Integer71, 1, dest);
110123 for(i=0; 1 /*Loop exits by "break"*/; i++){
110124 int regFree1 = 0, regFree2 = 0;
110125 Expr *pL = 0, *pR = 0;
110126 int r1, r2;
110127 assert( i>=0 && i<nLeft )((void) (0));
110128 if( addrCmp ) sqlite3VdbeJumpHere(v, addrCmp);
110129 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
110130 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
110131 addrCmp = sqlite3VdbeCurrentAddr(v);
110132 codeCompare(pParse, pL, pR, opx, r1, r2, addrDone, p5, isCommuted);
110133 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
110134 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
110135 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
110136 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
110137 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
110138 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
110139 sqlite3ReleaseTempReg(pParse, regFree1);
110140 sqlite3ReleaseTempReg(pParse, regFree2);
110141 if( (opx==TK_LT57 || opx==TK_GT55) && i<nLeft-1 ){
110142 addrCmp = sqlite3VdbeAddOp0(v, OP_ElseEq59);
110143 testcase(opx==TK_LT); VdbeCoverageIf(v,opx==TK_LT);
110144 testcase(opx==TK_GT); VdbeCoverageIf(v,opx==TK_GT);
110145 }
110146 if( p5==SQLITE_NULLEQ0x80 ){
110147 sqlite3VdbeAddOp2(v, OP_Integer71, 0, dest);
110148 }else{
110149 sqlite3VdbeAddOp3(v, OP_ZeroOrNull92, r1, dest, r2);
110150 }
110151 if( i==nLeft-1 ){
110152 break;
110153 }
110154 if( opx==TK_EQ54 ){
110155 sqlite3VdbeAddOp2(v, OP_NotNull52, dest, addrDone); VdbeCoverage(v);
110156 }else{
110157 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE )((void) (0));
110158 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrDone);
110159 if( i==nLeft-2 ) opx = op;
110160 }
110161 }
110162 sqlite3VdbeJumpHere(v, addrCmp);
110163 sqlite3VdbeResolveLabel(v, addrDone);
110164 if( op==TK_NE53 ){
110165 sqlite3VdbeAddOp2(v, OP_Not19, dest, dest);
110166 }
110167}
110168
110169#if SQLITE_MAX_EXPR_DEPTH1000>0
110170/*
110171** Check that argument nHeight is less than or equal to the maximum
110172** expression depth allowed. If it is not, leave an error message in
110173** pParse.
110174*/
110175SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
110176 int rc = SQLITE_OK0;
110177 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH3];
110178 if( nHeight>mxHeight ){
110179 sqlite3ErrorMsg(pParse,
110180 "Expression tree is too large (maximum depth %d)", mxHeight
110181 );
110182 rc = SQLITE_ERROR1;
110183 }
110184 return rc;
110185}
110186
110187/* The following three functions, heightOfExpr(), heightOfExprList()
110188** and heightOfSelect(), are used to determine the maximum height
110189** of any expression tree referenced by the structure passed as the
110190** first argument.
110191**
110192** If this maximum height is greater than the current value pointed
110193** to by pnHeight, the second parameter, then set *pnHeight to that
110194** value.
110195*/
110196static void heightOfExpr(const Expr *p, int *pnHeight){
110197 if( p ){
110198 if( p->nHeight>*pnHeight ){
110199 *pnHeight = p->nHeight;
110200 }
110201 }
110202}
110203static void heightOfExprList(const ExprList *p, int *pnHeight){
110204 if( p ){
110205 int i;
110206 for(i=0; i<p->nExpr; i++){
110207 heightOfExpr(p->a[i].pExpr, pnHeight);
110208 }
110209 }
110210}
110211static void heightOfSelect(const Select *pSelect, int *pnHeight){
110212 const Select *p;
110213 for(p=pSelect; p; p=p->pPrior){
110214 heightOfExpr(p->pWhere, pnHeight);
110215 heightOfExpr(p->pHaving, pnHeight);
110216 heightOfExpr(p->pLimit, pnHeight);
110217 heightOfExprList(p->pEList, pnHeight);
110218 heightOfExprList(p->pGroupBy, pnHeight);
110219 heightOfExprList(p->pOrderBy, pnHeight);
110220 }
110221}
110222
110223/*
110224** Set the Expr.nHeight variable in the structure passed as an
110225** argument. An expression with no children, Expr.pList or
110226** Expr.pSelect member has a height of 1. Any other expression
110227** has a height equal to the maximum height of any other
110228** referenced Expr plus one.
110229**
110230** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
110231** if appropriate.
110232*/
110233static void exprSetHeight(Expr *p){
110234 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
110235 if( NEVER(p->pRight)(p->pRight) && p->pRight->nHeight>nHeight ){
110236 nHeight = p->pRight->nHeight;
110237 }
110238 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
110239 heightOfSelect(p->x.pSelect, &nHeight);
110240 }else if( p->x.pList ){
110241 heightOfExprList(p->x.pList, &nHeight);
110242 p->flags |= EP_Propagate(0x000200|0x400000|0x000008) & sqlite3ExprListFlags(p->x.pList);
110243 }
110244 p->nHeight = nHeight + 1;
110245}
110246
110247/*
110248** Set the Expr.nHeight variable using the exprSetHeight() function. If
110249** the height is greater than the maximum allowed expression depth,
110250** leave an error in pParse.
110251**
110252** Also propagate all EP_Propagate flags from the Expr.x.pList into
110253** Expr.flags.
110254*/
110255SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
110256 if( pParse->nErr ) return;
110257 exprSetHeight(p);
110258 sqlite3ExprCheckHeight(pParse, p->nHeight);
110259}
110260
110261/*
110262** Return the maximum height of any expression tree referenced
110263** by the select statement passed as an argument.
110264*/
110265SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(const Select *p){
110266 int nHeight = 0;
110267 heightOfSelect(p, &nHeight);
110268 return nHeight;
110269}
110270#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
110271/*
110272** Propagate all EP_Propagate flags from the Expr.x.pList into
110273** Expr.flags.
110274*/
110275SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
110276 if( pParse->nErr ) return;
110277 if( p && ExprUseXList(p)(((p)->flags&0x001000)==0) && p->x.pList ){
110278 p->flags |= EP_Propagate(0x000200|0x400000|0x000008) & sqlite3ExprListFlags(p->x.pList);
110279 }
110280}
110281#define exprSetHeight(y)
110282#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
110283
110284/*
110285** Set the error offset for an Expr node, if possible.
110286*/
110287SQLITE_PRIVATEstatic void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){
110288 if( pExpr==0 ) return;
110289 if( NEVER(ExprUseWJoin(pExpr))((((pExpr)->flags&(0x000002|0x000001))!=0)) ) return;
110290 pExpr->w.iOfst = iOfst;
110291}
110292
110293/*
110294** This routine is the core allocator for Expr nodes.
110295**
110296** Construct a new expression node and return a pointer to it. Memory
110297** for this node and for the pToken argument is a single allocation
110298** obtained from sqlite3DbMalloc(). The calling function
110299** is responsible for making sure the node eventually gets freed.
110300**
110301** If dequote is true, then the token (if it exists) is dequoted.
110302** If dequote is false, no dequoting is performed. The deQuote
110303** parameter is ignored if pToken is NULL or if the token does not
110304** appear to be quoted. If the quotes were of the form "..." (double-quotes)
110305** then the EP_DblQuoted flag is set on the expression node.
110306**
110307** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to
110308** a string that can be translated into a 32-bit integer, then the token is
110309** not stored in u.zToken. Instead, the integer values is written
110310** into u.iValue and the EP_IntValue flag is set. No extra storage
110311** is allocated to hold the integer text and the dequote flag is ignored.
110312** See also tag-20240227-b.
110313*/
110314SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(
110315 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
110316 int op, /* Expression opcode */
110317 const Token *pToken, /* Token argument. Might be NULL */
110318 int dequote /* True to dequote */
110319){
110320 Expr *pNew;
110321 int nExtra = 0;
110322 int iValue = 0;
110323
110324 assert( db!=0 )((void) (0));
110325 if( pToken ){
110326 if( op!=TK_INTEGER156 || pToken->z==0
110327 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
110328 nExtra = pToken->n+1; /* tag-20240227-a */
110329 assert( iValue>=0 )((void) (0));
110330 }
110331 }
110332 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
110333 if( pNew ){
110334 memset(pNew, 0, sizeof(Expr));
110335 pNew->op = (u8)op;
110336 pNew->iAgg = -1;
110337 if( pToken ){
110338 if( nExtra==0 ){
110339 pNew->flags |= EP_IntValue0x000800|EP_Leaf0x800000|(iValue?EP_IsTrue0x10000000:EP_IsFalse0x20000000);
110340 pNew->u.iValue = iValue;
110341 }else{
110342 pNew->u.zToken = (char*)&pNew[1];
110343 assert( pToken->z!=0 || pToken->n==0 )((void) (0));
110344 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
110345 pNew->u.zToken[pToken->n] = 0;
110346 if( dequote && sqlite3Isquote(pNew->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(pNew->u.zToken[0])]&0x80
)
){
110347 sqlite3DequoteExpr(pNew);
110348 }
110349 }
110350 }
110351#if SQLITE_MAX_EXPR_DEPTH1000>0
110352 pNew->nHeight = 1;
110353#endif
110354 }
110355 return pNew;
110356}
110357
110358/*
110359** Allocate a new expression node from a zero-terminated token that has
110360** already been dequoted.
110361*/
110362SQLITE_PRIVATEstatic Expr *sqlite3Expr(
110363 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
110364 int op, /* Expression opcode */
110365 const char *zToken /* Token argument. Might be NULL */
110366){
110367 Token x;
110368 x.z = zToken;
110369 x.n = sqlite3Strlen30(zToken);
110370 return sqlite3ExprAlloc(db, op, &x, 0);
110371}
110372
110373/*
110374** Attach subtrees pLeft and pRight to the Expr node pRoot.
110375**
110376** If pRoot==NULL that means that a memory allocation error has occurred.
110377** In that case, delete the subtrees pLeft and pRight.
110378*/
110379SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(
110380 sqlite3 *db,
110381 Expr *pRoot,
110382 Expr *pLeft,
110383 Expr *pRight
110384){
110385 if( pRoot==0 ){
110386 assert( db->mallocFailed )((void) (0));
110387 sqlite3ExprDelete(db, pLeft);
110388 sqlite3ExprDelete(db, pRight);
110389 }else{
110390 assert( ExprUseXList(pRoot) )((void) (0));
110391 assert( pRoot->x.pSelect==0 )((void) (0));
110392 if( pRight ){
110393 pRoot->pRight = pRight;
110394 pRoot->flags |= EP_Propagate(0x000200|0x400000|0x000008) & pRight->flags;
110395#if SQLITE_MAX_EXPR_DEPTH1000>0
110396 pRoot->nHeight = pRight->nHeight+1;
110397 }else{
110398 pRoot->nHeight = 1;
110399#endif
110400 }
110401 if( pLeft ){
110402 pRoot->pLeft = pLeft;
110403 pRoot->flags |= EP_Propagate(0x000200|0x400000|0x000008) & pLeft->flags;
110404#if SQLITE_MAX_EXPR_DEPTH1000>0
110405 if( pLeft->nHeight>=pRoot->nHeight ){
110406 pRoot->nHeight = pLeft->nHeight+1;
110407 }
110408#endif
110409 }
110410 }
110411}
110412
110413/*
110414** Allocate an Expr node which joins as many as two subtrees.
110415**
110416** One or both of the subtrees can be NULL. Return a pointer to the new
110417** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
110418** free the subtrees and return NULL.
110419*/
110420SQLITE_PRIVATEstatic Expr *sqlite3PExpr(
110421 Parse *pParse, /* Parsing context */
110422 int op, /* Expression opcode */
110423 Expr *pLeft, /* Left operand */
110424 Expr *pRight /* Right operand */
110425){
110426 Expr *p;
110427 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
110428 if( p ){
110429 memset(p, 0, sizeof(Expr));
110430 p->op = op & 0xff;
110431 p->iAgg = -1;
110432 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
110433 sqlite3ExprCheckHeight(pParse, p->nHeight);
110434 }else{
110435 sqlite3ExprDelete(pParse->db, pLeft);
110436 sqlite3ExprDelete(pParse->db, pRight);
110437 }
110438 return p;
110439}
110440
110441/*
110442** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
110443** do a memory allocation failure) then delete the pSelect object.
110444*/
110445SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
110446 if( pExpr ){
110447 pExpr->x.pSelect = pSelect;
110448 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery)(pExpr)->flags|=(0x001000|0x400000);
110449 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
110450 }else{
110451 assert( pParse->db->mallocFailed )((void) (0));
110452 sqlite3SelectDelete(pParse->db, pSelect);
110453 }
110454}
110455
110456/*
110457** Expression list pEList is a list of vector values. This function
110458** converts the contents of pEList to a VALUES(...) Select statement
110459** returning 1 row for each element of the list. For example, the
110460** expression list:
110461**
110462** ( (1,2), (3,4) (5,6) )
110463**
110464** is translated to the equivalent of:
110465**
110466** VALUES(1,2), (3,4), (5,6)
110467**
110468** Each of the vector values in pEList must contain exactly nElem terms.
110469** If a list element that is not a vector or does not contain nElem terms,
110470** an error message is left in pParse.
110471**
110472** This is used as part of processing IN(...) expressions with a list
110473** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))".
110474*/
110475SQLITE_PRIVATEstatic Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){
110476 int ii;
110477 Select *pRet = 0;
110478 assert( nElem>1 )((void) (0));
110479 for(ii=0; ii<pEList->nExpr; ii++){
110480 Select *pSel;
110481 Expr *pExpr = pEList->a[ii].pExpr;
110482 int nExprElem;
110483 if( pExpr->op==TK_VECTOR177 ){
110484 assert( ExprUseXList(pExpr) )((void) (0));
110485 nExprElem = pExpr->x.pList->nExpr;
110486 }else{
110487 nExprElem = 1;
110488 }
110489 if( nExprElem!=nElem ){
110490 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
110491 nExprElem, nExprElem>1?"s":"", nElem
110492 );
110493 break;
110494 }
110495 assert( ExprUseXList(pExpr) )((void) (0));
110496 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values0x0000200,0);
110497 pExpr->x.pList = 0;
110498 if( pSel ){
110499 if( pRet ){
110500 pSel->op = TK_ALL136;
110501 pSel->pPrior = pRet;
110502 }
110503 pRet = pSel;
110504 }
110505 }
110506
110507 if( pRet && pRet->pPrior ){
110508 pRet->selFlags |= SF_MultiValue0x0000400;
110509 }
110510 sqlite3ExprListDelete(pParse->db, pEList);
110511 return pRet;
110512}
110513
110514/*
110515** Join two expressions using an AND operator. If either expression is
110516** NULL, then just return the other expression.
110517**
110518** If one side or the other of the AND is known to be false, and neither side
110519** is part of an ON clause, then instead of returning an AND expression,
110520** just return a constant expression with a value of false.
110521*/
110522SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
110523 sqlite3 *db = pParse->db;
110524 if( pLeft==0 ){
110525 return pRight;
110526 }else if( pRight==0 ){
110527 return pLeft;
110528 }else{
110529 u32 f = pLeft->flags | pRight->flags;
110530 if( (f&(EP_OuterON0x000001|EP_InnerON0x000002|EP_IsFalse0x20000000))==EP_IsFalse0x20000000
110531 && !IN_RENAME_OBJECT(pParse->eParseMode>=2)
110532 ){
110533 sqlite3ExprDeferredDelete(pParse, pLeft);
110534 sqlite3ExprDeferredDelete(pParse, pRight);
110535 return sqlite3Expr(db, TK_INTEGER156, "0");
110536 }else{
110537 return sqlite3PExpr(pParse, TK_AND44, pLeft, pRight);
110538 }
110539 }
110540}
110541
110542/*
110543** Construct a new expression node for a function with multiple
110544** arguments.
110545*/
110546SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(
110547 Parse *pParse, /* Parsing context */
110548 ExprList *pList, /* Argument list */
110549 const Token *pToken, /* Name of the function */
110550 int eDistinct /* SF_Distinct or SF_ALL or 0 */
110551){
110552 Expr *pNew;
110553 sqlite3 *db = pParse->db;
110554 assert( pToken )((void) (0));
110555 pNew = sqlite3ExprAlloc(db, TK_FUNCTION172, pToken, 1);
110556 if( pNew==0 ){
110557 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
110558 return 0;
110559 }
110560 assert( !ExprHasProperty(pNew, EP_InnerON|EP_OuterON) )((void) (0));
110561 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
110562 if( pList
110563 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG6]
110564 && !pParse->nested
110565 ){
110566 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
110567 }
110568 pNew->x.pList = pList;
110569 ExprSetProperty(pNew, EP_HasFunc)(pNew)->flags|=(0x000008);
110570 assert( ExprUseXList(pNew) )((void) (0));
110571 sqlite3ExprSetHeightAndFlags(pParse, pNew);
110572 if( eDistinct==SF_Distinct0x0000001 ) ExprSetProperty(pNew, EP_Distinct)(pNew)->flags|=(0x000004);
110573 return pNew;
110574}
110575
110576/*
110577** Report an error when attempting to use an ORDER BY clause within
110578** the arguments of a non-aggregate function.
110579*/
110580SQLITE_PRIVATEstatic void sqlite3ExprOrderByAggregateError(Parse *pParse, Expr *p){
110581 sqlite3ErrorMsg(pParse,
110582 "ORDER BY may not be used with non-aggregate %#T()", p
110583 );
110584}
110585
110586/*
110587** Attach an ORDER BY clause to a function call.
110588**
110589** functionname( arguments ORDER BY sortlist )
110590** \_____________________/ \______/
110591** pExpr pOrderBy
110592**
110593** The ORDER BY clause is inserted into a new Expr node of type TK_ORDER
110594** and added to the Expr.pLeft field of the parent TK_FUNCTION node.
110595*/
110596SQLITE_PRIVATEstatic void sqlite3ExprAddFunctionOrderBy(
110597 Parse *pParse, /* Parsing context */
110598 Expr *pExpr, /* The function call to which ORDER BY is to be added */
110599 ExprList *pOrderBy /* The ORDER BY clause to add */
110600){
110601 Expr *pOB;
110602 sqlite3 *db = pParse->db;
110603 if( NEVER(pOrderBy==0)(pOrderBy==0) ){
110604 assert( db->mallocFailed )((void) (0));
110605 return;
110606 }
110607 if( pExpr==0 ){
110608 assert( db->mallocFailed )((void) (0));
110609 sqlite3ExprListDelete(db, pOrderBy);
110610 return;
110611 }
110612 assert( pExpr->op==TK_FUNCTION )((void) (0));
110613 assert( pExpr->pLeft==0 )((void) (0));
110614 assert( ExprUseXList(pExpr) )((void) (0));
110615 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0)(pExpr->x.pList->nExpr==0) ){
110616 /* Ignore ORDER BY on zero-argument aggregates */
110617 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pOrderBy);
110618 return;
110619 }
110620 if( IsWindowFunc(pExpr)( ((((pExpr))->flags&(0x1000000))!=0) && pExpr
->y.pWin->eFrmType!=167 )
){
110621 sqlite3ExprOrderByAggregateError(pParse, pExpr);
110622 sqlite3ExprListDelete(db, pOrderBy);
110623 return;
110624 }
110625
110626 pOB = sqlite3ExprAlloc(db, TK_ORDER146, 0, 0);
110627 if( pOB==0 ){
110628 sqlite3ExprListDelete(db, pOrderBy);
110629 return;
110630 }
110631 pOB->x.pList = pOrderBy;
110632 assert( ExprUseXList(pOB) )((void) (0));
110633 pExpr->pLeft = pOB;
110634 ExprSetProperty(pOB, EP_FullSize)(pOB)->flags|=(0x020000);
110635}
110636
110637/*
110638** Check to see if a function is usable according to current access
110639** rules:
110640**
110641** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
110642**
110643** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
110644** top-level SQL
110645**
110646** If the function is not usable, create an error.
110647*/
110648SQLITE_PRIVATEstatic void sqlite3ExprFunctionUsable(
110649 Parse *pParse, /* Parsing and code generating context */
110650 const Expr *pExpr, /* The function invocation */
110651 const FuncDef *pDef /* The function being invoked */
110652){
110653 assert( !IN_RENAME_OBJECT )((void) (0));
110654 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 )((void) (0));
110655 if( ExprHasProperty(pExpr, EP_FromDDL)(((pExpr)->flags&(0x40000000))!=0) ){
110656 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT0x00080000)!=0
110657 || (pParse->db->flags & SQLITE_TrustedSchema0x00000080)==0
110658 ){
110659 /* Functions prohibited in triggers and views if:
110660 ** (1) tagged with SQLITE_DIRECTONLY
110661 ** (2) not tagged with SQLITE_INNOCUOUS (which means it
110662 ** is tagged with SQLITE_FUNC_UNSAFE) and
110663 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning
110664 ** that the schema is possibly tainted).
110665 */
110666 sqlite3ErrorMsg(pParse, "unsafe use of %#T()", pExpr);
110667 }
110668 }
110669}
110670
110671/*
110672** Assign a variable number to an expression that encodes a wildcard
110673** in the original SQL statement.
110674**
110675** Wildcards consisting of a single "?" are assigned the next sequential
110676** variable number.
110677**
110678** Wildcards of the form "?nnn" are assigned the number "nnn". We make
110679** sure "nnn" is not too big to avoid a denial of service attack when
110680** the SQL statement comes from an external source.
110681**
110682** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
110683** as the previous instance of the same wildcard. Or if this is the first
110684** instance of the wildcard, the next sequential variable number is
110685** assigned.
110686*/
110687SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
110688 sqlite3 *db = pParse->db;
110689 const char *z;
110690 ynVar x;
110691
110692 if( pExpr==0 ) return;
110693 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) )((void) (0));
110694 z = pExpr->u.zToken;
110695 assert( z!=0 )((void) (0));
110696 assert( z[0]!=0 )((void) (0));
110697 assert( n==(u32)sqlite3Strlen30(z) )((void) (0));
110698 if( z[1]==0 ){
110699 /* Wildcard of the form "?". Assign the next variable number */
110700 assert( z[0]=='?' )((void) (0));
110701 x = (ynVar)(++pParse->nVar);
110702 }else{
110703 int doAdd = 0;
110704 if( z[0]=='?' ){
110705 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
110706 ** use it as the variable number */
110707 i64 i;
110708 int bOk;
110709 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
110710 i = z[1]-'0'; /* The common case of ?N for a single digit N */
110711 bOk = 1;
110712 }else{
110713 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF81);
110714 }
110715 testcase( i==0 );
110716 testcase( i==1 );
110717 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
110718 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
110719 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
110720 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
110721 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9]);
110722 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
110723 return;
110724 }
110725 x = (ynVar)i;
110726 if( x>pParse->nVar ){
110727 pParse->nVar = (int)x;
110728 doAdd = 1;
110729 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
110730 doAdd = 1;
110731 }
110732 }else{
110733 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
110734 ** number as the prior appearance of the same name, or if the name
110735 ** has never appeared before, reuse the same variable number
110736 */
110737 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
110738 if( x==0 ){
110739 x = (ynVar)(++pParse->nVar);
110740 doAdd = 1;
110741 }
110742 }
110743 if( doAdd ){
110744 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
110745 }
110746 }
110747 pExpr->iColumn = x;
110748 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
110749 sqlite3ErrorMsg(pParse, "too many SQL variables");
110750 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
110751 }
110752}
110753
110754/*
110755** Recursively delete an expression tree.
110756*/
110757static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
110758 assert( p!=0 )((void) (0));
110759 assert( db!=0 )((void) (0));
110760exprDeleteRestart:
110761 assert( !ExprUseUValue(p) || p->u.iValue>=0 )((void) (0));
110762 assert( !ExprUseYWin(p) || !ExprUseYSub(p) )((void) (0));
110763 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed )((void) (0));
110764 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) )((void) (0));
110765#ifdef SQLITE_DEBUG
110766 if( ExprHasProperty(p, EP_Leaf)(((p)->flags&(0x800000))!=0) && !ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(0x010000))!=0) ){
110767 assert( p->pLeft==0 )((void) (0));
110768 assert( p->pRight==0 )((void) (0));
110769 assert( !ExprUseXSelect(p) || p->x.pSelect==0 )((void) (0));
110770 assert( !ExprUseXList(p) || p->x.pList==0 )((void) (0));
110771 }
110772#endif
110773 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf))(((p)->flags&((0x010000|0x800000)))!=0) ){
110774 /* The Expr.x union is never used at the same time as Expr.pRight */
110775 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 )((void) (0));
110776 if( p->pRight ){
110777 assert( !ExprHasProperty(p, EP_WinFunc) )((void) (0));
110778 sqlite3ExprDeleteNN(db, p->pRight);
110779 }else if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
110780 assert( !ExprHasProperty(p, EP_WinFunc) )((void) (0));
110781 sqlite3SelectDelete(db, p->x.pSelect);
110782 }else{
110783 sqlite3ExprListDelete(db, p->x.pList);
110784#ifndef SQLITE_OMIT_WINDOWFUNC
110785 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0) ){
110786 sqlite3WindowDelete(db, p->y.pWin);
110787 }
110788#endif
110789 }
110790 if( p->pLeft && p->op!=TK_SELECT_COLUMN178 ){
110791 Expr *pLeft = p->pLeft;
110792 if( !ExprHasProperty(p, EP_Static)(((p)->flags&(0x8000000))!=0)
110793 && !ExprHasProperty(pLeft, EP_Static)(((pLeft)->flags&(0x8000000))!=0)
110794 ){
110795 /* Avoid unnecessary recursion on unary operators */
110796 sqlite3DbNNFreeNN(db, p);
110797 p = pLeft;
110798 goto exprDeleteRestart;
110799 }else{
110800 sqlite3ExprDeleteNN(db, pLeft);
110801 }
110802 }
110803 }
110804 if( !ExprHasProperty(p, EP_Static)(((p)->flags&(0x8000000))!=0) ){
110805 sqlite3DbNNFreeNN(db, p);
110806 }
110807}
110808SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3 *db, Expr *p){
110809 if( p ) sqlite3ExprDeleteNN(db, p);
110810}
110811SQLITE_PRIVATEstatic void sqlite3ExprDeleteGeneric(sqlite3 *db, void *p){
110812 if( ALWAYS(p)(p) ) sqlite3ExprDeleteNN(db, (Expr*)p);
110813}
110814
110815/*
110816** Clear both elements of an OnOrUsing object
110817*/
110818SQLITE_PRIVATEstatic void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){
110819 if( p==0 ){
110820 /* Nothing to clear */
110821 }else if( p->pOn ){
110822 sqlite3ExprDeleteNN(db, p->pOn);
110823 }else if( p->pUsing ){
110824 sqlite3IdListDelete(db, p->pUsing);
110825 }
110826}
110827
110828/*
110829** Arrange to cause pExpr to be deleted when the pParse is deleted.
110830** This is similar to sqlite3ExprDelete() except that the delete is
110831** deferred until the pParse is deleted.
110832**
110833** The pExpr might be deleted immediately on an OOM error.
110834**
110835** Return 0 if the delete was successfully deferred. Return non-zero
110836** if the delete happened immediately because of an OOM.
110837*/
110838SQLITE_PRIVATEstatic int sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){
110839 return 0==sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr);
110840}
110841
110842/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
110843** expression.
110844*/
110845SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
110846 if( p ){
110847 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110848 sqlite3RenameExprUnmap(pParse, p);
110849 }
110850 sqlite3ExprDeleteNN(pParse->db, p);
110851 }
110852}
110853
110854/*
110855** Return the number of bytes allocated for the expression structure
110856** passed as the first argument. This is always one of EXPR_FULLSIZE,
110857** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
110858*/
110859static int exprStructSize(const Expr *p){
110860 if( ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(0x010000))!=0) ) return EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft);
110861 if( ExprHasProperty(p, EP_Reduced)(((p)->flags&(0x004000))!=0) ) return EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable);
110862 return EXPR_FULLSIZEsizeof(Expr);
110863}
110864
110865/*
110866** The dupedExpr*Size() routines each return the number of bytes required
110867** to store a copy of an expression or expression tree. They differ in
110868** how much of the tree is measured.
110869**
110870** dupedExprStructSize() Size of only the Expr structure
110871** dupedExprNodeSize() Size of Expr + space for token
110872** dupedExprSize() Expr + token + subtree components
110873**
110874***************************************************************************
110875**
110876** The dupedExprStructSize() function returns two values OR-ed together:
110877** (1) the space required for a copy of the Expr structure only and
110878** (2) the EP_xxx flags that indicate what the structure size should be.
110879** The return values is always one of:
110880**
110881** EXPR_FULLSIZE
110882** EXPR_REDUCEDSIZE | EP_Reduced
110883** EXPR_TOKENONLYSIZE | EP_TokenOnly
110884**
110885** The size of the structure can be found by masking the return value
110886** of this routine with 0xfff. The flags can be found by masking the
110887** return value with EP_Reduced|EP_TokenOnly.
110888**
110889** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
110890** (unreduced) Expr objects as they or originally constructed by the parser.
110891** During expression analysis, extra information is computed and moved into
110892** later parts of the Expr object and that extra information might get chopped
110893** off if the expression is reduced. Note also that it does not work to
110894** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
110895** to reduce a pristine expression tree from the parser. The implementation
110896** of dupedExprStructSize() contain multiple assert() statements that attempt
110897** to enforce this constraint.
110898*/
110899static int dupedExprStructSize(const Expr *p, int flags){
110900 int nSize;
110901 assert( flags==EXPRDUP_REDUCE || flags==0 )((void) (0)); /* Only one flag value allowed */
110902 assert( EXPR_FULLSIZE<=0xfff )((void) (0));
110903 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 )((void) (0));
110904 if( 0==flags || ExprHasProperty(p, EP_FullSize)(((p)->flags&(0x020000))!=0) ){
110905 nSize = EXPR_FULLSIZEsizeof(Expr);
110906 }else{
110907 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
110908 assert( !ExprHasProperty(p, EP_OuterON) )((void) (0));
110909 assert( !ExprHasVVAProperty(p, EP_NoReduce) )((void) (0));
110910 if( p->pLeft || p->x.pList ){
110911 nSize = EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) | EP_Reduced0x004000;
110912 }else{
110913 assert( p->pRight==0 )((void) (0));
110914 nSize = EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) | EP_TokenOnly0x010000;
110915 }
110916 }
110917 return nSize;
110918}
110919
110920/*
110921** This function returns the space in bytes required to store the copy
110922** of the Expr structure and a copy of the Expr.u.zToken string (if that
110923** string is defined.)
110924*/
110925static int dupedExprNodeSize(const Expr *p, int flags){
110926 int nByte = dupedExprStructSize(p, flags) & 0xfff;
110927 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000800))!=0) && p->u.zToken ){
110928 nByte += sqlite3Strlen30NN(p->u.zToken)(strlen(p->u.zToken)&0x3fffffff)+1;
110929 }
110930 return ROUND8(nByte)(((nByte)+7)&~7);
110931}
110932
110933/*
110934** Return the number of bytes required to create a duplicate of the
110935** expression passed as the first argument.
110936**
110937** The value returned includes space to create a copy of the Expr struct
110938** itself and the buffer referred to by Expr.u.zToken, if any.
110939**
110940** The return value includes space to duplicate all Expr nodes in the
110941** tree formed by Expr.pLeft and Expr.pRight, but not any other
110942** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin.
110943*/
110944static int dupedExprSize(const Expr *p){
110945 int nByte;
110946 assert( p!=0 )((void) (0));
110947 nByte = dupedExprNodeSize(p, EXPRDUP_REDUCE0x0001);
110948 if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
110949 if( p->pRight ) nByte += dupedExprSize(p->pRight);
110950 assert( nByte==ROUND8(nByte) )((void) (0));
110951 return nByte;
110952}
110953
110954/*
110955** An EdupBuf is a memory allocation used to stored multiple Expr objects
110956** together with their Expr.zToken content. This is used to help implement
110957** compression while doing sqlite3ExprDup(). The top-level Expr does the
110958** allocation for itself and many of its decendents, then passes an instance
110959** of the structure down into exprDup() so that they decendents can have
110960** access to that memory.
110961*/
110962typedef struct EdupBuf EdupBuf;
110963struct EdupBuf {
110964 u8 *zAlloc; /* Memory space available for storage */
110965#ifdef SQLITE_DEBUG
110966 u8 *zEnd; /* First byte past the end of memory */
110967#endif
110968};
110969
110970/*
110971** This function is similar to sqlite3ExprDup(), except that if pEdupBuf
110972** is not NULL then it points to memory that can be used to store a copy
110973** of the input Expr p together with its p->u.zToken (if any). pEdupBuf
110974** is updated with the new buffer tail prior to returning.
110975*/
110976static Expr *exprDup(
110977 sqlite3 *db, /* Database connection (for memory allocation) */
110978 const Expr *p, /* Expr tree to be duplicated */
110979 int dupFlags, /* EXPRDUP_REDUCE for compression. 0 if not */
110980 EdupBuf *pEdupBuf /* Preallocated storage space, or NULL */
110981){
110982 Expr *pNew; /* Value to return */
110983 EdupBuf sEdupBuf; /* Memory space from which to build Expr object */
110984 u32 staticFlag; /* EP_Static if space not obtained from malloc */
110985 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */
110986
110987 assert( db!=0 )((void) (0));
110988 assert( p )((void) (0));
110989 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
110990 assert( pEdupBuf==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
110991
110992 /* Figure out where to write the new Expr structure. */
110993 if( pEdupBuf ){
110994 sEdupBuf.zAlloc = pEdupBuf->zAlloc;
110995#ifdef SQLITE_DEBUG
110996 sEdupBuf.zEnd = pEdupBuf->zEnd;
110997#endif
110998 staticFlag = EP_Static0x8000000;
110999 assert( sEdupBuf.zAlloc!=0 )((void) (0));
111000 assert( dupFlags==EXPRDUP_REDUCE )((void) (0));
111001 }else{
111002 int nAlloc;
111003 if( dupFlags ){
111004 nAlloc = dupedExprSize(p);
111005 }else if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000800))!=0) && p->u.zToken ){
111006 nToken = sqlite3Strlen30NN(p->u.zToken)(strlen(p->u.zToken)&0x3fffffff)+1;
111007 nAlloc = ROUND8(EXPR_FULLSIZE + nToken)(((sizeof(Expr) + nToken)+7)&~7);
111008 }else{
111009 nToken = 0;
111010 nAlloc = ROUND8(EXPR_FULLSIZE)(((sizeof(Expr))+7)&~7);
111011 }
111012 assert( nAlloc==ROUND8(nAlloc) )((void) (0));
111013 sEdupBuf.zAlloc = sqlite3DbMallocRawNN(db, nAlloc);
111014#ifdef SQLITE_DEBUG
111015 sEdupBuf.zEnd = sEdupBuf.zAlloc ? sEdupBuf.zAlloc+nAlloc : 0;
111016#endif
111017
111018 staticFlag = 0;
111019 }
111020 pNew = (Expr *)sEdupBuf.zAlloc;
111021 assert( EIGHT_BYTE_ALIGNMENT(pNew) )((void) (0));
111022
111023 if( pNew ){
111024 /* Set nNewSize to the size allocated for the structure pointed to
111025 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
111026 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
111027 ** by the copy of the p->u.zToken string (if any).
111028 */
111029 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
111030 int nNewSize = nStructSize & 0xfff;
111031 if( nToken<0 ){
111032 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000800))!=0) && p->u.zToken ){
111033 nToken = sqlite3Strlen30(p->u.zToken) + 1;
111034 }else{
111035 nToken = 0;
111036 }
111037 }
111038 if( dupFlags ){
111039 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken )((void) (0));
111040 assert( ExprHasProperty(p, EP_Reduced)==0 )((void) (0));
111041 memcpy(sEdupBuf.zAlloc, p, nNewSize);
111042 }else{
111043 u32 nSize = (u32)exprStructSize(p);
111044 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >=((void) (0))
111045 (int)EXPR_FULLSIZE+nToken )((void) (0));
111046 memcpy(sEdupBuf.zAlloc, p, nSize);
111047 if( nSize<EXPR_FULLSIZEsizeof(Expr) ){
111048 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZEsizeof(Expr)-nSize);
111049 }
111050 nNewSize = EXPR_FULLSIZEsizeof(Expr);
111051 }
111052
111053 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
111054 pNew->flags &= ~(EP_Reduced0x004000|EP_TokenOnly0x010000|EP_Static0x8000000);
111055 pNew->flags |= nStructSize & (EP_Reduced0x004000|EP_TokenOnly0x010000);
111056 pNew->flags |= staticFlag;
111057 ExprClearVVAProperties(pNew);
111058 if( dupFlags ){
111059 ExprSetVVAProperty(pNew, EP_Immutable);
111060 }
111061
111062 /* Copy the p->u.zToken string, if any. */
111063 assert( nToken>=0 )((void) (0));
111064 if( nToken>0 ){
111065 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize];
111066 memcpy(zToken, p->u.zToken, nToken);
111067 nNewSize += nToken;
111068 }
111069 sEdupBuf.zAlloc += ROUND8(nNewSize)(((nNewSize)+7)&~7);
111070
111071 if( ((p->flags|pNew->flags)&(EP_TokenOnly0x010000|EP_Leaf0x800000))==0 ){
111072
111073 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
111074 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
111075 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
111076 }else{
111077 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList,
111078 p->op!=TK_ORDER146 ? dupFlags : 0);
111079 }
111080
111081#ifndef SQLITE_OMIT_WINDOWFUNC
111082 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0) ){
111083 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
111084 assert( ExprHasProperty(pNew, EP_WinFunc) )((void) (0));
111085 }
111086#endif /* SQLITE_OMIT_WINDOWFUNC */
111087
111088 /* Fill in pNew->pLeft and pNew->pRight. */
111089 if( dupFlags ){
111090 if( p->op==TK_SELECT_COLUMN178 ){
111091 pNew->pLeft = p->pLeft;
111092 assert( p->pRight==0((void) (0))
111093 || p->pRight==p->pLeft((void) (0))
111094 || ExprHasProperty(p->pLeft, EP_Subquery) )((void) (0));
111095 }else{
111096 pNew->pLeft = p->pLeft ?
111097 exprDup(db, p->pLeft, EXPRDUP_REDUCE0x0001, &sEdupBuf) : 0;
111098 }
111099 pNew->pRight = p->pRight ?
111100 exprDup(db, p->pRight, EXPRDUP_REDUCE0x0001, &sEdupBuf) : 0;
111101 }else{
111102 if( p->op==TK_SELECT_COLUMN178 ){
111103 pNew->pLeft = p->pLeft;
111104 assert( p->pRight==0((void) (0))
111105 || p->pRight==p->pLeft((void) (0))
111106 || ExprHasProperty(p->pLeft, EP_Subquery) )((void) (0));
111107 }else{
111108 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
111109 }
111110 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
111111 }
111112 }
111113 }
111114 if( pEdupBuf ) memcpy(pEdupBuf, &sEdupBuf, sizeof(sEdupBuf));
111115 assert( sEdupBuf.zAlloc <= sEdupBuf.zEnd )((void) (0));
111116 return pNew;
111117}
111118
111119/*
111120** Create and return a deep copy of the object passed as the second
111121** argument. If an OOM condition is encountered, NULL is returned
111122** and the db->mallocFailed flag set.
111123*/
111124#ifndef SQLITE_OMIT_CTE
111125SQLITE_PRIVATEstatic With *sqlite3WithDup(sqlite3 *db, With *p){
111126 With *pRet = 0;
111127 if( p ){
111128 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
111129 pRet = sqlite3DbMallocZero(db, nByte);
111130 if( pRet ){
111131 int i;
111132 pRet->nCte = p->nCte;
111133 for(i=0; i<p->nCte; i++){
111134 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
111135 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
111136 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
111137 pRet->a[i].eM10d = p->a[i].eM10d;
111138 }
111139 }
111140 }
111141 return pRet;
111142}
111143#else
111144# define sqlite3WithDup(x,y) 0
111145#endif
111146
111147#ifndef SQLITE_OMIT_WINDOWFUNC
111148/*
111149** The gatherSelectWindows() procedure and its helper routine
111150** gatherSelectWindowsCallback() are used to scan all the expressions
111151** an a newly duplicated SELECT statement and gather all of the Window
111152** objects found there, assembling them onto the linked list at Select->pWin.
111153*/
111154static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
111155 if( pExpr->op==TK_FUNCTION172 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
111156 Select *pSelect = pWalker->u.pSelect;
111157 Window *pWin = pExpr->y.pWin;
111158 assert( pWin )((void) (0));
111159 assert( IsWindowFunc(pExpr) )((void) (0));
111160 assert( pWin->ppThis==0 )((void) (0));
111161 sqlite3WindowLink(pSelect, pWin);
111162 }
111163 return WRC_Continue0;
111164}
111165static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
111166 return p==pWalker->u.pSelect ? WRC_Continue0 : WRC_Prune1;
111167}
111168static void gatherSelectWindows(Select *p){
111169 Walker w;
111170 w.xExprCallback = gatherSelectWindowsCallback;
111171 w.xSelectCallback = gatherSelectWindowsSelectCallback;
111172 w.xSelectCallback2 = 0;
111173 w.pParse = 0;
111174 w.u.pSelect = p;
111175 sqlite3WalkSelect(&w, p);
111176}
111177#endif
111178
111179
111180/*
111181** The following group of routines make deep copies of expressions,
111182** expression lists, ID lists, and select statements. The copies can
111183** be deleted (by being passed to their respective ...Delete() routines)
111184** without effecting the originals.
111185**
111186** The expression list, ID, and source lists return by sqlite3ExprListDup(),
111187** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
111188** by subsequent calls to sqlite*ListAppend() routines.
111189**
111190** Any tables that the SrcList might point to are not duplicated.
111191**
111192** The flags parameter contains a combination of the EXPRDUP_XXX flags.
111193** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
111194** truncated version of the usual Expr structure that will be stored as
111195** part of the in-memory representation of the database schema.
111196*/
111197SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){
111198 assert( flags==0 || flags==EXPRDUP_REDUCE )((void) (0));
111199 return p ? exprDup(db, p, flags, 0) : 0;
111200}
111201SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){
111202 ExprList *pNew;
111203 struct ExprList_item *pItem;
111204 const struct ExprList_item *pOldItem;
111205 int i;
111206 Expr *pPriorSelectColOld = 0;
111207 Expr *pPriorSelectColNew = 0;
111208 assert( db!=0 )((void) (0));
111209 if( p==0 ) return 0;
111210 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
111211 if( pNew==0 ) return 0;
111212 pNew->nExpr = p->nExpr;
111213 pNew->nAlloc = p->nAlloc;
111214 pItem = pNew->a;
111215 pOldItem = p->a;
111216 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
111217 Expr *pOldExpr = pOldItem->pExpr;
111218 Expr *pNewExpr;
111219 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
111220 if( pOldExpr
111221 && pOldExpr->op==TK_SELECT_COLUMN178
111222 && (pNewExpr = pItem->pExpr)!=0
111223 ){
111224 if( pNewExpr->pRight ){
111225 pPriorSelectColOld = pOldExpr->pRight;
111226 pPriorSelectColNew = pNewExpr->pRight;
111227 pNewExpr->pLeft = pNewExpr->pRight;
111228 }else{
111229 if( pOldExpr->pLeft!=pPriorSelectColOld ){
111230 pPriorSelectColOld = pOldExpr->pLeft;
111231 pPriorSelectColNew = sqlite3ExprDup(db, pPriorSelectColOld, flags);
111232 pNewExpr->pRight = pPriorSelectColNew;
111233 }
111234 pNewExpr->pLeft = pPriorSelectColNew;
111235 }
111236 }
111237 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
111238 pItem->fg = pOldItem->fg;
111239 pItem->fg.done = 0;
111240 pItem->u = pOldItem->u;
111241 }
111242 return pNew;
111243}
111244
111245/*
111246** If cursors, triggers, views and subqueries are all omitted from
111247** the build, then none of the following routines, except for
111248** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
111249** called with a NULL argument.
111250*/
111251#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
111252 || !defined(SQLITE_OMIT_SUBQUERY)
111253SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
111254 SrcList *pNew;
111255 int i;
111256 int nByte;
111257 assert( db!=0 )((void) (0));
111258 if( p==0 ) return 0;
111259 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
111260 pNew = sqlite3DbMallocRawNN(db, nByte );
111261 if( pNew==0 ) return 0;
111262 pNew->nSrc = pNew->nAlloc = p->nSrc;
111263 for(i=0; i<p->nSrc; i++){
111264 SrcItem *pNewItem = &pNew->a[i];
111265 const SrcItem *pOldItem = &p->a[i];
111266 Table *pTab;
111267 pNewItem->fg = pOldItem->fg;
111268 if( pOldItem->fg.isSubquery ){
111269 Subquery *pNewSubq = sqlite3DbMallocRaw(db, sizeof(Subquery));
111270 if( pNewSubq==0 ){
111271 assert( db->mallocFailed )((void) (0));
111272 pNewItem->fg.isSubquery = 0;
111273 }else{
111274 memcpy(pNewSubq, pOldItem->u4.pSubq, sizeof(*pNewSubq));
111275 pNewSubq->pSelect = sqlite3SelectDup(db, pNewSubq->pSelect, flags);
111276 if( pNewSubq->pSelect==0 ){
111277 sqlite3DbFree(db, pNewSubq);
111278 pNewSubq = 0;
111279 pNewItem->fg.isSubquery = 0;
111280 }
111281 }
111282 pNewItem->u4.pSubq = pNewSubq;
111283 }else if( pOldItem->fg.fixedSchema ){
111284 pNewItem->u4.pSchema = pOldItem->u4.pSchema;
111285 }else{
111286 pNewItem->u4.zDatabase = sqlite3DbStrDup(db, pOldItem->u4.zDatabase);
111287 }
111288 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
111289 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
111290 pNewItem->iCursor = pOldItem->iCursor;
111291 if( pNewItem->fg.isIndexedBy ){
111292 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
111293 }else if( pNewItem->fg.isTabFunc ){
111294 pNewItem->u1.pFuncArg =
111295 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
111296 }else{
111297 pNewItem->u1.nRow = pOldItem->u1.nRow;
111298 }
111299 pNewItem->u2 = pOldItem->u2;
111300 if( pNewItem->fg.isCte ){
111301 pNewItem->u2.pCteUse->nUse++;
111302 }
111303 pTab = pNewItem->pSTab = pOldItem->pSTab;
111304 if( pTab ){
111305 pTab->nTabRef++;
111306 }
111307 if( pOldItem->fg.isUsing ){
111308 assert( pNewItem->fg.isUsing )((void) (0));
111309 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
111310 }else{
111311 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
111312 }
111313 pNewItem->colUsed = pOldItem->colUsed;
111314 }
111315 return pNew;
111316}
111317SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){
111318 IdList *pNew;
111319 int i;
111320 assert( db!=0 )((void) (0));
111321 if( p==0 ) return 0;
111322 assert( p->eU4!=EU4_EXPR )((void) (0));
111323 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
111324 if( pNew==0 ) return 0;
111325 pNew->nId = p->nId;
111326 pNew->eU4 = p->eU4;
111327 for(i=0; i<p->nId; i++){
111328 struct IdList_item *pNewItem = &pNew->a[i];
111329 const struct IdList_item *pOldItem = &p->a[i];
111330 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
111331 pNewItem->u4 = pOldItem->u4;
111332 }
111333 return pNew;
111334}
111335SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){
111336 Select *pRet = 0;
111337 Select *pNext = 0;
111338 Select **pp = &pRet;
111339 const Select *p;
111340
111341 assert( db!=0 )((void) (0));
111342 for(p=pDup; p; p=p->pPrior){
111343 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
111344 if( pNew==0 ) break;
111345 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
111346 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
111347 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
111348 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
111349 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
111350 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
111351 pNew->op = p->op;
111352 pNew->pNext = pNext;
111353 pNew->pPrior = 0;
111354 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
111355 pNew->iLimit = 0;
111356 pNew->iOffset = 0;
111357 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral0x0000020;
111358 pNew->addrOpenEphm[0] = -1;
111359 pNew->addrOpenEphm[1] = -1;
111360 pNew->nSelectRow = p->nSelectRow;
111361 pNew->pWith = sqlite3WithDup(db, p->pWith);
111362#ifndef SQLITE_OMIT_WINDOWFUNC
111363 pNew->pWin = 0;
111364 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
111365 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
111366#endif
111367 pNew->selId = p->selId;
111368 if( db->mallocFailed ){
111369 /* Any prior OOM might have left the Select object incomplete.
111370 ** Delete the whole thing rather than allow an incomplete Select
111371 ** to be used by the code generator. */
111372 pNew->pNext = 0;
111373 sqlite3SelectDelete(db, pNew);
111374 break;
111375 }
111376 *pp = pNew;
111377 pp = &pNew->pPrior;
111378 pNext = pNew;
111379 }
111380 return pRet;
111381}
111382#else
111383SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){
111384 assert( p==0 )((void) (0));
111385 return 0;
111386}
111387#endif
111388
111389
111390/*
111391** Add a new element to the end of an expression list. If pList is
111392** initially NULL, then create a new expression list.
111393**
111394** The pList argument must be either NULL or a pointer to an ExprList
111395** obtained from a prior call to sqlite3ExprListAppend().
111396**
111397** If a memory allocation error occurs, the entire list is freed and
111398** NULL is returned. If non-NULL is returned, then it is guaranteed
111399** that the new entry was successfully appended.
111400*/
111401static const struct ExprList_item zeroItem = {0};
111402SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) ExprList *sqlite3ExprListAppendNew(
111403 sqlite3 *db, /* Database handle. Used for memory allocation */
111404 Expr *pExpr /* Expression to be appended. Might be NULL */
111405){
111406 struct ExprList_item *pItem;
111407 ExprList *pList;
111408
111409 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
111410 if( pList==0 ){
111411 sqlite3ExprDelete(db, pExpr);
111412 return 0;
111413 }
111414 pList->nAlloc = 4;
111415 pList->nExpr = 1;
111416 pItem = &pList->a[0];
111417 *pItem = zeroItem;
111418 pItem->pExpr = pExpr;
111419 return pList;
111420}
111421SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) ExprList *sqlite3ExprListAppendGrow(
111422 sqlite3 *db, /* Database handle. Used for memory allocation */
111423 ExprList *pList, /* List to which to append. Might be NULL */
111424 Expr *pExpr /* Expression to be appended. Might be NULL */
111425){
111426 struct ExprList_item *pItem;
111427 ExprList *pNew;
111428 pList->nAlloc *= 2;
111429 pNew = sqlite3DbRealloc(db, pList,
111430 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
111431 if( pNew==0 ){
111432 sqlite3ExprListDelete(db, pList);
111433 sqlite3ExprDelete(db, pExpr);
111434 return 0;
111435 }else{
111436 pList = pNew;
111437 }
111438 pItem = &pList->a[pList->nExpr++];
111439 *pItem = zeroItem;
111440 pItem->pExpr = pExpr;
111441 return pList;
111442}
111443SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(
111444 Parse *pParse, /* Parsing context */
111445 ExprList *pList, /* List to which to append. Might be NULL */
111446 Expr *pExpr /* Expression to be appended. Might be NULL */
111447){
111448 struct ExprList_item *pItem;
111449 if( pList==0 ){
111450 return sqlite3ExprListAppendNew(pParse->db,pExpr);
111451 }
111452 if( pList->nAlloc<pList->nExpr+1 ){
111453 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
111454 }
111455 pItem = &pList->a[pList->nExpr++];
111456 *pItem = zeroItem;
111457 pItem->pExpr = pExpr;
111458 return pList;
111459}
111460
111461/*
111462** pColumns and pExpr form a vector assignment which is part of the SET
111463** clause of an UPDATE statement. Like this:
111464**
111465** (a,b,c) = (expr1,expr2,expr3)
111466** Or: (a,b,c) = (SELECT x,y,z FROM ....)
111467**
111468** For each term of the vector assignment, append new entries to the
111469** expression list pList. In the case of a subquery on the RHS, append
111470** TK_SELECT_COLUMN expressions.
111471*/
111472SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(
111473 Parse *pParse, /* Parsing context */
111474 ExprList *pList, /* List to which to append. Might be NULL */
111475 IdList *pColumns, /* List of names of LHS of the assignment */
111476 Expr *pExpr /* Vector expression to be appended. Might be NULL */
111477){
111478 sqlite3 *db = pParse->db;
111479 int n;
111480 int i;
111481 int iFirst = pList ? pList->nExpr : 0;
111482 /* pColumns can only be NULL due to an OOM but an OOM will cause an
111483 ** exit prior to this routine being invoked */
111484 if( NEVER(pColumns==0)(pColumns==0) ) goto vector_append_error;
111485 if( pExpr==0 ) goto vector_append_error;
111486
111487 /* If the RHS is a vector, then we can immediately check to see that
111488 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
111489 ** wildcards ("*") in the result set of the SELECT must be expanded before
111490 ** we can do the size check, so defer the size check until code generation.
111491 */
111492 if( pExpr->op!=TK_SELECT139 && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
111493 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
111494 pColumns->nId, n);
111495 goto vector_append_error;
111496 }
111497
111498 for(i=0; i<pColumns->nId; i++){
111499 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
111500 assert( pSubExpr!=0 || db->mallocFailed )((void) (0));
111501 if( pSubExpr==0 ) continue;
111502 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
111503 if( pList ){
111504 assert( pList->nExpr==iFirst+i+1 )((void) (0));
111505 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
111506 pColumns->a[i].zName = 0;
111507 }
111508 }
111509
111510 if( !db->mallocFailed && pExpr->op==TK_SELECT139 && ALWAYS(pList!=0)(pList!=0) ){
111511 Expr *pFirst = pList->a[iFirst].pExpr;
111512 assert( pFirst!=0 )((void) (0));
111513 assert( pFirst->op==TK_SELECT_COLUMN )((void) (0));
111514
111515 /* Store the SELECT statement in pRight so it will be deleted when
111516 ** sqlite3ExprListDelete() is called */
111517 pFirst->pRight = pExpr;
111518 pExpr = 0;
111519
111520 /* Remember the size of the LHS in iTable so that we can check that
111521 ** the RHS and LHS sizes match during code generation. */
111522 pFirst->iTable = pColumns->nId;
111523 }
111524
111525vector_append_error:
111526 sqlite3ExprUnmapAndDelete(pParse, pExpr);
111527 sqlite3IdListDelete(db, pColumns);
111528 return pList;
111529}
111530
111531/*
111532** Set the sort order for the last element on the given ExprList.
111533*/
111534SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
111535 struct ExprList_item *pItem;
111536 if( p==0 ) return;
111537 assert( p->nExpr>0 )((void) (0));
111538
111539 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 )((void) (0));
111540 assert( iSortOrder==SQLITE_SO_UNDEFINED((void) (0))
111541 || iSortOrder==SQLITE_SO_ASC((void) (0))
111542 || iSortOrder==SQLITE_SO_DESC((void) (0))
111543 )((void) (0));
111544 assert( eNulls==SQLITE_SO_UNDEFINED((void) (0))
111545 || eNulls==SQLITE_SO_ASC((void) (0))
111546 || eNulls==SQLITE_SO_DESC((void) (0))
111547 )((void) (0));
111548
111549 pItem = &p->a[p->nExpr-1];
111550 assert( pItem->fg.bNulls==0 )((void) (0));
111551 if( iSortOrder==SQLITE_SO_UNDEFINED-1 ){
111552 iSortOrder = SQLITE_SO_ASC0;
111553 }
111554 pItem->fg.sortFlags = (u8)iSortOrder;
111555
111556 if( eNulls!=SQLITE_SO_UNDEFINED-1 ){
111557 pItem->fg.bNulls = 1;
111558 if( iSortOrder!=eNulls ){
111559 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL0x02;
111560 }
111561 }
111562}
111563
111564/*
111565** Set the ExprList.a[].zEName element of the most recently added item
111566** on the expression list.
111567**
111568** pList might be NULL following an OOM error. But pName should never be
111569** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
111570** is set.
111571*/
111572SQLITE_PRIVATEstatic void sqlite3ExprListSetName(
111573 Parse *pParse, /* Parsing context */
111574 ExprList *pList, /* List to which to add the span. */
111575 const Token *pName, /* Name to be added */
111576 int dequote /* True to cause the name to be dequoted */
111577){
111578 assert( pList!=0 || pParse->db->mallocFailed!=0 )((void) (0));
111579 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 )((void) (0));
111580 if( pList ){
111581 struct ExprList_item *pItem;
111582 assert( pList->nExpr>0 )((void) (0));
111583 pItem = &pList->a[pList->nExpr-1];
111584 assert( pItem->zEName==0 )((void) (0));
111585 assert( pItem->fg.eEName==ENAME_NAME )((void) (0));
111586 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
111587 if( dequote ){
111588 /* If dequote==0, then pName->z does not point to part of a DDL
111589 ** statement handled by the parser. And so no token need be added
111590 ** to the token-map. */
111591 sqlite3Dequote(pItem->zEName);
111592 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
111593 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
111594 }
111595 }
111596 }
111597}
111598
111599/*
111600** Set the ExprList.a[].zSpan element of the most recently added item
111601** on the expression list.
111602**
111603** pList might be NULL following an OOM error. But pSpan should never be
111604** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
111605** is set.
111606*/
111607SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(
111608 Parse *pParse, /* Parsing context */
111609 ExprList *pList, /* List to which to add the span. */
111610 const char *zStart, /* Start of the span */
111611 const char *zEnd /* End of the span */
111612){
111613 sqlite3 *db = pParse->db;
111614 assert( pList!=0 || db->mallocFailed!=0 )((void) (0));
111615 if( pList ){
111616 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
111617 assert( pList->nExpr>0 )((void) (0));
111618 if( pItem->zEName==0 ){
111619 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
111620 pItem->fg.eEName = ENAME_SPAN1;
111621 }
111622 }
111623}
111624
111625/*
111626** If the expression list pEList contains more than iLimit elements,
111627** leave an error message in pParse.
111628*/
111629SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(
111630 Parse *pParse,
111631 ExprList *pEList,
111632 const char *zObject
111633){
111634 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN2];
111635 testcase( pEList && pEList->nExpr==mx );
111636 testcase( pEList && pEList->nExpr==mx+1 );
111637 if( pEList && pEList->nExpr>mx ){
111638 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
111639 }
111640}
111641
111642/*
111643** Delete an entire expression list.
111644*/
111645static SQLITE_NOINLINE__attribute__((noinline)) void exprListDeleteNN(sqlite3 *db, ExprList *pList){
111646 int i = pList->nExpr;
111647 struct ExprList_item *pItem = pList->a;
111648 assert( pList->nExpr>0 )((void) (0));
111649 assert( db!=0 )((void) (0));
111650 do{
111651 sqlite3ExprDelete(db, pItem->pExpr);
111652 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
111653 pItem++;
111654 }while( --i>0 );
111655 sqlite3DbNNFreeNN(db, pList);
111656}
111657SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
111658 if( pList ) exprListDeleteNN(db, pList);
111659}
111660SQLITE_PRIVATEstatic void sqlite3ExprListDeleteGeneric(sqlite3 *db, void *pList){
111661 if( ALWAYS(pList)(pList) ) exprListDeleteNN(db, (ExprList*)pList);
111662}
111663
111664/*
111665** Return the bitwise-OR of all Expr.flags fields in the given
111666** ExprList.
111667*/
111668SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList *pList){
111669 int i;
111670 u32 m = 0;
111671 assert( pList!=0 )((void) (0));
111672 for(i=0; i<pList->nExpr; i++){
111673 Expr *pExpr = pList->a[i].pExpr;
111674 assert( pExpr!=0 )((void) (0));
111675 m |= pExpr->flags;
111676 }
111677 return m;
111678}
111679
111680/*
111681** This is a SELECT-node callback for the expression walker that
111682** always "fails". By "fail" in this case, we mean set
111683** pWalker->eCode to zero and abort.
111684**
111685** This callback is used by multiple expression walkers.
111686*/
111687SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
111688 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
111689 pWalker->eCode = 0;
111690 return WRC_Abort2;
111691}
111692
111693/*
111694** Check the input string to see if it is "true" or "false" (in any case).
111695**
111696** If the string is.... Return
111697** "true" EP_IsTrue
111698** "false" EP_IsFalse
111699** anything else 0
111700*/
111701SQLITE_PRIVATEstatic u32 sqlite3IsTrueOrFalse(const char *zIn){
111702 if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue0x10000000;
111703 if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse0x20000000;
111704 return 0;
111705}
111706
111707
111708/*
111709** If the input expression is an ID with the name "true" or "false"
111710** then convert it into an TK_TRUEFALSE term. Return non-zero if
111711** the conversion happened, and zero if the expression is unaltered.
111712*/
111713SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr *pExpr){
111714 u32 v;
111715 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING )((void) (0));
111716 if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue)(((pExpr)->flags&(0x4000000|0x000800))!=0)
111717 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
111718 ){
111719 pExpr->op = TK_TRUEFALSE171;
111720 ExprSetProperty(pExpr, v)(pExpr)->flags|=(v);
111721 return 1;
111722 }
111723 return 0;
111724}
111725
111726/*
111727** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
111728** and 0 if it is FALSE.
111729*/
111730SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr *pExpr){
111731 pExpr = sqlite3ExprSkipCollateAndLikely((Expr*)pExpr);
111732 assert( pExpr->op==TK_TRUEFALSE )((void) (0));
111733 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
111734 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0((void) (0))
111735 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 )((void) (0));
111736 return pExpr->u.zToken[4]==0;
111737}
111738
111739/*
111740** If pExpr is an AND or OR expression, try to simplify it by eliminating
111741** terms that are always true or false. Return the simplified expression.
111742** Or return the original expression if no simplification is possible.
111743**
111744** Examples:
111745**
111746** (x<10) AND true => (x<10)
111747** (x<10) AND false => false
111748** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
111749** (x<10) AND (y=22 OR true) => (x<10)
111750** (y=22) OR true => true
111751*/
111752SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
111753 assert( pExpr!=0 )((void) (0));
111754 if( pExpr->op==TK_AND44 || pExpr->op==TK_OR43 ){
111755 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
111756 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
111757 if( ExprAlwaysTrue(pLeft)(((pLeft)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pRight)(((pRight)->flags&(0x000001|0x20000000))==0x20000000) ){
111758 pExpr = pExpr->op==TK_AND44 ? pRight : pLeft;
111759 }else if( ExprAlwaysTrue(pRight)(((pRight)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pLeft)(((pLeft)->flags&(0x000001|0x20000000))==0x20000000) ){
111760 pExpr = pExpr->op==TK_AND44 ? pLeft : pRight;
111761 }
111762 }
111763 return pExpr;
111764}
111765
111766/*
111767** pExpr is a TK_FUNCTION node. Try to determine whether or not the
111768** function is a constant function. A function is constant if all of
111769** the following are true:
111770**
111771** (1) It is a scalar function (not an aggregate or window function)
111772** (2) It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG
111773** property.
111774** (3) All of its arguments are constants
111775**
111776** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
111777** It makes no changes to pWalker->eCode if pExpr is constant. In
111778** every case, it returns WRC_Abort.
111779**
111780** Called as a service subroutine from exprNodeIsConstant().
111781*/
111782static SQLITE_NOINLINE__attribute__((noinline)) int exprNodeIsConstantFunction(
111783 Walker *pWalker,
111784 Expr *pExpr
111785){
111786 int n; /* Number of arguments */
111787 ExprList *pList; /* List of arguments */
111788 FuncDef *pDef; /* The function */
111789 sqlite3 *db; /* The database */
111790
111791 assert( pExpr->op==TK_FUNCTION )((void) (0));
111792 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(0x010000))!=0)
111793 || (pList = pExpr->x.pList)==0
111794 ){;
111795 n = 0;
111796 }else{
111797 n = pList->nExpr;
111798 sqlite3WalkExprList(pWalker, pList);
111799 if( pWalker->eCode==0 ) return WRC_Abort2;
111800 }
111801 db = pWalker->pParse->db;
111802 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db)((db)->enc), 0);
111803 if( pDef==0
111804 || pDef->xFinalize!=0
111805 || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000))==0
111806 || ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0)
111807 ){
111808 pWalker->eCode = 0;
111809 return WRC_Abort2;
111810 }
111811 return WRC_Prune1;
111812}
111813
111814
111815/*
111816** These routines are Walker callbacks used to check expressions to
111817** see if they are "constant" for some definition of constant. The
111818** Walker.eCode value determines the type of "constant" we are looking
111819** for.
111820**
111821** These callback routines are used to implement the following:
111822**
111823** sqlite3ExprIsConstant() pWalker->eCode==1
111824** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
111825** sqlite3ExprIsTableConstant() pWalker->eCode==3
111826** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
111827**
111828** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
111829** is found to not be a constant.
111830**
111831** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
111832** expressions in a CREATE TABLE statement. The Walker.eCode value is 5
111833** when parsing an existing schema out of the sqlite_schema table and 4
111834** when processing a new CREATE TABLE statement. A bound parameter raises
111835** an error for new statements, but is silently converted
111836** to NULL for existing schemas. This allows sqlite_schema tables that
111837** contain a bound parameter because they were generated by older versions
111838** of SQLite to be parsed by newer versions of SQLite without raising a
111839** malformed schema error.
111840*/
111841static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
111842 assert( pWalker->eCode>0 )((void) (0));
111843
111844 /* If pWalker->eCode is 2 then any term of the expression that comes from
111845 ** the ON or USING clauses of an outer join disqualifies the expression
111846 ** from being considered constant. */
111847 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
111848 pWalker->eCode = 0;
111849 return WRC_Abort2;
111850 }
111851
111852 switch( pExpr->op ){
111853 /* Consider functions to be constant if all their arguments are constant
111854 ** and either pWalker->eCode==4 or 5 or the function has the
111855 ** SQLITE_FUNC_CONST flag. */
111856 case TK_FUNCTION172:
111857 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)(((pExpr)->flags&(0x100000))!=0))
111858 && !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0)
111859 ){
111860 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(0x40000000);
111861 return WRC_Continue0;
111862 }else if( pWalker->pParse ){
111863 return exprNodeIsConstantFunction(pWalker, pExpr);
111864 }else{
111865 pWalker->eCode = 0;
111866 return WRC_Abort2;
111867 }
111868 case TK_ID60:
111869 /* Convert "true" or "false" in a DEFAULT clause into the
111870 ** appropriate TK_TRUEFALSE operator */
111871 if( sqlite3ExprIdToTrueFalse(pExpr) ){
111872 return WRC_Prune1;
111873 }
111874 /* no break */ deliberate_fall_through__attribute__((fallthrough));
111875 case TK_COLUMN168:
111876 case TK_AGG_FUNCTION169:
111877 case TK_AGG_COLUMN170:
111878 testcase( pExpr->op==TK_ID );
111879 testcase( pExpr->op==TK_COLUMN );
111880 testcase( pExpr->op==TK_AGG_FUNCTION );
111881 testcase( pExpr->op==TK_AGG_COLUMN );
111882 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0) && pWalker->eCode!=2 ){
111883 return WRC_Continue0;
111884 }
111885 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
111886 return WRC_Continue0;
111887 }
111888 /* no break */ deliberate_fall_through__attribute__((fallthrough));
111889 case TK_IF_NULL_ROW179:
111890 case TK_REGISTER176:
111891 case TK_DOT142:
111892 case TK_RAISE72:
111893 testcase( pExpr->op==TK_REGISTER );
111894 testcase( pExpr->op==TK_IF_NULL_ROW );
111895 testcase( pExpr->op==TK_DOT );
111896 testcase( pExpr->op==TK_RAISE );
111897 pWalker->eCode = 0;
111898 return WRC_Abort2;
111899 case TK_VARIABLE157:
111900 if( pWalker->eCode==5 ){
111901 /* Silently convert bound parameters that appear inside of CREATE
111902 ** statements into a NULL when parsing the CREATE statement text out
111903 ** of the sqlite_schema table */
111904 pExpr->op = TK_NULL122;
111905 }else if( pWalker->eCode==4 ){
111906 /* A bound parameter in a CREATE statement that originates from
111907 ** sqlite3_prepare() causes an error */
111908 pWalker->eCode = 0;
111909 return WRC_Abort2;
111910 }
111911 /* no break */ deliberate_fall_through__attribute__((fallthrough));
111912 default:
111913 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
111914 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
111915 return WRC_Continue0;
111916 }
111917}
111918static int exprIsConst(Parse *pParse, Expr *p, int initFlag){
111919 Walker w;
111920 w.eCode = initFlag;
111921 w.pParse = pParse;
111922 w.xExprCallback = exprNodeIsConstant;
111923 w.xSelectCallback = sqlite3SelectWalkFail;
111924#ifdef SQLITE_DEBUG
111925 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
111926#endif
111927 sqlite3WalkExpr(&w, p);
111928 return w.eCode;
111929}
111930
111931/*
111932** Walk an expression tree. Return non-zero if the expression is constant
111933** and 0 if it involves variables or function calls.
111934**
111935** For the purposes of this function, a double-quoted string (ex: "abc")
111936** is considered a variable but a single-quoted string (ex: 'abc') is
111937** a constant.
111938**
111939** The pParse parameter may be NULL. But if it is NULL, there is no way
111940** to determine if function calls are constant or not, and hence all
111941** function calls will be considered to be non-constant. If pParse is
111942** not NULL, then a function call might be constant, depending on the
111943** function and on its parameters.
111944*/
111945SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Parse *pParse, Expr *p){
111946 return exprIsConst(pParse, p, 1);
111947}
111948
111949/*
111950** Walk an expression tree. Return non-zero if
111951**
111952** (1) the expression is constant, and
111953** (2) the expression does originate in the ON or USING clause
111954** of a LEFT JOIN, and
111955** (3) the expression does not contain any EP_FixedCol TK_COLUMN
111956** operands created by the constant propagation optimization.
111957**
111958** When this routine returns true, it indicates that the expression
111959** can be added to the pParse->pConstExpr list and evaluated once when
111960** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce().
111961*/
111962static int sqlite3ExprIsConstantNotJoin(Parse *pParse, Expr *p){
111963 return exprIsConst(pParse, p, 2);
111964}
111965
111966/*
111967** This routine examines sub-SELECT statements as an expression is being
111968** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered
111969** constant as long as they are uncorrelated - meaning that they do not
111970** contain any terms from outer contexts.
111971*/
111972static int exprSelectWalkTableConstant(Walker *pWalker, Select *pSelect){
111973 assert( pSelect!=0 )((void) (0));
111974 assert( pWalker->eCode==3 || pWalker->eCode==0 )((void) (0));
111975 if( (pSelect->selFlags & SF_Correlated0x20000000)!=0 ){
111976 pWalker->eCode = 0;
111977 return WRC_Abort2;
111978 }
111979 return WRC_Prune1;
111980}
111981
111982/*
111983** Walk an expression tree. Return non-zero if the expression is constant
111984** for any single row of the table with cursor iCur. In other words, the
111985** expression must not refer to any non-deterministic function nor any
111986** table other than iCur.
111987**
111988** Consider uncorrelated subqueries to be constants if the bAllowSubq
111989** parameter is true.
111990*/
111991static int sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq){
111992 Walker w;
111993 w.eCode = 3;
111994 w.pParse = 0;
111995 w.xExprCallback = exprNodeIsConstant;
111996 if( bAllowSubq ){
111997 w.xSelectCallback = exprSelectWalkTableConstant;
111998 }else{
111999 w.xSelectCallback = sqlite3SelectWalkFail;
112000#ifdef SQLITE_DEBUG
112001 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
112002#endif
112003 }
112004 w.u.iCur = iCur;
112005 sqlite3WalkExpr(&w, p);
112006 return w.eCode;
112007}
112008
112009/*
112010** Check pExpr to see if it is an constraint on the single data source
112011** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
112012** constrains pSrc but does not depend on any other tables or data
112013** sources anywhere else in the query. Return true (non-zero) if pExpr
112014** is a constraint on pSrc only.
112015**
112016** This is an optimization. False negatives will perhaps cause slower
112017** queries, but false positives will yield incorrect answers. So when in
112018** doubt, return 0.
112019**
112020** To be an single-source constraint, the following must be true:
112021**
112022** (1) pExpr cannot refer to any table other than pSrc->iCursor.
112023**
112024** (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is
112025** true and the subquery is non-correlated
112026**
112027** (2b) pExpr cannot use non-deterministic functions.
112028**
112029** (3) pSrc cannot be part of the left operand for a RIGHT JOIN.
112030** (Is there some way to relax this constraint?)
112031**
112032** (4) If pSrc is the right operand of a LEFT JOIN, then...
112033** (4a) pExpr must come from an ON clause..
112034** (4b) and specifically the ON clause associated with the LEFT JOIN.
112035**
112036** (5) If pSrc is not the right operand of a LEFT JOIN or the left
112037** operand of a RIGHT JOIN, then pExpr must be from the WHERE
112038** clause, not an ON clause.
112039**
112040** (6) Either:
112041**
112042** (6a) pExpr does not originate in an ON or USING clause, or
112043**
112044** (6b) The ON or USING clause from which pExpr is derived is
112045** not to the left of a RIGHT JOIN (or FULL JOIN).
112046**
112047** Without this restriction, accepting pExpr as a single-table
112048** constraint might move the the ON/USING filter expression
112049** from the left side of a RIGHT JOIN over to the right side,
112050** which leads to incorrect answers. See also restriction (9)
112051** on push-down.
112052*/
112053SQLITE_PRIVATEstatic int sqlite3ExprIsSingleTableConstraint(
112054 Expr *pExpr, /* The constraint */
112055 const SrcList *pSrcList, /* Complete FROM clause */
112056 int iSrc, /* Which element of pSrcList to use */
112057 int bAllowSubq /* Allow non-correlated subqueries */
112058){
112059 const SrcItem *pSrc = &pSrcList->a[iSrc];
112060 if( pSrc->fg.jointype & JT_LTORJ0x40 ){
112061 return 0; /* rule (3) */
112062 }
112063 if( pSrc->fg.jointype & JT_LEFT0x08 ){
112064 if( !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return 0; /* rule (4a) */
112065 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
112066 }else{
112067 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return 0; /* rule (5) */
112068 }
112069 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) /* (6a) */
112070 && (pSrcList->a[0].fg.jointype & JT_LTORJ0x40)!=0 /* Fast pre-test of (6b) */
112071 ){
112072 int jj;
112073 for(jj=0; jj<iSrc; jj++){
112074 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
112075 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ0x40)!=0 ){
112076 return 0; /* restriction (6) */
112077 }
112078 break;
112079 }
112080 }
112081 }
112082 /* Rules (1), (2a), and (2b) handled by the following: */
112083 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq);
112084}
112085
112086
112087/*
112088** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
112089*/
112090static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
112091 ExprList *pGroupBy = pWalker->u.pGroupBy;
112092 int i;
112093
112094 /* Check if pExpr is identical to any GROUP BY term. If so, consider
112095 ** it constant. */
112096 for(i=0; i<pGroupBy->nExpr; i++){
112097 Expr *p = pGroupBy->a[i].pExpr;
112098 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
112099 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
112100 if( sqlite3IsBinary(pColl) ){
112101 return WRC_Prune1;
112102 }
112103 }
112104 }
112105
112106 /* Check if pExpr is a sub-select. If so, consider it variable. */
112107 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
112108 pWalker->eCode = 0;
112109 return WRC_Abort2;
112110 }
112111
112112 return exprNodeIsConstant(pWalker, pExpr);
112113}
112114
112115/*
112116** Walk the expression tree passed as the first argument. Return non-zero
112117** if the expression consists entirely of constants or copies of terms
112118** in pGroupBy that sort with the BINARY collation sequence.
112119**
112120** This routine is used to determine if a term of the HAVING clause can
112121** be promoted into the WHERE clause. In order for such a promotion to work,
112122** the value of the HAVING clause term must be the same for all members of
112123** a "group". The requirement that the GROUP BY term must be BINARY
112124** assumes that no other collating sequence will have a finer-grained
112125** grouping than binary. In other words (A=B COLLATE binary) implies
112126** A=B in every other collating sequence. The requirement that the
112127** GROUP BY be BINARY is stricter than necessary. It would also work
112128** to promote HAVING clauses that use the same alternative collating
112129** sequence as the GROUP BY term, but that is much harder to check,
112130** alternative collating sequences are uncommon, and this is only an
112131** optimization, so we take the easy way out and simply require the
112132** GROUP BY to use the BINARY collating sequence.
112133*/
112134SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
112135 Walker w;
112136 w.eCode = 1;
112137 w.xExprCallback = exprNodeIsConstantOrGroupBy;
112138 w.xSelectCallback = 0;
112139 w.u.pGroupBy = pGroupBy;
112140 w.pParse = pParse;
112141 sqlite3WalkExpr(&w, p);
112142 return w.eCode;
112143}
112144
112145/*
112146** Walk an expression tree for the DEFAULT field of a column definition
112147** in a CREATE TABLE statement. Return non-zero if the expression is
112148** acceptable for use as a DEFAULT. That is to say, return non-zero if
112149** the expression is constant or a function call with constant arguments.
112150** Return and 0 if there are any variables.
112151**
112152** isInit is true when parsing from sqlite_schema. isInit is false when
112153** processing a new CREATE TABLE statement. When isInit is true, parameters
112154** (such as ? or $abc) in the expression are converted into NULL. When
112155** isInit is false, parameters raise an error. Parameters should not be
112156** allowed in a CREATE TABLE statement, but some legacy versions of SQLite
112157** allowed it, so we need to support it when reading sqlite_schema for
112158** backwards compatibility.
112159**
112160** If isInit is true, set EP_FromDDL on every TK_FUNCTION node.
112161**
112162** For the purposes of this function, a double-quoted string (ex: "abc")
112163** is considered a variable but a single-quoted string (ex: 'abc') is
112164** a constant.
112165*/
112166SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
112167 assert( isInit==0 || isInit==1 )((void) (0));
112168 return exprIsConst(0, p, 4+isInit);
112169}
112170
112171#ifdef SQLITE_ENABLE_CURSOR_HINTS
112172/*
112173** Walk an expression tree. Return 1 if the expression contains a
112174** subquery of some kind. Return 0 if there are no subqueries.
112175*/
112176SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr *p){
112177 Walker w;
112178 w.eCode = 1;
112179 w.xExprCallback = sqlite3ExprWalkNoop;
112180 w.xSelectCallback = sqlite3SelectWalkFail;
112181#ifdef SQLITE_DEBUG
112182 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
112183#endif
112184 sqlite3WalkExpr(&w, p);
112185 return w.eCode==0;
112186}
112187#endif
112188
112189/*
112190** If the expression p codes a constant integer that is small enough
112191** to fit in a 32-bit integer, return 1 and put the value of the integer
112192** in *pValue. If the expression is not an integer or if it is too big
112193** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
112194**
112195** If the pParse pointer is provided, then allow the expression p to be
112196** a parameter (TK_VARIABLE) that is bound to an integer.
112197** But if pParse is NULL, then p must be a pure integer literal.
112198*/
112199SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse){
112200 int rc = 0;
112201 if( NEVER(p==0)(p==0) ) return 0; /* Used to only happen following on OOM */
112202
112203 /* If an expression is an integer literal that fits in a signed 32-bit
112204 ** integer, then the EP_IntValue flag will have already been set */
112205 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0((void) (0))
112206 || sqlite3GetInt32(p->u.zToken, &rc)==0 )((void) (0));
112207
112208 if( p->flags & EP_IntValue0x000800 ){
112209 *pValue = p->u.iValue;
112210 return 1;
112211 }
112212 switch( p->op ){
112213 case TK_UPLUS173: {
112214 rc = sqlite3ExprIsInteger(p->pLeft, pValue, 0);
112215 break;
112216 }
112217 case TK_UMINUS174: {
112218 int v = 0;
112219 if( sqlite3ExprIsInteger(p->pLeft, &v, 0) ){
112220 assert( ((unsigned int)v)!=0x80000000 )((void) (0));
112221 *pValue = -v;
112222 rc = 1;
112223 }
112224 break;
112225 }
112226 case TK_VARIABLE157: {
112227 sqlite3_value *pVal;
112228 if( pParse==0 ) break;
112229 if( NEVER(pParse->pVdbe==0)(pParse->pVdbe==0) ) break;
112230 if( (pParse->db->flags & SQLITE_EnableQPSG0x00800000)!=0 ) break;
112231 sqlite3VdbeSetVarmask(pParse->pVdbe, p->iColumn);
112232 pVal = sqlite3VdbeGetBoundValue(pParse->pReprepare, p->iColumn,
112233 SQLITE_AFF_BLOB0x41);
112234 if( pVal ){
112235 if( sqlite3_value_type(pVal)==SQLITE_INTEGER1 ){
112236 sqlite3_int64 vv = sqlite3_value_int64(pVal);
112237 if( vv == (vv & 0x7fffffff) ){ /* non-negative numbers only */
112238 *pValue = (int)vv;
112239 rc = 1;
112240 }
112241 }
112242 sqlite3ValueFree(pVal);
112243 }
112244 break;
112245 }
112246 default: break;
112247 }
112248 return rc;
112249}
112250
112251/*
112252** Return FALSE if there is no chance that the expression can be NULL.
112253**
112254** If the expression might be NULL or if the expression is too complex
112255** to tell return TRUE.
112256**
112257** This routine is used as an optimization, to skip OP_IsNull opcodes
112258** when we know that a value cannot be NULL. Hence, a false positive
112259** (returning TRUE when in fact the expression can never be NULL) might
112260** be a small performance hit but is otherwise harmless. On the other
112261** hand, a false negative (returning FALSE when the result could be NULL)
112262** will likely result in an incorrect answer. So when in doubt, return
112263** TRUE.
112264*/
112265SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr *p){
112266 u8 op;
112267 assert( p!=0 )((void) (0));
112268 while( p->op==TK_UPLUS173 || p->op==TK_UMINUS174 ){
112269 p = p->pLeft;
112270 assert( p!=0 )((void) (0));
112271 }
112272 op = p->op;
112273 if( op==TK_REGISTER176 ) op = p->op2;
112274 switch( op ){
112275 case TK_INTEGER156:
112276 case TK_STRING118:
112277 case TK_FLOAT154:
112278 case TK_BLOB155:
112279 return 0;
112280 case TK_COLUMN168:
112281 assert( ExprUseYTab(p) )((void) (0));
112282 return ExprHasProperty(p, EP_CanBeNull)(((p)->flags&(0x200000))!=0)
112283 || NEVER(p->y.pTab==0)(p->y.pTab==0) /* Reference to column of index on expr */
112284#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
112285 || (p->iColumn==XN_ROWID(-1) && IsView(p->y.pTab)((p->y.pTab)->eTabType==2))
112286#endif
112287 || (p->iColumn>=0
112288 && p->y.pTab->aCol!=0 /* Possible due to prior error */
112289 && ALWAYS(p->iColumn<p->y.pTab->nCol)(p->iColumn<p->y.pTab->nCol)
112290 && p->y.pTab->aCol[p->iColumn].notNull==0);
112291 default:
112292 return 1;
112293 }
112294}
112295
112296/*
112297** Return TRUE if the given expression is a constant which would be
112298** unchanged by OP_Affinity with the affinity given in the second
112299** argument.
112300**
112301** This routine is used to determine if the OP_Affinity operation
112302** can be omitted. When in doubt return FALSE. A false negative
112303** is harmless. A false positive, however, can result in the wrong
112304** answer.
112305*/
112306SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
112307 u8 op;
112308 int unaryMinus = 0;
112309 if( aff==SQLITE_AFF_BLOB0x41 ) return 1;
112310 while( p->op==TK_UPLUS173 || p->op==TK_UMINUS174 ){
112311 if( p->op==TK_UMINUS174 ) unaryMinus = 1;
112312 p = p->pLeft;
112313 }
112314 op = p->op;
112315 if( op==TK_REGISTER176 ) op = p->op2;
112316 switch( op ){
112317 case TK_INTEGER156: {
112318 return aff>=SQLITE_AFF_NUMERIC0x43;
112319 }
112320 case TK_FLOAT154: {
112321 return aff>=SQLITE_AFF_NUMERIC0x43;
112322 }
112323 case TK_STRING118: {
112324 return !unaryMinus && aff==SQLITE_AFF_TEXT0x42;
112325 }
112326 case TK_BLOB155: {
112327 return !unaryMinus;
112328 }
112329 case TK_COLUMN168: {
112330 assert( p->iTable>=0 )((void) (0)); /* p cannot be part of a CHECK constraint */
112331 return aff>=SQLITE_AFF_NUMERIC0x43 && p->iColumn<0;
112332 }
112333 default: {
112334 return 0;
112335 }
112336 }
112337}
112338
112339/*
112340** Return TRUE if the given string is a row-id column name.
112341*/
112342SQLITE_PRIVATEstatic int sqlite3IsRowid(const char *z){
112343 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
112344 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
112345 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
112346 return 0;
112347}
112348
112349/*
112350** Return a pointer to a buffer containing a usable rowid alias for table
112351** pTab. An alias is usable if there is not an explicit user-defined column
112352** of the same name.
112353*/
112354SQLITE_PRIVATEstatic const char *sqlite3RowidAlias(Table *pTab){
112355 const char *azOpt[] = {"_ROWID_", "ROWID", "OID"};
112356 int ii;
112357 assert( VisibleRowid(pTab) )((void) (0));
112358 for(ii=0; ii<ArraySize(azOpt)((int)(sizeof(azOpt)/sizeof(azOpt[0]))); ii++){
112359 int iCol;
112360 for(iCol=0; iCol<pTab->nCol; iCol++){
112361 if( sqlite3_stricmp(azOpt[ii], pTab->aCol[iCol].zCnName)==0 ) break;
112362 }
112363 if( iCol==pTab->nCol ){
112364 return azOpt[ii];
112365 }
112366 }
112367 return 0;
112368}
112369
112370/*
112371** pX is the RHS of an IN operator. If pX is a SELECT statement
112372** that can be simplified to a direct table access, then return
112373** a pointer to the SELECT statement. If pX is not a SELECT statement,
112374** or if the SELECT statement needs to be materialized into a transient
112375** table, then return NULL.
112376*/
112377#ifndef SQLITE_OMIT_SUBQUERY
112378static Select *isCandidateForInOpt(const Expr *pX){
112379 Select *p;
112380 SrcList *pSrc;
112381 ExprList *pEList;
112382 Table *pTab;
112383 int i;
112384 if( !ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) ) return 0; /* Not a subquery */
112385 if( ExprHasProperty(pX, EP_VarSelect)(((pX)->flags&(0x000040))!=0) ) return 0; /* Correlated subq */
112386 p = pX->x.pSelect;
112387 if( p->pPrior ) return 0; /* Not a compound SELECT */
112388 if( p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008) ){
112389 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
112390 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
112391 return 0; /* No DISTINCT keyword and no aggregate functions */
112392 }
112393 assert( p->pGroupBy==0 )((void) (0)); /* Has no GROUP BY clause */
112394 if( p->pLimit ) return 0; /* Has no LIMIT clause */
112395 if( p->pWhere ) return 0; /* Has no WHERE clause */
112396 pSrc = p->pSrc;
112397 assert( pSrc!=0 )((void) (0));
112398 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
112399 if( pSrc->a[0].fg.isSubquery) return 0;/* FROM is not a subquery or view */
112400 pTab = pSrc->a[0].pSTab;
112401 assert( pTab!=0 )((void) (0));
112402 assert( !IsView(pTab) )((void) (0)); /* FROM clause is not a view */
112403 if( IsVirtual(pTab)((pTab)->eTabType==1) ) return 0; /* FROM clause not a virtual table */
112404 pEList = p->pEList;
112405 assert( pEList!=0 )((void) (0));
112406 /* All SELECT results must be columns. */
112407 for(i=0; i<pEList->nExpr; i++){
112408 Expr *pRes = pEList->a[i].pExpr;
112409 if( pRes->op!=TK_COLUMN168 ) return 0;
112410 assert( pRes->iTable==pSrc->a[0].iCursor )((void) (0)); /* Not a correlated subquery */
112411 }
112412 return p;
112413}
112414#endif /* SQLITE_OMIT_SUBQUERY */
112415
112416#ifndef SQLITE_OMIT_SUBQUERY
112417/*
112418** Generate code that checks the left-most column of index table iCur to see if
112419** it contains any NULL entries. Cause the register at regHasNull to be set
112420** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
112421** to be set to NULL if iCur contains one or more NULL values.
112422*/
112423static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
112424 int addr1;
112425 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regHasNull);
112426 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
112427 sqlite3VdbeAddOp3(v, OP_Column94, iCur, 0, regHasNull);
112428 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
112429 VdbeComment((v, "first_entry_in(%d)", iCur));
112430 sqlite3VdbeJumpHere(v, addr1);
112431}
112432#endif
112433
112434
112435#ifndef SQLITE_OMIT_SUBQUERY
112436/*
112437** The argument is an IN operator with a list (not a subquery) on the
112438** right-hand side. Return TRUE if that list is constant.
112439*/
112440static int sqlite3InRhsIsConstant(Parse *pParse, Expr *pIn){
112441 Expr *pLHS;
112442 int res;
112443 assert( !ExprHasProperty(pIn, EP_xIsSelect) )((void) (0));
112444 pLHS = pIn->pLeft;
112445 pIn->pLeft = 0;
112446 res = sqlite3ExprIsConstant(pParse, pIn);
112447 pIn->pLeft = pLHS;
112448 return res;
112449}
112450#endif
112451
112452/*
112453** This function is used by the implementation of the IN (...) operator.
112454** The pX parameter is the expression on the RHS of the IN operator, which
112455** might be either a list of expressions or a subquery.
112456**
112457** The job of this routine is to find or create a b-tree object that can
112458** be used either to test for membership in the RHS set or to iterate through
112459** all members of the RHS set, skipping duplicates.
112460**
112461** A cursor is opened on the b-tree object that is the RHS of the IN operator
112462** and the *piTab parameter is set to the index of that cursor.
112463**
112464** The returned value of this function indicates the b-tree type, as follows:
112465**
112466** IN_INDEX_ROWID - The cursor was opened on a database table.
112467** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
112468** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
112469** IN_INDEX_EPH - The cursor was opened on a specially created and
112470** populated ephemeral table.
112471** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
112472** implemented as a sequence of comparisons.
112473**
112474** An existing b-tree might be used if the RHS expression pX is a simple
112475** subquery such as:
112476**
112477** SELECT <column1>, <column2>... FROM <table>
112478**
112479** If the RHS of the IN operator is a list or a more complex subquery, then
112480** an ephemeral table might need to be generated from the RHS and then
112481** pX->iTable made to point to the ephemeral table instead of an
112482** existing table. In this case, the creation and initialization of the
112483** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag
112484** will be set on pX and the pX->y.sub fields will be set to show where
112485** the subroutine is coded.
112486**
112487** The inFlags parameter must contain, at a minimum, one of the bits
112488** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
112489** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
112490** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
112491** be used to loop over all values of the RHS of the IN operator.
112492**
112493** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
112494** through the set members) then the b-tree must not contain duplicates.
112495** An ephemeral table will be created unless the selected columns are guaranteed
112496** to be unique - either because it is an INTEGER PRIMARY KEY or due to
112497** a UNIQUE constraint or index.
112498**
112499** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
112500** for fast set membership tests) then an ephemeral table must
112501** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
112502** index can be found with the specified <columns> as its left-most.
112503**
112504** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
112505** if the RHS of the IN operator is a list (not a subquery) then this
112506** routine might decide that creating an ephemeral b-tree for membership
112507** testing is too expensive and return IN_INDEX_NOOP. In that case, the
112508** calling routine should implement the IN operator using a sequence
112509** of Eq or Ne comparison operations.
112510**
112511** When the b-tree is being used for membership tests, the calling function
112512** might need to know whether or not the RHS side of the IN operator
112513** contains a NULL. If prRhsHasNull is not a NULL pointer and
112514** if there is any chance that the (...) might contain a NULL value at
112515** runtime, then a register is allocated and the register number written
112516** to *prRhsHasNull. If there is no chance that the (...) contains a
112517** NULL value, then *prRhsHasNull is left unchanged.
112518**
112519** If a register is allocated and its location stored in *prRhsHasNull, then
112520** the value in that register will be NULL if the b-tree contains one or more
112521** NULL values, and it will be some non-NULL value if the b-tree contains no
112522** NULL values.
112523**
112524** If the aiMap parameter is not NULL, it must point to an array containing
112525** one element for each column returned by the SELECT statement on the RHS
112526** of the IN(...) operator. The i'th entry of the array is populated with the
112527** offset of the index column that matches the i'th column returned by the
112528** SELECT. For example, if the expression and selected index are:
112529**
112530** (?,?,?) IN (SELECT a, b, c FROM t1)
112531** CREATE INDEX i1 ON t1(b, c, a);
112532**
112533** then aiMap[] is populated with {2, 0, 1}.
112534*/
112535#ifndef SQLITE_OMIT_SUBQUERY
112536SQLITE_PRIVATEstatic int sqlite3FindInIndex(
112537 Parse *pParse, /* Parsing context */
112538 Expr *pX, /* The IN expression */
112539 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
112540 int *prRhsHasNull, /* Register holding NULL status. See notes */
112541 int *aiMap, /* Mapping from Index fields to RHS fields */
112542 int *piTab /* OUT: index to use */
112543){
112544 Select *p; /* SELECT to the right of IN operator */
112545 int eType = 0; /* Type of RHS table. IN_INDEX_* */
112546 int iTab; /* Cursor of the RHS table */
112547 int mustBeUnique; /* True if RHS must be unique */
112548 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
112549
112550 assert( pX->op==TK_IN )((void) (0));
112551 mustBeUnique = (inFlags & IN_INDEX_LOOP0x0004)!=0;
112552 iTab = pParse->nTab++;
112553
112554 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
112555 ** whether or not the SELECT result contains NULL values, check whether
112556 ** or not NULL is actually possible (it may not be, for example, due
112557 ** to NOT NULL constraints in the schema). If no NULL values are possible,
112558 ** set prRhsHasNull to 0 before continuing. */
112559 if( prRhsHasNull && ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) ){
112560 int i;
112561 ExprList *pEList = pX->x.pSelect->pEList;
112562 for(i=0; i<pEList->nExpr; i++){
112563 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
112564 }
112565 if( i==pEList->nExpr ){
112566 prRhsHasNull = 0;
112567 }
112568 }
112569
112570 /* Check to see if an existing table or index can be used to
112571 ** satisfy the query. This is preferable to generating a new
112572 ** ephemeral table. */
112573 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
112574 sqlite3 *db = pParse->db; /* Database connection */
112575 Table *pTab; /* Table <table>. */
112576 int iDb; /* Database idx for pTab */
112577 ExprList *pEList = p->pEList;
112578 int nExpr = pEList->nExpr;
112579
112580 assert( p->pEList!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
112581 assert( p->pEList->a[0].pExpr!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
112582 assert( p->pSrc!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
112583 pTab = p->pSrc->a[0].pSTab;
112584
112585 /* Code an OP_Transaction and OP_TableLock for <table>. */
112586 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112587 assert( iDb>=0 && iDb<SQLITE_MAX_DB )((void) (0));
112588 sqlite3CodeVerifySchema(pParse, iDb);
112589 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
112590
112591 assert(v)((void) (0)); /* sqlite3GetVdbe() has always been previously called */
112592 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
112593 /* The "x IN (SELECT rowid FROM table)" case */
112594 int iAddr = sqlite3VdbeAddOp0(v, OP_Once15);
112595 VdbeCoverage(v);
112596
112597 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead102);
112598 eType = IN_INDEX_ROWID1;
112599 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR"
,pTab->zName)
112600 "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)
;
112601 sqlite3VdbeJumpHere(v, iAddr);
112602 }else{
112603 Index *pIdx; /* Iterator variable */
112604 int affinity_ok = 1;
112605 int i;
112606
112607 /* Check that the affinity that will be used to perform each
112608 ** comparison is the same as the affinity of each column in table
112609 ** on the RHS of the IN operator. If it not, it is not possible to
112610 ** use any index of the RHS table. */
112611 for(i=0; i<nExpr && affinity_ok; i++){
112612 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
112613 int iCol = pEList->a[i].pExpr->iColumn;
112614 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
112615 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
112616 testcase( cmpaff==SQLITE_AFF_BLOB );
112617 testcase( cmpaff==SQLITE_AFF_TEXT );
112618 switch( cmpaff ){
112619 case SQLITE_AFF_BLOB0x41:
112620 break;
112621 case SQLITE_AFF_TEXT0x42:
112622 /* sqlite3CompareAffinity() only returns TEXT if one side or the
112623 ** other has no affinity and the other side is TEXT. Hence,
112624 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
112625 ** and for the term on the LHS of the IN to have no affinity. */
112626 assert( idxaff==SQLITE_AFF_TEXT )((void) (0));
112627 break;
112628 default:
112629 affinity_ok = sqlite3IsNumericAffinity(idxaff)((idxaff)>=0x43);
112630 }
112631 }
112632
112633 if( affinity_ok ){
112634 /* Search for an existing index that will work for this IN operator */
112635 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
112636 Bitmask colUsed; /* Columns of the index used */
112637 Bitmask mCol; /* Mask for the current column */
112638 if( pIdx->nColumn<nExpr ) continue;
112639 if( pIdx->pPartIdxWhere!=0 ) continue;
112640 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
112641 ** BITMASK(nExpr) without overflowing */
112642 testcase( pIdx->nColumn==BMS-2 );
112643 testcase( pIdx->nColumn==BMS-1 );
112644 if( pIdx->nColumn>=BMS((int)(sizeof(Bitmask)*8))-1 ) continue;
112645 if( mustBeUnique ){
112646 if( pIdx->nKeyCol>nExpr
112647 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)((pIdx)->onError!=0))
112648 ){
112649 continue; /* This index is not unique over the IN RHS columns */
112650 }
112651 }
112652
112653 colUsed = 0; /* Columns of index used so far */
112654 for(i=0; i<nExpr; i++){
112655 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
112656 Expr *pRhs = pEList->a[i].pExpr;
112657 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
112658 int j;
112659
112660 for(j=0; j<nExpr; j++){
112661 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
112662 assert( pIdx->azColl[j] )((void) (0));
112663 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
112664 continue;
112665 }
112666 break;
112667 }
112668 if( j==nExpr ) break;
112669 mCol = MASKBIT(j)(((Bitmask)1)<<(j));
112670 if( mCol & colUsed ) break; /* Each column used only once */
112671 colUsed |= mCol;
112672 if( aiMap ) aiMap[i] = j;
112673 }
112674
112675 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) )((void) (0));
112676 if( colUsed==(MASKBIT(nExpr)(((Bitmask)1)<<(nExpr))-1) ){
112677 /* If we reach this point, that means the index pIdx is usable */
112678 int iAddr = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
112679 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
112680 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName))sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
;
112681 sqlite3VdbeAddOp3(v, OP_OpenRead102, iTab, pIdx->tnum, iDb);
112682 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
112683 VdbeComment((v, "%s", pIdx->zName));
112684 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 )((void) (0));
112685 eType = IN_INDEX_INDEX_ASC3 + pIdx->aSortOrder[0];
112686
112687 if( prRhsHasNull ){
112688#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
112689 i64 mask = (1<<nExpr)-1;
112690 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123,
112691 iTab, 0, 0, (u8*)&mask, P4_INT64(-13));
112692#endif
112693 *prRhsHasNull = ++pParse->nMem;
112694 if( nExpr==1 ){
112695 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
112696 }
112697 }
112698 sqlite3VdbeJumpHere(v, iAddr);
112699 }
112700 } /* End loop over indexes */
112701 } /* End if( affinity_ok ) */
112702 } /* End if not an rowid index */
112703 } /* End attempt to optimize using an index */
112704
112705 /* If no preexisting index is available for the IN clause
112706 ** and IN_INDEX_NOOP is an allowed reply
112707 ** and the RHS of the IN operator is a list, not a subquery
112708 ** and the RHS is not constant or has two or fewer terms,
112709 ** then it is not worth creating an ephemeral table to evaluate
112710 ** the IN operator so return IN_INDEX_NOOP.
112711 */
112712 if( eType==0
112713 && (inFlags & IN_INDEX_NOOP_OK0x0001)
112714 && ExprUseXList(pX)(((pX)->flags&0x001000)==0)
112715 && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2)
112716 ){
112717 pParse->nTab--; /* Back out the allocation of the unused cursor */
112718 iTab = -1; /* Cursor is not allocated */
112719 eType = IN_INDEX_NOOP5;
112720 }
112721
112722 if( eType==0 ){
112723 /* Could not find an existing table or index to use as the RHS b-tree.
112724 ** We will have to generate an ephemeral table to do the job.
112725 */
112726 u32 savedNQueryLoop = pParse->nQueryLoop;
112727 int rMayHaveNull = 0;
112728 eType = IN_INDEX_EPH2;
112729 if( inFlags & IN_INDEX_LOOP0x0004 ){
112730 pParse->nQueryLoop = 0;
112731 }else if( prRhsHasNull ){
112732 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
112733 }
112734 assert( pX->op==TK_IN )((void) (0));
112735 sqlite3CodeRhsOfIN(pParse, pX, iTab);
112736 if( rMayHaveNull ){
112737 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
112738 }
112739 pParse->nQueryLoop = savedNQueryLoop;
112740 }
112741
112742 if( aiMap && eType!=IN_INDEX_INDEX_ASC3 && eType!=IN_INDEX_INDEX_DESC4 ){
112743 int i, n;
112744 n = sqlite3ExprVectorSize(pX->pLeft);
112745 for(i=0; i<n; i++) aiMap[i] = i;
112746 }
112747 *piTab = iTab;
112748 return eType;
112749}
112750#endif
112751
112752#ifndef SQLITE_OMIT_SUBQUERY
112753/*
112754** Argument pExpr is an (?, ?...) IN(...) expression. This
112755** function allocates and returns a nul-terminated string containing
112756** the affinities to be used for each column of the comparison.
112757**
112758** It is the responsibility of the caller to ensure that the returned
112759** string is eventually freed using sqlite3DbFree().
112760*/
112761static char *exprINAffinity(Parse *pParse, const Expr *pExpr){
112762 Expr *pLeft = pExpr->pLeft;
112763 int nVal = sqlite3ExprVectorSize(pLeft);
112764 Select *pSelect = ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ? pExpr->x.pSelect : 0;
112765 char *zRet;
112766
112767 assert( pExpr->op==TK_IN )((void) (0));
112768 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
112769 if( zRet ){
112770 int i;
112771 for(i=0; i<nVal; i++){
112772 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
112773 char a = sqlite3ExprAffinity(pA);
112774 if( pSelect ){
112775 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
112776 }else{
112777 zRet[i] = a;
112778 }
112779 }
112780 zRet[nVal] = '\0';
112781 }
112782 return zRet;
112783}
112784#endif
112785
112786#ifndef SQLITE_OMIT_SUBQUERY
112787/*
112788** Load the Parse object passed as the first argument with an error
112789** message of the form:
112790**
112791** "sub-select returns N columns - expected M"
112792*/
112793SQLITE_PRIVATEstatic void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
112794 if( pParse->nErr==0 ){
112795 const char *zFmt = "sub-select returns %d columns - expected %d";
112796 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
112797 }
112798}
112799#endif
112800
112801/*
112802** Expression pExpr is a vector that has been used in a context where
112803** it is not permitted. If pExpr is a sub-select vector, this routine
112804** loads the Parse object with a message of the form:
112805**
112806** "sub-select returns N columns - expected 1"
112807**
112808** Or, if it is a regular scalar vector:
112809**
112810** "row value misused"
112811*/
112812SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
112813#ifndef SQLITE_OMIT_SUBQUERY
112814 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
112815 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
112816 }else
112817#endif
112818 {
112819 sqlite3ErrorMsg(pParse, "row value misused");
112820 }
112821}
112822
112823#ifndef SQLITE_OMIT_SUBQUERY
112824/*
112825** Scan all previously generated bytecode looking for an OP_BeginSubrtn
112826** that is compatible with pExpr. If found, add the y.sub values
112827** to pExpr and return true. If not found, return false.
112828*/
112829static int findCompatibleInRhsSubrtn(
112830 Parse *pParse, /* Parsing context */
112831 Expr *pExpr, /* IN operator with RHS that we want to reuse */
112832 SubrtnSig *pNewSig /* Signature for the IN operator */
112833){
112834 VdbeOp *pOp, *pEnd;
112835 SubrtnSig *pSig;
112836 Vdbe *v;
112837
112838 if( pNewSig==0 ) return 0;
112839 if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0;
112840 assert( pExpr->op==TK_IN )((void) (0));
112841 assert( !ExprUseYSub(pExpr) )((void) (0));
112842 assert( ExprUseXSelect(pExpr) )((void) (0));
112843 assert( pExpr->x.pSelect!=0 )((void) (0));
112844 assert( (pExpr->x.pSelect->selFlags & SF_All)==0 )((void) (0));
112845 v = pParse->pVdbe;
112846 assert( v!=0 )((void) (0));
112847 pOp = sqlite3VdbeGetOp(v, 1);
112848 pEnd = sqlite3VdbeGetLastOp(v);
112849 for(; pOp<pEnd; pOp++){
112850 if( pOp->p4type!=P4_SUBRTNSIG(-17) ) continue;
112851 assert( pOp->opcode==OP_BeginSubrtn )((void) (0));
112852 pSig = pOp->p4.pSubrtnSig;
112853 assert( pSig!=0 )((void) (0));
112854 if( pNewSig->selId!=pSig->selId ) continue;
112855 if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue;
112856 pExpr->y.sub.iAddr = pSig->iAddr;
112857 pExpr->y.sub.regReturn = pSig->regReturn;
112858 pExpr->iTable = pSig->iTable;
112859 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
112860 return 1;
112861 }
112862 return 0;
112863}
112864#endif /* SQLITE_OMIT_SUBQUERY */
112865
112866#ifndef SQLITE_OMIT_SUBQUERY
112867/*
112868** Generate code that will construct an ephemeral table containing all terms
112869** in the RHS of an IN operator. The IN operator can be in either of two
112870** forms:
112871**
112872** x IN (4,5,11) -- IN operator with list on right-hand side
112873** x IN (SELECT a FROM b) -- IN operator with subquery on the right
112874**
112875** The pExpr parameter is the IN operator. The cursor number for the
112876** constructed ephemeral table is returned. The first time the ephemeral
112877** table is computed, the cursor number is also stored in pExpr->iTable,
112878** however the cursor number returned might not be the same, as it might
112879** have been duplicated using OP_OpenDup.
112880**
112881** If the LHS expression ("x" in the examples) is a column value, or
112882** the SELECT statement returns a column value, then the affinity of that
112883** column is used to build the index keys. If both 'x' and the
112884** SELECT... statement are columns, then numeric affinity is used
112885** if either column has NUMERIC or INTEGER affinity. If neither
112886** 'x' nor the SELECT... statement are columns, then numeric affinity
112887** is used.
112888*/
112889SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(
112890 Parse *pParse, /* Parsing context */
112891 Expr *pExpr, /* The IN operator */
112892 int iTab /* Use this cursor number */
112893){
112894 int addrOnce = 0; /* Address of the OP_Once instruction at top */
112895 int addr; /* Address of OP_OpenEphemeral instruction */
112896 Expr *pLeft; /* the LHS of the IN operator */
112897 KeyInfo *pKeyInfo = 0; /* Key information */
112898 int nVal; /* Size of vector pLeft */
112899 Vdbe *v; /* The prepared statement under construction */
112900
112901 v = pParse->pVdbe;
112902 assert( v!=0 )((void) (0));
112903
112904 /* The evaluation of the IN must be repeated every time it
112905 ** is encountered if any of the following is true:
112906 **
112907 ** * The right-hand side is a correlated subquery
112908 ** * The right-hand side is an expression list containing variables
112909 ** * We are inside a trigger
112910 **
112911 ** If all of the above are false, then we can compute the RHS just once
112912 ** and reuse it many names.
112913 */
112914 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(0x000040))!=0) && pParse->iSelfTab==0 ){
112915 /* Reuse of the RHS is allowed
112916 **
112917 ** Compute a signature for the RHS of the IN operator to facility
112918 ** finding and reusing prior instances of the same IN operator.
112919 */
112920 SubrtnSig *pSig = 0;
112921 assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 )((void) (0));
112922 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) && (pExpr->x.pSelect->selFlags & SF_All0x0000002)==0 ){
112923 pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0]));
112924 if( pSig ){
112925 pSig->selId = pExpr->x.pSelect->selId;
112926 pSig->zAff = exprINAffinity(pParse, pExpr);
112927 }
112928 }
112929
112930 /* Check to see if there is a prior materialization of the RHS of
112931 ** this IN operator. If there is, then make use of that prior
112932 ** materialization rather than recomputing it.
112933 */
112934 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0)
112935 || findCompatibleInRhsSubrtn(pParse, pExpr, pSig)
112936 ){
112937 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
112938 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
112939 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
112940 pExpr->x.pSelect->selId))sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
;
112941 }
112942 assert( ExprUseYSub(pExpr) )((void) (0));
112943 sqlite3VdbeAddOp2(v, OP_Gosub10, pExpr->y.sub.regReturn,
112944 pExpr->y.sub.iAddr);
112945 assert( iTab!=pExpr->iTable )((void) (0));
112946 sqlite3VdbeAddOp2(v, OP_OpenDup114, iTab, pExpr->iTable);
112947 sqlite3VdbeJumpHere(v, addrOnce);
112948 if( pSig ){
112949 sqlite3DbFree(pParse->db, pSig->zAff);
112950 sqlite3DbFree(pParse->db, pSig);
112951 }
112952 return;
112953 }
112954
112955 /* Begin coding the subroutine */
112956 assert( !ExprUseYWin(pExpr) )((void) (0));
112957 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
112958 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
112959 pExpr->y.sub.regReturn = ++pParse->nMem;
112960 pExpr->y.sub.iAddr =
112961 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pExpr->y.sub.regReturn) + 1;
112962 if( pSig ){
112963 pSig->iAddr = pExpr->y.sub.iAddr;
112964 pSig->regReturn = pExpr->y.sub.regReturn;
112965 pSig->iTable = iTab;
112966 pParse->mSubrtnSig = 1 << (pSig->selId&7);
112967 sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG(-17));
112968 }
112969 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
112970 }
112971
112972 /* Check to see if this is a vector IN operator */
112973 pLeft = pExpr->pLeft;
112974 nVal = sqlite3ExprVectorSize(pLeft);
112975
112976 /* Construct the ephemeral table that will contain the content of
112977 ** RHS of the IN operator.
112978 */
112979 pExpr->iTable = iTab;
112980 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pExpr->iTable, nVal);
112981#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
112982 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
112983 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
112984 }else{
112985 VdbeComment((v, "RHS of IN operator"));
112986 }
112987#endif
112988 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
112989
112990 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
112991 /* Case 1: expr IN (SELECT ...)
112992 **
112993 ** Generate code to write the results of the select into the temporary
112994 ** table allocated and opened above.
112995 */
112996 Select *pSelect = pExpr->x.pSelect;
112997 ExprList *pEList = pSelect->pEList;
112998
112999 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
113000 addrOnce?"":"CORRELATED ", pSelect->selIdsqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
113001 ))sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
;
113002 /* If the LHS and RHS of the IN operator do not match, that
113003 ** error will have been caught long before we reach this point. */
113004 if( ALWAYS(pEList->nExpr==nVal)(pEList->nExpr==nVal) ){
113005 Select *pCopy;
113006 SelectDest dest;
113007 int i;
113008 int rc;
113009 int addrBloom = 0;
113010 sqlite3SelectDestInit(&dest, SRT_Set11, iTab);
113011 dest.zAffSdst = exprINAffinity(pParse, pExpr);
113012 pSelect->iLimit = 0;
113013 if( addrOnce && OptimizationEnabled(pParse->db, SQLITE_BloomFilter)(((pParse->db)->dbOptFlags&(0x00080000))==0) ){
113014 int regBloom = ++pParse->nMem;
113015 addrBloom = sqlite3VdbeAddOp2(v, OP_Blob77, 10000, regBloom);
113016 VdbeComment((v, "Bloom filter"));
113017 dest.iSDParm2 = regBloom;
113018 }
113019 testcase( pSelect->selFlags & SF_Distinct );
113020 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
113021 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
113022 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
113023 sqlite3SelectDelete(pParse->db, pCopy);
113024 sqlite3DbFree(pParse->db, dest.zAffSdst);
113025 if( addrBloom ){
113026 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
113027 if( dest.iSDParm2==0 ){
113028 sqlite3VdbeChangeToNoop(v, addrBloom);
113029 }else{
113030 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
113031 }
113032 }
113033 if( rc ){
113034 sqlite3KeyInfoUnref(pKeyInfo);
113035 return;
113036 }
113037 assert( pKeyInfo!=0 )((void) (0)); /* OOM will cause exit after sqlite3Select() */
113038 assert( pEList!=0 )((void) (0));
113039 assert( pEList->nExpr>0 )((void) (0));
113040 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
113041 for(i=0; i<nVal; i++){
113042 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
113043 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
113044 pParse, p, pEList->a[i].pExpr
113045 );
113046 }
113047 }
113048 }else if( ALWAYS(pExpr->x.pList!=0)(pExpr->x.pList!=0) ){
113049 /* Case 2: expr IN (exprlist)
113050 **
113051 ** For each expression, build an index key from the evaluation and
113052 ** store it in the temporary table. If <expr> is a column, then use
113053 ** that columns affinity when building index keys. If <expr> is not
113054 ** a column, use numeric affinity.
113055 */
113056 char affinity; /* Affinity of the LHS of the IN */
113057 int i;
113058 ExprList *pList = pExpr->x.pList;
113059 struct ExprList_item *pItem;
113060 int r1, r2;
113061 affinity = sqlite3ExprAffinity(pLeft);
113062 if( affinity<=SQLITE_AFF_NONE0x40 ){
113063 affinity = SQLITE_AFF_BLOB0x41;
113064 }else if( affinity==SQLITE_AFF_REAL0x45 ){
113065 affinity = SQLITE_AFF_NUMERIC0x43;
113066 }
113067 if( pKeyInfo ){
113068 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
113069 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
113070 }
113071
113072 /* Loop through each expression in <exprlist>. */
113073 r1 = sqlite3GetTempReg(pParse);
113074 r2 = sqlite3GetTempReg(pParse);
113075 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
113076 Expr *pE2 = pItem->pExpr;
113077
113078 /* If the expression is not constant then we will need to
113079 ** disable the test that was generated above that makes sure
113080 ** this code only executes once. Because for a non-constant
113081 ** expression we need to rerun this code each time.
113082 */
113083 if( addrOnce && !sqlite3ExprIsConstant(pParse, pE2) ){
113084 sqlite3VdbeChangeToNoop(v, addrOnce-1);
113085 sqlite3VdbeChangeToNoop(v, addrOnce);
113086 ExprClearProperty(pExpr, EP_Subrtn)(pExpr)->flags&=~(0x2000000);
113087 addrOnce = 0;
113088 }
113089
113090 /* Evaluate the expression and insert it into the temp table */
113091 sqlite3ExprCode(pParse, pE2, r1);
113092 sqlite3VdbeAddOp4(v, OP_MakeRecord97, r1, 1, r2, &affinity, 1);
113093 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iTab, r2, r1, 1);
113094 }
113095 sqlite3ReleaseTempReg(pParse, r1);
113096 sqlite3ReleaseTempReg(pParse, r2);
113097 }
113098 if( pKeyInfo ){
113099 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO(-8));
113100 }
113101 if( addrOnce ){
113102 sqlite3VdbeAddOp1(v, OP_NullRow136, iTab);
113103 sqlite3VdbeJumpHere(v, addrOnce);
113104 /* Subroutine return */
113105 assert( ExprUseYSub(pExpr) )((void) (0));
113106 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn((void) (0))
113107 || pParse->nErr )((void) (0));
113108 sqlite3VdbeAddOp3(v, OP_Return67, pExpr->y.sub.regReturn,
113109 pExpr->y.sub.iAddr, 1);
113110 VdbeCoverage(v);
113111 sqlite3ClearTempRegCache(pParse);
113112 }
113113}
113114#endif /* SQLITE_OMIT_SUBQUERY */
113115
113116/*
113117** Generate code for scalar subqueries used as a subquery expression
113118** or EXISTS operator:
113119**
113120** (SELECT a FROM b) -- subquery
113121** EXISTS (SELECT a FROM b) -- EXISTS subquery
113122**
113123** The pExpr parameter is the SELECT or EXISTS operator to be coded.
113124**
113125** Return the register that holds the result. For a multi-column SELECT,
113126** the result is stored in a contiguous array of registers and the
113127** return value is the register of the left-most result column.
113128** Return 0 if an error occurs.
113129*/
113130#ifndef SQLITE_OMIT_SUBQUERY
113131SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
113132 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
113133 int rReg = 0; /* Register storing resulting */
113134 Select *pSel; /* SELECT statement to encode */
113135 SelectDest dest; /* How to deal with SELECT result */
113136 int nReg; /* Registers to allocate */
113137 Expr *pLimit; /* New limit expression */
113138#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
113139 int addrExplain; /* Address of OP_Explain instruction */
113140#endif
113141
113142 Vdbe *v = pParse->pVdbe;
113143 assert( v!=0 )((void) (0));
113144 if( pParse->nErr ) return 0;
113145 testcase( pExpr->op==TK_EXISTS );
113146 testcase( pExpr->op==TK_SELECT );
113147 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT )((void) (0));
113148 assert( ExprUseXSelect(pExpr) )((void) (0));
113149 pSel = pExpr->x.pSelect;
113150
113151 /* If this routine has already been coded, then invoke it as a
113152 ** subroutine. */
113153 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
113154 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId))sqlite3VdbeExplain (pParse, 0, "REUSE SUBQUERY %d", pSel->
selId)
;
113155 assert( ExprUseYSub(pExpr) )((void) (0));
113156 sqlite3VdbeAddOp2(v, OP_Gosub10, pExpr->y.sub.regReturn,
113157 pExpr->y.sub.iAddr);
113158 return pExpr->iTable;
113159 }
113160
113161 /* Begin coding the subroutine */
113162 assert( !ExprUseYWin(pExpr) )((void) (0));
113163 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) )((void) (0));
113164 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
113165 pExpr->y.sub.regReturn = ++pParse->nMem;
113166 pExpr->y.sub.iAddr =
113167 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pExpr->y.sub.regReturn) + 1;
113168
113169 /* The evaluation of the EXISTS/SELECT must be repeated every time it
113170 ** is encountered if any of the following is true:
113171 **
113172 ** * The right-hand side is a correlated subquery
113173 ** * The right-hand side is an expression list containing variables
113174 ** * We are inside a trigger
113175 **
113176 ** If all of the above are false, then we can run this code just once
113177 ** save the results, and reuse the same result on subsequent invocations.
113178 */
113179 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(0x000040))!=0) ){
113180 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
113181 }
113182
113183 /* For a SELECT, generate code to put the values for all columns of
113184 ** the first row into an array of registers and return the index of
113185 ** the first register.
113186 **
113187 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
113188 ** into a register and return that register number.
113189 **
113190 ** In both cases, the query is augmented with "LIMIT 1". Any
113191 ** preexisting limit is discarded in place of the new LIMIT 1.
113192 */
113193 ExplainQueryPlan2(addrExplain, (pParse, 1, "%sSCALAR SUBQUERY %d",(addrExplain = sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d"
, addrOnce?"":"CORRELATED ", pSel->selId))
113194 addrOnce?"":"CORRELATED ", pSel->selId))(addrExplain = sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d"
, addrOnce?"":"CORRELATED ", pSel->selId))
;
113195 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
113196 nReg = pExpr->op==TK_SELECT139 ? pSel->pEList->nExpr : 1;
113197 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
113198 pParse->nMem += nReg;
113199 if( pExpr->op==TK_SELECT139 ){
113200 dest.eDest = SRT_Mem10;
113201 dest.iSdst = dest.iSDParm;
113202 dest.nSdst = nReg;
113203 sqlite3VdbeAddOp3(v, OP_Null75, 0, dest.iSDParm, dest.iSDParm+nReg-1);
113204 VdbeComment((v, "Init subquery result"));
113205 }else{
113206 dest.eDest = SRT_Exists3;
113207 sqlite3VdbeAddOp2(v, OP_Integer71, 0, dest.iSDParm);
113208 VdbeComment((v, "Init EXISTS result"));
113209 }
113210 if( pSel->pLimit ){
113211 /* The subquery already has a limit. If the pre-existing limit is X
113212 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
113213 sqlite3 *db = pParse->db;
113214 pLimit = sqlite3Expr(db, TK_INTEGER156, "0");
113215 if( pLimit ){
113216 pLimit->affExpr = SQLITE_AFF_NUMERIC0x43;
113217 pLimit = sqlite3PExpr(pParse, TK_NE53,
113218 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
113219 }
113220 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
113221 pSel->pLimit->pLeft = pLimit;
113222 }else{
113223 /* If there is no pre-existing limit add a limit of 1 */
113224 pLimit = sqlite3Expr(pParse->db, TK_INTEGER156, "1");
113225 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT149, pLimit, 0);
113226 }
113227 pSel->iLimit = 0;
113228 if( sqlite3Select(pParse, pSel, &dest) ){
113229 pExpr->op2 = pExpr->op;
113230 pExpr->op = TK_ERROR182;
113231 return 0;
113232 }
113233 pExpr->iTable = rReg = dest.iSDParm;
113234 ExprSetVVAProperty(pExpr, EP_NoReduce);
113235 if( addrOnce ){
113236 sqlite3VdbeJumpHere(v, addrOnce);
113237 }
113238 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
113239
113240 /* Subroutine return */
113241 assert( ExprUseYSub(pExpr) )((void) (0));
113242 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn((void) (0))
113243 || pParse->nErr )((void) (0));
113244 sqlite3VdbeAddOp3(v, OP_Return67, pExpr->y.sub.regReturn,
113245 pExpr->y.sub.iAddr, 1);
113246 VdbeCoverage(v);
113247 sqlite3ClearTempRegCache(pParse);
113248 return rReg;
113249}
113250#endif /* SQLITE_OMIT_SUBQUERY */
113251
113252#ifndef SQLITE_OMIT_SUBQUERY
113253/*
113254** Expr pIn is an IN(...) expression. This function checks that the
113255** sub-select on the RHS of the IN() operator has the same number of
113256** columns as the vector on the LHS. Or, if the RHS of the IN() is not
113257** a sub-query, that the LHS is a vector of size 1.
113258*/
113259SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
113260 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
113261 if( ExprUseXSelect(pIn)(((pIn)->flags&0x001000)!=0) && !pParse->db->mallocFailed ){
113262 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
113263 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
113264 return 1;
113265 }
113266 }else if( nVector!=1 ){
113267 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
113268 return 1;
113269 }
113270 return 0;
113271}
113272#endif
113273
113274#ifndef SQLITE_OMIT_SUBQUERY
113275/*
113276** Generate code for an IN expression.
113277**
113278** x IN (SELECT ...)
113279** x IN (value, value, ...)
113280**
113281** The left-hand side (LHS) is a scalar or vector expression. The
113282** right-hand side (RHS) is an array of zero or more scalar values, or a
113283** subquery. If the RHS is a subquery, the number of result columns must
113284** match the number of columns in the vector on the LHS. If the RHS is
113285** a list of values, the LHS must be a scalar.
113286**
113287** The IN operator is true if the LHS value is contained within the RHS.
113288** The result is false if the LHS is definitely not in the RHS. The
113289** result is NULL if the presence of the LHS in the RHS cannot be
113290** determined due to NULLs.
113291**
113292** This routine generates code that jumps to destIfFalse if the LHS is not
113293** contained within the RHS. If due to NULLs we cannot determine if the LHS
113294** is contained in the RHS then jump to destIfNull. If the LHS is contained
113295** within the RHS then fall through.
113296**
113297** See the separate in-operator.md documentation file in the canonical
113298** SQLite source tree for additional information.
113299*/
113300static void sqlite3ExprCodeIN(
113301 Parse *pParse, /* Parsing and code generating context */
113302 Expr *pExpr, /* The IN expression */
113303 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
113304 int destIfNull /* Jump here if the results are unknown due to NULLs */
113305){
113306 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
113307 int eType; /* Type of the RHS */
113308 int rLhs; /* Register(s) holding the LHS values */
113309 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
113310 Vdbe *v; /* Statement under construction */
113311 int *aiMap = 0; /* Map from vector field to index column */
113312 char *zAff = 0; /* Affinity string for comparisons */
113313 int nVector; /* Size of vectors for this IN operator */
113314 int iDummy; /* Dummy parameter to exprCodeVector() */
113315 Expr *pLeft; /* The LHS of the IN operator */
113316 int i; /* loop counter */
113317 int destStep2; /* Where to jump when NULLs seen in step 2 */
113318 int destStep6 = 0; /* Start of code for Step 6 */
113319 int addrTruthOp; /* Address of opcode that determines the IN is true */
113320 int destNotNull; /* Jump here if a comparison is not true in step 6 */
113321 int addrTop; /* Top of the step-6 loop */
113322 int iTab = 0; /* Index to use */
113323 u8 okConstFactor = pParse->okConstFactor;
113324
113325 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
113326 pLeft = pExpr->pLeft;
113327 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
113328 zAff = exprINAffinity(pParse, pExpr);
113329 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
113330 aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int));
113331 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
113332
113333 /* Attempt to compute the RHS. After this step, if anything other than
113334 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
113335 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
113336 ** the RHS has not yet been coded. */
113337 v = pParse->pVdbe;
113338 assert( v!=0 )((void) (0)); /* OOM detected prior to this routine */
113339 VdbeNoopComment((v, "begin IN expr"));
113340 eType = sqlite3FindInIndex(pParse, pExpr,
113341 IN_INDEX_MEMBERSHIP0x0002 | IN_INDEX_NOOP_OK0x0001,
113342 destIfFalse==destIfNull ? 0 : &rRhsHasNull,
113343 aiMap, &iTab);
113344
113345 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH((void) (0))
113346 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC((void) (0))
113347 )((void) (0));
113348#ifdef SQLITE_DEBUG
113349 /* Confirm that aiMap[] contains nVector integer values between 0 and
113350 ** nVector-1. */
113351 for(i=0; i<nVector; i++){
113352 int j, cnt;
113353 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
113354 assert( cnt==1 )((void) (0));
113355 }
113356#endif
113357
113358 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
113359 ** vector, then it is stored in an array of nVector registers starting
113360 ** at r1.
113361 **
113362 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
113363 ** so that the fields are in the same order as an existing index. The
113364 ** aiMap[] array contains a mapping from the original LHS field order to
113365 ** the field order that matches the RHS index.
113366 **
113367 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
113368 ** even if it is constant, as OP_Affinity may be used on the register
113369 ** by code generated below. */
113370 assert( pParse->okConstFactor==okConstFactor )((void) (0));
113371 pParse->okConstFactor = 0;
113372 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
113373 pParse->okConstFactor = okConstFactor;
113374 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
113375 if( i==nVector ){
113376 /* LHS fields are not reordered */
113377 rLhs = rLhsOrig;
113378 }else{
113379 /* Need to reorder the LHS fields according to aiMap */
113380 rLhs = sqlite3GetTempRange(pParse, nVector);
113381 for(i=0; i<nVector; i++){
113382 sqlite3VdbeAddOp3(v, OP_Copy80, rLhsOrig+i, rLhs+aiMap[i], 0);
113383 }
113384 }
113385
113386 /* If sqlite3FindInIndex() did not find or create an index that is
113387 ** suitable for evaluating the IN operator, then evaluate using a
113388 ** sequence of comparisons.
113389 **
113390 ** This is step (1) in the in-operator.md optimized algorithm.
113391 */
113392 if( eType==IN_INDEX_NOOP5 ){
113393 ExprList *pList;
113394 CollSeq *pColl;
113395 int labelOk = sqlite3VdbeMakeLabel(pParse);
113396 int r2, regToFree;
113397 int regCkNull = 0;
113398 int ii;
113399 assert( ExprUseXList(pExpr) )((void) (0));
113400 pList = pExpr->x.pList;
113401 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
113402 if( destIfNull!=destIfFalse ){
113403 regCkNull = sqlite3GetTempReg(pParse);
113404 sqlite3VdbeAddOp3(v, OP_BitAnd103, rLhs, rLhs, regCkNull);
113405 }
113406 for(ii=0; ii<pList->nExpr; ii++){
113407 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
113408 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
113409 sqlite3VdbeAddOp3(v, OP_BitAnd103, regCkNull, r2, regCkNull);
113410 }
113411 sqlite3ReleaseTempReg(pParse, regToFree);
113412 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
113413 int op = rLhs!=r2 ? OP_Eq54 : OP_NotNull52;
113414 sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2,
113415 (void*)pColl, P4_COLLSEQ(-2));
113416 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
113417 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
113418 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
113419 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
113420 sqlite3VdbeChangeP5(v, zAff[0]);
113421 }else{
113422 int op = rLhs!=r2 ? OP_Ne53 : OP_IsNull51;
113423 assert( destIfNull==destIfFalse )((void) (0));
113424 sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2,
113425 (void*)pColl, P4_COLLSEQ(-2));
113426 VdbeCoverageIf(v, op==OP_Ne);
113427 VdbeCoverageIf(v, op==OP_IsNull);
113428 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL0x10);
113429 }
113430 }
113431 if( regCkNull ){
113432 sqlite3VdbeAddOp2(v, OP_IsNull51, regCkNull, destIfNull); VdbeCoverage(v);
113433 sqlite3VdbeGoto(v, destIfFalse);
113434 }
113435 sqlite3VdbeResolveLabel(v, labelOk);
113436 sqlite3ReleaseTempReg(pParse, regCkNull);
113437 goto sqlite3ExprCodeIN_finished;
113438 }
113439
113440 /* Step 2: Check to see if the LHS contains any NULL columns. If the
113441 ** LHS does contain NULLs then the result must be either FALSE or NULL.
113442 ** We will then skip the binary search of the RHS.
113443 */
113444 if( destIfNull==destIfFalse ){
113445 destStep2 = destIfFalse;
113446 }else{
113447 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
113448 }
113449 for(i=0; i<nVector; i++){
113450 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
113451 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
113452 if( sqlite3ExprCanBeNull(p) ){
113453 sqlite3VdbeAddOp2(v, OP_IsNull51, rLhs+i, destStep2);
113454 VdbeCoverage(v);
113455 }
113456 }
113457
113458 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
113459 ** of the RHS using the LHS as a probe. If found, the result is
113460 ** true.
113461 */
113462 if( eType==IN_INDEX_ROWID1 ){
113463 /* In this case, the RHS is the ROWID of table b-tree and so we also
113464 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
113465 ** into a single opcode. */
113466 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iTab, destIfFalse, rLhs);
113467 VdbeCoverage(v);
113468 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto9); /* Return True */
113469 }else{
113470 sqlite3VdbeAddOp4(v, OP_Affinity96, rLhs, nVector, 0, zAff, nVector);
113471 if( destIfFalse==destIfNull ){
113472 /* Combine Step 3 and Step 5 into a single opcode */
113473 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
113474 const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
113475 assert( pOp->opcode==OP_Once || pParse->nErr )((void) (0));
113476 if( pOp->opcode==OP_Once15 && pOp->p3>0 ){
113477 assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) )((void) (0));
113478 sqlite3VdbeAddOp4Int(v, OP_Filter64, pOp->p3, destIfFalse,
113479 rLhs, nVector); VdbeCoverage(v);
113480 }
113481 }
113482 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iTab, destIfFalse,
113483 rLhs, nVector); VdbeCoverage(v);
113484 goto sqlite3ExprCodeIN_finished;
113485 }
113486 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
113487 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found29, iTab, 0,
113488 rLhs, nVector); VdbeCoverage(v);
113489 }
113490
113491 /* Step 4. If the RHS is known to be non-NULL and we did not find
113492 ** an match on the search above, then the result must be FALSE.
113493 */
113494 if( rRhsHasNull && nVector==1 ){
113495 sqlite3VdbeAddOp2(v, OP_NotNull52, rRhsHasNull, destIfFalse);
113496 VdbeCoverage(v);
113497 }
113498
113499 /* Step 5. If we do not care about the difference between NULL and
113500 ** FALSE, then just return false.
113501 */
113502 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
113503
113504 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
113505 ** If any comparison is NULL, then the result is NULL. If all
113506 ** comparisons are FALSE then the final result is FALSE.
113507 **
113508 ** For a scalar LHS, it is sufficient to check just the first row
113509 ** of the RHS.
113510 */
113511 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
113512 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind36, iTab, destIfFalse);
113513 VdbeCoverage(v);
113514 if( nVector>1 ){
113515 destNotNull = sqlite3VdbeMakeLabel(pParse);
113516 }else{
113517 /* For nVector==1, combine steps 6 and 7 by immediately returning
113518 ** FALSE if the first comparison is not NULL */
113519 destNotNull = destIfFalse;
113520 }
113521 for(i=0; i<nVector; i++){
113522 Expr *p;
113523 CollSeq *pColl;
113524 int r3 = sqlite3GetTempReg(pParse);
113525 p = sqlite3VectorFieldSubexpr(pLeft, i);
113526 pColl = sqlite3ExprCollSeq(pParse, p);
113527 sqlite3VdbeAddOp3(v, OP_Column94, iTab, i, r3);
113528 sqlite3VdbeAddOp4(v, OP_Ne53, rLhs+i, destNotNull, r3,
113529 (void*)pColl, P4_COLLSEQ(-2));
113530 VdbeCoverage(v);
113531 sqlite3ReleaseTempReg(pParse, r3);
113532 }
113533 sqlite3VdbeAddOp2(v, OP_Goto9, 0, destIfNull);
113534 if( nVector>1 ){
113535 sqlite3VdbeResolveLabel(v, destNotNull);
113536 sqlite3VdbeAddOp2(v, OP_Next39, iTab, addrTop+1);
113537 VdbeCoverage(v);
113538
113539 /* Step 7: If we reach this point, we know that the result must
113540 ** be false. */
113541 sqlite3VdbeAddOp2(v, OP_Goto9, 0, destIfFalse);
113542 }
113543
113544 /* Jumps here in order to return true. */
113545 sqlite3VdbeJumpHere(v, addrTruthOp);
113546
113547sqlite3ExprCodeIN_finished:
113548 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
113549 VdbeComment((v, "end IN expr"));
113550sqlite3ExprCodeIN_oom_error:
113551 sqlite3DbFree(pParse->db, aiMap);
113552 sqlite3DbFree(pParse->db, zAff);
113553}
113554#endif /* SQLITE_OMIT_SUBQUERY */
113555
113556#ifndef SQLITE_OMIT_FLOATING_POINT
113557/*
113558** Generate an instruction that will put the floating point
113559** value described by z[0..n-1] into register iMem.
113560**
113561** The z[] string will probably not be zero-terminated. But the
113562** z[n] character is guaranteed to be something that does not look
113563** like the continuation of the number.
113564*/
113565static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
113566 if( ALWAYS(z!=0)(z!=0) ){
113567 double value;
113568 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF81);
113569 assert( !sqlite3IsNaN(value) )((void) (0)); /* The new AtoF never returns NaN */
113570 if( negateFlag ) value = -value;
113571 sqlite3VdbeAddOp4Dup8(v, OP_Real154, 0, iMem, 0, (u8*)&value, P4_REAL(-12));
113572 }
113573}
113574#endif
113575
113576
113577/*
113578** Generate an instruction that will put the integer describe by
113579** text z[0..n-1] into register iMem.
113580**
113581** Expr.u.zToken is always UTF8 and zero-terminated.
113582*/
113583static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
113584 Vdbe *v = pParse->pVdbe;
113585 if( pExpr->flags & EP_IntValue0x000800 ){
113586 int i = pExpr->u.iValue;
113587 assert( i>=0 )((void) (0));
113588 if( negFlag ) i = -i;
113589 sqlite3VdbeAddOp2(v, OP_Integer71, i, iMem);
113590 }else{
113591 int c;
113592 i64 value;
113593 const char *z = pExpr->u.zToken;
113594 assert( z!=0 )((void) (0));
113595 c = sqlite3DecOrHexToI64(z, &value);
113596 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))))){
113597#ifdef SQLITE_OMIT_FLOATING_POINT
113598 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
113599#else
113600#ifndef SQLITE_OMIT_HEX_INTEGER
113601 if( sqlite3_strnicmp(z,"0x",2)==0 ){
113602 sqlite3ErrorMsg(pParse, "hex literal too big: %s%#T",
113603 negFlag?"-":"",pExpr);
113604 }else
113605#endif
113606 {
113607 codeReal(v, z, negFlag, iMem);
113608 }
113609#endif
113610 }else{
113611 if( negFlag ){ value = c==3 ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : -value; }
113612 sqlite3VdbeAddOp4Dup8(v, OP_Int6472, 0, iMem, 0, (u8*)&value, P4_INT64(-13));
113613 }
113614 }
113615}
113616
113617
113618/* Generate code that will load into register regOut a value that is
113619** appropriate for the iIdxCol-th column of index pIdx.
113620*/
113621SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(
113622 Parse *pParse, /* The parsing context */
113623 Index *pIdx, /* The index whose column is to be loaded */
113624 int iTabCur, /* Cursor pointing to a table row */
113625 int iIdxCol, /* The column of the index to be loaded */
113626 int regOut /* Store the index column value in this register */
113627){
113628 i16 iTabCol = pIdx->aiColumn[iIdxCol];
113629 if( iTabCol==XN_EXPR(-2) ){
113630 assert( pIdx->aColExpr )((void) (0));
113631 assert( pIdx->aColExpr->nExpr>iIdxCol )((void) (0));
113632 pParse->iSelfTab = iTabCur + 1;
113633 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
113634 pParse->iSelfTab = 0;
113635 }else{
113636 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
113637 iTabCol, regOut);
113638 }
113639}
113640
113641#ifndef SQLITE_OMIT_GENERATED_COLUMNS
113642/*
113643** Generate code that will compute the value of generated column pCol
113644** and store the result in register regOut
113645*/
113646SQLITE_PRIVATEstatic void sqlite3ExprCodeGeneratedColumn(
113647 Parse *pParse, /* Parsing context */
113648 Table *pTab, /* Table containing the generated column */
113649 Column *pCol, /* The generated column */
113650 int regOut /* Put the result in this register */
113651){
113652 int iAddr;
113653 Vdbe *v = pParse->pVdbe;
113654 int nErr = pParse->nErr;
113655 assert( v!=0 )((void) (0));
113656 assert( pParse->iSelfTab!=0 )((void) (0));
113657 if( pParse->iSelfTab>0 ){
113658 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow20, pParse->iSelfTab-1, 0, regOut);
113659 }else{
113660 iAddr = 0;
113661 }
113662 sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut);
113663 if( pCol->affinity>=SQLITE_AFF_TEXT0x42 ){
113664 sqlite3VdbeAddOp4(v, OP_Affinity96, regOut, 1, 0, &pCol->affinity, 1);
113665 }
113666 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
113667 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
113668}
113669#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
113670
113671/*
113672** Generate code to extract the value of the iCol-th column of a table.
113673*/
113674SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(
113675 Vdbe *v, /* Parsing context */
113676 Table *pTab, /* The table containing the value */
113677 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
113678 int iCol, /* Index of the column to extract */
113679 int regOut /* Extract the value into this register */
113680){
113681 Column *pCol;
113682 assert( v!=0 )((void) (0));
113683 assert( pTab!=0 )((void) (0));
113684 assert( iCol!=XN_EXPR )((void) (0));
113685 if( iCol<0 || iCol==pTab->iPKey ){
113686 sqlite3VdbeAddOp2(v, OP_Rowid135, iTabCur, regOut);
113687 VdbeComment((v, "%s.rowid", pTab->zName));
113688 }else{
113689 int op;
113690 int x;
113691 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
113692 op = OP_VColumn176;
113693 x = iCol;
113694#ifndef SQLITE_OMIT_GENERATED_COLUMNS
113695 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL0x0020 ){
113696 Parse *pParse = sqlite3VdbeParser(v);
113697 if( pCol->colFlags & COLFLAG_BUSY0x0100 ){
113698 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
113699 pCol->zCnName);
113700 }else{
113701 int savedSelfTab = pParse->iSelfTab;
113702 pCol->colFlags |= COLFLAG_BUSY0x0100;
113703 pParse->iSelfTab = iTabCur+1;
113704 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut);
113705 pParse->iSelfTab = savedSelfTab;
113706 pCol->colFlags &= ~COLFLAG_BUSY0x0100;
113707 }
113708 return;
113709#endif
113710 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
113711 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
113712 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
113713 op = OP_Column94;
113714 }else{
113715 x = sqlite3TableColumnToStorage(pTab,iCol);
113716 testcase( x!=iCol );
113717 op = OP_Column94;
113718 }
113719 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
113720 sqlite3ColumnDefault(v, pTab, iCol, regOut);
113721 }
113722}
113723
113724/*
113725** Generate code that will extract the iColumn-th column from
113726** table pTab and store the column value in register iReg.
113727**
113728** There must be an open cursor to pTab in iTable when this routine
113729** is called. If iColumn<0 then code is generated that extracts the rowid.
113730*/
113731SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(
113732 Parse *pParse, /* Parsing and code generating context */
113733 Table *pTab, /* Description of the table we are reading from */
113734 int iColumn, /* Index of the table column */
113735 int iTable, /* The cursor pointing to the table */
113736 int iReg, /* Store results here */
113737 u8 p5 /* P5 value for OP_Column + FLAGS */
113738){
113739 assert( pParse->pVdbe!=0 )((void) (0));
113740 assert( (p5 & (OPFLAG_NOCHNG|OPFLAG_TYPEOFARG|OPFLAG_LENGTHARG))==p5 )((void) (0));
113741 assert( IsVirtual(pTab) || (p5 & OPFLAG_NOCHNG)==0 )((void) (0));
113742 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
113743 if( p5 ){
113744 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
113745 if( pOp->opcode==OP_Column94 ) pOp->p5 = p5;
113746 if( pOp->opcode==OP_VColumn176 ) pOp->p5 = (p5 & OPFLAG_NOCHNG0x01);
113747 }
113748 return iReg;
113749}
113750
113751/*
113752** Generate code to move content from registers iFrom...iFrom+nReg-1
113753** over to iTo..iTo+nReg-1.
113754*/
113755SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
113756 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move79, iFrom, iTo, nReg);
113757}
113758
113759/*
113760** Convert a scalar expression node to a TK_REGISTER referencing
113761** register iReg. The caller must ensure that iReg already contains
113762** the correct value for the expression.
113763*/
113764SQLITE_PRIVATEstatic void sqlite3ExprToRegister(Expr *pExpr, int iReg){
113765 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
113766 if( NEVER(p==0)(p==0) ) return;
113767 if( p->op==TK_REGISTER176 ){
113768 assert( p->iTable==iReg )((void) (0));
113769 }else{
113770 p->op2 = p->op;
113771 p->op = TK_REGISTER176;
113772 p->iTable = iReg;
113773 ExprClearProperty(p, EP_Skip)(p)->flags&=~(0x002000);
113774 }
113775}
113776
113777/*
113778** Evaluate an expression (either a vector or a scalar expression) and store
113779** the result in contiguous temporary registers. Return the index of
113780** the first register used to store the result.
113781**
113782** If the returned result register is a temporary scalar, then also write
113783** that register number into *piFreeable. If the returned result register
113784** is not a temporary or if the expression is a vector set *piFreeable
113785** to 0.
113786*/
113787static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
113788 int iResult;
113789 int nResult = sqlite3ExprVectorSize(p);
113790 if( nResult==1 ){
113791 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
113792 }else{
113793 *piFreeable = 0;
113794 if( p->op==TK_SELECT139 ){
113795#if SQLITE_OMIT_SUBQUERY
113796 iResult = 0;
113797#else
113798 iResult = sqlite3CodeSubselect(pParse, p);
113799#endif
113800 }else{
113801 int i;
113802 iResult = pParse->nMem+1;
113803 pParse->nMem += nResult;
113804 assert( ExprUseXList(p) )((void) (0));
113805 for(i=0; i<nResult; i++){
113806 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
113807 }
113808 }
113809 }
113810 return iResult;
113811}
113812
113813/*
113814** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
113815** so that a subsequent copy will not be merged into this one.
113816*/
113817static void setDoNotMergeFlagOnCopy(Vdbe *v){
113818 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy80 ){
113819 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergeable */
113820 }
113821}
113822
113823/*
113824** Generate code to implement special SQL functions that are implemented
113825** in-line rather than by using the usual callbacks.
113826*/
113827static int exprCodeInlineFunction(
113828 Parse *pParse, /* Parsing context */
113829 ExprList *pFarg, /* List of function arguments */
113830 int iFuncId, /* Function ID. One of the INTFUNC_... values */
113831 int target /* Store function result in this register */
113832){
113833 int nFarg;
113834 Vdbe *v = pParse->pVdbe;
113835 assert( v!=0 )((void) (0));
113836 assert( pFarg!=0 )((void) (0));
113837 nFarg = pFarg->nExpr;
113838 assert( nFarg>0 )((void) (0)); /* All in-line functions have at least one argument */
113839 switch( iFuncId ){
113840 case INLINEFUNC_coalesce0: {
113841 /* Attempt a direct implementation of the built-in COALESCE() and
113842 ** IFNULL() functions. This avoids unnecessary evaluation of
113843 ** arguments past the first non-NULL argument.
113844 */
113845 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
113846 int i;
113847 assert( nFarg>=2 )((void) (0));
113848 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
113849 for(i=1; i<nFarg; i++){
113850 sqlite3VdbeAddOp2(v, OP_NotNull52, target, endCoalesce);
113851 VdbeCoverage(v);
113852 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
113853 }
113854 setDoNotMergeFlagOnCopy(v);
113855 sqlite3VdbeResolveLabel(v, endCoalesce);
113856 break;
113857 }
113858 case INLINEFUNC_iif5: {
113859 Expr caseExpr;
113860 memset(&caseExpr, 0, sizeof(caseExpr));
113861 caseExpr.op = TK_CASE158;
113862 caseExpr.x.pList = pFarg;
113863 return sqlite3ExprCodeTarget(pParse, &caseExpr, target);
113864 }
113865#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
113866 case INLINEFUNC_sqlite_offset6: {
113867 Expr *pArg = pFarg->a[0].pExpr;
113868 if( pArg->op==TK_COLUMN168 && pArg->iTable>=0 ){
113869 sqlite3VdbeAddOp3(v, OP_Offset93, pArg->iTable, pArg->iColumn, target);
113870 }else{
113871 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
113872 }
113873 break;
113874 }
113875#endif
113876 default: {
113877 /* The UNLIKELY() function is a no-op. The result is the value
113878 ** of the first argument.
113879 */
113880 assert( nFarg==1 || nFarg==2 )((void) (0));
113881 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
113882 break;
113883 }
113884
113885 /***********************************************************************
113886 ** Test-only SQL functions that are only usable if enabled
113887 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
113888 */
113889#if !defined(SQLITE_UNTESTABLE)
113890 case INLINEFUNC_expr_compare3: {
113891 /* Compare two expressions using sqlite3ExprCompare() */
113892 assert( nFarg==2 )((void) (0));
113893 sqlite3VdbeAddOp2(v, OP_Integer71,
113894 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
113895 target);
113896 break;
113897 }
113898
113899 case INLINEFUNC_expr_implies_expr2: {
113900 /* Compare two expressions using sqlite3ExprImpliesExpr() */
113901 assert( nFarg==2 )((void) (0));
113902 sqlite3VdbeAddOp2(v, OP_Integer71,
113903 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
113904 target);
113905 break;
113906 }
113907
113908 case INLINEFUNC_implies_nonnull_row1: {
113909 /* Result of sqlite3ExprImpliesNonNullRow() */
113910 Expr *pA1;
113911 assert( nFarg==2 )((void) (0));
113912 pA1 = pFarg->a[1].pExpr;
113913 if( pA1->op==TK_COLUMN168 ){
113914 sqlite3VdbeAddOp2(v, OP_Integer71,
113915 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
113916 target);
113917 }else{
113918 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
113919 }
113920 break;
113921 }
113922
113923 case INLINEFUNC_affinity4: {
113924 /* The AFFINITY() function evaluates to a string that describes
113925 ** the type affinity of the argument. This is used for testing of
113926 ** the SQLite type logic.
113927 */
113928 const char *azAff[] = { "blob", "text", "numeric", "integer",
113929 "real", "flexnum" };
113930 char aff;
113931 assert( nFarg==1 )((void) (0));
113932 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
113933 assert( aff<=SQLITE_AFF_NONE((void) (0))
113934 || (aff>=SQLITE_AFF_BLOB && aff<=SQLITE_AFF_FLEXNUM) )((void) (0));
113935 sqlite3VdbeLoadString(v, target,
113936 (aff<=SQLITE_AFF_NONE0x40) ? "none" : azAff[aff-SQLITE_AFF_BLOB0x41]);
113937 break;
113938 }
113939#endif /* !defined(SQLITE_UNTESTABLE) */
113940 }
113941 return target;
113942}
113943
113944/*
113945** Expression Node callback for sqlite3ExprCanReturnSubtype().
113946**
113947** Only a function call is able to return a subtype. So if the node
113948** is not a function call, return WRC_Prune immediately.
113949**
113950** A function call is able to return a subtype if it has the
113951** SQLITE_RESULT_SUBTYPE property.
113952**
113953** Assume that every function is able to pass-through a subtype from
113954** one of its argument (using sqlite3_result_value()). Most functions
113955** are not this way, but we don't have a mechanism to distinguish those
113956** that are from those that are not, so assume they all work this way.
113957** That means that if one of its arguments is another function and that
113958** other function is able to return a subtype, then this function is
113959** able to return a subtype.
113960*/
113961static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){
113962 int n;
113963 FuncDef *pDef;
113964 sqlite3 *db;
113965 if( pExpr->op!=TK_FUNCTION172 ){
113966 return WRC_Prune1;
113967 }
113968 assert( ExprUseXList(pExpr) )((void) (0));
113969 db = pWalker->pParse->db;
113970 n = ALWAYS(pExpr->x.pList)(pExpr->x.pList) ? pExpr->x.pList->nExpr : 0;
113971 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db)((db)->enc), 0);
113972 if( NEVER(pDef==0)(pDef==0) || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE0x001000000)!=0 ){
113973 pWalker->eCode = 1;
113974 return WRC_Prune1;
113975 }
113976 return WRC_Continue0;
113977}
113978
113979/*
113980** Return TRUE if expression pExpr is able to return a subtype.
113981**
113982** A TRUE return does not guarantee that a subtype will be returned.
113983** It only indicates that a subtype return is possible. False positives
113984** are acceptable as they only disable an optimization. False negatives,
113985** on the other hand, can lead to incorrect answers.
113986*/
113987static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){
113988 Walker w;
113989 memset(&w, 0, sizeof(w));
113990 w.pParse = pParse;
113991 w.xExprCallback = exprNodeCanReturnSubtype;
113992 sqlite3WalkExpr(&w, pExpr);
113993 return w.eCode;
113994}
113995
113996
113997/*
113998** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
113999** If it is, then resolve the expression by reading from the index and
114000** return the register into which the value has been read. If pExpr is
114001** not an indexed expression, then return negative.
114002*/
114003static SQLITE_NOINLINE__attribute__((noinline)) int sqlite3IndexedExprLookup(
114004 Parse *pParse, /* The parsing context */
114005 Expr *pExpr, /* The expression to potentially bypass */
114006 int target /* Where to store the result of the expression */
114007){
114008 IndexedExpr *p;
114009 Vdbe *v;
114010 for(p=pParse->pIdxEpr; p; p=p->pIENext){
114011 u8 exprAff;
114012 int iDataCur = p->iDataCur;
114013 if( iDataCur<0 ) continue;
114014 if( pParse->iSelfTab ){
114015 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
114016 iDataCur = -1;
114017 }
114018 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
114019 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC )((void) (0));
114020 exprAff = sqlite3ExprAffinity(pExpr);
114021 if( (exprAff<=SQLITE_AFF_BLOB0x41 && p->aff!=SQLITE_AFF_BLOB0x41)
114022 || (exprAff==SQLITE_AFF_TEXT0x42 && p->aff!=SQLITE_AFF_TEXT0x42)
114023 || (exprAff>=SQLITE_AFF_NUMERIC0x43 && p->aff!=SQLITE_AFF_NUMERIC0x43)
114024 ){
114025 /* Affinity mismatch on a generated column */
114026 continue;
114027 }
114028
114029
114030 /* Functions that might set a subtype should not be replaced by the
114031 ** value taken from an expression index if they are themselves an
114032 ** argument to another scalar function or aggregate.
114033 ** https://sqlite.org/forum/forumpost/68d284c86b082c3e */
114034 if( ExprHasProperty(pExpr, EP_SubtArg)(((pExpr)->flags&(0x80000000))!=0)
114035 && sqlite3ExprCanReturnSubtype(pParse, pExpr)
114036 ){
114037 continue;
114038 }
114039
114040 v = pParse->pVdbe;
114041 assert( v!=0 )((void) (0));
114042 if( p->bMaybeNullRow ){
114043 /* If the index is on a NULL row due to an outer join, then we
114044 ** cannot extract the value from the index. The value must be
114045 ** computed using the original expression. */
114046 int addr = sqlite3VdbeCurrentAddr(v);
114047 sqlite3VdbeAddOp3(v, OP_IfNullRow20, p->iIdxCur, addr+3, target);
114048 VdbeCoverage(v);
114049 sqlite3VdbeAddOp3(v, OP_Column94, p->iIdxCur, p->iIdxCol, target);
114050 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
114051 sqlite3VdbeGoto(v, 0);
114052 p = pParse->pIdxEpr;
114053 pParse->pIdxEpr = 0;
114054 sqlite3ExprCode(pParse, pExpr, target);
114055 pParse->pIdxEpr = p;
114056 sqlite3VdbeJumpHere(v, addr+2);
114057 }else{
114058 sqlite3VdbeAddOp3(v, OP_Column94, p->iIdxCur, p->iIdxCol, target);
114059 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
114060 }
114061 return target;
114062 }
114063 return -1; /* Not found */
114064}
114065
114066
114067/*
114068** Expresion pExpr is guaranteed to be a TK_COLUMN or equivalent. This
114069** function checks the Parse.pIdxPartExpr list to see if this column
114070** can be replaced with a constant value. If so, it generates code to
114071** put the constant value in a register (ideally, but not necessarily,
114072** register iTarget) and returns the register number.
114073**
114074** Or, if the TK_COLUMN cannot be replaced by a constant, zero is
114075** returned.
114076*/
114077static int exprPartidxExprLookup(Parse *pParse, Expr *pExpr, int iTarget){
114078 IndexedExpr *p;
114079 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){
114080 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){
114081 Vdbe *v = pParse->pVdbe;
114082 int addr = 0;
114083 int ret;
114084
114085 if( p->bMaybeNullRow ){
114086 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow20, p->iIdxCur);
114087 }
114088 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget);
114089 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity96, ret, 1, 0,
114090 (const char*)&p->aff, 1);
114091 if( addr ){
114092 sqlite3VdbeJumpHere(v, addr);
114093 sqlite3VdbeChangeP3(v, addr, ret);
114094 }
114095 return ret;
114096 }
114097 }
114098 return 0;
114099}
114100
114101
114102/*
114103** Generate code into the current Vdbe to evaluate the given
114104** expression. Attempt to store the results in register "target".
114105** Return the register where results are stored.
114106**
114107** With this routine, there is no guarantee that results will
114108** be stored in target. The result might be stored in some other
114109** register if it is convenient to do so. The calling function
114110** must check the return code and move the results to the desired
114111** register.
114112*/
114113SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
114114 Vdbe *v = pParse->pVdbe; /* The VM under construction */
114115 int op; /* The opcode being coded */
114116 int inReg = target; /* Results stored in register inReg */
114117 int regFree1 = 0; /* If non-zero free this temporary register */
114118 int regFree2 = 0; /* If non-zero free this temporary register */
114119 int r1, r2; /* Various register numbers */
114120 Expr tempX; /* Temporary expression node */
114121 int p5 = 0;
114122
114123 assert( target>0 && target<=pParse->nMem )((void) (0));
114124 assert( v!=0 )((void) (0));
114125
114126expr_code_doover:
114127 if( pExpr==0 ){
114128 op = TK_NULL122;
114129 }else if( pParse->pIdxEpr!=0
114130 && !ExprHasProperty(pExpr, EP_Leaf)(((pExpr)->flags&(0x800000))!=0)
114131 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
114132 ){
114133 return r1;
114134 }else{
114135 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
114136 op = pExpr->op;
114137 }
114138 assert( op!=TK_ORDER )((void) (0));
114139 switch( op ){
114140 case TK_AGG_COLUMN170: {
114141 AggInfo *pAggInfo = pExpr->pAggInfo;
114142 struct AggInfo_col *pCol;
114143 assert( pAggInfo!=0 )((void) (0));
114144 assert( pExpr->iAgg>=0 )((void) (0));
114145 if( pExpr->iAgg>=pAggInfo->nColumn ){
114146 /* Happens when the left table of a RIGHT JOIN is null and
114147 ** is using an expression index */
114148 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
114149#ifdef SQLITE_VDBE_COVERAGE
114150 /* Verify that the OP_Null above is exercised by tests
114151 ** tag-20230325-2 */
114152 sqlite3VdbeAddOp3(v, OP_NotNull52, target, 1, 20230325);
114153 VdbeCoverageNeverTaken(v);
114154#endif
114155 break;
114156 }
114157 pCol = &pAggInfo->aCol[pExpr->iAgg];
114158 if( !pAggInfo->directMode ){
114159 return AggInfoColumnReg(pAggInfo, pExpr->iAgg)((pAggInfo)->iFirstReg+(pExpr->iAgg));
114160 }else if( pAggInfo->useSortingIdx ){
114161 Table *pTab = pCol->pTab;
114162 sqlite3VdbeAddOp3(v, OP_Column94, pAggInfo->sortingIdxPTab,
114163 pCol->iSorterColumn, target);
114164 if( pTab==0 ){
114165 /* No comment added */
114166 }else if( pCol->iColumn<0 ){
114167 VdbeComment((v,"%s.rowid",pTab->zName));
114168 }else{
114169 VdbeComment((v,"%s.%s",
114170 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
114171 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL0x45 ){
114172 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
114173 }
114174 }
114175 return target;
114176 }else if( pExpr->y.pTab==0 ){
114177 /* This case happens when the argument to an aggregate function
114178 ** is rewritten by aggregateConvertIndexedExprRefToColumn() */
114179 sqlite3VdbeAddOp3(v, OP_Column94, pExpr->iTable, pExpr->iColumn, target);
114180 return target;
114181 }
114182 /* Otherwise, fall thru into the TK_COLUMN case */
114183 /* no break */ deliberate_fall_through__attribute__((fallthrough));
114184 }
114185 case TK_COLUMN168: {
114186 int iTab = pExpr->iTable;
114187 int iReg;
114188 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0) ){
114189 /* This COLUMN expression is really a constant due to WHERE clause
114190 ** constraints, and that constant is coded by the pExpr->pLeft
114191 ** expression. However, make sure the constant has the correct
114192 ** datatype by applying the Affinity of the table column to the
114193 ** constant.
114194 */
114195 int aff;
114196 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
114197 assert( ExprUseYTab(pExpr) )((void) (0));
114198 assert( pExpr->y.pTab!=0 )((void) (0));
114199 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
114200 if( aff>SQLITE_AFF_BLOB0x41 ){
114201 static const char zAff[] = "B\000C\000D\000E\000F";
114202 assert( SQLITE_AFF_BLOB=='A' )((void) (0));
114203 assert( SQLITE_AFF_TEXT=='B' )((void) (0));
114204 sqlite3VdbeAddOp4(v, OP_Affinity96, iReg, 1, 0,
114205 &zAff[(aff-'B')*2], P4_STATIC(-1));
114206 }
114207 return iReg;
114208 }
114209 if( iTab<0 ){
114210 if( pParse->iSelfTab<0 ){
114211 /* Other columns in the same row for CHECK constraints or
114212 ** generated columns or for inserting into partial index.
114213 ** The row is unpacked into registers beginning at
114214 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
114215 ** immediately prior to the first column.
114216 */
114217 Column *pCol;
114218 Table *pTab;
114219 int iSrc;
114220 int iCol = pExpr->iColumn;
114221 assert( ExprUseYTab(pExpr) )((void) (0));
114222 pTab = pExpr->y.pTab;
114223 assert( pTab!=0 )((void) (0));
114224 assert( iCol>=XN_ROWID )((void) (0));
114225 assert( iCol<pTab->nCol )((void) (0));
114226 if( iCol<0 ){
114227 return -1-pParse->iSelfTab;
114228 }
114229 pCol = pTab->aCol + iCol;
114230 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
114231 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
114232#ifndef SQLITE_OMIT_GENERATED_COLUMNS
114233 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
114234 if( pCol->colFlags & COLFLAG_BUSY0x0100 ){
114235 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
114236 pCol->zCnName);
114237 return 0;
114238 }
114239 pCol->colFlags |= COLFLAG_BUSY0x0100;
114240 if( pCol->colFlags & COLFLAG_NOTAVAIL0x0080 ){
114241 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, iSrc);
114242 }
114243 pCol->colFlags &= ~(COLFLAG_BUSY0x0100|COLFLAG_NOTAVAIL0x0080);
114244 return iSrc;
114245 }else
114246#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
114247 if( pCol->affinity==SQLITE_AFF_REAL0x45 ){
114248 sqlite3VdbeAddOp2(v, OP_SCopy81, iSrc, target);
114249 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
114250 return target;
114251 }else{
114252 return iSrc;
114253 }
114254 }else{
114255 /* Coding an expression that is part of an index where column names
114256 ** in the index refer to the table to which the index belongs */
114257 iTab = pParse->iSelfTab - 1;
114258 }
114259 }
114260 else if( pParse->pIdxPartExpr
114261 && 0!=(r1 = exprPartidxExprLookup(pParse, pExpr, target))
114262 ){
114263 return r1;
114264 }
114265 assert( ExprUseYTab(pExpr) )((void) (0));
114266 assert( pExpr->y.pTab!=0 )((void) (0));
114267 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
114268 pExpr->iColumn, iTab, target,
114269 pExpr->op2);
114270 return iReg;
114271 }
114272 case TK_INTEGER156: {
114273 codeInteger(pParse, pExpr, 0, target);
114274 return target;
114275 }
114276 case TK_TRUEFALSE171: {
114277 sqlite3VdbeAddOp2(v, OP_Integer71, sqlite3ExprTruthValue(pExpr), target);
114278 return target;
114279 }
114280#ifndef SQLITE_OMIT_FLOATING_POINT
114281 case TK_FLOAT154: {
114282 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114283 codeReal(v, pExpr->u.zToken, 0, target);
114284 return target;
114285 }
114286#endif
114287 case TK_STRING118: {
114288 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114289 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
114290 return target;
114291 }
114292 default: {
114293 /* Make NULL the default case so that if a bug causes an illegal
114294 ** Expr node to be passed into this function, it will be handled
114295 ** sanely and not crash. But keep the assert() to bring the problem
114296 ** to the attention of the developers. */
114297 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed )((void) (0));
114298 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
114299 return target;
114300 }
114301#ifndef SQLITE_OMIT_BLOB_LITERAL
114302 case TK_BLOB155: {
114303 int n;
114304 const char *z;
114305 char *zBlob;
114306 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114307 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
114308 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
114309 z = &pExpr->u.zToken[2];
114310 n = sqlite3Strlen30(z) - 1;
114311 assert( z[n]=='\'' )((void) (0));
114312 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
114313 sqlite3VdbeAddOp4(v, OP_Blob77, n/2, target, 0, zBlob, P4_DYNAMIC(-6));
114314 return target;
114315 }
114316#endif
114317 case TK_VARIABLE157: {
114318 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114319 assert( pExpr->u.zToken!=0 )((void) (0));
114320 assert( pExpr->u.zToken[0]!=0 )((void) (0));
114321 sqlite3VdbeAddOp2(v, OP_Variable78, pExpr->iColumn, target);
114322 return target;
114323 }
114324 case TK_REGISTER176: {
114325 return pExpr->iTable;
114326 }
114327#ifndef SQLITE_OMIT_CAST
114328 case TK_CAST36: {
114329 /* Expressions of the form: CAST(pLeft AS token) */
114330 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114331 assert( inReg==target )((void) (0));
114332 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114333 sqlite3VdbeAddOp2(v, OP_Cast88, target,
114334 sqlite3AffinityType(pExpr->u.zToken, 0));
114335 return inReg;
114336 }
114337#endif /* SQLITE_OMIT_CAST */
114338 case TK_IS45:
114339 case TK_ISNOT46:
114340 op = (op==TK_IS45) ? TK_EQ54 : TK_NE53;
114341 p5 = SQLITE_NULLEQ0x80;
114342 /* fall-through */
114343 case TK_LT57:
114344 case TK_LE56:
114345 case TK_GT55:
114346 case TK_GE58:
114347 case TK_NE53:
114348 case TK_EQ54: {
114349 Expr *pLeft = pExpr->pLeft;
114350 if( sqlite3ExprIsVector(pLeft) ){
114351 codeVectorCompare(pParse, pExpr, target, op, p5);
114352 }else{
114353 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
114354 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
114355 sqlite3VdbeAddOp2(v, OP_Integer71, 1, inReg);
114356 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
114357 sqlite3VdbeCurrentAddr(v)+2, p5,
114358 ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0));
114359 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
114360 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
114361 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
114362 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
114363 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
114364 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
114365 if( p5==SQLITE_NULLEQ0x80 ){
114366 sqlite3VdbeAddOp2(v, OP_Integer71, 0, inReg);
114367 }else{
114368 sqlite3VdbeAddOp3(v, OP_ZeroOrNull92, r1, inReg, r2);
114369 }
114370 testcase( regFree1==0 );
114371 testcase( regFree2==0 );
114372 }
114373 break;
114374 }
114375 case TK_AND44:
114376 case TK_OR43:
114377 case TK_PLUS107:
114378 case TK_STAR109:
114379 case TK_MINUS108:
114380 case TK_REM111:
114381 case TK_BITAND103:
114382 case TK_BITOR104:
114383 case TK_SLASH110:
114384 case TK_LSHIFT105:
114385 case TK_RSHIFT106:
114386 case TK_CONCAT112: {
114387 assert( TK_AND==OP_And )((void) (0)); testcase( op==TK_AND );
114388 assert( TK_OR==OP_Or )((void) (0)); testcase( op==TK_OR );
114389 assert( TK_PLUS==OP_Add )((void) (0)); testcase( op==TK_PLUS );
114390 assert( TK_MINUS==OP_Subtract )((void) (0)); testcase( op==TK_MINUS );
114391 assert( TK_REM==OP_Remainder )((void) (0)); testcase( op==TK_REM );
114392 assert( TK_BITAND==OP_BitAnd )((void) (0)); testcase( op==TK_BITAND );
114393 assert( TK_BITOR==OP_BitOr )((void) (0)); testcase( op==TK_BITOR );
114394 assert( TK_SLASH==OP_Divide )((void) (0)); testcase( op==TK_SLASH );
114395 assert( TK_LSHIFT==OP_ShiftLeft )((void) (0)); testcase( op==TK_LSHIFT );
114396 assert( TK_RSHIFT==OP_ShiftRight )((void) (0)); testcase( op==TK_RSHIFT );
114397 assert( TK_CONCAT==OP_Concat )((void) (0)); testcase( op==TK_CONCAT );
114398 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114399 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
114400 sqlite3VdbeAddOp3(v, op, r2, r1, target);
114401 testcase( regFree1==0 );
114402 testcase( regFree2==0 );
114403 break;
114404 }
114405 case TK_UMINUS174: {
114406 Expr *pLeft = pExpr->pLeft;
114407 assert( pLeft )((void) (0));
114408 if( pLeft->op==TK_INTEGER156 ){
114409 codeInteger(pParse, pLeft, 1, target);
114410 return target;
114411#ifndef SQLITE_OMIT_FLOATING_POINT
114412 }else if( pLeft->op==TK_FLOAT154 ){
114413 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114414 codeReal(v, pLeft->u.zToken, 1, target);
114415 return target;
114416#endif
114417 }else{
114418 tempX.op = TK_INTEGER156;
114419 tempX.flags = EP_IntValue0x000800|EP_TokenOnly0x010000;
114420 tempX.u.iValue = 0;
114421 ExprClearVVAProperties(&tempX);
114422 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
114423 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
114424 sqlite3VdbeAddOp3(v, OP_Subtract108, r2, r1, target);
114425 testcase( regFree2==0 );
114426 }
114427 break;
114428 }
114429 case TK_BITNOT115:
114430 case TK_NOT19: {
114431 assert( TK_BITNOT==OP_BitNot )((void) (0)); testcase( op==TK_BITNOT );
114432 assert( TK_NOT==OP_Not )((void) (0)); testcase( op==TK_NOT );
114433 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114434 testcase( regFree1==0 );
114435 sqlite3VdbeAddOp2(v, op, r1, inReg);
114436 break;
114437 }
114438 case TK_TRUTH175: {
114439 int isTrue; /* IS TRUE or IS NOT TRUE */
114440 int bNormal; /* IS TRUE or IS FALSE */
114441 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114442 testcase( regFree1==0 );
114443 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
114444 bNormal = pExpr->op2==TK_IS45;
114445 testcase( isTrue && bNormal);
114446 testcase( !isTrue && bNormal);
114447 sqlite3VdbeAddOp4Int(v, OP_IsTrue91, r1, inReg, !isTrue, isTrue ^ bNormal);
114448 break;
114449 }
114450 case TK_ISNULL51:
114451 case TK_NOTNULL52: {
114452 int addr;
114453 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
114454 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
114455 sqlite3VdbeAddOp2(v, OP_Integer71, 1, target);
114456 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114457 testcase( regFree1==0 );
114458 addr = sqlite3VdbeAddOp1(v, op, r1);
114459 VdbeCoverageIf(v, op==TK_ISNULL);
114460 VdbeCoverageIf(v, op==TK_NOTNULL);
114461 sqlite3VdbeAddOp2(v, OP_Integer71, 0, target);
114462 sqlite3VdbeJumpHere(v, addr);
114463 break;
114464 }
114465 case TK_AGG_FUNCTION169: {
114466 AggInfo *pInfo = pExpr->pAggInfo;
114467 if( pInfo==0
114468 || NEVER(pExpr->iAgg<0)(pExpr->iAgg<0)
114469 || NEVER(pExpr->iAgg>=pInfo->nFunc)(pExpr->iAgg>=pInfo->nFunc)
114470 ){
114471 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114472 sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr);
114473 }else{
114474 return AggInfoFuncReg(pInfo, pExpr->iAgg)((pInfo)->iFirstReg+(pInfo)->nColumn+(pExpr->iAgg));
114475 }
114476 break;
114477 }
114478 case TK_FUNCTION172: {
114479 ExprList *pFarg; /* List of function arguments */
114480 int nFarg; /* Number of function arguments */
114481 FuncDef *pDef; /* The function definition object */
114482 const char *zId; /* The function name */
114483 u32 constMask = 0; /* Mask of function arguments that are constant */
114484 int i; /* Loop counter */
114485 sqlite3 *db = pParse->db; /* The database connection */
114486 u8 enc = ENC(db)((db)->enc); /* The text encoding used by this database */
114487 CollSeq *pColl = 0; /* A collating sequence */
114488
114489#ifndef SQLITE_OMIT_WINDOWFUNC
114490 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
114491 return pExpr->y.pWin->regResult;
114492 }
114493#endif
114494
114495 if( ConstFactorOk(pParse)((pParse)->okConstFactor)
114496 && sqlite3ExprIsConstantNotJoin(pParse,pExpr)
114497 ){
114498 /* SQL functions can be expensive. So try to avoid running them
114499 ** multiple times if we know they always give the same result */
114500 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
114501 }
114502 assert( !ExprHasProperty(pExpr, EP_TokenOnly) )((void) (0));
114503 assert( ExprUseXList(pExpr) )((void) (0));
114504 pFarg = pExpr->x.pList;
114505 nFarg = pFarg ? pFarg->nExpr : 0;
114506 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114507 zId = pExpr->u.zToken;
114508 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
114509#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
114510 if( pDef==0 && pParse->explain ){
114511 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
114512 }
114513#endif
114514 if( pDef==0 || pDef->xFinalize!=0 ){
114515 sqlite3ErrorMsg(pParse, "unknown function: %#T()", pExpr);
114516 break;
114517 }
114518 if( (pDef->funcFlags & SQLITE_FUNC_INLINE0x00400000)!=0 && ALWAYS(pFarg!=0)(pFarg!=0) ){
114519 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 )((void) (0));
114520 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 )((void) (0));
114521 return exprCodeInlineFunction(pParse, pFarg,
114522 SQLITE_PTR_TO_INT(pDef->pUserData)((int)(long int)(pDef->pUserData)), target);
114523 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT0x00080000|SQLITE_FUNC_UNSAFE0x00200000) ){
114524 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
114525 }
114526
114527 for(i=0; i<nFarg; i++){
114528 if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){
114529 testcase( i==31 );
114530 constMask |= MASKBIT32(i)(((unsigned int)1)<<(i));
114531 }
114532 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)!=0 && !pColl ){
114533 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
114534 }
114535 }
114536 if( pFarg ){
114537 if( constMask ){
114538 r1 = pParse->nMem+1;
114539 pParse->nMem += nFarg;
114540 }else{
114541 r1 = sqlite3GetTempRange(pParse, nFarg);
114542 }
114543
114544 /* For length() and typeof() and octet_length() functions,
114545 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
114546 ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid
114547 ** unnecessary data loading.
114548 */
114549 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH0x0040|SQLITE_FUNC_TYPEOF0x0080))!=0 ){
114550 u8 exprOp;
114551 assert( nFarg==1 )((void) (0));
114552 assert( pFarg->a[0].pExpr!=0 )((void) (0));
114553 exprOp = pFarg->a[0].pExpr->op;
114554 if( exprOp==TK_COLUMN168 || exprOp==TK_AGG_COLUMN170 ){
114555 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG )((void) (0));
114556 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG )((void) (0));
114557 assert( SQLITE_FUNC_BYTELEN==OPFLAG_BYTELENARG )((void) (0));
114558 assert( (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG)==OPFLAG_BYTELENARG )((void) (0));
114559 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
114560 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
114561 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
114562 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG0xc0;
114563 }
114564 }
114565
114566 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, SQLITE_ECEL_FACTOR0x02);
114567 }else{
114568 r1 = 0;
114569 }
114570#ifndef SQLITE_OMIT_VIRTUALTABLE
114571 /* Possibly overload the function if the first argument is
114572 ** a virtual table column.
114573 **
114574 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
114575 ** second argument, not the first, as the argument to test to
114576 ** see if it is a column in a virtual table. This is done because
114577 ** the left operand of infix functions (the operand we want to
114578 ** control overloading) ends up as the second argument to the
114579 ** function. The expression "A glob B" is equivalent to
114580 ** "glob(B,A). We want to use the A in "A glob B" to test
114581 ** for function overloading. But we use the B term in "glob(B,A)".
114582 */
114583 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc)(((pExpr)->flags&(0x000100))!=0) ){
114584 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
114585 }else if( nFarg>0 ){
114586 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
114587 }
114588#endif
114589 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
114590 if( !pColl ) pColl = db->pDfltColl;
114591 sqlite3VdbeAddOp4(v, OP_CollSeq85, 0, 0, 0, (char *)pColl, P4_COLLSEQ(-2));
114592 }
114593 sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg,
114594 pDef, pExpr->op2);
114595 if( nFarg ){
114596 if( constMask==0 ){
114597 sqlite3ReleaseTempRange(pParse, r1, nFarg);
114598 }else{
114599 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
114600 }
114601 }
114602 return target;
114603 }
114604#ifndef SQLITE_OMIT_SUBQUERY
114605 case TK_EXISTS20:
114606 case TK_SELECT139: {
114607 int nCol;
114608 testcase( op==TK_EXISTS );
114609 testcase( op==TK_SELECT );
114610 if( pParse->db->mallocFailed ){
114611 return 0;
114612 }else if( op==TK_SELECT139
114613 && ALWAYS( ExprUseXSelect(pExpr) )((((pExpr)->flags&0x001000)!=0))
114614 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
114615 ){
114616 sqlite3SubselectError(pParse, nCol, 1);
114617 }else{
114618 return sqlite3CodeSubselect(pParse, pExpr);
114619 }
114620 break;
114621 }
114622 case TK_SELECT_COLUMN178: {
114623 int n;
114624 Expr *pLeft = pExpr->pLeft;
114625 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
114626 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
114627 pLeft->op2 = pParse->withinRJSubrtn;
114628 }
114629 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR )((void) (0));
114630 n = sqlite3ExprVectorSize(pLeft);
114631 if( pExpr->iTable!=n ){
114632 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
114633 pExpr->iTable, n);
114634 }
114635 return pLeft->iTable + pExpr->iColumn;
114636 }
114637 case TK_IN50: {
114638 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
114639 int destIfNull = sqlite3VdbeMakeLabel(pParse);
114640 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
114641 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
114642 sqlite3VdbeAddOp2(v, OP_Integer71, 1, target);
114643 sqlite3VdbeResolveLabel(v, destIfFalse);
114644 sqlite3VdbeAddOp2(v, OP_AddImm86, target, 0);
114645 sqlite3VdbeResolveLabel(v, destIfNull);
114646 return target;
114647 }
114648#endif /* SQLITE_OMIT_SUBQUERY */
114649
114650
114651 /*
114652 ** x BETWEEN y AND z
114653 **
114654 ** This is equivalent to
114655 **
114656 ** x>=y AND x<=z
114657 **
114658 ** X is stored in pExpr->pLeft.
114659 ** Y is stored in pExpr->pList->a[0].pExpr.
114660 ** Z is stored in pExpr->pList->a[1].pExpr.
114661 */
114662 case TK_BETWEEN49: {
114663 exprCodeBetween(pParse, pExpr, target, 0, 0);
114664 return target;
114665 }
114666 case TK_COLLATE114: {
114667 if( !ExprHasProperty(pExpr, EP_Collate)(((pExpr)->flags&(0x000200))!=0) ){
114668 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
114669 ** "SOFT-COLLATE" that is added to constraints that are pushed down
114670 ** from outer queries into sub-queries by the WHERE-clause push-down
114671 ** optimization. Clear subtypes as subtypes may not cross a subquery
114672 ** boundary.
114673 */
114674 assert( pExpr->pLeft )((void) (0));
114675 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114676 sqlite3VdbeAddOp1(v, OP_ClrSubtype180, target);
114677 return target;
114678 }else{
114679 pExpr = pExpr->pLeft;
114680 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
114681 }
114682 }
114683 case TK_SPAN181:
114684 case TK_UPLUS173: {
114685 pExpr = pExpr->pLeft;
114686 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
114687 }
114688
114689 case TK_TRIGGER78: {
114690 /* If the opcode is TK_TRIGGER, then the expression is a reference
114691 ** to a column in the new.* or old.* pseudo-tables available to
114692 ** trigger programs. In this case Expr.iTable is set to 1 for the
114693 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
114694 ** is set to the column of the pseudo-table to read, or to -1 to
114695 ** read the rowid field.
114696 **
114697 ** The expression is implemented using an OP_Param opcode. The p1
114698 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
114699 ** to reference another column of the old.* pseudo-table, where
114700 ** i is the index of the column. For a new.rowid reference, p1 is
114701 ** set to (n+1), where n is the number of columns in each pseudo-table.
114702 ** For a reference to any other column in the new.* pseudo-table, p1
114703 ** is set to (n+2+i), where n and i are as defined previously. For
114704 ** example, if the table on which triggers are being fired is
114705 ** declared as:
114706 **
114707 ** CREATE TABLE t1(a, b);
114708 **
114709 ** Then p1 is interpreted as follows:
114710 **
114711 ** p1==0 -> old.rowid p1==3 -> new.rowid
114712 ** p1==1 -> old.a p1==4 -> new.a
114713 ** p1==2 -> old.b p1==5 -> new.b
114714 */
114715 Table *pTab;
114716 int iCol;
114717 int p1;
114718
114719 assert( ExprUseYTab(pExpr) )((void) (0));
114720 pTab = pExpr->y.pTab;
114721 iCol = pExpr->iColumn;
114722 p1 = pExpr->iTable * (pTab->nCol+1) + 1
114723 + sqlite3TableColumnToStorage(pTab, iCol);
114724
114725 assert( pExpr->iTable==0 || pExpr->iTable==1 )((void) (0));
114726 assert( iCol>=-1 && iCol<pTab->nCol )((void) (0));
114727 assert( pTab->iPKey<0 || iCol!=pTab->iPKey )((void) (0));
114728 assert( p1>=0 && p1<(pTab->nCol*2+2) )((void) (0));
114729
114730 sqlite3VdbeAddOp2(v, OP_Param157, p1, target);
114731 VdbeComment((v, "r[%d]=%s.%s", target,
114732 (pExpr->iTable ? "new" : "old"),
114733 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
114734 ));
114735
114736#ifndef SQLITE_OMIT_FLOATING_POINT
114737 /* If the column has REAL affinity, it may currently be stored as an
114738 ** integer. Use OP_RealAffinity to make sure it is really real.
114739 **
114740 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
114741 ** floating point when extracting it from the record. */
114742 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL0x45 ){
114743 sqlite3VdbeAddOp1(v, OP_RealAffinity87, target);
114744 }
114745#endif
114746 break;
114747 }
114748
114749 case TK_VECTOR177: {
114750 sqlite3ErrorMsg(pParse, "row value misused");
114751 break;
114752 }
114753
114754 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
114755 ** that derive from the right-hand table of a LEFT JOIN. The
114756 ** Expr.iTable value is the table number for the right-hand table.
114757 ** The expression is only evaluated if that table is not currently
114758 ** on a LEFT JOIN NULL row.
114759 */
114760 case TK_IF_NULL_ROW179: {
114761 int addrINR;
114762 u8 okConstFactor = pParse->okConstFactor;
114763 AggInfo *pAggInfo = pExpr->pAggInfo;
114764 if( pAggInfo ){
114765 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn )((void) (0));
114766 if( !pAggInfo->directMode ){
114767 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg)((pAggInfo)->iFirstReg+(pExpr->iAgg));
114768 break;
114769 }
114770 if( pExpr->pAggInfo->useSortingIdx ){
114771 sqlite3VdbeAddOp3(v, OP_Column94, pAggInfo->sortingIdxPTab,
114772 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
114773 target);
114774 inReg = target;
114775 break;
114776 }
114777 }
114778 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow20, pExpr->iTable, 0, target);
114779 /* The OP_IfNullRow opcode above can overwrite the result register with
114780 ** NULL. So we have to ensure that the result register is not a value
114781 ** that is suppose to be a constant. Two defenses are needed:
114782 ** (1) Temporarily disable factoring of constant expressions
114783 ** (2) Make sure the computed value really is stored in register
114784 ** "target" and not someplace else.
114785 */
114786 pParse->okConstFactor = 0; /* note (1) above */
114787 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114788 assert( target==inReg )((void) (0));
114789 pParse->okConstFactor = okConstFactor;
114790 sqlite3VdbeJumpHere(v, addrINR);
114791 break;
114792 }
114793
114794 /*
114795 ** Form A:
114796 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
114797 **
114798 ** Form B:
114799 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
114800 **
114801 ** Form A is can be transformed into the equivalent form B as follows:
114802 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
114803 ** WHEN x=eN THEN rN ELSE y END
114804 **
114805 ** X (if it exists) is in pExpr->pLeft.
114806 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
114807 ** odd. The Y is also optional. If the number of elements in x.pList
114808 ** is even, then Y is omitted and the "otherwise" result is NULL.
114809 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
114810 **
114811 ** The result of the expression is the Ri for the first matching Ei,
114812 ** or if there is no matching Ei, the ELSE term Y, or if there is
114813 ** no ELSE term, NULL.
114814 */
114815 case TK_CASE158: {
114816 int endLabel; /* GOTO label for end of CASE stmt */
114817 int nextCase; /* GOTO label for next WHEN clause */
114818 int nExpr; /* 2x number of WHEN terms */
114819 int i; /* Loop counter */
114820 ExprList *pEList; /* List of WHEN terms */
114821 struct ExprList_item *aListelem; /* Array of WHEN terms */
114822 Expr opCompare; /* The X==Ei expression */
114823 Expr *pX; /* The X expression */
114824 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
114825 Expr *pDel = 0;
114826 sqlite3 *db = pParse->db;
114827
114828 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 )((void) (0));
114829 assert(pExpr->x.pList->nExpr > 0)((void) (0));
114830 pEList = pExpr->x.pList;
114831 aListelem = pEList->a;
114832 nExpr = pEList->nExpr;
114833 endLabel = sqlite3VdbeMakeLabel(pParse);
114834 if( (pX = pExpr->pLeft)!=0 ){
114835 pDel = sqlite3ExprDup(db, pX, 0);
114836 if( db->mallocFailed ){
114837 sqlite3ExprDelete(db, pDel);
114838 break;
114839 }
114840 testcase( pX->op==TK_COLUMN );
114841 sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
114842 testcase( regFree1==0 );
114843 memset(&opCompare, 0, sizeof(opCompare));
114844 opCompare.op = TK_EQ54;
114845 opCompare.pLeft = pDel;
114846 pTest = &opCompare;
114847 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
114848 ** The value in regFree1 might get SCopy-ed into the file result.
114849 ** So make sure that the regFree1 register is not reused for other
114850 ** purposes and possibly overwritten. */
114851 regFree1 = 0;
114852 }
114853 for(i=0; i<nExpr-1; i=i+2){
114854 if( pX ){
114855 assert( pTest!=0 )((void) (0));
114856 opCompare.pRight = aListelem[i].pExpr;
114857 }else{
114858 pTest = aListelem[i].pExpr;
114859 }
114860 nextCase = sqlite3VdbeMakeLabel(pParse);
114861 testcase( pTest->op==TK_COLUMN );
114862 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL0x10);
114863 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
114864 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
114865 sqlite3VdbeGoto(v, endLabel);
114866 sqlite3VdbeResolveLabel(v, nextCase);
114867 }
114868 if( (nExpr&1)!=0 ){
114869 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
114870 }else{
114871 sqlite3VdbeAddOp2(v, OP_Null75, 0, target);
114872 }
114873 sqlite3ExprDelete(db, pDel);
114874 setDoNotMergeFlagOnCopy(v);
114875 sqlite3VdbeResolveLabel(v, endLabel);
114876 break;
114877 }
114878#ifndef SQLITE_OMIT_TRIGGER
114879 case TK_RAISE72: {
114880 assert( pExpr->affExpr==OE_Rollback((void) (0))
114881 || pExpr->affExpr==OE_Abort((void) (0))
114882 || pExpr->affExpr==OE_Fail((void) (0))
114883 || pExpr->affExpr==OE_Ignore((void) (0))
114884 )((void) (0));
114885 if( !pParse->pTriggerTab && !pParse->nested ){
114886 sqlite3ErrorMsg(pParse,
114887 "RAISE() may only be used within a trigger-program");
114888 return 0;
114889 }
114890 if( pExpr->affExpr==OE_Abort2 ){
114891 sqlite3MayAbort(pParse);
114892 }
114893 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
114894 if( pExpr->affExpr==OE_Ignore4 ){
114895 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_OK0, OE_Ignore4);
114896 VdbeCoverage(v);
114897 }else{
114898 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
114899 sqlite3VdbeAddOp3(v, OP_Halt70,
114900 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) : SQLITE_ERROR1,
114901 pExpr->affExpr, r1);
114902 }
114903 break;
114904 }
114905#endif
114906 }
114907 sqlite3ReleaseTempReg(pParse, regFree1);
114908 sqlite3ReleaseTempReg(pParse, regFree2);
114909 return inReg;
114910}
114911
114912/*
114913** Generate code that will evaluate expression pExpr just one time
114914** per prepared statement execution.
114915**
114916** If the expression uses functions (that might throw an exception) then
114917** guard them with an OP_Once opcode to ensure that the code is only executed
114918** once. If no functions are involved, then factor the code out and put it at
114919** the end of the prepared statement in the initialization section.
114920**
114921** If regDest>0 then the result is always stored in that register and the
114922** result is not reusable. If regDest<0 then this routine is free to
114923** store the value wherever it wants. The register where the expression
114924** is stored is returned. When regDest<0, two identical expressions might
114925** code to the same register, if they do not contain function calls and hence
114926** are factored out into the initialization section at the end of the
114927** prepared statement.
114928*/
114929SQLITE_PRIVATEstatic int sqlite3ExprCodeRunJustOnce(
114930 Parse *pParse, /* Parsing context */
114931 Expr *pExpr, /* The expression to code when the VDBE initializes */
114932 int regDest /* Store the value in this register */
114933){
114934 ExprList *p;
114935 assert( ConstFactorOk(pParse) )((void) (0));
114936 assert( regDest!=0 )((void) (0));
114937 p = pParse->pConstExpr;
114938 if( regDest<0 && p ){
114939 struct ExprList_item *pItem;
114940 int i;
114941 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
114942 if( pItem->fg.reusable
114943 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
114944 ){
114945 return pItem->u.iConstExprReg;
114946 }
114947 }
114948 }
114949 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
114950 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc)(((pExpr)->flags&(0x000008))!=0) ){
114951 Vdbe *v = pParse->pVdbe;
114952 int addr;
114953 assert( v )((void) (0));
114954 addr = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
114955 pParse->okConstFactor = 0;
114956 if( !pParse->db->mallocFailed ){
114957 if( regDest<0 ) regDest = ++pParse->nMem;
114958 sqlite3ExprCode(pParse, pExpr, regDest);
114959 }
114960 pParse->okConstFactor = 1;
114961 sqlite3ExprDelete(pParse->db, pExpr);
114962 sqlite3VdbeJumpHere(v, addr);
114963 }else{
114964 p = sqlite3ExprListAppend(pParse, p, pExpr);
114965 if( p ){
114966 struct ExprList_item *pItem = &p->a[p->nExpr-1];
114967 pItem->fg.reusable = regDest<0;
114968 if( regDest<0 ) regDest = ++pParse->nMem;
114969 pItem->u.iConstExprReg = regDest;
114970 }
114971 pParse->pConstExpr = p;
114972 }
114973 return regDest;
114974}
114975
114976/*
114977** Generate code to evaluate an expression and store the results
114978** into a register. Return the register number where the results
114979** are stored.
114980**
114981** If the register is a temporary register that can be deallocated,
114982** then write its number into *pReg. If the result register is not
114983** a temporary, then set *pReg to zero.
114984**
114985** If pExpr is a constant, then this routine might generate this
114986** code to fill the register in the initialization section of the
114987** VDBE program, in order to factor it out of the evaluation loop.
114988*/
114989SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
114990 int r2;
114991 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
114992 if( ConstFactorOk(pParse)((pParse)->okConstFactor)
114993 && ALWAYS(pExpr!=0)(pExpr!=0)
114994 && pExpr->op!=TK_REGISTER176
114995 && sqlite3ExprIsConstantNotJoin(pParse, pExpr)
114996 ){
114997 *pReg = 0;
114998 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
114999 }else{
115000 int r1 = sqlite3GetTempReg(pParse);
115001 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
115002 if( r2==r1 ){
115003 *pReg = r1;
115004 }else{
115005 sqlite3ReleaseTempReg(pParse, r1);
115006 *pReg = 0;
115007 }
115008 }
115009 return r2;
115010}
115011
115012/*
115013** Generate code that will evaluate expression pExpr and store the
115014** results in register target. The results are guaranteed to appear
115015** in register target.
115016*/
115017SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
115018 int inReg;
115019
115020 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
115021 assert( target>0 && target<=pParse->nMem )((void) (0));
115022 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed )((void) (0));
115023 if( pParse->pVdbe==0 ) return;
115024 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
115025 if( inReg!=target ){
115026 u8 op;
115027 Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr);
115028 testcase( pX!=pExpr );
115029 if( ALWAYS(pX)(pX)
115030 && (ExprHasProperty(pX,EP_Subquery)(((pX)->flags&(0x400000))!=0) || pX->op==TK_REGISTER176)
115031 ){
115032 op = OP_Copy80;
115033 }else{
115034 op = OP_SCopy81;
115035 }
115036 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
115037 }
115038}
115039
115040/*
115041** Make a transient copy of expression pExpr and then code it using
115042** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
115043** except that the input expression is guaranteed to be unchanged.
115044*/
115045SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
115046 sqlite3 *db = pParse->db;
115047 pExpr = sqlite3ExprDup(db, pExpr, 0);
115048 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
115049 sqlite3ExprDelete(db, pExpr);
115050}
115051
115052/*
115053** Generate code that will evaluate expression pExpr and store the
115054** results in register target. The results are guaranteed to appear
115055** in register target. If the expression is constant, then this routine
115056** might choose to code the expression at initialization time.
115057*/
115058SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
115059 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){
115060 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target);
115061 }else{
115062 sqlite3ExprCodeCopy(pParse, pExpr, target);
115063 }
115064}
115065
115066/*
115067** Generate code that pushes the value of every element of the given
115068** expression list into a sequence of registers beginning at target.
115069**
115070** Return the number of elements evaluated. The number returned will
115071** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
115072** is defined.
115073**
115074** The SQLITE_ECEL_DUP flag prevents the arguments from being
115075** filled using OP_SCopy. OP_Copy must be used instead.
115076**
115077** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
115078** factored out into initialization code.
115079**
115080** The SQLITE_ECEL_REF flag means that expressions in the list with
115081** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
115082** in registers at srcReg, and so the value can be copied from there.
115083** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
115084** are simply omitted rather than being copied from srcReg.
115085*/
115086SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(
115087 Parse *pParse, /* Parsing context */
115088 ExprList *pList, /* The expression list to be coded */
115089 int target, /* Where to write results */
115090 int srcReg, /* Source registers if SQLITE_ECEL_REF */
115091 u8 flags /* SQLITE_ECEL_* flags */
115092){
115093 struct ExprList_item *pItem;
115094 int i, j, n;
115095 u8 copyOp = (flags & SQLITE_ECEL_DUP0x01) ? OP_Copy80 : OP_SCopy81;
115096 Vdbe *v = pParse->pVdbe;
115097 assert( pList!=0 )((void) (0));
115098 assert( target>0 )((void) (0));
115099 assert( pParse->pVdbe!=0 )((void) (0)); /* Never gets this far otherwise */
115100 n = pList->nExpr;
115101 if( !ConstFactorOk(pParse)((pParse)->okConstFactor) ) flags &= ~SQLITE_ECEL_FACTOR0x02;
115102 for(pItem=pList->a, i=0; i<n; i++, pItem++){
115103 Expr *pExpr = pItem->pExpr;
115104#ifdef SQLITE_ENABLE_SORTER_REFERENCES
115105 if( pItem->fg.bSorterRef ){
115106 i--;
115107 n--;
115108 }else
115109#endif
115110 if( (flags & SQLITE_ECEL_REF0x04)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
115111 if( flags & SQLITE_ECEL_OMITREF0x08 ){
115112 i--;
115113 n--;
115114 }else{
115115 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
115116 }
115117 }else if( (flags & SQLITE_ECEL_FACTOR0x02)!=0
115118 && sqlite3ExprIsConstantNotJoin(pParse,pExpr)
115119 ){
115120 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i);
115121 }else{
115122 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
115123 if( inReg!=target+i ){
115124 VdbeOp *pOp;
115125 if( copyOp==OP_Copy80
115126 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy80
115127 && pOp->p1+pOp->p3+1==inReg
115128 && pOp->p2+pOp->p3+1==target+i
115129 && pOp->p5==0 /* The do-not-merge flag must be clear */
115130 ){
115131 pOp->p3++;
115132 }else{
115133 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
115134 }
115135 }
115136 }
115137 }
115138 return n;
115139}
115140
115141/*
115142** Generate code for a BETWEEN operator.
115143**
115144** x BETWEEN y AND z
115145**
115146** The above is equivalent to
115147**
115148** x>=y AND x<=z
115149**
115150** Code it as such, taking care to do the common subexpression
115151** elimination of x.
115152**
115153** The xJumpIf parameter determines details:
115154**
115155** NULL: Store the boolean result in reg[dest]
115156** sqlite3ExprIfTrue: Jump to dest if true
115157** sqlite3ExprIfFalse: Jump to dest if false
115158**
115159** The jumpIfNull parameter is ignored if xJumpIf is NULL.
115160*/
115161static void exprCodeBetween(
115162 Parse *pParse, /* Parsing and code generating context */
115163 Expr *pExpr, /* The BETWEEN expression */
115164 int dest, /* Jump destination or storage location */
115165 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
115166 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
115167){
115168 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
115169 Expr compLeft; /* The x>=y term */
115170 Expr compRight; /* The x<=z term */
115171 int regFree1 = 0; /* Temporary use register */
115172 Expr *pDel = 0;
115173 sqlite3 *db = pParse->db;
115174
115175 memset(&compLeft, 0, sizeof(Expr));
115176 memset(&compRight, 0, sizeof(Expr));
115177 memset(&exprAnd, 0, sizeof(Expr));
115178
115179 assert( ExprUseXList(pExpr) )((void) (0));
115180 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
115181 if( db->mallocFailed==0 ){
115182 exprAnd.op = TK_AND44;
115183 exprAnd.pLeft = &compLeft;
115184 exprAnd.pRight = &compRight;
115185 compLeft.op = TK_GE58;
115186 compLeft.pLeft = pDel;
115187 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
115188 compRight.op = TK_LE56;
115189 compRight.pLeft = pDel;
115190 compRight.pRight = pExpr->x.pList->a[1].pExpr;
115191 sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
115192 if( xJump ){
115193 xJump(pParse, &exprAnd, dest, jumpIfNull);
115194 }else{
115195 /* Mark the expression is being from the ON or USING clause of a join
115196 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
115197 ** it into the Parse.pConstExpr list. We should use a new bit for this,
115198 ** for clarity, but we are out of bits in the Expr.flags field so we
115199 ** have to reuse the EP_OuterON bit. Bummer. */
115200 pDel->flags |= EP_OuterON0x000001;
115201 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
115202 }
115203 sqlite3ReleaseTempReg(pParse, regFree1);
115204 }
115205 sqlite3ExprDelete(db, pDel);
115206
115207 /* Ensure adequate test coverage */
115208 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
115209 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
115210 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
115211 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
115212 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
115213 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
115214 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
115215 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
115216 testcase( xJump==0 );
115217}
115218
115219/*
115220** Generate code for a boolean expression such that a jump is made
115221** to the label "dest" if the expression is true but execution
115222** continues straight thru if the expression is false.
115223**
115224** If the expression evaluates to NULL (neither true nor false), then
115225** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
115226**
115227** This code depends on the fact that certain token values (ex: TK_EQ)
115228** are the same as opcode values (ex: OP_Eq) that implement the corresponding
115229** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
115230** the make process cause these values to align. Assert()s in the code
115231** below verify that the numbers are aligned correctly.
115232*/
115233SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
115234 Vdbe *v = pParse->pVdbe;
115235 int op = 0;
115236 int regFree1 = 0;
115237 int regFree2 = 0;
115238 int r1, r2;
115239
115240 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
115241 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
115242 if( NEVER(pExpr==0)(pExpr==0) ) return; /* No way this can happen */
115243 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) )((void) (0));
115244 op = pExpr->op;
115245 switch( op ){
115246 case TK_AND44:
115247 case TK_OR43: {
115248 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
115249 if( pAlt!=pExpr ){
115250 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
115251 }else if( op==TK_AND44 ){
115252 int d2 = sqlite3VdbeMakeLabel(pParse);
115253 testcase( jumpIfNull==0 );
115254 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
115255 jumpIfNull^SQLITE_JUMPIFNULL0x10);
115256 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
115257 sqlite3VdbeResolveLabel(v, d2);
115258 }else{
115259 testcase( jumpIfNull==0 );
115260 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
115261 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
115262 }
115263 break;
115264 }
115265 case TK_NOT19: {
115266 testcase( jumpIfNull==0 );
115267 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
115268 break;
115269 }
115270 case TK_TRUTH175: {
115271 int isNot; /* IS NOT TRUE or IS NOT FALSE */
115272 int isTrue; /* IS TRUE or IS NOT TRUE */
115273 testcase( jumpIfNull==0 );
115274 isNot = pExpr->op2==TK_ISNOT46;
115275 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
115276 testcase( isTrue && isNot );
115277 testcase( !isTrue && isNot );
115278 if( isTrue ^ isNot ){
115279 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
115280 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
115281 }else{
115282 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
115283 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
115284 }
115285 break;
115286 }
115287 case TK_IS45:
115288 case TK_ISNOT46:
115289 testcase( op==TK_IS );
115290 testcase( op==TK_ISNOT );
115291 op = (op==TK_IS45) ? TK_EQ54 : TK_NE53;
115292 jumpIfNull = SQLITE_NULLEQ0x80;
115293 /* no break */ deliberate_fall_through__attribute__((fallthrough));
115294 case TK_LT57:
115295 case TK_LE56:
115296 case TK_GT55:
115297 case TK_GE58:
115298 case TK_NE53:
115299 case TK_EQ54: {
115300 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
115301 testcase( jumpIfNull==0 );
115302 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115303 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
115304 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
115305 r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0));
115306 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
115307 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
115308 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
115309 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
115310 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
115311 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
115312 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
115313 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
115314 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
115315 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
115316 testcase( regFree1==0 );
115317 testcase( regFree2==0 );
115318 break;
115319 }
115320 case TK_ISNULL51:
115321 case TK_NOTNULL52: {
115322 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
115323 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
115324 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115325 sqlite3VdbeTypeofColumn(v, r1);
115326 sqlite3VdbeAddOp2(v, op, r1, dest);
115327 VdbeCoverageIf(v, op==TK_ISNULL);
115328 VdbeCoverageIf(v, op==TK_NOTNULL);
115329 testcase( regFree1==0 );
115330 break;
115331 }
115332 case TK_BETWEEN49: {
115333 testcase( jumpIfNull==0 );
115334 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
115335 break;
115336 }
115337#ifndef SQLITE_OMIT_SUBQUERY
115338 case TK_IN50: {
115339 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
115340 int destIfNull = jumpIfNull ? dest : destIfFalse;
115341 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
115342 sqlite3VdbeGoto(v, dest);
115343 sqlite3VdbeResolveLabel(v, destIfFalse);
115344 break;
115345 }
115346#endif
115347 default: {
115348 default_expr:
115349 if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
115350 sqlite3VdbeGoto(v, dest);
115351 }else if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
115352 /* No-op */
115353 }else{
115354 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
115355 sqlite3VdbeAddOp3(v, OP_If16, r1, dest, jumpIfNull!=0);
115356 VdbeCoverage(v);
115357 testcase( regFree1==0 );
115358 testcase( jumpIfNull==0 );
115359 }
115360 break;
115361 }
115362 }
115363 sqlite3ReleaseTempReg(pParse, regFree1);
115364 sqlite3ReleaseTempReg(pParse, regFree2);
115365}
115366
115367/*
115368** Generate code for a boolean expression such that a jump is made
115369** to the label "dest" if the expression is false but execution
115370** continues straight thru if the expression is true.
115371**
115372** If the expression evaluates to NULL (neither true nor false) then
115373** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
115374** is 0.
115375*/
115376SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
115377 Vdbe *v = pParse->pVdbe;
115378 int op = 0;
115379 int regFree1 = 0;
115380 int regFree2 = 0;
115381 int r1, r2;
115382
115383 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
115384 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
115385 if( pExpr==0 ) return;
115386 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) )((void) (0));
115387
115388 /* The value of pExpr->op and op are related as follows:
115389 **
115390 ** pExpr->op op
115391 ** --------- ----------
115392 ** TK_ISNULL OP_NotNull
115393 ** TK_NOTNULL OP_IsNull
115394 ** TK_NE OP_Eq
115395 ** TK_EQ OP_Ne
115396 ** TK_GT OP_Le
115397 ** TK_LE OP_Gt
115398 ** TK_GE OP_Lt
115399 ** TK_LT OP_Ge
115400 **
115401 ** For other values of pExpr->op, op is undefined and unused.
115402 ** The value of TK_ and OP_ constants are arranged such that we
115403 ** can compute the mapping above using the following expression.
115404 ** Assert()s verify that the computation is correct.
115405 */
115406 op = ((pExpr->op+(TK_ISNULL51&1))^1)-(TK_ISNULL51&1);
115407
115408 /* Verify correct alignment of TK_ and OP_ constants
115409 */
115410 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull )((void) (0));
115411 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull )((void) (0));
115412 assert( pExpr->op!=TK_NE || op==OP_Eq )((void) (0));
115413 assert( pExpr->op!=TK_EQ || op==OP_Ne )((void) (0));
115414 assert( pExpr->op!=TK_LT || op==OP_Ge )((void) (0));
115415 assert( pExpr->op!=TK_LE || op==OP_Gt )((void) (0));
115416 assert( pExpr->op!=TK_GT || op==OP_Le )((void) (0));
115417 assert( pExpr->op!=TK_GE || op==OP_Lt )((void) (0));
115418
115419 switch( pExpr->op ){
115420 case TK_AND44:
115421 case TK_OR43: {
115422 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
115423 if( pAlt!=pExpr ){
115424 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
115425 }else if( pExpr->op==TK_AND44 ){
115426 testcase( jumpIfNull==0 );
115427 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
115428 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
115429 }else{
115430 int d2 = sqlite3VdbeMakeLabel(pParse);
115431 testcase( jumpIfNull==0 );
115432 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
115433 jumpIfNull^SQLITE_JUMPIFNULL0x10);
115434 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
115435 sqlite3VdbeResolveLabel(v, d2);
115436 }
115437 break;
115438 }
115439 case TK_NOT19: {
115440 testcase( jumpIfNull==0 );
115441 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
115442 break;
115443 }
115444 case TK_TRUTH175: {
115445 int isNot; /* IS NOT TRUE or IS NOT FALSE */
115446 int isTrue; /* IS TRUE or IS NOT TRUE */
115447 testcase( jumpIfNull==0 );
115448 isNot = pExpr->op2==TK_ISNOT46;
115449 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
115450 testcase( isTrue && isNot );
115451 testcase( !isTrue && isNot );
115452 if( isTrue ^ isNot ){
115453 /* IS TRUE and IS NOT FALSE */
115454 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
115455 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
115456
115457 }else{
115458 /* IS FALSE and IS NOT TRUE */
115459 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
115460 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
115461 }
115462 break;
115463 }
115464 case TK_IS45:
115465 case TK_ISNOT46:
115466 testcase( pExpr->op==TK_IS );
115467 testcase( pExpr->op==TK_ISNOT );
115468 op = (pExpr->op==TK_IS45) ? TK_NE53 : TK_EQ54;
115469 jumpIfNull = SQLITE_NULLEQ0x80;
115470 /* no break */ deliberate_fall_through__attribute__((fallthrough));
115471 case TK_LT57:
115472 case TK_LE56:
115473 case TK_GT55:
115474 case TK_GE58:
115475 case TK_NE53:
115476 case TK_EQ54: {
115477 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
115478 testcase( jumpIfNull==0 );
115479 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115480 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
115481 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
115482 r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)(((pExpr)->flags&(0x000400))!=0));
115483 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
115484 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
115485 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
115486 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
115487 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
115488 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
115489 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
115490 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
115491 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
115492 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
115493 testcase( regFree1==0 );
115494 testcase( regFree2==0 );
115495 break;
115496 }
115497 case TK_ISNULL51:
115498 case TK_NOTNULL52: {
115499 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115500 sqlite3VdbeTypeofColumn(v, r1);
115501 sqlite3VdbeAddOp2(v, op, r1, dest);
115502 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
115503 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
115504 testcase( regFree1==0 );
115505 break;
115506 }
115507 case TK_BETWEEN49: {
115508 testcase( jumpIfNull==0 );
115509 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
115510 break;
115511 }
115512#ifndef SQLITE_OMIT_SUBQUERY
115513 case TK_IN50: {
115514 if( jumpIfNull ){
115515 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
115516 }else{
115517 int destIfNull = sqlite3VdbeMakeLabel(pParse);
115518 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
115519 sqlite3VdbeResolveLabel(v, destIfNull);
115520 }
115521 break;
115522 }
115523#endif
115524 default: {
115525 default_expr:
115526 if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
115527 sqlite3VdbeGoto(v, dest);
115528 }else if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
115529 /* no-op */
115530 }else{
115531 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
115532 sqlite3VdbeAddOp3(v, OP_IfNot17, r1, dest, jumpIfNull!=0);
115533 VdbeCoverage(v);
115534 testcase( regFree1==0 );
115535 testcase( jumpIfNull==0 );
115536 }
115537 break;
115538 }
115539 }
115540 sqlite3ReleaseTempReg(pParse, regFree1);
115541 sqlite3ReleaseTempReg(pParse, regFree2);
115542}
115543
115544/*
115545** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
115546** code generation, and that copy is deleted after code generation. This
115547** ensures that the original pExpr is unchanged.
115548*/
115549SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
115550 sqlite3 *db = pParse->db;
115551 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
115552 if( db->mallocFailed==0 ){
115553 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
115554 }
115555 sqlite3ExprDelete(db, pCopy);
115556}
115557
115558/*
115559** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
115560** type of expression.
115561**
115562** If pExpr is a simple SQL value - an integer, real, string, blob
115563** or NULL value - then the VDBE currently being prepared is configured
115564** to re-prepare each time a new value is bound to variable pVar.
115565**
115566** Additionally, if pExpr is a simple SQL value and the value is the
115567** same as that currently bound to variable pVar, non-zero is returned.
115568** Otherwise, if the values are not the same or if pExpr is not a simple
115569** SQL value, zero is returned.
115570*/
115571static int exprCompareVariable(
115572 const Parse *pParse,
115573 const Expr *pVar,
115574 const Expr *pExpr
115575){
115576 int res = 0;
115577 int iVar;
115578 sqlite3_value *pL, *pR = 0;
115579
115580 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF81, SQLITE_AFF_BLOB0x41, &pR);
115581 if( pR ){
115582 iVar = pVar->iColumn;
115583 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
115584 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB0x41);
115585 if( pL ){
115586 if( sqlite3_value_type(pL)==SQLITE_TEXT3 ){
115587 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
115588 }
115589 res = 0==sqlite3MemCompare(pL, pR, 0);
115590 }
115591 sqlite3ValueFree(pR);
115592 sqlite3ValueFree(pL);
115593 }
115594
115595 return res;
115596}
115597
115598/*
115599** Do a deep comparison of two expression trees. Return 0 if the two
115600** expressions are completely identical. Return 1 if they differ only
115601** by a COLLATE operator at the top level. Return 2 if there are differences
115602** other than the top-level COLLATE operator.
115603**
115604** If any subelement of pB has Expr.iTable==(-1) then it is allowed
115605** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
115606**
115607** The pA side might be using TK_REGISTER. If that is the case and pB is
115608** not using TK_REGISTER but is otherwise equivalent, then still return 0.
115609**
115610** Sometimes this routine will return 2 even if the two expressions
115611** really are equivalent. If we cannot prove that the expressions are
115612** identical, we return 2 just to be safe. So if this routine
115613** returns 2, then you do not really know for certain if the two
115614** expressions are the same. But if you get a 0 or 1 return, then you
115615** can be sure the expressions are the same. In the places where
115616** this routine is used, it does not hurt to get an extra 2 - that
115617** just might result in some slightly slower code. But returning
115618** an incorrect 0 or 1 could lead to a malfunction.
115619**
115620** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
115621** pParse->pReprepare can be matched against literals in pB. The
115622** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
115623** If pParse is NULL (the normal case) then any TK_VARIABLE term in
115624** Argument pParse should normally be NULL. If it is not NULL and pA or
115625** pB causes a return value of 2.
115626*/
115627SQLITE_PRIVATEstatic int sqlite3ExprCompare(
115628 const Parse *pParse,
115629 const Expr *pA,
115630 const Expr *pB,
115631 int iTab
115632){
115633 u32 combinedFlags;
115634 if( pA==0 || pB==0 ){
115635 return pB==pA ? 0 : 2;
115636 }
115637 if( pParse && pA->op==TK_VARIABLE157 && exprCompareVariable(pParse, pA, pB) ){
115638 return 0;
115639 }
115640 combinedFlags = pA->flags | pB->flags;
115641 if( combinedFlags & EP_IntValue0x000800 ){
115642 if( (pA->flags&pB->flags&EP_IntValue0x000800)!=0 && pA->u.iValue==pB->u.iValue ){
115643 return 0;
115644 }
115645 return 2;
115646 }
115647 if( pA->op!=pB->op || pA->op==TK_RAISE72 ){
115648 if( pA->op==TK_COLLATE114 && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
115649 return 1;
115650 }
115651 if( pB->op==TK_COLLATE114 && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
115652 return 1;
115653 }
115654 if( pA->op==TK_AGG_COLUMN170 && pB->op==TK_COLUMN168
115655 && pB->iTable<0 && pA->iTable==iTab
115656 ){
115657 /* fall through */
115658 }else{
115659 return 2;
115660 }
115661 }
115662 assert( !ExprHasProperty(pA, EP_IntValue) )((void) (0));
115663 assert( !ExprHasProperty(pB, EP_IntValue) )((void) (0));
115664 if( pA->u.zToken ){
115665 if( pA->op==TK_FUNCTION172 || pA->op==TK_AGG_FUNCTION169 ){
115666 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
115667#ifndef SQLITE_OMIT_WINDOWFUNC
115668 assert( pA->op==pB->op )((void) (0));
115669 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(0x1000000))!=0)!=ExprHasProperty(pB,EP_WinFunc)(((pB)->flags&(0x1000000))!=0) ){
115670 return 2;
115671 }
115672 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(0x1000000))!=0) ){
115673 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
115674 return 2;
115675 }
115676 }
115677#endif
115678 }else if( pA->op==TK_NULL122 ){
115679 return 0;
115680 }else if( pA->op==TK_COLLATE114 ){
115681 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
115682 }else
115683 if( pB->u.zToken!=0
115684 && pA->op!=TK_COLUMN168
115685 && pA->op!=TK_AGG_COLUMN170
115686 && strcmp(pA->u.zToken,pB->u.zToken)!=0
115687 ){
115688 return 2;
115689 }
115690 }
115691 if( (pA->flags & (EP_Distinct0x000004|EP_Commuted0x000400))
115692 != (pB->flags & (EP_Distinct0x000004|EP_Commuted0x000400)) ) return 2;
115693 if( ALWAYS((combinedFlags & EP_TokenOnly)==0)((combinedFlags & 0x010000)==0) ){
115694 if( combinedFlags & EP_xIsSelect0x001000 ) return 2;
115695 if( (combinedFlags & EP_FixedCol0x000020)==0
115696 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
115697 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
115698 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
115699 if( pA->op!=TK_STRING118
115700 && pA->op!=TK_TRUEFALSE171
115701 && ALWAYS((combinedFlags & EP_Reduced)==0)((combinedFlags & 0x004000)==0)
115702 ){
115703 if( pA->iColumn!=pB->iColumn ) return 2;
115704 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH175 ) return 2;
115705 if( pA->op!=TK_IN50 && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
115706 return 2;
115707 }
115708 }
115709 }
115710 return 0;
115711}
115712
115713/*
115714** Compare two ExprList objects. Return 0 if they are identical, 1
115715** if they are certainly different, or 2 if it is not possible to
115716** determine if they are identical or not.
115717**
115718** If any subelement of pB has Expr.iTable==(-1) then it is allowed
115719** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
115720**
115721** This routine might return non-zero for equivalent ExprLists. The
115722** only consequence will be disabled optimizations. But this routine
115723** must never return 0 if the two ExprList objects are different, or
115724** a malfunction will result.
115725**
115726** Two NULL pointers are considered to be the same. But a NULL pointer
115727** always differs from a non-NULL pointer.
115728*/
115729SQLITE_PRIVATEstatic int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){
115730 int i;
115731 if( pA==0 && pB==0 ) return 0;
115732 if( pA==0 || pB==0 ) return 1;
115733 if( pA->nExpr!=pB->nExpr ) return 1;
115734 for(i=0; i<pA->nExpr; i++){
115735 int res;
115736 Expr *pExprA = pA->a[i].pExpr;
115737 Expr *pExprB = pB->a[i].pExpr;
115738 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
115739 if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res;
115740 }
115741 return 0;
115742}
115743
115744/*
115745** Like sqlite3ExprCompare() except COLLATE operators at the top-level
115746** are ignored.
115747*/
115748SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
115749 return sqlite3ExprCompare(0,
115750 sqlite3ExprSkipCollate(pA),
115751 sqlite3ExprSkipCollate(pB),
115752 iTab);
115753}
115754
115755/*
115756** Return non-zero if Expr p can only be true if pNN is not NULL.
115757**
115758** Or if seenNot is true, return non-zero if Expr p can only be
115759** non-NULL if pNN is not NULL
115760*/
115761static int exprImpliesNotNull(
115762 const Parse *pParse,/* Parsing context */
115763 const Expr *p, /* The expression to be checked */
115764 const Expr *pNN, /* The expression that is NOT NULL */
115765 int iTab, /* Table being evaluated */
115766 int seenNot /* Return true only if p can be any non-NULL value */
115767){
115768 assert( p )((void) (0));
115769 assert( pNN )((void) (0));
115770 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
115771 return pNN->op!=TK_NULL122;
115772 }
115773 switch( p->op ){
115774 case TK_IN50: {
115775 if( seenNot && ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x001000))!=0) ) return 0;
115776 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) )((void) (0));
115777 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115778 }
115779 case TK_BETWEEN49: {
115780 ExprList *pList;
115781 assert( ExprUseXList(p) )((void) (0));
115782 pList = p->x.pList;
115783 assert( pList!=0 )((void) (0));
115784 assert( pList->nExpr==2 )((void) (0));
115785 if( seenNot ) return 0;
115786 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
115787 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
115788 ){
115789 return 1;
115790 }
115791 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115792 }
115793 case TK_EQ54:
115794 case TK_NE53:
115795 case TK_LT57:
115796 case TK_LE56:
115797 case TK_GT55:
115798 case TK_GE58:
115799 case TK_PLUS107:
115800 case TK_MINUS108:
115801 case TK_BITOR104:
115802 case TK_LSHIFT105:
115803 case TK_RSHIFT106:
115804 case TK_CONCAT112:
115805 seenNot = 1;
115806 /* no break */ deliberate_fall_through__attribute__((fallthrough));
115807 case TK_STAR109:
115808 case TK_REM111:
115809 case TK_BITAND103:
115810 case TK_SLASH110: {
115811 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
115812 /* no break */ deliberate_fall_through__attribute__((fallthrough));
115813 }
115814 case TK_SPAN181:
115815 case TK_COLLATE114:
115816 case TK_UPLUS173:
115817 case TK_UMINUS174: {
115818 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
115819 }
115820 case TK_TRUTH175: {
115821 if( seenNot ) return 0;
115822 if( p->op2!=TK_IS45 ) return 0;
115823 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115824 }
115825 case TK_BITNOT115:
115826 case TK_NOT19: {
115827 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115828 }
115829 }
115830 return 0;
115831}
115832
115833/*
115834** Return true if we can prove the pE2 will always be true if pE1 is
115835** true. Return false if we cannot complete the proof or if pE2 might
115836** be false. Examples:
115837**
115838** pE1: x==5 pE2: x==5 Result: true
115839** pE1: x>0 pE2: x==5 Result: false
115840** pE1: x=21 pE2: x=21 OR y=43 Result: true
115841** pE1: x!=123 pE2: x IS NOT NULL Result: true
115842** pE1: x!=?1 pE2: x IS NOT NULL Result: true
115843** pE1: x IS NULL pE2: x IS NOT NULL Result: false
115844** pE1: x IS ?2 pE2: x IS NOT NULL Result: false
115845**
115846** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
115847** Expr.iTable<0 then assume a table number given by iTab.
115848**
115849** If pParse is not NULL, then the values of bound variables in pE1 are
115850** compared against literal values in pE2 and pParse->pVdbe->expmask is
115851** modified to record which bound variables are referenced. If pParse
115852** is NULL, then false will be returned if pE1 contains any bound variables.
115853**
115854** When in doubt, return false. Returning true might give a performance
115855** improvement. Returning false might cause a performance reduction, but
115856** it will always give the correct answer and is hence always safe.
115857*/
115858SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(
115859 const Parse *pParse,
115860 const Expr *pE1,
115861 const Expr *pE2,
115862 int iTab
115863){
115864 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
115865 return 1;
115866 }
115867 if( pE2->op==TK_OR43
115868 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
115869 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
115870 ){
115871 return 1;
115872 }
115873 if( pE2->op==TK_NOTNULL52
115874 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
115875 ){
115876 return 1;
115877 }
115878 return 0;
115879}
115880
115881/* This is a helper function to impliesNotNullRow(). In this routine,
115882** set pWalker->eCode to one only if *both* of the input expressions
115883** separately have the implies-not-null-row property.
115884*/
115885static void bothImplyNotNullRow(Walker *pWalker, Expr *pE1, Expr *pE2){
115886 if( pWalker->eCode==0 ){
115887 sqlite3WalkExpr(pWalker, pE1);
115888 if( pWalker->eCode ){
115889 pWalker->eCode = 0;
115890 sqlite3WalkExpr(pWalker, pE2);
115891 }
115892 }
115893}
115894
115895/*
115896** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
115897** If the expression node requires that the table at pWalker->iCur
115898** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
115899**
115900** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
115901** behalf of a RIGHT JOIN (or FULL JOIN). That makes a difference when
115902** evaluating terms in the ON clause of an inner join.
115903**
115904** This routine controls an optimization. False positives (setting
115905** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
115906** (never setting pWalker->eCode) is a harmless missed optimization.
115907*/
115908static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
115909 testcase( pExpr->op==TK_AGG_COLUMN );
115910 testcase( pExpr->op==TK_AGG_FUNCTION );
115911 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return WRC_Prune1;
115912 if( ExprHasProperty(pExpr, EP_InnerON)(((pExpr)->flags&(0x000002))!=0) && pWalker->mWFlags ){
115913 /* If iCur is used in an inner-join ON clause to the left of a
115914 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
115915 ** But it is difficult to check for that condition precisely.
115916 ** To keep things simple, any use of iCur from any inner-join is
115917 ** ignored while attempting to simplify a RIGHT JOIN. */
115918 return WRC_Prune1;
115919 }
115920 switch( pExpr->op ){
115921 case TK_ISNOT46:
115922 case TK_ISNULL51:
115923 case TK_NOTNULL52:
115924 case TK_IS45:
115925 case TK_VECTOR177:
115926 case TK_FUNCTION172:
115927 case TK_TRUTH175:
115928 case TK_CASE158:
115929 testcase( pExpr->op==TK_ISNOT );
115930 testcase( pExpr->op==TK_ISNULL );
115931 testcase( pExpr->op==TK_NOTNULL );
115932 testcase( pExpr->op==TK_IS );
115933 testcase( pExpr->op==TK_VECTOR );
115934 testcase( pExpr->op==TK_FUNCTION );
115935 testcase( pExpr->op==TK_TRUTH );
115936 testcase( pExpr->op==TK_CASE );
115937 return WRC_Prune1;
115938
115939 case TK_COLUMN168:
115940 if( pWalker->u.iCur==pExpr->iTable ){
115941 pWalker->eCode = 1;
115942 return WRC_Abort2;
115943 }
115944 return WRC_Prune1;
115945
115946 case TK_OR43:
115947 case TK_AND44:
115948 /* Both sides of an AND or OR must separately imply non-null-row.
115949 ** Consider these cases:
115950 ** 1. NOT (x AND y)
115951 ** 2. x OR y
115952 ** If only one of x or y is non-null-row, then the overall expression
115953 ** can be true if the other arm is false (case 1) or true (case 2).
115954 */
115955 testcase( pExpr->op==TK_OR );
115956 testcase( pExpr->op==TK_AND );
115957 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight);
115958 return WRC_Prune1;
115959
115960 case TK_IN50:
115961 /* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)",
115962 ** both of which can be true. But apart from these cases, if
115963 ** the left-hand side of the IN is NULL then the IN itself will be
115964 ** NULL. */
115965 if( ExprUseXList(pExpr)(((pExpr)->flags&0x001000)==0) && ALWAYS(pExpr->x.pList->nExpr>0)(pExpr->x.pList->nExpr>0) ){
115966 sqlite3WalkExpr(pWalker, pExpr->pLeft);
115967 }
115968 return WRC_Prune1;
115969
115970 case TK_BETWEEN49:
115971 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
115972 ** both y and z must be non-null row */
115973 assert( ExprUseXList(pExpr) )((void) (0));
115974 assert( pExpr->x.pList->nExpr==2 )((void) (0));
115975 sqlite3WalkExpr(pWalker, pExpr->pLeft);
115976 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr,
115977 pExpr->x.pList->a[1].pExpr);
115978 return WRC_Prune1;
115979
115980 /* Virtual tables are allowed to use constraints like x=NULL. So
115981 ** a term of the form x=y does not prove that y is not null if x
115982 ** is the column of a virtual table */
115983 case TK_EQ54:
115984 case TK_NE53:
115985 case TK_LT57:
115986 case TK_LE56:
115987 case TK_GT55:
115988 case TK_GE58: {
115989 Expr *pLeft = pExpr->pLeft;
115990 Expr *pRight = pExpr->pRight;
115991 testcase( pExpr->op==TK_EQ );
115992 testcase( pExpr->op==TK_NE );
115993 testcase( pExpr->op==TK_LT );
115994 testcase( pExpr->op==TK_LE );
115995 testcase( pExpr->op==TK_GT );
115996 testcase( pExpr->op==TK_GE );
115997 /* The y.pTab=0 assignment in wherecode.c always happens after the
115998 ** impliesNotNullRow() test */
115999 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) )((void) (0));
116000 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) )((void) (0));
116001 if( (pLeft->op==TK_COLUMN168
116002 && ALWAYS(pLeft->y.pTab!=0)(pLeft->y.pTab!=0)
116003 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->eTabType==1))
116004 || (pRight->op==TK_COLUMN168
116005 && ALWAYS(pRight->y.pTab!=0)(pRight->y.pTab!=0)
116006 && IsVirtual(pRight->y.pTab)((pRight->y.pTab)->eTabType==1))
116007 ){
116008 return WRC_Prune1;
116009 }
116010 /* no break */ deliberate_fall_through__attribute__((fallthrough));
116011 }
116012 default:
116013 return WRC_Continue0;
116014 }
116015}
116016
116017/*
116018** Return true (non-zero) if expression p can only be true if at least
116019** one column of table iTab is non-null. In other words, return true
116020** if expression p will always be NULL or false if every column of iTab
116021** is NULL.
116022**
116023** False negatives are acceptable. In other words, it is ok to return
116024** zero even if expression p will never be true of every column of iTab
116025** is NULL. A false negative is merely a missed optimization opportunity.
116026**
116027** False positives are not allowed, however. A false positive may result
116028** in an incorrect answer.
116029**
116030** Terms of p that are marked with EP_OuterON (and hence that come from
116031** the ON or USING clauses of OUTER JOINS) are excluded from the analysis.
116032**
116033** This routine is used to check if a LEFT JOIN can be converted into
116034** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
116035** clause requires that some column of the right table of the LEFT JOIN
116036** be non-NULL, then the LEFT JOIN can be safely converted into an
116037** ordinary join.
116038*/
116039SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab, int isRJ){
116040 Walker w;
116041 p = sqlite3ExprSkipCollateAndLikely(p);
116042 if( p==0 ) return 0;
116043 if( p->op==TK_NOTNULL52 ){
116044 p = p->pLeft;
116045 }else{
116046 while( p->op==TK_AND44 ){
116047 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1;
116048 p = p->pRight;
116049 }
116050 }
116051 w.xExprCallback = impliesNotNullRow;
116052 w.xSelectCallback = 0;
116053 w.xSelectCallback2 = 0;
116054 w.eCode = 0;
116055 w.mWFlags = isRJ!=0;
116056 w.u.iCur = iTab;
116057 sqlite3WalkExpr(&w, p);
116058 return w.eCode;
116059}
116060
116061/*
116062** An instance of the following structure is used by the tree walker
116063** to determine if an expression can be evaluated by reference to the
116064** index only, without having to do a search for the corresponding
116065** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
116066** is the cursor for the table.
116067*/
116068struct IdxCover {
116069 Index *pIdx; /* The index to be tested for coverage */
116070 int iCur; /* Cursor number for the table corresponding to the index */
116071};
116072
116073/*
116074** Check to see if there are references to columns in table
116075** pWalker->u.pIdxCover->iCur can be satisfied using the index
116076** pWalker->u.pIdxCover->pIdx.
116077*/
116078static int exprIdxCover(Walker *pWalker, Expr *pExpr){
116079 if( pExpr->op==TK_COLUMN168
116080 && pExpr->iTable==pWalker->u.pIdxCover->iCur
116081 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
116082 ){
116083 pWalker->eCode = 1;
116084 return WRC_Abort2;
116085 }
116086 return WRC_Continue0;
116087}
116088
116089/*
116090** Determine if an index pIdx on table with cursor iCur contains will
116091** the expression pExpr. Return true if the index does cover the
116092** expression and false if the pExpr expression references table columns
116093** that are not found in the index pIdx.
116094**
116095** An index covering an expression means that the expression can be
116096** evaluated using only the index and without having to lookup the
116097** corresponding table entry.
116098*/
116099SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(
116100 Expr *pExpr, /* The index to be tested */
116101 int iCur, /* The cursor number for the corresponding table */
116102 Index *pIdx /* The index that might be used for coverage */
116103){
116104 Walker w;
116105 struct IdxCover xcov;
116106 memset(&w, 0, sizeof(w));
116107 xcov.iCur = iCur;
116108 xcov.pIdx = pIdx;
116109 w.xExprCallback = exprIdxCover;
116110 w.u.pIdxCover = &xcov;
116111 sqlite3WalkExpr(&w, pExpr);
116112 return !w.eCode;
116113}
116114
116115
116116/* Structure used to pass information throughout the Walker in order to
116117** implement sqlite3ReferencesSrcList().
116118*/
116119struct RefSrcList {
116120 sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */
116121 SrcList *pRef; /* Looking for references to these tables */
116122 i64 nExclude; /* Number of tables to exclude from the search */
116123 int *aiExclude; /* Cursor IDs for tables to exclude from the search */
116124};
116125
116126/*
116127** Walker SELECT callbacks for sqlite3ReferencesSrcList().
116128**
116129** When entering a new subquery on the pExpr argument, add all FROM clause
116130** entries for that subquery to the exclude list.
116131**
116132** When leaving the subquery, remove those entries from the exclude list.
116133*/
116134static int selectRefEnter(Walker *pWalker, Select *pSelect){
116135 struct RefSrcList *p = pWalker->u.pRefSrcList;
116136 SrcList *pSrc = pSelect->pSrc;
116137 i64 i, j;
116138 int *piNew;
116139 if( pSrc->nSrc==0 ) return WRC_Continue0;
116140 j = p->nExclude;
116141 p->nExclude += pSrc->nSrc;
116142 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
116143 if( piNew==0 ){
116144 p->nExclude = 0;
116145 return WRC_Abort2;
116146 }else{
116147 p->aiExclude = piNew;
116148 }
116149 for(i=0; i<pSrc->nSrc; i++, j++){
116150 p->aiExclude[j] = pSrc->a[i].iCursor;
116151 }
116152 return WRC_Continue0;
116153}
116154static void selectRefLeave(Walker *pWalker, Select *pSelect){
116155 struct RefSrcList *p = pWalker->u.pRefSrcList;
116156 SrcList *pSrc = pSelect->pSrc;
116157 if( p->nExclude ){
116158 assert( p->nExclude>=pSrc->nSrc )((void) (0));
116159 p->nExclude -= pSrc->nSrc;
116160 }
116161}
116162
116163/* This is the Walker EXPR callback for sqlite3ReferencesSrcList().
116164**
116165** Set the 0x01 bit of pWalker->eCode if there is a reference to any
116166** of the tables shown in RefSrcList.pRef.
116167**
116168** Set the 0x02 bit of pWalker->eCode if there is a reference to a
116169** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude.
116170*/
116171static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){
116172 if( pExpr->op==TK_COLUMN168
116173 || pExpr->op==TK_AGG_COLUMN170
116174 ){
116175 int i;
116176 struct RefSrcList *p = pWalker->u.pRefSrcList;
116177 SrcList *pSrc = p->pRef;
116178 int nSrc = pSrc ? pSrc->nSrc : 0;
116179 for(i=0; i<nSrc; i++){
116180 if( pExpr->iTable==pSrc->a[i].iCursor ){
116181 pWalker->eCode |= 1;
116182 return WRC_Continue0;
116183 }
116184 }
116185 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
116186 if( i>=p->nExclude ){
116187 pWalker->eCode |= 2;
116188 }
116189 }
116190 return WRC_Continue0;
116191}
116192
116193/*
116194** Check to see if pExpr references any tables in pSrcList.
116195** Possible return values:
116196**
116197** 1 pExpr does references a table in pSrcList.
116198**
116199** 0 pExpr references some table that is not defined in either
116200** pSrcList or in subqueries of pExpr itself.
116201**
116202** -1 pExpr only references no tables at all, or it only
116203** references tables defined in subqueries of pExpr itself.
116204**
116205** As currently used, pExpr is always an aggregate function call. That
116206** fact is exploited for efficiency.
116207*/
116208SQLITE_PRIVATEstatic int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){
116209 Walker w;
116210 struct RefSrcList x;
116211 assert( pParse->db!=0 )((void) (0));
116212 memset(&w, 0, sizeof(w));
116213 memset(&x, 0, sizeof(x));
116214 w.xExprCallback = exprRefToSrcList;
116215 w.xSelectCallback = selectRefEnter;
116216 w.xSelectCallback2 = selectRefLeave;
116217 w.u.pRefSrcList = &x;
116218 x.db = pParse->db;
116219 x.pRef = pSrcList;
116220 assert( pExpr->op==TK_AGG_FUNCTION )((void) (0));
116221 assert( ExprUseXList(pExpr) )((void) (0));
116222 sqlite3WalkExprList(&w, pExpr->x.pList);
116223 if( pExpr->pLeft ){
116224 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
116225 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
116226 assert( pExpr->pLeft->x.pList!=0 )((void) (0));
116227 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList);
116228 }
116229#ifndef SQLITE_OMIT_WINDOWFUNC
116230 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
116231 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
116232 }
116233#endif
116234 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
116235 if( w.eCode & 0x01 ){
116236 return 1;
116237 }else if( w.eCode ){
116238 return 0;
116239 }else{
116240 return -1;
116241 }
116242}
116243
116244/*
116245** This is a Walker expression node callback.
116246**
116247** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
116248** object that is referenced does not refer directly to the Expr. If
116249** it does, make a copy. This is done because the pExpr argument is
116250** subject to change.
116251**
116252** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete()
116253** which builds on the sqlite3ParserAddCleanup() mechanism.
116254*/
116255static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){
116256 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced))(!(((pExpr)->flags&(0x010000|0x004000))!=0))
116257 && pExpr->pAggInfo!=0
116258 ){
116259 AggInfo *pAggInfo = pExpr->pAggInfo;
116260 int iAgg = pExpr->iAgg;
116261 Parse *pParse = pWalker->pParse;
116262 sqlite3 *db = pParse->db;
116263 assert( iAgg>=0 )((void) (0));
116264 if( pExpr->op!=TK_AGG_FUNCTION169 ){
116265 if( iAgg<pAggInfo->nColumn
116266 && pAggInfo->aCol[iAgg].pCExpr==pExpr
116267 ){
116268 pExpr = sqlite3ExprDup(db, pExpr, 0);
116269 if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){
116270 pAggInfo->aCol[iAgg].pCExpr = pExpr;
116271 }
116272 }
116273 }else{
116274 assert( pExpr->op==TK_AGG_FUNCTION )((void) (0));
116275 if( ALWAYS(iAgg<pAggInfo->nFunc)(iAgg<pAggInfo->nFunc)
116276 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
116277 ){
116278 pExpr = sqlite3ExprDup(db, pExpr, 0);
116279 if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){
116280 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
116281 }
116282 }
116283 }
116284 }
116285 return WRC_Continue0;
116286}
116287
116288/*
116289** Initialize a Walker object so that will persist AggInfo entries referenced
116290** by the tree that is walked.
116291*/
116292SQLITE_PRIVATEstatic void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){
116293 memset(pWalker, 0, sizeof(*pWalker));
116294 pWalker->pParse = pParse;
116295 pWalker->xExprCallback = agginfoPersistExprCb;
116296 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
116297}
116298
116299/*
116300** Add a new element to the pAggInfo->aCol[] array. Return the index of
116301** the new element. Return a negative number if malloc fails.
116302*/
116303static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
116304 int i;
116305 pInfo->aCol = sqlite3ArrayAllocate(
116306 db,
116307 pInfo->aCol,
116308 sizeof(pInfo->aCol[0]),
116309 &pInfo->nColumn,
116310 &i
116311 );
116312 return i;
116313}
116314
116315/*
116316** Add a new element to the pAggInfo->aFunc[] array. Return the index of
116317** the new element. Return a negative number if malloc fails.
116318*/
116319static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
116320 int i;
116321 pInfo->aFunc = sqlite3ArrayAllocate(
116322 db,
116323 pInfo->aFunc,
116324 sizeof(pInfo->aFunc[0]),
116325 &pInfo->nFunc,
116326 &i
116327 );
116328 return i;
116329}
116330
116331/*
116332** Search the AggInfo object for an aCol[] entry that has iTable and iColumn.
116333** Return the index in aCol[] of the entry that describes that column.
116334**
116335** If no prior entry is found, create a new one and return -1. The
116336** new column will have an index of pAggInfo->nColumn-1.
116337*/
116338static void findOrCreateAggInfoColumn(
116339 Parse *pParse, /* Parsing context */
116340 AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */
116341 Expr *pExpr /* Expr describing the column to find or insert */
116342){
116343 struct AggInfo_col *pCol;
116344 int k;
116345
116346 assert( pAggInfo->iFirstReg==0 )((void) (0));
116347 pCol = pAggInfo->aCol;
116348 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
116349 if( pCol->pCExpr==pExpr ) return;
116350 if( pCol->iTable==pExpr->iTable
116351 && pCol->iColumn==pExpr->iColumn
116352 && pExpr->op!=TK_IF_NULL_ROW179
116353 ){
116354 goto fix_up_expr;
116355 }
116356 }
116357 k = addAggInfoColumn(pParse->db, pAggInfo);
116358 if( k<0 ){
116359 /* OOM on resize */
116360 assert( pParse->db->mallocFailed )((void) (0));
116361 return;
116362 }
116363 pCol = &pAggInfo->aCol[k];
116364 assert( ExprUseYTab(pExpr) )((void) (0));
116365 pCol->pTab = pExpr->y.pTab;
116366 pCol->iTable = pExpr->iTable;
116367 pCol->iColumn = pExpr->iColumn;
116368 pCol->iSorterColumn = -1;
116369 pCol->pCExpr = pExpr;
116370 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW179 ){
116371 int j, n;
116372 ExprList *pGB = pAggInfo->pGroupBy;
116373 struct ExprList_item *pTerm = pGB->a;
116374 n = pGB->nExpr;
116375 for(j=0; j<n; j++, pTerm++){
116376 Expr *pE = pTerm->pExpr;
116377 if( pE->op==TK_COLUMN168
116378 && pE->iTable==pExpr->iTable
116379 && pE->iColumn==pExpr->iColumn
116380 ){
116381 pCol->iSorterColumn = j;
116382 break;
116383 }
116384 }
116385 }
116386 if( pCol->iSorterColumn<0 ){
116387 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
116388 }
116389fix_up_expr:
116390 ExprSetVVAProperty(pExpr, EP_NoReduce);
116391 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo )((void) (0));
116392 pExpr->pAggInfo = pAggInfo;
116393 if( pExpr->op==TK_COLUMN168 ){
116394 pExpr->op = TK_AGG_COLUMN170;
116395 }
116396 pExpr->iAgg = (i16)k;
116397}
116398
116399/*
116400** This is the xExprCallback for a tree walker. It is used to
116401** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
116402** for additional information.
116403*/
116404static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
116405 int i;
116406 NameContext *pNC = pWalker->u.pNC;
116407 Parse *pParse = pNC->pParse;
116408 SrcList *pSrcList = pNC->pSrcList;
116409 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
116410
116411 assert( pNC->ncFlags & NC_UAggInfo )((void) (0));
116412 assert( pAggInfo->iFirstReg==0 )((void) (0));
116413 switch( pExpr->op ){
116414 default: {
116415 IndexedExpr *pIEpr;
116416 Expr tmp;
116417 assert( pParse->iSelfTab==0 )((void) (0));
116418 if( (pNC->ncFlags & NC_InAggFunc0x020000)==0 ) break;
116419 if( pParse->pIdxEpr==0 ) break;
116420 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
116421 int iDataCur = pIEpr->iDataCur;
116422 if( iDataCur<0 ) continue;
116423 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
116424 }
116425 if( pIEpr==0 ) break;
116426 if( NEVER(!ExprUseYTab(pExpr))(!(((pExpr)->flags&(0x1000000|0x2000000))==0)) ) break;
116427 for(i=0; i<pSrcList->nSrc; i++){
116428 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
116429 }
116430 if( i>=pSrcList->nSrc ) break;
116431 if( NEVER(pExpr->pAggInfo!=0)(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
116432 if( pParse->nErr ){ return WRC_Abort2; }
116433
116434 /* If we reach this point, it means that expression pExpr can be
116435 ** translated into a reference to an index column as described by
116436 ** pIEpr.
116437 */
116438 memset(&tmp, 0, sizeof(tmp));
116439 tmp.op = TK_AGG_COLUMN170;
116440 tmp.iTable = pIEpr->iIdxCur;
116441 tmp.iColumn = pIEpr->iIdxCol;
116442 findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp);
116443 if( pParse->nErr ){ return WRC_Abort2; }
116444 assert( pAggInfo->aCol!=0 )((void) (0));
116445 assert( tmp.iAgg<pAggInfo->nColumn )((void) (0));
116446 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
116447 pExpr->pAggInfo = pAggInfo;
116448 pExpr->iAgg = tmp.iAgg;
116449 return WRC_Prune1;
116450 }
116451 case TK_IF_NULL_ROW179:
116452 case TK_AGG_COLUMN170:
116453 case TK_COLUMN168: {
116454 testcase( pExpr->op==TK_AGG_COLUMN );
116455 testcase( pExpr->op==TK_COLUMN );
116456 testcase( pExpr->op==TK_IF_NULL_ROW );
116457 /* Check to see if the column is in one of the tables in the FROM
116458 ** clause of the aggregate query */
116459 if( ALWAYS(pSrcList!=0)(pSrcList!=0) ){
116460 SrcItem *pItem = pSrcList->a;
116461 for(i=0; i<pSrcList->nSrc; i++, pItem++){
116462 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
116463 if( pExpr->iTable==pItem->iCursor ){
116464 findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr);
116465 break;
116466 } /* endif pExpr->iTable==pItem->iCursor */
116467 } /* end loop over pSrcList */
116468 }
116469 return WRC_Continue0;
116470 }
116471 case TK_AGG_FUNCTION169: {
116472 if( (pNC->ncFlags & NC_InAggFunc0x020000)==0
116473 && pWalker->walkerDepth==pExpr->op2
116474 && pExpr->pAggInfo==0
116475 ){
116476 /* Check to see if pExpr is a duplicate of another aggregate
116477 ** function that is already in the pAggInfo structure
116478 */
116479 struct AggInfo_func *pItem = pAggInfo->aFunc;
116480 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
116481 if( NEVER(pItem->pFExpr==pExpr)(pItem->pFExpr==pExpr) ) break;
116482 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
116483 break;
116484 }
116485 }
116486 if( i>=pAggInfo->nFunc ){
116487 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
116488 */
116489 u8 enc = ENC(pParse->db)((pParse->db)->enc);
116490 i = addAggInfoFunc(pParse->db, pAggInfo);
116491 if( i>=0 ){
116492 int nArg;
116493 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
116494 pItem = &pAggInfo->aFunc[i];
116495 pItem->pFExpr = pExpr;
116496 assert( ExprUseUToken(pExpr) )((void) (0));
116497 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
116498 pItem->pFunc = sqlite3FindFunction(pParse->db,
116499 pExpr->u.zToken, nArg, enc, 0);
116500 assert( pItem->bOBUnique==0 )((void) (0));
116501 if( pExpr->pLeft
116502 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)==0
116503 ){
116504 /* The NEEDCOLL test above causes any ORDER BY clause on
116505 ** aggregate min() or max() to be ignored. */
116506 ExprList *pOBList;
116507 assert( nArg>0 )((void) (0));
116508 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
116509 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
116510 pItem->iOBTab = pParse->nTab++;
116511 pOBList = pExpr->pLeft->x.pList;
116512 assert( pOBList->nExpr>0 )((void) (0));
116513 assert( pItem->bOBUnique==0 )((void) (0));
116514 if( pOBList->nExpr==1
116515 && nArg==1
116516 && sqlite3ExprCompare(0,pOBList->a[0].pExpr,
116517 pExpr->x.pList->a[0].pExpr,0)==0
116518 ){
116519 pItem->bOBPayload = 0;
116520 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct)(((pExpr)->flags&(0x000004))!=0);
116521 }else{
116522 pItem->bOBPayload = 1;
116523 }
116524 pItem->bUseSubtype =
116525 (pItem->pFunc->funcFlags & SQLITE_SUBTYPE0x000100000)!=0;
116526 }else{
116527 pItem->iOBTab = -1;
116528 }
116529 if( ExprHasProperty(pExpr, EP_Distinct)(((pExpr)->flags&(0x000004))!=0) && !pItem->bOBUnique ){
116530 pItem->iDistinct = pParse->nTab++;
116531 }else{
116532 pItem->iDistinct = -1;
116533 }
116534 }
116535 }
116536 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
116537 */
116538 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
116539 ExprSetVVAProperty(pExpr, EP_NoReduce);
116540 pExpr->iAgg = (i16)i;
116541 pExpr->pAggInfo = pAggInfo;
116542 return WRC_Prune1;
116543 }else{
116544 return WRC_Continue0;
116545 }
116546 }
116547 }
116548 return WRC_Continue0;
116549}
116550
116551/*
116552** Analyze the pExpr expression looking for aggregate functions and
116553** for variables that need to be added to AggInfo object that pNC->pAggInfo
116554** points to. Additional entries are made on the AggInfo object as
116555** necessary.
116556**
116557** This routine should only be called after the expression has been
116558** analyzed by sqlite3ResolveExprNames().
116559*/
116560SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
116561 Walker w;
116562 w.xExprCallback = analyzeAggregate;
116563 w.xSelectCallback = sqlite3WalkerDepthIncrease;
116564 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
116565 w.walkerDepth = 0;
116566 w.u.pNC = pNC;
116567 w.pParse = 0;
116568 assert( pNC->pSrcList!=0 )((void) (0));
116569 sqlite3WalkExpr(&w, pExpr);
116570}
116571
116572/*
116573** Call sqlite3ExprAnalyzeAggregates() for every expression in an
116574** expression list. Return the number of errors.
116575**
116576** If an error is found, the analysis is cut short.
116577*/
116578SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
116579 struct ExprList_item *pItem;
116580 int i;
116581 if( pList ){
116582 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
116583 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
116584 }
116585 }
116586}
116587
116588/*
116589** Allocate a single new register for use to hold some intermediate result.
116590*/
116591SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse *pParse){
116592 if( pParse->nTempReg==0 ){
116593 return ++pParse->nMem;
116594 }
116595 return pParse->aTempReg[--pParse->nTempReg];
116596}
116597
116598/*
116599** Deallocate a register, making available for reuse for some other
116600** purpose.
116601*/
116602SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
116603 if( iReg ){
116604 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
116605 if( pParse->nTempReg<ArraySize(pParse->aTempReg)((int)(sizeof(pParse->aTempReg)/sizeof(pParse->aTempReg
[0])))
){
116606 pParse->aTempReg[pParse->nTempReg++] = iReg;
116607 }
116608 }
116609}
116610
116611/*
116612** Allocate or deallocate a block of nReg consecutive registers.
116613*/
116614SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse *pParse, int nReg){
116615 int i, n;
116616 if( nReg==1 ) return sqlite3GetTempReg(pParse);
116617 i = pParse->iRangeReg;
116618 n = pParse->nRangeReg;
116619 if( nReg<=n ){
116620 pParse->iRangeReg += nReg;
116621 pParse->nRangeReg -= nReg;
116622 }else{
116623 i = pParse->nMem+1;
116624 pParse->nMem += nReg;
116625 }
116626 return i;
116627}
116628SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
116629 if( nReg==1 ){
116630 sqlite3ReleaseTempReg(pParse, iReg);
116631 return;
116632 }
116633 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
116634 if( nReg>pParse->nRangeReg ){
116635 pParse->nRangeReg = nReg;
116636 pParse->iRangeReg = iReg;
116637 }
116638}
116639
116640/*
116641** Mark all temporary registers as being unavailable for reuse.
116642**
116643** Always invoke this procedure after coding a subroutine or co-routine
116644** that might be invoked from other parts of the code, to ensure that
116645** the sub/co-routine does not use registers in common with the code that
116646** invokes the sub/co-routine.
116647*/
116648SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse *pParse){
116649 pParse->nTempReg = 0;
116650 pParse->nRangeReg = 0;
116651}
116652
116653/*
116654** Make sure sufficient registers have been allocated so that
116655** iReg is a valid register number.
116656*/
116657SQLITE_PRIVATEstatic void sqlite3TouchRegister(Parse *pParse, int iReg){
116658 if( pParse->nMem<iReg ) pParse->nMem = iReg;
116659}
116660
116661#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG)
116662/*
116663** Return the latest reusable register in the set of all registers.
116664** The value returned is no less than iMin. If any register iMin or
116665** greater is in permanent use, then return one more than that last
116666** permanent register.
116667*/
116668SQLITE_PRIVATEstatic int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){
116669 const ExprList *pList = pParse->pConstExpr;
116670 if( pList ){
116671 int i;
116672 for(i=0; i<pList->nExpr; i++){
116673 if( pList->a[i].u.iConstExprReg>=iMin ){
116674 iMin = pList->a[i].u.iConstExprReg + 1;
116675 }
116676 }
116677 }
116678 pParse->nTempReg = 0;
116679 pParse->nRangeReg = 0;
116680 return iMin;
116681}
116682#endif /* SQLITE_ENABLE_STAT4 || SQLITE_DEBUG */
116683
116684/*
116685** Validate that no temporary register falls within the range of
116686** iFirst..iLast, inclusive. This routine is only call from within assert()
116687** statements.
116688*/
116689#ifdef SQLITE_DEBUG
116690SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
116691 int i;
116692 if( pParse->nRangeReg>0
116693 && pParse->iRangeReg+pParse->nRangeReg > iFirst
116694 && pParse->iRangeReg <= iLast
116695 ){
116696 return 0;
116697 }
116698 for(i=0; i<pParse->nTempReg; i++){
116699 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
116700 return 0;
116701 }
116702 }
116703 if( pParse->pConstExpr ){
116704 ExprList *pList = pParse->pConstExpr;
116705 for(i=0; i<pList->nExpr; i++){
116706 int iReg = pList->a[i].u.iConstExprReg;
116707 if( iReg==0 ) continue;
116708 if( iReg>=iFirst && iReg<=iLast ) return 0;
116709 }
116710 }
116711 return 1;
116712}
116713#endif /* SQLITE_DEBUG */
116714
116715/************** End of expr.c ************************************************/
116716/************** Begin file alter.c *******************************************/
116717/*
116718** 2005 February 15
116719**
116720** The author disclaims copyright to this source code. In place of
116721** a legal notice, here is a blessing:
116722**
116723** May you do good and not evil.
116724** May you find forgiveness for yourself and forgive others.
116725** May you share freely, never taking more than you give.
116726**
116727*************************************************************************
116728** This file contains C code routines that used to generate VDBE code
116729** that implements the ALTER TABLE command.
116730*/
116731/* #include "sqliteInt.h" */
116732
116733/*
116734** The code in this file only exists if we are not omitting the
116735** ALTER TABLE logic from the build.
116736*/
116737#ifndef SQLITE_OMIT_ALTERTABLE
116738
116739/*
116740** Parameter zName is the name of a table that is about to be altered
116741** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
116742** If the table is a system table, this function leaves an error message
116743** in pParse->zErr (system tables may not be altered) and returns non-zero.
116744**
116745** Or, if zName is not a system table, zero is returned.
116746*/
116747static int isAlterableTable(Parse *pParse, Table *pTab){
116748 if( 0==sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)
116749#ifndef SQLITE_OMIT_VIRTUALTABLE
116750 || (pTab->tabFlags & TF_Eponymous0x00008000)!=0
116751 || ( (pTab->tabFlags & TF_Shadow0x00001000)!=0
116752 && sqlite3ReadOnlyShadowTables(pParse->db)
116753 )
116754#endif
116755 ){
116756 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
116757 return 1;
116758 }
116759 return 0;
116760}
116761
116762/*
116763** Generate code to verify that the schemas of database zDb and, if
116764** bTemp is not true, database "temp", can still be parsed. This is
116765** called at the end of the generation of an ALTER TABLE ... RENAME ...
116766** statement to ensure that the operation has not rendered any schema
116767** objects unusable.
116768*/
116769static void renameTestSchema(
116770 Parse *pParse, /* Parse context */
116771 const char *zDb, /* Name of db to verify schema of */
116772 int bTemp, /* True if this is the temp db */
116773 const char *zWhen, /* "when" part of error message */
116774 int bNoDQS /* Do not allow DQS in the schema */
116775){
116776 pParse->colNamesSet = 1;
116777 sqlite3NestedParse(pParse,
116778 "SELECT 1 "
116779 "FROM \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " "
116780 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
116781 " AND sql NOT LIKE 'create virtual%%'"
116782 " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ",
116783 zDb,
116784 zDb, bTemp, zWhen, bNoDQS
116785 );
116786
116787 if( bTemp==0 ){
116788 sqlite3NestedParse(pParse,
116789 "SELECT 1 "
116790 "FROM temp." LEGACY_SCHEMA_TABLE"sqlite_master" " "
116791 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
116792 " AND sql NOT LIKE 'create virtual%%'"
116793 " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ",
116794 zDb, zWhen, bNoDQS
116795 );
116796 }
116797}
116798
116799/*
116800** Generate VM code to replace any double-quoted strings (but not double-quoted
116801** identifiers) within the "sql" column of the sqlite_schema table in
116802** database zDb with their single-quoted equivalents. If argument bTemp is
116803** not true, similarly update all SQL statements in the sqlite_schema table
116804** of the temp db.
116805*/
116806static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){
116807 sqlite3NestedParse(pParse,
116808 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master"
116809 " SET sql = sqlite_rename_quotefix(%Q, sql)"
116810 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
116811 " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb
116812 );
116813 if( bTemp==0 ){
116814 sqlite3NestedParse(pParse,
116815 "UPDATE temp." LEGACY_SCHEMA_TABLE"sqlite_master"
116816 " SET sql = sqlite_rename_quotefix('temp', sql)"
116817 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
116818 " AND sql NOT LIKE 'create virtual%%'"
116819 );
116820 }
116821}
116822
116823/*
116824** Generate code to reload the schema for database iDb. And, if iDb!=1, for
116825** the temp database as well.
116826*/
116827static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){
116828 Vdbe *v = pParse->pVdbe;
116829 if( v ){
116830 sqlite3ChangeCookie(pParse, iDb);
116831 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
116832 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
116833 }
116834}
116835
116836/*
116837** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
116838** command.
116839*/
116840SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(
116841 Parse *pParse, /* Parser context. */
116842 SrcList *pSrc, /* The table to rename. */
116843 Token *pName /* The new table name. */
116844){
116845 int iDb; /* Database that contains the table */
116846 char *zDb; /* Name of database iDb */
116847 Table *pTab; /* Table being renamed */
116848 char *zName = 0; /* NULL-terminated version of pName */
116849 sqlite3 *db = pParse->db; /* Database connection */
116850 int nTabName; /* Number of UTF-8 characters in zTabName */
116851 const char *zTabName; /* Original name of the table */
116852 Vdbe *v;
116853 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
116854
116855 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_rename_table;
116856 assert( pSrc->nSrc==1 )((void) (0));
116857 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
116858
116859 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116860 if( !pTab ) goto exit_rename_table;
116861 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116862 zDb = db->aDb[iDb].zDbSName;
116863
116864 /* Get a NULL terminated version of the new table name. */
116865 zName = sqlite3NameFromToken(db, pName);
116866 if( !zName ) goto exit_rename_table;
116867
116868 /* Check that a table or index named 'zName' does not already exist
116869 ** in database iDb. If so, this is an error.
116870 */
116871 if( sqlite3FindTable(db, zName, zDb)
116872 || sqlite3FindIndex(db, zName, zDb)
116873 || sqlite3IsShadowTableOf(db, pTab, zName)
116874 ){
116875 sqlite3ErrorMsg(pParse,
116876 "there is already another table or index with this name: %s", zName);
116877 goto exit_rename_table;
116878 }
116879
116880 /* Make sure it is not a system table being altered, or a reserved name
116881 ** that the table is being renamed to.
116882 */
116883 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
116884 goto exit_rename_table;
116885 }
116886 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
116887 goto exit_rename_table;
116888 }
116889
116890#ifndef SQLITE_OMIT_VIEW
116891 if( IsView(pTab)((pTab)->eTabType==2) ){
116892 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
116893 goto exit_rename_table;
116894 }
116895#endif
116896
116897#ifndef SQLITE_OMIT_AUTHORIZATION
116898 /* Invoke the authorization callback. */
116899 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
116900 goto exit_rename_table;
116901 }
116902#endif
116903
116904#ifndef SQLITE_OMIT_VIRTUALTABLE
116905 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
116906 goto exit_rename_table;
116907 }
116908 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
116909 pVTab = sqlite3GetVTable(db, pTab);
116910 if( pVTab->pVtab->pModule->xRename==0 ){
116911 pVTab = 0;
116912 }
116913 }
116914#endif
116915
116916 /* Begin a transaction for database iDb. Then modify the schema cookie
116917 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
116918 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
116919 ** nested SQL may raise an exception. */
116920 v = sqlite3GetVdbe(pParse);
116921 if( v==0 ){
116922 goto exit_rename_table;
116923 }
116924 sqlite3MayAbort(pParse);
116925
116926 /* figure out how many UTF-8 characters are in zName */
116927 zTabName = pTab->zName;
116928 nTabName = sqlite3Utf8CharLen(zTabName, -1);
116929
116930 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
116931 ** the schema to use the new table name. */
116932 sqlite3NestedParse(pParse,
116933 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
116934 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
116935 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
116936 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
116937 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
116938 );
116939
116940 /* Update the tbl_name and name columns of the sqlite_schema table
116941 ** as required. */
116942 sqlite3NestedParse(pParse,
116943 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
116944 "tbl_name = %Q, "
116945 "name = CASE "
116946 "WHEN type='table' THEN %Q "
116947 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
116948 " AND type='index' THEN "
116949 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
116950 "ELSE name END "
116951 "WHERE tbl_name=%Q COLLATE nocase AND "
116952 "(type='table' OR type='index' OR type='trigger');",
116953 zDb,
116954 zName, zName, zName,
116955 nTabName, zTabName
116956 );
116957
116958#ifndef SQLITE_OMIT_AUTOINCREMENT
116959 /* If the sqlite_sequence table exists in this database, then update
116960 ** it with the new table name.
116961 */
116962 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
116963 sqlite3NestedParse(pParse,
116964 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
116965 zDb, zName, pTab->zName);
116966 }
116967#endif
116968
116969 /* If the table being renamed is not itself part of the temp database,
116970 ** edit view and trigger definitions within the temp database
116971 ** as required. */
116972 if( iDb!=1 ){
116973 sqlite3NestedParse(pParse,
116974 "UPDATE sqlite_temp_schema SET "
116975 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
116976 "tbl_name = "
116977 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
116978 " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0) "
116979 "THEN %Q ELSE tbl_name END "
116980 "WHERE type IN ('view', 'trigger')"
116981 , zDb, zTabName, zName, zTabName, zDb, zName);
116982 }
116983
116984 /* If this is a virtual table, invoke the xRename() function if
116985 ** one is defined. The xRename() callback will modify the names
116986 ** of any resources used by the v-table implementation (including other
116987 ** SQLite tables) that are identified by the name of the virtual table.
116988 */
116989#ifndef SQLITE_OMIT_VIRTUALTABLE
116990 if( pVTab ){
116991 int i = ++pParse->nMem;
116992 sqlite3VdbeLoadString(v, i, zName);
116993 sqlite3VdbeAddOp4(v, OP_VRename177, i, 0, 0,(const char*)pVTab, P4_VTAB(-11));
116994 }
116995#endif
116996
116997 renameReloadSchema(pParse, iDb, INITFLAG_AlterRename0x0001);
116998 renameTestSchema(pParse, zDb, iDb==1, "after rename", 0);
116999
117000exit_rename_table:
117001 sqlite3SrcListDelete(db, pSrc);
117002 sqlite3DbFree(db, zName);
117003}
117004
117005/*
117006** Write code that will raise an error if the table described by
117007** zDb and zTab is not empty.
117008*/
117009static void sqlite3ErrorIfNotEmpty(
117010 Parse *pParse, /* Parsing context */
117011 const char *zDb, /* Schema holding the table */
117012 const char *zTab, /* Table to check for empty */
117013 const char *zErr /* Error message text */
117014){
117015 sqlite3NestedParse(pParse,
117016 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
117017 zErr, zDb, zTab
117018 );
117019}
117020
117021/*
117022** This function is called after an "ALTER TABLE ... ADD" statement
117023** has been parsed. Argument pColDef contains the text of the new
117024** column definition.
117025**
117026** The Table structure pParse->pNewTable was extended to include
117027** the new column during parsing.
117028*/
117029SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
117030 Table *pNew; /* Copy of pParse->pNewTable */
117031 Table *pTab; /* Table being altered */
117032 int iDb; /* Database number */
117033 const char *zDb; /* Database name */
117034 const char *zTab; /* Table name */
117035 char *zCol; /* Null-terminated column definition */
117036 Column *pCol; /* The new column */
117037 Expr *pDflt; /* Default value for the new column */
117038 sqlite3 *db; /* The database connection; */
117039 Vdbe *v; /* The prepared statement under construction */
117040 int r1; /* Temporary registers */
117041
117042 db = pParse->db;
117043 assert( db->pParse==pParse )((void) (0));
117044 if( pParse->nErr ) return;
117045 assert( db->mallocFailed==0 )((void) (0));
117046 pNew = pParse->pNewTable;
117047 assert( pNew )((void) (0));
117048
117049 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
117050 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
117051 zDb = db->aDb[iDb].zDbSName;
117052 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
117053 pCol = &pNew->aCol[pNew->nCol-1];
117054 pDflt = sqlite3ColumnExpr(pNew, pCol);
117055 pTab = sqlite3FindTable(db, zTab, zDb);
117056 assert( pTab )((void) (0));
117057
117058#ifndef SQLITE_OMIT_AUTHORIZATION
117059 /* Invoke the authorization callback. */
117060 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
117061 return;
117062 }
117063#endif
117064
117065
117066 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
117067 ** If there is a NOT NULL constraint, then the default value for the
117068 ** column must not be NULL.
117069 */
117070 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
117071 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
117072 return;
117073 }
117074 if( pNew->pIndex ){
117075 sqlite3ErrorMsg(pParse,
117076 "Cannot add a UNIQUE column");
117077 return;
117078 }
117079 if( (pCol->colFlags & COLFLAG_GENERATED0x0060)==0 ){
117080 /* If the default value for the new column was specified with a
117081 ** literal NULL, then set pDflt to 0. This simplifies checking
117082 ** for an SQL NULL default below.
117083 */
117084 assert( pDflt==0 || pDflt->op==TK_SPAN )((void) (0));
117085 if( pDflt && pDflt->pLeft->op==TK_NULL122 ){
117086 pDflt = 0;
117087 }
117088 assert( IsOrdinaryTable(pNew) )((void) (0));
117089 if( (db->flags&SQLITE_ForeignKeys0x00004000) && pNew->u.tab.pFKey && pDflt ){
117090 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
117091 "Cannot add a REFERENCES column with non-NULL default value");
117092 }
117093 if( pCol->notNull && !pDflt ){
117094 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
117095 "Cannot add a NOT NULL column with default value NULL");
117096 }
117097
117098
117099 /* Ensure the default expression is something that sqlite3ValueFromExpr()
117100 ** can handle (i.e. not CURRENT_TIME etc.)
117101 */
117102 if( pDflt ){
117103 sqlite3_value *pVal = 0;
117104 int rc;
117105 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF81, SQLITE_AFF_BLOB0x41, &pVal);
117106 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
117107 if( rc!=SQLITE_OK0 ){
117108 assert( db->mallocFailed == 1 )((void) (0));
117109 return;
117110 }
117111 if( !pVal ){
117112 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
117113 "Cannot add a column with non-constant default");
117114 }
117115 sqlite3ValueFree(pVal);
117116 }
117117 }else if( pCol->colFlags & COLFLAG_STORED0x0040 ){
117118 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
117119 }
117120
117121
117122 /* Modify the CREATE TABLE statement. */
117123 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
117124 if( zCol ){
117125 char *zEnd = &zCol[pColDef->n-1];
117126 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)(sqlite3CtypeMap[(unsigned char)(*zEnd)]&0x01)) ){
117127 *zEnd-- = '\0';
117128 }
117129 /* substr() operations on characters, but addColOffset is in bytes. So we
117130 ** have to use printf() to translate between these units: */
117131 assert( IsOrdinaryTable(pTab) )((void) (0));
117132 assert( IsOrdinaryTable(pNew) )((void) (0));
117133 sqlite3NestedParse(pParse,
117134 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
117135 "sql = printf('%%.%ds, ',sql) || %Q"
117136 " || substr(sql,1+length(printf('%%.%ds',sql))) "
117137 "WHERE type = 'table' AND name = %Q",
117138 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
117139 zTab
117140 );
117141 sqlite3DbFree(db, zCol);
117142 }
117143
117144 v = sqlite3GetVdbe(pParse);
117145 if( v ){
117146 /* Make sure the schema version is at least 3. But do not upgrade
117147 ** from less than 3 to 4, as that will corrupt any preexisting DESC
117148 ** index.
117149 */
117150 r1 = sqlite3GetTempReg(pParse);
117151 sqlite3VdbeAddOp3(v, OP_ReadCookie99, iDb, r1, BTREE_FILE_FORMAT2);
117152 sqlite3VdbeUsesBtree(v, iDb);
117153 sqlite3VdbeAddOp2(v, OP_AddImm86, r1, -2);
117154 sqlite3VdbeAddOp2(v, OP_IfPos50, r1, sqlite3VdbeCurrentAddr(v)+2);
117155 VdbeCoverage(v);
117156 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_FILE_FORMAT2, 3);
117157 sqlite3ReleaseTempReg(pParse, r1);
117158
117159 /* Reload the table definition */
117160 renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd0x0003);
117161
117162 /* Verify that constraints are still satisfied */
117163 if( pNew->pCheck!=0
117164 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED0x0060)!=0)
117165 || (pTab->tabFlags & TF_Strict0x00010000)!=0
117166 ){
117167 sqlite3NestedParse(pParse,
117168 "SELECT CASE WHEN quick_check GLOB 'CHECK*'"
117169 " THEN raise(ABORT,'CHECK constraint failed')"
117170 " WHEN quick_check GLOB 'non-* value in*'"
117171 " THEN raise(ABORT,'type mismatch on DEFAULT')"
117172 " ELSE raise(ABORT,'NOT NULL constraint failed')"
117173 " END"
117174 " FROM pragma_quick_check(%Q,%Q)"
117175 " WHERE quick_check GLOB 'CHECK*'"
117176 " OR quick_check GLOB 'NULL*'"
117177 " OR quick_check GLOB 'non-* value in*'",
117178 zTab, zDb
117179 );
117180 }
117181 }
117182}
117183
117184/*
117185** This function is called by the parser after the table-name in
117186** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
117187** pSrc is the full-name of the table being altered.
117188**
117189** This routine makes a (partial) copy of the Table structure
117190** for the table being altered and sets Parse.pNewTable to point
117191** to it. Routines called by the parser as the column definition
117192** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
117193** the copy. The copy of the Table structure is deleted by tokenize.c
117194** after parsing is finished.
117195**
117196** Routine sqlite3AlterFinishAddColumn() will be called to complete
117197** coding the "ALTER TABLE ... ADD" statement.
117198*/
117199SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
117200 Table *pNew;
117201 Table *pTab;
117202 int iDb;
117203 int i;
117204 int nAlloc;
117205 sqlite3 *db = pParse->db;
117206
117207 /* Look up the table being altered. */
117208 assert( pParse->pNewTable==0 )((void) (0));
117209 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
117210 if( db->mallocFailed ) goto exit_begin_add_column;
117211 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
117212 if( !pTab ) goto exit_begin_add_column;
117213
117214#ifndef SQLITE_OMIT_VIRTUALTABLE
117215 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
117216 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
117217 goto exit_begin_add_column;
117218 }
117219#endif
117220
117221 /* Make sure this is not an attempt to ALTER a view. */
117222 if( IsView(pTab)((pTab)->eTabType==2) ){
117223 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
117224 goto exit_begin_add_column;
117225 }
117226 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
117227 goto exit_begin_add_column;
117228 }
117229
117230 sqlite3MayAbort(pParse);
117231 assert( IsOrdinaryTable(pTab) )((void) (0));
117232 assert( pTab->u.tab.addColOffset>0 )((void) (0));
117233 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117234
117235 /* Put a copy of the Table struct in Parse.pNewTable for the
117236 ** sqlite3AddColumn() function and friends to modify. But modify
117237 ** the name by adding an "sqlite_altertab_" prefix. By adding this
117238 ** prefix, we insure that the name will not collide with an existing
117239 ** table because user table are not allowed to have the "sqlite_"
117240 ** prefix on their name.
117241 */
117242 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
117243 if( !pNew ) goto exit_begin_add_column;
117244 pParse->pNewTable = pNew;
117245 pNew->nTabRef = 1;
117246 pNew->nCol = pTab->nCol;
117247 assert( pNew->nCol>0 )((void) (0));
117248 nAlloc = (((pNew->nCol-1)/8)*8)+8;
117249 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 )((void) (0));
117250 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
117251 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
117252 if( !pNew->aCol || !pNew->zName ){
117253 assert( db->mallocFailed )((void) (0));
117254 goto exit_begin_add_column;
117255 }
117256 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
117257 for(i=0; i<pNew->nCol; i++){
117258 Column *pCol = &pNew->aCol[i];
117259 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
117260 pCol->hName = sqlite3StrIHash(pCol->zCnName);
117261 }
117262 assert( IsOrdinaryTable(pNew) )((void) (0));
117263 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
117264 pNew->pSchema = db->aDb[iDb].pSchema;
117265 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
117266 assert( pNew->nTabRef==1 )((void) (0));
117267
117268exit_begin_add_column:
117269 sqlite3SrcListDelete(db, pSrc);
117270 return;
117271}
117272
117273/*
117274** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
117275** command. This function checks if the table is a view or virtual
117276** table (columns of views or virtual tables may not be renamed). If so,
117277** it loads an error message into pParse and returns non-zero.
117278**
117279** Or, if pTab is not a view or virtual table, zero is returned.
117280*/
117281#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
117282static int isRealTable(Parse *pParse, Table *pTab, int bDrop){
117283 const char *zType = 0;
117284#ifndef SQLITE_OMIT_VIEW
117285 if( IsView(pTab)((pTab)->eTabType==2) ){
117286 zType = "view";
117287 }
117288#endif
117289#ifndef SQLITE_OMIT_VIRTUALTABLE
117290 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
117291 zType = "virtual table";
117292 }
117293#endif
117294 if( zType ){
117295 sqlite3ErrorMsg(pParse, "cannot %s %s \"%s\"",
117296 (bDrop ? "drop column from" : "rename columns of"),
117297 zType, pTab->zName
117298 );
117299 return 1;
117300 }
117301 return 0;
117302}
117303#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
117304# define isRealTable(x,y,z) (0)
117305#endif
117306
117307/*
117308** Handles the following parser reduction:
117309**
117310** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
117311*/
117312SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(
117313 Parse *pParse, /* Parsing context */
117314 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
117315 Token *pOld, /* Name of column being changed */
117316 Token *pNew /* New column name */
117317){
117318 sqlite3 *db = pParse->db; /* Database connection */
117319 Table *pTab; /* Table being updated */
117320 int iCol; /* Index of column being renamed */
117321 char *zOld = 0; /* Old column name */
117322 char *zNew = 0; /* New column name */
117323 const char *zDb; /* Name of schema containing the table */
117324 int iSchema; /* Index of the schema */
117325 int bQuote; /* True to quote the new name */
117326
117327 /* Locate the table to be altered */
117328 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
117329 if( !pTab ) goto exit_rename_column;
117330
117331 /* Cannot alter a system table */
117332 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
117333 if( SQLITE_OK0!=isRealTable(pParse, pTab, 0) ) goto exit_rename_column;
117334
117335 /* Which schema holds the table to be altered */
117336 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
117337 assert( iSchema>=0 )((void) (0));
117338 zDb = db->aDb[iSchema].zDbSName;
117339
117340#ifndef SQLITE_OMIT_AUTHORIZATION
117341 /* Invoke the authorization callback. */
117342 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
117343 goto exit_rename_column;
117344 }
117345#endif
117346
117347 /* Make sure the old name really is a column name in the table to be
117348 ** altered. Set iCol to be the index of the column being renamed */
117349 zOld = sqlite3NameFromToken(db, pOld);
117350 if( !zOld ) goto exit_rename_column;
117351 for(iCol=0; iCol<pTab->nCol; iCol++){
117352 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
117353 }
117354 if( iCol==pTab->nCol ){
117355 sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pOld);
117356 goto exit_rename_column;
117357 }
117358
117359 /* Ensure the schema contains no double-quoted strings */
117360 renameTestSchema(pParse, zDb, iSchema==1, "", 0);
117361 renameFixQuotes(pParse, zDb, iSchema==1);
117362
117363 /* Do the rename operation using a recursive UPDATE statement that
117364 ** uses the sqlite_rename_column() SQL function to compute the new
117365 ** CREATE statement text for the sqlite_schema table.
117366 */
117367 sqlite3MayAbort(pParse);
117368 zNew = sqlite3NameFromToken(db, pNew);
117369 if( !zNew ) goto exit_rename_column;
117370 assert( pNew->n>0 )((void) (0));
117371 bQuote = sqlite3Isquote(pNew->z[0])(sqlite3CtypeMap[(unsigned char)(pNew->z[0])]&0x80);
117372 sqlite3NestedParse(pParse,
117373 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
117374 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
117375 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
117376 " AND (type != 'index' OR tbl_name = %Q)",
117377 zDb,
117378 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
117379 pTab->zName
117380 );
117381
117382 sqlite3NestedParse(pParse,
117383 "UPDATE temp." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
117384 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
117385 "WHERE type IN ('trigger', 'view')",
117386 zDb, pTab->zName, iCol, zNew, bQuote
117387 );
117388
117389 /* Drop and reload the database schema. */
117390 renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename0x0001);
117391 renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1);
117392
117393 exit_rename_column:
117394 sqlite3SrcListDelete(db, pSrc);
117395 sqlite3DbFree(db, zOld);
117396 sqlite3DbFree(db, zNew);
117397 return;
117398}
117399
117400/*
117401** Each RenameToken object maps an element of the parse tree into
117402** the token that generated that element. The parse tree element
117403** might be one of:
117404**
117405** * A pointer to an Expr that represents an ID
117406** * The name of a table column in Column.zName
117407**
117408** A list of RenameToken objects can be constructed during parsing.
117409** Each new object is created by sqlite3RenameTokenMap().
117410** As the parse tree is transformed, the sqlite3RenameTokenRemap()
117411** routine is used to keep the mapping current.
117412**
117413** After the parse finishes, renameTokenFind() routine can be used
117414** to look up the actual token value that created some element in
117415** the parse tree.
117416*/
117417struct RenameToken {
117418 const void *p; /* Parse tree element created by token t */
117419 Token t; /* The token that created parse tree element p */
117420 RenameToken *pNext; /* Next is a list of all RenameToken objects */
117421};
117422
117423/*
117424** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
117425** down into the Walker.
117426*/
117427typedef struct RenameCtx RenameCtx;
117428struct RenameCtx {
117429 RenameToken *pList; /* List of tokens to overwrite */
117430 int nList; /* Number of tokens in pList */
117431 int iCol; /* Index of column being renamed */
117432 Table *pTab; /* Table being ALTERed */
117433 const char *zOld; /* Old column name */
117434};
117435
117436#ifdef SQLITE_DEBUG
117437/*
117438** This function is only for debugging. It performs two tasks:
117439**
117440** 1. Checks that pointer pPtr does not already appear in the
117441** rename-token list.
117442**
117443** 2. Dereferences each pointer in the rename-token list.
117444**
117445** The second is most effective when debugging under valgrind or
117446** address-sanitizer or similar. If any of these pointers no longer
117447** point to valid objects, an exception is raised by the memory-checking
117448** tool.
117449**
117450** The point of this is to prevent comparisons of invalid pointer values.
117451** Even though this always seems to work, it is undefined according to the
117452** C standard. Example of undefined comparison:
117453**
117454** sqlite3_free(x);
117455** if( x==y ) ...
117456**
117457** Technically, as x no longer points into a valid object or to the byte
117458** following a valid object, it may not be used in comparison operations.
117459*/
117460static void renameTokenCheckAll(Parse *pParse, const void *pPtr){
117461 assert( pParse==pParse->db->pParse )((void) (0));
117462 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
117463 if( pParse->nErr==0 ){
117464 const RenameToken *p;
117465 u32 i = 1;
117466 for(p=pParse->pRename; p; p=p->pNext){
117467 if( p->p ){
117468 assert( p->p!=pPtr )((void) (0));
117469 i += *(u8*)(p->p) | 1;
117470 }
117471 }
117472 assert( i>0 )((void) (0));
117473 }
117474}
117475#else
117476# define renameTokenCheckAll(x,y)
117477#endif
117478
117479/*
117480** Remember that the parser tree element pPtr was created using
117481** the token pToken.
117482**
117483** In other words, construct a new RenameToken object and add it
117484** to the list of RenameToken objects currently being built up
117485** in pParse->pRename.
117486**
117487** The pPtr argument is returned so that this routine can be used
117488** with tail recursion in tokenExpr() routine, for a small performance
117489** improvement.
117490*/
117491SQLITE_PRIVATEstatic const void *sqlite3RenameTokenMap(
117492 Parse *pParse,
117493 const void *pPtr,
117494 const Token *pToken
117495){
117496 RenameToken *pNew;
117497 assert( pPtr || pParse->db->mallocFailed )((void) (0));
117498 renameTokenCheckAll(pParse, pPtr);
117499 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP)(pParse->eParseMode!=3) ){
117500 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
117501 if( pNew ){
117502 pNew->p = pPtr;
117503 pNew->t = *pToken;
117504 pNew->pNext = pParse->pRename;
117505 pParse->pRename = pNew;
117506 }
117507 }
117508
117509 return pPtr;
117510}
117511
117512/*
117513** It is assumed that there is already a RenameToken object associated
117514** with parse tree element pFrom. This function remaps the associated token
117515** to parse tree element pTo.
117516*/
117517SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse *pParse, const void *pTo, const void *pFrom){
117518 RenameToken *p;
117519 renameTokenCheckAll(pParse, pTo);
117520 for(p=pParse->pRename; p; p=p->pNext){
117521 if( p->p==pFrom ){
117522 p->p = pTo;
117523 break;
117524 }
117525 }
117526}
117527
117528/*
117529** Walker callback used by sqlite3RenameExprUnmap().
117530*/
117531static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
117532 Parse *pParse = pWalker->pParse;
117533 sqlite3RenameTokenRemap(pParse, 0, (const void*)pExpr);
117534 if( ExprUseYTab(pExpr)(((pExpr)->flags&(0x1000000|0x2000000))==0) ){
117535 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
117536 }
117537 return WRC_Continue0;
117538}
117539
117540/*
117541** Iterate through the Select objects that are part of WITH clauses attached
117542** to select statement pSelect.
117543*/
117544static void renameWalkWith(Walker *pWalker, Select *pSelect){
117545 With *pWith = pSelect->pWith;
117546 if( pWith ){
117547 Parse *pParse = pWalker->pParse;
117548 int i;
117549 With *pCopy = 0;
117550 assert( pWith->nCte>0 )((void) (0));
117551 if( (pWith->a[0].pSelect->selFlags & SF_Expanded0x0000040)==0 ){
117552 /* Push a copy of the With object onto the with-stack. We use a copy
117553 ** here as the original will be expanded and resolved (flags SF_Expanded
117554 ** and SF_Resolved) below. And the parser code that uses the with-stack
117555 ** fails if the Select objects on it have already been expanded and
117556 ** resolved. */
117557 pCopy = sqlite3WithDup(pParse->db, pWith);
117558 pCopy = sqlite3WithPush(pParse, pCopy, 1);
117559 }
117560 for(i=0; i<pWith->nCte; i++){
117561 Select *p = pWith->a[i].pSelect;
117562 NameContext sNC;
117563 memset(&sNC, 0, sizeof(sNC));
117564 sNC.pParse = pParse;
117565 if( pCopy ) sqlite3SelectPrep(sNC.pParse, p, &sNC);
117566 if( sNC.pParse->db->mallocFailed ) return;
117567 sqlite3WalkSelect(pWalker, p);
117568 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
117569 }
117570 if( pCopy && pParse->pWith==pCopy ){
117571 pParse->pWith = pCopy->pOuter;
117572 }
117573 }
117574}
117575
117576/*
117577** Unmap all tokens in the IdList object passed as the second argument.
117578*/
117579static void unmapColumnIdlistNames(
117580 Parse *pParse,
117581 const IdList *pIdList
117582){
117583 int ii;
117584 assert( pIdList!=0 )((void) (0));
117585 for(ii=0; ii<pIdList->nId; ii++){
117586 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
117587 }
117588}
117589
117590/*
117591** Walker callback used by sqlite3RenameExprUnmap().
117592*/
117593static int renameUnmapSelectCb(Walker *pWalker, Select *p){
117594 Parse *pParse = pWalker->pParse;
117595 int i;
117596 if( pParse->nErr ) return WRC_Abort2;
117597 testcase( p->selFlags & SF_View );
117598 testcase( p->selFlags & SF_CopyCte );
117599 if( p->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
117600 return WRC_Prune1;
117601 }
117602 if( ALWAYS(p->pEList)(p->pEList) ){
117603 ExprList *pList = p->pEList;
117604 for(i=0; i<pList->nExpr; i++){
117605 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME0 ){
117606 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
117607 }
117608 }
117609 }
117610 if( ALWAYS(p->pSrc)(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
117611 SrcList *pSrc = p->pSrc;
117612 for(i=0; i<pSrc->nSrc; i++){
117613 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
117614 if( pSrc->a[i].fg.isUsing==0 ){
117615 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
117616 }else{
117617 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
117618 }
117619 }
117620 }
117621
117622 renameWalkWith(pWalker, p);
117623 return WRC_Continue0;
117624}
117625
117626/*
117627** Remove all nodes that are part of expression pExpr from the rename list.
117628*/
117629SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
117630 u8 eMode = pParse->eParseMode;
117631 Walker sWalker;
117632 memset(&sWalker, 0, sizeof(Walker));
117633 sWalker.pParse = pParse;
117634 sWalker.xExprCallback = renameUnmapExprCb;
117635 sWalker.xSelectCallback = renameUnmapSelectCb;
117636 pParse->eParseMode = PARSE_MODE_UNMAP3;
117637 sqlite3WalkExpr(&sWalker, pExpr);
117638 pParse->eParseMode = eMode;
117639}
117640
117641/*
117642** Remove all nodes that are part of expression-list pEList from the
117643** rename list.
117644*/
117645SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
117646 if( pEList ){
117647 int i;
117648 Walker sWalker;
117649 memset(&sWalker, 0, sizeof(Walker));
117650 sWalker.pParse = pParse;
117651 sWalker.xExprCallback = renameUnmapExprCb;
117652 sqlite3WalkExprList(&sWalker, pEList);
117653 for(i=0; i<pEList->nExpr; i++){
117654 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)(pEList->a[i].fg.eEName==0) ){
117655 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
117656 }
117657 }
117658 }
117659}
117660
117661/*
117662** Free the list of RenameToken objects given in the second argument
117663*/
117664static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
117665 RenameToken *pNext;
117666 RenameToken *p;
117667 for(p=pToken; p; p=pNext){
117668 pNext = p->pNext;
117669 sqlite3DbFree(db, p);
117670 }
117671}
117672
117673/*
117674** Search the Parse object passed as the first argument for a RenameToken
117675** object associated with parse tree element pPtr. If found, return a pointer
117676** to it. Otherwise, return NULL.
117677**
117678** If the second argument passed to this function is not NULL and a matching
117679** RenameToken object is found, remove it from the Parse object and add it to
117680** the list maintained by the RenameCtx object.
117681*/
117682static RenameToken *renameTokenFind(
117683 Parse *pParse,
117684 struct RenameCtx *pCtx,
117685 const void *pPtr
117686){
117687 RenameToken **pp;
117688 if( NEVER(pPtr==0)(pPtr==0) ){
117689 return 0;
117690 }
117691 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
117692 if( (*pp)->p==pPtr ){
117693 RenameToken *pToken = *pp;
117694 if( pCtx ){
117695 *pp = pToken->pNext;
117696 pToken->pNext = pCtx->pList;
117697 pCtx->pList = pToken;
117698 pCtx->nList++;
117699 }
117700 return pToken;
117701 }
117702 }
117703 return 0;
117704}
117705
117706/*
117707** This is a Walker select callback. It does nothing. It is only required
117708** because without a dummy callback, sqlite3WalkExpr() and similar do not
117709** descend into sub-select statements.
117710*/
117711static int renameColumnSelectCb(Walker *pWalker, Select *p){
117712 if( p->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
117713 testcase( p->selFlags & SF_View );
117714 testcase( p->selFlags & SF_CopyCte );
117715 return WRC_Prune1;
117716 }
117717 renameWalkWith(pWalker, p);
117718 return WRC_Continue0;
117719}
117720
117721/*
117722** This is a Walker expression callback.
117723**
117724** For every TK_COLUMN node in the expression tree, search to see
117725** if the column being references is the column being renamed by an
117726** ALTER TABLE statement. If it is, then attach its associated
117727** RenameToken object to the list of RenameToken objects being
117728** constructed in RenameCtx object at pWalker->u.pRename.
117729*/
117730static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
117731 RenameCtx *p = pWalker->u.pRename;
117732 if( pExpr->op==TK_TRIGGER78
117733 && pExpr->iColumn==p->iCol
117734 && pWalker->pParse->pTriggerTab==p->pTab
117735 ){
117736 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
117737 }else if( pExpr->op==TK_COLUMN168
117738 && pExpr->iColumn==p->iCol
117739 && ALWAYS(ExprUseYTab(pExpr))((((pExpr)->flags&(0x1000000|0x2000000))==0))
117740 && p->pTab==pExpr->y.pTab
117741 ){
117742 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
117743 }
117744 return WRC_Continue0;
117745}
117746
117747/*
117748** The RenameCtx contains a list of tokens that reference a column that
117749** is being renamed by an ALTER TABLE statement. Return the "last"
117750** RenameToken in the RenameCtx and remove that RenameToken from the
117751** RenameContext. "Last" means the last RenameToken encountered when
117752** the input SQL is parsed from left to right. Repeated calls to this routine
117753** return all column name tokens in the order that they are encountered
117754** in the SQL statement.
117755*/
117756static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
117757 RenameToken *pBest = pCtx->pList;
117758 RenameToken *pToken;
117759 RenameToken **pp;
117760
117761 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
117762 if( pToken->t.z>pBest->t.z ) pBest = pToken;
117763 }
117764 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
117765 *pp = pBest->pNext;
117766
117767 return pBest;
117768}
117769
117770/*
117771** An error occurred while parsing or otherwise processing a database
117772** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
117773** ALTER TABLE RENAME COLUMN program. The error message emitted by the
117774** sub-routine is currently stored in pParse->zErrMsg. This function
117775** adds context to the error message and then stores it in pCtx.
117776*/
117777static void renameColumnParseError(
117778 sqlite3_context *pCtx,
117779 const char *zWhen,
117780 sqlite3_value *pType,
117781 sqlite3_value *pObject,
117782 Parse *pParse
117783){
117784 const char *zT = (const char*)sqlite3_value_text(pType);
117785 const char *zN = (const char*)sqlite3_value_text(pObject);
117786 char *zErr;
117787
117788 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
117789 zT, zN, (zWhen[0] ? " " : ""), zWhen,
117790 pParse->zErrMsg
117791 );
117792 sqlite3_result_error(pCtx, zErr, -1);
117793 sqlite3DbFree(pParse->db, zErr);
117794}
117795
117796/*
117797** For each name in the the expression-list pEList (i.e. each
117798** pEList->a[i].zName) that matches the string in zOld, extract the
117799** corresponding rename-token from Parse object pParse and add it
117800** to the RenameCtx pCtx.
117801*/
117802static void renameColumnElistNames(
117803 Parse *pParse,
117804 RenameCtx *pCtx,
117805 const ExprList *pEList,
117806 const char *zOld
117807){
117808 if( pEList ){
117809 int i;
117810 for(i=0; i<pEList->nExpr; i++){
117811 const char *zName = pEList->a[i].zEName;
117812 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)(pEList->a[i].fg.eEName==0)
117813 && ALWAYS(zName!=0)(zName!=0)
117814 && 0==sqlite3_stricmp(zName, zOld)
117815 ){
117816 renameTokenFind(pParse, pCtx, (const void*)zName);
117817 }
117818 }
117819 }
117820}
117821
117822/*
117823** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
117824** that matches the string in zOld, extract the corresponding rename-token
117825** from Parse object pParse and add it to the RenameCtx pCtx.
117826*/
117827static void renameColumnIdlistNames(
117828 Parse *pParse,
117829 RenameCtx *pCtx,
117830 const IdList *pIdList,
117831 const char *zOld
117832){
117833 if( pIdList ){
117834 int i;
117835 for(i=0; i<pIdList->nId; i++){
117836 const char *zName = pIdList->a[i].zName;
117837 if( 0==sqlite3_stricmp(zName, zOld) ){
117838 renameTokenFind(pParse, pCtx, (const void*)zName);
117839 }
117840 }
117841 }
117842}
117843
117844
117845/*
117846** Parse the SQL statement zSql using Parse object (*p). The Parse object
117847** is initialized by this function before it is used.
117848*/
117849static int renameParseSql(
117850 Parse *p, /* Memory to use for Parse object */
117851 const char *zDb, /* Name of schema SQL belongs to */
117852 sqlite3 *db, /* Database handle */
117853 const char *zSql, /* SQL to parse */
117854 int bTemp /* True if SQL is from temp schema */
117855){
117856 int rc;
117857
117858 sqlite3ParseObjectInit(p, db);
117859 if( zSql==0 ){
117860 return SQLITE_NOMEM7;
117861 }
117862 if( sqlite3StrNICmpsqlite3_strnicmp(zSql,"CREATE ",7)!=0 ){
117863 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(117863);
117864 }
117865 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
117866 p->eParseMode = PARSE_MODE_RENAME2;
117867 p->db = db;
117868 p->nQueryLoop = 1;
117869 rc = sqlite3RunParser(p, zSql);
117870 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
117871 if( rc==SQLITE_OK0
117872 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)(p->pNewTable==0 && p->pNewIndex==0 && p
->pNewTrigger==0)
117873 ){
117874 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(117874);
117875 }
117876
117877#ifdef SQLITE_DEBUG
117878 /* Ensure that all mappings in the Parse.pRename list really do map to
117879 ** a part of the input string. */
117880 if( rc==SQLITE_OK0 ){
117881 int nSql = sqlite3Strlen30(zSql);
117882 RenameToken *pToken;
117883 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
117884 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] )((void) (0));
117885 }
117886 }
117887#endif
117888
117889 db->init.iDb = 0;
117890 return rc;
117891}
117892
117893/*
117894** This function edits SQL statement zSql, replacing each token identified
117895** by the linked list pRename with the text of zNew. If argument bQuote is
117896** true, then zNew is always quoted first. If no error occurs, the result
117897** is loaded into context object pCtx as the result.
117898**
117899** Or, if an error occurs (i.e. an OOM condition), an error is left in
117900** pCtx and an SQLite error code returned.
117901*/
117902static int renameEditSql(
117903 sqlite3_context *pCtx, /* Return result here */
117904 RenameCtx *pRename, /* Rename context */
117905 const char *zSql, /* SQL statement to edit */
117906 const char *zNew, /* New token text */
117907 int bQuote /* True to always quote token */
117908){
117909 i64 nNew = sqlite3Strlen30(zNew);
117910 i64 nSql = sqlite3Strlen30(zSql);
117911 sqlite3 *db = sqlite3_context_db_handle(pCtx);
117912 int rc = SQLITE_OK0;
117913 char *zQuot = 0;
117914 char *zOut;
117915 i64 nQuot = 0;
117916 char *zBuf1 = 0;
117917 char *zBuf2 = 0;
117918
117919 if( zNew ){
117920 /* Set zQuot to point to a buffer containing a quoted copy of the
117921 ** identifier zNew. If the corresponding identifier in the original
117922 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
117923 ** point to zQuot so that all substitutions are made using the
117924 ** quoted version of the new column name. */
117925 zQuot = sqlite3MPrintf(db, "\"%w\" ", zNew);
117926 if( zQuot==0 ){
117927 return SQLITE_NOMEM7;
117928 }else{
117929 nQuot = sqlite3Strlen30(zQuot)-1;
117930 }
117931
117932 assert( nQuot>=nNew )((void) (0));
117933 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
117934 }else{
117935 zOut = (char*)sqlite3DbMallocZero(db, (nSql*2+1) * 3);
117936 if( zOut ){
117937 zBuf1 = &zOut[nSql*2+1];
117938 zBuf2 = &zOut[nSql*4+2];
117939 }
117940 }
117941
117942 /* At this point pRename->pList contains a list of RenameToken objects
117943 ** corresponding to all tokens in the input SQL that must be replaced
117944 ** with the new column name, or with single-quoted versions of themselves.
117945 ** All that remains is to construct and return the edited SQL string. */
117946 if( zOut ){
117947 int nOut = nSql;
117948 memcpy(zOut, zSql, nSql);
117949 while( pRename->pList ){
117950 int iOff; /* Offset of token to replace in zOut */
117951 u32 nReplace;
117952 const char *zReplace;
117953 RenameToken *pBest = renameColumnTokenNext(pRename);
117954
117955 if( zNew ){
117956 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
117957 nReplace = nNew;
117958 zReplace = zNew;
117959 }else{
117960 nReplace = nQuot;
117961 zReplace = zQuot;
117962 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
117963 }
117964 }else{
117965 /* Dequote the double-quoted token. Then requote it again, this time
117966 ** using single quotes. If the character immediately following the
117967 ** original token within the input SQL was a single quote ('), then
117968 ** add another space after the new, single-quoted version of the
117969 ** token. This is so that (SELECT "string"'alias') maps to
117970 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
117971 memcpy(zBuf1, pBest->t.z, pBest->t.n);
117972 zBuf1[pBest->t.n] = 0;
117973 sqlite3Dequote(zBuf1);
117974 sqlite3_snprintf(nSql*2, zBuf2, "%Q%s", zBuf1,
117975 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
117976 );
117977 zReplace = zBuf2;
117978 nReplace = sqlite3Strlen30(zReplace);
117979 }
117980
117981 iOff = pBest->t.z - zSql;
117982 if( pBest->t.n!=nReplace ){
117983 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
117984 nOut - (iOff + pBest->t.n)
117985 );
117986 nOut += nReplace - pBest->t.n;
117987 zOut[nOut] = '\0';
117988 }
117989 memcpy(&zOut[iOff], zReplace, nReplace);
117990 sqlite3DbFree(db, pBest);
117991 }
117992
117993 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
117994 sqlite3DbFree(db, zOut);
117995 }else{
117996 rc = SQLITE_NOMEM7;
117997 }
117998
117999 sqlite3_free(zQuot);
118000 return rc;
118001}
118002
118003/*
118004** Set all pEList->a[].fg.eEName fields in the expression-list to val.
118005*/
118006static void renameSetENames(ExprList *pEList, int val){
118007 if( pEList ){
118008 int i;
118009 for(i=0; i<pEList->nExpr; i++){
118010 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME )((void) (0));
118011 pEList->a[i].fg.eEName = val;
118012 }
118013 }
118014}
118015
118016/*
118017** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
118018** it was read from the schema of database zDb. Return SQLITE_OK if
118019** successful. Otherwise, return an SQLite error code and leave an error
118020** message in the Parse object.
118021*/
118022static int renameResolveTrigger(Parse *pParse){
118023 sqlite3 *db = pParse->db;
118024 Trigger *pNew = pParse->pNewTrigger;
118025 TriggerStep *pStep;
118026 NameContext sNC;
118027 int rc = SQLITE_OK0;
118028
118029 memset(&sNC, 0, sizeof(sNC));
118030 sNC.pParse = pParse;
118031 assert( pNew->pTabSchema )((void) (0));
118032 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
118033 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
118034 );
118035 pParse->eTriggerOp = pNew->op;
118036 /* ALWAYS() because if the table of the trigger does not exist, the
118037 ** error would have been hit before this point */
118038 if( ALWAYS(pParse->pTriggerTab)(pParse->pTriggerTab) ){
118039 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0;
118040 }
118041
118042 /* Resolve symbols in WHEN clause */
118043 if( rc==SQLITE_OK0 && pNew->pWhen ){
118044 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
118045 }
118046
118047 for(pStep=pNew->step_list; rc==SQLITE_OK0 && pStep; pStep=pStep->pNext){
118048 if( pStep->pSelect ){
118049 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
118050 if( pParse->nErr ) rc = pParse->rc;
118051 }
118052 if( rc==SQLITE_OK0 && pStep->zTarget ){
118053 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
118054 if( pSrc ){
118055 Select *pSel = sqlite3SelectNew(
118056 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
118057 );
118058 if( pSel==0 ){
118059 pStep->pExprList = 0;
118060 pSrc = 0;
118061 rc = SQLITE_NOMEM7;
118062 }else{
118063 /* pStep->pExprList contains an expression-list used for an UPDATE
118064 ** statement. So the a[].zEName values are the RHS of the
118065 ** "<col> = <expr>" clauses of the UPDATE statement. So, before
118066 ** running SelectPrep(), change all the eEName values in
118067 ** pStep->pExprList to ENAME_SPAN (from their current value of
118068 ** ENAME_NAME). This is to prevent any ids in ON() clauses that are
118069 ** part of pSrc from being incorrectly resolved against the
118070 ** a[].zEName values as if they were column aliases. */
118071 renameSetENames(pStep->pExprList, ENAME_SPAN1);
118072 sqlite3SelectPrep(pParse, pSel, 0);
118073 renameSetENames(pStep->pExprList, ENAME_NAME0);
118074 rc = pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
118075 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList )((void) (0));
118076 assert( pSrc==pSel->pSrc )((void) (0));
118077 if( pStep->pExprList ) pSel->pEList = 0;
118078 pSel->pSrc = 0;
118079 sqlite3SelectDelete(db, pSel);
118080 }
118081 if( pStep->pFrom ){
118082 int i;
118083 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK0; i++){
118084 SrcItem *p = &pStep->pFrom->a[i];
118085 if( p->fg.isSubquery ){
118086 assert( p->u4.pSubq!=0 )((void) (0));
118087 sqlite3SelectPrep(pParse, p->u4.pSubq->pSelect, 0);
118088 }
118089 }
118090 }
118091
118092 if( db->mallocFailed ){
118093 rc = SQLITE_NOMEM7;
118094 }
118095 sNC.pSrcList = pSrc;
118096 if( rc==SQLITE_OK0 && pStep->pWhere ){
118097 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
118098 }
118099 if( rc==SQLITE_OK0 ){
118100 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
118101 }
118102 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) )((void) (0));
118103 if( pStep->pUpsert && rc==SQLITE_OK0 ){
118104 Upsert *pUpsert = pStep->pUpsert;
118105 pUpsert->pUpsertSrc = pSrc;
118106 sNC.uNC.pUpsert = pUpsert;
118107 sNC.ncFlags = NC_UUpsert0x000200;
118108 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
118109 if( rc==SQLITE_OK0 ){
118110 ExprList *pUpsertSet = pUpsert->pUpsertSet;
118111 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
118112 }
118113 if( rc==SQLITE_OK0 ){
118114 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
118115 }
118116 if( rc==SQLITE_OK0 ){
118117 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
118118 }
118119 sNC.ncFlags = 0;
118120 }
118121 sNC.pSrcList = 0;
118122 sqlite3SrcListDelete(db, pSrc);
118123 }else{
118124 rc = SQLITE_NOMEM7;
118125 }
118126 }
118127 }
118128 return rc;
118129}
118130
118131/*
118132** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
118133** objects that are part of the trigger passed as the second argument.
118134*/
118135static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
118136 TriggerStep *pStep;
118137
118138 /* Find tokens to edit in WHEN clause */
118139 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
118140
118141 /* Find tokens to edit in trigger steps */
118142 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
118143 sqlite3WalkSelect(pWalker, pStep->pSelect);
118144 sqlite3WalkExpr(pWalker, pStep->pWhere);
118145 sqlite3WalkExprList(pWalker, pStep->pExprList);
118146 if( pStep->pUpsert ){
118147 Upsert *pUpsert = pStep->pUpsert;
118148 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
118149 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
118150 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
118151 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
118152 }
118153 if( pStep->pFrom ){
118154 int i;
118155 SrcList *pFrom = pStep->pFrom;
118156 for(i=0; i<pFrom->nSrc; i++){
118157 if( pFrom->a[i].fg.isSubquery ){
118158 assert( pFrom->a[i].u4.pSubq!=0 )((void) (0));
118159 sqlite3WalkSelect(pWalker, pFrom->a[i].u4.pSubq->pSelect);
118160 }
118161 }
118162 }
118163 }
118164}
118165
118166/*
118167** Free the contents of Parse object (*pParse). Do not free the memory
118168** occupied by the Parse object itself.
118169*/
118170static void renameParseCleanup(Parse *pParse){
118171 sqlite3 *db = pParse->db;
118172 Index *pIdx;
118173 if( pParse->pVdbe ){
118174 sqlite3VdbeFinalize(pParse->pVdbe);
118175 }
118176 sqlite3DeleteTable(db, pParse->pNewTable);
118177 while( (pIdx = pParse->pNewIndex)!=0 ){
118178 pParse->pNewIndex = pIdx->pNext;
118179 sqlite3FreeIndex(db, pIdx);
118180 }
118181 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
118182 sqlite3DbFree(db, pParse->zErrMsg);
118183 renameTokenFree(db, pParse->pRename);
118184 sqlite3ParseObjectReset(pParse);
118185}
118186
118187/*
118188** SQL function:
118189**
118190** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP)
118191**
118192** 0. zSql: SQL statement to rewrite
118193** 1. type: Type of object ("table", "view" etc.)
118194** 2. object: Name of object
118195** 3. Database: Database name (e.g. "main")
118196** 4. Table: Table name
118197** 5. iCol: Index of column to rename
118198** 6. zNew: New column name
118199** 7. bQuote: Non-zero if the new column name should be quoted.
118200** 8. bTemp: True if zSql comes from temp schema
118201**
118202** Do a column rename operation on the CREATE statement given in zSql.
118203** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
118204** into zNew. The name should be quoted if bQuote is true.
118205**
118206** This function is used internally by the ALTER TABLE RENAME COLUMN command.
118207** It is only accessible to SQL created using sqlite3NestedParse(). It is
118208** not reachable from ordinary SQL passed into sqlite3_prepare() unless the
118209** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled.
118210*/
118211static void renameColumnFunc(
118212 sqlite3_context *context,
118213 int NotUsed,
118214 sqlite3_value **argv
118215){
118216 sqlite3 *db = sqlite3_context_db_handle(context);
118217 RenameCtx sCtx;
118218 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
118219 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
118220 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
118221 int iCol = sqlite3_value_int(argv[5]);
118222 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
118223 int bQuote = sqlite3_value_int(argv[7]);
118224 int bTemp = sqlite3_value_int(argv[8]);
118225 const char *zOld;
118226 int rc;
118227 Parse sParse;
118228 Walker sWalker;
118229 Index *pIdx;
118230 int i;
118231 Table *pTab;
118232#ifndef SQLITE_OMIT_AUTHORIZATION
118233 sqlite3_xauth xAuth = db->xAuth;
118234#endif
118235
118236 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
118237 if( zSql==0 ) return;
118238 if( zTable==0 ) return;
118239 if( zNew==0 ) return;
118240 if( iCol<0 ) return;
118241 sqlite3BtreeEnterAll(db);
118242 pTab = sqlite3FindTable(db, zTable, zDb);
118243 if( pTab==0 || iCol>=pTab->nCol ){
118244 sqlite3BtreeLeaveAll(db);
118245 return;
118246 }
118247 zOld = pTab->aCol[iCol].zCnName;
118248 memset(&sCtx, 0, sizeof(sCtx));
118249 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
118250
118251#ifndef SQLITE_OMIT_AUTHORIZATION
118252 db->xAuth = 0;
118253#endif
118254 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
118255
118256 /* Find tokens that need to be replaced. */
118257 memset(&sWalker, 0, sizeof(Walker));
118258 sWalker.pParse = &sParse;
118259 sWalker.xExprCallback = renameColumnExprCb;
118260 sWalker.xSelectCallback = renameColumnSelectCb;
118261 sWalker.u.pRename = &sCtx;
118262
118263 sCtx.pTab = pTab;
118264 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
118265 if( sParse.pNewTable ){
118266 if( IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
118267 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118268 pSelect->selFlags &= ~SF_View0x0200000;
118269 sParse.rc = SQLITE_OK0;
118270 sqlite3SelectPrep(&sParse, pSelect, 0);
118271 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
118272 if( rc==SQLITE_OK0 ){
118273 sqlite3WalkSelect(&sWalker, pSelect);
118274 }
118275 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
118276 }else if( IsOrdinaryTable(sParse.pNewTable)((sParse.pNewTable)->eTabType==0) ){
118277 /* A regular table */
118278 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
118279 FKey *pFKey;
118280 sCtx.pTab = sParse.pNewTable;
118281 if( bFKOnly==0 ){
118282 if( iCol<sParse.pNewTable->nCol ){
118283 renameTokenFind(
118284 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
118285 );
118286 }
118287 if( sCtx.iCol<0 ){
118288 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
118289 }
118290 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
118291 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
118292 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
118293 }
118294 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
118295 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
118296 }
118297#ifndef SQLITE_OMIT_GENERATED_COLUMNS
118298 for(i=0; i<sParse.pNewTable->nCol; i++){
118299 Expr *pExpr = sqlite3ColumnExpr(sParse.pNewTable,
118300 &sParse.pNewTable->aCol[i]);
118301 sqlite3WalkExpr(&sWalker, pExpr);
118302 }
118303#endif
118304 }
118305
118306 assert( IsOrdinaryTable(sParse.pNewTable) )((void) (0));
118307 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
118308 for(i=0; i<pFKey->nCol; i++){
118309 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
118310 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
118311 }
118312 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
118313 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
118314 ){
118315 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
118316 }
118317 }
118318 }
118319 }
118320 }else if( sParse.pNewIndex ){
118321 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
118322 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
118323 }else{
118324 /* A trigger */
118325 TriggerStep *pStep;
118326 rc = renameResolveTrigger(&sParse);
118327 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
118328
118329 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
118330 if( pStep->zTarget ){
118331 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
118332 if( pTarget==pTab ){
118333 if( pStep->pUpsert ){
118334 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
118335 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
118336 }
118337 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
118338 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
118339 }
118340 }
118341 }
118342
118343
118344 /* Find tokens to edit in UPDATE OF clause */
118345 if( sParse.pTriggerTab==pTab ){
118346 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
118347 }
118348
118349 /* Find tokens to edit in various expressions and selects */
118350 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
118351 }
118352
118353 assert( rc==SQLITE_OK )((void) (0));
118354 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
118355
118356renameColumnFunc_done:
118357 if( rc!=SQLITE_OK0 ){
118358 if( rc==SQLITE_ERROR1 && sqlite3WritableSchema(db) ){
118359 sqlite3_result_value(context, argv[0]);
118360 }else if( sParse.zErrMsg ){
118361 renameColumnParseError(context, "", argv[1], argv[2], &sParse);
118362 }else{
118363 sqlite3_result_error_code(context, rc);
118364 }
118365 }
118366
118367 renameParseCleanup(&sParse);
118368 renameTokenFree(db, sCtx.pList);
118369#ifndef SQLITE_OMIT_AUTHORIZATION
118370 db->xAuth = xAuth;
118371#endif
118372 sqlite3BtreeLeaveAll(db);
118373}
118374
118375/*
118376** Walker expression callback used by "RENAME TABLE".
118377*/
118378static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
118379 RenameCtx *p = pWalker->u.pRename;
118380 if( pExpr->op==TK_COLUMN168
118381 && ALWAYS(ExprUseYTab(pExpr))((((pExpr)->flags&(0x1000000|0x2000000))==0))
118382 && p->pTab==pExpr->y.pTab
118383 ){
118384 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
118385 }
118386 return WRC_Continue0;
118387}
118388
118389/*
118390** Walker select callback used by "RENAME TABLE".
118391*/
118392static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
118393 int i;
118394 RenameCtx *p = pWalker->u.pRename;
118395 SrcList *pSrc = pSelect->pSrc;
118396 if( pSelect->selFlags & (SF_View0x0200000|SF_CopyCte0x4000000) ){
118397 testcase( pSelect->selFlags & SF_View );
118398 testcase( pSelect->selFlags & SF_CopyCte );
118399 return WRC_Prune1;
118400 }
118401 if( NEVER(pSrc==0)(pSrc==0) ){
118402 assert( pWalker->pParse->db->mallocFailed )((void) (0));
118403 return WRC_Abort2;
118404 }
118405 for(i=0; i<pSrc->nSrc; i++){
118406 SrcItem *pItem = &pSrc->a[i];
118407 if( pItem->pSTab==p->pTab ){
118408 renameTokenFind(pWalker->pParse, p, pItem->zName);
118409 }
118410 }
118411 renameWalkWith(pWalker, pSelect);
118412
118413 return WRC_Continue0;
118414}
118415
118416
118417/*
118418** This C function implements an SQL user function that is used by SQL code
118419** generated by the ALTER TABLE ... RENAME command to modify the definition
118420** of any foreign key constraints that use the table being renamed as the
118421** parent table. It is passed three arguments:
118422**
118423** 0: The database containing the table being renamed.
118424** 1. type: Type of object ("table", "view" etc.)
118425** 2. object: Name of object
118426** 3: The complete text of the schema statement being modified,
118427** 4: The old name of the table being renamed, and
118428** 5: The new name of the table being renamed.
118429** 6: True if the schema statement comes from the temp db.
118430**
118431** It returns the new schema statement. For example:
118432**
118433** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
118434** -> 'CREATE TABLE t1(a REFERENCES t3)'
118435*/
118436static void renameTableFunc(
118437 sqlite3_context *context,
118438 int NotUsed,
118439 sqlite3_value **argv
118440){
118441 sqlite3 *db = sqlite3_context_db_handle(context);
118442 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
118443 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
118444 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
118445 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
118446 int bTemp = sqlite3_value_int(argv[6]);
118447 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
118448
118449 if( zInput && zOld && zNew ){
118450 Parse sParse;
118451 int rc;
118452 int bQuote = 1;
118453 RenameCtx sCtx;
118454 Walker sWalker;
118455
118456#ifndef SQLITE_OMIT_AUTHORIZATION
118457 sqlite3_xauth xAuth = db->xAuth;
118458 db->xAuth = 0;
118459#endif
118460
118461 sqlite3BtreeEnterAll(db);
118462
118463 memset(&sCtx, 0, sizeof(RenameCtx));
118464 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
118465 memset(&sWalker, 0, sizeof(Walker));
118466 sWalker.pParse = &sParse;
118467 sWalker.xExprCallback = renameTableExprCb;
118468 sWalker.xSelectCallback = renameTableSelectCb;
118469 sWalker.u.pRename = &sCtx;
118470
118471 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
118472
118473 if( rc==SQLITE_OK0 ){
118474 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
118475 if( sParse.pNewTable ){
118476 Table *pTab = sParse.pNewTable;
118477
118478 if( IsView(pTab)((pTab)->eTabType==2) ){
118479 if( isLegacy==0 ){
118480 Select *pSelect = pTab->u.view.pSelect;
118481 NameContext sNC;
118482 memset(&sNC, 0, sizeof(sNC));
118483 sNC.pParse = &sParse;
118484
118485 assert( pSelect->selFlags & SF_View )((void) (0));
118486 pSelect->selFlags &= ~SF_View0x0200000;
118487 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
118488 if( sParse.nErr ){
118489 rc = sParse.rc;
118490 }else{
118491 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
118492 }
118493 }
118494 }else{
118495 /* Modify any FK definitions to point to the new table. */
118496#ifndef SQLITE_OMIT_FOREIGN_KEY
118497 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys0x00004000))
118498 && !IsVirtual(pTab)((pTab)->eTabType==1)
118499 ){
118500 FKey *pFKey;
118501 assert( IsOrdinaryTable(pTab) )((void) (0));
118502 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
118503 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
118504 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
118505 }
118506 }
118507 }
118508#endif
118509
118510 /* If this is the table being altered, fix any table refs in CHECK
118511 ** expressions. Also update the name that appears right after the
118512 ** "CREATE [VIRTUAL] TABLE" bit. */
118513 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
118514 sCtx.pTab = pTab;
118515 if( isLegacy==0 ){
118516 sqlite3WalkExprList(&sWalker, pTab->pCheck);
118517 }
118518 renameTokenFind(&sParse, &sCtx, pTab->zName);
118519 }
118520 }
118521 }
118522
118523 else if( sParse.pNewIndex ){
118524 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
118525 if( isLegacy==0 ){
118526 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
118527 }
118528 }
118529
118530#ifndef SQLITE_OMIT_TRIGGER
118531 else{
118532 Trigger *pTrigger = sParse.pNewTrigger;
118533 TriggerStep *pStep;
118534 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
118535 && sCtx.pTab->pSchema==pTrigger->pTabSchema
118536 ){
118537 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
118538 }
118539
118540 if( isLegacy==0 ){
118541 rc = renameResolveTrigger(&sParse);
118542 if( rc==SQLITE_OK0 ){
118543 renameWalkTrigger(&sWalker, pTrigger);
118544 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
118545 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
118546 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
118547 }
118548 if( pStep->pFrom ){
118549 int i;
118550 for(i=0; i<pStep->pFrom->nSrc; i++){
118551 SrcItem *pItem = &pStep->pFrom->a[i];
118552 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
118553 renameTokenFind(&sParse, &sCtx, pItem->zName);
118554 }
118555 }
118556 }
118557 }
118558 }
118559 }
118560 }
118561#endif
118562 }
118563
118564 if( rc==SQLITE_OK0 ){
118565 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
118566 }
118567 if( rc!=SQLITE_OK0 ){
118568 if( rc==SQLITE_ERROR1 && sqlite3WritableSchema(db) ){
118569 sqlite3_result_value(context, argv[3]);
118570 }else if( sParse.zErrMsg ){
118571 renameColumnParseError(context, "", argv[1], argv[2], &sParse);
118572 }else{
118573 sqlite3_result_error_code(context, rc);
118574 }
118575 }
118576
118577 renameParseCleanup(&sParse);
118578 renameTokenFree(db, sCtx.pList);
118579 sqlite3BtreeLeaveAll(db);
118580#ifndef SQLITE_OMIT_AUTHORIZATION
118581 db->xAuth = xAuth;
118582#endif
118583 }
118584
118585 return;
118586}
118587
118588static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){
118589 if( pExpr->op==TK_STRING118 && (pExpr->flags & EP_DblQuoted0x000080) ){
118590 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
118591 }
118592 return WRC_Continue0;
118593}
118594
118595/* SQL function: sqlite_rename_quotefix(DB,SQL)
118596**
118597** Rewrite the DDL statement "SQL" so that any string literals that use
118598** double-quotes use single quotes instead.
118599**
118600** Two arguments must be passed:
118601**
118602** 0: Database name ("main", "temp" etc.).
118603** 1: SQL statement to edit.
118604**
118605** The returned value is the modified SQL statement. For example, given
118606** the database schema:
118607**
118608** CREATE TABLE t1(a, b, c);
118609**
118610** SELECT sqlite_rename_quotefix('main',
118611** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1'
118612** );
118613**
118614** returns the string:
118615**
118616** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1
118617**
118618** If there is a error in the input SQL, then raise an error, except
118619** if PRAGMA writable_schema=ON, then just return the input string
118620** unmodified following an error.
118621*/
118622static void renameQuotefixFunc(
118623 sqlite3_context *context,
118624 int NotUsed,
118625 sqlite3_value **argv
118626){
118627 sqlite3 *db = sqlite3_context_db_handle(context);
118628 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
118629 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
118630
118631#ifndef SQLITE_OMIT_AUTHORIZATION
118632 sqlite3_xauth xAuth = db->xAuth;
118633 db->xAuth = 0;
118634#endif
118635
118636 sqlite3BtreeEnterAll(db);
118637
118638 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
118639 if( zDb && zInput ){
118640 int rc;
118641 Parse sParse;
118642 rc = renameParseSql(&sParse, zDb, db, zInput, 0);
118643
118644 if( rc==SQLITE_OK0 ){
118645 RenameCtx sCtx;
118646 Walker sWalker;
118647
118648 /* Walker to find tokens that need to be replaced. */
118649 memset(&sCtx, 0, sizeof(RenameCtx));
118650 memset(&sWalker, 0, sizeof(Walker));
118651 sWalker.pParse = &sParse;
118652 sWalker.xExprCallback = renameQuotefixExprCb;
118653 sWalker.xSelectCallback = renameColumnSelectCb;
118654 sWalker.u.pRename = &sCtx;
118655
118656 if( sParse.pNewTable ){
118657 if( IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
118658 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118659 pSelect->selFlags &= ~SF_View0x0200000;
118660 sParse.rc = SQLITE_OK0;
118661 sqlite3SelectPrep(&sParse, pSelect, 0);
118662 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
118663 if( rc==SQLITE_OK0 ){
118664 sqlite3WalkSelect(&sWalker, pSelect);
118665 }
118666 }else{
118667 int i;
118668 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
118669#ifndef SQLITE_OMIT_GENERATED_COLUMNS
118670 for(i=0; i<sParse.pNewTable->nCol; i++){
118671 sqlite3WalkExpr(&sWalker,
118672 sqlite3ColumnExpr(sParse.pNewTable,
118673 &sParse.pNewTable->aCol[i]));
118674 }
118675#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
118676 }
118677 }else if( sParse.pNewIndex ){
118678 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
118679 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
118680 }else{
118681#ifndef SQLITE_OMIT_TRIGGER
118682 rc = renameResolveTrigger(&sParse);
118683 if( rc==SQLITE_OK0 ){
118684 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
118685 }
118686#endif /* SQLITE_OMIT_TRIGGER */
118687 }
118688
118689 if( rc==SQLITE_OK0 ){
118690 rc = renameEditSql(context, &sCtx, zInput, 0, 0);
118691 }
118692 renameTokenFree(db, sCtx.pList);
118693 }
118694 if( rc!=SQLITE_OK0 ){
118695 if( sqlite3WritableSchema(db) && rc==SQLITE_ERROR1 ){
118696 sqlite3_result_value(context, argv[1]);
118697 }else{
118698 sqlite3_result_error_code(context, rc);
118699 }
118700 }
118701 renameParseCleanup(&sParse);
118702 }
118703
118704#ifndef SQLITE_OMIT_AUTHORIZATION
118705 db->xAuth = xAuth;
118706#endif
118707
118708 sqlite3BtreeLeaveAll(db);
118709}
118710
118711/* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS)
118712**
118713** An SQL user function that checks that there are no parse or symbol
118714** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
118715** After an ALTER TABLE .. RENAME operation is performed and the schema
118716** reloaded, this function is called on each SQL statement in the schema
118717** to ensure that it is still usable.
118718**
118719** 0: Database name ("main", "temp" etc.).
118720** 1: SQL statement.
118721** 2: Object type ("view", "table", "trigger" or "index").
118722** 3: Object name.
118723** 4: True if object is from temp schema.
118724** 5: "when" part of error message.
118725** 6: True to disable the DQS quirk when parsing SQL.
118726**
118727** The return value is computed as follows:
118728**
118729** A. If an error is seen and not in PRAGMA writable_schema=ON mode,
118730** then raise the error.
118731** B. Else if a trigger is created and the the table that the trigger is
118732** attached to is in database zDb, then return 1.
118733** C. Otherwise return NULL.
118734*/
118735static void renameTableTest(
118736 sqlite3_context *context,
118737 int NotUsed,
118738 sqlite3_value **argv
118739){
118740 sqlite3 *db = sqlite3_context_db_handle(context);
118741 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
118742 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
118743 int bTemp = sqlite3_value_int(argv[4]);
118744 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
118745 char const *zWhen = (const char*)sqlite3_value_text(argv[5]);
118746 int bNoDQS = sqlite3_value_int(argv[6]);
118747
118748#ifndef SQLITE_OMIT_AUTHORIZATION
118749 sqlite3_xauth xAuth = db->xAuth;
118750 db->xAuth = 0;
118751#endif
118752
118753 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
118754
118755 if( zDb && zInput ){
118756 int rc;
118757 Parse sParse;
118758 int flags = db->flags;
118759 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML0x40000000|SQLITE_DqsDDL0x20000000);
118760 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
118761 db->flags |= (flags & (SQLITE_DqsDML0x40000000|SQLITE_DqsDDL0x20000000));
118762 if( rc==SQLITE_OK0 ){
118763 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable)((sParse.pNewTable)->eTabType==2) ){
118764 NameContext sNC;
118765 memset(&sNC, 0, sizeof(sNC));
118766 sNC.pParse = &sParse;
118767 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
118768 if( sParse.nErr ) rc = sParse.rc;
118769 }
118770
118771 else if( sParse.pNewTrigger ){
118772 if( isLegacy==0 ){
118773 rc = renameResolveTrigger(&sParse);
118774 }
118775 if( rc==SQLITE_OK0 ){
118776 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
118777 int i2 = sqlite3FindDbName(db, zDb);
118778 if( i1==i2 ){
118779 /* Handle output case B */
118780 sqlite3_result_int(context, 1);
118781 }
118782 }
118783 }
118784 }
118785
118786 if( rc!=SQLITE_OK0 && zWhen && !sqlite3WritableSchema(db) ){
118787 /* Output case A */
118788 renameColumnParseError(context, zWhen, argv[2], argv[3],&sParse);
118789 }
118790 renameParseCleanup(&sParse);
118791 }
118792
118793#ifndef SQLITE_OMIT_AUTHORIZATION
118794 db->xAuth = xAuth;
118795#endif
118796}
118797
118798/*
118799** The implementation of internal UDF sqlite_drop_column().
118800**
118801** Arguments:
118802**
118803** argv[0]: An integer - the index of the schema containing the table
118804** argv[1]: CREATE TABLE statement to modify.
118805** argv[2]: An integer - the index of the column to remove.
118806**
118807** The value returned is a string containing the CREATE TABLE statement
118808** with column argv[2] removed.
118809*/
118810static void dropColumnFunc(
118811 sqlite3_context *context,
118812 int NotUsed,
118813 sqlite3_value **argv
118814){
118815 sqlite3 *db = sqlite3_context_db_handle(context);
118816 int iSchema = sqlite3_value_int(argv[0]);
118817 const char *zSql = (const char*)sqlite3_value_text(argv[1]);
118818 int iCol = sqlite3_value_int(argv[2]);
118819 const char *zDb = db->aDb[iSchema].zDbSName;
118820 int rc;
118821 Parse sParse;
118822 RenameToken *pCol;
118823 Table *pTab;
118824 const char *zEnd;
118825 char *zNew = 0;
118826
118827#ifndef SQLITE_OMIT_AUTHORIZATION
118828 sqlite3_xauth xAuth = db->xAuth;
118829 db->xAuth = 0;
118830#endif
118831
118832 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
118833 rc = renameParseSql(&sParse, zDb, db, zSql, iSchema==1);
118834 if( rc!=SQLITE_OK0 ) goto drop_column_done;
118835 pTab = sParse.pNewTable;
118836 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
118837 /* This can happen if the sqlite_schema table is corrupt */
118838 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(118838);
118839 goto drop_column_done;
118840 }
118841
118842 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
118843 if( iCol<pTab->nCol-1 ){
118844 RenameToken *pEnd;
118845 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
118846 zEnd = (const char*)pEnd->t.z;
118847 }else{
118848 assert( IsOrdinaryTable(pTab) )((void) (0));
118849 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
118850 while( ALWAYS(pCol->t.z[0]!=0)(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
118851 }
118852
118853 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
118854 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
118855 sqlite3_free(zNew);
118856
118857drop_column_done:
118858 renameParseCleanup(&sParse);
118859#ifndef SQLITE_OMIT_AUTHORIZATION
118860 db->xAuth = xAuth;
118861#endif
118862 if( rc!=SQLITE_OK0 ){
118863 sqlite3_result_error_code(context, rc);
118864 }
118865}
118866
118867/*
118868** This function is called by the parser upon parsing an
118869**
118870** ALTER TABLE pSrc DROP COLUMN pName
118871**
118872** statement. Argument pSrc contains the possibly qualified name of the
118873** table being edited, and token pName the name of the column to drop.
118874*/
118875SQLITE_PRIVATEstatic void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const Token *pName){
118876 sqlite3 *db = pParse->db; /* Database handle */
118877 Table *pTab; /* Table to modify */
118878 int iDb; /* Index of db containing pTab in aDb[] */
118879 const char *zDb; /* Database containing pTab ("main" etc.) */
118880 char *zCol = 0; /* Name of column to drop */
118881 int iCol; /* Index of column zCol in pTab->aCol[] */
118882
118883 /* Look up the table being altered. */
118884 assert( pParse->pNewTable==0 )((void) (0));
118885 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
118886 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_drop_column;
118887 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118888 if( !pTab ) goto exit_drop_column;
118889
118890 /* Make sure this is not an attempt to ALTER a view, virtual table or
118891 ** system table. */
118892 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_drop_column;
118893 if( SQLITE_OK0!=isRealTable(pParse, pTab, 1) ) goto exit_drop_column;
118894
118895 /* Find the index of the column being dropped. */
118896 zCol = sqlite3NameFromToken(db, pName);
118897 if( zCol==0 ){
118898 assert( db->mallocFailed )((void) (0));
118899 goto exit_drop_column;
118900 }
118901 iCol = sqlite3ColumnIndex(pTab, zCol);
118902 if( iCol<0 ){
118903 sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pName);
118904 goto exit_drop_column;
118905 }
118906
118907 /* Do not allow the user to drop a PRIMARY KEY column or a column
118908 ** constrained by a UNIQUE constraint. */
118909 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY0x0001|COLFLAG_UNIQUE0x0008) ){
118910 sqlite3ErrorMsg(pParse, "cannot drop %s column: \"%s\"",
118911 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY0x0001) ? "PRIMARY KEY" : "UNIQUE",
118912 zCol
118913 );
118914 goto exit_drop_column;
118915 }
118916
118917 /* Do not allow the number of columns to go to zero */
118918 if( pTab->nCol<=1 ){
118919 sqlite3ErrorMsg(pParse, "cannot drop column \"%s\": no other columns exist",zCol);
118920 goto exit_drop_column;
118921 }
118922
118923 /* Edit the sqlite_schema table */
118924 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118925 assert( iDb>=0 )((void) (0));
118926 zDb = db->aDb[iDb].zDbSName;
118927#ifndef SQLITE_OMIT_AUTHORIZATION
118928 /* Invoke the authorization callback. */
118929 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, zCol) ){
118930 goto exit_drop_column;
118931 }
118932#endif
118933 renameTestSchema(pParse, zDb, iDb==1, "", 0);
118934 renameFixQuotes(pParse, zDb, iDb==1);
118935 sqlite3NestedParse(pParse,
118936 "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE"sqlite_master" " SET "
118937 "sql = sqlite_drop_column(%d, sql, %d) "
118938 "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)"
118939 , zDb, iDb, iCol, pTab->zName
118940 );
118941
118942 /* Drop and reload the database schema. */
118943 renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop0x0002);
118944 renameTestSchema(pParse, zDb, iDb==1, "after drop column", 1);
118945
118946 /* Edit rows of table on disk */
118947 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
118948 int i;
118949 int addr;
118950 int reg;
118951 int regRec;
118952 Index *pPk = 0;
118953 int nField = 0; /* Number of non-virtual columns after drop */
118954 int iCur;
118955 Vdbe *v = sqlite3GetVdbe(pParse);
118956 iCur = pParse->nTab++;
118957 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite113);
118958 addr = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
118959 reg = ++pParse->nMem;
118960 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
118961 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, reg);
118962 pParse->nMem += pTab->nCol;
118963 }else{
118964 pPk = sqlite3PrimaryKeyIndex(pTab);
118965 pParse->nMem += pPk->nColumn;
118966 for(i=0; i<pPk->nKeyCol; i++){
118967 sqlite3VdbeAddOp3(v, OP_Column94, iCur, i, reg+i+1);
118968 }
118969 nField = pPk->nKeyCol;
118970 }
118971 regRec = ++pParse->nMem;
118972 for(i=0; i<pTab->nCol; i++){
118973 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
118974 int regOut;
118975 if( pPk ){
118976 int iPos = sqlite3TableColumnToIndex(pPk, i);
118977 int iColPos = sqlite3TableColumnToIndex(pPk, iCol);
118978 if( iPos<pPk->nKeyCol ) continue;
118979 regOut = reg+1+iPos-(iPos>iColPos);
118980 }else{
118981 regOut = reg+1+nField;
118982 }
118983 if( i==pTab->iPKey ){
118984 sqlite3VdbeAddOp2(v, OP_Null75, 0, regOut);
118985 }else{
118986 char aff = pTab->aCol[i].affinity;
118987 if( aff==SQLITE_AFF_REAL0x45 ){
118988 pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC0x43;
118989 }
118990 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
118991 pTab->aCol[i].affinity = aff;
118992 }
118993 nField++;
118994 }
118995 }
118996 if( nField==0 ){
118997 /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */
118998 pParse->nMem++;
118999 sqlite3VdbeAddOp2(v, OP_Null75, 0, reg+1);
119000 nField = 1;
119001 }
119002 sqlite3VdbeAddOp3(v, OP_MakeRecord97, reg+1, nField, regRec);
119003 if( pPk ){
119004 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iCur, regRec, reg+1, pPk->nKeyCol);
119005 }else{
119006 sqlite3VdbeAddOp3(v, OP_Insert128, iCur, regRec, reg);
119007 }
119008 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
119009
119010 sqlite3VdbeAddOp2(v, OP_Next39, iCur, addr+1); VdbeCoverage(v);
119011 sqlite3VdbeJumpHere(v, addr);
119012 }
119013
119014exit_drop_column:
119015 sqlite3DbFree(db, zCol);
119016 sqlite3SrcListDelete(db, pSrc);
119017}
119018
119019/*
119020** Register built-in functions used to help implement ALTER TABLE
119021*/
119022SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void){
119023 static FuncDef aAlterTableFuncs[] = {
119024 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc){9, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameColumnFunc, 0
, 0, 0, "sqlite_rename_column", {0} }
,
119025 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc){7, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameTableFunc, 0
, 0, 0, "sqlite_rename_table", {0} }
,
119026 INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest){7, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameTableTest, 0
, 0, 0, "sqlite_rename_test", {0} }
,
119027 INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc){3, 0x00800000| 0x00040000|1|0x0800, 0, 0, dropColumnFunc, 0,
0, 0, "sqlite_drop_column", {0} }
,
119028 INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc){2, 0x00800000| 0x00040000|1|0x0800, 0, 0, renameQuotefixFunc
, 0, 0, 0, "sqlite_rename_quotefix", {0} }
,
119029 };
119030 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)((int)(sizeof(aAlterTableFuncs)/sizeof(aAlterTableFuncs[0]))));
119031}
119032#endif /* SQLITE_ALTER_TABLE */
119033
119034/************** End of alter.c ***********************************************/
119035/************** Begin file analyze.c *****************************************/
119036/*
119037** 2005-07-08
119038**
119039** The author disclaims copyright to this source code. In place of
119040** a legal notice, here is a blessing:
119041**
119042** May you do good and not evil.
119043** May you find forgiveness for yourself and forgive others.
119044** May you share freely, never taking more than you give.
119045**
119046*************************************************************************
119047** This file contains code associated with the ANALYZE command.
119048**
119049** The ANALYZE command gather statistics about the content of tables
119050** and indices. These statistics are made available to the query planner
119051** to help it make better decisions about how to perform queries.
119052**
119053** The following system tables are or have been supported:
119054**
119055** CREATE TABLE sqlite_stat1(tbl, idx, stat);
119056** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
119057** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
119058** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
119059**
119060** Additional tables might be added in future releases of SQLite.
119061** The sqlite_stat2 table is not created or used unless the SQLite version
119062** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
119063** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
119064** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
119065** created and used by SQLite versions 3.7.9 through 3.29.0 when
119066** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
119067** is a superset of sqlite_stat2 and is also now deprecated. The
119068** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only
119069** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
119070** versions 3.8.1 and later. STAT4 is the only variant that is still
119071** supported.
119072**
119073** For most applications, sqlite_stat1 provides all the statistics required
119074** for the query planner to make good choices.
119075**
119076** Format of sqlite_stat1:
119077**
119078** There is normally one row per index, with the index identified by the
119079** name in the idx column. The tbl column is the name of the table to
119080** which the index belongs. In each such row, the stat column will be
119081** a string consisting of a list of integers. The first integer in this
119082** list is the number of rows in the index. (This is the same as the
119083** number of rows in the table, except for partial indices.) The second
119084** integer is the average number of rows in the index that have the same
119085** value in the first column of the index. The third integer is the average
119086** number of rows in the index that have the same value for the first two
119087** columns. The N-th integer (for N>1) is the average number of rows in
119088** the index which have the same value for the first N-1 columns. For
119089** a K-column index, there will be K+1 integers in the stat column. If
119090** the index is unique, then the last integer will be 1.
119091**
119092** The list of integers in the stat column can optionally be followed
119093** by the keyword "unordered". The "unordered" keyword, if it is present,
119094** must be separated from the last integer by a single space. If the
119095** "unordered" keyword is present, then the query planner assumes that
119096** the index is unordered and will not use the index for a range query.
119097**
119098** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
119099** column contains a single integer which is the (estimated) number of
119100** rows in the table identified by sqlite_stat1.tbl.
119101**
119102** Format of sqlite_stat2:
119103**
119104** The sqlite_stat2 is only created and is only used if SQLite is compiled
119105** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
119106** 3.6.18 and 3.7.8. The "stat2" table contains additional information
119107** about the distribution of keys within an index. The index is identified by
119108** the "idx" column and the "tbl" column is the name of the table to which
119109** the index belongs. There are usually 10 rows in the sqlite_stat2
119110** table for each index.
119111**
119112** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
119113** inclusive are samples of the left-most key value in the index taken at
119114** evenly spaced points along the index. Let the number of samples be S
119115** (10 in the standard build) and let C be the number of rows in the index.
119116** Then the sampled rows are given by:
119117**
119118** rownumber = (i*C*2 + C)/(S*2)
119119**
119120** For i between 0 and S-1. Conceptually, the index space is divided into
119121** S uniform buckets and the samples are the middle row from each bucket.
119122**
119123** The format for sqlite_stat2 is recorded here for legacy reference. This
119124** version of SQLite does not support sqlite_stat2. It neither reads nor
119125** writes the sqlite_stat2 table. This version of SQLite only supports
119126** sqlite_stat3.
119127**
119128** Format for sqlite_stat3:
119129**
119130** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
119131** sqlite_stat4 format will be described first. Further information
119132** about sqlite_stat3 follows the sqlite_stat4 description.
119133**
119134** Format for sqlite_stat4:
119135**
119136** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
119137** to aid the query planner in choosing good indices based on the values
119138** that indexed columns are compared against in the WHERE clauses of
119139** queries.
119140**
119141** The sqlite_stat4 table contains multiple entries for each index.
119142** The idx column names the index and the tbl column is the table of the
119143** index. If the idx and tbl columns are the same, then the sample is
119144** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
119145** binary encoding of a key from the index. The nEq column is a
119146** list of integers. The first integer is the approximate number
119147** of entries in the index whose left-most column exactly matches
119148** the left-most column of the sample. The second integer in nEq
119149** is the approximate number of entries in the index where the
119150** first two columns match the first two columns of the sample.
119151** And so forth. nLt is another list of integers that show the approximate
119152** number of entries that are strictly less than the sample. The first
119153** integer in nLt contains the number of entries in the index where the
119154** left-most column is less than the left-most column of the sample.
119155** The K-th integer in the nLt entry is the number of index entries
119156** where the first K columns are less than the first K columns of the
119157** sample. The nDLt column is like nLt except that it contains the
119158** number of distinct entries in the index that are less than the
119159** sample.
119160**
119161** There can be an arbitrary number of sqlite_stat4 entries per index.
119162** The ANALYZE command will typically generate sqlite_stat4 tables
119163** that contain between 10 and 40 samples which are distributed across
119164** the key space, though not uniformly, and which include samples with
119165** large nEq values.
119166**
119167** Format for sqlite_stat3 redux:
119168**
119169** The sqlite_stat3 table is like sqlite_stat4 except that it only
119170** looks at the left-most column of the index. The sqlite_stat3.sample
119171** column contains the actual value of the left-most column instead
119172** of a blob encoding of the complete index key as is found in
119173** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
119174** all contain just a single integer which is the same as the first
119175** integer in the equivalent columns in sqlite_stat4.
119176*/
119177#ifndef SQLITE_OMIT_ANALYZE
119178/* #include "sqliteInt.h" */
119179
119180#if defined(SQLITE_ENABLE_STAT4)
119181# define IsStat40 1
119182#else
119183# define IsStat40 0
119184# undef SQLITE_STAT4_SAMPLES1
119185# define SQLITE_STAT4_SAMPLES1 1
119186#endif
119187
119188/*
119189** This routine generates code that opens the sqlite_statN tables.
119190** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
119191** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
119192** appropriate compile-time options are provided.
119193**
119194** If the sqlite_statN tables do not previously exist, it is created.
119195**
119196** Argument zWhere may be a pointer to a buffer containing a table name,
119197** or it may be a NULL pointer. If it is not NULL, then all entries in
119198** the sqlite_statN tables associated with the named table are deleted.
119199** If zWhere==0, then code is generated to delete all stat table entries.
119200*/
119201static void openStatTable(
119202 Parse *pParse, /* Parsing context */
119203 int iDb, /* The database we are looking in */
119204 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
119205 const char *zWhere, /* Delete entries for this table or index */
119206 const char *zWhereType /* Either "tbl" or "idx" */
119207){
119208 static const struct {
119209 const char *zName;
119210 const char *zCols;
119211 } aTable[] = {
119212 { "sqlite_stat1", "tbl,idx,stat" },
119213#if defined(SQLITE_ENABLE_STAT4)
119214 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
119215#else
119216 { "sqlite_stat4", 0 },
119217#endif
119218 { "sqlite_stat3", 0 },
119219 };
119220 int i;
119221 sqlite3 *db = pParse->db;
119222 Db *pDb;
119223 Vdbe *v = sqlite3GetVdbe(pParse);
119224 u32 aRoot[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
119225 u8 aCreateTbl[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
119226#ifdef SQLITE_ENABLE_STAT4
119227 const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ? 2 : 1;
119228#else
119229 const int nToOpen = 1;
119230#endif
119231
119232 if( v==0 ) return;
119233 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
119234 assert( sqlite3VdbeDb(v)==db )((void) (0));
119235 pDb = &db->aDb[iDb];
119236
119237 /* Create new statistic tables if they do not exist, or clear them
119238 ** if they do already exist.
119239 */
119240 for(i=0; i<ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0]))); i++){
119241 const char *zTab = aTable[i].zName;
119242 Table *pStat;
119243 aCreateTbl[i] = 0;
119244 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
119245 if( i<nToOpen ){
119246 /* The sqlite_statN table does not exist. Create it. Note that a
119247 ** side-effect of the CREATE TABLE statement is to leave the rootpage
119248 ** of the new table in register pParse->regRoot. This is important
119249 ** because the OpenWrite opcode below will be needing it. */
119250 sqlite3NestedParse(pParse,
119251 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
119252 );
119253 aRoot[i] = (u32)pParse->regRoot;
119254 aCreateTbl[i] = OPFLAG_P2ISREG0x10;
119255 }
119256 }else{
119257 /* The table already exists. If zWhere is not NULL, delete all entries
119258 ** associated with the table zWhere. If zWhere is NULL, delete the
119259 ** entire contents of the table. */
119260 aRoot[i] = pStat->tnum;
119261 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
119262 if( zWhere ){
119263 sqlite3NestedParse(pParse,
119264 "DELETE FROM %Q.%s WHERE %s=%Q",
119265 pDb->zDbSName, zTab, zWhereType, zWhere
119266 );
119267#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
119268 }else if( db->xPreUpdateCallback ){
119269 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
119270#endif
119271 }else{
119272 /* The sqlite_stat[134] table already exists. Delete all rows. */
119273 sqlite3VdbeAddOp2(v, OP_Clear145, (int)aRoot[i], iDb);
119274 }
119275 }
119276 }
119277
119278 /* Open the sqlite_stat[134] tables for writing. */
119279 for(i=0; i<nToOpen; i++){
119280 assert( i<ArraySize(aTable) )((void) (0));
119281 sqlite3VdbeAddOp4Int(v, OP_OpenWrite113, iStatCur+i, (int)aRoot[i], iDb, 3);
119282 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
119283 VdbeComment((v, aTable[i].zName));
119284 }
119285}
119286
119287/*
119288** Recommended number of samples for sqlite_stat4
119289*/
119290#ifndef SQLITE_STAT4_SAMPLES1
119291# define SQLITE_STAT4_SAMPLES1 24
119292#endif
119293
119294/*
119295** Three SQL functions - stat_init(), stat_push(), and stat_get() -
119296** share an instance of the following structure to hold their state
119297** information.
119298*/
119299typedef struct StatAccum StatAccum;
119300typedef struct StatSample StatSample;
119301struct StatSample {
119302 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
119303#ifdef SQLITE_ENABLE_STAT4
119304 tRowcnt *anEq; /* sqlite_stat4.nEq */
119305 tRowcnt *anLt; /* sqlite_stat4.nLt */
119306 union {
119307 i64 iRowid; /* Rowid in main table of the key */
119308 u8 *aRowid; /* Key for WITHOUT ROWID tables */
119309 } u;
119310 u32 nRowid; /* Sizeof aRowid[] */
119311 u8 isPSample; /* True if a periodic sample */
119312 int iCol; /* If !isPSample, the reason for inclusion */
119313 u32 iHash; /* Tiebreaker hash */
119314#endif
119315};
119316struct StatAccum {
119317 sqlite3 *db; /* Database connection, for malloc() */
119318 tRowcnt nEst; /* Estimated number of rows */
119319 tRowcnt nRow; /* Number of rows visited so far */
119320 int nLimit; /* Analysis row-scan limit */
119321 int nCol; /* Number of columns in index + pk/rowid */
119322 int nKeyCol; /* Number of index columns w/o the pk/rowid */
119323 u8 nSkipAhead; /* Number of times of skip-ahead */
119324 StatSample current; /* Current row as a StatSample */
119325#ifdef SQLITE_ENABLE_STAT4
119326 tRowcnt nPSample; /* How often to do a periodic sample */
119327 int mxSample; /* Maximum number of samples to accumulate */
119328 u32 iPrn; /* Pseudo-random number used for sampling */
119329 StatSample *aBest; /* Array of nCol best samples */
119330 int iMin; /* Index in a[] of entry with minimum score */
119331 int nSample; /* Current number of samples */
119332 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
119333 int iGet; /* Index of current sample accessed by stat_get() */
119334 StatSample *a; /* Array of mxSample StatSample objects */
119335#endif
119336};
119337
119338/* Reclaim memory used by a StatSample
119339*/
119340#ifdef SQLITE_ENABLE_STAT4
119341static void sampleClear(sqlite3 *db, StatSample *p){
119342 assert( db!=0 )((void) (0));
119343 if( p->nRowid ){
119344 sqlite3DbFree(db, p->u.aRowid);
119345 p->nRowid = 0;
119346 }
119347}
119348#endif
119349
119350/* Initialize the BLOB value of a ROWID
119351*/
119352#ifdef SQLITE_ENABLE_STAT4
119353static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){
119354 assert( db!=0 )((void) (0));
119355 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
119356 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
119357 if( p->u.aRowid ){
119358 p->nRowid = n;
119359 memcpy(p->u.aRowid, pData, n);
119360 }else{
119361 p->nRowid = 0;
119362 }
119363}
119364#endif
119365
119366/* Initialize the INTEGER value of a ROWID.
119367*/
119368#ifdef SQLITE_ENABLE_STAT4
119369static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){
119370 assert( db!=0 )((void) (0));
119371 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
119372 p->nRowid = 0;
119373 p->u.iRowid = iRowid;
119374}
119375#endif
119376
119377
119378/*
119379** Copy the contents of object (*pFrom) into (*pTo).
119380*/
119381#ifdef SQLITE_ENABLE_STAT4
119382static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){
119383 pTo->isPSample = pFrom->isPSample;
119384 pTo->iCol = pFrom->iCol;
119385 pTo->iHash = pFrom->iHash;
119386 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
119387 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
119388 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
119389 if( pFrom->nRowid ){
119390 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
119391 }else{
119392 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
119393 }
119394}
119395#endif
119396
119397/*
119398** Reclaim all memory of a StatAccum structure.
119399*/
119400static void statAccumDestructor(void *pOld){
119401 StatAccum *p = (StatAccum*)pOld;
119402#ifdef SQLITE_ENABLE_STAT4
119403 if( p->mxSample ){
119404 int i;
119405 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
119406 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
119407 sampleClear(p->db, &p->current);
119408 }
119409#endif
119410 sqlite3DbFree(p->db, p);
119411}
119412
119413/*
119414** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
119415** are:
119416** N: The number of columns in the index including the rowid/pk (note 1)
119417** K: The number of columns in the index excluding the rowid/pk.
119418** C: Estimated number of rows in the index
119419** L: A limit on the number of rows to scan, or 0 for no-limit
119420**
119421** Note 1: In the special case of the covering index that implements a
119422** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
119423** total number of columns in the table.
119424**
119425** For indexes on ordinary rowid tables, N==K+1. But for indexes on
119426** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
119427** PRIMARY KEY of the table. The covering index that implements the
119428** original WITHOUT ROWID table as N==K as a special case.
119429**
119430** This routine allocates the StatAccum object in heap memory. The return
119431** value is a pointer to the StatAccum object. The datatype of the
119432** return value is BLOB, but it is really just a pointer to the StatAccum
119433** object.
119434*/
119435static void statInit(
119436 sqlite3_context *context,
119437 int argc,
119438 sqlite3_value **argv
119439){
119440 StatAccum *p;
119441 int nCol; /* Number of columns in index being sampled */
119442 int nKeyCol; /* Number of key columns */
119443 int nColUp; /* nCol rounded up for alignment */
119444 int n; /* Bytes of space to allocate */
119445 sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */
119446#ifdef SQLITE_ENABLE_STAT4
119447 /* Maximum number of samples. 0 if STAT4 data is not collected */
119448 int mxSample = OptimizationEnabled(db,SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ?SQLITE_STAT4_SAMPLES1 :0;
119449#endif
119450
119451 /* Decode the three function arguments */
119452 UNUSED_PARAMETER(argc)(void)(argc);
119453 nCol = sqlite3_value_int(argv[0]);
119454 assert( nCol>0 )((void) (0));
119455 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
119456 nKeyCol = sqlite3_value_int(argv[1]);
119457 assert( nKeyCol<=nCol )((void) (0));
119458 assert( nKeyCol>0 )((void) (0));
119459
119460 /* Allocate the space required for the StatAccum object */
119461 n = sizeof(*p)
119462 + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */
119463#ifdef SQLITE_ENABLE_STAT4
119464 n += sizeof(tRowcnt)*nColUp; /* StatAccum.anEq */
119465 if( mxSample ){
119466 n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */
119467 + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */
119468 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample);
119469 }
119470#endif
119471 p = sqlite3DbMallocZero(db, n);
119472 if( p==0 ){
119473 sqlite3_result_error_nomem(context);
119474 return;
119475 }
119476
119477 p->db = db;
119478 p->nEst = sqlite3_value_int64(argv[2]);
119479 p->nRow = 0;
119480 p->nLimit = sqlite3_value_int64(argv[3]);
119481 p->nCol = nCol;
119482 p->nKeyCol = nKeyCol;
119483 p->nSkipAhead = 0;
119484 p->current.anDLt = (tRowcnt*)&p[1];
119485
119486#ifdef SQLITE_ENABLE_STAT4
119487 p->current.anEq = &p->current.anDLt[nColUp];
119488 p->mxSample = p->nLimit==0 ? mxSample : 0;
119489 if( mxSample ){
119490 u8 *pSpace; /* Allocated space not yet assigned */
119491 int i; /* Used to iterate through p->aSample[] */
119492
119493 p->iGet = -1;
119494 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
119495 p->current.anLt = &p->current.anEq[nColUp];
119496 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
119497
119498 /* Set up the StatAccum.a[] and aBest[] arrays */
119499 p->a = (struct StatSample*)&p->current.anLt[nColUp];
119500 p->aBest = &p->a[mxSample];
119501 pSpace = (u8*)(&p->a[mxSample+nCol]);
119502 for(i=0; i<(mxSample+nCol); i++){
119503 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
119504 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
119505 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
119506 }
119507 assert( (pSpace - (u8*)p)==n )((void) (0));
119508
119509 for(i=0; i<nCol; i++){
119510 p->aBest[i].iCol = i;
119511 }
119512 }
119513#endif
119514
119515 /* Return a pointer to the allocated object to the caller. Note that
119516 ** only the pointer (the 2nd parameter) matters. The size of the object
119517 ** (given by the 3rd parameter) is never used and can be any positive
119518 ** value. */
119519 sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor);
119520}
119521static const FuncDef statInitFuncdef = {
119522 4, /* nArg */
119523 SQLITE_UTF81, /* funcFlags */
119524 0, /* pUserData */
119525 0, /* pNext */
119526 statInit, /* xSFunc */
119527 0, /* xFinalize */
119528 0, 0, /* xValue, xInverse */
119529 "stat_init", /* zName */
119530 {0}
119531};
119532
119533#ifdef SQLITE_ENABLE_STAT4
119534/*
119535** pNew and pOld are both candidate non-periodic samples selected for
119536** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
119537** considering only any trailing columns and the sample hash value, this
119538** function returns true if sample pNew is to be preferred over pOld.
119539** In other words, if we assume that the cardinalities of the selected
119540** column for pNew and pOld are equal, is pNew to be preferred over pOld.
119541**
119542** This function assumes that for each argument sample, the contents of
119543** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
119544*/
119545static int sampleIsBetterPost(
119546 StatAccum *pAccum,
119547 StatSample *pNew,
119548 StatSample *pOld
119549){
119550 int nCol = pAccum->nCol;
119551 int i;
119552 assert( pNew->iCol==pOld->iCol )((void) (0));
119553 for(i=pNew->iCol+1; i<nCol; i++){
119554 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
119555 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
119556 }
119557 if( pNew->iHash>pOld->iHash ) return 1;
119558 return 0;
119559}
119560#endif
119561
119562#ifdef SQLITE_ENABLE_STAT4
119563/*
119564** Return true if pNew is to be preferred over pOld.
119565**
119566** This function assumes that for each argument sample, the contents of
119567** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
119568*/
119569static int sampleIsBetter(
119570 StatAccum *pAccum,
119571 StatSample *pNew,
119572 StatSample *pOld
119573){
119574 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
119575 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
119576
119577 assert( pOld->isPSample==0 && pNew->isPSample==0 )((void) (0));
119578 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) )((void) (0));
119579
119580 if( (nEqNew>nEqOld) ) return 1;
119581 if( nEqNew==nEqOld ){
119582 if( pNew->iCol<pOld->iCol ) return 1;
119583 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
119584 }
119585 return 0;
119586}
119587
119588/*
119589** Copy the contents of sample *pNew into the p->a[] array. If necessary,
119590** remove the least desirable sample from p->a[] to make room.
119591*/
119592static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){
119593 StatSample *pSample = 0;
119594 int i;
119595
119596 assert( IsStat4 || nEqZero==0 )((void) (0));
119597
119598 /* StatAccum.nMaxEqZero is set to the maximum number of leading 0
119599 ** values in the anEq[] array of any sample in StatAccum.a[]. In
119600 ** other words, if nMaxEqZero is n, then it is guaranteed that there
119601 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
119602 if( nEqZero>p->nMaxEqZero ){
119603 p->nMaxEqZero = nEqZero;
119604 }
119605 if( pNew->isPSample==0 ){
119606 StatSample *pUpgrade = 0;
119607 assert( pNew->anEq[pNew->iCol]>0 )((void) (0));
119608
119609 /* This sample is being added because the prefix that ends in column
119610 ** iCol occurs many times in the table. However, if we have already
119611 ** added a sample that shares this prefix, there is no need to add
119612 ** this one. Instead, upgrade the priority of the highest priority
119613 ** existing sample that shares this prefix. */
119614 for(i=p->nSample-1; i>=0; i--){
119615 StatSample *pOld = &p->a[i];
119616 if( pOld->anEq[pNew->iCol]==0 ){
119617 if( pOld->isPSample ) return;
119618 assert( pOld->iCol>pNew->iCol )((void) (0));
119619 assert( sampleIsBetter(p, pNew, pOld) )((void) (0));
119620 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
119621 pUpgrade = pOld;
119622 }
119623 }
119624 }
119625 if( pUpgrade ){
119626 pUpgrade->iCol = pNew->iCol;
119627 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
119628 goto find_new_min;
119629 }
119630 }
119631
119632 /* If necessary, remove sample iMin to make room for the new sample. */
119633 if( p->nSample>=p->mxSample ){
119634 StatSample *pMin = &p->a[p->iMin];
119635 tRowcnt *anEq = pMin->anEq;
119636 tRowcnt *anLt = pMin->anLt;
119637 tRowcnt *anDLt = pMin->anDLt;
119638 sampleClear(p->db, pMin);
119639 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
119640 pSample = &p->a[p->nSample-1];
119641 pSample->nRowid = 0;
119642 pSample->anEq = anEq;
119643 pSample->anDLt = anDLt;
119644 pSample->anLt = anLt;
119645 p->nSample = p->mxSample-1;
119646 }
119647
119648 /* The "rows less-than" for the rowid column must be greater than that
119649 ** for the last sample in the p->a[] array. Otherwise, the samples would
119650 ** be out of order. */
119651 assert( p->nSample==0((void) (0))
119652 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] )((void) (0));
119653
119654 /* Insert the new sample */
119655 pSample = &p->a[p->nSample];
119656 sampleCopy(p, pSample, pNew);
119657 p->nSample++;
119658
119659 /* Zero the first nEqZero entries in the anEq[] array. */
119660 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
119661
119662find_new_min:
119663 if( p->nSample>=p->mxSample ){
119664 int iMin = -1;
119665 for(i=0; i<p->mxSample; i++){
119666 if( p->a[i].isPSample ) continue;
119667 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
119668 iMin = i;
119669 }
119670 }
119671 assert( iMin>=0 )((void) (0));
119672 p->iMin = iMin;
119673 }
119674}
119675#endif /* SQLITE_ENABLE_STAT4 */
119676
119677#ifdef SQLITE_ENABLE_STAT4
119678/*
119679** Field iChng of the index being scanned has changed. So at this point
119680** p->current contains a sample that reflects the previous row of the
119681** index. The value of anEq[iChng] and subsequent anEq[] elements are
119682** correct at this point.
119683*/
119684static void samplePushPrevious(StatAccum *p, int iChng){
119685 int i;
119686
119687 /* Check if any samples from the aBest[] array should be pushed
119688 ** into IndexSample.a[] at this point. */
119689 for(i=(p->nCol-2); i>=iChng; i--){
119690 StatSample *pBest = &p->aBest[i];
119691 pBest->anEq[i] = p->current.anEq[i];
119692 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
119693 sampleInsert(p, pBest, i);
119694 }
119695 }
119696
119697 /* Check that no sample contains an anEq[] entry with an index of
119698 ** p->nMaxEqZero or greater set to zero. */
119699 for(i=p->nSample-1; i>=0; i--){
119700 int j;
119701 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 )((void) (0));
119702 }
119703
119704 /* Update the anEq[] fields of any samples already collected. */
119705 if( iChng<p->nMaxEqZero ){
119706 for(i=p->nSample-1; i>=0; i--){
119707 int j;
119708 for(j=iChng; j<p->nCol; j++){
119709 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
119710 }
119711 }
119712 p->nMaxEqZero = iChng;
119713 }
119714}
119715#endif /* SQLITE_ENABLE_STAT4 */
119716
119717/*
119718** Implementation of the stat_push SQL function: stat_push(P,C,R)
119719** Arguments:
119720**
119721** P Pointer to the StatAccum object created by stat_init()
119722** C Index of left-most column to differ from previous row
119723** R Rowid for the current row. Might be a key record for
119724** WITHOUT ROWID tables.
119725**
119726** The purpose of this routine is to collect statistical data and/or
119727** samples from the index being analyzed into the StatAccum object.
119728** The stat_get() SQL function will be used afterwards to
119729** retrieve the information gathered.
119730**
119731** This SQL function usually returns NULL, but might return an integer
119732** if it wants the byte-code to do special processing.
119733**
119734** The R parameter is only used for STAT4
119735*/
119736static void statPush(
119737 sqlite3_context *context,
119738 int argc,
119739 sqlite3_value **argv
119740){
119741 int i;
119742
119743 /* The three function arguments */
119744 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
119745 int iChng = sqlite3_value_int(argv[1]);
119746
119747 UNUSED_PARAMETER( argc )(void)(argc);
119748 UNUSED_PARAMETER( context )(void)(context);
119749 assert( p->nCol>0 )((void) (0));
119750 assert( iChng<p->nCol )((void) (0));
119751
119752 if( p->nRow==0 ){
119753 /* This is the first call to this function. Do initialization. */
119754#ifdef SQLITE_ENABLE_STAT4
119755 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
119756#endif
119757 }else{
119758 /* Second and subsequent calls get processed here */
119759#ifdef SQLITE_ENABLE_STAT4
119760 if( p->mxSample ) samplePushPrevious(p, iChng);
119761#endif
119762
119763 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
119764 ** to the current row of the index. */
119765#ifdef SQLITE_ENABLE_STAT4
119766 for(i=0; i<iChng; i++){
119767 p->current.anEq[i]++;
119768 }
119769#endif
119770 for(i=iChng; i<p->nCol; i++){
119771 p->current.anDLt[i]++;
119772#ifdef SQLITE_ENABLE_STAT4
119773 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
119774 p->current.anEq[i] = 1;
119775#endif
119776 }
119777 }
119778
119779 p->nRow++;
119780#ifdef SQLITE_ENABLE_STAT4
119781 if( p->mxSample ){
119782 tRowcnt nLt;
119783 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER1 ){
119784 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
119785 }else{
119786 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
119787 sqlite3_value_blob(argv[2]));
119788 }
119789 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
119790
119791 nLt = p->current.anLt[p->nCol-1];
119792 /* Check if this is to be a periodic sample. If so, add it. */
119793 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
119794 p->current.isPSample = 1;
119795 p->current.iCol = 0;
119796 sampleInsert(p, &p->current, p->nCol-1);
119797 p->current.isPSample = 0;
119798 }
119799
119800 /* Update the aBest[] array. */
119801 for(i=0; i<(p->nCol-1); i++){
119802 p->current.iCol = i;
119803 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
119804 sampleCopy(p, &p->aBest[i], &p->current);
119805 }
119806 }
119807 }else
119808#endif
119809 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
119810 p->nSkipAhead++;
119811 sqlite3_result_int(context, p->current.anDLt[0]>0);
119812 }
119813}
119814
119815static const FuncDef statPushFuncdef = {
119816 2+IsStat40, /* nArg */
119817 SQLITE_UTF81, /* funcFlags */
119818 0, /* pUserData */
119819 0, /* pNext */
119820 statPush, /* xSFunc */
119821 0, /* xFinalize */
119822 0, 0, /* xValue, xInverse */
119823 "stat_push", /* zName */
119824 {0}
119825};
119826
119827#define STAT_GET_STAT10 0 /* "stat" column of stat1 table */
119828#define STAT_GET_ROWID1 1 /* "rowid" column of stat[34] entry */
119829#define STAT_GET_NEQ2 2 /* "neq" column of stat[34] entry */
119830#define STAT_GET_NLT3 3 /* "nlt" column of stat[34] entry */
119831#define STAT_GET_NDLT4 4 /* "ndlt" column of stat[34] entry */
119832
119833/*
119834** Implementation of the stat_get(P,J) SQL function. This routine is
119835** used to query statistical information that has been gathered into
119836** the StatAccum object by prior calls to stat_push(). The P parameter
119837** has type BLOB but it is really just a pointer to the StatAccum object.
119838** The content to returned is determined by the parameter J
119839** which is one of the STAT_GET_xxxx values defined above.
119840**
119841** The stat_get(P,J) function is not available to generic SQL. It is
119842** inserted as part of a manually constructed bytecode program. (See
119843** the callStatGet() routine below.) It is guaranteed that the P
119844** parameter will always be a pointer to a StatAccum object, never a
119845** NULL.
119846**
119847** If STAT4 is not enabled, then J is always
119848** STAT_GET_STAT1 and is hence omitted and this routine becomes
119849** a one-parameter function, stat_get(P), that always returns the
119850** stat1 table entry information.
119851*/
119852static void statGet(
119853 sqlite3_context *context,
119854 int argc,
119855 sqlite3_value **argv
119856){
119857 StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]);
119858#ifdef SQLITE_ENABLE_STAT4
119859 /* STAT4 has a parameter on this routine. */
119860 int eCall = sqlite3_value_int(argv[1]);
119861 assert( argc==2 )((void) (0));
119862 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ((void) (0))
119863 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT((void) (0))
119864 || eCall==STAT_GET_NDLT((void) (0))
119865 )((void) (0));
119866 assert( eCall==STAT_GET_STAT1 || p->mxSample )((void) (0));
119867 if( eCall==STAT_GET_STAT10 )
119868#else
119869 assert( argc==1 )((void) (0));
119870#endif
119871 {
119872 /* Return the value to store in the "stat" column of the sqlite_stat1
119873 ** table for this index.
119874 **
119875 ** The value is a string composed of a list of integers describing
119876 ** the index. The first integer in the list is the total number of
119877 ** entries in the index. There is one additional integer in the list
119878 ** for each indexed column. This additional integer is an estimate of
119879 ** the number of rows matched by a equality query on the index using
119880 ** a key with the corresponding number of fields. In other words,
119881 ** if the index is on columns (a,b) and the sqlite_stat1 value is
119882 ** "100 10 2", then SQLite estimates that:
119883 **
119884 ** * the index contains 100 rows,
119885 ** * "WHERE a=?" matches 10 rows, and
119886 ** * "WHERE a=? AND b=?" matches 2 rows.
119887 **
119888 ** If D is the count of distinct values and K is the total number of
119889 ** rows, then each estimate is usually computed as:
119890 **
119891 ** I = (K+D-1)/D
119892 **
119893 ** In other words, I is K/D rounded up to the next whole integer.
119894 ** However, if I is between 1.0 and 1.1 (in other words if I is
119895 ** close to 1.0 but just a little larger) then do not round up but
119896 ** instead keep the I value at 1.0.
119897 */
119898 sqlite3_str sStat; /* Text of the constructed "stat" line */
119899 int i; /* Loop counter */
119900
119901 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
119902 sqlite3_str_appendf(&sStat, "%llu",
119903 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
119904 for(i=0; i<p->nKeyCol; i++){
119905 u64 nDistinct = p->current.anDLt[i] + 1;
119906 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
119907 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
119908 sqlite3_str_appendf(&sStat, " %llu", iVal);
119909#ifdef SQLITE_ENABLE_STAT4
119910 assert( p->current.anEq[i] || p->nRow==0 )((void) (0));
119911#endif
119912 }
119913 sqlite3ResultStrAccum(context, &sStat);
119914 }
119915#ifdef SQLITE_ENABLE_STAT4
119916 else if( eCall==STAT_GET_ROWID1 ){
119917 if( p->iGet<0 ){
119918 samplePushPrevious(p, 0);
119919 p->iGet = 0;
119920 }
119921 if( p->iGet<p->nSample ){
119922 StatSample *pS = p->a + p->iGet;
119923 if( pS->nRowid==0 ){
119924 sqlite3_result_int64(context, pS->u.iRowid);
119925 }else{
119926 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
119927 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
119928 }
119929 }
119930 }else{
119931 tRowcnt *aCnt = 0;
119932 sqlite3_str sStat;
119933 int i;
119934
119935 assert( p->iGet<p->nSample )((void) (0));
119936 switch( eCall ){
119937 case STAT_GET_NEQ2: aCnt = p->a[p->iGet].anEq; break;
119938 case STAT_GET_NLT3: aCnt = p->a[p->iGet].anLt; break;
119939 default: {
119940 aCnt = p->a[p->iGet].anDLt;
119941 p->iGet++;
119942 break;
119943 }
119944 }
119945 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
119946 for(i=0; i<p->nCol; i++){
119947 sqlite3_str_appendf(&sStat, "%llu ", (u64)aCnt[i]);
119948 }
119949 if( sStat.nChar ) sStat.nChar--;
119950 sqlite3ResultStrAccum(context, &sStat);
119951 }
119952#endif /* SQLITE_ENABLE_STAT4 */
119953#ifndef SQLITE_DEBUG
119954 UNUSED_PARAMETER( argc )(void)(argc);
119955#endif
119956}
119957static const FuncDef statGetFuncdef = {
119958 1+IsStat40, /* nArg */
119959 SQLITE_UTF81, /* funcFlags */
119960 0, /* pUserData */
119961 0, /* pNext */
119962 statGet, /* xSFunc */
119963 0, /* xFinalize */
119964 0, 0, /* xValue, xInverse */
119965 "stat_get", /* zName */
119966 {0}
119967};
119968
119969static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){
119970#ifdef SQLITE_ENABLE_STAT4
119971 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer71, iParam, regStat+1);
119972#elif SQLITE_DEBUG
119973 assert( iParam==STAT_GET_STAT1 )((void) (0));
119974#else
119975 UNUSED_PARAMETER( iParam )(void)(iParam);
119976#endif
119977 assert( regOut!=regStat && regOut!=regStat+1 )((void) (0));
119978 sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat40,
119979 &statGetFuncdef, 0);
119980}
119981
119982#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
119983/* Add a comment to the most recent VDBE opcode that is the name
119984** of the k-th column of the pIdx index.
119985*/
119986static void analyzeVdbeCommentIndexWithColumnName(
119987 Vdbe *v, /* Prepared statement under construction */
119988 Index *pIdx, /* Index whose column is being loaded */
119989 int k /* Which column index */
119990){
119991 int i; /* Index of column in the table */
119992 assert( k>=0 && k<pIdx->nColumn )((void) (0));
119993 i = pIdx->aiColumn[k];
119994 if( NEVER(i==XN_ROWID)(i==(-1)) ){
119995 VdbeComment((v,"%s.rowid",pIdx->zName));
119996 }else if( i==XN_EXPR(-2) ){
119997 assert( pIdx->bHasExpr )((void) (0));
119998 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
119999 }else{
120000 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
120001 }
120002}
120003#else
120004# define analyzeVdbeCommentIndexWithColumnName(a,b,c)
120005#endif /* SQLITE_DEBUG */
120006
120007/*
120008** Generate code to do an analysis of all indices associated with
120009** a single table.
120010*/
120011static void analyzeOneTable(
120012 Parse *pParse, /* Parser context */
120013 Table *pTab, /* Table whose indices are to be analyzed */
120014 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
120015 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
120016 int iMem, /* Available memory locations begin here */
120017 int iTab /* Next available cursor */
120018){
120019 sqlite3 *db = pParse->db; /* Database handle */
120020 Index *pIdx; /* An index to being analyzed */
120021 int iIdxCur; /* Cursor open on index being analyzed */
120022 int iTabCur; /* Table cursor */
120023 Vdbe *v; /* The virtual machine being built up */
120024 int i; /* Loop counter */
120025 int jZeroRows = -1; /* Jump from here if number of rows is zero */
120026 int iDb; /* Index of database containing pTab */
120027 u8 needTableCnt = 1; /* True to count the table */
120028 int regNewRowid = iMem++; /* Rowid for the inserted record */
120029 int regStat = iMem++; /* Register to hold StatAccum object */
120030 int regChng = iMem++; /* Index of changed index field */
120031 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
120032 int regTemp = iMem++; /* Temporary use register */
120033 int regTemp2 = iMem++; /* Second temporary use register */
120034 int regTabname = iMem++; /* Register containing table name */
120035 int regIdxname = iMem++; /* Register containing index name */
120036 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
120037 int regPrev = iMem; /* MUST BE LAST (see below) */
120038#ifdef SQLITE_ENABLE_STAT4
120039 int doOnce = 1; /* Flag for a one-time computation */
120040#endif
120041#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
120042 Table *pStat1 = 0;
120043#endif
120044
120045 sqlite3TouchRegister(pParse, iMem);
120046 assert( sqlite3NoTempsInRange(pParse, regNewRowid, iMem) )((void) (0));
120047 v = sqlite3GetVdbe(pParse);
120048 if( v==0 || NEVER(pTab==0)(pTab==0) ){
120049 return;
120050 }
120051 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
120052 /* Do not gather statistics on views or virtual tables */
120053 return;
120054 }
120055 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
120056 /* Do not gather statistics on system tables */
120057 return;
120058 }
120059 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
120060 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120061 assert( iDb>=0 )((void) (0));
120062 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
120063#ifndef SQLITE_OMIT_AUTHORIZATION
120064 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE28, pTab->zName, 0,
120065 db->aDb[iDb].zDbSName ) ){
120066 return;
120067 }
120068#endif
120069
120070#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
120071 if( db->xPreUpdateCallback ){
120072 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
120073 if( pStat1==0 ) return;
120074 pStat1->zName = (char*)&pStat1[1];
120075 memcpy(pStat1->zName, "sqlite_stat1", 13);
120076 pStat1->nCol = 3;
120077 pStat1->iPKey = -1;
120078 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop187, 0, 0, 0,(char*)pStat1,P4_DYNAMIC(-6));
120079 }
120080#endif
120081
120082 /* Establish a read-lock on the table at the shared-cache level.
120083 ** Open a read-only cursor on the table. Also allocate a cursor number
120084 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
120085 ** this time though. */
120086 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
120087 iTabCur = iTab++;
120088 iIdxCur = iTab++;
120089 pParse->nTab = MAX(pParse->nTab, iTab)((pParse->nTab)>(iTab)?(pParse->nTab):(iTab));
120090 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead102);
120091 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
120092
120093 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120094 int nCol; /* Number of columns in pIdx. "N" */
120095 int addrGotoEnd; /* Address of "OP_Rewind iIdxCur" */
120096 int addrNextRow; /* Address of "next_row:" */
120097 const char *zIdxName; /* Name of the index */
120098 int nColTest; /* Number of columns to test for changes */
120099
120100 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
120101 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
120102 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
120103 nCol = pIdx->nKeyCol;
120104 zIdxName = pTab->zName;
120105 nColTest = nCol - 1;
120106 }else{
120107 nCol = pIdx->nColumn;
120108 zIdxName = pIdx->zName;
120109 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
120110 }
120111
120112 /* Populate the register containing the index name. */
120113 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
120114 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
120115
120116 /*
120117 ** Pseudo-code for loop that calls stat_push():
120118 **
120119 ** regChng = 0
120120 ** Rewind csr
120121 ** if eof(csr){
120122 ** stat_init() with count = 0;
120123 ** goto end_of_scan;
120124 ** }
120125 ** count()
120126 ** stat_init()
120127 ** goto chng_addr_0;
120128 **
120129 ** next_row:
120130 ** regChng = 0
120131 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
120132 ** regChng = 1
120133 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
120134 ** ...
120135 ** regChng = N
120136 ** goto chng_addr_N
120137 **
120138 ** chng_addr_0:
120139 ** regPrev(0) = idx(0)
120140 ** chng_addr_1:
120141 ** regPrev(1) = idx(1)
120142 ** ...
120143 **
120144 ** endDistinctTest:
120145 ** regRowid = idx(rowid)
120146 ** stat_push(P, regChng, regRowid)
120147 ** Next csr
120148 ** if !eof(csr) goto next_row;
120149 **
120150 ** end_of_scan:
120151 */
120152
120153 /* Make sure there are enough memory cells allocated to accommodate
120154 ** the regPrev array and a trailing rowid (the rowid slot is required
120155 ** when building a record to insert into the sample column of
120156 ** the sqlite_stat4 table. */
120157 sqlite3TouchRegister(pParse, regPrev+nColTest);
120158
120159 /* Open a read-only cursor on the index being analyzed. */
120160 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) )((void) (0));
120161 sqlite3VdbeAddOp3(v, OP_OpenRead102, iIdxCur, pIdx->tnum, iDb);
120162 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
120163 VdbeComment((v, "%s", pIdx->zName));
120164
120165 /* Implementation of the following:
120166 **
120167 ** regChng = 0
120168 ** Rewind csr
120169 ** if eof(csr){
120170 ** stat_init() with count = 0;
120171 ** goto end_of_scan;
120172 ** }
120173 ** count()
120174 ** stat_init()
120175 ** goto chng_addr_0;
120176 */
120177 assert( regTemp2==regStat+4 )((void) (0));
120178 sqlite3VdbeAddOp2(v, OP_Integer71, db->nAnalysisLimit, regTemp2);
120179
120180 /* Arguments to stat_init():
120181 ** (1) the number of columns in the index including the rowid
120182 ** (or for a WITHOUT ROWID table, the number of PK columns),
120183 ** (2) the number of columns in the key without the rowid/pk
120184 ** (3) estimated number of rows in the index. */
120185 sqlite3VdbeAddOp2(v, OP_Integer71, nCol, regStat+1);
120186 assert( regRowid==regStat+2 )((void) (0));
120187 sqlite3VdbeAddOp2(v, OP_Integer71, pIdx->nKeyCol, regRowid);
120188 sqlite3VdbeAddOp3(v, OP_Count98, iIdxCur, regTemp,
120189 OptimizationDisabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))!=0));
120190 sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4,
120191 &statInitFuncdef, 0);
120192 addrGotoEnd = sqlite3VdbeAddOp1(v, OP_Rewind36, iIdxCur);
120193 VdbeCoverage(v);
120194
120195 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regChng);
120196 addrNextRow = sqlite3VdbeCurrentAddr(v);
120197
120198 if( nColTest>0 ){
120199 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
120200 int *aGotoChng; /* Array of jump instruction addresses */
120201 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
120202 if( aGotoChng==0 ) continue;
120203
120204 /*
120205 ** next_row:
120206 ** regChng = 0
120207 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
120208 ** regChng = 1
120209 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
120210 ** ...
120211 ** regChng = N
120212 ** goto endDistinctTest
120213 */
120214 sqlite3VdbeAddOp0(v, OP_Goto9);
120215 addrNextRow = sqlite3VdbeCurrentAddr(v);
120216 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
120217 /* For a single-column UNIQUE index, once we have found a non-NULL
120218 ** row, we know that all the rest will be distinct, so skip
120219 ** subsequent distinctness tests. */
120220 sqlite3VdbeAddOp2(v, OP_NotNull52, regPrev, endDistinctTest);
120221 VdbeCoverage(v);
120222 }
120223 for(i=0; i<nColTest; i++){
120224 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
120225 sqlite3VdbeAddOp2(v, OP_Integer71, i, regChng);
120226 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, i, regTemp);
120227 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
120228 aGotoChng[i] =
120229 sqlite3VdbeAddOp4(v, OP_Ne53, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ(-2));
120230 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
120231 VdbeCoverage(v);
120232 }
120233 sqlite3VdbeAddOp2(v, OP_Integer71, nColTest, regChng);
120234 sqlite3VdbeGoto(v, endDistinctTest);
120235
120236
120237 /*
120238 ** chng_addr_0:
120239 ** regPrev(0) = idx(0)
120240 ** chng_addr_1:
120241 ** regPrev(1) = idx(1)
120242 ** ...
120243 */
120244 sqlite3VdbeJumpHere(v, addrNextRow-1);
120245 for(i=0; i<nColTest; i++){
120246 sqlite3VdbeJumpHere(v, aGotoChng[i]);
120247 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, i, regPrev+i);
120248 analyzeVdbeCommentIndexWithColumnName(v,pIdx,i);
120249 }
120250 sqlite3VdbeResolveLabel(v, endDistinctTest);
120251 sqlite3DbFree(db, aGotoChng);
120252 }
120253
120254 /*
120255 ** chng_addr_N:
120256 ** regRowid = idx(rowid) // STAT4 only
120257 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only
120258 ** Next csr
120259 ** if !eof(csr) goto next_row;
120260 */
120261#ifdef SQLITE_ENABLE_STAT4
120262 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) ){
120263 assert( regRowid==(regStat+2) )((void) (0));
120264 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
120265 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iIdxCur, regRowid);
120266 }else{
120267 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
120268 int j, k, regKey;
120269 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
120270 for(j=0; j<pPk->nKeyCol; j++){
120271 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
120272 assert( k>=0 && k<pIdx->nColumn )((void) (0));
120273 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, k, regKey+j);
120274 analyzeVdbeCommentIndexWithColumnName(v,pIdx,k);
120275 }
120276 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regKey, pPk->nKeyCol, regRowid);
120277 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
120278 }
120279 }
120280#endif
120281 assert( regChng==(regStat+1) )((void) (0));
120282 {
120283 sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat40,
120284 &statPushFuncdef, 0);
120285 if( db->nAnalysisLimit ){
120286 int j1, j2, j3;
120287 j1 = sqlite3VdbeAddOp1(v, OP_IsNull51, regTemp); VdbeCoverage(v);
120288 j2 = sqlite3VdbeAddOp1(v, OP_If16, regTemp); VdbeCoverage(v);
120289 j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT24, iIdxCur, 0, regPrev, 1);
120290 VdbeCoverage(v);
120291 sqlite3VdbeJumpHere(v, j1);
120292 sqlite3VdbeAddOp2(v, OP_Next39, iIdxCur, addrNextRow); VdbeCoverage(v);
120293 sqlite3VdbeJumpHere(v, j2);
120294 sqlite3VdbeJumpHere(v, j3);
120295 }else{
120296 sqlite3VdbeAddOp2(v, OP_Next39, iIdxCur, addrNextRow); VdbeCoverage(v);
120297 }
120298 }
120299
120300 /* Add the entry to the stat1 table. */
120301 if( pIdx->pPartIdxWhere ){
120302 /* Partial indexes might get a zero-entry in sqlite_stat1. But
120303 ** an empty table is omitted from sqlite_stat1. */
120304 sqlite3VdbeJumpHere(v, addrGotoEnd);
120305 addrGotoEnd = 0;
120306 }
120307 callStatGet(pParse, regStat, STAT_GET_STAT10, regStat1);
120308 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
120309 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regTabname, 3, regTemp, "BBB", 0);
120310 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur, regNewRowid);
120311 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur, regTemp, regNewRowid);
120312#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
120313 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-5));
120314#endif
120315 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
120316
120317 /* Add the entries to the stat4 table. */
120318#ifdef SQLITE_ENABLE_STAT4
120319 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0) && db->nAnalysisLimit==0 ){
120320 int regEq = regStat1;
120321 int regLt = regStat1+1;
120322 int regDLt = regStat1+2;
120323 int regSample = regStat1+3;
120324 int regCol = regStat1+4;
120325 int regSampleRowid = regCol + nCol;
120326 int addrNext;
120327 int addrIsNull;
120328 u8 seekOp = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? OP_NotExists31 : OP_NotFound28;
120329
120330 /* No STAT4 data is generated if the number of rows is zero */
120331 if( addrGotoEnd==0 ){
120332 sqlite3VdbeAddOp2(v, OP_Cast88, regStat1, SQLITE_AFF_INTEGER0x44);
120333 addrGotoEnd = sqlite3VdbeAddOp1(v, OP_IfNot17, regStat1);
120334 VdbeCoverage(v);
120335 }
120336
120337 if( doOnce ){
120338 int mxCol = nCol;
120339 Index *pX;
120340
120341 /* Compute the maximum number of columns in any index */
120342 for(pX=pTab->pIndex; pX; pX=pX->pNext){
120343 int nColX; /* Number of columns in pX */
120344 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pX)((pX)->idxType==2) ){
120345 nColX = pX->nKeyCol;
120346 }else{
120347 nColX = pX->nColumn;
120348 }
120349 if( nColX>mxCol ) mxCol = nColX;
120350 }
120351
120352 /* Allocate space to compute results for the largest index */
120353 sqlite3TouchRegister(pParse, regCol+mxCol);
120354 doOnce = 0;
120355#ifdef SQLITE_DEBUG
120356 /* Verify that the call to sqlite3ClearTempRegCache() below
120357 ** really is needed.
120358 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
120359 */
120360 testcase( !sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) );
120361#endif
120362 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
120363 assert( sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) )((void) (0));
120364 }
120365 assert( sqlite3NoTempsInRange(pParse, regEq, regCol+nCol) )((void) (0));
120366
120367 addrNext = sqlite3VdbeCurrentAddr(v);
120368 callStatGet(pParse, regStat, STAT_GET_ROWID1, regSampleRowid);
120369 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull51, regSampleRowid);
120370 VdbeCoverage(v);
120371 callStatGet(pParse, regStat, STAT_GET_NEQ2, regEq);
120372 callStatGet(pParse, regStat, STAT_GET_NLT3, regLt);
120373 callStatGet(pParse, regStat, STAT_GET_NDLT4, regDLt);
120374 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
120375 VdbeCoverage(v);
120376 for(i=0; i<nCol; i++){
120377 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
120378 }
120379 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regCol, nCol, regSample);
120380 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regTabname, 6, regTemp);
120381 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur+1, regNewRowid);
120382 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur+1, regTemp, regNewRowid);
120383 sqlite3VdbeAddOp2(v, OP_Goto9, 1, addrNext); /* P1==1 for end-of-loop */
120384 sqlite3VdbeJumpHere(v, addrIsNull);
120385 }
120386#endif /* SQLITE_ENABLE_STAT4 */
120387
120388 /* End of analysis */
120389 if( addrGotoEnd ) sqlite3VdbeJumpHere(v, addrGotoEnd);
120390 }
120391
120392
120393 /* Create a single sqlite_stat1 entry containing NULL as the index
120394 ** name and the row count as the content.
120395 */
120396 if( pOnlyIdx==0 && needTableCnt ){
120397 VdbeComment((v, "%s", pTab->zName));
120398 sqlite3VdbeAddOp2(v, OP_Count98, iTabCur, regStat1);
120399 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot17, regStat1); VdbeCoverage(v);
120400 sqlite3VdbeAddOp2(v, OP_Null75, 0, regIdxname);
120401 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
120402 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regTabname, 3, regTemp, "BBB", 0);
120403 sqlite3VdbeAddOp2(v, OP_NewRowid127, iStatCur, regNewRowid);
120404 sqlite3VdbeAddOp3(v, OP_Insert128, iStatCur, regTemp, regNewRowid);
120405 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
120406#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
120407 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-5));
120408#endif
120409 sqlite3VdbeJumpHere(v, jZeroRows);
120410 }
120411}
120412
120413
120414/*
120415** Generate code that will cause the most recent index analysis to
120416** be loaded into internal hash tables where is can be used.
120417*/
120418static void loadAnalysis(Parse *pParse, int iDb){
120419 Vdbe *v = sqlite3GetVdbe(pParse);
120420 if( v ){
120421 sqlite3VdbeAddOp1(v, OP_LoadAnalysis150, iDb);
120422 }
120423}
120424
120425/*
120426** Generate code that will do an analysis of an entire database
120427*/
120428static void analyzeDatabase(Parse *pParse, int iDb){
120429 sqlite3 *db = pParse->db;
120430 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
120431 HashElem *k;
120432 int iStatCur;
120433 int iMem;
120434 int iTab;
120435
120436 sqlite3BeginWriteOperation(pParse, 0, iDb);
120437 iStatCur = pParse->nTab;
120438 pParse->nTab += 3;
120439 openStatTable(pParse, iDb, iStatCur, 0, 0);
120440 iMem = pParse->nMem+1;
120441 iTab = pParse->nTab;
120442 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
120443 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
120444 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
120445 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
120446#ifdef SQLITE_ENABLE_STAT4
120447 iMem = sqlite3FirstAvailableRegister(pParse, iMem);
120448#else
120449 assert( iMem==sqlite3FirstAvailableRegister(pParse,iMem) )((void) (0));
120450#endif
120451 }
120452 loadAnalysis(pParse, iDb);
120453}
120454
120455/*
120456** Generate code that will do an analysis of a single table in
120457** a database. If pOnlyIdx is not NULL then it is a single index
120458** in pTab that should be analyzed.
120459*/
120460static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
120461 int iDb;
120462 int iStatCur;
120463
120464 assert( pTab!=0 )((void) (0));
120465 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
120466 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
120467 sqlite3BeginWriteOperation(pParse, 0, iDb);
120468 iStatCur = pParse->nTab;
120469 pParse->nTab += 3;
120470 if( pOnlyIdx ){
120471 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
120472 }else{
120473 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
120474 }
120475 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
120476 loadAnalysis(pParse, iDb);
120477}
120478
120479/*
120480** Generate code for the ANALYZE command. The parser calls this routine
120481** when it recognizes an ANALYZE command.
120482**
120483** ANALYZE -- 1
120484** ANALYZE <database> -- 2
120485** ANALYZE ?<database>.?<tablename> -- 3
120486**
120487** Form 1 causes all indices in all attached databases to be analyzed.
120488** Form 2 analyzes all indices the single database named.
120489** Form 3 analyzes all indices associated with the named table.
120490*/
120491SQLITE_PRIVATEstatic void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
120492 sqlite3 *db = pParse->db;
120493 int iDb;
120494 int i;
120495 char *z, *zDb;
120496 Table *pTab;
120497 Index *pIdx;
120498 Token *pTableName;
120499 Vdbe *v;
120500
120501 /* Read the database schema. If an error occurs, leave an error message
120502 ** and code in pParse and return NULL. */
120503 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
120504 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
120505 return;
120506 }
120507
120508 assert( pName2!=0 || pName1==0 )((void) (0));
120509 if( pName1==0 ){
120510 /* Form 1: Analyze everything */
120511 for(i=0; i<db->nDb; i++){
120512 if( i==1 ) continue; /* Do not analyze the TEMP database */
120513 analyzeDatabase(pParse, i);
120514 }
120515 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
120516 /* Analyze the schema named as the argument */
120517 analyzeDatabase(pParse, iDb);
120518 }else{
120519 /* Form 3: Analyze the table or index named as an argument */
120520 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
120521 if( iDb>=0 ){
120522 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
120523 z = sqlite3NameFromToken(db, pTableName);
120524 if( z ){
120525 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
120526 analyzeTable(pParse, pIdx->pTable, pIdx);
120527 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
120528 analyzeTable(pParse, pTab, 0);
120529 }
120530 sqlite3DbFree(db, z);
120531 }
120532 }
120533 }
120534 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
120535 sqlite3VdbeAddOp0(v, OP_Expire166);
120536 }
120537}
120538
120539/*
120540** Used to pass information from the analyzer reader through to the
120541** callback routine.
120542*/
120543typedef struct analysisInfo analysisInfo;
120544struct analysisInfo {
120545 sqlite3 *db;
120546 const char *zDatabase;
120547};
120548
120549/*
120550** The first argument points to a nul-terminated string containing a
120551** list of space separated integers. Read the first nOut of these into
120552** the array aOut[].
120553*/
120554static void decodeIntArray(
120555 char *zIntArray, /* String containing int array to decode */
120556 int nOut, /* Number of slots in aOut[] */
120557 tRowcnt *aOut, /* Store integers here */
120558 LogEst *aLog, /* Or, if aOut==0, here */
120559 Index *pIndex /* Handle extra flags for this index, if not NULL */
120560){
120561 char *z = zIntArray;
120562 int c;
120563 int i;
120564 tRowcnt v;
120565
120566#ifdef SQLITE_ENABLE_STAT4
120567 if( z==0 ) z = "";
120568#else
120569 assert( z!=0 )((void) (0));
120570#endif
120571 for(i=0; *z && i<nOut; i++){
120572 v = 0;
120573 while( (c=z[0])>='0' && c<='9' ){
120574 v = v*10 + c - '0';
120575 z++;
120576 }
120577#ifdef SQLITE_ENABLE_STAT4
120578 if( aOut ) aOut[i] = v;
120579 if( aLog ) aLog[i] = sqlite3LogEst(v);
120580#else
120581 assert( aOut==0 )((void) (0));
120582 UNUSED_PARAMETER(aOut)(void)(aOut);
120583 assert( aLog!=0 )((void) (0));
120584 aLog[i] = sqlite3LogEst(v);
120585#endif
120586 if( *z==' ' ) z++;
120587 }
120588#ifndef SQLITE_ENABLE_STAT4
120589 assert( pIndex!=0 )((void) (0)); {
120590#else
120591 if( pIndex ){
120592#endif
120593 pIndex->bUnordered = 0;
120594 pIndex->noSkipScan = 0;
120595 while( z[0] ){
120596 if( sqlite3_strglob("unordered*", z)==0 ){
120597 pIndex->bUnordered = 1;
120598 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
120599 int sz = sqlite3Atoi(z+3);
120600 if( sz<2 ) sz = 2;
120601 pIndex->szIdxRow = sqlite3LogEst(sz);
120602 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
120603 pIndex->noSkipScan = 1;
120604 }
120605#ifdef SQLITE_ENABLE_COSTMULT
120606 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
120607 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
120608 }
120609#endif
120610 while( z[0]!=0 && z[0]!=' ' ) z++;
120611 while( z[0]==' ' ) z++;
120612 }
120613
120614 /* Set the bLowQual flag if the peak number of rows obtained
120615 ** from a full equality match is so large that a full table scan
120616 ** seems likely to be faster than using the index.
120617 */
120618 if( aLog[0] > 66 /* Index has more than 100 rows */
120619 && aLog[0] <= aLog[nOut-1] /* And only a single value seen */
120620 ){
120621 pIndex->bLowQual = 1;
120622 }
120623 }
120624}
120625
120626/*
120627** This callback is invoked once for each index when reading the
120628** sqlite_stat1 table.
120629**
120630** argv[0] = name of the table
120631** argv[1] = name of the index (might be NULL)
120632** argv[2] = results of analysis - on integer for each column
120633**
120634** Entries for which argv[1]==NULL simply record the number of rows in
120635** the table.
120636*/
120637static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
120638 analysisInfo *pInfo = (analysisInfo*)pData;
120639 Index *pIndex;
120640 Table *pTable;
120641 const char *z;
120642
120643 assert( argc==3 )((void) (0));
120644 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
120645
120646 if( argv==0 || argv[0]==0 || argv[2]==0 ){
120647 return 0;
120648 }
120649 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
120650 if( pTable==0 ){
120651 return 0;
120652 }
120653 if( argv[1]==0 ){
120654 pIndex = 0;
120655 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
120656 pIndex = sqlite3PrimaryKeyIndex(pTable);
120657 }else{
120658 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
120659 }
120660 z = argv[2];
120661
120662 if( pIndex ){
120663 tRowcnt *aiRowEst = 0;
120664 int nCol = pIndex->nKeyCol+1;
120665#ifdef SQLITE_ENABLE_STAT4
120666 /* Index.aiRowEst may already be set here if there are duplicate
120667 ** sqlite_stat1 entries for this index. In that case just clobber
120668 ** the old data with the new instead of allocating a new array. */
120669 if( pIndex->aiRowEst==0 ){
120670 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
120671 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
120672 }
120673 aiRowEst = pIndex->aiRowEst;
120674#endif
120675 pIndex->bUnordered = 0;
120676 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
120677 pIndex->hasStat1 = 1;
120678 if( pIndex->pPartIdxWhere==0 ){
120679 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
120680 pTable->tabFlags |= TF_HasStat10x00000010;
120681 }
120682 }else{
120683 Index fakeIdx;
120684 fakeIdx.szIdxRow = pTable->szTabRow;
120685#ifdef SQLITE_ENABLE_COSTMULT
120686 fakeIdx.pTable = pTable;
120687#endif
120688 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
120689 pTable->szTabRow = fakeIdx.szIdxRow;
120690 pTable->tabFlags |= TF_HasStat10x00000010;
120691 }
120692
120693 return 0;
120694}
120695
120696/*
120697** If the Index.aSample variable is not NULL, delete the aSample[] array
120698** and its contents.
120699*/
120700SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
120701 assert( db!=0 )((void) (0));
120702 assert( pIdx!=0 )((void) (0));
120703#ifdef SQLITE_ENABLE_STAT4
120704 if( pIdx->aSample ){
120705 int j;
120706 for(j=0; j<pIdx->nSample; j++){
120707 IndexSample *p = &pIdx->aSample[j];
120708 sqlite3DbFree(db, p->p);
120709 }
120710 sqlite3DbFree(db, pIdx->aSample);
120711 }
120712 if( db->pnBytesFreed==0 ){
120713 pIdx->nSample = 0;
120714 pIdx->aSample = 0;
120715 }
120716#else
120717 UNUSED_PARAMETER(db)(void)(db);
120718 UNUSED_PARAMETER(pIdx)(void)(pIdx);
120719#endif /* SQLITE_ENABLE_STAT4 */
120720}
120721
120722#ifdef SQLITE_ENABLE_STAT4
120723/*
120724** Populate the pIdx->aAvgEq[] array based on the samples currently
120725** stored in pIdx->aSample[].
120726*/
120727static void initAvgEq(Index *pIdx){
120728 if( pIdx ){
120729 IndexSample *aSample = pIdx->aSample;
120730 IndexSample *pFinal = &aSample[pIdx->nSample-1];
120731 int iCol;
120732 int nCol = 1;
120733 if( pIdx->nSampleCol>1 ){
120734 /* If this is stat4 data, then calculate aAvgEq[] values for all
120735 ** sample columns except the last. The last is always set to 1, as
120736 ** once the trailing PK fields are considered all index keys are
120737 ** unique. */
120738 nCol = pIdx->nSampleCol-1;
120739 pIdx->aAvgEq[nCol] = 1;
120740 }
120741 for(iCol=0; iCol<nCol; iCol++){
120742 int nSample = pIdx->nSample;
120743 int i; /* Used to iterate through samples */
120744 tRowcnt sumEq = 0; /* Sum of the nEq values */
120745 tRowcnt avgEq = 0;
120746 tRowcnt nRow; /* Number of rows in index */
120747 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
120748 i64 nDist100; /* Number of distinct values in index */
120749
120750 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
120751 nRow = pFinal->anLt[iCol];
120752 nDist100 = (i64)100 * pFinal->anDLt[iCol];
120753 nSample--;
120754 }else{
120755 nRow = pIdx->aiRowEst[0];
120756 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
120757 }
120758 pIdx->nRowEst0 = nRow;
120759
120760 /* Set nSum to the number of distinct (iCol+1) field prefixes that
120761 ** occur in the stat4 table for this index. Set sumEq to the sum of
120762 ** the nEq values for column iCol for the same set (adding the value
120763 ** only once where there exist duplicate prefixes). */
120764 for(i=0; i<nSample; i++){
120765 if( i==(pIdx->nSample-1)
120766 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
120767 ){
120768 sumEq += aSample[i].anEq[iCol];
120769 nSum100 += 100;
120770 }
120771 }
120772
120773 if( nDist100>nSum100 && sumEq<nRow ){
120774 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
120775 }
120776 if( avgEq==0 ) avgEq = 1;
120777 pIdx->aAvgEq[iCol] = avgEq;
120778 }
120779 }
120780}
120781
120782/*
120783** Look up an index by name. Or, if the name of a WITHOUT ROWID table
120784** is supplied instead, find the PRIMARY KEY index for that table.
120785*/
120786static Index *findIndexOrPrimaryKey(
120787 sqlite3 *db,
120788 const char *zName,
120789 const char *zDb
120790){
120791 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
120792 if( pIdx==0 ){
120793 Table *pTab = sqlite3FindTable(db, zName, zDb);
120794 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
120795 }
120796 return pIdx;
120797}
120798
120799/*
120800** Load the content from either the sqlite_stat4
120801** into the relevant Index.aSample[] arrays.
120802**
120803** Arguments zSql1 and zSql2 must point to SQL statements that return
120804** data equivalent to the following:
120805**
120806** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
120807** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
120808**
120809** where %Q is replaced with the database name before the SQL is executed.
120810*/
120811static int loadStatTbl(
120812 sqlite3 *db, /* Database handle */
120813 const char *zSql1, /* SQL statement 1 (see above) */
120814 const char *zSql2, /* SQL statement 2 (see above) */
120815 const char *zDb /* Database name (e.g. "main") */
120816){
120817 int rc; /* Result codes from subroutines */
120818 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
120819 char *zSql; /* Text of the SQL statement */
120820 Index *pPrevIdx = 0; /* Previous index in the loop */
120821 IndexSample *pSample; /* A slot in pIdx->aSample[] */
120822
120823 assert( db->lookaside.bDisable )((void) (0));
120824 zSql = sqlite3MPrintf(db, zSql1, zDb);
120825 if( !zSql ){
120826 return SQLITE_NOMEM_BKPT7;
120827 }
120828 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
120829 sqlite3DbFree(db, zSql);
120830 if( rc ) return rc;
120831
120832 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
120833 int nIdxCol = 1; /* Number of columns in stat4 records */
120834
120835 char *zIndex; /* Index name */
120836 Index *pIdx; /* Pointer to the index object */
120837 int nSample; /* Number of samples */
120838 i64 nByte; /* Bytes of space required */
120839 i64 i; /* Bytes of space required */
120840 tRowcnt *pSpace; /* Available allocated memory space */
120841 u8 *pPtr; /* Available memory as a u8 for easier manipulation */
120842
120843 zIndex = (char *)sqlite3_column_text(pStmt, 0);
120844 if( zIndex==0 ) continue;
120845 nSample = sqlite3_column_int(pStmt, 1);
120846 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
120847 assert( pIdx==0 || pIdx->nSample==0 )((void) (0));
120848 if( pIdx==0 ) continue;
120849 if( pIdx->aSample!=0 ){
120850 /* The same index appears in sqlite_stat4 under multiple names */
120851 continue;
120852 }
120853 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 )((void) (0));
120854 if( !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
120855 nIdxCol = pIdx->nKeyCol;
120856 }else{
120857 nIdxCol = pIdx->nColumn;
120858 }
120859 pIdx->nSampleCol = nIdxCol;
120860 pIdx->mxSample = nSample;
120861 nByte = ROUND8(sizeof(IndexSample) * nSample)(((sizeof(IndexSample) * nSample)+7)&~7);
120862 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
120863 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
120864
120865 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
120866 if( pIdx->aSample==0 ){
120867 sqlite3_finalize(pStmt);
120868 return SQLITE_NOMEM_BKPT7;
120869 }
120870 pPtr = (u8*)pIdx->aSample;
120871 pPtr += ROUND8(nSample*sizeof(pIdx->aSample[0]))(((nSample*sizeof(pIdx->aSample[0]))+7)&~7);
120872 pSpace = (tRowcnt*)pPtr;
120873 assert( EIGHT_BYTE_ALIGNMENT( pSpace ) )((void) (0));
120874 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
120875 pIdx->pTable->tabFlags |= TF_HasStat40x00002000;
120876 for(i=0; i<nSample; i++){
120877 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
120878 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
120879 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
120880 }
120881 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) )((void) (0));
120882 }
120883 rc = sqlite3_finalize(pStmt);
120884 if( rc ) return rc;
120885
120886 zSql = sqlite3MPrintf(db, zSql2, zDb);
120887 if( !zSql ){
120888 return SQLITE_NOMEM_BKPT7;
120889 }
120890 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
120891 sqlite3DbFree(db, zSql);
120892 if( rc ) return rc;
120893
120894 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
120895 char *zIndex; /* Index name */
120896 Index *pIdx; /* Pointer to the index object */
120897 int nCol = 1; /* Number of columns in index */
120898
120899 zIndex = (char *)sqlite3_column_text(pStmt, 0);
120900 if( zIndex==0 ) continue;
120901 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
120902 if( pIdx==0 ) continue;
120903 if( pIdx->nSample>=pIdx->mxSample ){
120904 /* Too many slots used because the same index appears in
120905 ** sqlite_stat4 using multiple names */
120906 continue;
120907 }
120908 /* This next condition is true if data has already been loaded from
120909 ** the sqlite_stat4 table. */
120910 nCol = pIdx->nSampleCol;
120911 if( pIdx!=pPrevIdx ){
120912 initAvgEq(pPrevIdx);
120913 pPrevIdx = pIdx;
120914 }
120915 pSample = &pIdx->aSample[pIdx->nSample];
120916 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
120917 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
120918 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
120919
120920 /* Take a copy of the sample. Add 8 extra 0x00 bytes the end of the buffer.
120921 ** This is in case the sample record is corrupted. In that case, the
120922 ** sqlite3VdbeRecordCompare() may read up to two varints past the
120923 ** end of the allocated buffer before it realizes it is dealing with
120924 ** a corrupt record. Or it might try to read a large integer from the
120925 ** buffer. In any case, eight 0x00 bytes prevents this from causing
120926 ** a buffer overread. */
120927 pSample->n = sqlite3_column_bytes(pStmt, 4);
120928 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
120929 if( pSample->p==0 ){
120930 sqlite3_finalize(pStmt);
120931 return SQLITE_NOMEM_BKPT7;
120932 }
120933 if( pSample->n ){
120934 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
120935 }
120936 pIdx->nSample++;
120937 }
120938 rc = sqlite3_finalize(pStmt);
120939 if( rc==SQLITE_OK0 ) initAvgEq(pPrevIdx);
120940 return rc;
120941}
120942
120943/*
120944** Load content from the sqlite_stat4 table into
120945** the Index.aSample[] arrays of all indices.
120946*/
120947static int loadStat4(sqlite3 *db, const char *zDb){
120948 int rc = SQLITE_OK0; /* Result codes from subroutines */
120949 const Table *pStat4;
120950
120951 assert( db->lookaside.bDisable )((void) (0));
120952 if( OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0)
120953 && (pStat4 = sqlite3FindTable(db, "sqlite_stat4", zDb))!=0
120954 && IsOrdinaryTable(pStat4)((pStat4)->eTabType==0)
120955 ){
120956 rc = loadStatTbl(db,
120957 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx COLLATE nocase",
120958 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
120959 zDb
120960 );
120961 }
120962 return rc;
120963}
120964#endif /* SQLITE_ENABLE_STAT4 */
120965
120966/*
120967** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
120968** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
120969** arrays. The contents of sqlite_stat4 are used to populate the
120970** Index.aSample[] arrays.
120971**
120972** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
120973** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
120974** during compilation and the sqlite_stat4 table is present, no data is
120975** read from it.
120976**
120977** If SQLITE_ENABLE_STAT4 was defined during compilation and the
120978** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
120979** returned. However, in this case, data is read from the sqlite_stat1
120980** table (if it is present) before returning.
120981**
120982** If an OOM error occurs, this function always sets db->mallocFailed.
120983** This means if the caller does not care about other errors, the return
120984** code may be ignored.
120985*/
120986SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
120987 analysisInfo sInfo;
120988 HashElem *i;
120989 char *zSql;
120990 int rc = SQLITE_OK0;
120991 Schema *pSchema = db->aDb[iDb].pSchema;
120992 const Table *pStat1;
120993
120994 assert( iDb>=0 && iDb<db->nDb )((void) (0));
120995 assert( db->aDb[iDb].pBt!=0 )((void) (0));
120996
120997 /* Clear any prior statistics */
120998 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
120999 for(i=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
121000 Table *pTab = sqliteHashData(i)((i)->data);
121001 pTab->tabFlags &= ~TF_HasStat10x00000010;
121002 }
121003 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
121004 Index *pIdx = sqliteHashData(i)((i)->data);
121005 pIdx->hasStat1 = 0;
121006#ifdef SQLITE_ENABLE_STAT4
121007 sqlite3DeleteIndexSamples(db, pIdx);
121008 pIdx->aSample = 0;
121009#endif
121010 }
121011
121012 /* Load new statistics out of the sqlite_stat1 table */
121013 sInfo.db = db;
121014 sInfo.zDatabase = db->aDb[iDb].zDbSName;
121015 if( (pStat1 = sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase))
121016 && IsOrdinaryTable(pStat1)((pStat1)->eTabType==0)
121017 ){
121018 zSql = sqlite3MPrintf(db,
121019 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
121020 if( zSql==0 ){
121021 rc = SQLITE_NOMEM_BKPT7;
121022 }else{
121023 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
121024 sqlite3DbFree(db, zSql);
121025 }
121026 }
121027
121028 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
121029 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
121030 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
121031 Index *pIdx = sqliteHashData(i)((i)->data);
121032 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
121033 }
121034
121035 /* Load the statistics from the sqlite_stat4 table. */
121036#ifdef SQLITE_ENABLE_STAT4
121037 if( rc==SQLITE_OK0 ){
121038 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
121039 rc = loadStat4(db, sInfo.zDatabase);
121040 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
121041 }
121042 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
121043 Index *pIdx = sqliteHashData(i)((i)->data);
121044 sqlite3_free(pIdx->aiRowEst);
121045 pIdx->aiRowEst = 0;
121046 }
121047#endif
121048
121049 if( rc==SQLITE_NOMEM7 ){
121050 sqlite3OomFault(db);
121051 }
121052 return rc;
121053}
121054
121055
121056#endif /* SQLITE_OMIT_ANALYZE */
121057
121058/************** End of analyze.c *********************************************/
121059/************** Begin file attach.c ******************************************/
121060/*
121061** 2003 April 6
121062**
121063** The author disclaims copyright to this source code. In place of
121064** a legal notice, here is a blessing:
121065**
121066** May you do good and not evil.
121067** May you find forgiveness for yourself and forgive others.
121068** May you share freely, never taking more than you give.
121069**
121070*************************************************************************
121071** This file contains code used to implement the ATTACH and DETACH commands.
121072*/
121073/* #include "sqliteInt.h" */
121074
121075#ifndef SQLITE_OMIT_ATTACH
121076/*
121077** Resolve an expression that was part of an ATTACH or DETACH statement. This
121078** is slightly different from resolving a normal SQL expression, because simple
121079** identifiers are treated as strings, not possible column names or aliases.
121080**
121081** i.e. if the parser sees:
121082**
121083** ATTACH DATABASE abc AS def
121084**
121085** it treats the two expressions as literal strings 'abc' and 'def' instead of
121086** looking for columns of the same name.
121087**
121088** This only applies to the root node of pExpr, so the statement:
121089**
121090** ATTACH DATABASE abc||def AS 'db2'
121091**
121092** will fail because neither abc or def can be resolved.
121093*/
121094static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
121095{
121096 int rc = SQLITE_OK0;
121097 if( pExpr ){
121098 if( pExpr->op!=TK_ID60 ){
121099 rc = sqlite3ResolveExprNames(pName, pExpr);
121100 }else{
121101 pExpr->op = TK_STRING118;
121102 }
121103 }
121104 return rc;
121105}
121106
121107/*
121108** Return true if zName points to a name that may be used to refer to
121109** database iDb attached to handle db.
121110*/
121111SQLITE_PRIVATEstatic int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){
121112 return (
121113 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
121114 || (iDb==0 && sqlite3StrICmp("main", zName)==0)
121115 );
121116}
121117
121118/*
121119** An SQL user-function registered to do the work of an ATTACH statement. The
121120** three arguments to the function come directly from an attach statement:
121121**
121122** ATTACH DATABASE x AS y KEY z
121123**
121124** SELECT sqlite_attach(x, y, z)
121125**
121126** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
121127** third argument.
121128**
121129** If the db->init.reopenMemdb flags is set, then instead of attaching a
121130** new database, close the database on db->init.iDb and reopen it as an
121131** empty MemDB.
121132*/
121133static void attachFunc(
121134 sqlite3_context *context,
121135 int NotUsed,
121136 sqlite3_value **argv
121137){
121138 int i;
121139 int rc = 0;
121140 sqlite3 *db = sqlite3_context_db_handle(context);
121141 const char *zName;
121142 const char *zFile;
121143 char *zPath = 0;
121144 char *zErr = 0;
121145 unsigned int flags;
121146 Db *aNew; /* New array of Db pointers */
121147 Db *pNew = 0; /* Db object for the newly attached database */
121148 char *zErrDyn = 0;
121149 sqlite3_vfs *pVfs;
121150
121151 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
121152 zFile = (const char *)sqlite3_value_text(argv[0]);
121153 zName = (const char *)sqlite3_value_text(argv[1]);
121154 if( zFile==0 ) zFile = "";
121155 if( zName==0 ) zName = "";
121156
121157#ifndef SQLITE_OMIT_DESERIALIZE
121158# define REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) (db->init.reopenMemdb)
121159#else
121160# define REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) (0)
121161#endif
121162
121163 if( REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
121164 /* This is not a real ATTACH. Instead, this routine is being called
121165 ** from sqlite3_deserialize() to close database db->init.iDb and
121166 ** reopen it as a MemDB */
121167 Btree *pNewBt = 0;
121168 pVfs = sqlite3_vfs_find("memdb");
121169 if( pVfs==0 ) return;
121170 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNewBt, 0, SQLITE_OPEN_MAIN_DB0x00000100);
121171 if( rc==SQLITE_OK0 ){
121172 Schema *pNewSchema = sqlite3SchemaGet(db, pNewBt);
121173 if( pNewSchema ){
121174 /* Both the Btree and the new Schema were allocated successfully.
121175 ** Close the old db and update the aDb[] slot with the new memdb
121176 ** values. */
121177 pNew = &db->aDb[db->init.iDb];
121178 if( ALWAYS(pNew->pBt)(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
121179 pNew->pBt = pNewBt;
121180 pNew->pSchema = pNewSchema;
121181 }else{
121182 sqlite3BtreeClose(pNewBt);
121183 rc = SQLITE_NOMEM7;
121184 }
121185 }
121186 if( rc ) goto attach_error;
121187 }else{
121188 /* This is a real ATTACH
121189 **
121190 ** Check for the following errors:
121191 **
121192 ** * Too many attached databases,
121193 ** * Transaction currently open
121194 ** * Specified database name already being used.
121195 */
121196 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED7]+2 ){
121197 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
121198 db->aLimit[SQLITE_LIMIT_ATTACHED7]
121199 );
121200 goto attach_error;
121201 }
121202 for(i=0; i<db->nDb; i++){
121203 assert( zName )((void) (0));
121204 if( sqlite3DbIsNamed(db, i, zName) ){
121205 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
121206 goto attach_error;
121207 }
121208 }
121209
121210 /* Allocate the new entry in the db->aDb[] array and initialize the schema
121211 ** hash tables.
121212 */
121213 if( db->aDb==db->aDbStatic ){
121214 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
121215 if( aNew==0 ) return;
121216 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
121217 }else{
121218 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
121219 if( aNew==0 ) return;
121220 }
121221 db->aDb = aNew;
121222 pNew = &db->aDb[db->nDb];
121223 memset(pNew, 0, sizeof(*pNew));
121224
121225 /* Open the database file. If the btree is successfully opened, use
121226 ** it to obtain the database schema. At this point the schema may
121227 ** or may not be initialized.
121228 */
121229 flags = db->openFlags;
121230 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
121231 if( rc!=SQLITE_OK0 ){
121232 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
121233 sqlite3_result_error(context, zErr, -1);
121234 sqlite3_free(zErr);
121235 return;
121236 }
121237 assert( pVfs )((void) (0));
121238 flags |= SQLITE_OPEN_MAIN_DB0x00000100;
121239 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
121240 db->nDb++;
121241 pNew->zDbSName = sqlite3DbStrDup(db, zName);
121242 }
121243 db->noSharedCache = 0;
121244 if( rc==SQLITE_CONSTRAINT19 ){
121245 rc = SQLITE_ERROR1;
121246 zErrDyn = sqlite3MPrintf(db, "database is already attached");
121247 }else if( rc==SQLITE_OK0 ){
121248 Pager *pPager;
121249 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
121250 if( !pNew->pSchema ){
121251 rc = SQLITE_NOMEM_BKPT7;
121252 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db)((db)->enc) ){
121253 zErrDyn = sqlite3MPrintf(db,
121254 "attached databases must use the same text encoding as main database");
121255 rc = SQLITE_ERROR1;
121256 }
121257 sqlite3BtreeEnter(pNew->pBt);
121258 pPager = sqlite3BtreePager(pNew->pBt);
121259 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
121260 sqlite3BtreeSecureDelete(pNew->pBt,
121261 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
121262#ifndef SQLITE_OMIT_PAGER_PRAGMAS
121263 sqlite3BtreeSetPagerFlags(pNew->pBt,
121264 PAGER_SYNCHRONOUS_FULL0x03 | (db->flags & PAGER_FLAGS_MASK0x38));
121265#endif
121266 sqlite3BtreeLeave(pNew->pBt);
121267 }
121268 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
121269 if( rc==SQLITE_OK0 && pNew->zDbSName==0 ){
121270 rc = SQLITE_NOMEM_BKPT7;
121271 }
121272 sqlite3_free_filename( zPath );
121273
121274 /* If the file was opened successfully, read the schema for the new database.
121275 ** If this fails, or if opening the file failed, then close the file and
121276 ** remove the entry from the db->aDb[] array. i.e. put everything back the
121277 ** way we found it.
121278 */
121279 if( rc==SQLITE_OK0 ){
121280 sqlite3BtreeEnterAll(db);
121281 db->init.iDb = 0;
121282 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk0x0010);
121283 if( !REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
121284 rc = sqlite3Init(db, &zErrDyn);
121285 }
121286 sqlite3BtreeLeaveAll(db);
121287 assert( zErrDyn==0 || rc!=SQLITE_OK )((void) (0));
121288 }
121289#ifdef SQLITE_USER_AUTHENTICATION
121290 if( rc==SQLITE_OK0 && !REOPEN_AS_MEMDB(db)(db->init.reopenMemdb) ){
121291 u8 newAuth = 0;
121292 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
121293 if( newAuth<db->auth.authLevel ){
121294 rc = SQLITE_AUTH_USER(23 | (1<<8));
121295 }
121296 }
121297#endif
121298 if( rc ){
121299 if( ALWAYS(!REOPEN_AS_MEMDB(db))(!(db->init.reopenMemdb)) ){
121300 int iDb = db->nDb - 1;
121301 assert( iDb>=2 )((void) (0));
121302 if( db->aDb[iDb].pBt ){
121303 sqlite3BtreeClose(db->aDb[iDb].pBt);
121304 db->aDb[iDb].pBt = 0;
121305 db->aDb[iDb].pSchema = 0;
121306 }
121307 sqlite3ResetAllSchemasOfConnection(db);
121308 db->nDb = iDb;
121309 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
121310 sqlite3OomFault(db);
121311 sqlite3DbFree(db, zErrDyn);
121312 zErrDyn = sqlite3MPrintf(db, "out of memory");
121313 }else if( zErrDyn==0 ){
121314 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
121315 }
121316 }
121317 goto attach_error;
121318 }
121319
121320 return;
121321
121322attach_error:
121323 /* Return an error if we get here */
121324 if( zErrDyn ){
121325 sqlite3_result_error(context, zErrDyn, -1);
121326 sqlite3DbFree(db, zErrDyn);
121327 }
121328 if( rc ) sqlite3_result_error_code(context, rc);
121329}
121330
121331/*
121332** An SQL user-function registered to do the work of an DETACH statement. The
121333** three arguments to the function come directly from a detach statement:
121334**
121335** DETACH DATABASE x
121336**
121337** SELECT sqlite_detach(x)
121338*/
121339static void detachFunc(
121340 sqlite3_context *context,
121341 int NotUsed,
121342 sqlite3_value **argv
121343){
121344 const char *zName = (const char *)sqlite3_value_text(argv[0]);
121345 sqlite3 *db = sqlite3_context_db_handle(context);
121346 int i;
121347 Db *pDb = 0;
121348 HashElem *pEntry;
121349 char zErr[128];
121350
121351 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
121352
121353 if( zName==0 ) zName = "";
121354 for(i=0; i<db->nDb; i++){
121355 pDb = &db->aDb[i];
121356 if( pDb->pBt==0 ) continue;
121357 if( sqlite3DbIsNamed(db, i, zName) ) break;
121358 }
121359
121360 if( i>=db->nDb ){
121361 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
121362 goto detach_error;
121363 }
121364 if( i<2 ){
121365 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
121366 goto detach_error;
121367 }
121368 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE0
121369 || sqlite3BtreeIsInBackup(pDb->pBt)
121370 ){
121371 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
121372 goto detach_error;
121373 }
121374
121375 /* If any TEMP triggers reference the schema being detached, move those
121376 ** triggers to reference the TEMP schema itself. */
121377 assert( db->aDb[1].pSchema )((void) (0));
121378 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash)((&db->aDb[1].pSchema->trigHash)->first);
121379 while( pEntry ){
121380 Trigger *pTrig = (Trigger*)sqliteHashData(pEntry)((pEntry)->data);
121381 if( pTrig->pTabSchema==pDb->pSchema ){
121382 pTrig->pTabSchema = pTrig->pSchema;
121383 }
121384 pEntry = sqliteHashNext(pEntry)((pEntry)->next);
121385 }
121386
121387 sqlite3BtreeClose(pDb->pBt);
121388 pDb->pBt = 0;
121389 pDb->pSchema = 0;
121390 sqlite3CollapseDatabaseArray(db);
121391 return;
121392
121393detach_error:
121394 sqlite3_result_error(context, zErr, -1);
121395}
121396
121397/*
121398** This procedure generates VDBE code for a single invocation of either the
121399** sqlite_detach() or sqlite_attach() SQL user functions.
121400*/
121401static void codeAttach(
121402 Parse *pParse, /* The parser context */
121403 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
121404 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
121405 Expr *pAuthArg, /* Expression to pass to authorization callback */
121406 Expr *pFilename, /* Name of database file */
121407 Expr *pDbname, /* Name of the database to use internally */
121408 Expr *pKey /* Database key for encryption extension */
121409){
121410 int rc;
121411 NameContext sName;
121412 Vdbe *v;
121413 sqlite3* db = pParse->db;
121414 int regArgs;
121415
121416 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ) goto attach_end;
121417
121418 if( pParse->nErr ) goto attach_end;
121419 memset(&sName, 0, sizeof(NameContext));
121420 sName.pParse = pParse;
121421
121422 if(
121423 SQLITE_OK0!=resolveAttachExpr(&sName, pFilename) ||
121424 SQLITE_OK0!=resolveAttachExpr(&sName, pDbname) ||
121425 SQLITE_OK0!=resolveAttachExpr(&sName, pKey)
121426 ){
121427 goto attach_end;
121428 }
121429
121430#ifndef SQLITE_OMIT_AUTHORIZATION
121431 if( ALWAYS(pAuthArg)(pAuthArg) ){
121432 char *zAuthArg;
121433 if( pAuthArg->op==TK_STRING118 ){
121434 assert( !ExprHasProperty(pAuthArg, EP_IntValue) )((void) (0));
121435 zAuthArg = pAuthArg->u.zToken;
121436 }else{
121437 zAuthArg = 0;
121438 }
121439 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
121440 if(rc!=SQLITE_OK0 ){
121441 goto attach_end;
121442 }
121443 }
121444#endif /* SQLITE_OMIT_AUTHORIZATION */
121445
121446
121447 v = sqlite3GetVdbe(pParse);
121448 regArgs = sqlite3GetTempRange(pParse, 4);
121449 sqlite3ExprCode(pParse, pFilename, regArgs);
121450 sqlite3ExprCode(pParse, pDbname, regArgs+1);
121451 sqlite3ExprCode(pParse, pKey, regArgs+2);
121452
121453 assert( v || db->mallocFailed )((void) (0));
121454 if( v ){
121455 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
121456 pFunc->nArg, pFunc, 0);
121457 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
121458 ** statement only). For DETACH, set it to false (expire all existing
121459 ** statements).
121460 */
121461 sqlite3VdbeAddOp1(v, OP_Expire166, (type==SQLITE_ATTACH24));
121462 }
121463
121464attach_end:
121465 sqlite3ExprDelete(db, pFilename);
121466 sqlite3ExprDelete(db, pDbname);
121467 sqlite3ExprDelete(db, pKey);
121468}
121469
121470/*
121471** Called by the parser to compile a DETACH statement.
121472**
121473** DETACH pDbname
121474*/
121475SQLITE_PRIVATEstatic void sqlite3Detach(Parse *pParse, Expr *pDbname){
121476 static const FuncDef detach_func = {
121477 1, /* nArg */
121478 SQLITE_UTF81, /* funcFlags */
121479 0, /* pUserData */
121480 0, /* pNext */
121481 detachFunc, /* xSFunc */
121482 0, /* xFinalize */
121483 0, 0, /* xValue, xInverse */
121484 "sqlite_detach", /* zName */
121485 {0}
121486 };
121487 codeAttach(pParse, SQLITE_DETACH25, &detach_func, pDbname, 0, 0, pDbname);
121488}
121489
121490/*
121491** Called by the parser to compile an ATTACH statement.
121492**
121493** ATTACH p AS pDbname KEY pKey
121494*/
121495SQLITE_PRIVATEstatic void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
121496 static const FuncDef attach_func = {
121497 3, /* nArg */
121498 SQLITE_UTF81, /* funcFlags */
121499 0, /* pUserData */
121500 0, /* pNext */
121501 attachFunc, /* xSFunc */
121502 0, /* xFinalize */
121503 0, 0, /* xValue, xInverse */
121504 "sqlite_attach", /* zName */
121505 {0}
121506 };
121507 codeAttach(pParse, SQLITE_ATTACH24, &attach_func, p, p, pDbname, pKey);
121508}
121509#endif /* SQLITE_OMIT_ATTACH */
121510
121511/*
121512** Expression callback used by sqlite3FixAAAA() routines.
121513*/
121514static int fixExprCb(Walker *p, Expr *pExpr){
121515 DbFixer *pFix = p->u.pFix;
121516 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL)(pExpr)->flags|=(0x40000000);
121517 if( pExpr->op==TK_VARIABLE157 ){
121518 if( pFix->pParse->db->init.busy ){
121519 pExpr->op = TK_NULL122;
121520 }else{
121521 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
121522 return WRC_Abort2;
121523 }
121524 }
121525 return WRC_Continue0;
121526}
121527
121528/*
121529** Select callback used by sqlite3FixAAAA() routines.
121530*/
121531static int fixSelectCb(Walker *p, Select *pSelect){
121532 DbFixer *pFix = p->u.pFix;
121533 int i;
121534 SrcItem *pItem;
121535 sqlite3 *db = pFix->pParse->db;
121536 int iDb = sqlite3FindDbName(db, pFix->zDb);
121537 SrcList *pList = pSelect->pSrc;
121538
121539 if( NEVER(pList==0)(pList==0) ) return WRC_Continue0;
121540 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
121541 if( pFix->bTemp==0 && pItem->fg.isSubquery==0 ){
121542 if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
121543 if( iDb!=sqlite3FindDbName(db, pItem->u4.zDatabase) ){
121544 sqlite3ErrorMsg(pFix->pParse,
121545 "%s %T cannot reference objects in database %s",
121546 pFix->zType, pFix->pName, pItem->u4.zDatabase);
121547 return WRC_Abort2;
121548 }
121549 sqlite3DbFree(db, pItem->u4.zDatabase);
121550 pItem->fg.notCte = 1;
121551 pItem->fg.hadSchema = 1;
121552 }
121553 pItem->u4.pSchema = pFix->pSchema;
121554 pItem->fg.fromDDL = 1;
121555 pItem->fg.fixedSchema = 1;
121556 }
121557#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
121558 if( pList->a[i].fg.isUsing==0
121559 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
121560 ){
121561 return WRC_Abort2;
121562 }
121563#endif
121564 }
121565 if( pSelect->pWith ){
121566 for(i=0; i<pSelect->pWith->nCte; i++){
121567 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
121568 return WRC_Abort2;
121569 }
121570 }
121571 }
121572 return WRC_Continue0;
121573}
121574
121575/*
121576** Initialize a DbFixer structure. This routine must be called prior
121577** to passing the structure to one of the sqliteFixAAAA() routines below.
121578*/
121579SQLITE_PRIVATEstatic void sqlite3FixInit(
121580 DbFixer *pFix, /* The fixer to be initialized */
121581 Parse *pParse, /* Error messages will be written here */
121582 int iDb, /* This is the database that must be used */
121583 const char *zType, /* "view", "trigger", or "index" */
121584 const Token *pName /* Name of the view, trigger, or index */
121585){
121586 sqlite3 *db = pParse->db;
121587 assert( db->nDb>iDb )((void) (0));
121588 pFix->pParse = pParse;
121589 pFix->zDb = db->aDb[iDb].zDbSName;
121590 pFix->pSchema = db->aDb[iDb].pSchema;
121591 pFix->zType = zType;
121592 pFix->pName = pName;
121593 pFix->bTemp = (iDb==1);
121594 pFix->w.pParse = pParse;
121595 pFix->w.xExprCallback = fixExprCb;
121596 pFix->w.xSelectCallback = fixSelectCb;
121597 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
121598 pFix->w.walkerDepth = 0;
121599 pFix->w.eCode = 0;
121600 pFix->w.u.pFix = pFix;
121601}
121602
121603/*
121604** The following set of routines walk through the parse tree and assign
121605** a specific database to all table references where the database name
121606** was left unspecified in the original SQL statement. The pFix structure
121607** must have been initialized by a prior call to sqlite3FixInit().
121608**
121609** These routines are used to make sure that an index, trigger, or
121610** view in one database does not refer to objects in a different database.
121611** (Exception: indices, triggers, and views in the TEMP database are
121612** allowed to refer to anything.) If a reference is explicitly made
121613** to an object in a different database, an error message is added to
121614** pParse->zErrMsg and these routines return non-zero. If everything
121615** checks out, these routines return 0.
121616*/
121617SQLITE_PRIVATEstatic int sqlite3FixSrcList(
121618 DbFixer *pFix, /* Context of the fixation */
121619 SrcList *pList /* The Source list to check and modify */
121620){
121621 int res = 0;
121622 if( pList ){
121623 Select s;
121624 memset(&s, 0, sizeof(s));
121625 s.pSrc = pList;
121626 res = sqlite3WalkSelect(&pFix->w, &s);
121627 }
121628 return res;
121629}
121630#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
121631SQLITE_PRIVATEstatic int sqlite3FixSelect(
121632 DbFixer *pFix, /* Context of the fixation */
121633 Select *pSelect /* The SELECT statement to be fixed to one database */
121634){
121635 return sqlite3WalkSelect(&pFix->w, pSelect);
121636}
121637SQLITE_PRIVATEstatic int sqlite3FixExpr(
121638 DbFixer *pFix, /* Context of the fixation */
121639 Expr *pExpr /* The expression to be fixed to one database */
121640){
121641 return sqlite3WalkExpr(&pFix->w, pExpr);
121642}
121643#endif
121644
121645#ifndef SQLITE_OMIT_TRIGGER
121646SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(
121647 DbFixer *pFix, /* Context of the fixation */
121648 TriggerStep *pStep /* The trigger step be fixed to one database */
121649){
121650 while( pStep ){
121651 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
121652 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
121653 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
121654 || sqlite3FixSrcList(pFix, pStep->pFrom)
121655 ){
121656 return 1;
121657 }
121658#ifndef SQLITE_OMIT_UPSERT
121659 {
121660 Upsert *pUp;
121661 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
121662 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
121663 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
121664 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
121665 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
121666 ){
121667 return 1;
121668 }
121669 }
121670 }
121671#endif
121672 pStep = pStep->pNext;
121673 }
121674
121675 return 0;
121676}
121677#endif
121678
121679/************** End of attach.c **********************************************/
121680/************** Begin file auth.c ********************************************/
121681/*
121682** 2003 January 11
121683**
121684** The author disclaims copyright to this source code. In place of
121685** a legal notice, here is a blessing:
121686**
121687** May you do good and not evil.
121688** May you find forgiveness for yourself and forgive others.
121689** May you share freely, never taking more than you give.
121690**
121691*************************************************************************
121692** This file contains code used to implement the sqlite3_set_authorizer()
121693** API. This facility is an optional feature of the library. Embedded
121694** systems that do not need this facility may omit it by recompiling
121695** the library with -DSQLITE_OMIT_AUTHORIZATION=1
121696*/
121697/* #include "sqliteInt.h" */
121698
121699/*
121700** All of the code in this file may be omitted by defining a single
121701** macro.
121702*/
121703#ifndef SQLITE_OMIT_AUTHORIZATION
121704
121705/*
121706** Set or clear the access authorization function.
121707**
121708** The access authorization function is be called during the compilation
121709** phase to verify that the user has read and/or write access permission on
121710** various fields of the database. The first argument to the auth function
121711** is a copy of the 3rd argument to this routine. The second argument
121712** to the auth function is one of these constants:
121713**
121714** SQLITE_CREATE_INDEX
121715** SQLITE_CREATE_TABLE
121716** SQLITE_CREATE_TEMP_INDEX
121717** SQLITE_CREATE_TEMP_TABLE
121718** SQLITE_CREATE_TEMP_TRIGGER
121719** SQLITE_CREATE_TEMP_VIEW
121720** SQLITE_CREATE_TRIGGER
121721** SQLITE_CREATE_VIEW
121722** SQLITE_DELETE
121723** SQLITE_DROP_INDEX
121724** SQLITE_DROP_TABLE
121725** SQLITE_DROP_TEMP_INDEX
121726** SQLITE_DROP_TEMP_TABLE
121727** SQLITE_DROP_TEMP_TRIGGER
121728** SQLITE_DROP_TEMP_VIEW
121729** SQLITE_DROP_TRIGGER
121730** SQLITE_DROP_VIEW
121731** SQLITE_INSERT
121732** SQLITE_PRAGMA
121733** SQLITE_READ
121734** SQLITE_SELECT
121735** SQLITE_TRANSACTION
121736** SQLITE_UPDATE
121737**
121738** The third and fourth arguments to the auth function are the name of
121739** the table and the column that are being accessed. The auth function
121740** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
121741** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
121742** means that the SQL statement will never-run - the sqlite3_exec() call
121743** will return with an error. SQLITE_IGNORE means that the SQL statement
121744** should run but attempts to read the specified column will return NULL
121745** and attempts to write the column will be ignored.
121746**
121747** Setting the auth function to NULL disables this hook. The default
121748** setting of the auth function is NULL.
121749*/
121750SQLITE_API int sqlite3_set_authorizer(
121751 sqlite3 *db,
121752 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
121753 void *pArg
121754){
121755#ifdef SQLITE_ENABLE_API_ARMOR1
121756 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(121756);
121757#endif
121758 sqlite3_mutex_enter(db->mutex);
121759 db->xAuth = (sqlite3_xauth)xAuth;
121760 db->pAuthArg = pArg;
121761 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
121762 sqlite3_mutex_leave(db->mutex);
121763 return SQLITE_OK0;
121764}
121765
121766/*
121767** Write an error message into pParse->zErrMsg that explains that the
121768** user-supplied authorization function returned an illegal value.
121769*/
121770static void sqliteAuthBadReturnCode(Parse *pParse){
121771 sqlite3ErrorMsg(pParse, "authorizer malfunction");
121772 pParse->rc = SQLITE_ERROR1;
121773}
121774
121775/*
121776** Invoke the authorization callback for permission to read column zCol from
121777** table zTab in database zDb. This function assumes that an authorization
121778** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
121779**
121780** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
121781** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
121782** is treated as SQLITE_DENY. In this case an error is left in pParse.
121783*/
121784SQLITE_PRIVATEstatic int sqlite3AuthReadCol(
121785 Parse *pParse, /* The parser context */
121786 const char *zTab, /* Table name */
121787 const char *zCol, /* Column name */
121788 int iDb /* Index of containing database. */
121789){
121790 sqlite3 *db = pParse->db; /* Database handle */
121791 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
121792 int rc; /* Auth callback return code */
121793
121794 if( db->init.busy ) return SQLITE_OK0;
121795 rc = db->xAuth(db->pAuthArg, SQLITE_READ20, zTab,zCol,zDb,pParse->zAuthContext
121796#ifdef SQLITE_USER_AUTHENTICATION
121797 ,db->auth.zAuthUser
121798#endif
121799 );
121800 if( rc==SQLITE_DENY1 ){
121801 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
121802 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
121803 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
121804 pParse->rc = SQLITE_AUTH23;
121805 }else if( rc!=SQLITE_IGNORE2 && rc!=SQLITE_OK0 ){
121806 sqliteAuthBadReturnCode(pParse);
121807 }
121808 return rc;
121809}
121810
121811/*
121812** The pExpr should be a TK_COLUMN expression. The table referred to
121813** is in pTabList or else it is the NEW or OLD table of a trigger.
121814** Check to see if it is OK to read this particular column.
121815**
121816** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
121817** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
121818** then generate an error.
121819*/
121820SQLITE_PRIVATEstatic void sqlite3AuthRead(
121821 Parse *pParse, /* The parser context */
121822 Expr *pExpr, /* The expression to check authorization on */
121823 Schema *pSchema, /* The schema of the expression */
121824 SrcList *pTabList /* All table that pExpr might refer to */
121825){
121826 Table *pTab = 0; /* The table being read */
121827 const char *zCol; /* Name of the column of the table */
121828 int iSrc; /* Index in pTabList->a[] of table being read */
121829 int iDb; /* The index of the database the expression refers to */
121830 int iCol; /* Index of column in table */
121831
121832 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER )((void) (0));
121833 assert( !IN_RENAME_OBJECT )((void) (0));
121834 assert( pParse->db->xAuth!=0 )((void) (0));
121835 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
121836 if( iDb<0 ){
121837 /* An attempt to read a column out of a subquery or other
121838 ** temporary table. */
121839 return;
121840 }
121841
121842 if( pExpr->op==TK_TRIGGER78 ){
121843 pTab = pParse->pTriggerTab;
121844 }else{
121845 assert( pTabList )((void) (0));
121846 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
121847 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
121848 pTab = pTabList->a[iSrc].pSTab;
121849 break;
121850 }
121851 }
121852 }
121853 iCol = pExpr->iColumn;
121854 if( pTab==0 ) return;
121855
121856 if( iCol>=0 ){
121857 assert( iCol<pTab->nCol )((void) (0));
121858 zCol = pTab->aCol[iCol].zCnName;
121859 }else if( pTab->iPKey>=0 ){
121860 assert( pTab->iPKey<pTab->nCol )((void) (0));
121861 zCol = pTab->aCol[pTab->iPKey].zCnName;
121862 }else{
121863 zCol = "ROWID";
121864 }
121865 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
121866 if( SQLITE_IGNORE2==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
121867 pExpr->op = TK_NULL122;
121868 }
121869}
121870
121871/*
121872** Do an authorization check using the code and arguments given. Return
121873** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
121874** is returned, then the error count and error message in pParse are
121875** modified appropriately.
121876*/
121877SQLITE_PRIVATEstatic int sqlite3AuthCheck(
121878 Parse *pParse,
121879 int code,
121880 const char *zArg1,
121881 const char *zArg2,
121882 const char *zArg3
121883){
121884 sqlite3 *db = pParse->db;
121885 int rc;
121886
121887 /* Don't do any authorization checks if the database is initializing
121888 ** or if the parser is being invoked from within sqlite3_declare_vtab.
121889 */
121890 assert( !IN_RENAME_OBJECT || db->xAuth==0 )((void) (0));
121891 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
121892 return SQLITE_OK0;
121893 }
121894
121895 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
121896 ** callback are either NULL pointers or zero-terminated strings that
121897 ** contain additional details about the action to be authorized.
121898 **
121899 ** The following testcase() macros show that any of the 3rd through 6th
121900 ** parameters can be either NULL or a string. */
121901 testcase( zArg1==0 );
121902 testcase( zArg2==0 );
121903 testcase( zArg3==0 );
121904 testcase( pParse->zAuthContext==0 );
121905
121906 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
121907#ifdef SQLITE_USER_AUTHENTICATION
121908 ,db->auth.zAuthUser
121909#endif
121910 );
121911 if( rc==SQLITE_DENY1 ){
121912 sqlite3ErrorMsg(pParse, "not authorized");
121913 pParse->rc = SQLITE_AUTH23;
121914 }else if( rc!=SQLITE_OK0 && rc!=SQLITE_IGNORE2 ){
121915 rc = SQLITE_DENY1;
121916 sqliteAuthBadReturnCode(pParse);
121917 }
121918 return rc;
121919}
121920
121921/*
121922** Push an authorization context. After this routine is called, the
121923** zArg3 argument to authorization callbacks will be zContext until
121924** popped. Or if pParse==0, this routine is a no-op.
121925*/
121926SQLITE_PRIVATEstatic void sqlite3AuthContextPush(
121927 Parse *pParse,
121928 AuthContext *pContext,
121929 const char *zContext
121930){
121931 assert( pParse )((void) (0));
121932 pContext->pParse = pParse;
121933 pContext->zAuthContext = pParse->zAuthContext;
121934 pParse->zAuthContext = zContext;
121935}
121936
121937/*
121938** Pop an authorization context that was previously pushed
121939** by sqlite3AuthContextPush
121940*/
121941SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext *pContext){
121942 if( pContext->pParse ){
121943 pContext->pParse->zAuthContext = pContext->zAuthContext;
121944 pContext->pParse = 0;
121945 }
121946}
121947
121948#endif /* SQLITE_OMIT_AUTHORIZATION */
121949
121950/************** End of auth.c ************************************************/
121951/************** Begin file build.c *******************************************/
121952/*
121953** 2001 September 15
121954**
121955** The author disclaims copyright to this source code. In place of
121956** a legal notice, here is a blessing:
121957**
121958** May you do good and not evil.
121959** May you find forgiveness for yourself and forgive others.
121960** May you share freely, never taking more than you give.
121961**
121962*************************************************************************
121963** This file contains C code routines that are called by the SQLite parser
121964** when syntax rules are reduced. The routines in this file handle the
121965** following kinds of SQL syntax:
121966**
121967** CREATE TABLE
121968** DROP TABLE
121969** CREATE INDEX
121970** DROP INDEX
121971** creating ID lists
121972** BEGIN TRANSACTION
121973** COMMIT
121974** ROLLBACK
121975*/
121976/* #include "sqliteInt.h" */
121977
121978#ifndef SQLITE_OMIT_SHARED_CACHE
121979/*
121980** The TableLock structure is only used by the sqlite3TableLock() and
121981** codeTableLocks() functions.
121982*/
121983struct TableLock {
121984 int iDb; /* The database containing the table to be locked */
121985 Pgno iTab; /* The root page of the table to be locked */
121986 u8 isWriteLock; /* True for write lock. False for a read lock */
121987 const char *zLockName; /* Name of the table */
121988};
121989
121990/*
121991** Record the fact that we want to lock a table at run-time.
121992**
121993** The table to be locked has root page iTab and is found in database iDb.
121994** A read or a write lock can be taken depending on isWritelock.
121995**
121996** This routine just records the fact that the lock is desired. The
121997** code to make the lock occur is generated by a later call to
121998** codeTableLocks() which occurs during sqlite3FinishCoding().
121999*/
122000static SQLITE_NOINLINE__attribute__((noinline)) void lockTable(
122001 Parse *pParse, /* Parsing context */
122002 int iDb, /* Index of the database containing the table to lock */
122003 Pgno iTab, /* Root page number of the table to be locked */
122004 u8 isWriteLock, /* True for a write lock */
122005 const char *zName /* Name of the table to be locked */
122006){
122007 Parse *pToplevel;
122008 int i;
122009 int nBytes;
122010 TableLock *p;
122011 assert( iDb>=0 )((void) (0));
122012
122013 pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
122014 for(i=0; i<pToplevel->nTableLock; i++){
122015 p = &pToplevel->aTableLock[i];
122016 if( p->iDb==iDb && p->iTab==iTab ){
122017 p->isWriteLock = (p->isWriteLock || isWriteLock);
122018 return;
122019 }
122020 }
122021
122022 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
122023 pToplevel->aTableLock =
122024 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
122025 if( pToplevel->aTableLock ){
122026 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
122027 p->iDb = iDb;
122028 p->iTab = iTab;
122029 p->isWriteLock = isWriteLock;
122030 p->zLockName = zName;
122031 }else{
122032 pToplevel->nTableLock = 0;
122033 sqlite3OomFault(pToplevel->db);
122034 }
122035}
122036SQLITE_PRIVATEstatic void sqlite3TableLock(
122037 Parse *pParse, /* Parsing context */
122038 int iDb, /* Index of the database containing the table to lock */
122039 Pgno iTab, /* Root page number of the table to be locked */
122040 u8 isWriteLock, /* True for a write lock */
122041 const char *zName /* Name of the table to be locked */
122042){
122043 if( iDb==1 ) return;
122044 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
122045 lockTable(pParse, iDb, iTab, isWriteLock, zName);
122046}
122047
122048/*
122049** Code an OP_TableLock instruction for each table locked by the
122050** statement (configured by calls to sqlite3TableLock()).
122051*/
122052static void codeTableLocks(Parse *pParse){
122053 int i;
122054 Vdbe *pVdbe = pParse->pVdbe;
122055 assert( pVdbe!=0 )((void) (0));
122056
122057 for(i=0; i<pParse->nTableLock; i++){
122058 TableLock *p = &pParse->aTableLock[i];
122059 int p1 = p->iDb;
122060 sqlite3VdbeAddOp4(pVdbe, OP_TableLock169, p1, p->iTab, p->isWriteLock,
122061 p->zLockName, P4_STATIC(-1));
122062 }
122063}
122064#else
122065 #define codeTableLocks(x)
122066#endif
122067
122068/*
122069** Return TRUE if the given yDbMask object is empty - if it contains no
122070** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
122071** macros when SQLITE_MAX_ATTACHED is greater than 30.
122072*/
122073#if SQLITE_MAX_ATTACHED10>30
122074SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask m){
122075 int i;
122076 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
122077 return 1;
122078}
122079#endif
122080
122081/*
122082** This routine is called after a single SQL statement has been
122083** parsed and a VDBE program to execute that statement has been
122084** prepared. This routine puts the finishing touches on the
122085** VDBE program and resets the pParse structure for the next
122086** parse.
122087**
122088** Note that if an error occurred, it might be the case that
122089** no VDBE code was generated.
122090*/
122091SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse *pParse){
122092 sqlite3 *db;
122093 Vdbe *v;
122094 int iDb, i;
122095
122096 assert( pParse->pToplevel==0 )((void) (0));
122097 db = pParse->db;
122098 assert( db->pParse==pParse )((void) (0));
122099 if( pParse->nested ) return;
122100 if( pParse->nErr ){
122101 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM7;
122102 return;
122103 }
122104 assert( db->mallocFailed==0 )((void) (0));
122105
122106 /* Begin by generating some termination code at the end of the
122107 ** vdbe program
122108 */
122109 v = pParse->pVdbe;
122110 if( v==0 ){
122111 if( db->init.busy ){
122112 pParse->rc = SQLITE_DONE101;
122113 return;
122114 }
122115 v = sqlite3GetVdbe(pParse);
122116 if( v==0 ) pParse->rc = SQLITE_ERROR1;
122117 }
122118 assert( !pParse->isMultiWrite((void) (0))
122119 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort))((void) (0));
122120 if( v ){
122121 if( pParse->bReturning ){
122122 Returning *pReturning = pParse->u1.pReturning;
122123 int addrRewind;
122124 int reg;
122125
122126 if( pReturning->nRetCol ){
122127 sqlite3VdbeAddOp0(v, OP_FkCheck83);
122128 addrRewind =
122129 sqlite3VdbeAddOp1(v, OP_Rewind36, pReturning->iRetCur);
122130 VdbeCoverage(v);
122131 reg = pReturning->iRetReg;
122132 for(i=0; i<pReturning->nRetCol; i++){
122133 sqlite3VdbeAddOp3(v, OP_Column94, pReturning->iRetCur, i, reg+i);
122134 }
122135 sqlite3VdbeAddOp2(v, OP_ResultRow84, reg, i);
122136 sqlite3VdbeAddOp2(v, OP_Next39, pReturning->iRetCur, addrRewind+1);
122137 VdbeCoverage(v);
122138 sqlite3VdbeJumpHere(v, addrRewind);
122139 }
122140 }
122141 sqlite3VdbeAddOp0(v, OP_Halt70);
122142
122143#if SQLITE_USER_AUTHENTICATION && !defined(SQLITE_OMIT_SHARED_CACHE)
122144 if( pParse->nTableLock>0 && db->init.busy==0 ){
122145 sqlite3UserAuthInit(db);
122146 if( db->auth.authLevel<UAUTH_User ){
122147 sqlite3ErrorMsg(pParse, "user not authenticated");
122148 pParse->rc = SQLITE_AUTH_USER(23 | (1<<8));
122149 return;
122150 }
122151 }
122152#endif
122153
122154 /* The cookie mask contains one bit for each database file open.
122155 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
122156 ** set for each database that is used. Generate code to start a
122157 ** transaction on each used database and to verify the schema cookie
122158 ** on each used database.
122159 */
122160 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init )((void) (0));
122161 sqlite3VdbeJumpHere(v, 0);
122162 assert( db->nDb>0 )((void) (0));
122163 iDb = 0;
122164 do{
122165 Schema *pSchema;
122166 if( DbMaskTest(pParse->cookieMask, iDb)(((pParse->cookieMask)&(((yDbMask)1)<<(iDb)))!=0
)
==0 ) continue;
122167 sqlite3VdbeUsesBtree(v, iDb);
122168 pSchema = db->aDb[iDb].pSchema;
122169 sqlite3VdbeAddOp4Int(v,
122170 OP_Transaction2, /* Opcode */
122171 iDb, /* P1 */
122172 DbMaskTest(pParse->writeMask,iDb)(((pParse->writeMask)&(((yDbMask)1)<<(iDb)))!=0), /* P2 */
122173 pSchema->schema_cookie, /* P3 */
122174 pSchema->iGeneration /* P4 */
122175 );
122176 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
122177 VdbeComment((v,
122178 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
122179 }while( ++iDb<db->nDb );
122180#ifndef SQLITE_OMIT_VIRTUALTABLE
122181 for(i=0; i<pParse->nVtabLock; i++){
122182 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
122183 sqlite3VdbeAddOp4(v, OP_VBegin170, 0, 0, 0, vtab, P4_VTAB(-11));
122184 }
122185 pParse->nVtabLock = 0;
122186#endif
122187
122188#ifndef SQLITE_OMIT_SHARED_CACHE
122189 /* Once all the cookies have been verified and transactions opened,
122190 ** obtain the required table-locks. This is a no-op unless the
122191 ** shared-cache feature is enabled.
122192 */
122193 if( pParse->nTableLock ) codeTableLocks(pParse);
122194#endif
122195
122196 /* Initialize any AUTOINCREMENT data structures required.
122197 */
122198 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
122199
122200 /* Code constant expressions that were factored out of inner loops.
122201 */
122202 if( pParse->pConstExpr ){
122203 ExprList *pEL = pParse->pConstExpr;
122204 pParse->okConstFactor = 0;
122205 for(i=0; i<pEL->nExpr; i++){
122206 assert( pEL->a[i].u.iConstExprReg>0 )((void) (0));
122207 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
122208 }
122209 }
122210
122211 if( pParse->bReturning ){
122212 Returning *pRet = pParse->u1.pReturning;
122213 if( pRet->nRetCol ){
122214 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pRet->iRetCur, pRet->nRetCol);
122215 }
122216 }
122217
122218 /* Finally, jump back to the beginning of the executable code. */
122219 sqlite3VdbeGoto(v, 1);
122220 }
122221
122222 /* Get the VDBE program ready for execution
122223 */
122224 assert( v!=0 || pParse->nErr )((void) (0));
122225 assert( db->mallocFailed==0 || pParse->nErr )((void) (0));
122226 if( pParse->nErr==0 ){
122227 /* A minimum of one cursor is required if autoincrement is used
122228 * See ticket [a696379c1f08866] */
122229 assert( pParse->pAinc==0 || pParse->nTab>0 )((void) (0));
122230 sqlite3VdbeMakeReady(v, pParse);
122231 pParse->rc = SQLITE_DONE101;
122232 }else{
122233 pParse->rc = SQLITE_ERROR1;
122234 }
122235}
122236
122237/*
122238** Run the parser and code generator recursively in order to generate
122239** code for the SQL statement given onto the end of the pParse context
122240** currently under construction. Notes:
122241**
122242** * The final OP_Halt is not appended and other initialization
122243** and finalization steps are omitted because those are handling by the
122244** outermost parser.
122245**
122246** * Built-in SQL functions always take precedence over application-defined
122247** SQL functions. In other words, it is not possible to override a
122248** built-in function.
122249*/
122250SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
122251 va_list ap;
122252 char *zSql;
122253 sqlite3 *db = pParse->db;
122254 u32 savedDbFlags = db->mDbFlags;
122255 char saveBuf[PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken))];
122256
122257 if( pParse->nErr ) return;
122258 if( pParse->eParseMode ) return;
122259 assert( pParse->nested<10 )((void) (0)); /* Nesting should only be of limited depth */
122260 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
122261 zSql = sqlite3VMPrintf(db, zFormat, ap);
122262 va_end(ap)__builtin_va_end(ap);
122263 if( zSql==0 ){
122264 /* This can result either from an OOM or because the formatted string
122265 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
122266 ** an error */
122267 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG18;
122268 pParse->nErr++;
122269 return;
122270 }
122271 pParse->nested++;
122272 memcpy(saveBuf, PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
122273 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
122274 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002;
122275 sqlite3RunParser(pParse, zSql);
122276 db->mDbFlags = savedDbFlags;
122277 sqlite3DbFree(db, zSql);
122278 memcpy(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), saveBuf, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
122279 pParse->nested--;
122280}
122281
122282#if SQLITE_USER_AUTHENTICATION
122283/*
122284** Return TRUE if zTable is the name of the system table that stores the
122285** list of users and their access credentials.
122286*/
122287SQLITE_PRIVATEstatic int sqlite3UserAuthTable(const char *zTable){
122288 return sqlite3_stricmp(zTable, "sqlite_user")==0;
122289}
122290#endif
122291
122292/*
122293** Locate the in-memory structure that describes a particular database
122294** table given the name of that table and (optionally) the name of the
122295** database containing the table. Return NULL if not found.
122296**
122297** If zDatabase is 0, all databases are searched for the table and the
122298** first matching table is returned. (No checking for duplicate table
122299** names is done.) The search order is TEMP first, then MAIN, then any
122300** auxiliary databases added using the ATTACH command.
122301**
122302** See also sqlite3LocateTable().
122303*/
122304SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
122305 Table *p = 0;
122306 int i;
122307
122308 /* All mutexes are required for schema access. Make sure we hold them. */
122309 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
122310#if SQLITE_USER_AUTHENTICATION
122311 /* Only the admin user is allowed to know that the sqlite_user table
122312 ** exists */
122313 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
122314 return 0;
122315 }
122316#endif
122317 if( zDatabase ){
122318 for(i=0; i<db->nDb; i++){
122319 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
122320 }
122321 if( i>=db->nDb ){
122322 /* No match against the official names. But always match "main"
122323 ** to schema 0 as a legacy fallback. */
122324 if( sqlite3StrICmp(zDatabase,"main")==0 ){
122325 i = 0;
122326 }else{
122327 return 0;
122328 }
122329 }
122330 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
122331 if( p==0 && sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
122332 if( i==1 ){
122333 if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0
122334 || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0
122335 || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0
122336 ){
122337 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
122338 LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master");
122339 }
122340 }else{
122341 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ){
122342 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
122343 LEGACY_SCHEMA_TABLE"sqlite_master");
122344 }
122345 }
122346 }
122347 }else{
122348 /* Match against TEMP first */
122349 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
122350 if( p ) return p;
122351 /* The main database is second */
122352 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
122353 if( p ) return p;
122354 /* Attached databases are in order of attachment */
122355 for(i=2; i<db->nDb; i++){
122356 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
122357 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
122358 if( p ) break;
122359 }
122360 if( p==0 && sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
122361 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE"sqlite_schema"[7])==0 ){
122362 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE"sqlite_master");
122363 }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema"[7])==0 ){
122364 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
122365 LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master");
122366 }
122367 }
122368 }
122369 return p;
122370}
122371
122372/*
122373** Locate the in-memory structure that describes a particular database
122374** table given the name of that table and (optionally) the name of the
122375** database containing the table. Return NULL if not found. Also leave an
122376** error message in pParse->zErrMsg.
122377**
122378** The difference between this routine and sqlite3FindTable() is that this
122379** routine leaves an error message in pParse->zErrMsg where
122380** sqlite3FindTable() does not.
122381*/
122382SQLITE_PRIVATEstatic Table *sqlite3LocateTable(
122383 Parse *pParse, /* context in which to report errors */
122384 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
122385 const char *zName, /* Name of the table we are looking for */
122386 const char *zDbase /* Name of the database. Might be NULL */
122387){
122388 Table *p;
122389 sqlite3 *db = pParse->db;
122390
122391 /* Read the database schema. If an error occurs, leave an error message
122392 ** and code in pParse and return NULL. */
122393 if( (db->mDbFlags & DBFLAG_SchemaKnownOk0x0010)==0
122394 && SQLITE_OK0!=sqlite3ReadSchema(pParse)
122395 ){
122396 return 0;
122397 }
122398
122399 p = sqlite3FindTable(db, zName, zDbase);
122400 if( p==0 ){
122401#ifndef SQLITE_OMIT_VIRTUALTABLE
122402 /* If zName is the not the name of a table in the schema created using
122403 ** CREATE, then check to see if it is the name of an virtual table that
122404 ** can be an eponymous virtual table. */
122405 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB0x04)==0 && db->init.busy==0 ){
122406 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
122407 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
122408 pMod = sqlite3PragmaVtabRegister(db, zName);
122409 }
122410 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
122411 testcase( pMod->pEpoTab==0 );
122412 return pMod->pEpoTab;
122413 }
122414 }
122415#endif
122416 if( flags & LOCATE_NOERR0x02 ) return 0;
122417 pParse->checkSchema = 1;
122418 }else if( IsVirtual(p)((p)->eTabType==1) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB0x04)!=0 ){
122419 p = 0;
122420 }
122421
122422 if( p==0 ){
122423 const char *zMsg = flags & LOCATE_VIEW0x01 ? "no such view" : "no such table";
122424 if( zDbase ){
122425 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
122426 }else{
122427 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
122428 }
122429 }else{
122430 assert( HasRowid(p) || p->iPKey<0 )((void) (0));
122431 }
122432
122433 return p;
122434}
122435
122436/*
122437** Locate the table identified by *p.
122438**
122439** This is a wrapper around sqlite3LocateTable(). The difference between
122440** sqlite3LocateTable() and this function is that this function restricts
122441** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
122442** non-NULL if it is part of a view or trigger program definition. See
122443** sqlite3FixSrcList() for details.
122444*/
122445SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(
122446 Parse *pParse,
122447 u32 flags,
122448 SrcItem *p
122449){
122450 const char *zDb;
122451 if( p->fg.fixedSchema ){
122452 int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema);
122453 zDb = pParse->db->aDb[iDb].zDbSName;
122454 }else{
122455 assert( !p->fg.isSubquery )((void) (0));
122456 zDb = p->u4.zDatabase;
122457 }
122458 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
122459}
122460
122461/*
122462** Return the preferred table name for system tables. Translate legacy
122463** names into the new preferred names, as appropriate.
122464*/
122465SQLITE_PRIVATEstatic const char *sqlite3PreferredTableName(const char *zName){
122466 if( sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7)==0 ){
122467 if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE"sqlite_master"[7])==0 ){
122468 return PREFERRED_SCHEMA_TABLE"sqlite_schema";
122469 }
122470 if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE"sqlite_temp_master"[7])==0 ){
122471 return PREFERRED_TEMP_SCHEMA_TABLE"sqlite_temp_schema";
122472 }
122473 }
122474 return zName;
122475}
122476
122477/*
122478** Locate the in-memory structure that describes
122479** a particular index given the name of that index
122480** and the name of the database that contains the index.
122481** Return NULL if not found.
122482**
122483** If zDatabase is 0, all databases are searched for the
122484** table and the first matching index is returned. (No checking
122485** for duplicate index names is done.) The search order is
122486** TEMP first, then MAIN, then any auxiliary databases added
122487** using the ATTACH command.
122488*/
122489SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
122490 Index *p = 0;
122491 int i;
122492 /* All mutexes are required for schema access. Make sure we hold them. */
122493 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
122494 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
122495 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
122496 Schema *pSchema = db->aDb[j].pSchema;
122497 assert( pSchema )((void) (0));
122498 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
122499 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
122500 p = sqlite3HashFind(&pSchema->idxHash, zName);
122501 if( p ) break;
122502 }
122503 return p;
122504}
122505
122506/*
122507** Reclaim the memory used by an index
122508*/
122509SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3 *db, Index *p){
122510#ifndef SQLITE_OMIT_ANALYZE
122511 sqlite3DeleteIndexSamples(db, p);
122512#endif
122513 sqlite3ExprDelete(db, p->pPartIdxWhere);
122514 sqlite3ExprListDelete(db, p->aColExpr);
122515 sqlite3DbFree(db, p->zColAff);
122516 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
122517#ifdef SQLITE_ENABLE_STAT4
122518 sqlite3_free(p->aiRowEst);
122519#endif
122520 sqlite3DbFree(db, p);
122521}
122522
122523/*
122524** For the index called zIdxName which is found in the database iDb,
122525** unlike that index from its Table then remove the index from
122526** the index hash table and free all memory structures associated
122527** with the index.
122528*/
122529SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
122530 Index *pIndex;
122531 Hash *pHash;
122532
122533 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122534 pHash = &db->aDb[iDb].pSchema->idxHash;
122535 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
122536 if( ALWAYS(pIndex)(pIndex) ){
122537 if( pIndex->pTable->pIndex==pIndex ){
122538 pIndex->pTable->pIndex = pIndex->pNext;
122539 }else{
122540 Index *p;
122541 /* Justification of ALWAYS(); The index must be on the list of
122542 ** indices. */
122543 p = pIndex->pTable->pIndex;
122544 while( ALWAYS(p)(p) && p->pNext!=pIndex ){ p = p->pNext; }
122545 if( ALWAYS(p && p->pNext==pIndex)(p && p->pNext==pIndex) ){
122546 p->pNext = pIndex->pNext;
122547 }
122548 }
122549 sqlite3FreeIndex(db, pIndex);
122550 }
122551 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
122552}
122553
122554/*
122555** Look through the list of open database files in db->aDb[] and if
122556** any have been closed, remove them from the list. Reallocate the
122557** db->aDb[] structure to a smaller size, if possible.
122558**
122559** Entry 0 (the "main" database) and entry 1 (the "temp" database)
122560** are never candidates for being collapsed.
122561*/
122562SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3 *db){
122563 int i, j;
122564 for(i=j=2; i<db->nDb; i++){
122565 struct Db *pDb = &db->aDb[i];
122566 if( pDb->pBt==0 ){
122567 sqlite3DbFree(db, pDb->zDbSName);
122568 pDb->zDbSName = 0;
122569 continue;
122570 }
122571 if( j<i ){
122572 db->aDb[j] = db->aDb[i];
122573 }
122574 j++;
122575 }
122576 db->nDb = j;
122577 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
122578 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
122579 sqlite3DbFree(db, db->aDb);
122580 db->aDb = db->aDbStatic;
122581 }
122582}
122583
122584/*
122585** Reset the schema for the database at index iDb. Also reset the
122586** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
122587** Deferred resets may be run by calling with iDb<0.
122588*/
122589SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
122590 int i;
122591 assert( iDb<db->nDb )((void) (0));
122592
122593 if( iDb>=0 ){
122594 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122595 DbSetProperty(db, iDb, DB_ResetWanted)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0008);
122596 DbSetProperty(db, 1, DB_ResetWanted)(db)->aDb[1].pSchema->schemaFlags|=(0x0008);
122597 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
122598 }
122599
122600 if( db->nSchemaLock==0 ){
122601 for(i=0; i<db->nDb; i++){
122602 if( DbHasProperty(db, i, DB_ResetWanted)(((db)->aDb[i].pSchema->schemaFlags&(0x0008))==(0x0008
))
){
122603 sqlite3SchemaClear(db->aDb[i].pSchema);
122604 }
122605 }
122606 }
122607}
122608
122609/*
122610** Erase all schema information from all attached databases (including
122611** "main" and "temp") for a single database connection.
122612*/
122613SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
122614 int i;
122615 sqlite3BtreeEnterAll(db);
122616 for(i=0; i<db->nDb; i++){
122617 Db *pDb = &db->aDb[i];
122618 if( pDb->pSchema ){
122619 if( db->nSchemaLock==0 ){
122620 sqlite3SchemaClear(pDb->pSchema);
122621 }else{
122622 DbSetProperty(db, i, DB_ResetWanted)(db)->aDb[i].pSchema->schemaFlags|=(0x0008);
122623 }
122624 }
122625 }
122626 db->mDbFlags &= ~(DBFLAG_SchemaChange0x0001|DBFLAG_SchemaKnownOk0x0010);
122627 sqlite3VtabUnlockList(db);
122628 sqlite3BtreeLeaveAll(db);
122629 if( db->nSchemaLock==0 ){
122630 sqlite3CollapseDatabaseArray(db);
122631 }
122632}
122633
122634/*
122635** This routine is called when a commit occurs.
122636*/
122637SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3 *db){
122638 db->mDbFlags &= ~DBFLAG_SchemaChange0x0001;
122639}
122640
122641/*
122642** Set the expression associated with a column. This is usually
122643** the DEFAULT value, but might also be the expression that computes
122644** the value for a generated column.
122645*/
122646SQLITE_PRIVATEstatic void sqlite3ColumnSetExpr(
122647 Parse *pParse, /* Parsing context */
122648 Table *pTab, /* The table containing the column */
122649 Column *pCol, /* The column to receive the new DEFAULT expression */
122650 Expr *pExpr /* The new default expression */
122651){
122652 ExprList *pList;
122653 assert( IsOrdinaryTable(pTab) )((void) (0));
122654 pList = pTab->u.tab.pDfltList;
122655 if( pCol->iDflt==0
122656 || NEVER(pList==0)(pList==0)
122657 || NEVER(pList->nExpr<pCol->iDflt)(pList->nExpr<pCol->iDflt)
122658 ){
122659 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
122660 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
122661 }else{
122662 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
122663 pList->a[pCol->iDflt-1].pExpr = pExpr;
122664 }
122665}
122666
122667/*
122668** Return the expression associated with a column. The expression might be
122669** the DEFAULT clause or the AS clause of a generated column.
122670** Return NULL if the column has no associated expression.
122671*/
122672SQLITE_PRIVATEstatic Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
122673 if( pCol->iDflt==0 ) return 0;
122674 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return 0;
122675 if( NEVER(pTab->u.tab.pDfltList==0)(pTab->u.tab.pDfltList==0) ) return 0;
122676 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt)(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
122677 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
122678}
122679
122680/*
122681** Set the collating sequence name for a column.
122682*/
122683SQLITE_PRIVATEstatic void sqlite3ColumnSetColl(
122684 sqlite3 *db,
122685 Column *pCol,
122686 const char *zColl
122687){
122688 i64 nColl;
122689 i64 n;
122690 char *zNew;
122691 assert( zColl!=0 )((void) (0));
122692 n = sqlite3Strlen30(pCol->zCnName) + 1;
122693 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
122694 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
122695 }
122696 nColl = sqlite3Strlen30(zColl) + 1;
122697 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
122698 if( zNew ){
122699 pCol->zCnName = zNew;
122700 memcpy(pCol->zCnName + n, zColl, nColl);
122701 pCol->colFlags |= COLFLAG_HASCOLL0x0200;
122702 }
122703}
122704
122705/*
122706** Return the collating sequence name for a column
122707*/
122708SQLITE_PRIVATEstatic const char *sqlite3ColumnColl(Column *pCol){
122709 const char *z;
122710 if( (pCol->colFlags & COLFLAG_HASCOLL0x0200)==0 ) return 0;
122711 z = pCol->zCnName;
122712 while( *z ){ z++; }
122713 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
122714 do{ z++; }while( *z );
122715 }
122716 return z+1;
122717}
122718
122719/*
122720** Delete memory allocated for the column names of a table or view (the
122721** Table.aCol[] array).
122722*/
122723SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
122724 int i;
122725 Column *pCol;
122726 assert( pTable!=0 )((void) (0));
122727 assert( db!=0 )((void) (0));
122728 if( (pCol = pTable->aCol)!=0 ){
122729 for(i=0; i<pTable->nCol; i++, pCol++){
122730 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) )((void) (0));
122731 sqlite3DbFree(db, pCol->zCnName);
122732 }
122733 sqlite3DbNNFreeNN(db, pTable->aCol);
122734 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
122735 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
122736 }
122737 if( db->pnBytesFreed==0 ){
122738 pTable->aCol = 0;
122739 pTable->nCol = 0;
122740 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
122741 pTable->u.tab.pDfltList = 0;
122742 }
122743 }
122744 }
122745}
122746
122747/*
122748** Remove the memory data structures associated with the given
122749** Table. No changes are made to disk by this routine.
122750**
122751** This routine just deletes the data structure. It does not unlink
122752** the table data structure from the hash table. But it does destroy
122753** memory structures of the indices and foreign keys associated with
122754** the table.
122755**
122756** The db parameter is optional. It is needed if the Table object
122757** contains lookaside memory. (Table objects in the schema do not use
122758** lookaside memory, but some ephemeral Table objects do.) Or the
122759** db parameter can be used with db->pnBytesFreed to measure the memory
122760** used by the Table object.
122761*/
122762static void SQLITE_NOINLINE__attribute__((noinline)) deleteTable(sqlite3 *db, Table *pTable){
122763 Index *pIndex, *pNext;
122764
122765#ifdef SQLITE_DEBUG
122766 /* Record the number of outstanding lookaside allocations in schema Tables
122767 ** prior to doing any free() operations. Since schema Tables do not use
122768 ** lookaside, this number should not change.
122769 **
122770 ** If malloc has already failed, it may be that it failed while allocating
122771 ** a Table object that was going to be marked ephemeral. So do not check
122772 ** that no lookaside memory is used in this case either. */
122773 int nLookaside = 0;
122774 assert( db!=0 )((void) (0));
122775 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral0x00004000)==0 ){
122776 nLookaside = sqlite3LookasideUsed(db, 0);
122777 }
122778#endif
122779
122780 /* Delete all indices associated with this table. */
122781 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
122782 pNext = pIndex->pNext;
122783 assert( pIndex->pSchema==pTable->pSchema((void) (0))
122784 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) )((void) (0));
122785 if( db->pnBytesFreed==0 && !IsVirtual(pTable)((pTable)->eTabType==1) ){
122786 char *zName = pIndex->zName;
122787 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
122788 &pIndex->pSchema->idxHash, zName, 0
122789 );
122790 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
122791 assert( pOld==pIndex || pOld==0 )((void) (0));
122792 }
122793 sqlite3FreeIndex(db, pIndex);
122794 }
122795
122796 if( IsOrdinaryTable(pTable)((pTable)->eTabType==0) ){
122797 sqlite3FkDelete(db, pTable);
122798 }
122799#ifndef SQLITE_OMIT_VIRTUALTABLE
122800 else if( IsVirtual(pTable)((pTable)->eTabType==1) ){
122801 sqlite3VtabClear(db, pTable);
122802 }
122803#endif
122804 else{
122805 assert( IsView(pTable) )((void) (0));
122806 sqlite3SelectDelete(db, pTable->u.view.pSelect);
122807 }
122808
122809 /* Delete the Table structure itself.
122810 */
122811 sqlite3DeleteColumnNames(db, pTable);
122812 sqlite3DbFree(db, pTable->zName);
122813 sqlite3DbFree(db, pTable->zColAff);
122814 sqlite3ExprListDelete(db, pTable->pCheck);
122815 sqlite3DbFree(db, pTable);
122816
122817 /* Verify that no lookaside memory was used by schema tables */
122818 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) )((void) (0));
122819}
122820SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
122821 /* Do not delete the table until the reference count reaches zero. */
122822 assert( db!=0 )((void) (0));
122823 if( !pTable ) return;
122824 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
122825 deleteTable(db, pTable);
122826}
122827SQLITE_PRIVATEstatic void sqlite3DeleteTableGeneric(sqlite3 *db, void *pTable){
122828 sqlite3DeleteTable(db, (Table*)pTable);
122829}
122830
122831
122832/*
122833** Unlink the given table from the hash tables and the delete the
122834** table structure with all its indices and foreign keys.
122835*/
122836SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
122837 Table *p;
122838 Db *pDb;
122839
122840 assert( db!=0 )((void) (0));
122841 assert( iDb>=0 && iDb<db->nDb )((void) (0));
122842 assert( zTabName )((void) (0));
122843 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122844 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
122845 pDb = &db->aDb[iDb];
122846 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
122847 sqlite3DeleteTable(db, p);
122848 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
122849}
122850
122851/*
122852** Given a token, return a string that consists of the text of that
122853** token. Space to hold the returned string
122854** is obtained from sqliteMalloc() and must be freed by the calling
122855** function.
122856**
122857** Any quotation marks (ex: "name", 'name', [name], or `name`) that
122858** surround the body of the token are removed.
122859**
122860** Tokens are often just pointers into the original SQL text and so
122861** are not \000 terminated and are not persistent. The returned string
122862** is \000 terminated and is persistent.
122863*/
122864SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
122865 char *zName;
122866 if( pName ){
122867 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
122868 sqlite3Dequote(zName);
122869 }else{
122870 zName = 0;
122871 }
122872 return zName;
122873}
122874
122875/*
122876** Open the sqlite_schema table stored in database number iDb for
122877** writing. The table is opened using cursor 0.
122878*/
122879SQLITE_PRIVATEstatic void sqlite3OpenSchemaTable(Parse *p, int iDb){
122880 Vdbe *v = sqlite3GetVdbe(p);
122881 sqlite3TableLock(p, iDb, SCHEMA_ROOT1, 1, LEGACY_SCHEMA_TABLE"sqlite_master");
122882 sqlite3VdbeAddOp4Int(v, OP_OpenWrite113, 0, SCHEMA_ROOT1, iDb, 5);
122883 if( p->nTab==0 ){
122884 p->nTab = 1;
122885 }
122886}
122887
122888/*
122889** Parameter zName points to a nul-terminated buffer containing the name
122890** of a database ("main", "temp" or the name of an attached db). This
122891** function returns the index of the named database in db->aDb[], or
122892** -1 if the named db cannot be found.
122893*/
122894SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *db, const char *zName){
122895 int i = -1; /* Database number */
122896 if( zName ){
122897 Db *pDb;
122898 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
122899 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
122900 /* "main" is always an acceptable alias for the primary database
122901 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
122902 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
122903 }
122904 }
122905 return i;
122906}
122907
122908/*
122909** The token *pName contains the name of a database (either "main" or
122910** "temp" or the name of an attached db). This routine returns the
122911** index of the named database in db->aDb[], or -1 if the named db
122912** does not exist.
122913*/
122914SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3 *db, Token *pName){
122915 int i; /* Database number */
122916 char *zName; /* Name we are searching for */
122917 zName = sqlite3NameFromToken(db, pName);
122918 i = sqlite3FindDbName(db, zName);
122919 sqlite3DbFree(db, zName);
122920 return i;
122921}
122922
122923/* The table or view or trigger name is passed to this routine via tokens
122924** pName1 and pName2. If the table name was fully qualified, for example:
122925**
122926** CREATE TABLE xxx.yyy (...);
122927**
122928** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
122929** the table name is not fully qualified, i.e.:
122930**
122931** CREATE TABLE yyy(...);
122932**
122933** Then pName1 is set to "yyy" and pName2 is "".
122934**
122935** This routine sets the *ppUnqual pointer to point at the token (pName1 or
122936** pName2) that stores the unqualified table name. The index of the
122937** database "xxx" is returned.
122938*/
122939SQLITE_PRIVATEstatic int sqlite3TwoPartName(
122940 Parse *pParse, /* Parsing and code generating context */
122941 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
122942 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
122943 Token **pUnqual /* Write the unqualified object name here */
122944){
122945 int iDb; /* Database holding the object */
122946 sqlite3 *db = pParse->db;
122947
122948 assert( pName2!=0 )((void) (0));
122949 if( pName2->n>0 ){
122950 if( db->init.busy ) {
122951 sqlite3ErrorMsg(pParse, "corrupt database");
122952 return -1;
122953 }
122954 *pUnqual = pName2;
122955 iDb = sqlite3FindDb(db, pName1);
122956 if( iDb<0 ){
122957 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
122958 return -1;
122959 }
122960 }else{
122961 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE((void) (0))
122962 || (db->mDbFlags & DBFLAG_Vacuum)!=0)((void) (0));
122963 iDb = db->init.iDb;
122964 *pUnqual = pName1;
122965 }
122966 return iDb;
122967}
122968
122969/*
122970** True if PRAGMA writable_schema is ON
122971*/
122972SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3 *db){
122973 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
122974 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122975 SQLITE_WriteSchema );
122976 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122977 SQLITE_Defensive );
122978 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122979 (SQLITE_WriteSchema|SQLITE_Defensive) );
122980 return (db->flags&(SQLITE_WriteSchema0x00000001|SQLITE_Defensive0x10000000))==SQLITE_WriteSchema0x00000001;
122981}
122982
122983/*
122984** This routine is used to check if the UTF-8 string zName is a legal
122985** unqualified name for a new schema object (table, index, view or
122986** trigger). All names are legal except those that begin with the string
122987** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
122988** is reserved for internal use.
122989**
122990** When parsing the sqlite_schema table, this routine also checks to
122991** make sure the "type", "name", and "tbl_name" columns are consistent
122992** with the SQL.
122993*/
122994SQLITE_PRIVATEstatic int sqlite3CheckObjectName(
122995 Parse *pParse, /* Parsing context */
122996 const char *zName, /* Name of the object to check */
122997 const char *zType, /* Type of this object */
122998 const char *zTblName /* Parent table name for triggers and indexes */
122999){
123000 sqlite3 *db = pParse->db;
123001 if( sqlite3WritableSchema(db)
123002 || db->init.imposterTable
123003 || !sqlite3Config.bExtraSchemaChecks
123004 ){
123005 /* Skip these error checks for writable_schema=ON */
123006 return SQLITE_OK0;
123007 }
123008 if( db->init.busy ){
123009 if( sqlite3_stricmp(zType, db->init.azInit[0])
123010 || sqlite3_stricmp(zName, db->init.azInit[1])
123011 || sqlite3_stricmp(zTblName, db->init.azInit[2])
123012 ){
123013 sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
123014 return SQLITE_ERROR1;
123015 }
123016 }else{
123017 if( (pParse->nested==0 && 0==sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7))
123018 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
123019 ){
123020 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
123021 zName);
123022 return SQLITE_ERROR1;
123023 }
123024
123025 }
123026 return SQLITE_OK0;
123027}
123028
123029/*
123030** Return the PRIMARY KEY index of a table
123031*/
123032SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table *pTab){
123033 Index *p;
123034 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p)((p)->idxType==2); p=p->pNext){}
123035 return p;
123036}
123037
123038/*
123039** Convert an table column number into a index column number. That is,
123040** for the column iCol in the table (as defined by the CREATE TABLE statement)
123041** find the (first) offset of that column in index pIdx. Or return -1
123042** if column iCol is not used in index pIdx.
123043*/
123044SQLITE_PRIVATEstatic i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
123045 int i;
123046 for(i=0; i<pIdx->nColumn; i++){
123047 if( iCol==pIdx->aiColumn[i] ) return i;
123048 }
123049 return -1;
123050}
123051
123052#ifndef SQLITE_OMIT_GENERATED_COLUMNS
123053/* Convert a storage column number into a table column number.
123054**
123055** The storage column number (0,1,2,....) is the index of the value
123056** as it appears in the record on disk. The true column number
123057** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
123058**
123059** The storage column number is less than the table column number if
123060** and only there are VIRTUAL columns to the left.
123061**
123062** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
123063*/
123064SQLITE_PRIVATEstatic i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
123065 if( pTab->tabFlags & TF_HasVirtual0x00000020 ){
123066 int i;
123067 for(i=0; i<=iCol; i++){
123068 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) iCol++;
123069 }
123070 }
123071 return iCol;
123072}
123073#endif
123074
123075#ifndef SQLITE_OMIT_GENERATED_COLUMNS
123076/* Convert a table column number into a storage column number.
123077**
123078** The storage column number (0,1,2,....) is the index of the value
123079** as it appears in the record on disk. Or, if the input column is
123080** the N-th virtual column (zero-based) then the storage number is
123081** the number of non-virtual columns in the table plus N.
123082**
123083** The true column number is the index (0,1,2,...) of the column in
123084** the CREATE TABLE statement.
123085**
123086** If the input column is a VIRTUAL column, then it should not appear
123087** in storage. But the value sometimes is cached in registers that
123088** follow the range of registers used to construct storage. This
123089** avoids computing the same VIRTUAL column multiple times, and provides
123090** values for use by OP_Param opcodes in triggers. Hence, if the
123091** input column is a VIRTUAL table, put it after all the other columns.
123092**
123093** In the following, N means "normal column", S means STORED, and
123094** V means VIRTUAL. Suppose the CREATE TABLE has columns like this:
123095**
123096** CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
123097** -- 0 1 2 3 4 5 6 7 8
123098**
123099** Then the mapping from this function is as follows:
123100**
123101** INPUTS: 0 1 2 3 4 5 6 7 8
123102** OUTPUTS: 0 1 6 2 3 7 4 5 8
123103**
123104** So, in other words, this routine shifts all the virtual columns to
123105** the end.
123106**
123107** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
123108** this routine is a no-op macro. If the pTab does not have any virtual
123109** columns, then this routine is no-op that always return iCol. If iCol
123110** is negative (indicating the ROWID column) then this routine return iCol.
123111*/
123112SQLITE_PRIVATEstatic i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
123113 int i;
123114 i16 n;
123115 assert( iCol<pTab->nCol )((void) (0));
123116 if( (pTab->tabFlags & TF_HasVirtual0x00000020)==0 || iCol<0 ) return iCol;
123117 for(i=0, n=0; i<iCol; i++){
123118 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) n++;
123119 }
123120 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ){
123121 /* iCol is a virtual column itself */
123122 return pTab->nNVCol + i - n;
123123 }else{
123124 /* iCol is a normal or stored column */
123125 return n;
123126 }
123127}
123128#endif
123129
123130/*
123131** Insert a single OP_JournalMode query opcode in order to force the
123132** prepared statement to return false for sqlite3_stmt_readonly(). This
123133** is used by CREATE TABLE IF NOT EXISTS and similar if the table already
123134** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS
123135** will return false for sqlite3_stmt_readonly() even if that statement
123136** is a read-only no-op.
123137*/
123138static void sqlite3ForceNotReadOnly(Parse *pParse){
123139 int iReg = ++pParse->nMem;
123140 Vdbe *v = sqlite3GetVdbe(pParse);
123141 if( v ){
123142 sqlite3VdbeAddOp3(v, OP_JournalMode4, 0, iReg, PAGER_JOURNALMODE_QUERY(-1));
123143 sqlite3VdbeUsesBtree(v, 0);
123144 }
123145}
123146
123147/*
123148** Begin constructing a new table representation in memory. This is
123149** the first of several action routines that get called in response
123150** to a CREATE TABLE statement. In particular, this routine is called
123151** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
123152** flag is true if the table should be stored in the auxiliary database
123153** file instead of in the main database file. This is normally the case
123154** when the "TEMP" or "TEMPORARY" keyword occurs in between
123155** CREATE and TABLE.
123156**
123157** The new table record is initialized and put in pParse->pNewTable.
123158** As more of the CREATE TABLE statement is parsed, additional action
123159** routines will be called to add more information to this record.
123160** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
123161** is called to complete the construction of the new table record.
123162*/
123163SQLITE_PRIVATEstatic void sqlite3StartTable(
123164 Parse *pParse, /* Parser context */
123165 Token *pName1, /* First part of the name of the table or view */
123166 Token *pName2, /* Second part of the name of the table or view */
123167 int isTemp, /* True if this is a TEMP table */
123168 int isView, /* True if this is a VIEW */
123169 int isVirtual, /* True if this is a VIRTUAL table */
123170 int noErr /* Do nothing if table already exists */
123171){
123172 Table *pTable;
123173 char *zName = 0; /* The name of the new table */
123174 sqlite3 *db = pParse->db;
123175 Vdbe *v;
123176 int iDb; /* Database number to create the table in */
123177 Token *pName; /* Unqualified name of the table to create */
123178
123179 if( db->init.busy && db->init.newTnum==1 ){
123180 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
123181 iDb = db->init.iDb;
123182 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"));
123183 pName = pName1;
123184 }else{
123185 /* The common case */
123186 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
123187 if( iDb<0 ) return;
123188 if( !OMIT_TEMPDB0 && isTemp && pName2->n>0 && iDb!=1 ){
123189 /* If creating a temp table, the name may not be qualified. Unless
123190 ** the database name is "temp" anyway. */
123191 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
123192 return;
123193 }
123194 if( !OMIT_TEMPDB0 && isTemp ) iDb = 1;
123195 zName = sqlite3NameFromToken(db, pName);
123196 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
123197 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
123198 }
123199 }
123200 pParse->sNameToken = *pName;
123201 if( zName==0 ) return;
123202 if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
123203 goto begin_table_error;
123204 }
123205 if( db->init.iDb==1 ) isTemp = 1;
123206#ifndef SQLITE_OMIT_AUTHORIZATION
123207 assert( isTemp==0 || isTemp==1 )((void) (0));
123208 assert( isView==0 || isView==1 )((void) (0));
123209 {
123210 static const u8 aCode[] = {
123211 SQLITE_CREATE_TABLE2,
123212 SQLITE_CREATE_TEMP_TABLE4,
123213 SQLITE_CREATE_VIEW8,
123214 SQLITE_CREATE_TEMP_VIEW6
123215 };
123216 char *zDb = db->aDb[iDb].zDbSName;
123217 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(isTemp)((!0)&&(isTemp==1)?"sqlite_temp_master":"sqlite_master"
)
, 0, zDb) ){
123218 goto begin_table_error;
123219 }
123220 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
123221 zName, 0, zDb) ){
123222 goto begin_table_error;
123223 }
123224 }
123225#endif
123226
123227 /* Make sure the new table name does not collide with an existing
123228 ** index or table name in the same database. Issue an error message if
123229 ** it does. The exception is if the statement being parsed was passed
123230 ** to an sqlite3_declare_vtab() call. In that case only the column names
123231 ** and types will be used, so there is no need to test for namespace
123232 ** collisions.
123233 */
123234 if( !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
123235 char *zDb = db->aDb[iDb].zDbSName;
123236 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
123237 goto begin_table_error;
123238 }
123239 pTable = sqlite3FindTable(db, zName, zDb);
123240 if( pTable ){
123241 if( !noErr ){
123242 sqlite3ErrorMsg(pParse, "%s %T already exists",
123243 (IsView(pTable)((pTable)->eTabType==2)? "view" : "table"), pName);
123244 }else{
123245 assert( !db->init.busy || CORRUPT_DB )((void) (0));
123246 sqlite3CodeVerifySchema(pParse, iDb);
123247 sqlite3ForceNotReadOnly(pParse);
123248 }
123249 goto begin_table_error;
123250 }
123251 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
123252 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
123253 goto begin_table_error;
123254 }
123255 }
123256
123257 pTable = sqlite3DbMallocZero(db, sizeof(Table));
123258 if( pTable==0 ){
123259 assert( db->mallocFailed )((void) (0));
123260 pParse->rc = SQLITE_NOMEM_BKPT7;
123261 pParse->nErr++;
123262 goto begin_table_error;
123263 }
123264 pTable->zName = zName;
123265 pTable->iPKey = -1;
123266 pTable->pSchema = db->aDb[iDb].pSchema;
123267 pTable->nTabRef = 1;
123268#ifdef SQLITE_DEFAULT_ROWEST
123269 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
123270#else
123271 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
123272#endif
123273 assert( pParse->pNewTable==0 )((void) (0));
123274 pParse->pNewTable = pTable;
123275
123276 /* Begin generating the code that will insert the table record into
123277 ** the schema table. Note in particular that we must go ahead
123278 ** and allocate the record number for the table entry now. Before any
123279 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
123280 ** indices to be created and the table record must come before the
123281 ** indices. Hence, the record number for the table must be allocated
123282 ** now.
123283 */
123284 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
123285 int addr1;
123286 int fileFormat;
123287 int reg1, reg2, reg3;
123288 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
123289 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
123290 sqlite3BeginWriteOperation(pParse, 1, iDb);
123291
123292#ifndef SQLITE_OMIT_VIRTUALTABLE
123293 if( isVirtual ){
123294 sqlite3VdbeAddOp0(v, OP_VBegin170);
123295 }
123296#endif
123297
123298 /* If the file format and encoding in the database have not been set,
123299 ** set them now.
123300 */
123301 reg1 = pParse->regRowid = ++pParse->nMem;
123302 reg2 = pParse->regRoot = ++pParse->nMem;
123303 reg3 = ++pParse->nMem;
123304 sqlite3VdbeAddOp3(v, OP_ReadCookie99, iDb, reg3, BTREE_FILE_FORMAT2);
123305 sqlite3VdbeUsesBtree(v, iDb);
123306 addr1 = sqlite3VdbeAddOp1(v, OP_If16, reg3); VdbeCoverage(v);
123307 fileFormat = (db->flags & SQLITE_LegacyFileFmt0x00000002)!=0 ?
123308 1 : SQLITE_MAX_FILE_FORMAT4;
123309 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_FILE_FORMAT2, fileFormat);
123310 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_TEXT_ENCODING5, ENC(db)((db)->enc));
123311 sqlite3VdbeJumpHere(v, addr1);
123312
123313 /* This just creates a place-holder record in the sqlite_schema table.
123314 ** The record created does not contain anything yet. It will be replaced
123315 ** by the real entry in code generated at sqlite3EndTable().
123316 **
123317 ** The rowid for the new entry is left in register pParse->regRowid.
123318 ** The root page number of the new table is left in reg pParse->regRoot.
123319 ** The rowid and root page number values are needed by the code that
123320 ** sqlite3EndTable will generate.
123321 */
123322#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
123323 if( isView || isVirtual ){
123324 sqlite3VdbeAddOp2(v, OP_Integer71, 0, reg2);
123325 }else
123326#endif
123327 {
123328 assert( !pParse->bReturning )((void) (0));
123329 pParse->u1.addrCrTab =
123330 sqlite3VdbeAddOp3(v, OP_CreateBtree147, iDb, reg2, BTREE_INTKEY1);
123331 }
123332 sqlite3OpenSchemaTable(pParse, iDb);
123333 sqlite3VdbeAddOp2(v, OP_NewRowid127, 0, reg1);
123334 sqlite3VdbeAddOp4(v, OP_Blob77, 6, reg3, 0, nullRow, P4_STATIC(-1));
123335 sqlite3VdbeAddOp3(v, OP_Insert128, 0, reg3, reg1);
123336 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
123337 sqlite3VdbeAddOp0(v, OP_Close122);
123338 }
123339
123340 /* Normal (non-error) return. */
123341 return;
123342
123343 /* If an error occurs, we jump here */
123344begin_table_error:
123345 pParse->checkSchema = 1;
123346 sqlite3DbFree(db, zName);
123347 return;
123348}
123349
123350/* Set properties of a table column based on the (magical)
123351** name of the column.
123352*/
123353#if SQLITE_ENABLE_HIDDEN_COLUMNS
123354SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
123355 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
123356 pCol->colFlags |= COLFLAG_HIDDEN0x0002;
123357 if( pTab ) pTab->tabFlags |= TF_HasHidden0x00000002;
123358 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN0x0002) ){
123359 pTab->tabFlags |= TF_OOOHidden0x00000400;
123360 }
123361}
123362#endif
123363
123364/*
123365** Clean up the data structures associated with the RETURNING clause.
123366*/
123367static void sqlite3DeleteReturning(sqlite3 *db, void *pArg){
123368 Returning *pRet = (Returning*)pArg;
123369 Hash *pHash;
123370 pHash = &(db->aDb[1].pSchema->trigHash);
123371 sqlite3HashInsert(pHash, pRet->zName, 0);
123372 sqlite3ExprListDelete(db, pRet->pReturnEL);
123373 sqlite3DbFree(db, pRet);
123374}
123375
123376/*
123377** Add the RETURNING clause to the parse currently underway.
123378**
123379** This routine creates a special TEMP trigger that will fire for each row
123380** of the DML statement. That TEMP trigger contains a single SELECT
123381** statement with a result set that is the argument of the RETURNING clause.
123382** The trigger has the Trigger.bReturning flag and an opcode of
123383** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
123384** knows to handle it specially. The TEMP trigger is automatically
123385** removed at the end of the parse.
123386**
123387** When this routine is called, we do not yet know if the RETURNING clause
123388** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
123389** RETURNING trigger instead. It will then be converted into the appropriate
123390** type on the first call to sqlite3TriggersExist().
123391*/
123392SQLITE_PRIVATEstatic void sqlite3AddReturning(Parse *pParse, ExprList *pList){
123393 Returning *pRet;
123394 Hash *pHash;
123395 sqlite3 *db = pParse->db;
123396 if( pParse->pNewTrigger ){
123397 sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger");
123398 }else{
123399 assert( pParse->bReturning==0 || pParse->ifNotExists )((void) (0));
123400 }
123401 pParse->bReturning = 1;
123402 pRet = sqlite3DbMallocZero(db, sizeof(*pRet));
123403 if( pRet==0 ){
123404 sqlite3ExprListDelete(db, pList);
123405 return;
123406 }
123407 pParse->u1.pReturning = pRet;
123408 pRet->pParse = pParse;
123409 pRet->pReturnEL = pList;
123410 sqlite3ParserAddCleanup(pParse, sqlite3DeleteReturning, pRet);
123411 testcase( pParse->earlyCleanup );
123412 if( db->mallocFailed ) return;
123413 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
123414 "sqlite_returning_%p", pParse);
123415 pRet->retTrig.zName = pRet->zName;
123416 pRet->retTrig.op = TK_RETURNING151;
123417 pRet->retTrig.tr_tm = TRIGGER_AFTER2;
123418 pRet->retTrig.bReturning = 1;
123419 pRet->retTrig.pSchema = db->aDb[1].pSchema;
123420 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
123421 pRet->retTrig.step_list = &pRet->retTStep;
123422 pRet->retTStep.op = TK_RETURNING151;
123423 pRet->retTStep.pTrig = &pRet->retTrig;
123424 pRet->retTStep.pExprList = pList;
123425 pHash = &(db->aDb[1].pSchema->trigHash);
123426 assert( sqlite3HashFind(pHash, pRet->zName)==0((void) (0))
123427 || pParse->nErr || pParse->ifNotExists )((void) (0));
123428 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
123429 ==&pRet->retTrig ){
123430 sqlite3OomFault(db);
123431 }
123432}
123433
123434/*
123435** Add a new column to the table currently being constructed.
123436**
123437** The parser calls this routine once for each column declaration
123438** in a CREATE TABLE statement. sqlite3StartTable() gets called
123439** first to get things going. Then this routine is called for each
123440** column.
123441*/
123442SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
123443 Table *p;
123444 int i;
123445 char *z;
123446 char *zType;
123447 Column *pCol;
123448 sqlite3 *db = pParse->db;
123449 u8 hName;
123450 Column *aNew;
123451 u8 eType = COLTYPE_CUSTOM0;
123452 u8 szEst = 1;
123453 char affinity = SQLITE_AFF_BLOB0x41;
123454
123455 if( (p = pParse->pNewTable)==0 ) return;
123456 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
123457 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
123458 return;
123459 }
123460 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3DequoteToken(&sName);
123461
123462 /* Because keywords GENERATE ALWAYS can be converted into identifiers
123463 ** by the parser, we can sometimes end up with a typename that ends
123464 ** with "generated always". Check for this case and omit the surplus
123465 ** text. */
123466 if( sType.n>=16
123467 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
123468 ){
123469 sType.n -= 6;
123470 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--;
123471 if( sType.n>=9
123472 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
123473 ){
123474 sType.n -= 9;
123475 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1])(sqlite3CtypeMap[(unsigned char)(sType.z[sType.n-1])]&0x01
)
) sType.n--;
123476 }
123477 }
123478
123479 /* Check for standard typenames. For standard typenames we will
123480 ** set the Column.eType field rather than storing the typename after
123481 ** the column name, in order to save space. */
123482 if( sType.n>=3 ){
123483 sqlite3DequoteToken(&sType);
123484 for(i=0; i<SQLITE_N_STDTYPE6; i++){
123485 if( sType.n==sqlite3StdTypeLen[i]
123486 && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0
123487 ){
123488 sType.n = 0;
123489 eType = i+1;
123490 affinity = sqlite3StdTypeAffinity[i];
123491 if( affinity<=SQLITE_AFF_TEXT0x42 ) szEst = 5;
123492 break;
123493 }
123494 }
123495 }
123496
123497 z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
123498 if( z==0 ) return;
123499 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameTokenMap(pParse, (void*)z, &sName);
123500 memcpy(z, sName.z, sName.n);
123501 z[sName.n] = 0;
123502 sqlite3Dequote(z);
123503 hName = sqlite3StrIHash(z);
123504 for(i=0; i<p->nCol; i++){
123505 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
123506 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
123507 sqlite3DbFree(db, z);
123508 return;
123509 }
123510 }
123511 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
123512 if( aNew==0 ){
123513 sqlite3DbFree(db, z);
123514 return;
123515 }
123516 p->aCol = aNew;
123517 pCol = &p->aCol[p->nCol];
123518 memset(pCol, 0, sizeof(p->aCol[0]));
123519 pCol->zCnName = z;
123520 pCol->hName = hName;
123521 sqlite3ColumnPropertiesFromName(p, pCol);
123522
123523 if( sType.n==0 ){
123524 /* If there is no type specified, columns have the default affinity
123525 ** 'BLOB' with a default size of 4 bytes. */
123526 pCol->affinity = affinity;
123527 pCol->eCType = eType;
123528 pCol->szEst = szEst;
123529#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123530 if( affinity==SQLITE_AFF_BLOB0x41 ){
123531 if( 4>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
123532 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
123533 }
123534 }
123535#endif
123536 }else{
123537 zType = z + sqlite3Strlen30(z) + 1;
123538 memcpy(zType, sType.z, sType.n);
123539 zType[sType.n] = 0;
123540 sqlite3Dequote(zType);
123541 pCol->affinity = sqlite3AffinityType(zType, pCol);
123542 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
123543 }
123544 p->nCol++;
123545 p->nNVCol++;
123546 pParse->constraintName.n = 0;
123547}
123548
123549/*
123550** This routine is called by the parser while in the middle of
123551** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
123552** been seen on a column. This routine sets the notNull flag on
123553** the column currently under construction.
123554*/
123555SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse *pParse, int onError){
123556 Table *p;
123557 Column *pCol;
123558 p = pParse->pNewTable;
123559 if( p==0 || NEVER(p->nCol<1)(p->nCol<1) ) return;
123560 pCol = &p->aCol[p->nCol-1];
123561 pCol->notNull = (u8)onError;
123562 p->tabFlags |= TF_HasNotNull0x00000800;
123563
123564 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
123565 ** on this column. */
123566 if( pCol->colFlags & COLFLAG_UNIQUE0x0008 ){
123567 Index *pIdx;
123568 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
123569 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None )((void) (0));
123570 if( pIdx->aiColumn[0]==p->nCol-1 ){
123571 pIdx->uniqNotNull = 1;
123572 }
123573 }
123574 }
123575}
123576
123577/*
123578** Scan the column type name zType (length nType) and return the
123579** associated affinity type.
123580**
123581** This routine does a case-independent search of zType for the
123582** substrings in the following table. If one of the substrings is
123583** found, the corresponding affinity is returned. If zType contains
123584** more than one of the substrings, entries toward the top of
123585** the table take priority. For example, if zType is 'BLOBINT',
123586** SQLITE_AFF_INTEGER is returned.
123587**
123588** Substring | Affinity
123589** --------------------------------
123590** 'INT' | SQLITE_AFF_INTEGER
123591** 'CHAR' | SQLITE_AFF_TEXT
123592** 'CLOB' | SQLITE_AFF_TEXT
123593** 'TEXT' | SQLITE_AFF_TEXT
123594** 'BLOB' | SQLITE_AFF_BLOB
123595** 'REAL' | SQLITE_AFF_REAL
123596** 'FLOA' | SQLITE_AFF_REAL
123597** 'DOUB' | SQLITE_AFF_REAL
123598**
123599** If none of the substrings in the above table are found,
123600** SQLITE_AFF_NUMERIC is returned.
123601*/
123602SQLITE_PRIVATEstatic char sqlite3AffinityType(const char *zIn, Column *pCol){
123603 u32 h = 0;
123604 char aff = SQLITE_AFF_NUMERIC0x43;
123605 const char *zChar = 0;
123606
123607 assert( zIn!=0 )((void) (0));
123608 while( zIn[0] ){
123609 u8 x = *(u8*)zIn;
123610 h = (h<<8) + sqlite3UpperToLower[x];
123611 zIn++;
123612 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
123613 aff = SQLITE_AFF_TEXT0x42;
123614 zChar = zIn;
123615 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
123616 aff = SQLITE_AFF_TEXT0x42;
123617 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
123618 aff = SQLITE_AFF_TEXT0x42;
123619 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
123620 && (aff==SQLITE_AFF_NUMERIC0x43 || aff==SQLITE_AFF_REAL0x45) ){
123621 aff = SQLITE_AFF_BLOB0x41;
123622 if( zIn[0]=='(' ) zChar = zIn;
123623#ifndef SQLITE_OMIT_FLOATING_POINT
123624 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
123625 && aff==SQLITE_AFF_NUMERIC0x43 ){
123626 aff = SQLITE_AFF_REAL0x45;
123627 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
123628 && aff==SQLITE_AFF_NUMERIC0x43 ){
123629 aff = SQLITE_AFF_REAL0x45;
123630 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
123631 && aff==SQLITE_AFF_NUMERIC0x43 ){
123632 aff = SQLITE_AFF_REAL0x45;
123633#endif
123634 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
123635 aff = SQLITE_AFF_INTEGER0x44;
123636 break;
123637 }
123638 }
123639
123640 /* If pCol is not NULL, store an estimate of the field size. The
123641 ** estimate is scaled so that the size of an integer is 1. */
123642 if( pCol ){
123643 int v = 0; /* default size is approx 4 bytes */
123644 if( aff<SQLITE_AFF_NUMERIC0x43 ){
123645 if( zChar ){
123646 while( zChar[0] ){
123647 if( sqlite3Isdigit(zChar[0])(sqlite3CtypeMap[(unsigned char)(zChar[0])]&0x04) ){
123648 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
123649 sqlite3GetInt32(zChar, &v);
123650 break;
123651 }
123652 zChar++;
123653 }
123654 }else{
123655 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
123656 }
123657 }
123658#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123659 if( v>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
123660 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
123661 }
123662#endif
123663 v = v/4 + 1;
123664 if( v>255 ) v = 255;
123665 pCol->szEst = v;
123666 }
123667 return aff;
123668}
123669
123670/*
123671** The expression is the default value for the most recently added column
123672** of the table currently under construction.
123673**
123674** Default value expressions must be constant. Raise an exception if this
123675** is not the case.
123676**
123677** This routine is called by the parser while in the middle of
123678** parsing a CREATE TABLE statement.
123679*/
123680SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(
123681 Parse *pParse, /* Parsing context */
123682 Expr *pExpr, /* The parsed expression of the default value */
123683 const char *zStart, /* Start of the default value text */
123684 const char *zEnd /* First character past end of default value text */
123685){
123686 Table *p;
123687 Column *pCol;
123688 sqlite3 *db = pParse->db;
123689 p = pParse->pNewTable;
123690 if( p!=0 ){
123691 int isInit = db->init.busy && db->init.iDb!=1;
123692 pCol = &(p->aCol[p->nCol-1]);
123693 if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
123694 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
123695 pCol->zCnName);
123696#ifndef SQLITE_OMIT_GENERATED_COLUMNS
123697 }else if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
123698 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123699 testcase( pCol->colFlags & COLFLAG_STORED );
123700 sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
123701#endif
123702 }else{
123703 /* A copy of pExpr is used instead of the original, as pExpr contains
123704 ** tokens that point to volatile memory.
123705 */
123706 Expr x, *pDfltExpr;
123707 memset(&x, 0, sizeof(x));
123708 x.op = TK_SPAN181;
123709 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
123710 x.pLeft = pExpr;
123711 x.flags = EP_Skip0x002000;
123712 pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE0x0001);
123713 sqlite3DbFree(db, x.u.zToken);
123714 sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr);
123715 }
123716 }
123717 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
123718 sqlite3RenameExprUnmap(pParse, pExpr);
123719 }
123720 sqlite3ExprDelete(db, pExpr);
123721}
123722
123723/*
123724** Backwards Compatibility Hack:
123725**
123726** Historical versions of SQLite accepted strings as column names in
123727** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
123728**
123729** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
123730** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
123731**
123732** This is goofy. But to preserve backwards compatibility we continue to
123733** accept it. This routine does the necessary conversion. It converts
123734** the expression given in its argument from a TK_STRING into a TK_ID
123735** if the expression is just a TK_STRING with an optional COLLATE clause.
123736** If the expression is anything other than TK_STRING, the expression is
123737** unchanged.
123738*/
123739static void sqlite3StringToId(Expr *p){
123740 if( p->op==TK_STRING118 ){
123741 p->op = TK_ID60;
123742 }else if( p->op==TK_COLLATE114 && p->pLeft->op==TK_STRING118 ){
123743 p->pLeft->op = TK_ID60;
123744 }
123745}
123746
123747/*
123748** Tag the given column as being part of the PRIMARY KEY
123749*/
123750static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){
123751 pCol->colFlags |= COLFLAG_PRIMKEY0x0001;
123752#ifndef SQLITE_OMIT_GENERATED_COLUMNS
123753 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
123754 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123755 testcase( pCol->colFlags & COLFLAG_STORED );
123756 sqlite3ErrorMsg(pParse,
123757 "generated columns cannot be part of the PRIMARY KEY");
123758 }
123759#endif
123760}
123761
123762/*
123763** Designate the PRIMARY KEY for the table. pList is a list of names
123764** of columns that form the primary key. If pList is NULL, then the
123765** most recently added column of the table is the primary key.
123766**
123767** A table can have at most one primary key. If the table already has
123768** a primary key (and this is the second primary key) then create an
123769** error.
123770**
123771** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
123772** then we will try to use that column as the rowid. Set the Table.iPKey
123773** field of the table under construction to be the index of the
123774** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
123775** no INTEGER PRIMARY KEY.
123776**
123777** If the key is not an INTEGER PRIMARY KEY, then create a unique
123778** index for the key. No index is created for INTEGER PRIMARY KEYs.
123779*/
123780SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(
123781 Parse *pParse, /* Parsing context */
123782 ExprList *pList, /* List of field names to be indexed */
123783 int onError, /* What to do with a uniqueness conflict */
123784 int autoInc, /* True if the AUTOINCREMENT keyword is present */
123785 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
123786){
123787 Table *pTab = pParse->pNewTable;
123788 Column *pCol = 0;
123789 int iCol = -1, i;
123790 int nTerm;
123791 if( pTab==0 ) goto primary_key_exit;
123792 if( pTab->tabFlags & TF_HasPrimaryKey0x00000004 ){
123793 sqlite3ErrorMsg(pParse,
123794 "table \"%s\" has more than one primary key", pTab->zName);
123795 goto primary_key_exit;
123796 }
123797 pTab->tabFlags |= TF_HasPrimaryKey0x00000004;
123798 if( pList==0 ){
123799 iCol = pTab->nCol - 1;
123800 pCol = &pTab->aCol[iCol];
123801 makeColumnPartOfPrimaryKey(pParse, pCol);
123802 nTerm = 1;
123803 }else{
123804 nTerm = pList->nExpr;
123805 for(i=0; i<nTerm; i++){
123806 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
123807 assert( pCExpr!=0 )((void) (0));
123808 sqlite3StringToId(pCExpr);
123809 if( pCExpr->op==TK_ID60 ){
123810 const char *zCName;
123811 assert( !ExprHasProperty(pCExpr, EP_IntValue) )((void) (0));
123812 zCName = pCExpr->u.zToken;
123813 for(iCol=0; iCol<pTab->nCol; iCol++){
123814 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
123815 pCol = &pTab->aCol[iCol];
123816 makeColumnPartOfPrimaryKey(pParse, pCol);
123817 break;
123818 }
123819 }
123820 }
123821 }
123822 }
123823 if( nTerm==1
123824 && pCol
123825 && pCol->eCType==COLTYPE_INTEGER4
123826 && sortOrder!=SQLITE_SO_DESC1
123827 ){
123828 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList ){
123829 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
123830 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
123831 }
123832 pTab->iPKey = iCol;
123833 pTab->keyConf = (u8)onError;
123834 assert( autoInc==0 || autoInc==1 )((void) (0));
123835 pTab->tabFlags |= autoInc*TF_Autoincrement0x00000008;
123836 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
123837 (void)sqlite3HasExplicitNulls(pParse, pList);
123838 }else if( autoInc ){
123839#ifndef SQLITE_OMIT_AUTOINCREMENT
123840 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
123841 "INTEGER PRIMARY KEY");
123842#endif
123843 }else{
123844 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
123845 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY2);
123846 pList = 0;
123847 }
123848
123849primary_key_exit:
123850 sqlite3ExprListDelete(pParse->db, pList);
123851 return;
123852}
123853
123854/*
123855** Add a new CHECK constraint to the table currently under construction.
123856*/
123857SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(
123858 Parse *pParse, /* Parsing context */
123859 Expr *pCheckExpr, /* The check expression */
123860 const char *zStart, /* Opening "(" */
123861 const char *zEnd /* Closing ")" */
123862){
123863#ifndef SQLITE_OMIT_CHECK
123864 Table *pTab = pParse->pNewTable;
123865 sqlite3 *db = pParse->db;
123866 if( pTab && !IN_DECLARE_VTAB(pParse->eParseMode==1)
123867 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
123868 ){
123869 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
123870 if( pParse->constraintName.n ){
123871 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
123872 }else{
123873 Token t;
123874 for(zStart++; sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01); zStart++){}
123875 while( sqlite3Isspace(zEnd[-1])(sqlite3CtypeMap[(unsigned char)(zEnd[-1])]&0x01) ){ zEnd--; }
123876 t.z = zStart;
123877 t.n = (int)(zEnd - t.z);
123878 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
123879 }
123880 }else
123881#endif
123882 {
123883 sqlite3ExprDelete(pParse->db, pCheckExpr);
123884 }
123885}
123886
123887/*
123888** Set the collation function of the most recently parsed table column
123889** to the CollSeq given.
123890*/
123891SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse *pParse, Token *pToken){
123892 Table *p;
123893 int i;
123894 char *zColl; /* Dequoted name of collation sequence */
123895 sqlite3 *db;
123896
123897 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return;
123898 i = p->nCol-1;
123899 db = pParse->db;
123900 zColl = sqlite3NameFromToken(db, pToken);
123901 if( !zColl ) return;
123902
123903 if( sqlite3LocateCollSeq(pParse, zColl) ){
123904 Index *pIdx;
123905 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
123906
123907 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
123908 ** then an index may have been created on this column before the
123909 ** collation type was added. Correct this if it is the case.
123910 */
123911 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
123912 assert( pIdx->nKeyCol==1 )((void) (0));
123913 if( pIdx->aiColumn[0]==i ){
123914 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
123915 }
123916 }
123917 }
123918 sqlite3DbFree(db, zColl);
123919}
123920
123921/* Change the most recently parsed column to be a GENERATED ALWAYS AS
123922** column.
123923*/
123924SQLITE_PRIVATEstatic void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){
123925#ifndef SQLITE_OMIT_GENERATED_COLUMNS
123926 u8 eType = COLFLAG_VIRTUAL0x0020;
123927 Table *pTab = pParse->pNewTable;
123928 Column *pCol;
123929 if( pTab==0 ){
123930 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
123931 goto generated_done;
123932 }
123933 pCol = &(pTab->aCol[pTab->nCol-1]);
123934 if( IN_DECLARE_VTAB(pParse->eParseMode==1) ){
123935 sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
123936 goto generated_done;
123937 }
123938 if( pCol->iDflt>0 ) goto generated_error;
123939 if( pType ){
123940 if( pType->n==7 && sqlite3StrNICmpsqlite3_strnicmp("virtual",pType->z,7)==0 ){
123941 /* no-op */
123942 }else if( pType->n==6 && sqlite3StrNICmpsqlite3_strnicmp("stored",pType->z,6)==0 ){
123943 eType = COLFLAG_STORED0x0040;
123944 }else{
123945 goto generated_error;
123946 }
123947 }
123948 if( eType==COLFLAG_VIRTUAL0x0020 ) pTab->nNVCol--;
123949 pCol->colFlags |= eType;
123950 assert( TF_HasVirtual==COLFLAG_VIRTUAL )((void) (0));
123951 assert( TF_HasStored==COLFLAG_STORED )((void) (0));
123952 pTab->tabFlags |= eType;
123953 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
123954 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
123955 }
123956 if( ALWAYS(pExpr)(pExpr) && pExpr->op==TK_ID60 ){
123957 /* The value of a generated column needs to be a real expression, not
123958 ** just a reference to another column, in order for covering index
123959 ** optimizations to work correctly. So if the value is not an expression,
123960 ** turn it into one by adding a unary "+" operator. */
123961 pExpr = sqlite3PExpr(pParse, TK_UPLUS173, pExpr, 0);
123962 }
123963 if( pExpr && pExpr->op!=TK_RAISE72 ) pExpr->affExpr = pCol->affinity;
123964 sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
123965 pExpr = 0;
123966 goto generated_done;
123967
123968generated_error:
123969 sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
123970 pCol->zCnName);
123971generated_done:
123972 sqlite3ExprDelete(pParse->db, pExpr);
123973#else
123974 /* Throw and error for the GENERATED ALWAYS AS clause if the
123975 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
123976 sqlite3ErrorMsg(pParse, "generated columns not supported");
123977 sqlite3ExprDelete(pParse->db, pExpr);
123978#endif
123979}
123980
123981/*
123982** Generate code that will increment the schema cookie.
123983**
123984** The schema cookie is used to determine when the schema for the
123985** database changes. After each schema change, the cookie value
123986** changes. When a process first reads the schema it records the
123987** cookie. Thereafter, whenever it goes to access the database,
123988** it checks the cookie to make sure the schema has not changed
123989** since it was last read.
123990**
123991** This plan is not completely bullet-proof. It is possible for
123992** the schema to change multiple times and for the cookie to be
123993** set back to prior value. But schema changes are infrequent
123994** and the probability of hitting the same cookie value is only
123995** 1 chance in 2^32. So we're safe enough.
123996**
123997** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
123998** the schema-version whenever the schema changes.
123999*/
124000SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse *pParse, int iDb){
124001 sqlite3 *db = pParse->db;
124002 Vdbe *v = pParse->pVdbe;
124003 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
124004 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_SCHEMA_VERSION1,
124005 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
124006}
124007
124008/*
124009** Measure the number of characters needed to output the given
124010** identifier. The number returned includes any quotes used
124011** but does not include the null terminator.
124012**
124013** The estimate is conservative. It might be larger that what is
124014** really needed.
124015*/
124016static int identLength(const char *z){
124017 int n;
124018 for(n=0; *z; n++, z++){
124019 if( *z=='"' ){ n++; }
124020 }
124021 return n + 2;
124022}
124023
124024/*
124025** The first parameter is a pointer to an output buffer. The second
124026** parameter is a pointer to an integer that contains the offset at
124027** which to write into the output buffer. This function copies the
124028** nul-terminated string pointed to by the third parameter, zSignedIdent,
124029** to the specified offset in the buffer and updates *pIdx to refer
124030** to the first byte after the last byte written before returning.
124031**
124032** If the string zSignedIdent consists entirely of alphanumeric
124033** characters, does not begin with a digit and is not an SQL keyword,
124034** then it is copied to the output buffer exactly as it is. Otherwise,
124035** it is quoted using double-quotes.
124036*/
124037static void identPut(char *z, int *pIdx, char *zSignedIdent){
124038 unsigned char *zIdent = (unsigned char*)zSignedIdent;
124039 int i, j, needQuote;
124040 i = *pIdx;
124041
124042 for(j=0; zIdent[j]; j++){
124043 if( !sqlite3Isalnum(zIdent[j])(sqlite3CtypeMap[(unsigned char)(zIdent[j])]&0x06) && zIdent[j]!='_' ) break;
124044 }
124045 needQuote = sqlite3Isdigit(zIdent[0])(sqlite3CtypeMap[(unsigned char)(zIdent[0])]&0x04)
124046 || sqlite3KeywordCode(zIdent, j)!=TK_ID60
124047 || zIdent[j]!=0
124048 || j==0;
124049
124050 if( needQuote ) z[i++] = '"';
124051 for(j=0; zIdent[j]; j++){
124052 z[i++] = zIdent[j];
124053 if( zIdent[j]=='"' ) z[i++] = '"';
124054 }
124055 if( needQuote ) z[i++] = '"';
124056 z[i] = 0;
124057 *pIdx = i;
124058}
124059
124060/*
124061** Generate a CREATE TABLE statement appropriate for the given
124062** table. Memory to hold the text of the statement is obtained
124063** from sqliteMalloc() and must be freed by the calling function.
124064*/
124065static char *createTableStmt(sqlite3 *db, Table *p){
124066 int i, k, n;
124067 char *zStmt;
124068 char *zSep, *zSep2, *zEnd;
124069 Column *pCol;
124070 n = 0;
124071 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
124072 n += identLength(pCol->zCnName) + 5;
124073 }
124074 n += identLength(p->zName);
124075 if( n<50 ){
124076 zSep = "";
124077 zSep2 = ",";
124078 zEnd = ")";
124079 }else{
124080 zSep = "\n ";
124081 zSep2 = ",\n ";
124082 zEnd = "\n)";
124083 }
124084 n += 35 + 6*p->nCol;
124085 zStmt = sqlite3DbMallocRaw(0, n);
124086 if( zStmt==0 ){
124087 sqlite3OomFault(db);
124088 return 0;
124089 }
124090 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
124091 k = sqlite3Strlen30(zStmt);
124092 identPut(zStmt, &k, p->zName);
124093 zStmt[k++] = '(';
124094 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
124095 static const char * const azType[] = {
124096 /* SQLITE_AFF_BLOB */ "",
124097 /* SQLITE_AFF_TEXT */ " TEXT",
124098 /* SQLITE_AFF_NUMERIC */ " NUM",
124099 /* SQLITE_AFF_INTEGER */ " INT",
124100 /* SQLITE_AFF_REAL */ " REAL",
124101 /* SQLITE_AFF_FLEXNUM */ " NUM",
124102 };
124103 int len;
124104 const char *zType;
124105
124106 sqlite3_snprintf(n-k, &zStmt[k], zSep);
124107 k += sqlite3Strlen30(&zStmt[k]);
124108 zSep = zSep2;
124109 identPut(zStmt, &k, pCol->zCnName);
124110 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 )((void) (0));
124111 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) )((void) (0));
124112 testcase( pCol->affinity==SQLITE_AFF_BLOB );
124113 testcase( pCol->affinity==SQLITE_AFF_TEXT );
124114 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
124115 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
124116 testcase( pCol->affinity==SQLITE_AFF_REAL );
124117 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
124118
124119 zType = azType[pCol->affinity - SQLITE_AFF_BLOB0x41];
124120 len = sqlite3Strlen30(zType);
124121 assert( pCol->affinity==SQLITE_AFF_BLOB((void) (0))
124122 || pCol->affinity==SQLITE_AFF_FLEXNUM((void) (0))
124123 || pCol->affinity==sqlite3AffinityType(zType, 0) )((void) (0));
124124 memcpy(&zStmt[k], zType, len);
124125 k += len;
124126 assert( k<=n )((void) (0));
124127 }
124128 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
124129 return zStmt;
124130}
124131
124132/*
124133** Resize an Index object to hold N columns total. Return SQLITE_OK
124134** on success and SQLITE_NOMEM on an OOM error.
124135*/
124136static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
124137 char *zExtra;
124138 int nByte;
124139 if( pIdx->nColumn>=N ) return SQLITE_OK0;
124140 assert( pIdx->isResized==0 )((void) (0));
124141 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
124142 zExtra = sqlite3DbMallocZero(db, nByte);
124143 if( zExtra==0 ) return SQLITE_NOMEM_BKPT7;
124144 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
124145 pIdx->azColl = (const char**)zExtra;
124146 zExtra += sizeof(char*)*N;
124147 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
124148 pIdx->aiRowLogEst = (LogEst*)zExtra;
124149 zExtra += sizeof(LogEst)*N;
124150 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
124151 pIdx->aiColumn = (i16*)zExtra;
124152 zExtra += sizeof(i16)*N;
124153 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
124154 pIdx->aSortOrder = (u8*)zExtra;
124155 pIdx->nColumn = N;
124156 pIdx->isResized = 1;
124157 return SQLITE_OK0;
124158}
124159
124160/*
124161** Estimate the total row width for a table.
124162*/
124163static void estimateTableWidth(Table *pTab){
124164 unsigned wTable = 0;
124165 const Column *pTabCol;
124166 int i;
124167 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
124168 wTable += pTabCol->szEst;
124169 }
124170 if( pTab->iPKey<0 ) wTable++;
124171 pTab->szTabRow = sqlite3LogEst(wTable*4);
124172}
124173
124174/*
124175** Estimate the average size of a row for an index.
124176*/
124177static void estimateIndexWidth(Index *pIdx){
124178 unsigned wIndex = 0;
124179 int i;
124180 const Column *aCol = pIdx->pTable->aCol;
124181 for(i=0; i<pIdx->nColumn; i++){
124182 i16 x = pIdx->aiColumn[i];
124183 assert( x<pIdx->pTable->nCol )((void) (0));
124184 wIndex += x<0 ? 1 : aCol[x].szEst;
124185 }
124186 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
124187}
124188
124189/* Return true if column number x is any of the first nCol entries of aiCol[].
124190** This is used to determine if the column number x appears in any of the
124191** first nCol entries of an index.
124192*/
124193static int hasColumn(const i16 *aiCol, int nCol, int x){
124194 while( nCol-- > 0 ){
124195 if( x==*(aiCol++) ){
124196 return 1;
124197 }
124198 }
124199 return 0;
124200}
124201
124202/*
124203** Return true if any of the first nKey entries of index pIdx exactly
124204** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
124205** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
124206** or may not be the same index as pPk.
124207**
124208** The first nKey entries of pIdx are guaranteed to be ordinary columns,
124209** not a rowid or expression.
124210**
124211** This routine differs from hasColumn() in that both the column and the
124212** collating sequence must match for this routine, but for hasColumn() only
124213** the column name must match.
124214*/
124215static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
124216 int i, j;
124217 assert( nKey<=pIdx->nColumn )((void) (0));
124218 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) )((void) (0));
124219 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
124220 assert( pPk->pTable->tabFlags & TF_WithoutRowid )((void) (0));
124221 assert( pPk->pTable==pIdx->pTable )((void) (0));
124222 testcase( pPk==pIdx );
124223 j = pPk->aiColumn[iCol];
124224 assert( j!=XN_ROWID && j!=XN_EXPR )((void) (0));
124225 for(i=0; i<nKey; i++){
124226 assert( pIdx->aiColumn[i]>=0 || j>=0 )((void) (0));
124227 if( pIdx->aiColumn[i]==j
124228 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
124229 ){
124230 return 1;
124231 }
124232 }
124233 return 0;
124234}
124235
124236/* Recompute the colNotIdxed field of the Index.
124237**
124238** colNotIdxed is a bitmask that has a 0 bit representing each indexed
124239** columns that are within the first 63 columns of the table and a 1 for
124240** all other bits (all columns that are not in the index). The
124241** high-order bit of colNotIdxed is always 1. All unindexed columns
124242** of the table have a 1.
124243**
124244** 2019-10-24: For the purpose of this computation, virtual columns are
124245** not considered to be covered by the index, even if they are in the
124246** index, because we do not trust the logic in whereIndexExprTrans() to be
124247** able to find all instances of a reference to the indexed table column
124248** and convert them into references to the index. Hence we always want
124249** the actual table at hand in order to recompute the virtual column, if
124250** necessary.
124251**
124252** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
124253** to determine if the index is covering index.
124254*/
124255static void recomputeColumnsNotIndexed(Index *pIdx){
124256 Bitmask m = 0;
124257 int j;
124258 Table *pTab = pIdx->pTable;
124259 for(j=pIdx->nColumn-1; j>=0; j--){
124260 int x = pIdx->aiColumn[j];
124261 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
124262 testcase( x==BMS-1 );
124263 testcase( x==BMS-2 );
124264 if( x<BMS((int)(sizeof(Bitmask)*8))-1 ) m |= MASKBIT(x)(((Bitmask)1)<<(x));
124265 }
124266 }
124267 pIdx->colNotIdxed = ~m;
124268 assert( (pIdx->colNotIdxed>>63)==1 )((void) (0)); /* See note-20221022-a */
124269}
124270
124271/*
124272** This routine runs at the end of parsing a CREATE TABLE statement that
124273** has a WITHOUT ROWID clause. The job of this routine is to convert both
124274** internal schema data structures and the generated VDBE code so that they
124275** are appropriate for a WITHOUT ROWID table instead of a rowid table.
124276** Changes include:
124277**
124278** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
124279** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
124280** into BTREE_BLOBKEY.
124281** (3) Bypass the creation of the sqlite_schema table entry
124282** for the PRIMARY KEY as the primary key index is now
124283** identified by the sqlite_schema table entry of the table itself.
124284** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
124285** schema to the rootpage from the main table.
124286** (5) Add all table columns to the PRIMARY KEY Index object
124287** so that the PRIMARY KEY is a covering index. The surplus
124288** columns are part of KeyInfo.nAllField and are not used for
124289** sorting or lookup or uniqueness checks.
124290** (6) Replace the rowid tail on all automatically generated UNIQUE
124291** indices with the PRIMARY KEY columns.
124292**
124293** For virtual tables, only (1) is performed.
124294*/
124295static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
124296 Index *pIdx;
124297 Index *pPk;
124298 int nPk;
124299 int nExtra;
124300 int i, j;
124301 sqlite3 *db = pParse->db;
124302 Vdbe *v = pParse->pVdbe;
124303
124304 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
124305 */
124306 if( !db->init.imposterTable ){
124307 for(i=0; i<pTab->nCol; i++){
124308 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001)!=0
124309 && (pTab->aCol[i].notNull==OE_None0)
124310 ){
124311 pTab->aCol[i].notNull = OE_Abort2;
124312 }
124313 }
124314 pTab->tabFlags |= TF_HasNotNull0x00000800;
124315 }
124316
124317 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
124318 ** into BTREE_BLOBKEY.
124319 */
124320 assert( !pParse->bReturning )((void) (0));
124321 if( pParse->u1.addrCrTab ){
124322 assert( v )((void) (0));
124323 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY2);
124324 }
124325
124326 /* Locate the PRIMARY KEY index. Or, if this table was originally
124327 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
124328 */
124329 if( pTab->iPKey>=0 ){
124330 ExprList *pList;
124331 Token ipkToken;
124332 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
124333 pList = sqlite3ExprListAppend(pParse, 0,
124334 sqlite3ExprAlloc(db, TK_ID60, &ipkToken, 0));
124335 if( pList==0 ){
124336 pTab->tabFlags &= ~TF_WithoutRowid0x00000080;
124337 return;
124338 }
124339 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
124340 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
124341 }
124342 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
124343 assert( pParse->pNewTable==pTab )((void) (0));
124344 pTab->iPKey = -1;
124345 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
124346 SQLITE_IDXTYPE_PRIMARYKEY2);
124347 if( pParse->nErr ){
124348 pTab->tabFlags &= ~TF_WithoutRowid0x00000080;
124349 return;
124350 }
124351 assert( db->mallocFailed==0 )((void) (0));
124352 pPk = sqlite3PrimaryKeyIndex(pTab);
124353 assert( pPk->nKeyCol==1 )((void) (0));
124354 }else{
124355 pPk = sqlite3PrimaryKeyIndex(pTab);
124356 assert( pPk!=0 )((void) (0));
124357
124358 /*
124359 ** Remove all redundant columns from the PRIMARY KEY. For example, change
124360 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
124361 ** code assumes the PRIMARY KEY contains no repeated columns.
124362 */
124363 for(i=j=1; i<pPk->nKeyCol; i++){
124364 if( isDupColumn(pPk, j, pPk, i) ){
124365 pPk->nColumn--;
124366 }else{
124367 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
124368 pPk->azColl[j] = pPk->azColl[i];
124369 pPk->aSortOrder[j] = pPk->aSortOrder[i];
124370 pPk->aiColumn[j++] = pPk->aiColumn[i];
124371 }
124372 }
124373 pPk->nKeyCol = j;
124374 }
124375 assert( pPk!=0 )((void) (0));
124376 pPk->isCovering = 1;
124377 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
124378 nPk = pPk->nColumn = pPk->nKeyCol;
124379
124380 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
124381 ** table entry. This is only required if currently generating VDBE
124382 ** code for a CREATE TABLE (not when parsing one as part of reading
124383 ** a database schema). */
124384 if( v && pPk->tnum>0 ){
124385 assert( db->init.busy==0 )((void) (0));
124386 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto9);
124387 }
124388
124389 /* The root page of the PRIMARY KEY is the table root page */
124390 pPk->tnum = pTab->tnum;
124391
124392 /* Update the in-memory representation of all UNIQUE indices by converting
124393 ** the final rowid column into one or more columns of the PRIMARY KEY.
124394 */
124395 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124396 int n;
124397 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ) continue;
124398 for(i=n=0; i<nPk; i++){
124399 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
124400 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
124401 n++;
124402 }
124403 }
124404 if( n==0 ){
124405 /* This index is a superset of the primary key */
124406 pIdx->nColumn = pIdx->nKeyCol;
124407 continue;
124408 }
124409 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
124410 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
124411 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
124412 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
124413 pIdx->aiColumn[j] = pPk->aiColumn[i];
124414 pIdx->azColl[j] = pPk->azColl[i];
124415 if( pPk->aSortOrder[i] ){
124416 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
124417 pIdx->bAscKeyBug = 1;
124418 }
124419 j++;
124420 }
124421 }
124422 assert( pIdx->nColumn>=pIdx->nKeyCol+n )((void) (0));
124423 assert( pIdx->nColumn>=j )((void) (0));
124424 }
124425
124426 /* Add all table columns to the PRIMARY KEY index
124427 */
124428 nExtra = 0;
124429 for(i=0; i<pTab->nCol; i++){
124430 if( !hasColumn(pPk->aiColumn, nPk, i)
124431 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) nExtra++;
124432 }
124433 if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
124434 for(i=0, j=nPk; i<pTab->nCol; i++){
124435 if( !hasColumn(pPk->aiColumn, j, i)
124436 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0
124437 ){
124438 assert( j<pPk->nColumn )((void) (0));
124439 pPk->aiColumn[j] = i;
124440 pPk->azColl[j] = sqlite3StrBINARY;
124441 j++;
124442 }
124443 }
124444 assert( pPk->nColumn==j )((void) (0));
124445 assert( pTab->nNVCol<=j )((void) (0));
124446 recomputeColumnsNotIndexed(pPk);
124447}
124448
124449
124450#ifndef SQLITE_OMIT_VIRTUALTABLE
124451/*
124452** Return true if pTab is a virtual table and zName is a shadow table name
124453** for that virtual table.
124454*/
124455SQLITE_PRIVATEstatic int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){
124456 int nName; /* Length of zName */
124457 Module *pMod; /* Module for the virtual table */
124458
124459 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
124460 nName = sqlite3Strlen30(pTab->zName);
124461 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
124462 if( zName[nName]!='_' ) return 0;
124463 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
124464 if( pMod==0 ) return 0;
124465 if( pMod->pModule->iVersion<3 ) return 0;
124466 if( pMod->pModule->xShadowName==0 ) return 0;
124467 return pMod->pModule->xShadowName(zName+nName+1);
124468}
124469#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
124470
124471#ifndef SQLITE_OMIT_VIRTUALTABLE
124472/*
124473** Table pTab is a virtual table. If it the virtual table implementation
124474** exists and has an xShadowName method, then loop over all other ordinary
124475** tables within the same schema looking for shadow tables of pTab, and mark
124476** any shadow tables seen using the TF_Shadow flag.
124477*/
124478SQLITE_PRIVATEstatic void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){
124479 int nName; /* Length of pTab->zName */
124480 Module *pMod; /* Module for the virtual table */
124481 HashElem *k; /* For looping through the symbol table */
124482
124483 assert( IsVirtual(pTab) )((void) (0));
124484 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
124485 if( pMod==0 ) return;
124486 if( NEVER(pMod->pModule==0)(pMod->pModule==0) ) return;
124487 if( pMod->pModule->iVersion<3 ) return;
124488 if( pMod->pModule->xShadowName==0 ) return;
124489 assert( pTab->zName!=0 )((void) (0));
124490 nName = sqlite3Strlen30(pTab->zName);
124491 for(k=sqliteHashFirst(&pTab->pSchema->tblHash)((&pTab->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
124492 Table *pOther = sqliteHashData(k)((k)->data);
124493 assert( pOther->zName!=0 )((void) (0));
124494 if( !IsOrdinaryTable(pOther)((pOther)->eTabType==0) ) continue;
124495 if( pOther->tabFlags & TF_Shadow0x00001000 ) continue;
124496 if( sqlite3StrNICmpsqlite3_strnicmp(pOther->zName, pTab->zName, nName)==0
124497 && pOther->zName[nName]=='_'
124498 && pMod->pModule->xShadowName(pOther->zName+nName+1)
124499 ){
124500 pOther->tabFlags |= TF_Shadow0x00001000;
124501 }
124502 }
124503}
124504#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
124505
124506#ifndef SQLITE_OMIT_VIRTUALTABLE
124507/*
124508** Return true if zName is a shadow table name in the current database
124509** connection.
124510**
124511** zName is temporarily modified while this routine is running, but is
124512** restored to its original value prior to this routine returning.
124513*/
124514SQLITE_PRIVATEstatic int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
124515 char *zTail; /* Pointer to the last "_" in zName */
124516 Table *pTab; /* Table that zName is a shadow of */
124517 zTail = strrchr(zName, '_');
124518 if( zTail==0 ) return 0;
124519 *zTail = 0;
124520 pTab = sqlite3FindTable(db, zName, 0);
124521 *zTail = '_';
124522 if( pTab==0 ) return 0;
124523 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
124524 return sqlite3IsShadowTableOf(db, pTab, zName);
124525}
124526#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
124527
124528
124529#ifdef SQLITE_DEBUG
124530/*
124531** Mark all nodes of an expression as EP_Immutable, indicating that
124532** they should not be changed. Expressions attached to a table or
124533** index definition are tagged this way to help ensure that we do
124534** not pass them into code generator routines by mistake.
124535*/
124536static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
124537 (void)pWalker;
124538 ExprSetVVAProperty(pExpr, EP_Immutable);
124539 return WRC_Continue0;
124540}
124541static void markExprListImmutable(ExprList *pList){
124542 if( pList ){
124543 Walker w;
124544 memset(&w, 0, sizeof(w));
124545 w.xExprCallback = markImmutableExprStep;
124546 w.xSelectCallback = sqlite3SelectWalkNoop;
124547 w.xSelectCallback2 = 0;
124548 sqlite3WalkExprList(&w, pList);
124549 }
124550}
124551#else
124552#define markExprListImmutable(X) /* no-op */
124553#endif /* SQLITE_DEBUG */
124554
124555
124556/*
124557** This routine is called to report the final ")" that terminates
124558** a CREATE TABLE statement.
124559**
124560** The table structure that other action routines have been building
124561** is added to the internal hash tables, assuming no errors have
124562** occurred.
124563**
124564** An entry for the table is made in the schema table on disk, unless
124565** this is a temporary table or db->init.busy==1. When db->init.busy==1
124566** it means we are reading the sqlite_schema table because we just
124567** connected to the database or because the sqlite_schema table has
124568** recently changed, so the entry for this table already exists in
124569** the sqlite_schema table. We do not want to create it again.
124570**
124571** If the pSelect argument is not NULL, it means that this routine
124572** was called to create a table generated from a
124573** "CREATE TABLE ... AS SELECT ..." statement. The column names of
124574** the new table will match the result set of the SELECT.
124575*/
124576SQLITE_PRIVATEstatic void sqlite3EndTable(
124577 Parse *pParse, /* Parse context */
124578 Token *pCons, /* The ',' token after the last column defn. */
124579 Token *pEnd, /* The ')' before options in the CREATE TABLE */
124580 u32 tabOpts, /* Extra table options. Usually 0. */
124581 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
124582){
124583 Table *p; /* The new table */
124584 sqlite3 *db = pParse->db; /* The database connection */
124585 int iDb; /* Database in which the table lives */
124586 Index *pIdx; /* An implied index of the table */
124587
124588 if( pEnd==0 && pSelect==0 ){
124589 return;
124590 }
124591 p = pParse->pNewTable;
124592 if( p==0 ) return;
124593
124594 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
124595 p->tabFlags |= TF_Shadow0x00001000;
124596 }
124597
124598 /* If the db->init.busy is 1 it means we are reading the SQL off the
124599 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
124600 ** So do not write to the disk again. Extract the root page number
124601 ** for the table from the db->init.newTnum field. (The page number
124602 ** should have been put there by the sqliteOpenCb routine.)
124603 **
124604 ** If the root page number is 1, that means this is the sqlite_schema
124605 ** table itself. So mark it read-only.
124606 */
124607 if( db->init.busy ){
124608 if( pSelect || (!IsOrdinaryTable(p)((p)->eTabType==0) && db->init.newTnum) ){
124609 sqlite3ErrorMsg(pParse, "");
124610 return;
124611 }
124612 p->tnum = db->init.newTnum;
124613 if( p->tnum==1 ) p->tabFlags |= TF_Readonly0x00000001;
124614 }
124615
124616 /* Special processing for tables that include the STRICT keyword:
124617 **
124618 ** * Do not allow custom column datatypes. Every column must have
124619 ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
124620 **
124621 ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
124622 ** then all columns of the PRIMARY KEY must have a NOT NULL
124623 ** constraint.
124624 */
124625 if( tabOpts & TF_Strict0x00010000 ){
124626 int ii;
124627 p->tabFlags |= TF_Strict0x00010000;
124628 for(ii=0; ii<p->nCol; ii++){
124629 Column *pCol = &p->aCol[ii];
124630 if( pCol->eCType==COLTYPE_CUSTOM0 ){
124631 if( pCol->colFlags & COLFLAG_HASTYPE0x0004 ){
124632 sqlite3ErrorMsg(pParse,
124633 "unknown datatype for %s.%s: \"%s\"",
124634 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
124635 );
124636 }else{
124637 sqlite3ErrorMsg(pParse, "missing datatype for %s.%s",
124638 p->zName, pCol->zCnName);
124639 }
124640 return;
124641 }else if( pCol->eCType==COLTYPE_ANY1 ){
124642 pCol->affinity = SQLITE_AFF_BLOB0x41;
124643 }
124644 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0
124645 && p->iPKey!=ii
124646 && pCol->notNull == OE_None0
124647 ){
124648 pCol->notNull = OE_Abort2;
124649 p->tabFlags |= TF_HasNotNull0x00000800;
124650 }
124651 }
124652 }
124653
124654 assert( (p->tabFlags & TF_HasPrimaryKey)==0((void) (0))
124655 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 )((void) (0));
124656 assert( (p->tabFlags & TF_HasPrimaryKey)!=0((void) (0))
124657 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) )((void) (0));
124658
124659 /* Special processing for WITHOUT ROWID Tables */
124660 if( tabOpts & TF_WithoutRowid0x00000080 ){
124661 if( (p->tabFlags & TF_Autoincrement0x00000008) ){
124662 sqlite3ErrorMsg(pParse,
124663 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
124664 return;
124665 }
124666 if( (p->tabFlags & TF_HasPrimaryKey0x00000004)==0 ){
124667 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
124668 return;
124669 }
124670 p->tabFlags |= TF_WithoutRowid0x00000080 | TF_NoVisibleRowid0x00000200;
124671 convertToWithoutRowidTable(pParse, p);
124672 }
124673 iDb = sqlite3SchemaToIndex(db, p->pSchema);
124674
124675#ifndef SQLITE_OMIT_CHECK
124676 /* Resolve names in all CHECK constraint expressions.
124677 */
124678 if( p->pCheck ){
124679 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck0x000004, 0, p->pCheck);
124680 if( pParse->nErr ){
124681 /* If errors are seen, delete the CHECK constraints now, else they might
124682 ** actually be used if PRAGMA writable_schema=ON is set. */
124683 sqlite3ExprListDelete(db, p->pCheck);
124684 p->pCheck = 0;
124685 }else{
124686 markExprListImmutable(p->pCheck);
124687 }
124688 }
124689#endif /* !defined(SQLITE_OMIT_CHECK) */
124690#ifndef SQLITE_OMIT_GENERATED_COLUMNS
124691 if( p->tabFlags & TF_HasGenerated0x00000060 ){
124692 int ii, nNG = 0;
124693 testcase( p->tabFlags & TF_HasVirtual );
124694 testcase( p->tabFlags & TF_HasStored );
124695 for(ii=0; ii<p->nCol; ii++){
124696 u32 colFlags = p->aCol[ii].colFlags;
124697 if( (colFlags & COLFLAG_GENERATED0x0060)!=0 ){
124698 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
124699 testcase( colFlags & COLFLAG_VIRTUAL );
124700 testcase( colFlags & COLFLAG_STORED );
124701 if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol0x000008, pX, 0) ){
124702 /* If there are errors in resolving the expression, change the
124703 ** expression to a NULL. This prevents code generators that operate
124704 ** on the expression from inserting extra parts into the expression
124705 ** tree that have been allocated from lookaside memory, which is
124706 ** illegal in a schema and will lead to errors or heap corruption
124707 ** when the database connection closes. */
124708 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
124709 sqlite3ExprAlloc(db, TK_NULL122, 0, 0));
124710 }
124711 }else{
124712 nNG++;
124713 }
124714 }
124715 if( nNG==0 ){
124716 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
124717 return;
124718 }
124719 }
124720#endif
124721
124722 /* Estimate the average row size for the table and for all implied indices */
124723 estimateTableWidth(p);
124724 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124725 estimateIndexWidth(pIdx);
124726 }
124727
124728 /* If not initializing, then create a record for the new table
124729 ** in the schema table of the database.
124730 **
124731 ** If this is a TEMPORARY table, write the entry into the auxiliary
124732 ** file instead of into the main database file.
124733 */
124734 if( !db->init.busy ){
124735 int n;
124736 Vdbe *v;
124737 char *zType; /* "view" or "table" */
124738 char *zType2; /* "VIEW" or "TABLE" */
124739 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
124740
124741 v = sqlite3GetVdbe(pParse);
124742 if( NEVER(v==0)(v==0) ) return;
124743
124744 sqlite3VdbeAddOp1(v, OP_Close122, 0);
124745
124746 /*
124747 ** Initialize zType for the new view or table.
124748 */
124749 if( IsOrdinaryTable(p)((p)->eTabType==0) ){
124750 /* A regular table */
124751 zType = "table";
124752 zType2 = "TABLE";
124753#ifndef SQLITE_OMIT_VIEW
124754 }else{
124755 /* A view */
124756 zType = "view";
124757 zType2 = "VIEW";
124758#endif
124759 }
124760
124761 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
124762 ** statement to populate the new table. The root-page number for the
124763 ** new table is in register pParse->regRoot.
124764 **
124765 ** Once the SELECT has been coded by sqlite3Select(), it is in a
124766 ** suitable state to query for the column names and types to be used
124767 ** by the new table.
124768 **
124769 ** A shared-cache write-lock is not required to write to the new table,
124770 ** as a schema-lock must have already been obtained to create it. Since
124771 ** a schema-lock excludes all other database users, the write-lock would
124772 ** be redundant.
124773 */
124774 if( pSelect ){
124775 SelectDest dest; /* Where the SELECT should store results */
124776 int regYield; /* Register holding co-routine entry-point */
124777 int addrTop; /* Top of the co-routine */
124778 int regRec; /* A record to be insert into the new table */
124779 int regRowid; /* Rowid of the next row to insert */
124780 int addrInsLoop; /* Top of the loop for inserting rows */
124781 Table *pSelTab; /* A table that describes the SELECT results */
124782 int iCsr; /* Write cursor on the new table */
124783
124784 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
124785 pParse->rc = SQLITE_ERROR1;
124786 pParse->nErr++;
124787 return;
124788 }
124789 iCsr = pParse->nTab++;
124790 regYield = ++pParse->nMem;
124791 regRec = ++pParse->nMem;
124792 regRowid = ++pParse->nMem;
124793 sqlite3MayAbort(pParse);
124794 sqlite3VdbeAddOp3(v, OP_OpenWrite113, iCsr, pParse->regRoot, iDb);
124795 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG0x10);
124796 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
124797 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, addrTop);
124798 if( pParse->nErr ) return;
124799 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB0x41);
124800 if( pSelTab==0 ) return;
124801 assert( p->aCol==0 )((void) (0));
124802 p->nCol = p->nNVCol = pSelTab->nCol;
124803 p->aCol = pSelTab->aCol;
124804 pSelTab->nCol = 0;
124805 pSelTab->aCol = 0;
124806 sqlite3DeleteTable(db, pSelTab);
124807 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
124808 sqlite3Select(pParse, pSelect, &dest);
124809 if( pParse->nErr ) return;
124810 sqlite3VdbeEndCoroutine(v, regYield);
124811 sqlite3VdbeJumpHere(v, addrTop - 1);
124812 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield12, dest.iSDParm);
124813 VdbeCoverage(v);
124814 sqlite3VdbeAddOp3(v, OP_MakeRecord97, dest.iSdst, dest.nSdst, regRec);
124815 sqlite3TableAffinity(v, p, 0);
124816 sqlite3VdbeAddOp2(v, OP_NewRowid127, iCsr, regRowid);
124817 sqlite3VdbeAddOp3(v, OP_Insert128, iCsr, regRec, regRowid);
124818 sqlite3VdbeGoto(v, addrInsLoop);
124819 sqlite3VdbeJumpHere(v, addrInsLoop);
124820 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
124821 }
124822
124823 /* Compute the complete text of the CREATE statement */
124824 if( pSelect ){
124825 zStmt = createTableStmt(db, p);
124826 }else{
124827 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
124828 n = (int)(pEnd2->z - pParse->sNameToken.z);
124829 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
124830 zStmt = sqlite3MPrintf(db,
124831 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
124832 );
124833 }
124834
124835 /* A slot for the record has already been allocated in the
124836 ** schema table. We just need to update that slot with all
124837 ** the information we've collected.
124838 */
124839 sqlite3NestedParse(pParse,
124840 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
124841 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
124842 " WHERE rowid=#%d",
124843 db->aDb[iDb].zDbSName,
124844 zType,
124845 p->zName,
124846 p->zName,
124847 pParse->regRoot,
124848 zStmt,
124849 pParse->regRowid
124850 );
124851 sqlite3DbFree(db, zStmt);
124852 sqlite3ChangeCookie(pParse, iDb);
124853
124854#ifndef SQLITE_OMIT_AUTOINCREMENT
124855 /* Check to see if we need to create an sqlite_sequence table for
124856 ** keeping track of autoincrement keys.
124857 */
124858 if( (p->tabFlags & TF_Autoincrement0x00000008)!=0 && !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
124859 Db *pDb = &db->aDb[iDb];
124860 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
124861 if( pDb->pSchema->pSeqTab==0 ){
124862 sqlite3NestedParse(pParse,
124863 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
124864 pDb->zDbSName
124865 );
124866 }
124867 }
124868#endif
124869
124870 /* Reparse everything to update our internal data structures */
124871 sqlite3VdbeAddParseSchemaOp(v, iDb,
124872 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
124873
124874 /* Test for cycles in generated columns and illegal expressions
124875 ** in CHECK constraints and in DEFAULT clauses. */
124876 if( p->tabFlags & TF_HasGenerated0x00000060 ){
124877 sqlite3VdbeAddOp4(v, OP_SqlExec148, 0x0001, 0, 0,
124878 sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
124879 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC(-6));
124880 }
124881 }
124882
124883 /* Add the table to the in-memory representation of the database.
124884 */
124885 if( db->init.busy ){
124886 Table *pOld;
124887 Schema *pSchema = p->pSchema;
124888 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
124889 assert( HasRowid(p) || p->iPKey<0 )((void) (0));
124890 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
124891 if( pOld ){
124892 assert( p==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
124893 sqlite3OomFault(db);
124894 return;
124895 }
124896 pParse->pNewTable = 0;
124897 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
124898
124899 /* If this is the magic sqlite_sequence table used by autoincrement,
124900 ** then record a pointer to this table in the main database structure
124901 ** so that INSERT can find the table easily. */
124902 assert( !pParse->nested )((void) (0));
124903#ifndef SQLITE_OMIT_AUTOINCREMENT
124904 if( strcmp(p->zName, "sqlite_sequence")==0 ){
124905 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
124906 p->pSchema->pSeqTab = p;
124907 }
124908#endif
124909 }
124910
124911#ifndef SQLITE_OMIT_ALTERTABLE
124912 if( !pSelect && IsOrdinaryTable(p)((p)->eTabType==0) ){
124913 assert( pCons && pEnd )((void) (0));
124914 if( pCons->z==0 ){
124915 pCons = pEnd;
124916 }
124917 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
124918 }
124919#endif
124920}
124921
124922#ifndef SQLITE_OMIT_VIEW
124923/*
124924** The parser calls this routine in order to create a new VIEW
124925*/
124926SQLITE_PRIVATEstatic void sqlite3CreateView(
124927 Parse *pParse, /* The parsing context */
124928 Token *pBegin, /* The CREATE token that begins the statement */
124929 Token *pName1, /* The token that holds the name of the view */
124930 Token *pName2, /* The token that holds the name of the view */
124931 ExprList *pCNames, /* Optional list of view column names */
124932 Select *pSelect, /* A SELECT statement that will become the new view */
124933 int isTemp, /* TRUE for a TEMPORARY view */
124934 int noErr /* Suppress error messages if VIEW already exists */
124935){
124936 Table *p;
124937 int n;
124938 const char *z;
124939 Token sEnd;
124940 DbFixer sFix;
124941 Token *pName = 0;
124942 int iDb;
124943 sqlite3 *db = pParse->db;
124944
124945 if( pParse->nVar>0 ){
124946 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
124947 goto create_view_fail;
124948 }
124949 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
124950 p = pParse->pNewTable;
124951 if( p==0 || pParse->nErr ) goto create_view_fail;
124952
124953 /* Legacy versions of SQLite allowed the use of the magic "rowid" column
124954 ** on a view, even though views do not have rowids. The following flag
124955 ** setting fixes this problem. But the fix can be disabled by compiling
124956 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
124957 ** depend upon the old buggy behavior. The ability can also be toggled
124958 ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */
124959#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
124960 p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */
124961#else
124962 p->tabFlags |= TF_NoVisibleRowid0x00000200; /* Never allow rowid in view */
124963#endif
124964
124965 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
124966 iDb = sqlite3SchemaToIndex(db, p->pSchema);
124967 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
124968 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
124969
124970 /* Make a copy of the entire SELECT statement that defines the view.
124971 ** This will force all the Expr.token.z values to be dynamically
124972 ** allocated rather than point to the input string - which means that
124973 ** they will persist after the current sqlite3_exec() call returns.
124974 */
124975 pSelect->selFlags |= SF_View0x0200000;
124976 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
124977 p->u.view.pSelect = pSelect;
124978 pSelect = 0;
124979 }else{
124980 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
124981 }
124982 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE0x0001);
124983 p->eTabType = TABTYP_VIEW2;
124984 if( db->mallocFailed ) goto create_view_fail;
124985
124986 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
124987 ** the end.
124988 */
124989 sEnd = pParse->sLastToken;
124990 assert( sEnd.z[0]!=0 || sEnd.n==0 )((void) (0));
124991 if( sEnd.z[0]!=';' ){
124992 sEnd.z += sEnd.n;
124993 }
124994 sEnd.n = 0;
124995 n = (int)(sEnd.z - pBegin->z);
124996 assert( n>0 )((void) (0));
124997 z = pBegin->z;
124998 while( sqlite3Isspace(z[n-1])(sqlite3CtypeMap[(unsigned char)(z[n-1])]&0x01) ){ n--; }
124999 sEnd.z = &z[n-1];
125000 sEnd.n = 1;
125001
125002 /* Use sqlite3EndTable() to add the view to the schema table */
125003 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
125004
125005create_view_fail:
125006 sqlite3SelectDelete(db, pSelect);
125007 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
125008 sqlite3RenameExprlistUnmap(pParse, pCNames);
125009 }
125010 sqlite3ExprListDelete(db, pCNames);
125011 return;
125012}
125013#endif /* SQLITE_OMIT_VIEW */
125014
125015#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
125016/*
125017** The Table structure pTable is really a VIEW. Fill in the names of
125018** the columns of the view in the pTable structure. Return non-zero if
125019** there are errors. If an error is seen an error message is left
125020** in pParse->zErrMsg.
125021*/
125022static SQLITE_NOINLINE__attribute__((noinline)) int viewGetColumnNames(Parse *pParse, Table *pTable){
125023 Table *pSelTab; /* A fake table from which we get the result set */
125024 Select *pSel; /* Copy of the SELECT that implements the view */
125025 int nErr = 0; /* Number of errors encountered */
125026 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
125027#ifndef SQLITE_OMIT_VIRTUALTABLE
125028 int rc;
125029#endif
125030#ifndef SQLITE_OMIT_AUTHORIZATION
125031 sqlite3_xauth xAuth; /* Saved xAuth pointer */
125032#endif
125033
125034 assert( pTable )((void) (0));
125035
125036#ifndef SQLITE_OMIT_VIRTUALTABLE
125037 if( IsVirtual(pTable)((pTable)->eTabType==1) ){
125038 db->nSchemaLock++;
125039 rc = sqlite3VtabCallConnect(pParse, pTable);
125040 db->nSchemaLock--;
125041 return rc;
125042 }
125043#endif
125044
125045#ifndef SQLITE_OMIT_VIEW
125046 /* A positive nCol means the columns names for this view are
125047 ** already known. This routine is not called unless either the
125048 ** table is virtual or nCol is zero.
125049 */
125050 assert( pTable->nCol<=0 )((void) (0));
125051
125052 /* A negative nCol is a special marker meaning that we are currently
125053 ** trying to compute the column names. If we enter this routine with
125054 ** a negative nCol, it means two or more views form a loop, like this:
125055 **
125056 ** CREATE VIEW one AS SELECT * FROM two;
125057 ** CREATE VIEW two AS SELECT * FROM one;
125058 **
125059 ** Actually, the error above is now caught prior to reaching this point.
125060 ** But the following test is still important as it does come up
125061 ** in the following:
125062 **
125063 ** CREATE TABLE main.ex1(a);
125064 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
125065 ** SELECT * FROM temp.ex1;
125066 */
125067 if( pTable->nCol<0 ){
125068 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
125069 return 1;
125070 }
125071 assert( pTable->nCol>=0 )((void) (0));
125072
125073 /* If we get this far, it means we need to compute the table names.
125074 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
125075 ** "*" elements in the results set of the view and will assign cursors
125076 ** to the elements of the FROM clause. But we do not want these changes
125077 ** to be permanent. So the computation is done on a copy of the SELECT
125078 ** statement that defines the view.
125079 */
125080 assert( IsView(pTable) )((void) (0));
125081 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
125082 if( pSel ){
125083 u8 eParseMode = pParse->eParseMode;
125084 int nTab = pParse->nTab;
125085 int nSelect = pParse->nSelect;
125086 pParse->eParseMode = PARSE_MODE_NORMAL0;
125087 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
125088 pTable->nCol = -1;
125089 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
125090#ifndef SQLITE_OMIT_AUTHORIZATION
125091 xAuth = db->xAuth;
125092 db->xAuth = 0;
125093 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE0x40);
125094 db->xAuth = xAuth;
125095#else
125096 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE0x40);
125097#endif
125098 pParse->nTab = nTab;
125099 pParse->nSelect = nSelect;
125100 if( pSelTab==0 ){
125101 pTable->nCol = 0;
125102 nErr++;
125103 }else if( pTable->pCheck ){
125104 /* CREATE VIEW name(arglist) AS ...
125105 ** The names of the columns in the table are taken from
125106 ** arglist which is stored in pTable->pCheck. The pCheck field
125107 ** normally holds CHECK constraints on an ordinary table, but for
125108 ** a VIEW it holds the list of column names.
125109 */
125110 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
125111 &pTable->nCol, &pTable->aCol);
125112 if( pParse->nErr==0
125113 && pTable->nCol==pSel->pEList->nExpr
125114 ){
125115 assert( db->mallocFailed==0 )((void) (0));
125116 sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE0x40);
125117 }
125118 }else{
125119 /* CREATE VIEW name AS... without an argument list. Construct
125120 ** the column names from the SELECT statement that defines the view.
125121 */
125122 assert( pTable->aCol==0 )((void) (0));
125123 pTable->nCol = pSelTab->nCol;
125124 pTable->aCol = pSelTab->aCol;
125125 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT0x0062);
125126 pSelTab->nCol = 0;
125127 pSelTab->aCol = 0;
125128 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) )((void) (0));
125129 }
125130 pTable->nNVCol = pTable->nCol;
125131 sqlite3DeleteTable(db, pSelTab);
125132 sqlite3SelectDelete(db, pSel);
125133 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
125134 pParse->eParseMode = eParseMode;
125135 } else {
125136 nErr++;
125137 }
125138 pTable->pSchema->schemaFlags |= DB_UnresetViews0x0002;
125139 if( db->mallocFailed ){
125140 sqlite3DeleteColumnNames(db, pTable);
125141 }
125142#endif /* SQLITE_OMIT_VIEW */
125143 return nErr + pParse->nErr;
125144}
125145SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
125146 assert( pTable!=0 )((void) (0));
125147 if( !IsVirtual(pTable)((pTable)->eTabType==1) && pTable->nCol>0 ) return 0;
125148 return viewGetColumnNames(pParse, pTable);
125149}
125150#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
125151
125152#ifndef SQLITE_OMIT_VIEW
125153/*
125154** Clear the column names from every VIEW in database idx.
125155*/
125156static void sqliteViewResetAll(sqlite3 *db, int idx){
125157 HashElem *i;
125158 assert( sqlite3SchemaMutexHeld(db, idx, 0) )((void) (0));
125159 if( !DbHasProperty(db, idx, DB_UnresetViews)(((db)->aDb[idx].pSchema->schemaFlags&(0x0002))==(0x0002
))
) return;
125160 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash)((&db->aDb[idx].pSchema->tblHash)->first); i;i=sqliteHashNext(i)((i)->next)){
125161 Table *pTab = sqliteHashData(i)((i)->data);
125162 if( IsView(pTab)((pTab)->eTabType==2) ){
125163 sqlite3DeleteColumnNames(db, pTab);
125164 }
125165 }
125166 DbClearProperty(db, idx, DB_UnresetViews)(db)->aDb[idx].pSchema->schemaFlags&=~(0x0002);
125167}
125168#else
125169# define sqliteViewResetAll(A,B)
125170#endif /* SQLITE_OMIT_VIEW */
125171
125172/*
125173** This function is called by the VDBE to adjust the internal schema
125174** used by SQLite when the btree layer moves a table root page. The
125175** root-page of a table or index in database iDb has changed from iFrom
125176** to iTo.
125177**
125178** Ticket #1728: The symbol table might still contain information
125179** on tables and/or indices that are the process of being deleted.
125180** If you are unlucky, one of those deleted indices or tables might
125181** have the same rootpage number as the real table or index that is
125182** being moved. So we cannot stop searching after the first match
125183** because the first match might be for one of the deleted indices
125184** or tables and not the table/index that is actually being moved.
125185** We must continue looping until all tables and indices with
125186** rootpage==iFrom have been converted to have a rootpage of iTo
125187** in order to be certain that we got the right one.
125188*/
125189#ifndef SQLITE_OMIT_AUTOVACUUM
125190SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
125191 HashElem *pElem;
125192 Hash *pHash;
125193 Db *pDb;
125194
125195 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
125196 pDb = &db->aDb[iDb];
125197 pHash = &pDb->pSchema->tblHash;
125198 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
125199 Table *pTab = sqliteHashData(pElem)((pElem)->data);
125200 if( pTab->tnum==iFrom ){
125201 pTab->tnum = iTo;
125202 }
125203 }
125204 pHash = &pDb->pSchema->idxHash;
125205 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
125206 Index *pIdx = sqliteHashData(pElem)((pElem)->data);
125207 if( pIdx->tnum==iFrom ){
125208 pIdx->tnum = iTo;
125209 }
125210 }
125211}
125212#endif
125213
125214/*
125215** Write code to erase the table with root-page iTable from database iDb.
125216** Also write code to modify the sqlite_schema table and internal schema
125217** if a root-page of another table is moved by the btree-layer whilst
125218** erasing iTable (this can happen with an auto-vacuum database).
125219*/
125220static void destroyRootPage(Parse *pParse, int iTable, int iDb){
125221 Vdbe *v = sqlite3GetVdbe(pParse);
125222 int r1 = sqlite3GetTempReg(pParse);
125223 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
125224 sqlite3VdbeAddOp3(v, OP_Destroy144, iTable, r1, iDb);
125225 sqlite3MayAbort(pParse);
125226#ifndef SQLITE_OMIT_AUTOVACUUM
125227 /* OP_Destroy stores an in integer r1. If this integer
125228 ** is non-zero, then it is the root page number of a table moved to
125229 ** location iTable. The following code modifies the sqlite_schema table to
125230 ** reflect this.
125231 **
125232 ** The "#NNN" in the SQL is a special constant that means whatever value
125233 ** is in register NNN. See grammar rules associated with the TK_REGISTER
125234 ** token for additional information.
125235 */
125236 sqlite3NestedParse(pParse,
125237 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
125238 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
125239 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
125240#endif
125241 sqlite3ReleaseTempReg(pParse, r1);
125242}
125243
125244/*
125245** Write VDBE code to erase table pTab and all associated indices on disk.
125246** Code to update the sqlite_schema tables and internal schema definitions
125247** in case a root-page belonging to another table is moved by the btree layer
125248** is also added (this can happen with an auto-vacuum database).
125249*/
125250static void destroyTable(Parse *pParse, Table *pTab){
125251 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
125252 ** is not defined), then it is important to call OP_Destroy on the
125253 ** table and index root-pages in order, starting with the numerically
125254 ** largest root-page number. This guarantees that none of the root-pages
125255 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
125256 ** following were coded:
125257 **
125258 ** OP_Destroy 4 0
125259 ** ...
125260 ** OP_Destroy 5 0
125261 **
125262 ** and root page 5 happened to be the largest root-page number in the
125263 ** database, then root page 5 would be moved to page 4 by the
125264 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
125265 ** a free-list page.
125266 */
125267 Pgno iTab = pTab->tnum;
125268 Pgno iDestroyed = 0;
125269
125270 while( 1 ){
125271 Index *pIdx;
125272 Pgno iLargest = 0;
125273
125274 if( iDestroyed==0 || iTab<iDestroyed ){
125275 iLargest = iTab;
125276 }
125277 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
125278 Pgno iIdx = pIdx->tnum;
125279 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
125280 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
125281 iLargest = iIdx;
125282 }
125283 }
125284 if( iLargest==0 ){
125285 return;
125286 }else{
125287 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
125288 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
125289 destroyRootPage(pParse, iLargest, iDb);
125290 iDestroyed = iLargest;
125291 }
125292 }
125293}
125294
125295/*
125296** Remove entries from the sqlite_statN tables (for N in (1,2,3))
125297** after a DROP INDEX or DROP TABLE command.
125298*/
125299static void sqlite3ClearStatTables(
125300 Parse *pParse, /* The parsing context */
125301 int iDb, /* The database number */
125302 const char *zType, /* "idx" or "tbl" */
125303 const char *zName /* Name of index or table */
125304){
125305 int i;
125306 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
125307 for(i=1; i<=4; i++){
125308 char zTab[24];
125309 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
125310 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
125311 sqlite3NestedParse(pParse,
125312 "DELETE FROM %Q.%s WHERE %s=%Q",
125313 zDbName, zTab, zType, zName
125314 );
125315 }
125316 }
125317}
125318
125319/*
125320** Generate code to drop a table.
125321*/
125322SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
125323 Vdbe *v;
125324 sqlite3 *db = pParse->db;
125325 Trigger *pTrigger;
125326 Db *pDb = &db->aDb[iDb];
125327
125328 v = sqlite3GetVdbe(pParse);
125329 assert( v!=0 )((void) (0));
125330 sqlite3BeginWriteOperation(pParse, 1, iDb);
125331
125332#ifndef SQLITE_OMIT_VIRTUALTABLE
125333 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
125334 sqlite3VdbeAddOp0(v, OP_VBegin170);
125335 }
125336#endif
125337
125338 /* Drop all triggers associated with the table being dropped. Code
125339 ** is generated to remove entries from sqlite_schema and/or
125340 ** sqlite_temp_schema if required.
125341 */
125342 pTrigger = sqlite3TriggerList(pParse, pTab);
125343 while( pTrigger ){
125344 assert( pTrigger->pSchema==pTab->pSchema ||((void) (0))
125345 pTrigger->pSchema==db->aDb[1].pSchema )((void) (0));
125346 sqlite3DropTriggerPtr(pParse, pTrigger);
125347 pTrigger = pTrigger->pNext;
125348 }
125349
125350#ifndef SQLITE_OMIT_AUTOINCREMENT
125351 /* Remove any entries of the sqlite_sequence table associated with
125352 ** the table being dropped. This is done before the table is dropped
125353 ** at the btree level, in case the sqlite_sequence table needs to
125354 ** move as a result of the drop (can happen in auto-vacuum mode).
125355 */
125356 if( pTab->tabFlags & TF_Autoincrement0x00000008 ){
125357 sqlite3NestedParse(pParse,
125358 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
125359 pDb->zDbSName, pTab->zName
125360 );
125361 }
125362#endif
125363
125364 /* Drop all entries in the schema table that refer to the
125365 ** table. The program name loops through the schema table and deletes
125366 ** every row that refers to a table of the same name as the one being
125367 ** dropped. Triggers are handled separately because a trigger can be
125368 ** created in the temp database that refers to a table in another
125369 ** database.
125370 */
125371 sqlite3NestedParse(pParse,
125372 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
125373 " WHERE tbl_name=%Q and type!='trigger'",
125374 pDb->zDbSName, pTab->zName);
125375 if( !isView && !IsVirtual(pTab)((pTab)->eTabType==1) ){
125376 destroyTable(pParse, pTab);
125377 }
125378
125379 /* Remove the table entry from SQLite's internal schema and modify
125380 ** the schema cookie.
125381 */
125382 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
125383 sqlite3VdbeAddOp4(v, OP_VDestroy172, iDb, 0, 0, pTab->zName, 0);
125384 sqlite3MayAbort(pParse);
125385 }
125386 sqlite3VdbeAddOp4(v, OP_DropTable151, iDb, 0, 0, pTab->zName, 0);
125387 sqlite3ChangeCookie(pParse, iDb);
125388 sqliteViewResetAll(db, iDb);
125389}
125390
125391/*
125392** Return TRUE if shadow tables should be read-only in the current
125393** context.
125394*/
125395SQLITE_PRIVATEstatic int sqlite3ReadOnlyShadowTables(sqlite3 *db){
125396#ifndef SQLITE_OMIT_VIRTUALTABLE
125397 if( (db->flags & SQLITE_Defensive0x10000000)!=0
125398 && db->pVtabCtx==0
125399 && db->nVdbeExec==0
125400 && !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
125401 ){
125402 return 1;
125403 }
125404#endif
125405 return 0;
125406}
125407
125408/*
125409** Return true if it is not allowed to drop the given table
125410*/
125411static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
125412 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
125413 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName+7, "stat", 4)==0 ) return 0;
125414 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
125415 return 1;
125416 }
125417 if( (pTab->tabFlags & TF_Shadow0x00001000)!=0 && sqlite3ReadOnlyShadowTables(db) ){
125418 return 1;
125419 }
125420 if( pTab->tabFlags & TF_Eponymous0x00008000 ){
125421 return 1;
125422 }
125423 return 0;
125424}
125425
125426/*
125427** This routine is called to do the work of a DROP TABLE statement.
125428** pName is the name of the table to be dropped.
125429*/
125430SQLITE_PRIVATEstatic void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
125431 Table *pTab;
125432 Vdbe *v;
125433 sqlite3 *db = pParse->db;
125434 int iDb;
125435
125436 if( db->mallocFailed ){
125437 goto exit_drop_table;
125438 }
125439 assert( pParse->nErr==0 )((void) (0));
125440 assert( pName->nSrc==1 )((void) (0));
125441 assert( pName->a[0].fg.fixedSchema==0 )((void) (0));
125442 assert( pName->a[0].fg.isSubquery==0 )((void) (0));
125443 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
125444 if( noErr ) db->suppressErr++;
125445 assert( isView==0 || isView==LOCATE_VIEW )((void) (0));
125446 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
125447 if( noErr ) db->suppressErr--;
125448
125449 if( pTab==0 ){
125450 if( noErr ){
125451 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
125452 sqlite3ForceNotReadOnly(pParse);
125453 }
125454 goto exit_drop_table;
125455 }
125456 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125457 assert( iDb>=0 && iDb<db->nDb )((void) (0));
125458
125459 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
125460 ** it is initialized.
125461 */
125462 if( IsVirtual(pTab)((pTab)->eTabType==1) && sqlite3ViewGetColumnNames(pParse, pTab) ){
125463 goto exit_drop_table;
125464 }
125465#ifndef SQLITE_OMIT_AUTHORIZATION
125466 {
125467 int code;
125468 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
125469 const char *zDb = db->aDb[iDb].zDbSName;
125470 const char *zArg2 = 0;
125471 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb)){
125472 goto exit_drop_table;
125473 }
125474 if( isView ){
125475 if( !OMIT_TEMPDB0 && iDb==1 ){
125476 code = SQLITE_DROP_TEMP_VIEW15;
125477 }else{
125478 code = SQLITE_DROP_VIEW17;
125479 }
125480#ifndef SQLITE_OMIT_VIRTUALTABLE
125481 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
125482 code = SQLITE_DROP_VTABLE30;
125483 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
125484#endif
125485 }else{
125486 if( !OMIT_TEMPDB0 && iDb==1 ){
125487 code = SQLITE_DROP_TEMP_TABLE13;
125488 }else{
125489 code = SQLITE_DROP_TABLE11;
125490 }
125491 }
125492 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
125493 goto exit_drop_table;
125494 }
125495 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0, zDb) ){
125496 goto exit_drop_table;
125497 }
125498 }
125499#endif
125500 if( tableMayNotBeDropped(db, pTab) ){
125501 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
125502 goto exit_drop_table;
125503 }
125504
125505#ifndef SQLITE_OMIT_VIEW
125506 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
125507 ** on a table.
125508 */
125509 if( isView && !IsView(pTab)((pTab)->eTabType==2) ){
125510 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
125511 goto exit_drop_table;
125512 }
125513 if( !isView && IsView(pTab)((pTab)->eTabType==2) ){
125514 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
125515 goto exit_drop_table;
125516 }
125517#endif
125518
125519 /* Generate code to remove the table from the schema table
125520 ** on disk.
125521 */
125522 v = sqlite3GetVdbe(pParse);
125523 if( v ){
125524 sqlite3BeginWriteOperation(pParse, 1, iDb);
125525 if( !isView ){
125526 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
125527 sqlite3FkDropTable(pParse, pName, pTab);
125528 }
125529 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
125530 }
125531
125532exit_drop_table:
125533 sqlite3SrcListDelete(db, pName);
125534}
125535
125536/*
125537** This routine is called to create a new foreign key on the table
125538** currently under construction. pFromCol determines which columns
125539** in the current table point to the foreign key. If pFromCol==0 then
125540** connect the key to the last column inserted. pTo is the name of
125541** the table referred to (a.k.a the "parent" table). pToCol is a list
125542** of tables in the parent pTo table. flags contains all
125543** information about the conflict resolution algorithms specified
125544** in the ON DELETE, ON UPDATE and ON INSERT clauses.
125545**
125546** An FKey structure is created and added to the table currently
125547** under construction in the pParse->pNewTable field.
125548**
125549** The foreign key is set for IMMEDIATE processing. A subsequent call
125550** to sqlite3DeferForeignKey() might change this to DEFERRED.
125551*/
125552SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(
125553 Parse *pParse, /* Parsing context */
125554 ExprList *pFromCol, /* Columns in this table that point to other table */
125555 Token *pTo, /* Name of the other table */
125556 ExprList *pToCol, /* Columns in the other table */
125557 int flags /* Conflict resolution algorithms. */
125558){
125559 sqlite3 *db = pParse->db;
125560#ifndef SQLITE_OMIT_FOREIGN_KEY
125561 FKey *pFKey = 0;
125562 FKey *pNextTo;
125563 Table *p = pParse->pNewTable;
125564 i64 nByte;
125565 int i;
125566 int nCol;
125567 char *z;
125568
125569 assert( pTo!=0 )((void) (0));
125570 if( p==0 || IN_DECLARE_VTAB(pParse->eParseMode==1) ) goto fk_end;
125571 if( pFromCol==0 ){
125572 int iCol = p->nCol-1;
125573 if( NEVER(iCol<0)(iCol<0) ) goto fk_end;
125574 if( pToCol && pToCol->nExpr!=1 ){
125575 sqlite3ErrorMsg(pParse, "foreign key on %s"
125576 " should reference only one column of table %T",
125577 p->aCol[iCol].zCnName, pTo);
125578 goto fk_end;
125579 }
125580 nCol = 1;
125581 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
125582 sqlite3ErrorMsg(pParse,
125583 "number of columns in foreign key does not match the number of "
125584 "columns in the referenced table");
125585 goto fk_end;
125586 }else{
125587 nCol = pFromCol->nExpr;
125588 }
125589 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
125590 if( pToCol ){
125591 for(i=0; i<pToCol->nExpr; i++){
125592 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
125593 }
125594 }
125595 pFKey = sqlite3DbMallocZero(db, nByte );
125596 if( pFKey==0 ){
125597 goto fk_end;
125598 }
125599 pFKey->pFrom = p;
125600 assert( IsOrdinaryTable(p) )((void) (0));
125601 pFKey->pNextFrom = p->u.tab.pFKey;
125602 z = (char*)&pFKey->aCol[nCol];
125603 pFKey->zTo = z;
125604 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
125605 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
125606 }
125607 memcpy(z, pTo->z, pTo->n);
125608 z[pTo->n] = 0;
125609 sqlite3Dequote(z);
125610 z += pTo->n+1;
125611 pFKey->nCol = nCol;
125612 if( pFromCol==0 ){
125613 pFKey->aCol[0].iFrom = p->nCol-1;
125614 }else{
125615 for(i=0; i<nCol; i++){
125616 int j;
125617 for(j=0; j<p->nCol; j++){
125618 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
125619 pFKey->aCol[i].iFrom = j;
125620 break;
125621 }
125622 }
125623 if( j>=p->nCol ){
125624 sqlite3ErrorMsg(pParse,
125625 "unknown column \"%s\" in foreign key definition",
125626 pFromCol->a[i].zEName);
125627 goto fk_end;
125628 }
125629 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
125630 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
125631 }
125632 }
125633 }
125634 if( pToCol ){
125635 for(i=0; i<nCol; i++){
125636 int n = sqlite3Strlen30(pToCol->a[i].zEName);
125637 pFKey->aCol[i].zCol = z;
125638 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
125639 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
125640 }
125641 memcpy(z, pToCol->a[i].zEName, n);
125642 z[n] = 0;
125643 z += n+1;
125644 }
125645 }
125646 pFKey->isDeferred = 0;
125647 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
125648 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
125649
125650 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
125651 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
125652 pFKey->zTo, (void *)pFKey
125653 );
125654 if( pNextTo==pFKey ){
125655 sqlite3OomFault(db);
125656 goto fk_end;
125657 }
125658 if( pNextTo ){
125659 assert( pNextTo->pPrevTo==0 )((void) (0));
125660 pFKey->pNextTo = pNextTo;
125661 pNextTo->pPrevTo = pFKey;
125662 }
125663
125664 /* Link the foreign key to the table as the last step.
125665 */
125666 assert( IsOrdinaryTable(p) )((void) (0));
125667 p->u.tab.pFKey = pFKey;
125668 pFKey = 0;
125669
125670fk_end:
125671 sqlite3DbFree(db, pFKey);
125672#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
125673 sqlite3ExprListDelete(db, pFromCol);
125674 sqlite3ExprListDelete(db, pToCol);
125675}
125676
125677/*
125678** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
125679** clause is seen as part of a foreign key definition. The isDeferred
125680** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
125681** The behavior of the most recently created foreign key is adjusted
125682** accordingly.
125683*/
125684SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
125685#ifndef SQLITE_OMIT_FOREIGN_KEY
125686 Table *pTab;
125687 FKey *pFKey;
125688 if( (pTab = pParse->pNewTable)==0 ) return;
125689 if( NEVER(!IsOrdinaryTable(pTab))(!((pTab)->eTabType==0)) ) return;
125690 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
125691 assert( isDeferred==0 || isDeferred==1 )((void) (0)); /* EV: R-30323-21917 */
125692 pFKey->isDeferred = (u8)isDeferred;
125693#endif
125694}
125695
125696/*
125697** Generate code that will erase and refill index *pIdx. This is
125698** used to initialize a newly created index or to recompute the
125699** content of an index in response to a REINDEX command.
125700**
125701** if memRootPage is not negative, it means that the index is newly
125702** created. The register specified by memRootPage contains the
125703** root page number of the index. If memRootPage is negative, then
125704** the index already exists and must be cleared before being refilled and
125705** the root page number of the index is taken from pIndex->tnum.
125706*/
125707static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
125708 Table *pTab = pIndex->pTable; /* The table that is indexed */
125709 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
125710 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
125711 int iSorter; /* Cursor opened by OpenSorter (if in use) */
125712 int addr1; /* Address of top of loop */
125713 int addr2; /* Address to jump to for next iteration */
125714 Pgno tnum; /* Root page of index */
125715 int iPartIdxLabel; /* Jump to this label to skip a row */
125716 Vdbe *v; /* Generate code into this virtual machine */
125717 KeyInfo *pKey; /* KeyInfo for index */
125718 int regRecord; /* Register holding assembled index record */
125719 sqlite3 *db = pParse->db; /* The database connection */
125720 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
125721
125722#ifndef SQLITE_OMIT_AUTHORIZATION
125723 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX27, pIndex->zName, 0,
125724 db->aDb[iDb].zDbSName ) ){
125725 return;
125726 }
125727#endif
125728
125729 /* Require a write-lock on the table to perform this operation */
125730 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
125731
125732 v = sqlite3GetVdbe(pParse);
125733 if( v==0 ) return;
125734 if( memRootPage>=0 ){
125735 tnum = (Pgno)memRootPage;
125736 }else{
125737 tnum = pIndex->tnum;
125738 }
125739 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
125740 assert( pKey!=0 || pParse->nErr )((void) (0));
125741
125742 /* Open the sorter cursor if we are to use one. */
125743 iSorter = pParse->nTab++;
125744 sqlite3VdbeAddOp4(v, OP_SorterOpen119, iSorter, 0, pIndex->nKeyCol, (char*)
125745 sqlite3KeyInfoRef(pKey), P4_KEYINFO(-8));
125746
125747 /* Open the table. Loop through all rows of the table, inserting index
125748 ** records into the sorter. */
125749 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead102);
125750 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iTab, 0); VdbeCoverage(v);
125751 regRecord = sqlite3GetTempReg(pParse);
125752 sqlite3MultiWrite(pParse);
125753
125754 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
125755 sqlite3VdbeAddOp2(v, OP_SorterInsert139, iSorter, regRecord);
125756 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
125757 sqlite3VdbeAddOp2(v, OP_Next39, iTab, addr1+1); VdbeCoverage(v);
125758 sqlite3VdbeJumpHere(v, addr1);
125759 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear145, tnum, iDb);
125760 sqlite3VdbeAddOp4(v, OP_OpenWrite113, iIdx, (int)tnum, iDb,
125761 (char *)pKey, P4_KEYINFO(-8));
125762 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01|((memRootPage>=0)?OPFLAG_P2ISREG0x10:0));
125763
125764 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort34, iSorter, 0); VdbeCoverage(v);
125765 if( IsUniqueIndex(pIndex)((pIndex)->onError!=0) ){
125766 int j2 = sqlite3VdbeGoto(v, 1);
125767 addr2 = sqlite3VdbeCurrentAddr(v);
125768 sqlite3VdbeVerifyAbortable(v, OE_Abort);
125769 sqlite3VdbeAddOp4Int(v, OP_SorterCompare132, iSorter, j2, regRecord,
125770 pIndex->nKeyCol); VdbeCoverage(v);
125771 sqlite3UniqueConstraint(pParse, OE_Abort2, pIndex);
125772 sqlite3VdbeJumpHere(v, j2);
125773 }else{
125774 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
125775 ** abort. The exception is if one of the indexed expressions contains a
125776 ** user function that throws an exception when it is evaluated. But the
125777 ** overhead of adding a statement journal to a CREATE INDEX statement is
125778 ** very small (since most of the pages written do not contain content that
125779 ** needs to be restored if the statement aborts), so we call
125780 ** sqlite3MayAbort() for all CREATE INDEX statements. */
125781 sqlite3MayAbort(pParse);
125782 addr2 = sqlite3VdbeCurrentAddr(v);
125783 }
125784 sqlite3VdbeAddOp3(v, OP_SorterData133, iSorter, regRecord, iIdx);
125785 if( !pIndex->bAscKeyBug ){
125786 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
125787 ** faster by avoiding unnecessary seeks. But the optimization does
125788 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
125789 ** with DESC primary keys, since those indexes have there keys in
125790 ** a different order from the main table.
125791 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
125792 */
125793 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iIdx);
125794 }
125795 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iIdx, regRecord);
125796 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
125797 sqlite3ReleaseTempReg(pParse, regRecord);
125798 sqlite3VdbeAddOp2(v, OP_SorterNext37, iSorter, addr2); VdbeCoverage(v);
125799 sqlite3VdbeJumpHere(v, addr1);
125800
125801 sqlite3VdbeAddOp1(v, OP_Close122, iTab);
125802 sqlite3VdbeAddOp1(v, OP_Close122, iIdx);
125803 sqlite3VdbeAddOp1(v, OP_Close122, iSorter);
125804}
125805
125806/*
125807** Allocate heap space to hold an Index object with nCol columns.
125808**
125809** Increase the allocation size to provide an extra nExtra bytes
125810** of 8-byte aligned space after the Index object and return a
125811** pointer to this extra space in *ppExtra.
125812*/
125813SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(
125814 sqlite3 *db, /* Database connection */
125815 i16 nCol, /* Total number of columns in the index */
125816 int nExtra, /* Number of bytes of extra space to alloc */
125817 char **ppExtra /* Pointer to the "extra" space */
125818){
125819 Index *p; /* Allocated index object */
125820 int nByte; /* Bytes of space for Index object + arrays */
125821
125822 nByte = ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7) + /* Index structure */
125823 ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7) + /* Index.azColl */
125824 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
125825 sizeof(i16)*nCol + /* Index.aiColumn */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
125826 sizeof(u8)*nCol)(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
; /* Index.aSortOrder */
125827 p = sqlite3DbMallocZero(db, nByte + nExtra);
125828 if( p ){
125829 char *pExtra = ((char*)p)+ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7);
125830 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7);
125831 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
125832 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
125833 p->aSortOrder = (u8*)pExtra;
125834 p->nColumn = nCol;
125835 p->nKeyCol = nCol - 1;
125836 *ppExtra = ((char*)p) + nByte;
125837 }
125838 return p;
125839}
125840
125841/*
125842** If expression list pList contains an expression that was parsed with
125843** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
125844** pParse and return non-zero. Otherwise, return zero.
125845*/
125846SQLITE_PRIVATEstatic int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
125847 if( pList ){
125848 int i;
125849 for(i=0; i<pList->nExpr; i++){
125850 if( pList->a[i].fg.bNulls ){
125851 u8 sf = pList->a[i].fg.sortFlags;
125852 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s",
125853 (sf==0 || sf==3) ? "FIRST" : "LAST"
125854 );
125855 return 1;
125856 }
125857 }
125858 }
125859 return 0;
125860}
125861
125862/*
125863** Create a new index for an SQL table. pName1.pName2 is the name of the index
125864** and pTblList is the name of the table that is to be indexed. Both will
125865** be NULL for a primary key or an index that is created to satisfy a
125866** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
125867** as the table to be indexed. pParse->pNewTable is a table that is
125868** currently being constructed by a CREATE TABLE statement.
125869**
125870** pList is a list of columns to be indexed. pList will be NULL if this
125871** is a primary key or unique-constraint on the most recent column added
125872** to the table currently under construction.
125873*/
125874SQLITE_PRIVATEstatic void sqlite3CreateIndex(
125875 Parse *pParse, /* All information about this parse */
125876 Token *pName1, /* First part of index name. May be NULL */
125877 Token *pName2, /* Second part of index name. May be NULL */
125878 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
125879 ExprList *pList, /* A list of columns to be indexed */
125880 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
125881 Token *pStart, /* The CREATE token that begins this statement */
125882 Expr *pPIWhere, /* WHERE clause for partial indices */
125883 int sortOrder, /* Sort order of primary key when pList==NULL */
125884 int ifNotExist, /* Omit error if index already exists */
125885 u8 idxType /* The index type */
125886){
125887 Table *pTab = 0; /* Table to be indexed */
125888 Index *pIndex = 0; /* The index to be created */
125889 char *zName = 0; /* Name of the index */
125890 int nName; /* Number of characters in zName */
125891 int i, j;
125892 DbFixer sFix; /* For assigning database names to pTable */
125893 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
125894 sqlite3 *db = pParse->db;
125895 Db *pDb; /* The specific table containing the indexed database */
125896 int iDb; /* Index of the database that is being written */
125897 Token *pName = 0; /* Unqualified name of the index to create */
125898 struct ExprList_item *pListItem; /* For looping over pList */
125899 int nExtra = 0; /* Space allocated for zExtra[] */
125900 int nExtraCol; /* Number of extra columns needed */
125901 char *zExtra = 0; /* Extra space after the Index object */
125902 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
125903
125904 assert( db->pParse==pParse )((void) (0));
125905 if( pParse->nErr ){
125906 goto exit_create_index;
125907 }
125908 assert( db->mallocFailed==0 )((void) (0));
125909 if( IN_DECLARE_VTAB(pParse->eParseMode==1) && idxType!=SQLITE_IDXTYPE_PRIMARYKEY2 ){
125910 goto exit_create_index;
125911 }
125912 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
125913 goto exit_create_index;
125914 }
125915 if( sqlite3HasExplicitNulls(pParse, pList) ){
125916 goto exit_create_index;
125917 }
125918
125919 /*
125920 ** Find the table that is to be indexed. Return early if not found.
125921 */
125922 if( pTblName!=0 ){
125923
125924 /* Use the two-part index name to determine the database
125925 ** to search for the table. 'Fix' the table name to this db
125926 ** before looking up the table.
125927 */
125928 assert( pName1 && pName2 )((void) (0));
125929 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
125930 if( iDb<0 ) goto exit_create_index;
125931 assert( pName && pName->z )((void) (0));
125932
125933#ifndef SQLITE_OMIT_TEMPDB
125934 /* If the index name was unqualified, check if the table
125935 ** is a temp table. If so, set the database to 1. Do not do this
125936 ** if initializing a database schema.
125937 */
125938 if( !db->init.busy ){
125939 pTab = sqlite3SrcListLookup(pParse, pTblName);
125940 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
125941 iDb = 1;
125942 }
125943 }
125944#endif
125945
125946 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
125947 if( sqlite3FixSrcList(&sFix, pTblName) ){
125948 /* Because the parser constructs pTblName from a single identifier,
125949 ** sqlite3FixSrcList can never fail. */
125950 assert(0)((void) (0));
125951 }
125952 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
125953 assert( db->mallocFailed==0 || pTab==0 )((void) (0));
125954 if( pTab==0 ) goto exit_create_index;
125955 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
125956 sqlite3ErrorMsg(pParse,
125957 "cannot create a TEMP index on non-TEMP table \"%s\"",
125958 pTab->zName);
125959 goto exit_create_index;
125960 }
125961 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pPk = sqlite3PrimaryKeyIndex(pTab);
125962 }else{
125963 assert( pName==0 )((void) (0));
125964 assert( pStart==0 )((void) (0));
125965 pTab = pParse->pNewTable;
125966 if( !pTab ) goto exit_create_index;
125967 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125968 }
125969 pDb = &db->aDb[iDb];
125970
125971 assert( pTab!=0 )((void) (0));
125972 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0
125973 && db->init.busy==0
125974 && pTblName!=0
125975#if SQLITE_USER_AUTHENTICATION
125976 && sqlite3UserAuthTable(pTab->zName)==0
125977#endif
125978 ){
125979 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
125980 goto exit_create_index;
125981 }
125982#ifndef SQLITE_OMIT_VIEW
125983 if( IsView(pTab)((pTab)->eTabType==2) ){
125984 sqlite3ErrorMsg(pParse, "views may not be indexed");
125985 goto exit_create_index;
125986 }
125987#endif
125988#ifndef SQLITE_OMIT_VIRTUALTABLE
125989 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
125990 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
125991 goto exit_create_index;
125992 }
125993#endif
125994
125995 /*
125996 ** Find the name of the index. Make sure there is not already another
125997 ** index or table with the same name.
125998 **
125999 ** Exception: If we are reading the names of permanent indices from the
126000 ** sqlite_schema table (because some other process changed the schema) and
126001 ** one of the index names collides with the name of a temporary table or
126002 ** index, then we will continue to process this index.
126003 **
126004 ** If pName==0 it means that we are
126005 ** dealing with a primary key or UNIQUE constraint. We have to invent our
126006 ** own name.
126007 */
126008 if( pName ){
126009 zName = sqlite3NameFromToken(db, pName);
126010 if( zName==0 ) goto exit_create_index;
126011 assert( pName->z!=0 )((void) (0));
126012 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
126013 goto exit_create_index;
126014 }
126015 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126016 if( !db->init.busy ){
126017 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
126018 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
126019 goto exit_create_index;
126020 }
126021 }
126022 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
126023 if( !ifNotExist ){
126024 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
126025 }else{
126026 assert( !db->init.busy )((void) (0));
126027 sqlite3CodeVerifySchema(pParse, iDb);
126028 sqlite3ForceNotReadOnly(pParse);
126029 }
126030 goto exit_create_index;
126031 }
126032 }
126033 }else{
126034 int n;
126035 Index *pLoop;
126036 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
126037 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
126038 if( zName==0 ){
126039 goto exit_create_index;
126040 }
126041
126042 /* Automatic index names generated from within sqlite3_declare_vtab()
126043 ** must have names that are distinct from normal automatic index names.
126044 ** The following statement converts "sqlite3_autoindex..." into
126045 ** "sqlite3_butoindex..." in order to make the names distinct.
126046 ** The "vtab_err.test" test demonstrates the need of this statement. */
126047 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ) zName[7]++;
126048 }
126049
126050 /* Check for authorization to create an index.
126051 */
126052#ifndef SQLITE_OMIT_AUTHORIZATION
126053 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126054 const char *zDb = pDb->zDbSName;
126055 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"), 0, zDb) ){
126056 goto exit_create_index;
126057 }
126058 i = SQLITE_CREATE_INDEX1;
126059 if( !OMIT_TEMPDB0 && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX3;
126060 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
126061 goto exit_create_index;
126062 }
126063 }
126064#endif
126065
126066 /* If pList==0, it means this routine was called to make a primary
126067 ** key out of the last column added to the table under construction.
126068 ** So create a fake list to simulate this.
126069 */
126070 if( pList==0 ){
126071 Token prevCol;
126072 Column *pCol = &pTab->aCol[pTab->nCol-1];
126073 pCol->colFlags |= COLFLAG_UNIQUE0x0008;
126074 sqlite3TokenInit(&prevCol, pCol->zCnName);
126075 pList = sqlite3ExprListAppend(pParse, 0,
126076 sqlite3ExprAlloc(db, TK_ID60, &prevCol, 0));
126077 if( pList==0 ) goto exit_create_index;
126078 assert( pList->nExpr==1 )((void) (0));
126079 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED-1);
126080 }else{
126081 sqlite3ExprListCheckLength(pParse, pList, "index");
126082 if( pParse->nErr ) goto exit_create_index;
126083 }
126084
126085 /* Figure out how many bytes of space are required to store explicitly
126086 ** specified collation sequence names.
126087 */
126088 for(i=0; i<pList->nExpr; i++){
126089 Expr *pExpr = pList->a[i].pExpr;
126090 assert( pExpr!=0 )((void) (0));
126091 if( pExpr->op==TK_COLLATE114 ){
126092 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
126093 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
126094 }
126095 }
126096
126097 /*
126098 ** Allocate the index structure.
126099 */
126100 nName = sqlite3Strlen30(zName);
126101 nExtraCol = pPk ? pPk->nKeyCol : 1;
126102 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ )((void) (0));
126103 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
126104 nName + nExtra + 1, &zExtra);
126105 if( db->mallocFailed ){
126106 goto exit_create_index;
126107 }
126108 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) )((void) (0));
126109 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) )((void) (0));
126110 pIndex->zName = zExtra;
126111 zExtra += nName + 1;
126112 memcpy(pIndex->zName, zName, nName+1);
126113 pIndex->pTable = pTab;
126114 pIndex->onError = (u8)onError;
126115 pIndex->uniqNotNull = onError!=OE_None0;
126116 pIndex->idxType = idxType;
126117 pIndex->pSchema = db->aDb[iDb].pSchema;
126118 pIndex->nKeyCol = pList->nExpr;
126119 if( pPIWhere ){
126120 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx0x000002, pPIWhere, 0);
126121 pIndex->pPartIdxWhere = pPIWhere;
126122 pPIWhere = 0;
126123 }
126124 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
126125
126126 /* Check to see if we should honor DESC requests on index columns
126127 */
126128 if( pDb->pSchema->file_format>=4 ){
126129 sortOrderMask = -1; /* Honor DESC */
126130 }else{
126131 sortOrderMask = 0; /* Ignore DESC */
126132 }
126133
126134 /* Analyze the list of expressions that form the terms of the index and
126135 ** report any errors. In the common case where the expression is exactly
126136 ** a table column, store that column in aiColumn[]. For general expressions,
126137 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
126138 **
126139 ** TODO: Issue a warning if two or more columns of the index are identical.
126140 ** TODO: Issue a warning if the table primary key is used as part of the
126141 ** index key.
126142 */
126143 pListItem = pList->a;
126144 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126145 pIndex->aColExpr = pList;
126146 pList = 0;
126147 }
126148 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
126149 Expr *pCExpr; /* The i-th index expression */
126150 int requestedSortOrder; /* ASC or DESC on the i-th expression */
126151 const char *zColl; /* Collation sequence name */
126152
126153 sqlite3StringToId(pListItem->pExpr);
126154 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr0x000020, pListItem->pExpr, 0);
126155 if( pParse->nErr ) goto exit_create_index;
126156 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
126157 if( pCExpr->op!=TK_COLUMN168 ){
126158 if( pTab==pParse->pNewTable ){
126159 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
126160 "UNIQUE constraints");
126161 goto exit_create_index;
126162 }
126163 if( pIndex->aColExpr==0 ){
126164 pIndex->aColExpr = pList;
126165 pList = 0;
126166 }
126167 j = XN_EXPR(-2);
126168 pIndex->aiColumn[i] = XN_EXPR(-2);
126169 pIndex->uniqNotNull = 0;
126170 pIndex->bHasExpr = 1;
126171 }else{
126172 j = pCExpr->iColumn;
126173 assert( j<=0x7fff )((void) (0));
126174 if( j<0 ){
126175 j = pTab->iPKey;
126176 }else{
126177 if( pTab->aCol[j].notNull==0 ){
126178 pIndex->uniqNotNull = 0;
126179 }
126180 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020 ){
126181 pIndex->bHasVCol = 1;
126182 pIndex->bHasExpr = 1;
126183 }
126184 }
126185 pIndex->aiColumn[i] = (i16)j;
126186 }
126187 zColl = 0;
126188 if( pListItem->pExpr->op==TK_COLLATE114 ){
126189 int nColl;
126190 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) )((void) (0));
126191 zColl = pListItem->pExpr->u.zToken;
126192 nColl = sqlite3Strlen30(zColl) + 1;
126193 assert( nExtra>=nColl )((void) (0));
126194 memcpy(zExtra, zColl, nColl);
126195 zColl = zExtra;
126196 zExtra += nColl;
126197 nExtra -= nColl;
126198 }else if( j>=0 ){
126199 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
126200 }
126201 if( !zColl ) zColl = sqlite3StrBINARY;
126202 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
126203 goto exit_create_index;
126204 }
126205 pIndex->azColl[i] = zColl;
126206 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
126207 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
126208 }
126209
126210 /* Append the table key to the end of the index. For WITHOUT ROWID
126211 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
126212 ** normal tables (when pPk==0) this will be the rowid.
126213 */
126214 if( pPk ){
126215 for(j=0; j<pPk->nKeyCol; j++){
126216 int x = pPk->aiColumn[j];
126217 assert( x>=0 )((void) (0));
126218 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
126219 pIndex->nColumn--;
126220 }else{
126221 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
126222 pIndex->aiColumn[i] = x;
126223 pIndex->azColl[i] = pPk->azColl[j];
126224 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
126225 i++;
126226 }
126227 }
126228 assert( i==pIndex->nColumn )((void) (0));
126229 }else{
126230 pIndex->aiColumn[i] = XN_ROWID(-1);
126231 pIndex->azColl[i] = sqlite3StrBINARY;
126232 }
126233 sqlite3DefaultRowEst(pIndex);
126234 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
126235
126236 /* If this index contains every column of its table, then mark
126237 ** it as a covering index */
126238 assert( HasRowid(pTab)((void) (0))
126239 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 )((void) (0));
126240 recomputeColumnsNotIndexed(pIndex);
126241 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
126242 pIndex->isCovering = 1;
126243 for(j=0; j<pTab->nCol; j++){
126244 if( j==pTab->iPKey ) continue;
126245 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
126246 pIndex->isCovering = 0;
126247 break;
126248 }
126249 }
126250
126251 if( pTab==pParse->pNewTable ){
126252 /* This routine has been called to create an automatic index as a
126253 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
126254 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
126255 ** i.e. one of:
126256 **
126257 ** CREATE TABLE t(x PRIMARY KEY, y);
126258 ** CREATE TABLE t(x, y, UNIQUE(x, y));
126259 **
126260 ** Either way, check to see if the table already has such an index. If
126261 ** so, don't bother creating this one. This only applies to
126262 ** automatically created indices. Users can do as they wish with
126263 ** explicit indices.
126264 **
126265 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
126266 ** (and thus suppressing the second one) even if they have different
126267 ** sort orders.
126268 **
126269 ** If there are different collating sequences or if the columns of
126270 ** the constraint occur in different orders, then the constraints are
126271 ** considered distinct and both result in separate indices.
126272 */
126273 Index *pIdx;
126274 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126275 int k;
126276 assert( IsUniqueIndex(pIdx) )((void) (0));
126277 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF )((void) (0));
126278 assert( IsUniqueIndex(pIndex) )((void) (0));
126279
126280 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
126281 for(k=0; k<pIdx->nKeyCol; k++){
126282 const char *z1;
126283 const char *z2;
126284 assert( pIdx->aiColumn[k]>=0 )((void) (0));
126285 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
126286 z1 = pIdx->azColl[k];
126287 z2 = pIndex->azColl[k];
126288 if( sqlite3StrICmp(z1, z2) ) break;
126289 }
126290 if( k==pIdx->nKeyCol ){
126291 if( pIdx->onError!=pIndex->onError ){
126292 /* This constraint creates the same index as a previous
126293 ** constraint specified somewhere in the CREATE TABLE statement.
126294 ** However the ON CONFLICT clauses are different. If both this
126295 ** constraint and the previous equivalent constraint have explicit
126296 ** ON CONFLICT clauses this is an error. Otherwise, use the
126297 ** explicitly specified behavior for the index.
126298 */
126299 if( !(pIdx->onError==OE_Default11 || pIndex->onError==OE_Default11) ){
126300 sqlite3ErrorMsg(pParse,
126301 "conflicting ON CONFLICT clauses specified", 0);
126302 }
126303 if( pIdx->onError==OE_Default11 ){
126304 pIdx->onError = pIndex->onError;
126305 }
126306 }
126307 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ) pIdx->idxType = idxType;
126308 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126309 pIndex->pNext = pParse->pNewIndex;
126310 pParse->pNewIndex = pIndex;
126311 pIndex = 0;
126312 }
126313 goto exit_create_index;
126314 }
126315 }
126316 }
126317
126318 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126319
126320 /* Link the new Index structure to its table and to the other
126321 ** in-memory database structures.
126322 */
126323 assert( pParse->nErr==0 )((void) (0));
126324 if( db->init.busy ){
126325 Index *p;
126326 assert( !IN_SPECIAL_PARSE )((void) (0));
126327 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
126328 if( pTblName!=0 ){
126329 pIndex->tnum = db->init.newTnum;
126330 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
126331 sqlite3ErrorMsg(pParse, "invalid rootpage");
126332 pParse->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(126332);
126333 goto exit_create_index;
126334 }
126335 }
126336 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
126337 pIndex->zName, pIndex);
126338 if( p ){
126339 assert( p==pIndex )((void) (0)); /* Malloc must have failed */
126340 sqlite3OomFault(db);
126341 goto exit_create_index;
126342 }
126343 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
126344 }
126345
126346 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
126347 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
126348 ** emit code to allocate the index rootpage on disk and make an entry for
126349 ** the index in the sqlite_schema table and populate the index with
126350 ** content. But, do not do this if we are simply reading the sqlite_schema
126351 ** table to parse the schema, or if this index is the PRIMARY KEY index
126352 ** of a WITHOUT ROWID table.
126353 **
126354 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
126355 ** or UNIQUE index in a CREATE TABLE statement. Since the table
126356 ** has just been created, it contains no data and the index initialization
126357 ** step can be skipped.
126358 */
126359 else if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || pTblName!=0 ){
126360 Vdbe *v;
126361 char *zStmt;
126362 int iMem = ++pParse->nMem;
126363
126364 v = sqlite3GetVdbe(pParse);
126365 if( v==0 ) goto exit_create_index;
126366
126367 sqlite3BeginWriteOperation(pParse, 1, iDb);
126368
126369 /* Create the rootpage for the index using CreateIndex. But before
126370 ** doing so, code a Noop instruction and store its address in
126371 ** Index.tnum. This is required in case this index is actually a
126372 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
126373 ** that case the convertToWithoutRowidTable() routine will replace
126374 ** the Noop with a Goto to jump over the VDBE code generated below. */
126375 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop187);
126376 sqlite3VdbeAddOp3(v, OP_CreateBtree147, iDb, iMem, BTREE_BLOBKEY2);
126377
126378 /* Gather the complete text of the CREATE INDEX statement into
126379 ** the zStmt variable
126380 */
126381 assert( pName!=0 || pStart==0 )((void) (0));
126382 if( pStart ){
126383 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
126384 if( pName->z[n-1]==';' ) n--;
126385 /* A named index with an explicit CREATE INDEX statement */
126386 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
126387 onError==OE_None0 ? "" : " UNIQUE", n, pName->z);
126388 }else{
126389 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
126390 /* zStmt = sqlite3MPrintf(""); */
126391 zStmt = 0;
126392 }
126393
126394 /* Add an entry in sqlite_schema for this index
126395 */
126396 sqlite3NestedParse(pParse,
126397 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " VALUES('index',%Q,%Q,#%d,%Q);",
126398 db->aDb[iDb].zDbSName,
126399 pIndex->zName,
126400 pTab->zName,
126401 iMem,
126402 zStmt
126403 );
126404 sqlite3DbFree(db, zStmt);
126405
126406 /* Fill the index with data and reparse the schema. Code an OP_Expire
126407 ** to invalidate all pre-compiled statements.
126408 */
126409 if( pTblName ){
126410 sqlite3RefillIndex(pParse, pIndex, iMem);
126411 sqlite3ChangeCookie(pParse, iDb);
126412 sqlite3VdbeAddParseSchemaOp(v, iDb,
126413 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
126414 sqlite3VdbeAddOp2(v, OP_Expire166, 0, 1);
126415 }
126416
126417 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
126418 }
126419 }
126420 if( db->init.busy || pTblName==0 ){
126421 pIndex->pNext = pTab->pIndex;
126422 pTab->pIndex = pIndex;
126423 pIndex = 0;
126424 }
126425 else if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
126426 assert( pParse->pNewIndex==0 )((void) (0));
126427 pParse->pNewIndex = pIndex;
126428 pIndex = 0;
126429 }
126430
126431 /* Clean up before exiting */
126432exit_create_index:
126433 if( pIndex ) sqlite3FreeIndex(db, pIndex);
126434 if( pTab ){
126435 /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list.
126436 ** The list was already ordered when this routine was entered, so at this
126437 ** point at most a single index (the newly added index) will be out of
126438 ** order. So we have to reorder at most one index. */
126439 Index **ppFrom;
126440 Index *pThis;
126441 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
126442 Index *pNext;
126443 if( pThis->onError!=OE_Replace5 ) continue;
126444 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace5 ){
126445 *ppFrom = pNext;
126446 pThis->pNext = pNext->pNext;
126447 pNext->pNext = pThis;
126448 ppFrom = &pNext->pNext;
126449 }
126450 break;
126451 }
126452#ifdef SQLITE_DEBUG
126453 /* Verify that all REPLACE indexes really are now at the end
126454 ** of the index list. In other words, no other index type ever
126455 ** comes after a REPLACE index on the list. */
126456 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
126457 assert( pThis->onError!=OE_Replace((void) (0))
126458 || pThis->pNext==0((void) (0))
126459 || pThis->pNext->onError==OE_Replace )((void) (0));
126460 }
126461#endif
126462 }
126463 sqlite3ExprDelete(db, pPIWhere);
126464 sqlite3ExprListDelete(db, pList);
126465 sqlite3SrcListDelete(db, pTblName);
126466 sqlite3DbFree(db, zName);
126467}
126468
126469/*
126470** Fill the Index.aiRowEst[] array with default information - information
126471** to be used when we have not run the ANALYZE command.
126472**
126473** aiRowEst[0] is supposed to contain the number of elements in the index.
126474** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
126475** number of rows in the table that match any particular value of the
126476** first column of the index. aiRowEst[2] is an estimate of the number
126477** of rows that match any particular combination of the first 2 columns
126478** of the index. And so forth. It must always be the case that
126479*
126480** aiRowEst[N]<=aiRowEst[N-1]
126481** aiRowEst[N]>=1
126482**
126483** Apart from that, we have little to go on besides intuition as to
126484** how aiRowEst[] should be initialized. The numbers generated here
126485** are based on typical values found in actual indices.
126486*/
126487SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index *pIdx){
126488 /* 10, 9, 8, 7, 6 */
126489 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
126490 LogEst *a = pIdx->aiRowLogEst;
126491 LogEst x;
126492 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol)((((int)(sizeof(aVal)/sizeof(aVal[0]))))<(pIdx->nKeyCol
)?(((int)(sizeof(aVal)/sizeof(aVal[0])))):(pIdx->nKeyCol))
;
126493 int i;
126494
126495 /* Indexes with default row estimates should not have stat1 data */
126496 assert( !pIdx->hasStat1 )((void) (0));
126497
126498 /* Set the first entry (number of rows in the index) to the estimated
126499 ** number of rows in the table, or half the number of rows in the table
126500 ** for a partial index.
126501 **
126502 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
126503 ** table but other parts we are having to guess at, then do not let the
126504 ** estimated number of rows in the table be less than 1000 (LogEst 99).
126505 ** Failure to do this can cause the indexes for which we do not have
126506 ** stat1 data to be ignored by the query planner.
126507 */
126508 x = pIdx->pTable->nRowLogEst;
126509 assert( 99==sqlite3LogEst(1000) )((void) (0));
126510 if( x<99 ){
126511 pIdx->pTable->nRowLogEst = x = 99;
126512 }
126513 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) )((void) (0)); }
126514 a[0] = x;
126515
126516 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
126517 ** 6 and each subsequent value (if any) is 5. */
126518 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
126519 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
126520 a[i] = 23; assert( 23==sqlite3LogEst(5) )((void) (0));
126521 }
126522
126523 assert( 0==sqlite3LogEst(1) )((void) (0));
126524 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) a[pIdx->nKeyCol] = 0;
126525}
126526
126527/*
126528** This routine will drop an existing named index. This routine
126529** implements the DROP INDEX statement.
126530*/
126531SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
126532 Index *pIndex;
126533 Vdbe *v;
126534 sqlite3 *db = pParse->db;
126535 int iDb;
126536
126537 if( db->mallocFailed ){
126538 goto exit_drop_index;
126539 }
126540 assert( pParse->nErr==0 )((void) (0)); /* Never called with prior non-OOM errors */
126541 assert( pName->nSrc==1 )((void) (0));
126542 assert( pName->a[0].fg.fixedSchema==0 )((void) (0));
126543 assert( pName->a[0].fg.isSubquery==0 )((void) (0));
126544 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
126545 goto exit_drop_index;
126546 }
126547 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].u4.zDatabase);
126548 if( pIndex==0 ){
126549 if( !ifExists ){
126550 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
126551 }else{
126552 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
126553 sqlite3ForceNotReadOnly(pParse);
126554 }
126555 pParse->checkSchema = 1;
126556 goto exit_drop_index;
126557 }
126558 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF0 ){
126559 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
126560 "or PRIMARY KEY constraint cannot be dropped", 0);
126561 goto exit_drop_index;
126562 }
126563 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
126564#ifndef SQLITE_OMIT_AUTHORIZATION
126565 {
126566 int code = SQLITE_DROP_INDEX10;
126567 Table *pTab = pIndex->pTable;
126568 const char *zDb = db->aDb[iDb].zDbSName;
126569 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
126570 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
126571 goto exit_drop_index;
126572 }
126573 if( !OMIT_TEMPDB0 && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX12;
126574 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
126575 goto exit_drop_index;
126576 }
126577 }
126578#endif
126579
126580 /* Generate code to remove the index and from the schema table */
126581 v = sqlite3GetVdbe(pParse);
126582 if( v ){
126583 sqlite3BeginWriteOperation(pParse, 1, iDb);
126584 sqlite3NestedParse(pParse,
126585 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " WHERE name=%Q AND type='index'",
126586 db->aDb[iDb].zDbSName, pIndex->zName
126587 );
126588 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
126589 sqlite3ChangeCookie(pParse, iDb);
126590 destroyRootPage(pParse, pIndex->tnum, iDb);
126591 sqlite3VdbeAddOp4(v, OP_DropIndex152, iDb, 0, 0, pIndex->zName, 0);
126592 }
126593
126594exit_drop_index:
126595 sqlite3SrcListDelete(db, pName);
126596}
126597
126598/*
126599** pArray is a pointer to an array of objects. Each object in the
126600** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
126601** to extend the array so that there is space for a new object at the end.
126602**
126603** When this function is called, *pnEntry contains the current size of
126604** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
126605** in total).
126606**
126607** If the realloc() is successful (i.e. if no OOM condition occurs), the
126608** space allocated for the new object is zeroed, *pnEntry updated to
126609** reflect the new size of the array and a pointer to the new allocation
126610** returned. *pIdx is set to the index of the new array entry in this case.
126611**
126612** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
126613** unchanged and a copy of pArray returned.
126614*/
126615SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(
126616 sqlite3 *db, /* Connection to notify of malloc failures */
126617 void *pArray, /* Array of objects. Might be reallocated */
126618 int szEntry, /* Size of each object in the array */
126619 int *pnEntry, /* Number of objects currently in use */
126620 int *pIdx /* Write the index of a new slot here */
126621){
126622 char *z;
126623 sqlite3_int64 n = *pIdx = *pnEntry;
126624 if( (n & (n-1))==0 ){
126625 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
126626 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
126627 if( pNew==0 ){
126628 *pIdx = -1;
126629 return pArray;
126630 }
126631 pArray = pNew;
126632 }
126633 z = (char*)pArray;
126634 memset(&z[n * szEntry], 0, szEntry);
126635 ++*pnEntry;
126636 return pArray;
126637}
126638
126639/*
126640** Append a new element to the given IdList. Create a new IdList if
126641** need be.
126642**
126643** A new IdList is returned, or NULL if malloc() fails.
126644*/
126645SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
126646 sqlite3 *db = pParse->db;
126647 int i;
126648 if( pList==0 ){
126649 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
126650 if( pList==0 ) return 0;
126651 }else{
126652 IdList *pNew;
126653 pNew = sqlite3DbRealloc(db, pList,
126654 sizeof(IdList) + pList->nId*sizeof(pList->a));
126655 if( pNew==0 ){
126656 sqlite3IdListDelete(db, pList);
126657 return 0;
126658 }
126659 pList = pNew;
126660 }
126661 i = pList->nId++;
126662 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
126663 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList->a[i].zName ){
126664 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
126665 }
126666 return pList;
126667}
126668
126669/*
126670** Delete an IdList.
126671*/
126672SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
126673 int i;
126674 assert( db!=0 )((void) (0));
126675 if( pList==0 ) return;
126676 assert( pList->eU4!=EU4_EXPR )((void) (0)); /* EU4_EXPR mode is not currently used */
126677 for(i=0; i<pList->nId; i++){
126678 sqlite3DbFree(db, pList->a[i].zName);
126679 }
126680 sqlite3DbNNFreeNN(db, pList);
126681}
126682
126683/*
126684** Return the index in pList of the identifier named zId. Return -1
126685** if not found.
126686*/
126687SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList *pList, const char *zName){
126688 int i;
126689 assert( pList!=0 )((void) (0));
126690 for(i=0; i<pList->nId; i++){
126691 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
126692 }
126693 return -1;
126694}
126695
126696/*
126697** Maximum size of a SrcList object.
126698** The SrcList object is used to represent the FROM clause of a
126699** SELECT statement, and the query planner cannot deal with more
126700** than 64 tables in a join. So any value larger than 64 here
126701** is sufficient for most uses. Smaller values, like say 10, are
126702** appropriate for small and memory-limited applications.
126703*/
126704#ifndef SQLITE_MAX_SRCLIST200
126705# define SQLITE_MAX_SRCLIST200 200
126706#endif
126707
126708/*
126709** Expand the space allocated for the given SrcList object by
126710** creating nExtra new slots beginning at iStart. iStart is zero based.
126711** New slots are zeroed.
126712**
126713** For example, suppose a SrcList initially contains two entries: A,B.
126714** To append 3 new entries onto the end, do this:
126715**
126716** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
126717**
126718** After the call above it would contain: A, B, nil, nil, nil.
126719** If the iStart argument had been 1 instead of 2, then the result
126720** would have been: A, nil, nil, nil, B. To prepend the new slots,
126721** the iStart value would be 0. The result then would
126722** be: nil, nil, nil, A, B.
126723**
126724** If a memory allocation fails or the SrcList becomes too large, leave
126725** the original SrcList unchanged, return NULL, and leave an error message
126726** in pParse.
126727*/
126728SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(
126729 Parse *pParse, /* Parsing context into which errors are reported */
126730 SrcList *pSrc, /* The SrcList to be enlarged */
126731 int nExtra, /* Number of new slots to add to pSrc->a[] */
126732 int iStart /* Index in pSrc->a[] of first new slot */
126733){
126734 int i;
126735
126736 /* Sanity checking on calling parameters */
126737 assert( iStart>=0 )((void) (0));
126738 assert( nExtra>=1 )((void) (0));
126739 assert( pSrc!=0 )((void) (0));
126740 assert( iStart<=pSrc->nSrc )((void) (0));
126741
126742 /* Allocate additional space if needed */
126743 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
126744 SrcList *pNew;
126745 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
126746 sqlite3 *db = pParse->db;
126747
126748 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST200 ){
126749 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
126750 SQLITE_MAX_SRCLIST200);
126751 return 0;
126752 }
126753 if( nAlloc>SQLITE_MAX_SRCLIST200 ) nAlloc = SQLITE_MAX_SRCLIST200;
126754 pNew = sqlite3DbRealloc(db, pSrc,
126755 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
126756 if( pNew==0 ){
126757 assert( db->mallocFailed )((void) (0));
126758 return 0;
126759 }
126760 pSrc = pNew;
126761 pSrc->nAlloc = nAlloc;
126762 }
126763
126764 /* Move existing slots that come after the newly inserted slots
126765 ** out of the way */
126766 for(i=pSrc->nSrc-1; i>=iStart; i--){
126767 pSrc->a[i+nExtra] = pSrc->a[i];
126768 }
126769 pSrc->nSrc += nExtra;
126770
126771 /* Zero the newly allocated slots */
126772 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
126773 for(i=iStart; i<iStart+nExtra; i++){
126774 pSrc->a[i].iCursor = -1;
126775 }
126776
126777 /* Return a pointer to the enlarged SrcList */
126778 return pSrc;
126779}
126780
126781
126782/*
126783** Append a new table name to the given SrcList. Create a new SrcList if
126784** need be. A new entry is created in the SrcList even if pTable is NULL.
126785**
126786** A SrcList is returned, or NULL if there is an OOM error or if the
126787** SrcList grows to large. The returned
126788** SrcList might be the same as the SrcList that was input or it might be
126789** a new one. If an OOM error does occurs, then the prior value of pList
126790** that is input to this routine is automatically freed.
126791**
126792** If pDatabase is not null, it means that the table has an optional
126793** database name prefix. Like this: "database.table". The pDatabase
126794** points to the table name and the pTable points to the database name.
126795** The SrcList.a[].zName field is filled with the table name which might
126796** come from pTable (if pDatabase is NULL) or from pDatabase.
126797** SrcList.a[].zDatabase is filled with the database name from pTable,
126798** or with NULL if no database is specified.
126799**
126800** In other words, if call like this:
126801**
126802** sqlite3SrcListAppend(D,A,B,0);
126803**
126804** Then B is a table name and the database name is unspecified. If called
126805** like this:
126806**
126807** sqlite3SrcListAppend(D,A,B,C);
126808**
126809** Then C is the table name and B is the database name. If C is defined
126810** then so is B. In other words, we never have a case where:
126811**
126812** sqlite3SrcListAppend(D,A,0,C);
126813**
126814** Both pTable and pDatabase are assumed to be quoted. They are dequoted
126815** before being added to the SrcList.
126816*/
126817SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(
126818 Parse *pParse, /* Parsing context, in which errors are reported */
126819 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
126820 Token *pTable, /* Table to append */
126821 Token *pDatabase /* Database of the table */
126822){
126823 SrcItem *pItem;
126824 sqlite3 *db;
126825 assert( pDatabase==0 || pTable!=0 )((void) (0)); /* Cannot have C without B */
126826 assert( pParse!=0 )((void) (0));
126827 assert( pParse->db!=0 )((void) (0));
126828 db = pParse->db;
126829 if( pList==0 ){
126830 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
126831 if( pList==0 ) return 0;
126832 pList->nAlloc = 1;
126833 pList->nSrc = 1;
126834 memset(&pList->a[0], 0, sizeof(pList->a[0]));
126835 pList->a[0].iCursor = -1;
126836 }else{
126837 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
126838 if( pNew==0 ){
126839 sqlite3SrcListDelete(db, pList);
126840 return 0;
126841 }else{
126842 pList = pNew;
126843 }
126844 }
126845 pItem = &pList->a[pList->nSrc-1];
126846 if( pDatabase && pDatabase->z==0 ){
126847 pDatabase = 0;
126848 }
126849 assert( pItem->fg.fixedSchema==0 )((void) (0));
126850 assert( pItem->fg.isSubquery==0 )((void) (0));
126851 if( pDatabase ){
126852 pItem->zName = sqlite3NameFromToken(db, pDatabase);
126853 pItem->u4.zDatabase = sqlite3NameFromToken(db, pTable);
126854 }else{
126855 pItem->zName = sqlite3NameFromToken(db, pTable);
126856 pItem->u4.zDatabase = 0;
126857 }
126858 return pList;
126859}
126860
126861/*
126862** Assign VdbeCursor index numbers to all tables in a SrcList
126863*/
126864SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
126865 int i;
126866 SrcItem *pItem;
126867 assert( pList || pParse->db->mallocFailed )((void) (0));
126868 if( ALWAYS(pList)(pList) ){
126869 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
126870 if( pItem->iCursor>=0 ) continue;
126871 pItem->iCursor = pParse->nTab++;
126872 if( pItem->fg.isSubquery ){
126873 assert( pItem->u4.pSubq!=0 )((void) (0));
126874 assert( pItem->u4.pSubq->pSelect!=0 )((void) (0));
126875 assert( pItem->u4.pSubq->pSelect->pSrc!=0 )((void) (0));
126876 sqlite3SrcListAssignCursors(pParse, pItem->u4.pSubq->pSelect->pSrc);
126877 }
126878 }
126879 }
126880}
126881
126882/*
126883** Delete a Subquery object and its substructure.
126884*/
126885SQLITE_PRIVATEstatic void sqlite3SubqueryDelete(sqlite3 *db, Subquery *pSubq){
126886 assert( pSubq!=0 && pSubq->pSelect!=0 )((void) (0));
126887 sqlite3SelectDelete(db, pSubq->pSelect);
126888 sqlite3DbFree(db, pSubq);
126889}
126890
126891/*
126892** Remove a Subquery from a SrcItem. Return the associated Select object.
126893** The returned Select becomes the responsibility of the caller.
126894*/
126895SQLITE_PRIVATEstatic Select *sqlite3SubqueryDetach(sqlite3 *db, SrcItem *pItem){
126896 Select *pSel;
126897 assert( pItem!=0 )((void) (0));
126898 assert( pItem->fg.isSubquery )((void) (0));
126899 pSel = pItem->u4.pSubq->pSelect;
126900 sqlite3DbFree(db, pItem->u4.pSubq);
126901 pItem->u4.pSubq = 0;
126902 pItem->fg.isSubquery = 0;
126903 return pSel;
126904}
126905
126906/*
126907** Delete an entire SrcList including all its substructure.
126908*/
126909SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
126910 int i;
126911 SrcItem *pItem;
126912 assert( db!=0 )((void) (0));
126913 if( pList==0 ) return;
126914 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
126915
126916 /* Check invariants on SrcItem */
126917 assert( !pItem->fg.isIndexedBy || !pItem->fg.isTabFunc )((void) (0));
126918 assert( !pItem->fg.isCte || !pItem->fg.isIndexedBy )((void) (0));
126919 assert( !pItem->fg.fixedSchema || !pItem->fg.isSubquery )((void) (0));
126920 assert( !pItem->fg.isSubquery || (pItem->u4.pSubq!=0 &&((void) (0))
126921 pItem->u4.pSubq->pSelect!=0) )((void) (0));
126922
126923 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
126924 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
126925 if( pItem->fg.isSubquery ){
126926 sqlite3SubqueryDelete(db, pItem->u4.pSubq);
126927 }else if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
126928 sqlite3DbNNFreeNN(db, pItem->u4.zDatabase);
126929 }
126930 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
126931 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
126932 sqlite3DeleteTable(db, pItem->pSTab);
126933 if( pItem->fg.isUsing ){
126934 sqlite3IdListDelete(db, pItem->u3.pUsing);
126935 }else if( pItem->u3.pOn ){
126936 sqlite3ExprDelete(db, pItem->u3.pOn);
126937 }
126938 }
126939 sqlite3DbNNFreeNN(db, pList);
126940}
126941
126942/*
126943** Attach a Subquery object to pItem->uv.pSubq. Set the
126944** pSelect value but leave all the other values initialized
126945** to zero.
126946**
126947** A copy of the Select object is made if dupSelect is true, and the
126948** SrcItem takes responsibility for deleting the copy. If dupSelect is
126949** false, ownership of the Select passes to the SrcItem. Either way,
126950** the SrcItem will take responsibility for deleting the Select.
126951**
126952** When dupSelect is zero, that means the Select might get deleted right
126953** away if there is an OOM error. Beware.
126954**
126955** Return non-zero on success. Return zero on an OOM error.
126956*/
126957SQLITE_PRIVATEstatic int sqlite3SrcItemAttachSubquery(
126958 Parse *pParse, /* Parsing context */
126959 SrcItem *pItem, /* Item to which the subquery is to be attached */
126960 Select *pSelect, /* The subquery SELECT. Must be non-NULL */
126961 int dupSelect /* If true, attach a copy of pSelect, not pSelect itself.*/
126962){
126963 Subquery *p;
126964 assert( pSelect!=0 )((void) (0));
126965 assert( pItem->fg.isSubquery==0 )((void) (0));
126966 if( pItem->fg.fixedSchema ){
126967 pItem->u4.pSchema = 0;
126968 pItem->fg.fixedSchema = 0;
126969 }else if( pItem->u4.zDatabase!=0 ){
126970 sqlite3DbFree(pParse->db, pItem->u4.zDatabase);
126971 pItem->u4.zDatabase = 0;
126972 }
126973 if( dupSelect ){
126974 pSelect = sqlite3SelectDup(pParse->db, pSelect, 0);
126975 if( pSelect==0 ) return 0;
126976 }
126977 p = pItem->u4.pSubq = sqlite3DbMallocRawNN(pParse->db, sizeof(Subquery));
126978 if( p==0 ){
126979 sqlite3SelectDelete(pParse->db, pSelect);
126980 return 0;
126981 }
126982 pItem->fg.isSubquery = 1;
126983 p->pSelect = pSelect;
126984 assert( offsetof(Subquery, pSelect)==0 )((void) (0));
126985 memset(((char*)p)+sizeof(p->pSelect), 0, sizeof(*p)-sizeof(p->pSelect));
126986 return 1;
126987}
126988
126989
126990/*
126991** This routine is called by the parser to add a new term to the
126992** end of a growing FROM clause. The "p" parameter is the part of
126993** the FROM clause that has already been constructed. "p" is NULL
126994** if this is the first term of the FROM clause. pTable and pDatabase
126995** are the name of the table and database named in the FROM clause term.
126996** pDatabase is NULL if the database name qualifier is missing - the
126997** usual case. If the term has an alias, then pAlias points to the
126998** alias token. If the term is a subquery, then pSubquery is the
126999** SELECT statement that the subquery encodes. The pTable and
127000** pDatabase parameters are NULL for subqueries. The pOn and pUsing
127001** parameters are the content of the ON and USING clauses.
127002**
127003** Return a new SrcList which encodes is the FROM with the new
127004** term added.
127005*/
127006SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(
127007 Parse *pParse, /* Parsing context */
127008 SrcList *p, /* The left part of the FROM clause already seen */
127009 Token *pTable, /* Name of the table to add to the FROM clause */
127010 Token *pDatabase, /* Name of the database containing pTable */
127011 Token *pAlias, /* The right-hand side of the AS subexpression */
127012 Select *pSubquery, /* A subquery used in place of a table name */
127013 OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */
127014){
127015 SrcItem *pItem;
127016 sqlite3 *db = pParse->db;
127017 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
127018 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
127019 (pOnUsing->pOn ? "ON" : "USING")
127020 );
127021 goto append_from_error;
127022 }
127023 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
127024 if( p==0 ){
127025 goto append_from_error;
127026 }
127027 assert( p->nSrc>0 )((void) (0));
127028 pItem = &p->a[p->nSrc-1];
127029 assert( (pTable==0)==(pDatabase==0) )((void) (0));
127030 assert( pItem->zName==0 || pDatabase!=0 )((void) (0));
127031 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zName ){
127032 Token *pToken = (ALWAYS(pDatabase)(pDatabase) && pDatabase->z) ? pDatabase : pTable;
127033 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
127034 }
127035 assert( pAlias!=0 )((void) (0));
127036 if( pAlias->n ){
127037 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
127038 }
127039 assert( pSubquery==0 || pDatabase==0 )((void) (0));
127040 if( pSubquery ){
127041 if( sqlite3SrcItemAttachSubquery(pParse, pItem, pSubquery, 0) ){
127042 if( pSubquery->selFlags & SF_NestedFrom0x0000800 ){
127043 pItem->fg.isNestedFrom = 1;
127044 }
127045 }
127046 }
127047 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 )((void) (0));
127048 assert( pItem->fg.isUsing==0 )((void) (0));
127049 if( pOnUsing==0 ){
127050 pItem->u3.pOn = 0;
127051 }else if( pOnUsing->pUsing ){
127052 pItem->fg.isUsing = 1;
127053 pItem->u3.pUsing = pOnUsing->pUsing;
127054 }else{
127055 pItem->u3.pOn = pOnUsing->pOn;
127056 }
127057 return p;
127058
127059append_from_error:
127060 assert( p==0 )((void) (0));
127061 sqlite3ClearOnOrUsing(db, pOnUsing);
127062 sqlite3SelectDelete(db, pSubquery);
127063 return 0;
127064}
127065
127066/*
127067** Add an INDEXED BY or NOT INDEXED clause to the most recently added
127068** element of the source-list passed as the second argument.
127069*/
127070SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
127071 assert( pIndexedBy!=0 )((void) (0));
127072 if( p && pIndexedBy->n>0 ){
127073 SrcItem *pItem;
127074 assert( p->nSrc>0 )((void) (0));
127075 pItem = &p->a[p->nSrc-1];
127076 assert( pItem->fg.notIndexed==0 )((void) (0));
127077 assert( pItem->fg.isIndexedBy==0 )((void) (0));
127078 assert( pItem->fg.isTabFunc==0 )((void) (0));
127079 if( pIndexedBy->n==1 && !pIndexedBy->z ){
127080 /* A "NOT INDEXED" clause was supplied. See parse.y
127081 ** construct "indexed_opt" for details. */
127082 pItem->fg.notIndexed = 1;
127083 }else{
127084 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
127085 pItem->fg.isIndexedBy = 1;
127086 assert( pItem->fg.isCte==0 )((void) (0)); /* No collision on union u2 */
127087 }
127088 }
127089}
127090
127091/*
127092** Append the contents of SrcList p2 to SrcList p1 and return the resulting
127093** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
127094** are deleted by this function.
127095*/
127096SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
127097 assert( p1 && p1->nSrc==1 )((void) (0));
127098 if( p2 ){
127099 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
127100 if( pNew==0 ){
127101 sqlite3SrcListDelete(pParse->db, p2);
127102 }else{
127103 p1 = pNew;
127104 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
127105 sqlite3DbFree(pParse->db, p2);
127106 p1->a[0].fg.jointype |= (JT_LTORJ0x40 & p1->a[1].fg.jointype);
127107 }
127108 }
127109 return p1;
127110}
127111
127112/*
127113** Add the list of function arguments to the SrcList entry for a
127114** table-valued-function.
127115*/
127116SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
127117 if( p ){
127118 SrcItem *pItem = &p->a[p->nSrc-1];
127119 assert( pItem->fg.notIndexed==0 )((void) (0));
127120 assert( pItem->fg.isIndexedBy==0 )((void) (0));
127121 assert( pItem->fg.isTabFunc==0 )((void) (0));
127122 pItem->u1.pFuncArg = pList;
127123 pItem->fg.isTabFunc = 1;
127124 }else{
127125 sqlite3ExprListDelete(pParse->db, pList);
127126 }
127127}
127128
127129/*
127130** When building up a FROM clause in the parser, the join operator
127131** is initially attached to the left operand. But the code generator
127132** expects the join operator to be on the right operand. This routine
127133** Shifts all join operators from left to right for an entire FROM
127134** clause.
127135**
127136** Example: Suppose the join is like this:
127137**
127138** A natural cross join B
127139**
127140** The operator is "natural cross join". The A and B operands are stored
127141** in p->a[0] and p->a[1], respectively. The parser initially stores the
127142** operator with A. This routine shifts that operator over to B.
127143**
127144** Additional changes:
127145**
127146** * All tables to the left of the right-most RIGHT JOIN are tagged with
127147** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the
127148** code generator can easily tell that the table is part of
127149** the left operand of at least one RIGHT JOIN.
127150*/
127151SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){
127152 (void)pParse;
127153 if( p && p->nSrc>1 ){
127154 int i = p->nSrc-1;
127155 u8 allFlags = 0;
127156 do{
127157 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
127158 }while( (--i)>0 );
127159 p->a[0].fg.jointype = 0;
127160
127161 /* All terms to the left of a RIGHT JOIN should be tagged with the
127162 ** JT_LTORJ flags */
127163 if( allFlags & JT_RIGHT0x10 ){
127164 for(i=p->nSrc-1; ALWAYS(i>0)(i>0) && (p->a[i].fg.jointype&JT_RIGHT0x10)==0; i--){}
127165 i--;
127166 assert( i>=0 )((void) (0));
127167 do{
127168 p->a[i].fg.jointype |= JT_LTORJ0x40;
127169 }while( (--i)>=0 );
127170 }
127171 }
127172}
127173
127174/*
127175** Generate VDBE code for a BEGIN statement.
127176*/
127177SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse *pParse, int type){
127178 sqlite3 *db;
127179 Vdbe *v;
127180 int i;
127181
127182 assert( pParse!=0 )((void) (0));
127183 db = pParse->db;
127184 assert( db!=0 )((void) (0));
127185 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22, "BEGIN", 0, 0) ){
127186 return;
127187 }
127188 v = sqlite3GetVdbe(pParse);
127189 if( !v ) return;
127190 if( type!=TK_DEFERRED7 ){
127191 for(i=0; i<db->nDb; i++){
127192 int eTxnType;
127193 Btree *pBt = db->aDb[i].pBt;
127194 if( pBt && sqlite3BtreeIsReadonly(pBt) ){
127195 eTxnType = 0; /* Read txn */
127196 }else if( type==TK_EXCLUSIVE9 ){
127197 eTxnType = 2; /* Exclusive txn */
127198 }else{
127199 eTxnType = 1; /* Write txn */
127200 }
127201 sqlite3VdbeAddOp2(v, OP_Transaction2, i, eTxnType);
127202 sqlite3VdbeUsesBtree(v, i);
127203 }
127204 }
127205 sqlite3VdbeAddOp0(v, OP_AutoCommit1);
127206}
127207
127208/*
127209** Generate VDBE code for a COMMIT or ROLLBACK statement.
127210** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
127211** code is generated for a COMMIT.
127212*/
127213SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse *pParse, int eType){
127214 Vdbe *v;
127215 int isRollback;
127216
127217 assert( pParse!=0 )((void) (0));
127218 assert( pParse->db!=0 )((void) (0));
127219 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK )((void) (0));
127220 isRollback = eType==TK_ROLLBACK12;
127221 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22,
127222 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
127223 return;
127224 }
127225 v = sqlite3GetVdbe(pParse);
127226 if( v ){
127227 sqlite3VdbeAddOp2(v, OP_AutoCommit1, 1, isRollback);
127228 }
127229}
127230
127231/*
127232** This function is called by the parser when it parses a command to create,
127233** release or rollback an SQL savepoint.
127234*/
127235SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
127236 char *zName = sqlite3NameFromToken(pParse->db, pName);
127237 if( zName ){
127238 Vdbe *v = sqlite3GetVdbe(pParse);
127239#ifndef SQLITE_OMIT_AUTHORIZATION
127240 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
127241 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 )((void) (0));
127242#endif
127243 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT32, az[op], zName, 0) ){
127244 sqlite3DbFree(pParse->db, zName);
127245 return;
127246 }
127247 sqlite3VdbeAddOp4(v, OP_Savepoint0, op, 0, 0, zName, P4_DYNAMIC(-6));
127248 }
127249}
127250
127251/*
127252** Make sure the TEMP database is open and available for use. Return
127253** the number of errors. Leave any error messages in the pParse structure.
127254*/
127255SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *pParse){
127256 sqlite3 *db = pParse->db;
127257 if( db->aDb[1].pBt==0 && !pParse->explain ){
127258 int rc;
127259 Btree *pBt;
127260 static const int flags =
127261 SQLITE_OPEN_READWRITE0x00000002 |
127262 SQLITE_OPEN_CREATE0x00000004 |
127263 SQLITE_OPEN_EXCLUSIVE0x00000010 |
127264 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
127265 SQLITE_OPEN_TEMP_DB0x00000200;
127266
127267 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
127268 if( rc!=SQLITE_OK0 ){
127269 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
127270 "file for storing temporary tables");
127271 pParse->rc = rc;
127272 return 1;
127273 }
127274 db->aDb[1].pBt = pBt;
127275 assert( db->aDb[1].pSchema )((void) (0));
127276 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
127277 sqlite3OomFault(db);
127278 return 1;
127279 }
127280 }
127281 return 0;
127282}
127283
127284/*
127285** Record the fact that the schema cookie will need to be verified
127286** for database iDb. The code to actually verify the schema cookie
127287** will occur at the end of the top-level VDBE and will be generated
127288** later, by sqlite3FinishCoding().
127289*/
127290static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
127291 assert( iDb>=0 && iDb<pToplevel->db->nDb )((void) (0));
127292 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 )((void) (0));
127293 assert( iDb<SQLITE_MAX_DB )((void) (0));
127294 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) )((void) (0));
127295 if( DbMaskTest(pToplevel->cookieMask, iDb)(((pToplevel->cookieMask)&(((yDbMask)1)<<(iDb)))
!=0)
==0 ){
127296 DbMaskSet(pToplevel->cookieMask, iDb)((pToplevel->cookieMask)|=(((yDbMask)1)<<(iDb)));
127297 if( !OMIT_TEMPDB0 && iDb==1 ){
127298 sqlite3OpenTempDatabase(pToplevel);
127299 }
127300 }
127301}
127302SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
127303 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse)), iDb);
127304}
127305
127306
127307/*
127308** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
127309** attached database. Otherwise, invoke it for the database named zDb only.
127310*/
127311SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
127312 sqlite3 *db = pParse->db;
127313 int i;
127314 for(i=0; i<db->nDb; i++){
127315 Db *pDb = &db->aDb[i];
127316 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
127317 sqlite3CodeVerifySchema(pParse, i);
127318 }
127319 }
127320}
127321
127322/*
127323** Generate VDBE code that prepares for doing an operation that
127324** might change the database.
127325**
127326** This routine starts a new transaction if we are not already within
127327** a transaction. If we are already within a transaction, then a checkpoint
127328** is set if the setStatement parameter is true. A checkpoint should
127329** be set for operations that might fail (due to a constraint) part of
127330** the way through and which will need to undo some writes without having to
127331** rollback the whole transaction. For operations where all constraints
127332** can be checked before any changes are made to the database, it is never
127333** necessary to undo a write and the checkpoint should not be set.
127334*/
127335SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
127336 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
127337 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
127338 DbMaskSet(pToplevel->writeMask, iDb)((pToplevel->writeMask)|=(((yDbMask)1)<<(iDb)));
127339 pToplevel->isMultiWrite |= setStatement;
127340}
127341
127342/*
127343** Indicate that the statement currently under construction might write
127344** more than one entry (example: deleting one row then inserting another,
127345** inserting multiple rows in a table, or inserting a row and index entries.)
127346** If an abort occurs after some of these writes have completed, then it will
127347** be necessary to undo the completed writes.
127348*/
127349SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse *pParse){
127350 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
127351 pToplevel->isMultiWrite = 1;
127352}
127353
127354/*
127355** The code generator calls this routine if is discovers that it is
127356** possible to abort a statement prior to completion. In order to
127357** perform this abort without corrupting the database, we need to make
127358** sure that the statement is protected by a statement transaction.
127359**
127360** Technically, we only need to set the mayAbort flag if the
127361** isMultiWrite flag was previously set. There is a time dependency
127362** such that the abort must occur after the multiwrite. This makes
127363** some statements involving the REPLACE conflict resolution algorithm
127364** go a little faster. But taking advantage of this time dependency
127365** makes it more difficult to prove that the code is correct (in
127366** particular, it prevents us from writing an effective
127367** implementation of sqlite3AssertMayAbort()) and so we have chosen
127368** to take the safe route and skip the optimization.
127369*/
127370SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse *pParse){
127371 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
127372 pToplevel->mayAbort = 1;
127373}
127374
127375/*
127376** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
127377** error. The onError parameter determines which (if any) of the statement
127378** and/or current transaction is rolled back.
127379*/
127380SQLITE_PRIVATEstatic void sqlite3HaltConstraint(
127381 Parse *pParse, /* Parsing context */
127382 int errCode, /* extended error code */
127383 int onError, /* Constraint type */
127384 char *p4, /* Error message */
127385 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
127386 u8 p5Errmsg /* P5_ErrMsg type */
127387){
127388 Vdbe *v;
127389 assert( pParse->pVdbe!=0 )((void) (0));
127390 v = sqlite3GetVdbe(pParse);
127391 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested )((void) (0));
127392 if( onError==OE_Abort2 ){
127393 sqlite3MayAbort(pParse);
127394 }
127395 sqlite3VdbeAddOp4(v, OP_Halt70, errCode, onError, 0, p4, p4type);
127396 sqlite3VdbeChangeP5(v, p5Errmsg);
127397}
127398
127399/*
127400** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
127401*/
127402SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(
127403 Parse *pParse, /* Parsing context */
127404 int onError, /* Constraint type */
127405 Index *pIdx /* The index that triggers the constraint */
127406){
127407 char *zErr;
127408 int j;
127409 StrAccum errMsg;
127410 Table *pTab = pIdx->pTable;
127411
127412 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
127413 pParse->db->aLimit[SQLITE_LIMIT_LENGTH0]);
127414 if( pIdx->aColExpr ){
127415 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
127416 }else{
127417 for(j=0; j<pIdx->nKeyCol; j++){
127418 char *zCol;
127419 assert( pIdx->aiColumn[j]>=0 )((void) (0));
127420 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
127421 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
127422 sqlite3_str_appendall(&errMsg, pTab->zName);
127423 sqlite3_str_append(&errMsg, ".", 1);
127424 sqlite3_str_appendall(&errMsg, zCol);
127425 }
127426 }
127427 zErr = sqlite3StrAccumFinish(&errMsg);
127428 sqlite3HaltConstraint(pParse,
127429 IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8))
127430 : SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)),
127431 onError, zErr, P4_DYNAMIC(-6), P5_ConstraintUnique2);
127432}
127433
127434
127435/*
127436** Code an OP_Halt due to non-unique rowid.
127437*/
127438SQLITE_PRIVATEstatic void sqlite3RowidConstraint(
127439 Parse *pParse, /* Parsing context */
127440 int onError, /* Conflict resolution algorithm */
127441 Table *pTab /* The table with the non-unique rowid */
127442){
127443 char *zMsg;
127444 int rc;
127445 if( pTab->iPKey>=0 ){
127446 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
127447 pTab->aCol[pTab->iPKey].zCnName);
127448 rc = SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8));
127449 }else{
127450 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
127451 rc = SQLITE_CONSTRAINT_ROWID(19 |(10<<8));
127452 }
127453 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC(-6),
127454 P5_ConstraintUnique2);
127455}
127456
127457/*
127458** Check to see if pIndex uses the collating sequence pColl. Return
127459** true if it does and false if it does not.
127460*/
127461#ifndef SQLITE_OMIT_REINDEX
127462static int collationMatch(const char *zColl, Index *pIndex){
127463 int i;
127464 assert( zColl!=0 )((void) (0));
127465 for(i=0; i<pIndex->nColumn; i++){
127466 const char *z = pIndex->azColl[i];
127467 assert( z!=0 || pIndex->aiColumn[i]<0 )((void) (0));
127468 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
127469 return 1;
127470 }
127471 }
127472 return 0;
127473}
127474#endif
127475
127476/*
127477** Recompute all indices of pTab that use the collating sequence pColl.
127478** If pColl==0 then recompute all indices of pTab.
127479*/
127480#ifndef SQLITE_OMIT_REINDEX
127481static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
127482 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
127483 Index *pIndex; /* An index associated with pTab */
127484
127485 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
127486 if( zColl==0 || collationMatch(zColl, pIndex) ){
127487 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
127488 sqlite3BeginWriteOperation(pParse, 0, iDb);
127489 sqlite3RefillIndex(pParse, pIndex, -1);
127490 }
127491 }
127492 }
127493}
127494#endif
127495
127496/*
127497** Recompute all indices of all tables in all databases where the
127498** indices use the collating sequence pColl. If pColl==0 then recompute
127499** all indices everywhere.
127500*/
127501#ifndef SQLITE_OMIT_REINDEX
127502static void reindexDatabases(Parse *pParse, char const *zColl){
127503 Db *pDb; /* A single database */
127504 int iDb; /* The database index number */
127505 sqlite3 *db = pParse->db; /* The database connection */
127506 HashElem *k; /* For looping over tables in pDb */
127507 Table *pTab; /* A table in the database */
127508
127509 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0)); /* Needed for schema access */
127510 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
127511 assert( pDb!=0 )((void) (0));
127512 for(k=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
127513 pTab = (Table*)sqliteHashData(k)((k)->data);
127514 reindexTable(pParse, pTab, zColl);
127515 }
127516 }
127517}
127518#endif
127519
127520/*
127521** Generate code for the REINDEX command.
127522**
127523** REINDEX -- 1
127524** REINDEX <collation> -- 2
127525** REINDEX ?<database>.?<tablename> -- 3
127526** REINDEX ?<database>.?<indexname> -- 4
127527**
127528** Form 1 causes all indices in all attached databases to be rebuilt.
127529** Form 2 rebuilds all indices in all databases that use the named
127530** collating function. Forms 3 and 4 rebuild the named index or all
127531** indices associated with the named table.
127532*/
127533#ifndef SQLITE_OMIT_REINDEX
127534SQLITE_PRIVATEstatic void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
127535 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
127536 char *z; /* Name of a table or index */
127537 const char *zDb; /* Name of the database */
127538 Table *pTab; /* A table in the database */
127539 Index *pIndex; /* An index associated with pTab */
127540 int iDb; /* The database index number */
127541 sqlite3 *db = pParse->db; /* The database connection */
127542 Token *pObjName; /* Name of the table or index to be reindexed */
127543
127544 /* Read the database schema. If an error occurs, leave an error message
127545 ** and code in pParse and return NULL. */
127546 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
127547 return;
127548 }
127549
127550 if( pName1==0 ){
127551 reindexDatabases(pParse, 0);
127552 return;
127553 }else if( NEVER(pName2==0)(pName2==0) || pName2->z==0 ){
127554 char *zColl;
127555 assert( pName1->z )((void) (0));
127556 zColl = sqlite3NameFromToken(pParse->db, pName1);
127557 if( !zColl ) return;
127558 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
127559 if( pColl ){
127560 reindexDatabases(pParse, zColl);
127561 sqlite3DbFree(db, zColl);
127562 return;
127563 }
127564 sqlite3DbFree(db, zColl);
127565 }
127566 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
127567 if( iDb<0 ) return;
127568 z = sqlite3NameFromToken(db, pObjName);
127569 if( z==0 ) return;
127570 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
127571 pTab = sqlite3FindTable(db, z, zDb);
127572 if( pTab ){
127573 reindexTable(pParse, pTab, 0);
127574 sqlite3DbFree(db, z);
127575 return;
127576 }
127577 pIndex = sqlite3FindIndex(db, z, zDb);
127578 sqlite3DbFree(db, z);
127579 if( pIndex ){
127580 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema);
127581 sqlite3BeginWriteOperation(pParse, 0, iDb);
127582 sqlite3RefillIndex(pParse, pIndex, -1);
127583 return;
127584 }
127585 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
127586}
127587#endif
127588
127589/*
127590** Return a KeyInfo structure that is appropriate for the given Index.
127591**
127592** The caller should invoke sqlite3KeyInfoUnref() on the returned object
127593** when it has finished using it.
127594*/
127595SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
127596 int i;
127597 int nCol = pIdx->nColumn;
127598 int nKey = pIdx->nKeyCol;
127599 KeyInfo *pKey;
127600 if( pParse->nErr ) return 0;
127601 if( pIdx->uniqNotNull ){
127602 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
127603 }else{
127604 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
127605 }
127606 if( pKey ){
127607 assert( sqlite3KeyInfoIsWriteable(pKey) )((void) (0));
127608 for(i=0; i<nCol; i++){
127609 const char *zColl = pIdx->azColl[i];
127610 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
127611 sqlite3LocateCollSeq(pParse, zColl);
127612 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
127613 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) )((void) (0));
127614 }
127615 if( pParse->nErr ){
127616 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ )((void) (0));
127617 if( pIdx->bNoQuery==0 ){
127618 /* Deactivate the index because it contains an unknown collating
127619 ** sequence. The only way to reactive the index is to reload the
127620 ** schema. Adding the missing collating sequence later does not
127621 ** reactive the index. The application had the chance to register
127622 ** the missing index using the collation-needed callback. For
127623 ** simplicity, SQLite will not give the application a second chance.
127624 */
127625 pIdx->bNoQuery = 1;
127626 pParse->rc = SQLITE_ERROR_RETRY(1 | (2<<8));
127627 }
127628 sqlite3KeyInfoUnref(pKey);
127629 pKey = 0;
127630 }
127631 }
127632 return pKey;
127633}
127634
127635#ifndef SQLITE_OMIT_CTE
127636/*
127637** Create a new CTE object
127638*/
127639SQLITE_PRIVATEstatic Cte *sqlite3CteNew(
127640 Parse *pParse, /* Parsing context */
127641 Token *pName, /* Name of the common-table */
127642 ExprList *pArglist, /* Optional column name list for the table */
127643 Select *pQuery, /* Query used to initialize the table */
127644 u8 eM10d /* The MATERIALIZED flag */
127645){
127646 Cte *pNew;
127647 sqlite3 *db = pParse->db;
127648
127649 pNew = sqlite3DbMallocZero(db, sizeof(*pNew));
127650 assert( pNew!=0 || db->mallocFailed )((void) (0));
127651
127652 if( db->mallocFailed ){
127653 sqlite3ExprListDelete(db, pArglist);
127654 sqlite3SelectDelete(db, pQuery);
127655 }else{
127656 pNew->pSelect = pQuery;
127657 pNew->pCols = pArglist;
127658 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
127659 pNew->eM10d = eM10d;
127660 }
127661 return pNew;
127662}
127663
127664/*
127665** Clear information from a Cte object, but do not deallocate storage
127666** for the object itself.
127667*/
127668static void cteClear(sqlite3 *db, Cte *pCte){
127669 assert( pCte!=0 )((void) (0));
127670 sqlite3ExprListDelete(db, pCte->pCols);
127671 sqlite3SelectDelete(db, pCte->pSelect);
127672 sqlite3DbFree(db, pCte->zName);
127673}
127674
127675/*
127676** Free the contents of the CTE object passed as the second argument.
127677*/
127678SQLITE_PRIVATEstatic void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
127679 assert( pCte!=0 )((void) (0));
127680 cteClear(db, pCte);
127681 sqlite3DbFree(db, pCte);
127682}
127683
127684/*
127685** This routine is invoked once per CTE by the parser while parsing a
127686** WITH clause. The CTE described by the third argument is added to
127687** the WITH clause of the second argument. If the second argument is
127688** NULL, then a new WITH argument is created.
127689*/
127690SQLITE_PRIVATEstatic With *sqlite3WithAdd(
127691 Parse *pParse, /* Parsing context */
127692 With *pWith, /* Existing WITH clause, or NULL */
127693 Cte *pCte /* CTE to add to the WITH clause */
127694){
127695 sqlite3 *db = pParse->db;
127696 With *pNew;
127697 char *zName;
127698
127699 if( pCte==0 ){
127700 return pWith;
127701 }
127702
127703 /* Check that the CTE name is unique within this WITH clause. If
127704 ** not, store an error in the Parse structure. */
127705 zName = pCte->zName;
127706 if( zName && pWith ){
127707 int i;
127708 for(i=0; i<pWith->nCte; i++){
127709 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
127710 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
127711 }
127712 }
127713 }
127714
127715 if( pWith ){
127716 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
127717 pNew = sqlite3DbRealloc(db, pWith, nByte);
127718 }else{
127719 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
127720 }
127721 assert( (pNew!=0 && zName!=0) || db->mallocFailed )((void) (0));
127722
127723 if( db->mallocFailed ){
127724 sqlite3CteDelete(db, pCte);
127725 pNew = pWith;
127726 }else{
127727 pNew->a[pNew->nCte++] = *pCte;
127728 sqlite3DbFree(db, pCte);
127729 }
127730
127731 return pNew;
127732}
127733
127734/*
127735** Free the contents of the With object passed as the second argument.
127736*/
127737SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3 *db, With *pWith){
127738 if( pWith ){
127739 int i;
127740 for(i=0; i<pWith->nCte; i++){
127741 cteClear(db, &pWith->a[i]);
127742 }
127743 sqlite3DbFree(db, pWith);
127744 }
127745}
127746SQLITE_PRIVATEstatic void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){
127747 sqlite3WithDelete(db, (With*)pWith);
127748}
127749#endif /* !defined(SQLITE_OMIT_CTE) */
127750
127751/************** End of build.c ***********************************************/
127752/************** Begin file callback.c ****************************************/
127753/*
127754** 2005 May 23
127755**
127756** The author disclaims copyright to this source code. In place of
127757** a legal notice, here is a blessing:
127758**
127759** May you do good and not evil.
127760** May you find forgiveness for yourself and forgive others.
127761** May you share freely, never taking more than you give.
127762**
127763*************************************************************************
127764**
127765** This file contains functions used to access the internal hash tables
127766** of user defined functions and collation sequences.
127767*/
127768
127769/* #include "sqliteInt.h" */
127770
127771/*
127772** Invoke the 'collation needed' callback to request a collation sequence
127773** in the encoding enc of name zName, length nName.
127774*/
127775static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
127776 assert( !db->xCollNeeded || !db->xCollNeeded16 )((void) (0));
127777 if( db->xCollNeeded ){
127778 char *zExternal = sqlite3DbStrDup(db, zName);
127779 if( !zExternal ) return;
127780 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
127781 sqlite3DbFree(db, zExternal);
127782 }
127783#ifndef SQLITE_OMIT_UTF16
127784 if( db->xCollNeeded16 ){
127785 char const *zExternal;
127786 sqlite3_value *pTmp = sqlite3ValueNew(db);
127787 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
127788 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE2);
127789 if( zExternal ){
127790 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db)((db)->enc), zExternal);
127791 }
127792 sqlite3ValueFree(pTmp);
127793 }
127794#endif
127795}
127796
127797/*
127798** This routine is called if the collation factory fails to deliver a
127799** collation function in the best encoding but there may be other versions
127800** of this collation function (for other text encodings) available. Use one
127801** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
127802** possible.
127803*/
127804static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
127805 CollSeq *pColl2;
127806 char *z = pColl->zName;
127807 int i;
127808 static const u8 aEnc[] = { SQLITE_UTF16BE3, SQLITE_UTF16LE2, SQLITE_UTF81 };
127809 for(i=0; i<3; i++){
127810 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
127811 if( pColl2->xCmp!=0 ){
127812 memcpy(pColl, pColl2, sizeof(CollSeq));
127813 pColl->xDel = 0; /* Do not copy the destructor */
127814 return SQLITE_OK0;
127815 }
127816 }
127817 return SQLITE_ERROR1;
127818}
127819
127820/*
127821** This routine is called on a collation sequence before it is used to
127822** check that it is defined. An undefined collation sequence exists when
127823** a database is loaded that contains references to collation sequences
127824** that have not been defined by sqlite3_create_collation() etc.
127825**
127826** If required, this routine calls the 'collation needed' callback to
127827** request a definition of the collating sequence. If this doesn't work,
127828** an equivalent collating sequence that uses a text encoding different
127829** from the main database is substituted, if one is available.
127830*/
127831SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
127832 if( pColl && pColl->xCmp==0 ){
127833 const char *zName = pColl->zName;
127834 sqlite3 *db = pParse->db;
127835 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), pColl, zName);
127836 if( !p ){
127837 return SQLITE_ERROR1;
127838 }
127839 assert( p==pColl )((void) (0));
127840 }
127841 return SQLITE_OK0;
127842}
127843
127844
127845
127846/*
127847** Locate and return an entry from the db.aCollSeq hash table. If the entry
127848** specified by zName and nName is not found and parameter 'create' is
127849** true, then create a new entry. Otherwise return NULL.
127850**
127851** Each pointer stored in the sqlite3.aCollSeq hash table contains an
127852** array of three CollSeq structures. The first is the collation sequence
127853** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
127854**
127855** Stored immediately after the three collation sequences is a copy of
127856** the collation sequence name. A pointer to this string is stored in
127857** each collation sequence structure.
127858*/
127859static CollSeq *findCollSeqEntry(
127860 sqlite3 *db, /* Database connection */
127861 const char *zName, /* Name of the collating sequence */
127862 int create /* Create a new entry if true */
127863){
127864 CollSeq *pColl;
127865 pColl = sqlite3HashFind(&db->aCollSeq, zName);
127866
127867 if( 0==pColl && create ){
127868 int nName = sqlite3Strlen30(zName) + 1;
127869 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
127870 if( pColl ){
127871 CollSeq *pDel = 0;
127872 pColl[0].zName = (char*)&pColl[3];
127873 pColl[0].enc = SQLITE_UTF81;
127874 pColl[1].zName = (char*)&pColl[3];
127875 pColl[1].enc = SQLITE_UTF16LE2;
127876 pColl[2].zName = (char*)&pColl[3];
127877 pColl[2].enc = SQLITE_UTF16BE3;
127878 memcpy(pColl[0].zName, zName, nName);
127879 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
127880
127881 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
127882 ** return the pColl pointer to be deleted (because it wasn't added
127883 ** to the hash table).
127884 */
127885 assert( pDel==0 || pDel==pColl )((void) (0));
127886 if( pDel!=0 ){
127887 sqlite3OomFault(db);
127888 sqlite3DbFree(db, pDel);
127889 pColl = 0;
127890 }
127891 }
127892 }
127893 return pColl;
127894}
127895
127896/*
127897** Parameter zName points to a UTF-8 encoded string nName bytes long.
127898** Return the CollSeq* pointer for the collation sequence named zName
127899** for the encoding 'enc' from the database 'db'.
127900**
127901** If the entry specified is not found and 'create' is true, then create a
127902** new entry. Otherwise return NULL.
127903**
127904** A separate function sqlite3LocateCollSeq() is a wrapper around
127905** this routine. sqlite3LocateCollSeq() invokes the collation factory
127906** if necessary and generates an error message if the collating sequence
127907** cannot be found.
127908**
127909** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
127910*/
127911SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(
127912 sqlite3 *db, /* Database connection to search */
127913 u8 enc, /* Desired text encoding */
127914 const char *zName, /* Name of the collating sequence. Might be NULL */
127915 int create /* True to create CollSeq if doesn't already exist */
127916){
127917 CollSeq *pColl;
127918 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
127919 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE )((void) (0));
127920 if( zName ){
127921 pColl = findCollSeqEntry(db, zName, create);
127922 if( pColl ) pColl += enc-1;
127923 }else{
127924 pColl = db->pDfltColl;
127925 }
127926 return pColl;
127927}
127928
127929/*
127930** Change the text encoding for a database connection. This means that
127931** the pDfltColl must change as well.
127932*/
127933SQLITE_PRIVATEstatic void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
127934 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
127935 db->enc = enc;
127936 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
127937 ** strings is BINARY.
127938 */
127939 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
127940 sqlite3ExpirePreparedStatements(db, 1);
127941}
127942
127943/*
127944** This function is responsible for invoking the collation factory callback
127945** or substituting a collation sequence of a different encoding when the
127946** requested collation sequence is not available in the desired encoding.
127947**
127948** If it is not NULL, then pColl must point to the database native encoding
127949** collation sequence with name zName, length nName.
127950**
127951** The return value is either the collation sequence to be used in database
127952** db for collation type name zName, length nName, or NULL, if no collation
127953** sequence can be found. If no collation is found, leave an error message.
127954**
127955** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
127956*/
127957SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(
127958 Parse *pParse, /* Parsing context */
127959 u8 enc, /* The desired encoding for the collating sequence */
127960 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
127961 const char *zName /* Collating sequence name */
127962){
127963 CollSeq *p;
127964 sqlite3 *db = pParse->db;
127965
127966 p = pColl;
127967 if( !p ){
127968 p = sqlite3FindCollSeq(db, enc, zName, 0);
127969 }
127970 if( !p || !p->xCmp ){
127971 /* No collation sequence of this type for this encoding is registered.
127972 ** Call the collation factory to see if it can supply us with one.
127973 */
127974 callCollNeeded(db, enc, zName);
127975 p = sqlite3FindCollSeq(db, enc, zName, 0);
127976 }
127977 if( p && !p->xCmp && synthCollSeq(db, p) ){
127978 p = 0;
127979 }
127980 assert( !p || p->xCmp )((void) (0));
127981 if( p==0 ){
127982 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
127983 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8));
127984 }
127985 return p;
127986}
127987
127988/*
127989** This function returns the collation sequence for database native text
127990** encoding identified by the string zName.
127991**
127992** If the requested collation sequence is not available, or not available
127993** in the database native encoding, the collation factory is invoked to
127994** request it. If the collation factory does not supply such a sequence,
127995** and the sequence is available in another text encoding, then that is
127996** returned instead.
127997**
127998** If no versions of the requested collations sequence are available, or
127999** another error occurs, NULL is returned and an error message written into
128000** pParse.
128001**
128002** This routine is a wrapper around sqlite3FindCollSeq(). This routine
128003** invokes the collation factory if the named collation cannot be found
128004** and generates an error message.
128005**
128006** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
128007*/
128008SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
128009 sqlite3 *db = pParse->db;
128010 u8 enc = ENC(db)((db)->enc);
128011 u8 initbusy = db->init.busy;
128012 CollSeq *pColl;
128013
128014 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
128015 if( !initbusy && (!pColl || !pColl->xCmp) ){
128016 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
128017 }
128018
128019 return pColl;
128020}
128021
128022/* During the search for the best function definition, this procedure
128023** is called to test how well the function passed as the first argument
128024** matches the request for a function with nArg arguments in a system
128025** that uses encoding enc. The value returned indicates how well the
128026** request is matched. A higher value indicates a better match.
128027**
128028** If nArg is -1 that means to only return a match (non-zero) if p->nArg
128029** is also -1. In other words, we are searching for a function that
128030** takes a variable number of arguments.
128031**
128032** If nArg is -2 that means that we are searching for any function
128033** regardless of the number of arguments it uses, so return a positive
128034** match score for any
128035**
128036** The returned value is always between 0 and 6, as follows:
128037**
128038** 0: Not a match.
128039** 1: UTF8/16 conversion required and function takes any number of arguments.
128040** 2: UTF16 byte order change required and function takes any number of args.
128041** 3: encoding matches and function takes any number of arguments
128042** 4: UTF8/16 conversion required - argument count matches exactly
128043** 5: UTF16 byte order conversion required - argument count matches exactly
128044** 6: Perfect match: encoding and argument count match exactly.
128045**
128046** If nArg==(-2) then any function with a non-null xSFunc is
128047** a perfect match and any function with xSFunc NULL is
128048** a non-match.
128049*/
128050#define FUNC_PERFECT_MATCH6 6 /* The score for a perfect match */
128051static int matchQuality(
128052 FuncDef *p, /* The function we are evaluating for match quality */
128053 int nArg, /* Desired number of arguments. (-1)==any */
128054 u8 enc /* Desired text encoding */
128055){
128056 int match;
128057 assert( p->nArg>=-1 )((void) (0));
128058
128059 /* Wrong number of arguments means "no match" */
128060 if( p->nArg!=nArg ){
128061 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH6;
128062 if( p->nArg>=0 ) return 0;
128063 }
128064
128065 /* Give a better score to a function with a specific number of arguments
128066 ** than to function that accepts any number of arguments. */
128067 if( p->nArg==nArg ){
128068 match = 4;
128069 }else{
128070 match = 1;
128071 }
128072
128073 /* Bonus points if the text encoding matches */
128074 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) ){
128075 match += 2; /* Exact encoding match */
128076 }else if( (enc & p->funcFlags & 2)!=0 ){
128077 match += 1; /* Both are UTF16, but with different byte orders */
128078 }
128079
128080 return match;
128081}
128082
128083/*
128084** Search a FuncDefHash for a function with the given name. Return
128085** a pointer to the matching FuncDef if found, or 0 if there is no match.
128086*/
128087SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(
128088 int h, /* Hash of the name */
128089 const char *zFunc /* Name of function */
128090){
128091 FuncDef *p;
128092 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
128093 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
128094 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
128095 return p;
128096 }
128097 }
128098 return 0;
128099}
128100
128101/*
128102** Insert a new FuncDef into a FuncDefHash hash table.
128103*/
128104SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(
128105 FuncDef *aDef, /* List of global functions to be inserted */
128106 int nDef /* Length of the apDef[] list */
128107){
128108 int i;
128109 for(i=0; i<nDef; i++){
128110 FuncDef *pOther;
128111 const char *zName = aDef[i].zName;
128112 int nName = sqlite3Strlen30(zName);
128113 int h = SQLITE_FUNC_HASH(zName[0], nName)(((zName[0])+(nName))%23);
128114 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
128115 pOther = sqlite3FunctionSearch(h, zName);
128116 if( pOther ){
128117 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] )((void) (0));
128118 aDef[i].pNext = pOther->pNext;
128119 pOther->pNext = &aDef[i];
128120 }else{
128121 aDef[i].pNext = 0;
128122 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
128123 sqlite3BuiltinFunctions.a[h] = &aDef[i];
128124 }
128125 }
128126}
128127
128128
128129
128130/*
128131** Locate a user function given a name, a number of arguments and a flag
128132** indicating whether the function prefers UTF-16 over UTF-8. Return a
128133** pointer to the FuncDef structure that defines that function, or return
128134** NULL if the function does not exist.
128135**
128136** If the createFlag argument is true, then a new (blank) FuncDef
128137** structure is created and liked into the "db" structure if a
128138** no matching function previously existed.
128139**
128140** If nArg is -2, then the first valid function found is returned. A
128141** function is valid if xSFunc is non-zero. The nArg==(-2)
128142** case is used to see if zName is a valid function name for some number
128143** of arguments. If nArg is -2, then createFlag must be 0.
128144**
128145** If createFlag is false, then a function with the required name and
128146** number of arguments may be returned even if the eTextRep flag does not
128147** match that requested.
128148*/
128149SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(
128150 sqlite3 *db, /* An open database */
128151 const char *zName, /* Name of the function. zero-terminated */
128152 int nArg, /* Number of arguments. -1 means any number */
128153 u8 enc, /* Preferred text encoding */
128154 u8 createFlag /* Create new entry if true and does not otherwise exist */
128155){
128156 FuncDef *p; /* Iterator variable */
128157 FuncDef *pBest = 0; /* Best match found so far */
128158 int bestScore = 0; /* Score of best match */
128159 int h; /* Hash value */
128160 int nName; /* Length of the name */
128161
128162 assert( nArg>=(-2) )((void) (0));
128163 assert( nArg>=(-1) || createFlag==0 )((void) (0));
128164 nName = sqlite3Strlen30(zName);
128165
128166 /* First search for a match amongst the application-defined functions.
128167 */
128168 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
128169 while( p ){
128170 int score = matchQuality(p, nArg, enc);
128171 if( score>bestScore ){
128172 pBest = p;
128173 bestScore = score;
128174 }
128175 p = p->pNext;
128176 }
128177
128178 /* If no match is found, search the built-in functions.
128179 **
128180 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
128181 ** functions even if a prior app-defined function was found. And give
128182 ** priority to built-in functions.
128183 **
128184 ** Except, if createFlag is true, that means that we are trying to
128185 ** install a new function. Whatever FuncDef structure is returned it will
128186 ** have fields overwritten with new information appropriate for the
128187 ** new function. But the FuncDefs for built-in functions are read-only.
128188 ** So we must not search for built-ins when creating a new function.
128189 */
128190 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin0x0002)!=0) ){
128191 bestScore = 0;
128192 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName)(((sqlite3UpperToLower[(u8)zName[0]])+(nName))%23);
128193 p = sqlite3FunctionSearch(h, zName);
128194 while( p ){
128195 int score = matchQuality(p, nArg, enc);
128196 if( score>bestScore ){
128197 pBest = p;
128198 bestScore = score;
128199 }
128200 p = p->pNext;
128201 }
128202 }
128203
128204 /* If the createFlag parameter is true and the search did not reveal an
128205 ** exact match for the name, number of arguments and encoding, then add a
128206 ** new entry to the hash table and return it.
128207 */
128208 if( createFlag && bestScore<FUNC_PERFECT_MATCH6 &&
128209 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
128210 FuncDef *pOther;
128211 u8 *z;
128212 pBest->zName = (const char*)&pBest[1];
128213 pBest->nArg = (u16)nArg;
128214 pBest->funcFlags = enc;
128215 memcpy((char*)&pBest[1], zName, nName+1);
128216 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
128217 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
128218 if( pOther==pBest ){
128219 sqlite3DbFree(db, pBest);
128220 sqlite3OomFault(db);
128221 return 0;
128222 }else{
128223 pBest->pNext = pOther;
128224 }
128225 }
128226
128227 if( pBest && (pBest->xSFunc || createFlag) ){
128228 return pBest;
128229 }
128230 return 0;
128231}
128232
128233/*
128234** Free all resources held by the schema structure. The void* argument points
128235** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
128236** pointer itself, it just cleans up subsidiary resources (i.e. the contents
128237** of the schema hash tables).
128238**
128239** The Schema.cache_size variable is not cleared.
128240*/
128241SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *p){
128242 Hash temp1;
128243 Hash temp2;
128244 HashElem *pElem;
128245 Schema *pSchema = (Schema *)p;
128246 sqlite3 xdb;
128247
128248 memset(&xdb, 0, sizeof(xdb));
128249 temp1 = pSchema->tblHash;
128250 temp2 = pSchema->trigHash;
128251 sqlite3HashInit(&pSchema->trigHash);
128252 sqlite3HashClear(&pSchema->idxHash);
128253 for(pElem=sqliteHashFirst(&temp2)((&temp2)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
128254 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem)((pElem)->data));
128255 }
128256 sqlite3HashClear(&temp2);
128257 sqlite3HashInit(&pSchema->tblHash);
128258 for(pElem=sqliteHashFirst(&temp1)((&temp1)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
128259 Table *pTab = sqliteHashData(pElem)((pElem)->data);
128260 sqlite3DeleteTable(&xdb, pTab);
128261 }
128262 sqlite3HashClear(&temp1);
128263 sqlite3HashClear(&pSchema->fkeyHash);
128264 pSchema->pSeqTab = 0;
128265 if( pSchema->schemaFlags & DB_SchemaLoaded0x0001 ){
128266 pSchema->iGeneration++;
128267 }
128268 pSchema->schemaFlags &= ~(DB_SchemaLoaded0x0001|DB_ResetWanted0x0008);
128269}
128270
128271/*
128272** Find and return the schema associated with a BTree. Create
128273** a new one if necessary.
128274*/
128275SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
128276 Schema * p;
128277 if( pBt ){
128278 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
128279 }else{
128280 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
128281 }
128282 if( !p ){
128283 sqlite3OomFault(db);
128284 }else if ( 0==p->file_format ){
128285 sqlite3HashInit(&p->tblHash);
128286 sqlite3HashInit(&p->idxHash);
128287 sqlite3HashInit(&p->trigHash);
128288 sqlite3HashInit(&p->fkeyHash);
128289 p->enc = SQLITE_UTF81;
128290 }
128291 return p;
128292}
128293
128294/************** End of callback.c ********************************************/
128295/************** Begin file delete.c ******************************************/
128296/*
128297** 2001 September 15
128298**
128299** The author disclaims copyright to this source code. In place of
128300** a legal notice, here is a blessing:
128301**
128302** May you do good and not evil.
128303** May you find forgiveness for yourself and forgive others.
128304** May you share freely, never taking more than you give.
128305**
128306*************************************************************************
128307** This file contains C code routines that are called by the parser
128308** in order to generate code for DELETE FROM statements.
128309*/
128310/* #include "sqliteInt.h" */
128311
128312/*
128313** While a SrcList can in general represent multiple tables and subqueries
128314** (as in the FROM clause of a SELECT statement) in this case it contains
128315** the name of a single table, as one might find in an INSERT, DELETE,
128316** or UPDATE statement. Look up that table in the symbol table and
128317** return a pointer. Set an error message and return NULL if the table
128318** name is not found or if any other error occurs.
128319**
128320** The following fields are initialized appropriate in pSrc:
128321**
128322** pSrc->a[0].spTab Pointer to the Table object
128323** pSrc->a[0].u2.pIBIndex Pointer to the INDEXED BY index, if there is one
128324**
128325*/
128326SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
128327 SrcItem *pItem = pSrc->a;
128328 Table *pTab;
128329 assert( pItem && pSrc->nSrc>=1 )((void) (0));
128330 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
128331 if( pItem->pSTab ) sqlite3DeleteTable(pParse->db, pItem->pSTab);
128332 pItem->pSTab = pTab;
128333 pItem->fg.notCte = 1;
128334 if( pTab ){
128335 pTab->nTabRef++;
128336 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
128337 pTab = 0;
128338 }
128339 }
128340 return pTab;
128341}
128342
128343/* Generate byte-code that will report the number of rows modified
128344** by a DELETE, INSERT, or UPDATE statement.
128345*/
128346SQLITE_PRIVATEstatic void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){
128347 sqlite3VdbeAddOp0(v, OP_FkCheck83);
128348 sqlite3VdbeAddOp2(v, OP_ResultRow84, regCounter, 1);
128349 sqlite3VdbeSetNumCols(v, 1);
128350 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, zColName, SQLITE_STATIC((sqlite3_destructor_type)0));
128351}
128352
128353/* Return true if table pTab is read-only.
128354**
128355** A table is read-only if any of the following are true:
128356**
128357** 1) It is a virtual table and no implementation of the xUpdate method
128358** has been provided
128359**
128360** 2) A trigger is currently being coded and the table is a virtual table
128361** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
128362** the table is not SQLITE_VTAB_INNOCUOUS.
128363**
128364** 3) It is a system table (i.e. sqlite_schema), this call is not
128365** part of a nested parse and writable_schema pragma has not
128366** been specified
128367**
128368** 4) The table is a shadow table, the database connection is in
128369** defensive mode, and the current sqlite3_prepare()
128370** is for a top-level SQL statement.
128371*/
128372static int vtabIsReadOnly(Parse *pParse, Table *pTab){
128373 assert( IsVirtual(pTab) )((void) (0));
128374 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
128375 return 1;
128376 }
128377
128378 /* Within triggers:
128379 ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
128380 ** virtual tables
128381 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
128382 ** virtual tables if PRAGMA trusted_schema=ON.
128383 */
128384 if( pParse->pToplevel!=0
128385 && pTab->u.vtab.p->eVtabRisk >
128386 ((pParse->db->flags & SQLITE_TrustedSchema0x00000080)!=0)
128387 ){
128388 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
128389 pTab->zName);
128390 }
128391 return 0;
128392}
128393static int tabIsReadOnly(Parse *pParse, Table *pTab){
128394 sqlite3 *db;
128395 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
128396 return vtabIsReadOnly(pParse, pTab);
128397 }
128398 if( (pTab->tabFlags & (TF_Readonly0x00000001|TF_Shadow0x00001000))==0 ) return 0;
128399 db = pParse->db;
128400 if( (pTab->tabFlags & TF_Readonly0x00000001)!=0 ){
128401 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
128402 }
128403 assert( pTab->tabFlags & TF_Shadow )((void) (0));
128404 return sqlite3ReadOnlyShadowTables(db);
128405}
128406
128407/*
128408** Check to make sure the given table is writable.
128409**
128410** If pTab is not writable -> generate an error message and return 1.
128411** If pTab is writable but other errors have occurred -> return 1.
128412** If pTab is writable and no prior errors -> return 0;
128413*/
128414SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){
128415 if( tabIsReadOnly(pParse, pTab) ){
128416 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
128417 return 1;
128418 }
128419#ifndef SQLITE_OMIT_VIEW
128420 if( IsView(pTab)((pTab)->eTabType==2)
128421 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
128422 ){
128423 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
128424 return 1;
128425 }
128426#endif
128427 return 0;
128428}
128429
128430
128431#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
128432/*
128433** Evaluate a view and store its result in an ephemeral table. The
128434** pWhere argument is an optional WHERE clause that restricts the
128435** set of rows in the view that are to be added to the ephemeral table.
128436*/
128437SQLITE_PRIVATEstatic void sqlite3MaterializeView(
128438 Parse *pParse, /* Parsing context */
128439 Table *pView, /* View definition */
128440 Expr *pWhere, /* Optional WHERE clause to be added */
128441 ExprList *pOrderBy, /* Optional ORDER BY clause */
128442 Expr *pLimit, /* Optional LIMIT clause */
128443 int iCur /* Cursor number for ephemeral table */
128444){
128445 SelectDest dest;
128446 Select *pSel;
128447 SrcList *pFrom;
128448 sqlite3 *db = pParse->db;
128449 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
128450 pWhere = sqlite3ExprDup(db, pWhere, 0);
128451 pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
128452 if( pFrom ){
128453 assert( pFrom->nSrc==1 )((void) (0));
128454 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
128455 assert( pFrom->a[0].fg.fixedSchema==0 && pFrom->a[0].fg.isSubquery==0 )((void) (0));
128456 pFrom->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
128457 assert( pFrom->a[0].fg.isUsing==0 )((void) (0));
128458 assert( pFrom->a[0].u3.pOn==0 )((void) (0));
128459 }
128460 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
128461 SF_IncludeHidden0x0020000, pLimit);
128462 sqlite3SelectDestInit(&dest, SRT_EphemTab12, iCur);
128463 sqlite3Select(pParse, pSel, &dest);
128464 sqlite3SelectDelete(db, pSel);
128465}
128466#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
128467
128468#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) && !defined(SQLITE_OMIT_SUBQUERY)
128469/*
128470** Generate an expression tree to implement the WHERE, ORDER BY,
128471** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
128472**
128473** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
128474** \__________________________/
128475** pLimitWhere (pInClause)
128476*/
128477SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(
128478 Parse *pParse, /* The parser context */
128479 SrcList *pSrc, /* the FROM clause -- which tables to scan */
128480 Expr *pWhere, /* The WHERE clause. May be null */
128481 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
128482 Expr *pLimit, /* The LIMIT clause. May be null */
128483 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
128484){
128485 sqlite3 *db = pParse->db;
128486 Expr *pLhs = NULL((void*)0); /* LHS of IN(SELECT...) operator */
128487 Expr *pInClause = NULL((void*)0); /* WHERE rowid IN ( select ) */
128488 ExprList *pEList = NULL((void*)0); /* Expression list containing only pSelectRowid*/
128489 SrcList *pSelectSrc = NULL((void*)0); /* SELECT rowid FROM x ... (dup of pSrc) */
128490 Select *pSelect = NULL((void*)0); /* Complete SELECT tree */
128491 Table *pTab;
128492
128493 /* Check that there isn't an ORDER BY without a LIMIT clause.
128494 */
128495 if( pOrderBy && pLimit==0 ) {
128496 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
128497 sqlite3ExprDelete(pParse->db, pWhere);
128498 sqlite3ExprListDelete(pParse->db, pOrderBy);
128499 return 0;
128500 }
128501
128502 /* We only need to generate a select expression if there
128503 ** is a limit/offset term to enforce.
128504 */
128505 if( pLimit == 0 ) {
128506 return pWhere;
128507 }
128508
128509 /* Generate a select expression tree to enforce the limit/offset
128510 ** term for the DELETE or UPDATE statement. For example:
128511 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
128512 ** becomes:
128513 ** DELETE FROM table_a WHERE rowid IN (
128514 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
128515 ** );
128516 */
128517
128518 pTab = pSrc->a[0].pSTab;
128519 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128520 pLhs = sqlite3PExpr(pParse, TK_ROW76, 0, 0);
128521 pEList = sqlite3ExprListAppend(
128522 pParse, 0, sqlite3PExpr(pParse, TK_ROW76, 0, 0)
128523 );
128524 }else{
128525 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
128526 assert( pPk!=0 )((void) (0));
128527 assert( pPk->nKeyCol>=1 )((void) (0));
128528 if( pPk->nKeyCol==1 ){
128529 const char *zName;
128530 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol )((void) (0));
128531 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
128532 pLhs = sqlite3Expr(db, TK_ID60, zName);
128533 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID60, zName));
128534 }else{
128535 int i;
128536 for(i=0; i<pPk->nKeyCol; i++){
128537 Expr *p;
128538 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol )((void) (0));
128539 p = sqlite3Expr(db, TK_ID60, pTab->aCol[pPk->aiColumn[i]].zCnName);
128540 pEList = sqlite3ExprListAppend(pParse, pEList, p);
128541 }
128542 pLhs = sqlite3PExpr(pParse, TK_VECTOR177, 0, 0);
128543 if( pLhs ){
128544 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
128545 }
128546 }
128547 }
128548
128549 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
128550 ** and the SELECT subtree. */
128551 pSrc->a[0].pSTab = 0;
128552 pSelectSrc = sqlite3SrcListDup(db, pSrc, 0);
128553 pSrc->a[0].pSTab = pTab;
128554 if( pSrc->a[0].fg.isIndexedBy ){
128555 assert( pSrc->a[0].fg.isCte==0 )((void) (0));
128556 pSrc->a[0].u2.pIBIndex = 0;
128557 pSrc->a[0].fg.isIndexedBy = 0;
128558 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
128559 }else if( pSrc->a[0].fg.isCte ){
128560 pSrc->a[0].u2.pCteUse->nUse++;
128561 }
128562
128563 /* generate the SELECT expression tree. */
128564 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
128565 pOrderBy,0,pLimit
128566 );
128567
128568 /* now generate the new WHERE rowid IN clause for the DELETE/UPDATE */
128569 pInClause = sqlite3PExpr(pParse, TK_IN50, pLhs, 0);
128570 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
128571 return pInClause;
128572}
128573#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
128574 /* && !defined(SQLITE_OMIT_SUBQUERY) */
128575
128576/*
128577** Generate code for a DELETE FROM statement.
128578**
128579** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
128580** \________/ \________________/
128581** pTabList pWhere
128582*/
128583SQLITE_PRIVATEstatic void sqlite3DeleteFrom(
128584 Parse *pParse, /* The parser context */
128585 SrcList *pTabList, /* The table from which we should delete things */
128586 Expr *pWhere, /* The WHERE clause. May be null */
128587 ExprList *pOrderBy, /* ORDER BY clause. May be null */
128588 Expr *pLimit /* LIMIT clause. May be null */
128589){
128590 Vdbe *v; /* The virtual database engine */
128591 Table *pTab; /* The table from which records will be deleted */
128592 int i; /* Loop counter */
128593 WhereInfo *pWInfo; /* Information about the WHERE clause */
128594 Index *pIdx; /* For looping over indices of the table */
128595 int iTabCur; /* Cursor number for the table */
128596 int iDataCur = 0; /* VDBE cursor for the canonical data source */
128597 int iIdxCur = 0; /* Cursor number of the first index */
128598 int nIdx; /* Number of indices */
128599 sqlite3 *db; /* Main database structure */
128600 AuthContext sContext; /* Authorization context */
128601 NameContext sNC; /* Name context to resolve expressions in */
128602 int iDb; /* Database number */
128603 int memCnt = 0; /* Memory cell used for change counting */
128604 int rcauth; /* Value returned by authorization callback */
128605 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
128606 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
128607 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
128608 Index *pPk; /* The PRIMARY KEY index on the table */
128609 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
128610 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
128611 int iKey; /* Memory cell holding key of row to be deleted */
128612 i16 nKey; /* Number of memory cells in the row key */
128613 int iEphCur = 0; /* Ephemeral table holding all primary key values */
128614 int iRowSet = 0; /* Register for rowset of rows to delete */
128615 int addrBypass = 0; /* Address of jump over the delete logic */
128616 int addrLoop = 0; /* Top of the delete loop */
128617 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
128618 int bComplex; /* True if there are triggers or FKs or
128619 ** subqueries in the WHERE clause */
128620
128621#ifndef SQLITE_OMIT_TRIGGER
128622 int isView; /* True if attempting to delete from a view */
128623 Trigger *pTrigger; /* List of table triggers, if required */
128624#endif
128625
128626 memset(&sContext, 0, sizeof(sContext));
128627 db = pParse->db;
128628 assert( db->pParse==pParse )((void) (0));
128629 if( pParse->nErr ){
128630 goto delete_from_cleanup;
128631 }
128632 assert( db->mallocFailed==0 )((void) (0));
128633 assert( pTabList->nSrc==1 )((void) (0));
128634
128635 /* Locate the table which we want to delete. This table has to be
128636 ** put in an SrcList structure because some of the subroutines we
128637 ** will be calling are designed to work with multiple tables and expect
128638 ** an SrcList* parameter instead of just a Table* parameter.
128639 */
128640 pTab = sqlite3SrcListLookup(pParse, pTabList);
128641 if( pTab==0 ) goto delete_from_cleanup;
128642
128643 /* Figure out if we have any triggers and if the table being
128644 ** deleted from is a view
128645 */
128646#ifndef SQLITE_OMIT_TRIGGER
128647 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE129, 0, 0);
128648 isView = IsView(pTab)((pTab)->eTabType==2);
128649#else
128650# define pTrigger 0
128651# define isView 0
128652#endif
128653 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
128654#ifdef SQLITE_OMIT_VIEW
128655# undef isView
128656# define isView 0
128657#endif
128658
128659#if TREETRACE_ENABLED0
128660 if( sqlite3TreeTrace & 0x10000 ){
128661 sqlite3TreeViewLine(0, "In sqlite3Delete() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__128661);
128662 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
128663 pOrderBy, pLimit, pTrigger);
128664 }
128665#endif
128666
128667#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
128668 if( !isView ){
128669 pWhere = sqlite3LimitWhere(
128670 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
128671 );
128672 pOrderBy = 0;
128673 pLimit = 0;
128674 }
128675#endif
128676
128677 /* If pTab is really a view, make sure it has been initialized.
128678 */
128679 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
128680 goto delete_from_cleanup;
128681 }
128682
128683 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
128684 goto delete_from_cleanup;
128685 }
128686 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128687 assert( iDb<db->nDb )((void) (0));
128688 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0,
128689 db->aDb[iDb].zDbSName);
128690 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE )((void) (0));
128691 if( rcauth==SQLITE_DENY1 ){
128692 goto delete_from_cleanup;
128693 }
128694 assert(!isView || pTrigger)((void) (0));
128695
128696 /* Assign cursor numbers to the table and all its indices.
128697 */
128698 assert( pTabList->nSrc==1 )((void) (0));
128699 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
128700 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
128701 pParse->nTab++;
128702 }
128703
128704 /* Start the view context
128705 */
128706 if( isView ){
128707 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
128708 }
128709
128710 /* Begin generating code.
128711 */
128712 v = sqlite3GetVdbe(pParse);
128713 if( v==0 ){
128714 goto delete_from_cleanup;
128715 }
128716 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
128717 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
128718
128719 /* If we are trying to delete from a view, realize that view into
128720 ** an ephemeral table.
128721 */
128722#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
128723 if( isView ){
128724 sqlite3MaterializeView(pParse, pTab,
128725 pWhere, pOrderBy, pLimit, iTabCur
128726 );
128727 iDataCur = iIdxCur = iTabCur;
128728 pOrderBy = 0;
128729 pLimit = 0;
128730 }
128731#endif
128732
128733 /* Resolve the column names in the WHERE clause.
128734 */
128735 memset(&sNC, 0, sizeof(sNC));
128736 sNC.pParse = pParse;
128737 sNC.pSrcList = pTabList;
128738 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
128739 goto delete_from_cleanup;
128740 }
128741
128742 /* Initialize the counter of the number of rows deleted, if
128743 ** we are counting rows.
128744 */
128745 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0
128746 && !pParse->nested
128747 && !pParse->pTriggerTab
128748 && !pParse->bReturning
128749 ){
128750 memCnt = ++pParse->nMem;
128751 sqlite3VdbeAddOp2(v, OP_Integer71, 0, memCnt);
128752 }
128753
128754#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
128755 /* Special case: A DELETE without a WHERE clause deletes everything.
128756 ** It is easier just to erase the whole table. Prior to version 3.6.5,
128757 ** this optimization caused the row change count (the value returned by
128758 ** API function sqlite3_count_changes) to be set incorrectly.
128759 **
128760 ** The "rcauth==SQLITE_OK" terms is the
128761 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
128762 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
128763 ** the truncate optimization is disabled and all rows are deleted
128764 ** individually.
128765 */
128766 if( rcauth==SQLITE_OK0
128767 && pWhere==0
128768 && !bComplex
128769 && !IsVirtual(pTab)((pTab)->eTabType==1)
128770#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
128771 && db->xPreUpdateCallback==0
128772#endif
128773 ){
128774 assert( !isView )((void) (0));
128775 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
128776 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128777 sqlite3VdbeAddOp4(v, OP_Clear145, pTab->tnum, iDb, memCnt ? memCnt : -1,
128778 pTab->zName, P4_STATIC(-1));
128779 }
128780 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
128781 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
128782 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128783 sqlite3VdbeAddOp3(v, OP_Clear145, pIdx->tnum, iDb, memCnt ? memCnt : -1);
128784 }else{
128785 sqlite3VdbeAddOp2(v, OP_Clear145, pIdx->tnum, iDb);
128786 }
128787 }
128788 }else
128789#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
128790 {
128791 u16 wcf = WHERE_ONEPASS_DESIRED0x0004|WHERE_DUPLICATES_OK0x0010;
128792 if( sNC.ncFlags & NC_Subquery0x000040 ) bComplex = 1;
128793 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW0x0008);
128794 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
128795 /* For a rowid table, initialize the RowSet to an empty set */
128796 pPk = 0;
128797 assert( nPk==1 )((void) (0));
128798 iRowSet = ++pParse->nMem;
128799 sqlite3VdbeAddOp2(v, OP_Null75, 0, iRowSet);
128800 }else{
128801 /* For a WITHOUT ROWID table, create an ephemeral table used to
128802 ** hold all primary keys for rows to be deleted. */
128803 pPk = sqlite3PrimaryKeyIndex(pTab);
128804 assert( pPk!=0 )((void) (0));
128805 nPk = pPk->nKeyCol;
128806 iPk = pParse->nMem+1;
128807 pParse->nMem += nPk;
128808 iEphCur = pParse->nTab++;
128809 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iEphCur, nPk);
128810 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
128811 }
128812
128813 /* Construct a query to find the rowid or primary key for every row
128814 ** to be deleted, based on the WHERE clause. Set variable eOnePass
128815 ** to indicate the strategy used to implement this delete:
128816 **
128817 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
128818 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
128819 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
128820 */
128821 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
128822 if( pWInfo==0 ) goto delete_from_cleanup;
128823 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
128824 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI )((void) (0));
128825 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF((void) (0))
128826 || OptimizationDisabled(db, SQLITE_OnePass) )((void) (0));
128827 if( eOnePass!=ONEPASS_SINGLE1 ) sqlite3MultiWrite(pParse);
128828 if( sqlite3WhereUsesDeferredSeek(pWInfo) ){
128829 sqlite3VdbeAddOp1(v, OP_FinishSeek143, iTabCur);
128830 }
128831
128832 /* Keep track of the number of rows to be deleted */
128833 if( memCnt ){
128834 sqlite3VdbeAddOp2(v, OP_AddImm86, memCnt, 1);
128835 }
128836
128837 /* Extract the rowid or primary key for the current row */
128838 if( pPk ){
128839 for(i=0; i<nPk; i++){
128840 assert( pPk->aiColumn[i]>=0 )((void) (0));
128841 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
128842 pPk->aiColumn[i], iPk+i);
128843 }
128844 iKey = iPk;
128845 }else{
128846 iKey = ++pParse->nMem;
128847 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
128848 }
128849
128850 if( eOnePass!=ONEPASS_OFF0 ){
128851 /* For ONEPASS, no need to store the rowid/primary-key. There is only
128852 ** one, so just keep it in its register(s) and fall through to the
128853 ** delete code. */
128854 nKey = nPk; /* OP_Found will use an unpacked key */
128855 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
128856 if( aToOpen==0 ){
128857 sqlite3WhereEnd(pWInfo);
128858 goto delete_from_cleanup;
128859 }
128860 memset(aToOpen, 1, nIdx+1);
128861 aToOpen[nIdx+1] = 0;
128862 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
128863 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
128864 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
128865 addrBypass = sqlite3VdbeMakeLabel(pParse);
128866 }else{
128867 if( pPk ){
128868 /* Add the PK key for this row to the temporary table */
128869 iKey = ++pParse->nMem;
128870 nKey = 0; /* Zero tells OP_Found to use a composite key */
128871 sqlite3VdbeAddOp4(v, OP_MakeRecord97, iPk, nPk, iKey,
128872 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
128873 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iEphCur, iKey, iPk, nPk);
128874 }else{
128875 /* Add the rowid of the row to be deleted to the RowSet */
128876 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
128877 sqlite3VdbeAddOp2(v, OP_RowSetAdd156, iRowSet, iKey);
128878 }
128879 sqlite3WhereEnd(pWInfo);
128880 }
128881
128882 /* Unless this is a view, open cursors for the table we are
128883 ** deleting from and all its indices. If this is a view, then the
128884 ** only effect this statement has is to fire the INSTEAD OF
128885 ** triggers.
128886 */
128887 if( !isView ){
128888 int iAddrOnce = 0;
128889 if( eOnePass==ONEPASS_MULTI2 ){
128890 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
128891 }
128892 testcase( IsVirtual(pTab) );
128893 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, OPFLAG_FORDELETE0x08,
128894 iTabCur, aToOpen, &iDataCur, &iIdxCur);
128895 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur )((void) (0));
128896 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 )((void) (0));
128897 if( eOnePass==ONEPASS_MULTI2 ){
128898 sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce);
128899 }
128900 }
128901
128902 /* Set up a loop over the rowids/primary-keys that were found in the
128903 ** where-clause loop above.
128904 */
128905 if( eOnePass!=ONEPASS_OFF0 ){
128906 assert( nKey==nPk )((void) (0)); /* OP_Found will use an unpacked key */
128907 if( !IsVirtual(pTab)((pTab)->eTabType==1) && aToOpen[iDataCur-iTabCur] ){
128908 assert( pPk!=0 || IsView(pTab) )((void) (0));
128909 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, addrBypass, iKey, nKey);
128910 VdbeCoverage(v);
128911 }
128912 }else if( pPk ){
128913 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind36, iEphCur); VdbeCoverage(v);
128914 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
128915 sqlite3VdbeAddOp3(v, OP_Column94, iEphCur, 0, iKey);
128916 }else{
128917 sqlite3VdbeAddOp2(v, OP_RowData134, iEphCur, iKey);
128918 }
128919 assert( nKey==0 )((void) (0)); /* OP_Found will use a composite key */
128920 }else{
128921 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead46, iRowSet, 0, iKey);
128922 VdbeCoverage(v);
128923 assert( nKey==1 )((void) (0));
128924 }
128925
128926 /* Delete the row */
128927#ifndef SQLITE_OMIT_VIRTUALTABLE
128928 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
128929 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
128930 sqlite3VtabMakeWritable(pParse, pTab);
128931 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
128932 sqlite3MayAbort(pParse);
128933 if( eOnePass==ONEPASS_SINGLE1 ){
128934 sqlite3VdbeAddOp1(v, OP_Close122, iTabCur);
128935 if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
128936 pParse->isMultiWrite = 0;
128937 }
128938 }
128939 sqlite3VdbeAddOp4(v, OP_VUpdate7, 0, 1, iKey, pVTab, P4_VTAB(-11));
128940 sqlite3VdbeChangeP5(v, OE_Abort2);
128941 }else
128942#endif
128943 {
128944 int count = (pParse->nested==0); /* True to count changes */
128945 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
128946 iKey, nKey, count, OE_Default11, eOnePass, aiCurOnePass[1]);
128947 }
128948
128949 /* End of the loop over all rowids/primary-keys. */
128950 if( eOnePass!=ONEPASS_OFF0 ){
128951 sqlite3VdbeResolveLabel(v, addrBypass);
128952 sqlite3WhereEnd(pWInfo);
128953 }else if( pPk ){
128954 sqlite3VdbeAddOp2(v, OP_Next39, iEphCur, addrLoop+1); VdbeCoverage(v);
128955 sqlite3VdbeJumpHere(v, addrLoop);
128956 }else{
128957 sqlite3VdbeGoto(v, addrLoop);
128958 sqlite3VdbeJumpHere(v, addrLoop);
128959 }
128960 } /* End non-truncate path */
128961
128962 /* Update the sqlite_sequence table by storing the content of the
128963 ** maximum rowid counter values recorded while inserting into
128964 ** autoincrement tables.
128965 */
128966 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
128967 sqlite3AutoincrementEnd(pParse);
128968 }
128969
128970 /* Return the number of rows that were deleted. If this routine is
128971 ** generating code because of a call to sqlite3NestedParse(), do not
128972 ** invoke the callback function.
128973 */
128974 if( memCnt ){
128975 sqlite3CodeChangeCount(v, memCnt, "rows deleted");
128976 }
128977
128978delete_from_cleanup:
128979 sqlite3AuthContextPop(&sContext);
128980 sqlite3SrcListDelete(db, pTabList);
128981 sqlite3ExprDelete(db, pWhere);
128982#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1)
128983 sqlite3ExprListDelete(db, pOrderBy);
128984 sqlite3ExprDelete(db, pLimit);
128985#endif
128986 if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen);
128987 return;
128988}
128989/* Make sure "isView" and other macros defined above are undefined. Otherwise
128990** they may interfere with compilation of other functions in this file
128991** (or in another file, if this file becomes part of the amalgamation). */
128992#ifdef isView
128993 #undef isView
128994#endif
128995#ifdef pTrigger
128996 #undef pTrigger
128997#endif
128998
128999/*
129000** This routine generates VDBE code that causes a single row of a
129001** single table to be deleted. Both the original table entry and
129002** all indices are removed.
129003**
129004** Preconditions:
129005**
129006** 1. iDataCur is an open cursor on the btree that is the canonical data
129007** store for the table. (This will be either the table itself,
129008** in the case of a rowid table, or the PRIMARY KEY index in the case
129009** of a WITHOUT ROWID table.)
129010**
129011** 2. Read/write cursors for all indices of pTab must be open as
129012** cursor number iIdxCur+i for the i-th index.
129013**
129014** 3. The primary key for the row to be deleted must be stored in a
129015** sequence of nPk memory cells starting at iPk. If nPk==0 that means
129016** that a search record formed from OP_MakeRecord is contained in the
129017** single memory location iPk.
129018**
129019** eMode:
129020** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
129021** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
129022** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
129023** then this function must seek iDataCur to the entry identified by iPk
129024** and nPk before reading from it.
129025**
129026** If eMode is ONEPASS_MULTI, then this call is being made as part
129027** of a ONEPASS delete that affects multiple rows. In this case, if
129028** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
129029** iDataCur, then its position should be preserved following the delete
129030** operation. Or, if iIdxNoSeek is not a valid cursor number, the
129031** position of iDataCur should be preserved instead.
129032**
129033** iIdxNoSeek:
129034** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
129035** then it identifies an index cursor (from within array of cursors
129036** starting at iIdxCur) that already points to the index entry to be deleted.
129037** Except, this optimization is disabled if there are BEFORE triggers since
129038** the trigger body might have moved the cursor.
129039*/
129040SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
129041 Parse *pParse, /* Parsing context */
129042 Table *pTab, /* Table containing the row to be deleted */
129043 Trigger *pTrigger, /* List of triggers to (potentially) fire */
129044 int iDataCur, /* Cursor from which column data is extracted */
129045 int iIdxCur, /* First index cursor */
129046 int iPk, /* First memory cell containing the PRIMARY KEY */
129047 i16 nPk, /* Number of PRIMARY KEY memory cells */
129048 u8 count, /* If non-zero, increment the row change counter */
129049 u8 onconf, /* Default ON CONFLICT policy for triggers */
129050 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
129051 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
129052){
129053 Vdbe *v = pParse->pVdbe; /* Vdbe */
129054 int iOld = 0; /* First register in OLD.* array */
129055 int iLabel; /* Label resolved to end of generated code */
129056 u8 opSeek; /* Seek opcode */
129057
129058 /* Vdbe is guaranteed to have been allocated by this stage. */
129059 assert( v )((void) (0));
129060 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
129061 iDataCur, iIdxCur, iPk, (int)nPk));
129062
129063 /* Seek cursor iCur to the row to delete. If this row no longer exists
129064 ** (this can happen if a trigger program has already deleted it), do
129065 ** not attempt to delete it or fire any DELETE triggers. */
129066 iLabel = sqlite3VdbeMakeLabel(pParse);
129067 opSeek = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? OP_NotExists31 : OP_NotFound28;
129068 if( eMode==ONEPASS_OFF0 ){
129069 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
129070 VdbeCoverageIf(v, opSeek==OP_NotExists);
129071 VdbeCoverageIf(v, opSeek==OP_NotFound);
129072 }
129073
129074 /* If there are any triggers to fire, allocate a range of registers to
129075 ** use for the old.* references in the triggers. */
129076 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
129077 u32 mask; /* Mask of OLD.* columns in use */
129078 int iCol; /* Iterator used while populating OLD.* */
129079 int addrStart; /* Start of BEFORE trigger programs */
129080
129081 /* TODO: Could use temporary registers here. Also could attempt to
129082 ** avoid copying the contents of the rowid register. */
129083 mask = sqlite3TriggerColmask(
129084 pParse, pTrigger, 0, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onconf
129085 );
129086 mask |= sqlite3FkOldmask(pParse, pTab);
129087 iOld = pParse->nMem+1;
129088 pParse->nMem += (1 + pTab->nCol);
129089
129090 /* Populate the OLD.* pseudo-table register array. These values will be
129091 ** used by any BEFORE and AFTER triggers that exist. */
129092 sqlite3VdbeAddOp2(v, OP_Copy80, iPk, iOld);
129093 for(iCol=0; iCol<pTab->nCol; iCol++){
129094 testcase( mask!=0xffffffff && iCol==31 );
129095 testcase( mask!=0xffffffff && iCol==32 );
129096 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol)(((unsigned int)1)<<(iCol)))!=0) ){
129097 int kk = sqlite3TableColumnToStorage(pTab, iCol);
129098 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1);
129099 }
129100 }
129101
129102 /* Invoke BEFORE DELETE trigger programs. */
129103 addrStart = sqlite3VdbeCurrentAddr(v);
129104 sqlite3CodeRowTrigger(pParse, pTrigger,
129105 TK_DELETE129, 0, TRIGGER_BEFORE1, pTab, iOld, onconf, iLabel
129106 );
129107
129108 /* If any BEFORE triggers were coded, then seek the cursor to the
129109 ** row to be deleted again. It may be that the BEFORE triggers moved
129110 ** the cursor or already deleted the row that the cursor was
129111 ** pointing to.
129112 **
129113 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
129114 ** may have moved that cursor.
129115 */
129116 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
129117 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
129118 VdbeCoverageIf(v, opSeek==OP_NotExists);
129119 VdbeCoverageIf(v, opSeek==OP_NotFound);
129120 testcase( iIdxNoSeek>=0 );
129121 iIdxNoSeek = -1;
129122 }
129123
129124 /* Do FK processing. This call checks that any FK constraints that
129125 ** refer to this table (i.e. constraints attached to other tables)
129126 ** are not violated by deleting this row. */
129127 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
129128 }
129129
129130 /* Delete the index and table entries. Skip this step if pTab is really
129131 ** a view (in which case the only effect of the DELETE statement is to
129132 ** fire the INSTEAD OF triggers).
129133 **
129134 ** If variable 'count' is non-zero, then this OP_Delete instruction should
129135 ** invoke the update-hook. The pre-update-hook, on the other hand should
129136 ** be invoked unless table pTab is a system table. The difference is that
129137 ** the update-hook is not invoked for rows removed by REPLACE, but the
129138 ** pre-update-hook is.
129139 */
129140 if( !IsView(pTab)((pTab)->eTabType==2) ){
129141 u8 p5 = 0;
129142 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
129143 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, (count?OPFLAG_NCHANGE0x01:0));
129144 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
129145 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE(-5));
129146 }
129147 if( eMode!=ONEPASS_OFF0 ){
129148 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE0x04);
129149 }
129150 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
129151 sqlite3VdbeAddOp1(v, OP_Delete130, iIdxNoSeek);
129152 }
129153 if( eMode==ONEPASS_MULTI2 ) p5 |= OPFLAG_SAVEPOSITION0x02;
129154 sqlite3VdbeChangeP5(v, p5);
129155 }
129156
129157 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
129158 ** handle rows (possibly in other tables) that refer via a foreign key
129159 ** to the row just deleted. */
129160 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
129161
129162 /* Invoke AFTER DELETE trigger programs. */
129163 if( pTrigger ){
129164 sqlite3CodeRowTrigger(pParse, pTrigger,
129165 TK_DELETE129, 0, TRIGGER_AFTER2, pTab, iOld, onconf, iLabel
129166 );
129167 }
129168
129169 /* Jump here if the row had already been deleted before any BEFORE
129170 ** trigger programs were invoked. Or if a trigger program throws a
129171 ** RAISE(IGNORE) exception. */
129172 sqlite3VdbeResolveLabel(v, iLabel);
129173 VdbeModuleComment((v, "END: GenRowDel()"));
129174}
129175
129176/*
129177** This routine generates VDBE code that causes the deletion of all
129178** index entries associated with a single row of a single table, pTab
129179**
129180** Preconditions:
129181**
129182** 1. A read/write cursor "iDataCur" must be open on the canonical storage
129183** btree for the table pTab. (This will be either the table itself
129184** for rowid tables or to the primary key index for WITHOUT ROWID
129185** tables.)
129186**
129187** 2. Read/write cursors for all indices of pTab must be open as
129188** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
129189** index is the 0-th index.)
129190**
129191** 3. The "iDataCur" cursor must be already be positioned on the row
129192** that is to be deleted.
129193*/
129194SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(
129195 Parse *pParse, /* Parsing and code generating context */
129196 Table *pTab, /* Table containing the row to be deleted */
129197 int iDataCur, /* Cursor of table holding data. */
129198 int iIdxCur, /* First index cursor */
129199 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
129200 int iIdxNoSeek /* Do not delete from this cursor */
129201){
129202 int i; /* Index loop counter */
129203 int r1 = -1; /* Register holding an index key */
129204 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
129205 Index *pIdx; /* Current index */
129206 Index *pPrior = 0; /* Prior index */
129207 Vdbe *v; /* The prepared statement under construction */
129208 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
129209
129210 v = pParse->pVdbe;
129211 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
129212 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
129213 assert( iIdxCur+i!=iDataCur || pPk==pIdx )((void) (0));
129214 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
129215 if( pIdx==pPk ) continue;
129216 if( iIdxCur+i==iIdxNoSeek ) continue;
129217 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
129218 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
129219 &iPartIdxLabel, pPrior, r1);
129220 sqlite3VdbeAddOp3(v, OP_IdxDelete140, iIdxCur+i, r1,
129221 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
129222 sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */
129223 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
129224 pPrior = pIdx;
129225 }
129226}
129227
129228/*
129229** Generate code that will assemble an index key and stores it in register
129230** regOut. The key with be for index pIdx which is an index on pTab.
129231** iCur is the index of a cursor open on the pTab table and pointing to
129232** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
129233** iCur must be the cursor of the PRIMARY KEY index.
129234**
129235** Return a register number which is the first in a block of
129236** registers that holds the elements of the index key. The
129237** block of registers has already been deallocated by the time
129238** this routine returns.
129239**
129240** If *piPartIdxLabel is not NULL, fill it in with a label and jump
129241** to that label if pIdx is a partial index that should be skipped.
129242** The label should be resolved using sqlite3ResolvePartIdxLabel().
129243** A partial index should be skipped if its WHERE clause evaluates
129244** to false or null. If pIdx is not a partial index, *piPartIdxLabel
129245** will be set to zero which is an empty label that is ignored by
129246** sqlite3ResolvePartIdxLabel().
129247**
129248** The pPrior and regPrior parameters are used to implement a cache to
129249** avoid unnecessary register loads. If pPrior is not NULL, then it is
129250** a pointer to a different index for which an index key has just been
129251** computed into register regPrior. If the current pIdx index is generating
129252** its key into the same sequence of registers and if pPrior and pIdx share
129253** a column in common, then the register corresponding to that column already
129254** holds the correct value and the loading of that register is skipped.
129255** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
129256** on a table with multiple indices, and especially with the ROWID or
129257** PRIMARY KEY columns of the index.
129258*/
129259SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(
129260 Parse *pParse, /* Parsing context */
129261 Index *pIdx, /* The index for which to generate a key */
129262 int iDataCur, /* Cursor number from which to take column data */
129263 int regOut, /* Put the new key into this register if not 0 */
129264 int prefixOnly, /* Compute only a unique prefix of the key */
129265 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
129266 Index *pPrior, /* Previously generated index key */
129267 int regPrior /* Register holding previous generated key */
129268){
129269 Vdbe *v = pParse->pVdbe;
129270 int j;
129271 int regBase;
129272 int nCol;
129273
129274 if( piPartIdxLabel ){
129275 if( pIdx->pPartIdxWhere ){
129276 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
129277 pParse->iSelfTab = iDataCur + 1;
129278 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
129279 SQLITE_JUMPIFNULL0x10);
129280 pParse->iSelfTab = 0;
129281 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
129282 ** pPartIdxWhere may have corrupted regPrior registers */
129283 }else{
129284 *piPartIdxLabel = 0;
129285 }
129286 }
129287 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
129288 regBase = sqlite3GetTempRange(pParse, nCol);
129289 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
129290 for(j=0; j<nCol; j++){
129291 if( pPrior
129292 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
129293 && pPrior->aiColumn[j]!=XN_EXPR(-2)
129294 ){
129295 /* This column was already computed by the previous index */
129296 continue;
129297 }
129298 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
129299 if( pIdx->aiColumn[j]>=0 ){
129300 /* If the column affinity is REAL but the number is an integer, then it
129301 ** might be stored in the table as an integer (using a compact
129302 ** representation) then converted to REAL by an OP_RealAffinity opcode.
129303 ** But we are getting ready to store this value back into an index, where
129304 ** it should be converted by to INTEGER again. So omit the
129305 ** OP_RealAffinity opcode if it is present */
129306 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity87);
129307 }
129308 }
129309 if( regOut ){
129310 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase, nCol, regOut);
129311 }
129312 sqlite3ReleaseTempRange(pParse, regBase, nCol);
129313 return regBase;
129314}
129315
129316/*
129317** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
129318** because it was a partial index, then this routine should be called to
129319** resolve that label.
129320*/
129321SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
129322 if( iLabel ){
129323 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
129324 }
129325}
129326
129327/************** End of delete.c **********************************************/
129328/************** Begin file func.c ********************************************/
129329/*
129330** 2002 February 23
129331**
129332** The author disclaims copyright to this source code. In place of
129333** a legal notice, here is a blessing:
129334**
129335** May you do good and not evil.
129336** May you find forgiveness for yourself and forgive others.
129337** May you share freely, never taking more than you give.
129338**
129339*************************************************************************
129340** This file contains the C-language implementations for many of the SQL
129341** functions of SQLite. (Some function, and in particular the date and
129342** time functions, are implemented separately.)
129343*/
129344/* #include "sqliteInt.h" */
129345/* #include <stdlib.h> */
129346/* #include <assert.h> */
129347#ifndef SQLITE_OMIT_FLOATING_POINT
129348/* #include <math.h> */
129349#endif
129350/* #include "vdbeInt.h" */
129351
129352/*
129353** Return the collating function associated with a function.
129354*/
129355static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
129356 VdbeOp *pOp;
129357 assert( context->pVdbe!=0 )((void) (0));
129358 pOp = &context->pVdbe->aOp[context->iOp-1];
129359 assert( pOp->opcode==OP_CollSeq )((void) (0));
129360 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
129361 return pOp->p4.pColl;
129362}
129363
129364/*
129365** Indicate that the accumulator load should be skipped on this
129366** iteration of the aggregate loop.
129367*/
129368static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
129369 assert( context->isError<=0 )((void) (0));
129370 context->isError = -1;
129371 context->skipFlag = 1;
129372}
129373
129374/*
129375** Implementation of the non-aggregate min() and max() functions
129376*/
129377static void minmaxFunc(
129378 sqlite3_context *context,
129379 int argc,
129380 sqlite3_value **argv
129381){
129382 int i;
129383 int mask; /* 0 for min() or 0xffffffff for max() */
129384 int iBest;
129385 CollSeq *pColl;
129386
129387 assert( argc>1 )((void) (0));
129388 mask = sqlite3_user_data(context)==0 ? 0 : -1;
129389 pColl = sqlite3GetFuncCollSeq(context);
129390 assert( pColl )((void) (0));
129391 assert( mask==-1 || mask==0 )((void) (0));
129392 iBest = 0;
129393 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
129394 for(i=1; i<argc; i++){
129395 if( sqlite3_value_type(argv[i])==SQLITE_NULL5 ) return;
129396 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
129397 testcase( mask==0 );
129398 iBest = i;
129399 }
129400 }
129401 sqlite3_result_value(context, argv[iBest]);
129402}
129403
129404/*
129405** Return the type of the argument.
129406*/
129407static void typeofFunc(
129408 sqlite3_context *context,
129409 int NotUsed,
129410 sqlite3_value **argv
129411){
129412 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
129413 int i = sqlite3_value_type(argv[0]) - 1;
129414 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
129415 assert( i>=0 && i<ArraySize(azType) )((void) (0));
129416 assert( SQLITE_INTEGER==1 )((void) (0));
129417 assert( SQLITE_FLOAT==2 )((void) (0));
129418 assert( SQLITE_TEXT==3 )((void) (0));
129419 assert( SQLITE_BLOB==4 )((void) (0));
129420 assert( SQLITE_NULL==5 )((void) (0));
129421 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
129422 ** the datatype code for the initial datatype of the sqlite3_value object
129423 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
129424 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
129425 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
129426}
129427
129428/* subtype(X)
129429**
129430** Return the subtype of X
129431*/
129432static void subtypeFunc(
129433 sqlite3_context *context,
129434 int argc,
129435 sqlite3_value **argv
129436){
129437 UNUSED_PARAMETER(argc)(void)(argc);
129438 sqlite3_result_int(context, sqlite3_value_subtype(argv[0]));
129439}
129440
129441/*
129442** Implementation of the length() function
129443*/
129444static void lengthFunc(
129445 sqlite3_context *context,
129446 int argc,
129447 sqlite3_value **argv
129448){
129449 assert( argc==1 )((void) (0));
129450 UNUSED_PARAMETER(argc)(void)(argc);
129451 switch( sqlite3_value_type(argv[0]) ){
129452 case SQLITE_BLOB4:
129453 case SQLITE_INTEGER1:
129454 case SQLITE_FLOAT2: {
129455 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
129456 break;
129457 }
129458 case SQLITE_TEXT3: {
129459 const unsigned char *z = sqlite3_value_text(argv[0]);
129460 const unsigned char *z0;
129461 unsigned char c;
129462 if( z==0 ) return;
129463 z0 = z;
129464 while( (c = *z)!=0 ){
129465 z++;
129466 if( c>=0xc0 ){
129467 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
129468 }
129469 }
129470 sqlite3_result_int(context, (int)(z-z0));
129471 break;
129472 }
129473 default: {
129474 sqlite3_result_null(context);
129475 break;
129476 }
129477 }
129478}
129479
129480/*
129481** Implementation of the octet_length() function
129482*/
129483static void bytelengthFunc(
129484 sqlite3_context *context,
129485 int argc,
129486 sqlite3_value **argv
129487){
129488 assert( argc==1 )((void) (0));
129489 UNUSED_PARAMETER(argc)(void)(argc);
129490 switch( sqlite3_value_type(argv[0]) ){
129491 case SQLITE_BLOB4: {
129492 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
129493 break;
129494 }
129495 case SQLITE_INTEGER1:
129496 case SQLITE_FLOAT2: {
129497 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF81 ? 1 : 2;
129498 sqlite3_result_int64(context, sqlite3_value_bytes(argv[0])*m);
129499 break;
129500 }
129501 case SQLITE_TEXT3: {
129502 if( sqlite3_value_encoding(argv[0])<=SQLITE_UTF81 ){
129503 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
129504 }else{
129505 sqlite3_result_int(context, sqlite3_value_bytes16(argv[0]));
129506 }
129507 break;
129508 }
129509 default: {
129510 sqlite3_result_null(context);
129511 break;
129512 }
129513 }
129514}
129515
129516/*
129517** Implementation of the abs() function.
129518**
129519** IMP: R-23979-26855 The abs(X) function returns the absolute value of
129520** the numeric argument X.
129521*/
129522static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129523 assert( argc==1 )((void) (0));
129524 UNUSED_PARAMETER(argc)(void)(argc);
129525 switch( sqlite3_value_type(argv[0]) ){
129526 case SQLITE_INTEGER1: {
129527 i64 iVal = sqlite3_value_int64(argv[0]);
129528 if( iVal<0 ){
129529 if( iVal==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
129530 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
129531 ** then abs(X) throws an integer overflow error since there is no
129532 ** equivalent positive 64-bit two complement value. */
129533 sqlite3_result_error(context, "integer overflow", -1);
129534 return;
129535 }
129536 iVal = -iVal;
129537 }
129538 sqlite3_result_int64(context, iVal);
129539 break;
129540 }
129541 case SQLITE_NULL5: {
129542 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
129543 sqlite3_result_null(context);
129544 break;
129545 }
129546 default: {
129547 /* Because sqlite3_value_double() returns 0.0 if the argument is not
129548 ** something that can be converted into a number, we have:
129549 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
129550 ** that cannot be converted to a numeric value.
129551 */
129552 double rVal = sqlite3_value_double(argv[0]);
129553 if( rVal<0 ) rVal = -rVal;
129554 sqlite3_result_double(context, rVal);
129555 break;
129556 }
129557 }
129558}
129559
129560/*
129561** Implementation of the instr() function.
129562**
129563** instr(haystack,needle) finds the first occurrence of needle
129564** in haystack and returns the number of previous characters plus 1,
129565** or 0 if needle does not occur within haystack.
129566**
129567** If both haystack and needle are BLOBs, then the result is one more than
129568** the number of bytes in haystack prior to the first occurrence of needle,
129569** or 0 if needle never occurs in haystack.
129570*/
129571static void instrFunc(
129572 sqlite3_context *context,
129573 int argc,
129574 sqlite3_value **argv
129575){
129576 const unsigned char *zHaystack;
129577 const unsigned char *zNeedle;
129578 int nHaystack;
129579 int nNeedle;
129580 int typeHaystack, typeNeedle;
129581 int N = 1;
129582 int isText;
129583 unsigned char firstChar;
129584 sqlite3_value *pC1 = 0;
129585 sqlite3_value *pC2 = 0;
129586
129587 UNUSED_PARAMETER(argc)(void)(argc);
129588 typeHaystack = sqlite3_value_type(argv[0]);
129589 typeNeedle = sqlite3_value_type(argv[1]);
129590 if( typeHaystack==SQLITE_NULL5 || typeNeedle==SQLITE_NULL5 ) return;
129591 nHaystack = sqlite3_value_bytes(argv[0]);
129592 nNeedle = sqlite3_value_bytes(argv[1]);
129593 if( nNeedle>0 ){
129594 if( typeHaystack==SQLITE_BLOB4 && typeNeedle==SQLITE_BLOB4 ){
129595 zHaystack = sqlite3_value_blob(argv[0]);
129596 zNeedle = sqlite3_value_blob(argv[1]);
129597 isText = 0;
129598 }else if( typeHaystack!=SQLITE_BLOB4 && typeNeedle!=SQLITE_BLOB4 ){
129599 zHaystack = sqlite3_value_text(argv[0]);
129600 zNeedle = sqlite3_value_text(argv[1]);
129601 isText = 1;
129602 }else{
129603 pC1 = sqlite3_value_dup(argv[0]);
129604 zHaystack = sqlite3_value_text(pC1);
129605 if( zHaystack==0 ) goto endInstrOOM;
129606 nHaystack = sqlite3_value_bytes(pC1);
129607 pC2 = sqlite3_value_dup(argv[1]);
129608 zNeedle = sqlite3_value_text(pC2);
129609 if( zNeedle==0 ) goto endInstrOOM;
129610 nNeedle = sqlite3_value_bytes(pC2);
129611 isText = 1;
129612 }
129613 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
129614 firstChar = zNeedle[0];
129615 while( nNeedle<=nHaystack
129616 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
129617 ){
129618 N++;
129619 do{
129620 nHaystack--;
129621 zHaystack++;
129622 }while( isText && (zHaystack[0]&0xc0)==0x80 );
129623 }
129624 if( nNeedle>nHaystack ) N = 0;
129625 }
129626 sqlite3_result_int(context, N);
129627endInstr:
129628 sqlite3_value_free(pC1);
129629 sqlite3_value_free(pC2);
129630 return;
129631endInstrOOM:
129632 sqlite3_result_error_nomem(context);
129633 goto endInstr;
129634}
129635
129636/*
129637** Implementation of the printf() (a.k.a. format()) SQL function.
129638*/
129639static void printfFunc(
129640 sqlite3_context *context,
129641 int argc,
129642 sqlite3_value **argv
129643){
129644 PrintfArguments x;
129645 StrAccum str;
129646 const char *zFormat;
129647 int n;
129648 sqlite3 *db = sqlite3_context_db_handle(context);
129649
129650 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
129651 x.nArg = argc-1;
129652 x.nUsed = 0;
129653 x.apArg = argv+1;
129654 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
129655 str.printfFlags = SQLITE_PRINTF_SQLFUNC0x02;
129656 sqlite3_str_appendf(&str, zFormat, &x);
129657 n = str.nChar;
129658 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
129659 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
129660 }
129661}
129662
129663/*
129664** Implementation of the substr() function.
129665**
129666** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
129667** p1 is 1-indexed. So substr(x,1,1) returns the first character
129668** of x. If x is text, then we actually count UTF-8 characters.
129669** If x is a blob, then we count bytes.
129670**
129671** If p1 is negative, then we begin abs(p1) from the end of x[].
129672**
129673** If p2 is negative, return the p2 characters preceding p1.
129674*/
129675static void substrFunc(
129676 sqlite3_context *context,
129677 int argc,
129678 sqlite3_value **argv
129679){
129680 const unsigned char *z;
129681 const unsigned char *z2;
129682 int len;
129683 int p0type;
129684 i64 p1, p2;
129685 int negP2 = 0;
129686
129687 assert( argc==3 || argc==2 )((void) (0));
129688 if( sqlite3_value_type(argv[1])==SQLITE_NULL5
129689 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL5)
129690 ){
129691 return;
129692 }
129693 p0type = sqlite3_value_type(argv[0]);
129694 p1 = sqlite3_value_int(argv[1]);
129695 if( p0type==SQLITE_BLOB4 ){
129696 len = sqlite3_value_bytes(argv[0]);
129697 z = sqlite3_value_blob(argv[0]);
129698 if( z==0 ) return;
129699 assert( len==sqlite3_value_bytes(argv[0]) )((void) (0));
129700 }else{
129701 z = sqlite3_value_text(argv[0]);
129702 if( z==0 ) return;
129703 len = 0;
129704 if( p1<0 ){
129705 for(z2=z; *z2; len++){
129706 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
129707 }
129708 }
129709 }
129710#ifdef SQLITE_SUBSTR_COMPATIBILITY1
129711 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
129712 ** as substr(X,1,N) - it returns the first N characters of X. This
129713 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
129714 ** from 2009-02-02 for compatibility of applications that exploited the
129715 ** old buggy behavior. */
129716 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
129717#endif
129718 if( argc==3 ){
129719 p2 = sqlite3_value_int(argv[2]);
129720 if( p2<0 ){
129721 p2 = -p2;
129722 negP2 = 1;
129723 }
129724 }else{
129725 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH0];
129726 }
129727 if( p1<0 ){
129728 p1 += len;
129729 if( p1<0 ){
129730 p2 += p1;
129731 if( p2<0 ) p2 = 0;
129732 p1 = 0;
129733 }
129734 }else if( p1>0 ){
129735 p1--;
129736 }else if( p2>0 ){
129737 p2--;
129738 }
129739 if( negP2 ){
129740 p1 -= p2;
129741 if( p1<0 ){
129742 p2 += p1;
129743 p1 = 0;
129744 }
129745 }
129746 assert( p1>=0 && p2>=0 )((void) (0));
129747 if( p0type!=SQLITE_BLOB4 ){
129748 while( *z && p1 ){
129749 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
129750 p1--;
129751 }
129752 for(z2=z; *z2 && p2; p2--){
129753 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
129754 }
129755 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
129756 SQLITE_UTF81);
129757 }else{
129758 if( p1+p2>len ){
129759 p2 = len-p1;
129760 if( p2<0 ) p2 = 0;
129761 }
129762 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
129763 }
129764}
129765
129766/*
129767** Implementation of the round() function
129768*/
129769#ifndef SQLITE_OMIT_FLOATING_POINT
129770static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129771 int n = 0;
129772 double r;
129773 char *zBuf;
129774 assert( argc==1 || argc==2 )((void) (0));
129775 if( argc==2 ){
129776 if( SQLITE_NULL5==sqlite3_value_type(argv[1]) ) return;
129777 n = sqlite3_value_int(argv[1]);
129778 if( n>30 ) n = 30;
129779 if( n<0 ) n = 0;
129780 }
129781 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
129782 r = sqlite3_value_double(argv[0]);
129783 /* If Y==0 and X will fit in a 64-bit int,
129784 ** handle the rounding directly,
129785 ** otherwise use printf.
129786 */
129787 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
129788 /* The value has no fractional part so there is nothing to round */
129789 }else if( n==0 ){
129790 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
129791 }else{
129792 zBuf = sqlite3_mprintf("%!.*f",n,r);
129793 if( zBuf==0 ){
129794 sqlite3_result_error_nomem(context);
129795 return;
129796 }
129797 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF81);
129798 sqlite3_free(zBuf);
129799 }
129800 sqlite3_result_double(context, r);
129801}
129802#endif
129803
129804/*
129805** Allocate nByte bytes of space using sqlite3Malloc(). If the
129806** allocation fails, call sqlite3_result_error_nomem() to notify
129807** the database handle that malloc() has failed and return NULL.
129808** If nByte is larger than the maximum string or blob length, then
129809** raise an SQLITE_TOOBIG exception and return NULL.
129810*/
129811static void *contextMalloc(sqlite3_context *context, i64 nByte){
129812 char *z;
129813 sqlite3 *db = sqlite3_context_db_handle(context);
129814 assert( nByte>0 )((void) (0));
129815 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
129816 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
129817 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
129818 sqlite3_result_error_toobig(context);
129819 z = 0;
129820 }else{
129821 z = sqlite3Malloc(nByte);
129822 if( !z ){
129823 sqlite3_result_error_nomem(context);
129824 }
129825 }
129826 return z;
129827}
129828
129829/*
129830** Implementation of the upper() and lower() SQL functions.
129831*/
129832static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129833 char *z1;
129834 const char *z2;
129835 int i, n;
129836 UNUSED_PARAMETER(argc)(void)(argc);
129837 z2 = (char*)sqlite3_value_text(argv[0]);
129838 n = sqlite3_value_bytes(argv[0]);
129839 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
129840 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
129841 if( z2 ){
129842 z1 = contextMalloc(context, ((i64)n)+1);
129843 if( z1 ){
129844 for(i=0; i<n; i++){
129845 z1[i] = (char)sqlite3Toupper(z2[i])((z2[i])&~(sqlite3CtypeMap[(unsigned char)(z2[i])]&0x20
))
;
129846 }
129847 sqlite3_result_text(context, z1, n, sqlite3_free);
129848 }
129849 }
129850}
129851static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129852 char *z1;
129853 const char *z2;
129854 int i, n;
129855 UNUSED_PARAMETER(argc)(void)(argc);
129856 z2 = (char*)sqlite3_value_text(argv[0]);
129857 n = sqlite3_value_bytes(argv[0]);
129858 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
129859 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
129860 if( z2 ){
129861 z1 = contextMalloc(context, ((i64)n)+1);
129862 if( z1 ){
129863 for(i=0; i<n; i++){
129864 z1[i] = sqlite3Tolower(z2[i])(sqlite3UpperToLower[(unsigned char)(z2[i])]);
129865 }
129866 sqlite3_result_text(context, z1, n, sqlite3_free);
129867 }
129868 }
129869}
129870
129871/*
129872** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
129873** as VDBE code so that unused argument values do not have to be computed.
129874** However, we still need some kind of function implementation for this
129875** routines in the function table. The noopFunc macro provides this.
129876** noopFunc will never be called so it doesn't matter what the implementation
129877** is. We might as well use the "version()" function as a substitute.
129878*/
129879#define noopFuncversionFunc versionFunc /* Substitute function - never called */
129880
129881/*
129882** Implementation of random(). Return a random integer.
129883*/
129884static void randomFunc(
129885 sqlite3_context *context,
129886 int NotUsed,
129887 sqlite3_value **NotUsed2
129888){
129889 sqlite_int64 r;
129890 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
129891 sqlite3_randomness(sizeof(r), &r);
129892 if( r<0 ){
129893 /* We need to prevent a random number of 0x8000000000000000
129894 ** (or -9223372036854775808) since when you do abs() of that
129895 ** number of you get the same value back again. To do this
129896 ** in a way that is testable, mask the sign bit off of negative
129897 ** values, resulting in a positive value. Then take the
129898 ** 2s complement of that positive value. The end result can
129899 ** therefore be no less than -9223372036854775807.
129900 */
129901 r = -(r & LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
129902 }
129903 sqlite3_result_int64(context, r);
129904}
129905
129906/*
129907** Implementation of randomblob(N). Return a random blob
129908** that is N bytes long.
129909*/
129910static void randomBlob(
129911 sqlite3_context *context,
129912 int argc,
129913 sqlite3_value **argv
129914){
129915 sqlite3_int64 n;
129916 unsigned char *p;
129917 assert( argc==1 )((void) (0));
129918 UNUSED_PARAMETER(argc)(void)(argc);
129919 n = sqlite3_value_int64(argv[0]);
129920 if( n<1 ){
129921 n = 1;
129922 }
129923 p = contextMalloc(context, n);
129924 if( p ){
129925 sqlite3_randomness(n, p);
129926 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
129927 }
129928}
129929
129930/*
129931** Implementation of the last_insert_rowid() SQL function. The return
129932** value is the same as the sqlite3_last_insert_rowid() API function.
129933*/
129934static void last_insert_rowid(
129935 sqlite3_context *context,
129936 int NotUsed,
129937 sqlite3_value **NotUsed2
129938){
129939 sqlite3 *db = sqlite3_context_db_handle(context);
129940 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
129941 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
129942 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
129943 ** function. */
129944 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
129945}
129946
129947/*
129948** Implementation of the changes() SQL function.
129949**
129950** IMP: R-32760-32347 The changes() SQL function is a wrapper
129951** around the sqlite3_changes64() C/C++ function and hence follows the
129952** same rules for counting changes.
129953*/
129954static void changes(
129955 sqlite3_context *context,
129956 int NotUsed,
129957 sqlite3_value **NotUsed2
129958){
129959 sqlite3 *db = sqlite3_context_db_handle(context);
129960 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
129961 sqlite3_result_int64(context, sqlite3_changes64(db));
129962}
129963
129964/*
129965** Implementation of the total_changes() SQL function. The return value is
129966** the same as the sqlite3_total_changes64() API function.
129967*/
129968static void total_changes(
129969 sqlite3_context *context,
129970 int NotUsed,
129971 sqlite3_value **NotUsed2
129972){
129973 sqlite3 *db = sqlite3_context_db_handle(context);
129974 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
129975 /* IMP: R-11217-42568 This function is a wrapper around the
129976 ** sqlite3_total_changes64() C/C++ interface. */
129977 sqlite3_result_int64(context, sqlite3_total_changes64(db));
129978}
129979
129980/*
129981** A structure defining how to do GLOB-style comparisons.
129982*/
129983struct compareInfo {
129984 u8 matchAll; /* "*" or "%" */
129985 u8 matchOne; /* "?" or "_" */
129986 u8 matchSet; /* "[" or 0 */
129987 u8 noCase; /* true to ignore case differences */
129988};
129989
129990/*
129991** For LIKE and GLOB matching on EBCDIC machines, assume that every
129992** character is exactly one byte in size. Also, provide the Utf8Read()
129993** macro for fast reading of the next character in the common case where
129994** the next character is ASCII.
129995*/
129996#if defined(SQLITE_EBCDIC)
129997# define sqlite3Utf8Read(A) (*((*A)++))
129998# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (*(A++))
129999#else
130000# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
130001#endif
130002
130003static const struct compareInfo globInfo = { '*', '?', '[', 0 };
130004/* The correct SQL-92 behavior is for the LIKE operator to ignore
130005** case. Thus 'a' LIKE 'A' would be true. */
130006static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
130007/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
130008** is case sensitive causing 'a' LIKE 'A' to be false */
130009static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
130010
130011/*
130012** Possible error returns from patternMatch()
130013*/
130014#define SQLITE_MATCH0 0
130015#define SQLITE_NOMATCH1 1
130016#define SQLITE_NOWILDCARDMATCH2 2
130017
130018/*
130019** Compare two UTF-8 strings for equality where the first string is
130020** a GLOB or LIKE expression. Return values:
130021**
130022** SQLITE_MATCH: Match
130023** SQLITE_NOMATCH: No match
130024** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
130025**
130026** Globbing rules:
130027**
130028** '*' Matches any sequence of zero or more characters.
130029**
130030** '?' Matches exactly one character.
130031**
130032** [...] Matches one character from the enclosed list of
130033** characters.
130034**
130035** [^...] Matches one character not in the enclosed list.
130036**
130037** With the [...] and [^...] matching, a ']' character can be included
130038** in the list by making it the first character after '[' or '^'. A
130039** range of characters can be specified using '-'. Example:
130040** "[a-z]" matches any single lower-case letter. To match a '-', make
130041** it the last character in the list.
130042**
130043** Like matching rules:
130044**
130045** '%' Matches any sequence of zero or more characters
130046**
130047*** '_' Matches any one character
130048**
130049** Ec Where E is the "esc" character and c is any other
130050** character, including '%', '_', and esc, match exactly c.
130051**
130052** The comments within this routine usually assume glob matching.
130053**
130054** This routine is usually quick, but can be N**2 in the worst case.
130055*/
130056static int patternCompare(
130057 const u8 *zPattern, /* The glob pattern */
130058 const u8 *zString, /* The string to compare against the glob */
130059 const struct compareInfo *pInfo, /* Information about how to do the compare */
130060 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
130061){
130062 u32 c, c2; /* Next pattern and input string chars */
130063 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
130064 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
130065 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
130066 const u8 *zEscaped = 0; /* One past the last escaped input char */
130067
130068 while( (c = Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
)!=0 ){
130069 if( c==matchAll ){ /* Match "*" */
130070 /* Skip over multiple "*" characters in the pattern. If there
130071 ** are also "?" characters, skip those as well, but consume a
130072 ** single character of the input string for each "?" skipped */
130073 while( (c=Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
) == matchAll
130074 || (c == matchOne && matchOne!=0) ){
130075 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
130076 return SQLITE_NOWILDCARDMATCH2;
130077 }
130078 }
130079 if( c==0 ){
130080 return SQLITE_MATCH0; /* "*" at the end of the pattern matches */
130081 }else if( c==matchOther ){
130082 if( pInfo->matchSet==0 ){
130083 c = sqlite3Utf8Read(&zPattern);
130084 if( c==0 ) return SQLITE_NOWILDCARDMATCH2;
130085 }else{
130086 /* "[...]" immediately follows the "*". We have to do a slow
130087 ** recursive search in this case, but it is an unusual case. */
130088 assert( matchOther<0x80 )((void) (0)); /* '[' is a single-byte character */
130089 while( *zString ){
130090 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
130091 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
130092 SQLITE_SKIP_UTF8(zString){ if( (*(zString++))>=0xc0 ){ while( (*zString & 0xc0)
==0x80 ){ zString++; } } }
;
130093 }
130094 return SQLITE_NOWILDCARDMATCH2;
130095 }
130096 }
130097
130098 /* At this point variable c contains the first character of the
130099 ** pattern string past the "*". Search in the input string for the
130100 ** first matching character and recursively continue the match from
130101 ** that point.
130102 **
130103 ** For a case-insensitive search, set variable cx to be the same as
130104 ** c but in the other case and search the input string for either
130105 ** c or cx.
130106 */
130107 if( c<0x80 ){
130108 char zStop[3];
130109 int bMatch;
130110 if( noCase ){
130111 zStop[0] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
130112 zStop[1] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
130113 zStop[2] = 0;
130114 }else{
130115 zStop[0] = c;
130116 zStop[1] = 0;
130117 }
130118 while(1){
130119 zString += strcspn((const char*)zString, zStop);
130120 if( zString[0]==0 ) break;
130121 zString++;
130122 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
130123 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
130124 }
130125 }else{
130126 int bMatch;
130127 while( (c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
)!=0 ){
130128 if( c2!=c ) continue;
130129 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
130130 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
130131 }
130132 }
130133 return SQLITE_NOWILDCARDMATCH2;
130134 }
130135 if( c==matchOther ){
130136 if( pInfo->matchSet==0 ){
130137 c = sqlite3Utf8Read(&zPattern);
130138 if( c==0 ) return SQLITE_NOMATCH1;
130139 zEscaped = zPattern;
130140 }else{
130141 u32 prior_c = 0;
130142 int seen = 0;
130143 int invert = 0;
130144 c = sqlite3Utf8Read(&zString);
130145 if( c==0 ) return SQLITE_NOMATCH1;
130146 c2 = sqlite3Utf8Read(&zPattern);
130147 if( c2=='^' ){
130148 invert = 1;
130149 c2 = sqlite3Utf8Read(&zPattern);
130150 }
130151 if( c2==']' ){
130152 if( c==']' ) seen = 1;
130153 c2 = sqlite3Utf8Read(&zPattern);
130154 }
130155 while( c2 && c2!=']' ){
130156 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
130157 c2 = sqlite3Utf8Read(&zPattern);
130158 if( c>=prior_c && c<=c2 ) seen = 1;
130159 prior_c = 0;
130160 }else{
130161 if( c==c2 ){
130162 seen = 1;
130163 }
130164 prior_c = c2;
130165 }
130166 c2 = sqlite3Utf8Read(&zPattern);
130167 }
130168 if( c2==0 || (seen ^ invert)==0 ){
130169 return SQLITE_NOMATCH1;
130170 }
130171 continue;
130172 }
130173 }
130174 c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
;
130175 if( c==c2 ) continue;
130176 if( noCase && sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)])==sqlite3Tolower(c2)(sqlite3UpperToLower[(unsigned char)(c2)]) && c<0x80 && c2<0x80 ){
130177 continue;
130178 }
130179 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
130180 return SQLITE_NOMATCH1;
130181 }
130182 return *zString==0 ? SQLITE_MATCH0 : SQLITE_NOMATCH1;
130183}
130184
130185/*
130186** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
130187** non-zero if there is no match.
130188*/
130189SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
130190 if( zString==0 ){
130191 return zGlobPattern!=0;
130192 }else if( zGlobPattern==0 ){
130193 return 1;
130194 }else {
130195 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
130196 }
130197}
130198
130199/*
130200** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
130201** a miss - like strcmp().
130202*/
130203SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
130204 if( zStr==0 ){
130205 return zPattern!=0;
130206 }else if( zPattern==0 ){
130207 return 1;
130208 }else{
130209 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
130210 }
130211}
130212
130213/*
130214** Count the number of times that the LIKE operator (or GLOB which is
130215** just a variation of LIKE) gets called. This is used for testing
130216** only.
130217*/
130218#ifdef SQLITE_TEST
130219SQLITE_API int sqlite3_like_count = 0;
130220#endif
130221
130222
130223/*
130224** Implementation of the like() SQL function. This function implements
130225** the built-in LIKE operator. The first argument to the function is the
130226** pattern and the second argument is the string. So, the SQL statements:
130227**
130228** A LIKE B
130229**
130230** is implemented as like(B,A).
130231**
130232** This same function (with a different compareInfo structure) computes
130233** the GLOB operator.
130234*/
130235static void likeFunc(
130236 sqlite3_context *context,
130237 int argc,
130238 sqlite3_value **argv
130239){
130240 const unsigned char *zA, *zB;
130241 u32 escape;
130242 int nPat;
130243 sqlite3 *db = sqlite3_context_db_handle(context);
130244 struct compareInfo *pInfo = sqlite3_user_data(context);
130245 struct compareInfo backupInfo;
130246
130247#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
130248 if( sqlite3_value_type(argv[0])==SQLITE_BLOB4
130249 || sqlite3_value_type(argv[1])==SQLITE_BLOB4
130250 ){
130251#ifdef SQLITE_TEST
130252 sqlite3_like_count++;
130253#endif
130254 sqlite3_result_int(context, 0);
130255 return;
130256 }
130257#endif
130258
130259 /* Limit the length of the LIKE or GLOB pattern to avoid problems
130260 ** of deep recursion and N*N behavior in patternCompare().
130261 */
130262 nPat = sqlite3_value_bytes(argv[0]);
130263 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
130264 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
130265 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH8] ){
130266 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
130267 return;
130268 }
130269 if( argc==3 ){
130270 /* The escape character string must consist of a single UTF-8 character.
130271 ** Otherwise, return an error.
130272 */
130273 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
130274 if( zEsc==0 ) return;
130275 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
130276 sqlite3_result_error(context,
130277 "ESCAPE expression must be a single character", -1);
130278 return;
130279 }
130280 escape = sqlite3Utf8Read(&zEsc);
130281 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
130282 memcpy(&backupInfo, pInfo, sizeof(backupInfo));
130283 pInfo = &backupInfo;
130284 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
130285 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
130286 }
130287 }else{
130288 escape = pInfo->matchSet;
130289 }
130290 zB = sqlite3_value_text(argv[0]);
130291 zA = sqlite3_value_text(argv[1]);
130292 if( zA && zB ){
130293#ifdef SQLITE_TEST
130294 sqlite3_like_count++;
130295#endif
130296 sqlite3_result_int(context,
130297 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH0);
130298 }
130299}
130300
130301/*
130302** Implementation of the NULLIF(x,y) function. The result is the first
130303** argument if the arguments are different. The result is NULL if the
130304** arguments are equal to each other.
130305*/
130306static void nullifFunc(
130307 sqlite3_context *context,
130308 int NotUsed,
130309 sqlite3_value **argv
130310){
130311 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
130312 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
130313 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
130314 sqlite3_result_value(context, argv[0]);
130315 }
130316}
130317
130318/*
130319** Implementation of the sqlite_version() function. The result is the version
130320** of the SQLite library that is running.
130321*/
130322static void versionFunc(
130323 sqlite3_context *context,
130324 int NotUsed,
130325 sqlite3_value **NotUsed2
130326){
130327 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130328 /* IMP: R-48699-48617 This function is an SQL wrapper around the
130329 ** sqlite3_libversion() C-interface. */
130330 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
130331}
130332
130333/*
130334** Implementation of the sqlite_source_id() function. The result is a string
130335** that identifies the particular version of the source code used to build
130336** SQLite.
130337*/
130338static void sourceidFunc(
130339 sqlite3_context *context,
130340 int NotUsed,
130341 sqlite3_value **NotUsed2
130342){
130343 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130344 /* IMP: R-24470-31136 This function is an SQL wrapper around the
130345 ** sqlite3_sourceid() C interface. */
130346 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
130347}
130348
130349/*
130350** Implementation of the sqlite_log() function. This is a wrapper around
130351** sqlite3_log(). The return value is NULL. The function exists purely for
130352** its side-effects.
130353*/
130354static void errlogFunc(
130355 sqlite3_context *context,
130356 int argc,
130357 sqlite3_value **argv
130358){
130359 UNUSED_PARAMETER(argc)(void)(argc);
130360 UNUSED_PARAMETER(context)(void)(context);
130361 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
130362}
130363
130364/*
130365** Implementation of the sqlite_compileoption_used() function.
130366** The result is an integer that identifies if the compiler option
130367** was used to build SQLite.
130368*/
130369#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
130370static void compileoptionusedFunc(
130371 sqlite3_context *context,
130372 int argc,
130373 sqlite3_value **argv
130374){
130375 const char *zOptName;
130376 assert( argc==1 )((void) (0));
130377 UNUSED_PARAMETER(argc)(void)(argc);
130378 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
130379 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
130380 ** function.
130381 */
130382 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
130383 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
130384 }
130385}
130386#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
130387
130388/*
130389** Implementation of the sqlite_compileoption_get() function.
130390** The result is a string that identifies the compiler options
130391** used to build SQLite.
130392*/
130393#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
130394static void compileoptiongetFunc(
130395 sqlite3_context *context,
130396 int argc,
130397 sqlite3_value **argv
130398){
130399 int n;
130400 assert( argc==1 )((void) (0));
130401 UNUSED_PARAMETER(argc)(void)(argc);
130402 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
130403 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
130404 */
130405 n = sqlite3_value_int(argv[0]);
130406 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
130407}
130408#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
130409
130410/* Array for converting from half-bytes (nybbles) into ASCII hex
130411** digits. */
130412static const char hexdigits[] = {
130413 '0', '1', '2', '3', '4', '5', '6', '7',
130414 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
130415};
130416
130417/*
130418** Append to pStr text that is the SQL literal representation of the
130419** value contained in pValue.
130420*/
130421SQLITE_PRIVATEstatic void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
130422 /* As currently implemented, the string must be initially empty.
130423 ** we might relax this requirement in the future, but that will
130424 ** require enhancements to the implementation. */
130425 assert( pStr!=0 && pStr->nChar==0 )((void) (0));
130426
130427 switch( sqlite3_value_type(pValue) ){
130428 case SQLITE_FLOAT2: {
130429 double r1, r2;
130430 const char *zVal;
130431 r1 = sqlite3_value_double(pValue);
130432 sqlite3_str_appendf(pStr, "%!0.15g", r1);
130433 zVal = sqlite3_str_value(pStr);
130434 if( zVal ){
130435 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF81);
130436 if( r1!=r2 ){
130437 sqlite3_str_reset(pStr);
130438 sqlite3_str_appendf(pStr, "%!0.20e", r1);
130439 }
130440 }
130441 break;
130442 }
130443 case SQLITE_INTEGER1: {
130444 sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue));
130445 break;
130446 }
130447 case SQLITE_BLOB4: {
130448 char const *zBlob = sqlite3_value_blob(pValue);
130449 i64 nBlob = sqlite3_value_bytes(pValue);
130450 assert( zBlob==sqlite3_value_blob(pValue) )((void) (0)); /* No encoding change */
130451 sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4);
130452 if( pStr->accError==0 ){
130453 char *zText = pStr->zText;
130454 int i;
130455 for(i=0; i<nBlob; i++){
130456 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
130457 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
130458 }
130459 zText[(nBlob*2)+2] = '\'';
130460 zText[(nBlob*2)+3] = '\0';
130461 zText[0] = 'X';
130462 zText[1] = '\'';
130463 pStr->nChar = nBlob*2 + 3;
130464 }
130465 break;
130466 }
130467 case SQLITE_TEXT3: {
130468 const unsigned char *zArg = sqlite3_value_text(pValue);
130469 sqlite3_str_appendf(pStr, "%Q", zArg);
130470 break;
130471 }
130472 default: {
130473 assert( sqlite3_value_type(pValue)==SQLITE_NULL )((void) (0));
130474 sqlite3_str_append(pStr, "NULL", 4);
130475 break;
130476 }
130477 }
130478}
130479
130480/*
130481** Implementation of the QUOTE() function.
130482**
130483** The quote(X) function returns the text of an SQL literal which is the
130484** value of its argument suitable for inclusion into an SQL statement.
130485** Strings are surrounded by single-quotes with escapes on interior quotes
130486** as needed. BLOBs are encoded as hexadecimal literals. Strings with
130487** embedded NUL characters cannot be represented as string literals in SQL
130488** and hence the returned string literal is truncated prior to the first NUL.
130489*/
130490static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130491 sqlite3_str str;
130492 sqlite3 *db = sqlite3_context_db_handle(context);
130493 assert( argc==1 )((void) (0));
130494 UNUSED_PARAMETER(argc)(void)(argc);
130495 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
130496 sqlite3QuoteValue(&str,argv[0]);
130497 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
130498 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
130499 if( str.accError!=SQLITE_OK0 ){
130500 sqlite3_result_null(context);
130501 sqlite3_result_error_code(context, str.accError);
130502 }
130503}
130504
130505/*
130506** The unicode() function. Return the integer unicode code-point value
130507** for the first character of the input string.
130508*/
130509static void unicodeFunc(
130510 sqlite3_context *context,
130511 int argc,
130512 sqlite3_value **argv
130513){
130514 const unsigned char *z = sqlite3_value_text(argv[0]);
130515 (void)argc;
130516 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
130517}
130518
130519/*
130520** The char() function takes zero or more arguments, each of which is
130521** an integer. It constructs a string where each character of the string
130522** is the unicode character for the corresponding integer argument.
130523*/
130524static void charFunc(
130525 sqlite3_context *context,
130526 int argc,
130527 sqlite3_value **argv
130528){
130529 unsigned char *z, *zOut;
130530 int i;
130531 zOut = z = sqlite3_malloc64( argc*4+1 );
130532 if( z==0 ){
130533 sqlite3_result_error_nomem(context);
130534 return;
130535 }
130536 for(i=0; i<argc; i++){
130537 sqlite3_int64 x;
130538 unsigned c;
130539 x = sqlite3_value_int64(argv[i]);
130540 if( x<0 || x>0x10ffff ) x = 0xfffd;
130541 c = (unsigned)(x & 0x1fffff);
130542 if( c<0x00080 ){
130543 *zOut++ = (u8)(c&0xFF);
130544 }else if( c<0x00800 ){
130545 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
130546 *zOut++ = 0x80 + (u8)(c & 0x3F);
130547 }else if( c<0x10000 ){
130548 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
130549 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
130550 *zOut++ = 0x80 + (u8)(c & 0x3F);
130551 }else{
130552 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
130553 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
130554 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
130555 *zOut++ = 0x80 + (u8)(c & 0x3F);
130556 } \
130557 }
130558 *zOut = 0;
130559 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF81);
130560}
130561
130562/*
130563** The hex() function. Interpret the argument as a blob. Return
130564** a hexadecimal rendering as text.
130565*/
130566static void hexFunc(
130567 sqlite3_context *context,
130568 int argc,
130569 sqlite3_value **argv
130570){
130571 int i, n;
130572 const unsigned char *pBlob;
130573 char *zHex, *z;
130574 assert( argc==1 )((void) (0));
130575 UNUSED_PARAMETER(argc)(void)(argc);
130576 pBlob = sqlite3_value_blob(argv[0]);
130577 n = sqlite3_value_bytes(argv[0]);
130578 assert( pBlob==sqlite3_value_blob(argv[0]) )((void) (0)); /* No encoding change */
130579 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
130580 if( zHex ){
130581 for(i=0; i<n; i++, pBlob++){
130582 unsigned char c = *pBlob;
130583 *(z++) = hexdigits[(c>>4)&0xf];
130584 *(z++) = hexdigits[c&0xf];
130585 }
130586 *z = 0;
130587 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
130588 sqlite3_free, SQLITE_UTF81);
130589 }
130590}
130591
130592/*
130593** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
130594** contains character ch, or 0 if it does not.
130595*/
130596static int strContainsChar(const u8 *zStr, int nStr, u32 ch){
130597 const u8 *zEnd = &zStr[nStr];
130598 const u8 *z = zStr;
130599 while( z<zEnd ){
130600 u32 tst = Utf8Read(z)(z[0]<0x80?*(z++):sqlite3Utf8Read(&z));
130601 if( tst==ch ) return 1;
130602 }
130603 return 0;
130604}
130605
130606/*
130607** The unhex() function. This function may be invoked with either one or
130608** two arguments. In both cases the first argument is interpreted as text
130609** a text value containing a set of pairs of hexadecimal digits which are
130610** decoded and returned as a blob.
130611**
130612** If there is only a single argument, then it must consist only of an
130613** even number of hexadecimal digits. Otherwise, return NULL.
130614**
130615** Or, if there is a second argument, then any character that appears in
130616** the second argument is also allowed to appear between pairs of hexadecimal
130617** digits in the first argument. If any other character appears in the
130618** first argument, or if one of the allowed characters appears between
130619** two hexadecimal digits that make up a single byte, NULL is returned.
130620**
130621** The following expressions are all true:
130622**
130623** unhex('ABCD') IS x'ABCD'
130624** unhex('AB CD') IS NULL
130625** unhex('AB CD', ' ') IS x'ABCD'
130626** unhex('A BCD', ' ') IS NULL
130627*/
130628static void unhexFunc(
130629 sqlite3_context *pCtx,
130630 int argc,
130631 sqlite3_value **argv
130632){
130633 const u8 *zPass = (const u8*)"";
130634 int nPass = 0;
130635 const u8 *zHex = sqlite3_value_text(argv[0]);
130636 int nHex = sqlite3_value_bytes(argv[0]);
130637#ifdef SQLITE_DEBUG
130638 const u8 *zEnd = zHex ? &zHex[nHex] : 0;
130639#endif
130640 u8 *pBlob = 0;
130641 u8 *p = 0;
130642
130643 assert( argc==1 || argc==2 )((void) (0));
130644 if( argc==2 ){
130645 zPass = sqlite3_value_text(argv[1]);
130646 nPass = sqlite3_value_bytes(argv[1]);
130647 }
130648 if( !zHex || !zPass ) return;
130649
130650 p = pBlob = contextMalloc(pCtx, (nHex/2)+1);
130651 if( pBlob ){
130652 u8 c; /* Most significant digit of next byte */
130653 u8 d; /* Least significant digit of next byte */
130654
130655 while( (c = *zHex)!=0x00 ){
130656 while( !sqlite3Isxdigit(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x08) ){
130657 u32 ch = Utf8Read(zHex)(zHex[0]<0x80?*(zHex++):sqlite3Utf8Read(&zHex));
130658 assert( zHex<=zEnd )((void) (0));
130659 if( !strContainsChar(zPass, nPass, ch) ) goto unhex_null;
130660 c = *zHex;
130661 if( c==0x00 ) goto unhex_done;
130662 }
130663 zHex++;
130664 assert( *zEnd==0x00 )((void) (0));
130665 assert( zHex<=zEnd )((void) (0));
130666 d = *(zHex++);
130667 if( !sqlite3Isxdigit(d)(sqlite3CtypeMap[(unsigned char)(d)]&0x08) ) goto unhex_null;
130668 *(p++) = (sqlite3HexToInt(c)<<4) | sqlite3HexToInt(d);
130669 }
130670 }
130671
130672 unhex_done:
130673 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
130674 return;
130675
130676 unhex_null:
130677 sqlite3_free(pBlob);
130678 return;
130679}
130680
130681
130682/*
130683** The zeroblob(N) function returns a zero-filled blob of size N bytes.
130684*/
130685static void zeroblobFunc(
130686 sqlite3_context *context,
130687 int argc,
130688 sqlite3_value **argv
130689){
130690 i64 n;
130691 int rc;
130692 assert( argc==1 )((void) (0));
130693 UNUSED_PARAMETER(argc)(void)(argc);
130694 n = sqlite3_value_int64(argv[0]);
130695 if( n<0 ) n = 0;
130696 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
130697 if( rc ){
130698 sqlite3_result_error_code(context, rc);
130699 }
130700}
130701
130702/*
130703** The replace() function. Three arguments are all strings: call
130704** them A, B, and C. The result is also a string which is derived
130705** from A by replacing every occurrence of B with C. The match
130706** must be exact. Collating sequences are not used.
130707*/
130708static void replaceFunc(
130709 sqlite3_context *context,
130710 int argc,
130711 sqlite3_value **argv
130712){
130713 const unsigned char *zStr; /* The input string A */
130714 const unsigned char *zPattern; /* The pattern string B */
130715 const unsigned char *zRep; /* The replacement string C */
130716 unsigned char *zOut; /* The output */
130717 int nStr; /* Size of zStr */
130718 int nPattern; /* Size of zPattern */
130719 int nRep; /* Size of zRep */
130720 i64 nOut; /* Maximum size of zOut */
130721 int loopLimit; /* Last zStr[] that might match zPattern[] */
130722 int i, j; /* Loop counters */
130723 unsigned cntExpand; /* Number zOut expansions */
130724 sqlite3 *db = sqlite3_context_db_handle(context);
130725
130726 assert( argc==3 )((void) (0));
130727 UNUSED_PARAMETER(argc)(void)(argc);
130728 zStr = sqlite3_value_text(argv[0]);
130729 if( zStr==0 ) return;
130730 nStr = sqlite3_value_bytes(argv[0]);
130731 assert( zStr==sqlite3_value_text(argv[0]) )((void) (0)); /* No encoding change */
130732 zPattern = sqlite3_value_text(argv[1]);
130733 if( zPattern==0 ){
130734 assert( sqlite3_value_type(argv[1])==SQLITE_NULL((void) (0))
130735 || sqlite3_context_db_handle(context)->mallocFailed )((void) (0));
130736 return;
130737 }
130738 if( zPattern[0]==0 ){
130739 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL )((void) (0));
130740 sqlite3_result_text(context, (const char*)zStr, nStr, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
130741 return;
130742 }
130743 nPattern = sqlite3_value_bytes(argv[1]);
130744 assert( zPattern==sqlite3_value_text(argv[1]) )((void) (0)); /* No encoding change */
130745 zRep = sqlite3_value_text(argv[2]);
130746 if( zRep==0 ) return;
130747 nRep = sqlite3_value_bytes(argv[2]);
130748 assert( zRep==sqlite3_value_text(argv[2]) )((void) (0));
130749 nOut = nStr + 1;
130750 assert( nOut<SQLITE_MAX_LENGTH )((void) (0));
130751 zOut = contextMalloc(context, (i64)nOut);
130752 if( zOut==0 ){
130753 return;
130754 }
130755 loopLimit = nStr - nPattern;
130756 cntExpand = 0;
130757 for(i=j=0; i<=loopLimit; i++){
130758 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
130759 zOut[j++] = zStr[i];
130760 }else{
130761 if( nRep>nPattern ){
130762 nOut += nRep - nPattern;
130763 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
130764 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
130765 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
130766 sqlite3_result_error_toobig(context);
130767 sqlite3_free(zOut);
130768 return;
130769 }
130770 cntExpand++;
130771 if( (cntExpand&(cntExpand-1))==0 ){
130772 /* Grow the size of the output buffer only on substitutions
130773 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
130774 u8 *zOld;
130775 zOld = zOut;
130776 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
130777 if( zOut==0 ){
130778 sqlite3_result_error_nomem(context);
130779 sqlite3_free(zOld);
130780 return;
130781 }
130782 }
130783 }
130784 memcpy(&zOut[j], zRep, nRep);
130785 j += nRep;
130786 i += nPattern-1;
130787 }
130788 }
130789 assert( j+nStr-i+1<=nOut )((void) (0));
130790 memcpy(&zOut[j], &zStr[i], nStr-i);
130791 j += nStr - i;
130792 assert( j<=nOut )((void) (0));
130793 zOut[j] = 0;
130794 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
130795}
130796
130797/*
130798** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
130799** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
130800*/
130801static void trimFunc(
130802 sqlite3_context *context,
130803 int argc,
130804 sqlite3_value **argv
130805){
130806 const unsigned char *zIn; /* Input string */
130807 const unsigned char *zCharSet; /* Set of characters to trim */
130808 unsigned int nIn; /* Number of bytes in input */
130809 int flags; /* 1: trimleft 2: trimright 3: trim */
130810 int i; /* Loop counter */
130811 unsigned int *aLen = 0; /* Length of each character in zCharSet */
130812 unsigned char **azChar = 0; /* Individual characters in zCharSet */
130813 int nChar; /* Number of characters in zCharSet */
130814
130815 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ){
130816 return;
130817 }
130818 zIn = sqlite3_value_text(argv[0]);
130819 if( zIn==0 ) return;
130820 nIn = (unsigned)sqlite3_value_bytes(argv[0]);
130821 assert( zIn==sqlite3_value_text(argv[0]) )((void) (0));
130822 if( argc==1 ){
130823 static const unsigned lenOne[] = { 1 };
130824 static unsigned char * const azOne[] = { (u8*)" " };
130825 nChar = 1;
130826 aLen = (unsigned*)lenOne;
130827 azChar = (unsigned char **)azOne;
130828 zCharSet = 0;
130829 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
130830 return;
130831 }else{
130832 const unsigned char *z;
130833 for(z=zCharSet, nChar=0; *z; nChar++){
130834 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
130835 }
130836 if( nChar>0 ){
130837 azChar = contextMalloc(context,
130838 ((i64)nChar)*(sizeof(char*)+sizeof(unsigned)));
130839 if( azChar==0 ){
130840 return;
130841 }
130842 aLen = (unsigned*)&azChar[nChar];
130843 for(z=zCharSet, nChar=0; *z; nChar++){
130844 azChar[nChar] = (unsigned char *)z;
130845 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
130846 aLen[nChar] = (unsigned)(z - azChar[nChar]);
130847 }
130848 }
130849 }
130850 if( nChar>0 ){
130851 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context)));
130852 if( flags & 1 ){
130853 while( nIn>0 ){
130854 unsigned int len = 0;
130855 for(i=0; i<nChar; i++){
130856 len = aLen[i];
130857 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
130858 }
130859 if( i>=nChar ) break;
130860 zIn += len;
130861 nIn -= len;
130862 }
130863 }
130864 if( flags & 2 ){
130865 while( nIn>0 ){
130866 unsigned int len = 0;
130867 for(i=0; i<nChar; i++){
130868 len = aLen[i];
130869 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
130870 }
130871 if( i>=nChar ) break;
130872 nIn -= len;
130873 }
130874 }
130875 if( zCharSet ){
130876 sqlite3_free(azChar);
130877 }
130878 }
130879 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
130880}
130881
130882/* The core implementation of the CONCAT(...) and CONCAT_WS(SEP,...)
130883** functions.
130884**
130885** Return a string value that is the concatenation of all non-null
130886** entries in argv[]. Use zSep as the separator.
130887*/
130888static void concatFuncCore(
130889 sqlite3_context *context,
130890 int argc,
130891 sqlite3_value **argv,
130892 int nSep,
130893 const char *zSep
130894){
130895 i64 j, k, n = 0;
130896 int i;
130897 char *z;
130898 for(i=0; i<argc; i++){
130899 n += sqlite3_value_bytes(argv[i]);
130900 }
130901 n += (argc-1)*nSep;
130902 z = sqlite3_malloc64(n+1);
130903 if( z==0 ){
130904 sqlite3_result_error_nomem(context);
130905 return;
130906 }
130907 j = 0;
130908 for(i=0; i<argc; i++){
130909 k = sqlite3_value_bytes(argv[i]);
130910 if( k>0 ){
130911 const char *v = (const char*)sqlite3_value_text(argv[i]);
130912 if( v!=0 ){
130913 if( j>0 && nSep>0 ){
130914 memcpy(&z[j], zSep, nSep);
130915 j += nSep;
130916 }
130917 memcpy(&z[j], v, k);
130918 j += k;
130919 }
130920 }
130921 }
130922 z[j] = 0;
130923 assert( j<=n )((void) (0));
130924 sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF81);
130925}
130926
130927/*
130928** The CONCAT(...) function. Generate a string result that is the
130929** concatentation of all non-null arguments.
130930*/
130931static void concatFunc(
130932 sqlite3_context *context,
130933 int argc,
130934 sqlite3_value **argv
130935){
130936 concatFuncCore(context, argc, argv, 0, "");
130937}
130938
130939/*
130940** The CONCAT_WS(separator, ...) function.
130941**
130942** Generate a string that is the concatenation of 2nd through the Nth
130943** argument. Use the first argument (which must be non-NULL) as the
130944** separator.
130945*/
130946static void concatwsFunc(
130947 sqlite3_context *context,
130948 int argc,
130949 sqlite3_value **argv
130950){
130951 int nSep = sqlite3_value_bytes(argv[0]);
130952 const char *zSep = (const char*)sqlite3_value_text(argv[0]);
130953 if( zSep==0 ) return;
130954 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
130955}
130956
130957
130958#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
130959/*
130960** The "unknown" function is automatically substituted in place of
130961** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
130962** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
130963** When the "sqlite3" command-line shell is built using this functionality,
130964** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
130965** involving application-defined functions to be examined in a generic
130966** sqlite3 shell.
130967*/
130968static void unknownFunc(
130969 sqlite3_context *context,
130970 int argc,
130971 sqlite3_value **argv
130972){
130973 /* no-op */
130974 (void)context;
130975 (void)argc;
130976 (void)argv;
130977}
130978#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
130979
130980
130981/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
130982** is only available if the SQLITE_SOUNDEX compile-time option is used
130983** when SQLite is built.
130984*/
130985#ifdef SQLITE_SOUNDEX
130986/*
130987** Compute the soundex encoding of a word.
130988**
130989** IMP: R-59782-00072 The soundex(X) function returns a string that is the
130990** soundex encoding of the string X.
130991*/
130992static void soundexFunc(
130993 sqlite3_context *context,
130994 int argc,
130995 sqlite3_value **argv
130996){
130997 char zResult[8];
130998 const u8 *zIn;
130999 int i, j;
131000 static const unsigned char iCode[] = {
131001 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131002 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131003 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131004 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131005 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
131006 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
131007 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
131008 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
131009 };
131010 assert( argc==1 )((void) (0));
131011 zIn = (u8*)sqlite3_value_text(argv[0]);
131012 if( zIn==0 ) zIn = (u8*)"";
131013 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i])(sqlite3CtypeMap[(unsigned char)(zIn[i])]&0x02); i++){}
131014 if( zIn[i] ){
131015 u8 prevcode = iCode[zIn[i]&0x7f];
131016 zResult[0] = sqlite3Toupper(zIn[i])((zIn[i])&~(sqlite3CtypeMap[(unsigned char)(zIn[i])]&
0x20))
;
131017 for(j=1; j<4 && zIn[i]; i++){
131018 int code = iCode[zIn[i]&0x7f];
131019 if( code>0 ){
131020 if( code!=prevcode ){
131021 prevcode = code;
131022 zResult[j++] = code + '0';
131023 }
131024 }else{
131025 prevcode = 0;
131026 }
131027 }
131028 while( j<4 ){
131029 zResult[j++] = '0';
131030 }
131031 zResult[j] = 0;
131032 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
131033 }else{
131034 /* IMP: R-64894-50321 The string "?000" is returned if the argument
131035 ** is NULL or contains no ASCII alphabetic characters. */
131036 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
131037 }
131038}
131039#endif /* SQLITE_SOUNDEX */
131040
131041#ifndef SQLITE_OMIT_LOAD_EXTENSION1
131042/*
131043** A function that loads a shared-library extension then returns NULL.
131044*/
131045static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
131046 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
131047 const char *zProc;
131048 sqlite3 *db = sqlite3_context_db_handle(context);
131049 char *zErrMsg = 0;
131050
131051 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
131052 ** flag is set. See the sqlite3_enable_load_extension() API.
131053 */
131054 if( (db->flags & SQLITE_LoadExtFunc0x00020000)==0 ){
131055 sqlite3_result_error(context, "not authorized", -1);
131056 return;
131057 }
131058
131059 if( argc==2 ){
131060 zProc = (const char *)sqlite3_value_text(argv[1]);
131061 }else{
131062 zProc = 0;
131063 }
131064 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
131065 sqlite3_result_error(context, zErrMsg, -1);
131066 sqlite3_free(zErrMsg);
131067 }
131068}
131069#endif
131070
131071
131072/*
131073** An instance of the following structure holds the context of a
131074** sum() or avg() aggregate computation.
131075*/
131076typedef struct SumCtx SumCtx;
131077struct SumCtx {
131078 double rSum; /* Running sum as as a double */
131079 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
131080 i64 iSum; /* Running sum as a signed integer */
131081 i64 cnt; /* Number of elements summed */
131082 u8 approx; /* True if any non-integer value was input to the sum */
131083 u8 ovrfl; /* Integer overflow seen */
131084};
131085
131086/*
131087** Do one step of the Kahan-Babushka-Neumaier summation.
131088**
131089** https://en.wikipedia.org/wiki/Kahan_summation_algorithm
131090**
131091** Variables are marked "volatile" to defeat c89 x86 floating point
131092** optimizations can mess up this algorithm.
131093*/
131094static void kahanBabuskaNeumaierStep(
131095 volatile SumCtx *pSum,
131096 volatile double r
131097){
131098 volatile double s = pSum->rSum;
131099 volatile double t = s + r;
131100 if( fabs(s) > fabs(r) ){
131101 pSum->rErr += (s - t) + r;
131102 }else{
131103 pSum->rErr += (r - t) + s;
131104 }
131105 pSum->rSum = t;
131106}
131107
131108/*
131109** Add a (possibly large) integer to the running sum.
131110*/
131111static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){
131112 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
131113 i64 iBig, iSm;
131114 iSm = iVal % 16384;
131115 iBig = iVal - iSm;
131116 kahanBabuskaNeumaierStep(pSum, iBig);
131117 kahanBabuskaNeumaierStep(pSum, iSm);
131118 }else{
131119 kahanBabuskaNeumaierStep(pSum, (double)iVal);
131120 }
131121}
131122
131123/*
131124** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
131125*/
131126static void kahanBabuskaNeumaierInit(
131127 volatile SumCtx *p,
131128 i64 iVal
131129){
131130 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
131131 i64 iSm = iVal % 16384;
131132 p->rSum = (double)(iVal - iSm);
131133 p->rErr = (double)iSm;
131134 }else{
131135 p->rSum = (double)iVal;
131136 p->rErr = 0.0;
131137 }
131138}
131139
131140/*
131141** Routines used to compute the sum, average, and total.
131142**
131143** The SUM() function follows the (broken) SQL standard which means
131144** that it returns NULL if it sums over no inputs. TOTAL returns
131145** 0.0 in that case. In addition, TOTAL always returns a float where
131146** SUM might return an integer if it never encounters a floating point
131147** value. TOTAL never fails, but SUM might through an exception if
131148** it overflows an integer.
131149*/
131150static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
131151 SumCtx *p;
131152 int type;
131153 assert( argc==1 )((void) (0));
131154 UNUSED_PARAMETER(argc)(void)(argc);
131155 p = sqlite3_aggregate_context(context, sizeof(*p));
131156 type = sqlite3_value_numeric_type(argv[0]);
131157 if( p && type!=SQLITE_NULL5 ){
131158 p->cnt++;
131159 if( p->approx==0 ){
131160 if( type!=SQLITE_INTEGER1 ){
131161 kahanBabuskaNeumaierInit(p, p->iSum);
131162 p->approx = 1;
131163 kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
131164 }else{
131165 i64 x = p->iSum;
131166 if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){
131167 p->iSum = x;
131168 }else{
131169 p->ovrfl = 1;
131170 kahanBabuskaNeumaierInit(p, p->iSum);
131171 p->approx = 1;
131172 kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0]));
131173 }
131174 }
131175 }else{
131176 if( type==SQLITE_INTEGER1 ){
131177 kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0]));
131178 }else{
131179 p->ovrfl = 0;
131180 kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0]));
131181 }
131182 }
131183 }
131184}
131185#ifndef SQLITE_OMIT_WINDOWFUNC
131186static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
131187 SumCtx *p;
131188 int type;
131189 assert( argc==1 )((void) (0));
131190 UNUSED_PARAMETER(argc)(void)(argc);
131191 p = sqlite3_aggregate_context(context, sizeof(*p));
131192 type = sqlite3_value_numeric_type(argv[0]);
131193 /* p is always non-NULL because sumStep() will have been called first
131194 ** to initialize it */
131195 if( ALWAYS(p)(p) && type!=SQLITE_NULL5 ){
131196 assert( p->cnt>0 )((void) (0));
131197 p->cnt--;
131198 if( !p->approx ){
131199 p->iSum -= sqlite3_value_int64(argv[0]);
131200 }else if( type==SQLITE_INTEGER1 ){
131201 i64 iVal = sqlite3_value_int64(argv[0]);
131202 if( iVal!=SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
131203 kahanBabuskaNeumaierStepInt64(p, -iVal);
131204 }else{
131205 kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
131206 kahanBabuskaNeumaierStepInt64(p, 1);
131207 }
131208 }else{
131209 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
131210 }
131211 }
131212}
131213#else
131214# define sumInverse 0
131215#endif /* SQLITE_OMIT_WINDOWFUNC */
131216static void sumFinalize(sqlite3_context *context){
131217 SumCtx *p;
131218 p = sqlite3_aggregate_context(context, 0);
131219 if( p && p->cnt>0 ){
131220 if( p->approx ){
131221 if( p->ovrfl ){
131222 sqlite3_result_error(context,"integer overflow",-1);
131223 }else if( !sqlite3IsOverflow(p->rErr) ){
131224 sqlite3_result_double(context, p->rSum+p->rErr);
131225 }else{
131226 sqlite3_result_double(context, p->rSum);
131227 }
131228 }else{
131229 sqlite3_result_int64(context, p->iSum);
131230 }
131231 }
131232}
131233static void avgFinalize(sqlite3_context *context){
131234 SumCtx *p;
131235 p = sqlite3_aggregate_context(context, 0);
131236 if( p && p->cnt>0 ){
131237 double r;
131238 if( p->approx ){
131239 r = p->rSum;
131240 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
131241 }else{
131242 r = (double)(p->iSum);
131243 }
131244 sqlite3_result_double(context, r/(double)p->cnt);
131245 }
131246}
131247static void totalFinalize(sqlite3_context *context){
131248 SumCtx *p;
131249 double r = 0.0;
131250 p = sqlite3_aggregate_context(context, 0);
131251 if( p ){
131252 if( p->approx ){
131253 r = p->rSum;
131254 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
131255 }else{
131256 r = (double)(p->iSum);
131257 }
131258 }
131259 sqlite3_result_double(context, r);
131260}
131261
131262/*
131263** The following structure keeps track of state information for the
131264** count() aggregate function.
131265*/
131266typedef struct CountCtx CountCtx;
131267struct CountCtx {
131268 i64 n;
131269#ifdef SQLITE_DEBUG
131270 int bInverse; /* True if xInverse() ever called */
131271#endif
131272};
131273
131274/*
131275** Routines to implement the count() aggregate function.
131276*/
131277static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
131278 CountCtx *p;
131279 p = sqlite3_aggregate_context(context, sizeof(*p));
131280 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && p ){
131281 p->n++;
131282 }
131283
131284#ifndef SQLITE_OMIT_DEPRECATED1
131285 /* The sqlite3_aggregate_count() function is deprecated. But just to make
131286 ** sure it still operates correctly, verify that its count agrees with our
131287 ** internal count when using count(*) and when the total count can be
131288 ** expressed as a 32-bit integer. */
131289 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse((void) (0))
131290 || p->n==sqlite3_aggregate_count(context) )((void) (0));
131291#endif
131292}
131293static void countFinalize(sqlite3_context *context){
131294 CountCtx *p;
131295 p = sqlite3_aggregate_context(context, 0);
131296 sqlite3_result_int64(context, p ? p->n : 0);
131297}
131298#ifndef SQLITE_OMIT_WINDOWFUNC
131299static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
131300 CountCtx *p;
131301 p = sqlite3_aggregate_context(ctx, sizeof(*p));
131302 /* p is always non-NULL since countStep() will have been called first */
131303 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && ALWAYS(p)(p) ){
131304 p->n--;
131305#ifdef SQLITE_DEBUG
131306 p->bInverse = 1;
131307#endif
131308 }
131309}
131310#else
131311# define countInverse 0
131312#endif /* SQLITE_OMIT_WINDOWFUNC */
131313
131314/*
131315** Routines to implement min() and max() aggregate functions.
131316*/
131317static void minmaxStep(
131318 sqlite3_context *context,
131319 int NotUsed,
131320 sqlite3_value **argv
131321){
131322 Mem *pArg = (Mem *)argv[0];
131323 Mem *pBest;
131324 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
131325
131326 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
131327 if( !pBest ) return;
131328
131329 if( sqlite3_value_type(pArg)==SQLITE_NULL5 ){
131330 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
131331 }else if( pBest->flags ){
131332 int max;
131333 int cmp;
131334 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
131335 /* This step function is used for both the min() and max() aggregates,
131336 ** the only difference between the two being that the sense of the
131337 ** comparison is inverted. For the max() aggregate, the
131338 ** sqlite3_user_data() function returns (void *)-1. For min() it
131339 ** returns (void *)db, where db is the sqlite3* database pointer.
131340 ** Therefore the next statement sets variable 'max' to 1 for the max()
131341 ** aggregate, or 0 for min().
131342 */
131343 max = sqlite3_user_data(context)!=0;
131344 cmp = sqlite3MemCompare(pBest, pArg, pColl);
131345 if( (max && cmp<0) || (!max && cmp>0) ){
131346 sqlite3VdbeMemCopy(pBest, pArg);
131347 }else{
131348 sqlite3SkipAccumulatorLoad(context);
131349 }
131350 }else{
131351 pBest->db = sqlite3_context_db_handle(context);
131352 sqlite3VdbeMemCopy(pBest, pArg);
131353 }
131354}
131355static void minMaxValueFinalize(sqlite3_context *context, int bValue){
131356 sqlite3_value *pRes;
131357 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
131358 if( pRes ){
131359 if( pRes->flags ){
131360 sqlite3_result_value(context, pRes);
131361 }
131362 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
131363 }
131364}
131365#ifndef SQLITE_OMIT_WINDOWFUNC
131366static void minMaxValue(sqlite3_context *context){
131367 minMaxValueFinalize(context, 1);
131368}
131369#else
131370# define minMaxValue 0
131371#endif /* SQLITE_OMIT_WINDOWFUNC */
131372static void minMaxFinalize(sqlite3_context *context){
131373 minMaxValueFinalize(context, 0);
131374}
131375
131376/*
131377** group_concat(EXPR, ?SEPARATOR?)
131378** string_agg(EXPR, SEPARATOR)
131379**
131380** Content is accumulated in GroupConcatCtx.str with the SEPARATOR
131381** coming before the EXPR value, except for the first entry which
131382** omits the SEPARATOR.
131383**
131384** It is tragic that the SEPARATOR goes before the EXPR string. The
131385** groupConcatInverse() implementation would have been easier if the
131386** SEPARATOR were appended after EXPR. And the order is undocumented,
131387** so we could change it, in theory. But the old behavior has been
131388** around for so long that we dare not, for fear of breaking something.
131389*/
131390typedef struct {
131391 StrAccum str; /* The accumulated concatenation */
131392#ifndef SQLITE_OMIT_WINDOWFUNC
131393 int nAccum; /* Number of strings presently concatenated */
131394 int nFirstSepLength; /* Used to detect separator length change */
131395 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
131396 ** stored as actually incorporated into presently accumulated result.
131397 ** (Hence, its slots in use number nAccum-1 between method calls.)
131398 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
131399 */
131400 int *pnSepLengths;
131401#endif
131402} GroupConcatCtx;
131403
131404static void groupConcatStep(
131405 sqlite3_context *context,
131406 int argc,
131407 sqlite3_value **argv
131408){
131409 const char *zVal;
131410 GroupConcatCtx *pGCC;
131411 const char *zSep;
131412 int nVal, nSep;
131413 assert( argc==1 || argc==2 )((void) (0));
131414 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
131415 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
131416 if( pGCC ){
131417 sqlite3 *db = sqlite3_context_db_handle(context);
131418 int firstTerm = pGCC->str.mxAlloc==0;
131419 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH0];
131420 if( argc==1 ){
131421 if( !firstTerm ){
131422 sqlite3_str_appendchar(&pGCC->str, 1, ',');
131423 }
131424#ifndef SQLITE_OMIT_WINDOWFUNC
131425 else{
131426 pGCC->nFirstSepLength = 1;
131427 }
131428#endif
131429 }else if( !firstTerm ){
131430 zSep = (char*)sqlite3_value_text(argv[1]);
131431 nSep = sqlite3_value_bytes(argv[1]);
131432 if( zSep ){
131433 sqlite3_str_append(&pGCC->str, zSep, nSep);
131434 }
131435#ifndef SQLITE_OMIT_WINDOWFUNC
131436 else{
131437 nSep = 0;
131438 }
131439 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
131440 int *pnsl = pGCC->pnSepLengths;
131441 if( pnsl == 0 ){
131442 /* First separator length variation seen, start tracking them. */
131443 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
131444 if( pnsl!=0 ){
131445 int i = 0, nA = pGCC->nAccum-1;
131446 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
131447 }
131448 }else{
131449 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
131450 }
131451 if( pnsl!=0 ){
131452 if( ALWAYS(pGCC->nAccum>0)(pGCC->nAccum>0) ){
131453 pnsl[pGCC->nAccum-1] = nSep;
131454 }
131455 pGCC->pnSepLengths = pnsl;
131456 }else{
131457 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM7);
131458 }
131459 }
131460#endif
131461 }
131462#ifndef SQLITE_OMIT_WINDOWFUNC
131463 else{
131464 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
131465 }
131466 pGCC->nAccum += 1;
131467#endif
131468 zVal = (char*)sqlite3_value_text(argv[0]);
131469 nVal = sqlite3_value_bytes(argv[0]);
131470 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
131471 }
131472}
131473
131474#ifndef SQLITE_OMIT_WINDOWFUNC
131475static void groupConcatInverse(
131476 sqlite3_context *context,
131477 int argc,
131478 sqlite3_value **argv
131479){
131480 GroupConcatCtx *pGCC;
131481 assert( argc==1 || argc==2 )((void) (0));
131482 (void)argc; /* Suppress unused parameter warning */
131483 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
131484 pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC));
131485 /* pGCC is always non-NULL since groupConcatStep() will have always
131486 ** run first to initialize it */
131487 if( ALWAYS(pGCC)(pGCC) ){
131488 int nVS; /* Number of characters to remove */
131489 /* Must call sqlite3_value_text() to convert the argument into text prior
131490 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
131491 (void)sqlite3_value_text(argv[0]);
131492 nVS = sqlite3_value_bytes(argv[0]);
131493 pGCC->nAccum -= 1;
131494 if( pGCC->pnSepLengths!=0 ){
131495 assert(pGCC->nAccum >= 0)((void) (0));
131496 if( pGCC->nAccum>0 ){
131497 nVS += *pGCC->pnSepLengths;
131498 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
131499 (pGCC->nAccum-1)*sizeof(int));
131500 }
131501 }else{
131502 /* If removing single accumulated string, harmlessly over-do. */
131503 nVS += pGCC->nFirstSepLength;
131504 }
131505 if( nVS>=(int)pGCC->str.nChar ){
131506 pGCC->str.nChar = 0;
131507 }else{
131508 pGCC->str.nChar -= nVS;
131509 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
131510 }
131511 if( pGCC->str.nChar==0 ){
131512 pGCC->str.mxAlloc = 0;
131513 sqlite3_free(pGCC->pnSepLengths);
131514 pGCC->pnSepLengths = 0;
131515 }
131516 }
131517}
131518#else
131519# define groupConcatInverse 0
131520#endif /* SQLITE_OMIT_WINDOWFUNC */
131521static void groupConcatFinalize(sqlite3_context *context){
131522 GroupConcatCtx *pGCC
131523 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
131524 if( pGCC ){
131525 sqlite3ResultStrAccum(context, &pGCC->str);
131526#ifndef SQLITE_OMIT_WINDOWFUNC
131527 sqlite3_free(pGCC->pnSepLengths);
131528#endif
131529 }
131530}
131531#ifndef SQLITE_OMIT_WINDOWFUNC
131532static void groupConcatValue(sqlite3_context *context){
131533 GroupConcatCtx *pGCC
131534 = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0);
131535 if( pGCC ){
131536 StrAccum *pAccum = &pGCC->str;
131537 if( pAccum->accError==SQLITE_TOOBIG18 ){
131538 sqlite3_result_error_toobig(context);
131539 }else if( pAccum->accError==SQLITE_NOMEM7 ){
131540 sqlite3_result_error_nomem(context);
131541 }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
131542 sqlite3_result_text(context, "", 1, SQLITE_STATIC((sqlite3_destructor_type)0));
131543 }else{
131544 const char *zText = sqlite3_str_value(pAccum);
131545 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
131546 }
131547 }
131548}
131549#else
131550# define groupConcatValue 0
131551#endif /* SQLITE_OMIT_WINDOWFUNC */
131552
131553/*
131554** This routine does per-connection function registration. Most
131555** of the built-in functions above are part of the global function set.
131556** This routine only deals with those that are not global.
131557*/
131558SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
131559 int rc = sqlite3_overload_function(db, "MATCH", 2);
131560 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK )((void) (0));
131561 if( rc==SQLITE_NOMEM7 ){
131562 sqlite3OomFault(db);
131563 }
131564}
131565
131566/*
131567** Re-register the built-in LIKE functions. The caseSensitive
131568** parameter determines whether or not the LIKE operator is case
131569** sensitive.
131570*/
131571SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
131572 FuncDef *pDef;
131573 struct compareInfo *pInfo;
131574 int flags;
131575 int nArg;
131576 if( caseSensitive ){
131577 pInfo = (struct compareInfo*)&likeInfoAlt;
131578 flags = SQLITE_FUNC_LIKE0x0004 | SQLITE_FUNC_CASE0x0008;
131579 }else{
131580 pInfo = (struct compareInfo*)&likeInfoNorm;
131581 flags = SQLITE_FUNC_LIKE0x0004;
131582 }
131583 for(nArg=2; nArg<=3; nArg++){
131584 sqlite3CreateFunc(db, "like", nArg, SQLITE_UTF81, pInfo, likeFunc,
131585 0, 0, 0, 0, 0);
131586 pDef = sqlite3FindFunction(db, "like", nArg, SQLITE_UTF81, 0);
131587 pDef->funcFlags |= flags;
131588 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE0x00200000;
131589 }
131590}
131591
131592/*
131593** pExpr points to an expression which implements a function. If
131594** it is appropriate to apply the LIKE optimization to that function
131595** then set aWc[0] through aWc[2] to the wildcard characters and the
131596** escape character and then return TRUE. If the function is not a
131597** LIKE-style function then return FALSE.
131598**
131599** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
131600** operator if c is a string literal that is exactly one byte in length.
131601** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
131602** no ESCAPE clause.
131603**
131604** *pIsNocase is set to true if uppercase and lowercase are equivalent for
131605** the function (default for LIKE). If the function makes the distinction
131606** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
131607** false.
131608*/
131609SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
131610 FuncDef *pDef;
131611 int nExpr;
131612 assert( pExpr!=0 )((void) (0));
131613 assert( pExpr->op==TK_FUNCTION )((void) (0));
131614 assert( ExprUseXList(pExpr) )((void) (0));
131615 if( !pExpr->x.pList ){
131616 return 0;
131617 }
131618 nExpr = pExpr->x.pList->nExpr;
131619 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
131620 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF81, 0);
131621#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
131622 if( pDef==0 ) return 0;
131623#endif
131624 if( NEVER(pDef==0)(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE0x0004)==0 ){
131625 return 0;
131626 }
131627
131628 /* The memcpy() statement assumes that the wildcard characters are
131629 ** the first three statements in the compareInfo structure. The
131630 ** asserts() that follow verify that assumption
131631 */
131632 memcpy(aWc, pDef->pUserData, 3);
131633 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll )((void) (0));
131634 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne )((void) (0));
131635 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet )((void) (0));
131636
131637 if( nExpr<3 ){
131638 aWc[3] = 0;
131639 }else{
131640 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
131641 char *zEscape;
131642 if( pEscape->op!=TK_STRING118 ) return 0;
131643 assert( !ExprHasProperty(pEscape, EP_IntValue) )((void) (0));
131644 zEscape = pEscape->u.zToken;
131645 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
131646 if( zEscape[0]==aWc[0] ) return 0;
131647 if( zEscape[0]==aWc[1] ) return 0;
131648 aWc[3] = zEscape[0];
131649 }
131650
131651 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE0x0008)==0;
131652 return 1;
131653}
131654
131655/* Mathematical Constants */
131656#ifndef M_PI3.14159265358979323846
131657# define M_PI3.14159265358979323846 3.141592653589793238462643383279502884
131658#endif
131659#ifndef M_LN102.30258509299404568402
131660# define M_LN102.30258509299404568402 2.302585092994045684017991454684364208
131661#endif
131662#ifndef M_LN20.69314718055994530942
131663# define M_LN20.69314718055994530942 0.693147180559945309417232121458176568
131664#endif
131665
131666
131667/* Extra math functions that require linking with -lm
131668*/
131669#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
131670/*
131671** Implementation SQL functions:
131672**
131673** ceil(X)
131674** ceiling(X)
131675** floor(X)
131676**
131677** The sqlite3_user_data() pointer is a pointer to the libm implementation
131678** of the underlying C function.
131679*/
131680static void ceilingFunc(
131681 sqlite3_context *context,
131682 int argc,
131683 sqlite3_value **argv
131684){
131685 assert( argc==1 )((void) (0));
131686 switch( sqlite3_value_numeric_type(argv[0]) ){
131687 case SQLITE_INTEGER1: {
131688 sqlite3_result_int64(context, sqlite3_value_int64(argv[0]));
131689 break;
131690 }
131691 case SQLITE_FLOAT2: {
131692 double (*x)(double) = (double(*)(double))sqlite3_user_data(context);
131693 sqlite3_result_double(context, x(sqlite3_value_double(argv[0])));
131694 break;
131695 }
131696 default: {
131697 break;
131698 }
131699 }
131700}
131701
131702/*
131703** On some systems, ceil() and floor() are intrinsic function. You are
131704** unable to take a pointer to these functions. Hence, we here wrap them
131705** in our own actual functions.
131706*/
131707static double xCeil(double x){ return ceil(x); }
131708static double xFloor(double x){ return floor(x); }
131709
131710/*
131711** Some systems do not have log2() and log10() in their standard math
131712** libraries.
131713*/
131714#if defined(HAVE_LOG10) && HAVE_LOG10==0
131715# define log10(X) (0.4342944819032517867*log(X))
131716#endif
131717#if defined(HAVE_LOG2) && HAVE_LOG2==0
131718# define log2(X) (1.442695040888963456*log(X))
131719#endif
131720
131721
131722/*
131723** Implementation of SQL functions:
131724**
131725** ln(X) - natural logarithm
131726** log(X) - log X base 10
131727** log10(X) - log X base 10
131728** log(B,X) - log X base B
131729*/
131730static void logFunc(
131731 sqlite3_context *context,
131732 int argc,
131733 sqlite3_value **argv
131734){
131735 double x, b, ans;
131736 assert( argc==1 || argc==2 )((void) (0));
131737 switch( sqlite3_value_numeric_type(argv[0]) ){
131738 case SQLITE_INTEGER1:
131739 case SQLITE_FLOAT2:
131740 x = sqlite3_value_double(argv[0]);
131741 if( x<=0.0 ) return;
131742 break;
131743 default:
131744 return;
131745 }
131746 if( argc==2 ){
131747 switch( sqlite3_value_numeric_type(argv[0]) ){
131748 case SQLITE_INTEGER1:
131749 case SQLITE_FLOAT2:
131750 b = log(x);
131751 if( b<=0.0 ) return;
131752 x = sqlite3_value_double(argv[1]);
131753 if( x<=0.0 ) return;
131754 break;
131755 default:
131756 return;
131757 }
131758 ans = log(x)/b;
131759 }else{
131760 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context))) ){
131761 case 1:
131762 ans = log10(x);
131763 break;
131764 case 2:
131765 ans = log2(x);
131766 break;
131767 default:
131768 ans = log(x);
131769 break;
131770 }
131771 }
131772 sqlite3_result_double(context, ans);
131773}
131774
131775/*
131776** Functions to converts degrees to radians and radians to degrees.
131777*/
131778static double degToRad(double x){ return x*(M_PI3.14159265358979323846/180.0); }
131779static double radToDeg(double x){ return x*(180.0/M_PI3.14159265358979323846); }
131780
131781/*
131782** Implementation of 1-argument SQL math functions:
131783**
131784** exp(X) - Compute e to the X-th power
131785*/
131786static void math1Func(
131787 sqlite3_context *context,
131788 int argc,
131789 sqlite3_value **argv
131790){
131791 int type0;
131792 double v0, ans;
131793 double (*x)(double);
131794 assert( argc==1 )((void) (0));
131795 type0 = sqlite3_value_numeric_type(argv[0]);
131796 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
131797 v0 = sqlite3_value_double(argv[0]);
131798 x = (double(*)(double))sqlite3_user_data(context);
131799 ans = x(v0);
131800 sqlite3_result_double(context, ans);
131801}
131802
131803/*
131804** Implementation of 2-argument SQL math functions:
131805**
131806** power(X,Y) - Compute X to the Y-th power
131807*/
131808static void math2Func(
131809 sqlite3_context *context,
131810 int argc,
131811 sqlite3_value **argv
131812){
131813 int type0, type1;
131814 double v0, v1, ans;
131815 double (*x)(double,double);
131816 assert( argc==2 )((void) (0));
131817 type0 = sqlite3_value_numeric_type(argv[0]);
131818 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
131819 type1 = sqlite3_value_numeric_type(argv[1]);
131820 if( type1!=SQLITE_INTEGER1 && type1!=SQLITE_FLOAT2 ) return;
131821 v0 = sqlite3_value_double(argv[0]);
131822 v1 = sqlite3_value_double(argv[1]);
131823 x = (double(*)(double,double))sqlite3_user_data(context);
131824 ans = x(v0, v1);
131825 sqlite3_result_double(context, ans);
131826}
131827
131828/*
131829** Implementation of 0-argument pi() function.
131830*/
131831static void piFunc(
131832 sqlite3_context *context,
131833 int argc,
131834 sqlite3_value **argv
131835){
131836 assert( argc==0 )((void) (0));
131837 (void)argv;
131838 sqlite3_result_double(context, M_PI3.14159265358979323846);
131839}
131840
131841#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
131842
131843/*
131844** Implementation of sign(X) function.
131845*/
131846static void signFunc(
131847 sqlite3_context *context,
131848 int argc,
131849 sqlite3_value **argv
131850){
131851 int type0;
131852 double x;
131853 UNUSED_PARAMETER(argc)(void)(argc);
131854 assert( argc==1 )((void) (0));
131855 type0 = sqlite3_value_numeric_type(argv[0]);
131856 if( type0!=SQLITE_INTEGER1 && type0!=SQLITE_FLOAT2 ) return;
131857 x = sqlite3_value_double(argv[0]);
131858 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
131859}
131860
131861#ifdef SQLITE_DEBUG
131862/*
131863** Implementation of fpdecode(x,y,z) function.
131864**
131865** x is a real number that is to be decoded. y is the precision.
131866** z is the maximum real precision. Return a string that shows the
131867** results of the sqlite3FpDecode() function.
131868**
131869** Used for testing and debugging only, specifically testing and debugging
131870** of the sqlite3FpDecode() function. This SQL function does not appear
131871** in production builds. This function is not an API and is subject to
131872** modification or removal in future versions of SQLite.
131873*/
131874static void fpdecodeFunc(
131875 sqlite3_context *context,
131876 int argc,
131877 sqlite3_value **argv
131878){
131879 FpDecode s;
131880 double x;
131881 int y, z;
131882 char zBuf[100];
131883 UNUSED_PARAMETER(argc)(void)(argc);
131884 assert( argc==3 )((void) (0));
131885 x = sqlite3_value_double(argv[0]);
131886 y = sqlite3_value_int(argv[1]);
131887 z = sqlite3_value_int(argv[2]);
131888 if( z<=0 ) z = 1;
131889 sqlite3FpDecode(&s, x, y, z);
131890 if( s.isSpecial==2 ){
131891 sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
131892 }else{
131893 sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
131894 }
131895 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
131896}
131897#endif /* SQLITE_DEBUG */
131898
131899#ifdef SQLITE_DEBUG
131900/*
131901** Implementation of parseuri(uri,flags) function.
131902**
131903** Required Arguments:
131904** "uri" The URI to parse.
131905** "flags" Bitmask of flags, as if to sqlite3_open_v2().
131906**
131907** Additional arguments beyond the first two make calls to
131908** sqlite3_uri_key() for integers and sqlite3_uri_parameter for
131909** anything else.
131910**
131911** The result is a string showing the results of calling sqlite3ParseUri().
131912**
131913** Used for testing and debugging only, specifically testing and debugging
131914** of the sqlite3ParseUri() function. This SQL function does not appear
131915** in production builds. This function is not an API and is subject to
131916** modification or removal in future versions of SQLite.
131917*/
131918static void parseuriFunc(
131919 sqlite3_context *ctx,
131920 int argc,
131921 sqlite3_value **argv
131922){
131923 sqlite3_str *pResult;
131924 const char *zVfs;
131925 const char *zUri;
131926 unsigned int flgs;
131927 int rc;
131928 sqlite3_vfs *pVfs = 0;
131929 char *zFile = 0;
131930 char *zErr = 0;
131931
131932 if( argc<2 ) return;
131933 pVfs = sqlite3_vfs_find(0);
131934 assert( pVfs )((void) (0));
131935 zVfs = pVfs->zName;
131936 zUri = (const char*)sqlite3_value_text(argv[0]);
131937 if( zUri==0 ) return;
131938 flgs = (unsigned int)sqlite3_value_int(argv[1]);
131939 rc = sqlite3ParseUri(zVfs, zUri, &flgs, &pVfs, &zFile, &zErr);
131940 pResult = sqlite3_str_new(0);
131941 if( pResult ){
131942 int i;
131943 sqlite3_str_appendf(pResult, "rc=%d", rc);
131944 sqlite3_str_appendf(pResult, ", flags=0x%x", flgs);
131945 sqlite3_str_appendf(pResult, ", vfs=%Q", pVfs ? pVfs->zName: 0);
131946 sqlite3_str_appendf(pResult, ", err=%Q", zErr);
131947 sqlite3_str_appendf(pResult, ", file=%Q", zFile);
131948 if( zFile ){
131949 const char *z = zFile;
131950 z += sqlite3Strlen30(z)+1;
131951 while( z[0] ){
131952 sqlite3_str_appendf(pResult, ", %Q", z);
131953 z += sqlite3Strlen30(z)+1;
131954 }
131955 for(i=2; i<argc; i++){
131956 const char *zArg;
131957 if( sqlite3_value_type(argv[i])==SQLITE_INTEGER1 ){
131958 int k = sqlite3_value_int(argv[i]);
131959 sqlite3_str_appendf(pResult, ", '%d:%q'",k,sqlite3_uri_key(zFile, k));
131960 }else if( (zArg = (const char*)sqlite3_value_text(argv[i]))!=0 ){
131961 sqlite3_str_appendf(pResult, ", '%q:%q'",
131962 zArg, sqlite3_uri_parameter(zFile,zArg));
131963 }else{
131964 sqlite3_str_appendf(pResult, ", NULL");
131965 }
131966 }
131967 }
131968 sqlite3_result_text(ctx, sqlite3_str_finish(pResult), -1, sqlite3_free);
131969 }
131970 sqlite3_free_filename(zFile);
131971 sqlite3_free(zErr);
131972}
131973#endif /* SQLITE_DEBUG */
131974
131975/*
131976** All of the FuncDef structures in the aBuiltinFunc[] array above
131977** to the global function hash table. This occurs at start-time (as
131978** a consequence of calling sqlite3_initialize()).
131979**
131980** After this routine runs
131981*/
131982SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void){
131983 /*
131984 ** The following array holds FuncDef structures for all of the functions
131985 ** defined in this file.
131986 **
131987 ** The array cannot be constant since changes are made to the
131988 ** FuncDef.pHash elements at start-time. The elements of this array
131989 ** are read-only after initialization is complete.
131990 **
131991 ** For peak efficiency, put the most frequently used function last.
131992 */
131993 static FuncDef aBuiltinFunc[] = {
131994/***** Functions only available with SQLITE_TESTCTRL_INTERNAL_FUNCTIONS *****/
131995#if !defined(SQLITE_UNTESTABLE)
131996 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} }
,
131997 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} }
,
131998 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} }
,
131999 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} }
,
132000#endif /* !defined(SQLITE_UNTESTABLE) */
132001/***** Regular functions *****/
132002#ifdef SQLITE_SOUNDEX
132003 FUNCTION(soundex, 1, 0, 0, soundexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, soundexFunc, 0, 0, 0, "soundex", {0} }
,
132004#endif
132005#ifndef SQLITE_OMIT_LOAD_EXTENSION1
132006 SFUNCTION(load_extension, 1, 0, 0, loadExt ){1, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int)(0
)), 0, loadExt, 0, 0, 0, "load_extension", {0} }
,
132007 SFUNCTION(load_extension, 2, 0, 0, loadExt ){2, 0x00800000|1|0x000080000|0x00200000, ((void*)(long int)(0
)), 0, loadExt, 0, 0, 0, "load_extension", {0} }
,
132008#endif
132009#if SQLITE_USER_AUTHENTICATION
132010 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, sqlite3CryptFunc, 0, 0, 0, "sqlite_crypt", {0} }
,
132011#endif
132012#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
132013 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ){1, 0x00800000|0x2000|1, 0, 0, compileoptionusedFunc, 0, 0, 0
, "sqlite_compileoption_used", {0} }
,
132014 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ){1, 0x00800000|0x2000|1, 0, 0, compileoptiongetFunc, 0, 0, 0,
"sqlite_compileoption_get", {0} }
,
132015#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
132016 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} }
,
132017 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} }
,
132018 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} }
,
132019#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
132020 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} }
,
132021#endif
132022 FUNCTION(ltrim, 1, 1, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, trimFunc, 0, 0, 0, "ltrim", {0} }
,
132023 FUNCTION(ltrim, 2, 1, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, trimFunc, 0, 0, 0, "ltrim", {0} }
,
132024 FUNCTION(rtrim, 1, 2, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, trimFunc, 0, 0, 0, "rtrim", {0} }
,
132025 FUNCTION(rtrim, 2, 2, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, trimFunc, 0, 0, 0, "rtrim", {0} }
,
132026 FUNCTION(trim, 1, 3, 0, trimFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0
, trimFunc, 0, 0, 0, "trim", {0} }
,
132027 FUNCTION(trim, 2, 3, 0, trimFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0
, trimFunc, 0, 0, 0, "trim", {0} }
,
132028 FUNCTION(min, -1, 0, 1, minmaxFunc ){-1, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)),
0, minmaxFunc, 0, 0, 0, "min", {0} }
,
132029 FUNCTION(min, 0, 0, 1, 0 ){0, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "min", {0} }
,
132030 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}}
132031 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(0)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"min", {0}}
,
132032 FUNCTION(max, -1, 1, 1, minmaxFunc ){-1, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(1)),
0, minmaxFunc, 0, 0, 0, "max", {0} }
,
132033 FUNCTION(max, 0, 1, 1, 0 ){0, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(1)), 0
, 0, 0, 0, 0, "max", {0} }
,
132034 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}}
132035 SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(1*0x0020)|0x1000|0x08000000, ((void*)(long int
)(1)), 0, minmaxStep,minMaxFinalize,minMaxValue,0,"max", {0}}
,
132036 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} }
,
132037 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} }
132038 SQLITE_FUNC_TYPEOF|SQLITE_SUBTYPE){1, 0x00800000| 0x0800|1|(0*0x0020)|0x0080|0x000100000, ((void
*)(long int)(0)), 0, subtypeFunc, 0, 0, 0, "subtype", {0} }
,
132039 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} }
,
132040 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} }
,
132041 FUNCTION(instr, 2, 0, 0, instrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, instrFunc, 0, 0, 0, "instr", {0} }
,
132042 FUNCTION(printf, -1, 0, 0, printfFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, printfFunc, 0, 0, 0, "printf", {0} }
,
132043 FUNCTION(format, -1, 0, 0, printfFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, printfFunc, 0, 0, 0, "format", {0} }
,
132044 FUNCTION(unicode, 1, 0, 0, unicodeFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unicodeFunc, 0, 0, 0, "unicode", {0} }
,
132045 FUNCTION(char, -1, 0, 0, charFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, charFunc, 0, 0, 0, "char", {0} }
,
132046 FUNCTION(abs, 1, 0, 0, absFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, absFunc, 0, 0, 0, "abs", {0} }
,
132047#ifdef SQLITE_DEBUG
132048 FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, fpdecodeFunc, 0, 0, 0, "fpdecode", {0} }
,
132049 FUNCTION(parseuri, -1, 0, 0, parseuriFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, parseuriFunc, 0, 0, 0, "parseuri", {0} }
,
132050#endif
132051#ifndef SQLITE_OMIT_FLOATING_POINT
132052 FUNCTION(round, 1, 0, 0, roundFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, roundFunc, 0, 0, 0, "round", {0} }
,
132053 FUNCTION(round, 2, 0, 0, roundFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, roundFunc, 0, 0, 0, "round", {0} }
,
132054#endif
132055 FUNCTION(upper, 1, 0, 0, upperFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, upperFunc, 0, 0, 0, "upper", {0} }
,
132056 FUNCTION(lower, 1, 0, 0, lowerFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, lowerFunc, 0, 0, 0, "lower", {0} }
,
132057 FUNCTION(hex, 1, 0, 0, hexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, hexFunc, 0, 0, 0, "hex", {0} }
,
132058 FUNCTION(unhex, 1, 0, 0, unhexFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unhexFunc, 0, 0, 0, "unhex", {0} }
,
132059 FUNCTION(unhex, 2, 0, 0, unhexFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, unhexFunc, 0, 0, 0, "unhex", {0} }
,
132060 FUNCTION(concat, -1, 0, 0, concatFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, concatFunc, 0, 0, 0, "concat", {0} }
,
132061 FUNCTION(concat, 0, 0, 0, 0 ){0, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "concat", {0} }
,
132062 FUNCTION(concat_ws, -1, 0, 0, concatwsFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, concatwsFunc, 0, 0, 0, "concat_ws", {0} }
,
132063 FUNCTION(concat_ws, 0, 0, 0, 0 ){0, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "concat_ws", {0} }
,
132064 FUNCTION(concat_ws, 1, 0, 0, 0 ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "concat_ws", {0} }
,
132065 INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ){2, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(0
)), 0, versionFunc, 0, 0, 0, "ifnull", {0} }
,
132066 VFUNCTION(random, 0, 0, 0, randomFunc ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, randomFunc
, 0, 0, 0, "random", {0} }
,
132067 VFUNCTION(randomblob, 1, 0, 0, randomBlob ){1, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, randomBlob
, 0, 0, 0, "randomblob", {0} }
,
132068 FUNCTION(nullif, 2, 0, 1, nullifFunc ){2, 0x00800000| 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0
, nullifFunc, 0, 0, 0, "nullif", {0} }
,
132069 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ){0, 0x00800000|0x2000|1, 0, 0, versionFunc, 0, 0, 0, "sqlite_version"
, {0} }
,
132070 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ){0, 0x00800000|0x2000|1, 0, 0, sourceidFunc, 0, 0, 0, "sqlite_source_id"
, {0} }
,
132071 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} }
,
132072 FUNCTION(quote, 1, 0, 0, quoteFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, quoteFunc, 0, 0, 0, "quote", {0} }
,
132073 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} }
,
132074 VFUNCTION(changes, 0, 0, 0, changes ){0, 0x00800000|1|(0*0x0020), ((void*)(long int)(0)), 0, changes
, 0, 0, 0, "changes", {0} }
,
132075 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} }
,
132076 FUNCTION(replace, 3, 0, 0, replaceFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, replaceFunc, 0, 0, 0, "replace", {0} }
,
132077 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, zeroblobFunc, 0, 0, 0, "zeroblob", {0} }
,
132078 FUNCTION(substr, 2, 0, 0, substrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substr", {0} }
,
132079 FUNCTION(substr, 3, 0, 0, substrFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substr", {0} }
,
132080 FUNCTION(substring, 2, 0, 0, substrFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substring", {0} }
,
132081 FUNCTION(substring, 3, 0, 0, substrFunc ){3, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, substrFunc, 0, 0, 0, "substring", {0} }
,
132082 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}}
,
132083 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}}
,
132084 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}}
,
132085 WAGGREGATE(count, 0,0,0, countStep,{0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
132086 countFinalize, countFinalize, countInverse,{0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
132087 SQLITE_FUNC_COUNT|SQLITE_FUNC_ANYORDER ){0, 0x00800000|1|(0*0x0020)|0x0100|0x08000000, ((void*)(long int
)(0)), 0, countStep,countFinalize,countFinalize,countInverse,
"count", {0}}
,
132088 WAGGREGATE(count, 1,0,0, countStep,{1, 0x00800000|1|(0*0x0020)|0x08000000, ((void*)(long int)(0)
), 0, countStep,countFinalize,countFinalize,countInverse,"count"
, {0}}
132089 countFinalize, countFinalize, countInverse, SQLITE_FUNC_ANYORDER ){1, 0x00800000|1|(0*0x0020)|0x08000000, ((void*)(long int)(0)
), 0, countStep,countFinalize,countFinalize,countInverse,"count"
, {0}}
,
132090 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}}
132091 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){1, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
132092 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}}
132093 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
132094 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}}
132095 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){2, 0x00800000|1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"string_agg"
, {0}}
,
132096
132097 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} }
,
132098#ifdef SQLITE_CASE_SENSITIVE_LIKE
132099 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} }
,
132100 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} }
,
132101#else
132102 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE){2, 0x00800000|0x0800|1|0x0004, (void *)&likeInfoNorm, 0,
likeFunc, 0, 0, 0, "like", {0} }
,
132103 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE){3, 0x00800000|0x0800|1|0x0004, (void *)&likeInfoNorm, 0,
likeFunc, 0, 0, 0, "like", {0} }
,
132104#endif
132105#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION1
132106 FUNCTION(unknown, -1, 0, 0, unknownFunc ){-1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)),
0, unknownFunc, 0, 0, 0, "unknown", {0} }
,
132107#endif
132108 FUNCTION(coalesce, 1, 0, 0, 0 ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "coalesce", {0} }
,
132109 FUNCTION(coalesce, 0, 0, 0, 0 ){0, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, 0, 0, 0, 0, "coalesce", {0} }
,
132110#ifdef SQLITE_ENABLE_MATH_FUNCTIONS1
132111 MFUNCTION(ceil, 1, xCeil, ceilingFunc ){1, 0x00800000|0x0800|1, xCeil, 0, ceilingFunc, 0, 0, 0, "ceil"
, {0} }
,
132112 MFUNCTION(ceiling, 1, xCeil, ceilingFunc ){1, 0x00800000|0x0800|1, xCeil, 0, ceilingFunc, 0, 0, 0, "ceiling"
, {0} }
,
132113 MFUNCTION(floor, 1, xFloor, ceilingFunc ){1, 0x00800000|0x0800|1, xFloor, 0, ceilingFunc, 0, 0, 0, "floor"
, {0} }
,
132114#if SQLITE_HAVE_C99_MATH_FUNCS(1)
132115 MFUNCTION(trunc, 1, trunc, ceilingFunc ){1, 0x00800000|0x0800|1, trunc, 0, ceilingFunc, 0, 0, 0, "trunc"
, {0} }
,
132116#endif
132117 FUNCTION(ln, 1, 0, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, logFunc, 0, 0, 0, "ln", {0} }
,
132118 FUNCTION(log, 1, 1, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, logFunc, 0, 0, 0, "log", {0} }
,
132119 FUNCTION(log10, 1, 1, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0
, logFunc, 0, 0, 0, "log10", {0} }
,
132120 FUNCTION(log2, 1, 2, 0, logFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0
, logFunc, 0, 0, 0, "log2", {0} }
,
132121 FUNCTION(log, 2, 0, 0, logFunc ){2, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, logFunc, 0, 0, 0, "log", {0} }
,
132122 MFUNCTION(exp, 1, exp, math1Func ){1, 0x00800000|0x0800|1, exp, 0, math1Func, 0, 0, 0, "exp", {
0} }
,
132123 MFUNCTION(pow, 2, pow, math2Func ){2, 0x00800000|0x0800|1, pow, 0, math2Func, 0, 0, 0, "pow", {
0} }
,
132124 MFUNCTION(power, 2, pow, math2Func ){2, 0x00800000|0x0800|1, pow, 0, math2Func, 0, 0, 0, "power",
{0} }
,
132125 MFUNCTION(mod, 2, fmod, math2Func ){2, 0x00800000|0x0800|1, fmod, 0, math2Func, 0, 0, 0, "mod", {
0} }
,
132126 MFUNCTION(acos, 1, acos, math1Func ){1, 0x00800000|0x0800|1, acos, 0, math1Func, 0, 0, 0, "acos",
{0} }
,
132127 MFUNCTION(asin, 1, asin, math1Func ){1, 0x00800000|0x0800|1, asin, 0, math1Func, 0, 0, 0, "asin",
{0} }
,
132128 MFUNCTION(atan, 1, atan, math1Func ){1, 0x00800000|0x0800|1, atan, 0, math1Func, 0, 0, 0, "atan",
{0} }
,
132129 MFUNCTION(atan2, 2, atan2, math2Func ){2, 0x00800000|0x0800|1, atan2, 0, math2Func, 0, 0, 0, "atan2"
, {0} }
,
132130 MFUNCTION(cos, 1, cos, math1Func ){1, 0x00800000|0x0800|1, cos, 0, math1Func, 0, 0, 0, "cos", {
0} }
,
132131 MFUNCTION(sin, 1, sin, math1Func ){1, 0x00800000|0x0800|1, sin, 0, math1Func, 0, 0, 0, "sin", {
0} }
,
132132 MFUNCTION(tan, 1, tan, math1Func ){1, 0x00800000|0x0800|1, tan, 0, math1Func, 0, 0, 0, "tan", {
0} }
,
132133 MFUNCTION(cosh, 1, cosh, math1Func ){1, 0x00800000|0x0800|1, cosh, 0, math1Func, 0, 0, 0, "cosh",
{0} }
,
132134 MFUNCTION(sinh, 1, sinh, math1Func ){1, 0x00800000|0x0800|1, sinh, 0, math1Func, 0, 0, 0, "sinh",
{0} }
,
132135 MFUNCTION(tanh, 1, tanh, math1Func ){1, 0x00800000|0x0800|1, tanh, 0, math1Func, 0, 0, 0, "tanh",
{0} }
,
132136#if SQLITE_HAVE_C99_MATH_FUNCS(1)
132137 MFUNCTION(acosh, 1, acosh, math1Func ){1, 0x00800000|0x0800|1, acosh, 0, math1Func, 0, 0, 0, "acosh"
, {0} }
,
132138 MFUNCTION(asinh, 1, asinh, math1Func ){1, 0x00800000|0x0800|1, asinh, 0, math1Func, 0, 0, 0, "asinh"
, {0} }
,
132139 MFUNCTION(atanh, 1, atanh, math1Func ){1, 0x00800000|0x0800|1, atanh, 0, math1Func, 0, 0, 0, "atanh"
, {0} }
,
132140#endif
132141 MFUNCTION(sqrt, 1, sqrt, math1Func ){1, 0x00800000|0x0800|1, sqrt, 0, math1Func, 0, 0, 0, "sqrt",
{0} }
,
132142 MFUNCTION(radians, 1, degToRad, math1Func ){1, 0x00800000|0x0800|1, degToRad, 0, math1Func, 0, 0, 0, "radians"
, {0} }
,
132143 MFUNCTION(degrees, 1, radToDeg, math1Func ){1, 0x00800000|0x0800|1, radToDeg, 0, math1Func, 0, 0, 0, "degrees"
, {0} }
,
132144 MFUNCTION(pi, 0, 0, piFunc ){0, 0x00800000|0x0800|1, 0, 0, piFunc, 0, 0, 0, "pi", {0} },
132145#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
132146 FUNCTION(sign, 1, 0, 0, signFunc ){1, 0x00800000| 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0
, signFunc, 0, 0, 0, "sign", {0} }
,
132147 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ){-1, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(
0)), 0, versionFunc, 0, 0, 0, "coalesce", {0} }
,
132148 INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ){3, 0x00800000| 1|0x00400000|0x0800|(0), ((void*)(long int)(5
)), 0, versionFunc, 0, 0, 0, "iif", {0} }
,
132149 };
132150#ifndef SQLITE_OMIT_ALTERTABLE
132151 sqlite3AlterFunctions();
132152#endif
132153 sqlite3WindowFunctions();
132154 sqlite3RegisterDateTimeFunctions();
132155 sqlite3RegisterJsonFunctions();
132156 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)((int)(sizeof(aBuiltinFunc)/sizeof(aBuiltinFunc[0]))));
132157
132158#if 0 /* Enable to print out how the built-in functions are hashed */
132159 {
132160 int i;
132161 FuncDef *p;
132162 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
132163 printf("FUNC-HASH %02d:", i);
132164 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
132165 int n = sqlite3Strlen30(p->zName);
132166 int h = p->zName[0] + n;
132167 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
132168 printf(" %s(%d)", p->zName, h);
132169 }
132170 printf("\n");
132171 }
132172 }
132173#endif
132174}
132175
132176/************** End of func.c ************************************************/
132177/************** Begin file fkey.c ********************************************/
132178/*
132179**
132180** The author disclaims copyright to this source code. In place of
132181** a legal notice, here is a blessing:
132182**
132183** May you do good and not evil.
132184** May you find forgiveness for yourself and forgive others.
132185** May you share freely, never taking more than you give.
132186**
132187*************************************************************************
132188** This file contains code used by the compiler to add foreign key
132189** support to compiled SQL statements.
132190*/
132191/* #include "sqliteInt.h" */
132192
132193#ifndef SQLITE_OMIT_FOREIGN_KEY
132194#ifndef SQLITE_OMIT_TRIGGER
132195
132196/*
132197** Deferred and Immediate FKs
132198** --------------------------
132199**
132200** Foreign keys in SQLite come in two flavours: deferred and immediate.
132201** If an immediate foreign key constraint is violated,
132202** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
132203** statement transaction rolled back. If a
132204** deferred foreign key constraint is violated, no action is taken
132205** immediately. However if the application attempts to commit the
132206** transaction before fixing the constraint violation, the attempt fails.
132207**
132208** Deferred constraints are implemented using a simple counter associated
132209** with the database handle. The counter is set to zero each time a
132210** database transaction is opened. Each time a statement is executed
132211** that causes a foreign key violation, the counter is incremented. Each
132212** time a statement is executed that removes an existing violation from
132213** the database, the counter is decremented. When the transaction is
132214** committed, the commit fails if the current value of the counter is
132215** greater than zero. This scheme has two big drawbacks:
132216**
132217** * When a commit fails due to a deferred foreign key constraint,
132218** there is no way to tell which foreign constraint is not satisfied,
132219** or which row it is not satisfied for.
132220**
132221** * If the database contains foreign key violations when the
132222** transaction is opened, this may cause the mechanism to malfunction.
132223**
132224** Despite these problems, this approach is adopted as it seems simpler
132225** than the alternatives.
132226**
132227** INSERT operations:
132228**
132229** I.1) For each FK for which the table is the child table, search
132230** the parent table for a match. If none is found increment the
132231** constraint counter.
132232**
132233** I.2) For each FK for which the table is the parent table,
132234** search the child table for rows that correspond to the new
132235** row in the parent table. Decrement the counter for each row
132236** found (as the constraint is now satisfied).
132237**
132238** DELETE operations:
132239**
132240** D.1) For each FK for which the table is the child table,
132241** search the parent table for a row that corresponds to the
132242** deleted row in the child table. If such a row is not found,
132243** decrement the counter.
132244**
132245** D.2) For each FK for which the table is the parent table, search
132246** the child table for rows that correspond to the deleted row
132247** in the parent table. For each found increment the counter.
132248**
132249** UPDATE operations:
132250**
132251** An UPDATE command requires that all 4 steps above are taken, but only
132252** for FK constraints for which the affected columns are actually
132253** modified (values must be compared at runtime).
132254**
132255** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
132256** This simplifies the implementation a bit.
132257**
132258** For the purposes of immediate FK constraints, the OR REPLACE conflict
132259** resolution is considered to delete rows before the new row is inserted.
132260** If a delete caused by OR REPLACE violates an FK constraint, an exception
132261** is thrown, even if the FK constraint would be satisfied after the new
132262** row is inserted.
132263**
132264** Immediate constraints are usually handled similarly. The only difference
132265** is that the counter used is stored as part of each individual statement
132266** object (struct Vdbe). If, after the statement has run, its immediate
132267** constraint counter is greater than zero,
132268** it returns SQLITE_CONSTRAINT_FOREIGNKEY
132269** and the statement transaction is rolled back. An exception is an INSERT
132270** statement that inserts a single row only (no triggers). In this case,
132271** instead of using a counter, an exception is thrown immediately if the
132272** INSERT violates a foreign key constraint. This is necessary as such
132273** an INSERT does not open a statement transaction.
132274**
132275** TODO: How should dropping a table be handled? How should renaming a
132276** table be handled?
132277**
132278**
132279** Query API Notes
132280** ---------------
132281**
132282** Before coding an UPDATE or DELETE row operation, the code-generator
132283** for those two operations needs to know whether or not the operation
132284** requires any FK processing and, if so, which columns of the original
132285** row are required by the FK processing VDBE code (i.e. if FKs were
132286** implemented using triggers, which of the old.* columns would be
132287** accessed). No information is required by the code-generator before
132288** coding an INSERT operation. The functions used by the UPDATE/DELETE
132289** generation code to query for this information are:
132290**
132291** sqlite3FkRequired() - Test to see if FK processing is required.
132292** sqlite3FkOldmask() - Query for the set of required old.* columns.
132293**
132294**
132295** Externally accessible module functions
132296** --------------------------------------
132297**
132298** sqlite3FkCheck() - Check for foreign key violations.
132299** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
132300** sqlite3FkDelete() - Delete an FKey structure.
132301*/
132302
132303/*
132304** VDBE Calling Convention
132305** -----------------------
132306**
132307** Example:
132308**
132309** For the following INSERT statement:
132310**
132311** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
132312** INSERT INTO t1 VALUES(1, 2, 3.1);
132313**
132314** Register (x): 2 (type integer)
132315** Register (x+1): 1 (type integer)
132316** Register (x+2): NULL (type NULL)
132317** Register (x+3): 3.1 (type real)
132318*/
132319
132320/*
132321** A foreign key constraint requires that the key columns in the parent
132322** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
132323** Given that pParent is the parent table for foreign key constraint pFKey,
132324** search the schema for a unique index on the parent key columns.
132325**
132326** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
132327** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
132328** is set to point to the unique index.
132329**
132330** If the parent key consists of a single column (the foreign key constraint
132331** is not a composite foreign key), output variable *paiCol is set to NULL.
132332** Otherwise, it is set to point to an allocated array of size N, where
132333** N is the number of columns in the parent key. The first element of the
132334** array is the index of the child table column that is mapped by the FK
132335** constraint to the parent table column stored in the left-most column
132336** of index *ppIdx. The second element of the array is the index of the
132337** child table column that corresponds to the second left-most column of
132338** *ppIdx, and so on.
132339**
132340** If the required index cannot be found, either because:
132341**
132342** 1) The named parent key columns do not exist, or
132343**
132344** 2) The named parent key columns do exist, but are not subject to a
132345** UNIQUE or PRIMARY KEY constraint, or
132346**
132347** 3) No parent key columns were provided explicitly as part of the
132348** foreign key definition, and the parent table does not have a
132349** PRIMARY KEY, or
132350**
132351** 4) No parent key columns were provided explicitly as part of the
132352** foreign key definition, and the PRIMARY KEY of the parent table
132353** consists of a different number of columns to the child key in
132354** the child table.
132355**
132356** then non-zero is returned, and a "foreign key mismatch" error loaded
132357** into pParse. If an OOM error occurs, non-zero is returned and the
132358** pParse->db->mallocFailed flag is set.
132359*/
132360SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(
132361 Parse *pParse, /* Parse context to store any error in */
132362 Table *pParent, /* Parent table of FK constraint pFKey */
132363 FKey *pFKey, /* Foreign key to find index for */
132364 Index **ppIdx, /* OUT: Unique index on parent table */
132365 int **paiCol /* OUT: Map of index columns in pFKey */
132366){
132367 Index *pIdx = 0; /* Value to return via *ppIdx */
132368 int *aiCol = 0; /* Value to return via *paiCol */
132369 int nCol = pFKey->nCol; /* Number of columns in parent key */
132370 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
132371
132372 /* The caller is responsible for zeroing output parameters. */
132373 assert( ppIdx && *ppIdx==0 )((void) (0));
132374 assert( !paiCol || *paiCol==0 )((void) (0));
132375 assert( pParse )((void) (0));
132376
132377 /* If this is a non-composite (single column) foreign key, check if it
132378 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
132379 ** and *paiCol set to zero and return early.
132380 **
132381 ** Otherwise, for a composite foreign key (more than one column), allocate
132382 ** space for the aiCol array (returned via output parameter *paiCol).
132383 ** Non-composite foreign keys do not require the aiCol array.
132384 */
132385 if( nCol==1 ){
132386 /* The FK maps to the IPK if any of the following are true:
132387 **
132388 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
132389 ** mapped to the primary key of table pParent, or
132390 ** 2) The FK is explicitly mapped to a column declared as INTEGER
132391 ** PRIMARY KEY.
132392 */
132393 if( pParent->iPKey>=0 ){
132394 if( !zKey ) return 0;
132395 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
132396 return 0;
132397 }
132398 }
132399 }else if( paiCol ){
132400 assert( nCol>1 )((void) (0));
132401 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
132402 if( !aiCol ) return 1;
132403 *paiCol = aiCol;
132404 }
132405
132406 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
132407 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx)((pIdx)->onError!=0) && pIdx->pPartIdxWhere==0 ){
132408 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
132409 ** of columns. If each indexed column corresponds to a foreign key
132410 ** column of pFKey, then this index is a winner. */
132411
132412 if( zKey==0 ){
132413 /* If zKey is NULL, then this foreign key is implicitly mapped to
132414 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
132415 ** identified by the test. */
132416 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
132417 if( aiCol ){
132418 int i;
132419 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
132420 }
132421 break;
132422 }
132423 }else{
132424 /* If zKey is non-NULL, then this foreign key was declared to
132425 ** map to an explicit list of columns in table pParent. Check if this
132426 ** index matches those columns. Also, check that the index uses
132427 ** the default collation sequences for each column. */
132428 int i, j;
132429 for(i=0; i<nCol; i++){
132430 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
132431 const char *zDfltColl; /* Def. collation for column */
132432 char *zIdxCol; /* Name of indexed column */
132433
132434 if( iCol<0 ) break; /* No foreign keys against expression indexes */
132435
132436 /* If the index uses a collation sequence that is different from
132437 ** the default collation sequence for the column, this index is
132438 ** unusable. Bail out early in this case. */
132439 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
132440 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
132441 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
132442
132443 zIdxCol = pParent->aCol[iCol].zCnName;
132444 for(j=0; j<nCol; j++){
132445 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
132446 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
132447 break;
132448 }
132449 }
132450 if( j==nCol ) break;
132451 }
132452 if( i==nCol ) break; /* pIdx is usable */
132453 }
132454 }
132455 }
132456
132457 if( !pIdx ){
132458 if( !pParse->disableTriggers ){
132459 sqlite3ErrorMsg(pParse,
132460 "foreign key mismatch - \"%w\" referencing \"%w\"",
132461 pFKey->pFrom->zName, pFKey->zTo);
132462 }
132463 sqlite3DbFree(pParse->db, aiCol);
132464 return 1;
132465 }
132466
132467 *ppIdx = pIdx;
132468 return 0;
132469}
132470
132471/*
132472** This function is called when a row is inserted into or deleted from the
132473** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
132474** on the child table of pFKey, this function is invoked twice for each row
132475** affected - once to "delete" the old row, and then again to "insert" the
132476** new row.
132477**
132478** Each time it is called, this function generates VDBE code to locate the
132479** row in the parent table that corresponds to the row being inserted into
132480** or deleted from the child table. If the parent row can be found, no
132481** special action is taken. Otherwise, if the parent row can *not* be
132482** found in the parent table:
132483**
132484** Operation | FK type | Action taken
132485** --------------------------------------------------------------------------
132486** INSERT immediate Increment the "immediate constraint counter".
132487**
132488** DELETE immediate Decrement the "immediate constraint counter".
132489**
132490** INSERT deferred Increment the "deferred constraint counter".
132491**
132492** DELETE deferred Decrement the "deferred constraint counter".
132493**
132494** These operations are identified in the comment at the top of this file
132495** (fkey.c) as "I.1" and "D.1".
132496*/
132497static void fkLookupParent(
132498 Parse *pParse, /* Parse context */
132499 int iDb, /* Index of database housing pTab */
132500 Table *pTab, /* Parent table of FK pFKey */
132501 Index *pIdx, /* Unique index on parent key columns in pTab */
132502 FKey *pFKey, /* Foreign key constraint */
132503 int *aiCol, /* Map from parent key columns to child table columns */
132504 int regData, /* Address of array containing child table row */
132505 int nIncr, /* Increment constraint counter by this */
132506 int isIgnore /* If true, pretend pTab contains all NULL values */
132507){
132508 int i; /* Iterator variable */
132509 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
132510 int iCur = pParse->nTab - 1; /* Cursor number to use */
132511 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
132512
132513 sqlite3VdbeVerifyAbortable(v,
132514 (!pFKey->isDeferred
132515 && !(pParse->db->flags & SQLITE_DeferFKs)
132516 && !pParse->pToplevel
132517 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
132518
132519 /* If nIncr is less than zero, then check at runtime if there are any
132520 ** outstanding constraints to resolve. If there are not, there is no need
132521 ** to check if deleting this row resolves any outstanding violations.
132522 **
132523 ** Check if any of the key columns in the child table row are NULL. If
132524 ** any are, then the constraint is considered satisfied. No need to
132525 ** search for a matching row in the parent table. */
132526 if( nIncr<0 ){
132527 sqlite3VdbeAddOp2(v, OP_FkIfZero49, pFKey->isDeferred, iOk);
132528 VdbeCoverage(v);
132529 }
132530 for(i=0; i<pFKey->nCol; i++){
132531 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
132532 sqlite3VdbeAddOp2(v, OP_IsNull51, iReg, iOk); VdbeCoverage(v);
132533 }
132534
132535 if( isIgnore==0 ){
132536 if( pIdx==0 ){
132537 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
132538 ** column of the parent table (table pTab). */
132539 int iMustBeInt; /* Address of MustBeInt instruction */
132540 int regTemp = sqlite3GetTempReg(pParse);
132541
132542 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
132543 ** apply the affinity of the parent key). If this fails, then there
132544 ** is no matching parent key. Before using MustBeInt, make a copy of
132545 ** the value. Otherwise, the value inserted into the child key column
132546 ** will have INTEGER affinity applied to it, which may not be correct. */
132547 sqlite3VdbeAddOp2(v, OP_SCopy81,
132548 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
132549 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt13, regTemp, 0);
132550 VdbeCoverage(v);
132551
132552 /* If the parent table is the same as the child table, and we are about
132553 ** to increment the constraint-counter (i.e. this is an INSERT operation),
132554 ** then check if the row being inserted matches itself. If so, do not
132555 ** increment the constraint-counter. */
132556 if( pTab==pFKey->pFrom && nIncr==1 ){
132557 sqlite3VdbeAddOp3(v, OP_Eq54, regData, iOk, regTemp); VdbeCoverage(v);
132558 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
132559 }
132560
132561 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead102);
132562 sqlite3VdbeAddOp3(v, OP_NotExists31, iCur, 0, regTemp); VdbeCoverage(v);
132563 sqlite3VdbeGoto(v, iOk);
132564 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
132565 sqlite3VdbeJumpHere(v, iMustBeInt);
132566 sqlite3ReleaseTempReg(pParse, regTemp);
132567 }else{
132568 int nCol = pFKey->nCol;
132569 int regTemp = sqlite3GetTempRange(pParse, nCol);
132570
132571 sqlite3VdbeAddOp3(v, OP_OpenRead102, iCur, pIdx->tnum, iDb);
132572 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
132573 for(i=0; i<nCol; i++){
132574 sqlite3VdbeAddOp2(v, OP_Copy80,
132575 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
132576 regTemp+i);
132577 }
132578
132579 /* If the parent table is the same as the child table, and we are about
132580 ** to increment the constraint-counter (i.e. this is an INSERT operation),
132581 ** then check if the row being inserted matches itself. If so, do not
132582 ** increment the constraint-counter.
132583 **
132584 ** If any of the parent-key values are NULL, then the row cannot match
132585 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
132586 ** of the parent-key values are NULL (at this point it is known that
132587 ** none of the child key values are).
132588 */
132589 if( pTab==pFKey->pFrom && nIncr==1 ){
132590 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
132591 for(i=0; i<nCol; i++){
132592 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
132593 +1+regData;
132594 int iParent = 1+regData;
132595 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
132596 pIdx->aiColumn[i]);
132597 assert( pIdx->aiColumn[i]>=0 )((void) (0));
132598 assert( aiCol[i]!=pTab->iPKey )((void) (0));
132599 if( pIdx->aiColumn[i]==pTab->iPKey ){
132600 /* The parent key is a composite key that includes the IPK column */
132601 iParent = regData;
132602 }
132603 sqlite3VdbeAddOp3(v, OP_Ne53, iChild, iJump, iParent); VdbeCoverage(v);
132604 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL0x10);
132605 }
132606 sqlite3VdbeGoto(v, iOk);
132607 }
132608
132609 sqlite3VdbeAddOp4(v, OP_Affinity96, regTemp, nCol, 0,
132610 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
132611 sqlite3VdbeAddOp4Int(v, OP_Found29, iCur, iOk, regTemp, nCol);
132612 VdbeCoverage(v);
132613 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
132614 }
132615 }
132616
132617 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs0x00080000)
132618 && !pParse->pToplevel
132619 && !pParse->isMultiWrite
132620 ){
132621 /* Special case: If this is an INSERT statement that will insert exactly
132622 ** one row into the table, raise a constraint immediately instead of
132623 ** incrementing a counter. This is necessary as the VM code is being
132624 ** generated for will not open a statement transaction. */
132625 assert( nIncr==1 )((void) (0));
132626 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
132627 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
132628 }else{
132629 if( nIncr>0 && pFKey->isDeferred==0 ){
132630 sqlite3MayAbort(pParse);
132631 }
132632 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, nIncr);
132633 }
132634
132635 sqlite3VdbeResolveLabel(v, iOk);
132636 sqlite3VdbeAddOp1(v, OP_Close122, iCur);
132637}
132638
132639
132640/*
132641** Return an Expr object that refers to a memory register corresponding
132642** to column iCol of table pTab.
132643**
132644** regBase is the first of an array of register that contains the data
132645** for pTab. regBase itself holds the rowid. regBase+1 holds the first
132646** column. regBase+2 holds the second column, and so forth.
132647*/
132648static Expr *exprTableRegister(
132649 Parse *pParse, /* Parsing and code generating context */
132650 Table *pTab, /* The table whose content is at r[regBase]... */
132651 int regBase, /* Contents of table pTab */
132652 i16 iCol /* Which column of pTab is desired */
132653){
132654 Expr *pExpr;
132655 Column *pCol;
132656 const char *zColl;
132657 sqlite3 *db = pParse->db;
132658
132659 pExpr = sqlite3Expr(db, TK_REGISTER176, 0);
132660 if( pExpr ){
132661 if( iCol>=0 && iCol!=pTab->iPKey ){
132662 pCol = &pTab->aCol[iCol];
132663 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
132664 pExpr->affExpr = pCol->affinity;
132665 zColl = sqlite3ColumnColl(pCol);
132666 if( zColl==0 ) zColl = db->pDfltColl->zName;
132667 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
132668 }else{
132669 pExpr->iTable = regBase;
132670 pExpr->affExpr = SQLITE_AFF_INTEGER0x44;
132671 }
132672 }
132673 return pExpr;
132674}
132675
132676/*
132677** Return an Expr object that refers to column iCol of table pTab which
132678** has cursor iCur.
132679*/
132680static Expr *exprTableColumn(
132681 sqlite3 *db, /* The database connection */
132682 Table *pTab, /* The table whose column is desired */
132683 int iCursor, /* The open cursor on the table */
132684 i16 iCol /* The column that is wanted */
132685){
132686 Expr *pExpr = sqlite3Expr(db, TK_COLUMN168, 0);
132687 if( pExpr ){
132688 assert( ExprUseYTab(pExpr) )((void) (0));
132689 pExpr->y.pTab = pTab;
132690 pExpr->iTable = iCursor;
132691 pExpr->iColumn = iCol;
132692 }
132693 return pExpr;
132694}
132695
132696/*
132697** This function is called to generate code executed when a row is deleted
132698** from the parent table of foreign key constraint pFKey and, if pFKey is
132699** deferred, when a row is inserted into the same table. When generating
132700** code for an SQL UPDATE operation, this function may be called twice -
132701** once to "delete" the old row and once to "insert" the new row.
132702**
132703** Parameter nIncr is passed -1 when inserting a row (as this may decrease
132704** the number of FK violations in the db) or +1 when deleting one (as this
132705** may increase the number of FK constraint problems).
132706**
132707** The code generated by this function scans through the rows in the child
132708** table that correspond to the parent table row being deleted or inserted.
132709** For each child row found, one of the following actions is taken:
132710**
132711** Operation | FK type | Action taken
132712** --------------------------------------------------------------------------
132713** DELETE immediate Increment the "immediate constraint counter".
132714**
132715** INSERT immediate Decrement the "immediate constraint counter".
132716**
132717** DELETE deferred Increment the "deferred constraint counter".
132718**
132719** INSERT deferred Decrement the "deferred constraint counter".
132720**
132721** These operations are identified in the comment at the top of this file
132722** (fkey.c) as "I.2" and "D.2".
132723*/
132724static void fkScanChildren(
132725 Parse *pParse, /* Parse context */
132726 SrcList *pSrc, /* The child table to be scanned */
132727 Table *pTab, /* The parent table */
132728 Index *pIdx, /* Index on parent covering the foreign key */
132729 FKey *pFKey, /* The foreign key linking pSrc to pTab */
132730 int *aiCol, /* Map from pIdx cols to child table cols */
132731 int regData, /* Parent row data starts here */
132732 int nIncr /* Amount to increment deferred counter by */
132733){
132734 sqlite3 *db = pParse->db; /* Database handle */
132735 int i; /* Iterator variable */
132736 Expr *pWhere = 0; /* WHERE clause to scan with */
132737 NameContext sNameContext; /* Context used to resolve WHERE clause */
132738 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
132739 int iFkIfZero = 0; /* Address of OP_FkIfZero */
132740 Vdbe *v = sqlite3GetVdbe(pParse);
132741
132742 assert( pIdx==0 || pIdx->pTable==pTab )((void) (0));
132743 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol )((void) (0));
132744 assert( pIdx!=0 || pFKey->nCol==1 )((void) (0));
132745 assert( pIdx!=0 || HasRowid(pTab) )((void) (0));
132746
132747 if( nIncr<0 ){
132748 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero49, pFKey->isDeferred, 0);
132749 VdbeCoverage(v);
132750 }
132751
132752 /* Create an Expr object representing an SQL expression like:
132753 **
132754 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
132755 **
132756 ** The collation sequence used for the comparison should be that of
132757 ** the parent key columns. The affinity of the parent key column should
132758 ** be applied to each child key value before the comparison takes place.
132759 */
132760 for(i=0; i<pFKey->nCol; i++){
132761 Expr *pLeft; /* Value from parent table row */
132762 Expr *pRight; /* Column ref to child table */
132763 Expr *pEq; /* Expression (pLeft = pRight) */
132764 i16 iCol; /* Index of column in child table */
132765 const char *zCol; /* Name of column in child table */
132766
132767 iCol = pIdx ? pIdx->aiColumn[i] : -1;
132768 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
132769 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
132770 assert( iCol>=0 )((void) (0));
132771 zCol = pFKey->pFrom->aCol[iCol].zCnName;
132772 pRight = sqlite3Expr(db, TK_ID60, zCol);
132773 pEq = sqlite3PExpr(pParse, TK_EQ54, pLeft, pRight);
132774 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
132775 }
132776
132777 /* If the child table is the same as the parent table, then add terms
132778 ** to the WHERE clause that prevent this entry from being scanned.
132779 ** The added WHERE clause terms are like this:
132780 **
132781 ** $current_rowid!=rowid
132782 ** NOT( $current_a==a AND $current_b==b AND ... )
132783 **
132784 ** The first form is used for rowid tables. The second form is used
132785 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
132786 ** (a,b,...). Either the parent or primary key could be used to
132787 ** uniquely identify the current row, but the parent key is more convenient
132788 ** as the required values have already been loaded into registers
132789 ** by the caller.
132790 */
132791 if( pTab==pFKey->pFrom && nIncr>0 ){
132792 Expr *pNe; /* Expression (pLeft != pRight) */
132793 Expr *pLeft; /* Value from parent table row */
132794 Expr *pRight; /* Column ref to child table */
132795 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
132796 pLeft = exprTableRegister(pParse, pTab, regData, -1);
132797 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
132798 pNe = sqlite3PExpr(pParse, TK_NE53, pLeft, pRight);
132799 }else{
132800 Expr *pEq, *pAll = 0;
132801 assert( pIdx!=0 )((void) (0));
132802 for(i=0; i<pIdx->nKeyCol; i++){
132803 i16 iCol = pIdx->aiColumn[i];
132804 assert( iCol>=0 )((void) (0));
132805 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
132806 pRight = sqlite3Expr(db, TK_ID60, pTab->aCol[iCol].zCnName);
132807 pEq = sqlite3PExpr(pParse, TK_IS45, pLeft, pRight);
132808 pAll = sqlite3ExprAnd(pParse, pAll, pEq);
132809 }
132810 pNe = sqlite3PExpr(pParse, TK_NOT19, pAll, 0);
132811 }
132812 pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
132813 }
132814
132815 /* Resolve the references in the WHERE clause. */
132816 memset(&sNameContext, 0, sizeof(NameContext));
132817 sNameContext.pSrcList = pSrc;
132818 sNameContext.pParse = pParse;
132819 sqlite3ResolveExprNames(&sNameContext, pWhere);
132820
132821 /* Create VDBE to loop through the entries in pSrc that match the WHERE
132822 ** clause. For each row found, increment either the deferred or immediate
132823 ** foreign key constraint counter. */
132824 if( pParse->nErr==0 ){
132825 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0);
132826 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, nIncr);
132827 if( pWInfo ){
132828 sqlite3WhereEnd(pWInfo);
132829 }
132830 }
132831
132832 /* Clean up the WHERE clause constructed above. */
132833 sqlite3ExprDelete(db, pWhere);
132834 if( iFkIfZero ){
132835 sqlite3VdbeJumpHereOrPopInst(v, iFkIfZero);
132836 }
132837}
132838
132839/*
132840** This function returns a linked list of FKey objects (connected by
132841** FKey.pNextTo) holding all children of table pTab. For example,
132842** given the following schema:
132843**
132844** CREATE TABLE t1(a PRIMARY KEY);
132845** CREATE TABLE t2(b REFERENCES t1(a);
132846**
132847** Calling this function with table "t1" as an argument returns a pointer
132848** to the FKey structure representing the foreign key constraint on table
132849** "t2". Calling this function with "t2" as the argument would return a
132850** NULL pointer (as there are no FK constraints for which t2 is the parent
132851** table).
132852*/
132853SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *pTab){
132854 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
132855}
132856
132857/*
132858** The second argument is a Trigger structure allocated by the
132859** fkActionTrigger() routine. This function deletes the Trigger structure
132860** and all of its sub-components.
132861**
132862** The Trigger structure or any of its sub-components may be allocated from
132863** the lookaside buffer belonging to database handle dbMem.
132864*/
132865static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
132866 if( p ){
132867 TriggerStep *pStep = p->step_list;
132868 sqlite3ExprDelete(dbMem, pStep->pWhere);
132869 sqlite3ExprListDelete(dbMem, pStep->pExprList);
132870 sqlite3SelectDelete(dbMem, pStep->pSelect);
132871 sqlite3ExprDelete(dbMem, p->pWhen);
132872 sqlite3DbFree(dbMem, p);
132873 }
132874}
132875
132876/*
132877** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys
132878** in a particular database. This needs to happen when the schema
132879** changes.
132880*/
132881SQLITE_PRIVATEstatic void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){
132882 HashElem *k;
132883 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
132884 for(k=sqliteHashFirst(pHash)((pHash)->first); k; k=sqliteHashNext(k)((k)->next)){
132885 Table *pTab = sqliteHashData(k)((k)->data);
132886 FKey *pFKey;
132887 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
132888 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
132889 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
132890 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
132891 }
132892 }
132893}
132894
132895/*
132896** This function is called to generate code that runs when table pTab is
132897** being dropped from the database. The SrcList passed as the second argument
132898** to this function contains a single entry guaranteed to resolve to
132899** table pTab.
132900**
132901** Normally, no code is required. However, if either
132902**
132903** (a) The table is the parent table of a FK constraint, or
132904** (b) The table is the child table of a deferred FK constraint and it is
132905** determined at runtime that there are outstanding deferred FK
132906** constraint violations in the database,
132907**
132908** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
132909** the table from the database. Triggers are disabled while running this
132910** DELETE, but foreign key actions are not.
132911*/
132912SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
132913 sqlite3 *db = pParse->db;
132914 if( (db->flags&SQLITE_ForeignKeys0x00004000) && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
132915 int iSkip = 0;
132916 Vdbe *v = sqlite3GetVdbe(pParse);
132917
132918 assert( v )((void) (0)); /* VDBE has already been allocated */
132919 assert( IsOrdinaryTable(pTab) )((void) (0));
132920 if( sqlite3FkReferences(pTab)==0 ){
132921 /* Search for a deferred foreign key constraint for which this table
132922 ** is the child table. If one cannot be found, return without
132923 ** generating any VDBE code. If one can be found, then jump over
132924 ** the entire DELETE if there are no outstanding deferred constraints
132925 ** when this statement is run. */
132926 FKey *p;
132927 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132928 if( p->isDeferred || (db->flags & SQLITE_DeferFKs0x00080000) ) break;
132929 }
132930 if( !p ) return;
132931 iSkip = sqlite3VdbeMakeLabel(pParse);
132932 sqlite3VdbeAddOp2(v, OP_FkIfZero49, 1, iSkip); VdbeCoverage(v);
132933 }
132934
132935 pParse->disableTriggers = 1;
132936 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
132937 pParse->disableTriggers = 0;
132938
132939 /* If the DELETE has generated immediate foreign key constraint
132940 ** violations, halt the VDBE and return an error at this point, before
132941 ** any modifications to the schema are made. This is because statement
132942 ** transactions are not able to rollback schema changes.
132943 **
132944 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
132945 ** the statement transaction will not be rolled back even if FK
132946 ** constraints are violated.
132947 */
132948 if( (db->flags & SQLITE_DeferFKs0x00080000)==0 ){
132949 sqlite3VdbeVerifyAbortable(v, OE_Abort);
132950 sqlite3VdbeAddOp2(v, OP_FkIfZero49, 0, sqlite3VdbeCurrentAddr(v)+2);
132951 VdbeCoverage(v);
132952 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
132953 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
132954 }
132955
132956 if( iSkip ){
132957 sqlite3VdbeResolveLabel(v, iSkip);
132958 }
132959 }
132960}
132961
132962
132963/*
132964** The second argument points to an FKey object representing a foreign key
132965** for which pTab is the child table. An UPDATE statement against pTab
132966** is currently being processed. For each column of the table that is
132967** actually updated, the corresponding element in the aChange[] array
132968** is zero or greater (if a column is unmodified the corresponding element
132969** is set to -1). If the rowid column is modified by the UPDATE statement
132970** the bChngRowid argument is non-zero.
132971**
132972** This function returns true if any of the columns that are part of the
132973** child key for FK constraint *p are modified.
132974*/
132975static int fkChildIsModified(
132976 Table *pTab, /* Table being updated */
132977 FKey *p, /* Foreign key for which pTab is the child */
132978 int *aChange, /* Array indicating modified columns */
132979 int bChngRowid /* True if rowid is modified by this update */
132980){
132981 int i;
132982 for(i=0; i<p->nCol; i++){
132983 int iChildKey = p->aCol[i].iFrom;
132984 if( aChange[iChildKey]>=0 ) return 1;
132985 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
132986 }
132987 return 0;
132988}
132989
132990/*
132991** The second argument points to an FKey object representing a foreign key
132992** for which pTab is the parent table. An UPDATE statement against pTab
132993** is currently being processed. For each column of the table that is
132994** actually updated, the corresponding element in the aChange[] array
132995** is zero or greater (if a column is unmodified the corresponding element
132996** is set to -1). If the rowid column is modified by the UPDATE statement
132997** the bChngRowid argument is non-zero.
132998**
132999** This function returns true if any of the columns that are part of the
133000** parent key for FK constraint *p are modified.
133001*/
133002static int fkParentIsModified(
133003 Table *pTab,
133004 FKey *p,
133005 int *aChange,
133006 int bChngRowid
133007){
133008 int i;
133009 for(i=0; i<p->nCol; i++){
133010 char *zKey = p->aCol[i].zCol;
133011 int iKey;
133012 for(iKey=0; iKey<pTab->nCol; iKey++){
133013 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
133014 Column *pCol = &pTab->aCol[iKey];
133015 if( zKey ){
133016 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
133017 }else if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
133018 return 1;
133019 }
133020 }
133021 }
133022 }
133023 return 0;
133024}
133025
133026/*
133027** Return true if the parser passed as the first argument is being
133028** used to code a trigger that is really a "SET NULL" action belonging
133029** to trigger pFKey.
133030*/
133031static int isSetNullAction(Parse *pParse, FKey *pFKey){
133032 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
133033 if( pTop->pTriggerPrg ){
133034 Trigger *p = pTop->pTriggerPrg->pTrigger;
133035 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull8)
133036 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull8)
133037 ){
133038 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 )((void) (0));
133039 return 1;
133040 }
133041 }
133042 return 0;
133043}
133044
133045/*
133046** This function is called when inserting, deleting or updating a row of
133047** table pTab to generate VDBE code to perform foreign key constraint
133048** processing for the operation.
133049**
133050** For a DELETE operation, parameter regOld is passed the index of the
133051** first register in an array of (pTab->nCol+1) registers containing the
133052** rowid of the row being deleted, followed by each of the column values
133053** of the row being deleted, from left to right. Parameter regNew is passed
133054** zero in this case.
133055**
133056** For an INSERT operation, regOld is passed zero and regNew is passed the
133057** first register of an array of (pTab->nCol+1) registers containing the new
133058** row data.
133059**
133060** For an UPDATE operation, this function is called twice. Once before
133061** the original record is deleted from the table using the calling convention
133062** described for DELETE. Then again after the original record is deleted
133063** but before the new record is inserted using the INSERT convention.
133064*/
133065SQLITE_PRIVATEstatic void sqlite3FkCheck(
133066 Parse *pParse, /* Parse context */
133067 Table *pTab, /* Row is being deleted from this table */
133068 int regOld, /* Previous row data is stored here */
133069 int regNew, /* New row data is stored here */
133070 int *aChange, /* Array indicating UPDATEd columns (or 0) */
133071 int bChngRowid /* True if rowid is UPDATEd */
133072){
133073 sqlite3 *db = pParse->db; /* Database handle */
133074 FKey *pFKey; /* Used to iterate through FKs */
133075 int iDb; /* Index of database containing pTab */
133076 const char *zDb; /* Name of database containing pTab */
133077 int isIgnoreErrors = pParse->disableTriggers;
133078
133079 /* Exactly one of regOld and regNew should be non-zero. */
133080 assert( (regOld==0)!=(regNew==0) )((void) (0));
133081
133082 /* If foreign-keys are disabled, this function is a no-op. */
133083 if( (db->flags&SQLITE_ForeignKeys0x00004000)==0 ) return;
133084 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return;
133085
133086 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133087 zDb = db->aDb[iDb].zDbSName;
133088
133089 /* Loop through all the foreign key constraints for which pTab is the
133090 ** child table (the table that the foreign key definition is part of). */
133091 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
133092 Table *pTo; /* Parent table of foreign key pFKey */
133093 Index *pIdx = 0; /* Index on key columns in pTo */
133094 int *aiFree = 0;
133095 int *aiCol;
133096 int iCol;
133097 int i;
133098 int bIgnore = 0;
133099
133100 if( aChange
133101 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
133102 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
133103 ){
133104 continue;
133105 }
133106
133107 /* Find the parent table of this foreign key. Also find a unique index
133108 ** on the parent key columns in the parent table. If either of these
133109 ** schema items cannot be located, set an error in pParse and return
133110 ** early. */
133111 if( pParse->disableTriggers ){
133112 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
133113 }else{
133114 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
133115 }
133116 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
133117 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) )((void) (0));
133118 if( !isIgnoreErrors || db->mallocFailed ) return;
133119 if( pTo==0 ){
133120 /* If isIgnoreErrors is true, then a table is being dropped. In this
133121 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
133122 ** before actually dropping it in order to check FK constraints.
133123 ** If the parent table of an FK constraint on the current table is
133124 ** missing, behave as if it is empty. i.e. decrement the relevant
133125 ** FK counter for each row of the current table with non-NULL keys.
133126 */
133127 Vdbe *v = sqlite3GetVdbe(pParse);
133128 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
133129 for(i=0; i<pFKey->nCol; i++){
133130 int iFromCol, iReg;
133131 iFromCol = pFKey->aCol[i].iFrom;
133132 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
133133 sqlite3VdbeAddOp2(v, OP_IsNull51, iReg, iJump); VdbeCoverage(v);
133134 }
133135 sqlite3VdbeAddOp2(v, OP_FkCounter158, pFKey->isDeferred, -1);
133136 }
133137 continue;
133138 }
133139 assert( pFKey->nCol==1 || (aiFree && pIdx) )((void) (0));
133140
133141 if( aiFree ){
133142 aiCol = aiFree;
133143 }else{
133144 iCol = pFKey->aCol[0].iFrom;
133145 aiCol = &iCol;
133146 }
133147 for(i=0; i<pFKey->nCol; i++){
133148 if( aiCol[i]==pTab->iPKey ){
133149 aiCol[i] = -1;
133150 }
133151 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
133152#ifndef SQLITE_OMIT_AUTHORIZATION
133153 /* Request permission to read the parent key columns. If the
133154 ** authorization callback returns SQLITE_IGNORE, behave as if any
133155 ** values read from the parent table are NULL. */
133156 if( db->xAuth ){
133157 int rcauth;
133158 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
133159 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
133160 bIgnore = (rcauth==SQLITE_IGNORE2);
133161 }
133162#endif
133163 }
133164
133165 /* Take a shared-cache advisory read-lock on the parent table. Allocate
133166 ** a cursor to use to search the unique index on the parent key columns
133167 ** in the parent table. */
133168 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
133169 pParse->nTab++;
133170
133171 if( regOld!=0 ){
133172 /* A row is being removed from the child table. Search for the parent.
133173 ** If the parent does not exist, removing the child row resolves an
133174 ** outstanding foreign key constraint violation. */
133175 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
133176 }
133177 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
133178 /* A row is being added to the child table. If a parent row cannot
133179 ** be found, adding the child row has violated the FK constraint.
133180 **
133181 ** If this operation is being performed as part of a trigger program
133182 ** that is actually a "SET NULL" action belonging to this very
133183 ** foreign key, then omit this scan altogether. As all child key
133184 ** values are guaranteed to be NULL, it is not possible for adding
133185 ** this row to cause an FK violation. */
133186 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
133187 }
133188
133189 sqlite3DbFree(db, aiFree);
133190 }
133191
133192 /* Loop through all the foreign key constraints that refer to this table.
133193 ** (the "child" constraints) */
133194 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
133195 Index *pIdx = 0; /* Foreign key index for pFKey */
133196 SrcList *pSrc;
133197 int *aiCol = 0;
133198
133199 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
133200 continue;
133201 }
133202
133203 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs0x00080000)
133204 && !pParse->pToplevel && !pParse->isMultiWrite
133205 ){
133206 assert( regOld==0 && regNew!=0 )((void) (0));
133207 /* Inserting a single row into a parent table cannot cause (or fix)
133208 ** an immediate foreign key violation. So do nothing in this case. */
133209 continue;
133210 }
133211
133212 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
133213 if( !isIgnoreErrors || db->mallocFailed ) return;
133214 continue;
133215 }
133216 assert( aiCol || pFKey->nCol==1 )((void) (0));
133217
133218 /* Create a SrcList structure containing the child table. We need the
133219 ** child table as a SrcList for sqlite3WhereBegin() */
133220 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
133221 if( pSrc ){
133222 SrcItem *pItem = pSrc->a;
133223 pItem->pSTab = pFKey->pFrom;
133224 pItem->zName = pFKey->pFrom->zName;
133225 pItem->pSTab->nTabRef++;
133226 pItem->iCursor = pParse->nTab++;
133227
133228 if( regNew!=0 ){
133229 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
133230 }
133231 if( regOld!=0 ){
133232 int eAction = pFKey->aAction[aChange!=0];
133233 if( (db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32)) ) eAction = OE_None0;
133234
133235 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
133236 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
133237 ** action applies, then any foreign key violations caused by
133238 ** removing the parent key will be rectified by the action trigger.
133239 ** So do not set the "may-abort" flag in this case.
133240 **
133241 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
133242 ** may-abort flag will eventually be set on this statement anyway
133243 ** (when this function is called as part of processing the UPDATE
133244 ** within the action trigger).
133245 **
133246 ** Note 2: At first glance it may seem like SQLite could simply omit
133247 ** all OP_FkCounter related scans when either CASCADE or SET NULL
133248 ** applies. The trouble starts if the CASCADE or SET NULL action
133249 ** trigger causes other triggers or action rules attached to the
133250 ** child table to fire. In these cases the fk constraint counters
133251 ** might be set incorrectly if any OP_FkCounter related scans are
133252 ** omitted. */
133253 if( !pFKey->isDeferred && eAction!=OE_Cascade10 && eAction!=OE_SetNull8 ){
133254 sqlite3MayAbort(pParse);
133255 }
133256 }
133257 pItem->zName = 0;
133258 sqlite3SrcListDelete(db, pSrc);
133259 }
133260 sqlite3DbFree(db, aiCol);
133261 }
133262}
133263
133264#define COLUMN_MASK(x)(((x)>31) ? 0xffffffff : ((u32)1<<(x))) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
133265
133266/*
133267** This function is called before generating code to update or delete a
133268** row contained in table pTab.
133269*/
133270SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(
133271 Parse *pParse, /* Parse context */
133272 Table *pTab /* Table being modified */
133273){
133274 u32 mask = 0;
133275 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
133276 FKey *p;
133277 int i;
133278 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
133279 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)))
;
133280 }
133281 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
133282 Index *pIdx = 0;
133283 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
133284 if( pIdx ){
133285 for(i=0; i<pIdx->nKeyCol; i++){
133286 assert( pIdx->aiColumn[i]>=0 )((void) (0));
133287 mask |= COLUMN_MASK(pIdx->aiColumn[i])(((pIdx->aiColumn[i])>31) ? 0xffffffff : ((u32)1<<
(pIdx->aiColumn[i])))
;
133288 }
133289 }
133290 }
133291 }
133292 return mask;
133293}
133294
133295
133296/*
133297** This function is called before generating code to update or delete a
133298** row contained in table pTab. If the operation is a DELETE, then
133299** parameter aChange is passed a NULL value. For an UPDATE, aChange points
133300** to an array of size N, where N is the number of columns in table pTab.
133301** If the i'th column is not modified by the UPDATE, then the corresponding
133302** entry in the aChange[] array is set to -1. If the column is modified,
133303** the value is 0 or greater. Parameter chngRowid is set to true if the
133304** UPDATE statement modifies the rowid fields of the table.
133305**
133306** If any foreign key processing will be required, this function returns
133307** non-zero. If there is no foreign key related processing, this function
133308** returns zero.
133309**
133310** For an UPDATE, this function returns 2 if:
133311**
133312** * There are any FKs for which pTab is the child and the parent table
133313** and any FK processing at all is required (even of a different FK), or
133314**
133315** * the UPDATE modifies one or more parent keys for which the action is
133316** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
133317**
133318** Or, assuming some other foreign key processing is required, 1.
133319*/
133320SQLITE_PRIVATEstatic int sqlite3FkRequired(
133321 Parse *pParse, /* Parse context */
133322 Table *pTab, /* Table being modified */
133323 int *aChange, /* Non-NULL for UPDATE operations */
133324 int chngRowid /* True for UPDATE that affects rowid */
133325){
133326 int eRet = 1; /* Value to return if bHaveFK is true */
133327 int bHaveFK = 0; /* If FK processing is required */
133328 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
133329 if( !aChange ){
133330 /* A DELETE operation. Foreign key processing is required if the
133331 ** table in question is either the child or parent table for any
133332 ** foreign key constraint. */
133333 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
133334 }else{
133335 /* This is an UPDATE. Foreign key processing is only required if the
133336 ** operation modifies one or more child or parent key columns. */
133337 FKey *p;
133338
133339 /* Check if any child key columns are being modified. */
133340 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
133341 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
133342 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
133343 bHaveFK = 1;
133344 }
133345 }
133346
133347 /* Check if any parent key columns are being modified. */
133348 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
133349 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
133350 if( (pParse->db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32))==0
133351 && p->aAction[1]!=OE_None0
133352 ){
133353 return 2;
133354 }
133355 bHaveFK = 1;
133356 }
133357 }
133358 }
133359 }
133360 return bHaveFK ? eRet : 0;
133361}
133362
133363/*
133364** This function is called when an UPDATE or DELETE operation is being
133365** compiled on table pTab, which is the parent table of foreign-key pFKey.
133366** If the current operation is an UPDATE, then the pChanges parameter is
133367** passed a pointer to the list of columns being modified. If it is a
133368** DELETE, pChanges is passed a NULL pointer.
133369**
133370** It returns a pointer to a Trigger structure containing a trigger
133371** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
133372** If the action is "NO ACTION" then a NULL pointer is returned (these actions
133373** require no special handling by the triggers sub-system, code for them is
133374** created by fkScanChildren()).
133375**
133376** For example, if pFKey is the foreign key and pTab is table "p" in
133377** the following schema:
133378**
133379** CREATE TABLE p(pk PRIMARY KEY);
133380** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
133381**
133382** then the returned trigger structure is equivalent to:
133383**
133384** CREATE TRIGGER ... DELETE ON p BEGIN
133385** DELETE FROM c WHERE ck = old.pk;
133386** END;
133387**
133388** The returned pointer is cached as part of the foreign key object. It
133389** is eventually freed along with the rest of the foreign key object by
133390** sqlite3FkDelete().
133391*/
133392static Trigger *fkActionTrigger(
133393 Parse *pParse, /* Parse context */
133394 Table *pTab, /* Table being updated or deleted from */
133395 FKey *pFKey, /* Foreign key to get action for */
133396 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
133397){
133398 sqlite3 *db = pParse->db; /* Database handle */
133399 int action; /* One of OE_None, OE_Cascade etc. */
133400 Trigger *pTrigger; /* Trigger definition to return */
133401 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
133402
133403 action = pFKey->aAction[iAction];
133404 if( (db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32)) ) action = OE_None0;
133405 if( action==OE_Restrict7 && (db->flags & SQLITE_DeferFKs0x00080000) ){
133406 return 0;
133407 }
133408 pTrigger = pFKey->apTrigger[iAction];
133409
133410 if( action!=OE_None0 && !pTrigger ){
133411 char const *zFrom; /* Name of child table */
133412 int nFrom; /* Length in bytes of zFrom */
133413 Index *pIdx = 0; /* Parent key index for this FK */
133414 int *aiCol = 0; /* child table cols -> parent key cols */
133415 TriggerStep *pStep = 0; /* First (only) step of trigger program */
133416 Expr *pWhere = 0; /* WHERE clause of trigger step */
133417 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
133418 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
133419 int i; /* Iterator variable */
133420 Expr *pWhen = 0; /* WHEN clause for the trigger */
133421
133422 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
133423 assert( aiCol || pFKey->nCol==1 )((void) (0));
133424
133425 for(i=0; i<pFKey->nCol; i++){
133426 Token tOld = { "old", 3 }; /* Literal "old" token */
133427 Token tNew = { "new", 3 }; /* Literal "new" token */
133428 Token tFromCol; /* Name of column in child table */
133429 Token tToCol; /* Name of column in parent table */
133430 int iFromCol; /* Idx of column in child table */
133431 Expr *pEq; /* tFromCol = OLD.tToCol */
133432
133433 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
133434 assert( iFromCol>=0 )((void) (0));
133435 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) )((void) (0));
133436 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
133437 sqlite3TokenInit(&tToCol,
133438 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
133439 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
133440
133441 /* Create the expression "OLD.zToCol = zFromCol". It is important
133442 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
133443 ** that the affinity and collation sequence associated with the
133444 ** parent table are used for the comparison. */
133445 pEq = sqlite3PExpr(pParse, TK_EQ54,
133446 sqlite3PExpr(pParse, TK_DOT142,
133447 sqlite3ExprAlloc(db, TK_ID60, &tOld, 0),
133448 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0)),
133449 sqlite3ExprAlloc(db, TK_ID60, &tFromCol, 0)
133450 );
133451 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
133452
133453 /* For ON UPDATE, construct the next term of the WHEN clause.
133454 ** The final WHEN clause will be like this:
133455 **
133456 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
133457 */
133458 if( pChanges ){
133459 pEq = sqlite3PExpr(pParse, TK_IS45,
133460 sqlite3PExpr(pParse, TK_DOT142,
133461 sqlite3ExprAlloc(db, TK_ID60, &tOld, 0),
133462 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0)),
133463 sqlite3PExpr(pParse, TK_DOT142,
133464 sqlite3ExprAlloc(db, TK_ID60, &tNew, 0),
133465 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0))
133466 );
133467 pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
133468 }
133469
133470 if( action!=OE_Restrict7 && (action!=OE_Cascade10 || pChanges) ){
133471 Expr *pNew;
133472 if( action==OE_Cascade10 ){
133473 pNew = sqlite3PExpr(pParse, TK_DOT142,
133474 sqlite3ExprAlloc(db, TK_ID60, &tNew, 0),
133475 sqlite3ExprAlloc(db, TK_ID60, &tToCol, 0));
133476 }else if( action==OE_SetDflt9 ){
133477 Column *pCol = pFKey->pFrom->aCol + iFromCol;
133478 Expr *pDflt;
133479 if( pCol->colFlags & COLFLAG_GENERATED0x0060 ){
133480 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
133481 testcase( pCol->colFlags & COLFLAG_STORED );
133482 pDflt = 0;
133483 }else{
133484 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
133485 }
133486 if( pDflt ){
133487 pNew = sqlite3ExprDup(db, pDflt, 0);
133488 }else{
133489 pNew = sqlite3ExprAlloc(db, TK_NULL122, 0, 0);
133490 }
133491 }else{
133492 pNew = sqlite3ExprAlloc(db, TK_NULL122, 0, 0);
133493 }
133494 pList = sqlite3ExprListAppend(pParse, pList, pNew);
133495 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
133496 }
133497 }
133498 sqlite3DbFree(db, aiCol);
133499
133500 zFrom = pFKey->pFrom->zName;
133501 nFrom = sqlite3Strlen30(zFrom);
133502
133503 if( action==OE_Restrict7 ){
133504 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133505 SrcList *pSrc;
133506 Expr *pRaise;
133507
133508 pRaise = sqlite3Expr(db, TK_STRING118, "FOREIGN KEY constraint failed"),
133509 pRaise = sqlite3PExpr(pParse, TK_RAISE72, pRaise, 0);
133510 if( pRaise ){
133511 pRaise->affExpr = OE_Abort2;
133512 }
133513 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
133514 if( pSrc ){
133515 assert( pSrc->nSrc==1 )((void) (0));
133516 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
133517 assert( pSrc->a[0].fg.fixedSchema==0 && pSrc->a[0].fg.isSubquery==0 )((void) (0));
133518 pSrc->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
133519 }
133520 pSelect = sqlite3SelectNew(pParse,
133521 sqlite3ExprListAppend(pParse, 0, pRaise),
133522 pSrc,
133523 pWhere,
133524 0, 0, 0, 0, 0
133525 );
133526 pWhere = 0;
133527 }
133528
133529 /* Disable lookaside memory allocation */
133530 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
133531
133532 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
133533 sizeof(Trigger) + /* struct Trigger */
133534 sizeof(TriggerStep) + /* Single step in trigger program */
133535 nFrom + 1 /* Space for pStep->zTarget */
133536 );
133537 if( pTrigger ){
133538 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
133539 pStep->zTarget = (char *)&pStep[1];
133540 memcpy((char *)pStep->zTarget, zFrom, nFrom);
133541
133542 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
133543 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE0x0001);
133544 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
133545 if( pWhen ){
133546 pWhen = sqlite3PExpr(pParse, TK_NOT19, pWhen, 0);
133547 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
133548 }
133549 }
133550
133551 /* Re-enable the lookaside buffer, if it was disabled earlier. */
133552 EnableLookasidedb->lookaside.bDisable--; db->lookaside.sz=db->lookaside
.bDisable?0:db->lookaside.szTrue
;
133553
133554 sqlite3ExprDelete(db, pWhere);
133555 sqlite3ExprDelete(db, pWhen);
133556 sqlite3ExprListDelete(db, pList);
133557 sqlite3SelectDelete(db, pSelect);
133558 if( db->mallocFailed==1 ){
133559 fkTriggerDelete(db, pTrigger);
133560 return 0;
133561 }
133562 assert( pStep!=0 )((void) (0));
133563 assert( pTrigger!=0 )((void) (0));
133564
133565 switch( action ){
133566 case OE_Restrict7:
133567 pStep->op = TK_SELECT139;
133568 break;
133569 case OE_Cascade10:
133570 if( !pChanges ){
133571 pStep->op = TK_DELETE129;
133572 break;
133573 }
133574 /* no break */ deliberate_fall_through__attribute__((fallthrough));
133575 default:
133576 pStep->op = TK_UPDATE130;
133577 }
133578 pStep->pTrig = pTrigger;
133579 pTrigger->pSchema = pTab->pSchema;
133580 pTrigger->pTabSchema = pTab->pSchema;
133581 pFKey->apTrigger[iAction] = pTrigger;
133582 pTrigger->op = (pChanges ? TK_UPDATE130 : TK_DELETE129);
133583 }
133584
133585 return pTrigger;
133586}
133587
133588/*
133589** This function is called when deleting or updating a row to implement
133590** any required CASCADE, SET NULL or SET DEFAULT actions.
133591*/
133592SQLITE_PRIVATEstatic void sqlite3FkActions(
133593 Parse *pParse, /* Parse context */
133594 Table *pTab, /* Table being updated or deleted from */
133595 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
133596 int regOld, /* Address of array containing old row */
133597 int *aChange, /* Array indicating UPDATEd columns (or 0) */
133598 int bChngRowid /* True if rowid is UPDATEd */
133599){
133600 /* If foreign-key support is enabled, iterate through all FKs that
133601 ** refer to table pTab. If there is an action associated with the FK
133602 ** for this operation (either update or delete), invoke the associated
133603 ** trigger sub-program. */
133604 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 ){
133605 FKey *pFKey; /* Iterator variable */
133606 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
133607 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
133608 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
133609 if( pAct ){
133610 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort2, 0);
133611 }
133612 }
133613 }
133614 }
133615}
133616
133617#endif /* ifndef SQLITE_OMIT_TRIGGER */
133618
133619/*
133620** Free all memory associated with foreign key definitions attached to
133621** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
133622** hash table.
133623*/
133624SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *db, Table *pTab){
133625 FKey *pFKey; /* Iterator variable */
133626 FKey *pNext; /* Copy of pFKey->pNextFrom */
133627
133628 assert( IsOrdinaryTable(pTab) )((void) (0));
133629 assert( db!=0 )((void) (0));
133630 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
133631 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) )((void) (0));
133632
133633 /* Remove the FK from the fkeyHash hash table. */
133634 if( db->pnBytesFreed==0 ){
133635 if( pFKey->pPrevTo ){
133636 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
133637 }else{
133638 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
133639 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo);
133640 }
133641 if( pFKey->pNextTo ){
133642 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
133643 }
133644 }
133645
133646 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
133647 ** classified as either immediate or deferred.
133648 */
133649 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 )((void) (0));
133650
133651 /* Delete any triggers created to implement actions for this FK. */
133652#ifndef SQLITE_OMIT_TRIGGER
133653 fkTriggerDelete(db, pFKey->apTrigger[0]);
133654 fkTriggerDelete(db, pFKey->apTrigger[1]);
133655#endif
133656
133657 pNext = pFKey->pNextFrom;
133658 sqlite3DbFree(db, pFKey);
133659 }
133660}
133661#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
133662
133663/************** End of fkey.c ************************************************/
133664/************** Begin file insert.c ******************************************/
133665/*
133666** 2001 September 15
133667**
133668** The author disclaims copyright to this source code. In place of
133669** a legal notice, here is a blessing:
133670**
133671** May you do good and not evil.
133672** May you find forgiveness for yourself and forgive others.
133673** May you share freely, never taking more than you give.
133674**
133675*************************************************************************
133676** This file contains C code routines that are called by the parser
133677** to handle INSERT statements in SQLite.
133678*/
133679/* #include "sqliteInt.h" */
133680
133681/*
133682** Generate code that will
133683**
133684** (1) acquire a lock for table pTab then
133685** (2) open pTab as cursor iCur.
133686**
133687** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
133688** for that table that is actually opened.
133689*/
133690SQLITE_PRIVATEstatic void sqlite3OpenTable(
133691 Parse *pParse, /* Generate code into this VDBE */
133692 int iCur, /* The cursor number of the table */
133693 int iDb, /* The database index in sqlite3.aDb[] */
133694 Table *pTab, /* The table to be opened */
133695 int opcode /* OP_OpenRead or OP_OpenWrite */
133696){
133697 Vdbe *v;
133698 assert( !IsVirtual(pTab) )((void) (0));
133699 assert( pParse->pVdbe!=0 )((void) (0));
133700 v = pParse->pVdbe;
133701 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead )((void) (0));
133702 if( !pParse->db->noSharedCache ){
133703 sqlite3TableLock(pParse, iDb, pTab->tnum,
133704 (opcode==OP_OpenWrite113)?1:0, pTab->zName);
133705 }
133706 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
133707 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
133708 VdbeComment((v, "%s", pTab->zName));
133709 }else{
133710 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
133711 assert( pPk!=0 )((void) (0));
133712 assert( pPk->tnum==pTab->tnum || CORRUPT_DB )((void) (0));
133713 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
133714 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
133715 VdbeComment((v, "%s", pTab->zName));
133716 }
133717}
133718
133719/*
133720** Return a pointer to the column affinity string associated with index
133721** pIdx. A column affinity string has one character for each column in
133722** the table, according to the affinity of the column:
133723**
133724** Character Column affinity
133725** ------------------------------
133726** 'A' BLOB
133727** 'B' TEXT
133728** 'C' NUMERIC
133729** 'D' INTEGER
133730** 'F' REAL
133731**
133732** An extra 'D' is appended to the end of the string to cover the
133733** rowid that appears as the last column in every index.
133734**
133735** Memory for the buffer containing the column index affinity string
133736** is managed along with the rest of the Index structure. It will be
133737** released when sqlite3DeleteIndex() is called.
133738*/
133739static SQLITE_NOINLINE__attribute__((noinline)) const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){
133740 /* The first time a column affinity string for a particular index is
133741 ** required, it is allocated and populated here. It is then stored as
133742 ** a member of the Index structure for subsequent use.
133743 **
133744 ** The column affinity string will eventually be deleted by
133745 ** sqliteDeleteIndex() when the Index structure itself is cleaned
133746 ** up.
133747 */
133748 int n;
133749 Table *pTab = pIdx->pTable;
133750 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
133751 if( !pIdx->zColAff ){
133752 sqlite3OomFault(db);
133753 return 0;
133754 }
133755 for(n=0; n<pIdx->nColumn; n++){
133756 i16 x = pIdx->aiColumn[n];
133757 char aff;
133758 if( x>=0 ){
133759 aff = pTab->aCol[x].affinity;
133760 }else if( x==XN_ROWID(-1) ){
133761 aff = SQLITE_AFF_INTEGER0x44;
133762 }else{
133763 assert( x==XN_EXPR )((void) (0));
133764 assert( pIdx->bHasExpr )((void) (0));
133765 assert( pIdx->aColExpr!=0 )((void) (0));
133766 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
133767 }
133768 if( aff<SQLITE_AFF_BLOB0x41 ) aff = SQLITE_AFF_BLOB0x41;
133769 if( aff>SQLITE_AFF_NUMERIC0x43) aff = SQLITE_AFF_NUMERIC0x43;
133770 pIdx->zColAff[n] = aff;
133771 }
133772 pIdx->zColAff[n] = 0;
133773 return pIdx->zColAff;
133774}
133775SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
133776 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
133777 return pIdx->zColAff;
133778}
133779
133780
133781/*
133782** Compute an affinity string for a table. Space is obtained
133783** from sqlite3DbMalloc(). The caller is responsible for freeing
133784** the space when done.
133785*/
133786SQLITE_PRIVATEstatic char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
133787 char *zColAff;
133788 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
133789 if( zColAff ){
133790 int i, j;
133791 for(i=j=0; i<pTab->nCol; i++){
133792 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020)==0 ){
133793 zColAff[j++] = pTab->aCol[i].affinity;
133794 }
133795 }
133796 do{
133797 zColAff[j--] = 0;
133798 }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB0x41 );
133799 }
133800 return zColAff;
133801}
133802
133803/*
133804** Make changes to the evolving bytecode to do affinity transformations
133805** of values that are about to be gathered into a row for table pTab.
133806**
133807** For ordinary (legacy, non-strict) tables:
133808** -----------------------------------------
133809**
133810** Compute the affinity string for table pTab, if it has not already been
133811** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
133812**
133813** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
133814** which were then optimized out) then this routine becomes a no-op.
133815**
133816** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
133817** affinities for register iReg and following. Or if iReg==0,
133818** then just set the P4 operand of the previous opcode (which should be
133819** an OP_MakeRecord) to the affinity string.
133820**
133821** A column affinity string has one character per column:
133822**
133823** Character Column affinity
133824** --------- ---------------
133825** 'A' BLOB
133826** 'B' TEXT
133827** 'C' NUMERIC
133828** 'D' INTEGER
133829** 'E' REAL
133830**
133831** For STRICT tables:
133832** ------------------
133833**
133834** Generate an appropriate OP_TypeCheck opcode that will verify the
133835** datatypes against the column definitions in pTab. If iReg==0, that
133836** means an OP_MakeRecord opcode has already been generated and should be
133837** the last opcode generated. The new OP_TypeCheck needs to be inserted
133838** before the OP_MakeRecord. The new OP_TypeCheck should use the same
133839** register set as the OP_MakeRecord. If iReg>0 then register iReg is
133840** the first of a series of registers that will form the new record.
133841** Apply the type checking to that array of registers.
133842*/
133843SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
133844 int i;
133845 char *zColAff;
133846 if( pTab->tabFlags & TF_Strict0x00010000 ){
133847 if( iReg==0 ){
133848 /* Move the previous opcode (which should be OP_MakeRecord) forward
133849 ** by one slot and insert a new OP_TypeCheck where the current
133850 ** OP_MakeRecord is found */
133851 VdbeOp *pPrev;
133852 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
133853 pPrev = sqlite3VdbeGetLastOp(v);
133854 assert( pPrev!=0 )((void) (0));
133855 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed )((void) (0));
133856 pPrev->opcode = OP_TypeCheck95;
133857 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pPrev->p1, pPrev->p2, pPrev->p3);
133858 }else{
133859 /* Insert an isolated OP_Typecheck */
133860 sqlite3VdbeAddOp2(v, OP_TypeCheck95, iReg, pTab->nNVCol);
133861 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
133862 }
133863 return;
133864 }
133865 zColAff = pTab->zColAff;
133866 if( zColAff==0 ){
133867 zColAff = sqlite3TableAffinityStr(0, pTab);
133868 if( !zColAff ){
133869 sqlite3OomFault(sqlite3VdbeDb(v));
133870 return;
133871 }
133872 pTab->zColAff = zColAff;
133873 }
133874 assert( zColAff!=0 )((void) (0));
133875 i = sqlite3Strlen30NN(zColAff)(strlen(zColAff)&0x3fffffff);
133876 if( i ){
133877 if( iReg ){
133878 sqlite3VdbeAddOp4(v, OP_Affinity96, iReg, i, 0, zColAff, i);
133879 }else{
133880 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord((void) (0))
133881 || sqlite3VdbeDb(v)->mallocFailed )((void) (0));
133882 sqlite3VdbeChangeP4(v, -1, zColAff, i);
133883 }
133884 }
133885}
133886
133887/*
133888** Return non-zero if the table pTab in database iDb or any of its indices
133889** have been opened at any point in the VDBE program. This is used to see if
133890** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
133891** run without using a temporary table for the results of the SELECT.
133892*/
133893static int readsTable(Parse *p, int iDb, Table *pTab){
133894 Vdbe *v = sqlite3GetVdbe(p);
133895 int i;
133896 int iEnd = sqlite3VdbeCurrentAddr(v);
133897#ifndef SQLITE_OMIT_VIRTUALTABLE
133898 VTable *pVTab = IsVirtual(pTab)((pTab)->eTabType==1) ? sqlite3GetVTable(p->db, pTab) : 0;
133899#endif
133900
133901 for(i=1; i<iEnd; i++){
133902 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
133903 assert( pOp!=0 )((void) (0));
133904 if( pOp->opcode==OP_OpenRead102 && pOp->p3==iDb ){
133905 Index *pIndex;
133906 Pgno tnum = pOp->p2;
133907 if( tnum==pTab->tnum ){
133908 return 1;
133909 }
133910 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
133911 if( tnum==pIndex->tnum ){
133912 return 1;
133913 }
133914 }
133915 }
133916#ifndef SQLITE_OMIT_VIRTUALTABLE
133917 if( pOp->opcode==OP_VOpen173 && pOp->p4.pVtab==pVTab ){
133918 assert( pOp->p4.pVtab!=0 )((void) (0));
133919 assert( pOp->p4type==P4_VTAB )((void) (0));
133920 return 1;
133921 }
133922#endif
133923 }
133924 return 0;
133925}
133926
133927/* This walker callback will compute the union of colFlags flags for all
133928** referenced columns in a CHECK constraint or generated column expression.
133929*/
133930static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){
133931 if( pExpr->op==TK_COLUMN168 && pExpr->iColumn>=0 ){
133932 assert( pExpr->iColumn < pWalker->u.pTab->nCol )((void) (0));
133933 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
133934 }
133935 return WRC_Continue0;
133936}
133937
133938#ifndef SQLITE_OMIT_GENERATED_COLUMNS
133939/*
133940** All regular columns for table pTab have been puts into registers
133941** starting with iRegStore. The registers that correspond to STORED
133942** or VIRTUAL columns have not yet been initialized. This routine goes
133943** back and computes the values for those columns based on the previously
133944** computed normal columns.
133945*/
133946SQLITE_PRIVATEstatic void sqlite3ComputeGeneratedColumns(
133947 Parse *pParse, /* Parsing context */
133948 int iRegStore, /* Register holding the first column */
133949 Table *pTab /* The table */
133950){
133951 int i;
133952 Walker w;
133953 Column *pRedo;
133954 int eProgress;
133955 VdbeOp *pOp;
133956
133957 assert( pTab->tabFlags & TF_HasGenerated )((void) (0));
133958 testcase( pTab->tabFlags & TF_HasVirtual );
133959 testcase( pTab->tabFlags & TF_HasStored );
133960
133961 /* Before computing generated columns, first go through and make sure
133962 ** that appropriate affinity has been applied to the regular columns
133963 */
133964 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
133965 if( (pTab->tabFlags & TF_HasStored0x00000040)!=0 ){
133966 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
133967 if( pOp->opcode==OP_Affinity96 ){
133968 /* Change the OP_Affinity argument to '@' (NONE) for all stored
133969 ** columns. '@' is the no-op affinity and those columns have not
133970 ** yet been computed. */
133971 int ii, jj;
133972 char *zP4 = pOp->p4.z;
133973 assert( zP4!=0 )((void) (0));
133974 assert( pOp->p4type==P4_DYNAMIC )((void) (0));
133975 for(ii=jj=0; zP4[jj]; ii++){
133976 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL0x0020 ){
133977 continue;
133978 }
133979 if( pTab->aCol[ii].colFlags & COLFLAG_STORED0x0040 ){
133980 zP4[jj] = SQLITE_AFF_NONE0x40;
133981 }
133982 jj++;
133983 }
133984 }else if( pOp->opcode==OP_TypeCheck95 ){
133985 /* If an OP_TypeCheck was generated because the table is STRICT,
133986 ** then set the P3 operand to indicate that generated columns should
133987 ** not be checked */
133988 pOp->p3 = 1;
133989 }
133990 }
133991
133992 /* Because there can be multiple generated columns that refer to one another,
133993 ** this is a two-pass algorithm. On the first pass, mark all generated
133994 ** columns as "not available".
133995 */
133996 for(i=0; i<pTab->nCol; i++){
133997 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
133998 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
133999 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
134000 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL0x0080;
134001 }
134002 }
134003
134004 w.u.pTab = pTab;
134005 w.xExprCallback = exprColumnFlagUnion;
134006 w.xSelectCallback = 0;
134007 w.xSelectCallback2 = 0;
134008
134009 /* On the second pass, compute the value of each NOT-AVAILABLE column.
134010 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
134011 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
134012 ** they are needed.
134013 */
134014 pParse->iSelfTab = -iRegStore;
134015 do{
134016 eProgress = 0;
134017 pRedo = 0;
134018 for(i=0; i<pTab->nCol; i++){
134019 Column *pCol = pTab->aCol + i;
134020 if( (pCol->colFlags & COLFLAG_NOTAVAIL0x0080)!=0 ){
134021 int x;
134022 pCol->colFlags |= COLFLAG_BUSY0x0100;
134023 w.eCode = 0;
134024 sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol));
134025 pCol->colFlags &= ~COLFLAG_BUSY0x0100;
134026 if( w.eCode & COLFLAG_NOTAVAIL0x0080 ){
134027 pRedo = pCol;
134028 continue;
134029 }
134030 eProgress = 1;
134031 assert( pCol->colFlags & COLFLAG_GENERATED )((void) (0));
134032 x = sqlite3TableColumnToStorage(pTab, i) + iRegStore;
134033 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x);
134034 pCol->colFlags &= ~COLFLAG_NOTAVAIL0x0080;
134035 }
134036 }
134037 }while( pRedo && eProgress );
134038 if( pRedo ){
134039 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
134040 }
134041 pParse->iSelfTab = 0;
134042}
134043#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
134044
134045
134046#ifndef SQLITE_OMIT_AUTOINCREMENT
134047/*
134048** Locate or create an AutoincInfo structure associated with table pTab
134049** which is in database iDb. Return the register number for the register
134050** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
134051** table. (Also return zero when doing a VACUUM since we do not want to
134052** update the AUTOINCREMENT counters during a VACUUM.)
134053**
134054** There is at most one AutoincInfo structure per table even if the
134055** same table is autoincremented multiple times due to inserts within
134056** triggers. A new AutoincInfo structure is created if this is the
134057** first use of table pTab. On 2nd and subsequent uses, the original
134058** AutoincInfo structure is used.
134059**
134060** Four consecutive registers are allocated:
134061**
134062** (1) The name of the pTab table.
134063** (2) The maximum ROWID of pTab.
134064** (3) The rowid in sqlite_sequence of pTab
134065** (4) The original value of the max ROWID in pTab, or NULL if none
134066**
134067** The 2nd register is the one that is returned. That is all the
134068** insert routine needs to know about.
134069*/
134070static int autoIncBegin(
134071 Parse *pParse, /* Parsing context */
134072 int iDb, /* Index of the database holding pTab */
134073 Table *pTab /* The table we are writing to */
134074){
134075 int memId = 0; /* Register holding maximum rowid */
134076 assert( pParse->db->aDb[iDb].pSchema!=0 )((void) (0));
134077 if( (pTab->tabFlags & TF_Autoincrement0x00000008)!=0
134078 && (pParse->db->mDbFlags & DBFLAG_Vacuum0x0004)==0
134079 ){
134080 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
134081 AutoincInfo *pInfo;
134082 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
134083
134084 /* Verify that the sqlite_sequence table exists and is an ordinary
134085 ** rowid table with exactly two columns.
134086 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
134087 if( pSeqTab==0
134088 || !HasRowid(pSeqTab)(((pSeqTab)->tabFlags & 0x00000080)==0)
134089 || NEVER(IsVirtual(pSeqTab))(((pSeqTab)->eTabType==1))
134090 || pSeqTab->nCol!=2
134091 ){
134092 pParse->nErr++;
134093 pParse->rc = SQLITE_CORRUPT_SEQUENCE(11 | (2<<8));
134094 return 0;
134095 }
134096
134097 pInfo = pToplevel->pAinc;
134098 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
134099 if( pInfo==0 ){
134100 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
134101 sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo);
134102 testcase( pParse->earlyCleanup );
134103 if( pParse->db->mallocFailed ) return 0;
134104 pInfo->pNext = pToplevel->pAinc;
134105 pToplevel->pAinc = pInfo;
134106 pInfo->pTab = pTab;
134107 pInfo->iDb = iDb;
134108 pToplevel->nMem++; /* Register to hold name of table */
134109 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
134110 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
134111 }
134112 memId = pInfo->regCtr;
134113 }
134114 return memId;
134115}
134116
134117/*
134118** This routine generates code that will initialize all of the
134119** register used by the autoincrement tracker.
134120*/
134121SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse){
134122 AutoincInfo *p; /* Information about an AUTOINCREMENT */
134123 sqlite3 *db = pParse->db; /* The database connection */
134124 Db *pDb; /* Database only autoinc table */
134125 int memId; /* Register holding max rowid */
134126 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
134127
134128 /* This routine is never called during trigger-generation. It is
134129 ** only called from the top-level */
134130 assert( pParse->pTriggerTab==0 )((void) (0));
134131 assert( sqlite3IsToplevel(pParse) )((void) (0));
134132
134133 assert( v )((void) (0)); /* We failed long ago if this is not so */
134134 for(p = pParse->pAinc; p; p = p->pNext){
134135 static const int iLn = VDBE_OFFSET_LINENO(2)0;
134136 static const VdbeOpList autoInc[] = {
134137 /* 0 */ {OP_Null75, 0, 0, 0},
134138 /* 1 */ {OP_Rewind36, 0, 10, 0},
134139 /* 2 */ {OP_Column94, 0, 0, 0},
134140 /* 3 */ {OP_Ne53, 0, 9, 0},
134141 /* 4 */ {OP_Rowid135, 0, 0, 0},
134142 /* 5 */ {OP_Column94, 0, 1, 0},
134143 /* 6 */ {OP_AddImm86, 0, 0, 0},
134144 /* 7 */ {OP_Copy80, 0, 0, 0},
134145 /* 8 */ {OP_Goto9, 0, 11, 0},
134146 /* 9 */ {OP_Next39, 0, 2, 0},
134147 /* 10 */ {OP_Integer71, 0, 0, 0},
134148 /* 11 */ {OP_Close122, 0, 0, 0}
134149 };
134150 VdbeOp *aOp;
134151 pDb = &db->aDb[p->iDb];
134152 memId = p->regCtr;
134153 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
134154 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead102);
134155 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
134156 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc)((int)(sizeof(autoInc)/sizeof(autoInc[0]))), autoInc, iLn);
134157 if( aOp==0 ) break;
134158 aOp[0].p2 = memId;
134159 aOp[0].p3 = memId+2;
134160 aOp[2].p3 = memId;
134161 aOp[3].p1 = memId-1;
134162 aOp[3].p3 = memId;
134163 aOp[3].p5 = SQLITE_JUMPIFNULL0x10;
134164 aOp[4].p2 = memId+1;
134165 aOp[5].p3 = memId;
134166 aOp[6].p1 = memId;
134167 aOp[7].p2 = memId+2;
134168 aOp[7].p1 = memId;
134169 aOp[10].p2 = memId;
134170 if( pParse->nTab==0 ) pParse->nTab = 1;
134171 }
134172}
134173
134174/*
134175** Update the maximum rowid for an autoincrement calculation.
134176**
134177** This routine should be called when the regRowid register holds a
134178** new rowid that is about to be inserted. If that new rowid is
134179** larger than the maximum rowid in the memId memory cell, then the
134180** memory cell is updated.
134181*/
134182static void autoIncStep(Parse *pParse, int memId, int regRowid){
134183 if( memId>0 ){
134184 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax159, memId, regRowid);
134185 }
134186}
134187
134188/*
134189** This routine generates the code needed to write autoincrement
134190** maximum rowid values back into the sqlite_sequence register.
134191** Every statement that might do an INSERT into an autoincrement
134192** table (either directly or through triggers) needs to call this
134193** routine just before the "exit" code.
134194*/
134195static SQLITE_NOINLINE__attribute__((noinline)) void autoIncrementEnd(Parse *pParse){
134196 AutoincInfo *p;
134197 Vdbe *v = pParse->pVdbe;
134198 sqlite3 *db = pParse->db;
134199
134200 assert( v )((void) (0));
134201 for(p = pParse->pAinc; p; p = p->pNext){
134202 static const int iLn = VDBE_OFFSET_LINENO(2)0;
134203 static const VdbeOpList autoIncEnd[] = {
134204 /* 0 */ {OP_NotNull52, 0, 2, 0},
134205 /* 1 */ {OP_NewRowid127, 0, 0, 0},
134206 /* 2 */ {OP_MakeRecord97, 0, 2, 0},
134207 /* 3 */ {OP_Insert128, 0, 0, 0},
134208 /* 4 */ {OP_Close122, 0, 0, 0}
134209 };
134210 VdbeOp *aOp;
134211 Db *pDb = &db->aDb[p->iDb];
134212 int iRec;
134213 int memId = p->regCtr;
134214
134215 iRec = sqlite3GetTempReg(pParse);
134216 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
134217 sqlite3VdbeAddOp3(v, OP_Le56, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
134218 VdbeCoverage(v);
134219 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite113);
134220 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd)((int)(sizeof(autoIncEnd)/sizeof(autoIncEnd[0]))), autoIncEnd, iLn);
134221 if( aOp==0 ) break;
134222 aOp[0].p1 = memId+1;
134223 aOp[1].p2 = memId+1;
134224 aOp[2].p1 = memId-1;
134225 aOp[2].p3 = iRec;
134226 aOp[3].p2 = iRec;
134227 aOp[3].p3 = memId+1;
134228 aOp[3].p5 = OPFLAG_APPEND0x08;
134229 sqlite3ReleaseTempReg(pParse, iRec);
134230 }
134231}
134232SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse){
134233 if( pParse->pAinc ) autoIncrementEnd(pParse);
134234}
134235#else
134236/*
134237** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
134238** above are all no-ops
134239*/
134240# define autoIncBegin(A,B,C) (0)
134241# define autoIncStep(A,B,C)
134242#endif /* SQLITE_OMIT_AUTOINCREMENT */
134243
134244/*
134245** If argument pVal is a Select object returned by an sqlite3MultiValues()
134246** that was able to use the co-routine optimization, finish coding the
134247** co-routine.
134248*/
134249SQLITE_PRIVATEstatic void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){
134250 if( ALWAYS(pVal)(pVal) && pVal->pSrc->nSrc>0 ){
134251 SrcItem *pItem = &pVal->pSrc->a[0];
134252 assert( (pItem->fg.isSubquery && pItem->u4.pSubq!=0) || pParse->nErr )((void) (0));
134253 if( pItem->fg.isSubquery ){
134254 sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->u4.pSubq->regReturn);
134255 sqlite3VdbeJumpHere(pParse->pVdbe, pItem->u4.pSubq->addrFillSub - 1);
134256 }
134257 }
134258}
134259
134260/*
134261** Return true if all expressions in the expression-list passed as the
134262** only argument are constant.
134263*/
134264static int exprListIsConstant(Parse *pParse, ExprList *pRow){
134265 int ii;
134266 for(ii=0; ii<pRow->nExpr; ii++){
134267 if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0;
134268 }
134269 return 1;
134270}
134271
134272/*
134273** Return true if all expressions in the expression-list passed as the
134274** only argument are both constant and have no affinity.
134275*/
134276static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){
134277 int ii;
134278 if( exprListIsConstant(pParse,pRow)==0 ) return 0;
134279 for(ii=0; ii<pRow->nExpr; ii++){
134280 Expr *pExpr = pRow->a[ii].pExpr;
134281 assert( pExpr->op!=TK_RAISE )((void) (0));
134282 assert( pExpr->affExpr==0 )((void) (0));
134283 if( 0!=sqlite3ExprAffinity(pExpr) ) return 0;
134284 }
134285 return 1;
134286
134287}
134288
134289/*
134290** This function is called by the parser for the second and subsequent
134291** rows of a multi-row VALUES clause. Argument pLeft is the part of
134292** the VALUES clause already parsed, argument pRow is the vector of values
134293** for the new row. The Select object returned represents the complete
134294** VALUES clause, including the new row.
134295**
134296** There are two ways in which this may be achieved - by incremental
134297** coding of a co-routine (the "co-routine" method) or by returning a
134298** Select object equivalent to the following (the "UNION ALL" method):
134299**
134300** "pLeft UNION ALL SELECT pRow"
134301**
134302** If the VALUES clause contains a lot of rows, this compound Select
134303** object may consume a lot of memory.
134304**
134305** When the co-routine method is used, each row that will be returned
134306** by the VALUES clause is coded into part of a co-routine as it is
134307** passed to this function. The returned Select object is equivalent to:
134308**
134309** SELECT * FROM (
134310** Select object to read co-routine
134311** )
134312**
134313** The co-routine method is used in most cases. Exceptions are:
134314**
134315** a) If the current statement has a WITH clause. This is to avoid
134316** statements like:
134317**
134318** WITH cte AS ( VALUES('x'), ('y') ... )
134319** SELECT * FROM cte AS a, cte AS b;
134320**
134321** This will not work, as the co-routine uses a hard-coded register
134322** for its OP_Yield instructions, and so it is not possible for two
134323** cursors to iterate through it concurrently.
134324**
134325** b) The schema is currently being parsed (i.e. the VALUES clause is part
134326** of a schema item like a VIEW or TRIGGER). In this case there is no VM
134327** being generated when parsing is taking place, and so generating
134328** a co-routine is not possible.
134329**
134330** c) There are non-constant expressions in the VALUES clause (e.g.
134331** the VALUES clause is part of a correlated sub-query).
134332**
134333** d) One or more of the values in the first row of the VALUES clause
134334** has an affinity (i.e. is a CAST expression). This causes problems
134335** because the complex rules SQLite uses (see function
134336** sqlite3SubqueryColumnTypes() in select.c) to determine the effective
134337** affinity of such a column for all rows require access to all values in
134338** the column simultaneously.
134339*/
134340SQLITE_PRIVATEstatic Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){
134341
134342 if( pParse->bHasWith /* condition (a) above */
134343 || pParse->db->init.busy /* condition (b) above */
134344 || exprListIsConstant(pParse,pRow)==0 /* condition (c) above */
134345 || (pLeft->pSrc->nSrc==0 &&
134346 exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */
134347 || IN_SPECIAL_PARSE(pParse->eParseMode!=0)
134348 ){
134349 /* The co-routine method cannot be used. Fall back to UNION ALL. */
134350 Select *pSelect = 0;
134351 int f = SF_Values0x0000200 | SF_MultiValue0x0000400;
134352 if( pLeft->pSrc->nSrc ){
134353 sqlite3MultiValuesEnd(pParse, pLeft);
134354 f = SF_Values0x0000200;
134355 }else if( pLeft->pPrior ){
134356 /* In this case set the SF_MultiValue flag only if it was set on pLeft */
134357 f = (f & pLeft->selFlags);
134358 }
134359 pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0);
134360 pLeft->selFlags &= ~SF_MultiValue0x0000400;
134361 if( pSelect ){
134362 pSelect->op = TK_ALL136;
134363 pSelect->pPrior = pLeft;
134364 pLeft = pSelect;
134365 }
134366 }else{
134367 SrcItem *p = 0; /* SrcItem that reads from co-routine */
134368
134369 if( pLeft->pSrc->nSrc==0 ){
134370 /* Co-routine has not yet been started and the special Select object
134371 ** that accesses the co-routine has not yet been created. This block
134372 ** does both those things. */
134373 Vdbe *v = sqlite3GetVdbe(pParse);
134374 Select *pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0);
134375
134376 /* Ensure the database schema has been read. This is to ensure we have
134377 ** the correct text encoding. */
134378 if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk0x0010)==0 ){
134379 sqlite3ReadSchema(pParse);
134380 }
134381
134382 if( pRet ){
134383 SelectDest dest;
134384 Subquery *pSubq;
134385 pRet->pSrc->nSrc = 1;
134386 pRet->pPrior = pLeft->pPrior;
134387 pRet->op = pLeft->op;
134388 if( pRet->pPrior ) pRet->selFlags |= SF_Values0x0000200;
134389 pLeft->pPrior = 0;
134390 pLeft->op = TK_SELECT139;
134391 assert( pLeft->pNext==0 )((void) (0));
134392 assert( pRet->pNext==0 )((void) (0));
134393 p = &pRet->pSrc->a[0];
134394 p->fg.viaCoroutine = 1;
134395 p->iCursor = -1;
134396 assert( !p->fg.isIndexedBy && !p->fg.isTabFunc )((void) (0));
134397 p->u1.nRow = 2;
134398 if( sqlite3SrcItemAttachSubquery(pParse, p, pLeft, 0) ){
134399 pSubq = p->u4.pSubq;
134400 pSubq->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1;
134401 pSubq->regReturn = ++pParse->nMem;
134402 sqlite3VdbeAddOp3(v, OP_InitCoroutine11,
134403 pSubq->regReturn, 0, pSubq->addrFillSub);
134404 sqlite3SelectDestInit(&dest, SRT_Coroutine13, pSubq->regReturn);
134405
134406 /* Allocate registers for the output of the co-routine. Do so so
134407 ** that there are two unused registers immediately before those
134408 ** used by the co-routine. This allows the code in sqlite3Insert()
134409 ** to use these registers directly, instead of copying the output
134410 ** of the co-routine to a separate array for processing. */
134411 dest.iSdst = pParse->nMem + 3;
134412 dest.nSdst = pLeft->pEList->nExpr;
134413 pParse->nMem += 2 + dest.nSdst;
134414
134415 pLeft->selFlags |= SF_MultiValue0x0000400;
134416 sqlite3Select(pParse, pLeft, &dest);
134417 pSubq->regResult = dest.iSdst;
134418 assert( pParse->nErr || dest.iSdst>0 )((void) (0));
134419 }
134420 pLeft = pRet;
134421 }
134422 }else{
134423 p = &pLeft->pSrc->a[0];
134424 assert( !p->fg.isTabFunc && !p->fg.isIndexedBy )((void) (0));
134425 p->u1.nRow++;
134426 }
134427
134428 if( pParse->nErr==0 ){
134429 Subquery *pSubq;
134430 assert( p!=0 )((void) (0));
134431 assert( p->fg.isSubquery )((void) (0));
134432 pSubq = p->u4.pSubq;
134433 assert( pSubq!=0 )((void) (0));
134434 assert( pSubq->pSelect!=0 )((void) (0));
134435 assert( pSubq->pSelect->pEList!=0 )((void) (0));
134436 if( pSubq->pSelect->pEList->nExpr!=pRow->nExpr ){
134437 sqlite3SelectWrongNumTermsError(pParse, pSubq->pSelect);
134438 }else{
134439 sqlite3ExprCodeExprList(pParse, pRow, pSubq->regResult, 0, 0);
134440 sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield12, pSubq->regReturn);
134441 }
134442 }
134443 sqlite3ExprListDelete(pParse->db, pRow);
134444 }
134445
134446 return pLeft;
134447}
134448
134449/* Forward declaration */
134450static int xferOptimization(
134451 Parse *pParse, /* Parser context */
134452 Table *pDest, /* The table we are inserting into */
134453 Select *pSelect, /* A SELECT statement to use as the data source */
134454 int onError, /* How to handle constraint errors */
134455 int iDbDest /* The database of pDest */
134456);
134457
134458/*
134459** This routine is called to handle SQL of the following forms:
134460**
134461** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
134462** insert into TABLE (IDLIST) select
134463** insert into TABLE (IDLIST) default values
134464**
134465** The IDLIST following the table name is always optional. If omitted,
134466** then a list of all (non-hidden) columns for the table is substituted.
134467** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
134468** is omitted.
134469**
134470** For the pSelect parameter holds the values to be inserted for the
134471** first two forms shown above. A VALUES clause is really just short-hand
134472** for a SELECT statement that omits the FROM clause and everything else
134473** that follows. If the pSelect parameter is NULL, that means that the
134474** DEFAULT VALUES form of the INSERT statement is intended.
134475**
134476** The code generated follows one of four templates. For a simple
134477** insert with data coming from a single-row VALUES clause, the code executes
134478** once straight down through. Pseudo-code follows (we call this
134479** the "1st template"):
134480**
134481** open write cursor to <table> and its indices
134482** put VALUES clause expressions into registers
134483** write the resulting record into <table>
134484** cleanup
134485**
134486** The three remaining templates assume the statement is of the form
134487**
134488** INSERT INTO <table> SELECT ...
134489**
134490** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
134491** in other words if the SELECT pulls all columns from a single table
134492** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
134493** if <table2> and <table1> are distinct tables but have identical
134494** schemas, including all the same indices, then a special optimization
134495** is invoked that copies raw records from <table2> over to <table1>.
134496** See the xferOptimization() function for the implementation of this
134497** template. This is the 2nd template.
134498**
134499** open a write cursor to <table>
134500** open read cursor on <table2>
134501** transfer all records in <table2> over to <table>
134502** close cursors
134503** foreach index on <table>
134504** open a write cursor on the <table> index
134505** open a read cursor on the corresponding <table2> index
134506** transfer all records from the read to the write cursors
134507** close cursors
134508** end foreach
134509**
134510** The 3rd template is for when the second template does not apply
134511** and the SELECT clause does not read from <table> at any time.
134512** The generated code follows this template:
134513**
134514** X <- A
134515** goto B
134516** A: setup for the SELECT
134517** loop over the rows in the SELECT
134518** load values into registers R..R+n
134519** yield X
134520** end loop
134521** cleanup after the SELECT
134522** end-coroutine X
134523** B: open write cursor to <table> and its indices
134524** C: yield X, at EOF goto D
134525** insert the select result into <table> from R..R+n
134526** goto C
134527** D: cleanup
134528**
134529** The 4th template is used if the insert statement takes its
134530** values from a SELECT but the data is being inserted into a table
134531** that is also read as part of the SELECT. In the third form,
134532** we have to use an intermediate table to store the results of
134533** the select. The template is like this:
134534**
134535** X <- A
134536** goto B
134537** A: setup for the SELECT
134538** loop over the tables in the SELECT
134539** load value into register R..R+n
134540** yield X
134541** end loop
134542** cleanup after the SELECT
134543** end co-routine R
134544** B: open temp table
134545** L: yield X, at EOF goto M
134546** insert row from R..R+n into temp table
134547** goto L
134548** M: open write cursor to <table> and its indices
134549** rewind temp table
134550** C: loop over rows of intermediate table
134551** transfer values form intermediate table into <table>
134552** end loop
134553** D: cleanup
134554*/
134555SQLITE_PRIVATEstatic void sqlite3Insert(
134556 Parse *pParse, /* Parser context */
134557 SrcList *pTabList, /* Name of table into which we are inserting */
134558 Select *pSelect, /* A SELECT statement to use as the data source */
134559 IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */
134560 int onError, /* How to handle constraint errors */
134561 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
134562){
134563 sqlite3 *db; /* The main database structure */
134564 Table *pTab; /* The table to insert into. aka TABLE */
134565 int i, j; /* Loop counters */
134566 Vdbe *v; /* Generate code into this virtual machine */
134567 Index *pIdx; /* For looping over indices of the table */
134568 int nColumn; /* Number of columns in the data */
134569 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
134570 int iDataCur = 0; /* VDBE cursor that is the main data repository */
134571 int iIdxCur = 0; /* First index cursor */
134572 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
134573 int endOfLoop; /* Label for the end of the insertion loop */
134574 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
134575 int addrInsTop = 0; /* Jump to label "D" */
134576 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
134577 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
134578 int iDb; /* Index of database holding TABLE */
134579 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
134580 u8 appendFlag = 0; /* True if the insert is likely to be an append */
134581 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
134582 u8 bIdListInOrder; /* True if IDLIST is in table order */
134583 ExprList *pList = 0; /* List of VALUES() to be inserted */
134584 int iRegStore; /* Register in which to store next column */
134585
134586 /* Register allocations */
134587 int regFromSelect = 0;/* Base register for data coming from SELECT */
134588 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
134589 int regRowCount = 0; /* Memory cell used for the row counter */
134590 int regIns; /* Block of regs holding rowid+data being inserted */
134591 int regRowid; /* registers holding insert rowid */
134592 int regData; /* register holding first column to insert */
134593 int *aRegIdx = 0; /* One register allocated to each index */
134594
134595#ifndef SQLITE_OMIT_TRIGGER
134596 int isView; /* True if attempting to insert into a view */
134597 Trigger *pTrigger; /* List of triggers on pTab, if required */
134598 int tmask; /* Mask of trigger times */
134599#endif
134600
134601 db = pParse->db;
134602 assert( db->pParse==pParse )((void) (0));
134603 if( pParse->nErr ){
134604 goto insert_cleanup;
134605 }
134606 assert( db->mallocFailed==0 )((void) (0));
134607 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
134608
134609 /* If the Select object is really just a simple VALUES() list with a
134610 ** single row (the common case) then keep that one row of values
134611 ** and discard the other (unused) parts of the pSelect object
134612 */
134613 if( pSelect && (pSelect->selFlags & SF_Values0x0000200)!=0 && pSelect->pPrior==0 ){
134614 pList = pSelect->pEList;
134615 pSelect->pEList = 0;
134616 sqlite3SelectDelete(db, pSelect);
134617 pSelect = 0;
134618 }
134619
134620 /* Locate the table into which we will be inserting new information.
134621 */
134622 assert( pTabList->nSrc==1 )((void) (0));
134623 pTab = sqlite3SrcListLookup(pParse, pTabList);
134624 if( pTab==0 ){
134625 goto insert_cleanup;
134626 }
134627 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134628 assert( iDb<db->nDb )((void) (0));
134629 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, pTab->zName, 0,
134630 db->aDb[iDb].zDbSName) ){
134631 goto insert_cleanup;
134632 }
134633 withoutRowid = !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0);
134634
134635 /* Figure out if we have any triggers and if the table being
134636 ** inserted into is a view
134637 */
134638#ifndef SQLITE_OMIT_TRIGGER
134639 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT128, 0, &tmask);
134640 isView = IsView(pTab)((pTab)->eTabType==2);
134641#else
134642# define pTrigger 0
134643# define tmask 0
134644# define isView 0
134645#endif
134646#ifdef SQLITE_OMIT_VIEW
134647# undef isView
134648# define isView 0
134649#endif
134650 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) )((void) (0));
134651
134652#if TREETRACE_ENABLED0
134653 if( sqlite3TreeTrace & 0x10000 ){
134654 sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__134654);
134655 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
134656 onError, pUpsert, pTrigger);
134657 }
134658#endif
134659
134660 /* If pTab is really a view, make sure it has been initialized.
134661 ** ViewGetColumnNames() is a no-op if pTab is not a view.
134662 */
134663 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
134664 goto insert_cleanup;
134665 }
134666
134667 /* Cannot insert into a read-only table.
134668 */
134669 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
134670 goto insert_cleanup;
134671 }
134672
134673 /* Allocate a VDBE
134674 */
134675 v = sqlite3GetVdbe(pParse);
134676 if( v==0 ) goto insert_cleanup;
134677 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
134678 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
134679
134680#ifndef SQLITE_OMIT_XFER_OPT
134681 /* If the statement is of the form
134682 **
134683 ** INSERT INTO <table1> SELECT * FROM <table2>;
134684 **
134685 ** Then special optimizations can be applied that make the transfer
134686 ** very fast and which reduce fragmentation of indices.
134687 **
134688 ** This is the 2nd template.
134689 */
134690 if( pColumn==0
134691 && pSelect!=0
134692 && pTrigger==0
134693 && xferOptimization(pParse, pTab, pSelect, onError, iDb)
134694 ){
134695 assert( !pTrigger )((void) (0));
134696 assert( pList==0 )((void) (0));
134697 goto insert_end;
134698 }
134699#endif /* SQLITE_OMIT_XFER_OPT */
134700
134701 /* If this is an AUTOINCREMENT table, look up the sequence number in the
134702 ** sqlite_sequence table and store it in memory cell regAutoinc.
134703 */
134704 regAutoinc = autoIncBegin(pParse, iDb, pTab);
134705
134706 /* Allocate a block registers to hold the rowid and the values
134707 ** for all columns of the new row.
134708 */
134709 regRowid = regIns = pParse->nMem+1;
134710 pParse->nMem += pTab->nCol + 1;
134711 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
134712 regRowid++;
134713 pParse->nMem++;
134714 }
134715 regData = regRowid+1;
134716
134717 /* If the INSERT statement included an IDLIST term, then make sure
134718 ** all elements of the IDLIST really are columns of the table and
134719 ** remember the column indices.
134720 **
134721 ** If the table has an INTEGER PRIMARY KEY column and that column
134722 ** is named in the IDLIST, then record in the ipkColumn variable
134723 ** the index into IDLIST of the primary key column. ipkColumn is
134724 ** the index of the primary key as it appears in IDLIST, not as
134725 ** is appears in the original table. (The index of the INTEGER
134726 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
134727 ** loop, if ipkColumn==(-1), that means that integer primary key
134728 ** is unspecified, and hence the table is either WITHOUT ROWID or
134729 ** it will automatically generated an integer primary key.
134730 **
134731 ** bIdListInOrder is true if the columns in IDLIST are in storage
134732 ** order. This enables an optimization that avoids shuffling the
134733 ** columns into storage order. False negatives are harmless,
134734 ** but false positives will cause database corruption.
134735 */
134736 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden0x00000400|TF_HasStored0x00000040))==0;
134737 if( pColumn ){
134738 assert( pColumn->eU4!=EU4_EXPR )((void) (0));
134739 pColumn->eU4 = EU4_IDX1;
134740 for(i=0; i<pColumn->nId; i++){
134741 pColumn->a[i].u4.idx = -1;
134742 }
134743 for(i=0; i<pColumn->nId; i++){
134744 for(j=0; j<pTab->nCol; j++){
134745 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
134746 pColumn->a[i].u4.idx = j;
134747 if( i!=j ) bIdListInOrder = 0;
134748 if( j==pTab->iPKey ){
134749 ipkColumn = i; assert( !withoutRowid )((void) (0));
134750 }
134751#ifndef SQLITE_OMIT_GENERATED_COLUMNS
134752 if( pTab->aCol[j].colFlags & (COLFLAG_STORED0x0040|COLFLAG_VIRTUAL0x0020) ){
134753 sqlite3ErrorMsg(pParse,
134754 "cannot INSERT into generated column \"%s\"",
134755 pTab->aCol[j].zCnName);
134756 goto insert_cleanup;
134757 }
134758#endif
134759 break;
134760 }
134761 }
134762 if( j>=pTab->nCol ){
134763 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
134764 ipkColumn = i;
134765 bIdListInOrder = 0;
134766 }else{
134767 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
134768 pTabList->a, pColumn->a[i].zName);
134769 pParse->checkSchema = 1;
134770 goto insert_cleanup;
134771 }
134772 }
134773 }
134774 }
134775
134776 /* Figure out how many columns of data are supplied. If the data
134777 ** is coming from a SELECT statement, then generate a co-routine that
134778 ** produces a single row of the SELECT on each invocation. The
134779 ** co-routine is the common header to the 3rd and 4th templates.
134780 */
134781 if( pSelect ){
134782 /* Data is coming from a SELECT or from a multi-row VALUES clause.
134783 ** Generate a co-routine to run the SELECT. */
134784 int rc; /* Result code */
134785
134786 if( pSelect->pSrc->nSrc==1
134787 && pSelect->pSrc->a[0].fg.viaCoroutine
134788 && pSelect->pPrior==0
134789 ){
134790 SrcItem *pItem = &pSelect->pSrc->a[0];
134791 Subquery *pSubq;
134792 assert( pItem->fg.isSubquery )((void) (0));
134793 pSubq = pItem->u4.pSubq;
134794 dest.iSDParm = pSubq->regReturn;
134795 regFromSelect = pSubq->regResult;
134796 assert( pSubq->pSelect!=0 )((void) (0));
134797 assert( pSubq->pSelect->pEList!=0 )((void) (0));
134798 nColumn = pSubq->pSelect->pEList->nExpr;
134799 ExplainQueryPlan((pParse, 0, "SCAN %S", pItem))sqlite3VdbeExplain (pParse, 0, "SCAN %S", pItem);
134800 if( bIdListInOrder && nColumn==pTab->nCol ){
134801 regData = regFromSelect;
134802 regRowid = regData - 1;
134803 regIns = regRowid - (IsVirtual(pTab)((pTab)->eTabType==1) ? 1 : 0);
134804 }
134805 }else{
134806 int addrTop; /* Top of the co-routine */
134807 int regYield = ++pParse->nMem;
134808 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
134809 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, addrTop);
134810 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
134811 dest.iSdst = bIdListInOrder ? regData : 0;
134812 dest.nSdst = pTab->nCol;
134813 rc = sqlite3Select(pParse, pSelect, &dest);
134814 regFromSelect = dest.iSdst;
134815 assert( db->pParse==pParse )((void) (0));
134816 if( rc || pParse->nErr ) goto insert_cleanup;
134817 assert( db->mallocFailed==0 )((void) (0));
134818 sqlite3VdbeEndCoroutine(v, regYield);
134819 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
134820 assert( pSelect->pEList )((void) (0));
134821 nColumn = pSelect->pEList->nExpr;
134822 }
134823
134824 /* Set useTempTable to TRUE if the result of the SELECT statement
134825 ** should be written into a temporary table (template 4). Set to
134826 ** FALSE if each output row of the SELECT can be written directly into
134827 ** the destination table (template 3).
134828 **
134829 ** A temp table must be used if the table being updated is also one
134830 ** of the tables being read by the SELECT statement. Also use a
134831 ** temp table in the case of row triggers.
134832 */
134833 if( pTrigger || readsTable(pParse, iDb, pTab) ){
134834 useTempTable = 1;
134835 }
134836
134837 if( useTempTable ){
134838 /* Invoke the coroutine to extract information from the SELECT
134839 ** and add it to a transient table srcTab. The code generated
134840 ** here is from the 4th template:
134841 **
134842 ** B: open temp table
134843 ** L: yield X, goto M at EOF
134844 ** insert row from R..R+n into temp table
134845 ** goto L
134846 ** M: ...
134847 */
134848 int regRec; /* Register to hold packed record */
134849 int regTempRowid; /* Register to hold temp table ROWID */
134850 int addrL; /* Label "L" */
134851
134852 srcTab = pParse->nTab++;
134853 regRec = sqlite3GetTempReg(pParse);
134854 regTempRowid = sqlite3GetTempReg(pParse);
134855 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, srcTab, nColumn);
134856 addrL = sqlite3VdbeAddOp1(v, OP_Yield12, dest.iSDParm); VdbeCoverage(v);
134857 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regFromSelect, nColumn, regRec);
134858 sqlite3VdbeAddOp2(v, OP_NewRowid127, srcTab, regTempRowid);
134859 sqlite3VdbeAddOp3(v, OP_Insert128, srcTab, regRec, regTempRowid);
134860 sqlite3VdbeGoto(v, addrL);
134861 sqlite3VdbeJumpHere(v, addrL);
134862 sqlite3ReleaseTempReg(pParse, regRec);
134863 sqlite3ReleaseTempReg(pParse, regTempRowid);
134864 }
134865 }else{
134866 /* This is the case if the data for the INSERT is coming from a
134867 ** single-row VALUES clause
134868 */
134869 NameContext sNC;
134870 memset(&sNC, 0, sizeof(sNC));
134871 sNC.pParse = pParse;
134872 srcTab = -1;
134873 assert( useTempTable==0 )((void) (0));
134874 if( pList ){
134875 nColumn = pList->nExpr;
134876 if( sqlite3ResolveExprListNames(&sNC, pList) ){
134877 goto insert_cleanup;
134878 }
134879 }else{
134880 nColumn = 0;
134881 }
134882 }
134883
134884 /* If there is no IDLIST term but the table has an integer primary
134885 ** key, the set the ipkColumn variable to the integer primary key
134886 ** column index in the original table definition.
134887 */
134888 if( pColumn==0 && nColumn>0 ){
134889 ipkColumn = pTab->iPKey;
134890#ifndef SQLITE_OMIT_GENERATED_COLUMNS
134891 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated0x00000060)!=0 ){
134892 testcase( pTab->tabFlags & TF_HasVirtual );
134893 testcase( pTab->tabFlags & TF_HasStored );
134894 for(i=ipkColumn-1; i>=0; i--){
134895 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
134896 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
134897 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
134898 ipkColumn--;
134899 }
134900 }
134901 }
134902#endif
134903
134904 /* Make sure the number of columns in the source data matches the number
134905 ** of columns to be inserted into the table.
134906 */
134907 assert( TF_HasHidden==COLFLAG_HIDDEN )((void) (0));
134908 assert( TF_HasGenerated==COLFLAG_GENERATED )((void) (0));
134909 assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) )((void) (0));
134910 if( (pTab->tabFlags & (TF_HasGenerated0x00000060|TF_HasHidden0x00000002))!=0 ){
134911 for(i=0; i<pTab->nCol; i++){
134912 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT0x0062 ) nHidden++;
134913 }
134914 }
134915 if( nColumn!=(pTab->nCol-nHidden) ){
134916 sqlite3ErrorMsg(pParse,
134917 "table %S has %d columns but %d values were supplied",
134918 pTabList->a, pTab->nCol-nHidden, nColumn);
134919 goto insert_cleanup;
134920 }
134921 }
134922 if( pColumn!=0 && nColumn!=pColumn->nId ){
134923 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
134924 goto insert_cleanup;
134925 }
134926
134927 /* Initialize the count of rows to be inserted
134928 */
134929 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0
134930 && !pParse->nested
134931 && !pParse->pTriggerTab
134932 && !pParse->bReturning
134933 ){
134934 regRowCount = ++pParse->nMem;
134935 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regRowCount);
134936 }
134937
134938 /* If this is not a view, open the table and and all indices */
134939 if( !isView ){
134940 int nIdx;
134941 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, 0, -1, 0,
134942 &iDataCur, &iIdxCur);
134943 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
134944 if( aRegIdx==0 ){
134945 goto insert_cleanup;
134946 }
134947 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
134948 assert( pIdx )((void) (0));
134949 aRegIdx[i] = ++pParse->nMem;
134950 pParse->nMem += pIdx->nColumn;
134951 }
134952 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
134953 }
134954#ifndef SQLITE_OMIT_UPSERT
134955 if( pUpsert ){
134956 Upsert *pNx;
134957 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
134958 sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
134959 pTab->zName);
134960 goto insert_cleanup;
134961 }
134962 if( IsView(pTab)((pTab)->eTabType==2) ){
134963 sqlite3ErrorMsg(pParse, "cannot UPSERT a view");
134964 goto insert_cleanup;
134965 }
134966 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
134967 goto insert_cleanup;
134968 }
134969 pTabList->a[0].iCursor = iDataCur;
134970 pNx = pUpsert;
134971 do{
134972 pNx->pUpsertSrc = pTabList;
134973 pNx->regData = regData;
134974 pNx->iDataCur = iDataCur;
134975 pNx->iIdxCur = iIdxCur;
134976 if( pNx->pUpsertTarget ){
134977 if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx, pUpsert) ){
134978 goto insert_cleanup;
134979 }
134980 }
134981 pNx = pNx->pNextUpsert;
134982 }while( pNx!=0 );
134983 }
134984#endif
134985
134986
134987 /* This is the top of the main insertion loop */
134988 if( useTempTable ){
134989 /* This block codes the top of loop only. The complete loop is the
134990 ** following pseudocode (template 4):
134991 **
134992 ** rewind temp table, if empty goto D
134993 ** C: loop over rows of intermediate table
134994 ** transfer values form intermediate table into <table>
134995 ** end loop
134996 ** D: ...
134997 */
134998 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind36, srcTab); VdbeCoverage(v);
134999 addrCont = sqlite3VdbeCurrentAddr(v);
135000 }else if( pSelect ){
135001 /* This block codes the top of loop only. The complete loop is the
135002 ** following pseudocode (template 3):
135003 **
135004 ** C: yield X, at EOF goto D
135005 ** insert the select result into <table> from R..R+n
135006 ** goto C
135007 ** D: ...
135008 */
135009 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
135010 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield12, dest.iSDParm);
135011 VdbeCoverage(v);
135012 if( ipkColumn>=0 ){
135013 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
135014 ** SELECT, go ahead and copy the value into the rowid slot now, so that
135015 ** the value does not get overwritten by a NULL at tag-20191021-002. */
135016 sqlite3VdbeAddOp2(v, OP_Copy80, regFromSelect+ipkColumn, regRowid);
135017 }
135018 }
135019
135020 /* Compute data for ordinary columns of the new entry. Values
135021 ** are written in storage order into registers starting with regData.
135022 ** Only ordinary columns are computed in this loop. The rowid
135023 ** (if there is one) is computed later and generated columns are
135024 ** computed after the rowid since they might depend on the value
135025 ** of the rowid.
135026 */
135027 nHidden = 0;
135028 iRegStore = regData; assert( regData==regRowid+1 )((void) (0));
135029 for(i=0; i<pTab->nCol; i++, iRegStore++){
135030 int k;
135031 u32 colFlags;
135032 assert( i>=nHidden )((void) (0));
135033 if( i==pTab->iPKey ){
135034 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
135035 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
135036 ** using excess space. The file format definition requires this extra
135037 ** NULL - we cannot optimize further by skipping the column completely */
135038 sqlite3VdbeAddOp1(v, OP_SoftNull76, iRegStore);
135039 continue;
135040 }
135041 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT0x0062)!=0 ){
135042 nHidden++;
135043 if( (colFlags & COLFLAG_VIRTUAL0x0020)!=0 ){
135044 /* Virtual columns do not participate in OP_MakeRecord. So back up
135045 ** iRegStore by one slot to compensate for the iRegStore++ in the
135046 ** outer for() loop */
135047 iRegStore--;
135048 continue;
135049 }else if( (colFlags & COLFLAG_STORED0x0040)!=0 ){
135050 /* Stored columns are computed later. But if there are BEFORE
135051 ** triggers, the slots used for stored columns will be OP_Copy-ed
135052 ** to a second block of registers, so the register needs to be
135053 ** initialized to NULL to avoid an uninitialized register read */
135054 if( tmask & TRIGGER_BEFORE1 ){
135055 sqlite3VdbeAddOp1(v, OP_SoftNull76, iRegStore);
135056 }
135057 continue;
135058 }else if( pColumn==0 ){
135059 /* Hidden columns that are not explicitly named in the INSERT
135060 ** get there default value */
135061 sqlite3ExprCodeFactorable(pParse,
135062 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
135063 iRegStore);
135064 continue;
135065 }
135066 }
135067 if( pColumn ){
135068 assert( pColumn->eU4==EU4_IDX )((void) (0));
135069 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
135070 if( j>=pColumn->nId ){
135071 /* A column not named in the insert column list gets its
135072 ** default value */
135073 sqlite3ExprCodeFactorable(pParse,
135074 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
135075 iRegStore);
135076 continue;
135077 }
135078 k = j;
135079 }else if( nColumn==0 ){
135080 /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */
135081 sqlite3ExprCodeFactorable(pParse,
135082 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
135083 iRegStore);
135084 continue;
135085 }else{
135086 k = i - nHidden;
135087 }
135088
135089 if( useTempTable ){
135090 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, k, iRegStore);
135091 }else if( pSelect ){
135092 if( regFromSelect!=regData ){
135093 sqlite3VdbeAddOp2(v, OP_SCopy81, regFromSelect+k, iRegStore);
135094 }
135095 }else{
135096 Expr *pX = pList->a[k].pExpr;
135097 int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore);
135098 if( y!=iRegStore ){
135099 sqlite3VdbeAddOp2(v,
135100 ExprHasProperty(pX, EP_Subquery)(((pX)->flags&(0x400000))!=0) ? OP_Copy80 : OP_SCopy81, y, iRegStore);
135101 }
135102 }
135103 }
135104
135105
135106 /* Run the BEFORE and INSTEAD OF triggers, if there are any
135107 */
135108 endOfLoop = sqlite3VdbeMakeLabel(pParse);
135109 if( tmask & TRIGGER_BEFORE1 ){
135110 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
135111
135112 /* build the NEW.* reference row. Note that if there is an INTEGER
135113 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
135114 ** translated into a unique ID for the row. But on a BEFORE trigger,
135115 ** we do not know what the unique ID will be (because the insert has
135116 ** not happened yet) so we substitute a rowid of -1
135117 */
135118 if( ipkColumn<0 ){
135119 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regCols);
135120 }else{
135121 int addr1;
135122 assert( !withoutRowid )((void) (0));
135123 if( useTempTable ){
135124 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, ipkColumn, regCols);
135125 }else{
135126 assert( pSelect==0 )((void) (0)); /* Otherwise useTempTable is true */
135127 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
135128 }
135129 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull52, regCols); VdbeCoverage(v);
135130 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regCols);
135131 sqlite3VdbeJumpHere(v, addr1);
135132 sqlite3VdbeAddOp1(v, OP_MustBeInt13, regCols); VdbeCoverage(v);
135133 }
135134
135135 /* Copy the new data already generated. */
135136 assert( pTab->nNVCol>0 || pParse->nErr>0 )((void) (0));
135137 sqlite3VdbeAddOp3(v, OP_Copy80, regRowid+1, regCols+1, pTab->nNVCol-1);
135138
135139#ifndef SQLITE_OMIT_GENERATED_COLUMNS
135140 /* Compute the new value for generated columns after all other
135141 ** columns have already been computed. This must be done after
135142 ** computing the ROWID in case one of the generated columns
135143 ** refers to the ROWID. */
135144 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
135145 testcase( pTab->tabFlags & TF_HasVirtual );
135146 testcase( pTab->tabFlags & TF_HasStored );
135147 sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab);
135148 }
135149#endif
135150
135151 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
135152 ** do not attempt any conversions before assembling the record.
135153 ** If this is a real table, attempt conversions as required by the
135154 ** table column affinities.
135155 */
135156 if( !isView ){
135157 sqlite3TableAffinity(v, pTab, regCols+1);
135158 }
135159
135160 /* Fire BEFORE or INSTEAD OF triggers */
135161 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT128, 0, TRIGGER_BEFORE1,
135162 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
135163
135164 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
135165 }
135166
135167 if( !isView ){
135168 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
135169 /* The row that the VUpdate opcode will delete: none */
135170 sqlite3VdbeAddOp2(v, OP_Null75, 0, regIns);
135171 }
135172 if( ipkColumn>=0 ){
135173 /* Compute the new rowid */
135174 if( useTempTable ){
135175 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, ipkColumn, regRowid);
135176 }else if( pSelect ){
135177 /* Rowid already initialized at tag-20191021-001 */
135178 }else{
135179 Expr *pIpk = pList->a[ipkColumn].pExpr;
135180 if( pIpk->op==TK_NULL122 && !IsVirtual(pTab)((pTab)->eTabType==1) ){
135181 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
135182 appendFlag = 1;
135183 }else{
135184 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
135185 }
135186 }
135187 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
135188 ** to generate a unique primary key value.
135189 */
135190 if( !appendFlag ){
135191 int addr1;
135192 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
135193 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull52, regRowid); VdbeCoverage(v);
135194 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
135195 sqlite3VdbeJumpHere(v, addr1);
135196 }else{
135197 addr1 = sqlite3VdbeCurrentAddr(v);
135198 sqlite3VdbeAddOp2(v, OP_IsNull51, regRowid, addr1+2); VdbeCoverage(v);
135199 }
135200 sqlite3VdbeAddOp1(v, OP_MustBeInt13, regRowid); VdbeCoverage(v);
135201 }
135202 }else if( IsVirtual(pTab)((pTab)->eTabType==1) || withoutRowid ){
135203 sqlite3VdbeAddOp2(v, OP_Null75, 0, regRowid);
135204 }else{
135205 sqlite3VdbeAddOp3(v, OP_NewRowid127, iDataCur, regRowid, regAutoinc);
135206 appendFlag = 1;
135207 }
135208 autoIncStep(pParse, regAutoinc, regRowid);
135209
135210#ifndef SQLITE_OMIT_GENERATED_COLUMNS
135211 /* Compute the new value for generated columns after all other
135212 ** columns have already been computed. This must be done after
135213 ** computing the ROWID in case one of the generated columns
135214 ** is derived from the INTEGER PRIMARY KEY. */
135215 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
135216 sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab);
135217 }
135218#endif
135219
135220 /* Generate code to check constraints and generate index keys and
135221 ** do the insertion.
135222 */
135223#ifndef SQLITE_OMIT_VIRTUALTABLE
135224 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
135225 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
135226 sqlite3VtabMakeWritable(pParse, pTab);
135227 sqlite3VdbeAddOp4(v, OP_VUpdate7, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB(-11));
135228 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
135229 sqlite3MayAbort(pParse);
135230 }else
135231#endif
135232 {
135233 int isReplace = 0;/* Set to true if constraints may cause a replace */
135234 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
135235 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
135236 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
135237 );
135238 if( db->flags & SQLITE_ForeignKeys0x00004000 ){
135239 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
135240 }
135241
135242 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
135243 ** constraints or (b) there are no triggers and this table is not a
135244 ** parent table in a foreign key constraint. It is safe to set the
135245 ** flag in the second case as if any REPLACE constraint is hit, an
135246 ** OP_Delete or OP_IdxDelete instruction will be executed on each
135247 ** cursor that is disturbed. And these instructions both clear the
135248 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
135249 ** functionality. */
135250 bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v));
135251 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
135252 regIns, aRegIdx, 0, appendFlag, bUseSeek
135253 );
135254 }
135255#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
135256 }else if( pParse->bReturning ){
135257 /* If there is a RETURNING clause, populate the rowid register with
135258 ** constant value -1, in case one or more of the returned expressions
135259 ** refer to the "rowid" of the view. */
135260 sqlite3VdbeAddOp2(v, OP_Integer71, -1, regRowid);
135261#endif
135262 }
135263
135264 /* Update the count of rows that are inserted
135265 */
135266 if( regRowCount ){
135267 sqlite3VdbeAddOp2(v, OP_AddImm86, regRowCount, 1);
135268 }
135269
135270 if( pTrigger ){
135271 /* Code AFTER triggers */
135272 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT128, 0, TRIGGER_AFTER2,
135273 pTab, regData-2-pTab->nCol, onError, endOfLoop);
135274 }
135275
135276 /* The bottom of the main insertion loop, if the data source
135277 ** is a SELECT statement.
135278 */
135279 sqlite3VdbeResolveLabel(v, endOfLoop);
135280 if( useTempTable ){
135281 sqlite3VdbeAddOp2(v, OP_Next39, srcTab, addrCont); VdbeCoverage(v);
135282 sqlite3VdbeJumpHere(v, addrInsTop);
135283 sqlite3VdbeAddOp1(v, OP_Close122, srcTab);
135284 }else if( pSelect ){
135285 sqlite3VdbeGoto(v, addrCont);
135286#ifdef SQLITE_DEBUG
135287 /* If we are jumping back to an OP_Yield that is preceded by an
135288 ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the
135289 ** OP_ReleaseReg will be included in the loop. */
135290 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg186 ){
135291 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield )((void) (0));
135292 sqlite3VdbeChangeP5(v, 1);
135293 }
135294#endif
135295 sqlite3VdbeJumpHere(v, addrInsTop);
135296 }
135297
135298#ifndef SQLITE_OMIT_XFER_OPT
135299insert_end:
135300#endif /* SQLITE_OMIT_XFER_OPT */
135301 /* Update the sqlite_sequence table by storing the content of the
135302 ** maximum rowid counter values recorded while inserting into
135303 ** autoincrement tables.
135304 */
135305 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
135306 sqlite3AutoincrementEnd(pParse);
135307 }
135308
135309 /*
135310 ** Return the number of rows inserted. If this routine is
135311 ** generating code because of a call to sqlite3NestedParse(), do not
135312 ** invoke the callback function.
135313 */
135314 if( regRowCount ){
135315 sqlite3CodeChangeCount(v, regRowCount, "rows inserted");
135316 }
135317
135318insert_cleanup:
135319 sqlite3SrcListDelete(db, pTabList);
135320 sqlite3ExprListDelete(db, pList);
135321 sqlite3UpsertDelete(db, pUpsert);
135322 sqlite3SelectDelete(db, pSelect);
135323 sqlite3IdListDelete(db, pColumn);
135324 if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx);
135325}
135326
135327/* Make sure "isView" and other macros defined above are undefined. Otherwise
135328** they may interfere with compilation of other functions in this file
135329** (or in another file, if this file becomes part of the amalgamation). */
135330#ifdef isView
135331 #undef isView
135332#endif
135333#ifdef pTrigger
135334 #undef pTrigger
135335#endif
135336#ifdef tmask
135337 #undef tmask
135338#endif
135339
135340/*
135341** Meanings of bits in of pWalker->eCode for
135342** sqlite3ExprReferencesUpdatedColumn()
135343*/
135344#define CKCNSTRNT_COLUMN0x01 0x01 /* CHECK constraint uses a changing column */
135345#define CKCNSTRNT_ROWID0x02 0x02 /* CHECK constraint references the ROWID */
135346
135347/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
135348* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
135349** expression node references any of the
135350** columns that are being modified by an UPDATE statement.
135351*/
135352static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
135353 if( pExpr->op==TK_COLUMN168 ){
135354 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 )((void) (0));
135355 if( pExpr->iColumn>=0 ){
135356 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
135357 pWalker->eCode |= CKCNSTRNT_COLUMN0x01;
135358 }
135359 }else{
135360 pWalker->eCode |= CKCNSTRNT_ROWID0x02;
135361 }
135362 }
135363 return WRC_Continue0;
135364}
135365
135366/*
135367** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
135368** only columns that are modified by the UPDATE are those for which
135369** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
135370**
135371** Return true if CHECK constraint pExpr uses any of the
135372** changing columns (or the rowid if it is changing). In other words,
135373** return true if this CHECK constraint must be validated for
135374** the new row in the UPDATE statement.
135375**
135376** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
135377** The operation of this routine is the same - return true if an only if
135378** the expression uses one or more of columns identified by the second and
135379** third arguments.
135380*/
135381SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(
135382 Expr *pExpr, /* The expression to be checked */
135383 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
135384 int chngRowid /* True if UPDATE changes the rowid */
135385){
135386 Walker w;
135387 memset(&w, 0, sizeof(w));
135388 w.eCode = 0;
135389 w.xExprCallback = checkConstraintExprNode;
135390 w.u.aiCol = aiChng;
135391 sqlite3WalkExpr(&w, pExpr);
135392 if( !chngRowid ){
135393 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
135394 w.eCode &= ~CKCNSTRNT_ROWID0x02;
135395 }
135396 testcase( w.eCode==0 );
135397 testcase( w.eCode==CKCNSTRNT_COLUMN );
135398 testcase( w.eCode==CKCNSTRNT_ROWID );
135399 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
135400 return w.eCode!=0;
135401}
135402
135403/*
135404** The sqlite3GenerateConstraintChecks() routine usually wants to visit
135405** the indexes of a table in the order provided in the Table->pIndex list.
135406** However, sometimes (rarely - when there is an upsert) it wants to visit
135407** the indexes in a different order. The following data structures accomplish
135408** this.
135409**
135410** The IndexIterator object is used to walk through all of the indexes
135411** of a table in either Index.pNext order, or in some other order established
135412** by an array of IndexListTerm objects.
135413*/
135414typedef struct IndexListTerm IndexListTerm;
135415typedef struct IndexIterator IndexIterator;
135416struct IndexIterator {
135417 int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */
135418 int i; /* Index of the current item from the list */
135419 union {
135420 struct { /* Use this object for eType==0: A Index.pNext list */
135421 Index *pIdx; /* The current Index */
135422 } lx;
135423 struct { /* Use this object for eType==1; Array of IndexListTerm */
135424 int nIdx; /* Size of the array */
135425 IndexListTerm *aIdx; /* Array of IndexListTerms */
135426 } ax;
135427 } u;
135428};
135429
135430/* When IndexIterator.eType==1, then each index is an array of instances
135431** of the following object
135432*/
135433struct IndexListTerm {
135434 Index *p; /* The index */
135435 int ix; /* Which entry in the original Table.pIndex list is this index*/
135436};
135437
135438/* Return the first index on the list */
135439static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){
135440 assert( pIter->i==0 )((void) (0));
135441 if( pIter->eType ){
135442 *pIx = pIter->u.ax.aIdx[0].ix;
135443 return pIter->u.ax.aIdx[0].p;
135444 }else{
135445 *pIx = 0;
135446 return pIter->u.lx.pIdx;
135447 }
135448}
135449
135450/* Return the next index from the list. Return NULL when out of indexes */
135451static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){
135452 if( pIter->eType ){
135453 int i = ++pIter->i;
135454 if( i>=pIter->u.ax.nIdx ){
135455 *pIx = i;
135456 return 0;
135457 }
135458 *pIx = pIter->u.ax.aIdx[i].ix;
135459 return pIter->u.ax.aIdx[i].p;
135460 }else{
135461 ++(*pIx);
135462 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
135463 return pIter->u.lx.pIdx;
135464 }
135465}
135466
135467/*
135468** Generate code to do constraint checks prior to an INSERT or an UPDATE
135469** on table pTab.
135470**
135471** The regNewData parameter is the first register in a range that contains
135472** the data to be inserted or the data after the update. There will be
135473** pTab->nCol+1 registers in this range. The first register (the one
135474** that regNewData points to) will contain the new rowid, or NULL in the
135475** case of a WITHOUT ROWID table. The second register in the range will
135476** contain the content of the first table column. The third register will
135477** contain the content of the second table column. And so forth.
135478**
135479** The regOldData parameter is similar to regNewData except that it contains
135480** the data prior to an UPDATE rather than afterwards. regOldData is zero
135481** for an INSERT. This routine can distinguish between UPDATE and INSERT by
135482** checking regOldData for zero.
135483**
135484** For an UPDATE, the pkChng boolean is true if the true primary key (the
135485** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
135486** might be modified by the UPDATE. If pkChng is false, then the key of
135487** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
135488**
135489** For an INSERT, the pkChng boolean indicates whether or not the rowid
135490** was explicitly specified as part of the INSERT statement. If pkChng
135491** is zero, it means that the either rowid is computed automatically or
135492** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
135493** pkChng will only be true if the INSERT statement provides an integer
135494** value for either the rowid column or its INTEGER PRIMARY KEY alias.
135495**
135496** The code generated by this routine will store new index entries into
135497** registers identified by aRegIdx[]. No index entry is created for
135498** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
135499** the same as the order of indices on the linked list of indices
135500** at pTab->pIndex.
135501**
135502** (2019-05-07) The generated code also creates a new record for the
135503** main table, if pTab is a rowid table, and stores that record in the
135504** register identified by aRegIdx[nIdx] - in other words in the first
135505** entry of aRegIdx[] past the last index. It is important that the
135506** record be generated during constraint checks to avoid affinity changes
135507** to the register content that occur after constraint checks but before
135508** the new record is inserted.
135509**
135510** The caller must have already opened writeable cursors on the main
135511** table and all applicable indices (that is to say, all indices for which
135512** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
135513** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
135514** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
135515** for the first index in the pTab->pIndex list. Cursors for other indices
135516** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
135517**
135518** This routine also generates code to check constraints. NOT NULL,
135519** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
135520** then the appropriate action is performed. There are five possible
135521** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
135522**
135523** Constraint type Action What Happens
135524** --------------- ---------- ----------------------------------------
135525** any ROLLBACK The current transaction is rolled back and
135526** sqlite3_step() returns immediately with a
135527** return code of SQLITE_CONSTRAINT.
135528**
135529** any ABORT Back out changes from the current command
135530** only (do not do a complete rollback) then
135531** cause sqlite3_step() to return immediately
135532** with SQLITE_CONSTRAINT.
135533**
135534** any FAIL Sqlite3_step() returns immediately with a
135535** return code of SQLITE_CONSTRAINT. The
135536** transaction is not rolled back and any
135537** changes to prior rows are retained.
135538**
135539** any IGNORE The attempt in insert or update the current
135540** row is skipped, without throwing an error.
135541** Processing continues with the next row.
135542** (There is an immediate jump to ignoreDest.)
135543**
135544** NOT NULL REPLACE The NULL value is replace by the default
135545** value for that column. If the default value
135546** is NULL, the action is the same as ABORT.
135547**
135548** UNIQUE REPLACE The other row that conflicts with the row
135549** being inserted is removed.
135550**
135551** CHECK REPLACE Illegal. The results in an exception.
135552**
135553** Which action to take is determined by the overrideError parameter.
135554** Or if overrideError==OE_Default, then the pParse->onError parameter
135555** is used. Or if pParse->onError==OE_Default then the onError value
135556** for the constraint is used.
135557*/
135558SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(
135559 Parse *pParse, /* The parser context */
135560 Table *pTab, /* The table being inserted or updated */
135561 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
135562 int iDataCur, /* Canonical data cursor (main table or PK index) */
135563 int iIdxCur, /* First index cursor */
135564 int regNewData, /* First register in a range holding values to insert */
135565 int regOldData, /* Previous content. 0 for INSERTs */
135566 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
135567 u8 overrideError, /* Override onError to this if not OE_Default */
135568 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
135569 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
135570 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
135571 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
135572){
135573 Vdbe *v; /* VDBE under construction */
135574 Index *pIdx; /* Pointer to one of the indices */
135575 Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */
135576 sqlite3 *db; /* Database connection */
135577 int i; /* loop counter */
135578 int ix; /* Index loop counter */
135579 int nCol; /* Number of columns */
135580 int onError; /* Conflict resolution strategy */
135581 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
135582 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
135583 Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */
135584 u8 isUpdate; /* True if this is an UPDATE operation */
135585 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
135586 int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */
135587 int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */
135588 int ipkTop = 0; /* Top of the IPK uniqueness check */
135589 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
135590 /* Variables associated with retesting uniqueness constraints after
135591 ** replace triggers fire have run */
135592 int regTrigCnt; /* Register used to count replace trigger invocations */
135593 int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */
135594 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
135595 Trigger *pTrigger; /* List of DELETE triggers on the table pTab */
135596 int nReplaceTrig = 0; /* Number of replace triggers coded */
135597 IndexIterator sIdxIter; /* Index iterator */
135598
135599 isUpdate = regOldData!=0;
135600 db = pParse->db;
135601 v = pParse->pVdbe;
135602 assert( v!=0 )((void) (0));
135603 assert( !IsView(pTab) )((void) (0)); /* This table is not a VIEW */
135604 nCol = pTab->nCol;
135605
135606 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
135607 ** normal rowid tables. nPkField is the number of key fields in the
135608 ** pPk index or 1 for a rowid table. In other words, nPkField is the
135609 ** number of fields in the true primary key of the table. */
135610 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
135611 pPk = 0;
135612 nPkField = 1;
135613 }else{
135614 pPk = sqlite3PrimaryKeyIndex(pTab);
135615 nPkField = pPk->nKeyCol;
135616 }
135617
135618 /* Record that this module has started */
135619 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
135620 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
135621
135622 /* Test all NOT NULL constraints.
135623 */
135624 if( pTab->tabFlags & TF_HasNotNull0x00000800 ){
135625 int b2ndPass = 0; /* True if currently running 2nd pass */
135626 int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */
135627 int nGenerated = 0; /* Number of generated columns with NOT NULL */
135628 while(1){ /* Make 2 passes over columns. Exit loop via "break" */
135629 for(i=0; i<nCol; i++){
135630 int iReg; /* Register holding column value */
135631 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
135632 int isGenerated; /* non-zero if column is generated */
135633 onError = pCol->notNull;
135634 if( onError==OE_None0 ) continue; /* No NOT NULL on this column */
135635 if( i==pTab->iPKey ){
135636 continue; /* ROWID is never NULL */
135637 }
135638 isGenerated = pCol->colFlags & COLFLAG_GENERATED0x0060;
135639 if( isGenerated && !b2ndPass ){
135640 nGenerated++;
135641 continue; /* Generated columns processed on 2nd pass */
135642 }
135643 if( aiChng && aiChng[i]<0 && !isGenerated ){
135644 /* Do not check NOT NULL on columns that do not change */
135645 continue;
135646 }
135647 if( overrideError!=OE_Default11 ){
135648 onError = overrideError;
135649 }else if( onError==OE_Default11 ){
135650 onError = OE_Abort2;
135651 }
135652 if( onError==OE_Replace5 ){
135653 if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */
135654 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
135655 ){
135656 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
135657 testcase( pCol->colFlags & COLFLAG_STORED );
135658 testcase( pCol->colFlags & COLFLAG_GENERATED );
135659 onError = OE_Abort2;
135660 }else{
135661 assert( !isGenerated )((void) (0));
135662 }
135663 }else if( b2ndPass && !isGenerated ){
135664 continue;
135665 }
135666 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
135667 || onError==OE_Ignore || onError==OE_Replace )((void) (0));
135668 testcase( i!=sqlite3TableColumnToStorage(pTab, i) );
135669 iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1;
135670 switch( onError ){
135671 case OE_Replace5: {
135672 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull52, iReg);
135673 VdbeCoverage(v);
135674 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 )((void) (0));
135675 nSeenReplace++;
135676 sqlite3ExprCodeCopy(pParse,
135677 sqlite3ColumnExpr(pTab, pCol), iReg);
135678 sqlite3VdbeJumpHere(v, addr1);
135679 break;
135680 }
135681 case OE_Abort2:
135682 sqlite3MayAbort(pParse);
135683 /* no break */ deliberate_fall_through__attribute__((fallthrough));
135684 case OE_Rollback1:
135685 case OE_Fail3: {
135686 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
135687 pCol->zCnName);
135688 testcase( zMsg==0 && db->mallocFailed==0 );
135689 sqlite3VdbeAddOp3(v, OP_HaltIfNull69, SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)),
135690 onError, iReg);
135691 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC(-6));
135692 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull1);
135693 VdbeCoverage(v);
135694 break;
135695 }
135696 default: {
135697 assert( onError==OE_Ignore )((void) (0));
135698 sqlite3VdbeAddOp2(v, OP_IsNull51, iReg, ignoreDest);
135699 VdbeCoverage(v);
135700 break;
135701 }
135702 } /* end switch(onError) */
135703 } /* end loop i over columns */
135704 if( nGenerated==0 && nSeenReplace==0 ){
135705 /* If there are no generated columns with NOT NULL constraints
135706 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
135707 ** pass is sufficient */
135708 break;
135709 }
135710 if( b2ndPass ) break; /* Never need more than 2 passes */
135711 b2ndPass = 1;
135712#ifndef SQLITE_OMIT_GENERATED_COLUMNS
135713 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated0x00000060)!=0 ){
135714 /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
135715 ** first pass, recomputed values for all generated columns, as
135716 ** those values might depend on columns affected by the REPLACE.
135717 */
135718 sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab);
135719 }
135720#endif
135721 } /* end of 2-pass loop */
135722 } /* end if( has-not-null-constraints ) */
135723
135724 /* Test all CHECK constraints
135725 */
135726#ifndef SQLITE_OMIT_CHECK
135727 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
135728 ExprList *pCheck = pTab->pCheck;
135729 pParse->iSelfTab = -(regNewData+1);
135730 onError = overrideError!=OE_Default11 ? overrideError : OE_Abort2;
135731 for(i=0; i<pCheck->nExpr; i++){
135732 int allOk;
135733 Expr *pCopy;
135734 Expr *pExpr = pCheck->a[i].pExpr;
135735 if( aiChng
135736 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
135737 ){
135738 /* The check constraints do not reference any of the columns being
135739 ** updated so there is no point it verifying the check constraint */
135740 continue;
135741 }
135742 if( bAffinityDone==0 ){
135743 sqlite3TableAffinity(v, pTab, regNewData+1);
135744 bAffinityDone = 1;
135745 }
135746 allOk = sqlite3VdbeMakeLabel(pParse);
135747 sqlite3VdbeVerifyAbortable(v, onError);
135748 pCopy = sqlite3ExprDup(db, pExpr, 0);
135749 if( !db->mallocFailed ){
135750 sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL0x10);
135751 }
135752 sqlite3ExprDelete(db, pCopy);
135753 if( onError==OE_Ignore4 ){
135754 sqlite3VdbeGoto(v, ignoreDest);
135755 }else{
135756 char *zName = pCheck->a[i].zEName;
135757 assert( zName!=0 || pParse->db->mallocFailed )((void) (0));
135758 if( onError==OE_Replace5 ) onError = OE_Abort2; /* IMP: R-26383-51744 */
135759 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK(19 | (1<<8)),
135760 onError, zName, P4_TRANSIENT0,
135761 P5_ConstraintCheck3);
135762 }
135763 sqlite3VdbeResolveLabel(v, allOk);
135764 }
135765 pParse->iSelfTab = 0;
135766 }
135767#endif /* !defined(SQLITE_OMIT_CHECK) */
135768
135769 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
135770 ** order:
135771 **
135772 ** (1) OE_Update
135773 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
135774 ** (3) OE_Replace
135775 **
135776 ** OE_Fail and OE_Ignore must happen before any changes are made.
135777 ** OE_Update guarantees that only a single row will change, so it
135778 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
135779 ** could happen in any order, but they are grouped up front for
135780 ** convenience.
135781 **
135782 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
135783 ** The order of constraints used to have OE_Update as (2) and OE_Abort
135784 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
135785 ** constraint before any others, so it had to be moved.
135786 **
135787 ** Constraint checking code is generated in this order:
135788 ** (A) The rowid constraint
135789 ** (B) Unique index constraints that do not have OE_Replace as their
135790 ** default conflict resolution strategy
135791 ** (C) Unique index that do use OE_Replace by default.
135792 **
135793 ** The ordering of (2) and (3) is accomplished by making sure the linked
135794 ** list of indexes attached to a table puts all OE_Replace indexes last
135795 ** in the list. See sqlite3CreateIndex() for where that happens.
135796 */
135797 sIdxIter.eType = 0;
135798 sIdxIter.i = 0;
135799 sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */
135800 sIdxIter.u.lx.pIdx = pTab->pIndex;
135801 if( pUpsert ){
135802 if( pUpsert->pUpsertTarget==0 ){
135803 /* There is just on ON CONFLICT clause and it has no constraint-target */
135804 assert( pUpsert->pNextUpsert==0 )((void) (0));
135805 if( pUpsert->isDoUpdate==0 ){
135806 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
135807 ** Make all unique constraint resolution be OE_Ignore */
135808 overrideError = OE_Ignore4;
135809 pUpsert = 0;
135810 }else{
135811 /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */
135812 overrideError = OE_Update6;
135813 }
135814 }else if( pTab->pIndex!=0 ){
135815 /* Otherwise, we'll need to run the IndexListTerm array version of the
135816 ** iterator to ensure that all of the ON CONFLICT conditions are
135817 ** checked first and in order. */
135818 int nIdx, jj;
135819 u64 nByte;
135820 Upsert *pTerm;
135821 u8 *bUsed;
135822 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
135823 assert( aRegIdx[nIdx]>0 )((void) (0));
135824 }
135825 sIdxIter.eType = 1;
135826 sIdxIter.u.ax.nIdx = nIdx;
135827 nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx;
135828 sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte);
135829 if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */
135830 bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx];
135831 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
135832 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
135833 if( pTerm->pUpsertTarget==0 ) break;
135834 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
135835 jj = 0;
135836 pIdx = pTab->pIndex;
135837 while( ALWAYS(pIdx!=0)(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
135838 pIdx = pIdx->pNext;
135839 jj++;
135840 }
135841 if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */
135842 bUsed[jj] = 1;
135843 sIdxIter.u.ax.aIdx[i].p = pIdx;
135844 sIdxIter.u.ax.aIdx[i].ix = jj;
135845 i++;
135846 }
135847 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
135848 if( bUsed[jj] ) continue;
135849 sIdxIter.u.ax.aIdx[i].p = pIdx;
135850 sIdxIter.u.ax.aIdx[i].ix = jj;
135851 i++;
135852 }
135853 assert( i==nIdx )((void) (0));
135854 }
135855 }
135856
135857 /* Determine if it is possible that triggers (either explicitly coded
135858 ** triggers or FK resolution actions) might run as a result of deletes
135859 ** that happen when OE_Replace conflict resolution occurs. (Call these
135860 ** "replace triggers".) If any replace triggers run, we will need to
135861 ** recheck all of the uniqueness constraints after they have all run.
135862 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
135863 **
135864 ** If replace triggers are a possibility, then
135865 **
135866 ** (1) Allocate register regTrigCnt and initialize it to zero.
135867 ** That register will count the number of replace triggers that
135868 ** fire. Constraint recheck only occurs if the number is positive.
135869 ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
135870 ** (3) Initialize addrRecheck and lblRecheckOk
135871 **
135872 ** The uniqueness rechecking code will create a series of tests to run
135873 ** in a second pass. The addrRecheck and lblRecheckOk variables are
135874 ** used to link together these tests which are separated from each other
135875 ** in the generate bytecode.
135876 */
135877 if( (db->flags & (SQLITE_RecTriggers0x00002000|SQLITE_ForeignKeys0x00004000))==0 ){
135878 /* There are not DELETE triggers nor FK constraints. No constraint
135879 ** rechecks are needed. */
135880 pTrigger = 0;
135881 regTrigCnt = 0;
135882 }else{
135883 if( db->flags&SQLITE_RecTriggers0x00002000 ){
135884 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE129, 0, 0);
135885 regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0);
135886 }else{
135887 pTrigger = 0;
135888 regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0);
135889 }
135890 if( regTrigCnt ){
135891 /* Replace triggers might exist. Allocate the counter and
135892 ** initialize it to zero. */
135893 regTrigCnt = ++pParse->nMem;
135894 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regTrigCnt);
135895 VdbeComment((v, "trigger count"));
135896 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
135897 addrRecheck = lblRecheckOk;
135898 }
135899 }
135900
135901 /* If rowid is changing, make sure the new rowid does not previously
135902 ** exist in the table.
135903 */
135904 if( pkChng && pPk==0 ){
135905 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
135906
135907 /* Figure out what action to take in case of a rowid collision */
135908 onError = pTab->keyConf;
135909 if( overrideError!=OE_Default11 ){
135910 onError = overrideError;
135911 }else if( onError==OE_Default11 ){
135912 onError = OE_Abort2;
135913 }
135914
135915 /* figure out whether or not upsert applies in this case */
135916 if( pUpsert ){
135917 pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0);
135918 if( pUpsertClause!=0 ){
135919 if( pUpsertClause->isDoUpdate==0 ){
135920 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
135921 }else{
135922 onError = OE_Update6; /* DO UPDATE */
135923 }
135924 }
135925 if( pUpsertClause!=pUpsert ){
135926 /* The first ON CONFLICT clause has a conflict target other than
135927 ** the IPK. We have to jump ahead to that first ON CONFLICT clause
135928 ** and then come back here and deal with the IPK afterwards */
135929 upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto9);
135930 }
135931 }
135932
135933 /* If the response to a rowid conflict is REPLACE but the response
135934 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
135935 ** to defer the running of the rowid conflict checking until after
135936 ** the UNIQUE constraints have run.
135937 */
135938 if( onError==OE_Replace5 /* IPK rule is REPLACE */
135939 && onError!=overrideError /* Rules for other constraints are different */
135940 && pTab->pIndex /* There exist other constraints */
135941 && !upsertIpkDelay /* IPK check already deferred by UPSERT */
135942 ){
135943 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto9)+1;
135944 VdbeComment((v, "defer IPK REPLACE until last"));
135945 }
135946
135947 if( isUpdate ){
135948 /* pkChng!=0 does not mean that the rowid has changed, only that
135949 ** it might have changed. Skip the conflict logic below if the rowid
135950 ** is unchanged. */
135951 sqlite3VdbeAddOp3(v, OP_Eq54, regNewData, addrRowidOk, regOldData);
135952 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
135953 VdbeCoverage(v);
135954 }
135955
135956 /* Check to see if the new rowid already exists in the table. Skip
135957 ** the following conflict logic if it does not. */
135958 VdbeNoopComment((v, "uniqueness check for ROWID"));
135959 sqlite3VdbeVerifyAbortable(v, onError);
135960 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, addrRowidOk, regNewData);
135961 VdbeCoverage(v);
135962
135963 switch( onError ){
135964 default: {
135965 onError = OE_Abort2;
135966 /* no break */ deliberate_fall_through__attribute__((fallthrough));
135967 }
135968 case OE_Rollback1:
135969 case OE_Abort2:
135970 case OE_Fail3: {
135971 testcase( onError==OE_Rollback );
135972 testcase( onError==OE_Abort );
135973 testcase( onError==OE_Fail );
135974 sqlite3RowidConstraint(pParse, onError, pTab);
135975 break;
135976 }
135977 case OE_Replace5: {
135978 /* If there are DELETE triggers on this table and the
135979 ** recursive-triggers flag is set, call GenerateRowDelete() to
135980 ** remove the conflicting row from the table. This will fire
135981 ** the triggers and remove both the table and index b-tree entries.
135982 **
135983 ** Otherwise, if there are no triggers or the recursive-triggers
135984 ** flag is not set, but the table has one or more indexes, call
135985 ** GenerateRowIndexDelete(). This removes the index b-tree entries
135986 ** only. The table b-tree entry will be replaced by the new entry
135987 ** when it is inserted.
135988 **
135989 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
135990 ** also invoke MultiWrite() to indicate that this VDBE may require
135991 ** statement rollback (if the statement is aborted after the delete
135992 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
135993 ** but being more selective here allows statements like:
135994 **
135995 ** REPLACE INTO t(rowid) VALUES($newrowid)
135996 **
135997 ** to run without a statement journal if there are no indexes on the
135998 ** table.
135999 */
136000 if( regTrigCnt ){
136001 sqlite3MultiWrite(pParse);
136002 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
136003 regNewData, 1, 0, OE_Replace5, 1, -1);
136004 sqlite3VdbeAddOp2(v, OP_AddImm86, regTrigCnt, 1); /* incr trigger cnt */
136005 nReplaceTrig++;
136006 }else{
136007#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
136008 assert( HasRowid(pTab) )((void) (0));
136009 /* This OP_Delete opcode fires the pre-update-hook only. It does
136010 ** not modify the b-tree. It is more efficient to let the coming
136011 ** OP_Insert replace the existing entry than it is to delete the
136012 ** existing entry and then insert a new one. */
136013 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, OPFLAG_ISNOOP0x40);
136014 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
136015#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
136016 if( pTab->pIndex ){
136017 sqlite3MultiWrite(pParse);
136018 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
136019 }
136020 }
136021 seenReplace = 1;
136022 break;
136023 }
136024#ifndef SQLITE_OMIT_UPSERT
136025 case OE_Update6: {
136026 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
136027 /* no break */ deliberate_fall_through__attribute__((fallthrough));
136028 }
136029#endif
136030 case OE_Ignore4: {
136031 testcase( onError==OE_Ignore );
136032 sqlite3VdbeGoto(v, ignoreDest);
136033 break;
136034 }
136035 }
136036 sqlite3VdbeResolveLabel(v, addrRowidOk);
136037 if( pUpsert && pUpsertClause!=pUpsert ){
136038 upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto9);
136039 }else if( ipkTop ){
136040 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto9);
136041 sqlite3VdbeJumpHere(v, ipkTop-1);
136042 }
136043 }
136044
136045 /* Test all UNIQUE constraints by creating entries for each UNIQUE
136046 ** index and making sure that duplicate entries do not already exist.
136047 ** Compute the revised record entries for indices as we go.
136048 **
136049 ** This loop also handles the case of the PRIMARY KEY index for a
136050 ** WITHOUT ROWID table.
136051 */
136052 for(pIdx = indexIteratorFirst(&sIdxIter, &ix);
136053 pIdx;
136054 pIdx = indexIteratorNext(&sIdxIter, &ix)
136055 ){
136056 int regIdx; /* Range of registers holding content for pIdx */
136057 int regR; /* Range of registers holding conflicting PK */
136058 int iThisCur; /* Cursor for this UNIQUE index */
136059 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
136060 int addrConflictCk; /* First opcode in the conflict check logic */
136061
136062 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
136063 if( pUpsert ){
136064 pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx);
136065 if( upsertIpkDelay && pUpsertClause==pUpsert ){
136066 sqlite3VdbeJumpHere(v, upsertIpkDelay);
136067 }
136068 }
136069 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
136070 if( bAffinityDone==0 ){
136071 sqlite3TableAffinity(v, pTab, regNewData+1);
136072 bAffinityDone = 1;
136073 }
136074 VdbeNoopComment((v, "prep index %s", pIdx->zName));
136075 iThisCur = iIdxCur+ix;
136076
136077
136078 /* Skip partial indices for which the WHERE clause is not true */
136079 if( pIdx->pPartIdxWhere ){
136080 sqlite3VdbeAddOp2(v, OP_Null75, 0, aRegIdx[ix]);
136081 pParse->iSelfTab = -(regNewData+1);
136082 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
136083 SQLITE_JUMPIFNULL0x10);
136084 pParse->iSelfTab = 0;
136085 }
136086
136087 /* Create a record for this index entry as it should appear after
136088 ** the insert or update. Store that record in the aRegIdx[ix] register
136089 */
136090 regIdx = aRegIdx[ix]+1;
136091 for(i=0; i<pIdx->nColumn; i++){
136092 int iField = pIdx->aiColumn[i];
136093 int x;
136094 if( iField==XN_EXPR(-2) ){
136095 pParse->iSelfTab = -(regNewData+1);
136096 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
136097 pParse->iSelfTab = 0;
136098 VdbeComment((v, "%s column %d", pIdx->zName, i));
136099 }else if( iField==XN_ROWID(-1) || iField==pTab->iPKey ){
136100 x = regNewData;
136101 sqlite3VdbeAddOp2(v, OP_IntCopy82, x, regIdx+i);
136102 VdbeComment((v, "rowid"));
136103 }else{
136104 testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField );
136105 x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1;
136106 sqlite3VdbeAddOp2(v, OP_SCopy81, x, regIdx+i);
136107 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
136108 }
136109 }
136110 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regIdx, pIdx->nColumn, aRegIdx[ix]);
136111 VdbeComment((v, "for %s", pIdx->zName));
136112#ifdef SQLITE_ENABLE_NULL_TRIM
136113 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
136114 sqlite3SetMakeRecordP5(v, pIdx->pTable);
136115 }
136116#endif
136117 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
136118
136119 /* In an UPDATE operation, if this index is the PRIMARY KEY index
136120 ** of a WITHOUT ROWID table and there has been no change the
136121 ** primary key, then no collision is possible. The collision detection
136122 ** logic below can all be skipped. */
136123 if( isUpdate && pPk==pIdx && pkChng==0 ){
136124 sqlite3VdbeResolveLabel(v, addrUniqueOk);
136125 continue;
136126 }
136127
136128 /* Find out what action to take in case there is a uniqueness conflict */
136129 onError = pIdx->onError;
136130 if( onError==OE_None0 ){
136131 sqlite3VdbeResolveLabel(v, addrUniqueOk);
136132 continue; /* pIdx is not a UNIQUE index */
136133 }
136134 if( overrideError!=OE_Default11 ){
136135 onError = overrideError;
136136 }else if( onError==OE_Default11 ){
136137 onError = OE_Abort2;
136138 }
136139
136140 /* Figure out if the upsert clause applies to this index */
136141 if( pUpsertClause ){
136142 if( pUpsertClause->isDoUpdate==0 ){
136143 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
136144 }else{
136145 onError = OE_Update6; /* DO UPDATE */
136146 }
136147 }
136148
136149 /* Collision detection may be omitted if all of the following are true:
136150 ** (1) The conflict resolution algorithm is REPLACE
136151 ** (2) The table is a WITHOUT ROWID table
136152 ** (3) There are no secondary indexes on the table
136153 ** (4) No delete triggers need to be fired if there is a conflict
136154 ** (5) No FK constraint counters need to be updated if a conflict occurs.
136155 **
136156 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
136157 ** must be explicitly deleted in order to ensure any pre-update hook
136158 ** is invoked. */
136159 assert( IsOrdinaryTable(pTab) )((void) (0));
136160#ifndef SQLITE_ENABLE_PREUPDATE_HOOK1
136161 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
136162 && pPk==pIdx /* Condition 2 */
136163 && onError==OE_Replace5 /* Condition 1 */
136164 && ( 0==(db->flags&SQLITE_RecTriggers0x00002000) || /* Condition 4 */
136165 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE129, 0, 0))
136166 && ( 0==(db->flags&SQLITE_ForeignKeys0x00004000) || /* Condition 5 */
136167 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
136168 ){
136169 sqlite3VdbeResolveLabel(v, addrUniqueOk);
136170 continue;
136171 }
136172#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
136173
136174 /* Check to see if the new index entry will be unique */
136175 sqlite3VdbeVerifyAbortable(v, onError);
136176 addrConflictCk =
136177 sqlite3VdbeAddOp4Int(v, OP_NoConflict27, iThisCur, addrUniqueOk,
136178 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
136179
136180 /* Generate code to handle collisions */
136181 regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField);
136182 if( isUpdate || onError==OE_Replace5 ){
136183 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
136184 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iThisCur, regR);
136185 /* Conflict only if the rowid of the existing index entry
136186 ** is different from old-rowid */
136187 if( isUpdate ){
136188 sqlite3VdbeAddOp3(v, OP_Eq54, regR, addrUniqueOk, regOldData);
136189 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
136190 VdbeCoverage(v);
136191 }
136192 }else{
136193 int x;
136194 /* Extract the PRIMARY KEY from the end of the index entry and
136195 ** store it in registers regR..regR+nPk-1 */
136196 if( pIdx!=pPk ){
136197 for(i=0; i<pPk->nKeyCol; i++){
136198 assert( pPk->aiColumn[i]>=0 )((void) (0));
136199 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
136200 sqlite3VdbeAddOp3(v, OP_Column94, iThisCur, x, regR+i);
136201 VdbeComment((v, "%s.%s", pTab->zName,
136202 pTab->aCol[pPk->aiColumn[i]].zCnName));
136203 }
136204 }
136205 if( isUpdate ){
136206 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
136207 ** table, only conflict if the new PRIMARY KEY values are actually
136208 ** different from the old. See TH3 withoutrowid04.test.
136209 **
136210 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
136211 ** of the matched index row are different from the original PRIMARY
136212 ** KEY values of this row before the update. */
136213 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
136214 int op = OP_Ne53;
136215 int regCmp = (IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? regIdx : regR);
136216
136217 for(i=0; i<pPk->nKeyCol; i++){
136218 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
136219 x = pPk->aiColumn[i];
136220 assert( x>=0 )((void) (0));
136221 if( i==(pPk->nKeyCol-1) ){
136222 addrJump = addrUniqueOk;
136223 op = OP_Eq54;
136224 }
136225 x = sqlite3TableColumnToStorage(pTab, x);
136226 sqlite3VdbeAddOp4(v, op,
136227 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ(-2)
136228 );
136229 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
136230 VdbeCoverageIf(v, op==OP_Eq);
136231 VdbeCoverageIf(v, op==OP_Ne);
136232 }
136233 }
136234 }
136235 }
136236
136237 /* Generate code that executes if the new index entry is not unique */
136238 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
136239 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update )((void) (0));
136240 switch( onError ){
136241 case OE_Rollback1:
136242 case OE_Abort2:
136243 case OE_Fail3: {
136244 testcase( onError==OE_Rollback );
136245 testcase( onError==OE_Abort );
136246 testcase( onError==OE_Fail );
136247 sqlite3UniqueConstraint(pParse, onError, pIdx);
136248 break;
136249 }
136250#ifndef SQLITE_OMIT_UPSERT
136251 case OE_Update6: {
136252 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
136253 /* no break */ deliberate_fall_through__attribute__((fallthrough));
136254 }
136255#endif
136256 case OE_Ignore4: {
136257 testcase( onError==OE_Ignore );
136258 sqlite3VdbeGoto(v, ignoreDest);
136259 break;
136260 }
136261 default: {
136262 int nConflictCk; /* Number of opcodes in conflict check logic */
136263
136264 assert( onError==OE_Replace )((void) (0));
136265 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
136266 assert( nConflictCk>0 || db->mallocFailed )((void) (0));
136267 testcase( nConflictCk<=0 );
136268 testcase( nConflictCk>1 );
136269 if( regTrigCnt ){
136270 sqlite3MultiWrite(pParse);
136271 nReplaceTrig++;
136272 }
136273 if( pTrigger && isUpdate ){
136274 sqlite3VdbeAddOp1(v, OP_CursorLock167, iDataCur);
136275 }
136276 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
136277 regR, nPkField, 0, OE_Replace5,
136278 (pIdx==pPk ? ONEPASS_SINGLE1 : ONEPASS_OFF0), iThisCur);
136279 if( pTrigger && isUpdate ){
136280 sqlite3VdbeAddOp1(v, OP_CursorUnlock168, iDataCur);
136281 }
136282 if( regTrigCnt ){
136283 int addrBypass; /* Jump destination to bypass recheck logic */
136284
136285 sqlite3VdbeAddOp2(v, OP_AddImm86, regTrigCnt, 1); /* incr trigger cnt */
136286 addrBypass = sqlite3VdbeAddOp0(v, OP_Goto9); /* Bypass recheck */
136287 VdbeComment((v, "bypass recheck"));
136288
136289 /* Here we insert code that will be invoked after all constraint
136290 ** checks have run, if and only if one or more replace triggers
136291 ** fired. */
136292 sqlite3VdbeResolveLabel(v, lblRecheckOk);
136293 lblRecheckOk = sqlite3VdbeMakeLabel(pParse);
136294 if( pIdx->pPartIdxWhere ){
136295 /* Bypass the recheck if this partial index is not defined
136296 ** for the current row */
136297 sqlite3VdbeAddOp2(v, OP_IsNull51, regIdx-1, lblRecheckOk);
136298 VdbeCoverage(v);
136299 }
136300 /* Copy the constraint check code from above, except change
136301 ** the constraint-ok jump destination to be the address of
136302 ** the next retest block */
136303 while( nConflictCk>0 ){
136304 VdbeOp x; /* Conflict check opcode to copy */
136305 /* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
136306 ** Hence, make a complete copy of the opcode, rather than using
136307 ** a pointer to the opcode. */
136308 x = *sqlite3VdbeGetOp(v, addrConflictCk);
136309 if( x.opcode!=OP_IdxRowid142 ){
136310 int p2; /* New P2 value for copied conflict check opcode */
136311 const char *zP4;
136312 if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP0x01 ){
136313 p2 = lblRecheckOk;
136314 }else{
136315 p2 = x.p2;
136316 }
136317 zP4 = x.p4type==P4_INT32(-3) ? SQLITE_INT_TO_PTR(x.p4.i)((void*)(long int)(x.p4.i)) : x.p4.z;
136318 sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type);
136319 sqlite3VdbeChangeP5(v, x.p5);
136320 VdbeCoverageIf(v, p2!=x.p2);
136321 }
136322 nConflictCk--;
136323 addrConflictCk++;
136324 }
136325 /* If the retest fails, issue an abort */
136326 sqlite3UniqueConstraint(pParse, OE_Abort2, pIdx);
136327
136328 sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */
136329 }
136330 seenReplace = 1;
136331 break;
136332 }
136333 }
136334 sqlite3VdbeResolveLabel(v, addrUniqueOk);
136335 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
136336 if( pUpsertClause
136337 && upsertIpkReturn
136338 && sqlite3UpsertNextIsIPK(pUpsertClause)
136339 ){
136340 sqlite3VdbeGoto(v, upsertIpkDelay+1);
136341 sqlite3VdbeJumpHere(v, upsertIpkReturn);
136342 upsertIpkReturn = 0;
136343 }
136344 }
136345
136346 /* If the IPK constraint is a REPLACE, run it last */
136347 if( ipkTop ){
136348 sqlite3VdbeGoto(v, ipkTop);
136349 VdbeComment((v, "Do IPK REPLACE"));
136350 assert( ipkBottom>0 )((void) (0));
136351 sqlite3VdbeJumpHere(v, ipkBottom);
136352 }
136353
136354 /* Recheck all uniqueness constraints after replace triggers have run */
136355 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
136356 assert( regTrigCnt!=0 || nReplaceTrig==0 )((void) (0));
136357 if( nReplaceTrig ){
136358 sqlite3VdbeAddOp2(v, OP_IfNot17, regTrigCnt, lblRecheckOk);VdbeCoverage(v);
136359 if( !pPk ){
136360 if( isUpdate ){
136361 sqlite3VdbeAddOp3(v, OP_Eq54, regNewData, addrRecheck, regOldData);
136362 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
136363 VdbeCoverage(v);
136364 }
136365 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, addrRecheck, regNewData);
136366 VdbeCoverage(v);
136367 sqlite3RowidConstraint(pParse, OE_Abort2, pTab);
136368 }else{
136369 sqlite3VdbeGoto(v, addrRecheck);
136370 }
136371 sqlite3VdbeResolveLabel(v, lblRecheckOk);
136372 }
136373
136374 /* Generate the table record */
136375 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
136376 int regRec = aRegIdx[ix];
136377 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regNewData+1, pTab->nNVCol, regRec);
136378 sqlite3SetMakeRecordP5(v, pTab);
136379 if( !bAffinityDone ){
136380 sqlite3TableAffinity(v, pTab, 0);
136381 }
136382 }
136383
136384 *pbMayReplace = seenReplace;
136385 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
136386}
136387
136388#ifdef SQLITE_ENABLE_NULL_TRIM
136389/*
136390** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
136391** to be the number of columns in table pTab that must not be NULL-trimmed.
136392**
136393** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
136394*/
136395SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
136396 u16 i;
136397
136398 /* Records with omitted columns are only allowed for schema format
136399 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
136400 if( pTab->pSchema->file_format<2 ) return;
136401
136402 for(i=pTab->nCol-1; i>0; i--){
136403 if( pTab->aCol[i].iDflt!=0 ) break;
136404 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001 ) break;
136405 }
136406 sqlite3VdbeChangeP5(v, i+1);
136407}
136408#endif
136409
136410/*
136411** Table pTab is a WITHOUT ROWID table that is being written to. The cursor
136412** number is iCur, and register regData contains the new record for the
136413** PK index. This function adds code to invoke the pre-update hook,
136414** if one is registered.
136415*/
136416#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
136417static void codeWithoutRowidPreupdate(
136418 Parse *pParse, /* Parse context */
136419 Table *pTab, /* Table being updated */
136420 int iCur, /* Cursor number for table */
136421 int regData /* Data containing new record */
136422){
136423 Vdbe *v = pParse->pVdbe;
136424 int r = sqlite3GetTempReg(pParse);
136425 assert( !HasRowid(pTab) )((void) (0));
136426 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB )((void) (0));
136427 sqlite3VdbeAddOp2(v, OP_Integer71, 0, r);
136428 sqlite3VdbeAddOp4(v, OP_Insert128, iCur, regData, r, (char*)pTab, P4_TABLE(-5));
136429 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP0x40);
136430 sqlite3ReleaseTempReg(pParse, r);
136431}
136432#else
136433# define codeWithoutRowidPreupdate(a,b,c,d)
136434#endif
136435
136436/*
136437** This routine generates code to finish the INSERT or UPDATE operation
136438** that was started by a prior call to sqlite3GenerateConstraintChecks.
136439** A consecutive range of registers starting at regNewData contains the
136440** rowid and the content to be inserted.
136441**
136442** The arguments to this routine should be the same as the first six
136443** arguments to sqlite3GenerateConstraintChecks.
136444*/
136445SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(
136446 Parse *pParse, /* The parser context */
136447 Table *pTab, /* the table into which we are inserting */
136448 int iDataCur, /* Cursor of the canonical data source */
136449 int iIdxCur, /* First index cursor */
136450 int regNewData, /* Range of content */
136451 int *aRegIdx, /* Register used by each index. 0 for unused indices */
136452 int update_flags, /* True for UPDATE, False for INSERT */
136453 int appendBias, /* True if this is likely to be an append */
136454 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
136455){
136456 Vdbe *v; /* Prepared statements under construction */
136457 Index *pIdx; /* An index being inserted or updated */
136458 u8 pik_flags; /* flag values passed to the btree insert */
136459 int i; /* Loop counter */
136460
136461 assert( update_flags==0((void) (0))
136462 || update_flags==OPFLAG_ISUPDATE((void) (0))
136463 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)((void) (0))
136464 )((void) (0));
136465
136466 v = pParse->pVdbe;
136467 assert( v!=0 )((void) (0));
136468 assert( !IsView(pTab) )((void) (0)); /* This table is not a VIEW */
136469 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
136470 /* All REPLACE indexes are at the end of the list */
136471 assert( pIdx->onError!=OE_Replace((void) (0))
136472 || pIdx->pNext==0((void) (0))
136473 || pIdx->pNext->onError==OE_Replace )((void) (0));
136474 if( aRegIdx[i]==0 ) continue;
136475 if( pIdx->pPartIdxWhere ){
136476 sqlite3VdbeAddOp2(v, OP_IsNull51, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
136477 VdbeCoverage(v);
136478 }
136479 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT0x10 : 0);
136480 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
136481 pik_flags |= OPFLAG_NCHANGE0x01;
136482 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION0x02);
136483 if( update_flags==0 ){
136484 codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]);
136485 }
136486 }
136487 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iIdxCur+i, aRegIdx[i],
136488 aRegIdx[i]+1,
136489 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
136490 sqlite3VdbeChangeP5(v, pik_flags);
136491 }
136492 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) return;
136493 if( pParse->nested ){
136494 pik_flags = 0;
136495 }else{
136496 pik_flags = OPFLAG_NCHANGE0x01;
136497 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID0x20);
136498 }
136499 if( appendBias ){
136500 pik_flags |= OPFLAG_APPEND0x08;
136501 }
136502 if( useSeekResult ){
136503 pik_flags |= OPFLAG_USESEEKRESULT0x10;
136504 }
136505 sqlite3VdbeAddOp3(v, OP_Insert128, iDataCur, aRegIdx[i], regNewData);
136506 if( !pParse->nested ){
136507 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
136508 }
136509 sqlite3VdbeChangeP5(v, pik_flags);
136510}
136511
136512/*
136513** Allocate cursors for the pTab table and all its indices and generate
136514** code to open and initialized those cursors.
136515**
136516** The cursor for the object that contains the complete data (normally
136517** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
136518** ROWID table) is returned in *piDataCur. The first index cursor is
136519** returned in *piIdxCur. The number of indices is returned.
136520**
136521** Use iBase as the first cursor (either the *piDataCur for rowid tables
136522** or the first index for WITHOUT ROWID tables) if it is non-negative.
136523** If iBase is negative, then allocate the next available cursor.
136524**
136525** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
136526** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
136527** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
136528** pTab->pIndex list.
136529**
136530** If pTab is a virtual table, then this routine is a no-op and the
136531** *piDataCur and *piIdxCur values are left uninitialized.
136532*/
136533SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(
136534 Parse *pParse, /* Parsing context */
136535 Table *pTab, /* Table to be opened */
136536 int op, /* OP_OpenRead or OP_OpenWrite */
136537 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
136538 int iBase, /* Use this for the table cursor, if there is one */
136539 u8 *aToOpen, /* If not NULL: boolean for each table and index */
136540 int *piDataCur, /* Write the database source cursor number here */
136541 int *piIdxCur /* Write the first index cursor number here */
136542){
136543 int i;
136544 int iDb;
136545 int iDataCur;
136546 Index *pIdx;
136547 Vdbe *v;
136548
136549 assert( op==OP_OpenRead || op==OP_OpenWrite )((void) (0));
136550 assert( op==OP_OpenWrite || p5==0 )((void) (0));
136551 assert( piDataCur!=0 )((void) (0));
136552 assert( piIdxCur!=0 )((void) (0));
136553 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
136554 /* This routine is a no-op for virtual tables. Leave the output
136555 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
136556 ** for improved error detection. */
136557 *piDataCur = *piIdxCur = -999;
136558 return 0;
136559 }
136560 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
136561 v = pParse->pVdbe;
136562 assert( v!=0 )((void) (0));
136563 if( iBase<0 ) iBase = pParse->nTab;
136564 iDataCur = iBase++;
136565 *piDataCur = iDataCur;
136566 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && (aToOpen==0 || aToOpen[0]) ){
136567 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
136568 }else if( pParse->db->noSharedCache==0 ){
136569 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite113, pTab->zName);
136570 }
136571 *piIdxCur = iBase;
136572 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
136573 int iIdxCur = iBase++;
136574 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
136575 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
136576 *piDataCur = iIdxCur;
136577 p5 = 0;
136578 }
136579 if( aToOpen==0 || aToOpen[i+1] ){
136580 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
136581 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
136582 sqlite3VdbeChangeP5(v, p5);
136583 VdbeComment((v, "%s", pIdx->zName));
136584 }
136585 }
136586 if( iBase>pParse->nTab ) pParse->nTab = iBase;
136587 return i;
136588}
136589
136590
136591#ifdef SQLITE_TEST
136592/*
136593** The following global variable is incremented whenever the
136594** transfer optimization is used. This is used for testing
136595** purposes only - to make sure the transfer optimization really
136596** is happening when it is supposed to.
136597*/
136598SQLITE_API int sqlite3_xferopt_count;
136599#endif /* SQLITE_TEST */
136600
136601
136602#ifndef SQLITE_OMIT_XFER_OPT
136603/*
136604** Check to see if index pSrc is compatible as a source of data
136605** for index pDest in an insert transfer optimization. The rules
136606** for a compatible index:
136607**
136608** * The index is over the same set of columns
136609** * The same DESC and ASC markings occurs on all columns
136610** * The same onError processing (OE_Abort, OE_Ignore, etc)
136611** * The same collating sequence on each column
136612** * The index has the exact same WHERE clause
136613*/
136614static int xferCompatibleIndex(Index *pDest, Index *pSrc){
136615 int i;
136616 assert( pDest && pSrc )((void) (0));
136617 assert( pDest->pTable!=pSrc->pTable )((void) (0));
136618 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
136619 return 0; /* Different number of columns */
136620 }
136621 if( pDest->onError!=pSrc->onError ){
136622 return 0; /* Different conflict resolution strategies */
136623 }
136624 for(i=0; i<pSrc->nKeyCol; i++){
136625 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
136626 return 0; /* Different columns indexed */
136627 }
136628 if( pSrc->aiColumn[i]==XN_EXPR(-2) ){
136629 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 )((void) (0));
136630 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
136631 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
136632 return 0; /* Different expressions in the index */
136633 }
136634 }
136635 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
136636 return 0; /* Different sort orders */
136637 }
136638 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
136639 return 0; /* Different collating sequences */
136640 }
136641 }
136642 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
136643 return 0; /* Different WHERE clauses */
136644 }
136645
136646 /* If no test above fails then the indices must be compatible */
136647 return 1;
136648}
136649
136650/*
136651** Attempt the transfer optimization on INSERTs of the form
136652**
136653** INSERT INTO tab1 SELECT * FROM tab2;
136654**
136655** The xfer optimization transfers raw records from tab2 over to tab1.
136656** Columns are not decoded and reassembled, which greatly improves
136657** performance. Raw index records are transferred in the same way.
136658**
136659** The xfer optimization is only attempted if tab1 and tab2 are compatible.
136660** There are lots of rules for determining compatibility - see comments
136661** embedded in the code for details.
136662**
136663** This routine returns TRUE if the optimization is guaranteed to be used.
136664** Sometimes the xfer optimization will only work if the destination table
136665** is empty - a factor that can only be determined at run-time. In that
136666** case, this routine generates code for the xfer optimization but also
136667** does a test to see if the destination table is empty and jumps over the
136668** xfer optimization code if the test fails. In that case, this routine
136669** returns FALSE so that the caller will know to go ahead and generate
136670** an unoptimized transfer. This routine also returns FALSE if there
136671** is no chance that the xfer optimization can be applied.
136672**
136673** This optimization is particularly useful at making VACUUM run faster.
136674*/
136675static int xferOptimization(
136676 Parse *pParse, /* Parser context */
136677 Table *pDest, /* The table we are inserting into */
136678 Select *pSelect, /* A SELECT statement to use as the data source */
136679 int onError, /* How to handle constraint errors */
136680 int iDbDest /* The database of pDest */
136681){
136682 sqlite3 *db = pParse->db;
136683 ExprList *pEList; /* The result set of the SELECT */
136684 Table *pSrc; /* The table in the FROM clause of SELECT */
136685 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
136686 SrcItem *pItem; /* An element of pSelect->pSrc */
136687 int i; /* Loop counter */
136688 int iDbSrc; /* The database of pSrc */
136689 int iSrc, iDest; /* Cursors from source and destination */
136690 int addr1, addr2; /* Loop addresses */
136691 int emptyDestTest = 0; /* Address of test for empty pDest */
136692 int emptySrcTest = 0; /* Address of test for empty pSrc */
136693 Vdbe *v; /* The VDBE we are building */
136694 int regAutoinc; /* Memory register used by AUTOINC */
136695 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
136696 int regData, regRowid; /* Registers holding data and rowid */
136697
136698 assert( pSelect!=0 )((void) (0));
136699 if( pParse->pWith || pSelect->pWith ){
136700 /* Do not attempt to process this query if there are an WITH clauses
136701 ** attached to it. Proceeding may generate a false "no such table: xxx"
136702 ** error if pSelect reads from a CTE named "xxx". */
136703 return 0;
136704 }
136705#ifndef SQLITE_OMIT_VIRTUALTABLE
136706 if( IsVirtual(pDest)((pDest)->eTabType==1) ){
136707 return 0; /* tab1 must not be a virtual table */
136708 }
136709#endif
136710 if( onError==OE_Default11 ){
136711 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
136712 if( onError==OE_Default11 ) onError = OE_Abort2;
136713 }
136714 assert(pSelect->pSrc)((void) (0)); /* allocated even if there is no FROM clause */
136715 if( pSelect->pSrc->nSrc!=1 ){
136716 return 0; /* FROM clause must have exactly one term */
136717 }
136718 if( pSelect->pSrc->a[0].fg.isSubquery ){
136719 return 0; /* FROM clause cannot contain a subquery */
136720 }
136721 if( pSelect->pWhere ){
136722 return 0; /* SELECT may not have a WHERE clause */
136723 }
136724 if( pSelect->pOrderBy ){
136725 return 0; /* SELECT may not have an ORDER BY clause */
136726 }
136727 /* Do not need to test for a HAVING clause. If HAVING is present but
136728 ** there is no ORDER BY, we will get an error. */
136729 if( pSelect->pGroupBy ){
136730 return 0; /* SELECT may not have a GROUP BY clause */
136731 }
136732 if( pSelect->pLimit ){
136733 return 0; /* SELECT may not have a LIMIT clause */
136734 }
136735 if( pSelect->pPrior ){
136736 return 0; /* SELECT may not be a compound query */
136737 }
136738 if( pSelect->selFlags & SF_Distinct0x0000001 ){
136739 return 0; /* SELECT may not be DISTINCT */
136740 }
136741 pEList = pSelect->pEList;
136742 assert( pEList!=0 )((void) (0));
136743 if( pEList->nExpr!=1 ){
136744 return 0; /* The result set must have exactly one column */
136745 }
136746 assert( pEList->a[0].pExpr )((void) (0));
136747 if( pEList->a[0].pExpr->op!=TK_ASTERISK180 ){
136748 return 0; /* The result set must be the special operator "*" */
136749 }
136750
136751 /* At this point we have established that the statement is of the
136752 ** correct syntactic form to participate in this optimization. Now
136753 ** we have to check the semantics.
136754 */
136755 pItem = pSelect->pSrc->a;
136756 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
136757 if( pSrc==0 ){
136758 return 0; /* FROM clause does not contain a real table */
136759 }
136760 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
136761 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */
136762 return 0; /* tab1 and tab2 may not be the same table */
136763 }
136764 if( HasRowid(pDest)(((pDest)->tabFlags & 0x00000080)==0)!=HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) ){
136765 return 0; /* source and destination must both be WITHOUT ROWID or not */
136766 }
136767 if( !IsOrdinaryTable(pSrc)((pSrc)->eTabType==0) ){
136768 return 0; /* tab2 may not be a view or virtual table */
136769 }
136770 if( pDest->nCol!=pSrc->nCol ){
136771 return 0; /* Number of columns must be the same in tab1 and tab2 */
136772 }
136773 if( pDest->iPKey!=pSrc->iPKey ){
136774 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
136775 }
136776 if( (pDest->tabFlags & TF_Strict0x00010000)!=0 && (pSrc->tabFlags & TF_Strict0x00010000)==0 ){
136777 return 0; /* Cannot feed from a non-strict into a strict table */
136778 }
136779 for(i=0; i<pDest->nCol; i++){
136780 Column *pDestCol = &pDest->aCol[i];
136781 Column *pSrcCol = &pSrc->aCol[i];
136782#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
136783 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
136784 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN0x0002
136785 ){
136786 return 0; /* Neither table may have __hidden__ columns */
136787 }
136788#endif
136789#ifndef SQLITE_OMIT_GENERATED_COLUMNS
136790 /* Even if tables t1 and t2 have identical schemas, if they contain
136791 ** generated columns, then this statement is semantically incorrect:
136792 **
136793 ** INSERT INTO t2 SELECT * FROM t1;
136794 **
136795 ** The reason is that generated column values are returned by the
136796 ** the SELECT statement on the right but the INSERT statement on the
136797 ** left wants them to be omitted.
136798 **
136799 ** Nevertheless, this is a useful notational shorthand to tell SQLite
136800 ** to do a bulk transfer all of the content from t1 over to t2.
136801 **
136802 ** We could, in theory, disable this (except for internal use by the
136803 ** VACUUM command where it is actually needed). But why do that? It
136804 ** seems harmless enough, and provides a useful service.
136805 */
136806 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060) !=
136807 (pSrcCol->colFlags & COLFLAG_GENERATED0x0060) ){
136808 return 0; /* Both columns have the same generated-column type */
136809 }
136810 /* But the transfer is only allowed if both the source and destination
136811 ** tables have the exact same expressions for generated columns.
136812 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
136813 */
136814 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060)!=0 ){
136815 if( sqlite3ExprCompare(0,
136816 sqlite3ColumnExpr(pSrc, pSrcCol),
136817 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
136818 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
136819 testcase( pDestCol->colFlags & COLFLAG_STORED );
136820 return 0; /* Different generator expressions */
136821 }
136822 }
136823#endif
136824 if( pDestCol->affinity!=pSrcCol->affinity ){
136825 return 0; /* Affinity must be the same on all columns */
136826 }
136827 if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol),
136828 sqlite3ColumnColl(pSrcCol))!=0 ){
136829 return 0; /* Collating sequence must be the same on all columns */
136830 }
136831 if( pDestCol->notNull && !pSrcCol->notNull ){
136832 return 0; /* tab2 must be NOT NULL if tab1 is */
136833 }
136834 /* Default values for second and subsequent columns need to match. */
136835 if( (pDestCol->colFlags & COLFLAG_GENERATED0x0060)==0 && i>0 ){
136836 Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol);
136837 Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol);
136838 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN )((void) (0));
136839 assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) )((void) (0));
136840 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN )((void) (0));
136841 assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) )((void) (0));
136842 if( (pDestExpr==0)!=(pSrcExpr==0)
136843 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
136844 pSrcExpr->u.zToken)!=0)
136845 ){
136846 return 0; /* Default values must be the same for all columns */
136847 }
136848 }
136849 }
136850 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
136851 if( IsUniqueIndex(pDestIdx)((pDestIdx)->onError!=0) ){
136852 destHasUniqueIdx = 1;
136853 }
136854 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
136855 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
136856 }
136857 if( pSrcIdx==0 ){
136858 return 0; /* pDestIdx has no corresponding index in pSrc */
136859 }
136860 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
136861 && sqlite3FaultSim(411)==SQLITE_OK0 ){
136862 /* The sqlite3FaultSim() call allows this corruption test to be
136863 ** bypassed during testing, in order to exercise other corruption tests
136864 ** further downstream. */
136865 return 0; /* Corrupt schema - two indexes on the same btree */
136866 }
136867 }
136868#ifndef SQLITE_OMIT_CHECK
136869 if( pDest->pCheck
136870 && (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
136871 && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1)
136872 ){
136873 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
136874 }
136875#endif
136876#ifndef SQLITE_OMIT_FOREIGN_KEY
136877 /* Disallow the transfer optimization if the destination table contains
136878 ** any foreign key constraints. This is more restrictive than necessary.
136879 ** But the main beneficiary of the transfer optimization is the VACUUM
136880 ** command, and the VACUUM command disables foreign key constraints. So
136881 ** the extra complication to make this rule less restrictive is probably
136882 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
136883 */
136884 assert( IsOrdinaryTable(pDest) )((void) (0));
136885 if( (db->flags & SQLITE_ForeignKeys0x00004000)!=0 && pDest->u.tab.pFKey!=0 ){
136886 return 0;
136887 }
136888#endif
136889 if( (db->flags & SQLITE_CountRows((u64)(0x00001)<<32))!=0 ){
136890 return 0; /* xfer opt does not play well with PRAGMA count_changes */
136891 }
136892
136893 /* If we get this far, it means that the xfer optimization is at
136894 ** least a possibility, though it might only work if the destination
136895 ** table (tab1) is initially empty.
136896 */
136897#ifdef SQLITE_TEST
136898 sqlite3_xferopt_count++;
136899#endif
136900 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
136901 v = sqlite3GetVdbe(pParse);
136902 sqlite3CodeVerifySchema(pParse, iDbSrc);
136903 iSrc = pParse->nTab++;
136904 iDest = pParse->nTab++;
136905 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
136906 regData = sqlite3GetTempReg(pParse);
136907 sqlite3VdbeAddOp2(v, OP_Null75, 0, regData);
136908 regRowid = sqlite3GetTempReg(pParse);
136909 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite113);
136910 assert( HasRowid(pDest) || destHasUniqueIdx )((void) (0));
136911 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 && (
136912 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
136913 || destHasUniqueIdx /* (2) */
136914 || (onError!=OE_Abort2 && onError!=OE_Rollback1) /* (3) */
136915 )){
136916 /* In some circumstances, we are able to run the xfer optimization
136917 ** only if the destination table is initially empty. Unless the
136918 ** DBFLAG_Vacuum flag is set, this block generates code to make
136919 ** that determination. If DBFLAG_Vacuum is set, then the destination
136920 ** table is always empty.
136921 **
136922 ** Conditions under which the destination must be empty:
136923 **
136924 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
136925 ** (If the destination is not initially empty, the rowid fields
136926 ** of index entries might need to change.)
136927 **
136928 ** (2) The destination has a unique index. (The xfer optimization
136929 ** is unable to test uniqueness.)
136930 **
136931 ** (3) onError is something other than OE_Abort and OE_Rollback.
136932 */
136933 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iDest, 0); VdbeCoverage(v);
136934 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto9);
136935 sqlite3VdbeJumpHere(v, addr1);
136936 }
136937 if( HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) ){
136938 u8 insFlags;
136939 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead102);
136940 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind36, iSrc, 0); VdbeCoverage(v);
136941 if( pDest->iPKey>=0 ){
136942 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid135, iSrc, regRowid);
136943 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
136944 sqlite3VdbeVerifyAbortable(v, onError);
136945 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists31, iDest, 0, regRowid);
136946 VdbeCoverage(v);
136947 sqlite3RowidConstraint(pParse, onError, pDest);
136948 sqlite3VdbeJumpHere(v, addr2);
136949 }
136950 autoIncStep(pParse, regAutoinc, regRowid);
136951 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto0x0008) ){
136952 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid127, iDest, regRowid);
136953 }else{
136954 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid135, iSrc, regRowid);
136955 assert( (pDest->tabFlags & TF_Autoincrement)==0 )((void) (0));
136956 }
136957
136958 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
136959 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iDest);
136960 insFlags = OPFLAG_APPEND0x08|OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80;
136961 }else{
136962 insFlags = OPFLAG_NCHANGE0x01|OPFLAG_LASTROWID0x20|OPFLAG_APPEND0x08|OPFLAG_PREFORMAT0x80;
136963 }
136964#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
136965 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
136966 sqlite3VdbeAddOp3(v, OP_RowData134, iSrc, regData, 1);
136967 insFlags &= ~OPFLAG_PREFORMAT0x80;
136968 }else
136969#endif
136970 {
136971 sqlite3VdbeAddOp3(v, OP_RowCell129, iDest, iSrc, regRowid);
136972 }
136973 sqlite3VdbeAddOp3(v, OP_Insert128, iDest, regData, regRowid);
136974 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 ){
136975 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE(-5));
136976 }
136977 sqlite3VdbeChangeP5(v, insFlags);
136978
136979 sqlite3VdbeAddOp2(v, OP_Next39, iSrc, addr1); VdbeCoverage(v);
136980 sqlite3VdbeAddOp2(v, OP_Close122, iSrc, 0);
136981 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
136982 }else{
136983 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
136984 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
136985 }
136986 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
136987 u8 idxInsFlags = 0;
136988 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx)(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
136989 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
136990 }
136991 assert( pSrcIdx )((void) (0));
136992 sqlite3VdbeAddOp3(v, OP_OpenRead102, iSrc, pSrcIdx->tnum, iDbSrc);
136993 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
136994 VdbeComment((v, "%s", pSrcIdx->zName));
136995 sqlite3VdbeAddOp3(v, OP_OpenWrite113, iDest, pDestIdx->tnum, iDbDest);
136996 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
136997 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01);
136998 VdbeComment((v, "%s", pDestIdx->zName));
136999 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iSrc, 0); VdbeCoverage(v);
137000 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
137001 /* This INSERT command is part of a VACUUM operation, which guarantees
137002 ** that the destination table is empty. If all indexed columns use
137003 ** collation sequence BINARY, then it can also be assumed that the
137004 ** index will be populated by inserting keys in strictly sorted
137005 ** order. In this case, instead of seeking within the b-tree as part
137006 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
137007 ** OP_IdxInsert to seek to the point within the b-tree where each key
137008 ** should be inserted. This is faster.
137009 **
137010 ** If any of the indexed columns use a collation sequence other than
137011 ** BINARY, this optimization is disabled. This is because the user
137012 ** might change the definition of a collation sequence and then run
137013 ** a VACUUM command. In that case keys may not be written in strictly
137014 ** sorted order. */
137015 for(i=0; i<pSrcIdx->nColumn; i++){
137016 const char *zColl = pSrcIdx->azColl[i];
137017 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
137018 }
137019 if( i==pSrcIdx->nColumn ){
137020 idxInsFlags = OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80;
137021 sqlite3VdbeAddOp1(v, OP_SeekEnd137, iDest);
137022 sqlite3VdbeAddOp2(v, OP_RowCell129, iDest, iSrc);
137023 }
137024 }else if( !HasRowid(pSrc)(((pSrc)->tabFlags & 0x00000080)==0) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
137025 idxInsFlags |= OPFLAG_NCHANGE0x01;
137026 }
137027 if( idxInsFlags!=(OPFLAG_USESEEKRESULT0x10|OPFLAG_PREFORMAT0x80) ){
137028 sqlite3VdbeAddOp3(v, OP_RowData134, iSrc, regData, 1);
137029 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
137030 && !HasRowid(pDest)(((pDest)->tabFlags & 0x00000080)==0)
137031 && IsPrimaryKeyIndex(pDestIdx)((pDestIdx)->idxType==2)
137032 ){
137033 codeWithoutRowidPreupdate(pParse, pDest, iDest, regData);
137034 }
137035 }
137036 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iDest, regData);
137037 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND0x08);
137038 sqlite3VdbeAddOp2(v, OP_Next39, iSrc, addr1+1); VdbeCoverage(v);
137039 sqlite3VdbeJumpHere(v, addr1);
137040 sqlite3VdbeAddOp2(v, OP_Close122, iSrc, 0);
137041 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
137042 }
137043 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
137044 sqlite3ReleaseTempReg(pParse, regRowid);
137045 sqlite3ReleaseTempReg(pParse, regData);
137046 if( emptyDestTest ){
137047 sqlite3AutoincrementEnd(pParse);
137048 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_OK0, 0);
137049 sqlite3VdbeJumpHere(v, emptyDestTest);
137050 sqlite3VdbeAddOp2(v, OP_Close122, iDest, 0);
137051 return 0;
137052 }else{
137053 return 1;
137054 }
137055}
137056#endif /* SQLITE_OMIT_XFER_OPT */
137057
137058/************** End of insert.c **********************************************/
137059/************** Begin file legacy.c ******************************************/
137060/*
137061** 2001 September 15
137062**
137063** The author disclaims copyright to this source code. In place of
137064** a legal notice, here is a blessing:
137065**
137066** May you do good and not evil.
137067** May you find forgiveness for yourself and forgive others.
137068** May you share freely, never taking more than you give.
137069**
137070*************************************************************************
137071** Main file for the SQLite library. The routines in this file
137072** implement the programmer interface to the library. Routines in
137073** other files are for internal use by SQLite and should not be
137074** accessed by users of the library.
137075*/
137076
137077/* #include "sqliteInt.h" */
137078
137079/*
137080** Execute SQL code. Return one of the SQLITE_ success/failure
137081** codes. Also write an error message into memory obtained from
137082** malloc() and make *pzErrMsg point to that message.
137083**
137084** If the SQL is a query, then for each row in the query result
137085** the xCallback() function is called. pArg becomes the first
137086** argument to xCallback(). If xCallback=NULL then no callback
137087** is invoked, even for queries.
137088*/
137089SQLITE_API int sqlite3_exec(
137090 sqlite3 *db, /* The database on which the SQL executes */
137091 const char *zSql, /* The SQL to be executed */
137092 sqlite3_callback xCallback, /* Invoke this callback routine */
137093 void *pArg, /* First argument to xCallback() */
137094 char **pzErrMsg /* Write error messages here */
137095){
137096 int rc = SQLITE_OK0; /* Return code */
137097 const char *zLeftover; /* Tail of unprocessed SQL */
137098 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
137099 char **azCols = 0; /* Names of result columns */
137100 int callbackIsInit; /* True if callback data is initialized */
137101
137102 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(137102);
137103 if( zSql==0 ) zSql = "";
137104
137105 sqlite3_mutex_enter(db->mutex);
137106 sqlite3Error(db, SQLITE_OK0);
137107 while( rc==SQLITE_OK0 && zSql[0] ){
137108 int nCol = 0;
137109 char **azVals = 0;
137110
137111 pStmt = 0;
137112 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
137113 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
137114 if( rc!=SQLITE_OK0 ){
137115 continue;
137116 }
137117 if( !pStmt ){
137118 /* this happens for a comment or white-space */
137119 zSql = zLeftover;
137120 continue;
137121 }
137122 callbackIsInit = 0;
137123
137124 while( 1 ){
137125 int i;
137126 rc = sqlite3_step(pStmt);
137127
137128 /* Invoke the callback function if required */
137129 if( xCallback && (SQLITE_ROW100==rc ||
137130 (SQLITE_DONE101==rc && !callbackIsInit
137131 && db->flags&SQLITE_NullCallback0x00000100)) ){
137132 if( !callbackIsInit ){
137133 nCol = sqlite3_column_count(pStmt);
137134 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
137135 if( azCols==0 ){
137136 goto exec_out;
137137 }
137138 for(i=0; i<nCol; i++){
137139 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
137140 /* sqlite3VdbeSetColName() installs column names as UTF8
137141 ** strings so there is no way for sqlite3_column_name() to fail. */
137142 assert( azCols[i]!=0 )((void) (0));
137143 }
137144 callbackIsInit = 1;
137145 }
137146 if( rc==SQLITE_ROW100 ){
137147 azVals = &azCols[nCol];
137148 for(i=0; i<nCol; i++){
137149 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
137150 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
137151 sqlite3OomFault(db);
137152 goto exec_out;
137153 }
137154 }
137155 azVals[i] = 0;
137156 }
137157 if( xCallback(pArg, nCol, azVals, azCols) ){
137158 /* EVIDENCE-OF: R-38229-40159 If the callback function to
137159 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
137160 ** return SQLITE_ABORT. */
137161 rc = SQLITE_ABORT4;
137162 sqlite3VdbeFinalize((Vdbe *)pStmt);
137163 pStmt = 0;
137164 sqlite3Error(db, SQLITE_ABORT4);
137165 goto exec_out;
137166 }
137167 }
137168
137169 if( rc!=SQLITE_ROW100 ){
137170 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
137171 pStmt = 0;
137172 zSql = zLeftover;
137173 while( sqlite3Isspace(zSql[0])(sqlite3CtypeMap[(unsigned char)(zSql[0])]&0x01) ) zSql++;
137174 break;
137175 }
137176 }
137177
137178 sqlite3DbFree(db, azCols);
137179 azCols = 0;
137180 }
137181
137182exec_out:
137183 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
137184 sqlite3DbFree(db, azCols);
137185
137186 rc = sqlite3ApiExit(db, rc);
137187 if( rc!=SQLITE_OK0 && pzErrMsg ){
137188 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
137189 if( *pzErrMsg==0 ){
137190 rc = SQLITE_NOMEM_BKPT7;
137191 sqlite3Error(db, SQLITE_NOMEM7);
137192 }
137193 }else if( pzErrMsg ){
137194 *pzErrMsg = 0;
137195 }
137196
137197 assert( (rc&db->errMask)==rc )((void) (0));
137198 sqlite3_mutex_leave(db->mutex);
137199 return rc;
137200}
137201
137202/************** End of legacy.c **********************************************/
137203/************** Begin file loadext.c *****************************************/
137204/*
137205** 2006 June 7
137206**
137207** The author disclaims copyright to this source code. In place of
137208** a legal notice, here is a blessing:
137209**
137210** May you do good and not evil.
137211** May you find forgiveness for yourself and forgive others.
137212** May you share freely, never taking more than you give.
137213**
137214*************************************************************************
137215** This file contains code used to dynamically load extensions into
137216** the SQLite library.
137217*/
137218
137219#ifndef SQLITE_CORE1
137220 #define SQLITE_CORE1 1 /* Disable the API redefinition in sqlite3ext.h */
137221#endif
137222/************** Include sqlite3ext.h in the middle of loadext.c **************/
137223/************** Begin file sqlite3ext.h **************************************/
137224/*
137225** 2006 June 7
137226**
137227** The author disclaims copyright to this source code. In place of
137228** a legal notice, here is a blessing:
137229**
137230** May you do good and not evil.
137231** May you find forgiveness for yourself and forgive others.
137232** May you share freely, never taking more than you give.
137233**
137234*************************************************************************
137235** This header file defines the SQLite interface for use by
137236** shared libraries that want to be imported as extensions into
137237** an SQLite instance. Shared libraries that intend to be loaded
137238** as extensions by SQLite should #include this file instead of
137239** sqlite3.h.
137240*/
137241#ifndef SQLITE3EXT_H
137242#define SQLITE3EXT_H
137243/* #include "sqlite3.h" */
137244
137245/*
137246** The following structure holds pointers to all of the SQLite API
137247** routines.
137248**
137249** WARNING: In order to maintain backwards compatibility, add new
137250** interfaces to the end of this structure only. If you insert new
137251** interfaces in the middle of this structure, then older different
137252** versions of SQLite will not be able to load each other's shared
137253** libraries!
137254*/
137255struct sqlite3_api_routines {
137256 void * (*aggregate_context)(sqlite3_context*,int nBytes);
137257 int (*aggregate_count)(sqlite3_context*);
137258 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
137259 int (*bind_double)(sqlite3_stmt*,int,double);
137260 int (*bind_int)(sqlite3_stmt*,int,int);
137261 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
137262 int (*bind_null)(sqlite3_stmt*,int);
137263 int (*bind_parameter_count)(sqlite3_stmt*);
137264 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
137265 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
137266 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
137267 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
137268 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
137269 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
137270 int (*busy_timeout)(sqlite3*,int ms);
137271 int (*changes)(sqlite3*);
137272 int (*close)(sqlite3*);
137273 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
137274 int eTextRep,const char*));
137275 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
137276 int eTextRep,const void*));
137277 const void * (*column_blob)(sqlite3_stmt*,int iCol);
137278 int (*column_bytes)(sqlite3_stmt*,int iCol);
137279 int (*column_bytes16)(sqlite3_stmt*,int iCol);
137280 int (*column_count)(sqlite3_stmt*pStmt);
137281 const char * (*column_database_name)(sqlite3_stmt*,int);
137282 const void * (*column_database_name16)(sqlite3_stmt*,int);
137283 const char * (*column_decltype)(sqlite3_stmt*,int i);
137284 const void * (*column_decltype16)(sqlite3_stmt*,int);
137285 double (*column_double)(sqlite3_stmt*,int iCol);
137286 int (*column_int)(sqlite3_stmt*,int iCol);
137287 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
137288 const char * (*column_name)(sqlite3_stmt*,int);
137289 const void * (*column_name16)(sqlite3_stmt*,int);
137290 const char * (*column_origin_name)(sqlite3_stmt*,int);
137291 const void * (*column_origin_name16)(sqlite3_stmt*,int);
137292 const char * (*column_table_name)(sqlite3_stmt*,int);
137293 const void * (*column_table_name16)(sqlite3_stmt*,int);
137294 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
137295 const void * (*column_text16)(sqlite3_stmt*,int iCol);
137296 int (*column_type)(sqlite3_stmt*,int iCol);
137297 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
137298 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
137299 int (*complete)(const char*sql);
137300 int (*complete16)(const void*sql);
137301 int (*create_collation)(sqlite3*,const char*,int,void*,
137302 int(*)(void*,int,const void*,int,const void*));
137303 int (*create_collation16)(sqlite3*,const void*,int,void*,
137304 int(*)(void*,int,const void*,int,const void*));
137305 int (*create_function)(sqlite3*,const char*,int,int,void*,
137306 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
137307 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
137308 void (*xFinal)(sqlite3_context*));
137309 int (*create_function16)(sqlite3*,const void*,int,int,void*,
137310 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
137311 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
137312 void (*xFinal)(sqlite3_context*));
137313 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
137314 int (*data_count)(sqlite3_stmt*pStmt);
137315 sqlite3 * (*db_handle)(sqlite3_stmt*);
137316 int (*declare_vtab)(sqlite3*,const char*);
137317 int (*enable_shared_cache)(int);
137318 int (*errcode)(sqlite3*db);
137319 const char * (*errmsg)(sqlite3*);
137320 const void * (*errmsg16)(sqlite3*);
137321 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
137322 int (*expired)(sqlite3_stmt*);
137323 int (*finalize)(sqlite3_stmt*pStmt);
137324 void (*free)(void*);
137325 void (*free_table)(char**result);
137326 int (*get_autocommit)(sqlite3*);
137327 void * (*get_auxdata)(sqlite3_context*,int);
137328 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
137329 int (*global_recover)(void);
137330 void (*interruptx)(sqlite3*);
137331 sqlite_int64 (*last_insert_rowid)(sqlite3*);
137332 const char * (*libversion)(void);
137333 int (*libversion_number)(void);
137334 void *(*malloc)(int);
137335 char * (*mprintf)(const char*,...);
137336 int (*open)(const char*,sqlite3**);
137337 int (*open16)(const void*,sqlite3**);
137338 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
137339 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
137340 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
137341 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
137342 void *(*realloc)(void*,int);
137343 int (*reset)(sqlite3_stmt*pStmt);
137344 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
137345 void (*result_double)(sqlite3_context*,double);
137346 void (*result_error)(sqlite3_context*,const char*,int);
137347 void (*result_error16)(sqlite3_context*,const void*,int);
137348 void (*result_int)(sqlite3_context*,int);
137349 void (*result_int64)(sqlite3_context*,sqlite_int64);
137350 void (*result_null)(sqlite3_context*);
137351 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
137352 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
137353 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
137354 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
137355 void (*result_value)(sqlite3_context*,sqlite3_value*);
137356 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
137357 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
137358 const char*,const char*),void*);
137359 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
137360 char * (*xsnprintf)(int,char*,const char*,...);
137361 int (*step)(sqlite3_stmt*);
137362 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
137363 char const**,char const**,int*,int*,int*);
137364 void (*thread_cleanup)(void);
137365 int (*total_changes)(sqlite3*);
137366 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
137367 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
137368 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
137369 sqlite_int64),void*);
137370 void * (*user_data)(sqlite3_context*);
137371 const void * (*value_blob)(sqlite3_value*);
137372 int (*value_bytes)(sqlite3_value*);
137373 int (*value_bytes16)(sqlite3_value*);
137374 double (*value_double)(sqlite3_value*);
137375 int (*value_int)(sqlite3_value*);
137376 sqlite_int64 (*value_int64)(sqlite3_value*);
137377 int (*value_numeric_type)(sqlite3_value*);
137378 const unsigned char * (*value_text)(sqlite3_value*);
137379 const void * (*value_text16)(sqlite3_value*);
137380 const void * (*value_text16be)(sqlite3_value*);
137381 const void * (*value_text16le)(sqlite3_value*);
137382 int (*value_type)(sqlite3_value*);
137383 char *(*vmprintf)(const char*,va_list);
137384 /* Added ??? */
137385 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
137386 /* Added by 3.3.13 */
137387 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
137388 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
137389 int (*clear_bindings)(sqlite3_stmt*);
137390 /* Added by 3.4.1 */
137391 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
137392 void (*xDestroy)(void *));
137393 /* Added by 3.5.0 */
137394 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
137395 int (*blob_bytes)(sqlite3_blob*);
137396 int (*blob_close)(sqlite3_blob*);
137397 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
137398 int,sqlite3_blob**);
137399 int (*blob_read)(sqlite3_blob*,void*,int,int);
137400 int (*blob_write)(sqlite3_blob*,const void*,int,int);
137401 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
137402 int(*)(void*,int,const void*,int,const void*),
137403 void(*)(void*));
137404 int (*file_control)(sqlite3*,const char*,int,void*);
137405 sqlite3_int64 (*memory_highwater)(int);
137406 sqlite3_int64 (*memory_used)(void);
137407 sqlite3_mutex *(*mutex_alloc)(int);
137408 void (*mutex_enter)(sqlite3_mutex*);
137409 void (*mutex_free)(sqlite3_mutex*);
137410 void (*mutex_leave)(sqlite3_mutex*);
137411 int (*mutex_try)(sqlite3_mutex*);
137412 int (*open_v2)(const char*,sqlite3**,int,const char*);
137413 int (*release_memory)(int);
137414 void (*result_error_nomem)(sqlite3_context*);
137415 void (*result_error_toobig)(sqlite3_context*);
137416 int (*sleep)(int);
137417 void (*soft_heap_limit)(int);
137418 sqlite3_vfs *(*vfs_find)(const char*);
137419 int (*vfs_register)(sqlite3_vfs*,int);
137420 int (*vfs_unregister)(sqlite3_vfs*);
137421 int (*xthreadsafe)(void);
137422 void (*result_zeroblob)(sqlite3_context*,int);
137423 void (*result_error_code)(sqlite3_context*,int);
137424 int (*test_control)(int, ...);
137425 void (*randomness)(int,void*);
137426 sqlite3 *(*context_db_handle)(sqlite3_context*);
137427 int (*extended_result_codes)(sqlite3*,int);
137428 int (*limit)(sqlite3*,int,int);
137429 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
137430 const char *(*sql)(sqlite3_stmt*);
137431 int (*status)(int,int*,int*,int);
137432 int (*backup_finish)(sqlite3_backup*);
137433 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
137434 int (*backup_pagecount)(sqlite3_backup*);
137435 int (*backup_remaining)(sqlite3_backup*);
137436 int (*backup_step)(sqlite3_backup*,int);
137437 const char *(*compileoption_get)(int);
137438 int (*compileoption_used)(const char*);
137439 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
137440 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
137441 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
137442 void (*xFinal)(sqlite3_context*),
137443 void(*xDestroy)(void*));
137444 int (*db_config)(sqlite3*,int,...);
137445 sqlite3_mutex *(*db_mutex)(sqlite3*);
137446 int (*db_status)(sqlite3*,int,int*,int*,int);
137447 int (*extended_errcode)(sqlite3*);
137448 void (*log)(int,const char*,...);
137449 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
137450 const char *(*sourceid)(void);
137451 int (*stmt_status)(sqlite3_stmt*,int,int);
137452 int (*strnicmp)(const char*,const char*,int);
137453 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
137454 int (*wal_autocheckpoint)(sqlite3*,int);
137455 int (*wal_checkpoint)(sqlite3*,const char*);
137456 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
137457 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
137458 int (*vtab_config)(sqlite3*,int op,...);
137459 int (*vtab_on_conflict)(sqlite3*);
137460 /* Version 3.7.16 and later */
137461 int (*close_v2)(sqlite3*);
137462 const char *(*db_filename)(sqlite3*,const char*);
137463 int (*db_readonly)(sqlite3*,const char*);
137464 int (*db_release_memory)(sqlite3*);
137465 const char *(*errstr)(int);
137466 int (*stmt_busy)(sqlite3_stmt*);
137467 int (*stmt_readonly)(sqlite3_stmt*);
137468 int (*stricmp)(const char*,const char*);
137469 int (*uri_boolean)(const char*,const char*,int);
137470 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
137471 const char *(*uri_parameter)(const char*,const char*);
137472 char *(*xvsnprintf)(int,char*,const char*,va_list);
137473 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
137474 /* Version 3.8.7 and later */
137475 int (*auto_extension)(void(*)(void));
137476 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
137477 void(*)(void*));
137478 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
137479 void(*)(void*),unsigned char);
137480 int (*cancel_auto_extension)(void(*)(void));
137481 int (*load_extension)(sqlite3*,const char*,const char*,char**);
137482 void *(*malloc64)(sqlite3_uint64);
137483 sqlite3_uint64 (*msize)(void*);
137484 void *(*realloc64)(void*,sqlite3_uint64);
137485 void (*reset_auto_extension)(void);
137486 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
137487 void(*)(void*));
137488 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
137489 void(*)(void*), unsigned char);
137490 int (*strglob)(const char*,const char*);
137491 /* Version 3.8.11 and later */
137492 sqlite3_value *(*value_dup)(const sqlite3_value*);
137493 void (*value_free)(sqlite3_value*);
137494 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
137495 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
137496 /* Version 3.9.0 and later */
137497 unsigned int (*value_subtype)(sqlite3_value*);
137498 void (*result_subtype)(sqlite3_context*,unsigned int);
137499 /* Version 3.10.0 and later */
137500 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
137501 int (*strlike)(const char*,const char*,unsigned int);
137502 int (*db_cacheflush)(sqlite3*);
137503 /* Version 3.12.0 and later */
137504 int (*system_errno)(sqlite3*);
137505 /* Version 3.14.0 and later */
137506 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
137507 char *(*expanded_sql)(sqlite3_stmt*);
137508 /* Version 3.18.0 and later */
137509 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
137510 /* Version 3.20.0 and later */
137511 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
137512 sqlite3_stmt**,const char**);
137513 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
137514 sqlite3_stmt**,const void**);
137515 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
137516 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
137517 void *(*value_pointer)(sqlite3_value*,const char*);
137518 int (*vtab_nochange)(sqlite3_context*);
137519 int (*value_nochange)(sqlite3_value*);
137520 const char *(*vtab_collation)(sqlite3_index_info*,int);
137521 /* Version 3.24.0 and later */
137522 int (*keyword_count)(void);
137523 int (*keyword_name)(int,const char**,int*);
137524 int (*keyword_check)(const char*,int);
137525 sqlite3_str *(*str_new)(sqlite3*);
137526 char *(*str_finish)(sqlite3_str*);
137527 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
137528 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
137529 void (*str_append)(sqlite3_str*, const char *zIn, int N);
137530 void (*str_appendall)(sqlite3_str*, const char *zIn);
137531 void (*str_appendchar)(sqlite3_str*, int N, char C);
137532 void (*str_reset)(sqlite3_str*);
137533 int (*str_errcode)(sqlite3_str*);
137534 int (*str_length)(sqlite3_str*);
137535 char *(*str_value)(sqlite3_str*);
137536 /* Version 3.25.0 and later */
137537 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
137538 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
137539 void (*xFinal)(sqlite3_context*),
137540 void (*xValue)(sqlite3_context*),
137541 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
137542 void(*xDestroy)(void*));
137543 /* Version 3.26.0 and later */
137544 const char *(*normalized_sql)(sqlite3_stmt*);
137545 /* Version 3.28.0 and later */
137546 int (*stmt_isexplain)(sqlite3_stmt*);
137547 int (*value_frombind)(sqlite3_value*);
137548 /* Version 3.30.0 and later */
137549 int (*drop_modules)(sqlite3*,const char**);
137550 /* Version 3.31.0 and later */
137551 sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
137552 const char *(*uri_key)(const char*,int);
137553 const char *(*filename_database)(const char*);
137554 const char *(*filename_journal)(const char*);
137555 const char *(*filename_wal)(const char*);
137556 /* Version 3.32.0 and later */
137557 const char *(*create_filename)(const char*,const char*,const char*,
137558 int,const char**);
137559 void (*free_filename)(const char*);
137560 sqlite3_file *(*database_file_object)(const char*);
137561 /* Version 3.34.0 and later */
137562 int (*txn_state)(sqlite3*,const char*);
137563 /* Version 3.36.1 and later */
137564 sqlite3_int64 (*changes64)(sqlite3*);
137565 sqlite3_int64 (*total_changes64)(sqlite3*);
137566 /* Version 3.37.0 and later */
137567 int (*autovacuum_pages)(sqlite3*,
137568 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
137569 void*, void(*)(void*));
137570 /* Version 3.38.0 and later */
137571 int (*error_offset)(sqlite3*);
137572 int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
137573 int (*vtab_distinct)(sqlite3_index_info*);
137574 int (*vtab_in)(sqlite3_index_info*,int,int);
137575 int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
137576 int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
137577 /* Version 3.39.0 and later */
137578 int (*deserialize)(sqlite3*,const char*,unsigned char*,
137579 sqlite3_int64,sqlite3_int64,unsigned);
137580 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
137581 unsigned int);
137582 const char *(*db_name)(sqlite3*,int);
137583 /* Version 3.40.0 and later */
137584 int (*value_encoding)(sqlite3_value*);
137585 /* Version 3.41.0 and later */
137586 int (*is_interrupted)(sqlite3*);
137587 /* Version 3.43.0 and later */
137588 int (*stmt_explain)(sqlite3_stmt*,int);
137589 /* Version 3.44.0 and later */
137590 void *(*get_clientdata)(sqlite3*,const char*);
137591 int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
137592};
137593
137594/*
137595** This is the function signature used for all extension entry points. It
137596** is also defined in the file "loadext.c".
137597*/
137598typedef int (*sqlite3_loadext_entry)(
137599 sqlite3 *db, /* Handle to the database. */
137600 char **pzErrMsg, /* Used to set error string on failure. */
137601 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
137602);
137603
137604/*
137605** The following macros redefine the API routines so that they are
137606** redirected through the global sqlite3_api structure.
137607**
137608** This header file is also used by the loadext.c source file
137609** (part of the main SQLite library - not an extension) so that
137610** it can get access to the sqlite3_api_routines structure
137611** definition. But the main library does not want to redefine
137612** the API. So the redefinition macros are only valid if the
137613** SQLITE_CORE macros is undefined.
137614*/
137615#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
137616#define sqlite3_aggregate_context sqlite3_api->aggregate_context
137617#ifndef SQLITE_OMIT_DEPRECATED1
137618#define sqlite3_aggregate_count sqlite3_api->aggregate_count
137619#endif
137620#define sqlite3_bind_blob sqlite3_api->bind_blob
137621#define sqlite3_bind_double sqlite3_api->bind_double
137622#define sqlite3_bind_int sqlite3_api->bind_int
137623#define sqlite3_bind_int64 sqlite3_api->bind_int64
137624#define sqlite3_bind_null sqlite3_api->bind_null
137625#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
137626#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
137627#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
137628#define sqlite3_bind_text sqlite3_api->bind_text
137629#define sqlite3_bind_text16 sqlite3_api->bind_text16
137630#define sqlite3_bind_value sqlite3_api->bind_value
137631#define sqlite3_busy_handler sqlite3_api->busy_handler
137632#define sqlite3_busy_timeout sqlite3_api->busy_timeout
137633#define sqlite3_changes sqlite3_api->changes
137634#define sqlite3_close sqlite3_api->close
137635#define sqlite3_collation_needed sqlite3_api->collation_needed
137636#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
137637#define sqlite3_column_blob sqlite3_api->column_blob
137638#define sqlite3_column_bytes sqlite3_api->column_bytes
137639#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
137640#define sqlite3_column_count sqlite3_api->column_count
137641#define sqlite3_column_database_name sqlite3_api->column_database_name
137642#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
137643#define sqlite3_column_decltype sqlite3_api->column_decltype
137644#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
137645#define sqlite3_column_double sqlite3_api->column_double
137646#define sqlite3_column_int sqlite3_api->column_int
137647#define sqlite3_column_int64 sqlite3_api->column_int64
137648#define sqlite3_column_name sqlite3_api->column_name
137649#define sqlite3_column_name16 sqlite3_api->column_name16
137650#define sqlite3_column_origin_name sqlite3_api->column_origin_name
137651#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
137652#define sqlite3_column_table_name sqlite3_api->column_table_name
137653#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
137654#define sqlite3_column_text sqlite3_api->column_text
137655#define sqlite3_column_text16 sqlite3_api->column_text16
137656#define sqlite3_column_type sqlite3_api->column_type
137657#define sqlite3_column_value sqlite3_api->column_value
137658#define sqlite3_commit_hook sqlite3_api->commit_hook
137659#define sqlite3_complete sqlite3_api->complete
137660#define sqlite3_complete16 sqlite3_api->complete16
137661#define sqlite3_create_collation sqlite3_api->create_collation
137662#define sqlite3_create_collation16 sqlite3_api->create_collation16
137663#define sqlite3_create_function sqlite3_api->create_function
137664#define sqlite3_create_function16 sqlite3_api->create_function16
137665#define sqlite3_create_module sqlite3_api->create_module
137666#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
137667#define sqlite3_data_count sqlite3_api->data_count
137668#define sqlite3_db_handle sqlite3_api->db_handle
137669#define sqlite3_declare_vtab sqlite3_api->declare_vtab
137670#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
137671#define sqlite3_errcode sqlite3_api->errcode
137672#define sqlite3_errmsg sqlite3_api->errmsg
137673#define sqlite3_errmsg16 sqlite3_api->errmsg16
137674#define sqlite3_exec sqlite3_api->exec
137675#ifndef SQLITE_OMIT_DEPRECATED1
137676#define sqlite3_expired sqlite3_api->expired
137677#endif
137678#define sqlite3_finalize sqlite3_api->finalize
137679#define sqlite3_free sqlite3_api->free
137680#define sqlite3_free_table sqlite3_api->free_table
137681#define sqlite3_get_autocommit sqlite3_api->get_autocommit
137682#define sqlite3_get_auxdata sqlite3_api->get_auxdata
137683#define sqlite3_get_table sqlite3_api->get_table
137684#ifndef SQLITE_OMIT_DEPRECATED1
137685#define sqlite3_global_recover sqlite3_api->global_recover
137686#endif
137687#define sqlite3_interrupt sqlite3_api->interruptx
137688#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
137689#define sqlite3_libversion sqlite3_api->libversion
137690#define sqlite3_libversion_number sqlite3_api->libversion_number
137691#define sqlite3_malloc sqlite3_api->malloc
137692#define sqlite3_mprintf sqlite3_api->mprintf
137693#define sqlite3_open sqlite3_api->open
137694#define sqlite3_open16 sqlite3_api->open16
137695#define sqlite3_prepare sqlite3_api->prepare
137696#define sqlite3_prepare16 sqlite3_api->prepare16
137697#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
137698#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
137699#define sqlite3_profile sqlite3_api->profile
137700#define sqlite3_progress_handler sqlite3_api->progress_handler
137701#define sqlite3_realloc sqlite3_api->realloc
137702#define sqlite3_reset sqlite3_api->reset
137703#define sqlite3_result_blob sqlite3_api->result_blob
137704#define sqlite3_result_double sqlite3_api->result_double
137705#define sqlite3_result_error sqlite3_api->result_error
137706#define sqlite3_result_error16 sqlite3_api->result_error16
137707#define sqlite3_result_int sqlite3_api->result_int
137708#define sqlite3_result_int64 sqlite3_api->result_int64
137709#define sqlite3_result_null sqlite3_api->result_null
137710#define sqlite3_result_text sqlite3_api->result_text
137711#define sqlite3_result_text16 sqlite3_api->result_text16
137712#define sqlite3_result_text16be sqlite3_api->result_text16be
137713#define sqlite3_result_text16le sqlite3_api->result_text16le
137714#define sqlite3_result_value sqlite3_api->result_value
137715#define sqlite3_rollback_hook sqlite3_api->rollback_hook
137716#define sqlite3_set_authorizer sqlite3_api->set_authorizer
137717#define sqlite3_set_auxdata sqlite3_api->set_auxdata
137718#define sqlite3_snprintf sqlite3_api->xsnprintf
137719#define sqlite3_step sqlite3_api->step
137720#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
137721#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
137722#define sqlite3_total_changes sqlite3_api->total_changes
137723#define sqlite3_trace sqlite3_api->trace
137724#ifndef SQLITE_OMIT_DEPRECATED1
137725#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
137726#endif
137727#define sqlite3_update_hook sqlite3_api->update_hook
137728#define sqlite3_user_data sqlite3_api->user_data
137729#define sqlite3_value_blob sqlite3_api->value_blob
137730#define sqlite3_value_bytes sqlite3_api->value_bytes
137731#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
137732#define sqlite3_value_double sqlite3_api->value_double
137733#define sqlite3_value_int sqlite3_api->value_int
137734#define sqlite3_value_int64 sqlite3_api->value_int64
137735#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
137736#define sqlite3_value_text sqlite3_api->value_text
137737#define sqlite3_value_text16 sqlite3_api->value_text16
137738#define sqlite3_value_text16be sqlite3_api->value_text16be
137739#define sqlite3_value_text16le sqlite3_api->value_text16le
137740#define sqlite3_value_type sqlite3_api->value_type
137741#define sqlite3_vmprintf sqlite3_api->vmprintf
137742#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
137743#define sqlite3_overload_function sqlite3_api->overload_function
137744#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
137745#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
137746#define sqlite3_clear_bindings sqlite3_api->clear_bindings
137747#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
137748#define sqlite3_blob_bytes sqlite3_api->blob_bytes
137749#define sqlite3_blob_close sqlite3_api->blob_close
137750#define sqlite3_blob_open sqlite3_api->blob_open
137751#define sqlite3_blob_read sqlite3_api->blob_read
137752#define sqlite3_blob_write sqlite3_api->blob_write
137753#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
137754#define sqlite3_file_control sqlite3_api->file_control
137755#define sqlite3_memory_highwater sqlite3_api->memory_highwater
137756#define sqlite3_memory_used sqlite3_api->memory_used
137757#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
137758#define sqlite3_mutex_enter sqlite3_api->mutex_enter
137759#define sqlite3_mutex_free sqlite3_api->mutex_free
137760#define sqlite3_mutex_leave sqlite3_api->mutex_leave
137761#define sqlite3_mutex_try sqlite3_api->mutex_try
137762#define sqlite3_open_v2 sqlite3_api->open_v2
137763#define sqlite3_release_memory sqlite3_api->release_memory
137764#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
137765#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
137766#define sqlite3_sleep sqlite3_api->sleep
137767#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
137768#define sqlite3_vfs_find sqlite3_api->vfs_find
137769#define sqlite3_vfs_register sqlite3_api->vfs_register
137770#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
137771#define sqlite3_threadsafe sqlite3_api->xthreadsafe
137772#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
137773#define sqlite3_result_error_code sqlite3_api->result_error_code
137774#define sqlite3_test_control sqlite3_api->test_control
137775#define sqlite3_randomness sqlite3_api->randomness
137776#define sqlite3_context_db_handle sqlite3_api->context_db_handle
137777#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
137778#define sqlite3_limit sqlite3_api->limit
137779#define sqlite3_next_stmt sqlite3_api->next_stmt
137780#define sqlite3_sql sqlite3_api->sql
137781#define sqlite3_status sqlite3_api->status
137782#define sqlite3_backup_finish sqlite3_api->backup_finish
137783#define sqlite3_backup_init sqlite3_api->backup_init
137784#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
137785#define sqlite3_backup_remaining sqlite3_api->backup_remaining
137786#define sqlite3_backup_step sqlite3_api->backup_step
137787#define sqlite3_compileoption_get sqlite3_api->compileoption_get
137788#define sqlite3_compileoption_used sqlite3_api->compileoption_used
137789#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
137790#define sqlite3_db_config sqlite3_api->db_config
137791#define sqlite3_db_mutex sqlite3_api->db_mutex
137792#define sqlite3_db_status sqlite3_api->db_status
137793#define sqlite3_extended_errcode sqlite3_api->extended_errcode
137794#define sqlite3_log sqlite3_api->log
137795#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
137796#define sqlite3_sourceid sqlite3_api->sourceid
137797#define sqlite3_stmt_status sqlite3_api->stmt_status
137798#define sqlite3_strnicmp sqlite3_api->strnicmp
137799#define sqlite3_unlock_notify sqlite3_api->unlock_notify
137800#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
137801#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
137802#define sqlite3_wal_hook sqlite3_api->wal_hook
137803#define sqlite3_blob_reopen sqlite3_api->blob_reopen
137804#define sqlite3_vtab_config sqlite3_api->vtab_config
137805#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
137806/* Version 3.7.16 and later */
137807#define sqlite3_close_v2 sqlite3_api->close_v2
137808#define sqlite3_db_filename sqlite3_api->db_filename
137809#define sqlite3_db_readonly sqlite3_api->db_readonly
137810#define sqlite3_db_release_memory sqlite3_api->db_release_memory
137811#define sqlite3_errstr sqlite3_api->errstr
137812#define sqlite3_stmt_busy sqlite3_api->stmt_busy
137813#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
137814#define sqlite3_stricmp sqlite3_api->stricmp
137815#define sqlite3_uri_boolean sqlite3_api->uri_boolean
137816#define sqlite3_uri_int64 sqlite3_api->uri_int64
137817#define sqlite3_uri_parameter sqlite3_api->uri_parameter
137818#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
137819#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
137820/* Version 3.8.7 and later */
137821#define sqlite3_auto_extension sqlite3_api->auto_extension
137822#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
137823#define sqlite3_bind_text64 sqlite3_api->bind_text64
137824#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
137825#define sqlite3_load_extension sqlite3_api->load_extension
137826#define sqlite3_malloc64 sqlite3_api->malloc64
137827#define sqlite3_msize sqlite3_api->msize
137828#define sqlite3_realloc64 sqlite3_api->realloc64
137829#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
137830#define sqlite3_result_blob64 sqlite3_api->result_blob64
137831#define sqlite3_result_text64 sqlite3_api->result_text64
137832#define sqlite3_strglob sqlite3_api->strglob
137833/* Version 3.8.11 and later */
137834#define sqlite3_value_dup sqlite3_api->value_dup
137835#define sqlite3_value_free sqlite3_api->value_free
137836#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
137837#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
137838/* Version 3.9.0 and later */
137839#define sqlite3_value_subtype sqlite3_api->value_subtype
137840#define sqlite3_result_subtype sqlite3_api->result_subtype
137841/* Version 3.10.0 and later */
137842#define sqlite3_status64 sqlite3_api->status64
137843#define sqlite3_strlike sqlite3_api->strlike
137844#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
137845/* Version 3.12.0 and later */
137846#define sqlite3_system_errno sqlite3_api->system_errno
137847/* Version 3.14.0 and later */
137848#define sqlite3_trace_v2 sqlite3_api->trace_v2
137849#define sqlite3_expanded_sql sqlite3_api->expanded_sql
137850/* Version 3.18.0 and later */
137851#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
137852/* Version 3.20.0 and later */
137853#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
137854#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
137855#define sqlite3_bind_pointer sqlite3_api->bind_pointer
137856#define sqlite3_result_pointer sqlite3_api->result_pointer
137857#define sqlite3_value_pointer sqlite3_api->value_pointer
137858/* Version 3.22.0 and later */
137859#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
137860#define sqlite3_value_nochange sqlite3_api->value_nochange
137861#define sqlite3_vtab_collation sqlite3_api->vtab_collation
137862/* Version 3.24.0 and later */
137863#define sqlite3_keyword_count sqlite3_api->keyword_count
137864#define sqlite3_keyword_name sqlite3_api->keyword_name
137865#define sqlite3_keyword_check sqlite3_api->keyword_check
137866#define sqlite3_str_new sqlite3_api->str_new
137867#define sqlite3_str_finish sqlite3_api->str_finish
137868#define sqlite3_str_appendf sqlite3_api->str_appendf
137869#define sqlite3_str_vappendf sqlite3_api->str_vappendf
137870#define sqlite3_str_append sqlite3_api->str_append
137871#define sqlite3_str_appendall sqlite3_api->str_appendall
137872#define sqlite3_str_appendchar sqlite3_api->str_appendchar
137873#define sqlite3_str_reset sqlite3_api->str_reset
137874#define sqlite3_str_errcode sqlite3_api->str_errcode
137875#define sqlite3_str_length sqlite3_api->str_length
137876#define sqlite3_str_value sqlite3_api->str_value
137877/* Version 3.25.0 and later */
137878#define sqlite3_create_window_function sqlite3_api->create_window_function
137879/* Version 3.26.0 and later */
137880#define sqlite3_normalized_sql sqlite3_api->normalized_sql
137881/* Version 3.28.0 and later */
137882#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
137883#define sqlite3_value_frombind sqlite3_api->value_frombind
137884/* Version 3.30.0 and later */
137885#define sqlite3_drop_modules sqlite3_api->drop_modules
137886/* Version 3.31.0 and later */
137887#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
137888#define sqlite3_uri_key sqlite3_api->uri_key
137889#define sqlite3_filename_database sqlite3_api->filename_database
137890#define sqlite3_filename_journal sqlite3_api->filename_journal
137891#define sqlite3_filename_wal sqlite3_api->filename_wal
137892/* Version 3.32.0 and later */
137893#define sqlite3_create_filename sqlite3_api->create_filename
137894#define sqlite3_free_filename sqlite3_api->free_filename
137895#define sqlite3_database_file_object sqlite3_api->database_file_object
137896/* Version 3.34.0 and later */
137897#define sqlite3_txn_state sqlite3_api->txn_state
137898/* Version 3.36.1 and later */
137899#define sqlite3_changes64 sqlite3_api->changes64
137900#define sqlite3_total_changes64 sqlite3_api->total_changes64
137901/* Version 3.37.0 and later */
137902#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
137903/* Version 3.38.0 and later */
137904#define sqlite3_error_offset sqlite3_api->error_offset
137905#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
137906#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
137907#define sqlite3_vtab_in sqlite3_api->vtab_in
137908#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
137909#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
137910/* Version 3.39.0 and later */
137911#ifndef SQLITE_OMIT_DESERIALIZE
137912#define sqlite3_deserialize sqlite3_api->deserialize
137913#define sqlite3_serialize sqlite3_api->serialize
137914#endif
137915#define sqlite3_db_name sqlite3_api->db_name
137916/* Version 3.40.0 and later */
137917#define sqlite3_value_encoding sqlite3_api->value_encoding
137918/* Version 3.41.0 and later */
137919#define sqlite3_is_interrupted sqlite3_api->is_interrupted
137920/* Version 3.43.0 and later */
137921#define sqlite3_stmt_explain sqlite3_api->stmt_explain
137922/* Version 3.44.0 and later */
137923#define sqlite3_get_clientdata sqlite3_api->get_clientdata
137924#define sqlite3_set_clientdata sqlite3_api->set_clientdata
137925#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
137926
137927#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION1)
137928 /* This case when the file really is being compiled as a loadable
137929 ** extension */
137930# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
137931# define SQLITE_EXTENSION_INIT2(v)(void)v; sqlite3_api=v;
137932# define SQLITE_EXTENSION_INIT3 \
137933 extern const sqlite3_api_routines *sqlite3_api;
137934#else
137935 /* This case when the file is being statically linked into the
137936 ** application */
137937# define SQLITE_EXTENSION_INIT1 /*no-op*/
137938# define SQLITE_EXTENSION_INIT2(v)(void)v; (void)v; /* unused parameter */
137939# define SQLITE_EXTENSION_INIT3 /*no-op*/
137940#endif
137941
137942#endif /* SQLITE3EXT_H */
137943
137944/************** End of sqlite3ext.h ******************************************/
137945/************** Continuing where we left off in loadext.c ********************/
137946/* #include "sqliteInt.h" */
137947
137948#ifndef SQLITE_OMIT_LOAD_EXTENSION1
137949/*
137950** Some API routines are omitted when various features are
137951** excluded from a build of SQLite. Substitute a NULL pointer
137952** for any missing APIs.
137953*/
137954#ifndef SQLITE_ENABLE_COLUMN_METADATA1
137955# define sqlite3_column_database_name 0
137956# define sqlite3_column_database_name16 0
137957# define sqlite3_column_table_name 0
137958# define sqlite3_column_table_name16 0
137959# define sqlite3_column_origin_name 0
137960# define sqlite3_column_origin_name16 0
137961#endif
137962
137963#ifdef SQLITE_OMIT_AUTHORIZATION
137964# define sqlite3_set_authorizer 0
137965#endif
137966
137967#ifdef SQLITE_OMIT_UTF16
137968# define sqlite3_bind_text16 0
137969# define sqlite3_collation_needed16 0
137970# define sqlite3_column_decltype16 0
137971# define sqlite3_column_name16 0
137972# define sqlite3_column_text16 0
137973# define sqlite3_complete16 0
137974# define sqlite3_create_collation16 0
137975# define sqlite3_create_function16 0
137976# define sqlite3_errmsg16 0
137977# define sqlite3_open16 0
137978# define sqlite3_prepare16 0
137979# define sqlite3_prepare16_v2 0
137980# define sqlite3_prepare16_v3 0
137981# define sqlite3_result_error16 0
137982# define sqlite3_result_text16 0
137983# define sqlite3_result_text16be 0
137984# define sqlite3_result_text16le 0
137985# define sqlite3_value_text16 0
137986# define sqlite3_value_text16be 0
137987# define sqlite3_value_text16le 0
137988# define sqlite3_column_database_name16 0
137989# define sqlite3_column_table_name16 0
137990# define sqlite3_column_origin_name16 0
137991#endif
137992
137993#ifdef SQLITE_OMIT_COMPLETE
137994# define sqlite3_complete 0
137995# define sqlite3_complete16 0
137996#endif
137997
137998#ifdef SQLITE_OMIT_DECLTYPE
137999# define sqlite3_column_decltype16 0
138000# define sqlite3_column_decltype 0
138001#endif
138002
138003#ifdef SQLITE_OMIT_PROGRESS_CALLBACK1
138004# define sqlite3_progress_handler 0
138005#endif
138006
138007#ifdef SQLITE_OMIT_VIRTUALTABLE
138008# define sqlite3_create_module 0
138009# define sqlite3_create_module_v2 0
138010# define sqlite3_declare_vtab 0
138011# define sqlite3_vtab_config 0
138012# define sqlite3_vtab_on_conflict 0
138013# define sqlite3_vtab_collation 0
138014#endif
138015
138016#ifdef SQLITE_OMIT_SHARED_CACHE
138017# define sqlite3_enable_shared_cache 0
138018#endif
138019
138020#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED1)
138021# define sqlite3_profile 0
138022# define sqlite3_trace 0
138023#endif
138024
138025#ifdef SQLITE_OMIT_GET_TABLE
138026# define sqlite3_free_table 0
138027# define sqlite3_get_table 0
138028#endif
138029
138030#ifdef SQLITE_OMIT_INCRBLOB
138031#define sqlite3_bind_zeroblob 0
138032#define sqlite3_blob_bytes 0
138033#define sqlite3_blob_close 0
138034#define sqlite3_blob_open 0
138035#define sqlite3_blob_read 0
138036#define sqlite3_blob_write 0
138037#define sqlite3_blob_reopen 0
138038#endif
138039
138040#if defined(SQLITE_OMIT_TRACE)
138041# define sqlite3_trace_v2 0
138042#endif
138043
138044/*
138045** The following structure contains pointers to all SQLite API routines.
138046** A pointer to this structure is passed into extensions when they are
138047** loaded so that the extension can make calls back into the SQLite
138048** library.
138049**
138050** When adding new APIs, add them to the bottom of this structure
138051** in order to preserve backwards compatibility.
138052**
138053** Extensions that use newer APIs should first call the
138054** sqlite3_libversion_number() to make sure that the API they
138055** intend to use is supported by the library. Extensions should
138056** also check to make sure that the pointer to the function is
138057** not NULL before calling it.
138058*/
138059static const sqlite3_api_routines sqlite3Apis = {
138060 sqlite3_aggregate_context,
138061#ifndef SQLITE_OMIT_DEPRECATED1
138062 sqlite3_aggregate_count,
138063#else
138064 0,
138065#endif
138066 sqlite3_bind_blob,
138067 sqlite3_bind_double,
138068 sqlite3_bind_int,
138069 sqlite3_bind_int64,
138070 sqlite3_bind_null,
138071 sqlite3_bind_parameter_count,
138072 sqlite3_bind_parameter_index,
138073 sqlite3_bind_parameter_name,
138074 sqlite3_bind_text,
138075 sqlite3_bind_text16,
138076 sqlite3_bind_value,
138077 sqlite3_busy_handler,
138078 sqlite3_busy_timeout,
138079 sqlite3_changes,
138080 sqlite3_close,
138081 sqlite3_collation_needed,
138082 sqlite3_collation_needed16,
138083 sqlite3_column_blob,
138084 sqlite3_column_bytes,
138085 sqlite3_column_bytes16,
138086 sqlite3_column_count,
138087 sqlite3_column_database_name,
138088 sqlite3_column_database_name16,
138089 sqlite3_column_decltype,
138090 sqlite3_column_decltype16,
138091 sqlite3_column_double,
138092 sqlite3_column_int,
138093 sqlite3_column_int64,
138094 sqlite3_column_name,
138095 sqlite3_column_name16,
138096 sqlite3_column_origin_name,
138097 sqlite3_column_origin_name16,
138098 sqlite3_column_table_name,
138099 sqlite3_column_table_name16,
138100 sqlite3_column_text,
138101 sqlite3_column_text16,
138102 sqlite3_column_type,
138103 sqlite3_column_value,
138104 sqlite3_commit_hook,
138105 sqlite3_complete,
138106 sqlite3_complete16,
138107 sqlite3_create_collation,
138108 sqlite3_create_collation16,
138109 sqlite3_create_function,
138110 sqlite3_create_function16,
138111 sqlite3_create_module,
138112 sqlite3_data_count,
138113 sqlite3_db_handle,
138114 sqlite3_declare_vtab,
138115 sqlite3_enable_shared_cache,
138116 sqlite3_errcode,
138117 sqlite3_errmsg,
138118 sqlite3_errmsg16,
138119 sqlite3_exec,
138120#ifndef SQLITE_OMIT_DEPRECATED1
138121 sqlite3_expired,
138122#else
138123 0,
138124#endif
138125 sqlite3_finalize,
138126 sqlite3_free,
138127 sqlite3_free_table,
138128 sqlite3_get_autocommit,
138129 sqlite3_get_auxdata,
138130 sqlite3_get_table,
138131 0, /* Was sqlite3_global_recover(), but that function is deprecated */
138132 sqlite3_interrupt,
138133 sqlite3_last_insert_rowid,
138134 sqlite3_libversion,
138135 sqlite3_libversion_number,
138136 sqlite3_malloc,
138137 sqlite3_mprintf,
138138 sqlite3_open,
138139 sqlite3_open16,
138140 sqlite3_prepare,
138141 sqlite3_prepare16,
138142 sqlite3_profile,
138143 sqlite3_progress_handler,
138144 sqlite3_realloc,
138145 sqlite3_reset,
138146 sqlite3_result_blob,
138147 sqlite3_result_double,
138148 sqlite3_result_error,
138149 sqlite3_result_error16,
138150 sqlite3_result_int,
138151 sqlite3_result_int64,
138152 sqlite3_result_null,
138153 sqlite3_result_text,
138154 sqlite3_result_text16,
138155 sqlite3_result_text16be,
138156 sqlite3_result_text16le,
138157 sqlite3_result_value,
138158 sqlite3_rollback_hook,
138159 sqlite3_set_authorizer,
138160 sqlite3_set_auxdata,
138161 sqlite3_snprintf,
138162 sqlite3_step,
138163 sqlite3_table_column_metadata,
138164#ifndef SQLITE_OMIT_DEPRECATED1
138165 sqlite3_thread_cleanup,
138166#else
138167 0,
138168#endif
138169 sqlite3_total_changes,
138170 sqlite3_trace,
138171#ifndef SQLITE_OMIT_DEPRECATED1
138172 sqlite3_transfer_bindings,
138173#else
138174 0,
138175#endif
138176 sqlite3_update_hook,
138177 sqlite3_user_data,
138178 sqlite3_value_blob,
138179 sqlite3_value_bytes,
138180 sqlite3_value_bytes16,
138181 sqlite3_value_double,
138182 sqlite3_value_int,
138183 sqlite3_value_int64,
138184 sqlite3_value_numeric_type,
138185 sqlite3_value_text,
138186 sqlite3_value_text16,
138187 sqlite3_value_text16be,
138188 sqlite3_value_text16le,
138189 sqlite3_value_type,
138190 sqlite3_vmprintf,
138191 /*
138192 ** The original API set ends here. All extensions can call any
138193 ** of the APIs above provided that the pointer is not NULL. But
138194 ** before calling APIs that follow, extension should check the
138195 ** sqlite3_libversion_number() to make sure they are dealing with
138196 ** a library that is new enough to support that API.
138197 *************************************************************************
138198 */
138199 sqlite3_overload_function,
138200
138201 /*
138202 ** Added after 3.3.13
138203 */
138204 sqlite3_prepare_v2,
138205 sqlite3_prepare16_v2,
138206 sqlite3_clear_bindings,
138207
138208 /*
138209 ** Added for 3.4.1
138210 */
138211 sqlite3_create_module_v2,
138212
138213 /*
138214 ** Added for 3.5.0
138215 */
138216 sqlite3_bind_zeroblob,
138217 sqlite3_blob_bytes,
138218 sqlite3_blob_close,
138219 sqlite3_blob_open,
138220 sqlite3_blob_read,
138221 sqlite3_blob_write,
138222 sqlite3_create_collation_v2,
138223 sqlite3_file_control,
138224 sqlite3_memory_highwater,
138225 sqlite3_memory_used,
138226#ifdef SQLITE_MUTEX_OMIT
138227 0,
138228 0,
138229 0,
138230 0,
138231 0,
138232#else
138233 sqlite3_mutex_alloc,
138234 sqlite3_mutex_enter,
138235 sqlite3_mutex_free,
138236 sqlite3_mutex_leave,
138237 sqlite3_mutex_try,
138238#endif
138239 sqlite3_open_v2,
138240 sqlite3_release_memory,
138241 sqlite3_result_error_nomem,
138242 sqlite3_result_error_toobig,
138243 sqlite3_sleep,
138244 sqlite3_soft_heap_limit,
138245 sqlite3_vfs_find,
138246 sqlite3_vfs_register,
138247 sqlite3_vfs_unregister,
138248
138249 /*
138250 ** Added for 3.5.8
138251 */
138252 sqlite3_threadsafe,
138253 sqlite3_result_zeroblob,
138254 sqlite3_result_error_code,
138255 sqlite3_test_control,
138256 sqlite3_randomness,
138257 sqlite3_context_db_handle,
138258
138259 /*
138260 ** Added for 3.6.0
138261 */
138262 sqlite3_extended_result_codes,
138263 sqlite3_limit,
138264 sqlite3_next_stmt,
138265 sqlite3_sql,
138266 sqlite3_status,
138267
138268 /*
138269 ** Added for 3.7.4
138270 */
138271 sqlite3_backup_finish,
138272 sqlite3_backup_init,
138273 sqlite3_backup_pagecount,
138274 sqlite3_backup_remaining,
138275 sqlite3_backup_step,
138276#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
138277 sqlite3_compileoption_get,
138278 sqlite3_compileoption_used,
138279#else
138280 0,
138281 0,
138282#endif
138283 sqlite3_create_function_v2,
138284 sqlite3_db_config,
138285 sqlite3_db_mutex,
138286 sqlite3_db_status,
138287 sqlite3_extended_errcode,
138288 sqlite3_log,
138289 sqlite3_soft_heap_limit64,
138290 sqlite3_sourceid,
138291 sqlite3_stmt_status,
138292 sqlite3_strnicmp,
138293#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
138294 sqlite3_unlock_notify,
138295#else
138296 0,
138297#endif
138298#ifndef SQLITE_OMIT_WAL
138299 sqlite3_wal_autocheckpoint,
138300 sqlite3_wal_checkpoint,
138301 sqlite3_wal_hook,
138302#else
138303 0,
138304 0,
138305 0,
138306#endif
138307 sqlite3_blob_reopen,
138308 sqlite3_vtab_config,
138309 sqlite3_vtab_on_conflict,
138310 sqlite3_close_v2,
138311 sqlite3_db_filename,
138312 sqlite3_db_readonly,
138313 sqlite3_db_release_memory,
138314 sqlite3_errstr,
138315 sqlite3_stmt_busy,
138316 sqlite3_stmt_readonly,
138317 sqlite3_stricmp,
138318 sqlite3_uri_boolean,
138319 sqlite3_uri_int64,
138320 sqlite3_uri_parameter,
138321 sqlite3_vsnprintf,
138322 sqlite3_wal_checkpoint_v2,
138323 /* Version 3.8.7 and later */
138324 sqlite3_auto_extension,
138325 sqlite3_bind_blob64,
138326 sqlite3_bind_text64,
138327 sqlite3_cancel_auto_extension,
138328 sqlite3_load_extension,
138329 sqlite3_malloc64,
138330 sqlite3_msize,
138331 sqlite3_realloc64,
138332 sqlite3_reset_auto_extension,
138333 sqlite3_result_blob64,
138334 sqlite3_result_text64,
138335 sqlite3_strglob,
138336 /* Version 3.8.11 and later */
138337 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
138338 sqlite3_value_free,
138339 sqlite3_result_zeroblob64,
138340 sqlite3_bind_zeroblob64,
138341 /* Version 3.9.0 and later */
138342 sqlite3_value_subtype,
138343 sqlite3_result_subtype,
138344 /* Version 3.10.0 and later */
138345 sqlite3_status64,
138346 sqlite3_strlike,
138347 sqlite3_db_cacheflush,
138348 /* Version 3.12.0 and later */
138349 sqlite3_system_errno,
138350 /* Version 3.14.0 and later */
138351 sqlite3_trace_v2,
138352 sqlite3_expanded_sql,
138353 /* Version 3.18.0 and later */
138354 sqlite3_set_last_insert_rowid,
138355 /* Version 3.20.0 and later */
138356 sqlite3_prepare_v3,
138357 sqlite3_prepare16_v3,
138358 sqlite3_bind_pointer,
138359 sqlite3_result_pointer,
138360 sqlite3_value_pointer,
138361 /* Version 3.22.0 and later */
138362 sqlite3_vtab_nochange,
138363 sqlite3_value_nochange,
138364 sqlite3_vtab_collation,
138365 /* Version 3.24.0 and later */
138366 sqlite3_keyword_count,
138367 sqlite3_keyword_name,
138368 sqlite3_keyword_check,
138369 sqlite3_str_new,
138370 sqlite3_str_finish,
138371 sqlite3_str_appendf,
138372 sqlite3_str_vappendf,
138373 sqlite3_str_append,
138374 sqlite3_str_appendall,
138375 sqlite3_str_appendchar,
138376 sqlite3_str_reset,
138377 sqlite3_str_errcode,
138378 sqlite3_str_length,
138379 sqlite3_str_value,
138380 /* Version 3.25.0 and later */
138381 sqlite3_create_window_function,
138382 /* Version 3.26.0 and later */
138383#ifdef SQLITE_ENABLE_NORMALIZE
138384 sqlite3_normalized_sql,
138385#else
138386 0,
138387#endif
138388 /* Version 3.28.0 and later */
138389 sqlite3_stmt_isexplain,
138390 sqlite3_value_frombind,
138391 /* Version 3.30.0 and later */
138392#ifndef SQLITE_OMIT_VIRTUALTABLE
138393 sqlite3_drop_modules,
138394#else
138395 0,
138396#endif
138397 /* Version 3.31.0 and later */
138398 sqlite3_hard_heap_limit64,
138399 sqlite3_uri_key,
138400 sqlite3_filename_database,
138401 sqlite3_filename_journal,
138402 sqlite3_filename_wal,
138403 /* Version 3.32.0 and later */
138404 sqlite3_create_filename,
138405 sqlite3_free_filename,
138406 sqlite3_database_file_object,
138407 /* Version 3.34.0 and later */
138408 sqlite3_txn_state,
138409 /* Version 3.36.1 and later */
138410 sqlite3_changes64,
138411 sqlite3_total_changes64,
138412 /* Version 3.37.0 and later */
138413 sqlite3_autovacuum_pages,
138414 /* Version 3.38.0 and later */
138415 sqlite3_error_offset,
138416#ifndef SQLITE_OMIT_VIRTUALTABLE
138417 sqlite3_vtab_rhs_value,
138418 sqlite3_vtab_distinct,
138419 sqlite3_vtab_in,
138420 sqlite3_vtab_in_first,
138421 sqlite3_vtab_in_next,
138422#else
138423 0,
138424 0,
138425 0,
138426 0,
138427 0,
138428#endif
138429 /* Version 3.39.0 and later */
138430#ifndef SQLITE_OMIT_DESERIALIZE
138431 sqlite3_deserialize,
138432 sqlite3_serialize,
138433#else
138434 0,
138435 0,
138436#endif
138437 sqlite3_db_name,
138438 /* Version 3.40.0 and later */
138439 sqlite3_value_encoding,
138440 /* Version 3.41.0 and later */
138441 sqlite3_is_interrupted,
138442 /* Version 3.43.0 and later */
138443 sqlite3_stmt_explain,
138444 /* Version 3.44.0 and later */
138445 sqlite3_get_clientdata,
138446 sqlite3_set_clientdata
138447};
138448
138449/* True if x is the directory separator character
138450*/
138451#if SQLITE_OS_WIN0
138452# define DirSep(X) ((X)=='/'||(X)=='\\')
138453#else
138454# define DirSep(X) ((X)=='/')
138455#endif
138456
138457/*
138458** Attempt to load an SQLite extension library contained in the file
138459** zFile. The entry point is zProc. zProc may be 0 in which case a
138460** default entry point name (sqlite3_extension_init) is used. Use
138461** of the default name is recommended.
138462**
138463** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
138464**
138465** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
138466** error message text. The calling function should free this memory
138467** by calling sqlite3DbFree(db, ).
138468*/
138469static int sqlite3LoadExtension(
138470 sqlite3 *db, /* Load the extension into this database connection */
138471 const char *zFile, /* Name of the shared library containing extension */
138472 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
138473 char **pzErrMsg /* Put error message here if not 0 */
138474){
138475 sqlite3_vfs *pVfs = db->pVfs;
138476 void *handle;
138477 sqlite3_loadext_entry xInit;
138478 char *zErrmsg = 0;
138479 const char *zEntry;
138480 char *zAltEntry = 0;
138481 void **aHandle;
138482 u64 nMsg = strlen(zFile);
138483 int ii;
138484 int rc;
138485
138486 /* Shared library endings to try if zFile cannot be loaded as written */
138487 static const char *azEndings[] = {
138488#if SQLITE_OS_WIN0
138489 "dll"
138490#elif defined(__APPLE__)
138491 "dylib"
138492#else
138493 "so"
138494#endif
138495 };
138496
138497
138498 if( pzErrMsg ) *pzErrMsg = 0;
138499
138500 /* Ticket #1863. To avoid a creating security problems for older
138501 ** applications that relink against newer versions of SQLite, the
138502 ** ability to run load_extension is turned off by default. One
138503 ** must call either sqlite3_enable_load_extension(db) or
138504 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
138505 ** to turn on extension loading.
138506 */
138507 if( (db->flags & SQLITE_LoadExtension0x00010000)==0 ){
138508 if( pzErrMsg ){
138509 *pzErrMsg = sqlite3_mprintf("not authorized");
138510 }
138511 return SQLITE_ERROR1;
138512 }
138513
138514 zEntry = zProc ? zProc : "sqlite3_extension_init";
138515
138516 /* tag-20210611-1. Some dlopen() implementations will segfault if given
138517 ** an oversize filename. Most filesystems have a pathname limit of 4K,
138518 ** so limit the extension filename length to about twice that.
138519 ** https://sqlite.org/forum/forumpost/08a0d6d9bf
138520 **
138521 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
138522 ** See https://sqlite.org/forum/forumpost/24083b579d.
138523 */
138524 if( nMsg>SQLITE_MAX_PATHLEN4096 ) goto extension_not_found;
138525
138526 /* Do not allow sqlite3_load_extension() to link to a copy of the
138527 ** running application, by passing in an empty filename. */
138528 if( nMsg==0 ) goto extension_not_found;
138529
138530 handle = sqlite3OsDlOpen(pVfs, zFile);
138531#if SQLITE_OS_UNIX1 || SQLITE_OS_WIN0
138532 for(ii=0; ii<ArraySize(azEndings)((int)(sizeof(azEndings)/sizeof(azEndings[0]))) && handle==0; ii++){
138533 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
138534 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT7;
138535 if( nMsg+strlen(azEndings[ii])+1<=SQLITE_MAX_PATHLEN4096 ){
138536 handle = sqlite3OsDlOpen(pVfs, zAltFile);
138537 }
138538 sqlite3_free(zAltFile);
138539 }
138540#endif
138541 if( handle==0 ) goto extension_not_found;
138542 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
138543
138544 /* If no entry point was specified and the default legacy
138545 ** entry point name "sqlite3_extension_init" was not found, then
138546 ** construct an entry point name "sqlite3_X_init" where the X is
138547 ** replaced by the lowercase value of every ASCII alphabetic
138548 ** character in the filename after the last "/" upto the first ".",
138549 ** and eliding the first three characters if they are "lib".
138550 ** Examples:
138551 **
138552 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
138553 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
138554 */
138555 if( xInit==0 && zProc==0 ){
138556 int iFile, iEntry, c;
138557 int ncFile = sqlite3Strlen30(zFile);
138558 zAltEntry = sqlite3_malloc64(ncFile+30);
138559 if( zAltEntry==0 ){
138560 sqlite3OsDlClose(pVfs, handle);
138561 return SQLITE_NOMEM_BKPT7;
138562 }
138563 memcpy(zAltEntry, "sqlite3_", 8);
138564 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
138565 iFile++;
138566 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
138567 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
138568 if( sqlite3Isalpha(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x02) ){
138569 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
138570 }
138571 }
138572 memcpy(zAltEntry+iEntry, "_init", 6);
138573 zEntry = zAltEntry;
138574 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
138575 }
138576 if( xInit==0 ){
138577 if( pzErrMsg ){
138578 nMsg += strlen(zEntry) + 300;
138579 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
138580 if( zErrmsg ){
138581 assert( nMsg<0x7fffffff )((void) (0)); /* zErrmsg would be NULL if not so */
138582 sqlite3_snprintf((int)nMsg, zErrmsg,
138583 "no entry point [%s] in shared library [%s]", zEntry, zFile);
138584 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
138585 }
138586 }
138587 sqlite3OsDlClose(pVfs, handle);
138588 sqlite3_free(zAltEntry);
138589 return SQLITE_ERROR1;
138590 }
138591 sqlite3_free(zAltEntry);
138592 rc = xInit(db, &zErrmsg, &sqlite3Apis);
138593 if( rc ){
138594 if( rc==SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) ) return SQLITE_OK0;
138595 if( pzErrMsg ){
138596 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
138597 }
138598 sqlite3_free(zErrmsg);
138599 sqlite3OsDlClose(pVfs, handle);
138600 return SQLITE_ERROR1;
138601 }
138602
138603 /* Append the new shared library handle to the db->aExtension array. */
138604 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
138605 if( aHandle==0 ){
138606 return SQLITE_NOMEM_BKPT7;
138607 }
138608 if( db->nExtension>0 ){
138609 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
138610 }
138611 sqlite3DbFree(db, db->aExtension);
138612 db->aExtension = aHandle;
138613
138614 db->aExtension[db->nExtension++] = handle;
138615 return SQLITE_OK0;
138616
138617extension_not_found:
138618 if( pzErrMsg ){
138619 nMsg += 300;
138620 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
138621 if( zErrmsg ){
138622 assert( nMsg<0x7fffffff )((void) (0)); /* zErrmsg would be NULL if not so */
138623 sqlite3_snprintf((int)nMsg, zErrmsg,
138624 "unable to open shared library [%.*s]", SQLITE_MAX_PATHLEN4096, zFile);
138625 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
138626 }
138627 }
138628 return SQLITE_ERROR1;
138629}
138630SQLITE_API int sqlite3_load_extension(
138631 sqlite3 *db, /* Load the extension into this database connection */
138632 const char *zFile, /* Name of the shared library containing extension */
138633 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
138634 char **pzErrMsg /* Put error message here if not 0 */
138635){
138636 int rc;
138637 sqlite3_mutex_enter(db->mutex);
138638 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
138639 rc = sqlite3ApiExit(db, rc);
138640 sqlite3_mutex_leave(db->mutex);
138641 return rc;
138642}
138643
138644/*
138645** Call this routine when the database connection is closing in order
138646** to clean up loaded extensions
138647*/
138648SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3 *db){
138649 int i;
138650 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
138651 for(i=0; i<db->nExtension; i++){
138652 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
138653 }
138654 sqlite3DbFree(db, db->aExtension);
138655}
138656
138657/*
138658** Enable or disable extension loading. Extension loading is disabled by
138659** default so as not to open security holes in older applications.
138660*/
138661SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
138662#ifdef SQLITE_ENABLE_API_ARMOR1
138663 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(138663);
138664#endif
138665 sqlite3_mutex_enter(db->mutex);
138666 if( onoff ){
138667 db->flags |= SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000;
138668 }else{
138669 db->flags &= ~(u64)(SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000);
138670 }
138671 sqlite3_mutex_leave(db->mutex);
138672 return SQLITE_OK0;
138673}
138674
138675#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
138676
138677/*
138678** The following object holds the list of automatically loaded
138679** extensions.
138680**
138681** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
138682** mutex must be held while accessing this list.
138683*/
138684typedef struct sqlite3AutoExtList sqlite3AutoExtList;
138685static SQLITE_WSD struct sqlite3AutoExtList {
138686 u32 nExt; /* Number of entries in aExt[] */
138687 void (**aExt)(void); /* Pointers to the extension init functions */
138688} sqlite3Autoext = { 0, 0 };
138689
138690/* The "wsdAutoext" macro will resolve to the autoextension
138691** state vector. If writable static data is unsupported on the target,
138692** we have to locate the state vector at run-time. In the more common
138693** case where writable static data is supported, wsdStat can refer directly
138694** to the "sqlite3Autoext" state vector declared above.
138695*/
138696#ifdef SQLITE_OMIT_WSD
138697# define wsdAutoextInit \
138698 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)sqlite3Autoext
138699# define wsdAutoextsqlite3Autoext x[0]
138700#else
138701# define wsdAutoextInit
138702# define wsdAutoextsqlite3Autoext sqlite3Autoext
138703#endif
138704
138705
138706/*
138707** Register a statically linked extension that is automatically
138708** loaded by every new database connection.
138709*/
138710SQLITE_API int sqlite3_auto_extension(
138711 void (*xInit)(void)
138712){
138713 int rc = SQLITE_OK0;
138714#ifdef SQLITE_ENABLE_API_ARMOR1
138715 if( xInit==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(138715);
138716#endif
138717#ifndef SQLITE_OMIT_AUTOINIT
138718 rc = sqlite3_initialize();
138719 if( rc ){
138720 return rc;
138721 }else
138722#endif
138723 {
138724 u32 i;
138725#if SQLITE_THREADSAFE2
138726 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
138727#endif
138728 wsdAutoextInit;
138729 sqlite3_mutex_enter(mutex);
138730 for(i=0; i<wsdAutoextsqlite3Autoext.nExt; i++){
138731 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ) break;
138732 }
138733 if( i==wsdAutoextsqlite3Autoext.nExt ){
138734 u64 nByte = (wsdAutoextsqlite3Autoext.nExt+1)*sizeof(wsdAutoextsqlite3Autoext.aExt[0]);
138735 void (**aNew)(void);
138736 aNew = sqlite3_realloc64(wsdAutoextsqlite3Autoext.aExt, nByte);
138737 if( aNew==0 ){
138738 rc = SQLITE_NOMEM_BKPT7;
138739 }else{
138740 wsdAutoextsqlite3Autoext.aExt = aNew;
138741 wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt] = xInit;
138742 wsdAutoextsqlite3Autoext.nExt++;
138743 }
138744 }
138745 sqlite3_mutex_leave(mutex);
138746 assert( (rc&0xff)==rc )((void) (0));
138747 return rc;
138748 }
138749}
138750
138751/*
138752** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
138753** set of routines that is invoked for each new database connection, if it
138754** is currently on the list. If xInit is not on the list, then this
138755** routine is a no-op.
138756**
138757** Return 1 if xInit was found on the list and removed. Return 0 if xInit
138758** was not on the list.
138759*/
138760SQLITE_API int sqlite3_cancel_auto_extension(
138761 void (*xInit)(void)
138762){
138763#if SQLITE_THREADSAFE2
138764 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
138765#endif
138766 int i;
138767 int n = 0;
138768 wsdAutoextInit;
138769#ifdef SQLITE_ENABLE_API_ARMOR1
138770 if( xInit==0 ) return 0;
138771#endif
138772 sqlite3_mutex_enter(mutex);
138773 for(i=(int)wsdAutoextsqlite3Autoext.nExt-1; i>=0; i--){
138774 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ){
138775 wsdAutoextsqlite3Autoext.nExt--;
138776 wsdAutoextsqlite3Autoext.aExt[i] = wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt];
138777 n++;
138778 break;
138779 }
138780 }
138781 sqlite3_mutex_leave(mutex);
138782 return n;
138783}
138784
138785/*
138786** Reset the automatic extension loading mechanism.
138787*/
138788SQLITE_API void sqlite3_reset_auto_extension(void){
138789#ifndef SQLITE_OMIT_AUTOINIT
138790 if( sqlite3_initialize()==SQLITE_OK0 )
138791#endif
138792 {
138793#if SQLITE_THREADSAFE2
138794 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
138795#endif
138796 wsdAutoextInit;
138797 sqlite3_mutex_enter(mutex);
138798 sqlite3_free(wsdAutoextsqlite3Autoext.aExt);
138799 wsdAutoextsqlite3Autoext.aExt = 0;
138800 wsdAutoextsqlite3Autoext.nExt = 0;
138801 sqlite3_mutex_leave(mutex);
138802 }
138803}
138804
138805/*
138806** Load all automatic extensions.
138807**
138808** If anything goes wrong, set an error in the database connection.
138809*/
138810SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3 *db){
138811 u32 i;
138812 int go = 1;
138813 int rc;
138814 sqlite3_loadext_entry xInit;
138815
138816 wsdAutoextInit;
138817 if( wsdAutoextsqlite3Autoext.nExt==0 ){
138818 /* Common case: early out without every having to acquire a mutex */
138819 return;
138820 }
138821 for(i=0; go; i++){
138822 char *zErrmsg;
138823#if SQLITE_THREADSAFE2
138824 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2);
138825#endif
138826#ifdef SQLITE_OMIT_LOAD_EXTENSION1
138827 const sqlite3_api_routines *pThunk = 0;
138828#else
138829 const sqlite3_api_routines *pThunk = &sqlite3Apis;
138830#endif
138831 sqlite3_mutex_enter(mutex);
138832 if( i>=wsdAutoextsqlite3Autoext.nExt ){
138833 xInit = 0;
138834 go = 0;
138835 }else{
138836 xInit = (sqlite3_loadext_entry)wsdAutoextsqlite3Autoext.aExt[i];
138837 }
138838 sqlite3_mutex_leave(mutex);
138839 zErrmsg = 0;
138840 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
138841 sqlite3ErrorWithMsg(db, rc,
138842 "automatic extension loading failed: %s", zErrmsg);
138843 go = 0;
138844 }
138845 sqlite3_free(zErrmsg);
138846 }
138847}
138848
138849/************** End of loadext.c *********************************************/
138850/************** Begin file pragma.c ******************************************/
138851/*
138852** 2003 April 6
138853**
138854** The author disclaims copyright to this source code. In place of
138855** a legal notice, here is a blessing:
138856**
138857** May you do good and not evil.
138858** May you find forgiveness for yourself and forgive others.
138859** May you share freely, never taking more than you give.
138860**
138861*************************************************************************
138862** This file contains code used to implement the PRAGMA command.
138863*/
138864/* #include "sqliteInt.h" */
138865
138866#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
138867# if defined(__APPLE__)
138868# define SQLITE_ENABLE_LOCKING_STYLE0 1
138869# else
138870# define SQLITE_ENABLE_LOCKING_STYLE0 0
138871# endif
138872#endif
138873
138874/***************************************************************************
138875** The "pragma.h" include file is an automatically generated file that
138876** that includes the PragType_XXXX macro definitions and the aPragmaName[]
138877** object. This ensures that the aPragmaName[] table is arranged in
138878** lexicographical order to facility a binary search of the pragma name.
138879** Do not edit pragma.h directly. Edit and rerun the script in at
138880** ../tool/mkpragmatab.tcl. */
138881/************** Include pragma.h in the middle of pragma.c *******************/
138882/************** Begin file pragma.h ******************************************/
138883/* DO NOT EDIT!
138884** This file is automatically generated by the script at
138885** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
138886** that script and rerun it.
138887*/
138888
138889/* The various pragma types */
138890#define PragTyp_ACTIVATE_EXTENSIONS0 0
138891#define PragTyp_ANALYSIS_LIMIT1 1
138892#define PragTyp_HEADER_VALUE2 2
138893#define PragTyp_AUTO_VACUUM3 3
138894#define PragTyp_FLAG4 4
138895#define PragTyp_BUSY_TIMEOUT5 5
138896#define PragTyp_CACHE_SIZE6 6
138897#define PragTyp_CACHE_SPILL7 7
138898#define PragTyp_CASE_SENSITIVE_LIKE8 8
138899#define PragTyp_COLLATION_LIST9 9
138900#define PragTyp_COMPILE_OPTIONS10 10
138901#define PragTyp_DATA_STORE_DIRECTORY11 11
138902#define PragTyp_DATABASE_LIST12 12
138903#define PragTyp_DEFAULT_CACHE_SIZE13 13
138904#define PragTyp_ENCODING14 14
138905#define PragTyp_FOREIGN_KEY_CHECK15 15
138906#define PragTyp_FOREIGN_KEY_LIST16 16
138907#define PragTyp_FUNCTION_LIST17 17
138908#define PragTyp_HARD_HEAP_LIMIT18 18
138909#define PragTyp_INCREMENTAL_VACUUM19 19
138910#define PragTyp_INDEX_INFO20 20
138911#define PragTyp_INDEX_LIST21 21
138912#define PragTyp_INTEGRITY_CHECK22 22
138913#define PragTyp_JOURNAL_MODE23 23
138914#define PragTyp_JOURNAL_SIZE_LIMIT24 24
138915#define PragTyp_LOCK_PROXY_FILE25 25
138916#define PragTyp_LOCKING_MODE26 26
138917#define PragTyp_PAGE_COUNT27 27
138918#define PragTyp_MMAP_SIZE28 28
138919#define PragTyp_MODULE_LIST29 29
138920#define PragTyp_OPTIMIZE30 30
138921#define PragTyp_PAGE_SIZE31 31
138922#define PragTyp_PRAGMA_LIST32 32
138923#define PragTyp_SECURE_DELETE33 33
138924#define PragTyp_SHRINK_MEMORY34 34
138925#define PragTyp_SOFT_HEAP_LIMIT35 35
138926#define PragTyp_SYNCHRONOUS36 36
138927#define PragTyp_TABLE_INFO37 37
138928#define PragTyp_TABLE_LIST38 38
138929#define PragTyp_TEMP_STORE39 39
138930#define PragTyp_TEMP_STORE_DIRECTORY40 40
138931#define PragTyp_THREADS41 41
138932#define PragTyp_WAL_AUTOCHECKPOINT42 42
138933#define PragTyp_WAL_CHECKPOINT43 43
138934#define PragTyp_LOCK_STATUS44 44
138935#define PragTyp_STATS45 45
138936
138937/* Property flags associated with various pragma. */
138938#define PragFlg_NeedSchema0x01 0x01 /* Force schema load before running */
138939#define PragFlg_NoColumns0x02 0x02 /* OP_ResultRow called with zero columns */
138940#define PragFlg_NoColumns10x04 0x04 /* zero columns if RHS argument is present */
138941#define PragFlg_ReadOnly0x08 0x08 /* Read-only HEADER_VALUE */
138942#define PragFlg_Result00x10 0x10 /* Acts as query when no argument */
138943#define PragFlg_Result10x20 0x20 /* Acts as query when has one argument */
138944#define PragFlg_SchemaOpt0x40 0x40 /* Schema restricts name search if present */
138945#define PragFlg_SchemaReq0x80 0x80 /* Schema required - "main" is default */
138946
138947/* Names of columns for pragmas that return multi-column result
138948** or that return single-column results where the name of the
138949** result column is different from the name of the pragma
138950*/
138951static const char *const pragCName[] = {
138952 /* 0 */ "id", /* Used by: foreign_key_list */
138953 /* 1 */ "seq",
138954 /* 2 */ "table",
138955 /* 3 */ "from",
138956 /* 4 */ "to",
138957 /* 5 */ "on_update",
138958 /* 6 */ "on_delete",
138959 /* 7 */ "match",
138960 /* 8 */ "cid", /* Used by: table_xinfo */
138961 /* 9 */ "name",
138962 /* 10 */ "type",
138963 /* 11 */ "notnull",
138964 /* 12 */ "dflt_value",
138965 /* 13 */ "pk",
138966 /* 14 */ "hidden",
138967 /* table_info reuses 8 */
138968 /* 15 */ "schema", /* Used by: table_list */
138969 /* 16 */ "name",
138970 /* 17 */ "type",
138971 /* 18 */ "ncol",
138972 /* 19 */ "wr",
138973 /* 20 */ "strict",
138974 /* 21 */ "seqno", /* Used by: index_xinfo */
138975 /* 22 */ "cid",
138976 /* 23 */ "name",
138977 /* 24 */ "desc",
138978 /* 25 */ "coll",
138979 /* 26 */ "key",
138980 /* 27 */ "name", /* Used by: function_list */
138981 /* 28 */ "builtin",
138982 /* 29 */ "type",
138983 /* 30 */ "enc",
138984 /* 31 */ "narg",
138985 /* 32 */ "flags",
138986 /* 33 */ "tbl", /* Used by: stats */
138987 /* 34 */ "idx",
138988 /* 35 */ "wdth",
138989 /* 36 */ "hght",
138990 /* 37 */ "flgs",
138991 /* 38 */ "seq", /* Used by: index_list */
138992 /* 39 */ "name",
138993 /* 40 */ "unique",
138994 /* 41 */ "origin",
138995 /* 42 */ "partial",
138996 /* 43 */ "table", /* Used by: foreign_key_check */
138997 /* 44 */ "rowid",
138998 /* 45 */ "parent",
138999 /* 46 */ "fkid",
139000 /* index_info reuses 21 */
139001 /* 47 */ "seq", /* Used by: database_list */
139002 /* 48 */ "name",
139003 /* 49 */ "file",
139004 /* 50 */ "busy", /* Used by: wal_checkpoint */
139005 /* 51 */ "log",
139006 /* 52 */ "checkpointed",
139007 /* collation_list reuses 38 */
139008 /* 53 */ "database", /* Used by: lock_status */
139009 /* 54 */ "status",
139010 /* 55 */ "cache_size", /* Used by: default_cache_size */
139011 /* module_list pragma_list reuses 9 */
139012 /* 56 */ "timeout", /* Used by: busy_timeout */
139013};
139014
139015/* Definitions of all built-in pragmas */
139016typedef struct PragmaName {
139017 const char *const zName; /* Name of pragma */
139018 u8 ePragTyp; /* PragTyp_XXX value */
139019 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
139020 u8 iPragCName; /* Start of column names in pragCName[] */
139021 u8 nPragCName; /* Num of col names. 0 means use pragma name */
139022 u64 iArg; /* Extra argument */
139023} PragmaName;
139024static const PragmaName aPragmaName[] = {
139025#if defined(SQLITE_ENABLE_CEROD)
139026 {/* zName: */ "activate_extensions",
139027 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS0,
139028 /* ePragFlg: */ 0,
139029 /* ColNames: */ 0, 0,
139030 /* iArg: */ 0 },
139031#endif
139032 {/* zName: */ "analysis_limit",
139033 /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT1,
139034 /* ePragFlg: */ PragFlg_Result00x10,
139035 /* ColNames: */ 0, 0,
139036 /* iArg: */ 0 },
139037#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
139038 {/* zName: */ "application_id",
139039 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
139040 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
139041 /* ColNames: */ 0, 0,
139042 /* iArg: */ BTREE_APPLICATION_ID8 },
139043#endif
139044#if !defined(SQLITE_OMIT_AUTOVACUUM)
139045 {/* zName: */ "auto_vacuum",
139046 /* ePragTyp: */ PragTyp_AUTO_VACUUM3,
139047 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
139048 /* ColNames: */ 0, 0,
139049 /* iArg: */ 0 },
139050#endif
139051#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139052#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
139053 {/* zName: */ "automatic_index",
139054 /* ePragTyp: */ PragTyp_FLAG4,
139055 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139056 /* ColNames: */ 0, 0,
139057 /* iArg: */ SQLITE_AutoIndex0x00008000 },
139058#endif
139059#endif
139060 {/* zName: */ "busy_timeout",
139061 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT5,
139062 /* ePragFlg: */ PragFlg_Result00x10,
139063 /* ColNames: */ 56, 1,
139064 /* iArg: */ 0 },
139065#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139066 {/* zName: */ "cache_size",
139067 /* ePragTyp: */ PragTyp_CACHE_SIZE6,
139068 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
139069 /* ColNames: */ 0, 0,
139070 /* iArg: */ 0 },
139071#endif
139072#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139073 {/* zName: */ "cache_spill",
139074 /* ePragTyp: */ PragTyp_CACHE_SPILL7,
139075 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
139076 /* ColNames: */ 0, 0,
139077 /* iArg: */ 0 },
139078#endif
139079#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
139080 {/* zName: */ "case_sensitive_like",
139081 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE8,
139082 /* ePragFlg: */ PragFlg_NoColumns0x02,
139083 /* ColNames: */ 0, 0,
139084 /* iArg: */ 0 },
139085#endif
139086 {/* zName: */ "cell_size_check",
139087 /* ePragTyp: */ PragTyp_FLAG4,
139088 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139089 /* ColNames: */ 0, 0,
139090 /* iArg: */ SQLITE_CellSizeCk0x00200000 },
139091#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139092 {/* zName: */ "checkpoint_fullfsync",
139093 /* ePragTyp: */ PragTyp_FLAG4,
139094 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139095 /* ColNames: */ 0, 0,
139096 /* iArg: */ SQLITE_CkptFullFSync0x00000010 },
139097#endif
139098#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
139099 {/* zName: */ "collation_list",
139100 /* ePragTyp: */ PragTyp_COLLATION_LIST9,
139101 /* ePragFlg: */ PragFlg_Result00x10,
139102 /* ColNames: */ 38, 2,
139103 /* iArg: */ 0 },
139104#endif
139105#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
139106 {/* zName: */ "compile_options",
139107 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS10,
139108 /* ePragFlg: */ PragFlg_Result00x10,
139109 /* ColNames: */ 0, 0,
139110 /* iArg: */ 0 },
139111#endif
139112#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139113 {/* zName: */ "count_changes",
139114 /* ePragTyp: */ PragTyp_FLAG4,
139115 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139116 /* ColNames: */ 0, 0,
139117 /* iArg: */ SQLITE_CountRows((u64)(0x00001)<<32) },
139118#endif
139119#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN0
139120 {/* zName: */ "data_store_directory",
139121 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY11,
139122 /* ePragFlg: */ PragFlg_NoColumns10x04,
139123 /* ColNames: */ 0, 0,
139124 /* iArg: */ 0 },
139125#endif
139126#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
139127 {/* zName: */ "data_version",
139128 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
139129 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
139130 /* ColNames: */ 0, 0,
139131 /* iArg: */ BTREE_DATA_VERSION15 },
139132#endif
139133#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
139134 {/* zName: */ "database_list",
139135 /* ePragTyp: */ PragTyp_DATABASE_LIST12,
139136 /* ePragFlg: */ PragFlg_Result00x10,
139137 /* ColNames: */ 47, 3,
139138 /* iArg: */ 0 },
139139#endif
139140#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED1)
139141 {/* zName: */ "default_cache_size",
139142 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE13,
139143 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
139144 /* ColNames: */ 55, 1,
139145 /* iArg: */ 0 },
139146#endif
139147#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139148#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
139149 {/* zName: */ "defer_foreign_keys",
139150 /* ePragTyp: */ PragTyp_FLAG4,
139151 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139152 /* ColNames: */ 0, 0,
139153 /* iArg: */ SQLITE_DeferFKs0x00080000 },
139154#endif
139155#endif
139156#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139157 {/* zName: */ "empty_result_callbacks",
139158 /* ePragTyp: */ PragTyp_FLAG4,
139159 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139160 /* ColNames: */ 0, 0,
139161 /* iArg: */ SQLITE_NullCallback0x00000100 },
139162#endif
139163#if !defined(SQLITE_OMIT_UTF16)
139164 {/* zName: */ "encoding",
139165 /* ePragTyp: */ PragTyp_ENCODING14,
139166 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139167 /* ColNames: */ 0, 0,
139168 /* iArg: */ 0 },
139169#endif
139170#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
139171 {/* zName: */ "foreign_key_check",
139172 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK15,
139173 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139174 /* ColNames: */ 43, 4,
139175 /* iArg: */ 0 },
139176#endif
139177#if !defined(SQLITE_OMIT_FOREIGN_KEY)
139178 {/* zName: */ "foreign_key_list",
139179 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST16,
139180 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139181 /* ColNames: */ 0, 8,
139182 /* iArg: */ 0 },
139183#endif
139184#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139185#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
139186 {/* zName: */ "foreign_keys",
139187 /* ePragTyp: */ PragTyp_FLAG4,
139188 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139189 /* ColNames: */ 0, 0,
139190 /* iArg: */ SQLITE_ForeignKeys0x00004000 },
139191#endif
139192#endif
139193#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
139194 {/* zName: */ "freelist_count",
139195 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
139196 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
139197 /* ColNames: */ 0, 0,
139198 /* iArg: */ BTREE_FREE_PAGE_COUNT0 },
139199#endif
139200#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139201 {/* zName: */ "full_column_names",
139202 /* ePragTyp: */ PragTyp_FLAG4,
139203 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139204 /* ColNames: */ 0, 0,
139205 /* iArg: */ SQLITE_FullColNames0x00000004 },
139206 {/* zName: */ "fullfsync",
139207 /* ePragTyp: */ PragTyp_FLAG4,
139208 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139209 /* ColNames: */ 0, 0,
139210 /* iArg: */ SQLITE_FullFSync0x00000008 },
139211#endif
139212#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
139213#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
139214 {/* zName: */ "function_list",
139215 /* ePragTyp: */ PragTyp_FUNCTION_LIST17,
139216 /* ePragFlg: */ PragFlg_Result00x10,
139217 /* ColNames: */ 27, 6,
139218 /* iArg: */ 0 },
139219#endif
139220#endif
139221 {/* zName: */ "hard_heap_limit",
139222 /* ePragTyp: */ PragTyp_HARD_HEAP_LIMIT18,
139223 /* ePragFlg: */ PragFlg_Result00x10,
139224 /* ColNames: */ 0, 0,
139225 /* iArg: */ 0 },
139226#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139227#if !defined(SQLITE_OMIT_CHECK)
139228 {/* zName: */ "ignore_check_constraints",
139229 /* ePragTyp: */ PragTyp_FLAG4,
139230 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139231 /* ColNames: */ 0, 0,
139232 /* iArg: */ SQLITE_IgnoreChecks0x00000200 },
139233#endif
139234#endif
139235#if !defined(SQLITE_OMIT_AUTOVACUUM)
139236 {/* zName: */ "incremental_vacuum",
139237 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM19,
139238 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_NoColumns0x02,
139239 /* ColNames: */ 0, 0,
139240 /* iArg: */ 0 },
139241#endif
139242#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
139243 {/* zName: */ "index_info",
139244 /* ePragTyp: */ PragTyp_INDEX_INFO20,
139245 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139246 /* ColNames: */ 21, 3,
139247 /* iArg: */ 0 },
139248 {/* zName: */ "index_list",
139249 /* ePragTyp: */ PragTyp_INDEX_LIST21,
139250 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139251 /* ColNames: */ 38, 5,
139252 /* iArg: */ 0 },
139253 {/* zName: */ "index_xinfo",
139254 /* ePragTyp: */ PragTyp_INDEX_INFO20,
139255 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139256 /* ColNames: */ 21, 6,
139257 /* iArg: */ 1 },
139258#endif
139259#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
139260 {/* zName: */ "integrity_check",
139261 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK22,
139262 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139263 /* ColNames: */ 0, 0,
139264 /* iArg: */ 0 },
139265#endif
139266#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139267 {/* zName: */ "journal_mode",
139268 /* ePragTyp: */ PragTyp_JOURNAL_MODE23,
139269 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
139270 /* ColNames: */ 0, 0,
139271 /* iArg: */ 0 },
139272 {/* zName: */ "journal_size_limit",
139273 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT24,
139274 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
139275 /* ColNames: */ 0, 0,
139276 /* iArg: */ 0 },
139277#endif
139278#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139279 {/* zName: */ "legacy_alter_table",
139280 /* ePragTyp: */ PragTyp_FLAG4,
139281 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139282 /* ColNames: */ 0, 0,
139283 /* iArg: */ SQLITE_LegacyAlter0x04000000 },
139284#endif
139285#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE0
139286 {/* zName: */ "lock_proxy_file",
139287 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE25,
139288 /* ePragFlg: */ PragFlg_NoColumns10x04,
139289 /* ColNames: */ 0, 0,
139290 /* iArg: */ 0 },
139291#endif
139292#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
139293 {/* zName: */ "lock_status",
139294 /* ePragTyp: */ PragTyp_LOCK_STATUS44,
139295 /* ePragFlg: */ PragFlg_Result00x10,
139296 /* ColNames: */ 53, 2,
139297 /* iArg: */ 0 },
139298#endif
139299#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139300 {/* zName: */ "locking_mode",
139301 /* ePragTyp: */ PragTyp_LOCKING_MODE26,
139302 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
139303 /* ColNames: */ 0, 0,
139304 /* iArg: */ 0 },
139305 {/* zName: */ "max_page_count",
139306 /* ePragTyp: */ PragTyp_PAGE_COUNT27,
139307 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
139308 /* ColNames: */ 0, 0,
139309 /* iArg: */ 0 },
139310 {/* zName: */ "mmap_size",
139311 /* ePragTyp: */ PragTyp_MMAP_SIZE28,
139312 /* ePragFlg: */ 0,
139313 /* ColNames: */ 0, 0,
139314 /* iArg: */ 0 },
139315#endif
139316#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
139317#if !defined(SQLITE_OMIT_VIRTUALTABLE)
139318#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
139319 {/* zName: */ "module_list",
139320 /* ePragTyp: */ PragTyp_MODULE_LIST29,
139321 /* ePragFlg: */ PragFlg_Result00x10,
139322 /* ColNames: */ 9, 1,
139323 /* iArg: */ 0 },
139324#endif
139325#endif
139326#endif
139327 {/* zName: */ "optimize",
139328 /* ePragTyp: */ PragTyp_OPTIMIZE30,
139329 /* ePragFlg: */ PragFlg_Result10x20|PragFlg_NeedSchema0x01,
139330 /* ColNames: */ 0, 0,
139331 /* iArg: */ 0 },
139332#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139333 {/* zName: */ "page_count",
139334 /* ePragTyp: */ PragTyp_PAGE_COUNT27,
139335 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
139336 /* ColNames: */ 0, 0,
139337 /* iArg: */ 0 },
139338 {/* zName: */ "page_size",
139339 /* ePragTyp: */ PragTyp_PAGE_SIZE31,
139340 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
139341 /* ColNames: */ 0, 0,
139342 /* iArg: */ 0 },
139343#endif
139344#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139345#if defined(SQLITE_DEBUG)
139346 {/* zName: */ "parser_trace",
139347 /* ePragTyp: */ PragTyp_FLAG4,
139348 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139349 /* ColNames: */ 0, 0,
139350 /* iArg: */ SQLITE_ParserTrace },
139351#endif
139352#endif
139353#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
139354 {/* zName: */ "pragma_list",
139355 /* ePragTyp: */ PragTyp_PRAGMA_LIST32,
139356 /* ePragFlg: */ PragFlg_Result00x10,
139357 /* ColNames: */ 9, 1,
139358 /* iArg: */ 0 },
139359#endif
139360#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139361 {/* zName: */ "query_only",
139362 /* ePragTyp: */ PragTyp_FLAG4,
139363 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139364 /* ColNames: */ 0, 0,
139365 /* iArg: */ SQLITE_QueryOnly0x00100000 },
139366#endif
139367#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
139368 {/* zName: */ "quick_check",
139369 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK22,
139370 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139371 /* ColNames: */ 0, 0,
139372 /* iArg: */ 0 },
139373#endif
139374#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139375 {/* zName: */ "read_uncommitted",
139376 /* ePragTyp: */ PragTyp_FLAG4,
139377 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139378 /* ColNames: */ 0, 0,
139379 /* iArg: */ SQLITE_ReadUncommit((u64)(0x00004)<<32) },
139380 {/* zName: */ "recursive_triggers",
139381 /* ePragTyp: */ PragTyp_FLAG4,
139382 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139383 /* ColNames: */ 0, 0,
139384 /* iArg: */ SQLITE_RecTriggers0x00002000 },
139385 {/* zName: */ "reverse_unordered_selects",
139386 /* ePragTyp: */ PragTyp_FLAG4,
139387 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139388 /* ColNames: */ 0, 0,
139389 /* iArg: */ SQLITE_ReverseOrder0x00001000 },
139390#endif
139391#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
139392 {/* zName: */ "schema_version",
139393 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
139394 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
139395 /* ColNames: */ 0, 0,
139396 /* iArg: */ BTREE_SCHEMA_VERSION1 },
139397#endif
139398#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139399 {/* zName: */ "secure_delete",
139400 /* ePragTyp: */ PragTyp_SECURE_DELETE33,
139401 /* ePragFlg: */ PragFlg_Result00x10,
139402 /* ColNames: */ 0, 0,
139403 /* iArg: */ 0 },
139404#endif
139405#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139406 {/* zName: */ "short_column_names",
139407 /* ePragTyp: */ PragTyp_FLAG4,
139408 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139409 /* ColNames: */ 0, 0,
139410 /* iArg: */ SQLITE_ShortColNames0x00000040 },
139411#endif
139412 {/* zName: */ "shrink_memory",
139413 /* ePragTyp: */ PragTyp_SHRINK_MEMORY34,
139414 /* ePragFlg: */ PragFlg_NoColumns0x02,
139415 /* ColNames: */ 0, 0,
139416 /* iArg: */ 0 },
139417 {/* zName: */ "soft_heap_limit",
139418 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT35,
139419 /* ePragFlg: */ PragFlg_Result00x10,
139420 /* ColNames: */ 0, 0,
139421 /* iArg: */ 0 },
139422#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139423#if defined(SQLITE_DEBUG)
139424 {/* zName: */ "sql_trace",
139425 /* ePragTyp: */ PragTyp_FLAG4,
139426 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139427 /* ColNames: */ 0, 0,
139428 /* iArg: */ SQLITE_SqlTrace },
139429#endif
139430#endif
139431#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
139432 {/* zName: */ "stats",
139433 /* ePragTyp: */ PragTyp_STATS45,
139434 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
139435 /* ColNames: */ 33, 5,
139436 /* iArg: */ 0 },
139437#endif
139438#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139439 {/* zName: */ "synchronous",
139440 /* ePragTyp: */ PragTyp_SYNCHRONOUS36,
139441 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
139442 /* ColNames: */ 0, 0,
139443 /* iArg: */ 0 },
139444#endif
139445#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
139446 {/* zName: */ "table_info",
139447 /* ePragTyp: */ PragTyp_TABLE_INFO37,
139448 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139449 /* ColNames: */ 8, 6,
139450 /* iArg: */ 0 },
139451 {/* zName: */ "table_list",
139452 /* ePragTyp: */ PragTyp_TABLE_LIST38,
139453 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20,
139454 /* ColNames: */ 15, 6,
139455 /* iArg: */ 0 },
139456 {/* zName: */ "table_xinfo",
139457 /* ePragTyp: */ PragTyp_TABLE_INFO37,
139458 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
139459 /* ColNames: */ 8, 7,
139460 /* iArg: */ 1 },
139461#endif
139462#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
139463 {/* zName: */ "temp_store",
139464 /* ePragTyp: */ PragTyp_TEMP_STORE39,
139465 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139466 /* ColNames: */ 0, 0,
139467 /* iArg: */ 0 },
139468 {/* zName: */ "temp_store_directory",
139469 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY40,
139470 /* ePragFlg: */ PragFlg_NoColumns10x04,
139471 /* ColNames: */ 0, 0,
139472 /* iArg: */ 0 },
139473#endif
139474 {/* zName: */ "threads",
139475 /* ePragTyp: */ PragTyp_THREADS41,
139476 /* ePragFlg: */ PragFlg_Result00x10,
139477 /* ColNames: */ 0, 0,
139478 /* iArg: */ 0 },
139479#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139480 {/* zName: */ "trusted_schema",
139481 /* ePragTyp: */ PragTyp_FLAG4,
139482 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139483 /* ColNames: */ 0, 0,
139484 /* iArg: */ SQLITE_TrustedSchema0x00000080 },
139485#endif
139486#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
139487 {/* zName: */ "user_version",
139488 /* ePragTyp: */ PragTyp_HEADER_VALUE2,
139489 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
139490 /* ColNames: */ 0, 0,
139491 /* iArg: */ BTREE_USER_VERSION6 },
139492#endif
139493#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139494#if defined(SQLITE_DEBUG)
139495 {/* zName: */ "vdbe_addoptrace",
139496 /* ePragTyp: */ PragTyp_FLAG4,
139497 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139498 /* ColNames: */ 0, 0,
139499 /* iArg: */ SQLITE_VdbeAddopTrace },
139500 {/* zName: */ "vdbe_debug",
139501 /* ePragTyp: */ PragTyp_FLAG4,
139502 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139503 /* ColNames: */ 0, 0,
139504 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
139505 {/* zName: */ "vdbe_eqp",
139506 /* ePragTyp: */ PragTyp_FLAG4,
139507 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139508 /* ColNames: */ 0, 0,
139509 /* iArg: */ SQLITE_VdbeEQP },
139510 {/* zName: */ "vdbe_listing",
139511 /* ePragTyp: */ PragTyp_FLAG4,
139512 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139513 /* ColNames: */ 0, 0,
139514 /* iArg: */ SQLITE_VdbeListing },
139515 {/* zName: */ "vdbe_trace",
139516 /* ePragTyp: */ PragTyp_FLAG4,
139517 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139518 /* ColNames: */ 0, 0,
139519 /* iArg: */ SQLITE_VdbeTrace },
139520#endif
139521#endif
139522#if !defined(SQLITE_OMIT_WAL)
139523 {/* zName: */ "wal_autocheckpoint",
139524 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT42,
139525 /* ePragFlg: */ 0,
139526 /* ColNames: */ 0, 0,
139527 /* iArg: */ 0 },
139528 {/* zName: */ "wal_checkpoint",
139529 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT43,
139530 /* ePragFlg: */ PragFlg_NeedSchema0x01,
139531 /* ColNames: */ 50, 3,
139532 /* iArg: */ 0 },
139533#endif
139534#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
139535 {/* zName: */ "writable_schema",
139536 /* ePragTyp: */ PragTyp_FLAG4,
139537 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
139538 /* ColNames: */ 0, 0,
139539 /* iArg: */ SQLITE_WriteSchema0x00000001|SQLITE_NoSchemaError0x08000000 },
139540#endif
139541};
139542/* Number of pragmas: 68 on by default, 78 total. */
139543
139544/************** End of pragma.h **********************************************/
139545/************** Continuing where we left off in pragma.c *********************/
139546
139547/*
139548** When the 0x10 bit of PRAGMA optimize is set, any ANALYZE commands
139549** will be run with an analysis_limit set to the lessor of the value of
139550** the following macro or to the actual analysis_limit if it is non-zero,
139551** in order to prevent PRAGMA optimize from running for too long.
139552**
139553** The value of 2000 is chosen emperically so that the worst-case run-time
139554** for PRAGMA optimize does not exceed 100 milliseconds against a variety
139555** of test databases on a RaspberryPI-4 compiled using -Os and without
139556** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
139557** this paragraph, "worst-case" means that ANALYZE ends up being
139558** run on every table in the database. The worst case typically only
139559** happens if PRAGMA optimize is run on a database file for which ANALYZE
139560** has not been previously run and the 0x10000 flag is included so that
139561** all tables are analyzed. The usual case for PRAGMA optimize is that
139562** no ANALYZE commands will be run at all, or if any ANALYZE happens it
139563** will be against a single table, so that expected timing for PRAGMA
139564** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
139565** flag or less than 100 microseconds without the 0x10000 flag.
139566**
139567** An analysis limit of 2000 is almost always sufficient for the query
139568** planner to fully characterize an index. The additional accuracy from
139569** a larger analysis is not usually helpful.
139570*/
139571#ifndef SQLITE_DEFAULT_OPTIMIZE_LIMIT2000
139572# define SQLITE_DEFAULT_OPTIMIZE_LIMIT2000 2000
139573#endif
139574
139575/*
139576** Interpret the given string as a safety level. Return 0 for OFF,
139577** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
139578** unrecognized string argument. The FULL and EXTRA option is disallowed
139579** if the omitFull parameter it 1.
139580**
139581** Note that the values returned are one less that the values that
139582** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
139583** to support legacy SQL code. The safety level used to be boolean
139584** and older scripts may have used numbers 0 for OFF and 1 for ON.
139585*/
139586static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
139587 /* 123456789 123456789 123 */
139588 static const char zText[] = "onoffalseyestruextrafull";
139589 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
139590 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
139591 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
139592 /* on no off false yes true extra full */
139593 int i, n;
139594 if( sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
139595 return (u8)sqlite3Atoi(z);
139596 }
139597 n = sqlite3Strlen30(z);
139598 for(i=0; i<ArraySize(iLength)((int)(sizeof(iLength)/sizeof(iLength[0]))); i++){
139599 if( iLength[i]==n && sqlite3StrNICmpsqlite3_strnicmp(&zText[iOffset[i]],z,n)==0
139600 && (!omitFull || iValue[i]<=1)
139601 ){
139602 return iValue[i];
139603 }
139604 }
139605 return dflt;
139606}
139607
139608/*
139609** Interpret the given string as a boolean value.
139610*/
139611SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z, u8 dflt){
139612 return getSafetyLevel(z,1,dflt)!=0;
139613}
139614
139615/* The sqlite3GetBoolean() function is used by other modules but the
139616** remainder of this file is specific to PRAGMA processing. So omit
139617** the rest of the file if PRAGMAs are omitted from the build.
139618*/
139619#if !defined(SQLITE_OMIT_PRAGMA)
139620
139621/*
139622** Interpret the given string as a locking mode value.
139623*/
139624static int getLockingMode(const char *z){
139625 if( z ){
139626 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE1;
139627 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL0;
139628 }
139629 return PAGER_LOCKINGMODE_QUERY-1;
139630}
139631
139632#ifndef SQLITE_OMIT_AUTOVACUUM
139633/*
139634** Interpret the given string as an auto-vacuum mode value.
139635**
139636** The following strings, "none", "full" and "incremental" are
139637** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
139638*/
139639static int getAutoVacuum(const char *z){
139640 int i;
139641 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE0;
139642 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL1;
139643 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR2;
139644 i = sqlite3Atoi(z);
139645 return (u8)((i>=0&&i<=2)?i:0);
139646}
139647#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
139648
139649#ifndef SQLITE_OMIT_PAGER_PRAGMAS
139650/*
139651** Interpret the given string as a temp db location. Return 1 for file
139652** backed temporary databases, 2 for the Red-Black tree in memory database
139653** and 0 to use the compile-time default.
139654*/
139655static int getTempStore(const char *z){
139656 if( z[0]>='0' && z[0]<='2' ){
139657 return z[0] - '0';
139658 }else if( sqlite3StrICmp(z, "file")==0 ){
139659 return 1;
139660 }else if( sqlite3StrICmp(z, "memory")==0 ){
139661 return 2;
139662 }else{
139663 return 0;
139664 }
139665}
139666#endif /* SQLITE_PAGER_PRAGMAS */
139667
139668#ifndef SQLITE_OMIT_PAGER_PRAGMAS
139669/*
139670** Invalidate temp storage, either when the temp storage is changed
139671** from default, or when 'file' and the temp_store_directory has changed
139672*/
139673static int invalidateTempStorage(Parse *pParse){
139674 sqlite3 *db = pParse->db;
139675 if( db->aDb[1].pBt!=0 ){
139676 if( !db->autoCommit
139677 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE0
139678 ){
139679 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
139680 "from within a transaction");
139681 return SQLITE_ERROR1;
139682 }
139683 sqlite3BtreeClose(db->aDb[1].pBt);
139684 db->aDb[1].pBt = 0;
139685 sqlite3ResetAllSchemasOfConnection(db);
139686 }
139687 return SQLITE_OK0;
139688}
139689#endif /* SQLITE_PAGER_PRAGMAS */
139690
139691#ifndef SQLITE_OMIT_PAGER_PRAGMAS
139692/*
139693** If the TEMP database is open, close it and mark the database schema
139694** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
139695** or DEFAULT_TEMP_STORE pragmas.
139696*/
139697static int changeTempStorage(Parse *pParse, const char *zStorageType){
139698 int ts = getTempStore(zStorageType);
139699 sqlite3 *db = pParse->db;
139700 if( db->temp_store==ts ) return SQLITE_OK0;
139701 if( invalidateTempStorage( pParse ) != SQLITE_OK0 ){
139702 return SQLITE_ERROR1;
139703 }
139704 db->temp_store = (u8)ts;
139705 return SQLITE_OK0;
139706}
139707#endif /* SQLITE_PAGER_PRAGMAS */
139708
139709/*
139710** Set result column names for a pragma.
139711*/
139712static void setPragmaResultColumnNames(
139713 Vdbe *v, /* The query under construction */
139714 const PragmaName *pPragma /* The pragma */
139715){
139716 u8 n = pPragma->nPragCName;
139717 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
139718 if( n==0 ){
139719 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, pPragma->zName, SQLITE_STATIC((sqlite3_destructor_type)0));
139720 }else{
139721 int i, j;
139722 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
139723 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, pragCName[j], SQLITE_STATIC((sqlite3_destructor_type)0));
139724 }
139725 }
139726}
139727
139728/*
139729** Generate code to return a single integer value.
139730*/
139731static void returnSingleInt(Vdbe *v, i64 value){
139732 sqlite3VdbeAddOp4Dup8(v, OP_Int6472, 0, 1, 0, (const u8*)&value, P4_INT64(-13));
139733 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
139734}
139735
139736/*
139737** Generate code to return a single text value.
139738*/
139739static void returnSingleText(
139740 Vdbe *v, /* Prepared statement under construction */
139741 const char *zValue /* Value to be returned */
139742){
139743 if( zValue ){
139744 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
139745 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
139746 }
139747}
139748
139749
139750/*
139751** Set the safety_level and pager flags for pager iDb. Or if iDb<0
139752** set these values for all pagers.
139753*/
139754#ifndef SQLITE_OMIT_PAGER_PRAGMAS
139755static void setAllPagerFlags(sqlite3 *db){
139756 if( db->autoCommit ){
139757 Db *pDb = db->aDb;
139758 int n = db->nDb;
139759 assert( SQLITE_FullFSync==PAGER_FULLFSYNC )((void) (0));
139760 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC )((void) (0));
139761 assert( SQLITE_CacheSpill==PAGER_CACHESPILL )((void) (0));
139762 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)((void) (0))
139763 == PAGER_FLAGS_MASK )((void) (0));
139764 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level )((void) (0));
139765 while( (n--) > 0 ){
139766 if( pDb->pBt ){
139767 sqlite3BtreeSetPagerFlags(pDb->pBt,
139768 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38) );
139769 }
139770 pDb++;
139771 }
139772 }
139773}
139774#else
139775# define setAllPagerFlags(X) /* no-op */
139776#endif
139777
139778
139779/*
139780** Return a human-readable name for a constraint resolution action.
139781*/
139782#ifndef SQLITE_OMIT_FOREIGN_KEY
139783static const char *actionName(u8 action){
139784 const char *zName;
139785 switch( action ){
139786 case OE_SetNull8: zName = "SET NULL"; break;
139787 case OE_SetDflt9: zName = "SET DEFAULT"; break;
139788 case OE_Cascade10: zName = "CASCADE"; break;
139789 case OE_Restrict7: zName = "RESTRICT"; break;
139790 default: zName = "NO ACTION";
139791 assert( action==OE_None )((void) (0)); break;
139792 }
139793 return zName;
139794}
139795#endif
139796
139797
139798/*
139799** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
139800** defined in pager.h. This function returns the associated lowercase
139801** journal-mode name.
139802*/
139803SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int eMode){
139804 static char * const azModeName[] = {
139805 "delete", "persist", "off", "truncate", "memory"
139806#ifndef SQLITE_OMIT_WAL
139807 , "wal"
139808#endif
139809 };
139810 assert( PAGER_JOURNALMODE_DELETE==0 )((void) (0));
139811 assert( PAGER_JOURNALMODE_PERSIST==1 )((void) (0));
139812 assert( PAGER_JOURNALMODE_OFF==2 )((void) (0));
139813 assert( PAGER_JOURNALMODE_TRUNCATE==3 )((void) (0));
139814 assert( PAGER_JOURNALMODE_MEMORY==4 )((void) (0));
139815 assert( PAGER_JOURNALMODE_WAL==5 )((void) (0));
139816 assert( eMode>=0 && eMode<=ArraySize(azModeName) )((void) (0));
139817
139818 if( eMode==ArraySize(azModeName)((int)(sizeof(azModeName)/sizeof(azModeName[0]))) ) return 0;
139819 return azModeName[eMode];
139820}
139821
139822/*
139823** Locate a pragma in the aPragmaName[] array.
139824*/
139825static const PragmaName *pragmaLocate(const char *zName){
139826 int upr, lwr, mid = 0, rc;
139827 lwr = 0;
139828 upr = ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0])))-1;
139829 while( lwr<=upr ){
139830 mid = (lwr+upr)/2;
139831 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
139832 if( rc==0 ) break;
139833 if( rc<0 ){
139834 upr = mid - 1;
139835 }else{
139836 lwr = mid + 1;
139837 }
139838 }
139839 return lwr>upr ? 0 : &aPragmaName[mid];
139840}
139841
139842/*
139843** Create zero or more entries in the output for the SQL functions
139844** defined by FuncDef p.
139845*/
139846static void pragmaFunclistLine(
139847 Vdbe *v, /* The prepared statement being created */
139848 FuncDef *p, /* A particular function definition */
139849 int isBuiltin, /* True if this is a built-in function */
139850 int showInternFuncs /* True if showing internal functions */
139851){
139852 u32 mask =
139853 SQLITE_DETERMINISTIC0x000000800 |
139854 SQLITE_DIRECTONLY0x000080000 |
139855 SQLITE_SUBTYPE0x000100000 |
139856 SQLITE_INNOCUOUS0x000200000 |
139857 SQLITE_FUNC_INTERNAL0x00040000
139858 ;
139859 if( showInternFuncs ) mask = 0xffffffff;
139860 for(; p; p=p->pNext){
139861 const char *zType;
139862 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
139863
139864 assert( SQLITE_FUNC_ENCMASK==0x3 )((void) (0));
139865 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 )((void) (0));
139866 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 )((void) (0));
139867 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 )((void) (0));
139868
139869 if( p->xSFunc==0 ) continue;
139870 if( (p->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
139871 && showInternFuncs==0
139872 ){
139873 continue;
139874 }
139875 if( p->xValue!=0 ){
139876 zType = "w";
139877 }else if( p->xFinalize!=0 ){
139878 zType = "a";
139879 }else{
139880 zType = "s";
139881 }
139882 sqlite3VdbeMultiLoad(v, 1, "sissii",
139883 p->zName, isBuiltin,
139884 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK0x0003],
139885 p->nArg,
139886 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS0x000200000
139887 );
139888 }
139889}
139890
139891
139892/*
139893** Helper subroutine for PRAGMA integrity_check:
139894**
139895** Generate code to output a single-column result row with a value of the
139896** string held in register 3. Decrement the result count in register 1
139897** and halt if the maximum number of result rows have been issued.
139898*/
139899static int integrityCheckResultRow(Vdbe *v){
139900 int addr;
139901 sqlite3VdbeAddOp2(v, OP_ResultRow84, 3, 1);
139902 addr = sqlite3VdbeAddOp3(v, OP_IfPos50, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
139903 VdbeCoverage(v);
139904 sqlite3VdbeAddOp0(v, OP_Halt70);
139905 return addr;
139906}
139907
139908/*
139909** Process a pragma statement.
139910**
139911** Pragmas are of this form:
139912**
139913** PRAGMA [schema.]id [= value]
139914**
139915** The identifier might also be a string. The value is a string, and
139916** identifier, or a number. If minusFlag is true, then the value is
139917** a number that was preceded by a minus sign.
139918**
139919** If the left side is "database.id" then pId1 is the database name
139920** and pId2 is the id. If the left side is just "id" then pId1 is the
139921** id and pId2 is any empty string.
139922*/
139923SQLITE_PRIVATEstatic void sqlite3Pragma(
139924 Parse *pParse,
139925 Token *pId1, /* First part of [schema.]id field */
139926 Token *pId2, /* Second part of [schema.]id field, or NULL */
139927 Token *pValue, /* Token for <value>, or NULL */
139928 int minusFlag /* True if a '-' sign preceded <value> */
139929){
139930 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
139931 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
139932 const char *zDb = 0; /* The database name */
139933 Token *pId; /* Pointer to <id> token */
139934 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
139935 int iDb; /* Database index for <database> */
139936 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
139937 sqlite3 *db = pParse->db; /* The database connection */
139938 Db *pDb; /* The specific database being pragmaed */
139939 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
139940 const PragmaName *pPragma; /* The pragma */
139941
139942 if( v==0 ) return;
139943 sqlite3VdbeRunOnlyOnce(v);
139944 pParse->nMem = 2;
139945
139946 /* Interpret the [schema.] part of the pragma statement. iDb is the
139947 ** index of the database this pragma is being applied to in db.aDb[]. */
139948 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
139949 if( iDb<0 ) return;
139950 pDb = &db->aDb[iDb];
139951
139952 /* If the temp database has been explicitly named as part of the
139953 ** pragma, make sure it is open.
139954 */
139955 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
139956 return;
139957 }
139958
139959 zLeft = sqlite3NameFromToken(db, pId);
139960 if( !zLeft ) return;
139961 if( minusFlag ){
139962 zRight = sqlite3MPrintf(db, "-%T", pValue);
139963 }else{
139964 zRight = sqlite3NameFromToken(db, pValue);
139965 }
139966
139967 assert( pId2 )((void) (0));
139968 zDb = pId2->n>0 ? pDb->zDbSName : 0;
139969 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA19, zLeft, zRight, zDb) ){
139970 goto pragma_out;
139971 }
139972
139973 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
139974 ** connection. If it returns SQLITE_OK, then assume that the VFS
139975 ** handled the pragma and generate a no-op prepared statement.
139976 **
139977 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
139978 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
139979 ** object corresponding to the database file to which the pragma
139980 ** statement refers.
139981 **
139982 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
139983 ** file control is an array of pointers to strings (char**) in which the
139984 ** second element of the array is the name of the pragma and the third
139985 ** element is the argument to the pragma or NULL if the pragma has no
139986 ** argument.
139987 */
139988 aFcntl[0] = 0;
139989 aFcntl[1] = zLeft;
139990 aFcntl[2] = zRight;
139991 aFcntl[3] = 0;
139992 db->busyHandler.nBusy = 0;
139993 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA14, (void*)aFcntl);
139994 if( rc==SQLITE_OK0 ){
139995 sqlite3VdbeSetNumCols(v, 1);
139996 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, aFcntl[0], SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
139997 returnSingleText(v, aFcntl[0]);
139998 sqlite3_free(aFcntl[0]);
139999 goto pragma_out;
140000 }
140001 if( rc!=SQLITE_NOTFOUND12 ){
140002 if( aFcntl[0] ){
140003 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
140004 sqlite3_free(aFcntl[0]);
140005 }
140006 pParse->nErr++;
140007 pParse->rc = rc;
140008 goto pragma_out;
140009 }
140010
140011 /* Locate the pragma in the lookup table */
140012 pPragma = pragmaLocate(zLeft);
140013 if( pPragma==0 ){
140014 /* IMP: R-43042-22504 No error messages are generated if an
140015 ** unknown pragma is issued. */
140016 goto pragma_out;
140017 }
140018
140019 /* Make sure the database schema is loaded if the pragma requires that */
140020 if( (pPragma->mPragFlg & PragFlg_NeedSchema0x01)!=0 ){
140021 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
140022 }
140023
140024 /* Register the result column names for pragmas that return results */
140025 if( (pPragma->mPragFlg & PragFlg_NoColumns0x02)==0
140026 && ((pPragma->mPragFlg & PragFlg_NoColumns10x04)==0 || zRight==0)
140027 ){
140028 setPragmaResultColumnNames(v, pPragma);
140029 }
140030
140031 /* Jump to the appropriate pragma handler */
140032 switch( pPragma->ePragTyp ){
140033
140034#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED1)
140035 /*
140036 ** PRAGMA [schema.]default_cache_size
140037 ** PRAGMA [schema.]default_cache_size=N
140038 **
140039 ** The first form reports the current persistent setting for the
140040 ** page cache size. The value returned is the maximum number of
140041 ** pages in the page cache. The second form sets both the current
140042 ** page cache size value and the persistent page cache size value
140043 ** stored in the database file.
140044 **
140045 ** Older versions of SQLite would set the default cache size to a
140046 ** negative number to indicate synchronous=OFF. These days, synchronous
140047 ** is always on by default regardless of the sign of the default cache
140048 ** size. But continue to take the absolute value of the default cache
140049 ** size of historical compatibility.
140050 */
140051 case PragTyp_DEFAULT_CACHE_SIZE13: {
140052 static const int iLn = VDBE_OFFSET_LINENO(2)0;
140053 static const VdbeOpList getCacheSize[] = {
140054 { OP_Transaction2, 0, 0, 0}, /* 0 */
140055 { OP_ReadCookie99, 0, 1, BTREE_DEFAULT_CACHE_SIZE3}, /* 1 */
140056 { OP_IfPos50, 1, 8, 0},
140057 { OP_Integer71, 0, 2, 0},
140058 { OP_Subtract108, 1, 2, 1},
140059 { OP_IfPos50, 1, 8, 0},
140060 { OP_Integer71, 0, 1, 0}, /* 6 */
140061 { OP_Noop187, 0, 0, 0},
140062 { OP_ResultRow84, 1, 1, 0},
140063 };
140064 VdbeOp *aOp;
140065 sqlite3VdbeUsesBtree(v, iDb);
140066 if( !zRight ){
140067 pParse->nMem += 2;
140068 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
140069 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize)((int)(sizeof(getCacheSize)/sizeof(getCacheSize[0]))), getCacheSize, iLn);
140070 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
140071 aOp[0].p1 = iDb;
140072 aOp[1].p1 = iDb;
140073 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE128;
140074 }else{
140075 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
140076 sqlite3BeginWriteOperation(pParse, 0, iDb);
140077 sqlite3VdbeAddOp3(v, OP_SetCookie100, iDb, BTREE_DEFAULT_CACHE_SIZE3, size);
140078 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
140079 pDb->pSchema->cache_size = size;
140080 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
140081 }
140082 break;
140083 }
140084#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
140085
140086#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
140087 /*
140088 ** PRAGMA [schema.]page_size
140089 ** PRAGMA [schema.]page_size=N
140090 **
140091 ** The first form reports the current setting for the
140092 ** database page size in bytes. The second form sets the
140093 ** database page size value. The value can only be set if
140094 ** the database has not yet been created.
140095 */
140096 case PragTyp_PAGE_SIZE31: {
140097 Btree *pBt = pDb->pBt;
140098 assert( pBt!=0 )((void) (0));
140099 if( !zRight ){
140100 int size = ALWAYS(pBt)(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
140101 returnSingleInt(v, size);
140102 }else{
140103 /* Malloc may fail when setting the page-size, as there is an internal
140104 ** buffer that the pager module resizes using sqlite3_realloc().
140105 */
140106 db->nextPagesize = sqlite3Atoi(zRight);
140107 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
140108 sqlite3OomFault(db);
140109 }
140110 }
140111 break;
140112 }
140113
140114 /*
140115 ** PRAGMA [schema.]secure_delete
140116 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
140117 **
140118 ** The first form reports the current setting for the
140119 ** secure_delete flag. The second form changes the secure_delete
140120 ** flag setting and reports the new value.
140121 */
140122 case PragTyp_SECURE_DELETE33: {
140123 Btree *pBt = pDb->pBt;
140124 int b = -1;
140125 assert( pBt!=0 )((void) (0));
140126 if( zRight ){
140127 if( sqlite3_stricmp(zRight, "fast")==0 ){
140128 b = 2;
140129 }else{
140130 b = sqlite3GetBoolean(zRight, 0);
140131 }
140132 }
140133 if( pId2->n==0 && b>=0 ){
140134 int ii;
140135 for(ii=0; ii<db->nDb; ii++){
140136 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
140137 }
140138 }
140139 b = sqlite3BtreeSecureDelete(pBt, b);
140140 returnSingleInt(v, b);
140141 break;
140142 }
140143
140144 /*
140145 ** PRAGMA [schema.]max_page_count
140146 ** PRAGMA [schema.]max_page_count=N
140147 **
140148 ** The first form reports the current setting for the
140149 ** maximum number of pages in the database file. The
140150 ** second form attempts to change this setting. Both
140151 ** forms return the current setting.
140152 **
140153 ** The absolute value of N is used. This is undocumented and might
140154 ** change. The only purpose is to provide an easy way to test
140155 ** the sqlite3AbsInt32() function.
140156 **
140157 ** PRAGMA [schema.]page_count
140158 **
140159 ** Return the number of pages in the specified database.
140160 */
140161 case PragTyp_PAGE_COUNT27: {
140162 int iReg;
140163 i64 x = 0;
140164 sqlite3CodeVerifySchema(pParse, iDb);
140165 iReg = ++pParse->nMem;
140166 if( sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='p' ){
140167 sqlite3VdbeAddOp2(v, OP_Pagecount178, iDb, iReg);
140168 }else{
140169 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
140170 if( x<0 ) x = 0;
140171 else if( x>0xfffffffe ) x = 0xfffffffe;
140172 }else{
140173 x = 0;
140174 }
140175 sqlite3VdbeAddOp3(v, OP_MaxPgcnt179, iDb, iReg, (int)x);
140176 }
140177 sqlite3VdbeAddOp2(v, OP_ResultRow84, iReg, 1);
140178 break;
140179 }
140180
140181 /*
140182 ** PRAGMA [schema.]locking_mode
140183 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
140184 */
140185 case PragTyp_LOCKING_MODE26: {
140186 const char *zRet = "normal";
140187 int eMode = getLockingMode(zRight);
140188
140189 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY-1 ){
140190 /* Simple "PRAGMA locking_mode;" statement. This is a query for
140191 ** the current default locking mode (which may be different to
140192 ** the locking-mode of the main database).
140193 */
140194 eMode = db->dfltLockMode;
140195 }else{
140196 Pager *pPager;
140197 if( pId2->n==0 ){
140198 /* This indicates that no database name was specified as part
140199 ** of the PRAGMA command. In this case the locking-mode must be
140200 ** set on all attached databases, as well as the main db file.
140201 **
140202 ** Also, the sqlite3.dfltLockMode variable is set so that
140203 ** any subsequently attached databases also use the specified
140204 ** locking mode.
140205 */
140206 int ii;
140207 assert(pDb==&db->aDb[0])((void) (0));
140208 for(ii=2; ii<db->nDb; ii++){
140209 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
140210 sqlite3PagerLockingMode(pPager, eMode);
140211 }
140212 db->dfltLockMode = (u8)eMode;
140213 }
140214 pPager = sqlite3BtreePager(pDb->pBt);
140215 eMode = sqlite3PagerLockingMode(pPager, eMode);
140216 }
140217
140218 assert( eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
140219 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
140220 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE1 ){
140221 zRet = "exclusive";
140222 }
140223 returnSingleText(v, zRet);
140224 break;
140225 }
140226
140227 /*
140228 ** PRAGMA [schema.]journal_mode
140229 ** PRAGMA [schema.]journal_mode =
140230 ** (delete|persist|off|truncate|memory|wal|off)
140231 */
140232 case PragTyp_JOURNAL_MODE23: {
140233 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
140234 int ii; /* Loop counter */
140235
140236 if( zRight==0 ){
140237 /* If there is no "=MODE" part of the pragma, do a query for the
140238 ** current mode */
140239 eMode = PAGER_JOURNALMODE_QUERY(-1);
140240 }else{
140241 const char *zMode;
140242 int n = sqlite3Strlen30(zRight);
140243 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
140244 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, zMode, n)==0 ) break;
140245 }
140246 if( !zMode ){
140247 /* If the "=MODE" part does not match any known journal mode,
140248 ** then do a query */
140249 eMode = PAGER_JOURNALMODE_QUERY(-1);
140250 }
140251 if( eMode==PAGER_JOURNALMODE_OFF2 && (db->flags & SQLITE_Defensive0x10000000)!=0 ){
140252 /* Do not allow journal-mode "OFF" in defensive since the database
140253 ** can become corrupted using ordinary SQL when the journal is off */
140254 eMode = PAGER_JOURNALMODE_QUERY(-1);
140255 }
140256 }
140257 if( eMode==PAGER_JOURNALMODE_QUERY(-1) && pId2->n==0 ){
140258 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
140259 iDb = 0;
140260 pId2->n = 1;
140261 }
140262 for(ii=db->nDb-1; ii>=0; ii--){
140263 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
140264 sqlite3VdbeUsesBtree(v, ii);
140265 sqlite3VdbeAddOp3(v, OP_JournalMode4, ii, 1, eMode);
140266 }
140267 }
140268 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
140269 break;
140270 }
140271
140272 /*
140273 ** PRAGMA [schema.]journal_size_limit
140274 ** PRAGMA [schema.]journal_size_limit=N
140275 **
140276 ** Get or set the size limit on rollback journal files.
140277 */
140278 case PragTyp_JOURNAL_SIZE_LIMIT24: {
140279 Pager *pPager = sqlite3BtreePager(pDb->pBt);
140280 i64 iLimit = -2;
140281 if( zRight ){
140282 sqlite3DecOrHexToI64(zRight, &iLimit);
140283 if( iLimit<-1 ) iLimit = -1;
140284 }
140285 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
140286 returnSingleInt(v, iLimit);
140287 break;
140288 }
140289
140290#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
140291
140292 /*
140293 ** PRAGMA [schema.]auto_vacuum
140294 ** PRAGMA [schema.]auto_vacuum=N
140295 **
140296 ** Get or set the value of the database 'auto-vacuum' parameter.
140297 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
140298 */
140299#ifndef SQLITE_OMIT_AUTOVACUUM
140300 case PragTyp_AUTO_VACUUM3: {
140301 Btree *pBt = pDb->pBt;
140302 assert( pBt!=0 )((void) (0));
140303 if( !zRight ){
140304 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
140305 }else{
140306 int eAuto = getAutoVacuum(zRight);
140307 assert( eAuto>=0 && eAuto<=2 )((void) (0));
140308 db->nextAutovac = (u8)eAuto;
140309 /* Call SetAutoVacuum() to set initialize the internal auto and
140310 ** incr-vacuum flags. This is required in case this connection
140311 ** creates the database file. It is important that it is created
140312 ** as an auto-vacuum capable db.
140313 */
140314 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
140315 if( rc==SQLITE_OK0 && (eAuto==1 || eAuto==2) ){
140316 /* When setting the auto_vacuum mode to either "full" or
140317 ** "incremental", write the value of meta[6] in the database
140318 ** file. Before writing to meta[6], check that meta[3] indicates
140319 ** that this really is an auto-vacuum capable database.
140320 */
140321 static const int iLn = VDBE_OFFSET_LINENO(2)0;
140322 static const VdbeOpList setMeta6[] = {
140323 { OP_Transaction2, 0, 1, 0}, /* 0 */
140324 { OP_ReadCookie99, 0, 1, BTREE_LARGEST_ROOT_PAGE4},
140325 { OP_If16, 1, 0, 0}, /* 2 */
140326 { OP_Halt70, SQLITE_OK0, OE_Abort2, 0}, /* 3 */
140327 { OP_SetCookie100, 0, BTREE_INCR_VACUUM7, 0}, /* 4 */
140328 };
140329 VdbeOp *aOp;
140330 int iAddr = sqlite3VdbeCurrentAddr(v);
140331 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
140332 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6)((int)(sizeof(setMeta6)/sizeof(setMeta6[0]))), setMeta6, iLn);
140333 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
140334 aOp[0].p1 = iDb;
140335 aOp[1].p1 = iDb;
140336 aOp[2].p2 = iAddr+4;
140337 aOp[4].p1 = iDb;
140338 aOp[4].p3 = eAuto - 1;
140339 sqlite3VdbeUsesBtree(v, iDb);
140340 }
140341 }
140342 break;
140343 }
140344#endif
140345
140346 /*
140347 ** PRAGMA [schema.]incremental_vacuum(N)
140348 **
140349 ** Do N steps of incremental vacuuming on a database.
140350 */
140351#ifndef SQLITE_OMIT_AUTOVACUUM
140352 case PragTyp_INCREMENTAL_VACUUM19: {
140353 int iLimit = 0, addr;
140354 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
140355 iLimit = 0x7fffffff;
140356 }
140357 sqlite3BeginWriteOperation(pParse, 0, iDb);
140358 sqlite3VdbeAddOp2(v, OP_Integer71, iLimit, 1);
140359 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum62, iDb); VdbeCoverage(v);
140360 sqlite3VdbeAddOp1(v, OP_ResultRow84, 1);
140361 sqlite3VdbeAddOp2(v, OP_AddImm86, 1, -1);
140362 sqlite3VdbeAddOp2(v, OP_IfPos50, 1, addr); VdbeCoverage(v);
140363 sqlite3VdbeJumpHere(v, addr);
140364 break;
140365 }
140366#endif
140367
140368#ifndef SQLITE_OMIT_PAGER_PRAGMAS
140369 /*
140370 ** PRAGMA [schema.]cache_size
140371 ** PRAGMA [schema.]cache_size=N
140372 **
140373 ** The first form reports the current local setting for the
140374 ** page cache size. The second form sets the local
140375 ** page cache size value. If N is positive then that is the
140376 ** number of pages in the cache. If N is negative, then the
140377 ** number of pages is adjusted so that the cache uses -N kibibytes
140378 ** of memory.
140379 */
140380 case PragTyp_CACHE_SIZE6: {
140381 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
140382 if( !zRight ){
140383 returnSingleInt(v, pDb->pSchema->cache_size);
140384 }else{
140385 int size = sqlite3Atoi(zRight);
140386 pDb->pSchema->cache_size = size;
140387 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
140388 }
140389 break;
140390 }
140391
140392 /*
140393 ** PRAGMA [schema.]cache_spill
140394 ** PRAGMA cache_spill=BOOLEAN
140395 ** PRAGMA [schema.]cache_spill=N
140396 **
140397 ** The first form reports the current local setting for the
140398 ** page cache spill size. The second form turns cache spill on
140399 ** or off. When turning cache spill on, the size is set to the
140400 ** current cache_size. The third form sets a spill size that
140401 ** may be different form the cache size.
140402 ** If N is positive then that is the
140403 ** number of pages in the cache. If N is negative, then the
140404 ** number of pages is adjusted so that the cache uses -N kibibytes
140405 ** of memory.
140406 **
140407 ** If the number of cache_spill pages is less then the number of
140408 ** cache_size pages, no spilling occurs until the page count exceeds
140409 ** the number of cache_size pages.
140410 **
140411 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
140412 ** not just the schema specified.
140413 */
140414 case PragTyp_CACHE_SPILL7: {
140415 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
140416 if( !zRight ){
140417 returnSingleInt(v,
140418 (db->flags & SQLITE_CacheSpill0x00000020)==0 ? 0 :
140419 sqlite3BtreeSetSpillSize(pDb->pBt,0));
140420 }else{
140421 int size = 1;
140422 if( sqlite3GetInt32(zRight, &size) ){
140423 sqlite3BtreeSetSpillSize(pDb->pBt, size);
140424 }
140425 if( sqlite3GetBoolean(zRight, size!=0) ){
140426 db->flags |= SQLITE_CacheSpill0x00000020;
140427 }else{
140428 db->flags &= ~(u64)SQLITE_CacheSpill0x00000020;
140429 }
140430 setAllPagerFlags(db);
140431 }
140432 break;
140433 }
140434
140435 /*
140436 ** PRAGMA [schema.]mmap_size(N)
140437 **
140438 ** Used to set mapping size limit. The mapping size limit is
140439 ** used to limit the aggregate size of all memory mapped regions of the
140440 ** database file. If this parameter is set to zero, then memory mapping
140441 ** is not used at all. If N is negative, then the default memory map
140442 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
140443 ** The parameter N is measured in bytes.
140444 **
140445 ** This value is advisory. The underlying VFS is free to memory map
140446 ** as little or as much as it wants. Except, if N is set to 0 then the
140447 ** upper layers will never invoke the xFetch interfaces to the VFS.
140448 */
140449 case PragTyp_MMAP_SIZE28: {
140450 sqlite3_int64 sz;
140451#if SQLITE_MAX_MMAP_SIZE20971520>0
140452 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
140453 if( zRight ){
140454 int ii;
140455 sqlite3DecOrHexToI64(zRight, &sz);
140456 if( sz<0 ) sz = sqlite3GlobalConfigsqlite3Config.szMmap;
140457 if( pId2->n==0 ) db->szMmap = sz;
140458 for(ii=db->nDb-1; ii>=0; ii--){
140459 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
140460 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
140461 }
140462 }
140463 }
140464 sz = -1;
140465 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE18, &sz);
140466#else
140467 sz = 0;
140468 rc = SQLITE_OK0;
140469#endif
140470 if( rc==SQLITE_OK0 ){
140471 returnSingleInt(v, sz);
140472 }else if( rc!=SQLITE_NOTFOUND12 ){
140473 pParse->nErr++;
140474 pParse->rc = rc;
140475 }
140476 break;
140477 }
140478
140479 /*
140480 ** PRAGMA temp_store
140481 ** PRAGMA temp_store = "default"|"memory"|"file"
140482 **
140483 ** Return or set the local value of the temp_store flag. Changing
140484 ** the local value does not make changes to the disk file and the default
140485 ** value will be restored the next time the database is opened.
140486 **
140487 ** Note that it is possible for the library compile-time options to
140488 ** override this setting
140489 */
140490 case PragTyp_TEMP_STORE39: {
140491 if( !zRight ){
140492 returnSingleInt(v, db->temp_store);
140493 }else{
140494 changeTempStorage(pParse, zRight);
140495 }
140496 break;
140497 }
140498
140499 /*
140500 ** PRAGMA temp_store_directory
140501 ** PRAGMA temp_store_directory = ""|"directory_name"
140502 **
140503 ** Return or set the local value of the temp_store_directory flag. Changing
140504 ** the value sets a specific directory to be used for temporary files.
140505 ** Setting to a null string reverts to the default temporary directory search.
140506 ** If temporary directory is changed, then invalidateTempStorage.
140507 **
140508 */
140509 case PragTyp_TEMP_STORE_DIRECTORY40: {
140510 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
140511 if( !zRight ){
140512 returnSingleText(v, sqlite3_temp_directory);
140513 }else{
140514#ifndef SQLITE_OMIT_WSD
140515 if( zRight[0] ){
140516 int res;
140517 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
140518 if( rc!=SQLITE_OK0 || res==0 ){
140519 sqlite3ErrorMsg(pParse, "not a writable directory");
140520 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
140521 goto pragma_out;
140522 }
140523 }
140524 if( SQLITE_TEMP_STORE1==0
140525 || (SQLITE_TEMP_STORE1==1 && db->temp_store<=1)
140526 || (SQLITE_TEMP_STORE1==2 && db->temp_store==1)
140527 ){
140528 invalidateTempStorage(pParse);
140529 }
140530 sqlite3_free(sqlite3_temp_directory);
140531 if( zRight[0] ){
140532 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
140533 }else{
140534 sqlite3_temp_directory = 0;
140535 }
140536#endif /* SQLITE_OMIT_WSD */
140537 }
140538 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
140539 break;
140540 }
140541
140542#if SQLITE_OS_WIN0
140543 /*
140544 ** PRAGMA data_store_directory
140545 ** PRAGMA data_store_directory = ""|"directory_name"
140546 **
140547 ** Return or set the local value of the data_store_directory flag. Changing
140548 ** the value sets a specific directory to be used for database files that
140549 ** were specified with a relative pathname. Setting to a null string reverts
140550 ** to the default database directory, which for database files specified with
140551 ** a relative path will probably be based on the current directory for the
140552 ** process. Database file specified with an absolute path are not impacted
140553 ** by this setting, regardless of its value.
140554 **
140555 */
140556 case PragTyp_DATA_STORE_DIRECTORY11: {
140557 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
140558 if( !zRight ){
140559 returnSingleText(v, sqlite3_data_directory);
140560 }else{
140561#ifndef SQLITE_OMIT_WSD
140562 if( zRight[0] ){
140563 int res;
140564 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
140565 if( rc!=SQLITE_OK0 || res==0 ){
140566 sqlite3ErrorMsg(pParse, "not a writable directory");
140567 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
140568 goto pragma_out;
140569 }
140570 }
140571 sqlite3_free(sqlite3_data_directory);
140572 if( zRight[0] ){
140573 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
140574 }else{
140575 sqlite3_data_directory = 0;
140576 }
140577#endif /* SQLITE_OMIT_WSD */
140578 }
140579 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR11));
140580 break;
140581 }
140582#endif
140583
140584#if SQLITE_ENABLE_LOCKING_STYLE0
140585 /*
140586 ** PRAGMA [schema.]lock_proxy_file
140587 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
140588 **
140589 ** Return or set the value of the lock_proxy_file flag. Changing
140590 ** the value sets a specific file to be used for database access locks.
140591 **
140592 */
140593 case PragTyp_LOCK_PROXY_FILE25: {
140594 if( !zRight ){
140595 Pager *pPager = sqlite3BtreePager(pDb->pBt);
140596 char *proxy_file_path = NULL((void*)0);
140597 sqlite3_file *pFile = sqlite3PagerFile(pPager);
140598 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE2,
140599 &proxy_file_path);
140600 returnSingleText(v, proxy_file_path);
140601 }else{
140602 Pager *pPager = sqlite3BtreePager(pDb->pBt);
140603 sqlite3_file *pFile = sqlite3PagerFile(pPager);
140604 int res;
140605 if( zRight[0] ){
140606 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
140607 zRight);
140608 } else {
140609 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
140610 NULL((void*)0));
140611 }
140612 if( res!=SQLITE_OK0 ){
140613 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
140614 goto pragma_out;
140615 }
140616 }
140617 break;
140618 }
140619#endif /* SQLITE_ENABLE_LOCKING_STYLE */
140620
140621 /*
140622 ** PRAGMA [schema.]synchronous
140623 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
140624 **
140625 ** Return or set the local value of the synchronous flag. Changing
140626 ** the local value does not make changes to the disk file and the
140627 ** default value will be restored the next time the database is
140628 ** opened.
140629 */
140630 case PragTyp_SYNCHRONOUS36: {
140631 if( !zRight ){
140632 returnSingleInt(v, pDb->safety_level-1);
140633 }else{
140634 if( !db->autoCommit ){
140635 sqlite3ErrorMsg(pParse,
140636 "Safety level may not be changed inside a transaction");
140637 }else if( iDb!=1 ){
140638 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK0x07;
140639 if( iLevel==0 ) iLevel = 1;
140640 pDb->safety_level = iLevel;
140641 pDb->bSyncSet = 1;
140642 setAllPagerFlags(db);
140643 }
140644 }
140645 break;
140646 }
140647#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
140648
140649#ifndef SQLITE_OMIT_FLAG_PRAGMAS
140650 case PragTyp_FLAG4: {
140651 if( zRight==0 ){
140652 setPragmaResultColumnNames(v, pPragma);
140653 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
140654 }else{
140655 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
140656 if( db->autoCommit==0 ){
140657 /* Foreign key support may not be enabled or disabled while not
140658 ** in auto-commit mode. */
140659 mask &= ~(SQLITE_ForeignKeys0x00004000);
140660 }
140661#if SQLITE_USER_AUTHENTICATION
140662 if( db->auth.authLevel==UAUTH_User ){
140663 /* Do not allow non-admin users to modify the schema arbitrarily */
140664 mask &= ~(SQLITE_WriteSchema0x00000001);
140665 }
140666#endif
140667
140668 if( sqlite3GetBoolean(zRight, 0) ){
140669 if( (mask & SQLITE_WriteSchema0x00000001)==0
140670 || (db->flags & SQLITE_Defensive0x10000000)==0
140671 ){
140672 db->flags |= mask;
140673 }
140674 }else{
140675 db->flags &= ~mask;
140676 if( mask==SQLITE_DeferFKs0x00080000 ) db->nDeferredImmCons = 0;
140677 if( (mask & SQLITE_WriteSchema0x00000001)!=0
140678 && sqlite3_stricmp(zRight, "reset")==0
140679 ){
140680 /* IMP: R-60817-01178 If the argument is "RESET" then schema
140681 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
140682 ** in addition, the schema is reloaded. */
140683 sqlite3ResetAllSchemasOfConnection(db);
140684 }
140685 }
140686
140687 /* Many of the flag-pragmas modify the code generated by the SQL
140688 ** compiler (eg. count_changes). So add an opcode to expire all
140689 ** compiled SQL statements after modifying a pragma value.
140690 */
140691 sqlite3VdbeAddOp0(v, OP_Expire166);
140692 setAllPagerFlags(db);
140693 }
140694 break;
140695 }
140696#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
140697
140698#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
140699 /*
140700 ** PRAGMA table_info(<table>)
140701 **
140702 ** Return a single row for each column of the named table. The columns of
140703 ** the returned data set are:
140704 **
140705 ** cid: Column id (numbered from left to right, starting at 0)
140706 ** name: Column name
140707 ** type: Column declaration type.
140708 ** notnull: True if 'NOT NULL' is part of column declaration
140709 ** dflt_value: The default value for the column, if any.
140710 ** pk: Non-zero for PK fields.
140711 */
140712 case PragTyp_TABLE_INFO37: if( zRight ){
140713 Table *pTab;
140714 sqlite3CodeVerifyNamedSchema(pParse, zDb);
140715 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
140716 if( pTab ){
140717 int i, k;
140718 int nHidden = 0;
140719 Column *pCol;
140720 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
140721 pParse->nMem = 7;
140722 sqlite3ViewGetColumnNames(pParse, pTab);
140723 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
140724 int isHidden = 0;
140725 const Expr *pColExpr;
140726 if( pCol->colFlags & COLFLAG_NOINSERT0x0062 ){
140727 if( pPragma->iArg==0 ){
140728 nHidden++;
140729 continue;
140730 }
140731 if( pCol->colFlags & COLFLAG_VIRTUAL0x0020 ){
140732 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */
140733 }else if( pCol->colFlags & COLFLAG_STORED0x0040 ){
140734 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */
140735 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN )((void) (0));
140736 isHidden = 1; /* HIDDEN */
140737 }
140738 }
140739 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)==0 ){
140740 k = 0;
140741 }else if( pPk==0 ){
140742 k = 1;
140743 }else{
140744 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
140745 }
140746 pColExpr = sqlite3ColumnExpr(pTab,pCol);
140747 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 )((void) (0));
140748 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)((void) (0))
140749 || isHidden>=2 )((void) (0));
140750 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
140751 i-nHidden,
140752 pCol->zCnName,
140753 sqlite3ColumnType(pCol,""),
140754 pCol->notNull ? 1 : 0,
140755 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
140756 k,
140757 isHidden);
140758 }
140759 }
140760 }
140761 break;
140762
140763 /*
140764 ** PRAGMA table_list
140765 **
140766 ** Return a single row for each table, virtual table, or view in the
140767 ** entire schema.
140768 **
140769 ** schema: Name of attached database hold this table
140770 ** name: Name of the table itself
140771 ** type: "table", "view", "virtual", "shadow"
140772 ** ncol: Number of columns
140773 ** wr: True for a WITHOUT ROWID table
140774 ** strict: True for a STRICT table
140775 */
140776 case PragTyp_TABLE_LIST38: {
140777 int ii;
140778 pParse->nMem = 6;
140779 sqlite3CodeVerifyNamedSchema(pParse, zDb);
140780 for(ii=0; ii<db->nDb; ii++){
140781 HashElem *k;
140782 Hash *pHash;
140783 int initNCol;
140784 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
140785
140786 /* Ensure that the Table.nCol field is initialized for all views
140787 ** and virtual tables. Each time we initialize a Table.nCol value
140788 ** for a table, that can potentially disrupt the hash table, so restart
140789 ** the initialization scan.
140790 */
140791 pHash = &db->aDb[ii].pSchema->tblHash;
140792 initNCol = sqliteHashCount(pHash)((pHash)->count);
140793 while( initNCol-- ){
140794 for(k=sqliteHashFirst(pHash)((pHash)->first); 1; k=sqliteHashNext(k)((k)->next) ){
140795 Table *pTab;
140796 if( k==0 ){ initNCol = 0; break; }
140797 pTab = sqliteHashData(k)((k)->data);
140798 if( pTab->nCol==0 ){
140799 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
140800 if( zSql ){
140801 sqlite3_stmt *pDummy = 0;
140802 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
140803 (void)sqlite3_finalize(pDummy);
140804 sqlite3DbFree(db, zSql);
140805 }
140806 if( db->mallocFailed ){
140807 sqlite3ErrorMsg(db->pParse, "out of memory");
140808 db->pParse->rc = SQLITE_NOMEM_BKPT7;
140809 }
140810 pHash = &db->aDb[ii].pSchema->tblHash;
140811 break;
140812 }
140813 }
140814 }
140815
140816 for(k=sqliteHashFirst(pHash)((pHash)->first); k; k=sqliteHashNext(k)((k)->next) ){
140817 Table *pTab = sqliteHashData(k)((k)->data);
140818 const char *zType;
140819 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
140820 if( IsView(pTab)((pTab)->eTabType==2) ){
140821 zType = "view";
140822 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
140823 zType = "virtual";
140824 }else if( pTab->tabFlags & TF_Shadow0x00001000 ){
140825 zType = "shadow";
140826 }else{
140827 zType = "table";
140828 }
140829 sqlite3VdbeMultiLoad(v, 1, "sssiii",
140830 db->aDb[ii].zDbSName,
140831 sqlite3PreferredTableName(pTab->zName),
140832 zType,
140833 pTab->nCol,
140834 (pTab->tabFlags & TF_WithoutRowid0x00000080)!=0,
140835 (pTab->tabFlags & TF_Strict0x00010000)!=0
140836 );
140837 }
140838 }
140839 }
140840 break;
140841
140842#ifdef SQLITE_DEBUG
140843 case PragTyp_STATS45: {
140844 Index *pIdx;
140845 HashElem *i;
140846 pParse->nMem = 5;
140847 sqlite3CodeVerifySchema(pParse, iDb);
140848 for(i=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
140849 Table *pTab = sqliteHashData(i)((i)->data);
140850 sqlite3VdbeMultiLoad(v, 1, "ssiii",
140851 sqlite3PreferredTableName(pTab->zName),
140852 0,
140853 pTab->szTabRow,
140854 pTab->nRowLogEst,
140855 pTab->tabFlags);
140856 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140857 sqlite3VdbeMultiLoad(v, 2, "siiiX",
140858 pIdx->zName,
140859 pIdx->szIdxRow,
140860 pIdx->aiRowLogEst[0],
140861 pIdx->hasStat1);
140862 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 5);
140863 }
140864 }
140865 }
140866 break;
140867#endif
140868
140869 case PragTyp_INDEX_INFO20: if( zRight ){
140870 Index *pIdx;
140871 Table *pTab;
140872 pIdx = sqlite3FindIndex(db, zRight, zDb);
140873 if( pIdx==0 ){
140874 /* If there is no index named zRight, check to see if there is a
140875 ** WITHOUT ROWID table named zRight, and if there is, show the
140876 ** structure of the PRIMARY KEY index for that table. */
140877 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
140878 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
140879 pIdx = sqlite3PrimaryKeyIndex(pTab);
140880 }
140881 }
140882 if( pIdx ){
140883 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
140884 int i;
140885 int mx;
140886 if( pPragma->iArg ){
140887 /* PRAGMA index_xinfo (newer version with more rows and columns) */
140888 mx = pIdx->nColumn;
140889 pParse->nMem = 6;
140890 }else{
140891 /* PRAGMA index_info (legacy version) */
140892 mx = pIdx->nKeyCol;
140893 pParse->nMem = 3;
140894 }
140895 pTab = pIdx->pTable;
140896 sqlite3CodeVerifySchema(pParse, iIdxDb);
140897 assert( pParse->nMem<=pPragma->nPragCName )((void) (0));
140898 for(i=0; i<mx; i++){
140899 i16 cnum = pIdx->aiColumn[i];
140900 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
140901 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
140902 if( pPragma->iArg ){
140903 sqlite3VdbeMultiLoad(v, 4, "isiX",
140904 pIdx->aSortOrder[i],
140905 pIdx->azColl[i],
140906 i<pIdx->nKeyCol);
140907 }
140908 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, pParse->nMem);
140909 }
140910 }
140911 }
140912 break;
140913
140914 case PragTyp_INDEX_LIST21: if( zRight ){
140915 Index *pIdx;
140916 Table *pTab;
140917 int i;
140918 pTab = sqlite3FindTable(db, zRight, zDb);
140919 if( pTab ){
140920 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140921 pParse->nMem = 5;
140922 sqlite3CodeVerifySchema(pParse, iTabDb);
140923 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
140924 const char *azOrigin[] = { "c", "u", "pk" };
140925 sqlite3VdbeMultiLoad(v, 1, "isisi",
140926 i,
140927 pIdx->zName,
140928 IsUniqueIndex(pIdx)((pIdx)->onError!=0),
140929 azOrigin[pIdx->idxType],
140930 pIdx->pPartIdxWhere!=0);
140931 }
140932 }
140933 }
140934 break;
140935
140936 case PragTyp_DATABASE_LIST12: {
140937 int i;
140938 pParse->nMem = 3;
140939 for(i=0; i<db->nDb; i++){
140940 if( db->aDb[i].pBt==0 ) continue;
140941 assert( db->aDb[i].zDbSName!=0 )((void) (0));
140942 sqlite3VdbeMultiLoad(v, 1, "iss",
140943 i,
140944 db->aDb[i].zDbSName,
140945 sqlite3BtreeGetFilename(db->aDb[i].pBt));
140946 }
140947 }
140948 break;
140949
140950 case PragTyp_COLLATION_LIST9: {
140951 int i = 0;
140952 HashElem *p;
140953 pParse->nMem = 2;
140954 for(p=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); p; p=sqliteHashNext(p)((p)->next)){
140955 CollSeq *pColl = (CollSeq *)sqliteHashData(p)((p)->data);
140956 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
140957 }
140958 }
140959 break;
140960
140961#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
140962 case PragTyp_FUNCTION_LIST17: {
140963 int i;
140964 HashElem *j;
140965 FuncDef *p;
140966 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc0x0020)!=0;
140967 pParse->nMem = 6;
140968 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
140969 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
140970 assert( p->funcFlags & SQLITE_FUNC_BUILTIN )((void) (0));
140971 pragmaFunclistLine(v, p, 1, showInternFunc);
140972 }
140973 }
140974 for(j=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); j; j=sqliteHashNext(j)((j)->next)){
140975 p = (FuncDef*)sqliteHashData(j)((j)->data);
140976 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 )((void) (0));
140977 pragmaFunclistLine(v, p, 0, showInternFunc);
140978 }
140979 }
140980 break;
140981
140982#ifndef SQLITE_OMIT_VIRTUALTABLE
140983 case PragTyp_MODULE_LIST29: {
140984 HashElem *j;
140985 pParse->nMem = 1;
140986 for(j=sqliteHashFirst(&db->aModule)((&db->aModule)->first); j; j=sqliteHashNext(j)((j)->next)){
140987 Module *pMod = (Module*)sqliteHashData(j)((j)->data);
140988 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
140989 }
140990 }
140991 break;
140992#endif /* SQLITE_OMIT_VIRTUALTABLE */
140993
140994 case PragTyp_PRAGMA_LIST32: {
140995 int i;
140996 for(i=0; i<ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0]))); i++){
140997 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
140998 }
140999 }
141000 break;
141001#endif /* SQLITE_INTROSPECTION_PRAGMAS */
141002
141003#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
141004
141005#ifndef SQLITE_OMIT_FOREIGN_KEY
141006 case PragTyp_FOREIGN_KEY_LIST16: if( zRight ){
141007 FKey *pFK;
141008 Table *pTab;
141009 pTab = sqlite3FindTable(db, zRight, zDb);
141010 if( pTab && IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
141011 pFK = pTab->u.tab.pFKey;
141012 if( pFK ){
141013 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
141014 int i = 0;
141015 pParse->nMem = 8;
141016 sqlite3CodeVerifySchema(pParse, iTabDb);
141017 while(pFK){
141018 int j;
141019 for(j=0; j<pFK->nCol; j++){
141020 sqlite3VdbeMultiLoad(v, 1, "iissssss",
141021 i,
141022 j,
141023 pFK->zTo,
141024 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
141025 pFK->aCol[j].zCol,
141026 actionName(pFK->aAction[1]), /* ON UPDATE */
141027 actionName(pFK->aAction[0]), /* ON DELETE */
141028 "NONE");
141029 }
141030 ++i;
141031 pFK = pFK->pNextFrom;
141032 }
141033 }
141034 }
141035 }
141036 break;
141037#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
141038
141039#ifndef SQLITE_OMIT_FOREIGN_KEY
141040#ifndef SQLITE_OMIT_TRIGGER
141041 case PragTyp_FOREIGN_KEY_CHECK15: {
141042 FKey *pFK; /* A foreign key constraint */
141043 Table *pTab; /* Child table contain "REFERENCES" keyword */
141044 Table *pParent; /* Parent table that child points to */
141045 Index *pIdx; /* Index in the parent table */
141046 int i; /* Loop counter: Foreign key number for pTab */
141047 int j; /* Loop counter: Field of the foreign key */
141048 HashElem *k; /* Loop counter: Next table in schema */
141049 int x; /* result variable */
141050 int regResult; /* 3 registers to hold a result row */
141051 int regRow; /* Registers to hold a row from pTab */
141052 int addrTop; /* Top of a loop checking foreign keys */
141053 int addrOk; /* Jump here if the key is OK */
141054 int *aiCols; /* child to parent column mapping */
141055
141056 regResult = pParse->nMem+1;
141057 pParse->nMem += 4;
141058 regRow = ++pParse->nMem;
141059 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash)((&db->aDb[iDb].pSchema->tblHash)->first);
141060 while( k ){
141061 if( zRight ){
141062 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
141063 k = 0;
141064 }else{
141065 pTab = (Table*)sqliteHashData(k)((k)->data);
141066 k = sqliteHashNext(k)((k)->next);
141067 }
141068 if( pTab==0 || !IsOrdinaryTable(pTab)((pTab)->eTabType==0) || pTab->u.tab.pFKey==0 ) continue;
141069 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
141070 zDb = db->aDb[iDb].zDbSName;
141071 sqlite3CodeVerifySchema(pParse, iDb);
141072 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
141073 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
141074 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead102);
141075 sqlite3VdbeLoadString(v, regResult, pTab->zName);
141076 assert( IsOrdinaryTable(pTab) )((void) (0));
141077 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
141078 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
141079 if( pParent==0 ) continue;
141080 pIdx = 0;
141081 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
141082 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
141083 if( x==0 ){
141084 if( pIdx==0 ){
141085 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead102);
141086 }else{
141087 sqlite3VdbeAddOp3(v, OP_OpenRead102, i, pIdx->tnum, iDb);
141088 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
141089 }
141090 }else{
141091 k = 0;
141092 break;
141093 }
141094 }
141095 assert( pParse->nErr>0 || pFK==0 )((void) (0));
141096 if( pFK ) break;
141097 if( pParse->nTab<i ) pParse->nTab = i;
141098 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, 0); VdbeCoverage(v);
141099 assert( IsOrdinaryTable(pTab) )((void) (0));
141100 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
141101 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
141102 pIdx = 0;
141103 aiCols = 0;
141104 if( pParent ){
141105 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
141106 assert( x==0 || db->mallocFailed )((void) (0));
141107 }
141108 addrOk = sqlite3VdbeMakeLabel(pParse);
141109
141110 /* Generate code to read the child key values into registers
141111 ** regRow..regRow+n. If any of the child key values are NULL, this
141112 ** row cannot cause an FK violation. Jump directly to addrOk in
141113 ** this case. */
141114 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
141115 for(j=0; j<pFK->nCol; j++){
141116 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
141117 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
141118 sqlite3VdbeAddOp2(v, OP_IsNull51, regRow+j, addrOk); VdbeCoverage(v);
141119 }
141120
141121 /* Generate code to query the parent index for a matching parent
141122 ** key. If a match is found, jump to addrOk. */
141123 if( pIdx ){
141124 sqlite3VdbeAddOp4(v, OP_Affinity96, regRow, pFK->nCol, 0,
141125 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
141126 sqlite3VdbeAddOp4Int(v, OP_Found29, i, addrOk, regRow, pFK->nCol);
141127 VdbeCoverage(v);
141128 }else if( pParent ){
141129 int jmp = sqlite3VdbeCurrentAddr(v)+2;
141130 sqlite3VdbeAddOp3(v, OP_SeekRowid30, i, jmp, regRow); VdbeCoverage(v);
141131 sqlite3VdbeGoto(v, addrOk);
141132 assert( pFK->nCol==1 || db->mallocFailed )((void) (0));
141133 }
141134
141135 /* Generate code to report an FK violation to the caller. */
141136 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
141137 sqlite3VdbeAddOp2(v, OP_Rowid135, 0, regResult+1);
141138 }else{
141139 sqlite3VdbeAddOp2(v, OP_Null75, 0, regResult+1);
141140 }
141141 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
141142 sqlite3VdbeAddOp2(v, OP_ResultRow84, regResult, 4);
141143 sqlite3VdbeResolveLabel(v, addrOk);
141144 sqlite3DbFree(db, aiCols);
141145 }
141146 sqlite3VdbeAddOp2(v, OP_Next39, 0, addrTop+1); VdbeCoverage(v);
141147 sqlite3VdbeJumpHere(v, addrTop);
141148 }
141149 }
141150 break;
141151#endif /* !defined(SQLITE_OMIT_TRIGGER) */
141152#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
141153
141154#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
141155 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
141156 ** used will be case sensitive or not depending on the RHS.
141157 */
141158 case PragTyp_CASE_SENSITIVE_LIKE8: {
141159 if( zRight ){
141160 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
141161 }
141162 }
141163 break;
141164#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
141165
141166#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
141167# define SQLITE_INTEGRITY_CHECK_ERROR_MAX100 100
141168#endif
141169
141170#ifndef SQLITE_OMIT_INTEGRITY_CHECK
141171 /* PRAGMA integrity_check
141172 ** PRAGMA integrity_check(N)
141173 ** PRAGMA quick_check
141174 ** PRAGMA quick_check(N)
141175 **
141176 ** Verify the integrity of the database.
141177 **
141178 ** The "quick_check" is reduced version of
141179 ** integrity_check designed to detect most database corruption
141180 ** without the overhead of cross-checking indexes. Quick_check
141181 ** is linear time whereas integrity_check is O(NlogN).
141182 **
141183 ** The maximum number of errors is 100 by default. A different default
141184 ** can be specified using a numeric parameter N.
141185 **
141186 ** Or, the parameter N can be the name of a table. In that case, only
141187 ** the one table named is verified. The freelist is only verified if
141188 ** the named table is "sqlite_schema" (or one of its aliases).
141189 **
141190 ** All schemas are checked by default. To check just a single
141191 ** schema, use the form:
141192 **
141193 ** PRAGMA schema.integrity_check;
141194 */
141195 case PragTyp_INTEGRITY_CHECK22: {
141196 int i, j, addr, mxErr;
141197 Table *pObjTab = 0; /* Check only this one table, if not NULL */
141198
141199 int isQuick = (sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='q');
141200
141201 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
141202 ** then iDb is set to the index of the database identified by <db>.
141203 ** In this case, the integrity of database iDb only is verified by
141204 ** the VDBE created below.
141205 **
141206 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
141207 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
141208 ** to -1 here, to indicate that the VDBE should verify the integrity
141209 ** of all attached databases. */
141210 assert( iDb>=0 )((void) (0));
141211 assert( iDb==0 || pId2->z )((void) (0));
141212 if( pId2->z==0 ) iDb = -1;
141213
141214 /* Initialize the VDBE program */
141215 pParse->nMem = 6;
141216
141217 /* Set the maximum error count */
141218 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
141219 if( zRight ){
141220 if( sqlite3GetInt32(pValue->z, &mxErr) ){
141221 if( mxErr<=0 ){
141222 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
141223 }
141224 }else{
141225 pObjTab = sqlite3LocateTable(pParse, 0, zRight,
141226 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
141227 }
141228 }
141229 sqlite3VdbeAddOp2(v, OP_Integer71, mxErr-1, 1); /* reg[1] holds errors left */
141230
141231 /* Do an integrity check on each database file */
141232 for(i=0; i<db->nDb; i++){
141233 HashElem *x; /* For looping over tables in the schema */
141234 Hash *pTbls; /* Set of all tables in the schema */
141235 int *aRoot; /* Array of root page numbers of all btrees */
141236 int cnt = 0; /* Number of entries in aRoot[] */
141237
141238 if( OMIT_TEMPDB0 && i==1 ) continue;
141239 if( iDb>=0 && i!=iDb ) continue;
141240
141241 sqlite3CodeVerifySchema(pParse, i);
141242 pParse->okConstFactor = 0; /* tag-20230327-1 */
141243
141244 /* Do an integrity check of the B-Tree
141245 **
141246 ** Begin by finding the root pages numbers
141247 ** for all tables and indices in the database.
141248 */
141249 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
141250 pTbls = &db->aDb[i].pSchema->tblHash;
141251 for(cnt=0, x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
141252 Table *pTab = sqliteHashData(x)((x)->data); /* Current table */
141253 Index *pIdx; /* An index on pTab */
141254 int nIdx; /* Number of indexes on pTab */
141255 if( pObjTab && pObjTab!=pTab ) continue;
141256 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) cnt++;
141257 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
141258 }
141259 if( cnt==0 ) continue;
141260 if( pObjTab ) cnt++;
141261 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
141262 if( aRoot==0 ) break;
141263 cnt = 0;
141264 if( pObjTab ) aRoot[++cnt] = 0;
141265 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
141266 Table *pTab = sqliteHashData(x)((x)->data);
141267 Index *pIdx;
141268 if( pObjTab && pObjTab!=pTab ) continue;
141269 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) aRoot[++cnt] = pTab->tnum;
141270 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141271 aRoot[++cnt] = pIdx->tnum;
141272 }
141273 }
141274 aRoot[0] = cnt;
141275
141276 /* Make sure sufficient number of registers have been allocated */
141277 sqlite3TouchRegister(pParse, 8+cnt);
141278 sqlite3VdbeAddOp3(v, OP_Null75, 0, 8, 8+cnt);
141279 sqlite3ClearTempRegCache(pParse);
141280
141281 /* Do the b-tree integrity checks */
141282 sqlite3VdbeAddOp4(v, OP_IntegrityCk155, 1, cnt, 8, (char*)aRoot,P4_INTARRAY(-14));
141283 sqlite3VdbeChangeP5(v, (u8)i);
141284 addr = sqlite3VdbeAddOp1(v, OP_IsNull51, 2); VdbeCoverage(v);
141285 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0,
141286 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
141287 P4_DYNAMIC(-6));
141288 sqlite3VdbeAddOp3(v, OP_Concat112, 2, 3, 3);
141289 integrityCheckResultRow(v);
141290 sqlite3VdbeJumpHere(v, addr);
141291
141292 /* Check that the indexes all have the right number of rows */
141293 cnt = pObjTab ? 1 : 0;
141294 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
141295 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
141296 int iTab = 0;
141297 Table *pTab = sqliteHashData(x)((x)->data);
141298 Index *pIdx;
141299 if( pObjTab && pObjTab!=pTab ) continue;
141300 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
141301 iTab = cnt++;
141302 }else{
141303 iTab = cnt;
141304 for(pIdx=pTab->pIndex; ALWAYS(pIdx)(pIdx); pIdx=pIdx->pNext){
141305 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ) break;
141306 iTab++;
141307 }
141308 }
141309 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141310 if( pIdx->pPartIdxWhere==0 ){
141311 addr = sqlite3VdbeAddOp3(v, OP_Eq54, 8+cnt, 0, 8+iTab);
141312 VdbeCoverageNeverNull(v);
141313 sqlite3VdbeLoadString(v, 4, pIdx->zName);
141314 sqlite3VdbeAddOp3(v, OP_Concat112, 4, 2, 3);
141315 integrityCheckResultRow(v);
141316 sqlite3VdbeJumpHere(v, addr);
141317 }
141318 cnt++;
141319 }
141320 }
141321
141322 /* Make sure all the indices are constructed correctly.
141323 */
141324 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
141325 Table *pTab = sqliteHashData(x)((x)->data);
141326 Index *pIdx, *pPk;
141327 Index *pPrior = 0; /* Previous index */
141328 int loopTop;
141329 int iDataCur, iIdxCur;
141330 int r1 = -1;
141331 int bStrict; /* True for a STRICT table */
141332 int r2; /* Previous key for WITHOUT ROWID tables */
141333 int mxCol; /* Maximum non-virtual column number */
141334
141335 if( pObjTab && pObjTab!=pTab ) continue;
141336 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
141337 if( isQuick || HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
141338 pPk = 0;
141339 r2 = 0;
141340 }else{
141341 pPk = sqlite3PrimaryKeyIndex(pTab);
141342 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
141343 sqlite3VdbeAddOp3(v, OP_Null75, 1, r2, r2+pPk->nKeyCol-1);
141344 }
141345 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead102, 0,
141346 1, 0, &iDataCur, &iIdxCur);
141347 /* reg[7] counts the number of entries in the table.
141348 ** reg[8+i] counts the number of entries in the i-th index
141349 */
141350 sqlite3VdbeAddOp2(v, OP_Integer71, 0, 7);
141351 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
141352 sqlite3VdbeAddOp2(v, OP_Integer71, 0, 8+j); /* index entries counter */
141353 }
141354 assert( pParse->nMem>=8+j )((void) (0));
141355 assert( sqlite3NoTempsInRange(pParse,1,7+j) )((void) (0));
141356 sqlite3VdbeAddOp2(v, OP_Rewind36, iDataCur, 0); VdbeCoverage(v);
141357 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm86, 7, 1);
141358
141359 /* Fetch the right-most column from the table. This will cause
141360 ** the entire record header to be parsed and sanity checked. It
141361 ** will also prepopulate the cursor column cache that is used
141362 ** by the OP_IsType code, so it is a required step.
141363 */
141364 assert( !IsVirtual(pTab) )((void) (0));
141365 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
141366 mxCol = -1;
141367 for(j=0; j<pTab->nCol; j++){
141368 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020)==0 ) mxCol++;
141369 }
141370 if( mxCol==pTab->iPKey ) mxCol--;
141371 }else{
141372 /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID
141373 ** PK index column-count, so there is no need to account for them
141374 ** in this case. */
141375 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
141376 }
141377 if( mxCol>=0 ){
141378 sqlite3VdbeAddOp3(v, OP_Column94, iDataCur, mxCol, 3);
141379 sqlite3VdbeTypeofColumn(v, 3);
141380 }
141381
141382 if( !isQuick ){
141383 if( pPk ){
141384 /* Verify WITHOUT ROWID keys are in ascending order */
141385 int a1;
141386 char *zErr;
141387 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT41, iDataCur, 0,r2,pPk->nKeyCol);
141388 VdbeCoverage(v);
141389 sqlite3VdbeAddOp1(v, OP_IsNull51, r2); VdbeCoverage(v);
141390 zErr = sqlite3MPrintf(db,
141391 "row not in PRIMARY KEY order for %s",
141392 pTab->zName);
141393 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
141394 integrityCheckResultRow(v);
141395 sqlite3VdbeJumpHere(v, a1);
141396 sqlite3VdbeJumpHere(v, a1+1);
141397 for(j=0; j<pPk->nKeyCol; j++){
141398 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
141399 }
141400 }
141401 }
141402 /* Verify datatypes for all columns:
141403 **
141404 ** (1) NOT NULL columns may not contain a NULL
141405 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
141406 ** (3) Datatype for TEXT columns in non-STRICT tables must be
141407 ** NULL, TEXT, or BLOB.
141408 ** (4) Datatype for numeric columns in non-STRICT tables must not
141409 ** be a TEXT value that can be losslessly converted to numeric.
141410 */
141411 bStrict = (pTab->tabFlags & TF_Strict0x00010000)!=0;
141412 for(j=0; j<pTab->nCol; j++){
141413 char *zErr;
141414 Column *pCol = pTab->aCol + j; /* The column to be checked */
141415 int labelError; /* Jump here to report an error */
141416 int labelOk; /* Jump here if all looks ok */
141417 int p1, p3, p4; /* Operands to the OP_IsType opcode */
141418 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
141419
141420 if( j==pTab->iPKey ) continue;
141421 if( bStrict ){
141422 doTypeCheck = pCol->eCType>COLTYPE_ANY1;
141423 }else{
141424 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB0x41;
141425 }
141426 if( pCol->notNull==0 && !doTypeCheck ) continue;
141427
141428 /* Compute the operands that will be needed for OP_IsType */
141429 p4 = SQLITE_NULL5;
141430 if( pCol->colFlags & COLFLAG_VIRTUAL0x0020 ){
141431 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
141432 p1 = -1;
141433 p3 = 3;
141434 }else{
141435 if( pCol->iDflt ){
141436 sqlite3_value *pDfltValue = 0;
141437 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db)((db)->enc),
141438 pCol->affinity, &pDfltValue);
141439 if( pDfltValue ){
141440 p4 = sqlite3_value_type(pDfltValue);
141441 sqlite3ValueFree(pDfltValue);
141442 }
141443 }
141444 p1 = iDataCur;
141445 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
141446 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
141447 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
141448 }else{
141449 p3 = sqlite3TableColumnToStorage(pTab,j);
141450 testcase( p3!=j);
141451 }
141452 }
141453
141454 labelError = sqlite3VdbeMakeLabel(pParse);
141455 labelOk = sqlite3VdbeMakeLabel(pParse);
141456 if( pCol->notNull ){
141457 /* (1) NOT NULL columns may not contain a NULL */
141458 int jmp3;
141459 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
141460 VdbeCoverage(v);
141461 if( p1<0 ){
141462 sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */
141463 jmp3 = jmp2;
141464 }else{
141465 sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */
141466 /* OP_IsType does not detect NaN values in the database file
141467 ** which should be treated as a NULL. So if the header type
141468 ** is REAL, we have to load the actual data using OP_Column
141469 ** to reliably determine if the value is a NULL. */
141470 sqlite3VdbeAddOp3(v, OP_Column94, p1, p3, 3);
141471 sqlite3ColumnDefault(v, pTab, j, 3);
141472 jmp3 = sqlite3VdbeAddOp2(v, OP_NotNull52, 3, labelOk);
141473 VdbeCoverage(v);
141474 }
141475 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
141476 pCol->zCnName);
141477 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
141478 if( doTypeCheck ){
141479 sqlite3VdbeGoto(v, labelError);
141480 sqlite3VdbeJumpHere(v, jmp2);
141481 sqlite3VdbeJumpHere(v, jmp3);
141482 }else{
141483 /* VDBE byte code will fall thru */
141484 }
141485 }
141486 if( bStrict && doTypeCheck ){
141487 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
141488 static unsigned char aStdTypeMask[] = {
141489 0x1f, /* ANY */
141490 0x18, /* BLOB */
141491 0x11, /* INT */
141492 0x11, /* INTEGER */
141493 0x13, /* REAL */
141494 0x14 /* TEXT */
141495 };
141496 sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
141497 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) )((void) (0));
141498 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
141499 VdbeCoverage(v);
141500 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
141501 sqlite3StdType[pCol->eCType-1],
141502 pTab->zName, pTab->aCol[j].zCnName);
141503 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
141504 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT0x42 ){
141505 /* (3) Datatype for TEXT columns in non-STRICT tables must be
141506 ** NULL, TEXT, or BLOB. */
141507 sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
141508 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
141509 VdbeCoverage(v);
141510 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
141511 pTab->zName, pTab->aCol[j].zCnName);
141512 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
141513 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC0x43 ){
141514 /* (4) Datatype for numeric columns in non-STRICT tables must not
141515 ** be a TEXT value that can be converted to numeric. */
141516 sqlite3VdbeAddOp4Int(v, OP_IsType18, p1, labelOk, p3, p4);
141517 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
141518 VdbeCoverage(v);
141519 if( p1>=0 ){
141520 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
141521 }
141522 sqlite3VdbeAddOp4(v, OP_Affinity96, 3, 1, 0, "C", P4_STATIC(-1));
141523 sqlite3VdbeAddOp4Int(v, OP_IsType18, -1, labelOk, 3, p4);
141524 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
141525 VdbeCoverage(v);
141526 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
141527 pTab->zName, pTab->aCol[j].zCnName);
141528 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
141529 }
141530 sqlite3VdbeResolveLabel(v, labelError);
141531 integrityCheckResultRow(v);
141532 sqlite3VdbeResolveLabel(v, labelOk);
141533 }
141534 /* Verify CHECK constraints */
141535 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
141536 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
141537 if( db->mallocFailed==0 ){
141538 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
141539 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
141540 char *zErr;
141541 int k;
141542 pParse->iSelfTab = iDataCur + 1;
141543 for(k=pCheck->nExpr-1; k>0; k--){
141544 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
141545 }
141546 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
141547 SQLITE_JUMPIFNULL0x10);
141548 sqlite3VdbeResolveLabel(v, addrCkFault);
141549 pParse->iSelfTab = 0;
141550 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
141551 pTab->zName);
141552 sqlite3VdbeAddOp4(v, OP_String8118, 0, 3, 0, zErr, P4_DYNAMIC(-6));
141553 integrityCheckResultRow(v);
141554 sqlite3VdbeResolveLabel(v, addrCkOk);
141555 }
141556 sqlite3ExprListDelete(db, pCheck);
141557 }
141558 if( !isQuick ){ /* Omit the remaining tests for quick_check */
141559 /* Validate index entries for the current row */
141560 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
141561 int jmp2, jmp3, jmp4, jmp5, label6;
141562 int kk;
141563 int ckUniq = sqlite3VdbeMakeLabel(pParse);
141564 if( pPk==pIdx ) continue;
141565 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
141566 pPrior, r1);
141567 pPrior = pIdx;
141568 sqlite3VdbeAddOp2(v, OP_AddImm86, 8+j, 1);/* increment entry count */
141569 /* Verify that an index entry exists for the current table row */
141570 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found29, iIdxCur+j, ckUniq, r1,
141571 pIdx->nColumn); VdbeCoverage(v);
141572 sqlite3VdbeLoadString(v, 3, "row ");
141573 sqlite3VdbeAddOp3(v, OP_Concat112, 7, 3, 3);
141574 sqlite3VdbeLoadString(v, 4, " missing from index ");
141575 sqlite3VdbeAddOp3(v, OP_Concat112, 4, 3, 3);
141576 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
141577 sqlite3VdbeAddOp3(v, OP_Concat112, 4, 3, 3);
141578 jmp4 = integrityCheckResultRow(v);
141579 sqlite3VdbeJumpHere(v, jmp2);
141580
141581 /* The OP_IdxRowid opcode is an optimized version of OP_Column
141582 ** that extracts the rowid off the end of the index record.
141583 ** But it only works correctly if index record does not have
141584 ** any extra bytes at the end. Verify that this is the case. */
141585 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
141586 int jmp7;
141587 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iIdxCur+j, 3);
141588 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq54, 3, 0, r1+pIdx->nColumn-1);
141589 VdbeCoverageNeverNull(v);
141590 sqlite3VdbeLoadString(v, 3,
141591 "rowid not at end-of-record for row ");
141592 sqlite3VdbeAddOp3(v, OP_Concat112, 7, 3, 3);
141593 sqlite3VdbeLoadString(v, 4, " of index ");
141594 sqlite3VdbeGoto(v, jmp5-1);
141595 sqlite3VdbeJumpHere(v, jmp7);
141596 }
141597
141598 /* Any indexed columns with non-BINARY collations must still hold
141599 ** the exact same text value as the table. */
141600 label6 = 0;
141601 for(kk=0; kk<pIdx->nKeyCol; kk++){
141602 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
141603 if( label6==0 ) label6 = sqlite3VdbeMakeLabel(pParse);
141604 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur+j, kk, 3);
141605 sqlite3VdbeAddOp3(v, OP_Ne53, 3, label6, r1+kk); VdbeCoverage(v);
141606 }
141607 if( label6 ){
141608 int jmp6 = sqlite3VdbeAddOp0(v, OP_Goto9);
141609 sqlite3VdbeResolveLabel(v, label6);
141610 sqlite3VdbeLoadString(v, 3, "row ");
141611 sqlite3VdbeAddOp3(v, OP_Concat112, 7, 3, 3);
141612 sqlite3VdbeLoadString(v, 4, " values differ from index ");
141613 sqlite3VdbeGoto(v, jmp5-1);
141614 sqlite3VdbeJumpHere(v, jmp6);
141615 }
141616
141617 /* For UNIQUE indexes, verify that only one entry exists with the
141618 ** current key. The entry is unique if (1) any column is NULL
141619 ** or (2) the next entry has a different key */
141620 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
141621 int uniqOk = sqlite3VdbeMakeLabel(pParse);
141622 int jmp6;
141623 for(kk=0; kk<pIdx->nKeyCol; kk++){
141624 int iCol = pIdx->aiColumn[kk];
141625 assert( iCol!=XN_ROWID && iCol<pTab->nCol )((void) (0));
141626 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
141627 sqlite3VdbeAddOp2(v, OP_IsNull51, r1+kk, uniqOk);
141628 VdbeCoverage(v);
141629 }
141630 jmp6 = sqlite3VdbeAddOp1(v, OP_Next39, iIdxCur+j); VdbeCoverage(v);
141631 sqlite3VdbeGoto(v, uniqOk);
141632 sqlite3VdbeJumpHere(v, jmp6);
141633 sqlite3VdbeAddOp4Int(v, OP_IdxGT41, iIdxCur+j, uniqOk, r1,
141634 pIdx->nKeyCol); VdbeCoverage(v);
141635 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
141636 sqlite3VdbeGoto(v, jmp5);
141637 sqlite3VdbeResolveLabel(v, uniqOk);
141638 }
141639 sqlite3VdbeJumpHere(v, jmp4);
141640 sqlite3ResolvePartIdxLabel(pParse, jmp3);
141641 }
141642 }
141643 sqlite3VdbeAddOp2(v, OP_Next39, iDataCur, loopTop); VdbeCoverage(v);
141644 sqlite3VdbeJumpHere(v, loopTop-1);
141645 if( pPk ){
141646 assert( !isQuick )((void) (0));
141647 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
141648 }
141649 }
141650
141651#ifndef SQLITE_OMIT_VIRTUALTABLE
141652 /* Second pass to invoke the xIntegrity method on all virtual
141653 ** tables.
141654 */
141655 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
141656 Table *pTab = sqliteHashData(x)((x)->data);
141657 sqlite3_vtab *pVTab;
141658 int a1;
141659 if( pObjTab && pObjTab!=pTab ) continue;
141660 if( IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
141661 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) continue;
141662 if( pTab->nCol<=0 ){
141663 const char *zMod = pTab->u.vtab.azArg[0];
141664 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
141665 }
141666 sqlite3ViewGetColumnNames(pParse, pTab);
141667 if( pTab->u.vtab.p==0 ) continue;
141668 pVTab = pTab->u.vtab.p->pVtab;
141669 if( NEVER(pVTab==0)(pVTab==0) ) continue;
141670 if( NEVER(pVTab->pModule==0)(pVTab->pModule==0) ) continue;
141671 if( pVTab->pModule->iVersion<4 ) continue;
141672 if( pVTab->pModule->xIntegrity==0 ) continue;
141673 sqlite3VdbeAddOp3(v, OP_VCheck174, i, 3, isQuick);
141674 pTab->nTabRef++;
141675 sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF(-16));
141676 a1 = sqlite3VdbeAddOp1(v, OP_IsNull51, 3); VdbeCoverage(v);
141677 integrityCheckResultRow(v);
141678 sqlite3VdbeJumpHere(v, a1);
141679 continue;
141680 }
141681#endif
141682 }
141683 {
141684 static const int iLn = VDBE_OFFSET_LINENO(2)0;
141685 static const VdbeOpList endCode[] = {
141686 { OP_AddImm86, 1, 0, 0}, /* 0 */
141687 { OP_IfNotZero60, 1, 4, 0}, /* 1 */
141688 { OP_String8118, 0, 3, 0}, /* 2 */
141689 { OP_ResultRow84, 3, 1, 0}, /* 3 */
141690 { OP_Halt70, 0, 0, 0}, /* 4 */
141691 { OP_String8118, 0, 3, 0}, /* 5 */
141692 { OP_Goto9, 0, 3, 0}, /* 6 */
141693 };
141694 VdbeOp *aOp;
141695
141696 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode)((int)(sizeof(endCode)/sizeof(endCode[0]))), endCode, iLn);
141697 if( aOp ){
141698 aOp[0].p2 = 1-mxErr;
141699 aOp[2].p4type = P4_STATIC(-1);
141700 aOp[2].p4.z = "ok";
141701 aOp[5].p4type = P4_STATIC(-1);
141702 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT11);
141703 }
141704 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
141705 }
141706 }
141707 break;
141708#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
141709
141710#ifndef SQLITE_OMIT_UTF16
141711 /*
141712 ** PRAGMA encoding
141713 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
141714 **
141715 ** In its first form, this pragma returns the encoding of the main
141716 ** database. If the database is not initialized, it is initialized now.
141717 **
141718 ** The second form of this pragma is a no-op if the main database file
141719 ** has not already been initialized. In this case it sets the default
141720 ** encoding that will be used for the main database file if a new file
141721 ** is created. If an existing main database file is opened, then the
141722 ** default text encoding for the existing database is used.
141723 **
141724 ** In all cases new databases created using the ATTACH command are
141725 ** created to use the same default text encoding as the main database. If
141726 ** the main database has not been initialized and/or created when ATTACH
141727 ** is executed, this is done before the ATTACH operation.
141728 **
141729 ** In the second form this pragma sets the text encoding to be used in
141730 ** new database files created using this database handle. It is only
141731 ** useful if invoked immediately after the main database i
141732 */
141733 case PragTyp_ENCODING14: {
141734 static const struct EncName {
141735 char *zName;
141736 u8 enc;
141737 } encnames[] = {
141738 { "UTF8", SQLITE_UTF81 },
141739 { "UTF-8", SQLITE_UTF81 }, /* Must be element [1] */
141740 { "UTF-16le", SQLITE_UTF16LE2 }, /* Must be element [2] */
141741 { "UTF-16be", SQLITE_UTF16BE3 }, /* Must be element [3] */
141742 { "UTF16le", SQLITE_UTF16LE2 },
141743 { "UTF16be", SQLITE_UTF16BE3 },
141744 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
141745 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
141746 { 0, 0 }
141747 };
141748 const struct EncName *pEnc;
141749 if( !zRight ){ /* "PRAGMA encoding" */
141750 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
141751 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 )((void) (0));
141752 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE )((void) (0));
141753 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE )((void) (0));
141754 returnSingleText(v, encnames[ENC(pParse->db)((pParse->db)->enc)].zName);
141755 }else{ /* "PRAGMA encoding = XXX" */
141756 /* Only change the value of sqlite.enc if the database handle is not
141757 ** initialized. If the main database exists, the new sqlite.enc value
141758 ** will be overwritten when the schema is next loaded. If it does not
141759 ** already exists, it will be created to use the new encoding value.
141760 */
141761 if( (db->mDbFlags & DBFLAG_EncodingFixed0x0040)==0 ){
141762 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
141763 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
141764 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE2;
141765 SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) = enc;
141766 sqlite3SetTextEncoding(db, enc);
141767 break;
141768 }
141769 }
141770 if( !pEnc->zName ){
141771 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
141772 }
141773 }
141774 }
141775 }
141776 break;
141777#endif /* SQLITE_OMIT_UTF16 */
141778
141779#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
141780 /*
141781 ** PRAGMA [schema.]schema_version
141782 ** PRAGMA [schema.]schema_version = <integer>
141783 **
141784 ** PRAGMA [schema.]user_version
141785 ** PRAGMA [schema.]user_version = <integer>
141786 **
141787 ** PRAGMA [schema.]freelist_count
141788 **
141789 ** PRAGMA [schema.]data_version
141790 **
141791 ** PRAGMA [schema.]application_id
141792 ** PRAGMA [schema.]application_id = <integer>
141793 **
141794 ** The pragma's schema_version and user_version are used to set or get
141795 ** the value of the schema-version and user-version, respectively. Both
141796 ** the schema-version and the user-version are 32-bit signed integers
141797 ** stored in the database header.
141798 **
141799 ** The schema-cookie is usually only manipulated internally by SQLite. It
141800 ** is incremented by SQLite whenever the database schema is modified (by
141801 ** creating or dropping a table or index). The schema version is used by
141802 ** SQLite each time a query is executed to ensure that the internal cache
141803 ** of the schema used when compiling the SQL query matches the schema of
141804 ** the database against which the compiled query is actually executed.
141805 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
141806 ** the schema-version is potentially dangerous and may lead to program
141807 ** crashes or database corruption. Use with caution!
141808 **
141809 ** The user-version is not used internally by SQLite. It may be used by
141810 ** applications for any purpose.
141811 */
141812 case PragTyp_HEADER_VALUE2: {
141813 int iCookie = pPragma->iArg; /* Which cookie to read or write */
141814 sqlite3VdbeUsesBtree(v, iDb);
141815 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly0x08)==0 ){
141816 /* Write the specified cookie value */
141817 static const VdbeOpList setCookie[] = {
141818 { OP_Transaction2, 0, 1, 0}, /* 0 */
141819 { OP_SetCookie100, 0, 0, 0}, /* 1 */
141820 };
141821 VdbeOp *aOp;
141822 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
141823 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie)((int)(sizeof(setCookie)/sizeof(setCookie[0]))), setCookie, 0);
141824 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
141825 aOp[0].p1 = iDb;
141826 aOp[1].p1 = iDb;
141827 aOp[1].p2 = iCookie;
141828 aOp[1].p3 = sqlite3Atoi(zRight);
141829 aOp[1].p5 = 1;
141830 if( iCookie==BTREE_SCHEMA_VERSION1 && (db->flags & SQLITE_Defensive0x10000000)!=0 ){
141831 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
141832 ** mode. Change the OP_SetCookie opcode into a no-op. */
141833 aOp[1].opcode = OP_Noop187;
141834 }
141835 }else{
141836 /* Read the specified cookie value */
141837 static const VdbeOpList readCookie[] = {
141838 { OP_Transaction2, 0, 0, 0}, /* 0 */
141839 { OP_ReadCookie99, 0, 1, 0}, /* 1 */
141840 { OP_ResultRow84, 1, 1, 0}
141841 };
141842 VdbeOp *aOp;
141843 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
141844 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie)((int)(sizeof(readCookie)/sizeof(readCookie[0]))),readCookie,0);
141845 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
141846 aOp[0].p1 = iDb;
141847 aOp[1].p1 = iDb;
141848 aOp[1].p3 = iCookie;
141849 sqlite3VdbeReusable(v);
141850 }
141851 }
141852 break;
141853#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
141854
141855#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
141856 /*
141857 ** PRAGMA compile_options
141858 **
141859 ** Return the names of all compile-time options used in this build,
141860 ** one option per row.
141861 */
141862 case PragTyp_COMPILE_OPTIONS10: {
141863 int i = 0;
141864 const char *zOpt;
141865 pParse->nMem = 1;
141866 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
141867 sqlite3VdbeLoadString(v, 1, zOpt);
141868 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 1);
141869 }
141870 sqlite3VdbeReusable(v);
141871 }
141872 break;
141873#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
141874
141875#ifndef SQLITE_OMIT_WAL
141876 /*
141877 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
141878 **
141879 ** Checkpoint the database.
141880 */
141881 case PragTyp_WAL_CHECKPOINT43: {
141882 int iBt = (pId2->z?iDb:SQLITE_MAX_DB(10 +2));
141883 int eMode = SQLITE_CHECKPOINT_PASSIVE0;
141884 if( zRight ){
141885 if( sqlite3StrICmp(zRight, "full")==0 ){
141886 eMode = SQLITE_CHECKPOINT_FULL1;
141887 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
141888 eMode = SQLITE_CHECKPOINT_RESTART2;
141889 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
141890 eMode = SQLITE_CHECKPOINT_TRUNCATE3;
141891 }
141892 }
141893 pParse->nMem = 3;
141894 sqlite3VdbeAddOp3(v, OP_Checkpoint3, iBt, eMode, 1);
141895 sqlite3VdbeAddOp2(v, OP_ResultRow84, 1, 3);
141896 }
141897 break;
141898
141899 /*
141900 ** PRAGMA wal_autocheckpoint
141901 ** PRAGMA wal_autocheckpoint = N
141902 **
141903 ** Configure a database connection to automatically checkpoint a database
141904 ** after accumulating N frames in the log. Or query for the current value
141905 ** of N.
141906 */
141907 case PragTyp_WAL_AUTOCHECKPOINT42: {
141908 if( zRight ){
141909 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
141910 }
141911 returnSingleInt(v,
141912 db->xWalCallback==sqlite3WalDefaultHook ?
141913 SQLITE_PTR_TO_INT(db->pWalArg)((int)(long int)(db->pWalArg)) : 0);
141914 }
141915 break;
141916#endif
141917
141918 /*
141919 ** PRAGMA shrink_memory
141920 **
141921 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
141922 ** connection on which it is invoked to free up as much memory as it
141923 ** can, by calling sqlite3_db_release_memory().
141924 */
141925 case PragTyp_SHRINK_MEMORY34: {
141926 sqlite3_db_release_memory(db);
141927 break;
141928 }
141929
141930 /*
141931 ** PRAGMA optimize
141932 ** PRAGMA optimize(MASK)
141933 ** PRAGMA schema.optimize
141934 ** PRAGMA schema.optimize(MASK)
141935 **
141936 ** Attempt to optimize the database. All schemas are optimized in the first
141937 ** two forms, and only the specified schema is optimized in the latter two.
141938 **
141939 ** The details of optimizations performed by this pragma are expected
141940 ** to change and improve over time. Applications should anticipate that
141941 ** this pragma will perform new optimizations in future releases.
141942 **
141943 ** The optional argument is a bitmask of optimizations to perform:
141944 **
141945 ** 0x00001 Debugging mode. Do not actually perform any optimizations
141946 ** but instead return one line of text for each optimization
141947 ** that would have been done. Off by default.
141948 **
141949 ** 0x00002 Run ANALYZE on tables that might benefit. On by default.
141950 ** See below for additional information.
141951 **
141952 ** 0x00010 Run all ANALYZE operations using an analysis_limit that
141953 ** is the lessor of the current analysis_limit and the
141954 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
141955 ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is
141956 ** currently (2024-02-19) set to 2000, which is such that
141957 ** the worst case run-time for PRAGMA optimize on a 100MB
141958 ** database will usually be less than 100 milliseconds on
141959 ** a RaspberryPI-4 class machine. On by default.
141960 **
141961 ** 0x10000 Look at tables to see if they need to be reanalyzed
141962 ** due to growth or shrinkage even if they have not been
141963 ** queried during the current connection. Off by default.
141964 **
141965 ** The default MASK is and always shall be 0x0fffe. In the current
141966 ** implementation, the default mask only covers the 0x00002 optimization,
141967 ** though additional optimizations that are covered by 0x0fffe might be
141968 ** added in the future. Optimizations that are off by default and must
141969 ** be explicitly requested have masks of 0x10000 or greater.
141970 **
141971 ** DETERMINATION OF WHEN TO RUN ANALYZE
141972 **
141973 ** In the current implementation, a table is analyzed if only if all of
141974 ** the following are true:
141975 **
141976 ** (1) MASK bit 0x00002 is set.
141977 **
141978 ** (2) The table is an ordinary table, not a virtual table or view.
141979 **
141980 ** (3) The table name does not begin with "sqlite_".
141981 **
141982 ** (4) One or more of the following is true:
141983 ** (4a) The 0x10000 MASK bit is set.
141984 ** (4b) One or more indexes on the table lacks an entry
141985 ** in the sqlite_stat1 table.
141986 ** (4c) The query planner used sqlite_stat1-style statistics for one
141987 ** or more indexes of the table at some point during the lifetime
141988 ** of the current connection.
141989 **
141990 ** (5) One or more of the following is true:
141991 ** (5a) One or more indexes on the table lacks an entry
141992 ** in the sqlite_stat1 table. (Same as 4a)
141993 ** (5b) The number of rows in the table has increased or decreased by
141994 ** 10-fold. In other words, the current size of the table is
141995 ** 10 times larger than the size in sqlite_stat1 or else the
141996 ** current size is less than 1/10th the size in sqlite_stat1.
141997 **
141998 ** The rules for when tables are analyzed are likely to change in
141999 ** future releases. Future versions of SQLite might accept a string
142000 ** literal argument to this pragma that contains a mnemonic description
142001 ** of the options rather than a bitmap.
142002 */
142003 case PragTyp_OPTIMIZE30: {
142004 int iDbLast; /* Loop termination point for the schema loop */
142005 int iTabCur; /* Cursor for a table whose size needs checking */
142006 HashElem *k; /* Loop over tables of a schema */
142007 Schema *pSchema; /* The current schema */
142008 Table *pTab; /* A table in the schema */
142009 Index *pIdx; /* An index of the table */
142010 LogEst szThreshold; /* Size threshold above which reanalysis needed */
142011 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
142012 u32 opMask; /* Mask of operations to perform */
142013 int nLimit; /* Analysis limit to use */
142014 int nCheck = 0; /* Number of tables to be optimized */
142015 int nBtree = 0; /* Number of btrees to scan */
142016 int nIndex; /* Number of indexes on the current table */
142017
142018 if( zRight ){
142019 opMask = (u32)sqlite3Atoi(zRight);
142020 if( (opMask & 0x02)==0 ) break;
142021 }else{
142022 opMask = 0xfffe;
142023 }
142024 if( (opMask & 0x10)==0 ){
142025 nLimit = 0;
142026 }else if( db->nAnalysisLimit>0
142027 && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT2000 ){
142028 nLimit = 0;
142029 }else{
142030 nLimit = SQLITE_DEFAULT_OPTIMIZE_LIMIT2000;
142031 }
142032 iTabCur = pParse->nTab++;
142033 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
142034 if( iDb==1 ) continue;
142035 sqlite3CodeVerifySchema(pParse, iDb);
142036 pSchema = db->aDb[iDb].pSchema;
142037 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
142038 pTab = (Table*)sqliteHashData(k)((k)->data);
142039
142040 /* This only works for ordinary tables */
142041 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) continue;
142042
142043 /* Do not scan system tables */
142044 if( 0==sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7) ) continue;
142045
142046 /* Find the size of the table as last recorded in sqlite_stat1.
142047 ** If any index is unanalyzed, then the threshold is -1 to
142048 ** indicate a new, unanalyzed index
142049 */
142050 szThreshold = pTab->nRowLogEst;
142051 nIndex = 0;
142052 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
142053 nIndex++;
142054 if( !pIdx->hasStat1 ){
142055 szThreshold = -1; /* Always analyze if any index lacks statistics */
142056 }
142057 }
142058
142059 /* If table pTab has not been used in a way that would benefit from
142060 ** having analysis statistics during the current session, then skip it,
142061 ** unless the 0x10000 MASK bit is set. */
142062 if( (pTab->tabFlags & TF_MaybeReanalyze0x00000100)!=0 ){
142063 /* Check for size change if stat1 has been used for a query */
142064 }else if( opMask & 0x10000 ){
142065 /* Check for size change if 0x10000 is set */
142066 }else if( pTab->pIndex!=0 && szThreshold<0 ){
142067 /* Do analysis if unanalyzed indexes exists */
142068 }else{
142069 /* Otherwise, we can skip this table */
142070 continue;
142071 }
142072
142073 nCheck++;
142074 if( nCheck==2 ){
142075 /* If ANALYZE might be invoked two or more times, hold a write
142076 ** transaction for efficiency */
142077 sqlite3BeginWriteOperation(pParse, 0, iDb);
142078 }
142079 nBtree += nIndex+1;
142080
142081 /* Reanalyze if the table is 10 times larger or smaller than
142082 ** the last analysis. Unconditional reanalysis if there are
142083 ** unanalyzed indexes. */
142084 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead102);
142085 if( szThreshold>=0 ){
142086 const LogEst iRange = 33; /* 10x size change */
142087 sqlite3VdbeAddOp4Int(v, OP_IfSizeBetween33, iTabCur,
142088 sqlite3VdbeCurrentAddr(v)+2+(opMask&1),
142089 szThreshold>=iRange ? szThreshold-iRange : -1,
142090 szThreshold+iRange);
142091 VdbeCoverage(v);
142092 }else{
142093 sqlite3VdbeAddOp2(v, OP_Rewind36, iTabCur,
142094 sqlite3VdbeCurrentAddr(v)+2+(opMask&1));
142095 VdbeCoverage(v);
142096 }
142097 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
142098 db->aDb[iDb].zDbSName, pTab->zName);
142099 if( opMask & 0x01 ){
142100 int r1 = sqlite3GetTempReg(pParse);
142101 sqlite3VdbeAddOp4(v, OP_String8118, 0, r1, 0, zSubSql, P4_DYNAMIC(-6));
142102 sqlite3VdbeAddOp2(v, OP_ResultRow84, r1, 1);
142103 }else{
142104 sqlite3VdbeAddOp4(v, OP_SqlExec148, nLimit ? 0x02 : 00, nLimit, 0,
142105 zSubSql, P4_DYNAMIC(-6));
142106 }
142107 }
142108 }
142109 sqlite3VdbeAddOp0(v, OP_Expire166);
142110
142111 /* In a schema with a large number of tables and indexes, scale back
142112 ** the analysis_limit to avoid excess run-time in the worst case.
142113 */
142114 if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
142115 int iAddr, iEnd;
142116 VdbeOp *aOp;
142117 nLimit = 100*nLimit/nBtree;
142118 if( nLimit<100 ) nLimit = 100;
142119 aOp = sqlite3VdbeGetOp(v, 0);
142120 iEnd = sqlite3VdbeCurrentAddr(v);
142121 for(iAddr=0; iAddr<iEnd; iAddr++){
142122 if( aOp[iAddr].opcode==OP_SqlExec148 ) aOp[iAddr].p2 = nLimit;
142123 }
142124 }
142125 break;
142126 }
142127
142128 /*
142129 ** PRAGMA busy_timeout
142130 ** PRAGMA busy_timeout = N
142131 **
142132 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
142133 ** if one is set. If no busy handler or a different busy handler is set
142134 ** then 0 is returned. Setting the busy_timeout to 0 or negative
142135 ** disables the timeout.
142136 */
142137 /*case PragTyp_BUSY_TIMEOUT*/ default: {
142138 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT )((void) (0));
142139 if( zRight ){
142140 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
142141 }
142142 returnSingleInt(v, db->busyTimeout);
142143 break;
142144 }
142145
142146 /*
142147 ** PRAGMA soft_heap_limit
142148 ** PRAGMA soft_heap_limit = N
142149 **
142150 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
142151 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
142152 ** specified and is a non-negative integer.
142153 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
142154 ** returns the same integer that would be returned by the
142155 ** sqlite3_soft_heap_limit64(-1) C-language function.
142156 */
142157 case PragTyp_SOFT_HEAP_LIMIT35: {
142158 sqlite3_int64 N;
142159 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
142160 sqlite3_soft_heap_limit64(N);
142161 }
142162 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
142163 break;
142164 }
142165
142166 /*
142167 ** PRAGMA hard_heap_limit
142168 ** PRAGMA hard_heap_limit = N
142169 **
142170 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
142171 ** limit. The hard heap limit can be activated or lowered by this
142172 ** pragma, but not raised or deactivated. Only the
142173 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
142174 ** the hard heap limit. This allows an application to set a heap limit
142175 ** constraint that cannot be relaxed by an untrusted SQL script.
142176 */
142177 case PragTyp_HARD_HEAP_LIMIT18: {
142178 sqlite3_int64 N;
142179 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
142180 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
142181 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
142182 }
142183 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
142184 break;
142185 }
142186
142187 /*
142188 ** PRAGMA threads
142189 ** PRAGMA threads = N
142190 **
142191 ** Configure the maximum number of worker threads. Return the new
142192 ** maximum, which might be less than requested.
142193 */
142194 case PragTyp_THREADS41: {
142195 sqlite3_int64 N;
142196 if( zRight
142197 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0
142198 && N>=0
142199 ){
142200 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, (int)(N&0x7fffffff));
142201 }
142202 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, -1));
142203 break;
142204 }
142205
142206 /*
142207 ** PRAGMA analysis_limit
142208 ** PRAGMA analysis_limit = N
142209 **
142210 ** Configure the maximum number of rows that ANALYZE will examine
142211 ** in each index that it looks at. Return the new limit.
142212 */
142213 case PragTyp_ANALYSIS_LIMIT1: {
142214 sqlite3_int64 N;
142215 if( zRight
142216 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 /* IMP: R-40975-20399 */
142217 && N>=0
142218 ){
142219 db->nAnalysisLimit = (int)(N&0x7fffffff);
142220 }
142221 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
142222 break;
142223 }
142224
142225#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
142226 /*
142227 ** Report the current state of file logs for all databases
142228 */
142229 case PragTyp_LOCK_STATUS44: {
142230 static const char *const azLockName[] = {
142231 "unlocked", "shared", "reserved", "pending", "exclusive"
142232 };
142233 int i;
142234 pParse->nMem = 2;
142235 for(i=0; i<db->nDb; i++){
142236 Btree *pBt;
142237 const char *zState = "unknown";
142238 int j;
142239 if( db->aDb[i].zDbSName==0 ) continue;
142240 pBt = db->aDb[i].pBt;
142241 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
142242 zState = "closed";
142243 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
142244 SQLITE_FCNTL_LOCKSTATE1, &j)==SQLITE_OK0 ){
142245 zState = azLockName[j];
142246 }
142247 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
142248 }
142249 break;
142250 }
142251#endif
142252
142253#if defined(SQLITE_ENABLE_CEROD)
142254 case PragTyp_ACTIVATE_EXTENSIONS0: if( zRight ){
142255 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, "cerod-", 6)==0 ){
142256 sqlite3_activate_cerod(&zRight[6]);
142257 }
142258 }
142259 break;
142260#endif
142261
142262 } /* End of the PRAGMA switch */
142263
142264 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
142265 ** purpose is to execute assert() statements to verify that if the
142266 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
142267 ** to the PRAGMA, the implementation has not added any OP_ResultRow
142268 ** instructions to the VM. */
142269 if( (pPragma->mPragFlg & PragFlg_NoColumns10x04) && zRight ){
142270 sqlite3VdbeVerifyNoResultRow(v);
142271 }
142272
142273pragma_out:
142274 sqlite3DbFree(db, zLeft);
142275 sqlite3DbFree(db, zRight);
142276}
142277#ifndef SQLITE_OMIT_VIRTUALTABLE
142278/*****************************************************************************
142279** Implementation of an eponymous virtual table that runs a pragma.
142280**
142281*/
142282typedef struct PragmaVtab PragmaVtab;
142283typedef struct PragmaVtabCursor PragmaVtabCursor;
142284struct PragmaVtab {
142285 sqlite3_vtab base; /* Base class. Must be first */
142286 sqlite3 *db; /* The database connection to which it belongs */
142287 const PragmaName *pName; /* Name of the pragma */
142288 u8 nHidden; /* Number of hidden columns */
142289 u8 iHidden; /* Index of the first hidden column */
142290};
142291struct PragmaVtabCursor {
142292 sqlite3_vtab_cursor base; /* Base class. Must be first */
142293 sqlite3_stmt *pPragma; /* The pragma statement to run */
142294 sqlite_int64 iRowid; /* Current rowid */
142295 char *azArg[2]; /* Value of the argument and schema */
142296};
142297
142298/*
142299** Pragma virtual table module xConnect method.
142300*/
142301static int pragmaVtabConnect(
142302 sqlite3 *db,
142303 void *pAux,
142304 int argc, const char *const*argv,
142305 sqlite3_vtab **ppVtab,
142306 char **pzErr
142307){
142308 const PragmaName *pPragma = (const PragmaName*)pAux;
142309 PragmaVtab *pTab = 0;
142310 int rc;
142311 int i, j;
142312 char cSep = '(';
142313 StrAccum acc;
142314 char zBuf[200];
142315
142316 UNUSED_PARAMETER(argc)(void)(argc);
142317 UNUSED_PARAMETER(argv)(void)(argv);
142318 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
142319 sqlite3_str_appendall(&acc, "CREATE TABLE x");
142320 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
142321 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
142322 cSep = ',';
142323 }
142324 if( i==0 ){
142325 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
142326 i++;
142327 }
142328 j = 0;
142329 if( pPragma->mPragFlg & PragFlg_Result10x20 ){
142330 sqlite3_str_appendall(&acc, ",arg HIDDEN");
142331 j++;
142332 }
142333 if( pPragma->mPragFlg & (PragFlg_SchemaOpt0x40|PragFlg_SchemaReq0x80) ){
142334 sqlite3_str_appendall(&acc, ",schema HIDDEN");
142335 j++;
142336 }
142337 sqlite3_str_append(&acc, ")", 1);
142338 sqlite3StrAccumFinish(&acc);
142339 assert( strlen(zBuf) < sizeof(zBuf)-1 )((void) (0));
142340 rc = sqlite3_declare_vtab(db, zBuf);
142341 if( rc==SQLITE_OK0 ){
142342 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
142343 if( pTab==0 ){
142344 rc = SQLITE_NOMEM7;
142345 }else{
142346 memset(pTab, 0, sizeof(PragmaVtab));
142347 pTab->pName = pPragma;
142348 pTab->db = db;
142349 pTab->iHidden = i;
142350 pTab->nHidden = j;
142351 }
142352 }else{
142353 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
142354 }
142355
142356 *ppVtab = (sqlite3_vtab*)pTab;
142357 return rc;
142358}
142359
142360/*
142361** Pragma virtual table module xDisconnect method.
142362*/
142363static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
142364 PragmaVtab *pTab = (PragmaVtab*)pVtab;
142365 sqlite3_free(pTab);
142366 return SQLITE_OK0;
142367}
142368
142369/* Figure out the best index to use to search a pragma virtual table.
142370**
142371** There are not really any index choices. But we want to encourage the
142372** query planner to give == constraints on as many hidden parameters as
142373** possible, and especially on the first hidden parameter. So return a
142374** high cost if hidden parameters are unconstrained.
142375*/
142376static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
142377 PragmaVtab *pTab = (PragmaVtab*)tab;
142378 const struct sqlite3_index_constraint *pConstraint;
142379 int i, j;
142380 int seen[2];
142381
142382 pIdxInfo->estimatedCost = (double)1;
142383 if( pTab->nHidden==0 ){ return SQLITE_OK0; }
142384 pConstraint = pIdxInfo->aConstraint;
142385 seen[0] = 0;
142386 seen[1] = 0;
142387 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
142388 if( pConstraint->iColumn < pTab->iHidden ) continue;
142389 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
142390 if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT19;
142391 j = pConstraint->iColumn - pTab->iHidden;
142392 assert( j < 2 )((void) (0));
142393 seen[j] = i+1;
142394 }
142395 if( seen[0]==0 ){
142396 pIdxInfo->estimatedCost = (double)2147483647;
142397 pIdxInfo->estimatedRows = 2147483647;
142398 return SQLITE_OK0;
142399 }
142400 j = seen[0]-1;
142401 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
142402 pIdxInfo->aConstraintUsage[j].omit = 1;
142403 pIdxInfo->estimatedCost = (double)20;
142404 pIdxInfo->estimatedRows = 20;
142405 if( seen[1] ){
142406 j = seen[1]-1;
142407 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
142408 pIdxInfo->aConstraintUsage[j].omit = 1;
142409 }
142410 return SQLITE_OK0;
142411}
142412
142413/* Create a new cursor for the pragma virtual table */
142414static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
142415 PragmaVtabCursor *pCsr;
142416 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
142417 if( pCsr==0 ) return SQLITE_NOMEM7;
142418 memset(pCsr, 0, sizeof(PragmaVtabCursor));
142419 pCsr->base.pVtab = pVtab;
142420 *ppCursor = &pCsr->base;
142421 return SQLITE_OK0;
142422}
142423
142424/* Clear all content from pragma virtual table cursor. */
142425static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
142426 int i;
142427 sqlite3_finalize(pCsr->pPragma);
142428 pCsr->pPragma = 0;
142429 pCsr->iRowid = 0;
142430 for(i=0; i<ArraySize(pCsr->azArg)((int)(sizeof(pCsr->azArg)/sizeof(pCsr->azArg[0]))); i++){
142431 sqlite3_free(pCsr->azArg[i]);
142432 pCsr->azArg[i] = 0;
142433 }
142434}
142435
142436/* Close a pragma virtual table cursor */
142437static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
142438 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
142439 pragmaVtabCursorClear(pCsr);
142440 sqlite3_free(pCsr);
142441 return SQLITE_OK0;
142442}
142443
142444/* Advance the pragma virtual table cursor to the next row */
142445static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
142446 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
142447 int rc = SQLITE_OK0;
142448
142449 /* Increment the xRowid value */
142450 pCsr->iRowid++;
142451 assert( pCsr->pPragma )((void) (0));
142452 if( SQLITE_ROW100!=sqlite3_step(pCsr->pPragma) ){
142453 rc = sqlite3_finalize(pCsr->pPragma);
142454 pCsr->pPragma = 0;
142455 pragmaVtabCursorClear(pCsr);
142456 }
142457 return rc;
142458}
142459
142460/*
142461** Pragma virtual table module xFilter method.
142462*/
142463static int pragmaVtabFilter(
142464 sqlite3_vtab_cursor *pVtabCursor,
142465 int idxNum, const char *idxStr,
142466 int argc, sqlite3_value **argv
142467){
142468 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
142469 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
142470 int rc;
142471 int i, j;
142472 StrAccum acc;
142473 char *zSql;
142474
142475 UNUSED_PARAMETER(idxNum)(void)(idxNum);
142476 UNUSED_PARAMETER(idxStr)(void)(idxStr);
142477 pragmaVtabCursorClear(pCsr);
142478 j = (pTab->pName->mPragFlg & PragFlg_Result10x20)!=0 ? 0 : 1;
142479 for(i=0; i<argc; i++, j++){
142480 const char *zText = (const char*)sqlite3_value_text(argv[i]);
142481 assert( j<ArraySize(pCsr->azArg) )((void) (0));
142482 assert( pCsr->azArg[j]==0 )((void) (0));
142483 if( zText ){
142484 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
142485 if( pCsr->azArg[j]==0 ){
142486 return SQLITE_NOMEM7;
142487 }
142488 }
142489 }
142490 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH1]);
142491 sqlite3_str_appendall(&acc, "PRAGMA ");
142492 if( pCsr->azArg[1] ){
142493 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
142494 }
142495 sqlite3_str_appendall(&acc, pTab->pName->zName);
142496 if( pCsr->azArg[0] ){
142497 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
142498 }
142499 zSql = sqlite3StrAccumFinish(&acc);
142500 if( zSql==0 ) return SQLITE_NOMEM7;
142501 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
142502 sqlite3_free(zSql);
142503 if( rc!=SQLITE_OK0 ){
142504 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
142505 return rc;
142506 }
142507 return pragmaVtabNext(pVtabCursor);
142508}
142509
142510/*
142511** Pragma virtual table module xEof method.
142512*/
142513static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
142514 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
142515 return (pCsr->pPragma==0);
142516}
142517
142518/* The xColumn method simply returns the corresponding column from
142519** the PRAGMA.
142520*/
142521static int pragmaVtabColumn(
142522 sqlite3_vtab_cursor *pVtabCursor,
142523 sqlite3_context *ctx,
142524 int i
142525){
142526 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
142527 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
142528 if( i<pTab->iHidden ){
142529 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
142530 }else{
142531 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
142532 }
142533 return SQLITE_OK0;
142534}
142535
142536/*
142537** Pragma virtual table module xRowid method.
142538*/
142539static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
142540 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
142541 *p = pCsr->iRowid;
142542 return SQLITE_OK0;
142543}
142544
142545/* The pragma virtual table object */
142546static const sqlite3_module pragmaVtabModule = {
142547 0, /* iVersion */
142548 0, /* xCreate - create a table */
142549 pragmaVtabConnect, /* xConnect - connect to an existing table */
142550 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
142551 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
142552 0, /* xDestroy - Drop a table */
142553 pragmaVtabOpen, /* xOpen - open a cursor */
142554 pragmaVtabClose, /* xClose - close a cursor */
142555 pragmaVtabFilter, /* xFilter - configure scan constraints */
142556 pragmaVtabNext, /* xNext - advance a cursor */
142557 pragmaVtabEof, /* xEof */
142558 pragmaVtabColumn, /* xColumn - read data */
142559 pragmaVtabRowid, /* xRowid - read data */
142560 0, /* xUpdate - write data */
142561 0, /* xBegin - begin transaction */
142562 0, /* xSync - sync transaction */
142563 0, /* xCommit - commit transaction */
142564 0, /* xRollback - rollback transaction */
142565 0, /* xFindFunction - function overloading */
142566 0, /* xRename - rename the table */
142567 0, /* xSavepoint */
142568 0, /* xRelease */
142569 0, /* xRollbackTo */
142570 0, /* xShadowName */
142571 0 /* xIntegrity */
142572};
142573
142574/*
142575** Check to see if zTabName is really the name of a pragma. If it is,
142576** then register an eponymous virtual table for that pragma and return
142577** a pointer to the Module object for the new virtual table.
142578*/
142579SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
142580 const PragmaName *pName;
142581 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 )((void) (0));
142582 pName = pragmaLocate(zName+7);
142583 if( pName==0 ) return 0;
142584 if( (pName->mPragFlg & (PragFlg_Result00x10|PragFlg_Result10x20))==0 ) return 0;
142585 assert( sqlite3HashFind(&db->aModule, zName)==0 )((void) (0));
142586 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
142587}
142588
142589#endif /* SQLITE_OMIT_VIRTUALTABLE */
142590
142591#endif /* SQLITE_OMIT_PRAGMA */
142592
142593/************** End of pragma.c **********************************************/
142594/************** Begin file prepare.c *****************************************/
142595/*
142596** 2005 May 25
142597**
142598** The author disclaims copyright to this source code. In place of
142599** a legal notice, here is a blessing:
142600**
142601** May you do good and not evil.
142602** May you find forgiveness for yourself and forgive others.
142603** May you share freely, never taking more than you give.
142604**
142605*************************************************************************
142606** This file contains the implementation of the sqlite3_prepare()
142607** interface, and routines that contribute to loading the database schema
142608** from disk.
142609*/
142610/* #include "sqliteInt.h" */
142611
142612/*
142613** Fill the InitData structure with an error message that indicates
142614** that the database is corrupt.
142615*/
142616static void corruptSchema(
142617 InitData *pData, /* Initialization context */
142618 char **azObj, /* Type and name of object being parsed */
142619 const char *zExtra /* Error information */
142620){
142621 sqlite3 *db = pData->db;
142622 if( db->mallocFailed ){
142623 pData->rc = SQLITE_NOMEM_BKPT7;
142624 }else if( pData->pzErrMsg[0]!=0 ){
142625 /* A error message has already been generated. Do not overwrite it */
142626 }else if( pData->mInitFlags & (INITFLAG_AlterMask0x0003) ){
142627 static const char *azAlterType[] = {
142628 "rename",
142629 "drop column",
142630 "add column"
142631 };
142632 *pData->pzErrMsg = sqlite3MPrintf(db,
142633 "error in %s %s after %s: %s", azObj[0], azObj[1],
142634 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask0x0003)-1],
142635 zExtra
142636 );
142637 pData->rc = SQLITE_ERROR1;
142638 }else if( db->flags & SQLITE_WriteSchema0x00000001 ){
142639 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(142639);
142640 }else{
142641 char *z;
142642 const char *zObj = azObj[1] ? azObj[1] : "?";
142643 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
142644 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
142645 *pData->pzErrMsg = z;
142646 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(142646);
142647 }
142648}
142649
142650/*
142651** Check to see if any sibling index (another index on the same table)
142652** of pIndex has the same root page number, and if it does, return true.
142653** This would indicate a corrupt schema.
142654*/
142655SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
142656 Index *p;
142657 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
142658 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
142659 }
142660 return 0;
142661}
142662
142663/* forward declaration */
142664static int sqlite3Prepare(
142665 sqlite3 *db, /* Database handle. */
142666 const char *zSql, /* UTF-8 encoded SQL statement. */
142667 int nBytes, /* Length of zSql in bytes. */
142668 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
142669 Vdbe *pReprepare, /* VM being reprepared */
142670 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
142671 const char **pzTail /* OUT: End of parsed string */
142672);
142673
142674
142675/*
142676** This is the callback routine for the code that initializes the
142677** database. See sqlite3Init() below for additional information.
142678** This routine is also called from the OP_ParseSchema opcode of the VDBE.
142679**
142680** Each callback contains the following information:
142681**
142682** argv[0] = type of object: "table", "index", "trigger", or "view".
142683** argv[1] = name of thing being created
142684** argv[2] = associated table if an index or trigger
142685** argv[3] = root page number for table or index. 0 for trigger or view.
142686** argv[4] = SQL text for the CREATE statement.
142687**
142688*/
142689SQLITE_PRIVATEstatic int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
142690 InitData *pData = (InitData*)pInit;
142691 sqlite3 *db = pData->db;
142692 int iDb = pData->iDb;
142693
142694 assert( argc==5 )((void) (0));
142695 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
142696 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
142697 db->mDbFlags |= DBFLAG_EncodingFixed0x0040;
142698 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
142699 pData->nInitRow++;
142700 if( db->mallocFailed ){
142701 corruptSchema(pData, argv, 0);
142702 return 1;
142703 }
142704
142705 assert( iDb>=0 && iDb<db->nDb )((void) (0));
142706 if( argv[3]==0 ){
142707 corruptSchema(pData, argv, 0);
142708 }else if( argv[4]
142709 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
142710 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
142711 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
142712 ** But because db->init.busy is set to 1, no VDBE code is generated
142713 ** or executed. All the parser does is build the internal data
142714 ** structures that describe the table, index, or view.
142715 **
142716 ** No other valid SQL statement, other than the variable CREATE statements,
142717 ** can begin with the letters "C" and "R". Thus, it is not possible run
142718 ** any other kind of statement while parsing the schema, even a corrupt
142719 ** schema.
142720 */
142721 int rc;
142722 u8 saved_iDb = db->init.iDb;
142723 sqlite3_stmt *pStmt;
142724 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
142725
142726 assert( db->init.busy )((void) (0));
142727 db->init.iDb = iDb;
142728 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
142729 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
142730 ){
142731 if( sqlite3Config.bExtraSchemaChecks ){
142732 corruptSchema(pData, argv, "invalid rootpage");
142733 }
142734 }
142735 db->init.orphanTrigger = 0;
142736 db->init.azInit = (const char**)argv;
142737 pStmt = 0;
142738 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
142739 rc = db->errCode;
142740 assert( (rc&0xFF)==(rcp&0xFF) )((void) (0));
142741 db->init.iDb = saved_iDb;
142742 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
142743 if( SQLITE_OK0!=rc ){
142744 if( db->init.orphanTrigger ){
142745 assert( iDb==1 )((void) (0));
142746 }else{
142747 if( rc > pData->rc ) pData->rc = rc;
142748 if( rc==SQLITE_NOMEM7 ){
142749 sqlite3OomFault(db);
142750 }else if( rc!=SQLITE_INTERRUPT9 && (rc&0xFF)!=SQLITE_LOCKED6 ){
142751 corruptSchema(pData, argv, sqlite3_errmsg(db));
142752 }
142753 }
142754 }
142755 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
142756 sqlite3_finalize(pStmt);
142757 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
142758 corruptSchema(pData, argv, 0);
142759 }else{
142760 /* If the SQL column is blank it means this is an index that
142761 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
142762 ** constraint for a CREATE TABLE. The index should have already
142763 ** been created when we processed the CREATE TABLE. All we have
142764 ** to do here is record the root page number for that index.
142765 */
142766 Index *pIndex;
142767 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
142768 if( pIndex==0 ){
142769 corruptSchema(pData, argv, "orphan index");
142770 }else
142771 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
142772 || pIndex->tnum<2
142773 || pIndex->tnum>pData->mxPage
142774 || sqlite3IndexHasDuplicateRootPage(pIndex)
142775 ){
142776 if( sqlite3Config.bExtraSchemaChecks ){
142777 corruptSchema(pData, argv, "invalid rootpage");
142778 }
142779 }
142780 }
142781 return 0;
142782}
142783
142784/*
142785** Attempt to read the database schema and initialize internal
142786** data structures for a single database file. The index of the
142787** database file is given by iDb. iDb==0 is used for the main
142788** database. iDb==1 should never be used. iDb>=2 is used for
142789** auxiliary databases. Return one of the SQLITE_ error codes to
142790** indicate success or failure.
142791*/
142792SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
142793 int rc;
142794 int i;
142795#ifndef SQLITE_OMIT_DEPRECATED1
142796 int size;
142797#endif
142798 Db *pDb;
142799 char const *azArg[6];
142800 int meta[5];
142801 InitData initData;
142802 const char *zSchemaTabName;
142803 int openedTransaction = 0;
142804 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed0x0040) | ~DBFLAG_EncodingFixed0x0040);
142805
142806 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 )((void) (0));
142807 assert( iDb>=0 && iDb<db->nDb )((void) (0));
142808 assert( db->aDb[iDb].pSchema )((void) (0));
142809 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
142810 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
142811
142812 db->init.busy = 1;
142813
142814 /* Construct the in-memory representation schema tables (sqlite_schema or
142815 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
142816 ** table name will be inserted automatically by the parser so we can just
142817 ** use the abbreviation "x" here. The parser will also automatically tag
142818 ** the schema table as read-only. */
142819 azArg[0] = "table";
142820 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
142821 azArg[2] = azArg[1];
142822 azArg[3] = "1";
142823 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
142824 "rootpage int,sql text)";
142825 azArg[5] = 0;
142826 initData.db = db;
142827 initData.iDb = iDb;
142828 initData.rc = SQLITE_OK0;
142829 initData.pzErrMsg = pzErrMsg;
142830 initData.mInitFlags = mFlags;
142831 initData.nInitRow = 0;
142832 initData.mxPage = 0;
142833 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
142834 db->mDbFlags &= mask;
142835 if( initData.rc ){
142836 rc = initData.rc;
142837 goto error_out;
142838 }
142839
142840 /* Create a cursor to hold the database open
142841 */
142842 pDb = &db->aDb[iDb];
142843 if( pDb->pBt==0 ){
142844 assert( iDb==1 )((void) (0));
142845 DbSetProperty(db, 1, DB_SchemaLoaded)(db)->aDb[1].pSchema->schemaFlags|=(0x0001);
142846 rc = SQLITE_OK0;
142847 goto error_out;
142848 }
142849
142850 /* If there is not already a read-only (or read-write) transaction opened
142851 ** on the b-tree database, open one now. If a transaction is opened, it
142852 ** will be closed before this function returns. */
142853 sqlite3BtreeEnter(pDb->pBt);
142854 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE0 ){
142855 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
142856 if( rc!=SQLITE_OK0 ){
142857 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
142858 goto initone_error_out;
142859 }
142860 openedTransaction = 1;
142861 }
142862
142863 /* Get the database meta information.
142864 **
142865 ** Meta values are as follows:
142866 ** meta[0] Schema cookie. Changes with each schema change.
142867 ** meta[1] File format of schema layer.
142868 ** meta[2] Size of the page cache.
142869 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
142870 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
142871 ** meta[5] User version
142872 ** meta[6] Incremental vacuum mode
142873 ** meta[7] unused
142874 ** meta[8] unused
142875 ** meta[9] unused
142876 **
142877 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
142878 ** the possible values of meta[4].
142879 */
142880 for(i=0; i<ArraySize(meta)((int)(sizeof(meta)/sizeof(meta[0]))); i++){
142881 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
142882 }
142883 if( (db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
142884 memset(meta, 0, sizeof(meta));
142885 }
142886 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION1-1];
142887
142888 /* If opening a non-empty database, check the text encoding. For the
142889 ** main database, set sqlite3.enc to the encoding of the main database.
142890 ** For an attached db, it is an error if the encoding is not the same
142891 ** as sqlite3.enc.
142892 */
142893 if( meta[BTREE_TEXT_ENCODING5-1] ){ /* text encoding */
142894 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed0x0040)==0 ){
142895 u8 encoding;
142896#ifndef SQLITE_OMIT_UTF16
142897 /* If opening the main database, set ENC(db). */
142898 encoding = (u8)meta[BTREE_TEXT_ENCODING5-1] & 3;
142899 if( encoding==0 ) encoding = SQLITE_UTF81;
142900#else
142901 encoding = SQLITE_UTF81;
142902#endif
142903 if( db->nVdbeActive>0 && encoding!=ENC(db)((db)->enc)
142904 && (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
142905 ){
142906 rc = SQLITE_LOCKED6;
142907 goto initone_error_out;
142908 }else{
142909 sqlite3SetTextEncoding(db, encoding);
142910 }
142911 }else{
142912 /* If opening an attached database, the encoding much match ENC(db) */
142913 if( (meta[BTREE_TEXT_ENCODING5-1] & 3)!=ENC(db)((db)->enc) ){
142914 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
142915 " text encoding as main database");
142916 rc = SQLITE_ERROR1;
142917 goto initone_error_out;
142918 }
142919 }
142920 }
142921 pDb->pSchema->enc = ENC(db)((db)->enc);
142922
142923 if( pDb->pSchema->cache_size==0 ){
142924#ifndef SQLITE_OMIT_DEPRECATED1
142925 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE3-1]);
142926 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE128; }
142927 pDb->pSchema->cache_size = size;
142928#else
142929 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE128;
142930#endif
142931 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
142932 }
142933
142934 /*
142935 ** file_format==1 Version 3.0.0.
142936 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
142937 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
142938 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
142939 */
142940 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT2-1];
142941 if( pDb->pSchema->file_format==0 ){
142942 pDb->pSchema->file_format = 1;
142943 }
142944 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT4 ){
142945 sqlite3SetString(pzErrMsg, db, "unsupported file format");
142946 rc = SQLITE_ERROR1;
142947 goto initone_error_out;
142948 }
142949
142950 /* Ticket #2804: When we open a database in the newer file format,
142951 ** clear the legacy_file_format pragma flag so that a VACUUM will
142952 ** not downgrade the database and thus invalidate any descending
142953 ** indices that the user might have created.
142954 */
142955 if( iDb==0 && meta[BTREE_FILE_FORMAT2-1]>=4 ){
142956 db->flags &= ~(u64)SQLITE_LegacyFileFmt0x00000002;
142957 }
142958
142959 /* Read the schema information out of the schema tables
142960 */
142961 assert( db->init.busy )((void) (0));
142962 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
142963 {
142964 char *zSql;
142965 zSql = sqlite3MPrintf(db,
142966 "SELECT*FROM\"%w\".%s ORDER BY rowid",
142967 db->aDb[iDb].zDbSName, zSchemaTabName);
142968#ifndef SQLITE_OMIT_AUTHORIZATION
142969 {
142970 sqlite3_xauth xAuth;
142971 xAuth = db->xAuth;
142972 db->xAuth = 0;
142973#endif
142974 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
142975#ifndef SQLITE_OMIT_AUTHORIZATION
142976 db->xAuth = xAuth;
142977 }
142978#endif
142979 if( rc==SQLITE_OK0 ) rc = initData.rc;
142980 sqlite3DbFree(db, zSql);
142981#ifndef SQLITE_OMIT_ANALYZE
142982 if( rc==SQLITE_OK0 ){
142983 sqlite3AnalysisLoad(db, iDb);
142984 }
142985#endif
142986 }
142987 assert( pDb == &(db->aDb[iDb]) )((void) (0));
142988 if( db->mallocFailed ){
142989 rc = SQLITE_NOMEM_BKPT7;
142990 sqlite3ResetAllSchemasOfConnection(db);
142991 pDb = &db->aDb[iDb];
142992 }else
142993 if( rc==SQLITE_OK0 || ((db->flags&SQLITE_NoSchemaError0x08000000) && rc!=SQLITE_NOMEM7)){
142994 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
142995 ** the schema loaded, even if errors (other than OOM) occurred. In
142996 ** this situation the current sqlite3_prepare() operation will fail,
142997 ** but the following one will attempt to compile the supplied statement
142998 ** against whatever subset of the schema was loaded before the error
142999 ** occurred.
143000 **
143001 ** The primary purpose of this is to allow access to the sqlite_schema
143002 ** table even when its contents have been corrupted.
143003 */
143004 DbSetProperty(db, iDb, DB_SchemaLoaded)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0001);
143005 rc = SQLITE_OK0;
143006 }
143007
143008 /* Jump here for an error that occurs after successfully allocating
143009 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
143010 ** before that point, jump to error_out.
143011 */
143012initone_error_out:
143013 if( openedTransaction ){
143014 sqlite3BtreeCommit(pDb->pBt);
143015 }
143016 sqlite3BtreeLeave(pDb->pBt);
143017
143018error_out:
143019 if( rc ){
143020 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
143021 sqlite3OomFault(db);
143022 }
143023 sqlite3ResetOneSchema(db, iDb);
143024 }
143025 db->init.busy = 0;
143026 return rc;
143027}
143028
143029/*
143030** Initialize all database files - the main database file, the file
143031** used to store temporary tables, and any additional database files
143032** created using ATTACH statements. Return a success code. If an
143033** error occurs, write an error message into *pzErrMsg.
143034**
143035** After a database is initialized, the DB_SchemaLoaded bit is set
143036** bit is set in the flags field of the Db structure.
143037*/
143038SQLITE_PRIVATEstatic int sqlite3Init(sqlite3 *db, char **pzErrMsg){
143039 int i, rc;
143040 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange0x0001);
143041
143042 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143043 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) )((void) (0));
143044 assert( db->init.busy==0 )((void) (0));
143045 ENC(db)((db)->enc) = SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc);
143046 assert( db->nDb>0 )((void) (0));
143047 /* Do the main schema first */
143048 if( !DbHasProperty(db, 0, DB_SchemaLoaded)(((db)->aDb[0].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
143049 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
143050 if( rc ) return rc;
143051 }
143052 /* All other schemas after the main schema. The "temp" schema must be last */
143053 for(i=db->nDb-1; i>0; i--){
143054 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) )((void) (0));
143055 if( !DbHasProperty(db, i, DB_SchemaLoaded)(((db)->aDb[i].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
143056 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
143057 if( rc ) return rc;
143058 }
143059 }
143060 if( commit_internal ){
143061 sqlite3CommitInternalChanges(db);
143062 }
143063 return SQLITE_OK0;
143064}
143065
143066/*
143067** This routine is a no-op if the database schema is already initialized.
143068** Otherwise, the schema is loaded. An error code is returned.
143069*/
143070SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse){
143071 int rc = SQLITE_OK0;
143072 sqlite3 *db = pParse->db;
143073 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143074 if( !db->init.busy ){
143075 rc = sqlite3Init(db, &pParse->zErrMsg);
143076 if( rc!=SQLITE_OK0 ){
143077 pParse->rc = rc;
143078 pParse->nErr++;
143079 }else if( db->noSharedCache ){
143080 db->mDbFlags |= DBFLAG_SchemaKnownOk0x0010;
143081 }
143082 }
143083 return rc;
143084}
143085
143086
143087/*
143088** Check schema cookies in all databases. If any cookie is out
143089** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
143090** make no changes to pParse->rc.
143091*/
143092static void schemaIsValid(Parse *pParse){
143093 sqlite3 *db = pParse->db;
143094 int iDb;
143095 int rc;
143096 int cookie;
143097
143098 assert( pParse->checkSchema )((void) (0));
143099 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143100 for(iDb=0; iDb<db->nDb; iDb++){
143101 int openedTransaction = 0; /* True if a transaction is opened */
143102 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
143103 if( pBt==0 ) continue;
143104
143105 /* If there is not already a read-only (or read-write) transaction opened
143106 ** on the b-tree database, open one now. If a transaction is opened, it
143107 ** will be closed immediately after reading the meta-value. */
143108 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE0 ){
143109 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
143110 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
143111 sqlite3OomFault(db);
143112 pParse->rc = SQLITE_NOMEM7;
143113 }
143114 if( rc!=SQLITE_OK0 ) return;
143115 openedTransaction = 1;
143116 }
143117
143118 /* Read the schema cookie from the database. If it does not match the
143119 ** value stored as part of the in-memory schema representation,
143120 ** set Parse.rc to SQLITE_SCHEMA. */
143121 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION1, (u32 *)&cookie);
143122 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
143123 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
143124 if( DbHasProperty(db, iDb, DB_SchemaLoaded)(((db)->aDb[iDb].pSchema->schemaFlags&(0x0001))==(0x0001
))
) pParse->rc = SQLITE_SCHEMA17;
143125 sqlite3ResetOneSchema(db, iDb);
143126 }
143127
143128 /* Close the transaction, if one was opened. */
143129 if( openedTransaction ){
143130 sqlite3BtreeCommit(pBt);
143131 }
143132 }
143133}
143134
143135/*
143136** Convert a schema pointer into the iDb index that indicates
143137** which database file in db->aDb[] the schema refers to.
143138**
143139** If the same database is attached more than once, the first
143140** attached database is returned.
143141*/
143142SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
143143 int i = -32768;
143144
143145 /* If pSchema is NULL, then return -32768. This happens when code in
143146 ** expr.c is trying to resolve a reference to a transient table (i.e. one
143147 ** created by a sub-select). In this case the return value of this
143148 ** function should never be used.
143149 **
143150 ** We return -32768 instead of the more usual -1 simply because using
143151 ** -32768 as the incorrect index into db->aDb[] is much
143152 ** more likely to cause a segfault than -1 (of course there are assert()
143153 ** statements too, but it never hurts to play the odds) and
143154 ** -32768 will still fit into a 16-bit signed integer.
143155 */
143156 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143157 if( pSchema ){
143158 for(i=0; 1; i++){
143159 assert( i<db->nDb )((void) (0));
143160 if( db->aDb[i].pSchema==pSchema ){
143161 break;
143162 }
143163 }
143164 assert( i>=0 && i<db->nDb )((void) (0));
143165 }
143166 return i;
143167}
143168
143169/*
143170** Free all memory allocations in the pParse object
143171*/
143172SQLITE_PRIVATEstatic void sqlite3ParseObjectReset(Parse *pParse){
143173 sqlite3 *db = pParse->db;
143174 assert( db!=0 )((void) (0));
143175 assert( db->pParse==pParse )((void) (0));
143176 assert( pParse->nested==0 )((void) (0));
143177#ifndef SQLITE_OMIT_SHARED_CACHE
143178 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
143179#endif
143180 while( pParse->pCleanup ){
143181 ParseCleanup *pCleanup = pParse->pCleanup;
143182 pParse->pCleanup = pCleanup->pNext;
143183 pCleanup->xCleanup(db, pCleanup->pPtr);
143184 sqlite3DbNNFreeNN(db, pCleanup);
143185 }
143186 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
143187 if( pParse->pConstExpr ){
143188 sqlite3ExprListDelete(db, pParse->pConstExpr);
143189 }
143190 assert( db->lookaside.bDisable >= pParse->disableLookaside )((void) (0));
143191 db->lookaside.bDisable -= pParse->disableLookaside;
143192 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
143193 assert( pParse->db->pParse==pParse )((void) (0));
143194 db->pParse = pParse->pOuterParse;
143195}
143196
143197/*
143198** Add a new cleanup operation to a Parser. The cleanup should happen when
143199** the parser object is destroyed. But, beware: the cleanup might happen
143200** immediately.
143201**
143202** Use this mechanism for uncommon cleanups. There is a higher setup
143203** cost for this mechanism (an extra malloc), so it should not be used
143204** for common cleanups that happen on most calls. But for less
143205** common cleanups, we save a single NULL-pointer comparison in
143206** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.
143207**
143208** If a memory allocation error occurs, then the cleanup happens immediately.
143209** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
143210** pParse->earlyCleanup flag is set in that case. Calling code show verify
143211** that test cases exist for which this happens, to guard against possible
143212** use-after-free errors following an OOM. The preferred way to do this is
143213** to immediately follow the call to this routine with:
143214**
143215** testcase( pParse->earlyCleanup );
143216**
143217** This routine returns a copy of its pPtr input (the third parameter)
143218** except if an early cleanup occurs, in which case it returns NULL. So
143219** another way to check for early cleanup is to check the return value.
143220** Or, stop using the pPtr parameter with this call and use only its
143221** return value thereafter. Something like this:
143222**
143223** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
143224*/
143225SQLITE_PRIVATEstatic void *sqlite3ParserAddCleanup(
143226 Parse *pParse, /* Destroy when this Parser finishes */
143227 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
143228 void *pPtr /* Pointer to object to be cleaned up */
143229){
143230 ParseCleanup *pCleanup;
143231 if( sqlite3FaultSim(300) ){
143232 pCleanup = 0;
143233 sqlite3OomFault(pParse->db);
143234 }else{
143235 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
143236 }
143237 if( pCleanup ){
143238 pCleanup->pNext = pParse->pCleanup;
143239 pParse->pCleanup = pCleanup;
143240 pCleanup->pPtr = pPtr;
143241 pCleanup->xCleanup = xCleanup;
143242 }else{
143243 xCleanup(pParse->db, pPtr);
143244 pPtr = 0;
143245#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
143246 pParse->earlyCleanup = 1;
143247#endif
143248 }
143249 return pPtr;
143250}
143251
143252/*
143253** Turn bulk memory into a valid Parse object and link that Parse object
143254** into database connection db.
143255**
143256** Call sqlite3ParseObjectReset() to undo this operation.
143257**
143258** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which
143259** is generated by Lemon.
143260*/
143261SQLITE_PRIVATEstatic void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){
143262 memset(PARSE_HDR(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, zErrMsg)), 0, PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
);
143263 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
143264 assert( db->pParse!=pParse )((void) (0));
143265 pParse->pOuterParse = db->pParse;
143266 db->pParse = pParse;
143267 pParse->db = db;
143268 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
143269}
143270
143271/*
143272** Maximum number of times that we will try again to prepare a statement
143273** that returns SQLITE_ERROR_RETRY.
143274*/
143275#ifndef SQLITE_MAX_PREPARE_RETRY25
143276# define SQLITE_MAX_PREPARE_RETRY25 25
143277#endif
143278
143279/*
143280** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
143281*/
143282static int sqlite3Prepare(
143283 sqlite3 *db, /* Database handle. */
143284 const char *zSql, /* UTF-8 encoded SQL statement. */
143285 int nBytes, /* Length of zSql in bytes. */
143286 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
143287 Vdbe *pReprepare, /* VM being reprepared */
143288 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143289 const char **pzTail /* OUT: End of parsed string */
143290){
143291 int rc = SQLITE_OK0; /* Result code */
143292 int i; /* Loop counter */
143293 Parse sParse; /* Parsing context */
143294
143295 /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
143296 memset(PARSE_HDR(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, zErrMsg)), 0, PARSE_HDR_SZ(__builtin_offsetof(Parse, aTempReg)-__builtin_offsetof(Parse
, zErrMsg))
);
143297 memset(PARSE_TAIL(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, sLastToken)
)
, 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
143298 sParse.pOuterParse = db->pParse;
143299 db->pParse = &sParse;
143300 sParse.db = db;
143301 if( pReprepare ){
143302 sParse.pReprepare = pReprepare;
143303 sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare);
143304 }else{
143305 assert( sParse.pReprepare==0 )((void) (0));
143306 }
143307 assert( ppStmt && *ppStmt==0 )((void) (0));
143308 if( db->mallocFailed ){
143309 sqlite3ErrorMsg(&sParse, "out of memory");
143310 db->errCode = rc = SQLITE_NOMEM7;
143311 goto end_prepare;
143312 }
143313 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143314
143315 /* For a long-term use prepared statement avoid the use of
143316 ** lookaside memory.
143317 */
143318 if( prepFlags & SQLITE_PREPARE_PERSISTENT0x01 ){
143319 sParse.disableLookaside++;
143320 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
143321 }
143322 sParse.prepFlags = prepFlags & 0xff;
143323
143324 /* Check to verify that it is possible to get a read lock on all
143325 ** database schemas. The inability to get a read lock indicates that
143326 ** some other database connection is holding a write-lock, which in
143327 ** turn means that the other connection has made uncommitted changes
143328 ** to the schema.
143329 **
143330 ** Were we to proceed and prepare the statement against the uncommitted
143331 ** schema changes and if those schema changes are subsequently rolled
143332 ** back and different changes are made in their place, then when this
143333 ** prepared statement goes to run the schema cookie would fail to detect
143334 ** the schema change. Disaster would follow.
143335 **
143336 ** This thread is currently holding mutexes on all Btrees (because
143337 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
143338 ** is not possible for another thread to start a new schema change
143339 ** while this routine is running. Hence, we do not need to hold
143340 ** locks on the schema, we just need to make sure nobody else is
143341 ** holding them.
143342 **
143343 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
143344 ** but it does *not* override schema lock detection, so this all still
143345 ** works even if READ_UNCOMMITTED is set.
143346 */
143347 if( !db->noSharedCache ){
143348 for(i=0; i<db->nDb; i++) {
143349 Btree *pBt = db->aDb[i].pBt;
143350 if( pBt ){
143351 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
143352 rc = sqlite3BtreeSchemaLocked(pBt);
143353 if( rc ){
143354 const char *zDb = db->aDb[i].zDbSName;
143355 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
143356 testcase( db->flags & SQLITE_ReadUncommit );
143357 goto end_prepare;
143358 }
143359 }
143360 }
143361 }
143362
143363#ifndef SQLITE_OMIT_VIRTUALTABLE
143364 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
143365#endif
143366
143367 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
143368 char *zSqlCopy;
143369 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
143370 testcase( nBytes==mxLen );
143371 testcase( nBytes==mxLen+1 );
143372 if( nBytes>mxLen ){
143373 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG18, "statement too long");
143374 rc = sqlite3ApiExit(db, SQLITE_TOOBIG18);
143375 goto end_prepare;
143376 }
143377 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
143378 if( zSqlCopy ){
143379 sqlite3RunParser(&sParse, zSqlCopy);
143380 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
143381 sqlite3DbFree(db, zSqlCopy);
143382 }else{
143383 sParse.zTail = &zSql[nBytes];
143384 }
143385 }else{
143386 sqlite3RunParser(&sParse, zSql);
143387 }
143388 assert( 0==sParse.nQueryLoop )((void) (0));
143389
143390 if( pzTail ){
143391 *pzTail = sParse.zTail;
143392 }
143393
143394 if( db->init.busy==0 ){
143395 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
143396 }
143397 if( db->mallocFailed ){
143398 sParse.rc = SQLITE_NOMEM_BKPT7;
143399 sParse.checkSchema = 0;
143400 }
143401 if( sParse.rc!=SQLITE_OK0 && sParse.rc!=SQLITE_DONE101 ){
143402 if( sParse.checkSchema && db->init.busy==0 ){
143403 schemaIsValid(&sParse);
143404 }
143405 if( sParse.pVdbe ){
143406 sqlite3VdbeFinalize(sParse.pVdbe);
143407 }
143408 assert( 0==(*ppStmt) )((void) (0));
143409 rc = sParse.rc;
143410 if( sParse.zErrMsg ){
143411 sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
143412 sqlite3DbFree(db, sParse.zErrMsg);
143413 }else{
143414 sqlite3Error(db, rc);
143415 }
143416 }else{
143417 assert( sParse.zErrMsg==0 )((void) (0));
143418 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
143419 rc = SQLITE_OK0;
143420 sqlite3ErrorClear(db);
143421 }
143422
143423
143424 /* Delete any TriggerPrg structures allocated while parsing this statement. */
143425 while( sParse.pTriggerPrg ){
143426 TriggerPrg *pT = sParse.pTriggerPrg;
143427 sParse.pTriggerPrg = pT->pNext;
143428 sqlite3DbFree(db, pT);
143429 }
143430
143431end_prepare:
143432
143433 sqlite3ParseObjectReset(&sParse);
143434 return rc;
143435}
143436static int sqlite3LockAndPrepare(
143437 sqlite3 *db, /* Database handle. */
143438 const char *zSql, /* UTF-8 encoded SQL statement. */
143439 int nBytes, /* Length of zSql in bytes. */
143440 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
143441 Vdbe *pOld, /* VM being reprepared */
143442 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143443 const char **pzTail /* OUT: End of parsed string */
143444){
143445 int rc;
143446 int cnt = 0;
143447
143448#ifdef SQLITE_ENABLE_API_ARMOR1
143449 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(143449);
143450#endif
143451 *ppStmt = 0;
143452 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
143453 return SQLITE_MISUSE_BKPTsqlite3MisuseError(143453);
143454 }
143455 sqlite3_mutex_enter(db->mutex);
143456 sqlite3BtreeEnterAll(db);
143457 do{
143458 /* Make multiple attempts to compile the SQL, until it either succeeds
143459 ** or encounters a permanent error. A schema problem after one schema
143460 ** reset is considered a permanent error. */
143461 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
143462 assert( rc==SQLITE_OK || *ppStmt==0 )((void) (0));
143463 if( rc==SQLITE_OK0 || db->mallocFailed ) break;
143464 }while( (rc==SQLITE_ERROR_RETRY(1 | (2<<8)) && (cnt++)<SQLITE_MAX_PREPARE_RETRY25)
143465 || (rc==SQLITE_SCHEMA17 && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
143466 sqlite3BtreeLeaveAll(db);
143467 rc = sqlite3ApiExit(db, rc);
143468 assert( (rc&db->errMask)==rc )((void) (0));
143469 db->busyHandler.nBusy = 0;
143470 sqlite3_mutex_leave(db->mutex);
143471 assert( rc==SQLITE_OK || (*ppStmt)==0 )((void) (0));
143472 return rc;
143473}
143474
143475
143476/*
143477** Rerun the compilation of a statement after a schema change.
143478**
143479** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
143480** if the statement cannot be recompiled because another connection has
143481** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
143482** occurs, return SQLITE_SCHEMA.
143483*/
143484SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe *p){
143485 int rc;
143486 sqlite3_stmt *pNew;
143487 const char *zSql;
143488 sqlite3 *db;
143489 u8 prepFlags;
143490
143491 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) )((void) (0));
143492 zSql = sqlite3_sql((sqlite3_stmt *)p);
143493 assert( zSql!=0 )((void) (0)); /* Reprepare only called for prepare_v2() statements */
143494 db = sqlite3VdbeDb(p);
143495 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
143496 prepFlags = sqlite3VdbePrepareFlags(p);
143497 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
143498 if( rc ){
143499 if( rc==SQLITE_NOMEM7 ){
143500 sqlite3OomFault(db);
143501 }
143502 assert( pNew==0 )((void) (0));
143503 return rc;
143504 }else{
143505 assert( pNew!=0 )((void) (0));
143506 }
143507 sqlite3VdbeSwap((Vdbe*)pNew, p);
143508 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
143509 sqlite3VdbeResetStepResult((Vdbe*)pNew);
143510 sqlite3VdbeFinalize((Vdbe*)pNew);
143511 return SQLITE_OK0;
143512}
143513
143514
143515/*
143516** Two versions of the official API. Legacy and new use. In the legacy
143517** version, the original SQL text is not saved in the prepared statement
143518** and so if a schema change occurs, SQLITE_SCHEMA is returned by
143519** sqlite3_step(). In the new version, the original SQL text is retained
143520** and the statement is automatically recompiled if an schema change
143521** occurs.
143522*/
143523SQLITE_API int sqlite3_prepare(
143524 sqlite3 *db, /* Database handle. */
143525 const char *zSql, /* UTF-8 encoded SQL statement. */
143526 int nBytes, /* Length of zSql in bytes. */
143527 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143528 const char **pzTail /* OUT: End of parsed string */
143529){
143530 int rc;
143531 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
143532 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
143533 return rc;
143534}
143535SQLITE_API int sqlite3_prepare_v2(
143536 sqlite3 *db, /* Database handle. */
143537 const char *zSql, /* UTF-8 encoded SQL statement. */
143538 int nBytes, /* Length of zSql in bytes. */
143539 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143540 const char **pzTail /* OUT: End of parsed string */
143541){
143542 int rc;
143543 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
143544 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
143545 ** parameter.
143546 **
143547 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
143548 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,0,
143549 ppStmt,pzTail);
143550 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
143551 return rc;
143552}
143553SQLITE_API int sqlite3_prepare_v3(
143554 sqlite3 *db, /* Database handle. */
143555 const char *zSql, /* UTF-8 encoded SQL statement. */
143556 int nBytes, /* Length of zSql in bytes. */
143557 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
143558 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143559 const char **pzTail /* OUT: End of parsed string */
143560){
143561 int rc;
143562 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
143563 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
143564 ** which is a bit array consisting of zero or more of the
143565 ** SQLITE_PREPARE_* flags.
143566 **
143567 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
143568 ** directly above. */
143569 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
143570 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x0f),
143571 0,ppStmt,pzTail);
143572 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
143573 return rc;
143574}
143575
143576
143577#ifndef SQLITE_OMIT_UTF16
143578/*
143579** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
143580*/
143581static int sqlite3Prepare16(
143582 sqlite3 *db, /* Database handle. */
143583 const void *zSql, /* UTF-16 encoded SQL statement. */
143584 int nBytes, /* Length of zSql in bytes. */
143585 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
143586 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143587 const void **pzTail /* OUT: End of parsed string */
143588){
143589 /* This function currently works by first transforming the UTF-16
143590 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
143591 ** tricky bit is figuring out the pointer to return in *pzTail.
143592 */
143593 char *zSql8;
143594 const char *zTail8 = 0;
143595 int rc = SQLITE_OK0;
143596
143597#ifdef SQLITE_ENABLE_API_ARMOR1
143598 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(143598);
143599#endif
143600 *ppStmt = 0;
143601 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
143602 return SQLITE_MISUSE_BKPTsqlite3MisuseError(143602);
143603 }
143604
143605 /* Make sure nBytes is non-negative and correct. It should be the
143606 ** number of bytes until the end of the input buffer or until the first
143607 ** U+0000 character. If the input nBytes is odd, convert it into
143608 ** an even number. If the input nBytes is negative, then the input
143609 ** must be terminated by at least one U+0000 character */
143610 if( nBytes>=0 ){
143611 int sz;
143612 const char *z = (const char*)zSql;
143613 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
143614 nBytes = sz;
143615 }else{
143616 int sz;
143617 const char *z = (const char*)zSql;
143618 for(sz=0; z[sz]!=0 || z[sz+1]!=0; sz += 2){}
143619 nBytes = sz;
143620 }
143621
143622 sqlite3_mutex_enter(db->mutex);
143623 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE2);
143624 if( zSql8 ){
143625 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
143626 }
143627
143628 if( zTail8 && pzTail ){
143629 /* If sqlite3_prepare returns a tail pointer, we calculate the
143630 ** equivalent pointer into the UTF-16 string by counting the unicode
143631 ** characters between zSql8 and zTail8, and then returning a pointer
143632 ** the same number of characters into the UTF-16 string.
143633 */
143634 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
143635 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, nBytes, chars_parsed);
143636 }
143637 sqlite3DbFree(db, zSql8);
143638 rc = sqlite3ApiExit(db, rc);
143639 sqlite3_mutex_leave(db->mutex);
143640 return rc;
143641}
143642
143643/*
143644** Two versions of the official API. Legacy and new use. In the legacy
143645** version, the original SQL text is not saved in the prepared statement
143646** and so if a schema change occurs, SQLITE_SCHEMA is returned by
143647** sqlite3_step(). In the new version, the original SQL text is retained
143648** and the statement is automatically recompiled if an schema change
143649** occurs.
143650*/
143651SQLITE_API int sqlite3_prepare16(
143652 sqlite3 *db, /* Database handle. */
143653 const void *zSql, /* UTF-16 encoded SQL statement. */
143654 int nBytes, /* Length of zSql in bytes. */
143655 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143656 const void **pzTail /* OUT: End of parsed string */
143657){
143658 int rc;
143659 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
143660 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
143661 return rc;
143662}
143663SQLITE_API int sqlite3_prepare16_v2(
143664 sqlite3 *db, /* Database handle. */
143665 const void *zSql, /* UTF-16 encoded SQL statement. */
143666 int nBytes, /* Length of zSql in bytes. */
143667 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143668 const void **pzTail /* OUT: End of parsed string */
143669){
143670 int rc;
143671 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,ppStmt,pzTail);
143672 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
143673 return rc;
143674}
143675SQLITE_API int sqlite3_prepare16_v3(
143676 sqlite3 *db, /* Database handle. */
143677 const void *zSql, /* UTF-16 encoded SQL statement. */
143678 int nBytes, /* Length of zSql in bytes. */
143679 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
143680 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
143681 const void **pzTail /* OUT: End of parsed string */
143682){
143683 int rc;
143684 rc = sqlite3Prepare16(db,zSql,nBytes,
143685 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x0f),
143686 ppStmt,pzTail);
143687 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
143688 return rc;
143689}
143690
143691#endif /* SQLITE_OMIT_UTF16 */
143692
143693/************** End of prepare.c *********************************************/
143694/************** Begin file select.c ******************************************/
143695/*
143696** 2001 September 15
143697**
143698** The author disclaims copyright to this source code. In place of
143699** a legal notice, here is a blessing:
143700**
143701** May you do good and not evil.
143702** May you find forgiveness for yourself and forgive others.
143703** May you share freely, never taking more than you give.
143704**
143705*************************************************************************
143706** This file contains C code routines that are called by the parser
143707** to handle SELECT statements in SQLite.
143708*/
143709/* #include "sqliteInt.h" */
143710
143711/*
143712** An instance of the following object is used to record information about
143713** how to process the DISTINCT keyword, to simplify passing that information
143714** into the selectInnerLoop() routine.
143715*/
143716typedef struct DistinctCtx DistinctCtx;
143717struct DistinctCtx {
143718 u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */
143719 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
143720 int tabTnct; /* Ephemeral table used for DISTINCT processing */
143721 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
143722};
143723
143724/*
143725** An instance of the following object is used to record information about
143726** the ORDER BY (or GROUP BY) clause of query is being coded.
143727**
143728** The aDefer[] array is used by the sorter-references optimization. For
143729** example, assuming there is no index that can be used for the ORDER BY,
143730** for the query:
143731**
143732** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
143733**
143734** it may be more efficient to add just the "a" values to the sorter, and
143735** retrieve the associated "bigblob" values directly from table t1 as the
143736** 10 smallest "a" values are extracted from the sorter.
143737**
143738** When the sorter-reference optimization is used, there is one entry in the
143739** aDefer[] array for each database table that may be read as values are
143740** extracted from the sorter.
143741*/
143742typedef struct SortCtx SortCtx;
143743struct SortCtx {
143744 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
143745 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
143746 int iECursor; /* Cursor number for the sorter */
143747 int regReturn; /* Register holding block-output return address */
143748 int labelBkOut; /* Start label for the block-output subroutine */
143749 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
143750 int labelDone; /* Jump here when done, ex: LIMIT reached */
143751 int labelOBLopt; /* Jump here when sorter is full */
143752 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
143753#ifdef SQLITE_ENABLE_SORTER_REFERENCES
143754 u8 nDefer; /* Number of valid entries in aDefer[] */
143755 struct DeferredCsr {
143756 Table *pTab; /* Table definition */
143757 int iCsr; /* Cursor number for table */
143758 int nKey; /* Number of PK columns for table pTab (>=1) */
143759 } aDefer[4];
143760#endif
143761 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
143762#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
143763 int addrPush; /* First instruction to push data into sorter */
143764 int addrPushEnd; /* Last instruction that pushes data into sorter */
143765#endif
143766};
143767#define SORTFLAG_UseSorter0x01 0x01 /* Use SorterOpen instead of OpenEphemeral */
143768
143769/*
143770** Delete all the content of a Select structure. Deallocate the structure
143771** itself depending on the value of bFree
143772**
143773** If bFree==1, call sqlite3DbFree() on the p object.
143774** If bFree==0, Leave the first Select object unfreed
143775*/
143776static void clearSelect(sqlite3 *db, Select *p, int bFree){
143777 assert( db!=0 )((void) (0));
143778 while( p ){
143779 Select *pPrior = p->pPrior;
143780 sqlite3ExprListDelete(db, p->pEList);
143781 sqlite3SrcListDelete(db, p->pSrc);
143782 sqlite3ExprDelete(db, p->pWhere);
143783 sqlite3ExprListDelete(db, p->pGroupBy);
143784 sqlite3ExprDelete(db, p->pHaving);
143785 sqlite3ExprListDelete(db, p->pOrderBy);
143786 sqlite3ExprDelete(db, p->pLimit);
143787 if( OK_IF_ALWAYS_TRUE(p->pWith)(p->pWith) ) sqlite3WithDelete(db, p->pWith);
143788#ifndef SQLITE_OMIT_WINDOWFUNC
143789 if( OK_IF_ALWAYS_TRUE(p->pWinDefn)(p->pWinDefn) ){
143790 sqlite3WindowListDelete(db, p->pWinDefn);
143791 }
143792 while( p->pWin ){
143793 assert( p->pWin->ppThis==&p->pWin )((void) (0));
143794 sqlite3WindowUnlinkFromSelect(p->pWin);
143795 }
143796#endif
143797 if( bFree ) sqlite3DbNNFreeNN(db, p);
143798 p = pPrior;
143799 bFree = 1;
143800 }
143801}
143802
143803/*
143804** Initialize a SelectDest structure.
143805*/
143806SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
143807 pDest->eDest = (u8)eDest;
143808 pDest->iSDParm = iParm;
143809 pDest->iSDParm2 = 0;
143810 pDest->zAffSdst = 0;
143811 pDest->iSdst = 0;
143812 pDest->nSdst = 0;
143813}
143814
143815
143816/*
143817** Allocate a new Select structure and return a pointer to that
143818** structure.
143819*/
143820SQLITE_PRIVATEstatic Select *sqlite3SelectNew(
143821 Parse *pParse, /* Parsing context */
143822 ExprList *pEList, /* which columns to include in the result */
143823 SrcList *pSrc, /* the FROM clause -- which tables to scan */
143824 Expr *pWhere, /* the WHERE clause */
143825 ExprList *pGroupBy, /* the GROUP BY clause */
143826 Expr *pHaving, /* the HAVING clause */
143827 ExprList *pOrderBy, /* the ORDER BY clause */
143828 u32 selFlags, /* Flag parameters, such as SF_Distinct */
143829 Expr *pLimit /* LIMIT value. NULL means not used */
143830){
143831 Select *pNew, *pAllocated;
143832 Select standin;
143833 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
143834 if( pNew==0 ){
143835 assert( pParse->db->mallocFailed )((void) (0));
143836 pNew = &standin;
143837 }
143838 if( pEList==0 ){
143839 pEList = sqlite3ExprListAppend(pParse, 0,
143840 sqlite3Expr(pParse->db,TK_ASTERISK180,0));
143841 }
143842 pNew->pEList = pEList;
143843 pNew->op = TK_SELECT139;
143844 pNew->selFlags = selFlags;
143845 pNew->iLimit = 0;
143846 pNew->iOffset = 0;
143847 pNew->selId = ++pParse->nSelect;
143848 pNew->addrOpenEphm[0] = -1;
143849 pNew->addrOpenEphm[1] = -1;
143850 pNew->nSelectRow = 0;
143851 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
143852 pNew->pSrc = pSrc;
143853 pNew->pWhere = pWhere;
143854 pNew->pGroupBy = pGroupBy;
143855 pNew->pHaving = pHaving;
143856 pNew->pOrderBy = pOrderBy;
143857 pNew->pPrior = 0;
143858 pNew->pNext = 0;
143859 pNew->pLimit = pLimit;
143860 pNew->pWith = 0;
143861#ifndef SQLITE_OMIT_WINDOWFUNC
143862 pNew->pWin = 0;
143863 pNew->pWinDefn = 0;
143864#endif
143865 if( pParse->db->mallocFailed ) {
143866 clearSelect(pParse->db, pNew, pNew!=&standin);
143867 pAllocated = 0;
143868 }else{
143869 assert( pNew->pSrc!=0 || pParse->nErr>0 )((void) (0));
143870 }
143871 return pAllocated;
143872}
143873
143874
143875/*
143876** Delete the given Select structure and all of its substructures.
143877*/
143878SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3 *db, Select *p){
143879 if( OK_IF_ALWAYS_TRUE(p)(p) ) clearSelect(db, p, 1);
143880}
143881SQLITE_PRIVATEstatic void sqlite3SelectDeleteGeneric(sqlite3 *db, void *p){
143882 if( ALWAYS(p)(p) ) clearSelect(db, (Select*)p, 1);
143883}
143884
143885/*
143886** Return a pointer to the right-most SELECT statement in a compound.
143887*/
143888static Select *findRightmost(Select *p){
143889 while( p->pNext ) p = p->pNext;
143890 return p;
143891}
143892
143893/*
143894** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
143895** type of join. Return an integer constant that expresses that type
143896** in terms of the following bit values:
143897**
143898** JT_INNER
143899** JT_CROSS
143900** JT_OUTER
143901** JT_NATURAL
143902** JT_LEFT
143903** JT_RIGHT
143904**
143905** A full outer join is the combination of JT_LEFT and JT_RIGHT.
143906**
143907** If an illegal or unsupported join type is seen, then still return
143908** a join type, but put an error in the pParse structure.
143909**
143910** These are the valid join types:
143911**
143912**
143913** pA pB pC Return Value
143914** ------- ----- ----- ------------
143915** CROSS - - JT_CROSS
143916** INNER - - JT_INNER
143917** LEFT - - JT_LEFT|JT_OUTER
143918** LEFT OUTER - JT_LEFT|JT_OUTER
143919** RIGHT - - JT_RIGHT|JT_OUTER
143920** RIGHT OUTER - JT_RIGHT|JT_OUTER
143921** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
143922** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
143923** NATURAL INNER - JT_NATURAL|JT_INNER
143924** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
143925** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER
143926** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
143927** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER
143928** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
143929** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT
143930**
143931** To preserve historical compatibly, SQLite also accepts a variety
143932** of other non-standard and in many cases nonsensical join types.
143933** This routine makes as much sense at it can from the nonsense join
143934** type and returns a result. Examples of accepted nonsense join types
143935** include but are not limited to:
143936**
143937** INNER CROSS JOIN -> same as JOIN
143938** NATURAL CROSS JOIN -> same as NATURAL JOIN
143939** OUTER LEFT JOIN -> same as LEFT JOIN
143940** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
143941** LEFT RIGHT JOIN -> same as FULL JOIN
143942** RIGHT OUTER FULL JOIN -> same as FULL JOIN
143943** CROSS CROSS CROSS JOIN -> same as JOIN
143944**
143945** The only restrictions on the join type name are:
143946**
143947** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",
143948** or "FULL".
143949**
143950** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,
143951** or "FULL".
143952**
143953** * If "OUTER" is present then there must also be one of
143954** "LEFT", "RIGHT", or "FULL"
143955*/
143956SQLITE_PRIVATEstatic int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
143957 int jointype = 0;
143958 Token *apAll[3];
143959 Token *p;
143960 /* 0123456789 123456789 123456789 123 */
143961 static const char zKeyText[] = "naturaleftouterightfullinnercross";
143962 static const struct {
143963 u8 i; /* Beginning of keyword text in zKeyText[] */
143964 u8 nChar; /* Length of the keyword in characters */
143965 u8 code; /* Join type mask */
143966 } aKeyword[] = {
143967 /* (0) natural */ { 0, 7, JT_NATURAL0x04 },
143968 /* (1) left */ { 6, 4, JT_LEFT0x08|JT_OUTER0x20 },
143969 /* (2) outer */ { 10, 5, JT_OUTER0x20 },
143970 /* (3) right */ { 14, 5, JT_RIGHT0x10|JT_OUTER0x20 },
143971 /* (4) full */ { 19, 4, JT_LEFT0x08|JT_RIGHT0x10|JT_OUTER0x20 },
143972 /* (5) inner */ { 23, 5, JT_INNER0x01 },
143973 /* (6) cross */ { 28, 5, JT_INNER0x01|JT_CROSS0x02 },
143974 };
143975 int i, j;
143976 apAll[0] = pA;
143977 apAll[1] = pB;
143978 apAll[2] = pC;
143979 for(i=0; i<3 && apAll[i]; i++){
143980 p = apAll[i];
143981 for(j=0; j<ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))); j++){
143982 if( p->n==aKeyword[j].nChar
143983 && sqlite3StrNICmpsqlite3_strnicmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
143984 jointype |= aKeyword[j].code;
143985 break;
143986 }
143987 }
143988 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
143989 if( j>=ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))) ){
143990 jointype |= JT_ERROR0x80;
143991 break;
143992 }
143993 }
143994 if(
143995 (jointype & (JT_INNER0x01|JT_OUTER0x20))==(JT_INNER0x01|JT_OUTER0x20) ||
143996 (jointype & JT_ERROR0x80)!=0 ||
143997 (jointype & (JT_OUTER0x20|JT_LEFT0x08|JT_RIGHT0x10))==JT_OUTER0x20
143998 ){
143999 const char *zSp1 = " ";
144000 const char *zSp2 = " ";
144001 if( pB==0 ){ zSp1++; }
144002 if( pC==0 ){ zSp2++; }
144003 sqlite3ErrorMsg(pParse, "unknown join type: "
144004 "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
144005 jointype = JT_INNER0x01;
144006 }
144007 return jointype;
144008}
144009
144010/*
144011** Return the index of a column in a table. Return -1 if the column
144012** is not contained in the table.
144013*/
144014SQLITE_PRIVATEstatic int sqlite3ColumnIndex(Table *pTab, const char *zCol){
144015 int i;
144016 u8 h = sqlite3StrIHash(zCol);
144017 Column *pCol;
144018 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
144019 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
144020 }
144021 return -1;
144022}
144023
144024/*
144025** Mark a subquery result column as having been used.
144026*/
144027SQLITE_PRIVATEstatic void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){
144028 assert( pItem!=0 )((void) (0));
144029 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) )((void) (0));
144030 if( pItem->fg.isNestedFrom ){
144031 ExprList *pResults;
144032 assert( pItem->fg.isSubquery )((void) (0));
144033 assert( pItem->u4.pSubq!=0 )((void) (0));
144034 assert( pItem->u4.pSubq->pSelect!=0 )((void) (0));
144035 pResults = pItem->u4.pSubq->pSelect->pEList;
144036 assert( pResults!=0 )((void) (0));
144037 assert( iCol>=0 && iCol<pResults->nExpr )((void) (0));
144038 pResults->a[iCol].fg.bUsed = 1;
144039 }
144040}
144041
144042/*
144043** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a
144044** table that has a column named zCol. The search is left-to-right.
144045** The first match found is returned.
144046**
144047** When found, set *piTab and *piCol to the table index and column index
144048** of the matching column and return TRUE.
144049**
144050** If not found, return FALSE.
144051*/
144052static int tableAndColumnIndex(
144053 SrcList *pSrc, /* Array of tables to search */
144054 int iStart, /* First member of pSrc->a[] to check */
144055 int iEnd, /* Last member of pSrc->a[] to check */
144056 const char *zCol, /* Name of the column we are looking for */
144057 int *piTab, /* Write index of pSrc->a[] here */
144058 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
144059 int bIgnoreHidden /* Ignore hidden columns */
144060){
144061 int i; /* For looping over tables in pSrc */
144062 int iCol; /* Index of column matching zCol */
144063
144064 assert( iEnd<pSrc->nSrc )((void) (0));
144065 assert( iStart>=0 )((void) (0));
144066 assert( (piTab==0)==(piCol==0) )((void) (0)); /* Both or neither are NULL */
144067
144068 for(i=iStart; i<=iEnd; i++){
144069 iCol = sqlite3ColumnIndex(pSrc->a[i].pSTab, zCol);
144070 if( iCol>=0
144071 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pSTab->aCol[iCol])(((&pSrc->a[i].pSTab->aCol[iCol])->colFlags &
0x0002)!=0)
==0)
144072 ){
144073 if( piTab ){
144074 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
144075 *piTab = i;
144076 *piCol = iCol;
144077 }
144078 return 1;
144079 }
144080 }
144081 return 0;
144082}
144083
144084/*
144085** Set the EP_OuterON property on all terms of the given expression.
144086** And set the Expr.w.iJoin to iTable for every term in the
144087** expression.
144088**
144089** The EP_OuterON property is used on terms of an expression to tell
144090** the OUTER JOIN processing logic that this term is part of the
144091** join restriction specified in the ON or USING clause and not a part
144092** of the more general WHERE clause. These terms are moved over to the
144093** WHERE clause during join processing but we need to remember that they
144094** originated in the ON or USING clause.
144095**
144096** The Expr.w.iJoin tells the WHERE clause processing that the
144097** expression depends on table w.iJoin even if that table is not
144098** explicitly mentioned in the expression. That information is needed
144099** for cases like this:
144100**
144101** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
144102**
144103** The where clause needs to defer the handling of the t1.x=5
144104** term until after the t2 loop of the join. In that way, a
144105** NULL t2 row will be inserted whenever t1.x!=5. If we do not
144106** defer the handling of t1.x=5, it will be processed immediately
144107** after the t1 loop and rows with t1.x!=5 will never appear in
144108** the output, which is incorrect.
144109*/
144110SQLITE_PRIVATEstatic void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){
144111 assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON )((void) (0));
144112 while( p ){
144113 ExprSetProperty(p, joinFlag)(p)->flags|=(joinFlag);
144114 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
144115 ExprSetVVAProperty(p, EP_NoReduce);
144116 p->w.iJoin = iTable;
144117 if( p->op==TK_FUNCTION172 ){
144118 assert( ExprUseXList(p) )((void) (0));
144119 if( p->x.pList ){
144120 int i;
144121 for(i=0; i<p->x.pList->nExpr; i++){
144122 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
144123 }
144124 }
144125 }
144126 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
144127 p = p->pRight;
144128 }
144129}
144130
144131/* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN
144132** is simplified into an ordinary JOIN, and when an ON expression is
144133** "pushed down" into the WHERE clause of a subquery.
144134**
144135** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into
144136** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then
144137** just clear every EP_OuterON and EP_InnerON mark from the expression tree.
144138**
144139** If nullable is true, that means that Expr p might evaluate to NULL even
144140** if it is a reference to a NOT NULL column. This can happen, for example,
144141** if the table that p references is on the left side of a RIGHT JOIN.
144142** If nullable is true, then take care to not remove the EP_CanBeNull bit.
144143** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c
144144*/
144145static void unsetJoinExpr(Expr *p, int iTable, int nullable){
144146 while( p ){
144147 if( iTable<0 || (ExprHasProperty(p, EP_OuterON)(((p)->flags&(0x000001))!=0) && p->w.iJoin==iTable) ){
144148 ExprClearProperty(p, EP_OuterON|EP_InnerON)(p)->flags&=~(0x000001|0x000002);
144149 if( iTable>=0 ) ExprSetProperty(p, EP_InnerON)(p)->flags|=(0x000002);
144150 }
144151 if( p->op==TK_COLUMN168 && p->iTable==iTable && !nullable ){
144152 ExprClearProperty(p, EP_CanBeNull)(p)->flags&=~(0x200000);
144153 }
144154 if( p->op==TK_FUNCTION172 ){
144155 assert( ExprUseXList(p) )((void) (0));
144156 assert( p->pLeft==0 )((void) (0));
144157 if( p->x.pList ){
144158 int i;
144159 for(i=0; i<p->x.pList->nExpr; i++){
144160 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
144161 }
144162 }
144163 }
144164 unsetJoinExpr(p->pLeft, iTable, nullable);
144165 p = p->pRight;
144166 }
144167}
144168
144169/*
144170** This routine processes the join information for a SELECT statement.
144171**
144172** * A NATURAL join is converted into a USING join. After that, we
144173** do not need to be concerned with NATURAL joins and we only have
144174** think about USING joins.
144175**
144176** * ON and USING clauses result in extra terms being added to the
144177** WHERE clause to enforce the specified constraints. The extra
144178** WHERE clause terms will be tagged with EP_OuterON or
144179** EP_InnerON so that we know that they originated in ON/USING.
144180**
144181** The terms of a FROM clause are contained in the Select.pSrc structure.
144182** The left most table is the first entry in Select.pSrc. The right-most
144183** table is the last entry. The join operator is held in the entry to
144184** the right. Thus entry 1 contains the join operator for the join between
144185** entries 0 and 1. Any ON or USING clauses associated with the join are
144186** also attached to the right entry.
144187**
144188** This routine returns the number of errors encountered.
144189*/
144190static int sqlite3ProcessJoin(Parse *pParse, Select *p){
144191 SrcList *pSrc; /* All tables in the FROM clause */
144192 int i, j; /* Loop counters */
144193 SrcItem *pLeft; /* Left table being joined */
144194 SrcItem *pRight; /* Right table being joined */
144195
144196 pSrc = p->pSrc;
144197 pLeft = &pSrc->a[0];
144198 pRight = &pLeft[1];
144199 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
144200 Table *pRightTab = pRight->pSTab;
144201 u32 joinType;
144202
144203 if( NEVER(pLeft->pSTab==0 || pRightTab==0)(pLeft->pSTab==0 || pRightTab==0) ) continue;
144204 joinType = (pRight->fg.jointype & JT_OUTER0x20)!=0 ? EP_OuterON0x000001 : EP_InnerON0x000002;
144205
144206 /* If this is a NATURAL join, synthesize an appropriate USING clause
144207 ** to specify which columns should be joined.
144208 */
144209 if( pRight->fg.jointype & JT_NATURAL0x04 ){
144210 IdList *pUsing = 0;
144211 if( pRight->fg.isUsing || pRight->u3.pOn ){
144212 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
144213 "an ON or USING clause", 0);
144214 return 1;
144215 }
144216 for(j=0; j<pRightTab->nCol; j++){
144217 char *zName; /* Name of column in the right table */
144218
144219 if( IsHiddenColumn(&pRightTab->aCol[j])(((&pRightTab->aCol[j])->colFlags & 0x0002)!=0) ) continue;
144220 zName = pRightTab->aCol[j].zCnName;
144221 if( tableAndColumnIndex(pSrc, 0, i, zName, 0, 0, 1) ){
144222 pUsing = sqlite3IdListAppend(pParse, pUsing, 0);
144223 if( pUsing ){
144224 assert( pUsing->nId>0 )((void) (0));
144225 assert( pUsing->a[pUsing->nId-1].zName==0 )((void) (0));
144226 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
144227 }
144228 }
144229 }
144230 if( pUsing ){
144231 pRight->fg.isUsing = 1;
144232 pRight->fg.isSynthUsing = 1;
144233 pRight->u3.pUsing = pUsing;
144234 }
144235 if( pParse->nErr ) return 1;
144236 }
144237
144238 /* Create extra terms on the WHERE clause for each column named
144239 ** in the USING clause. Example: If the two tables to be joined are
144240 ** A and B and the USING clause names X, Y, and Z, then add this
144241 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
144242 ** Report an error if any column mentioned in the USING clause is
144243 ** not contained in both tables to be joined.
144244 */
144245 if( pRight->fg.isUsing ){
144246 IdList *pList = pRight->u3.pUsing;
144247 sqlite3 *db = pParse->db;
144248 assert( pList!=0 )((void) (0));
144249 for(j=0; j<pList->nId; j++){
144250 char *zName; /* Name of the term in the USING clause */
144251 int iLeft; /* Table on the left with matching column name */
144252 int iLeftCol; /* Column number of matching column on the left */
144253 int iRightCol; /* Column number of matching column on the right */
144254 Expr *pE1; /* Reference to the column on the LEFT of the join */
144255 Expr *pE2; /* Reference to the column on the RIGHT of the join */
144256 Expr *pEq; /* Equality constraint. pE1 == pE2 */
144257
144258 zName = pList->a[j].zName;
144259 iRightCol = sqlite3ColumnIndex(pRightTab, zName);
144260 if( iRightCol<0
144261 || tableAndColumnIndex(pSrc, 0, i, zName, &iLeft, &iLeftCol,
144262 pRight->fg.isSynthUsing)==0
144263 ){
144264 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
144265 "not present in both tables", zName);
144266 return 1;
144267 }
144268 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);
144269 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
144270 if( (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
144271 /* This branch runs if the query contains one or more RIGHT or FULL
144272 ** JOINs. If only a single table on the left side of this join
144273 ** contains the zName column, then this branch is a no-op.
144274 ** But if there are two or more tables on the left side
144275 ** of the join, construct a coalesce() function that gathers all
144276 ** such tables. Raise an error if more than one of those references
144277 ** to zName is not also within a prior USING clause.
144278 **
144279 ** We really ought to raise an error if there are two or more
144280 ** non-USING references to zName on the left of an INNER or LEFT
144281 ** JOIN. But older versions of SQLite do not do that, so we avoid
144282 ** adding a new error so as to not break legacy applications.
144283 */
144284 ExprList *pFuncArgs = 0; /* Arguments to the coalesce() */
144285 static const Token tkCoalesce = { "coalesce", 8 };
144286 while( tableAndColumnIndex(pSrc, iLeft+1, i, zName, &iLeft, &iLeftCol,
144287 pRight->fg.isSynthUsing)!=0 ){
144288 if( pSrc->a[iLeft].fg.isUsing==0
144289 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
144290 ){
144291 sqlite3ErrorMsg(pParse, "ambiguous reference to %s in USING()",
144292 zName);
144293 break;
144294 }
144295 pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);
144296 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol);
144297 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
144298 }
144299 if( pFuncArgs ){
144300 pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1);
144301 pE1 = sqlite3ExprFunction(pParse, pFuncArgs, &tkCoalesce, 0);
144302 }
144303 }
144304 pE2 = sqlite3CreateColumnExpr(db, pSrc, i+1, iRightCol);
144305 sqlite3SrcItemColumnUsed(pRight, iRightCol);
144306 pEq = sqlite3PExpr(pParse, TK_EQ54, pE1, pE2);
144307 assert( pE2!=0 || pEq==0 )((void) (0));
144308 if( pEq ){
144309 ExprSetProperty(pEq, joinType)(pEq)->flags|=(joinType);
144310 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) )((void) (0));
144311 ExprSetVVAProperty(pEq, EP_NoReduce);
144312 pEq->w.iJoin = pE2->iTable;
144313 }
144314 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
144315 }
144316 }
144317
144318 /* Add the ON clause to the end of the WHERE clause, connected by
144319 ** an AND operator.
144320 */
144321 else if( pRight->u3.pOn ){
144322 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
144323 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
144324 pRight->u3.pOn = 0;
144325 pRight->fg.isOn = 1;
144326 }
144327 }
144328 return 0;
144329}
144330
144331/*
144332** An instance of this object holds information (beyond pParse and pSelect)
144333** needed to load the next result row that is to be added to the sorter.
144334*/
144335typedef struct RowLoadInfo RowLoadInfo;
144336struct RowLoadInfo {
144337 int regResult; /* Store results in array of registers here */
144338 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
144339#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144340 ExprList *pExtra; /* Extra columns needed by sorter refs */
144341 int regExtraResult; /* Where to load the extra columns */
144342#endif
144343};
144344
144345/*
144346** This routine does the work of loading query data into an array of
144347** registers so that it can be added to the sorter.
144348*/
144349static void innerLoopLoadRow(
144350 Parse *pParse, /* Statement under construction */
144351 Select *pSelect, /* The query being coded */
144352 RowLoadInfo *pInfo /* Info needed to complete the row load */
144353){
144354 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
144355 0, pInfo->ecelFlags);
144356#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144357 if( pInfo->pExtra ){
144358 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
144359 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
144360 }
144361#endif
144362}
144363
144364/*
144365** Code the OP_MakeRecord instruction that generates the entry to be
144366** added into the sorter.
144367**
144368** Return the register in which the result is stored.
144369*/
144370static int makeSorterRecord(
144371 Parse *pParse,
144372 SortCtx *pSort,
144373 Select *pSelect,
144374 int regBase,
144375 int nBase
144376){
144377 int nOBSat = pSort->nOBSat;
144378 Vdbe *v = pParse->pVdbe;
144379 int regOut = ++pParse->nMem;
144380 if( pSort->pDeferredRowLoad ){
144381 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
144382 }
144383 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase+nOBSat, nBase-nOBSat, regOut);
144384 return regOut;
144385}
144386
144387/*
144388** Generate code that will push the record in registers regData
144389** through regData+nData-1 onto the sorter.
144390*/
144391static void pushOntoSorter(
144392 Parse *pParse, /* Parser context */
144393 SortCtx *pSort, /* Information about the ORDER BY clause */
144394 Select *pSelect, /* The whole SELECT statement */
144395 int regData, /* First register holding data to be sorted */
144396 int regOrigData, /* First register holding data before packing */
144397 int nData, /* Number of elements in the regData data array */
144398 int nPrefixReg /* No. of reg prior to regData available for use */
144399){
144400 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
144401 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter0x01)==0);
144402 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
144403 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
144404 int regBase; /* Regs for sorter record */
144405 int regRecord = 0; /* Assembled sorter record */
144406 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
144407 int op; /* Opcode to add sorter record to sorter */
144408 int iLimit; /* LIMIT counter */
144409 int iSkip = 0; /* End of the sorter insert loop */
144410
144411 assert( bSeq==0 || bSeq==1 )((void) (0));
144412
144413 /* Three cases:
144414 ** (1) The data to be sorted has already been packed into a Record
144415 ** by a prior OP_MakeRecord. In this case nData==1 and regData
144416 ** will be completely unrelated to regOrigData.
144417 ** (2) All output columns are included in the sort record. In that
144418 ** case regData==regOrigData.
144419 ** (3) Some output columns are omitted from the sort record due to
144420 ** the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the
144421 ** SQLITE_ECEL_OMITREF optimization, or due to the
144422 ** SortCtx.pDeferredRowLoad optimization. In any of these cases
144423 ** regOrigData is 0 to prevent this routine from trying to copy
144424 ** values that might not yet exist.
144425 */
144426 assert( nData==1 || regData==regOrigData || regOrigData==0 )((void) (0));
144427
144428#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
144429 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
144430#endif
144431
144432 if( nPrefixReg ){
144433 assert( nPrefixReg==nExpr+bSeq )((void) (0));
144434 regBase = regData - nPrefixReg;
144435 }else{
144436 regBase = pParse->nMem + 1;
144437 pParse->nMem += nBase;
144438 }
144439 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 )((void) (0));
144440 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
144441 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
144442 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
144443 SQLITE_ECEL_DUP0x01 | (regOrigData? SQLITE_ECEL_REF0x04 : 0));
144444 if( bSeq ){
144445 sqlite3VdbeAddOp2(v, OP_Sequence126, pSort->iECursor, regBase+nExpr);
144446 }
144447 if( nPrefixReg==0 && nData>0 ){
144448 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
144449 }
144450 if( nOBSat>0 ){
144451 int regPrevKey; /* The first nOBSat columns of the previous row */
144452 int addrFirst; /* Address of the OP_IfNot opcode */
144453 int addrJmp; /* Address of the OP_Jump opcode */
144454 VdbeOp *pOp; /* Opcode that opens the sorter */
144455 int nKey; /* Number of sorting key columns, including OP_Sequence */
144456 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
144457
144458 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
144459 regPrevKey = pParse->nMem+1;
144460 pParse->nMem += pSort->nOBSat;
144461 nKey = nExpr - pSort->nOBSat + bSeq;
144462 if( bSeq ){
144463 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot17, regBase+nExpr);
144464 }else{
144465 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest120, pSort->iECursor);
144466 }
144467 VdbeCoverage(v);
144468 sqlite3VdbeAddOp3(v, OP_Compare90, regPrevKey, regBase, pSort->nOBSat);
144469 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
144470 if( pParse->db->mallocFailed ) return;
144471 pOp->p2 = nKey + nData;
144472 pKI = pOp->p4.pKeyInfo;
144473 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
144474 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO(-8));
144475 testcase( pKI->nAllField > pKI->nKeyField+2 );
144476 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
144477 pKI->nAllField-pKI->nKeyField-1);
144478 pOp = 0; /* Ensure pOp not used after sqlite3VdbeAddOp3() */
144479 addrJmp = sqlite3VdbeCurrentAddr(v);
144480 sqlite3VdbeAddOp3(v, OP_Jump14, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
144481 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
144482 pSort->regReturn = ++pParse->nMem;
144483 sqlite3VdbeAddOp2(v, OP_Gosub10, pSort->regReturn, pSort->labelBkOut);
144484 sqlite3VdbeAddOp1(v, OP_ResetSorter146, pSort->iECursor);
144485 if( iLimit ){
144486 sqlite3VdbeAddOp2(v, OP_IfNot17, iLimit, pSort->labelDone);
144487 VdbeCoverage(v);
144488 }
144489 sqlite3VdbeJumpHere(v, addrFirst);
144490 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
144491 sqlite3VdbeJumpHere(v, addrJmp);
144492 }
144493 if( iLimit ){
144494 /* At this point the values for the new sorter entry are stored
144495 ** in an array of registers. They need to be composed into a record
144496 ** and inserted into the sorter if either (a) there are currently
144497 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
144498 ** the largest record currently in the sorter. If (b) is true and there
144499 ** are already LIMIT+OFFSET items in the sorter, delete the largest
144500 ** entry before inserting the new one. This way there are never more
144501 ** than LIMIT+OFFSET items in the sorter.
144502 **
144503 ** If the new record does not need to be inserted into the sorter,
144504 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
144505 ** value is not zero, then it is a label of where to jump. Otherwise,
144506 ** just bypass the row insert logic. See the header comment on the
144507 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
144508 */
144509 int iCsr = pSort->iECursor;
144510 sqlite3VdbeAddOp2(v, OP_IfNotZero60, iLimit, sqlite3VdbeCurrentAddr(v)+4);
144511 VdbeCoverage(v);
144512 sqlite3VdbeAddOp2(v, OP_Last32, iCsr, 0);
144513 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE40,
144514 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
144515 VdbeCoverage(v);
144516 sqlite3VdbeAddOp1(v, OP_Delete130, iCsr);
144517 }
144518 if( regRecord==0 ){
144519 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
144520 }
144521 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
144522 op = OP_SorterInsert139;
144523 }else{
144524 op = OP_IdxInsert138;
144525 }
144526 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
144527 regBase+nOBSat, nBase-nOBSat);
144528 if( iSkip ){
144529 sqlite3VdbeChangeP2(v, iSkip,
144530 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
144531 }
144532#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
144533 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
144534#endif
144535}
144536
144537/*
144538** Add code to implement the OFFSET
144539*/
144540static void codeOffset(
144541 Vdbe *v, /* Generate code into this VM */
144542 int iOffset, /* Register holding the offset counter */
144543 int iContinue /* Jump here to skip the current record */
144544){
144545 if( iOffset>0 ){
144546 sqlite3VdbeAddOp3(v, OP_IfPos50, iOffset, iContinue, 1); VdbeCoverage(v);
144547 VdbeComment((v, "OFFSET"));
144548 }
144549}
144550
144551/*
144552** Add code that will check to make sure the array of registers starting at
144553** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and
144554** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies
144555** are available. Which is used depends on the value of parameter eTnctType,
144556** as follows:
144557**
144558** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP:
144559** Build an ephemeral table that contains all entries seen before and
144560** skip entries which have been seen before.
144561**
144562** Parameter iTab is the cursor number of an ephemeral table that must
144563** be opened before the VM code generated by this routine is executed.
144564** The ephemeral cursor table is queried for a record identical to the
144565** record formed by the current array of registers. If one is found,
144566** jump to VM address addrRepeat. Otherwise, insert a new record into
144567** the ephemeral cursor and proceed.
144568**
144569** The returned value in this case is a copy of parameter iTab.
144570**
144571** WHERE_DISTINCT_ORDERED:
144572** In this case rows are being delivered sorted order. The ephemeral
144573** table is not required. Instead, the current set of values
144574** is compared against previous row. If they match, the new row
144575** is not distinct and control jumps to VM address addrRepeat. Otherwise,
144576** the VM program proceeds with processing the new row.
144577**
144578** The returned value in this case is the register number of the first
144579** in an array of registers used to store the previous result row so that
144580** it can be compared to the next. The caller must ensure that this
144581** register is initialized to NULL. (The fixDistinctOpenEph() routine
144582** will take care of this initialization.)
144583**
144584** WHERE_DISTINCT_UNIQUE:
144585** In this case it has already been determined that the rows are distinct.
144586** No special action is required. The return value is zero.
144587**
144588** Parameter pEList is the list of expressions used to generated the
144589** contents of each row. It is used by this routine to determine (a)
144590** how many elements there are in the array of registers and (b) the
144591** collation sequences that should be used for the comparisons if
144592** eTnctType is WHERE_DISTINCT_ORDERED.
144593*/
144594static int codeDistinct(
144595 Parse *pParse, /* Parsing and code generating context */
144596 int eTnctType, /* WHERE_DISTINCT_* value */
144597 int iTab, /* A sorting index used to test for distinctness */
144598 int addrRepeat, /* Jump to here if not distinct */
144599 ExprList *pEList, /* Expression for each element */
144600 int regElem /* First element */
144601){
144602 int iRet = 0;
144603 int nResultCol = pEList->nExpr;
144604 Vdbe *v = pParse->pVdbe;
144605
144606 switch( eTnctType ){
144607 case WHERE_DISTINCT_ORDERED2: {
144608 int i;
144609 int iJump; /* Jump destination */
144610 int regPrev; /* Previous row content */
144611
144612 /* Allocate space for the previous row */
144613 iRet = regPrev = pParse->nMem+1;
144614 pParse->nMem += nResultCol;
144615
144616 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
144617 for(i=0; i<nResultCol; i++){
144618 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
144619 if( i<nResultCol-1 ){
144620 sqlite3VdbeAddOp3(v, OP_Ne53, regElem+i, iJump, regPrev+i);
144621 VdbeCoverage(v);
144622 }else{
144623 sqlite3VdbeAddOp3(v, OP_Eq54, regElem+i, addrRepeat, regPrev+i);
144624 VdbeCoverage(v);
144625 }
144626 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ(-2));
144627 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
144628 }
144629 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed )((void) (0));
144630 sqlite3VdbeAddOp3(v, OP_Copy80, regElem, regPrev, nResultCol-1);
144631 break;
144632 }
144633
144634 case WHERE_DISTINCT_UNIQUE1: {
144635 /* nothing to do */
144636 break;
144637 }
144638
144639 default: {
144640 int r1 = sqlite3GetTempReg(pParse);
144641 sqlite3VdbeAddOp4Int(v, OP_Found29, iTab, addrRepeat, regElem, nResultCol);
144642 VdbeCoverage(v);
144643 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regElem, nResultCol, r1);
144644 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iTab, r1, regElem, nResultCol);
144645 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
144646 sqlite3ReleaseTempReg(pParse, r1);
144647 iRet = iTab;
144648 break;
144649 }
144650 }
144651
144652 return iRet;
144653}
144654
144655/*
144656** This routine runs after codeDistinct(). It makes necessary
144657** adjustments to the OP_OpenEphemeral opcode that the codeDistinct()
144658** routine made use of. This processing must be done separately since
144659** sometimes codeDistinct is called before the OP_OpenEphemeral is actually
144660** laid down.
144661**
144662** WHERE_DISTINCT_NOOP:
144663** WHERE_DISTINCT_UNORDERED:
144664**
144665** No adjustments necessary. This function is a no-op.
144666**
144667** WHERE_DISTINCT_UNIQUE:
144668**
144669** The ephemeral table is not needed. So change the
144670** OP_OpenEphemeral opcode into an OP_Noop.
144671**
144672** WHERE_DISTINCT_ORDERED:
144673**
144674** The ephemeral table is not needed. But we do need register
144675** iVal to be initialized to NULL. So change the OP_OpenEphemeral
144676** into an OP_Null on the iVal register.
144677*/
144678static void fixDistinctOpenEph(
144679 Parse *pParse, /* Parsing and code generating context */
144680 int eTnctType, /* WHERE_DISTINCT_* value */
144681 int iVal, /* Value returned by codeDistinct() */
144682 int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */
144683){
144684 if( pParse->nErr==0
144685 && (eTnctType==WHERE_DISTINCT_UNIQUE1 || eTnctType==WHERE_DISTINCT_ORDERED2)
144686 ){
144687 Vdbe *v = pParse->pVdbe;
144688 sqlite3VdbeChangeToNoop(v, iOpenEphAddr);
144689 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain188 ){
144690 sqlite3VdbeChangeToNoop(v, iOpenEphAddr+1);
144691 }
144692 if( eTnctType==WHERE_DISTINCT_ORDERED2 ){
144693 /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared
144694 ** bit on the first register of the previous value. This will cause the
144695 ** OP_Ne added in codeDistinct() to always fail on the first iteration of
144696 ** the loop even if the first row is all NULLs. */
144697 VdbeOp *pOp = sqlite3VdbeGetOp(v, iOpenEphAddr);
144698 pOp->opcode = OP_Null75;
144699 pOp->p1 = 1;
144700 pOp->p2 = iVal;
144701 }
144702 }
144703}
144704
144705#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144706/*
144707** This function is called as part of inner-loop generation for a SELECT
144708** statement with an ORDER BY that is not optimized by an index. It
144709** determines the expressions, if any, that the sorter-reference
144710** optimization should be used for. The sorter-reference optimization
144711** is used for SELECT queries like:
144712**
144713** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
144714**
144715** If the optimization is used for expression "bigblob", then instead of
144716** storing values read from that column in the sorter records, the PK of
144717** the row from table t1 is stored instead. Then, as records are extracted from
144718** the sorter to return to the user, the required value of bigblob is
144719** retrieved directly from table t1. If the values are very large, this
144720** can be more efficient than storing them directly in the sorter records.
144721**
144722** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList
144723** for which the sorter-reference optimization should be enabled.
144724** Additionally, the pSort->aDefer[] array is populated with entries
144725** for all cursors required to evaluate all selected expressions. Finally.
144726** output variable (*ppExtra) is set to an expression list containing
144727** expressions for all extra PK values that should be stored in the
144728** sorter records.
144729*/
144730static void selectExprDefer(
144731 Parse *pParse, /* Leave any error here */
144732 SortCtx *pSort, /* Sorter context */
144733 ExprList *pEList, /* Expressions destined for sorter */
144734 ExprList **ppExtra /* Expressions to append to sorter record */
144735){
144736 int i;
144737 int nDefer = 0;
144738 ExprList *pExtra = 0;
144739 for(i=0; i<pEList->nExpr; i++){
144740 struct ExprList_item *pItem = &pEList->a[i];
144741 if( pItem->u.x.iOrderByCol==0 ){
144742 Expr *pExpr = pItem->pExpr;
144743 Table *pTab;
144744 if( pExpr->op==TK_COLUMN168
144745 && pExpr->iColumn>=0
144746 && ALWAYS( ExprUseYTab(pExpr) )((((pExpr)->flags&(0x1000000|0x2000000))==0))
144747 && (pTab = pExpr->y.pTab)!=0
144748 && IsOrdinaryTable(pTab)((pTab)->eTabType==0)
144749 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF0x0010)!=0
144750 ){
144751 int j;
144752 for(j=0; j<nDefer; j++){
144753 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
144754 }
144755 if( j==nDefer ){
144756 if( nDefer==ArraySize(pSort->aDefer)((int)(sizeof(pSort->aDefer)/sizeof(pSort->aDefer[0]))) ){
144757 continue;
144758 }else{
144759 int nKey = 1;
144760 int k;
144761 Index *pPk = 0;
144762 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
144763 pPk = sqlite3PrimaryKeyIndex(pTab);
144764 nKey = pPk->nKeyCol;
144765 }
144766 for(k=0; k<nKey; k++){
144767 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN168, 0, 0);
144768 if( pNew ){
144769 pNew->iTable = pExpr->iTable;
144770 assert( ExprUseYTab(pNew) )((void) (0));
144771 pNew->y.pTab = pExpr->y.pTab;
144772 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
144773 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
144774 }
144775 }
144776 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
144777 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
144778 pSort->aDefer[nDefer].nKey = nKey;
144779 nDefer++;
144780 }
144781 }
144782 pItem->fg.bSorterRef = 1;
144783 }
144784 }
144785 }
144786 pSort->nDefer = (u8)nDefer;
144787 *ppExtra = pExtra;
144788}
144789#endif
144790
144791/*
144792** This routine generates the code for the inside of the inner loop
144793** of a SELECT.
144794**
144795** If srcTab is negative, then the p->pEList expressions
144796** are evaluated in order to get the data for this row. If srcTab is
144797** zero or more, then data is pulled from srcTab and p->pEList is used only
144798** to get the number of columns and the collation sequence for each column.
144799*/
144800static void selectInnerLoop(
144801 Parse *pParse, /* The parser context */
144802 Select *p, /* The complete select statement being coded */
144803 int srcTab, /* Pull data from this table if non-negative */
144804 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
144805 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
144806 SelectDest *pDest, /* How to dispose of the results */
144807 int iContinue, /* Jump here to continue with next row */
144808 int iBreak /* Jump here to break out of the inner loop */
144809){
144810 Vdbe *v = pParse->pVdbe;
144811 int i;
144812 int hasDistinct; /* True if the DISTINCT keyword is present */
144813 int eDest = pDest->eDest; /* How to dispose of results */
144814 int iParm = pDest->iSDParm; /* First argument to disposal method */
144815 int nResultCol; /* Number of result columns */
144816 int nPrefixReg = 0; /* Number of extra registers before regResult */
144817 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
144818
144819 /* Usually, regResult is the first cell in an array of memory cells
144820 ** containing the current result row. In this case regOrig is set to the
144821 ** same value. However, if the results are being sent to the sorter, the
144822 ** values for any expressions that are also part of the sort-key are omitted
144823 ** from this array. In this case regOrig is set to zero. */
144824 int regResult; /* Start of memory holding current results */
144825 int regOrig; /* Start of memory holding full result (or 0) */
144826
144827 assert( v )((void) (0));
144828 assert( p->pEList!=0 )((void) (0));
144829 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP0;
144830 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
144831 if( pSort==0 && !hasDistinct ){
144832 assert( iContinue!=0 )((void) (0));
144833 codeOffset(v, p->iOffset, iContinue);
144834 }
144835
144836 /* Pull the requested columns.
144837 */
144838 nResultCol = p->pEList->nExpr;
144839
144840 if( pDest->iSdst==0 ){
144841 if( pSort ){
144842 nPrefixReg = pSort->pOrderBy->nExpr;
144843 if( !(pSort->sortFlags & SORTFLAG_UseSorter0x01) ) nPrefixReg++;
144844 pParse->nMem += nPrefixReg;
144845 }
144846 pDest->iSdst = pParse->nMem+1;
144847 pParse->nMem += nResultCol;
144848 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
144849 /* This is an error condition that can result, for example, when a SELECT
144850 ** on the right-hand side of an INSERT contains more result columns than
144851 ** there are columns in the table on the left. The error will be caught
144852 ** and reported later. But we need to make sure enough memory is allocated
144853 ** to avoid other spurious errors in the meantime. */
144854 pParse->nMem += nResultCol;
144855 }
144856 pDest->nSdst = nResultCol;
144857 regOrig = regResult = pDest->iSdst;
144858 if( srcTab>=0 ){
144859 for(i=0; i<nResultCol; i++){
144860 sqlite3VdbeAddOp3(v, OP_Column94, srcTab, i, regResult+i);
144861 VdbeComment((v, "%s", p->pEList->a[i].zEName));
144862 }
144863 }else if( eDest!=SRT_Exists3 ){
144864#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144865 ExprList *pExtra = 0;
144866#endif
144867 /* If the destination is an EXISTS(...) expression, the actual
144868 ** values returned by the SELECT are not required.
144869 */
144870 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
144871 ExprList *pEList;
144872 if( eDest==SRT_Mem10 || eDest==SRT_Output9 || eDest==SRT_Coroutine13 ){
144873 ecelFlags = SQLITE_ECEL_DUP0x01;
144874 }else{
144875 ecelFlags = 0;
144876 }
144877 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab12 && eDest!=SRT_Table14 ){
144878 /* For each expression in p->pEList that is a copy of an expression in
144879 ** the ORDER BY clause (pSort->pOrderBy), set the associated
144880 ** iOrderByCol value to one more than the index of the ORDER BY
144881 ** expression within the sort-key that pushOntoSorter() will generate.
144882 ** This allows the p->pEList field to be omitted from the sorted record,
144883 ** saving space and CPU cycles. */
144884 ecelFlags |= (SQLITE_ECEL_OMITREF0x08|SQLITE_ECEL_REF0x04);
144885
144886 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
144887 int j;
144888 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
144889 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
144890 }
144891 }
144892#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144893 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
144894 if( pExtra && pParse->db->mallocFailed==0 ){
144895 /* If there are any extra PK columns to add to the sorter records,
144896 ** allocate extra memory cells and adjust the OpenEphemeral
144897 ** instruction to account for the larger records. This is only
144898 ** required if there are one or more WITHOUT ROWID tables with
144899 ** composite primary keys in the SortCtx.aDefer[] array. */
144900 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
144901 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
144902 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
144903 pParse->nMem += pExtra->nExpr;
144904 }
144905#endif
144906
144907 /* Adjust nResultCol to account for columns that are omitted
144908 ** from the sorter by the optimizations in this branch */
144909 pEList = p->pEList;
144910 for(i=0; i<pEList->nExpr; i++){
144911 if( pEList->a[i].u.x.iOrderByCol>0
144912#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144913 || pEList->a[i].fg.bSorterRef
144914#endif
144915 ){
144916 nResultCol--;
144917 regOrig = 0;
144918 }
144919 }
144920
144921 testcase( regOrig );
144922 testcase( eDest==SRT_Set );
144923 testcase( eDest==SRT_Mem );
144924 testcase( eDest==SRT_Coroutine );
144925 testcase( eDest==SRT_Output );
144926 assert( eDest==SRT_Set || eDest==SRT_Mem((void) (0))
144927 || eDest==SRT_Coroutine || eDest==SRT_Output((void) (0))
144928 || eDest==SRT_Upfrom )((void) (0));
144929 }
144930 sRowLoadInfo.regResult = regResult;
144931 sRowLoadInfo.ecelFlags = ecelFlags;
144932#ifdef SQLITE_ENABLE_SORTER_REFERENCES
144933 sRowLoadInfo.pExtra = pExtra;
144934 sRowLoadInfo.regExtraResult = regResult + nResultCol;
144935 if( pExtra ) nResultCol += pExtra->nExpr;
144936#endif
144937 if( p->iLimit
144938 && (ecelFlags & SQLITE_ECEL_OMITREF0x08)!=0
144939 && nPrefixReg>0
144940 ){
144941 assert( pSort!=0 )((void) (0));
144942 assert( hasDistinct==0 )((void) (0));
144943 pSort->pDeferredRowLoad = &sRowLoadInfo;
144944 regOrig = 0;
144945 }else{
144946 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
144947 }
144948 }
144949
144950 /* If the DISTINCT keyword was present on the SELECT statement
144951 ** and this row has been seen before, then do not make this row
144952 ** part of the result.
144953 */
144954 if( hasDistinct ){
144955 int eType = pDistinct->eTnctType;
144956 int iTab = pDistinct->tabTnct;
144957 assert( nResultCol==p->pEList->nExpr )((void) (0));
144958 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
144959 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
144960 if( pSort==0 ){
144961 codeOffset(v, p->iOffset, iContinue);
144962 }
144963 }
144964
144965 switch( eDest ){
144966 /* In this mode, write each query result to the key of the temporary
144967 ** table iParm.
144968 */
144969#ifndef SQLITE_OMIT_COMPOUND_SELECT
144970 case SRT_Union1: {
144971 int r1;
144972 r1 = sqlite3GetTempReg(pParse);
144973 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r1);
144974 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, nResultCol);
144975 sqlite3ReleaseTempReg(pParse, r1);
144976 break;
144977 }
144978
144979 /* Construct a record from the query result, but instead of
144980 ** saving that record, use it as a key to delete elements from
144981 ** the temporary table iParm.
144982 */
144983 case SRT_Except2: {
144984 sqlite3VdbeAddOp3(v, OP_IdxDelete140, iParm, regResult, nResultCol);
144985 break;
144986 }
144987#endif /* SQLITE_OMIT_COMPOUND_SELECT */
144988
144989 /* Store the result as data using a unique key.
144990 */
144991 case SRT_Fifo8:
144992 case SRT_DistFifo5:
144993 case SRT_Table14:
144994 case SRT_EphemTab12: {
144995 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
144996 testcase( eDest==SRT_Table );
144997 testcase( eDest==SRT_EphemTab );
144998 testcase( eDest==SRT_Fifo );
144999 testcase( eDest==SRT_DistFifo );
145000 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r1+nPrefixReg);
145001#if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG)
145002 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
145003 ** that this is an "UPDATE ... FROM" on a virtual table or view. In this
145004 ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC.
145005 ** This does not affect operation in any way - it just allows MakeRecord
145006 ** to process OPFLAG_NOCHANGE values without an assert() failing. */
145007 if( eDest==SRT_Table14 && pDest->iSDParm2 ){
145008 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC0x6d);
145009 }
145010#endif
145011#ifndef SQLITE_OMIT_CTE
145012 if( eDest==SRT_DistFifo5 ){
145013 /* If the destination is DistFifo, then cursor (iParm+1) is open
145014 ** on an ephemeral index. If the current row is already present
145015 ** in the index, do not write it to the output. If not, add the
145016 ** current row to the index and proceed with writing it to the
145017 ** output table as well. */
145018 int addr = sqlite3VdbeCurrentAddr(v) + 4;
145019 sqlite3VdbeAddOp4Int(v, OP_Found29, iParm+1, addr, r1, 0);
145020 VdbeCoverage(v);
145021 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm+1, r1,regResult,nResultCol);
145022 assert( pSort==0 )((void) (0));
145023 }
145024#endif
145025 if( pSort ){
145026 assert( regResult==regOrig )((void) (0));
145027 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
145028 }else{
145029 int r2 = sqlite3GetTempReg(pParse);
145030 sqlite3VdbeAddOp2(v, OP_NewRowid127, iParm, r2);
145031 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, r2);
145032 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
145033 sqlite3ReleaseTempReg(pParse, r2);
145034 }
145035 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
145036 break;
145037 }
145038
145039 case SRT_Upfrom15: {
145040 if( pSort ){
145041 pushOntoSorter(
145042 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
145043 }else{
145044 int i2 = pDest->iSDParm2;
145045 int r1 = sqlite3GetTempReg(pParse);
145046
145047 /* If the UPDATE FROM join is an aggregate that matches no rows, it
145048 ** might still be trying to return one row, because that is what
145049 ** aggregates do. Don't record that empty row in the output table. */
145050 sqlite3VdbeAddOp2(v, OP_IsNull51, regResult, iBreak); VdbeCoverage(v);
145051
145052 sqlite3VdbeAddOp3(v, OP_MakeRecord97,
145053 regResult+(i2<0), nResultCol-(i2<0), r1);
145054 if( i2<0 ){
145055 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, regResult);
145056 }else{
145057 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, i2);
145058 }
145059 }
145060 break;
145061 }
145062
145063#ifndef SQLITE_OMIT_SUBQUERY
145064 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
145065 ** then there should be a single item on the stack. Write this
145066 ** item into the set table with bogus data.
145067 */
145068 case SRT_Set11: {
145069 if( pSort ){
145070 /* At first glance you would think we could optimize out the
145071 ** ORDER BY in this case since the order of entries in the set
145072 ** does not matter. But there might be a LIMIT clause, in which
145073 ** case the order does matter */
145074 pushOntoSorter(
145075 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
145076 pDest->iSDParm2 = 0; /* Signal that any Bloom filter is unpopulated */
145077 }else{
145078 int r1 = sqlite3GetTempReg(pParse);
145079 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol )((void) (0));
145080 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regResult, nResultCol,
145081 r1, pDest->zAffSdst, nResultCol);
145082 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regResult, nResultCol);
145083 if( pDest->iSDParm2 ){
145084 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pDest->iSDParm2, 0,
145085 regResult, nResultCol);
145086 ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER"))sqlite3VdbeExplain (pParse, 0, "CREATE BLOOM FILTER");
145087 }
145088 sqlite3ReleaseTempReg(pParse, r1);
145089 }
145090 break;
145091 }
145092
145093
145094 /* If any row exist in the result set, record that fact and abort.
145095 */
145096 case SRT_Exists3: {
145097 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iParm);
145098 /* The LIMIT clause will terminate the loop for us */
145099 break;
145100 }
145101
145102 /* If this is a scalar select that is part of an expression, then
145103 ** store the results in the appropriate memory cell or array of
145104 ** memory cells and break out of the scan loop.
145105 */
145106 case SRT_Mem10: {
145107 if( pSort ){
145108 assert( nResultCol<=pDest->nSdst )((void) (0));
145109 pushOntoSorter(
145110 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
145111 }else{
145112 assert( nResultCol==pDest->nSdst )((void) (0));
145113 assert( regResult==iParm )((void) (0));
145114 /* The LIMIT clause will jump out of the loop for us */
145115 }
145116 break;
145117 }
145118#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
145119
145120 case SRT_Coroutine13: /* Send data to a co-routine */
145121 case SRT_Output9: { /* Return the results */
145122 testcase( eDest==SRT_Coroutine );
145123 testcase( eDest==SRT_Output );
145124 if( pSort ){
145125 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
145126 nPrefixReg);
145127 }else if( eDest==SRT_Coroutine13 ){
145128 sqlite3VdbeAddOp1(v, OP_Yield12, pDest->iSDParm);
145129 }else{
145130 sqlite3VdbeAddOp2(v, OP_ResultRow84, regResult, nResultCol);
145131 }
145132 break;
145133 }
145134
145135#ifndef SQLITE_OMIT_CTE
145136 /* Write the results into a priority queue that is order according to
145137 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
145138 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
145139 ** pSO->nExpr columns, then make sure all keys are unique by adding a
145140 ** final OP_Sequence column. The last column is the record as a blob.
145141 */
145142 case SRT_DistQueue6:
145143 case SRT_Queue7: {
145144 int nKey;
145145 int r1, r2, r3;
145146 int addrTest = 0;
145147 ExprList *pSO;
145148 pSO = pDest->pOrderBy;
145149 assert( pSO )((void) (0));
145150 nKey = pSO->nExpr;
145151 r1 = sqlite3GetTempReg(pParse);
145152 r2 = sqlite3GetTempRange(pParse, nKey+2);
145153 r3 = r2+nKey+1;
145154 if( eDest==SRT_DistQueue6 ){
145155 /* If the destination is DistQueue, then cursor (iParm+1) is open
145156 ** on a second ephemeral index that holds all values every previously
145157 ** added to the queue. */
145158 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found29, iParm+1, 0,
145159 regResult, nResultCol);
145160 VdbeCoverage(v);
145161 }
145162 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regResult, nResultCol, r3);
145163 if( eDest==SRT_DistQueue6 ){
145164 sqlite3VdbeAddOp2(v, OP_IdxInsert138, iParm+1, r3);
145165 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
145166 }
145167 for(i=0; i<nKey; i++){
145168 sqlite3VdbeAddOp2(v, OP_SCopy81,
145169 regResult + pSO->a[i].u.x.iOrderByCol - 1,
145170 r2+i);
145171 }
145172 sqlite3VdbeAddOp2(v, OP_Sequence126, iParm, r2+nKey);
145173 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r2, nKey+2, r1);
145174 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, r2, nKey+2);
145175 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
145176 sqlite3ReleaseTempReg(pParse, r1);
145177 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
145178 break;
145179 }
145180#endif /* SQLITE_OMIT_CTE */
145181
145182
145183
145184#if !defined(SQLITE_OMIT_TRIGGER)
145185 /* Discard the results. This is used for SELECT statements inside
145186 ** the body of a TRIGGER. The purpose of such selects is to call
145187 ** user-defined functions that have side effects. We do not care
145188 ** about the actual results of the select.
145189 */
145190 default: {
145191 assert( eDest==SRT_Discard )((void) (0));
145192 break;
145193 }
145194#endif
145195 }
145196
145197 /* Jump to the end of the loop if the LIMIT is reached. Except, if
145198 ** there is a sorter, in which case the sorter has already limited
145199 ** the output for us.
145200 */
145201 if( pSort==0 && p->iLimit ){
145202 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, p->iLimit, iBreak); VdbeCoverage(v);
145203 }
145204}
145205
145206/*
145207** Allocate a KeyInfo object sufficient for an index of N key columns and
145208** X extra columns.
145209*/
145210SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
145211 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
145212 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
145213 if( p ){
145214 p->aSortFlags = (u8*)&p->aColl[N+X];
145215 p->nKeyField = (u16)N;
145216 p->nAllField = (u16)(N+X);
145217 p->enc = ENC(db)((db)->enc);
145218 p->db = db;
145219 p->nRef = 1;
145220 memset(&p[1], 0, nExtra);
145221 }else{
145222 return (KeyInfo*)sqlite3OomFault(db);
145223 }
145224 return p;
145225}
145226
145227/*
145228** Deallocate a KeyInfo object
145229*/
145230SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo *p){
145231 if( p ){
145232 assert( p->db!=0 )((void) (0));
145233 assert( p->nRef>0 )((void) (0));
145234 p->nRef--;
145235 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
145236 }
145237}
145238
145239/*
145240** Make a new pointer to a KeyInfo object
145241*/
145242SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
145243 if( p ){
145244 assert( p->nRef>0 )((void) (0));
145245 p->nRef++;
145246 }
145247 return p;
145248}
145249
145250#ifdef SQLITE_DEBUG
145251/*
145252** Return TRUE if a KeyInfo object can be change. The KeyInfo object
145253** can only be changed if this is just a single reference to the object.
145254**
145255** This routine is used only inside of assert() statements.
145256*/
145257SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
145258#endif /* SQLITE_DEBUG */
145259
145260/*
145261** Given an expression list, generate a KeyInfo structure that records
145262** the collating sequence for each expression in that expression list.
145263**
145264** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
145265** KeyInfo structure is appropriate for initializing a virtual index to
145266** implement that clause. If the ExprList is the result set of a SELECT
145267** then the KeyInfo structure is appropriate for initializing a virtual
145268** index to implement a DISTINCT test.
145269**
145270** Space to hold the KeyInfo structure is obtained from malloc. The calling
145271** function is responsible for seeing that this structure is eventually
145272** freed.
145273*/
145274SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(
145275 Parse *pParse, /* Parsing context */
145276 ExprList *pList, /* Form the KeyInfo object from this ExprList */
145277 int iStart, /* Begin with this column of pList */
145278 int nExtra /* Add this many extra columns to the end */
145279){
145280 int nExpr;
145281 KeyInfo *pInfo;
145282 struct ExprList_item *pItem;
145283 sqlite3 *db = pParse->db;
145284 int i;
145285
145286 nExpr = pList->nExpr;
145287 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
145288 if( pInfo ){
145289 assert( sqlite3KeyInfoIsWriteable(pInfo) )((void) (0));
145290 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
145291 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
145292 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
145293 }
145294 }
145295 return pInfo;
145296}
145297
145298/*
145299** Name of the connection operator, used for error messages.
145300*/
145301SQLITE_PRIVATEstatic const char *sqlite3SelectOpName(int id){
145302 char *z;
145303 switch( id ){
145304 case TK_ALL136: z = "UNION ALL"; break;
145305 case TK_INTERSECT138: z = "INTERSECT"; break;
145306 case TK_EXCEPT137: z = "EXCEPT"; break;
145307 default: z = "UNION"; break;
145308 }
145309 return z;
145310}
145311
145312#ifndef SQLITE_OMIT_EXPLAIN
145313/*
145314** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
145315** is a no-op. Otherwise, it adds a single row of output to the EQP result,
145316** where the caption is of the form:
145317**
145318** "USE TEMP B-TREE FOR xxx"
145319**
145320** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
145321** is determined by the zUsage argument.
145322*/
145323static void explainTempTable(Parse *pParse, const char *zUsage){
145324 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s", zUsage
)
;
145325}
145326
145327/*
145328** Assign expression b to lvalue a. A second, no-op, version of this macro
145329** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
145330** in sqlite3Select() to assign values to structure member variables that
145331** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
145332** code with #ifndef directives.
145333*/
145334# define explainSetInteger(a, b)a = b a = b
145335
145336#else
145337/* No-op versions of the explainXXX() functions and macros. */
145338# define explainTempTable(y,z)
145339# define explainSetInteger(y,z)y = z
145340#endif
145341
145342
145343/*
145344** If the inner loop was generated using a non-null pOrderBy argument,
145345** then the results were placed in a sorter. After the loop is terminated
145346** we need to run the sorter and output the results. The following
145347** routine generates the code needed to do that.
145348*/
145349static void generateSortTail(
145350 Parse *pParse, /* Parsing context */
145351 Select *p, /* The SELECT statement */
145352 SortCtx *pSort, /* Information on the ORDER BY clause */
145353 int nColumn, /* Number of columns of data */
145354 SelectDest *pDest /* Write the sorted results here */
145355){
145356 Vdbe *v = pParse->pVdbe; /* The prepared statement */
145357 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
145358 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
145359 int addr; /* Top of output loop. Jump for Next. */
145360 int addrOnce = 0;
145361 int iTab;
145362 ExprList *pOrderBy = pSort->pOrderBy;
145363 int eDest = pDest->eDest;
145364 int iParm = pDest->iSDParm;
145365 int regRow;
145366 int regRowid;
145367 int iCol;
145368 int nKey; /* Number of key columns in sorter record */
145369 int iSortTab; /* Sorter cursor to read from */
145370 int i;
145371 int bSeq; /* True if sorter record includes seq. no. */
145372 int nRefKey = 0;
145373 struct ExprList_item *aOutEx = p->pEList->a;
145374#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
145375 int addrExplain; /* Address of OP_Explain instruction */
145376#endif
145377
145378 nKey = pOrderBy->nExpr - pSort->nOBSat;
145379 if( pSort->nOBSat==0 || nKey==1 ){
145380 ExplainQueryPlan2(addrExplain, (pParse, 0,(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %sORDER BY"
, pSort->nOBSat?"LAST TERM OF ":"" ))
145381 "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 ":"" ))
145382 ))(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %sORDER BY"
, pSort->nOBSat?"LAST TERM OF ":"" ))
;
145383 }else{
145384 ExplainQueryPlan2(addrExplain, (pParse, 0,(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY"
, nKey ))
145385 "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 ))
145386 ))(addrExplain = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY"
, nKey ))
;
145387 }
145388 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
145389 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
145390
145391
145392 assert( addrBreak<0 )((void) (0));
145393 if( pSort->labelBkOut ){
145394 sqlite3VdbeAddOp2(v, OP_Gosub10, pSort->regReturn, pSort->labelBkOut);
145395 sqlite3VdbeGoto(v, addrBreak);
145396 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
145397 }
145398
145399#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145400 /* Open any cursors needed for sorter-reference expressions */
145401 for(i=0; i<pSort->nDefer; i++){
145402 Table *pTab = pSort->aDefer[i].pTab;
145403 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
145404 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead102);
145405 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey)((nRefKey)>(pSort->aDefer[i].nKey)?(nRefKey):(pSort->
aDefer[i].nKey))
;
145406 }
145407#endif
145408
145409 iTab = pSort->iECursor;
145410 if( eDest==SRT_Output9 || eDest==SRT_Coroutine13 || eDest==SRT_Mem10 ){
145411 if( eDest==SRT_Mem10 && p->iOffset ){
145412 sqlite3VdbeAddOp2(v, OP_Null75, 0, pDest->iSdst);
145413 }
145414 regRowid = 0;
145415 regRow = pDest->iSdst;
145416 }else{
145417 regRowid = sqlite3GetTempReg(pParse);
145418 if( eDest==SRT_EphemTab12 || eDest==SRT_Table14 ){
145419 regRow = sqlite3GetTempReg(pParse);
145420 nColumn = 0;
145421 }else{
145422 regRow = sqlite3GetTempRange(pParse, nColumn);
145423 }
145424 }
145425 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
145426 int regSortOut = ++pParse->nMem;
145427 iSortTab = pParse->nTab++;
145428 if( pSort->labelBkOut ){
145429 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
145430 }
145431 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, iSortTab, regSortOut,
145432 nKey+1+nColumn+nRefKey);
145433 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
145434 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort34, iTab, addrBreak);
145435 VdbeCoverage(v);
145436 assert( p->iLimit==0 && p->iOffset==0 )((void) (0));
145437 sqlite3VdbeAddOp3(v, OP_SorterData133, iTab, regSortOut, iSortTab);
145438 bSeq = 0;
145439 }else{
145440 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort35, iTab, addrBreak); VdbeCoverage(v);
145441 codeOffset(v, p->iOffset, addrContinue);
145442 iSortTab = iTab;
145443 bSeq = 1;
145444 if( p->iOffset>0 ){
145445 sqlite3VdbeAddOp2(v, OP_AddImm86, p->iLimit, -1);
145446 }
145447 }
145448 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
145449#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145450 if( aOutEx[i].fg.bSorterRef ) continue;
145451#endif
145452 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
145453 }
145454#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145455 if( pSort->nDefer ){
145456 int iKey = iCol+1;
145457 int regKey = sqlite3GetTempRange(pParse, nRefKey);
145458
145459 for(i=0; i<pSort->nDefer; i++){
145460 int iCsr = pSort->aDefer[i].iCsr;
145461 Table *pTab = pSort->aDefer[i].pTab;
145462 int nKey = pSort->aDefer[i].nKey;
145463
145464 sqlite3VdbeAddOp1(v, OP_NullRow136, iCsr);
145465 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
145466 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iKey++, regKey);
145467 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iCsr,
145468 sqlite3VdbeCurrentAddr(v)+1, regKey);
145469 }else{
145470 int k;
145471 int iJmp;
145472 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey )((void) (0));
145473 for(k=0; k<nKey; k++){
145474 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iKey++, regKey+k);
145475 }
145476 iJmp = sqlite3VdbeCurrentAddr(v);
145477 sqlite3VdbeAddOp4Int(v, OP_SeekGE23, iCsr, iJmp+2, regKey, nKey);
145478 sqlite3VdbeAddOp4Int(v, OP_IdxLE40, iCsr, iJmp+3, regKey, nKey);
145479 sqlite3VdbeAddOp1(v, OP_NullRow136, iCsr);
145480 }
145481 }
145482 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
145483 }
145484#endif
145485 for(i=nColumn-1; i>=0; i--){
145486#ifdef SQLITE_ENABLE_SORTER_REFERENCES
145487 if( aOutEx[i].fg.bSorterRef ){
145488 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
145489 }else
145490#endif
145491 {
145492 int iRead;
145493 if( aOutEx[i].u.x.iOrderByCol ){
145494 iRead = aOutEx[i].u.x.iOrderByCol-1;
145495 }else{
145496 iRead = iCol--;
145497 }
145498 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, iRead, regRow+i);
145499 VdbeComment((v, "%s", aOutEx[i].zEName));
145500 }
145501 }
145502 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
145503 switch( eDest ){
145504 case SRT_Table14:
145505 case SRT_EphemTab12: {
145506 sqlite3VdbeAddOp3(v, OP_Column94, iSortTab, nKey+bSeq, regRow);
145507 sqlite3VdbeAddOp2(v, OP_NewRowid127, iParm, regRowid);
145508 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, regRow, regRowid);
145509 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
145510 break;
145511 }
145512#ifndef SQLITE_OMIT_SUBQUERY
145513 case SRT_Set11: {
145514 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) )((void) (0));
145515 sqlite3VdbeAddOp4(v, OP_MakeRecord97, regRow, nColumn, regRowid,
145516 pDest->zAffSdst, nColumn);
145517 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, regRowid, regRow, nColumn);
145518 break;
145519 }
145520 case SRT_Mem10: {
145521 /* The LIMIT clause will terminate the loop for us */
145522 break;
145523 }
145524#endif
145525 case SRT_Upfrom15: {
145526 int i2 = pDest->iSDParm2;
145527 int r1 = sqlite3GetTempReg(pParse);
145528 sqlite3VdbeAddOp3(v, OP_MakeRecord97,regRow+(i2<0),nColumn-(i2<0),r1);
145529 if( i2<0 ){
145530 sqlite3VdbeAddOp3(v, OP_Insert128, iParm, r1, regRow);
145531 }else{
145532 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iParm, r1, regRow, i2);
145533 }
145534 break;
145535 }
145536 default: {
145537 assert( eDest==SRT_Output || eDest==SRT_Coroutine )((void) (0));
145538 testcase( eDest==SRT_Output );
145539 testcase( eDest==SRT_Coroutine );
145540 if( eDest==SRT_Output9 ){
145541 sqlite3VdbeAddOp2(v, OP_ResultRow84, pDest->iSdst, nColumn);
145542 }else{
145543 sqlite3VdbeAddOp1(v, OP_Yield12, pDest->iSDParm);
145544 }
145545 break;
145546 }
145547 }
145548 if( regRowid ){
145549 if( eDest==SRT_Set11 ){
145550 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
145551 }else{
145552 sqlite3ReleaseTempReg(pParse, regRow);
145553 }
145554 sqlite3ReleaseTempReg(pParse, regRowid);
145555 }
145556 /* The bottom of the loop
145557 */
145558 sqlite3VdbeResolveLabel(v, addrContinue);
145559 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
145560 sqlite3VdbeAddOp2(v, OP_SorterNext37, iTab, addr); VdbeCoverage(v);
145561 }else{
145562 sqlite3VdbeAddOp2(v, OP_Next39, iTab, addr); VdbeCoverage(v);
145563 }
145564 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
145565 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return67, pSort->regReturn);
145566 sqlite3VdbeResolveLabel(v, addrBreak);
145567}
145568
145569/*
145570** Return a pointer to a string containing the 'declaration type' of the
145571** expression pExpr. The string may be treated as static by the caller.
145572**
145573** The declaration type is the exact datatype definition extracted from the
145574** original CREATE TABLE statement if the expression is a column. The
145575** declaration type for a ROWID field is INTEGER. Exactly when an expression
145576** is considered a column can be complex in the presence of subqueries. The
145577** result-set expression in all of the following SELECT statements is
145578** considered a column by this function.
145579**
145580** SELECT col FROM tbl;
145581** SELECT (SELECT col FROM tbl;
145582** SELECT (SELECT col FROM tbl);
145583** SELECT abc FROM (SELECT col AS abc FROM tbl);
145584**
145585** The declaration type for any expression other than a column is NULL.
145586**
145587** This routine has either 3 or 6 parameters depending on whether or not
145588** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
145589*/
145590#ifdef SQLITE_ENABLE_COLUMN_METADATA1
145591# define columnType(A,B,C,D,E)columnTypeImpl(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
145592#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
145593# define columnType(A,B,C,D,E)columnTypeImpl(A,B,C,D,E) columnTypeImpl(A,B)
145594#endif
145595static const char *columnTypeImpl(
145596 NameContext *pNC,
145597#ifndef SQLITE_ENABLE_COLUMN_METADATA1
145598 Expr *pExpr
145599#else
145600 Expr *pExpr,
145601 const char **pzOrigDb,
145602 const char **pzOrigTab,
145603 const char **pzOrigCol
145604#endif
145605){
145606 char const *zType = 0;
145607 int j;
145608#ifdef SQLITE_ENABLE_COLUMN_METADATA1
145609 char const *zOrigDb = 0;
145610 char const *zOrigTab = 0;
145611 char const *zOrigCol = 0;
145612#endif
145613
145614 assert( pExpr!=0 )((void) (0));
145615 assert( pNC->pSrcList!=0 )((void) (0));
145616 switch( pExpr->op ){
145617 case TK_COLUMN168: {
145618 /* The expression is a column. Locate the table the column is being
145619 ** extracted from in NameContext.pSrcList. This table may be real
145620 ** database table or a subquery.
145621 */
145622 Table *pTab = 0; /* Table structure column is extracted from */
145623 Select *pS = 0; /* Select the column is extracted from */
145624 int iCol = pExpr->iColumn; /* Index of column in pTab */
145625 while( pNC && !pTab ){
145626 SrcList *pTabList = pNC->pSrcList;
145627 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
145628 if( j<pTabList->nSrc ){
145629 pTab = pTabList->a[j].pSTab;
145630 if( pTabList->a[j].fg.isSubquery ){
145631 pS = pTabList->a[j].u4.pSubq->pSelect;
145632 }else{
145633 pS = 0;
145634 }
145635 }else{
145636 pNC = pNC->pNext;
145637 }
145638 }
145639
145640 if( pTab==0 ){
145641 /* At one time, code such as "SELECT new.x" within a trigger would
145642 ** cause this condition to run. Since then, we have restructured how
145643 ** trigger code is generated and so this condition is no longer
145644 ** possible. However, it can still be true for statements like
145645 ** the following:
145646 **
145647 ** CREATE TABLE t1(col INTEGER);
145648 ** SELECT (SELECT t1.col) FROM FROM t1;
145649 **
145650 ** when columnType() is called on the expression "t1.col" in the
145651 ** sub-select. In this case, set the column type to NULL, even
145652 ** though it should really be "INTEGER".
145653 **
145654 ** This is not a problem, as the column type of "t1.col" is never
145655 ** used. When columnType() is called on the expression
145656 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
145657 ** branch below. */
145658 break;
145659 }
145660
145661 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab )((void) (0));
145662 if( pS ){
145663 /* The "table" is actually a sub-select or a view in the FROM clause
145664 ** of the SELECT statement. Return the declaration type and origin
145665 ** data for the result-set column of the sub-select.
145666 */
145667 if( iCol<pS->pEList->nExpr
145668 && (!ViewCanHaveRowid0 || iCol>=0)
145669 ){
145670 /* If iCol is less than zero, then the expression requests the
145671 ** rowid of the sub-select or view. This expression is legal (see
145672 ** test case misc2.2.2) - it always evaluates to NULL.
145673 */
145674 NameContext sNC;
145675 Expr *p = pS->pEList->a[iCol].pExpr;
145676 sNC.pSrcList = pS->pSrc;
145677 sNC.pNext = pNC;
145678 sNC.pParse = pNC->pParse;
145679 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
145680 }
145681 }else{
145682 /* A real table or a CTE table */
145683 assert( !pS )((void) (0));
145684#ifdef SQLITE_ENABLE_COLUMN_METADATA1
145685 if( iCol<0 ) iCol = pTab->iPKey;
145686 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
145687 if( iCol<0 ){
145688 zType = "INTEGER";
145689 zOrigCol = "rowid";
145690 }else{
145691 zOrigCol = pTab->aCol[iCol].zCnName;
145692 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
145693 }
145694 zOrigTab = pTab->zName;
145695 if( pNC->pParse && pTab->pSchema ){
145696 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
145697 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
145698 }
145699#else
145700 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
145701 if( iCol<0 ){
145702 zType = "INTEGER";
145703 }else{
145704 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
145705 }
145706#endif
145707 }
145708 break;
145709 }
145710#ifndef SQLITE_OMIT_SUBQUERY
145711 case TK_SELECT139: {
145712 /* The expression is a sub-select. Return the declaration type and
145713 ** origin info for the single column in the result set of the SELECT
145714 ** statement.
145715 */
145716 NameContext sNC;
145717 Select *pS;
145718 Expr *p;
145719 assert( ExprUseXSelect(pExpr) )((void) (0));
145720 pS = pExpr->x.pSelect;
145721 p = pS->pEList->a[0].pExpr;
145722 sNC.pSrcList = pS->pSrc;
145723 sNC.pNext = pNC;
145724 sNC.pParse = pNC->pParse;
145725 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
145726 break;
145727 }
145728#endif
145729 }
145730
145731#ifdef SQLITE_ENABLE_COLUMN_METADATA1
145732 if( pzOrigDb ){
145733 assert( pzOrigTab && pzOrigCol )((void) (0));
145734 *pzOrigDb = zOrigDb;
145735 *pzOrigTab = zOrigTab;
145736 *pzOrigCol = zOrigCol;
145737 }
145738#endif
145739 return zType;
145740}
145741
145742/*
145743** Generate code that will tell the VDBE the declaration types of columns
145744** in the result set.
145745*/
145746static void generateColumnTypes(
145747 Parse *pParse, /* Parser context */
145748 SrcList *pTabList, /* List of tables */
145749 ExprList *pEList /* Expressions defining the result set */
145750){
145751#ifndef SQLITE_OMIT_DECLTYPE
145752 Vdbe *v = pParse->pVdbe;
145753 int i;
145754 NameContext sNC;
145755 sNC.pSrcList = pTabList;
145756 sNC.pParse = pParse;
145757 sNC.pNext = 0;
145758 for(i=0; i<pEList->nExpr; i++){
145759 Expr *p = pEList->a[i].pExpr;
145760 const char *zType;
145761#ifdef SQLITE_ENABLE_COLUMN_METADATA1
145762 const char *zOrigDb = 0;
145763 const char *zOrigTab = 0;
145764 const char *zOrigCol = 0;
145765 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p,&zOrigDb,&zOrigTab,&zOrigCol
)
;
145766
145767 /* The vdbe must make its own copy of the column-type and other
145768 ** column specific strings, in case the schema is reset before this
145769 ** virtual machine is deleted.
145770 */
145771 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE2, zOrigDb, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
145772 sqlite3VdbeSetColName(v, i, COLNAME_TABLE3, zOrigTab, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
145773 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN4, zOrigCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
145774#else
145775 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p,0,0,0);
145776#endif
145777 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE1, zType, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
145778 }
145779#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
145780}
145781
145782
145783/*
145784** Compute the column names for a SELECT statement.
145785**
145786** The only guarantee that SQLite makes about column names is that if the
145787** column has an AS clause assigning it a name, that will be the name used.
145788** That is the only documented guarantee. However, countless applications
145789** developed over the years have made baseless assumptions about column names
145790** and will break if those assumptions changes. Hence, use extreme caution
145791** when modifying this routine to avoid breaking legacy.
145792**
145793** See Also: sqlite3ColumnsFromExprList()
145794**
145795** The PRAGMA short_column_names and PRAGMA full_column_names settings are
145796** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
145797** applications should operate this way. Nevertheless, we need to support the
145798** other modes for legacy:
145799**
145800** short=OFF, full=OFF: Column name is the text of the expression has it
145801** originally appears in the SELECT statement. In
145802** other words, the zSpan of the result expression.
145803**
145804** short=ON, full=OFF: (This is the default setting). If the result
145805** refers directly to a table column, then the
145806** result column name is just the table column
145807** name: COLUMN. Otherwise use zSpan.
145808**
145809** full=ON, short=ANY: If the result refers directly to a table column,
145810** then the result column name with the table name
145811** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
145812*/
145813SQLITE_PRIVATEstatic void sqlite3GenerateColumnNames(
145814 Parse *pParse, /* Parser context */
145815 Select *pSelect /* Generate column names for this SELECT statement */
145816){
145817 Vdbe *v = pParse->pVdbe;
145818 int i;
145819 Table *pTab;
145820 SrcList *pTabList;
145821 ExprList *pEList;
145822 sqlite3 *db = pParse->db;
145823 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
145824 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
145825
145826 if( pParse->colNamesSet ) return;
145827 /* Column names are determined by the left-most term of a compound select */
145828 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145829 TREETRACE(0x80,pParse,pSelect,("generating column names\n"));
145830 pTabList = pSelect->pSrc;
145831 pEList = pSelect->pEList;
145832 assert( v!=0 )((void) (0));
145833 assert( pTabList!=0 )((void) (0));
145834 pParse->colNamesSet = 1;
145835 fullName = (db->flags & SQLITE_FullColNames0x00000004)!=0;
145836 srcName = (db->flags & SQLITE_ShortColNames0x00000040)!=0 || fullName;
145837 sqlite3VdbeSetNumCols(v, pEList->nExpr);
145838 for(i=0; i<pEList->nExpr; i++){
145839 Expr *p = pEList->a[i].pExpr;
145840
145841 assert( p!=0 )((void) (0));
145842 assert( p->op!=TK_AGG_COLUMN )((void) (0)); /* Agg processing has not run yet */
145843 assert( p->op!=TK_COLUMN((void) (0))
145844 || (ExprUseYTab(p) && p->y.pTab!=0) )((void) (0)); /* Covering idx not yet coded */
145845 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME0 ){
145846 /* An AS clause always takes first priority */
145847 char *zName = pEList->a[i].zEName;
145848 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
145849 }else if( srcName && p->op==TK_COLUMN168 ){
145850 char *zCol;
145851 int iCol = p->iColumn;
145852 pTab = p->y.pTab;
145853 assert( pTab!=0 )((void) (0));
145854 if( iCol<0 ) iCol = pTab->iPKey;
145855 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
145856 if( iCol<0 ){
145857 zCol = "rowid";
145858 }else{
145859 zCol = pTab->aCol[iCol].zCnName;
145860 }
145861 if( fullName ){
145862 char *zName = 0;
145863 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
145864 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
145865 }else{
145866 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
145867 }
145868 }else{
145869 const char *z = pEList->a[i].zEName;
145870 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
145871 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, z, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
145872 }
145873 }
145874 generateColumnTypes(pParse, pTabList, pEList);
145875}
145876
145877/*
145878** Given an expression list (which is really the list of expressions
145879** that form the result set of a SELECT statement) compute appropriate
145880** column names for a table that would hold the expression list.
145881**
145882** All column names will be unique.
145883**
145884** Only the column names are computed. Column.zType, Column.zColl,
145885** and other fields of Column are zeroed.
145886**
145887** Return SQLITE_OK on success. If a memory allocation error occurs,
145888** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
145889**
145890** The only guarantee that SQLite makes about column names is that if the
145891** column has an AS clause assigning it a name, that will be the name used.
145892** That is the only documented guarantee. However, countless applications
145893** developed over the years have made baseless assumptions about column names
145894** and will break if those assumptions changes. Hence, use extreme caution
145895** when modifying this routine to avoid breaking legacy.
145896**
145897** See Also: sqlite3GenerateColumnNames()
145898*/
145899SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(
145900 Parse *pParse, /* Parsing context */
145901 ExprList *pEList, /* Expr list from which to derive column names */
145902 i16 *pnCol, /* Write the number of columns here */
145903 Column **paCol /* Write the new column list here */
145904){
145905 sqlite3 *db = pParse->db; /* Database connection */
145906 int i, j; /* Loop counters */
145907 u32 cnt; /* Index added to make the name unique */
145908 Column *aCol, *pCol; /* For looping over result columns */
145909 int nCol; /* Number of columns in the result set */
145910 char *zName; /* Column name */
145911 int nName; /* Size of name in zName[] */
145912 Hash ht; /* Hash table of column names */
145913 Table *pTab;
145914
145915 sqlite3HashInit(&ht);
145916 if( pEList ){
145917 nCol = pEList->nExpr;
145918 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
145919 testcase( aCol==0 );
145920 if( NEVER(nCol>32767)(nCol>32767) ) nCol = 32767;
145921 }else{
145922 nCol = 0;
145923 aCol = 0;
145924 }
145925 assert( nCol==(i16)nCol )((void) (0));
145926 *pnCol = nCol;
145927 *paCol = aCol;
145928
145929 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
145930 struct ExprList_item *pX = &pEList->a[i];
145931 struct ExprList_item *pCollide;
145932 /* Get an appropriate name for the column
145933 */
145934 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME0 ){
145935 /* If the column contains an "AS <name>" phrase, use <name> as the name */
145936 }else{
145937 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
145938 while( ALWAYS(pColExpr!=0)(pColExpr!=0) && pColExpr->op==TK_DOT142 ){
145939 pColExpr = pColExpr->pRight;
145940 assert( pColExpr!=0 )((void) (0));
145941 }
145942 if( pColExpr->op==TK_COLUMN168
145943 && ALWAYS( ExprUseYTab(pColExpr) )((((pColExpr)->flags&(0x1000000|0x2000000))==0))
145944 && ALWAYS( pColExpr->y.pTab!=0 )(pColExpr->y.pTab!=0)
145945 ){
145946 /* For columns use the column name name */
145947 int iCol = pColExpr->iColumn;
145948 pTab = pColExpr->y.pTab;
145949 if( iCol<0 ) iCol = pTab->iPKey;
145950 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
145951 }else if( pColExpr->op==TK_ID60 ){
145952 assert( !ExprHasProperty(pColExpr, EP_IntValue) )((void) (0));
145953 zName = pColExpr->u.zToken;
145954 }else{
145955 /* Use the original text of the column expression as its name */
145956 assert( zName==pX->zEName )((void) (0)); /* pointer comparison intended */
145957 }
145958 }
145959 if( zName && !sqlite3IsTrueOrFalse(zName) ){
145960 zName = sqlite3DbStrDup(db, zName);
145961 }else{
145962 zName = sqlite3MPrintf(db,"column%d",i+1);
145963 }
145964
145965 /* Make sure the column name is unique. If the name is not unique,
145966 ** append an integer to the name so that it becomes unique.
145967 */
145968 cnt = 0;
145969 while( zName && (pCollide = sqlite3HashFind(&ht, zName))!=0 ){
145970 if( pCollide->fg.bUsingTerm ){
145971 pCol->colFlags |= COLFLAG_NOEXPAND0x0400;
145972 }
145973 nName = sqlite3Strlen30(zName);
145974 if( nName>0 ){
145975 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j])(sqlite3CtypeMap[(unsigned char)(zName[j])]&0x04); j--){}
145976 if( zName[j]==':' ) nName = j;
145977 }
145978 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
145979 sqlite3ProgressCheck(pParse);
145980 if( cnt>3 ){
145981 sqlite3_randomness(sizeof(cnt), &cnt);
145982 }
145983 }
145984 pCol->zCnName = zName;
145985 pCol->hName = sqlite3StrIHash(zName);
145986 if( pX->fg.bNoExpand ){
145987 pCol->colFlags |= COLFLAG_NOEXPAND0x0400;
145988 }
145989 sqlite3ColumnPropertiesFromName(0, pCol);
145990 if( zName && sqlite3HashInsert(&ht, zName, pX)==pX ){
145991 sqlite3OomFault(db);
145992 }
145993 }
145994 sqlite3HashClear(&ht);
145995 if( pParse->nErr ){
145996 for(j=0; j<i; j++){
145997 sqlite3DbFree(db, aCol[j].zCnName);
145998 }
145999 sqlite3DbFree(db, aCol);
146000 *paCol = 0;
146001 *pnCol = 0;
146002 return pParse->rc;
146003 }
146004 return SQLITE_OK0;
146005}
146006
146007/*
146008** pTab is a transient Table object that represents a subquery of some
146009** kind (maybe a parenthesized subquery in the FROM clause of a larger
146010** query, or a VIEW, or a CTE). This routine computes type information
146011** for that Table object based on the Select object that implements the
146012** subquery. For the purposes of this routine, "type information" means:
146013**
146014** * The datatype name, as it might appear in a CREATE TABLE statement
146015** * Which collating sequence to use for the column
146016** * The affinity of the column
146017*/
146018SQLITE_PRIVATEstatic void sqlite3SubqueryColumnTypes(
146019 Parse *pParse, /* Parsing contexts */
146020 Table *pTab, /* Add column type information to this table */
146021 Select *pSelect, /* SELECT used to determine types and collations */
146022 char aff /* Default affinity. */
146023){
146024 sqlite3 *db = pParse->db;
146025 Column *pCol;
146026 CollSeq *pColl;
146027 int i,j;
146028 Expr *p;
146029 struct ExprList_item *a;
146030 NameContext sNC;
146031
146032 assert( pSelect!=0 )((void) (0));
146033 assert( (pSelect->selFlags & SF_Resolved)!=0 )((void) (0));
146034 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 )((void) (0));
146035 assert( aff==SQLITE_AFF_NONE || aff==SQLITE_AFF_BLOB )((void) (0));
146036 if( db->mallocFailed || IN_RENAME_OBJECT(pParse->eParseMode>=2) ) return;
146037 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
146038 a = pSelect->pEList->a;
146039 memset(&sNC, 0, sizeof(sNC));
146040 sNC.pSrcList = pSelect->pSrc;
146041 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
146042 const char *zType;
146043 i64 n;
146044 int m = 0;
146045 Select *pS2 = pSelect;
146046 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT0x0062);
146047 p = a[i].pExpr;
146048 /* pCol->szEst = ... // Column size est for SELECT tables never used */
146049 pCol->affinity = sqlite3ExprAffinity(p);
146050 while( pCol->affinity<=SQLITE_AFF_NONE0x40 && pS2->pNext!=0 ){
146051 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
146052 pS2 = pS2->pNext;
146053 pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
146054 }
146055 if( pCol->affinity<=SQLITE_AFF_NONE0x40 ){
146056 pCol->affinity = aff;
146057 }
146058 if( pCol->affinity>=SQLITE_AFF_TEXT0x42 && (pS2->pNext || pS2!=pSelect) ){
146059 for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
146060 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
146061 }
146062 if( pCol->affinity==SQLITE_AFF_TEXT0x42 && (m&0x01)!=0 ){
146063 pCol->affinity = SQLITE_AFF_BLOB0x41;
146064 }else
146065 if( pCol->affinity>=SQLITE_AFF_NUMERIC0x43 && (m&0x02)!=0 ){
146066 pCol->affinity = SQLITE_AFF_BLOB0x41;
146067 }
146068 if( pCol->affinity>=SQLITE_AFF_NUMERIC0x43 && p->op==TK_CAST36 ){
146069 pCol->affinity = SQLITE_AFF_FLEXNUM0x46;
146070 }
146071 }
146072 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p,0,0,0);
146073 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
146074 if( pCol->affinity==SQLITE_AFF_NUMERIC0x43
146075 || pCol->affinity==SQLITE_AFF_FLEXNUM0x46
146076 ){
146077 zType = "NUM";
146078 }else{
146079 zType = 0;
146080 for(j=1; j<SQLITE_N_STDTYPE6; j++){
146081 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
146082 zType = sqlite3StdType[j];
146083 break;
146084 }
146085 }
146086 }
146087 }
146088 if( zType ){
146089 const i64 k = sqlite3Strlen30(zType);
146090 n = sqlite3Strlen30(pCol->zCnName);
146091 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2);
146092 pCol->colFlags &= ~(COLFLAG_HASTYPE0x0004|COLFLAG_HASCOLL0x0200);
146093 if( pCol->zCnName ){
146094 memcpy(&pCol->zCnName[n+1], zType, k+1);
146095 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
146096 }
146097 }
146098 pColl = sqlite3ExprCollSeq(pParse, p);
146099 if( pColl ){
146100 assert( pTab->pIndex==0 )((void) (0));
146101 sqlite3ColumnSetColl(db, pCol, pColl->zName);
146102 }
146103 }
146104 pTab->szTabRow = 1; /* Any non-zero value works */
146105}
146106
146107/*
146108** Given a SELECT statement, generate a Table structure that describes
146109** the result set of that SELECT.
146110*/
146111SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
146112 Table *pTab;
146113 sqlite3 *db = pParse->db;
146114 u64 savedFlags;
146115
146116 savedFlags = db->flags;
146117 db->flags &= ~(u64)SQLITE_FullColNames0x00000004;
146118 db->flags |= SQLITE_ShortColNames0x00000040;
146119 sqlite3SelectPrep(pParse, pSelect, 0);
146120 db->flags = savedFlags;
146121 if( pParse->nErr ) return 0;
146122 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
146123 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
146124 if( pTab==0 ){
146125 return 0;
146126 }
146127 pTab->nTabRef = 1;
146128 pTab->zName = 0;
146129 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
146130 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
146131 sqlite3SubqueryColumnTypes(pParse, pTab, pSelect, aff);
146132 pTab->iPKey = -1;
146133 if( db->mallocFailed ){
146134 sqlite3DeleteTable(db, pTab);
146135 return 0;
146136 }
146137 return pTab;
146138}
146139
146140/*
146141** Get a VDBE for the given parser context. Create a new one if necessary.
146142** If an error occurs, return NULL and leave a message in pParse.
146143*/
146144SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse *pParse){
146145 if( pParse->pVdbe ){
146146 return pParse->pVdbe;
146147 }
146148 if( pParse->pToplevel==0
146149 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)(((pParse->db)->dbOptFlags&(0x00000008))==0)
146150 ){
146151 pParse->okConstFactor = 1;
146152 }
146153 return sqlite3VdbeCreate(pParse);
146154}
146155
146156
146157/*
146158** Compute the iLimit and iOffset fields of the SELECT based on the
146159** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
146160** that appear in the original SQL statement after the LIMIT and OFFSET
146161** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
146162** are the integer memory register numbers for counters used to compute
146163** the limit and offset. If there is no limit and/or offset, then
146164** iLimit and iOffset are negative.
146165**
146166** This routine changes the values of iLimit and iOffset only if
146167** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
146168** and iOffset should have been preset to appropriate default values (zero)
146169** prior to calling this routine.
146170**
146171** The iOffset register (if it exists) is initialized to the value
146172** of the OFFSET. The iLimit register is initialized to LIMIT. Register
146173** iOffset+1 is initialized to LIMIT+OFFSET.
146174**
146175** Only if pLimit->pLeft!=0 do the limit registers get
146176** redefined. The UNION ALL operator uses this property to force
146177** the reuse of the same limit and offset registers across multiple
146178** SELECT statements.
146179*/
146180static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
146181 Vdbe *v = 0;
146182 int iLimit = 0;
146183 int iOffset;
146184 int n;
146185 Expr *pLimit = p->pLimit;
146186
146187 if( p->iLimit ) return;
146188
146189 /*
146190 ** "LIMIT -1" always shows all rows. There is some
146191 ** controversy about what the correct behavior should be.
146192 ** The current implementation interprets "LIMIT 0" to mean
146193 ** no rows.
146194 */
146195 if( pLimit ){
146196 assert( pLimit->op==TK_LIMIT )((void) (0));
146197 assert( pLimit->pLeft!=0 )((void) (0));
146198 p->iLimit = iLimit = ++pParse->nMem;
146199 v = sqlite3GetVdbe(pParse);
146200 assert( v!=0 )((void) (0));
146201 if( sqlite3ExprIsInteger(pLimit->pLeft, &n, pParse) ){
146202 sqlite3VdbeAddOp2(v, OP_Integer71, n, iLimit);
146203 VdbeComment((v, "LIMIT counter"));
146204 if( n==0 ){
146205 sqlite3VdbeGoto(v, iBreak);
146206 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
146207 p->nSelectRow = sqlite3LogEst((u64)n);
146208 p->selFlags |= SF_FixedLimit0x0004000;
146209 }
146210 }else{
146211 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
146212 sqlite3VdbeAddOp1(v, OP_MustBeInt13, iLimit); VdbeCoverage(v);
146213 VdbeComment((v, "LIMIT counter"));
146214 sqlite3VdbeAddOp2(v, OP_IfNot17, iLimit, iBreak); VdbeCoverage(v);
146215 }
146216 if( pLimit->pRight ){
146217 p->iOffset = iOffset = ++pParse->nMem;
146218 pParse->nMem++; /* Allocate an extra register for limit+offset */
146219 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
146220 sqlite3VdbeAddOp1(v, OP_MustBeInt13, iOffset); VdbeCoverage(v);
146221 VdbeComment((v, "OFFSET counter"));
146222 sqlite3VdbeAddOp3(v, OP_OffsetLimit160, iLimit, iOffset+1, iOffset);
146223 VdbeComment((v, "LIMIT+OFFSET"));
146224 }
146225 }
146226}
146227
146228#ifndef SQLITE_OMIT_COMPOUND_SELECT
146229/*
146230** Return the appropriate collating sequence for the iCol-th column of
146231** the result set for the compound-select statement "p". Return NULL if
146232** the column has no default collating sequence.
146233**
146234** The collating sequence for the compound select is taken from the
146235** left-most term of the select that has a collating sequence.
146236*/
146237static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
146238 CollSeq *pRet;
146239 if( p->pPrior ){
146240 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
146241 }else{
146242 pRet = 0;
146243 }
146244 assert( iCol>=0 )((void) (0));
146245 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
146246 ** have been thrown during name resolution and we would not have gotten
146247 ** this far */
146248 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr)(iCol<p->pEList->nExpr) ){
146249 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
146250 }
146251 return pRet;
146252}
146253
146254/*
146255** The select statement passed as the second parameter is a compound SELECT
146256** with an ORDER BY clause. This function allocates and returns a KeyInfo
146257** structure suitable for implementing the ORDER BY.
146258**
146259** Space to hold the KeyInfo structure is obtained from malloc. The calling
146260** function is responsible for ensuring that this structure is eventually
146261** freed.
146262*/
146263static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
146264 ExprList *pOrderBy = p->pOrderBy;
146265 int nOrderBy = ALWAYS(pOrderBy!=0)(pOrderBy!=0) ? pOrderBy->nExpr : 0;
146266 sqlite3 *db = pParse->db;
146267 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
146268 if( pRet ){
146269 int i;
146270 for(i=0; i<nOrderBy; i++){
146271 struct ExprList_item *pItem = &pOrderBy->a[i];
146272 Expr *pTerm = pItem->pExpr;
146273 CollSeq *pColl;
146274
146275 if( pTerm->flags & EP_Collate0x000200 ){
146276 pColl = sqlite3ExprCollSeq(pParse, pTerm);
146277 }else{
146278 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
146279 if( pColl==0 ) pColl = db->pDfltColl;
146280 pOrderBy->a[i].pExpr =
146281 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
146282 }
146283 assert( sqlite3KeyInfoIsWriteable(pRet) )((void) (0));
146284 pRet->aColl[i] = pColl;
146285 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
146286 }
146287 }
146288
146289 return pRet;
146290}
146291
146292#ifndef SQLITE_OMIT_CTE
146293/*
146294** This routine generates VDBE code to compute the content of a WITH RECURSIVE
146295** query of the form:
146296**
146297** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
146298** \___________/ \_______________/
146299** p->pPrior p
146300**
146301**
146302** There is exactly one reference to the recursive-table in the FROM clause
146303** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
146304**
146305** The setup-query runs once to generate an initial set of rows that go
146306** into a Queue table. Rows are extracted from the Queue table one by
146307** one. Each row extracted from Queue is output to pDest. Then the single
146308** extracted row (now in the iCurrent table) becomes the content of the
146309** recursive-table for a recursive-query run. The output of the recursive-query
146310** is added back into the Queue table. Then another row is extracted from Queue
146311** and the iteration continues until the Queue table is empty.
146312**
146313** If the compound query operator is UNION then no duplicate rows are ever
146314** inserted into the Queue table. The iDistinct table keeps a copy of all rows
146315** that have ever been inserted into Queue and causes duplicates to be
146316** discarded. If the operator is UNION ALL, then duplicates are allowed.
146317**
146318** If the query has an ORDER BY, then entries in the Queue table are kept in
146319** ORDER BY order and the first entry is extracted for each cycle. Without
146320** an ORDER BY, the Queue table is just a FIFO.
146321**
146322** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
146323** have been output to pDest. A LIMIT of zero means to output no rows and a
146324** negative LIMIT means to output all rows. If there is also an OFFSET clause
146325** with a positive value, then the first OFFSET outputs are discarded rather
146326** than being sent to pDest. The LIMIT count does not begin until after OFFSET
146327** rows have been skipped.
146328*/
146329static void generateWithRecursiveQuery(
146330 Parse *pParse, /* Parsing context */
146331 Select *p, /* The recursive SELECT to be coded */
146332 SelectDest *pDest /* What to do with query results */
146333){
146334 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
146335 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
146336 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
146337 Select *pSetup; /* The setup query */
146338 Select *pFirstRec; /* Left-most recursive term */
146339 int addrTop; /* Top of the loop */
146340 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
146341 int iCurrent = 0; /* The Current table */
146342 int regCurrent; /* Register holding Current table */
146343 int iQueue; /* The Queue table */
146344 int iDistinct = 0; /* To ensure unique results if UNION */
146345 int eDest = SRT_Fifo8; /* How to write to Queue */
146346 SelectDest destQueue; /* SelectDest targeting the Queue table */
146347 int i; /* Loop counter */
146348 int rc; /* Result code */
146349 ExprList *pOrderBy; /* The ORDER BY clause */
146350 Expr *pLimit; /* Saved LIMIT and OFFSET */
146351 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
146352
146353#ifndef SQLITE_OMIT_WINDOWFUNC
146354 if( p->pWin ){
146355 sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
146356 return;
146357 }
146358#endif
146359
146360 /* Obtain authorization to do a recursive query */
146361 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE33, 0, 0, 0) ) return;
146362
146363 /* Process the LIMIT and OFFSET clauses, if they exist */
146364 addrBreak = sqlite3VdbeMakeLabel(pParse);
146365 p->nSelectRow = 320; /* 4 billion rows */
146366 computeLimitRegisters(pParse, p, addrBreak);
146367 pLimit = p->pLimit;
146368 regLimit = p->iLimit;
146369 regOffset = p->iOffset;
146370 p->pLimit = 0;
146371 p->iLimit = p->iOffset = 0;
146372 pOrderBy = p->pOrderBy;
146373
146374 /* Locate the cursor number of the Current table */
146375 for(i=0; ALWAYS(i<pSrc->nSrc)(i<pSrc->nSrc); i++){
146376 if( pSrc->a[i].fg.isRecursive ){
146377 iCurrent = pSrc->a[i].iCursor;
146378 break;
146379 }
146380 }
146381
146382 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
146383 ** the Distinct table must be exactly one greater than Queue in order
146384 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
146385 iQueue = pParse->nTab++;
146386 if( p->op==TK_UNION135 ){
146387 eDest = pOrderBy ? SRT_DistQueue6 : SRT_DistFifo5;
146388 iDistinct = pParse->nTab++;
146389 }else{
146390 eDest = pOrderBy ? SRT_Queue7 : SRT_Fifo8;
146391 }
146392 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
146393
146394 /* Allocate cursors for Current, Queue, and Distinct. */
146395 regCurrent = ++pParse->nMem;
146396 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, iCurrent, regCurrent, nCol);
146397 if( pOrderBy ){
146398 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
146399 sqlite3VdbeAddOp4(v, OP_OpenEphemeral117, iQueue, pOrderBy->nExpr+2, 0,
146400 (char*)pKeyInfo, P4_KEYINFO(-8));
146401 destQueue.pOrderBy = pOrderBy;
146402 }else{
146403 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iQueue, nCol);
146404 }
146405 VdbeComment((v, "Queue table"));
146406 if( iDistinct ){
146407 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iDistinct, 0);
146408 p->selFlags |= SF_UsesEphemeral0x0000020;
146409 }
146410
146411 /* Detach the ORDER BY clause from the compound SELECT */
146412 p->pOrderBy = 0;
146413
146414 /* Figure out how many elements of the compound SELECT are part of the
146415 ** recursive query. Make sure no recursive elements use aggregate
146416 ** functions. Mark the recursive elements as UNION ALL even if they
146417 ** are really UNION because the distinctness will be enforced by the
146418 ** iDistinct table. pFirstRec is left pointing to the left-most
146419 ** recursive term of the CTE.
146420 */
146421 for(pFirstRec=p; ALWAYS(pFirstRec!=0)(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
146422 if( pFirstRec->selFlags & SF_Aggregate0x0000008 ){
146423 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
146424 goto end_of_recursive_query;
146425 }
146426 pFirstRec->op = TK_ALL136;
146427 if( (pFirstRec->pPrior->selFlags & SF_Recursive0x0002000)==0 ) break;
146428 }
146429
146430 /* Store the results of the setup-query in Queue. */
146431 pSetup = pFirstRec->pPrior;
146432 pSetup->pNext = 0;
146433 ExplainQueryPlan((pParse, 1, "SETUP"))sqlite3VdbeExplain (pParse, 1, "SETUP");
146434 rc = sqlite3Select(pParse, pSetup, &destQueue);
146435 pSetup->pNext = p;
146436 if( rc ) goto end_of_recursive_query;
146437
146438 /* Find the next row in the Queue and output that row */
146439 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind36, iQueue, addrBreak); VdbeCoverage(v);
146440
146441 /* Transfer the next row in Queue over to Current */
146442 sqlite3VdbeAddOp1(v, OP_NullRow136, iCurrent); /* To reset column cache */
146443 if( pOrderBy ){
146444 sqlite3VdbeAddOp3(v, OP_Column94, iQueue, pOrderBy->nExpr+1, regCurrent);
146445 }else{
146446 sqlite3VdbeAddOp2(v, OP_RowData134, iQueue, regCurrent);
146447 }
146448 sqlite3VdbeAddOp1(v, OP_Delete130, iQueue);
146449
146450 /* Output the single row in Current */
146451 addrCont = sqlite3VdbeMakeLabel(pParse);
146452 codeOffset(v, regOffset, addrCont);
146453 selectInnerLoop(pParse, p, iCurrent,
146454 0, 0, pDest, addrCont, addrBreak);
146455 if( regLimit ){
146456 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, regLimit, addrBreak);
146457 VdbeCoverage(v);
146458 }
146459 sqlite3VdbeResolveLabel(v, addrCont);
146460
146461 /* Execute the recursive SELECT taking the single row in Current as
146462 ** the value for the recursive-table. Store the results in the Queue.
146463 */
146464 pFirstRec->pPrior = 0;
146465 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"))sqlite3VdbeExplain (pParse, 1, "RECURSIVE STEP");
146466 sqlite3Select(pParse, p, &destQueue);
146467 assert( pFirstRec->pPrior==0 )((void) (0));
146468 pFirstRec->pPrior = pSetup;
146469
146470 /* Keep running the loop until the Queue is empty */
146471 sqlite3VdbeGoto(v, addrTop);
146472 sqlite3VdbeResolveLabel(v, addrBreak);
146473
146474end_of_recursive_query:
146475 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
146476 p->pOrderBy = pOrderBy;
146477 p->pLimit = pLimit;
146478 return;
146479}
146480#endif /* SQLITE_OMIT_CTE */
146481
146482/* Forward references */
146483static int multiSelectOrderBy(
146484 Parse *pParse, /* Parsing context */
146485 Select *p, /* The right-most of SELECTs to be coded */
146486 SelectDest *pDest /* What to do with query results */
146487);
146488
146489/*
146490** Handle the special case of a compound-select that originates from a
146491** VALUES clause. By handling this as a special case, we avoid deep
146492** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
146493** on a VALUES clause.
146494**
146495** Because the Select object originates from a VALUES clause:
146496** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
146497** (2) All terms are UNION ALL
146498** (3) There is no ORDER BY clause
146499**
146500** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
146501** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
146502** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
146503** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
146504*/
146505static int multiSelectValues(
146506 Parse *pParse, /* Parsing context */
146507 Select *p, /* The right-most of SELECTs to be coded */
146508 SelectDest *pDest /* What to do with query results */
146509){
146510 int nRow = 1;
146511 int rc = 0;
146512 int bShowAll = p->pLimit==0;
146513 assert( p->selFlags & SF_MultiValue )((void) (0));
146514 do{
146515 assert( p->selFlags & SF_Values )((void) (0));
146516 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) )((void) (0));
146517 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr )((void) (0));
146518#ifndef SQLITE_OMIT_WINDOWFUNC
146519 if( p->pWin ) return -1;
146520#endif
146521 if( p->pPrior==0 ) break;
146522 assert( p->pPrior->pNext==p )((void) (0));
146523 p = p->pPrior;
146524 nRow += bShowAll;
146525 }while(1);
146526 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
146527 nRow==1 ? "" : "S"))sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
;
146528 while( p ){
146529 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
146530 if( !bShowAll ) break;
146531 p->nSelectRow = nRow;
146532 p = p->pNext;
146533 }
146534 return rc;
146535}
146536
146537/*
146538** Return true if the SELECT statement which is known to be the recursive
146539** part of a recursive CTE still has its anchor terms attached. If the
146540** anchor terms have already been removed, then return false.
146541*/
146542static int hasAnchor(Select *p){
146543 while( p && (p->selFlags & SF_Recursive0x0002000)!=0 ){ p = p->pPrior; }
146544 return p!=0;
146545}
146546
146547/*
146548** This routine is called to process a compound query form from
146549** two or more separate queries using UNION, UNION ALL, EXCEPT, or
146550** INTERSECT
146551**
146552** "p" points to the right-most of the two queries. the query on the
146553** left is p->pPrior. The left query could also be a compound query
146554** in which case this routine will be called recursively.
146555**
146556** The results of the total query are to be written into a destination
146557** of type eDest with parameter iParm.
146558**
146559** Example 1: Consider a three-way compound SQL statement.
146560**
146561** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
146562**
146563** This statement is parsed up as follows:
146564**
146565** SELECT c FROM t3
146566** |
146567** `-----> SELECT b FROM t2
146568** |
146569** `------> SELECT a FROM t1
146570**
146571** The arrows in the diagram above represent the Select.pPrior pointer.
146572** So if this routine is called with p equal to the t3 query, then
146573** pPrior will be the t2 query. p->op will be TK_UNION in this case.
146574**
146575** Notice that because of the way SQLite parses compound SELECTs, the
146576** individual selects always group from left to right.
146577*/
146578static int multiSelect(
146579 Parse *pParse, /* Parsing context */
146580 Select *p, /* The right-most of SELECTs to be coded */
146581 SelectDest *pDest /* What to do with query results */
146582){
146583 int rc = SQLITE_OK0; /* Success code from a subroutine */
146584 Select *pPrior; /* Another SELECT immediately to our left */
146585 Vdbe *v; /* Generate code to this VDBE */
146586 SelectDest dest; /* Alternative data destination */
146587 Select *pDelete = 0; /* Chain of simple selects to delete */
146588 sqlite3 *db; /* Database connection */
146589
146590 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
146591 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
146592 */
146593 assert( p && p->pPrior )((void) (0)); /* Calling function guarantees this much */
146594 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION )((void) (0));
146595 assert( p->selFlags & SF_Compound )((void) (0));
146596 db = pParse->db;
146597 pPrior = p->pPrior;
146598 dest = *pDest;
146599 assert( pPrior->pOrderBy==0 )((void) (0));
146600 assert( pPrior->pLimit==0 )((void) (0));
146601
146602 v = sqlite3GetVdbe(pParse);
146603 assert( v!=0 )((void) (0)); /* The VDBE already created by calling function */
146604
146605 /* Create the destination temporary table if necessary
146606 */
146607 if( dest.eDest==SRT_EphemTab12 ){
146608 assert( p->pEList )((void) (0));
146609 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, dest.iSDParm, p->pEList->nExpr);
146610 dest.eDest = SRT_Table14;
146611 }
146612
146613 /* Special handling for a compound-select that originates as a VALUES clause.
146614 */
146615 if( p->selFlags & SF_MultiValue0x0000400 ){
146616 rc = multiSelectValues(pParse, p, &dest);
146617 if( rc>=0 ) goto multi_select_end;
146618 rc = SQLITE_OK0;
146619 }
146620
146621 /* Make sure all SELECTs in the statement have the same number of elements
146622 ** in their result sets.
146623 */
146624 assert( p->pEList && pPrior->pEList )((void) (0));
146625 assert( p->pEList->nExpr==pPrior->pEList->nExpr )((void) (0));
146626
146627#ifndef SQLITE_OMIT_CTE
146628 if( (p->selFlags & SF_Recursive0x0002000)!=0 && hasAnchor(p) ){
146629 generateWithRecursiveQuery(pParse, p, &dest);
146630 }else
146631#endif
146632
146633 /* Compound SELECTs that have an ORDER BY clause are handled separately.
146634 */
146635 if( p->pOrderBy ){
146636 return multiSelectOrderBy(pParse, p, pDest);
146637 }else{
146638
146639#ifndef SQLITE_OMIT_EXPLAIN
146640 if( pPrior->pPrior==0 ){
146641 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"))sqlite3VdbeExplain (pParse, 1, "COMPOUND QUERY");
146642 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"))sqlite3VdbeExplain (pParse, 1, "LEFT-MOST SUBQUERY");
146643 }
146644#endif
146645
146646 /* Generate code for the left and right SELECT statements.
146647 */
146648 switch( p->op ){
146649 case TK_ALL136: {
146650 int addr = 0;
146651 int nLimit = 0; /* Initialize to suppress harmless compiler warning */
146652 assert( !pPrior->pLimit )((void) (0));
146653 pPrior->iLimit = p->iLimit;
146654 pPrior->iOffset = p->iOffset;
146655 pPrior->pLimit = p->pLimit;
146656 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n"));
146657 rc = sqlite3Select(pParse, pPrior, &dest);
146658 pPrior->pLimit = 0;
146659 if( rc ){
146660 goto multi_select_end;
146661 }
146662 p->pPrior = 0;
146663 p->iLimit = pPrior->iLimit;
146664 p->iOffset = pPrior->iOffset;
146665 if( p->iLimit ){
146666 addr = sqlite3VdbeAddOp1(v, OP_IfNot17, p->iLimit); VdbeCoverage(v);
146667 VdbeComment((v, "Jump ahead if LIMIT reached"));
146668 if( p->iOffset ){
146669 sqlite3VdbeAddOp3(v, OP_OffsetLimit160,
146670 p->iLimit, p->iOffset+1, p->iOffset);
146671 }
146672 }
146673 ExplainQueryPlan((pParse, 1, "UNION ALL"))sqlite3VdbeExplain (pParse, 1, "UNION ALL");
146674 TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n"));
146675 rc = sqlite3Select(pParse, p, &dest);
146676 testcase( rc!=SQLITE_OK );
146677 pDelete = p->pPrior;
146678 p->pPrior = pPrior;
146679 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
146680 if( p->pLimit
146681 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
146682 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
146683 ){
146684 p->nSelectRow = sqlite3LogEst((u64)nLimit);
146685 }
146686 if( addr ){
146687 sqlite3VdbeJumpHere(v, addr);
146688 }
146689 break;
146690 }
146691 case TK_EXCEPT137:
146692 case TK_UNION135: {
146693 int unionTab; /* Cursor number of the temp table holding result */
146694 u8 op = 0; /* One of the SRT_ operations to apply to self */
146695 int priorOp; /* The SRT_ operation to apply to prior selects */
146696 Expr *pLimit; /* Saved values of p->nLimit */
146697 int addr;
146698 SelectDest uniondest;
146699
146700 testcase( p->op==TK_EXCEPT );
146701 testcase( p->op==TK_UNION );
146702 priorOp = SRT_Union1;
146703 if( dest.eDest==priorOp ){
146704 /* We can reuse a temporary table generated by a SELECT to our
146705 ** right.
146706 */
146707 assert( p->pLimit==0 )((void) (0)); /* Not allowed on leftward elements */
146708 unionTab = dest.iSDParm;
146709 }else{
146710 /* We will need to create our own temporary table to hold the
146711 ** intermediate results.
146712 */
146713 unionTab = pParse->nTab++;
146714 assert( p->pOrderBy==0 )((void) (0));
146715 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, unionTab, 0);
146716 assert( p->addrOpenEphm[0] == -1 )((void) (0));
146717 p->addrOpenEphm[0] = addr;
146718 findRightmost(p)->selFlags |= SF_UsesEphemeral0x0000020;
146719 assert( p->pEList )((void) (0));
146720 }
146721
146722
146723 /* Code the SELECT statements to our left
146724 */
146725 assert( !pPrior->pOrderBy )((void) (0));
146726 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
146727 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n"));
146728 rc = sqlite3Select(pParse, pPrior, &uniondest);
146729 if( rc ){
146730 goto multi_select_end;
146731 }
146732
146733 /* Code the current SELECT statement
146734 */
146735 if( p->op==TK_EXCEPT137 ){
146736 op = SRT_Except2;
146737 }else{
146738 assert( p->op==TK_UNION )((void) (0));
146739 op = SRT_Union1;
146740 }
146741 p->pPrior = 0;
146742 pLimit = p->pLimit;
146743 p->pLimit = 0;
146744 uniondest.eDest = op;
146745 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
146746 sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
;
146747 TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n"));
146748 rc = sqlite3Select(pParse, p, &uniondest);
146749 testcase( rc!=SQLITE_OK );
146750 assert( p->pOrderBy==0 )((void) (0));
146751 pDelete = p->pPrior;
146752 p->pPrior = pPrior;
146753 p->pOrderBy = 0;
146754 if( p->op==TK_UNION135 ){
146755 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
146756 }
146757 sqlite3ExprDelete(db, p->pLimit);
146758 p->pLimit = pLimit;
146759 p->iLimit = 0;
146760 p->iOffset = 0;
146761
146762 /* Convert the data in the temporary table into whatever form
146763 ** it is that we currently need.
146764 */
146765 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp )((void) (0));
146766 assert( p->pEList || db->mallocFailed )((void) (0));
146767 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
146768 int iCont, iBreak, iStart;
146769 iBreak = sqlite3VdbeMakeLabel(pParse);
146770 iCont = sqlite3VdbeMakeLabel(pParse);
146771 computeLimitRegisters(pParse, p, iBreak);
146772 sqlite3VdbeAddOp2(v, OP_Rewind36, unionTab, iBreak); VdbeCoverage(v);
146773 iStart = sqlite3VdbeCurrentAddr(v);
146774 selectInnerLoop(pParse, p, unionTab,
146775 0, 0, &dest, iCont, iBreak);
146776 sqlite3VdbeResolveLabel(v, iCont);
146777 sqlite3VdbeAddOp2(v, OP_Next39, unionTab, iStart); VdbeCoverage(v);
146778 sqlite3VdbeResolveLabel(v, iBreak);
146779 sqlite3VdbeAddOp2(v, OP_Close122, unionTab, 0);
146780 }
146781 break;
146782 }
146783 default: assert( p->op==TK_INTERSECT )((void) (0)); {
146784 int tab1, tab2;
146785 int iCont, iBreak, iStart;
146786 Expr *pLimit;
146787 int addr;
146788 SelectDest intersectdest;
146789 int r1;
146790
146791 /* INTERSECT is different from the others since it requires
146792 ** two temporary tables. Hence it has its own case. Begin
146793 ** by allocating the tables we will need.
146794 */
146795 tab1 = pParse->nTab++;
146796 tab2 = pParse->nTab++;
146797 assert( p->pOrderBy==0 )((void) (0));
146798
146799 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, tab1, 0);
146800 assert( p->addrOpenEphm[0] == -1 )((void) (0));
146801 p->addrOpenEphm[0] = addr;
146802 findRightmost(p)->selFlags |= SF_UsesEphemeral0x0000020;
146803 assert( p->pEList )((void) (0));
146804
146805 /* Code the SELECTs to our left into temporary table "tab1".
146806 */
146807 sqlite3SelectDestInit(&intersectdest, SRT_Union1, tab1);
146808 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n"));
146809 rc = sqlite3Select(pParse, pPrior, &intersectdest);
146810 if( rc ){
146811 goto multi_select_end;
146812 }
146813
146814 /* Code the current SELECT into temporary table "tab2"
146815 */
146816 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, tab2, 0);
146817 assert( p->addrOpenEphm[1] == -1 )((void) (0));
146818 p->addrOpenEphm[1] = addr;
146819 p->pPrior = 0;
146820 pLimit = p->pLimit;
146821 p->pLimit = 0;
146822 intersectdest.iSDParm = tab2;
146823 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
146824 sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", sqlite3SelectOpName
(p->op))
;
146825 TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n"));
146826 rc = sqlite3Select(pParse, p, &intersectdest);
146827 testcase( rc!=SQLITE_OK );
146828 pDelete = p->pPrior;
146829 p->pPrior = pPrior;
146830 if( p->nSelectRow>pPrior->nSelectRow ){
146831 p->nSelectRow = pPrior->nSelectRow;
146832 }
146833 sqlite3ExprDelete(db, p->pLimit);
146834 p->pLimit = pLimit;
146835
146836 /* Generate code to take the intersection of the two temporary
146837 ** tables.
146838 */
146839 if( rc ) break;
146840 assert( p->pEList )((void) (0));
146841 iBreak = sqlite3VdbeMakeLabel(pParse);
146842 iCont = sqlite3VdbeMakeLabel(pParse);
146843 computeLimitRegisters(pParse, p, iBreak);
146844 sqlite3VdbeAddOp2(v, OP_Rewind36, tab1, iBreak); VdbeCoverage(v);
146845 r1 = sqlite3GetTempReg(pParse);
146846 iStart = sqlite3VdbeAddOp2(v, OP_RowData134, tab1, r1);
146847 sqlite3VdbeAddOp4Int(v, OP_NotFound28, tab2, iCont, r1, 0);
146848 VdbeCoverage(v);
146849 sqlite3ReleaseTempReg(pParse, r1);
146850 selectInnerLoop(pParse, p, tab1,
146851 0, 0, &dest, iCont, iBreak);
146852 sqlite3VdbeResolveLabel(v, iCont);
146853 sqlite3VdbeAddOp2(v, OP_Next39, tab1, iStart); VdbeCoverage(v);
146854 sqlite3VdbeResolveLabel(v, iBreak);
146855 sqlite3VdbeAddOp2(v, OP_Close122, tab2, 0);
146856 sqlite3VdbeAddOp2(v, OP_Close122, tab1, 0);
146857 break;
146858 }
146859 }
146860
146861 #ifndef SQLITE_OMIT_EXPLAIN
146862 if( p->pNext==0 ){
146863 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
146864 }
146865 #endif
146866 }
146867 if( pParse->nErr ) goto multi_select_end;
146868
146869 /* Compute collating sequences used by
146870 ** temporary tables needed to implement the compound select.
146871 ** Attach the KeyInfo structure to all temporary tables.
146872 **
146873 ** This section is run by the right-most SELECT statement only.
146874 ** SELECT statements to the left always skip this part. The right-most
146875 ** SELECT might also skip this part if it has no ORDER BY clause and
146876 ** no temp tables are required.
146877 */
146878 if( p->selFlags & SF_UsesEphemeral0x0000020 ){
146879 int i; /* Loop counter */
146880 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
146881 Select *pLoop; /* For looping through SELECT statements */
146882 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
146883 int nCol; /* Number of columns in result set */
146884
146885 assert( p->pNext==0 )((void) (0));
146886 assert( p->pEList!=0 )((void) (0));
146887 nCol = p->pEList->nExpr;
146888 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
146889 if( !pKeyInfo ){
146890 rc = SQLITE_NOMEM_BKPT7;
146891 goto multi_select_end;
146892 }
146893 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
146894 *apColl = multiSelectCollSeq(pParse, p, i);
146895 if( 0==*apColl ){
146896 *apColl = db->pDfltColl;
146897 }
146898 }
146899
146900 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
146901 for(i=0; i<2; i++){
146902 int addr = pLoop->addrOpenEphm[i];
146903 if( addr<0 ){
146904 /* If [0] is unused then [1] is also unused. So we can
146905 ** always safely abort as soon as the first unused slot is found */
146906 assert( pLoop->addrOpenEphm[1]<0 )((void) (0));
146907 break;
146908 }
146909 sqlite3VdbeChangeP2(v, addr, nCol);
146910 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
146911 P4_KEYINFO(-8));
146912 pLoop->addrOpenEphm[i] = -1;
146913 }
146914 }
146915 sqlite3KeyInfoUnref(pKeyInfo);
146916 }
146917
146918multi_select_end:
146919 pDest->iSdst = dest.iSdst;
146920 pDest->nSdst = dest.nSdst;
146921 if( pDelete ){
146922 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pDelete);
146923 }
146924 return rc;
146925}
146926#endif /* SQLITE_OMIT_COMPOUND_SELECT */
146927
146928/*
146929** Error message for when two or more terms of a compound select have different
146930** size result sets.
146931*/
146932SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
146933 if( p->selFlags & SF_Values0x0000200 ){
146934 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
146935 }else{
146936 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
146937 " do not have the same number of result columns",
146938 sqlite3SelectOpName(p->op));
146939 }
146940}
146941
146942/*
146943** Code an output subroutine for a coroutine implementation of a
146944** SELECT statement.
146945**
146946** The data to be output is contained in pIn->iSdst. There are
146947** pIn->nSdst columns to be output. pDest is where the output should
146948** be sent.
146949**
146950** regReturn is the number of the register holding the subroutine
146951** return address.
146952**
146953** If regPrev>0 then it is the first register in a vector that
146954** records the previous output. mem[regPrev] is a flag that is false
146955** if there has been no previous output. If regPrev>0 then code is
146956** generated to suppress duplicates. pKeyInfo is used for comparing
146957** keys.
146958**
146959** If the LIMIT found in p->iLimit is reached, jump immediately to
146960** iBreak.
146961*/
146962static int generateOutputSubroutine(
146963 Parse *pParse, /* Parsing context */
146964 Select *p, /* The SELECT statement */
146965 SelectDest *pIn, /* Coroutine supplying data */
146966 SelectDest *pDest, /* Where to send the data */
146967 int regReturn, /* The return address register */
146968 int regPrev, /* Previous result register. No uniqueness if 0 */
146969 KeyInfo *pKeyInfo, /* For comparing with previous entry */
146970 int iBreak /* Jump here if we hit the LIMIT */
146971){
146972 Vdbe *v = pParse->pVdbe;
146973 int iContinue;
146974 int addr;
146975
146976 addr = sqlite3VdbeCurrentAddr(v);
146977 iContinue = sqlite3VdbeMakeLabel(pParse);
146978
146979 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
146980 */
146981 if( regPrev ){
146982 int addr1, addr2;
146983 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot17, regPrev); VdbeCoverage(v);
146984 addr2 = sqlite3VdbeAddOp4(v, OP_Compare90, pIn->iSdst, regPrev+1, pIn->nSdst,
146985 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-8));
146986 sqlite3VdbeAddOp3(v, OP_Jump14, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
146987 sqlite3VdbeJumpHere(v, addr1);
146988 sqlite3VdbeAddOp3(v, OP_Copy80, pIn->iSdst, regPrev+1, pIn->nSdst-1);
146989 sqlite3VdbeAddOp2(v, OP_Integer71, 1, regPrev);
146990 }
146991 if( pParse->db->mallocFailed ) return 0;
146992
146993 /* Suppress the first OFFSET entries if there is an OFFSET clause
146994 */
146995 codeOffset(v, p->iOffset, iContinue);
146996
146997 assert( pDest->eDest!=SRT_Exists )((void) (0));
146998 assert( pDest->eDest!=SRT_Table )((void) (0));
146999 switch( pDest->eDest ){
147000 /* Store the result as data using a unique key.
147001 */
147002 case SRT_EphemTab12: {
147003 int r1 = sqlite3GetTempReg(pParse);
147004 int r2 = sqlite3GetTempReg(pParse);
147005 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pIn->iSdst, pIn->nSdst, r1);
147006 sqlite3VdbeAddOp2(v, OP_NewRowid127, pDest->iSDParm, r2);
147007 sqlite3VdbeAddOp3(v, OP_Insert128, pDest->iSDParm, r1, r2);
147008 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
147009 sqlite3ReleaseTempReg(pParse, r2);
147010 sqlite3ReleaseTempReg(pParse, r1);
147011 break;
147012 }
147013
147014#ifndef SQLITE_OMIT_SUBQUERY
147015 /* If we are creating a set for an "expr IN (SELECT ...)".
147016 */
147017 case SRT_Set11: {
147018 int r1;
147019 testcase( pIn->nSdst>1 );
147020 r1 = sqlite3GetTempReg(pParse);
147021 sqlite3VdbeAddOp4(v, OP_MakeRecord97, pIn->iSdst, pIn->nSdst,
147022 r1, pDest->zAffSdst, pIn->nSdst);
147023 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pDest->iSDParm, r1,
147024 pIn->iSdst, pIn->nSdst);
147025 if( pDest->iSDParm2>0 ){
147026 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pDest->iSDParm2, 0,
147027 pIn->iSdst, pIn->nSdst);
147028 ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER"))sqlite3VdbeExplain (pParse, 0, "CREATE BLOOM FILTER");
147029 }
147030 sqlite3ReleaseTempReg(pParse, r1);
147031 break;
147032 }
147033
147034 /* If this is a scalar select that is part of an expression, then
147035 ** store the results in the appropriate memory cell and break out
147036 ** of the scan loop. Note that the select might return multiple columns
147037 ** if it is the RHS of a row-value IN operator.
147038 */
147039 case SRT_Mem10: {
147040 testcase( pIn->nSdst>1 );
147041 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
147042 /* The LIMIT clause will jump out of the loop for us */
147043 break;
147044 }
147045#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
147046
147047 /* The results are stored in a sequence of registers
147048 ** starting at pDest->iSdst. Then the co-routine yields.
147049 */
147050 case SRT_Coroutine13: {
147051 if( pDest->iSdst==0 ){
147052 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
147053 pDest->nSdst = pIn->nSdst;
147054 }
147055 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
147056 sqlite3VdbeAddOp1(v, OP_Yield12, pDest->iSDParm);
147057 break;
147058 }
147059
147060 /* If none of the above, then the result destination must be
147061 ** SRT_Output. This routine is never called with any other
147062 ** destination other than the ones handled above or SRT_Output.
147063 **
147064 ** For SRT_Output, results are stored in a sequence of registers.
147065 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
147066 ** return the next row of result.
147067 */
147068 default: {
147069 assert( pDest->eDest==SRT_Output )((void) (0));
147070 sqlite3VdbeAddOp2(v, OP_ResultRow84, pIn->iSdst, pIn->nSdst);
147071 break;
147072 }
147073 }
147074
147075 /* Jump to the end of the loop if the LIMIT is reached.
147076 */
147077 if( p->iLimit ){
147078 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, p->iLimit, iBreak); VdbeCoverage(v);
147079 }
147080
147081 /* Generate the subroutine return
147082 */
147083 sqlite3VdbeResolveLabel(v, iContinue);
147084 sqlite3VdbeAddOp1(v, OP_Return67, regReturn);
147085
147086 return addr;
147087}
147088
147089/*
147090** Alternative compound select code generator for cases when there
147091** is an ORDER BY clause.
147092**
147093** We assume a query of the following form:
147094**
147095** <selectA> <operator> <selectB> ORDER BY <orderbylist>
147096**
147097** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
147098** is to code both <selectA> and <selectB> with the ORDER BY clause as
147099** co-routines. Then run the co-routines in parallel and merge the results
147100** into the output. In addition to the two coroutines (called selectA and
147101** selectB) there are 7 subroutines:
147102**
147103** outA: Move the output of the selectA coroutine into the output
147104** of the compound query.
147105**
147106** outB: Move the output of the selectB coroutine into the output
147107** of the compound query. (Only generated for UNION and
147108** UNION ALL. EXCEPT and INSERTSECT never output a row that
147109** appears only in B.)
147110**
147111** AltB: Called when there is data from both coroutines and A<B.
147112**
147113** AeqB: Called when there is data from both coroutines and A==B.
147114**
147115** AgtB: Called when there is data from both coroutines and A>B.
147116**
147117** EofA: Called when data is exhausted from selectA.
147118**
147119** EofB: Called when data is exhausted from selectB.
147120**
147121** The implementation of the latter five subroutines depend on which
147122** <operator> is used:
147123**
147124**
147125** UNION ALL UNION EXCEPT INTERSECT
147126** ------------- ----------------- -------------- -----------------
147127** AltB: outA, nextA outA, nextA outA, nextA nextA
147128**
147129** AeqB: outA, nextA nextA nextA outA, nextA
147130**
147131** AgtB: outB, nextB outB, nextB nextB nextB
147132**
147133** EofA: outB, nextB outB, nextB halt halt
147134**
147135** EofB: outA, nextA outA, nextA outA, nextA halt
147136**
147137** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
147138** causes an immediate jump to EofA and an EOF on B following nextB causes
147139** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
147140** following nextX causes a jump to the end of the select processing.
147141**
147142** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
147143** within the output subroutine. The regPrev register set holds the previously
147144** output value. A comparison is made against this value and the output
147145** is skipped if the next results would be the same as the previous.
147146**
147147** The implementation plan is to implement the two coroutines and seven
147148** subroutines first, then put the control logic at the bottom. Like this:
147149**
147150** goto Init
147151** coA: coroutine for left query (A)
147152** coB: coroutine for right query (B)
147153** outA: output one row of A
147154** outB: output one row of B (UNION and UNION ALL only)
147155** EofA: ...
147156** EofB: ...
147157** AltB: ...
147158** AeqB: ...
147159** AgtB: ...
147160** Init: initialize coroutine registers
147161** yield coA
147162** if eof(A) goto EofA
147163** yield coB
147164** if eof(B) goto EofB
147165** Cmpr: Compare A, B
147166** Jump AltB, AeqB, AgtB
147167** End: ...
147168**
147169** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
147170** actually called using Gosub and they do not Return. EofA and EofB loop
147171** until all data is exhausted then jump to the "end" label. AltB, AeqB,
147172** and AgtB jump to either L2 or to one of EofA or EofB.
147173*/
147174#ifndef SQLITE_OMIT_COMPOUND_SELECT
147175static int multiSelectOrderBy(
147176 Parse *pParse, /* Parsing context */
147177 Select *p, /* The right-most of SELECTs to be coded */
147178 SelectDest *pDest /* What to do with query results */
147179){
147180 int i, j; /* Loop counters */
147181 Select *pPrior; /* Another SELECT immediately to our left */
147182 Select *pSplit; /* Left-most SELECT in the right-hand group */
147183 int nSelect; /* Number of SELECT statements in the compound */
147184 Vdbe *v; /* Generate code to this VDBE */
147185 SelectDest destA; /* Destination for coroutine A */
147186 SelectDest destB; /* Destination for coroutine B */
147187 int regAddrA; /* Address register for select-A coroutine */
147188 int regAddrB; /* Address register for select-B coroutine */
147189 int addrSelectA; /* Address of the select-A coroutine */
147190 int addrSelectB; /* Address of the select-B coroutine */
147191 int regOutA; /* Address register for the output-A subroutine */
147192 int regOutB; /* Address register for the output-B subroutine */
147193 int addrOutA; /* Address of the output-A subroutine */
147194 int addrOutB = 0; /* Address of the output-B subroutine */
147195 int addrEofA; /* Address of the select-A-exhausted subroutine */
147196 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
147197 int addrEofB; /* Address of the select-B-exhausted subroutine */
147198 int addrAltB; /* Address of the A<B subroutine */
147199 int addrAeqB; /* Address of the A==B subroutine */
147200 int addrAgtB; /* Address of the A>B subroutine */
147201 int regLimitA; /* Limit register for select-A */
147202 int regLimitB; /* Limit register for select-A */
147203 int regPrev; /* A range of registers to hold previous output */
147204 int savedLimit; /* Saved value of p->iLimit */
147205 int savedOffset; /* Saved value of p->iOffset */
147206 int labelCmpr; /* Label for the start of the merge algorithm */
147207 int labelEnd; /* Label for the end of the overall SELECT stmt */
147208 int addr1; /* Jump instructions that get retargeted */
147209 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
147210 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
147211 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
147212 sqlite3 *db; /* Database connection */
147213 ExprList *pOrderBy; /* The ORDER BY clause */
147214 int nOrderBy; /* Number of terms in the ORDER BY clause */
147215 u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */
147216
147217 assert( p->pOrderBy!=0 )((void) (0));
147218 assert( pKeyDup==0 )((void) (0)); /* "Managed" code needs this. Ticket #3382. */
147219 db = pParse->db;
147220 v = pParse->pVdbe;
147221 assert( v!=0 )((void) (0)); /* Already thrown the error if VDBE alloc failed */
147222 labelEnd = sqlite3VdbeMakeLabel(pParse);
147223 labelCmpr = sqlite3VdbeMakeLabel(pParse);
147224
147225
147226 /* Patch up the ORDER BY clause
147227 */
147228 op = p->op;
147229 assert( p->pPrior->pOrderBy==0 )((void) (0));
147230 pOrderBy = p->pOrderBy;
147231 assert( pOrderBy )((void) (0));
147232 nOrderBy = pOrderBy->nExpr;
147233
147234 /* For operators other than UNION ALL we have to make sure that
147235 ** the ORDER BY clause covers every term of the result set. Add
147236 ** terms to the ORDER BY clause as necessary.
147237 */
147238 if( op!=TK_ALL136 ){
147239 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
147240 struct ExprList_item *pItem;
147241 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
147242 assert( pItem!=0 )((void) (0));
147243 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
147244 if( pItem->u.x.iOrderByCol==i ) break;
147245 }
147246 if( j==nOrderBy ){
147247 Expr *pNew = sqlite3Expr(db, TK_INTEGER156, 0);
147248 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
147249 pNew->flags |= EP_IntValue0x000800;
147250 pNew->u.iValue = i;
147251 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
147252 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
147253 }
147254 }
147255 }
147256
147257 /* Compute the comparison permutation and keyinfo that is used with
147258 ** the permutation used to determine if the next
147259 ** row of results comes from selectA or selectB. Also add explicit
147260 ** collations to the ORDER BY clause terms so that when the subqueries
147261 ** to the right and the left are evaluated, they use the correct
147262 ** collation.
147263 */
147264 aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
147265 if( aPermute ){
147266 struct ExprList_item *pItem;
147267 aPermute[0] = nOrderBy;
147268 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
147269 assert( pItem!=0 )((void) (0));
147270 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
147271 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr )((void) (0));
147272 aPermute[i] = pItem->u.x.iOrderByCol - 1;
147273 }
147274 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
147275 }else{
147276 pKeyMerge = 0;
147277 }
147278
147279 /* Allocate a range of temporary registers and the KeyInfo needed
147280 ** for the logic that removes duplicate result rows when the
147281 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
147282 */
147283 if( op==TK_ALL136 ){
147284 regPrev = 0;
147285 }else{
147286 int nExpr = p->pEList->nExpr;
147287 assert( nOrderBy>=nExpr || db->mallocFailed )((void) (0));
147288 regPrev = pParse->nMem+1;
147289 pParse->nMem += nExpr+1;
147290 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regPrev);
147291 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
147292 if( pKeyDup ){
147293 assert( sqlite3KeyInfoIsWriteable(pKeyDup) )((void) (0));
147294 for(i=0; i<nExpr; i++){
147295 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
147296 pKeyDup->aSortFlags[i] = 0;
147297 }
147298 }
147299 }
147300
147301 /* Separate the left and the right query from one another
147302 */
147303 nSelect = 1;
147304 if( (op==TK_ALL136 || op==TK_UNION135)
147305 && OptimizationEnabled(db, SQLITE_BalancedMerge)(((db)->dbOptFlags&(0x00200000))==0)
147306 ){
147307 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
147308 nSelect++;
147309 assert( pSplit->pPrior->pNext==pSplit )((void) (0));
147310 }
147311 }
147312 if( nSelect<=3 ){
147313 pSplit = p;
147314 }else{
147315 pSplit = p;
147316 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
147317 }
147318 pPrior = pSplit->pPrior;
147319 assert( pPrior!=0 )((void) (0));
147320 pSplit->pPrior = 0;
147321 pPrior->pNext = 0;
147322 assert( p->pOrderBy == pOrderBy )((void) (0));
147323 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
147324 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
147325 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
147326 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
147327
147328 /* Compute the limit registers */
147329 computeLimitRegisters(pParse, p, labelEnd);
147330 if( p->iLimit && op==TK_ALL136 ){
147331 regLimitA = ++pParse->nMem;
147332 regLimitB = ++pParse->nMem;
147333 sqlite3VdbeAddOp2(v, OP_Copy80, p->iOffset ? p->iOffset+1 : p->iLimit,
147334 regLimitA);
147335 sqlite3VdbeAddOp2(v, OP_Copy80, regLimitA, regLimitB);
147336 }else{
147337 regLimitA = regLimitB = 0;
147338 }
147339 sqlite3ExprDelete(db, p->pLimit);
147340 p->pLimit = 0;
147341
147342 regAddrA = ++pParse->nMem;
147343 regAddrB = ++pParse->nMem;
147344 regOutA = ++pParse->nMem;
147345 regOutB = ++pParse->nMem;
147346 sqlite3SelectDestInit(&destA, SRT_Coroutine13, regAddrA);
147347 sqlite3SelectDestInit(&destB, SRT_Coroutine13, regAddrB);
147348
147349 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "MERGE (%s)", sqlite3SelectOpName
(p->op))
;
147350
147351 /* Generate a coroutine to evaluate the SELECT statement to the
147352 ** left of the compound operator - the "A" select.
147353 */
147354 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
147355 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regAddrA, 0, addrSelectA);
147356 VdbeComment((v, "left SELECT"));
147357 pPrior->iLimit = regLimitA;
147358 ExplainQueryPlan((pParse, 1, "LEFT"))sqlite3VdbeExplain (pParse, 1, "LEFT");
147359 sqlite3Select(pParse, pPrior, &destA);
147360 sqlite3VdbeEndCoroutine(v, regAddrA);
147361 sqlite3VdbeJumpHere(v, addr1);
147362
147363 /* Generate a coroutine to evaluate the SELECT statement on
147364 ** the right - the "B" select
147365 */
147366 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
147367 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regAddrB, 0, addrSelectB);
147368 VdbeComment((v, "right SELECT"));
147369 savedLimit = p->iLimit;
147370 savedOffset = p->iOffset;
147371 p->iLimit = regLimitB;
147372 p->iOffset = 0;
147373 ExplainQueryPlan((pParse, 1, "RIGHT"))sqlite3VdbeExplain (pParse, 1, "RIGHT");
147374 sqlite3Select(pParse, p, &destB);
147375 p->iLimit = savedLimit;
147376 p->iOffset = savedOffset;
147377 sqlite3VdbeEndCoroutine(v, regAddrB);
147378
147379 /* Generate a subroutine that outputs the current row of the A
147380 ** select as the next output row of the compound select.
147381 */
147382 VdbeNoopComment((v, "Output routine for A"));
147383 addrOutA = generateOutputSubroutine(pParse,
147384 p, &destA, pDest, regOutA,
147385 regPrev, pKeyDup, labelEnd);
147386
147387 /* Generate a subroutine that outputs the current row of the B
147388 ** select as the next output row of the compound select.
147389 */
147390 if( op==TK_ALL136 || op==TK_UNION135 ){
147391 VdbeNoopComment((v, "Output routine for B"));
147392 addrOutB = generateOutputSubroutine(pParse,
147393 p, &destB, pDest, regOutB,
147394 regPrev, pKeyDup, labelEnd);
147395 }
147396 sqlite3KeyInfoUnref(pKeyDup);
147397
147398 /* Generate a subroutine to run when the results from select A
147399 ** are exhausted and only data in select B remains.
147400 */
147401 if( op==TK_EXCEPT137 || op==TK_INTERSECT138 ){
147402 addrEofA_noB = addrEofA = labelEnd;
147403 }else{
147404 VdbeNoopComment((v, "eof-A subroutine"));
147405 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub10, regOutB, addrOutB);
147406 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield12, regAddrB, labelEnd);
147407 VdbeCoverage(v);
147408 sqlite3VdbeGoto(v, addrEofA);
147409 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
147410 }
147411
147412 /* Generate a subroutine to run when the results from select B
147413 ** are exhausted and only data in select A remains.
147414 */
147415 if( op==TK_INTERSECT138 ){
147416 addrEofB = addrEofA;
147417 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
147418 }else{
147419 VdbeNoopComment((v, "eof-B subroutine"));
147420 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub10, regOutA, addrOutA);
147421 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, labelEnd); VdbeCoverage(v);
147422 sqlite3VdbeGoto(v, addrEofB);
147423 }
147424
147425 /* Generate code to handle the case of A<B
147426 */
147427 VdbeNoopComment((v, "A-lt-B subroutine"));
147428 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub10, regOutA, addrOutA);
147429 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, addrEofA); VdbeCoverage(v);
147430 sqlite3VdbeGoto(v, labelCmpr);
147431
147432 /* Generate code to handle the case of A==B
147433 */
147434 if( op==TK_ALL136 ){
147435 addrAeqB = addrAltB;
147436 }else if( op==TK_INTERSECT138 ){
147437 addrAeqB = addrAltB;
147438 addrAltB++;
147439 }else{
147440 VdbeNoopComment((v, "A-eq-B subroutine"));
147441 addrAeqB =
147442 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, addrEofA); VdbeCoverage(v);
147443 sqlite3VdbeGoto(v, labelCmpr);
147444 }
147445
147446 /* Generate code to handle the case of A>B
147447 */
147448 VdbeNoopComment((v, "A-gt-B subroutine"));
147449 addrAgtB = sqlite3VdbeCurrentAddr(v);
147450 if( op==TK_ALL136 || op==TK_UNION135 ){
147451 sqlite3VdbeAddOp2(v, OP_Gosub10, regOutB, addrOutB);
147452 }
147453 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrB, addrEofB); VdbeCoverage(v);
147454 sqlite3VdbeGoto(v, labelCmpr);
147455
147456 /* This code runs once to initialize everything.
147457 */
147458 sqlite3VdbeJumpHere(v, addr1);
147459 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrA, addrEofA_noB); VdbeCoverage(v);
147460 sqlite3VdbeAddOp2(v, OP_Yield12, regAddrB, addrEofB); VdbeCoverage(v);
147461
147462 /* Implement the main merge loop
147463 */
147464 sqlite3VdbeResolveLabel(v, labelCmpr);
147465 sqlite3VdbeAddOp4(v, OP_Permutation89, 0, 0, 0, (char*)aPermute, P4_INTARRAY(-14));
147466 sqlite3VdbeAddOp4(v, OP_Compare90, destA.iSdst, destB.iSdst, nOrderBy,
147467 (char*)pKeyMerge, P4_KEYINFO(-8));
147468 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE0x01);
147469 sqlite3VdbeAddOp3(v, OP_Jump14, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
147470
147471 /* Jump to the this point in order to terminate the query.
147472 */
147473 sqlite3VdbeResolveLabel(v, labelEnd);
147474
147475 /* Make arrangements to free the 2nd and subsequent arms of the compound
147476 ** after the parse has finished */
147477 if( pSplit->pPrior ){
147478 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior);
147479 }
147480 pSplit->pPrior = pPrior;
147481 pPrior->pNext = pSplit;
147482 sqlite3ExprListDelete(db, pPrior->pOrderBy);
147483 pPrior->pOrderBy = 0;
147484
147485 /*** TBD: Insert subroutine calls to close cursors on incomplete
147486 **** subqueries ****/
147487 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
147488 return pParse->nErr!=0;
147489}
147490#endif
147491
147492#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147493
147494/* An instance of the SubstContext object describes an substitution edit
147495** to be performed on a parse tree.
147496**
147497** All references to columns in table iTable are to be replaced by corresponding
147498** expressions in pEList.
147499**
147500** ## About "isOuterJoin":
147501**
147502** The isOuterJoin column indicates that the replacement will occur into a
147503** position in the parent that NULL-able due to an OUTER JOIN. Either the
147504** target slot in the parent is the right operand of a LEFT JOIN, or one of
147505** the left operands of a RIGHT JOIN. In either case, we need to potentially
147506** bypass the substituted expression with OP_IfNullRow.
147507**
147508** Suppose the original expression is an integer constant. Even though the table
147509** has the nullRow flag set, because the expression is an integer constant,
147510** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode
147511** that checks to see if the nullRow flag is set on the table. If the nullRow
147512** flag is set, then the value in the register is set to NULL and the original
147513** expression is bypassed. If the nullRow flag is not set, then the original
147514** expression runs to populate the register.
147515**
147516** Example where this is needed:
147517**
147518** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
147519** CREATE TABLE t2(x INT UNIQUE);
147520**
147521** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x;
147522**
147523** When the subquery on the right side of the LEFT JOIN is flattened, we
147524** have to add OP_IfNullRow in front of the OP_Integer that implements the
147525** "m" value of the subquery so that a NULL will be loaded instead of 59
147526** when processing a non-matched row of the left.
147527*/
147528typedef struct SubstContext {
147529 Parse *pParse; /* The parsing context */
147530 int iTable; /* Replace references to this table */
147531 int iNewTable; /* New table number */
147532 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
147533 ExprList *pEList; /* Replacement expressions */
147534 ExprList *pCList; /* Collation sequences for replacement expr */
147535} SubstContext;
147536
147537/* Forward Declarations */
147538static void substExprList(SubstContext*, ExprList*);
147539static void substSelect(SubstContext*, Select*, int);
147540
147541/*
147542** Scan through the expression pExpr. Replace every reference to
147543** a column in table number iTable with a copy of the iColumn-th
147544** entry in pEList. (But leave references to the ROWID column
147545** unchanged.)
147546**
147547** This routine is part of the flattening procedure. A subquery
147548** whose result set is defined by pEList appears as entry in the
147549** FROM clause of a SELECT such that the VDBE cursor assigned to that
147550** FORM clause entry is iTable. This routine makes the necessary
147551** changes to pExpr so that it refers directly to the source table
147552** of the subquery rather the result set of the subquery.
147553*/
147554static Expr *substExpr(
147555 SubstContext *pSubst, /* Description of the substitution */
147556 Expr *pExpr /* Expr in which substitution occurs */
147557){
147558 if( pExpr==0 ) return 0;
147559 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0)
147560 && pExpr->w.iJoin==pSubst->iTable
147561 ){
147562 testcase( ExprHasProperty(pExpr, EP_InnerON) );
147563 pExpr->w.iJoin = pSubst->iNewTable;
147564 }
147565 if( pExpr->op==TK_COLUMN168
147566 && pExpr->iTable==pSubst->iTable
147567 && !ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000020))!=0)
147568 ){
147569#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
147570 if( pExpr->iColumn<0 ){
147571 pExpr->op = TK_NULL122;
147572 }else
147573#endif
147574 {
147575 Expr *pNew;
147576 int iColumn;
147577 Expr *pCopy;
147578 Expr ifNullRow;
147579 iColumn = pExpr->iColumn;
147580 assert( iColumn>=0 )((void) (0));
147581 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr )((void) (0));
147582 assert( pExpr->pRight==0 )((void) (0));
147583 pCopy = pSubst->pEList->a[iColumn].pExpr;
147584 if( sqlite3ExprIsVector(pCopy) ){
147585 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
147586 }else{
147587 sqlite3 *db = pSubst->pParse->db;
147588 if( pSubst->isOuterJoin
147589 && (pCopy->op!=TK_COLUMN168 || pCopy->iTable!=pSubst->iNewTable)
147590 ){
147591 memset(&ifNullRow, 0, sizeof(ifNullRow));
147592 ifNullRow.op = TK_IF_NULL_ROW179;
147593 ifNullRow.pLeft = pCopy;
147594 ifNullRow.iTable = pSubst->iNewTable;
147595 ifNullRow.iColumn = -99;
147596 ifNullRow.flags = EP_IfNullRow0x040000;
147597 pCopy = &ifNullRow;
147598 }
147599 testcase( ExprHasProperty(pCopy, EP_Subquery) );
147600 pNew = sqlite3ExprDup(db, pCopy, 0);
147601 if( db->mallocFailed ){
147602 sqlite3ExprDelete(db, pNew);
147603 return pExpr;
147604 }
147605 if( pSubst->isOuterJoin ){
147606 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(0x200000);
147607 }
147608 if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) ){
147609 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
147610 pExpr->flags & (EP_OuterON0x000001|EP_InnerON0x000002));
147611 }
147612 sqlite3ExprDelete(db, pExpr);
147613 pExpr = pNew;
147614 if( pExpr->op==TK_TRUEFALSE171 ){
147615 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
147616 pExpr->op = TK_INTEGER156;
147617 ExprSetProperty(pExpr, EP_IntValue)(pExpr)->flags|=(0x000800);
147618 }
147619
147620 /* Ensure that the expression now has an implicit collation sequence,
147621 ** just as it did when it was a column of a view or sub-query. */
147622 {
147623 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
147624 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
147625 pSubst->pCList->a[iColumn].pExpr
147626 );
147627 if( pNat!=pColl || (pExpr->op!=TK_COLUMN168 && pExpr->op!=TK_COLLATE114) ){
147628 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
147629 (pColl ? pColl->zName : "BINARY")
147630 );
147631 }
147632 }
147633 ExprClearProperty(pExpr, EP_Collate)(pExpr)->flags&=~(0x000200);
147634 }
147635 }
147636 }else{
147637 if( pExpr->op==TK_IF_NULL_ROW179 && pExpr->iTable==pSubst->iTable ){
147638 pExpr->iTable = pSubst->iNewTable;
147639 }
147640 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
147641 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
147642 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
147643 substSelect(pSubst, pExpr->x.pSelect, 1);
147644 }else{
147645 substExprList(pSubst, pExpr->x.pList);
147646 }
147647#ifndef SQLITE_OMIT_WINDOWFUNC
147648 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
147649 Window *pWin = pExpr->y.pWin;
147650 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
147651 substExprList(pSubst, pWin->pPartition);
147652 substExprList(pSubst, pWin->pOrderBy);
147653 }
147654#endif
147655 }
147656 return pExpr;
147657}
147658static void substExprList(
147659 SubstContext *pSubst, /* Description of the substitution */
147660 ExprList *pList /* List to scan and in which to make substitutes */
147661){
147662 int i;
147663 if( pList==0 ) return;
147664 for(i=0; i<pList->nExpr; i++){
147665 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
147666 }
147667}
147668static void substSelect(
147669 SubstContext *pSubst, /* Description of the substitution */
147670 Select *p, /* SELECT statement in which to make substitutions */
147671 int doPrior /* Do substitutes on p->pPrior too */
147672){
147673 SrcList *pSrc;
147674 SrcItem *pItem;
147675 int i;
147676 if( !p ) return;
147677 do{
147678 substExprList(pSubst, p->pEList);
147679 substExprList(pSubst, p->pGroupBy);
147680 substExprList(pSubst, p->pOrderBy);
147681 p->pHaving = substExpr(pSubst, p->pHaving);
147682 p->pWhere = substExpr(pSubst, p->pWhere);
147683 pSrc = p->pSrc;
147684 assert( pSrc!=0 )((void) (0));
147685 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
147686 if( pItem->fg.isSubquery ){
147687 substSelect(pSubst, pItem->u4.pSubq->pSelect, 1);
147688 }
147689 if( pItem->fg.isTabFunc ){
147690 substExprList(pSubst, pItem->u1.pFuncArg);
147691 }
147692 }
147693 }while( doPrior && (p = p->pPrior)!=0 );
147694}
147695#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
147696
147697#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147698/*
147699** pSelect is a SELECT statement and pSrcItem is one item in the FROM
147700** clause of that SELECT.
147701**
147702** This routine scans the entire SELECT statement and recomputes the
147703** pSrcItem->colUsed mask.
147704*/
147705static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){
147706 SrcItem *pItem;
147707 if( pExpr->op!=TK_COLUMN168 ) return WRC_Continue0;
147708 pItem = pWalker->u.pSrcItem;
147709 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue0;
147710 if( pExpr->iColumn<0 ) return WRC_Continue0;
147711 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
147712 return WRC_Continue0;
147713}
147714static void recomputeColumnsUsed(
147715 Select *pSelect, /* The complete SELECT statement */
147716 SrcItem *pSrcItem /* Which FROM clause item to recompute */
147717){
147718 Walker w;
147719 if( NEVER(pSrcItem->pSTab==0)(pSrcItem->pSTab==0) ) return;
147720 memset(&w, 0, sizeof(w));
147721 w.xExprCallback = recomputeColumnsUsedExpr;
147722 w.xSelectCallback = sqlite3SelectWalkNoop;
147723 w.u.pSrcItem = pSrcItem;
147724 pSrcItem->colUsed = 0;
147725 sqlite3WalkSelect(&w, pSelect);
147726}
147727#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
147728
147729#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147730/*
147731** Assign new cursor numbers to each of the items in pSrc. For each
147732** new cursor number assigned, set an entry in the aCsrMap[] array
147733** to map the old cursor number to the new:
147734**
147735** aCsrMap[iOld+1] = iNew;
147736**
147737** The array is guaranteed by the caller to be large enough for all
147738** existing cursor numbers in pSrc. aCsrMap[0] is the array size.
147739**
147740** If pSrc contains any sub-selects, call this routine recursively
147741** on the FROM clause of each such sub-select, with iExcept set to -1.
147742*/
147743static void srclistRenumberCursors(
147744 Parse *pParse, /* Parse context */
147745 int *aCsrMap, /* Array to store cursor mappings in */
147746 SrcList *pSrc, /* FROM clause to renumber */
147747 int iExcept /* FROM clause item to skip */
147748){
147749 int i;
147750 SrcItem *pItem;
147751 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
147752 if( i!=iExcept ){
147753 Select *p;
147754 assert( pItem->iCursor < aCsrMap[0] )((void) (0));
147755 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
147756 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
147757 }
147758 pItem->iCursor = aCsrMap[pItem->iCursor+1];
147759 if( pItem->fg.isSubquery ){
147760 for(p=pItem->u4.pSubq->pSelect; p; p=p->pPrior){
147761 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
147762 }
147763 }
147764 }
147765 }
147766}
147767
147768/*
147769** *piCursor is a cursor number. Change it if it needs to be mapped.
147770*/
147771static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){
147772 int *aCsrMap = pWalker->u.aiCol;
147773 int iCsr = *piCursor;
147774 if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){
147775 *piCursor = aCsrMap[iCsr+1];
147776 }
147777}
147778
147779/*
147780** Expression walker callback used by renumberCursors() to update
147781** Expr objects to match newly assigned cursor numbers.
147782*/
147783static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){
147784 int op = pExpr->op;
147785 if( op==TK_COLUMN168 || op==TK_IF_NULL_ROW179 ){
147786 renumberCursorDoMapping(pWalker, &pExpr->iTable);
147787 }
147788 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
147789 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
147790 }
147791 return WRC_Continue0;
147792}
147793
147794/*
147795** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc)
147796** of the SELECT statement passed as the second argument, and to each
147797** cursor in the FROM clause of any FROM clause sub-selects, recursively.
147798** Except, do not assign a new cursor number to the iExcept'th element in
147799** the FROM clause of (*p). Update all expressions and other references
147800** to refer to the new cursor numbers.
147801**
147802** Argument aCsrMap is an array that may be used for temporary working
147803** space. Two guarantees are made by the caller:
147804**
147805** * the array is larger than the largest cursor number used within the
147806** select statement passed as an argument, and
147807**
147808** * the array entries for all cursor numbers that do *not* appear in
147809** FROM clauses of the select statement as described above are
147810** initialized to zero.
147811*/
147812static void renumberCursors(
147813 Parse *pParse, /* Parse context */
147814 Select *p, /* Select to renumber cursors within */
147815 int iExcept, /* FROM clause item to skip */
147816 int *aCsrMap /* Working space */
147817){
147818 Walker w;
147819 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
147820 memset(&w, 0, sizeof(w));
147821 w.u.aiCol = aCsrMap;
147822 w.xExprCallback = renumberCursorsCb;
147823 w.xSelectCallback = sqlite3SelectWalkNoop;
147824 sqlite3WalkSelect(&w, p);
147825}
147826#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
147827
147828/*
147829** If pSel is not part of a compound SELECT, return a pointer to its
147830** expression list. Otherwise, return a pointer to the expression list
147831** of the leftmost SELECT in the compound.
147832*/
147833static ExprList *findLeftmostExprlist(Select *pSel){
147834 while( pSel->pPrior ){
147835 pSel = pSel->pPrior;
147836 }
147837 return pSel->pEList;
147838}
147839
147840/*
147841** Return true if any of the result-set columns in the compound query
147842** have incompatible affinities on one or more arms of the compound.
147843*/
147844static int compoundHasDifferentAffinities(Select *p){
147845 int ii;
147846 ExprList *pList;
147847 assert( p!=0 )((void) (0));
147848 assert( p->pEList!=0 )((void) (0));
147849 assert( p->pPrior!=0 )((void) (0));
147850 pList = p->pEList;
147851 for(ii=0; ii<pList->nExpr; ii++){
147852 char aff;
147853 Select *pSub1;
147854 assert( pList->a[ii].pExpr!=0 )((void) (0));
147855 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
147856 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
147857 assert( pSub1->pEList!=0 )((void) (0));
147858 assert( pSub1->pEList->nExpr>ii )((void) (0));
147859 assert( pSub1->pEList->a[ii].pExpr!=0 )((void) (0));
147860 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
147861 return 1;
147862 }
147863 }
147864 }
147865 return 0;
147866}
147867
147868#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
147869/*
147870** This routine attempts to flatten subqueries as a performance optimization.
147871** This routine returns 1 if it makes changes and 0 if no flattening occurs.
147872**
147873** To understand the concept of flattening, consider the following
147874** query:
147875**
147876** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
147877**
147878** The default way of implementing this query is to execute the
147879** subquery first and store the results in a temporary table, then
147880** run the outer query on that temporary table. This requires two
147881** passes over the data. Furthermore, because the temporary table
147882** has no indices, the WHERE clause on the outer query cannot be
147883** optimized.
147884**
147885** This routine attempts to rewrite queries such as the above into
147886** a single flat select, like this:
147887**
147888** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
147889**
147890** The code generated for this simplification gives the same result
147891** but only has to scan the data once. And because indices might
147892** exist on the table t1, a complete scan of the data might be
147893** avoided.
147894**
147895** Flattening is subject to the following constraints:
147896**
147897** (**) We no longer attempt to flatten aggregate subqueries. Was:
147898** The subquery and the outer query cannot both be aggregates.
147899**
147900** (**) We no longer attempt to flatten aggregate subqueries. Was:
147901** (2) If the subquery is an aggregate then
147902** (2a) the outer query must not be a join and
147903** (2b) the outer query must not use subqueries
147904** other than the one FROM-clause subquery that is a candidate
147905** for flattening. (This is due to ticket [2f7170d73bf9abf80]
147906** from 2015-02-09.)
147907**
147908** (3) If the subquery is the right operand of a LEFT JOIN then
147909** (3a) the subquery may not be a join and
147910** (3b) the FROM clause of the subquery may not contain a virtual
147911** table and
147912** (**) Was: "The outer query may not have a GROUP BY." This case
147913** is now managed correctly
147914** (3d) the outer query may not be DISTINCT.
147915** See also (26) for restrictions on RIGHT JOIN.
147916**
147917** (4) The subquery can not be DISTINCT.
147918**
147919** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
147920** sub-queries that were excluded from this optimization. Restriction
147921** (4) has since been expanded to exclude all DISTINCT subqueries.
147922**
147923** (**) We no longer attempt to flatten aggregate subqueries. Was:
147924** If the subquery is aggregate, the outer query may not be DISTINCT.
147925**
147926** (7) The subquery must have a FROM clause. TODO: For subqueries without
147927** A FROM clause, consider adding a FROM clause with the special
147928** table sqlite_once that consists of a single row containing a
147929** single NULL.
147930**
147931** (8) If the subquery uses LIMIT then the outer query may not be a join.
147932**
147933** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
147934**
147935** (**) Restriction (10) was removed from the code on 2005-02-05 but we
147936** accidentally carried the comment forward until 2014-09-15. Original
147937** constraint: "If the subquery is aggregate then the outer query
147938** may not use LIMIT."
147939**
147940** (11) The subquery and the outer query may not both have ORDER BY clauses.
147941**
147942** (**) Not implemented. Subsumed into restriction (3). Was previously
147943** a separate restriction deriving from ticket #350.
147944**
147945** (13) The subquery and outer query may not both use LIMIT.
147946**
147947** (14) The subquery may not use OFFSET.
147948**
147949** (15) If the outer query is part of a compound select, then the
147950** subquery may not use LIMIT.
147951** (See ticket #2339 and ticket [02a8e81d44]).
147952**
147953** (16) If the outer query is aggregate, then the subquery may not
147954** use ORDER BY. (Ticket #2942) This used to not matter
147955** until we introduced the group_concat() function.
147956**
147957** (17) If the subquery is a compound select, then
147958** (17a) all compound operators must be a UNION ALL, and
147959** (17b) no terms within the subquery compound may be aggregate
147960** or DISTINCT, and
147961** (17c) every term within the subquery compound must have a FROM clause
147962** (17d) the outer query may not be
147963** (17d1) aggregate, or
147964** (17d2) DISTINCT
147965** (17e) the subquery may not contain window functions, and
147966** (17f) the subquery must not be the RHS of a LEFT JOIN.
147967** (17g) either the subquery is the first element of the outer
147968** query or there are no RIGHT or FULL JOINs in any arm
147969** of the subquery. (This is a duplicate of condition (27b).)
147970** (17h) The corresponding result set expressions in all arms of the
147971** compound must have the same affinity.
147972**
147973** The parent and sub-query may contain WHERE clauses. Subject to
147974** rules (11), (13) and (14), they may also contain ORDER BY,
147975** LIMIT and OFFSET clauses. The subquery cannot use any compound
147976** operator other than UNION ALL because all the other compound
147977** operators have an implied DISTINCT which is disallowed by
147978** restriction (4).
147979**
147980** Also, each component of the sub-query must return the same number
147981** of result columns. This is actually a requirement for any compound
147982** SELECT statement, but all the code here does is make sure that no
147983** such (illegal) sub-query is flattened. The caller will detect the
147984** syntax error and return a detailed message.
147985**
147986** (18) If the sub-query is a compound select, then all terms of the
147987** ORDER BY clause of the parent must be copies of a term returned
147988** by the parent query.
147989**
147990** (19) If the subquery uses LIMIT then the outer query may not
147991** have a WHERE clause.
147992**
147993** (20) If the sub-query is a compound select, then it must not use
147994** an ORDER BY clause. Ticket #3773. We could relax this constraint
147995** somewhat by saying that the terms of the ORDER BY clause must
147996** appear as unmodified result columns in the outer query. But we
147997** have other optimizations in mind to deal with that case.
147998**
147999** (21) If the subquery uses LIMIT then the outer query may not be
148000** DISTINCT. (See ticket [752e1646fc]).
148001**
148002** (22) The subquery may not be a recursive CTE.
148003**
148004** (23) If the outer query is a recursive CTE, then the sub-query may not be
148005** a compound query. This restriction is because transforming the
148006** parent to a compound query confuses the code that handles
148007** recursive queries in multiSelect().
148008**
148009** (**) We no longer attempt to flatten aggregate subqueries. Was:
148010** The subquery may not be an aggregate that uses the built-in min() or
148011** or max() functions. (Without this restriction, a query like:
148012** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
148013** return the value X for which Y was maximal.)
148014**
148015** (25) If either the subquery or the parent query contains a window
148016** function in the select list or ORDER BY clause, flattening
148017** is not attempted.
148018**
148019** (26) The subquery may not be the right operand of a RIGHT JOIN.
148020** See also (3) for restrictions on LEFT JOIN.
148021**
148022** (27) The subquery may not contain a FULL or RIGHT JOIN unless it
148023** is the first element of the parent query. Two subcases:
148024** (27a) the subquery is not a compound query.
148025** (27b) the subquery is a compound query and the RIGHT JOIN occurs
148026** in any arm of the compound query. (See also (17g).)
148027**
148028** (28) The subquery is not a MATERIALIZED CTE. (This is handled
148029** in the caller before ever reaching this routine.)
148030**
148031**
148032** In this routine, the "p" parameter is a pointer to the outer query.
148033** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
148034** uses aggregates.
148035**
148036** If flattening is not attempted, this routine is a no-op and returns 0.
148037** If flattening is attempted this routine returns 1.
148038**
148039** All of the expression analysis must occur on both the outer query and
148040** the subquery before this routine runs.
148041*/
148042static int flattenSubquery(
148043 Parse *pParse, /* Parsing context */
148044 Select *p, /* The parent or outer SELECT statement */
148045 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
148046 int isAgg /* True if outer SELECT uses aggregate functions */
148047){
148048 const char *zSavedAuthContext = pParse->zAuthContext;
148049 Select *pParent; /* Current UNION ALL term of the other query */
148050 Select *pSub; /* The inner query or "subquery" */
148051 Select *pSub1; /* Pointer to the rightmost select in sub-query */
148052 SrcList *pSrc; /* The FROM clause of the outer query */
148053 SrcList *pSubSrc; /* The FROM clause of the subquery */
148054 int iParent; /* VDBE cursor number of the pSub result set temp table */
148055 int iNewParent = -1;/* Replacement table for iParent */
148056 int isOuterJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
148057 int i; /* Loop counter */
148058 Expr *pWhere; /* The WHERE clause */
148059 SrcItem *pSubitem; /* The subquery */
148060 sqlite3 *db = pParse->db;
148061 Walker w; /* Walker to persist agginfo data */
148062 int *aCsrMap = 0;
148063
148064 /* Check to see if flattening is permitted. Return 0 if not.
148065 */
148066 assert( p!=0 )((void) (0));
148067 assert( p->pPrior==0 )((void) (0));
148068 if( OptimizationDisabled(db, SQLITE_QueryFlattener)(((db)->dbOptFlags&(0x00000001))!=0) ) return 0;
148069 pSrc = p->pSrc;
148070 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc )((void) (0));
148071 pSubitem = &pSrc->a[iFrom];
148072 iParent = pSubitem->iCursor;
148073 assert( pSubitem->fg.isSubquery )((void) (0));
148074 pSub = pSubitem->u4.pSubq->pSelect;
148075 assert( pSub!=0 )((void) (0));
148076
148077#ifndef SQLITE_OMIT_WINDOWFUNC
148078 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
148079#endif
148080
148081 pSubSrc = pSub->pSrc;
148082 assert( pSubSrc )((void) (0));
148083 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
148084 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
148085 ** because they could be computed at compile-time. But when LIMIT and OFFSET
148086 ** became arbitrary expressions, we were forced to add restrictions (13)
148087 ** and (14). */
148088 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
148089 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
148090 if( (p->selFlags & SF_Compound0x0000100)!=0 && pSub->pLimit ){
148091 return 0; /* Restriction (15) */
148092 }
148093 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
148094 if( pSub->selFlags & SF_Distinct0x0000001 ) return 0; /* Restriction (4) */
148095 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
148096 return 0; /* Restrictions (8)(9) */
148097 }
148098 if( p->pOrderBy && pSub->pOrderBy ){
148099 return 0; /* Restriction (11) */
148100 }
148101 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
148102 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
148103 if( pSub->pLimit && (p->selFlags & SF_Distinct0x0000001)!=0 ){
148104 return 0; /* Restriction (21) */
148105 }
148106 if( pSub->selFlags & (SF_Recursive0x0002000) ){
148107 return 0; /* Restrictions (22) */
148108 }
148109
148110 /*
148111 ** If the subquery is the right operand of a LEFT JOIN, then the
148112 ** subquery may not be a join itself (3a). Example of why this is not
148113 ** allowed:
148114 **
148115 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
148116 **
148117 ** If we flatten the above, we would get
148118 **
148119 ** (t1 LEFT OUTER JOIN t2) JOIN t3
148120 **
148121 ** which is not at all the same thing.
148122 **
148123 ** See also tickets #306, #350, and #3300.
148124 */
148125 if( (pSubitem->fg.jointype & (JT_OUTER0x20|JT_LTORJ0x40))!=0 ){
148126 if( pSubSrc->nSrc>1 /* (3a) */
148127 || IsVirtual(pSubSrc->a[0].pSTab)((pSubSrc->a[0].pSTab)->eTabType==1) /* (3b) */
148128 || (p->selFlags & SF_Distinct0x0000001)!=0 /* (3d) */
148129 || (pSubitem->fg.jointype & JT_RIGHT0x10)!=0 /* (26) */
148130 ){
148131 return 0;
148132 }
148133 isOuterJoin = 1;
148134 }
148135
148136 assert( pSubSrc->nSrc>0 )((void) (0)); /* True by restriction (7) */
148137 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
148138 return 0; /* Restriction (27a) */
148139 }
148140
148141 /* Condition (28) is blocked by the caller */
148142 assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes )((void) (0));
148143
148144 /* Restriction (17): If the sub-query is a compound SELECT, then it must
148145 ** use only the UNION ALL operator. And none of the simple select queries
148146 ** that make up the compound SELECT are allowed to be aggregate or distinct
148147 ** queries.
148148 */
148149 if( pSub->pPrior ){
148150 int ii;
148151 if( pSub->pOrderBy ){
148152 return 0; /* Restriction (20) */
148153 }
148154 if( isAgg || (p->selFlags & SF_Distinct0x0000001)!=0 || isOuterJoin>0 ){
148155 return 0; /* (17d1), (17d2), or (17f) */
148156 }
148157 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
148158 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
148159 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
148160 assert( pSub->pSrc!=0 )((void) (0));
148161 assert( (pSub->selFlags & SF_Recursive)==0 )((void) (0));
148162 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr )((void) (0));
148163 if( (pSub1->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))!=0 /* (17b) */
148164 || (pSub1->pPrior && pSub1->op!=TK_ALL136) /* (17a) */
148165 || pSub1->pSrc->nSrc<1 /* (17c) */
148166#ifndef SQLITE_OMIT_WINDOWFUNC
148167 || pSub1->pWin /* (17e) */
148168#endif
148169 ){
148170 return 0;
148171 }
148172 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
148173 /* Without this restriction, the JT_LTORJ flag would end up being
148174 ** omitted on left-hand tables of the right join that is being
148175 ** flattened. */
148176 return 0; /* Restrictions (17g), (27b) */
148177 }
148178 testcase( pSub1->pSrc->nSrc>1 );
148179 }
148180
148181 /* Restriction (18). */
148182 if( p->pOrderBy ){
148183 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
148184 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
148185 }
148186 }
148187
148188 /* Restriction (23) */
148189 if( (p->selFlags & SF_Recursive0x0002000) ) return 0;
148190
148191 /* Restriction (17h) */
148192 if( compoundHasDifferentAffinities(pSub) ) return 0;
148193
148194 if( pSrc->nSrc>1 ){
148195 if( pParse->nSelect>500 ) return 0;
148196 if( OptimizationDisabled(db, SQLITE_FlttnUnionAll)(((db)->dbOptFlags&(0x00800000))!=0) ) return 0;
148197 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
148198 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
148199 }
148200 }
148201
148202 /***** If we reach this point, flattening is permitted. *****/
148203 TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n",
148204 pSub->selId, pSub, iFrom));
148205
148206 /* Authorize the subquery */
148207 pParse->zAuthContext = pSubitem->zName;
148208 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0);
148209 testcase( i==SQLITE_DENY );
148210 pParse->zAuthContext = zSavedAuthContext;
148211
148212 /* Delete the transient structures associated with the subquery */
148213
148214 if( ALWAYS(pSubitem->fg.isSubquery)(pSubitem->fg.isSubquery) ){
148215 pSub1 = sqlite3SubqueryDetach(db, pSubitem);
148216 }else{
148217 pSub1 = 0;
148218 }
148219 assert( pSubitem->fg.isSubquery==0 )((void) (0));
148220 assert( pSubitem->fg.fixedSchema==0 )((void) (0));
148221 sqlite3DbFree(db, pSubitem->zName);
148222 sqlite3DbFree(db, pSubitem->zAlias);
148223 pSubitem->zName = 0;
148224 pSubitem->zAlias = 0;
148225 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 )((void) (0));
148226
148227 /* If the sub-query is a compound SELECT statement, then (by restrictions
148228 ** 17 and 18 above) it must be a UNION ALL and the parent query must
148229 ** be of the form:
148230 **
148231 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
148232 **
148233 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
148234 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
148235 ** OFFSET clauses and joins them to the left-hand-side of the original
148236 ** using UNION ALL operators. In this case N is the number of simple
148237 ** select statements in the compound sub-query.
148238 **
148239 ** Example:
148240 **
148241 ** SELECT a+1 FROM (
148242 ** SELECT x FROM tab
148243 ** UNION ALL
148244 ** SELECT y FROM tab
148245 ** UNION ALL
148246 ** SELECT abs(z*2) FROM tab2
148247 ** ) WHERE a!=5 ORDER BY 1
148248 **
148249 ** Transformed into:
148250 **
148251 ** SELECT x+1 FROM tab WHERE x+1!=5
148252 ** UNION ALL
148253 ** SELECT y+1 FROM tab WHERE y+1!=5
148254 ** UNION ALL
148255 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
148256 ** ORDER BY 1
148257 **
148258 ** We call this the "compound-subquery flattening".
148259 */
148260 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
148261 Select *pNew;
148262 ExprList *pOrderBy = p->pOrderBy;
148263 Expr *pLimit = p->pLimit;
148264 Select *pPrior = p->pPrior;
148265 Table *pItemTab = pSubitem->pSTab;
148266 pSubitem->pSTab = 0;
148267 p->pOrderBy = 0;
148268 p->pPrior = 0;
148269 p->pLimit = 0;
148270 pNew = sqlite3SelectDup(db, p, 0);
148271 p->pLimit = pLimit;
148272 p->pOrderBy = pOrderBy;
148273 p->op = TK_ALL136;
148274 pSubitem->pSTab = pItemTab;
148275 if( pNew==0 ){
148276 p->pPrior = pPrior;
148277 }else{
148278 pNew->selId = ++pParse->nSelect;
148279 if( aCsrMap && ALWAYS(db->mallocFailed==0)(db->mallocFailed==0) ){
148280 renumberCursors(pParse, pNew, iFrom, aCsrMap);
148281 }
148282 pNew->pPrior = pPrior;
148283 if( pPrior ) pPrior->pNext = pNew;
148284 pNew->pNext = p;
148285 p->pPrior = pNew;
148286 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
148287 " creates %u as peer\n",pNew->selId));
148288 }
148289 assert( pSubitem->fg.isSubquery==0 )((void) (0));
148290 }
148291 sqlite3DbFree(db, aCsrMap);
148292 if( db->mallocFailed ){
148293 assert( pSubitem->fg.fixedSchema==0 )((void) (0));
148294 assert( pSubitem->fg.isSubquery==0 )((void) (0));
148295 assert( pSubitem->u4.zDatabase==0 )((void) (0));
148296 sqlite3SrcItemAttachSubquery(pParse, pSubitem, pSub1, 0);
148297 return 1;
148298 }
148299
148300 /* Defer deleting the Table object associated with the
148301 ** subquery until code generation is
148302 ** complete, since there may still exist Expr.pTab entries that
148303 ** refer to the subquery even after flattening. Ticket #3346.
148304 **
148305 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
148306 */
148307 if( ALWAYS(pSubitem->pSTab!=0)(pSubitem->pSTab!=0) ){
148308 Table *pTabToDel = pSubitem->pSTab;
148309 if( pTabToDel->nTabRef==1 ){
148310 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
148311 sqlite3ParserAddCleanup(pToplevel, sqlite3DeleteTableGeneric, pTabToDel);
148312 testcase( pToplevel->earlyCleanup );
148313 }else{
148314 pTabToDel->nTabRef--;
148315 }
148316 pSubitem->pSTab = 0;
148317 }
148318
148319 /* The following loop runs once for each term in a compound-subquery
148320 ** flattening (as described above). If we are doing a different kind
148321 ** of flattening - a flattening other than a compound-subquery flattening -
148322 ** then this loop only runs once.
148323 **
148324 ** This loop moves all of the FROM elements of the subquery into the
148325 ** the FROM clause of the outer query. Before doing this, remember
148326 ** the cursor number for the original outer query FROM element in
148327 ** iParent. The iParent cursor will never be used. Subsequent code
148328 ** will scan expressions looking for iParent references and replace
148329 ** those references with expressions that resolve to the subquery FROM
148330 ** elements we are now copying in.
148331 */
148332 pSub = pSub1;
148333 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
148334 int nSubSrc;
148335 u8 jointype = 0;
148336 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ0x40;
148337 assert( pSub!=0 )((void) (0));
148338 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
148339 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
148340 pSrc = pParent->pSrc; /* FROM clause of the outer query */
148341
148342 if( pParent==p ){
148343 jointype = pSubitem->fg.jointype; /* First time through the loop */
148344 }
148345
148346 /* The subquery uses a single slot of the FROM clause of the outer
148347 ** query. If the subquery has more than one element in its FROM clause,
148348 ** then expand the outer query to make space for it to hold all elements
148349 ** of the subquery.
148350 **
148351 ** Example:
148352 **
148353 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
148354 **
148355 ** The outer query has 3 slots in its FROM clause. One slot of the
148356 ** outer query (the middle slot) is used by the subquery. The next
148357 ** block of code will expand the outer query FROM clause to 4 slots.
148358 ** The middle slot is expanded to two slots in order to make space
148359 ** for the two elements in the FROM clause of the subquery.
148360 */
148361 if( nSubSrc>1 ){
148362 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
148363 if( pSrc==0 ) break;
148364 pParent->pSrc = pSrc;
148365 }
148366
148367 /* Transfer the FROM clause terms from the subquery into the
148368 ** outer query.
148369 */
148370 for(i=0; i<nSubSrc; i++){
148371 SrcItem *pItem = &pSrc->a[i+iFrom];
148372 assert( pItem->fg.isTabFunc==0 )((void) (0));
148373 assert( pItem->fg.isSubquery((void) (0))
148374 || pItem->fg.fixedSchema((void) (0))
148375 || pItem->u4.zDatabase==0 )((void) (0));
148376 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
148377 *pItem = pSubSrc->a[i];
148378 pItem->fg.jointype |= ltorj;
148379 iNewParent = pSubSrc->a[i].iCursor;
148380 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
148381 }
148382 pSrc->a[iFrom].fg.jointype &= JT_LTORJ0x40;
148383 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
148384
148385 /* Now begin substituting subquery result set expressions for
148386 ** references to the iParent in the outer query.
148387 **
148388 ** Example:
148389 **
148390 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
148391 ** \ \_____________ subquery __________/ /
148392 ** \_____________________ outer query ______________________________/
148393 **
148394 ** We look at every expression in the outer query and every place we see
148395 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
148396 */
148397 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy0x0400000)==0 ){
148398 /* At this point, any non-zero iOrderByCol values indicate that the
148399 ** ORDER BY column expression is identical to the iOrderByCol'th
148400 ** expression returned by SELECT statement pSub. Since these values
148401 ** do not necessarily correspond to columns in SELECT statement pParent,
148402 ** zero them before transferring the ORDER BY clause.
148403 **
148404 ** Not doing this may cause an error if a subsequent call to this
148405 ** function attempts to flatten a compound sub-query into pParent
148406 ** (the only way this can happen is if the compound sub-query is
148407 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
148408 ExprList *pOrderBy = pSub->pOrderBy;
148409 for(i=0; i<pOrderBy->nExpr; i++){
148410 pOrderBy->a[i].u.x.iOrderByCol = 0;
148411 }
148412 assert( pParent->pOrderBy==0 )((void) (0));
148413 pParent->pOrderBy = pOrderBy;
148414 pSub->pOrderBy = 0;
148415 }
148416 pWhere = pSub->pWhere;
148417 pSub->pWhere = 0;
148418 if( isOuterJoin>0 ){
148419 sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON0x000001);
148420 }
148421 if( pWhere ){
148422 if( pParent->pWhere ){
148423 pParent->pWhere = sqlite3PExpr(pParse, TK_AND44, pWhere, pParent->pWhere);
148424 }else{
148425 pParent->pWhere = pWhere;
148426 }
148427 }
148428 if( db->mallocFailed==0 ){
148429 SubstContext x;
148430 x.pParse = pParse;
148431 x.iTable = iParent;
148432 x.iNewTable = iNewParent;
148433 x.isOuterJoin = isOuterJoin;
148434 x.pEList = pSub->pEList;
148435 x.pCList = findLeftmostExprlist(pSub);
148436 substSelect(&x, pParent, 0);
148437 }
148438
148439 /* The flattened query is a compound if either the inner or the
148440 ** outer query is a compound. */
148441 pParent->selFlags |= pSub->selFlags & SF_Compound0x0000100;
148442 assert( (pSub->selFlags & SF_Distinct)==0 )((void) (0)); /* restriction (17b) */
148443
148444 /*
148445 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
148446 **
148447 ** One is tempted to try to add a and b to combine the limits. But this
148448 ** does not work if either limit is negative.
148449 */
148450 if( pSub->pLimit ){
148451 pParent->pLimit = pSub->pLimit;
148452 pSub->pLimit = 0;
148453 }
148454
148455 /* Recompute the SrcItem.colUsed masks for the flattened
148456 ** tables. */
148457 for(i=0; i<nSubSrc; i++){
148458 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
148459 }
148460 }
148461
148462 /* Finally, delete what is left of the subquery and return success.
148463 */
148464 sqlite3AggInfoPersistWalkerInit(&w, pParse);
148465 sqlite3WalkSelect(&w,pSub1);
148466 sqlite3SelectDelete(db, pSub1);
148467
148468#if TREETRACE_ENABLED0
148469 if( sqlite3TreeTrace & 0x4 ){
148470 TREETRACE(0x4,pParse,p,("After flattening:\n"));
148471 sqlite3TreeViewSelect(0, p, 0);
148472 }
148473#endif
148474
148475 return 1;
148476}
148477#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
148478
148479/*
148480** A structure to keep track of all of the column values that are fixed to
148481** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
148482*/
148483typedef struct WhereConst WhereConst;
148484struct WhereConst {
148485 Parse *pParse; /* Parsing context */
148486 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
148487 int nConst; /* Number for COLUMN=CONSTANT terms */
148488 int nChng; /* Number of times a constant is propagated */
148489 int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */
148490 u32 mExcludeOn; /* Which ON expressions to exclude from considertion.
148491 ** Either EP_OuterON or EP_InnerON|EP_OuterON */
148492 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
148493};
148494
148495/*
148496** Add a new entry to the pConst object. Except, do not add duplicate
148497** pColumn entries. Also, do not add if doing so would not be appropriate.
148498**
148499** The caller guarantees the pColumn is a column and pValue is a constant.
148500** This routine has to do some additional checks before completing the
148501** insert.
148502*/
148503static void constInsert(
148504 WhereConst *pConst, /* The WhereConst into which we are inserting */
148505 Expr *pColumn, /* The COLUMN part of the constraint */
148506 Expr *pValue, /* The VALUE part of the constraint */
148507 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
148508){
148509 int i;
148510 assert( pColumn->op==TK_COLUMN )((void) (0));
148511 assert( sqlite3ExprIsConstant(pConst->pParse, pValue) )((void) (0));
148512
148513 if( ExprHasProperty(pColumn, EP_FixedCol)(((pColumn)->flags&(0x000020))!=0) ) return;
148514 if( sqlite3ExprAffinity(pValue)!=0 ) return;
148515 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
148516 return;
148517 }
148518
148519 /* 2018-10-25 ticket [cf5ed20f]
148520 ** Make sure the same pColumn is not inserted more than once */
148521 for(i=0; i<pConst->nConst; i++){
148522 const Expr *pE2 = pConst->apExpr[i*2];
148523 assert( pE2->op==TK_COLUMN )((void) (0));
148524 if( pE2->iTable==pColumn->iTable
148525 && pE2->iColumn==pColumn->iColumn
148526 ){
148527 return; /* Already present. Return without doing anything. */
148528 }
148529 }
148530 if( sqlite3ExprAffinity(pColumn)==SQLITE_AFF_BLOB0x41 ){
148531 pConst->bHasAffBlob = 1;
148532 }
148533
148534 pConst->nConst++;
148535 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
148536 pConst->nConst*2*sizeof(Expr*));
148537 if( pConst->apExpr==0 ){
148538 pConst->nConst = 0;
148539 }else{
148540 pConst->apExpr[pConst->nConst*2-2] = pColumn;
148541 pConst->apExpr[pConst->nConst*2-1] = pValue;
148542 }
148543}
148544
148545/*
148546** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
148547** is a constant expression and where the term must be true because it
148548** is part of the AND-connected terms of the expression. For each term
148549** found, add it to the pConst structure.
148550*/
148551static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
148552 Expr *pRight, *pLeft;
148553 if( NEVER(pExpr==0)(pExpr==0) ) return;
148554 if( ExprHasProperty(pExpr, pConst->mExcludeOn)(((pExpr)->flags&(pConst->mExcludeOn))!=0) ){
148555 testcase( ExprHasProperty(pExpr, EP_OuterON) );
148556 testcase( ExprHasProperty(pExpr, EP_InnerON) );
148557 return;
148558 }
148559 if( pExpr->op==TK_AND44 ){
148560 findConstInWhere(pConst, pExpr->pRight);
148561 findConstInWhere(pConst, pExpr->pLeft);
148562 return;
148563 }
148564 if( pExpr->op!=TK_EQ54 ) return;
148565 pRight = pExpr->pRight;
148566 pLeft = pExpr->pLeft;
148567 assert( pRight!=0 )((void) (0));
148568 assert( pLeft!=0 )((void) (0));
148569 if( pRight->op==TK_COLUMN168 && sqlite3ExprIsConstant(pConst->pParse, pLeft) ){
148570 constInsert(pConst,pRight,pLeft,pExpr);
148571 }
148572 if( pLeft->op==TK_COLUMN168 && sqlite3ExprIsConstant(pConst->pParse, pRight) ){
148573 constInsert(pConst,pLeft,pRight,pExpr);
148574 }
148575}
148576
148577/*
148578** This is a helper function for Walker callback propagateConstantExprRewrite().
148579**
148580** Argument pExpr is a candidate expression to be replaced by a value. If
148581** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
148582** then overwrite it with the corresponding value. Except, do not do so
148583** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
148584** is SQLITE_AFF_BLOB.
148585*/
148586static int propagateConstantExprRewriteOne(
148587 WhereConst *pConst,
148588 Expr *pExpr,
148589 int bIgnoreAffBlob
148590){
148591 int i;
148592 if( pConst->pOomFault[0] ) return WRC_Prune1;
148593 if( pExpr->op!=TK_COLUMN168 ) return WRC_Continue0;
148594 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn)(((pExpr)->flags&(0x000020|pConst->mExcludeOn))!=0) ){
148595 testcase( ExprHasProperty(pExpr, EP_FixedCol) );
148596 testcase( ExprHasProperty(pExpr, EP_OuterON) );
148597 testcase( ExprHasProperty(pExpr, EP_InnerON) );
148598 return WRC_Continue0;
148599 }
148600 for(i=0; i<pConst->nConst; i++){
148601 Expr *pColumn = pConst->apExpr[i*2];
148602 if( pColumn==pExpr ) continue;
148603 if( pColumn->iTable!=pExpr->iTable ) continue;
148604 if( pColumn->iColumn!=pExpr->iColumn ) continue;
148605 if( bIgnoreAffBlob && sqlite3ExprAffinity(pColumn)==SQLITE_AFF_BLOB0x41 ){
148606 break;
148607 }
148608 /* A match is found. Add the EP_FixedCol property */
148609 pConst->nChng++;
148610 ExprClearProperty(pExpr, EP_Leaf)(pExpr)->flags&=~(0x800000);
148611 ExprSetProperty(pExpr, EP_FixedCol)(pExpr)->flags|=(0x000020);
148612 assert( pExpr->pLeft==0 )((void) (0));
148613 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
148614 if( pConst->pParse->db->mallocFailed ) return WRC_Prune1;
148615 break;
148616 }
148617 return WRC_Prune1;
148618}
148619
148620/*
148621** This is a Walker expression callback. pExpr is a node from the WHERE
148622** clause of a SELECT statement. This function examines pExpr to see if
148623** any substitutions based on the contents of pWalker->u.pConst should
148624** be made to pExpr or its immediate children.
148625**
148626** A substitution is made if:
148627**
148628** + pExpr is a column with an affinity other than BLOB that matches
148629** one of the columns in pWalker->u.pConst, or
148630**
148631** + pExpr is a binary comparison operator (=, <=, >=, <, >) that
148632** uses an affinity other than TEXT and one of its immediate
148633** children is a column that matches one of the columns in
148634** pWalker->u.pConst.
148635*/
148636static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
148637 WhereConst *pConst = pWalker->u.pConst;
148638 assert( TK_GT==TK_EQ+1 )((void) (0));
148639 assert( TK_LE==TK_EQ+2 )((void) (0));
148640 assert( TK_LT==TK_EQ+3 )((void) (0));
148641 assert( TK_GE==TK_EQ+4 )((void) (0));
148642 if( pConst->bHasAffBlob ){
148643 if( (pExpr->op>=TK_EQ54 && pExpr->op<=TK_GE58)
148644 || pExpr->op==TK_IS45
148645 ){
148646 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
148647 if( pConst->pOomFault[0] ) return WRC_Prune1;
148648 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT0x42 ){
148649 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
148650 }
148651 }
148652 }
148653 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
148654}
148655
148656/*
148657** The WHERE-clause constant propagation optimization.
148658**
148659** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
148660** CONSTANT=COLUMN that are top-level AND-connected terms that are not
148661** part of a ON clause from a LEFT JOIN, then throughout the query
148662** replace all other occurrences of COLUMN with CONSTANT.
148663**
148664** For example, the query:
148665**
148666** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
148667**
148668** Is transformed into
148669**
148670** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
148671**
148672** Return true if any transformations where made and false if not.
148673**
148674** Implementation note: Constant propagation is tricky due to affinity
148675** and collating sequence interactions. Consider this example:
148676**
148677** CREATE TABLE t1(a INT,b TEXT);
148678** INSERT INTO t1 VALUES(123,'0123');
148679** SELECT * FROM t1 WHERE a=123 AND b=a;
148680** SELECT * FROM t1 WHERE a=123 AND b=123;
148681**
148682** The two SELECT statements above should return different answers. b=a
148683** is always true because the comparison uses numeric affinity, but b=123
148684** is false because it uses text affinity and '0123' is not the same as '123'.
148685** To work around this, the expression tree is not actually changed from
148686** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
148687** and the "123" value is hung off of the pLeft pointer. Code generator
148688** routines know to generate the constant "123" instead of looking up the
148689** column value. Also, to avoid collation problems, this optimization is
148690** only attempted if the "a=123" term uses the default BINARY collation.
148691**
148692** 2021-05-25 forum post 6a06202608: Another troublesome case is...
148693**
148694** CREATE TABLE t1(x);
148695** INSERT INTO t1 VALUES(10.0);
148696** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
148697**
148698** The query should return no rows, because the t1.x value is '10.0' not '10'
148699** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE
148700** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
148701** resulting in a false positive. To avoid this, constant propagation for
148702** columns with BLOB affinity is only allowed if the constant is used with
148703** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
148704** type conversions to occur. See logic associated with the bHasAffBlob flag
148705** for details.
148706*/
148707static int propagateConstants(
148708 Parse *pParse, /* The parsing context */
148709 Select *p /* The query in which to propagate constants */
148710){
148711 WhereConst x;
148712 Walker w;
148713 int nChng = 0;
148714 x.pParse = pParse;
148715 x.pOomFault = &pParse->db->mallocFailed;
148716 do{
148717 x.nConst = 0;
148718 x.nChng = 0;
148719 x.apExpr = 0;
148720 x.bHasAffBlob = 0;
148721 if( ALWAYS(p->pSrc!=0)(p->pSrc!=0)
148722 && p->pSrc->nSrc>0
148723 && (p->pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0
148724 ){
148725 /* Do not propagate constants on any ON clause if there is a
148726 ** RIGHT JOIN anywhere in the query */
148727 x.mExcludeOn = EP_InnerON0x000002 | EP_OuterON0x000001;
148728 }else{
148729 /* Do not propagate constants through the ON clause of a LEFT JOIN */
148730 x.mExcludeOn = EP_OuterON0x000001;
148731 }
148732 findConstInWhere(&x, p->pWhere);
148733 if( x.nConst ){
148734 memset(&w, 0, sizeof(w));
148735 w.pParse = pParse;
148736 w.xExprCallback = propagateConstantExprRewrite;
148737 w.xSelectCallback = sqlite3SelectWalkNoop;
148738 w.xSelectCallback2 = 0;
148739 w.walkerDepth = 0;
148740 w.u.pConst = &x;
148741 sqlite3WalkExpr(&w, p->pWhere);
148742 sqlite3DbFree(x.pParse->db, x.apExpr);
148743 nChng += x.nChng;
148744 }
148745 }while( x.nChng );
148746 return nChng;
148747}
148748
148749#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
148750# if !defined(SQLITE_OMIT_WINDOWFUNC)
148751/*
148752** This function is called to determine whether or not it is safe to
148753** push WHERE clause expression pExpr down to FROM clause sub-query
148754** pSubq, which contains at least one window function. Return 1
148755** if it is safe and the expression should be pushed down, or 0
148756** otherwise.
148757**
148758** It is only safe to push the expression down if it consists only
148759** of constants and copies of expressions that appear in the PARTITION
148760** BY clause of all window function used by the sub-query. It is safe
148761** to filter out entire partitions, but not rows within partitions, as
148762** this may change the results of the window functions.
148763**
148764** At the time this function is called it is guaranteed that
148765**
148766** * the sub-query uses only one distinct window frame, and
148767** * that the window frame has a PARTITION BY clause.
148768*/
148769static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){
148770 assert( pSubq->pWin->pPartition )((void) (0));
148771 assert( (pSubq->selFlags & SF_MultiPart)==0 )((void) (0));
148772 assert( pSubq->pPrior==0 )((void) (0));
148773 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
148774}
148775# endif /* SQLITE_OMIT_WINDOWFUNC */
148776#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
148777
148778#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
148779/*
148780** Make copies of relevant WHERE clause terms of the outer query into
148781** the WHERE clause of subquery. Example:
148782**
148783** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
148784**
148785** Transformed into:
148786**
148787** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
148788** WHERE x=5 AND y=10;
148789**
148790** The hope is that the terms added to the inner query will make it more
148791** efficient.
148792**
148793** NAME AMBIGUITY
148794**
148795** This optimization is called the "WHERE-clause push-down optimization"
148796** or sometimes the "predicate push-down optimization".
148797**
148798** Do not confuse this optimization with another unrelated optimization
148799** with a similar name: The "MySQL push-down optimization" causes WHERE
148800** clause terms that can be evaluated using only the index and without
148801** reference to the table are run first, so that if they are false,
148802** unnecessary table seeks are avoided.
148803**
148804** RULES
148805**
148806** Do not attempt this optimization if:
148807**
148808** (1) (** This restriction was removed on 2017-09-29. We used to
148809** disallow this optimization for aggregate subqueries, but now
148810** it is allowed by putting the extra terms on the HAVING clause.
148811** The added HAVING clause is pointless if the subquery lacks
148812** a GROUP BY clause. But such a HAVING clause is also harmless
148813** so there does not appear to be any reason to add extra logic
148814** to suppress it. **)
148815**
148816** (2) The inner query is the recursive part of a common table expression.
148817**
148818** (3) The inner query has a LIMIT clause (since the changes to the WHERE
148819** clause would change the meaning of the LIMIT).
148820**
148821** (4) The inner query is the right operand of a LEFT JOIN and the
148822** expression to be pushed down does not come from the ON clause
148823** on that LEFT JOIN.
148824**
148825** (5) The WHERE clause expression originates in the ON or USING clause
148826** of a LEFT JOIN where iCursor is not the right-hand table of that
148827** left join. An example:
148828**
148829** SELECT *
148830** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
148831** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
148832** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
148833**
148834** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
148835** But if the (b2=2) term were to be pushed down into the bb subquery,
148836** then the (1,1,NULL) row would be suppressed.
148837**
148838** (6) Window functions make things tricky as changes to the WHERE clause
148839** of the inner query could change the window over which window
148840** functions are calculated. Therefore, do not attempt the optimization
148841** if:
148842**
148843** (6a) The inner query uses multiple incompatible window partitions.
148844**
148845** (6b) The inner query is a compound and uses window-functions.
148846**
148847** (6c) The WHERE clause does not consist entirely of constants and
148848** copies of expressions found in the PARTITION BY clause of
148849** all window-functions used by the sub-query. It is safe to
148850** filter out entire partitions, as this does not change the
148851** window over which any window-function is calculated.
148852**
148853** (7) The inner query is a Common Table Expression (CTE) that should
148854** be materialized. (This restriction is implemented in the calling
148855** routine.)
148856**
148857** (8) If the subquery is a compound that uses UNION, INTERSECT,
148858** or EXCEPT, then all of the result set columns for all arms of
148859** the compound must use the BINARY collating sequence.
148860**
148861** (9) All three of the following are true:
148862**
148863** (9a) The WHERE clause expression originates in the ON or USING clause
148864** of a join (either an INNER or an OUTER join), and
148865**
148866** (9b) The subquery is to the right of the ON/USING clause
148867**
148868** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING
148869** clause and the subquery.
148870**
148871** Without this restriction, the WHERE-clause push-down optimization
148872** might move the ON/USING filter expression from the left side of a
148873** RIGHT JOIN over to the right side, which leads to incorrect answers.
148874** See also restriction (6) in sqlite3ExprIsSingleTableConstraint().
148875**
148876** (10) The inner query is not the right-hand table of a RIGHT JOIN.
148877**
148878** (11) The subquery is not a VALUES clause
148879**
148880** (12) The WHERE clause is not "rowid ISNULL" or the equivalent. This
148881** case only comes up if SQLite is compiled using
148882** SQLITE_ALLOW_ROWID_IN_VIEW.
148883**
148884** Return 0 if no changes are made and non-zero if one or more WHERE clause
148885** terms are duplicated into the subquery.
148886*/
148887static int pushDownWhereTerms(
148888 Parse *pParse, /* Parse context (for malloc() and error reporting) */
148889 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
148890 Expr *pWhere, /* The WHERE clause of the outer query */
148891 SrcList *pSrcList, /* The complete from clause of the outer query */
148892 int iSrc /* Which FROM clause term to try to push into */
148893){
148894 Expr *pNew;
148895 SrcItem *pSrc; /* The subquery FROM term into which WHERE is pushed */
148896 int nChng = 0;
148897 pSrc = &pSrcList->a[iSrc];
148898 if( pWhere==0 ) return 0;
148899 if( pSubq->selFlags & (SF_Recursive0x0002000|SF_MultiPart0x2000000) ){
148900 return 0; /* restrictions (2) and (11) */
148901 }
148902 if( pSrc->fg.jointype & (JT_LTORJ0x40|JT_RIGHT0x10) ){
148903 return 0; /* restrictions (10) */
148904 }
148905
148906 if( pSubq->pPrior ){
148907 Select *pSel;
148908 int notUnionAll = 0;
148909 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
148910 u8 op = pSel->op;
148911 assert( op==TK_ALL || op==TK_SELECT((void) (0))
148912 || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT )((void) (0));
148913 if( op!=TK_ALL136 && op!=TK_SELECT139 ){
148914 notUnionAll = 1;
148915 }
148916#ifndef SQLITE_OMIT_WINDOWFUNC
148917 if( pSel->pWin ) return 0; /* restriction (6b) */
148918#endif
148919 }
148920 if( notUnionAll ){
148921 /* If any of the compound arms are connected using UNION, INTERSECT,
148922 ** or EXCEPT, then we must ensure that none of the columns use a
148923 ** non-BINARY collating sequence. */
148924 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
148925 int ii;
148926 const ExprList *pList = pSel->pEList;
148927 assert( pList!=0 )((void) (0));
148928 for(ii=0; ii<pList->nExpr; ii++){
148929 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
148930 if( !sqlite3IsBinary(pColl) ){
148931 return 0; /* Restriction (8) */
148932 }
148933 }
148934 }
148935 }
148936 }else{
148937#ifndef SQLITE_OMIT_WINDOWFUNC
148938 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
148939#endif
148940 }
148941
148942#ifdef SQLITE_DEBUG
148943 /* Only the first term of a compound can have a WITH clause. But make
148944 ** sure no other terms are marked SF_Recursive in case something changes
148945 ** in the future.
148946 */
148947 {
148948 Select *pX;
148949 for(pX=pSubq; pX; pX=pX->pPrior){
148950 assert( (pX->selFlags & (SF_Recursive))==0 )((void) (0));
148951 }
148952 }
148953#endif
148954
148955 if( pSubq->pLimit!=0 ){
148956 return 0; /* restriction (3) */
148957 }
148958 while( pWhere->op==TK_AND44 ){
148959 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc);
148960 pWhere = pWhere->pLeft;
148961 }
148962
148963#if 0 /* These checks now done by sqlite3ExprIsSingleTableConstraint() */
148964 if( ExprHasProperty(pWhere, EP_OuterON|EP_InnerON)(((pWhere)->flags&(0x000001|0x000002))!=0) /* (9a) */
148965 && (pSrcList->a[0].fg.jointype & JT_LTORJ0x40)!=0 /* Fast pre-test of (9c) */
148966 ){
148967 int jj;
148968 for(jj=0; jj<iSrc; jj++){
148969 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
148970 /* If we reach this point, both (9a) and (9b) are satisfied.
148971 ** The following loop checks (9c):
148972 */
148973 for(jj++; jj<iSrc; jj++){
148974 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT0x10)!=0 ){
148975 return 0; /* restriction (9) */
148976 }
148977 }
148978 }
148979 }
148980 }
148981 if( isLeftJoin
148982 && (ExprHasProperty(pWhere,EP_OuterON)(((pWhere)->flags&(0x000001))!=0)==0
148983 || pWhere->w.iJoin!=iCursor)
148984 ){
148985 return 0; /* restriction (4) */
148986 }
148987 if( ExprHasProperty(pWhere,EP_OuterON)(((pWhere)->flags&(0x000001))!=0)
148988 && pWhere->w.iJoin!=iCursor
148989 ){
148990 return 0; /* restriction (5) */
148991 }
148992#endif
148993
148994#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
148995 if( ViewCanHaveRowid0 && (pWhere->op==TK_ISNULL51 || pWhere->op==TK_NOTNULL52) ){
148996 Expr *pLeft = pWhere->pLeft;
148997 if( ALWAYS(pLeft)(pLeft)
148998 && pLeft->op==TK_COLUMN168
148999 && pLeft->iColumn < 0
149000 ){
149001 return 0; /* Restriction (12) */
149002 }
149003 }
149004#endif
149005
149006 if( sqlite3ExprIsSingleTableConstraint(pWhere, pSrcList, iSrc, 1) ){
149007 nChng++;
149008 pSubq->selFlags |= SF_PushDown0x1000000;
149009 while( pSubq ){
149010 SubstContext x;
149011 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
149012 unsetJoinExpr(pNew, -1, 1);
149013 x.pParse = pParse;
149014 x.iTable = pSrc->iCursor;
149015 x.iNewTable = pSrc->iCursor;
149016 x.isOuterJoin = 0;
149017 x.pEList = pSubq->pEList;
149018 x.pCList = findLeftmostExprlist(pSubq);
149019 pNew = substExpr(&x, pNew);
149020#ifndef SQLITE_OMIT_WINDOWFUNC
149021 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
149022 /* Restriction 6c has prevented push-down in this case */
149023 sqlite3ExprDelete(pParse->db, pNew);
149024 nChng--;
149025 break;
149026 }
149027#endif
149028 if( pSubq->selFlags & SF_Aggregate0x0000008 ){
149029 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
149030 }else{
149031 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
149032 }
149033 pSubq = pSubq->pPrior;
149034 }
149035 }
149036 return nChng;
149037}
149038#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
149039
149040/*
149041** Check to see if a subquery contains result-set columns that are
149042** never used. If it does, change the value of those result-set columns
149043** to NULL so that they do not cause unnecessary work to compute.
149044**
149045** Return the number of column that were changed to NULL.
149046*/
149047static int disableUnusedSubqueryResultColumns(SrcItem *pItem){
149048 int nCol;
149049 Select *pSub; /* The subquery to be simplified */
149050 Select *pX; /* For looping over compound elements of pSub */
149051 Table *pTab; /* The table that describes the subquery */
149052 int j; /* Column number */
149053 int nChng = 0; /* Number of columns converted to NULL */
149054 Bitmask colUsed; /* Columns that may not be NULLed out */
149055
149056 assert( pItem!=0 )((void) (0));
149057 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
149058 return 0;
149059 }
149060 assert( pItem->pSTab!=0 )((void) (0));
149061 pTab = pItem->pSTab;
149062 assert( pItem->fg.isSubquery )((void) (0));
149063 pSub = pItem->u4.pSubq->pSelect;
149064 assert( pSub->pEList->nExpr==pTab->nCol )((void) (0));
149065 for(pX=pSub; pX; pX=pX->pPrior){
149066 if( (pX->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))!=0 ){
149067 testcase( pX->selFlags & SF_Distinct );
149068 testcase( pX->selFlags & SF_Aggregate );
149069 return 0;
149070 }
149071 if( pX->pPrior && pX->op!=TK_ALL136 ){
149072 /* This optimization does not work for compound subqueries that
149073 ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */
149074 return 0;
149075 }
149076#ifndef SQLITE_OMIT_WINDOWFUNC
149077 if( pX->pWin ){
149078 /* This optimization does not work for subqueries that use window
149079 ** functions. */
149080 return 0;
149081 }
149082#endif
149083 }
149084 colUsed = pItem->colUsed;
149085 if( pSub->pOrderBy ){
149086 ExprList *pList = pSub->pOrderBy;
149087 for(j=0; j<pList->nExpr; j++){
149088 u16 iCol = pList->a[j].u.x.iOrderByCol;
149089 if( iCol>0 ){
149090 iCol--;
149091 colUsed |= ((Bitmask)1)<<(iCol>=BMS((int)(sizeof(Bitmask)*8)) ? BMS((int)(sizeof(Bitmask)*8))-1 : iCol);
149092 }
149093 }
149094 }
149095 nCol = pTab->nCol;
149096 for(j=0; j<nCol; j++){
149097 Bitmask m = j<BMS((int)(sizeof(Bitmask)*8))-1 ? MASKBIT(j)(((Bitmask)1)<<(j)) : TOPBIT(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1));
149098 if( (m & colUsed)!=0 ) continue;
149099 for(pX=pSub; pX; pX=pX->pPrior) {
149100 Expr *pY = pX->pEList->a[j].pExpr;
149101 if( pY->op==TK_NULL122 ) continue;
149102 pY->op = TK_NULL122;
149103 ExprClearProperty(pY, EP_Skip|EP_Unlikely)(pY)->flags&=~(0x002000|0x080000);
149104 pX->selFlags |= SF_PushDown0x1000000;
149105 nChng++;
149106 }
149107 }
149108 return nChng;
149109}
149110
149111
149112/*
149113** The pFunc is the only aggregate function in the query. Check to see
149114** if the query is a candidate for the min/max optimization.
149115**
149116** If the query is a candidate for the min/max optimization, then set
149117** *ppMinMax to be an ORDER BY clause to be used for the optimization
149118** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
149119** whether pFunc is a min() or max() function.
149120**
149121** If the query is not a candidate for the min/max optimization, return
149122** WHERE_ORDERBY_NORMAL (which must be zero).
149123**
149124** This routine must be called after aggregate functions have been
149125** located but before their arguments have been subjected to aggregate
149126** analysis.
149127*/
149128static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
149129 int eRet = WHERE_ORDERBY_NORMAL0x0000; /* Return value */
149130 ExprList *pEList; /* Arguments to agg function */
149131 const char *zFunc; /* Name of aggregate function pFunc */
149132 ExprList *pOrderBy;
149133 u8 sortFlags = 0;
149134
149135 assert( *ppMinMax==0 )((void) (0));
149136 assert( pFunc->op==TK_AGG_FUNCTION )((void) (0));
149137 assert( !IsWindowFunc(pFunc) )((void) (0));
149138 assert( ExprUseXList(pFunc) )((void) (0));
149139 pEList = pFunc->x.pList;
149140 if( pEList==0
149141 || pEList->nExpr!=1
149142 || ExprHasProperty(pFunc, EP_WinFunc)(((pFunc)->flags&(0x1000000))!=0)
149143 || OptimizationDisabled(db, SQLITE_MinMaxOpt)(((db)->dbOptFlags&(0x00010000))!=0)
149144 ){
149145 return eRet;
149146 }
149147 assert( !ExprHasProperty(pFunc, EP_IntValue) )((void) (0));
149148 zFunc = pFunc->u.zToken;
149149 if( sqlite3StrICmp(zFunc, "min")==0 ){
149150 eRet = WHERE_ORDERBY_MIN0x0001;
149151 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
149152 sortFlags = KEYINFO_ORDER_BIGNULL0x02;
149153 }
149154 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
149155 eRet = WHERE_ORDERBY_MAX0x0002;
149156 sortFlags = KEYINFO_ORDER_DESC0x01;
149157 }else{
149158 return eRet;
149159 }
149160 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
149161 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
149162 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
149163 return eRet;
149164}
149165
149166/*
149167** The select statement passed as the first argument is an aggregate query.
149168** The second argument is the associated aggregate-info object. This
149169** function tests if the SELECT is of the form:
149170**
149171** SELECT count(*) FROM <tbl>
149172**
149173** where table is a database table, not a sub-select or view. If the query
149174** does match this pattern, then a pointer to the Table object representing
149175** <tbl> is returned. Otherwise, NULL is returned.
149176**
149177** This routine checks to see if it is safe to use the count optimization.
149178** A correct answer is still obtained (though perhaps more slowly) if
149179** this routine returns NULL when it could have returned a table pointer.
149180** But returning the pointer when NULL should have been returned can
149181** result in incorrect answers and/or crashes. So, when in doubt, return NULL.
149182*/
149183static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
149184 Table *pTab;
149185 Expr *pExpr;
149186
149187 assert( !p->pGroupBy )((void) (0));
149188
149189 if( p->pWhere
149190 || p->pEList->nExpr!=1
149191 || p->pSrc->nSrc!=1
149192 || p->pSrc->a[0].fg.isSubquery
149193 || pAggInfo->nFunc!=1
149194 || p->pHaving
149195 ){
149196 return 0;
149197 }
149198 pTab = p->pSrc->a[0].pSTab;
149199 assert( pTab!=0 )((void) (0));
149200 assert( !IsView(pTab) )((void) (0));
149201 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ) return 0;
149202 pExpr = p->pEList->a[0].pExpr;
149203 assert( pExpr!=0 )((void) (0));
149204 if( pExpr->op!=TK_AGG_FUNCTION169 ) return 0;
149205 if( pExpr->pAggInfo!=pAggInfo ) return 0;
149206 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT0x0100)==0 ) return 0;
149207 assert( pAggInfo->aFunc[0].pFExpr==pExpr )((void) (0));
149208 testcase( ExprHasProperty(pExpr, EP_Distinct) );
149209 testcase( ExprHasProperty(pExpr, EP_WinFunc) );
149210 if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc)(((pExpr)->flags&(0x000004|0x1000000))!=0) ) return 0;
149211
149212 return pTab;
149213}
149214
149215/*
149216** If the source-list item passed as an argument was augmented with an
149217** INDEXED BY clause, then try to locate the specified index. If there
149218** was such a clause and the named index cannot be found, return
149219** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
149220** pFrom->pIndex and return SQLITE_OK.
149221*/
149222SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){
149223 Table *pTab = pFrom->pSTab;
149224 char *zIndexedBy = pFrom->u1.zIndexedBy;
149225 Index *pIdx;
149226 assert( pTab!=0 )((void) (0));
149227 assert( pFrom->fg.isIndexedBy!=0 )((void) (0));
149228
149229 for(pIdx=pTab->pIndex;
149230 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
149231 pIdx=pIdx->pNext
149232 );
149233 if( !pIdx ){
149234 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
149235 pParse->checkSchema = 1;
149236 return SQLITE_ERROR1;
149237 }
149238 assert( pFrom->fg.isCte==0 )((void) (0));
149239 pFrom->u2.pIBIndex = pIdx;
149240 return SQLITE_OK0;
149241}
149242
149243/*
149244** Detect compound SELECT statements that use an ORDER BY clause with
149245** an alternative collating sequence.
149246**
149247** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
149248**
149249** These are rewritten as a subquery:
149250**
149251** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
149252** ORDER BY ... COLLATE ...
149253**
149254** This transformation is necessary because the multiSelectOrderBy() routine
149255** above that generates the code for a compound SELECT with an ORDER BY clause
149256** uses a merge algorithm that requires the same collating sequence on the
149257** result columns as on the ORDER BY clause. See ticket
149258** http://www.sqlite.org/src/info/6709574d2a
149259**
149260** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
149261** The UNION ALL operator works fine with multiSelectOrderBy() even when
149262** there are COLLATE terms in the ORDER BY.
149263*/
149264static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
149265 int i;
149266 Select *pNew;
149267 Select *pX;
149268 sqlite3 *db;
149269 struct ExprList_item *a;
149270 SrcList *pNewSrc;
149271 Parse *pParse;
149272 Token dummy;
149273
149274 if( p->pPrior==0 ) return WRC_Continue0;
149275 if( p->pOrderBy==0 ) return WRC_Continue0;
149276 for(pX=p; pX && (pX->op==TK_ALL136 || pX->op==TK_SELECT139); pX=pX->pPrior){}
149277 if( pX==0 ) return WRC_Continue0;
149278 a = p->pOrderBy->a;
149279#ifndef SQLITE_OMIT_WINDOWFUNC
149280 /* If iOrderByCol is already non-zero, then it has already been matched
149281 ** to a result column of the SELECT statement. This occurs when the
149282 ** SELECT is rewritten for window-functions processing and then passed
149283 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
149284 ** by this function is not required in this case. */
149285 if( a[0].u.x.iOrderByCol ) return WRC_Continue0;
149286#endif
149287 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
149288 if( a[i].pExpr->flags & EP_Collate0x000200 ) break;
149289 }
149290 if( i<0 ) return WRC_Continue0;
149291
149292 /* If we reach this point, that means the transformation is required. */
149293
149294 pParse = pWalker->pParse;
149295 db = pParse->db;
149296 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
149297 if( pNew==0 ) return WRC_Abort2;
149298 memset(&dummy, 0, sizeof(dummy));
149299 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0);
149300 assert( pNewSrc!=0 || pParse->nErr )((void) (0));
149301 if( pParse->nErr ){
149302 sqlite3SrcListDelete(db, pNewSrc);
149303 return WRC_Abort2;
149304 }
149305 *pNew = *p;
149306 p->pSrc = pNewSrc;
149307 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK180, 0));
149308 p->op = TK_SELECT139;
149309 p->pWhere = 0;
149310 pNew->pGroupBy = 0;
149311 pNew->pHaving = 0;
149312 pNew->pOrderBy = 0;
149313 p->pPrior = 0;
149314 p->pNext = 0;
149315 p->pWith = 0;
149316#ifndef SQLITE_OMIT_WINDOWFUNC
149317 p->pWinDefn = 0;
149318#endif
149319 p->selFlags &= ~SF_Compound0x0000100;
149320 assert( (p->selFlags & SF_Converted)==0 )((void) (0));
149321 p->selFlags |= SF_Converted0x0010000;
149322 assert( pNew->pPrior!=0 )((void) (0));
149323 pNew->pPrior->pNext = pNew;
149324 pNew->pLimit = 0;
149325 return WRC_Continue0;
149326}
149327
149328/*
149329** Check to see if the FROM clause term pFrom has table-valued function
149330** arguments. If it does, leave an error message in pParse and return
149331** non-zero, since pFrom is not allowed to be a table-valued function.
149332*/
149333static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){
149334 if( pFrom->fg.isTabFunc ){
149335 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
149336 return 1;
149337 }
149338 return 0;
149339}
149340
149341#ifndef SQLITE_OMIT_CTE
149342/*
149343** Argument pWith (which may be NULL) points to a linked list of nested
149344** WITH contexts, from inner to outermost. If the table identified by
149345** FROM clause element pItem is really a common-table-expression (CTE)
149346** then return a pointer to the CTE definition for that table. Otherwise
149347** return NULL.
149348**
149349** If a non-NULL value is returned, set *ppContext to point to the With
149350** object that the returned CTE belongs to.
149351*/
149352static struct Cte *searchWith(
149353 With *pWith, /* Current innermost WITH clause */
149354 SrcItem *pItem, /* FROM clause element to resolve */
149355 With **ppContext /* OUT: WITH clause return value belongs to */
149356){
149357 const char *zName = pItem->zName;
149358 With *p;
149359 assert( pItem->fg.fixedSchema || pItem->u4.zDatabase==0 )((void) (0));
149360 assert( zName!=0 )((void) (0));
149361 for(p=pWith; p; p=p->pOuter){
149362 int i;
149363 for(i=0; i<p->nCte; i++){
149364 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
149365 *ppContext = p;
149366 return &p->a[i];
149367 }
149368 }
149369 if( p->bView ) break;
149370 }
149371 return 0;
149372}
149373
149374/* The code generator maintains a stack of active WITH clauses
149375** with the inner-most WITH clause being at the top of the stack.
149376**
149377** This routine pushes the WITH clause passed as the second argument
149378** onto the top of the stack. If argument bFree is true, then this
149379** WITH clause will never be popped from the stack but should instead
149380** be freed along with the Parse object. In other cases, when
149381** bFree==0, the With object will be freed along with the SELECT
149382** statement with which it is associated.
149383**
149384** This routine returns a copy of pWith. Or, if bFree is true and
149385** the pWith object is destroyed immediately due to an OOM condition,
149386** then this routine return NULL.
149387**
149388** If bFree is true, do not continue to use the pWith pointer after
149389** calling this routine, Instead, use only the return value.
149390*/
149391SQLITE_PRIVATEstatic With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
149392 if( pWith ){
149393 if( bFree ){
149394 pWith = (With*)sqlite3ParserAddCleanup(pParse, sqlite3WithDeleteGeneric,
149395 pWith);
149396 if( pWith==0 ) return 0;
149397 }
149398 if( pParse->nErr==0 ){
149399 assert( pParse->pWith!=pWith )((void) (0));
149400 pWith->pOuter = pParse->pWith;
149401 pParse->pWith = pWith;
149402 }
149403 }
149404 return pWith;
149405}
149406
149407/*
149408** This function checks if argument pFrom refers to a CTE declared by
149409** a WITH clause on the stack currently maintained by the parser (on the
149410** pParse->pWith linked list). And if currently processing a CTE
149411** CTE expression, through routine checks to see if the reference is
149412** a recursive reference to the CTE.
149413**
149414** If pFrom matches a CTE according to either of these two above, pFrom->pTab
149415** and other fields are populated accordingly.
149416**
149417** Return 0 if no match is found.
149418** Return 1 if a match is found.
149419** Return 2 if an error condition is detected.
149420*/
149421static int resolveFromTermToCte(
149422 Parse *pParse, /* The parsing context */
149423 Walker *pWalker, /* Current tree walker */
149424 SrcItem *pFrom /* The FROM clause term to check */
149425){
149426 Cte *pCte; /* Matched CTE (or NULL if no match) */
149427 With *pWith; /* The matching WITH */
149428
149429 assert( pFrom->pSTab==0 )((void) (0));
149430 if( pParse->pWith==0 ){
149431 /* There are no WITH clauses in the stack. No match is possible */
149432 return 0;
149433 }
149434 if( pParse->nErr ){
149435 /* Prior errors might have left pParse->pWith in a goofy state, so
149436 ** go no further. */
149437 return 0;
149438 }
149439 assert( pFrom->fg.hadSchema==0 || pFrom->fg.notCte!=0 )((void) (0));
149440 if( pFrom->fg.fixedSchema==0 && pFrom->u4.zDatabase!=0 ){
149441 /* The FROM term contains a schema qualifier (ex: main.t1) and so
149442 ** it cannot possibly be a CTE reference. */
149443 return 0;
149444 }
149445 if( pFrom->fg.notCte ){
149446 /* The FROM term is specifically excluded from matching a CTE.
149447 ** (1) It is part of a trigger that used to have zDatabase but had
149448 ** zDatabase removed by sqlite3FixTriggerStep().
149449 ** (2) This is the first term in the FROM clause of an UPDATE.
149450 */
149451 return 0;
149452 }
149453 pCte = searchWith(pParse->pWith, pFrom, &pWith);
149454 if( pCte ){
149455 sqlite3 *db = pParse->db;
149456 Table *pTab;
149457 ExprList *pEList;
149458 Select *pSel;
149459 Select *pLeft; /* Left-most SELECT statement */
149460 Select *pRecTerm; /* Left-most recursive term */
149461 int bMayRecursive; /* True if compound joined by UNION [ALL] */
149462 With *pSavedWith; /* Initial value of pParse->pWith */
149463 int iRecTab = -1; /* Cursor for recursive table */
149464 CteUse *pCteUse;
149465
149466 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
149467 ** recursive reference to CTE pCte. Leave an error in pParse and return
149468 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
149469 ** In this case, proceed. */
149470 if( pCte->zCteErr ){
149471 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
149472 return 2;
149473 }
149474 if( cannotBeFunction(pParse, pFrom) ) return 2;
149475
149476 assert( pFrom->pSTab==0 )((void) (0));
149477 pTab = sqlite3DbMallocZero(db, sizeof(Table));
149478 if( pTab==0 ) return 2;
149479 pCteUse = pCte->pUse;
149480 if( pCteUse==0 ){
149481 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
149482 if( pCteUse==0
149483 || sqlite3ParserAddCleanup(pParse,sqlite3DbFree,pCteUse)==0
149484 ){
149485 sqlite3DbFree(db, pTab);
149486 return 2;
149487 }
149488 pCteUse->eM10d = pCte->eM10d;
149489 }
149490 pFrom->pSTab = pTab;
149491 pTab->nTabRef = 1;
149492 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
149493 pTab->iPKey = -1;
149494 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
149495 pTab->tabFlags |= TF_Ephemeral0x00004000 | TF_NoVisibleRowid0x00000200;
149496 sqlite3SrcItemAttachSubquery(pParse, pFrom, pCte->pSelect, 1);
149497 if( db->mallocFailed ) return 2;
149498 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq )((void) (0));
149499 pSel = pFrom->u4.pSubq->pSelect;
149500 assert( pSel!=0 )((void) (0));
149501 pSel->selFlags |= SF_CopyCte0x4000000;
149502 if( pFrom->fg.isIndexedBy ){
149503 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
149504 return 2;
149505 }
149506 assert( !pFrom->fg.isIndexedBy )((void) (0));
149507 pFrom->fg.isCte = 1;
149508 pFrom->u2.pCteUse = pCteUse;
149509 pCteUse->nUse++;
149510
149511 /* Check if this is a recursive CTE. */
149512 pRecTerm = pSel;
149513 bMayRecursive = ( pSel->op==TK_ALL136 || pSel->op==TK_UNION135 );
149514 while( bMayRecursive && pRecTerm->op==pSel->op ){
149515 int i;
149516 SrcList *pSrc = pRecTerm->pSrc;
149517 assert( pRecTerm->pPrior!=0 )((void) (0));
149518 for(i=0; i<pSrc->nSrc; i++){
149519 SrcItem *pItem = &pSrc->a[i];
149520 if( pItem->zName!=0
149521 && !pItem->fg.hadSchema
149522 && ALWAYS( !pItem->fg.isSubquery )(!pItem->fg.isSubquery)
149523 && (pItem->fg.fixedSchema || pItem->u4.zDatabase==0)
149524 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
149525 ){
149526 pItem->pSTab = pTab;
149527 pTab->nTabRef++;
149528 pItem->fg.isRecursive = 1;
149529 if( pRecTerm->selFlags & SF_Recursive0x0002000 ){
149530 sqlite3ErrorMsg(pParse,
149531 "multiple references to recursive table: %s", pCte->zName
149532 );
149533 return 2;
149534 }
149535 pRecTerm->selFlags |= SF_Recursive0x0002000;
149536 if( iRecTab<0 ) iRecTab = pParse->nTab++;
149537 pItem->iCursor = iRecTab;
149538 }
149539 }
149540 if( (pRecTerm->selFlags & SF_Recursive0x0002000)==0 ) break;
149541 pRecTerm = pRecTerm->pPrior;
149542 }
149543
149544 pCte->zCteErr = "circular reference: %s";
149545 pSavedWith = pParse->pWith;
149546 pParse->pWith = pWith;
149547 if( pSel->selFlags & SF_Recursive0x0002000 ){
149548 int rc;
149549 assert( pRecTerm!=0 )((void) (0));
149550 assert( (pRecTerm->selFlags & SF_Recursive)==0 )((void) (0));
149551 assert( pRecTerm->pNext!=0 )((void) (0));
149552 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 )((void) (0));
149553 assert( pRecTerm->pWith==0 )((void) (0));
149554 pRecTerm->pWith = pSel->pWith;
149555 rc = sqlite3WalkSelect(pWalker, pRecTerm);
149556 pRecTerm->pWith = 0;
149557 if( rc ){
149558 pParse->pWith = pSavedWith;
149559 return 2;
149560 }
149561 }else{
149562 if( sqlite3WalkSelect(pWalker, pSel) ){
149563 pParse->pWith = pSavedWith;
149564 return 2;
149565 }
149566 }
149567 pParse->pWith = pWith;
149568
149569 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
149570 pEList = pLeft->pEList;
149571 if( pCte->pCols ){
149572 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
149573 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
149574 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
149575 );
149576 pParse->pWith = pSavedWith;
149577 return 2;
149578 }
149579 pEList = pCte->pCols;
149580 }
149581
149582 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
149583 if( bMayRecursive ){
149584 if( pSel->selFlags & SF_Recursive0x0002000 ){
149585 pCte->zCteErr = "multiple recursive references: %s";
149586 }else{
149587 pCte->zCteErr = "recursive reference in a subquery: %s";
149588 }
149589 sqlite3WalkSelect(pWalker, pSel);
149590 }
149591 pCte->zCteErr = 0;
149592 pParse->pWith = pSavedWith;
149593 return 1; /* Success */
149594 }
149595 return 0; /* No match */
149596}
149597#endif
149598
149599#ifndef SQLITE_OMIT_CTE
149600/*
149601** If the SELECT passed as the second argument has an associated WITH
149602** clause, pop it from the stack stored as part of the Parse object.
149603**
149604** This function is used as the xSelectCallback2() callback by
149605** sqlite3SelectExpand() when walking a SELECT tree to resolve table
149606** names and other FROM clause elements.
149607*/
149608SQLITE_PRIVATEstatic void sqlite3SelectPopWith(Walker *pWalker, Select *p){
149609 Parse *pParse = pWalker->pParse;
149610 if( OK_IF_ALWAYS_TRUE(pParse->pWith)(pParse->pWith) && p->pPrior==0 ){
149611 With *pWith = findRightmost(p)->pWith;
149612 if( pWith!=0 ){
149613 assert( pParse->pWith==pWith || pParse->nErr )((void) (0));
149614 pParse->pWith = pWith->pOuter;
149615 }
149616 }
149617}
149618#endif
149619
149620/*
149621** The SrcItem structure passed as the second argument represents a
149622** sub-query in the FROM clause of a SELECT statement. This function
149623** allocates and populates the SrcItem.pTab object. If successful,
149624** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
149625** SQLITE_NOMEM.
149626*/
149627SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
149628 Select *pSel;
149629 Table *pTab;
149630
149631 assert( pFrom->fg.isSubquery )((void) (0));
149632 assert( pFrom->u4.pSubq!=0 )((void) (0));
149633 pSel = pFrom->u4.pSubq->pSelect;
149634 assert( pSel )((void) (0));
149635 pFrom->pSTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
149636 if( pTab==0 ) return SQLITE_NOMEM7;
149637 pTab->nTabRef = 1;
149638 if( pFrom->zAlias ){
149639 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
149640 }else{
149641 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
149642 }
149643 while( pSel->pPrior ){ pSel = pSel->pPrior; }
149644 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
149645 pTab->iPKey = -1;
149646 pTab->eTabType = TABTYP_VIEW2;
149647 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
149648#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
149649 /* The usual case - do not allow ROWID on a subquery */
149650 pTab->tabFlags |= TF_Ephemeral0x00004000 | TF_NoVisibleRowid0x00000200;
149651#else
149652 /* Legacy compatibility mode */
149653 pTab->tabFlags |= TF_Ephemeral0x00004000 | sqlite3Config.mNoVisibleRowid;
149654#endif
149655 return pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
149656}
149657
149658
149659/*
149660** Check the N SrcItem objects to the right of pBase. (N might be zero!)
149661** If any of those SrcItem objects have a USING clause containing zName
149662** then return true.
149663**
149664** If N is zero, or none of the N SrcItem objects to the right of pBase
149665** contains a USING clause, or if none of the USING clauses contain zName,
149666** then return false.
149667*/
149668static int inAnyUsingClause(
149669 const char *zName, /* Name we are looking for */
149670 SrcItem *pBase, /* The base SrcItem. Looking at pBase[1] and following */
149671 int N /* How many SrcItems to check */
149672){
149673 while( N>0 ){
149674 N--;
149675 pBase++;
149676 if( pBase->fg.isUsing==0 ) continue;
149677 if( NEVER(pBase->u3.pUsing==0)(pBase->u3.pUsing==0) ) continue;
149678 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
149679 }
149680 return 0;
149681}
149682
149683
149684/*
149685** This routine is a Walker callback for "expanding" a SELECT statement.
149686** "Expanding" means to do the following:
149687**
149688** (1) Make sure VDBE cursor numbers have been assigned to every
149689** element of the FROM clause.
149690**
149691** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
149692** defines FROM clause. When views appear in the FROM clause,
149693** fill pTabList->a[].pSelect with a copy of the SELECT statement
149694** that implements the view. A copy is made of the view's SELECT
149695** statement so that we can freely modify or delete that statement
149696** without worrying about messing up the persistent representation
149697** of the view.
149698**
149699** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
149700** on joins and the ON and USING clause of joins.
149701**
149702** (4) Scan the list of columns in the result set (pEList) looking
149703** for instances of the "*" operator or the TABLE.* operator.
149704** If found, expand each "*" to be every column in every table
149705** and TABLE.* to be every column in TABLE.
149706**
149707*/
149708static int selectExpander(Walker *pWalker, Select *p){
149709 Parse *pParse = pWalker->pParse;
149710 int i, j, k, rc;
149711 SrcList *pTabList;
149712 ExprList *pEList;
149713 SrcItem *pFrom;
149714 sqlite3 *db = pParse->db;
149715 Expr *pE, *pRight, *pExpr;
149716 u16 selFlags = p->selFlags;
149717 u32 elistFlags = 0;
149718
149719 p->selFlags |= SF_Expanded0x0000040;
149720 if( db->mallocFailed ){
149721 return WRC_Abort2;
149722 }
149723 assert( p->pSrc!=0 )((void) (0));
149724 if( (selFlags & SF_Expanded0x0000040)!=0 ){
149725 return WRC_Prune1;
149726 }
149727 if( pWalker->eCode ){
149728 /* Renumber selId because it has been copied from a view */
149729 p->selId = ++pParse->nSelect;
149730 }
149731 pTabList = p->pSrc;
149732 pEList = p->pEList;
149733 if( pParse->pWith && (p->selFlags & SF_View0x0200000) ){
149734 if( p->pWith==0 ){
149735 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
149736 if( p->pWith==0 ){
149737 return WRC_Abort2;
149738 }
149739 }
149740 p->pWith->bView = 1;
149741 }
149742 sqlite3WithPush(pParse, p->pWith, 0);
149743
149744 /* Make sure cursor numbers have been assigned to all entries in
149745 ** the FROM clause of the SELECT statement.
149746 */
149747 sqlite3SrcListAssignCursors(pParse, pTabList);
149748
149749 /* Look up every table named in the FROM clause of the select. If
149750 ** an entry of the FROM clause is a subquery instead of a table or view,
149751 ** then create a transient table structure to describe the subquery.
149752 */
149753 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
149754 Table *pTab;
149755 assert( pFrom->fg.isRecursive==0 || pFrom->pSTab!=0 )((void) (0));
149756 if( pFrom->pSTab ) continue;
149757 assert( pFrom->fg.isRecursive==0 )((void) (0));
149758 if( pFrom->zName==0 ){
149759#ifndef SQLITE_OMIT_SUBQUERY
149760 Select *pSel;
149761 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq!=0 )((void) (0));
149762 pSel = pFrom->u4.pSubq->pSelect;
149763 /* A sub-query in the FROM clause of a SELECT */
149764 assert( pSel!=0 )((void) (0));
149765 assert( pFrom->pSTab==0 )((void) (0));
149766 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort2;
149767 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort2;
149768#endif
149769#ifndef SQLITE_OMIT_CTE
149770 }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){
149771 if( rc>1 ) return WRC_Abort2;
149772 pTab = pFrom->pSTab;
149773 assert( pTab!=0 )((void) (0));
149774#endif
149775 }else{
149776 /* An ordinary table or view name in the FROM clause */
149777 assert( pFrom->pSTab==0 )((void) (0));
149778 pFrom->pSTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
149779 if( pTab==0 ) return WRC_Abort2;
149780 if( pTab->nTabRef>=0xffff ){
149781 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
149782 pTab->zName);
149783 pFrom->pSTab = 0;
149784 return WRC_Abort2;
149785 }
149786 pTab->nTabRef++;
149787 if( !IsVirtual(pTab)((pTab)->eTabType==1) && cannotBeFunction(pParse, pFrom) ){
149788 return WRC_Abort2;
149789 }
149790#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
149791 if( !IsOrdinaryTable(pTab)((pTab)->eTabType==0) ){
149792 i16 nCol;
149793 u8 eCodeOrig = pWalker->eCode;
149794 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort2;
149795 assert( pFrom->fg.isSubquery==0 )((void) (0));
149796 if( IsView(pTab)((pTab)->eTabType==2) ){
149797 if( (db->flags & SQLITE_EnableView0x80000000)==0
149798 && pTab->pSchema!=db->aDb[1].pSchema
149799 ){
149800 sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
149801 pTab->zName);
149802 }
149803 sqlite3SrcItemAttachSubquery(pParse, pFrom, pTab->u.view.pSelect, 1);
149804 }
149805#ifndef SQLITE_OMIT_VIRTUALTABLE
149806 else if( ALWAYS(IsVirtual(pTab))(((pTab)->eTabType==1))
149807 && pFrom->fg.fromDDL
149808 && ALWAYS(pTab->u.vtab.p!=0)(pTab->u.vtab.p!=0)
149809 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema0x00000080)!=0)
149810 ){
149811 sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"",
149812 pTab->zName);
149813 }
149814 assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 )((void) (0));
149815#endif
149816 nCol = pTab->nCol;
149817 pTab->nCol = -1;
149818 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
149819 if( pFrom->fg.isSubquery ){
149820 sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect);
149821 }
149822 pWalker->eCode = eCodeOrig;
149823 pTab->nCol = nCol;
149824 }
149825#endif
149826 }
149827
149828 /* Locate the index named by the INDEXED BY clause, if any. */
149829 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
149830 return WRC_Abort2;
149831 }
149832 }
149833
149834 /* Process NATURAL keywords, and ON and USING clauses of joins.
149835 */
149836 assert( db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
149837 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
149838 return WRC_Abort2;
149839 }
149840
149841 /* For every "*" that occurs in the column list, insert the names of
149842 ** all columns in all tables. And for every TABLE.* insert the names
149843 ** of all columns in TABLE. The parser inserted a special expression
149844 ** with the TK_ASTERISK operator for each "*" that it found in the column
149845 ** list. The following code just has to locate the TK_ASTERISK
149846 ** expressions and expand each one to the list of all columns in
149847 ** all tables.
149848 **
149849 ** The first loop just checks to see if there are any "*" operators
149850 ** that need expanding.
149851 */
149852 for(k=0; k<pEList->nExpr; k++){
149853 pE = pEList->a[k].pExpr;
149854 if( pE->op==TK_ASTERISK180 ) break;
149855 assert( pE->op!=TK_DOT || pE->pRight!=0 )((void) (0));
149856 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) )((void) (0));
149857 if( pE->op==TK_DOT142 && pE->pRight->op==TK_ASTERISK180 ) break;
149858 elistFlags |= pE->flags;
149859 }
149860 if( k<pEList->nExpr ){
149861 /*
149862 ** If we get here it means the result set contains one or more "*"
149863 ** operators that need to be expanded. Loop through each expression
149864 ** in the result set and expand them one by one.
149865 */
149866 struct ExprList_item *a = pEList->a;
149867 ExprList *pNew = 0;
149868 int flags = pParse->db->flags;
149869 int longNames = (flags & SQLITE_FullColNames0x00000004)!=0
149870 && (flags & SQLITE_ShortColNames0x00000040)==0;
149871
149872 for(k=0; k<pEList->nExpr; k++){
149873 pE = a[k].pExpr;
149874 elistFlags |= pE->flags;
149875 pRight = pE->pRight;
149876 assert( pE->op!=TK_DOT || pRight!=0 )((void) (0));
149877 if( pE->op!=TK_ASTERISK180
149878 && (pE->op!=TK_DOT142 || pRight->op!=TK_ASTERISK180)
149879 ){
149880 /* This particular expression does not need to be expanded.
149881 */
149882 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
149883 if( pNew ){
149884 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
149885 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
149886 a[k].zEName = 0;
149887 }
149888 a[k].pExpr = 0;
149889 }else{
149890 /* This expression is a "*" or a "TABLE.*" and needs to be
149891 ** expanded. */
149892 int tableSeen = 0; /* Set to 1 when TABLE matches */
149893 char *zTName = 0; /* text of name of TABLE */
149894 int iErrOfst;
149895 if( pE->op==TK_DOT142 ){
149896 assert( (selFlags & SF_NestedFrom)==0 )((void) (0));
149897 assert( pE->pLeft!=0 )((void) (0));
149898 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) )((void) (0));
149899 zTName = pE->pLeft->u.zToken;
149900 assert( ExprUseWOfst(pE->pLeft) )((void) (0));
149901 iErrOfst = pE->pRight->w.iOfst;
149902 }else{
149903 assert( ExprUseWOfst(pE) )((void) (0));
149904 iErrOfst = pE->w.iOfst;
149905 }
149906 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
149907 int nAdd; /* Number of cols including rowid */
149908 Table *pTab = pFrom->pSTab; /* Table for this data source */
149909 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
149910 char *zTabName; /* AS name for this data source */
149911 const char *zSchemaName = 0; /* Schema name for this data source */
149912 int iDb; /* Schema index for this data src */
149913 IdList *pUsing; /* USING clause for pFrom[1] */
149914
149915 if( (zTabName = pFrom->zAlias)==0 ){
149916 zTabName = pTab->zName;
149917 }
149918 if( db->mallocFailed ) break;
149919 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom) )((void) (0));
149920 if( pFrom->fg.isNestedFrom ){
149921 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq )((void) (0));
149922 assert( pFrom->u4.pSubq->pSelect!=0 )((void) (0));
149923 pNestedFrom = pFrom->u4.pSubq->pSelect->pEList;
149924 assert( pNestedFrom!=0 )((void) (0));
149925 assert( pNestedFrom->nExpr==pTab->nCol )((void) (0));
149926 assert( VisibleRowid(pTab)==0 || ViewCanHaveRowid )((void) (0));
149927 }else{
149928 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
149929 continue;
149930 }
149931 pNestedFrom = 0;
149932 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
149933 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
149934 }
149935 if( i+1<pTabList->nSrc
149936 && pFrom[1].fg.isUsing
149937 && (selFlags & SF_NestedFrom0x0000800)!=0
149938 ){
149939 int ii;
149940 pUsing = pFrom[1].u3.pUsing;
149941 for(ii=0; ii<pUsing->nId; ii++){
149942 const char *zUName = pUsing->a[ii].zName;
149943 pRight = sqlite3Expr(db, TK_ID60, zUName);
149944 sqlite3ExprSetErrorOffset(pRight, iErrOfst);
149945 pNew = sqlite3ExprListAppend(pParse, pNew, pRight);
149946 if( pNew ){
149947 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
149948 assert( pX->zEName==0 )((void) (0));
149949 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
149950 pX->fg.eEName = ENAME_TAB2;
149951 pX->fg.bUsingTerm = 1;
149952 }
149953 }
149954 }else{
149955 pUsing = 0;
149956 }
149957
149958 nAdd = pTab->nCol;
149959 if( VisibleRowid(pTab)(((pTab)->tabFlags & 0x00000200)==0) && (selFlags & SF_NestedFrom0x0000800)!=0 ) nAdd++;
149960 for(j=0; j<nAdd; j++){
149961 const char *zName;
149962 struct ExprList_item *pX; /* Newly added ExprList term */
149963
149964 if( j==pTab->nCol ){
149965 zName = sqlite3RowidAlias(pTab);
149966 if( zName==0 ) continue;
149967 }else{
149968 zName = pTab->aCol[j].zCnName;
149969
149970 /* If pTab is actually an SF_NestedFrom sub-select, do not
149971 ** expand any ENAME_ROWID columns. */
149972 if( pNestedFrom && pNestedFrom->a[j].fg.eEName==ENAME_ROWID3 ){
149973 continue;
149974 }
149975
149976 if( zTName
149977 && pNestedFrom
149978 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0, 0)==0
149979 ){
149980 continue;
149981 }
149982
149983 /* If a column is marked as 'hidden', omit it from the expanded
149984 ** result-set list unless the SELECT has the SF_IncludeHidden
149985 ** bit set.
149986 */
149987 if( (p->selFlags & SF_IncludeHidden0x0020000)==0
149988 && IsHiddenColumn(&pTab->aCol[j])(((&pTab->aCol[j])->colFlags & 0x0002)!=0)
149989 ){
149990 continue;
149991 }
149992 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND0x0400)!=0
149993 && zTName==0
149994 && (selFlags & (SF_NestedFrom0x0000800))==0
149995 ){
149996 continue;
149997 }
149998 }
149999 assert( zName )((void) (0));
150000 tableSeen = 1;
150001
150002 if( i>0 && zTName==0 && (selFlags & SF_NestedFrom0x0000800)==0 ){
150003 if( pFrom->fg.isUsing
150004 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
150005 ){
150006 /* In a join with a USING clause, omit columns in the
150007 ** using clause from the table on the right. */
150008 continue;
150009 }
150010 }
150011 pRight = sqlite3Expr(db, TK_ID60, zName);
150012 if( (pTabList->nSrc>1
150013 && ( (pFrom->fg.jointype & JT_LTORJ0x40)==0
150014 || (selFlags & SF_NestedFrom0x0000800)!=0
150015 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
150016 )
150017 )
150018 || IN_RENAME_OBJECT(pParse->eParseMode>=2)
150019 ){
150020 Expr *pLeft;
150021 pLeft = sqlite3Expr(db, TK_ID60, zTabName);
150022 pExpr = sqlite3PExpr(pParse, TK_DOT142, pLeft, pRight);
150023 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pE->pLeft ){
150024 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
150025 }
150026 if( zSchemaName ){
150027 pLeft = sqlite3Expr(db, TK_ID60, zSchemaName);
150028 pExpr = sqlite3PExpr(pParse, TK_DOT142, pLeft, pExpr);
150029 }
150030 }else{
150031 pExpr = pRight;
150032 }
150033 sqlite3ExprSetErrorOffset(pExpr, iErrOfst);
150034 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
150035 if( pNew==0 ){
150036 break; /* OOM */
150037 }
150038 pX = &pNew->a[pNew->nExpr-1];
150039 assert( pX->zEName==0 )((void) (0));
150040 if( (selFlags & SF_NestedFrom0x0000800)!=0 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
150041 if( pNestedFrom && (!ViewCanHaveRowid0 || j<pNestedFrom->nExpr) ){
150042 assert( j<pNestedFrom->nExpr )((void) (0));
150043 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
150044 testcase( pX->zEName==0 );
150045 }else{
150046 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
150047 zSchemaName, zTabName, zName);
150048 testcase( pX->zEName==0 );
150049 }
150050 pX->fg.eEName = (j==pTab->nCol ? ENAME_ROWID3 : ENAME_TAB2);
150051 if( (pFrom->fg.isUsing
150052 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
150053 || (pUsing && sqlite3IdListIndex(pUsing, zName)>=0)
150054 || (j<pTab->nCol && (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND0x0400))
150055 ){
150056 pX->fg.bNoExpand = 1;
150057 }
150058 }else if( longNames ){
150059 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
150060 pX->fg.eEName = ENAME_NAME0;
150061 }else{
150062 pX->zEName = sqlite3DbStrDup(db, zName);
150063 pX->fg.eEName = ENAME_NAME0;
150064 }
150065 }
150066 }
150067 if( !tableSeen ){
150068 if( zTName ){
150069 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
150070 }else{
150071 sqlite3ErrorMsg(pParse, "no tables specified");
150072 }
150073 }
150074 }
150075 }
150076 sqlite3ExprListDelete(db, pEList);
150077 p->pEList = pNew;
150078 }
150079 if( p->pEList ){
150080 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
150081 sqlite3ErrorMsg(pParse, "too many columns in result set");
150082 return WRC_Abort2;
150083 }
150084 if( (elistFlags & (EP_HasFunc0x000008|EP_Subquery0x400000))!=0 ){
150085 p->selFlags |= SF_ComplexResult0x0040000;
150086 }
150087 }
150088#if TREETRACE_ENABLED0
150089 if( sqlite3TreeTrace & 0x8 ){
150090 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
150091 sqlite3TreeViewSelect(0, p, 0);
150092 }
150093#endif
150094 return WRC_Continue0;
150095}
150096
150097#if SQLITE_DEBUG
150098/*
150099** Always assert. This xSelectCallback2 implementation proves that the
150100** xSelectCallback2 is never invoked.
150101*/
150102SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
150103 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
150104 assert( 0 )((void) (0));
150105}
150106#endif
150107/*
150108** This routine "expands" a SELECT statement and all of its subqueries.
150109** For additional information on what it means to "expand" a SELECT
150110** statement, see the comment on the selectExpand worker callback above.
150111**
150112** Expanding a SELECT statement is the first step in processing a
150113** SELECT statement. The SELECT statement must be expanded before
150114** name resolution is performed.
150115**
150116** If anything goes wrong, an error message is written into pParse.
150117** The calling function can detect the problem by looking at pParse->nErr
150118** and/or pParse->db->mallocFailed.
150119*/
150120static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
150121 Walker w;
150122 w.xExprCallback = sqlite3ExprWalkNoop;
150123 w.pParse = pParse;
150124 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound)(pParse->hasCompound) ){
150125 w.xSelectCallback = convertCompoundSelectToSubquery;
150126 w.xSelectCallback2 = 0;
150127 sqlite3WalkSelect(&w, pSelect);
150128 }
150129 w.xSelectCallback = selectExpander;
150130 w.xSelectCallback2 = sqlite3SelectPopWith;
150131 w.eCode = 0;
150132 sqlite3WalkSelect(&w, pSelect);
150133}
150134
150135
150136#ifndef SQLITE_OMIT_SUBQUERY
150137/*
150138** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
150139** interface.
150140**
150141** For each FROM-clause subquery, add Column.zType, Column.zColl, and
150142** Column.affinity information to the Table structure that represents
150143** the result set of that subquery.
150144**
150145** The Table structure that represents the result set was constructed
150146** by selectExpander() but the type and collation and affinity information
150147** was omitted at that point because identifiers had not yet been resolved.
150148** This routine is called after identifier resolution.
150149*/
150150static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
150151 Parse *pParse;
150152 int i;
150153 SrcList *pTabList;
150154 SrcItem *pFrom;
150155
150156 if( p->selFlags & SF_HasTypeInfo0x0000080 ) return;
150157 p->selFlags |= SF_HasTypeInfo0x0000080;
150158 pParse = pWalker->pParse;
150159 assert( (p->selFlags & SF_Resolved) )((void) (0));
150160 pTabList = p->pSrc;
150161 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
150162 Table *pTab = pFrom->pSTab;
150163 assert( pTab!=0 )((void) (0));
150164 if( (pTab->tabFlags & TF_Ephemeral0x00004000)!=0 && pFrom->fg.isSubquery ){
150165 /* A sub-query in the FROM clause of a SELECT */
150166 Select *pSel = pFrom->u4.pSubq->pSelect;
150167 sqlite3SubqueryColumnTypes(pParse, pTab, pSel, SQLITE_AFF_NONE0x40);
150168 }
150169 }
150170}
150171#endif
150172
150173
150174/*
150175** This routine adds datatype and collating sequence information to
150176** the Table structures of all FROM-clause subqueries in a
150177** SELECT statement.
150178**
150179** Use this routine after name resolution.
150180*/
150181static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
150182#ifndef SQLITE_OMIT_SUBQUERY
150183 Walker w;
150184 w.xSelectCallback = sqlite3SelectWalkNoop;
150185 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
150186 w.xExprCallback = sqlite3ExprWalkNoop;
150187 w.pParse = pParse;
150188 sqlite3WalkSelect(&w, pSelect);
150189#endif
150190}
150191
150192
150193/*
150194** This routine sets up a SELECT statement for processing. The
150195** following is accomplished:
150196**
150197** * VDBE Cursor numbers are assigned to all FROM-clause terms.
150198** * Ephemeral Table objects are created for all FROM-clause subqueries.
150199** * ON and USING clauses are shifted into WHERE statements
150200** * Wildcards "*" and "TABLE.*" in result sets are expanded.
150201** * Identifiers in expression are matched to tables.
150202**
150203** This routine acts recursively on all subqueries within the SELECT.
150204*/
150205SQLITE_PRIVATEstatic void sqlite3SelectPrep(
150206 Parse *pParse, /* The parser context */
150207 Select *p, /* The SELECT statement being coded. */
150208 NameContext *pOuterNC /* Name context for container */
150209){
150210 assert( p!=0 || pParse->db->mallocFailed )((void) (0));
150211 assert( pParse->db->pParse==pParse )((void) (0));
150212 if( pParse->db->mallocFailed ) return;
150213 if( p->selFlags & SF_HasTypeInfo0x0000080 ) return;
150214 sqlite3SelectExpand(pParse, p);
150215 if( pParse->nErr ) return;
150216 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
150217 if( pParse->nErr ) return;
150218 sqlite3SelectAddTypeInfo(pParse, p);
150219}
150220
150221#if TREETRACE_ENABLED0
150222/*
150223** Display all information about an AggInfo object
150224*/
150225static void printAggInfo(AggInfo *pAggInfo){
150226 int ii;
150227 sqlite3DebugPrintf("AggInfo %d/%p:\n",
150228 pAggInfo->selId, pAggInfo);
150229 for(ii=0; ii<pAggInfo->nColumn; ii++){
150230 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
150231 sqlite3DebugPrintf(
150232 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
150233 " iSorterColumn=%d %s\n",
150234 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
150235 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
150236 pCol->iSorterColumn,
150237 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
150238 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
150239 }
150240 for(ii=0; ii<pAggInfo->nFunc; ii++){
150241 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
150242 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
150243 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
150244 }
150245}
150246#endif /* TREETRACE_ENABLED */
150247
150248/*
150249** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
150250** entries for columns that are arguments to aggregate functions but which
150251** are not otherwise used.
150252**
150253** The aCol[] entries in AggInfo prior to nAccumulator are columns that
150254** are referenced outside of aggregate functions. These might be columns
150255** that are part of the GROUP by clause, for example. Other database engines
150256** would throw an error if there is a column reference that is not in the
150257** GROUP BY clause and that is not part of an aggregate function argument.
150258** But SQLite allows this.
150259**
150260** The aCol[] entries beginning with the aCol[nAccumulator] and following
150261** are column references that are used exclusively as arguments to
150262** aggregate functions. This routine is responsible for computing
150263** (or recomputing) those aCol[] entries.
150264*/
150265static void analyzeAggFuncArgs(
150266 AggInfo *pAggInfo,
150267 NameContext *pNC
150268){
150269 int i;
150270 assert( pAggInfo!=0 )((void) (0));
150271 assert( pAggInfo->iFirstReg==0 )((void) (0));
150272 pNC->ncFlags |= NC_InAggFunc0x020000;
150273 for(i=0; i<pAggInfo->nFunc; i++){
150274 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
150275 assert( pExpr->op==TK_FUNCTION || pExpr->op==TK_AGG_FUNCTION )((void) (0));
150276 assert( ExprUseXList(pExpr) )((void) (0));
150277 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
150278 if( pExpr->pLeft ){
150279 assert( pExpr->pLeft->op==TK_ORDER )((void) (0));
150280 assert( ExprUseXList(pExpr->pLeft) )((void) (0));
150281 sqlite3ExprAnalyzeAggList(pNC, pExpr->pLeft->x.pList);
150282 }
150283#ifndef SQLITE_OMIT_WINDOWFUNC
150284 assert( !IsWindowFunc(pExpr) )((void) (0));
150285 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
150286 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
150287 }
150288#endif
150289 }
150290 pNC->ncFlags &= ~NC_InAggFunc0x020000;
150291}
150292
150293/*
150294** An index on expressions is being used in the inner loop of an
150295** aggregate query with a GROUP BY clause. This routine attempts
150296** to adjust the AggInfo object to take advantage of index and to
150297** perhaps use the index as a covering index.
150298**
150299*/
150300static void optimizeAggregateUseOfIndexedExpr(
150301 Parse *pParse, /* Parsing context */
150302 Select *pSelect, /* The SELECT statement being processed */
150303 AggInfo *pAggInfo, /* The aggregate info */
150304 NameContext *pNC /* Name context used to resolve agg-func args */
150305){
150306 assert( pAggInfo->iFirstReg==0 )((void) (0));
150307 assert( pSelect!=0 )((void) (0));
150308 assert( pSelect->pGroupBy!=0 )((void) (0));
150309 pAggInfo->nColumn = pAggInfo->nAccumulator;
150310 if( ALWAYS(pAggInfo->nSortingColumn>0)(pAggInfo->nSortingColumn>0) ){
150311 int mx = pSelect->pGroupBy->nExpr - 1;
150312 int j, k;
150313 for(j=0; j<pAggInfo->nColumn; j++){
150314 k = pAggInfo->aCol[j].iSorterColumn;
150315 if( k>mx ) mx = k;
150316 }
150317 pAggInfo->nSortingColumn = mx+1;
150318 }
150319 analyzeAggFuncArgs(pAggInfo, pNC);
150320#if TREETRACE_ENABLED0
150321 if( sqlite3TreeTrace & 0x20 ){
150322 IndexedExpr *pIEpr;
150323 TREETRACE(0x20, pParse, pSelect,
150324 ("AggInfo (possibly) adjusted for Indexed Exprs\n"));
150325 sqlite3TreeViewSelect(0, pSelect, 0);
150326 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
150327 printf("data-cursor=%d index={%d,%d}\n",
150328 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
150329 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
150330 }
150331 printAggInfo(pAggInfo);
150332 }
150333#else
150334 UNUSED_PARAMETER(pSelect)(void)(pSelect);
150335 UNUSED_PARAMETER(pParse)(void)(pParse);
150336#endif
150337}
150338
150339/*
150340** Walker callback for aggregateConvertIndexedExprRefToColumn().
150341*/
150342static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){
150343 AggInfo *pAggInfo;
150344 struct AggInfo_col *pCol;
150345 UNUSED_PARAMETER(pWalker)(void)(pWalker);
150346 if( pExpr->pAggInfo==0 ) return WRC_Continue0;
150347 if( pExpr->op==TK_AGG_COLUMN170 ) return WRC_Continue0;
150348 if( pExpr->op==TK_AGG_FUNCTION169 ) return WRC_Continue0;
150349 if( pExpr->op==TK_IF_NULL_ROW179 ) return WRC_Continue0;
150350 pAggInfo = pExpr->pAggInfo;
150351 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn)(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue0;
150352 assert( pExpr->iAgg>=0 )((void) (0));
150353 pCol = &pAggInfo->aCol[pExpr->iAgg];
150354 pExpr->op = TK_AGG_COLUMN170;
150355 pExpr->iTable = pCol->iTable;
150356 pExpr->iColumn = pCol->iColumn;
150357 ExprClearProperty(pExpr, EP_Skip|EP_Collate|EP_Unlikely)(pExpr)->flags&=~(0x002000|0x000200|0x080000);
150358 return WRC_Prune1;
150359}
150360
150361/*
150362** Convert every pAggInfo->aFunc[].pExpr such that any node within
150363** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN
150364** opcode.
150365*/
150366static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){
150367 int i;
150368 Walker w;
150369 memset(&w, 0, sizeof(w));
150370 w.xExprCallback = aggregateIdxEprRefToColCallback;
150371 for(i=0; i<pAggInfo->nFunc; i++){
150372 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
150373 }
150374}
150375
150376
150377/*
150378** Allocate a block of registers so that there is one register for each
150379** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
150380** register in this block is stored in pAggInfo->iFirstReg.
150381**
150382** This routine may only be called once for each AggInfo object. Prior
150383** to calling this routine:
150384**
150385** * The aCol[] and aFunc[] arrays may be modified
150386** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used
150387**
150388** After calling this routine:
150389**
150390** * The aCol[] and aFunc[] arrays are fixed
150391** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used
150392**
150393*/
150394static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){
150395 assert( pAggInfo!=0 )((void) (0));
150396 assert( pAggInfo->iFirstReg==0 )((void) (0));
150397 pAggInfo->iFirstReg = pParse->nMem + 1;
150398 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
150399}
150400
150401/*
150402** Reset the aggregate accumulator.
150403**
150404** The aggregate accumulator is a set of memory cells that hold
150405** intermediate results while calculating an aggregate. This
150406** routine generates code that stores NULLs in all of those memory
150407** cells.
150408*/
150409static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
150410 Vdbe *v = pParse->pVdbe;
150411 int i;
150412 struct AggInfo_func *pFunc;
150413 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
150414 assert( pAggInfo->iFirstReg>0 )((void) (0));
150415 assert( pParse->db->pParse==pParse )((void) (0));
150416 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
150417 if( nReg==0 ) return;
150418 if( pParse->nErr ) return;
150419 sqlite3VdbeAddOp3(v, OP_Null75, 0, pAggInfo->iFirstReg,
150420 pAggInfo->iFirstReg+nReg-1);
150421 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
150422 if( pFunc->iDistinct>=0 ){
150423 Expr *pE = pFunc->pFExpr;
150424 assert( ExprUseXList(pE) )((void) (0));
150425 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
150426 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
150427 "argument");
150428 pFunc->iDistinct = -1;
150429 }else{
150430 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
150431 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
150432 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO(-8));
150433 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)"
, pFunc->pFunc->zName)
150434 pFunc->pFunc->zName))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)"
, pFunc->pFunc->zName)
;
150435 }
150436 }
150437 if( pFunc->iOBTab>=0 ){
150438 ExprList *pOBList;
150439 KeyInfo *pKeyInfo;
150440 int nExtra = 0;
150441 assert( pFunc->pFExpr->pLeft!=0 )((void) (0));
150442 assert( pFunc->pFExpr->pLeft->op==TK_ORDER )((void) (0));
150443 assert( ExprUseXList(pFunc->pFExpr->pLeft) )((void) (0));
150444 assert( pFunc->pFunc!=0 )((void) (0));
150445 pOBList = pFunc->pFExpr->pLeft->x.pList;
150446 if( !pFunc->bOBUnique ){
150447 nExtra++; /* One extra column for the OP_Sequence */
150448 }
150449 if( pFunc->bOBPayload ){
150450 /* extra columns for the function arguments */
150451 assert( ExprUseXList(pFunc->pFExpr) )((void) (0));
150452 nExtra += pFunc->pFExpr->x.pList->nExpr;
150453 }
150454 if( pFunc->bUseSubtype ){
150455 nExtra += pFunc->pFExpr->x.pList->nExpr;
150456 }
150457 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOBList, 0, nExtra);
150458 if( !pFunc->bOBUnique && pParse->nErr==0 ){
150459 pKeyInfo->nKeyField++;
150460 }
150461 sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
150462 pFunc->iOBTab, pOBList->nExpr+nExtra, 0,
150463 (char*)pKeyInfo, P4_KEYINFO(-8));
150464 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)
150465 pFunc->pFunc->zName))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)"
, pFunc->pFunc->zName)
;
150466 }
150467 }
150468}
150469
150470/*
150471** Invoke the OP_AggFinalize opcode for every aggregate function
150472** in the AggInfo structure.
150473*/
150474static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
150475 Vdbe *v = pParse->pVdbe;
150476 int i;
150477 struct AggInfo_func *pF;
150478 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
150479 ExprList *pList;
150480 assert( ExprUseXList(pF->pFExpr) )((void) (0));
150481 if( pParse->nErr ) return;
150482 pList = pF->pFExpr->x.pList;
150483 if( pF->iOBTab>=0 ){
150484 /* For an ORDER BY aggregate, calls to OP_AggStep were deferred. Inputs
150485 ** were stored in emphermal table pF->iOBTab. Here, we extract those
150486 ** inputs (in ORDER BY order) and make all calls to OP_AggStep
150487 ** before doing the OP_AggFinal call. */
150488 int iTop; /* Start of loop for extracting columns */
150489 int nArg; /* Number of columns to extract */
150490 int nKey; /* Key columns to be skipped */
150491 int regAgg; /* Extract into this array */
150492 int j; /* Loop counter */
150493
150494 assert( pF->pFunc!=0 )((void) (0));
150495 nArg = pList->nExpr;
150496 regAgg = sqlite3GetTempRange(pParse, nArg);
150497
150498 if( pF->bOBPayload==0 ){
150499 nKey = 0;
150500 }else{
150501 assert( pF->pFExpr->pLeft!=0 )((void) (0));
150502 assert( ExprUseXList(pF->pFExpr->pLeft) )((void) (0));
150503 assert( pF->pFExpr->pLeft->x.pList!=0 )((void) (0));
150504 nKey = pF->pFExpr->pLeft->x.pList->nExpr;
150505 if( ALWAYS(!pF->bOBUnique)(!pF->bOBUnique) ) nKey++;
150506 }
150507 iTop = sqlite3VdbeAddOp1(v, OP_Rewind36, pF->iOBTab); VdbeCoverage(v);
150508 for(j=nArg-1; j>=0; j--){
150509 sqlite3VdbeAddOp3(v, OP_Column94, pF->iOBTab, nKey+j, regAgg+j);
150510 }
150511 if( pF->bUseSubtype ){
150512 int regSubtype = sqlite3GetTempReg(pParse);
150513 int iBaseCol = nKey + nArg + (pF->bOBPayload==0 && pF->bOBUnique==0);
150514 for(j=nArg-1; j>=0; j--){
150515 sqlite3VdbeAddOp3(v, OP_Column94, pF->iOBTab, iBaseCol+j, regSubtype);
150516 sqlite3VdbeAddOp2(v, OP_SetSubtype182, regSubtype, regAgg+j);
150517 }
150518 sqlite3ReleaseTempReg(pParse, regSubtype);
150519 }
150520 sqlite3VdbeAddOp3(v, OP_AggStep162, 0, regAgg, AggInfoFuncReg(pAggInfo,i)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(i)));
150521 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
150522 sqlite3VdbeChangeP5(v, (u8)nArg);
150523 sqlite3VdbeAddOp2(v, OP_Next39, pF->iOBTab, iTop+1); VdbeCoverage(v);
150524 sqlite3VdbeJumpHere(v, iTop);
150525 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
150526 }
150527 sqlite3VdbeAddOp2(v, OP_AggFinal165, AggInfoFuncReg(pAggInfo,i)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(i)),
150528 pList ? pList->nExpr : 0);
150529 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
150530 }
150531}
150532
150533/*
150534** Generate code that will update the accumulator memory cells for an
150535** aggregate based on the current cursor position.
150536**
150537** If regAcc is non-zero and there are no min() or max() aggregates
150538** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
150539** registers if register regAcc contains 0. The caller will take care
150540** of setting and clearing regAcc.
150541**
150542** For an ORDER BY aggregate, the actual accumulator memory cell update
150543** is deferred until after all input rows have been received, so that they
150544** can be run in the requested order. In that case, instead of invoking
150545** OP_AggStep to update the accumulator, just add the arguments that would
150546** have been passed into OP_AggStep into the sorting ephemeral table
150547** (along with the appropriate sort key).
150548*/
150549static void updateAccumulator(
150550 Parse *pParse,
150551 int regAcc,
150552 AggInfo *pAggInfo,
150553 int eDistinctType
150554){
150555 Vdbe *v = pParse->pVdbe;
150556 int i;
150557 int regHit = 0;
150558 int addrHitTest = 0;
150559 struct AggInfo_func *pF;
150560 struct AggInfo_col *pC;
150561
150562 assert( pAggInfo->iFirstReg>0 )((void) (0));
150563 if( pParse->nErr ) return;
150564 pAggInfo->directMode = 1;
150565 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
150566 int nArg;
150567 int addrNext = 0;
150568 int regAgg;
150569 int regAggSz = 0;
150570 int regDistinct = 0;
150571 ExprList *pList;
150572 assert( ExprUseXList(pF->pFExpr) )((void) (0));
150573 assert( !IsWindowFunc(pF->pFExpr) )((void) (0));
150574 assert( pF->pFunc!=0 )((void) (0));
150575 pList = pF->pFExpr->x.pList;
150576 if( ExprHasProperty(pF->pFExpr, EP_WinFunc)(((pF->pFExpr)->flags&(0x1000000))!=0) ){
150577 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
150578 if( pAggInfo->nAccumulator
150579 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)
150580 && regAcc
150581 ){
150582 /* If regAcc==0, there there exists some min() or max() function
150583 ** without a FILTER clause that will ensure the magnet registers
150584 ** are populated. */
150585 if( regHit==0 ) regHit = ++pParse->nMem;
150586 /* If this is the first row of the group (regAcc contains 0), clear the
150587 ** "magnet" register regHit so that the accumulator registers
150588 ** are populated if the FILTER clause jumps over the the
150589 ** invocation of min() or max() altogether. Or, if this is not
150590 ** the first row (regAcc contains 1), set the magnet register so that
150591 ** the accumulators are not populated unless the min()/max() is invoked
150592 ** and indicates that they should be. */
150593 sqlite3VdbeAddOp2(v, OP_Copy80, regAcc, regHit);
150594 }
150595 addrNext = sqlite3VdbeMakeLabel(pParse);
150596 sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL0x10);
150597 }
150598 if( pF->iOBTab>=0 ){
150599 /* Instead of invoking AggStep, we must push the arguments that would
150600 ** have been passed to AggStep onto the sorting table. */
150601 int jj; /* Registered used so far in building the record */
150602 ExprList *pOBList; /* The ORDER BY clause */
150603 assert( pList!=0 )((void) (0));
150604 nArg = pList->nExpr;
150605 assert( nArg>0 )((void) (0));
150606 assert( pF->pFExpr->pLeft!=0 )((void) (0));
150607 assert( pF->pFExpr->pLeft->op==TK_ORDER )((void) (0));
150608 assert( ExprUseXList(pF->pFExpr->pLeft) )((void) (0));
150609 pOBList = pF->pFExpr->pLeft->x.pList;
150610 assert( pOBList!=0 )((void) (0));
150611 assert( pOBList->nExpr>0 )((void) (0));
150612 regAggSz = pOBList->nExpr;
150613 if( !pF->bOBUnique ){
150614 regAggSz++; /* One register for OP_Sequence */
150615 }
150616 if( pF->bOBPayload ){
150617 regAggSz += nArg;
150618 }
150619 if( pF->bUseSubtype ){
150620 regAggSz += nArg;
150621 }
150622 regAggSz++; /* One extra register to hold result of MakeRecord */
150623 regAgg = sqlite3GetTempRange(pParse, regAggSz);
150624 regDistinct = regAgg;
150625 sqlite3ExprCodeExprList(pParse, pOBList, regAgg, 0, SQLITE_ECEL_DUP0x01);
150626 jj = pOBList->nExpr;
150627 if( !pF->bOBUnique ){
150628 sqlite3VdbeAddOp2(v, OP_Sequence126, pF->iOBTab, regAgg+jj);
150629 jj++;
150630 }
150631 if( pF->bOBPayload ){
150632 regDistinct = regAgg+jj;
150633 sqlite3ExprCodeExprList(pParse, pList, regDistinct, 0, SQLITE_ECEL_DUP0x01);
150634 jj += nArg;
150635 }
150636 if( pF->bUseSubtype ){
150637 int kk;
150638 int regBase = pF->bOBPayload ? regDistinct : regAgg;
150639 for(kk=0; kk<nArg; kk++, jj++){
150640 sqlite3VdbeAddOp2(v, OP_GetSubtype181, regBase+kk, regAgg+jj);
150641 }
150642 }
150643 }else if( pList ){
150644 nArg = pList->nExpr;
150645 regAgg = sqlite3GetTempRange(pParse, nArg);
150646 regDistinct = regAgg;
150647 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP0x01);
150648 }else{
150649 nArg = 0;
150650 regAgg = 0;
150651 }
150652 if( pF->iDistinct>=0 && pList ){
150653 if( addrNext==0 ){
150654 addrNext = sqlite3VdbeMakeLabel(pParse);
150655 }
150656 pF->iDistinct = codeDistinct(pParse, eDistinctType,
150657 pF->iDistinct, addrNext, pList, regDistinct);
150658 }
150659 if( pF->iOBTab>=0 ){
150660 /* Insert a new record into the ORDER BY table */
150661 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regAgg, regAggSz-1,
150662 regAgg+regAggSz-1);
150663 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pF->iOBTab, regAgg+regAggSz-1,
150664 regAgg, regAggSz-1);
150665 sqlite3ReleaseTempRange(pParse, regAgg, regAggSz);
150666 }else{
150667 /* Invoke the AggStep function */
150668 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
150669 CollSeq *pColl = 0;
150670 struct ExprList_item *pItem;
150671 int j;
150672 assert( pList!=0 )((void) (0)); /* pList!=0 if pF->pFunc has NEEDCOLL */
150673 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
150674 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
150675 }
150676 if( !pColl ){
150677 pColl = pParse->db->pDfltColl;
150678 }
150679 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
150680 sqlite3VdbeAddOp4(v, OP_CollSeq85, regHit, 0, 0,
150681 (char *)pColl, P4_COLLSEQ(-2));
150682 }
150683 sqlite3VdbeAddOp3(v, OP_AggStep162, 0, regAgg, AggInfoFuncReg(pAggInfo,i)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(i)));
150684 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-7));
150685 sqlite3VdbeChangeP5(v, (u8)nArg);
150686 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
150687 }
150688 if( addrNext ){
150689 sqlite3VdbeResolveLabel(v, addrNext);
150690 }
150691 if( pParse->nErr ) return;
150692 }
150693 if( regHit==0 && pAggInfo->nAccumulator ){
150694 regHit = regAcc;
150695 }
150696 if( regHit ){
150697 addrHitTest = sqlite3VdbeAddOp1(v, OP_If16, regHit); VdbeCoverage(v);
150698 }
150699 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
150700 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i)((pAggInfo)->iFirstReg+(i)));
150701 if( pParse->nErr ) return;
150702 }
150703
150704 pAggInfo->directMode = 0;
150705 if( addrHitTest ){
150706 sqlite3VdbeJumpHereOrPopInst(v, addrHitTest);
150707 }
150708}
150709
150710/*
150711** Add a single OP_Explain instruction to the VDBE to explain a simple
150712** count(*) query ("SELECT count(*) FROM pTab").
150713*/
150714#ifndef SQLITE_OMIT_EXPLAIN
150715static void explainSimpleCount(
150716 Parse *pParse, /* Parse context */
150717 Table *pTab, /* Table being queried */
150718 Index *pIdx /* Index used to optimize scan, or NULL */
150719){
150720 if( pParse->explain==2 ){
150721 int bCover = (pIdx!=0 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || !IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2)));
150722 sqlite3VdbeExplain(pParse, 0, "SCAN %s%s%s",
150723 pTab->zName,
150724 bCover ? " USING COVERING INDEX " : "",
150725 bCover ? pIdx->zName : ""
150726 );
150727 }
150728}
150729#else
150730# define explainSimpleCount(a,b,c)
150731#endif
150732
150733/*
150734** sqlite3WalkExpr() callback used by havingToWhere().
150735**
150736** If the node passed to the callback is a TK_AND node, return
150737** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
150738**
150739** Otherwise, return WRC_Prune. In this case, also check if the
150740** sub-expression matches the criteria for being moved to the WHERE
150741** clause. If so, add it to the WHERE clause and replace the sub-expression
150742** within the HAVING expression with a constant "1".
150743*/
150744static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
150745 if( pExpr->op!=TK_AND44 ){
150746 Select *pS = pWalker->u.pSelect;
150747 /* This routine is called before the HAVING clause of the current
150748 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
150749 ** here, it indicates that the expression is a correlated reference to a
150750 ** column from an outer aggregate query, or an aggregate function that
150751 ** belongs to an outer query. Do not move the expression to the WHERE
150752 ** clause in this obscure case, as doing so may corrupt the outer Select
150753 ** statements AggInfo structure. */
150754 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
150755 && ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000)==0
150756 && pExpr->pAggInfo==0
150757 ){
150758 sqlite3 *db = pWalker->pParse->db;
150759 Expr *pNew = sqlite3Expr(db, TK_INTEGER156, "1");
150760 if( pNew ){
150761 Expr *pWhere = pS->pWhere;
150762 SWAP(Expr, *pNew, *pExpr){Expr t=*pNew; *pNew=*pExpr; *pExpr=t;};
150763 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
150764 pS->pWhere = pNew;
150765 pWalker->eCode = 1;
150766 }
150767 }
150768 return WRC_Prune1;
150769 }
150770 return WRC_Continue0;
150771}
150772
150773/*
150774** Transfer eligible terms from the HAVING clause of a query, which is
150775** processed after grouping, to the WHERE clause, which is processed before
150776** grouping. For example, the query:
150777**
150778** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
150779**
150780** can be rewritten as:
150781**
150782** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
150783**
150784** A term of the HAVING expression is eligible for transfer if it consists
150785** entirely of constants and expressions that are also GROUP BY terms that
150786** use the "BINARY" collation sequence.
150787*/
150788static void havingToWhere(Parse *pParse, Select *p){
150789 Walker sWalker;
150790 memset(&sWalker, 0, sizeof(sWalker));
150791 sWalker.pParse = pParse;
150792 sWalker.xExprCallback = havingToWhereExprCb;
150793 sWalker.u.pSelect = p;
150794 sqlite3WalkExpr(&sWalker, p->pHaving);
150795#if TREETRACE_ENABLED0
150796 if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){
150797 TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
150798 sqlite3TreeViewSelect(0, p, 0);
150799 }
150800#endif
150801}
150802
150803/*
150804** Check to see if the pThis entry of pTabList is a self-join of another view.
150805** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
150806** but stopping before iEnd.
150807**
150808** If pThis is a self-join, then return the SrcItem for the first other
150809** instance of that view found. If pThis is not a self-join then return 0.
150810*/
150811static SrcItem *isSelfJoinView(
150812 SrcList *pTabList, /* Search for self-joins in this FROM clause */
150813 SrcItem *pThis, /* Search for prior reference to this subquery */
150814 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
150815){
150816 SrcItem *pItem;
150817 Select *pSel;
150818 assert( pThis->fg.isSubquery )((void) (0));
150819 pSel = pThis->u4.pSubq->pSelect;
150820 assert( pSel!=0 )((void) (0));
150821 if( pSel->selFlags & SF_PushDown0x1000000 ) return 0;
150822 while( iFirst<iEnd ){
150823 Select *pS1;
150824 pItem = &pTabList->a[iFirst++];
150825 if( !pItem->fg.isSubquery ) continue;
150826 if( pItem->fg.viaCoroutine ) continue;
150827 if( pItem->zName==0 ) continue;
150828 assert( pItem->pSTab!=0 )((void) (0));
150829 assert( pThis->pSTab!=0 )((void) (0));
150830 if( pItem->pSTab->pSchema!=pThis->pSTab->pSchema ) continue;
150831 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
150832 pS1 = pItem->u4.pSubq->pSelect;
150833 if( pItem->pSTab->pSchema==0 && pSel->selId!=pS1->selId ){
150834 /* The query flattener left two different CTE tables with identical
150835 ** names in the same FROM clause. */
150836 continue;
150837 }
150838 if( pS1->selFlags & SF_PushDown0x1000000 ){
150839 /* The view was modified by some other optimization such as
150840 ** pushDownWhereTerms() */
150841 continue;
150842 }
150843 return pItem;
150844 }
150845 return 0;
150846}
150847
150848/*
150849** Deallocate a single AggInfo object
150850*/
150851static void agginfoFree(sqlite3 *db, void *pArg){
150852 AggInfo *p = (AggInfo*)pArg;
150853 sqlite3DbFree(db, p->aCol);
150854 sqlite3DbFree(db, p->aFunc);
150855 sqlite3DbFreeNN(db, p);
150856}
150857
150858/*
150859** Attempt to transform a query of the form
150860**
150861** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
150862**
150863** Into this:
150864**
150865** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
150866**
150867** The transformation only works if all of the following are true:
150868**
150869** * The subquery is a UNION ALL of two or more terms
150870** * The subquery does not have a LIMIT clause
150871** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
150872** * The outer query is a simple count(*) with no WHERE clause or other
150873** extraneous syntax.
150874**
150875** Return TRUE if the optimization is undertaken.
150876*/
150877static int countOfViewOptimization(Parse *pParse, Select *p){
150878 Select *pSub, *pPrior;
150879 Expr *pExpr;
150880 Expr *pCount;
150881 sqlite3 *db;
150882 SrcItem *pFrom;
150883 if( (p->selFlags & SF_Aggregate0x0000008)==0 ) return 0; /* This is an aggregate */
150884 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
150885 if( p->pWhere ) return 0;
150886 if( p->pHaving ) return 0;
150887 if( p->pGroupBy ) return 0;
150888 if( p->pOrderBy ) return 0;
150889 pExpr = p->pEList->a[0].pExpr;
150890 if( pExpr->op!=TK_AGG_FUNCTION169 ) return 0; /* Result is an aggregate */
150891 assert( ExprUseUToken(pExpr) )((void) (0));
150892 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
150893 assert( ExprUseXList(pExpr) )((void) (0));
150894 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
150895 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
150896 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ) return 0;/* Not a window function */
150897 pFrom = p->pSrc->a;
150898 if( pFrom->fg.isSubquery==0 ) return 0; /* FROM is a subquery */
150899 pSub = pFrom->u4.pSubq->pSelect;
150900 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
150901 if( pSub->selFlags & SF_CopyCte0x4000000 ) return 0; /* Not a CTE */
150902 do{
150903 if( pSub->op!=TK_ALL136 && pSub->pPrior ) return 0; /* Must be UNION ALL */
150904 if( pSub->pWhere ) return 0; /* No WHERE clause */
150905 if( pSub->pLimit ) return 0; /* No LIMIT clause */
150906 if( pSub->selFlags & SF_Aggregate0x0000008 ) return 0; /* Not an aggregate */
150907 assert( pSub->pHaving==0 )((void) (0)); /* Due to the previous */
150908 pSub = pSub->pPrior; /* Repeat over compound */
150909 }while( pSub );
150910
150911 /* If we reach this point then it is OK to perform the transformation */
150912
150913 db = pParse->db;
150914 pCount = pExpr;
150915 pExpr = 0;
150916 pSub = sqlite3SubqueryDetach(db, pFrom);
150917 sqlite3SrcListDelete(db, p->pSrc);
150918 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
150919 while( pSub ){
150920 Expr *pTerm;
150921 pPrior = pSub->pPrior;
150922 pSub->pPrior = 0;
150923 pSub->pNext = 0;
150924 pSub->selFlags |= SF_Aggregate0x0000008;
150925 pSub->selFlags &= ~SF_Compound0x0000100;
150926 pSub->nSelectRow = 0;
150927 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
150928 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
150929 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
150930 pTerm = sqlite3PExpr(pParse, TK_SELECT139, 0, 0);
150931 sqlite3PExprAddSelect(pParse, pTerm, pSub);
150932 if( pExpr==0 ){
150933 pExpr = pTerm;
150934 }else{
150935 pExpr = sqlite3PExpr(pParse, TK_PLUS107, pTerm, pExpr);
150936 }
150937 pSub = pPrior;
150938 }
150939 p->pEList->a[0].pExpr = pExpr;
150940 p->selFlags &= ~SF_Aggregate0x0000008;
150941
150942#if TREETRACE_ENABLED0
150943 if( sqlite3TreeTrace & 0x200 ){
150944 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
150945 sqlite3TreeViewSelect(0, p, 0);
150946 }
150947#endif
150948 return 1;
150949}
150950
150951/*
150952** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
150953** as pSrcItem but has the same alias as p0, then return true.
150954** Otherwise return false.
150955*/
150956static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){
150957 int i;
150958 for(i=0; i<pSrc->nSrc; i++){
150959 SrcItem *p1 = &pSrc->a[i];
150960 if( p1==p0 ) continue;
150961 if( p0->pSTab==p1->pSTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
150962 return 1;
150963 }
150964 if( p1->fg.isSubquery
150965 && (p1->u4.pSubq->pSelect->selFlags & SF_NestedFrom0x0000800)!=0
150966 && sameSrcAlias(p0, p1->u4.pSubq->pSelect->pSrc)
150967 ){
150968 return 1;
150969 }
150970 }
150971 return 0;
150972}
150973
150974/*
150975** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
150976** be implemented as a co-routine. The i-th entry is guaranteed to be
150977** a subquery.
150978**
150979** The subquery is implemented as a co-routine if all of the following are
150980** true:
150981**
150982** (1) The subquery will likely be implemented in the outer loop of
150983** the query. This will be the case if any one of the following
150984** conditions hold:
150985** (a) The subquery is the only term in the FROM clause
150986** (b) The subquery is the left-most term and a CROSS JOIN or similar
150987** requires it to be the outer loop
150988** (c) All of the following are true:
150989** (i) The subquery is the left-most subquery in the FROM clause
150990** (ii) There is nothing that would prevent the subquery from
150991** being used as the outer loop if the sqlite3WhereBegin()
150992** routine nominates it to that position.
150993** (iii) The query is not a UPDATE ... FROM
150994** (2) The subquery is not a CTE that should be materialized because
150995** (a) the AS MATERIALIZED keyword is used, or
150996** (b) the CTE is used multiple times and does not have the
150997** NOT MATERIALIZED keyword
150998** (3) The subquery is not part of a left operand for a RIGHT JOIN
150999** (4) The SQLITE_Coroutine optimization disable flag is not set
151000** (5) The subquery is not self-joined
151001*/
151002static int fromClauseTermCanBeCoroutine(
151003 Parse *pParse, /* Parsing context */
151004 SrcList *pTabList, /* FROM clause */
151005 int i, /* Which term of the FROM clause holds the subquery */
151006 int selFlags /* Flags on the SELECT statement */
151007){
151008 SrcItem *pItem = &pTabList->a[i];
151009 if( pItem->fg.isCte ){
151010 const CteUse *pCteUse = pItem->u2.pCteUse;
151011 if( pCteUse->eM10d==M10d_Yes0 ) return 0; /* (2a) */
151012 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No2 ) return 0; /* (2b) */
151013 }
151014 if( pTabList->a[0].fg.jointype & JT_LTORJ0x40 ) return 0; /* (3) */
151015 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines)(((pParse->db)->dbOptFlags&(0x02000000))!=0) ) return 0; /* (4) */
151016 if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){
151017 return 0; /* (5) */
151018 }
151019 if( i==0 ){
151020 if( pTabList->nSrc==1 ) return 1; /* (1a) */
151021 if( pTabList->a[1].fg.jointype & JT_CROSS0x02 ) return 1; /* (1b) */
151022 if( selFlags & SF_UpdateFrom0x10000000 ) return 0; /* (1c-iii) */
151023 return 1;
151024 }
151025 if( selFlags & SF_UpdateFrom0x10000000 ) return 0; /* (1c-iii) */
151026 while( 1 /*exit-by-break*/ ){
151027 if( pItem->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02) ) return 0; /* (1c-ii) */
151028 if( i==0 ) break;
151029 i--;
151030 pItem--;
151031 if( pItem->fg.isSubquery ) return 0; /* (1c-i) */
151032 }
151033 return 1;
151034}
151035
151036/*
151037** Generate byte-code for the SELECT statement given in the p argument.
151038**
151039** The results are returned according to the SelectDest structure.
151040** See comments in sqliteInt.h for further information.
151041**
151042** This routine returns the number of errors. If any errors are
151043** encountered, then an appropriate error message is left in
151044** pParse->zErrMsg.
151045**
151046** This routine does NOT free the Select structure passed in. The
151047** calling function needs to do that.
151048**
151049** This is a long function. The following is an outline of the processing
151050** steps, with tags referencing various milestones:
151051**
151052** * Resolve names and similar preparation tag-select-0100
151053** * Scan of the FROM clause tag-select-0200
151054** + OUTER JOIN strength reduction tag-select-0220
151055** + Sub-query ORDER BY removal tag-select-0230
151056** + Query flattening tag-select-0240
151057** * Separate subroutine for compound-SELECT tag-select-0300
151058** * WHERE-clause constant propagation tag-select-0330
151059** * Count()-of-VIEW optimization tag-select-0350
151060** * Scan of the FROM clause again tag-select-0400
151061** + Authorize unreferenced tables tag-select-0410
151062** + Predicate push-down optimization tag-select-0420
151063** + Omit unused subquery columns optimization tag-select-0440
151064** + Generate code to implement subqueries tag-select-0480
151065** - Co-routines tag-select-0482
151066** - Reuse previously computed CTE tag-select-0484
151067** - REuse previously computed VIEW tag-select-0486
151068** - Materialize a VIEW or CTE tag-select-0488
151069** * DISTINCT ORDER BY -> GROUP BY optimization tag-select-0500
151070** * Set up for ORDER BY tag-select-0600
151071** * Create output table tag-select-0630
151072** * Prepare registers for LIMIT tag-select-0650
151073** * Setup for DISTINCT tag-select-0680
151074** * Generate code for non-aggregate and non-GROUP BY tag-select-0700
151075** * Generate code for aggregate and/or GROUP BY tag-select-0800
151076** + GROUP BY queries tag-select-0810
151077** + non-GROUP BY queries tag-select-0820
151078** - Special case of count() w/o GROUP BY tag-select-0821
151079** - General case of non-GROUP BY aggregates tag-select-0822
151080** * Sort results, as needed tag-select-0900
151081** * Internal self-checks tag-select-1000
151082*/
151083SQLITE_PRIVATEstatic int sqlite3Select(
151084 Parse *pParse, /* The parser context */
151085 Select *p, /* The SELECT statement being coded. */
151086 SelectDest *pDest /* What to do with the query results */
151087){
151088 int i, j; /* Loop counters */
151089 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
151090 Vdbe *v; /* The virtual machine under construction */
151091 int isAgg; /* True for select lists like "count(*)" */
151092 ExprList *pEList = 0; /* List of columns to extract. */
151093 SrcList *pTabList; /* List of tables to select from */
151094 Expr *pWhere; /* The WHERE clause. May be NULL */
151095 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
151096 Expr *pHaving; /* The HAVING clause. May be NULL */
151097 AggInfo *pAggInfo = 0; /* Aggregate information */
151098 int rc = 1; /* Value to return from this function */
151099 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
151100 SortCtx sSort; /* Info on how to code the ORDER BY clause */
151101 int iEnd; /* Address of the end of the query */
151102 sqlite3 *db; /* The database connection */
151103 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
151104 u8 minMaxFlag; /* Flag for min/max queries */
151105
151106 db = pParse->db;
151107 assert( pParse==db->pParse )((void) (0));
151108 v = sqlite3GetVdbe(pParse);
151109 if( p==0 || pParse->nErr ){
151110 return 1;
151111 }
151112 assert( db->mallocFailed==0 )((void) (0));
151113 if( sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0) ) return 1;
151114#if TREETRACE_ENABLED0
151115 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
151116 if( sqlite3TreeTrace & 0x10000 ){
151117 if( (sqlite3TreeTrace & 0x10001)==0x10000 ){
151118 sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d",
151119 __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__151119);
151120 }
151121 sqlite3ShowSelect(p);
151122 }
151123#endif
151124
151125 /* tag-select-0100 */
151126 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo )((void) (0));
151127 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo )((void) (0));
151128 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue )((void) (0));
151129 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue )((void) (0));
151130 if( IgnorableDistinct(pDest)((pDest->eDest)<=6) ){
151131 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||((void) (0))
151132 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||((void) (0))
151133 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo )((void) (0));
151134 /* All of these destinations are also able to ignore the ORDER BY clause */
151135 if( p->pOrderBy ){
151136#if TREETRACE_ENABLED0
151137 TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n"));
151138 if( sqlite3TreeTrace & 0x800 ){
151139 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
151140 }
151141#endif
151142 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
151143 p->pOrderBy);
151144 testcase( pParse->earlyCleanup );
151145 p->pOrderBy = 0;
151146 }
151147 p->selFlags &= ~SF_Distinct0x0000001;
151148 p->selFlags |= SF_NoopOrderBy0x0400000;
151149 }
151150 sqlite3SelectPrep(pParse, p, 0);
151151 if( pParse->nErr ){
151152 goto select_end;
151153 }
151154 assert( db->mallocFailed==0 )((void) (0));
151155 assert( p->pEList!=0 )((void) (0));
151156#if TREETRACE_ENABLED0
151157 if( sqlite3TreeTrace & 0x10 ){
151158 TREETRACE(0x10,pParse,p, ("after name resolution:\n"));
151159 sqlite3TreeViewSelect(0, p, 0);
151160 }
151161#endif
151162
151163 /* If the SF_UFSrcCheck flag is set, then this function is being called
151164 ** as part of populating the temp table for an UPDATE...FROM statement.
151165 ** In this case, it is an error if the target object (pSrc->a[0]) name
151166 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
151167 **
151168 ** Postgres disallows this case too. The reason is that some other
151169 ** systems handle this case differently, and not all the same way,
151170 ** which is just confusing. To avoid this, we follow PG's lead and
151171 ** disallow it altogether. */
151172 if( p->selFlags & SF_UFSrcCheck0x0800000 ){
151173 SrcItem *p0 = &p->pSrc->a[0];
151174 if( sameSrcAlias(p0, p->pSrc) ){
151175 sqlite3ErrorMsg(pParse,
151176 "target object/alias may not appear in FROM clause: %s",
151177 p0->zAlias ? p0->zAlias : p0->pSTab->zName
151178 );
151179 goto select_end;
151180 }
151181
151182 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
151183 ** and leaving this flag set can cause errors if a compound sub-query
151184 ** in p->pSrc is flattened into this query and this function called
151185 ** again as part of compound SELECT processing. */
151186 p->selFlags &= ~SF_UFSrcCheck0x0800000;
151187 }
151188
151189 if( pDest->eDest==SRT_Output9 ){
151190 sqlite3GenerateColumnNames(pParse, p);
151191 }
151192
151193#ifndef SQLITE_OMIT_WINDOWFUNC
151194 if( sqlite3WindowRewrite(pParse, p) ){
151195 assert( pParse->nErr )((void) (0));
151196 goto select_end;
151197 }
151198#if TREETRACE_ENABLED0
151199 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
151200 TREETRACE(0x40,pParse,p, ("after window rewrite:\n"));
151201 sqlite3TreeViewSelect(0, p, 0);
151202 }
151203#endif
151204#endif /* SQLITE_OMIT_WINDOWFUNC */
151205 pTabList = p->pSrc;
151206 isAgg = (p->selFlags & SF_Aggregate0x0000008)!=0;
151207 memset(&sSort, 0, sizeof(sSort));
151208 sSort.pOrderBy = p->pOrderBy;
151209
151210 /* Try to do various optimizations (flattening subqueries, and strength
151211 ** reduction of join operators) in the FROM clause up into the main query
151212 ** tag-select-0200
151213 */
151214#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
151215 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
151216 SrcItem *pItem = &pTabList->a[i];
151217 Select *pSub = pItem->fg.isSubquery ? pItem->u4.pSubq->pSelect : 0;
151218 Table *pTab = pItem->pSTab;
151219
151220 /* The expander should have already created transient Table objects
151221 ** even for FROM clause elements such as subqueries that do not correspond
151222 ** to a real table */
151223 assert( pTab!=0 )((void) (0));
151224
151225 /* Try to simplify joins:
151226 **
151227 ** LEFT JOIN -> JOIN
151228 ** RIGHT JOIN -> JOIN
151229 ** FULL JOIN -> RIGHT JOIN
151230 **
151231 ** If terms of the i-th table are used in the WHERE clause in such a
151232 ** way that the i-th table cannot be the NULL row of a join, then
151233 ** perform the appropriate simplification. This is called
151234 ** "OUTER JOIN strength reduction" in the SQLite documentation.
151235 ** tag-select-0220
151236 */
151237 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40))!=0
151238 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor,
151239 pItem->fg.jointype & JT_LTORJ0x40)
151240 && OptimizationEnabled(db, SQLITE_SimplifyJoin)(((db)->dbOptFlags&(0x00002000))==0)
151241 ){
151242 if( pItem->fg.jointype & JT_LEFT0x08 ){
151243 if( pItem->fg.jointype & JT_RIGHT0x10 ){
151244 TREETRACE(0x1000,pParse,p,
151245 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i));
151246 pItem->fg.jointype &= ~JT_LEFT0x08;
151247 }else{
151248 TREETRACE(0x1000,pParse,p,
151249 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
151250 pItem->fg.jointype &= ~(JT_LEFT0x08|JT_OUTER0x20);
151251 unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
151252 }
151253 }
151254 if( pItem->fg.jointype & JT_LTORJ0x40 ){
151255 for(j=i+1; j<pTabList->nSrc; j++){
151256 SrcItem *pI2 = &pTabList->a[j];
151257 if( pI2->fg.jointype & JT_RIGHT0x10 ){
151258 if( pI2->fg.jointype & JT_LEFT0x08 ){
151259 TREETRACE(0x1000,pParse,p,
151260 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j));
151261 pI2->fg.jointype &= ~JT_RIGHT0x10;
151262 }else{
151263 TREETRACE(0x1000,pParse,p,
151264 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
151265 pI2->fg.jointype &= ~(JT_RIGHT0x10|JT_OUTER0x20);
151266 unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
151267 }
151268 }
151269 }
151270 for(j=pTabList->nSrc-1; j>=0; j--){
151271 pTabList->a[j].fg.jointype &= ~JT_LTORJ0x40;
151272 if( pTabList->a[j].fg.jointype & JT_RIGHT0x10 ) break;
151273 }
151274 }
151275 }
151276
151277 /* No further action if this term of the FROM clause is not a subquery */
151278 if( pSub==0 ) continue;
151279
151280 /* Catch mismatch in the declared columns of a view and the number of
151281 ** columns in the SELECT on the RHS */
151282 if( pTab->nCol!=pSub->pEList->nExpr ){
151283 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
151284 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
151285 goto select_end;
151286 }
151287
151288 /* Do not attempt the usual optimizations (flattening and ORDER BY
151289 ** elimination) on a MATERIALIZED common table expression because
151290 ** a MATERIALIZED common table expression is an optimization fence.
151291 */
151292 if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes0 ){
151293 continue;
151294 }
151295
151296 /* Do not try to flatten an aggregate subquery.
151297 **
151298 ** Flattening an aggregate subquery is only possible if the outer query
151299 ** is not a join. But if the outer query is not a join, then the subquery
151300 ** will be implemented as a co-routine and there is no advantage to
151301 ** flattening in that case.
151302 */
151303 if( (pSub->selFlags & SF_Aggregate0x0000008)!=0 ) continue;
151304 assert( pSub->pGroupBy==0 )((void) (0));
151305
151306 /* tag-select-0230:
151307 ** If a FROM-clause subquery has an ORDER BY clause that is not
151308 ** really doing anything, then delete it now so that it does not
151309 ** interfere with query flattening. See the discussion at
151310 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
151311 **
151312 ** Beware of these cases where the ORDER BY clause may not be safely
151313 ** omitted:
151314 **
151315 ** (1) There is also a LIMIT clause
151316 ** (2) The subquery was added to help with window-function
151317 ** processing
151318 ** (3) The subquery is in the FROM clause of an UPDATE
151319 ** (4) The outer query uses an aggregate function other than
151320 ** the built-in count(), min(), or max().
151321 ** (5) The ORDER BY isn't going to accomplish anything because
151322 ** one of:
151323 ** (a) The outer query has a different ORDER BY clause
151324 ** (b) The subquery is part of a join
151325 ** See forum post 062d576715d277c8
151326 ** (6) The subquery is not a recursive CTE. ORDER BY has a different
151327 ** meaning for recursive CTEs and this optimization does not
151328 ** apply.
151329 **
151330 ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled.
151331 */
151332 if( pSub->pOrderBy!=0
151333 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
151334 && pSub->pLimit==0 /* Condition (1) */
151335 && (pSub->selFlags & (SF_OrderByReqd0x8000000|SF_Recursive0x0002000))==0 /* (2) and (6) */
151336 && (p->selFlags & SF_OrderByReqd0x8000000)==0 /* Condition (3) and (4) */
151337 && OptimizationEnabled(db, SQLITE_OmitOrderBy)(((db)->dbOptFlags&(0x00040000))==0)
151338 ){
151339 TREETRACE(0x800,pParse,p,
151340 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
151341 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
151342 pSub->pOrderBy);
151343 pSub->pOrderBy = 0;
151344 }
151345
151346 /* If the outer query contains a "complex" result set (that is,
151347 ** if the result set of the outer query uses functions or subqueries)
151348 ** and if the subquery contains an ORDER BY clause and if
151349 ** it will be implemented as a co-routine, then do not flatten. This
151350 ** restriction allows SQL constructs like this:
151351 **
151352 ** SELECT expensive_function(x)
151353 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
151354 **
151355 ** The expensive_function() is only computed on the 10 rows that
151356 ** are output, rather than every row of the table.
151357 **
151358 ** The requirement that the outer query have a complex result set
151359 ** means that flattening does occur on simpler SQL constraints without
151360 ** the expensive_function() like:
151361 **
151362 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
151363 */
151364 if( pSub->pOrderBy!=0
151365 && i==0
151366 && (p->selFlags & SF_ComplexResult0x0040000)!=0
151367 && (pTabList->nSrc==1
151368 || (pTabList->a[1].fg.jointype&(JT_OUTER0x20|JT_CROSS0x02))!=0)
151369 ){
151370 continue;
151371 }
151372
151373 /* tag-select-0240 */
151374 if( flattenSubquery(pParse, p, i, isAgg) ){
151375 if( pParse->nErr ) goto select_end;
151376 /* This subquery can be absorbed into its parent. */
151377 i = -1;
151378 }
151379 pTabList = p->pSrc;
151380 if( db->mallocFailed ) goto select_end;
151381 if( !IgnorableOrderby(pDest)((pDest->eDest)<=8) ){
151382 sSort.pOrderBy = p->pOrderBy;
151383 }
151384 }
151385#endif
151386
151387#ifndef SQLITE_OMIT_COMPOUND_SELECT
151388 /* Handle compound SELECT statements using the separate multiSelect()
151389 ** procedure. tag-select-0300
151390 */
151391 if( p->pPrior ){
151392 rc = multiSelect(pParse, p, pDest);
151393#if TREETRACE_ENABLED0
151394 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
151395 if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
151396 sqlite3TreeViewSelect(0, p, 0);
151397 }
151398#endif
151399 if( p->pNext==0 ) ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
151400 return rc;
151401 }
151402#endif
151403
151404 /* Do the WHERE-clause constant propagation optimization if this is
151405 ** a join. No need to spend time on this operation for non-join queries
151406 ** as the equivalent optimization will be handled by query planner in
151407 ** sqlite3WhereBegin(). tag-select-0330
151408 */
151409 if( p->pWhere!=0
151410 && p->pWhere->op==TK_AND44
151411 && OptimizationEnabled(db, SQLITE_PropagateConst)(((db)->dbOptFlags&(0x00008000))==0)
151412 && propagateConstants(pParse, p)
151413 ){
151414#if TREETRACE_ENABLED0
151415 if( sqlite3TreeTrace & 0x2000 ){
151416 TREETRACE(0x2000,pParse,p,("After constant propagation:\n"));
151417 sqlite3TreeViewSelect(0, p, 0);
151418 }
151419#endif
151420 }else{
151421 TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n"));
151422 }
151423
151424 /* tag-select-0350 */
151425 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)(((db)->dbOptFlags&(0x00000001|0x00000200))==0)
151426 && countOfViewOptimization(pParse, p)
151427 ){
151428 if( db->mallocFailed ) goto select_end;
151429 pTabList = p->pSrc;
151430 }
151431
151432 /* Loop over all terms in the FROM clause and do two things for each term:
151433 **
151434 ** (1) Authorize unreferenced tables
151435 ** (2) Generate code for all sub-queries
151436 **
151437 ** tag-select-0400
151438 */
151439 for(i=0; i<pTabList->nSrc; i++){
151440 SrcItem *pItem = &pTabList->a[i];
151441 SrcItem *pPrior;
151442 SelectDest dest;
151443 Subquery *pSubq;
151444 Select *pSub;
151445#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
151446 const char *zSavedAuthContext;
151447#endif
151448
151449 /* Authorized unreferenced tables. tag-select-0410
151450 **
151451 ** Issue SQLITE_READ authorizations with a fake column name for any
151452 ** tables that are referenced but from which no values are extracted.
151453 ** Examples of where these kinds of null SQLITE_READ authorizations
151454 ** would occur:
151455 **
151456 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
151457 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
151458 **
151459 ** The fake column name is an empty string. It is possible for a table to
151460 ** have a column named by the empty string, in which case there is no way to
151461 ** distinguish between an unreferenced table and an actual reference to the
151462 ** "" column. The original design was for the fake column name to be a NULL,
151463 ** which would be unambiguous. But legacy authorization callbacks might
151464 ** assume the column name is non-NULL and segfault. The use of an empty
151465 ** string for the fake column name seems safer.
151466 */
151467 if( pItem->colUsed==0 && pItem->zName!=0 ){
151468 const char *zDb;
151469 if( pItem->fg.fixedSchema ){
151470 int iDb = sqlite3SchemaToIndex(pParse->db, pItem->u4.pSchema);
151471 zDb = db->aDb[iDb].zDbSName;
151472 }else if( pItem->fg.isSubquery ){
151473 zDb = 0;
151474 }else{
151475 zDb = pItem->u4.zDatabase;
151476 }
151477 sqlite3AuthCheck(pParse, SQLITE_READ20, pItem->zName, "", zDb);
151478 }
151479
151480#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
151481 /* Generate code for all sub-queries in the FROM clause
151482 */
151483 if( pItem->fg.isSubquery==0 ) continue;
151484 pSubq = pItem->u4.pSubq;
151485 assert( pSubq!=0 )((void) (0));
151486 pSub = pSubq->pSelect;
151487
151488 /* The code for a subquery should only be generated once. */
151489 if( pSubq->addrFillSub!=0 ) continue;
151490
151491 /* Increment Parse.nHeight by the height of the largest expression
151492 ** tree referred to by this, the parent select. The child select
151493 ** may contain expression trees of at most
151494 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
151495 ** more conservative than necessary, but much easier than enforcing
151496 ** an exact limit.
151497 */
151498 pParse->nHeight += sqlite3SelectExprHeight(p);
151499
151500 /* Make copies of constant WHERE-clause terms in the outer query down
151501 ** inside the subquery. This can help the subquery to run more efficiently.
151502 ** This is the "predicate push-down optimization". tag-select-0420
151503 */
151504 if( OptimizationEnabled(db, SQLITE_PushDown)(((db)->dbOptFlags&(0x00001000))==0)
151505 && (pItem->fg.isCte==0
151506 || (pItem->u2.pCteUse->eM10d!=M10d_Yes0 && pItem->u2.pCteUse->nUse<2))
151507 && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i)
151508 ){
151509#if TREETRACE_ENABLED0
151510 if( sqlite3TreeTrace & 0x4000 ){
151511 TREETRACE(0x4000,pParse,p,
151512 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
151513 sqlite3TreeViewSelect(0, p, 0);
151514 }
151515#endif
151516 assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 )((void) (0));
151517 }else{
151518 TREETRACE(0x4000,pParse,p,("WHERE-lcause push-down not possible\n"));
151519 }
151520
151521 /* Convert unused result columns of the subquery into simple NULL
151522 ** expressions, to avoid unneeded searching and computation.
151523 ** tag-select-0440
151524 */
151525 if( OptimizationEnabled(db, SQLITE_NullUnusedCols)(((db)->dbOptFlags&(0x04000000))==0)
151526 && disableUnusedSubqueryResultColumns(pItem)
151527 ){
151528#if TREETRACE_ENABLED0
151529 if( sqlite3TreeTrace & 0x4000 ){
151530 TREETRACE(0x4000,pParse,p,
151531 ("Change unused result columns to NULL for subquery %d:\n",
151532 pSub->selId));
151533 sqlite3TreeViewSelect(0, p, 0);
151534 }
151535#endif
151536 }
151537
151538 zSavedAuthContext = pParse->zAuthContext;
151539 pParse->zAuthContext = pItem->zName;
151540
151541 /* Generate byte-code to implement the subquery tag-select-0480
151542 */
151543 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){
151544 /* Implement a co-routine that will return a single row of the result
151545 ** set on each invocation. tag-select-0482
151546 */
151547 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
151548
151549 pSubq->regReturn = ++pParse->nMem;
151550 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, pSubq->regReturn, 0, addrTop);
151551 VdbeComment((v, "%!S", pItem));
151552 pSubq->addrFillSub = addrTop;
151553 sqlite3SelectDestInit(&dest, SRT_Coroutine13, pSubq->regReturn);
151554 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem))sqlite3VdbeExplain (pParse, 1, "CO-ROUTINE %!S", pItem);
151555 sqlite3Select(pParse, pSub, &dest);
151556 pItem->pSTab->nRowLogEst = pSub->nSelectRow;
151557 pItem->fg.viaCoroutine = 1;
151558 pSubq->regResult = dest.iSdst;
151559 sqlite3VdbeEndCoroutine(v, pSubq->regReturn);
151560 VdbeComment((v, "end %!S", pItem));
151561 sqlite3VdbeJumpHere(v, addrTop-1);
151562 sqlite3ClearTempRegCache(pParse);
151563 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
151564 /* This is a CTE for which materialization code has already been
151565 ** generated. Invoke the subroutine to compute the materialization,
151566 ** then make the pItem->iCursor be a copy of the ephemeral table that
151567 ** holds the result of the materialization. tag-select-0484 */
151568 CteUse *pCteUse = pItem->u2.pCteUse;
151569 sqlite3VdbeAddOp2(v, OP_Gosub10, pCteUse->regRtn, pCteUse->addrM9e);
151570 if( pItem->iCursor!=pCteUse->iCur ){
151571 sqlite3VdbeAddOp2(v, OP_OpenDup114, pItem->iCursor, pCteUse->iCur);
151572 VdbeComment((v, "%!S", pItem));
151573 }
151574 pSub->nSelectRow = pCteUse->nRowEst;
151575 }else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){
151576 /* This view has already been materialized by a prior entry in
151577 ** this same FROM clause. Reuse it. tag-select-0486 */
151578 Subquery *pPriorSubq;
151579 assert( pPrior->fg.isSubquery )((void) (0));
151580 pPriorSubq = pPrior->u4.pSubq;
151581 assert( pPriorSubq!=0 )((void) (0));
151582 if( pPriorSubq->addrFillSub ){
151583 sqlite3VdbeAddOp2(v, OP_Gosub10, pPriorSubq->regReturn,
151584 pPriorSubq->addrFillSub);
151585 }
151586 sqlite3VdbeAddOp2(v, OP_OpenDup114, pItem->iCursor, pPrior->iCursor);
151587 pSub->nSelectRow = pPriorSubq->pSelect->nSelectRow;
151588 }else{
151589 /* Materialize the view. If the view is not correlated, generate a
151590 ** subroutine to do the materialization so that subsequent uses of
151591 ** the same view can reuse the materialization. tag-select-0488 */
151592 int topAddr;
151593 int onceAddr = 0;
151594#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
151595 int addrExplain;
151596#endif
151597
151598 pSubq->regReturn = ++pParse->nMem;
151599 topAddr = sqlite3VdbeAddOp0(v, OP_Goto9);
151600 pSubq->addrFillSub = topAddr+1;
151601 pItem->fg.isMaterialized = 1;
151602 if( pItem->fg.isCorrelated==0 ){
151603 /* If the subquery is not correlated and if we are not inside of
151604 ** a trigger, then we only need to compute the value of the subquery
151605 ** once. */
151606 onceAddr = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
151607 VdbeComment((v, "materialize %!S", pItem));
151608 }else{
151609 VdbeNoopComment((v, "materialize %!S", pItem));
151610 }
151611 sqlite3SelectDestInit(&dest, SRT_EphemTab12, pItem->iCursor);
151612
151613 ExplainQueryPlan2(addrExplain, (pParse, 1, "MATERIALIZE %!S", pItem))(addrExplain = sqlite3VdbeExplain (pParse, 1, "MATERIALIZE %!S"
, pItem))
;
151614 sqlite3Select(pParse, pSub, &dest);
151615 pItem->pSTab->nRowLogEst = pSub->nSelectRow;
151616 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
151617 sqlite3VdbeAddOp2(v, OP_Return67, pSubq->regReturn, topAddr+1);
151618 VdbeComment((v, "end %!S", pItem));
151619 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
151620 sqlite3VdbeJumpHere(v, topAddr);
151621 sqlite3ClearTempRegCache(pParse);
151622 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
151623 CteUse *pCteUse = pItem->u2.pCteUse;
151624 pCteUse->addrM9e = pSubq->addrFillSub;
151625 pCteUse->regRtn = pSubq->regReturn;
151626 pCteUse->iCur = pItem->iCursor;
151627 pCteUse->nRowEst = pSub->nSelectRow;
151628 }
151629 }
151630 if( db->mallocFailed ) goto select_end;
151631 pParse->nHeight -= sqlite3SelectExprHeight(p);
151632 pParse->zAuthContext = zSavedAuthContext;
151633#endif
151634 }
151635
151636 /* Various elements of the SELECT copied into local variables for
151637 ** convenience */
151638 pEList = p->pEList;
151639 pWhere = p->pWhere;
151640 pGroupBy = p->pGroupBy;
151641 pHaving = p->pHaving;
151642 sDistinct.isTnct = (p->selFlags & SF_Distinct0x0000001)!=0;
151643
151644#if TREETRACE_ENABLED0
151645 if( sqlite3TreeTrace & 0x8000 ){
151646 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
151647 sqlite3TreeViewSelect(0, p, 0);
151648 }
151649#endif
151650
151651 /* tag-select-0500
151652 **
151653 ** If the query is DISTINCT with an ORDER BY but is not an aggregate, and
151654 ** if the select-list is the same as the ORDER BY list, then this query
151655 ** can be rewritten as a GROUP BY. In other words, this:
151656 **
151657 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
151658 **
151659 ** is transformed to:
151660 **
151661 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
151662 **
151663 ** The second form is preferred as a single index (or temp-table) may be
151664 ** used for both the ORDER BY and DISTINCT processing. As originally
151665 ** written the query must use a temp-table for at least one of the ORDER
151666 ** BY and DISTINCT, and an index or separate temp-table for the other.
151667 */
151668 if( (p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))==SF_Distinct0x0000001
151669 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
151670 && OptimizationEnabled(db, SQLITE_GroupByOrder)(((db)->dbOptFlags&(0x00000004))==0)
151671#ifndef SQLITE_OMIT_WINDOWFUNC
151672 && p->pWin==0
151673#endif
151674 ){
151675 p->selFlags &= ~SF_Distinct0x0000001;
151676 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
151677 if( pGroupBy ){
151678 for(i=0; i<pGroupBy->nExpr; i++){
151679 pGroupBy->a[i].u.x.iOrderByCol = i+1;
151680 }
151681 }
151682 p->selFlags |= SF_Aggregate0x0000008;
151683 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
151684 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
151685 ** original setting of the SF_Distinct flag, not the current setting */
151686 assert( sDistinct.isTnct )((void) (0));
151687 sDistinct.isTnct = 2;
151688
151689#if TREETRACE_ENABLED0
151690 if( sqlite3TreeTrace & 0x20000 ){
151691 TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
151692 sqlite3TreeViewSelect(0, p, 0);
151693 }
151694#endif
151695 }
151696
151697 /* If there is an ORDER BY clause, then create an ephemeral index to
151698 ** do the sorting. But this sorting ephemeral index might end up
151699 ** being unused if the data can be extracted in pre-sorted order.
151700 ** If that is the case, then the OP_OpenEphemeral instruction will be
151701 ** changed to an OP_Noop once we figure out that the sorting index is
151702 ** not needed. The sSort.addrSortIndex variable is used to facilitate
151703 ** that change. tag-select-0600
151704 */
151705 if( sSort.pOrderBy ){
151706 KeyInfo *pKeyInfo;
151707 pKeyInfo = sqlite3KeyInfoFromExprList(
151708 pParse, sSort.pOrderBy, 0, pEList->nExpr);
151709 sSort.iECursor = pParse->nTab++;
151710 sSort.addrSortIndex =
151711 sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
151712 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
151713 (char*)pKeyInfo, P4_KEYINFO(-8)
151714 );
151715 }else{
151716 sSort.addrSortIndex = -1;
151717 }
151718
151719 /* If the output is destined for a temporary table, open that table.
151720 ** tag-select-0630
151721 */
151722 if( pDest->eDest==SRT_EphemTab12 ){
151723 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pDest->iSDParm, pEList->nExpr);
151724 if( p->selFlags & SF_NestedFrom0x0000800 ){
151725 /* Delete or NULL-out result columns that will never be used */
151726 int ii;
151727 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
151728 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
151729 sqlite3DbFree(db, pEList->a[ii].zEName);
151730 pEList->nExpr--;
151731 }
151732 for(ii=0; ii<pEList->nExpr; ii++){
151733 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL122;
151734 }
151735 }
151736 }
151737
151738 /* Set the limiter. tag-select-0650
151739 */
151740 iEnd = sqlite3VdbeMakeLabel(pParse);
151741 if( (p->selFlags & SF_FixedLimit0x0004000)==0 ){
151742 p->nSelectRow = 320; /* 4 billion rows */
151743 }
151744 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
151745 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
151746 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen119);
151747 sSort.sortFlags |= SORTFLAG_UseSorter0x01;
151748 }
151749
151750 /* Open an ephemeral index to use for the distinct set. tag-select-0680
151751 */
151752 if( p->selFlags & SF_Distinct0x0000001 ){
151753 sDistinct.tabTnct = pParse->nTab++;
151754 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral117,
151755 sDistinct.tabTnct, 0, 0,
151756 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
151757 P4_KEYINFO(-8));
151758 sqlite3VdbeChangeP5(v, BTREE_UNORDERED8);
151759 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED3;
151760 }else{
151761 sDistinct.eTnctType = WHERE_DISTINCT_NOOP0;
151762 }
151763
151764 if( !isAgg && pGroupBy==0 ){
151765 /* No aggregate functions and no GROUP BY clause. tag-select-0700 */
151766 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT0x0100 : 0)
151767 | (p->selFlags & SF_FixedLimit0x0004000);
151768#ifndef SQLITE_OMIT_WINDOWFUNC
151769 Window *pWin = p->pWin; /* Main window object (or NULL) */
151770 if( pWin ){
151771 sqlite3WindowCodeInit(pParse, p);
151772 }
151773#endif
151774 assert( WHERE_USE_LIMIT==SF_FixedLimit )((void) (0));
151775
151776
151777 /* Begin the database scan. */
151778 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
151779 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
151780 p->pEList, p, wctrlFlags, p->nSelectRow);
151781 if( pWInfo==0 ) goto select_end;
151782 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
151783 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
151784 }
151785 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
151786 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
151787 }
151788 if( sSort.pOrderBy ){
151789 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
151790 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
151791 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
151792 sSort.pOrderBy = 0;
151793 }
151794 }
151795 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
151796
151797 /* If sorting index that was created by a prior OP_OpenEphemeral
151798 ** instruction ended up not being needed, then change the OP_OpenEphemeral
151799 ** into an OP_Noop.
151800 */
151801 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
151802 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
151803 }
151804
151805 assert( p->pEList==pEList )((void) (0));
151806#ifndef SQLITE_OMIT_WINDOWFUNC
151807 if( pWin ){
151808 int addrGosub = sqlite3VdbeMakeLabel(pParse);
151809 int iCont = sqlite3VdbeMakeLabel(pParse);
151810 int iBreak = sqlite3VdbeMakeLabel(pParse);
151811 int regGosub = ++pParse->nMem;
151812
151813 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
151814
151815 sqlite3VdbeAddOp2(v, OP_Goto9, 0, iBreak);
151816 sqlite3VdbeResolveLabel(v, addrGosub);
151817 VdbeNoopComment((v, "inner-loop subroutine"));
151818 sSort.labelOBLopt = 0;
151819 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
151820 sqlite3VdbeResolveLabel(v, iCont);
151821 sqlite3VdbeAddOp1(v, OP_Return67, regGosub);
151822 VdbeComment((v, "end inner-loop subroutine"));
151823 sqlite3VdbeResolveLabel(v, iBreak);
151824 }else
151825#endif /* SQLITE_OMIT_WINDOWFUNC */
151826 {
151827 /* Use the standard inner loop. */
151828 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
151829 sqlite3WhereContinueLabel(pWInfo),
151830 sqlite3WhereBreakLabel(pWInfo));
151831
151832 /* End the database scan loop.
151833 */
151834 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
151835 sqlite3WhereEnd(pWInfo);
151836 }
151837 }else{
151838 /* This case is for when there exist aggregate functions or a GROUP BY
151839 ** clause or both. tag-select-0800 */
151840 NameContext sNC; /* Name context for processing aggregate information */
151841 int iAMem; /* First Mem address for storing current GROUP BY */
151842 int iBMem; /* First Mem address for previous GROUP BY */
151843 int iUseFlag; /* Mem address holding flag indicating that at least
151844 ** one row of the input to the aggregator has been
151845 ** processed */
151846 int iAbortFlag; /* Mem address which causes query abort if positive */
151847 int groupBySort; /* Rows come from source in GROUP BY order */
151848 int addrEnd; /* End of processing for this SELECT */
151849 int sortPTab = 0; /* Pseudotable used to decode sorting results */
151850 int sortOut = 0; /* Output register from the sorter */
151851 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
151852
151853 /* Remove any and all aliases between the result set and the
151854 ** GROUP BY clause.
151855 */
151856 if( pGroupBy ){
151857 int k; /* Loop counter */
151858 struct ExprList_item *pItem; /* For looping over expression in a list */
151859
151860 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
151861 pItem->u.x.iAlias = 0;
151862 }
151863 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
151864 pItem->u.x.iAlias = 0;
151865 }
151866 assert( 66==sqlite3LogEst(100) )((void) (0));
151867 if( p->nSelectRow>66 ) p->nSelectRow = 66;
151868
151869 /* If there is both a GROUP BY and an ORDER BY clause and they are
151870 ** identical, then it may be possible to disable the ORDER BY clause
151871 ** on the grounds that the GROUP BY will cause elements to come out
151872 ** in the correct order. It also may not - the GROUP BY might use a
151873 ** database index that causes rows to be grouped together as required
151874 ** but not actually sorted. Either way, record the fact that the
151875 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
151876 ** variable. */
151877 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
151878 int ii;
151879 /* The GROUP BY processing doesn't care whether rows are delivered in
151880 ** ASC or DESC order - only that each group is returned contiguously.
151881 ** So set the ASC/DESC flags in the GROUP BY to match those in the
151882 ** ORDER BY to maximize the chances of rows being delivered in an
151883 ** order that makes the ORDER BY redundant. */
151884 for(ii=0; ii<pGroupBy->nExpr; ii++){
151885 u8 sortFlags;
151886 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC0x01;
151887 pGroupBy->a[ii].fg.sortFlags = sortFlags;
151888 }
151889 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
151890 orderByGrp = 1;
151891 }
151892 }
151893 }else{
151894 assert( 0==sqlite3LogEst(1) )((void) (0));
151895 p->nSelectRow = 0;
151896 }
151897
151898 /* Create a label to jump to when we want to abort the query */
151899 addrEnd = sqlite3VdbeMakeLabel(pParse);
151900
151901 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
151902 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
151903 ** SELECT statement.
151904 */
151905 pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) );
151906 if( pAggInfo ){
151907 sqlite3ParserAddCleanup(pParse, agginfoFree, pAggInfo);
151908 testcase( pParse->earlyCleanup );
151909 }
151910 if( db->mallocFailed ){
151911 goto select_end;
151912 }
151913 pAggInfo->selId = p->selId;
151914#ifdef SQLITE_DEBUG
151915 pAggInfo->pSelect = p;
151916#endif
151917 memset(&sNC, 0, sizeof(sNC));
151918 sNC.pParse = pParse;
151919 sNC.pSrcList = pTabList;
151920 sNC.uNC.pAggInfo = pAggInfo;
151921 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
151922 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
151923 pAggInfo->pGroupBy = pGroupBy;
151924 sqlite3ExprAnalyzeAggList(&sNC, pEList);
151925 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
151926 if( pHaving ){
151927 if( pGroupBy ){
151928 assert( pWhere==p->pWhere )((void) (0));
151929 assert( pHaving==p->pHaving )((void) (0));
151930 assert( pGroupBy==p->pGroupBy )((void) (0));
151931 havingToWhere(pParse, p);
151932 pWhere = p->pWhere;
151933 }
151934 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
151935 }
151936 pAggInfo->nAccumulator = pAggInfo->nColumn;
151937 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
151938 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
151939 }else{
151940 minMaxFlag = WHERE_ORDERBY_NORMAL0x0000;
151941 }
151942 analyzeAggFuncArgs(pAggInfo, &sNC);
151943 if( db->mallocFailed ) goto select_end;
151944#if TREETRACE_ENABLED0
151945 if( sqlite3TreeTrace & 0x20 ){
151946 TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo));
151947 sqlite3TreeViewSelect(0, p, 0);
151948 if( minMaxFlag ){
151949 sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag);
151950 sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY");
151951 }
151952 printAggInfo(pAggInfo);
151953 }
151954#endif
151955
151956
151957 /* Processing for aggregates with GROUP BY is very different and
151958 ** much more complex than aggregates without a GROUP BY. tag-select-0810
151959 */
151960 if( pGroupBy ){
151961 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
151962 int addr1; /* A-vs-B comparison jump */
151963 int addrOutputRow; /* Start of subroutine that outputs a result row */
151964 int regOutputRow; /* Return address register for output subroutine */
151965 int addrSetAbort; /* Set the abort flag and return */
151966 int addrTopOfLoop; /* Top of the input loop */
151967 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
151968 int addrReset; /* Subroutine for resetting the accumulator */
151969 int regReset; /* Return address register for reset subroutine */
151970 ExprList *pDistinct = 0;
151971 u16 distFlag = 0;
151972 int eDist = WHERE_DISTINCT_NOOP0;
151973
151974 if( pAggInfo->nFunc==1
151975 && pAggInfo->aFunc[0].iDistinct>=0
151976 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)(pAggInfo->aFunc[0].pFExpr!=0)
151977 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))((((pAggInfo->aFunc[0].pFExpr)->flags&0x001000)==0)
)
151978 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
151979 ){
151980 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
151981 pExpr = sqlite3ExprDup(db, pExpr, 0);
151982 pDistinct = sqlite3ExprListDup(db, pGroupBy, 0);
151983 pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr);
151984 distFlag = pDistinct ? (WHERE_WANT_DISTINCT0x0100|WHERE_AGG_DISTINCT0x0400) : 0;
151985 }
151986
151987 /* If there is a GROUP BY clause we might need a sorting index to
151988 ** implement it. Allocate that sorting index now. If it turns out
151989 ** that we do not need it after all, the OP_SorterOpen instruction
151990 ** will be converted into a Noop.
151991 */
151992 pAggInfo->sortingIdx = pParse->nTab++;
151993 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy,
151994 0, pAggInfo->nColumn);
151995 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen119,
151996 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
151997 0, (char*)pKeyInfo, P4_KEYINFO(-8));
151998
151999 /* Initialize memory locations used by GROUP BY aggregate processing
152000 */
152001 iUseFlag = ++pParse->nMem;
152002 iAbortFlag = ++pParse->nMem;
152003 regOutputRow = ++pParse->nMem;
152004 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
152005 regReset = ++pParse->nMem;
152006 addrReset = sqlite3VdbeMakeLabel(pParse);
152007 iAMem = pParse->nMem + 1;
152008 pParse->nMem += pGroupBy->nExpr;
152009 iBMem = pParse->nMem + 1;
152010 pParse->nMem += pGroupBy->nExpr;
152011 sqlite3VdbeAddOp2(v, OP_Integer71, 0, iAbortFlag);
152012 VdbeComment((v, "clear abort flag"));
152013 sqlite3VdbeAddOp3(v, OP_Null75, 0, iAMem, iAMem+pGroupBy->nExpr-1);
152014
152015 /* Begin a loop that will extract all source rows in GROUP BY order.
152016 ** This might involve two separate loops with an OP_Sort in between, or
152017 ** it might be a single loop that uses an index to extract information
152018 ** in the right order to begin with.
152019 */
152020 sqlite3VdbeAddOp2(v, OP_Gosub10, regReset, addrReset);
152021 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
152022 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
152023 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY0x0080 : WHERE_GROUPBY0x0040)
152024 | (orderByGrp ? WHERE_SORTBYGROUP0x0200 : 0) | distFlag, 0
152025 );
152026 if( pWInfo==0 ){
152027 sqlite3ExprListDelete(db, pDistinct);
152028 goto select_end;
152029 }
152030 if( pParse->pIdxEpr ){
152031 optimizeAggregateUseOfIndexedExpr(pParse, p, pAggInfo, &sNC);
152032 }
152033 assignAggregateRegisters(pParse, pAggInfo);
152034 eDist = sqlite3WhereIsDistinct(pWInfo);
152035 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
152036 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
152037 /* The optimizer is able to deliver rows in group by order so
152038 ** we do not have to sort. The OP_OpenEphemeral table will be
152039 ** cancelled later because we still need to use the pKeyInfo
152040 */
152041 groupBySort = 0;
152042 }else{
152043 /* Rows are coming out in undetermined order. We have to push
152044 ** each row into a sorting index, terminate the first loop,
152045 ** then loop over the sorting index in order to get the output
152046 ** in sorted order
152047 */
152048 int regBase;
152049 int regRecord;
152050 int nCol;
152051 int nGroupBy;
152052
152053#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
152054 int addrExp; /* Address of OP_Explain instruction */
152055#endif
152056 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" ))
152057 (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" ))
152058 "DISTINCT" : "GROUP BY"(addrExp = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s"
, (sDistinct.isTnct && (p->selFlags&0x0000001)
==0) ? "DISTINCT" : "GROUP BY" ))
152059 ))(addrExp = sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s"
, (sDistinct.isTnct && (p->selFlags&0x0000001)
==0) ? "DISTINCT" : "GROUP BY" ))
;
152060
152061 groupBySort = 1;
152062 nGroupBy = pGroupBy->nExpr;
152063 nCol = nGroupBy;
152064 j = nGroupBy;
152065 for(i=0; i<pAggInfo->nColumn; i++){
152066 if( pAggInfo->aCol[i].iSorterColumn>=j ){
152067 nCol++;
152068 j++;
152069 }
152070 }
152071 regBase = sqlite3GetTempRange(pParse, nCol);
152072 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
152073 j = nGroupBy;
152074 pAggInfo->directMode = 1;
152075 for(i=0; i<pAggInfo->nColumn; i++){
152076 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
152077 if( pCol->iSorterColumn>=j ){
152078 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
152079 j++;
152080 }
152081 }
152082 pAggInfo->directMode = 0;
152083 regRecord = sqlite3GetTempReg(pParse);
152084 sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v));
152085 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regBase, nCol, regRecord);
152086 sqlite3VdbeAddOp2(v, OP_SorterInsert139, pAggInfo->sortingIdx, regRecord);
152087 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
152088 sqlite3ReleaseTempReg(pParse, regRecord);
152089 sqlite3ReleaseTempRange(pParse, regBase, nCol);
152090 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
152091 sqlite3WhereEnd(pWInfo);
152092 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
152093 sortOut = sqlite3GetTempReg(pParse);
152094 sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0);
152095 sqlite3VdbeAddOp3(v, OP_OpenPseudo121, sortPTab, sortOut, nCol);
152096 sqlite3VdbeAddOp2(v, OP_SorterSort34, pAggInfo->sortingIdx, addrEnd);
152097 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
152098 pAggInfo->useSortingIdx = 1;
152099 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
152100 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
152101 }
152102
152103 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
152104 ** that are indexed (and that were previously identified and tagged
152105 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
152106 ** must now be converted into a TK_AGG_COLUMN node so that the value
152107 ** is correctly pulled from the index rather than being recomputed. */
152108 if( pParse->pIdxEpr ){
152109 aggregateConvertIndexedExprRefToColumn(pAggInfo);
152110#if TREETRACE_ENABLED0
152111 if( sqlite3TreeTrace & 0x20 ){
152112 TREETRACE(0x20, pParse, p,
152113 ("AggInfo function expressions converted to reference index\n"));
152114 sqlite3TreeViewSelect(0, p, 0);
152115 printAggInfo(pAggInfo);
152116 }
152117#endif
152118 }
152119
152120 /* If the index or temporary table used by the GROUP BY sort
152121 ** will naturally deliver rows in the order required by the ORDER BY
152122 ** clause, cancel the ephemeral table open coded earlier.
152123 **
152124 ** This is an optimization - the correct answer should result regardless.
152125 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
152126 ** disable this optimization for testing purposes. */
152127 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)(((db)->dbOptFlags&(0x00000004))==0)
152128 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
152129 ){
152130 sSort.pOrderBy = 0;
152131 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
152132 }
152133
152134 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
152135 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
152136 ** Then compare the current GROUP BY terms against the GROUP BY terms
152137 ** from the previous row currently stored in a0, a1, a2...
152138 */
152139 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
152140 if( groupBySort ){
152141 sqlite3VdbeAddOp3(v, OP_SorterData133, pAggInfo->sortingIdx,
152142 sortOut, sortPTab);
152143 }
152144 for(j=0; j<pGroupBy->nExpr; j++){
152145 int iOrderByCol = pGroupBy->a[j].u.x.iOrderByCol;
152146
152147 if( groupBySort ){
152148 sqlite3VdbeAddOp3(v, OP_Column94, sortPTab, j, iBMem+j);
152149 }else{
152150 pAggInfo->directMode = 1;
152151 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
152152 }
152153
152154 if( iOrderByCol ){
152155 Expr *pX = p->pEList->a[iOrderByCol-1].pExpr;
152156 Expr *pBase = sqlite3ExprSkipCollateAndLikely(pX);
152157 if( ALWAYS(pBase!=0)(pBase!=0)
152158 && pBase->op!=TK_AGG_COLUMN170
152159 && pBase->op!=TK_REGISTER176
152160 ){
152161 sqlite3ExprToRegister(pX, iAMem+j);
152162 }
152163 }
152164 }
152165 sqlite3VdbeAddOp4(v, OP_Compare90, iAMem, iBMem, pGroupBy->nExpr,
152166 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-8));
152167 addr1 = sqlite3VdbeCurrentAddr(v);
152168 sqlite3VdbeAddOp3(v, OP_Jump14, addr1+1, 0, addr1+1); VdbeCoverage(v);
152169
152170 /* Generate code that runs whenever the GROUP BY changes.
152171 ** Changes in the GROUP BY are detected by the previous code
152172 ** block. If there were no changes, this block is skipped.
152173 **
152174 ** This code copies current group by terms in b0,b1,b2,...
152175 ** over to a0,a1,a2. It then calls the output subroutine
152176 ** and resets the aggregate accumulator registers in preparation
152177 ** for the next GROUP BY batch.
152178 */
152179 sqlite3VdbeAddOp2(v, OP_Gosub10, regOutputRow, addrOutputRow);
152180 VdbeComment((v, "output one row"));
152181 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
152182 sqlite3VdbeAddOp2(v, OP_IfPos50, iAbortFlag, addrEnd); VdbeCoverage(v);
152183 VdbeComment((v, "check abort flag"));
152184 sqlite3VdbeAddOp2(v, OP_Gosub10, regReset, addrReset);
152185 VdbeComment((v, "reset accumulator"));
152186
152187 /* Update the aggregate accumulators based on the content of
152188 ** the current row
152189 */
152190 sqlite3VdbeJumpHere(v, addr1);
152191 updateAccumulator(pParse, iUseFlag, pAggInfo, eDist);
152192 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iUseFlag);
152193 VdbeComment((v, "indicate data in accumulator"));
152194
152195 /* End of the loop
152196 */
152197 if( groupBySort ){
152198 sqlite3VdbeAddOp2(v, OP_SorterNext37, pAggInfo->sortingIdx,addrTopOfLoop);
152199 VdbeCoverage(v);
152200 }else{
152201 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
152202 sqlite3WhereEnd(pWInfo);
152203 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
152204 }
152205 sqlite3ExprListDelete(db, pDistinct);
152206
152207 /* Output the final row of result
152208 */
152209 sqlite3VdbeAddOp2(v, OP_Gosub10, regOutputRow, addrOutputRow);
152210 VdbeComment((v, "output final row"));
152211
152212 /* Jump over the subroutines
152213 */
152214 sqlite3VdbeGoto(v, addrEnd);
152215
152216 /* Generate a subroutine that outputs a single row of the result
152217 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
152218 ** is less than or equal to zero, the subroutine is a no-op. If
152219 ** the processing calls for the query to abort, this subroutine
152220 ** increments the iAbortFlag memory location before returning in
152221 ** order to signal the caller to abort.
152222 */
152223 addrSetAbort = sqlite3VdbeCurrentAddr(v);
152224 sqlite3VdbeAddOp2(v, OP_Integer71, 1, iAbortFlag);
152225 VdbeComment((v, "set abort flag"));
152226 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
152227 sqlite3VdbeResolveLabel(v, addrOutputRow);
152228 addrOutputRow = sqlite3VdbeCurrentAddr(v);
152229 sqlite3VdbeAddOp2(v, OP_IfPos50, iUseFlag, addrOutputRow+2);
152230 VdbeCoverage(v);
152231 VdbeComment((v, "Groupby result generator entry point"));
152232 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
152233 finalizeAggFunctions(pParse, pAggInfo);
152234 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL0x10);
152235 selectInnerLoop(pParse, p, -1, &sSort,
152236 &sDistinct, pDest,
152237 addrOutputRow+1, addrSetAbort);
152238 sqlite3VdbeAddOp1(v, OP_Return67, regOutputRow);
152239 VdbeComment((v, "end groupby result generator"));
152240
152241 /* Generate a subroutine that will reset the group-by accumulator
152242 */
152243 sqlite3VdbeResolveLabel(v, addrReset);
152244 resetAccumulator(pParse, pAggInfo);
152245 sqlite3VdbeAddOp2(v, OP_Integer71, 0, iUseFlag);
152246 VdbeComment((v, "indicate accumulator empty"));
152247 sqlite3VdbeAddOp1(v, OP_Return67, regReset);
152248
152249 if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP0 ){
152250 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
152251 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
152252 }
152253 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
152254 else {
152255 /* Aggregate functions without GROUP BY. tag-select-0820 */
152256 Table *pTab;
152257 if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){
152258 /* tag-select-0821
152259 **
152260 ** If isSimpleCount() returns a pointer to a Table structure, then
152261 ** the SQL statement is of the form:
152262 **
152263 ** SELECT count(*) FROM <tbl>
152264 **
152265 ** where the Table structure returned represents table <tbl>.
152266 **
152267 ** This statement is so common that it is optimized specially. The
152268 ** OP_Count instruction is executed either on the intkey table that
152269 ** contains the data for table <tbl> or on one of its indexes. It
152270 ** is better to execute the op on an index, as indexes are almost
152271 ** always spread across less pages than their corresponding tables.
152272 */
152273 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
152274 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
152275 Index *pIdx; /* Iterator variable */
152276 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
152277 Index *pBest = 0; /* Best index found so far */
152278 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
152279
152280 sqlite3CodeVerifySchema(pParse, iDb);
152281 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
152282
152283 /* Search for the index that has the lowest scan cost.
152284 **
152285 ** (2011-04-15) Do not do a full scan of an unordered index.
152286 **
152287 ** (2013-10-03) Do not count the entries in a partial index.
152288 **
152289 ** In practice the KeyInfo structure will not be used. It is only
152290 ** passed to keep OP_OpenRead happy.
152291 */
152292 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ) pBest = sqlite3PrimaryKeyIndex(pTab);
152293 if( !p->pSrc->a[0].fg.notIndexed ){
152294 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
152295 if( pIdx->bUnordered==0
152296 && pIdx->szIdxRow<pTab->szTabRow
152297 && pIdx->pPartIdxWhere==0
152298 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
152299 ){
152300 pBest = pIdx;
152301 }
152302 }
152303 }
152304 if( pBest ){
152305 iRoot = pBest->tnum;
152306 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
152307 }
152308
152309 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
152310 sqlite3VdbeAddOp4Int(v, OP_OpenRead102, iCsr, (int)iRoot, iDb, 1);
152311 if( pKeyInfo ){
152312 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO(-8));
152313 }
152314 assignAggregateRegisters(pParse, pAggInfo);
152315 sqlite3VdbeAddOp2(v, OP_Count98, iCsr, AggInfoFuncReg(pAggInfo,0)((pAggInfo)->iFirstReg+(pAggInfo)->nColumn+(0)));
152316 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
152317 explainSimpleCount(pParse, pTab, pBest);
152318 }else{
152319 /* The general case of an aggregate query without GROUP BY
152320 ** tag-select-0822 */
152321 int regAcc = 0; /* "populate accumulators" flag */
152322 ExprList *pDistinct = 0;
152323 u16 distFlag = 0;
152324 int eDist;
152325
152326 /* If there are accumulator registers but no min() or max() functions
152327 ** without FILTER clauses, allocate register regAcc. Register regAcc
152328 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
152329 ** The code generated by updateAccumulator() uses this to ensure
152330 ** that the accumulator registers are (a) updated only once if
152331 ** there are no min() or max functions or (b) always updated for the
152332 ** first row visited by the aggregate, so that they are updated at
152333 ** least once even if the FILTER clause means the min() or max()
152334 ** function visits zero rows. */
152335 if( pAggInfo->nAccumulator ){
152336 for(i=0; i<pAggInfo->nFunc; i++){
152337 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc)(((pAggInfo->aFunc[i].pFExpr)->flags&(0x1000000))!=
0)
){
152338 continue;
152339 }
152340 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL0x0020 ){
152341 break;
152342 }
152343 }
152344 if( i==pAggInfo->nFunc ){
152345 regAcc = ++pParse->nMem;
152346 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regAcc);
152347 }
152348 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
152349 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) )((void) (0));
152350 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
152351 distFlag = pDistinct ? (WHERE_WANT_DISTINCT0x0100|WHERE_AGG_DISTINCT0x0400) : 0;
152352 }
152353 assignAggregateRegisters(pParse, pAggInfo);
152354
152355 /* This case runs if the aggregate has no GROUP BY clause. The
152356 ** processing is much simpler since there is only a single row
152357 ** of output.
152358 */
152359 assert( p->pGroupBy==0 )((void) (0));
152360 resetAccumulator(pParse, pAggInfo);
152361
152362 /* If this query is a candidate for the min/max optimization, then
152363 ** minMaxFlag will have been previously set to either
152364 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
152365 ** be an appropriate ORDER BY expression for the optimization.
152366 */
152367 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 )((void) (0));
152368 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 )((void) (0));
152369
152370 TREETRACE(0x2,pParse,p,("WhereBegin\n"));
152371 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
152372 pDistinct, p, minMaxFlag|distFlag, 0);
152373 if( pWInfo==0 ){
152374 goto select_end;
152375 }
152376 TREETRACE(0x2,pParse,p,("WhereBegin returns\n"));
152377 eDist = sqlite3WhereIsDistinct(pWInfo);
152378 updateAccumulator(pParse, regAcc, pAggInfo, eDist);
152379 if( eDist!=WHERE_DISTINCT_NOOP0 ){
152380 struct AggInfo_func *pF = pAggInfo->aFunc;
152381 if( pF ){
152382 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
152383 }
152384 }
152385
152386 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer71, 1, regAcc);
152387 if( minMaxFlag ){
152388 sqlite3WhereMinMaxOptEarlyOut(v, pWInfo);
152389 }
152390 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
152391 sqlite3WhereEnd(pWInfo);
152392 finalizeAggFunctions(pParse, pAggInfo);
152393 }
152394
152395 sSort.pOrderBy = 0;
152396 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL0x10);
152397 selectInnerLoop(pParse, p, -1, 0, 0,
152398 pDest, addrEnd, addrEnd);
152399 }
152400 sqlite3VdbeResolveLabel(v, addrEnd);
152401
152402 } /* endif aggregate query */
152403
152404 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED3 ){
152405 explainTempTable(pParse, "DISTINCT");
152406 }
152407
152408 /* If there is an ORDER BY clause, then we need to sort the results
152409 ** and send them to the callback one by one. tag-select-0900
152410 */
152411 if( sSort.pOrderBy ){
152412 assert( p->pEList==pEList )((void) (0));
152413 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
152414 }
152415
152416 /* Jump here to skip this query
152417 */
152418 sqlite3VdbeResolveLabel(v, iEnd);
152419
152420 /* The SELECT has been coded. If there is an error in the Parse structure,
152421 ** set the return code to 1. Otherwise 0. */
152422 rc = (pParse->nErr>0);
152423
152424 /* Control jumps to here if an error is encountered above, or upon
152425 ** successful coding of the SELECT.
152426 */
152427select_end:
152428 assert( db->mallocFailed==0 || db->mallocFailed==1 )((void) (0));
152429 assert( db->mallocFailed==0 || pParse->nErr!=0 )((void) (0));
152430 sqlite3ExprListDelete(db, pMinMaxOrderBy);
152431#ifdef SQLITE_DEBUG
152432 /* Internal self-checks. tag-select-1000 */
152433 if( pAggInfo && !db->mallocFailed ){
152434#if TREETRACE_ENABLED0
152435 if( sqlite3TreeTrace & 0x20 ){
152436 TREETRACE(0x20,pParse,p,("Finished with AggInfo\n"));
152437 printAggInfo(pAggInfo);
152438 }
152439#endif
152440 for(i=0; i<pAggInfo->nColumn; i++){
152441 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
152442 if( pExpr==0 ) continue;
152443 assert( pExpr->pAggInfo==pAggInfo )((void) (0));
152444 assert( pExpr->iAgg==i )((void) (0));
152445 }
152446 for(i=0; i<pAggInfo->nFunc; i++){
152447 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
152448 assert( pExpr!=0 )((void) (0));
152449 assert( pExpr->pAggInfo==pAggInfo )((void) (0));
152450 assert( pExpr->iAgg==i )((void) (0));
152451 }
152452 }
152453#endif
152454
152455#if TREETRACE_ENABLED0
152456 TREETRACE(0x1,pParse,p,("end processing\n"));
152457 if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
152458 sqlite3TreeViewSelect(0, p, 0);
152459 }
152460#endif
152461 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
152462 return rc;
152463}
152464
152465/************** End of select.c **********************************************/
152466/************** Begin file table.c *******************************************/
152467/*
152468** 2001 September 15
152469**
152470** The author disclaims copyright to this source code. In place of
152471** a legal notice, here is a blessing:
152472**
152473** May you do good and not evil.
152474** May you find forgiveness for yourself and forgive others.
152475** May you share freely, never taking more than you give.
152476**
152477*************************************************************************
152478** This file contains the sqlite3_get_table() and sqlite3_free_table()
152479** interface routines. These are just wrappers around the main
152480** interface routine of sqlite3_exec().
152481**
152482** These routines are in a separate files so that they will not be linked
152483** if they are not used.
152484*/
152485/* #include "sqliteInt.h" */
152486
152487#ifndef SQLITE_OMIT_GET_TABLE
152488
152489/*
152490** This structure is used to pass data from sqlite3_get_table() through
152491** to the callback function is uses to build the result.
152492*/
152493typedef struct TabResult {
152494 char **azResult; /* Accumulated output */
152495 char *zErrMsg; /* Error message text, if an error occurs */
152496 u32 nAlloc; /* Slots allocated for azResult[] */
152497 u32 nRow; /* Number of rows in the result */
152498 u32 nColumn; /* Number of columns in the result */
152499 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
152500 int rc; /* Return code from sqlite3_exec() */
152501} TabResult;
152502
152503/*
152504** This routine is called once for each row in the result table. Its job
152505** is to fill in the TabResult structure appropriately, allocating new
152506** memory as necessary.
152507*/
152508static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
152509 TabResult *p = (TabResult*)pArg; /* Result accumulator */
152510 int need; /* Slots needed in p->azResult[] */
152511 int i; /* Loop counter */
152512 char *z; /* A single column of result */
152513
152514 /* Make sure there is enough space in p->azResult to hold everything
152515 ** we need to remember from this invocation of the callback.
152516 */
152517 if( p->nRow==0 && argv!=0 ){
152518 need = nCol*2;
152519 }else{
152520 need = nCol;
152521 }
152522 if( p->nData + need > p->nAlloc ){
152523 char **azNew;
152524 p->nAlloc = p->nAlloc*2 + need;
152525 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
152526 if( azNew==0 ) goto malloc_failed;
152527 p->azResult = azNew;
152528 }
152529
152530 /* If this is the first row, then generate an extra row containing
152531 ** the names of all columns.
152532 */
152533 if( p->nRow==0 ){
152534 p->nColumn = nCol;
152535 for(i=0; i<nCol; i++){
152536 z = sqlite3_mprintf("%s", colv[i]);
152537 if( z==0 ) goto malloc_failed;
152538 p->azResult[p->nData++] = z;
152539 }
152540 }else if( (int)p->nColumn!=nCol ){
152541 sqlite3_free(p->zErrMsg);
152542 p->zErrMsg = sqlite3_mprintf(
152543 "sqlite3_get_table() called with two or more incompatible queries"
152544 );
152545 p->rc = SQLITE_ERROR1;
152546 return 1;
152547 }
152548
152549 /* Copy over the row data
152550 */
152551 if( argv!=0 ){
152552 for(i=0; i<nCol; i++){
152553 if( argv[i]==0 ){
152554 z = 0;
152555 }else{
152556 int n = sqlite3Strlen30(argv[i])+1;
152557 z = sqlite3_malloc64( n );
152558 if( z==0 ) goto malloc_failed;
152559 memcpy(z, argv[i], n);
152560 }
152561 p->azResult[p->nData++] = z;
152562 }
152563 p->nRow++;
152564 }
152565 return 0;
152566
152567malloc_failed:
152568 p->rc = SQLITE_NOMEM_BKPT7;
152569 return 1;
152570}
152571
152572/*
152573** Query the database. But instead of invoking a callback for each row,
152574** malloc() for space to hold the result and return the entire results
152575** at the conclusion of the call.
152576**
152577** The result that is written to ***pazResult is held in memory obtained
152578** from malloc(). But the caller cannot free this memory directly.
152579** Instead, the entire table should be passed to sqlite3_free_table() when
152580** the calling procedure is finished using it.
152581*/
152582SQLITE_API int sqlite3_get_table(
152583 sqlite3 *db, /* The database on which the SQL executes */
152584 const char *zSql, /* The SQL to be executed */
152585 char ***pazResult, /* Write the result table here */
152586 int *pnRow, /* Write the number of rows in the result here */
152587 int *pnColumn, /* Write the number of columns of result here */
152588 char **pzErrMsg /* Write error messages here */
152589){
152590 int rc;
152591 TabResult res;
152592
152593#ifdef SQLITE_ENABLE_API_ARMOR1
152594 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(152594);
152595#endif
152596 *pazResult = 0;
152597 if( pnColumn ) *pnColumn = 0;
152598 if( pnRow ) *pnRow = 0;
152599 if( pzErrMsg ) *pzErrMsg = 0;
152600 res.zErrMsg = 0;
152601 res.nRow = 0;
152602 res.nColumn = 0;
152603 res.nData = 1;
152604 res.nAlloc = 20;
152605 res.rc = SQLITE_OK0;
152606 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
152607 if( res.azResult==0 ){
152608 db->errCode = SQLITE_NOMEM7;
152609 return SQLITE_NOMEM_BKPT7;
152610 }
152611 res.azResult[0] = 0;
152612 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
152613 assert( sizeof(res.azResult[0])>= sizeof(res.nData) )((void) (0));
152614 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData)((void*)(long int)(res.nData));
152615 if( (rc&0xff)==SQLITE_ABORT4 ){
152616 sqlite3_free_table(&res.azResult[1]);
152617 if( res.zErrMsg ){
152618 if( pzErrMsg ){
152619 sqlite3_free(*pzErrMsg);
152620 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
152621 }
152622 sqlite3_free(res.zErrMsg);
152623 }
152624 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
152625 return res.rc;
152626 }
152627 sqlite3_free(res.zErrMsg);
152628 if( rc!=SQLITE_OK0 ){
152629 sqlite3_free_table(&res.azResult[1]);
152630 return rc;
152631 }
152632 if( res.nAlloc>res.nData ){
152633 char **azNew;
152634 azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
152635 if( azNew==0 ){
152636 sqlite3_free_table(&res.azResult[1]);
152637 db->errCode = SQLITE_NOMEM7;
152638 return SQLITE_NOMEM_BKPT7;
152639 }
152640 res.azResult = azNew;
152641 }
152642 *pazResult = &res.azResult[1];
152643 if( pnColumn ) *pnColumn = res.nColumn;
152644 if( pnRow ) *pnRow = res.nRow;
152645 return rc;
152646}
152647
152648/*
152649** This routine frees the space the sqlite3_get_table() malloced.
152650*/
152651SQLITE_API void sqlite3_free_table(
152652 char **azResult /* Result returned from sqlite3_get_table() */
152653){
152654 if( azResult ){
152655 int i, n;
152656 azResult--;
152657 assert( azResult!=0 )((void) (0));
152658 n = SQLITE_PTR_TO_INT(azResult[0])((int)(long int)(azResult[0]));
152659 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
152660 sqlite3_free(azResult);
152661 }
152662}
152663
152664#endif /* SQLITE_OMIT_GET_TABLE */
152665
152666/************** End of table.c ***********************************************/
152667/************** Begin file trigger.c *****************************************/
152668/*
152669**
152670** The author disclaims copyright to this source code. In place of
152671** a legal notice, here is a blessing:
152672**
152673** May you do good and not evil.
152674** May you find forgiveness for yourself and forgive others.
152675** May you share freely, never taking more than you give.
152676**
152677*************************************************************************
152678** This file contains the implementation for TRIGGERs
152679*/
152680/* #include "sqliteInt.h" */
152681
152682#ifndef SQLITE_OMIT_TRIGGER
152683/*
152684** Delete a linked list of TriggerStep structures.
152685*/
152686SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
152687 while( pTriggerStep ){
152688 TriggerStep * pTmp = pTriggerStep;
152689 pTriggerStep = pTriggerStep->pNext;
152690
152691 sqlite3ExprDelete(db, pTmp->pWhere);
152692 sqlite3ExprListDelete(db, pTmp->pExprList);
152693 sqlite3SelectDelete(db, pTmp->pSelect);
152694 sqlite3IdListDelete(db, pTmp->pIdList);
152695 sqlite3UpsertDelete(db, pTmp->pUpsert);
152696 sqlite3SrcListDelete(db, pTmp->pFrom);
152697 sqlite3DbFree(db, pTmp->zSpan);
152698
152699 sqlite3DbFree(db, pTmp);
152700 }
152701}
152702
152703/*
152704** Given table pTab, return a list of all the triggers attached to
152705** the table. The list is connected by Trigger.pNext pointers.
152706**
152707** All of the triggers on pTab that are in the same database as pTab
152708** are already attached to pTab->pTrigger. But there might be additional
152709** triggers on pTab in the TEMP schema. This routine prepends all
152710** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
152711** and returns the combined list.
152712**
152713** To state it another way: This routine returns a list of all triggers
152714** that fire off of pTab. The list will include any TEMP triggers on
152715** pTab as well as the triggers lised in pTab->pTrigger.
152716*/
152717SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
152718 Schema *pTmpSchema; /* Schema of the pTab table */
152719 Trigger *pList; /* List of triggers to return */
152720 HashElem *p; /* Loop variable for TEMP triggers */
152721
152722 assert( pParse->disableTriggers==0 )((void) (0));
152723 pTmpSchema = pParse->db->aDb[1].pSchema;
152724 p = sqliteHashFirst(&pTmpSchema->trigHash)((&pTmpSchema->trigHash)->first);
152725 pList = pTab->pTrigger;
152726 while( p ){
152727 Trigger *pTrig = (Trigger *)sqliteHashData(p)((p)->data);
152728 if( pTrig->pTabSchema==pTab->pSchema
152729 && pTrig->table
152730 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
152731 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
152732 ){
152733 pTrig->pNext = pList;
152734 pList = pTrig;
152735 }else if( pTrig->op==TK_RETURNING151 ){
152736#ifndef SQLITE_OMIT_VIRTUALTABLE
152737 assert( pParse->db->pVtabCtx==0 )((void) (0));
152738#endif
152739 assert( pParse->bReturning )((void) (0));
152740 assert( &(pParse->u1.pReturning->retTrig) == pTrig )((void) (0));
152741 pTrig->table = pTab->zName;
152742 pTrig->pTabSchema = pTab->pSchema;
152743 pTrig->pNext = pList;
152744 pList = pTrig;
152745 }
152746 p = sqliteHashNext(p)((p)->next);
152747 }
152748#if 0
152749 if( pList ){
152750 Trigger *pX;
152751 printf("Triggers for %s:", pTab->zName);
152752 for(pX=pList; pX; pX=pX->pNext){
152753 printf(" %s", pX->zName);
152754 }
152755 printf("\n");
152756 fflush(stdoutstdout);
152757 }
152758#endif
152759 return pList;
152760}
152761
152762/*
152763** This is called by the parser when it sees a CREATE TRIGGER statement
152764** up to the point of the BEGIN before the trigger actions. A Trigger
152765** structure is generated based on the information available and stored
152766** in pParse->pNewTrigger. After the trigger actions have been parsed, the
152767** sqlite3FinishTrigger() function is called to complete the trigger
152768** construction process.
152769*/
152770SQLITE_PRIVATEstatic void sqlite3BeginTrigger(
152771 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
152772 Token *pName1, /* The name of the trigger */
152773 Token *pName2, /* The name of the trigger */
152774 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
152775 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
152776 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
152777 SrcList *pTableName,/* The name of the table/view the trigger applies to */
152778 Expr *pWhen, /* WHEN clause */
152779 int isTemp, /* True if the TEMPORARY keyword is present */
152780 int noErr /* Suppress errors if the trigger already exists */
152781){
152782 Trigger *pTrigger = 0; /* The new trigger */
152783 Table *pTab; /* Table that the trigger fires off of */
152784 char *zName = 0; /* Name of the trigger */
152785 sqlite3 *db = pParse->db; /* The database connection */
152786 int iDb; /* The database to store the trigger in */
152787 Token *pName; /* The unqualified db name */
152788 DbFixer sFix; /* State vector for the DB fixer */
152789
152790 assert( pName1!=0 )((void) (0)); /* pName1->z might be NULL, but not pName1 itself */
152791 assert( pName2!=0 )((void) (0));
152792 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE )((void) (0));
152793 assert( op>0 && op<0xff )((void) (0));
152794 if( isTemp ){
152795 /* If TEMP was specified, then the trigger name may not be qualified. */
152796 if( pName2->n>0 ){
152797 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
152798 goto trigger_cleanup;
152799 }
152800 iDb = 1;
152801 pName = pName1;
152802 }else{
152803 /* Figure out the db that the trigger will be created in */
152804 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
152805 if( iDb<0 ){
152806 goto trigger_cleanup;
152807 }
152808 }
152809 if( !pTableName || db->mallocFailed ){
152810 goto trigger_cleanup;
152811 }
152812
152813 /* A long-standing parser bug is that this syntax was allowed:
152814 **
152815 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
152816 ** ^^^^^^^^
152817 **
152818 ** To maintain backwards compatibility, ignore the database
152819 ** name on pTableName if we are reparsing out of the schema table
152820 */
152821 if( db->init.busy && iDb!=1 ){
152822 assert( pTableName->a[0].fg.fixedSchema==0 )((void) (0));
152823 assert( pTableName->a[0].fg.isSubquery==0 )((void) (0));
152824 sqlite3DbFree(db, pTableName->a[0].u4.zDatabase);
152825 pTableName->a[0].u4.zDatabase = 0;
152826 }
152827
152828 /* If the trigger name was unqualified, and the table is a temp table,
152829 ** then set iDb to 1 to create the trigger in the temporary database.
152830 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
152831 ** exist, the error is caught by the block below.
152832 */
152833 pTab = sqlite3SrcListLookup(pParse, pTableName);
152834 if( db->init.busy==0 && pName2->n==0 && pTab
152835 && pTab->pSchema==db->aDb[1].pSchema ){
152836 iDb = 1;
152837 }
152838
152839 /* Ensure the table name matches database name and that the table exists */
152840 if( db->mallocFailed ) goto trigger_cleanup;
152841 assert( pTableName->nSrc==1 )((void) (0));
152842 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
152843 if( sqlite3FixSrcList(&sFix, pTableName) ){
152844 goto trigger_cleanup;
152845 }
152846 pTab = sqlite3SrcListLookup(pParse, pTableName);
152847 if( !pTab ){
152848 /* The table does not exist. */
152849 goto trigger_orphan_error;
152850 }
152851 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
152852 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
152853 goto trigger_orphan_error;
152854 }
152855 if( (pTab->tabFlags & TF_Shadow0x00001000)!=0 && sqlite3ReadOnlyShadowTables(db) ){
152856 sqlite3ErrorMsg(pParse, "cannot create triggers on shadow tables");
152857 goto trigger_orphan_error;
152858 }
152859
152860 /* Check that the trigger name is not reserved and that no trigger of the
152861 ** specified name exists */
152862 zName = sqlite3NameFromToken(db, pName);
152863 if( zName==0 ){
152864 assert( db->mallocFailed )((void) (0));
152865 goto trigger_cleanup;
152866 }
152867 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
152868 goto trigger_cleanup;
152869 }
152870 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
152871 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
152872 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
152873 if( !noErr ){
152874 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
152875 }else{
152876 assert( !db->init.busy )((void) (0));
152877 sqlite3CodeVerifySchema(pParse, iDb);
152878 VVA_ONLY( pParse->ifNotExists = 1; )
152879 }
152880 goto trigger_cleanup;
152881 }
152882 }
152883
152884 /* Do not create a trigger on a system table */
152885 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
152886 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
152887 goto trigger_cleanup;
152888 }
152889
152890 /* INSTEAD of triggers are only for views and views only support INSTEAD
152891 ** of triggers.
152892 */
152893 if( IsView(pTab)((pTab)->eTabType==2) && tr_tm!=TK_INSTEAD66 ){
152894 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
152895 (tr_tm == TK_BEFORE33)?"BEFORE":"AFTER", pTableName->a);
152896 goto trigger_orphan_error;
152897 }
152898 if( !IsView(pTab)((pTab)->eTabType==2) && tr_tm==TK_INSTEAD66 ){
152899 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
152900 " trigger on table: %S", pTableName->a);
152901 goto trigger_orphan_error;
152902 }
152903
152904#ifndef SQLITE_OMIT_AUTHORIZATION
152905 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
152906 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
152907 int code = SQLITE_CREATE_TRIGGER7;
152908 const char *zDb = db->aDb[iTabDb].zDbSName;
152909 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
152910 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER5;
152911 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
152912 goto trigger_cleanup;
152913 }
152914 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iTabDb)((!0)&&(iTabDb==1)?"sqlite_temp_master":"sqlite_master"
)
,0,zDb)){
152915 goto trigger_cleanup;
152916 }
152917 }
152918#endif
152919
152920 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
152921 ** cannot appear on views. So we might as well translate every
152922 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
152923 ** elsewhere.
152924 */
152925 if (tr_tm == TK_INSTEAD66){
152926 tr_tm = TK_BEFORE33;
152927 }
152928
152929 /* Build the Trigger object */
152930 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
152931 if( pTrigger==0 ) goto trigger_cleanup;
152932 pTrigger->zName = zName;
152933 zName = 0;
152934 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
152935 pTrigger->pSchema = db->aDb[iDb].pSchema;
152936 pTrigger->pTabSchema = pTab->pSchema;
152937 pTrigger->op = (u8)op;
152938 pTrigger->tr_tm = tr_tm==TK_BEFORE33 ? TRIGGER_BEFORE1 : TRIGGER_AFTER2;
152939 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
152940 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
152941 pTrigger->pWhen = pWhen;
152942 pWhen = 0;
152943 }else{
152944 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
152945 }
152946 pTrigger->pColumns = pColumns;
152947 pColumns = 0;
152948 assert( pParse->pNewTrigger==0 )((void) (0));
152949 pParse->pNewTrigger = pTrigger;
152950
152951trigger_cleanup:
152952 sqlite3DbFree(db, zName);
152953 sqlite3SrcListDelete(db, pTableName);
152954 sqlite3IdListDelete(db, pColumns);
152955 sqlite3ExprDelete(db, pWhen);
152956 if( !pParse->pNewTrigger ){
152957 sqlite3DeleteTrigger(db, pTrigger);
152958 }else{
152959 assert( pParse->pNewTrigger==pTrigger )((void) (0));
152960 }
152961 return;
152962
152963trigger_orphan_error:
152964 if( db->init.iDb==1 ){
152965 /* Ticket #3810.
152966 ** Normally, whenever a table is dropped, all associated triggers are
152967 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
152968 ** and the table is dropped by a different database connection, the
152969 ** trigger is not visible to the database connection that does the
152970 ** drop so the trigger cannot be dropped. This results in an
152971 ** "orphaned trigger" - a trigger whose associated table is missing.
152972 **
152973 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
152974 */
152975 db->init.orphanTrigger = 1;
152976 }
152977 goto trigger_cleanup;
152978}
152979
152980/*
152981** This routine is called after all of the trigger actions have been parsed
152982** in order to complete the process of building the trigger.
152983*/
152984SQLITE_PRIVATEstatic void sqlite3FinishTrigger(
152985 Parse *pParse, /* Parser context */
152986 TriggerStep *pStepList, /* The triggered program */
152987 Token *pAll /* Token that describes the complete CREATE TRIGGER */
152988){
152989 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
152990 char *zName; /* Name of trigger */
152991 sqlite3 *db = pParse->db; /* The database */
152992 DbFixer sFix; /* Fixer object */
152993 int iDb; /* Database containing the trigger */
152994 Token nameToken; /* Trigger name for error reporting */
152995
152996 pParse->pNewTrigger = 0;
152997 if( NEVER(pParse->nErr)(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
152998 zName = pTrig->zName;
152999 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
153000 pTrig->step_list = pStepList;
153001 while( pStepList ){
153002 pStepList->pTrig = pTrig;
153003 pStepList = pStepList->pNext;
153004 }
153005 sqlite3TokenInit(&nameToken, pTrig->zName);
153006 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
153007 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
153008 || sqlite3FixExpr(&sFix, pTrig->pWhen)
153009 ){
153010 goto triggerfinish_cleanup;
153011 }
153012
153013#ifndef SQLITE_OMIT_ALTERTABLE
153014 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
153015 assert( !db->init.busy )((void) (0));
153016 pParse->pNewTrigger = pTrig;
153017 pTrig = 0;
153018 }else
153019#endif
153020
153021 /* if we are not initializing,
153022 ** build the sqlite_schema entry
153023 */
153024 if( !db->init.busy ){
153025 Vdbe *v;
153026 char *z;
153027
153028 /* If this is a new CREATE TABLE statement, and if shadow tables
153029 ** are read-only, and the trigger makes a change to a shadow table,
153030 ** then raise an error - do not allow the trigger to be created. */
153031 if( sqlite3ReadOnlyShadowTables(db) ){
153032 TriggerStep *pStep;
153033 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
153034 if( pStep->zTarget!=0
153035 && sqlite3ShadowTableName(db, pStep->zTarget)
153036 ){
153037 sqlite3ErrorMsg(pParse,
153038 "trigger \"%s\" may not write to shadow table \"%s\"",
153039 pTrig->zName, pStep->zTarget);
153040 goto triggerfinish_cleanup;
153041 }
153042 }
153043 }
153044
153045 /* Make an entry in the sqlite_schema table */
153046 v = sqlite3GetVdbe(pParse);
153047 if( v==0 ) goto triggerfinish_cleanup;
153048 sqlite3BeginWriteOperation(pParse, 0, iDb);
153049 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
153050 testcase( z==0 );
153051 sqlite3NestedParse(pParse,
153052 "INSERT INTO %Q." LEGACY_SCHEMA_TABLE"sqlite_master"
153053 " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
153054 db->aDb[iDb].zDbSName, zName,
153055 pTrig->table, z);
153056 sqlite3DbFree(db, z);
153057 sqlite3ChangeCookie(pParse, iDb);
153058 sqlite3VdbeAddParseSchemaOp(v, iDb,
153059 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0);
153060 }
153061
153062 if( db->init.busy ){
153063 Trigger *pLink = pTrig;
153064 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
153065 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
153066 assert( pLink!=0 )((void) (0));
153067 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
153068 if( pTrig ){
153069 sqlite3OomFault(db);
153070 }else if( pLink->pSchema==pLink->pTabSchema ){
153071 Table *pTab;
153072 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
153073 assert( pTab!=0 )((void) (0));
153074 pLink->pNext = pTab->pTrigger;
153075 pTab->pTrigger = pLink;
153076 }
153077 }
153078
153079triggerfinish_cleanup:
153080 sqlite3DeleteTrigger(db, pTrig);
153081 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger )((void) (0));
153082 sqlite3DeleteTriggerStep(db, pStepList);
153083}
153084
153085/*
153086** Duplicate a range of text from an SQL statement, then convert all
153087** whitespace characters into ordinary space characters.
153088*/
153089static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
153090 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
153091 int i;
153092 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ) z[i] = ' ';
153093 return z;
153094}
153095
153096/*
153097** Turn a SELECT statement (that the pSelect parameter points to) into
153098** a trigger step. Return a pointer to a TriggerStep structure.
153099**
153100** The parser calls this routine when it finds a SELECT statement in
153101** body of a TRIGGER.
153102*/
153103SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(
153104 sqlite3 *db, /* Database connection */
153105 Select *pSelect, /* The SELECT statement */
153106 const char *zStart, /* Start of SQL text */
153107 const char *zEnd /* End of SQL text */
153108){
153109 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
153110 if( pTriggerStep==0 ) {
153111 sqlite3SelectDelete(db, pSelect);
153112 return 0;
153113 }
153114 pTriggerStep->op = TK_SELECT139;
153115 pTriggerStep->pSelect = pSelect;
153116 pTriggerStep->orconf = OE_Default11;
153117 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
153118 return pTriggerStep;
153119}
153120
153121/*
153122** Allocate space to hold a new trigger step. The allocated space
153123** holds both the TriggerStep object and the TriggerStep.target.z string.
153124**
153125** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
153126*/
153127static TriggerStep *triggerStepAllocate(
153128 Parse *pParse, /* Parser context */
153129 u8 op, /* Trigger opcode */
153130 Token *pName, /* The target name */
153131 const char *zStart, /* Start of SQL text */
153132 const char *zEnd /* End of SQL text */
153133){
153134 sqlite3 *db = pParse->db;
153135 TriggerStep *pTriggerStep;
153136
153137 if( pParse->nErr ) return 0;
153138 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
153139 if( pTriggerStep ){
153140 char *z = (char*)&pTriggerStep[1];
153141 memcpy(z, pName->z, pName->n);
153142 sqlite3Dequote(z);
153143 pTriggerStep->zTarget = z;
153144 pTriggerStep->op = op;
153145 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
153146 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
153147 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
153148 }
153149 }
153150 return pTriggerStep;
153151}
153152
153153/*
153154** Build a trigger step out of an INSERT statement. Return a pointer
153155** to the new trigger step.
153156**
153157** The parser calls this routine when it sees an INSERT inside the
153158** body of a trigger.
153159*/
153160SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(
153161 Parse *pParse, /* Parser */
153162 Token *pTableName, /* Name of the table into which we insert */
153163 IdList *pColumn, /* List of columns in pTableName to insert into */
153164 Select *pSelect, /* A SELECT statement that supplies values */
153165 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
153166 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
153167 const char *zStart, /* Start of SQL text */
153168 const char *zEnd /* End of SQL text */
153169){
153170 sqlite3 *db = pParse->db;
153171 TriggerStep *pTriggerStep;
153172
153173 assert(pSelect != 0 || db->mallocFailed)((void) (0));
153174
153175 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT128, pTableName,zStart,zEnd);
153176 if( pTriggerStep ){
153177 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
153178 pTriggerStep->pSelect = pSelect;
153179 pSelect = 0;
153180 }else{
153181 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
153182 }
153183 pTriggerStep->pIdList = pColumn;
153184 pTriggerStep->pUpsert = pUpsert;
153185 pTriggerStep->orconf = orconf;
153186 if( pUpsert ){
153187 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
153188 }
153189 }else{
153190 testcase( pColumn );
153191 sqlite3IdListDelete(db, pColumn);
153192 testcase( pUpsert );
153193 sqlite3UpsertDelete(db, pUpsert);
153194 }
153195 sqlite3SelectDelete(db, pSelect);
153196
153197 return pTriggerStep;
153198}
153199
153200/*
153201** Construct a trigger step that implements an UPDATE statement and return
153202** a pointer to that trigger step. The parser calls this routine when it
153203** sees an UPDATE statement inside the body of a CREATE TRIGGER.
153204*/
153205SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(
153206 Parse *pParse, /* Parser */
153207 Token *pTableName, /* Name of the table to be updated */
153208 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
153209 ExprList *pEList, /* The SET clause: list of column and new values */
153210 Expr *pWhere, /* The WHERE clause */
153211 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
153212 const char *zStart, /* Start of SQL text */
153213 const char *zEnd /* End of SQL text */
153214){
153215 sqlite3 *db = pParse->db;
153216 TriggerStep *pTriggerStep;
153217
153218 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE130, pTableName,zStart,zEnd);
153219 if( pTriggerStep ){
153220 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
153221 pTriggerStep->pExprList = pEList;
153222 pTriggerStep->pWhere = pWhere;
153223 pTriggerStep->pFrom = pFrom;
153224 pEList = 0;
153225 pWhere = 0;
153226 pFrom = 0;
153227 }else{
153228 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE0x0001);
153229 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
153230 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE0x0001);
153231 }
153232 pTriggerStep->orconf = orconf;
153233 }
153234 sqlite3ExprListDelete(db, pEList);
153235 sqlite3ExprDelete(db, pWhere);
153236 sqlite3SrcListDelete(db, pFrom);
153237 return pTriggerStep;
153238}
153239
153240/*
153241** Construct a trigger step that implements a DELETE statement and return
153242** a pointer to that trigger step. The parser calls this routine when it
153243** sees a DELETE statement inside the body of a CREATE TRIGGER.
153244*/
153245SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(
153246 Parse *pParse, /* Parser */
153247 Token *pTableName, /* The table from which rows are deleted */
153248 Expr *pWhere, /* The WHERE clause */
153249 const char *zStart, /* Start of SQL text */
153250 const char *zEnd /* End of SQL text */
153251){
153252 sqlite3 *db = pParse->db;
153253 TriggerStep *pTriggerStep;
153254
153255 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE129, pTableName,zStart,zEnd);
153256 if( pTriggerStep ){
153257 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
153258 pTriggerStep->pWhere = pWhere;
153259 pWhere = 0;
153260 }else{
153261 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
153262 }
153263 pTriggerStep->orconf = OE_Default11;
153264 }
153265 sqlite3ExprDelete(db, pWhere);
153266 return pTriggerStep;
153267}
153268
153269/*
153270** Recursively delete a Trigger structure
153271*/
153272SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
153273 if( pTrigger==0 || pTrigger->bReturning ) return;
153274 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
153275 sqlite3DbFree(db, pTrigger->zName);
153276 sqlite3DbFree(db, pTrigger->table);
153277 sqlite3ExprDelete(db, pTrigger->pWhen);
153278 sqlite3IdListDelete(db, pTrigger->pColumns);
153279 sqlite3DbFree(db, pTrigger);
153280}
153281
153282/*
153283** This function is called to drop a trigger from the database schema.
153284**
153285** This may be called directly from the parser and therefore identifies
153286** the trigger by name. The sqlite3DropTriggerPtr() routine does the
153287** same job as this routine except it takes a pointer to the trigger
153288** instead of the trigger name.
153289**/
153290SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
153291 Trigger *pTrigger = 0;
153292 int i;
153293 const char *zDb;
153294 const char *zName;
153295 sqlite3 *db = pParse->db;
153296
153297 if( db->mallocFailed ) goto drop_trigger_cleanup;
153298 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
153299 goto drop_trigger_cleanup;
153300 }
153301
153302 assert( pName->nSrc==1 )((void) (0));
153303 assert( pName->a[0].fg.fixedSchema==0 && pName->a[0].fg.isSubquery==0 )((void) (0));
153304 zDb = pName->a[0].u4.zDatabase;
153305 zName = pName->a[0].zName;
153306 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
153307 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
153308 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
153309 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
153310 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
153311 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
153312 if( pTrigger ) break;
153313 }
153314 if( !pTrigger ){
153315 if( !noErr ){
153316 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
153317 }else{
153318 sqlite3CodeVerifyNamedSchema(pParse, zDb);
153319 }
153320 pParse->checkSchema = 1;
153321 goto drop_trigger_cleanup;
153322 }
153323 sqlite3DropTriggerPtr(pParse, pTrigger);
153324
153325drop_trigger_cleanup:
153326 sqlite3SrcListDelete(db, pName);
153327}
153328
153329/*
153330** Return a pointer to the Table structure for the table that a trigger
153331** is set on.
153332*/
153333static Table *tableOfTrigger(Trigger *pTrigger){
153334 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
153335}
153336
153337
153338/*
153339** Drop a trigger given a pointer to that trigger.
153340*/
153341SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
153342 Table *pTable;
153343 Vdbe *v;
153344 sqlite3 *db = pParse->db;
153345 int iDb;
153346
153347 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
153348 assert( iDb>=0 && iDb<db->nDb )((void) (0));
153349 pTable = tableOfTrigger(pTrigger);
153350 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 )((void) (0));
153351#ifndef SQLITE_OMIT_AUTHORIZATION
153352 if( pTable ){
153353 int code = SQLITE_DROP_TRIGGER16;
153354 const char *zDb = db->aDb[iDb].zDbSName;
153355 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
153356 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER14;
153357 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
153358 sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
153359 return;
153360 }
153361 }
153362#endif
153363
153364 /* Generate code to destroy the database record of the trigger.
153365 */
153366 if( (v = sqlite3GetVdbe(pParse))!=0 ){
153367 sqlite3NestedParse(pParse,
153368 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " WHERE name=%Q AND type='trigger'",
153369 db->aDb[iDb].zDbSName, pTrigger->zName
153370 );
153371 sqlite3ChangeCookie(pParse, iDb);
153372 sqlite3VdbeAddOp4(v, OP_DropTrigger153, iDb, 0, 0, pTrigger->zName, 0);
153373 }
153374}
153375
153376/*
153377** Remove a trigger from the hash tables of the sqlite* pointer.
153378*/
153379SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
153380 Trigger *pTrigger;
153381 Hash *pHash;
153382
153383 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
153384 pHash = &(db->aDb[iDb].pSchema->trigHash);
153385 pTrigger = sqlite3HashInsert(pHash, zName, 0);
153386 if( ALWAYS(pTrigger)(pTrigger) ){
153387 if( pTrigger->pSchema==pTrigger->pTabSchema ){
153388 Table *pTab = tableOfTrigger(pTrigger);
153389 if( pTab ){
153390 Trigger **pp;
153391 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
153392 if( *pp==pTrigger ){
153393 *pp = (*pp)->pNext;
153394 break;
153395 }
153396 }
153397 }
153398 }
153399 sqlite3DeleteTrigger(db, pTrigger);
153400 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
153401 }
153402}
153403
153404/*
153405** pEList is the SET clause of an UPDATE statement. Each entry
153406** in pEList is of the format <id>=<expr>. If any of the entries
153407** in pEList have an <id> which matches an identifier in pIdList,
153408** then return TRUE. If pIdList==NULL, then it is considered a
153409** wildcard that matches anything. Likewise if pEList==NULL then
153410** it matches anything so always return true. Return false only
153411** if there is no match.
153412*/
153413static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
153414 int e;
153415 if( pIdList==0 || NEVER(pEList==0)(pEList==0) ) return 1;
153416 for(e=0; e<pEList->nExpr; e++){
153417 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
153418 }
153419 return 0;
153420}
153421
153422/*
153423** Return true if any TEMP triggers exist
153424*/
153425static int tempTriggersExist(sqlite3 *db){
153426 if( NEVER(db->aDb[1].pSchema==0)(db->aDb[1].pSchema==0) ) return 0;
153427 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)((&db->aDb[1].pSchema->trigHash)->first)==0 ) return 0;
153428 return 1;
153429}
153430
153431/*
153432** Return a list of all triggers on table pTab if there exists at least
153433** one trigger that must be fired when an operation of type 'op' is
153434** performed on the table, and, if that operation is an UPDATE, if at
153435** least one of the columns in pChanges is being modified.
153436*/
153437static SQLITE_NOINLINE__attribute__((noinline)) Trigger *triggersReallyExist(
153438 Parse *pParse, /* Parse context */
153439 Table *pTab, /* The table the contains the triggers */
153440 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
153441 ExprList *pChanges, /* Columns that change in an UPDATE statement */
153442 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
153443){
153444 int mask = 0;
153445 Trigger *pList = 0;
153446 Trigger *p;
153447
153448 pList = sqlite3TriggerList(pParse, pTab);
153449 assert( pList==0 || IsVirtual(pTab)==0((void) (0))
153450 || (pList->bReturning && pList->pNext==0) )((void) (0));
153451 if( pList!=0 ){
153452 p = pList;
153453 if( (pParse->db->flags & SQLITE_EnableTrigger0x00040000)==0
153454 && pTab->pTrigger!=0
153455 ){
153456 /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that
153457 ** only TEMP triggers are allowed. Truncate the pList so that it
153458 ** includes only TEMP triggers */
153459 if( pList==pTab->pTrigger ){
153460 pList = 0;
153461 goto exit_triggers_exist;
153462 }
153463 while( ALWAYS(p->pNext)(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
153464 p->pNext = 0;
153465 p = pList;
153466 }
153467 do{
153468 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
153469 mask |= p->tr_tm;
153470 }else if( p->op==TK_RETURNING151 ){
153471 /* The first time a RETURNING trigger is seen, the "op" value tells
153472 ** us what time of trigger it should be. */
153473 assert( sqlite3IsToplevel(pParse) )((void) (0));
153474 p->op = op;
153475 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
153476 if( op!=TK_INSERT128 ){
153477 sqlite3ErrorMsg(pParse,
153478 "%s RETURNING is not available on virtual tables",
153479 op==TK_DELETE129 ? "DELETE" : "UPDATE");
153480 }
153481 p->tr_tm = TRIGGER_BEFORE1;
153482 }else{
153483 p->tr_tm = TRIGGER_AFTER2;
153484 }
153485 mask |= p->tr_tm;
153486 }else if( p->bReturning && p->op==TK_INSERT128 && op==TK_UPDATE130
153487 && sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
153488 /* Also fire a RETURNING trigger for an UPSERT */
153489 mask |= p->tr_tm;
153490 }
153491 p = p->pNext;
153492 }while( p );
153493 }
153494exit_triggers_exist:
153495 if( pMask ){
153496 *pMask = mask;
153497 }
153498 return (mask ? pList : 0);
153499}
153500SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(
153501 Parse *pParse, /* Parse context */
153502 Table *pTab, /* The table the contains the triggers */
153503 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
153504 ExprList *pChanges, /* Columns that change in an UPDATE statement */
153505 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
153506){
153507 assert( pTab!=0 )((void) (0));
153508 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
153509 || pParse->disableTriggers
153510 ){
153511 if( pMask ) *pMask = 0;
153512 return 0;
153513 }
153514 return triggersReallyExist(pParse,pTab,op,pChanges,pMask);
153515}
153516
153517/*
153518** Convert the pStep->zTarget string into a SrcList and return a pointer
153519** to that SrcList.
153520**
153521** This routine adds a specific database name, if needed, to the target when
153522** forming the SrcList. This prevents a trigger in one database from
153523** referring to a target in another database. An exception is when the
153524** trigger is in TEMP in which case it can refer to any other database it
153525** wants.
153526*/
153527SQLITE_PRIVATEstatic SrcList *sqlite3TriggerStepSrc(
153528 Parse *pParse, /* The parsing context */
153529 TriggerStep *pStep /* The trigger containing the target token */
153530){
153531 sqlite3 *db = pParse->db;
153532 SrcList *pSrc; /* SrcList to be returned */
153533 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
153534 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
153535 assert( pSrc==0 || pSrc->nSrc==1 )((void) (0));
153536 assert( zName || pSrc==0 )((void) (0));
153537 if( pSrc ){
153538 Schema *pSchema = pStep->pTrig->pSchema;
153539 pSrc->a[0].zName = zName;
153540 if( pSchema!=db->aDb[1].pSchema ){
153541 assert( pSrc->a[0].fg.fixedSchema || pSrc->a[0].u4.zDatabase==0 )((void) (0));
153542 pSrc->a[0].u4.pSchema = pSchema;
153543 pSrc->a[0].fg.fixedSchema = 1;
153544 }
153545 if( pStep->pFrom ){
153546 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
153547 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
153548 Select *pSubquery;
153549 Token as;
153550 pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom0x0000800,0);
153551 as.n = 0;
153552 as.z = 0;
153553 pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
153554 }
153555 pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup);
153556 }
153557 }else{
153558 sqlite3DbFree(db, zName);
153559 }
153560 return pSrc;
153561}
153562
153563/*
153564** Return true if the pExpr term from the RETURNING clause argument
153565** list is of the form "*". Raise an error if the terms if of the
153566** form "table.*".
153567*/
153568static int isAsteriskTerm(
153569 Parse *pParse, /* Parsing context */
153570 Expr *pTerm /* A term in the RETURNING clause */
153571){
153572 assert( pTerm!=0 )((void) (0));
153573 if( pTerm->op==TK_ASTERISK180 ) return 1;
153574 if( pTerm->op!=TK_DOT142 ) return 0;
153575 assert( pTerm->pRight!=0 )((void) (0));
153576 assert( pTerm->pLeft!=0 )((void) (0));
153577 if( pTerm->pRight->op!=TK_ASTERISK180 ) return 0;
153578 sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards");
153579 return 1;
153580}
153581
153582/* The input list pList is the list of result set terms from a RETURNING
153583** clause. The table that we are returning from is pTab.
153584**
153585** This routine makes a copy of the pList, and at the same time expands
153586** any "*" wildcards to be the complete set of columns from pTab.
153587*/
153588static ExprList *sqlite3ExpandReturning(
153589 Parse *pParse, /* Parsing context */
153590 ExprList *pList, /* The arguments to RETURNING */
153591 Table *pTab /* The table being updated */
153592){
153593 ExprList *pNew = 0;
153594 sqlite3 *db = pParse->db;
153595 int i;
153596
153597 for(i=0; i<pList->nExpr; i++){
153598 Expr *pOldExpr = pList->a[i].pExpr;
153599 if( NEVER(pOldExpr==0)(pOldExpr==0) ) continue;
153600 if( isAsteriskTerm(pParse, pOldExpr) ){
153601 int jj;
153602 for(jj=0; jj<pTab->nCol; jj++){
153603 Expr *pNewExpr;
153604 if( IsHiddenColumn(pTab->aCol+jj)(((pTab->aCol+jj)->colFlags & 0x0002)!=0) ) continue;
153605 pNewExpr = sqlite3Expr(db, TK_ID60, pTab->aCol[jj].zCnName);
153606 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
153607 if( !db->mallocFailed ){
153608 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
153609 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
153610 pItem->fg.eEName = ENAME_NAME0;
153611 }
153612 }
153613 }else{
153614 Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0);
153615 pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr);
153616 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0)(pList->a[i].zEName!=0) ){
153617 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
153618 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
153619 pItem->fg.eEName = pList->a[i].fg.eEName;
153620 }
153621 }
153622 }
153623 return pNew;
153624}
153625
153626/* If the Expr node is a subquery or an EXISTS operator or an IN operator that
153627** uses a subquery, and if the subquery is SF_Correlated, then mark the
153628** expression as EP_VarSelect.
153629*/
153630static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){
153631 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
153632 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0)
153633 && (pExpr->x.pSelect->selFlags & SF_Correlated0x20000000)!=0
153634 ){
153635 testcase( ExprHasProperty(pExpr, EP_VarSelect) );
153636 ExprSetProperty(pExpr, EP_VarSelect)(pExpr)->flags|=(0x000040);
153637 }
153638 return WRC_Continue0;
153639}
153640
153641
153642/*
153643** If the SELECT references the table pWalker->u.pTab, then do two things:
153644**
153645** (1) Mark the SELECT as as SF_Correlated.
153646** (2) Set pWalker->eCode to non-zero so that the caller will know
153647** that (1) has happened.
153648*/
153649static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){
153650 int i;
153651 SrcList *pSrc;
153652 assert( pSelect!=0 )((void) (0));
153653 pSrc = pSelect->pSrc;
153654 assert( pSrc!=0 )((void) (0));
153655 for(i=0; i<pSrc->nSrc; i++){
153656 if( pSrc->a[i].pSTab==pWalker->u.pTab ){
153657 testcase( pSelect->selFlags & SF_Correlated );
153658 pSelect->selFlags |= SF_Correlated0x20000000;
153659 pWalker->eCode = 1;
153660 break;
153661 }
153662 }
153663 return WRC_Continue0;
153664}
153665
153666/*
153667** Scan the expression list that is the argument to RETURNING looking
153668** for subqueries that depend on the table which is being modified in the
153669** statement that is hosting the RETURNING clause (pTab). Mark all such
153670** subqueries as SF_Correlated. If the subqueries are part of an
153671** expression, mark the expression as EP_VarSelect.
153672**
153673** https://sqlite.org/forum/forumpost/2c83569ce8945d39
153674*/
153675static void sqlite3ProcessReturningSubqueries(
153676 ExprList *pEList,
153677 Table *pTab
153678){
153679 Walker w;
153680 memset(&w, 0, sizeof(w));
153681 w.xExprCallback = sqlite3ExprWalkNoop;
153682 w.xSelectCallback = sqlite3ReturningSubqueryCorrelated;
153683 w.u.pTab = pTab;
153684 sqlite3WalkExprList(&w, pEList);
153685 if( w.eCode ){
153686 w.xExprCallback = sqlite3ReturningSubqueryVarSelect;
153687 w.xSelectCallback = sqlite3SelectWalkNoop;
153688 sqlite3WalkExprList(&w, pEList);
153689 }
153690}
153691
153692/*
153693** Generate code for the RETURNING trigger. Unlike other triggers
153694** that invoke a subprogram in the bytecode, the code for RETURNING
153695** is generated in-line.
153696*/
153697static void codeReturningTrigger(
153698 Parse *pParse, /* Parse context */
153699 Trigger *pTrigger, /* The trigger step that defines the RETURNING */
153700 Table *pTab, /* The table to code triggers from */
153701 int regIn /* The first in an array of registers */
153702){
153703 Vdbe *v = pParse->pVdbe;
153704 sqlite3 *db = pParse->db;
153705 ExprList *pNew;
153706 Returning *pReturning;
153707 Select sSelect;
153708 SrcList sFrom;
153709
153710 assert( v!=0 )((void) (0));
153711 if( !pParse->bReturning ){
153712 /* This RETURNING trigger must be for a different statement as
153713 ** this statement lacks a RETURNING clause. */
153714 return;
153715 }
153716 assert( db->pParse==pParse )((void) (0));
153717 pReturning = pParse->u1.pReturning;
153718 if( pTrigger != &(pReturning->retTrig) ){
153719 /* This RETURNING trigger is for a different statement */
153720 return;
153721 }
153722 memset(&sSelect, 0, sizeof(sSelect));
153723 memset(&sFrom, 0, sizeof(sFrom));
153724 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
153725 sSelect.pSrc = &sFrom;
153726 sFrom.nSrc = 1;
153727 sFrom.a[0].pSTab = pTab;
153728 sFrom.a[0].zName = pTab->zName; /* tag-20240424-1 */
153729 sFrom.a[0].iCursor = -1;
153730 sqlite3SelectPrep(pParse, &sSelect, 0);
153731 if( pParse->nErr==0 ){
153732 assert( db->mallocFailed==0 )((void) (0));
153733 sqlite3GenerateColumnNames(pParse, &sSelect);
153734 }
153735 sqlite3ExprListDelete(db, sSelect.pEList);
153736 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
153737 if( pParse->nErr==0 ){
153738 NameContext sNC;
153739 memset(&sNC, 0, sizeof(sNC));
153740 if( pReturning->nRetCol==0 ){
153741 pReturning->nRetCol = pNew->nExpr;
153742 pReturning->iRetCur = pParse->nTab++;
153743 }
153744 sNC.pParse = pParse;
153745 sNC.uNC.iBaseReg = regIn;
153746 sNC.ncFlags = NC_UBaseReg0x000400;
153747 pParse->eTriggerOp = pTrigger->op;
153748 pParse->pTriggerTab = pTab;
153749 if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK0
153750 && ALWAYS(!db->mallocFailed)(!db->mallocFailed)
153751 ){
153752 int i;
153753 int nCol = pNew->nExpr;
153754 int reg = pParse->nMem+1;
153755 sqlite3ProcessReturningSubqueries(pNew, pTab);
153756 pParse->nMem += nCol+2;
153757 pReturning->iRetReg = reg;
153758 for(i=0; i<nCol; i++){
153759 Expr *pCol = pNew->a[i].pExpr;
153760 assert( pCol!=0 )((void) (0)); /* Due to !db->mallocFailed ~9 lines above */
153761 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
153762 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL0x45 ){
153763 sqlite3VdbeAddOp1(v, OP_RealAffinity87, reg+i);
153764 }
153765 }
153766 sqlite3VdbeAddOp3(v, OP_MakeRecord97, reg, i, reg+i);
153767 sqlite3VdbeAddOp2(v, OP_NewRowid127, pReturning->iRetCur, reg+i+1);
153768 sqlite3VdbeAddOp3(v, OP_Insert128, pReturning->iRetCur, reg+i, reg+i+1);
153769 }
153770 }
153771 sqlite3ExprListDelete(db, pNew);
153772 pParse->eTriggerOp = 0;
153773 pParse->pTriggerTab = 0;
153774}
153775
153776
153777
153778/*
153779** Generate VDBE code for the statements inside the body of a single
153780** trigger.
153781*/
153782static int codeTriggerProgram(
153783 Parse *pParse, /* The parser context */
153784 TriggerStep *pStepList, /* List of statements inside the trigger body */
153785 int orconf /* Conflict algorithm. (OE_Abort, etc) */
153786){
153787 TriggerStep *pStep;
153788 Vdbe *v = pParse->pVdbe;
153789 sqlite3 *db = pParse->db;
153790
153791 assert( pParse->pTriggerTab && pParse->pToplevel )((void) (0));
153792 assert( pStepList )((void) (0));
153793 assert( v!=0 )((void) (0));
153794 for(pStep=pStepList; pStep; pStep=pStep->pNext){
153795 /* Figure out the ON CONFLICT policy that will be used for this step
153796 ** of the trigger program. If the statement that caused this trigger
153797 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
153798 ** the ON CONFLICT policy that was specified as part of the trigger
153799 ** step statement. Example:
153800 **
153801 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
153802 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
153803 ** END;
153804 **
153805 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
153806 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
153807 */
153808 pParse->eOrconf = (orconf==OE_Default11)?pStep->orconf:(u8)orconf;
153809 assert( pParse->okConstFactor==0 )((void) (0));
153810
153811#ifndef SQLITE_OMIT_TRACE
153812 if( pStep->zSpan ){
153813 sqlite3VdbeAddOp4(v, OP_Trace184, 0x7fffffff, 1, 0,
153814 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
153815 P4_DYNAMIC(-6));
153816 }
153817#endif
153818
153819 switch( pStep->op ){
153820 case TK_UPDATE130: {
153821 sqlite3Update(pParse,
153822 sqlite3TriggerStepSrc(pParse, pStep),
153823 sqlite3ExprListDup(db, pStep->pExprList, 0),
153824 sqlite3ExprDup(db, pStep->pWhere, 0),
153825 pParse->eOrconf, 0, 0, 0
153826 );
153827 sqlite3VdbeAddOp0(v, OP_ResetCount131);
153828 break;
153829 }
153830 case TK_INSERT128: {
153831 sqlite3Insert(pParse,
153832 sqlite3TriggerStepSrc(pParse, pStep),
153833 sqlite3SelectDup(db, pStep->pSelect, 0),
153834 sqlite3IdListDup(db, pStep->pIdList),
153835 pParse->eOrconf,
153836 sqlite3UpsertDup(db, pStep->pUpsert)
153837 );
153838 sqlite3VdbeAddOp0(v, OP_ResetCount131);
153839 break;
153840 }
153841 case TK_DELETE129: {
153842 sqlite3DeleteFrom(pParse,
153843 sqlite3TriggerStepSrc(pParse, pStep),
153844 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
153845 );
153846 sqlite3VdbeAddOp0(v, OP_ResetCount131);
153847 break;
153848 }
153849 default: assert( pStep->op==TK_SELECT )((void) (0)); {
153850 SelectDest sDest;
153851 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
153852 sqlite3SelectDestInit(&sDest, SRT_Discard4, 0);
153853 sqlite3Select(pParse, pSelect, &sDest);
153854 sqlite3SelectDelete(db, pSelect);
153855 break;
153856 }
153857 }
153858 }
153859
153860 return 0;
153861}
153862
153863#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
153864/*
153865** This function is used to add VdbeComment() annotations to a VDBE
153866** program. It is not used in production code, only for debugging.
153867*/
153868static const char *onErrorText(int onError){
153869 switch( onError ){
153870 case OE_Abort2: return "abort";
153871 case OE_Rollback1: return "rollback";
153872 case OE_Fail3: return "fail";
153873 case OE_Replace5: return "replace";
153874 case OE_Ignore4: return "ignore";
153875 case OE_Default11: return "default";
153876 }
153877 return "n/a";
153878}
153879#endif
153880
153881/*
153882** Parse context structure pFrom has just been used to create a sub-vdbe
153883** (trigger program). If an error has occurred, transfer error information
153884** from pFrom to pTo.
153885*/
153886static void transferParseError(Parse *pTo, Parse *pFrom){
153887 assert( pFrom->zErrMsg==0 || pFrom->nErr )((void) (0));
153888 assert( pTo->zErrMsg==0 || pTo->nErr )((void) (0));
153889 if( pTo->nErr==0 ){
153890 pTo->zErrMsg = pFrom->zErrMsg;
153891 pTo->nErr = pFrom->nErr;
153892 pTo->rc = pFrom->rc;
153893 }else{
153894 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
153895 }
153896}
153897
153898/*
153899** Create and populate a new TriggerPrg object with a sub-program
153900** implementing trigger pTrigger with ON CONFLICT policy orconf.
153901*/
153902static TriggerPrg *codeRowTrigger(
153903 Parse *pParse, /* Current parse context */
153904 Trigger *pTrigger, /* Trigger to code */
153905 Table *pTab, /* The table pTrigger is attached to */
153906 int orconf /* ON CONFLICT policy to code trigger program with */
153907){
153908 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
153909 sqlite3 *db = pParse->db; /* Database handle */
153910 TriggerPrg *pPrg; /* Value to return */
153911 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
153912 Vdbe *v; /* Temporary VM */
153913 NameContext sNC; /* Name context for sub-vdbe */
153914 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
153915 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
153916 Parse sSubParse; /* Parse context for sub-vdbe */
153917
153918 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
153919 assert( pTop->pVdbe )((void) (0));
153920
153921 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
153922 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
153923 ** list of the top-level Parse object sooner rather than later. */
153924 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
153925 if( !pPrg ) return 0;
153926 pPrg->pNext = pTop->pTriggerPrg;
153927 pTop->pTriggerPrg = pPrg;
153928 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
153929 if( !pProgram ) return 0;
153930 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
153931 pPrg->pTrigger = pTrigger;
153932 pPrg->orconf = orconf;
153933 pPrg->aColmask[0] = 0xffffffff;
153934 pPrg->aColmask[1] = 0xffffffff;
153935
153936 /* Allocate and populate a new Parse context to use for coding the
153937 ** trigger sub-program. */
153938 sqlite3ParseObjectInit(&sSubParse, db);
153939 memset(&sNC, 0, sizeof(sNC));
153940 sNC.pParse = &sSubParse;
153941 sSubParse.pTriggerTab = pTab;
153942 sSubParse.pToplevel = pTop;
153943 sSubParse.zAuthContext = pTrigger->zName;
153944 sSubParse.eTriggerOp = pTrigger->op;
153945 sSubParse.nQueryLoop = pParse->nQueryLoop;
153946 sSubParse.prepFlags = pParse->prepFlags;
153947
153948 v = sqlite3GetVdbe(&sSubParse);
153949 if( v ){
153950 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
153951 pTrigger->zName, onErrorText(orconf),
153952 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
153953 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
153954 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
153955 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
153956 pTab->zName
153957 ));
153958#ifndef SQLITE_OMIT_TRACE
153959 if( pTrigger->zName ){
153960 sqlite3VdbeChangeP4(v, -1,
153961 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC(-6)
153962 );
153963 }
153964#endif
153965
153966 /* If one was specified, code the WHEN clause. If it evaluates to false
153967 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
153968 ** OP_Halt inserted at the end of the program. */
153969 if( pTrigger->pWhen ){
153970 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
153971 if( db->mallocFailed==0
153972 && SQLITE_OK0==sqlite3ResolveExprNames(&sNC, pWhen)
153973 ){
153974 iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse);
153975 sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL0x10);
153976 }
153977 sqlite3ExprDelete(db, pWhen);
153978 }
153979
153980 /* Code the trigger program into the sub-vdbe. */
153981 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
153982
153983 /* Insert an OP_Halt at the end of the sub-program. */
153984 if( iEndTrigger ){
153985 sqlite3VdbeResolveLabel(v, iEndTrigger);
153986 }
153987 sqlite3VdbeAddOp0(v, OP_Halt70);
153988 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
153989 transferParseError(pParse, &sSubParse);
153990
153991 if( pParse->nErr==0 ){
153992 assert( db->mallocFailed==0 )((void) (0));
153993 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
153994 }
153995 pProgram->nMem = sSubParse.nMem;
153996 pProgram->nCsr = sSubParse.nTab;
153997 pProgram->token = (void *)pTrigger;
153998 pPrg->aColmask[0] = sSubParse.oldmask;
153999 pPrg->aColmask[1] = sSubParse.newmask;
154000 sqlite3VdbeDelete(v);
154001 }else{
154002 transferParseError(pParse, &sSubParse);
154003 }
154004
154005 assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg )((void) (0));
154006 sqlite3ParseObjectReset(&sSubParse);
154007 return pPrg;
154008}
154009
154010/*
154011** Return a pointer to a TriggerPrg object containing the sub-program for
154012** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
154013** TriggerPrg object exists, a new object is allocated and populated before
154014** being returned.
154015*/
154016static TriggerPrg *getRowTrigger(
154017 Parse *pParse, /* Current parse context */
154018 Trigger *pTrigger, /* Trigger to code */
154019 Table *pTab, /* The table trigger pTrigger is attached to */
154020 int orconf /* ON CONFLICT algorithm. */
154021){
154022 Parse *pRoot = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
154023 TriggerPrg *pPrg;
154024
154025 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
154026
154027 /* It may be that this trigger has already been coded (or is in the
154028 ** process of being coded). If this is the case, then an entry with
154029 ** a matching TriggerPrg.pTrigger field will be present somewhere
154030 ** in the Parse.pTriggerPrg list. Search for such an entry. */
154031 for(pPrg=pRoot->pTriggerPrg;
154032 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
154033 pPrg=pPrg->pNext
154034 );
154035
154036 /* If an existing TriggerPrg could not be located, create a new one. */
154037 if( !pPrg ){
154038 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
154039 pParse->db->errByteOffset = -1;
154040 }
154041
154042 return pPrg;
154043}
154044
154045/*
154046** Generate code for the trigger program associated with trigger p on
154047** table pTab. The reg, orconf and ignoreJump parameters passed to this
154048** function are the same as those described in the header function for
154049** sqlite3CodeRowTrigger()
154050*/
154051SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(
154052 Parse *pParse, /* Parse context */
154053 Trigger *p, /* Trigger to code */
154054 Table *pTab, /* The table to code triggers from */
154055 int reg, /* Reg array containing OLD.* and NEW.* values */
154056 int orconf, /* ON CONFLICT policy */
154057 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
154058){
154059 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
154060 TriggerPrg *pPrg;
154061 pPrg = getRowTrigger(pParse, p, pTab, orconf);
154062 assert( pPrg || pParse->nErr )((void) (0));
154063
154064 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
154065 ** is a pointer to the sub-vdbe containing the trigger program. */
154066 if( pPrg ){
154067 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers0x00002000));
154068
154069 sqlite3VdbeAddOp4(v, OP_Program48, reg, ignoreJump, ++pParse->nMem,
154070 (const char *)pPrg->pProgram, P4_SUBPROGRAM(-4));
154071 VdbeComment(
154072 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
154073
154074 /* Set the P5 operand of the OP_Program instruction to non-zero if
154075 ** recursive invocation of this trigger program is disallowed. Recursive
154076 ** invocation is disallowed if (a) the sub-program is really a trigger,
154077 ** not a foreign key action, and (b) the flag to enable recursive triggers
154078 ** is clear. */
154079 sqlite3VdbeChangeP5(v, (u8)bRecursive);
154080 }
154081}
154082
154083/*
154084** This is called to code the required FOR EACH ROW triggers for an operation
154085** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
154086** is given by the op parameter. The tr_tm parameter determines whether the
154087** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
154088** parameter pChanges is passed the list of columns being modified.
154089**
154090** If there are no triggers that fire at the specified time for the specified
154091** operation on pTab, this function is a no-op.
154092**
154093** The reg argument is the address of the first in an array of registers
154094** that contain the values substituted for the new.* and old.* references
154095** in the trigger program. If N is the number of columns in table pTab
154096** (a copy of pTab->nCol), then registers are populated as follows:
154097**
154098** Register Contains
154099** ------------------------------------------------------
154100** reg+0 OLD.rowid
154101** reg+1 OLD.* value of left-most column of pTab
154102** ... ...
154103** reg+N OLD.* value of right-most column of pTab
154104** reg+N+1 NEW.rowid
154105** reg+N+2 NEW.* value of left-most column of pTab
154106** ... ...
154107** reg+N+N+1 NEW.* value of right-most column of pTab
154108**
154109** For ON DELETE triggers, the registers containing the NEW.* values will
154110** never be accessed by the trigger program, so they are not allocated or
154111** populated by the caller (there is no data to populate them with anyway).
154112** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
154113** are never accessed, and so are not allocated by the caller. So, for an
154114** ON INSERT trigger, the value passed to this function as parameter reg
154115** is not a readable register, although registers (reg+N) through
154116** (reg+N+N+1) are.
154117**
154118** Parameter orconf is the default conflict resolution algorithm for the
154119** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
154120** is the instruction that control should jump to if a trigger program
154121** raises an IGNORE exception.
154122*/
154123SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(
154124 Parse *pParse, /* Parse context */
154125 Trigger *pTrigger, /* List of triggers on table pTab */
154126 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
154127 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
154128 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
154129 Table *pTab, /* The table to code triggers from */
154130 int reg, /* The first in an array of registers (see above) */
154131 int orconf, /* ON CONFLICT policy */
154132 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
154133){
154134 Trigger *p; /* Used to iterate through pTrigger list */
154135
154136 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE )((void) (0));
154137 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER )((void) (0));
154138 assert( (op==TK_UPDATE)==(pChanges!=0) )((void) (0));
154139
154140 for(p=pTrigger; p; p=p->pNext){
154141
154142 /* Sanity checking: The schema for the trigger and for the table are
154143 ** always defined. The trigger must be in the same schema as the table
154144 ** or else it must be a TEMP trigger. */
154145 assert( p->pSchema!=0 )((void) (0));
154146 assert( p->pTabSchema!=0 )((void) (0));
154147 assert( p->pSchema==p->pTabSchema((void) (0))
154148 || p->pSchema==pParse->db->aDb[1].pSchema )((void) (0));
154149
154150 /* Determine whether we should code this trigger. One of two choices:
154151 ** 1. The trigger is an exact match to the current DML statement
154152 ** 2. This is a RETURNING trigger for INSERT but we are currently
154153 ** doing the UPDATE part of an UPSERT.
154154 */
154155 if( (p->op==op || (p->bReturning && p->op==TK_INSERT128 && op==TK_UPDATE130))
154156 && p->tr_tm==tr_tm
154157 && checkColumnOverlap(p->pColumns, pChanges)
154158 ){
154159 if( !p->bReturning ){
154160 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
154161 }else if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
154162 codeReturningTrigger(pParse, p, pTab, reg);
154163 }
154164 }
154165 }
154166}
154167
154168/*
154169** Triggers may access values stored in the old.* or new.* pseudo-table.
154170** This function returns a 32-bit bitmask indicating which columns of the
154171** old.* or new.* tables actually are used by triggers. This information
154172** may be used by the caller, for example, to avoid having to load the entire
154173** old.* record into memory when executing an UPDATE or DELETE command.
154174**
154175** Bit 0 of the returned mask is set if the left-most column of the
154176** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
154177** the second leftmost column value is required, and so on. If there
154178** are more than 32 columns in the table, and at least one of the columns
154179** with an index greater than 32 may be accessed, 0xffffffff is returned.
154180**
154181** It is not possible to determine if the old.rowid or new.rowid column is
154182** accessed by triggers. The caller must always assume that it is.
154183**
154184** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
154185** applies to the old.* table. If 1, the new.* table.
154186**
154187** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
154188** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
154189** included in the returned mask if the TRIGGER_BEFORE bit is set in the
154190** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
154191** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
154192*/
154193SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(
154194 Parse *pParse, /* Parse context */
154195 Trigger *pTrigger, /* List of triggers on table pTab */
154196 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
154197 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
154198 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
154199 Table *pTab, /* The table to code triggers from */
154200 int orconf /* Default ON CONFLICT policy for trigger steps */
154201){
154202 const int op = pChanges ? TK_UPDATE130 : TK_DELETE129;
154203 u32 mask = 0;
154204 Trigger *p;
154205
154206 assert( isNew==1 || isNew==0 )((void) (0));
154207 if( IsView(pTab)((pTab)->eTabType==2) ){
154208 return 0xffffffff;
154209 }
154210 for(p=pTrigger; p; p=p->pNext){
154211 if( p->op==op
154212 && (tr_tm&p->tr_tm)
154213 && checkColumnOverlap(p->pColumns,pChanges)
154214 ){
154215 if( p->bReturning ){
154216 mask = 0xffffffff;
154217 }else{
154218 TriggerPrg *pPrg;
154219 pPrg = getRowTrigger(pParse, p, pTab, orconf);
154220 if( pPrg ){
154221 mask |= pPrg->aColmask[isNew];
154222 }
154223 }
154224 }
154225 }
154226
154227 return mask;
154228}
154229
154230#endif /* !defined(SQLITE_OMIT_TRIGGER) */
154231
154232/************** End of trigger.c *********************************************/
154233/************** Begin file update.c ******************************************/
154234/*
154235** 2001 September 15
154236**
154237** The author disclaims copyright to this source code. In place of
154238** a legal notice, here is a blessing:
154239**
154240** May you do good and not evil.
154241** May you find forgiveness for yourself and forgive others.
154242** May you share freely, never taking more than you give.
154243**
154244*************************************************************************
154245** This file contains C code routines that are called by the parser
154246** to handle UPDATE statements.
154247*/
154248/* #include "sqliteInt.h" */
154249
154250#ifndef SQLITE_OMIT_VIRTUALTABLE
154251/* Forward declaration */
154252static void updateVirtualTable(
154253 Parse *pParse, /* The parsing context */
154254 SrcList *pSrc, /* The virtual table to be modified */
154255 Table *pTab, /* The virtual table */
154256 ExprList *pChanges, /* The columns to change in the UPDATE statement */
154257 Expr *pRowidExpr, /* Expression used to recompute the rowid */
154258 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
154259 Expr *pWhere, /* WHERE clause of the UPDATE statement */
154260 int onError /* ON CONFLICT strategy */
154261);
154262#endif /* SQLITE_OMIT_VIRTUALTABLE */
154263
154264/*
154265** The most recently coded instruction was an OP_Column to retrieve the
154266** i-th column of table pTab. This routine sets the P4 parameter of the
154267** OP_Column to the default value, if any.
154268**
154269** The default value of a column is specified by a DEFAULT clause in the
154270** column definition. This was either supplied by the user when the table
154271** was created, or added later to the table definition by an ALTER TABLE
154272** command. If the latter, then the row-records in the table btree on disk
154273** may not contain a value for the column and the default value, taken
154274** from the P4 parameter of the OP_Column instruction, is returned instead.
154275** If the former, then all row-records are guaranteed to include a value
154276** for the column and the P4 value is not required.
154277**
154278** Column definitions created by an ALTER TABLE command may only have
154279** literal default values specified: a number, null or a string. (If a more
154280** complicated default expression value was provided, it is evaluated
154281** when the ALTER TABLE is executed and one of the literal values written
154282** into the sqlite_schema table.)
154283**
154284** Therefore, the P4 parameter is only required if the default value for
154285** the column is a literal number, string or null. The sqlite3ValueFromExpr()
154286** function is capable of transforming these types of expressions into
154287** sqlite3_value objects.
154288**
154289** If column as REAL affinity and the table is an ordinary b-tree table
154290** (not a virtual table) then the value might have been stored as an
154291** integer. In that case, add an OP_RealAffinity opcode to make sure
154292** it has been converted into REAL.
154293*/
154294SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
154295 Column *pCol;
154296 assert( pTab!=0 )((void) (0));
154297 assert( pTab->nCol>i )((void) (0));
154298 pCol = &pTab->aCol[i];
154299 if( pCol->iDflt ){
154300 sqlite3_value *pValue = 0;
154301 u8 enc = ENC(sqlite3VdbeDb(v))((sqlite3VdbeDb(v))->enc);
154302 assert( !IsView(pTab) )((void) (0));
154303 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
154304 assert( i<pTab->nCol )((void) (0));
154305 sqlite3ValueFromExpr(sqlite3VdbeDb(v),
154306 sqlite3ColumnExpr(pTab,pCol), enc,
154307 pCol->affinity, &pValue);
154308 if( pValue ){
154309 sqlite3VdbeAppendP4(v, pValue, P4_MEM(-10));
154310 }
154311 }
154312#ifndef SQLITE_OMIT_FLOATING_POINT
154313 if( pCol->affinity==SQLITE_AFF_REAL0x45 && !IsVirtual(pTab)((pTab)->eTabType==1) ){
154314 sqlite3VdbeAddOp1(v, OP_RealAffinity87, iReg);
154315 }
154316#endif
154317}
154318
154319/*
154320** Check to see if column iCol of index pIdx references any of the
154321** columns defined by aXRef and chngRowid. Return true if it does
154322** and false if not. This is an optimization. False-positives are a
154323** performance degradation, but false-negatives can result in a corrupt
154324** index and incorrect answers.
154325**
154326** aXRef[j] will be non-negative if column j of the original table is
154327** being updated. chngRowid will be true if the rowid of the table is
154328** being updated.
154329*/
154330static int indexColumnIsBeingUpdated(
154331 Index *pIdx, /* The index to check */
154332 int iCol, /* Which column of the index to check */
154333 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
154334 int chngRowid /* true if the rowid is being updated */
154335){
154336 i16 iIdxCol = pIdx->aiColumn[iCol];
154337 assert( iIdxCol!=XN_ROWID )((void) (0)); /* Cannot index rowid */
154338 if( iIdxCol>=0 ){
154339 return aXRef[iIdxCol]>=0;
154340 }
154341 assert( iIdxCol==XN_EXPR )((void) (0));
154342 assert( pIdx->aColExpr!=0 )((void) (0));
154343 assert( pIdx->aColExpr->a[iCol].pExpr!=0 )((void) (0));
154344 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
154345 aXRef,chngRowid);
154346}
154347
154348/*
154349** Check to see if index pIdx is a partial index whose conditional
154350** expression might change values due to an UPDATE. Return true if
154351** the index is subject to change and false if the index is guaranteed
154352** to be unchanged. This is an optimization. False-positives are a
154353** performance degradation, but false-negatives can result in a corrupt
154354** index and incorrect answers.
154355**
154356** aXRef[j] will be non-negative if column j of the original table is
154357** being updated. chngRowid will be true if the rowid of the table is
154358** being updated.
154359*/
154360static int indexWhereClauseMightChange(
154361 Index *pIdx, /* The index to check */
154362 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
154363 int chngRowid /* true if the rowid is being updated */
154364){
154365 if( pIdx->pPartIdxWhere==0 ) return 0;
154366 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
154367 aXRef, chngRowid);
154368}
154369
154370/*
154371** Allocate and return a pointer to an expression of type TK_ROW with
154372** Expr.iColumn set to value (iCol+1). The resolver will modify the
154373** expression to be a TK_COLUMN reading column iCol of the first
154374** table in the source-list (pSrc->a[0]).
154375*/
154376static Expr *exprRowColumn(Parse *pParse, int iCol){
154377 Expr *pRet = sqlite3PExpr(pParse, TK_ROW76, 0, 0);
154378 if( pRet ) pRet->iColumn = iCol+1;
154379 return pRet;
154380}
154381
154382/*
154383** Assuming both the pLimit and pOrderBy parameters are NULL, this function
154384** generates VM code to run the query:
154385**
154386** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
154387**
154388** and write the results to the ephemeral table already opened as cursor
154389** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
154390** this function, they must be deleted by the caller.
154391**
154392** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
154393**
154394** SELECT <other-columns>, pChanges FROM pTabList
154395** WHERE pWhere
154396** GROUP BY <other-columns>
154397** ORDER BY pOrderBy LIMIT pLimit
154398**
154399** If pTab is a view, the GROUP BY clause is omitted.
154400**
154401** Exactly how results are written to table iEph, and exactly what
154402** the <other-columns> in the query above are is determined by the type
154403** of table pTabList->a[0].pTab.
154404**
154405** If the table is a WITHOUT ROWID table, then argument pPk must be its
154406** PRIMARY KEY. In this case <other-columns> are the primary key columns
154407** of the table, in order. The results of the query are written to ephemeral
154408** table iEph as index keys, using OP_IdxInsert.
154409**
154410** If the table is actually a view, then <other-columns> are all columns of
154411** the view. The results are written to the ephemeral table iEph as records
154412** with automatically assigned integer keys.
154413**
154414** If the table is a virtual or ordinary intkey table, then <other-columns>
154415** is its rowid. For a virtual table, the results are written to iEph as
154416** records with automatically assigned integer keys For intkey tables, the
154417** rowid value in <other-columns> is used as the integer key, and the
154418** remaining fields make up the table record.
154419*/
154420static void updateFromSelect(
154421 Parse *pParse, /* Parse context */
154422 int iEph, /* Cursor for open eph. table */
154423 Index *pPk, /* PK if table 0 is WITHOUT ROWID */
154424 ExprList *pChanges, /* List of expressions to return */
154425 SrcList *pTabList, /* List of tables to select from */
154426 Expr *pWhere, /* WHERE clause for query */
154427 ExprList *pOrderBy, /* ORDER BY clause */
154428 Expr *pLimit /* LIMIT clause */
154429){
154430 int i;
154431 SelectDest dest;
154432 Select *pSelect = 0;
154433 ExprList *pList = 0;
154434 ExprList *pGrp = 0;
154435 Expr *pLimit2 = 0;
154436 ExprList *pOrderBy2 = 0;
154437 sqlite3 *db = pParse->db;
154438 Table *pTab = pTabList->a[0].pSTab;
154439 SrcList *pSrc;
154440 Expr *pWhere2;
154441 int eDest;
154442
154443#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
154444 if( pOrderBy && pLimit==0 ) {
154445 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE");
154446 return;
154447 }
154448 pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0);
154449 pLimit2 = sqlite3ExprDup(db, pLimit, 0);
154450#else
154451 UNUSED_PARAMETER(pOrderBy)(void)(pOrderBy);
154452 UNUSED_PARAMETER(pLimit)(void)(pLimit);
154453#endif
154454
154455 pSrc = sqlite3SrcListDup(db, pTabList, 0);
154456 pWhere2 = sqlite3ExprDup(db, pWhere, 0);
154457
154458 assert( pTabList->nSrc>1 )((void) (0));
154459 if( pSrc ){
154460 assert( pSrc->a[0].fg.notCte )((void) (0));
154461 pSrc->a[0].iCursor = -1;
154462 pSrc->a[0].pSTab->nTabRef--;
154463 pSrc->a[0].pSTab = 0;
154464 }
154465 if( pPk ){
154466 for(i=0; i<pPk->nKeyCol; i++){
154467 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
154468#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
154469 if( pLimit ){
154470 pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0));
154471 }
154472#endif
154473 pList = sqlite3ExprListAppend(pParse, pList, pNew);
154474 }
154475 eDest = IsVirtual(pTab)((pTab)->eTabType==1) ? SRT_Table14 : SRT_Upfrom15;
154476 }else if( IsView(pTab)((pTab)->eTabType==2) ){
154477 for(i=0; i<pTab->nCol; i++){
154478 pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i));
154479 }
154480 eDest = SRT_Table14;
154481 }else{
154482 eDest = IsVirtual(pTab)((pTab)->eTabType==1) ? SRT_Table14 : SRT_Upfrom15;
154483 pList = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW76,0,0));
154484#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
154485 if( pLimit ){
154486 pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW76,0,0));
154487 }
154488#endif
154489 }
154490 assert( pChanges!=0 || pParse->db->mallocFailed )((void) (0));
154491 if( pChanges ){
154492 for(i=0; i<pChanges->nExpr; i++){
154493 pList = sqlite3ExprListAppend(pParse, pList,
154494 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
154495 );
154496 }
154497 }
154498 pSelect = sqlite3SelectNew(pParse, pList,
154499 pSrc, pWhere2, pGrp, 0, pOrderBy2,
154500 SF_UFSrcCheck0x0800000|SF_IncludeHidden0x0020000|SF_UpdateFrom0x10000000, pLimit2
154501 );
154502 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd0x8000000;
154503 sqlite3SelectDestInit(&dest, eDest, iEph);
154504 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
154505 sqlite3Select(pParse, pSelect, &dest);
154506 sqlite3SelectDelete(db, pSelect);
154507}
154508
154509/*
154510** Process an UPDATE statement.
154511**
154512** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
154513** \_______/ \_/ \______/ \_____/ \________________/
154514** onError | pChanges | pWhere
154515** \_______________________/
154516** pTabList
154517*/
154518SQLITE_PRIVATEstatic void sqlite3Update(
154519 Parse *pParse, /* The parser context */
154520 SrcList *pTabList, /* The table in which we should change things */
154521 ExprList *pChanges, /* Things to be changed */
154522 Expr *pWhere, /* The WHERE clause. May be null */
154523 int onError, /* How to handle constraint errors */
154524 ExprList *pOrderBy, /* ORDER BY clause. May be null */
154525 Expr *pLimit, /* LIMIT clause. May be null */
154526 Upsert *pUpsert /* ON CONFLICT clause, or null */
154527){
154528 int i, j, k; /* Loop counters */
154529 Table *pTab; /* The table to be updated */
154530 int addrTop = 0; /* VDBE instruction address of the start of the loop */
154531 WhereInfo *pWInfo = 0; /* Information about the WHERE clause */
154532 Vdbe *v; /* The virtual database engine */
154533 Index *pIdx; /* For looping over indices */
154534 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
154535 int nIdx; /* Number of indices that need updating */
154536 int nAllIdx; /* Total number of indexes */
154537 int iBaseCur; /* Base cursor number */
154538 int iDataCur; /* Cursor for the canonical data btree */
154539 int iIdxCur; /* Cursor for the first index */
154540 sqlite3 *db; /* The database structure */
154541 int *aRegIdx = 0; /* Registers for to each index and the main table */
154542 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
154543 ** an expression for the i-th column of the table.
154544 ** aXRef[i]==-1 if the i-th column is not changed. */
154545 u8 *aToOpen; /* 1 for tables and indices to be opened */
154546 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
154547 u8 chngRowid; /* Rowid changed in a normal table */
154548 u8 chngKey; /* Either chngPk or chngRowid */
154549 Expr *pRowidExpr = 0; /* Expression defining the new record number */
154550 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
154551 AuthContext sContext; /* The authorization context */
154552 NameContext sNC; /* The name-context to resolve expressions in */
154553 int iDb; /* Database containing the table being updated */
154554 int eOnePass; /* ONEPASS_XXX value from where.c */
154555 int hasFK; /* True if foreign key processing is required */
154556 int labelBreak; /* Jump here to break out of UPDATE loop */
154557 int labelContinue; /* Jump here to continue next step of UPDATE loop */
154558 int flags; /* Flags for sqlite3WhereBegin() */
154559
154560#ifndef SQLITE_OMIT_TRIGGER
154561 int isView; /* True when updating a view (INSTEAD OF trigger) */
154562 Trigger *pTrigger; /* List of triggers on pTab, if required */
154563 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
154564#endif
154565 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
154566 int iEph = 0; /* Ephemeral table holding all primary key values */
154567 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
154568 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
154569 int addrOpen = 0; /* Address of OP_OpenEphemeral */
154570 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
154571 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
154572 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
154573 int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */
154574 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
154575
154576 /* Register Allocations */
154577 int regRowCount = 0; /* A count of rows changed */
154578 int regOldRowid = 0; /* The old rowid */
154579 int regNewRowid = 0; /* The new rowid */
154580 int regNew = 0; /* Content of the NEW.* table in triggers */
154581 int regOld = 0; /* Content of OLD.* table in triggers */
154582 int regRowSet = 0; /* Rowset of rows to be updated */
154583 int regKey = 0; /* composite PRIMARY KEY value */
154584
154585 memset(&sContext, 0, sizeof(sContext));
154586 db = pParse->db;
154587 assert( db->pParse==pParse )((void) (0));
154588 if( pParse->nErr ){
154589 goto update_cleanup;
154590 }
154591 assert( db->mallocFailed==0 )((void) (0));
154592
154593 /* Locate the table which we want to update.
154594 */
154595 pTab = sqlite3SrcListLookup(pParse, pTabList);
154596 if( pTab==0 ) goto update_cleanup;
154597 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
154598
154599 /* Figure out if we have any triggers and if the table being
154600 ** updated is a view.
154601 */
154602#ifndef SQLITE_OMIT_TRIGGER
154603 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE130, pChanges, &tmask);
154604 isView = IsView(pTab)((pTab)->eTabType==2);
154605 assert( pTrigger || tmask==0 )((void) (0));
154606#else
154607# define pTrigger 0
154608# define isView 0
154609# define tmask 0
154610#endif
154611#ifdef SQLITE_OMIT_VIEW
154612# undef isView
154613# define isView 0
154614#endif
154615
154616#if TREETRACE_ENABLED0
154617 if( sqlite3TreeTrace & 0x10000 ){
154618 sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__"3rdparty/sqlite3/sqlite3.c", __LINE__154618);
154619 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
154620 onError, pOrderBy, pLimit, pUpsert, pTrigger);
154621 }
154622#endif
154623
154624 /* If there was a FROM clause, set nChangeFrom to the number of expressions
154625 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
154626 ** clause if this function is being called to generate code for part of
154627 ** an UPSERT statement. */
154628 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
154629 assert( nChangeFrom==0 || pUpsert==0 )((void) (0));
154630
154631#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT1
154632 if( !isView && nChangeFrom==0 ){
154633 pWhere = sqlite3LimitWhere(
154634 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
154635 );
154636 pOrderBy = 0;
154637 pLimit = 0;
154638 }
154639#endif
154640
154641 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
154642 goto update_cleanup;
154643 }
154644 if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){
154645 goto update_cleanup;
154646 }
154647
154648 /* Allocate a cursors for the main database table and for all indices.
154649 ** The index cursors might not be used, but if they are used they
154650 ** need to occur right after the database cursor. So go ahead and
154651 ** allocate enough space, just in case.
154652 */
154653 iBaseCur = iDataCur = pParse->nTab++;
154654 iIdxCur = iDataCur+1;
154655 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
154656 testcase( pPk!=0 && pPk!=pTab->pIndex );
154657 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
154658 if( pPk==pIdx ){
154659 iDataCur = pParse->nTab;
154660 }
154661 pParse->nTab++;
154662 }
154663 if( pUpsert ){
154664 /* On an UPSERT, reuse the same cursors already opened by INSERT */
154665 iDataCur = pUpsert->iDataCur;
154666 iIdxCur = pUpsert->iIdxCur;
154667 pParse->nTab = iBaseCur;
154668 }
154669 pTabList->a[0].iCursor = iDataCur;
154670
154671 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
154672 ** Initialize aXRef[] and aToOpen[] to their default values.
154673 */
154674 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
154675 if( aXRef==0 ) goto update_cleanup;
154676 aRegIdx = aXRef+pTab->nCol;
154677 aToOpen = (u8*)(aRegIdx+nIdx+1);
154678 memset(aToOpen, 1, nIdx+1);
154679 aToOpen[nIdx+1] = 0;
154680 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
154681
154682 /* Initialize the name-context */
154683 memset(&sNC, 0, sizeof(sNC));
154684 sNC.pParse = pParse;
154685 sNC.pSrcList = pTabList;
154686 sNC.uNC.pUpsert = pUpsert;
154687 sNC.ncFlags = NC_UUpsert0x000200;
154688
154689 /* Begin generating code. */
154690 v = sqlite3GetVdbe(pParse);
154691 if( v==0 ) goto update_cleanup;
154692
154693 /* Resolve the column names in all the expressions of the
154694 ** of the UPDATE statement. Also find the column index
154695 ** for each column to be updated in the pChanges array. For each
154696 ** column to be updated, make sure we have authorization to change
154697 ** that column.
154698 */
154699 chngRowid = chngPk = 0;
154700 for(i=0; i<pChanges->nExpr; i++){
154701 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
154702 /* If this is an UPDATE with a FROM clause, do not resolve expressions
154703 ** here. The call to sqlite3Select() below will do that. */
154704 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
154705 goto update_cleanup;
154706 }
154707 for(j=0; j<pTab->nCol; j++){
154708 if( pTab->aCol[j].hName==hCol
154709 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
154710 ){
154711 if( j==pTab->iPKey ){
154712 chngRowid = 1;
154713 pRowidExpr = pChanges->a[i].pExpr;
154714 iRowidExpr = i;
154715 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY0x0001)!=0 ){
154716 chngPk = 1;
154717 }
154718#ifndef SQLITE_OMIT_GENERATED_COLUMNS
154719 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED0x0060 ){
154720 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
154721 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
154722 sqlite3ErrorMsg(pParse,
154723 "cannot UPDATE generated column \"%s\"",
154724 pTab->aCol[j].zCnName);
154725 goto update_cleanup;
154726 }
154727#endif
154728 aXRef[j] = i;
154729 break;
154730 }
154731 }
154732 if( j>=pTab->nCol ){
154733 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
154734 j = -1;
154735 chngRowid = 1;
154736 pRowidExpr = pChanges->a[i].pExpr;
154737 iRowidExpr = i;
154738 }else{
154739 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
154740 pParse->checkSchema = 1;
154741 goto update_cleanup;
154742 }
154743 }
154744#ifndef SQLITE_OMIT_AUTHORIZATION
154745 {
154746 int rc;
154747 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE23, pTab->zName,
154748 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
154749 db->aDb[iDb].zDbSName);
154750 if( rc==SQLITE_DENY1 ){
154751 goto update_cleanup;
154752 }else if( rc==SQLITE_IGNORE2 ){
154753 aXRef[j] = -1;
154754 }
154755 }
154756#endif
154757 }
154758 assert( (chngRowid & chngPk)==0 )((void) (0));
154759 assert( chngRowid==0 || chngRowid==1 )((void) (0));
154760 assert( chngPk==0 || chngPk==1 )((void) (0));
154761 chngKey = chngRowid + chngPk;
154762
154763#ifndef SQLITE_OMIT_GENERATED_COLUMNS
154764 /* Mark generated columns as changing if their generator expressions
154765 ** reference any changing column. The actual aXRef[] value for
154766 ** generated expressions is not used, other than to check to see that it
154767 ** is non-negative, so the value of aXRef[] for generated columns can be
154768 ** set to any non-negative number. We use 99999 so that the value is
154769 ** obvious when looking at aXRef[] in a symbolic debugger.
154770 */
154771 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
154772 int bProgress;
154773 testcase( pTab->tabFlags & TF_HasVirtual );
154774 testcase( pTab->tabFlags & TF_HasStored );
154775 do{
154776 bProgress = 0;
154777 for(i=0; i<pTab->nCol; i++){
154778 if( aXRef[i]>=0 ) continue;
154779 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060)==0 ) continue;
154780 if( sqlite3ExprReferencesUpdatedColumn(
154781 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
154782 aXRef, chngRowid)
154783 ){
154784 aXRef[i] = 99999;
154785 bProgress = 1;
154786 }
154787 }
154788 }while( bProgress );
154789 }
154790#endif
154791
154792 /* The SET expressions are not actually used inside the WHERE loop.
154793 ** So reset the colUsed mask. Unless this is a virtual table. In that
154794 ** case, set all bits of the colUsed mask (to ensure that the virtual
154795 ** table implementation makes all columns available).
154796 */
154797 pTabList->a[0].colUsed = IsVirtual(pTab)((pTab)->eTabType==1) ? ALLBITS((Bitmask)-1) : 0;
154798
154799 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
154800
154801 /* There is one entry in the aRegIdx[] array for each index on the table
154802 ** being updated. Fill in aRegIdx[] with a register number that will hold
154803 ** the key for accessing each index.
154804 */
154805 if( onError==OE_Replace5 ) bReplace = 1;
154806 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
154807 int reg;
154808 if( chngKey || hasFK>1 || pIdx==pPk
154809 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
154810 ){
154811 reg = ++pParse->nMem;
154812 pParse->nMem += pIdx->nColumn;
154813 }else{
154814 reg = 0;
154815 for(i=0; i<pIdx->nKeyCol; i++){
154816 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
154817 reg = ++pParse->nMem;
154818 pParse->nMem += pIdx->nColumn;
154819 if( onError==OE_Default11 && pIdx->onError==OE_Replace5 ){
154820 bReplace = 1;
154821 }
154822 break;
154823 }
154824 }
154825 }
154826 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
154827 aRegIdx[nAllIdx] = reg;
154828 }
154829 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
154830 if( bReplace ){
154831 /* If REPLACE conflict resolution might be invoked, open cursors on all
154832 ** indexes in case they are needed to delete records. */
154833 memset(aToOpen, 1, nIdx+1);
154834 }
154835
154836 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
154837 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
154838
154839 /* Allocate required registers. */
154840 if( !IsVirtual(pTab)((pTab)->eTabType==1) ){
154841 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
154842 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
154843 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
154844 ** table record is written. regRowSet holds the RowSet for the
154845 ** two-pass update algorithm. */
154846 assert( aRegIdx[nAllIdx]==pParse->nMem )((void) (0));
154847 regRowSet = aRegIdx[nAllIdx];
154848 regOldRowid = regNewRowid = ++pParse->nMem;
154849 if( chngPk || pTrigger || hasFK ){
154850 regOld = pParse->nMem + 1;
154851 pParse->nMem += pTab->nCol;
154852 }
154853 if( chngKey || pTrigger || hasFK ){
154854 regNewRowid = ++pParse->nMem;
154855 }
154856 regNew = pParse->nMem + 1;
154857 pParse->nMem += pTab->nCol;
154858 }
154859
154860 /* Start the view context. */
154861 if( isView ){
154862 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
154863 }
154864
154865 /* If we are trying to update a view, realize that view into
154866 ** an ephemeral table.
154867 */
154868#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
154869 if( nChangeFrom==0 && isView ){
154870 sqlite3MaterializeView(pParse, pTab,
154871 pWhere, pOrderBy, pLimit, iDataCur
154872 );
154873 pOrderBy = 0;
154874 pLimit = 0;
154875 }
154876#endif
154877
154878 /* Resolve the column names in all the expressions in the
154879 ** WHERE clause.
154880 */
154881 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pWhere) ){
154882 goto update_cleanup;
154883 }
154884
154885#ifndef SQLITE_OMIT_VIRTUALTABLE
154886 /* Virtual tables must be handled separately */
154887 if( IsVirtual(pTab)((pTab)->eTabType==1) ){
154888 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
154889 pWhere, onError);
154890 goto update_cleanup;
154891 }
154892#endif
154893
154894 /* Jump to labelBreak to abandon further processing of this UPDATE */
154895 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
154896
154897 /* Not an UPSERT. Normal processing. Begin by
154898 ** initialize the count of updated rows */
154899 if( (db->flags&SQLITE_CountRows((u64)(0x00001)<<32))!=0
154900 && !pParse->pTriggerTab
154901 && !pParse->nested
154902 && !pParse->bReturning
154903 && pUpsert==0
154904 ){
154905 regRowCount = ++pParse->nMem;
154906 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regRowCount);
154907 }
154908
154909 if( nChangeFrom==0 && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
154910 sqlite3VdbeAddOp3(v, OP_Null75, 0, regRowSet, regOldRowid);
154911 iEph = pParse->nTab++;
154912 addrOpen = sqlite3VdbeAddOp3(v, OP_OpenEphemeral117, iEph, 0, regRowSet);
154913 }else{
154914 assert( pPk!=0 || HasRowid(pTab) )((void) (0));
154915 nPk = pPk ? pPk->nKeyCol : 0;
154916 iPk = pParse->nMem+1;
154917 pParse->nMem += nPk;
154918 pParse->nMem += nChangeFrom;
154919 regKey = ++pParse->nMem;
154920 if( pUpsert==0 ){
154921 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
154922 iEph = pParse->nTab++;
154923 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null75, 0, iPk, iPk+nPk-1);
154924 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, iEph, nEphCol);
154925 if( pPk ){
154926 KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pPk);
154927 if( pKeyInfo ){
154928 pKeyInfo->nAllField = nEphCol;
154929 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
154930 }
154931 }
154932 if( nChangeFrom ){
154933 updateFromSelect(
154934 pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit
154935 );
154936#ifndef SQLITE_OMIT_SUBQUERY
154937 if( isView ) iDataCur = iEph;
154938#endif
154939 }
154940 }
154941 }
154942
154943 if( nChangeFrom ){
154944 sqlite3MultiWrite(pParse);
154945 eOnePass = ONEPASS_OFF0;
154946 nKey = nPk;
154947 regKey = iPk;
154948 }else{
154949 if( pUpsert ){
154950 /* If this is an UPSERT, then all cursors have already been opened by
154951 ** the outer INSERT and the data cursor should be pointing at the row
154952 ** that is to be updated. So bypass the code that searches for the
154953 ** row(s) to be updated.
154954 */
154955 pWInfo = 0;
154956 eOnePass = ONEPASS_SINGLE1;
154957 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL0x10);
154958 bFinishSeek = 0;
154959 }else{
154960 /* Begin the database scan.
154961 **
154962 ** Do not consider a single-pass strategy for a multi-row update if
154963 ** there is anything that might disrupt the cursor being used to do
154964 ** the UPDATE:
154965 ** (1) This is a nested UPDATE
154966 ** (2) There are triggers
154967 ** (3) There are FOREIGN KEY constraints
154968 ** (4) There are REPLACE conflict handlers
154969 ** (5) There are subqueries in the WHERE clause
154970 */
154971 flags = WHERE_ONEPASS_DESIRED0x0004;
154972 if( !pParse->nested
154973 && !pTrigger
154974 && !hasFK
154975 && !chngKey
154976 && !bReplace
154977 && (pWhere==0 || !ExprHasProperty(pWhere, EP_Subquery)(((pWhere)->flags&(0x400000))!=0))
154978 ){
154979 flags |= WHERE_ONEPASS_MULTIROW0x0008;
154980 }
154981 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);
154982 if( pWInfo==0 ) goto update_cleanup;
154983
154984 /* A one-pass strategy that might update more than one row may not
154985 ** be used if any column of the index used for the scan is being
154986 ** updated. Otherwise, if there is an index on "b", statements like
154987 ** the following could create an infinite loop:
154988 **
154989 ** UPDATE t1 SET b=b+1 WHERE b>?
154990 **
154991 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
154992 ** strategy that uses an index for which one or more columns are being
154993 ** updated. */
154994 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
154995 bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo);
154996 if( eOnePass!=ONEPASS_SINGLE1 ){
154997 sqlite3MultiWrite(pParse);
154998 if( eOnePass==ONEPASS_MULTI2 ){
154999 int iCur = aiCurOnePass[1];
155000 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
155001 eOnePass = ONEPASS_OFF0;
155002 }
155003 assert( iCur!=iDataCur || !HasRowid(pTab) )((void) (0));
155004 }
155005 }
155006 }
155007
155008 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
155009 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
155010 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
155011 ** leave it in register regOldRowid. */
155012 sqlite3VdbeAddOp2(v, OP_Rowid135, iDataCur, regOldRowid);
155013 if( eOnePass==ONEPASS_OFF0 ){
155014 aRegIdx[nAllIdx] = ++pParse->nMem;
155015 sqlite3VdbeAddOp3(v, OP_Insert128, iEph, regRowSet, regOldRowid);
155016 }else{
155017 if( ALWAYS(addrOpen)(addrOpen) ) sqlite3VdbeChangeToNoop(v, addrOpen);
155018 }
155019 }else{
155020 /* Read the PK of the current row into an array of registers. In
155021 ** ONEPASS_OFF mode, serialize the array into a record and store it in
155022 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
155023 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
155024 ** is not required) and leave the PK fields in the array of registers. */
155025 for(i=0; i<nPk; i++){
155026 assert( pPk->aiColumn[i]>=0 )((void) (0));
155027 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,
155028 pPk->aiColumn[i], iPk+i);
155029 }
155030 if( eOnePass ){
155031 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
155032 nKey = nPk;
155033 regKey = iPk;
155034 }else{
155035 sqlite3VdbeAddOp4(v, OP_MakeRecord97, iPk, nPk, regKey,
155036 sqlite3IndexAffinityStr(db, pPk), nPk);
155037 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, iEph, regKey, iPk, nPk);
155038 }
155039 }
155040 }
155041
155042 if( pUpsert==0 ){
155043 if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI2 ){
155044 sqlite3WhereEnd(pWInfo);
155045 }
155046
155047 if( !isView ){
155048 int addrOnce = 0;
155049 int iNotUsed1 = 0;
155050 int iNotUsed2 = 0;
155051
155052 /* Open every index that needs updating. */
155053 if( eOnePass!=ONEPASS_OFF0 ){
155054 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
155055 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
155056 }
155057
155058 if( eOnePass==ONEPASS_MULTI2 && (nIdx-(aiCurOnePass[1]>=0))>0 ){
155059 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
155060 }
155061 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite113, 0, iBaseCur,
155062 aToOpen, &iNotUsed1, &iNotUsed2);
155063 if( addrOnce ){
155064 sqlite3VdbeJumpHereOrPopInst(v, addrOnce);
155065 }
155066 }
155067
155068 /* Top of the update loop */
155069 if( eOnePass!=ONEPASS_OFF0 ){
155070 if( aiCurOnePass[0]!=iDataCur
155071 && aiCurOnePass[1]!=iDataCur
155072#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
155073 && !isView
155074#endif
155075 ){
155076 assert( pPk )((void) (0));
155077 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelBreak, regKey,nKey);
155078 VdbeCoverage(v);
155079 }
155080 if( eOnePass!=ONEPASS_SINGLE1 ){
155081 labelContinue = sqlite3VdbeMakeLabel(pParse);
155082 }
155083 sqlite3VdbeAddOp2(v, OP_IsNull51, pPk ? regKey : regOldRowid, labelBreak);
155084 VdbeCoverageIf(v, pPk==0);
155085 VdbeCoverageIf(v, pPk!=0);
155086 }else if( pPk || nChangeFrom ){
155087 labelContinue = sqlite3VdbeMakeLabel(pParse);
155088 sqlite3VdbeAddOp2(v, OP_Rewind36, iEph, labelBreak); VdbeCoverage(v);
155089 addrTop = sqlite3VdbeCurrentAddr(v);
155090 if( nChangeFrom ){
155091 if( !isView ){
155092 if( pPk ){
155093 for(i=0; i<nPk; i++){
155094 sqlite3VdbeAddOp3(v, OP_Column94, iEph, i, iPk+i);
155095 }
155096 sqlite3VdbeAddOp4Int(
155097 v, OP_NotFound28, iDataCur, labelContinue, iPk, nPk
155098 ); VdbeCoverage(v);
155099 }else{
155100 sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, regOldRowid);
155101 sqlite3VdbeAddOp3(
155102 v, OP_NotExists31, iDataCur, labelContinue, regOldRowid
155103 ); VdbeCoverage(v);
155104 }
155105 }
155106 }else{
155107 sqlite3VdbeAddOp2(v, OP_RowData134, iEph, regKey);
155108 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelContinue, regKey,0);
155109 VdbeCoverage(v);
155110 }
155111 }else{
155112 sqlite3VdbeAddOp2(v, OP_Rewind36, iEph, labelBreak); VdbeCoverage(v);
155113 labelContinue = sqlite3VdbeMakeLabel(pParse);
155114 addrTop = sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, regOldRowid);
155115 VdbeCoverage(v);
155116 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue, regOldRowid);
155117 VdbeCoverage(v);
155118 }
155119 }
155120
155121 /* If the rowid value will change, set register regNewRowid to
155122 ** contain the new value. If the rowid is not being modified,
155123 ** then regNewRowid is the same register as regOldRowid, which is
155124 ** already populated. */
155125 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid )((void) (0));
155126 if( chngRowid ){
155127 assert( iRowidExpr>=0 )((void) (0));
155128 if( nChangeFrom==0 ){
155129 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
155130 }else{
155131 sqlite3VdbeAddOp3(v, OP_Column94, iEph, iRowidExpr, regNewRowid);
155132 }
155133 sqlite3VdbeAddOp1(v, OP_MustBeInt13, regNewRowid); VdbeCoverage(v);
155134 }
155135
155136 /* Compute the old pre-UPDATE content of the row being changed, if that
155137 ** information is needed */
155138 if( chngPk || hasFK || pTrigger ){
155139 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
155140 oldmask |= sqlite3TriggerColmask(pParse,
155141 pTrigger, pChanges, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onError
155142 );
155143 for(i=0; i<pTab->nCol; i++){
155144 u32 colFlags = pTab->aCol[i].colFlags;
155145 k = sqlite3TableColumnToStorage(pTab, i) + regOld;
155146 if( oldmask==0xffffffff
155147 || (i<32 && (oldmask & MASKBIT32(i)(((unsigned int)1)<<(i)))!=0)
155148 || (colFlags & COLFLAG_PRIMKEY0x0001)!=0
155149 ){
155150 testcase( oldmask!=0xffffffff && i==31 );
155151 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
155152 }else{
155153 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
155154 }
155155 }
155156 if( chngRowid==0 && pPk==0 ){
155157#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
155158 if( isView ) sqlite3VdbeAddOp2(v, OP_Null75, 0, regOldRowid);
155159#endif
155160 sqlite3VdbeAddOp2(v, OP_Copy80, regOldRowid, regNewRowid);
155161 }
155162 }
155163
155164 /* Populate the array of registers beginning at regNew with the new
155165 ** row data. This array is used to check constants, create the new
155166 ** table and index records, and as the values for any new.* references
155167 ** made by triggers.
155168 **
155169 ** If there are one or more BEFORE triggers, then do not populate the
155170 ** registers associated with columns that are (a) not modified by
155171 ** this UPDATE statement and (b) not accessed by new.* references. The
155172 ** values for registers not modified by the UPDATE must be reloaded from
155173 ** the database after the BEFORE triggers are fired anyway (as the trigger
155174 ** may have modified them). So not loading those that are not going to
155175 ** be used eliminates some redundant opcodes.
155176 */
155177 newmask = sqlite3TriggerColmask(
155178 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE1, pTab, onError
155179 );
155180 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
155181 if( i==pTab->iPKey ){
155182 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
155183 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060)!=0 ){
155184 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) k--;
155185 }else{
155186 j = aXRef[i];
155187 if( j>=0 ){
155188 if( nChangeFrom ){
155189 int nOff = (isView ? pTab->nCol : nPk);
155190 assert( eOnePass==ONEPASS_OFF )((void) (0));
155191 sqlite3VdbeAddOp3(v, OP_Column94, iEph, nOff+j, k);
155192 }else{
155193 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
155194 }
155195 }else if( 0==(tmask&TRIGGER_BEFORE1) || i>31 || (newmask & MASKBIT32(i)(((unsigned int)1)<<(i))) ){
155196 /* This branch loads the value of a column that will not be changed
155197 ** into a register. This is done if there are no BEFORE triggers, or
155198 ** if there are one or more BEFORE triggers that use this value via
155199 ** a new.* reference in a trigger program.
155200 */
155201 testcase( i==31 );
155202 testcase( i==32 );
155203 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
155204 bFinishSeek = 0;
155205 }else{
155206 sqlite3VdbeAddOp2(v, OP_Null75, 0, k);
155207 }
155208 }
155209 }
155210#ifndef SQLITE_OMIT_GENERATED_COLUMNS
155211 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
155212 testcase( pTab->tabFlags & TF_HasVirtual );
155213 testcase( pTab->tabFlags & TF_HasStored );
155214 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
155215 }
155216#endif
155217
155218 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
155219 ** verified. One could argue that this is wrong.
155220 */
155221 if( tmask&TRIGGER_BEFORE1 ){
155222 sqlite3TableAffinity(v, pTab, regNew);
155223 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE130, pChanges,
155224 TRIGGER_BEFORE1, pTab, regOldRowid, onError, labelContinue);
155225
155226 if( !isView ){
155227 /* The row-trigger may have deleted the row being updated. In this
155228 ** case, jump to the next row. No updates or AFTER triggers are
155229 ** required. This behavior - what happens when the row being updated
155230 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
155231 ** documentation.
155232 */
155233 if( pPk ){
155234 sqlite3VdbeAddOp4Int(v, OP_NotFound28,iDataCur,labelContinue,regKey,nKey);
155235 VdbeCoverage(v);
155236 }else{
155237 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue,regOldRowid);
155238 VdbeCoverage(v);
155239 }
155240
155241 /* After-BEFORE-trigger-reload-loop:
155242 ** If it did not delete it, the BEFORE trigger may still have modified
155243 ** some of the columns of the row being updated. Load the values for
155244 ** all columns not modified by the update statement into their registers
155245 ** in case this has happened. Only unmodified columns are reloaded.
155246 ** The values computed for modified columns use the values before the
155247 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
155248 ** for an example.
155249 */
155250 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
155251 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED0x0060 ){
155252 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL0x0020 ) k--;
155253 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
155254 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k);
155255 }
155256 }
155257#ifndef SQLITE_OMIT_GENERATED_COLUMNS
155258 if( pTab->tabFlags & TF_HasGenerated0x00000060 ){
155259 testcase( pTab->tabFlags & TF_HasVirtual );
155260 testcase( pTab->tabFlags & TF_HasStored );
155261 sqlite3ComputeGeneratedColumns(pParse, regNew, pTab);
155262 }
155263#endif
155264 }
155265 }
155266
155267 if( !isView ){
155268 /* Do constraint checks. */
155269 assert( regOldRowid>0 )((void) (0));
155270 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
155271 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
155272 aXRef, 0);
155273
155274 /* If REPLACE conflict handling may have been used, or if the PK of the
155275 ** row is changing, then the GenerateConstraintChecks() above may have
155276 ** moved cursor iDataCur. Reseek it. */
155277 if( bReplace || chngKey ){
155278 if( pPk ){
155279 sqlite3VdbeAddOp4Int(v, OP_NotFound28,iDataCur,labelContinue,regKey,nKey);
155280 }else{
155281 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue,regOldRowid);
155282 }
155283 VdbeCoverage(v);
155284 }
155285
155286 /* Do FK constraint checks. */
155287 if( hasFK ){
155288 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
155289 }
155290
155291 /* Delete the index entries associated with the current record. */
155292 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
155293
155294 /* We must run the OP_FinishSeek opcode to resolve a prior
155295 ** OP_DeferredSeek if there is any possibility that there have been
155296 ** no OP_Column opcodes since the OP_DeferredSeek was issued. But
155297 ** we want to avoid the OP_FinishSeek if possible, as running it
155298 ** costs CPU cycles. */
155299 if( bFinishSeek ){
155300 sqlite3VdbeAddOp1(v, OP_FinishSeek143, iDataCur);
155301 }
155302
155303 /* If changing the rowid value, or if there are foreign key constraints
155304 ** to process, delete the old record. Otherwise, add a noop OP_Delete
155305 ** to invoke the pre-update hook.
155306 **
155307 ** That (regNew==regnewRowid+1) is true is also important for the
155308 ** pre-update hook. If the caller invokes preupdate_new(), the returned
155309 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
155310 ** is the column index supplied by the user.
155311 */
155312 assert( regNew==regNewRowid+1 )((void) (0));
155313#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
155314 sqlite3VdbeAddOp3(v, OP_Delete130, iDataCur,
155315 OPFLAG_ISUPDATE0x04 | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP0x40),
155316 regNewRowid
155317 );
155318 if( eOnePass==ONEPASS_MULTI2 ){
155319 assert( hasFK==0 && chngKey==0 )((void) (0));
155320 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
155321 }
155322 if( !pParse->nested ){
155323 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-5));
155324 }
155325#else
155326 if( hasFK>1 || chngKey ){
155327 sqlite3VdbeAddOp2(v, OP_Delete130, iDataCur, 0);
155328 }
155329#endif
155330
155331 if( hasFK ){
155332 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
155333 }
155334
155335 /* Insert the new index entries and the new record. */
155336 sqlite3CompleteInsertion(
155337 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
155338 OPFLAG_ISUPDATE0x04 | (eOnePass==ONEPASS_MULTI2 ? OPFLAG_SAVEPOSITION0x02 : 0),
155339 0, 0
155340 );
155341
155342 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
155343 ** handle rows (possibly in other tables) that refer via a foreign key
155344 ** to the row just updated. */
155345 if( hasFK ){
155346 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
155347 }
155348 }
155349
155350 /* Increment the row counter
155351 */
155352 if( regRowCount ){
155353 sqlite3VdbeAddOp2(v, OP_AddImm86, regRowCount, 1);
155354 }
155355
155356 if( pTrigger ){
155357 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE130, pChanges,
155358 TRIGGER_AFTER2, pTab, regOldRowid, onError, labelContinue);
155359 }
155360
155361 /* Repeat the above with the next record to be updated, until
155362 ** all record selected by the WHERE clause have been updated.
155363 */
155364 if( eOnePass==ONEPASS_SINGLE1 ){
155365 /* Nothing to do at end-of-loop for a single-pass */
155366 }else if( eOnePass==ONEPASS_MULTI2 ){
155367 sqlite3VdbeResolveLabel(v, labelContinue);
155368 sqlite3WhereEnd(pWInfo);
155369 }else{
155370 sqlite3VdbeResolveLabel(v, labelContinue);
155371 sqlite3VdbeAddOp2(v, OP_Next39, iEph, addrTop); VdbeCoverage(v);
155372 }
155373 sqlite3VdbeResolveLabel(v, labelBreak);
155374
155375 /* Update the sqlite_sequence table by storing the content of the
155376 ** maximum rowid counter values recorded while inserting into
155377 ** autoincrement tables.
155378 */
155379 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
155380 sqlite3AutoincrementEnd(pParse);
155381 }
155382
155383 /*
155384 ** Return the number of rows that were changed, if we are tracking
155385 ** that information.
155386 */
155387 if( regRowCount ){
155388 sqlite3CodeChangeCount(v, regRowCount, "rows updated");
155389 }
155390
155391update_cleanup:
155392 sqlite3AuthContextPop(&sContext);
155393 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
155394 sqlite3SrcListDelete(db, pTabList);
155395 sqlite3ExprListDelete(db, pChanges);
155396 sqlite3ExprDelete(db, pWhere);
155397#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1)
155398 sqlite3ExprListDelete(db, pOrderBy);
155399 sqlite3ExprDelete(db, pLimit);
155400#endif
155401 return;
155402}
155403/* Make sure "isView" and other macros defined above are undefined. Otherwise
155404** they may interfere with compilation of other functions in this file
155405** (or in another file, if this file becomes part of the amalgamation). */
155406#ifdef isView
155407 #undef isView
155408#endif
155409#ifdef pTrigger
155410 #undef pTrigger
155411#endif
155412
155413#ifndef SQLITE_OMIT_VIRTUALTABLE
155414/*
155415** Generate code for an UPDATE of a virtual table.
155416**
155417** There are two possible strategies - the default and the special
155418** "onepass" strategy. Onepass is only used if the virtual table
155419** implementation indicates that pWhere may match at most one row.
155420**
155421** The default strategy is to create an ephemeral table that contains
155422** for each row to be changed:
155423**
155424** (A) The original rowid of that row.
155425** (B) The revised rowid for the row.
155426** (C) The content of every column in the row.
155427**
155428** Then loop through the contents of this ephemeral table executing a
155429** VUpdate for each row. When finished, drop the ephemeral table.
155430**
155431** The "onepass" strategy does not use an ephemeral table. Instead, it
155432** stores the same values (A, B and C above) in a register array and
155433** makes a single invocation of VUpdate.
155434*/
155435static void updateVirtualTable(
155436 Parse *pParse, /* The parsing context */
155437 SrcList *pSrc, /* The virtual table to be modified */
155438 Table *pTab, /* The virtual table */
155439 ExprList *pChanges, /* The columns to change in the UPDATE statement */
155440 Expr *pRowid, /* Expression used to recompute the rowid */
155441 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
155442 Expr *pWhere, /* WHERE clause of the UPDATE statement */
155443 int onError /* ON CONFLICT strategy */
155444){
155445 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
155446 int ephemTab; /* Table holding the result of the SELECT */
155447 int i; /* Loop counter */
155448 sqlite3 *db = pParse->db; /* Database connection */
155449 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
155450 WhereInfo *pWInfo = 0;
155451 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
155452 int regArg; /* First register in VUpdate arg array */
155453 int regRec; /* Register in which to assemble record */
155454 int regRowid; /* Register for ephemeral table rowid */
155455 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
155456 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
155457 int eOnePass; /* True to use onepass strategy */
155458 int addr; /* Address of OP_OpenEphemeral */
155459
155460 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
155461 ** create and open the ephemeral table in which the records created from
155462 ** these arguments will be temporarily stored. */
155463 assert( v )((void) (0));
155464 ephemTab = pParse->nTab++;
155465 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, ephemTab, nArg);
155466 regArg = pParse->nMem + 1;
155467 pParse->nMem += nArg;
155468 if( pSrc->nSrc>1 ){
155469 Index *pPk = 0;
155470 Expr *pRow;
155471 ExprList *pList;
155472 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
155473 if( pRowid ){
155474 pRow = sqlite3ExprDup(db, pRowid, 0);
155475 }else{
155476 pRow = sqlite3PExpr(pParse, TK_ROW76, 0, 0);
155477 }
155478 }else{
155479 i16 iPk; /* PRIMARY KEY column */
155480 pPk = sqlite3PrimaryKeyIndex(pTab);
155481 assert( pPk!=0 )((void) (0));
155482 assert( pPk->nKeyCol==1 )((void) (0));
155483 iPk = pPk->aiColumn[0];
155484 if( aXRef[iPk]>=0 ){
155485 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
155486 }else{
155487 pRow = exprRowColumn(pParse, iPk);
155488 }
155489 }
155490 pList = sqlite3ExprListAppend(pParse, 0, pRow);
155491
155492 for(i=0; i<pTab->nCol; i++){
155493 if( aXRef[i]>=0 ){
155494 pList = sqlite3ExprListAppend(pParse, pList,
155495 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
155496 );
155497 }else{
155498 Expr *pRowExpr = exprRowColumn(pParse, i);
155499 if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG0x01;
155500 pList = sqlite3ExprListAppend(pParse, pList, pRowExpr);
155501 }
155502 }
155503
155504 updateFromSelect(pParse, ephemTab, pPk, pList, pSrc, pWhere, 0, 0);
155505 sqlite3ExprListDelete(db, pList);
155506 eOnePass = ONEPASS_OFF0;
155507 }else{
155508 regRec = ++pParse->nMem;
155509 regRowid = ++pParse->nMem;
155510
155511 /* Start scanning the virtual table */
155512 pWInfo = sqlite3WhereBegin(
155513 pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED0x0004, 0
155514 );
155515 if( pWInfo==0 ) return;
155516
155517 /* Populate the argument registers. */
155518 for(i=0; i<pTab->nCol; i++){
155519 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 )((void) (0));
155520 if( aXRef[i]>=0 ){
155521 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
155522 }else{
155523 sqlite3VdbeAddOp3(v, OP_VColumn176, iCsr, i, regArg+2+i);
155524 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG0x01);/* For sqlite3_vtab_nochange() */
155525 }
155526 }
155527 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
155528 sqlite3VdbeAddOp2(v, OP_Rowid135, iCsr, regArg);
155529 if( pRowid ){
155530 sqlite3ExprCode(pParse, pRowid, regArg+1);
155531 }else{
155532 sqlite3VdbeAddOp2(v, OP_Rowid135, iCsr, regArg+1);
155533 }
155534 }else{
155535 Index *pPk; /* PRIMARY KEY index */
155536 i16 iPk; /* PRIMARY KEY column */
155537 pPk = sqlite3PrimaryKeyIndex(pTab);
155538 assert( pPk!=0 )((void) (0));
155539 assert( pPk->nKeyCol==1 )((void) (0));
155540 iPk = pPk->aiColumn[0];
155541 sqlite3VdbeAddOp3(v, OP_VColumn176, iCsr, iPk, regArg);
155542 sqlite3VdbeAddOp2(v, OP_SCopy81, regArg+2+iPk, regArg+1);
155543 }
155544
155545 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
155546
155547 /* There is no ONEPASS_MULTI on virtual tables */
155548 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
155549
155550 if( eOnePass ){
155551 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
155552 ** above. */
155553 sqlite3VdbeChangeToNoop(v, addr);
155554 sqlite3VdbeAddOp1(v, OP_Close122, iCsr);
155555 }else{
155556 /* Create a record from the argument register contents and insert it into
155557 ** the ephemeral table. */
155558 sqlite3MultiWrite(pParse);
155559 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regArg, nArg, regRec);
155560#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM)
155561 /* Signal an assert() within OP_MakeRecord that it is allowed to
155562 ** accept no-change records with serial_type 10 */
155563 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC0x6d);
155564#endif
155565 sqlite3VdbeAddOp2(v, OP_NewRowid127, ephemTab, regRowid);
155566 sqlite3VdbeAddOp3(v, OP_Insert128, ephemTab, regRec, regRowid);
155567 }
155568 }
155569
155570
155571 if( eOnePass==ONEPASS_OFF0 ){
155572 /* End the virtual table scan */
155573 if( pSrc->nSrc==1 ){
155574 sqlite3WhereEnd(pWInfo);
155575 }
155576
155577 /* Begin scanning through the ephemeral table. */
155578 addr = sqlite3VdbeAddOp1(v, OP_Rewind36, ephemTab); VdbeCoverage(v);
155579
155580 /* Extract arguments from the current row of the ephemeral table and
155581 ** invoke the VUpdate method. */
155582 for(i=0; i<nArg; i++){
155583 sqlite3VdbeAddOp3(v, OP_Column94, ephemTab, i, regArg+i);
155584 }
155585 }
155586 sqlite3VtabMakeWritable(pParse, pTab);
155587 sqlite3VdbeAddOp4(v, OP_VUpdate7, 0, nArg, regArg, pVTab, P4_VTAB(-11));
155588 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
155589 sqlite3MayAbort(pParse);
155590
155591 /* End of the ephemeral table scan. Or, if using the onepass strategy,
155592 ** jump to here if the scan visited zero rows. */
155593 if( eOnePass==ONEPASS_OFF0 ){
155594 sqlite3VdbeAddOp2(v, OP_Next39, ephemTab, addr+1); VdbeCoverage(v);
155595 sqlite3VdbeJumpHere(v, addr);
155596 sqlite3VdbeAddOp2(v, OP_Close122, ephemTab, 0);
155597 }else{
155598 sqlite3WhereEnd(pWInfo);
155599 }
155600}
155601#endif /* SQLITE_OMIT_VIRTUALTABLE */
155602
155603/************** End of update.c **********************************************/
155604/************** Begin file upsert.c ******************************************/
155605/*
155606** 2018-04-12
155607**
155608** The author disclaims copyright to this source code. In place of
155609** a legal notice, here is a blessing:
155610**
155611** May you do good and not evil.
155612** May you find forgiveness for yourself and forgive others.
155613** May you share freely, never taking more than you give.
155614**
155615*************************************************************************
155616** This file contains code to implement various aspects of UPSERT
155617** processing and handling of the Upsert object.
155618*/
155619/* #include "sqliteInt.h" */
155620
155621#ifndef SQLITE_OMIT_UPSERT
155622/*
155623** Free a list of Upsert objects
155624*/
155625static void SQLITE_NOINLINE__attribute__((noinline)) upsertDelete(sqlite3 *db, Upsert *p){
155626 do{
155627 Upsert *pNext = p->pNextUpsert;
155628 sqlite3ExprListDelete(db, p->pUpsertTarget);
155629 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
155630 sqlite3ExprListDelete(db, p->pUpsertSet);
155631 sqlite3ExprDelete(db, p->pUpsertWhere);
155632 sqlite3DbFree(db, p->pToFree);
155633 sqlite3DbFree(db, p);
155634 p = pNext;
155635 }while( p );
155636}
155637SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
155638 if( p ) upsertDelete(db, p);
155639}
155640
155641
155642/*
155643** Duplicate an Upsert object.
155644*/
155645SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
155646 if( p==0 ) return 0;
155647 return sqlite3UpsertNew(db,
155648 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
155649 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
155650 sqlite3ExprListDup(db, p->pUpsertSet, 0),
155651 sqlite3ExprDup(db, p->pUpsertWhere, 0),
155652 sqlite3UpsertDup(db, p->pNextUpsert)
155653 );
155654}
155655
155656/*
155657** Create a new Upsert object.
155658*/
155659SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(
155660 sqlite3 *db, /* Determines which memory allocator to use */
155661 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
155662 Expr *pTargetWhere, /* Optional WHERE clause on the target */
155663 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
155664 Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */
155665 Upsert *pNext /* Next ON CONFLICT clause in the list */
155666){
155667 Upsert *pNew;
155668 pNew = sqlite3DbMallocZero(db, sizeof(Upsert));
155669 if( pNew==0 ){
155670 sqlite3ExprListDelete(db, pTarget);
155671 sqlite3ExprDelete(db, pTargetWhere);
155672 sqlite3ExprListDelete(db, pSet);
155673 sqlite3ExprDelete(db, pWhere);
155674 sqlite3UpsertDelete(db, pNext);
155675 return 0;
155676 }else{
155677 pNew->pUpsertTarget = pTarget;
155678 pNew->pUpsertTargetWhere = pTargetWhere;
155679 pNew->pUpsertSet = pSet;
155680 pNew->pUpsertWhere = pWhere;
155681 pNew->isDoUpdate = pSet!=0;
155682 pNew->pNextUpsert = pNext;
155683 }
155684 return pNew;
155685}
155686
155687/*
155688** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
155689** symbols in the conflict-target.
155690**
155691** Return SQLITE_OK if everything works, or an error code is something
155692** is wrong.
155693*/
155694SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(
155695 Parse *pParse, /* The parsing context */
155696 SrcList *pTabList, /* Table into which we are inserting */
155697 Upsert *pUpsert, /* The ON CONFLICT clauses */
155698 Upsert *pAll /* Complete list of all ON CONFLICT clauses */
155699){
155700 Table *pTab; /* That table into which we are inserting */
155701 int rc; /* Result code */
155702 int iCursor; /* Cursor used by pTab */
155703 Index *pIdx; /* One of the indexes of pTab */
155704 ExprList *pTarget; /* The conflict-target clause */
155705 Expr *pTerm; /* One term of the conflict-target clause */
155706 NameContext sNC; /* Context for resolving symbolic names */
155707 Expr sCol[2]; /* Index column converted into an Expr */
155708 int nClause = 0; /* Counter of ON CONFLICT clauses */
155709
155710 assert( pTabList->nSrc==1 )((void) (0));
155711 assert( pTabList->a[0].pSTab!=0 )((void) (0));
155712 assert( pUpsert!=0 )((void) (0));
155713 assert( pUpsert->pUpsertTarget!=0 )((void) (0));
155714
155715 /* Resolve all symbolic names in the conflict-target clause, which
155716 ** includes both the list of columns and the optional partial-index
155717 ** WHERE clause.
155718 */
155719 memset(&sNC, 0, sizeof(sNC));
155720 sNC.pParse = pParse;
155721 sNC.pSrcList = pTabList;
155722 for(; pUpsert && pUpsert->pUpsertTarget;
155723 pUpsert=pUpsert->pNextUpsert, nClause++){
155724 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
155725 if( rc ) return rc;
155726 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
155727 if( rc ) return rc;
155728
155729 /* Check to see if the conflict target matches the rowid. */
155730 pTab = pTabList->a[0].pSTab;
155731 pTarget = pUpsert->pUpsertTarget;
155732 iCursor = pTabList->a[0].iCursor;
155733 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)
155734 && pTarget->nExpr==1
155735 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN168
155736 && pTerm->iColumn==XN_ROWID(-1)
155737 ){
155738 /* The conflict-target is the rowid of the primary table */
155739 assert( pUpsert->pUpsertIdx==0 )((void) (0));
155740 continue;
155741 }
155742
155743 /* Initialize sCol[0..1] to be an expression parse tree for a
155744 ** single column of an index. The sCol[0] node will be the TK_COLLATE
155745 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
155746 ** will populate the specific collation and column number values
155747 ** prior to comparing against the conflict-target expression.
155748 */
155749 memset(sCol, 0, sizeof(sCol));
155750 sCol[0].op = TK_COLLATE114;
155751 sCol[0].pLeft = &sCol[1];
155752 sCol[1].op = TK_COLUMN168;
155753 sCol[1].iTable = pTabList->a[0].iCursor;
155754
155755 /* Check for matches against other indexes */
155756 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
155757 int ii, jj, nn;
155758 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
155759 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
155760 if( pIdx->pPartIdxWhere ){
155761 if( pUpsert->pUpsertTargetWhere==0 ) continue;
155762 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
155763 pIdx->pPartIdxWhere, iCursor)!=0 ){
155764 continue;
155765 }
155766 }
155767 nn = pIdx->nKeyCol;
155768 for(ii=0; ii<nn; ii++){
155769 Expr *pExpr;
155770 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
155771 if( pIdx->aiColumn[ii]==XN_EXPR(-2) ){
155772 assert( pIdx->aColExpr!=0 )((void) (0));
155773 assert( pIdx->aColExpr->nExpr>ii )((void) (0));
155774 assert( pIdx->bHasExpr )((void) (0));
155775 pExpr = pIdx->aColExpr->a[ii].pExpr;
155776 if( pExpr->op!=TK_COLLATE114 ){
155777 sCol[0].pLeft = pExpr;
155778 pExpr = &sCol[0];
155779 }
155780 }else{
155781 sCol[0].pLeft = &sCol[1];
155782 sCol[1].iColumn = pIdx->aiColumn[ii];
155783 pExpr = &sCol[0];
155784 }
155785 for(jj=0; jj<nn; jj++){
155786 if( sqlite3ExprCompare(0,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
155787 break; /* Column ii of the index matches column jj of target */
155788 }
155789 }
155790 if( jj>=nn ){
155791 /* The target contains no match for column jj of the index */
155792 break;
155793 }
155794 }
155795 if( ii<nn ){
155796 /* Column ii of the index did not match any term of the conflict target.
155797 ** Continue the search with the next index. */
155798 continue;
155799 }
155800 pUpsert->pUpsertIdx = pIdx;
155801 if( sqlite3UpsertOfIndex(pAll,pIdx)!=pUpsert ){
155802 /* Really this should be an error. The isDup ON CONFLICT clause will
155803 ** never fire. But this problem was not discovered until three years
155804 ** after multi-CONFLICT upsert was added, and so we silently ignore
155805 ** the problem to prevent breaking applications that might actually
155806 ** have redundant ON CONFLICT clauses. */
155807 pUpsert->isDup = 1;
155808 }
155809 break;
155810 }
155811 if( pUpsert->pUpsertIdx==0 ){
155812 char zWhich[16];
155813 if( nClause==0 && pUpsert->pNextUpsert==0 ){
155814 zWhich[0] = 0;
155815 }else{
155816 sqlite3_snprintf(sizeof(zWhich),zWhich,"%r ", nClause+1);
155817 }
155818 sqlite3ErrorMsg(pParse, "%sON CONFLICT clause does not match any "
155819 "PRIMARY KEY or UNIQUE constraint", zWhich);
155820 return SQLITE_ERROR1;
155821 }
155822 }
155823 return SQLITE_OK0;
155824}
155825
155826/*
155827** Return true if pUpsert is the last ON CONFLICT clause with a
155828** conflict target, or if pUpsert is followed by another ON CONFLICT
155829** clause that targets the INTEGER PRIMARY KEY.
155830*/
155831SQLITE_PRIVATEstatic int sqlite3UpsertNextIsIPK(Upsert *pUpsert){
155832 Upsert *pNext;
155833 if( NEVER(pUpsert==0)(pUpsert==0) ) return 0;
155834 pNext = pUpsert->pNextUpsert;
155835 while( 1 /*exit-by-return*/ ){
155836 if( pNext==0 ) return 1;
155837 if( pNext->pUpsertTarget==0 ) return 1;
155838 if( pNext->pUpsertIdx==0 ) return 1;
155839 if( !pNext->isDup ) return 0;
155840 pNext = pNext->pNextUpsert;
155841 }
155842 return 0;
155843}
155844
155845/*
155846** Given the list of ON CONFLICT clauses described by pUpsert, and
155847** a particular index pIdx, return a pointer to the particular ON CONFLICT
155848** clause that applies to the index. Or, if the index is not subject to
155849** any ON CONFLICT clause, return NULL.
155850*/
155851SQLITE_PRIVATEstatic Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){
155852 while(
155853 pUpsert
155854 && pUpsert->pUpsertTarget!=0
155855 && pUpsert->pUpsertIdx!=pIdx
155856 ){
155857 pUpsert = pUpsert->pNextUpsert;
155858 }
155859 return pUpsert;
155860}
155861
155862/*
155863** Generate bytecode that does an UPDATE as part of an upsert.
155864**
155865** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
155866** In this case parameter iCur is a cursor open on the table b-tree that
155867** currently points to the conflicting table row. Otherwise, if pIdx
155868** is not NULL, then pIdx is the constraint that failed and iCur is a
155869** cursor points to the conflicting row.
155870*/
155871SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(
155872 Parse *pParse, /* The parsing and code-generating context */
155873 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
155874 Table *pTab, /* The table being updated */
155875 Index *pIdx, /* The UNIQUE constraint that failed */
155876 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
155877){
155878 Vdbe *v = pParse->pVdbe;
155879 sqlite3 *db = pParse->db;
155880 SrcList *pSrc; /* FROM clause for the UPDATE */
155881 int iDataCur;
155882 int i;
155883 Upsert *pTop = pUpsert;
155884
155885 assert( v!=0 )((void) (0));
155886 assert( pUpsert!=0 )((void) (0));
155887 iDataCur = pUpsert->iDataCur;
155888 pUpsert = sqlite3UpsertOfIndex(pTop, pIdx);
155889 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
155890 if( pIdx && iCur!=iDataCur ){
155891 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
155892 int regRowid = sqlite3GetTempReg(pParse);
155893 sqlite3VdbeAddOp2(v, OP_IdxRowid142, iCur, regRowid);
155894 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iDataCur, 0, regRowid);
155895 VdbeCoverage(v);
155896 sqlite3ReleaseTempReg(pParse, regRowid);
155897 }else{
155898 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
155899 int nPk = pPk->nKeyCol;
155900 int iPk = pParse->nMem+1;
155901 pParse->nMem += nPk;
155902 for(i=0; i<nPk; i++){
155903 int k;
155904 assert( pPk->aiColumn[i]>=0 )((void) (0));
155905 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
155906 sqlite3VdbeAddOp3(v, OP_Column94, iCur, k, iPk+i);
155907 VdbeComment((v, "%s.%s", pIdx->zName,
155908 pTab->aCol[pPk->aiColumn[i]].zCnName));
155909 }
155910 sqlite3VdbeVerifyAbortable(v, OE_Abort);
155911 i = sqlite3VdbeAddOp4Int(v, OP_Found29, iDataCur, 0, iPk, nPk);
155912 VdbeCoverage(v);
155913 sqlite3VdbeAddOp4(v, OP_Halt70, SQLITE_CORRUPT11, OE_Abort2, 0,
155914 "corrupt database", P4_STATIC(-1));
155915 sqlite3MayAbort(pParse);
155916 sqlite3VdbeJumpHere(v, i);
155917 }
155918 }
155919 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
155920 ** So we have to make a copy before passing it down into sqlite3Update() */
155921 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
155922 /* excluded.* columns of type REAL need to be converted to a hard real */
155923 for(i=0; i<pTab->nCol; i++){
155924 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL0x45 ){
155925 sqlite3VdbeAddOp1(v, OP_RealAffinity87, pTop->regData+i);
155926 }
155927 }
155928 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
155929 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort2, 0, 0, pUpsert);
155930 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
155931}
155932
155933#endif /* SQLITE_OMIT_UPSERT */
155934
155935/************** End of upsert.c **********************************************/
155936/************** Begin file vacuum.c ******************************************/
155937/*
155938** 2003 April 6
155939**
155940** The author disclaims copyright to this source code. In place of
155941** a legal notice, here is a blessing:
155942**
155943** May you do good and not evil.
155944** May you find forgiveness for yourself and forgive others.
155945** May you share freely, never taking more than you give.
155946**
155947*************************************************************************
155948** This file contains code used to implement the VACUUM command.
155949**
155950** Most of the code in this file may be omitted by defining the
155951** SQLITE_OMIT_VACUUM macro.
155952*/
155953/* #include "sqliteInt.h" */
155954/* #include "vdbeInt.h" */
155955
155956#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
155957
155958/*
155959** Execute zSql on database db.
155960**
155961** If zSql returns rows, then each row will have exactly one
155962** column. (This will only happen if zSql begins with "SELECT".)
155963** Take each row of result and call execSql() again recursively.
155964**
155965** The execSqlF() routine does the same thing, except it accepts
155966** a format string as its third argument
155967*/
155968static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
155969 sqlite3_stmt *pStmt;
155970 int rc;
155971
155972 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
155973 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
155974 if( rc!=SQLITE_OK0 ) return rc;
155975 while( SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
155976 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
155977 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 )((void) (0));
155978 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
155979 ** or INSERT. Historically there have been attacks that first
155980 ** corrupt the sqlite_schema.sql field with other kinds of statements
155981 ** then run VACUUM to get those statements to execute at inappropriate
155982 ** times. */
155983 if( zSubSql
155984 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
155985 ){
155986 rc = execSql(db, pzErrMsg, zSubSql);
155987 if( rc!=SQLITE_OK0 ) break;
155988 }
155989 }
155990 assert( rc!=SQLITE_ROW )((void) (0));
155991 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
155992 if( rc ){
155993 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
155994 }
155995 (void)sqlite3_finalize(pStmt);
155996 return rc;
155997}
155998static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
155999 char *z;
156000 va_list ap;
156001 int rc;
156002 va_start(ap, zSql)__builtin_va_start(ap, zSql);
156003 z = sqlite3VMPrintf(db, zSql, ap);
156004 va_end(ap)__builtin_va_end(ap);
156005 if( z==0 ) return SQLITE_NOMEM7;
156006 rc = execSql(db, pzErrMsg, z);
156007 sqlite3DbFree(db, z);
156008 return rc;
156009}
156010
156011/*
156012** The VACUUM command is used to clean up the database,
156013** collapse free space, etc. It is modelled after the VACUUM command
156014** in PostgreSQL. The VACUUM command works as follows:
156015**
156016** (1) Create a new transient database file
156017** (2) Copy all content from the database being vacuumed into
156018** the new transient database file
156019** (3) Copy content from the transient database back into the
156020** original database.
156021**
156022** The transient database requires temporary disk space approximately
156023** equal to the size of the original database. The copy operation of
156024** step (3) requires additional temporary disk space approximately equal
156025** to the size of the original database for the rollback journal.
156026** Hence, temporary disk space that is approximately 2x the size of the
156027** original database is required. Every page of the database is written
156028** approximately 3 times: Once for step (2) and twice for step (3).
156029** Two writes per page are required in step (3) because the original
156030** database content must be written into the rollback journal prior to
156031** overwriting the database with the vacuumed content.
156032**
156033** Only 1x temporary space and only 1x writes would be required if
156034** the copy of step (3) were replaced by deleting the original database
156035** and renaming the transient database as the original. But that will
156036** not work if other processes are attached to the original database.
156037** And a power loss in between deleting the original and renaming the
156038** transient would cause the database file to appear to be deleted
156039** following reboot.
156040*/
156041SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
156042 Vdbe *v = sqlite3GetVdbe(pParse);
156043 int iDb = 0;
156044 if( v==0 ) goto build_vacuum_end;
156045 if( pParse->nErr ) goto build_vacuum_end;
156046 if( pNm ){
156047#ifndef SQLITE_BUG_COMPATIBLE_20160819
156048 /* Default behavior: Report an error if the argument to VACUUM is
156049 ** not recognized */
156050 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
156051 if( iDb<0 ) goto build_vacuum_end;
156052#else
156053 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
156054 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
156055 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
156056 ** The buggy behavior is required for binary compatibility with some
156057 ** legacy applications. */
156058 iDb = sqlite3FindDb(pParse->db, pNm);
156059 if( iDb<0 ) iDb = 0;
156060#endif
156061 }
156062 if( iDb!=1 ){
156063 int iIntoReg = 0;
156064 if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
156065 iIntoReg = ++pParse->nMem;
156066 sqlite3ExprCode(pParse, pInto, iIntoReg);
156067 }
156068 sqlite3VdbeAddOp2(v, OP_Vacuum5, iDb, iIntoReg);
156069 sqlite3VdbeUsesBtree(v, iDb);
156070 }
156071build_vacuum_end:
156072 sqlite3ExprDelete(pParse->db, pInto);
156073 return;
156074}
156075
156076/*
156077** This routine implements the OP_Vacuum opcode of the VDBE.
156078*/
156079SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3RunVacuum(
156080 char **pzErrMsg, /* Write error message here */
156081 sqlite3 *db, /* Database connection */
156082 int iDb, /* Which attached DB to vacuum */
156083 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
156084){
156085 int rc = SQLITE_OK0; /* Return code from service routines */
156086 Btree *pMain; /* The database being vacuumed */
156087 Btree *pTemp; /* The temporary database we vacuum into */
156088 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
156089 u64 saved_flags; /* Saved value of db->flags */
156090 i64 saved_nChange; /* Saved value of db->nChange */
156091 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
156092 u32 saved_openFlags; /* Saved value of db->openFlags */
156093 u8 saved_mTrace; /* Saved trace settings */
156094 Db *pDb = 0; /* Database to detach at end of vacuum */
156095 int isMemDb; /* True if vacuuming a :memory: database */
156096 int nRes; /* Bytes of reserved space at the end of each page */
156097 int nDb; /* Number of attached databases */
156098 const char *zDbMain; /* Schema name of database to vacuum */
156099 const char *zOut; /* Name of output file */
156100 u32 pgflags = PAGER_SYNCHRONOUS_OFF0x01; /* sync flags for output db */
156101 u64 iRandom; /* Random value used for zDbVacuum[] */
156102 char zDbVacuum[42]; /* Name of the ATTACH-ed database used for vacuum */
156103
156104
156105 if( !db->autoCommit ){
156106 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
156107 return SQLITE_ERROR1; /* IMP: R-12218-18073 */
156108 }
156109 if( db->nVdbeActive>1 ){
156110 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
156111 return SQLITE_ERROR1; /* IMP: R-15610-35227 */
156112 }
156113 saved_openFlags = db->openFlags;
156114 if( pOut ){
156115 if( sqlite3_value_type(pOut)!=SQLITE_TEXT3 ){
156116 sqlite3SetString(pzErrMsg, db, "non-text filename");
156117 return SQLITE_ERROR1;
156118 }
156119 zOut = (const char*)sqlite3_value_text(pOut);
156120 db->openFlags &= ~SQLITE_OPEN_READONLY0x00000001;
156121 db->openFlags |= SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002;
156122 }else{
156123 zOut = "";
156124 }
156125
156126 /* Save the current value of the database flags so that it can be
156127 ** restored before returning. Then set the writable-schema flag, and
156128 ** disable CHECK and foreign key constraints. */
156129 saved_flags = db->flags;
156130 saved_mDbFlags = db->mDbFlags;
156131 saved_nChange = db->nChange;
156132 saved_nTotalChange = db->nTotalChange;
156133 saved_mTrace = db->mTrace;
156134 db->flags |= SQLITE_WriteSchema0x00000001 | SQLITE_IgnoreChecks0x00000200;
156135 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002 | DBFLAG_Vacuum0x0004;
156136 db->flags &= ~(u64)(SQLITE_ForeignKeys0x00004000 | SQLITE_ReverseOrder0x00001000
156137 | SQLITE_Defensive0x10000000 | SQLITE_CountRows((u64)(0x00001)<<32));
156138 db->mTrace = 0;
156139
156140 zDbMain = db->aDb[iDb].zDbSName;
156141 pMain = db->aDb[iDb].pBt;
156142 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
156143
156144 /* Attach the temporary database as 'vacuum_XXXXXX'. The synchronous pragma
156145 ** can be set to 'off' for this file, as it is not recovered if a crash
156146 ** occurs anyway. The integrity of the database is maintained by a
156147 ** (possibly synchronous) transaction opened on the main database before
156148 ** sqlite3BtreeCopyFile() is called.
156149 **
156150 ** An optimization would be to use a non-journaled pager.
156151 ** (Later:) I tried setting "PRAGMA vacuum_XXXXXX.journal_mode=OFF" but
156152 ** that actually made the VACUUM run slower. Very little journalling
156153 ** actually occurs when doing a vacuum since the vacuum_db is initially
156154 ** empty. Only the journal header is written. Apparently it takes more
156155 ** time to parse and run the PRAGMA to turn journalling off than it does
156156 ** to write the journal header file.
156157 */
156158 sqlite3_randomness(sizeof(iRandom),&iRandom);
156159 sqlite3_snprintf(sizeof(zDbVacuum), zDbVacuum, "vacuum_%016llx", iRandom);
156160 nDb = db->nDb;
156161 rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS %s", zOut, zDbVacuum);
156162 db->openFlags = saved_openFlags;
156163 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156164 assert( (db->nDb-1)==nDb )((void) (0));
156165 pDb = &db->aDb[nDb];
156166 assert( strcmp(pDb->zDbSName,zDbVacuum)==0 )((void) (0));
156167 pTemp = pDb->pBt;
156168 if( pOut ){
156169 sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
156170 i64 sz = 0;
156171 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK0 || sz>0) ){
156172 rc = SQLITE_ERROR1;
156173 sqlite3SetString(pzErrMsg, db, "output file already exists");
156174 goto end_of_vacuum;
156175 }
156176 db->mDbFlags |= DBFLAG_VacuumInto0x0008;
156177
156178 /* For a VACUUM INTO, the pager-flags are set to the same values as
156179 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
156180 ** is always set. */
156181 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK0x38);
156182 }
156183 nRes = sqlite3BtreeGetRequestedReserve(pMain);
156184
156185 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
156186 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
156187 sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL0x20);
156188
156189 /* Begin a transaction and take an exclusive lock on the main database
156190 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
156191 ** to ensure that we do not try to change the page-size on a WAL database.
156192 */
156193 rc = execSql(db, pzErrMsg, "BEGIN");
156194 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156195 rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
156196 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156197
156198 /* Do not attempt to change the page size for a WAL database */
156199 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
156200 ==PAGER_JOURNALMODE_WAL5
156201 && pOut==0
156202 ){
156203 db->nextPagesize = 0;
156204 }
156205
156206 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
156207 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
156208 || NEVER(db->mallocFailed)(db->mallocFailed)
156209 ){
156210 rc = SQLITE_NOMEM_BKPT7;
156211 goto end_of_vacuum;
156212 }
156213
156214#ifndef SQLITE_OMIT_AUTOVACUUM
156215 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
156216 sqlite3BtreeGetAutoVacuum(pMain));
156217#endif
156218
156219 /* Query the schema of the main database. Create a mirror schema
156220 ** in the temporary database.
156221 */
156222 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
156223 rc = execSqlF(db, pzErrMsg,
156224 "SELECT sql FROM \"%w\".sqlite_schema"
156225 " WHERE type='table'AND name<>'sqlite_sequence'"
156226 " AND coalesce(rootpage,1)>0",
156227 zDbMain
156228 );
156229 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156230 rc = execSqlF(db, pzErrMsg,
156231 "SELECT sql FROM \"%w\".sqlite_schema"
156232 " WHERE type='index'",
156233 zDbMain
156234 );
156235 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156236 db->init.iDb = 0;
156237
156238 /* Loop through the tables in the main database. For each, do
156239 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
156240 ** the contents to the temporary database.
156241 */
156242 rc = execSqlF(db, pzErrMsg,
156243 "SELECT'INSERT INTO %s.'||quote(name)"
156244 "||' SELECT*FROM\"%w\".'||quote(name)"
156245 "FROM %s.sqlite_schema "
156246 "WHERE type='table'AND coalesce(rootpage,1)>0",
156247 zDbVacuum, zDbMain, zDbVacuum
156248 );
156249 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 )((void) (0));
156250 db->mDbFlags &= ~DBFLAG_Vacuum0x0004;
156251 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156252
156253 /* Copy the triggers, views, and virtual tables from the main database
156254 ** over to the temporary database. None of these objects has any
156255 ** associated storage, so all we have to do is copy their entries
156256 ** from the schema table.
156257 */
156258 rc = execSqlF(db, pzErrMsg,
156259 "INSERT INTO %s.sqlite_schema"
156260 " SELECT*FROM \"%w\".sqlite_schema"
156261 " WHERE type IN('view','trigger')"
156262 " OR(type='table'AND rootpage=0)",
156263 zDbVacuum, zDbMain
156264 );
156265 if( rc ) goto end_of_vacuum;
156266
156267 /* At this point, there is a write transaction open on both the
156268 ** vacuum database and the main database. Assuming no error occurs,
156269 ** both transactions are closed by this block - the main database
156270 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
156271 ** call to sqlite3BtreeCommit().
156272 */
156273 {
156274 u32 meta;
156275 int i;
156276
156277 /* This array determines which meta meta values are preserved in the
156278 ** vacuum. Even entries are the meta value number and odd entries
156279 ** are an increment to apply to the meta value after the vacuum.
156280 ** The increment is used to increase the schema cookie so that other
156281 ** connections to the same database will know to reread the schema.
156282 */
156283 static const unsigned char aCopy[] = {
156284 BTREE_SCHEMA_VERSION1, 1, /* Add one to the old schema cookie */
156285 BTREE_DEFAULT_CACHE_SIZE3, 0, /* Preserve the default page cache size */
156286 BTREE_TEXT_ENCODING5, 0, /* Preserve the text encoding */
156287 BTREE_USER_VERSION6, 0, /* Preserve the user version */
156288 BTREE_APPLICATION_ID8, 0, /* Preserve the application id */
156289 };
156290
156291 assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) )((void) (0));
156292 assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) )((void) (0));
156293
156294 /* Copy Btree meta values */
156295 for(i=0; i<ArraySize(aCopy)((int)(sizeof(aCopy)/sizeof(aCopy[0]))); i+=2){
156296 /* GetMeta() and UpdateMeta() cannot fail in this context because
156297 ** we already have page 1 loaded into cache and marked dirty. */
156298 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
156299 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
156300 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) goto end_of_vacuum;
156301 }
156302
156303 if( pOut==0 ){
156304 rc = sqlite3BtreeCopyFile(pMain, pTemp);
156305 }
156306 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156307 rc = sqlite3BtreeCommit(pTemp);
156308 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
156309#ifndef SQLITE_OMIT_AUTOVACUUM
156310 if( pOut==0 ){
156311 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
156312 }
156313#endif
156314 }
156315
156316 assert( rc==SQLITE_OK )((void) (0));
156317 if( pOut==0 ){
156318 nRes = sqlite3BtreeGetRequestedReserve(pTemp);
156319 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
156320 }
156321
156322end_of_vacuum:
156323 /* Restore the original value of db->flags */
156324 db->init.iDb = 0;
156325 db->mDbFlags = saved_mDbFlags;
156326 db->flags = saved_flags;
156327 db->nChange = saved_nChange;
156328 db->nTotalChange = saved_nTotalChange;
156329 db->mTrace = saved_mTrace;
156330 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
156331
156332 /* Currently there is an SQL level transaction open on the vacuum
156333 ** database. No locks are held on any other files (since the main file
156334 ** was committed at the btree level). So it safe to end the transaction
156335 ** by manually setting the autoCommit flag to true and detaching the
156336 ** vacuum database. The vacuum_db journal file is deleted when the pager
156337 ** is closed by the DETACH.
156338 */
156339 db->autoCommit = 1;
156340
156341 if( pDb ){
156342 sqlite3BtreeClose(pDb->pBt);
156343 pDb->pBt = 0;
156344 pDb->pSchema = 0;
156345 }
156346
156347 /* This both clears the schemas and reduces the size of the db->aDb[]
156348 ** array. */
156349 sqlite3ResetAllSchemasOfConnection(db);
156350
156351 return rc;
156352}
156353
156354#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
156355
156356/************** End of vacuum.c **********************************************/
156357/************** Begin file vtab.c ********************************************/
156358/*
156359** 2006 June 10
156360**
156361** The author disclaims copyright to this source code. In place of
156362** a legal notice, here is a blessing:
156363**
156364** May you do good and not evil.
156365** May you find forgiveness for yourself and forgive others.
156366** May you share freely, never taking more than you give.
156367**
156368*************************************************************************
156369** This file contains code used to help implement virtual tables.
156370*/
156371#ifndef SQLITE_OMIT_VIRTUALTABLE
156372/* #include "sqliteInt.h" */
156373
156374/*
156375** Before a virtual table xCreate() or xConnect() method is invoked, the
156376** sqlite3.pVtabCtx member variable is set to point to an instance of
156377** this struct allocated on the stack. It is used by the implementation of
156378** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
156379** are invoked only from within xCreate and xConnect methods.
156380*/
156381struct VtabCtx {
156382 VTable *pVTable; /* The virtual table being constructed */
156383 Table *pTab; /* The Table object to which the virtual table belongs */
156384 VtabCtx *pPrior; /* Parent context (if any) */
156385 int bDeclared; /* True after sqlite3_declare_vtab() is called */
156386};
156387
156388/*
156389** Construct and install a Module object for a virtual table. When this
156390** routine is called, it is guaranteed that all appropriate locks are held
156391** and the module is not already part of the connection.
156392**
156393** If there already exists a module with zName, replace it with the new one.
156394** If pModule==0, then delete the module zName if it exists.
156395*/
156396SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
156397 sqlite3 *db, /* Database in which module is registered */
156398 const char *zName, /* Name assigned to this module */
156399 const sqlite3_module *pModule, /* The definition of the module */
156400 void *pAux, /* Context pointer for xCreate/xConnect */
156401 void (*xDestroy)(void *) /* Module destructor function */
156402){
156403 Module *pMod;
156404 Module *pDel;
156405 char *zCopy;
156406 if( pModule==0 ){
156407 zCopy = (char*)zName;
156408 pMod = 0;
156409 }else{
156410 int nName = sqlite3Strlen30(zName);
156411 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
156412 if( pMod==0 ){
156413 sqlite3OomFault(db);
156414 return 0;
156415 }
156416 zCopy = (char *)(&pMod[1]);
156417 memcpy(zCopy, zName, nName+1);
156418 pMod->zName = zCopy;
156419 pMod->pModule = pModule;
156420 pMod->pAux = pAux;
156421 pMod->xDestroy = xDestroy;
156422 pMod->pEpoTab = 0;
156423 pMod->nRefModule = 1;
156424 }
156425 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
156426 if( pDel ){
156427 if( pDel==pMod ){
156428 sqlite3OomFault(db);
156429 sqlite3DbFree(db, pDel);
156430 pMod = 0;
156431 }else{
156432 sqlite3VtabEponymousTableClear(db, pDel);
156433 sqlite3VtabModuleUnref(db, pDel);
156434 }
156435 }
156436 return pMod;
156437}
156438
156439/*
156440** The actual function that does the work of creating a new module.
156441** This function implements the sqlite3_create_module() and
156442** sqlite3_create_module_v2() interfaces.
156443*/
156444static int createModule(
156445 sqlite3 *db, /* Database in which module is registered */
156446 const char *zName, /* Name assigned to this module */
156447 const sqlite3_module *pModule, /* The definition of the module */
156448 void *pAux, /* Context pointer for xCreate/xConnect */
156449 void (*xDestroy)(void *) /* Module destructor function */
156450){
156451 int rc = SQLITE_OK0;
156452
156453 sqlite3_mutex_enter(db->mutex);
156454 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
156455 rc = sqlite3ApiExit(db, rc);
156456 if( rc!=SQLITE_OK0 && xDestroy ) xDestroy(pAux);
156457 sqlite3_mutex_leave(db->mutex);
156458 return rc;
156459}
156460
156461
156462/*
156463** External API function used to create a new virtual-table module.
156464*/
156465SQLITE_API int sqlite3_create_module(
156466 sqlite3 *db, /* Database in which module is registered */
156467 const char *zName, /* Name assigned to this module */
156468 const sqlite3_module *pModule, /* The definition of the module */
156469 void *pAux /* Context pointer for xCreate/xConnect */
156470){
156471#ifdef SQLITE_ENABLE_API_ARMOR1
156472 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(156472);
156473#endif
156474 return createModule(db, zName, pModule, pAux, 0);
156475}
156476
156477/*
156478** External API function used to create a new virtual-table module.
156479*/
156480SQLITE_API int sqlite3_create_module_v2(
156481 sqlite3 *db, /* Database in which module is registered */
156482 const char *zName, /* Name assigned to this module */
156483 const sqlite3_module *pModule, /* The definition of the module */
156484 void *pAux, /* Context pointer for xCreate/xConnect */
156485 void (*xDestroy)(void *) /* Module destructor function */
156486){
156487#ifdef SQLITE_ENABLE_API_ARMOR1
156488 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(156488);
156489#endif
156490 return createModule(db, zName, pModule, pAux, xDestroy);
156491}
156492
156493/*
156494** External API to drop all virtual-table modules, except those named
156495** on the azNames list.
156496*/
156497SQLITE_API int sqlite3_drop_modules(sqlite3 *db, const char** azNames){
156498 HashElem *pThis, *pNext;
156499#ifdef SQLITE_ENABLE_API_ARMOR1
156500 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(156500);
156501#endif
156502 for(pThis=sqliteHashFirst(&db->aModule)((&db->aModule)->first); pThis; pThis=pNext){
156503 Module *pMod = (Module*)sqliteHashData(pThis)((pThis)->data);
156504 pNext = sqliteHashNext(pThis)((pThis)->next);
156505 if( azNames ){
156506 int ii;
156507 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
156508 if( azNames[ii]!=0 ) continue;
156509 }
156510 createModule(db, pMod->zName, 0, 0, 0);
156511 }
156512 return SQLITE_OK0;
156513}
156514
156515/*
156516** Decrement the reference count on a Module object. Destroy the
156517** module when the reference count reaches zero.
156518*/
156519SQLITE_PRIVATEstatic void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){
156520 assert( pMod->nRefModule>0 )((void) (0));
156521 pMod->nRefModule--;
156522 if( pMod->nRefModule==0 ){
156523 if( pMod->xDestroy ){
156524 pMod->xDestroy(pMod->pAux);
156525 }
156526 assert( pMod->pEpoTab==0 )((void) (0));
156527 sqlite3DbFree(db, pMod);
156528 }
156529}
156530
156531/*
156532** Lock the virtual table so that it cannot be disconnected.
156533** Locks nest. Every lock should have a corresponding unlock.
156534** If an unlock is omitted, resources leaks will occur.
156535**
156536** If a disconnect is attempted while a virtual table is locked,
156537** the disconnect is deferred until all locks have been removed.
156538*/
156539SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *pVTab){
156540 pVTab->nRef++;
156541}
156542
156543
156544/*
156545** pTab is a pointer to a Table structure representing a virtual-table.
156546** Return a pointer to the VTable object used by connection db to access
156547** this virtual-table, if one has been created, or NULL otherwise.
156548*/
156549SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
156550 VTable *pVtab;
156551 assert( IsVirtual(pTab) )((void) (0));
156552 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
156553 return pVtab;
156554}
156555
156556/*
156557** Decrement the ref-count on a virtual table object. When the ref-count
156558** reaches zero, call the xDisconnect() method to delete the object.
156559*/
156560SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *pVTab){
156561 sqlite3 *db = pVTab->db;
156562
156563 assert( db )((void) (0));
156564 assert( pVTab->nRef>0 )((void) (0));
156565 assert( db->eOpenState==SQLITE_STATE_OPEN((void) (0))
156566 || db->eOpenState==SQLITE_STATE_ZOMBIE )((void) (0));
156567
156568 pVTab->nRef--;
156569 if( pVTab->nRef==0 ){
156570 sqlite3_vtab *p = pVTab->pVtab;
156571 if( p ){
156572 p->pModule->xDisconnect(p);
156573 }
156574 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
156575 sqlite3DbFree(db, pVTab);
156576 }
156577}
156578
156579/*
156580** Table p is a virtual table. This function moves all elements in the
156581** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
156582** database connections to be disconnected at the next opportunity.
156583** Except, if argument db is not NULL, then the entry associated with
156584** connection db is left in the p->u.vtab.p list.
156585*/
156586static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
156587 VTable *pRet = 0;
156588 VTable *pVTable;
156589
156590 assert( IsVirtual(p) )((void) (0));
156591 pVTable = p->u.vtab.p;
156592 p->u.vtab.p = 0;
156593
156594 /* Assert that the mutex (if any) associated with the BtShared database
156595 ** that contains table p is held by the caller. See header comments
156596 ** above function sqlite3VtabUnlockList() for an explanation of why
156597 ** this makes it safe to access the sqlite3.pDisconnect list of any
156598 ** database connection that may have an entry in the p->u.vtab.p list.
156599 */
156600 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
156601
156602 while( pVTable ){
156603 sqlite3 *db2 = pVTable->db;
156604 VTable *pNext = pVTable->pNext;
156605 assert( db2 )((void) (0));
156606 if( db2==db ){
156607 pRet = pVTable;
156608 p->u.vtab.p = pRet;
156609 pRet->pNext = 0;
156610 }else{
156611 pVTable->pNext = db2->pDisconnect;
156612 db2->pDisconnect = pVTable;
156613 }
156614 pVTable = pNext;
156615 }
156616
156617 assert( !db || pRet )((void) (0));
156618 return pRet;
156619}
156620
156621/*
156622** Table *p is a virtual table. This function removes the VTable object
156623** for table *p associated with database connection db from the linked
156624** list in p->pVTab. It also decrements the VTable ref count. This is
156625** used when closing database connection db to free all of its VTable
156626** objects without disturbing the rest of the Schema object (which may
156627** be being used by other shared-cache connections).
156628*/
156629SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
156630 VTable **ppVTab;
156631
156632 assert( IsVirtual(p) )((void) (0));
156633 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
156634 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
156635
156636 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
156637 if( (*ppVTab)->db==db ){
156638 VTable *pVTab = *ppVTab;
156639 *ppVTab = pVTab->pNext;
156640 sqlite3VtabUnlock(pVTab);
156641 break;
156642 }
156643 }
156644}
156645
156646
156647/*
156648** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
156649**
156650** This function may only be called when the mutexes associated with all
156651** shared b-tree databases opened using connection db are held by the
156652** caller. This is done to protect the sqlite3.pDisconnect list. The
156653** sqlite3.pDisconnect list is accessed only as follows:
156654**
156655** 1) By this function. In this case, all BtShared mutexes and the mutex
156656** associated with the database handle itself must be held.
156657**
156658** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
156659** the sqlite3.pDisconnect list. In this case either the BtShared mutex
156660** associated with the database the virtual table is stored in is held
156661** or, if the virtual table is stored in a non-sharable database, then
156662** the database handle mutex is held.
156663**
156664** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
156665** by multiple threads. It is thread-safe.
156666*/
156667SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3 *db){
156668 VTable *p = db->pDisconnect;
156669
156670 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
156671 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
156672
156673 if( p ){
156674 db->pDisconnect = 0;
156675 do {
156676 VTable *pNext = p->pNext;
156677 sqlite3VtabUnlock(p);
156678 p = pNext;
156679 }while( p );
156680 }
156681}
156682
156683/*
156684** Clear any and all virtual-table information from the Table record.
156685** This routine is called, for example, just before deleting the Table
156686** record.
156687**
156688** Since it is a virtual-table, the Table structure contains a pointer
156689** to the head of a linked list of VTable structures. Each VTable
156690** structure is associated with a single sqlite3* user of the schema.
156691** The reference count of the VTable structure associated with database
156692** connection db is decremented immediately (which may lead to the
156693** structure being xDisconnected and free). Any other VTable structures
156694** in the list are moved to the sqlite3.pDisconnect list of the associated
156695** database connection.
156696*/
156697SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table *p){
156698 assert( IsVirtual(p) )((void) (0));
156699 assert( db!=0 )((void) (0));
156700 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
156701 if( p->u.vtab.azArg ){
156702 int i;
156703 for(i=0; i<p->u.vtab.nArg; i++){
156704 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
156705 }
156706 sqlite3DbFree(db, p->u.vtab.azArg);
156707 }
156708}
156709
156710/*
156711** Add a new module argument to pTable->u.vtab.azArg[].
156712** The string is not copied - the pointer is stored. The
156713** string will be freed automatically when the table is
156714** deleted.
156715*/
156716static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
156717 sqlite3_int64 nBytes;
156718 char **azModuleArg;
156719 sqlite3 *db = pParse->db;
156720
156721 assert( IsVirtual(pTable) )((void) (0));
156722 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
156723 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN2] ){
156724 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
156725 }
156726 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
156727 if( azModuleArg==0 ){
156728 sqlite3DbFree(db, zArg);
156729 }else{
156730 int i = pTable->u.vtab.nArg++;
156731 azModuleArg[i] = zArg;
156732 azModuleArg[i+1] = 0;
156733 pTable->u.vtab.azArg = azModuleArg;
156734 }
156735}
156736
156737/*
156738** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
156739** statement. The module name has been parsed, but the optional list
156740** of parameters that follow the module name are still pending.
156741*/
156742SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(
156743 Parse *pParse, /* Parsing context */
156744 Token *pName1, /* Name of new table, or database name */
156745 Token *pName2, /* Name of new table or NULL */
156746 Token *pModuleName, /* Name of the module for the virtual table */
156747 int ifNotExists /* No error if the table already exists */
156748){
156749 Table *pTable; /* The new virtual table */
156750 sqlite3 *db; /* Database connection */
156751
156752 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
156753 pTable = pParse->pNewTable;
156754 if( pTable==0 ) return;
156755 assert( 0==pTable->pIndex )((void) (0));
156756 pTable->eTabType = TABTYP_VTAB1;
156757
156758 db = pParse->db;
156759
156760 assert( pTable->u.vtab.nArg==0 )((void) (0));
156761 addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
156762 addModuleArgument(pParse, pTable, 0);
156763 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
156764 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)((void) (0))
156765 || (pParse->sNameToken.z==pName1->z && pName2->z==0)((void) (0))
156766 )((void) (0));
156767 pParse->sNameToken.n = (int)(
156768 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
156769 );
156770
156771#ifndef SQLITE_OMIT_AUTHORIZATION
156772 /* Creating a virtual table invokes the authorization callback twice.
156773 ** The first invocation, to obtain permission to INSERT a row into the
156774 ** sqlite_schema table, has already been made by sqlite3StartTable().
156775 ** The second call, to obtain permission to create the table, is made now.
156776 */
156777 if( pTable->u.vtab.azArg ){
156778 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
156779 assert( iDb>=0 )((void) (0)); /* The database the table is being created in */
156780 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE29, pTable->zName,
156781 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
156782 }
156783#endif
156784}
156785
156786/*
156787** This routine takes the module argument that has been accumulating
156788** in pParse->zArg[] and appends it to the list of arguments on the
156789** virtual table currently under construction in pParse->pTable.
156790*/
156791static void addArgumentToVtab(Parse *pParse){
156792 if( pParse->sArg.z && pParse->pNewTable ){
156793 const char *z = (const char*)pParse->sArg.z;
156794 int n = pParse->sArg.n;
156795 sqlite3 *db = pParse->db;
156796 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
156797 }
156798}
156799
156800/*
156801** The parser calls this routine after the CREATE VIRTUAL TABLE statement
156802** has been completely parsed.
156803*/
156804SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
156805 Table *pTab = pParse->pNewTable; /* The table being constructed */
156806 sqlite3 *db = pParse->db; /* The database connection */
156807
156808 if( pTab==0 ) return;
156809 assert( IsVirtual(pTab) )((void) (0));
156810 addArgumentToVtab(pParse);
156811 pParse->sArg.z = 0;
156812 if( pTab->u.vtab.nArg<1 ) return;
156813
156814 /* If the CREATE VIRTUAL TABLE statement is being entered for the
156815 ** first time (in other words if the virtual table is actually being
156816 ** created now instead of just being read out of sqlite_schema) then
156817 ** do additional initialization work and store the statement text
156818 ** in the sqlite_schema table.
156819 */
156820 if( !db->init.busy ){
156821 char *zStmt;
156822 char *zWhere;
156823 int iDb;
156824 int iReg;
156825 Vdbe *v;
156826
156827 sqlite3MayAbort(pParse);
156828
156829 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
156830 if( pEnd ){
156831 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
156832 }
156833 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
156834
156835 /* A slot for the record has already been allocated in the
156836 ** schema table. We just need to update that slot with all
156837 ** the information we've collected.
156838 **
156839 ** The VM register number pParse->regRowid holds the rowid of an
156840 ** entry in the sqlite_schema table that was created for this vtab
156841 ** by sqlite3StartTable().
156842 */
156843 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
156844 sqlite3NestedParse(pParse,
156845 "UPDATE %Q." LEGACY_SCHEMA_TABLE"sqlite_master" " "
156846 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
156847 "WHERE rowid=#%d",
156848 db->aDb[iDb].zDbSName,
156849 pTab->zName,
156850 pTab->zName,
156851 zStmt,
156852 pParse->regRowid
156853 );
156854 v = sqlite3GetVdbe(pParse);
156855 sqlite3ChangeCookie(pParse, iDb);
156856
156857 sqlite3VdbeAddOp0(v, OP_Expire166);
156858 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
156859 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere, 0);
156860 sqlite3DbFree(db, zStmt);
156861
156862 iReg = ++pParse->nMem;
156863 sqlite3VdbeLoadString(v, iReg, pTab->zName);
156864 sqlite3VdbeAddOp2(v, OP_VCreate171, iDb, iReg);
156865 }else{
156866 /* If we are rereading the sqlite_schema table create the in-memory
156867 ** record of the table. */
156868 Table *pOld;
156869 Schema *pSchema = pTab->pSchema;
156870 const char *zName = pTab->zName;
156871 assert( zName!=0 )((void) (0));
156872 sqlite3MarkAllShadowTablesOf(db, pTab);
156873 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
156874 if( pOld ){
156875 sqlite3OomFault(db);
156876 assert( pTab==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
156877 return;
156878 }
156879 pParse->pNewTable = 0;
156880 }
156881}
156882
156883/*
156884** The parser calls this routine when it sees the first token
156885** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
156886*/
156887SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse *pParse){
156888 addArgumentToVtab(pParse);
156889 pParse->sArg.z = 0;
156890 pParse->sArg.n = 0;
156891}
156892
156893/*
156894** The parser calls this routine for each token after the first token
156895** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
156896*/
156897SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse *pParse, Token *p){
156898 Token *pArg = &pParse->sArg;
156899 if( pArg->z==0 ){
156900 pArg->z = p->z;
156901 pArg->n = p->n;
156902 }else{
156903 assert(pArg->z <= p->z)((void) (0));
156904 pArg->n = (int)(&p->z[p->n] - pArg->z);
156905 }
156906}
156907
156908/*
156909** Invoke a virtual table constructor (either xCreate or xConnect). The
156910** pointer to the function to invoke is passed as the fourth parameter
156911** to this procedure.
156912*/
156913static int vtabCallConstructor(
156914 sqlite3 *db,
156915 Table *pTab,
156916 Module *pMod,
156917 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
156918 char **pzErr
156919){
156920 VtabCtx sCtx;
156921 VTable *pVTable;
156922 int rc;
156923 const char *const*azArg;
156924 int nArg = pTab->u.vtab.nArg;
156925 char *zErr = 0;
156926 char *zModuleName;
156927 int iDb;
156928 VtabCtx *pCtx;
156929
156930 assert( IsVirtual(pTab) )((void) (0));
156931 azArg = (const char *const*)pTab->u.vtab.azArg;
156932
156933 /* Check that the virtual-table is not already being initialized */
156934 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
156935 if( pCtx->pTab==pTab ){
156936 *pzErr = sqlite3MPrintf(db,
156937 "vtable constructor called recursively: %s", pTab->zName
156938 );
156939 return SQLITE_LOCKED6;
156940 }
156941 }
156942
156943 zModuleName = sqlite3DbStrDup(db, pTab->zName);
156944 if( !zModuleName ){
156945 return SQLITE_NOMEM_BKPT7;
156946 }
156947
156948 pVTable = sqlite3MallocZero(sizeof(VTable));
156949 if( !pVTable ){
156950 sqlite3OomFault(db);
156951 sqlite3DbFree(db, zModuleName);
156952 return SQLITE_NOMEM_BKPT7;
156953 }
156954 pVTable->db = db;
156955 pVTable->pMod = pMod;
156956 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal1;
156957
156958 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
156959 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
156960
156961 /* Invoke the virtual table constructor */
156962 assert( &db->pVtabCtx )((void) (0));
156963 assert( xConstruct )((void) (0));
156964 sCtx.pTab = pTab;
156965 sCtx.pVTable = pVTable;
156966 sCtx.pPrior = db->pVtabCtx;
156967 sCtx.bDeclared = 0;
156968 db->pVtabCtx = &sCtx;
156969 pTab->nTabRef++;
156970 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
156971 assert( pTab!=0 )((void) (0));
156972 assert( pTab->nTabRef>1 || rc!=SQLITE_OK )((void) (0));
156973 sqlite3DeleteTable(db, pTab);
156974 db->pVtabCtx = sCtx.pPrior;
156975 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
156976 assert( sCtx.pTab==pTab )((void) (0));
156977
156978 if( SQLITE_OK0!=rc ){
156979 if( zErr==0 ){
156980 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
156981 }else {
156982 *pzErr = sqlite3MPrintf(db, "%s", zErr);
156983 sqlite3_free(zErr);
156984 }
156985 sqlite3DbFree(db, pVTable);
156986 }else if( ALWAYS(pVTable->pVtab)(pVTable->pVtab) ){
156987 /* Justification of ALWAYS(): A correct vtab constructor must allocate
156988 ** the sqlite3_vtab object if successful. */
156989 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
156990 pVTable->pVtab->pModule = pMod->pModule;
156991 pMod->nRefModule++;
156992 pVTable->nRef = 1;
156993 if( sCtx.bDeclared==0 ){
156994 const char *zFormat = "vtable constructor did not declare schema: %s";
156995 *pzErr = sqlite3MPrintf(db, zFormat, zModuleName);
156996 sqlite3VtabUnlock(pVTable);
156997 rc = SQLITE_ERROR1;
156998 }else{
156999 int iCol;
157000 u16 oooHidden = 0;
157001 /* If everything went according to plan, link the new VTable structure
157002 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
157003 ** columns of the table to see if any of them contain the token "hidden".
157004 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
157005 ** the type string. */
157006 pVTable->pNext = pTab->u.vtab.p;
157007 pTab->u.vtab.p = pVTable;
157008
157009 for(iCol=0; iCol<pTab->nCol; iCol++){
157010 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
157011 int nType;
157012 int i = 0;
157013 nType = sqlite3Strlen30(zType);
157014 for(i=0; i<nType; i++){
157015 if( 0==sqlite3StrNICmpsqlite3_strnicmp("hidden", &zType[i], 6)
157016 && (i==0 || zType[i-1]==' ')
157017 && (zType[i+6]=='\0' || zType[i+6]==' ')
157018 ){
157019 break;
157020 }
157021 }
157022 if( i<nType ){
157023 int j;
157024 int nDel = 6 + (zType[i+6] ? 1 : 0);
157025 for(j=i; (j+nDel)<=nType; j++){
157026 zType[j] = zType[j+nDel];
157027 }
157028 if( zType[i]=='\0' && i>0 ){
157029 assert(zType[i-1]==' ')((void) (0));
157030 zType[i-1] = '\0';
157031 }
157032 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN0x0002;
157033 pTab->tabFlags |= TF_HasHidden0x00000002;
157034 oooHidden = TF_OOOHidden0x00000400;
157035 }else{
157036 pTab->tabFlags |= oooHidden;
157037 }
157038 }
157039 }
157040 }
157041
157042 sqlite3DbFree(db, zModuleName);
157043 return rc;
157044}
157045
157046/*
157047** This function is invoked by the parser to call the xConnect() method
157048** of the virtual table pTab. If an error occurs, an error code is returned
157049** and an error left in pParse.
157050**
157051** This call is a no-op if table pTab is not a virtual table.
157052*/
157053SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
157054 sqlite3 *db = pParse->db;
157055 const char *zMod;
157056 Module *pMod;
157057 int rc;
157058
157059 assert( pTab )((void) (0));
157060 assert( IsVirtual(pTab) )((void) (0));
157061 if( sqlite3GetVTable(db, pTab) ){
157062 return SQLITE_OK0;
157063 }
157064
157065 /* Locate the required virtual table module */
157066 zMod = pTab->u.vtab.azArg[0];
157067 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
157068
157069 if( !pMod ){
157070 const char *zModule = pTab->u.vtab.azArg[0];
157071 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
157072 rc = SQLITE_ERROR1;
157073 }else{
157074 char *zErr = 0;
157075 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
157076 if( rc!=SQLITE_OK0 ){
157077 sqlite3ErrorMsg(pParse, "%s", zErr);
157078 pParse->rc = rc;
157079 }
157080 sqlite3DbFree(db, zErr);
157081 }
157082
157083 return rc;
157084}
157085/*
157086** Grow the db->aVTrans[] array so that there is room for at least one
157087** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
157088*/
157089static int growVTrans(sqlite3 *db){
157090 const int ARRAY_INCR = 5;
157091
157092 /* Grow the sqlite3.aVTrans array if required */
157093 if( (db->nVTrans%ARRAY_INCR)==0 ){
157094 VTable **aVTrans;
157095 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
157096 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
157097 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
157098 if( !aVTrans ){
157099 return SQLITE_NOMEM_BKPT7;
157100 }
157101 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
157102 db->aVTrans = aVTrans;
157103 }
157104
157105 return SQLITE_OK0;
157106}
157107
157108/*
157109** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
157110** have already been reserved using growVTrans().
157111*/
157112static void addToVTrans(sqlite3 *db, VTable *pVTab){
157113 /* Add pVtab to the end of sqlite3.aVTrans */
157114 db->aVTrans[db->nVTrans++] = pVTab;
157115 sqlite3VtabLock(pVTab);
157116}
157117
157118/*
157119** This function is invoked by the vdbe to call the xCreate method
157120** of the virtual table named zTab in database iDb.
157121**
157122** If an error occurs, *pzErr is set to point to an English language
157123** description of the error and an SQLITE_XXX error code is returned.
157124** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
157125*/
157126SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
157127 int rc = SQLITE_OK0;
157128 Table *pTab;
157129 Module *pMod;
157130 const char *zMod;
157131
157132 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
157133 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p )((void) (0));
157134
157135 /* Locate the required virtual table module */
157136 zMod = pTab->u.vtab.azArg[0];
157137 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
157138
157139 /* If the module has been registered and includes a Create method,
157140 ** invoke it now. If the module has not been registered, return an
157141 ** error. Otherwise, do nothing.
157142 */
157143 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
157144 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
157145 rc = SQLITE_ERROR1;
157146 }else{
157147 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
157148 }
157149
157150 /* Justification of ALWAYS(): The xConstructor method is required to
157151 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
157152 if( rc==SQLITE_OK0 && ALWAYS(sqlite3GetVTable(db, pTab))(sqlite3GetVTable(db, pTab)) ){
157153 rc = growVTrans(db);
157154 if( rc==SQLITE_OK0 ){
157155 addToVTrans(db, sqlite3GetVTable(db, pTab));
157156 }
157157 }
157158
157159 return rc;
157160}
157161
157162/*
157163** This function is used to set the schema of a virtual table. It is only
157164** valid to call this function from within the xCreate() or xConnect() of a
157165** virtual table module.
157166*/
157167SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
157168 VtabCtx *pCtx;
157169 int rc = SQLITE_OK0;
157170 Table *pTab;
157171 Parse sParse;
157172 int initBusy;
157173 int i;
157174 const unsigned char *z;
157175 static const u8 aKeyword[] = { TK_CREATE17, TK_TABLE16, 0 };
157176
157177#ifdef SQLITE_ENABLE_API_ARMOR1
157178 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
157179 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157179);
157180 }
157181#endif
157182
157183 /* Verify that the first two keywords in the CREATE TABLE statement
157184 ** really are "CREATE" and "TABLE". If this is not the case, then
157185 ** sqlite3_declare_vtab() is being misused.
157186 */
157187 z = (const unsigned char*)zCreateTable;
157188 for(i=0; aKeyword[i]; i++){
157189 int tokenType = 0;
157190 do{ z += sqlite3GetToken(z, &tokenType); }while( tokenType==TK_SPACE184 );
157191 if( tokenType!=aKeyword[i] ){
157192 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "syntax error");
157193 return SQLITE_ERROR1;
157194 }
157195 }
157196
157197 sqlite3_mutex_enter(db->mutex);
157198 pCtx = db->pVtabCtx;
157199 if( !pCtx || pCtx->bDeclared ){
157200 sqlite3Error(db, SQLITE_MISUSE_BKPTsqlite3MisuseError(157200));
157201 sqlite3_mutex_leave(db->mutex);
157202 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157202);
157203 }
157204
157205 pTab = pCtx->pTab;
157206 assert( IsVirtual(pTab) )((void) (0));
157207
157208 sqlite3ParseObjectInit(&sParse, db);
157209 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB1;
157210 sParse.disableTriggers = 1;
157211 /* We should never be able to reach this point while loading the
157212 ** schema. Nevertheless, defend against that (turn off db->init.busy)
157213 ** in case a bug arises. */
157214 assert( db->init.busy==0 )((void) (0));
157215 initBusy = db->init.busy;
157216 db->init.busy = 0;
157217 sParse.nQueryLoop = 1;
157218 if( SQLITE_OK0==sqlite3RunParser(&sParse, zCreateTable) ){
157219 assert( sParse.pNewTable!=0 )((void) (0));
157220 assert( !db->mallocFailed )((void) (0));
157221 assert( IsOrdinaryTable(sParse.pNewTable) )((void) (0));
157222 assert( sParse.zErrMsg==0 )((void) (0));
157223 if( !pTab->aCol ){
157224 Table *pNew = sParse.pNewTable;
157225 Index *pIdx;
157226 pTab->aCol = pNew->aCol;
157227 assert( IsOrdinaryTable(pNew) )((void) (0));
157228 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
157229 pTab->nNVCol = pTab->nCol = pNew->nCol;
157230 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid0x00000080|TF_NoVisibleRowid0x00000200);
157231 pNew->nCol = 0;
157232 pNew->aCol = 0;
157233 assert( pTab->pIndex==0 )((void) (0));
157234 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 )((void) (0));
157235 if( !HasRowid(pNew)(((pNew)->tabFlags & 0x00000080)==0)
157236 && pCtx->pVTable->pMod->pModule->xUpdate!=0
157237 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
157238 ){
157239 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
157240 ** or else must have a single-column PRIMARY KEY */
157241 rc = SQLITE_ERROR1;
157242 }
157243 pIdx = pNew->pIndex;
157244 if( pIdx ){
157245 assert( pIdx->pNext==0 )((void) (0));
157246 pTab->pIndex = pIdx;
157247 pNew->pIndex = 0;
157248 pIdx->pTable = pTab;
157249 }
157250 }
157251 pCtx->bDeclared = 1;
157252 }else{
157253 sqlite3ErrorWithMsg(db, SQLITE_ERROR1,
157254 (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg);
157255 sqlite3DbFree(db, sParse.zErrMsg);
157256 rc = SQLITE_ERROR1;
157257 }
157258 sParse.eParseMode = PARSE_MODE_NORMAL0;
157259
157260 if( sParse.pVdbe ){
157261 sqlite3VdbeFinalize(sParse.pVdbe);
157262 }
157263 sqlite3DeleteTable(db, sParse.pNewTable);
157264 sqlite3ParseObjectReset(&sParse);
157265 db->init.busy = initBusy;
157266
157267 assert( (rc&0xff)==rc )((void) (0));
157268 rc = sqlite3ApiExit(db, rc);
157269 sqlite3_mutex_leave(db->mutex);
157270 return rc;
157271}
157272
157273/*
157274** This function is invoked by the vdbe to call the xDestroy method
157275** of the virtual table named zTab in database iDb. This occurs
157276** when a DROP TABLE is mentioned.
157277**
157278** This call is a no-op if zTab is not a virtual table.
157279*/
157280SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
157281 int rc = SQLITE_OK0;
157282 Table *pTab;
157283
157284 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
157285 if( ALWAYS(pTab!=0)(pTab!=0)
157286 && ALWAYS(IsVirtual(pTab))(((pTab)->eTabType==1))
157287 && ALWAYS(pTab->u.vtab.p!=0)(pTab->u.vtab.p!=0)
157288 ){
157289 VTable *p;
157290 int (*xDestroy)(sqlite3_vtab *);
157291 for(p=pTab->u.vtab.p; p; p=p->pNext){
157292 assert( p->pVtab )((void) (0));
157293 if( p->pVtab->nRef>0 ){
157294 return SQLITE_LOCKED6;
157295 }
157296 }
157297 p = vtabDisconnectAll(db, pTab);
157298 xDestroy = p->pMod->pModule->xDestroy;
157299 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
157300 assert( xDestroy!=0 )((void) (0));
157301 pTab->nTabRef++;
157302 rc = xDestroy(p->pVtab);
157303 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
157304 if( rc==SQLITE_OK0 ){
157305 assert( pTab->u.vtab.p==p && p->pNext==0 )((void) (0));
157306 p->pVtab = 0;
157307 pTab->u.vtab.p = 0;
157308 sqlite3VtabUnlock(p);
157309 }
157310 sqlite3DeleteTable(db, pTab);
157311 }
157312
157313 return rc;
157314}
157315
157316/*
157317** This function invokes either the xRollback or xCommit method
157318** of each of the virtual tables in the sqlite3.aVTrans array. The method
157319** called is identified by the second argument, "offset", which is
157320** the offset of the method to call in the sqlite3_module structure.
157321**
157322** The array is cleared after invoking the callbacks.
157323*/
157324static void callFinaliser(sqlite3 *db, int offset){
157325 int i;
157326 if( db->aVTrans ){
157327 VTable **aVTrans = db->aVTrans;
157328 db->aVTrans = 0;
157329 for(i=0; i<db->nVTrans; i++){
157330 VTable *pVTab = aVTrans[i];
157331 sqlite3_vtab *p = pVTab->pVtab;
157332 if( p ){
157333 int (*x)(sqlite3_vtab *);
157334 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
157335 if( x ) x(p);
157336 }
157337 pVTab->iSavepoint = 0;
157338 sqlite3VtabUnlock(pVTab);
157339 }
157340 sqlite3DbFree(db, aVTrans);
157341 db->nVTrans = 0;
157342 }
157343}
157344
157345/*
157346** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
157347** array. Return the error code for the first error that occurs, or
157348** SQLITE_OK if all xSync operations are successful.
157349**
157350** If an error message is available, leave it in p->zErrMsg.
157351*/
157352SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
157353 int i;
157354 int rc = SQLITE_OK0;
157355 VTable **aVTrans = db->aVTrans;
157356
157357 db->aVTrans = 0;
157358 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
157359 int (*x)(sqlite3_vtab *);
157360 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
157361 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
157362 rc = x(pVtab);
157363 sqlite3VtabImportErrmsg(p, pVtab);
157364 }
157365 }
157366 db->aVTrans = aVTrans;
157367 return rc;
157368}
157369
157370/*
157371** Invoke the xRollback method of all virtual tables in the
157372** sqlite3.aVTrans array. Then clear the array itself.
157373*/
157374SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db){
157375 callFinaliser(db, offsetof(sqlite3_module,xRollback)__builtin_offsetof(sqlite3_module, xRollback));
157376 return SQLITE_OK0;
157377}
157378
157379/*
157380** Invoke the xCommit method of all virtual tables in the
157381** sqlite3.aVTrans array. Then clear the array itself.
157382*/
157383SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db){
157384 callFinaliser(db, offsetof(sqlite3_module,xCommit)__builtin_offsetof(sqlite3_module, xCommit));
157385 return SQLITE_OK0;
157386}
157387
157388/*
157389** If the virtual table pVtab supports the transaction interface
157390** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
157391** not currently open, invoke the xBegin method now.
157392**
157393** If the xBegin call is successful, place the sqlite3_vtab pointer
157394** in the sqlite3.aVTrans array.
157395*/
157396SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
157397 int rc = SQLITE_OK0;
157398 const sqlite3_module *pModule;
157399
157400 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
157401 ** than zero, then this function is being called from within a
157402 ** virtual module xSync() callback. It is illegal to write to
157403 ** virtual module tables in this case, so return SQLITE_LOCKED.
157404 */
157405 if( sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ){
157406 return SQLITE_LOCKED6;
157407 }
157408 if( !pVTab ){
157409 return SQLITE_OK0;
157410 }
157411 pModule = pVTab->pVtab->pModule;
157412
157413 if( pModule->xBegin ){
157414 int i;
157415
157416 /* If pVtab is already in the aVTrans array, return early */
157417 for(i=0; i<db->nVTrans; i++){
157418 if( db->aVTrans[i]==pVTab ){
157419 return SQLITE_OK0;
157420 }
157421 }
157422
157423 /* Invoke the xBegin method. If successful, add the vtab to the
157424 ** sqlite3.aVTrans[] array. */
157425 rc = growVTrans(db);
157426 if( rc==SQLITE_OK0 ){
157427 rc = pModule->xBegin(pVTab->pVtab);
157428 if( rc==SQLITE_OK0 ){
157429 int iSvpt = db->nStatement + db->nSavepoint;
157430 addToVTrans(db, pVTab);
157431 if( iSvpt && pModule->xSavepoint ){
157432 pVTab->iSavepoint = iSvpt;
157433 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
157434 }
157435 }
157436 }
157437 }
157438 return rc;
157439}
157440
157441/*
157442** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
157443** virtual tables that currently have an open transaction. Pass iSavepoint
157444** as the second argument to the virtual table method invoked.
157445**
157446** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
157447** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
157448** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
157449** an open transaction is invoked.
157450**
157451** If any virtual table method returns an error code other than SQLITE_OK,
157452** processing is abandoned and the error returned to the caller of this
157453** function immediately. If all calls to virtual table methods are successful,
157454** SQLITE_OK is returned.
157455*/
157456SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
157457 int rc = SQLITE_OK0;
157458
157459 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN )((void) (0));
157460 assert( iSavepoint>=-1 )((void) (0));
157461 if( db->aVTrans ){
157462 int i;
157463 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
157464 VTable *pVTab = db->aVTrans[i];
157465 const sqlite3_module *pMod = pVTab->pMod->pModule;
157466 if( pVTab->pVtab && pMod->iVersion>=2 ){
157467 int (*xMethod)(sqlite3_vtab *, int);
157468 sqlite3VtabLock(pVTab);
157469 switch( op ){
157470 case SAVEPOINT_BEGIN0:
157471 xMethod = pMod->xSavepoint;
157472 pVTab->iSavepoint = iSavepoint+1;
157473 break;
157474 case SAVEPOINT_ROLLBACK2:
157475 xMethod = pMod->xRollbackTo;
157476 break;
157477 default:
157478 xMethod = pMod->xRelease;
157479 break;
157480 }
157481 if( xMethod && pVTab->iSavepoint>iSavepoint ){
157482 u64 savedFlags = (db->flags & SQLITE_Defensive0x10000000);
157483 db->flags &= ~(u64)SQLITE_Defensive0x10000000;
157484 rc = xMethod(pVTab->pVtab, iSavepoint);
157485 db->flags |= savedFlags;
157486 }
157487 sqlite3VtabUnlock(pVTab);
157488 }
157489 }
157490 }
157491 return rc;
157492}
157493
157494/*
157495** The first parameter (pDef) is a function implementation. The
157496** second parameter (pExpr) is the first argument to this function.
157497** If pExpr is a column in a virtual table, then let the virtual
157498** table implementation have an opportunity to overload the function.
157499**
157500** This routine is used to allow virtual table implementations to
157501** overload MATCH, LIKE, GLOB, and REGEXP operators.
157502**
157503** Return either the pDef argument (indicating no change) or a
157504** new FuncDef structure that is marked as ephemeral using the
157505** SQLITE_FUNC_EPHEM flag.
157506*/
157507SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(
157508 sqlite3 *db, /* Database connection for reporting malloc problems */
157509 FuncDef *pDef, /* Function to possibly overload */
157510 int nArg, /* Number of arguments to the function */
157511 Expr *pExpr /* First argument to the function */
157512){
157513 Table *pTab;
157514 sqlite3_vtab *pVtab;
157515 sqlite3_module *pMod;
157516 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
157517 void *pArg = 0;
157518 FuncDef *pNew;
157519 int rc = 0;
157520
157521 /* Check to see the left operand is a column in a virtual table */
157522 if( NEVER(pExpr==0)(pExpr==0) ) return pDef;
157523 if( pExpr->op!=TK_COLUMN168 ) return pDef;
157524 assert( ExprUseYTab(pExpr) )((void) (0));
157525 pTab = pExpr->y.pTab;
157526 if( NEVER(pTab==0)(pTab==0) ) return pDef;
157527 if( !IsVirtual(pTab)((pTab)->eTabType==1) ) return pDef;
157528 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
157529 assert( pVtab!=0 )((void) (0));
157530 assert( pVtab->pModule!=0 )((void) (0));
157531 pMod = (sqlite3_module *)pVtab->pModule;
157532 if( pMod->xFindFunction==0 ) return pDef;
157533
157534 /* Call the xFindFunction method on the virtual table implementation
157535 ** to see if the implementation wants to overload this function.
157536 **
157537 ** Though undocumented, we have historically always invoked xFindFunction
157538 ** with an all lower-case function name. Continue in this tradition to
157539 ** avoid any chance of an incompatibility.
157540 */
157541#ifdef SQLITE_DEBUG
157542 {
157543 int i;
157544 for(i=0; pDef->zName[i]; i++){
157545 unsigned char x = (unsigned char)pDef->zName[i];
157546 assert( x==sqlite3UpperToLower[x] )((void) (0));
157547 }
157548 }
157549#endif
157550 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
157551 if( rc==0 ){
157552 return pDef;
157553 }
157554
157555 /* Create a new ephemeral function definition for the overloaded
157556 ** function */
157557 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
157558 + sqlite3Strlen30(pDef->zName) + 1);
157559 if( pNew==0 ){
157560 return pDef;
157561 }
157562 *pNew = *pDef;
157563 pNew->zName = (const char*)&pNew[1];
157564 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
157565 pNew->xSFunc = xSFunc;
157566 pNew->pUserData = pArg;
157567 pNew->funcFlags |= SQLITE_FUNC_EPHEM0x0010;
157568 return pNew;
157569}
157570
157571/*
157572** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
157573** array so that an OP_VBegin will get generated for it. Add pTab to the
157574** array if it is missing. If pTab is already in the array, this routine
157575** is a no-op.
157576*/
157577SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
157578 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
157579 int i, n;
157580 Table **apVtabLock;
157581
157582 assert( IsVirtual(pTab) )((void) (0));
157583 for(i=0; i<pToplevel->nVtabLock; i++){
157584 if( pTab==pToplevel->apVtabLock[i] ) return;
157585 }
157586 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
157587 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
157588 if( apVtabLock ){
157589 pToplevel->apVtabLock = apVtabLock;
157590 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
157591 }else{
157592 sqlite3OomFault(pToplevel->db);
157593 }
157594}
157595
157596/*
157597** Check to see if virtual table module pMod can be have an eponymous
157598** virtual table instance. If it can, create one if one does not already
157599** exist. Return non-zero if either the eponymous virtual table instance
157600** exists when this routine returns or if an attempt to create it failed
157601** and an error message was left in pParse.
157602**
157603** An eponymous virtual table instance is one that is named after its
157604** module, and more importantly, does not require a CREATE VIRTUAL TABLE
157605** statement in order to come into existence. Eponymous virtual table
157606** instances always exist. They cannot be DROP-ed.
157607**
157608** Any virtual table module for which xConnect and xCreate are the same
157609** method can have an eponymous virtual table instance.
157610*/
157611SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
157612 const sqlite3_module *pModule = pMod->pModule;
157613 Table *pTab;
157614 char *zErr = 0;
157615 int rc;
157616 sqlite3 *db = pParse->db;
157617 if( pMod->pEpoTab ) return 1;
157618 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
157619 pTab = sqlite3DbMallocZero(db, sizeof(Table));
157620 if( pTab==0 ) return 0;
157621 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
157622 if( pTab->zName==0 ){
157623 sqlite3DbFree(db, pTab);
157624 return 0;
157625 }
157626 pMod->pEpoTab = pTab;
157627 pTab->nTabRef = 1;
157628 pTab->eTabType = TABTYP_VTAB1;
157629 pTab->pSchema = db->aDb[0].pSchema;
157630 assert( pTab->u.vtab.nArg==0 )((void) (0));
157631 pTab->iPKey = -1;
157632 pTab->tabFlags |= TF_Eponymous0x00008000;
157633 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
157634 addModuleArgument(pParse, pTab, 0);
157635 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
157636 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
157637 if( rc ){
157638 sqlite3ErrorMsg(pParse, "%s", zErr);
157639 sqlite3DbFree(db, zErr);
157640 sqlite3VtabEponymousTableClear(db, pMod);
157641 }
157642 return 1;
157643}
157644
157645/*
157646** Erase the eponymous virtual table instance associated with
157647** virtual table module pMod, if it exists.
157648*/
157649SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
157650 Table *pTab = pMod->pEpoTab;
157651 if( pTab!=0 ){
157652 /* Mark the table as Ephemeral prior to deleting it, so that the
157653 ** sqlite3DeleteTable() routine will know that it is not stored in
157654 ** the schema. */
157655 pTab->tabFlags |= TF_Ephemeral0x00004000;
157656 sqlite3DeleteTable(db, pTab);
157657 pMod->pEpoTab = 0;
157658 }
157659}
157660
157661/*
157662** Return the ON CONFLICT resolution mode in effect for the virtual
157663** table update operation currently in progress.
157664**
157665** The results of this routine are undefined unless it is called from
157666** within an xUpdate method.
157667*/
157668SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
157669 static const unsigned char aMap[] = {
157670 SQLITE_ROLLBACK1, SQLITE_ABORT4, SQLITE_FAIL3, SQLITE_IGNORE2, SQLITE_REPLACE5
157671 };
157672#ifdef SQLITE_ENABLE_API_ARMOR1
157673 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157673);
157674#endif
157675 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 )((void) (0));
157676 assert( OE_Ignore==4 && OE_Replace==5 )((void) (0));
157677 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 )((void) (0));
157678 return (int)aMap[db->vtabOnConflict-1];
157679}
157680
157681/*
157682** Call from within the xCreate() or xConnect() methods to provide
157683** the SQLite core with additional information about the behavior
157684** of the virtual table being implemented.
157685*/
157686SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
157687 va_list ap;
157688 int rc = SQLITE_OK0;
157689 VtabCtx *p;
157690
157691#ifdef SQLITE_ENABLE_API_ARMOR1
157692 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157692);
157693#endif
157694 sqlite3_mutex_enter(db->mutex);
157695 p = db->pVtabCtx;
157696 if( !p ){
157697 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(157697);
157698 }else{
157699 assert( p->pTab==0 || IsVirtual(p->pTab) )((void) (0));
157700 va_start(ap, op)__builtin_va_start(ap, op);
157701 switch( op ){
157702 case SQLITE_VTAB_CONSTRAINT_SUPPORT1: {
157703 p->pVTable->bConstraint = (u8)va_arg(ap, int)__builtin_va_arg(ap, int);
157704 break;
157705 }
157706 case SQLITE_VTAB_INNOCUOUS2: {
157707 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low0;
157708 break;
157709 }
157710 case SQLITE_VTAB_DIRECTONLY3: {
157711 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High2;
157712 break;
157713 }
157714 case SQLITE_VTAB_USES_ALL_SCHEMAS4: {
157715 p->pVTable->bAllSchemas = 1;
157716 break;
157717 }
157718 default: {
157719 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(157719);
157720 break;
157721 }
157722 }
157723 va_end(ap)__builtin_va_end(ap);
157724 }
157725
157726 if( rc!=SQLITE_OK0 ) sqlite3Error(db, rc);
157727 sqlite3_mutex_leave(db->mutex);
157728 return rc;
157729}
157730
157731#endif /* SQLITE_OMIT_VIRTUALTABLE */
157732
157733/************** End of vtab.c ************************************************/
157734/************** Begin file wherecode.c ***************************************/
157735/*
157736** 2015-06-06
157737**
157738** The author disclaims copyright to this source code. In place of
157739** a legal notice, here is a blessing:
157740**
157741** May you do good and not evil.
157742** May you find forgiveness for yourself and forgive others.
157743** May you share freely, never taking more than you give.
157744**
157745*************************************************************************
157746** This module contains C code that generates VDBE code used to process
157747** the WHERE clause of SQL statements.
157748**
157749** This file was split off from where.c on 2015-06-06 in order to reduce the
157750** size of where.c and make it easier to edit. This file contains the routines
157751** that actually generate the bulk of the WHERE loop code. The original where.c
157752** file retains the code that does query planning and analysis.
157753*/
157754/* #include "sqliteInt.h" */
157755/************** Include whereInt.h in the middle of wherecode.c **************/
157756/************** Begin file whereInt.h ****************************************/
157757/*
157758** 2013-11-12
157759**
157760** The author disclaims copyright to this source code. In place of
157761** a legal notice, here is a blessing:
157762**
157763** May you do good and not evil.
157764** May you find forgiveness for yourself and forgive others.
157765** May you share freely, never taking more than you give.
157766**
157767*************************************************************************
157768**
157769** This file contains structure and macro definitions for the query
157770** planner logic in "where.c". These definitions are broken out into
157771** a separate source file for easier editing.
157772*/
157773#ifndef SQLITE_WHEREINT_H
157774#define SQLITE_WHEREINT_H
157775
157776
157777/* Forward references
157778*/
157779typedef struct WhereClause WhereClause;
157780typedef struct WhereMaskSet WhereMaskSet;
157781typedef struct WhereOrInfo WhereOrInfo;
157782typedef struct WhereAndInfo WhereAndInfo;
157783typedef struct WhereLevel WhereLevel;
157784typedef struct WhereLoop WhereLoop;
157785typedef struct WherePath WherePath;
157786typedef struct WhereTerm WhereTerm;
157787typedef struct WhereLoopBuilder WhereLoopBuilder;
157788typedef struct WhereScan WhereScan;
157789typedef struct WhereOrCost WhereOrCost;
157790typedef struct WhereOrSet WhereOrSet;
157791typedef struct WhereMemBlock WhereMemBlock;
157792typedef struct WhereRightJoin WhereRightJoin;
157793
157794/*
157795** This object is a header on a block of allocated memory that will be
157796** automatically freed when its WInfo object is destructed.
157797*/
157798struct WhereMemBlock {
157799 WhereMemBlock *pNext; /* Next block in the chain */
157800 u64 sz; /* Bytes of space */
157801};
157802
157803/*
157804** Extra information attached to a WhereLevel that is a RIGHT JOIN.
157805*/
157806struct WhereRightJoin {
157807 int iMatch; /* Cursor used to determine prior matched rows */
157808 int regBloom; /* Bloom filter for iRJMatch */
157809 int regReturn; /* Return register for the interior subroutine */
157810 int addrSubrtn; /* Starting address for the interior subroutine */
157811 int endSubrtn; /* The last opcode in the interior subroutine */
157812};
157813
157814/*
157815** This object contains information needed to implement a single nested
157816** loop in WHERE clause.
157817**
157818** Contrast this object with WhereLoop. This object describes the
157819** implementation of the loop. WhereLoop describes the algorithm.
157820** This object contains a pointer to the WhereLoop algorithm as one of
157821** its elements.
157822**
157823** The WhereInfo object contains a single instance of this object for
157824** each term in the FROM clause (which is to say, for each of the
157825** nested loops as implemented). The order of WhereLevel objects determines
157826** the loop nested order, with WhereInfo.a[0] being the outer loop and
157827** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
157828*/
157829struct WhereLevel {
157830 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
157831 int iTabCur; /* The VDBE cursor used to access the table */
157832 int iIdxCur; /* The VDBE cursor used to access pIdx */
157833 int addrBrk; /* Jump here to break out of the loop */
157834 int addrNxt; /* Jump here to start the next IN combination */
157835 int addrSkip; /* Jump here for next iteration of skip-scan */
157836 int addrCont; /* Jump here to continue with the next loop cycle */
157837 int addrFirst; /* First instruction of interior of the loop */
157838 int addrBody; /* Beginning of the body of this loop */
157839 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
157840 int addrBignull; /* Jump here for next part of big-null scan */
157841#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
157842 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
157843 int addrLikeRep; /* LIKE range processing address */
157844#endif
157845 int regFilter; /* Bloom filter */
157846 WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */
157847 u8 iFrom; /* Which entry in the FROM clause */
157848 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
157849 int p1, p2; /* Operands of the opcode used to end the loop */
157850 union { /* Information that depends on pWLoop->wsFlags */
157851 struct {
157852 int nIn; /* Number of entries in aInLoop[] */
157853 struct InLoop {
157854 int iCur; /* The VDBE cursor used by this IN operator */
157855 int addrInTop; /* Top of the IN loop */
157856 int iBase; /* Base register of multi-key index record */
157857 int nPrefix; /* Number of prior entries in the key */
157858 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
157859 } *aInLoop; /* Information about each nested IN operator */
157860 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
157861 Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */
157862 } u;
157863 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
157864 Bitmask notReady; /* FROM entries not usable at this level */
157865#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
157866 int addrVisit; /* Address at which row is visited */
157867#endif
157868};
157869
157870/*
157871** Each instance of this object represents an algorithm for evaluating one
157872** term of a join. Every term of the FROM clause will have at least
157873** one corresponding WhereLoop object (unless INDEXED BY constraints
157874** prevent a query solution - which is an error) and many terms of the
157875** FROM clause will have multiple WhereLoop objects, each describing a
157876** potential way of implementing that FROM-clause term, together with
157877** dependencies and cost estimates for using the chosen algorithm.
157878**
157879** Query planning consists of building up a collection of these WhereLoop
157880** objects, then computing a particular sequence of WhereLoop objects, with
157881** one WhereLoop object per FROM clause term, that satisfy all dependencies
157882** and that minimize the overall cost.
157883*/
157884struct WhereLoop {
157885 Bitmask prereq; /* Bitmask of other loops that must run first */
157886 Bitmask maskSelf; /* Bitmask identifying table iTab */
157887#ifdef SQLITE_DEBUG
157888 char cId; /* Symbolic ID of this loop for debugging use */
157889#endif
157890 u8 iTab; /* Position in FROM clause of table for this loop */
157891 u8 iSortIdx; /* Sorting index number. 0==None */
157892 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
157893 LogEst rRun; /* Cost of running each loop */
157894 LogEst nOut; /* Estimated number of output rows */
157895 union {
157896 struct { /* Information for internal btree tables */
157897 u16 nEq; /* Number of equality constraints */
157898 u16 nBtm; /* Size of BTM vector */
157899 u16 nTop; /* Size of TOP vector */
157900 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
157901 Index *pIndex; /* Index used, or NULL */
157902 ExprList *pOrderBy; /* ORDER BY clause if this is really a subquery */
157903 } btree;
157904 struct { /* Information for virtual tables */
157905 int idxNum; /* Index number */
157906 u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
157907 u32 bOmitOffset : 1; /* True to let virtual table handle offset */
157908 u32 bIdxNumHex : 1; /* Show idxNum as hex in EXPLAIN QUERY PLAN */
157909 i8 isOrdered; /* True if satisfies ORDER BY */
157910 u16 omitMask; /* Terms that may be omitted */
157911 char *idxStr; /* Index identifier string */
157912 u32 mHandleIn; /* Terms to handle as IN(...) instead of == */
157913 } vtab;
157914 } u;
157915 u32 wsFlags; /* WHERE_* flags describing the plan */
157916 u16 nLTerm; /* Number of entries in aLTerm[] */
157917 u16 nSkip; /* Number of NULL aLTerm[] entries */
157918 /**** whereLoopXfer() copies fields above ***********************/
157919# define WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot) offsetof(WhereLoop,nLSlot)__builtin_offsetof(WhereLoop, nLSlot)
157920 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
157921 LogEst rStarDelta; /* Cost delta due to star-schema heuristic. Not
157922 ** initialized unless pWInfo->nOutStarDelta>0 */
157923 WhereTerm **aLTerm; /* WhereTerms used */
157924 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
157925 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
157926};
157927
157928/* This object holds the prerequisites and the cost of running a
157929** subquery on one operand of an OR operator in the WHERE clause.
157930** See WhereOrSet for additional information
157931*/
157932struct WhereOrCost {
157933 Bitmask prereq; /* Prerequisites */
157934 LogEst rRun; /* Cost of running this subquery */
157935 LogEst nOut; /* Number of outputs for this subquery */
157936};
157937
157938/* The WhereOrSet object holds a set of possible WhereOrCosts that
157939** correspond to the subquery(s) of OR-clause processing. Only the
157940** best N_OR_COST elements are retained.
157941*/
157942#define N_OR_COST3 3
157943struct WhereOrSet {
157944 u16 n; /* Number of valid a[] entries */
157945 WhereOrCost a[N_OR_COST3]; /* Set of best costs */
157946};
157947
157948/*
157949** Each instance of this object holds a sequence of WhereLoop objects
157950** that implement some or all of a query plan.
157951**
157952** Think of each WhereLoop object as a node in a graph with arcs
157953** showing dependencies and costs for travelling between nodes. (That is
157954** not a completely accurate description because WhereLoop costs are a
157955** vector, not a scalar, and because dependencies are many-to-one, not
157956** one-to-one as are graph nodes. But it is a useful visualization aid.)
157957** Then a WherePath object is a path through the graph that visits some
157958** or all of the WhereLoop objects once.
157959**
157960** The "solver" works by creating the N best WherePath objects of length
157961** 1. Then using those as a basis to compute the N best WherePath objects
157962** of length 2. And so forth until the length of WherePaths equals the
157963** number of nodes in the FROM clause. The best (lowest cost) WherePath
157964** at the end is the chosen query plan.
157965*/
157966struct WherePath {
157967 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
157968 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
157969 LogEst nRow; /* Estimated number of rows generated by this path */
157970 LogEst rCost; /* Total cost of this path */
157971 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
157972 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
157973 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
157974};
157975
157976/*
157977** The query generator uses an array of instances of this structure to
157978** help it analyze the subexpressions of the WHERE clause. Each WHERE
157979** clause subexpression is separated from the others by AND operators,
157980** usually, or sometimes subexpressions separated by OR.
157981**
157982** All WhereTerms are collected into a single WhereClause structure.
157983** The following identity holds:
157984**
157985** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
157986**
157987** When a term is of the form:
157988**
157989** X <op> <expr>
157990**
157991** where X is a column name and <op> is one of certain operators,
157992** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
157993** cursor number and column number for X. WhereTerm.eOperator records
157994** the <op> using a bitmask encoding defined by WO_xxx below. The
157995** use of a bitmask encoding for the operator allows us to search
157996** quickly for terms that match any of several different operators.
157997**
157998** A WhereTerm might also be two or more subterms connected by OR:
157999**
158000** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
158001**
158002** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
158003** and the WhereTerm.u.pOrInfo field points to auxiliary information that
158004** is collected about the OR clause.
158005**
158006** If a term in the WHERE clause does not match either of the two previous
158007** categories, then eOperator==0. The WhereTerm.pExpr field is still set
158008** to the original subexpression content and wtFlags is set up appropriately
158009** but no other fields in the WhereTerm object are meaningful.
158010**
158011** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
158012** but they do so indirectly. A single WhereMaskSet structure translates
158013** cursor number into bits and the translated bit is stored in the prereq
158014** fields. The translation is used in order to maximize the number of
158015** bits that will fit in a Bitmask. The VDBE cursor numbers might be
158016** spread out over the non-negative integers. For example, the cursor
158017** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
158018** translates these sparse cursor numbers into consecutive integers
158019** beginning with 0 in order to make the best possible use of the available
158020** bits in the Bitmask. So, in the example above, the cursor numbers
158021** would be mapped into integers 0 through 7.
158022**
158023** The number of terms in a join is limited by the number of bits
158024** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
158025** is only able to process joins with 64 or fewer tables.
158026*/
158027struct WhereTerm {
158028 Expr *pExpr; /* Pointer to the subexpression that is this term */
158029 WhereClause *pWC; /* The clause this term is part of */
158030 LogEst truthProb; /* Probability of truth for this expression */
158031 u16 wtFlags; /* TERM_xxx bit flags. See below */
158032 u16 eOperator; /* A WO_xx value describing <op> */
158033 u8 nChild; /* Number of children that must disable us */
158034 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
158035 int iParent; /* Disable pWC->a[iParent] when this term disabled */
158036 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
158037 union {
158038 struct {
158039 int leftColumn; /* Column number of X in "X <op> <expr>" */
158040 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
158041 } x; /* Opcode other than OP_OR or OP_AND */
158042 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
158043 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
158044 } u;
158045 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
158046 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
158047};
158048
158049/*
158050** Allowed values of WhereTerm.wtFlags
158051*/
158052#define TERM_DYNAMIC0x0001 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
158053#define TERM_VIRTUAL0x0002 0x0002 /* Added by the optimizer. Do not code */
158054#define TERM_CODED0x0004 0x0004 /* This term is already coded */
158055#define TERM_COPIED0x0008 0x0008 /* Has a child */
158056#define TERM_ORINFO0x0010 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
158057#define TERM_ANDINFO0x0020 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
158058#define TERM_OK0x0040 0x0040 /* Used during OR-clause processing */
158059#define TERM_VNULL0x0080 0x0080 /* Manufactured x>NULL or x<=NULL term */
158060#define TERM_LIKEOPT0x0100 0x0100 /* Virtual terms from the LIKE optimization */
158061#define TERM_LIKECOND0x0200 0x0200 /* Conditionally this LIKE operator term */
158062#define TERM_LIKE0x0400 0x0400 /* The original LIKE operator */
158063#define TERM_IS0x0800 0x0800 /* Term.pExpr is an IS operator */
158064#define TERM_VARSELECT0x1000 0x1000 /* Term.pExpr contains a correlated sub-query */
158065#define TERM_HEURTRUTH0x2000 0x2000 /* Heuristic truthProb used */
158066#ifdef SQLITE_ENABLE_STAT4
158067# define TERM_HIGHTRUTH0 0x4000 /* Term excludes few rows */
158068#else
158069# define TERM_HIGHTRUTH0 0 /* Only used with STAT4 */
158070#endif
158071#define TERM_SLICE0x8000 0x8000 /* One slice of a row-value/vector comparison */
158072
158073/*
158074** An instance of the WhereScan object is used as an iterator for locating
158075** terms in the WHERE clause that are useful to the query planner.
158076*/
158077struct WhereScan {
158078 WhereClause *pOrigWC; /* Original, innermost WhereClause */
158079 WhereClause *pWC; /* WhereClause currently being scanned */
158080 const char *zCollName; /* Required collating sequence, if not NULL */
158081 Expr *pIdxExpr; /* Search for this index expression */
158082 int k; /* Resume scanning at this->pWC->a[this->k] */
158083 u32 opMask; /* Acceptable operators */
158084 char idxaff; /* Must match this affinity, if zCollName!=NULL */
158085 unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */
158086 unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */
158087 int aiCur[11]; /* Cursors in the equivalence class */
158088 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
158089};
158090
158091/*
158092** An instance of the following structure holds all information about a
158093** WHERE clause. Mostly this is a container for one or more WhereTerms.
158094**
158095** Explanation of pOuter: For a WHERE clause of the form
158096**
158097** a AND ((b AND c) OR (d AND e)) AND f
158098**
158099** There are separate WhereClause objects for the whole clause and for
158100** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
158101** subclauses points to the WhereClause object for the whole clause.
158102*/
158103struct WhereClause {
158104 WhereInfo *pWInfo; /* WHERE clause processing context */
158105 WhereClause *pOuter; /* Outer conjunction */
158106 u8 op; /* Split operator. TK_AND or TK_OR */
158107 u8 hasOr; /* True if any a[].eOperator is WO_OR */
158108 int nTerm; /* Number of terms */
158109 int nSlot; /* Number of entries in a[] */
158110 int nBase; /* Number of terms through the last non-Virtual */
158111 WhereTerm *a; /* Each a[] describes a term of the WHERE clause */
158112#if defined(SQLITE_SMALL_STACK)
158113 WhereTerm aStatic[1]; /* Initial static space for a[] */
158114#else
158115 WhereTerm aStatic[8]; /* Initial static space for a[] */
158116#endif
158117};
158118
158119/*
158120** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
158121** a dynamically allocated instance of the following structure.
158122*/
158123struct WhereOrInfo {
158124 WhereClause wc; /* Decomposition into subterms */
158125 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
158126};
158127
158128/*
158129** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
158130** a dynamically allocated instance of the following structure.
158131*/
158132struct WhereAndInfo {
158133 WhereClause wc; /* The subexpression broken out */
158134};
158135
158136/*
158137** An instance of the following structure keeps track of a mapping
158138** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
158139**
158140** The VDBE cursor numbers are small integers contained in
158141** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
158142** clause, the cursor numbers might not begin with 0 and they might
158143** contain gaps in the numbering sequence. But we want to make maximum
158144** use of the bits in our bitmasks. This structure provides a mapping
158145** from the sparse cursor numbers into consecutive integers beginning
158146** with 0.
158147**
158148** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
158149** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
158150**
158151** For example, if the WHERE clause expression used these VDBE
158152** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
158153** would map those cursor numbers into bits 0 through 5.
158154**
158155** Note that the mapping is not necessarily ordered. In the example
158156** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
158157** 57->5, 73->4. Or one of 719 other combinations might be used. It
158158** does not really matter. What is important is that sparse cursor
158159** numbers all get mapped into bit numbers that begin with 0 and contain
158160** no gaps.
158161*/
158162struct WhereMaskSet {
158163 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
158164 int n; /* Number of assigned cursor values */
158165 int ix[BMS((int)(sizeof(Bitmask)*8))]; /* Cursor assigned to each bit */
158166};
158167
158168/*
158169** This object is a convenience wrapper holding all information needed
158170** to construct WhereLoop objects for a particular query.
158171*/
158172struct WhereLoopBuilder {
158173 WhereInfo *pWInfo; /* Information about this WHERE */
158174 WhereClause *pWC; /* WHERE clause terms */
158175 WhereLoop *pNew; /* Template WhereLoop */
158176 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
158177#ifdef SQLITE_ENABLE_STAT4
158178 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
158179 int nRecValid; /* Number of valid fields currently in pRec */
158180#endif
158181 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
158182 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
158183 unsigned int iPlanLimit; /* Search limiter */
158184};
158185
158186/* Allowed values for WhereLoopBuider.bldFlags */
158187#define SQLITE_BLDF1_INDEXED0x0001 0x0001 /* An index is used */
158188#define SQLITE_BLDF1_UNIQUE0x0002 0x0002 /* All keys of a UNIQUE index used */
158189
158190#define SQLITE_BLDF2_2NDPASS0x0004 0x0004 /* Second builder pass needed */
158191
158192/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
158193** index+constraint combinations the query planner will consider for a
158194** particular query. If this parameter is unlimited, then certain
158195** pathological queries can spend excess time in the sqlite3WhereBegin()
158196** routine. The limit is high enough that is should not impact real-world
158197** queries.
158198**
158199** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
158200** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
158201** clause is processed, so that every table in a join is guaranteed to be
158202** able to propose a some index+constraint combinations even if the initial
158203** baseline limit was exhausted by prior tables of the join.
158204*/
158205#ifndef SQLITE_QUERY_PLANNER_LIMIT20000
158206# define SQLITE_QUERY_PLANNER_LIMIT20000 20000
158207#endif
158208#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR1000
158209# define SQLITE_QUERY_PLANNER_LIMIT_INCR1000 1000
158210#endif
158211
158212/*
158213** The WHERE clause processing routine has two halves. The
158214** first part does the start of the WHERE loop and the second
158215** half does the tail of the WHERE loop. An instance of
158216** this structure is returned by the first half and passed
158217** into the second half to give some continuity.
158218**
158219** An instance of this object holds the complete state of the query
158220** planner.
158221*/
158222struct WhereInfo {
158223 Parse *pParse; /* Parsing and code generating context */
158224 SrcList *pTabList; /* List of tables in the join */
158225 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
158226 ExprList *pResultSet; /* Result set of the query */
158227#if WHERETRACE_ENABLED
158228 Expr *pWhere; /* The complete WHERE clause */
158229#endif
158230 Select *pSelect; /* The entire SELECT statement containing WHERE */
158231 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
158232 int iContinue; /* Jump here to continue with next record */
158233 int iBreak; /* Jump here to break out of the loop */
158234 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
158235 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
158236 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
158237 u8 nLevel; /* Number of nested loop */
158238 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
158239 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
158240 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
158241 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
158242 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
158243 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
158244 unsigned sorted :1; /* True if really sorted (not just grouped) */
158245 LogEst nOutStarDelta; /* Artifical nOut reduction for star-query */
158246 LogEst nRowOut; /* Estimated number of output rows */
158247 int iTop; /* The very beginning of the WHERE loop */
158248 int iEndWhere; /* End of the WHERE clause itself */
158249 WhereLoop *pLoops; /* List of all WhereLoop objects */
158250 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
158251 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
158252 WhereClause sWC; /* Decomposition of the WHERE clause */
158253 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
158254 WhereLevel a[1]; /* Information about each nest loop in WHERE */
158255};
158256
158257/*
158258** Private interfaces - callable only by other where.c routines.
158259**
158260** where.c:
158261*/
158262SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
158263#ifdef WHERETRACE_ENABLED
158264SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC);
158265SQLITE_PRIVATEstatic void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
158266SQLITE_PRIVATEstatic void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC);
158267#endif
158268SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
158269 WhereClause *pWC, /* The WHERE clause to be searched */
158270 int iCur, /* Cursor number of LHS */
158271 int iColumn, /* Column number of LHS */
158272 Bitmask notReady, /* RHS must not overlap with this mask */
158273 u32 op, /* Mask of WO_xx values describing operator */
158274 Index *pIdx /* Must be compatible with this index, if not NULL */
158275);
158276SQLITE_PRIVATEstatic void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte);
158277SQLITE_PRIVATEstatic void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte);
158278
158279/* wherecode.c: */
158280#ifndef SQLITE_OMIT_EXPLAIN
158281SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
158282 Parse *pParse, /* Parse context */
158283 SrcList *pTabList, /* Table list this loop refers to */
158284 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
158285 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
158286);
158287SQLITE_PRIVATEstatic int sqlite3WhereExplainBloomFilter(
158288 const Parse *pParse, /* Parse context */
158289 const WhereInfo *pWInfo, /* WHERE clause */
158290 const WhereLevel *pLevel /* Bloom filter on this level */
158291);
158292#else
158293# define sqlite3WhereExplainOneScan(u,v,w,x) 0
158294# define sqlite3WhereExplainBloomFilter(u,v,w) 0
158295#endif /* SQLITE_OMIT_EXPLAIN */
158296#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
158297SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(
158298 Vdbe *v, /* Vdbe to add scanstatus entry to */
158299 SrcList *pSrclist, /* FROM clause pLvl reads data from */
158300 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
158301 int addrExplain /* Address of OP_Explain (or 0) */
158302);
158303#else
158304# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
158305#endif
158306SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
158307 Parse *pParse, /* Parsing context */
158308 Vdbe *v, /* Prepared statement under construction */
158309 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
158310 int iLevel, /* Which level of pWInfo->a[] should be coded */
158311 WhereLevel *pLevel, /* The current level pointer */
158312 Bitmask notReady /* Which tables are currently available */
158313);
158314SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) void sqlite3WhereRightJoinLoop(
158315 WhereInfo *pWInfo,
158316 int iLevel,
158317 WhereLevel *pLevel
158318);
158319
158320/* whereexpr.c: */
158321SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
158322SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause*);
158323SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause*,Expr*,u8);
158324SQLITE_PRIVATEstatic void sqlite3WhereAddLimit(WhereClause*, Select*);
158325SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
158326SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
158327SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
158328SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
158329SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
158330
158331
158332
158333
158334
158335/*
158336** Bitmasks for the operators on WhereTerm objects. These are all
158337** operators that are of interest to the query planner. An
158338** OR-ed combination of these values can be used when searching for
158339** particular WhereTerms within a WhereClause.
158340**
158341** Value constraints:
158342** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
158343** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
158344** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
158345** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
158346** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
158347*/
158348#define WO_IN0x0001 0x0001
158349#define WO_EQ0x0002 0x0002
158350#define WO_LT(0x0002<<(57 -54)) (WO_EQ0x0002<<(TK_LT57-TK_EQ54))
158351#define WO_LE(0x0002<<(56 -54)) (WO_EQ0x0002<<(TK_LE56-TK_EQ54))
158352#define WO_GT(0x0002<<(55 -54)) (WO_EQ0x0002<<(TK_GT55-TK_EQ54))
158353#define WO_GE(0x0002<<(58 -54)) (WO_EQ0x0002<<(TK_GE58-TK_EQ54))
158354#define WO_AUX0x0040 0x0040 /* Op useful to virtual tables only */
158355#define WO_IS0x0080 0x0080
158356#define WO_ISNULL0x0100 0x0100
158357#define WO_OR0x0200 0x0200 /* Two or more OR-connected terms */
158358#define WO_AND0x0400 0x0400 /* Two or more AND-connected terms */
158359#define WO_EQUIV0x0800 0x0800 /* Of the form A==B, both columns */
158360#define WO_NOOP0x1000 0x1000 /* This term does not restrict search space */
158361#define WO_ROWVAL0x2000 0x2000 /* A row-value term */
158362
158363#define WO_ALL0x3fff 0x3fff /* Mask of all possible WO_* values */
158364#define WO_SINGLE0x01ff 0x01ff /* Mask of all non-compound WO_* values */
158365
158366/*
158367** These are definitions of bits in the WhereLoop.wsFlags field.
158368** The particular combination of bits in each WhereLoop help to
158369** determine the algorithm that WhereLoop represents.
158370*/
158371#define WHERE_COLUMN_EQ0x00000001 0x00000001 /* x=EXPR */
158372#define WHERE_COLUMN_RANGE0x00000002 0x00000002 /* x<EXPR and/or x>EXPR */
158373#define WHERE_COLUMN_IN0x00000004 0x00000004 /* x IN (...) */
158374#define WHERE_COLUMN_NULL0x00000008 0x00000008 /* x IS NULL */
158375#define WHERE_CONSTRAINT0x0000000f 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
158376#define WHERE_TOP_LIMIT0x00000010 0x00000010 /* x<EXPR or x<=EXPR constraint */
158377#define WHERE_BTM_LIMIT0x00000020 0x00000020 /* x>EXPR or x>=EXPR constraint */
158378#define WHERE_BOTH_LIMIT0x00000030 0x00000030 /* Both x>EXPR and x<EXPR */
158379#define WHERE_IDX_ONLY0x00000040 0x00000040 /* Use index only - omit table */
158380#define WHERE_IPK0x00000100 0x00000100 /* x is the INTEGER PRIMARY KEY */
158381#define WHERE_INDEXED0x00000200 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
158382#define WHERE_VIRTUALTABLE0x00000400 0x00000400 /* WhereLoop.u.vtab is valid */
158383#define WHERE_IN_ABLE0x00000800 0x00000800 /* Able to support an IN operator */
158384#define WHERE_ONEROW0x00001000 0x00001000 /* Selects no more than one row */
158385#define WHERE_MULTI_OR0x00002000 0x00002000 /* OR using multiple indices */
158386#define WHERE_AUTO_INDEX0x00004000 0x00004000 /* Uses an ephemeral index */
158387#define WHERE_SKIPSCAN0x00008000 0x00008000 /* Uses the skip-scan algorithm */
158388#define WHERE_UNQ_WANTED0x00010000 0x00010000 /* WHERE_ONEROW would have been helpful*/
158389#define WHERE_PARTIALIDX0x00020000 0x00020000 /* The automatic index is partial */
158390#define WHERE_IN_EARLYOUT0x00040000 0x00040000 /* Perhaps quit IN loops early */
158391#define WHERE_BIGNULL_SORT0x00080000 0x00080000 /* Column nEq of index is BIGNULL */
158392#define WHERE_IN_SEEKSCAN0x00100000 0x00100000 /* Seek-scan optimization for IN */
158393#define WHERE_TRANSCONS0x00200000 0x00200000 /* Uses a transitive constraint */
158394#define WHERE_BLOOMFILTER0x00400000 0x00400000 /* Consider using a Bloom-filter */
158395#define WHERE_SELFCULL0x00800000 0x00800000 /* nOut reduced by extra WHERE terms */
158396#define WHERE_OMIT_OFFSET0x01000000 0x01000000 /* Set offset counter to zero */
158397#define WHERE_COROUTINE0x02000000 0x02000000 /* Implemented by co-routine.
158398 ** NB: False-negatives are possible */
158399#define WHERE_EXPRIDX0x04000000 0x04000000 /* Uses an index-on-expressions */
158400
158401#endif /* !defined(SQLITE_WHEREINT_H) */
158402
158403/************** End of whereInt.h ********************************************/
158404/************** Continuing where we left off in wherecode.c ******************/
158405
158406#ifndef SQLITE_OMIT_EXPLAIN
158407
158408/*
158409** Return the name of the i-th column of the pIdx index.
158410*/
158411static const char *explainIndexColumnName(Index *pIdx, int i){
158412 i = pIdx->aiColumn[i];
158413 if( i==XN_EXPR(-2) ) return "<expr>";
158414 if( i==XN_ROWID(-1) ) return "rowid";
158415 return pIdx->pTable->aCol[i].zCnName;
158416}
158417
158418/*
158419** This routine is a helper for explainIndexRange() below
158420**
158421** pStr holds the text of an expression that we are building up one term
158422** at a time. This routine adds a new term to the end of the expression.
158423** Terms are separated by AND so add the "AND" text for second and subsequent
158424** terms only.
158425*/
158426static void explainAppendTerm(
158427 StrAccum *pStr, /* The text expression being built */
158428 Index *pIdx, /* Index to read column names from */
158429 int nTerm, /* Number of terms */
158430 int iTerm, /* Zero-based index of first term. */
158431 int bAnd, /* Non-zero to append " AND " */
158432 const char *zOp /* Name of the operator */
158433){
158434 int i;
158435
158436 assert( nTerm>=1 )((void) (0));
158437 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
158438
158439 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
158440 for(i=0; i<nTerm; i++){
158441 if( i ) sqlite3_str_append(pStr, ",", 1);
158442 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
158443 }
158444 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
158445
158446 sqlite3_str_append(pStr, zOp, 1);
158447
158448 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
158449 for(i=0; i<nTerm; i++){
158450 if( i ) sqlite3_str_append(pStr, ",", 1);
158451 sqlite3_str_append(pStr, "?", 1);
158452 }
158453 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
158454}
158455
158456/*
158457** Argument pLevel describes a strategy for scanning table pTab. This
158458** function appends text to pStr that describes the subset of table
158459** rows scanned by the strategy in the form of an SQL expression.
158460**
158461** For example, if the query:
158462**
158463** SELECT * FROM t1 WHERE a=1 AND b>2;
158464**
158465** is run and there is an index on (a, b), then this function returns a
158466** string similar to:
158467**
158468** "a=? AND b>?"
158469*/
158470static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
158471 Index *pIndex = pLoop->u.btree.pIndex;
158472 u16 nEq = pLoop->u.btree.nEq;
158473 u16 nSkip = pLoop->nSkip;
158474 int i, j;
158475
158476 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))==0 ) return;
158477 sqlite3_str_append(pStr, " (", 2);
158478 for(i=0; i<nEq; i++){
158479 const char *z = explainIndexColumnName(pIndex, i);
158480 if( i ) sqlite3_str_append(pStr, " AND ", 5);
158481 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
158482 }
158483
158484 j = i;
158485 if( pLoop->wsFlags&WHERE_BTM_LIMIT0x00000020 ){
158486 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
158487 i = 1;
158488 }
158489 if( pLoop->wsFlags&WHERE_TOP_LIMIT0x00000010 ){
158490 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
158491 }
158492 sqlite3_str_append(pStr, ")", 1);
158493}
158494
158495/*
158496** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
158497** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG
158498** was defined at compile-time. If it is not a no-op, a single OP_Explain
158499** opcode is added to the output to describe the table scan strategy in pLevel.
158500**
158501** If an OP_Explain opcode is added to the VM, its address is returned.
158502** Otherwise, if no OP_Explain is coded, zero is returned.
158503*/
158504SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
158505 Parse *pParse, /* Parse context */
158506 SrcList *pTabList, /* Table list this loop refers to */
158507 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
158508 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
158509){
158510 int ret = 0;
158511#if !defined(SQLITE_DEBUG)
158512 if( sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))->explain==2 || IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) )
158513#endif
158514 {
158515 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
158516 Vdbe *v = pParse->pVdbe; /* VM being constructed */
158517 sqlite3 *db = pParse->db; /* Database handle */
158518 int isSearch; /* True for a SEARCH. False for SCAN. */
158519 WhereLoop *pLoop; /* The controlling WhereLoop object */
158520 u32 flags; /* Flags that describe this loop */
158521 char *zMsg; /* Text to add to EQP output */
158522 StrAccum str; /* EQP output string */
158523 char zBuf[100]; /* Initial space for EQP output string */
158524
158525 pLoop = pLevel->pWLoop;
158526 flags = pLoop->wsFlags;
158527 if( (flags&WHERE_MULTI_OR0x00002000) || (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020) ) return 0;
158528
158529 isSearch = (flags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))!=0
158530 || ((flags&WHERE_VIRTUALTABLE0x00000400)==0 && (pLoop->u.btree.nEq>0))
158531 || (wctrlFlags&(WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002));
158532
158533 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH2147483645);
158534 str.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
158535 sqlite3_str_appendf(&str, "%s %S", isSearch ? "SEARCH" : "SCAN", pItem);
158536 if( (flags & (WHERE_IPK0x00000100|WHERE_VIRTUALTABLE0x00000400))==0 ){
158537 const char *zFmt = 0;
158538 Index *pIdx;
158539
158540 assert( pLoop->u.btree.pIndex!=0 )((void) (0));
158541 pIdx = pLoop->u.btree.pIndex;
158542 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) )((void) (0));
158543 if( !HasRowid(pItem->pSTab)(((pItem->pSTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
158544 if( isSearch ){
158545 zFmt = "PRIMARY KEY";
158546 }
158547 }else if( flags & WHERE_PARTIALIDX0x00020000 ){
158548 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
158549 }else if( flags & WHERE_AUTO_INDEX0x00004000 ){
158550 zFmt = "AUTOMATIC COVERING INDEX";
158551 }else if( flags & WHERE_IDX_ONLY0x00000040 ){
158552 zFmt = "COVERING INDEX %s";
158553 }else{
158554 zFmt = "INDEX %s";
158555 }
158556 if( zFmt ){
158557 sqlite3_str_append(&str, " USING ", 7);
158558 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
158559 explainIndexRange(&str, pLoop);
158560 }
158561 }else if( (flags & WHERE_IPK0x00000100)!=0 && (flags & WHERE_CONSTRAINT0x0000000f)!=0 ){
158562 char cRangeOp;
158563#if 0 /* Better output, but breaks many tests */
158564 const Table *pTab = pItem->pTab;
158565 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
158566 "rowid";
158567#else
158568 const char *zRowid = "rowid";
158569#endif
158570 sqlite3_str_appendf(&str, " USING INTEGER PRIMARY KEY (%s", zRowid);
158571 if( flags&(WHERE_COLUMN_EQ0x00000001|WHERE_COLUMN_IN0x00000004) ){
158572 cRangeOp = '=';
158573 }else if( (flags&WHERE_BOTH_LIMIT0x00000030)==WHERE_BOTH_LIMIT0x00000030 ){
158574 sqlite3_str_appendf(&str, ">? AND %s", zRowid);
158575 cRangeOp = '<';
158576 }else if( flags&WHERE_BTM_LIMIT0x00000020 ){
158577 cRangeOp = '>';
158578 }else{
158579 assert( flags&WHERE_TOP_LIMIT)((void) (0));
158580 cRangeOp = '<';
158581 }
158582 sqlite3_str_appendf(&str, "%c?)", cRangeOp);
158583 }
158584#ifndef SQLITE_OMIT_VIRTUALTABLE
158585 else if( (flags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
158586 sqlite3_str_appendall(&str, " VIRTUAL TABLE INDEX ");
158587 sqlite3_str_appendf(&str,
158588 pLoop->u.vtab.bIdxNumHex ? "0x%x:%s" : "%d:%s",
158589 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
158590 }
158591#endif
158592 if( pItem->fg.jointype & JT_LEFT0x08 ){
158593 sqlite3_str_appendf(&str, " LEFT-JOIN");
158594 }
158595#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
158596 if( pLoop->nOut>=10 ){
158597 sqlite3_str_appendf(&str, " (~%llu rows)",
158598 sqlite3LogEstToInt(pLoop->nOut));
158599 }else{
158600 sqlite3_str_append(&str, " (~1 row)", 9);
158601 }
158602#endif
158603 zMsg = sqlite3StrAccumFinish(&str);
158604 sqlite3ExplainBreakpoint("",zMsg);
158605 ret = sqlite3VdbeAddOp4(v, OP_Explain188, sqlite3VdbeCurrentAddr(v),
158606 pParse->addrExplain, pLoop->rRun,
158607 zMsg, P4_DYNAMIC(-6));
158608 }
158609 return ret;
158610}
158611
158612/*
158613** Add a single OP_Explain opcode that describes a Bloom filter.
158614**
158615** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or
158616** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not
158617** required and this routine is a no-op.
158618**
158619** If an OP_Explain opcode is added to the VM, its address is returned.
158620** Otherwise, if no OP_Explain is coded, zero is returned.
158621*/
158622SQLITE_PRIVATEstatic int sqlite3WhereExplainBloomFilter(
158623 const Parse *pParse, /* Parse context */
158624 const WhereInfo *pWInfo, /* WHERE clause */
158625 const WhereLevel *pLevel /* Bloom filter on this level */
158626){
158627 int ret = 0;
158628 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
158629 Vdbe *v = pParse->pVdbe; /* VM being constructed */
158630 sqlite3 *db = pParse->db; /* Database handle */
158631 char *zMsg; /* Text to add to EQP output */
158632 int i; /* Loop counter */
158633 WhereLoop *pLoop; /* The where loop */
158634 StrAccum str; /* EQP output string */
158635 char zBuf[100]; /* Initial space for EQP output string */
158636
158637 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH2147483645);
158638 str.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
158639 sqlite3_str_appendf(&str, "BLOOM FILTER ON %S (", pItem);
158640 pLoop = pLevel->pWLoop;
158641 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
158642 const Table *pTab = pItem->pSTab;
158643 if( pTab->iPKey>=0 ){
158644 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
158645 }else{
158646 sqlite3_str_appendf(&str, "rowid=?");
158647 }
158648 }else{
158649 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
158650 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
158651 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
158652 sqlite3_str_appendf(&str, "%s=?", z);
158653 }
158654 }
158655 sqlite3_str_append(&str, ")", 1);
158656 zMsg = sqlite3StrAccumFinish(&str);
158657 ret = sqlite3VdbeAddOp4(v, OP_Explain188, sqlite3VdbeCurrentAddr(v),
158658 pParse->addrExplain, 0, zMsg,P4_DYNAMIC(-6));
158659
158660 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
158661 return ret;
158662}
158663#endif /* SQLITE_OMIT_EXPLAIN */
158664
158665#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
158666/*
158667** Configure the VM passed as the first argument with an
158668** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
158669** implement level pLvl. Argument pSrclist is a pointer to the FROM
158670** clause that the scan reads data from.
158671**
158672** If argument addrExplain is not 0, it must be the address of an
158673** OP_Explain instruction that describes the same loop.
158674*/
158675SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(
158676 Vdbe *v, /* Vdbe to add scanstatus entry to */
158677 SrcList *pSrclist, /* FROM clause pLvl reads data from */
158678 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
158679 int addrExplain /* Address of OP_Explain (or 0) */
158680){
158681 if( IS_STMT_SCANSTATUS( sqlite3VdbeDb(v) )(sqlite3VdbeDb(v)->flags & 0x00000400) ){
158682 const char *zObj = 0;
158683 WhereLoop *pLoop = pLvl->pWLoop;
158684 int wsFlags = pLoop->wsFlags;
158685 int viaCoroutine = 0;
158686
158687 if( (wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 && pLoop->u.btree.pIndex!=0 ){
158688 zObj = pLoop->u.btree.pIndex->zName;
158689 }else{
158690 zObj = pSrclist->a[pLvl->iFrom].zName;
158691 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
158692 }
158693 sqlite3VdbeScanStatus(
158694 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
158695 );
158696
158697 if( viaCoroutine==0 ){
158698 if( (wsFlags & (WHERE_MULTI_OR0x00002000|WHERE_AUTO_INDEX0x00004000))==0 ){
158699 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
158700 }
158701 if( wsFlags & WHERE_INDEXED0x00000200 ){
158702 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
158703 }
158704 }else{
158705 int addr;
158706 assert( pSrclist->a[pLvl->iFrom].fg.isSubquery )((void) (0));
158707 addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub;
158708 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1);
158709 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine )((void) (0));
158710 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr )((void) (0));
158711 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
158712 }
158713 }
158714}
158715#endif
158716
158717
158718/*
158719** Disable a term in the WHERE clause. Except, do not disable the term
158720** if it controls a LEFT OUTER JOIN and it did not originate in the ON
158721** or USING clause of that join.
158722**
158723** Consider the term t2.z='ok' in the following queries:
158724**
158725** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
158726** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
158727** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
158728**
158729** The t2.z='ok' is disabled in the in (2) because it originates
158730** in the ON clause. The term is disabled in (3) because it is not part
158731** of a LEFT OUTER JOIN. In (1), the term is not disabled.
158732**
158733** Disabling a term causes that term to not be tested in the inner loop
158734** of the join. Disabling is an optimization. When terms are satisfied
158735** by indices, we disable them to prevent redundant tests in the inner
158736** loop. We would get the correct results if nothing were ever disabled,
158737** but joins might run a little slower. The trick is to disable as much
158738** as we can without disabling too much. If we disabled in (1), we'd get
158739** the wrong answer. See ticket #813.
158740**
158741** If all the children of a term are disabled, then that term is also
158742** automatically disabled. In this way, terms get disabled if derived
158743** virtual terms are tested first. For example:
158744**
158745** x GLOB 'abc*' AND x>='abc' AND x<'acd'
158746** \___________/ \______/ \_____/
158747** parent child1 child2
158748**
158749** Only the parent term was in the original WHERE clause. The child1
158750** and child2 terms were added by the LIKE optimization. If both of
158751** the virtual child terms are valid, then testing of the parent can be
158752** skipped.
158753**
158754** Usually the parent term is marked as TERM_CODED. But if the parent
158755** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
158756** The TERM_LIKECOND marking indicates that the term should be coded inside
158757** a conditional such that is only evaluated on the second pass of a
158758** LIKE-optimization loop, when scanning BLOBs instead of strings.
158759*/
158760static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
158761 int nLoop = 0;
158762 assert( pTerm!=0 )((void) (0));
158763 while( (pTerm->wtFlags & TERM_CODED0x0004)==0
158764 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0))
158765 && (pLevel->notReady & pTerm->prereqAll)==0
158766 ){
158767 if( nLoop && (pTerm->wtFlags & TERM_LIKE0x0400)!=0 ){
158768 pTerm->wtFlags |= TERM_LIKECOND0x0200;
158769 }else{
158770 pTerm->wtFlags |= TERM_CODED0x0004;
158771 }
158772#ifdef WHERETRACE_ENABLED
158773 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
158774 sqlite3DebugPrintf("DISABLE-");
158775 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
158776 }
158777#endif
158778 if( pTerm->iParent<0 ) break;
158779 pTerm = &pTerm->pWC->a[pTerm->iParent];
158780 assert( pTerm!=0 )((void) (0));
158781 pTerm->nChild--;
158782 if( pTerm->nChild!=0 ) break;
158783 nLoop++;
158784 }
158785}
158786
158787/*
158788** Code an OP_Affinity opcode to apply the column affinity string zAff
158789** to the n registers starting at base.
158790**
158791** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
158792** are no-ops) at the beginning and end of zAff are ignored. If all entries
158793** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
158794**
158795** This routine makes its own copy of zAff so that the caller is free
158796** to modify zAff after this routine returns.
158797*/
158798static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
158799 Vdbe *v = pParse->pVdbe;
158800 if( zAff==0 ){
158801 assert( pParse->db->mallocFailed )((void) (0));
158802 return;
158803 }
158804 assert( v!=0 )((void) (0));
158805
158806 /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
158807 ** entries at the beginning and end of the affinity string.
158808 */
158809 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB )((void) (0));
158810 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB0x41 ){
158811 n--;
158812 base++;
158813 zAff++;
158814 }
158815 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB0x41 ){
158816 n--;
158817 }
158818
158819 /* Code the OP_Affinity opcode if there is anything left to do. */
158820 if( n>0 ){
158821 sqlite3VdbeAddOp4(v, OP_Affinity96, base, n, 0, zAff, n);
158822 }
158823}
158824
158825/*
158826** Expression pRight, which is the RHS of a comparison operation, is
158827** either a vector of n elements or, if n==1, a scalar expression.
158828** Before the comparison operation, affinity zAff is to be applied
158829** to the pRight values. This function modifies characters within the
158830** affinity string to SQLITE_AFF_BLOB if either:
158831**
158832** * the comparison will be performed with no affinity, or
158833** * the affinity change in zAff is guaranteed not to change the value.
158834*/
158835static void updateRangeAffinityStr(
158836 Expr *pRight, /* RHS of comparison */
158837 int n, /* Number of vector elements in comparison */
158838 char *zAff /* Affinity string to modify */
158839){
158840 int i;
158841 for(i=0; i<n; i++){
158842 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
158843 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB0x41
158844 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
158845 ){
158846 zAff[i] = SQLITE_AFF_BLOB0x41;
158847 }
158848 }
158849}
158850
158851/*
158852** The pOrderBy->a[].u.x.iOrderByCol values might be incorrect because
158853** columns might have been rearranged in the result set. This routine
158854** fixes them up.
158855**
158856** pEList is the new result set. The pEList->a[].u.x.iOrderByCol values
158857** contain the *old* locations of each expression. This is a temporary
158858** use of u.x.iOrderByCol, not its intended use. The caller must reset
158859** u.x.iOrderByCol back to zero for all entries in pEList before the
158860** caller returns.
158861**
158862** This routine changes pOrderBy->a[].u.x.iOrderByCol values from
158863** pEList->a[N].u.x.iOrderByCol into N+1. (The "+1" is because of the 1-based
158864** indexing used by iOrderByCol.) Or if no match, iOrderByCol is set to zero.
158865*/
158866static void adjustOrderByCol(ExprList *pOrderBy, ExprList *pEList){
158867 int i, j;
158868 if( pOrderBy==0 ) return;
158869 for(i=0; i<pOrderBy->nExpr; i++){
158870 int t = pOrderBy->a[i].u.x.iOrderByCol;
158871 if( t==0 ) continue;
158872 for(j=0; j<pEList->nExpr; j++){
158873 if( pEList->a[j].u.x.iOrderByCol==t ){
158874 pOrderBy->a[i].u.x.iOrderByCol = j+1;
158875 break;
158876 }
158877 }
158878 if( j>=pEList->nExpr ){
158879 pOrderBy->a[i].u.x.iOrderByCol = 0;
158880 }
158881 }
158882}
158883
158884
158885/*
158886** pX is an expression of the form: (vector) IN (SELECT ...)
158887** In other words, it is a vector IN operator with a SELECT clause on the
158888** LHS. But not all terms in the vector are indexable and the terms might
158889** not be in the correct order for indexing.
158890**
158891** This routine makes a copy of the input pX expression and then adjusts
158892** the vector on the LHS with corresponding changes to the SELECT so that
158893** the vector contains only index terms and those terms are in the correct
158894** order. The modified IN expression is returned. The caller is responsible
158895** for deleting the returned expression.
158896**
158897** Example:
158898**
158899** CREATE TABLE t1(a,b,c,d,e,f);
158900** CREATE INDEX t1x1 ON t1(e,c);
158901** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
158902** \_______________________________________/
158903** The pX expression
158904**
158905** Since only columns e and c can be used with the index, in that order,
158906** the modified IN expression that is returned will be:
158907**
158908** (e,c) IN (SELECT z,x FROM t2)
158909**
158910** The reduced pX is different from the original (obviously) and thus is
158911** only used for indexing, to improve performance. The original unaltered
158912** IN expression must also be run on each output row for correctness.
158913*/
158914static Expr *removeUnindexableInClauseTerms(
158915 Parse *pParse, /* The parsing context */
158916 int iEq, /* Look at loop terms starting here */
158917 WhereLoop *pLoop, /* The current loop */
158918 Expr *pX /* The IN expression to be reduced */
158919){
158920 sqlite3 *db = pParse->db;
158921 Select *pSelect; /* Pointer to the SELECT on the RHS */
158922 Expr *pNew;
158923 pNew = sqlite3ExprDup(db, pX, 0);
158924 if( db->mallocFailed==0 ){
158925 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
158926 ExprList *pOrigRhs; /* Original unmodified RHS */
158927 ExprList *pOrigLhs = 0; /* Original unmodified LHS */
158928 ExprList *pRhs = 0; /* New RHS after modifications */
158929 ExprList *pLhs = 0; /* New LHS after mods */
158930 int i; /* Loop counter */
158931
158932 assert( ExprUseXSelect(pNew) )((void) (0));
158933 pOrigRhs = pSelect->pEList;
158934 assert( pNew->pLeft!=0 )((void) (0));
158935 assert( ExprUseXList(pNew->pLeft) )((void) (0));
158936 if( pSelect==pNew->x.pSelect ){
158937 pOrigLhs = pNew->pLeft->x.pList;
158938 }
158939 for(i=iEq; i<pLoop->nLTerm; i++){
158940 if( pLoop->aLTerm[i]->pExpr==pX ){
158941 int iField;
158942 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
158943 iField = pLoop->aLTerm[i]->u.x.iField - 1;
158944 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
158945 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
158946 pOrigRhs->a[iField].pExpr = 0;
158947 if( pRhs ) pRhs->a[pRhs->nExpr-1].u.x.iOrderByCol = iField+1;
158948 if( pOrigLhs ){
158949 assert( pOrigLhs->a[iField].pExpr!=0 )((void) (0));
158950 pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr);
158951 pOrigLhs->a[iField].pExpr = 0;
158952 }
158953 }
158954 }
158955 sqlite3ExprListDelete(db, pOrigRhs);
158956 if( pOrigLhs ){
158957 sqlite3ExprListDelete(db, pOrigLhs);
158958 pNew->pLeft->x.pList = pLhs;
158959 }
158960 pSelect->pEList = pRhs;
158961 pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */
158962 if( pLhs && pLhs->nExpr==1 ){
158963 /* Take care here not to generate a TK_VECTOR containing only a
158964 ** single value. Since the parser never creates such a vector, some
158965 ** of the subroutines do not handle this case. */
158966 Expr *p = pLhs->a[0].pExpr;
158967 pLhs->a[0].pExpr = 0;
158968 sqlite3ExprDelete(db, pNew->pLeft);
158969 pNew->pLeft = p;
158970 }
158971
158972 /* If either the ORDER BY clause or the GROUP BY clause contains
158973 ** references to result-set columns, those references might now be
158974 ** obsolete. So fix them up.
158975 */
158976 assert( pRhs!=0 || db->mallocFailed )((void) (0));
158977 if( pRhs ){
158978 adjustOrderByCol(pSelect->pOrderBy, pRhs);
158979 adjustOrderByCol(pSelect->pGroupBy, pRhs);
158980 for(i=0; i<pRhs->nExpr; i++) pRhs->a[i].u.x.iOrderByCol = 0;
158981 }
158982
158983#if 0
158984 printf("For indexing, change the IN expr:\n");
158985 sqlite3TreeViewExpr(0, pX, 0);
158986 printf("Into:\n");
158987 sqlite3TreeViewExpr(0, pNew, 0);
158988#endif
158989 }
158990 }
158991 return pNew;
158992}
158993
158994
158995#ifndef SQLITE_OMIT_SUBQUERY
158996/*
158997** Generate code for a single X IN (....) term of the WHERE clause.
158998**
158999** This is a special-case of codeEqualityTerm() that works for IN operators
159000** only. It is broken out into a subroutine because this case is
159001** uncommon and by splitting it off into a subroutine, the common case
159002** runs faster.
159003**
159004** The current value for the constraint is left in register iTarget.
159005** This routine sets up a loop that will iterate over all values of X.
159006*/
159007static SQLITE_NOINLINE__attribute__((noinline)) void codeINTerm(
159008 Parse *pParse, /* The parsing context */
159009 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
159010 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
159011 int iEq, /* Index of the equality term within this level */
159012 int bRev, /* True for reverse-order IN operations */
159013 int iTarget /* Attempt to leave results in this register */
159014){
159015 Expr *pX = pTerm->pExpr;
159016 int eType = IN_INDEX_NOOP5;
159017 int iTab;
159018 struct InLoop *pIn;
159019 WhereLoop *pLoop = pLevel->pWLoop;
159020 Vdbe *v = pParse->pVdbe;
159021 int i;
159022 int nEq = 0;
159023 int *aiMap = 0;
159024
159025 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
159026 && pLoop->u.btree.pIndex!=0
159027 && pLoop->u.btree.pIndex->aSortOrder[iEq]
159028 ){
159029 testcase( iEq==0 );
159030 testcase( bRev );
159031 bRev = !bRev;
159032 }
159033 assert( pX->op==TK_IN )((void) (0));
159034
159035 for(i=0; i<iEq; i++){
159036 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
159037 disableTerm(pLevel, pTerm);
159038 return;
159039 }
159040 }
159041 for(i=iEq;i<pLoop->nLTerm; i++){
159042 assert( pLoop->aLTerm[i]!=0 )((void) (0));
159043 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
159044 }
159045
159046 iTab = 0;
159047 if( !ExprUseXSelect(pX)(((pX)->flags&0x001000)!=0) || pX->x.pSelect->pEList->nExpr==1 ){
159048 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, 0, &iTab);
159049 }else{
159050 Expr *pExpr = pTerm->pExpr;
159051 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
159052 sqlite3 *db = pParse->db;
159053 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
159054 if( !db->mallocFailed ){
159055 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
159056 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, aiMap,&iTab);
159057 pExpr->iTable = iTab;
159058 }
159059 sqlite3ExprDelete(db, pX);
159060 }else{
159061 int n = sqlite3ExprVectorSize(pX->pLeft);
159062 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n)((nEq)>(n)?(nEq):(n)));
159063 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, aiMap, &iTab);
159064 }
159065 pX = pExpr;
159066 }
159067
159068 if( eType==IN_INDEX_INDEX_DESC4 ){
159069 testcase( bRev );
159070 bRev = !bRev;
159071 }
159072 sqlite3VdbeAddOp2(v, bRev ? OP_Last32 : OP_Rewind36, iTab, 0);
159073 VdbeCoverageIf(v, bRev);
159074 VdbeCoverageIf(v, !bRev);
159075
159076 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
159077 pLoop->wsFlags |= WHERE_IN_ABLE0x00000800;
159078 if( pLevel->u.in.nIn==0 ){
159079 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
159080 }
159081 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)==0 ){
159082 pLoop->wsFlags |= WHERE_IN_EARLYOUT0x00040000;
159083 }
159084
159085 i = pLevel->u.in.nIn;
159086 pLevel->u.in.nIn += nEq;
159087 pLevel->u.in.aInLoop =
159088 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
159089 pLevel->u.in.aInLoop,
159090 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
159091 pIn = pLevel->u.in.aInLoop;
159092 if( pIn ){
159093 int iMap = 0; /* Index in aiMap[] */
159094 pIn += i;
159095 for(i=iEq;i<pLoop->nLTerm; i++){
159096 if( pLoop->aLTerm[i]->pExpr==pX ){
159097 int iOut = iTarget + i - iEq;
159098 if( eType==IN_INDEX_ROWID1 ){
159099 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid135, iTab, iOut);
159100 }else{
159101 int iCol = aiMap ? aiMap[iMap++] : 0;
159102 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column94,iTab, iCol, iOut);
159103 }
159104 sqlite3VdbeAddOp1(v, OP_IsNull51, iOut); VdbeCoverage(v);
159105 if( i==iEq ){
159106 pIn->iCur = iTab;
159107 pIn->eEndLoopOp = bRev ? OP_Prev38 : OP_Next39;
159108 if( iEq>0 ){
159109 pIn->iBase = iTarget - i;
159110 pIn->nPrefix = i;
159111 }else{
159112 pIn->nPrefix = 0;
159113 }
159114 }else{
159115 pIn->eEndLoopOp = OP_Noop187;
159116 }
159117 pIn++;
159118 }
159119 }
159120 testcase( iEq>0
159121 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
159122 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
159123 if( iEq>0
159124 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN0x00100000|WHERE_VIRTUALTABLE0x00000400))==0
159125 ){
159126 sqlite3VdbeAddOp3(v, OP_SeekHit125, pLevel->iIdxCur, 0, iEq);
159127 }
159128 }else{
159129 pLevel->u.in.nIn = 0;
159130 }
159131 sqlite3DbFree(pParse->db, aiMap);
159132}
159133#endif
159134
159135
159136/*
159137** Generate code for a single equality term of the WHERE clause. An equality
159138** term can be either X=expr or X IN (...). pTerm is the term to be
159139** coded.
159140**
159141** The current value for the constraint is left in a register, the index
159142** of which is returned. An attempt is made store the result in iTarget but
159143** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
159144** constraint is a TK_EQ or TK_IS, then the current value might be left in
159145** some other register and it is the caller's responsibility to compensate.
159146**
159147** For a constraint of the form X=expr, the expression is evaluated in
159148** straight-line code. For constraints of the form X IN (...)
159149** this routine sets up a loop that will iterate over all values of X.
159150*/
159151static int codeEqualityTerm(
159152 Parse *pParse, /* The parsing context */
159153 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
159154 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
159155 int iEq, /* Index of the equality term within this level */
159156 int bRev, /* True for reverse-order IN operations */
159157 int iTarget /* Attempt to leave results in this register */
159158){
159159 Expr *pX = pTerm->pExpr;
159160 int iReg; /* Register holding results */
159161
159162 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm )((void) (0));
159163 assert( iTarget>0 )((void) (0));
159164 if( pX->op==TK_EQ54 || pX->op==TK_IS45 ){
159165 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
159166 }else if( pX->op==TK_ISNULL51 ){
159167 iReg = iTarget;
159168 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Null75, 0, iReg);
159169#ifndef SQLITE_OMIT_SUBQUERY
159170 }else{
159171 assert( pX->op==TK_IN )((void) (0));
159172 iReg = iTarget;
159173 codeINTerm(pParse, pTerm, pLevel, iEq, bRev, iTarget);
159174#endif
159175 }
159176
159177 /* As an optimization, try to disable the WHERE clause term that is
159178 ** driving the index as it will always be true. The correct answer is
159179 ** obtained regardless, but we might get the answer with fewer CPU cycles
159180 ** by omitting the term.
159181 **
159182 ** But do not disable the term unless we are certain that the term is
159183 ** not a transitive constraint. For an example of where that does not
159184 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
159185 */
159186 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS0x00200000)==0
159187 || (pTerm->eOperator & WO_EQUIV0x0800)==0
159188 ){
159189 disableTerm(pLevel, pTerm);
159190 }
159191
159192 return iReg;
159193}
159194
159195/*
159196** Generate code that will evaluate all == and IN constraints for an
159197** index scan.
159198**
159199** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
159200** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
159201** The index has as many as three equality constraints, but in this
159202** example, the third "c" value is an inequality. So only two
159203** constraints are coded. This routine will generate code to evaluate
159204** a==5 and b IN (1,2,3). The current values for a and b will be stored
159205** in consecutive registers and the index of the first register is returned.
159206**
159207** In the example above nEq==2. But this subroutine works for any value
159208** of nEq including 0. If nEq==0, this routine is nearly a no-op.
159209** The only thing it does is allocate the pLevel->iMem memory cell and
159210** compute the affinity string.
159211**
159212** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
159213** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
159214** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
159215** occurs after the nEq quality constraints.
159216**
159217** This routine allocates a range of nEq+nExtraReg memory cells and returns
159218** the index of the first memory cell in that range. The code that
159219** calls this routine will use that memory range to store keys for
159220** start and termination conditions of the loop.
159221** key value of the loop. If one or more IN operators appear, then
159222** this routine allocates an additional nEq memory cells for internal
159223** use.
159224**
159225** Before returning, *pzAff is set to point to a buffer containing a
159226** copy of the column affinity string of the index allocated using
159227** sqlite3DbMalloc(). Except, entries in the copy of the string associated
159228** with equality constraints that use BLOB or NONE affinity are set to
159229** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
159230**
159231** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
159232** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
159233**
159234** In the example above, the index on t1(a) has TEXT affinity. But since
159235** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
159236** no conversion should be attempted before using a t2.b value as part of
159237** a key to search the index. Hence the first byte in the returned affinity
159238** string in this example would be set to SQLITE_AFF_BLOB.
159239*/
159240static int codeAllEqualityTerms(
159241 Parse *pParse, /* Parsing context */
159242 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
159243 int bRev, /* Reverse the order of IN operators */
159244 int nExtraReg, /* Number of extra registers to allocate */
159245 char **pzAff /* OUT: Set to point to affinity string */
159246){
159247 u16 nEq; /* The number of == or IN constraints to code */
159248 u16 nSkip; /* Number of left-most columns to skip */
159249 Vdbe *v = pParse->pVdbe; /* The vm under construction */
159250 Index *pIdx; /* The index being used for this loop */
159251 WhereTerm *pTerm; /* A single constraint term */
159252 WhereLoop *pLoop; /* The WhereLoop object */
159253 int j; /* Loop counter */
159254 int regBase; /* Base register */
159255 int nReg; /* Number of registers to allocate */
159256 char *zAff; /* Affinity string to return */
159257
159258 /* This module is only called on query plans that use an index. */
159259 pLoop = pLevel->pWLoop;
159260 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
159261 nEq = pLoop->u.btree.nEq;
159262 nSkip = pLoop->nSkip;
159263 pIdx = pLoop->u.btree.pIndex;
159264 assert( pIdx!=0 )((void) (0));
159265
159266 /* Figure out how many memory cells we will need then allocate them.
159267 */
159268 regBase = pParse->nMem + 1;
159269 nReg = nEq + nExtraReg;
159270 pParse->nMem += nReg;
159271
159272 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
159273 assert( zAff!=0 || pParse->db->mallocFailed )((void) (0));
159274
159275 if( nSkip ){
159276 int iIdxCur = pLevel->iIdxCur;
159277 sqlite3VdbeAddOp3(v, OP_Null75, 0, regBase, regBase+nSkip-1);
159278 sqlite3VdbeAddOp1(v, (bRev?OP_Last32:OP_Rewind36), iIdxCur);
159279 VdbeCoverageIf(v, bRev==0);
159280 VdbeCoverageIf(v, bRev!=0);
159281 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
159282 j = sqlite3VdbeAddOp0(v, OP_Goto9);
159283 assert( pLevel->addrSkip==0 )((void) (0));
159284 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT21:OP_SeekGT24),
159285 iIdxCur, 0, regBase, nSkip);
159286 VdbeCoverageIf(v, bRev==0);
159287 VdbeCoverageIf(v, bRev!=0);
159288 sqlite3VdbeJumpHere(v, j);
159289 for(j=0; j<nSkip; j++){
159290 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, j, regBase+j);
159291 testcase( pIdx->aiColumn[j]==XN_EXPR );
159292 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
159293 }
159294 }
159295
159296 /* Evaluate the equality constraints
159297 */
159298 assert( zAff==0 || (int)strlen(zAff)>=nEq )((void) (0));
159299 for(j=nSkip; j<nEq; j++){
159300 int r1;
159301 pTerm = pLoop->aLTerm[j];
159302 assert( pTerm!=0 )((void) (0));
159303 /* The following testcase is true for indices with redundant columns.
159304 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
159305 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
159306 testcase( pTerm->wtFlags & TERM_VIRTUAL );
159307 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
159308 if( r1!=regBase+j ){
159309 if( nReg==1 ){
159310 sqlite3ReleaseTempReg(pParse, regBase);
159311 regBase = r1;
159312 }else{
159313 sqlite3VdbeAddOp2(v, OP_Copy80, r1, regBase+j);
159314 }
159315 }
159316 if( pTerm->eOperator & WO_IN0x0001 ){
159317 if( pTerm->pExpr->flags & EP_xIsSelect0x001000 ){
159318 /* No affinity ever needs to be (or should be) applied to a value
159319 ** from the RHS of an "? IN (SELECT ...)" expression. The
159320 ** sqlite3FindInIndex() routine has already ensured that the
159321 ** affinity of the comparison has been applied to the value. */
159322 if( zAff ) zAff[j] = SQLITE_AFF_BLOB0x41;
159323 }
159324 }else if( (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
159325 Expr *pRight = pTerm->pExpr->pRight;
159326 if( (pTerm->wtFlags & TERM_IS0x0800)==0 && sqlite3ExprCanBeNull(pRight) ){
159327 sqlite3VdbeAddOp2(v, OP_IsNull51, regBase+j, pLevel->addrBrk);
159328 VdbeCoverage(v);
159329 }
159330 if( pParse->nErr==0 ){
159331 assert( pParse->db->mallocFailed==0 )((void) (0));
159332 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB0x41 ){
159333 zAff[j] = SQLITE_AFF_BLOB0x41;
159334 }
159335 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
159336 zAff[j] = SQLITE_AFF_BLOB0x41;
159337 }
159338 }
159339 }
159340 }
159341 *pzAff = zAff;
159342 return regBase;
159343}
159344
159345#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
159346/*
159347** If the most recently coded instruction is a constant range constraint
159348** (a string literal) that originated from the LIKE optimization, then
159349** set P3 and P5 on the OP_String opcode so that the string will be cast
159350** to a BLOB at appropriate times.
159351**
159352** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
159353** expression: "x>='ABC' AND x<'abd'". But this requires that the range
159354** scan loop run twice, once for strings and a second time for BLOBs.
159355** The OP_String opcodes on the second pass convert the upper and lower
159356** bound string constants to blobs. This routine makes the necessary changes
159357** to the OP_String opcodes for that to happen.
159358**
159359** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
159360** only the one pass through the string space is required, so this routine
159361** becomes a no-op.
159362*/
159363static void whereLikeOptimizationStringFixup(
159364 Vdbe *v, /* prepared statement under construction */
159365 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
159366 WhereTerm *pTerm /* The upper or lower bound just coded */
159367){
159368 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 ){
159369 VdbeOp *pOp;
159370 assert( pLevel->iLikeRepCntr>0 )((void) (0));
159371 pOp = sqlite3VdbeGetLastOp(v);
159372 assert( pOp!=0 )((void) (0));
159373 assert( pOp->opcode==OP_String8((void) (0))
159374 || pTerm->pWC->pWInfo->pParse->db->mallocFailed )((void) (0));
159375 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
159376 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
159377 }
159378}
159379#else
159380# define whereLikeOptimizationStringFixup(A,B,C)
159381#endif
159382
159383#ifdef SQLITE_ENABLE_CURSOR_HINTS
159384/*
159385** Information is passed from codeCursorHint() down to individual nodes of
159386** the expression tree (by sqlite3WalkExpr()) using an instance of this
159387** structure.
159388*/
159389struct CCurHint {
159390 int iTabCur; /* Cursor for the main table */
159391 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
159392 Index *pIdx; /* The index used to access the table */
159393};
159394
159395/*
159396** This function is called for every node of an expression that is a candidate
159397** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
159398** the table CCurHint.iTabCur, verify that the same column can be
159399** accessed through the index. If it cannot, then set pWalker->eCode to 1.
159400*/
159401static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
159402 struct CCurHint *pHint = pWalker->u.pCCurHint;
159403 assert( pHint->pIdx!=0 )((void) (0));
159404 if( pExpr->op==TK_COLUMN168
159405 && pExpr->iTable==pHint->iTabCur
159406 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
159407 ){
159408 pWalker->eCode = 1;
159409 }
159410 return WRC_Continue0;
159411}
159412
159413/*
159414** Test whether or not expression pExpr, which was part of a WHERE clause,
159415** should be included in the cursor-hint for a table that is on the rhs
159416** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
159417** expression is not suitable.
159418**
159419** An expression is unsuitable if it might evaluate to non NULL even if
159420** a TK_COLUMN node that does affect the value of the expression is set
159421** to NULL. For example:
159422**
159423** col IS NULL
159424** col IS NOT NULL
159425** coalesce(col, 1)
159426** CASE WHEN col THEN 0 ELSE 1 END
159427*/
159428static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
159429 if( pExpr->op==TK_IS45
159430 || pExpr->op==TK_ISNULL51 || pExpr->op==TK_ISNOT46
159431 || pExpr->op==TK_NOTNULL52 || pExpr->op==TK_CASE158
159432 ){
159433 pWalker->eCode = 1;
159434 }else if( pExpr->op==TK_FUNCTION172 ){
159435 int d1;
159436 char d2[4];
159437 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
159438 pWalker->eCode = 1;
159439 }
159440 }
159441
159442 return WRC_Continue0;
159443}
159444
159445
159446/*
159447** This function is called on every node of an expression tree used as an
159448** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
159449** that accesses any table other than the one identified by
159450** CCurHint.iTabCur, then do the following:
159451**
159452** 1) allocate a register and code an OP_Column instruction to read
159453** the specified column into the new register, and
159454**
159455** 2) transform the expression node to a TK_REGISTER node that reads
159456** from the newly populated register.
159457**
159458** Also, if the node is a TK_COLUMN that does access the table identified
159459** by pCCurHint.iTabCur, and an index is being used (which we will
159460** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
159461** an access of the index rather than the original table.
159462*/
159463static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
159464 int rc = WRC_Continue0;
159465 int reg;
159466 struct CCurHint *pHint = pWalker->u.pCCurHint;
159467 if( pExpr->op==TK_COLUMN168 ){
159468 if( pExpr->iTable!=pHint->iTabCur ){
159469 reg = ++pWalker->pParse->nMem; /* Register for column value */
159470 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
159471 pExpr->op = TK_REGISTER176;
159472 pExpr->iTable = reg;
159473 }else if( pHint->pIdx!=0 ){
159474 pExpr->iTable = pHint->iIdxCur;
159475 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
159476 assert( pExpr->iColumn>=0 )((void) (0));
159477 }
159478 }else if( pExpr->pAggInfo ){
159479 rc = WRC_Prune1;
159480 reg = ++pWalker->pParse->nMem; /* Register for column value */
159481 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
159482 pExpr->op = TK_REGISTER176;
159483 pExpr->iTable = reg;
159484 }else if( pExpr->op==TK_TRUEFALSE171 ){
159485 /* Do not walk disabled expressions. tag-20230504-1 */
159486 return WRC_Prune1;
159487 }
159488 return rc;
159489}
159490
159491/*
159492** Insert an OP_CursorHint instruction if it is appropriate to do so.
159493*/
159494static void codeCursorHint(
159495 SrcItem *pTabItem, /* FROM clause item */
159496 WhereInfo *pWInfo, /* The where clause */
159497 WhereLevel *pLevel, /* Which loop to provide hints for */
159498 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
159499){
159500 Parse *pParse = pWInfo->pParse;
159501 sqlite3 *db = pParse->db;
159502 Vdbe *v = pParse->pVdbe;
159503 Expr *pExpr = 0;
159504 WhereLoop *pLoop = pLevel->pWLoop;
159505 int iCur;
159506 WhereClause *pWC;
159507 WhereTerm *pTerm;
159508 int i, j;
159509 struct CCurHint sHint;
159510 Walker sWalker;
159511
159512 if( OptimizationDisabled(db, SQLITE_CursorHints)(((db)->dbOptFlags&(0x00000400))!=0) ) return;
159513 iCur = pLevel->iTabCur;
159514 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor )((void) (0));
159515 sHint.iTabCur = iCur;
159516 sHint.iIdxCur = pLevel->iIdxCur;
159517 sHint.pIdx = pLoop->u.btree.pIndex;
159518 memset(&sWalker, 0, sizeof(sWalker));
159519 sWalker.pParse = pParse;
159520 sWalker.u.pCCurHint = &sHint;
159521 pWC = &pWInfo->sWC;
159522 for(i=0; i<pWC->nBase; i++){
159523 pTerm = &pWC->a[i];
159524 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
159525 if( pTerm->prereqAll & pLevel->notReady ) continue;
159526
159527 /* Any terms specified as part of the ON(...) clause for any LEFT
159528 ** JOIN for which the current table is not the rhs are omitted
159529 ** from the cursor-hint.
159530 **
159531 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
159532 ** that were specified as part of the WHERE clause must be excluded.
159533 ** This is to address the following:
159534 **
159535 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
159536 **
159537 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
159538 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
159539 ** pushed down to the cursor, this row is filtered out, causing
159540 ** SQLite to synthesize a row of NULL values. Which does match the
159541 ** WHERE clause, and so the query returns a row. Which is incorrect.
159542 **
159543 ** For the same reason, WHERE terms such as:
159544 **
159545 ** WHERE 1 = (t2.c IS NULL)
159546 **
159547 ** are also excluded. See codeCursorHintIsOrFunction() for details.
159548 */
159549 if( pTabItem->fg.jointype & JT_LEFT0x08 ){
159550 Expr *pExpr = pTerm->pExpr;
159551 if( !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0)
159552 || pExpr->w.iJoin!=pTabItem->iCursor
159553 ){
159554 sWalker.eCode = 0;
159555 sWalker.xExprCallback = codeCursorHintIsOrFunction;
159556 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
159557 if( sWalker.eCode ) continue;
159558 }
159559 }else{
159560 if( ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0) ) continue;
159561 }
159562
159563 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
159564 ** the cursor. These terms are not needed as hints for a pure range
159565 ** scan (that has no == terms) so omit them. */
159566 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
159567 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
159568 if( j<pLoop->nLTerm ) continue;
159569 }
159570
159571 /* No subqueries or non-deterministic functions allowed */
159572 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
159573
159574 /* For an index scan, make sure referenced columns are actually in
159575 ** the index. */
159576 if( sHint.pIdx!=0 ){
159577 sWalker.eCode = 0;
159578 sWalker.xExprCallback = codeCursorHintCheckExpr;
159579 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
159580 if( sWalker.eCode ) continue;
159581 }
159582
159583 /* If we survive all prior tests, that means this term is worth hinting */
159584 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
159585 }
159586 if( pExpr!=0 ){
159587 sWalker.xExprCallback = codeCursorHintFixExpr;
159588 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
159589 sqlite3VdbeAddOp4(v, OP_CursorHint185,
159590 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
159591 (const char*)pExpr, P4_EXPR(-9));
159592 }
159593}
159594#else
159595# define codeCursorHint(A,B,C,D) /* No-op */
159596#endif /* SQLITE_ENABLE_CURSOR_HINTS */
159597
159598/*
159599** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
159600** a rowid value just read from cursor iIdxCur, open on index pIdx. This
159601** function generates code to do a deferred seek of cursor iCur to the
159602** rowid stored in register iRowid.
159603**
159604** Normally, this is just:
159605**
159606** OP_DeferredSeek $iCur $iRowid
159607**
159608** Which causes a seek on $iCur to the row with rowid $iRowid.
159609**
159610** However, if the scan currently being coded is a branch of an OR-loop and
159611** the statement currently being coded is a SELECT, then additional information
159612** is added that might allow OP_Column to omit the seek and instead do its
159613** lookup on the index, thus avoiding an expensive seek operation. To
159614** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur
159615** and P4 is set to an array of integers containing one entry for each column
159616** in the table. For each table column, if the column is the i'th
159617** column of the index, then the corresponding array entry is set to (i+1).
159618** If the column does not appear in the index at all, the array entry is set
159619** to 0. The OP_Column opcode can check this array to see if the column it
159620** wants is in the index and if it is, it will substitute the index cursor
159621** and column number and continue with those new values, rather than seeking
159622** the table cursor.
159623*/
159624static void codeDeferredSeek(
159625 WhereInfo *pWInfo, /* Where clause context */
159626 Index *pIdx, /* Index scan is using */
159627 int iCur, /* Cursor for IPK b-tree */
159628 int iIdxCur /* Index cursor */
159629){
159630 Parse *pParse = pWInfo->pParse; /* Parse context */
159631 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
159632
159633 assert( iIdxCur>0 )((void) (0));
159634 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 )((void) (0));
159635
159636 pWInfo->bDeferredSeek = 1;
159637 sqlite3VdbeAddOp3(v, OP_DeferredSeek141, iIdxCur, 0, iCur);
159638 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))
159639 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)((((pParse)->pToplevel ? (pParse)->pToplevel : (pParse)
)->writeMask)==0)
159640 ){
159641 int i;
159642 Table *pTab = pIdx->pTable;
159643 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
159644 if( ai ){
159645 ai[0] = pTab->nCol;
159646 for(i=0; i<pIdx->nColumn-1; i++){
159647 int x1, x2;
159648 assert( pIdx->aiColumn[i]<pTab->nCol )((void) (0));
159649 x1 = pIdx->aiColumn[i];
159650 x2 = sqlite3TableColumnToStorage(pTab, x1);
159651 testcase( x1!=x2 );
159652 if( x1>=0 ) ai[x2+1] = i+1;
159653 }
159654 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY(-14));
159655 }
159656 }
159657}
159658
159659/*
159660** If the expression passed as the second argument is a vector, generate
159661** code to write the first nReg elements of the vector into an array
159662** of registers starting with iReg.
159663**
159664** If the expression is not a vector, then nReg must be passed 1. In
159665** this case, generate code to evaluate the expression and leave the
159666** result in register iReg.
159667*/
159668static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
159669 assert( nReg>0 )((void) (0));
159670 if( p && sqlite3ExprIsVector(p) ){
159671#ifndef SQLITE_OMIT_SUBQUERY
159672 if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
159673 Vdbe *v = pParse->pVdbe;
159674 int iSelect;
159675 assert( p->op==TK_SELECT )((void) (0));
159676 iSelect = sqlite3CodeSubselect(pParse, p);
159677 sqlite3VdbeAddOp3(v, OP_Copy80, iSelect, iReg, nReg-1);
159678 }else
159679#endif
159680 {
159681 int i;
159682 const ExprList *pList;
159683 assert( ExprUseXList(p) )((void) (0));
159684 pList = p->x.pList;
159685 assert( nReg<=pList->nExpr )((void) (0));
159686 for(i=0; i<nReg; i++){
159687 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
159688 }
159689 }
159690 }else{
159691 assert( nReg==1 || pParse->nErr )((void) (0));
159692 sqlite3ExprCode(pParse, p, iReg);
159693 }
159694}
159695
159696/*
159697** The pTruth expression is always true because it is the WHERE clause
159698** a partial index that is driving a query loop. Look through all of the
159699** WHERE clause terms on the query, and if any of those terms must be
159700** true because pTruth is true, then mark those WHERE clause terms as
159701** coded.
159702*/
159703static void whereApplyPartialIndexConstraints(
159704 Expr *pTruth,
159705 int iTabCur,
159706 WhereClause *pWC
159707){
159708 int i;
159709 WhereTerm *pTerm;
159710 while( pTruth->op==TK_AND44 ){
159711 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
159712 pTruth = pTruth->pRight;
159713 }
159714 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
159715 Expr *pExpr;
159716 if( pTerm->wtFlags & TERM_CODED0x0004 ) continue;
159717 pExpr = pTerm->pExpr;
159718 if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
159719 pTerm->wtFlags |= TERM_CODED0x0004;
159720 }
159721 }
159722}
159723
159724/*
159725** This routine is called right after An OP_Filter has been generated and
159726** before the corresponding index search has been performed. This routine
159727** checks to see if there are additional Bloom filters in inner loops that
159728** can be checked prior to doing the index lookup. If there are available
159729** inner-loop Bloom filters, then evaluate those filters now, before the
159730** index lookup. The idea is that a Bloom filter check is way faster than
159731** an index lookup, and the Bloom filter might return false, meaning that
159732** the index lookup can be skipped.
159733**
159734** We know that an inner loop uses a Bloom filter because it has the
159735** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
159736** then clear the WhereLevel.regFilter value to prevent the Bloom filter
159737** from being checked a second time when the inner loop is evaluated.
159738*/
159739static SQLITE_NOINLINE__attribute__((noinline)) void filterPullDown(
159740 Parse *pParse, /* Parsing context */
159741 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
159742 int iLevel, /* Which level of pWInfo->a[] should be coded */
159743 int addrNxt, /* Jump here to bypass inner loops */
159744 Bitmask notReady /* Loops that are not ready */
159745){
159746 while( ++iLevel < pWInfo->nLevel ){
159747 WhereLevel *pLevel = &pWInfo->a[iLevel];
159748 WhereLoop *pLoop = pLevel->pWLoop;
159749 if( pLevel->regFilter==0 ) continue;
159750 if( pLevel->pWLoop->nSkip ) continue;
159751 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
159752 ** vvvvv--' pLevel->regFilter if this were true. */
159753 if( NEVER(pLoop->prereq & notReady)(pLoop->prereq & notReady) ) continue;
159754 assert( pLevel->addrBrk==0 )((void) (0));
159755 pLevel->addrBrk = addrNxt;
159756 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
159757 WhereTerm *pTerm = pLoop->aLTerm[0];
159758 int regRowid;
159759 assert( pTerm!=0 )((void) (0));
159760 assert( pTerm->pExpr!=0 )((void) (0));
159761 testcase( pTerm->wtFlags & TERM_VIRTUAL );
159762 regRowid = sqlite3GetTempReg(pParse);
159763 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
159764 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt13, regRowid, addrNxt);
159765 VdbeCoverage(pParse->pVdbe);
159766 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter64, pLevel->regFilter,
159767 addrNxt, regRowid, 1);
159768 VdbeCoverage(pParse->pVdbe);
159769 }else{
159770 u16 nEq = pLoop->u.btree.nEq;
159771 int r1;
159772 char *zStartAff;
159773
159774 assert( pLoop->wsFlags & WHERE_INDEXED )((void) (0));
159775 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 )((void) (0));
159776 r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
159777 codeApplyAffinity(pParse, r1, nEq, zStartAff);
159778 sqlite3DbFree(pParse->db, zStartAff);
159779 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter64, pLevel->regFilter,
159780 addrNxt, r1, nEq);
159781 VdbeCoverage(pParse->pVdbe);
159782 }
159783 pLevel->regFilter = 0;
159784 pLevel->addrBrk = 0;
159785 }
159786}
159787
159788/*
159789** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...)
159790** operator. Return true if level pLoop is guaranteed to visit only one
159791** row for each key generated for the index.
159792*/
159793static int whereLoopIsOneRow(WhereLoop *pLoop){
159794 if( pLoop->u.btree.pIndex->onError
159795 && pLoop->nSkip==0
159796 && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
159797 ){
159798 int ii;
159799 for(ii=0; ii<pLoop->u.btree.nEq; ii++){
159800 if( pLoop->aLTerm[ii]->eOperator & (WO_IS0x0080|WO_ISNULL0x0100) ){
159801 return 0;
159802 }
159803 }
159804 return 1;
159805 }
159806 return 0;
159807}
159808
159809/*
159810** Generate code for the start of the iLevel-th loop in the WHERE clause
159811** implementation described by pWInfo.
159812*/
159813SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
159814 Parse *pParse, /* Parsing context */
159815 Vdbe *v, /* Prepared statement under construction */
159816 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
159817 int iLevel, /* Which level of pWInfo->a[] should be coded */
159818 WhereLevel *pLevel, /* The current level pointer */
159819 Bitmask notReady /* Which tables are currently available */
159820){
159821 int j, k; /* Loop counters */
159822 int iCur; /* The VDBE cursor for the table */
159823 int addrNxt; /* Where to jump to continue with the next IN case */
159824 int bRev; /* True if we need to scan in reverse order */
159825 WhereLoop *pLoop; /* The WhereLoop object being coded */
159826 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
159827 WhereTerm *pTerm; /* A WHERE clause term */
159828 sqlite3 *db; /* Database connection */
159829 SrcItem *pTabItem; /* FROM clause term being coded */
159830 int addrBrk; /* Jump here to break out of the loop */
159831 int addrHalt; /* addrBrk for the outermost loop */
159832 int addrCont; /* Jump here to continue with next cycle */
159833 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
159834 int iReleaseReg = 0; /* Temp register to free before returning */
159835 Index *pIdx = 0; /* Index used by loop (if any) */
159836 int iLoop; /* Iteration of constraint generator loop */
159837
159838 pWC = &pWInfo->sWC;
159839 db = pParse->db;
159840 pLoop = pLevel->pWLoop;
159841 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
159842 iCur = pTabItem->iCursor;
159843 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
159844 bRev = (pWInfo->revMask>>iLevel)&1;
159845 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",
159846 iLevel, pTabItem->pSTab->zName));
159847#if WHERETRACE_ENABLED /* 0x4001 */
159848 if( sqlite3WhereTrace & 0x1 ){
159849 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
159850 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
159851 if( sqlite3WhereTrace & 0x1000 ){
159852 sqlite3WhereLoopPrint(pLoop, pWC);
159853 }
159854 }
159855 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
159856 if( iLevel==0 ){
159857 sqlite3DebugPrintf("WHERE clause being coded:\n");
159858 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
159859 }
159860 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
159861 sqlite3WhereClausePrint(pWC);
159862 }
159863#endif
159864
159865 /* Create labels for the "break" and "continue" instructions
159866 ** for the current loop. Jump to addrBrk to break out of a loop.
159867 ** Jump to cont to go immediately to the next iteration of the
159868 ** loop.
159869 **
159870 ** When there is an IN operator, we also have a "addrNxt" label that
159871 ** means to continue with the next IN value combination. When
159872 ** there are no IN operators in the constraints, the "addrNxt" label
159873 ** is the same as "addrBrk".
159874 */
159875 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
159876 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
159877
159878 /* If this is the right table of a LEFT OUTER JOIN, allocate and
159879 ** initialize a memory cell that records if this table matches any
159880 ** row of the left table of the join.
159881 */
159882 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))((void) (0))
159883 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0((void) (0))
159884 )((void) (0));
159885 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT0x08)!=0 ){
159886 pLevel->iLeftJoin = ++pParse->nMem;
159887 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pLevel->iLeftJoin);
159888 VdbeComment((v, "init LEFT JOIN match flag"));
159889 }
159890
159891 /* Compute a safe address to jump to if we discover that the table for
159892 ** this loop is empty and can never contribute content. */
159893 for(j=iLevel; j>0; j--){
159894 if( pWInfo->a[j].iLeftJoin ) break;
159895 if( pWInfo->a[j].pRJ ) break;
159896 }
159897 addrHalt = pWInfo->a[j].addrBrk;
159898
159899 /* Special case of a FROM clause subquery implemented as a co-routine */
159900 if( pTabItem->fg.viaCoroutine ){
159901 int regYield;
159902 Subquery *pSubq;
159903 assert( pTabItem->fg.isSubquery && pTabItem->u4.pSubq!=0 )((void) (0));
159904 pSubq = pTabItem->u4.pSubq;
159905 regYield = pSubq->regReturn;
159906 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, pSubq->addrFillSub);
159907 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield12, regYield, addrBrk);
159908 VdbeCoverage(v);
159909 VdbeComment((v, "next row of %s", pTabItem->pSTab->zName));
159910 pLevel->op = OP_Goto9;
159911 }else
159912
159913#ifndef SQLITE_OMIT_VIRTUALTABLE
159914 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
159915 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
159916 ** to access the data.
159917 */
159918 int iReg; /* P3 Value for OP_VFilter */
159919 int addrNotFound;
159920 int nConstraint = pLoop->nLTerm;
159921
159922 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
159923 addrNotFound = pLevel->addrBrk;
159924 for(j=0; j<nConstraint; j++){
159925 int iTarget = iReg+j+2;
159926 pTerm = pLoop->aLTerm[j];
159927 if( NEVER(pTerm==0)(pTerm==0) ) continue;
159928 if( pTerm->eOperator & WO_IN0x0001 ){
159929 if( SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0) & pLoop->u.vtab.mHandleIn ){
159930 int iTab = pParse->nTab++;
159931 int iCache = ++pParse->nMem;
159932 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
159933 sqlite3VdbeAddOp3(v, OP_VInitIn175, iTab, iTarget, iCache);
159934 }else{
159935 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
159936 addrNotFound = pLevel->addrNxt;
159937 }
159938 }else{
159939 Expr *pRight = pTerm->pExpr->pRight;
159940 codeExprOrVector(pParse, pRight, iTarget, 1);
159941 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET74
159942 && pLoop->u.vtab.bOmitOffset
159943 ){
159944 assert( pTerm->eOperator==WO_AUX )((void) (0));
159945 assert( pWInfo->pSelect!=0 )((void) (0));
159946 assert( pWInfo->pSelect->iOffset>0 )((void) (0));
159947 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWInfo->pSelect->iOffset);
159948 VdbeComment((v,"Zero OFFSET counter"));
159949 }
159950 }
159951 }
159952 sqlite3VdbeAddOp2(v, OP_Integer71, pLoop->u.vtab.idxNum, iReg);
159953 sqlite3VdbeAddOp2(v, OP_Integer71, nConstraint, iReg+1);
159954 sqlite3VdbeAddOp4(v, OP_VFilter6, iCur, addrNotFound, iReg,
159955 pLoop->u.vtab.idxStr,
159956 pLoop->u.vtab.needFree ? P4_DYNAMIC(-6) : P4_STATIC(-1));
159957 VdbeCoverage(v);
159958 pLoop->u.vtab.needFree = 0;
159959 /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed
159960 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
159961 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
159962 pLevel->p1 = iCur;
159963 pLevel->op = pWInfo->eOnePass ? OP_Noop187 : OP_VNext63;
159964 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
159965 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
159966
159967 for(j=0; j<nConstraint; j++){
159968 pTerm = pLoop->aLTerm[j];
159969 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
159970 disableTerm(pLevel, pTerm);
159971 continue;
159972 }
159973 if( (pTerm->eOperator & WO_IN0x0001)!=0
159974 && (SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0) & pLoop->u.vtab.mHandleIn)==0
159975 && !db->mallocFailed
159976 ){
159977 Expr *pCompare; /* The comparison operator */
159978 Expr *pRight; /* RHS of the comparison */
159979 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
159980 int iIn; /* IN loop corresponding to the j-th constraint */
159981
159982 /* Reload the constraint value into reg[iReg+j+2]. The same value
159983 ** was loaded into the same register prior to the OP_VFilter, but
159984 ** the xFilter implementation might have changed the datatype or
159985 ** encoding of the value in the register, so it *must* be reloaded.
159986 */
159987 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn)(iIn<pLevel->u.in.nIn); iIn++){
159988 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
159989 if( (pOp->opcode==OP_Column94 && pOp->p3==iReg+j+2)
159990 || (pOp->opcode==OP_Rowid135 && pOp->p2==iReg+j+2)
159991 ){
159992 testcase( pOp->opcode==OP_Rowid );
159993 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
159994 break;
159995 }
159996 }
159997
159998 /* Generate code that will continue to the next row if
159999 ** the IN constraint is not satisfied
160000 */
160001 pCompare = sqlite3PExpr(pParse, TK_EQ54, 0, 0);
160002 if( !db->mallocFailed ){
160003 int iFld = pTerm->u.x.iField;
160004 Expr *pLeft = pTerm->pExpr->pLeft;
160005 assert( pLeft!=0 )((void) (0));
160006 if( iFld>0 ){
160007 assert( pLeft->op==TK_VECTOR )((void) (0));
160008 assert( ExprUseXList(pLeft) )((void) (0));
160009 assert( iFld<=pLeft->x.pList->nExpr )((void) (0));
160010 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
160011 }else{
160012 pCompare->pLeft = pLeft;
160013 }
160014 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER176, 0);
160015 if( pRight ){
160016 pRight->iTable = iReg+j+2;
160017 sqlite3ExprIfFalse(
160018 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL0x10
160019 );
160020 }
160021 pCompare->pLeft = 0;
160022 }
160023 sqlite3ExprDelete(db, pCompare);
160024 }
160025 }
160026
160027 /* These registers need to be preserved in case there is an IN operator
160028 ** loop. So we could deallocate the registers here (and potentially
160029 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
160030 ** simpler and safer to simply not reuse the registers.
160031 **
160032 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
160033 */
160034 }else
160035#endif /* SQLITE_OMIT_VIRTUALTABLE */
160036
160037 if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
160038 && (pLoop->wsFlags & (WHERE_COLUMN_IN0x00000004|WHERE_COLUMN_EQ0x00000001))!=0
160039 ){
160040 /* Case 2: We can directly reference a single row using an
160041 ** equality comparison against the ROWID field. Or
160042 ** we reference multiple rows using a "rowid IN (...)"
160043 ** construct.
160044 */
160045 assert( pLoop->u.btree.nEq==1 )((void) (0));
160046 pTerm = pLoop->aLTerm[0];
160047 assert( pTerm!=0 )((void) (0));
160048 assert( pTerm->pExpr!=0 )((void) (0));
160049 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160050 iReleaseReg = ++pParse->nMem;
160051 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
160052 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
160053 addrNxt = pLevel->addrNxt;
160054 if( pLevel->regFilter ){
160055 sqlite3VdbeAddOp2(v, OP_MustBeInt13, iRowidReg, addrNxt);
160056 VdbeCoverage(v);
160057 sqlite3VdbeAddOp4Int(v, OP_Filter64, pLevel->regFilter, addrNxt,
160058 iRowidReg, 1);
160059 VdbeCoverage(v);
160060 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
160061 }
160062 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iCur, addrNxt, iRowidReg);
160063 VdbeCoverage(v);
160064 pLevel->op = OP_Noop187;
160065 }else if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
160066 && (pLoop->wsFlags & WHERE_COLUMN_RANGE0x00000002)!=0
160067 ){
160068 /* Case 3: We have an inequality comparison against the ROWID field.
160069 */
160070 int testOp = OP_Noop187;
160071 int start;
160072 int memEndValue = 0;
160073 WhereTerm *pStart, *pEnd;
160074
160075 j = 0;
160076 pStart = pEnd = 0;
160077 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ) pStart = pLoop->aLTerm[j++];
160078 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ) pEnd = pLoop->aLTerm[j++];
160079 assert( pStart!=0 || pEnd!=0 )((void) (0));
160080 if( bRev ){
160081 pTerm = pStart;
160082 pStart = pEnd;
160083 pEnd = pTerm;
160084 }
160085 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
160086 if( pStart ){
160087 Expr *pX; /* The expression that defines the start bound */
160088 int r1, rTemp; /* Registers for holding the start boundary */
160089 int op; /* Cursor seek operation */
160090
160091 /* The following constant maps TK_xx codes into corresponding
160092 ** seek opcodes. It depends on a particular ordering of TK_xx
160093 */
160094 const u8 aMoveOp[] = {
160095 /* TK_GT */ OP_SeekGT24,
160096 /* TK_LE */ OP_SeekLE22,
160097 /* TK_LT */ OP_SeekLT21,
160098 /* TK_GE */ OP_SeekGE23
160099 };
160100 assert( TK_LE==TK_GT+1 )((void) (0)); /* Make sure the ordering.. */
160101 assert( TK_LT==TK_GT+2 )((void) (0)); /* ... of the TK_xx values... */
160102 assert( TK_GE==TK_GT+3 )((void) (0)); /* ... is correct. */
160103
160104 assert( (pStart->wtFlags & TERM_VNULL)==0 )((void) (0));
160105 testcase( pStart->wtFlags & TERM_VIRTUAL );
160106 pX = pStart->pExpr;
160107 assert( pX!=0 )((void) (0));
160108 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
160109 if( sqlite3ExprIsVector(pX->pRight) ){
160110 r1 = rTemp = sqlite3GetTempReg(pParse);
160111 codeExprOrVector(pParse, pX->pRight, r1, 1);
160112 testcase( pX->op==TK_GT );
160113 testcase( pX->op==TK_GE );
160114 testcase( pX->op==TK_LT );
160115 testcase( pX->op==TK_LE );
160116 op = aMoveOp[((pX->op - TK_GT55 - 1) & 0x3) | 0x1];
160117 assert( pX->op!=TK_GT || op==OP_SeekGE )((void) (0));
160118 assert( pX->op!=TK_GE || op==OP_SeekGE )((void) (0));
160119 assert( pX->op!=TK_LT || op==OP_SeekLE )((void) (0));
160120 assert( pX->op!=TK_LE || op==OP_SeekLE )((void) (0));
160121 }else{
160122 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
160123 disableTerm(pLevel, pStart);
160124 op = aMoveOp[(pX->op - TK_GT55)];
160125 }
160126 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
160127 VdbeComment((v, "pk"));
160128 VdbeCoverageIf(v, pX->op==TK_GT);
160129 VdbeCoverageIf(v, pX->op==TK_LE);
160130 VdbeCoverageIf(v, pX->op==TK_LT);
160131 VdbeCoverageIf(v, pX->op==TK_GE);
160132 sqlite3ReleaseTempReg(pParse, rTemp);
160133 }else{
160134 sqlite3VdbeAddOp2(v, bRev ? OP_Last32 : OP_Rewind36, iCur, addrHalt);
160135 VdbeCoverageIf(v, bRev==0);
160136 VdbeCoverageIf(v, bRev!=0);
160137 }
160138 if( pEnd ){
160139 Expr *pX;
160140 pX = pEnd->pExpr;
160141 assert( pX!=0 )((void) (0));
160142 assert( (pEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
160143 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
160144 testcase( pEnd->wtFlags & TERM_VIRTUAL );
160145 memEndValue = ++pParse->nMem;
160146 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
160147 if( 0==sqlite3ExprIsVector(pX->pRight)
160148 && (pX->op==TK_LT57 || pX->op==TK_GT55)
160149 ){
160150 testOp = bRev ? OP_Le56 : OP_Ge58;
160151 }else{
160152 testOp = bRev ? OP_Lt57 : OP_Gt55;
160153 }
160154 if( 0==sqlite3ExprIsVector(pX->pRight) ){
160155 disableTerm(pLevel, pEnd);
160156 }
160157 }
160158 start = sqlite3VdbeCurrentAddr(v);
160159 pLevel->op = bRev ? OP_Prev38 : OP_Next39;
160160 pLevel->p1 = iCur;
160161 pLevel->p2 = start;
160162 assert( pLevel->p5==0 )((void) (0));
160163 if( testOp!=OP_Noop187 ){
160164 iRowidReg = ++pParse->nMem;
160165 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, iRowidReg);
160166 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
160167 VdbeCoverageIf(v, testOp==OP_Le);
160168 VdbeCoverageIf(v, testOp==OP_Lt);
160169 VdbeCoverageIf(v, testOp==OP_Ge);
160170 VdbeCoverageIf(v, testOp==OP_Gt);
160171 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43 | SQLITE_JUMPIFNULL0x10);
160172 }
160173 }else if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
160174 /* Case 4: A scan using an index.
160175 **
160176 ** The WHERE clause may contain zero or more equality
160177 ** terms ("==" or "IN" operators) that refer to the N
160178 ** left-most columns of the index. It may also contain
160179 ** inequality constraints (>, <, >= or <=) on the indexed
160180 ** column that immediately follows the N equalities. Only
160181 ** the right-most column can be an inequality - the rest must
160182 ** use the "==" and "IN" operators. For example, if the
160183 ** index is on (x,y,z), then the following clauses are all
160184 ** optimized:
160185 **
160186 ** x=5
160187 ** x=5 AND y=10
160188 ** x=5 AND y<10
160189 ** x=5 AND y>5 AND y<10
160190 ** x=5 AND y=5 AND z<=10
160191 **
160192 ** The z<10 term of the following cannot be used, only
160193 ** the x=5 term:
160194 **
160195 ** x=5 AND z<10
160196 **
160197 ** N may be zero if there are inequality constraints.
160198 ** If there are no inequality constraints, then N is at
160199 ** least one.
160200 **
160201 ** This case is also used when there are no WHERE clause
160202 ** constraints but an index is selected anyway, in order
160203 ** to force the output order to conform to an ORDER BY.
160204 */
160205 static const u8 aStartOp[] = {
160206 0,
160207 0,
160208 OP_Rewind36, /* 2: (!start_constraints && startEq && !bRev) */
160209 OP_Last32, /* 3: (!start_constraints && startEq && bRev) */
160210 OP_SeekGT24, /* 4: (start_constraints && !startEq && !bRev) */
160211 OP_SeekLT21, /* 5: (start_constraints && !startEq && bRev) */
160212 OP_SeekGE23, /* 6: (start_constraints && startEq && !bRev) */
160213 OP_SeekLE22 /* 7: (start_constraints && startEq && bRev) */
160214 };
160215 static const u8 aEndOp[] = {
160216 OP_IdxGE45, /* 0: (end_constraints && !bRev && !endEq) */
160217 OP_IdxGT41, /* 1: (end_constraints && !bRev && endEq) */
160218 OP_IdxLE40, /* 2: (end_constraints && bRev && !endEq) */
160219 OP_IdxLT42, /* 3: (end_constraints && bRev && endEq) */
160220 };
160221 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
160222 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
160223 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
160224 int regBase; /* Base register holding constraint values */
160225 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
160226 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
160227 int startEq; /* True if range start uses ==, >= or <= */
160228 int endEq; /* True if range end uses ==, >= or <= */
160229 int start_constraints; /* Start of range is constrained */
160230 int nConstraint; /* Number of constraint terms */
160231 int iIdxCur; /* The VDBE cursor for the index */
160232 int nExtraReg = 0; /* Number of extra registers needed */
160233 int op; /* Instruction opcode */
160234 char *zStartAff; /* Affinity for start of range constraint */
160235 char *zEndAff = 0; /* Affinity for end of range constraint */
160236 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
160237 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
160238 int omitTable; /* True if we use the index only */
160239 int regBignull = 0; /* big-null flag register */
160240 int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */
160241
160242 pIdx = pLoop->u.btree.pIndex;
160243 iIdxCur = pLevel->iIdxCur;
160244 assert( nEq>=pLoop->nSkip )((void) (0));
160245
160246 /* Find any inequality constraint terms for the start and end
160247 ** of the range.
160248 */
160249 j = nEq;
160250 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
160251 pRangeStart = pLoop->aLTerm[j++];
160252 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm)((nExtraReg)>(pLoop->u.btree.nBtm)?(nExtraReg):(pLoop->
u.btree.nBtm))
;
160253 /* Like optimization range constraints always occur in pairs */
160254 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||((void) (0))
160255 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 )((void) (0));
160256 }
160257 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ){
160258 pRangeEnd = pLoop->aLTerm[j++];
160259 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop)((nExtraReg)>(pLoop->u.btree.nTop)?(nExtraReg):(pLoop->
u.btree.nTop))
;
160260#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
160261 if( (pRangeEnd->wtFlags & TERM_LIKEOPT0x0100)!=0 ){
160262 assert( pRangeStart!=0 )((void) (0)); /* LIKE opt constraints */
160263 assert( pRangeStart->wtFlags & TERM_LIKEOPT )((void) (0)); /* occur in pairs */
160264 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
160265 sqlite3VdbeAddOp2(v, OP_Integer71, 1, (int)pLevel->iLikeRepCntr);
160266 VdbeComment((v, "LIKE loop counter"));
160267 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
160268 /* iLikeRepCntr actually stores 2x the counter register number. The
160269 ** bottom bit indicates whether the search order is ASC or DESC. */
160270 testcase( bRev );
160271 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
160272 assert( (bRev & ~1)==0 )((void) (0));
160273 pLevel->iLikeRepCntr <<=1;
160274 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC1);
160275 }
160276#endif
160277 if( pRangeStart==0 ){
160278 j = pIdx->aiColumn[nEq];
160279 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR(-2) ){
160280 bSeekPastNull = 1;
160281 }
160282 }
160283 }
160284 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
160285
160286 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
160287 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
160288 ** FIRST). In both cases separate ordered scans are made of those
160289 ** index entries for which the column is null and for those for which
160290 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
160291 ** For DESC, NULL entries are scanned first.
160292 */
160293 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT0x00000010|WHERE_BTM_LIMIT0x00000020))==0
160294 && (pLoop->wsFlags & WHERE_BIGNULL_SORT0x00080000)!=0
160295 ){
160296 assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 )((void) (0));
160297 assert( pRangeEnd==0 && pRangeStart==0 )((void) (0));
160298 testcase( pLoop->nSkip>0 );
160299 nExtraReg = 1;
160300 bSeekPastNull = 1;
160301 pLevel->regBignull = regBignull = ++pParse->nMem;
160302 if( pLevel->iLeftJoin ){
160303 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regBignull);
160304 }
160305 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
160306 }
160307
160308 /* If we are doing a reverse order scan on an ascending index, or
160309 ** a forward order scan on a descending index, interchange the
160310 ** start and end terms (pRangeStart and pRangeEnd).
160311 */
160312 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC0)) ){
160313 SWAP(WhereTerm *, pRangeEnd, pRangeStart){WhereTerm * t=pRangeEnd; pRangeEnd=pRangeStart; pRangeStart=
t;}
;
160314 SWAP(u8, bSeekPastNull, bStopAtNull){u8 t=bSeekPastNull; bSeekPastNull=bStopAtNull; bStopAtNull=t
;}
;
160315 SWAP(u8, nBtm, nTop){u8 t=nBtm; nBtm=nTop; nTop=t;};
160316 }
160317
160318 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)!=0 ){
160319 /* In case OP_SeekScan is used, ensure that the index cursor does not
160320 ** point to a valid row for the first iteration of this loop. */
160321 sqlite3VdbeAddOp1(v, OP_NullRow136, iIdxCur);
160322 }
160323
160324 /* Generate code to evaluate all constraint terms using == or IN
160325 ** and store the values of those terms in an array of registers
160326 ** starting at regBase.
160327 */
160328 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
160329 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
160330 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq )((void) (0));
160331 if( zStartAff && nTop ){
160332 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
160333 }
160334 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
160335
160336 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
160337 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
160338 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
160339 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
160340 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE(0x0002<<(56 -54))|WO_GE(0x0002<<(58 -54)));
160341 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE(0x0002<<(56 -54))|WO_GE(0x0002<<(58 -54)));
160342 start_constraints = pRangeStart || nEq>0;
160343
160344 /* Seek the index cursor to the start of the range. */
160345 nConstraint = nEq;
160346 if( pRangeStart ){
160347 Expr *pRight = pRangeStart->pExpr->pRight;
160348 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
160349 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
160350 if( (pRangeStart->wtFlags & TERM_VNULL0x0080)==0
160351 && sqlite3ExprCanBeNull(pRight)
160352 ){
160353 sqlite3VdbeAddOp2(v, OP_IsNull51, regBase+nEq, addrNxt);
160354 VdbeCoverage(v);
160355 }
160356 if( zStartAff ){
160357 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
160358 }
160359 nConstraint += nBtm;
160360 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
160361 if( sqlite3ExprIsVector(pRight)==0 ){
160362 disableTerm(pLevel, pRangeStart);
160363 }else{
160364 startEq = 1;
160365 }
160366 bSeekPastNull = 0;
160367 }else if( bSeekPastNull ){
160368 startEq = 0;
160369 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
160370 start_constraints = 1;
160371 nConstraint++;
160372 }else if( regBignull ){
160373 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
160374 start_constraints = 1;
160375 nConstraint++;
160376 }
160377 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
160378 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
160379 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
160380 ** above has already left the cursor sitting on the correct row,
160381 ** so no further seeking is needed */
160382 }else{
160383 if( regBignull ){
160384 sqlite3VdbeAddOp2(v, OP_Integer71, 1, regBignull);
160385 VdbeComment((v, "NULL-scan pass ctr"));
160386 }
160387 if( pLevel->regFilter ){
160388 sqlite3VdbeAddOp4Int(v, OP_Filter64, pLevel->regFilter, addrNxt,
160389 regBase, nEq);
160390 VdbeCoverage(v);
160391 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
160392 }
160393
160394 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
160395 assert( op!=0 )((void) (0));
160396 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)!=0 && op==OP_SeekGE23 ){
160397 assert( regBignull==0 )((void) (0));
160398 /* TUNING: The OP_SeekScan opcode seeks to reduce the number
160399 ** of expensive seek operations by replacing a single seek with
160400 ** 1 or more step operations. The question is, how many steps
160401 ** should we try before giving up and going with a seek. The cost
160402 ** of a seek is proportional to the logarithm of the of the number
160403 ** of entries in the tree, so basing the number of steps to try
160404 ** on the estimated number of rows in the btree seems like a good
160405 ** guess. */
160406 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan124,
160407 (pIdx->aiRowLogEst[0]+9)/10);
160408 if( pRangeStart || pRangeEnd ){
160409 sqlite3VdbeChangeP5(v, 1);
160410 sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
160411 addrSeekScan = 0;
160412 }
160413 VdbeCoverage(v);
160414 }
160415 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
160416 VdbeCoverage(v);
160417 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
160418 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
160419 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
160420 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
160421 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
160422 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
160423
160424 assert( bSeekPastNull==0 || bStopAtNull==0 )((void) (0));
160425 if( regBignull ){
160426 assert( bSeekPastNull==1 || bStopAtNull==1 )((void) (0));
160427 assert( bSeekPastNull==!bStopAtNull )((void) (0));
160428 assert( bStopAtNull==startEq )((void) (0));
160429 sqlite3VdbeAddOp2(v, OP_Goto9, 0, sqlite3VdbeCurrentAddr(v)+2);
160430 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
160431 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
160432 nConstraint-startEq);
160433 VdbeCoverage(v);
160434 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
160435 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
160436 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
160437 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
160438 assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE)((void) (0));
160439 }
160440 }
160441
160442 /* Load the value for the inequality constraint at the end of the
160443 ** range (if any).
160444 */
160445 nConstraint = nEq;
160446 assert( pLevel->p2==0 )((void) (0));
160447 if( pRangeEnd ){
160448 Expr *pRight = pRangeEnd->pExpr->pRight;
160449 assert( addrSeekScan==0 )((void) (0));
160450 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
160451 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
160452 if( (pRangeEnd->wtFlags & TERM_VNULL0x0080)==0
160453 && sqlite3ExprCanBeNull(pRight)
160454 ){
160455 sqlite3VdbeAddOp2(v, OP_IsNull51, regBase+nEq, addrNxt);
160456 VdbeCoverage(v);
160457 }
160458 if( zEndAff ){
160459 updateRangeAffinityStr(pRight, nTop, zEndAff);
160460 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
160461 }else{
160462 assert( pParse->db->mallocFailed )((void) (0));
160463 }
160464 nConstraint += nTop;
160465 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
160466
160467 if( sqlite3ExprIsVector(pRight)==0 ){
160468 disableTerm(pLevel, pRangeEnd);
160469 }else{
160470 endEq = 1;
160471 }
160472 }else if( bStopAtNull ){
160473 if( regBignull==0 ){
160474 sqlite3VdbeAddOp2(v, OP_Null75, 0, regBase+nEq);
160475 endEq = 0;
160476 }
160477 nConstraint++;
160478 }
160479 if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff);
160480 if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff);
160481
160482 /* Top of the loop body */
160483 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
160484
160485 /* Check if the index cursor is past the end of the range. */
160486 if( nConstraint ){
160487 if( regBignull ){
160488 /* Except, skip the end-of-range check while doing the NULL-scan */
160489 sqlite3VdbeAddOp2(v, OP_IfNot17, regBignull, sqlite3VdbeCurrentAddr(v)+3);
160490 VdbeComment((v, "If NULL-scan 2nd pass"));
160491 VdbeCoverage(v);
160492 }
160493 op = aEndOp[bRev*2 + endEq];
160494 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
160495 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
160496 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
160497 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
160498 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
160499 if( addrSeekScan ) sqlite3VdbeJumpHere(v, addrSeekScan);
160500 }
160501 if( regBignull ){
160502 /* During a NULL-scan, check to see if we have reached the end of
160503 ** the NULLs */
160504 assert( bSeekPastNull==!bStopAtNull )((void) (0));
160505 assert( bSeekPastNull+bStopAtNull==1 )((void) (0));
160506 assert( nConstraint+bSeekPastNull>0 )((void) (0));
160507 sqlite3VdbeAddOp2(v, OP_If16, regBignull, sqlite3VdbeCurrentAddr(v)+2);
160508 VdbeComment((v, "If NULL-scan 1st pass"));
160509 VdbeCoverage(v);
160510 op = aEndOp[bRev*2 + bSeekPastNull];
160511 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
160512 nConstraint+bSeekPastNull);
160513 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
160514 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
160515 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
160516 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
160517 }
160518
160519 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000)!=0 ){
160520 sqlite3VdbeAddOp3(v, OP_SeekHit125, iIdxCur, nEq, nEq);
160521 }
160522
160523 /* Seek the table cursor, if required */
160524 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)!=0
160525 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE0x0020|WHERE_RIGHT_JOIN0x1000))==0;
160526 if( omitTable ){
160527 /* pIdx is a covering index. No need to access the main table. */
160528 }else if( HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) ){
160529 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
160530 }else if( iCur!=iIdxCur ){
160531 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
160532 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
160533 for(j=0; j<pPk->nKeyCol; j++){
160534 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
160535 sqlite3VdbeAddOp3(v, OP_Column94, iIdxCur, k, iRowidReg+j);
160536 }
160537 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iCur, addrCont,
160538 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
160539 }
160540
160541 if( pLevel->iLeftJoin==0 ){
160542 /* If a partial index is driving the loop, try to eliminate WHERE clause
160543 ** terms from the query that must be true due to the WHERE clause of
160544 ** the partial index.
160545 **
160546 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
160547 ** for a LEFT JOIN.
160548 */
160549 if( pIdx->pPartIdxWhere ){
160550 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
160551 }
160552 }else{
160553 testcase( pIdx->pPartIdxWhere );
160554 /* The following assert() is not a requirement, merely an observation:
160555 ** The OR-optimization doesn't work for the right hand table of
160556 ** a LEFT JOIN: */
160557 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 )((void) (0));
160558 }
160559
160560 /* Record the instruction used to terminate the loop. */
160561 if( (pLoop->wsFlags & WHERE_ONEROW0x00001000)
160562 || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
160563 ){
160564 pLevel->op = OP_Noop187;
160565 }else if( bRev ){
160566 pLevel->op = OP_Prev38;
160567 }else{
160568 pLevel->op = OP_Next39;
160569 }
160570 pLevel->p1 = iIdxCur;
160571 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED0x00010000)!=0 ? 1:0;
160572 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)==0 ){
160573 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
160574 }else{
160575 assert( pLevel->p5==0 )((void) (0));
160576 }
160577 if( omitTable ) pIdx = 0;
160578 }else
160579
160580#ifndef SQLITE_OMIT_OR_OPTIMIZATION
160581 if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
160582 /* Case 5: Two or more separately indexed terms connected by OR
160583 **
160584 ** Example:
160585 **
160586 ** CREATE TABLE t1(a,b,c,d);
160587 ** CREATE INDEX i1 ON t1(a);
160588 ** CREATE INDEX i2 ON t1(b);
160589 ** CREATE INDEX i3 ON t1(c);
160590 **
160591 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
160592 **
160593 ** In the example, there are three indexed terms connected by OR.
160594 ** The top of the loop looks like this:
160595 **
160596 ** Null 1 # Zero the rowset in reg 1
160597 **
160598 ** Then, for each indexed term, the following. The arguments to
160599 ** RowSetTest are such that the rowid of the current row is inserted
160600 ** into the RowSet. If it is already present, control skips the
160601 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
160602 **
160603 ** sqlite3WhereBegin(<term>)
160604 ** RowSetTest # Insert rowid into rowset
160605 ** Gosub 2 A
160606 ** sqlite3WhereEnd()
160607 **
160608 ** Following the above, code to terminate the loop. Label A, the target
160609 ** of the Gosub above, jumps to the instruction right after the Goto.
160610 **
160611 ** Null 1 # Zero the rowset in reg 1
160612 ** Goto B # The loop is finished.
160613 **
160614 ** A: <loop body> # Return data, whatever.
160615 **
160616 ** Return 2 # Jump back to the Gosub
160617 **
160618 ** B: <after the loop>
160619 **
160620 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
160621 ** use an ephemeral index instead of a RowSet to record the primary
160622 ** keys of the rows we have already seen.
160623 **
160624 */
160625 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
160626 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
160627 Index *pCov = 0; /* Potential covering index (or NULL) */
160628 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
160629
160630 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
160631 int regRowset = 0; /* Register for RowSet object */
160632 int regRowid = 0; /* Register holding rowid */
160633 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
160634 int iRetInit; /* Address of regReturn init */
160635 int untestedTerms = 0; /* Some terms not completely tested */
160636 int ii; /* Loop counter */
160637 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
160638 Table *pTab = pTabItem->pSTab;
160639
160640 pTerm = pLoop->aLTerm[0];
160641 assert( pTerm!=0 )((void) (0));
160642 assert( pTerm->eOperator & WO_OR )((void) (0));
160643 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 )((void) (0));
160644 pOrWc = &pTerm->u.pOrInfo->wc;
160645 pLevel->op = OP_Return67;
160646 pLevel->p1 = regReturn;
160647
160648 /* Set up a new SrcList in pOrTab containing the table being scanned
160649 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
160650 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
160651 */
160652 if( pWInfo->nLevel>1 ){
160653 int nNotReady; /* The number of notReady tables */
160654 SrcItem *origSrc; /* Original list of tables */
160655 nNotReady = pWInfo->nLevel - iLevel - 1;
160656 pOrTab = sqlite3DbMallocRawNN(db,
160657 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
160658 if( pOrTab==0 ) return notReady;
160659 pOrTab->nAlloc = (u8)(nNotReady + 1);
160660 pOrTab->nSrc = pOrTab->nAlloc;
160661 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
160662 origSrc = pWInfo->pTabList->a;
160663 for(k=1; k<=nNotReady; k++){
160664 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
160665 }
160666 }else{
160667 pOrTab = pWInfo->pTabList;
160668 }
160669
160670 /* Initialize the rowset register to contain NULL. An SQL NULL is
160671 ** equivalent to an empty rowset. Or, create an ephemeral index
160672 ** capable of holding primary keys in the case of a WITHOUT ROWID.
160673 **
160674 ** Also initialize regReturn to contain the address of the instruction
160675 ** immediately following the OP_Return at the bottom of the loop. This
160676 ** is required in a few obscure LEFT JOIN cases where control jumps
160677 ** over the top of the loop into the body of it. In this case the
160678 ** correct response for the end-of-loop code (the OP_Return) is to
160679 ** fall through to the next instruction, just as an OP_Next does if
160680 ** called on an uninitialized cursor.
160681 */
160682 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
160683 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
160684 regRowset = ++pParse->nMem;
160685 sqlite3VdbeAddOp2(v, OP_Null75, 0, regRowset);
160686 }else{
160687 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
160688 regRowset = pParse->nTab++;
160689 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, regRowset, pPk->nKeyCol);
160690 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
160691 }
160692 regRowid = ++pParse->nMem;
160693 }
160694 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer71, 0, regReturn);
160695
160696 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
160697 ** Then for every term xN, evaluate as the subexpression: xN AND y
160698 ** That way, terms in y that are factored into the disjunction will
160699 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
160700 **
160701 ** Actually, each subexpression is converted to "xN AND w" where w is
160702 ** the "interesting" terms of z - terms that did not originate in the
160703 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
160704 ** indices.
160705 **
160706 ** This optimization also only applies if the (x1 OR x2 OR ...) term
160707 ** is not contained in the ON clause of a LEFT JOIN.
160708 ** See ticket http://www.sqlite.org/src/info/f2369304e4
160709 **
160710 ** 2022-02-04: Do not push down slices of a row-value comparison.
160711 ** In other words, "w" or "y" may not be a slice of a vector. Otherwise,
160712 ** the initialization of the right-hand operand of the vector comparison
160713 ** might not occur, or might occur only in an OR branch that is not
160714 ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
160715 **
160716 ** 2022-03-03: Do not push down expressions that involve subqueries.
160717 ** The subquery might get coded as a subroutine. Any table-references
160718 ** in the subquery might be resolved to index-references for the index on
160719 ** the OR branch in which the subroutine is coded. But if the subroutine
160720 ** is invoked from a different OR branch that uses a different index, such
160721 ** index-references will not work. tag-20220303a
160722 ** https://sqlite.org/forum/forumpost/36937b197273d403
160723 */
160724 if( pWC->nTerm>1 ){
160725 int iTerm;
160726 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
160727 Expr *pExpr = pWC->a[iTerm].pExpr;
160728 if( &pWC->a[iTerm] == pTerm ) continue;
160729 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
160730 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
160731 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
160732 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004|TERM_SLICE0x8000))!=0 ){
160733 continue;
160734 }
160735 if( (pWC->a[iTerm].eOperator & WO_ALL0x3fff)==0 ) continue;
160736 if( ExprHasProperty(pExpr, EP_Subquery)(((pExpr)->flags&(0x400000))!=0) ) continue; /* tag-20220303a */
160737 pExpr = sqlite3ExprDup(db, pExpr, 0);
160738 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
160739 }
160740 if( pAndExpr ){
160741 /* The extra 0x10000 bit on the opcode is masked off and does not
160742 ** become part of the new Expr.op. However, it does make the
160743 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
160744 ** prevents sqlite3PExpr() from applying the AND short-circuit
160745 ** optimization, which we do not want here. */
160746 pAndExpr = sqlite3PExpr(pParse, TK_AND44|0x10000, 0, pAndExpr);
160747 }
160748 }
160749
160750 /* Run a separate WHERE clause for each term of the OR clause. After
160751 ** eliminating duplicates from other WHERE clauses, the action for each
160752 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
160753 */
160754 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"))sqlite3VdbeExplain (pParse, 1, "MULTI-INDEX OR");
160755 for(ii=0; ii<pOrWc->nTerm; ii++){
160756 WhereTerm *pOrTerm = &pOrWc->a[ii];
160757 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
160758 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
160759 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
160760 Expr *pDelete; /* Local copy of OR clause term */
160761 int jmp1 = 0; /* Address of jump operation */
160762 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
160763 && !ExprHasProperty(pOrExpr, EP_OuterON)
160764 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
160765 pDelete = pOrExpr = sqlite3ExprDup(db, pOrExpr, 0);
160766 if( db->mallocFailed ){
160767 sqlite3ExprDelete(db, pDelete);
160768 continue;
160769 }
160770 if( pAndExpr ){
160771 pAndExpr->pLeft = pOrExpr;
160772 pOrExpr = pAndExpr;
160773 }
160774 /* Loop through table entries that match term pOrTerm. */
160775 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1))sqlite3VdbeExplain (pParse, 1, "INDEX %d", ii+1);
160776 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
160777 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
160778 WHERE_OR_SUBCLAUSE0x0020, iCovCur);
160779 assert( pSubWInfo || pParse->nErr )((void) (0));
160780 if( pSubWInfo ){
160781 WhereLoop *pSubLoop;
160782 int addrExplain = sqlite3WhereExplainOneScan(
160783 pParse, pOrTab, &pSubWInfo->a[0], 0
160784 );
160785 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
160786
160787 /* This is the sub-WHERE clause body. First skip over
160788 ** duplicate rows from prior sub-WHERE clauses, and record the
160789 ** rowid (or PRIMARY KEY) for the current row so that the same
160790 ** row will be skipped in subsequent sub-WHERE clauses.
160791 */
160792 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
160793 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
160794 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
160795 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
160796 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest47, regRowset, 0,
160797 regRowid, iSet);
160798 VdbeCoverage(v);
160799 }else{
160800 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
160801 int nPk = pPk->nKeyCol;
160802 int iPk;
160803 int r;
160804
160805 /* Read the PK into an array of temp registers. */
160806 r = sqlite3GetTempRange(pParse, nPk);
160807 for(iPk=0; iPk<nPk; iPk++){
160808 int iCol = pPk->aiColumn[iPk];
160809 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
160810 }
160811
160812 /* Check if the temp table already contains this key. If so,
160813 ** the row has already been included in the result set and
160814 ** can be ignored (by jumping past the Gosub below). Otherwise,
160815 ** insert the key into the temp table and proceed with processing
160816 ** the row.
160817 **
160818 ** Use some of the same optimizations as OP_RowSetTest: If iSet
160819 ** is zero, assume that the key cannot already be present in
160820 ** the temp table. And if iSet is -1, assume that there is no
160821 ** need to insert the key into the temp table, as it will never
160822 ** be tested for. */
160823 if( iSet ){
160824 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found29, regRowset, 0, r, nPk);
160825 VdbeCoverage(v);
160826 }
160827 if( iSet>=0 ){
160828 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r, nPk, regRowid);
160829 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, regRowset, regRowid,
160830 r, nPk);
160831 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
160832 }
160833
160834 /* Release the array of temp registers */
160835 sqlite3ReleaseTempRange(pParse, r, nPk);
160836 }
160837 }
160838
160839 /* Invoke the main loop body as a subroutine */
160840 sqlite3VdbeAddOp2(v, OP_Gosub10, regReturn, iLoopBody);
160841
160842 /* Jump here (skipping the main loop body subroutine) if the
160843 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
160844 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
160845
160846 /* The pSubWInfo->untestedTerms flag means that this OR term
160847 ** contained one or more AND term from a notReady table. The
160848 ** terms from the notReady table could not be tested and will
160849 ** need to be tested later.
160850 */
160851 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
160852
160853 /* If all of the OR-connected terms are optimized using the same
160854 ** index, and the index is opened using the same cursor number
160855 ** by each call to sqlite3WhereBegin() made by this loop, it may
160856 ** be possible to use that index as a covering index.
160857 **
160858 ** If the call to sqlite3WhereBegin() above resulted in a scan that
160859 ** uses an index, and this is either the first OR-connected term
160860 ** processed or the index is the same as that used by all previous
160861 ** terms, set pCov to the candidate covering index. Otherwise, set
160862 ** pCov to NULL to indicate that no candidate covering index will
160863 ** be available.
160864 */
160865 pSubLoop = pSubWInfo->a[0].pWLoop;
160866 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
160867 if( (pSubLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
160868 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
160869 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)((pSubLoop->u.btree.pIndex)->idxType==2))
160870 ){
160871 assert( pSubWInfo->a[0].iIdxCur==iCovCur )((void) (0));
160872 pCov = pSubLoop->u.btree.pIndex;
160873 }else{
160874 pCov = 0;
160875 }
160876 if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){
160877 pWInfo->bDeferredSeek = 1;
160878 }
160879
160880 /* Finish the loop through table entries that match term pOrTerm. */
160881 sqlite3WhereEnd(pSubWInfo);
160882 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
160883 }
160884 sqlite3ExprDelete(db, pDelete);
160885 }
160886 }
160887 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
160888 assert( pLevel->pWLoop==pLoop )((void) (0));
160889 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 )((void) (0));
160890 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 )((void) (0));
160891 pLevel->u.pCoveringIdx = pCov;
160892 if( pCov ) pLevel->iIdxCur = iCovCur;
160893 if( pAndExpr ){
160894 pAndExpr->pLeft = 0;
160895 sqlite3ExprDelete(db, pAndExpr);
160896 }
160897 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
160898 sqlite3VdbeGoto(v, pLevel->addrBrk);
160899 sqlite3VdbeResolveLabel(v, iLoopBody);
160900
160901 /* Set the P2 operand of the OP_Return opcode that will end the current
160902 ** loop to point to this spot, which is the top of the next containing
160903 ** loop. The byte-code formatter will use that P2 value as a hint to
160904 ** indent everything in between the this point and the final OP_Return.
160905 ** See tag-20220407a in vdbe.c and shell.c */
160906 assert( pLevel->op==OP_Return )((void) (0));
160907 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
160908
160909 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
160910 if( !untestedTerms ) disableTerm(pLevel, pTerm);
160911 }else
160912#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
160913
160914 {
160915 /* Case 6: There is no usable index. We must do a complete
160916 ** scan of the entire table.
160917 */
160918 static const u8 aStep[] = { OP_Next39, OP_Prev38 };
160919 static const u8 aStart[] = { OP_Rewind36, OP_Last32 };
160920 assert( bRev==0 || bRev==1 )((void) (0));
160921 if( pTabItem->fg.isRecursive ){
160922 /* Tables marked isRecursive have only a single row that is stored in
160923 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
160924 pLevel->op = OP_Noop187;
160925 }else{
160926 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
160927 pLevel->op = aStep[bRev];
160928 pLevel->p1 = iCur;
160929 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
160930 VdbeCoverageIf(v, bRev==0);
160931 VdbeCoverageIf(v, bRev!=0);
160932 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
160933 }
160934 }
160935
160936#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
160937 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
160938#endif
160939
160940 /* Insert code to test every subexpression that can be completely
160941 ** computed using the current set of tables.
160942 **
160943 ** This loop may run between one and three times, depending on the
160944 ** constraints to be generated. The value of stack variable iLoop
160945 ** determines the constraints coded by each iteration, as follows:
160946 **
160947 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
160948 ** iLoop==2: Code remaining expressions that do not contain correlated
160949 ** sub-queries.
160950 ** iLoop==3: Code all remaining expressions.
160951 **
160952 ** An effort is made to skip unnecessary iterations of the loop.
160953 **
160954 ** This optimization of causing simple query restrictions to occur before
160955 ** more complex one is call the "push-down" optimization in MySQL. Here
160956 ** in SQLite, the name is "MySQL push-down", since there is also another
160957 ** totally unrelated optimization called "WHERE-clause push-down".
160958 ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware.
160959 */
160960 iLoop = (pIdx ? 1 : 2);
160961 do{
160962 int iNext = 0; /* Next value for iLoop */
160963 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
160964 Expr *pE;
160965 int skipLikeAddr = 0;
160966 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160967 testcase( pTerm->wtFlags & TERM_CODED );
160968 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
160969 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
160970 testcase( pWInfo->untestedTerms==0
160971 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
160972 pWInfo->untestedTerms = 1;
160973 continue;
160974 }
160975 pE = pTerm->pExpr;
160976 assert( pE!=0 )((void) (0));
160977 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10) ){
160978 if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON)(((pE)->flags&(0x000001|0x000002))!=0) ){
160979 /* Defer processing WHERE clause constraints until after outer
160980 ** join processing. tag-20220513a */
160981 continue;
160982 }else if( (pTabItem->fg.jointype & JT_LEFT0x08)==JT_LEFT0x08
160983 && !ExprHasProperty(pE,EP_OuterON)(((pE)->flags&(0x000001))!=0) ){
160984 continue;
160985 }else{
160986 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
160987 if( m & pLevel->notReady ){
160988 /* An ON clause that is not ripe */
160989 continue;
160990 }
160991 }
160992 }
160993 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
160994 iNext = 2;
160995 continue;
160996 }
160997 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT0x1000) ){
160998 if( iNext==0 ) iNext = 3;
160999 continue;
161000 }
161001
161002 if( (pTerm->wtFlags & TERM_LIKECOND0x0200)!=0 ){
161003 /* If the TERM_LIKECOND flag is set, that means that the range search
161004 ** is sufficient to guarantee that the LIKE operator is true, so we
161005 ** can skip the call to the like(A,B) function. But this only works
161006 ** for strings. So do not skip the call to the function on the pass
161007 ** that compares BLOBs. */
161008#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
161009 continue;
161010#else
161011 u32 x = pLevel->iLikeRepCntr;
161012 if( x>0 ){
161013 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot17:OP_If16,(int)(x>>1));
161014 VdbeCoverageIf(v, (x&1)==1);
161015 VdbeCoverageIf(v, (x&1)==0);
161016 }
161017#endif
161018 }
161019#ifdef WHERETRACE_ENABLED /* 0xffffffff */
161020 if( sqlite3WhereTrace ){
161021 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
161022 pWC->nTerm-j, pTerm, iLoop));
161023 }
161024 if( sqlite3WhereTrace & 0x4000 ){
161025 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
161026 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
161027 }
161028#endif
161029 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL0x10);
161030 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
161031 pTerm->wtFlags |= TERM_CODED0x0004;
161032 }
161033 iLoop = iNext;
161034 }while( iLoop>0 );
161035
161036 /* Insert code to test for implied constraints based on transitivity
161037 ** of the "==" operator.
161038 **
161039 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
161040 ** and we are coding the t1 loop and the t2 loop has not yet coded,
161041 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
161042 ** the implied "t1.a=123" constraint.
161043 */
161044 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
161045 Expr *pE, sEAlt;
161046 WhereTerm *pAlt;
161047 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
161048 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) continue;
161049 if( (pTerm->eOperator & WO_EQUIV0x0800)==0 ) continue;
161050 if( pTerm->leftCursor!=iCur ) continue;
161051 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10) ) continue;
161052 pE = pTerm->pExpr;
161053#ifdef WHERETRACE_ENABLED /* 0x4001 */
161054 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
161055 sqlite3DebugPrintf("Coding transitive constraint:\n");
161056 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
161057 }
161058#endif
161059 assert( !ExprHasProperty(pE, EP_OuterON) )((void) (0));
161060 assert( (pTerm->prereqRight & pLevel->notReady)!=0 )((void) (0));
161061 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
161062 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
161063 WO_EQ0x0002|WO_IN0x0001|WO_IS0x0080, 0);
161064 if( pAlt==0 ) continue;
161065 if( pAlt->wtFlags & (TERM_CODED0x0004) ) continue;
161066 if( (pAlt->eOperator & WO_IN0x0001)
161067 && ExprUseXSelect(pAlt->pExpr)(((pAlt->pExpr)->flags&0x001000)!=0)
161068 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
161069 ){
161070 continue;
161071 }
161072 testcase( pAlt->eOperator & WO_EQ );
161073 testcase( pAlt->eOperator & WO_IS );
161074 testcase( pAlt->eOperator & WO_IN );
161075 VdbeModuleComment((v, "begin transitive constraint"));
161076 sEAlt = *pAlt->pExpr;
161077 sEAlt.pLeft = pE->pLeft;
161078 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL0x10);
161079 pAlt->wtFlags |= TERM_CODED0x0004;
161080 }
161081
161082 /* For a RIGHT OUTER JOIN, record the fact that the current row has
161083 ** been matched at least once.
161084 */
161085 if( pLevel->pRJ ){
161086 Table *pTab;
161087 int nPk;
161088 int r;
161089 int jmp1 = 0;
161090 WhereRightJoin *pRJ = pLevel->pRJ;
161091
161092 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
161093 ** will record that the current row of that table has been matched at
161094 ** least once. This is accomplished by storing the PK for the row in
161095 ** both the iMatch index and the regBloom Bloom filter.
161096 */
161097 pTab = pWInfo->pTabList->a[pLevel->iFrom].pSTab;
161098 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
161099 r = sqlite3GetTempRange(pParse, 2);
161100 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
161101 nPk = 1;
161102 }else{
161103 int iPk;
161104 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
161105 nPk = pPk->nKeyCol;
161106 r = sqlite3GetTempRange(pParse, nPk+1);
161107 for(iPk=0; iPk<nPk; iPk++){
161108 int iCol = pPk->aiColumn[iPk];
161109 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+1+iPk);
161110 }
161111 }
161112 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found29, pRJ->iMatch, 0, r+1, nPk);
161113 VdbeCoverage(v);
161114 VdbeComment((v, "match against %s", pTab->zName));
161115 sqlite3VdbeAddOp3(v, OP_MakeRecord97, r+1, nPk, r);
161116 sqlite3VdbeAddOp4Int(v, OP_IdxInsert138, pRJ->iMatch, r, r+1, nPk);
161117 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pRJ->regBloom, 0, r+1, nPk);
161118 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
161119 sqlite3VdbeJumpHere(v, jmp1);
161120 sqlite3ReleaseTempRange(pParse, r, nPk+1);
161121 }
161122
161123 /* For a LEFT OUTER JOIN, generate code that will record the fact that
161124 ** at least one row of the right table has matched the left table.
161125 */
161126 if( pLevel->iLeftJoin ){
161127 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
161128 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pLevel->iLeftJoin);
161129 VdbeComment((v, "record LEFT JOIN hit"));
161130 if( pLevel->pRJ==0 ){
161131 goto code_outer_join_constraints; /* WHERE clause constraints */
161132 }
161133 }
161134
161135 if( pLevel->pRJ ){
161136 /* Create a subroutine used to process all interior loops and code
161137 ** of the RIGHT JOIN. During normal operation, the subroutine will
161138 ** be in-line with the rest of the code. But at the end, a separate
161139 ** loop will run that invokes this subroutine for unmatched rows
161140 ** of pTab, with all tables to left begin set to NULL.
161141 */
161142 WhereRightJoin *pRJ = pLevel->pRJ;
161143 sqlite3VdbeAddOp2(v, OP_BeginSubrtn74, 0, pRJ->regReturn);
161144 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
161145 assert( pParse->withinRJSubrtn < 255 )((void) (0));
161146 pParse->withinRJSubrtn++;
161147
161148 /* WHERE clause constraints must be deferred until after outer join
161149 ** row elimination has completed, since WHERE clause constraints apply
161150 ** to the results of the OUTER JOIN. The following loop generates the
161151 ** appropriate WHERE clause constraint checks. tag-20220513a.
161152 */
161153 code_outer_join_constraints:
161154 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
161155 testcase( pTerm->wtFlags & TERM_VIRTUAL );
161156 testcase( pTerm->wtFlags & TERM_CODED );
161157 if( pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_CODED0x0004) ) continue;
161158 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
161159 assert( pWInfo->untestedTerms )((void) (0));
161160 continue;
161161 }
161162 if( pTabItem->fg.jointype & JT_LTORJ0x40 ) continue;
161163 assert( pTerm->pExpr )((void) (0));
161164 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
161165 pTerm->wtFlags |= TERM_CODED0x0004;
161166 }
161167 }
161168
161169#if WHERETRACE_ENABLED /* 0x4001 */
161170 if( sqlite3WhereTrace & 0x4000 ){
161171 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
161172 iLevel);
161173 sqlite3WhereClausePrint(pWC);
161174 }
161175 if( sqlite3WhereTrace & 0x1 ){
161176 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
161177 iLevel, (u64)pLevel->notReady);
161178 }
161179#endif
161180 return pLevel->notReady;
161181}
161182
161183/*
161184** Generate the code for the loop that finds all non-matched terms
161185** for a RIGHT JOIN.
161186*/
161187SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) void sqlite3WhereRightJoinLoop(
161188 WhereInfo *pWInfo,
161189 int iLevel,
161190 WhereLevel *pLevel
161191){
161192 Parse *pParse = pWInfo->pParse;
161193 Vdbe *v = pParse->pVdbe;
161194 WhereRightJoin *pRJ = pLevel->pRJ;
161195 Expr *pSubWhere = 0;
161196 WhereClause *pWC = &pWInfo->sWC;
161197 WhereInfo *pSubWInfo;
161198 WhereLoop *pLoop = pLevel->pWLoop;
161199 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
161200 SrcList sFrom;
161201 Bitmask mAll = 0;
161202 int k;
161203
161204 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pSTab->zName))sqlite3VdbeExplain (pParse, 1, "RIGHT-JOIN %s", pTabItem->
pSTab->zName)
;
161205 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
161206 pRJ->regReturn);
161207 for(k=0; k<iLevel; k++){
161208 int iIdxCur;
161209 SrcItem *pRight;
161210 assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom )((void) (0));
161211 pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom];
161212 mAll |= pWInfo->a[k].pWLoop->maskSelf;
161213 if( pRight->fg.viaCoroutine ){
161214 Subquery *pSubq;
161215 assert( pRight->fg.isSubquery && pRight->u4.pSubq!=0 )((void) (0));
161216 pSubq = pRight->u4.pSubq;
161217 assert( pSubq->pSelect!=0 && pSubq->pSelect->pEList!=0 )((void) (0));
161218 sqlite3VdbeAddOp3(
161219 v, OP_Null75, 0, pSubq->regResult,
161220 pSubq->regResult + pSubq->pSelect->pEList->nExpr-1
161221 );
161222 }
161223 sqlite3VdbeAddOp1(v, OP_NullRow136, pWInfo->a[k].iTabCur);
161224 iIdxCur = pWInfo->a[k].iIdxCur;
161225 if( iIdxCur ){
161226 sqlite3VdbeAddOp1(v, OP_NullRow136, iIdxCur);
161227 }
161228 }
161229 if( (pTabItem->fg.jointype & JT_LTORJ0x40)==0 ){
161230 mAll |= pLoop->maskSelf;
161231 for(k=0; k<pWC->nTerm; k++){
161232 WhereTerm *pTerm = &pWC->a[k];
161233 if( (pTerm->wtFlags & (TERM_VIRTUAL0x0002|TERM_SLICE0x8000))!=0
161234 && pTerm->eOperator!=WO_ROWVAL0x2000
161235 ){
161236 break;
161237 }
161238 if( pTerm->prereqAll & ~mAll ) continue;
161239 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(0x000001|0x000002))!=0) ) continue;
161240 pSubWhere = sqlite3ExprAnd(pParse, pSubWhere,
161241 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
161242 }
161243 }
161244 sFrom.nSrc = 1;
161245 sFrom.nAlloc = 1;
161246 memcpy(&sFrom.a[0], pTabItem, sizeof(SrcItem));
161247 sFrom.a[0].fg.jointype = 0;
161248 assert( pParse->withinRJSubrtn < 100 )((void) (0));
161249 pParse->withinRJSubrtn++;
161250 pSubWInfo = sqlite3WhereBegin(pParse, &sFrom, pSubWhere, 0, 0, 0,
161251 WHERE_RIGHT_JOIN0x1000, 0);
161252 if( pSubWInfo ){
161253 int iCur = pLevel->iTabCur;
161254 int r = ++pParse->nMem;
161255 int nPk;
161256 int jmp;
161257 int addrCont = sqlite3WhereContinueLabel(pSubWInfo);
161258 Table *pTab = pTabItem->pSTab;
161259 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
161260 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
161261 nPk = 1;
161262 }else{
161263 int iPk;
161264 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
161265 nPk = pPk->nKeyCol;
161266 pParse->nMem += nPk - 1;
161267 for(iPk=0; iPk<nPk; iPk++){
161268 int iCol = pPk->aiColumn[iPk];
161269 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
161270 }
161271 }
161272 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter64, pRJ->regBloom, 0, r, nPk);
161273 VdbeCoverage(v);
161274 sqlite3VdbeAddOp4Int(v, OP_Found29, pRJ->iMatch, addrCont, r, nPk);
161275 VdbeCoverage(v);
161276 sqlite3VdbeJumpHere(v, jmp);
161277 sqlite3VdbeAddOp2(v, OP_Gosub10, pRJ->regReturn, pRJ->addrSubrtn);
161278 sqlite3WhereEnd(pSubWInfo);
161279 }
161280 sqlite3ExprDelete(pParse->db, pSubWhere);
161281 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
161282 assert( pParse->withinRJSubrtn>0 )((void) (0));
161283 pParse->withinRJSubrtn--;
161284}
161285
161286/************** End of wherecode.c *******************************************/
161287/************** Begin file whereexpr.c ***************************************/
161288/*
161289** 2015-06-08
161290**
161291** The author disclaims copyright to this source code. In place of
161292** a legal notice, here is a blessing:
161293**
161294** May you do good and not evil.
161295** May you find forgiveness for yourself and forgive others.
161296** May you share freely, never taking more than you give.
161297**
161298*************************************************************************
161299** This module contains C code that generates VDBE code used to process
161300** the WHERE clause of SQL statements.
161301**
161302** This file was originally part of where.c but was split out to improve
161303** readability and editability. This file contains utility routines for
161304** analyzing Expr objects in the WHERE clause.
161305*/
161306/* #include "sqliteInt.h" */
161307/* #include "whereInt.h" */
161308
161309/* Forward declarations */
161310static void exprAnalyze(SrcList*, WhereClause*, int);
161311
161312/*
161313** Deallocate all memory associated with a WhereOrInfo object.
161314*/
161315static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
161316 sqlite3WhereClauseClear(&p->wc);
161317 sqlite3DbFree(db, p);
161318}
161319
161320/*
161321** Deallocate all memory associated with a WhereAndInfo object.
161322*/
161323static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
161324 sqlite3WhereClauseClear(&p->wc);
161325 sqlite3DbFree(db, p);
161326}
161327
161328/*
161329** Add a single new WhereTerm entry to the WhereClause object pWC.
161330** The new WhereTerm object is constructed from Expr p and with wtFlags.
161331** The index in pWC->a[] of the new WhereTerm is returned on success.
161332** 0 is returned if the new WhereTerm could not be added due to a memory
161333** allocation error. The memory allocation failure will be recorded in
161334** the db->mallocFailed flag so that higher-level functions can detect it.
161335**
161336** This routine will increase the size of the pWC->a[] array as necessary.
161337**
161338** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
161339** for freeing the expression p is assumed by the WhereClause object pWC.
161340** This is true even if this routine fails to allocate a new WhereTerm.
161341**
161342** WARNING: This routine might reallocate the space used to store
161343** WhereTerms. All pointers to WhereTerms should be invalidated after
161344** calling this routine. Such pointers may be reinitialized by referencing
161345** the pWC->a[] array.
161346*/
161347static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
161348 WhereTerm *pTerm;
161349 int idx;
161350 testcase( wtFlags & TERM_VIRTUAL );
161351 if( pWC->nTerm>=pWC->nSlot ){
161352 WhereTerm *pOld = pWC->a;
161353 sqlite3 *db = pWC->pWInfo->pParse->db;
161354 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
161355 if( pWC->a==0 ){
161356 if( wtFlags & TERM_DYNAMIC0x0001 ){
161357 sqlite3ExprDelete(db, p);
161358 }
161359 pWC->a = pOld;
161360 return 0;
161361 }
161362 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
161363 pWC->nSlot = pWC->nSlot*2;
161364 }
161365 pTerm = &pWC->a[idx = pWC->nTerm++];
161366 if( (wtFlags & TERM_VIRTUAL0x0002)==0 ) pWC->nBase = pWC->nTerm;
161367 if( p && ExprHasProperty(p, EP_Unlikely)(((p)->flags&(0x080000))!=0) ){
161368 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
161369 }else{
161370 pTerm->truthProb = 1;
161371 }
161372 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
161373 pTerm->wtFlags = wtFlags;
161374 pTerm->pWC = pWC;
161375 pTerm->iParent = -1;
161376 memset(&pTerm->eOperator, 0,
161377 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)__builtin_offsetof(WhereTerm, eOperator));
161378 return idx;
161379}
161380
161381/*
161382** Return TRUE if the given operator is one of the operators that is
161383** allowed for an indexable WHERE clause term. The allowed operators are
161384** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
161385*/
161386static int allowedOp(int op){
161387 assert( TK_GT>TK_EQ && TK_GT<TK_GE )((void) (0));
161388 assert( TK_LT>TK_EQ && TK_LT<TK_GE )((void) (0));
161389 assert( TK_LE>TK_EQ && TK_LE<TK_GE )((void) (0));
161390 assert( TK_GE==TK_EQ+4 )((void) (0));
161391 assert( TK_IN<TK_EQ )((void) (0));
161392 assert( TK_IS<TK_EQ )((void) (0));
161393 assert( TK_ISNULL<TK_EQ )((void) (0));
161394 if( op>TK_GE58 ) return 0;
161395 if( op>=TK_EQ54 ) return 1;
161396 return op==TK_IN50 || op==TK_ISNULL51 || op==TK_IS45;
161397}
161398
161399/*
161400** Commute a comparison operator. Expressions of the form "X op Y"
161401** are converted into "Y op X".
161402*/
161403static u16 exprCommute(Parse *pParse, Expr *pExpr){
161404 if( pExpr->pLeft->op==TK_VECTOR177
161405 || pExpr->pRight->op==TK_VECTOR177
161406 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
161407 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
161408 ){
161409 pExpr->flags ^= EP_Commuted0x000400;
161410 }
161411 SWAP(Expr*,pExpr->pRight,pExpr->pLeft){Expr* t=pExpr->pRight; pExpr->pRight=pExpr->pLeft; pExpr
->pLeft=t;}
;
161412 if( pExpr->op>=TK_GT55 ){
161413 assert( TK_LT==TK_GT+2 )((void) (0));
161414 assert( TK_GE==TK_LE+2 )((void) (0));
161415 assert( TK_GT>TK_EQ )((void) (0));
161416 assert( TK_GT<TK_LE )((void) (0));
161417 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE )((void) (0));
161418 pExpr->op = ((pExpr->op-TK_GT55)^2)+TK_GT55;
161419 }
161420 return 0;
161421}
161422
161423/*
161424** Translate from TK_xx operator to WO_xx bitmask.
161425*/
161426static u16 operatorMask(int op){
161427 u16 c;
161428 assert( allowedOp(op) )((void) (0));
161429 if( op>=TK_EQ54 ){
161430 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff )((void) (0));
161431 c = (u16)(WO_EQ0x0002<<(op-TK_EQ54));
161432 }else if( op==TK_IN50 ){
161433 c = WO_IN0x0001;
161434 }else if( op==TK_ISNULL51 ){
161435 c = WO_ISNULL0x0100;
161436 }else{
161437 assert( op==TK_IS )((void) (0));
161438 c = WO_IS0x0080;
161439 }
161440 assert( op!=TK_ISNULL || c==WO_ISNULL )((void) (0));
161441 assert( op!=TK_IN || c==WO_IN )((void) (0));
161442 assert( op!=TK_EQ || c==WO_EQ )((void) (0));
161443 assert( op!=TK_LT || c==WO_LT )((void) (0));
161444 assert( op!=TK_LE || c==WO_LE )((void) (0));
161445 assert( op!=TK_GT || c==WO_GT )((void) (0));
161446 assert( op!=TK_GE || c==WO_GE )((void) (0));
161447 assert( op!=TK_IS || c==WO_IS )((void) (0));
161448 return c;
161449}
161450
161451
161452#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
161453/*
161454** Check to see if the given expression is a LIKE or GLOB operator that
161455** can be optimized using inequality constraints. Return TRUE if it is
161456** so and false if not.
161457**
161458** In order for the operator to be optimizible, the RHS must be a string
161459** literal that does not begin with a wildcard. The LHS must be a column
161460** that may only be NULL, a string, or a BLOB, never a number. (This means
161461** that virtual tables cannot participate in the LIKE optimization.) The
161462** collating sequence for the column on the LHS must be appropriate for
161463** the operator.
161464*/
161465static int isLikeOrGlob(
161466 Parse *pParse, /* Parsing and code generating context */
161467 Expr *pExpr, /* Test this expression */
161468 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
161469 int *pisComplete, /* True if the only wildcard is % in the last character */
161470 int *pnoCase /* True if uppercase is equivalent to lowercase */
161471){
161472 const u8 *z = 0; /* String on RHS of LIKE operator */
161473 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
161474 ExprList *pList; /* List of operands to the LIKE operator */
161475 u8 c; /* One character in z[] */
161476 int cnt; /* Number of non-wildcard prefix characters */
161477 u8 wc[4]; /* Wildcard characters */
161478 sqlite3 *db = pParse->db; /* Database connection */
161479 sqlite3_value *pVal = 0;
161480 int op; /* Opcode of pRight */
161481 int rc; /* Result code to return */
161482
161483 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
161484 return 0;
161485 }
161486#ifdef SQLITE_EBCDIC
161487 if( *pnoCase ) return 0;
161488#endif
161489 assert( ExprUseXList(pExpr) )((void) (0));
161490 pList = pExpr->x.pList;
161491 pLeft = pList->a[1].pExpr;
161492
161493 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
161494 op = pRight->op;
161495 if( op==TK_VARIABLE157 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
161496 Vdbe *pReprepare = pParse->pReprepare;
161497 int iCol = pRight->iColumn;
161498 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB0x41);
161499 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
161500 z = sqlite3_value_text(pVal);
161501 }
161502 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
161503 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER )((void) (0));
161504 }else if( op==TK_STRING118 ){
161505 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
161506 z = (u8*)pRight->u.zToken;
161507 }
161508 if( z ){
161509 /* Count the number of prefix bytes prior to the first wildcard.
161510 ** or U+fffd character. If the underlying database has a UTF16LE
161511 ** encoding, then only consider ASCII characters. Note that the
161512 ** encoding of z[] is UTF8 - we are dealing with only UTF8 here in
161513 ** this code, but the database engine itself might be processing
161514 ** content using a different encoding. */
161515 cnt = 0;
161516 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
161517 cnt++;
161518 if( c==wc[3] && z[cnt]>0 && z[cnt]<0x80 ){
161519 cnt++;
161520 }else if( c>=0x80 ){
161521 const u8 *z2 = z+cnt-1;
161522 if( sqlite3Utf8Read(&z2)==0xfffd || ENC(db)((db)->enc)==SQLITE_UTF16LE2 ){
161523 cnt--;
161524 break;
161525 }else{
161526 cnt = (int)(z2-z);
161527 }
161528 }
161529 }
161530
161531 /* The optimization is possible only if (1) the pattern does not begin
161532 ** with a wildcard and if (2) the non-wildcard prefix does not end with
161533 ** an (illegal 0xff) character, or (3) the pattern does not consist of
161534 ** a single escape character. The second condition is necessary so
161535 ** that we can increment the prefix key to find an upper bound for the
161536 ** range search. The third is because the caller assumes that the pattern
161537 ** consists of at least one character after all escapes have been
161538 ** removed. */
161539 if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && ALWAYS(255!=(u8)z[cnt-1])(255!=(u8)z[cnt-1]) ){
161540 Expr *pPrefix;
161541
161542 /* A "complete" match if the pattern ends with "*" or "%" */
161543 *pisComplete = c==wc[0] && z[cnt+1]==0 && ENC(db)((db)->enc)!=SQLITE_UTF16LE2;
161544
161545 /* Get the pattern prefix. Remove all escapes from the prefix. */
161546 pPrefix = sqlite3Expr(db, TK_STRING118, (char*)z);
161547 if( pPrefix ){
161548 int iFrom, iTo;
161549 char *zNew;
161550 assert( !ExprHasProperty(pPrefix, EP_IntValue) )((void) (0));
161551 zNew = pPrefix->u.zToken;
161552 zNew[cnt] = 0;
161553 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
161554 if( zNew[iFrom]==wc[3] ) iFrom++;
161555 zNew[iTo++] = zNew[iFrom];
161556 }
161557 zNew[iTo] = 0;
161558 assert( iTo>0 )((void) (0));
161559
161560 /* If the LHS is not an ordinary column with TEXT affinity, then the
161561 ** pattern prefix boundaries (both the start and end boundaries) must
161562 ** not look like a number. Otherwise the pattern might be treated as
161563 ** a number, which will invalidate the LIKE optimization.
161564 **
161565 ** Getting this right has been a persistent source of bugs in the
161566 ** LIKE optimization. See, for example:
161567 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
161568 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
161569 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
161570 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
161571 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
161572 */
161573 if( pLeft->op!=TK_COLUMN168
161574 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT0x42
161575 || (ALWAYS( ExprUseYTab(pLeft) )((((pLeft)->flags&(0x1000000|0x2000000))==0))
161576 && ALWAYS(pLeft->y.pTab)(pLeft->y.pTab)
161577 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->eTabType==1)) /* Might be numeric */
161578 ){
161579 int isNum;
161580 double rDummy;
161581 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
161582 if( isNum<=0 ){
161583 if( iTo==1 && zNew[0]=='-' ){
161584 isNum = +1;
161585 }else{
161586 zNew[iTo-1]++;
161587 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
161588 zNew[iTo-1]--;
161589 }
161590 }
161591 if( isNum>0 ){
161592 sqlite3ExprDelete(db, pPrefix);
161593 sqlite3ValueFree(pVal);
161594 return 0;
161595 }
161596 }
161597 }
161598 *ppPrefix = pPrefix;
161599
161600 /* If the RHS pattern is a bound parameter, make arrangements to
161601 ** reprepare the statement when that parameter is rebound */
161602 if( op==TK_VARIABLE157 ){
161603 Vdbe *v = pParse->pVdbe;
161604 sqlite3VdbeSetVarmask(v, pRight->iColumn);
161605 assert( !ExprHasProperty(pRight, EP_IntValue) )((void) (0));
161606 if( *pisComplete && pRight->u.zToken[1] ){
161607 /* If the rhs of the LIKE expression is a variable, and the current
161608 ** value of the variable means there is no need to invoke the LIKE
161609 ** function, then no OP_Variable will be added to the program.
161610 ** This causes problems for the sqlite3_bind_parameter_name()
161611 ** API. To work around them, add a dummy OP_Variable here.
161612 */
161613 int r1 = sqlite3GetTempReg(pParse);
161614 sqlite3ExprCodeTarget(pParse, pRight, r1);
161615 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
161616 sqlite3ReleaseTempReg(pParse, r1);
161617 }
161618 }
161619 }else{
161620 z = 0;
161621 }
161622 }
161623
161624 rc = (z!=0);
161625 sqlite3ValueFree(pVal);
161626 return rc;
161627}
161628#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
161629
161630
161631#ifndef SQLITE_OMIT_VIRTUALTABLE
161632/*
161633** Check to see if the pExpr expression is a form that needs to be passed
161634** to the xBestIndex method of virtual tables. Forms of interest include:
161635**
161636** Expression Virtual Table Operator
161637** ----------------------- ---------------------------------
161638** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
161639** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
161640** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
161641** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
161642** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
161643** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
161644** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
161645** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
161646** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
161647**
161648** In every case, "column" must be a column of a virtual table. If there
161649** is a match, set *ppLeft to the "column" expression, set *ppRight to the
161650** "expr" expression (even though in forms (6) and (8) the column is on the
161651** right and the expression is on the left). Also set *peOp2 to the
161652** appropriate virtual table operator. The return value is 1 or 2 if there
161653** is a match. The usual return is 1, but if the RHS is also a column
161654** of virtual table in forms (5) or (7) then return 2.
161655**
161656** If the expression matches none of the patterns above, return 0.
161657*/
161658static int isAuxiliaryVtabOperator(
161659 sqlite3 *db, /* Parsing context */
161660 Expr *pExpr, /* Test this expression */
161661 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
161662 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
161663 Expr **ppRight /* Expression to left of MATCH/op2 */
161664){
161665 if( pExpr->op==TK_FUNCTION172 ){
161666 static const struct Op2 {
161667 const char *zOp;
161668 unsigned char eOp2;
161669 } aOp[] = {
161670 { "match", SQLITE_INDEX_CONSTRAINT_MATCH64 },
161671 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB66 },
161672 { "like", SQLITE_INDEX_CONSTRAINT_LIKE65 },
161673 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP67 }
161674 };
161675 ExprList *pList;
161676 Expr *pCol; /* Column reference */
161677 int i;
161678
161679 assert( ExprUseXList(pExpr) )((void) (0));
161680 pList = pExpr->x.pList;
161681 if( pList==0 || pList->nExpr!=2 ){
161682 return 0;
161683 }
161684
161685 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
161686 ** virtual table on their second argument, which is the same as
161687 ** the left-hand side operand in their in-fix form.
161688 **
161689 ** vtab_column MATCH expression
161690 ** MATCH(expression,vtab_column)
161691 */
161692 pCol = pList->a[1].pExpr;
161693 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) )((void) (0));
161694 if( ExprIsVtab(pCol)((pCol)->op==168 && (pCol)->y.pTab->eTabType
==1)
){
161695 for(i=0; i<ArraySize(aOp)((int)(sizeof(aOp)/sizeof(aOp[0]))); i++){
161696 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
161697 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
161698 *peOp2 = aOp[i].eOp2;
161699 *ppRight = pList->a[0].pExpr;
161700 *ppLeft = pCol;
161701 return 1;
161702 }
161703 }
161704 }
161705
161706 /* We can also match against the first column of overloaded
161707 ** functions where xFindFunction returns a value of at least
161708 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
161709 **
161710 ** OVERLOADED(vtab_column,expression)
161711 **
161712 ** Historically, xFindFunction expected to see lower-case function
161713 ** names. But for this use case, xFindFunction is expected to deal
161714 ** with function names in an arbitrary case.
161715 */
161716 pCol = pList->a[0].pExpr;
161717 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) )((void) (0));
161718 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) )((void) (0));
161719 if( ExprIsVtab(pCol)((pCol)->op==168 && (pCol)->y.pTab->eTabType
==1)
){
161720 sqlite3_vtab *pVtab;
161721 sqlite3_module *pMod;
161722 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
161723 void *pNotUsed;
161724 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
161725 assert( pVtab!=0 )((void) (0));
161726 assert( pVtab->pModule!=0 )((void) (0));
161727 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
161728 pMod = (sqlite3_module *)pVtab->pModule;
161729 if( pMod->xFindFunction!=0 ){
161730 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
161731 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
161732 *peOp2 = i;
161733 *ppRight = pList->a[1].pExpr;
161734 *ppLeft = pCol;
161735 return 1;
161736 }
161737 }
161738 }
161739 }else if( pExpr->op>=TK_EQ54 ){
161740 /* Comparison operators are a common case. Save a few comparisons for
161741 ** that common case by terminating early. */
161742 assert( TK_NE < TK_EQ )((void) (0));
161743 assert( TK_ISNOT < TK_EQ )((void) (0));
161744 assert( TK_NOTNULL < TK_EQ )((void) (0));
161745 return 0;
161746 }else if( pExpr->op==TK_NE53 || pExpr->op==TK_ISNOT46 || pExpr->op==TK_NOTNULL52 ){
161747 int res = 0;
161748 Expr *pLeft = pExpr->pLeft;
161749 Expr *pRight = pExpr->pRight;
161750 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) )((void) (0));
161751 if( ExprIsVtab(pLeft)((pLeft)->op==168 && (pLeft)->y.pTab->eTabType
==1)
){
161752 res++;
161753 }
161754 assert( pRight==0 || pRight->op!=TK_COLUMN((void) (0))
161755 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) )((void) (0));
161756 if( pRight && ExprIsVtab(pRight)((pRight)->op==168 && (pRight)->y.pTab->eTabType
==1)
){
161757 res++;
161758 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
161759 }
161760 *ppLeft = pLeft;
161761 *ppRight = pRight;
161762 if( pExpr->op==TK_NE53 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE68;
161763 if( pExpr->op==TK_ISNOT46 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT69;
161764 if( pExpr->op==TK_NOTNULL52 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL70;
161765 return res;
161766 }
161767 return 0;
161768}
161769#endif /* SQLITE_OMIT_VIRTUALTABLE */
161770
161771/*
161772** If the pBase expression originated in the ON or USING clause of
161773** a join, then transfer the appropriate markings over to derived.
161774*/
161775static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
161776 if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON)(((pBase)->flags&(0x000001|0x000002))!=0) ){
161777 pDerived->flags |= pBase->flags & (EP_OuterON0x000001|EP_InnerON0x000002);
161778 pDerived->w.iJoin = pBase->w.iJoin;
161779 }
161780}
161781
161782/*
161783** Mark term iChild as being a child of term iParent
161784*/
161785static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
161786 pWC->a[iChild].iParent = iParent;
161787 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
161788 pWC->a[iParent].nChild++;
161789}
161790
161791/*
161792** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
161793** a conjunction, then return just pTerm when N==0. If N is exceeds
161794** the number of available subterms, return NULL.
161795*/
161796static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
161797 if( pTerm->eOperator!=WO_AND0x0400 ){
161798 return N==0 ? pTerm : 0;
161799 }
161800 if( N<pTerm->u.pAndInfo->wc.nTerm ){
161801 return &pTerm->u.pAndInfo->wc.a[N];
161802 }
161803 return 0;
161804}
161805
161806/*
161807** Subterms pOne and pTwo are contained within WHERE clause pWC. The
161808** two subterms are in disjunction - they are OR-ed together.
161809**
161810** If these two terms are both of the form: "A op B" with the same
161811** A and B values but different operators and if the operators are
161812** compatible (if one is = and the other is <, for example) then
161813** add a new virtual AND term to pWC that is the combination of the
161814** two.
161815**
161816** Some examples:
161817**
161818** x<y OR x=y --> x<=y
161819** x=y OR x=y --> x=y
161820** x<=y OR x<y --> x<=y
161821**
161822** The following is NOT generated:
161823**
161824** x<y OR x>y --> x!=y
161825*/
161826static void whereCombineDisjuncts(
161827 SrcList *pSrc, /* the FROM clause */
161828 WhereClause *pWC, /* The complete WHERE clause */
161829 WhereTerm *pOne, /* First disjunct */
161830 WhereTerm *pTwo /* Second disjunct */
161831){
161832 u16 eOp = pOne->eOperator | pTwo->eOperator;
161833 sqlite3 *db; /* Database connection (for malloc) */
161834 Expr *pNew; /* New virtual expression */
161835 int op; /* Operator for the combined expression */
161836 int idxNew; /* Index in pWC of the next virtual term */
161837
161838 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL0x0080 ) return;
161839 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;
161840 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;
161841 if( (eOp & (WO_EQ0x0002|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))))!=eOp
161842 && (eOp & (WO_EQ0x0002|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))))!=eOp ) return;
161843 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 )((void) (0));
161844 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 )((void) (0));
161845 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
161846 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
161847 /* If we reach this point, it means the two subterms can be combined */
161848 if( (eOp & (eOp-1))!=0 ){
161849 if( eOp & (WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))) ){
161850 eOp = WO_LE(0x0002<<(56 -54));
161851 }else{
161852 assert( eOp & (WO_GT|WO_GE) )((void) (0));
161853 eOp = WO_GE(0x0002<<(58 -54));
161854 }
161855 }
161856 db = pWC->pWInfo->pParse->db;
161857 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
161858 if( pNew==0 ) return;
161859 for(op=TK_EQ54; eOp!=(WO_EQ0x0002<<(op-TK_EQ54)); op++){ assert( op<TK_GE )((void) (0)); }
161860 pNew->op = op;
161861 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
161862 exprAnalyze(pSrc, pWC, idxNew);
161863}
161864
161865#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
161866/*
161867** Analyze a term that consists of two or more OR-connected
161868** subterms. So in:
161869**
161870** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
161871** ^^^^^^^^^^^^^^^^^^^^
161872**
161873** This routine analyzes terms such as the middle term in the above example.
161874** A WhereOrTerm object is computed and attached to the term under
161875** analysis, regardless of the outcome of the analysis. Hence:
161876**
161877** WhereTerm.wtFlags |= TERM_ORINFO
161878** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
161879**
161880** The term being analyzed must have two or more of OR-connected subterms.
161881** A single subterm might be a set of AND-connected sub-subterms.
161882** Examples of terms under analysis:
161883**
161884** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
161885** (B) x=expr1 OR expr2=x OR x=expr3
161886** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
161887** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
161888** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
161889** (F) x>A OR (x=A AND y>=B)
161890**
161891** CASE 1:
161892**
161893** If all subterms are of the form T.C=expr for some single column of C and
161894** a single table T (as shown in example B above) then create a new virtual
161895** term that is an equivalent IN expression. In other words, if the term
161896** being analyzed is:
161897**
161898** x = expr1 OR expr2 = x OR x = expr3
161899**
161900** then create a new virtual term like this:
161901**
161902** x IN (expr1,expr2,expr3)
161903**
161904** CASE 2:
161905**
161906** If there are exactly two disjuncts and one side has x>A and the other side
161907** has x=A (for the same x and A) then add a new virtual conjunct term to the
161908** WHERE clause of the form "x>=A". Example:
161909**
161910** x>A OR (x=A AND y>B) adds: x>=A
161911**
161912** The added conjunct can sometimes be helpful in query planning.
161913**
161914** CASE 3:
161915**
161916** If all subterms are indexable by a single table T, then set
161917**
161918** WhereTerm.eOperator = WO_OR
161919** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
161920**
161921** A subterm is "indexable" if it is of the form
161922** "T.C <op> <expr>" where C is any column of table T and
161923** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
161924** A subterm is also indexable if it is an AND of two or more
161925** subsubterms at least one of which is indexable. Indexable AND
161926** subterms have their eOperator set to WO_AND and they have
161927** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
161928**
161929** From another point of view, "indexable" means that the subterm could
161930** potentially be used with an index if an appropriate index exists.
161931** This analysis does not consider whether or not the index exists; that
161932** is decided elsewhere. This analysis only looks at whether subterms
161933** appropriate for indexing exist.
161934**
161935** All examples A through E above satisfy case 3. But if a term
161936** also satisfies case 1 (such as B) we know that the optimizer will
161937** always prefer case 1, so in that case we pretend that case 3 is not
161938** satisfied.
161939**
161940** It might be the case that multiple tables are indexable. For example,
161941** (E) above is indexable on tables P, Q, and R.
161942**
161943** Terms that satisfy case 3 are candidates for lookup by using
161944** separate indices to find rowids for each subterm and composing
161945** the union of all rowids using a RowSet object. This is similar
161946** to "bitmap indices" in other database engines.
161947**
161948** OTHERWISE:
161949**
161950** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
161951** zero. This term is not useful for search.
161952*/
161953static void exprAnalyzeOrTerm(
161954 SrcList *pSrc, /* the FROM clause */
161955 WhereClause *pWC, /* the complete WHERE clause */
161956 int idxTerm /* Index of the OR-term to be analyzed */
161957){
161958 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
161959 Parse *pParse = pWInfo->pParse; /* Parser context */
161960 sqlite3 *db = pParse->db; /* Database connection */
161961 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
161962 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
161963 int i; /* Loop counters */
161964 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
161965 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
161966 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
161967 Bitmask chngToIN; /* Tables that might satisfy case 1 */
161968 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
161969
161970 /*
161971 ** Break the OR clause into its separate subterms. The subterms are
161972 ** stored in a WhereClause structure containing within the WhereOrInfo
161973 ** object that is attached to the original OR clause term.
161974 */
161975 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 )((void) (0));
161976 assert( pExpr->op==TK_OR )((void) (0));
161977 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
161978 if( pOrInfo==0 ) return;
161979 pTerm->wtFlags |= TERM_ORINFO0x0010;
161980 pOrWc = &pOrInfo->wc;
161981 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
161982 sqlite3WhereClauseInit(pOrWc, pWInfo);
161983 sqlite3WhereSplit(pOrWc, pExpr, TK_OR43);
161984 sqlite3WhereExprAnalyze(pSrc, pOrWc);
161985 if( db->mallocFailed ) return;
161986 assert( pOrWc->nTerm>=2 )((void) (0));
161987
161988 /*
161989 ** Compute the set of tables that might satisfy cases 1 or 3.
161990 */
161991 indexable = ~(Bitmask)0;
161992 chngToIN = ~(Bitmask)0;
161993 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
161994 if( (pOrTerm->eOperator & WO_SINGLE0x01ff)==0 ){
161995 WhereAndInfo *pAndInfo;
161996 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 )((void) (0));
161997 chngToIN = 0;
161998 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
161999 if( pAndInfo ){
162000 WhereClause *pAndWC;
162001 WhereTerm *pAndTerm;
162002 int j;
162003 Bitmask b = 0;
162004 pOrTerm->u.pAndInfo = pAndInfo;
162005 pOrTerm->wtFlags |= TERM_ANDINFO0x0020;
162006 pOrTerm->eOperator = WO_AND0x0400;
162007 pOrTerm->leftCursor = -1;
162008 pAndWC = &pAndInfo->wc;
162009 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
162010 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
162011 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND44);
162012 sqlite3WhereExprAnalyze(pSrc, pAndWC);
162013 pAndWC->pOuter = pWC;
162014 if( !db->mallocFailed ){
162015 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
162016 assert( pAndTerm->pExpr )((void) (0));
162017 if( allowedOp(pAndTerm->pExpr->op)
162018 || pAndTerm->eOperator==WO_AUX0x0040
162019 ){
162020 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
162021 }
162022 }
162023 }
162024 indexable &= b;
162025 }
162026 }else if( pOrTerm->wtFlags & TERM_COPIED0x0008 ){
162027 /* Skip this term for now. We revisit it when we process the
162028 ** corresponding TERM_VIRTUAL term */
162029 }else{
162030 Bitmask b;
162031 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
162032 if( pOrTerm->wtFlags & TERM_VIRTUAL0x0002 ){
162033 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
162034 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
162035 }
162036 indexable &= b;
162037 if( (pOrTerm->eOperator & WO_EQ0x0002)==0 ){
162038 chngToIN = 0;
162039 }else{
162040 chngToIN &= b;
162041 }
162042 }
162043 }
162044
162045 /*
162046 ** Record the set of tables that satisfy case 3. The set might be
162047 ** empty.
162048 */
162049 pOrInfo->indexable = indexable;
162050 pTerm->eOperator = WO_OR0x0200;
162051 pTerm->leftCursor = -1;
162052 if( indexable ){
162053 pWC->hasOr = 1;
162054 }
162055
162056 /* For a two-way OR, attempt to implementation case 2.
162057 */
162058 if( indexable && pOrWc->nTerm==2 ){
162059 int iOne = 0;
162060 WhereTerm *pOne;
162061 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
162062 int iTwo = 0;
162063 WhereTerm *pTwo;
162064 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
162065 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
162066 }
162067 }
162068 }
162069
162070 /*
162071 ** chngToIN holds a set of tables that *might* satisfy case 1. But
162072 ** we have to do some additional checking to see if case 1 really
162073 ** is satisfied.
162074 **
162075 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
162076 ** that there is no possibility of transforming the OR clause into an
162077 ** IN operator because one or more terms in the OR clause contain
162078 ** something other than == on a column in the single table. The 1-bit
162079 ** case means that every term of the OR clause is of the form
162080 ** "table.column=expr" for some single table. The one bit that is set
162081 ** will correspond to the common table. We still need to check to make
162082 ** sure the same column is used on all terms. The 2-bit case is when
162083 ** the all terms are of the form "table1.column=table2.column". It
162084 ** might be possible to form an IN operator with either table1.column
162085 ** or table2.column as the LHS if either is common to every term of
162086 ** the OR clause.
162087 **
162088 ** Note that terms of the form "table.column1=table.column2" (the
162089 ** same table on both sizes of the ==) cannot be optimized.
162090 */
162091 if( chngToIN ){
162092 int okToChngToIN = 0; /* True if the conversion to IN is valid */
162093 int iColumn = -1; /* Column index on lhs of IN operator */
162094 int iCursor = -1; /* Table cursor common to all terms */
162095 int j = 0; /* Loop counter */
162096
162097 /* Search for a table and column that appears on one side or the
162098 ** other of the == operator in every subterm. That table and column
162099 ** will be recorded in iCursor and iColumn. There might not be any
162100 ** such table and column. Set okToChngToIN if an appropriate table
162101 ** and column is found but leave okToChngToIN false if not found.
162102 */
162103 for(j=0; j<2 && !okToChngToIN; j++){
162104 Expr *pLeft = 0;
162105 pOrTerm = pOrWc->a;
162106 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
162107 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
162108 pOrTerm->wtFlags &= ~TERM_OK0x0040;
162109 if( pOrTerm->leftCursor==iCursor ){
162110 /* This is the 2-bit case and we are on the second iteration and
162111 ** current term is from the first iteration. So skip this term. */
162112 assert( j==1 )((void) (0));
162113 continue;
162114 }
162115 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
162116 pOrTerm->leftCursor))==0 ){
162117 /* This term must be of the form t1.a==t2.b where t2 is in the
162118 ** chngToIN set but t1 is not. This term will be either preceded
162119 ** or followed by an inverted copy (t2.b==t1.a). Skip this term
162120 ** and use its inversion. */
162121 testcase( pOrTerm->wtFlags & TERM_COPIED );
162122 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
162123 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) )((void) (0));
162124 continue;
162125 }
162126 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
162127 iColumn = pOrTerm->u.x.leftColumn;
162128 iCursor = pOrTerm->leftCursor;
162129 pLeft = pOrTerm->pExpr->pLeft;
162130 break;
162131 }
162132 if( i<0 ){
162133 /* No candidate table+column was found. This can only occur
162134 ** on the second iteration */
162135 assert( j==1 )((void) (0));
162136 assert( IsPowerOfTwo(chngToIN) )((void) (0));
162137 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) )((void) (0));
162138 break;
162139 }
162140 testcase( j==1 );
162141
162142 /* We have found a candidate table and column. Check to see if that
162143 ** table and column is common to every term in the OR clause */
162144 okToChngToIN = 1;
162145 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
162146 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
162147 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
162148 if( pOrTerm->leftCursor!=iCursor ){
162149 pOrTerm->wtFlags &= ~TERM_OK0x0040;
162150 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR(-2)
162151 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
162152 )){
162153 okToChngToIN = 0;
162154 }else{
162155 int affLeft, affRight;
162156 /* If the right-hand side is also a column, then the affinities
162157 ** of both right and left sides must be such that no type
162158 ** conversions are required on the right. (Ticket #2249)
162159 */
162160 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
162161 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
162162 if( affRight!=0 && affRight!=affLeft ){
162163 okToChngToIN = 0;
162164 }else{
162165 pOrTerm->wtFlags |= TERM_OK0x0040;
162166 }
162167 }
162168 }
162169 }
162170
162171 /* At this point, okToChngToIN is true if original pTerm satisfies
162172 ** case 1. In that case, construct a new virtual term that is
162173 ** pTerm converted into an IN operator.
162174 */
162175 if( okToChngToIN ){
162176 Expr *pDup; /* A transient duplicate expression */
162177 ExprList *pList = 0; /* The RHS of the IN operator */
162178 Expr *pLeft = 0; /* The LHS of the IN operator */
162179 Expr *pNew; /* The complete IN operator */
162180
162181 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
162182 if( (pOrTerm->wtFlags & TERM_OK0x0040)==0 ) continue;
162183 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
162184 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
162185 assert( pOrTerm->leftCursor==iCursor )((void) (0));
162186 assert( pOrTerm->u.x.leftColumn==iColumn )((void) (0));
162187 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
162188 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
162189 pLeft = pOrTerm->pExpr->pLeft;
162190 }
162191 assert( pLeft!=0 )((void) (0));
162192 pDup = sqlite3ExprDup(db, pLeft, 0);
162193 pNew = sqlite3PExpr(pParse, TK_IN50, pDup, 0);
162194 if( pNew ){
162195 int idxNew;
162196 transferJoinMarkings(pNew, pExpr);
162197 assert( ExprUseXList(pNew) )((void) (0));
162198 pNew->x.pList = pList;
162199 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
162200 testcase( idxNew==0 );
162201 exprAnalyze(pSrc, pWC, idxNew);
162202 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
162203 markTermAsChild(pWC, idxNew, idxTerm);
162204 }else{
162205 sqlite3ExprListDelete(db, pList);
162206 }
162207 }
162208 }
162209}
162210#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
162211
162212/*
162213** We already know that pExpr is a binary operator where both operands are
162214** column references. This routine checks to see if pExpr is an equivalence
162215** relation:
162216** 1. The SQLITE_Transitive optimization must be enabled
162217** 2. Must be either an == or an IS operator
162218** 3. Not originating in the ON clause of an OUTER JOIN
162219** 4. The affinities of A and B must be compatible
162220** 5a. Both operands use the same collating sequence OR
162221** 5b. The overall collating sequence is BINARY
162222** If this routine returns TRUE, that means that the RHS can be substituted
162223** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
162224** This is an optimization. No harm comes from returning 0. But if 1 is
162225** returned when it should not be, then incorrect answers might result.
162226*/
162227static int termIsEquivalence(Parse *pParse, Expr *pExpr){
162228 char aff1, aff2;
162229 CollSeq *pColl;
162230 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive)(((pParse->db)->dbOptFlags&(0x00000080))==0) ) return 0;
162231 if( pExpr->op!=TK_EQ54 && pExpr->op!=TK_IS45 ) return 0;
162232 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ) return 0;
162233 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
162234 aff2 = sqlite3ExprAffinity(pExpr->pRight);
162235 if( aff1!=aff2
162236 && (!sqlite3IsNumericAffinity(aff1)((aff1)>=0x43) || !sqlite3IsNumericAffinity(aff2)((aff2)>=0x43))
162237 ){
162238 return 0;
162239 }
162240 pColl = sqlite3ExprCompareCollSeq(pParse, pExpr);
162241 if( sqlite3IsBinary(pColl) ) return 1;
162242 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
162243}
162244
162245/*
162246** Recursively walk the expressions of a SELECT statement and generate
162247** a bitmask indicating which tables are used in that expression
162248** tree.
162249*/
162250static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
162251 Bitmask mask = 0;
162252 while( pS ){
162253 SrcList *pSrc = pS->pSrc;
162254 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
162255 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
162256 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
162257 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
162258 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
162259 if( ALWAYS(pSrc!=0)(pSrc!=0) ){
162260 int i;
162261 for(i=0; i<pSrc->nSrc; i++){
162262 if( pSrc->a[i].fg.isSubquery ){
162263 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].u4.pSubq->pSelect);
162264 }
162265 if( pSrc->a[i].fg.isUsing==0 ){
162266 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
162267 }
162268 if( pSrc->a[i].fg.isTabFunc ){
162269 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
162270 }
162271 }
162272 }
162273 pS = pS->pPrior;
162274 }
162275 return mask;
162276}
162277
162278/*
162279** Expression pExpr is one operand of a comparison operator that might
162280** be useful for indexing. This routine checks to see if pExpr appears
162281** in any index. Return TRUE (1) if pExpr is an indexed term and return
162282** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
162283** number of the table that is indexed and aiCurCol[1] to the column number
162284** of the column that is indexed, or XN_EXPR (-2) if an expression is being
162285** indexed.
162286**
162287** If pExpr is a TK_COLUMN column reference, then this routine always returns
162288** true even if that particular column is not indexed, because the column
162289** might be added to an automatic index later.
162290*/
162291static SQLITE_NOINLINE__attribute__((noinline)) int exprMightBeIndexed2(
162292 SrcList *pFrom, /* The FROM clause */
162293 int *aiCurCol, /* Write the referenced table cursor and column here */
162294 Expr *pExpr, /* An operand of a comparison operator */
162295 int j /* Start looking with the j-th pFrom entry */
162296){
162297 Index *pIdx;
162298 int i;
162299 int iCur;
162300 do{
162301 iCur = pFrom->a[j].iCursor;
162302 for(pIdx=pFrom->a[j].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){
162303 if( pIdx->aColExpr==0 ) continue;
162304 for(i=0; i<pIdx->nKeyCol; i++){
162305 if( pIdx->aiColumn[i]!=XN_EXPR(-2) ) continue;
162306 assert( pIdx->bHasExpr )((void) (0));
162307 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
162308 && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr)
162309 ){
162310 aiCurCol[0] = iCur;
162311 aiCurCol[1] = XN_EXPR(-2);
162312 return 1;
162313 }
162314 }
162315 }
162316 }while( ++j < pFrom->nSrc );
162317 return 0;
162318}
162319static int exprMightBeIndexed(
162320 SrcList *pFrom, /* The FROM clause */
162321 int *aiCurCol, /* Write the referenced table cursor & column here */
162322 Expr *pExpr, /* An operand of a comparison operator */
162323 int op /* The specific comparison operator */
162324){
162325 int i;
162326
162327 /* If this expression is a vector to the left or right of a
162328 ** inequality constraint (>, <, >= or <=), perform the processing
162329 ** on the first element of the vector. */
162330 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE )((void) (0));
162331 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE )((void) (0));
162332 assert( op<=TK_GE )((void) (0));
162333 if( pExpr->op==TK_VECTOR177 && (op>=TK_GT55 && ALWAYS(op<=TK_GE)(op<=58)) ){
162334 assert( ExprUseXList(pExpr) )((void) (0));
162335 pExpr = pExpr->x.pList->a[0].pExpr;
162336 }
162337
162338 if( pExpr->op==TK_COLUMN168 ){
162339 aiCurCol[0] = pExpr->iTable;
162340 aiCurCol[1] = pExpr->iColumn;
162341 return 1;
162342 }
162343
162344 for(i=0; i<pFrom->nSrc; i++){
162345 Index *pIdx;
162346 for(pIdx=pFrom->a[i].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){
162347 if( pIdx->aColExpr ){
162348 return exprMightBeIndexed2(pFrom,aiCurCol,pExpr,i);
162349 }
162350 }
162351 }
162352 return 0;
162353}
162354
162355
162356/*
162357** The input to this routine is an WhereTerm structure with only the
162358** "pExpr" field filled in. The job of this routine is to analyze the
162359** subexpression and populate all the other fields of the WhereTerm
162360** structure.
162361**
162362** If the expression is of the form "<expr> <op> X" it gets commuted
162363** to the standard form of "X <op> <expr>".
162364**
162365** If the expression is of the form "X <op> Y" where both X and Y are
162366** columns, then the original expression is unchanged and a new virtual
162367** term of the form "Y <op> X" is added to the WHERE clause and
162368** analyzed separately. The original term is marked with TERM_COPIED
162369** and the new term is marked with TERM_DYNAMIC (because it's pExpr
162370** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
162371** is a commuted copy of a prior term.) The original term has nChild=1
162372** and the copy has idxParent set to the index of the original term.
162373*/
162374static void exprAnalyze(
162375 SrcList *pSrc, /* the FROM clause */
162376 WhereClause *pWC, /* the WHERE clause */
162377 int idxTerm /* Index of the term to be analyzed */
162378){
162379 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
162380 WhereTerm *pTerm; /* The term to be analyzed */
162381 WhereMaskSet *pMaskSet; /* Set of table index masks */
162382 Expr *pExpr; /* The expression to be analyzed */
162383 Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */
162384 Bitmask prereqAll; /* Prerequisites of pExpr */
162385 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
162386 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
162387 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
162388 int noCase = 0; /* uppercase equivalent to lowercase */
162389 int op; /* Top-level operator. pExpr->op */
162390 Parse *pParse = pWInfo->pParse; /* Parsing context */
162391 sqlite3 *db = pParse->db; /* Database connection */
162392 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
162393 int nLeft; /* Number of elements on left side vector */
162394
162395 if( db->mallocFailed ){
162396 return;
162397 }
162398 assert( pWC->nTerm > idxTerm )((void) (0));
162399 pTerm = &pWC->a[idxTerm];
162400 pMaskSet = &pWInfo->sMaskSet;
162401 pExpr = pTerm->pExpr;
162402 assert( pExpr!=0 )((void) (0)); /* Because malloc() has not failed */
162403 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE )((void) (0));
162404 pMaskSet->bVarSelect = 0;
162405 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
162406 op = pExpr->op;
162407 if( op==TK_IN50 ){
162408 assert( pExpr->pRight==0 )((void) (0));
162409 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
162410 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
162411 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
162412 }else{
162413 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
162414 }
162415 prereqAll = prereqLeft | pTerm->prereqRight;
162416 }else{
162417 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
162418 if( pExpr->pLeft==0
162419 || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow)(((pExpr)->flags&(0x001000|0x040000))!=0)
162420 || pExpr->x.pList!=0
162421 ){
162422 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
162423 }else{
162424 prereqAll = prereqLeft | pTerm->prereqRight;
162425 }
162426 }
162427 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT0x1000;
162428
162429#ifdef SQLITE_DEBUG
162430 if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){
162431 printf("\n*** Incorrect prereqAll computed for:\n");
162432 sqlite3TreeViewExpr(0,pExpr,0);
162433 assert( 0 )((void) (0));
162434 }
162435#endif
162436
162437 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON)(((pExpr)->flags&(0x000001|0x000002))!=0) ){
162438 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
162439 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) ){
162440 prereqAll |= x;
162441 extraRight = x-1; /* ON clause terms may not be used with an index
162442 ** on left table of a LEFT JOIN. Ticket #3015 */
162443 if( (prereqAll>>1)>=x ){
162444 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
162445 return;
162446 }
162447 }else if( (prereqAll>>1)>=x ){
162448 /* The ON clause of an INNER JOIN references a table to its right.
162449 ** Most other SQL database engines raise an error. But SQLite versions
162450 ** 3.0 through 3.38 just put the ON clause constraint into the WHERE
162451 ** clause and carried on. Beginning with 3.39, raise an error only
162452 ** if there is a RIGHT or FULL JOIN in the query. This makes SQLite
162453 ** more like other systems, and also preserves legacy. */
162454 if( ALWAYS(pSrc->nSrc>0)(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ0x40)!=0 ){
162455 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
162456 return;
162457 }
162458 ExprClearProperty(pExpr, EP_InnerON)(pExpr)->flags&=~(0x000002);
162459 }
162460 }
162461 pTerm->prereqAll = prereqAll;
162462 pTerm->leftCursor = -1;
162463 pTerm->iParent = -1;
162464 pTerm->eOperator = 0;
162465 if( allowedOp(op) ){
162466 int aiCurCol[2];
162467 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
162468 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
162469 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL0x3fff : WO_EQUIV0x0800;
162470
162471 if( pTerm->u.x.iField>0 ){
162472 assert( op==TK_IN )((void) (0));
162473 assert( pLeft->op==TK_VECTOR )((void) (0));
162474 assert( ExprUseXList(pLeft) )((void) (0));
162475 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
162476 }
162477
162478 if( exprMightBeIndexed(pSrc, aiCurCol, pLeft, op) ){
162479 pTerm->leftCursor = aiCurCol[0];
162480 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
162481 pTerm->u.x.leftColumn = aiCurCol[1];
162482 pTerm->eOperator = operatorMask(op) & opMask;
162483 }
162484 if( op==TK_IS45 ) pTerm->wtFlags |= TERM_IS0x0800;
162485 if( pRight
162486 && exprMightBeIndexed(pSrc, aiCurCol, pRight, op)
162487 && !ExprHasProperty(pRight, EP_FixedCol)(((pRight)->flags&(0x000020))!=0)
162488 ){
162489 WhereTerm *pNew;
162490 Expr *pDup;
162491 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
162492 assert( pTerm->u.x.iField==0 )((void) (0));
162493 if( pTerm->leftCursor>=0 ){
162494 int idxNew;
162495 pDup = sqlite3ExprDup(db, pExpr, 0);
162496 if( db->mallocFailed ){
162497 sqlite3ExprDelete(db, pDup);
162498 return;
162499 }
162500 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
162501 if( idxNew==0 ) return;
162502 pNew = &pWC->a[idxNew];
162503 markTermAsChild(pWC, idxNew, idxTerm);
162504 if( op==TK_IS45 ) pNew->wtFlags |= TERM_IS0x0800;
162505 pTerm = &pWC->a[idxTerm];
162506 pTerm->wtFlags |= TERM_COPIED0x0008;
162507
162508 if( termIsEquivalence(pParse, pDup) ){
162509 pTerm->eOperator |= WO_EQUIV0x0800;
162510 eExtraOp = WO_EQUIV0x0800;
162511 }
162512 }else{
162513 pDup = pExpr;
162514 pNew = pTerm;
162515 }
162516 pNew->wtFlags |= exprCommute(pParse, pDup);
162517 pNew->leftCursor = aiCurCol[0];
162518 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
162519 pNew->u.x.leftColumn = aiCurCol[1];
162520 testcase( (prereqLeft | extraRight) != prereqLeft );
162521 pNew->prereqRight = prereqLeft | extraRight;
162522 pNew->prereqAll = prereqAll;
162523 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
162524 }else
162525 if( op==TK_ISNULL51
162526 && !ExprHasProperty(pExpr,EP_OuterON)(((pExpr)->flags&(0x000001))!=0)
162527 && 0==sqlite3ExprCanBeNull(pLeft)
162528 ){
162529 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
162530 pExpr->op = TK_TRUEFALSE171; /* See tag-20230504-1 */
162531 pExpr->u.zToken = "false";
162532 ExprSetProperty(pExpr, EP_IsFalse)(pExpr)->flags|=(0x20000000);
162533 pTerm->prereqAll = 0;
162534 pTerm->eOperator = 0;
162535 }
162536 }
162537
162538#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
162539 /* If a term is the BETWEEN operator, create two new virtual terms
162540 ** that define the range that the BETWEEN implements. For example:
162541 **
162542 ** a BETWEEN b AND c
162543 **
162544 ** is converted into:
162545 **
162546 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
162547 **
162548 ** The two new terms are added onto the end of the WhereClause object.
162549 ** The new terms are "dynamic" and are children of the original BETWEEN
162550 ** term. That means that if the BETWEEN term is coded, the children are
162551 ** skipped. Or, if the children are satisfied by an index, the original
162552 ** BETWEEN term is skipped.
162553 */
162554 else if( pExpr->op==TK_BETWEEN49 && pWC->op==TK_AND44 ){
162555 ExprList *pList;
162556 int i;
162557 static const u8 ops[] = {TK_GE58, TK_LE56};
162558 assert( ExprUseXList(pExpr) )((void) (0));
162559 pList = pExpr->x.pList;
162560 assert( pList!=0 )((void) (0));
162561 assert( pList->nExpr==2 )((void) (0));
162562 for(i=0; i<2; i++){
162563 Expr *pNewExpr;
162564 int idxNew;
162565 pNewExpr = sqlite3PExpr(pParse, ops[i],
162566 sqlite3ExprDup(db, pExpr->pLeft, 0),
162567 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
162568 transferJoinMarkings(pNewExpr, pExpr);
162569 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
162570 testcase( idxNew==0 );
162571 exprAnalyze(pSrc, pWC, idxNew);
162572 pTerm = &pWC->a[idxTerm];
162573 markTermAsChild(pWC, idxNew, idxTerm);
162574 }
162575 }
162576#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
162577
162578#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
162579 /* Analyze a term that is composed of two or more subterms connected by
162580 ** an OR operator.
162581 */
162582 else if( pExpr->op==TK_OR43 ){
162583 assert( pWC->op==TK_AND )((void) (0));
162584 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
162585 pTerm = &pWC->a[idxTerm];
162586 }
162587#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
162588 /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently
162589 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
162590 ** virtual term of that form.
162591 **
162592 ** The virtual term must be tagged with TERM_VNULL.
162593 */
162594 else if( pExpr->op==TK_NOTNULL52 ){
162595 if( pExpr->pLeft->op==TK_COLUMN168
162596 && pExpr->pLeft->iColumn>=0
162597 && !ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0)
162598 ){
162599 Expr *pNewExpr;
162600 Expr *pLeft = pExpr->pLeft;
162601 int idxNew;
162602 WhereTerm *pNewTerm;
162603
162604 pNewExpr = sqlite3PExpr(pParse, TK_GT55,
162605 sqlite3ExprDup(db, pLeft, 0),
162606 sqlite3ExprAlloc(db, TK_NULL122, 0, 0));
162607
162608 idxNew = whereClauseInsert(pWC, pNewExpr,
162609 TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001|TERM_VNULL0x0080);
162610 if( idxNew ){
162611 pNewTerm = &pWC->a[idxNew];
162612 pNewTerm->prereqRight = 0;
162613 pNewTerm->leftCursor = pLeft->iTable;
162614 pNewTerm->u.x.leftColumn = pLeft->iColumn;
162615 pNewTerm->eOperator = WO_GT(0x0002<<(55 -54));
162616 markTermAsChild(pWC, idxNew, idxTerm);
162617 pTerm = &pWC->a[idxTerm];
162618 pTerm->wtFlags |= TERM_COPIED0x0008;
162619 pNewTerm->prereqAll = pTerm->prereqAll;
162620 }
162621 }
162622 }
162623
162624
162625#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
162626 /* Add constraints to reduce the search space on a LIKE or GLOB
162627 ** operator.
162628 **
162629 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
162630 **
162631 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
162632 **
162633 ** The last character of the prefix "abc" is incremented to form the
162634 ** termination condition "abd". If case is not significant (the default
162635 ** for LIKE) then the lower-bound is made all uppercase and the upper-
162636 ** bound is made all lowercase so that the bounds also work when comparing
162637 ** BLOBs.
162638 */
162639 else if( pExpr->op==TK_FUNCTION172
162640 && pWC->op==TK_AND44
162641 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
162642 ){
162643 Expr *pLeft; /* LHS of LIKE/GLOB operator */
162644 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
162645 Expr *pNewExpr1;
162646 Expr *pNewExpr2;
162647 int idxNew1;
162648 int idxNew2;
162649 const char *zCollSeqName; /* Name of collating sequence */
162650 const u16 wtFlags = TERM_LIKEOPT0x0100 | TERM_VIRTUAL0x0002 | TERM_DYNAMIC0x0001;
162651
162652 assert( ExprUseXList(pExpr) )((void) (0));
162653 pLeft = pExpr->x.pList->a[1].pExpr;
162654 pStr2 = sqlite3ExprDup(db, pStr1, 0);
162655 assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) )((void) (0));
162656 assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) )((void) (0));
162657
162658
162659 /* Convert the lower bound to upper-case and the upper bound to
162660 ** lower-case (upper-case is less than lower-case in ASCII) so that
162661 ** the range constraints also work for BLOBs
162662 */
162663 if( noCase && !pParse->db->mallocFailed ){
162664 int i;
162665 char c;
162666 pTerm->wtFlags |= TERM_LIKE0x0400;
162667 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
162668 pStr1->u.zToken[i] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
162669 pStr2->u.zToken[i] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
162670 }
162671 }
162672
162673 if( !db->mallocFailed ){
162674 u8 c, *pC; /* Last character before the first wildcard */
162675 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
162676 c = *pC;
162677 if( noCase ){
162678 /* The point is to increment the last character before the first
162679 ** wildcard. But if we increment '@', that will push it into the
162680 ** alphabetic range where case conversions will mess up the
162681 ** inequality. To avoid this, make sure to also run the full
162682 ** LIKE on all candidate expressions by clearing the isComplete flag
162683 */
162684 if( c=='A'-1 ) isComplete = 0;
162685 c = sqlite3UpperToLower[c];
162686 }
162687 *pC = c + 1;
162688 }
162689 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
162690 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
162691 pNewExpr1 = sqlite3PExpr(pParse, TK_GE58,
162692 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
162693 pStr1);
162694 transferJoinMarkings(pNewExpr1, pExpr);
162695 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
162696 testcase( idxNew1==0 );
162697 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
162698 pNewExpr2 = sqlite3PExpr(pParse, TK_LT57,
162699 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
162700 pStr2);
162701 transferJoinMarkings(pNewExpr2, pExpr);
162702 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
162703 testcase( idxNew2==0 );
162704 exprAnalyze(pSrc, pWC, idxNew1);
162705 exprAnalyze(pSrc, pWC, idxNew2);
162706 pTerm = &pWC->a[idxTerm];
162707 if( isComplete ){
162708 markTermAsChild(pWC, idxNew1, idxTerm);
162709 markTermAsChild(pWC, idxNew2, idxTerm);
162710 }
162711 }
162712#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
162713
162714 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
162715 ** new terms for each component comparison - "a = ?" and "b = ?". The
162716 ** new terms completely replace the original vector comparison, which is
162717 ** no longer used.
162718 **
162719 ** This is only required if at least one side of the comparison operation
162720 ** is not a sub-select.
162721 **
162722 ** tag-20220128a
162723 */
162724 if( (pExpr->op==TK_EQ54 || pExpr->op==TK_IS45)
162725 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
162726 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
162727 && ( (pExpr->pLeft->flags & EP_xIsSelect0x001000)==0
162728 || (pExpr->pRight->flags & EP_xIsSelect0x001000)==0)
162729 && pWC->op==TK_AND44
162730 ){
162731 int i;
162732 for(i=0; i<nLeft; i++){
162733 int idxNew;
162734 Expr *pNew;
162735 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
162736 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
162737
162738 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
162739 transferJoinMarkings(pNew, pExpr);
162740 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x0001|TERM_SLICE0x8000);
162741 exprAnalyze(pSrc, pWC, idxNew);
162742 }
162743 pTerm = &pWC->a[idxTerm];
162744 pTerm->wtFlags |= TERM_CODED0x0004|TERM_VIRTUAL0x0002; /* Disable the original */
162745 pTerm->eOperator = WO_ROWVAL0x2000;
162746 }
162747
162748 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
162749 ** a virtual term for each vector component. The expression object
162750 ** used by each such virtual term is pExpr (the full vector IN(...)
162751 ** expression). The WhereTerm.u.x.iField variable identifies the index within
162752 ** the vector on the LHS that the virtual term represents.
162753 **
162754 ** This only works if the RHS is a simple SELECT (not a compound) that does
162755 ** not use window functions.
162756 */
162757 else if( pExpr->op==TK_IN50
162758 && pTerm->u.x.iField==0
162759 && pExpr->pLeft->op==TK_VECTOR177
162760 && ALWAYS( ExprUseXSelect(pExpr) )((((pExpr)->flags&0x001000)!=0))
162761 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values0x0000200))
162762#ifndef SQLITE_OMIT_WINDOWFUNC
162763 && pExpr->x.pSelect->pWin==0
162764#endif
162765 && pWC->op==TK_AND44
162766 ){
162767 int i;
162768 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
162769 int idxNew;
162770 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL0x0002|TERM_SLICE0x8000);
162771 pWC->a[idxNew].u.x.iField = i+1;
162772 exprAnalyze(pSrc, pWC, idxNew);
162773 markTermAsChild(pWC, idxNew, idxTerm);
162774 }
162775 }
162776
162777#ifndef SQLITE_OMIT_VIRTUALTABLE
162778 /* Add a WO_AUX auxiliary term to the constraint set if the
162779 ** current expression is of the form "column OP expr" where OP
162780 ** is an operator that gets passed into virtual tables but which is
162781 ** not normally optimized for ordinary tables. In other words, OP
162782 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
162783 ** This information is used by the xBestIndex methods of
162784 ** virtual tables. The native query optimizer does not attempt
162785 ** to do anything with MATCH functions.
162786 */
162787 else if( pWC->op==TK_AND44 ){
162788 Expr *pRight = 0, *pLeft = 0;
162789 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
162790 while( res-- > 0 ){
162791 int idxNew;
162792 WhereTerm *pNewTerm;
162793 Bitmask prereqColumn, prereqExpr;
162794
162795 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
162796 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
162797 if( (prereqExpr & prereqColumn)==0 ){
162798 Expr *pNewExpr;
162799 pNewExpr = sqlite3PExpr(pParse, TK_MATCH47,
162800 0, sqlite3ExprDup(db, pRight, 0));
162801 if( ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) && pNewExpr ){
162802 ExprSetProperty(pNewExpr, EP_OuterON)(pNewExpr)->flags|=(0x000001);
162803 pNewExpr->w.iJoin = pExpr->w.iJoin;
162804 }
162805 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x0002|TERM_DYNAMIC0x0001);
162806 testcase( idxNew==0 );
162807 pNewTerm = &pWC->a[idxNew];
162808 pNewTerm->prereqRight = prereqExpr;
162809 pNewTerm->leftCursor = pLeft->iTable;
162810 pNewTerm->u.x.leftColumn = pLeft->iColumn;
162811 pNewTerm->eOperator = WO_AUX0x0040;
162812 pNewTerm->eMatchOp = eOp2;
162813 markTermAsChild(pWC, idxNew, idxTerm);
162814 pTerm = &pWC->a[idxTerm];
162815 pTerm->wtFlags |= TERM_COPIED0x0008;
162816 pNewTerm->prereqAll = pTerm->prereqAll;
162817 }
162818 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
162819 }
162820 }
162821#endif /* SQLITE_OMIT_VIRTUALTABLE */
162822
162823 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
162824 ** an index for tables to the left of the join.
162825 */
162826 testcase( pTerm!=&pWC->a[idxTerm] );
162827 pTerm = &pWC->a[idxTerm];
162828 pTerm->prereqRight |= extraRight;
162829}
162830
162831/***************************************************************************
162832** Routines with file scope above. Interface to the rest of the where.c
162833** subsystem follows.
162834***************************************************************************/
162835
162836/*
162837** This routine identifies subexpressions in the WHERE clause where
162838** each subexpression is separated by the AND operator or some other
162839** operator specified in the op parameter. The WhereClause structure
162840** is filled with pointers to subexpressions. For example:
162841**
162842** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
162843** \________/ \_______________/ \________________/
162844** slot[0] slot[1] slot[2]
162845**
162846** The original WHERE clause in pExpr is unaltered. All this routine
162847** does is make slot[] entries point to substructure within pExpr.
162848**
162849** In the previous sentence and in the diagram, "slot[]" refers to
162850** the WhereClause.a[] array. The slot[] array grows as needed to contain
162851** all terms of the WHERE clause.
162852*/
162853SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
162854 Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr);
162855 pWC->op = op;
162856 assert( pE2!=0 || pExpr==0 )((void) (0));
162857 if( pE2==0 ) return;
162858 if( pE2->op!=op ){
162859 whereClauseInsert(pWC, pExpr, 0);
162860 }else{
162861 sqlite3WhereSplit(pWC, pE2->pLeft, op);
162862 sqlite3WhereSplit(pWC, pE2->pRight, op);
162863 }
162864}
162865
162866/*
162867** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
162868** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
162869** where-clause passed as the first argument. The value for the term
162870** is found in register iReg.
162871**
162872** In the common case where the value is a simple integer
162873** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
162874** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
162875** If not, then it codes as a TK_REGISTER expression.
162876*/
162877static void whereAddLimitExpr(
162878 WhereClause *pWC, /* Add the constraint to this WHERE clause */
162879 int iReg, /* Register that will hold value of the limit/offset */
162880 Expr *pExpr, /* Expression that defines the limit/offset */
162881 int iCsr, /* Cursor to which the constraint applies */
162882 int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
162883){
162884 Parse *pParse = pWC->pWInfo->pParse;
162885 sqlite3 *db = pParse->db;
162886 Expr *pNew;
162887 int iVal = 0;
162888
162889 if( sqlite3ExprIsInteger(pExpr, &iVal, pParse) && iVal>=0 ){
162890 Expr *pVal = sqlite3Expr(db, TK_INTEGER156, 0);
162891 if( pVal==0 ) return;
162892 ExprSetProperty(pVal, EP_IntValue)(pVal)->flags|=(0x000800);
162893 pVal->u.iValue = iVal;
162894 pNew = sqlite3PExpr(pParse, TK_MATCH47, 0, pVal);
162895 }else{
162896 Expr *pVal = sqlite3Expr(db, TK_REGISTER176, 0);
162897 if( pVal==0 ) return;
162898 pVal->iTable = iReg;
162899 pNew = sqlite3PExpr(pParse, TK_MATCH47, 0, pVal);
162900 }
162901 if( pNew ){
162902 WhereTerm *pTerm;
162903 int idx;
162904 idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x0001|TERM_VIRTUAL0x0002);
162905 pTerm = &pWC->a[idx];
162906 pTerm->leftCursor = iCsr;
162907 pTerm->eOperator = WO_AUX0x0040;
162908 pTerm->eMatchOp = eMatchOp;
162909 }
162910}
162911
162912/*
162913** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
162914** SELECT statement passed as the second argument. These terms are only
162915** added if:
162916**
162917** 1. The SELECT statement has a LIMIT clause, and
162918** 2. The SELECT statement is not an aggregate or DISTINCT query, and
162919** 3. The SELECT statement has exactly one object in its from clause, and
162920** that object is a virtual table, and
162921** 4. There are no terms in the WHERE clause that will not be passed
162922** to the virtual table xBestIndex method.
162923** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
162924** method.
162925**
162926** LIMIT and OFFSET terms are ignored by most of the planner code. They
162927** exist only so that they may be passed to the xBestIndex method of the
162928** single virtual table in the FROM clause of the SELECT.
162929*/
162930SQLITE_PRIVATEstatic void SQLITE_NOINLINE__attribute__((noinline)) sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
162931 assert( p!=0 && p->pLimit!=0 )((void) (0)); /* 1 -- checked by caller */
162932 if( p->pGroupBy==0
162933 && (p->selFlags & (SF_Distinct0x0000001|SF_Aggregate0x0000008))==0 /* 2 */
162934 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pSTab)((p->pSrc->a[0].pSTab)->eTabType==1)) /* 3 */
162935 ){
162936 ExprList *pOrderBy = p->pOrderBy;
162937 int iCsr = p->pSrc->a[0].iCursor;
162938 int ii;
162939
162940 /* Check condition (4). Return early if it is not met. */
162941 for(ii=0; ii<pWC->nTerm; ii++){
162942 if( pWC->a[ii].wtFlags & TERM_CODED0x0004 ){
162943 /* This term is a vector operation that has been decomposed into
162944 ** other, subsequent terms. It can be ignored. See tag-20220128a */
162945 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL )((void) (0));
162946 assert( pWC->a[ii].eOperator==WO_ROWVAL )((void) (0));
162947 continue;
162948 }
162949 if( pWC->a[ii].nChild ){
162950 /* If this term has child terms, then they are also part of the
162951 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
162952 ** will only be added if each of the child terms passes the
162953 ** (leftCursor==iCsr) test below. */
162954 continue;
162955 }
162956 if( pWC->a[ii].leftCursor!=iCsr ) return;
162957 if( pWC->a[ii].prereqRight!=0 ) return;
162958 }
162959
162960 /* Check condition (5). Return early if it is not met. */
162961 if( pOrderBy ){
162962 for(ii=0; ii<pOrderBy->nExpr; ii++){
162963 Expr *pExpr = pOrderBy->a[ii].pExpr;
162964 if( pExpr->op!=TK_COLUMN168 ) return;
162965 if( pExpr->iTable!=iCsr ) return;
162966 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ) return;
162967 }
162968 }
162969
162970 /* All conditions are met. Add the terms to the where-clause object. */
162971 assert( p->pLimit->op==TK_LIMIT )((void) (0));
162972 if( p->iOffset!=0 && (p->selFlags & SF_Compound0x0000100)==0 ){
162973 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
162974 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET74);
162975 }
162976 if( p->iOffset==0 || (p->selFlags & SF_Compound0x0000100)==0 ){
162977 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
162978 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT73);
162979 }
162980 }
162981}
162982
162983/*
162984** Initialize a preallocated WhereClause structure.
162985*/
162986SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(
162987 WhereClause *pWC, /* The WhereClause to be initialized */
162988 WhereInfo *pWInfo /* The WHERE processing context */
162989){
162990 pWC->pWInfo = pWInfo;
162991 pWC->hasOr = 0;
162992 pWC->pOuter = 0;
162993 pWC->nTerm = 0;
162994 pWC->nBase = 0;
162995 pWC->nSlot = ArraySize(pWC->aStatic)((int)(sizeof(pWC->aStatic)/sizeof(pWC->aStatic[0])));
162996 pWC->a = pWC->aStatic;
162997}
162998
162999/*
163000** Deallocate a WhereClause structure. The WhereClause structure
163001** itself is not freed. This routine is the inverse of
163002** sqlite3WhereClauseInit().
163003*/
163004SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause *pWC){
163005 sqlite3 *db = pWC->pWInfo->pParse->db;
163006 assert( pWC->nTerm>=pWC->nBase )((void) (0));
163007 if( pWC->nTerm>0 ){
163008 WhereTerm *a = pWC->a;
163009 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
163010#ifdef SQLITE_DEBUG
163011 int i;
163012 /* Verify that every term past pWC->nBase is virtual */
163013 for(i=pWC->nBase; i<pWC->nTerm; i++){
163014 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 )((void) (0));
163015 }
163016#endif
163017 while(1){
163018 assert( a->eMatchOp==0 || a->eOperator==WO_AUX )((void) (0));
163019 if( a->wtFlags & TERM_DYNAMIC0x0001 ){
163020 sqlite3ExprDelete(db, a->pExpr);
163021 }
163022 if( a->wtFlags & (TERM_ORINFO0x0010|TERM_ANDINFO0x0020) ){
163023 if( a->wtFlags & TERM_ORINFO0x0010 ){
163024 assert( (a->wtFlags & TERM_ANDINFO)==0 )((void) (0));
163025 whereOrInfoDelete(db, a->u.pOrInfo);
163026 }else{
163027 assert( (a->wtFlags & TERM_ANDINFO)!=0 )((void) (0));
163028 whereAndInfoDelete(db, a->u.pAndInfo);
163029 }
163030 }
163031 if( a==aLast ) break;
163032 a++;
163033 }
163034 }
163035}
163036
163037
163038/*
163039** These routines walk (recursively) an expression tree and generate
163040** a bitmask indicating which tables are used in that expression
163041** tree.
163042**
163043** sqlite3WhereExprUsage(MaskSet, Expr) ->
163044**
163045** Return a Bitmask of all tables referenced by Expr. Expr can be
163046** be NULL, in which case 0 is returned.
163047**
163048** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
163049**
163050** Same as sqlite3WhereExprUsage() except that Expr must not be
163051** NULL. The "NN" suffix on the name stands for "Not Null".
163052**
163053** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
163054**
163055** Return a Bitmask of all tables referenced by every expression
163056** in the expression list ExprList. ExprList can be NULL, in which
163057** case 0 is returned.
163058**
163059** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
163060**
163061** Internal use only. Called only by sqlite3WhereExprUsageNN() for
163062** complex expressions that require pushing register values onto
163063** the stack. Many calls to sqlite3WhereExprUsageNN() do not need
163064** the more complex analysis done by this routine. Hence, the
163065** computations done by this routine are broken out into a separate
163066** "no-inline" function to avoid the stack push overhead in the
163067** common case where it is not needed.
163068*/
163069static SQLITE_NOINLINE__attribute__((noinline)) Bitmask sqlite3WhereExprUsageFull(
163070 WhereMaskSet *pMaskSet,
163071 Expr *p
163072){
163073 Bitmask mask;
163074 mask = (p->op==TK_IF_NULL_ROW179) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
163075 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
163076 if( p->pRight ){
163077 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
163078 assert( p->x.pList==0 )((void) (0));
163079 }else if( ExprUseXSelect(p)(((p)->flags&0x001000)!=0) ){
163080 if( ExprHasProperty(p, EP_VarSelect)(((p)->flags&(0x000040))!=0) ) pMaskSet->bVarSelect = 1;
163081 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
163082 }else if( p->x.pList ){
163083 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
163084 }
163085#ifndef SQLITE_OMIT_WINDOWFUNC
163086 if( (p->op==TK_FUNCTION172 || p->op==TK_AGG_FUNCTION169) && ExprUseYWin(p)(((p)->flags&0x1000000)!=0) ){
163087 assert( p->y.pWin!=0 )((void) (0));
163088 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
163089 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
163090 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
163091 }
163092#endif
163093 return mask;
163094}
163095SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
163096 if( p->op==TK_COLUMN168 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(0x000020))!=0) ){
163097 return sqlite3WhereGetMask(pMaskSet, p->iTable);
163098 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf)(((p)->flags&(0x010000|0x800000))!=0) ){
163099 assert( p->op!=TK_IF_NULL_ROW )((void) (0));
163100 return 0;
163101 }
163102 return sqlite3WhereExprUsageFull(pMaskSet, p);
163103}
163104SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
163105 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
163106}
163107SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
163108 int i;
163109 Bitmask mask = 0;
163110 if( pList ){
163111 for(i=0; i<pList->nExpr; i++){
163112 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
163113 }
163114 }
163115 return mask;
163116}
163117
163118
163119/*
163120** Call exprAnalyze on all terms in a WHERE clause.
163121**
163122** Note that exprAnalyze() might add new virtual terms onto the
163123** end of the WHERE clause. We do not want to analyze these new
163124** virtual terms, so start analyzing at the end and work forward
163125** so that the added virtual terms are never processed.
163126*/
163127SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(
163128 SrcList *pTabList, /* the FROM clause */
163129 WhereClause *pWC /* the WHERE clause to be analyzed */
163130){
163131 int i;
163132 for(i=pWC->nTerm-1; i>=0; i--){
163133 exprAnalyze(pTabList, pWC, i);
163134 }
163135}
163136
163137/*
163138** For table-valued-functions, transform the function arguments into
163139** new WHERE clause terms.
163140**
163141** Each function argument translates into an equality constraint against
163142** a HIDDEN column in the table.
163143*/
163144SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(
163145 Parse *pParse, /* Parsing context */
163146 SrcItem *pItem, /* The FROM clause term to process */
163147 WhereClause *pWC /* Xfer function arguments to here */
163148){
163149 Table *pTab;
163150 int j, k;
163151 ExprList *pArgs;
163152 Expr *pColRef;
163153 Expr *pTerm;
163154 if( pItem->fg.isTabFunc==0 ) return;
163155 pTab = pItem->pSTab;
163156 assert( pTab!=0 )((void) (0));
163157 pArgs = pItem->u1.pFuncArg;
163158 if( pArgs==0 ) return;
163159 for(j=k=0; j<pArgs->nExpr; j++){
163160 Expr *pRhs;
163161 u32 joinType;
163162 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN0x0002)==0 ){k++;}
163163 if( k>=pTab->nCol ){
163164 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
163165 pTab->zName, j);
163166 return;
163167 }
163168 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN168, 0, 0);
163169 if( pColRef==0 ) return;
163170 pColRef->iTable = pItem->iCursor;
163171 pColRef->iColumn = k++;
163172 assert( ExprUseYTab(pColRef) )((void) (0));
163173 pColRef->y.pTab = pTab;
163174 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
163175 pRhs = sqlite3PExpr(pParse, TK_UPLUS173,
163176 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
163177 pTerm = sqlite3PExpr(pParse, TK_EQ54, pColRef, pRhs);
163178 if( pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10) ){
163179 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
163180 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
163181 joinType = EP_OuterON0x000001;
163182 }else{
163183 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
163184 joinType = EP_InnerON0x000002;
163185 }
163186 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
163187 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC0x0001);
163188 }
163189}
163190
163191/************** End of whereexpr.c *******************************************/
163192/************** Begin file where.c *******************************************/
163193/*
163194** 2001 September 15
163195**
163196** The author disclaims copyright to this source code. In place of
163197** a legal notice, here is a blessing:
163198**
163199** May you do good and not evil.
163200** May you find forgiveness for yourself and forgive others.
163201** May you share freely, never taking more than you give.
163202**
163203*************************************************************************
163204** This module contains C code that generates VDBE code used to process
163205** the WHERE clause of SQL statements. This module is responsible for
163206** generating the code that loops through a table looking for applicable
163207** rows. Indices are selected and used to speed the search when doing
163208** so is applicable. Because this module is responsible for selecting
163209** indices, you might also think of this module as the "query optimizer".
163210*/
163211/* #include "sqliteInt.h" */
163212/* #include "whereInt.h" */
163213
163214/*
163215** Extra information appended to the end of sqlite3_index_info but not
163216** visible to the xBestIndex function, at least not directly. The
163217** sqlite3_vtab_collation() interface knows how to reach it, however.
163218**
163219** This object is not an API and can be changed from one release to the
163220** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
163221** agree on the structure, all will be well.
163222*/
163223typedef struct HiddenIndexInfo HiddenIndexInfo;
163224struct HiddenIndexInfo {
163225 WhereClause *pWC; /* The Where clause being analyzed */
163226 Parse *pParse; /* The parsing context */
163227 int eDistinct; /* Value to return from sqlite3_vtab_distinct() */
163228 u32 mIn; /* Mask of terms that are <col> IN (...) */
163229 u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */
163230 sqlite3_value *aRhs[1]; /* RHS values for constraints. MUST BE LAST
163231 ** because extra space is allocated to hold up
163232 ** to nTerm such values */
163233};
163234
163235/* Forward declaration of methods */
163236static int whereLoopResize(sqlite3*, WhereLoop*, int);
163237
163238/*
163239** Return the estimated number of output rows from a WHERE clause
163240*/
163241SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
163242 return pWInfo->nRowOut;
163243}
163244
163245/*
163246** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
163247** WHERE clause returns outputs for DISTINCT processing.
163248*/
163249SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
163250 return pWInfo->eDistinct;
163251}
163252
163253/*
163254** Return the number of ORDER BY terms that are satisfied by the
163255** WHERE clause. A return of 0 means that the output must be
163256** completely sorted. A return equal to the number of ORDER BY
163257** terms means that no sorting is needed at all. A return that
163258** is positive but less than the number of ORDER BY terms means that
163259** block sorting is required.
163260*/
163261SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
163262 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
163263}
163264
163265/*
163266** In the ORDER BY LIMIT optimization, if the inner-most loop is known
163267** to emit rows in increasing order, and if the last row emitted by the
163268** inner-most loop did not fit within the sorter, then we can skip all
163269** subsequent rows for the current iteration of the inner loop (because they
163270** will not fit in the sorter either) and continue with the second inner
163271** loop - the loop immediately outside the inner-most.
163272**
163273** When a row does not fit in the sorter (because the sorter already
163274** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
163275** label returned by this function.
163276**
163277** If the ORDER BY LIMIT optimization applies, the jump destination should
163278** be the continuation for the second-inner-most loop. If the ORDER BY
163279** LIMIT optimization does not apply, then the jump destination should
163280** be the continuation for the inner-most loop.
163281**
163282** It is always safe for this routine to return the continuation of the
163283** inner-most loop, in the sense that a correct answer will result.
163284** Returning the continuation the second inner loop is an optimization
163285** that might make the code run a little faster, but should not change
163286** the final answer.
163287*/
163288SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
163289 WhereLevel *pInner;
163290 if( !pWInfo->bOrderedInnerLoop ){
163291 /* The ORDER BY LIMIT optimization does not apply. Jump to the
163292 ** continuation of the inner-most loop. */
163293 return pWInfo->iContinue;
163294 }
163295 pInner = &pWInfo->a[pWInfo->nLevel-1];
163296 assert( pInner->addrNxt!=0 )((void) (0));
163297 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
163298}
163299
163300/*
163301** While generating code for the min/max optimization, after handling
163302** the aggregate-step call to min() or max(), check to see if any
163303** additional looping is required. If the output order is such that
163304** we are certain that the correct answer has already been found, then
163305** code an OP_Goto to by pass subsequent processing.
163306**
163307** Any extra OP_Goto that is coded here is an optimization. The
163308** correct answer should be obtained regardless. This OP_Goto just
163309** makes the answer appear faster.
163310*/
163311SQLITE_PRIVATEstatic void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){
163312 WhereLevel *pInner;
163313 int i;
163314 if( !pWInfo->bOrderedInnerLoop ) return;
163315 if( pWInfo->nOBSat==0 ) return;
163316 for(i=pWInfo->nLevel-1; i>=0; i--){
163317 pInner = &pWInfo->a[i];
163318 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN0x00000004)!=0 ){
163319 sqlite3VdbeGoto(v, pInner->addrNxt);
163320 return;
163321 }
163322 }
163323 sqlite3VdbeGoto(v, pWInfo->iBreak);
163324}
163325
163326/*
163327** Return the VDBE address or label to jump to in order to continue
163328** immediately with the next row of a WHERE clause.
163329*/
163330SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
163331 assert( pWInfo->iContinue!=0 )((void) (0));
163332 return pWInfo->iContinue;
163333}
163334
163335/*
163336** Return the VDBE address or label to jump to in order to break
163337** out of a WHERE loop.
163338*/
163339SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
163340 return pWInfo->iBreak;
163341}
163342
163343/*
163344** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
163345** operate directly on the rowids returned by a WHERE clause. Return
163346** ONEPASS_SINGLE (1) if the statement can operation directly because only
163347** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
163348** optimization can be used on multiple
163349**
163350** If the ONEPASS optimization is used (if this routine returns true)
163351** then also write the indices of open cursors used by ONEPASS
163352** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
163353** table and iaCur[1] gets the cursor used by an auxiliary index.
163354** Either value may be -1, indicating that cursor is not used.
163355** Any cursors returned will have been opened for writing.
163356**
163357** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
163358** unable to use the ONEPASS optimization.
163359*/
163360SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
163361 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
163362#ifdef WHERETRACE_ENABLED
163363 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF0 ){
163364 sqlite3DebugPrintf("%s cursors: %d %d\n",
163365 pWInfo->eOnePass==ONEPASS_SINGLE1 ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
163366 aiCur[0], aiCur[1]);
163367 }
163368#endif
163369 return pWInfo->eOnePass;
163370}
163371
163372/*
163373** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move
163374** the data cursor to the row selected by the index cursor.
163375*/
163376SQLITE_PRIVATEstatic int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){
163377 return pWInfo->bDeferredSeek;
163378}
163379
163380/*
163381** Move the content of pSrc into pDest
163382*/
163383static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
163384 pDest->n = pSrc->n;
163385 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
163386}
163387
163388/*
163389** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
163390**
163391** The new entry might overwrite an existing entry, or it might be
163392** appended, or it might be discarded. Do whatever is the right thing
163393** so that pSet keeps the N_OR_COST best entries seen so far.
163394*/
163395static int whereOrInsert(
163396 WhereOrSet *pSet, /* The WhereOrSet to be updated */
163397 Bitmask prereq, /* Prerequisites of the new entry */
163398 LogEst rRun, /* Run-cost of the new entry */
163399 LogEst nOut /* Number of outputs for the new entry */
163400){
163401 u16 i;
163402 WhereOrCost *p;
163403 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
163404 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
163405 goto whereOrInsert_done;
163406 }
163407 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
163408 return 0;
163409 }
163410 }
163411 if( pSet->n<N_OR_COST3 ){
163412 p = &pSet->a[pSet->n++];
163413 p->nOut = nOut;
163414 }else{
163415 p = pSet->a;
163416 for(i=1; i<pSet->n; i++){
163417 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
163418 }
163419 if( p->rRun<=rRun ) return 0;
163420 }
163421whereOrInsert_done:
163422 p->prereq = prereq;
163423 p->rRun = rRun;
163424 if( p->nOut>nOut ) p->nOut = nOut;
163425 return 1;
163426}
163427
163428/*
163429** Return the bitmask for the given cursor number. Return 0 if
163430** iCursor is not in the set.
163431*/
163432SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
163433 int i;
163434 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 )((void) (0));
163435 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 )((void) (0));
163436 assert( iCursor>=-1 )((void) (0));
163437 if( pMaskSet->ix[0]==iCursor ){
163438 return 1;
163439 }
163440 for(i=1; i<pMaskSet->n; i++){
163441 if( pMaskSet->ix[i]==iCursor ){
163442 return MASKBIT(i)(((Bitmask)1)<<(i));
163443 }
163444 }
163445 return 0;
163446}
163447
163448/* Allocate memory that is automatically freed when pWInfo is freed.
163449*/
163450SQLITE_PRIVATEstatic void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte){
163451 WhereMemBlock *pBlock;
163452 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
163453 if( pBlock ){
163454 pBlock->pNext = pWInfo->pMemToFree;
163455 pBlock->sz = nByte;
163456 pWInfo->pMemToFree = pBlock;
163457 pBlock++;
163458 }
163459 return (void*)pBlock;
163460}
163461SQLITE_PRIVATEstatic void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte){
163462 void *pNew = sqlite3WhereMalloc(pWInfo, nByte);
163463 if( pNew && pOld ){
163464 WhereMemBlock *pOldBlk = (WhereMemBlock*)pOld;
163465 pOldBlk--;
163466 assert( pOldBlk->sz<nByte )((void) (0));
163467 memcpy(pNew, pOld, pOldBlk->sz);
163468 }
163469 return pNew;
163470}
163471
163472/*
163473** Create a new mask for cursor iCursor.
163474**
163475** There is one cursor per table in the FROM clause. The number of
163476** tables in the FROM clause is limited by a test early in the
163477** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
163478** array will never overflow.
163479*/
163480static void createMask(WhereMaskSet *pMaskSet, int iCursor){
163481 assert( pMaskSet->n < ArraySize(pMaskSet->ix) )((void) (0));
163482 pMaskSet->ix[pMaskSet->n++] = iCursor;
163483}
163484
163485/*
163486** If the right-hand branch of the expression is a TK_COLUMN, then return
163487** a pointer to the right-hand branch. Otherwise, return NULL.
163488*/
163489static Expr *whereRightSubexprIsColumn(Expr *p){
163490 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
163491 if( ALWAYS(p!=0)(p!=0) && p->op==TK_COLUMN168 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(0x000020))!=0) ){
163492 return p;
163493 }
163494 return 0;
163495}
163496
163497/*
163498** Term pTerm is guaranteed to be a WO_IN term. It may be a component term
163499** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)".
163500** This function checks to see if the term is compatible with an index
163501** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so,
163502** it returns a pointer to the name of the collation sequence (e.g. "BINARY"
163503** or "NOCASE") used by the comparison in pTerm. If it is not compatible
163504** with affinity idxaff, NULL is returned.
163505*/
163506static SQLITE_NOINLINE__attribute__((noinline)) const char *indexInAffinityOk(
163507 Parse *pParse,
163508 WhereTerm *pTerm,
163509 u8 idxaff
163510){
163511 Expr *pX = pTerm->pExpr;
163512 Expr inexpr;
163513
163514 assert( pTerm->eOperator & WO_IN )((void) (0));
163515
163516 if( sqlite3ExprIsVector(pX->pLeft) ){
163517 int iField = pTerm->u.x.iField - 1;
163518 inexpr.flags = 0;
163519 inexpr.op = TK_EQ54;
163520 inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
163521 assert( ExprUseXSelect(pX) )((void) (0));
163522 inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
163523 pX = &inexpr;
163524 }
163525
163526 if( sqlite3IndexAffinityOk(pX, idxaff) ){
163527 CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX);
163528 return pRet ? pRet->zName : sqlite3StrBINARY;
163529 }
163530 return 0;
163531}
163532
163533/*
163534** Advance to the next WhereTerm that matches according to the criteria
163535** established when the pScan object was initialized by whereScanInit().
163536** Return NULL if there are no more matching WhereTerms.
163537*/
163538static WhereTerm *whereScanNext(WhereScan *pScan){
163539 int iCur; /* The cursor on the LHS of the term */
163540 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
163541 Expr *pX; /* An expression being tested */
163542 WhereClause *pWC; /* Shorthand for pScan->pWC */
163543 WhereTerm *pTerm; /* The term being tested */
163544 int k = pScan->k; /* Where to start scanning */
163545
163546 assert( pScan->iEquiv<=pScan->nEquiv )((void) (0));
163547 pWC = pScan->pWC;
163548 while(1){
163549 iColumn = pScan->aiColumn[pScan->iEquiv-1];
163550 iCur = pScan->aiCur[pScan->iEquiv-1];
163551 assert( pWC!=0 )((void) (0));
163552 assert( iCur>=0 )((void) (0));
163553 do{
163554 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
163555 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 )((void) (0));
163556 if( pTerm->leftCursor==iCur
163557 && pTerm->u.x.leftColumn==iColumn
163558 && (iColumn!=XN_EXPR(-2)
163559 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
163560 pScan->pIdxExpr,iCur)==0)
163561 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0))
163562 ){
163563 if( (pTerm->eOperator & WO_EQUIV0x0800)!=0
163564 && pScan->nEquiv<ArraySize(pScan->aiCur)((int)(sizeof(pScan->aiCur)/sizeof(pScan->aiCur[0])))
163565 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
163566 ){
163567 int j;
163568 for(j=0; j<pScan->nEquiv; j++){
163569 if( pScan->aiCur[j]==pX->iTable
163570 && pScan->aiColumn[j]==pX->iColumn ){
163571 break;
163572 }
163573 }
163574 if( j==pScan->nEquiv ){
163575 pScan->aiCur[j] = pX->iTable;
163576 pScan->aiColumn[j] = pX->iColumn;
163577 pScan->nEquiv++;
163578 }
163579 }
163580 if( (pTerm->eOperator & pScan->opMask)!=0 ){
163581 /* Verify the affinity and collating sequence match */
163582 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
163583 const char *zCollName;
163584 Parse *pParse = pWC->pWInfo->pParse;
163585 pX = pTerm->pExpr;
163586
163587 if( (pTerm->eOperator & WO_IN0x0001) ){
163588 zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
163589 if( !zCollName ) continue;
163590 }else{
163591 CollSeq *pColl;
163592 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
163593 continue;
163594 }
163595 assert(pX->pLeft)((void) (0));
163596 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
163597 zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
163598 }
163599
163600 if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
163601 continue;
163602 }
163603 }
163604 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))!=0
163605 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0)(pX!=0))
163606 && pX->op==TK_COLUMN168
163607 && pX->iTable==pScan->aiCur[0]
163608 && pX->iColumn==pScan->aiColumn[0]
163609 ){
163610 testcase( pTerm->eOperator & WO_IS );
163611 continue;
163612 }
163613 pScan->pWC = pWC;
163614 pScan->k = k+1;
163615#ifdef WHERETRACE_ENABLED
163616 if( sqlite3WhereTrace & 0x20000 ){
163617 int ii;
163618 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
163619 pTerm, pScan->nEquiv);
163620 for(ii=0; ii<pScan->nEquiv; ii++){
163621 sqlite3DebugPrintf(" {%d:%d}",
163622 pScan->aiCur[ii], pScan->aiColumn[ii]);
163623 }
163624 sqlite3DebugPrintf("\n");
163625 }
163626#endif
163627 return pTerm;
163628 }
163629 }
163630 }
163631 pWC = pWC->pOuter;
163632 k = 0;
163633 }while( pWC!=0 );
163634 if( pScan->iEquiv>=pScan->nEquiv ) break;
163635 pWC = pScan->pOrigWC;
163636 k = 0;
163637 pScan->iEquiv++;
163638 }
163639 return 0;
163640}
163641
163642/*
163643** This is whereScanInit() for the case of an index on an expression.
163644** It is factored out into a separate tail-recursion subroutine so that
163645** the normal whereScanInit() routine, which is a high-runner, does not
163646** need to push registers onto the stack as part of its prologue.
163647*/
163648static SQLITE_NOINLINE__attribute__((noinline)) WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
163649 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
163650 return whereScanNext(pScan);
163651}
163652
163653/*
163654** Initialize a WHERE clause scanner object. Return a pointer to the
163655** first match. Return NULL if there are no matches.
163656**
163657** The scanner will be searching the WHERE clause pWC. It will look
163658** for terms of the form "X <op> <expr>" where X is column iColumn of table
163659** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
163660** must be one of the indexes of table iCur.
163661**
163662** The <op> must be one of the operators described by opMask.
163663**
163664** If the search is for X and the WHERE clause contains terms of the
163665** form X=Y then this routine might also return terms of the form
163666** "Y <op> <expr>". The number of levels of transitivity is limited,
163667** but is enough to handle most commonly occurring SQL statements.
163668**
163669** If X is not the INTEGER PRIMARY KEY then X must be compatible with
163670** index pIdx.
163671*/
163672static WhereTerm *whereScanInit(
163673 WhereScan *pScan, /* The WhereScan object being initialized */
163674 WhereClause *pWC, /* The WHERE clause to be scanned */
163675 int iCur, /* Cursor to scan for */
163676 int iColumn, /* Column to scan for */
163677 u32 opMask, /* Operator(s) to scan for */
163678 Index *pIdx /* Must be compatible with this index */
163679){
163680 pScan->pOrigWC = pWC;
163681 pScan->pWC = pWC;
163682 pScan->pIdxExpr = 0;
163683 pScan->idxaff = 0;
163684 pScan->zCollName = 0;
163685 pScan->opMask = opMask;
163686 pScan->k = 0;
163687 pScan->aiCur[0] = iCur;
163688 pScan->nEquiv = 1;
163689 pScan->iEquiv = 1;
163690 if( pIdx ){
163691 int j = iColumn;
163692 iColumn = pIdx->aiColumn[j];
163693 if( iColumn==pIdx->pTable->iPKey ){
163694 iColumn = XN_ROWID(-1);
163695 }else if( iColumn>=0 ){
163696 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
163697 pScan->zCollName = pIdx->azColl[j];
163698 }else if( iColumn==XN_EXPR(-2) ){
163699 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
163700 pScan->zCollName = pIdx->azColl[j];
163701 pScan->aiColumn[0] = XN_EXPR(-2);
163702 return whereScanInitIndexExpr(pScan);
163703 }
163704 }else if( iColumn==XN_EXPR(-2) ){
163705 return 0;
163706 }
163707 pScan->aiColumn[0] = iColumn;
163708 return whereScanNext(pScan);
163709}
163710
163711/*
163712** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
163713** where X is a reference to the iColumn of table iCur or of index pIdx
163714** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
163715** the op parameter. Return a pointer to the term. Return 0 if not found.
163716**
163717** If pIdx!=0 then it must be one of the indexes of table iCur.
163718** Search for terms matching the iColumn-th column of pIdx
163719** rather than the iColumn-th column of table iCur.
163720**
163721** The term returned might by Y=<expr> if there is another constraint in
163722** the WHERE clause that specifies that X=Y. Any such constraints will be
163723** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
163724** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
163725** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
163726** other equivalent values. Hence a search for X will return <expr> if X=A1
163727** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
163728**
163729** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
163730** then try for the one with no dependencies on <expr> - in other words where
163731** <expr> is a constant expression of some kind. Only return entries of
163732** the form "X <op> Y" where Y is a column in another table if no terms of
163733** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
163734** exist, try to return a term that does not use WO_EQUIV.
163735*/
163736SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
163737 WhereClause *pWC, /* The WHERE clause to be searched */
163738 int iCur, /* Cursor number of LHS */
163739 int iColumn, /* Column number of LHS */
163740 Bitmask notReady, /* RHS must not overlap with this mask */
163741 u32 op, /* Mask of WO_xx values describing operator */
163742 Index *pIdx /* Must be compatible with this index, if not NULL */
163743){
163744 WhereTerm *pResult = 0;
163745 WhereTerm *p;
163746 WhereScan scan;
163747
163748 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
163749 op &= WO_EQ0x0002|WO_IS0x0080;
163750 while( p ){
163751 if( (p->prereqRight & notReady)==0 ){
163752 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
163753 testcase( p->eOperator & WO_IS );
163754 return p;
163755 }
163756 if( pResult==0 ) pResult = p;
163757 }
163758 p = whereScanNext(&scan);
163759 }
163760 return pResult;
163761}
163762
163763/*
163764** This function searches pList for an entry that matches the iCol-th column
163765** of index pIdx.
163766**
163767** If such an expression is found, its index in pList->a[] is returned. If
163768** no expression is found, -1 is returned.
163769*/
163770static int findIndexCol(
163771 Parse *pParse, /* Parse context */
163772 ExprList *pList, /* Expression list to search */
163773 int iBase, /* Cursor for table associated with pIdx */
163774 Index *pIdx, /* Index to match column of */
163775 int iCol /* Column of index to match */
163776){
163777 int i;
163778 const char *zColl = pIdx->azColl[iCol];
163779
163780 for(i=0; i<pList->nExpr; i++){
163781 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
163782 if( ALWAYS(p!=0)(p!=0)
163783 && (p->op==TK_COLUMN168 || p->op==TK_AGG_COLUMN170)
163784 && p->iColumn==pIdx->aiColumn[iCol]
163785 && p->iTable==iBase
163786 ){
163787 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
163788 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
163789 return i;
163790 }
163791 }
163792 }
163793
163794 return -1;
163795}
163796
163797/*
163798** Return TRUE if the iCol-th column of index pIdx is NOT NULL
163799*/
163800static int indexColumnNotNull(Index *pIdx, int iCol){
163801 int j;
163802 assert( pIdx!=0 )((void) (0));
163803 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
163804 j = pIdx->aiColumn[iCol];
163805 if( j>=0 ){
163806 return pIdx->pTable->aCol[j].notNull;
163807 }else if( j==(-1) ){
163808 return 1;
163809 }else{
163810 assert( j==(-2) )((void) (0));
163811 return 0; /* Assume an indexed expression can always yield a NULL */
163812
163813 }
163814}
163815
163816/*
163817** Return true if the DISTINCT expression-list passed as the third argument
163818** is redundant.
163819**
163820** A DISTINCT list is redundant if any subset of the columns in the
163821** DISTINCT list are collectively unique and individually non-null.
163822*/
163823static int isDistinctRedundant(
163824 Parse *pParse, /* Parsing context */
163825 SrcList *pTabList, /* The FROM clause */
163826 WhereClause *pWC, /* The WHERE clause */
163827 ExprList *pDistinct /* The result set that needs to be DISTINCT */
163828){
163829 Table *pTab;
163830 Index *pIdx;
163831 int i;
163832 int iBase;
163833
163834 /* If there is more than one table or sub-select in the FROM clause of
163835 ** this query, then it will not be possible to show that the DISTINCT
163836 ** clause is redundant. */
163837 if( pTabList->nSrc!=1 ) return 0;
163838 iBase = pTabList->a[0].iCursor;
163839 pTab = pTabList->a[0].pSTab;
163840
163841 /* If any of the expressions is an IPK column on table iBase, then return
163842 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
163843 ** current SELECT is a correlated sub-query.
163844 */
163845 for(i=0; i<pDistinct->nExpr; i++){
163846 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
163847 if( NEVER(p==0)(p==0) ) continue;
163848 if( p->op!=TK_COLUMN168 && p->op!=TK_AGG_COLUMN170 ) continue;
163849 if( p->iTable==iBase && p->iColumn<0 ) return 1;
163850 }
163851
163852 /* Loop through all indices on the table, checking each to see if it makes
163853 ** the DISTINCT qualifier redundant. It does so if:
163854 **
163855 ** 1. The index is itself UNIQUE, and
163856 **
163857 ** 2. All of the columns in the index are either part of the pDistinct
163858 ** list, or else the WHERE clause contains a term of the form "col=X",
163859 ** where X is a constant value. The collation sequences of the
163860 ** comparison and select-list expressions must match those of the index.
163861 **
163862 ** 3. All of those index columns for which the WHERE clause does not
163863 ** contain a "col=X" term are subject to a NOT NULL constraint.
163864 */
163865 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
163866 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
163867 if( pIdx->pPartIdxWhere ) continue;
163868 for(i=0; i<pIdx->nKeyCol; i++){
163869 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ0x0002, pIdx) ){
163870 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
163871 if( indexColumnNotNull(pIdx, i)==0 ) break;
163872 }
163873 }
163874 if( i==pIdx->nKeyCol ){
163875 /* This index implies that the DISTINCT qualifier is redundant. */
163876 return 1;
163877 }
163878 }
163879
163880 return 0;
163881}
163882
163883
163884/*
163885** Estimate the logarithm of the input value to base 2.
163886*/
163887static LogEst estLog(LogEst N){
163888 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
163889}
163890
163891/*
163892** Convert OP_Column opcodes to OP_Copy in previously generated code.
163893**
163894** This routine runs over generated VDBE code and translates OP_Column
163895** opcodes into OP_Copy when the table is being accessed via co-routine
163896** instead of via table lookup.
163897**
163898** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
163899** cursor iTabCur are transformed into OP_Sequence opcode for the
163900** iAutoidxCur cursor, in order to generate unique rowids for the
163901** automatic index being generated.
163902*/
163903static void translateColumnToCopy(
163904 Parse *pParse, /* Parsing context */
163905 int iStart, /* Translate from this opcode to the end */
163906 int iTabCur, /* OP_Column/OP_Rowid references to this table */
163907 int iRegister, /* The first column is in this register */
163908 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
163909){
163910 Vdbe *v = pParse->pVdbe;
163911 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
163912 int iEnd = sqlite3VdbeCurrentAddr(v);
163913 if( pParse->db->mallocFailed ) return;
163914#ifdef SQLITE_DEBUG
163915 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
163916 printf("CHECKING for column-to-copy on cursor %d for %d..%d\n",
163917 iTabCur, iStart, iEnd);
163918 }
163919#endif
163920 for(; iStart<iEnd; iStart++, pOp++){
163921 if( pOp->p1!=iTabCur ) continue;
163922 if( pOp->opcode==OP_Column94 ){
163923#ifdef SQLITE_DEBUG
163924 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
163925 printf("TRANSLATE OP_Column to OP_Copy at %d\n", iStart);
163926 }
163927#endif
163928 pOp->opcode = OP_Copy80;
163929 pOp->p1 = pOp->p2 + iRegister;
163930 pOp->p2 = pOp->p3;
163931 pOp->p3 = 0;
163932 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
163933 }else if( pOp->opcode==OP_Rowid135 ){
163934#ifdef SQLITE_DEBUG
163935 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
163936 printf("TRANSLATE OP_Rowid to OP_Sequence at %d\n", iStart);
163937 }
163938#endif
163939 pOp->opcode = OP_Sequence126;
163940 pOp->p1 = iAutoidxCur;
163941#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
163942 if( iAutoidxCur==0 ){
163943 pOp->opcode = OP_Null75;
163944 pOp->p3 = 0;
163945 }
163946#endif
163947 }
163948 }
163949}
163950
163951/*
163952** Two routines for printing the content of an sqlite3_index_info
163953** structure. Used for testing and debugging only. If neither
163954** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
163955** are no-ops.
163956*/
163957#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
163958static void whereTraceIndexInfoInputs(
163959 sqlite3_index_info *p, /* The IndexInfo object */
163960 Table *pTab /* The TABLE that is the virtual table */
163961){
163962 int i;
163963 if( (sqlite3WhereTrace & 0x10)==0 ) return;
163964 sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName);
163965 for(i=0; i<p->nConstraint; i++){
163966 sqlite3DebugPrintf(
163967 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
163968 i,
163969 p->aConstraint[i].iColumn,
163970 p->aConstraint[i].iTermOffset,
163971 p->aConstraint[i].op,
163972 p->aConstraint[i].usable,
163973 sqlite3_vtab_collation(p,i));
163974 }
163975 for(i=0; i<p->nOrderBy; i++){
163976 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
163977 i,
163978 p->aOrderBy[i].iColumn,
163979 p->aOrderBy[i].desc);
163980 }
163981}
163982static void whereTraceIndexInfoOutputs(
163983 sqlite3_index_info *p, /* The IndexInfo object */
163984 Table *pTab /* The TABLE that is the virtual table */
163985){
163986 int i;
163987 if( (sqlite3WhereTrace & 0x10)==0 ) return;
163988 sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName);
163989 for(i=0; i<p->nConstraint; i++){
163990 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
163991 i,
163992 p->aConstraintUsage[i].argvIndex,
163993 p->aConstraintUsage[i].omit);
163994 }
163995 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
163996 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
163997 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
163998 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
163999 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
164000}
164001#else
164002#define whereTraceIndexInfoInputs(A,B)
164003#define whereTraceIndexInfoOutputs(A,B)
164004#endif
164005
164006/*
164007** We know that pSrc is an operand of an outer join. Return true if
164008** pTerm is a constraint that is compatible with that join.
164009**
164010** pTerm must be EP_OuterON if pSrc is the right operand of an
164011** outer join. pTerm can be either EP_OuterON or EP_InnerON if pSrc
164012** is the left operand of a RIGHT join.
164013**
164014** See https://sqlite.org/forum/forumpost/206d99a16dd9212f
164015** for an example of a WHERE clause constraints that may not be used on
164016** the right table of a RIGHT JOIN because the constraint implies a
164017** not-NULL condition on the left table of the RIGHT JOIN.
164018*/
164019static int constraintCompatibleWithOuterJoin(
164020 const WhereTerm *pTerm, /* WHERE clause term to check */
164021 const SrcItem *pSrc /* Table we are trying to access */
164022){
164023 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 )((void) (0)); /* By caller */
164024 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
164025 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
164026 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
164027 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
164028 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)(((pTerm->pExpr)->flags&(0x000001|0x000002))!=0)
164029 || pTerm->pExpr->w.iJoin != pSrc->iCursor
164030 ){
164031 return 0;
164032 }
164033 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))!=0
164034 && ExprHasProperty(pTerm->pExpr, EP_InnerON)(((pTerm->pExpr)->flags&(0x000002))!=0)
164035 ){
164036 return 0;
164037 }
164038 return 1;
164039}
164040
164041#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
164042/*
164043** Return true if column iCol of table pTab seem like it might be a
164044** good column to use as part of a query-time index.
164045**
164046** Current algorithm (subject to improvement!):
164047**
164048** 1. If iCol is already the left-most column of some other index,
164049** then return false.
164050**
164051** 2. If iCol is part of an existing index that has an aiRowLogEst of
164052** more than 20, then return false.
164053**
164054** 3. If no disqualifying conditions above are found, return true.
164055*/
164056static SQLITE_NOINLINE__attribute__((noinline)) int columnIsGoodIndexCandidate(
164057 const Table *pTab,
164058 int iCol
164059){
164060 const Index *pIdx;
164061 for(pIdx = pTab->pIndex; pIdx!=0; pIdx=pIdx->pNext){
164062 int j;
164063 for(j=0; j<pIdx->nKeyCol; j++){
164064 if( pIdx->aiColumn[j]==iCol ){
164065 if( j==0 ) return 0;
164066 if( pIdx->hasStat1 && pIdx->aiRowLogEst[j+1]>20 ) return 0;
164067 break;
164068 }
164069 }
164070 }
164071 return 1;
164072}
164073#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
164074
164075
164076
164077#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
164078/*
164079** Return TRUE if the WHERE clause term pTerm is of a form where it
164080** could be used with an index to access pSrc, assuming an appropriate
164081** index existed.
164082*/
164083static int termCanDriveIndex(
164084 const WhereTerm *pTerm, /* WHERE clause term to check */
164085 const SrcItem *pSrc, /* Table we are trying to access */
164086 const Bitmask notReady /* Tables in outer loops of the join */
164087){
164088 char aff;
164089 int leftCol;
164090
164091 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
164092 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) return 0;
164093 assert( (pSrc->fg.jointype & JT_RIGHT)==0 )((void) (0));
164094 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0
164095 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
164096 ){
164097 return 0; /* See https://sqlite.org/forum/forumpost/51e6959f61 */
164098 }
164099 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
164100 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
164101 leftCol = pTerm->u.x.leftColumn;
164102 if( leftCol<0 ) return 0;
164103 aff = pSrc->pSTab->aCol[leftCol].affinity;
164104 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
164105 testcase( pTerm->pExpr->op==TK_IS );
164106 return columnIsGoodIndexCandidate(pSrc->pSTab, leftCol);
164107}
164108#endif
164109
164110
164111#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
164112
164113#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
164114/*
164115** Argument pIdx represents an automatic index that the current statement
164116** will create and populate. Add an OP_Explain with text of the form:
164117**
164118** CREATE AUTOMATIC INDEX ON <table>(<cols>) [WHERE <expr>]
164119**
164120** This is only required if sqlite3_stmt_scanstatus() is enabled, to
164121** associate an SQLITE_SCANSTAT_NCYCLE and SQLITE_SCANSTAT_NLOOP
164122** values with. In order to avoid breaking legacy code and test cases,
164123** the OP_Explain is not added if this is an EXPLAIN QUERY PLAN command.
164124*/
164125static void explainAutomaticIndex(
164126 Parse *pParse,
164127 Index *pIdx, /* Automatic index to explain */
164128 int bPartial, /* True if pIdx is a partial index */
164129 int *pAddrExplain /* OUT: Address of OP_Explain */
164130){
164131 if( IS_STMT_SCANSTATUS(pParse->db)(pParse->db->flags & 0x00000400) && pParse->explain!=2 ){
164132 Table *pTab = pIdx->pTable;
164133 const char *zSep = "";
164134 char *zText = 0;
164135 int ii = 0;
164136 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
164137 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
164138 assert( pIdx->nColumn>1 )((void) (0));
164139 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID )((void) (0));
164140 for(ii=0; ii<(pIdx->nColumn-1); ii++){
164141 const char *zName = 0;
164142 int iCol = pIdx->aiColumn[ii];
164143
164144 zName = pTab->aCol[iCol].zCnName;
164145 sqlite3_str_appendf(pStr, "%s%s", zSep, zName);
164146 zSep = ", ";
164147 }
164148 zText = sqlite3_str_finish(pStr);
164149 if( zText==0 ){
164150 sqlite3OomFault(pParse->db);
164151 }else{
164152 *pAddrExplain = sqlite3VdbeExplain(
164153 pParse, 0, "%s)%s", zText, (bPartial ? " WHERE <expr>" : "")
164154 );
164155 sqlite3_free(zText);
164156 }
164157 }
164158}
164159#else
164160# define explainAutomaticIndex(a,b,c,d)
164161#endif
164162
164163/*
164164** Generate code to construct the Index object for an automatic index
164165** and to set up the WhereLevel object pLevel so that the code generator
164166** makes use of the automatic index.
164167*/
164168static SQLITE_NOINLINE__attribute__((noinline)) void constructAutomaticIndex(
164169 Parse *pParse, /* The parsing context */
164170 WhereClause *pWC, /* The WHERE clause */
164171 const Bitmask notReady, /* Mask of cursors that are not available */
164172 WhereLevel *pLevel /* Write new index here */
164173){
164174 int nKeyCol; /* Number of columns in the constructed index */
164175 WhereTerm *pTerm; /* A single term of the WHERE clause */
164176 WhereTerm *pWCEnd; /* End of pWC->a[] */
164177 Index *pIdx; /* Object describing the transient index */
164178 Vdbe *v; /* Prepared statement under construction */
164179 int addrInit; /* Address of the initialization bypass jump */
164180 Table *pTable; /* The table being indexed */
164181 int addrTop; /* Top of the index fill loop */
164182 int regRecord; /* Register holding an index record */
164183 int n; /* Column counter */
164184 int i; /* Loop counter */
164185 int mxBitCol; /* Maximum column in pSrc->colUsed */
164186 CollSeq *pColl; /* Collating sequence to on a column */
164187 WhereLoop *pLoop; /* The Loop object */
164188 char *zNotUsed; /* Extra space on the end of pIdx */
164189 Bitmask idxCols; /* Bitmap of columns used for indexing */
164190 Bitmask extraCols; /* Bitmap of additional columns */
164191 u8 sentWarning = 0; /* True if a warning has been issued */
164192 u8 useBloomFilter = 0; /* True to also add a Bloom filter */
164193 Expr *pPartial = 0; /* Partial Index Expression */
164194 int iContinue = 0; /* Jump here to skip excluded rows */
164195 SrcList *pTabList; /* The complete FROM clause */
164196 SrcItem *pSrc; /* The FROM clause term to get the next index */
164197 int addrCounter = 0; /* Address where integer counter is initialized */
164198 int regBase; /* Array of registers where record is assembled */
164199#ifdef SQLITE_ENABLE_STMT_SCANSTATUS1
164200 int addrExp = 0; /* Address of OP_Explain */
164201#endif
164202
164203 /* Generate code to skip over the creation and initialization of the
164204 ** transient index on 2nd and subsequent iterations of the loop. */
164205 v = pParse->pVdbe;
164206 assert( v!=0 )((void) (0));
164207 addrInit = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
164208
164209 /* Count the number of columns that will be added to the index
164210 ** and used to match WHERE clause constraints */
164211 nKeyCol = 0;
164212 pTabList = pWC->pWInfo->pTabList;
164213 pSrc = &pTabList->a[pLevel->iFrom];
164214 pTable = pSrc->pSTab;
164215 pWCEnd = &pWC->a[pWC->nTerm];
164216 pLoop = pLevel->pWLoop;
164217 idxCols = 0;
164218 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
164219 Expr *pExpr = pTerm->pExpr;
164220 /* Make the automatic index a partial index if there are terms in the
164221 ** WHERE clause (or the ON clause of a LEFT join) that constrain which
164222 ** rows of the target table (pSrc) that can be used. */
164223 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)==0
164224 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom, 0)
164225 ){
164226 pPartial = sqlite3ExprAnd(pParse, pPartial,
164227 sqlite3ExprDup(pParse->db, pExpr, 0));
164228 }
164229 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
164230 int iCol;
164231 Bitmask cMask;
164232 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
164233 iCol = pTerm->u.x.leftColumn;
164234 cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
164235 testcase( iCol==BMS );
164236 testcase( iCol==BMS-1 );
164237 if( !sentWarning ){
164238 sqlite3_log(SQLITE_WARNING_AUTOINDEX(28 | (1<<8)),
164239 "automatic index on %s(%s)", pTable->zName,
164240 pTable->aCol[iCol].zCnName);
164241 sentWarning = 1;
164242 }
164243 if( (idxCols & cMask)==0 ){
164244 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
164245 goto end_auto_index_create;
164246 }
164247 pLoop->aLTerm[nKeyCol++] = pTerm;
164248 idxCols |= cMask;
164249 }
164250 }
164251 }
164252 assert( nKeyCol>0 || pParse->db->mallocFailed )((void) (0));
164253 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
164254 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001 | WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200
164255 | WHERE_AUTO_INDEX0x00004000;
164256
164257 /* Count the number of additional columns needed to create a
164258 ** covering index. A "covering index" is an index that contains all
164259 ** columns that are needed by the query. With a covering index, the
164260 ** original table never needs to be accessed. Automatic indices must
164261 ** be a covering index because the index will not be updated if the
164262 ** original table changes and the index and table cannot both be used
164263 ** if they go out of sync.
164264 */
164265 if( IsView(pTable)((pTable)->eTabType==2) ){
164266 extraCols = ALLBITS((Bitmask)-1) & ~idxCols;
164267 }else{
164268 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)));
164269 }
164270 mxBitCol = MIN(BMS-1,pTable->nCol)((((int)(sizeof(Bitmask)*8))-1)<(pTable->nCol)?(((int)(
sizeof(Bitmask)*8))-1):(pTable->nCol))
;
164271 testcase( pTable->nCol==BMS-1 );
164272 testcase( pTable->nCol==BMS-2 );
164273 for(i=0; i<mxBitCol; i++){
164274 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ) nKeyCol++;
164275 }
164276 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
164277 nKeyCol += pTable->nCol - BMS((int)(sizeof(Bitmask)*8)) + 1;
164278 }
164279
164280 /* Construct the Index object to describe this index */
164281 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
164282 if( pIdx==0 ) goto end_auto_index_create;
164283 pLoop->u.btree.pIndex = pIdx;
164284 pIdx->zName = "auto-index";
164285 pIdx->pTable = pTable;
164286 n = 0;
164287 idxCols = 0;
164288 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
164289 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
164290 int iCol;
164291 Bitmask cMask;
164292 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
164293 iCol = pTerm->u.x.leftColumn;
164294 cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
164295 testcase( iCol==BMS-1 );
164296 testcase( iCol==BMS );
164297 if( (idxCols & cMask)==0 ){
164298 Expr *pX = pTerm->pExpr;
164299 idxCols |= cMask;
164300 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
164301 pColl = sqlite3ExprCompareCollSeq(pParse, pX);
164302 assert( pColl!=0 || pParse->nErr>0 )((void) (0)); /* TH3 collate01.800 */
164303 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
164304 n++;
164305 if( ALWAYS(pX->pLeft!=0)(pX->pLeft!=0)
164306 && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT0x42
164307 ){
164308 /* TUNING: only use a Bloom filter on an automatic index
164309 ** if one or more key columns has the ability to hold numeric
164310 ** values, since strings all have the same hash in the Bloom
164311 ** filter implementation and hence a Bloom filter on a text column
164312 ** is not usually helpful. */
164313 useBloomFilter = 1;
164314 }
164315 }
164316 }
164317 }
164318 assert( (u32)n==pLoop->u.btree.nEq )((void) (0));
164319
164320 /* Add additional columns needed to make the automatic index into
164321 ** a covering index */
164322 for(i=0; i<mxBitCol; i++){
164323 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ){
164324 pIdx->aiColumn[n] = i;
164325 pIdx->azColl[n] = sqlite3StrBINARY;
164326 n++;
164327 }
164328 }
164329 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
164330 for(i=BMS((int)(sizeof(Bitmask)*8))-1; i<pTable->nCol; i++){
164331 pIdx->aiColumn[n] = i;
164332 pIdx->azColl[n] = sqlite3StrBINARY;
164333 n++;
164334 }
164335 }
164336 assert( n==nKeyCol )((void) (0));
164337 pIdx->aiColumn[n] = XN_ROWID(-1);
164338 pIdx->azColl[n] = sqlite3StrBINARY;
164339
164340 /* Create the automatic index */
164341 explainAutomaticIndex(pParse, pIdx, pPartial!=0, &addrExp);
164342 assert( pLevel->iIdxCur>=0 )((void) (0));
164343 pLevel->iIdxCur = pParse->nTab++;
164344 sqlite3VdbeAddOp2(v, OP_OpenAutoindex116, pLevel->iIdxCur, nKeyCol+1);
164345 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
164346 VdbeComment((v, "for %s", pTable->zName));
164347 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter)(((pParse->db)->dbOptFlags&(0x00080000))==0) && useBloomFilter ){
164348 sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel);
164349 pLevel->regFilter = ++pParse->nMem;
164350 sqlite3VdbeAddOp2(v, OP_Blob77, 10000, pLevel->regFilter);
164351 }
164352
164353 /* Fill the automatic index with content */
164354 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] )((void) (0));
164355 if( pSrc->fg.viaCoroutine ){
164356 int regYield;
164357 Subquery *pSubq;
164358 assert( pSrc->fg.isSubquery )((void) (0));
164359 pSubq = pSrc->u4.pSubq;
164360 assert( pSubq!=0 )((void) (0));
164361 regYield = pSubq->regReturn;
164362 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer71, 0, 0);
164363 sqlite3VdbeAddOp3(v, OP_InitCoroutine11, regYield, 0, pSubq->addrFillSub);
164364 addrTop = sqlite3VdbeAddOp1(v, OP_Yield12, regYield);
164365 VdbeCoverage(v);
164366 VdbeComment((v, "next row of %s", pSrc->pSTab->zName));
164367 }else{
164368 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, pLevel->iTabCur); VdbeCoverage(v);
164369 }
164370 if( pPartial ){
164371 iContinue = sqlite3VdbeMakeLabel(pParse);
164372 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL0x10);
164373 pLoop->wsFlags |= WHERE_PARTIALIDX0x00020000;
164374 }
164375 regRecord = sqlite3GetTempReg(pParse);
164376 regBase = sqlite3GenerateIndexKey(
164377 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
164378 );
164379 if( pLevel->regFilter ){
164380 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pLevel->regFilter, 0,
164381 regBase, pLoop->u.btree.nEq);
164382 }
164383 sqlite3VdbeScanStatusCounters(v, addrExp, addrExp, sqlite3VdbeCurrentAddr(v));
164384 sqlite3VdbeAddOp2(v, OP_IdxInsert138, pLevel->iIdxCur, regRecord);
164385 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
164386 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
164387 if( pSrc->fg.viaCoroutine ){
164388 assert( pSrc->fg.isSubquery && pSrc->u4.pSubq!=0 )((void) (0));
164389 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
164390 testcase( pParse->db->mallocFailed );
164391 assert( pLevel->iIdxCur>0 )((void) (0));
164392 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
164393 pSrc->u4.pSubq->regResult, pLevel->iIdxCur);
164394 sqlite3VdbeGoto(v, addrTop);
164395 pSrc->fg.viaCoroutine = 0;
164396 }else{
164397 sqlite3VdbeAddOp2(v, OP_Next39, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
164398 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX3);
164399 }
164400 sqlite3VdbeJumpHere(v, addrTop);
164401 sqlite3ReleaseTempReg(pParse, regRecord);
164402
164403 /* Jump here when skipping the initialization */
164404 sqlite3VdbeJumpHere(v, addrInit);
164405 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
164406
164407end_auto_index_create:
164408 sqlite3ExprDelete(pParse->db, pPartial);
164409}
164410#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
164411
164412/*
164413** Generate bytecode that will initialize a Bloom filter that is appropriate
164414** for pLevel.
164415**
164416** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER
164417** flag set, initialize a Bloomfilter for them as well. Except don't do
164418** this recursive initialization if the SQLITE_BloomPulldown optimization has
164419** been turned off.
164420**
164421** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared
164422** from the loop, but the regFilter value is set to a register that implements
164423** the Bloom filter. When regFilter is positive, the
164424** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter
164425** and skip the subsequence B-Tree seek if the Bloom filter indicates that
164426** no matching rows exist.
164427**
164428** This routine may only be called if it has previously been determined that
164429** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
164430** is set.
164431*/
164432static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ConstructBloomFilter(
164433 WhereInfo *pWInfo, /* The WHERE clause */
164434 int iLevel, /* Index in pWInfo->a[] that is pLevel */
164435 WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
164436 Bitmask notReady /* Loops that are not ready */
164437){
164438 int addrOnce; /* Address of opening OP_Once */
164439 int addrTop; /* Address of OP_Rewind */
164440 int addrCont; /* Jump here to skip a row */
164441 const WhereTerm *pTerm; /* For looping over WHERE clause terms */
164442 const WhereTerm *pWCEnd; /* Last WHERE clause term */
164443 Parse *pParse = pWInfo->pParse; /* Parsing context */
164444 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
164445 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
164446 int iCur; /* Cursor for table getting the filter */
164447 IndexedExpr *saved_pIdxEpr; /* saved copy of Parse.pIdxEpr */
164448 IndexedExpr *saved_pIdxPartExpr; /* saved copy of Parse.pIdxPartExpr */
164449
164450 saved_pIdxEpr = pParse->pIdxEpr;
164451 saved_pIdxPartExpr = pParse->pIdxPartExpr;
164452 pParse->pIdxEpr = 0;
164453 pParse->pIdxPartExpr = 0;
164454
164455 assert( pLoop!=0 )((void) (0));
164456 assert( v!=0 )((void) (0));
164457 assert( pLoop->wsFlags & WHERE_BLOOMFILTER )((void) (0));
164458 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 )((void) (0));
164459
164460 addrOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
164461 do{
164462 const SrcList *pTabList;
164463 const SrcItem *pItem;
164464 const Table *pTab;
164465 u64 sz;
164466 int iSrc;
164467 sqlite3WhereExplainBloomFilter(pParse, pWInfo, pLevel);
164468 addrCont = sqlite3VdbeMakeLabel(pParse);
164469 iCur = pLevel->iTabCur;
164470 pLevel->regFilter = ++pParse->nMem;
164471
164472 /* The Bloom filter is a Blob held in a register. Initialize it
164473 ** to zero-filled blob of at least 80K bits, but maybe more if the
164474 ** estimated size of the table is larger. We could actually
164475 ** measure the size of the table at run-time using OP_Count with
164476 ** P3==1 and use that value to initialize the blob. But that makes
164477 ** testing complicated. By basing the blob size on the value in the
164478 ** sqlite_stat1 table, testing is much easier.
164479 */
164480 pTabList = pWInfo->pTabList;
164481 iSrc = pLevel->iFrom;
164482 pItem = &pTabList->a[iSrc];
164483 assert( pItem!=0 )((void) (0));
164484 pTab = pItem->pSTab;
164485 assert( pTab!=0 )((void) (0));
164486 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
164487 if( sz<10000 ){
164488 sz = 10000;
164489 }else if( sz>10000000 ){
164490 sz = 10000000;
164491 }
164492 sqlite3VdbeAddOp2(v, OP_Blob77, (int)sz, pLevel->regFilter);
164493
164494 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
164495 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
164496 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
164497 Expr *pExpr = pTerm->pExpr;
164498 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)==0
164499 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, iSrc, 0)
164500 ){
164501 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
164502 }
164503 }
164504 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
164505 int r1 = sqlite3GetTempReg(pParse);
164506 sqlite3VdbeAddOp2(v, OP_Rowid135, iCur, r1);
164507 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pLevel->regFilter, 0, r1, 1);
164508 sqlite3ReleaseTempReg(pParse, r1);
164509 }else{
164510 Index *pIdx = pLoop->u.btree.pIndex;
164511 int n = pLoop->u.btree.nEq;
164512 int r1 = sqlite3GetTempRange(pParse, n);
164513 int jj;
164514 for(jj=0; jj<n; jj++){
164515 assert( pIdx->pTable==pItem->pSTab )((void) (0));
164516 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iCur, jj, r1+jj);
164517 }
164518 sqlite3VdbeAddOp4Int(v, OP_FilterAdd183, pLevel->regFilter, 0, r1, n);
164519 sqlite3ReleaseTempRange(pParse, r1, n);
164520 }
164521 sqlite3VdbeResolveLabel(v, addrCont);
164522 sqlite3VdbeAddOp2(v, OP_Next39, pLevel->iTabCur, addrTop+1);
164523 VdbeCoverage(v);
164524 sqlite3VdbeJumpHere(v, addrTop);
164525 pLoop->wsFlags &= ~WHERE_BLOOMFILTER0x00400000;
164526 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown)(((pParse->db)->dbOptFlags&(0x00100000))!=0) ) break;
164527 while( ++iLevel < pWInfo->nLevel ){
164528 const SrcItem *pTabItem;
164529 pLevel = &pWInfo->a[iLevel];
164530 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
164531 if( pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40) ) continue;
164532 pLoop = pLevel->pWLoop;
164533 if( NEVER(pLoop==0)(pLoop==0) ) continue;
164534 if( pLoop->prereq & notReady ) continue;
164535 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER0x00400000|WHERE_COLUMN_IN0x00000004))
164536 ==WHERE_BLOOMFILTER0x00400000
164537 ){
164538 /* This is a candidate for bloom-filter pull-down (early evaluation).
164539 ** The test that WHERE_COLUMN_IN is omitted is important, as we are
164540 ** not able to do early evaluation of bloom filters that make use of
164541 ** the IN operator */
164542 break;
164543 }
164544 }
164545 }while( iLevel < pWInfo->nLevel );
164546 sqlite3VdbeJumpHere(v, addrOnce);
164547 pParse->pIdxEpr = saved_pIdxEpr;
164548 pParse->pIdxPartExpr = saved_pIdxPartExpr;
164549}
164550
164551
164552#ifndef SQLITE_OMIT_VIRTUALTABLE
164553/*
164554** Return term iTerm of the WhereClause passed as the first argument. Terms
164555** are numbered from 0 upwards, starting with the terms in pWC->a[], then
164556** those in pWC->pOuter->a[] (if any), and so on.
164557*/
164558static WhereTerm *termFromWhereClause(WhereClause *pWC, int iTerm){
164559 WhereClause *p;
164560 for(p=pWC; p; p=p->pOuter){
164561 if( iTerm<p->nTerm ) return &p->a[iTerm];
164562 iTerm -= p->nTerm;
164563 }
164564 return 0;
164565}
164566
164567/*
164568** Allocate and populate an sqlite3_index_info structure. It is the
164569** responsibility of the caller to eventually release the structure
164570** by passing the pointer returned by this function to freeIndexInfo().
164571*/
164572static sqlite3_index_info *allocateIndexInfo(
164573 WhereInfo *pWInfo, /* The WHERE clause */
164574 WhereClause *pWC, /* The WHERE clause being analyzed */
164575 Bitmask mUnusable, /* Ignore terms with these prereqs */
164576 SrcItem *pSrc, /* The FROM clause term that is the vtab */
164577 u16 *pmNoOmit /* Mask of terms not to omit */
164578){
164579 int i, j;
164580 int nTerm;
164581 Parse *pParse = pWInfo->pParse;
164582 struct sqlite3_index_constraint *pIdxCons;
164583 struct sqlite3_index_orderby *pIdxOrderBy;
164584 struct sqlite3_index_constraint_usage *pUsage;
164585 struct HiddenIndexInfo *pHidden;
164586 WhereTerm *pTerm;
164587 int nOrderBy;
164588 sqlite3_index_info *pIdxInfo;
164589 u16 mNoOmit = 0;
164590 const Table *pTab;
164591 int eDistinct = 0;
164592 ExprList *pOrderBy = pWInfo->pOrderBy;
164593 WhereClause *p;
164594
164595 assert( pSrc!=0 )((void) (0));
164596 pTab = pSrc->pSTab;
164597 assert( pTab!=0 )((void) (0));
164598 assert( IsVirtual(pTab) )((void) (0));
164599
164600 /* Find all WHERE clause constraints referring to this virtual table.
164601 ** Mark each term with the TERM_OK flag. Set nTerm to the number of
164602 ** terms found.
164603 */
164604 for(p=pWC, nTerm=0; p; p=p->pOuter){
164605 for(i=0, pTerm=p->a; i<p->nTerm; i++, pTerm++){
164606 pTerm->wtFlags &= ~TERM_OK0x0040;
164607 if( pTerm->leftCursor != pSrc->iCursor ) continue;
164608 if( pTerm->prereqRight & mUnusable ) continue;
164609 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) )((void) (0));
164610 testcase( pTerm->eOperator & WO_IN );
164611 testcase( pTerm->eOperator & WO_ISNULL );
164612 testcase( pTerm->eOperator & WO_IS );
164613 testcase( pTerm->eOperator & WO_ALL );
164614 if( (pTerm->eOperator & ~(WO_EQUIV0x0800))==0 ) continue;
164615 if( pTerm->wtFlags & TERM_VNULL0x0080 ) continue;
164616
164617 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
164618 assert( pTerm->u.x.leftColumn>=XN_ROWID )((void) (0));
164619 assert( pTerm->u.x.leftColumn<pTab->nCol )((void) (0));
164620 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0
164621 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
164622 ){
164623 continue;
164624 }
164625 nTerm++;
164626 pTerm->wtFlags |= TERM_OK0x0040;
164627 }
164628 }
164629
164630 /* If the ORDER BY clause contains only columns in the current
164631 ** virtual table then allocate space for the aOrderBy part of
164632 ** the sqlite3_index_info structure.
164633 */
164634 nOrderBy = 0;
164635 if( pOrderBy ){
164636 int n = pOrderBy->nExpr;
164637 for(i=0; i<n; i++){
164638 Expr *pExpr = pOrderBy->a[i].pExpr;
164639 Expr *pE2;
164640
164641 /* Skip over constant terms in the ORDER BY clause */
164642 if( sqlite3ExprIsConstant(0, pExpr) ){
164643 continue;
164644 }
164645
164646 /* Virtual tables are unable to deal with NULLS FIRST */
164647 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ) break;
164648
164649 /* First case - a direct column references without a COLLATE operator */
164650 if( pExpr->op==TK_COLUMN168 && pExpr->iTable==pSrc->iCursor ){
164651 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol )((void) (0));
164652 continue;
164653 }
164654
164655 /* 2nd case - a column reference with a COLLATE operator. Only match
164656 ** of the COLLATE operator matches the collation of the column. */
164657 if( pExpr->op==TK_COLLATE114
164658 && (pE2 = pExpr->pLeft)->op==TK_COLUMN168
164659 && pE2->iTable==pSrc->iCursor
164660 ){
164661 const char *zColl; /* The collating sequence name */
164662 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
164663 assert( pExpr->u.zToken!=0 )((void) (0));
164664 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol )((void) (0));
164665 pExpr->iColumn = pE2->iColumn;
164666 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
164667 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
164668 if( zColl==0 ) zColl = sqlite3StrBINARY;
164669 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
164670 }
164671
164672 /* No matches cause a break out of the loop */
164673 break;
164674 }
164675 if( i==n ){
164676 nOrderBy = n;
164677 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080) && !pSrc->fg.rowidUsed ){
164678 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)!=0);
164679 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY0x0040 ){
164680 eDistinct = 1;
164681 }
164682 }
164683 }
164684
164685 /* Allocate the sqlite3_index_info structure
164686 */
164687 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
164688 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
164689 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden)
164690 + sizeof(sqlite3_value*)*nTerm );
164691 if( pIdxInfo==0 ){
164692 sqlite3ErrorMsg(pParse, "out of memory");
164693 return 0;
164694 }
164695 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
164696 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
164697 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
164698 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
164699 pIdxInfo->aConstraint = pIdxCons;
164700 pIdxInfo->aOrderBy = pIdxOrderBy;
164701 pIdxInfo->aConstraintUsage = pUsage;
164702 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
164703 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)==0 ){
164704 /* Ensure that all bits associated with PK columns are set. This is to
164705 ** ensure they are available for cases like RIGHT joins or OR loops. */
164706 Index *pPk = sqlite3PrimaryKeyIndex((Table*)pTab);
164707 assert( pPk!=0 )((void) (0));
164708 for(i=0; i<pPk->nKeyCol; i++){
164709 int iCol = pPk->aiColumn[i];
164710 assert( iCol>=0 )((void) (0));
164711 if( iCol>=BMS((int)(sizeof(Bitmask)*8))-1 ) iCol = BMS((int)(sizeof(Bitmask)*8))-1;
164712 pIdxInfo->colUsed |= MASKBIT(iCol)(((Bitmask)1)<<(iCol));
164713 }
164714 }
164715 pHidden->pWC = pWC;
164716 pHidden->pParse = pParse;
164717 pHidden->eDistinct = eDistinct;
164718 pHidden->mIn = 0;
164719 for(p=pWC, i=j=0; p; p=p->pOuter){
164720 int nLast = i+p->nTerm;;
164721 for(pTerm=p->a; i<nLast; i++, pTerm++){
164722 u16 op;
164723 if( (pTerm->wtFlags & TERM_OK0x0040)==0 ) continue;
164724 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
164725 pIdxCons[j].iTermOffset = i;
164726 op = pTerm->eOperator & WO_ALL0x3fff;
164727 if( op==WO_IN0x0001 ){
164728 if( (pTerm->wtFlags & TERM_SLICE0x8000)==0 ){
164729 pHidden->mIn |= SMASKBIT32(j)((j)<=31?((unsigned int)1)<<(j):0);
164730 }
164731 op = WO_EQ0x0002;
164732 }
164733 if( op==WO_AUX0x0040 ){
164734 pIdxCons[j].op = pTerm->eMatchOp;
164735 }else if( op & (WO_ISNULL0x0100|WO_IS0x0080) ){
164736 if( op==WO_ISNULL0x0100 ){
164737 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL71;
164738 }else{
164739 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS72;
164740 }
164741 }else{
164742 pIdxCons[j].op = (u8)op;
164743 /* The direct assignment in the previous line is possible only because
164744 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
164745 ** following asserts verify this fact. */
164746 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ )((void) (0));
164747 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT )((void) (0));
164748 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE )((void) (0));
164749 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT )((void) (0));
164750 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE )((void) (0));
164751 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) )((void) (0));
164752
164753 if( op & (WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54))|WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54)))
164754 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
164755 ){
164756 testcase( j!=i );
164757 if( j<16 ) mNoOmit |= (1 << j);
164758 if( op==WO_LT(0x0002<<(57 -54)) ) pIdxCons[j].op = WO_LE(0x0002<<(56 -54));
164759 if( op==WO_GT(0x0002<<(55 -54)) ) pIdxCons[j].op = WO_GE(0x0002<<(58 -54));
164760 }
164761 }
164762
164763 j++;
164764 }
164765 }
164766 assert( j==nTerm )((void) (0));
164767 pIdxInfo->nConstraint = j;
164768 for(i=j=0; i<nOrderBy; i++){
164769 Expr *pExpr = pOrderBy->a[i].pExpr;
164770 if( sqlite3ExprIsConstant(0, pExpr) ) continue;
164771 assert( pExpr->op==TK_COLUMN((void) (0))
164772 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN((void) (0))
164773 && pExpr->iColumn==pExpr->pLeft->iColumn) )((void) (0));
164774 pIdxOrderBy[j].iColumn = pExpr->iColumn;
164775 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC0x01;
164776 j++;
164777 }
164778 pIdxInfo->nOrderBy = j;
164779
164780 *pmNoOmit = mNoOmit;
164781 return pIdxInfo;
164782}
164783
164784/*
164785** Free and zero the sqlite3_index_info.idxStr value if needed.
164786*/
164787static void freeIdxStr(sqlite3_index_info *pIdxInfo){
164788 if( pIdxInfo->needToFreeIdxStr ){
164789 sqlite3_free(pIdxInfo->idxStr);
164790 pIdxInfo->idxStr = 0;
164791 pIdxInfo->needToFreeIdxStr = 0;
164792 }
164793}
164794
164795/*
164796** Free an sqlite3_index_info structure allocated by allocateIndexInfo()
164797** and possibly modified by xBestIndex methods.
164798*/
164799static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){
164800 HiddenIndexInfo *pHidden;
164801 int i;
164802 assert( pIdxInfo!=0 )((void) (0));
164803 pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
164804 assert( pHidden->pParse!=0 )((void) (0));
164805 assert( pHidden->pParse->db==db )((void) (0));
164806 for(i=0; i<pIdxInfo->nConstraint; i++){
164807 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
164808 pHidden->aRhs[i] = 0;
164809 }
164810 freeIdxStr(pIdxInfo);
164811 sqlite3DbFree(db, pIdxInfo);
164812}
164813
164814/*
164815** The table object reference passed as the second argument to this function
164816** must represent a virtual table. This function invokes the xBestIndex()
164817** method of the virtual table with the sqlite3_index_info object that
164818** comes in as the 3rd argument to this function.
164819**
164820** If an error occurs, pParse is populated with an error message and an
164821** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
164822** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
164823** the current configuration of "unusable" flags in sqlite3_index_info can
164824** not result in a valid plan.
164825**
164826** Whether or not an error is returned, it is the responsibility of the
164827** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
164828** that this is required.
164829*/
164830static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
164831 int rc;
164832 sqlite3_vtab *pVtab;
164833
164834 assert( IsVirtual(pTab) )((void) (0));
164835 pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
164836 whereTraceIndexInfoInputs(p, pTab);
164837 pParse->db->nSchemaLock++;
164838 rc = pVtab->pModule->xBestIndex(pVtab, p);
164839 pParse->db->nSchemaLock--;
164840 whereTraceIndexInfoOutputs(p, pTab);
164841
164842 if( rc!=SQLITE_OK0 && rc!=SQLITE_CONSTRAINT19 ){
164843 if( rc==SQLITE_NOMEM7 ){
164844 sqlite3OomFault(pParse->db);
164845 }else if( !pVtab->zErrMsg ){
164846 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
164847 }else{
164848 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
164849 }
164850 }
164851 if( pTab->u.vtab.p->bAllSchemas ){
164852 sqlite3VtabUsesAllSchemas(pParse);
164853 }
164854 sqlite3_free(pVtab->zErrMsg);
164855 pVtab->zErrMsg = 0;
164856 return rc;
164857}
164858#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
164859
164860#ifdef SQLITE_ENABLE_STAT4
164861/*
164862** Estimate the location of a particular key among all keys in an
164863** index. Store the results in aStat as follows:
164864**
164865** aStat[0] Est. number of rows less than pRec
164866** aStat[1] Est. number of rows equal to pRec
164867**
164868** Return the index of the sample that is the smallest sample that
164869** is greater than or equal to pRec. Note that this index is not an index
164870** into the aSample[] array - it is an index into a virtual set of samples
164871** based on the contents of aSample[] and the number of fields in record
164872** pRec.
164873*/
164874static int whereKeyStats(
164875 Parse *pParse, /* Database connection */
164876 Index *pIdx, /* Index to consider domain of */
164877 UnpackedRecord *pRec, /* Vector of values to consider */
164878 int roundUp, /* Round up if true. Round down if false */
164879 tRowcnt *aStat /* OUT: stats written here */
164880){
164881 IndexSample *aSample = pIdx->aSample;
164882 int iCol; /* Index of required stats in anEq[] etc. */
164883 int i; /* Index of first sample >= pRec */
164884 int iSample; /* Smallest sample larger than or equal to pRec */
164885 int iMin = 0; /* Smallest sample not yet tested */
164886 int iTest; /* Next sample to test */
164887 int res; /* Result of comparison operation */
164888 int nField; /* Number of fields in pRec */
164889 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
164890
164891#ifndef SQLITE_DEBUG
164892 UNUSED_PARAMETER( pParse )(void)(pParse);
164893#endif
164894 assert( pRec!=0 )((void) (0));
164895 assert( pIdx->nSample>0 )((void) (0));
164896 assert( pRec->nField>0 )((void) (0));
164897
164898
164899 /* Do a binary search to find the first sample greater than or equal
164900 ** to pRec. If pRec contains a single field, the set of samples to search
164901 ** is simply the aSample[] array. If the samples in aSample[] contain more
164902 ** than one fields, all fields following the first are ignored.
164903 **
164904 ** If pRec contains N fields, where N is more than one, then as well as the
164905 ** samples in aSample[] (truncated to N fields), the search also has to
164906 ** consider prefixes of those samples. For example, if the set of samples
164907 ** in aSample is:
164908 **
164909 ** aSample[0] = (a, 5)
164910 ** aSample[1] = (a, 10)
164911 ** aSample[2] = (b, 5)
164912 ** aSample[3] = (c, 100)
164913 ** aSample[4] = (c, 105)
164914 **
164915 ** Then the search space should ideally be the samples above and the
164916 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
164917 ** the code actually searches this set:
164918 **
164919 ** 0: (a)
164920 ** 1: (a, 5)
164921 ** 2: (a, 10)
164922 ** 3: (a, 10)
164923 ** 4: (b)
164924 ** 5: (b, 5)
164925 ** 6: (c)
164926 ** 7: (c, 100)
164927 ** 8: (c, 105)
164928 ** 9: (c, 105)
164929 **
164930 ** For each sample in the aSample[] array, N samples are present in the
164931 ** effective sample array. In the above, samples 0 and 1 are based on
164932 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
164933 **
164934 ** Often, sample i of each block of N effective samples has (i+1) fields.
164935 ** Except, each sample may be extended to ensure that it is greater than or
164936 ** equal to the previous sample in the array. For example, in the above,
164937 ** sample 2 is the first sample of a block of N samples, so at first it
164938 ** appears that it should be 1 field in size. However, that would make it
164939 ** smaller than sample 1, so the binary search would not work. As a result,
164940 ** it is extended to two fields. The duplicates that this creates do not
164941 ** cause any problems.
164942 */
164943 if( !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
164944 nField = pIdx->nKeyCol;
164945 }else{
164946 nField = pIdx->nColumn;
164947 }
164948 nField = MIN(pRec->nField, nField)((pRec->nField)<(nField)?(pRec->nField):(nField));
164949 iCol = 0;
164950 iSample = pIdx->nSample * nField;
164951 do{
164952 int iSamp; /* Index in aSample[] of test sample */
164953 int n; /* Number of fields in test sample */
164954
164955 iTest = (iMin+iSample)/2;
164956 iSamp = iTest / nField;
164957 if( iSamp>0 ){
164958 /* The proposed effective sample is a prefix of sample aSample[iSamp].
164959 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
164960 ** fields that is greater than the previous effective sample. */
164961 for(n=(iTest % nField) + 1; n<nField; n++){
164962 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
164963 }
164964 }else{
164965 n = iTest + 1;
164966 }
164967
164968 pRec->nField = n;
164969 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
164970 if( res<0 ){
164971 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
164972 iMin = iTest+1;
164973 }else if( res==0 && n<nField ){
164974 iLower = aSample[iSamp].anLt[n-1];
164975 iMin = iTest+1;
164976 res = -1;
164977 }else{
164978 iSample = iTest;
164979 iCol = n-1;
164980 }
164981 }while( res && iMin<iSample );
164982 i = iSample / nField;
164983
164984#ifdef SQLITE_DEBUG
164985 /* The following assert statements check that the binary search code
164986 ** above found the right answer. This block serves no purpose other
164987 ** than to invoke the asserts. */
164988 if( pParse->db->mallocFailed==0 ){
164989 if( res==0 ){
164990 /* If (res==0) is true, then pRec must be equal to sample i. */
164991 assert( i<pIdx->nSample )((void) (0));
164992 assert( iCol==nField-1 )((void) (0));
164993 pRec->nField = nField;
164994 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)((void) (0))
164995 || pParse->db->mallocFailed((void) (0))
164996 )((void) (0));
164997 }else{
164998 /* Unless i==pIdx->nSample, indicating that pRec is larger than
164999 ** all samples in the aSample[] array, pRec must be smaller than the
165000 ** (iCol+1) field prefix of sample i. */
165001 assert( i<=pIdx->nSample && i>=0 )((void) (0));
165002 pRec->nField = iCol+1;
165003 assert( i==pIdx->nSample((void) (0))
165004 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0((void) (0))
165005 || pParse->db->mallocFailed )((void) (0));
165006
165007 /* if i==0 and iCol==0, then record pRec is smaller than all samples
165008 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
165009 ** be greater than or equal to the (iCol) field prefix of sample i.
165010 ** If (i>0), then pRec must also be greater than sample (i-1). */
165011 if( iCol>0 ){
165012 pRec->nField = iCol;
165013 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0((void) (0))
165014 || pParse->db->mallocFailed || CORRUPT_DB )((void) (0));
165015 }
165016 if( i>0 ){
165017 pRec->nField = nField;
165018 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0((void) (0))
165019 || pParse->db->mallocFailed || CORRUPT_DB )((void) (0));
165020 }
165021 }
165022 }
165023#endif /* ifdef SQLITE_DEBUG */
165024
165025 if( res==0 ){
165026 /* Record pRec is equal to sample i */
165027 assert( iCol==nField-1 )((void) (0));
165028 aStat[0] = aSample[i].anLt[iCol];
165029 aStat[1] = aSample[i].anEq[iCol];
165030 }else{
165031 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
165032 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
165033 ** is larger than all samples in the array. */
165034 tRowcnt iUpper, iGap;
165035 if( i>=pIdx->nSample ){
165036 iUpper = pIdx->nRowEst0;
165037 }else{
165038 iUpper = aSample[i].anLt[iCol];
165039 }
165040
165041 if( iLower>=iUpper ){
165042 iGap = 0;
165043 }else{
165044 iGap = iUpper - iLower;
165045 }
165046 if( roundUp ){
165047 iGap = (iGap*2)/3;
165048 }else{
165049 iGap = iGap/3;
165050 }
165051 aStat[0] = iLower + iGap;
165052 aStat[1] = pIdx->aAvgEq[nField-1];
165053 }
165054
165055 /* Restore the pRec->nField value before returning. */
165056 pRec->nField = nField;
165057 return i;
165058}
165059#endif /* SQLITE_ENABLE_STAT4 */
165060
165061/*
165062** If it is not NULL, pTerm is a term that provides an upper or lower
165063** bound on a range scan. Without considering pTerm, it is estimated
165064** that the scan will visit nNew rows. This function returns the number
165065** estimated to be visited after taking pTerm into account.
165066**
165067** If the user explicitly specified a likelihood() value for this term,
165068** then the return value is the likelihood multiplied by the number of
165069** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
165070** has a likelihood of 0.50, and any other term a likelihood of 0.25.
165071*/
165072static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
165073 LogEst nRet = nNew;
165074 if( pTerm ){
165075 if( pTerm->truthProb<=0 ){
165076 nRet += pTerm->truthProb;
165077 }else if( (pTerm->wtFlags & TERM_VNULL0x0080)==0 ){
165078 nRet -= 20; assert( 20==sqlite3LogEst(4) )((void) (0));
165079 }
165080 }
165081 return nRet;
165082}
165083
165084
165085#ifdef SQLITE_ENABLE_STAT4
165086/*
165087** Return the affinity for a single column of an index.
165088*/
165089SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
165090 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
165091 if( !pIdx->zColAff ){
165092 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB0x41;
165093 }
165094 assert( pIdx->zColAff[iCol]!=0 )((void) (0));
165095 return pIdx->zColAff[iCol];
165096}
165097#endif
165098
165099
165100#ifdef SQLITE_ENABLE_STAT4
165101/*
165102** This function is called to estimate the number of rows visited by a
165103** range-scan on a skip-scan index. For example:
165104**
165105** CREATE INDEX i1 ON t1(a, b, c);
165106** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
165107**
165108** Value pLoop->nOut is currently set to the estimated number of rows
165109** visited for scanning (a=? AND b=?). This function reduces that estimate
165110** by some factor to account for the (c BETWEEN ? AND ?) expression based
165111** on the stat4 data for the index. this scan will be performed multiple
165112** times (once for each (a,b) combination that matches a=?) is dealt with
165113** by the caller.
165114**
165115** It does this by scanning through all stat4 samples, comparing values
165116** extracted from pLower and pUpper with the corresponding column in each
165117** sample. If L and U are the number of samples found to be less than or
165118** equal to the values extracted from pLower and pUpper respectively, and
165119** N is the total number of samples, the pLoop->nOut value is adjusted
165120** as follows:
165121**
165122** nOut = nOut * ( min(U - L, 1) / N )
165123**
165124** If pLower is NULL, or a value cannot be extracted from the term, L is
165125** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
165126** U is set to N.
165127**
165128** Normally, this function sets *pbDone to 1 before returning. However,
165129** if no value can be extracted from either pLower or pUpper (and so the
165130** estimate of the number of rows delivered remains unchanged), *pbDone
165131** is left as is.
165132**
165133** If an error occurs, an SQLite error code is returned. Otherwise,
165134** SQLITE_OK.
165135*/
165136static int whereRangeSkipScanEst(
165137 Parse *pParse, /* Parsing & code generating context */
165138 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
165139 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
165140 WhereLoop *pLoop, /* Update the .nOut value of this loop */
165141 int *pbDone /* Set to true if at least one expr. value extracted */
165142){
165143 Index *p = pLoop->u.btree.pIndex;
165144 int nEq = pLoop->u.btree.nEq;
165145 sqlite3 *db = pParse->db;
165146 int nLower = -1;
165147 int nUpper = p->nSample+1;
165148 int rc = SQLITE_OK0;
165149 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
165150 CollSeq *pColl;
165151
165152 sqlite3_value *p1 = 0; /* Value extracted from pLower */
165153 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
165154 sqlite3_value *pVal = 0; /* Value extracted from record */
165155
165156 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
165157 if( pLower ){
165158 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
165159 nLower = 0;
165160 }
165161 if( pUpper && rc==SQLITE_OK0 ){
165162 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
165163 nUpper = p2 ? 0 : p->nSample;
165164 }
165165
165166 if( p1 || p2 ){
165167 int i;
165168 int nDiff;
165169 for(i=0; rc==SQLITE_OK0 && i<p->nSample; i++){
165170 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
165171 if( rc==SQLITE_OK0 && p1 ){
165172 int res = sqlite3MemCompare(p1, pVal, pColl);
165173 if( res>=0 ) nLower++;
165174 }
165175 if( rc==SQLITE_OK0 && p2 ){
165176 int res = sqlite3MemCompare(p2, pVal, pColl);
165177 if( res>=0 ) nUpper++;
165178 }
165179 }
165180 nDiff = (nUpper - nLower);
165181 if( nDiff<=0 ) nDiff = 1;
165182
165183 /* If there is both an upper and lower bound specified, and the
165184 ** comparisons indicate that they are close together, use the fallback
165185 ** method (assume that the scan visits 1/64 of the rows) for estimating
165186 ** the number of rows visited. Otherwise, estimate the number of rows
165187 ** using the method described in the header comment for this function. */
165188 if( nDiff!=1 || pUpper==0 || pLower==0 ){
165189 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
165190 pLoop->nOut -= nAdjust;
165191 *pbDone = 1;
165192 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
165193 nLower, nUpper, nAdjust*-1, pLoop->nOut));
165194 }
165195
165196 }else{
165197 assert( *pbDone==0 )((void) (0));
165198 }
165199
165200 sqlite3ValueFree(p1);
165201 sqlite3ValueFree(p2);
165202 sqlite3ValueFree(pVal);
165203
165204 return rc;
165205}
165206#endif /* SQLITE_ENABLE_STAT4 */
165207
165208/*
165209** This function is used to estimate the number of rows that will be visited
165210** by scanning an index for a range of values. The range may have an upper
165211** bound, a lower bound, or both. The WHERE clause terms that set the upper
165212** and lower bounds are represented by pLower and pUpper respectively. For
165213** example, assuming that index p is on t1(a):
165214**
165215** ... FROM t1 WHERE a > ? AND a < ? ...
165216** |_____| |_____|
165217** | |
165218** pLower pUpper
165219**
165220** If either of the upper or lower bound is not present, then NULL is passed in
165221** place of the corresponding WhereTerm.
165222**
165223** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
165224** column subject to the range constraint. Or, equivalently, the number of
165225** equality constraints optimized by the proposed index scan. For example,
165226** assuming index p is on t1(a, b), and the SQL query is:
165227**
165228** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
165229**
165230** then nEq is set to 1 (as the range restricted column, b, is the second
165231** left-most column of the index). Or, if the query is:
165232**
165233** ... FROM t1 WHERE a > ? AND a < ? ...
165234**
165235** then nEq is set to 0.
165236**
165237** When this function is called, *pnOut is set to the sqlite3LogEst() of the
165238** number of rows that the index scan is expected to visit without
165239** considering the range constraints. If nEq is 0, then *pnOut is the number of
165240** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
165241** to account for the range constraints pLower and pUpper.
165242**
165243** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
165244** used, a single range inequality reduces the search space by a factor of 4.
165245** and a pair of constraints (x>? AND x<?) reduces the expected number of
165246** rows visited by a factor of 64.
165247*/
165248static int whereRangeScanEst(
165249 Parse *pParse, /* Parsing & code generating context */
165250 WhereLoopBuilder *pBuilder,
165251 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
165252 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
165253 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
165254){
165255 int rc = SQLITE_OK0;
165256 int nOut = pLoop->nOut;
165257 LogEst nNew;
165258
165259#ifdef SQLITE_ENABLE_STAT4
165260 Index *p = pLoop->u.btree.pIndex;
165261 int nEq = pLoop->u.btree.nEq;
165262
165263 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)(nEq<p->nSampleCol)
165264 && OptimizationEnabled(pParse->db, SQLITE_Stat4)(((pParse->db)->dbOptFlags&(0x00000800))==0)
165265 ){
165266 if( nEq==pBuilder->nRecValid ){
165267 UnpackedRecord *pRec = pBuilder->pRec;
165268 tRowcnt a[2];
165269 int nBtm = pLoop->u.btree.nBtm;
165270 int nTop = pLoop->u.btree.nTop;
165271
165272 /* Variable iLower will be set to the estimate of the number of rows in
165273 ** the index that are less than the lower bound of the range query. The
165274 ** lower bound being the concatenation of $P and $L, where $P is the
165275 ** key-prefix formed by the nEq values matched against the nEq left-most
165276 ** columns of the index, and $L is the value in pLower.
165277 **
165278 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
165279 ** is not a simple variable or literal value), the lower bound of the
165280 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
165281 ** if $L is available, whereKeyStats() is called for both ($P) and
165282 ** ($P:$L) and the larger of the two returned values is used.
165283 **
165284 ** Similarly, iUpper is to be set to the estimate of the number of rows
165285 ** less than the upper bound of the range query. Where the upper bound
165286 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
165287 ** of iUpper are requested of whereKeyStats() and the smaller used.
165288 **
165289 ** The number of rows between the two bounds is then just iUpper-iLower.
165290 */
165291 tRowcnt iLower; /* Rows less than the lower bound */
165292 tRowcnt iUpper; /* Rows less than the upper bound */
165293 int iLwrIdx = -2; /* aSample[] for the lower bound */
165294 int iUprIdx = -1; /* aSample[] for the upper bound */
165295
165296 if( pRec ){
165297 testcase( pRec->nField!=pBuilder->nRecValid );
165298 pRec->nField = pBuilder->nRecValid;
165299 }
165300 /* Determine iLower and iUpper using ($P) only. */
165301 if( nEq==0 ){
165302 iLower = 0;
165303 iUpper = p->nRowEst0;
165304 }else{
165305 /* Note: this call could be optimized away - since the same values must
165306 ** have been requested when testing key $P in whereEqualScanEst(). */
165307 whereKeyStats(pParse, p, pRec, 0, a);
165308 iLower = a[0];
165309 iUpper = a[0] + a[1];
165310 }
165311
165312 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 )((void) (0));
165313 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 )((void) (0));
165314 assert( p->aSortOrder!=0 )((void) (0));
165315 if( p->aSortOrder[nEq] ){
165316 /* The roles of pLower and pUpper are swapped for a DESC index */
165317 SWAP(WhereTerm*, pLower, pUpper){WhereTerm* t=pLower; pLower=pUpper; pUpper=t;};
165318 SWAP(int, nBtm, nTop){int t=nBtm; nBtm=nTop; nTop=t;};
165319 }
165320
165321 /* If possible, improve on the iLower estimate using ($P:$L). */
165322 if( pLower ){
165323 int n; /* Values extracted from pExpr */
165324 Expr *pExpr = pLower->pExpr->pRight;
165325 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
165326 if( rc==SQLITE_OK0 && n ){
165327 tRowcnt iNew;
165328 u16 mask = WO_GT(0x0002<<(55 -54))|WO_LE(0x0002<<(56 -54));
165329 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(56 -54))|WO_LT(0x0002<<(57 -54)));
165330 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
165331 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
165332 if( iNew>iLower ) iLower = iNew;
165333 nOut--;
165334 pLower = 0;
165335 }
165336 }
165337
165338 /* If possible, improve on the iUpper estimate using ($P:$U). */
165339 if( pUpper ){
165340 int n; /* Values extracted from pExpr */
165341 Expr *pExpr = pUpper->pExpr->pRight;
165342 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
165343 if( rc==SQLITE_OK0 && n ){
165344 tRowcnt iNew;
165345 u16 mask = WO_GT(0x0002<<(55 -54))|WO_LE(0x0002<<(56 -54));
165346 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(56 -54))|WO_LT(0x0002<<(57 -54)));
165347 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
165348 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
165349 if( iNew<iUpper ) iUpper = iNew;
165350 nOut--;
165351 pUpper = 0;
165352 }
165353 }
165354
165355 pBuilder->pRec = pRec;
165356 if( rc==SQLITE_OK0 ){
165357 if( iUpper>iLower ){
165358 nNew = sqlite3LogEst(iUpper - iLower);
165359 /* TUNING: If both iUpper and iLower are derived from the same
165360 ** sample, then assume they are 4x more selective. This brings
165361 ** the estimated selectivity more in line with what it would be
165362 ** if estimated without the use of STAT4 tables. */
165363 if( iLwrIdx==iUprIdx ){ nNew -= 20; }
165364 assert( 20==sqlite3LogEst(4) )((void) (0));
165365 }else{
165366 nNew = 10; assert( 10==sqlite3LogEst(2) )((void) (0));
165367 }
165368 if( nNew<nOut ){
165369 nOut = nNew;
165370 }
165371 WHERETRACE(0x20, ("STAT4 range scan: %u..%u est=%d\n",
165372 (u32)iLower, (u32)iUpper, nOut));
165373 }
165374 }else{
165375 int bDone = 0;
165376 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
165377 if( bDone ) return rc;
165378 }
165379 }
165380#else
165381 UNUSED_PARAMETER(pParse)(void)(pParse);
165382 UNUSED_PARAMETER(pBuilder)(void)(pBuilder);
165383 assert( pLower || pUpper )((void) (0));
165384#endif
165385 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 )((void) (0));
165386 nNew = whereRangeAdjust(pLower, nOut);
165387 nNew = whereRangeAdjust(pUpper, nNew);
165388
165389 /* TUNING: If there is both an upper and lower limit and neither limit
165390 ** has an application-defined likelihood(), assume the range is
165391 ** reduced by an additional 75%. This means that, by default, an open-ended
165392 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
165393 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
165394 ** match 1/64 of the index. */
165395 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
165396 nNew -= 20;
165397 }
165398
165399 nOut -= (pLower!=0) + (pUpper!=0);
165400 if( nNew<10 ) nNew = 10;
165401 if( nNew<nOut ) nOut = nNew;
165402#if defined(WHERETRACE_ENABLED)
165403 if( pLoop->nOut>nOut ){
165404 WHERETRACE(0x20,("Range scan lowers nOut from %d to %d\n",
165405 pLoop->nOut, nOut));
165406 }
165407#endif
165408 pLoop->nOut = (LogEst)nOut;
165409 return rc;
165410}
165411
165412#ifdef SQLITE_ENABLE_STAT4
165413/*
165414** Estimate the number of rows that will be returned based on
165415** an equality constraint x=VALUE and where that VALUE occurs in
165416** the histogram data. This only works when x is the left-most
165417** column of an index and sqlite_stat4 histogram data is available
165418** for that index. When pExpr==NULL that means the constraint is
165419** "x IS NULL" instead of "x=VALUE".
165420**
165421** Write the estimated row count into *pnRow and return SQLITE_OK.
165422** If unable to make an estimate, leave *pnRow unchanged and return
165423** non-zero.
165424**
165425** This routine can fail if it is unable to load a collating sequence
165426** required for string comparison, or if unable to allocate memory
165427** for a UTF conversion required for comparison. The error is stored
165428** in the pParse structure.
165429*/
165430static int whereEqualScanEst(
165431 Parse *pParse, /* Parsing & code generating context */
165432 WhereLoopBuilder *pBuilder,
165433 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
165434 tRowcnt *pnRow /* Write the revised row estimate here */
165435){
165436 Index *p = pBuilder->pNew->u.btree.pIndex;
165437 int nEq = pBuilder->pNew->u.btree.nEq;
165438 UnpackedRecord *pRec = pBuilder->pRec;
165439 int rc; /* Subfunction return code */
165440 tRowcnt a[2]; /* Statistics */
165441 int bOk;
165442
165443 assert( nEq>=1 )((void) (0));
165444 assert( nEq<=p->nColumn )((void) (0));
165445 assert( p->aSample!=0 )((void) (0));
165446 assert( p->nSample>0 )((void) (0));
165447 assert( pBuilder->nRecValid<nEq )((void) (0));
165448
165449 /* If values are not available for all fields of the index to the left
165450 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
165451 if( pBuilder->nRecValid<(nEq-1) ){
165452 return SQLITE_NOTFOUND12;
165453 }
165454
165455 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
165456 ** below would return the same value. */
165457 if( nEq>=p->nColumn ){
165458 *pnRow = 1;
165459 return SQLITE_OK0;
165460 }
165461
165462 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
165463 pBuilder->pRec = pRec;
165464 if( rc!=SQLITE_OK0 ) return rc;
165465 if( bOk==0 ) return SQLITE_NOTFOUND12;
165466 pBuilder->nRecValid = nEq;
165467
165468 whereKeyStats(pParse, p, pRec, 0, a);
165469 WHERETRACE(0x20,("equality scan regions %s(%d): %d\n",
165470 p->zName, nEq-1, (int)a[1]));
165471 *pnRow = a[1];
165472
165473 return rc;
165474}
165475#endif /* SQLITE_ENABLE_STAT4 */
165476
165477#ifdef SQLITE_ENABLE_STAT4
165478/*
165479** Estimate the number of rows that will be returned based on
165480** an IN constraint where the right-hand side of the IN operator
165481** is a list of values. Example:
165482**
165483** WHERE x IN (1,2,3,4)
165484**
165485** Write the estimated row count into *pnRow and return SQLITE_OK.
165486** If unable to make an estimate, leave *pnRow unchanged and return
165487** non-zero.
165488**
165489** This routine can fail if it is unable to load a collating sequence
165490** required for string comparison, or if unable to allocate memory
165491** for a UTF conversion required for comparison. The error is stored
165492** in the pParse structure.
165493*/
165494static int whereInScanEst(
165495 Parse *pParse, /* Parsing & code generating context */
165496 WhereLoopBuilder *pBuilder,
165497 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
165498 tRowcnt *pnRow /* Write the revised row estimate here */
165499){
165500 Index *p = pBuilder->pNew->u.btree.pIndex;
165501 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
165502 int nRecValid = pBuilder->nRecValid;
165503 int rc = SQLITE_OK0; /* Subfunction return code */
165504 tRowcnt nEst; /* Number of rows for a single term */
165505 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
165506 int i; /* Loop counter */
165507
165508 assert( p->aSample!=0 )((void) (0));
165509 for(i=0; rc==SQLITE_OK0 && i<pList->nExpr; i++){
165510 nEst = nRow0;
165511 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
165512 nRowEst += nEst;
165513 pBuilder->nRecValid = nRecValid;
165514 }
165515
165516 if( rc==SQLITE_OK0 ){
165517 if( nRowEst > (tRowcnt)nRow0 ) nRowEst = nRow0;
165518 *pnRow = nRowEst;
165519 WHERETRACE(0x20,("IN row estimate: est=%d\n", nRowEst));
165520 }
165521 assert( pBuilder->nRecValid==nRecValid )((void) (0));
165522 return rc;
165523}
165524#endif /* SQLITE_ENABLE_STAT4 */
165525
165526
165527#ifdef WHERETRACE_ENABLED
165528/*
165529** Print the content of a WhereTerm object
165530*/
165531SQLITE_PRIVATEstatic void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
165532 if( pTerm==0 ){
165533 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
165534 }else{
165535 char zType[8];
165536 char zLeft[50];
165537 memcpy(zType, "....", 5);
165538 if( pTerm->wtFlags & TERM_VIRTUAL0x0002 ) zType[0] = 'V';
165539 if( pTerm->eOperator & WO_EQUIV0x0800 ) zType[1] = 'E';
165540 if( ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0) ) zType[2] = 'L';
165541 if( pTerm->wtFlags & TERM_CODED0x0004 ) zType[3] = 'C';
165542 if( pTerm->eOperator & WO_SINGLE0x01ff ){
165543 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 )((void) (0));
165544 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
165545 pTerm->leftCursor, pTerm->u.x.leftColumn);
165546 }else if( (pTerm->eOperator & WO_OR0x0200)!=0 && pTerm->u.pOrInfo!=0 ){
165547 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%llx",
165548 pTerm->u.pOrInfo->indexable);
165549 }else{
165550 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
165551 }
165552 sqlite3DebugPrintf(
165553 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
165554 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
165555 /* The 0x10000 .wheretrace flag causes extra information to be
165556 ** shown about each Term */
165557 if( sqlite3WhereTrace & 0x10000 ){
165558 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
165559 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
165560 }
165561 if( (pTerm->eOperator & (WO_OR0x0200|WO_AND0x0400))==0 && pTerm->u.x.iField ){
165562 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
165563 }
165564 if( pTerm->iParent>=0 ){
165565 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
165566 }
165567 sqlite3DebugPrintf("\n");
165568 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
165569 }
165570}
165571#endif
165572
165573#ifdef WHERETRACE_ENABLED
165574/*
165575** Show the complete content of a WhereClause
165576*/
165577SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC){
165578 int i;
165579 for(i=0; i<pWC->nTerm; i++){
165580 sqlite3WhereTermPrint(&pWC->a[i], i);
165581 }
165582}
165583#endif
165584
165585#ifdef WHERETRACE_ENABLED
165586/*
165587** Print a WhereLoop object for debugging purposes
165588**
165589** Format example:
165590**
165591** .--- Position in WHERE clause rSetup, rRun, nOut ---.
165592** | |
165593** | .--- selfMask nTerm ------. |
165594** | | | |
165595** | | .-- prereq Idx wsFlags----. | |
165596** | | | Name | | |
165597** | | | __|__ nEq ---. ___|__ | __|__
165598** | / \ / \ / \ | / \ / \ / \
165599** 1.002.001 t2.t2xy 2 f 010241 N 2 cost 0,56,31
165600*/
165601SQLITE_PRIVATEstatic void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC){
165602 if( pWC ){
165603 WhereInfo *pWInfo = pWC->pWInfo;
165604 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
165605 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
165606 Table *pTab = pItem->pSTab;
165607 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
165608 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
165609 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
165610 sqlite3DebugPrintf(" %12s",
165611 pItem->zAlias ? pItem->zAlias : pTab->zName);
165612 }else{
165613 sqlite3DebugPrintf("%c%2d.%03llx.%03llx %c%d",
165614 p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab);
165615 }
165616 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
165617 const char *zName;
165618 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
165619 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
165620 int i = sqlite3Strlen30(zName) - 1;
165621 while( zName[i]!='_' ) i--;
165622 zName += i;
165623 }
165624 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
165625 }else{
165626 sqlite3DebugPrintf("%20s","");
165627 }
165628 }else{
165629 char *z;
165630 if( p->u.vtab.idxStr ){
165631 z = sqlite3_mprintf("(%d,\"%s\",%#x)",
165632 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
165633 }else{
165634 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
165635 }
165636 sqlite3DebugPrintf(" %-19s", z);
165637 sqlite3_free(z);
165638 }
165639 if( p->wsFlags & WHERE_SKIPSCAN0x00008000 ){
165640 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
165641 }else{
165642 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
165643 }
165644 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
165645 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
165646 int i;
165647 for(i=0; i<p->nLTerm; i++){
165648 sqlite3WhereTermPrint(p->aLTerm[i], i);
165649 }
165650 }
165651}
165652SQLITE_PRIVATEstatic void sqlite3ShowWhereLoop(const WhereLoop *p){
165653 if( p ) sqlite3WhereLoopPrint(p, 0);
165654}
165655SQLITE_PRIVATEstatic void sqlite3ShowWhereLoopList(const WhereLoop *p){
165656 while( p ){
165657 sqlite3ShowWhereLoop(p);
165658 p = p->pNextLoop;
165659 }
165660}
165661#endif
165662
165663/*
165664** Convert bulk memory into a valid WhereLoop that can be passed
165665** to whereLoopClear harmlessly.
165666*/
165667static void whereLoopInit(WhereLoop *p){
165668 p->aLTerm = p->aLTermSpace;
165669 p->nLTerm = 0;
165670 p->nLSlot = ArraySize(p->aLTermSpace)((int)(sizeof(p->aLTermSpace)/sizeof(p->aLTermSpace[0])
))
;
165671 p->wsFlags = 0;
165672}
165673
165674/*
165675** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
165676*/
165677static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
165678 if( p->wsFlags & (WHERE_VIRTUALTABLE0x00000400|WHERE_AUTO_INDEX0x00004000) ){
165679 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 && p->u.vtab.needFree ){
165680 sqlite3_free(p->u.vtab.idxStr);
165681 p->u.vtab.needFree = 0;
165682 p->u.vtab.idxStr = 0;
165683 }else if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && p->u.btree.pIndex!=0 ){
165684 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
165685 sqlite3DbFreeNN(db, p->u.btree.pIndex);
165686 p->u.btree.pIndex = 0;
165687 }
165688 }
165689}
165690
165691/*
165692** Deallocate internal memory used by a WhereLoop object. Leave the
165693** object in an initialized state, as if it had been newly allocated.
165694*/
165695static void whereLoopClear(sqlite3 *db, WhereLoop *p){
165696 if( p->aLTerm!=p->aLTermSpace ){
165697 sqlite3DbFreeNN(db, p->aLTerm);
165698 p->aLTerm = p->aLTermSpace;
165699 p->nLSlot = ArraySize(p->aLTermSpace)((int)(sizeof(p->aLTermSpace)/sizeof(p->aLTermSpace[0])
))
;
165700 }
165701 whereLoopClearUnion(db, p);
165702 p->nLTerm = 0;
165703 p->wsFlags = 0;
165704}
165705
165706/*
165707** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
165708*/
165709static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
165710 WhereTerm **paNew;
165711 if( p->nLSlot>=n ) return SQLITE_OK0;
165712 n = (n+7)&~7;
165713 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
165714 if( paNew==0 ) return SQLITE_NOMEM_BKPT7;
165715 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
165716 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
165717 p->aLTerm = paNew;
165718 p->nLSlot = n;
165719 return SQLITE_OK0;
165720}
165721
165722/*
165723** Transfer content from the second pLoop into the first.
165724*/
165725static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
165726 whereLoopClearUnion(db, pTo);
165727 if( pFrom->nLTerm > pTo->nLSlot
165728 && whereLoopResize(db, pTo, pFrom->nLTerm)
165729 ){
165730 memset(pTo, 0, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
165731 return SQLITE_NOMEM_BKPT7;
165732 }
165733 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
165734 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
165735 if( pFrom->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
165736 pFrom->u.vtab.needFree = 0;
165737 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
165738 pFrom->u.btree.pIndex = 0;
165739 }
165740 return SQLITE_OK0;
165741}
165742
165743/*
165744** Delete a WhereLoop object
165745*/
165746static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
165747 assert( db!=0 )((void) (0));
165748 whereLoopClear(db, p);
165749 sqlite3DbNNFreeNN(db, p);
165750}
165751
165752/*
165753** Free a WhereInfo structure
165754*/
165755static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
165756 assert( pWInfo!=0 )((void) (0));
165757 assert( db!=0 )((void) (0));
165758 sqlite3WhereClauseClear(&pWInfo->sWC);
165759 while( pWInfo->pLoops ){
165760 WhereLoop *p = pWInfo->pLoops;
165761 pWInfo->pLoops = p->pNextLoop;
165762 whereLoopDelete(db, p);
165763 }
165764 while( pWInfo->pMemToFree ){
165765 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
165766 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
165767 pWInfo->pMemToFree = pNext;
165768 }
165769 sqlite3DbNNFreeNN(db, pWInfo);
165770}
165771
165772/*
165773** Return TRUE if X is a proper subset of Y but is of equal or less cost.
165774** In other words, return true if all constraints of X are also part of Y
165775** and Y has additional constraints that might speed the search that X lacks
165776** but the cost of running X is not more than the cost of running Y.
165777**
165778** In other words, return true if the cost relationship between X and Y
165779** is inverted and needs to be adjusted.
165780**
165781** Case 1:
165782**
165783** (1a) X and Y use the same index.
165784** (1b) X has fewer == terms than Y
165785** (1c) Neither X nor Y use skip-scan
165786** (1d) X does not have a a greater cost than Y
165787**
165788** Case 2:
165789**
165790** (2a) X has the same or lower cost, or returns the same or fewer rows,
165791** than Y.
165792** (2b) X uses fewer WHERE clause terms than Y
165793** (2c) Every WHERE clause term used by X is also used by Y
165794** (2d) X skips at least as many columns as Y
165795** (2e) If X is a covering index, than Y is too
165796*/
165797static int whereLoopCheaperProperSubset(
165798 const WhereLoop *pX, /* First WhereLoop to compare */
165799 const WhereLoop *pY /* Compare against this WhereLoop */
165800){
165801 int i, j;
165802 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */
165803 assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
165804 assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
165805 if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */
165806 && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */
165807 && pX->nSkip==0 && pY->nSkip==0 /* (1c) */
165808 ){
165809 return 1; /* Case 1 is true */
165810 }
165811 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
165812 return 0; /* (2b) */
165813 }
165814 if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */
165815 for(i=pX->nLTerm-1; i>=0; i--){
165816 if( pX->aLTerm[i]==0 ) continue;
165817 for(j=pY->nLTerm-1; j>=0; j--){
165818 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
165819 }
165820 if( j<0 ) return 0; /* (2c) */
165821 }
165822 if( (pX->wsFlags&WHERE_IDX_ONLY0x00000040)!=0
165823 && (pY->wsFlags&WHERE_IDX_ONLY0x00000040)==0 ){
165824 return 0; /* (2e) */
165825 }
165826 return 1; /* Case 2 is true */
165827}
165828
165829/*
165830** Try to adjust the cost and number of output rows of WhereLoop pTemplate
165831** upwards or downwards so that:
165832**
165833** (1) pTemplate costs less than any other WhereLoops that are a proper
165834** subset of pTemplate
165835**
165836** (2) pTemplate costs more than any other WhereLoops for which pTemplate
165837** is a proper subset.
165838**
165839** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
165840** WHERE clause terms than Y and that every WHERE clause term used by X is
165841** also used by Y.
165842*/
165843static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
165844 if( (pTemplate->wsFlags & WHERE_INDEXED0x00000200)==0 ) return;
165845 for(; p; p=p->pNextLoop){
165846 if( p->iTab!=pTemplate->iTab ) continue;
165847 if( (p->wsFlags & WHERE_INDEXED0x00000200)==0 ) continue;
165848 if( whereLoopCheaperProperSubset(p, pTemplate) ){
165849 /* Adjust pTemplate cost downward so that it is cheaper than its
165850 ** subset p. */
165851 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
165852 pTemplate->rRun, pTemplate->nOut,
165853 MIN(p->rRun, pTemplate->rRun),
165854 MIN(p->nOut - 1, pTemplate->nOut)));
165855 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun)((p->rRun)<(pTemplate->rRun)?(p->rRun):(pTemplate
->rRun))
;
165856 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut)((p->nOut - 1)<(pTemplate->nOut)?(p->nOut - 1):(pTemplate
->nOut))
;
165857 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
165858 /* Adjust pTemplate cost upward so that it is costlier than p since
165859 ** pTemplate is a proper subset of p */
165860 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
165861 pTemplate->rRun, pTemplate->nOut,
165862 MAX(p->rRun, pTemplate->rRun),
165863 MAX(p->nOut + 1, pTemplate->nOut)));
165864 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun)((p->rRun)>(pTemplate->rRun)?(p->rRun):(pTemplate
->rRun))
;
165865 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut)((p->nOut + 1)>(pTemplate->nOut)?(p->nOut + 1):(pTemplate
->nOut))
;
165866 }
165867 }
165868}
165869
165870/*
165871** Search the list of WhereLoops in *ppPrev looking for one that can be
165872** replaced by pTemplate.
165873**
165874** Return NULL if pTemplate does not belong on the WhereLoop list.
165875** In other words if pTemplate ought to be dropped from further consideration.
165876**
165877** If pX is a WhereLoop that pTemplate can replace, then return the
165878** link that points to pX.
165879**
165880** If pTemplate cannot replace any existing element of the list but needs
165881** to be added to the list as a new entry, then return a pointer to the
165882** tail of the list.
165883*/
165884static WhereLoop **whereLoopFindLesser(
165885 WhereLoop **ppPrev,
165886 const WhereLoop *pTemplate
165887){
165888 WhereLoop *p;
165889 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
165890 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
165891 /* If either the iTab or iSortIdx values for two WhereLoop are different
165892 ** then those WhereLoops need to be considered separately. Neither is
165893 ** a candidate to replace the other. */
165894 continue;
165895 }
165896 /* In the current implementation, the rSetup value is either zero
165897 ** or the cost of building an automatic index (NlogN) and the NlogN
165898 ** is the same for compatible WhereLoops. */
165899 assert( p->rSetup==0 || pTemplate->rSetup==0((void) (0))
165900 || p->rSetup==pTemplate->rSetup )((void) (0));
165901
165902 /* whereLoopAddBtree() always generates and inserts the automatic index
165903 ** case first. Hence compatible candidate WhereLoops never have a larger
165904 ** rSetup. Call this SETUP-INVARIANT */
165905 assert( p->rSetup>=pTemplate->rSetup )((void) (0));
165906
165907 /* Any loop using an application-defined index (or PRIMARY KEY or
165908 ** UNIQUE constraint) with one or more == constraints is better
165909 ** than an automatic index. Unless it is a skip-scan. */
165910 if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0
165911 && (pTemplate->nSkip)==0
165912 && (pTemplate->wsFlags & WHERE_INDEXED0x00000200)!=0
165913 && (pTemplate->wsFlags & WHERE_COLUMN_EQ0x00000001)!=0
165914 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
165915 ){
165916 break;
165917 }
165918
165919 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
165920 ** discarded. WhereLoop p is better if:
165921 ** (1) p has no more dependencies than pTemplate, and
165922 ** (2) p has an equal or lower cost than pTemplate
165923 */
165924 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
165925 && p->rSetup<=pTemplate->rSetup /* (2a) */
165926 && p->rRun<=pTemplate->rRun /* (2b) */
165927 && p->nOut<=pTemplate->nOut /* (2c) */
165928 ){
165929 return 0; /* Discard pTemplate */
165930 }
165931
165932 /* If pTemplate is always better than p, then cause p to be overwritten
165933 ** with pTemplate. pTemplate is better than p if:
165934 ** (1) pTemplate has no more dependencies than p, and
165935 ** (2) pTemplate has an equal or lower cost than p.
165936 */
165937 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
165938 && p->rRun>=pTemplate->rRun /* (2a) */
165939 && p->nOut>=pTemplate->nOut /* (2b) */
165940 ){
165941 assert( p->rSetup>=pTemplate->rSetup )((void) (0)); /* SETUP-INVARIANT above */
165942 break; /* Cause p to be overwritten by pTemplate */
165943 }
165944 }
165945 return ppPrev;
165946}
165947
165948/*
165949** Insert or replace a WhereLoop entry using the template supplied.
165950**
165951** An existing WhereLoop entry might be overwritten if the new template
165952** is better and has fewer dependencies. Or the template will be ignored
165953** and no insert will occur if an existing WhereLoop is faster and has
165954** fewer dependencies than the template. Otherwise a new WhereLoop is
165955** added based on the template.
165956**
165957** If pBuilder->pOrSet is not NULL then we care about only the
165958** prerequisites and rRun and nOut costs of the N best loops. That
165959** information is gathered in the pBuilder->pOrSet object. This special
165960** processing mode is used only for OR clause processing.
165961**
165962** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
165963** still might overwrite similar loops with the new template if the
165964** new template is better. Loops may be overwritten if the following
165965** conditions are met:
165966**
165967** (1) They have the same iTab.
165968** (2) They have the same iSortIdx.
165969** (3) The template has same or fewer dependencies than the current loop
165970** (4) The template has the same or lower cost than the current loop
165971*/
165972static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
165973 WhereLoop **ppPrev, *p;
165974 WhereInfo *pWInfo = pBuilder->pWInfo;
165975 sqlite3 *db = pWInfo->pParse->db;
165976 int rc;
165977
165978 /* Stop the search once we hit the query planner search limit */
165979 if( pBuilder->iPlanLimit==0 ){
165980 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
165981 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
165982 return SQLITE_DONE101;
165983 }
165984 pBuilder->iPlanLimit--;
165985
165986 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
165987
165988 /* If pBuilder->pOrSet is defined, then only keep track of the costs
165989 ** and prereqs.
165990 */
165991 if( pBuilder->pOrSet!=0 ){
165992 if( pTemplate->nLTerm ){
165993#if WHERETRACE_ENABLED
165994 u16 n = pBuilder->pOrSet->n;
165995 int x =
165996#endif
165997 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
165998 pTemplate->nOut);
165999#if WHERETRACE_ENABLED /* 0x8 */
166000 if( sqlite3WhereTrace & 0x8 ){
166001 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
166002 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
166003 }
166004#endif
166005 }
166006 return SQLITE_OK0;
166007 }
166008
166009 /* Look for an existing WhereLoop to replace with pTemplate
166010 */
166011 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
166012
166013 if( ppPrev==0 ){
166014 /* There already exists a WhereLoop on the list that is better
166015 ** than pTemplate, so just ignore pTemplate */
166016#if WHERETRACE_ENABLED /* 0x8 */
166017 if( sqlite3WhereTrace & 0x8 ){
166018 sqlite3DebugPrintf(" skip: ");
166019 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
166020 }
166021#endif
166022 return SQLITE_OK0;
166023 }else{
166024 p = *ppPrev;
166025 }
166026
166027 /* If we reach this point it means that either p[] should be overwritten
166028 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
166029 ** WhereLoop and insert it.
166030 */
166031#if WHERETRACE_ENABLED /* 0x8 */
166032 if( sqlite3WhereTrace & 0x8 ){
166033 if( p!=0 ){
166034 sqlite3DebugPrintf("replace: ");
166035 sqlite3WhereLoopPrint(p, pBuilder->pWC);
166036 sqlite3DebugPrintf(" with: ");
166037 }else{
166038 sqlite3DebugPrintf(" add: ");
166039 }
166040 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
166041 }
166042#endif
166043 if( p==0 ){
166044 /* Allocate a new WhereLoop to add to the end of the list */
166045 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
166046 if( p==0 ) return SQLITE_NOMEM_BKPT7;
166047 whereLoopInit(p);
166048 p->pNextLoop = 0;
166049 }else{
166050 /* We will be overwriting WhereLoop p[]. But before we do, first
166051 ** go through the rest of the list and delete any other entries besides
166052 ** p[] that are also supplanted by pTemplate */
166053 WhereLoop **ppTail = &p->pNextLoop;
166054 WhereLoop *pToDel;
166055 while( *ppTail ){
166056 ppTail = whereLoopFindLesser(ppTail, pTemplate);
166057 if( ppTail==0 ) break;
166058 pToDel = *ppTail;
166059 if( pToDel==0 ) break;
166060 *ppTail = pToDel->pNextLoop;
166061#if WHERETRACE_ENABLED /* 0x8 */
166062 if( sqlite3WhereTrace & 0x8 ){
166063 sqlite3DebugPrintf(" delete: ");
166064 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
166065 }
166066#endif
166067 whereLoopDelete(db, pToDel);
166068 }
166069 }
166070 rc = whereLoopXfer(db, p, pTemplate);
166071 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
166072 Index *pIndex = p->u.btree.pIndex;
166073 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK3 ){
166074 p->u.btree.pIndex = 0;
166075 }
166076 }
166077 return rc;
166078}
166079
166080/*
166081** Adjust the WhereLoop.nOut value downward to account for terms of the
166082** WHERE clause that reference the loop but which are not used by an
166083** index.
166084*
166085** For every WHERE clause term that is not used by the index
166086** and which has a truth probability assigned by one of the likelihood(),
166087** likely(), or unlikely() SQL functions, reduce the estimated number
166088** of output rows by the probability specified.
166089**
166090** TUNING: For every WHERE clause term that is not used by the index
166091** and which does not have an assigned truth probability, heuristics
166092** described below are used to try to estimate the truth probability.
166093** TODO --> Perhaps this is something that could be improved by better
166094** table statistics.
166095**
166096** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
166097** value corresponds to -1 in LogEst notation, so this means decrement
166098** the WhereLoop.nOut field for every such WHERE clause term.
166099**
166100** Heuristic 2: If there exists one or more WHERE clause terms of the
166101** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
166102** final output row estimate is no greater than 1/4 of the total number
166103** of rows in the table. In other words, assume that x==EXPR will filter
166104** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
166105** "x" column is boolean or else -1 or 0 or 1 is a common default value
166106** on the "x" column and so in that case only cap the output row estimate
166107** at 1/2 instead of 1/4.
166108*/
166109static void whereLoopOutputAdjust(
166110 WhereClause *pWC, /* The WHERE clause */
166111 WhereLoop *pLoop, /* The loop to adjust downward */
166112 LogEst nRow /* Number of rows in the entire table */
166113){
166114 WhereTerm *pTerm, *pX;
166115 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
166116 int i, j;
166117 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
166118
166119 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
166120 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
166121 assert( pTerm!=0 )((void) (0));
166122 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
166123 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
166124 if( (pTerm->wtFlags & TERM_VIRTUAL0x0002)!=0 ) continue;
166125 for(j=pLoop->nLTerm-1; j>=0; j--){
166126 pX = pLoop->aLTerm[j];
166127 if( pX==0 ) continue;
166128 if( pX==pTerm ) break;
166129 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
166130 }
166131 if( j<0 ){
166132 sqlite3ProgressCheck(pWC->pWInfo->pParse);
166133 if( pLoop->maskSelf==pTerm->prereqAll ){
166134 /* If there are extra terms in the WHERE clause not used by an index
166135 ** that depend only on the table being scanned, and that will tend to
166136 ** cause many rows to be omitted, then mark that table as
166137 ** "self-culling".
166138 **
166139 ** 2022-03-24: Self-culling only applies if either the extra terms
166140 ** are straight comparison operators that are non-true with NULL
166141 ** operand, or if the loop is not an OUTER JOIN.
166142 */
166143 if( (pTerm->eOperator & 0x3f)!=0
166144 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
166145 & (JT_LEFT0x08|JT_LTORJ0x40))==0
166146 ){
166147 pLoop->wsFlags |= WHERE_SELFCULL0x00800000;
166148 }
166149 }
166150 if( pTerm->truthProb<=0 ){
166151 /* If a truth probability is specified using the likelihood() hints,
166152 ** then use the probability provided by the application. */
166153 pLoop->nOut += pTerm->truthProb;
166154 }else{
166155 /* In the absence of explicit truth probabilities, use heuristics to
166156 ** guess a reasonable truth probability. */
166157 pLoop->nOut--;
166158 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0
166159 && (pTerm->wtFlags & TERM_HIGHTRUTH0)==0 /* tag-20200224-1 */
166160 ){
166161 Expr *pRight = pTerm->pExpr->pRight;
166162 int k = 0;
166163 testcase( pTerm->pExpr->op==TK_IS );
166164 if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){
166165 k = 10;
166166 }else{
166167 k = 20;
166168 }
166169 if( iReduce<k ){
166170 pTerm->wtFlags |= TERM_HEURTRUTH0x2000;
166171 iReduce = k;
166172 }
166173 }
166174 }
166175 }
166176 }
166177 if( pLoop->nOut > nRow-iReduce ){
166178 pLoop->nOut = nRow - iReduce;
166179 }
166180}
166181
166182/*
166183** Term pTerm is a vector range comparison operation. The first comparison
166184** in the vector can be optimized using column nEq of the index. This
166185** function returns the total number of vector elements that can be used
166186** as part of the range comparison.
166187**
166188** For example, if the query is:
166189**
166190** WHERE a = ? AND (b, c, d) > (?, ?, ?)
166191**
166192** and the index:
166193**
166194** CREATE INDEX ... ON (a, b, c, d, e)
166195**
166196** then this function would be invoked with nEq=1. The value returned in
166197** this case is 3.
166198*/
166199static int whereRangeVectorLen(
166200 Parse *pParse, /* Parsing context */
166201 int iCur, /* Cursor open on pIdx */
166202 Index *pIdx, /* The index to be used for a inequality constraint */
166203 int nEq, /* Number of prior equality constraints on same index */
166204 WhereTerm *pTerm /* The vector inequality constraint */
166205){
166206 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
166207 int i;
166208
166209 nCmp = MIN(nCmp, (pIdx->nColumn - nEq))((nCmp)<((pIdx->nColumn - nEq))?(nCmp):((pIdx->nColumn
- nEq)))
;
166210 for(i=1; i<nCmp; i++){
166211 /* Test if comparison i of pTerm is compatible with column (i+nEq)
166212 ** of the index. If not, exit the loop. */
166213 char aff; /* Comparison affinity */
166214 char idxaff = 0; /* Indexed columns affinity */
166215 CollSeq *pColl; /* Comparison collation sequence */
166216 Expr *pLhs, *pRhs;
166217
166218 assert( ExprUseXList(pTerm->pExpr->pLeft) )((void) (0));
166219 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
166220 pRhs = pTerm->pExpr->pRight;
166221 if( ExprUseXSelect(pRhs)(((pRhs)->flags&0x001000)!=0) ){
166222 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
166223 }else{
166224 pRhs = pRhs->x.pList->a[i].pExpr;
166225 }
166226
166227 /* Check that the LHS of the comparison is a column reference to
166228 ** the right column of the right source table. And that the sort
166229 ** order of the index column is the same as the sort order of the
166230 ** leftmost index column. */
166231 if( pLhs->op!=TK_COLUMN168
166232 || pLhs->iTable!=iCur
166233 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
166234 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
166235 ){
166236 break;
166237 }
166238
166239 testcase( pLhs->iColumn==XN_ROWID );
166240 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
166241 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
166242 if( aff!=idxaff ) break;
166243
166244 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
166245 if( pColl==0 ) break;
166246 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
166247 }
166248 return i;
166249}
166250
166251/*
166252** Adjust the cost C by the costMult factor T. This only occurs if
166253** compiled with -DSQLITE_ENABLE_COSTMULT
166254*/
166255#ifdef SQLITE_ENABLE_COSTMULT
166256# define ApplyCostMultiplier(C,T) C += T
166257#else
166258# define ApplyCostMultiplier(C,T)
166259#endif
166260
166261/*
166262** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
166263** index pIndex. Try to match one more.
166264**
166265** When this function is called, pBuilder->pNew->nOut contains the
166266** number of rows expected to be visited by filtering using the nEq
166267** terms only. If it is modified, this value is restored before this
166268** function returns.
166269**
166270** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
166271** a fake index used for the INTEGER PRIMARY KEY.
166272*/
166273static int whereLoopAddBtreeIndex(
166274 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
166275 SrcItem *pSrc, /* FROM clause term being analyzed */
166276 Index *pProbe, /* An index on pSrc */
166277 LogEst nInMul /* log(Number of iterations due to IN) */
166278){
166279 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */
166280 Parse *pParse = pWInfo->pParse; /* Parsing context */
166281 sqlite3 *db = pParse->db; /* Database connection malloc context */
166282 WhereLoop *pNew; /* Template WhereLoop under construction */
166283 WhereTerm *pTerm; /* A WhereTerm under consideration */
166284 int opMask; /* Valid operators for constraints */
166285 WhereScan scan; /* Iterator for WHERE terms */
166286 Bitmask saved_prereq; /* Original value of pNew->prereq */
166287 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
166288 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
166289 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
166290 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
166291 u16 saved_nSkip; /* Original value of pNew->nSkip */
166292 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
166293 LogEst saved_nOut; /* Original value of pNew->nOut */
166294 int rc = SQLITE_OK0; /* Return code */
166295 LogEst rSize; /* Number of rows in the table */
166296 LogEst rLogSize; /* Logarithm of table size */
166297 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
166298
166299 pNew = pBuilder->pNew;
166300 assert( db->mallocFailed==0 || pParse->nErr>0 )((void) (0));
166301 if( pParse->nErr ){
166302 return pParse->rc;
166303 }
166304 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d, rRun=%d\n",
166305 pProbe->pTable->zName,pProbe->zName,
166306 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
166307
166308 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
166309 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 )((void) (0));
166310 if( pNew->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
166311 opMask = WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54));
166312 }else{
166313 assert( pNew->u.btree.nBtm==0 )((void) (0));
166314 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;
166315 }
166316 if( pProbe->bUnordered || pProbe->bLowQual ){
166317 if( pProbe->bUnordered ) opMask &= ~(WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))|WO_LT(0x0002<<(57 -54))|WO_LE(0x0002<<(56 -54)));
166318 if( pProbe->bLowQual && pSrc->fg.isIndexedBy==0 ){
166319 opMask &= ~(WO_EQ0x0002|WO_IN0x0001|WO_IS0x0080);
166320 }
166321 }
166322
166323 assert( pNew->u.btree.nEq<pProbe->nColumn )((void) (0));
166324 assert( pNew->u.btree.nEq<pProbe->nKeyCol((void) (0))
166325 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
166326
166327 saved_nEq = pNew->u.btree.nEq;
166328 saved_nBtm = pNew->u.btree.nBtm;
166329 saved_nTop = pNew->u.btree.nTop;
166330 saved_nSkip = pNew->nSkip;
166331 saved_nLTerm = pNew->nLTerm;
166332 saved_wsFlags = pNew->wsFlags;
166333 saved_prereq = pNew->prereq;
166334 saved_nOut = pNew->nOut;
166335 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
166336 opMask, pProbe);
166337 pNew->rSetup = 0;
166338 rSize = pProbe->aiRowLogEst[0];
166339 rLogSize = estLog(rSize);
166340 for(; rc==SQLITE_OK0 && pTerm!=0; pTerm = whereScanNext(&scan)){
166341 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
166342 LogEst rCostIdx;
166343 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
166344 int nIn = 0;
166345#ifdef SQLITE_ENABLE_STAT4
166346 int nRecValid = pBuilder->nRecValid;
166347#endif
166348 if( (eOp==WO_ISNULL0x0100 || (pTerm->wtFlags&TERM_VNULL0x0080)!=0)
166349 && indexColumnNotNull(pProbe, saved_nEq)
166350 ){
166351 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
166352 }
166353 if( pTerm->prereqRight & pNew->maskSelf ) continue;
166354
166355 /* Do not allow the upper bound of a LIKE optimization range constraint
166356 ** to mix with a lower range bound from some other source */
166357 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 && pTerm->eOperator==WO_LT(0x0002<<(57 -54)) ) continue;
166358
166359 if( (pSrc->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0
166360 && !constraintCompatibleWithOuterJoin(pTerm,pSrc)
166361 ){
166362 continue;
166363 }
166364 if( IsUniqueIndex(pProbe)((pProbe)->onError!=0) && saved_nEq==pProbe->nKeyCol-1 ){
166365 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE0x0002;
166366 }else{
166367 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED0x0001;
166368 }
166369 pNew->wsFlags = saved_wsFlags;
166370 pNew->u.btree.nEq = saved_nEq;
166371 pNew->u.btree.nBtm = saved_nBtm;
166372 pNew->u.btree.nTop = saved_nTop;
166373 pNew->nLTerm = saved_nLTerm;
166374 if( pNew->nLTerm>=pNew->nLSlot
166375 && whereLoopResize(db, pNew, pNew->nLTerm+1)
166376 ){
166377 break; /* OOM while trying to enlarge the pNew->aLTerm array */
166378 }
166379 pNew->aLTerm[pNew->nLTerm++] = pTerm;
166380 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
166381
166382 assert( nInMul==0((void) (0))
166383 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0((void) (0))
166384 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0((void) (0))
166385 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0((void) (0))
166386 )((void) (0));
166387
166388 if( eOp & WO_IN0x0001 ){
166389 Expr *pExpr = pTerm->pExpr;
166390 if( ExprUseXSelect(pExpr)(((pExpr)->flags&0x001000)!=0) ){
166391 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
166392 int i;
166393 nIn = 46; assert( 46==sqlite3LogEst(25) )((void) (0));
166394
166395 /* The expression may actually be of the form (x, y) IN (SELECT...).
166396 ** In this case there is a separate term for each of (x) and (y).
166397 ** However, the nIn multiplier should only be applied once, not once
166398 ** for each such term. The following loop checks that pTerm is the
166399 ** first such term in use, and sets nIn back to 0 if it is not. */
166400 for(i=0; i<pNew->nLTerm-1; i++){
166401 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
166402 }
166403 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr)(pExpr->x.pList && pExpr->x.pList->nExpr) ){
166404 /* "x IN (value, value, ...)" */
166405 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
166406 }
166407 if( pProbe->hasStat1 && rLogSize>=10 ){
166408 LogEst M, logK, x;
166409 /* Let:
166410 ** N = the total number of rows in the table
166411 ** K = the number of entries on the RHS of the IN operator
166412 ** M = the number of rows in the table that match terms to the
166413 ** to the left in the same index. If the IN operator is on
166414 ** the left-most index column, M==N.
166415 **
166416 ** Given the definitions above, it is better to omit the IN operator
166417 ** from the index lookup and instead do a scan of the M elements,
166418 ** testing each scanned row against the IN operator separately, if:
166419 **
166420 ** M*log(K) < K*log(N)
166421 **
166422 ** Our estimates for M, K, and N might be inaccurate, so we build in
166423 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
166424 ** with the index, as using an index has better worst-case behavior.
166425 ** If we do not have real sqlite_stat1 data, always prefer to use
166426 ** the index. Do not bother with this optimization on very small
166427 ** tables (less than 2 rows) as it is pointless in that case.
166428 */
166429 M = pProbe->aiRowLogEst[saved_nEq];
166430 logK = estLog(nIn);
166431 /* TUNING v----- 10 to bias toward indexed IN */
166432 x = M + logK + 10 - (nIn + rLogSize);
166433 if( x>=0 ){
166434 WHERETRACE(0x40,
166435 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d) "
166436 "prefers indexed lookup\n",
166437 saved_nEq, M, logK, nIn, rLogSize, x));
166438 }else if( nInMul<2 && OptimizationEnabled(db, SQLITE_SeekScan)(((db)->dbOptFlags&(0x00020000))==0) ){
166439 WHERETRACE(0x40,
166440 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
166441 " nInMul=%d) prefers skip-scan\n",
166442 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
166443 pNew->wsFlags |= WHERE_IN_SEEKSCAN0x00100000;
166444 }else{
166445 WHERETRACE(0x40,
166446 ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d"
166447 " nInMul=%d) prefers normal scan\n",
166448 saved_nEq, M, logK, nIn, rLogSize, x, nInMul));
166449 continue;
166450 }
166451 }
166452 pNew->wsFlags |= WHERE_COLUMN_IN0x00000004;
166453 }else if( eOp & (WO_EQ0x0002|WO_IS0x0080) ){
166454 int iCol = pProbe->aiColumn[saved_nEq];
166455 pNew->wsFlags |= WHERE_COLUMN_EQ0x00000001;
166456 assert( saved_nEq==pNew->u.btree.nEq )((void) (0));
166457 if( iCol==XN_ROWID(-1)
166458 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
166459 ){
166460 if( iCol==XN_ROWID(-1) || pProbe->uniqNotNull
166461 || (pProbe->nKeyCol==1 && pProbe->onError && (eOp & WO_EQ0x0002))
166462 ){
166463 pNew->wsFlags |= WHERE_ONEROW0x00001000;
166464 }else{
166465 pNew->wsFlags |= WHERE_UNQ_WANTED0x00010000;
166466 }
166467 }
166468 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS0x00200000;
166469 }else if( eOp & WO_ISNULL0x0100 ){
166470 pNew->wsFlags |= WHERE_COLUMN_NULL0x00000008;
166471 }else{
166472 int nVecLen = whereRangeVectorLen(
166473 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
166474 );
166475 if( eOp & (WO_GT(0x0002<<(55 -54))|WO_GE(0x0002<<(58 -54))) ){
166476 testcase( eOp & WO_GT );
166477 testcase( eOp & WO_GE );
166478 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_BTM_LIMIT0x00000020;
166479 pNew->u.btree.nBtm = nVecLen;
166480 pBtm = pTerm;
166481 pTop = 0;
166482 if( pTerm->wtFlags & TERM_LIKEOPT0x0100 ){
166483 /* Range constraints that come from the LIKE optimization are
166484 ** always used in pairs. */
166485 pTop = &pTerm[1];
166486 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm )((void) (0));
166487 assert( pTop->wtFlags & TERM_LIKEOPT )((void) (0));
166488 assert( pTop->eOperator==WO_LT )((void) (0));
166489 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
166490 pNew->aLTerm[pNew->nLTerm++] = pTop;
166491 pNew->wsFlags |= WHERE_TOP_LIMIT0x00000010;
166492 pNew->u.btree.nTop = 1;
166493 }
166494 }else{
166495 assert( eOp & (WO_LT|WO_LE) )((void) (0));
166496 testcase( eOp & WO_LT );
166497 testcase( eOp & WO_LE );
166498 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_TOP_LIMIT0x00000010;
166499 pNew->u.btree.nTop = nVecLen;
166500 pTop = pTerm;
166501 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT0x00000020)!=0 ?
166502 pNew->aLTerm[pNew->nLTerm-2] : 0;
166503 }
166504 }
166505
166506 /* At this point pNew->nOut is set to the number of rows expected to
166507 ** be visited by the index scan before considering term pTerm, or the
166508 ** values of nIn and nInMul. In other words, assuming that all
166509 ** "x IN(...)" terms are replaced with "x = ?". This block updates
166510 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
166511 assert( pNew->nOut==saved_nOut )((void) (0));
166512 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
166513 /* Adjust nOut using stat4 data. Or, if there is no stat4
166514 ** data, using some other estimate. */
166515 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
166516 }else{
166517 int nEq = ++pNew->u.btree.nEq;
166518 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) )((void) (0));
166519
166520 assert( pNew->nOut==saved_nOut )((void) (0));
166521 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
166522 assert( (eOp & WO_IN) || nIn==0 )((void) (0));
166523 testcase( eOp & WO_IN );
166524 pNew->nOut += pTerm->truthProb;
166525 pNew->nOut -= nIn;
166526 }else{
166527#ifdef SQLITE_ENABLE_STAT4
166528 tRowcnt nOut = 0;
166529 if( nInMul==0
166530 && pProbe->nSample
166531 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)(pNew->u.btree.nEq<=pProbe->nSampleCol)
166532 && ((eOp & WO_IN0x0001)==0 || ExprUseXList(pTerm->pExpr)(((pTerm->pExpr)->flags&0x001000)==0))
166533 && OptimizationEnabled(db, SQLITE_Stat4)(((db)->dbOptFlags&(0x00000800))==0)
166534 ){
166535 Expr *pExpr = pTerm->pExpr;
166536 if( (eOp & (WO_EQ0x0002|WO_ISNULL0x0100|WO_IS0x0080))!=0 ){
166537 testcase( eOp & WO_EQ );
166538 testcase( eOp & WO_IS );
166539 testcase( eOp & WO_ISNULL );
166540 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
166541 }else{
166542 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
166543 }
166544 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
166545 if( rc!=SQLITE_OK0 ) break; /* Jump out of the pTerm loop */
166546 if( nOut ){
166547 pNew->nOut = sqlite3LogEst(nOut);
166548 if( nEq==1
166549 /* TUNING: Mark terms as "low selectivity" if they seem likely
166550 ** to be true for half or more of the rows in the table.
166551 ** See tag-202002240-1 */
166552 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
166553 ){
166554#if WHERETRACE_ENABLED /* 0x01 */
166555 if( sqlite3WhereTrace & 0x20 ){
166556 sqlite3DebugPrintf(
166557 "STAT4 determines term has low selectivity:\n");
166558 sqlite3WhereTermPrint(pTerm, 999);
166559 }
166560#endif
166561 pTerm->wtFlags |= TERM_HIGHTRUTH0;
166562 if( pTerm->wtFlags & TERM_HEURTRUTH0x2000 ){
166563 /* If the term has previously been used with an assumption of
166564 ** higher selectivity, then set the flag to rerun the
166565 ** loop computations. */
166566 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS0x0004;
166567 }
166568 }
166569 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
166570 pNew->nOut -= nIn;
166571 }
166572 }
166573 if( nOut==0 )
166574#endif
166575 {
166576 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
166577 if( eOp & WO_ISNULL0x0100 ){
166578 /* TUNING: If there is no likelihood() value, assume that a
166579 ** "col IS NULL" expression matches twice as many rows
166580 ** as (col=?). */
166581 pNew->nOut += 10;
166582 }
166583 }
166584 }
166585 }
166586
166587 /* Set rCostIdx to the estimated cost of visiting selected rows in the
166588 ** index. The estimate is the sum of two values:
166589 ** 1. The cost of doing one search-by-key to find the first matching
166590 ** entry
166591 ** 2. Stepping forward in the index pNew->nOut times to find all
166592 ** additional matching entries.
166593 */
166594 assert( pSrc->pSTab->szTabRow>0 )((void) (0));
166595 if( pProbe->idxType==SQLITE_IDXTYPE_IPK3 ){
166596 /* The pProbe->szIdxRow is low for an IPK table since the interior
166597 ** pages are small. Thus szIdxRow gives a good estimate of seek cost.
166598 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
166599 ** under-estimate the scanning cost. */
166600 rCostIdx = pNew->nOut + 16;
166601 }else{
166602 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pSTab->szTabRow;
166603 }
166604 rCostIdx = sqlite3LogEstAdd(rLogSize, rCostIdx);
166605
166606 /* Estimate the cost of running the loop. If all data is coming
166607 ** from the index, then this is just the cost of doing the index
166608 ** lookup and scan. But if some data is coming out of the main table,
166609 ** we also have to add in the cost of doing pNew->nOut searches to
166610 ** locate the row in the main table that corresponds to the index entry.
166611 */
166612 pNew->rRun = rCostIdx;
166613 if( (pNew->wsFlags & (WHERE_IDX_ONLY0x00000040|WHERE_IPK0x00000100|WHERE_EXPRIDX0x04000000))==0 ){
166614 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
166615 }
166616 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
166617
166618 nOutUnadjusted = pNew->nOut;
166619 pNew->rRun += nInMul + nIn;
166620 pNew->nOut += nInMul + nIn;
166621 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
166622 rc = whereLoopInsert(pBuilder, pNew);
166623
166624 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
166625 pNew->nOut = saved_nOut;
166626 }else{
166627 pNew->nOut = nOutUnadjusted;
166628 }
166629
166630 if( (pNew->wsFlags & WHERE_TOP_LIMIT0x00000010)==0
166631 && pNew->u.btree.nEq<pProbe->nColumn
166632 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
166633 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY2)
166634 ){
166635 if( pNew->u.btree.nEq>3 ){
166636 sqlite3ProgressCheck(pParse);
166637 }
166638 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
166639 }
166640 pNew->nOut = saved_nOut;
166641#ifdef SQLITE_ENABLE_STAT4
166642 pBuilder->nRecValid = nRecValid;
166643#endif
166644 }
166645 pNew->prereq = saved_prereq;
166646 pNew->u.btree.nEq = saved_nEq;
166647 pNew->u.btree.nBtm = saved_nBtm;
166648 pNew->u.btree.nTop = saved_nTop;
166649 pNew->nSkip = saved_nSkip;
166650 pNew->wsFlags = saved_wsFlags;
166651 pNew->nOut = saved_nOut;
166652 pNew->nLTerm = saved_nLTerm;
166653
166654 /* Consider using a skip-scan if there are no WHERE clause constraints
166655 ** available for the left-most terms of the index, and if the average
166656 ** number of repeats in the left-most terms is at least 18.
166657 **
166658 ** The magic number 18 is selected on the basis that scanning 17 rows
166659 ** is almost always quicker than an index seek (even though if the index
166660 ** contains fewer than 2^17 rows we assume otherwise in other parts of
166661 ** the code). And, even if it is not, it should not be too much slower.
166662 ** On the other hand, the extra seeks could end up being significantly
166663 ** more expensive. */
166664 assert( 42==sqlite3LogEst(18) )((void) (0));
166665 if( saved_nEq==saved_nSkip
166666 && saved_nEq+1<pProbe->nKeyCol
166667 && saved_nEq==pNew->nLTerm
166668 && pProbe->noSkipScan==0
166669 && pProbe->hasStat1!=0
166670 && OptimizationEnabled(db, SQLITE_SkipScan)(((db)->dbOptFlags&(0x00004000))==0)
166671 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
166672 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK0
166673 ){
166674 LogEst nIter;
166675 pNew->u.btree.nEq++;
166676 pNew->nSkip++;
166677 pNew->aLTerm[pNew->nLTerm++] = 0;
166678 pNew->wsFlags |= WHERE_SKIPSCAN0x00008000;
166679 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
166680 pNew->nOut -= nIter;
166681 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
166682 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
166683 nIter += 5;
166684 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
166685 pNew->nOut = saved_nOut;
166686 pNew->u.btree.nEq = saved_nEq;
166687 pNew->nSkip = saved_nSkip;
166688 pNew->wsFlags = saved_wsFlags;
166689 }
166690
166691 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
166692 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
166693 return rc;
166694}
166695
166696/*
166697** Return True if it is possible that pIndex might be useful in
166698** implementing the ORDER BY clause in pBuilder.
166699**
166700** Return False if pBuilder does not contain an ORDER BY clause or
166701** if there is no way for pIndex to be useful in implementing that
166702** ORDER BY clause.
166703*/
166704static int indexMightHelpWithOrderBy(
166705 WhereLoopBuilder *pBuilder,
166706 Index *pIndex,
166707 int iCursor
166708){
166709 ExprList *pOB;
166710 ExprList *aColExpr;
166711 int ii, jj;
166712
166713 if( pIndex->bUnordered ) return 0;
166714 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
166715 for(ii=0; ii<pOB->nExpr; ii++){
166716 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
166717 if( NEVER(pExpr==0)(pExpr==0) ) continue;
166718 if( (pExpr->op==TK_COLUMN168 || pExpr->op==TK_AGG_COLUMN170)
166719 && pExpr->iTable==iCursor
166720 ){
166721 if( pExpr->iColumn<0 ) return 1;
166722 for(jj=0; jj<pIndex->nKeyCol; jj++){
166723 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
166724 }
166725 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
166726 for(jj=0; jj<pIndex->nKeyCol; jj++){
166727 if( pIndex->aiColumn[jj]!=XN_EXPR(-2) ) continue;
166728 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
166729 return 1;
166730 }
166731 }
166732 }
166733 }
166734 return 0;
166735}
166736
166737/* Check to see if a partial index with pPartIndexWhere can be used
166738** in the current query. Return true if it can be and false if not.
166739*/
166740static int whereUsablePartialIndex(
166741 int iTab, /* The table for which we want an index */
166742 u8 jointype, /* The JT_* flags on the join */
166743 WhereClause *pWC, /* The WHERE clause of the query */
166744 Expr *pWhere /* The WHERE clause from the partial index */
166745){
166746 int i;
166747 WhereTerm *pTerm;
166748 Parse *pParse;
166749
166750 if( jointype & JT_LTORJ0x40 ) return 0;
166751 pParse = pWC->pWInfo->pParse;
166752 while( pWhere->op==TK_AND44 ){
166753 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
166754 pWhere = pWhere->pRight;
166755 }
166756 if( pParse->db->flags & SQLITE_EnableQPSG0x00800000 ) pParse = 0;
166757 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
166758 Expr *pExpr;
166759 pExpr = pTerm->pExpr;
166760 if( (!ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0) || pExpr->w.iJoin==iTab)
166761 && ((jointype & JT_OUTER0x20)==0 || ExprHasProperty(pExpr, EP_OuterON)(((pExpr)->flags&(0x000001))!=0))
166762 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
166763 && (pTerm->wtFlags & TERM_VNULL0x0080)==0
166764 ){
166765 return 1;
166766 }
166767 }
166768 return 0;
166769}
166770
166771/*
166772** pIdx is an index containing expressions. Check it see if any of the
166773** expressions in the index match the pExpr expression.
166774*/
166775static int exprIsCoveredByIndex(
166776 const Expr *pExpr,
166777 const Index *pIdx,
166778 int iTabCur
166779){
166780 int i;
166781 for(i=0; i<pIdx->nColumn; i++){
166782 if( pIdx->aiColumn[i]==XN_EXPR(-2)
166783 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
166784 ){
166785 return 1;
166786 }
166787 }
166788 return 0;
166789}
166790
166791/*
166792** Structure passed to the whereIsCoveringIndex Walker callback.
166793*/
166794typedef struct CoveringIndexCheck CoveringIndexCheck;
166795struct CoveringIndexCheck {
166796 Index *pIdx; /* The index */
166797 int iTabCur; /* Cursor number for the corresponding table */
166798 u8 bExpr; /* Uses an indexed expression */
166799 u8 bUnidx; /* Uses an unindexed column not within an indexed expr */
166800};
166801
166802/*
166803** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
166804**
166805** If the Expr node references the table with cursor pCk->iTabCur, then
166806** make sure that column is covered by the index pCk->pIdx. We know that
166807** all columns less than 63 (really BMS-1) are covered, so we don't need
166808** to check them. But we do need to check any column at 63 or greater.
166809**
166810** If the index does not cover the column, then set pWalk->eCode to
166811** non-zero and return WRC_Abort to stop the search.
166812**
166813** If this node does not disprove that the index can be a covering index,
166814** then just return WRC_Continue, to continue the search.
166815**
166816** If pCk->pIdx contains indexed expressions and one of those expressions
166817** matches pExpr, then prune the search.
166818*/
166819static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
166820 int i; /* Loop counter */
166821 const Index *pIdx; /* The index of interest */
166822 const i16 *aiColumn; /* Columns contained in the index */
166823 u16 nColumn; /* Number of columns in the index */
166824 CoveringIndexCheck *pCk; /* Info about this search */
166825
166826 pCk = pWalk->u.pCovIdxCk;
166827 pIdx = pCk->pIdx;
166828 if( (pExpr->op==TK_COLUMN168 || pExpr->op==TK_AGG_COLUMN170) ){
166829 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
166830 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue0;
166831 pIdx = pWalk->u.pCovIdxCk->pIdx;
166832 aiColumn = pIdx->aiColumn;
166833 nColumn = pIdx->nColumn;
166834 for(i=0; i<nColumn; i++){
166835 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue0;
166836 }
166837 pCk->bUnidx = 1;
166838 return WRC_Abort2;
166839 }else if( pIdx->bHasExpr
166840 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
166841 pCk->bExpr = 1;
166842 return WRC_Prune1;
166843 }
166844 return WRC_Continue0;
166845}
166846
166847
166848/*
166849** pIdx is an index that covers all of the low-number columns used by
166850** pWInfo->pSelect (columns from 0 through 62) or an index that has
166851** expressions terms. Hence, we cannot determine whether or not it is
166852** a covering index by using the colUsed bitmasks. We have to do a search
166853** to see if the index is covering. This routine does that search.
166854**
166855** The return value is one of these:
166856**
166857** 0 The index is definitely not a covering index
166858**
166859** WHERE_IDX_ONLY The index is definitely a covering index
166860**
166861** WHERE_EXPRIDX The index is likely a covering index, but it is
166862** difficult to determine precisely because of the
166863** expressions that are indexed. Score it as a
166864** covering index, but still keep the main table open
166865** just in case we need it.
166866**
166867** This routine is an optimization. It is always safe to return zero.
166868** But returning one of the other two values when zero should have been
166869** returned can lead to incorrect bytecode and assertion faults.
166870*/
166871static SQLITE_NOINLINE__attribute__((noinline)) u32 whereIsCoveringIndex(
166872 WhereInfo *pWInfo, /* The WHERE clause context */
166873 Index *pIdx, /* Index that is being tested */
166874 int iTabCur /* Cursor for the table being indexed */
166875){
166876 int i, rc;
166877 struct CoveringIndexCheck ck;
166878 Walker w;
166879 if( pWInfo->pSelect==0 ){
166880 /* We don't have access to the full query, so we cannot check to see
166881 ** if pIdx is covering. Assume it is not. */
166882 return 0;
166883 }
166884 if( pIdx->bHasExpr==0 ){
166885 for(i=0; i<pIdx->nColumn; i++){
166886 if( pIdx->aiColumn[i]>=BMS((int)(sizeof(Bitmask)*8))-1 ) break;
166887 }
166888 if( i>=pIdx->nColumn ){
166889 /* pIdx does not index any columns greater than 62, but we know from
166890 ** colMask that columns greater than 62 are used, so this is not a
166891 ** covering index */
166892 return 0;
166893 }
166894 }
166895 ck.pIdx = pIdx;
166896 ck.iTabCur = iTabCur;
166897 ck.bExpr = 0;
166898 ck.bUnidx = 0;
166899 memset(&w, 0, sizeof(w));
166900 w.xExprCallback = whereIsCoveringIndexWalkCallback;
166901 w.xSelectCallback = sqlite3SelectWalkNoop;
166902 w.u.pCovIdxCk = &ck;
166903 sqlite3WalkSelect(&w, pWInfo->pSelect);
166904 if( ck.bUnidx ){
166905 rc = 0;
166906 }else if( ck.bExpr ){
166907 rc = WHERE_EXPRIDX0x04000000;
166908 }else{
166909 rc = WHERE_IDX_ONLY0x00000040;
166910 }
166911 return rc;
166912}
166913
166914/*
166915** This is an sqlite3ParserAddCleanup() callback that is invoked to
166916** free the Parse->pIdxEpr list when the Parse object is destroyed.
166917*/
166918static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
166919 IndexedExpr **pp = (IndexedExpr**)pObject;
166920 while( *pp!=0 ){
166921 IndexedExpr *p = *pp;
166922 *pp = p->pIENext;
166923 sqlite3ExprDelete(db, p->pExpr);
166924 sqlite3DbFreeNN(db, p);
166925 }
166926}
166927
166928/*
166929** This function is called for a partial index - one with a WHERE clause - in
166930** two scenarios. In both cases, it determines whether or not the WHERE
166931** clause on the index implies that a column of the table may be safely
166932** replaced by a constant expression. For example, in the following
166933** SELECT:
166934**
166935** CREATE INDEX i1 ON t1(b, c) WHERE a=<expr>;
166936** SELECT a, b, c FROM t1 WHERE a=<expr> AND b=?;
166937**
166938** The "a" in the select-list may be replaced by <expr>, iff:
166939**
166940** (a) <expr> is a constant expression, and
166941** (b) The (a=<expr>) comparison uses the BINARY collation sequence, and
166942** (c) Column "a" has an affinity other than NONE or BLOB.
166943**
166944** If argument pItem is NULL, then pMask must not be NULL. In this case this
166945** function is being called as part of determining whether or not pIdx
166946** is a covering index. This function clears any bits in (*pMask)
166947** corresponding to columns that may be replaced by constants as described
166948** above.
166949**
166950** Otherwise, if pItem is not NULL, then this function is being called
166951** as part of coding a loop that uses index pIdx. In this case, add entries
166952** to the Parse.pIdxPartExpr list for each column that can be replaced
166953** by a constant.
166954*/
166955static void wherePartIdxExpr(
166956 Parse *pParse, /* Parse context */
166957 Index *pIdx, /* Partial index being processed */
166958 Expr *pPart, /* WHERE clause being processed */
166959 Bitmask *pMask, /* Mask to clear bits in */
166960 int iIdxCur, /* Cursor number for index */
166961 SrcItem *pItem /* The FROM clause entry for the table */
166962){
166963 assert( pItem==0 || (pItem->fg.jointype & JT_RIGHT)==0 )((void) (0));
166964 assert( (pItem==0 || pMask==0) && (pMask!=0 || pItem!=0) )((void) (0));
166965
166966 if( pPart->op==TK_AND44 ){
166967 wherePartIdxExpr(pParse, pIdx, pPart->pRight, pMask, iIdxCur, pItem);
166968 pPart = pPart->pLeft;
166969 }
166970
166971 if( (pPart->op==TK_EQ54 || pPart->op==TK_IS45) ){
166972 Expr *pLeft = pPart->pLeft;
166973 Expr *pRight = pPart->pRight;
166974 u8 aff;
166975
166976 if( pLeft->op!=TK_COLUMN168 ) return;
166977 if( !sqlite3ExprIsConstant(0, pRight) ) return;
166978 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pParse, pPart)) ) return;
166979 if( pLeft->iColumn<0 ) return;
166980 aff = pIdx->pTable->aCol[pLeft->iColumn].affinity;
166981 if( aff>=SQLITE_AFF_TEXT0x42 ){
166982 if( pItem ){
166983 sqlite3 *db = pParse->db;
166984 IndexedExpr *p = (IndexedExpr*)sqlite3DbMallocRaw(db, sizeof(*p));
166985 if( p ){
166986 int bNullRow = (pItem->fg.jointype&(JT_LEFT0x08|JT_LTORJ0x40))!=0;
166987 p->pExpr = sqlite3ExprDup(db, pRight, 0);
166988 p->iDataCur = pItem->iCursor;
166989 p->iIdxCur = iIdxCur;
166990 p->iIdxCol = pLeft->iColumn;
166991 p->bMaybeNullRow = bNullRow;
166992 p->pIENext = pParse->pIdxPartExpr;
166993 p->aff = aff;
166994 pParse->pIdxPartExpr = p;
166995 if( p->pIENext==0 ){
166996 void *pArg = (void*)&pParse->pIdxPartExpr;
166997 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pArg);
166998 }
166999 }
167000 }else if( pLeft->iColumn<(BMS((int)(sizeof(Bitmask)*8))-1) ){
167001 *pMask &= ~((Bitmask)1 << pLeft->iColumn);
167002 }
167003 }
167004 }
167005}
167006
167007
167008/*
167009** Add all WhereLoop objects for a single table of the join where the table
167010** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
167011** a b-tree table, not a virtual table.
167012**
167013** The costs (WhereLoop.rRun) of the b-tree loops added by this function
167014** are calculated as follows:
167015**
167016** For a full scan, assuming the table (or index) contains nRow rows:
167017**
167018** cost = nRow * 3.0 // full-table scan
167019** cost = nRow * K // scan of covering index
167020** cost = nRow * (K+3.0) // scan of non-covering index
167021**
167022** where K is a value between 1.1 and 3.0 set based on the relative
167023** estimated average size of the index and table records.
167024**
167025** For an index scan, where nVisit is the number of index rows visited
167026** by the scan, and nSeek is the number of seek operations required on
167027** the index b-tree:
167028**
167029** cost = nSeek * (log(nRow) + K * nVisit) // covering index
167030** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
167031**
167032** Normally, nSeek is 1. nSeek values greater than 1 come about if the
167033** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
167034** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
167035**
167036** The estimated values (nRow, nVisit, nSeek) often contain a large amount
167037** of uncertainty. For this reason, scoring is designed to pick plans that
167038** "do the least harm" if the estimates are inaccurate. For example, a
167039** log(nRow) factor is omitted from a non-covering index scan in order to
167040** bias the scoring in favor of using an index, since the worst-case
167041** performance of using an index is far better than the worst-case performance
167042** of a full table scan.
167043*/
167044static int whereLoopAddBtree(
167045 WhereLoopBuilder *pBuilder, /* WHERE clause information */
167046 Bitmask mPrereq /* Extra prerequisites for using this table */
167047){
167048 WhereInfo *pWInfo; /* WHERE analysis context */
167049 Index *pProbe; /* An index we are evaluating */
167050 Index sPk; /* A fake index object for the primary key */
167051 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
167052 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
167053 SrcList *pTabList; /* The FROM clause */
167054 SrcItem *pSrc; /* The FROM clause btree term to add */
167055 WhereLoop *pNew; /* Template WhereLoop object */
167056 int rc = SQLITE_OK0; /* Return code */
167057 int iSortIdx = 1; /* Index number */
167058 int b; /* A boolean value */
167059 LogEst rSize; /* number of rows in the table */
167060 WhereClause *pWC; /* The parsed WHERE clause */
167061 Table *pTab; /* Table being queried */
167062
167063 pNew = pBuilder->pNew;
167064 pWInfo = pBuilder->pWInfo;
167065 pTabList = pWInfo->pTabList;
167066 pSrc = pTabList->a + pNew->iTab;
167067 pTab = pSrc->pSTab;
167068 pWC = pBuilder->pWC;
167069 assert( !IsVirtual(pSrc->pSTab) )((void) (0));
167070
167071 if( pSrc->fg.isIndexedBy ){
167072 assert( pSrc->fg.isCte==0 )((void) (0));
167073 /* An INDEXED BY clause specifies a particular index to use */
167074 pProbe = pSrc->u2.pIBIndex;
167075 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
167076 pProbe = pTab->pIndex;
167077 }else{
167078 /* There is no INDEXED BY clause. Create a fake Index object in local
167079 ** variable sPk to represent the rowid primary key index. Make this
167080 ** fake index the first in a chain of Index objects with all of the real
167081 ** indices to follow */
167082 Index *pFirst; /* First of real indices on the table */
167083 memset(&sPk, 0, sizeof(Index));
167084 sPk.nKeyCol = 1;
167085 sPk.nColumn = 1;
167086 sPk.aiColumn = &aiColumnPk;
167087 sPk.aiRowLogEst = aiRowEstPk;
167088 sPk.onError = OE_Replace5;
167089 sPk.pTable = pTab;
167090 sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
167091 sPk.idxType = SQLITE_IDXTYPE_IPK3;
167092 aiRowEstPk[0] = pTab->nRowLogEst;
167093 aiRowEstPk[1] = 0;
167094 pFirst = pSrc->pSTab->pIndex;
167095 if( pSrc->fg.notIndexed==0 ){
167096 /* The real indices of the table are only considered if the
167097 ** NOT INDEXED qualifier is omitted from the FROM clause */
167098 sPk.pNext = pFirst;
167099 }
167100 pProbe = &sPk;
167101 }
167102 rSize = pTab->nRowLogEst;
167103
167104#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
167105 /* Automatic indexes */
167106 if( !pBuilder->pOrSet /* Not part of an OR optimization */
167107 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN0x1000|WHERE_OR_SUBCLAUSE0x0020))==0
167108 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex0x00008000)!=0
167109 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
167110 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
167111 && HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
167112 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
167113 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
167114 && (pSrc->fg.jointype & JT_RIGHT0x10)==0 /* Not the right tab of a RIGHT JOIN */
167115 ){
167116 /* Generate auto-index WhereLoops */
167117 LogEst rLogSize; /* Logarithm of the number of rows in the table */
167118 WhereTerm *pTerm;
167119 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
167120 rLogSize = estLog(rSize);
167121 for(pTerm=pWC->a; rc==SQLITE_OK0 && pTerm<pWCEnd; pTerm++){
167122 if( pTerm->prereqRight & pNew->maskSelf ) continue;
167123 if( termCanDriveIndex(pTerm, pSrc, 0) ){
167124 pNew->u.btree.nEq = 1;
167125 pNew->nSkip = 0;
167126 pNew->u.btree.pIndex = 0;
167127 pNew->nLTerm = 1;
167128 pNew->aLTerm[0] = pTerm;
167129 /* TUNING: One-time cost for computing the automatic index is
167130 ** estimated to be X*N*log2(N) where N is the number of rows in
167131 ** the table being indexed and where X is 7 (LogEst=28) for normal
167132 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
167133 ** of X is smaller for views and subqueries so that the query planner
167134 ** will be more aggressive about generating automatic indexes for
167135 ** those objects, since there is no opportunity to add schema
167136 ** indexes on subqueries and views. */
167137 pNew->rSetup = rLogSize + rSize;
167138 if( !IsView(pTab)((pTab)->eTabType==2) && (pTab->tabFlags & TF_Ephemeral0x00004000)==0 ){
167139 pNew->rSetup += 28;
167140 }else{
167141 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
167142 ** on ephemeral materializations of views */
167143 }
167144 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
167145 if( pNew->rSetup<0 ) pNew->rSetup = 0;
167146 /* TUNING: Each index lookup yields 20 rows in the table. This
167147 ** is more than the usual guess of 10 rows, since we have no way
167148 ** of knowing how selective the index will ultimately be. It would
167149 ** not be unreasonable to make this value much larger. */
167150 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) )((void) (0));
167151 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
167152 pNew->wsFlags = WHERE_AUTO_INDEX0x00004000;
167153 pNew->prereq = mPrereq | pTerm->prereqRight;
167154 rc = whereLoopInsert(pBuilder, pNew);
167155 }
167156 }
167157 }
167158#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
167159
167160 /* Loop over all indices. If there was an INDEXED BY clause, then only
167161 ** consider index pProbe. */
167162 for(; rc==SQLITE_OK0 && pProbe;
167163 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
167164 ){
167165 if( pProbe->pPartIdxWhere!=0
167166 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
167167 pProbe->pPartIdxWhere)
167168 ){
167169 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
167170 continue; /* Partial index inappropriate for this query */
167171 }
167172 if( pProbe->bNoQuery ) continue;
167173 rSize = pProbe->aiRowLogEst[0];
167174 pNew->u.btree.nEq = 0;
167175 pNew->u.btree.nBtm = 0;
167176 pNew->u.btree.nTop = 0;
167177 pNew->nSkip = 0;
167178 pNew->nLTerm = 0;
167179 pNew->iSortIdx = 0;
167180 pNew->rSetup = 0;
167181 pNew->prereq = mPrereq;
167182 pNew->nOut = rSize;
167183 pNew->u.btree.pIndex = pProbe;
167184 pNew->u.btree.pOrderBy = 0;
167185 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
167186
167187 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
167188 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 )((void) (0));
167189 if( pProbe->idxType==SQLITE_IDXTYPE_IPK3 ){
167190 /* Integer primary key index */
167191 pNew->wsFlags = WHERE_IPK0x00000100;
167192
167193 /* Full table scan */
167194 pNew->iSortIdx = b ? iSortIdx : 0;
167195 /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an
167196 ** extra cost designed to discourage the use of full table scans,
167197 ** since index lookups have better worst-case performance if our
167198 ** stat guesses are wrong. Reduce the 3.0 penalty slightly
167199 ** (to 2.75) if we have valid STAT4 information for the table.
167200 ** At 2.75, a full table scan is preferred over using an index on
167201 ** a column with just two distinct values where each value has about
167202 ** an equal number of appearances. Without STAT4 data, we still want
167203 ** to use an index in that case, since the constraint might be for
167204 ** the scarcer of the two values, and in that case an index lookup is
167205 ** better.
167206 */
167207#ifdef SQLITE_ENABLE_STAT4
167208 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat40x00002000)!=0);
167209#else
167210 pNew->rRun = rSize + 16;
167211#endif
167212 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
167213 whereLoopOutputAdjust(pWC, pNew, rSize);
167214 if( pSrc->fg.isSubquery ){
167215 if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE0x02000000;
167216 pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy;
167217 }
167218 rc = whereLoopInsert(pBuilder, pNew);
167219 pNew->nOut = rSize;
167220 if( rc ) break;
167221 }else{
167222 Bitmask m;
167223 if( pProbe->isCovering ){
167224 m = 0;
167225 pNew->wsFlags = WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200;
167226 }else{
167227 m = pSrc->colUsed & pProbe->colNotIdxed;
167228 if( pProbe->pPartIdxWhere ){
167229 wherePartIdxExpr(
167230 pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0
167231 );
167232 }
167233 pNew->wsFlags = WHERE_INDEXED0x00000200;
167234 if( m==TOPBIT(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
167235 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
167236 if( isCov==0 ){
167237 WHERETRACE(0x200,
167238 ("-> %s is not a covering index"
167239 " according to whereIsCoveringIndex()\n", pProbe->zName));
167240 assert( m!=0 )((void) (0));
167241 }else{
167242 m = 0;
167243 pNew->wsFlags |= isCov;
167244 if( isCov & WHERE_IDX_ONLY0x00000040 ){
167245 WHERETRACE(0x200,
167246 ("-> %s is a covering expression index"
167247 " according to whereIsCoveringIndex()\n", pProbe->zName));
167248 }else{
167249 assert( isCov==WHERE_EXPRIDX )((void) (0));
167250 WHERETRACE(0x200,
167251 ("-> %s might be a covering expression index"
167252 " according to whereIsCoveringIndex()\n", pProbe->zName));
167253 }
167254 }
167255 }else if( m==0
167256 && (HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) || pWInfo->pSelect!=0 || sqlite3FaultSim(700))
167257 ){
167258 WHERETRACE(0x200,
167259 ("-> %s a covering index according to bitmasks\n",
167260 pProbe->zName, m==0 ? "is" : "is not"));
167261 pNew->wsFlags = WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200;
167262 }
167263 }
167264
167265 /* Full scan via index */
167266 if( b
167267 || !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0)
167268 || pProbe->pPartIdxWhere!=0
167269 || pSrc->fg.isIndexedBy
167270 || ( m==0
167271 && pProbe->bUnordered==0
167272 && (pProbe->szIdxRow<pTab->szTabRow)
167273 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)==0
167274 && sqlite3GlobalConfigsqlite3Config.bUseCis
167275 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)(((pWInfo->pParse->db)->dbOptFlags&(0x00000020))
==0)
167276 )
167277 ){
167278 pNew->iSortIdx = b ? iSortIdx : 0;
167279
167280 /* The cost of visiting the index rows is N*K, where K is
167281 ** between 1.1 and 3.0, depending on the relative sizes of the
167282 ** index and table rows. */
167283 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
167284 if( m!=0 ){
167285 /* If this is a non-covering index scan, add in the cost of
167286 ** doing table lookups. The cost will be 3x the number of
167287 ** lookups. Take into account WHERE clause terms that can be
167288 ** satisfied using just the index, and that do not require a
167289 ** table lookup. */
167290 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
167291 int ii;
167292 int iCur = pSrc->iCursor;
167293 WhereClause *pWC2 = &pWInfo->sWC;
167294 for(ii=0; ii<pWC2->nTerm; ii++){
167295 WhereTerm *pTerm = &pWC2->a[ii];
167296 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
167297 break;
167298 }
167299 /* pTerm can be evaluated using just the index. So reduce
167300 ** the expected number of table lookups accordingly */
167301 if( pTerm->truthProb<=0 ){
167302 nLookup += pTerm->truthProb;
167303 }else{
167304 nLookup--;
167305 if( pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080) ) nLookup -= 19;
167306 }
167307 }
167308
167309 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
167310 }
167311 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
167312 whereLoopOutputAdjust(pWC, pNew, rSize);
167313 if( (pSrc->fg.jointype & JT_RIGHT0x10)!=0 && pProbe->aColExpr ){
167314 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
167315 ** because the cursor used to access the index might not be
167316 ** positioned to the correct row during the right-join no-match
167317 ** loop. */
167318 }else{
167319 rc = whereLoopInsert(pBuilder, pNew);
167320 }
167321 pNew->nOut = rSize;
167322 if( rc ) break;
167323 }
167324 }
167325
167326 pBuilder->bldFlags1 = 0;
167327 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
167328 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED0x0001 ){
167329 /* If a non-unique index is used, or if a prefix of the key for
167330 ** unique index is used (making the index functionally non-unique)
167331 ** then the sqlite_stat1 data becomes important for scoring the
167332 ** plan */
167333 pTab->tabFlags |= TF_MaybeReanalyze0x00000100;
167334 }
167335#ifdef SQLITE_ENABLE_STAT4
167336 sqlite3Stat4ProbeFree(pBuilder->pRec);
167337 pBuilder->nRecValid = 0;
167338 pBuilder->pRec = 0;
167339#endif
167340 }
167341 return rc;
167342}
167343
167344#ifndef SQLITE_OMIT_VIRTUALTABLE
167345
167346/*
167347** Return true if pTerm is a virtual table LIMIT or OFFSET term.
167348*/
167349static int isLimitTerm(WhereTerm *pTerm){
167350 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 )((void) (0));
167351 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT73
167352 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET74;
167353}
167354
167355/*
167356** Return true if the first nCons constraints in the pUsage array are
167357** marked as in-use (have argvIndex>0). False otherwise.
167358*/
167359static int allConstraintsUsed(
167360 struct sqlite3_index_constraint_usage *aUsage,
167361 int nCons
167362){
167363 int ii;
167364 for(ii=0; ii<nCons; ii++){
167365 if( aUsage[ii].argvIndex<=0 ) return 0;
167366 }
167367 return 1;
167368}
167369
167370/*
167371** Argument pIdxInfo is already populated with all constraints that may
167372** be used by the virtual table identified by pBuilder->pNew->iTab. This
167373** function marks a subset of those constraints usable, invokes the
167374** xBestIndex method and adds the returned plan to pBuilder.
167375**
167376** A constraint is marked usable if:
167377**
167378** * Argument mUsable indicates that its prerequisites are available, and
167379**
167380** * It is not one of the operators specified in the mExclude mask passed
167381** as the fourth argument (which in practice is either WO_IN or 0).
167382**
167383** Argument mPrereq is a mask of tables that must be scanned before the
167384** virtual table in question. These are added to the plans prerequisites
167385** before it is added to pBuilder.
167386**
167387** Output parameter *pbIn is set to true if the plan added to pBuilder
167388** uses one or more WO_IN terms, or false otherwise.
167389*/
167390static int whereLoopAddVirtualOne(
167391 WhereLoopBuilder *pBuilder,
167392 Bitmask mPrereq, /* Mask of tables that must be used. */
167393 Bitmask mUsable, /* Mask of usable tables */
167394 u16 mExclude, /* Exclude terms using these operators */
167395 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
167396 u16 mNoOmit, /* Do not omit these constraints */
167397 int *pbIn, /* OUT: True if plan uses an IN(...) op */
167398 int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */
167399){
167400 WhereClause *pWC = pBuilder->pWC;
167401 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
167402 struct sqlite3_index_constraint *pIdxCons;
167403 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
167404 int i;
167405 int mxTerm;
167406 int rc = SQLITE_OK0;
167407 WhereLoop *pNew = pBuilder->pNew;
167408 Parse *pParse = pBuilder->pWInfo->pParse;
167409 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
167410 int nConstraint = pIdxInfo->nConstraint;
167411
167412 assert( (mUsable & mPrereq)==mPrereq )((void) (0));
167413 *pbIn = 0;
167414 pNew->prereq = mPrereq;
167415
167416 /* Set the usable flag on the subset of constraints identified by
167417 ** arguments mUsable and mExclude. */
167418 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
167419 for(i=0; i<nConstraint; i++, pIdxCons++){
167420 WhereTerm *pTerm = termFromWhereClause(pWC, pIdxCons->iTermOffset);
167421 pIdxCons->usable = 0;
167422 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
167423 && (pTerm->eOperator & mExclude)==0
167424 && (pbRetryLimit || !isLimitTerm(pTerm))
167425 ){
167426 pIdxCons->usable = 1;
167427 }
167428 }
167429
167430 /* Initialize the output fields of the sqlite3_index_info structure */
167431 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
167432 assert( pIdxInfo->needToFreeIdxStr==0 )((void) (0));
167433 pIdxInfo->idxStr = 0;
167434 pIdxInfo->idxNum = 0;
167435 pIdxInfo->orderByConsumed = 0;
167436 pIdxInfo->estimatedCost = SQLITE_BIG_DBL(1e99) / (double)2;
167437 pIdxInfo->estimatedRows = 25;
167438 pIdxInfo->idxFlags = 0;
167439 pHidden->mHandleIn = 0;
167440
167441 /* Invoke the virtual table xBestIndex() method */
167442 rc = vtabBestIndex(pParse, pSrc->pSTab, pIdxInfo);
167443 if( rc ){
167444 if( rc==SQLITE_CONSTRAINT19 ){
167445 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
167446 ** that the particular combination of parameters provided is unusable.
167447 ** Make no entries in the loop table.
167448 */
167449 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
167450 freeIdxStr(pIdxInfo);
167451 return SQLITE_OK0;
167452 }
167453 return rc;
167454 }
167455
167456 mxTerm = -1;
167457 assert( pNew->nLSlot>=nConstraint )((void) (0));
167458 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
167459 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
167460 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
167461 for(i=0; i<nConstraint; i++, pIdxCons++){
167462 int iTerm;
167463 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
167464 WhereTerm *pTerm;
167465 int j = pIdxCons->iTermOffset;
167466 if( iTerm>=nConstraint
167467 || j<0
167468 || (pTerm = termFromWhereClause(pWC, j))==0
167469 || pNew->aLTerm[iTerm]!=0
167470 || pIdxCons->usable==0
167471 ){
167472 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName);
167473 freeIdxStr(pIdxInfo);
167474 return SQLITE_ERROR1;
167475 }
167476 testcase( iTerm==nConstraint-1 );
167477 testcase( j==0 );
167478 testcase( j==pWC->nTerm-1 );
167479 pNew->prereq |= pTerm->prereqRight;
167480 assert( iTerm<pNew->nLSlot )((void) (0));
167481 pNew->aLTerm[iTerm] = pTerm;
167482 if( iTerm>mxTerm ) mxTerm = iTerm;
167483 testcase( iTerm==15 );
167484 testcase( iTerm==16 );
167485 if( pUsage[i].omit ){
167486 if( i<16 && ((1<<i)&mNoOmit)==0 ){
167487 testcase( i!=iTerm );
167488 pNew->u.vtab.omitMask |= 1<<iTerm;
167489 }else{
167490 testcase( i!=iTerm );
167491 }
167492 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET74 ){
167493 pNew->u.vtab.bOmitOffset = 1;
167494 }
167495 }
167496 if( SMASKBIT32(i)((i)<=31?((unsigned int)1)<<(i):0) & pHidden->mHandleIn ){
167497 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm)(((unsigned int)1)<<(iTerm));
167498 }else if( (pTerm->eOperator & WO_IN0x0001)!=0 ){
167499 /* A virtual table that is constrained by an IN clause may not
167500 ** consume the ORDER BY clause because (1) the order of IN terms
167501 ** is not necessarily related to the order of output terms and
167502 ** (2) Multiple outputs from a single IN value will not merge
167503 ** together. */
167504 pIdxInfo->orderByConsumed = 0;
167505 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE0x00000001;
167506 *pbIn = 1; assert( (mExclude & WO_IN)==0 )((void) (0));
167507 }
167508
167509 /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
167510 ** terms. And if there are any, they should follow all other terms. */
167511 assert( pbRetryLimit || !isLimitTerm(pTerm) )((void) (0));
167512 assert( !isLimitTerm(pTerm) || i>=nConstraint-2 )((void) (0));
167513 assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) )((void) (0));
167514
167515 if( isLimitTerm(pTerm) && (*pbIn || !allConstraintsUsed(pUsage, i)) ){
167516 /* If there is an IN(...) term handled as an == (separate call to
167517 ** xFilter for each value on the RHS of the IN) and a LIMIT or
167518 ** OFFSET term handled as well, the plan is unusable. Similarly,
167519 ** if there is a LIMIT/OFFSET and there are other unused terms,
167520 ** the plan cannot be used. In these cases set variable *pbRetryLimit
167521 ** to true to tell the caller to retry with LIMIT and OFFSET
167522 ** disabled. */
167523 freeIdxStr(pIdxInfo);
167524 *pbRetryLimit = 1;
167525 return SQLITE_OK0;
167526 }
167527 }
167528 }
167529
167530 pNew->nLTerm = mxTerm+1;
167531 for(i=0; i<=mxTerm; i++){
167532 if( pNew->aLTerm[i]==0 ){
167533 /* The non-zero argvIdx values must be contiguous. Raise an
167534 ** error if they are not */
167535 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName);
167536 freeIdxStr(pIdxInfo);
167537 return SQLITE_ERROR1;
167538 }
167539 }
167540 assert( pNew->nLTerm<=pNew->nLSlot )((void) (0));
167541 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
167542 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
167543 pIdxInfo->needToFreeIdxStr = 0;
167544 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
167545 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
167546 pIdxInfo->nOrderBy : 0);
167547 pNew->u.vtab.bIdxNumHex = (pIdxInfo->idxFlags&SQLITE_INDEX_SCAN_HEX0x00000002)!=0;
167548 pNew->rSetup = 0;
167549 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
167550 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
167551
167552 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
167553 ** that the scan will visit at most one row. Clear it otherwise. */
167554 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE0x00000001 ){
167555 pNew->wsFlags |= WHERE_ONEROW0x00001000;
167556 }else{
167557 pNew->wsFlags &= ~WHERE_ONEROW0x00001000;
167558 }
167559 rc = whereLoopInsert(pBuilder, pNew);
167560 if( pNew->u.vtab.needFree ){
167561 sqlite3_free(pNew->u.vtab.idxStr);
167562 pNew->u.vtab.needFree = 0;
167563 }
167564 WHERETRACE(0xffffffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
167565 *pbIn, (sqlite3_uint64)mPrereq,
167566 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
167567
167568 return rc;
167569}
167570
167571/*
167572** Return the collating sequence for a constraint passed into xBestIndex.
167573**
167574** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex.
167575** This routine depends on there being a HiddenIndexInfo structure immediately
167576** following the sqlite3_index_info structure.
167577**
167578** Return a pointer to the collation name:
167579**
167580** 1. If there is an explicit COLLATE operator on the constraint, return it.
167581**
167582** 2. Else, if the column has an alternative collation, return that.
167583**
167584** 3. Otherwise, return "BINARY".
167585*/
167586SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
167587 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
167588 const char *zRet = 0;
167589 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
167590 CollSeq *pC = 0;
167591 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
167592 Expr *pX = termFromWhereClause(pHidden->pWC, iTerm)->pExpr;
167593 if( pX->pLeft ){
167594 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
167595 }
167596 zRet = (pC ? pC->zName : sqlite3StrBINARY);
167597 }
167598 return zRet;
167599}
167600
167601/*
167602** Return true if constraint iCons is really an IN(...) constraint, or
167603** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0)
167604** or clear (if bHandle==0) the flag to handle it using an iterator.
167605*/
167606SQLITE_API int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){
167607 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
167608 u32 m = SMASKBIT32(iCons)((iCons)<=31?((unsigned int)1)<<(iCons):0);
167609 if( m & pHidden->mIn ){
167610 if( bHandle==0 ){
167611 pHidden->mHandleIn &= ~m;
167612 }else if( bHandle>0 ){
167613 pHidden->mHandleIn |= m;
167614 }
167615 return 1;
167616 }
167617 return 0;
167618}
167619
167620/*
167621** This interface is callable from within the xBestIndex callback only.
167622**
167623** If possible, set (*ppVal) to point to an object containing the value
167624** on the right-hand-side of constraint iCons.
167625*/
167626SQLITE_API int sqlite3_vtab_rhs_value(
167627 sqlite3_index_info *pIdxInfo, /* Copy of first argument to xBestIndex */
167628 int iCons, /* Constraint for which RHS is wanted */
167629 sqlite3_value **ppVal /* Write value extracted here */
167630){
167631 HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1];
167632 sqlite3_value *pVal = 0;
167633 int rc = SQLITE_OK0;
167634 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
167635 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(167635); /* EV: R-30545-25046 */
167636 }else{
167637 if( pH->aRhs[iCons]==0 ){
167638 WhereTerm *pTerm = termFromWhereClause(
167639 pH->pWC, pIdxInfo->aConstraint[iCons].iTermOffset
167640 );
167641 rc = sqlite3ValueFromExpr(
167642 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db)((pH->pParse->db)->enc),
167643 SQLITE_AFF_BLOB0x41, &pH->aRhs[iCons]
167644 );
167645 testcase( rc!=SQLITE_OK );
167646 }
167647 pVal = pH->aRhs[iCons];
167648 }
167649 *ppVal = pVal;
167650
167651 if( rc==SQLITE_OK0 && pVal==0 ){ /* IMP: R-19933-32160 */
167652 rc = SQLITE_NOTFOUND12; /* IMP: R-36424-56542 */
167653 }
167654
167655 return rc;
167656}
167657
167658/*
167659** Return true if ORDER BY clause may be handled as DISTINCT.
167660*/
167661SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){
167662 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
167663 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 )((void) (0));
167664 return pHidden->eDistinct;
167665}
167666
167667/*
167668** Cause the prepared statement that is associated with a call to
167669** xBestIndex to potentially use all schemas. If the statement being
167670** prepared is read-only, then just start read transactions on all
167671** schemas. But if this is a write operation, start writes on all
167672** schemas.
167673**
167674** This is used by the (built-in) sqlite_dbpage virtual table.
167675*/
167676SQLITE_PRIVATEstatic void sqlite3VtabUsesAllSchemas(Parse *pParse){
167677 int nDb = pParse->db->nDb;
167678 int i;
167679 for(i=0; i<nDb; i++){
167680 sqlite3CodeVerifySchema(pParse, i);
167681 }
167682 if( DbMaskNonZero(pParse->writeMask)((pParse->writeMask)!=0) ){
167683 for(i=0; i<nDb; i++){
167684 sqlite3BeginWriteOperation(pParse, 0, i);
167685 }
167686 }
167687}
167688
167689/*
167690** Add all WhereLoop objects for a table of the join identified by
167691** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
167692**
167693** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
167694** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
167695** entries that occur before the virtual table in the FROM clause and are
167696** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
167697** mUnusable mask contains all FROM clause entries that occur after the
167698** virtual table and are separated from it by at least one LEFT or
167699** CROSS JOIN.
167700**
167701** For example, if the query were:
167702**
167703** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
167704**
167705** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
167706**
167707** All the tables in mPrereq must be scanned before the current virtual
167708** table. So any terms for which all prerequisites are satisfied by
167709** mPrereq may be specified as "usable" in all calls to xBestIndex.
167710** Conversely, all tables in mUnusable must be scanned after the current
167711** virtual table, so any terms for which the prerequisites overlap with
167712** mUnusable should always be configured as "not-usable" for xBestIndex.
167713*/
167714static int whereLoopAddVirtual(
167715 WhereLoopBuilder *pBuilder, /* WHERE clause information */
167716 Bitmask mPrereq, /* Tables that must be scanned before this one */
167717 Bitmask mUnusable /* Tables that must be scanned after this one */
167718){
167719 int rc = SQLITE_OK0; /* Return code */
167720 WhereInfo *pWInfo; /* WHERE analysis context */
167721 Parse *pParse; /* The parsing context */
167722 WhereClause *pWC; /* The WHERE clause */
167723 SrcItem *pSrc; /* The FROM clause term to search */
167724 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
167725 int nConstraint; /* Number of constraints in p */
167726 int bIn; /* True if plan uses IN(...) operator */
167727 WhereLoop *pNew;
167728 Bitmask mBest; /* Tables used by best possible plan */
167729 u16 mNoOmit;
167730 int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */
167731
167732 assert( (mPrereq & mUnusable)==0 )((void) (0));
167733 pWInfo = pBuilder->pWInfo;
167734 pParse = pWInfo->pParse;
167735 pWC = pBuilder->pWC;
167736 pNew = pBuilder->pNew;
167737 pSrc = &pWInfo->pTabList->a[pNew->iTab];
167738 assert( IsVirtual(pSrc->pSTab) )((void) (0));
167739 p = allocateIndexInfo(pWInfo, pWC, mUnusable, pSrc, &mNoOmit);
167740 if( p==0 ) return SQLITE_NOMEM_BKPT7;
167741 pNew->rSetup = 0;
167742 pNew->wsFlags = WHERE_VIRTUALTABLE0x00000400;
167743 pNew->nLTerm = 0;
167744 pNew->u.vtab.needFree = 0;
167745 nConstraint = p->nConstraint;
167746 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
167747 freeIndexInfo(pParse->db, p);
167748 return SQLITE_NOMEM_BKPT7;
167749 }
167750
167751 /* First call xBestIndex() with all constraints usable. */
167752 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pSTab->zName));
167753 WHERETRACE(0x800, (" VirtualOne: all usable\n"));
167754 rc = whereLoopAddVirtualOne(
167755 pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn, &bRetry
167756 );
167757 if( bRetry ){
167758 assert( rc==SQLITE_OK )((void) (0));
167759 rc = whereLoopAddVirtualOne(
167760 pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn, 0
167761 );
167762 }
167763
167764 /* If the call to xBestIndex() with all terms enabled produced a plan
167765 ** that does not require any source tables (IOW: a plan with mBest==0)
167766 ** and does not use an IN(...) operator, then there is no point in making
167767 ** any further calls to xBestIndex() since they will all return the same
167768 ** result (if the xBestIndex() implementation is sane). */
167769 if( rc==SQLITE_OK0 && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
167770 int seenZero = 0; /* True if a plan with no prereqs seen */
167771 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
167772 Bitmask mPrev = 0;
167773 Bitmask mBestNoIn = 0;
167774
167775 /* If the plan produced by the earlier call uses an IN(...) term, call
167776 ** xBestIndex again, this time with IN(...) terms disabled. */
167777 if( bIn ){
167778 WHERETRACE(0x800, (" VirtualOne: all usable w/o IN\n"));
167779 rc = whereLoopAddVirtualOne(
167780 pBuilder, mPrereq, ALLBITS((Bitmask)-1), WO_IN0x0001, p, mNoOmit, &bIn, 0);
167781 assert( bIn==0 )((void) (0));
167782 mBestNoIn = pNew->prereq & ~mPrereq;
167783 if( mBestNoIn==0 ){
167784 seenZero = 1;
167785 seenZeroNoIN = 1;
167786 }
167787 }
167788
167789 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
167790 ** in the set of terms that apply to the current virtual table. */
167791 while( rc==SQLITE_OK0 ){
167792 int i;
167793 Bitmask mNext = ALLBITS((Bitmask)-1);
167794 assert( mNext>0 )((void) (0));
167795 for(i=0; i<nConstraint; i++){
167796 int iTerm = p->aConstraint[i].iTermOffset;
167797 Bitmask mThis = termFromWhereClause(pWC, iTerm)->prereqRight & ~mPrereq;
167798 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
167799 }
167800 mPrev = mNext;
167801 if( mNext==ALLBITS((Bitmask)-1) ) break;
167802 if( mNext==mBest || mNext==mBestNoIn ) continue;
167803 WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
167804 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
167805 rc = whereLoopAddVirtualOne(
167806 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
167807 if( pNew->prereq==mPrereq ){
167808 seenZero = 1;
167809 if( bIn==0 ) seenZeroNoIN = 1;
167810 }
167811 }
167812
167813 /* If the calls to xBestIndex() in the above loop did not find a plan
167814 ** that requires no source tables at all (i.e. one guaranteed to be
167815 ** usable), make a call here with all source tables disabled */
167816 if( rc==SQLITE_OK0 && seenZero==0 ){
167817 WHERETRACE(0x800, (" VirtualOne: all disabled\n"));
167818 rc = whereLoopAddVirtualOne(
167819 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
167820 if( bIn==0 ) seenZeroNoIN = 1;
167821 }
167822
167823 /* If the calls to xBestIndex() have so far failed to find a plan
167824 ** that requires no source tables at all and does not use an IN(...)
167825 ** operator, make a final call to obtain one here. */
167826 if( rc==SQLITE_OK0 && seenZeroNoIN==0 ){
167827 WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n"));
167828 rc = whereLoopAddVirtualOne(
167829 pBuilder, mPrereq, mPrereq, WO_IN0x0001, p, mNoOmit, &bIn, 0);
167830 }
167831 }
167832
167833 freeIndexInfo(pParse->db, p);
167834 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pSTab->zName, rc));
167835 return rc;
167836}
167837#endif /* SQLITE_OMIT_VIRTUALTABLE */
167838
167839/*
167840** Add WhereLoop entries to handle OR terms. This works for either
167841** btrees or virtual tables.
167842*/
167843static int whereLoopAddOr(
167844 WhereLoopBuilder *pBuilder,
167845 Bitmask mPrereq,
167846 Bitmask mUnusable
167847){
167848 WhereInfo *pWInfo = pBuilder->pWInfo;
167849 WhereClause *pWC;
167850 WhereLoop *pNew;
167851 WhereTerm *pTerm, *pWCEnd;
167852 int rc = SQLITE_OK0;
167853 int iCur;
167854 WhereClause tempWC;
167855 WhereLoopBuilder sSubBuild;
167856 WhereOrSet sSum, sCur;
167857 SrcItem *pItem;
167858
167859 pWC = pBuilder->pWC;
167860 pWCEnd = pWC->a + pWC->nTerm;
167861 pNew = pBuilder->pNew;
167862 memset(&sSum, 0, sizeof(sSum));
167863 pItem = pWInfo->pTabList->a + pNew->iTab;
167864 iCur = pItem->iCursor;
167865
167866 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
167867 if( pItem->fg.jointype & JT_RIGHT0x10 ) return SQLITE_OK0;
167868
167869 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK0; pTerm++){
167870 if( (pTerm->eOperator & WO_OR0x0200)!=0
167871 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
167872 ){
167873 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
167874 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
167875 WhereTerm *pOrTerm;
167876 int once = 1;
167877 int i, j;
167878
167879 sSubBuild = *pBuilder;
167880 sSubBuild.pOrSet = &sCur;
167881
167882 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
167883 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
167884 if( (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
167885 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
167886 }else if( pOrTerm->leftCursor==iCur ){
167887 tempWC.pWInfo = pWC->pWInfo;
167888 tempWC.pOuter = pWC;
167889 tempWC.op = TK_AND44;
167890 tempWC.nTerm = 1;
167891 tempWC.nBase = 1;
167892 tempWC.a = pOrTerm;
167893 sSubBuild.pWC = &tempWC;
167894 }else{
167895 continue;
167896 }
167897 sCur.n = 0;
167898#ifdef WHERETRACE_ENABLED
167899 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
167900 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
167901 if( sqlite3WhereTrace & 0x20000 ){
167902 sqlite3WhereClausePrint(sSubBuild.pWC);
167903 }
167904#endif
167905#ifndef SQLITE_OMIT_VIRTUALTABLE
167906 if( IsVirtual(pItem->pSTab)((pItem->pSTab)->eTabType==1) ){
167907 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
167908 }else
167909#endif
167910 {
167911 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
167912 }
167913 if( rc==SQLITE_OK0 ){
167914 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
167915 }
167916 testcase( rc==SQLITE_NOMEM && sCur.n>0 );
167917 testcase( rc==SQLITE_DONE );
167918 if( sCur.n==0 ){
167919 sSum.n = 0;
167920 break;
167921 }else if( once ){
167922 whereOrMove(&sSum, &sCur);
167923 once = 0;
167924 }else{
167925 WhereOrSet sPrev;
167926 whereOrMove(&sPrev, &sSum);
167927 sSum.n = 0;
167928 for(i=0; i<sPrev.n; i++){
167929 for(j=0; j<sCur.n; j++){
167930 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
167931 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
167932 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
167933 }
167934 }
167935 }
167936 }
167937 pNew->nLTerm = 1;
167938 pNew->aLTerm[0] = pTerm;
167939 pNew->wsFlags = WHERE_MULTI_OR0x00002000;
167940 pNew->rSetup = 0;
167941 pNew->iSortIdx = 0;
167942 memset(&pNew->u, 0, sizeof(pNew->u));
167943 for(i=0; rc==SQLITE_OK0 && i<sSum.n; i++){
167944 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
167945 ** of all sub-scans required by the OR-scan. However, due to rounding
167946 ** errors, it may be that the cost of the OR-scan is equal to its
167947 ** most expensive sub-scan. Add the smallest possible penalty
167948 ** (equivalent to multiplying the cost by 1.07) to ensure that
167949 ** this does not happen. Otherwise, for WHERE clauses such as the
167950 ** following where there is an index on "y":
167951 **
167952 ** WHERE likelihood(x=?, 0.99) OR y=?
167953 **
167954 ** the planner may elect to "OR" together a full-table scan and an
167955 ** index lookup. And other similarly odd results. */
167956 pNew->rRun = sSum.a[i].rRun + 1;
167957 pNew->nOut = sSum.a[i].nOut;
167958 pNew->prereq = sSum.a[i].prereq;
167959 rc = whereLoopInsert(pBuilder, pNew);
167960 }
167961 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
167962 }
167963 }
167964 return rc;
167965}
167966
167967/*
167968** Add all WhereLoop objects for all tables
167969*/
167970static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
167971 WhereInfo *pWInfo = pBuilder->pWInfo;
167972 Bitmask mPrereq = 0;
167973 Bitmask mPrior = 0;
167974 int iTab;
167975 SrcList *pTabList = pWInfo->pTabList;
167976 SrcItem *pItem;
167977 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
167978 sqlite3 *db = pWInfo->pParse->db;
167979 int rc = SQLITE_OK0;
167980 int bFirstPastRJ = 0;
167981 int hasRightJoin = 0;
167982 WhereLoop *pNew;
167983
167984
167985 /* Loop over the tables in the join, from left to right */
167986 pNew = pBuilder->pNew;
167987
167988 /* Verify that pNew has already been initialized */
167989 assert( pNew->nLTerm==0 )((void) (0));
167990 assert( pNew->wsFlags==0 )((void) (0));
167991 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) )((void) (0));
167992 assert( pNew->aLTerm!=0 )((void) (0));
167993
167994 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT20000;
167995 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
167996 Bitmask mUnusable = 0;
167997 pNew->iTab = iTab;
167998 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR1000;
167999 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
168000 if( bFirstPastRJ
168001 || (pItem->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02|JT_LTORJ0x40))!=0
168002 ){
168003 /* Add prerequisites to prevent reordering of FROM clause terms
168004 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
168005 ** prevents the right operand of a RIGHT JOIN from being swapped with
168006 ** other elements even further to the right.
168007 **
168008 ** The JT_LTORJ case and the hasRightJoin flag work together to
168009 ** prevent FROM-clause terms from moving from the right side of
168010 ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
168011 ** is itself on the left side of a RIGHT JOIN.
168012 */
168013 if( pItem->fg.jointype & JT_LTORJ0x40 ) hasRightJoin = 1;
168014 mPrereq |= mPrior;
168015 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT0x10)!=0;
168016 }else if( !hasRightJoin ){
168017 mPrereq = 0;
168018 }
168019#ifndef SQLITE_OMIT_VIRTUALTABLE
168020 if( IsVirtual(pItem->pSTab)((pItem->pSTab)->eTabType==1) ){
168021 SrcItem *p;
168022 for(p=&pItem[1]; p<pEnd; p++){
168023 if( mUnusable || (p->fg.jointype & (JT_OUTER0x20|JT_CROSS0x02)) ){
168024 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
168025 }
168026 }
168027 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
168028 }else
168029#endif /* SQLITE_OMIT_VIRTUALTABLE */
168030 {
168031 rc = whereLoopAddBtree(pBuilder, mPrereq);
168032 }
168033 if( rc==SQLITE_OK0 && pBuilder->pWC->hasOr ){
168034 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
168035 }
168036 mPrior |= pNew->maskSelf;
168037 if( rc || db->mallocFailed ){
168038 if( rc==SQLITE_DONE101 ){
168039 /* We hit the query planner search limit set by iPlanLimit */
168040 sqlite3_log(SQLITE_WARNING28, "abbreviated query algorithm search");
168041 rc = SQLITE_OK0;
168042 }else{
168043 break;
168044 }
168045 }
168046 }
168047
168048 whereLoopClear(db, pNew);
168049 return rc;
168050}
168051
168052/* Implementation of the order-by-subquery optimization:
168053**
168054** WhereLoop pLoop, which the iLoop-th term of the nested loop, is really
168055** a subquery or CTE that has an ORDER BY clause. See if any of the terms
168056** in the subquery ORDER BY clause will satisfy pOrderBy from the outer
168057** query. Mark off all satisfied terms (by setting bits in *pOBSat) and
168058** return TRUE if they do. If not, return false.
168059**
168060** Example:
168061**
168062** CREATE TABLE t1(a,b,c, PRIMARY KEY(a,b));
168063** CREATE TABLE t2(x,y);
168064** WITH t3(p,q) AS MATERIALIZED (SELECT x+y, x-y FROM t2 ORDER BY x+y)
168065** SELECT * FROM t3 JOIN t1 ON a=q ORDER BY p, b;
168066**
168067** The CTE named "t3" comes out in the natural order of "p", so the first
168068** first them of "ORDER BY p,b" is satisfied by a sequential scan of "t3"
168069** and sorting only needs to occur on the second term "b".
168070**
168071** Limitations:
168072**
168073** (1) The optimization is not applied if the outer ORDER BY contains
168074** a COLLATE clause. The optimization might be applied if the
168075** outer ORDER BY uses NULLS FIRST, NULLS LAST, ASC, and/or DESC as
168076** long as the subquery ORDER BY does the same. But if the
168077** outer ORDER BY uses COLLATE, even a redundant COLLATE, the
168078** optimization is bypassed.
168079**
168080** (2) The subquery ORDER BY terms must exactly match subquery result
168081** columns, including any COLLATE annotations. This routine relies
168082** on iOrderByCol to do matching between order by terms and result
168083** columns, and iOrderByCol will not be set if the result column
168084** and ORDER BY collations differ.
168085**
168086** (3) The subquery and outer ORDER BY can be in opposite directions as
168087** long as the subquery is materialized. If the subquery is
168088** implemented as a co-routine, the sort orders must be in the same
168089** direction because there is no way to run a co-routine backwards.
168090*/
168091static SQLITE_NOINLINE__attribute__((noinline)) int wherePathMatchSubqueryOB(
168092 WhereInfo *pWInfo, /* The WHERE clause */
168093 WhereLoop *pLoop, /* The nested loop term that is a subquery */
168094 int iLoop, /* Which level of the nested loop. 0==outermost */
168095 int iCur, /* Cursor used by the this loop */
168096 ExprList *pOrderBy, /* The ORDER BY clause on the whole query */
168097 Bitmask *pRevMask, /* When loops need to go in reverse order */
168098 Bitmask *pOBSat /* Which terms of pOrderBy are satisfied so far */
168099){
168100 int iOB; /* Index into pOrderBy->a[] */
168101 int jSub; /* Index into pSubOB->a[] */
168102 u8 rev = 0; /* True if iOB and jSub sort in opposite directions */
168103 u8 revIdx = 0; /* Sort direction for jSub */
168104 Expr *pOBExpr; /* Current term of outer ORDER BY */
168105 ExprList *pSubOB; /* Complete ORDER BY on the subquery */
168106
168107 pSubOB = pLoop->u.btree.pOrderBy;
168108 assert( pSubOB!=0 )((void) (0));
168109 for(iOB=0; (MASKBIT(iOB)(((Bitmask)1)<<(iOB)) & *pOBSat)!=0; iOB++){}
168110 for(jSub=0; jSub<pSubOB->nExpr && iOB<pOrderBy->nExpr; jSub++, iOB++){
168111 if( pSubOB->a[jSub].u.x.iOrderByCol==0 ) break;
168112 pOBExpr = pOrderBy->a[iOB].pExpr;
168113 if( pOBExpr->op!=TK_COLUMN168 && pOBExpr->op!=TK_AGG_COLUMN170 ) break;
168114 if( pOBExpr->iTable!=iCur ) break;
168115 if( pOBExpr->iColumn!=pSubOB->a[jSub].u.x.iOrderByCol-1 ) break;
168116 if( (pWInfo->wctrlFlags & WHERE_GROUPBY0x0040)==0 ){
168117 u8 sfOB = pOrderBy->a[iOB].fg.sortFlags; /* sortFlags for iOB */
168118 u8 sfSub = pSubOB->a[jSub].fg.sortFlags; /* sortFlags for jSub */
168119 if( (sfSub & KEYINFO_ORDER_BIGNULL0x02) != (sfOB & KEYINFO_ORDER_BIGNULL0x02) ){
168120 break;
168121 }
168122 revIdx = sfSub & KEYINFO_ORDER_DESC0x01;
168123 if( jSub>0 ){
168124 if( (rev^revIdx)!=(sfOB & KEYINFO_ORDER_DESC0x01) ){
168125 break;
168126 }
168127 }else{
168128 rev = revIdx ^ (sfOB & KEYINFO_ORDER_DESC0x01);
168129 if( rev ){
168130 if( (pLoop->wsFlags & WHERE_COROUTINE0x02000000)!=0 ){
168131 /* Cannot run a co-routine in reverse order */
168132 break;
168133 }
168134 *pRevMask |= MASKBIT(iLoop)(((Bitmask)1)<<(iLoop));
168135 }
168136 }
168137 }
168138 *pOBSat |= MASKBIT(iOB)(((Bitmask)1)<<(iOB));
168139 }
168140 return jSub>0;
168141}
168142
168143/*
168144** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
168145** parameters) to see if it outputs rows in the requested ORDER BY
168146** (or GROUP BY) without requiring a separate sort operation. Return N:
168147**
168148** N>0: N terms of the ORDER BY clause are satisfied
168149** N==0: No terms of the ORDER BY clause are satisfied
168150** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
168151**
168152** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
168153** strict. With GROUP BY and DISTINCT the only requirement is that
168154** equivalent rows appear immediately adjacent to one another. GROUP BY
168155** and DISTINCT do not require rows to appear in any particular order as long
168156** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
168157** the pOrderBy terms can be matched in any order. With ORDER BY, the
168158** pOrderBy terms must be matched in strict left-to-right order.
168159*/
168160static i8 wherePathSatisfiesOrderBy(
168161 WhereInfo *pWInfo, /* The WHERE clause */
168162 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
168163 WherePath *pPath, /* The WherePath to check */
168164 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
168165 u16 nLoop, /* Number of entries in pPath->aLoop[] */
168166 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
168167 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
168168){
168169 u8 revSet; /* True if rev is known */
168170 u8 rev; /* Composite sort order */
168171 u8 revIdx; /* Index sort order */
168172 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
168173 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
168174 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
168175 u16 eqOpMask; /* Allowed equality operators */
168176 u16 nKeyCol; /* Number of key columns in pIndex */
168177 u16 nColumn; /* Total number of ordered columns in the index */
168178 u16 nOrderBy; /* Number terms in the ORDER BY clause */
168179 int iLoop; /* Index of WhereLoop in pPath being processed */
168180 int i, j; /* Loop counters */
168181 int iCur; /* Cursor number for current WhereLoop */
168182 int iColumn; /* A column number within table iCur */
168183 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
168184 WhereTerm *pTerm; /* A single term of the WHERE clause */
168185 Expr *pOBExpr; /* An expression from the ORDER BY clause */
168186 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
168187 Index *pIndex; /* The index associated with pLoop */
168188 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
168189 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
168190 Bitmask obDone; /* Mask of all ORDER BY terms */
168191 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
168192 Bitmask ready; /* Mask of inner loops */
168193
168194 /*
168195 ** We say the WhereLoop is "one-row" if it generates no more than one
168196 ** row of output. A WhereLoop is one-row if all of the following are true:
168197 ** (a) All index columns match with WHERE_COLUMN_EQ.
168198 ** (b) The index is unique
168199 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
168200 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
168201 **
168202 ** We say the WhereLoop is "order-distinct" if the set of columns from
168203 ** that WhereLoop that are in the ORDER BY clause are different for every
168204 ** row of the WhereLoop. Every one-row WhereLoop is automatically
168205 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
168206 ** is not order-distinct. To be order-distinct is not quite the same as being
168207 ** UNIQUE since a UNIQUE column or index can have multiple rows that
168208 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
168209 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
168210 **
168211 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
168212 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
168213 ** automatically order-distinct.
168214 */
168215
168216 assert( pOrderBy!=0 )((void) (0));
168217 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin)(((db)->dbOptFlags&(0x00000040))!=0) ) return 0;
168218
168219 nOrderBy = pOrderBy->nExpr;
168220 testcase( nOrderBy==BMS-1 );
168221 if( nOrderBy>BMS((int)(sizeof(Bitmask)*8))-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
168222 isOrderDistinct = 1;
168223 obDone = MASKBIT(nOrderBy)(((Bitmask)1)<<(nOrderBy))-1;
168224 orderDistinctMask = 0;
168225 ready = 0;
168226 eqOpMask = WO_EQ0x0002 | WO_IS0x0080 | WO_ISNULL0x0100;
168227 if( wctrlFlags & (WHERE_ORDERBY_LIMIT0x0800|WHERE_ORDERBY_MAX0x0002|WHERE_ORDERBY_MIN0x0001) ){
168228 eqOpMask |= WO_IN0x0001;
168229 }
168230 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
168231 if( iLoop>0 ) ready |= pLoop->maskSelf;
168232 if( iLoop<nLoop ){
168233 pLoop = pPath->aLoop[iLoop];
168234 if( wctrlFlags & WHERE_ORDERBY_LIMIT0x0800 ) continue;
168235 }else{
168236 pLoop = pLast;
168237 }
168238 if( pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
168239 if( pLoop->u.vtab.isOrdered
168240 && ((wctrlFlags&(WHERE_DISTINCTBY0x0080|WHERE_SORTBYGROUP0x0200))!=WHERE_DISTINCTBY0x0080)
168241 ){
168242 obSat = obDone;
168243 }
168244 break;
168245 }else if( wctrlFlags & WHERE_DISTINCTBY0x0080 ){
168246 pLoop->u.btree.nDistinctCol = 0;
168247 }
168248 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
168249
168250 /* Mark off any ORDER BY term X that is a column in the table of
168251 ** the current loop for which there is term in the WHERE
168252 ** clause of the form X IS NULL or X=? that reference only outer
168253 ** loops.
168254 */
168255 for(i=0; i<nOrderBy; i++){
168256 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
168257 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
168258 if( NEVER(pOBExpr==0)(pOBExpr==0) ) continue;
168259 if( pOBExpr->op!=TK_COLUMN168 && pOBExpr->op!=TK_AGG_COLUMN170 ) continue;
168260 if( pOBExpr->iTable!=iCur ) continue;
168261 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
168262 ~ready, eqOpMask, 0);
168263 if( pTerm==0 ) continue;
168264 if( pTerm->eOperator==WO_IN0x0001 ){
168265 /* IN terms are only valid for sorting in the ORDER BY LIMIT
168266 ** optimization, and then only if they are actually used
168267 ** by the query plan */
168268 assert( wctrlFlags &((void) (0))
168269 (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) )((void) (0));
168270 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
168271 if( j>=pLoop->nLTerm ) continue;
168272 }
168273 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0 && pOBExpr->iColumn>=0 ){
168274 Parse *pParse = pWInfo->pParse;
168275 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
168276 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
168277 assert( pColl1 )((void) (0));
168278 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
168279 continue;
168280 }
168281 testcase( pTerm->pExpr->op==TK_IS );
168282 }
168283 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
168284 }
168285
168286 if( (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0 ){
168287 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
168288 if( pLoop->u.btree.pOrderBy
168289 && OptimizationEnabled(db, SQLITE_OrderBySubq)(((db)->dbOptFlags&(0x10000000))==0)
168290 && wherePathMatchSubqueryOB(pWInfo,pLoop,iLoop,iCur,
168291 pOrderBy,pRevMask, &obSat)
168292 ){
168293 nColumn = 0;
168294 isOrderDistinct = 0;
168295 }else{
168296 nColumn = 1;
168297 }
168298 pIndex = 0;
168299 nKeyCol = 0;
168300 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
168301 return 0;
168302 }else{
168303 nKeyCol = pIndex->nKeyCol;
168304 nColumn = pIndex->nColumn;
168305 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) )((void) (0));
168306 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID((void) (0))
168307 || !HasRowid(pIndex->pTable))((void) (0));
168308 /* All relevant terms of the index must also be non-NULL in order
168309 ** for isOrderDistinct to be true. So the isOrderDistinct value
168310 ** computed here might be a false positive. Corrections will be
168311 ** made at tag-20210426-1 below */
168312 isOrderDistinct = IsUniqueIndex(pIndex)((pIndex)->onError!=0)
168313 && (pLoop->wsFlags & WHERE_SKIPSCAN0x00008000)==0;
168314 }
168315
168316 /* Loop through all columns of the index and deal with the ones
168317 ** that are not constrained by == or IN.
168318 */
168319 rev = revSet = 0;
168320 distinctColumns = 0;
168321 for(j=0; j<nColumn; j++){
168322 u8 bOnce = 1; /* True to run the ORDER BY search loop */
168323
168324 assert( j>=pLoop->u.btree.nEq((void) (0))
168325 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)((void) (0))
168326 )((void) (0));
168327 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
168328 u16 eOp = pLoop->aLTerm[j]->eOperator;
168329
168330 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
168331 ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL
168332 ** terms imply that the index is not UNIQUE NOT NULL in which case
168333 ** the loop need to be marked as not order-distinct because it can
168334 ** have repeated NULL rows.
168335 **
168336 ** If the current term is a column of an ((?,?) IN (SELECT...))
168337 ** expression for which the SELECT returns more than one column,
168338 ** check that it is the only column used by this loop. Otherwise,
168339 ** if it is one of two or more, none of the columns can be
168340 ** considered to match an ORDER BY term.
168341 */
168342 if( (eOp & eqOpMask)!=0 ){
168343 if( eOp & (WO_ISNULL0x0100|WO_IS0x0080) ){
168344 testcase( eOp & WO_ISNULL );
168345 testcase( eOp & WO_IS );
168346 testcase( isOrderDistinct );
168347 isOrderDistinct = 0;
168348 }
168349 continue;
168350 }else if( ALWAYS(eOp & WO_IN)(eOp & 0x0001) ){
168351 /* ALWAYS() justification: eOp is an equality operator due to the
168352 ** j<pLoop->u.btree.nEq constraint above. Any equality other
168353 ** than WO_IN is captured by the previous "if". So this one
168354 ** always has to be WO_IN. */
168355 Expr *pX = pLoop->aLTerm[j]->pExpr;
168356 for(i=j+1; i<pLoop->u.btree.nEq; i++){
168357 if( pLoop->aLTerm[i]->pExpr==pX ){
168358 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) )((void) (0));
168359 bOnce = 0;
168360 break;
168361 }
168362 }
168363 }
168364 }
168365
168366 /* Get the column number in the table (iColumn) and sort order
168367 ** (revIdx) for the j-th column of the index.
168368 */
168369 if( pIndex ){
168370 iColumn = pIndex->aiColumn[j];
168371 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC0x01;
168372 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID(-1);
168373 }else{
168374 iColumn = XN_ROWID(-1);
168375 revIdx = 0;
168376 }
168377
168378 /* An unconstrained column that might be NULL means that this
168379 ** WhereLoop is not well-ordered. tag-20210426-1
168380 */
168381 if( isOrderDistinct ){
168382 if( iColumn>=0
168383 && j>=pLoop->u.btree.nEq
168384 && pIndex->pTable->aCol[iColumn].notNull==0
168385 ){
168386 isOrderDistinct = 0;
168387 }
168388 if( iColumn==XN_EXPR(-2) ){
168389 isOrderDistinct = 0;
168390 }
168391 }
168392
168393 /* Find the ORDER BY term that corresponds to the j-th column
168394 ** of the index and mark that ORDER BY term having been satisfied.
168395 */
168396 isMatch = 0;
168397 for(i=0; bOnce && i<nOrderBy; i++){
168398 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
168399 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
168400 testcase( wctrlFlags & WHERE_GROUPBY );
168401 testcase( wctrlFlags & WHERE_DISTINCTBY );
168402 if( NEVER(pOBExpr==0)(pOBExpr==0) ) continue;
168403 if( (wctrlFlags & (WHERE_GROUPBY0x0040|WHERE_DISTINCTBY0x0080))==0 ) bOnce = 0;
168404 if( iColumn>=XN_ROWID(-1) ){
168405 if( pOBExpr->op!=TK_COLUMN168 && pOBExpr->op!=TK_AGG_COLUMN170 ) continue;
168406 if( pOBExpr->iTable!=iCur ) continue;
168407 if( pOBExpr->iColumn!=iColumn ) continue;
168408 }else{
168409 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
168410 if( sqlite3ExprCompareSkip(pOBExpr, pIxExpr, iCur) ){
168411 continue;
168412 }
168413 }
168414 if( iColumn!=XN_ROWID(-1) ){
168415 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
168416 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
168417 }
168418 if( wctrlFlags & WHERE_DISTINCTBY0x0080 ){
168419 pLoop->u.btree.nDistinctCol = j+1;
168420 }
168421 isMatch = 1;
168422 break;
168423 }
168424 if( isMatch && (wctrlFlags & WHERE_GROUPBY0x0040)==0 ){
168425 /* Make sure the sort order is compatible in an ORDER BY clause.
168426 ** Sort order is irrelevant for a GROUP BY clause. */
168427 if( revSet ){
168428 if( (rev ^ revIdx)
168429 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC0x01)
168430 ){
168431 isMatch = 0;
168432 }
168433 }else{
168434 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC0x01);
168435 if( rev ) *pRevMask |= MASKBIT(iLoop)(((Bitmask)1)<<(iLoop));
168436 revSet = 1;
168437 }
168438 }
168439 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02) ){
168440 if( j==pLoop->u.btree.nEq ){
168441 pLoop->wsFlags |= WHERE_BIGNULL_SORT0x00080000;
168442 }else{
168443 isMatch = 0;
168444 }
168445 }
168446 if( isMatch ){
168447 if( iColumn==XN_ROWID(-1) ){
168448 testcase( distinctColumns==0 );
168449 distinctColumns = 1;
168450 }
168451 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
168452 }else{
168453 /* No match found */
168454 if( j==0 || j<nKeyCol ){
168455 testcase( isOrderDistinct!=0 );
168456 isOrderDistinct = 0;
168457 }
168458 break;
168459 }
168460 } /* end Loop over all index columns */
168461 if( distinctColumns ){
168462 testcase( isOrderDistinct==0 );
168463 isOrderDistinct = 1;
168464 }
168465 } /* end-if not one-row */
168466
168467 /* Mark off any other ORDER BY terms that reference pLoop */
168468 if( isOrderDistinct ){
168469 orderDistinctMask |= pLoop->maskSelf;
168470 for(i=0; i<nOrderBy; i++){
168471 Expr *p;
168472 Bitmask mTerm;
168473 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
168474 p = pOrderBy->a[i].pExpr;
168475 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
168476 if( mTerm==0 && !sqlite3ExprIsConstant(0,p) ) continue;
168477 if( (mTerm&~orderDistinctMask)==0 ){
168478 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
168479 }
168480 }
168481 }
168482 } /* End the loop over all WhereLoops from outer-most down to inner-most */
168483 if( obSat==obDone ) return (i8)nOrderBy;
168484 if( !isOrderDistinct ){
168485 for(i=nOrderBy-1; i>0; i--){
168486 Bitmask m = ALWAYS(i<BMS)(i<((int)(sizeof(Bitmask)*8))) ? MASKBIT(i)(((Bitmask)1)<<(i)) - 1 : 0;
168487 if( (obSat&m)==m ) return i;
168488 }
168489 return 0;
168490 }
168491 return -1;
168492}
168493
168494
168495/*
168496** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
168497** the planner assumes that the specified pOrderBy list is actually a GROUP
168498** BY clause - and so any order that groups rows as required satisfies the
168499** request.
168500**
168501** Normally, in this case it is not possible for the caller to determine
168502** whether or not the rows are really being delivered in sorted order, or
168503** just in some other order that provides the required grouping. However,
168504** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
168505** this function may be called on the returned WhereInfo object. It returns
168506** true if the rows really will be sorted in the specified order, or false
168507** otherwise.
168508**
168509** For example, assuming:
168510**
168511** CREATE INDEX i1 ON t1(x, Y);
168512**
168513** then
168514**
168515** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
168516** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
168517*/
168518SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo *pWInfo){
168519 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) )((void) (0));
168520 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP )((void) (0));
168521 return pWInfo->sorted;
168522}
168523
168524#ifdef WHERETRACE_ENABLED
168525/* For debugging use only: */
168526static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
168527 static char zName[65];
168528 int i;
168529 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
168530 if( pLast ) zName[i++] = pLast->cId;
168531 zName[i] = 0;
168532 return zName;
168533}
168534#endif
168535
168536/*
168537** Return the cost of sorting nRow rows, assuming that the keys have
168538** nOrderby columns and that the first nSorted columns are already in
168539** order.
168540*/
168541static LogEst whereSortingCost(
168542 WhereInfo *pWInfo, /* Query planning context */
168543 LogEst nRow, /* Estimated number of rows to sort */
168544 int nOrderBy, /* Number of ORDER BY clause terms */
168545 int nSorted /* Number of initial ORDER BY terms naturally in order */
168546){
168547 /* Estimated cost of a full external sort, where N is
168548 ** the number of rows to sort is:
168549 **
168550 ** cost = (K * N * log(N)).
168551 **
168552 ** Or, if the order-by clause has X terms but only the last Y
168553 ** terms are out of order, then block-sorting will reduce the
168554 ** sorting cost to:
168555 **
168556 ** cost = (K * N * log(N)) * (Y/X)
168557 **
168558 ** The constant K is at least 2.0 but will be larger if there are a
168559 ** large number of columns to be sorted, as the sorting time is
168560 ** proportional to the amount of content to be sorted. The algorithm
168561 ** does not currently distinguish between fat columns (BLOBs and TEXTs)
168562 ** and skinny columns (INTs). It just uses the number of columns as
168563 ** an approximation for the row width.
168564 **
168565 ** And extra factor of 2.0 or 3.0 is added to the sorting cost if the sort
168566 ** is built using OP_IdxInsert and OP_Sort rather than with OP_SorterInsert.
168567 */
168568 LogEst rSortCost, nCol;
168569 assert( pWInfo->pSelect!=0 )((void) (0));
168570 assert( pWInfo->pSelect->pEList!=0 )((void) (0));
168571 /* TUNING: sorting cost proportional to the number of output columns: */
168572 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
168573 rSortCost = nRow + nCol;
168574 if( nSorted>0 ){
168575 /* Scale the result by (Y/X) */
168576 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
168577 }
168578
168579 /* Multiple by log(M) where M is the number of output rows.
168580 ** Use the LIMIT for M if it is smaller. Or if this sort is for
168581 ** a DISTINCT operator, M will be the number of distinct output
168582 ** rows, so fudge it downwards a bit.
168583 */
168584 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT0x4000)!=0 ){
168585 rSortCost += 10; /* TUNING: Extra 2.0x if using LIMIT */
168586 if( nSorted!=0 ){
168587 rSortCost += 6; /* TUNING: Extra 1.5x if also using partial sort */
168588 }
168589 if( pWInfo->iLimit<nRow ){
168590 nRow = pWInfo->iLimit;
168591 }
168592 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100) ){
168593 /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
168594 ** reduces the number of output rows by a factor of 2 */
168595 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) )((void) (0)); }
168596 }
168597 rSortCost += estLog(nRow);
168598 return rSortCost;
168599}
168600
168601/*
168602** Compute the maximum number of paths in the solver algorithm, for
168603** queries that have three or more terms in the FROM clause. Queries with
168604** two or fewer FROM clause terms are handled by the caller.
168605**
168606** Query planning is NP-hard. We must limit the number of paths at
168607** each step of the solver search algorithm to avoid exponential behavior.
168608**
168609** The value returned is a tuning parameter. Currently the value is:
168610**
168611** 18 for star queries
168612** 12 otherwise
168613**
168614** For the purposes of SQLite, a star-query is defined as a query
168615** with a large central table that is joined against four or more
168616** smaller tables. The central table is called the "fact" table.
168617** The smaller tables that get joined are "dimension tables".
168618**
168619** SIDE EFFECT: (and really the whole point of this subroutine)
168620**
168621** If pWInfo describes a star-query, then the cost on WhereLoops for the
168622** fact table is reduced. This heuristic helps keep fact tables in
168623** outer loops. Without this heuristic, paths with fact tables in outer
168624** loops tend to get pruned by the mxChoice limit on the number of paths,
168625** resulting in poor query plans. The total amount of heuristic cost
168626** adjustment is stored in pWInfo->nOutStarDelta and the cost adjustment
168627** for each WhereLoop is stored in its rStarDelta field.
168628*/
168629static int computeMxChoice(WhereInfo *pWInfo, LogEst nRowEst){
168630 int nLoop = pWInfo->nLevel; /* Number of terms in the join */
168631 if( nRowEst==0 && nLoop>=5 ){
168632 /* Check to see if we are dealing with a star schema and if so, reduce
168633 ** the cost of fact tables relative to dimension tables, as a heuristic
168634 ** to help keep the fact tables in outer loops.
168635 */
168636 int iLoop; /* Counter over join terms */
168637 Bitmask m; /* Bitmask for current loop */
168638 assert( pWInfo->nOutStarDelta==0 )((void) (0));
168639 for(iLoop=0, m=1; iLoop<nLoop; iLoop++, m<<=1){
168640 WhereLoop *pWLoop; /* For looping over WhereLoops */
168641 int nDep = 0; /* Number of dimension tables */
168642 LogEst rDelta; /* Heuristic cost adjustment */
168643 Bitmask mSeen = 0; /* Mask of dimension tables */
168644 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
168645 if( (pWLoop->prereq & m)!=0 && (pWLoop->maskSelf & mSeen)==0 ){
168646 nDep++;
168647 mSeen |= pWLoop->maskSelf;
168648 }
168649 }
168650 if( nDep<=3 ) continue;
168651 rDelta = 15*(nDep-3);
168652#ifdef WHERETRACE_ENABLED /* 0x4 */
168653 if( sqlite3WhereTrace&0x4 ){
168654 SrcItem *pItem = pWInfo->pTabList->a + iLoop;
168655 sqlite3DebugPrintf("Fact-table %s: %d dimensions, cost reduced %d\n",
168656 pItem->zAlias ? pItem->zAlias : pItem->pSTab->zName,
168657 nDep, rDelta);
168658 }
168659#endif
168660 if( pWInfo->nOutStarDelta==0 ){
168661 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
168662 pWLoop->rStarDelta = 0;
168663 }
168664 }
168665 pWInfo->nOutStarDelta += rDelta;
168666 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
168667 if( pWLoop->maskSelf==m ){
168668 pWLoop->rRun -= rDelta;
168669 pWLoop->nOut -= rDelta;
168670 pWLoop->rStarDelta = rDelta;
168671 }
168672 }
168673 }
168674 }
168675 return pWInfo->nOutStarDelta>0 ? 18 : 12;
168676}
168677
168678/*
168679** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
168680** attempts to find the lowest cost path that visits each WhereLoop
168681** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
168682**
168683** Assume that the total number of output rows that will need to be sorted
168684** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
168685** costs if nRowEst==0.
168686**
168687** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
168688** error occurs.
168689*/
168690static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
168691 int mxChoice; /* Maximum number of simultaneous paths tracked */
168692 int nLoop; /* Number of terms in the join */
168693 Parse *pParse; /* Parsing context */
168694 int iLoop; /* Loop counter over the terms of the join */
168695 int ii, jj; /* Loop counters */
168696 int mxI = 0; /* Index of next entry to replace */
168697 int nOrderBy; /* Number of ORDER BY clause terms */
168698 LogEst mxCost = 0; /* Maximum cost of a set of paths */
168699 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
168700 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
168701 WherePath *aFrom; /* All nFrom paths at the previous level */
168702 WherePath *aTo; /* The nTo best paths at the current level */
168703 WherePath *pFrom; /* An element of aFrom[] that we are working on */
168704 WherePath *pTo; /* An element of aTo[] that we are working on */
168705 WhereLoop *pWLoop; /* One of the WhereLoop objects */
168706 WhereLoop **pX; /* Used to divy up the pSpace memory */
168707 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
168708 char *pSpace; /* Temporary memory used by this routine */
168709 int nSpace; /* Bytes of space allocated at pSpace */
168710
168711 pParse = pWInfo->pParse;
168712 nLoop = pWInfo->nLevel;
168713 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n",
168714 nRowEst, pParse->nQueryLoop));
168715 /* TUNING: mxChoice is the maximum number of possible paths to preserve
168716 ** at each step. Based on the number of loops in the FROM clause:
168717 **
168718 ** nLoop mxChoice
168719 ** ----- --------
168720 ** 1 1 // the most common case
168721 ** 2 5
168722 ** 3+ 12 or 18 // see computeMxChoice()
168723 */
168724 if( nLoop<=1 ){
168725 mxChoice = 1;
168726 }else if( nLoop==2 ){
168727 mxChoice = 5;
168728 }else{
168729 mxChoice = computeMxChoice(pWInfo, nRowEst);
168730 }
168731 assert( nLoop<=pWInfo->pTabList->nSrc )((void) (0));
168732
168733 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
168734 ** case the purpose of this call is to estimate the number of rows returned
168735 ** by the overall query. Once this estimate has been obtained, the caller
168736 ** will invoke this function a second time, passing the estimate as the
168737 ** nRowEst parameter. */
168738 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
168739 nOrderBy = 0;
168740 }else{
168741 nOrderBy = pWInfo->pOrderBy->nExpr;
168742 }
168743
168744 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
168745 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
168746 nSpace += sizeof(LogEst) * nOrderBy;
168747 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace)sqlite3DbMallocRawNN(pParse->db,nSpace);
168748 if( pSpace==0 ) return SQLITE_NOMEM_BKPT7;
168749 aTo = (WherePath*)pSpace;
168750 aFrom = aTo+mxChoice;
168751 memset(aFrom, 0, sizeof(aFrom[0]));
168752 pX = (WhereLoop**)(aFrom+mxChoice);
168753 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
168754 pFrom->aLoop = pX;
168755 }
168756 if( nOrderBy ){
168757 /* If there is an ORDER BY clause and it is not being ignored, set up
168758 ** space for the aSortCost[] array. Each element of the aSortCost array
168759 ** is either zero - meaning it has not yet been initialized - or the
168760 ** cost of sorting nRowEst rows of data where the first X terms of
168761 ** the ORDER BY clause are already in order, where X is the array
168762 ** index. */
168763 aSortCost = (LogEst*)pX;
168764 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
168765 }
168766 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] )((void) (0));
168767 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX )((void) (0));
168768
168769 /* Seed the search with a single WherePath containing zero WhereLoops.
168770 **
168771 ** TUNING: Do not let the number of iterations go above 28. If the cost
168772 ** of computing an automatic index is not paid back within the first 28
168773 ** rows, then do not use the automatic index. */
168774 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48)((pParse->nQueryLoop)<(48)?(pParse->nQueryLoop):(48)
)
; assert( 48==sqlite3LogEst(28) )((void) (0));
168775 nFrom = 1;
168776 assert( aFrom[0].isOrdered==0 )((void) (0));
168777 if( nOrderBy ){
168778 /* If nLoop is zero, then there are no FROM terms in the query. Since
168779 ** in this case the query may return a maximum of one row, the results
168780 ** are already in the requested order. Set isOrdered to nOrderBy to
168781 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
168782 ** -1, indicating that the result set may or may not be ordered,
168783 ** depending on the loops added to the current plan. */
168784 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
168785 }
168786
168787 /* Compute successively longer WherePaths using the previous generation
168788 ** of WherePaths as the basis for the next. Keep track of the mxChoice
168789 ** best paths at each generation */
168790 for(iLoop=0; iLoop<nLoop; iLoop++){
168791 nTo = 0;
168792 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
168793 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
168794 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
168795 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
168796 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
168797 i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */
168798 Bitmask maskNew; /* Mask of src visited by (..) */
168799 Bitmask revMask; /* Mask of rev-order loops for (..) */
168800
168801 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
168802 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
168803 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && pFrom->nRow<3 ){
168804 /* Do not use an automatic index if the this loop is expected
168805 ** to run less than 1.25 times. It is tempting to also exclude
168806 ** automatic index usage on an outer loop, but sometimes an automatic
168807 ** index is useful in the outer loop of a correlated subquery. */
168808 assert( 10==sqlite3LogEst(2) )((void) (0));
168809 continue;
168810 }
168811
168812 /* At this point, pWLoop is a candidate to be the next loop.
168813 ** Compute its cost */
168814 rUnsorted = pWLoop->rRun + pFrom->nRow;
168815 if( pWLoop->rSetup ){
168816 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup, rUnsorted);
168817 }
168818 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
168819 nOut = pFrom->nRow + pWLoop->nOut;
168820 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
168821 isOrdered = pFrom->isOrdered;
168822 if( isOrdered<0 ){
168823 revMask = 0;
168824 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
168825 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
168826 iLoop, pWLoop, &revMask);
168827 }else{
168828 revMask = pFrom->revLoop;
168829 }
168830 if( isOrdered>=0 && isOrdered<nOrderBy ){
168831 if( aSortCost[isOrdered]==0 ){
168832 aSortCost[isOrdered] = whereSortingCost(
168833 pWInfo, nRowEst, nOrderBy, isOrdered
168834 );
168835 }
168836 /* TUNING: Add a small extra penalty (3) to sorting as an
168837 ** extra encouragement to the query planner to select a plan
168838 ** where the rows emerge in the correct order without any sorting
168839 ** required. */
168840 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 3;
168841
168842 WHERETRACE(0x002,
168843 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
168844 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
168845 rUnsorted, rCost));
168846 }else{
168847 rCost = rUnsorted;
168848 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
168849 }
168850
168851 /* Check to see if pWLoop should be added to the set of
168852 ** mxChoice best-so-far paths.
168853 **
168854 ** First look for an existing path among best-so-far paths
168855 ** that covers the same set of loops and has the same isOrdered
168856 ** setting as the current path candidate.
168857 **
168858 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
168859 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
168860 ** of legal values for isOrdered, -1..64.
168861 */
168862 testcase( nTo==0 );
168863 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
168864 if( pTo->maskLoop==maskNew
168865 && ((pTo->isOrdered^isOrdered)&0x80)==0
168866 ){
168867 testcase( jj==nTo-1 );
168868 break;
168869 }
168870 }
168871 if( jj>=nTo ){
168872 /* None of the existing best-so-far paths match the candidate. */
168873 if( nTo>=mxChoice
168874 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
168875 ){
168876 /* The current candidate is no better than any of the mxChoice
168877 ** paths currently in the best-so-far buffer. So discard
168878 ** this candidate as not viable. */
168879#ifdef WHERETRACE_ENABLED /* 0x4 */
168880 if( sqlite3WhereTrace&0x4 ){
168881 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
168882 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
168883 isOrdered>=0 ? isOrdered+'0' : '?');
168884 }
168885#endif
168886 continue;
168887 }
168888 /* If we reach this points it means that the new candidate path
168889 ** needs to be added to the set of best-so-far paths. */
168890 if( nTo<mxChoice ){
168891 /* Increase the size of the aTo set by one */
168892 jj = nTo++;
168893 }else{
168894 /* New path replaces the prior worst to keep count below mxChoice */
168895 jj = mxI;
168896 }
168897 pTo = &aTo[jj];
168898#ifdef WHERETRACE_ENABLED /* 0x4 */
168899 if( sqlite3WhereTrace&0x4 ){
168900 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
168901 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
168902 isOrdered>=0 ? isOrdered+'0' : '?');
168903 }
168904#endif
168905 }else{
168906 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
168907 ** same set of loops and has the same isOrdered setting as the
168908 ** candidate path. Check to see if the candidate should replace
168909 ** pTo or if the candidate should be skipped.
168910 **
168911 ** The conditional is an expanded vector comparison equivalent to:
168912 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
168913 */
168914 if( pTo->rCost<rCost
168915 || (pTo->rCost==rCost
168916 && (pTo->nRow<nOut
168917 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
168918 )
168919 )
168920 ){
168921#ifdef WHERETRACE_ENABLED /* 0x4 */
168922 if( sqlite3WhereTrace&0x4 ){
168923 sqlite3DebugPrintf(
168924 "Skip %s cost=%-3d,%3d,%3d order=%c",
168925 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
168926 isOrdered>=0 ? isOrdered+'0' : '?');
168927 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
168928 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
168929 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
168930 }
168931#endif
168932 /* Discard the candidate path from further consideration */
168933 testcase( pTo->rCost==rCost );
168934 continue;
168935 }
168936 testcase( pTo->rCost==rCost+1 );
168937 /* Control reaches here if the candidate path is better than the
168938 ** pTo path. Replace pTo with the candidate. */
168939#ifdef WHERETRACE_ENABLED /* 0x4 */
168940 if( sqlite3WhereTrace&0x4 ){
168941 sqlite3DebugPrintf(
168942 "Update %s cost=%-3d,%3d,%3d order=%c",
168943 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
168944 isOrdered>=0 ? isOrdered+'0' : '?');
168945 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
168946 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
168947 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
168948 }
168949#endif
168950 }
168951 /* pWLoop is a winner. Add it to the set of best so far */
168952 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
168953 pTo->revLoop = revMask;
168954 pTo->nRow = nOut;
168955 pTo->rCost = rCost;
168956 pTo->rUnsorted = rUnsorted;
168957 pTo->isOrdered = isOrdered;
168958 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
168959 pTo->aLoop[iLoop] = pWLoop;
168960 if( nTo>=mxChoice ){
168961 mxI = 0;
168962 mxCost = aTo[0].rCost;
168963 mxUnsorted = aTo[0].nRow;
168964 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
168965 if( pTo->rCost>mxCost
168966 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
168967 ){
168968 mxCost = pTo->rCost;
168969 mxUnsorted = pTo->rUnsorted;
168970 mxI = jj;
168971 }
168972 }
168973 }
168974 }
168975 }
168976
168977#ifdef WHERETRACE_ENABLED /* >=2 */
168978 if( sqlite3WhereTrace & 0x02 ){
168979 LogEst rMin, rFloor = 0;
168980 int nDone = 0;
168981 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
168982 while( nDone<nTo ){
168983 rMin = 0x7fff;
168984 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
168985 if( pTo->rCost>rFloor && pTo->rCost<rMin ) rMin = pTo->rCost;
168986 }
168987 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
168988 if( pTo->rCost==rMin ){
168989 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
168990 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
168991 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
168992 if( pTo->isOrdered>0 ){
168993 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
168994 }else{
168995 sqlite3DebugPrintf("\n");
168996 }
168997 nDone++;
168998 }
168999 }
169000 rFloor = rMin;
169001 }
169002 }
169003#endif
169004
169005 /* Swap the roles of aFrom and aTo for the next generation */
169006 pFrom = aTo;
169007 aTo = aFrom;
169008 aFrom = pFrom;
169009 nFrom = nTo;
169010 }
169011
169012 if( nFrom==0 ){
169013 sqlite3ErrorMsg(pParse, "no query solution");
169014 sqlite3StackFreeNN(pParse->db, pSpace)sqlite3DbFreeNN(pParse->db,pSpace);
169015 return SQLITE_ERROR1;
169016 }
169017
169018 /* Find the lowest cost path. pFrom will be left pointing to that path */
169019 pFrom = aFrom;
169020 for(ii=1; ii<nFrom; ii++){
169021 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
169022 }
169023 assert( pWInfo->nLevel==nLoop )((void) (0));
169024 /* Load the lowest cost path into pWInfo */
169025 for(iLoop=0; iLoop<nLoop; iLoop++){
169026 WhereLevel *pLevel = pWInfo->a + iLoop;
169027 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
169028 pLevel->iFrom = pWLoop->iTab;
169029 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
169030 }
169031 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
169032 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080)==0
169033 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP0
169034 && nRowEst
169035 ){
169036 Bitmask notUsed;
169037 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
169038 WHERE_DISTINCTBY0x0080, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
169039 if( rc==pWInfo->pResultSet->nExpr ){
169040 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
169041 }
169042 }
169043 pWInfo->bOrderedInnerLoop = 0;
169044 if( pWInfo->pOrderBy ){
169045 pWInfo->nOBSat = pFrom->isOrdered;
169046 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080 ){
169047 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
169048 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
169049 }
169050 /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */
169051 assert( pWInfo->pSelect->pOrderBy==0((void) (0))
169052 || pWInfo->nOBSat <= pWInfo->pSelect->pOrderBy->nExpr )((void) (0));
169053 }else{
169054 pWInfo->revMask = pFrom->revLoop;
169055 if( pWInfo->nOBSat<=0 ){
169056 pWInfo->nOBSat = 0;
169057 if( nLoop>0 ){
169058 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
169059 if( (wsFlags & WHERE_ONEROW0x00001000)==0
169060 && (wsFlags&(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004))!=(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004)
169061 ){
169062 Bitmask m = 0;
169063 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
169064 WHERE_ORDERBY_LIMIT0x0800, nLoop-1, pFrom->aLoop[nLoop-1], &m);
169065 testcase( wsFlags & WHERE_IPK );
169066 testcase( wsFlags & WHERE_COLUMN_IN );
169067 if( rc==pWInfo->pOrderBy->nExpr ){
169068 pWInfo->bOrderedInnerLoop = 1;
169069 pWInfo->revMask = m;
169070 }
169071 }
169072 }
169073 }else if( nLoop
169074 && pWInfo->nOBSat==1
169075 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002))!=0
169076 ){
169077 pWInfo->bOrderedInnerLoop = 1;
169078 }
169079 }
169080 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)
169081 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
169082 ){
169083 Bitmask revMask = 0;
169084 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
169085 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
169086 );
169087 assert( pWInfo->sorted==0 )((void) (0));
169088 if( nOrder==pWInfo->pOrderBy->nExpr ){
169089 pWInfo->sorted = 1;
169090 pWInfo->revMask = revMask;
169091 }
169092 }
169093 }
169094
169095 pWInfo->nRowOut = pFrom->nRow + pWInfo->nOutStarDelta;
169096
169097 /* Free temporary memory and return success */
169098 sqlite3StackFreeNN(pParse->db, pSpace)sqlite3DbFreeNN(pParse->db,pSpace);
169099 return SQLITE_OK0;
169100}
169101
169102/*
169103** This routine implements a heuristic designed to improve query planning.
169104** This routine is called in between the first and second call to
169105** wherePathSolver(). Hence the name "Interstage" "Heuristic".
169106**
169107** The first call to wherePathSolver() (hereafter just "solver()") computes
169108** the best path without regard to the order of the outputs. The second call
169109** to the solver() builds upon the first call to try to find an alternative
169110** path that satisfies the ORDER BY clause.
169111**
169112** This routine looks at the results of the first solver() run, and for
169113** every FROM clause term in the resulting query plan that uses an equality
169114** constraint against an index, disable other WhereLoops for that same
169115** FROM clause term that would try to do a full-table scan. This prevents
169116** an index search from being converted into a full-table scan in order to
169117** satisfy an ORDER BY clause, since even though we might get slightly better
169118** performance using the full-scan without sorting if the output size
169119** estimates are very precise, we might also get severe performance
169120** degradation using the full-scan if the output size estimate is too large.
169121** It is better to err on the side of caution.
169122**
169123** Except, if the first solver() call generated a full-table scan in an outer
169124** loop then stop this analysis at the first full-scan, since the second
169125** solver() run might try to swap that full-scan for another in order to
169126** get the output into the correct order. In other words, we allow a
169127** rewrite like this:
169128**
169129** First Solver() Second Solver()
169130** |-- SCAN t1 |-- SCAN t2
169131** |-- SEARCH t2 `-- SEARCH t1
169132** `-- SORT USING B-TREE
169133**
169134** The purpose of this routine is to disallow rewrites such as:
169135**
169136** First Solver() Second Solver()
169137** |-- SEARCH t1 |-- SCAN t2 <--- bad!
169138** |-- SEARCH t2 `-- SEARCH t1
169139** `-- SORT USING B-TREE
169140**
169141** See test cases in test/whereN.test for the real-world query that
169142** originally provoked this heuristic.
169143*/
169144static SQLITE_NOINLINE__attribute__((noinline)) void whereInterstageHeuristic(WhereInfo *pWInfo){
169145 int i;
169146#ifdef WHERETRACE_ENABLED
169147 int once = 0;
169148#endif
169149 for(i=0; i<pWInfo->nLevel; i++){
169150 WhereLoop *p = pWInfo->a[i].pWLoop;
169151 if( p==0 ) break;
169152 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ) continue;
169153 if( (p->wsFlags & (WHERE_COLUMN_EQ0x00000001|WHERE_COLUMN_NULL0x00000008|WHERE_COLUMN_IN0x00000004))!=0 ){
169154 u8 iTab = p->iTab;
169155 WhereLoop *pLoop;
169156 for(pLoop=pWInfo->pLoops; pLoop; pLoop=pLoop->pNextLoop){
169157 if( pLoop->iTab!=iTab ) continue;
169158 if( (pLoop->wsFlags & (WHERE_CONSTRAINT0x0000000f|WHERE_AUTO_INDEX0x00004000))!=0 ){
169159 /* Auto-index and index-constrained loops allowed to remain */
169160 continue;
169161 }
169162#ifdef WHERETRACE_ENABLED
169163 if( sqlite3WhereTrace & 0x80 ){
169164 if( once==0 ){
169165 sqlite3DebugPrintf("Loops disabled by interstage heuristic:\n");
169166 once = 1;
169167 }
169168 sqlite3WhereLoopPrint(pLoop, &pWInfo->sWC);
169169 }
169170#endif /* WHERETRACE_ENABLED */
169171 pLoop->prereq = ALLBITS((Bitmask)-1); /* Prevent 2nd solver() from using this one */
169172 }
169173 }else{
169174 break;
169175 }
169176 }
169177}
169178
169179/*
169180** Most queries use only a single table (they are not joins) and have
169181** simple == constraints against indexed fields. This routine attempts
169182** to plan those simple cases using much less ceremony than the
169183** general-purpose query planner, and thereby yield faster sqlite3_prepare()
169184** times for the common case.
169185**
169186** Return non-zero on success, if this query can be handled by this
169187** no-frills query planner. Return zero if this query needs the
169188** general-purpose query planner.
169189*/
169190static int whereShortCut(WhereLoopBuilder *pBuilder){
169191 WhereInfo *pWInfo;
169192 SrcItem *pItem;
169193 WhereClause *pWC;
169194 WhereTerm *pTerm;
169195 WhereLoop *pLoop;
169196 int iCur;
169197 int j;
169198 Table *pTab;
169199 Index *pIdx;
169200 WhereScan scan;
169201
169202 pWInfo = pBuilder->pWInfo;
169203 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020 ) return 0;
169204 assert( pWInfo->pTabList->nSrc>=1 )((void) (0));
169205 pItem = pWInfo->pTabList->a;
169206 pTab = pItem->pSTab;
169207 if( IsVirtual(pTab)((pTab)->eTabType==1) ) return 0;
169208 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
169209 testcase( pItem->fg.isIndexedBy );
169210 testcase( pItem->fg.notIndexed );
169211 return 0;
169212 }
169213 iCur = pItem->iCursor;
169214 pWC = &pWInfo->sWC;
169215 pLoop = pBuilder->pNew;
169216 pLoop->wsFlags = 0;
169217 pLoop->nSkip = 0;
169218 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ0x0002|WO_IS0x0080, 0);
169219 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
169220 if( pTerm ){
169221 testcase( pTerm->eOperator & WO_IS );
169222 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_IPK0x00000100|WHERE_ONEROW0x00001000;
169223 pLoop->aLTerm[0] = pTerm;
169224 pLoop->nLTerm = 1;
169225 pLoop->u.btree.nEq = 1;
169226 /* TUNING: Cost of a rowid lookup is 10 */
169227 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
169228 }else{
169229 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
169230 int opMask;
169231 assert( pLoop->aLTermSpace==pLoop->aLTerm )((void) (0));
169232 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0)
169233 || pIdx->pPartIdxWhere!=0
169234 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)((int)(sizeof(pLoop->aLTermSpace)/sizeof(pLoop->aLTermSpace
[0])))
169235 ) continue;
169236 opMask = pIdx->uniqNotNull ? (WO_EQ0x0002|WO_IS0x0080) : WO_EQ0x0002;
169237 for(j=0; j<pIdx->nKeyCol; j++){
169238 pTerm = whereScanInit(&scan, pWC, iCur, j, opMask, pIdx);
169239 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
169240 if( pTerm==0 ) break;
169241 testcase( pTerm->eOperator & WO_IS );
169242 pLoop->aLTerm[j] = pTerm;
169243 }
169244 if( j!=pIdx->nKeyCol ) continue;
169245 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_ONEROW0x00001000|WHERE_INDEXED0x00000200;
169246 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
169247 pLoop->wsFlags |= WHERE_IDX_ONLY0x00000040;
169248 }
169249 pLoop->nLTerm = j;
169250 pLoop->u.btree.nEq = j;
169251 pLoop->u.btree.pIndex = pIdx;
169252 /* TUNING: Cost of a unique index lookup is 15 */
169253 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
169254 break;
169255 }
169256 }
169257 if( pLoop->wsFlags ){
169258 pLoop->nOut = (LogEst)1;
169259 pWInfo->a[0].pWLoop = pLoop;
169260 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] )((void) (0));
169261 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
169262 pWInfo->a[0].iTabCur = iCur;
169263 pWInfo->nRowOut = 1;
169264 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
169265 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
169266 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
169267 }
169268 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS0x00200000;
169269#ifdef SQLITE_DEBUG
169270 pLoop->cId = '0';
169271#endif
169272#ifdef WHERETRACE_ENABLED
169273 if( sqlite3WhereTrace & 0x02 ){
169274 sqlite3DebugPrintf("whereShortCut() used to compute solution\n");
169275 }
169276#endif
169277 return 1;
169278 }
169279 return 0;
169280}
169281
169282/*
169283** Helper function for exprIsDeterministic().
169284*/
169285static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
169286 if( pExpr->op==TK_FUNCTION172 && ExprHasProperty(pExpr, EP_ConstFunc)(((pExpr)->flags&(0x100000))!=0)==0 ){
169287 pWalker->eCode = 0;
169288 return WRC_Abort2;
169289 }
169290 return WRC_Continue0;
169291}
169292
169293/*
169294** Return true if the expression contains no non-deterministic SQL
169295** functions. Do not consider non-deterministic SQL functions that are
169296** part of sub-select statements.
169297*/
169298static int exprIsDeterministic(Expr *p){
169299 Walker w;
169300 memset(&w, 0, sizeof(w));
169301 w.eCode = 1;
169302 w.xExprCallback = exprNodeIsDeterministic;
169303 w.xSelectCallback = sqlite3SelectWalkFail;
169304 sqlite3WalkExpr(&w, p);
169305 return w.eCode;
169306}
169307
169308
169309#ifdef WHERETRACE_ENABLED
169310/*
169311** Display all WhereLoops in pWInfo
169312*/
169313static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
169314 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
169315 WhereLoop *p;
169316 int i;
169317 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
169318 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
169319 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
169320 p->cId = zLabel[i%(sizeof(zLabel)-1)];
169321 sqlite3WhereLoopPrint(p, pWC);
169322 }
169323 }
169324}
169325# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C)
169326#else
169327# define WHERETRACE_ALL_LOOPS(W,C)
169328#endif
169329
169330/* Attempt to omit tables from a join that do not affect the result.
169331** For a table to not affect the result, the following must be true:
169332**
169333** 1) The query must not be an aggregate.
169334** 2) The table must be the RHS of a LEFT JOIN.
169335** 3) Either the query must be DISTINCT, or else the ON or USING clause
169336** must contain a constraint that limits the scan of the table to
169337** at most a single row.
169338** 4) The table must not be referenced by any part of the query apart
169339** from its own USING or ON clause.
169340** 5) The table must not have an inner-join ON or USING clause if there is
169341** a RIGHT JOIN anywhere in the query. Otherwise the ON/USING clause
169342** might move from the right side to the left side of the RIGHT JOIN.
169343** Note: Due to (2), this condition can only arise if the table is
169344** the right-most table of a subquery that was flattened into the
169345** main query and that subquery was the right-hand operand of an
169346** inner join that held an ON or USING clause.
169347** 6) The ORDER BY clause has 63 or fewer terms
169348** 7) The omit-noop-join optimization is enabled.
169349**
169350** Items (1), (6), and (7) are checked by the caller.
169351**
169352** For example, given:
169353**
169354** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
169355** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
169356** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
169357**
169358** then table t2 can be omitted from the following:
169359**
169360** SELECT v1, v3 FROM t1
169361** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
169362** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
169363**
169364** or from:
169365**
169366** SELECT DISTINCT v1, v3 FROM t1
169367** LEFT JOIN t2
169368** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
169369*/
169370static SQLITE_NOINLINE__attribute__((noinline)) Bitmask whereOmitNoopJoin(
169371 WhereInfo *pWInfo,
169372 Bitmask notReady
169373){
169374 int i;
169375 Bitmask tabUsed;
169376 int hasRightJoin;
169377
169378 /* Preconditions checked by the caller */
169379 assert( pWInfo->nLevel>=2 )((void) (0));
169380 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) )((void) (0));
169381
169382 /* These two preconditions checked by the caller combine to guarantee
169383 ** condition (1) of the header comment */
169384 assert( pWInfo->pResultSet!=0 )((void) (0));
169385 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) )((void) (0));
169386
169387 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
169388 if( pWInfo->pOrderBy ){
169389 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
169390 }
169391 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ0x40)!=0;
169392 for(i=pWInfo->nLevel-1; i>=1; i--){
169393 WhereTerm *pTerm, *pEnd;
169394 SrcItem *pItem;
169395 WhereLoop *pLoop;
169396 Bitmask m1;
169397 pLoop = pWInfo->a[i].pWLoop;
169398 pItem = &pWInfo->pTabList->a[pLoop->iTab];
169399 if( (pItem->fg.jointype & (JT_LEFT0x08|JT_RIGHT0x10))!=JT_LEFT0x08 ) continue;
169400 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)==0
169401 && (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0
169402 ){
169403 continue;
169404 }
169405 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
169406 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
169407 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
169408 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
169409 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)(((pTerm->pExpr)->flags&(0x000001))!=0)
169410 || pTerm->pExpr->w.iJoin!=pItem->iCursor
169411 ){
169412 break;
169413 }
169414 }
169415 if( hasRightJoin
169416 && ExprHasProperty(pTerm->pExpr, EP_InnerON)(((pTerm->pExpr)->flags&(0x000002))!=0)
169417 && pTerm->pExpr->w.iJoin==pItem->iCursor
169418 ){
169419 break; /* restriction (5) */
169420 }
169421 }
169422 if( pTerm<pEnd ) continue;
169423 WHERETRACE(0xffffffff,("-> omit unused FROM-clause term %c\n",pLoop->cId));
169424 m1 = MASKBIT(i)(((Bitmask)1)<<(i))-1;
169425 testcase( ((pWInfo->revMask>>1) & ~m1)!=0 );
169426 pWInfo->revMask = (m1 & pWInfo->revMask) | ((pWInfo->revMask>>1) & ~m1);
169427 notReady &= ~pLoop->maskSelf;
169428 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
169429 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
169430 pTerm->wtFlags |= TERM_CODED0x0004;
169431 }
169432 }
169433 if( i!=pWInfo->nLevel-1 ){
169434 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
169435 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
169436 }
169437 pWInfo->nLevel--;
169438 assert( pWInfo->nLevel>0 )((void) (0));
169439 }
169440 return notReady;
169441}
169442
169443/*
169444** Check to see if there are any SEARCH loops that might benefit from
169445** using a Bloom filter. Consider a Bloom filter if:
169446**
169447** (1) The SEARCH happens more than N times where N is the number
169448** of rows in the table that is being considered for the Bloom
169449** filter.
169450** (2) Some searches are expected to find zero rows. (This is determined
169451** by the WHERE_SELFCULL flag on the term.)
169452** (3) Bloom-filter processing is not disabled. (Checked by the
169453** caller.)
169454** (4) The size of the table being searched is known by ANALYZE.
169455**
169456** This block of code merely checks to see if a Bloom filter would be
169457** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the
169458** WhereLoop. The implementation of the Bloom filter comes further
169459** down where the code for each WhereLoop is generated.
169460*/
169461static SQLITE_NOINLINE__attribute__((noinline)) void whereCheckIfBloomFilterIsUseful(
169462 const WhereInfo *pWInfo
169463){
169464 int i;
169465 LogEst nSearch = 0;
169466
169467 assert( pWInfo->nLevel>=2 )((void) (0));
169468 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) )((void) (0));
169469 for(i=0; i<pWInfo->nLevel; i++){
169470 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
169471 const unsigned int reqFlags = (WHERE_SELFCULL0x00800000|WHERE_COLUMN_EQ0x00000001);
169472 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
169473 Table *pTab = pItem->pSTab;
169474 if( (pTab->tabFlags & TF_HasStat10x00000010)==0 ) break;
169475 pTab->tabFlags |= TF_MaybeReanalyze0x00000100;
169476 if( i>=1
169477 && (pLoop->wsFlags & reqFlags)==reqFlags
169478 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
169479 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)((pLoop->wsFlags & (0x00000100|0x00000200))!=0)
169480 ){
169481 if( nSearch > pTab->nRowLogEst ){
169482 testcase( pItem->fg.jointype & JT_LEFT );
169483 pLoop->wsFlags |= WHERE_BLOOMFILTER0x00400000;
169484 pLoop->wsFlags &= ~WHERE_IDX_ONLY0x00000040;
169485 WHERETRACE(0xffffffff, (
169486 "-> use Bloom-filter on loop %c because there are ~%.1e "
169487 "lookups into %s which has only ~%.1e rows\n",
169488 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
169489 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
169490 }
169491 }
169492 nSearch += pLoop->nOut;
169493 if( pWInfo->nOutStarDelta ) nSearch += pLoop->rStarDelta;
169494 }
169495}
169496
169497/*
169498** The index pIdx is used by a query and contains one or more expressions.
169499** In other words pIdx is an index on an expression. iIdxCur is the cursor
169500** number for the index and iDataCur is the cursor number for the corresponding
169501** table.
169502**
169503** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
169504** each of the expressions in the index so that the expression code generator
169505** will know to replace occurrences of the indexed expression with
169506** references to the corresponding column of the index.
169507*/
169508static SQLITE_NOINLINE__attribute__((noinline)) void whereAddIndexedExpr(
169509 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
169510 Index *pIdx, /* The index-on-expression that contains the expressions */
169511 int iIdxCur, /* Cursor number for pIdx */
169512 SrcItem *pTabItem /* The FROM clause entry for the table */
169513){
169514 int i;
169515 IndexedExpr *p;
169516 Table *pTab;
169517 assert( pIdx->bHasExpr )((void) (0));
169518 pTab = pIdx->pTable;
169519 for(i=0; i<pIdx->nColumn; i++){
169520 Expr *pExpr;
169521 int j = pIdx->aiColumn[i];
169522 if( j==XN_EXPR(-2) ){
169523 pExpr = pIdx->aColExpr->a[i].pExpr;
169524 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL0x0020)!=0 ){
169525 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
169526 }else{
169527 continue;
169528 }
169529 if( sqlite3ExprIsConstant(0,pExpr) ) continue;
169530 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
169531 if( p==0 ) break;
169532 p->pIENext = pParse->pIdxEpr;
169533#ifdef WHERETRACE_ENABLED
169534 if( sqlite3WhereTrace & 0x200 ){
169535 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
169536 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(pExpr);
169537 }
169538#endif
169539 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
169540 p->iDataCur = pTabItem->iCursor;
169541 p->iIdxCur = iIdxCur;
169542 p->iIdxCol = i;
169543 p->bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT0x08|JT_LTORJ0x40|JT_RIGHT0x10))!=0;
169544 if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){
169545 p->aff = pIdx->zColAff[i];
169546 }
169547#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
169548 p->zIdxName = pIdx->zName;
169549#endif
169550 pParse->pIdxEpr = p;
169551 if( p->pIENext==0 ){
169552 void *pArg = (void*)&pParse->pIdxEpr;
169553 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pArg);
169554 }
169555 }
169556}
169557
169558/*
169559** Set the reverse-scan order mask to one for all tables in the query
169560** with the exception of MATERIALIZED common table expressions that have
169561** their own internal ORDER BY clauses.
169562**
169563** This implements the PRAGMA reverse_unordered_selects=ON setting.
169564** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER).
169565*/
169566static SQLITE_NOINLINE__attribute__((noinline)) void whereReverseScanOrder(WhereInfo *pWInfo){
169567 int ii;
169568 for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
169569 SrcItem *pItem = &pWInfo->pTabList->a[ii];
169570 if( !pItem->fg.isCte
169571 || pItem->u2.pCteUse->eM10d!=M10d_Yes0
169572 || NEVER(pItem->fg.isSubquery==0)(pItem->fg.isSubquery==0)
169573 || pItem->u4.pSubq->pSelect->pOrderBy==0
169574 ){
169575 pWInfo->revMask |= MASKBIT(ii)(((Bitmask)1)<<(ii));
169576 }
169577 }
169578}
169579
169580/*
169581** Generate the beginning of the loop used for WHERE clause processing.
169582** The return value is a pointer to an opaque structure that contains
169583** information needed to terminate the loop. Later, the calling routine
169584** should invoke sqlite3WhereEnd() with the return value of this function
169585** in order to complete the WHERE clause processing.
169586**
169587** If an error occurs, this routine returns NULL.
169588**
169589** The basic idea is to do a nested loop, one loop for each table in
169590** the FROM clause of a select. (INSERT and UPDATE statements are the
169591** same as a SELECT with only a single table in the FROM clause.) For
169592** example, if the SQL is this:
169593**
169594** SELECT * FROM t1, t2, t3 WHERE ...;
169595**
169596** Then the code generated is conceptually like the following:
169597**
169598** foreach row1 in t1 do \ Code generated
169599** foreach row2 in t2 do |-- by sqlite3WhereBegin()
169600** foreach row3 in t3 do /
169601** ...
169602** end \ Code generated
169603** end |-- by sqlite3WhereEnd()
169604** end /
169605**
169606** Note that the loops might not be nested in the order in which they
169607** appear in the FROM clause if a different order is better able to make
169608** use of indices. Note also that when the IN operator appears in
169609** the WHERE clause, it might result in additional nested loops for
169610** scanning through all values on the right-hand side of the IN.
169611**
169612** There are Btree cursors associated with each table. t1 uses cursor
169613** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
169614** And so forth. This routine generates code to open those VDBE cursors
169615** and sqlite3WhereEnd() generates the code to close them.
169616**
169617** The code that sqlite3WhereBegin() generates leaves the cursors named
169618** in pTabList pointing at their appropriate entries. The [...] code
169619** can use OP_Column and OP_Rowid opcodes on these cursors to extract
169620** data from the various tables of the loop.
169621**
169622** If the WHERE clause is empty, the foreach loops must each scan their
169623** entire tables. Thus a three-way join is an O(N^3) operation. But if
169624** the tables have indices and there are terms in the WHERE clause that
169625** refer to those indices, a complete table scan can be avoided and the
169626** code will run much faster. Most of the work of this routine is checking
169627** to see if there are indices that can be used to speed up the loop.
169628**
169629** Terms of the WHERE clause are also used to limit which rows actually
169630** make it to the "..." in the middle of the loop. After each "foreach",
169631** terms of the WHERE clause that use only terms in that loop and outer
169632** loops are evaluated and if false a jump is made around all subsequent
169633** inner loops (or around the "..." if the test occurs within the inner-
169634** most loop)
169635**
169636** OUTER JOINS
169637**
169638** An outer join of tables t1 and t2 is conceptually coded as follows:
169639**
169640** foreach row1 in t1 do
169641** flag = 0
169642** foreach row2 in t2 do
169643** start:
169644** ...
169645** flag = 1
169646** end
169647** if flag==0 then
169648** move the row2 cursor to a null row
169649** goto start
169650** fi
169651** end
169652**
169653** ORDER BY CLAUSE PROCESSING
169654**
169655** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
169656** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
169657** if there is one. If there is no ORDER BY clause or if this routine
169658** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
169659**
169660** The iIdxCur parameter is the cursor number of an index. If
169661** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
169662** to use for OR clause processing. The WHERE clause should use this
169663** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
169664** the first cursor in an array of cursors for all indices. iIdxCur should
169665** be used to compute the appropriate cursor depending on which index is
169666** used.
169667*/
169668SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(
169669 Parse *pParse, /* The parser context */
169670 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
169671 Expr *pWhere, /* The WHERE clause */
169672 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
169673 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
169674 Select *pSelect, /* The entire SELECT statement */
169675 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
169676 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
169677 ** If WHERE_USE_LIMIT, then the limit amount */
169678){
169679 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
169680 int nTabList; /* Number of elements in pTabList */
169681 WhereInfo *pWInfo; /* Will become the return value of this function */
169682 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
169683 Bitmask notReady; /* Cursors that are not yet positioned */
169684 WhereLoopBuilder sWLB; /* The WhereLoop builder */
169685 WhereMaskSet *pMaskSet; /* The expression mask set */
169686 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
169687 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
169688 int ii; /* Loop counter */
169689 sqlite3 *db; /* Database connection */
169690 int rc; /* Return code */
169691 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
169692
169693 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (((void) (0))
169694 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0((void) (0))
169695 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
169696 ))((void) (0));
169697
169698 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
169699 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
169700 || (wctrlFlags & WHERE_USE_LIMIT)==0 )((void) (0));
169701
169702 /* Variable initialization */
169703 db = pParse->db;
169704 memset(&sWLB, 0, sizeof(sWLB));
169705
169706 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
169707 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
169708 if( pOrderBy && pOrderBy->nExpr>=BMS((int)(sizeof(Bitmask)*8)) ){
169709 pOrderBy = 0;
169710 wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
169711 wctrlFlags |= WHERE_KEEP_ALL_JOINS0x2000; /* Disable omit-noop-join opt */
169712 }
169713
169714 /* The number of tables in the FROM clause is limited by the number of
169715 ** bits in a Bitmask
169716 */
169717 testcase( pTabList->nSrc==BMS );
169718 if( pTabList->nSrc>BMS((int)(sizeof(Bitmask)*8)) ){
169719 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS((int)(sizeof(Bitmask)*8)));
169720 return 0;
169721 }
169722
169723 /* This function normally generates a nested loop for all tables in
169724 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
169725 ** only generate code for the first table in pTabList and assume that
169726 ** any cursors associated with subsequent tables are uninitialized.
169727 */
169728 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020) ? 1 : pTabList->nSrc;
169729
169730 /* Allocate and initialize the WhereInfo structure that will become the
169731 ** return value. A single allocation is used to store the WhereInfo
169732 ** struct, the contents of WhereInfo.a[], the WhereClause structure
169733 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
169734 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
169735 ** some architectures. Hence the ROUND8() below.
169736 */
169737 nByteWInfo = ROUND8P(sizeof(WhereInfo))(sizeof(WhereInfo));
169738 if( nTabList>1 ){
169739 nByteWInfo = ROUND8P(nByteWInfo + (nTabList-1)*sizeof(WhereLevel))(nByteWInfo + (nTabList-1)*sizeof(WhereLevel));
169740 }
169741 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
169742 if( db->mallocFailed ){
169743 sqlite3DbFree(db, pWInfo);
169744 pWInfo = 0;
169745 goto whereBeginError;
169746 }
169747 pWInfo->pParse = pParse;
169748 pWInfo->pTabList = pTabList;
169749 pWInfo->pOrderBy = pOrderBy;
169750#if WHERETRACE_ENABLED
169751 pWInfo->pWhere = pWhere;
169752#endif
169753 pWInfo->pResultSet = pResultSet;
169754 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
169755 pWInfo->nLevel = nTabList;
169756 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
169757 pWInfo->wctrlFlags = wctrlFlags;
169758 pWInfo->iLimit = iAuxArg;
169759 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
169760 pWInfo->pSelect = pSelect;
169761 memset(&pWInfo->nOBSat, 0,
169762 offsetof(WhereInfo,sWC)__builtin_offsetof(WhereInfo, sWC) - offsetof(WhereInfo,nOBSat)__builtin_offsetof(WhereInfo, nOBSat));
169763 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
169764 assert( pWInfo->eOnePass==ONEPASS_OFF )((void) (0)); /* ONEPASS defaults to OFF */
169765 pMaskSet = &pWInfo->sMaskSet;
169766 pMaskSet->n = 0;
169767 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
169768 ** a valid cursor number, to avoid an initial
169769 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
169770 sWLB.pWInfo = pWInfo;
169771 sWLB.pWC = &pWInfo->sWC;
169772 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
169773 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) )((void) (0));
169774 whereLoopInit(sWLB.pNew);
169775#ifdef SQLITE_DEBUG
169776 sWLB.pNew->cId = '*';
169777#endif
169778
169779 /* Split the WHERE clause into separate subexpressions where each
169780 ** subexpression is separated by an AND operator.
169781 */
169782 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
169783 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND44);
169784
169785 /* Special case: No FROM clause
169786 */
169787 if( nTabList==0 ){
169788 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
169789 if( (wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
169790 && OptimizationEnabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x00000010))==0)
169791 ){
169792 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
169793 }
169794 if( ALWAYS(pWInfo->pSelect)(pWInfo->pSelect)
169795 && (pWInfo->pSelect->selFlags & SF_MultiValue0x0000400)==0
169796 ){
169797 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"))sqlite3VdbeExplain (pParse, 0, "SCAN CONSTANT ROW");
169798 }
169799 }else{
169800 /* Assign a bit from the bitmask to every term in the FROM clause.
169801 **
169802 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
169803 **
169804 ** The rule of the previous sentence ensures that if X is the bitmask for
169805 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
169806 ** Knowing the bitmask for all tables to the left of a left join is
169807 ** important. Ticket #3015.
169808 **
169809 ** Note that bitmasks are created for all pTabList->nSrc tables in
169810 ** pTabList, not just the first nTabList tables. nTabList is normally
169811 ** equal to pTabList->nSrc but might be shortened to 1 if the
169812 ** WHERE_OR_SUBCLAUSE flag is set.
169813 */
169814 ii = 0;
169815 do{
169816 createMask(pMaskSet, pTabList->a[ii].iCursor);
169817 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
169818 }while( (++ii)<pTabList->nSrc );
169819 #ifdef SQLITE_DEBUG
169820 {
169821 Bitmask mx = 0;
169822 for(ii=0; ii<pTabList->nSrc; ii++){
169823 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
169824 assert( m>=mx )((void) (0));
169825 mx = m;
169826 }
169827 }
169828 #endif
169829 }
169830
169831 /* Analyze all of the subexpressions. */
169832 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
169833 if( pSelect && pSelect->pLimit ){
169834 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
169835 }
169836 if( pParse->nErr ) goto whereBeginError;
169837
169838 /* The False-WHERE-Term-Bypass optimization:
169839 **
169840 ** If there are WHERE terms that are false, then no rows will be output,
169841 ** so skip over all of the code generated here.
169842 **
169843 ** Conditions:
169844 **
169845 ** (1) The WHERE term must not refer to any tables in the join.
169846 ** (2) The term must not come from an ON clause on the
169847 ** right-hand side of a LEFT or FULL JOIN.
169848 ** (3) The term must not come from an ON clause, or there must be
169849 ** no RIGHT or FULL OUTER joins in pTabList.
169850 ** (4) If the expression contains non-deterministic functions
169851 ** that are not within a sub-select. This is not required
169852 ** for correctness but rather to preserves SQLite's legacy
169853 ** behaviour in the following two cases:
169854 **
169855 ** WHERE random()>0; -- eval random() once per row
169856 ** WHERE (SELECT random())>0; -- eval random() just once overall
169857 **
169858 ** Note that the Where term need not be a constant in order for this
169859 ** optimization to apply, though it does need to be constant relative to
169860 ** the current subquery (condition 1). The term might include variables
169861 ** from outer queries so that the value of the term changes from one
169862 ** invocation of the current subquery to the next.
169863 */
169864 for(ii=0; ii<sWLB.pWC->nBase; ii++){
169865 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
169866 Expr *pX; /* The expression of pT */
169867 if( pT->wtFlags & TERM_VIRTUAL0x0002 ) continue;
169868 pX = pT->pExpr;
169869 assert( pX!=0 )((void) (0));
169870 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) )((void) (0));
169871 if( pT->prereqAll==0 /* Conditions (1) and (2) */
169872 && (nTabList==0 || exprIsDeterministic(pX)) /* Condition (4) */
169873 && !(ExprHasProperty(pX, EP_InnerON)(((pX)->flags&(0x000002))!=0) /* Condition (3) */
169874 && (pTabList->a[0].fg.jointype & JT_LTORJ0x40)!=0 )
169875 ){
169876 sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL0x10);
169877 pT->wtFlags |= TERM_CODED0x0004;
169878 }
169879 }
169880
169881 if( wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
169882 if( OptimizationDisabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x00000010))!=0) ){
169883 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
169884 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
169885 wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
169886 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
169887 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
169888 /* The DISTINCT marking is pointless. Ignore it. */
169889 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
169890 }else if( pOrderBy==0 ){
169891 /* Try to ORDER BY the result set to make distinct processing easier */
169892 pWInfo->wctrlFlags |= WHERE_DISTINCTBY0x0080;
169893 pWInfo->pOrderBy = pResultSet;
169894 }
169895 }
169896
169897 /* Construct the WhereLoop objects */
169898#if defined(WHERETRACE_ENABLED)
169899 if( sqlite3WhereTrace & 0xffffffff ){
169900 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
169901 if( wctrlFlags & WHERE_USE_LIMIT0x4000 ){
169902 sqlite3DebugPrintf(", limit: %d", iAuxArg);
169903 }
169904 sqlite3DebugPrintf(")\n");
169905 if( sqlite3WhereTrace & 0x8000 ){
169906 Select sSelect;
169907 memset(&sSelect, 0, sizeof(sSelect));
169908 sSelect.selFlags = SF_WhereBegin0x0080000;
169909 sSelect.pSrc = pTabList;
169910 sSelect.pWhere = pWhere;
169911 sSelect.pOrderBy = pOrderBy;
169912 sSelect.pEList = pResultSet;
169913 sqlite3TreeViewSelect(0, &sSelect, 0);
169914 }
169915 if( sqlite3WhereTrace & 0x4000 ){ /* Display all WHERE clause terms */
169916 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
169917 sqlite3WhereClausePrint(sWLB.pWC);
169918 }
169919 }
169920#endif
169921
169922 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
169923 rc = whereLoopAddAll(&sWLB);
169924 if( rc ) goto whereBeginError;
169925
169926#ifdef SQLITE_ENABLE_STAT4
169927 /* If one or more WhereTerm.truthProb values were used in estimating
169928 ** loop parameters, but then those truthProb values were subsequently
169929 ** changed based on STAT4 information while computing subsequent loops,
169930 ** then we need to rerun the whole loop building process so that all
169931 ** loops will be built using the revised truthProb values. */
169932 if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS0x0004 ){
169933 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
169934 WHERETRACE(0xffffffff,
169935 ("**** Redo all loop computations due to"
169936 " TERM_HIGHTRUTH changes ****\n"));
169937 while( pWInfo->pLoops ){
169938 WhereLoop *p = pWInfo->pLoops;
169939 pWInfo->pLoops = p->pNextLoop;
169940 whereLoopDelete(db, p);
169941 }
169942 rc = whereLoopAddAll(&sWLB);
169943 if( rc ) goto whereBeginError;
169944 }
169945#endif
169946 WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC);
169947
169948 wherePathSolver(pWInfo, 0);
169949 if( db->mallocFailed ) goto whereBeginError;
169950 if( pWInfo->pOrderBy ){
169951 whereInterstageHeuristic(pWInfo);
169952 wherePathSolver(pWInfo, pWInfo->nRowOut<0 ? 1 : pWInfo->nRowOut+1);
169953 if( db->mallocFailed ) goto whereBeginError;
169954 }
169955
169956 /* TUNING: Assume that a DISTINCT clause on a subquery reduces
169957 ** the output size by a factor of 8 (LogEst -30).
169958 */
169959 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0 ){
169960 WHERETRACE(0x0080,("nRowOut reduced from %d to %d due to DISTINCT\n",
169961 pWInfo->nRowOut, pWInfo->nRowOut-30));
169962 pWInfo->nRowOut -= 30;
169963 }
169964
169965 }
169966 assert( pWInfo->pTabList!=0 )((void) (0));
169967 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder0x00001000)!=0 ){
169968 whereReverseScanOrder(pWInfo);
169969 }
169970 if( pParse->nErr ){
169971 goto whereBeginError;
169972 }
169973 assert( db->mallocFailed==0 )((void) (0));
169974#ifdef WHERETRACE_ENABLED
169975 if( sqlite3WhereTrace ){
169976 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
169977 if( pWInfo->nOBSat>0 ){
169978 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
169979 }
169980 switch( pWInfo->eDistinct ){
169981 case WHERE_DISTINCT_UNIQUE1: {
169982 sqlite3DebugPrintf(" DISTINCT=unique");
169983 break;
169984 }
169985 case WHERE_DISTINCT_ORDERED2: {
169986 sqlite3DebugPrintf(" DISTINCT=ordered");
169987 break;
169988 }
169989 case WHERE_DISTINCT_UNORDERED3: {
169990 sqlite3DebugPrintf(" DISTINCT=unordered");
169991 break;
169992 }
169993 }
169994 sqlite3DebugPrintf("\n");
169995 for(ii=0; ii<pWInfo->nLevel; ii++){
169996 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
169997 }
169998 }
169999#endif
170000
170001 /* Attempt to omit tables from a join that do not affect the result.
170002 ** See the comment on whereOmitNoopJoin() for further information.
170003 **
170004 ** This query optimization is factored out into a separate "no-inline"
170005 ** procedure to keep the sqlite3WhereBegin() procedure from becoming
170006 ** too large. If sqlite3WhereBegin() becomes too large, that prevents
170007 ** some C-compiler optimizers from in-lining the
170008 ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to
170009 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
170010 */
170011 notReady = ~(Bitmask)0;
170012 if( pWInfo->nLevel>=2 /* Must be a join, or this opt8n is pointless */
170013 && pResultSet!=0 /* Condition (1) */
170014 && 0==(wctrlFlags & (WHERE_AGG_DISTINCT0x0400|WHERE_KEEP_ALL_JOINS0x2000)) /* (1),(6) */
170015 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)(((db)->dbOptFlags&(0x00000100))==0) /* (7) */
170016 ){
170017 notReady = whereOmitNoopJoin(pWInfo, notReady);
170018 nTabList = pWInfo->nLevel;
170019 assert( nTabList>0 )((void) (0));
170020 }
170021
170022 /* Check to see if there are any SEARCH loops that might benefit from
170023 ** using a Bloom filter.
170024 */
170025 if( pWInfo->nLevel>=2
170026 && OptimizationEnabled(db, SQLITE_BloomFilter)(((db)->dbOptFlags&(0x00080000))==0)
170027 ){
170028 whereCheckIfBloomFilterIsUseful(pWInfo);
170029 }
170030
170031#if defined(WHERETRACE_ENABLED)
170032 if( sqlite3WhereTrace & 0x4000 ){ /* Display all terms of the WHERE clause */
170033 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
170034 sqlite3WhereClausePrint(sWLB.pWC);
170035 }
170036 WHERETRACE(0xffffffff,("*** Optimizer Finished ***\n"));
170037#endif
170038 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
170039
170040 /* If the caller is an UPDATE or DELETE statement that is requesting
170041 ** to use a one-pass algorithm, determine if this is appropriate.
170042 **
170043 ** A one-pass approach can be used if the caller has requested one
170044 ** and either (a) the scan visits at most one row or (b) each
170045 ** of the following are true:
170046 **
170047 ** * the caller has indicated that a one-pass approach can be used
170048 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
170049 ** * the table is not a virtual table, and
170050 ** * either the scan does not use the OR optimization or the caller
170051 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
170052 ** for DELETE).
170053 **
170054 ** The last qualification is because an UPDATE statement uses
170055 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
170056 ** use a one-pass approach, and this is not set accurately for scans
170057 ** that use the OR optimization.
170058 */
170059 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 )((void) (0));
170060 if( (wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)!=0 ){
170061 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
170062 int bOnerow = (wsFlags & WHERE_ONEROW0x00001000)!=0;
170063 assert( !(wsFlags&WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pSTab) )((void) (0));
170064 if( bOnerow || (
170065 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008)
170066 && !IsVirtual(pTabList->a[0].pSTab)((pTabList->a[0].pSTab)->eTabType==1)
170067 && (0==(wsFlags & WHERE_MULTI_OR0x00002000) || (wctrlFlags & WHERE_DUPLICATES_OK0x0010))
170068 && OptimizationEnabled(db, SQLITE_OnePass)(((db)->dbOptFlags&(0x08000000))==0)
170069 )){
170070 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE1 : ONEPASS_MULTI2;
170071 if( HasRowid(pTabList->a[0].pSTab)(((pTabList->a[0].pSTab)->tabFlags & 0x00000080)==0
)
&& (wsFlags & WHERE_IDX_ONLY0x00000040) ){
170072 if( wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008 ){
170073 bFordelete = OPFLAG_FORDELETE0x08;
170074 }
170075 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY0x00000040);
170076 }
170077 }
170078 }
170079
170080 /* Open all tables in the pTabList and any indices selected for
170081 ** searching those tables.
170082 */
170083 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
170084 Table *pTab; /* Table to open */
170085 int iDb; /* Index of database containing table/index */
170086 SrcItem *pTabItem;
170087
170088 pTabItem = &pTabList->a[pLevel->iFrom];
170089 pTab = pTabItem->pSTab;
170090 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
170091 pLoop = pLevel->pWLoop;
170092 if( (pTab->tabFlags & TF_Ephemeral0x00004000)!=0 || IsView(pTab)((pTab)->eTabType==2) ){
170093 /* Do nothing */
170094 }else
170095#ifndef SQLITE_OMIT_VIRTUALTABLE
170096 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
170097 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
170098 int iCur = pTabItem->iCursor;
170099 sqlite3VdbeAddOp4(v, OP_VOpen173, iCur, 0, 0, pVTab, P4_VTAB(-11));
170100 }else if( IsVirtual(pTab)((pTab)->eTabType==1) ){
170101 /* noop */
170102 }else
170103#endif
170104 if( ((pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)==0
170105 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0)
170106 || (pTabItem->fg.jointype & (JT_LTORJ0x40|JT_RIGHT0x10))!=0
170107 ){
170108 int op = OP_OpenRead102;
170109 if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
170110 op = OP_OpenWrite113;
170111 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
170112 };
170113 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
170114 assert( pTabItem->iCursor==pLevel->iTabCur )((void) (0));
170115 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
170116 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
170117 if( pWInfo->eOnePass==ONEPASS_OFF0
170118 && pTab->nCol<BMS((int)(sizeof(Bitmask)*8))
170119 && (pTab->tabFlags & (TF_HasGenerated0x00000060|TF_WithoutRowid0x00000080))==0
170120 && (pLoop->wsFlags & (WHERE_AUTO_INDEX0x00004000|WHERE_BLOOMFILTER0x00400000))==0
170121 ){
170122 /* If we know that only a prefix of the record will be used,
170123 ** it is advantageous to reduce the "column count" field in
170124 ** the P4 operand of the OP_OpenRead/Write opcode. */
170125 Bitmask b = pTabItem->colUsed;
170126 int n = 0;
170127 for(; b; b=b>>1, n++){}
170128 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n)((void*)(long int)(n)), P4_INT32(-3));
170129 assert( n<=pTab->nCol )((void) (0));
170130 }
170131#ifdef SQLITE_ENABLE_CURSOR_HINTS
170132 if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid0x00000080)==0 ){
170133 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02|bFordelete);
170134 }else
170135#endif
170136 {
170137 sqlite3VdbeChangeP5(v, bFordelete);
170138 }
170139#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
170140 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123, pTabItem->iCursor, 0, 0,
170141 (const u8*)&pTabItem->colUsed, P4_INT64(-13));
170142#endif
170143 }else{
170144 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
170145 }
170146 if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
170147 Index *pIx = pLoop->u.btree.pIndex;
170148 int iIndexCur;
170149 int op = OP_OpenRead102;
170150 /* iAuxArg is always set to a positive value if ONEPASS is possible */
170151 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 )((void) (0));
170152 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && IsPrimaryKeyIndex(pIx)((pIx)->idxType==2)
170153 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0
170154 ){
170155 /* This is one term of an OR-optimization using the PRIMARY KEY of a
170156 ** WITHOUT ROWID table. No need for a separate index */
170157 iIndexCur = pLevel->iTabCur;
170158 op = 0;
170159 }else if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
170160 Index *pJ = pTabItem->pSTab->pIndex;
170161 iIndexCur = iAuxArg;
170162 assert( wctrlFlags & WHERE_ONEPASS_DESIRED )((void) (0));
170163 while( ALWAYS(pJ)(pJ) && pJ!=pIx ){
170164 iIndexCur++;
170165 pJ = pJ->pNext;
170166 }
170167 op = OP_OpenWrite113;
170168 pWInfo->aiCurOnePass[1] = iIndexCur;
170169 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0 ){
170170 iIndexCur = iAuxArg;
170171 op = OP_ReopenIdx101;
170172 }else{
170173 iIndexCur = pParse->nTab++;
170174 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr)(((db)->dbOptFlags&(0x01000000))==0) ){
170175 whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem);
170176 }
170177 if( pIx->pPartIdxWhere && (pTabItem->fg.jointype & JT_RIGHT0x10)==0 ){
170178 wherePartIdxExpr(
170179 pParse, pIx, pIx->pPartIdxWhere, 0, iIndexCur, pTabItem
170180 );
170181 }
170182 }
170183 pLevel->iIdxCur = iIndexCur;
170184 assert( pIx!=0 )((void) (0));
170185 assert( pIx->pSchema==pTab->pSchema )((void) (0));
170186 assert( iIndexCur>=0 )((void) (0));
170187 if( op ){
170188 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
170189 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
170190 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)!=0
170191 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE0x00000002|WHERE_SKIPSCAN0x00008000))==0
170192 && (pLoop->wsFlags & WHERE_BIGNULL_SORT0x00080000)==0
170193 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN0x00100000)==0
170194 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN0x0001)==0
170195 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED2
170196 ){
170197 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02);
170198 }
170199 VdbeComment((v, "%s", pIx->zName));
170200#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
170201 {
170202 u64 colUsed = 0;
170203 int ii, jj;
170204 for(ii=0; ii<pIx->nColumn; ii++){
170205 jj = pIx->aiColumn[ii];
170206 if( jj<0 ) continue;
170207 if( jj>63 ) jj = 63;
170208 if( (pTabItem->colUsed & MASKBIT(jj)(((Bitmask)1)<<(jj)))==0 ) continue;
170209 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
170210 }
170211 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed123, iIndexCur, 0, 0,
170212 (u8*)&colUsed, P4_INT64(-13));
170213 }
170214#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
170215 }
170216 }
170217 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
170218 if( (pTabItem->fg.jointype & JT_RIGHT0x10)!=0
170219 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
170220 ){
170221 WhereRightJoin *pRJ = pLevel->pRJ;
170222 pRJ->iMatch = pParse->nTab++;
170223 pRJ->regBloom = ++pParse->nMem;
170224 sqlite3VdbeAddOp2(v, OP_Blob77, 65536, pRJ->regBloom);
170225 pRJ->regReturn = ++pParse->nMem;
170226 sqlite3VdbeAddOp2(v, OP_Null75, 0, pRJ->regReturn);
170227 assert( pTab==pTabItem->pSTab )((void) (0));
170228 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
170229 KeyInfo *pInfo;
170230 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pRJ->iMatch, 1);
170231 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
170232 if( pInfo ){
170233 pInfo->aColl[0] = 0;
170234 pInfo->aSortFlags[0] = 0;
170235 sqlite3VdbeAppendP4(v, pInfo, P4_KEYINFO(-8));
170236 }
170237 }else{
170238 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
170239 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pRJ->iMatch, pPk->nKeyCol);
170240 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
170241 }
170242 pLoop->wsFlags &= ~WHERE_IDX_ONLY0x00000040;
170243 /* The nature of RIGHT JOIN processing is such that it messes up
170244 ** the output order. So omit any ORDER BY/GROUP BY elimination
170245 ** optimizations. We need to do an actual sort for RIGHT JOIN. */
170246 pWInfo->nOBSat = 0;
170247 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED3;
170248 }
170249 }
170250 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
170251 if( db->mallocFailed ) goto whereBeginError;
170252
170253 /* Generate the code to do the search. Each iteration of the for
170254 ** loop below generates code for a single nested loop of the VM
170255 ** program.
170256 */
170257 for(ii=0; ii<nTabList; ii++){
170258 int addrExplain;
170259 int wsFlags;
170260 SrcItem *pSrc;
170261 if( pParse->nErr ) goto whereBeginError;
170262 pLevel = &pWInfo->a[ii];
170263 wsFlags = pLevel->pWLoop->wsFlags;
170264 pSrc = &pTabList->a[pLevel->iFrom];
170265 if( pSrc->fg.isMaterialized ){
170266 Subquery *pSubq;
170267 int iOnce = 0;
170268 assert( pSrc->fg.isSubquery )((void) (0));
170269 pSubq = pSrc->u4.pSubq;
170270 if( pSrc->fg.isCorrelated==0 ){
170271 iOnce = sqlite3VdbeAddOp0(v, OP_Once15); VdbeCoverage(v);
170272 }else{
170273 iOnce = 0;
170274 }
170275 sqlite3VdbeAddOp2(v, OP_Gosub10, pSubq->regReturn, pSubq->addrFillSub);
170276 VdbeComment((v, "materialize %!S", pSrc));
170277 if( iOnce ) sqlite3VdbeJumpHere(v, iOnce);
170278 }
170279 assert( pTabList == pWInfo->pTabList )((void) (0));
170280 if( (wsFlags & (WHERE_AUTO_INDEX0x00004000|WHERE_BLOOMFILTER0x00400000))!=0 ){
170281 if( (wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
170282#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
170283 constructAutomaticIndex(pParse, &pWInfo->sWC, notReady, pLevel);
170284#endif
170285 }else{
170286 sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady);
170287 }
170288 if( db->mallocFailed ) goto whereBeginError;
170289 }
170290 addrExplain = sqlite3WhereExplainOneScan(
170291 pParse, pTabList, pLevel, wctrlFlags
170292 );
170293 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
170294 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
170295 pWInfo->iContinue = pLevel->addrCont;
170296 if( (wsFlags&WHERE_MULTI_OR0x00002000)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020)==0 ){
170297 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
170298 }
170299 }
170300
170301 /* Done. */
170302 VdbeModuleComment((v, "Begin WHERE-core"));
170303 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
170304 return pWInfo;
170305
170306 /* Jump here if malloc fails */
170307whereBeginError:
170308 if( pWInfo ){
170309 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
170310 whereInfoFree(db, pWInfo);
170311 }
170312#ifdef WHERETRACE_ENABLED
170313 /* Prevent harmless compiler warnings about debugging routines
170314 ** being declared but never used */
170315 sqlite3ShowWhereLoopList(0);
170316#endif /* WHERETRACE_ENABLED */
170317 return 0;
170318}
170319
170320/*
170321** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
170322** index rather than the main table. In SQLITE_DEBUG mode, we want
170323** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
170324** does that.
170325*/
170326#ifndef SQLITE_DEBUG
170327# define OpcodeRewriteTrace(D,K,P) /* no-op */
170328#else
170329# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
170330 static void sqlite3WhereOpcodeRewriteTrace(
170331 sqlite3 *db,
170332 int pc,
170333 VdbeOp *pOp
170334 ){
170335 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
170336 sqlite3VdbePrintOp(0, pc, pOp);
170337 }
170338#endif
170339
170340/*
170341** Generate the end of the WHERE loop. See comments on
170342** sqlite3WhereBegin() for additional information.
170343*/
170344SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo *pWInfo){
170345 Parse *pParse = pWInfo->pParse;
170346 Vdbe *v = pParse->pVdbe;
170347 int i;
170348 WhereLevel *pLevel;
170349 WhereLoop *pLoop;
170350 SrcList *pTabList = pWInfo->pTabList;
170351 sqlite3 *db = pParse->db;
170352 int iEnd = sqlite3VdbeCurrentAddr(v);
170353 int nRJ = 0;
170354
170355 /* Generate loop termination code.
170356 */
170357 VdbeModuleComment((v, "End WHERE-core"));
170358 for(i=pWInfo->nLevel-1; i>=0; i--){
170359 int addr;
170360 pLevel = &pWInfo->a[i];
170361 if( pLevel->pRJ ){
170362 /* Terminate the subroutine that forms the interior of the loop of
170363 ** the RIGHT JOIN table */
170364 WhereRightJoin *pRJ = pLevel->pRJ;
170365 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
170366 pLevel->addrCont = 0;
170367 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
170368 sqlite3VdbeAddOp3(v, OP_Return67, pRJ->regReturn, pRJ->addrSubrtn, 1);
170369 VdbeCoverage(v);
170370 nRJ++;
170371 }
170372 pLoop = pLevel->pWLoop;
170373 if( pLevel->op!=OP_Noop187 ){
170374#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
170375 int addrSeek = 0;
170376 Index *pIdx;
170377 int n;
170378 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED2
170379 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
170380 && (pLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
170381 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
170382 && (n = pLoop->u.btree.nDistinctCol)>0
170383 && pIdx->aiRowLogEst[n]>=36
170384 ){
170385 int r1 = pParse->nMem+1;
170386 int j, op;
170387 for(j=0; j<n; j++){
170388 sqlite3VdbeAddOp3(v, OP_Column94, pLevel->iIdxCur, j, r1+j);
170389 }
170390 pParse->nMem += n+1;
170391 op = pLevel->op==OP_Prev38 ? OP_SeekLT21 : OP_SeekGT24;
170392 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
170393 VdbeCoverageIf(v, op==OP_SeekLT);
170394 VdbeCoverageIf(v, op==OP_SeekGT);
170395 sqlite3VdbeAddOp2(v, OP_Goto9, 1, pLevel->p2);
170396 }
170397#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
170398 /* The common case: Advance to the next row */
170399 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
170400 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
170401 sqlite3VdbeChangeP5(v, pLevel->p5);
170402 VdbeCoverage(v);
170403 VdbeCoverageIf(v, pLevel->op==OP_Next);
170404 VdbeCoverageIf(v, pLevel->op==OP_Prev);
170405 VdbeCoverageIf(v, pLevel->op==OP_VNext);
170406 if( pLevel->regBignull ){
170407 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
170408 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, pLevel->regBignull, pLevel->p2-1);
170409 VdbeCoverage(v);
170410 }
170411#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
170412 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
170413#endif
170414 }else if( pLevel->addrCont ){
170415 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
170416 }
170417 if( (pLoop->wsFlags & WHERE_IN_ABLE0x00000800)!=0 && pLevel->u.in.nIn>0 ){
170418 struct InLoop *pIn;
170419 int j;
170420 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
170421 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
170422 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull((void) (0))
170423 || pParse->db->mallocFailed )((void) (0));
170424 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
170425 if( pIn->eEndLoopOp!=OP_Noop187 ){
170426 if( pIn->nPrefix ){
170427 int bEarlyOut =
170428 (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
170429 && (pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000)!=0;
170430 if( pLevel->iLeftJoin ){
170431 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
170432 ** opened yet. This occurs for WHERE clauses such as
170433 ** "a = ? AND b IN (...)", where the index is on (a, b). If
170434 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
170435 ** never have been coded, but the body of the loop run to
170436 ** return the null-row. So, if the cursor is not open yet,
170437 ** jump over the OP_Next or OP_Prev instruction about to
170438 ** be coded. */
170439 sqlite3VdbeAddOp2(v, OP_IfNotOpen25, pIn->iCur,
170440 sqlite3VdbeCurrentAddr(v) + 2 + bEarlyOut);
170441 VdbeCoverage(v);
170442 }
170443 if( bEarlyOut ){
170444 sqlite3VdbeAddOp4Int(v, OP_IfNoHope26, pLevel->iIdxCur,
170445 sqlite3VdbeCurrentAddr(v)+2,
170446 pIn->iBase, pIn->nPrefix);
170447 VdbeCoverage(v);
170448 /* Retarget the OP_IsNull against the left operand of IN so
170449 ** it jumps past the OP_IfNoHope. This is because the
170450 ** OP_IsNull also bypasses the OP_Affinity opcode that is
170451 ** required by OP_IfNoHope. */
170452 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
170453 }
170454 }
170455 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
170456 VdbeCoverage(v);
170457 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
170458 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
170459 }
170460 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
170461 }
170462 }
170463 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
170464 if( pLevel->pRJ ){
170465 sqlite3VdbeAddOp3(v, OP_Return67, pLevel->pRJ->regReturn, 0, 1);
170466 VdbeCoverage(v);
170467 }
170468 if( pLevel->addrSkip ){
170469 sqlite3VdbeGoto(v, pLevel->addrSkip);
170470 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
170471 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
170472 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
170473 }
170474#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
170475 if( pLevel->addrLikeRep ){
170476 sqlite3VdbeAddOp2(v, OP_DecrJumpZero61, (int)(pLevel->iLikeRepCntr>>1),
170477 pLevel->addrLikeRep);
170478 VdbeCoverage(v);
170479 }
170480#endif
170481 if( pLevel->iLeftJoin ){
170482 int ws = pLoop->wsFlags;
170483 addr = sqlite3VdbeAddOp1(v, OP_IfPos50, pLevel->iLeftJoin); VdbeCoverage(v);
170484 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 )((void) (0));
170485 if( (ws & WHERE_IDX_ONLY0x00000040)==0 ){
170486 SrcItem *pSrc = &pTabList->a[pLevel->iFrom];
170487 assert( pLevel->iTabCur==pSrc->iCursor )((void) (0));
170488 if( pSrc->fg.viaCoroutine ){
170489 int m, n;
170490 assert( pSrc->fg.isSubquery )((void) (0));
170491 n = pSrc->u4.pSubq->regResult;
170492 assert( pSrc->pSTab!=0 )((void) (0));
170493 m = pSrc->pSTab->nCol;
170494 sqlite3VdbeAddOp3(v, OP_Null75, 0, n, n+m-1);
170495 }
170496 sqlite3VdbeAddOp1(v, OP_NullRow136, pLevel->iTabCur);
170497 }
170498 if( (ws & WHERE_INDEXED0x00000200)
170499 || ((ws & WHERE_MULTI_OR0x00002000) && pLevel->u.pCoveringIdx)
170500 ){
170501 if( ws & WHERE_MULTI_OR0x00002000 ){
170502 Index *pIx = pLevel->u.pCoveringIdx;
170503 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
170504 sqlite3VdbeAddOp3(v, OP_ReopenIdx101, pLevel->iIdxCur, pIx->tnum, iDb);
170505 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
170506 }
170507 sqlite3VdbeAddOp1(v, OP_NullRow136, pLevel->iIdxCur);
170508 }
170509 if( pLevel->op==OP_Return67 ){
170510 sqlite3VdbeAddOp2(v, OP_Gosub10, pLevel->p1, pLevel->addrFirst);
170511 }else{
170512 sqlite3VdbeGoto(v, pLevel->addrFirst);
170513 }
170514 sqlite3VdbeJumpHere(v, addr);
170515 }
170516 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
170517 pWInfo->pTabList->a[pLevel->iFrom].pSTab->zName));
170518 }
170519
170520 assert( pWInfo->nLevel<=pTabList->nSrc )((void) (0));
170521 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
170522 int k, last;
170523 VdbeOp *pOp, *pLastOp;
170524 Index *pIdx = 0;
170525 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
170526 Table *pTab = pTabItem->pSTab;
170527 assert( pTab!=0 )((void) (0));
170528 pLoop = pLevel->pWLoop;
170529
170530 /* Do RIGHT JOIN processing. Generate code that will output the
170531 ** unmatched rows of the right operand of the RIGHT JOIN with
170532 ** all of the columns of the left operand set to NULL.
170533 */
170534 if( pLevel->pRJ ){
170535 sqlite3WhereRightJoinLoop(pWInfo, i, pLevel);
170536 continue;
170537 }
170538
170539 /* For a co-routine, change all OP_Column references to the table of
170540 ** the co-routine into OP_Copy of result contained in a register.
170541 ** OP_Rowid becomes OP_Null.
170542 */
170543 if( pTabItem->fg.viaCoroutine ){
170544 testcase( pParse->db->mallocFailed );
170545 assert( pTabItem->fg.isSubquery )((void) (0));
170546 assert( pTabItem->u4.pSubq->regResult>=0 )((void) (0));
170547 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
170548 pTabItem->u4.pSubq->regResult, 0);
170549 continue;
170550 }
170551
170552 /* If this scan uses an index, make VDBE code substitutions to read data
170553 ** from the index instead of from the table where possible. In some cases
170554 ** this optimization prevents the table from ever being read, which can
170555 ** yield a significant performance boost.
170556 **
170557 ** Calls to the code generator in between sqlite3WhereBegin and
170558 ** sqlite3WhereEnd will have created code that references the table
170559 ** directly. This loop scans all that code looking for opcodes
170560 ** that reference the table and converts them into opcodes that
170561 ** reference the index.
170562 */
170563 if( pLoop->wsFlags & (WHERE_INDEXED0x00000200|WHERE_IDX_ONLY0x00000040) ){
170564 pIdx = pLoop->u.btree.pIndex;
170565 }else if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
170566 pIdx = pLevel->u.pCoveringIdx;
170567 }
170568 if( pIdx
170569 && !db->mallocFailed
170570 ){
170571 if( pWInfo->eOnePass==ONEPASS_OFF0 || !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x00000080)==0) ){
170572 last = iEnd;
170573 }else{
170574 last = pWInfo->iEndWhere;
170575 }
170576 if( pIdx->bHasExpr ){
170577 IndexedExpr *p = pParse->pIdxEpr;
170578 while( p ){
170579 if( p->iIdxCur==pLevel->iIdxCur ){
170580#ifdef WHERETRACE_ENABLED
170581 if( sqlite3WhereTrace & 0x200 ){
170582 sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n",
170583 p->iIdxCur, p->iIdxCol);
170584 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr);
170585 }
170586#endif
170587 p->iDataCur = -1;
170588 p->iIdxCur = -1;
170589 }
170590 p = p->pIENext;
170591 }
170592 }
170593 k = pLevel->addrBody + 1;
170594#ifdef SQLITE_DEBUG
170595 if( db->flags & SQLITE_VdbeAddopTrace ){
170596 printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n",
170597 pLevel->iTabCur, pLevel->iIdxCur, k, last-1);
170598 }
170599 /* Proof that the "+1" on the k value above is safe */
170600 pOp = sqlite3VdbeGetOp(v, k - 1);
170601 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur )((void) (0));
170602 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur )((void) (0));
170603 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur )((void) (0));
170604#endif
170605 pOp = sqlite3VdbeGetOp(v, k);
170606 pLastOp = pOp + (last - k);
170607 assert( pOp<=pLastOp )((void) (0));
170608 do{
170609 if( pOp->p1!=pLevel->iTabCur ){
170610 /* no-op */
170611 }else if( pOp->opcode==OP_Column94
170612#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
170613 || pOp->opcode==OP_Offset93
170614#endif
170615 ){
170616 int x = pOp->p2;
170617 assert( pIdx->pTable==pTab )((void) (0));
170618#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
170619 if( pOp->opcode==OP_Offset93 ){
170620 /* Do not need to translate the column number */
170621 }else
170622#endif
170623 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) ){
170624 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
170625 x = pPk->aiColumn[x];
170626 assert( x>=0 )((void) (0));
170627 }else{
170628 testcase( x!=sqlite3StorageColumnToTable(pTab,x) );
170629 x = sqlite3StorageColumnToTable(pTab,x);
170630 }
170631 x = sqlite3TableColumnToIndex(pIdx, x);
170632 if( x>=0 ){
170633 pOp->p2 = x;
170634 pOp->p1 = pLevel->iIdxCur;
170635 OpcodeRewriteTrace(db, k, pOp);
170636 }else{
170637 /* Unable to translate the table reference into an index
170638 ** reference. Verify that this is harmless - that the
170639 ** table being referenced really is open.
170640 */
170641 if( pLoop->wsFlags & WHERE_IDX_ONLY0x00000040 ){
170642 sqlite3ErrorMsg(pParse, "internal query planner error");
170643 pParse->rc = SQLITE_INTERNAL2;
170644 }
170645 }
170646 }else if( pOp->opcode==OP_Rowid135 ){
170647 pOp->p1 = pLevel->iIdxCur;
170648 pOp->opcode = OP_IdxRowid142;
170649 OpcodeRewriteTrace(db, k, pOp);
170650 }else if( pOp->opcode==OP_IfNullRow20 ){
170651 pOp->p1 = pLevel->iIdxCur;
170652 OpcodeRewriteTrace(db, k, pOp);
170653 }
170654#ifdef SQLITE_DEBUG
170655 k++;
170656#endif
170657 }while( (++pOp)<pLastOp );
170658#ifdef SQLITE_DEBUG
170659 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
170660#endif
170661 }
170662 }
170663
170664 /* The "break" point is here, just past the end of the outer loop.
170665 ** Set it.
170666 */
170667 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
170668
170669 /* Final cleanup
170670 */
170671 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
170672 whereInfoFree(db, pWInfo);
170673 pParse->withinRJSubrtn -= nRJ;
170674 return;
170675}
170676
170677/************** End of where.c ***********************************************/
170678/************** Begin file window.c ******************************************/
170679/*
170680** 2018 May 08
170681**
170682** The author disclaims copyright to this source code. In place of
170683** a legal notice, here is a blessing:
170684**
170685** May you do good and not evil.
170686** May you find forgiveness for yourself and forgive others.
170687** May you share freely, never taking more than you give.
170688**
170689*************************************************************************
170690*/
170691/* #include "sqliteInt.h" */
170692
170693#ifndef SQLITE_OMIT_WINDOWFUNC
170694
170695/*
170696** SELECT REWRITING
170697**
170698** Any SELECT statement that contains one or more window functions in
170699** either the select list or ORDER BY clause (the only two places window
170700** functions may be used) is transformed by function sqlite3WindowRewrite()
170701** in order to support window function processing. For example, with the
170702** schema:
170703**
170704** CREATE TABLE t1(a, b, c, d, e, f, g);
170705**
170706** the statement:
170707**
170708** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
170709**
170710** is transformed to:
170711**
170712** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
170713** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
170714** ) ORDER BY e;
170715**
170716** The flattening optimization is disabled when processing this transformed
170717** SELECT statement. This allows the implementation of the window function
170718** (in this case max()) to process rows sorted in order of (c, d), which
170719** makes things easier for obvious reasons. More generally:
170720**
170721** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
170722** the sub-query.
170723**
170724** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
170725**
170726** * Terminals from each of the expression trees that make up the
170727** select-list and ORDER BY expressions in the parent query are
170728** selected by the sub-query. For the purposes of the transformation,
170729** terminals are column references and aggregate functions.
170730**
170731** If there is more than one window function in the SELECT that uses
170732** the same window declaration (the OVER bit), then a single scan may
170733** be used to process more than one window function. For example:
170734**
170735** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
170736** min(e) OVER (PARTITION BY c ORDER BY d)
170737** FROM t1;
170738**
170739** is transformed in the same way as the example above. However:
170740**
170741** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
170742** min(e) OVER (PARTITION BY a ORDER BY b)
170743** FROM t1;
170744**
170745** Must be transformed to:
170746**
170747** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
170748** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
170749** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
170750** ) ORDER BY c, d
170751** ) ORDER BY e;
170752**
170753** so that both min() and max() may process rows in the order defined by
170754** their respective window declarations.
170755**
170756** INTERFACE WITH SELECT.C
170757**
170758** When processing the rewritten SELECT statement, code in select.c calls
170759** sqlite3WhereBegin() to begin iterating through the results of the
170760** sub-query, which is always implemented as a co-routine. It then calls
170761** sqlite3WindowCodeStep() to process rows and finish the scan by calling
170762** sqlite3WhereEnd().
170763**
170764** sqlite3WindowCodeStep() generates VM code so that, for each row returned
170765** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
170766** When the sub-routine is invoked:
170767**
170768** * The results of all window-functions for the row are stored
170769** in the associated Window.regResult registers.
170770**
170771** * The required terminal values are stored in the current row of
170772** temp table Window.iEphCsr.
170773**
170774** In some cases, depending on the window frame and the specific window
170775** functions invoked, sqlite3WindowCodeStep() caches each entire partition
170776** in a temp table before returning any rows. In other cases it does not.
170777** This detail is encapsulated within this file, the code generated by
170778** select.c is the same in either case.
170779**
170780** BUILT-IN WINDOW FUNCTIONS
170781**
170782** This implementation features the following built-in window functions:
170783**
170784** row_number()
170785** rank()
170786** dense_rank()
170787** percent_rank()
170788** cume_dist()
170789** ntile(N)
170790** lead(expr [, offset [, default]])
170791** lag(expr [, offset [, default]])
170792** first_value(expr)
170793** last_value(expr)
170794** nth_value(expr, N)
170795**
170796** These are the same built-in window functions supported by Postgres.
170797** Although the behaviour of aggregate window functions (functions that
170798** can be used as either aggregates or window functions) allows them to
170799** be implemented using an API, built-in window functions are much more
170800** esoteric. Additionally, some window functions (e.g. nth_value())
170801** may only be implemented by caching the entire partition in memory.
170802** As such, some built-in window functions use the same API as aggregate
170803** window functions and some are implemented directly using VDBE
170804** instructions. Additionally, for those functions that use the API, the
170805** window frame is sometimes modified before the SELECT statement is
170806** rewritten. For example, regardless of the specified window frame, the
170807** row_number() function always uses:
170808**
170809** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
170810**
170811** See sqlite3WindowUpdate() for details.
170812**
170813** As well as some of the built-in window functions, aggregate window
170814** functions min() and max() are implemented using VDBE instructions if
170815** the start of the window frame is declared as anything other than
170816** UNBOUNDED PRECEDING.
170817*/
170818
170819/*
170820** Implementation of built-in window function row_number(). Assumes that the
170821** window frame has been coerced to:
170822**
170823** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
170824*/
170825static void row_numberStepFunc(
170826 sqlite3_context *pCtx,
170827 int nArg,
170828 sqlite3_value **apArg
170829){
170830 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170831 if( p ) (*p)++;
170832 UNUSED_PARAMETER(nArg)(void)(nArg);
170833 UNUSED_PARAMETER(apArg)(void)(apArg);
170834}
170835static void row_numberValueFunc(sqlite3_context *pCtx){
170836 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170837 sqlite3_result_int64(pCtx, (p ? *p : 0));
170838}
170839
170840/*
170841** Context object type used by rank(), dense_rank(), percent_rank() and
170842** cume_dist().
170843*/
170844struct CallCount {
170845 i64 nValue;
170846 i64 nStep;
170847 i64 nTotal;
170848};
170849
170850/*
170851** Implementation of built-in window function dense_rank(). Assumes that
170852** the window frame has been set to:
170853**
170854** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
170855*/
170856static void dense_rankStepFunc(
170857 sqlite3_context *pCtx,
170858 int nArg,
170859 sqlite3_value **apArg
170860){
170861 struct CallCount *p;
170862 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170863 if( p ) p->nStep = 1;
170864 UNUSED_PARAMETER(nArg)(void)(nArg);
170865 UNUSED_PARAMETER(apArg)(void)(apArg);
170866}
170867static void dense_rankValueFunc(sqlite3_context *pCtx){
170868 struct CallCount *p;
170869 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170870 if( p ){
170871 if( p->nStep ){
170872 p->nValue++;
170873 p->nStep = 0;
170874 }
170875 sqlite3_result_int64(pCtx, p->nValue);
170876 }
170877}
170878
170879/*
170880** Implementation of built-in window function nth_value(). This
170881** implementation is used in "slow mode" only - when the EXCLUDE clause
170882** is not set to the default value "NO OTHERS".
170883*/
170884struct NthValueCtx {
170885 i64 nStep;
170886 sqlite3_value *pValue;
170887};
170888static void nth_valueStepFunc(
170889 sqlite3_context *pCtx,
170890 int nArg,
170891 sqlite3_value **apArg
170892){
170893 struct NthValueCtx *p;
170894 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170895 if( p ){
170896 i64 iVal;
170897 switch( sqlite3_value_numeric_type(apArg[1]) ){
170898 case SQLITE_INTEGER1:
170899 iVal = sqlite3_value_int64(apArg[1]);
170900 break;
170901 case SQLITE_FLOAT2: {
170902 double fVal = sqlite3_value_double(apArg[1]);
170903 if( ((i64)fVal)!=fVal ) goto error_out;
170904 iVal = (i64)fVal;
170905 break;
170906 }
170907 default:
170908 goto error_out;
170909 }
170910 if( iVal<=0 ) goto error_out;
170911
170912 p->nStep++;
170913 if( iVal==p->nStep ){
170914 p->pValue = sqlite3_value_dup(apArg[0]);
170915 if( !p->pValue ){
170916 sqlite3_result_error_nomem(pCtx);
170917 }
170918 }
170919 }
170920 UNUSED_PARAMETER(nArg)(void)(nArg);
170921 UNUSED_PARAMETER(apArg)(void)(apArg);
170922 return;
170923
170924 error_out:
170925 sqlite3_result_error(
170926 pCtx, "second argument to nth_value must be a positive integer", -1
170927 );
170928}
170929static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
170930 struct NthValueCtx *p;
170931 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
170932 if( p && p->pValue ){
170933 sqlite3_result_value(pCtx, p->pValue);
170934 sqlite3_value_free(p->pValue);
170935 p->pValue = 0;
170936 }
170937}
170938#define nth_valueInvFuncnoopStepFunc noopStepFunc
170939#define nth_valueValueFuncnoopValueFunc noopValueFunc
170940
170941static void first_valueStepFunc(
170942 sqlite3_context *pCtx,
170943 int nArg,
170944 sqlite3_value **apArg
170945){
170946 struct NthValueCtx *p;
170947 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170948 if( p && p->pValue==0 ){
170949 p->pValue = sqlite3_value_dup(apArg[0]);
170950 if( !p->pValue ){
170951 sqlite3_result_error_nomem(pCtx);
170952 }
170953 }
170954 UNUSED_PARAMETER(nArg)(void)(nArg);
170955 UNUSED_PARAMETER(apArg)(void)(apArg);
170956}
170957static void first_valueFinalizeFunc(sqlite3_context *pCtx){
170958 struct NthValueCtx *p;
170959 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170960 if( p && p->pValue ){
170961 sqlite3_result_value(pCtx, p->pValue);
170962 sqlite3_value_free(p->pValue);
170963 p->pValue = 0;
170964 }
170965}
170966#define first_valueInvFuncnoopStepFunc noopStepFunc
170967#define first_valueValueFuncnoopValueFunc noopValueFunc
170968
170969/*
170970** Implementation of built-in window function rank(). Assumes that
170971** the window frame has been set to:
170972**
170973** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
170974*/
170975static void rankStepFunc(
170976 sqlite3_context *pCtx,
170977 int nArg,
170978 sqlite3_value **apArg
170979){
170980 struct CallCount *p;
170981 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170982 if( p ){
170983 p->nStep++;
170984 if( p->nValue==0 ){
170985 p->nValue = p->nStep;
170986 }
170987 }
170988 UNUSED_PARAMETER(nArg)(void)(nArg);
170989 UNUSED_PARAMETER(apArg)(void)(apArg);
170990}
170991static void rankValueFunc(sqlite3_context *pCtx){
170992 struct CallCount *p;
170993 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
170994 if( p ){
170995 sqlite3_result_int64(pCtx, p->nValue);
170996 p->nValue = 0;
170997 }
170998}
170999
171000/*
171001** Implementation of built-in window function percent_rank(). Assumes that
171002** the window frame has been set to:
171003**
171004** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
171005*/
171006static void percent_rankStepFunc(
171007 sqlite3_context *pCtx,
171008 int nArg,
171009 sqlite3_value **apArg
171010){
171011 struct CallCount *p;
171012 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
171013 UNUSED_PARAMETER(apArg)(void)(apArg);
171014 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171015 if( p ){
171016 p->nTotal++;
171017 }
171018}
171019static void percent_rankInvFunc(
171020 sqlite3_context *pCtx,
171021 int nArg,
171022 sqlite3_value **apArg
171023){
171024 struct CallCount *p;
171025 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
171026 UNUSED_PARAMETER(apArg)(void)(apArg);
171027 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171028 p->nStep++;
171029}
171030static void percent_rankValueFunc(sqlite3_context *pCtx){
171031 struct CallCount *p;
171032 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171033 if( p ){
171034 p->nValue = p->nStep;
171035 if( p->nTotal>1 ){
171036 double r = (double)p->nValue / (double)(p->nTotal-1);
171037 sqlite3_result_double(pCtx, r);
171038 }else{
171039 sqlite3_result_double(pCtx, 0.0);
171040 }
171041 }
171042}
171043#define percent_rankFinalizeFuncpercent_rankValueFunc percent_rankValueFunc
171044
171045/*
171046** Implementation of built-in window function cume_dist(). Assumes that
171047** the window frame has been set to:
171048**
171049** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
171050*/
171051static void cume_distStepFunc(
171052 sqlite3_context *pCtx,
171053 int nArg,
171054 sqlite3_value **apArg
171055){
171056 struct CallCount *p;
171057 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
171058 UNUSED_PARAMETER(apArg)(void)(apArg);
171059 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171060 if( p ){
171061 p->nTotal++;
171062 }
171063}
171064static void cume_distInvFunc(
171065 sqlite3_context *pCtx,
171066 int nArg,
171067 sqlite3_value **apArg
171068){
171069 struct CallCount *p;
171070 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
171071 UNUSED_PARAMETER(apArg)(void)(apArg);
171072 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171073 p->nStep++;
171074}
171075static void cume_distValueFunc(sqlite3_context *pCtx){
171076 struct CallCount *p;
171077 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
171078 if( p ){
171079 double r = (double)(p->nStep) / (double)(p->nTotal);
171080 sqlite3_result_double(pCtx, r);
171081 }
171082}
171083#define cume_distFinalizeFunccume_distValueFunc cume_distValueFunc
171084
171085/*
171086** Context object for ntile() window function.
171087*/
171088struct NtileCtx {
171089 i64 nTotal; /* Total rows in partition */
171090 i64 nParam; /* Parameter passed to ntile(N) */
171091 i64 iRow; /* Current row */
171092};
171093
171094/*
171095** Implementation of ntile(). This assumes that the window frame has
171096** been coerced to:
171097**
171098** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
171099*/
171100static void ntileStepFunc(
171101 sqlite3_context *pCtx,
171102 int nArg,
171103 sqlite3_value **apArg
171104){
171105 struct NtileCtx *p;
171106 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
171107 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171108 if( p ){
171109 if( p->nTotal==0 ){
171110 p->nParam = sqlite3_value_int64(apArg[0]);
171111 if( p->nParam<=0 ){
171112 sqlite3_result_error(
171113 pCtx, "argument of ntile must be a positive integer", -1
171114 );
171115 }
171116 }
171117 p->nTotal++;
171118 }
171119}
171120static void ntileInvFunc(
171121 sqlite3_context *pCtx,
171122 int nArg,
171123 sqlite3_value **apArg
171124){
171125 struct NtileCtx *p;
171126 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
171127 UNUSED_PARAMETER(apArg)(void)(apArg);
171128 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171129 p->iRow++;
171130}
171131static void ntileValueFunc(sqlite3_context *pCtx){
171132 struct NtileCtx *p;
171133 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171134 if( p && p->nParam>0 ){
171135 int nSize = (p->nTotal / p->nParam);
171136 if( nSize==0 ){
171137 sqlite3_result_int64(pCtx, p->iRow+1);
171138 }else{
171139 i64 nLarge = p->nTotal - p->nParam*nSize;
171140 i64 iSmall = nLarge*(nSize+1);
171141 i64 iRow = p->iRow;
171142
171143 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal )((void) (0));
171144
171145 if( iRow<iSmall ){
171146 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
171147 }else{
171148 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
171149 }
171150 }
171151 }
171152}
171153#define ntileFinalizeFuncntileValueFunc ntileValueFunc
171154
171155/*
171156** Context object for last_value() window function.
171157*/
171158struct LastValueCtx {
171159 sqlite3_value *pVal;
171160 int nVal;
171161};
171162
171163/*
171164** Implementation of last_value().
171165*/
171166static void last_valueStepFunc(
171167 sqlite3_context *pCtx,
171168 int nArg,
171169 sqlite3_value **apArg
171170){
171171 struct LastValueCtx *p;
171172 UNUSED_PARAMETER(nArg)(void)(nArg);
171173 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171174 if( p ){
171175 sqlite3_value_free(p->pVal);
171176 p->pVal = sqlite3_value_dup(apArg[0]);
171177 if( p->pVal==0 ){
171178 sqlite3_result_error_nomem(pCtx);
171179 }else{
171180 p->nVal++;
171181 }
171182 }
171183}
171184static void last_valueInvFunc(
171185 sqlite3_context *pCtx,
171186 int nArg,
171187 sqlite3_value **apArg
171188){
171189 struct LastValueCtx *p;
171190 UNUSED_PARAMETER(nArg)(void)(nArg);
171191 UNUSED_PARAMETER(apArg)(void)(apArg);
171192 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171193 if( ALWAYS(p)(p) ){
171194 p->nVal--;
171195 if( p->nVal==0 ){
171196 sqlite3_value_free(p->pVal);
171197 p->pVal = 0;
171198 }
171199 }
171200}
171201static void last_valueValueFunc(sqlite3_context *pCtx){
171202 struct LastValueCtx *p;
171203 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
171204 if( p && p->pVal ){
171205 sqlite3_result_value(pCtx, p->pVal);
171206 }
171207}
171208static void last_valueFinalizeFunc(sqlite3_context *pCtx){
171209 struct LastValueCtx *p;
171210 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
171211 if( p && p->pVal ){
171212 sqlite3_result_value(pCtx, p->pVal);
171213 sqlite3_value_free(p->pVal);
171214 p->pVal = 0;
171215 }
171216}
171217
171218/*
171219** Static names for the built-in window function names. These static
171220** names are used, rather than string literals, so that FuncDef objects
171221** can be associated with a particular window function by direct
171222** comparison of the zName pointer. Example:
171223**
171224** if( pFuncDef->zName==row_valueName ){ ... }
171225*/
171226static const char row_numberName[] = "row_number";
171227static const char dense_rankName[] = "dense_rank";
171228static const char rankName[] = "rank";
171229static const char percent_rankName[] = "percent_rank";
171230static const char cume_distName[] = "cume_dist";
171231static const char ntileName[] = "ntile";
171232static const char last_valueName[] = "last_value";
171233static const char nth_valueName[] = "nth_value";
171234static const char first_valueName[] = "first_value";
171235static const char leadName[] = "lead";
171236static const char lagName[] = "lag";
171237
171238/*
171239** No-op implementations of xStep() and xFinalize(). Used as place-holders
171240** for built-in window functions that never call those interfaces.
171241**
171242** The noopValueFunc() is called but is expected to do nothing. The
171243** noopStepFunc() is never called, and so it is marked with NO_TEST to
171244** let the test coverage routine know not to expect this function to be
171245** invoked.
171246*/
171247static void noopStepFunc( /*NO_TEST*/
171248 sqlite3_context *p, /*NO_TEST*/
171249 int n, /*NO_TEST*/
171250 sqlite3_value **a /*NO_TEST*/
171251){ /*NO_TEST*/
171252 UNUSED_PARAMETER(p)(void)(p); /*NO_TEST*/
171253 UNUSED_PARAMETER(n)(void)(n); /*NO_TEST*/
171254 UNUSED_PARAMETER(a)(void)(a); /*NO_TEST*/
171255 assert(0)((void) (0)); /*NO_TEST*/
171256} /*NO_TEST*/
171257static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p)(void)(p); /*no-op*/ }
171258
171259/* Window functions that use all window interfaces: xStep, xFinal,
171260** xValue, and xInverse */
171261#define WINDOWFUNCALL(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, nameStepFunc, nameFinalizeFunc
, nameValueFunc, nameInvFunc, nameName, {0} }
{ \
171262 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
171263 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
171264 name ## InvFunc, name ## Name, {0} \
171265}
171266
171267/* Window functions that are implemented using bytecode and thus have
171268** no-op routines for their methods */
171269#define WINDOWFUNCNOOP(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, nameName, {0} }
{ \
171270 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
171271 noopStepFunc, noopValueFunc, noopValueFunc, \
171272 noopStepFunc, name ## Name, {0} \
171273}
171274
171275/* Window functions that use all window interfaces: xStep, the
171276** same routine for xFinalize and xValue and which never call
171277** xInverse. */
171278#define WINDOWFUNCX(name,nArg,extra){ nArg, (0x00800000|1|0x00010000|extra), 0, 0, nameStepFunc, nameValueFunc
, nameValueFunc, noopStepFunc, nameName, {0} }
{ \
171279 nArg, (SQLITE_FUNC_BUILTIN0x00800000|SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
171280 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
171281 noopStepFunc, name ## Name, {0} \
171282}
171283
171284
171285/*
171286** Register those built-in window functions that are not also aggregates.
171287*/
171288SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void){
171289 static FuncDef aWindowFuncs[] = {
171290 WINDOWFUNCX(row_number, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, row_numberStepFunc, row_numberValueFunc
, row_numberValueFunc, noopStepFunc, row_numberName, {0} }
,
171291 WINDOWFUNCX(dense_rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, dense_rankStepFunc, dense_rankValueFunc
, dense_rankValueFunc, noopStepFunc, dense_rankName, {0} }
,
171292 WINDOWFUNCX(rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, rankStepFunc, rankValueFunc
, rankValueFunc, noopStepFunc, rankName, {0} }
,
171293 WINDOWFUNCALL(percent_rank, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, percent_rankStepFunc,
percent_rankValueFunc, percent_rankValueFunc, percent_rankInvFunc
, percent_rankName, {0} }
,
171294 WINDOWFUNCALL(cume_dist, 0, 0){ 0, (0x00800000|1|0x00010000|0), 0, 0, cume_distStepFunc, cume_distValueFunc
, cume_distValueFunc, cume_distInvFunc, cume_distName, {0} }
,
171295 WINDOWFUNCALL(ntile, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, ntileStepFunc, ntileValueFunc
, ntileValueFunc, ntileInvFunc, ntileName, {0} }
,
171296 WINDOWFUNCALL(last_value, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, last_valueStepFunc, last_valueFinalizeFunc
, last_valueValueFunc, last_valueInvFunc, last_valueName, {0}
}
,
171297 WINDOWFUNCALL(nth_value, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, nth_valueStepFunc, nth_valueFinalizeFunc
, noopValueFunc, noopStepFunc, nth_valueName, {0} }
,
171298 WINDOWFUNCALL(first_value, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, first_valueStepFunc, first_valueFinalizeFunc
, noopValueFunc, noopStepFunc, first_valueName, {0} }
,
171299 WINDOWFUNCNOOP(lead, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
171300 WINDOWFUNCNOOP(lead, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
171301 WINDOWFUNCNOOP(lead, 3, 0){ 3, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, leadName, {0} }
,
171302 WINDOWFUNCNOOP(lag, 1, 0){ 1, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
171303 WINDOWFUNCNOOP(lag, 2, 0){ 2, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
171304 WINDOWFUNCNOOP(lag, 3, 0){ 3, (0x00800000|1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, lagName, {0} }
,
171305 };
171306 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)((int)(sizeof(aWindowFuncs)/sizeof(aWindowFuncs[0]))));
171307}
171308
171309static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
171310 Window *p;
171311 for(p=pList; p; p=p->pNextWin){
171312 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
171313 }
171314 if( p==0 ){
171315 sqlite3ErrorMsg(pParse, "no such window: %s", zName);
171316 }
171317 return p;
171318}
171319
171320/*
171321** This function is called immediately after resolving the function name
171322** for a window function within a SELECT statement. Argument pList is a
171323** linked list of WINDOW definitions for the current SELECT statement.
171324** Argument pFunc is the function definition just resolved and pWin
171325** is the Window object representing the associated OVER clause. This
171326** function updates the contents of pWin as follows:
171327**
171328** * If the OVER clause referred to a named window (as in "max(x) OVER win"),
171329** search list pList for a matching WINDOW definition, and update pWin
171330** accordingly. If no such WINDOW clause can be found, leave an error
171331** in pParse.
171332**
171333** * If the function is a built-in window function that requires the
171334** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
171335** of this file), pWin is updated here.
171336*/
171337SQLITE_PRIVATEstatic void sqlite3WindowUpdate(
171338 Parse *pParse,
171339 Window *pList, /* List of named windows for this SELECT */
171340 Window *pWin, /* Window frame to update */
171341 FuncDef *pFunc /* Window function definition */
171342){
171343 if( pWin->zName && pWin->eFrmType==0 ){
171344 Window *p = windowFind(pParse, pList, pWin->zName);
171345 if( p==0 ) return;
171346 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
171347 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
171348 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
171349 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
171350 pWin->eStart = p->eStart;
171351 pWin->eEnd = p->eEnd;
171352 pWin->eFrmType = p->eFrmType;
171353 pWin->eExclude = p->eExclude;
171354 }else{
171355 sqlite3WindowChain(pParse, pWin, pList);
171356 }
171357 if( (pWin->eFrmType==TK_RANGE90)
171358 && (pWin->pStart || pWin->pEnd)
171359 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
171360 ){
171361 sqlite3ErrorMsg(pParse,
171362 "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
171363 );
171364 }else
171365 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW0x00010000 ){
171366 sqlite3 *db = pParse->db;
171367 if( pWin->pFilter ){
171368 sqlite3ErrorMsg(pParse,
171369 "FILTER clause may only be used with aggregate window functions"
171370 );
171371 }else{
171372 struct WindowUpdate {
171373 const char *zFunc;
171374 int eFrmType;
171375 int eStart;
171376 int eEnd;
171377 } aUp[] = {
171378 { row_numberName, TK_ROWS77, TK_UNBOUNDED91, TK_CURRENT86 },
171379 { dense_rankName, TK_RANGE90, TK_UNBOUNDED91, TK_CURRENT86 },
171380 { rankName, TK_RANGE90, TK_UNBOUNDED91, TK_CURRENT86 },
171381 { percent_rankName, TK_GROUPS93, TK_CURRENT86, TK_UNBOUNDED91 },
171382 { cume_distName, TK_GROUPS93, TK_FOLLOWING87, TK_UNBOUNDED91 },
171383 { ntileName, TK_ROWS77, TK_CURRENT86, TK_UNBOUNDED91 },
171384 { leadName, TK_ROWS77, TK_UNBOUNDED91, TK_UNBOUNDED91 },
171385 { lagName, TK_ROWS77, TK_UNBOUNDED91, TK_CURRENT86 },
171386 };
171387 int i;
171388 for(i=0; i<ArraySize(aUp)((int)(sizeof(aUp)/sizeof(aUp[0]))); i++){
171389 if( pFunc->zName==aUp[i].zFunc ){
171390 sqlite3ExprDelete(db, pWin->pStart);
171391 sqlite3ExprDelete(db, pWin->pEnd);
171392 pWin->pEnd = pWin->pStart = 0;
171393 pWin->eFrmType = aUp[i].eFrmType;
171394 pWin->eStart = aUp[i].eStart;
171395 pWin->eEnd = aUp[i].eEnd;
171396 pWin->eExclude = 0;
171397 if( pWin->eStart==TK_FOLLOWING87 ){
171398 pWin->pStart = sqlite3Expr(db, TK_INTEGER156, "1");
171399 }
171400 break;
171401 }
171402 }
171403 }
171404 }
171405 pWin->pWFunc = pFunc;
171406}
171407
171408/*
171409** Context object passed through sqlite3WalkExprList() to
171410** selectWindowRewriteExprCb() by selectWindowRewriteEList().
171411*/
171412typedef struct WindowRewrite WindowRewrite;
171413struct WindowRewrite {
171414 Window *pWin;
171415 SrcList *pSrc;
171416 ExprList *pSub;
171417 Table *pTab;
171418 Select *pSubSelect; /* Current sub-select, if any */
171419};
171420
171421/*
171422** Callback function used by selectWindowRewriteEList(). If necessary,
171423** this function appends to the output expression-list and updates
171424** expression (*ppExpr) in place.
171425*/
171426static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
171427 struct WindowRewrite *p = pWalker->u.pRewrite;
171428 Parse *pParse = pWalker->pParse;
171429 assert( p!=0 )((void) (0));
171430 assert( p->pWin!=0 )((void) (0));
171431
171432 /* If this function is being called from within a scalar sub-select
171433 ** that used by the SELECT statement being processed, only process
171434 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
171435 ** not process aggregates or window functions at all, as they belong
171436 ** to the scalar sub-select. */
171437 if( p->pSubSelect ){
171438 if( pExpr->op!=TK_COLUMN168 ){
171439 return WRC_Continue0;
171440 }else{
171441 int nSrc = p->pSrc->nSrc;
171442 int i;
171443 for(i=0; i<nSrc; i++){
171444 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
171445 }
171446 if( i==nSrc ) return WRC_Continue0;
171447 }
171448 }
171449
171450 switch( pExpr->op ){
171451
171452 case TK_FUNCTION172:
171453 if( !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
171454 break;
171455 }else{
171456 Window *pWin;
171457 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
171458 if( pExpr->y.pWin==pWin ){
171459 assert( pWin->pOwner==pExpr )((void) (0));
171460 return WRC_Prune1;
171461 }
171462 }
171463 }
171464 /* no break */ deliberate_fall_through__attribute__((fallthrough));
171465
171466 case TK_IF_NULL_ROW179:
171467 case TK_AGG_FUNCTION169:
171468 case TK_COLUMN168: {
171469 int iCol = -1;
171470 if( pParse->db->mallocFailed ) return WRC_Abort2;
171471 if( p->pSub ){
171472 int i;
171473 for(i=0; i<p->pSub->nExpr; i++){
171474 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
171475 iCol = i;
171476 break;
171477 }
171478 }
171479 }
171480 if( iCol<0 ){
171481 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
171482 if( pDup && pDup->op==TK_AGG_FUNCTION169 ) pDup->op = TK_FUNCTION172;
171483 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
171484 }
171485 if( p->pSub ){
171486 int f = pExpr->flags & EP_Collate0x000200;
171487 assert( ExprHasProperty(pExpr, EP_Static)==0 )((void) (0));
171488 ExprSetProperty(pExpr, EP_Static)(pExpr)->flags|=(0x8000000);
171489 sqlite3ExprDelete(pParse->db, pExpr);
171490 ExprClearProperty(pExpr, EP_Static)(pExpr)->flags&=~(0x8000000);
171491 memset(pExpr, 0, sizeof(Expr));
171492
171493 pExpr->op = TK_COLUMN168;
171494 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
171495 pExpr->iTable = p->pWin->iEphCsr;
171496 pExpr->y.pTab = p->pTab;
171497 pExpr->flags = f;
171498 }
171499 if( pParse->db->mallocFailed ) return WRC_Abort2;
171500 break;
171501 }
171502
171503 default: /* no-op */
171504 break;
171505 }
171506
171507 return WRC_Continue0;
171508}
171509static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
171510 struct WindowRewrite *p = pWalker->u.pRewrite;
171511 Select *pSave = p->pSubSelect;
171512 if( pSave==pSelect ){
171513 return WRC_Continue0;
171514 }else{
171515 p->pSubSelect = pSelect;
171516 sqlite3WalkSelect(pWalker, pSelect);
171517 p->pSubSelect = pSave;
171518 }
171519 return WRC_Prune1;
171520}
171521
171522
171523/*
171524** Iterate through each expression in expression-list pEList. For each:
171525**
171526** * TK_COLUMN,
171527** * aggregate function, or
171528** * window function with a Window object that is not a member of the
171529** Window list passed as the second argument (pWin).
171530**
171531** Append the node to output expression-list (*ppSub). And replace it
171532** with a TK_COLUMN that reads the (N-1)th element of table
171533** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
171534** appending the new one.
171535*/
171536static void selectWindowRewriteEList(
171537 Parse *pParse,
171538 Window *pWin,
171539 SrcList *pSrc,
171540 ExprList *pEList, /* Rewrite expressions in this list */
171541 Table *pTab,
171542 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
171543){
171544 Walker sWalker;
171545 WindowRewrite sRewrite;
171546
171547 assert( pWin!=0 )((void) (0));
171548 memset(&sWalker, 0, sizeof(Walker));
171549 memset(&sRewrite, 0, sizeof(WindowRewrite));
171550
171551 sRewrite.pSub = *ppSub;
171552 sRewrite.pWin = pWin;
171553 sRewrite.pSrc = pSrc;
171554 sRewrite.pTab = pTab;
171555
171556 sWalker.pParse = pParse;
171557 sWalker.xExprCallback = selectWindowRewriteExprCb;
171558 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
171559 sWalker.u.pRewrite = &sRewrite;
171560
171561 (void)sqlite3WalkExprList(&sWalker, pEList);
171562
171563 *ppSub = sRewrite.pSub;
171564}
171565
171566/*
171567** Append a copy of each expression in expression-list pAppend to
171568** expression list pList. Return a pointer to the result list.
171569*/
171570static ExprList *exprListAppendList(
171571 Parse *pParse, /* Parsing context */
171572 ExprList *pList, /* List to which to append. Might be NULL */
171573 ExprList *pAppend, /* List of values to append. Might be NULL */
171574 int bIntToNull
171575){
171576 if( pAppend ){
171577 int i;
171578 int nInit = pList ? pList->nExpr : 0;
171579 for(i=0; i<pAppend->nExpr; i++){
171580 sqlite3 *db = pParse->db;
171581 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
171582 if( db->mallocFailed ){
171583 sqlite3ExprDelete(db, pDup);
171584 break;
171585 }
171586 if( bIntToNull ){
171587 int iDummy;
171588 Expr *pSub;
171589 pSub = sqlite3ExprSkipCollateAndLikely(pDup);
171590 if( sqlite3ExprIsInteger(pSub, &iDummy, 0) ){
171591 pSub->op = TK_NULL122;
171592 pSub->flags &= ~(EP_IntValue0x000800|EP_IsTrue0x10000000|EP_IsFalse0x20000000);
171593 pSub->u.zToken = 0;
171594 }
171595 }
171596 pList = sqlite3ExprListAppend(pParse, pList, pDup);
171597 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
171598 }
171599 }
171600 return pList;
171601}
171602
171603/*
171604** When rewriting a query, if the new subquery in the FROM clause
171605** contains TK_AGG_FUNCTION nodes that refer to an outer query,
171606** then we have to increase the Expr->op2 values of those nodes
171607** due to the extra subquery layer that was added.
171608**
171609** See also the incrAggDepth() routine in resolve.c
171610*/
171611static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){
171612 if( pExpr->op==TK_AGG_FUNCTION169
171613 && pExpr->op2>=pWalker->walkerDepth
171614 ){
171615 pExpr->op2++;
171616 }
171617 return WRC_Continue0;
171618}
171619
171620static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
171621 if( pExpr->op==TK_AGG_FUNCTION169 && pExpr->pAggInfo==0 ){
171622 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
171623 sqlite3ErrorMsg(pWalker->pParse,
171624 "misuse of aggregate: %s()", pExpr->u.zToken);
171625 }
171626 return WRC_Continue0;
171627}
171628
171629/*
171630** If the SELECT statement passed as the second argument does not invoke
171631** any SQL window functions, this function is a no-op. Otherwise, it
171632** rewrites the SELECT statement so that window function xStep functions
171633** are invoked in the correct order as described under "SELECT REWRITING"
171634** at the top of this file.
171635*/
171636SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse *pParse, Select *p){
171637 int rc = SQLITE_OK0;
171638 if( p->pWin
171639 && p->pPrior==0
171640 && ALWAYS((p->selFlags & SF_WinRewrite)==0)((p->selFlags & 0x0100000)==0)
171641 && ALWAYS(!IN_RENAME_OBJECT)(!(pParse->eParseMode>=2))
171642 ){
171643 Vdbe *v = sqlite3GetVdbe(pParse);
171644 sqlite3 *db = pParse->db;
171645 Select *pSub = 0; /* The subquery */
171646 SrcList *pSrc = p->pSrc;
171647 Expr *pWhere = p->pWhere;
171648 ExprList *pGroupBy = p->pGroupBy;
171649 Expr *pHaving = p->pHaving;
171650 ExprList *pSort = 0;
171651
171652 ExprList *pSublist = 0; /* Expression list for sub-query */
171653 Window *pMWin = p->pWin; /* Main window object */
171654 Window *pWin; /* Window object iterator */
171655 Table *pTab;
171656 Walker w;
171657
171658 u32 selFlags = p->selFlags;
171659
171660 pTab = sqlite3DbMallocZero(db, sizeof(Table));
171661 if( pTab==0 ){
171662 return sqlite3ErrorToParser(db, SQLITE_NOMEM7);
171663 }
171664 sqlite3AggInfoPersistWalkerInit(&w, pParse);
171665 sqlite3WalkSelect(&w, p);
171666 if( (p->selFlags & SF_Aggregate0x0000008)==0 ){
171667 w.xExprCallback = disallowAggregatesInOrderByCb;
171668 w.xSelectCallback = 0;
171669 sqlite3WalkExprList(&w, p->pOrderBy);
171670 }
171671
171672 p->pSrc = 0;
171673 p->pWhere = 0;
171674 p->pGroupBy = 0;
171675 p->pHaving = 0;
171676 p->selFlags &= ~SF_Aggregate0x0000008;
171677 p->selFlags |= SF_WinRewrite0x0100000;
171678
171679 /* Create the ORDER BY clause for the sub-select. This is the concatenation
171680 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
171681 ** redundant, remove the ORDER BY from the parent SELECT. */
171682 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
171683 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
171684 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
171685 int nSave = pSort->nExpr;
171686 pSort->nExpr = p->pOrderBy->nExpr;
171687 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
171688 sqlite3ExprListDelete(db, p->pOrderBy);
171689 p->pOrderBy = 0;
171690 }
171691 pSort->nExpr = nSave;
171692 }
171693
171694 /* Assign a cursor number for the ephemeral table used to buffer rows.
171695 ** The OpenEphemeral instruction is coded later, after it is known how
171696 ** many columns the table will have. */
171697 pMWin->iEphCsr = pParse->nTab++;
171698 pParse->nTab += 3;
171699
171700 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
171701 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
171702 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
171703
171704 /* Append the PARTITION BY and ORDER BY expressions to the to the
171705 ** sub-select expression list. They are required to figure out where
171706 ** boundaries for partitions and sets of peer rows lie. */
171707 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
171708 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
171709
171710 /* Append the arguments passed to each window function to the
171711 ** sub-select expression list. Also allocate two registers for each
171712 ** window function - one for the accumulator, another for interim
171713 ** results. */
171714 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171715 ExprList *pArgs;
171716 assert( ExprUseXList(pWin->pOwner) )((void) (0));
171717 assert( pWin->pWFunc!=0 )((void) (0));
171718 pArgs = pWin->pOwner->x.pList;
171719 if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE0x000100000 ){
171720 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
171721 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
171722 pWin->bExprArgs = 1;
171723 }else{
171724 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
171725 pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
171726 }
171727 if( pWin->pFilter ){
171728 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
171729 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
171730 }
171731 pWin->regAccum = ++pParse->nMem;
171732 pWin->regResult = ++pParse->nMem;
171733 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
171734 }
171735
171736 /* If there is no ORDER BY or PARTITION BY clause, and the window
171737 ** function accepts zero arguments, and there are no other columns
171738 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
171739 ** that pSublist is still NULL here. Add a constant expression here to
171740 ** keep everything legal in this case.
171741 */
171742 if( pSublist==0 ){
171743 pSublist = sqlite3ExprListAppend(pParse, 0,
171744 sqlite3Expr(db, TK_INTEGER156, "0")
171745 );
171746 }
171747
171748 pSub = sqlite3SelectNew(
171749 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
171750 );
171751 TREETRACE(0x40,pParse,pSub,
171752 ("New window-function subquery in FROM clause of (%u/%p)\n",
171753 p->selId, p));
171754 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
171755 assert( pSub!=0 || p->pSrc==0 )((void) (0)); /* Due to db->mallocFailed test inside
171756 ** of sqlite3DbMallocRawNN() called from
171757 ** sqlite3SrcListAppend() */
171758 if( p->pSrc==0 ){
171759 sqlite3SelectDelete(db, pSub);
171760 }else if( sqlite3SrcItemAttachSubquery(pParse, &p->pSrc->a[0], pSub, 0) ){
171761 Table *pTab2;
171762 p->pSrc->a[0].fg.isCorrelated = 1;
171763 sqlite3SrcListAssignCursors(pParse, p->pSrc);
171764 pSub->selFlags |= SF_Expanded0x0000040|SF_OrderByReqd0x8000000;
171765 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE0x40);
171766 pSub->selFlags |= (selFlags & SF_Aggregate0x0000008);
171767 if( pTab2==0 ){
171768 /* Might actually be some other kind of error, but in that case
171769 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
171770 ** the correct error message regardless. */
171771 rc = SQLITE_NOMEM7;
171772 }else{
171773 memcpy(pTab, pTab2, sizeof(Table));
171774 pTab->tabFlags |= TF_Ephemeral0x00004000;
171775 p->pSrc->a[0].pSTab = pTab;
171776 pTab = pTab2;
171777 memset(&w, 0, sizeof(w));
171778 w.xExprCallback = sqlite3WindowExtraAggFuncDepth;
171779 w.xSelectCallback = sqlite3WalkerDepthIncrease;
171780 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
171781 sqlite3WalkSelect(&w, pSub);
171782 }
171783 }
171784 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
171785
171786 /* Defer deleting the temporary table pTab because if an error occurred,
171787 ** there could still be references to that table embedded in the
171788 ** result-set or ORDER BY clause of the SELECT statement p. */
171789 sqlite3ParserAddCleanup(pParse, sqlite3DbFree, pTab);
171790 }
171791
171792 assert( rc==SQLITE_OK || pParse->nErr!=0 )((void) (0));
171793 return rc;
171794}
171795
171796/*
171797** Unlink the Window object from the Select to which it is attached,
171798** if it is attached.
171799*/
171800SQLITE_PRIVATEstatic void sqlite3WindowUnlinkFromSelect(Window *p){
171801 if( p->ppThis ){
171802 *p->ppThis = p->pNextWin;
171803 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
171804 p->ppThis = 0;
171805 }
171806}
171807
171808/*
171809** Free the Window object passed as the second argument.
171810*/
171811SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3 *db, Window *p){
171812 if( p ){
171813 sqlite3WindowUnlinkFromSelect(p);
171814 sqlite3ExprDelete(db, p->pFilter);
171815 sqlite3ExprListDelete(db, p->pPartition);
171816 sqlite3ExprListDelete(db, p->pOrderBy);
171817 sqlite3ExprDelete(db, p->pEnd);
171818 sqlite3ExprDelete(db, p->pStart);
171819 sqlite3DbFree(db, p->zName);
171820 sqlite3DbFree(db, p->zBase);
171821 sqlite3DbFree(db, p);
171822 }
171823}
171824
171825/*
171826** Free the linked list of Window objects starting at the second argument.
171827*/
171828SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p){
171829 while( p ){
171830 Window *pNext = p->pNextWin;
171831 sqlite3WindowDelete(db, p);
171832 p = pNext;
171833 }
171834}
171835
171836/*
171837** The argument expression is an PRECEDING or FOLLOWING offset. The
171838** value should be a non-negative integer. If the value is not a
171839** constant, change it to NULL. The fact that it is then a non-negative
171840** integer will be caught later. But it is important not to leave
171841** variable values in the expression tree.
171842*/
171843static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
171844 if( 0==sqlite3ExprIsConstant(0,pExpr) ){
171845 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameExprUnmap(pParse, pExpr);
171846 sqlite3ExprDelete(pParse->db, pExpr);
171847 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL122, 0, 0);
171848 }
171849 return pExpr;
171850}
171851
171852/*
171853** Allocate and return a new Window object describing a Window Definition.
171854*/
171855SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(
171856 Parse *pParse, /* Parsing context */
171857 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
171858 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
171859 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
171860 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
171861 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
171862 u8 eExclude /* EXCLUDE clause */
171863){
171864 Window *pWin = 0;
171865 int bImplicitFrame = 0;
171866
171867 /* Parser assures the following: */
171868 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS )((void) (0));
171869 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING((void) (0))
171870 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING )((void) (0));
171871 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING((void) (0))
171872 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING )((void) (0));
171873 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) )((void) (0));
171874 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) )((void) (0));
171875
171876 if( eType==0 ){
171877 bImplicitFrame = 1;
171878 eType = TK_RANGE90;
171879 }
171880
171881 /* Additionally, the
171882 ** starting boundary type may not occur earlier in the following list than
171883 ** the ending boundary type:
171884 **
171885 ** UNBOUNDED PRECEDING
171886 ** <expr> PRECEDING
171887 ** CURRENT ROW
171888 ** <expr> FOLLOWING
171889 ** UNBOUNDED FOLLOWING
171890 **
171891 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
171892 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
171893 ** frame boundary.
171894 */
171895 if( (eStart==TK_CURRENT86 && eEnd==TK_PRECEDING89)
171896 || (eStart==TK_FOLLOWING87 && (eEnd==TK_PRECEDING89 || eEnd==TK_CURRENT86))
171897 ){
171898 sqlite3ErrorMsg(pParse, "unsupported frame specification");
171899 goto windowAllocErr;
171900 }
171901
171902 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
171903 if( pWin==0 ) goto windowAllocErr;
171904 pWin->eFrmType = eType;
171905 pWin->eStart = eStart;
171906 pWin->eEnd = eEnd;
171907 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc)(((pParse->db)->dbOptFlags&(0x00000002))!=0) ){
171908 eExclude = TK_NO67;
171909 }
171910 pWin->eExclude = eExclude;
171911 pWin->bImplicitFrame = bImplicitFrame;
171912 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
171913 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
171914 return pWin;
171915
171916windowAllocErr:
171917 sqlite3ExprDelete(pParse->db, pEnd);
171918 sqlite3ExprDelete(pParse->db, pStart);
171919 return 0;
171920}
171921
171922/*
171923** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
171924** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
171925** equivalent nul-terminated string.
171926*/
171927SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(
171928 Parse *pParse,
171929 Window *pWin,
171930 ExprList *pPartition,
171931 ExprList *pOrderBy,
171932 Token *pBase
171933){
171934 if( pWin ){
171935 pWin->pPartition = pPartition;
171936 pWin->pOrderBy = pOrderBy;
171937 if( pBase ){
171938 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
171939 }
171940 }else{
171941 sqlite3ExprListDelete(pParse->db, pPartition);
171942 sqlite3ExprListDelete(pParse->db, pOrderBy);
171943 }
171944 return pWin;
171945}
171946
171947/*
171948** Window *pWin has just been created from a WINDOW clause. Token pBase
171949** is the base window. Earlier windows from the same WINDOW clause are
171950** stored in the linked list starting at pWin->pNextWin. This function
171951** either updates *pWin according to the base specification, or else
171952** leaves an error in pParse.
171953*/
171954SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
171955 if( pWin->zBase ){
171956 sqlite3 *db = pParse->db;
171957 Window *pExist = windowFind(pParse, pList, pWin->zBase);
171958 if( pExist ){
171959 const char *zErr = 0;
171960 /* Check for errors */
171961 if( pWin->pPartition ){
171962 zErr = "PARTITION clause";
171963 }else if( pExist->pOrderBy && pWin->pOrderBy ){
171964 zErr = "ORDER BY clause";
171965 }else if( pExist->bImplicitFrame==0 ){
171966 zErr = "frame specification";
171967 }
171968 if( zErr ){
171969 sqlite3ErrorMsg(pParse,
171970 "cannot override %s of window: %s", zErr, pWin->zBase
171971 );
171972 }else{
171973 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
171974 if( pExist->pOrderBy ){
171975 assert( pWin->pOrderBy==0 )((void) (0));
171976 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
171977 }
171978 sqlite3DbFree(db, pWin->zBase);
171979 pWin->zBase = 0;
171980 }
171981 }
171982 }
171983}
171984
171985/*
171986** Attach window object pWin to expression p.
171987*/
171988SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
171989 if( p ){
171990 assert( p->op==TK_FUNCTION )((void) (0));
171991 assert( pWin )((void) (0));
171992 assert( ExprIsFullSize(p) )((void) (0));
171993 p->y.pWin = pWin;
171994 ExprSetProperty(p, EP_WinFunc|EP_FullSize)(p)->flags|=(0x1000000|0x020000);
171995 pWin->pOwner = p;
171996 if( (p->flags & EP_Distinct0x000004) && pWin->eFrmType!=TK_FILTER167 ){
171997 sqlite3ErrorMsg(pParse,
171998 "DISTINCT is not supported for window functions"
171999 );
172000 }
172001 }else{
172002 sqlite3WindowDelete(pParse->db, pWin);
172003 }
172004}
172005
172006/*
172007** Possibly link window pWin into the list at pSel->pWin (window functions
172008** to be processed as part of SELECT statement pSel). The window is linked
172009** in if either (a) there are no other windows already linked to this
172010** SELECT, or (b) the windows already linked use a compatible window frame.
172011*/
172012SQLITE_PRIVATEstatic void sqlite3WindowLink(Select *pSel, Window *pWin){
172013 if( pSel ){
172014 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
172015 pWin->pNextWin = pSel->pWin;
172016 if( pSel->pWin ){
172017 pSel->pWin->ppThis = &pWin->pNextWin;
172018 }
172019 pSel->pWin = pWin;
172020 pWin->ppThis = &pSel->pWin;
172021 }else{
172022 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
172023 pSel->selFlags |= SF_MultiPart0x2000000;
172024 }
172025 }
172026 }
172027}
172028
172029/*
172030** Return 0 if the two window objects are identical, 1 if they are
172031** different, or 2 if it cannot be determined if the objects are identical
172032** or not. Identical window objects can be processed in a single scan.
172033*/
172034SQLITE_PRIVATEstatic int sqlite3WindowCompare(
172035 const Parse *pParse,
172036 const Window *p1,
172037 const Window *p2,
172038 int bFilter
172039){
172040 int res;
172041 if( NEVER(p1==0)(p1==0) || NEVER(p2==0)(p2==0) ) return 1;
172042 if( p1->eFrmType!=p2->eFrmType ) return 1;
172043 if( p1->eStart!=p2->eStart ) return 1;
172044 if( p1->eEnd!=p2->eEnd ) return 1;
172045 if( p1->eExclude!=p2->eExclude ) return 1;
172046 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
172047 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
172048 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
172049 return res;
172050 }
172051 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
172052 return res;
172053 }
172054 if( bFilter ){
172055 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
172056 return res;
172057 }
172058 }
172059 return 0;
172060}
172061
172062
172063/*
172064** This is called by code in select.c before it calls sqlite3WhereBegin()
172065** to begin iterating through the sub-query results. It is used to allocate
172066** and initialize registers and cursors used by sqlite3WindowCodeStep().
172067*/
172068SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){
172069 Window *pWin;
172070 int nEphExpr;
172071 Window *pMWin;
172072 Vdbe *v;
172073
172074 assert( pSelect->pSrc->a[0].fg.isSubquery )((void) (0));
172075 nEphExpr = pSelect->pSrc->a[0].u4.pSubq->pSelect->pEList->nExpr;
172076 pMWin = pSelect->pWin;
172077 v = sqlite3GetVdbe(pParse);
172078
172079 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pMWin->iEphCsr, nEphExpr);
172080 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->iEphCsr+1, pMWin->iEphCsr);
172081 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->iEphCsr+2, pMWin->iEphCsr);
172082 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->iEphCsr+3, pMWin->iEphCsr);
172083
172084 /* Allocate registers to use for PARTITION BY values, if any. Initialize
172085 ** said registers to NULL. */
172086 if( pMWin->pPartition ){
172087 int nExpr = pMWin->pPartition->nExpr;
172088 pMWin->regPart = pParse->nMem+1;
172089 pParse->nMem += nExpr;
172090 sqlite3VdbeAddOp3(v, OP_Null75, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
172091 }
172092
172093 pMWin->regOne = ++pParse->nMem;
172094 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regOne);
172095
172096 if( pMWin->eExclude ){
172097 pMWin->regStartRowid = ++pParse->nMem;
172098 pMWin->regEndRowid = ++pParse->nMem;
172099 pMWin->csrApp = pParse->nTab++;
172100 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regStartRowid);
172101 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pMWin->regEndRowid);
172102 sqlite3VdbeAddOp2(v, OP_OpenDup114, pMWin->csrApp, pMWin->iEphCsr);
172103 return;
172104 }
172105
172106 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172107 FuncDef *p = pWin->pWFunc;
172108 if( (p->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->eStart!=TK_UNBOUNDED91 ){
172109 /* The inline versions of min() and max() require a single ephemeral
172110 ** table and 3 registers. The registers are used as follows:
172111 **
172112 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
172113 ** regApp+1: integer value used to ensure keys are unique
172114 ** regApp+2: output of MakeRecord
172115 */
172116 ExprList *pList;
172117 KeyInfo *pKeyInfo;
172118 assert( ExprUseXList(pWin->pOwner) )((void) (0));
172119 pList = pWin->pOwner->x.pList;
172120 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
172121 pWin->csrApp = pParse->nTab++;
172122 pWin->regApp = pParse->nMem+1;
172123 pParse->nMem += 3;
172124 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
172125 assert( pKeyInfo->aSortFlags[0]==0 )((void) (0));
172126 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC0x01;
172127 }
172128 sqlite3VdbeAddOp2(v, OP_OpenEphemeral117, pWin->csrApp, 2);
172129 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-8));
172130 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
172131 }
172132 else if( p->zName==nth_valueName || p->zName==first_valueName ){
172133 /* Allocate two registers at pWin->regApp. These will be used to
172134 ** store the start and end index of the current frame. */
172135 pWin->regApp = pParse->nMem+1;
172136 pWin->csrApp = pParse->nTab++;
172137 pParse->nMem += 2;
172138 sqlite3VdbeAddOp2(v, OP_OpenDup114, pWin->csrApp, pMWin->iEphCsr);
172139 }
172140 else if( p->zName==leadName || p->zName==lagName ){
172141 pWin->csrApp = pParse->nTab++;
172142 sqlite3VdbeAddOp2(v, OP_OpenDup114, pWin->csrApp, pMWin->iEphCsr);
172143 }
172144 }
172145}
172146
172147#define WINDOW_STARTING_INT0 0
172148#define WINDOW_ENDING_INT1 1
172149#define WINDOW_NTH_VALUE_INT2 2
172150#define WINDOW_STARTING_NUM3 3
172151#define WINDOW_ENDING_NUM4 4
172152
172153/*
172154** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
172155** value of the second argument to nth_value() (eCond==2) has just been
172156** evaluated and the result left in register reg. This function generates VM
172157** code to check that the value is a non-negative integer and throws an
172158** exception if it is not.
172159*/
172160static void windowCheckValue(Parse *pParse, int reg, int eCond){
172161 static const char *azErr[] = {
172162 "frame starting offset must be a non-negative integer",
172163 "frame ending offset must be a non-negative integer",
172164 "second argument to nth_value must be a positive integer",
172165 "frame starting offset must be a non-negative number",
172166 "frame ending offset must be a non-negative number",
172167 };
172168 static int aOp[] = { OP_Ge58, OP_Ge58, OP_Gt55, OP_Ge58, OP_Ge58 };
172169 Vdbe *v = sqlite3GetVdbe(pParse);
172170 int regZero = sqlite3GetTempReg(pParse);
172171 assert( eCond>=0 && eCond<ArraySize(azErr) )((void) (0));
172172 sqlite3VdbeAddOp2(v, OP_Integer71, 0, regZero);
172173 if( eCond>=WINDOW_STARTING_NUM3 ){
172174 int regString = sqlite3GetTempReg(pParse);
172175 sqlite3VdbeAddOp4(v, OP_String8118, 0, regString, 0, "", P4_STATIC(-1));
172176 sqlite3VdbeAddOp3(v, OP_Ge58, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
172177 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43|SQLITE_JUMPIFNULL0x10);
172178 VdbeCoverage(v);
172179 assert( eCond==3 || eCond==4 )((void) (0));
172180 VdbeCoverageIf(v, eCond==3);
172181 VdbeCoverageIf(v, eCond==4);
172182 }else{
172183 sqlite3VdbeAddOp2(v, OP_MustBeInt13, reg, sqlite3VdbeCurrentAddr(v)+2);
172184 VdbeCoverage(v);
172185 assert( eCond==0 || eCond==1 || eCond==2 )((void) (0));
172186 VdbeCoverageIf(v, eCond==0);
172187 VdbeCoverageIf(v, eCond==1);
172188 VdbeCoverageIf(v, eCond==2);
172189 }
172190 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
172191 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC0x43);
172192 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
172193 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
172194 VdbeCoverageNeverNullIf(v, eCond==2);
172195 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
172196 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
172197 sqlite3MayAbort(pParse);
172198 sqlite3VdbeAddOp2(v, OP_Halt70, SQLITE_ERROR1, OE_Abort2);
172199 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC(-1));
172200 sqlite3ReleaseTempReg(pParse, regZero);
172201}
172202
172203/*
172204** Return the number of arguments passed to the window-function associated
172205** with the object passed as the only argument to this function.
172206*/
172207static int windowArgCount(Window *pWin){
172208 const ExprList *pList;
172209 assert( ExprUseXList(pWin->pOwner) )((void) (0));
172210 pList = pWin->pOwner->x.pList;
172211 return (pList ? pList->nExpr : 0);
172212}
172213
172214typedef struct WindowCodeArg WindowCodeArg;
172215typedef struct WindowCsrAndReg WindowCsrAndReg;
172216
172217/*
172218** See comments above struct WindowCodeArg.
172219*/
172220struct WindowCsrAndReg {
172221 int csr; /* Cursor number */
172222 int reg; /* First in array of peer values */
172223};
172224
172225/*
172226** A single instance of this structure is allocated on the stack by
172227** sqlite3WindowCodeStep() and a pointer to it passed to the various helper
172228** routines. This is to reduce the number of arguments required by each
172229** helper function.
172230**
172231** regArg:
172232** Each window function requires an accumulator register (just as an
172233** ordinary aggregate function does). This variable is set to the first
172234** in an array of accumulator registers - one for each window function
172235** in the WindowCodeArg.pMWin list.
172236**
172237** eDelete:
172238** The window functions implementation sometimes caches the input rows
172239** that it processes in a temporary table. If it is not zero, this
172240** variable indicates when rows may be removed from the temp table (in
172241** order to reduce memory requirements - it would always be safe just
172242** to leave them there). Possible values for eDelete are:
172243**
172244** WINDOW_RETURN_ROW:
172245** An input row can be discarded after it is returned to the caller.
172246**
172247** WINDOW_AGGINVERSE:
172248** An input row can be discarded after the window functions xInverse()
172249** callbacks have been invoked in it.
172250**
172251** WINDOW_AGGSTEP:
172252** An input row can be discarded after the window functions xStep()
172253** callbacks have been invoked in it.
172254**
172255** start,current,end
172256** Consider a window-frame similar to the following:
172257**
172258** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING)
172259**
172260** The windows functions implementation caches the input rows in a temp
172261** table, sorted by "a, b" (it actually populates the cache lazily, and
172262** aggressively removes rows once they are no longer required, but that's
172263** a mere detail). It keeps three cursors open on the temp table. One
172264** (current) that points to the next row to return to the query engine
172265** once its window function values have been calculated. Another (end)
172266** points to the next row to call the xStep() method of each window function
172267** on (so that it is 2 groups ahead of current). And a third (start) that
172268** points to the next row to call the xInverse() method of each window
172269** function on.
172270**
172271** Each cursor (start, current and end) consists of a VDBE cursor
172272** (WindowCsrAndReg.csr) and an array of registers (starting at
172273** WindowCodeArg.reg) that always contains a copy of the peer values
172274** read from the corresponding cursor.
172275**
172276** Depending on the window-frame in question, all three cursors may not
172277** be required. In this case both WindowCodeArg.csr and reg are set to
172278** 0.
172279*/
172280struct WindowCodeArg {
172281 Parse *pParse; /* Parse context */
172282 Window *pMWin; /* First in list of functions being processed */
172283 Vdbe *pVdbe; /* VDBE object */
172284 int addrGosub; /* OP_Gosub to this address to return one row */
172285 int regGosub; /* Register used with OP_Gosub(addrGosub) */
172286 int regArg; /* First in array of accumulator registers */
172287 int eDelete; /* See above */
172288 int regRowid;
172289
172290 WindowCsrAndReg start;
172291 WindowCsrAndReg current;
172292 WindowCsrAndReg end;
172293};
172294
172295/*
172296** Generate VM code to read the window frames peer values from cursor csr into
172297** an array of registers starting at reg.
172298*/
172299static void windowReadPeerValues(
172300 WindowCodeArg *p,
172301 int csr,
172302 int reg
172303){
172304 Window *pMWin = p->pMWin;
172305 ExprList *pOrderBy = pMWin->pOrderBy;
172306 if( pOrderBy ){
172307 Vdbe *v = sqlite3GetVdbe(p->pParse);
172308 ExprList *pPart = pMWin->pPartition;
172309 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
172310 int i;
172311 for(i=0; i<pOrderBy->nExpr; i++){
172312 sqlite3VdbeAddOp3(v, OP_Column94, csr, iColOff+i, reg+i);
172313 }
172314 }
172315}
172316
172317/*
172318** Generate VM code to invoke either xStep() (if bInverse is 0) or
172319** xInverse (if bInverse is non-zero) for each window function in the
172320** linked list starting at pMWin. Or, for built-in window functions
172321** that do not use the standard function API, generate the required
172322** inline VM code.
172323**
172324** If argument csr is greater than or equal to 0, then argument reg is
172325** the first register in an array of registers guaranteed to be large
172326** enough to hold the array of arguments for each function. In this case
172327** the arguments are extracted from the current row of csr into the
172328** array of registers before invoking OP_AggStep or OP_AggInverse
172329**
172330** Or, if csr is less than zero, then the array of registers at reg is
172331** already populated with all columns from the current row of the sub-query.
172332**
172333** If argument regPartSize is non-zero, then it is a register containing the
172334** number of rows in the current partition.
172335*/
172336static void windowAggStep(
172337 WindowCodeArg *p,
172338 Window *pMWin, /* Linked list of window functions */
172339 int csr, /* Read arguments from this cursor */
172340 int bInverse, /* True to invoke xInverse instead of xStep */
172341 int reg /* Array of registers */
172342){
172343 Parse *pParse = p->pParse;
172344 Vdbe *v = sqlite3GetVdbe(pParse);
172345 Window *pWin;
172346 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172347 FuncDef *pFunc = pWin->pWFunc;
172348 int regArg;
172349 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
172350 int i;
172351
172352 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED )((void) (0));
172353
172354 /* All OVER clauses in the same window function aggregate step must
172355 ** be the same. */
172356 assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 )((void) (0));
172357
172358 for(i=0; i<nArg; i++){
172359 if( i!=1 || pFunc->zName!=nth_valueName ){
172360 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol+i, reg+i);
172361 }else{
172362 sqlite3VdbeAddOp3(v, OP_Column94, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
172363 }
172364 }
172365 regArg = reg;
172366
172367 if( pMWin->regStartRowid==0
172368 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
172369 && (pWin->eStart!=TK_UNBOUNDED91)
172370 ){
172371 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull51, regArg);
172372 VdbeCoverage(v);
172373 if( bInverse==0 ){
172374 sqlite3VdbeAddOp2(v, OP_AddImm86, pWin->regApp+1, 1);
172375 sqlite3VdbeAddOp2(v, OP_SCopy81, regArg, pWin->regApp);
172376 sqlite3VdbeAddOp3(v, OP_MakeRecord97, pWin->regApp, 2, pWin->regApp+2);
172377 sqlite3VdbeAddOp2(v, OP_IdxInsert138, pWin->csrApp, pWin->regApp+2);
172378 }else{
172379 sqlite3VdbeAddOp4Int(v, OP_SeekGE23, pWin->csrApp, 0, regArg, 1);
172380 VdbeCoverageNeverTaken(v);
172381 sqlite3VdbeAddOp1(v, OP_Delete130, pWin->csrApp);
172382 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
172383 }
172384 sqlite3VdbeJumpHere(v, addrIsNull);
172385 }else if( pWin->regApp ){
172386 assert( pFunc->zName==nth_valueName((void) (0))
172387 || pFunc->zName==first_valueName((void) (0))
172388 )((void) (0));
172389 assert( bInverse==0 || bInverse==1 )((void) (0));
172390 sqlite3VdbeAddOp2(v, OP_AddImm86, pWin->regApp+1-bInverse, 1);
172391 }else if( pFunc->xSFunc!=noopStepFunc ){
172392 int addrIf = 0;
172393 if( pWin->pFilter ){
172394 int regTmp;
172395 assert( ExprUseXList(pWin->pOwner) )((void) (0));
172396 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr )((void) (0));
172397 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 )((void) (0));
172398 regTmp = sqlite3GetTempReg(pParse);
172399 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol+nArg,regTmp);
172400 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot17, regTmp, 0, 1);
172401 VdbeCoverage(v);
172402 sqlite3ReleaseTempReg(pParse, regTmp);
172403 }
172404
172405 if( pWin->bExprArgs ){
172406 int iOp = sqlite3VdbeCurrentAddr(v);
172407 int iEnd;
172408
172409 assert( ExprUseXList(pWin->pOwner) )((void) (0));
172410 nArg = pWin->pOwner->x.pList->nExpr;
172411 regArg = sqlite3GetTempRange(pParse, nArg);
172412 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
172413
172414 for(iEnd=sqlite3VdbeCurrentAddr(v); iOp<iEnd; iOp++){
172415 VdbeOp *pOp = sqlite3VdbeGetOp(v, iOp);
172416 if( pOp->opcode==OP_Column94 && pOp->p1==pMWin->iEphCsr ){
172417 pOp->p1 = csr;
172418 }
172419 }
172420 }
172421 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
172422 CollSeq *pColl;
172423 assert( nArg>0 )((void) (0));
172424 assert( ExprUseXList(pWin->pOwner) )((void) (0));
172425 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
172426 sqlite3VdbeAddOp4(v, OP_CollSeq85, 0,0,0, (const char*)pColl, P4_COLLSEQ(-2));
172427 }
172428 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse161 : OP_AggStep162,
172429 bInverse, regArg, pWin->regAccum);
172430 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF(-7));
172431 sqlite3VdbeChangeP5(v, (u8)nArg);
172432 if( pWin->bExprArgs ){
172433 sqlite3ReleaseTempRange(pParse, regArg, nArg);
172434 }
172435 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
172436 }
172437 }
172438}
172439
172440/*
172441** Values that may be passed as the second argument to windowCodeOp().
172442*/
172443#define WINDOW_RETURN_ROW1 1
172444#define WINDOW_AGGINVERSE2 2
172445#define WINDOW_AGGSTEP3 3
172446
172447/*
172448** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
172449** (bFin==1) for each window function in the linked list starting at
172450** pMWin. Or, for built-in window-functions that do not use the standard
172451** API, generate the equivalent VM code.
172452*/
172453static void windowAggFinal(WindowCodeArg *p, int bFin){
172454 Parse *pParse = p->pParse;
172455 Window *pMWin = p->pMWin;
172456 Vdbe *v = sqlite3GetVdbe(pParse);
172457 Window *pWin;
172458
172459 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172460 if( pMWin->regStartRowid==0
172461 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
172462 && (pWin->eStart!=TK_UNBOUNDED91)
172463 ){
172464 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
172465 sqlite3VdbeAddOp1(v, OP_Last32, pWin->csrApp);
172466 VdbeCoverage(v);
172467 sqlite3VdbeAddOp3(v, OP_Column94, pWin->csrApp, 0, pWin->regResult);
172468 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
172469 }else if( pWin->regApp ){
172470 assert( pMWin->regStartRowid==0 )((void) (0));
172471 }else{
172472 int nArg = windowArgCount(pWin);
172473 if( bFin ){
172474 sqlite3VdbeAddOp2(v, OP_AggFinal165, pWin->regAccum, nArg);
172475 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF(-7));
172476 sqlite3VdbeAddOp2(v, OP_Copy80, pWin->regAccum, pWin->regResult);
172477 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
172478 }else{
172479 sqlite3VdbeAddOp3(v, OP_AggValue164,pWin->regAccum,nArg,pWin->regResult);
172480 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF(-7));
172481 }
172482 }
172483 }
172484}
172485
172486/*
172487** Generate code to calculate the current values of all window functions in the
172488** p->pMWin list by doing a full scan of the current window frame. Store the
172489** results in the Window.regResult registers, ready to return the upper
172490** layer.
172491*/
172492static void windowFullScan(WindowCodeArg *p){
172493 Window *pWin;
172494 Parse *pParse = p->pParse;
172495 Window *pMWin = p->pMWin;
172496 Vdbe *v = p->pVdbe;
172497
172498 int regCRowid = 0; /* Current rowid value */
172499 int regCPeer = 0; /* Current peer values */
172500 int regRowid = 0; /* AggStep rowid value */
172501 int regPeer = 0; /* AggStep peer values */
172502
172503 int nPeer;
172504 int lblNext;
172505 int lblBrk;
172506 int addrNext;
172507 int csr;
172508
172509 VdbeModuleComment((v, "windowFullScan begin"));
172510
172511 assert( pMWin!=0 )((void) (0));
172512 csr = pMWin->csrApp;
172513 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
172514
172515 lblNext = sqlite3VdbeMakeLabel(pParse);
172516 lblBrk = sqlite3VdbeMakeLabel(pParse);
172517
172518 regCRowid = sqlite3GetTempReg(pParse);
172519 regRowid = sqlite3GetTempReg(pParse);
172520 if( nPeer ){
172521 regCPeer = sqlite3GetTempRange(pParse, nPeer);
172522 regPeer = sqlite3GetTempRange(pParse, nPeer);
172523 }
172524
172525 sqlite3VdbeAddOp2(v, OP_Rowid135, pMWin->iEphCsr, regCRowid);
172526 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
172527
172528 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172529 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
172530 }
172531
172532 sqlite3VdbeAddOp3(v, OP_SeekGE23, csr, lblBrk, pMWin->regStartRowid);
172533 VdbeCoverage(v);
172534 addrNext = sqlite3VdbeCurrentAddr(v);
172535 sqlite3VdbeAddOp2(v, OP_Rowid135, csr, regRowid);
172536 sqlite3VdbeAddOp3(v, OP_Gt55, pMWin->regEndRowid, lblBrk, regRowid);
172537 VdbeCoverageNeverNull(v);
172538
172539 if( pMWin->eExclude==TK_CURRENT86 ){
172540 sqlite3VdbeAddOp3(v, OP_Eq54, regCRowid, lblNext, regRowid);
172541 VdbeCoverageNeverNull(v);
172542 }else if( pMWin->eExclude!=TK_NO67 ){
172543 int addr;
172544 int addrEq = 0;
172545 KeyInfo *pKeyInfo = 0;
172546
172547 if( pMWin->pOrderBy ){
172548 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
172549 }
172550 if( pMWin->eExclude==TK_TIES95 ){
172551 addrEq = sqlite3VdbeAddOp3(v, OP_Eq54, regCRowid, 0, regRowid);
172552 VdbeCoverageNeverNull(v);
172553 }
172554 if( pKeyInfo ){
172555 windowReadPeerValues(p, csr, regPeer);
172556 sqlite3VdbeAddOp3(v, OP_Compare90, regPeer, regCPeer, nPeer);
172557 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
172558 addr = sqlite3VdbeCurrentAddr(v)+1;
172559 sqlite3VdbeAddOp3(v, OP_Jump14, addr, lblNext, addr);
172560 VdbeCoverageEqNe(v);
172561 }else{
172562 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblNext);
172563 }
172564 if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
172565 }
172566
172567 windowAggStep(p, pMWin, csr, 0, p->regArg);
172568
172569 sqlite3VdbeResolveLabel(v, lblNext);
172570 sqlite3VdbeAddOp2(v, OP_Next39, csr, addrNext);
172571 VdbeCoverage(v);
172572 sqlite3VdbeJumpHere(v, addrNext-1);
172573 sqlite3VdbeJumpHere(v, addrNext+1);
172574 sqlite3ReleaseTempReg(pParse, regRowid);
172575 sqlite3ReleaseTempReg(pParse, regCRowid);
172576 if( nPeer ){
172577 sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
172578 sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
172579 }
172580
172581 windowAggFinal(p, 1);
172582 VdbeModuleComment((v, "windowFullScan end"));
172583}
172584
172585/*
172586** Invoke the sub-routine at regGosub (generated by code in select.c) to
172587** return the current row of Window.iEphCsr. If all window functions are
172588** aggregate window functions that use the standard API, a single
172589** OP_Gosub instruction is all that this routine generates. Extra VM code
172590** for per-row processing is only generated for the following built-in window
172591** functions:
172592**
172593** nth_value()
172594** first_value()
172595** lag()
172596** lead()
172597*/
172598static void windowReturnOneRow(WindowCodeArg *p){
172599 Window *pMWin = p->pMWin;
172600 Vdbe *v = p->pVdbe;
172601
172602 if( pMWin->regStartRowid ){
172603 windowFullScan(p);
172604 }else{
172605 Parse *pParse = p->pParse;
172606 Window *pWin;
172607
172608 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172609 FuncDef *pFunc = pWin->pWFunc;
172610 assert( ExprUseXList(pWin->pOwner) )((void) (0));
172611 if( pFunc->zName==nth_valueName
172612 || pFunc->zName==first_valueName
172613 ){
172614 int csr = pWin->csrApp;
172615 int lbl = sqlite3VdbeMakeLabel(pParse);
172616 int tmpReg = sqlite3GetTempReg(pParse);
172617 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
172618
172619 if( pFunc->zName==nth_valueName ){
172620 sqlite3VdbeAddOp3(v, OP_Column94,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
172621 windowCheckValue(pParse, tmpReg, 2);
172622 }else{
172623 sqlite3VdbeAddOp2(v, OP_Integer71, 1, tmpReg);
172624 }
172625 sqlite3VdbeAddOp3(v, OP_Add107, tmpReg, pWin->regApp, tmpReg);
172626 sqlite3VdbeAddOp3(v, OP_Gt55, pWin->regApp+1, lbl, tmpReg);
172627 VdbeCoverageNeverNull(v);
172628 sqlite3VdbeAddOp3(v, OP_SeekRowid30, csr, 0, tmpReg);
172629 VdbeCoverageNeverTaken(v);
172630 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol, pWin->regResult);
172631 sqlite3VdbeResolveLabel(v, lbl);
172632 sqlite3ReleaseTempReg(pParse, tmpReg);
172633 }
172634 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
172635 int nArg = pWin->pOwner->x.pList->nExpr;
172636 int csr = pWin->csrApp;
172637 int lbl = sqlite3VdbeMakeLabel(pParse);
172638 int tmpReg = sqlite3GetTempReg(pParse);
172639 int iEph = pMWin->iEphCsr;
172640
172641 if( nArg<3 ){
172642 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regResult);
172643 }else{
172644 sqlite3VdbeAddOp3(v, OP_Column94, iEph,pWin->iArgCol+2,pWin->regResult);
172645 }
172646 sqlite3VdbeAddOp2(v, OP_Rowid135, iEph, tmpReg);
172647 if( nArg<2 ){
172648 int val = (pFunc->zName==leadName ? 1 : -1);
172649 sqlite3VdbeAddOp2(v, OP_AddImm86, tmpReg, val);
172650 }else{
172651 int op = (pFunc->zName==leadName ? OP_Add107 : OP_Subtract108);
172652 int tmpReg2 = sqlite3GetTempReg(pParse);
172653 sqlite3VdbeAddOp3(v, OP_Column94, iEph, pWin->iArgCol+1, tmpReg2);
172654 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
172655 sqlite3ReleaseTempReg(pParse, tmpReg2);
172656 }
172657
172658 sqlite3VdbeAddOp3(v, OP_SeekRowid30, csr, lbl, tmpReg);
172659 VdbeCoverage(v);
172660 sqlite3VdbeAddOp3(v, OP_Column94, csr, pWin->iArgCol, pWin->regResult);
172661 sqlite3VdbeResolveLabel(v, lbl);
172662 sqlite3ReleaseTempReg(pParse, tmpReg);
172663 }
172664 }
172665 }
172666 sqlite3VdbeAddOp2(v, OP_Gosub10, p->regGosub, p->addrGosub);
172667}
172668
172669/*
172670** Generate code to set the accumulator register for each window function
172671** in the linked list passed as the second argument to NULL. And perform
172672** any equivalent initialization required by any built-in window functions
172673** in the list.
172674*/
172675static int windowInitAccum(Parse *pParse, Window *pMWin){
172676 Vdbe *v = sqlite3GetVdbe(pParse);
172677 int regArg;
172678 int nArg = 0;
172679 Window *pWin;
172680 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172681 FuncDef *pFunc = pWin->pWFunc;
172682 assert( pWin->regAccum )((void) (0));
172683 sqlite3VdbeAddOp2(v, OP_Null75, 0, pWin->regAccum);
172684 nArg = MAX(nArg, windowArgCount(pWin))((nArg)>(windowArgCount(pWin))?(nArg):(windowArgCount(pWin
)))
;
172685 if( pMWin->regStartRowid==0 ){
172686 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
172687 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp);
172688 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
172689 }
172690
172691 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->csrApp ){
172692 assert( pWin->eStart!=TK_UNBOUNDED )((void) (0));
172693 sqlite3VdbeAddOp1(v, OP_ResetSorter146, pWin->csrApp);
172694 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pWin->regApp+1);
172695 }
172696 }
172697 }
172698 regArg = pParse->nMem+1;
172699 pParse->nMem += nArg;
172700 return regArg;
172701}
172702
172703/*
172704** Return true if the current frame should be cached in the ephemeral table,
172705** even if there are no xInverse() calls required.
172706*/
172707static int windowCacheFrame(Window *pMWin){
172708 Window *pWin;
172709 if( pMWin->regStartRowid ) return 1;
172710 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172711 FuncDef *pFunc = pWin->pWFunc;
172712 if( (pFunc->zName==nth_valueName)
172713 || (pFunc->zName==first_valueName)
172714 || (pFunc->zName==leadName)
172715 || (pFunc->zName==lagName)
172716 ){
172717 return 1;
172718 }
172719 }
172720 return 0;
172721}
172722
172723/*
172724** regOld and regNew are each the first register in an array of size
172725** pOrderBy->nExpr. This function generates code to compare the two
172726** arrays of registers using the collation sequences and other comparison
172727** parameters specified by pOrderBy.
172728**
172729** If the two arrays are not equal, the contents of regNew is copied to
172730** regOld and control falls through. Otherwise, if the contents of the arrays
172731** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
172732*/
172733static void windowIfNewPeer(
172734 Parse *pParse,
172735 ExprList *pOrderBy,
172736 int regNew, /* First in array of new values */
172737 int regOld, /* First in array of old values */
172738 int addr /* Jump here */
172739){
172740 Vdbe *v = sqlite3GetVdbe(pParse);
172741 if( pOrderBy ){
172742 int nVal = pOrderBy->nExpr;
172743 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
172744 sqlite3VdbeAddOp3(v, OP_Compare90, regOld, regNew, nVal);
172745 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
172746 sqlite3VdbeAddOp3(v, OP_Jump14,
172747 sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
172748 );
172749 VdbeCoverageEqNe(v);
172750 sqlite3VdbeAddOp3(v, OP_Copy80, regNew, regOld, nVal-1);
172751 }else{
172752 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addr);
172753 }
172754}
172755
172756/*
172757** This function is called as part of generating VM programs for RANGE
172758** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
172759** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
172760** code equivalent to:
172761**
172762** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
172763**
172764** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
172765** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
172766**
172767** If the sort-order for the ORDER BY term in the window is DESC, then the
172768** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
172769** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
172770** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
172771** is OP_Ge, the generated code is equivalent to:
172772**
172773** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
172774**
172775** A special type of arithmetic is used such that if csr1.peerVal is not
172776** a numeric type (real or integer), then the result of the addition
172777** or subtraction is a a copy of csr1.peerVal.
172778*/
172779static void windowCodeRangeTest(
172780 WindowCodeArg *p,
172781 int op, /* OP_Ge, OP_Gt, or OP_Le */
172782 int csr1, /* Cursor number for cursor 1 */
172783 int regVal, /* Register containing non-negative number */
172784 int csr2, /* Cursor number for cursor 2 */
172785 int lbl /* Jump destination if condition is true */
172786){
172787 Parse *pParse = p->pParse;
172788 Vdbe *v = sqlite3GetVdbe(pParse);
172789 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
172790 int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */
172791 int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */
172792 int regString = ++pParse->nMem; /* Reg. for constant value '' */
172793 int arith = OP_Add107; /* OP_Add or OP_Subtract */
172794 int addrGe; /* Jump destination */
172795 int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */
172796 CollSeq *pColl;
172797
172798 /* Read the peer-value from each cursor into a register */
172799 windowReadPeerValues(p, csr1, reg1);
172800 windowReadPeerValues(p, csr2, reg2);
172801
172802 assert( op==OP_Ge || op==OP_Gt || op==OP_Le )((void) (0));
172803 assert( pOrderBy && pOrderBy->nExpr==1 )((void) (0));
172804 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC0x01 ){
172805 switch( op ){
172806 case OP_Ge58: op = OP_Le56; break;
172807 case OP_Gt55: op = OP_Lt57; break;
172808 default: assert( op==OP_Le )((void) (0)); op = OP_Ge58; break;
172809 }
172810 arith = OP_Subtract108;
172811 }
172812
172813 VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl",
172814 reg1, (arith==OP_Add ? "+" : "-"), regVal,
172815 ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2
172816 ));
172817
172818 /* If the BIGNULL flag is set for the ORDER BY, then it is required to
172819 ** consider NULL values to be larger than all other values, instead of
172820 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
172821 ** (and adding that capability causes a performance regression), so
172822 ** instead if the BIGNULL flag is set then cases where either reg1 or
172823 ** reg2 are NULL are handled separately in the following block. The code
172824 ** generated is equivalent to:
172825 **
172826 ** if( reg1 IS NULL ){
172827 ** if( op==OP_Ge ) goto lbl;
172828 ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
172829 ** if( op==OP_Le && reg2 IS NULL ) goto lbl;
172830 ** }else if( reg2 IS NULL ){
172831 ** if( op==OP_Le ) goto lbl;
172832 ** }
172833 **
172834 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
172835 ** not taken, control jumps over the comparison operator coded below this
172836 ** block. */
172837 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL0x02 ){
172838 /* This block runs if reg1 contains a NULL. */
172839 int addr = sqlite3VdbeAddOp1(v, OP_NotNull52, reg1); VdbeCoverage(v);
172840 switch( op ){
172841 case OP_Ge58:
172842 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lbl);
172843 break;
172844 case OP_Gt55:
172845 sqlite3VdbeAddOp2(v, OP_NotNull52, reg2, lbl);
172846 VdbeCoverage(v);
172847 break;
172848 case OP_Le56:
172849 sqlite3VdbeAddOp2(v, OP_IsNull51, reg2, lbl);
172850 VdbeCoverage(v);
172851 break;
172852 default: assert( op==OP_Lt )((void) (0)); /* no-op */ break;
172853 }
172854 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrDone);
172855
172856 /* This block runs if reg1 is not NULL, but reg2 is. */
172857 sqlite3VdbeJumpHere(v, addr);
172858 sqlite3VdbeAddOp2(v, OP_IsNull51, reg2,
172859 (op==OP_Gt55 || op==OP_Ge58) ? addrDone : lbl);
172860 VdbeCoverage(v);
172861 }
172862
172863 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
172864 ** This block adds (or subtracts for DESC) the numeric value in regVal
172865 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
172866 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
172867 **
172868 ** if( reg1>='' ) goto addrGe;
172869 ** reg1 = reg1 +/- regVal
172870 ** addrGe:
172871 **
172872 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
172873 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
172874 ** then the arithmetic is performed, but since adding or subtracting from
172875 ** NULL is always NULL anyway, this case is handled as required too. */
172876 sqlite3VdbeAddOp4(v, OP_String8118, 0, regString, 0, "", P4_STATIC(-1));
172877 addrGe = sqlite3VdbeAddOp3(v, OP_Ge58, regString, 0, reg1);
172878 VdbeCoverage(v);
172879 if( (op==OP_Ge58 && arith==OP_Add107) || (op==OP_Le56 && arith==OP_Subtract108) ){
172880 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
172881 }
172882 sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
172883 sqlite3VdbeJumpHere(v, addrGe);
172884
172885 /* Compare registers reg2 and reg1, taking the jump if required. Note that
172886 ** control skips over this test if the BIGNULL flag is set and either
172887 ** reg1 or reg2 contain a NULL value. */
172888 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
172889 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
172890 sqlite3VdbeAppendP4(v, (void*)pColl, P4_COLLSEQ(-2));
172891 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
172892 sqlite3VdbeResolveLabel(v, addrDone);
172893
172894 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le )((void) (0));
172895 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
172896 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
172897 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
172898 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
172899 sqlite3ReleaseTempReg(pParse, reg1);
172900 sqlite3ReleaseTempReg(pParse, reg2);
172901
172902 VdbeModuleComment((v, "CodeRangeTest: end"));
172903}
172904
172905/*
172906** Helper function for sqlite3WindowCodeStep(). Each call to this function
172907** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
172908** operation. Refer to the header comment for sqlite3WindowCodeStep() for
172909** details.
172910*/
172911static int windowCodeOp(
172912 WindowCodeArg *p, /* Context object */
172913 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
172914 int regCountdown, /* Register for OP_IfPos countdown */
172915 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
172916){
172917 int csr, reg;
172918 Parse *pParse = p->pParse;
172919 Window *pMWin = p->pMWin;
172920 int ret = 0;
172921 Vdbe *v = p->pVdbe;
172922 int addrContinue = 0;
172923 int bPeer = (pMWin->eFrmType!=TK_ROWS77);
172924
172925 int lblDone = sqlite3VdbeMakeLabel(pParse);
172926 int addrNextRange = 0;
172927
172928 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
172929 ** starts with UNBOUNDED PRECEDING. */
172930 if( op==WINDOW_AGGINVERSE2 && pMWin->eStart==TK_UNBOUNDED91 ){
172931 assert( regCountdown==0 && jumpOnEof==0 )((void) (0));
172932 return 0;
172933 }
172934
172935 if( regCountdown>0 ){
172936 if( pMWin->eFrmType==TK_RANGE90 ){
172937 addrNextRange = sqlite3VdbeCurrentAddr(v);
172938 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP )((void) (0));
172939 if( op==WINDOW_AGGINVERSE2 ){
172940 if( pMWin->eStart==TK_FOLLOWING87 ){
172941 windowCodeRangeTest(
172942 p, OP_Le56, p->current.csr, regCountdown, p->start.csr, lblDone
172943 );
172944 }else{
172945 windowCodeRangeTest(
172946 p, OP_Ge58, p->start.csr, regCountdown, p->current.csr, lblDone
172947 );
172948 }
172949 }else{
172950 windowCodeRangeTest(
172951 p, OP_Gt55, p->end.csr, regCountdown, p->current.csr, lblDone
172952 );
172953 }
172954 }else{
172955 sqlite3VdbeAddOp3(v, OP_IfPos50, regCountdown, lblDone, 1);
172956 VdbeCoverage(v);
172957 }
172958 }
172959
172960 if( op==WINDOW_RETURN_ROW1 && pMWin->regStartRowid==0 ){
172961 windowAggFinal(p, 0);
172962 }
172963 addrContinue = sqlite3VdbeCurrentAddr(v);
172964
172965 /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or
172966 ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the
172967 ** start cursor does not advance past the end cursor within the
172968 ** temporary table. It otherwise might, if (a>b). Also ensure that,
172969 ** if the input cursor is still finding new rows, that the end
172970 ** cursor does not go past it to EOF. */
172971 if( pMWin->eStart==pMWin->eEnd && regCountdown
172972 && pMWin->eFrmType==TK_RANGE90
172973 ){
172974 int regRowid1 = sqlite3GetTempReg(pParse);
172975 int regRowid2 = sqlite3GetTempReg(pParse);
172976 if( op==WINDOW_AGGINVERSE2 ){
172977 sqlite3VdbeAddOp2(v, OP_Rowid135, p->start.csr, regRowid1);
172978 sqlite3VdbeAddOp2(v, OP_Rowid135, p->end.csr, regRowid2);
172979 sqlite3VdbeAddOp3(v, OP_Ge58, regRowid2, lblDone, regRowid1);
172980 VdbeCoverage(v);
172981 }else if( p->regRowid ){
172982 sqlite3VdbeAddOp2(v, OP_Rowid135, p->end.csr, regRowid1);
172983 sqlite3VdbeAddOp3(v, OP_Ge58, p->regRowid, lblDone, regRowid1);
172984 VdbeCoverageNeverNull(v);
172985 }
172986 sqlite3ReleaseTempReg(pParse, regRowid1);
172987 sqlite3ReleaseTempReg(pParse, regRowid2);
172988 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING )((void) (0));
172989 }
172990
172991 switch( op ){
172992 case WINDOW_RETURN_ROW1:
172993 csr = p->current.csr;
172994 reg = p->current.reg;
172995 windowReturnOneRow(p);
172996 break;
172997
172998 case WINDOW_AGGINVERSE2:
172999 csr = p->start.csr;
173000 reg = p->start.reg;
173001 if( pMWin->regStartRowid ){
173002 assert( pMWin->regEndRowid )((void) (0));
173003 sqlite3VdbeAddOp2(v, OP_AddImm86, pMWin->regStartRowid, 1);
173004 }else{
173005 windowAggStep(p, pMWin, csr, 1, p->regArg);
173006 }
173007 break;
173008
173009 default:
173010 assert( op==WINDOW_AGGSTEP )((void) (0));
173011 csr = p->end.csr;
173012 reg = p->end.reg;
173013 if( pMWin->regStartRowid ){
173014 assert( pMWin->regEndRowid )((void) (0));
173015 sqlite3VdbeAddOp2(v, OP_AddImm86, pMWin->regEndRowid, 1);
173016 }else{
173017 windowAggStep(p, pMWin, csr, 0, p->regArg);
173018 }
173019 break;
173020 }
173021
173022 if( op==p->eDelete ){
173023 sqlite3VdbeAddOp1(v, OP_Delete130, csr);
173024 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
173025 }
173026
173027 if( jumpOnEof ){
173028 sqlite3VdbeAddOp2(v, OP_Next39, csr, sqlite3VdbeCurrentAddr(v)+2);
173029 VdbeCoverage(v);
173030 ret = sqlite3VdbeAddOp0(v, OP_Goto9);
173031 }else{
173032 sqlite3VdbeAddOp2(v, OP_Next39, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
173033 VdbeCoverage(v);
173034 if( bPeer ){
173035 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblDone);
173036 }
173037 }
173038
173039 if( bPeer ){
173040 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
173041 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
173042 windowReadPeerValues(p, csr, regTmp);
173043 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
173044 sqlite3ReleaseTempRange(pParse, regTmp, nReg);
173045 }
173046
173047 if( addrNextRange ){
173048 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrNextRange);
173049 }
173050 sqlite3VdbeResolveLabel(v, lblDone);
173051 return ret;
173052}
173053
173054
173055/*
173056** Allocate and return a duplicate of the Window object indicated by the
173057** third argument. Set the Window.pOwner field of the new object to
173058** pOwner.
173059*/
173060SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
173061 Window *pNew = 0;
173062 if( ALWAYS(p)(p) ){
173063 pNew = sqlite3DbMallocZero(db, sizeof(Window));
173064 if( pNew ){
173065 pNew->zName = sqlite3DbStrDup(db, p->zName);
173066 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
173067 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
173068 pNew->pWFunc = p->pWFunc;
173069 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
173070 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
173071 pNew->eFrmType = p->eFrmType;
173072 pNew->eEnd = p->eEnd;
173073 pNew->eStart = p->eStart;
173074 pNew->eExclude = p->eExclude;
173075 pNew->regResult = p->regResult;
173076 pNew->regAccum = p->regAccum;
173077 pNew->iArgCol = p->iArgCol;
173078 pNew->iEphCsr = p->iEphCsr;
173079 pNew->bExprArgs = p->bExprArgs;
173080 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
173081 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
173082 pNew->pOwner = pOwner;
173083 pNew->bImplicitFrame = p->bImplicitFrame;
173084 }
173085 }
173086 return pNew;
173087}
173088
173089/*
173090** Return a copy of the linked list of Window objects passed as the
173091** second argument.
173092*/
173093SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
173094 Window *pWin;
173095 Window *pRet = 0;
173096 Window **pp = &pRet;
173097
173098 for(pWin=p; pWin; pWin=pWin->pNextWin){
173099 *pp = sqlite3WindowDup(db, 0, pWin);
173100 if( *pp==0 ) break;
173101 pp = &((*pp)->pNextWin);
173102 }
173103
173104 return pRet;
173105}
173106
173107/*
173108** Return true if it can be determined at compile time that expression
173109** pExpr evaluates to a value that, when cast to an integer, is greater
173110** than zero. False otherwise.
173111**
173112** If an OOM error occurs, this function sets the Parse.db.mallocFailed
173113** flag and returns zero.
173114*/
173115static int windowExprGtZero(Parse *pParse, Expr *pExpr){
173116 int ret = 0;
173117 sqlite3 *db = pParse->db;
173118 sqlite3_value *pVal = 0;
173119 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC0x43, &pVal);
173120 if( pVal && sqlite3_value_int(pVal)>0 ){
173121 ret = 1;
173122 }
173123 sqlite3ValueFree(pVal);
173124 return ret;
173125}
173126
173127/*
173128** sqlite3WhereBegin() has already been called for the SELECT statement
173129** passed as the second argument when this function is invoked. It generates
173130** code to populate the Window.regResult register for each window function
173131** and invoke the sub-routine at instruction addrGosub once for each row.
173132** sqlite3WhereEnd() is always called before returning.
173133**
173134** This function handles several different types of window frames, which
173135** require slightly different processing. The following pseudo code is
173136** used to implement window frames of the form:
173137**
173138** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
173139**
173140** Other window frame types use variants of the following:
173141**
173142** ... loop started by sqlite3WhereBegin() ...
173143** if( new partition ){
173144** Gosub flush
173145** }
173146** Insert new row into eph table.
173147**
173148** if( first row of partition ){
173149** // Rewind three cursors, all open on the eph table.
173150** Rewind(csrEnd);
173151** Rewind(csrStart);
173152** Rewind(csrCurrent);
173153**
173154** regEnd = <expr2> // FOLLOWING expression
173155** regStart = <expr1> // PRECEDING expression
173156** }else{
173157** // First time this branch is taken, the eph table contains two
173158** // rows. The first row in the partition, which all three cursors
173159** // currently point to, and the following row.
173160** AGGSTEP
173161** if( (regEnd--)<=0 ){
173162** RETURN_ROW
173163** if( (regStart--)<=0 ){
173164** AGGINVERSE
173165** }
173166** }
173167** }
173168** }
173169** flush:
173170** AGGSTEP
173171** while( 1 ){
173172** RETURN ROW
173173** if( csrCurrent is EOF ) break;
173174** if( (regStart--)<=0 ){
173175** AggInverse(csrStart)
173176** Next(csrStart)
173177** }
173178** }
173179**
173180** The pseudo-code above uses the following shorthand:
173181**
173182** AGGSTEP: invoke the aggregate xStep() function for each window function
173183** with arguments read from the current row of cursor csrEnd, then
173184** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
173185**
173186** RETURN_ROW: return a row to the caller based on the contents of the
173187** current row of csrCurrent and the current state of all
173188** aggregates. Then step cursor csrCurrent forward one row.
173189**
173190** AGGINVERSE: invoke the aggregate xInverse() function for each window
173191** functions with arguments read from the current row of cursor
173192** csrStart. Then step csrStart forward one row.
173193**
173194** There are two other ROWS window frames that are handled significantly
173195** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
173196** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
173197** cases because they change the order in which the three cursors (csrStart,
173198** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
173199** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
173200** three.
173201**
173202** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
173203**
173204** ... loop started by sqlite3WhereBegin() ...
173205** if( new partition ){
173206** Gosub flush
173207** }
173208** Insert new row into eph table.
173209** if( first row of partition ){
173210** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173211** regEnd = <expr2>
173212** regStart = <expr1>
173213** }else{
173214** if( (regEnd--)<=0 ){
173215** AGGSTEP
173216** }
173217** RETURN_ROW
173218** if( (regStart--)<=0 ){
173219** AGGINVERSE
173220** }
173221** }
173222** }
173223** flush:
173224** if( (regEnd--)<=0 ){
173225** AGGSTEP
173226** }
173227** RETURN_ROW
173228**
173229**
173230** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
173231**
173232** ... loop started by sqlite3WhereBegin() ...
173233** if( new partition ){
173234** Gosub flush
173235** }
173236** Insert new row into eph table.
173237** if( first row of partition ){
173238** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173239** regEnd = <expr2>
173240** regStart = regEnd - <expr1>
173241** }else{
173242** AGGSTEP
173243** if( (regEnd--)<=0 ){
173244** RETURN_ROW
173245** }
173246** if( (regStart--)<=0 ){
173247** AGGINVERSE
173248** }
173249** }
173250** }
173251** flush:
173252** AGGSTEP
173253** while( 1 ){
173254** if( (regEnd--)<=0 ){
173255** RETURN_ROW
173256** if( eof ) break;
173257** }
173258** if( (regStart--)<=0 ){
173259** AGGINVERSE
173260** if( eof ) break
173261** }
173262** }
173263** while( !eof csrCurrent ){
173264** RETURN_ROW
173265** }
173266**
173267** For the most part, the patterns above are adapted to support UNBOUNDED by
173268** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
173269** CURRENT ROW by assuming that it is equivalent to "0 PRECEDING/FOLLOWING".
173270** This is optimized of course - branches that will never be taken and
173271** conditions that are always true are omitted from the VM code. The only
173272** exceptional case is:
173273**
173274** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
173275**
173276** ... loop started by sqlite3WhereBegin() ...
173277** if( new partition ){
173278** Gosub flush
173279** }
173280** Insert new row into eph table.
173281** if( first row of partition ){
173282** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173283** regStart = <expr1>
173284** }else{
173285** AGGSTEP
173286** }
173287** }
173288** flush:
173289** AGGSTEP
173290** while( 1 ){
173291** if( (regStart--)<=0 ){
173292** AGGINVERSE
173293** if( eof ) break
173294** }
173295** RETURN_ROW
173296** }
173297** while( !eof csrCurrent ){
173298** RETURN_ROW
173299** }
173300**
173301** Also requiring special handling are the cases:
173302**
173303** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
173304** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
173305**
173306** when (expr1 < expr2). This is detected at runtime, not by this function.
173307** To handle this case, the pseudo-code programs depicted above are modified
173308** slightly to be:
173309**
173310** ... loop started by sqlite3WhereBegin() ...
173311** if( new partition ){
173312** Gosub flush
173313** }
173314** Insert new row into eph table.
173315** if( first row of partition ){
173316** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173317** regEnd = <expr2>
173318** regStart = <expr1>
173319** if( regEnd < regStart ){
173320** RETURN_ROW
173321** delete eph table contents
173322** continue
173323** }
173324** ...
173325**
173326** The new "continue" statement in the above jumps to the next iteration
173327** of the outer loop - the one started by sqlite3WhereBegin().
173328**
173329** The various GROUPS cases are implemented using the same patterns as
173330** ROWS. The VM code is modified slightly so that:
173331**
173332** 1. The else branch in the main loop is only taken if the row just
173333** added to the ephemeral table is the start of a new group. In
173334** other words, it becomes:
173335**
173336** ... loop started by sqlite3WhereBegin() ...
173337** if( new partition ){
173338** Gosub flush
173339** }
173340** Insert new row into eph table.
173341** if( first row of partition ){
173342** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173343** regEnd = <expr2>
173344** regStart = <expr1>
173345** }else if( new group ){
173346** ...
173347** }
173348** }
173349**
173350** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
173351** AGGINVERSE step processes the current row of the relevant cursor and
173352** all subsequent rows belonging to the same group.
173353**
173354** RANGE window frames are a little different again. As for GROUPS, the
173355** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
173356** deal in groups instead of rows. As for ROWS and GROUPS, there are three
173357** basic cases:
173358**
173359** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
173360**
173361** ... loop started by sqlite3WhereBegin() ...
173362** if( new partition ){
173363** Gosub flush
173364** }
173365** Insert new row into eph table.
173366** if( first row of partition ){
173367** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173368** regEnd = <expr2>
173369** regStart = <expr1>
173370** }else{
173371** AGGSTEP
173372** while( (csrCurrent.key + regEnd) < csrEnd.key ){
173373** RETURN_ROW
173374** while( csrStart.key + regStart) < csrCurrent.key ){
173375** AGGINVERSE
173376** }
173377** }
173378** }
173379** }
173380** flush:
173381** AGGSTEP
173382** while( 1 ){
173383** RETURN ROW
173384** if( csrCurrent is EOF ) break;
173385** while( csrStart.key + regStart) < csrCurrent.key ){
173386** AGGINVERSE
173387** }
173388** }
173389** }
173390**
173391** In the above notation, "csr.key" means the current value of the ORDER BY
173392** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
173393** or <expr PRECEDING) read from cursor csr.
173394**
173395** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
173396**
173397** ... loop started by sqlite3WhereBegin() ...
173398** if( new partition ){
173399** Gosub flush
173400** }
173401** Insert new row into eph table.
173402** if( first row of partition ){
173403** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173404** regEnd = <expr2>
173405** regStart = <expr1>
173406** }else{
173407** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
173408** AGGSTEP
173409** }
173410** while( (csrStart.key + regStart) < csrCurrent.key ){
173411** AGGINVERSE
173412** }
173413** RETURN_ROW
173414** }
173415** }
173416** flush:
173417** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
173418** AGGSTEP
173419** }
173420** while( (csrStart.key + regStart) < csrCurrent.key ){
173421** AGGINVERSE
173422** }
173423** RETURN_ROW
173424**
173425** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
173426**
173427** ... loop started by sqlite3WhereBegin() ...
173428** if( new partition ){
173429** Gosub flush
173430** }
173431** Insert new row into eph table.
173432** if( first row of partition ){
173433** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
173434** regEnd = <expr2>
173435** regStart = <expr1>
173436** }else{
173437** AGGSTEP
173438** while( (csrCurrent.key + regEnd) < csrEnd.key ){
173439** while( (csrCurrent.key + regStart) > csrStart.key ){
173440** AGGINVERSE
173441** }
173442** RETURN_ROW
173443** }
173444** }
173445** }
173446** flush:
173447** AGGSTEP
173448** while( 1 ){
173449** while( (csrCurrent.key + regStart) > csrStart.key ){
173450** AGGINVERSE
173451** if( eof ) break "while( 1 )" loop.
173452** }
173453** RETURN_ROW
173454** }
173455** while( !eof csrCurrent ){
173456** RETURN_ROW
173457** }
173458**
173459** The text above leaves out many details. Refer to the code and comments
173460** below for a more complete picture.
173461*/
173462SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(
173463 Parse *pParse, /* Parse context */
173464 Select *p, /* Rewritten SELECT statement */
173465 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
173466 int regGosub, /* Register for OP_Gosub */
173467 int addrGosub /* OP_Gosub here to return each row */
173468){
173469 Window *pMWin = p->pWin;
173470 ExprList *pOrderBy = pMWin->pOrderBy;
173471 Vdbe *v = sqlite3GetVdbe(pParse);
173472 int csrWrite; /* Cursor used to write to eph. table */
173473 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
173474 int nInput = p->pSrc->a[0].pSTab->nCol; /* Number of cols returned by sub */
173475 int iInput; /* To iterate through sub cols */
173476 int addrNe; /* Address of OP_Ne */
173477 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
173478 int addrInteger = 0; /* Address of OP_Integer */
173479 int addrEmpty; /* Address of OP_Rewind in flush: */
173480 int regNew; /* Array of registers holding new input row */
173481 int regRecord; /* regNew array in record form */
173482 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
173483 int regPeer = 0; /* Peer values for current row */
173484 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
173485 WindowCodeArg s; /* Context object for sub-routines */
173486 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
173487 int regStart = 0; /* Value of <expr> PRECEDING */
173488 int regEnd = 0; /* Value of <expr> FOLLOWING */
173489
173490 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT((void) (0))
173491 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED((void) (0))
173492 )((void) (0));
173493 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT((void) (0))
173494 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING((void) (0))
173495 )((void) (0));
173496 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT((void) (0))
173497 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES((void) (0))
173498 || pMWin->eExclude==TK_NO((void) (0))
173499 )((void) (0));
173500
173501 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
173502
173503 /* Fill in the context object */
173504 memset(&s, 0, sizeof(WindowCodeArg));
173505 s.pParse = pParse;
173506 s.pMWin = pMWin;
173507 s.pVdbe = v;
173508 s.regGosub = regGosub;
173509 s.addrGosub = addrGosub;
173510 s.current.csr = pMWin->iEphCsr;
173511 csrWrite = s.current.csr+1;
173512 s.start.csr = s.current.csr+2;
173513 s.end.csr = s.current.csr+3;
173514
173515 /* Figure out when rows may be deleted from the ephemeral table. There
173516 ** are four options - they may never be deleted (eDelete==0), they may
173517 ** be deleted as soon as they are no longer part of the window frame
173518 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
173519 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
173520 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
173521 switch( pMWin->eStart ){
173522 case TK_FOLLOWING87:
173523 if( pMWin->eFrmType!=TK_RANGE90
173524 && windowExprGtZero(pParse, pMWin->pStart)
173525 ){
173526 s.eDelete = WINDOW_RETURN_ROW1;
173527 }
173528 break;
173529 case TK_UNBOUNDED91:
173530 if( windowCacheFrame(pMWin)==0 ){
173531 if( pMWin->eEnd==TK_PRECEDING89 ){
173532 if( pMWin->eFrmType!=TK_RANGE90
173533 && windowExprGtZero(pParse, pMWin->pEnd)
173534 ){
173535 s.eDelete = WINDOW_AGGSTEP3;
173536 }
173537 }else{
173538 s.eDelete = WINDOW_RETURN_ROW1;
173539 }
173540 }
173541 break;
173542 default:
173543 s.eDelete = WINDOW_AGGINVERSE2;
173544 break;
173545 }
173546
173547 /* Allocate registers for the array of values from the sub-query, the
173548 ** same values in record form, and the rowid used to insert said record
173549 ** into the ephemeral table. */
173550 regNew = pParse->nMem+1;
173551 pParse->nMem += nInput;
173552 regRecord = ++pParse->nMem;
173553 s.regRowid = ++pParse->nMem;
173554
173555 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
173556 ** clause, allocate registers to store the results of evaluating each
173557 ** <expr>. */
173558 if( pMWin->eStart==TK_PRECEDING89 || pMWin->eStart==TK_FOLLOWING87 ){
173559 regStart = ++pParse->nMem;
173560 }
173561 if( pMWin->eEnd==TK_PRECEDING89 || pMWin->eEnd==TK_FOLLOWING87 ){
173562 regEnd = ++pParse->nMem;
173563 }
173564
173565 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
173566 ** registers to store copies of the ORDER BY expressions (peer values)
173567 ** for the main loop, and for each cursor (start, current and end). */
173568 if( pMWin->eFrmType!=TK_ROWS77 ){
173569 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
173570 regNewPeer = regNew + pMWin->nBufferCol;
173571 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
173572 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
173573 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
173574 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
173575 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
173576 }
173577
173578 /* Load the column values for the row returned by the sub-select
173579 ** into an array of registers starting at regNew. Assemble them into
173580 ** a record in register regRecord. */
173581 for(iInput=0; iInput<nInput; iInput++){
173582 sqlite3VdbeAddOp3(v, OP_Column94, csrInput, iInput, regNew+iInput);
173583 }
173584 sqlite3VdbeAddOp3(v, OP_MakeRecord97, regNew, nInput, regRecord);
173585
173586 /* An input row has just been read into an array of registers starting
173587 ** at regNew. If the window has a PARTITION clause, this block generates
173588 ** VM code to check if the input row is the start of a new partition.
173589 ** If so, it does an OP_Gosub to an address to be filled in later. The
173590 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
173591 if( pMWin->pPartition ){
173592 int addr;
173593 ExprList *pPart = pMWin->pPartition;
173594 int nPart = pPart->nExpr;
173595 int regNewPart = regNew + pMWin->nBufferCol;
173596 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
173597
173598 regFlushPart = ++pParse->nMem;
173599 addr = sqlite3VdbeAddOp3(v, OP_Compare90, regNewPart, pMWin->regPart, nPart);
173600 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-8));
173601 sqlite3VdbeAddOp3(v, OP_Jump14, addr+2, addr+4, addr+2);
173602 VdbeCoverageEqNe(v);
173603 addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub10, regFlushPart);
173604 VdbeComment((v, "call flush_partition"));
173605 sqlite3VdbeAddOp3(v, OP_Copy80, regNewPart, pMWin->regPart, nPart-1);
173606 }
173607
173608 /* Insert the new row into the ephemeral table */
173609 sqlite3VdbeAddOp2(v, OP_NewRowid127, csrWrite, s.regRowid);
173610 sqlite3VdbeAddOp3(v, OP_Insert128, csrWrite, regRecord, s.regRowid);
173611 addrNe = sqlite3VdbeAddOp3(v, OP_Ne53, pMWin->regOne, 0, s.regRowid);
173612 VdbeCoverageNeverNull(v);
173613
173614 /* This block is run for the first row of each partition */
173615 s.regArg = windowInitAccum(pParse, pMWin);
173616
173617 if( regStart ){
173618 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
173619 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE90?3:0));
173620 }
173621 if( regEnd ){
173622 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
173623 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE90?3:0));
173624 }
173625
173626 if( pMWin->eFrmType!=TK_RANGE90 && pMWin->eStart==pMWin->eEnd && regStart ){
173627 int op = ((pMWin->eStart==TK_FOLLOWING87) ? OP_Ge58 : OP_Le56);
173628 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
173629 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
173630 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
173631 windowAggFinal(&s, 0);
173632 sqlite3VdbeAddOp1(v, OP_Rewind36, s.current.csr);
173633 windowReturnOneRow(&s);
173634 sqlite3VdbeAddOp1(v, OP_ResetSorter146, s.current.csr);
173635 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblWhereEnd);
173636 sqlite3VdbeJumpHere(v, addrGe);
173637 }
173638 if( pMWin->eStart==TK_FOLLOWING87 && pMWin->eFrmType!=TK_RANGE90 && regEnd ){
173639 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
173640 sqlite3VdbeAddOp3(v, OP_Subtract108, regStart, regEnd, regStart);
173641 }
173642
173643 if( pMWin->eStart!=TK_UNBOUNDED91 ){
173644 sqlite3VdbeAddOp1(v, OP_Rewind36, s.start.csr);
173645 }
173646 sqlite3VdbeAddOp1(v, OP_Rewind36, s.current.csr);
173647 sqlite3VdbeAddOp1(v, OP_Rewind36, s.end.csr);
173648 if( regPeer && pOrderBy ){
173649 sqlite3VdbeAddOp3(v, OP_Copy80, regNewPeer, regPeer, pOrderBy->nExpr-1);
173650 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.start.reg, pOrderBy->nExpr-1);
173651 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.current.reg, pOrderBy->nExpr-1);
173652 sqlite3VdbeAddOp3(v, OP_Copy80, regPeer, s.end.reg, pOrderBy->nExpr-1);
173653 }
173654
173655 sqlite3VdbeAddOp2(v, OP_Goto9, 0, lblWhereEnd);
173656
173657 sqlite3VdbeJumpHere(v, addrNe);
173658
173659 /* Beginning of the block executed for the second and subsequent rows. */
173660 if( regPeer ){
173661 windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
173662 }
173663 if( pMWin->eStart==TK_FOLLOWING87 ){
173664 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
173665 if( pMWin->eEnd!=TK_UNBOUNDED91 ){
173666 if( pMWin->eFrmType==TK_RANGE90 ){
173667 int lbl = sqlite3VdbeMakeLabel(pParse);
173668 int addrNext = sqlite3VdbeCurrentAddr(v);
173669 windowCodeRangeTest(&s, OP_Ge58, s.current.csr, regEnd, s.end.csr, lbl);
173670 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173671 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
173672 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrNext);
173673 sqlite3VdbeResolveLabel(v, lbl);
173674 }else{
173675 windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 0);
173676 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173677 }
173678 }
173679 }else
173680 if( pMWin->eEnd==TK_PRECEDING89 ){
173681 int bRPS = (pMWin->eStart==TK_PRECEDING89 && pMWin->eFrmType==TK_RANGE90);
173682 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
173683 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173684 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
173685 if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173686 }else{
173687 int addr = 0;
173688 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
173689 if( pMWin->eEnd!=TK_UNBOUNDED91 ){
173690 if( pMWin->eFrmType==TK_RANGE90 ){
173691 int lbl = 0;
173692 addr = sqlite3VdbeCurrentAddr(v);
173693 if( regEnd ){
173694 lbl = sqlite3VdbeMakeLabel(pParse);
173695 windowCodeRangeTest(&s, OP_Ge58, s.current.csr, regEnd, s.end.csr, lbl);
173696 }
173697 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
173698 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173699 if( regEnd ){
173700 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addr);
173701 sqlite3VdbeResolveLabel(v, lbl);
173702 }
173703 }else{
173704 if( regEnd ){
173705 addr = sqlite3VdbeAddOp3(v, OP_IfPos50, regEnd, 0, 1);
173706 VdbeCoverage(v);
173707 }
173708 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
173709 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173710 if( regEnd ) sqlite3VdbeJumpHere(v, addr);
173711 }
173712 }
173713 }
173714
173715 /* End of the main input loop */
173716 sqlite3VdbeResolveLabel(v, lblWhereEnd);
173717 sqlite3WhereEnd(pWInfo);
173718
173719 /* Fall through */
173720 if( pMWin->pPartition ){
173721 addrInteger = sqlite3VdbeAddOp2(v, OP_Integer71, 0, regFlushPart);
173722 sqlite3VdbeJumpHere(v, addrGosubFlush);
173723 }
173724
173725 s.regRowid = 0;
173726 addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind36, csrWrite);
173727 VdbeCoverage(v);
173728 if( pMWin->eEnd==TK_PRECEDING89 ){
173729 int bRPS = (pMWin->eStart==TK_PRECEDING89 && pMWin->eFrmType==TK_RANGE90);
173730 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
173731 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173732 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
173733 }else if( pMWin->eStart==TK_FOLLOWING87 ){
173734 int addrStart;
173735 int addrBreak1;
173736 int addrBreak2;
173737 int addrBreak3;
173738 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
173739 if( pMWin->eFrmType==TK_RANGE90 ){
173740 addrStart = sqlite3VdbeCurrentAddr(v);
173741 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
173742 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
173743 }else
173744 if( pMWin->eEnd==TK_UNBOUNDED91 ){
173745 addrStart = sqlite3VdbeCurrentAddr(v);
173746 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regStart, 1);
173747 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, 0, 1);
173748 }else{
173749 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
173750 addrStart = sqlite3VdbeCurrentAddr(v);
173751 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 1);
173752 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
173753 }
173754 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrStart);
173755 sqlite3VdbeJumpHere(v, addrBreak2);
173756 addrStart = sqlite3VdbeCurrentAddr(v);
173757 addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
173758 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrStart);
173759 sqlite3VdbeJumpHere(v, addrBreak1);
173760 sqlite3VdbeJumpHere(v, addrBreak3);
173761 }else{
173762 int addrBreak;
173763 int addrStart;
173764 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
173765 addrStart = sqlite3VdbeCurrentAddr(v);
173766 addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
173767 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
173768 sqlite3VdbeAddOp2(v, OP_Goto9, 0, addrStart);
173769 sqlite3VdbeJumpHere(v, addrBreak);
173770 }
173771 sqlite3VdbeJumpHere(v, addrEmpty);
173772
173773 sqlite3VdbeAddOp1(v, OP_ResetSorter146, s.current.csr);
173774 if( pMWin->pPartition ){
173775 if( pMWin->regStartRowid ){
173776 sqlite3VdbeAddOp2(v, OP_Integer71, 1, pMWin->regStartRowid);
173777 sqlite3VdbeAddOp2(v, OP_Integer71, 0, pMWin->regEndRowid);
173778 }
173779 sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
173780 sqlite3VdbeAddOp1(v, OP_Return67, regFlushPart);
173781 }
173782}
173783
173784#endif /* SQLITE_OMIT_WINDOWFUNC */
173785
173786/************** End of window.c **********************************************/
173787/************** Begin file parse.c *******************************************/
173788/* This file is automatically generated by Lemon from input grammar
173789** source file "parse.y".
173790*/
173791/*
173792** 2001-09-15
173793**
173794** The author disclaims copyright to this source code. In place of
173795** a legal notice, here is a blessing:
173796**
173797** May you do good and not evil.
173798** May you find forgiveness for yourself and forgive others.
173799** May you share freely, never taking more than you give.
173800**
173801*************************************************************************
173802** This file contains SQLite's SQL parser.
173803**
173804** The canonical source code to this file ("parse.y") is a Lemon grammar
173805** file that specifies the input grammar and actions to take while parsing.
173806** That input file is processed by Lemon to generate a C-language
173807** implementation of a parser for the given grammar. You might be reading
173808** this comment as part of the translated C-code. Edits should be made
173809** to the original parse.y sources.
173810*/
173811
173812/* #include "sqliteInt.h" */
173813
173814/*
173815** Disable all error recovery processing in the parser push-down
173816** automaton.
173817*/
173818#define YYNOERRORRECOVERY1 1
173819
173820/*
173821** Make yytestcase() the same as testcase()
173822*/
173823#define yytestcase(X) testcase(X)
173824
173825/*
173826** Indicate that sqlite3ParserFree() will never be called with a null
173827** pointer.
173828*/
173829#define YYPARSEFREENEVERNULL1 1
173830
173831/*
173832** In the amalgamation, the parse.c file generated by lemon and the
173833** tokenize.c file are concatenated. In that case, sqlite3RunParser()
173834** has access to the the size of the yyParser object and so the parser
173835** engine can be allocated from stack. In that case, only the
173836** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
173837** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
173838** omitted.
173839*/
173840#ifdef SQLITE_AMALGAMATION1
173841# define sqlite3Parser_ENGINEALWAYSONSTACK1 1
173842#endif
173843
173844/*
173845** Alternative datatype for the argument to the malloc() routine passed
173846** into sqlite3ParserAlloc(). The default is size_t.
173847*/
173848#define YYMALLOCARGTYPEu64 u64
173849
173850/*
173851** An instance of the following structure describes the event of a
173852** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
173853** TK_DELETE, or TK_INSTEAD. If the event is of the form
173854**
173855** UPDATE ON (a,b,c)
173856**
173857** Then the "b" IdList records the list "a,b,c".
173858*/
173859struct TrigEvent { int a; IdList * b; };
173860
173861struct FrameBound { int eType; Expr *pExpr; };
173862
173863/*
173864** Disable lookaside memory allocation for objects that might be
173865** shared across database connections.
173866*/
173867static void disableLookaside(Parse *pParse){
173868 sqlite3 *db = pParse->db;
173869 pParse->disableLookaside++;
173870 DisableLookasidedb->lookaside.bDisable++;db->lookaside.sz=0;
173871}
173872
173873#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT1) \
173874 && defined(SQLITE_UDL_CAPABLE_PARSER)
173875/*
173876** Issue an error message if an ORDER BY or LIMIT clause occurs on an
173877** UPDATE or DELETE statement.
173878*/
173879static void updateDeleteLimitError(
173880 Parse *pParse,
173881 ExprList *pOrderBy,
173882 Expr *pLimit
173883){
173884 if( pOrderBy ){
173885 sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\"");
173886 }else{
173887 sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\"");
173888 }
173889 sqlite3ExprListDelete(pParse->db, pOrderBy);
173890 sqlite3ExprDelete(pParse->db, pLimit);
173891}
173892#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */
173893
173894
173895 /*
173896 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
173897 ** all elements in the list. And make sure list length does not exceed
173898 ** SQLITE_LIMIT_COMPOUND_SELECT.
173899 */
173900 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
173901 assert( p!=0 )((void) (0));
173902 if( p->pPrior ){
173903 Select *pNext = 0, *pLoop = p;
173904 int mxSelect, cnt = 1;
173905 while(1){
173906 pLoop->pNext = pNext;
173907 pLoop->selFlags |= SF_Compound0x0000100;
173908 pNext = pLoop;
173909 pLoop = pLoop->pPrior;
173910 if( pLoop==0 ) break;
173911 cnt++;
173912 if( pLoop->pOrderBy || pLoop->pLimit ){
173913 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
173914 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
173915 sqlite3SelectOpName(pNext->op));
173916 break;
173917 }
173918 }
173919 if( (p->selFlags & (SF_MultiValue0x0000400|SF_Values0x0000200))==0
173920 && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT4])>0
173921 && cnt>mxSelect
173922 ){
173923 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
173924 }
173925 }
173926 }
173927
173928 /* Attach a With object describing the WITH clause to a Select
173929 ** object describing the query for which the WITH clause is a prefix.
173930 */
173931 static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){
173932 if( pSelect ){
173933 pSelect->pWith = pWith;
173934 parserDoubleLinkSelect(pParse, pSelect);
173935 }else{
173936 sqlite3WithDelete(pParse->db, pWith);
173937 }
173938 return pSelect;
173939 }
173940
173941 /* Memory allocator for parser stack resizing. This is a thin wrapper around
173942 ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate
173943 ** testing.
173944 */
173945 static void *parserStackRealloc(void *pOld, sqlite3_uint64 newSize){
173946 return sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize);
173947 }
173948
173949
173950 /* Construct a new Expr object from a single token */
173951 static Expr *tokenExpr(Parse *pParse, int op, Token t){
173952 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
173953 if( p ){
173954 /* memset(p, 0, sizeof(Expr)); */
173955 p->op = (u8)op;
173956 p->affExpr = 0;
173957 p->flags = EP_Leaf0x800000;
173958 ExprClearVVAProperties(p);
173959 /* p->iAgg = -1; // Not required */
173960 p->pLeft = p->pRight = 0;
173961 p->pAggInfo = 0;
173962 memset(&p->x, 0, sizeof(p->x));
173963 memset(&p->y, 0, sizeof(p->y));
173964 p->op2 = 0;
173965 p->iTable = 0;
173966 p->iColumn = 0;
173967 p->u.zToken = (char*)&p[1];
173968 memcpy(p->u.zToken, t.z, t.n);
173969 p->u.zToken[t.n] = 0;
173970 p->w.iOfst = (int)(t.z - pParse->zTail);
173971 if( sqlite3Isquote(p->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(p->u.zToken[0])]&0x80
)
){
173972 sqlite3DequoteExpr(p);
173973 }
173974#if SQLITE_MAX_EXPR_DEPTH1000>0
173975 p->nHeight = 1;
173976#endif
173977 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
173978 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
173979 }
173980 }
173981 return p;
173982 }
173983
173984
173985 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
173986 ** unary TK_ISNULL or TK_NOTNULL expression. */
173987 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
173988 sqlite3 *db = pParse->db;
173989 if( pA && pY && pY->op==TK_NULL122 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
173990 pA->op = (u8)op;
173991 sqlite3ExprDelete(db, pA->pRight);
173992 pA->pRight = 0;
173993 }
173994 }
173995
173996 /* Add a single new term to an ExprList that is used to store a
173997 ** list of identifiers. Report an error if the ID list contains
173998 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
173999 ** error while parsing a legacy schema.
174000 */
174001 static ExprList *parserAddExprIdListTerm(
174002 Parse *pParse,
174003 ExprList *pPrior,
174004 Token *pIdToken,
174005 int hasCollate,
174006 int sortOrder
174007 ){
174008 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
174009 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED-1)
174010 && pParse->db->init.busy==0
174011 ){
174012 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
174013 pIdToken->n, pIdToken->z);
174014 }
174015 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
174016 return p;
174017 }
174018
174019#if TK_SPAN181>255
174020# error too many tokens in the grammar
174021#endif
174022/**************** End of %include directives **********************************/
174023/* These constants specify the various numeric values for terminal symbols.
174024***************** Begin token definitions *************************************/
174025#ifndef TK_SEMI1
174026#define TK_SEMI1 1
174027#define TK_EXPLAIN2 2
174028#define TK_QUERY3 3
174029#define TK_PLAN4 4
174030#define TK_BEGIN5 5
174031#define TK_TRANSACTION6 6
174032#define TK_DEFERRED7 7
174033#define TK_IMMEDIATE8 8
174034#define TK_EXCLUSIVE9 9
174035#define TK_COMMIT10 10
174036#define TK_END11 11
174037#define TK_ROLLBACK12 12
174038#define TK_SAVEPOINT13 13
174039#define TK_RELEASE14 14
174040#define TK_TO15 15
174041#define TK_TABLE16 16
174042#define TK_CREATE17 17
174043#define TK_IF18 18
174044#define TK_NOT19 19
174045#define TK_EXISTS20 20
174046#define TK_TEMP21 21
174047#define TK_LP22 22
174048#define TK_RP23 23
174049#define TK_AS24 24
174050#define TK_COMMA25 25
174051#define TK_WITHOUT26 26
174052#define TK_ABORT27 27
174053#define TK_ACTION28 28
174054#define TK_AFTER29 29
174055#define TK_ANALYZE30 30
174056#define TK_ASC31 31
174057#define TK_ATTACH32 32
174058#define TK_BEFORE33 33
174059#define TK_BY34 34
174060#define TK_CASCADE35 35
174061#define TK_CAST36 36
174062#define TK_CONFLICT37 37
174063#define TK_DATABASE38 38
174064#define TK_DESC39 39
174065#define TK_DETACH40 40
174066#define TK_EACH41 41
174067#define TK_FAIL42 42
174068#define TK_OR43 43
174069#define TK_AND44 44
174070#define TK_IS45 45
174071#define TK_ISNOT46 46
174072#define TK_MATCH47 47
174073#define TK_LIKE_KW48 48
174074#define TK_BETWEEN49 49
174075#define TK_IN50 50
174076#define TK_ISNULL51 51
174077#define TK_NOTNULL52 52
174078#define TK_NE53 53
174079#define TK_EQ54 54
174080#define TK_GT55 55
174081#define TK_LE56 56
174082#define TK_LT57 57
174083#define TK_GE58 58
174084#define TK_ESCAPE59 59
174085#define TK_ID60 60
174086#define TK_COLUMNKW61 61
174087#define TK_DO62 62
174088#define TK_FOR63 63
174089#define TK_IGNORE64 64
174090#define TK_INITIALLY65 65
174091#define TK_INSTEAD66 66
174092#define TK_NO67 67
174093#define TK_KEY68 68
174094#define TK_OF69 69
174095#define TK_OFFSET70 70
174096#define TK_PRAGMA71 71
174097#define TK_RAISE72 72
174098#define TK_RECURSIVE73 73
174099#define TK_REPLACE74 74
174100#define TK_RESTRICT75 75
174101#define TK_ROW76 76
174102#define TK_ROWS77 77
174103#define TK_TRIGGER78 78
174104#define TK_VACUUM79 79
174105#define TK_VIEW80 80
174106#define TK_VIRTUAL81 81
174107#define TK_WITH82 82
174108#define TK_NULLS83 83
174109#define TK_FIRST84 84
174110#define TK_LAST85 85
174111#define TK_CURRENT86 86
174112#define TK_FOLLOWING87 87
174113#define TK_PARTITION88 88
174114#define TK_PRECEDING89 89
174115#define TK_RANGE90 90
174116#define TK_UNBOUNDED91 91
174117#define TK_EXCLUDE92 92
174118#define TK_GROUPS93 93
174119#define TK_OTHERS94 94
174120#define TK_TIES95 95
174121#define TK_GENERATED96 96
174122#define TK_ALWAYS97 97
174123#define TK_MATERIALIZED98 98
174124#define TK_REINDEX99 99
174125#define TK_RENAME100 100
174126#define TK_CTIME_KW101 101
174127#define TK_ANY102 102
174128#define TK_BITAND103 103
174129#define TK_BITOR104 104
174130#define TK_LSHIFT105 105
174131#define TK_RSHIFT106 106
174132#define TK_PLUS107 107
174133#define TK_MINUS108 108
174134#define TK_STAR109 109
174135#define TK_SLASH110 110
174136#define TK_REM111 111
174137#define TK_CONCAT112 112
174138#define TK_PTR113 113
174139#define TK_COLLATE114 114
174140#define TK_BITNOT115 115
174141#define TK_ON116 116
174142#define TK_INDEXED117 117
174143#define TK_STRING118 118
174144#define TK_JOIN_KW119 119
174145#define TK_CONSTRAINT120 120
174146#define TK_DEFAULT121 121
174147#define TK_NULL122 122
174148#define TK_PRIMARY123 123
174149#define TK_UNIQUE124 124
174150#define TK_CHECK125 125
174151#define TK_REFERENCES126 126
174152#define TK_AUTOINCR127 127
174153#define TK_INSERT128 128
174154#define TK_DELETE129 129
174155#define TK_UPDATE130 130
174156#define TK_SET131 131
174157#define TK_DEFERRABLE132 132
174158#define TK_FOREIGN133 133
174159#define TK_DROP134 134
174160#define TK_UNION135 135
174161#define TK_ALL136 136
174162#define TK_EXCEPT137 137
174163#define TK_INTERSECT138 138
174164#define TK_SELECT139 139
174165#define TK_VALUES140 140
174166#define TK_DISTINCT141 141
174167#define TK_DOT142 142
174168#define TK_FROM143 143
174169#define TK_JOIN144 144
174170#define TK_USING145 145
174171#define TK_ORDER146 146
174172#define TK_GROUP147 147
174173#define TK_HAVING148 148
174174#define TK_LIMIT149 149
174175#define TK_WHERE150 150
174176#define TK_RETURNING151 151
174177#define TK_INTO152 152
174178#define TK_NOTHING153 153
174179#define TK_FLOAT154 154
174180#define TK_BLOB155 155
174181#define TK_INTEGER156 156
174182#define TK_VARIABLE157 157
174183#define TK_CASE158 158
174184#define TK_WHEN159 159
174185#define TK_THEN160 160
174186#define TK_ELSE161 161
174187#define TK_INDEX162 162
174188#define TK_ALTER163 163
174189#define TK_ADD164 164
174190#define TK_WINDOW165 165
174191#define TK_OVER166 166
174192#define TK_FILTER167 167
174193#define TK_COLUMN168 168
174194#define TK_AGG_FUNCTION169 169
174195#define TK_AGG_COLUMN170 170
174196#define TK_TRUEFALSE171 171
174197#define TK_FUNCTION172 172
174198#define TK_UPLUS173 173
174199#define TK_UMINUS174 174
174200#define TK_TRUTH175 175
174201#define TK_REGISTER176 176
174202#define TK_VECTOR177 177
174203#define TK_SELECT_COLUMN178 178
174204#define TK_IF_NULL_ROW179 179
174205#define TK_ASTERISK180 180
174206#define TK_SPAN181 181
174207#define TK_ERROR182 182
174208#define TK_QNUMBER183 183
174209#define TK_SPACE184 184
174210#define TK_ILLEGAL185 185
174211#endif
174212/**************** End token definitions ***************************************/
174213
174214/* The next sections is a series of control #defines.
174215** various aspects of the generated parser.
174216** YYCODETYPE is the data type used to store the integer codes
174217** that represent terminal and non-terminal symbols.
174218** "unsigned char" is used if there are fewer than
174219** 256 symbols. Larger types otherwise.
174220** YYNOCODE is a number of type YYCODETYPE that is not used for
174221** any terminal or nonterminal symbol.
174222** YYFALLBACK If defined, this indicates that one or more tokens
174223** (also known as: "terminal symbols") have fall-back
174224** values which should be used if the original symbol
174225** would not parse. This permits keywords to sometimes
174226** be used as identifiers, for example.
174227** YYACTIONTYPE is the data type used for "action codes" - numbers
174228** that indicate what to do in response to the next
174229** token.
174230** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
174231** symbols. Background: A "minor type" is a semantic
174232** value associated with a terminal or non-terminal
174233** symbols. For example, for an "ID" terminal symbol,
174234** the minor type might be the name of the identifier.
174235** Each non-terminal can have a different minor type.
174236** Terminal symbols all have the same minor type, though.
174237** This macros defines the minor type for terminal
174238** symbols.
174239** YYMINORTYPE is the data type used for all minor types.
174240** This is typically a union of many types, one of
174241** which is sqlite3ParserTOKENTYPE. The entry in the union
174242** for terminal symbols is called "yy0".
174243** YYSTACKDEPTH is the maximum depth of the parser's stack. If
174244** zero the stack is dynamically sized using realloc()
174245** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
174246** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
174247** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
174248** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
174249** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
174250** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
174251** YYREALLOC Name of the realloc() function to use
174252** YYFREE Name of the free() function to use
174253** YYDYNSTACK True if stack space should be extended on heap
174254** YYERRORSYMBOL is the code number of the error symbol. If not
174255** defined, then do no error processing.
174256** YYNSTATE the combined number of states.
174257** YYNRULE the number of rules in the grammar
174258** YYNTOKEN Number of terminal symbols
174259** YY_MAX_SHIFT Maximum value for shift actions
174260** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
174261** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
174262** YY_ERROR_ACTION The yy_action[] code for syntax error
174263** YY_ACCEPT_ACTION The yy_action[] code for accept
174264** YY_NO_ACTION The yy_action[] code for no-op
174265** YY_MIN_REDUCE Minimum value for reduce actions
174266** YY_MAX_REDUCE Maximum value for reduce actions
174267** YY_MIN_DSTRCTR Minimum symbol value that has a destructor
174268** YY_MAX_DSTRCTR Maximum symbol value that has a destructor
174269*/
174270#ifndef INTERFACE1
174271# define INTERFACE1 1
174272#endif
174273/************* Begin control #defines *****************************************/
174274#define YYCODETYPEunsigned short int unsigned short int
174275#define YYNOCODE322 322
174276#define YYACTIONTYPEunsigned short int unsigned short int
174277#define YYWILDCARD102 102
174278#define sqlite3ParserTOKENTYPEToken Token
174279typedef union {
174280 int yyinit;
174281 sqlite3ParserTOKENTYPEToken yy0;
174282 ExprList* yy14;
174283 With* yy59;
174284 Cte* yy67;
174285 Upsert* yy122;
174286 IdList* yy132;
174287 int yy144;
174288 const char* yy168;
174289 SrcList* yy203;
174290 Window* yy211;
174291 OnOrUsing yy269;
174292 struct TrigEvent yy286;
174293 struct {int value; int mask;} yy383;
174294 u32 yy391;
174295 TriggerStep* yy427;
174296 Expr* yy454;
174297 u8 yy462;
174298 struct FrameBound yy509;
174299 Select* yy555;
174300} YYMINORTYPE;
174301#ifndef YYSTACKDEPTH100
174302#define YYSTACKDEPTH100 100
174303#endif
174304#define sqlite3ParserARG_SDECL
174305#define sqlite3ParserARG_PDECL
174306#define sqlite3ParserARG_PARAM
174307#define sqlite3ParserARG_FETCH
174308#define sqlite3ParserARG_STORE
174309#define YYREALLOCparserStackRealloc parserStackRealloc
174310#define YYFREEsqlite3_free sqlite3_free
174311#define YYDYNSTACK1 1
174312#define sqlite3ParserCTX_SDECLParse *pParse; Parse *pParse;
174313#define sqlite3ParserCTX_PDECL,Parse *pParse ,Parse *pParse
174314#define sqlite3ParserCTX_PARAM,pParse ,pParse
174315#define sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse; Parse *pParse=yypParser->pParse;
174316#define sqlite3ParserCTX_STOREyypParser->pParse=pParse; yypParser->pParse=pParse;
174317#define YYFALLBACK1 1
174318#define YYNSTATE583 583
174319#define YYNRULE409 409
174320#define YYNRULE_WITH_ACTION344 344
174321#define YYNTOKEN186 186
174322#define YY_MAX_SHIFT582 582
174323#define YY_MIN_SHIFTREDUCE845 845
174324#define YY_MAX_SHIFTREDUCE1253 1253
174325#define YY_ERROR_ACTION1254 1254
174326#define YY_ACCEPT_ACTION1255 1255
174327#define YY_NO_ACTION1256 1256
174328#define YY_MIN_REDUCE1257 1257
174329#define YY_MAX_REDUCE1665 1665
174330#define YY_MIN_DSTRCTR205 205
174331#define YY_MAX_DSTRCTR319 319
174332/************* End control #defines *******************************************/
174333#define YY_NLOOKAHEAD((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
174334
174335/* Define the yytestcase() macro to be a no-op if is not already defined
174336** otherwise.
174337**
174338** Applications can choose to define yytestcase() in the %include section
174339** to a macro that can assist in verifying code coverage. For production
174340** code the yytestcase() macro should be turned off. But it is useful
174341** for testing.
174342*/
174343#ifndef yytestcase
174344# define yytestcase(X)
174345#endif
174346
174347/* Macro to determine if stack space has the ability to grow using
174348** heap memory.
174349*/
174350#if YYSTACKDEPTH100<=0 || YYDYNSTACK1
174351# define YYGROWABLESTACK1 1
174352#else
174353# define YYGROWABLESTACK1 0
174354#endif
174355
174356/* Guarantee a minimum number of initial stack slots.
174357*/
174358#if YYSTACKDEPTH100<=0
174359# undef YYSTACKDEPTH100
174360# define YYSTACKDEPTH100 2 /* Need a minimum stack size */
174361#endif
174362
174363
174364/* Next are the tables used to determine what action to take based on the
174365** current state and lookahead token. These tables are used to implement
174366** functions that take a state number and lookahead value and return an
174367** action integer.
174368**
174369** Suppose the action integer is N. Then the action is determined as
174370** follows
174371**
174372** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
174373** token onto the stack and goto state N.
174374**
174375** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
174376** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
174377**
174378** N == YY_ERROR_ACTION A syntax error has occurred.
174379**
174380** N == YY_ACCEPT_ACTION The parser accepts its input.
174381**
174382** N == YY_NO_ACTION No such action. Denotes unused
174383** slots in the yy_action[] table.
174384**
174385** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
174386** and YY_MAX_REDUCE
174387**
174388** The action table is constructed as a single large table named yy_action[].
174389** Given state S and lookahead X, the action is computed as either:
174390**
174391** (A) N = yy_action[ yy_shift_ofst[S] + X ]
174392** (B) N = yy_default[S]
174393**
174394** The (A) formula is preferred. The B formula is used instead if
174395** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
174396**
174397** The formulas above are for computing the action when the lookahead is
174398** a terminal symbol. If the lookahead is a non-terminal (as occurs after
174399** a reduce action) then the yy_reduce_ofst[] array is used in place of
174400** the yy_shift_ofst[] array.
174401**
174402** The following are the tables generated in this section:
174403**
174404** yy_action[] A single table containing all actions.
174405** yy_lookahead[] A table containing the lookahead for each entry in
174406** yy_action. Used to detect hash collisions.
174407** yy_shift_ofst[] For each state, the offset into yy_action for
174408** shifting terminals.
174409** yy_reduce_ofst[] For each state, the offset into yy_action for
174410** shifting non-terminals after a reduce.
174411** yy_default[] Default action for each state.
174412**
174413*********** Begin parsing tables **********************************************/
174414#define YY_ACTTAB_COUNT(2207) (2207)
174415static const YYACTIONTYPEunsigned short int yy_action[] = {
174416 /* 0 */ 130, 127, 234, 282, 282, 1328, 576, 1307, 460, 289,
174417 /* 10 */ 289, 576, 1622, 381, 576, 1328, 573, 576, 562, 413,
174418 /* 20 */ 1300, 1542, 573, 481, 562, 524, 460, 459, 558, 82,
174419 /* 30 */ 82, 983, 294, 375, 51, 51, 498, 61, 61, 984,
174420 /* 40 */ 82, 82, 1577, 137, 138, 91, 7, 1228, 1228, 1063,
174421 /* 50 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 413,
174422 /* 60 */ 288, 288, 182, 288, 288, 481, 536, 288, 288, 130,
174423 /* 70 */ 127, 234, 432, 573, 525, 562, 573, 557, 562, 1290,
174424 /* 80 */ 573, 421, 562, 137, 138, 91, 559, 1228, 1228, 1063,
174425 /* 90 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 296,
174426 /* 100 */ 460, 398, 1249, 134, 134, 134, 134, 133, 133, 132,
174427 /* 110 */ 132, 132, 131, 128, 451, 44, 1050, 1050, 1064, 1067,
174428 /* 120 */ 1255, 1, 1, 582, 2, 1259, 581, 1174, 1259, 1174,
174429 /* 130 */ 321, 413, 155, 321, 1584, 155, 379, 112, 498, 1341,
174430 /* 140 */ 456, 299, 1341, 134, 134, 134, 134, 133, 133, 132,
174431 /* 150 */ 132, 132, 131, 128, 451, 137, 138, 91, 1105, 1228,
174432 /* 160 */ 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136,
174433 /* 170 */ 136, 1204, 320, 567, 288, 288, 283, 288, 288, 523,
174434 /* 180 */ 523, 1250, 139, 1541, 7, 214, 503, 573, 1169, 562,
174435 /* 190 */ 573, 1054, 562, 136, 136, 136, 136, 129, 401, 547,
174436 /* 200 */ 487, 1169, 245, 1568, 1169, 245, 133, 133, 132, 132,
174437 /* 210 */ 132, 131, 128, 451, 261, 134, 134, 134, 134, 133,
174438 /* 220 */ 133, 132, 132, 132, 131, 128, 451, 451, 1204, 1205,
174439 /* 230 */ 1204, 130, 127, 234, 455, 413, 182, 455, 130, 127,
174440 /* 240 */ 234, 134, 134, 134, 134, 133, 133, 132, 132, 132,
174441 /* 250 */ 131, 128, 451, 136, 136, 136, 136, 538, 576, 137,
174442 /* 260 */ 138, 91, 261, 1228, 1228, 1063, 1066, 1053, 1053, 135,
174443 /* 270 */ 135, 136, 136, 136, 136, 44, 472, 346, 1204, 472,
174444 /* 280 */ 346, 51, 51, 418, 93, 157, 134, 134, 134, 134,
174445 /* 290 */ 133, 133, 132, 132, 132, 131, 128, 451, 166, 363,
174446 /* 300 */ 298, 134, 134, 134, 134, 133, 133, 132, 132, 132,
174447 /* 310 */ 131, 128, 451, 1293, 461, 1570, 423, 377, 275, 134,
174448 /* 320 */ 134, 134, 134, 133, 133, 132, 132, 132, 131, 128,
174449 /* 330 */ 451, 418, 320, 567, 1292, 1204, 1205, 1204, 257, 413,
174450 /* 340 */ 483, 511, 508, 507, 94, 132, 132, 132, 131, 128,
174451 /* 350 */ 451, 506, 1204, 548, 548, 388, 576, 384, 7, 413,
174452 /* 360 */ 550, 229, 522, 137, 138, 91, 530, 1228, 1228, 1063,
174453 /* 370 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 51,
174454 /* 380 */ 51, 1582, 380, 137, 138, 91, 331, 1228, 1228, 1063,
174455 /* 390 */ 1066, 1053, 1053, 135, 135, 136, 136, 136, 136, 320,
174456 /* 400 */ 567, 288, 288, 320, 567, 1602, 582, 2, 1259, 1204,
174457 /* 410 */ 1205, 1204, 1628, 321, 573, 155, 562, 576, 1511, 264,
174458 /* 420 */ 231, 520, 1341, 134, 134, 134, 134, 133, 133, 132,
174459 /* 430 */ 132, 132, 131, 128, 451, 519, 1511, 1513, 1333, 1333,
174460 /* 440 */ 82, 82, 498, 134, 134, 134, 134, 133, 133, 132,
174461 /* 450 */ 132, 132, 131, 128, 451, 1435, 257, 288, 288, 511,
174462 /* 460 */ 508, 507, 944, 1568, 413, 1019, 1204, 943, 360, 506,
174463 /* 470 */ 573, 1598, 562, 44, 575, 551, 551, 557, 1107, 1582,
174464 /* 480 */ 544, 576, 1107, 40, 417, 245, 531, 1505, 137, 138,
174465 /* 490 */ 91, 219, 1228, 1228, 1063, 1066, 1053, 1053, 135, 135,
174466 /* 500 */ 136, 136, 136, 136, 81, 81, 1281, 1204, 413, 553,
174467 /* 510 */ 1511, 48, 512, 448, 447, 493, 578, 455, 578, 344,
174468 /* 520 */ 45, 1204, 1233, 1204, 1205, 1204, 428, 1235, 158, 882,
174469 /* 530 */ 320, 567, 137, 138, 91, 1234, 1228, 1228, 1063, 1066,
174470 /* 540 */ 1053, 1053, 135, 135, 136, 136, 136, 136, 134, 134,
174471 /* 550 */ 134, 134, 133, 133, 132, 132, 132, 131, 128, 451,
174472 /* 560 */ 1236, 576, 1236, 329, 1204, 1205, 1204, 387, 492, 403,
174473 /* 570 */ 1040, 382, 489, 123, 568, 1569, 4, 377, 1204, 1205,
174474 /* 580 */ 1204, 570, 570, 570, 82, 82, 882, 1029, 1331, 1331,
174475 /* 590 */ 571, 1028, 134, 134, 134, 134, 133, 133, 132, 132,
174476 /* 600 */ 132, 131, 128, 451, 288, 288, 1281, 1204, 576, 423,
174477 /* 610 */ 576, 1568, 413, 423, 452, 378, 886, 573, 1279, 562,
174478 /* 620 */ 46, 557, 532, 1028, 1028, 1030, 565, 130, 127, 234,
174479 /* 630 */ 556, 82, 82, 82, 82, 479, 137, 138, 91, 462,
174480 /* 640 */ 1228, 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136,
174481 /* 650 */ 136, 136, 1188, 487, 1506, 1040, 413, 6, 1204, 50,
174482 /* 660 */ 879, 121, 121, 948, 1204, 1205, 1204, 358, 557, 122,
174483 /* 670 */ 316, 452, 577, 452, 535, 1204, 1028, 439, 303, 212,
174484 /* 680 */ 137, 138, 91, 213, 1228, 1228, 1063, 1066, 1053, 1053,
174485 /* 690 */ 135, 135, 136, 136, 136, 136, 134, 134, 134, 134,
174486 /* 700 */ 133, 133, 132, 132, 132, 131, 128, 451, 1028, 1028,
174487 /* 710 */ 1030, 1031, 35, 288, 288, 1204, 1205, 1204, 1040, 1339,
174488 /* 720 */ 533, 123, 568, 1569, 4, 377, 573, 1019, 562, 353,
174489 /* 730 */ 1277, 356, 1204, 1205, 1204, 1029, 488, 1188, 571, 1028,
174490 /* 740 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
174491 /* 750 */ 128, 451, 576, 343, 288, 288, 449, 449, 449, 971,
174492 /* 760 */ 413, 1627, 452, 911, 1187, 288, 288, 573, 464, 562,
174493 /* 770 */ 238, 1028, 1028, 1030, 565, 82, 82, 498, 573, 411,
174494 /* 780 */ 562, 344, 467, 332, 137, 138, 91, 197, 1228, 1228,
174495 /* 790 */ 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136, 136,
174496 /* 800 */ 1188, 528, 1169, 1040, 413, 1110, 1110, 495, 1041, 121,
174497 /* 810 */ 121, 1204, 317, 540, 862, 1169, 1244, 122, 1169, 452,
174498 /* 820 */ 577, 452, 1340, 198, 1028, 1204, 481, 526, 137, 138,
174499 /* 830 */ 91, 560, 1228, 1228, 1063, 1066, 1053, 1053, 135, 135,
174500 /* 840 */ 136, 136, 136, 136, 134, 134, 134, 134, 133, 133,
174501 /* 850 */ 132, 132, 132, 131, 128, 451, 1028, 1028, 1030, 1031,
174502 /* 860 */ 35, 1204, 288, 288, 1204, 477, 288, 288, 1204, 1205,
174503 /* 870 */ 1204, 539, 481, 437, 470, 573, 1451, 562, 364, 573,
174504 /* 880 */ 1153, 562, 1204, 1205, 1204, 1188, 5, 576, 134, 134,
174505 /* 890 */ 134, 134, 133, 133, 132, 132, 132, 131, 128, 451,
174506 /* 900 */ 221, 214, 302, 96, 1149, 1657, 232, 1657, 413, 392,
174507 /* 910 */ 19, 19, 1024, 949, 406, 373, 1595, 1085, 1204, 1205,
174508 /* 920 */ 1204, 1204, 1205, 1204, 1204, 426, 1149, 1658, 413, 1658,
174509 /* 930 */ 1659, 399, 137, 138, 91, 3, 1228, 1228, 1063, 1066,
174510 /* 940 */ 1053, 1053, 135, 135, 136, 136, 136, 136, 304, 1311,
174511 /* 950 */ 514, 1204, 137, 138, 91, 1498, 1228, 1228, 1063, 1066,
174512 /* 960 */ 1053, 1053, 135, 135, 136, 136, 136, 136, 434, 131,
174513 /* 970 */ 128, 451, 375, 1204, 274, 291, 372, 517, 367, 516,
174514 /* 980 */ 262, 1204, 1205, 1204, 1147, 227, 363, 448, 447, 1435,
174515 /* 990 */ 1568, 1310, 134, 134, 134, 134, 133, 133, 132, 132,
174516 /* 1000 */ 132, 131, 128, 451, 1568, 576, 1147, 487, 1204, 1205,
174517 /* 1010 */ 1204, 442, 134, 134, 134, 134, 133, 133, 132, 132,
174518 /* 1020 */ 132, 131, 128, 451, 386, 576, 485, 576, 19, 19,
174519 /* 1030 */ 1204, 1205, 1204, 1345, 1236, 970, 1236, 574, 47, 936,
174520 /* 1040 */ 936, 473, 413, 431, 1552, 573, 1125, 562, 19, 19,
174521 /* 1050 */ 19, 19, 49, 336, 850, 851, 852, 111, 1368, 315,
174522 /* 1060 */ 429, 576, 413, 433, 341, 306, 137, 138, 91, 115,
174523 /* 1070 */ 1228, 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136,
174524 /* 1080 */ 136, 136, 576, 1309, 82, 82, 137, 138, 91, 529,
174525 /* 1090 */ 1228, 1228, 1063, 1066, 1053, 1053, 135, 135, 136, 136,
174526 /* 1100 */ 136, 136, 1569, 222, 377, 19, 19, 305, 1126, 1169,
174527 /* 1110 */ 398, 1148, 22, 22, 498, 333, 1569, 335, 377, 576,
174528 /* 1120 */ 438, 445, 1169, 1127, 486, 1169, 134, 134, 134, 134,
174529 /* 1130 */ 133, 133, 132, 132, 132, 131, 128, 451, 1128, 576,
174530 /* 1140 */ 902, 576, 145, 145, 6, 576, 134, 134, 134, 134,
174531 /* 1150 */ 133, 133, 132, 132, 132, 131, 128, 451, 214, 1336,
174532 /* 1160 */ 922, 576, 19, 19, 19, 19, 1282, 419, 19, 19,
174533 /* 1170 */ 923, 412, 515, 141, 576, 1169, 413, 206, 465, 207,
174534 /* 1180 */ 903, 215, 1575, 552, 147, 147, 7, 227, 1169, 411,
174535 /* 1190 */ 1250, 1169, 120, 307, 117, 307, 413, 66, 66, 334,
174536 /* 1200 */ 137, 138, 91, 119, 1228, 1228, 1063, 1066, 1053, 1053,
174537 /* 1210 */ 135, 135, 136, 136, 136, 136, 413, 285, 209, 969,
174538 /* 1220 */ 137, 138, 91, 471, 1228, 1228, 1063, 1066, 1053, 1053,
174539 /* 1230 */ 135, 135, 136, 136, 136, 136, 435, 10, 1450, 267,
174540 /* 1240 */ 137, 126, 91, 1435, 1228, 1228, 1063, 1066, 1053, 1053,
174541 /* 1250 */ 135, 135, 136, 136, 136, 136, 1435, 1435, 410, 409,
174542 /* 1260 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
174543 /* 1270 */ 128, 451, 576, 969, 576, 1224, 498, 373, 1595, 1554,
174544 /* 1280 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
174545 /* 1290 */ 128, 451, 532, 457, 576, 82, 82, 82, 82, 111,
174546 /* 1300 */ 134, 134, 134, 134, 133, 133, 132, 132, 132, 131,
174547 /* 1310 */ 128, 451, 109, 233, 430, 1576, 546, 67, 67, 7,
174548 /* 1320 */ 413, 351, 550, 1550, 260, 259, 258, 494, 443, 569,
174549 /* 1330 */ 419, 983, 446, 1224, 450, 545, 1207, 576, 969, 984,
174550 /* 1340 */ 413, 475, 1449, 1574, 1180, 138, 91, 7, 1228, 1228,
174551 /* 1350 */ 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136, 136,
174552 /* 1360 */ 21, 21, 267, 576, 300, 1126, 91, 233, 1228, 1228,
174553 /* 1370 */ 1063, 1066, 1053, 1053, 135, 135, 136, 136, 136, 136,
174554 /* 1380 */ 1127, 373, 1595, 161, 1573, 16, 53, 53, 7, 108,
174555 /* 1390 */ 533, 38, 969, 125, 1207, 1128, 1180, 576, 1224, 123,
174556 /* 1400 */ 568, 893, 4, 324, 134, 134, 134, 134, 133, 133,
174557 /* 1410 */ 132, 132, 132, 131, 128, 451, 571, 564, 534, 576,
174558 /* 1420 */ 68, 68, 576, 39, 134, 134, 134, 134, 133, 133,
174559 /* 1430 */ 132, 132, 132, 131, 128, 451, 576, 160, 1571, 1223,
174560 /* 1440 */ 452, 576, 54, 54, 576, 69, 69, 576, 1366, 576,
174561 /* 1450 */ 420, 184, 565, 463, 297, 576, 1224, 463, 297, 70,
174562 /* 1460 */ 70, 576, 44, 474, 71, 71, 576, 72, 72, 576,
174563 /* 1470 */ 73, 73, 55, 55, 411, 874, 242, 576, 56, 56,
174564 /* 1480 */ 576, 1040, 576, 478, 57, 57, 576, 121, 121, 59,
174565 /* 1490 */ 59, 23, 60, 60, 411, 122, 319, 452, 577, 452,
174566 /* 1500 */ 74, 74, 1028, 75, 75, 76, 76, 411, 290, 20,
174567 /* 1510 */ 20, 108, 287, 231, 553, 123, 568, 325, 4, 320,
174568 /* 1520 */ 567, 97, 218, 944, 1144, 328, 400, 576, 943, 576,
174569 /* 1530 */ 1380, 424, 571, 874, 1028, 1028, 1030, 1031, 35, 293,
174570 /* 1540 */ 534, 576, 1104, 576, 1104, 9, 576, 342, 576, 111,
174571 /* 1550 */ 77, 77, 143, 143, 576, 205, 452, 222, 1379, 889,
174572 /* 1560 */ 576, 901, 900, 1188, 144, 144, 78, 78, 565, 62,
174573 /* 1570 */ 62, 79, 79, 323, 1021, 576, 266, 63, 63, 908,
174574 /* 1580 */ 909, 1589, 542, 80, 80, 576, 371, 541, 123, 568,
174575 /* 1590 */ 480, 4, 266, 482, 244, 266, 370, 1040, 64, 64,
174576 /* 1600 */ 576, 466, 576, 121, 121, 571, 1557, 576, 170, 170,
174577 /* 1610 */ 576, 122, 576, 452, 577, 452, 576, 889, 1028, 576,
174578 /* 1620 */ 165, 576, 111, 171, 171, 87, 87, 337, 1616, 452,
174579 /* 1630 */ 65, 65, 1530, 83, 83, 146, 146, 986, 987, 84,
174580 /* 1640 */ 84, 565, 168, 168, 148, 148, 1092, 347, 1032, 111,
174581 /* 1650 */ 1028, 1028, 1030, 1031, 35, 542, 1103, 576, 1103, 576,
174582 /* 1660 */ 543, 123, 568, 504, 4, 263, 576, 361, 1529, 111,
174583 /* 1670 */ 1040, 1088, 576, 263, 576, 490, 121, 121, 571, 1188,
174584 /* 1680 */ 142, 142, 169, 169, 122, 576, 452, 577, 452, 162,
174585 /* 1690 */ 162, 1028, 576, 563, 576, 152, 152, 151, 151, 348,
174586 /* 1700 */ 1376, 974, 452, 266, 1092, 942, 1032, 125, 149, 149,
174587 /* 1710 */ 939, 576, 125, 576, 565, 150, 150, 86, 86, 872,
174588 /* 1720 */ 352, 159, 576, 1028, 1028, 1030, 1031, 35, 542, 941,
174589 /* 1730 */ 576, 125, 355, 541, 88, 88, 85, 85, 357, 359,
174590 /* 1740 */ 1324, 1308, 366, 1040, 376, 52, 52, 499, 1389, 121,
174591 /* 1750 */ 121, 1434, 1188, 58, 58, 1362, 1374, 122, 1439, 452,
174592 /* 1760 */ 577, 452, 1289, 167, 1028, 1280, 280, 1268, 1267, 1269,
174593 /* 1770 */ 1609, 1359, 312, 313, 12, 314, 397, 1421, 224, 1416,
174594 /* 1780 */ 295, 237, 1409, 339, 340, 1426, 301, 345, 484, 228,
174595 /* 1790 */ 1371, 1307, 1372, 1370, 1425, 404, 1028, 1028, 1030, 1031,
174596 /* 1800 */ 35, 1601, 1192, 454, 509, 369, 292, 1502, 210, 1501,
174597 /* 1810 */ 1369, 396, 396, 395, 277, 393, 211, 566, 859, 1612,
174598 /* 1820 */ 1244, 123, 568, 391, 4, 1188, 223, 270, 1549, 1547,
174599 /* 1830 */ 1241, 239, 186, 327, 422, 96, 195, 220, 571, 235,
174600 /* 1840 */ 180, 326, 188, 468, 190, 1507, 191, 192, 92, 193,
174601 /* 1850 */ 469, 95, 1422, 13, 502, 247, 1430, 109, 199, 402,
174602 /* 1860 */ 476, 405, 452, 1496, 1428, 1427, 14, 491, 251, 102,
174603 /* 1870 */ 497, 1518, 241, 281, 565, 253, 203, 354, 500, 254,
174604 /* 1880 */ 175, 1270, 407, 43, 350, 518, 1327, 436, 255, 1326,
174605 /* 1890 */ 1325, 1318, 104, 893, 1626, 229, 408, 440, 1625, 441,
174606 /* 1900 */ 240, 310, 1296, 1040, 311, 1317, 527, 1594, 1297, 121,
174607 /* 1910 */ 121, 368, 1295, 1624, 268, 269, 1580, 122, 1579, 452,
174608 /* 1920 */ 577, 452, 374, 444, 1028, 1394, 1393, 140, 553, 90,
174609 /* 1930 */ 568, 11, 4, 1483, 383, 414, 385, 110, 116, 216,
174610 /* 1940 */ 320, 567, 1350, 555, 42, 318, 571, 537, 1349, 389,
174611 /* 1950 */ 390, 579, 1198, 276, 279, 278, 1028, 1028, 1030, 1031,
174612 /* 1960 */ 35, 580, 415, 1265, 458, 1260, 416, 185, 1534, 172,
174613 /* 1970 */ 452, 1535, 173, 156, 308, 846, 1533, 1532, 453, 217,
174614 /* 1980 */ 225, 89, 565, 174, 322, 1188, 226, 236, 1102, 154,
174615 /* 1990 */ 1100, 330, 176, 187, 1223, 189, 925, 338, 243, 1116,
174616 /* 2000 */ 246, 194, 177, 178, 425, 427, 98, 99, 196, 100,
174617 /* 2010 */ 101, 1040, 179, 1119, 248, 1115, 249, 121, 121, 24,
174618 /* 2020 */ 163, 250, 349, 1108, 266, 122, 1238, 452, 577, 452,
174619 /* 2030 */ 1192, 454, 1028, 200, 292, 496, 252, 201, 861, 396,
174620 /* 2040 */ 396, 395, 277, 393, 15, 501, 859, 370, 292, 256,
174621 /* 2050 */ 202, 554, 505, 396, 396, 395, 277, 393, 103, 239,
174622 /* 2060 */ 859, 327, 25, 26, 1028, 1028, 1030, 1031, 35, 326,
174623 /* 2070 */ 362, 510, 891, 239, 365, 327, 513, 904, 105, 309,
174624 /* 2080 */ 164, 181, 27, 326, 106, 521, 107, 1185, 1069, 1155,
174625 /* 2090 */ 17, 1154, 284, 1188, 286, 978, 265, 204, 125, 1171,
174626 /* 2100 */ 241, 230, 972, 1175, 28, 1160, 29, 1179, 175, 1173,
174627 /* 2110 */ 30, 43, 31, 1178, 241, 32, 41, 549, 8, 33,
174628 /* 2120 */ 208, 111, 175, 1083, 1070, 43, 113, 1068, 240, 114,
174629 /* 2130 */ 1072, 34, 1073, 561, 1124, 118, 271, 36, 18, 1194,
174630 /* 2140 */ 1033, 873, 240, 935, 124, 37, 272, 273, 1617, 572,
174631 /* 2150 */ 183, 153, 394, 1193, 1256, 1256, 1256, 1256, 1256, 1256,
174632 /* 2160 */ 1256, 1256, 1256, 414, 1256, 1256, 1256, 1256, 320, 567,
174633 /* 2170 */ 1256, 1256, 1256, 1256, 1256, 1256, 1256, 414, 1256, 1256,
174634 /* 2180 */ 1256, 1256, 320, 567, 1256, 1256, 1256, 1256, 1256, 1256,
174635 /* 2190 */ 1256, 1256, 458, 1256, 1256, 1256, 1256, 1256, 1256, 1256,
174636 /* 2200 */ 1256, 1256, 1256, 1256, 1256, 1256, 458,
174637};
174638static const YYCODETYPEunsigned short int yy_lookahead[] = {
174639 /* 0 */ 276, 277, 278, 240, 241, 224, 194, 226, 194, 240,
174640 /* 10 */ 241, 194, 216, 220, 194, 234, 253, 194, 255, 19,
174641 /* 20 */ 224, 297, 253, 194, 255, 205, 212, 213, 205, 217,
174642 /* 30 */ 218, 31, 205, 194, 217, 218, 194, 217, 218, 39,
174643 /* 40 */ 217, 218, 312, 43, 44, 45, 316, 47, 48, 49,
174644 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 19,
174645 /* 60 */ 240, 241, 194, 240, 241, 194, 254, 240, 241, 276,
174646 /* 70 */ 277, 278, 233, 253, 254, 255, 253, 254, 255, 217,
174647 /* 80 */ 253, 239, 255, 43, 44, 45, 263, 47, 48, 49,
174648 /* 90 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 270,
174649 /* 100 */ 286, 22, 23, 103, 104, 105, 106, 107, 108, 109,
174650 /* 110 */ 110, 111, 112, 113, 114, 82, 47, 48, 49, 50,
174651 /* 120 */ 186, 187, 188, 189, 190, 191, 189, 87, 191, 89,
174652 /* 130 */ 196, 19, 198, 196, 317, 198, 319, 25, 194, 205,
174653 /* 140 */ 298, 270, 205, 103, 104, 105, 106, 107, 108, 109,
174654 /* 150 */ 110, 111, 112, 113, 114, 43, 44, 45, 11, 47,
174655 /* 160 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
174656 /* 170 */ 58, 60, 139, 140, 240, 241, 214, 240, 241, 311,
174657 /* 180 */ 312, 102, 70, 239, 316, 194, 19, 253, 77, 255,
174658 /* 190 */ 253, 122, 255, 55, 56, 57, 58, 59, 207, 88,
174659 /* 200 */ 194, 90, 268, 194, 93, 268, 107, 108, 109, 110,
174660 /* 210 */ 111, 112, 113, 114, 47, 103, 104, 105, 106, 107,
174661 /* 220 */ 108, 109, 110, 111, 112, 113, 114, 114, 117, 118,
174662 /* 230 */ 119, 276, 277, 278, 300, 19, 194, 300, 276, 277,
174663 /* 240 */ 278, 103, 104, 105, 106, 107, 108, 109, 110, 111,
174664 /* 250 */ 112, 113, 114, 55, 56, 57, 58, 146, 194, 43,
174665 /* 260 */ 44, 45, 47, 47, 48, 49, 50, 51, 52, 53,
174666 /* 270 */ 54, 55, 56, 57, 58, 82, 129, 130, 60, 129,
174667 /* 280 */ 130, 217, 218, 116, 68, 25, 103, 104, 105, 106,
174668 /* 290 */ 107, 108, 109, 110, 111, 112, 113, 114, 23, 132,
174669 /* 300 */ 294, 103, 104, 105, 106, 107, 108, 109, 110, 111,
174670 /* 310 */ 112, 113, 114, 217, 121, 306, 194, 308, 26, 103,
174671 /* 320 */ 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
174672 /* 330 */ 114, 116, 139, 140, 217, 117, 118, 119, 120, 19,
174673 /* 340 */ 194, 123, 124, 125, 24, 109, 110, 111, 112, 113,
174674 /* 350 */ 114, 133, 60, 311, 312, 250, 194, 252, 316, 19,
174675 /* 360 */ 194, 166, 167, 43, 44, 45, 205, 47, 48, 49,
174676 /* 370 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 217,
174677 /* 380 */ 218, 317, 318, 43, 44, 45, 264, 47, 48, 49,
174678 /* 390 */ 50, 51, 52, 53, 54, 55, 56, 57, 58, 139,
174679 /* 400 */ 140, 240, 241, 139, 140, 188, 189, 190, 191, 117,
174680 /* 410 */ 118, 119, 231, 196, 253, 198, 255, 194, 194, 258,
174681 /* 420 */ 259, 146, 205, 103, 104, 105, 106, 107, 108, 109,
174682 /* 430 */ 110, 111, 112, 113, 114, 109, 212, 213, 236, 237,
174683 /* 440 */ 217, 218, 194, 103, 104, 105, 106, 107, 108, 109,
174684 /* 450 */ 110, 111, 112, 113, 114, 194, 120, 240, 241, 123,
174685 /* 460 */ 124, 125, 136, 194, 19, 74, 60, 141, 23, 133,
174686 /* 470 */ 253, 194, 255, 82, 194, 309, 310, 254, 29, 317,
174687 /* 480 */ 318, 194, 33, 22, 199, 268, 263, 239, 43, 44,
174688 /* 490 */ 45, 151, 47, 48, 49, 50, 51, 52, 53, 54,
174689 /* 500 */ 55, 56, 57, 58, 217, 218, 194, 60, 19, 146,
174690 /* 510 */ 286, 242, 23, 107, 108, 66, 204, 300, 206, 128,
174691 /* 520 */ 73, 60, 116, 117, 118, 119, 265, 121, 165, 60,
174692 /* 530 */ 139, 140, 43, 44, 45, 129, 47, 48, 49, 50,
174693 /* 540 */ 51, 52, 53, 54, 55, 56, 57, 58, 103, 104,
174694 /* 550 */ 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
174695 /* 560 */ 154, 194, 156, 194, 117, 118, 119, 280, 283, 205,
174696 /* 570 */ 101, 220, 287, 19, 20, 306, 22, 308, 117, 118,
174697 /* 580 */ 119, 211, 212, 213, 217, 218, 117, 118, 236, 237,
174698 /* 590 */ 36, 122, 103, 104, 105, 106, 107, 108, 109, 110,
174699 /* 600 */ 111, 112, 113, 114, 240, 241, 194, 60, 194, 194,
174700 /* 610 */ 194, 194, 19, 194, 60, 194, 23, 253, 206, 255,
174701 /* 620 */ 73, 254, 19, 154, 155, 156, 72, 276, 277, 278,
174702 /* 630 */ 263, 217, 218, 217, 218, 271, 43, 44, 45, 271,
174703 /* 640 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
174704 /* 650 */ 57, 58, 183, 194, 285, 101, 19, 214, 60, 242,
174705 /* 660 */ 23, 107, 108, 109, 117, 118, 119, 16, 254, 115,
174706 /* 670 */ 254, 117, 118, 119, 194, 60, 122, 263, 205, 264,
174707 /* 680 */ 43, 44, 45, 264, 47, 48, 49, 50, 51, 52,
174708 /* 690 */ 53, 54, 55, 56, 57, 58, 103, 104, 105, 106,
174709 /* 700 */ 107, 108, 109, 110, 111, 112, 113, 114, 154, 155,
174710 /* 710 */ 156, 157, 158, 240, 241, 117, 118, 119, 101, 205,
174711 /* 720 */ 117, 19, 20, 306, 22, 308, 253, 74, 255, 78,
174712 /* 730 */ 205, 80, 117, 118, 119, 118, 293, 183, 36, 122,
174713 /* 740 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
174714 /* 750 */ 113, 114, 194, 294, 240, 241, 211, 212, 213, 144,
174715 /* 760 */ 19, 23, 60, 25, 23, 240, 241, 253, 245, 255,
174716 /* 770 */ 15, 154, 155, 156, 72, 217, 218, 194, 253, 256,
174717 /* 780 */ 255, 128, 129, 130, 43, 44, 45, 22, 47, 48,
174718 /* 790 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
174719 /* 800 */ 183, 19, 77, 101, 19, 128, 129, 130, 23, 107,
174720 /* 810 */ 108, 60, 254, 88, 21, 90, 61, 115, 93, 117,
174721 /* 820 */ 118, 119, 239, 22, 122, 60, 194, 205, 43, 44,
174722 /* 830 */ 45, 205, 47, 48, 49, 50, 51, 52, 53, 54,
174723 /* 840 */ 55, 56, 57, 58, 103, 104, 105, 106, 107, 108,
174724 /* 850 */ 109, 110, 111, 112, 113, 114, 154, 155, 156, 157,
174725 /* 860 */ 158, 60, 240, 241, 60, 116, 240, 241, 117, 118,
174726 /* 870 */ 119, 146, 194, 19, 81, 253, 275, 255, 24, 253,
174727 /* 880 */ 98, 255, 117, 118, 119, 183, 22, 194, 103, 104,
174728 /* 890 */ 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
174729 /* 900 */ 151, 194, 270, 152, 22, 23, 194, 25, 19, 202,
174730 /* 910 */ 217, 218, 23, 109, 207, 314, 315, 124, 117, 118,
174731 /* 920 */ 119, 117, 118, 119, 60, 232, 22, 23, 19, 25,
174732 /* 930 */ 303, 304, 43, 44, 45, 22, 47, 48, 49, 50,
174733 /* 940 */ 51, 52, 53, 54, 55, 56, 57, 58, 270, 227,
174734 /* 950 */ 96, 60, 43, 44, 45, 162, 47, 48, 49, 50,
174735 /* 960 */ 51, 52, 53, 54, 55, 56, 57, 58, 114, 112,
174736 /* 970 */ 113, 114, 194, 60, 120, 121, 122, 123, 124, 125,
174737 /* 980 */ 126, 117, 118, 119, 102, 25, 132, 107, 108, 194,
174738 /* 990 */ 194, 227, 103, 104, 105, 106, 107, 108, 109, 110,
174739 /* 1000 */ 111, 112, 113, 114, 194, 194, 102, 194, 117, 118,
174740 /* 1010 */ 119, 233, 103, 104, 105, 106, 107, 108, 109, 110,
174741 /* 1020 */ 111, 112, 113, 114, 194, 194, 19, 194, 217, 218,
174742 /* 1030 */ 117, 118, 119, 241, 154, 144, 156, 135, 242, 137,
174743 /* 1040 */ 138, 130, 19, 232, 194, 253, 23, 255, 217, 218,
174744 /* 1050 */ 217, 218, 242, 16, 7, 8, 9, 25, 261, 262,
174745 /* 1060 */ 265, 194, 19, 232, 153, 232, 43, 44, 45, 160,
174746 /* 1070 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
174747 /* 1080 */ 57, 58, 194, 227, 217, 218, 43, 44, 45, 194,
174748 /* 1090 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
174749 /* 1100 */ 57, 58, 306, 143, 308, 217, 218, 294, 12, 77,
174750 /* 1110 */ 22, 23, 217, 218, 194, 78, 306, 80, 308, 194,
174751 /* 1120 */ 232, 254, 90, 27, 117, 93, 103, 104, 105, 106,
174752 /* 1130 */ 107, 108, 109, 110, 111, 112, 113, 114, 42, 194,
174753 /* 1140 */ 35, 194, 217, 218, 214, 194, 103, 104, 105, 106,
174754 /* 1150 */ 107, 108, 109, 110, 111, 112, 113, 114, 194, 239,
174755 /* 1160 */ 64, 194, 217, 218, 217, 218, 209, 210, 217, 218,
174756 /* 1170 */ 74, 207, 67, 22, 194, 77, 19, 232, 245, 232,
174757 /* 1180 */ 75, 24, 312, 232, 217, 218, 316, 25, 90, 256,
174758 /* 1190 */ 102, 93, 159, 229, 161, 231, 19, 217, 218, 162,
174759 /* 1200 */ 43, 44, 45, 160, 47, 48, 49, 50, 51, 52,
174760 /* 1210 */ 53, 54, 55, 56, 57, 58, 19, 23, 288, 25,
174761 /* 1220 */ 43, 44, 45, 293, 47, 48, 49, 50, 51, 52,
174762 /* 1230 */ 53, 54, 55, 56, 57, 58, 131, 22, 275, 24,
174763 /* 1240 */ 43, 44, 45, 194, 47, 48, 49, 50, 51, 52,
174764 /* 1250 */ 53, 54, 55, 56, 57, 58, 194, 194, 107, 108,
174765 /* 1260 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
174766 /* 1270 */ 113, 114, 194, 25, 194, 60, 194, 314, 315, 194,
174767 /* 1280 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
174768 /* 1290 */ 113, 114, 19, 194, 194, 217, 218, 217, 218, 25,
174769 /* 1300 */ 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
174770 /* 1310 */ 113, 114, 150, 119, 265, 312, 67, 217, 218, 316,
174771 /* 1320 */ 19, 239, 194, 194, 128, 129, 130, 265, 265, 209,
174772 /* 1330 */ 210, 31, 254, 118, 254, 86, 60, 194, 144, 39,
174773 /* 1340 */ 19, 130, 275, 312, 95, 44, 45, 316, 47, 48,
174774 /* 1350 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
174775 /* 1360 */ 217, 218, 24, 194, 153, 12, 45, 119, 47, 48,
174776 /* 1370 */ 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
174777 /* 1380 */ 27, 314, 315, 22, 312, 24, 217, 218, 316, 116,
174778 /* 1390 */ 117, 22, 144, 25, 118, 42, 147, 194, 60, 19,
174779 /* 1400 */ 20, 127, 22, 194, 103, 104, 105, 106, 107, 108,
174780 /* 1410 */ 109, 110, 111, 112, 113, 114, 36, 64, 145, 194,
174781 /* 1420 */ 217, 218, 194, 54, 103, 104, 105, 106, 107, 108,
174782 /* 1430 */ 109, 110, 111, 112, 113, 114, 194, 22, 310, 25,
174783 /* 1440 */ 60, 194, 217, 218, 194, 217, 218, 194, 260, 194,
174784 /* 1450 */ 301, 302, 72, 262, 262, 194, 118, 266, 266, 217,
174785 /* 1460 */ 218, 194, 82, 245, 217, 218, 194, 217, 218, 194,
174786 /* 1470 */ 217, 218, 217, 218, 256, 60, 24, 194, 217, 218,
174787 /* 1480 */ 194, 101, 194, 245, 217, 218, 194, 107, 108, 217,
174788 /* 1490 */ 218, 22, 217, 218, 256, 115, 245, 117, 118, 119,
174789 /* 1500 */ 217, 218, 122, 217, 218, 217, 218, 256, 22, 217,
174790 /* 1510 */ 218, 116, 258, 259, 146, 19, 20, 194, 22, 139,
174791 /* 1520 */ 140, 150, 151, 136, 23, 194, 25, 194, 141, 194,
174792 /* 1530 */ 194, 62, 36, 118, 154, 155, 156, 157, 158, 100,
174793 /* 1540 */ 145, 194, 154, 194, 156, 49, 194, 23, 194, 25,
174794 /* 1550 */ 217, 218, 217, 218, 194, 257, 60, 143, 194, 60,
174795 /* 1560 */ 194, 121, 122, 183, 217, 218, 217, 218, 72, 217,
174796 /* 1570 */ 218, 217, 218, 134, 23, 194, 25, 217, 218, 7,
174797 /* 1580 */ 8, 321, 86, 217, 218, 194, 122, 91, 19, 20,
174798 /* 1590 */ 23, 22, 25, 23, 142, 25, 132, 101, 217, 218,
174799 /* 1600 */ 194, 194, 194, 107, 108, 36, 194, 194, 217, 218,
174800 /* 1610 */ 194, 115, 194, 117, 118, 119, 194, 118, 122, 194,
174801 /* 1620 */ 23, 194, 25, 217, 218, 217, 218, 194, 142, 60,
174802 /* 1630 */ 217, 218, 194, 217, 218, 217, 218, 84, 85, 217,
174803 /* 1640 */ 218, 72, 217, 218, 217, 218, 60, 23, 60, 25,
174804 /* 1650 */ 154, 155, 156, 157, 158, 86, 154, 194, 156, 194,
174805 /* 1660 */ 91, 19, 20, 23, 22, 25, 194, 23, 194, 25,
174806 /* 1670 */ 101, 23, 194, 25, 194, 194, 107, 108, 36, 183,
174807 /* 1680 */ 217, 218, 217, 218, 115, 194, 117, 118, 119, 217,
174808 /* 1690 */ 218, 122, 194, 237, 194, 217, 218, 217, 218, 194,
174809 /* 1700 */ 194, 23, 60, 25, 118, 23, 118, 25, 217, 218,
174810 /* 1710 */ 23, 194, 25, 194, 72, 217, 218, 217, 218, 23,
174811 /* 1720 */ 194, 25, 194, 154, 155, 156, 157, 158, 86, 23,
174812 /* 1730 */ 194, 25, 194, 91, 217, 218, 217, 218, 194, 194,
174813 /* 1740 */ 194, 194, 194, 101, 194, 217, 218, 290, 194, 107,
174814 /* 1750 */ 108, 194, 183, 217, 218, 194, 194, 115, 194, 117,
174815 /* 1760 */ 118, 119, 194, 243, 122, 194, 289, 194, 194, 194,
174816 /* 1770 */ 194, 257, 257, 257, 244, 257, 192, 273, 215, 269,
174817 /* 1780 */ 246, 299, 269, 295, 247, 273, 247, 246, 295, 230,
174818 /* 1790 */ 261, 226, 261, 261, 273, 273, 154, 155, 156, 157,
174819 /* 1800 */ 158, 0, 1, 2, 221, 220, 5, 220, 250, 220,
174820 /* 1810 */ 261, 10, 11, 12, 13, 14, 250, 282, 17, 197,
174821 /* 1820 */ 61, 19, 20, 246, 22, 183, 244, 142, 201, 201,
174822 /* 1830 */ 38, 30, 299, 32, 201, 152, 22, 151, 36, 299,
174823 /* 1840 */ 43, 40, 235, 18, 238, 285, 238, 238, 296, 238,
174824 /* 1850 */ 201, 296, 274, 272, 18, 200, 235, 150, 235, 247,
174825 /* 1860 */ 247, 247, 60, 247, 274, 274, 272, 201, 200, 159,
174826 /* 1870 */ 63, 292, 71, 201, 72, 200, 22, 201, 222, 200,
174827 /* 1880 */ 79, 201, 222, 82, 291, 116, 219, 65, 200, 219,
174828 /* 1890 */ 219, 228, 22, 127, 225, 166, 222, 24, 225, 114,
174829 /* 1900 */ 99, 284, 221, 101, 284, 228, 307, 315, 219, 107,
174830 /* 1910 */ 108, 219, 219, 219, 201, 92, 320, 115, 320, 117,
174831 /* 1920 */ 118, 119, 222, 83, 122, 267, 267, 149, 146, 19,
174832 /* 1930 */ 20, 22, 22, 279, 250, 134, 201, 148, 159, 249,
174833 /* 1940 */ 139, 140, 251, 141, 25, 281, 36, 147, 251, 248,
174834 /* 1950 */ 247, 203, 13, 195, 6, 195, 154, 155, 156, 157,
174835 /* 1960 */ 158, 193, 305, 193, 163, 193, 305, 302, 214, 208,
174836 /* 1970 */ 60, 214, 208, 223, 223, 4, 214, 214, 3, 22,
174837 /* 1980 */ 215, 214, 72, 208, 164, 183, 215, 15, 23, 16,
174838 /* 1990 */ 23, 140, 131, 152, 25, 143, 20, 16, 24, 1,
174839 /* 2000 */ 145, 143, 131, 131, 62, 37, 54, 54, 152, 54,
174840 /* 2010 */ 54, 101, 131, 117, 34, 1, 142, 107, 108, 22,
174841 /* 2020 */ 5, 116, 162, 69, 25, 115, 76, 117, 118, 119,
174842 /* 2030 */ 1, 2, 122, 69, 5, 41, 142, 116, 20, 10,
174843 /* 2040 */ 11, 12, 13, 14, 24, 19, 17, 132, 5, 126,
174844 /* 2050 */ 22, 141, 68, 10, 11, 12, 13, 14, 22, 30,
174845 /* 2060 */ 17, 32, 22, 22, 154, 155, 156, 157, 158, 40,
174846 /* 2070 */ 23, 68, 60, 30, 24, 32, 97, 28, 22, 68,
174847 /* 2080 */ 23, 37, 34, 40, 150, 22, 25, 23, 23, 23,
174848 /* 2090 */ 22, 98, 23, 183, 23, 117, 34, 22, 25, 89,
174849 /* 2100 */ 71, 142, 144, 76, 34, 23, 34, 76, 79, 87,
174850 /* 2110 */ 34, 82, 34, 94, 71, 34, 22, 24, 44, 34,
174851 /* 2120 */ 25, 25, 79, 23, 23, 82, 143, 23, 99, 143,
174852 /* 2130 */ 23, 22, 11, 25, 23, 25, 22, 22, 22, 1,
174853 /* 2140 */ 23, 23, 99, 136, 22, 22, 142, 142, 142, 25,
174854 /* 2150 */ 25, 23, 15, 1, 322, 322, 322, 322, 322, 322,
174855 /* 2160 */ 322, 322, 322, 134, 322, 322, 322, 322, 139, 140,
174856 /* 2170 */ 322, 322, 322, 322, 322, 322, 322, 134, 322, 322,
174857 /* 2180 */ 322, 322, 139, 140, 322, 322, 322, 322, 322, 322,
174858 /* 2190 */ 322, 322, 163, 322, 322, 322, 322, 322, 322, 322,
174859 /* 2200 */ 322, 322, 322, 322, 322, 322, 163, 322, 322, 322,
174860 /* 2210 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174861 /* 2220 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174862 /* 2230 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174863 /* 2240 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174864 /* 2250 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174865 /* 2260 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174866 /* 2270 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174867 /* 2280 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174868 /* 2290 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174869 /* 2300 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174870 /* 2310 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174871 /* 2320 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174872 /* 2330 */ 322, 322, 322, 322, 322, 322, 322, 322, 322, 322,
174873 /* 2340 */ 186, 186, 186, 186, 186, 186, 186, 186, 186, 186,
174874 /* 2350 */ 186, 186, 186, 186, 186, 186, 186, 186, 186, 186,
174875 /* 2360 */ 186, 186, 186, 186, 186, 186, 186, 186, 186, 186,
174876 /* 2370 */ 186, 186, 186, 186, 186, 186, 186, 186, 186, 186,
174877 /* 2380 */ 186, 186, 186, 186, 186, 186, 186, 186, 186, 186,
174878 /* 2390 */ 186, 186, 186,
174879};
174880#define YY_SHIFT_COUNT(582) (582)
174881#define YY_SHIFT_MIN(0) (0)
174882#define YY_SHIFT_MAX(2152) (2152)
174883static const unsigned short int yy_shift_ofst[] = {
174884 /* 0 */ 2029, 1801, 2043, 1380, 1380, 33, 391, 1496, 1569, 1642,
174885 /* 10 */ 702, 702, 702, 193, 33, 33, 33, 33, 33, 0,
174886 /* 20 */ 0, 216, 1177, 702, 702, 702, 702, 702, 702, 702,
174887 /* 30 */ 702, 702, 702, 702, 702, 702, 702, 702, 406, 406,
174888 /* 40 */ 111, 111, 218, 447, 547, 598, 598, 260, 260, 260,
174889 /* 50 */ 260, 40, 112, 320, 340, 445, 489, 593, 637, 741,
174890 /* 60 */ 785, 889, 909, 1023, 1043, 1157, 1177, 1177, 1177, 1177,
174891 /* 70 */ 1177, 1177, 1177, 1177, 1177, 1177, 1177, 1177, 1177, 1177,
174892 /* 80 */ 1177, 1177, 1177, 1177, 1197, 1177, 1301, 1321, 1321, 554,
174893 /* 90 */ 1802, 1910, 702, 702, 702, 702, 702, 702, 702, 702,
174894 /* 100 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
174895 /* 110 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
174896 /* 120 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
174897 /* 130 */ 702, 702, 702, 702, 702, 702, 702, 702, 702, 702,
174898 /* 140 */ 702, 702, 138, 198, 198, 198, 198, 198, 198, 198,
174899 /* 150 */ 183, 99, 236, 292, 598, 793, 167, 598, 598, 880,
174900 /* 160 */ 880, 598, 857, 150, 195, 195, 195, 264, 113, 113,
174901 /* 170 */ 2207, 2207, 854, 854, 854, 751, 765, 765, 765, 765,
174902 /* 180 */ 1096, 1096, 725, 292, 882, 904, 598, 598, 598, 598,
174903 /* 190 */ 598, 598, 598, 598, 598, 598, 598, 598, 598, 598,
174904 /* 200 */ 598, 598, 598, 598, 598, 1273, 1032, 1032, 598, 147,
174905 /* 210 */ 1098, 1098, 603, 603, 1276, 1276, 363, 2207, 2207, 2207,
174906 /* 220 */ 2207, 2207, 2207, 2207, 469, 617, 617, 801, 336, 461,
174907 /* 230 */ 804, 864, 615, 891, 913, 598, 598, 598, 598, 598,
174908 /* 240 */ 598, 598, 598, 598, 598, 653, 598, 598, 598, 598,
174909 /* 250 */ 598, 598, 598, 598, 598, 598, 598, 598, 1105, 1105,
174910 /* 260 */ 1105, 598, 598, 598, 1194, 598, 598, 598, 1215, 1249,
174911 /* 270 */ 598, 1353, 598, 598, 598, 598, 598, 598, 598, 598,
174912 /* 280 */ 677, 449, 902, 1338, 1338, 1338, 1338, 1248, 902, 902,
174913 /* 290 */ 326, 1151, 1047, 755, 749, 1371, 960, 1371, 1007, 1162,
174914 /* 300 */ 749, 749, 1162, 749, 960, 1007, 1274, 738, 215, 1300,
174915 /* 310 */ 1300, 1300, 1395, 1395, 1395, 1395, 1368, 1368, 1033, 1414,
174916 /* 320 */ 1387, 1361, 1759, 1759, 1685, 1685, 1792, 1792, 1685, 1683,
174917 /* 330 */ 1686, 1814, 1797, 1825, 1825, 1825, 1825, 1685, 1836, 1707,
174918 /* 340 */ 1686, 1686, 1707, 1814, 1797, 1707, 1797, 1707, 1685, 1836,
174919 /* 350 */ 1710, 1807, 1685, 1836, 1854, 1685, 1836, 1685, 1836, 1854,
174920 /* 360 */ 1769, 1769, 1769, 1822, 1870, 1870, 1854, 1769, 1766, 1769,
174921 /* 370 */ 1822, 1769, 1769, 1729, 1873, 1785, 1785, 1854, 1685, 1823,
174922 /* 380 */ 1823, 1840, 1840, 1778, 1782, 1909, 1685, 1779, 1778, 1789,
174923 /* 390 */ 1800, 1707, 1919, 1939, 1939, 1948, 1948, 1948, 2207, 2207,
174924 /* 400 */ 2207, 2207, 2207, 2207, 2207, 2207, 2207, 2207, 2207, 2207,
174925 /* 410 */ 2207, 2207, 2207, 69, 1037, 79, 1088, 651, 1196, 1415,
174926 /* 420 */ 1501, 1439, 1369, 1452, 911, 1211, 1524, 1469, 1551, 1567,
174927 /* 430 */ 1570, 1624, 1640, 1644, 1499, 1440, 1572, 1464, 1597, 275,
174928 /* 440 */ 782, 1586, 1648, 1678, 1553, 1682, 1687, 1388, 1502, 1696,
174929 /* 450 */ 1706, 1588, 1486, 1971, 1975, 1957, 1820, 1972, 1973, 1965,
174930 /* 460 */ 1967, 1851, 1841, 1861, 1969, 1969, 1974, 1852, 1976, 1855,
174931 /* 470 */ 1981, 1998, 1858, 1871, 1969, 1872, 1942, 1968, 1969, 1856,
174932 /* 480 */ 1952, 1953, 1955, 1956, 1881, 1896, 1980, 1874, 2014, 2015,
174933 /* 490 */ 1997, 1905, 1860, 1954, 1999, 1964, 1950, 1994, 1894, 1921,
174934 /* 500 */ 2020, 2018, 2026, 1915, 1923, 2028, 1984, 2036, 2040, 2047,
174935 /* 510 */ 2041, 2003, 2012, 2050, 1979, 2049, 2056, 2011, 2044, 2057,
174936 /* 520 */ 2048, 1934, 2063, 2064, 2065, 2061, 2066, 2068, 1993, 1959,
174937 /* 530 */ 2069, 2071, 1978, 2062, 2075, 1958, 2073, 2070, 2072, 2076,
174938 /* 540 */ 2078, 2010, 2027, 2022, 2074, 2031, 2019, 2081, 2082, 2094,
174939 /* 550 */ 2093, 2095, 2096, 2085, 1983, 1986, 2100, 2073, 2101, 2104,
174940 /* 560 */ 2107, 2109, 2108, 2110, 2111, 2114, 2121, 2115, 2116, 2117,
174941 /* 570 */ 2118, 2122, 2123, 2124, 2007, 2004, 2005, 2006, 2125, 2128,
174942 /* 580 */ 2137, 2138, 2152,
174943};
174944#define YY_REDUCE_COUNT(412) (412)
174945#define YY_REDUCE_MIN(-276) (-276)
174946#define YY_REDUCE_MAX(1775) (1775)
174947static const short yy_reduce_ofst[] = {
174948 /* 0 */ -66, 217, -63, -177, -180, 161, 364, 64, -183, 162,
174949 /* 10 */ 223, 367, 414, -173, 473, 514, 525, 622, 626, -207,
174950 /* 20 */ 351, -276, -38, 693, 811, 831, 833, 888, -188, 945,
174951 /* 30 */ 947, 416, 558, 951, 867, 287, 1078, 1080, -186, 224,
174952 /* 40 */ -132, 42, 964, 269, 417, 796, 810, -237, -231, -237,
174953 /* 50 */ -231, -45, -45, -45, -45, -45, -45, -45, -45, -45,
174954 /* 60 */ -45, -45, -45, -45, -45, -45, -45, -45, -45, -45,
174955 /* 70 */ -45, -45, -45, -45, -45, -45, -45, -45, -45, -45,
174956 /* 80 */ -45, -45, -45, -45, -45, -45, -45, -45, -45, 895,
174957 /* 90 */ 925, 967, 980, 1100, 1143, 1169, 1203, 1225, 1228, 1242,
174958 /* 100 */ 1247, 1250, 1253, 1255, 1261, 1267, 1272, 1275, 1283, 1286,
174959 /* 110 */ 1288, 1292, 1333, 1335, 1347, 1349, 1352, 1354, 1360, 1366,
174960 /* 120 */ 1381, 1391, 1406, 1408, 1413, 1416, 1418, 1422, 1425, 1427,
174961 /* 130 */ 1463, 1465, 1472, 1478, 1480, 1491, 1498, 1500, 1517, 1519,
174962 /* 140 */ 1528, 1536, -45, -45, -45, -45, -45, -45, -45, -45,
174963 /* 150 */ -45, -45, -45, 312, -158, 285, -219, 9, 166, 370,
174964 /* 160 */ 545, 707, -45, 930, 601, 963, 1067, 792, -45, -45,
174965 /* 170 */ -45, -45, -204, -204, -204, 369, -171, -129, 632, 678,
174966 /* 180 */ 202, 352, -270, 412, 627, 627, -9, 122, 415, 419,
174967 /* 190 */ -56, 248, 583, 920, 6, 261, 459, 795, 1049, 813,
174968 /* 200 */ 1062, 1082, -161, 778, 1063, 797, 870, 1003, 1128, 443,
174969 /* 210 */ 1031, 1072, 1191, 1192, 957, 1120, 105, 1149, 523, 933,
174970 /* 220 */ 1218, 1238, 1254, 1251, -138, 96, 117, 146, 181, 277,
174971 /* 230 */ 280, 421, 480, 712, 830, 850, 1085, 1099, 1129, 1209,
174972 /* 240 */ 1323, 1331, 1336, 1364, 1407, 368, 1412, 1433, 1438, 1474,
174973 /* 250 */ 1481, 1505, 1506, 1526, 1538, 1544, 1545, 1546, 722, 764,
174974 /* 260 */ 856, 1547, 1548, 1550, 1188, 1554, 1557, 1561, 1298, 1260,
174975 /* 270 */ 1562, 1456, 1564, 280, 1568, 1571, 1573, 1574, 1575, 1576,
174976 /* 280 */ 1457, 1477, 1520, 1514, 1515, 1516, 1518, 1188, 1520, 1520,
174977 /* 290 */ 1530, 1563, 1584, 1482, 1504, 1510, 1534, 1513, 1488, 1537,
174978 /* 300 */ 1512, 1521, 1539, 1522, 1541, 1493, 1583, 1559, 1565, 1585,
174979 /* 310 */ 1587, 1589, 1529, 1531, 1532, 1549, 1558, 1566, 1535, 1577,
174980 /* 320 */ 1582, 1622, 1533, 1540, 1627, 1628, 1552, 1555, 1633, 1560,
174981 /* 330 */ 1578, 1581, 1607, 1606, 1608, 1609, 1611, 1649, 1655, 1612,
174982 /* 340 */ 1590, 1591, 1613, 1594, 1621, 1614, 1623, 1616, 1666, 1668,
174983 /* 350 */ 1579, 1593, 1672, 1675, 1656, 1676, 1679, 1680, 1688, 1660,
174984 /* 360 */ 1667, 1670, 1671, 1663, 1669, 1673, 1674, 1689, 1681, 1692,
174985 /* 370 */ 1677, 1693, 1694, 1592, 1599, 1617, 1620, 1700, 1713, 1596,
174986 /* 380 */ 1598, 1658, 1659, 1691, 1684, 1654, 1735, 1664, 1697, 1690,
174987 /* 390 */ 1701, 1703, 1748, 1758, 1760, 1768, 1770, 1772, 1657, 1661,
174988 /* 400 */ 1665, 1761, 1754, 1757, 1762, 1763, 1764, 1750, 1751, 1765,
174989 /* 410 */ 1771, 1767, 1775,
174990};
174991static const YYACTIONTYPEunsigned short int yy_default[] = {
174992 /* 0 */ 1663, 1663, 1663, 1491, 1254, 1367, 1254, 1254, 1254, 1254,
174993 /* 10 */ 1491, 1491, 1491, 1254, 1254, 1254, 1254, 1254, 1254, 1397,
174994 /* 20 */ 1397, 1544, 1287, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
174995 /* 30 */ 1254, 1254, 1254, 1254, 1254, 1490, 1254, 1254, 1254, 1254,
174996 /* 40 */ 1578, 1578, 1254, 1254, 1254, 1254, 1254, 1563, 1562, 1254,
174997 /* 50 */ 1254, 1254, 1406, 1254, 1413, 1254, 1254, 1254, 1254, 1254,
174998 /* 60 */ 1492, 1493, 1254, 1254, 1254, 1254, 1543, 1545, 1508, 1420,
174999 /* 70 */ 1419, 1418, 1417, 1526, 1385, 1411, 1404, 1408, 1487, 1488,
175000 /* 80 */ 1486, 1641, 1493, 1492, 1254, 1407, 1455, 1471, 1454, 1254,
175001 /* 90 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175002 /* 100 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175003 /* 110 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175004 /* 120 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175005 /* 130 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175006 /* 140 */ 1254, 1254, 1463, 1470, 1469, 1468, 1477, 1467, 1464, 1457,
175007 /* 150 */ 1456, 1458, 1459, 1278, 1254, 1275, 1329, 1254, 1254, 1254,
175008 /* 160 */ 1254, 1254, 1460, 1287, 1448, 1447, 1446, 1254, 1474, 1461,
175009 /* 170 */ 1473, 1472, 1551, 1615, 1614, 1509, 1254, 1254, 1254, 1254,
175010 /* 180 */ 1254, 1254, 1578, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175011 /* 190 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175012 /* 200 */ 1254, 1254, 1254, 1254, 1254, 1387, 1578, 1578, 1254, 1287,
175013 /* 210 */ 1578, 1578, 1388, 1388, 1283, 1283, 1391, 1558, 1358, 1358,
175014 /* 220 */ 1358, 1358, 1367, 1358, 1254, 1254, 1254, 1254, 1254, 1254,
175015 /* 230 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1548,
175016 /* 240 */ 1546, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175017 /* 250 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175018 /* 260 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1363, 1254,
175019 /* 270 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1608,
175020 /* 280 */ 1254, 1521, 1343, 1363, 1363, 1363, 1363, 1365, 1344, 1342,
175021 /* 290 */ 1357, 1288, 1261, 1655, 1423, 1412, 1364, 1412, 1652, 1410,
175022 /* 300 */ 1423, 1423, 1410, 1423, 1364, 1652, 1304, 1630, 1299, 1397,
175023 /* 310 */ 1397, 1397, 1387, 1387, 1387, 1387, 1391, 1391, 1489, 1364,
175024 /* 320 */ 1357, 1254, 1655, 1655, 1373, 1373, 1654, 1654, 1373, 1509,
175025 /* 330 */ 1638, 1432, 1332, 1338, 1338, 1338, 1338, 1373, 1272, 1410,
175026 /* 340 */ 1638, 1638, 1410, 1432, 1332, 1410, 1332, 1410, 1373, 1272,
175027 /* 350 */ 1525, 1649, 1373, 1272, 1499, 1373, 1272, 1373, 1272, 1499,
175028 /* 360 */ 1330, 1330, 1330, 1319, 1254, 1254, 1499, 1330, 1304, 1330,
175029 /* 370 */ 1319, 1330, 1330, 1596, 1254, 1503, 1503, 1499, 1373, 1588,
175030 /* 380 */ 1588, 1400, 1400, 1405, 1391, 1494, 1373, 1254, 1405, 1403,
175031 /* 390 */ 1401, 1410, 1322, 1611, 1611, 1607, 1607, 1607, 1660, 1660,
175032 /* 400 */ 1558, 1623, 1287, 1287, 1287, 1287, 1623, 1306, 1306, 1288,
175033 /* 410 */ 1288, 1287, 1623, 1254, 1254, 1254, 1254, 1254, 1254, 1618,
175034 /* 420 */ 1254, 1553, 1510, 1377, 1254, 1254, 1254, 1254, 1254, 1254,
175035 /* 430 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175036 /* 440 */ 1564, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175037 /* 450 */ 1254, 1254, 1437, 1254, 1257, 1555, 1254, 1254, 1254, 1254,
175038 /* 460 */ 1254, 1254, 1254, 1254, 1414, 1415, 1378, 1254, 1254, 1254,
175039 /* 470 */ 1254, 1254, 1254, 1254, 1429, 1254, 1254, 1254, 1424, 1254,
175040 /* 480 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1651, 1254, 1254,
175041 /* 490 */ 1254, 1254, 1254, 1254, 1524, 1523, 1254, 1254, 1375, 1254,
175042 /* 500 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175043 /* 510 */ 1254, 1254, 1302, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175044 /* 520 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175045 /* 530 */ 1254, 1254, 1254, 1254, 1254, 1254, 1402, 1254, 1254, 1254,
175046 /* 540 */ 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175047 /* 550 */ 1254, 1593, 1392, 1254, 1254, 1254, 1254, 1642, 1254, 1254,
175048 /* 560 */ 1254, 1254, 1352, 1254, 1254, 1254, 1254, 1254, 1254, 1254,
175049 /* 570 */ 1254, 1254, 1254, 1634, 1346, 1438, 1254, 1441, 1276, 1254,
175050 /* 580 */ 1266, 1254, 1254,
175051};
175052/********** End of lemon-generated parsing tables *****************************/
175053
175054/* The next table maps tokens (terminal symbols) into fallback tokens.
175055** If a construct like the following:
175056**
175057** %fallback ID X Y Z.
175058**
175059** appears in the grammar, then ID becomes a fallback token for X, Y,
175060** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
175061** but it does not parse, the type of the token is changed to ID and
175062** the parse is retried before an error is thrown.
175063**
175064** This feature can be used, for example, to cause some keywords in a language
175065** to revert to identifiers if they keyword does not apply in the context where
175066** it appears.
175067*/
175068#ifdef YYFALLBACK1
175069static const YYCODETYPEunsigned short int yyFallback[] = {
175070 0, /* $ => nothing */
175071 0, /* SEMI => nothing */
175072 60, /* EXPLAIN => ID */
175073 60, /* QUERY => ID */
175074 60, /* PLAN => ID */
175075 60, /* BEGIN => ID */
175076 0, /* TRANSACTION => nothing */
175077 60, /* DEFERRED => ID */
175078 60, /* IMMEDIATE => ID */
175079 60, /* EXCLUSIVE => ID */
175080 0, /* COMMIT => nothing */
175081 60, /* END => ID */
175082 60, /* ROLLBACK => ID */
175083 60, /* SAVEPOINT => ID */
175084 60, /* RELEASE => ID */
175085 0, /* TO => nothing */
175086 0, /* TABLE => nothing */
175087 0, /* CREATE => nothing */
175088 60, /* IF => ID */
175089 0, /* NOT => nothing */
175090 0, /* EXISTS => nothing */
175091 60, /* TEMP => ID */
175092 0, /* LP => nothing */
175093 0, /* RP => nothing */
175094 0, /* AS => nothing */
175095 0, /* COMMA => nothing */
175096 60, /* WITHOUT => ID */
175097 60, /* ABORT => ID */
175098 60, /* ACTION => ID */
175099 60, /* AFTER => ID */
175100 60, /* ANALYZE => ID */
175101 60, /* ASC => ID */
175102 60, /* ATTACH => ID */
175103 60, /* BEFORE => ID */
175104 60, /* BY => ID */
175105 60, /* CASCADE => ID */
175106 60, /* CAST => ID */
175107 60, /* CONFLICT => ID */
175108 60, /* DATABASE => ID */
175109 60, /* DESC => ID */
175110 60, /* DETACH => ID */
175111 60, /* EACH => ID */
175112 60, /* FAIL => ID */
175113 0, /* OR => nothing */
175114 0, /* AND => nothing */
175115 0, /* IS => nothing */
175116 0, /* ISNOT => nothing */
175117 60, /* MATCH => ID */
175118 60, /* LIKE_KW => ID */
175119 0, /* BETWEEN => nothing */
175120 0, /* IN => nothing */
175121 0, /* ISNULL => nothing */
175122 0, /* NOTNULL => nothing */
175123 0, /* NE => nothing */
175124 0, /* EQ => nothing */
175125 0, /* GT => nothing */
175126 0, /* LE => nothing */
175127 0, /* LT => nothing */
175128 0, /* GE => nothing */
175129 0, /* ESCAPE => nothing */
175130 0, /* ID => nothing */
175131 60, /* COLUMNKW => ID */
175132 60, /* DO => ID */
175133 60, /* FOR => ID */
175134 60, /* IGNORE => ID */
175135 60, /* INITIALLY => ID */
175136 60, /* INSTEAD => ID */
175137 60, /* NO => ID */
175138 60, /* KEY => ID */
175139 60, /* OF => ID */
175140 60, /* OFFSET => ID */
175141 60, /* PRAGMA => ID */
175142 60, /* RAISE => ID */
175143 60, /* RECURSIVE => ID */
175144 60, /* REPLACE => ID */
175145 60, /* RESTRICT => ID */
175146 60, /* ROW => ID */
175147 60, /* ROWS => ID */
175148 60, /* TRIGGER => ID */
175149 60, /* VACUUM => ID */
175150 60, /* VIEW => ID */
175151 60, /* VIRTUAL => ID */
175152 60, /* WITH => ID */
175153 60, /* NULLS => ID */
175154 60, /* FIRST => ID */
175155 60, /* LAST => ID */
175156 60, /* CURRENT => ID */
175157 60, /* FOLLOWING => ID */
175158 60, /* PARTITION => ID */
175159 60, /* PRECEDING => ID */
175160 60, /* RANGE => ID */
175161 60, /* UNBOUNDED => ID */
175162 60, /* EXCLUDE => ID */
175163 60, /* GROUPS => ID */
175164 60, /* OTHERS => ID */
175165 60, /* TIES => ID */
175166 60, /* GENERATED => ID */
175167 60, /* ALWAYS => ID */
175168 60, /* MATERIALIZED => ID */
175169 60, /* REINDEX => ID */
175170 60, /* RENAME => ID */
175171 60, /* CTIME_KW => ID */
175172 0, /* ANY => nothing */
175173 0, /* BITAND => nothing */
175174 0, /* BITOR => nothing */
175175 0, /* LSHIFT => nothing */
175176 0, /* RSHIFT => nothing */
175177 0, /* PLUS => nothing */
175178 0, /* MINUS => nothing */
175179 0, /* STAR => nothing */
175180 0, /* SLASH => nothing */
175181 0, /* REM => nothing */
175182 0, /* CONCAT => nothing */
175183 0, /* PTR => nothing */
175184 0, /* COLLATE => nothing */
175185 0, /* BITNOT => nothing */
175186 0, /* ON => nothing */
175187 0, /* INDEXED => nothing */
175188 0, /* STRING => nothing */
175189 0, /* JOIN_KW => nothing */
175190 0, /* CONSTRAINT => nothing */
175191 0, /* DEFAULT => nothing */
175192 0, /* NULL => nothing */
175193 0, /* PRIMARY => nothing */
175194 0, /* UNIQUE => nothing */
175195 0, /* CHECK => nothing */
175196 0, /* REFERENCES => nothing */
175197 0, /* AUTOINCR => nothing */
175198 0, /* INSERT => nothing */
175199 0, /* DELETE => nothing */
175200 0, /* UPDATE => nothing */
175201 0, /* SET => nothing */
175202 0, /* DEFERRABLE => nothing */
175203 0, /* FOREIGN => nothing */
175204 0, /* DROP => nothing */
175205 0, /* UNION => nothing */
175206 0, /* ALL => nothing */
175207 0, /* EXCEPT => nothing */
175208 0, /* INTERSECT => nothing */
175209 0, /* SELECT => nothing */
175210 0, /* VALUES => nothing */
175211 0, /* DISTINCT => nothing */
175212 0, /* DOT => nothing */
175213 0, /* FROM => nothing */
175214 0, /* JOIN => nothing */
175215 0, /* USING => nothing */
175216 0, /* ORDER => nothing */
175217 0, /* GROUP => nothing */
175218 0, /* HAVING => nothing */
175219 0, /* LIMIT => nothing */
175220 0, /* WHERE => nothing */
175221 0, /* RETURNING => nothing */
175222 0, /* INTO => nothing */
175223 0, /* NOTHING => nothing */
175224 0, /* FLOAT => nothing */
175225 0, /* BLOB => nothing */
175226 0, /* INTEGER => nothing */
175227 0, /* VARIABLE => nothing */
175228 0, /* CASE => nothing */
175229 0, /* WHEN => nothing */
175230 0, /* THEN => nothing */
175231 0, /* ELSE => nothing */
175232 0, /* INDEX => nothing */
175233 0, /* ALTER => nothing */
175234 0, /* ADD => nothing */
175235 0, /* WINDOW => nothing */
175236 0, /* OVER => nothing */
175237 0, /* FILTER => nothing */
175238 0, /* COLUMN => nothing */
175239 0, /* AGG_FUNCTION => nothing */
175240 0, /* AGG_COLUMN => nothing */
175241 0, /* TRUEFALSE => nothing */
175242 0, /* FUNCTION => nothing */
175243 0, /* UPLUS => nothing */
175244 0, /* UMINUS => nothing */
175245 0, /* TRUTH => nothing */
175246 0, /* REGISTER => nothing */
175247 0, /* VECTOR => nothing */
175248 0, /* SELECT_COLUMN => nothing */
175249 0, /* IF_NULL_ROW => nothing */
175250 0, /* ASTERISK => nothing */
175251 0, /* SPAN => nothing */
175252 0, /* ERROR => nothing */
175253 0, /* QNUMBER => nothing */
175254 0, /* SPACE => nothing */
175255 0, /* ILLEGAL => nothing */
175256};
175257#endif /* YYFALLBACK */
175258
175259/* The following structure represents a single element of the
175260** parser's stack. Information stored includes:
175261**
175262** + The state number for the parser at this level of the stack.
175263**
175264** + The value of the token stored at this level of the stack.
175265** (In other words, the "major" token.)
175266**
175267** + The semantic value stored at this level of the stack. This is
175268** the information used by the action routines in the grammar.
175269** It is sometimes called the "minor" token.
175270**
175271** After the "shift" half of a SHIFTREDUCE action, the stateno field
175272** actually contains the reduce action for the second half of the
175273** SHIFTREDUCE.
175274*/
175275struct yyStackEntry {
175276 YYACTIONTYPEunsigned short int stateno; /* The state-number, or reduce action in SHIFTREDUCE */
175277 YYCODETYPEunsigned short int major; /* The major token value. This is the code
175278 ** number for the token at this stack level */
175279 YYMINORTYPE minor; /* The user-supplied minor token value. This
175280 ** is the value of the token */
175281};
175282typedef struct yyStackEntry yyStackEntry;
175283
175284/* The state of the parser is completely contained in an instance of
175285** the following structure */
175286struct yyParser {
175287 yyStackEntry *yytos; /* Pointer to top element of the stack */
175288#ifdef YYTRACKMAXSTACKDEPTH
175289 int yyhwm; /* High-water mark of the stack */
175290#endif
175291#ifndef YYNOERRORRECOVERY1
175292 int yyerrcnt; /* Shifts left before out of the error */
175293#endif
175294 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
175295 sqlite3ParserCTX_SDECLParse *pParse; /* A place to hold %extra_context */
175296 yyStackEntry *yystackEnd; /* Last entry in the stack */
175297 yyStackEntry *yystack; /* The parser stack */
175298 yyStackEntry yystk0[YYSTACKDEPTH100]; /* Initial stack space */
175299};
175300typedef struct yyParser yyParser;
175301
175302/* #include <assert.h> */
175303#ifndef NDEBUG1
175304/* #include <stdio.h> */
175305static FILE *yyTraceFILE = 0;
175306static char *yyTracePrompt = 0;
175307#endif /* NDEBUG */
175308
175309#ifndef NDEBUG1
175310/*
175311** Turn parser tracing on by giving a stream to which to write the trace
175312** and a prompt to preface each trace message. Tracing is turned off
175313** by making either argument NULL
175314**
175315** Inputs:
175316** <ul>
175317** <li> A FILE* to which trace output should be written.
175318** If NULL, then tracing is turned off.
175319** <li> A prefix string written at the beginning of every
175320** line of trace output. If NULL, then tracing is
175321** turned off.
175322** </ul>
175323**
175324** Outputs:
175325** None.
175326*/
175327SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
175328 yyTraceFILE = TraceFILE;
175329 yyTracePrompt = zTracePrompt;
175330 if( yyTraceFILE==0 ) yyTracePrompt = 0;
175331 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
175332}
175333#endif /* NDEBUG */
175334
175335#if defined(YYCOVERAGE) || !defined(NDEBUG1)
175336/* For tracing shifts, the names of all terminals and nonterminals
175337** are required. The following table supplies these names */
175338static const char *const yyTokenName[] = {
175339 /* 0 */ "$",
175340 /* 1 */ "SEMI",
175341 /* 2 */ "EXPLAIN",
175342 /* 3 */ "QUERY",
175343 /* 4 */ "PLAN",
175344 /* 5 */ "BEGIN",
175345 /* 6 */ "TRANSACTION",
175346 /* 7 */ "DEFERRED",
175347 /* 8 */ "IMMEDIATE",
175348 /* 9 */ "EXCLUSIVE",
175349 /* 10 */ "COMMIT",
175350 /* 11 */ "END",
175351 /* 12 */ "ROLLBACK",
175352 /* 13 */ "SAVEPOINT",
175353 /* 14 */ "RELEASE",
175354 /* 15 */ "TO",
175355 /* 16 */ "TABLE",
175356 /* 17 */ "CREATE",
175357 /* 18 */ "IF",
175358 /* 19 */ "NOT",
175359 /* 20 */ "EXISTS",
175360 /* 21 */ "TEMP",
175361 /* 22 */ "LP",
175362 /* 23 */ "RP",
175363 /* 24 */ "AS",
175364 /* 25 */ "COMMA",
175365 /* 26 */ "WITHOUT",
175366 /* 27 */ "ABORT",
175367 /* 28 */ "ACTION",
175368 /* 29 */ "AFTER",
175369 /* 30 */ "ANALYZE",
175370 /* 31 */ "ASC",
175371 /* 32 */ "ATTACH",
175372 /* 33 */ "BEFORE",
175373 /* 34 */ "BY",
175374 /* 35 */ "CASCADE",
175375 /* 36 */ "CAST",
175376 /* 37 */ "CONFLICT",
175377 /* 38 */ "DATABASE",
175378 /* 39 */ "DESC",
175379 /* 40 */ "DETACH",
175380 /* 41 */ "EACH",
175381 /* 42 */ "FAIL",
175382 /* 43 */ "OR",
175383 /* 44 */ "AND",
175384 /* 45 */ "IS",
175385 /* 46 */ "ISNOT",
175386 /* 47 */ "MATCH",
175387 /* 48 */ "LIKE_KW",
175388 /* 49 */ "BETWEEN",
175389 /* 50 */ "IN",
175390 /* 51 */ "ISNULL",
175391 /* 52 */ "NOTNULL",
175392 /* 53 */ "NE",
175393 /* 54 */ "EQ",
175394 /* 55 */ "GT",
175395 /* 56 */ "LE",
175396 /* 57 */ "LT",
175397 /* 58 */ "GE",
175398 /* 59 */ "ESCAPE",
175399 /* 60 */ "ID",
175400 /* 61 */ "COLUMNKW",
175401 /* 62 */ "DO",
175402 /* 63 */ "FOR",
175403 /* 64 */ "IGNORE",
175404 /* 65 */ "INITIALLY",
175405 /* 66 */ "INSTEAD",
175406 /* 67 */ "NO",
175407 /* 68 */ "KEY",
175408 /* 69 */ "OF",
175409 /* 70 */ "OFFSET",
175410 /* 71 */ "PRAGMA",
175411 /* 72 */ "RAISE",
175412 /* 73 */ "RECURSIVE",
175413 /* 74 */ "REPLACE",
175414 /* 75 */ "RESTRICT",
175415 /* 76 */ "ROW",
175416 /* 77 */ "ROWS",
175417 /* 78 */ "TRIGGER",
175418 /* 79 */ "VACUUM",
175419 /* 80 */ "VIEW",
175420 /* 81 */ "VIRTUAL",
175421 /* 82 */ "WITH",
175422 /* 83 */ "NULLS",
175423 /* 84 */ "FIRST",
175424 /* 85 */ "LAST",
175425 /* 86 */ "CURRENT",
175426 /* 87 */ "FOLLOWING",
175427 /* 88 */ "PARTITION",
175428 /* 89 */ "PRECEDING",
175429 /* 90 */ "RANGE",
175430 /* 91 */ "UNBOUNDED",
175431 /* 92 */ "EXCLUDE",
175432 /* 93 */ "GROUPS",
175433 /* 94 */ "OTHERS",
175434 /* 95 */ "TIES",
175435 /* 96 */ "GENERATED",
175436 /* 97 */ "ALWAYS",
175437 /* 98 */ "MATERIALIZED",
175438 /* 99 */ "REINDEX",
175439 /* 100 */ "RENAME",
175440 /* 101 */ "CTIME_KW",
175441 /* 102 */ "ANY",
175442 /* 103 */ "BITAND",
175443 /* 104 */ "BITOR",
175444 /* 105 */ "LSHIFT",
175445 /* 106 */ "RSHIFT",
175446 /* 107 */ "PLUS",
175447 /* 108 */ "MINUS",
175448 /* 109 */ "STAR",
175449 /* 110 */ "SLASH",
175450 /* 111 */ "REM",
175451 /* 112 */ "CONCAT",
175452 /* 113 */ "PTR",
175453 /* 114 */ "COLLATE",
175454 /* 115 */ "BITNOT",
175455 /* 116 */ "ON",
175456 /* 117 */ "INDEXED",
175457 /* 118 */ "STRING",
175458 /* 119 */ "JOIN_KW",
175459 /* 120 */ "CONSTRAINT",
175460 /* 121 */ "DEFAULT",
175461 /* 122 */ "NULL",
175462 /* 123 */ "PRIMARY",
175463 /* 124 */ "UNIQUE",
175464 /* 125 */ "CHECK",
175465 /* 126 */ "REFERENCES",
175466 /* 127 */ "AUTOINCR",
175467 /* 128 */ "INSERT",
175468 /* 129 */ "DELETE",
175469 /* 130 */ "UPDATE",
175470 /* 131 */ "SET",
175471 /* 132 */ "DEFERRABLE",
175472 /* 133 */ "FOREIGN",
175473 /* 134 */ "DROP",
175474 /* 135 */ "UNION",
175475 /* 136 */ "ALL",
175476 /* 137 */ "EXCEPT",
175477 /* 138 */ "INTERSECT",
175478 /* 139 */ "SELECT",
175479 /* 140 */ "VALUES",
175480 /* 141 */ "DISTINCT",
175481 /* 142 */ "DOT",
175482 /* 143 */ "FROM",
175483 /* 144 */ "JOIN",
175484 /* 145 */ "USING",
175485 /* 146 */ "ORDER",
175486 /* 147 */ "GROUP",
175487 /* 148 */ "HAVING",
175488 /* 149 */ "LIMIT",
175489 /* 150 */ "WHERE",
175490 /* 151 */ "RETURNING",
175491 /* 152 */ "INTO",
175492 /* 153 */ "NOTHING",
175493 /* 154 */ "FLOAT",
175494 /* 155 */ "BLOB",
175495 /* 156 */ "INTEGER",
175496 /* 157 */ "VARIABLE",
175497 /* 158 */ "CASE",
175498 /* 159 */ "WHEN",
175499 /* 160 */ "THEN",
175500 /* 161 */ "ELSE",
175501 /* 162 */ "INDEX",
175502 /* 163 */ "ALTER",
175503 /* 164 */ "ADD",
175504 /* 165 */ "WINDOW",
175505 /* 166 */ "OVER",
175506 /* 167 */ "FILTER",
175507 /* 168 */ "COLUMN",
175508 /* 169 */ "AGG_FUNCTION",
175509 /* 170 */ "AGG_COLUMN",
175510 /* 171 */ "TRUEFALSE",
175511 /* 172 */ "FUNCTION",
175512 /* 173 */ "UPLUS",
175513 /* 174 */ "UMINUS",
175514 /* 175 */ "TRUTH",
175515 /* 176 */ "REGISTER",
175516 /* 177 */ "VECTOR",
175517 /* 178 */ "SELECT_COLUMN",
175518 /* 179 */ "IF_NULL_ROW",
175519 /* 180 */ "ASTERISK",
175520 /* 181 */ "SPAN",
175521 /* 182 */ "ERROR",
175522 /* 183 */ "QNUMBER",
175523 /* 184 */ "SPACE",
175524 /* 185 */ "ILLEGAL",
175525 /* 186 */ "input",
175526 /* 187 */ "cmdlist",
175527 /* 188 */ "ecmd",
175528 /* 189 */ "cmdx",
175529 /* 190 */ "explain",
175530 /* 191 */ "cmd",
175531 /* 192 */ "transtype",
175532 /* 193 */ "trans_opt",
175533 /* 194 */ "nm",
175534 /* 195 */ "savepoint_opt",
175535 /* 196 */ "create_table",
175536 /* 197 */ "create_table_args",
175537 /* 198 */ "createkw",
175538 /* 199 */ "temp",
175539 /* 200 */ "ifnotexists",
175540 /* 201 */ "dbnm",
175541 /* 202 */ "columnlist",
175542 /* 203 */ "conslist_opt",
175543 /* 204 */ "table_option_set",
175544 /* 205 */ "select",
175545 /* 206 */ "table_option",
175546 /* 207 */ "columnname",
175547 /* 208 */ "carglist",
175548 /* 209 */ "typetoken",
175549 /* 210 */ "typename",
175550 /* 211 */ "signed",
175551 /* 212 */ "plus_num",
175552 /* 213 */ "minus_num",
175553 /* 214 */ "scanpt",
175554 /* 215 */ "scantok",
175555 /* 216 */ "ccons",
175556 /* 217 */ "term",
175557 /* 218 */ "expr",
175558 /* 219 */ "onconf",
175559 /* 220 */ "sortorder",
175560 /* 221 */ "autoinc",
175561 /* 222 */ "eidlist_opt",
175562 /* 223 */ "refargs",
175563 /* 224 */ "defer_subclause",
175564 /* 225 */ "generated",
175565 /* 226 */ "refarg",
175566 /* 227 */ "refact",
175567 /* 228 */ "init_deferred_pred_opt",
175568 /* 229 */ "conslist",
175569 /* 230 */ "tconscomma",
175570 /* 231 */ "tcons",
175571 /* 232 */ "sortlist",
175572 /* 233 */ "eidlist",
175573 /* 234 */ "defer_subclause_opt",
175574 /* 235 */ "orconf",
175575 /* 236 */ "resolvetype",
175576 /* 237 */ "raisetype",
175577 /* 238 */ "ifexists",
175578 /* 239 */ "fullname",
175579 /* 240 */ "selectnowith",
175580 /* 241 */ "oneselect",
175581 /* 242 */ "wqlist",
175582 /* 243 */ "multiselect_op",
175583 /* 244 */ "distinct",
175584 /* 245 */ "selcollist",
175585 /* 246 */ "from",
175586 /* 247 */ "where_opt",
175587 /* 248 */ "groupby_opt",
175588 /* 249 */ "having_opt",
175589 /* 250 */ "orderby_opt",
175590 /* 251 */ "limit_opt",
175591 /* 252 */ "window_clause",
175592 /* 253 */ "values",
175593 /* 254 */ "nexprlist",
175594 /* 255 */ "mvalues",
175595 /* 256 */ "sclp",
175596 /* 257 */ "as",
175597 /* 258 */ "seltablist",
175598 /* 259 */ "stl_prefix",
175599 /* 260 */ "joinop",
175600 /* 261 */ "on_using",
175601 /* 262 */ "indexed_by",
175602 /* 263 */ "exprlist",
175603 /* 264 */ "xfullname",
175604 /* 265 */ "idlist",
175605 /* 266 */ "indexed_opt",
175606 /* 267 */ "nulls",
175607 /* 268 */ "with",
175608 /* 269 */ "where_opt_ret",
175609 /* 270 */ "setlist",
175610 /* 271 */ "insert_cmd",
175611 /* 272 */ "idlist_opt",
175612 /* 273 */ "upsert",
175613 /* 274 */ "returning",
175614 /* 275 */ "filter_over",
175615 /* 276 */ "likeop",
175616 /* 277 */ "between_op",
175617 /* 278 */ "in_op",
175618 /* 279 */ "paren_exprlist",
175619 /* 280 */ "case_operand",
175620 /* 281 */ "case_exprlist",
175621 /* 282 */ "case_else",
175622 /* 283 */ "uniqueflag",
175623 /* 284 */ "collate",
175624 /* 285 */ "vinto",
175625 /* 286 */ "nmnum",
175626 /* 287 */ "trigger_decl",
175627 /* 288 */ "trigger_cmd_list",
175628 /* 289 */ "trigger_time",
175629 /* 290 */ "trigger_event",
175630 /* 291 */ "foreach_clause",
175631 /* 292 */ "when_clause",
175632 /* 293 */ "trigger_cmd",
175633 /* 294 */ "trnm",
175634 /* 295 */ "tridxby",
175635 /* 296 */ "database_kw_opt",
175636 /* 297 */ "key_opt",
175637 /* 298 */ "add_column_fullname",
175638 /* 299 */ "kwcolumn_opt",
175639 /* 300 */ "create_vtab",
175640 /* 301 */ "vtabarglist",
175641 /* 302 */ "vtabarg",
175642 /* 303 */ "vtabargtoken",
175643 /* 304 */ "lp",
175644 /* 305 */ "anylist",
175645 /* 306 */ "wqitem",
175646 /* 307 */ "wqas",
175647 /* 308 */ "withnm",
175648 /* 309 */ "windowdefn_list",
175649 /* 310 */ "windowdefn",
175650 /* 311 */ "window",
175651 /* 312 */ "frame_opt",
175652 /* 313 */ "part_opt",
175653 /* 314 */ "filter_clause",
175654 /* 315 */ "over_clause",
175655 /* 316 */ "range_or_rows",
175656 /* 317 */ "frame_bound",
175657 /* 318 */ "frame_bound_s",
175658 /* 319 */ "frame_bound_e",
175659 /* 320 */ "frame_exclude_opt",
175660 /* 321 */ "frame_exclude",
175661};
175662#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
175663
175664#ifndef NDEBUG1
175665/* For tracing reduce actions, the names of all rules are required.
175666*/
175667static const char *const yyRuleName[] = {
175668 /* 0 */ "explain ::= EXPLAIN",
175669 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
175670 /* 2 */ "cmdx ::= cmd",
175671 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
175672 /* 4 */ "transtype ::=",
175673 /* 5 */ "transtype ::= DEFERRED",
175674 /* 6 */ "transtype ::= IMMEDIATE",
175675 /* 7 */ "transtype ::= EXCLUSIVE",
175676 /* 8 */ "cmd ::= COMMIT|END trans_opt",
175677 /* 9 */ "cmd ::= ROLLBACK trans_opt",
175678 /* 10 */ "cmd ::= SAVEPOINT nm",
175679 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
175680 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
175681 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
175682 /* 14 */ "createkw ::= CREATE",
175683 /* 15 */ "ifnotexists ::=",
175684 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
175685 /* 17 */ "temp ::= TEMP",
175686 /* 18 */ "temp ::=",
175687 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_option_set",
175688 /* 20 */ "create_table_args ::= AS select",
175689 /* 21 */ "table_option_set ::=",
175690 /* 22 */ "table_option_set ::= table_option_set COMMA table_option",
175691 /* 23 */ "table_option ::= WITHOUT nm",
175692 /* 24 */ "table_option ::= nm",
175693 /* 25 */ "columnname ::= nm typetoken",
175694 /* 26 */ "typetoken ::=",
175695 /* 27 */ "typetoken ::= typename LP signed RP",
175696 /* 28 */ "typetoken ::= typename LP signed COMMA signed RP",
175697 /* 29 */ "typename ::= typename ID|STRING",
175698 /* 30 */ "scanpt ::=",
175699 /* 31 */ "scantok ::=",
175700 /* 32 */ "ccons ::= CONSTRAINT nm",
175701 /* 33 */ "ccons ::= DEFAULT scantok term",
175702 /* 34 */ "ccons ::= DEFAULT LP expr RP",
175703 /* 35 */ "ccons ::= DEFAULT PLUS scantok term",
175704 /* 36 */ "ccons ::= DEFAULT MINUS scantok term",
175705 /* 37 */ "ccons ::= DEFAULT scantok ID|INDEXED",
175706 /* 38 */ "ccons ::= NOT NULL onconf",
175707 /* 39 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
175708 /* 40 */ "ccons ::= UNIQUE onconf",
175709 /* 41 */ "ccons ::= CHECK LP expr RP",
175710 /* 42 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
175711 /* 43 */ "ccons ::= defer_subclause",
175712 /* 44 */ "ccons ::= COLLATE ID|STRING",
175713 /* 45 */ "generated ::= LP expr RP",
175714 /* 46 */ "generated ::= LP expr RP ID",
175715 /* 47 */ "autoinc ::=",
175716 /* 48 */ "autoinc ::= AUTOINCR",
175717 /* 49 */ "refargs ::=",
175718 /* 50 */ "refargs ::= refargs refarg",
175719 /* 51 */ "refarg ::= MATCH nm",
175720 /* 52 */ "refarg ::= ON INSERT refact",
175721 /* 53 */ "refarg ::= ON DELETE refact",
175722 /* 54 */ "refarg ::= ON UPDATE refact",
175723 /* 55 */ "refact ::= SET NULL",
175724 /* 56 */ "refact ::= SET DEFAULT",
175725 /* 57 */ "refact ::= CASCADE",
175726 /* 58 */ "refact ::= RESTRICT",
175727 /* 59 */ "refact ::= NO ACTION",
175728 /* 60 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
175729 /* 61 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
175730 /* 62 */ "init_deferred_pred_opt ::=",
175731 /* 63 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
175732 /* 64 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
175733 /* 65 */ "conslist_opt ::=",
175734 /* 66 */ "tconscomma ::= COMMA",
175735 /* 67 */ "tcons ::= CONSTRAINT nm",
175736 /* 68 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
175737 /* 69 */ "tcons ::= UNIQUE LP sortlist RP onconf",
175738 /* 70 */ "tcons ::= CHECK LP expr RP onconf",
175739 /* 71 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
175740 /* 72 */ "defer_subclause_opt ::=",
175741 /* 73 */ "onconf ::=",
175742 /* 74 */ "onconf ::= ON CONFLICT resolvetype",
175743 /* 75 */ "orconf ::=",
175744 /* 76 */ "orconf ::= OR resolvetype",
175745 /* 77 */ "resolvetype ::= IGNORE",
175746 /* 78 */ "resolvetype ::= REPLACE",
175747 /* 79 */ "cmd ::= DROP TABLE ifexists fullname",
175748 /* 80 */ "ifexists ::= IF EXISTS",
175749 /* 81 */ "ifexists ::=",
175750 /* 82 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
175751 /* 83 */ "cmd ::= DROP VIEW ifexists fullname",
175752 /* 84 */ "cmd ::= select",
175753 /* 85 */ "select ::= WITH wqlist selectnowith",
175754 /* 86 */ "select ::= WITH RECURSIVE wqlist selectnowith",
175755 /* 87 */ "select ::= selectnowith",
175756 /* 88 */ "selectnowith ::= selectnowith multiselect_op oneselect",
175757 /* 89 */ "multiselect_op ::= UNION",
175758 /* 90 */ "multiselect_op ::= UNION ALL",
175759 /* 91 */ "multiselect_op ::= EXCEPT|INTERSECT",
175760 /* 92 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
175761 /* 93 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
175762 /* 94 */ "values ::= VALUES LP nexprlist RP",
175763 /* 95 */ "oneselect ::= mvalues",
175764 /* 96 */ "mvalues ::= values COMMA LP nexprlist RP",
175765 /* 97 */ "mvalues ::= mvalues COMMA LP nexprlist RP",
175766 /* 98 */ "distinct ::= DISTINCT",
175767 /* 99 */ "distinct ::= ALL",
175768 /* 100 */ "distinct ::=",
175769 /* 101 */ "sclp ::=",
175770 /* 102 */ "selcollist ::= sclp scanpt expr scanpt as",
175771 /* 103 */ "selcollist ::= sclp scanpt STAR",
175772 /* 104 */ "selcollist ::= sclp scanpt nm DOT STAR",
175773 /* 105 */ "as ::= AS nm",
175774 /* 106 */ "as ::=",
175775 /* 107 */ "from ::=",
175776 /* 108 */ "from ::= FROM seltablist",
175777 /* 109 */ "stl_prefix ::= seltablist joinop",
175778 /* 110 */ "stl_prefix ::=",
175779 /* 111 */ "seltablist ::= stl_prefix nm dbnm as on_using",
175780 /* 112 */ "seltablist ::= stl_prefix nm dbnm as indexed_by on_using",
175781 /* 113 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using",
175782 /* 114 */ "seltablist ::= stl_prefix LP select RP as on_using",
175783 /* 115 */ "seltablist ::= stl_prefix LP seltablist RP as on_using",
175784 /* 116 */ "dbnm ::=",
175785 /* 117 */ "dbnm ::= DOT nm",
175786 /* 118 */ "fullname ::= nm",
175787 /* 119 */ "fullname ::= nm DOT nm",
175788 /* 120 */ "xfullname ::= nm",
175789 /* 121 */ "xfullname ::= nm DOT nm",
175790 /* 122 */ "xfullname ::= nm DOT nm AS nm",
175791 /* 123 */ "xfullname ::= nm AS nm",
175792 /* 124 */ "joinop ::= COMMA|JOIN",
175793 /* 125 */ "joinop ::= JOIN_KW JOIN",
175794 /* 126 */ "joinop ::= JOIN_KW nm JOIN",
175795 /* 127 */ "joinop ::= JOIN_KW nm nm JOIN",
175796 /* 128 */ "on_using ::= ON expr",
175797 /* 129 */ "on_using ::= USING LP idlist RP",
175798 /* 130 */ "on_using ::=",
175799 /* 131 */ "indexed_opt ::=",
175800 /* 132 */ "indexed_by ::= INDEXED BY nm",
175801 /* 133 */ "indexed_by ::= NOT INDEXED",
175802 /* 134 */ "orderby_opt ::=",
175803 /* 135 */ "orderby_opt ::= ORDER BY sortlist",
175804 /* 136 */ "sortlist ::= sortlist COMMA expr sortorder nulls",
175805 /* 137 */ "sortlist ::= expr sortorder nulls",
175806 /* 138 */ "sortorder ::= ASC",
175807 /* 139 */ "sortorder ::= DESC",
175808 /* 140 */ "sortorder ::=",
175809 /* 141 */ "nulls ::= NULLS FIRST",
175810 /* 142 */ "nulls ::= NULLS LAST",
175811 /* 143 */ "nulls ::=",
175812 /* 144 */ "groupby_opt ::=",
175813 /* 145 */ "groupby_opt ::= GROUP BY nexprlist",
175814 /* 146 */ "having_opt ::=",
175815 /* 147 */ "having_opt ::= HAVING expr",
175816 /* 148 */ "limit_opt ::=",
175817 /* 149 */ "limit_opt ::= LIMIT expr",
175818 /* 150 */ "limit_opt ::= LIMIT expr OFFSET expr",
175819 /* 151 */ "limit_opt ::= LIMIT expr COMMA expr",
175820 /* 152 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret",
175821 /* 153 */ "where_opt ::=",
175822 /* 154 */ "where_opt ::= WHERE expr",
175823 /* 155 */ "where_opt_ret ::=",
175824 /* 156 */ "where_opt_ret ::= WHERE expr",
175825 /* 157 */ "where_opt_ret ::= RETURNING selcollist",
175826 /* 158 */ "where_opt_ret ::= WHERE expr RETURNING selcollist",
175827 /* 159 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret",
175828 /* 160 */ "setlist ::= setlist COMMA nm EQ expr",
175829 /* 161 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
175830 /* 162 */ "setlist ::= nm EQ expr",
175831 /* 163 */ "setlist ::= LP idlist RP EQ expr",
175832 /* 164 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
175833 /* 165 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning",
175834 /* 166 */ "upsert ::=",
175835 /* 167 */ "upsert ::= RETURNING selcollist",
175836 /* 168 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert",
175837 /* 169 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert",
175838 /* 170 */ "upsert ::= ON CONFLICT DO NOTHING returning",
175839 /* 171 */ "upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning",
175840 /* 172 */ "returning ::= RETURNING selcollist",
175841 /* 173 */ "insert_cmd ::= INSERT orconf",
175842 /* 174 */ "insert_cmd ::= REPLACE",
175843 /* 175 */ "idlist_opt ::=",
175844 /* 176 */ "idlist_opt ::= LP idlist RP",
175845 /* 177 */ "idlist ::= idlist COMMA nm",
175846 /* 178 */ "idlist ::= nm",
175847 /* 179 */ "expr ::= LP expr RP",
175848 /* 180 */ "expr ::= ID|INDEXED|JOIN_KW",
175849 /* 181 */ "expr ::= nm DOT nm",
175850 /* 182 */ "expr ::= nm DOT nm DOT nm",
175851 /* 183 */ "term ::= NULL|FLOAT|BLOB",
175852 /* 184 */ "term ::= STRING",
175853 /* 185 */ "term ::= INTEGER",
175854 /* 186 */ "expr ::= VARIABLE",
175855 /* 187 */ "expr ::= expr COLLATE ID|STRING",
175856 /* 188 */ "expr ::= CAST LP expr AS typetoken RP",
175857 /* 189 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP",
175858 /* 190 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP",
175859 /* 191 */ "expr ::= ID|INDEXED|JOIN_KW LP STAR RP",
175860 /* 192 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over",
175861 /* 193 */ "expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over",
175862 /* 194 */ "expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over",
175863 /* 195 */ "term ::= CTIME_KW",
175864 /* 196 */ "expr ::= LP nexprlist COMMA expr RP",
175865 /* 197 */ "expr ::= expr AND expr",
175866 /* 198 */ "expr ::= expr OR expr",
175867 /* 199 */ "expr ::= expr LT|GT|GE|LE expr",
175868 /* 200 */ "expr ::= expr EQ|NE expr",
175869 /* 201 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
175870 /* 202 */ "expr ::= expr PLUS|MINUS expr",
175871 /* 203 */ "expr ::= expr STAR|SLASH|REM expr",
175872 /* 204 */ "expr ::= expr CONCAT expr",
175873 /* 205 */ "likeop ::= NOT LIKE_KW|MATCH",
175874 /* 206 */ "expr ::= expr likeop expr",
175875 /* 207 */ "expr ::= expr likeop expr ESCAPE expr",
175876 /* 208 */ "expr ::= expr ISNULL|NOTNULL",
175877 /* 209 */ "expr ::= expr NOT NULL",
175878 /* 210 */ "expr ::= expr IS expr",
175879 /* 211 */ "expr ::= expr IS NOT expr",
175880 /* 212 */ "expr ::= expr IS NOT DISTINCT FROM expr",
175881 /* 213 */ "expr ::= expr IS DISTINCT FROM expr",
175882 /* 214 */ "expr ::= NOT expr",
175883 /* 215 */ "expr ::= BITNOT expr",
175884 /* 216 */ "expr ::= PLUS|MINUS expr",
175885 /* 217 */ "expr ::= expr PTR expr",
175886 /* 218 */ "between_op ::= BETWEEN",
175887 /* 219 */ "between_op ::= NOT BETWEEN",
175888 /* 220 */ "expr ::= expr between_op expr AND expr",
175889 /* 221 */ "in_op ::= IN",
175890 /* 222 */ "in_op ::= NOT IN",
175891 /* 223 */ "expr ::= expr in_op LP exprlist RP",
175892 /* 224 */ "expr ::= LP select RP",
175893 /* 225 */ "expr ::= expr in_op LP select RP",
175894 /* 226 */ "expr ::= expr in_op nm dbnm paren_exprlist",
175895 /* 227 */ "expr ::= EXISTS LP select RP",
175896 /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
175897 /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
175898 /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
175899 /* 231 */ "case_else ::= ELSE expr",
175900 /* 232 */ "case_else ::=",
175901 /* 233 */ "case_operand ::=",
175902 /* 234 */ "exprlist ::=",
175903 /* 235 */ "nexprlist ::= nexprlist COMMA expr",
175904 /* 236 */ "nexprlist ::= expr",
175905 /* 237 */ "paren_exprlist ::=",
175906 /* 238 */ "paren_exprlist ::= LP exprlist RP",
175907 /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
175908 /* 240 */ "uniqueflag ::= UNIQUE",
175909 /* 241 */ "uniqueflag ::=",
175910 /* 242 */ "eidlist_opt ::=",
175911 /* 243 */ "eidlist_opt ::= LP eidlist RP",
175912 /* 244 */ "eidlist ::= eidlist COMMA nm collate sortorder",
175913 /* 245 */ "eidlist ::= nm collate sortorder",
175914 /* 246 */ "collate ::=",
175915 /* 247 */ "collate ::= COLLATE ID|STRING",
175916 /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
175917 /* 249 */ "cmd ::= VACUUM vinto",
175918 /* 250 */ "cmd ::= VACUUM nm vinto",
175919 /* 251 */ "vinto ::= INTO expr",
175920 /* 252 */ "vinto ::=",
175921 /* 253 */ "cmd ::= PRAGMA nm dbnm",
175922 /* 254 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
175923 /* 255 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
175924 /* 256 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
175925 /* 257 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
175926 /* 258 */ "plus_num ::= PLUS INTEGER|FLOAT",
175927 /* 259 */ "minus_num ::= MINUS INTEGER|FLOAT",
175928 /* 260 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
175929 /* 261 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
175930 /* 262 */ "trigger_time ::= BEFORE|AFTER",
175931 /* 263 */ "trigger_time ::= INSTEAD OF",
175932 /* 264 */ "trigger_time ::=",
175933 /* 265 */ "trigger_event ::= DELETE|INSERT",
175934 /* 266 */ "trigger_event ::= UPDATE",
175935 /* 267 */ "trigger_event ::= UPDATE OF idlist",
175936 /* 268 */ "when_clause ::=",
175937 /* 269 */ "when_clause ::= WHEN expr",
175938 /* 270 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
175939 /* 271 */ "trigger_cmd_list ::= trigger_cmd SEMI",
175940 /* 272 */ "trnm ::= nm DOT nm",
175941 /* 273 */ "tridxby ::= INDEXED BY nm",
175942 /* 274 */ "tridxby ::= NOT INDEXED",
175943 /* 275 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt",
175944 /* 276 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
175945 /* 277 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
175946 /* 278 */ "trigger_cmd ::= scanpt select scanpt",
175947 /* 279 */ "expr ::= RAISE LP IGNORE RP",
175948 /* 280 */ "expr ::= RAISE LP raisetype COMMA expr RP",
175949 /* 281 */ "raisetype ::= ROLLBACK",
175950 /* 282 */ "raisetype ::= ABORT",
175951 /* 283 */ "raisetype ::= FAIL",
175952 /* 284 */ "cmd ::= DROP TRIGGER ifexists fullname",
175953 /* 285 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
175954 /* 286 */ "cmd ::= DETACH database_kw_opt expr",
175955 /* 287 */ "key_opt ::=",
175956 /* 288 */ "key_opt ::= KEY expr",
175957 /* 289 */ "cmd ::= REINDEX",
175958 /* 290 */ "cmd ::= REINDEX nm dbnm",
175959 /* 291 */ "cmd ::= ANALYZE",
175960 /* 292 */ "cmd ::= ANALYZE nm dbnm",
175961 /* 293 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
175962 /* 294 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
175963 /* 295 */ "cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm",
175964 /* 296 */ "add_column_fullname ::= fullname",
175965 /* 297 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
175966 /* 298 */ "cmd ::= create_vtab",
175967 /* 299 */ "cmd ::= create_vtab LP vtabarglist RP",
175968 /* 300 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
175969 /* 301 */ "vtabarg ::=",
175970 /* 302 */ "vtabargtoken ::= ANY",
175971 /* 303 */ "vtabargtoken ::= lp anylist RP",
175972 /* 304 */ "lp ::= LP",
175973 /* 305 */ "with ::= WITH wqlist",
175974 /* 306 */ "with ::= WITH RECURSIVE wqlist",
175975 /* 307 */ "wqas ::= AS",
175976 /* 308 */ "wqas ::= AS MATERIALIZED",
175977 /* 309 */ "wqas ::= AS NOT MATERIALIZED",
175978 /* 310 */ "wqitem ::= withnm eidlist_opt wqas LP select RP",
175979 /* 311 */ "withnm ::= nm",
175980 /* 312 */ "wqlist ::= wqitem",
175981 /* 313 */ "wqlist ::= wqlist COMMA wqitem",
175982 /* 314 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
175983 /* 315 */ "windowdefn ::= nm AS LP window RP",
175984 /* 316 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
175985 /* 317 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
175986 /* 318 */ "window ::= ORDER BY sortlist frame_opt",
175987 /* 319 */ "window ::= nm ORDER BY sortlist frame_opt",
175988 /* 320 */ "window ::= nm frame_opt",
175989 /* 321 */ "frame_opt ::=",
175990 /* 322 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
175991 /* 323 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
175992 /* 324 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
175993 /* 325 */ "frame_bound_s ::= frame_bound",
175994 /* 326 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
175995 /* 327 */ "frame_bound_e ::= frame_bound",
175996 /* 328 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
175997 /* 329 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
175998 /* 330 */ "frame_bound ::= CURRENT ROW",
175999 /* 331 */ "frame_exclude_opt ::=",
176000 /* 332 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
176001 /* 333 */ "frame_exclude ::= NO OTHERS",
176002 /* 334 */ "frame_exclude ::= CURRENT ROW",
176003 /* 335 */ "frame_exclude ::= GROUP|TIES",
176004 /* 336 */ "window_clause ::= WINDOW windowdefn_list",
176005 /* 337 */ "filter_over ::= filter_clause over_clause",
176006 /* 338 */ "filter_over ::= over_clause",
176007 /* 339 */ "filter_over ::= filter_clause",
176008 /* 340 */ "over_clause ::= OVER LP window RP",
176009 /* 341 */ "over_clause ::= OVER nm",
176010 /* 342 */ "filter_clause ::= FILTER LP WHERE expr RP",
176011 /* 343 */ "term ::= QNUMBER",
176012 /* 344 */ "input ::= cmdlist",
176013 /* 345 */ "cmdlist ::= cmdlist ecmd",
176014 /* 346 */ "cmdlist ::= ecmd",
176015 /* 347 */ "ecmd ::= SEMI",
176016 /* 348 */ "ecmd ::= cmdx SEMI",
176017 /* 349 */ "ecmd ::= explain cmdx SEMI",
176018 /* 350 */ "trans_opt ::=",
176019 /* 351 */ "trans_opt ::= TRANSACTION",
176020 /* 352 */ "trans_opt ::= TRANSACTION nm",
176021 /* 353 */ "savepoint_opt ::= SAVEPOINT",
176022 /* 354 */ "savepoint_opt ::=",
176023 /* 355 */ "cmd ::= create_table create_table_args",
176024 /* 356 */ "table_option_set ::= table_option",
176025 /* 357 */ "columnlist ::= columnlist COMMA columnname carglist",
176026 /* 358 */ "columnlist ::= columnname carglist",
176027 /* 359 */ "nm ::= ID|INDEXED|JOIN_KW",
176028 /* 360 */ "nm ::= STRING",
176029 /* 361 */ "typetoken ::= typename",
176030 /* 362 */ "typename ::= ID|STRING",
176031 /* 363 */ "signed ::= plus_num",
176032 /* 364 */ "signed ::= minus_num",
176033 /* 365 */ "carglist ::= carglist ccons",
176034 /* 366 */ "carglist ::=",
176035 /* 367 */ "ccons ::= NULL onconf",
176036 /* 368 */ "ccons ::= GENERATED ALWAYS AS generated",
176037 /* 369 */ "ccons ::= AS generated",
176038 /* 370 */ "conslist_opt ::= COMMA conslist",
176039 /* 371 */ "conslist ::= conslist tconscomma tcons",
176040 /* 372 */ "conslist ::= tcons",
176041 /* 373 */ "tconscomma ::=",
176042 /* 374 */ "defer_subclause_opt ::= defer_subclause",
176043 /* 375 */ "resolvetype ::= raisetype",
176044 /* 376 */ "selectnowith ::= oneselect",
176045 /* 377 */ "oneselect ::= values",
176046 /* 378 */ "sclp ::= selcollist COMMA",
176047 /* 379 */ "as ::= ID|STRING",
176048 /* 380 */ "indexed_opt ::= indexed_by",
176049 /* 381 */ "returning ::=",
176050 /* 382 */ "expr ::= term",
176051 /* 383 */ "likeop ::= LIKE_KW|MATCH",
176052 /* 384 */ "case_operand ::= expr",
176053 /* 385 */ "exprlist ::= nexprlist",
176054 /* 386 */ "nmnum ::= plus_num",
176055 /* 387 */ "nmnum ::= nm",
176056 /* 388 */ "nmnum ::= ON",
176057 /* 389 */ "nmnum ::= DELETE",
176058 /* 390 */ "nmnum ::= DEFAULT",
176059 /* 391 */ "plus_num ::= INTEGER|FLOAT",
176060 /* 392 */ "foreach_clause ::=",
176061 /* 393 */ "foreach_clause ::= FOR EACH ROW",
176062 /* 394 */ "trnm ::= nm",
176063 /* 395 */ "tridxby ::=",
176064 /* 396 */ "database_kw_opt ::= DATABASE",
176065 /* 397 */ "database_kw_opt ::=",
176066 /* 398 */ "kwcolumn_opt ::=",
176067 /* 399 */ "kwcolumn_opt ::= COLUMNKW",
176068 /* 400 */ "vtabarglist ::= vtabarg",
176069 /* 401 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
176070 /* 402 */ "vtabarg ::= vtabarg vtabargtoken",
176071 /* 403 */ "anylist ::=",
176072 /* 404 */ "anylist ::= anylist LP anylist RP",
176073 /* 405 */ "anylist ::= anylist ANY",
176074 /* 406 */ "with ::=",
176075 /* 407 */ "windowdefn_list ::= windowdefn",
176076 /* 408 */ "window ::= frame_opt",
176077};
176078#endif /* NDEBUG */
176079
176080
176081#if YYGROWABLESTACK1
176082/*
176083** Try to increase the size of the parser stack. Return the number
176084** of errors. Return 0 on success.
176085*/
176086static int yyGrowStack(yyParser *p){
176087 int oldSize = 1 + (int)(p->yystackEnd - p->yystack);
176088 int newSize;
176089 int idx;
176090 yyStackEntry *pNew;
176091
176092 newSize = oldSize*2 + 100;
176093 idx = (int)(p->yytos - p->yystack);
176094 if( p->yystack==p->yystk0 ){
176095 pNew = YYREALLOCparserStackRealloc(0, newSize*sizeof(pNew[0]));
176096 if( pNew==0 ) return 1;
176097 memcpy(pNew, p->yystack, oldSize*sizeof(pNew[0]));
176098 }else{
176099 pNew = YYREALLOCparserStackRealloc(p->yystack, newSize*sizeof(pNew[0]));
176100 if( pNew==0 ) return 1;
176101 }
176102 p->yystack = pNew;
176103 p->yytos = &p->yystack[idx];
176104#ifndef NDEBUG1
176105 if( yyTraceFILE ){
176106 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
176107 yyTracePrompt, oldSize, newSize);
176108 }
176109#endif
176110 p->yystackEnd = &p->yystack[newSize-1];
176111 return 0;
176112}
176113#endif /* YYGROWABLESTACK */
176114
176115#if !YYGROWABLESTACK1
176116/* For builds that do no have a growable stack, yyGrowStack always
176117** returns an error.
176118*/
176119# define yyGrowStack(X) 1
176120#endif
176121
176122/* Datatype of the argument to the memory allocated passed as the
176123** second argument to sqlite3ParserAlloc() below. This can be changed by
176124** putting an appropriate #define in the %include section of the input
176125** grammar.
176126*/
176127#ifndef YYMALLOCARGTYPEu64
176128# define YYMALLOCARGTYPEu64 size_t
176129#endif
176130
176131/* Initialize a new parser that has already been allocated.
176132*/
176133SQLITE_PRIVATEstatic void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL,Parse *pParse){
176134 yyParser *yypParser = (yyParser*)yypRawParser;
176135 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
176136#ifdef YYTRACKMAXSTACKDEPTH
176137 yypParser->yyhwm = 0;
176138#endif
176139 yypParser->yystack = yypParser->yystk0;
176140 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH100-1];
176141#ifndef YYNOERRORRECOVERY1
176142 yypParser->yyerrcnt = -1;
176143#endif
176144 yypParser->yytos = yypParser->yystack;
176145 yypParser->yystack[0].stateno = 0;
176146 yypParser->yystack[0].major = 0;
176147}
176148
176149#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
176150/*
176151** This function allocates a new parser.
176152** The only argument is a pointer to a function which works like
176153** malloc.
176154**
176155** Inputs:
176156** A pointer to the function used to allocate memory.
176157**
176158** Outputs:
176159** A pointer to a parser. This pointer is used in subsequent calls
176160** to sqlite3Parser and sqlite3ParserFree.
176161*/
176162SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPEu64) sqlite3ParserCTX_PDECL,Parse *pParse){
176163 yyParser *yypParser;
176164 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPEu64)sizeof(yyParser) );
176165 if( yypParser ){
176166 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
176167 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM,pParse);
176168 }
176169 return (void*)yypParser;
176170}
176171#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
176172
176173
176174/* The following function deletes the "minor type" or semantic value
176175** associated with a symbol. The symbol can be either a terminal
176176** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
176177** a pointer to the value to be deleted. The code used to do the
176178** deletions is derived from the %destructor and/or %token_destructor
176179** directives of the input grammar.
176180*/
176181static void yy_destructor(
176182 yyParser *yypParser, /* The parser */
176183 YYCODETYPEunsigned short int yymajor, /* Type code for object to destroy */
176184 YYMINORTYPE *yypminor /* The object to be destroyed */
176185){
176186 sqlite3ParserARG_FETCH
176187 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
176188 switch( yymajor ){
176189 /* Here is inserted the actions which take place when a
176190 ** terminal or non-terminal is destroyed. This can happen
176191 ** when the symbol is popped from the stack during a
176192 ** reduce or during error processing or when a parser is
176193 ** being destroyed before it is finished parsing.
176194 **
176195 ** Note: during a reduce, the only symbols destroyed are those
176196 ** which appear on the RHS of the rule, but which are *not* used
176197 ** inside the C code.
176198 */
176199/********* Begin destructor definitions ***************************************/
176200 case 205: /* select */
176201 case 240: /* selectnowith */
176202 case 241: /* oneselect */
176203 case 253: /* values */
176204 case 255: /* mvalues */
176205{
176206sqlite3SelectDelete(pParse->db, (yypminor->yy555));
176207}
176208 break;
176209 case 217: /* term */
176210 case 218: /* expr */
176211 case 247: /* where_opt */
176212 case 249: /* having_opt */
176213 case 269: /* where_opt_ret */
176214 case 280: /* case_operand */
176215 case 282: /* case_else */
176216 case 285: /* vinto */
176217 case 292: /* when_clause */
176218 case 297: /* key_opt */
176219 case 314: /* filter_clause */
176220{
176221sqlite3ExprDelete(pParse->db, (yypminor->yy454));
176222}
176223 break;
176224 case 222: /* eidlist_opt */
176225 case 232: /* sortlist */
176226 case 233: /* eidlist */
176227 case 245: /* selcollist */
176228 case 248: /* groupby_opt */
176229 case 250: /* orderby_opt */
176230 case 254: /* nexprlist */
176231 case 256: /* sclp */
176232 case 263: /* exprlist */
176233 case 270: /* setlist */
176234 case 279: /* paren_exprlist */
176235 case 281: /* case_exprlist */
176236 case 313: /* part_opt */
176237{
176238sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
176239}
176240 break;
176241 case 239: /* fullname */
176242 case 246: /* from */
176243 case 258: /* seltablist */
176244 case 259: /* stl_prefix */
176245 case 264: /* xfullname */
176246{
176247sqlite3SrcListDelete(pParse->db, (yypminor->yy203));
176248}
176249 break;
176250 case 242: /* wqlist */
176251{
176252sqlite3WithDelete(pParse->db, (yypminor->yy59));
176253}
176254 break;
176255 case 252: /* window_clause */
176256 case 309: /* windowdefn_list */
176257{
176258sqlite3WindowListDelete(pParse->db, (yypminor->yy211));
176259}
176260 break;
176261 case 265: /* idlist */
176262 case 272: /* idlist_opt */
176263{
176264sqlite3IdListDelete(pParse->db, (yypminor->yy132));
176265}
176266 break;
176267 case 275: /* filter_over */
176268 case 310: /* windowdefn */
176269 case 311: /* window */
176270 case 312: /* frame_opt */
176271 case 315: /* over_clause */
176272{
176273sqlite3WindowDelete(pParse->db, (yypminor->yy211));
176274}
176275 break;
176276 case 288: /* trigger_cmd_list */
176277 case 293: /* trigger_cmd */
176278{
176279sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy427));
176280}
176281 break;
176282 case 290: /* trigger_event */
176283{
176284sqlite3IdListDelete(pParse->db, (yypminor->yy286).b);
176285}
176286 break;
176287 case 317: /* frame_bound */
176288 case 318: /* frame_bound_s */
176289 case 319: /* frame_bound_e */
176290{
176291sqlite3ExprDelete(pParse->db, (yypminor->yy509).pExpr);
176292}
176293 break;
176294/********* End destructor definitions *****************************************/
176295 default: break; /* If no destructor action specified: do nothing */
176296 }
176297}
176298
176299/*
176300** Pop the parser's stack once.
176301**
176302** If there is a destructor routine associated with the token which
176303** is popped from the stack, then call it.
176304*/
176305static void yy_pop_parser_stack(yyParser *pParser){
176306 yyStackEntry *yytos;
176307 assert( pParser->yytos!=0 )((void) (0));
176308 assert( pParser->yytos > pParser->yystack )((void) (0));
176309 yytos = pParser->yytos--;
176310#ifndef NDEBUG1
176311 if( yyTraceFILE ){
176312 fprintf(yyTraceFILE,"%sPopping %s\n",
176313 yyTracePrompt,
176314 yyTokenName[yytos->major]);
176315 }
176316#endif
176317 yy_destructor(pParser, yytos->major, &yytos->minor);
176318}
176319
176320/*
176321** Clear all secondary memory allocations from the parser
176322*/
176323SQLITE_PRIVATEstatic void sqlite3ParserFinalize(void *p){
176324 yyParser *pParser = (yyParser*)p;
176325
176326 /* In-lined version of calling yy_pop_parser_stack() for each
176327 ** element left in the stack */
176328 yyStackEntry *yytos = pParser->yytos;
176329 while( yytos>pParser->yystack ){
176330#ifndef NDEBUG1
176331 if( yyTraceFILE ){
176332 fprintf(yyTraceFILE,"%sPopping %s\n",
176333 yyTracePrompt,
176334 yyTokenName[yytos->major]);
176335 }
176336#endif
176337 if( yytos->major>=YY_MIN_DSTRCTR205 ){
176338 yy_destructor(pParser, yytos->major, &yytos->minor);
176339 }
176340 yytos--;
176341 }
176342
176343#if YYGROWABLESTACK1
176344 if( pParser->yystack!=pParser->yystk0 ) YYFREEsqlite3_free(pParser->yystack);
176345#endif
176346}
176347
176348#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
176349/*
176350** Deallocate and destroy a parser. Destructors are called for
176351** all stack elements before shutting the parser down.
176352**
176353** If the YYPARSEFREENEVERNULL macro exists (for example because it
176354** is defined in a %include section of the input grammar) then it is
176355** assumed that the input pointer is never NULL.
176356*/
176357SQLITE_PRIVATEstatic void sqlite3ParserFree(
176358 void *p, /* The parser to be deleted */
176359 void (*freeProc)(void*) /* Function used to reclaim memory */
176360){
176361#ifndef YYPARSEFREENEVERNULL1
176362 if( p==0 ) return;
176363#endif
176364 sqlite3ParserFinalize(p);
176365 (*freeProc)(p);
176366}
176367#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
176368
176369/*
176370** Return the peak depth of the stack for a parser.
176371*/
176372#ifdef YYTRACKMAXSTACKDEPTH
176373SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void *p){
176374 yyParser *pParser = (yyParser*)p;
176375 return pParser->yyhwm;
176376}
176377#endif
176378
176379/* This array of booleans keeps track of the parser statement
176380** coverage. The element yycoverage[X][Y] is set when the parser
176381** is in state X and has a lookahead token Y. In a well-tested
176382** systems, every element of this matrix should end up being set.
176383*/
176384#if defined(YYCOVERAGE)
176385static unsigned char yycoverage[YYNSTATE583][YYNTOKEN186];
176386#endif
176387
176388/*
176389** Write into out a description of every state/lookahead combination that
176390**
176391** (1) has not been used by the parser, and
176392** (2) is not a syntax error.
176393**
176394** Return the number of missed state/lookahead combinations.
176395*/
176396#if defined(YYCOVERAGE)
176397SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE *out){
176398 int stateno, iLookAhead, i;
176399 int nMissed = 0;
176400 for(stateno=0; stateno<YYNSTATE583; stateno++){
176401 i = yy_shift_ofst[stateno];
176402 for(iLookAhead=0; iLookAhead<YYNTOKEN186; iLookAhead++){
176403 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
176404 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
176405 if( out ){
176406 fprintf(out,"State %d lookahead %s %s\n", stateno,
176407 yyTokenName[iLookAhead],
176408 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
176409 }
176410 }
176411 }
176412 return nMissed;
176413}
176414#endif
176415
176416/*
176417** Find the appropriate action for a parser given the terminal
176418** look-ahead token iLookAhead.
176419*/
176420static YYACTIONTYPEunsigned short int yy_find_shift_action(
176421 YYCODETYPEunsigned short int iLookAhead, /* The look-ahead token */
176422 YYACTIONTYPEunsigned short int stateno /* Current state number */
176423){
176424 int i;
176425
176426 if( stateno>YY_MAX_SHIFT582 ) return stateno;
176427 assert( stateno <= YY_SHIFT_COUNT )((void) (0));
176428#if defined(YYCOVERAGE)
176429 yycoverage[stateno][iLookAhead] = 1;
176430#endif
176431 do{
176432 i = yy_shift_ofst[stateno];
176433 assert( i>=0 )((void) (0));
176434 assert( i<=YY_ACTTAB_COUNT )((void) (0));
176435 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD )((void) (0));
176436 assert( iLookAhead!=YYNOCODE )((void) (0));
176437 assert( iLookAhead < YYNTOKEN )((void) (0));
176438 i += iLookAhead;
176439 assert( i<(int)YY_NLOOKAHEAD )((void) (0));
176440 if( yy_lookahead[i]!=iLookAhead ){
176441#ifdef YYFALLBACK1
176442 YYCODETYPEunsigned short int iFallback; /* Fallback token */
176443 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) )((void) (0));
176444 iFallback = yyFallback[iLookAhead];
176445 if( iFallback!=0 ){
176446#ifndef NDEBUG1
176447 if( yyTraceFILE ){
176448 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
176449 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
176450 }
176451#endif
176452 assert( yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
176453 iLookAhead = iFallback;
176454 continue;
176455 }
176456#endif
176457#ifdef YYWILDCARD102
176458 {
176459 int j = i - iLookAhead + YYWILDCARD102;
176460 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) )((void) (0));
176461 if( yy_lookahead[j]==YYWILDCARD102 && iLookAhead>0 ){
176462#ifndef NDEBUG1
176463 if( yyTraceFILE ){
176464 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
176465 yyTracePrompt, yyTokenName[iLookAhead],
176466 yyTokenName[YYWILDCARD102]);
176467 }
176468#endif /* NDEBUG */
176469 return yy_action[j];
176470 }
176471 }
176472#endif /* YYWILDCARD */
176473 return yy_default[stateno];
176474 }else{
176475 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) )((void) (0));
176476 return yy_action[i];
176477 }
176478 }while(1);
176479}
176480
176481/*
176482** Find the appropriate action for a parser given the non-terminal
176483** look-ahead token iLookAhead.
176484*/
176485static YYACTIONTYPEunsigned short int yy_find_reduce_action(
176486 YYACTIONTYPEunsigned short int stateno, /* Current state number */
176487 YYCODETYPEunsigned short int iLookAhead /* The look-ahead token */
176488){
176489 int i;
176490#ifdef YYERRORSYMBOL
176491 if( stateno>YY_REDUCE_COUNT(412) ){
176492 return yy_default[stateno];
176493 }
176494#else
176495 assert( stateno<=YY_REDUCE_COUNT )((void) (0));
176496#endif
176497 i = yy_reduce_ofst[stateno];
176498 assert( iLookAhead!=YYNOCODE )((void) (0));
176499 i += iLookAhead;
176500#ifdef YYERRORSYMBOL
176501 if( i<0 || i>=YY_ACTTAB_COUNT(2207) || yy_lookahead[i]!=iLookAhead ){
176502 return yy_default[stateno];
176503 }
176504#else
176505 assert( i>=0 && i<YY_ACTTAB_COUNT )((void) (0));
176506 assert( yy_lookahead[i]==iLookAhead )((void) (0));
176507#endif
176508 return yy_action[i];
176509}
176510
176511/*
176512** The following routine is called if the stack overflows.
176513*/
176514static void yyStackOverflow(yyParser *yypParser){
176515 sqlite3ParserARG_FETCH
176516 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
176517#ifndef NDEBUG1
176518 if( yyTraceFILE ){
176519 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
176520 }
176521#endif
176522 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
176523 /* Here code is inserted which will execute if the parser
176524 ** stack every overflows */
176525/******** Begin %stack_overflow code ******************************************/
176526
176527 sqlite3OomFault(pParse->db);
176528/******** End %stack_overflow code ********************************************/
176529 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
176530 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
176531}
176532
176533/*
176534** Print tracing information for a SHIFT action
176535*/
176536#ifndef NDEBUG1
176537static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
176538 if( yyTraceFILE ){
176539 if( yyNewState<YYNSTATE583 ){
176540 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
176541 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
176542 yyNewState);
176543 }else{
176544 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
176545 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
176546 yyNewState - YY_MIN_REDUCE1257);
176547 }
176548 }
176549}
176550#else
176551# define yyTraceShift(X,Y,Z)
176552#endif
176553
176554/*
176555** Perform a shift action.
176556*/
176557static void yy_shift(
176558 yyParser *yypParser, /* The parser to be shifted */
176559 YYACTIONTYPEunsigned short int yyNewState, /* The new state to shift in */
176560 YYCODETYPEunsigned short int yyMajor, /* The major token to shift in */
176561 sqlite3ParserTOKENTYPEToken yyMinor /* The minor token to shift in */
176562){
176563 yyStackEntry *yytos;
176564 yypParser->yytos++;
176565#ifdef YYTRACKMAXSTACKDEPTH
176566 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
176567 yypParser->yyhwm++;
176568 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) )((void) (0));
176569 }
176570#endif
176571 yytos = yypParser->yytos;
176572 if( yytos>yypParser->yystackEnd ){
176573 if( yyGrowStack(yypParser) ){
176574 yypParser->yytos--;
176575 yyStackOverflow(yypParser);
176576 return;
176577 }
176578 yytos = yypParser->yytos;
176579 assert( yytos <= yypParser->yystackEnd )((void) (0));
176580 }
176581 if( yyNewState > YY_MAX_SHIFT582 ){
176582 yyNewState += YY_MIN_REDUCE1257 - YY_MIN_SHIFTREDUCE845;
176583 }
176584 yytos->stateno = yyNewState;
176585 yytos->major = yyMajor;
176586 yytos->minor.yy0 = yyMinor;
176587 yyTraceShift(yypParser, yyNewState, "Shift");
176588}
176589
176590/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
176591** of that rule */
176592static const YYCODETYPEunsigned short int yyRuleInfoLhs[] = {
176593 190, /* (0) explain ::= EXPLAIN */
176594 190, /* (1) explain ::= EXPLAIN QUERY PLAN */
176595 189, /* (2) cmdx ::= cmd */
176596 191, /* (3) cmd ::= BEGIN transtype trans_opt */
176597 192, /* (4) transtype ::= */
176598 192, /* (5) transtype ::= DEFERRED */
176599 192, /* (6) transtype ::= IMMEDIATE */
176600 192, /* (7) transtype ::= EXCLUSIVE */
176601 191, /* (8) cmd ::= COMMIT|END trans_opt */
176602 191, /* (9) cmd ::= ROLLBACK trans_opt */
176603 191, /* (10) cmd ::= SAVEPOINT nm */
176604 191, /* (11) cmd ::= RELEASE savepoint_opt nm */
176605 191, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
176606 196, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
176607 198, /* (14) createkw ::= CREATE */
176608 200, /* (15) ifnotexists ::= */
176609 200, /* (16) ifnotexists ::= IF NOT EXISTS */
176610 199, /* (17) temp ::= TEMP */
176611 199, /* (18) temp ::= */
176612 197, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
176613 197, /* (20) create_table_args ::= AS select */
176614 204, /* (21) table_option_set ::= */
176615 204, /* (22) table_option_set ::= table_option_set COMMA table_option */
176616 206, /* (23) table_option ::= WITHOUT nm */
176617 206, /* (24) table_option ::= nm */
176618 207, /* (25) columnname ::= nm typetoken */
176619 209, /* (26) typetoken ::= */
176620 209, /* (27) typetoken ::= typename LP signed RP */
176621 209, /* (28) typetoken ::= typename LP signed COMMA signed RP */
176622 210, /* (29) typename ::= typename ID|STRING */
176623 214, /* (30) scanpt ::= */
176624 215, /* (31) scantok ::= */
176625 216, /* (32) ccons ::= CONSTRAINT nm */
176626 216, /* (33) ccons ::= DEFAULT scantok term */
176627 216, /* (34) ccons ::= DEFAULT LP expr RP */
176628 216, /* (35) ccons ::= DEFAULT PLUS scantok term */
176629 216, /* (36) ccons ::= DEFAULT MINUS scantok term */
176630 216, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
176631 216, /* (38) ccons ::= NOT NULL onconf */
176632 216, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
176633 216, /* (40) ccons ::= UNIQUE onconf */
176634 216, /* (41) ccons ::= CHECK LP expr RP */
176635 216, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
176636 216, /* (43) ccons ::= defer_subclause */
176637 216, /* (44) ccons ::= COLLATE ID|STRING */
176638 225, /* (45) generated ::= LP expr RP */
176639 225, /* (46) generated ::= LP expr RP ID */
176640 221, /* (47) autoinc ::= */
176641 221, /* (48) autoinc ::= AUTOINCR */
176642 223, /* (49) refargs ::= */
176643 223, /* (50) refargs ::= refargs refarg */
176644 226, /* (51) refarg ::= MATCH nm */
176645 226, /* (52) refarg ::= ON INSERT refact */
176646 226, /* (53) refarg ::= ON DELETE refact */
176647 226, /* (54) refarg ::= ON UPDATE refact */
176648 227, /* (55) refact ::= SET NULL */
176649 227, /* (56) refact ::= SET DEFAULT */
176650 227, /* (57) refact ::= CASCADE */
176651 227, /* (58) refact ::= RESTRICT */
176652 227, /* (59) refact ::= NO ACTION */
176653 224, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
176654 224, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
176655 228, /* (62) init_deferred_pred_opt ::= */
176656 228, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
176657 228, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
176658 203, /* (65) conslist_opt ::= */
176659 230, /* (66) tconscomma ::= COMMA */
176660 231, /* (67) tcons ::= CONSTRAINT nm */
176661 231, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
176662 231, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
176663 231, /* (70) tcons ::= CHECK LP expr RP onconf */
176664 231, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
176665 234, /* (72) defer_subclause_opt ::= */
176666 219, /* (73) onconf ::= */
176667 219, /* (74) onconf ::= ON CONFLICT resolvetype */
176668 235, /* (75) orconf ::= */
176669 235, /* (76) orconf ::= OR resolvetype */
176670 236, /* (77) resolvetype ::= IGNORE */
176671 236, /* (78) resolvetype ::= REPLACE */
176672 191, /* (79) cmd ::= DROP TABLE ifexists fullname */
176673 238, /* (80) ifexists ::= IF EXISTS */
176674 238, /* (81) ifexists ::= */
176675 191, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
176676 191, /* (83) cmd ::= DROP VIEW ifexists fullname */
176677 191, /* (84) cmd ::= select */
176678 205, /* (85) select ::= WITH wqlist selectnowith */
176679 205, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
176680 205, /* (87) select ::= selectnowith */
176681 240, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
176682 243, /* (89) multiselect_op ::= UNION */
176683 243, /* (90) multiselect_op ::= UNION ALL */
176684 243, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
176685 241, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
176686 241, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
176687 253, /* (94) values ::= VALUES LP nexprlist RP */
176688 241, /* (95) oneselect ::= mvalues */
176689 255, /* (96) mvalues ::= values COMMA LP nexprlist RP */
176690 255, /* (97) mvalues ::= mvalues COMMA LP nexprlist RP */
176691 244, /* (98) distinct ::= DISTINCT */
176692 244, /* (99) distinct ::= ALL */
176693 244, /* (100) distinct ::= */
176694 256, /* (101) sclp ::= */
176695 245, /* (102) selcollist ::= sclp scanpt expr scanpt as */
176696 245, /* (103) selcollist ::= sclp scanpt STAR */
176697 245, /* (104) selcollist ::= sclp scanpt nm DOT STAR */
176698 257, /* (105) as ::= AS nm */
176699 257, /* (106) as ::= */
176700 246, /* (107) from ::= */
176701 246, /* (108) from ::= FROM seltablist */
176702 259, /* (109) stl_prefix ::= seltablist joinop */
176703 259, /* (110) stl_prefix ::= */
176704 258, /* (111) seltablist ::= stl_prefix nm dbnm as on_using */
176705 258, /* (112) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
176706 258, /* (113) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
176707 258, /* (114) seltablist ::= stl_prefix LP select RP as on_using */
176708 258, /* (115) seltablist ::= stl_prefix LP seltablist RP as on_using */
176709 201, /* (116) dbnm ::= */
176710 201, /* (117) dbnm ::= DOT nm */
176711 239, /* (118) fullname ::= nm */
176712 239, /* (119) fullname ::= nm DOT nm */
176713 264, /* (120) xfullname ::= nm */
176714 264, /* (121) xfullname ::= nm DOT nm */
176715 264, /* (122) xfullname ::= nm DOT nm AS nm */
176716 264, /* (123) xfullname ::= nm AS nm */
176717 260, /* (124) joinop ::= COMMA|JOIN */
176718 260, /* (125) joinop ::= JOIN_KW JOIN */
176719 260, /* (126) joinop ::= JOIN_KW nm JOIN */
176720 260, /* (127) joinop ::= JOIN_KW nm nm JOIN */
176721 261, /* (128) on_using ::= ON expr */
176722 261, /* (129) on_using ::= USING LP idlist RP */
176723 261, /* (130) on_using ::= */
176724 266, /* (131) indexed_opt ::= */
176725 262, /* (132) indexed_by ::= INDEXED BY nm */
176726 262, /* (133) indexed_by ::= NOT INDEXED */
176727 250, /* (134) orderby_opt ::= */
176728 250, /* (135) orderby_opt ::= ORDER BY sortlist */
176729 232, /* (136) sortlist ::= sortlist COMMA expr sortorder nulls */
176730 232, /* (137) sortlist ::= expr sortorder nulls */
176731 220, /* (138) sortorder ::= ASC */
176732 220, /* (139) sortorder ::= DESC */
176733 220, /* (140) sortorder ::= */
176734 267, /* (141) nulls ::= NULLS FIRST */
176735 267, /* (142) nulls ::= NULLS LAST */
176736 267, /* (143) nulls ::= */
176737 248, /* (144) groupby_opt ::= */
176738 248, /* (145) groupby_opt ::= GROUP BY nexprlist */
176739 249, /* (146) having_opt ::= */
176740 249, /* (147) having_opt ::= HAVING expr */
176741 251, /* (148) limit_opt ::= */
176742 251, /* (149) limit_opt ::= LIMIT expr */
176743 251, /* (150) limit_opt ::= LIMIT expr OFFSET expr */
176744 251, /* (151) limit_opt ::= LIMIT expr COMMA expr */
176745 191, /* (152) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
176746 247, /* (153) where_opt ::= */
176747 247, /* (154) where_opt ::= WHERE expr */
176748 269, /* (155) where_opt_ret ::= */
176749 269, /* (156) where_opt_ret ::= WHERE expr */
176750 269, /* (157) where_opt_ret ::= RETURNING selcollist */
176751 269, /* (158) where_opt_ret ::= WHERE expr RETURNING selcollist */
176752 191, /* (159) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
176753 270, /* (160) setlist ::= setlist COMMA nm EQ expr */
176754 270, /* (161) setlist ::= setlist COMMA LP idlist RP EQ expr */
176755 270, /* (162) setlist ::= nm EQ expr */
176756 270, /* (163) setlist ::= LP idlist RP EQ expr */
176757 191, /* (164) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
176758 191, /* (165) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
176759 273, /* (166) upsert ::= */
176760 273, /* (167) upsert ::= RETURNING selcollist */
176761 273, /* (168) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
176762 273, /* (169) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
176763 273, /* (170) upsert ::= ON CONFLICT DO NOTHING returning */
176764 273, /* (171) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
176765 274, /* (172) returning ::= RETURNING selcollist */
176766 271, /* (173) insert_cmd ::= INSERT orconf */
176767 271, /* (174) insert_cmd ::= REPLACE */
176768 272, /* (175) idlist_opt ::= */
176769 272, /* (176) idlist_opt ::= LP idlist RP */
176770 265, /* (177) idlist ::= idlist COMMA nm */
176771 265, /* (178) idlist ::= nm */
176772 218, /* (179) expr ::= LP expr RP */
176773 218, /* (180) expr ::= ID|INDEXED|JOIN_KW */
176774 218, /* (181) expr ::= nm DOT nm */
176775 218, /* (182) expr ::= nm DOT nm DOT nm */
176776 217, /* (183) term ::= NULL|FLOAT|BLOB */
176777 217, /* (184) term ::= STRING */
176778 217, /* (185) term ::= INTEGER */
176779 218, /* (186) expr ::= VARIABLE */
176780 218, /* (187) expr ::= expr COLLATE ID|STRING */
176781 218, /* (188) expr ::= CAST LP expr AS typetoken RP */
176782 218, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
176783 218, /* (190) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
176784 218, /* (191) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
176785 218, /* (192) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
176786 218, /* (193) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
176787 218, /* (194) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
176788 217, /* (195) term ::= CTIME_KW */
176789 218, /* (196) expr ::= LP nexprlist COMMA expr RP */
176790 218, /* (197) expr ::= expr AND expr */
176791 218, /* (198) expr ::= expr OR expr */
176792 218, /* (199) expr ::= expr LT|GT|GE|LE expr */
176793 218, /* (200) expr ::= expr EQ|NE expr */
176794 218, /* (201) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
176795 218, /* (202) expr ::= expr PLUS|MINUS expr */
176796 218, /* (203) expr ::= expr STAR|SLASH|REM expr */
176797 218, /* (204) expr ::= expr CONCAT expr */
176798 276, /* (205) likeop ::= NOT LIKE_KW|MATCH */
176799 218, /* (206) expr ::= expr likeop expr */
176800 218, /* (207) expr ::= expr likeop expr ESCAPE expr */
176801 218, /* (208) expr ::= expr ISNULL|NOTNULL */
176802 218, /* (209) expr ::= expr NOT NULL */
176803 218, /* (210) expr ::= expr IS expr */
176804 218, /* (211) expr ::= expr IS NOT expr */
176805 218, /* (212) expr ::= expr IS NOT DISTINCT FROM expr */
176806 218, /* (213) expr ::= expr IS DISTINCT FROM expr */
176807 218, /* (214) expr ::= NOT expr */
176808 218, /* (215) expr ::= BITNOT expr */
176809 218, /* (216) expr ::= PLUS|MINUS expr */
176810 218, /* (217) expr ::= expr PTR expr */
176811 277, /* (218) between_op ::= BETWEEN */
176812 277, /* (219) between_op ::= NOT BETWEEN */
176813 218, /* (220) expr ::= expr between_op expr AND expr */
176814 278, /* (221) in_op ::= IN */
176815 278, /* (222) in_op ::= NOT IN */
176816 218, /* (223) expr ::= expr in_op LP exprlist RP */
176817 218, /* (224) expr ::= LP select RP */
176818 218, /* (225) expr ::= expr in_op LP select RP */
176819 218, /* (226) expr ::= expr in_op nm dbnm paren_exprlist */
176820 218, /* (227) expr ::= EXISTS LP select RP */
176821 218, /* (228) expr ::= CASE case_operand case_exprlist case_else END */
176822 281, /* (229) case_exprlist ::= case_exprlist WHEN expr THEN expr */
176823 281, /* (230) case_exprlist ::= WHEN expr THEN expr */
176824 282, /* (231) case_else ::= ELSE expr */
176825 282, /* (232) case_else ::= */
176826 280, /* (233) case_operand ::= */
176827 263, /* (234) exprlist ::= */
176828 254, /* (235) nexprlist ::= nexprlist COMMA expr */
176829 254, /* (236) nexprlist ::= expr */
176830 279, /* (237) paren_exprlist ::= */
176831 279, /* (238) paren_exprlist ::= LP exprlist RP */
176832 191, /* (239) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
176833 283, /* (240) uniqueflag ::= UNIQUE */
176834 283, /* (241) uniqueflag ::= */
176835 222, /* (242) eidlist_opt ::= */
176836 222, /* (243) eidlist_opt ::= LP eidlist RP */
176837 233, /* (244) eidlist ::= eidlist COMMA nm collate sortorder */
176838 233, /* (245) eidlist ::= nm collate sortorder */
176839 284, /* (246) collate ::= */
176840 284, /* (247) collate ::= COLLATE ID|STRING */
176841 191, /* (248) cmd ::= DROP INDEX ifexists fullname */
176842 191, /* (249) cmd ::= VACUUM vinto */
176843 191, /* (250) cmd ::= VACUUM nm vinto */
176844 285, /* (251) vinto ::= INTO expr */
176845 285, /* (252) vinto ::= */
176846 191, /* (253) cmd ::= PRAGMA nm dbnm */
176847 191, /* (254) cmd ::= PRAGMA nm dbnm EQ nmnum */
176848 191, /* (255) cmd ::= PRAGMA nm dbnm LP nmnum RP */
176849 191, /* (256) cmd ::= PRAGMA nm dbnm EQ minus_num */
176850 191, /* (257) cmd ::= PRAGMA nm dbnm LP minus_num RP */
176851 212, /* (258) plus_num ::= PLUS INTEGER|FLOAT */
176852 213, /* (259) minus_num ::= MINUS INTEGER|FLOAT */
176853 191, /* (260) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
176854 287, /* (261) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
176855 289, /* (262) trigger_time ::= BEFORE|AFTER */
176856 289, /* (263) trigger_time ::= INSTEAD OF */
176857 289, /* (264) trigger_time ::= */
176858 290, /* (265) trigger_event ::= DELETE|INSERT */
176859 290, /* (266) trigger_event ::= UPDATE */
176860 290, /* (267) trigger_event ::= UPDATE OF idlist */
176861 292, /* (268) when_clause ::= */
176862 292, /* (269) when_clause ::= WHEN expr */
176863 288, /* (270) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
176864 288, /* (271) trigger_cmd_list ::= trigger_cmd SEMI */
176865 294, /* (272) trnm ::= nm DOT nm */
176866 295, /* (273) tridxby ::= INDEXED BY nm */
176867 295, /* (274) tridxby ::= NOT INDEXED */
176868 293, /* (275) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
176869 293, /* (276) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
176870 293, /* (277) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
176871 293, /* (278) trigger_cmd ::= scanpt select scanpt */
176872 218, /* (279) expr ::= RAISE LP IGNORE RP */
176873 218, /* (280) expr ::= RAISE LP raisetype COMMA expr RP */
176874 237, /* (281) raisetype ::= ROLLBACK */
176875 237, /* (282) raisetype ::= ABORT */
176876 237, /* (283) raisetype ::= FAIL */
176877 191, /* (284) cmd ::= DROP TRIGGER ifexists fullname */
176878 191, /* (285) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
176879 191, /* (286) cmd ::= DETACH database_kw_opt expr */
176880 297, /* (287) key_opt ::= */
176881 297, /* (288) key_opt ::= KEY expr */
176882 191, /* (289) cmd ::= REINDEX */
176883 191, /* (290) cmd ::= REINDEX nm dbnm */
176884 191, /* (291) cmd ::= ANALYZE */
176885 191, /* (292) cmd ::= ANALYZE nm dbnm */
176886 191, /* (293) cmd ::= ALTER TABLE fullname RENAME TO nm */
176887 191, /* (294) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
176888 191, /* (295) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
176889 298, /* (296) add_column_fullname ::= fullname */
176890 191, /* (297) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
176891 191, /* (298) cmd ::= create_vtab */
176892 191, /* (299) cmd ::= create_vtab LP vtabarglist RP */
176893 300, /* (300) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
176894 302, /* (301) vtabarg ::= */
176895 303, /* (302) vtabargtoken ::= ANY */
176896 303, /* (303) vtabargtoken ::= lp anylist RP */
176897 304, /* (304) lp ::= LP */
176898 268, /* (305) with ::= WITH wqlist */
176899 268, /* (306) with ::= WITH RECURSIVE wqlist */
176900 307, /* (307) wqas ::= AS */
176901 307, /* (308) wqas ::= AS MATERIALIZED */
176902 307, /* (309) wqas ::= AS NOT MATERIALIZED */
176903 306, /* (310) wqitem ::= withnm eidlist_opt wqas LP select RP */
176904 308, /* (311) withnm ::= nm */
176905 242, /* (312) wqlist ::= wqitem */
176906 242, /* (313) wqlist ::= wqlist COMMA wqitem */
176907 309, /* (314) windowdefn_list ::= windowdefn_list COMMA windowdefn */
176908 310, /* (315) windowdefn ::= nm AS LP window RP */
176909 311, /* (316) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
176910 311, /* (317) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
176911 311, /* (318) window ::= ORDER BY sortlist frame_opt */
176912 311, /* (319) window ::= nm ORDER BY sortlist frame_opt */
176913 311, /* (320) window ::= nm frame_opt */
176914 312, /* (321) frame_opt ::= */
176915 312, /* (322) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
176916 312, /* (323) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
176917 316, /* (324) range_or_rows ::= RANGE|ROWS|GROUPS */
176918 318, /* (325) frame_bound_s ::= frame_bound */
176919 318, /* (326) frame_bound_s ::= UNBOUNDED PRECEDING */
176920 319, /* (327) frame_bound_e ::= frame_bound */
176921 319, /* (328) frame_bound_e ::= UNBOUNDED FOLLOWING */
176922 317, /* (329) frame_bound ::= expr PRECEDING|FOLLOWING */
176923 317, /* (330) frame_bound ::= CURRENT ROW */
176924 320, /* (331) frame_exclude_opt ::= */
176925 320, /* (332) frame_exclude_opt ::= EXCLUDE frame_exclude */
176926 321, /* (333) frame_exclude ::= NO OTHERS */
176927 321, /* (334) frame_exclude ::= CURRENT ROW */
176928 321, /* (335) frame_exclude ::= GROUP|TIES */
176929 252, /* (336) window_clause ::= WINDOW windowdefn_list */
176930 275, /* (337) filter_over ::= filter_clause over_clause */
176931 275, /* (338) filter_over ::= over_clause */
176932 275, /* (339) filter_over ::= filter_clause */
176933 315, /* (340) over_clause ::= OVER LP window RP */
176934 315, /* (341) over_clause ::= OVER nm */
176935 314, /* (342) filter_clause ::= FILTER LP WHERE expr RP */
176936 217, /* (343) term ::= QNUMBER */
176937 186, /* (344) input ::= cmdlist */
176938 187, /* (345) cmdlist ::= cmdlist ecmd */
176939 187, /* (346) cmdlist ::= ecmd */
176940 188, /* (347) ecmd ::= SEMI */
176941 188, /* (348) ecmd ::= cmdx SEMI */
176942 188, /* (349) ecmd ::= explain cmdx SEMI */
176943 193, /* (350) trans_opt ::= */
176944 193, /* (351) trans_opt ::= TRANSACTION */
176945 193, /* (352) trans_opt ::= TRANSACTION nm */
176946 195, /* (353) savepoint_opt ::= SAVEPOINT */
176947 195, /* (354) savepoint_opt ::= */
176948 191, /* (355) cmd ::= create_table create_table_args */
176949 204, /* (356) table_option_set ::= table_option */
176950 202, /* (357) columnlist ::= columnlist COMMA columnname carglist */
176951 202, /* (358) columnlist ::= columnname carglist */
176952 194, /* (359) nm ::= ID|INDEXED|JOIN_KW */
176953 194, /* (360) nm ::= STRING */
176954 209, /* (361) typetoken ::= typename */
176955 210, /* (362) typename ::= ID|STRING */
176956 211, /* (363) signed ::= plus_num */
176957 211, /* (364) signed ::= minus_num */
176958 208, /* (365) carglist ::= carglist ccons */
176959 208, /* (366) carglist ::= */
176960 216, /* (367) ccons ::= NULL onconf */
176961 216, /* (368) ccons ::= GENERATED ALWAYS AS generated */
176962 216, /* (369) ccons ::= AS generated */
176963 203, /* (370) conslist_opt ::= COMMA conslist */
176964 229, /* (371) conslist ::= conslist tconscomma tcons */
176965 229, /* (372) conslist ::= tcons */
176966 230, /* (373) tconscomma ::= */
176967 234, /* (374) defer_subclause_opt ::= defer_subclause */
176968 236, /* (375) resolvetype ::= raisetype */
176969 240, /* (376) selectnowith ::= oneselect */
176970 241, /* (377) oneselect ::= values */
176971 256, /* (378) sclp ::= selcollist COMMA */
176972 257, /* (379) as ::= ID|STRING */
176973 266, /* (380) indexed_opt ::= indexed_by */
176974 274, /* (381) returning ::= */
176975 218, /* (382) expr ::= term */
176976 276, /* (383) likeop ::= LIKE_KW|MATCH */
176977 280, /* (384) case_operand ::= expr */
176978 263, /* (385) exprlist ::= nexprlist */
176979 286, /* (386) nmnum ::= plus_num */
176980 286, /* (387) nmnum ::= nm */
176981 286, /* (388) nmnum ::= ON */
176982 286, /* (389) nmnum ::= DELETE */
176983 286, /* (390) nmnum ::= DEFAULT */
176984 212, /* (391) plus_num ::= INTEGER|FLOAT */
176985 291, /* (392) foreach_clause ::= */
176986 291, /* (393) foreach_clause ::= FOR EACH ROW */
176987 294, /* (394) trnm ::= nm */
176988 295, /* (395) tridxby ::= */
176989 296, /* (396) database_kw_opt ::= DATABASE */
176990 296, /* (397) database_kw_opt ::= */
176991 299, /* (398) kwcolumn_opt ::= */
176992 299, /* (399) kwcolumn_opt ::= COLUMNKW */
176993 301, /* (400) vtabarglist ::= vtabarg */
176994 301, /* (401) vtabarglist ::= vtabarglist COMMA vtabarg */
176995 302, /* (402) vtabarg ::= vtabarg vtabargtoken */
176996 305, /* (403) anylist ::= */
176997 305, /* (404) anylist ::= anylist LP anylist RP */
176998 305, /* (405) anylist ::= anylist ANY */
176999 268, /* (406) with ::= */
177000 309, /* (407) windowdefn_list ::= windowdefn */
177001 311, /* (408) window ::= frame_opt */
177002};
177003
177004/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
177005** of symbols on the right-hand side of that rule. */
177006static const signed char yyRuleInfoNRhs[] = {
177007 -1, /* (0) explain ::= EXPLAIN */
177008 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
177009 -1, /* (2) cmdx ::= cmd */
177010 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
177011 0, /* (4) transtype ::= */
177012 -1, /* (5) transtype ::= DEFERRED */
177013 -1, /* (6) transtype ::= IMMEDIATE */
177014 -1, /* (7) transtype ::= EXCLUSIVE */
177015 -2, /* (8) cmd ::= COMMIT|END trans_opt */
177016 -2, /* (9) cmd ::= ROLLBACK trans_opt */
177017 -2, /* (10) cmd ::= SAVEPOINT nm */
177018 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
177019 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
177020 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
177021 -1, /* (14) createkw ::= CREATE */
177022 0, /* (15) ifnotexists ::= */
177023 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
177024 -1, /* (17) temp ::= TEMP */
177025 0, /* (18) temp ::= */
177026 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
177027 -2, /* (20) create_table_args ::= AS select */
177028 0, /* (21) table_option_set ::= */
177029 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
177030 -2, /* (23) table_option ::= WITHOUT nm */
177031 -1, /* (24) table_option ::= nm */
177032 -2, /* (25) columnname ::= nm typetoken */
177033 0, /* (26) typetoken ::= */
177034 -4, /* (27) typetoken ::= typename LP signed RP */
177035 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
177036 -2, /* (29) typename ::= typename ID|STRING */
177037 0, /* (30) scanpt ::= */
177038 0, /* (31) scantok ::= */
177039 -2, /* (32) ccons ::= CONSTRAINT nm */
177040 -3, /* (33) ccons ::= DEFAULT scantok term */
177041 -4, /* (34) ccons ::= DEFAULT LP expr RP */
177042 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
177043 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
177044 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
177045 -3, /* (38) ccons ::= NOT NULL onconf */
177046 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
177047 -2, /* (40) ccons ::= UNIQUE onconf */
177048 -4, /* (41) ccons ::= CHECK LP expr RP */
177049 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
177050 -1, /* (43) ccons ::= defer_subclause */
177051 -2, /* (44) ccons ::= COLLATE ID|STRING */
177052 -3, /* (45) generated ::= LP expr RP */
177053 -4, /* (46) generated ::= LP expr RP ID */
177054 0, /* (47) autoinc ::= */
177055 -1, /* (48) autoinc ::= AUTOINCR */
177056 0, /* (49) refargs ::= */
177057 -2, /* (50) refargs ::= refargs refarg */
177058 -2, /* (51) refarg ::= MATCH nm */
177059 -3, /* (52) refarg ::= ON INSERT refact */
177060 -3, /* (53) refarg ::= ON DELETE refact */
177061 -3, /* (54) refarg ::= ON UPDATE refact */
177062 -2, /* (55) refact ::= SET NULL */
177063 -2, /* (56) refact ::= SET DEFAULT */
177064 -1, /* (57) refact ::= CASCADE */
177065 -1, /* (58) refact ::= RESTRICT */
177066 -2, /* (59) refact ::= NO ACTION */
177067 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
177068 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
177069 0, /* (62) init_deferred_pred_opt ::= */
177070 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
177071 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
177072 0, /* (65) conslist_opt ::= */
177073 -1, /* (66) tconscomma ::= COMMA */
177074 -2, /* (67) tcons ::= CONSTRAINT nm */
177075 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
177076 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
177077 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
177078 -10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
177079 0, /* (72) defer_subclause_opt ::= */
177080 0, /* (73) onconf ::= */
177081 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
177082 0, /* (75) orconf ::= */
177083 -2, /* (76) orconf ::= OR resolvetype */
177084 -1, /* (77) resolvetype ::= IGNORE */
177085 -1, /* (78) resolvetype ::= REPLACE */
177086 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
177087 -2, /* (80) ifexists ::= IF EXISTS */
177088 0, /* (81) ifexists ::= */
177089 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
177090 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
177091 -1, /* (84) cmd ::= select */
177092 -3, /* (85) select ::= WITH wqlist selectnowith */
177093 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
177094 -1, /* (87) select ::= selectnowith */
177095 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
177096 -1, /* (89) multiselect_op ::= UNION */
177097 -2, /* (90) multiselect_op ::= UNION ALL */
177098 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
177099 -9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
177100 -10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
177101 -4, /* (94) values ::= VALUES LP nexprlist RP */
177102 -1, /* (95) oneselect ::= mvalues */
177103 -5, /* (96) mvalues ::= values COMMA LP nexprlist RP */
177104 -5, /* (97) mvalues ::= mvalues COMMA LP nexprlist RP */
177105 -1, /* (98) distinct ::= DISTINCT */
177106 -1, /* (99) distinct ::= ALL */
177107 0, /* (100) distinct ::= */
177108 0, /* (101) sclp ::= */
177109 -5, /* (102) selcollist ::= sclp scanpt expr scanpt as */
177110 -3, /* (103) selcollist ::= sclp scanpt STAR */
177111 -5, /* (104) selcollist ::= sclp scanpt nm DOT STAR */
177112 -2, /* (105) as ::= AS nm */
177113 0, /* (106) as ::= */
177114 0, /* (107) from ::= */
177115 -2, /* (108) from ::= FROM seltablist */
177116 -2, /* (109) stl_prefix ::= seltablist joinop */
177117 0, /* (110) stl_prefix ::= */
177118 -5, /* (111) seltablist ::= stl_prefix nm dbnm as on_using */
177119 -6, /* (112) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
177120 -8, /* (113) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
177121 -6, /* (114) seltablist ::= stl_prefix LP select RP as on_using */
177122 -6, /* (115) seltablist ::= stl_prefix LP seltablist RP as on_using */
177123 0, /* (116) dbnm ::= */
177124 -2, /* (117) dbnm ::= DOT nm */
177125 -1, /* (118) fullname ::= nm */
177126 -3, /* (119) fullname ::= nm DOT nm */
177127 -1, /* (120) xfullname ::= nm */
177128 -3, /* (121) xfullname ::= nm DOT nm */
177129 -5, /* (122) xfullname ::= nm DOT nm AS nm */
177130 -3, /* (123) xfullname ::= nm AS nm */
177131 -1, /* (124) joinop ::= COMMA|JOIN */
177132 -2, /* (125) joinop ::= JOIN_KW JOIN */
177133 -3, /* (126) joinop ::= JOIN_KW nm JOIN */
177134 -4, /* (127) joinop ::= JOIN_KW nm nm JOIN */
177135 -2, /* (128) on_using ::= ON expr */
177136 -4, /* (129) on_using ::= USING LP idlist RP */
177137 0, /* (130) on_using ::= */
177138 0, /* (131) indexed_opt ::= */
177139 -3, /* (132) indexed_by ::= INDEXED BY nm */
177140 -2, /* (133) indexed_by ::= NOT INDEXED */
177141 0, /* (134) orderby_opt ::= */
177142 -3, /* (135) orderby_opt ::= ORDER BY sortlist */
177143 -5, /* (136) sortlist ::= sortlist COMMA expr sortorder nulls */
177144 -3, /* (137) sortlist ::= expr sortorder nulls */
177145 -1, /* (138) sortorder ::= ASC */
177146 -1, /* (139) sortorder ::= DESC */
177147 0, /* (140) sortorder ::= */
177148 -2, /* (141) nulls ::= NULLS FIRST */
177149 -2, /* (142) nulls ::= NULLS LAST */
177150 0, /* (143) nulls ::= */
177151 0, /* (144) groupby_opt ::= */
177152 -3, /* (145) groupby_opt ::= GROUP BY nexprlist */
177153 0, /* (146) having_opt ::= */
177154 -2, /* (147) having_opt ::= HAVING expr */
177155 0, /* (148) limit_opt ::= */
177156 -2, /* (149) limit_opt ::= LIMIT expr */
177157 -4, /* (150) limit_opt ::= LIMIT expr OFFSET expr */
177158 -4, /* (151) limit_opt ::= LIMIT expr COMMA expr */
177159 -6, /* (152) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
177160 0, /* (153) where_opt ::= */
177161 -2, /* (154) where_opt ::= WHERE expr */
177162 0, /* (155) where_opt_ret ::= */
177163 -2, /* (156) where_opt_ret ::= WHERE expr */
177164 -2, /* (157) where_opt_ret ::= RETURNING selcollist */
177165 -4, /* (158) where_opt_ret ::= WHERE expr RETURNING selcollist */
177166 -9, /* (159) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
177167 -5, /* (160) setlist ::= setlist COMMA nm EQ expr */
177168 -7, /* (161) setlist ::= setlist COMMA LP idlist RP EQ expr */
177169 -3, /* (162) setlist ::= nm EQ expr */
177170 -5, /* (163) setlist ::= LP idlist RP EQ expr */
177171 -7, /* (164) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
177172 -8, /* (165) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
177173 0, /* (166) upsert ::= */
177174 -2, /* (167) upsert ::= RETURNING selcollist */
177175 -12, /* (168) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
177176 -9, /* (169) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
177177 -5, /* (170) upsert ::= ON CONFLICT DO NOTHING returning */
177178 -8, /* (171) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
177179 -2, /* (172) returning ::= RETURNING selcollist */
177180 -2, /* (173) insert_cmd ::= INSERT orconf */
177181 -1, /* (174) insert_cmd ::= REPLACE */
177182 0, /* (175) idlist_opt ::= */
177183 -3, /* (176) idlist_opt ::= LP idlist RP */
177184 -3, /* (177) idlist ::= idlist COMMA nm */
177185 -1, /* (178) idlist ::= nm */
177186 -3, /* (179) expr ::= LP expr RP */
177187 -1, /* (180) expr ::= ID|INDEXED|JOIN_KW */
177188 -3, /* (181) expr ::= nm DOT nm */
177189 -5, /* (182) expr ::= nm DOT nm DOT nm */
177190 -1, /* (183) term ::= NULL|FLOAT|BLOB */
177191 -1, /* (184) term ::= STRING */
177192 -1, /* (185) term ::= INTEGER */
177193 -1, /* (186) expr ::= VARIABLE */
177194 -3, /* (187) expr ::= expr COLLATE ID|STRING */
177195 -6, /* (188) expr ::= CAST LP expr AS typetoken RP */
177196 -5, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
177197 -8, /* (190) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
177198 -4, /* (191) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
177199 -6, /* (192) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
177200 -9, /* (193) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
177201 -5, /* (194) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
177202 -1, /* (195) term ::= CTIME_KW */
177203 -5, /* (196) expr ::= LP nexprlist COMMA expr RP */
177204 -3, /* (197) expr ::= expr AND expr */
177205 -3, /* (198) expr ::= expr OR expr */
177206 -3, /* (199) expr ::= expr LT|GT|GE|LE expr */
177207 -3, /* (200) expr ::= expr EQ|NE expr */
177208 -3, /* (201) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
177209 -3, /* (202) expr ::= expr PLUS|MINUS expr */
177210 -3, /* (203) expr ::= expr STAR|SLASH|REM expr */
177211 -3, /* (204) expr ::= expr CONCAT expr */
177212 -2, /* (205) likeop ::= NOT LIKE_KW|MATCH */
177213 -3, /* (206) expr ::= expr likeop expr */
177214 -5, /* (207) expr ::= expr likeop expr ESCAPE expr */
177215 -2, /* (208) expr ::= expr ISNULL|NOTNULL */
177216 -3, /* (209) expr ::= expr NOT NULL */
177217 -3, /* (210) expr ::= expr IS expr */
177218 -4, /* (211) expr ::= expr IS NOT expr */
177219 -6, /* (212) expr ::= expr IS NOT DISTINCT FROM expr */
177220 -5, /* (213) expr ::= expr IS DISTINCT FROM expr */
177221 -2, /* (214) expr ::= NOT expr */
177222 -2, /* (215) expr ::= BITNOT expr */
177223 -2, /* (216) expr ::= PLUS|MINUS expr */
177224 -3, /* (217) expr ::= expr PTR expr */
177225 -1, /* (218) between_op ::= BETWEEN */
177226 -2, /* (219) between_op ::= NOT BETWEEN */
177227 -5, /* (220) expr ::= expr between_op expr AND expr */
177228 -1, /* (221) in_op ::= IN */
177229 -2, /* (222) in_op ::= NOT IN */
177230 -5, /* (223) expr ::= expr in_op LP exprlist RP */
177231 -3, /* (224) expr ::= LP select RP */
177232 -5, /* (225) expr ::= expr in_op LP select RP */
177233 -5, /* (226) expr ::= expr in_op nm dbnm paren_exprlist */
177234 -4, /* (227) expr ::= EXISTS LP select RP */
177235 -5, /* (228) expr ::= CASE case_operand case_exprlist case_else END */
177236 -5, /* (229) case_exprlist ::= case_exprlist WHEN expr THEN expr */
177237 -4, /* (230) case_exprlist ::= WHEN expr THEN expr */
177238 -2, /* (231) case_else ::= ELSE expr */
177239 0, /* (232) case_else ::= */
177240 0, /* (233) case_operand ::= */
177241 0, /* (234) exprlist ::= */
177242 -3, /* (235) nexprlist ::= nexprlist COMMA expr */
177243 -1, /* (236) nexprlist ::= expr */
177244 0, /* (237) paren_exprlist ::= */
177245 -3, /* (238) paren_exprlist ::= LP exprlist RP */
177246 -12, /* (239) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
177247 -1, /* (240) uniqueflag ::= UNIQUE */
177248 0, /* (241) uniqueflag ::= */
177249 0, /* (242) eidlist_opt ::= */
177250 -3, /* (243) eidlist_opt ::= LP eidlist RP */
177251 -5, /* (244) eidlist ::= eidlist COMMA nm collate sortorder */
177252 -3, /* (245) eidlist ::= nm collate sortorder */
177253 0, /* (246) collate ::= */
177254 -2, /* (247) collate ::= COLLATE ID|STRING */
177255 -4, /* (248) cmd ::= DROP INDEX ifexists fullname */
177256 -2, /* (249) cmd ::= VACUUM vinto */
177257 -3, /* (250) cmd ::= VACUUM nm vinto */
177258 -2, /* (251) vinto ::= INTO expr */
177259 0, /* (252) vinto ::= */
177260 -3, /* (253) cmd ::= PRAGMA nm dbnm */
177261 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ nmnum */
177262 -6, /* (255) cmd ::= PRAGMA nm dbnm LP nmnum RP */
177263 -5, /* (256) cmd ::= PRAGMA nm dbnm EQ minus_num */
177264 -6, /* (257) cmd ::= PRAGMA nm dbnm LP minus_num RP */
177265 -2, /* (258) plus_num ::= PLUS INTEGER|FLOAT */
177266 -2, /* (259) minus_num ::= MINUS INTEGER|FLOAT */
177267 -5, /* (260) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
177268 -11, /* (261) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
177269 -1, /* (262) trigger_time ::= BEFORE|AFTER */
177270 -2, /* (263) trigger_time ::= INSTEAD OF */
177271 0, /* (264) trigger_time ::= */
177272 -1, /* (265) trigger_event ::= DELETE|INSERT */
177273 -1, /* (266) trigger_event ::= UPDATE */
177274 -3, /* (267) trigger_event ::= UPDATE OF idlist */
177275 0, /* (268) when_clause ::= */
177276 -2, /* (269) when_clause ::= WHEN expr */
177277 -3, /* (270) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
177278 -2, /* (271) trigger_cmd_list ::= trigger_cmd SEMI */
177279 -3, /* (272) trnm ::= nm DOT nm */
177280 -3, /* (273) tridxby ::= INDEXED BY nm */
177281 -2, /* (274) tridxby ::= NOT INDEXED */
177282 -9, /* (275) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
177283 -8, /* (276) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
177284 -6, /* (277) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
177285 -3, /* (278) trigger_cmd ::= scanpt select scanpt */
177286 -4, /* (279) expr ::= RAISE LP IGNORE RP */
177287 -6, /* (280) expr ::= RAISE LP raisetype COMMA expr RP */
177288 -1, /* (281) raisetype ::= ROLLBACK */
177289 -1, /* (282) raisetype ::= ABORT */
177290 -1, /* (283) raisetype ::= FAIL */
177291 -4, /* (284) cmd ::= DROP TRIGGER ifexists fullname */
177292 -6, /* (285) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
177293 -3, /* (286) cmd ::= DETACH database_kw_opt expr */
177294 0, /* (287) key_opt ::= */
177295 -2, /* (288) key_opt ::= KEY expr */
177296 -1, /* (289) cmd ::= REINDEX */
177297 -3, /* (290) cmd ::= REINDEX nm dbnm */
177298 -1, /* (291) cmd ::= ANALYZE */
177299 -3, /* (292) cmd ::= ANALYZE nm dbnm */
177300 -6, /* (293) cmd ::= ALTER TABLE fullname RENAME TO nm */
177301 -7, /* (294) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
177302 -6, /* (295) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
177303 -1, /* (296) add_column_fullname ::= fullname */
177304 -8, /* (297) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
177305 -1, /* (298) cmd ::= create_vtab */
177306 -4, /* (299) cmd ::= create_vtab LP vtabarglist RP */
177307 -8, /* (300) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
177308 0, /* (301) vtabarg ::= */
177309 -1, /* (302) vtabargtoken ::= ANY */
177310 -3, /* (303) vtabargtoken ::= lp anylist RP */
177311 -1, /* (304) lp ::= LP */
177312 -2, /* (305) with ::= WITH wqlist */
177313 -3, /* (306) with ::= WITH RECURSIVE wqlist */
177314 -1, /* (307) wqas ::= AS */
177315 -2, /* (308) wqas ::= AS MATERIALIZED */
177316 -3, /* (309) wqas ::= AS NOT MATERIALIZED */
177317 -6, /* (310) wqitem ::= withnm eidlist_opt wqas LP select RP */
177318 -1, /* (311) withnm ::= nm */
177319 -1, /* (312) wqlist ::= wqitem */
177320 -3, /* (313) wqlist ::= wqlist COMMA wqitem */
177321 -3, /* (314) windowdefn_list ::= windowdefn_list COMMA windowdefn */
177322 -5, /* (315) windowdefn ::= nm AS LP window RP */
177323 -5, /* (316) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
177324 -6, /* (317) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
177325 -4, /* (318) window ::= ORDER BY sortlist frame_opt */
177326 -5, /* (319) window ::= nm ORDER BY sortlist frame_opt */
177327 -2, /* (320) window ::= nm frame_opt */
177328 0, /* (321) frame_opt ::= */
177329 -3, /* (322) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
177330 -6, /* (323) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
177331 -1, /* (324) range_or_rows ::= RANGE|ROWS|GROUPS */
177332 -1, /* (325) frame_bound_s ::= frame_bound */
177333 -2, /* (326) frame_bound_s ::= UNBOUNDED PRECEDING */
177334 -1, /* (327) frame_bound_e ::= frame_bound */
177335 -2, /* (328) frame_bound_e ::= UNBOUNDED FOLLOWING */
177336 -2, /* (329) frame_bound ::= expr PRECEDING|FOLLOWING */
177337 -2, /* (330) frame_bound ::= CURRENT ROW */
177338 0, /* (331) frame_exclude_opt ::= */
177339 -2, /* (332) frame_exclude_opt ::= EXCLUDE frame_exclude */
177340 -2, /* (333) frame_exclude ::= NO OTHERS */
177341 -2, /* (334) frame_exclude ::= CURRENT ROW */
177342 -1, /* (335) frame_exclude ::= GROUP|TIES */
177343 -2, /* (336) window_clause ::= WINDOW windowdefn_list */
177344 -2, /* (337) filter_over ::= filter_clause over_clause */
177345 -1, /* (338) filter_over ::= over_clause */
177346 -1, /* (339) filter_over ::= filter_clause */
177347 -4, /* (340) over_clause ::= OVER LP window RP */
177348 -2, /* (341) over_clause ::= OVER nm */
177349 -5, /* (342) filter_clause ::= FILTER LP WHERE expr RP */
177350 -1, /* (343) term ::= QNUMBER */
177351 -1, /* (344) input ::= cmdlist */
177352 -2, /* (345) cmdlist ::= cmdlist ecmd */
177353 -1, /* (346) cmdlist ::= ecmd */
177354 -1, /* (347) ecmd ::= SEMI */
177355 -2, /* (348) ecmd ::= cmdx SEMI */
177356 -3, /* (349) ecmd ::= explain cmdx SEMI */
177357 0, /* (350) trans_opt ::= */
177358 -1, /* (351) trans_opt ::= TRANSACTION */
177359 -2, /* (352) trans_opt ::= TRANSACTION nm */
177360 -1, /* (353) savepoint_opt ::= SAVEPOINT */
177361 0, /* (354) savepoint_opt ::= */
177362 -2, /* (355) cmd ::= create_table create_table_args */
177363 -1, /* (356) table_option_set ::= table_option */
177364 -4, /* (357) columnlist ::= columnlist COMMA columnname carglist */
177365 -2, /* (358) columnlist ::= columnname carglist */
177366 -1, /* (359) nm ::= ID|INDEXED|JOIN_KW */
177367 -1, /* (360) nm ::= STRING */
177368 -1, /* (361) typetoken ::= typename */
177369 -1, /* (362) typename ::= ID|STRING */
177370 -1, /* (363) signed ::= plus_num */
177371 -1, /* (364) signed ::= minus_num */
177372 -2, /* (365) carglist ::= carglist ccons */
177373 0, /* (366) carglist ::= */
177374 -2, /* (367) ccons ::= NULL onconf */
177375 -4, /* (368) ccons ::= GENERATED ALWAYS AS generated */
177376 -2, /* (369) ccons ::= AS generated */
177377 -2, /* (370) conslist_opt ::= COMMA conslist */
177378 -3, /* (371) conslist ::= conslist tconscomma tcons */
177379 -1, /* (372) conslist ::= tcons */
177380 0, /* (373) tconscomma ::= */
177381 -1, /* (374) defer_subclause_opt ::= defer_subclause */
177382 -1, /* (375) resolvetype ::= raisetype */
177383 -1, /* (376) selectnowith ::= oneselect */
177384 -1, /* (377) oneselect ::= values */
177385 -2, /* (378) sclp ::= selcollist COMMA */
177386 -1, /* (379) as ::= ID|STRING */
177387 -1, /* (380) indexed_opt ::= indexed_by */
177388 0, /* (381) returning ::= */
177389 -1, /* (382) expr ::= term */
177390 -1, /* (383) likeop ::= LIKE_KW|MATCH */
177391 -1, /* (384) case_operand ::= expr */
177392 -1, /* (385) exprlist ::= nexprlist */
177393 -1, /* (386) nmnum ::= plus_num */
177394 -1, /* (387) nmnum ::= nm */
177395 -1, /* (388) nmnum ::= ON */
177396 -1, /* (389) nmnum ::= DELETE */
177397 -1, /* (390) nmnum ::= DEFAULT */
177398 -1, /* (391) plus_num ::= INTEGER|FLOAT */
177399 0, /* (392) foreach_clause ::= */
177400 -3, /* (393) foreach_clause ::= FOR EACH ROW */
177401 -1, /* (394) trnm ::= nm */
177402 0, /* (395) tridxby ::= */
177403 -1, /* (396) database_kw_opt ::= DATABASE */
177404 0, /* (397) database_kw_opt ::= */
177405 0, /* (398) kwcolumn_opt ::= */
177406 -1, /* (399) kwcolumn_opt ::= COLUMNKW */
177407 -1, /* (400) vtabarglist ::= vtabarg */
177408 -3, /* (401) vtabarglist ::= vtabarglist COMMA vtabarg */
177409 -2, /* (402) vtabarg ::= vtabarg vtabargtoken */
177410 0, /* (403) anylist ::= */
177411 -4, /* (404) anylist ::= anylist LP anylist RP */
177412 -2, /* (405) anylist ::= anylist ANY */
177413 0, /* (406) with ::= */
177414 -1, /* (407) windowdefn_list ::= windowdefn */
177415 -1, /* (408) window ::= frame_opt */
177416};
177417
177418static void yy_accept(yyParser*); /* Forward Declaration */
177419
177420/*
177421** Perform a reduce action and the shift that must immediately
177422** follow the reduce.
177423**
177424** The yyLookahead and yyLookaheadToken parameters provide reduce actions
177425** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
177426** if the lookahead token has already been consumed. As this procedure is
177427** only called from one place, optimizing compilers will in-line it, which
177428** means that the extra parameters have no performance impact.
177429*/
177430static YYACTIONTYPEunsigned short int yy_reduce(
177431 yyParser *yypParser, /* The parser */
177432 unsigned int yyruleno, /* Number of the rule by which to reduce */
177433 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
177434 sqlite3ParserTOKENTYPEToken yyLookaheadToken /* Value of the lookahead token */
177435 sqlite3ParserCTX_PDECL,Parse *pParse /* %extra_context */
177436){
177437 int yygoto; /* The next state */
177438 YYACTIONTYPEunsigned short int yyact; /* The next action */
177439 yyStackEntry *yymsp; /* The top of the parser's stack */
177440 int yysize; /* Amount to pop the stack */
177441 sqlite3ParserARG_FETCH
177442 (void)yyLookahead;
177443 (void)yyLookaheadToken;
177444 yymsp = yypParser->yytos;
177445
177446 switch( yyruleno ){
177447 /* Beginning here are the reduction cases. A typical example
177448 ** follows:
177449 ** case 0:
177450 ** #line <lineno> <grammarfile>
177451 ** { ... } // User supplied code
177452 ** #line <lineno> <thisfile>
177453 ** break;
177454 */
177455/********** Begin reduce actions **********************************************/
177456 YYMINORTYPE yylhsminor;
177457 case 0: /* explain ::= EXPLAIN */
177458{ if( pParse->pReprepare==0 ) pParse->explain = 1; }
177459 break;
177460 case 1: /* explain ::= EXPLAIN QUERY PLAN */
177461{ if( pParse->pReprepare==0 ) pParse->explain = 2; }
177462 break;
177463 case 2: /* cmdx ::= cmd */
177464{ sqlite3FinishCoding(pParse); }
177465 break;
177466 case 3: /* cmd ::= BEGIN transtype trans_opt */
177467{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy144);}
177468 break;
177469 case 4: /* transtype ::= */
177470{yymsp[1].minor.yy144 = TK_DEFERRED7;}
177471 break;
177472 case 5: /* transtype ::= DEFERRED */
177473 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
177474 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
177475 case 324: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==324);
177476{yymsp[0].minor.yy144 = yymsp[0].major; /*A-overwrites-X*/}
177477 break;
177478 case 8: /* cmd ::= COMMIT|END trans_opt */
177479 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
177480{sqlite3EndTransaction(pParse,yymsp[-1].major);}
177481 break;
177482 case 10: /* cmd ::= SAVEPOINT nm */
177483{
177484 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN0, &yymsp[0].minor.yy0);
177485}
177486 break;
177487 case 11: /* cmd ::= RELEASE savepoint_opt nm */
177488{
177489 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE1, &yymsp[0].minor.yy0);
177490}
177491 break;
177492 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
177493{
177494 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK2, &yymsp[0].minor.yy0);
177495}
177496 break;
177497 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
177498{
177499 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy144,0,0,yymsp[-2].minor.yy144);
177500}
177501 break;
177502 case 14: /* createkw ::= CREATE */
177503{disableLookaside(pParse);}
177504 break;
177505 case 15: /* ifnotexists ::= */
177506 case 18: /* temp ::= */ yytestcase(yyruleno==18);
177507 case 47: /* autoinc ::= */ yytestcase(yyruleno==47);
177508 case 62: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==62);
177509 case 72: /* defer_subclause_opt ::= */ yytestcase(yyruleno==72);
177510 case 81: /* ifexists ::= */ yytestcase(yyruleno==81);
177511 case 100: /* distinct ::= */ yytestcase(yyruleno==100);
177512 case 246: /* collate ::= */ yytestcase(yyruleno==246);
177513{yymsp[1].minor.yy144 = 0;}
177514 break;
177515 case 16: /* ifnotexists ::= IF NOT EXISTS */
177516{yymsp[-2].minor.yy144 = 1;}
177517 break;
177518 case 17: /* temp ::= TEMP */
177519{yymsp[0].minor.yy144 = pParse->db->init.busy==0;}
177520 break;
177521 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */
177522{
177523 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy391,0);
177524}
177525 break;
177526 case 20: /* create_table_args ::= AS select */
177527{
177528 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy555);
177529 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy555);
177530}
177531 break;
177532 case 21: /* table_option_set ::= */
177533{yymsp[1].minor.yy391 = 0;}
177534 break;
177535 case 22: /* table_option_set ::= table_option_set COMMA table_option */
177536{yylhsminor.yy391 = yymsp[-2].minor.yy391|yymsp[0].minor.yy391;}
177537 yymsp[-2].minor.yy391 = yylhsminor.yy391;
177538 break;
177539 case 23: /* table_option ::= WITHOUT nm */
177540{
177541 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
177542 yymsp[-1].minor.yy391 = TF_WithoutRowid0x00000080 | TF_NoVisibleRowid0x00000200;
177543 }else{
177544 yymsp[-1].minor.yy391 = 0;
177545 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
177546 }
177547}
177548 break;
177549 case 24: /* table_option ::= nm */
177550{
177551 if( yymsp[0].minor.yy0.n==6 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"strict",6)==0 ){
177552 yylhsminor.yy391 = TF_Strict0x00010000;
177553 }else{
177554 yylhsminor.yy391 = 0;
177555 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
177556 }
177557}
177558 yymsp[0].minor.yy391 = yylhsminor.yy391;
177559 break;
177560 case 25: /* columnname ::= nm typetoken */
177561{sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
177562 break;
177563 case 26: /* typetoken ::= */
177564 case 65: /* conslist_opt ::= */ yytestcase(yyruleno==65);
177565 case 106: /* as ::= */ yytestcase(yyruleno==106);
177566{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
177567 break;
177568 case 27: /* typetoken ::= typename LP signed RP */
177569{
177570 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
177571}
177572 break;
177573 case 28: /* typetoken ::= typename LP signed COMMA signed RP */
177574{
177575 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
177576}
177577 break;
177578 case 29: /* typename ::= typename ID|STRING */
177579{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
177580 break;
177581 case 30: /* scanpt ::= */
177582{
177583 assert( yyLookahead!=YYNOCODE )((void) (0));
177584 yymsp[1].minor.yy168 = yyLookaheadToken.z;
177585}
177586 break;
177587 case 31: /* scantok ::= */
177588{
177589 assert( yyLookahead!=YYNOCODE )((void) (0));
177590 yymsp[1].minor.yy0 = yyLookaheadToken;
177591}
177592 break;
177593 case 32: /* ccons ::= CONSTRAINT nm */
177594 case 67: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==67);
177595{pParse->constraintName = yymsp[0].minor.yy0;}
177596 break;
177597 case 33: /* ccons ::= DEFAULT scantok term */
177598{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy454,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
177599 break;
177600 case 34: /* ccons ::= DEFAULT LP expr RP */
177601{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy454,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
177602 break;
177603 case 35: /* ccons ::= DEFAULT PLUS scantok term */
177604{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy454,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
177605 break;
177606 case 36: /* ccons ::= DEFAULT MINUS scantok term */
177607{
177608 Expr *p = sqlite3PExpr(pParse, TK_UMINUS174, yymsp[0].minor.yy454, 0);
177609 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
177610}
177611 break;
177612 case 37: /* ccons ::= DEFAULT scantok ID|INDEXED */
177613{
177614 Expr *p = tokenExpr(pParse, TK_STRING118, yymsp[0].minor.yy0);
177615 if( p ){
177616 sqlite3ExprIdToTrueFalse(p);
177617 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
177618 }
177619 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
177620}
177621 break;
177622 case 38: /* ccons ::= NOT NULL onconf */
177623{sqlite3AddNotNull(pParse, yymsp[0].minor.yy144);}
177624 break;
177625 case 39: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
177626{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy144,yymsp[0].minor.yy144,yymsp[-2].minor.yy144);}
177627 break;
177628 case 40: /* ccons ::= UNIQUE onconf */
177629{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy144,0,0,0,0,
177630 SQLITE_IDXTYPE_UNIQUE1);}
177631 break;
177632 case 41: /* ccons ::= CHECK LP expr RP */
177633{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy454,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z);}
177634 break;
177635 case 42: /* ccons ::= REFERENCES nm eidlist_opt refargs */
177636{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy144);}
177637 break;
177638 case 43: /* ccons ::= defer_subclause */
177639{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy144);}
177640 break;
177641 case 44: /* ccons ::= COLLATE ID|STRING */
177642{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
177643 break;
177644 case 45: /* generated ::= LP expr RP */
177645{sqlite3AddGenerated(pParse,yymsp[-1].minor.yy454,0);}
177646 break;
177647 case 46: /* generated ::= LP expr RP ID */
177648{sqlite3AddGenerated(pParse,yymsp[-2].minor.yy454,&yymsp[0].minor.yy0);}
177649 break;
177650 case 48: /* autoinc ::= AUTOINCR */
177651{yymsp[0].minor.yy144 = 1;}
177652 break;
177653 case 49: /* refargs ::= */
177654{ yymsp[1].minor.yy144 = OE_None0*0x0101; /* EV: R-19803-45884 */}
177655 break;
177656 case 50: /* refargs ::= refargs refarg */
177657{ yymsp[-1].minor.yy144 = (yymsp[-1].minor.yy144 & ~yymsp[0].minor.yy383.mask) | yymsp[0].minor.yy383.value; }
177658 break;
177659 case 51: /* refarg ::= MATCH nm */
177660{ yymsp[-1].minor.yy383.value = 0; yymsp[-1].minor.yy383.mask = 0x000000; }
177661 break;
177662 case 52: /* refarg ::= ON INSERT refact */
177663{ yymsp[-2].minor.yy383.value = 0; yymsp[-2].minor.yy383.mask = 0x000000; }
177664 break;
177665 case 53: /* refarg ::= ON DELETE refact */
177666{ yymsp[-2].minor.yy383.value = yymsp[0].minor.yy144; yymsp[-2].minor.yy383.mask = 0x0000ff; }
177667 break;
177668 case 54: /* refarg ::= ON UPDATE refact */
177669{ yymsp[-2].minor.yy383.value = yymsp[0].minor.yy144<<8; yymsp[-2].minor.yy383.mask = 0x00ff00; }
177670 break;
177671 case 55: /* refact ::= SET NULL */
177672{ yymsp[-1].minor.yy144 = OE_SetNull8; /* EV: R-33326-45252 */}
177673 break;
177674 case 56: /* refact ::= SET DEFAULT */
177675{ yymsp[-1].minor.yy144 = OE_SetDflt9; /* EV: R-33326-45252 */}
177676 break;
177677 case 57: /* refact ::= CASCADE */
177678{ yymsp[0].minor.yy144 = OE_Cascade10; /* EV: R-33326-45252 */}
177679 break;
177680 case 58: /* refact ::= RESTRICT */
177681{ yymsp[0].minor.yy144 = OE_Restrict7; /* EV: R-33326-45252 */}
177682 break;
177683 case 59: /* refact ::= NO ACTION */
177684{ yymsp[-1].minor.yy144 = OE_None0; /* EV: R-33326-45252 */}
177685 break;
177686 case 60: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
177687{yymsp[-2].minor.yy144 = 0;}
177688 break;
177689 case 61: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
177690 case 76: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==76);
177691 case 173: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==173);
177692{yymsp[-1].minor.yy144 = yymsp[0].minor.yy144;}
177693 break;
177694 case 63: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
177695 case 80: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==80);
177696 case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
177697 case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
177698 case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
177699{yymsp[-1].minor.yy144 = 1;}
177700 break;
177701 case 64: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
177702{yymsp[-1].minor.yy144 = 0;}
177703 break;
177704 case 66: /* tconscomma ::= COMMA */
177705{pParse->constraintName.n = 0;}
177706 break;
177707 case 68: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
177708{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy144,yymsp[-2].minor.yy144,0);}
177709 break;
177710 case 69: /* tcons ::= UNIQUE LP sortlist RP onconf */
177711{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy144,0,0,0,0,
177712 SQLITE_IDXTYPE_UNIQUE1);}
177713 break;
177714 case 70: /* tcons ::= CHECK LP expr RP onconf */
177715{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy454,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z);}
177716 break;
177717 case 71: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
177718{
177719 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy144);
177720 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy144);
177721}
177722 break;
177723 case 73: /* onconf ::= */
177724 case 75: /* orconf ::= */ yytestcase(yyruleno==75);
177725{yymsp[1].minor.yy144 = OE_Default11;}
177726 break;
177727 case 74: /* onconf ::= ON CONFLICT resolvetype */
177728{yymsp[-2].minor.yy144 = yymsp[0].minor.yy144;}
177729 break;
177730 case 77: /* resolvetype ::= IGNORE */
177731{yymsp[0].minor.yy144 = OE_Ignore4;}
177732 break;
177733 case 78: /* resolvetype ::= REPLACE */
177734 case 174: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==174);
177735{yymsp[0].minor.yy144 = OE_Replace5;}
177736 break;
177737 case 79: /* cmd ::= DROP TABLE ifexists fullname */
177738{
177739 sqlite3DropTable(pParse, yymsp[0].minor.yy203, 0, yymsp[-1].minor.yy144);
177740}
177741 break;
177742 case 82: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
177743{
177744 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[0].minor.yy555, yymsp[-7].minor.yy144, yymsp[-5].minor.yy144);
177745}
177746 break;
177747 case 83: /* cmd ::= DROP VIEW ifexists fullname */
177748{
177749 sqlite3DropTable(pParse, yymsp[0].minor.yy203, 1, yymsp[-1].minor.yy144);
177750}
177751 break;
177752 case 84: /* cmd ::= select */
177753{
177754 SelectDest dest = {SRT_Output9, 0, 0, 0, 0, 0, 0};
177755 sqlite3Select(pParse, yymsp[0].minor.yy555, &dest);
177756 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy555);
177757}
177758 break;
177759 case 85: /* select ::= WITH wqlist selectnowith */
177760{yymsp[-2].minor.yy555 = attachWithToSelect(pParse,yymsp[0].minor.yy555,yymsp[-1].minor.yy59);}
177761 break;
177762 case 86: /* select ::= WITH RECURSIVE wqlist selectnowith */
177763{yymsp[-3].minor.yy555 = attachWithToSelect(pParse,yymsp[0].minor.yy555,yymsp[-1].minor.yy59);}
177764 break;
177765 case 87: /* select ::= selectnowith */
177766{
177767 Select *p = yymsp[0].minor.yy555;
177768 if( p ){
177769 parserDoubleLinkSelect(pParse, p);
177770 }
177771}
177772 break;
177773 case 88: /* selectnowith ::= selectnowith multiselect_op oneselect */
177774{
177775 Select *pRhs = yymsp[0].minor.yy555;
177776 Select *pLhs = yymsp[-2].minor.yy555;
177777 if( pRhs && pRhs->pPrior ){
177778 SrcList *pFrom;
177779 Token x;
177780 x.n = 0;
177781 parserDoubleLinkSelect(pParse, pRhs);
177782 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0);
177783 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
177784 }
177785 if( pRhs ){
177786 pRhs->op = (u8)yymsp[-1].minor.yy144;
177787 pRhs->pPrior = pLhs;
177788 if( ALWAYS(pLhs)(pLhs) ) pLhs->selFlags &= ~SF_MultiValue0x0000400;
177789 pRhs->selFlags &= ~SF_MultiValue0x0000400;
177790 if( yymsp[-1].minor.yy144!=TK_ALL136 ) pParse->hasCompound = 1;
177791 }else{
177792 sqlite3SelectDelete(pParse->db, pLhs);
177793 }
177794 yymsp[-2].minor.yy555 = pRhs;
177795}
177796 break;
177797 case 89: /* multiselect_op ::= UNION */
177798 case 91: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==91);
177799{yymsp[0].minor.yy144 = yymsp[0].major; /*A-overwrites-OP*/}
177800 break;
177801 case 90: /* multiselect_op ::= UNION ALL */
177802{yymsp[-1].minor.yy144 = TK_ALL136;}
177803 break;
177804 case 92: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
177805{
177806 yymsp[-8].minor.yy555 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy203,yymsp[-4].minor.yy454,yymsp[-3].minor.yy14,yymsp[-2].minor.yy454,yymsp[-1].minor.yy14,yymsp[-7].minor.yy144,yymsp[0].minor.yy454);
177807}
177808 break;
177809 case 93: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
177810{
177811 yymsp[-9].minor.yy555 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy14,yymsp[-6].minor.yy203,yymsp[-5].minor.yy454,yymsp[-4].minor.yy14,yymsp[-3].minor.yy454,yymsp[-1].minor.yy14,yymsp[-8].minor.yy144,yymsp[0].minor.yy454);
177812 if( yymsp[-9].minor.yy555 ){
177813 yymsp[-9].minor.yy555->pWinDefn = yymsp[-2].minor.yy211;
177814 }else{
177815 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy211);
177816 }
177817}
177818 break;
177819 case 94: /* values ::= VALUES LP nexprlist RP */
177820{
177821 yymsp[-3].minor.yy555 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values0x0000200,0);
177822}
177823 break;
177824 case 95: /* oneselect ::= mvalues */
177825{
177826 sqlite3MultiValuesEnd(pParse, yymsp[0].minor.yy555);
177827}
177828 break;
177829 case 96: /* mvalues ::= values COMMA LP nexprlist RP */
177830 case 97: /* mvalues ::= mvalues COMMA LP nexprlist RP */ yytestcase(yyruleno==97);
177831{
177832 yymsp[-4].minor.yy555 = sqlite3MultiValues(pParse, yymsp[-4].minor.yy555, yymsp[-1].minor.yy14);
177833}
177834 break;
177835 case 98: /* distinct ::= DISTINCT */
177836{yymsp[0].minor.yy144 = SF_Distinct0x0000001;}
177837 break;
177838 case 99: /* distinct ::= ALL */
177839{yymsp[0].minor.yy144 = SF_All0x0000002;}
177840 break;
177841 case 101: /* sclp ::= */
177842 case 134: /* orderby_opt ::= */ yytestcase(yyruleno==134);
177843 case 144: /* groupby_opt ::= */ yytestcase(yyruleno==144);
177844 case 234: /* exprlist ::= */ yytestcase(yyruleno==234);
177845 case 237: /* paren_exprlist ::= */ yytestcase(yyruleno==237);
177846 case 242: /* eidlist_opt ::= */ yytestcase(yyruleno==242);
177847{yymsp[1].minor.yy14 = 0;}
177848 break;
177849 case 102: /* selcollist ::= sclp scanpt expr scanpt as */
177850{
177851 yymsp[-4].minor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[-2].minor.yy454);
177852 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy14, &yymsp[0].minor.yy0, 1);
177853 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy14,yymsp[-3].minor.yy168,yymsp[-1].minor.yy168);
177854}
177855 break;
177856 case 103: /* selcollist ::= sclp scanpt STAR */
177857{
177858 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK180, 0);
177859 sqlite3ExprSetErrorOffset(p, (int)(yymsp[0].minor.yy0.z - pParse->zTail));
177860 yymsp[-2].minor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, p);
177861}
177862 break;
177863 case 104: /* selcollist ::= sclp scanpt nm DOT STAR */
177864{
177865 Expr *pRight, *pLeft, *pDot;
177866 pRight = sqlite3PExpr(pParse, TK_ASTERISK180, 0, 0);
177867 sqlite3ExprSetErrorOffset(pRight, (int)(yymsp[0].minor.yy0.z - pParse->zTail));
177868 pLeft = tokenExpr(pParse, TK_ID60, yymsp[-2].minor.yy0);
177869 pDot = sqlite3PExpr(pParse, TK_DOT142, pLeft, pRight);
177870 yymsp[-4].minor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, pDot);
177871}
177872 break;
177873 case 105: /* as ::= AS nm */
177874 case 117: /* dbnm ::= DOT nm */ yytestcase(yyruleno==117);
177875 case 258: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==258);
177876 case 259: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==259);
177877{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
177878 break;
177879 case 107: /* from ::= */
177880 case 110: /* stl_prefix ::= */ yytestcase(yyruleno==110);
177881{yymsp[1].minor.yy203 = 0;}
177882 break;
177883 case 108: /* from ::= FROM seltablist */
177884{
177885 yymsp[-1].minor.yy203 = yymsp[0].minor.yy203;
177886 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy203);
177887}
177888 break;
177889 case 109: /* stl_prefix ::= seltablist joinop */
177890{
177891 if( ALWAYS(yymsp[-1].minor.yy203 && yymsp[-1].minor.yy203->nSrc>0)(yymsp[-1].minor.yy203 && yymsp[-1].minor.yy203->nSrc
>0)
) yymsp[-1].minor.yy203->a[yymsp[-1].minor.yy203->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy144;
177892}
177893 break;
177894 case 111: /* seltablist ::= stl_prefix nm dbnm as on_using */
177895{
177896 yymsp[-4].minor.yy203 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy203,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy269);
177897}
177898 break;
177899 case 112: /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
177900{
177901 yymsp[-5].minor.yy203 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy203,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,0,&yymsp[0].minor.yy269);
177902 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy203, &yymsp[-1].minor.yy0);
177903}
177904 break;
177905 case 113: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
177906{
177907 yymsp[-7].minor.yy203 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy203,&yymsp[-6].minor.yy0,&yymsp[-5].minor.yy0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy269);
177908 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy203, yymsp[-3].minor.yy14);
177909}
177910 break;
177911 case 114: /* seltablist ::= stl_prefix LP select RP as on_using */
177912{
177913 yymsp[-5].minor.yy203 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy203,0,0,&yymsp[-1].minor.yy0,yymsp[-3].minor.yy555,&yymsp[0].minor.yy269);
177914 }
177915 break;
177916 case 115: /* seltablist ::= stl_prefix LP seltablist RP as on_using */
177917{
177918 if( yymsp[-5].minor.yy203==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy269.pOn==0 && yymsp[0].minor.yy269.pUsing==0 ){
177919 yymsp[-5].minor.yy203 = yymsp[-3].minor.yy203;
177920 }else if( ALWAYS(yymsp[-3].minor.yy203!=0)(yymsp[-3].minor.yy203!=0) && yymsp[-3].minor.yy203->nSrc==1 ){
177921 yymsp[-5].minor.yy203 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy203,0,0,&yymsp[-1].minor.yy0,0,&yymsp[0].minor.yy269);
177922 if( yymsp[-5].minor.yy203 ){
177923 SrcItem *pNew = &yymsp[-5].minor.yy203->a[yymsp[-5].minor.yy203->nSrc-1];
177924 SrcItem *pOld = yymsp[-3].minor.yy203->a;
177925 assert( pOld->fg.fixedSchema==0 )((void) (0));
177926 pNew->zName = pOld->zName;
177927 assert( pOld->fg.fixedSchema==0 )((void) (0));
177928 if( pOld->fg.isSubquery ){
177929 pNew->fg.isSubquery = 1;
177930 pNew->u4.pSubq = pOld->u4.pSubq;
177931 pOld->u4.pSubq = 0;
177932 pOld->fg.isSubquery = 0;
177933 assert( pNew->u4.pSubq!=0 && pNew->u4.pSubq->pSelect!=0 )((void) (0));
177934 if( (pNew->u4.pSubq->pSelect->selFlags & SF_NestedFrom0x0000800)!=0 ){
177935 pNew->fg.isNestedFrom = 1;
177936 }
177937 }else{
177938 pNew->u4.zDatabase = pOld->u4.zDatabase;
177939 pOld->u4.zDatabase = 0;
177940 }
177941 if( pOld->fg.isTabFunc ){
177942 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
177943 pOld->u1.pFuncArg = 0;
177944 pOld->fg.isTabFunc = 0;
177945 pNew->fg.isTabFunc = 1;
177946 }
177947 pOld->zName = 0;
177948 }
177949 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy203);
177950 }else{
177951 Select *pSubquery;
177952 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy203);
177953 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy203,0,0,0,0,SF_NestedFrom0x0000800,0);
177954 yymsp[-5].minor.yy203 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy203,0,0,&yymsp[-1].minor.yy0,pSubquery,&yymsp[0].minor.yy269);
177955 }
177956 }
177957 break;
177958 case 116: /* dbnm ::= */
177959 case 131: /* indexed_opt ::= */ yytestcase(yyruleno==131);
177960{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
177961 break;
177962 case 118: /* fullname ::= nm */
177963{
177964 yylhsminor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
177965 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy203 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy203->a[0].zName, &yymsp[0].minor.yy0);
177966}
177967 yymsp[0].minor.yy203 = yylhsminor.yy203;
177968 break;
177969 case 119: /* fullname ::= nm DOT nm */
177970{
177971 yylhsminor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
177972 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy203 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy203->a[0].zName, &yymsp[0].minor.yy0);
177973}
177974 yymsp[-2].minor.yy203 = yylhsminor.yy203;
177975 break;
177976 case 120: /* xfullname ::= nm */
177977{yymsp[0].minor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
177978 break;
177979 case 121: /* xfullname ::= nm DOT nm */
177980{yymsp[-2].minor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
177981 break;
177982 case 122: /* xfullname ::= nm DOT nm AS nm */
177983{
177984 yymsp[-4].minor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
177985 if( yymsp[-4].minor.yy203 ) yymsp[-4].minor.yy203->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
177986}
177987 break;
177988 case 123: /* xfullname ::= nm AS nm */
177989{
177990 yymsp[-2].minor.yy203 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
177991 if( yymsp[-2].minor.yy203 ) yymsp[-2].minor.yy203->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
177992}
177993 break;
177994 case 124: /* joinop ::= COMMA|JOIN */
177995{ yymsp[0].minor.yy144 = JT_INNER0x01; }
177996 break;
177997 case 125: /* joinop ::= JOIN_KW JOIN */
177998{yymsp[-1].minor.yy144 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
177999 break;
178000 case 126: /* joinop ::= JOIN_KW nm JOIN */
178001{yymsp[-2].minor.yy144 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
178002 break;
178003 case 127: /* joinop ::= JOIN_KW nm nm JOIN */
178004{yymsp[-3].minor.yy144 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
178005 break;
178006 case 128: /* on_using ::= ON expr */
178007{yymsp[-1].minor.yy269.pOn = yymsp[0].minor.yy454; yymsp[-1].minor.yy269.pUsing = 0;}
178008 break;
178009 case 129: /* on_using ::= USING LP idlist RP */
178010{yymsp[-3].minor.yy269.pOn = 0; yymsp[-3].minor.yy269.pUsing = yymsp[-1].minor.yy132;}
178011 break;
178012 case 130: /* on_using ::= */
178013{yymsp[1].minor.yy269.pOn = 0; yymsp[1].minor.yy269.pUsing = 0;}
178014 break;
178015 case 132: /* indexed_by ::= INDEXED BY nm */
178016{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
178017 break;
178018 case 133: /* indexed_by ::= NOT INDEXED */
178019{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
178020 break;
178021 case 135: /* orderby_opt ::= ORDER BY sortlist */
178022 case 145: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==145);
178023{yymsp[-2].minor.yy14 = yymsp[0].minor.yy14;}
178024 break;
178025 case 136: /* sortlist ::= sortlist COMMA expr sortorder nulls */
178026{
178027 yymsp[-4].minor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14,yymsp[-2].minor.yy454);
178028 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy14,yymsp[-1].minor.yy144,yymsp[0].minor.yy144);
178029}
178030 break;
178031 case 137: /* sortlist ::= expr sortorder nulls */
178032{
178033 yymsp[-2].minor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy454); /*A-overwrites-Y*/
178034 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy14,yymsp[-1].minor.yy144,yymsp[0].minor.yy144);
178035}
178036 break;
178037 case 138: /* sortorder ::= ASC */
178038{yymsp[0].minor.yy144 = SQLITE_SO_ASC0;}
178039 break;
178040 case 139: /* sortorder ::= DESC */
178041{yymsp[0].minor.yy144 = SQLITE_SO_DESC1;}
178042 break;
178043 case 140: /* sortorder ::= */
178044 case 143: /* nulls ::= */ yytestcase(yyruleno==143);
178045{yymsp[1].minor.yy144 = SQLITE_SO_UNDEFINED-1;}
178046 break;
178047 case 141: /* nulls ::= NULLS FIRST */
178048{yymsp[-1].minor.yy144 = SQLITE_SO_ASC0;}
178049 break;
178050 case 142: /* nulls ::= NULLS LAST */
178051{yymsp[-1].minor.yy144 = SQLITE_SO_DESC1;}
178052 break;
178053 case 146: /* having_opt ::= */
178054 case 148: /* limit_opt ::= */ yytestcase(yyruleno==148);
178055 case 153: /* where_opt ::= */ yytestcase(yyruleno==153);
178056 case 155: /* where_opt_ret ::= */ yytestcase(yyruleno==155);
178057 case 232: /* case_else ::= */ yytestcase(yyruleno==232);
178058 case 233: /* case_operand ::= */ yytestcase(yyruleno==233);
178059 case 252: /* vinto ::= */ yytestcase(yyruleno==252);
178060{yymsp[1].minor.yy454 = 0;}
178061 break;
178062 case 147: /* having_opt ::= HAVING expr */
178063 case 154: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==154);
178064 case 156: /* where_opt_ret ::= WHERE expr */ yytestcase(yyruleno==156);
178065 case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
178066 case 251: /* vinto ::= INTO expr */ yytestcase(yyruleno==251);
178067{yymsp[-1].minor.yy454 = yymsp[0].minor.yy454;}
178068 break;
178069 case 149: /* limit_opt ::= LIMIT expr */
178070{yymsp[-1].minor.yy454 = sqlite3PExpr(pParse,TK_LIMIT149,yymsp[0].minor.yy454,0);}
178071 break;
178072 case 150: /* limit_opt ::= LIMIT expr OFFSET expr */
178073{yymsp[-3].minor.yy454 = sqlite3PExpr(pParse,TK_LIMIT149,yymsp[-2].minor.yy454,yymsp[0].minor.yy454);}
178074 break;
178075 case 151: /* limit_opt ::= LIMIT expr COMMA expr */
178076{yymsp[-3].minor.yy454 = sqlite3PExpr(pParse,TK_LIMIT149,yymsp[0].minor.yy454,yymsp[-2].minor.yy454);}
178077 break;
178078 case 152: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
178079{
178080 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy203, &yymsp[-1].minor.yy0);
178081 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy203,yymsp[0].minor.yy454,0,0);
178082}
178083 break;
178084 case 157: /* where_opt_ret ::= RETURNING selcollist */
178085{sqlite3AddReturning(pParse,yymsp[0].minor.yy14); yymsp[-1].minor.yy454 = 0;}
178086 break;
178087 case 158: /* where_opt_ret ::= WHERE expr RETURNING selcollist */
178088{sqlite3AddReturning(pParse,yymsp[0].minor.yy14); yymsp[-3].minor.yy454 = yymsp[-2].minor.yy454;}
178089 break;
178090 case 159: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
178091{
178092 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy203, &yymsp[-4].minor.yy0);
178093 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy14,"set list");
178094 if( yymsp[-1].minor.yy203 ){
178095 SrcList *pFromClause = yymsp[-1].minor.yy203;
178096 if( pFromClause->nSrc>1 ){
178097 Select *pSubquery;
178098 Token as;
178099 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom0x0000800,0);
178100 as.n = 0;
178101 as.z = 0;
178102 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
178103 }
178104 yymsp[-5].minor.yy203 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy203, pFromClause);
178105 }
178106 sqlite3Update(pParse,yymsp[-5].minor.yy203,yymsp[-2].minor.yy14,yymsp[0].minor.yy454,yymsp[-6].minor.yy144,0,0,0);
178107}
178108 break;
178109 case 160: /* setlist ::= setlist COMMA nm EQ expr */
178110{
178111 yymsp[-4].minor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy454);
178112 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy14, &yymsp[-2].minor.yy0, 1);
178113}
178114 break;
178115 case 161: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
178116{
178117 yymsp[-6].minor.yy14 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy14, yymsp[-3].minor.yy132, yymsp[0].minor.yy454);
178118}
178119 break;
178120 case 162: /* setlist ::= nm EQ expr */
178121{
178122 yylhsminor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy454);
178123 sqlite3ExprListSetName(pParse, yylhsminor.yy14, &yymsp[-2].minor.yy0, 1);
178124}
178125 yymsp[-2].minor.yy14 = yylhsminor.yy14;
178126 break;
178127 case 163: /* setlist ::= LP idlist RP EQ expr */
178128{
178129 yymsp[-4].minor.yy14 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy132, yymsp[0].minor.yy454);
178130}
178131 break;
178132 case 164: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
178133{
178134 sqlite3Insert(pParse, yymsp[-3].minor.yy203, yymsp[-1].minor.yy555, yymsp[-2].minor.yy132, yymsp[-5].minor.yy144, yymsp[0].minor.yy122);
178135}
178136 break;
178137 case 165: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
178138{
178139 sqlite3Insert(pParse, yymsp[-4].minor.yy203, 0, yymsp[-3].minor.yy132, yymsp[-6].minor.yy144, 0);
178140}
178141 break;
178142 case 166: /* upsert ::= */
178143{ yymsp[1].minor.yy122 = 0; }
178144 break;
178145 case 167: /* upsert ::= RETURNING selcollist */
178146{ yymsp[-1].minor.yy122 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy14); }
178147 break;
178148 case 168: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
178149{ yymsp[-11].minor.yy122 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy14,yymsp[-6].minor.yy454,yymsp[-2].minor.yy14,yymsp[-1].minor.yy454,yymsp[0].minor.yy122);}
178150 break;
178151 case 169: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
178152{ yymsp[-8].minor.yy122 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy14,yymsp[-3].minor.yy454,0,0,yymsp[0].minor.yy122); }
178153 break;
178154 case 170: /* upsert ::= ON CONFLICT DO NOTHING returning */
178155{ yymsp[-4].minor.yy122 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
178156 break;
178157 case 171: /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
178158{ yymsp[-7].minor.yy122 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy14,yymsp[-1].minor.yy454,0);}
178159 break;
178160 case 172: /* returning ::= RETURNING selcollist */
178161{sqlite3AddReturning(pParse,yymsp[0].minor.yy14);}
178162 break;
178163 case 175: /* idlist_opt ::= */
178164{yymsp[1].minor.yy132 = 0;}
178165 break;
178166 case 176: /* idlist_opt ::= LP idlist RP */
178167{yymsp[-2].minor.yy132 = yymsp[-1].minor.yy132;}
178168 break;
178169 case 177: /* idlist ::= idlist COMMA nm */
178170{yymsp[-2].minor.yy132 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy132,&yymsp[0].minor.yy0);}
178171 break;
178172 case 178: /* idlist ::= nm */
178173{yymsp[0].minor.yy132 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
178174 break;
178175 case 179: /* expr ::= LP expr RP */
178176{yymsp[-2].minor.yy454 = yymsp[-1].minor.yy454;}
178177 break;
178178 case 180: /* expr ::= ID|INDEXED|JOIN_KW */
178179{yymsp[0].minor.yy454=tokenExpr(pParse,TK_ID60,yymsp[0].minor.yy0); /*A-overwrites-X*/}
178180 break;
178181 case 181: /* expr ::= nm DOT nm */
178182{
178183 Expr *temp1 = tokenExpr(pParse,TK_ID60,yymsp[-2].minor.yy0);
178184 Expr *temp2 = tokenExpr(pParse,TK_ID60,yymsp[0].minor.yy0);
178185 yylhsminor.yy454 = sqlite3PExpr(pParse, TK_DOT142, temp1, temp2);
178186}
178187 yymsp[-2].minor.yy454 = yylhsminor.yy454;
178188 break;
178189 case 182: /* expr ::= nm DOT nm DOT nm */
178190{
178191 Expr *temp1 = tokenExpr(pParse,TK_ID60,yymsp[-4].minor.yy0);
178192 Expr *temp2 = tokenExpr(pParse,TK_ID60,yymsp[-2].minor.yy0);
178193 Expr *temp3 = tokenExpr(pParse,TK_ID60,yymsp[0].minor.yy0);
178194 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT142, temp2, temp3);
178195 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
178196 sqlite3RenameTokenRemap(pParse, 0, temp1);
178197 }
178198 yylhsminor.yy454 = sqlite3PExpr(pParse, TK_DOT142, temp1, temp4);
178199}
178200 yymsp[-4].minor.yy454 = yylhsminor.yy454;
178201 break;
178202 case 183: /* term ::= NULL|FLOAT|BLOB */
178203 case 184: /* term ::= STRING */ yytestcase(yyruleno==184);
178204{yymsp[0].minor.yy454=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
178205 break;
178206 case 185: /* term ::= INTEGER */
178207{
178208 yylhsminor.yy454 = sqlite3ExprAlloc(pParse->db, TK_INTEGER156, &yymsp[0].minor.yy0, 1);
178209 if( yylhsminor.yy454 ) yylhsminor.yy454->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
178210}
178211 yymsp[0].minor.yy454 = yylhsminor.yy454;
178212 break;
178213 case 186: /* expr ::= VARIABLE */
178214{
178215 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])(sqlite3CtypeMap[(unsigned char)(yymsp[0].minor.yy0.z[1])]&
0x04)
) ){
178216 u32 n = yymsp[0].minor.yy0.n;
178217 yymsp[0].minor.yy454 = tokenExpr(pParse, TK_VARIABLE157, yymsp[0].minor.yy0);
178218 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy454, n);
178219 }else{
178220 /* When doing a nested parse, one can include terms in an expression
178221 ** that look like this: #1 #2 ... These terms refer to registers
178222 ** in the virtual machine. #N is the N-th register. */
178223 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
178224 assert( t.n>=2 )((void) (0));
178225 if( pParse->nested==0 ){
178226 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
178227 yymsp[0].minor.yy454 = 0;
178228 }else{
178229 yymsp[0].minor.yy454 = sqlite3PExpr(pParse, TK_REGISTER176, 0, 0);
178230 if( yymsp[0].minor.yy454 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy454->iTable);
178231 }
178232 }
178233}
178234 break;
178235 case 187: /* expr ::= expr COLLATE ID|STRING */
178236{
178237 yymsp[-2].minor.yy454 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy454, &yymsp[0].minor.yy0, 1);
178238}
178239 break;
178240 case 188: /* expr ::= CAST LP expr AS typetoken RP */
178241{
178242 yymsp[-5].minor.yy454 = sqlite3ExprAlloc(pParse->db, TK_CAST36, &yymsp[-1].minor.yy0, 1);
178243 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy454, yymsp[-3].minor.yy454, 0);
178244}
178245 break;
178246 case 189: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
178247{
178248 yylhsminor.yy454 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy144);
178249}
178250 yymsp[-4].minor.yy454 = yylhsminor.yy454;
178251 break;
178252 case 190: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
178253{
178254 yylhsminor.yy454 = sqlite3ExprFunction(pParse, yymsp[-4].minor.yy14, &yymsp[-7].minor.yy0, yymsp[-5].minor.yy144);
178255 sqlite3ExprAddFunctionOrderBy(pParse, yylhsminor.yy454, yymsp[-1].minor.yy14);
178256}
178257 yymsp[-7].minor.yy454 = yylhsminor.yy454;
178258 break;
178259 case 191: /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
178260{
178261 yylhsminor.yy454 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
178262}
178263 yymsp[-3].minor.yy454 = yylhsminor.yy454;
178264 break;
178265 case 192: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
178266{
178267 yylhsminor.yy454 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy14, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy144);
178268 sqlite3WindowAttach(pParse, yylhsminor.yy454, yymsp[0].minor.yy211);
178269}
178270 yymsp[-5].minor.yy454 = yylhsminor.yy454;
178271 break;
178272 case 193: /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
178273{
178274 yylhsminor.yy454 = sqlite3ExprFunction(pParse, yymsp[-5].minor.yy14, &yymsp[-8].minor.yy0, yymsp[-6].minor.yy144);
178275 sqlite3WindowAttach(pParse, yylhsminor.yy454, yymsp[0].minor.yy211);
178276 sqlite3ExprAddFunctionOrderBy(pParse, yylhsminor.yy454, yymsp[-2].minor.yy14);
178277}
178278 yymsp[-8].minor.yy454 = yylhsminor.yy454;
178279 break;
178280 case 194: /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
178281{
178282 yylhsminor.yy454 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
178283 sqlite3WindowAttach(pParse, yylhsminor.yy454, yymsp[0].minor.yy211);
178284}
178285 yymsp[-4].minor.yy454 = yylhsminor.yy454;
178286 break;
178287 case 195: /* term ::= CTIME_KW */
178288{
178289 yylhsminor.yy454 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
178290}
178291 yymsp[0].minor.yy454 = yylhsminor.yy454;
178292 break;
178293 case 196: /* expr ::= LP nexprlist COMMA expr RP */
178294{
178295 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy14, yymsp[-1].minor.yy454);
178296 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_VECTOR177, 0, 0);
178297 if( yymsp[-4].minor.yy454 ){
178298 yymsp[-4].minor.yy454->x.pList = pList;
178299 if( ALWAYS(pList->nExpr)(pList->nExpr) ){
178300 yymsp[-4].minor.yy454->flags |= pList->a[0].pExpr->flags & EP_Propagate(0x000200|0x400000|0x000008);
178301 }
178302 }else{
178303 sqlite3ExprListDelete(pParse->db, pList);
178304 }
178305}
178306 break;
178307 case 197: /* expr ::= expr AND expr */
178308{yymsp[-2].minor.yy454=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy454,yymsp[0].minor.yy454);}
178309 break;
178310 case 198: /* expr ::= expr OR expr */
178311 case 199: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==199);
178312 case 200: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==200);
178313 case 201: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==201);
178314 case 202: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==202);
178315 case 203: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==203);
178316 case 204: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==204);
178317{yymsp[-2].minor.yy454=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy454,yymsp[0].minor.yy454);}
178318 break;
178319 case 205: /* likeop ::= NOT LIKE_KW|MATCH */
178320{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
178321 break;
178322 case 206: /* expr ::= expr likeop expr */
178323{
178324 ExprList *pList;
178325 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
178326 yymsp[-1].minor.yy0.n &= 0x7fffffff;
178327 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy454);
178328 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy454);
178329 yymsp[-2].minor.yy454 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
178330 if( bNot ) yymsp[-2].minor.yy454 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-2].minor.yy454, 0);
178331 if( yymsp[-2].minor.yy454 ) yymsp[-2].minor.yy454->flags |= EP_InfixFunc0x000100;
178332}
178333 break;
178334 case 207: /* expr ::= expr likeop expr ESCAPE expr */
178335{
178336 ExprList *pList;
178337 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
178338 yymsp[-3].minor.yy0.n &= 0x7fffffff;
178339 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy454);
178340 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy454);
178341 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy454);
178342 yymsp[-4].minor.yy454 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
178343 if( bNot ) yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy454, 0);
178344 if( yymsp[-4].minor.yy454 ) yymsp[-4].minor.yy454->flags |= EP_InfixFunc0x000100;
178345}
178346 break;
178347 case 208: /* expr ::= expr ISNULL|NOTNULL */
178348{yymsp[-1].minor.yy454 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy454,0);}
178349 break;
178350 case 209: /* expr ::= expr NOT NULL */
178351{yymsp[-2].minor.yy454 = sqlite3PExpr(pParse,TK_NOTNULL52,yymsp[-2].minor.yy454,0);}
178352 break;
178353 case 210: /* expr ::= expr IS expr */
178354{
178355 yymsp[-2].minor.yy454 = sqlite3PExpr(pParse,TK_IS45,yymsp[-2].minor.yy454,yymsp[0].minor.yy454);
178356 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy454, yymsp[-2].minor.yy454, TK_ISNULL51);
178357}
178358 break;
178359 case 211: /* expr ::= expr IS NOT expr */
178360{
178361 yymsp[-3].minor.yy454 = sqlite3PExpr(pParse,TK_ISNOT46,yymsp[-3].minor.yy454,yymsp[0].minor.yy454);
178362 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy454, yymsp[-3].minor.yy454, TK_NOTNULL52);
178363}
178364 break;
178365 case 212: /* expr ::= expr IS NOT DISTINCT FROM expr */
178366{
178367 yymsp[-5].minor.yy454 = sqlite3PExpr(pParse,TK_IS45,yymsp[-5].minor.yy454,yymsp[0].minor.yy454);
178368 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy454, yymsp[-5].minor.yy454, TK_ISNULL51);
178369}
178370 break;
178371 case 213: /* expr ::= expr IS DISTINCT FROM expr */
178372{
178373 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse,TK_ISNOT46,yymsp[-4].minor.yy454,yymsp[0].minor.yy454);
178374 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy454, yymsp[-4].minor.yy454, TK_NOTNULL52);
178375}
178376 break;
178377 case 214: /* expr ::= NOT expr */
178378 case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
178379{yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy454, 0);/*A-overwrites-B*/}
178380 break;
178381 case 216: /* expr ::= PLUS|MINUS expr */
178382{
178383 Expr *p = yymsp[0].minor.yy454;
178384 u8 op = yymsp[-1].major + (TK_UPLUS173-TK_PLUS107);
178385 assert( TK_UPLUS>TK_PLUS )((void) (0));
178386 assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) )((void) (0));
178387 if( p && p->op==TK_UPLUS173 ){
178388 p->op = op;
178389 yymsp[-1].minor.yy454 = p;
178390 }else{
178391 yymsp[-1].minor.yy454 = sqlite3PExpr(pParse, op, p, 0);
178392 /*A-overwrites-B*/
178393 }
178394}
178395 break;
178396 case 217: /* expr ::= expr PTR expr */
178397{
178398 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy454);
178399 pList = sqlite3ExprListAppend(pParse, pList, yymsp[0].minor.yy454);
178400 yylhsminor.yy454 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
178401}
178402 yymsp[-2].minor.yy454 = yylhsminor.yy454;
178403 break;
178404 case 218: /* between_op ::= BETWEEN */
178405 case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
178406{yymsp[0].minor.yy144 = 0;}
178407 break;
178408 case 220: /* expr ::= expr between_op expr AND expr */
178409{
178410 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy454);
178411 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy454);
178412 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_BETWEEN49, yymsp[-4].minor.yy454, 0);
178413 if( yymsp[-4].minor.yy454 ){
178414 yymsp[-4].minor.yy454->x.pList = pList;
178415 }else{
178416 sqlite3ExprListDelete(pParse->db, pList);
178417 }
178418 if( yymsp[-3].minor.yy144 ) yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy454, 0);
178419}
178420 break;
178421 case 223: /* expr ::= expr in_op LP exprlist RP */
178422{
178423 if( yymsp[-1].minor.yy14==0 ){
178424 /* Expressions of the form
178425 **
178426 ** expr1 IN ()
178427 ** expr1 NOT IN ()
178428 **
178429 ** simplify to constants 0 (false) and 1 (true), respectively,
178430 ** regardless of the value of expr1.
178431 */
178432 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy454);
178433 yymsp[-4].minor.yy454 = sqlite3Expr(pParse->db, TK_STRING118, yymsp[-3].minor.yy144 ? "true" : "false");
178434 if( yymsp[-4].minor.yy454 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy454);
178435 }else{
178436 Expr *pRHS = yymsp[-1].minor.yy14->a[0].pExpr;
178437 if( yymsp[-1].minor.yy14->nExpr==1 && sqlite3ExprIsConstant(pParse,pRHS) && yymsp[-4].minor.yy454->op!=TK_VECTOR177 ){
178438 yymsp[-1].minor.yy14->a[0].pExpr = 0;
178439 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
178440 pRHS = sqlite3PExpr(pParse, TK_UPLUS173, pRHS, 0);
178441 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_EQ54, yymsp[-4].minor.yy454, pRHS);
178442 }else if( yymsp[-1].minor.yy14->nExpr==1 && pRHS->op==TK_SELECT139 ){
178443 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy454, 0);
178444 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy454, pRHS->x.pSelect);
178445 pRHS->x.pSelect = 0;
178446 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
178447 }else{
178448 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy454, 0);
178449 if( yymsp[-4].minor.yy454==0 ){
178450 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
178451 }else if( yymsp[-4].minor.yy454->pLeft->op==TK_VECTOR177 ){
178452 int nExpr = yymsp[-4].minor.yy454->pLeft->x.pList->nExpr;
178453 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy14);
178454 if( pSelectRHS ){
178455 parserDoubleLinkSelect(pParse, pSelectRHS);
178456 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy454, pSelectRHS);
178457 }
178458 }else{
178459 yymsp[-4].minor.yy454->x.pList = yymsp[-1].minor.yy14;
178460 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy454);
178461 }
178462 }
178463 if( yymsp[-3].minor.yy144 ) yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy454, 0);
178464 }
178465 }
178466 break;
178467 case 224: /* expr ::= LP select RP */
178468{
178469 yymsp[-2].minor.yy454 = sqlite3PExpr(pParse, TK_SELECT139, 0, 0);
178470 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy454, yymsp[-1].minor.yy555);
178471 }
178472 break;
178473 case 225: /* expr ::= expr in_op LP select RP */
178474{
178475 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy454, 0);
178476 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy454, yymsp[-1].minor.yy555);
178477 if( yymsp[-3].minor.yy144 ) yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy454, 0);
178478 }
178479 break;
178480 case 226: /* expr ::= expr in_op nm dbnm paren_exprlist */
178481{
178482 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
178483 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
178484 if( yymsp[0].minor.yy14 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy14);
178485 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_IN50, yymsp[-4].minor.yy454, 0);
178486 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy454, pSelect);
178487 if( yymsp[-3].minor.yy144 ) yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy454, 0);
178488 }
178489 break;
178490 case 227: /* expr ::= EXISTS LP select RP */
178491{
178492 Expr *p;
178493 p = yymsp[-3].minor.yy454 = sqlite3PExpr(pParse, TK_EXISTS20, 0, 0);
178494 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy555);
178495 }
178496 break;
178497 case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
178498{
178499 yymsp[-4].minor.yy454 = sqlite3PExpr(pParse, TK_CASE158, yymsp[-3].minor.yy454, 0);
178500 if( yymsp[-4].minor.yy454 ){
178501 yymsp[-4].minor.yy454->x.pList = yymsp[-1].minor.yy454 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[-1].minor.yy454) : yymsp[-2].minor.yy14;
178502 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy454);
178503 }else{
178504 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
178505 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy454);
178506 }
178507}
178508 break;
178509 case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
178510{
178511 yymsp[-4].minor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy454);
178512 yymsp[-4].minor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[0].minor.yy454);
178513}
178514 break;
178515 case 230: /* case_exprlist ::= WHEN expr THEN expr */
178516{
178517 yymsp[-3].minor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy454);
178518 yymsp[-3].minor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, yymsp[0].minor.yy454);
178519}
178520 break;
178521 case 235: /* nexprlist ::= nexprlist COMMA expr */
178522{yymsp[-2].minor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy454);}
178523 break;
178524 case 236: /* nexprlist ::= expr */
178525{yymsp[0].minor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy454); /*A-overwrites-Y*/}
178526 break;
178527 case 238: /* paren_exprlist ::= LP exprlist RP */
178528 case 243: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==243);
178529{yymsp[-2].minor.yy14 = yymsp[-1].minor.yy14;}
178530 break;
178531 case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
178532{
178533 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
178534 sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy14, yymsp[-10].minor.yy144,
178535 &yymsp[-11].minor.yy0, yymsp[0].minor.yy454, SQLITE_SO_ASC0, yymsp[-8].minor.yy144, SQLITE_IDXTYPE_APPDEF0);
178536 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pParse->pNewIndex ){
178537 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
178538 }
178539}
178540 break;
178541 case 240: /* uniqueflag ::= UNIQUE */
178542 case 282: /* raisetype ::= ABORT */ yytestcase(yyruleno==282);
178543{yymsp[0].minor.yy144 = OE_Abort2;}
178544 break;
178545 case 241: /* uniqueflag ::= */
178546{yymsp[1].minor.yy144 = OE_None0;}
178547 break;
178548 case 244: /* eidlist ::= eidlist COMMA nm collate sortorder */
178549{
178550 yymsp[-4].minor.yy14 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy14, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy144, yymsp[0].minor.yy144);
178551}
178552 break;
178553 case 245: /* eidlist ::= nm collate sortorder */
178554{
178555 yymsp[-2].minor.yy14 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy144, yymsp[0].minor.yy144); /*A-overwrites-Y*/
178556}
178557 break;
178558 case 248: /* cmd ::= DROP INDEX ifexists fullname */
178559{sqlite3DropIndex(pParse, yymsp[0].minor.yy203, yymsp[-1].minor.yy144);}
178560 break;
178561 case 249: /* cmd ::= VACUUM vinto */
178562{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy454);}
178563 break;
178564 case 250: /* cmd ::= VACUUM nm vinto */
178565{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy454);}
178566 break;
178567 case 253: /* cmd ::= PRAGMA nm dbnm */
178568{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
178569 break;
178570 case 254: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
178571{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
178572 break;
178573 case 255: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
178574{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
178575 break;
178576 case 256: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
178577{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
178578 break;
178579 case 257: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
178580{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
178581 break;
178582 case 260: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
178583{
178584 Token all;
178585 all.z = yymsp[-3].minor.yy0.z;
178586 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
178587 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy427, &all);
178588}
178589 break;
178590 case 261: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
178591{
178592 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy144, yymsp[-4].minor.yy286.a, yymsp[-4].minor.yy286.b, yymsp[-2].minor.yy203, yymsp[0].minor.yy454, yymsp[-10].minor.yy144, yymsp[-8].minor.yy144);
178593 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
178594}
178595 break;
178596 case 262: /* trigger_time ::= BEFORE|AFTER */
178597{ yymsp[0].minor.yy144 = yymsp[0].major; /*A-overwrites-X*/ }
178598 break;
178599 case 263: /* trigger_time ::= INSTEAD OF */
178600{ yymsp[-1].minor.yy144 = TK_INSTEAD66;}
178601 break;
178602 case 264: /* trigger_time ::= */
178603{ yymsp[1].minor.yy144 = TK_BEFORE33; }
178604 break;
178605 case 265: /* trigger_event ::= DELETE|INSERT */
178606 case 266: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==266);
178607{yymsp[0].minor.yy286.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy286.b = 0;}
178608 break;
178609 case 267: /* trigger_event ::= UPDATE OF idlist */
178610{yymsp[-2].minor.yy286.a = TK_UPDATE130; yymsp[-2].minor.yy286.b = yymsp[0].minor.yy132;}
178611 break;
178612 case 268: /* when_clause ::= */
178613 case 287: /* key_opt ::= */ yytestcase(yyruleno==287);
178614{ yymsp[1].minor.yy454 = 0; }
178615 break;
178616 case 269: /* when_clause ::= WHEN expr */
178617 case 288: /* key_opt ::= KEY expr */ yytestcase(yyruleno==288);
178618{ yymsp[-1].minor.yy454 = yymsp[0].minor.yy454; }
178619 break;
178620 case 270: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
178621{
178622 assert( yymsp[-2].minor.yy427!=0 )((void) (0));
178623 yymsp[-2].minor.yy427->pLast->pNext = yymsp[-1].minor.yy427;
178624 yymsp[-2].minor.yy427->pLast = yymsp[-1].minor.yy427;
178625}
178626 break;
178627 case 271: /* trigger_cmd_list ::= trigger_cmd SEMI */
178628{
178629 assert( yymsp[-1].minor.yy427!=0 )((void) (0));
178630 yymsp[-1].minor.yy427->pLast = yymsp[-1].minor.yy427;
178631}
178632 break;
178633 case 272: /* trnm ::= nm DOT nm */
178634{
178635 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
178636 sqlite3ErrorMsg(pParse,
178637 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
178638 "statements within triggers");
178639}
178640 break;
178641 case 273: /* tridxby ::= INDEXED BY nm */
178642{
178643 sqlite3ErrorMsg(pParse,
178644 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
178645 "within triggers");
178646}
178647 break;
178648 case 274: /* tridxby ::= NOT INDEXED */
178649{
178650 sqlite3ErrorMsg(pParse,
178651 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
178652 "within triggers");
178653}
178654 break;
178655 case 275: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
178656{yylhsminor.yy427 = sqlite3TriggerUpdateStep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy203, yymsp[-3].minor.yy14, yymsp[-1].minor.yy454, yymsp[-7].minor.yy144, yymsp[-8].minor.yy0.z, yymsp[0].minor.yy168);}
178657 yymsp[-8].minor.yy427 = yylhsminor.yy427;
178658 break;
178659 case 276: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
178660{
178661 yylhsminor.yy427 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy132,yymsp[-2].minor.yy555,yymsp[-6].minor.yy144,yymsp[-1].minor.yy122,yymsp[-7].minor.yy168,yymsp[0].minor.yy168);/*yylhsminor.yy427-overwrites-yymsp[-6].minor.yy144*/
178662}
178663 yymsp[-7].minor.yy427 = yylhsminor.yy427;
178664 break;
178665 case 277: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
178666{yylhsminor.yy427 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy454, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy168);}
178667 yymsp[-5].minor.yy427 = yylhsminor.yy427;
178668 break;
178669 case 278: /* trigger_cmd ::= scanpt select scanpt */
178670{yylhsminor.yy427 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy555, yymsp[-2].minor.yy168, yymsp[0].minor.yy168); /*yylhsminor.yy427-overwrites-yymsp[-1].minor.yy555*/}
178671 yymsp[-2].minor.yy427 = yylhsminor.yy427;
178672 break;
178673 case 279: /* expr ::= RAISE LP IGNORE RP */
178674{
178675 yymsp[-3].minor.yy454 = sqlite3PExpr(pParse, TK_RAISE72, 0, 0);
178676 if( yymsp[-3].minor.yy454 ){
178677 yymsp[-3].minor.yy454->affExpr = OE_Ignore4;
178678 }
178679}
178680 break;
178681 case 280: /* expr ::= RAISE LP raisetype COMMA expr RP */
178682{
178683 yymsp[-5].minor.yy454 = sqlite3PExpr(pParse, TK_RAISE72, yymsp[-1].minor.yy454, 0);
178684 if( yymsp[-5].minor.yy454 ) {
178685 yymsp[-5].minor.yy454->affExpr = (char)yymsp[-3].minor.yy144;
178686 }
178687}
178688 break;
178689 case 281: /* raisetype ::= ROLLBACK */
178690{yymsp[0].minor.yy144 = OE_Rollback1;}
178691 break;
178692 case 283: /* raisetype ::= FAIL */
178693{yymsp[0].minor.yy144 = OE_Fail3;}
178694 break;
178695 case 284: /* cmd ::= DROP TRIGGER ifexists fullname */
178696{
178697 sqlite3DropTrigger(pParse,yymsp[0].minor.yy203,yymsp[-1].minor.yy144);
178698}
178699 break;
178700 case 285: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
178701{
178702 sqlite3Attach(pParse, yymsp[-3].minor.yy454, yymsp[-1].minor.yy454, yymsp[0].minor.yy454);
178703}
178704 break;
178705 case 286: /* cmd ::= DETACH database_kw_opt expr */
178706{
178707 sqlite3Detach(pParse, yymsp[0].minor.yy454);
178708}
178709 break;
178710 case 289: /* cmd ::= REINDEX */
178711{sqlite3Reindex(pParse, 0, 0);}
178712 break;
178713 case 290: /* cmd ::= REINDEX nm dbnm */
178714{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
178715 break;
178716 case 291: /* cmd ::= ANALYZE */
178717{sqlite3Analyze(pParse, 0, 0);}
178718 break;
178719 case 292: /* cmd ::= ANALYZE nm dbnm */
178720{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
178721 break;
178722 case 293: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
178723{
178724 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy203,&yymsp[0].minor.yy0);
178725}
178726 break;
178727 case 294: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
178728{
178729 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
178730 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
178731}
178732 break;
178733 case 295: /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
178734{
178735 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy203, &yymsp[0].minor.yy0);
178736}
178737 break;
178738 case 296: /* add_column_fullname ::= fullname */
178739{
178740 disableLookaside(pParse);
178741 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy203);
178742}
178743 break;
178744 case 297: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
178745{
178746 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy203, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
178747}
178748 break;
178749 case 298: /* cmd ::= create_vtab */
178750{sqlite3VtabFinishParse(pParse,0);}
178751 break;
178752 case 299: /* cmd ::= create_vtab LP vtabarglist RP */
178753{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
178754 break;
178755 case 300: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
178756{
178757 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy144);
178758}
178759 break;
178760 case 301: /* vtabarg ::= */
178761{sqlite3VtabArgInit(pParse);}
178762 break;
178763 case 302: /* vtabargtoken ::= ANY */
178764 case 303: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==303);
178765 case 304: /* lp ::= LP */ yytestcase(yyruleno==304);
178766{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
178767 break;
178768 case 305: /* with ::= WITH wqlist */
178769 case 306: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==306);
178770{ sqlite3WithPush(pParse, yymsp[0].minor.yy59, 1); }
178771 break;
178772 case 307: /* wqas ::= AS */
178773{yymsp[0].minor.yy462 = M10d_Any1;}
178774 break;
178775 case 308: /* wqas ::= AS MATERIALIZED */
178776{yymsp[-1].minor.yy462 = M10d_Yes0;}
178777 break;
178778 case 309: /* wqas ::= AS NOT MATERIALIZED */
178779{yymsp[-2].minor.yy462 = M10d_No2;}
178780 break;
178781 case 310: /* wqitem ::= withnm eidlist_opt wqas LP select RP */
178782{
178783 yymsp[-5].minor.yy67 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy555, yymsp[-3].minor.yy462); /*A-overwrites-X*/
178784}
178785 break;
178786 case 311: /* withnm ::= nm */
178787{pParse->bHasWith = 1;}
178788 break;
178789 case 312: /* wqlist ::= wqitem */
178790{
178791 yymsp[0].minor.yy59 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy67); /*A-overwrites-X*/
178792}
178793 break;
178794 case 313: /* wqlist ::= wqlist COMMA wqitem */
178795{
178796 yymsp[-2].minor.yy59 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy59, yymsp[0].minor.yy67);
178797}
178798 break;
178799 case 314: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
178800{
178801 assert( yymsp[0].minor.yy211!=0 )((void) (0));
178802 sqlite3WindowChain(pParse, yymsp[0].minor.yy211, yymsp[-2].minor.yy211);
178803 yymsp[0].minor.yy211->pNextWin = yymsp[-2].minor.yy211;
178804 yylhsminor.yy211 = yymsp[0].minor.yy211;
178805}
178806 yymsp[-2].minor.yy211 = yylhsminor.yy211;
178807 break;
178808 case 315: /* windowdefn ::= nm AS LP window RP */
178809{
178810 if( ALWAYS(yymsp[-1].minor.yy211)(yymsp[-1].minor.yy211) ){
178811 yymsp[-1].minor.yy211->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
178812 }
178813 yylhsminor.yy211 = yymsp[-1].minor.yy211;
178814}
178815 yymsp[-4].minor.yy211 = yylhsminor.yy211;
178816 break;
178817 case 316: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
178818{
178819 yymsp[-4].minor.yy211 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy211, yymsp[-2].minor.yy14, yymsp[-1].minor.yy14, 0);
178820}
178821 break;
178822 case 317: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
178823{
178824 yylhsminor.yy211 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy211, yymsp[-2].minor.yy14, yymsp[-1].minor.yy14, &yymsp[-5].minor.yy0);
178825}
178826 yymsp[-5].minor.yy211 = yylhsminor.yy211;
178827 break;
178828 case 318: /* window ::= ORDER BY sortlist frame_opt */
178829{
178830 yymsp[-3].minor.yy211 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy211, 0, yymsp[-1].minor.yy14, 0);
178831}
178832 break;
178833 case 319: /* window ::= nm ORDER BY sortlist frame_opt */
178834{
178835 yylhsminor.yy211 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy211, 0, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
178836}
178837 yymsp[-4].minor.yy211 = yylhsminor.yy211;
178838 break;
178839 case 320: /* window ::= nm frame_opt */
178840{
178841 yylhsminor.yy211 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy211, 0, 0, &yymsp[-1].minor.yy0);
178842}
178843 yymsp[-1].minor.yy211 = yylhsminor.yy211;
178844 break;
178845 case 321: /* frame_opt ::= */
178846{
178847 yymsp[1].minor.yy211 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED91, 0, TK_CURRENT86, 0, 0);
178848}
178849 break;
178850 case 322: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
178851{
178852 yylhsminor.yy211 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy144, yymsp[-1].minor.yy509.eType, yymsp[-1].minor.yy509.pExpr, TK_CURRENT86, 0, yymsp[0].minor.yy462);
178853}
178854 yymsp[-2].minor.yy211 = yylhsminor.yy211;
178855 break;
178856 case 323: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
178857{
178858 yylhsminor.yy211 = sqlite3WindowAlloc(pParse, yymsp[-5].minor.yy144, yymsp[-3].minor.yy509.eType, yymsp[-3].minor.yy509.pExpr, yymsp[-1].minor.yy509.eType, yymsp[-1].minor.yy509.pExpr, yymsp[0].minor.yy462);
178859}
178860 yymsp[-5].minor.yy211 = yylhsminor.yy211;
178861 break;
178862 case 325: /* frame_bound_s ::= frame_bound */
178863 case 327: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==327);
178864{yylhsminor.yy509 = yymsp[0].minor.yy509;}
178865 yymsp[0].minor.yy509 = yylhsminor.yy509;
178866 break;
178867 case 326: /* frame_bound_s ::= UNBOUNDED PRECEDING */
178868 case 328: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==328);
178869 case 330: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==330);
178870{yylhsminor.yy509.eType = yymsp[-1].major; yylhsminor.yy509.pExpr = 0;}
178871 yymsp[-1].minor.yy509 = yylhsminor.yy509;
178872 break;
178873 case 329: /* frame_bound ::= expr PRECEDING|FOLLOWING */
178874{yylhsminor.yy509.eType = yymsp[0].major; yylhsminor.yy509.pExpr = yymsp[-1].minor.yy454;}
178875 yymsp[-1].minor.yy509 = yylhsminor.yy509;
178876 break;
178877 case 331: /* frame_exclude_opt ::= */
178878{yymsp[1].minor.yy462 = 0;}
178879 break;
178880 case 332: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
178881{yymsp[-1].minor.yy462 = yymsp[0].minor.yy462;}
178882 break;
178883 case 333: /* frame_exclude ::= NO OTHERS */
178884 case 334: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==334);
178885{yymsp[-1].minor.yy462 = yymsp[-1].major; /*A-overwrites-X*/}
178886 break;
178887 case 335: /* frame_exclude ::= GROUP|TIES */
178888{yymsp[0].minor.yy462 = yymsp[0].major; /*A-overwrites-X*/}
178889 break;
178890 case 336: /* window_clause ::= WINDOW windowdefn_list */
178891{ yymsp[-1].minor.yy211 = yymsp[0].minor.yy211; }
178892 break;
178893 case 337: /* filter_over ::= filter_clause over_clause */
178894{
178895 if( yymsp[0].minor.yy211 ){
178896 yymsp[0].minor.yy211->pFilter = yymsp[-1].minor.yy454;
178897 }else{
178898 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy454);
178899 }
178900 yylhsminor.yy211 = yymsp[0].minor.yy211;
178901}
178902 yymsp[-1].minor.yy211 = yylhsminor.yy211;
178903 break;
178904 case 338: /* filter_over ::= over_clause */
178905{
178906 yylhsminor.yy211 = yymsp[0].minor.yy211;
178907}
178908 yymsp[0].minor.yy211 = yylhsminor.yy211;
178909 break;
178910 case 339: /* filter_over ::= filter_clause */
178911{
178912 yylhsminor.yy211 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
178913 if( yylhsminor.yy211 ){
178914 yylhsminor.yy211->eFrmType = TK_FILTER167;
178915 yylhsminor.yy211->pFilter = yymsp[0].minor.yy454;
178916 }else{
178917 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy454);
178918 }
178919}
178920 yymsp[0].minor.yy211 = yylhsminor.yy211;
178921 break;
178922 case 340: /* over_clause ::= OVER LP window RP */
178923{
178924 yymsp[-3].minor.yy211 = yymsp[-1].minor.yy211;
178925 assert( yymsp[-3].minor.yy211!=0 )((void) (0));
178926}
178927 break;
178928 case 341: /* over_clause ::= OVER nm */
178929{
178930 yymsp[-1].minor.yy211 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
178931 if( yymsp[-1].minor.yy211 ){
178932 yymsp[-1].minor.yy211->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
178933 }
178934}
178935 break;
178936 case 342: /* filter_clause ::= FILTER LP WHERE expr RP */
178937{ yymsp[-4].minor.yy454 = yymsp[-1].minor.yy454; }
178938 break;
178939 case 343: /* term ::= QNUMBER */
178940{
178941 yylhsminor.yy454=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0);
178942 sqlite3DequoteNumber(pParse, yylhsminor.yy454);
178943}
178944 yymsp[0].minor.yy454 = yylhsminor.yy454;
178945 break;
178946 default:
178947 /* (344) input ::= cmdlist */ yytestcase(yyruleno==344);
178948 /* (345) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==345);
178949 /* (346) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=346)((void) (0));
178950 /* (347) ecmd ::= SEMI */ yytestcase(yyruleno==347);
178951 /* (348) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==348);
178952 /* (349) ecmd ::= explain cmdx SEMI (NEVER REDUCES) */ assert(yyruleno!=349)((void) (0));
178953 /* (350) trans_opt ::= */ yytestcase(yyruleno==350);
178954 /* (351) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==351);
178955 /* (352) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==352);
178956 /* (353) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==353);
178957 /* (354) savepoint_opt ::= */ yytestcase(yyruleno==354);
178958 /* (355) cmd ::= create_table create_table_args */ yytestcase(yyruleno==355);
178959 /* (356) table_option_set ::= table_option (OPTIMIZED OUT) */ assert(yyruleno!=356)((void) (0));
178960 /* (357) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==357);
178961 /* (358) columnlist ::= columnname carglist */ yytestcase(yyruleno==358);
178962 /* (359) nm ::= ID|INDEXED|JOIN_KW */ yytestcase(yyruleno==359);
178963 /* (360) nm ::= STRING */ yytestcase(yyruleno==360);
178964 /* (361) typetoken ::= typename */ yytestcase(yyruleno==361);
178965 /* (362) typename ::= ID|STRING */ yytestcase(yyruleno==362);
178966 /* (363) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=363)((void) (0));
178967 /* (364) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=364)((void) (0));
178968 /* (365) carglist ::= carglist ccons */ yytestcase(yyruleno==365);
178969 /* (366) carglist ::= */ yytestcase(yyruleno==366);
178970 /* (367) ccons ::= NULL onconf */ yytestcase(yyruleno==367);
178971 /* (368) ccons ::= GENERATED ALWAYS AS generated */ yytestcase(yyruleno==368);
178972 /* (369) ccons ::= AS generated */ yytestcase(yyruleno==369);
178973 /* (370) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==370);
178974 /* (371) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==371);
178975 /* (372) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=372)((void) (0));
178976 /* (373) tconscomma ::= */ yytestcase(yyruleno==373);
178977 /* (374) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=374)((void) (0));
178978 /* (375) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=375)((void) (0));
178979 /* (376) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=376)((void) (0));
178980 /* (377) oneselect ::= values */ yytestcase(yyruleno==377);
178981 /* (378) sclp ::= selcollist COMMA */ yytestcase(yyruleno==378);
178982 /* (379) as ::= ID|STRING */ yytestcase(yyruleno==379);
178983 /* (380) indexed_opt ::= indexed_by (OPTIMIZED OUT) */ assert(yyruleno!=380)((void) (0));
178984 /* (381) returning ::= */ yytestcase(yyruleno==381);
178985 /* (382) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=382)((void) (0));
178986 /* (383) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==383);
178987 /* (384) case_operand ::= expr */ yytestcase(yyruleno==384);
178988 /* (385) exprlist ::= nexprlist */ yytestcase(yyruleno==385);
178989 /* (386) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=386)((void) (0));
178990 /* (387) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=387)((void) (0));
178991 /* (388) nmnum ::= ON */ yytestcase(yyruleno==388);
178992 /* (389) nmnum ::= DELETE */ yytestcase(yyruleno==389);
178993 /* (390) nmnum ::= DEFAULT */ yytestcase(yyruleno==390);
178994 /* (391) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==391);
178995 /* (392) foreach_clause ::= */ yytestcase(yyruleno==392);
178996 /* (393) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==393);
178997 /* (394) trnm ::= nm */ yytestcase(yyruleno==394);
178998 /* (395) tridxby ::= */ yytestcase(yyruleno==395);
178999 /* (396) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==396);
179000 /* (397) database_kw_opt ::= */ yytestcase(yyruleno==397);
179001 /* (398) kwcolumn_opt ::= */ yytestcase(yyruleno==398);
179002 /* (399) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==399);
179003 /* (400) vtabarglist ::= vtabarg */ yytestcase(yyruleno==400);
179004 /* (401) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==401);
179005 /* (402) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==402);
179006 /* (403) anylist ::= */ yytestcase(yyruleno==403);
179007 /* (404) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==404);
179008 /* (405) anylist ::= anylist ANY */ yytestcase(yyruleno==405);
179009 /* (406) with ::= */ yytestcase(yyruleno==406);
179010 /* (407) windowdefn_list ::= windowdefn (OPTIMIZED OUT) */ assert(yyruleno!=407)((void) (0));
179011 /* (408) window ::= frame_opt (OPTIMIZED OUT) */ assert(yyruleno!=408)((void) (0));
179012 break;
179013/********** End reduce actions ************************************************/
179014 };
179015 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) )((void) (0));
179016 yygoto = yyRuleInfoLhs[yyruleno];
179017 yysize = yyRuleInfoNRhs[yyruleno];
179018 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPEunsigned short int)yygoto);
179019
179020 /* There are no SHIFTREDUCE actions on nonterminals because the table
179021 ** generator has simplified them to pure REDUCE actions. */
179022 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) )((void) (0));
179023
179024 /* It is not possible for a REDUCE to be followed by an error */
179025 assert( yyact!=YY_ERROR_ACTION )((void) (0));
179026
179027 yymsp += yysize+1;
179028 yypParser->yytos = yymsp;
179029 yymsp->stateno = (YYACTIONTYPEunsigned short int)yyact;
179030 yymsp->major = (YYCODETYPEunsigned short int)yygoto;
179031 yyTraceShift(yypParser, yyact, "... then shift");
179032 return yyact;
179033}
179034
179035/*
179036** The following code executes when the parse fails
179037*/
179038#ifndef YYNOERRORRECOVERY1
179039static void yy_parse_failed(
179040 yyParser *yypParser /* The parser */
179041){
179042 sqlite3ParserARG_FETCH
179043 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
179044#ifndef NDEBUG1
179045 if( yyTraceFILE ){
179046 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
179047 }
179048#endif
179049 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
179050 /* Here code is inserted which will be executed whenever the
179051 ** parser fails */
179052/************ Begin %parse_failure code ***************************************/
179053/************ End %parse_failure code *****************************************/
179054 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
179055 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
179056}
179057#endif /* YYNOERRORRECOVERY */
179058
179059/*
179060** The following code executes when a syntax error first occurs.
179061*/
179062static void yy_syntax_error(
179063 yyParser *yypParser, /* The parser */
179064 int yymajor, /* The major type of the error token */
179065 sqlite3ParserTOKENTYPEToken yyminor /* The minor type of the error token */
179066){
179067 sqlite3ParserARG_FETCH
179068 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
179069#define TOKENyyminor yyminor
179070/************ Begin %syntax_error code ****************************************/
179071
179072 UNUSED_PARAMETER(yymajor)(void)(yymajor); /* Silence some compiler warnings */
179073 if( TOKENyyminor.z[0] ){
179074 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKENyyminor);
179075 }else{
179076 sqlite3ErrorMsg(pParse, "incomplete input");
179077 }
179078/************ End %syntax_error code ******************************************/
179079 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
179080 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
179081}
179082
179083/*
179084** The following is executed when the parser accepts
179085*/
179086static void yy_accept(
179087 yyParser *yypParser /* The parser */
179088){
179089 sqlite3ParserARG_FETCH
179090 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
179091#ifndef NDEBUG1
179092 if( yyTraceFILE ){
179093 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
179094 }
179095#endif
179096#ifndef YYNOERRORRECOVERY1
179097 yypParser->yyerrcnt = -1;
179098#endif
179099 assert( yypParser->yytos==yypParser->yystack )((void) (0));
179100 /* Here code is inserted which will be executed whenever the
179101 ** parser accepts */
179102/*********** Begin %parse_accept code *****************************************/
179103/*********** End %parse_accept code *******************************************/
179104 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
179105 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
179106}
179107
179108/* The main parser program.
179109** The first argument is a pointer to a structure obtained from
179110** "sqlite3ParserAlloc" which describes the current state of the parser.
179111** The second argument is the major token number. The third is
179112** the minor token. The fourth optional argument is whatever the
179113** user wants (and specified in the grammar) and is available for
179114** use by the action routines.
179115**
179116** Inputs:
179117** <ul>
179118** <li> A pointer to the parser (an opaque structure.)
179119** <li> The major token number.
179120** <li> The minor token number.
179121** <li> An option argument of a grammar-specified type.
179122** </ul>
179123**
179124** Outputs:
179125** None.
179126*/
179127SQLITE_PRIVATEstatic void sqlite3Parser(
179128 void *yyp, /* The parser */
179129 int yymajor, /* The major token code number */
179130 sqlite3ParserTOKENTYPEToken yyminor /* The value for the token */
179131 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
179132){
179133 YYMINORTYPE yyminorunion;
179134 YYACTIONTYPEunsigned short int yyact; /* The parser action. */
179135#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
179136 int yyendofinput; /* True if we are at the end of input */
179137#endif
179138#ifdef YYERRORSYMBOL
179139 int yyerrorhit = 0; /* True if yymajor has invoked an error */
179140#endif
179141 yyParser *yypParser = (yyParser*)yyp; /* The parser */
179142 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
179143 sqlite3ParserARG_STORE
179144
179145 assert( yypParser->yytos!=0 )((void) (0));
179146#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
179147 yyendofinput = (yymajor==0);
179148#endif
179149
179150 yyact = yypParser->yytos->stateno;
179151#ifndef NDEBUG1
179152 if( yyTraceFILE ){
179153 if( yyact < YY_MIN_REDUCE1257 ){
179154 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
179155 yyTracePrompt,yyTokenName[yymajor],yyact);
179156 }else{
179157 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
179158 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE1257);
179159 }
179160 }
179161#endif
179162
179163 while(1){ /* Exit by "break" */
179164 assert( yypParser->yytos>=yypParser->yystack )((void) (0));
179165 assert( yyact==yypParser->yytos->stateno )((void) (0));
179166 yyact = yy_find_shift_action((YYCODETYPEunsigned short int)yymajor,yyact);
179167 if( yyact >= YY_MIN_REDUCE1257 ){
179168 unsigned int yyruleno = yyact - YY_MIN_REDUCE1257; /* Reduce by this rule */
179169#ifndef NDEBUG1
179170 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) )((void) (0));
179171 if( yyTraceFILE ){
179172 int yysize = yyRuleInfoNRhs[yyruleno];
179173 if( yysize ){
179174 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
179175 yyTracePrompt,
179176 yyruleno, yyRuleName[yyruleno],
179177 yyruleno<YYNRULE_WITH_ACTION344 ? "" : " without external action",
179178 yypParser->yytos[yysize].stateno);
179179 }else{
179180 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
179181 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
179182 yyruleno<YYNRULE_WITH_ACTION344 ? "" : " without external action");
179183 }
179184 }
179185#endif /* NDEBUG */
179186
179187 /* Check that the stack is large enough to grow by a single entry
179188 ** if the RHS of the rule is empty. This ensures that there is room
179189 ** enough on the stack to push the LHS value */
179190 if( yyRuleInfoNRhs[yyruleno]==0 ){
179191#ifdef YYTRACKMAXSTACKDEPTH
179192 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
179193 yypParser->yyhwm++;
179194 assert( yypParser->yyhwm ==((void) (0))
179195 (int)(yypParser->yytos - yypParser->yystack))((void) (0));
179196 }
179197#endif
179198 if( yypParser->yytos>=yypParser->yystackEnd ){
179199 if( yyGrowStack(yypParser) ){
179200 yyStackOverflow(yypParser);
179201 break;
179202 }
179203 }
179204 }
179205 yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor sqlite3ParserCTX_PARAM,pParse);
179206 }else if( yyact <= YY_MAX_SHIFTREDUCE1253 ){
179207 yy_shift(yypParser,yyact,(YYCODETYPEunsigned short int)yymajor,yyminor);
179208#ifndef YYNOERRORRECOVERY1
179209 yypParser->yyerrcnt--;
179210#endif
179211 break;
179212 }else if( yyact==YY_ACCEPT_ACTION1255 ){
179213 yypParser->yytos--;
179214 yy_accept(yypParser);
179215 return;
179216 }else{
179217 assert( yyact == YY_ERROR_ACTION )((void) (0));
179218 yyminorunion.yy0 = yyminor;
179219#ifdef YYERRORSYMBOL
179220 int yymx;
179221#endif
179222#ifndef NDEBUG1
179223 if( yyTraceFILE ){
179224 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
179225 }
179226#endif
179227#ifdef YYERRORSYMBOL
179228 /* A syntax error has occurred.
179229 ** The response to an error depends upon whether or not the
179230 ** grammar defines an error token "ERROR".
179231 **
179232 ** This is what we do if the grammar does define ERROR:
179233 **
179234 ** * Call the %syntax_error function.
179235 **
179236 ** * Begin popping the stack until we enter a state where
179237 ** it is legal to shift the error symbol, then shift
179238 ** the error symbol.
179239 **
179240 ** * Set the error count to three.
179241 **
179242 ** * Begin accepting and shifting new tokens. No new error
179243 ** processing will occur until three tokens have been
179244 ** shifted successfully.
179245 **
179246 */
179247 if( yypParser->yyerrcnt<0 ){
179248 yy_syntax_error(yypParser,yymajor,yyminor);
179249 }
179250 yymx = yypParser->yytos->major;
179251 if( yymx==YYERRORSYMBOL || yyerrorhit ){
179252#ifndef NDEBUG1
179253 if( yyTraceFILE ){
179254 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
179255 yyTracePrompt,yyTokenName[yymajor]);
179256 }
179257#endif
179258 yy_destructor(yypParser, (YYCODETYPEunsigned short int)yymajor, &yyminorunion);
179259 yymajor = YYNOCODE322;
179260 }else{
179261 while( yypParser->yytos > yypParser->yystack ){
179262 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
179263 YYERRORSYMBOL);
179264 if( yyact<=YY_MAX_SHIFTREDUCE1253 ) break;
179265 yy_pop_parser_stack(yypParser);
179266 }
179267 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
179268 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
179269 yy_parse_failed(yypParser);
179270#ifndef YYNOERRORRECOVERY1
179271 yypParser->yyerrcnt = -1;
179272#endif
179273 yymajor = YYNOCODE322;
179274 }else if( yymx!=YYERRORSYMBOL ){
179275 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
179276 }
179277 }
179278 yypParser->yyerrcnt = 3;
179279 yyerrorhit = 1;
179280 if( yymajor==YYNOCODE322 ) break;
179281 yyact = yypParser->yytos->stateno;
179282#elif defined(YYNOERRORRECOVERY1)
179283 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
179284 ** do any kind of error recovery. Instead, simply invoke the syntax
179285 ** error routine and continue going as if nothing had happened.
179286 **
179287 ** Applications can set this macro (for example inside %include) if
179288 ** they intend to abandon the parse upon the first syntax error seen.
179289 */
179290 yy_syntax_error(yypParser,yymajor, yyminor);
179291 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
179292 break;
179293#else /* YYERRORSYMBOL is not defined */
179294 /* This is what we do if the grammar does not define ERROR:
179295 **
179296 ** * Report an error message, and throw away the input token.
179297 **
179298 ** * If the input token is $, then fail the parse.
179299 **
179300 ** As before, subsequent error messages are suppressed until
179301 ** three input tokens have been successfully shifted.
179302 */
179303 if( yypParser->yyerrcnt<=0 ){
179304 yy_syntax_error(yypParser,yymajor, yyminor);
179305 }
179306 yypParser->yyerrcnt = 3;
179307 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
179308 if( yyendofinput ){
179309 yy_parse_failed(yypParser);
179310#ifndef YYNOERRORRECOVERY1
179311 yypParser->yyerrcnt = -1;
179312#endif
179313 }
179314 break;
179315#endif
179316 }
179317 }
179318#ifndef NDEBUG1
179319 if( yyTraceFILE ){
179320 yyStackEntry *i;
179321 char cDiv = '[';
179322 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
179323 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
179324 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
179325 cDiv = ' ';
179326 }
179327 fprintf(yyTraceFILE,"]\n");
179328 }
179329#endif
179330 return;
179331}
179332
179333/*
179334** Return the fallback token corresponding to canonical token iToken, or
179335** 0 if iToken has no fallback.
179336*/
179337SQLITE_PRIVATEstatic int sqlite3ParserFallback(int iToken){
179338#ifdef YYFALLBACK1
179339 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) )((void) (0));
179340 return yyFallback[iToken];
179341#else
179342 (void)iToken;
179343 return 0;
179344#endif
179345}
179346
179347/************** End of parse.c ***********************************************/
179348/************** Begin file tokenize.c ****************************************/
179349/*
179350** 2001 September 15
179351**
179352** The author disclaims copyright to this source code. In place of
179353** a legal notice, here is a blessing:
179354**
179355** May you do good and not evil.
179356** May you find forgiveness for yourself and forgive others.
179357** May you share freely, never taking more than you give.
179358**
179359*************************************************************************
179360** An tokenizer for SQL
179361**
179362** This file contains C code that splits an SQL input string up into
179363** individual tokens and sends those tokens one-by-one over to the
179364** parser for analysis.
179365*/
179366/* #include "sqliteInt.h" */
179367/* #include <stdlib.h> */
179368
179369/* Character classes for tokenizing
179370**
179371** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
179372** using a lookup table, whereas a switch() directly on c uses a binary search.
179373** The lookup table is much faster. To maximize speed, and to ensure that
179374** a lookup table is used, all of the classes need to be small integers and
179375** all of them need to be used within the switch.
179376*/
179377#define CC_X0 0 /* The letter 'x', or start of BLOB literal */
179378#define CC_KYWD01 1 /* First letter of a keyword */
179379#define CC_KYWD2 2 /* Alphabetics or '_'. Usable in a keyword */
179380#define CC_DIGIT3 3 /* Digits */
179381#define CC_DOLLAR4 4 /* '$' */
179382#define CC_VARALPHA5 5 /* '@', '#', ':'. Alphabetic SQL variables */
179383#define CC_VARNUM6 6 /* '?'. Numeric SQL variables */
179384#define CC_SPACE7 7 /* Space characters */
179385#define CC_QUOTE8 8 /* '"', '\'', or '`'. String literals, quoted ids */
179386#define CC_QUOTE29 9 /* '['. [...] style quoted ids */
179387#define CC_PIPE10 10 /* '|'. Bitwise OR or concatenate */
179388#define CC_MINUS11 11 /* '-'. Minus or SQL-style comment */
179389#define CC_LT12 12 /* '<'. Part of < or <= or <> */
179390#define CC_GT13 13 /* '>'. Part of > or >= */
179391#define CC_EQ14 14 /* '='. Part of = or == */
179392#define CC_BANG15 15 /* '!'. Part of != */
179393#define CC_SLASH16 16 /* '/'. / or c-style comment */
179394#define CC_LP17 17 /* '(' */
179395#define CC_RP18 18 /* ')' */
179396#define CC_SEMI19 19 /* ';' */
179397#define CC_PLUS20 20 /* '+' */
179398#define CC_STAR21 21 /* '*' */
179399#define CC_PERCENT22 22 /* '%' */
179400#define CC_COMMA23 23 /* ',' */
179401#define CC_AND24 24 /* '&' */
179402#define CC_TILDA25 25 /* '~' */
179403#define CC_DOT26 26 /* '.' */
179404#define CC_ID27 27 /* unicode characters usable in IDs */
179405#define CC_ILLEGAL28 28 /* Illegal character */
179406#define CC_NUL29 29 /* 0x00 */
179407#define CC_BOM30 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */
179408
179409static const unsigned char aiClass[] = {
179410#ifdef SQLITE_ASCII1
179411/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
179412/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28,
179413/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
179414/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
179415/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
179416/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
179417/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2,
179418/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
179419/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28,
179420/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
179421/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
179422/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
179423/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
179424/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
179425/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
179426/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30,
179427/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27
179428#endif
179429#ifdef SQLITE_EBCDIC
179430/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
179431/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28,
179432/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
179433/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
179434/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
179435/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10,
179436/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28,
179437/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6,
179438/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8,
179439/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
179440/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
179441/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
179442/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28,
179443/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
179444/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28,
179445/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28,
179446/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28,
179447#endif
179448};
179449
179450/*
179451** The charMap() macro maps alphabetic characters (only) into their
179452** lower-case ASCII equivalent. On ASCII machines, this is just
179453** an upper-to-lower case map. On EBCDIC machines we also need
179454** to adjust the encoding. The mapping is only valid for alphabetics
179455** which are the only characters for which this feature is used.
179456**
179457** Used by keywordhash.h
179458*/
179459#ifdef SQLITE_ASCII1
179460# define charMap(X)sqlite3UpperToLower[(unsigned char)X] sqlite3UpperToLower[(unsigned char)X]
179461#endif
179462#ifdef SQLITE_EBCDIC
179463# define charMap(X)sqlite3UpperToLower[(unsigned char)X] ebcdicToAscii[(unsigned char)X]
179464const unsigned char ebcdicToAscii[] = {
179465/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
179466 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
179467 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
179468 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
179469 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
179470 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
179471 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
179472 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
179473 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
179474 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
179475 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
179476 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
179477 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
179478 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
179479 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
179480 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
179481 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
179482};
179483#endif
179484
179485/*
179486** The sqlite3KeywordCode function looks up an identifier to determine if
179487** it is a keyword. If it is a keyword, the token code of that keyword is
179488** returned. If the input is not a keyword, TK_ID is returned.
179489**
179490** The implementation of this routine was generated by a program,
179491** mkkeywordhash.c, located in the tool subdirectory of the distribution.
179492** The output of the mkkeywordhash.c program is written into a file
179493** named keywordhash.h and then included into this source file by
179494** the #include below.
179495*/
179496/************** Include keywordhash.h in the middle of tokenize.c ************/
179497/************** Begin file keywordhash.h *************************************/
179498/***** This file contains automatically generated code ******
179499**
179500** The code in this file has been automatically generated by
179501**
179502** sqlite/tool/mkkeywordhash.c
179503**
179504** The code in this file implements a function that determines whether
179505** or not a given identifier is really an SQL keyword. The same thing
179506** might be implemented more directly using a hand-written hash table.
179507** But by using this automatically generated code, the size of the code
179508** is substantially reduced. This is important for embedded applications
179509** on platforms with limited memory.
179510*/
179511/* Hash score: 231 */
179512/* zKWText[] encodes 1007 bytes of keyword text in 667 bytes */
179513/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
179514/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYISNULLSAVEPOINTERSECT */
179515/* IESNOTNULLIKEXCEPTRANSACTIONATURALTERAISEXCLUSIVEXISTS */
179516/* CONSTRAINTOFFSETRIGGERANGENERATEDETACHAVINGLOBEGINNEREFERENCES */
179517/* UNIQUERYWITHOUTERELEASEATTACHBETWEENOTHINGROUPSCASCADEFAULT */
179518/* CASECOLLATECREATECURRENT_DATEIMMEDIATEJOINSERTMATCHPLANALYZE */
179519/* PRAGMATERIALIZEDEFERREDISTINCTUPDATEVALUESVIRTUALWAYSWHENWHERE */
179520/* CURSIVEABORTAFTERENAMEANDROPARTITIONAUTOINCREMENTCASTCOLUMN */
179521/* COMMITCONFLICTCROSSCURRENT_TIMESTAMPRECEDINGFAILASTFILTER */
179522/* EPLACEFIRSTFOLLOWINGFROMFULLIMITIFORDERESTRICTOTHERSOVER */
179523/* ETURNINGRIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBY */
179524/* INITIALLYPRIMARY */
179525static const char zKWText[666] = {
179526 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
179527 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
179528 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
179529 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
179530 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
179531 'T','E','M','P','O','R','A','R','Y','I','S','N','U','L','L','S','A','V',
179532 'E','P','O','I','N','T','E','R','S','E','C','T','I','E','S','N','O','T',
179533 'N','U','L','L','I','K','E','X','C','E','P','T','R','A','N','S','A','C',
179534 'T','I','O','N','A','T','U','R','A','L','T','E','R','A','I','S','E','X',
179535 'C','L','U','S','I','V','E','X','I','S','T','S','C','O','N','S','T','R',
179536 'A','I','N','T','O','F','F','S','E','T','R','I','G','G','E','R','A','N',
179537 'G','E','N','E','R','A','T','E','D','E','T','A','C','H','A','V','I','N',
179538 'G','L','O','B','E','G','I','N','N','E','R','E','F','E','R','E','N','C',
179539 'E','S','U','N','I','Q','U','E','R','Y','W','I','T','H','O','U','T','E',
179540 'R','E','L','E','A','S','E','A','T','T','A','C','H','B','E','T','W','E',
179541 'E','N','O','T','H','I','N','G','R','O','U','P','S','C','A','S','C','A',
179542 'D','E','F','A','U','L','T','C','A','S','E','C','O','L','L','A','T','E',
179543 'C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E',
179544 'I','M','M','E','D','I','A','T','E','J','O','I','N','S','E','R','T','M',
179545 'A','T','C','H','P','L','A','N','A','L','Y','Z','E','P','R','A','G','M',
179546 'A','T','E','R','I','A','L','I','Z','E','D','E','F','E','R','R','E','D',
179547 'I','S','T','I','N','C','T','U','P','D','A','T','E','V','A','L','U','E',
179548 'S','V','I','R','T','U','A','L','W','A','Y','S','W','H','E','N','W','H',
179549 'E','R','E','C','U','R','S','I','V','E','A','B','O','R','T','A','F','T',
179550 'E','R','E','N','A','M','E','A','N','D','R','O','P','A','R','T','I','T',
179551 'I','O','N','A','U','T','O','I','N','C','R','E','M','E','N','T','C','A',
179552 'S','T','C','O','L','U','M','N','C','O','M','M','I','T','C','O','N','F',
179553 'L','I','C','T','C','R','O','S','S','C','U','R','R','E','N','T','_','T',
179554 'I','M','E','S','T','A','M','P','R','E','C','E','D','I','N','G','F','A',
179555 'I','L','A','S','T','F','I','L','T','E','R','E','P','L','A','C','E','F',
179556 'I','R','S','T','F','O','L','L','O','W','I','N','G','F','R','O','M','F',
179557 'U','L','L','I','M','I','T','I','F','O','R','D','E','R','E','S','T','R',
179558 'I','C','T','O','T','H','E','R','S','O','V','E','R','E','T','U','R','N',
179559 'I','N','G','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O',
179560 'W','S','U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S',
179561 'I','N','G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W',
179562 'B','Y','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
179563};
179564/* aKWHash[i] is the hash value for the i-th keyword */
179565static const unsigned char aKWHash[127] = {
179566 84, 92, 134, 82, 105, 29, 0, 0, 94, 0, 85, 72, 0,
179567 53, 35, 86, 15, 0, 42, 97, 54, 89, 135, 19, 0, 0,
179568 140, 0, 40, 129, 0, 22, 107, 0, 9, 0, 0, 123, 80,
179569 0, 78, 6, 0, 65, 103, 147, 0, 136, 115, 0, 0, 48,
179570 0, 90, 24, 0, 17, 0, 27, 70, 23, 26, 5, 60, 142,
179571 110, 122, 0, 73, 91, 71, 145, 61, 120, 74, 0, 49, 0,
179572 11, 41, 0, 113, 0, 0, 0, 109, 10, 111, 116, 125, 14,
179573 50, 124, 0, 100, 0, 18, 121, 144, 56, 130, 139, 88, 83,
179574 37, 30, 126, 0, 0, 108, 51, 131, 128, 0, 34, 0, 0,
179575 132, 0, 98, 38, 39, 0, 20, 45, 117, 93,
179576};
179577/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
179578** then the i-th keyword has no more hash collisions. Otherwise,
179579** the next keyword with the same hash is aKWHash[i]-1. */
179580static const unsigned char aKWNext[148] = {0,
179581 0, 0, 0, 0, 4, 0, 43, 0, 0, 106, 114, 0, 0,
179582 0, 2, 0, 0, 143, 0, 0, 0, 13, 0, 0, 0, 0,
179583 141, 0, 0, 119, 52, 0, 0, 137, 12, 0, 0, 62, 0,
179584 138, 0, 133, 0, 0, 36, 0, 0, 28, 77, 0, 0, 0,
179585 0, 59, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179586 0, 69, 0, 0, 0, 0, 0, 146, 3, 0, 58, 0, 1,
179587 75, 0, 0, 0, 31, 0, 0, 0, 0, 0, 127, 0, 104,
179588 0, 64, 66, 63, 0, 0, 0, 0, 0, 46, 0, 16, 8,
179589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 101, 0,
179590 112, 21, 7, 67, 0, 79, 96, 118, 0, 0, 68, 0, 0,
179591 99, 44, 0, 55, 0, 76, 0, 95, 32, 33, 57, 25, 0,
179592 102, 0, 0, 87,
179593};
179594/* aKWLen[i] is the length (in bytes) of the i-th keyword */
179595static const unsigned char aKWLen[148] = {0,
179596 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
179597 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
179598 6, 9, 4, 2, 6, 5, 9, 9, 4, 7, 3, 2, 4,
179599 4, 6, 11, 6, 2, 7, 5, 5, 9, 6, 10, 4, 6,
179600 2, 3, 7, 5, 9, 6, 6, 4, 5, 5, 10, 6, 5,
179601 7, 4, 5, 7, 6, 7, 7, 6, 5, 7, 3, 7, 4,
179602 7, 6, 12, 9, 4, 6, 5, 4, 7, 6, 12, 8, 8,
179603 2, 6, 6, 7, 6, 4, 5, 9, 5, 5, 6, 3, 4,
179604 9, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 9,
179605 4, 4, 6, 7, 5, 9, 4, 4, 5, 2, 5, 8, 6,
179606 4, 9, 5, 8, 4, 3, 9, 5, 5, 6, 4, 6, 2,
179607 2, 9, 3, 7,
179608};
179609/* aKWOffset[i] is the index into zKWText[] of the start of
179610** the text for the i-th keyword. */
179611static const unsigned short int aKWOffset[148] = {0,
179612 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
179613 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
179614 86, 90, 90, 94, 99, 101, 105, 111, 119, 123, 123, 123, 126,
179615 129, 132, 137, 142, 146, 147, 152, 156, 160, 168, 174, 181, 184,
179616 184, 187, 189, 195, 198, 206, 211, 216, 219, 222, 226, 236, 239,
179617 244, 244, 248, 252, 259, 265, 271, 277, 277, 283, 284, 288, 295,
179618 299, 306, 312, 324, 333, 335, 341, 346, 348, 355, 359, 370, 377,
179619 378, 385, 391, 397, 402, 408, 412, 415, 424, 429, 433, 439, 441,
179620 444, 453, 455, 457, 466, 470, 476, 482, 490, 495, 495, 495, 511,
179621 520, 523, 527, 532, 539, 544, 553, 557, 560, 565, 567, 571, 579,
179622 585, 588, 597, 602, 610, 610, 614, 623, 628, 633, 639, 642, 645,
179623 648, 650, 655, 659,
179624};
179625/* aKWCode[i] is the parser symbol code for the i-th keyword */
179626static const unsigned char aKWCode[148] = {0,
179627 TK_REINDEX99, TK_INDEXED117, TK_INDEX162, TK_DESC39, TK_ESCAPE59,
179628 TK_EACH41, TK_CHECK125, TK_KEY68, TK_BEFORE33, TK_FOREIGN133,
179629 TK_FOR63, TK_IGNORE64, TK_LIKE_KW48, TK_EXPLAIN2, TK_INSTEAD66,
179630 TK_ADD164, TK_DATABASE38, TK_AS24, TK_SELECT139, TK_TABLE16,
179631 TK_JOIN_KW119, TK_THEN160, TK_END11, TK_DEFERRABLE132, TK_ELSE161,
179632 TK_EXCLUDE92, TK_DELETE129, TK_TEMP21, TK_TEMP21, TK_OR43,
179633 TK_ISNULL51, TK_NULLS83, TK_SAVEPOINT13, TK_INTERSECT138, TK_TIES95,
179634 TK_NOTNULL52, TK_NOT19, TK_NO67, TK_NULL122, TK_LIKE_KW48,
179635 TK_EXCEPT137, TK_TRANSACTION6,TK_ACTION28, TK_ON116, TK_JOIN_KW119,
179636 TK_ALTER163, TK_RAISE72, TK_EXCLUSIVE9, TK_EXISTS20, TK_CONSTRAINT120,
179637 TK_INTO152, TK_OFFSET70, TK_OF69, TK_SET131, TK_TRIGGER78,
179638 TK_RANGE90, TK_GENERATED96, TK_DETACH40, TK_HAVING148, TK_LIKE_KW48,
179639 TK_BEGIN5, TK_JOIN_KW119, TK_REFERENCES126, TK_UNIQUE124, TK_QUERY3,
179640 TK_WITHOUT26, TK_WITH82, TK_JOIN_KW119, TK_RELEASE14, TK_ATTACH32,
179641 TK_BETWEEN49, TK_NOTHING153, TK_GROUPS93, TK_GROUP147, TK_CASCADE35,
179642 TK_ASC31, TK_DEFAULT121, TK_CASE158, TK_COLLATE114, TK_CREATE17,
179643 TK_CTIME_KW101, TK_IMMEDIATE8, TK_JOIN144, TK_INSERT128, TK_MATCH47,
179644 TK_PLAN4, TK_ANALYZE30, TK_PRAGMA71, TK_MATERIALIZED98, TK_DEFERRED7,
179645 TK_DISTINCT141, TK_IS45, TK_UPDATE130, TK_VALUES140, TK_VIRTUAL81,
179646 TK_ALWAYS97, TK_WHEN159, TK_WHERE150, TK_RECURSIVE73, TK_ABORT27,
179647 TK_AFTER29, TK_RENAME100, TK_AND44, TK_DROP134, TK_PARTITION88,
179648 TK_AUTOINCR127, TK_TO15, TK_IN50, TK_CAST36, TK_COLUMNKW61,
179649 TK_COMMIT10, TK_CONFLICT37, TK_JOIN_KW119, TK_CTIME_KW101, TK_CTIME_KW101,
179650 TK_CURRENT86, TK_PRECEDING89, TK_FAIL42, TK_LAST85, TK_FILTER167,
179651 TK_REPLACE74, TK_FIRST84, TK_FOLLOWING87, TK_FROM143, TK_JOIN_KW119,
179652 TK_LIMIT149, TK_IF18, TK_ORDER146, TK_RESTRICT75, TK_OTHERS94,
179653 TK_OVER166, TK_RETURNING151, TK_JOIN_KW119, TK_ROLLBACK12, TK_ROWS77,
179654 TK_ROW76, TK_UNBOUNDED91, TK_UNION135, TK_USING145, TK_VACUUM79,
179655 TK_VIEW80, TK_WINDOW165, TK_DO62, TK_BY34, TK_INITIALLY65,
179656 TK_ALL136, TK_PRIMARY123,
179657};
179658/* Hash table decoded:
179659** 0: INSERT
179660** 1: IS
179661** 2: ROLLBACK TRIGGER
179662** 3: IMMEDIATE
179663** 4: PARTITION
179664** 5: TEMP
179665** 6:
179666** 7:
179667** 8: VALUES WITHOUT
179668** 9:
179669** 10: MATCH
179670** 11: NOTHING
179671** 12:
179672** 13: OF
179673** 14: TIES IGNORE
179674** 15: PLAN
179675** 16: INSTEAD INDEXED
179676** 17:
179677** 18: TRANSACTION RIGHT
179678** 19: WHEN
179679** 20: SET HAVING
179680** 21: MATERIALIZED IF
179681** 22: ROWS
179682** 23: SELECT
179683** 24:
179684** 25:
179685** 26: VACUUM SAVEPOINT
179686** 27:
179687** 28: LIKE UNION VIRTUAL REFERENCES
179688** 29: RESTRICT
179689** 30:
179690** 31: THEN REGEXP
179691** 32: TO
179692** 33:
179693** 34: BEFORE
179694** 35:
179695** 36:
179696** 37: FOLLOWING COLLATE CASCADE
179697** 38: CREATE
179698** 39:
179699** 40: CASE REINDEX
179700** 41: EACH
179701** 42:
179702** 43: QUERY
179703** 44: AND ADD
179704** 45: PRIMARY ANALYZE
179705** 46:
179706** 47: ROW ASC DETACH
179707** 48: CURRENT_TIME CURRENT_DATE
179708** 49:
179709** 50:
179710** 51: EXCLUSIVE TEMPORARY
179711** 52:
179712** 53: DEFERRED
179713** 54: DEFERRABLE
179714** 55:
179715** 56: DATABASE
179716** 57:
179717** 58: DELETE VIEW GENERATED
179718** 59: ATTACH
179719** 60: END
179720** 61: EXCLUDE
179721** 62: ESCAPE DESC
179722** 63: GLOB
179723** 64: WINDOW ELSE
179724** 65: COLUMN
179725** 66: FIRST
179726** 67:
179727** 68: GROUPS ALL
179728** 69: DISTINCT DROP KEY
179729** 70: BETWEEN
179730** 71: INITIALLY
179731** 72: BEGIN
179732** 73: FILTER CHECK ACTION
179733** 74: GROUP INDEX
179734** 75:
179735** 76: EXISTS DEFAULT
179736** 77:
179737** 78: FOR CURRENT_TIMESTAMP
179738** 79: EXCEPT
179739** 80:
179740** 81: CROSS
179741** 82:
179742** 83:
179743** 84:
179744** 85: CAST
179745** 86: FOREIGN AUTOINCREMENT
179746** 87: COMMIT
179747** 88: CURRENT AFTER ALTER
179748** 89: FULL FAIL CONFLICT
179749** 90: EXPLAIN
179750** 91: CONSTRAINT
179751** 92: FROM ALWAYS
179752** 93:
179753** 94: ABORT
179754** 95:
179755** 96: AS DO
179756** 97: REPLACE WITH RELEASE
179757** 98: BY RENAME
179758** 99: RANGE RAISE
179759** 100: OTHERS
179760** 101: USING NULLS
179761** 102: PRAGMA
179762** 103: JOIN ISNULL OFFSET
179763** 104: NOT
179764** 105: OR LAST LEFT
179765** 106: LIMIT
179766** 107:
179767** 108:
179768** 109: IN
179769** 110: INTO
179770** 111: OVER RECURSIVE
179771** 112: ORDER OUTER
179772** 113:
179773** 114: INTERSECT UNBOUNDED
179774** 115:
179775** 116:
179776** 117: RETURNING ON
179777** 118:
179778** 119: WHERE
179779** 120: NO INNER
179780** 121: NULL
179781** 122:
179782** 123: TABLE
179783** 124: NATURAL NOTNULL
179784** 125: PRECEDING
179785** 126: UPDATE UNIQUE
179786*/
179787/* Check to see if z[0..n-1] is a keyword. If it is, write the
179788** parser symbol code for that keyword into *pType. Always
179789** return the integer n (the length of the token). */
179790static int keywordCode(const char *z, int n, int *pType){
179791 int i, j;
179792 const char *zKW;
179793 assert( n>=2 )((void) (0));
179794 i = ((charMap(z[0])sqlite3UpperToLower[(unsigned char)z[0]]*4) ^ (charMap(z[n-1])sqlite3UpperToLower[(unsigned char)z[n-1]]*3) ^ n*1) % 127;
179795 for(i=(int)aKWHash[i]; i>0; i=aKWNext[i]){
179796 if( aKWLen[i]!=n ) continue;
179797 zKW = &zKWText[aKWOffset[i]];
179798#ifdef SQLITE_ASCII1
179799 if( (z[0]&~0x20)!=zKW[0] ) continue;
179800 if( (z[1]&~0x20)!=zKW[1] ) continue;
179801 j = 2;
179802 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
179803#endif
179804#ifdef SQLITE_EBCDIC
179805 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;
179806 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;
179807 j = 2;
179808 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++; }
179809#endif
179810 if( j<n ) continue;
179811 testcase( i==1 ); /* REINDEX */
179812 testcase( i==2 ); /* INDEXED */
179813 testcase( i==3 ); /* INDEX */
179814 testcase( i==4 ); /* DESC */
179815 testcase( i==5 ); /* ESCAPE */
179816 testcase( i==6 ); /* EACH */
179817 testcase( i==7 ); /* CHECK */
179818 testcase( i==8 ); /* KEY */
179819 testcase( i==9 ); /* BEFORE */
179820 testcase( i==10 ); /* FOREIGN */
179821 testcase( i==11 ); /* FOR */
179822 testcase( i==12 ); /* IGNORE */
179823 testcase( i==13 ); /* REGEXP */
179824 testcase( i==14 ); /* EXPLAIN */
179825 testcase( i==15 ); /* INSTEAD */
179826 testcase( i==16 ); /* ADD */
179827 testcase( i==17 ); /* DATABASE */
179828 testcase( i==18 ); /* AS */
179829 testcase( i==19 ); /* SELECT */
179830 testcase( i==20 ); /* TABLE */
179831 testcase( i==21 ); /* LEFT */
179832 testcase( i==22 ); /* THEN */
179833 testcase( i==23 ); /* END */
179834 testcase( i==24 ); /* DEFERRABLE */
179835 testcase( i==25 ); /* ELSE */
179836 testcase( i==26 ); /* EXCLUDE */
179837 testcase( i==27 ); /* DELETE */
179838 testcase( i==28 ); /* TEMPORARY */
179839 testcase( i==29 ); /* TEMP */
179840 testcase( i==30 ); /* OR */
179841 testcase( i==31 ); /* ISNULL */
179842 testcase( i==32 ); /* NULLS */
179843 testcase( i==33 ); /* SAVEPOINT */
179844 testcase( i==34 ); /* INTERSECT */
179845 testcase( i==35 ); /* TIES */
179846 testcase( i==36 ); /* NOTNULL */
179847 testcase( i==37 ); /* NOT */
179848 testcase( i==38 ); /* NO */
179849 testcase( i==39 ); /* NULL */
179850 testcase( i==40 ); /* LIKE */
179851 testcase( i==41 ); /* EXCEPT */
179852 testcase( i==42 ); /* TRANSACTION */
179853 testcase( i==43 ); /* ACTION */
179854 testcase( i==44 ); /* ON */
179855 testcase( i==45 ); /* NATURAL */
179856 testcase( i==46 ); /* ALTER */
179857 testcase( i==47 ); /* RAISE */
179858 testcase( i==48 ); /* EXCLUSIVE */
179859 testcase( i==49 ); /* EXISTS */
179860 testcase( i==50 ); /* CONSTRAINT */
179861 testcase( i==51 ); /* INTO */
179862 testcase( i==52 ); /* OFFSET */
179863 testcase( i==53 ); /* OF */
179864 testcase( i==54 ); /* SET */
179865 testcase( i==55 ); /* TRIGGER */
179866 testcase( i==56 ); /* RANGE */
179867 testcase( i==57 ); /* GENERATED */
179868 testcase( i==58 ); /* DETACH */
179869 testcase( i==59 ); /* HAVING */
179870 testcase( i==60 ); /* GLOB */
179871 testcase( i==61 ); /* BEGIN */
179872 testcase( i==62 ); /* INNER */
179873 testcase( i==63 ); /* REFERENCES */
179874 testcase( i==64 ); /* UNIQUE */
179875 testcase( i==65 ); /* QUERY */
179876 testcase( i==66 ); /* WITHOUT */
179877 testcase( i==67 ); /* WITH */
179878 testcase( i==68 ); /* OUTER */
179879 testcase( i==69 ); /* RELEASE */
179880 testcase( i==70 ); /* ATTACH */
179881 testcase( i==71 ); /* BETWEEN */
179882 testcase( i==72 ); /* NOTHING */
179883 testcase( i==73 ); /* GROUPS */
179884 testcase( i==74 ); /* GROUP */
179885 testcase( i==75 ); /* CASCADE */
179886 testcase( i==76 ); /* ASC */
179887 testcase( i==77 ); /* DEFAULT */
179888 testcase( i==78 ); /* CASE */
179889 testcase( i==79 ); /* COLLATE */
179890 testcase( i==80 ); /* CREATE */
179891 testcase( i==81 ); /* CURRENT_DATE */
179892 testcase( i==82 ); /* IMMEDIATE */
179893 testcase( i==83 ); /* JOIN */
179894 testcase( i==84 ); /* INSERT */
179895 testcase( i==85 ); /* MATCH */
179896 testcase( i==86 ); /* PLAN */
179897 testcase( i==87 ); /* ANALYZE */
179898 testcase( i==88 ); /* PRAGMA */
179899 testcase( i==89 ); /* MATERIALIZED */
179900 testcase( i==90 ); /* DEFERRED */
179901 testcase( i==91 ); /* DISTINCT */
179902 testcase( i==92 ); /* IS */
179903 testcase( i==93 ); /* UPDATE */
179904 testcase( i==94 ); /* VALUES */
179905 testcase( i==95 ); /* VIRTUAL */
179906 testcase( i==96 ); /* ALWAYS */
179907 testcase( i==97 ); /* WHEN */
179908 testcase( i==98 ); /* WHERE */
179909 testcase( i==99 ); /* RECURSIVE */
179910 testcase( i==100 ); /* ABORT */
179911 testcase( i==101 ); /* AFTER */
179912 testcase( i==102 ); /* RENAME */
179913 testcase( i==103 ); /* AND */
179914 testcase( i==104 ); /* DROP */
179915 testcase( i==105 ); /* PARTITION */
179916 testcase( i==106 ); /* AUTOINCREMENT */
179917 testcase( i==107 ); /* TO */
179918 testcase( i==108 ); /* IN */
179919 testcase( i==109 ); /* CAST */
179920 testcase( i==110 ); /* COLUMN */
179921 testcase( i==111 ); /* COMMIT */
179922 testcase( i==112 ); /* CONFLICT */
179923 testcase( i==113 ); /* CROSS */
179924 testcase( i==114 ); /* CURRENT_TIMESTAMP */
179925 testcase( i==115 ); /* CURRENT_TIME */
179926 testcase( i==116 ); /* CURRENT */
179927 testcase( i==117 ); /* PRECEDING */
179928 testcase( i==118 ); /* FAIL */
179929 testcase( i==119 ); /* LAST */
179930 testcase( i==120 ); /* FILTER */
179931 testcase( i==121 ); /* REPLACE */
179932 testcase( i==122 ); /* FIRST */
179933 testcase( i==123 ); /* FOLLOWING */
179934 testcase( i==124 ); /* FROM */
179935 testcase( i==125 ); /* FULL */
179936 testcase( i==126 ); /* LIMIT */
179937 testcase( i==127 ); /* IF */
179938 testcase( i==128 ); /* ORDER */
179939 testcase( i==129 ); /* RESTRICT */
179940 testcase( i==130 ); /* OTHERS */
179941 testcase( i==131 ); /* OVER */
179942 testcase( i==132 ); /* RETURNING */
179943 testcase( i==133 ); /* RIGHT */
179944 testcase( i==134 ); /* ROLLBACK */
179945 testcase( i==135 ); /* ROWS */
179946 testcase( i==136 ); /* ROW */
179947 testcase( i==137 ); /* UNBOUNDED */
179948 testcase( i==138 ); /* UNION */
179949 testcase( i==139 ); /* USING */
179950 testcase( i==140 ); /* VACUUM */
179951 testcase( i==141 ); /* VIEW */
179952 testcase( i==142 ); /* WINDOW */
179953 testcase( i==143 ); /* DO */
179954 testcase( i==144 ); /* BY */
179955 testcase( i==145 ); /* INITIALLY */
179956 testcase( i==146 ); /* ALL */
179957 testcase( i==147 ); /* PRIMARY */
179958 *pType = aKWCode[i];
179959 break;
179960 }
179961 return n;
179962}
179963SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char *z, int n){
179964 int id = TK_ID60;
179965 if( n>=2 ) keywordCode((char*)z, n, &id);
179966 return id;
179967}
179968#define SQLITE_N_KEYWORD147 147
179969SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
179970 if( i<0 || i>=SQLITE_N_KEYWORD147 ) return SQLITE_ERROR1;
179971 i++;
179972 *pzName = zKWText + aKWOffset[i];
179973 *pnName = aKWLen[i];
179974 return SQLITE_OK0;
179975}
179976SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD147; }
179977SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
179978 return TK_ID60!=sqlite3KeywordCode((const u8*)zName, nName);
179979}
179980
179981/************** End of keywordhash.h *****************************************/
179982/************** Continuing where we left off in tokenize.c *******************/
179983
179984
179985/*
179986** If X is a character that can be used in an identifier then
179987** IdChar(X) will be true. Otherwise it is false.
179988**
179989** For ASCII, any character with the high-order bit set is
179990** allowed in an identifier. For 7-bit characters,
179991** sqlite3IsIdChar[X] must be 1.
179992**
179993** For EBCDIC, the rules are more complex but have the same
179994** end result.
179995**
179996** Ticket #1066. the SQL standard does not allow '$' in the
179997** middle of identifiers. But many SQL implementations do.
179998** SQLite will allow '$' in identifiers for compatibility.
179999** But the feature is undocumented.
180000*/
180001#ifdef SQLITE_ASCII1
180002#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
180003#endif
180004#ifdef SQLITE_EBCDIC
180005SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[] = {
180006/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
180007 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
180008 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
180009 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
180010 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
180011 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
180012 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
180013 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
180014 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
180015 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
180016 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
180017 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
180018 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
180019};
180020#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
180021#endif
180022
180023/* Make the IdChar function accessible from ctime.c and alter.c */
180024SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8 c){ return IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0); }
180025
180026#ifndef SQLITE_OMIT_WINDOWFUNC
180027/*
180028** Return the id of the next token in string (*pz). Before returning, set
180029** (*pz) to point to the byte following the parsed token.
180030*/
180031static int getToken(const unsigned char **pz){
180032 const unsigned char *z = *pz;
180033 int t; /* Token type to return */
180034 do {
180035 z += sqlite3GetToken(z, &t);
180036 }while( t==TK_SPACE184 );
180037 if( t==TK_ID60
180038 || t==TK_STRING118
180039 || t==TK_JOIN_KW119
180040 || t==TK_WINDOW165
180041 || t==TK_OVER166
180042 || sqlite3ParserFallback(t)==TK_ID60
180043 ){
180044 t = TK_ID60;
180045 }
180046 *pz = z;
180047 return t;
180048}
180049
180050/*
180051** The following three functions are called immediately after the tokenizer
180052** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
180053** whether the token should be treated as a keyword or an SQL identifier.
180054** This cannot be handled by the usual lemon %fallback method, due to
180055** the ambiguity in some constructions. e.g.
180056**
180057** SELECT sum(x) OVER ...
180058**
180059** In the above, "OVER" might be a keyword, or it might be an alias for the
180060** sum(x) expression. If a "%fallback ID OVER" directive were added to
180061** grammar, then SQLite would always treat "OVER" as an alias, making it
180062** impossible to call a window-function without a FILTER clause.
180063**
180064** WINDOW is treated as a keyword if:
180065**
180066** * the following token is an identifier, or a keyword that can fallback
180067** to being an identifier, and
180068** * the token after than one is TK_AS.
180069**
180070** OVER is a keyword if:
180071**
180072** * the previous token was TK_RP, and
180073** * the next token is either TK_LP or an identifier.
180074**
180075** FILTER is a keyword if:
180076**
180077** * the previous token was TK_RP, and
180078** * the next token is TK_LP.
180079*/
180080static int analyzeWindowKeyword(const unsigned char *z){
180081 int t;
180082 t = getToken(&z);
180083 if( t!=TK_ID60 ) return TK_ID60;
180084 t = getToken(&z);
180085 if( t!=TK_AS24 ) return TK_ID60;
180086 return TK_WINDOW165;
180087}
180088static int analyzeOverKeyword(const unsigned char *z, int lastToken){
180089 if( lastToken==TK_RP23 ){
180090 int t = getToken(&z);
180091 if( t==TK_LP22 || t==TK_ID60 ) return TK_OVER166;
180092 }
180093 return TK_ID60;
180094}
180095static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
180096 if( lastToken==TK_RP23 && getToken(&z)==TK_LP22 ){
180097 return TK_FILTER167;
180098 }
180099 return TK_ID60;
180100}
180101#endif /* SQLITE_OMIT_WINDOWFUNC */
180102
180103/*
180104** Return the length (in bytes) of the token that begins at z[0].
180105** Store the token type in *tokenType before returning.
180106*/
180107SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *z, int *tokenType){
180108 int i, c;
180109 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
180110 ** of the token. See the comment on the CC_ defines
180111 ** above. */
180112 case CC_SPACE7: {
180113 testcase( z[0]==' ' );
180114 testcase( z[0]=='\t' );
180115 testcase( z[0]=='\n' );
180116 testcase( z[0]=='\f' );
180117 testcase( z[0]=='\r' );
180118 for(i=1; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
180119 *tokenType = TK_SPACE184;
180120 return i;
180121 }
180122 case CC_MINUS11: {
180123 if( z[1]=='-' ){
180124 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
180125 *tokenType = TK_SPACE184; /* IMP: R-22934-25134 */
180126 return i;
180127 }else if( z[1]=='>' ){
180128 *tokenType = TK_PTR113;
180129 return 2 + (z[2]=='>');
180130 }
180131 *tokenType = TK_MINUS108;
180132 return 1;
180133 }
180134 case CC_LP17: {
180135 *tokenType = TK_LP22;
180136 return 1;
180137 }
180138 case CC_RP18: {
180139 *tokenType = TK_RP23;
180140 return 1;
180141 }
180142 case CC_SEMI19: {
180143 *tokenType = TK_SEMI1;
180144 return 1;
180145 }
180146 case CC_PLUS20: {
180147 *tokenType = TK_PLUS107;
180148 return 1;
180149 }
180150 case CC_STAR21: {
180151 *tokenType = TK_STAR109;
180152 return 1;
180153 }
180154 case CC_SLASH16: {
180155 if( z[1]!='*' || z[2]==0 ){
180156 *tokenType = TK_SLASH110;
180157 return 1;
180158 }
180159 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
180160 if( c ) i++;
180161 *tokenType = TK_SPACE184; /* IMP: R-22934-25134 */
180162 return i;
180163 }
180164 case CC_PERCENT22: {
180165 *tokenType = TK_REM111;
180166 return 1;
180167 }
180168 case CC_EQ14: {
180169 *tokenType = TK_EQ54;
180170 return 1 + (z[1]=='=');
180171 }
180172 case CC_LT12: {
180173 if( (c=z[1])=='=' ){
180174 *tokenType = TK_LE56;
180175 return 2;
180176 }else if( c=='>' ){
180177 *tokenType = TK_NE53;
180178 return 2;
180179 }else if( c=='<' ){
180180 *tokenType = TK_LSHIFT105;
180181 return 2;
180182 }else{
180183 *tokenType = TK_LT57;
180184 return 1;
180185 }
180186 }
180187 case CC_GT13: {
180188 if( (c=z[1])=='=' ){
180189 *tokenType = TK_GE58;
180190 return 2;
180191 }else if( c=='>' ){
180192 *tokenType = TK_RSHIFT106;
180193 return 2;
180194 }else{
180195 *tokenType = TK_GT55;
180196 return 1;
180197 }
180198 }
180199 case CC_BANG15: {
180200 if( z[1]!='=' ){
180201 *tokenType = TK_ILLEGAL185;
180202 return 1;
180203 }else{
180204 *tokenType = TK_NE53;
180205 return 2;
180206 }
180207 }
180208 case CC_PIPE10: {
180209 if( z[1]!='|' ){
180210 *tokenType = TK_BITOR104;
180211 return 1;
180212 }else{
180213 *tokenType = TK_CONCAT112;
180214 return 2;
180215 }
180216 }
180217 case CC_COMMA23: {
180218 *tokenType = TK_COMMA25;
180219 return 1;
180220 }
180221 case CC_AND24: {
180222 *tokenType = TK_BITAND103;
180223 return 1;
180224 }
180225 case CC_TILDA25: {
180226 *tokenType = TK_BITNOT115;
180227 return 1;
180228 }
180229 case CC_QUOTE8: {
180230 int delim = z[0];
180231 testcase( delim=='`' );
180232 testcase( delim=='\'' );
180233 testcase( delim=='"' );
180234 for(i=1; (c=z[i])!=0; i++){
180235 if( c==delim ){
180236 if( z[i+1]==delim ){
180237 i++;
180238 }else{
180239 break;
180240 }
180241 }
180242 }
180243 if( c=='\'' ){
180244 *tokenType = TK_STRING118;
180245 return i+1;
180246 }else if( c!=0 ){
180247 *tokenType = TK_ID60;
180248 return i+1;
180249 }else{
180250 *tokenType = TK_ILLEGAL185;
180251 return i;
180252 }
180253 }
180254 case CC_DOT26: {
180255#ifndef SQLITE_OMIT_FLOATING_POINT
180256 if( !sqlite3Isdigit(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x04) )
180257#endif
180258 {
180259 *tokenType = TK_DOT142;
180260 return 1;
180261 }
180262 /* If the next character is a digit, this is a floating point
180263 ** number that begins with ".". Fall thru into the next case */
180264 /* no break */ deliberate_fall_through__attribute__((fallthrough));
180265 }
180266 case CC_DIGIT3: {
180267 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
180268 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
180269 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
180270 testcase( z[0]=='9' ); testcase( z[0]=='.' );
180271 *tokenType = TK_INTEGER156;
180272#ifndef SQLITE_OMIT_HEX_INTEGER
180273 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2])(sqlite3CtypeMap[(unsigned char)(z[2])]&0x08) ){
180274 for(i=3; 1; i++){
180275 if( sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08)==0 ){
180276 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
180277 *tokenType = TK_QNUMBER183;
180278 }else{
180279 break;
180280 }
180281 }
180282 }
180283 }else
180284#endif
180285 {
180286 for(i=0; 1; i++){
180287 if( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04)==0 ){
180288 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
180289 *tokenType = TK_QNUMBER183;
180290 }else{
180291 break;
180292 }
180293 }
180294 }
180295#ifndef SQLITE_OMIT_FLOATING_POINT
180296 if( z[i]=='.' ){
180297 if( *tokenType==TK_INTEGER156 ) *tokenType = TK_FLOAT154;
180298 for(i++; 1; i++){
180299 if( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04)==0 ){
180300 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
180301 *tokenType = TK_QNUMBER183;
180302 }else{
180303 break;
180304 }
180305 }
180306 }
180307 }
180308 if( (z[i]=='e' || z[i]=='E') &&
180309 ( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04)
180310 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x04))
180311 )
180312 ){
180313 if( *tokenType==TK_INTEGER156 ) *tokenType = TK_FLOAT154;
180314 for(i+=2; 1; i++){
180315 if( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04)==0 ){
180316 if( z[i]==SQLITE_DIGIT_SEPARATOR'_' ){
180317 *tokenType = TK_QNUMBER183;
180318 }else{
180319 break;
180320 }
180321 }
180322 }
180323 }
180324#endif
180325 }
180326 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
180327 *tokenType = TK_ILLEGAL185;
180328 i++;
180329 }
180330 return i;
180331 }
180332 case CC_QUOTE29: {
180333 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
180334 *tokenType = c==']' ? TK_ID60 : TK_ILLEGAL185;
180335 return i;
180336 }
180337 case CC_VARNUM6: {
180338 *tokenType = TK_VARIABLE157;
180339 for(i=1; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){}
180340 return i;
180341 }
180342 case CC_DOLLAR4:
180343 case CC_VARALPHA5: {
180344 int n = 0;
180345 testcase( z[0]=='$' ); testcase( z[0]=='@' );
180346 testcase( z[0]==':' ); testcase( z[0]=='#' );
180347 *tokenType = TK_VARIABLE157;
180348 for(i=1; (c=z[i])!=0; i++){
180349 if( IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0) ){
180350 n++;
180351#ifndef SQLITE_OMIT_TCL_VARIABLE
180352 }else if( c=='(' && n>0 ){
180353 do{
180354 i++;
180355 }while( (c=z[i])!=0 && !sqlite3Isspace(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x01) && c!=')' );
180356 if( c==')' ){
180357 i++;
180358 }else{
180359 *tokenType = TK_ILLEGAL185;
180360 }
180361 break;
180362 }else if( c==':' && z[i+1]==':' ){
180363 i++;
180364#endif
180365 }else{
180366 break;
180367 }
180368 }
180369 if( n==0 ) *tokenType = TK_ILLEGAL185;
180370 return i;
180371 }
180372 case CC_KYWD01: {
180373 if( aiClass[z[1]]>CC_KYWD2 ){ i = 1; break; }
180374 for(i=2; aiClass[z[i]]<=CC_KYWD2; i++){}
180375 if( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
180376 /* This token started out using characters that can appear in keywords,
180377 ** but z[i] is a character not allowed within keywords, so this must
180378 ** be an identifier instead */
180379 i++;
180380 break;
180381 }
180382 *tokenType = TK_ID60;
180383 return keywordCode((char*)z, i, tokenType);
180384 }
180385 case CC_X0: {
180386#ifndef SQLITE_OMIT_BLOB_LITERAL
180387 testcase( z[0]=='x' ); testcase( z[0]=='X' );
180388 if( z[1]=='\'' ){
180389 *tokenType = TK_BLOB155;
180390 for(i=2; sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08); i++){}
180391 if( z[i]!='\'' || i%2 ){
180392 *tokenType = TK_ILLEGAL185;
180393 while( z[i] && z[i]!='\'' ){ i++; }
180394 }
180395 if( z[i] ) i++;
180396 return i;
180397 }
180398#endif
180399 /* If it is not a BLOB literal, then it must be an ID, since no
180400 ** SQL keywords start with the letter 'x'. Fall through */
180401 /* no break */ deliberate_fall_through__attribute__((fallthrough));
180402 }
180403 case CC_KYWD2:
180404 case CC_ID27: {
180405 i = 1;
180406 break;
180407 }
180408 case CC_BOM30: {
180409 if( z[1]==0xbb && z[2]==0xbf ){
180410 *tokenType = TK_SPACE184;
180411 return 3;
180412 }
180413 i = 1;
180414 break;
180415 }
180416 case CC_NUL29: {
180417 *tokenType = TK_ILLEGAL185;
180418 return 0;
180419 }
180420 default: {
180421 *tokenType = TK_ILLEGAL185;
180422 return 1;
180423 }
180424 }
180425 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){ i++; }
180426 *tokenType = TK_ID60;
180427 return i;
180428}
180429
180430/*
180431** Run the parser on the given SQL string.
180432*/
180433SQLITE_PRIVATEstatic int sqlite3RunParser(Parse *pParse, const char *zSql){
180434 int nErr = 0; /* Number of errors encountered */
180435 void *pEngine; /* The LEMON-generated LALR(1) parser */
180436 int n = 0; /* Length of the next token token */
180437 int tokenType; /* type of the next token */
180438 int lastTokenParsed = -1; /* type of the previous token */
180439 sqlite3 *db = pParse->db; /* The database connection */
180440 int mxSqlLen; /* Max length of an SQL string */
180441 Parse *pParentParse = 0; /* Outer parse context, if any */
180442#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
180443 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
180444#endif
180445 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
180446
180447 assert( zSql!=0 )((void) (0));
180448 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
180449 if( db->nVdbeActive==0 ){
180450 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
180451 }
180452 pParse->rc = SQLITE_OK0;
180453 pParse->zTail = zSql;
180454#ifdef SQLITE_DEBUG
180455 if( db->flags & SQLITE_ParserTrace ){
180456 printf("parser: [[[%s]]]\n", zSql);
180457 sqlite3ParserTrace(stdoutstdout, "parser: ");
180458 }else{
180459 sqlite3ParserTrace(0, 0);
180460 }
180461#endif
180462#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
180463 pEngine = &sEngine;
180464 sqlite3ParserInit(pEngine, pParse);
180465#else
180466 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
180467 if( pEngine==0 ){
180468 sqlite3OomFault(db);
180469 return SQLITE_NOMEM_BKPT7;
180470 }
180471#endif
180472 assert( pParse->pNewTable==0 )((void) (0));
180473 assert( pParse->pNewTrigger==0 )((void) (0));
180474 assert( pParse->nVar==0 )((void) (0));
180475 assert( pParse->pVList==0 )((void) (0));
180476 pParentParse = db->pParse;
180477 db->pParse = pParse;
180478 while( 1 ){
180479 n = sqlite3GetToken((u8*)zSql, &tokenType);
180480 mxSqlLen -= n;
180481 if( mxSqlLen<0 ){
180482 pParse->rc = SQLITE_TOOBIG18;
180483 pParse->nErr++;
180484 break;
180485 }
180486#ifndef SQLITE_OMIT_WINDOWFUNC
180487 if( tokenType>=TK_WINDOW165 ){
180488 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER((void) (0))
180489 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW((void) (0))
180490 || tokenType==TK_QNUMBER((void) (0))
180491 )((void) (0));
180492#else
180493 if( tokenType>=TK_SPACE184 ){
180494 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL((void) (0))
180495 || tokenType==TK_QNUMBER((void) (0))
180496 )((void) (0));
180497#endif /* SQLITE_OMIT_WINDOWFUNC */
180498 if( AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0) ){
180499 pParse->rc = SQLITE_INTERRUPT9;
180500 pParse->nErr++;
180501 break;
180502 }
180503 if( tokenType==TK_SPACE184 ){
180504 zSql += n;
180505 continue;
180506 }
180507 if( zSql[0]==0 ){
180508 /* Upon reaching the end of input, call the parser two more times
180509 ** with tokens TK_SEMI and 0, in that order. */
180510 if( lastTokenParsed==TK_SEMI1 ){
180511 tokenType = 0;
180512 }else if( lastTokenParsed==0 ){
180513 break;
180514 }else{
180515 tokenType = TK_SEMI1;
180516 }
180517 n = 0;
180518#ifndef SQLITE_OMIT_WINDOWFUNC
180519 }else if( tokenType==TK_WINDOW165 ){
180520 assert( n==6 )((void) (0));
180521 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
180522 }else if( tokenType==TK_OVER166 ){
180523 assert( n==4 )((void) (0));
180524 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
180525 }else if( tokenType==TK_FILTER167 ){
180526 assert( n==6 )((void) (0));
180527 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
180528#endif /* SQLITE_OMIT_WINDOWFUNC */
180529 }else if( tokenType!=TK_QNUMBER183 ){
180530 Token x;
180531 x.z = zSql;
180532 x.n = n;
180533 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x);
180534 break;
180535 }
180536 }
180537 pParse->sLastToken.z = zSql;
180538 pParse->sLastToken.n = n;
180539 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
180540 lastTokenParsed = tokenType;
180541 zSql += n;
180542 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom )((void) (0));
180543 if( pParse->rc!=SQLITE_OK0 ) break;
180544 }
180545 assert( nErr==0 )((void) (0));
180546#ifdef YYTRACKMAXSTACKDEPTH
180547 sqlite3_mutex_enter(sqlite3MallocMutex());
180548 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK6,
180549 sqlite3ParserStackPeak(pEngine)
180550 );
180551 sqlite3_mutex_leave(sqlite3MallocMutex());
180552#endif /* YYDEBUG */
180553#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
180554 sqlite3ParserFinalize(pEngine);
180555#else
180556 sqlite3ParserFree(pEngine, sqlite3_free);
180557#endif
180558 if( db->mallocFailed ){
180559 pParse->rc = SQLITE_NOMEM_BKPT7;
180560 }
180561 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK0 && pParse->rc!=SQLITE_DONE101) ){
180562 if( pParse->zErrMsg==0 ){
180563 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
180564 }
180565 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
180566 nErr++;
180567 }
180568 pParse->zTail = zSql;
180569#ifndef SQLITE_OMIT_VIRTUALTABLE
180570 sqlite3_free(pParse->apVtabLock);
180571#endif
180572
180573 if( pParse->pNewTable && !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
180574 /* If the pParse->declareVtab flag is set, do not delete any table
180575 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
180576 ** will take responsibility for freeing the Table structure.
180577 */
180578 sqlite3DeleteTable(db, pParse->pNewTable);
180579 }
180580 if( pParse->pNewTrigger && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
180581 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
180582 }
180583 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
180584 db->pParse = pParentParse;
180585 assert( nErr==0 || pParse->rc!=SQLITE_OK )((void) (0));
180586 return nErr;
180587}
180588
180589
180590#ifdef SQLITE_ENABLE_NORMALIZE
180591/*
180592** Insert a single space character into pStr if the current string
180593** ends with an identifier
180594*/
180595static void addSpaceSeparator(sqlite3_str *pStr){
180596 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
180597 sqlite3_str_append(pStr, " ", 1);
180598 }
180599}
180600
180601/*
180602** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
180603** the normalization in space obtained from sqlite3DbMalloc(). Or return
180604** NULL if anything goes wrong or if zSql is NULL.
180605*/
180606SQLITE_PRIVATEstatic char *sqlite3Normalize(
180607 Vdbe *pVdbe, /* VM being reprepared */
180608 const char *zSql /* The original SQL string */
180609){
180610 sqlite3 *db; /* The database connection */
180611 int i; /* Next unread byte of zSql[] */
180612 int n; /* length of current token */
180613 int tokenType; /* type of current token */
180614 int prevType = 0; /* Previous non-whitespace token */
180615 int nParen; /* Number of nested levels of parentheses */
180616 int iStartIN; /* Start of RHS of IN operator in z[] */
180617 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
180618 u32 j; /* Bytes of normalized SQL generated so far */
180619 sqlite3_str *pStr; /* The normalized SQL string under construction */
180620
180621 db = sqlite3VdbeDb(pVdbe);
180622 tokenType = -1;
180623 nParen = iStartIN = nParenAtIN = 0;
180624 pStr = sqlite3_str_new(db);
180625 assert( pStr!=0 )((void) (0)); /* sqlite3_str_new() never returns NULL */
180626 for(i=0; zSql[i] && pStr->accError==0; i+=n){
180627 if( tokenType!=TK_SPACE184 ){
180628 prevType = tokenType;
180629 }
180630 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
180631 if( NEVER(n<=0)(n<=0) ) break;
180632 switch( tokenType ){
180633 case TK_SPACE184: {
180634 break;
180635 }
180636 case TK_NULL122: {
180637 if( prevType==TK_IS45 || prevType==TK_NOT19 ){
180638 sqlite3_str_append(pStr, " NULL", 5);
180639 break;
180640 }
180641 /* Fall through */
180642 }
180643 case TK_STRING118:
180644 case TK_INTEGER156:
180645 case TK_FLOAT154:
180646 case TK_VARIABLE157:
180647 case TK_BLOB155: {
180648 sqlite3_str_append(pStr, "?", 1);
180649 break;
180650 }
180651 case TK_LP22: {
180652 nParen++;
180653 if( prevType==TK_IN50 ){
180654 iStartIN = pStr->nChar;
180655 nParenAtIN = nParen;
180656 }
180657 sqlite3_str_append(pStr, "(", 1);
180658 break;
180659 }
180660 case TK_RP23: {
180661 if( iStartIN>0 && nParen==nParenAtIN ){
180662 assert( pStr->nChar>=(u32)iStartIN )((void) (0));
180663 pStr->nChar = iStartIN+1;
180664 sqlite3_str_append(pStr, "?,?,?", 5);
180665 iStartIN = 0;
180666 }
180667 nParen--;
180668 sqlite3_str_append(pStr, ")", 1);
180669 break;
180670 }
180671 case TK_ID60: {
180672 iStartIN = 0;
180673 j = pStr->nChar;
180674 if( sqlite3Isquote(zSql[i])(sqlite3CtypeMap[(unsigned char)(zSql[i])]&0x80) ){
180675 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
180676 int nId;
180677 int eType = 0;
180678 if( zId==0 ) break;
180679 sqlite3Dequote(zId);
180680 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
180681 sqlite3_str_append(pStr, "?", 1);
180682 sqlite3DbFree(db, zId);
180683 break;
180684 }
180685 nId = sqlite3Strlen30(zId);
180686 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID60 ){
180687 addSpaceSeparator(pStr);
180688 sqlite3_str_append(pStr, zId, nId);
180689 }else{
180690 sqlite3_str_appendf(pStr, "\"%w\"", zId);
180691 }
180692 sqlite3DbFree(db, zId);
180693 }else{
180694 addSpaceSeparator(pStr);
180695 sqlite3_str_append(pStr, zSql+i, n);
180696 }
180697 while( j<pStr->nChar ){
180698 pStr->zText[j] = sqlite3Tolower(pStr->zText[j])(sqlite3UpperToLower[(unsigned char)(pStr->zText[j])]);
180699 j++;
180700 }
180701 break;
180702 }
180703 case TK_SELECT139: {
180704 iStartIN = 0;
180705 /* fall through */
180706 }
180707 default: {
180708 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
180709 j = pStr->nChar;
180710 sqlite3_str_append(pStr, zSql+i, n);
180711 while( j<pStr->nChar ){
180712 pStr->zText[j] = sqlite3Toupper(pStr->zText[j])((pStr->zText[j])&~(sqlite3CtypeMap[(unsigned char)(pStr
->zText[j])]&0x20))
;
180713 j++;
180714 }
180715 break;
180716 }
180717 }
180718 }
180719 if( tokenType!=TK_SEMI1 ) sqlite3_str_append(pStr, ";", 1);
180720 return sqlite3_str_finish(pStr);
180721}
180722#endif /* SQLITE_ENABLE_NORMALIZE */
180723
180724/************** End of tokenize.c ********************************************/
180725/************** Begin file complete.c ****************************************/
180726/*
180727** 2001 September 15
180728**
180729** The author disclaims copyright to this source code. In place of
180730** a legal notice, here is a blessing:
180731**
180732** May you do good and not evil.
180733** May you find forgiveness for yourself and forgive others.
180734** May you share freely, never taking more than you give.
180735**
180736*************************************************************************
180737** An tokenizer for SQL
180738**
180739** This file contains C code that implements the sqlite3_complete() API.
180740** This code used to be part of the tokenizer.c source file. But by
180741** separating it out, the code will be automatically omitted from
180742** static links that do not use it.
180743*/
180744/* #include "sqliteInt.h" */
180745#ifndef SQLITE_OMIT_COMPLETE
180746
180747/*
180748** This is defined in tokenize.c. We just have to import the definition.
180749*/
180750#ifndef SQLITE_AMALGAMATION1
180751#ifdef SQLITE_ASCII1
180752#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
180753#endif
180754#ifdef SQLITE_EBCDIC
180755SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[];
180756#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
180757#endif
180758#endif /* SQLITE_AMALGAMATION */
180759
180760
180761/*
180762** Token types used by the sqlite3_complete() routine. See the header
180763** comments on that procedure for additional information.
180764*/
180765#define tkSEMI0 0
180766#define tkWS1 1
180767#define tkOTHER2 2
180768#ifndef SQLITE_OMIT_TRIGGER
180769#define tkEXPLAIN3 3
180770#define tkCREATE4 4
180771#define tkTEMP5 5
180772#define tkTRIGGER6 6
180773#define tkEND7 7
180774#endif
180775
180776/*
180777** Return TRUE if the given SQL string ends in a semicolon.
180778**
180779** Special handling is require for CREATE TRIGGER statements.
180780** Whenever the CREATE TRIGGER keywords are seen, the statement
180781** must end with ";END;".
180782**
180783** This implementation uses a state machine with 8 states:
180784**
180785** (0) INVALID We have not yet seen a non-whitespace character.
180786**
180787** (1) START At the beginning or end of an SQL statement. This routine
180788** returns 1 if it ends in the START state and 0 if it ends
180789** in any other state.
180790**
180791** (2) NORMAL We are in the middle of statement which ends with a single
180792** semicolon.
180793**
180794** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
180795** a statement.
180796**
180797** (4) CREATE The keyword CREATE has been seen at the beginning of a
180798** statement, possibly preceded by EXPLAIN and/or followed by
180799** TEMP or TEMPORARY
180800**
180801** (5) TRIGGER We are in the middle of a trigger definition that must be
180802** ended by a semicolon, the keyword END, and another semicolon.
180803**
180804** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
180805** the end of a trigger definition.
180806**
180807** (7) END We've seen the ";END" of the ";END;" that occurs at the end
180808** of a trigger definition.
180809**
180810** Transitions between states above are determined by tokens extracted
180811** from the input. The following tokens are significant:
180812**
180813** (0) tkSEMI A semicolon.
180814** (1) tkWS Whitespace.
180815** (2) tkOTHER Any other SQL token.
180816** (3) tkEXPLAIN The "explain" keyword.
180817** (4) tkCREATE The "create" keyword.
180818** (5) tkTEMP The "temp" or "temporary" keyword.
180819** (6) tkTRIGGER The "trigger" keyword.
180820** (7) tkEND The "end" keyword.
180821**
180822** Whitespace never causes a state transition and is always ignored.
180823** This means that a SQL string of all whitespace is invalid.
180824**
180825** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
180826** to recognize the end of a trigger can be omitted. All we have to do
180827** is look for a semicolon that is not part of an string or comment.
180828*/
180829SQLITE_API int sqlite3_complete(const char *zSql){
180830 u8 state = 0; /* Current state, using numbers defined in header comment */
180831 u8 token; /* Value of the next token */
180832
180833#ifndef SQLITE_OMIT_TRIGGER
180834 /* A complex statement machine used to detect the end of a CREATE TRIGGER
180835 ** statement. This is the normal case.
180836 */
180837 static const u8 trans[8][8] = {
180838 /* Token: */
180839 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
180840 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
180841 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
180842 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
180843 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
180844 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
180845 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
180846 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
180847 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
180848 };
180849#else
180850 /* If triggers are not supported by this compile then the statement machine
180851 ** used to detect the end of a statement is much simpler
180852 */
180853 static const u8 trans[3][3] = {
180854 /* Token: */
180855 /* State: ** SEMI WS OTHER */
180856 /* 0 INVALID: */ { 1, 0, 2, },
180857 /* 1 START: */ { 1, 1, 2, },
180858 /* 2 NORMAL: */ { 1, 2, 2, },
180859 };
180860#endif /* SQLITE_OMIT_TRIGGER */
180861
180862#ifdef SQLITE_ENABLE_API_ARMOR1
180863 if( zSql==0 ){
180864 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(180864);
180865 return 0;
180866 }
180867#endif
180868
180869 while( *zSql ){
180870 switch( *zSql ){
180871 case ';': { /* A semicolon */
180872 token = tkSEMI0;
180873 break;
180874 }
180875 case ' ':
180876 case '\r':
180877 case '\t':
180878 case '\n':
180879 case '\f': { /* White space is ignored */
180880 token = tkWS1;
180881 break;
180882 }
180883 case '/': { /* C-style comments */
180884 if( zSql[1]!='*' ){
180885 token = tkOTHER2;
180886 break;
180887 }
180888 zSql += 2;
180889 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
180890 if( zSql[0]==0 ) return 0;
180891 zSql++;
180892 token = tkWS1;
180893 break;
180894 }
180895 case '-': { /* SQL-style comments from "--" to end of line */
180896 if( zSql[1]!='-' ){
180897 token = tkOTHER2;
180898 break;
180899 }
180900 while( *zSql && *zSql!='\n' ){ zSql++; }
180901 if( *zSql==0 ) return state==1;
180902 token = tkWS1;
180903 break;
180904 }
180905 case '[': { /* Microsoft-style identifiers in [...] */
180906 zSql++;
180907 while( *zSql && *zSql!=']' ){ zSql++; }
180908 if( *zSql==0 ) return 0;
180909 token = tkOTHER2;
180910 break;
180911 }
180912 case '`': /* Grave-accent quoted symbols used by MySQL */
180913 case '"': /* single- and double-quoted strings */
180914 case '\'': {
180915 int c = *zSql;
180916 zSql++;
180917 while( *zSql && *zSql!=c ){ zSql++; }
180918 if( *zSql==0 ) return 0;
180919 token = tkOTHER2;
180920 break;
180921 }
180922 default: {
180923#ifdef SQLITE_EBCDIC
180924 unsigned char c;
180925#endif
180926 if( IdChar((u8)*zSql)((sqlite3CtypeMap[(unsigned char)(u8)*zSql]&0x46)!=0) ){
180927 /* Keywords and unquoted identifiers */
180928 int nId;
180929 for(nId=1; IdChar(zSql[nId])((sqlite3CtypeMap[(unsigned char)zSql[nId]]&0x46)!=0); nId++){}
180930#ifdef SQLITE_OMIT_TRIGGER
180931 token = tkOTHER2;
180932#else
180933 switch( *zSql ){
180934 case 'c': case 'C': {
180935 if( nId==6 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "create", 6)==0 ){
180936 token = tkCREATE4;
180937 }else{
180938 token = tkOTHER2;
180939 }
180940 break;
180941 }
180942 case 't': case 'T': {
180943 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "trigger", 7)==0 ){
180944 token = tkTRIGGER6;
180945 }else if( nId==4 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temp", 4)==0 ){
180946 token = tkTEMP5;
180947 }else if( nId==9 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temporary", 9)==0 ){
180948 token = tkTEMP5;
180949 }else{
180950 token = tkOTHER2;
180951 }
180952 break;
180953 }
180954 case 'e': case 'E': {
180955 if( nId==3 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "end", 3)==0 ){
180956 token = tkEND7;
180957 }else
180958#ifndef SQLITE_OMIT_EXPLAIN
180959 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "explain", 7)==0 ){
180960 token = tkEXPLAIN3;
180961 }else
180962#endif
180963 {
180964 token = tkOTHER2;
180965 }
180966 break;
180967 }
180968 default: {
180969 token = tkOTHER2;
180970 break;
180971 }
180972 }
180973#endif /* SQLITE_OMIT_TRIGGER */
180974 zSql += nId-1;
180975 }else{
180976 /* Operators and special symbols */
180977 token = tkOTHER2;
180978 }
180979 break;
180980 }
180981 }
180982 state = trans[state][token];
180983 zSql++;
180984 }
180985 return state==1;
180986}
180987
180988#ifndef SQLITE_OMIT_UTF16
180989/*
180990** This routine is the same as the sqlite3_complete() routine described
180991** above, except that the parameter is required to be UTF-16 encoded, not
180992** UTF-8.
180993*/
180994SQLITE_API int sqlite3_complete16(const void *zSql){
180995 sqlite3_value *pVal;
180996 char const *zSql8;
180997 int rc;
180998
180999#ifndef SQLITE_OMIT_AUTOINIT
181000 rc = sqlite3_initialize();
181001 if( rc ) return rc;
181002#endif
181003 pVal = sqlite3ValueNew(0);
181004 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
181005 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF81);
181006 if( zSql8 ){
181007 rc = sqlite3_complete(zSql8);
181008 }else{
181009 rc = SQLITE_NOMEM_BKPT7;
181010 }
181011 sqlite3ValueFree(pVal);
181012 return rc & 0xff;
181013}
181014#endif /* SQLITE_OMIT_UTF16 */
181015#endif /* SQLITE_OMIT_COMPLETE */
181016
181017/************** End of complete.c ********************************************/
181018/************** Begin file main.c ********************************************/
181019/*
181020** 2001 September 15
181021**
181022** The author disclaims copyright to this source code. In place of
181023** a legal notice, here is a blessing:
181024**
181025** May you do good and not evil.
181026** May you find forgiveness for yourself and forgive others.
181027** May you share freely, never taking more than you give.
181028**
181029*************************************************************************
181030** Main file for the SQLite library. The routines in this file
181031** implement the programmer interface to the library. Routines in
181032** other files are for internal use by SQLite and should not be
181033** accessed by users of the library.
181034*/
181035/* #include "sqliteInt.h" */
181036
181037#ifdef SQLITE_ENABLE_FTS31
181038/************** Include fts3.h in the middle of main.c ***********************/
181039/************** Begin file fts3.h ********************************************/
181040/*
181041** 2006 Oct 10
181042**
181043** The author disclaims copyright to this source code. In place of
181044** a legal notice, here is a blessing:
181045**
181046** May you do good and not evil.
181047** May you find forgiveness for yourself and forgive others.
181048** May you share freely, never taking more than you give.
181049**
181050******************************************************************************
181051**
181052** This header file is used by programs that want to link against the
181053** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
181054*/
181055/* #include "sqlite3.h" */
181056
181057#if 0
181058extern "C" {
181059#endif /* __cplusplus */
181060
181061SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db);
181062
181063#if 0
181064} /* extern "C" */
181065#endif /* __cplusplus */
181066
181067/************** End of fts3.h ************************************************/
181068/************** Continuing where we left off in main.c ***********************/
181069#endif
181070#ifdef SQLITE_ENABLE_RTREE1
181071/************** Include rtree.h in the middle of main.c **********************/
181072/************** Begin file rtree.h *******************************************/
181073/*
181074** 2008 May 26
181075**
181076** The author disclaims copyright to this source code. In place of
181077** a legal notice, here is a blessing:
181078**
181079** May you do good and not evil.
181080** May you find forgiveness for yourself and forgive others.
181081** May you share freely, never taking more than you give.
181082**
181083******************************************************************************
181084**
181085** This header file is used by programs that want to link against the
181086** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
181087*/
181088/* #include "sqlite3.h" */
181089
181090#ifdef SQLITE_OMIT_VIRTUALTABLE
181091# undef SQLITE_ENABLE_RTREE1
181092#endif
181093
181094#if 0
181095extern "C" {
181096#endif /* __cplusplus */
181097
181098SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db);
181099
181100#if 0
181101} /* extern "C" */
181102#endif /* __cplusplus */
181103
181104/************** End of rtree.h ***********************************************/
181105/************** Continuing where we left off in main.c ***********************/
181106#endif
181107#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
181108/************** Include sqliteicu.h in the middle of main.c ******************/
181109/************** Begin file sqliteicu.h ***************************************/
181110/*
181111** 2008 May 26
181112**
181113** The author disclaims copyright to this source code. In place of
181114** a legal notice, here is a blessing:
181115**
181116** May you do good and not evil.
181117** May you find forgiveness for yourself and forgive others.
181118** May you share freely, never taking more than you give.
181119**
181120******************************************************************************
181121**
181122** This header file is used by programs that want to link against the
181123** ICU extension. All it does is declare the sqlite3IcuInit() interface.
181124*/
181125/* #include "sqlite3.h" */
181126
181127#if 0
181128extern "C" {
181129#endif /* __cplusplus */
181130
181131SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db);
181132
181133#if 0
181134} /* extern "C" */
181135#endif /* __cplusplus */
181136
181137/************** End of sqliteicu.h *******************************************/
181138/************** Continuing where we left off in main.c ***********************/
181139#endif
181140
181141/*
181142** This is an extension initializer that is a no-op and always
181143** succeeds, except that it fails if the fault-simulation is set
181144** to 500.
181145*/
181146static int sqlite3TestExtInit(sqlite3 *db){
181147 (void)db;
181148 return sqlite3FaultSim(500);
181149}
181150
181151
181152/*
181153** Forward declarations of external module initializer functions
181154** for modules that need them.
181155*/
181156#ifdef SQLITE_ENABLE_FTS51
181157SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3*);
181158#endif
181159#ifdef SQLITE_ENABLE_STMTVTAB
181160SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3*);
181161#endif
181162#ifdef SQLITE_EXTRA_AUTOEXT
181163int SQLITE_EXTRA_AUTOEXT(sqlite3*);
181164#endif
181165/*
181166** An array of pointers to extension initializer functions for
181167** built-in extensions.
181168*/
181169static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
181170#ifdef SQLITE_ENABLE_FTS31
181171 sqlite3Fts3Init,
181172#endif
181173#ifdef SQLITE_ENABLE_FTS51
181174 sqlite3Fts5Init,
181175#endif
181176#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
181177 sqlite3IcuInit,
181178#endif
181179#ifdef SQLITE_ENABLE_RTREE1
181180 sqlite3RtreeInit,
181181#endif
181182#ifdef SQLITE_ENABLE_DBPAGE_VTAB
181183 sqlite3DbpageRegister,
181184#endif
181185#ifdef SQLITE_ENABLE_DBSTAT_VTAB1
181186 sqlite3DbstatRegister,
181187#endif
181188 sqlite3TestExtInit,
181189#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
181190 sqlite3JsonTableFunctions,
181191#endif
181192#ifdef SQLITE_ENABLE_STMTVTAB
181193 sqlite3StmtVtabInit,
181194#endif
181195#ifdef SQLITE_ENABLE_BYTECODE_VTAB
181196 sqlite3VdbeBytecodeVtabInit,
181197#endif
181198#ifdef SQLITE_EXTRA_AUTOEXT
181199 SQLITE_EXTRA_AUTOEXT,
181200#endif
181201};
181202
181203#ifndef SQLITE_AMALGAMATION1
181204/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
181205** contains the text of SQLITE_VERSION macro.
181206*/
181207SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.47.1";
181208#endif
181209
181210/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
181211** a pointer to the to the sqlite3_version[] string constant.
181212*/
181213SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
181214
181215/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
181216** pointer to a string constant whose value is the same as the
181217** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
181218** an edited copy of the amalgamation, then the last four characters of
181219** the hash might be different from SQLITE_SOURCE_ID.
181220*/
181221/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
181222
181223/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
181224** returns an integer equal to SQLITE_VERSION_NUMBER.
181225*/
181226SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER3047001; }
181227
181228/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
181229** zero if and only if SQLite was compiled with mutexing code omitted due to
181230** the SQLITE_THREADSAFE compile-time option being set to 0.
181231*/
181232SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE2; }
181233
181234/*
181235** When compiling the test fixture or with debugging enabled (on Win32),
181236** this variable being set to non-zero will cause OSTRACE macros to emit
181237** extra diagnostic information.
181238*/
181239#ifdef SQLITE_HAVE_OS_TRACE
181240# ifndef SQLITE_DEBUG_OS_TRACE
181241# define SQLITE_DEBUG_OS_TRACE 0
181242# endif
181243 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
181244#endif
181245
181246#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
181247/*
181248** If the following function pointer is not NULL and if
181249** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
181250** I/O active are written using this function. These messages
181251** are intended for debugging activity only.
181252*/
181253SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
181254#endif
181255
181256/*
181257** If the following global variable points to a string which is the
181258** name of a directory, then that directory will be used to store
181259** temporary files.
181260**
181261** See also the "PRAGMA temp_store_directory" SQL command.
181262*/
181263SQLITE_API char *sqlite3_temp_directory = 0;
181264
181265/*
181266** If the following global variable points to a string which is the
181267** name of a directory, then that directory will be used to store
181268** all database files specified with a relative pathname.
181269**
181270** See also the "PRAGMA data_store_directory" SQL command.
181271*/
181272SQLITE_API char *sqlite3_data_directory = 0;
181273
181274/*
181275** Initialize SQLite.
181276**
181277** This routine must be called to initialize the memory allocation,
181278** VFS, and mutex subsystems prior to doing any serious work with
181279** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
181280** this routine will be called automatically by key routines such as
181281** sqlite3_open().
181282**
181283** This routine is a no-op except on its very first call for the process,
181284** or for the first call after a call to sqlite3_shutdown.
181285**
181286** The first thread to call this routine runs the initialization to
181287** completion. If subsequent threads call this routine before the first
181288** thread has finished the initialization process, then the subsequent
181289** threads must block until the first thread finishes with the initialization.
181290**
181291** The first thread might call this routine recursively. Recursive
181292** calls to this routine should not block, of course. Otherwise the
181293** initialization process would never complete.
181294**
181295** Let X be the first thread to enter this routine. Let Y be some other
181296** thread. Then while the initial invocation of this routine by X is
181297** incomplete, it is required that:
181298**
181299** * Calls to this routine from Y must block until the outer-most
181300** call by X completes.
181301**
181302** * Recursive calls to this routine from thread X return immediately
181303** without blocking.
181304*/
181305SQLITE_API int sqlite3_initialize(void){
181306 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )sqlite3_mutex *pMainMtx; /* The main static mutex */
181307 int rc; /* Result code */
181308#ifdef SQLITE_EXTRA_INIT
181309 int bRunExtraInit = 0; /* Extra initialization needed */
181310#endif
181311
181312#ifdef SQLITE_OMIT_WSD
181313 rc = sqlite3_wsd_init(4096, 24);
181314 if( rc!=SQLITE_OK0 ){
181315 return rc;
181316 }
181317#endif
181318
181319 /* If the following assert() fails on some obscure processor/compiler
181320 ** combination, the work-around is to set the correct pointer
181321 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
181322 assert( SQLITE_PTRSIZE==sizeof(char*) )((void) (0));
181323
181324 /* If SQLite is already completely initialized, then this call
181325 ** to sqlite3_initialize() should be a no-op. But the initialization
181326 ** must be complete. So isInit must not be set until the very end
181327 ** of this routine.
181328 */
181329 if( sqlite3GlobalConfigsqlite3Config.isInit ){
181330 sqlite3MemoryBarrier();
181331 return SQLITE_OK0;
181332 }
181333
181334 /* Make sure the mutex subsystem is initialized. If unable to
181335 ** initialize the mutex subsystem, return early with the error.
181336 ** If the system is so sick that we are unable to allocate a mutex,
181337 ** there is not much SQLite is going to be able to do.
181338 **
181339 ** The mutex subsystem must take care of serializing its own
181340 ** initialization.
181341 */
181342 rc = sqlite3MutexInit();
181343 if( rc ) return rc;
181344
181345 /* Initialize the malloc() system and the recursive pInitMutex mutex.
181346 ** This operation is protected by the STATIC_MAIN mutex. Note that
181347 ** MutexAlloc() is called for a static mutex prior to initializing the
181348 ** malloc subsystem - this implies that the allocation of a static
181349 ** mutex must not require support from the malloc subsystem.
181350 */
181351 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )pMainMtx = sqlite3MutexAlloc(2);
181352 sqlite3_mutex_enter(pMainMtx);
181353 sqlite3GlobalConfigsqlite3Config.isMutexInit = 1;
181354 if( !sqlite3GlobalConfigsqlite3Config.isMallocInit ){
181355 rc = sqlite3MallocInit();
181356 }
181357 if( rc==SQLITE_OK0 ){
181358 sqlite3GlobalConfigsqlite3Config.isMallocInit = 1;
181359 if( !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
181360 sqlite3GlobalConfigsqlite3Config.pInitMutex =
181361 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
181362 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex && !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
181363 rc = SQLITE_NOMEM_BKPT7;
181364 }
181365 }
181366 }
181367 if( rc==SQLITE_OK0 ){
181368 sqlite3GlobalConfigsqlite3Config.nRefInitMutex++;
181369 }
181370 sqlite3_mutex_leave(pMainMtx);
181371
181372 /* If rc is not SQLITE_OK at this point, then either the malloc
181373 ** subsystem could not be initialized or the system failed to allocate
181374 ** the pInitMutex mutex. Return an error in either case. */
181375 if( rc!=SQLITE_OK0 ){
181376 return rc;
181377 }
181378
181379 /* Do the rest of the initialization under the recursive mutex so
181380 ** that we will be able to handle recursive calls into
181381 ** sqlite3_initialize(). The recursive calls normally come through
181382 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
181383 ** recursive calls might also be possible.
181384 **
181385 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
181386 ** to the xInit method, so the xInit method need not be threadsafe.
181387 **
181388 ** The following mutex is what serializes access to the appdef pcache xInit
181389 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
181390 ** call to sqlite3PcacheInitialize().
181391 */
181392 sqlite3_mutex_enter(sqlite3GlobalConfigsqlite3Config.pInitMutex);
181393 if( sqlite3GlobalConfigsqlite3Config.isInit==0 && sqlite3GlobalConfigsqlite3Config.inProgress==0 ){
181394 sqlite3GlobalConfigsqlite3Config.inProgress = 1;
181395#ifdef SQLITE_ENABLE_SQLLOG
181396 {
181397 extern void sqlite3_init_sqllog(void);
181398 sqlite3_init_sqllog();
181399 }
181400#endif
181401 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
181402 sqlite3RegisterBuiltinFunctions();
181403 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit==0 ){
181404 rc = sqlite3PcacheInitialize();
181405 }
181406 if( rc==SQLITE_OK0 ){
181407 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 1;
181408 rc = sqlite3OsInit();
181409 }
181410#ifndef SQLITE_OMIT_DESERIALIZE
181411 if( rc==SQLITE_OK0 ){
181412 rc = sqlite3MemdbInit();
181413 }
181414#endif
181415 if( rc==SQLITE_OK0 ){
181416 sqlite3PCacheBufferSetup( sqlite3GlobalConfigsqlite3Config.pPage,
181417 sqlite3GlobalConfigsqlite3Config.szPage, sqlite3GlobalConfigsqlite3Config.nPage);
181418 sqlite3MemoryBarrier();
181419 sqlite3GlobalConfigsqlite3Config.isInit = 1;
181420#ifdef SQLITE_EXTRA_INIT
181421 bRunExtraInit = 1;
181422#endif
181423 }
181424 sqlite3GlobalConfigsqlite3Config.inProgress = 0;
181425 }
181426 sqlite3_mutex_leave(sqlite3GlobalConfigsqlite3Config.pInitMutex);
181427
181428 /* Go back under the static mutex and clean up the recursive
181429 ** mutex to prevent a resource leak.
181430 */
181431 sqlite3_mutex_enter(pMainMtx);
181432 sqlite3GlobalConfigsqlite3Config.nRefInitMutex--;
181433 if( sqlite3GlobalConfigsqlite3Config.nRefInitMutex<=0 ){
181434 assert( sqlite3GlobalConfig.nRefInitMutex==0 )((void) (0));
181435 sqlite3_mutex_free(sqlite3GlobalConfigsqlite3Config.pInitMutex);
181436 sqlite3GlobalConfigsqlite3Config.pInitMutex = 0;
181437 }
181438 sqlite3_mutex_leave(pMainMtx);
181439
181440 /* The following is just a sanity check to make sure SQLite has
181441 ** been compiled correctly. It is important to run this code, but
181442 ** we don't want to run it too often and soak up CPU cycles for no
181443 ** reason. So we run it once during initialization.
181444 */
181445#ifndef NDEBUG1
181446#ifndef SQLITE_OMIT_FLOATING_POINT
181447 /* This section of code's only "output" is via assert() statements. */
181448 if( rc==SQLITE_OK0 ){
181449 u64 x = (((u64)1)<<63)-1;
181450 double y;
181451 assert(sizeof(x)==8)((void) (0));
181452 assert(sizeof(x)==sizeof(y))((void) (0));
181453 memcpy(&y, &x, 8);
181454 assert( sqlite3IsNaN(y) )((void) (0));
181455 }
181456#endif
181457#endif
181458
181459 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
181460 ** compile-time option.
181461 */
181462#ifdef SQLITE_EXTRA_INIT
181463 if( bRunExtraInit ){
181464 int SQLITE_EXTRA_INIT(const char*);
181465 rc = SQLITE_EXTRA_INIT(0);
181466 }
181467#endif
181468 return rc;
181469}
181470
181471/*
181472** Undo the effects of sqlite3_initialize(). Must not be called while
181473** there are outstanding database connections or memory allocations or
181474** while any part of SQLite is otherwise in use in any thread. This
181475** routine is not threadsafe. But it is safe to invoke this routine
181476** on when SQLite is already shut down. If SQLite is already shut down
181477** when this routine is invoked, then this routine is a harmless no-op.
181478*/
181479SQLITE_API int sqlite3_shutdown(void){
181480#ifdef SQLITE_OMIT_WSD
181481 int rc = sqlite3_wsd_init(4096, 24);
181482 if( rc!=SQLITE_OK0 ){
181483 return rc;
181484 }
181485#endif
181486
181487 if( sqlite3GlobalConfigsqlite3Config.isInit ){
181488#ifdef SQLITE_EXTRA_SHUTDOWN
181489 void SQLITE_EXTRA_SHUTDOWN(void);
181490 SQLITE_EXTRA_SHUTDOWN();
181491#endif
181492 sqlite3_os_end();
181493 sqlite3_reset_auto_extension();
181494 sqlite3GlobalConfigsqlite3Config.isInit = 0;
181495 }
181496 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit ){
181497 sqlite3PcacheShutdown();
181498 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 0;
181499 }
181500 if( sqlite3GlobalConfigsqlite3Config.isMallocInit ){
181501 sqlite3MallocEnd();
181502 sqlite3GlobalConfigsqlite3Config.isMallocInit = 0;
181503
181504#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
181505 /* The heap subsystem has now been shutdown and these values are supposed
181506 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
181507 ** which would rely on that heap subsystem; therefore, make sure these
181508 ** values cannot refer to heap memory that was just invalidated when the
181509 ** heap subsystem was shutdown. This is only done if the current call to
181510 ** this function resulted in the heap subsystem actually being shutdown.
181511 */
181512 sqlite3_data_directory = 0;
181513 sqlite3_temp_directory = 0;
181514#endif
181515 }
181516 if( sqlite3GlobalConfigsqlite3Config.isMutexInit ){
181517 sqlite3MutexEnd();
181518 sqlite3GlobalConfigsqlite3Config.isMutexInit = 0;
181519 }
181520
181521 return SQLITE_OK0;
181522}
181523
181524/*
181525** This API allows applications to modify the global configuration of
181526** the SQLite library at run-time.
181527**
181528** This routine should only be called when there are no outstanding
181529** database connections or memory allocations. This routine is not
181530** threadsafe. Failure to heed these warnings can lead to unpredictable
181531** behavior.
181532*/
181533SQLITE_API int sqlite3_config(int op, ...){
181534 va_list ap;
181535 int rc = SQLITE_OK0;
181536
181537 /* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while
181538 ** the SQLite library is in use. Except, a few selected opcodes
181539 ** are allowed.
181540 */
181541 if( sqlite3GlobalConfigsqlite3Config.isInit ){
181542 static const u64 mAnytimeConfigOption = 0
181543 | MASKBIT64( SQLITE_CONFIG_LOG )(((u64)1)<<(16))
181544 | MASKBIT64( SQLITE_CONFIG_PCACHE_HDRSZ )(((u64)1)<<(24))
181545 ;
181546 if( op<0 || op>63 || (MASKBIT64(op)(((u64)1)<<(op)) & mAnytimeConfigOption)==0 ){
181547 return SQLITE_MISUSE_BKPTsqlite3MisuseError(181547);
181548 }
181549 testcase( op==SQLITE_CONFIG_LOG );
181550 testcase( op==SQLITE_CONFIG_PCACHE_HDRSZ );
181551 }
181552
181553 va_start(ap, op)__builtin_va_start(ap, op);
181554 switch( op ){
181555
181556 /* Mutex configuration options are only available in a threadsafe
181557 ** compile.
181558 */
181559#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-54466-46756 */
181560 case SQLITE_CONFIG_SINGLETHREAD1: {
181561 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
181562 ** Single-thread. */
181563 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 0; /* Disable mutex on core */
181564 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
181565 break;
181566 }
181567#endif
181568#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-20520-54086 */
181569 case SQLITE_CONFIG_MULTITHREAD2: {
181570 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
181571 ** Multi-thread. */
181572 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
181573 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
181574 break;
181575 }
181576#endif
181577#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-59593-21810 */
181578 case SQLITE_CONFIG_SERIALIZED3: {
181579 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
181580 ** Serialized. */
181581 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
181582 sqlite3GlobalConfigsqlite3Config.bFullMutex = 1; /* Enable mutex on connections */
181583 break;
181584 }
181585#endif
181586#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-63666-48755 */
181587 case SQLITE_CONFIG_MUTEX10: {
181588 /* Specify an alternative mutex implementation */
181589 sqlite3GlobalConfigsqlite3Config.mutex = *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*);
181590 break;
181591 }
181592#endif
181593#if defined(SQLITE_THREADSAFE2) && SQLITE_THREADSAFE2>0 /* IMP: R-14450-37597 */
181594 case SQLITE_CONFIG_GETMUTEX11: {
181595 /* Retrieve the current mutex implementation */
181596 *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfigsqlite3Config.mutex;
181597 break;
181598 }
181599#endif
181600
181601 case SQLITE_CONFIG_MALLOC4: {
181602 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
181603 ** single argument which is a pointer to an instance of the
181604 ** sqlite3_mem_methods structure. The argument specifies alternative
181605 ** low-level memory allocation routines to be used in place of the memory
181606 ** allocation routines built into SQLite. */
181607 sqlite3GlobalConfigsqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*);
181608 break;
181609 }
181610 case SQLITE_CONFIG_GETMALLOC5: {
181611 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
181612 ** single argument which is a pointer to an instance of the
181613 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
181614 ** filled with the currently defined memory allocation routines. */
181615 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ) sqlite3MemSetDefault();
181616 *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfigsqlite3Config.m;
181617 break;
181618 }
181619 case SQLITE_CONFIG_MEMSTATUS9: {
181620 assert( !sqlite3GlobalConfig.isInit )((void) (0)); /* Cannot change at runtime */
181621 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
181622 ** single argument of type int, interpreted as a boolean, which enables
181623 ** or disables the collection of memory allocation statistics. */
181624 sqlite3GlobalConfigsqlite3Config.bMemstat = va_arg(ap, int)__builtin_va_arg(ap, int);
181625 break;
181626 }
181627 case SQLITE_CONFIG_SMALL_MALLOC27: {
181628 sqlite3GlobalConfigsqlite3Config.bSmallMalloc = va_arg(ap, int)__builtin_va_arg(ap, int);
181629 break;
181630 }
181631 case SQLITE_CONFIG_PAGECACHE7: {
181632 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
181633 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
181634 ** the size of each page cache line (sz), and the number of cache lines
181635 ** (N). */
181636 sqlite3GlobalConfigsqlite3Config.pPage = va_arg(ap, void*)__builtin_va_arg(ap, void*);
181637 sqlite3GlobalConfigsqlite3Config.szPage = va_arg(ap, int)__builtin_va_arg(ap, int);
181638 sqlite3GlobalConfigsqlite3Config.nPage = va_arg(ap, int)__builtin_va_arg(ap, int);
181639 break;
181640 }
181641 case SQLITE_CONFIG_PCACHE_HDRSZ24: {
181642 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
181643 ** a single parameter which is a pointer to an integer and writes into
181644 ** that integer the number of extra bytes per page required for each page
181645 ** in SQLITE_CONFIG_PAGECACHE. */
181646 *va_arg(ap, int*)__builtin_va_arg(ap, int*) =
181647 sqlite3HeaderSizeBtree() +
181648 sqlite3HeaderSizePcache() +
181649 sqlite3HeaderSizePcache1();
181650 break;
181651 }
181652
181653 case SQLITE_CONFIG_PCACHE14: {
181654 /* no-op */
181655 break;
181656 }
181657 case SQLITE_CONFIG_GETPCACHE15: {
181658 /* now an error */
181659 rc = SQLITE_ERROR1;
181660 break;
181661 }
181662
181663 case SQLITE_CONFIG_PCACHE218: {
181664 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
181665 ** single argument which is a pointer to an sqlite3_pcache_methods2
181666 ** object. This object specifies the interface to a custom page cache
181667 ** implementation. */
181668 sqlite3GlobalConfigsqlite3Config.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*);
181669 break;
181670 }
181671 case SQLITE_CONFIG_GETPCACHE219: {
181672 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
181673 ** single argument which is a pointer to an sqlite3_pcache_methods2
181674 ** object. SQLite copies of the current page cache implementation into
181675 ** that object. */
181676 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
181677 sqlite3PCacheSetDefault();
181678 }
181679 *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfigsqlite3Config.pcache2;
181680 break;
181681 }
181682
181683/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
181684** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
181685** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
181686#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
181687 case SQLITE_CONFIG_HEAP8: {
181688 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
181689 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
181690 ** number of bytes in the memory buffer, and the minimum allocation size.
181691 */
181692 sqlite3GlobalConfigsqlite3Config.pHeap = va_arg(ap, void*)__builtin_va_arg(ap, void*);
181693 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
181694 sqlite3GlobalConfigsqlite3Config.mnReq = va_arg(ap, int)__builtin_va_arg(ap, int);
181695
181696 if( sqlite3GlobalConfigsqlite3Config.mnReq<1 ){
181697 sqlite3GlobalConfigsqlite3Config.mnReq = 1;
181698 }else if( sqlite3GlobalConfigsqlite3Config.mnReq>(1<<12) ){
181699 /* cap min request size at 2^12 */
181700 sqlite3GlobalConfigsqlite3Config.mnReq = (1<<12);
181701 }
181702
181703 if( sqlite3GlobalConfigsqlite3Config.pHeap==0 ){
181704 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
181705 ** is NULL, then SQLite reverts to using its default memory allocator
181706 ** (the system malloc() implementation), undoing any prior invocation of
181707 ** SQLITE_CONFIG_MALLOC.
181708 **
181709 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
181710 ** revert to its default implementation when sqlite3_initialize() is run
181711 */
181712 memset(&sqlite3GlobalConfigsqlite3Config.m, 0, sizeof(sqlite3GlobalConfigsqlite3Config.m));
181713 }else{
181714 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
181715 ** alternative memory allocator is engaged to handle all of SQLites
181716 ** memory allocation needs. */
181717#ifdef SQLITE_ENABLE_MEMSYS3
181718 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys3();
181719#endif
181720#ifdef SQLITE_ENABLE_MEMSYS5
181721 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys5();
181722#endif
181723 }
181724 break;
181725 }
181726#endif
181727
181728 case SQLITE_CONFIG_LOOKASIDE13: {
181729 sqlite3GlobalConfigsqlite3Config.szLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
181730 sqlite3GlobalConfigsqlite3Config.nLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
181731 break;
181732 }
181733
181734 /* Record a pointer to the logger function and its first argument.
181735 ** The default is NULL. Logging is disabled if the function pointer is
181736 ** NULL.
181737 */
181738 case SQLITE_CONFIG_LOG16: {
181739 /* MSVC is picky about pulling func ptrs from va lists.
181740 ** http://support.microsoft.com/kb/47961
181741 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
181742 */
181743 typedef void(*LOGFUNC_t)(void*,int,const char*);
181744 LOGFUNC_t xLog = va_arg(ap, LOGFUNC_t)__builtin_va_arg(ap, LOGFUNC_t);
181745 void *pLogArg = va_arg(ap, void*)__builtin_va_arg(ap, void*);
181746 AtomicStore(&sqlite3GlobalConfig.xLog, xLog)__atomic_store_n((&sqlite3Config.xLog),(xLog),0);
181747 AtomicStore(&sqlite3GlobalConfig.pLogArg, pLogArg)__atomic_store_n((&sqlite3Config.pLogArg),(pLogArg),0);
181748 break;
181749 }
181750
181751 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
181752 ** can be changed at start-time using the
181753 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
181754 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
181755 */
181756 case SQLITE_CONFIG_URI17: {
181757 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
181758 ** argument of type int. If non-zero, then URI handling is globally
181759 ** enabled. If the parameter is zero, then URI handling is globally
181760 ** disabled. */
181761 int bOpenUri = va_arg(ap, int)__builtin_va_arg(ap, int);
181762 AtomicStore(&sqlite3GlobalConfig.bOpenUri, bOpenUri)__atomic_store_n((&sqlite3Config.bOpenUri),(bOpenUri),0);
181763 break;
181764 }
181765
181766 case SQLITE_CONFIG_COVERING_INDEX_SCAN20: {
181767 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
181768 ** option takes a single integer argument which is interpreted as a
181769 ** boolean in order to enable or disable the use of covering indices for
181770 ** full table scans in the query optimizer. */
181771 sqlite3GlobalConfigsqlite3Config.bUseCis = va_arg(ap, int)__builtin_va_arg(ap, int);
181772 break;
181773 }
181774
181775#ifdef SQLITE_ENABLE_SQLLOG
181776 case SQLITE_CONFIG_SQLLOG21: {
181777 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
181778 sqlite3GlobalConfigsqlite3Config.xSqllog = va_arg(ap, SQLLOGFUNC_t)__builtin_va_arg(ap, SQLLOGFUNC_t);
181779 sqlite3GlobalConfigsqlite3Config.pSqllogArg = va_arg(ap, void *)__builtin_va_arg(ap, void *);
181780 break;
181781 }
181782#endif
181783
181784 case SQLITE_CONFIG_MMAP_SIZE22: {
181785 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
181786 ** integer (sqlite3_int64) values that are the default mmap size limit
181787 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
181788 ** mmap size limit. */
181789 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
181790 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
181791 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
181792 ** negative, then that argument is changed to its compile-time default.
181793 **
181794 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
181795 ** silently truncated if necessary so that it does not exceed the
181796 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
181797 ** compile-time option.
181798 */
181799 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE20971520 ){
181800 mxMmap = SQLITE_MAX_MMAP_SIZE20971520;
181801 }
181802 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE0;
181803 if( szMmap>mxMmap) szMmap = mxMmap;
181804 sqlite3GlobalConfigsqlite3Config.mxMmap = mxMmap;
181805 sqlite3GlobalConfigsqlite3Config.szMmap = szMmap;
181806 break;
181807 }
181808
181809#if SQLITE_OS_WIN0 && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
181810 case SQLITE_CONFIG_WIN32_HEAPSIZE23: {
181811 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
181812 ** unsigned integer value that specifies the maximum size of the created
181813 ** heap. */
181814 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
181815 break;
181816 }
181817#endif
181818
181819 case SQLITE_CONFIG_PMASZ25: {
181820 sqlite3GlobalConfigsqlite3Config.szPma = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
181821 break;
181822 }
181823
181824 case SQLITE_CONFIG_STMTJRNL_SPILL26: {
181825 sqlite3GlobalConfigsqlite3Config.nStmtSpill = va_arg(ap, int)__builtin_va_arg(ap, int);
181826 break;
181827 }
181828
181829#ifdef SQLITE_ENABLE_SORTER_REFERENCES
181830 case SQLITE_CONFIG_SORTERREF_SIZE28: {
181831 int iVal = va_arg(ap, int)__builtin_va_arg(ap, int);
181832 if( iVal<0 ){
181833 iVal = SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff;
181834 }
181835 sqlite3GlobalConfigsqlite3Config.szSorterRef = (u32)iVal;
181836 break;
181837 }
181838#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
181839
181840#ifndef SQLITE_OMIT_DESERIALIZE
181841 case SQLITE_CONFIG_MEMDB_MAXSIZE29: {
181842 sqlite3GlobalConfigsqlite3Config.mxMemdbSize = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
181843 break;
181844 }
181845#endif /* SQLITE_OMIT_DESERIALIZE */
181846
181847 case SQLITE_CONFIG_ROWID_IN_VIEW30: {
181848 int *pVal = va_arg(ap,int*)__builtin_va_arg(ap, int*);
181849#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
181850 if( 0==*pVal ) sqlite3GlobalConfigsqlite3Config.mNoVisibleRowid = TF_NoVisibleRowid0x00000200;
181851 if( 1==*pVal ) sqlite3GlobalConfigsqlite3Config.mNoVisibleRowid = 0;
181852 *pVal = (sqlite3GlobalConfigsqlite3Config.mNoVisibleRowid==0);
181853#else
181854 *pVal = 0;
181855#endif
181856 break;
181857 }
181858
181859 default: {
181860 rc = SQLITE_ERROR1;
181861 break;
181862 }
181863 }
181864 va_end(ap)__builtin_va_end(ap);
181865 return rc;
181866}
181867
181868/*
181869** Set up the lookaside buffers for a database connection.
181870** Return SQLITE_OK on success.
181871** If lookaside is already active, return SQLITE_BUSY.
181872**
181873** The sz parameter is the number of bytes in each lookaside slot.
181874** The cnt parameter is the number of slots. If pStart is NULL the
181875** space for the lookaside memory is obtained from sqlite3_malloc().
181876** If pStart is not NULL then it is sz*cnt bytes of memory to use for
181877** the lookaside memory.
181878*/
181879static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
181880#ifndef SQLITE_OMIT_LOOKASIDE
181881 void *pStart;
181882 sqlite3_int64 szAlloc = sz*(sqlite3_int64)cnt;
181883 int nBig; /* Number of full-size slots */
181884 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
181885
181886 if( sqlite3LookasideUsed(db,0)>0 ){
181887 return SQLITE_BUSY5;
181888 }
181889 /* Free any existing lookaside buffer for this handle before
181890 ** allocating a new one so we don't have to have space for
181891 ** both at the same time.
181892 */
181893 if( db->lookaside.bMalloced ){
181894 sqlite3_free(db->lookaside.pStart);
181895 }
181896 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
181897 ** than a pointer to be useful.
181898 */
181899 sz = ROUNDDOWN8(sz)((sz)&~7); /* IMP: R-33038-09382 */
181900 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
181901 if( cnt<0 ) cnt = 0;
181902 if( sz==0 || cnt==0 ){
181903 sz = 0;
181904 pStart = 0;
181905 }else if( pBuf==0 ){
181906 sqlite3BeginBenignMalloc();
181907 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
181908 sqlite3EndBenignMalloc();
181909 if( pStart ) szAlloc = sqlite3MallocSize(pStart);
181910 }else{
181911 pStart = pBuf;
181912 }
181913#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
181914 if( sz>=LOOKASIDE_SMALL128*3 ){
181915 nBig = szAlloc/(3*LOOKASIDE_SMALL128+sz);
181916 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL128;
181917 }else if( sz>=LOOKASIDE_SMALL128*2 ){
181918 nBig = szAlloc/(LOOKASIDE_SMALL128+sz);
181919 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL128;
181920 }else
181921#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
181922 if( sz>0 ){
181923 nBig = szAlloc/sz;
181924 nSm = 0;
181925 }else{
181926 nBig = nSm = 0;
181927 }
181928 db->lookaside.pStart = pStart;
181929 db->lookaside.pInit = 0;
181930 db->lookaside.pFree = 0;
181931 db->lookaside.sz = (u16)sz;
181932 db->lookaside.szTrue = (u16)sz;
181933 if( pStart ){
181934 int i;
181935 LookasideSlot *p;
181936 assert( sz > (int)sizeof(LookasideSlot*) )((void) (0));
181937 p = (LookasideSlot*)pStart;
181938 for(i=0; i<nBig; i++){
181939 p->pNext = db->lookaside.pInit;
181940 db->lookaside.pInit = p;
181941 p = (LookasideSlot*)&((u8*)p)[sz];
181942 }
181943#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
181944 db->lookaside.pSmallInit = 0;
181945 db->lookaside.pSmallFree = 0;
181946 db->lookaside.pMiddle = p;
181947 for(i=0; i<nSm; i++){
181948 p->pNext = db->lookaside.pSmallInit;
181949 db->lookaside.pSmallInit = p;
181950 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL128];
181951 }
181952#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
181953 assert( ((uptr)p)<=szAlloc + (uptr)pStart )((void) (0));
181954 db->lookaside.pEnd = p;
181955 db->lookaside.bDisable = 0;
181956 db->lookaside.bMalloced = pBuf==0 ?1:0;
181957 db->lookaside.nSlot = nBig+nSm;
181958 }else{
181959 db->lookaside.pStart = 0;
181960#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
181961 db->lookaside.pSmallInit = 0;
181962 db->lookaside.pSmallFree = 0;
181963 db->lookaside.pMiddle = 0;
181964#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
181965 db->lookaside.pEnd = 0;
181966 db->lookaside.bDisable = 1;
181967 db->lookaside.sz = 0;
181968 db->lookaside.bMalloced = 0;
181969 db->lookaside.nSlot = 0;
181970 }
181971 db->lookaside.pTrueEnd = db->lookaside.pEnd;
181972 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
181973#endif /* SQLITE_OMIT_LOOKASIDE */
181974 return SQLITE_OK0;
181975}
181976
181977/*
181978** Return the mutex associated with a database connection.
181979*/
181980SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
181981#ifdef SQLITE_ENABLE_API_ARMOR1
181982 if( !sqlite3SafetyCheckOk(db) ){
181983 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(181983);
181984 return 0;
181985 }
181986#endif
181987 return db->mutex;
181988}
181989
181990/*
181991** Free up as much memory as we can from the given database
181992** connection.
181993*/
181994SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
181995 int i;
181996
181997#ifdef SQLITE_ENABLE_API_ARMOR1
181998 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(181998);
181999#endif
182000 sqlite3_mutex_enter(db->mutex);
182001 sqlite3BtreeEnterAll(db);
182002 for(i=0; i<db->nDb; i++){
182003 Btree *pBt = db->aDb[i].pBt;
182004 if( pBt ){
182005 Pager *pPager = sqlite3BtreePager(pBt);
182006 sqlite3PagerShrink(pPager);
182007 }
182008 }
182009 sqlite3BtreeLeaveAll(db);
182010 sqlite3_mutex_leave(db->mutex);
182011 return SQLITE_OK0;
182012}
182013
182014/*
182015** Flush any dirty pages in the pager-cache for any attached database
182016** to disk.
182017*/
182018SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
182019 int i;
182020 int rc = SQLITE_OK0;
182021 int bSeenBusy = 0;
182022
182023#ifdef SQLITE_ENABLE_API_ARMOR1
182024 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(182024);
182025#endif
182026 sqlite3_mutex_enter(db->mutex);
182027 sqlite3BtreeEnterAll(db);
182028 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
182029 Btree *pBt = db->aDb[i].pBt;
182030 if( pBt && sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE2 ){
182031 Pager *pPager = sqlite3BtreePager(pBt);
182032 rc = sqlite3PagerFlush(pPager);
182033 if( rc==SQLITE_BUSY5 ){
182034 bSeenBusy = 1;
182035 rc = SQLITE_OK0;
182036 }
182037 }
182038 }
182039 sqlite3BtreeLeaveAll(db);
182040 sqlite3_mutex_leave(db->mutex);
182041 return ((rc==SQLITE_OK0 && bSeenBusy) ? SQLITE_BUSY5 : rc);
182042}
182043
182044/*
182045** Configuration settings for an individual database connection
182046*/
182047SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
182048 va_list ap;
182049 int rc;
182050
182051#ifdef SQLITE_ENABLE_API_ARMOR1
182052 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(182052);
182053#endif
182054 sqlite3_mutex_enter(db->mutex);
182055 va_start(ap, op)__builtin_va_start(ap, op);
182056 switch( op ){
182057 case SQLITE_DBCONFIG_MAINDBNAME1000: {
182058 /* IMP: R-06824-28531 */
182059 /* IMP: R-36257-52125 */
182060 db->aDb[0].zDbSName = va_arg(ap,char*)__builtin_va_arg(ap, char*);
182061 rc = SQLITE_OK0;
182062 break;
182063 }
182064 case SQLITE_DBCONFIG_LOOKASIDE1001: {
182065 void *pBuf = va_arg(ap, void*)__builtin_va_arg(ap, void*); /* IMP: R-26835-10964 */
182066 int sz = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-47871-25994 */
182067 int cnt = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-04460-53386 */
182068 rc = setupLookaside(db, pBuf, sz, cnt);
182069 break;
182070 }
182071 default: {
182072 static const struct {
182073 int op; /* The opcode */
182074 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
182075 } aFlagOp[] = {
182076 { SQLITE_DBCONFIG_ENABLE_FKEY1002, SQLITE_ForeignKeys0x00004000 },
182077 { SQLITE_DBCONFIG_ENABLE_TRIGGER1003, SQLITE_EnableTrigger0x00040000 },
182078 { SQLITE_DBCONFIG_ENABLE_VIEW1015, SQLITE_EnableView0x80000000 },
182079 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004, SQLITE_Fts3Tokenizer0x00400000 },
182080 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005, SQLITE_LoadExtension0x00010000 },
182081 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006, SQLITE_NoCkptOnClose0x00000800 },
182082 { SQLITE_DBCONFIG_ENABLE_QPSG1007, SQLITE_EnableQPSG0x00800000 },
182083 { SQLITE_DBCONFIG_TRIGGER_EQP1008, SQLITE_TriggerEQP0x01000000 },
182084 { SQLITE_DBCONFIG_RESET_DATABASE1009, SQLITE_ResetDatabase0x02000000 },
182085 { SQLITE_DBCONFIG_DEFENSIVE1010, SQLITE_Defensive0x10000000 },
182086 { SQLITE_DBCONFIG_WRITABLE_SCHEMA1011, SQLITE_WriteSchema0x00000001|
182087 SQLITE_NoSchemaError0x08000000 },
182088 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012, SQLITE_LegacyAlter0x04000000 },
182089 { SQLITE_DBCONFIG_DQS_DDL1014, SQLITE_DqsDDL0x20000000 },
182090 { SQLITE_DBCONFIG_DQS_DML1013, SQLITE_DqsDML0x40000000 },
182091 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT1016, SQLITE_LegacyFileFmt0x00000002 },
182092 { SQLITE_DBCONFIG_TRUSTED_SCHEMA1017, SQLITE_TrustedSchema0x00000080 },
182093 { SQLITE_DBCONFIG_STMT_SCANSTATUS1018, SQLITE_StmtScanStatus0x00000400 },
182094 { SQLITE_DBCONFIG_REVERSE_SCANORDER1019, SQLITE_ReverseOrder0x00001000 },
182095 };
182096 unsigned int i;
182097 rc = SQLITE_ERROR1; /* IMP: R-42790-23372 */
182098 for(i=0; i<ArraySize(aFlagOp)((int)(sizeof(aFlagOp)/sizeof(aFlagOp[0]))); i++){
182099 if( aFlagOp[i].op==op ){
182100 int onoff = va_arg(ap, int)__builtin_va_arg(ap, int);
182101 int *pRes = va_arg(ap, int*)__builtin_va_arg(ap, int*);
182102 u64 oldFlags = db->flags;
182103 if( onoff>0 ){
182104 db->flags |= aFlagOp[i].mask;
182105 }else if( onoff==0 ){
182106 db->flags &= ~(u64)aFlagOp[i].mask;
182107 }
182108 if( oldFlags!=db->flags ){
182109 sqlite3ExpirePreparedStatements(db, 0);
182110 }
182111 if( pRes ){
182112 *pRes = (db->flags & aFlagOp[i].mask)!=0;
182113 }
182114 rc = SQLITE_OK0;
182115 break;
182116 }
182117 }
182118 break;
182119 }
182120 }
182121 va_end(ap)__builtin_va_end(ap);
182122 sqlite3_mutex_leave(db->mutex);
182123 return rc;
182124}
182125
182126/*
182127** This is the default collating function named "BINARY" which is always
182128** available.
182129*/
182130static int binCollFunc(
182131 void *NotUsed,
182132 int nKey1, const void *pKey1,
182133 int nKey2, const void *pKey2
182134){
182135 int rc, n;
182136 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
182137 n = nKey1<nKey2 ? nKey1 : nKey2;
182138 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
182139 ** strings byte by byte using the memcmp() function from the standard C
182140 ** library. */
182141 assert( pKey1 && pKey2 )((void) (0));
182142 rc = memcmp(pKey1, pKey2, n);
182143 if( rc==0 ){
182144 rc = nKey1 - nKey2;
182145 }
182146 return rc;
182147}
182148
182149/*
182150** This is the collating function named "RTRIM" which is always
182151** available. Ignore trailing spaces.
182152*/
182153static int rtrimCollFunc(
182154 void *pUser,
182155 int nKey1, const void *pKey1,
182156 int nKey2, const void *pKey2
182157){
182158 const u8 *pK1 = (const u8*)pKey1;
182159 const u8 *pK2 = (const u8*)pKey2;
182160 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
182161 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
182162 return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
182163}
182164
182165/*
182166** Return true if CollSeq is the default built-in BINARY.
182167*/
182168SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq *p){
182169 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 )((void) (0));
182170 return p==0 || p->xCmp==binCollFunc;
182171}
182172
182173/*
182174** Another built-in collating sequence: NOCASE.
182175**
182176** This collating sequence is intended to be used for "case independent
182177** comparison". SQLite's knowledge of upper and lower case equivalents
182178** extends only to the 26 characters used in the English language.
182179**
182180** At the moment there is only a UTF-8 implementation.
182181*/
182182static int nocaseCollatingFunc(
182183 void *NotUsed,
182184 int nKey1, const void *pKey1,
182185 int nKey2, const void *pKey2
182186){
182187 int r = sqlite3StrNICmpsqlite3_strnicmp(
182188 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
182189 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
182190 if( 0==r ){
182191 r = nKey1-nKey2;
182192 }
182193 return r;
182194}
182195
182196/*
182197** Return the ROWID of the most recent insert
182198*/
182199SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
182200#ifdef SQLITE_ENABLE_API_ARMOR1
182201 if( !sqlite3SafetyCheckOk(db) ){
182202 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182202);
182203 return 0;
182204 }
182205#endif
182206 return db->lastRowid;
182207}
182208
182209/*
182210** Set the value returned by the sqlite3_last_insert_rowid() API function.
182211*/
182212SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
182213#ifdef SQLITE_ENABLE_API_ARMOR1
182214 if( !sqlite3SafetyCheckOk(db) ){
182215 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182215);
182216 return;
182217 }
182218#endif
182219 sqlite3_mutex_enter(db->mutex);
182220 db->lastRowid = iRowid;
182221 sqlite3_mutex_leave(db->mutex);
182222}
182223
182224/*
182225** Return the number of changes in the most recent call to sqlite3_exec().
182226*/
182227SQLITE_API sqlite3_int64 sqlite3_changes64(sqlite3 *db){
182228#ifdef SQLITE_ENABLE_API_ARMOR1
182229 if( !sqlite3SafetyCheckOk(db) ){
182230 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182230);
182231 return 0;
182232 }
182233#endif
182234 return db->nChange;
182235}
182236SQLITE_API int sqlite3_changes(sqlite3 *db){
182237 return (int)sqlite3_changes64(db);
182238}
182239
182240/*
182241** Return the number of changes since the database handle was opened.
182242*/
182243SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){
182244#ifdef SQLITE_ENABLE_API_ARMOR1
182245 if( !sqlite3SafetyCheckOk(db) ){
182246 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182246);
182247 return 0;
182248 }
182249#endif
182250 return db->nTotalChange;
182251}
182252SQLITE_API int sqlite3_total_changes(sqlite3 *db){
182253 return (int)sqlite3_total_changes64(db);
182254}
182255
182256/*
182257** Close all open savepoints. This function only manipulates fields of the
182258** database handle object, it does not close any savepoints that may be open
182259** at the b-tree/pager level.
182260*/
182261SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *db){
182262 while( db->pSavepoint ){
182263 Savepoint *pTmp = db->pSavepoint;
182264 db->pSavepoint = pTmp->pNext;
182265 sqlite3DbFree(db, pTmp);
182266 }
182267 db->nSavepoint = 0;
182268 db->nStatement = 0;
182269 db->isTransactionSavepoint = 0;
182270}
182271
182272/*
182273** Invoke the destructor function associated with FuncDef p, if any. Except,
182274** if this is not the last copy of the function, do not invoke it. Multiple
182275** copies of a single function are created when create_function() is called
182276** with SQLITE_ANY as the encoding.
182277*/
182278static void functionDestroy(sqlite3 *db, FuncDef *p){
182279 FuncDestructor *pDestructor;
182280 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 )((void) (0));
182281 pDestructor = p->u.pDestructor;
182282 if( pDestructor ){
182283 pDestructor->nRef--;
182284 if( pDestructor->nRef==0 ){
182285 pDestructor->xDestroy(pDestructor->pUserData);
182286 sqlite3DbFree(db, pDestructor);
182287 }
182288 }
182289}
182290
182291/*
182292** Disconnect all sqlite3_vtab objects that belong to database connection
182293** db. This is called when db is being closed.
182294*/
182295static void disconnectAllVtab(sqlite3 *db){
182296#ifndef SQLITE_OMIT_VIRTUALTABLE
182297 int i;
182298 HashElem *p;
182299 sqlite3BtreeEnterAll(db);
182300 for(i=0; i<db->nDb; i++){
182301 Schema *pSchema = db->aDb[i].pSchema;
182302 if( pSchema ){
182303 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
182304 Table *pTab = (Table *)sqliteHashData(p)((p)->data);
182305 if( IsVirtual(pTab)((pTab)->eTabType==1) ) sqlite3VtabDisconnect(db, pTab);
182306 }
182307 }
182308 }
182309 for(p=sqliteHashFirst(&db->aModule)((&db->aModule)->first); p; p=sqliteHashNext(p)((p)->next)){
182310 Module *pMod = (Module *)sqliteHashData(p)((p)->data);
182311 if( pMod->pEpoTab ){
182312 sqlite3VtabDisconnect(db, pMod->pEpoTab);
182313 }
182314 }
182315 sqlite3VtabUnlockList(db);
182316 sqlite3BtreeLeaveAll(db);
182317#else
182318 UNUSED_PARAMETER(db)(void)(db);
182319#endif
182320}
182321
182322/*
182323** Return TRUE if database connection db has unfinalized prepared
182324** statements or unfinished sqlite3_backup objects.
182325*/
182326static int connectionIsBusy(sqlite3 *db){
182327 int j;
182328 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
182329 if( db->pVdbe ) return 1;
182330 for(j=0; j<db->nDb; j++){
182331 Btree *pBt = db->aDb[j].pBt;
182332 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
182333 }
182334 return 0;
182335}
182336
182337/*
182338** Close an existing SQLite database
182339*/
182340static int sqlite3Close(sqlite3 *db, int forceZombie){
182341 if( !db ){
182342 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
182343 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
182344 return SQLITE_OK0;
182345 }
182346 if( !sqlite3SafetyCheckSickOrOk(db) ){
182347 return SQLITE_MISUSE_BKPTsqlite3MisuseError(182347);
182348 }
182349 sqlite3_mutex_enter(db->mutex);
182350 if( db->mTrace & SQLITE_TRACE_CLOSE0x08 ){
182351 db->trace.xV2(SQLITE_TRACE_CLOSE0x08, db->pTraceArg, db, 0);
182352 }
182353
182354 /* Force xDisconnect calls on all virtual tables */
182355 disconnectAllVtab(db);
182356
182357 /* If a transaction is open, the disconnectAllVtab() call above
182358 ** will not have called the xDisconnect() method on any virtual
182359 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
182360 ** call will do so. We need to do this before the check for active
182361 ** SQL statements below, as the v-table implementation may be storing
182362 ** some prepared statements internally.
182363 */
182364 sqlite3VtabRollback(db);
182365
182366 /* Legacy behavior (sqlite3_close() behavior) is to return
182367 ** SQLITE_BUSY if the connection can not be closed immediately.
182368 */
182369 if( !forceZombie && connectionIsBusy(db) ){
182370 sqlite3ErrorWithMsg(db, SQLITE_BUSY5, "unable to close due to unfinalized "
182371 "statements or unfinished backups");
182372 sqlite3_mutex_leave(db->mutex);
182373 return SQLITE_BUSY5;
182374 }
182375
182376#ifdef SQLITE_ENABLE_SQLLOG
182377 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
182378 /* Closing the handle. Fourth parameter is passed the value 2. */
182379 sqlite3GlobalConfigsqlite3Config.xSqllog(sqlite3GlobalConfigsqlite3Config.pSqllogArg, db, 0, 2);
182380 }
182381#endif
182382
182383 while( db->pDbData ){
182384 DbClientData *p = db->pDbData;
182385 db->pDbData = p->pNext;
182386 assert( p->pData!=0 )((void) (0));
182387 if( p->xDestructor ) p->xDestructor(p->pData);
182388 sqlite3_free(p);
182389 }
182390
182391 /* Convert the connection into a zombie and then close it.
182392 */
182393 db->eOpenState = SQLITE_STATE_ZOMBIE0xa7;
182394 sqlite3LeaveMutexAndCloseZombie(db);
182395 return SQLITE_OK0;
182396}
182397
182398/*
182399** Return the transaction state for a single databse, or the maximum
182400** transaction state over all attached databases if zSchema is null.
182401*/
182402SQLITE_API int sqlite3_txn_state(sqlite3 *db, const char *zSchema){
182403 int iDb, nDb;
182404 int iTxn = -1;
182405#ifdef SQLITE_ENABLE_API_ARMOR1
182406 if( !sqlite3SafetyCheckOk(db) ){
182407 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182407);
182408 return -1;
182409 }
182410#endif
182411 sqlite3_mutex_enter(db->mutex);
182412 if( zSchema ){
182413 nDb = iDb = sqlite3FindDbName(db, zSchema);
182414 if( iDb<0 ) nDb--;
182415 }else{
182416 iDb = 0;
182417 nDb = db->nDb-1;
182418 }
182419 for(; iDb<=nDb; iDb++){
182420 Btree *pBt = db->aDb[iDb].pBt;
182421 int x = pBt!=0 ? sqlite3BtreeTxnState(pBt) : SQLITE_TXN_NONE0;
182422 if( x>iTxn ) iTxn = x;
182423 }
182424 sqlite3_mutex_leave(db->mutex);
182425 return iTxn;
182426}
182427
182428/*
182429** Two variations on the public interface for closing a database
182430** connection. The sqlite3_close() version returns SQLITE_BUSY and
182431** leaves the connection open if there are unfinalized prepared
182432** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
182433** version forces the connection to become a zombie if there are
182434** unclosed resources, and arranges for deallocation when the last
182435** prepare statement or sqlite3_backup closes.
182436*/
182437SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
182438SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
182439
182440
182441/*
182442** Close the mutex on database connection db.
182443**
182444** Furthermore, if database connection db is a zombie (meaning that there
182445** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
182446** every sqlite3_stmt has now been finalized and every sqlite3_backup has
182447** finished, then free all resources.
182448*/
182449SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
182450 HashElem *i; /* Hash table iterator */
182451 int j;
182452
182453 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
182454 ** or if the connection has not yet been closed by sqlite3_close_v2(),
182455 ** then just leave the mutex and return.
182456 */
182457 if( db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7 || connectionIsBusy(db) ){
182458 sqlite3_mutex_leave(db->mutex);
182459 return;
182460 }
182461
182462 /* If we reach this point, it means that the database connection has
182463 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
182464 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
182465 ** go ahead and free all resources.
182466 */
182467
182468 /* If a transaction is open, roll it back. This also ensures that if
182469 ** any database schemas have been modified by an uncommitted transaction
182470 ** they are reset. And that the required b-tree mutex is held to make
182471 ** the pager rollback and schema reset an atomic operation. */
182472 sqlite3RollbackAll(db, SQLITE_OK0);
182473
182474 /* Free any outstanding Savepoint structures. */
182475 sqlite3CloseSavepoints(db);
182476
182477 /* Close all database connections */
182478 for(j=0; j<db->nDb; j++){
182479 struct Db *pDb = &db->aDb[j];
182480 if( pDb->pBt ){
182481 sqlite3BtreeClose(pDb->pBt);
182482 pDb->pBt = 0;
182483 if( j!=1 ){
182484 pDb->pSchema = 0;
182485 }
182486 }
182487 }
182488 /* Clear the TEMP schema separately and last */
182489 if( db->aDb[1].pSchema ){
182490 sqlite3SchemaClear(db->aDb[1].pSchema);
182491 }
182492 sqlite3VtabUnlockList(db);
182493
182494 /* Free up the array of auxiliary databases */
182495 sqlite3CollapseDatabaseArray(db);
182496 assert( db->nDb<=2 )((void) (0));
182497 assert( db->aDb==db->aDbStatic )((void) (0));
182498
182499 /* Tell the code in notify.c that the connection no longer holds any
182500 ** locks and does not require any further unlock-notify callbacks.
182501 */
182502 sqlite3ConnectionClosed(db);
182503
182504 for(i=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); i; i=sqliteHashNext(i)((i)->next)){
182505 FuncDef *pNext, *p;
182506 p = sqliteHashData(i)((i)->data);
182507 do{
182508 functionDestroy(db, p);
182509 pNext = p->pNext;
182510 sqlite3DbFree(db, p);
182511 p = pNext;
182512 }while( p );
182513 }
182514 sqlite3HashClear(&db->aFunc);
182515 for(i=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); i; i=sqliteHashNext(i)((i)->next)){
182516 CollSeq *pColl = (CollSeq *)sqliteHashData(i)((i)->data);
182517 /* Invoke any destructors registered for collation sequence user data. */
182518 for(j=0; j<3; j++){
182519 if( pColl[j].xDel ){
182520 pColl[j].xDel(pColl[j].pUser);
182521 }
182522 }
182523 sqlite3DbFree(db, pColl);
182524 }
182525 sqlite3HashClear(&db->aCollSeq);
182526#ifndef SQLITE_OMIT_VIRTUALTABLE
182527 for(i=sqliteHashFirst(&db->aModule)((&db->aModule)->first); i; i=sqliteHashNext(i)((i)->next)){
182528 Module *pMod = (Module *)sqliteHashData(i)((i)->data);
182529 sqlite3VtabEponymousTableClear(db, pMod);
182530 sqlite3VtabModuleUnref(db, pMod);
182531 }
182532 sqlite3HashClear(&db->aModule);
182533#endif
182534
182535 sqlite3Error(db, SQLITE_OK0); /* Deallocates any cached error strings. */
182536 sqlite3ValueFree(db->pErr);
182537 sqlite3CloseExtensions(db);
182538#if SQLITE_USER_AUTHENTICATION
182539 sqlite3_free(db->auth.zAuthUser);
182540 sqlite3_free(db->auth.zAuthPW);
182541#endif
182542
182543 db->eOpenState = SQLITE_STATE_ERROR0xd5;
182544
182545 /* The temp-database schema is allocated differently from the other schema
182546 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
182547 ** So it needs to be freed here. Todo: Why not roll the temp schema into
182548 ** the same sqliteMalloc() as the one that allocates the database
182549 ** structure?
182550 */
182551 sqlite3DbFree(db, db->aDb[1].pSchema);
182552 if( db->xAutovacDestr ){
182553 db->xAutovacDestr(db->pAutovacPagesArg);
182554 }
182555 sqlite3_mutex_leave(db->mutex);
182556 db->eOpenState = SQLITE_STATE_CLOSED0xce;
182557 sqlite3_mutex_free(db->mutex);
182558 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
182559 if( db->lookaside.bMalloced ){
182560 sqlite3_free(db->lookaside.pStart);
182561 }
182562 sqlite3_free(db);
182563}
182564
182565/*
182566** Rollback all database files. If tripCode is not SQLITE_OK, then
182567** any write cursors are invalidated ("tripped" - as in "tripping a circuit
182568** breaker") and made to return tripCode if there are any further
182569** attempts to use that cursor. Read cursors remain open and valid
182570** but are "saved" in case the table pages are moved around.
182571*/
182572SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3 *db, int tripCode){
182573 int i;
182574 int inTrans = 0;
182575 int schemaChange;
182576 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
182577 sqlite3BeginBenignMalloc();
182578
182579 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
182580 ** This is important in case the transaction being rolled back has
182581 ** modified the database schema. If the b-tree mutexes are not taken
182582 ** here, then another shared-cache connection might sneak in between
182583 ** the database rollback and schema reset, which can cause false
182584 ** corruption reports in some cases. */
182585 sqlite3BtreeEnterAll(db);
182586 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0 && db->init.busy==0;
182587
182588 for(i=0; i<db->nDb; i++){
182589 Btree *p = db->aDb[i].pBt;
182590 if( p ){
182591 if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE2 ){
182592 inTrans = 1;
182593 }
182594 sqlite3BtreeRollback(p, tripCode, !schemaChange);
182595 }
182596 }
182597 sqlite3VtabRollback(db);
182598 sqlite3EndBenignMalloc();
182599
182600 if( schemaChange ){
182601 sqlite3ExpirePreparedStatements(db, 0);
182602 sqlite3ResetAllSchemasOfConnection(db);
182603 }
182604 sqlite3BtreeLeaveAll(db);
182605
182606 /* Any deferred constraint violations have now been resolved. */
182607 db->nDeferredCons = 0;
182608 db->nDeferredImmCons = 0;
182609 db->flags &= ~(u64)(SQLITE_DeferFKs0x00080000|SQLITE_CorruptRdOnly((u64)(0x00002)<<32));
182610
182611 /* If one has been configured, invoke the rollback-hook callback */
182612 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
182613 db->xRollbackCallback(db->pRollbackArg);
182614 }
182615}
182616
182617/*
182618** Return a static string containing the name corresponding to the error code
182619** specified in the argument.
182620*/
182621#if defined(SQLITE_NEED_ERR_NAME)
182622SQLITE_PRIVATEstatic const char *sqlite3ErrName(int rc){
182623 const char *zName = 0;
182624 int i, origRc = rc;
182625 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
182626 switch( rc ){
182627 case SQLITE_OK0: zName = "SQLITE_OK"; break;
182628 case SQLITE_ERROR1: zName = "SQLITE_ERROR"; break;
182629 case SQLITE_ERROR_SNAPSHOT(1 | (3<<8)): zName = "SQLITE_ERROR_SNAPSHOT"; break;
182630 case SQLITE_INTERNAL2: zName = "SQLITE_INTERNAL"; break;
182631 case SQLITE_PERM3: zName = "SQLITE_PERM"; break;
182632 case SQLITE_ABORT4: zName = "SQLITE_ABORT"; break;
182633 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): zName = "SQLITE_ABORT_ROLLBACK"; break;
182634 case SQLITE_BUSY5: zName = "SQLITE_BUSY"; break;
182635 case SQLITE_BUSY_RECOVERY(5 | (1<<8)): zName = "SQLITE_BUSY_RECOVERY"; break;
182636 case SQLITE_BUSY_SNAPSHOT(5 | (2<<8)): zName = "SQLITE_BUSY_SNAPSHOT"; break;
182637 case SQLITE_LOCKED6: zName = "SQLITE_LOCKED"; break;
182638 case SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)): zName = "SQLITE_LOCKED_SHAREDCACHE";break;
182639 case SQLITE_NOMEM7: zName = "SQLITE_NOMEM"; break;
182640 case SQLITE_READONLY8: zName = "SQLITE_READONLY"; break;
182641 case SQLITE_READONLY_RECOVERY(8 | (1<<8)): zName = "SQLITE_READONLY_RECOVERY"; break;
182642 case SQLITE_READONLY_CANTINIT(8 | (5<<8)): zName = "SQLITE_READONLY_CANTINIT"; break;
182643 case SQLITE_READONLY_ROLLBACK(8 | (3<<8)): zName = "SQLITE_READONLY_ROLLBACK"; break;
182644 case SQLITE_READONLY_DBMOVED(8 | (4<<8)): zName = "SQLITE_READONLY_DBMOVED"; break;
182645 case SQLITE_READONLY_DIRECTORY(8 | (6<<8)): zName = "SQLITE_READONLY_DIRECTORY";break;
182646 case SQLITE_INTERRUPT9: zName = "SQLITE_INTERRUPT"; break;
182647 case SQLITE_IOERR10: zName = "SQLITE_IOERR"; break;
182648 case SQLITE_IOERR_READ(10 | (1<<8)): zName = "SQLITE_IOERR_READ"; break;
182649 case SQLITE_IOERR_SHORT_READ(10 | (2<<8)): zName = "SQLITE_IOERR_SHORT_READ"; break;
182650 case SQLITE_IOERR_WRITE(10 | (3<<8)): zName = "SQLITE_IOERR_WRITE"; break;
182651 case SQLITE_IOERR_FSYNC(10 | (4<<8)): zName = "SQLITE_IOERR_FSYNC"; break;
182652 case SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)): zName = "SQLITE_IOERR_DIR_FSYNC"; break;
182653 case SQLITE_IOERR_TRUNCATE(10 | (6<<8)): zName = "SQLITE_IOERR_TRUNCATE"; break;
182654 case SQLITE_IOERR_FSTAT(10 | (7<<8)): zName = "SQLITE_IOERR_FSTAT"; break;
182655 case SQLITE_IOERR_UNLOCK(10 | (8<<8)): zName = "SQLITE_IOERR_UNLOCK"; break;
182656 case SQLITE_IOERR_RDLOCK(10 | (9<<8)): zName = "SQLITE_IOERR_RDLOCK"; break;
182657 case SQLITE_IOERR_DELETE(10 | (10<<8)): zName = "SQLITE_IOERR_DELETE"; break;
182658 case SQLITE_IOERR_NOMEM(10 | (12<<8)): zName = "SQLITE_IOERR_NOMEM"; break;
182659 case SQLITE_IOERR_ACCESS(10 | (13<<8)): zName = "SQLITE_IOERR_ACCESS"; break;
182660 case SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)):
182661 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
182662 case SQLITE_IOERR_LOCK(10 | (15<<8)): zName = "SQLITE_IOERR_LOCK"; break;
182663 case SQLITE_IOERR_CLOSE(10 | (16<<8)): zName = "SQLITE_IOERR_CLOSE"; break;
182664 case SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)): zName = "SQLITE_IOERR_DIR_CLOSE"; break;
182665 case SQLITE_IOERR_SHMOPEN(10 | (18<<8)): zName = "SQLITE_IOERR_SHMOPEN"; break;
182666 case SQLITE_IOERR_SHMSIZE(10 | (19<<8)): zName = "SQLITE_IOERR_SHMSIZE"; break;
182667 case SQLITE_IOERR_SHMLOCK(10 | (20<<8)): zName = "SQLITE_IOERR_SHMLOCK"; break;
182668 case SQLITE_IOERR_SHMMAP(10 | (21<<8)): zName = "SQLITE_IOERR_SHMMAP"; break;
182669 case SQLITE_IOERR_SEEK(10 | (22<<8)): zName = "SQLITE_IOERR_SEEK"; break;
182670 case SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)): zName = "SQLITE_IOERR_DELETE_NOENT";break;
182671 case SQLITE_IOERR_MMAP(10 | (24<<8)): zName = "SQLITE_IOERR_MMAP"; break;
182672 case SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)): zName = "SQLITE_IOERR_GETTEMPPATH"; break;
182673 case SQLITE_IOERR_CONVPATH(10 | (26<<8)): zName = "SQLITE_IOERR_CONVPATH"; break;
182674 case SQLITE_CORRUPT11: zName = "SQLITE_CORRUPT"; break;
182675 case SQLITE_CORRUPT_VTAB(11 | (1<<8)): zName = "SQLITE_CORRUPT_VTAB"; break;
182676 case SQLITE_NOTFOUND12: zName = "SQLITE_NOTFOUND"; break;
182677 case SQLITE_FULL13: zName = "SQLITE_FULL"; break;
182678 case SQLITE_CANTOPEN14: zName = "SQLITE_CANTOPEN"; break;
182679 case SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)): zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
182680 case SQLITE_CANTOPEN_ISDIR(14 | (2<<8)): zName = "SQLITE_CANTOPEN_ISDIR"; break;
182681 case SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)): zName = "SQLITE_CANTOPEN_FULLPATH"; break;
182682 case SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)): zName = "SQLITE_CANTOPEN_CONVPATH"; break;
182683 case SQLITE_CANTOPEN_SYMLINK(14 | (6<<8)): zName = "SQLITE_CANTOPEN_SYMLINK"; break;
182684 case SQLITE_PROTOCOL15: zName = "SQLITE_PROTOCOL"; break;
182685 case SQLITE_EMPTY16: zName = "SQLITE_EMPTY"; break;
182686 case SQLITE_SCHEMA17: zName = "SQLITE_SCHEMA"; break;
182687 case SQLITE_TOOBIG18: zName = "SQLITE_TOOBIG"; break;
182688 case SQLITE_CONSTRAINT19: zName = "SQLITE_CONSTRAINT"; break;
182689 case SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)): zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
182690 case SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)): zName = "SQLITE_CONSTRAINT_TRIGGER";break;
182691 case SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)):
182692 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
182693 case SQLITE_CONSTRAINT_CHECK(19 | (1<<8)): zName = "SQLITE_CONSTRAINT_CHECK"; break;
182694 case SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)):
182695 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
182696 case SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)): zName = "SQLITE_CONSTRAINT_NOTNULL";break;
182697 case SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)):
182698 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
182699 case SQLITE_CONSTRAINT_VTAB(19 | (9<<8)): zName = "SQLITE_CONSTRAINT_VTAB"; break;
182700 case SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)):
182701 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
182702 case SQLITE_CONSTRAINT_ROWID(19 |(10<<8)): zName = "SQLITE_CONSTRAINT_ROWID"; break;
182703 case SQLITE_MISMATCH20: zName = "SQLITE_MISMATCH"; break;
182704 case SQLITE_MISUSE21: zName = "SQLITE_MISUSE"; break;
182705 case SQLITE_NOLFS22: zName = "SQLITE_NOLFS"; break;
182706 case SQLITE_AUTH23: zName = "SQLITE_AUTH"; break;
182707 case SQLITE_FORMAT24: zName = "SQLITE_FORMAT"; break;
182708 case SQLITE_RANGE25: zName = "SQLITE_RANGE"; break;
182709 case SQLITE_NOTADB26: zName = "SQLITE_NOTADB"; break;
182710 case SQLITE_ROW100: zName = "SQLITE_ROW"; break;
182711 case SQLITE_NOTICE27: zName = "SQLITE_NOTICE"; break;
182712 case SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)): zName = "SQLITE_NOTICE_RECOVER_WAL";break;
182713 case SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)):
182714 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
182715 case SQLITE_NOTICE_RBU(27 | (3<<8)): zName = "SQLITE_NOTICE_RBU"; break;
182716 case SQLITE_WARNING28: zName = "SQLITE_WARNING"; break;
182717 case SQLITE_WARNING_AUTOINDEX(28 | (1<<8)): zName = "SQLITE_WARNING_AUTOINDEX"; break;
182718 case SQLITE_DONE101: zName = "SQLITE_DONE"; break;
182719 }
182720 }
182721 if( zName==0 ){
182722 static char zBuf[50];
182723 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
182724 zName = zBuf;
182725 }
182726 return zName;
182727}
182728#endif
182729
182730/*
182731** Return a static string that describes the kind of error specified in the
182732** argument.
182733*/
182734SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int rc){
182735 static const char* const aMsg[] = {
182736 /* SQLITE_OK */ "not an error",
182737 /* SQLITE_ERROR */ "SQL logic error",
182738 /* SQLITE_INTERNAL */ 0,
182739 /* SQLITE_PERM */ "access permission denied",
182740 /* SQLITE_ABORT */ "query aborted",
182741 /* SQLITE_BUSY */ "database is locked",
182742 /* SQLITE_LOCKED */ "database table is locked",
182743 /* SQLITE_NOMEM */ "out of memory",
182744 /* SQLITE_READONLY */ "attempt to write a readonly database",
182745 /* SQLITE_INTERRUPT */ "interrupted",
182746 /* SQLITE_IOERR */ "disk I/O error",
182747 /* SQLITE_CORRUPT */ "database disk image is malformed",
182748 /* SQLITE_NOTFOUND */ "unknown operation",
182749 /* SQLITE_FULL */ "database or disk is full",
182750 /* SQLITE_CANTOPEN */ "unable to open database file",
182751 /* SQLITE_PROTOCOL */ "locking protocol",
182752 /* SQLITE_EMPTY */ 0,
182753 /* SQLITE_SCHEMA */ "database schema has changed",
182754 /* SQLITE_TOOBIG */ "string or blob too big",
182755 /* SQLITE_CONSTRAINT */ "constraint failed",
182756 /* SQLITE_MISMATCH */ "datatype mismatch",
182757 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
182758#ifdef SQLITE_DISABLE_LFS
182759 /* SQLITE_NOLFS */ "large file support is disabled",
182760#else
182761 /* SQLITE_NOLFS */ 0,
182762#endif
182763 /* SQLITE_AUTH */ "authorization denied",
182764 /* SQLITE_FORMAT */ 0,
182765 /* SQLITE_RANGE */ "column index out of range",
182766 /* SQLITE_NOTADB */ "file is not a database",
182767 /* SQLITE_NOTICE */ "notification message",
182768 /* SQLITE_WARNING */ "warning message",
182769 };
182770 const char *zErr = "unknown error";
182771 switch( rc ){
182772 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): {
182773 zErr = "abort due to ROLLBACK";
182774 break;
182775 }
182776 case SQLITE_ROW100: {
182777 zErr = "another row available";
182778 break;
182779 }
182780 case SQLITE_DONE101: {
182781 zErr = "no more rows available";
182782 break;
182783 }
182784 default: {
182785 rc &= 0xff;
182786 if( ALWAYS(rc>=0)(rc>=0) && rc<ArraySize(aMsg)((int)(sizeof(aMsg)/sizeof(aMsg[0]))) && aMsg[rc]!=0 ){
182787 zErr = aMsg[rc];
182788 }
182789 break;
182790 }
182791 }
182792 return zErr;
182793}
182794
182795/*
182796** This routine implements a busy callback that sleeps and tries
182797** again until a timeout value is reached. The timeout value is
182798** an integer number of milliseconds passed in as the first
182799** argument.
182800**
182801** Return non-zero to retry the lock. Return zero to stop trying
182802** and cause SQLite to return SQLITE_BUSY.
182803*/
182804static int sqliteDefaultBusyCallback(
182805 void *ptr, /* Database connection */
182806 int count /* Number of times table has been busy */
182807){
182808#if SQLITE_OS_WIN0 || !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP
182809 /* This case is for systems that have support for sleeping for fractions of
182810 ** a second. Examples: All windows systems, unix systems with nanosleep() */
182811 static const u8 delays[] =
182812 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
182813 static const u8 totals[] =
182814 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
182815# define NDELAY((int)(sizeof(delays)/sizeof(delays[0]))) ArraySize(delays)((int)(sizeof(delays)/sizeof(delays[0])))
182816 sqlite3 *db = (sqlite3 *)ptr;
182817 int tmout = db->busyTimeout;
182818 int delay, prior;
182819
182820 assert( count>=0 )((void) (0));
182821 if( count < NDELAY((int)(sizeof(delays)/sizeof(delays[0]))) ){
182822 delay = delays[count];
182823 prior = totals[count];
182824 }else{
182825 delay = delays[NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1];
182826 prior = totals[NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1] + delay*(count-(NDELAY((int)(sizeof(delays)/sizeof(delays[0])))-1));
182827 }
182828 if( prior + delay > tmout ){
182829 delay = tmout - prior;
182830 if( delay<=0 ) return 0;
182831 }
182832 sqlite3OsSleep(db->pVfs, delay*1000);
182833 return 1;
182834#else
182835 /* This case for unix systems that lack usleep() support. Sleeping
182836 ** must be done in increments of whole seconds */
182837 sqlite3 *db = (sqlite3 *)ptr;
182838 int tmout = ((sqlite3 *)ptr)->busyTimeout;
182839 if( (count+1)*1000 > tmout ){
182840 return 0;
182841 }
182842 sqlite3OsSleep(db->pVfs, 1000000);
182843 return 1;
182844#endif
182845}
182846
182847/*
182848** Invoke the given busy handler.
182849**
182850** This routine is called when an operation failed to acquire a
182851** lock on VFS file pFile.
182852**
182853** If this routine returns non-zero, the lock is retried. If it
182854** returns 0, the operation aborts with an SQLITE_BUSY error.
182855*/
182856SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler *p){
182857 int rc;
182858 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
182859 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
182860 if( rc==0 ){
182861 p->nBusy = -1;
182862 }else{
182863 p->nBusy++;
182864 }
182865 return rc;
182866}
182867
182868/*
182869** This routine sets the busy callback for an Sqlite database to the
182870** given callback function with the given argument.
182871*/
182872SQLITE_API int sqlite3_busy_handler(
182873 sqlite3 *db,
182874 int (*xBusy)(void*,int),
182875 void *pArg
182876){
182877#ifdef SQLITE_ENABLE_API_ARMOR1
182878 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(182878);
182879#endif
182880 sqlite3_mutex_enter(db->mutex);
182881 db->busyHandler.xBusyHandler = xBusy;
182882 db->busyHandler.pBusyArg = pArg;
182883 db->busyHandler.nBusy = 0;
182884 db->busyTimeout = 0;
182885 sqlite3_mutex_leave(db->mutex);
182886 return SQLITE_OK0;
182887}
182888
182889#ifndef SQLITE_OMIT_PROGRESS_CALLBACK1
182890/*
182891** This routine sets the progress callback for an Sqlite database to the
182892** given callback function with the given argument. The progress callback will
182893** be invoked every nOps opcodes.
182894*/
182895SQLITE_API void sqlite3_progress_handler(
182896 sqlite3 *db,
182897 int nOps,
182898 int (*xProgress)(void*),
182899 void *pArg
182900){
182901#ifdef SQLITE_ENABLE_API_ARMOR1
182902 if( !sqlite3SafetyCheckOk(db) ){
182903 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182903);
182904 return;
182905 }
182906#endif
182907 sqlite3_mutex_enter(db->mutex);
182908 if( nOps>0 ){
182909 db->xProgress = xProgress;
182910 db->nProgressOps = (unsigned)nOps;
182911 db->pProgressArg = pArg;
182912 }else{
182913 db->xProgress = 0;
182914 db->nProgressOps = 0;
182915 db->pProgressArg = 0;
182916 }
182917 sqlite3_mutex_leave(db->mutex);
182918}
182919#endif
182920
182921
182922/*
182923** This routine installs a default busy handler that waits for the
182924** specified number of milliseconds before returning 0.
182925*/
182926SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
182927#ifdef SQLITE_ENABLE_API_ARMOR1
182928 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(182928);
182929#endif
182930 if( ms>0 ){
182931 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
182932 (void*)db);
182933 db->busyTimeout = ms;
182934 }else{
182935 sqlite3_busy_handler(db, 0, 0);
182936 }
182937 return SQLITE_OK0;
182938}
182939
182940/*
182941** Cause any pending operation to stop at its earliest opportunity.
182942*/
182943SQLITE_API void sqlite3_interrupt(sqlite3 *db){
182944#ifdef SQLITE_ENABLE_API_ARMOR1
182945 if( !sqlite3SafetyCheckOk(db)
182946 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7)
182947 ){
182948 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182948);
182949 return;
182950 }
182951#endif
182952 AtomicStore(&db->u1.isInterrupted, 1)__atomic_store_n((&db->u1.isInterrupted),(1),0);
182953}
182954
182955/*
182956** Return true or false depending on whether or not an interrupt is
182957** pending on connection db.
182958*/
182959SQLITE_API int sqlite3_is_interrupted(sqlite3 *db){
182960#ifdef SQLITE_ENABLE_API_ARMOR1
182961 if( !sqlite3SafetyCheckOk(db)
182962 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE0xa7)
182963 ){
182964 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(182964);
182965 return 0;
182966 }
182967#endif
182968 return AtomicLoad(&db->u1.isInterrupted)__atomic_load_n((&db->u1.isInterrupted),0)!=0;
182969}
182970
182971/*
182972** This function is exactly the same as sqlite3_create_function(), except
182973** that it is designed to be called by internal code. The difference is
182974** that if a malloc() fails in sqlite3_create_function(), an error code
182975** is returned and the mallocFailed flag cleared.
182976*/
182977SQLITE_PRIVATEstatic int sqlite3CreateFunc(
182978 sqlite3 *db,
182979 const char *zFunctionName,
182980 int nArg,
182981 int enc,
182982 void *pUserData,
182983 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
182984 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
182985 void (*xFinal)(sqlite3_context*),
182986 void (*xValue)(sqlite3_context*),
182987 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
182988 FuncDestructor *pDestructor
182989){
182990 FuncDef *p;
182991 int extraFlags;
182992
182993 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
182994 assert( xValue==0 || xSFunc==0 )((void) (0));
182995 if( zFunctionName==0 /* Must have a valid name */
182996 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
182997 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
182998 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
182999 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG127)
183000 || (255<sqlite3Strlen30(zFunctionName))
183001 ){
183002 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183002);
183003 }
183004
183005 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC )((void) (0));
183006 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY )((void) (0));
183007 extraFlags = enc & (SQLITE_DETERMINISTIC0x000000800|SQLITE_DIRECTONLY0x000080000|
183008 SQLITE_SUBTYPE0x000100000|SQLITE_INNOCUOUS0x000200000|
183009 SQLITE_RESULT_SUBTYPE0x001000000|SQLITE_SELFORDER10x002000000);
183010 enc &= (SQLITE_FUNC_ENCMASK0x0003|SQLITE_ANY5);
183011
183012 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
183013 ** the meaning is inverted. So flip the bit. */
183014 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS )((void) (0));
183015 extraFlags ^= SQLITE_FUNC_UNSAFE0x00200000; /* tag-20230109-1 */
183016
183017
183018#ifndef SQLITE_OMIT_UTF16
183019 /* If SQLITE_UTF16 is specified as the encoding type, transform this
183020 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
183021 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
183022 **
183023 ** If SQLITE_ANY is specified, add three versions of the function
183024 ** to the hash table.
183025 */
183026 switch( enc ){
183027 case SQLITE_UTF164:
183028 enc = SQLITE_UTF16NATIVE2;
183029 break;
183030 case SQLITE_ANY5: {
183031 int rc;
183032 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
183033 (SQLITE_UTF81|extraFlags)^SQLITE_FUNC_UNSAFE0x00200000, /* tag-20230109-1 */
183034 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
183035 if( rc==SQLITE_OK0 ){
183036 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
183037 (SQLITE_UTF16LE2|extraFlags)^SQLITE_FUNC_UNSAFE0x00200000, /* tag-20230109-1*/
183038 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
183039 }
183040 if( rc!=SQLITE_OK0 ){
183041 return rc;
183042 }
183043 enc = SQLITE_UTF16BE3;
183044 break;
183045 }
183046 case SQLITE_UTF81:
183047 case SQLITE_UTF16LE2:
183048 case SQLITE_UTF16BE3:
183049 break;
183050 default:
183051 enc = SQLITE_UTF81;
183052 break;
183053 }
183054#else
183055 enc = SQLITE_UTF81;
183056#endif
183057
183058 /* Check if an existing function is being overridden or deleted. If so,
183059 ** and there are active VMs, then return SQLITE_BUSY. If a function
183060 ** is being overridden/deleted but there are no active VMs, allow the
183061 ** operation to continue but invalidate all precompiled statements.
183062 */
183063 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
183064 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003)==(u32)enc && p->nArg==nArg ){
183065 if( db->nVdbeActive ){
183066 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
183067 "unable to delete/modify user-function due to active statements");
183068 assert( !db->mallocFailed )((void) (0));
183069 return SQLITE_BUSY5;
183070 }else{
183071 sqlite3ExpirePreparedStatements(db, 0);
183072 }
183073 }else if( xSFunc==0 && xFinal==0 ){
183074 /* Trying to delete a function that does not exist. This is a no-op.
183075 ** https://sqlite.org/forum/forumpost/726219164b */
183076 return SQLITE_OK0;
183077 }
183078
183079 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
183080 assert(p || db->mallocFailed)((void) (0));
183081 if( !p ){
183082 return SQLITE_NOMEM_BKPT7;
183083 }
183084
183085 /* If an older version of the function with a configured destructor is
183086 ** being replaced invoke the destructor function here. */
183087 functionDestroy(db, p);
183088
183089 if( pDestructor ){
183090 pDestructor->nRef++;
183091 }
183092 p->u.pDestructor = pDestructor;
183093 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) | extraFlags;
183094 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
183095 testcase( p->funcFlags & SQLITE_DIRECTONLY );
183096 p->xSFunc = xSFunc ? xSFunc : xStep;
183097 p->xFinalize = xFinal;
183098 p->xValue = xValue;
183099 p->xInverse = xInverse;
183100 p->pUserData = pUserData;
183101 p->nArg = (u16)nArg;
183102 return SQLITE_OK0;
183103}
183104
183105/*
183106** Worker function used by utf-8 APIs that create new functions:
183107**
183108** sqlite3_create_function()
183109** sqlite3_create_function_v2()
183110** sqlite3_create_window_function()
183111*/
183112static int createFunctionApi(
183113 sqlite3 *db,
183114 const char *zFunc,
183115 int nArg,
183116 int enc,
183117 void *p,
183118 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
183119 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
183120 void (*xFinal)(sqlite3_context*),
183121 void (*xValue)(sqlite3_context*),
183122 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
183123 void(*xDestroy)(void*)
183124){
183125 int rc = SQLITE_ERROR1;
183126 FuncDestructor *pArg = 0;
183127
183128#ifdef SQLITE_ENABLE_API_ARMOR1
183129 if( !sqlite3SafetyCheckOk(db) ){
183130 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183130);
183131 }
183132#endif
183133 sqlite3_mutex_enter(db->mutex);
183134 if( xDestroy ){
183135 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
183136 if( !pArg ){
183137 sqlite3OomFault(db);
183138 xDestroy(p);
183139 goto out;
183140 }
183141 pArg->nRef = 0;
183142 pArg->xDestroy = xDestroy;
183143 pArg->pUserData = p;
183144 }
183145 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
183146 xSFunc, xStep, xFinal, xValue, xInverse, pArg
183147 );
183148 if( pArg && pArg->nRef==0 ){
183149 assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) )((void) (0));
183150 xDestroy(p);
183151 sqlite3_free(pArg);
183152 }
183153
183154 out:
183155 rc = sqlite3ApiExit(db, rc);
183156 sqlite3_mutex_leave(db->mutex);
183157 return rc;
183158}
183159
183160/*
183161** Create new user functions.
183162*/
183163SQLITE_API int sqlite3_create_function(
183164 sqlite3 *db,
183165 const char *zFunc,
183166 int nArg,
183167 int enc,
183168 void *p,
183169 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
183170 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
183171 void (*xFinal)(sqlite3_context*)
183172){
183173 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
183174 xFinal, 0, 0, 0);
183175}
183176SQLITE_API int sqlite3_create_function_v2(
183177 sqlite3 *db,
183178 const char *zFunc,
183179 int nArg,
183180 int enc,
183181 void *p,
183182 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
183183 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
183184 void (*xFinal)(sqlite3_context*),
183185 void (*xDestroy)(void *)
183186){
183187 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
183188 xFinal, 0, 0, xDestroy);
183189}
183190SQLITE_API int sqlite3_create_window_function(
183191 sqlite3 *db,
183192 const char *zFunc,
183193 int nArg,
183194 int enc,
183195 void *p,
183196 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
183197 void (*xFinal)(sqlite3_context*),
183198 void (*xValue)(sqlite3_context*),
183199 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
183200 void (*xDestroy)(void *)
183201){
183202 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
183203 xFinal, xValue, xInverse, xDestroy);
183204}
183205
183206#ifndef SQLITE_OMIT_UTF16
183207SQLITE_API int sqlite3_create_function16(
183208 sqlite3 *db,
183209 const void *zFunctionName,
183210 int nArg,
183211 int eTextRep,
183212 void *p,
183213 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
183214 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
183215 void (*xFinal)(sqlite3_context*)
183216){
183217 int rc;
183218 char *zFunc8;
183219
183220#ifdef SQLITE_ENABLE_API_ARMOR1
183221 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(183221);
183222#endif
183223 sqlite3_mutex_enter(db->mutex);
183224 assert( !db->mallocFailed )((void) (0));
183225 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE2);
183226 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
183227 sqlite3DbFree(db, zFunc8);
183228 rc = sqlite3ApiExit(db, rc);
183229 sqlite3_mutex_leave(db->mutex);
183230 return rc;
183231}
183232#endif
183233
183234
183235/*
183236** The following is the implementation of an SQL function that always
183237** fails with an error message stating that the function is used in the
183238** wrong context. The sqlite3_overload_function() API might construct
183239** SQL function that use this routine so that the functions will exist
183240** for name resolution but are actually overloaded by the xFindFunction
183241** method of virtual tables.
183242*/
183243static void sqlite3InvalidFunction(
183244 sqlite3_context *context, /* The function calling context */
183245 int NotUsed, /* Number of arguments to the function */
183246 sqlite3_value **NotUsed2 /* Value of each argument */
183247){
183248 const char *zName = (const char*)sqlite3_user_data(context);
183249 char *zErr;
183250 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
183251 zErr = sqlite3_mprintf(
183252 "unable to use function %s in the requested context", zName);
183253 sqlite3_result_error(context, zErr, -1);
183254 sqlite3_free(zErr);
183255}
183256
183257/*
183258** Declare that a function has been overloaded by a virtual table.
183259**
183260** If the function already exists as a regular global function, then
183261** this routine is a no-op. If the function does not exist, then create
183262** a new one that always throws a run-time error.
183263**
183264** When virtual tables intend to provide an overloaded function, they
183265** should call this routine to make sure the global function exists.
183266** A global function must exist in order for name resolution to work
183267** properly.
183268*/
183269SQLITE_API int sqlite3_overload_function(
183270 sqlite3 *db,
183271 const char *zName,
183272 int nArg
183273){
183274 int rc;
183275 char *zCopy;
183276
183277#ifdef SQLITE_ENABLE_API_ARMOR1
183278 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
183279 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183279);
183280 }
183281#endif
183282 sqlite3_mutex_enter(db->mutex);
183283 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF81, 0)!=0;
183284 sqlite3_mutex_leave(db->mutex);
183285 if( rc ) return SQLITE_OK0;
183286 zCopy = sqlite3_mprintf("%s", zName);
183287 if( zCopy==0 ) return SQLITE_NOMEM7;
183288 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF81,
183289 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
183290}
183291
183292#ifndef SQLITE_OMIT_TRACE
183293/*
183294** Register a trace function. The pArg from the previously registered trace
183295** is returned.
183296**
183297** A NULL trace function means that no tracing is executes. A non-NULL
183298** trace is a pointer to a function that is invoked at the start of each
183299** SQL statement.
183300*/
183301#ifndef SQLITE_OMIT_DEPRECATED1
183302SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
183303 void *pOld;
183304
183305#ifdef SQLITE_ENABLE_API_ARMOR1
183306 if( !sqlite3SafetyCheckOk(db) ){
183307 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183307);
183308 return 0;
183309 }
183310#endif
183311 sqlite3_mutex_enter(db->mutex);
183312 pOld = db->pTraceArg;
183313 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY0 : 0;
183314 db->trace.xLegacy = xTrace;
183315 db->pTraceArg = pArg;
183316 sqlite3_mutex_leave(db->mutex);
183317 return pOld;
183318}
183319#endif /* SQLITE_OMIT_DEPRECATED */
183320
183321/* Register a trace callback using the version-2 interface.
183322*/
183323SQLITE_API int sqlite3_trace_v2(
183324 sqlite3 *db, /* Trace this connection */
183325 unsigned mTrace, /* Mask of events to be traced */
183326 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
183327 void *pArg /* Context */
183328){
183329#ifdef SQLITE_ENABLE_API_ARMOR1
183330 if( !sqlite3SafetyCheckOk(db) ){
183331 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183331);
183332 }
183333#endif
183334 sqlite3_mutex_enter(db->mutex);
183335 if( mTrace==0 ) xTrace = 0;
183336 if( xTrace==0 ) mTrace = 0;
183337 db->mTrace = mTrace;
183338 db->trace.xV2 = xTrace;
183339 db->pTraceArg = pArg;
183340 sqlite3_mutex_leave(db->mutex);
183341 return SQLITE_OK0;
183342}
183343
183344#ifndef SQLITE_OMIT_DEPRECATED1
183345/*
183346** Register a profile function. The pArg from the previously registered
183347** profile function is returned.
183348**
183349** A NULL profile function means that no profiling is executes. A non-NULL
183350** profile is a pointer to a function that is invoked at the conclusion of
183351** each SQL statement that is run.
183352*/
183353SQLITE_API void *sqlite3_profile(
183354 sqlite3 *db,
183355 void (*xProfile)(void*,const char*,sqlite_uint64),
183356 void *pArg
183357){
183358 void *pOld;
183359
183360#ifdef SQLITE_ENABLE_API_ARMOR1
183361 if( !sqlite3SafetyCheckOk(db) ){
183362 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183362);
183363 return 0;
183364 }
183365#endif
183366 sqlite3_mutex_enter(db->mutex);
183367 pOld = db->pProfileArg;
183368 db->xProfile = xProfile;
183369 db->pProfileArg = pArg;
183370 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK0x0f;
183371 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE0;
183372 sqlite3_mutex_leave(db->mutex);
183373 return pOld;
183374}
183375#endif /* SQLITE_OMIT_DEPRECATED */
183376#endif /* SQLITE_OMIT_TRACE */
183377
183378/*
183379** Register a function to be invoked when a transaction commits.
183380** If the invoked function returns non-zero, then the commit becomes a
183381** rollback.
183382*/
183383SQLITE_API void *sqlite3_commit_hook(
183384 sqlite3 *db, /* Attach the hook to this database */
183385 int (*xCallback)(void*), /* Function to invoke on each commit */
183386 void *pArg /* Argument to the function */
183387){
183388 void *pOld;
183389
183390#ifdef SQLITE_ENABLE_API_ARMOR1
183391 if( !sqlite3SafetyCheckOk(db) ){
183392 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183392);
183393 return 0;
183394 }
183395#endif
183396 sqlite3_mutex_enter(db->mutex);
183397 pOld = db->pCommitArg;
183398 db->xCommitCallback = xCallback;
183399 db->pCommitArg = pArg;
183400 sqlite3_mutex_leave(db->mutex);
183401 return pOld;
183402}
183403
183404/*
183405** Register a callback to be invoked each time a row is updated,
183406** inserted or deleted using this database connection.
183407*/
183408SQLITE_API void *sqlite3_update_hook(
183409 sqlite3 *db, /* Attach the hook to this database */
183410 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
183411 void *pArg /* Argument to the function */
183412){
183413 void *pRet;
183414
183415#ifdef SQLITE_ENABLE_API_ARMOR1
183416 if( !sqlite3SafetyCheckOk(db) ){
183417 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183417);
183418 return 0;
183419 }
183420#endif
183421 sqlite3_mutex_enter(db->mutex);
183422 pRet = db->pUpdateArg;
183423 db->xUpdateCallback = xCallback;
183424 db->pUpdateArg = pArg;
183425 sqlite3_mutex_leave(db->mutex);
183426 return pRet;
183427}
183428
183429/*
183430** Register a callback to be invoked each time a transaction is rolled
183431** back by this database connection.
183432*/
183433SQLITE_API void *sqlite3_rollback_hook(
183434 sqlite3 *db, /* Attach the hook to this database */
183435 void (*xCallback)(void*), /* Callback function */
183436 void *pArg /* Argument to the function */
183437){
183438 void *pRet;
183439
183440#ifdef SQLITE_ENABLE_API_ARMOR1
183441 if( !sqlite3SafetyCheckOk(db) ){
183442 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183442);
183443 return 0;
183444 }
183445#endif
183446 sqlite3_mutex_enter(db->mutex);
183447 pRet = db->pRollbackArg;
183448 db->xRollbackCallback = xCallback;
183449 db->pRollbackArg = pArg;
183450 sqlite3_mutex_leave(db->mutex);
183451 return pRet;
183452}
183453
183454#ifdef SQLITE_ENABLE_PREUPDATE_HOOK1
183455/*
183456** Register a callback to be invoked each time a row is updated,
183457** inserted or deleted using this database connection.
183458*/
183459SQLITE_API void *sqlite3_preupdate_hook(
183460 sqlite3 *db, /* Attach the hook to this database */
183461 void(*xCallback)( /* Callback function */
183462 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
183463 void *pArg /* First callback argument */
183464){
183465 void *pRet;
183466
183467#ifdef SQLITE_ENABLE_API_ARMOR1
183468 if( db==0 ){
183469 return 0;
183470 }
183471#endif
183472 sqlite3_mutex_enter(db->mutex);
183473 pRet = db->pPreUpdateArg;
183474 db->xPreUpdateCallback = xCallback;
183475 db->pPreUpdateArg = pArg;
183476 sqlite3_mutex_leave(db->mutex);
183477 return pRet;
183478}
183479#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
183480
183481/*
183482** Register a function to be invoked prior to each autovacuum that
183483** determines the number of pages to vacuum.
183484*/
183485SQLITE_API int sqlite3_autovacuum_pages(
183486 sqlite3 *db, /* Attach the hook to this database */
183487 unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
183488 void *pArg, /* Argument to the function */
183489 void (*xDestructor)(void*) /* Destructor for pArg */
183490){
183491#ifdef SQLITE_ENABLE_API_ARMOR1
183492 if( !sqlite3SafetyCheckOk(db) ){
183493 if( xDestructor ) xDestructor(pArg);
183494 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183494);
183495 }
183496#endif
183497 sqlite3_mutex_enter(db->mutex);
183498 if( db->xAutovacDestr ){
183499 db->xAutovacDestr(db->pAutovacPagesArg);
183500 }
183501 db->xAutovacPages = xCallback;
183502 db->pAutovacPagesArg = pArg;
183503 db->xAutovacDestr = xDestructor;
183504 sqlite3_mutex_leave(db->mutex);
183505 return SQLITE_OK0;
183506}
183507
183508
183509#ifndef SQLITE_OMIT_WAL
183510/*
183511** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
183512** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
183513** is greater than sqlite3.pWalArg cast to an integer (the value configured by
183514** wal_autocheckpoint()).
183515*/
183516SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(
183517 void *pClientData, /* Argument */
183518 sqlite3 *db, /* Connection */
183519 const char *zDb, /* Database */
183520 int nFrame /* Size of WAL */
183521){
183522 if( nFrame>=SQLITE_PTR_TO_INT(pClientData)((int)(long int)(pClientData)) ){
183523 sqlite3BeginBenignMalloc();
183524 sqlite3_wal_checkpoint(db, zDb);
183525 sqlite3EndBenignMalloc();
183526 }
183527 return SQLITE_OK0;
183528}
183529#endif /* SQLITE_OMIT_WAL */
183530
183531/*
183532** Configure an sqlite3_wal_hook() callback to automatically checkpoint
183533** a database after committing a transaction if there are nFrame or
183534** more frames in the log file. Passing zero or a negative value as the
183535** nFrame parameter disables automatic checkpoints entirely.
183536**
183537** The callback registered by this function replaces any existing callback
183538** registered using sqlite3_wal_hook(). Likewise, registering a callback
183539** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
183540** configured by this function.
183541*/
183542SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
183543#ifdef SQLITE_OMIT_WAL
183544 UNUSED_PARAMETER(db)(void)(db);
183545 UNUSED_PARAMETER(nFrame)(void)(nFrame);
183546#else
183547#ifdef SQLITE_ENABLE_API_ARMOR1
183548 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(183548);
183549#endif
183550 if( nFrame>0 ){
183551 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)((void*)(long int)(nFrame)));
183552 }else{
183553 sqlite3_wal_hook(db, 0, 0);
183554 }
183555#endif
183556 return SQLITE_OK0;
183557}
183558
183559/*
183560** Register a callback to be invoked each time a transaction is written
183561** into the write-ahead-log by this database connection.
183562*/
183563SQLITE_API void *sqlite3_wal_hook(
183564 sqlite3 *db, /* Attach the hook to this db handle */
183565 int(*xCallback)(void *, sqlite3*, const char*, int),
183566 void *pArg /* First argument passed to xCallback() */
183567){
183568#ifndef SQLITE_OMIT_WAL
183569 void *pRet;
183570#ifdef SQLITE_ENABLE_API_ARMOR1
183571 if( !sqlite3SafetyCheckOk(db) ){
183572 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(183572);
183573 return 0;
183574 }
183575#endif
183576 sqlite3_mutex_enter(db->mutex);
183577 pRet = db->pWalArg;
183578 db->xWalCallback = xCallback;
183579 db->pWalArg = pArg;
183580 sqlite3_mutex_leave(db->mutex);
183581 return pRet;
183582#else
183583 return 0;
183584#endif
183585}
183586
183587/*
183588** Checkpoint database zDb.
183589*/
183590SQLITE_API int sqlite3_wal_checkpoint_v2(
183591 sqlite3 *db, /* Database handle */
183592 const char *zDb, /* Name of attached database (or NULL) */
183593 int eMode, /* SQLITE_CHECKPOINT_* value */
183594 int *pnLog, /* OUT: Size of WAL log in frames */
183595 int *pnCkpt /* OUT: Total number of frames checkpointed */
183596){
183597#ifdef SQLITE_OMIT_WAL
183598 return SQLITE_OK0;
183599#else
183600 int rc; /* Return code */
183601 int iDb; /* Schema to checkpoint */
183602
183603#ifdef SQLITE_ENABLE_API_ARMOR1
183604 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(183604);
183605#endif
183606
183607 /* Initialize the output variables to -1 in case an error occurs. */
183608 if( pnLog ) *pnLog = -1;
183609 if( pnCkpt ) *pnCkpt = -1;
183610
183611 assert( SQLITE_CHECKPOINT_PASSIVE==0 )((void) (0));
183612 assert( SQLITE_CHECKPOINT_FULL==1 )((void) (0));
183613 assert( SQLITE_CHECKPOINT_RESTART==2 )((void) (0));
183614 assert( SQLITE_CHECKPOINT_TRUNCATE==3 )((void) (0));
183615 if( eMode<SQLITE_CHECKPOINT_PASSIVE0 || eMode>SQLITE_CHECKPOINT_TRUNCATE3 ){
183616 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
183617 ** mode: */
183618 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183618);
183619 }
183620
183621 sqlite3_mutex_enter(db->mutex);
183622 if( zDb && zDb[0] ){
183623 iDb = sqlite3FindDbName(db, zDb);
183624 }else{
183625 iDb = SQLITE_MAX_DB(10 +2); /* This means process all schemas */
183626 }
183627 if( iDb<0 ){
183628 rc = SQLITE_ERROR1;
183629 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "unknown database: %s", zDb);
183630 }else{
183631 db->busyHandler.nBusy = 0;
183632 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
183633 sqlite3Error(db, rc);
183634 }
183635 rc = sqlite3ApiExit(db, rc);
183636
183637 /* If there are no active statements, clear the interrupt flag at this
183638 ** point. */
183639 if( db->nVdbeActive==0 ){
183640 AtomicStore(&db->u1.isInterrupted, 0)__atomic_store_n((&db->u1.isInterrupted),(0),0);
183641 }
183642
183643 sqlite3_mutex_leave(db->mutex);
183644 return rc;
183645#endif
183646}
183647
183648
183649/*
183650** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
183651** to contains a zero-length string, all attached databases are
183652** checkpointed.
183653*/
183654SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
183655 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
183656 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
183657 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE0,0,0);
183658}
183659
183660#ifndef SQLITE_OMIT_WAL
183661/*
183662** Run a checkpoint on database iDb. This is a no-op if database iDb is
183663** not currently open in WAL mode.
183664**
183665** If a transaction is open on the database being checkpointed, this
183666** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
183667** an error occurs while running the checkpoint, an SQLite error code is
183668** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
183669**
183670** The mutex on database handle db should be held by the caller. The mutex
183671** associated with the specific b-tree being checkpointed is taken by
183672** this function while the checkpoint is running.
183673**
183674** If iDb is passed SQLITE_MAX_DB then all attached databases are
183675** checkpointed. If an error is encountered it is returned immediately -
183676** no attempt is made to checkpoint any remaining databases.
183677**
183678** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
183679** or TRUNCATE.
183680*/
183681SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
183682 int rc = SQLITE_OK0; /* Return code */
183683 int i; /* Used to iterate through attached dbs */
183684 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
183685
183686 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
183687 assert( !pnLog || *pnLog==-1 )((void) (0));
183688 assert( !pnCkpt || *pnCkpt==-1 )((void) (0));
183689 testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */
183690 testcase( iDb==SQLITE_MAX_DB );
183691
183692 for(i=0; i<db->nDb && rc==SQLITE_OK0; i++){
183693 if( i==iDb || iDb==SQLITE_MAX_DB(10 +2) ){
183694 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
183695 pnLog = 0;
183696 pnCkpt = 0;
183697 if( rc==SQLITE_BUSY5 ){
183698 bBusy = 1;
183699 rc = SQLITE_OK0;
183700 }
183701 }
183702 }
183703
183704 return (rc==SQLITE_OK0 && bBusy) ? SQLITE_BUSY5 : rc;
183705}
183706#endif /* SQLITE_OMIT_WAL */
183707
183708/*
183709** This function returns true if main-memory should be used instead of
183710** a temporary file for transient pager files and statement journals.
183711** The value returned depends on the value of db->temp_store (runtime
183712** parameter) and the compile time value of SQLITE_TEMP_STORE. The
183713** following table describes the relationship between these two values
183714** and this functions return value.
183715**
183716** SQLITE_TEMP_STORE db->temp_store Location of temporary database
183717** ----------------- -------------- ------------------------------
183718** 0 any file (return 0)
183719** 1 1 file (return 0)
183720** 1 2 memory (return 1)
183721** 1 0 file (return 0)
183722** 2 1 file (return 0)
183723** 2 2 memory (return 1)
183724** 2 0 memory (return 1)
183725** 3 any memory (return 1)
183726*/
183727SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3 *db){
183728#if SQLITE_TEMP_STORE1==1
183729 return ( db->temp_store==2 );
183730#endif
183731#if SQLITE_TEMP_STORE1==2
183732 return ( db->temp_store!=1 );
183733#endif
183734#if SQLITE_TEMP_STORE1==3
183735 UNUSED_PARAMETER(db)(void)(db);
183736 return 1;
183737#endif
183738#if SQLITE_TEMP_STORE1<1 || SQLITE_TEMP_STORE1>3
183739 UNUSED_PARAMETER(db)(void)(db);
183740 return 0;
183741#endif
183742}
183743
183744/*
183745** Return UTF-8 encoded English language explanation of the most recent
183746** error.
183747*/
183748SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
183749 const char *z;
183750 if( !db ){
183751 return sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
183752 }
183753 if( !sqlite3SafetyCheckSickOrOk(db) ){
183754 return sqlite3ErrStr(SQLITE_MISUSE_BKPTsqlite3MisuseError(183754));
183755 }
183756 sqlite3_mutex_enter(db->mutex);
183757 if( db->mallocFailed ){
183758 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
183759 }else{
183760 testcase( db->pErr==0 );
183761 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
183762 assert( !db->mallocFailed )((void) (0));
183763 if( z==0 ){
183764 z = sqlite3ErrStr(db->errCode);
183765 }
183766 }
183767 sqlite3_mutex_leave(db->mutex);
183768 return z;
183769}
183770
183771/*
183772** Return the byte offset of the most recent error
183773*/
183774SQLITE_API int sqlite3_error_offset(sqlite3 *db){
183775 int iOffset = -1;
183776 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
183777 sqlite3_mutex_enter(db->mutex);
183778 iOffset = db->errByteOffset;
183779 sqlite3_mutex_leave(db->mutex);
183780 }
183781 return iOffset;
183782}
183783
183784#ifndef SQLITE_OMIT_UTF16
183785/*
183786** Return UTF-16 encoded English language explanation of the most recent
183787** error.
183788*/
183789SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
183790 static const u16 outOfMem[] = {
183791 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
183792 };
183793 static const u16 misuse[] = {
183794 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
183795 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
183796 'm', 'i', 's', 'u', 's', 'e', 0
183797 };
183798
183799 const void *z;
183800 if( !db ){
183801 return (void *)outOfMem;
183802 }
183803 if( !sqlite3SafetyCheckSickOrOk(db) ){
183804 return (void *)misuse;
183805 }
183806 sqlite3_mutex_enter(db->mutex);
183807 if( db->mallocFailed ){
183808 z = (void *)outOfMem;
183809 }else{
183810 z = sqlite3_value_text16(db->pErr);
183811 if( z==0 ){
183812 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
183813 z = sqlite3_value_text16(db->pErr);
183814 }
183815 /* A malloc() may have failed within the call to sqlite3_value_text16()
183816 ** above. If this is the case, then the db->mallocFailed flag needs to
183817 ** be cleared before returning. Do this directly, instead of via
183818 ** sqlite3ApiExit(), to avoid setting the database handle error message.
183819 */
183820 sqlite3OomClear(db);
183821 }
183822 sqlite3_mutex_leave(db->mutex);
183823 return z;
183824}
183825#endif /* SQLITE_OMIT_UTF16 */
183826
183827/*
183828** Return the most recent error code generated by an SQLite routine. If NULL is
183829** passed to this function, we assume a malloc() failed during sqlite3_open().
183830*/
183831SQLITE_API int sqlite3_errcode(sqlite3 *db){
183832 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
183833 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183833);
183834 }
183835 if( !db || db->mallocFailed ){
183836 return SQLITE_NOMEM_BKPT7;
183837 }
183838 return db->errCode & db->errMask;
183839}
183840SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
183841 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
183842 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183842);
183843 }
183844 if( !db || db->mallocFailed ){
183845 return SQLITE_NOMEM_BKPT7;
183846 }
183847 return db->errCode;
183848}
183849SQLITE_API int sqlite3_system_errno(sqlite3 *db){
183850 return db ? db->iSysErrno : 0;
183851}
183852
183853/*
183854** Return a string that describes the kind of error specified in the
183855** argument. For now, this simply calls the internal sqlite3ErrStr()
183856** function.
183857*/
183858SQLITE_API const char *sqlite3_errstr(int rc){
183859 return sqlite3ErrStr(rc);
183860}
183861
183862/*
183863** Create a new collating function for database "db". The name is zName
183864** and the encoding is enc.
183865*/
183866static int createCollation(
183867 sqlite3* db,
183868 const char *zName,
183869 u8 enc,
183870 void* pCtx,
183871 int(*xCompare)(void*,int,const void*,int,const void*),
183872 void(*xDel)(void*)
183873){
183874 CollSeq *pColl;
183875 int enc2;
183876
183877 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
183878
183879 /* If SQLITE_UTF16 is specified as the encoding type, transform this
183880 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
183881 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
183882 */
183883 enc2 = enc;
183884 testcase( enc2==SQLITE_UTF16 );
183885 testcase( enc2==SQLITE_UTF16_ALIGNED );
183886 if( enc2==SQLITE_UTF164 || enc2==SQLITE_UTF16_ALIGNED8 ){
183887 enc2 = SQLITE_UTF16NATIVE2;
183888 }
183889 if( enc2<SQLITE_UTF81 || enc2>SQLITE_UTF16BE3 ){
183890 return SQLITE_MISUSE_BKPTsqlite3MisuseError(183890);
183891 }
183892
183893 /* Check if this call is removing or replacing an existing collation
183894 ** sequence. If so, and there are active VMs, return busy. If there
183895 ** are no active VMs, invalidate any pre-compiled statements.
183896 */
183897 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
183898 if( pColl && pColl->xCmp ){
183899 if( db->nVdbeActive ){
183900 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
183901 "unable to delete/modify collation sequence due to active statements");
183902 return SQLITE_BUSY5;
183903 }
183904 sqlite3ExpirePreparedStatements(db, 0);
183905
183906 /* If collation sequence pColl was created directly by a call to
183907 ** sqlite3_create_collation, and not generated by synthCollSeq(),
183908 ** then any copies made by synthCollSeq() need to be invalidated.
183909 ** Also, collation destructor - CollSeq.xDel() - function may need
183910 ** to be called.
183911 */
183912 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED8)==enc2 ){
183913 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
183914 int j;
183915 for(j=0; j<3; j++){
183916 CollSeq *p = &aColl[j];
183917 if( p->enc==pColl->enc ){
183918 if( p->xDel ){
183919 p->xDel(p->pUser);
183920 }
183921 p->xCmp = 0;
183922 }
183923 }
183924 }
183925 }
183926
183927 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
183928 if( pColl==0 ) return SQLITE_NOMEM_BKPT7;
183929 pColl->xCmp = xCompare;
183930 pColl->pUser = pCtx;
183931 pColl->xDel = xDel;
183932 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED8));
183933 sqlite3Error(db, SQLITE_OK0);
183934 return SQLITE_OK0;
183935}
183936
183937
183938/*
183939** This array defines hard upper bounds on limit values. The
183940** initializer must be kept in sync with the SQLITE_LIMIT_*
183941** #defines in sqlite3.h.
183942*/
183943static const int aHardLimit[] = {
183944 SQLITE_MAX_LENGTH2147483645,
183945 SQLITE_MAX_SQL_LENGTH1000000000,
183946 SQLITE_MAX_COLUMN2000,
183947 SQLITE_MAX_EXPR_DEPTH1000,
183948 SQLITE_MAX_COMPOUND_SELECT500,
183949 SQLITE_MAX_VDBE_OP250000000,
183950 SQLITE_MAX_FUNCTION_ARG127,
183951 SQLITE_MAX_ATTACHED10,
183952 SQLITE_MAX_LIKE_PATTERN_LENGTH50000,
183953 SQLITE_MAX_VARIABLE_NUMBER500000, /* IMP: R-38091-32352 */
183954 SQLITE_MAX_TRIGGER_DEPTH1000,
183955 SQLITE_MAX_WORKER_THREADS8,
183956};
183957
183958/*
183959** Make sure the hard limits are set to reasonable values
183960*/
183961#if SQLITE_MAX_LENGTH2147483645<100
183962# error SQLITE_MAX_LENGTH2147483645 must be at least 100
183963#endif
183964#if SQLITE_MAX_SQL_LENGTH1000000000<100
183965# error SQLITE_MAX_SQL_LENGTH1000000000 must be at least 100
183966#endif
183967#if SQLITE_MAX_SQL_LENGTH1000000000>SQLITE_MAX_LENGTH2147483645
183968# error SQLITE_MAX_SQL_LENGTH1000000000 must not be greater than SQLITE_MAX_LENGTH2147483645
183969#endif
183970#if SQLITE_MAX_COMPOUND_SELECT500<2
183971# error SQLITE_MAX_COMPOUND_SELECT500 must be at least 2
183972#endif
183973#if SQLITE_MAX_VDBE_OP250000000<40
183974# error SQLITE_MAX_VDBE_OP250000000 must be at least 40
183975#endif
183976#if SQLITE_MAX_FUNCTION_ARG127<0 || SQLITE_MAX_FUNCTION_ARG127>127
183977# error SQLITE_MAX_FUNCTION_ARG127 must be between 0 and 127
183978#endif
183979#if SQLITE_MAX_ATTACHED10<0 || SQLITE_MAX_ATTACHED10>125
183980# error SQLITE_MAX_ATTACHED10 must be between 0 and 125
183981#endif
183982#if SQLITE_MAX_LIKE_PATTERN_LENGTH50000<1
183983# error SQLITE_MAX_LIKE_PATTERN_LENGTH50000 must be at least 1
183984#endif
183985#if SQLITE_MAX_COLUMN2000>32767
183986# error SQLITE_MAX_COLUMN2000 must not exceed 32767
183987#endif
183988#if SQLITE_MAX_TRIGGER_DEPTH1000<1
183989# error SQLITE_MAX_TRIGGER_DEPTH1000 must be at least 1
183990#endif
183991#if SQLITE_MAX_WORKER_THREADS8<0 || SQLITE_MAX_WORKER_THREADS8>50
183992# error SQLITE_MAX_WORKER_THREADS8 must be between 0 and 50
183993#endif
183994
183995
183996/*
183997** Change the value of a limit. Report the old value.
183998** If an invalid limit index is supplied, report -1.
183999** Make no changes but still report the old value if the
184000** new limit is negative.
184001**
184002** A new lower limit does not shrink existing constructs.
184003** It merely prevents new constructs that exceed the limit
184004** from forming.
184005*/
184006SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
184007 int oldLimit;
184008
184009#ifdef SQLITE_ENABLE_API_ARMOR1
184010 if( !sqlite3SafetyCheckOk(db) ){
184011 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184011);
184012 return -1;
184013 }
184014#endif
184015
184016 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
184017 ** there is a hard upper bound set at compile-time by a C preprocessor
184018 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
184019 ** "_MAX_".)
184020 */
184021 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH )((void) (0));
184022 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH )((void) (0));
184023 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN )((void) (0));
184024 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH )((void) (0));
184025 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT)((void) (0));
184026 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP )((void) (0));
184027 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG )((void) (0));
184028 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED )((void) (0));
184029 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==((void) (0))
184030 SQLITE_MAX_LIKE_PATTERN_LENGTH )((void) (0));
184031 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER)((void) (0));
184032 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH )((void) (0));
184033 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS )((void) (0));
184034 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) )((void) (0));
184035
184036
184037 if( limitId<0 || limitId>=SQLITE_N_LIMIT(11 +1) ){
184038 return -1;
184039 }
184040 oldLimit = db->aLimit[limitId];
184041 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
184042 if( newLimit>aHardLimit[limitId] ){
184043 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
184044 }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH0 ){
184045 newLimit = 1;
184046 }
184047 db->aLimit[limitId] = newLimit;
184048 }
184049 return oldLimit; /* IMP: R-53341-35419 */
184050}
184051
184052/*
184053** This function is used to parse both URIs and non-URI filenames passed by the
184054** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
184055** URIs specified as part of ATTACH statements.
184056**
184057** The first argument to this function is the name of the VFS to use (or
184058** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
184059** query parameter. The second argument contains the URI (or non-URI filename)
184060** itself. When this function is called the *pFlags variable should contain
184061** the default flags to open the database handle with. The value stored in
184062** *pFlags may be updated before returning if the URI filename contains
184063** "cache=xxx" or "mode=xxx" query parameters.
184064**
184065** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
184066** the VFS that should be used to open the database file. *pzFile is set to
184067** point to a buffer containing the name of the file to open. The value
184068** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
184069** and is in the same format as names created using sqlite3_create_filename().
184070** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
184071** the value returned in *pzFile to avoid a memory leak.
184072**
184073** If an error occurs, then an SQLite error code is returned and *pzErrMsg
184074** may be set to point to a buffer containing an English language error
184075** message. It is the responsibility of the caller to eventually release
184076** this buffer by calling sqlite3_free().
184077*/
184078SQLITE_PRIVATEstatic int sqlite3ParseUri(
184079 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
184080 const char *zUri, /* Nul-terminated URI to parse */
184081 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
184082 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
184083 char **pzFile, /* OUT: Filename component of URI */
184084 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
184085){
184086 int rc = SQLITE_OK0;
184087 unsigned int flags = *pFlags;
184088 const char *zVfs = zDefaultVfs;
184089 char *zFile;
184090 char c;
184091 int nUri = sqlite3Strlen30(zUri);
184092
184093 assert( *pzErrMsg==0 )((void) (0));
184094
184095 if( ((flags & SQLITE_OPEN_URI0x00000040) /* IMP: R-48725-32206 */
184096 || AtomicLoad(&sqlite3GlobalConfig.bOpenUri)__atomic_load_n((&sqlite3Config.bOpenUri),0)) /* IMP: R-51689-46548 */
184097 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
184098 ){
184099 char *zOpt;
184100 int eState; /* Parser state when parsing URI */
184101 int iIn; /* Input character index */
184102 int iOut = 0; /* Output character index */
184103 u64 nByte = nUri+8; /* Bytes of space to allocate */
184104
184105 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
184106 ** method that there may be extra parameters following the file-name. */
184107 flags |= SQLITE_OPEN_URI0x00000040;
184108
184109 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
184110 zFile = sqlite3_malloc64(nByte);
184111 if( !zFile ) return SQLITE_NOMEM_BKPT7;
184112
184113 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
184114 zFile += 4;
184115
184116 iIn = 5;
184117#ifdef SQLITE_ALLOW_URI_AUTHORITY
184118 if( strncmp(zUri+5, "///", 3)==0 ){
184119 iIn = 7;
184120 /* The following condition causes URIs with five leading / characters
184121 ** like file://///host/path to be converted into UNCs like //host/path.
184122 ** The correct URI for that UNC has only two or four leading / characters
184123 ** file://host/path or file:////host/path. But 5 leading slashes is a
184124 ** common error, we are told, so we handle it as a special case. */
184125 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
184126 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
184127 iIn = 16;
184128 }
184129#else
184130 /* Discard the scheme and authority segments of the URI. */
184131 if( zUri[5]=='/' && zUri[6]=='/' ){
184132 iIn = 7;
184133 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
184134 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
184135 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
184136 iIn-7, &zUri[7]);
184137 rc = SQLITE_ERROR1;
184138 goto parse_uri_out;
184139 }
184140 }
184141#endif
184142
184143 /* Copy the filename and any query parameters into the zFile buffer.
184144 ** Decode %HH escape codes along the way.
184145 **
184146 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
184147 ** on the parsing context. As follows:
184148 **
184149 ** 0: Parsing file-name.
184150 ** 1: Parsing name section of a name=value query parameter.
184151 ** 2: Parsing value section of a name=value query parameter.
184152 */
184153 eState = 0;
184154 while( (c = zUri[iIn])!=0 && c!='#' ){
184155 iIn++;
184156 if( c=='%'
184157 && sqlite3Isxdigit(zUri[iIn])(sqlite3CtypeMap[(unsigned char)(zUri[iIn])]&0x08)
184158 && sqlite3Isxdigit(zUri[iIn+1])(sqlite3CtypeMap[(unsigned char)(zUri[iIn+1])]&0x08)
184159 ){
184160 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
184161 octet += sqlite3HexToInt(zUri[iIn++]);
184162
184163 assert( octet>=0 && octet<256 )((void) (0));
184164 if( octet==0 ){
184165#ifndef SQLITE_ENABLE_URI_00_ERROR
184166 /* This branch is taken when "%00" appears within the URI. In this
184167 ** case we ignore all text in the remainder of the path, name or
184168 ** value currently being parsed. So ignore the current character
184169 ** and skip to the next "?", "=" or "&", as appropriate. */
184170 while( (c = zUri[iIn])!=0 && c!='#'
184171 && (eState!=0 || c!='?')
184172 && (eState!=1 || (c!='=' && c!='&'))
184173 && (eState!=2 || c!='&')
184174 ){
184175 iIn++;
184176 }
184177 continue;
184178#else
184179 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
184180 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
184181 rc = SQLITE_ERROR1;
184182 goto parse_uri_out;
184183#endif
184184 }
184185 c = octet;
184186 }else if( eState==1 && (c=='&' || c=='=') ){
184187 if( zFile[iOut-1]==0 ){
184188 /* An empty option name. Ignore this option altogether. */
184189 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
184190 continue;
184191 }
184192 if( c=='&' ){
184193 zFile[iOut++] = '\0';
184194 }else{
184195 eState = 2;
184196 }
184197 c = 0;
184198 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
184199 c = 0;
184200 eState = 1;
184201 }
184202 zFile[iOut++] = c;
184203 }
184204 if( eState==1 ) zFile[iOut++] = '\0';
184205 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
184206
184207 /* Check if there were any options specified that should be interpreted
184208 ** here. Options that are interpreted here include "vfs" and those that
184209 ** correspond to flags that may be passed to the sqlite3_open_v2()
184210 ** method. */
184211 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
184212 while( zOpt[0] ){
184213 int nOpt = sqlite3Strlen30(zOpt);
184214 char *zVal = &zOpt[nOpt+1];
184215 int nVal = sqlite3Strlen30(zVal);
184216
184217 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
184218 zVfs = zVal;
184219 }else{
184220 struct OpenMode {
184221 const char *z;
184222 int mode;
184223 } *aMode = 0;
184224 char *zModeType = 0;
184225 int mask = 0;
184226 int limit = 0;
184227
184228 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
184229 static struct OpenMode aCacheMode[] = {
184230 { "shared", SQLITE_OPEN_SHAREDCACHE0x00020000 },
184231 { "private", SQLITE_OPEN_PRIVATECACHE0x00040000 },
184232 { 0, 0 }
184233 };
184234
184235 mask = SQLITE_OPEN_SHAREDCACHE0x00020000|SQLITE_OPEN_PRIVATECACHE0x00040000;
184236 aMode = aCacheMode;
184237 limit = mask;
184238 zModeType = "cache";
184239 }
184240 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
184241 static struct OpenMode aOpenMode[] = {
184242 { "ro", SQLITE_OPEN_READONLY0x00000001 },
184243 { "rw", SQLITE_OPEN_READWRITE0x00000002 },
184244 { "rwc", SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 },
184245 { "memory", SQLITE_OPEN_MEMORY0x00000080 },
184246 { 0, 0 }
184247 };
184248
184249 mask = SQLITE_OPEN_READONLY0x00000001 | SQLITE_OPEN_READWRITE0x00000002
184250 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_MEMORY0x00000080;
184251 aMode = aOpenMode;
184252 limit = mask & flags;
184253 zModeType = "access";
184254 }
184255
184256 if( aMode ){
184257 int i;
184258 int mode = 0;
184259 for(i=0; aMode[i].z; i++){
184260 const char *z = aMode[i].z;
184261 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
184262 mode = aMode[i].mode;
184263 break;
184264 }
184265 }
184266 if( mode==0 ){
184267 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
184268 rc = SQLITE_ERROR1;
184269 goto parse_uri_out;
184270 }
184271 if( (mode & ~SQLITE_OPEN_MEMORY0x00000080)>limit ){
184272 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
184273 zModeType, zVal);
184274 rc = SQLITE_PERM3;
184275 goto parse_uri_out;
184276 }
184277 flags = (flags & ~mask) | mode;
184278 }
184279 }
184280
184281 zOpt = &zVal[nVal+1];
184282 }
184283
184284 }else{
184285 zFile = sqlite3_malloc64(nUri+8);
184286 if( !zFile ) return SQLITE_NOMEM_BKPT7;
184287 memset(zFile, 0, 4);
184288 zFile += 4;
184289 if( nUri ){
184290 memcpy(zFile, zUri, nUri);
184291 }
184292 memset(zFile+nUri, 0, 4);
184293 flags &= ~SQLITE_OPEN_URI0x00000040;
184294 }
184295
184296 *ppVfs = sqlite3_vfs_find(zVfs);
184297 if( *ppVfs==0 ){
184298 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
184299 rc = SQLITE_ERROR1;
184300 }
184301 parse_uri_out:
184302 if( rc!=SQLITE_OK0 ){
184303 sqlite3_free_filename(zFile);
184304 zFile = 0;
184305 }
184306 *pFlags = flags;
184307 *pzFile = zFile;
184308 return rc;
184309}
184310
184311/*
184312** This routine does the core work of extracting URI parameters from a
184313** database filename for the sqlite3_uri_parameter() interface.
184314*/
184315static const char *uriParameter(const char *zFilename, const char *zParam){
184316 zFilename += sqlite3Strlen30(zFilename) + 1;
184317 while( ALWAYS(zFilename!=0)(zFilename!=0) && zFilename[0] ){
184318 int x = strcmp(zFilename, zParam);
184319 zFilename += sqlite3Strlen30(zFilename) + 1;
184320 if( x==0 ) return zFilename;
184321 zFilename += sqlite3Strlen30(zFilename) + 1;
184322 }
184323 return 0;
184324}
184325
184326
184327
184328/*
184329** This routine does the work of opening a database on behalf of
184330** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
184331** is UTF-8 encoded.
184332*/
184333static int openDatabase(
184334 const char *zFilename, /* Database filename UTF-8 encoded */
184335 sqlite3 **ppDb, /* OUT: Returned database handle */
184336 unsigned int flags, /* Operational flags */
184337 const char *zVfs /* Name of the VFS to use */
184338){
184339 sqlite3 *db; /* Store allocated handle here */
184340 int rc; /* Return code */
184341 int isThreadsafe; /* True for threadsafe connections */
184342 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
184343 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
184344 int i; /* Loop counter */
184345
184346#ifdef SQLITE_ENABLE_API_ARMOR1
184347 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184347);
184348#endif
184349 *ppDb = 0;
184350#ifndef SQLITE_OMIT_AUTOINIT
184351 rc = sqlite3_initialize();
184352 if( rc ) return rc;
184353#endif
184354
184355 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
184356 isThreadsafe = 0;
184357 }else if( flags & SQLITE_OPEN_NOMUTEX0x00008000 ){
184358 isThreadsafe = 0;
184359 }else if( flags & SQLITE_OPEN_FULLMUTEX0x00010000 ){
184360 isThreadsafe = 1;
184361 }else{
184362 isThreadsafe = sqlite3GlobalConfigsqlite3Config.bFullMutex;
184363 }
184364
184365 if( flags & SQLITE_OPEN_PRIVATECACHE0x00040000 ){
184366 flags &= ~SQLITE_OPEN_SHAREDCACHE0x00020000;
184367 }else if( sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled ){
184368 flags |= SQLITE_OPEN_SHAREDCACHE0x00020000;
184369 }
184370
184371 /* Remove harmful bits from the flags parameter
184372 **
184373 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
184374 ** dealt with in the previous code block. Besides these, the only
184375 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
184376 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
184377 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
184378 ** bits. Silently mask off all other flags.
184379 */
184380 flags &= ~( SQLITE_OPEN_DELETEONCLOSE0x00000008 |
184381 SQLITE_OPEN_EXCLUSIVE0x00000010 |
184382 SQLITE_OPEN_MAIN_DB0x00000100 |
184383 SQLITE_OPEN_TEMP_DB0x00000200 |
184384 SQLITE_OPEN_TRANSIENT_DB0x00000400 |
184385 SQLITE_OPEN_MAIN_JOURNAL0x00000800 |
184386 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
184387 SQLITE_OPEN_SUBJOURNAL0x00002000 |
184388 SQLITE_OPEN_SUPER_JOURNAL0x00004000 |
184389 SQLITE_OPEN_NOMUTEX0x00008000 |
184390 SQLITE_OPEN_FULLMUTEX0x00010000 |
184391 SQLITE_OPEN_WAL0x00080000
184392 );
184393
184394 /* Allocate the sqlite data structure */
184395 db = sqlite3MallocZero( sizeof(sqlite3) );
184396 if( db==0 ) goto opendb_out;
184397 if( isThreadsafe
184398#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
184399 || sqlite3GlobalConfigsqlite3Config.bCoreMutex
184400#endif
184401 ){
184402 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
184403 if( db->mutex==0 ){
184404 sqlite3_free(db);
184405 db = 0;
184406 goto opendb_out;
184407 }
184408 if( isThreadsafe==0 ){
184409 sqlite3MutexWarnOnContention(db->mutex);
184410 }
184411 }
184412 sqlite3_mutex_enter(db->mutex);
184413 db->errMask = (flags & SQLITE_OPEN_EXRESCODE0x02000000)!=0 ? 0xffffffff : 0xff;
184414 db->nDb = 2;
184415 db->eOpenState = SQLITE_STATE_BUSY0x6d;
184416 db->aDb = db->aDbStatic;
184417 db->lookaside.bDisable = 1;
184418 db->lookaside.sz = 0;
184419
184420 assert( sizeof(db->aLimit)==sizeof(aHardLimit) )((void) (0));
184421 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
184422 db->aLimit[SQLITE_LIMIT_WORKER_THREADS11] = SQLITE_DEFAULT_WORKER_THREADS0;
184423 db->autoCommit = 1;
184424 db->nextAutovac = -1;
184425 db->szMmap = sqlite3GlobalConfigsqlite3Config.szMmap;
184426 db->nextPagesize = 0;
184427 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
184428#ifdef SQLITE_ENABLE_SORTER_MMAP
184429 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
184430 ** the temporary files used to do external sorts (see code in vdbesort.c)
184431 ** is disabled. It can still be used either by defining
184432 ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the
184433 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
184434 db->nMaxSorterMmap = 0x7FFFFFFF;
184435#endif
184436 db->flags |= SQLITE_ShortColNames0x00000040
184437 | SQLITE_EnableTrigger0x00040000
184438 | SQLITE_EnableView0x80000000
184439 | SQLITE_CacheSpill0x00000020
184440#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
184441 | SQLITE_TrustedSchema0x00000080
184442#endif
184443/* The SQLITE_DQS compile-time option determines the default settings
184444** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
184445**
184446** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
184447** ---------- ----------------------- -----------------------
184448** undefined on on
184449** 3 on on
184450** 2 on off
184451** 1 off on
184452** 0 off off
184453**
184454** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
184455** and so that is the default. But developers are encouraged to use
184456** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
184457*/
184458#if !defined(SQLITE_DQS0)
184459# define SQLITE_DQS0 3
184460#endif
184461#if (SQLITE_DQS0&1)==1
184462 | SQLITE_DqsDML0x40000000
184463#endif
184464#if (SQLITE_DQS0&2)==2
184465 | SQLITE_DqsDDL0x20000000
184466#endif
184467
184468#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
184469 | SQLITE_AutoIndex0x00008000
184470#endif
184471#if SQLITE_DEFAULT_CKPTFULLFSYNC1
184472 | SQLITE_CkptFullFSync0x00000010
184473#endif
184474#if SQLITE_DEFAULT_FILE_FORMAT4<4
184475 | SQLITE_LegacyFileFmt0x00000002
184476#endif
184477#ifdef SQLITE_ENABLE_LOAD_EXTENSION
184478 | SQLITE_LoadExtension0x00010000
184479#endif
184480#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
184481 | SQLITE_RecTriggers0x00002000
184482#endif
184483#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
184484 | SQLITE_ForeignKeys0x00004000
184485#endif
184486#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
184487 | SQLITE_ReverseOrder0x00001000
184488#endif
184489#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
184490 | SQLITE_CellSizeCk0x00200000
184491#endif
184492#if defined(SQLITE_ENABLE_FTS3_TOKENIZER1)
184493 | SQLITE_Fts3Tokenizer0x00400000
184494#endif
184495#if defined(SQLITE_ENABLE_QPSG)
184496 | SQLITE_EnableQPSG0x00800000
184497#endif
184498#if defined(SQLITE_DEFAULT_DEFENSIVE)
184499 | SQLITE_Defensive0x10000000
184500#endif
184501#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
184502 | SQLITE_LegacyAlter0x04000000
184503#endif
184504#if defined(SQLITE_ENABLE_STMT_SCANSTATUS1)
184505 | SQLITE_StmtScanStatus0x00000400
184506#endif
184507 ;
184508 sqlite3HashInit(&db->aCollSeq);
184509#ifndef SQLITE_OMIT_VIRTUALTABLE
184510 sqlite3HashInit(&db->aModule);
184511#endif
184512
184513 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
184514 ** and UTF-16, so add a version for each to avoid any unnecessary
184515 ** conversions. The only error that can occur here is a malloc() failure.
184516 **
184517 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
184518 ** functions:
184519 */
184520 createCollation(db, sqlite3StrBINARY, SQLITE_UTF81, 0, binCollFunc, 0);
184521 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE3, 0, binCollFunc, 0);
184522 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE2, 0, binCollFunc, 0);
184523 createCollation(db, "NOCASE", SQLITE_UTF81, 0, nocaseCollatingFunc, 0);
184524 createCollation(db, "RTRIM", SQLITE_UTF81, 0, rtrimCollFunc, 0);
184525 if( db->mallocFailed ){
184526 goto opendb_out;
184527 }
184528
184529#if SQLITE_OS_UNIX1 && defined(SQLITE_OS_KV_OPTIONAL)
184530 /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
184531 if( zFilename && zFilename[0]==':' ){
184532 if( strcmp(zFilename, ":localStorage:")==0 ){
184533 zFilename = "file:local?vfs=kvvfs";
184534 flags |= SQLITE_OPEN_URI0x00000040;
184535 }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
184536 zFilename = "file:session?vfs=kvvfs";
184537 flags |= SQLITE_OPEN_URI0x00000040;
184538 }
184539 }
184540#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
184541
184542 /* Parse the filename/URI argument
184543 **
184544 ** Only allow sensible combinations of bits in the flags argument.
184545 ** Throw an error if any non-sense combination is used. If we
184546 ** do not block illegal combinations here, it could trigger
184547 ** assert() statements in deeper layers. Sensible combinations
184548 ** are:
184549 **
184550 ** 1: SQLITE_OPEN_READONLY
184551 ** 2: SQLITE_OPEN_READWRITE
184552 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
184553 */
184554 db->openFlags = flags;
184555 assert( SQLITE_OPEN_READONLY == 0x01 )((void) (0));
184556 assert( SQLITE_OPEN_READWRITE == 0x02 )((void) (0));
184557 assert( SQLITE_OPEN_CREATE == 0x04 )((void) (0));
184558 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
184559 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
184560 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
184561 if( ((1<<(flags&7)) & 0x46)==0 ){
184562 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(184562); /* IMP: R-18321-05872 */
184563 }else{
184564 if( zFilename==0 ) zFilename = ":memory:";
184565 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
184566 }
184567 if( rc!=SQLITE_OK0 ){
184568 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
184569 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
184570 sqlite3_free(zErrMsg);
184571 goto opendb_out;
184572 }
184573 assert( db->pVfs!=0 )((void) (0));
184574#if SQLITE_OS_KV0 || defined(SQLITE_OS_KV_OPTIONAL)
184575 if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
184576 db->temp_store = 2;
184577 }
184578#endif
184579
184580 /* Open the backend database driver */
184581 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
184582 flags | SQLITE_OPEN_MAIN_DB0x00000100);
184583 if( rc!=SQLITE_OK0 ){
184584 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
184585 rc = SQLITE_NOMEM_BKPT7;
184586 }
184587 sqlite3Error(db, rc);
184588 goto opendb_out;
184589 }
184590 sqlite3BtreeEnter(db->aDb[0].pBt);
184591 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
184592 if( !db->mallocFailed ){
184593 sqlite3SetTextEncoding(db, SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc));
184594 }
184595 sqlite3BtreeLeave(db->aDb[0].pBt);
184596 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
184597
184598 /* The default safety_level for the main database is FULL; for the temp
184599 ** database it is OFF. This matches the pager layer defaults.
184600 */
184601 db->aDb[0].zDbSName = "main";
184602 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
184603 db->aDb[1].zDbSName = "temp";
184604 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF0x01;
184605
184606 db->eOpenState = SQLITE_STATE_OPEN0x76;
184607 if( db->mallocFailed ){
184608 goto opendb_out;
184609 }
184610
184611 /* Register all built-in functions, but do not attempt to read the
184612 ** database schema yet. This is delayed until the first time the database
184613 ** is accessed.
184614 */
184615 sqlite3Error(db, SQLITE_OK0);
184616 sqlite3RegisterPerConnectionBuiltinFunctions(db);
184617 rc = sqlite3_errcode(db);
184618
184619
184620 /* Load compiled-in extensions */
184621 for(i=0; rc==SQLITE_OK0 && i<ArraySize(sqlite3BuiltinExtensions)((int)(sizeof(sqlite3BuiltinExtensions)/sizeof(sqlite3BuiltinExtensions
[0])))
; i++){
184622 rc = sqlite3BuiltinExtensions[i](db);
184623 }
184624
184625 /* Load automatic extensions - extensions that have been registered
184626 ** using the sqlite3_automatic_extension() API.
184627 */
184628 if( rc==SQLITE_OK0 ){
184629 sqlite3AutoLoadExtensions(db);
184630 rc = sqlite3_errcode(db);
184631 if( rc!=SQLITE_OK0 ){
184632 goto opendb_out;
184633 }
184634 }
184635
184636#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
184637 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
184638 ** option gives access to internal functions by default.
184639 ** Testing use only!!! */
184640 db->mDbFlags |= DBFLAG_InternalFunc0x0020;
184641#endif
184642
184643 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
184644 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
184645 ** mode. Doing nothing at all also makes NORMAL the default.
184646 */
184647#ifdef SQLITE_DEFAULT_LOCKING_MODE
184648 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
184649 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
184650 SQLITE_DEFAULT_LOCKING_MODE);
184651#endif
184652
184653 if( rc ) sqlite3Error(db, rc);
184654
184655 /* Enable the lookaside-malloc subsystem */
184656 setupLookaside(db, 0, sqlite3GlobalConfigsqlite3Config.szLookaside,
184657 sqlite3GlobalConfigsqlite3Config.nLookaside);
184658
184659 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000);
184660
184661opendb_out:
184662 if( db ){
184663 assert( db->mutex!=0 || isThreadsafe==0((void) (0))
184664 || sqlite3GlobalConfig.bFullMutex==0 )((void) (0));
184665 sqlite3_mutex_leave(db->mutex);
184666 }
184667 rc = sqlite3_errcode(db);
184668 assert( db!=0 || (rc&0xff)==SQLITE_NOMEM )((void) (0));
184669 if( (rc&0xff)==SQLITE_NOMEM7 ){
184670 sqlite3_close(db);
184671 db = 0;
184672 }else if( rc!=SQLITE_OK0 ){
184673 db->eOpenState = SQLITE_STATE_SICK0xba;
184674 }
184675 *ppDb = db;
184676#ifdef SQLITE_ENABLE_SQLLOG
184677 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
184678 /* Opening a db handle. Fourth parameter is passed 0. */
184679 void *pArg = sqlite3GlobalConfigsqlite3Config.pSqllogArg;
184680 sqlite3GlobalConfigsqlite3Config.xSqllog(pArg, db, zFilename, 0);
184681 }
184682#endif
184683 sqlite3_free_filename(zOpen);
184684 return rc;
184685}
184686
184687
184688/*
184689** Open a new database handle.
184690*/
184691SQLITE_API int sqlite3_open(
184692 const char *zFilename,
184693 sqlite3 **ppDb
184694){
184695 return openDatabase(zFilename, ppDb,
184696 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
184697}
184698SQLITE_API int sqlite3_open_v2(
184699 const char *filename, /* Database filename (UTF-8) */
184700 sqlite3 **ppDb, /* OUT: SQLite db handle */
184701 int flags, /* Flags */
184702 const char *zVfs /* Name of VFS module to use */
184703){
184704 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
184705}
184706
184707#ifndef SQLITE_OMIT_UTF16
184708/*
184709** Open a new database handle.
184710*/
184711SQLITE_API int sqlite3_open16(
184712 const void *zFilename,
184713 sqlite3 **ppDb
184714){
184715 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
184716 sqlite3_value *pVal;
184717 int rc;
184718
184719#ifdef SQLITE_ENABLE_API_ARMOR1
184720 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184720);
184721#endif
184722 *ppDb = 0;
184723#ifndef SQLITE_OMIT_AUTOINIT
184724 rc = sqlite3_initialize();
184725 if( rc ) return rc;
184726#endif
184727 if( zFilename==0 ) zFilename = "\000\000";
184728 pVal = sqlite3ValueNew(0);
184729 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
184730 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF81);
184731 if( zFilename8 ){
184732 rc = openDatabase(zFilename8, ppDb,
184733 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
184734 assert( *ppDb || rc==SQLITE_NOMEM )((void) (0));
184735 if( rc==SQLITE_OK0 && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded)(((*ppDb)->aDb[0].pSchema->schemaFlags&(0x0001))==(
0x0001))
){
184736 SCHEMA_ENC(*ppDb)((*ppDb)->aDb[0].pSchema->enc) = ENC(*ppDb)((*ppDb)->enc) = SQLITE_UTF16NATIVE2;
184737 }
184738 }else{
184739 rc = SQLITE_NOMEM_BKPT7;
184740 }
184741 sqlite3ValueFree(pVal);
184742
184743 return rc & 0xff;
184744}
184745#endif /* SQLITE_OMIT_UTF16 */
184746
184747/*
184748** Register a new collation sequence with the database handle db.
184749*/
184750SQLITE_API int sqlite3_create_collation(
184751 sqlite3* db,
184752 const char *zName,
184753 int enc,
184754 void* pCtx,
184755 int(*xCompare)(void*,int,const void*,int,const void*)
184756){
184757 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
184758}
184759
184760/*
184761** Register a new collation sequence with the database handle db.
184762*/
184763SQLITE_API int sqlite3_create_collation_v2(
184764 sqlite3* db,
184765 const char *zName,
184766 int enc,
184767 void* pCtx,
184768 int(*xCompare)(void*,int,const void*,int,const void*),
184769 void(*xDel)(void*)
184770){
184771 int rc;
184772
184773#ifdef SQLITE_ENABLE_API_ARMOR1
184774 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184774);
184775#endif
184776 sqlite3_mutex_enter(db->mutex);
184777 assert( !db->mallocFailed )((void) (0));
184778 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
184779 rc = sqlite3ApiExit(db, rc);
184780 sqlite3_mutex_leave(db->mutex);
184781 return rc;
184782}
184783
184784#ifndef SQLITE_OMIT_UTF16
184785/*
184786** Register a new collation sequence with the database handle db.
184787*/
184788SQLITE_API int sqlite3_create_collation16(
184789 sqlite3* db,
184790 const void *zName,
184791 int enc,
184792 void* pCtx,
184793 int(*xCompare)(void*,int,const void*,int,const void*)
184794){
184795 int rc = SQLITE_OK0;
184796 char *zName8;
184797
184798#ifdef SQLITE_ENABLE_API_ARMOR1
184799 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184799);
184800#endif
184801 sqlite3_mutex_enter(db->mutex);
184802 assert( !db->mallocFailed )((void) (0));
184803 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE2);
184804 if( zName8 ){
184805 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
184806 sqlite3DbFree(db, zName8);
184807 }
184808 rc = sqlite3ApiExit(db, rc);
184809 sqlite3_mutex_leave(db->mutex);
184810 return rc;
184811}
184812#endif /* SQLITE_OMIT_UTF16 */
184813
184814/*
184815** Register a collation sequence factory callback with the database handle
184816** db. Replace any previously installed collation sequence factory.
184817*/
184818SQLITE_API int sqlite3_collation_needed(
184819 sqlite3 *db,
184820 void *pCollNeededArg,
184821 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
184822){
184823#ifdef SQLITE_ENABLE_API_ARMOR1
184824 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184824);
184825#endif
184826 sqlite3_mutex_enter(db->mutex);
184827 db->xCollNeeded = xCollNeeded;
184828 db->xCollNeeded16 = 0;
184829 db->pCollNeededArg = pCollNeededArg;
184830 sqlite3_mutex_leave(db->mutex);
184831 return SQLITE_OK0;
184832}
184833
184834#ifndef SQLITE_OMIT_UTF16
184835/*
184836** Register a collation sequence factory callback with the database handle
184837** db. Replace any previously installed collation sequence factory.
184838*/
184839SQLITE_API int sqlite3_collation_needed16(
184840 sqlite3 *db,
184841 void *pCollNeededArg,
184842 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
184843){
184844#ifdef SQLITE_ENABLE_API_ARMOR1
184845 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(184845);
184846#endif
184847 sqlite3_mutex_enter(db->mutex);
184848 db->xCollNeeded = 0;
184849 db->xCollNeeded16 = xCollNeeded16;
184850 db->pCollNeededArg = pCollNeededArg;
184851 sqlite3_mutex_leave(db->mutex);
184852 return SQLITE_OK0;
184853}
184854#endif /* SQLITE_OMIT_UTF16 */
184855
184856/*
184857** Find existing client data.
184858*/
184859SQLITE_API void *sqlite3_get_clientdata(sqlite3 *db, const char *zName){
184860 DbClientData *p;
184861 sqlite3_mutex_enter(db->mutex);
184862 for(p=db->pDbData; p; p=p->pNext){
184863 if( strcmp(p->zName, zName)==0 ){
184864 void *pResult = p->pData;
184865 sqlite3_mutex_leave(db->mutex);
184866 return pResult;
184867 }
184868 }
184869 sqlite3_mutex_leave(db->mutex);
184870 return 0;
184871}
184872
184873/*
184874** Add new client data to a database connection.
184875*/
184876SQLITE_API int sqlite3_set_clientdata(
184877 sqlite3 *db, /* Attach client data to this connection */
184878 const char *zName, /* Name of the client data */
184879 void *pData, /* The client data itself */
184880 void (*xDestructor)(void*) /* Destructor */
184881){
184882 DbClientData *p, **pp;
184883 sqlite3_mutex_enter(db->mutex);
184884 pp = &db->pDbData;
184885 for(p=db->pDbData; p && strcmp(p->zName,zName); p=p->pNext){
184886 pp = &p->pNext;
184887 }
184888 if( p ){
184889 assert( p->pData!=0 )((void) (0));
184890 if( p->xDestructor ) p->xDestructor(p->pData);
184891 if( pData==0 ){
184892 *pp = p->pNext;
184893 sqlite3_free(p);
184894 sqlite3_mutex_leave(db->mutex);
184895 return SQLITE_OK0;
184896 }
184897 }else if( pData==0 ){
184898 sqlite3_mutex_leave(db->mutex);
184899 return SQLITE_OK0;
184900 }else{
184901 size_t n = strlen(zName);
184902 p = sqlite3_malloc64( sizeof(DbClientData)+n+1 );
184903 if( p==0 ){
184904 if( xDestructor ) xDestructor(pData);
184905 sqlite3_mutex_leave(db->mutex);
184906 return SQLITE_NOMEM7;
184907 }
184908 memcpy(p->zName, zName, n+1);
184909 p->pNext = db->pDbData;
184910 db->pDbData = p;
184911 }
184912 p->pData = pData;
184913 p->xDestructor = xDestructor;
184914 sqlite3_mutex_leave(db->mutex);
184915 return SQLITE_OK0;
184916}
184917
184918
184919#ifndef SQLITE_OMIT_DEPRECATED1
184920/*
184921** This function is now an anachronism. It used to be used to recover from a
184922** malloc() failure, but SQLite now does this automatically.
184923*/
184924SQLITE_API int sqlite3_global_recover(void){
184925 return SQLITE_OK0;
184926}
184927#endif
184928
184929/*
184930** Test to see whether or not the database connection is in autocommit
184931** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
184932** by default. Autocommit is disabled by a BEGIN statement and reenabled
184933** by the next COMMIT or ROLLBACK.
184934*/
184935SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
184936#ifdef SQLITE_ENABLE_API_ARMOR1
184937 if( !sqlite3SafetyCheckOk(db) ){
184938 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(184938);
184939 return 0;
184940 }
184941#endif
184942 return db->autoCommit;
184943}
184944
184945/*
184946** The following routines are substitutes for constants SQLITE_CORRUPT,
184947** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
184948** constants. They serve two purposes:
184949**
184950** 1. Serve as a convenient place to set a breakpoint in a debugger
184951** to detect when version error conditions occurs.
184952**
184953** 2. Invoke sqlite3_log() to provide the source code location where
184954** a low-level error is first detected.
184955*/
184956SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType){
184957 sqlite3_log(iErr, "%s at line %d of [%.10s]",
184958 zType, lineno, 20+sqlite3_sourceid());
184959 return iErr;
184960}
184961SQLITE_PRIVATEstatic int sqlite3CorruptError(int lineno){
184962 testcase( sqlite3GlobalConfig.xLog!=0 );
184963 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, "database corruption");
184964}
184965SQLITE_PRIVATEstatic int sqlite3MisuseError(int lineno){
184966 testcase( sqlite3GlobalConfig.xLog!=0 );
184967 return sqlite3ReportError(SQLITE_MISUSE21, lineno, "misuse");
184968}
184969SQLITE_PRIVATEstatic int sqlite3CantopenError(int lineno){
184970 testcase( sqlite3GlobalConfig.xLog!=0 );
184971 return sqlite3ReportError(SQLITE_CANTOPEN14, lineno, "cannot open file");
184972}
184973#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
184974SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
184975 char zMsg[100];
184976 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
184977 testcase( sqlite3GlobalConfig.xLog!=0 );
184978 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
184979}
184980#endif
184981#ifdef SQLITE_DEBUG
184982SQLITE_PRIVATEstatic int sqlite3NomemError(int lineno){
184983 testcase( sqlite3GlobalConfig.xLog!=0 );
184984 return sqlite3ReportError(SQLITE_NOMEM7, lineno, "OOM");
184985}
184986SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int lineno){
184987 testcase( sqlite3GlobalConfig.xLog!=0 );
184988 return sqlite3ReportError(SQLITE_IOERR_NOMEM(10 | (12<<8)), lineno, "I/O OOM error");
184989}
184990#endif
184991
184992#ifndef SQLITE_OMIT_DEPRECATED1
184993/*
184994** This is a convenience routine that makes sure that all thread-specific
184995** data for this thread has been deallocated.
184996**
184997** SQLite no longer uses thread-specific data so this routine is now a
184998** no-op. It is retained for historical compatibility.
184999*/
185000SQLITE_API void sqlite3_thread_cleanup(void){
185001}
185002#endif
185003
185004/*
185005** Return meta information about a specific column of a database table.
185006** See comment in sqlite3.h (sqlite.h.in) for details.
185007*/
185008SQLITE_API int sqlite3_table_column_metadata(
185009 sqlite3 *db, /* Connection handle */
185010 const char *zDbName, /* Database name or NULL */
185011 const char *zTableName, /* Table name */
185012 const char *zColumnName, /* Column name */
185013 char const **pzDataType, /* OUTPUT: Declared data type */
185014 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
185015 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
185016 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
185017 int *pAutoinc /* OUTPUT: True if column is auto-increment */
185018){
185019 int rc;
185020 char *zErrMsg = 0;
185021 Table *pTab = 0;
185022 Column *pCol = 0;
185023 int iCol = 0;
185024 char const *zDataType = 0;
185025 char const *zCollSeq = 0;
185026 int notnull = 0;
185027 int primarykey = 0;
185028 int autoinc = 0;
185029
185030
185031#ifdef SQLITE_ENABLE_API_ARMOR1
185032 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
185033 return SQLITE_MISUSE_BKPTsqlite3MisuseError(185033);
185034 }
185035#endif
185036
185037 /* Ensure the database schema has been loaded */
185038 sqlite3_mutex_enter(db->mutex);
185039 sqlite3BtreeEnterAll(db);
185040 rc = sqlite3Init(db, &zErrMsg);
185041 if( SQLITE_OK0!=rc ){
185042 goto error_out;
185043 }
185044
185045 /* Locate the table in question */
185046 pTab = sqlite3FindTable(db, zTableName, zDbName);
185047 if( !pTab || IsView(pTab)((pTab)->eTabType==2) ){
185048 pTab = 0;
185049 goto error_out;
185050 }
185051
185052 /* Find the column for which info is requested */
185053 if( zColumnName==0 ){
185054 /* Query for existence of table only */
185055 }else{
185056 for(iCol=0; iCol<pTab->nCol; iCol++){
185057 pCol = &pTab->aCol[iCol];
185058 if( 0==sqlite3StrICmp(pCol->zCnName, zColumnName) ){
185059 break;
185060 }
185061 }
185062 if( iCol==pTab->nCol ){
185063 if( HasRowid(pTab)(((pTab)->tabFlags & 0x00000080)==0) && sqlite3IsRowid(zColumnName) ){
185064 iCol = pTab->iPKey;
185065 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
185066 }else{
185067 pTab = 0;
185068 goto error_out;
185069 }
185070 }
185071 }
185072
185073 /* The following block stores the meta information that will be returned
185074 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
185075 ** and autoinc. At this point there are two possibilities:
185076 **
185077 ** 1. The specified column name was rowid", "oid" or "_rowid_"
185078 ** and there is no explicitly declared IPK column.
185079 **
185080 ** 2. The table is not a view and the column name identified an
185081 ** explicitly declared column. Copy meta information from *pCol.
185082 */
185083 if( pCol ){
185084 zDataType = sqlite3ColumnType(pCol,0);
185085 zCollSeq = sqlite3ColumnColl(pCol);
185086 notnull = pCol->notNull!=0;
185087 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0;
185088 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement0x00000008)!=0;
185089 }else{
185090 zDataType = "INTEGER";
185091 primarykey = 1;
185092 }
185093 if( !zCollSeq ){
185094 zCollSeq = sqlite3StrBINARY;
185095 }
185096
185097error_out:
185098 sqlite3BtreeLeaveAll(db);
185099
185100 /* Whether the function call succeeded or failed, set the output parameters
185101 ** to whatever their local counterparts contain. If an error did occur,
185102 ** this has the effect of zeroing all output parameters.
185103 */
185104 if( pzDataType ) *pzDataType = zDataType;
185105 if( pzCollSeq ) *pzCollSeq = zCollSeq;
185106 if( pNotNull ) *pNotNull = notnull;
185107 if( pPrimaryKey ) *pPrimaryKey = primarykey;
185108 if( pAutoinc ) *pAutoinc = autoinc;
185109
185110 if( SQLITE_OK0==rc && !pTab ){
185111 sqlite3DbFree(db, zErrMsg);
185112 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
185113 zColumnName);
185114 rc = SQLITE_ERROR1;
185115 }
185116 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
185117 sqlite3DbFree(db, zErrMsg);
185118 rc = sqlite3ApiExit(db, rc);
185119 sqlite3_mutex_leave(db->mutex);
185120 return rc;
185121}
185122
185123/*
185124** Sleep for a little while. Return the amount of time slept.
185125*/
185126SQLITE_API int sqlite3_sleep(int ms){
185127 sqlite3_vfs *pVfs;
185128 int rc;
185129 pVfs = sqlite3_vfs_find(0);
185130 if( pVfs==0 ) return 0;
185131
185132 /* This function works in milliseconds, but the underlying OsSleep()
185133 ** API uses microseconds. Hence the 1000's.
185134 */
185135 rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000);
185136 return rc;
185137}
185138
185139/*
185140** Enable or disable the extended result codes.
185141*/
185142SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
185143#ifdef SQLITE_ENABLE_API_ARMOR1
185144 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(185144);
185145#endif
185146 sqlite3_mutex_enter(db->mutex);
185147 db->errMask = onoff ? 0xffffffff : 0xff;
185148 sqlite3_mutex_leave(db->mutex);
185149 return SQLITE_OK0;
185150}
185151
185152/*
185153** Invoke the xFileControl method on a particular database.
185154*/
185155SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
185156 int rc = SQLITE_ERROR1;
185157 Btree *pBtree;
185158
185159#ifdef SQLITE_ENABLE_API_ARMOR1
185160 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(185160);
185161#endif
185162 sqlite3_mutex_enter(db->mutex);
185163 pBtree = sqlite3DbNameToBtree(db, zDbName);
185164 if( pBtree ){
185165 Pager *pPager;
185166 sqlite3_file *fd;
185167 sqlite3BtreeEnter(pBtree);
185168 pPager = sqlite3BtreePager(pBtree);
185169 assert( pPager!=0 )((void) (0));
185170 fd = sqlite3PagerFile(pPager);
185171 assert( fd!=0 )((void) (0));
185172 if( op==SQLITE_FCNTL_FILE_POINTER7 ){
185173 *(sqlite3_file**)pArg = fd;
185174 rc = SQLITE_OK0;
185175 }else if( op==SQLITE_FCNTL_VFS_POINTER27 ){
185176 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
185177 rc = SQLITE_OK0;
185178 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER28 ){
185179 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
185180 rc = SQLITE_OK0;
185181 }else if( op==SQLITE_FCNTL_DATA_VERSION35 ){
185182 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
185183 rc = SQLITE_OK0;
185184 }else if( op==SQLITE_FCNTL_RESERVE_BYTES38 ){
185185 int iNew = *(int*)pArg;
185186 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
185187 if( iNew>=0 && iNew<=255 ){
185188 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
185189 }
185190 rc = SQLITE_OK0;
185191 }else if( op==SQLITE_FCNTL_RESET_CACHE42 ){
185192 sqlite3BtreeClearCache(pBtree);
185193 rc = SQLITE_OK0;
185194 }else{
185195 int nSave = db->busyHandler.nBusy;
185196 rc = sqlite3OsFileControl(fd, op, pArg);
185197 db->busyHandler.nBusy = nSave;
185198 }
185199 sqlite3BtreeLeave(pBtree);
185200 }
185201 sqlite3_mutex_leave(db->mutex);
185202 return rc;
185203}
185204
185205/*
185206** Interface to the testing logic.
185207*/
185208SQLITE_API int sqlite3_test_control(int op, ...){
185209 int rc = 0;
185210#ifdef SQLITE_UNTESTABLE
185211 UNUSED_PARAMETER(op)(void)(op);
185212#else
185213 va_list ap;
185214 va_start(ap, op)__builtin_va_start(ap, op);
185215 switch( op ){
185216
185217 /*
185218 ** Save the current state of the PRNG.
185219 */
185220 case SQLITE_TESTCTRL_PRNG_SAVE5: {
185221 sqlite3PrngSaveState();
185222 break;
185223 }
185224
185225 /*
185226 ** Restore the state of the PRNG to the last state saved using
185227 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
185228 ** this verb acts like PRNG_RESET.
185229 */
185230 case SQLITE_TESTCTRL_PRNG_RESTORE6: {
185231 sqlite3PrngRestoreState();
185232 break;
185233 }
185234
185235 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
185236 **
185237 ** Control the seed for the pseudo-random number generator (PRNG) that
185238 ** is built into SQLite. Cases:
185239 **
185240 ** x!=0 && db!=0 Seed the PRNG to the current value of the
185241 ** schema cookie in the main database for db, or
185242 ** x if the schema cookie is zero. This case
185243 ** is convenient to use with database fuzzers
185244 ** as it allows the fuzzer some control over the
185245 ** the PRNG seed.
185246 **
185247 ** x!=0 && db==0 Seed the PRNG to the value of x.
185248 **
185249 ** x==0 && db==0 Revert to default behavior of using the
185250 ** xRandomness method on the primary VFS.
185251 **
185252 ** This test-control also resets the PRNG so that the new seed will
185253 ** be used for the next call to sqlite3_randomness().
185254 */
185255#ifndef SQLITE_OMIT_WSD
185256 case SQLITE_TESTCTRL_PRNG_SEED28: {
185257 int x = va_arg(ap, int)__builtin_va_arg(ap, int);
185258 int y;
185259 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185260 assert( db==0 || db->aDb[0].pSchema!=0 )((void) (0));
185261 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
185262 sqlite3Config.iPrngSeed = x;
185263 sqlite3_randomness(0,0);
185264 break;
185265 }
185266#endif
185267
185268 /* sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, sqlite3 *db, int b);
185269 **
185270 ** If b is true, then activate the SQLITE_FkNoAction setting. If b is
185271 ** false then clearn that setting. If the SQLITE_FkNoAction setting is
185272 ** abled, all foreign key ON DELETE and ON UPDATE actions behave as if
185273 ** they were NO ACTION, regardless of how they are defined.
185274 **
185275 ** NB: One must usually run "PRAGMA writable_schema=RESET" after
185276 ** using this test-control, before it will take full effect. failing
185277 ** to reset the schema can result in some unexpected behavior.
185278 */
185279 case SQLITE_TESTCTRL_FK_NO_ACTION7: {
185280 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185281 int b = va_arg(ap, int)__builtin_va_arg(ap, int);
185282 if( b ){
185283 db->flags |= SQLITE_FkNoAction((u64)(0x00008)<<32);
185284 }else{
185285 db->flags &= ~SQLITE_FkNoAction((u64)(0x00008)<<32);
185286 }
185287 break;
185288 }
185289
185290 /*
185291 ** sqlite3_test_control(BITVEC_TEST, size, program)
185292 **
185293 ** Run a test against a Bitvec object of size. The program argument
185294 ** is an array of integers that defines the test. Return -1 on a
185295 ** memory allocation error, 0 on success, or non-zero for an error.
185296 ** See the sqlite3BitvecBuiltinTest() for additional information.
185297 */
185298 case SQLITE_TESTCTRL_BITVEC_TEST8: {
185299 int sz = va_arg(ap, int)__builtin_va_arg(ap, int);
185300 int *aProg = va_arg(ap, int*)__builtin_va_arg(ap, int*);
185301 rc = sqlite3BitvecBuiltinTest(sz, aProg);
185302 break;
185303 }
185304
185305 /*
185306 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
185307 **
185308 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
185309 ** if xCallback is not NULL.
185310 **
185311 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
185312 ** is called immediately after installing the new callback and the return
185313 ** value from sqlite3FaultSim(0) becomes the return from
185314 ** sqlite3_test_control().
185315 */
185316 case SQLITE_TESTCTRL_FAULT_INSTALL9: {
185317 /* A bug in MSVC prevents it from understanding pointers to functions
185318 ** types in the second argument to va_arg(). Work around the problem
185319 ** using a typedef.
185320 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
185321 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
185322 ** of the link above to see the original text.
185323 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
185324 */
185325 typedef int(*sqlite3FaultFuncType)(int);
185326 sqlite3GlobalConfigsqlite3Config.xTestCallback = va_arg(ap, sqlite3FaultFuncType)__builtin_va_arg(ap, sqlite3FaultFuncType);
185327 rc = sqlite3FaultSim(0);
185328 break;
185329 }
185330
185331 /*
185332 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
185333 **
185334 ** Register hooks to call to indicate which malloc() failures
185335 ** are benign.
185336 */
185337 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10: {
185338 typedef void (*void_function)(void);
185339 void_function xBenignBegin;
185340 void_function xBenignEnd;
185341 xBenignBegin = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
185342 xBenignEnd = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
185343 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
185344 break;
185345 }
185346
185347 /*
185348 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
185349 **
185350 ** Set the PENDING byte to the value in the argument, if X>0.
185351 ** Make no changes if X==0. Return the value of the pending byte
185352 ** as it existing before this routine was called.
185353 **
185354 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
185355 ** an incompatible database file format. Changing the PENDING byte
185356 ** while any database connection is open results in undefined and
185357 ** deleterious behavior.
185358 */
185359 case SQLITE_TESTCTRL_PENDING_BYTE11: {
185360 rc = PENDING_BYTEsqlite3PendingByte;
185361#ifndef SQLITE_OMIT_WSD
185362 {
185363 unsigned int newVal = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
185364 if( newVal ) sqlite3PendingByte = newVal;
185365 }
185366#endif
185367 break;
185368 }
185369
185370 /*
185371 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
185372 **
185373 ** This action provides a run-time test to see whether or not
185374 ** assert() was enabled at compile-time. If X is true and assert()
185375 ** is enabled, then the return value is true. If X is true and
185376 ** assert() is disabled, then the return value is zero. If X is
185377 ** false and assert() is enabled, then the assertion fires and the
185378 ** process aborts. If X is false and assert() is disabled, then the
185379 ** return value is zero.
185380 */
185381 case SQLITE_TESTCTRL_ASSERT12: {
185382 volatile int x = 0;
185383 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 )((void) (0));
185384 rc = x;
185385#if defined(SQLITE_DEBUG)
185386 /* Invoke these debugging routines so that the compiler does not
185387 ** issue "defined but not used" warnings. */
185388 if( x==9999 ){
185389 sqlite3ShowExpr(0);
185390 sqlite3ShowExpr(0);
185391 sqlite3ShowExprList(0);
185392 sqlite3ShowIdList(0);
185393 sqlite3ShowSrcList(0);
185394 sqlite3ShowWith(0);
185395 sqlite3ShowUpsert(0);
185396#ifndef SQLITE_OMIT_TRIGGER
185397 sqlite3ShowTriggerStep(0);
185398 sqlite3ShowTriggerStepList(0);
185399 sqlite3ShowTrigger(0);
185400 sqlite3ShowTriggerList(0);
185401#endif
185402#ifndef SQLITE_OMIT_WINDOWFUNC
185403 sqlite3ShowWindow(0);
185404 sqlite3ShowWinFunc(0);
185405#endif
185406 sqlite3ShowSelect(0);
185407 }
185408#endif
185409 break;
185410 }
185411
185412
185413 /*
185414 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
185415 **
185416 ** This action provides a run-time test to see how the ALWAYS and
185417 ** NEVER macros were defined at compile-time.
185418 **
185419 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
185420 **
185421 ** The recommended test is X==2. If the return value is 2, that means
185422 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
185423 ** default setting. If the return value is 1, then ALWAYS() is either
185424 ** hard-coded to true or else it asserts if its argument is false.
185425 ** The first behavior (hard-coded to true) is the case if
185426 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
185427 ** behavior (assert if the argument to ALWAYS() is false) is the case if
185428 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
185429 **
185430 ** The run-time test procedure might look something like this:
185431 **
185432 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
185433 ** // ALWAYS() and NEVER() are no-op pass-through macros
185434 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
185435 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
185436 ** }else{
185437 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
185438 ** }
185439 */
185440 case SQLITE_TESTCTRL_ALWAYS13: {
185441 int x = va_arg(ap,int)__builtin_va_arg(ap, int);
185442 rc = x ? ALWAYS(x)(x) : 0;
185443 break;
185444 }
185445
185446 /*
185447 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
185448 **
185449 ** The integer returned reveals the byte-order of the computer on which
185450 ** SQLite is running:
185451 **
185452 ** 1 big-endian, determined at run-time
185453 ** 10 little-endian, determined at run-time
185454 ** 432101 big-endian, determined at compile-time
185455 ** 123410 little-endian, determined at compile-time
185456 */
185457 case SQLITE_TESTCTRL_BYTEORDER22: {
185458 rc = SQLITE_BYTEORDER1234*100 + SQLITE_LITTLEENDIAN1*10 + SQLITE_BIGENDIAN0;
185459 break;
185460 }
185461
185462 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
185463 **
185464 ** Enable or disable various optimizations for testing purposes. The
185465 ** argument N is a bitmask of optimizations to be disabled. For normal
185466 ** operation N should be 0. The idea is that a test program (like the
185467 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
185468 ** with various optimizations disabled to verify that the same answer
185469 ** is obtained in every case.
185470 */
185471 case SQLITE_TESTCTRL_OPTIMIZATIONS15: {
185472 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185473 db->dbOptFlags = va_arg(ap, u32)__builtin_va_arg(ap, u32);
185474 break;
185475 }
185476
185477 /* sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, int *N)
185478 **
185479 ** Write the current optimization settings into *N. A zero bit means that
185480 ** the optimization is on, and a 1 bit means that the optimization is off.
185481 */
185482 case SQLITE_TESTCTRL_GETOPT16: {
185483 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185484 int *pN = va_arg(ap, int*)__builtin_va_arg(ap, int*);
185485 *pN = db->dbOptFlags;
185486 break;
185487 }
185488
185489 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt);
185490 **
185491 ** If parameter onoff is 1, subsequent calls to localtime() fail.
185492 ** If 2, then invoke xAlt() instead of localtime(). If 0, normal
185493 ** processing.
185494 **
185495 ** xAlt arguments are void pointers, but they really want to be:
185496 **
185497 ** int xAlt(const time_t*, struct tm*);
185498 **
185499 ** xAlt should write results in to struct tm object of its 2nd argument
185500 ** and return zero on success, or return non-zero on failure.
185501 */
185502 case SQLITE_TESTCTRL_LOCALTIME_FAULT18: {
185503 sqlite3GlobalConfigsqlite3Config.bLocaltimeFault = va_arg(ap, int)__builtin_va_arg(ap, int);
185504 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault==2 ){
185505 typedef int(*sqlite3LocaltimeType)(const void*,void*);
185506 sqlite3GlobalConfigsqlite3Config.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType)__builtin_va_arg(ap, sqlite3LocaltimeType);
185507 }else{
185508 sqlite3GlobalConfigsqlite3Config.xAltLocaltime = 0;
185509 }
185510 break;
185511 }
185512
185513 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
185514 **
185515 ** Toggle the ability to use internal functions on or off for
185516 ** the database connection given in the argument.
185517 */
185518 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17: {
185519 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185520 db->mDbFlags ^= DBFLAG_InternalFunc0x0020;
185521 break;
185522 }
185523
185524 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
185525 **
185526 ** Set or clear a flag that indicates that the database file is always well-
185527 ** formed and never corrupt. This flag is clear by default, indicating that
185528 ** database files might have arbitrary corruption. Setting the flag during
185529 ** testing causes certain assert() statements in the code to be activated
185530 ** that demonstrate invariants on well-formed database files.
185531 */
185532 case SQLITE_TESTCTRL_NEVER_CORRUPT20: {
185533 sqlite3GlobalConfigsqlite3Config.neverCorrupt = va_arg(ap, int)__builtin_va_arg(ap, int);
185534 break;
185535 }
185536
185537 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
185538 **
185539 ** Set or clear a flag that causes SQLite to verify that type, name,
185540 ** and tbl_name fields of the sqlite_schema table. This is normally
185541 ** on, but it is sometimes useful to turn it off for testing.
185542 **
185543 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
185544 ** verification of rootpage numbers when parsing the schema. This
185545 ** is useful to make it easier to reach strange internal error states
185546 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
185547 ** in production.
185548 */
185549 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS29: {
185550 sqlite3GlobalConfigsqlite3Config.bExtraSchemaChecks = va_arg(ap, int)__builtin_va_arg(ap, int);
185551 break;
185552 }
185553
185554 /* Set the threshold at which OP_Once counters reset back to zero.
185555 ** By default this is 0x7ffffffe (over 2 billion), but that value is
185556 ** too big to test in a reasonable amount of time, so this control is
185557 ** provided to set a small and easily reachable reset value.
185558 */
185559 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19: {
185560 sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold = va_arg(ap, int)__builtin_va_arg(ap, int);
185561 break;
185562 }
185563
185564 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
185565 **
185566 ** Set the VDBE coverage callback function to xCallback with context
185567 ** pointer ptr.
185568 */
185569 case SQLITE_TESTCTRL_VDBE_COVERAGE21: {
185570#ifdef SQLITE_VDBE_COVERAGE
185571 typedef void (*branch_callback)(void*,unsigned int,
185572 unsigned char,unsigned char);
185573 sqlite3GlobalConfigsqlite3Config.xVdbeBranch = va_arg(ap,branch_callback)__builtin_va_arg(ap, branch_callback);
185574 sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg = va_arg(ap,void*)__builtin_va_arg(ap, void*);
185575#endif
185576 break;
185577 }
185578
185579 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
185580 case SQLITE_TESTCTRL_SORTER_MMAP24: {
185581 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185582 db->nMaxSorterMmap = va_arg(ap, int)__builtin_va_arg(ap, int);
185583 break;
185584 }
185585
185586 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
185587 **
185588 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
185589 ** not.
185590 */
185591 case SQLITE_TESTCTRL_ISINIT23: {
185592 if( sqlite3GlobalConfigsqlite3Config.isInit==0 ) rc = SQLITE_ERROR1;
185593 break;
185594 }
185595
185596 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
185597 **
185598 ** This test control is used to create imposter tables. "db" is a pointer
185599 ** to the database connection. dbName is the database name (ex: "main" or
185600 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
185601 ** or off. "tnum" is the root page of the b-tree to which the imposter
185602 ** table should connect.
185603 **
185604 ** Enable imposter mode only when the schema has already been parsed. Then
185605 ** run a single CREATE TABLE statement to construct the imposter table in
185606 ** the parsed schema. Then turn imposter mode back off again.
185607 **
185608 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
185609 ** the schema to be reparsed the next time it is needed. This has the
185610 ** effect of erasing all imposter tables.
185611 */
185612 case SQLITE_TESTCTRL_IMPOSTER25: {
185613 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185614 int iDb;
185615 sqlite3_mutex_enter(db->mutex);
185616 iDb = sqlite3FindDbName(db, va_arg(ap,const char*)__builtin_va_arg(ap, const char*));
185617 if( iDb>=0 ){
185618 db->init.iDb = iDb;
185619 db->init.busy = db->init.imposterTable = va_arg(ap,int)__builtin_va_arg(ap, int);
185620 db->init.newTnum = va_arg(ap,int)__builtin_va_arg(ap, int);
185621 if( db->init.busy==0 && db->init.newTnum>0 ){
185622 sqlite3ResetAllSchemasOfConnection(db);
185623 }
185624 }
185625 sqlite3_mutex_leave(db->mutex);
185626 break;
185627 }
185628
185629#if defined(YYCOVERAGE)
185630 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
185631 **
185632 ** This test control (only available when SQLite is compiled with
185633 ** -DYYCOVERAGE) writes a report onto "out" that shows all
185634 ** state/lookahead combinations in the parser state machine
185635 ** which are never exercised. If any state is missed, make the
185636 ** return code SQLITE_ERROR.
185637 */
185638 case SQLITE_TESTCTRL_PARSER_COVERAGE26: {
185639 FILE *out = va_arg(ap, FILE*)__builtin_va_arg(ap, FILE*);
185640 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR1;
185641 break;
185642 }
185643#endif /* defined(YYCOVERAGE) */
185644
185645 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
185646 **
185647 ** This test-control causes the most recent sqlite3_result_int64() value
185648 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
185649 ** MEM_IntReal values only arise during an INSERT operation of integer
185650 ** values into a REAL column, so they can be challenging to test. This
185651 ** test-control enables us to write an intreal() SQL function that can
185652 ** inject an intreal() value at arbitrary places in an SQL statement,
185653 ** for testing purposes.
185654 */
185655 case SQLITE_TESTCTRL_RESULT_INTREAL27: {
185656 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*)__builtin_va_arg(ap, sqlite3_context*);
185657 sqlite3ResultIntReal(pCtx);
185658 break;
185659 }
185660
185661 /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
185662 ** sqlite3 *db, // Database connection
185663 ** u64 *pnSeek // Write seek count here
185664 ** );
185665 **
185666 ** This test-control queries the seek-counter on the "main" database
185667 ** file. The seek-counter is written into *pnSeek and is then reset.
185668 ** The seek-count is only available if compiled with SQLITE_DEBUG.
185669 */
185670 case SQLITE_TESTCTRL_SEEK_COUNT30: {
185671 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
185672 u64 *pn = va_arg(ap, sqlite3_uint64*)__builtin_va_arg(ap, sqlite3_uint64*);
185673 *pn = sqlite3BtreeSeekCount(db->aDb->pBt)0;
185674 (void)db; /* Silence harmless unused variable warning */
185675 break;
185676 }
185677
185678 /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
185679 **
185680 ** "ptr" is a pointer to a u32.
185681 **
185682 ** op==0 Store the current sqlite3TreeTrace in *ptr
185683 ** op==1 Set sqlite3TreeTrace to the value *ptr
185684 ** op==2 Store the current sqlite3WhereTrace in *ptr
185685 ** op==3 Set sqlite3WhereTrace to the value *ptr
185686 */
185687 case SQLITE_TESTCTRL_TRACEFLAGS31: {
185688 int opTrace = va_arg(ap, int)__builtin_va_arg(ap, int);
185689 u32 *ptr = va_arg(ap, u32*)__builtin_va_arg(ap, u32*);
185690 switch( opTrace ){
185691 case 0: *ptr = sqlite3TreeTrace; break;
185692 case 1: sqlite3TreeTrace = *ptr; break;
185693 case 2: *ptr = sqlite3WhereTrace; break;
185694 case 3: sqlite3WhereTrace = *ptr; break;
185695 }
185696 break;
185697 }
185698
185699 /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST,
185700 ** double fIn, // Input value
185701 ** int *pLogEst, // sqlite3LogEstFromDouble(fIn)
185702 ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst)
185703 ** int *pLogEst2 // sqlite3LogEst(*pInt)
185704 ** );
185705 **
185706 ** Test access for the LogEst conversion routines.
185707 */
185708 case SQLITE_TESTCTRL_LOGEST33: {
185709 double rIn = va_arg(ap, double)__builtin_va_arg(ap, double);
185710 LogEst rLogEst = sqlite3LogEstFromDouble(rIn);
185711 int *pI1 = va_arg(ap,int*)__builtin_va_arg(ap, int*);
185712 u64 *pU64 = va_arg(ap,u64*)__builtin_va_arg(ap, u64*);
185713 int *pI2 = va_arg(ap,int*)__builtin_va_arg(ap, int*);
185714 *pI1 = rLogEst;
185715 *pU64 = sqlite3LogEstToInt(rLogEst);
185716 *pI2 = sqlite3LogEst(*pU64);
185717 break;
185718 }
185719
185720#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
185721 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
185722 **
185723 ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value
185724 ** of the id-th tuning parameter to *piValue. If "id" is between -1
185725 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
185726 ** tuning parameter into *piValue.
185727 **
185728 ** Tuning parameters are for use during transient development builds,
185729 ** to help find the best values for constants in the query planner.
185730 ** Access tuning parameters using the Tuning(ID) macro. Set the
185731 ** parameters in the CLI using ".testctrl tune ID VALUE".
185732 **
185733 ** Transient use only. Tuning parameters should not be used in
185734 ** checked-in code.
185735 */
185736 case SQLITE_TESTCTRL_TUNE32: {
185737 int id = va_arg(ap, int)__builtin_va_arg(ap, int);
185738 int *piValue = va_arg(ap, int*)__builtin_va_arg(ap, int*);
185739 if( id>0 && id<=SQLITE_NTUNE6 ){
185740 Tuning(id)0 = *piValue;
185741 }else if( id<0 && id>=-SQLITE_NTUNE6 ){
185742 *piValue = Tuning(-id)0;
185743 }else{
185744 rc = SQLITE_NOTFOUND12;
185745 }
185746 break;
185747 }
185748#endif
185749
185750 /* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff);
185751 **
185752 ** Activate or deactivate validation of JSONB that is generated from
185753 ** text. Off by default, as the validation is slow. Validation is
185754 ** only available if compiled using SQLITE_DEBUG.
185755 **
185756 ** If onOff is initially 1, then turn it on. If onOff is initially
185757 ** off, turn it off. If onOff is initially -1, then change onOff
185758 ** to be the current setting.
185759 */
185760 case SQLITE_TESTCTRL_JSON_SELFCHECK14: {
185761#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
185762 int *pOnOff = va_arg(ap, int*)__builtin_va_arg(ap, int*);
185763 if( *pOnOff<0 ){
185764 *pOnOff = sqlite3Config.bJsonSelfcheck;
185765 }else{
185766 sqlite3Config.bJsonSelfcheck = (u8)((*pOnOff)&0xff);
185767 }
185768#endif
185769 break;
185770 }
185771 }
185772 va_end(ap)__builtin_va_end(ap);
185773#endif /* SQLITE_UNTESTABLE */
185774 return rc;
185775}
185776
185777/*
185778** The Pager stores the Database filename, Journal filename, and WAL filename
185779** consecutively in memory, in that order. The database filename is prefixed
185780** by four zero bytes. Locate the start of the database filename by searching
185781** backwards for the first byte following four consecutive zero bytes.
185782**
185783** This only works if the filename passed in was obtained from the Pager.
185784*/
185785static const char *databaseName(const char *zName){
185786 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
185787 zName--;
185788 }
185789 return zName;
185790}
185791
185792/*
185793** Append text z[] to the end of p[]. Return a pointer to the first
185794** character after then zero terminator on the new text in p[].
185795*/
185796static char *appendText(char *p, const char *z){
185797 size_t n = strlen(z);
185798 memcpy(p, z, n+1);
185799 return p+n+1;
185800}
185801
185802/*
185803** Allocate memory to hold names for a database, journal file, WAL file,
185804** and query parameters. The pointer returned is valid for use by
185805** sqlite3_filename_database() and sqlite3_uri_parameter() and related
185806** functions.
185807**
185808** Memory layout must be compatible with that generated by the pager
185809** and expected by sqlite3_uri_parameter() and databaseName().
185810*/
185811SQLITE_API const char *sqlite3_create_filename(
185812 const char *zDatabase,
185813 const char *zJournal,
185814 const char *zWal,
185815 int nParam,
185816 const char **azParam
185817){
185818 sqlite3_int64 nByte;
185819 int i;
185820 char *pResult, *p;
185821 nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10;
185822 for(i=0; i<nParam*2; i++){
185823 nByte += strlen(azParam[i])+1;
185824 }
185825 pResult = p = sqlite3_malloc64( nByte );
185826 if( p==0 ) return 0;
185827 memset(p, 0, 4);
185828 p += 4;
185829 p = appendText(p, zDatabase);
185830 for(i=0; i<nParam*2; i++){
185831 p = appendText(p, azParam[i]);
185832 }
185833 *(p++) = 0;
185834 p = appendText(p, zJournal);
185835 p = appendText(p, zWal);
185836 *(p++) = 0;
185837 *(p++) = 0;
185838 assert( (sqlite3_int64)(p - pResult)==nByte )((void) (0));
185839 return pResult + 4;
185840}
185841
185842/*
185843** Free memory obtained from sqlite3_create_filename(). It is a severe
185844** error to call this routine with any parameter other than a pointer
185845** previously obtained from sqlite3_create_filename() or a NULL pointer.
185846*/
185847SQLITE_API void sqlite3_free_filename(const char *p){
185848 if( p==0 ) return;
185849 p = databaseName(p);
185850 sqlite3_free((char*)p - 4);
185851}
185852
185853
185854/*
185855** This is a utility routine, useful to VFS implementations, that checks
185856** to see if a database file was a URI that contained a specific query
185857** parameter, and if so obtains the value of the query parameter.
185858**
185859** The zFilename argument is the filename pointer passed into the xOpen()
185860** method of a VFS implementation. The zParam argument is the name of the
185861** query parameter we seek. This routine returns the value of the zParam
185862** parameter if it exists. If the parameter does not exist, this routine
185863** returns a NULL pointer.
185864*/
185865SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
185866 if( zFilename==0 || zParam==0 ) return 0;
185867 zFilename = databaseName(zFilename);
185868 return uriParameter(zFilename, zParam);
185869}
185870
185871/*
185872** Return a pointer to the name of Nth query parameter of the filename.
185873*/
185874SQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N){
185875 if( zFilename==0 || N<0 ) return 0;
185876 zFilename = databaseName(zFilename);
185877 zFilename += sqlite3Strlen30(zFilename) + 1;
185878 while( ALWAYS(zFilename)(zFilename) && zFilename[0] && (N--)>0 ){
185879 zFilename += sqlite3Strlen30(zFilename) + 1;
185880 zFilename += sqlite3Strlen30(zFilename) + 1;
185881 }
185882 return zFilename[0] ? zFilename : 0;
185883}
185884
185885/*
185886** Return a boolean value for a query parameter.
185887*/
185888SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
185889 const char *z = sqlite3_uri_parameter(zFilename, zParam);
185890 bDflt = bDflt!=0;
185891 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
185892}
185893
185894/*
185895** Return a 64-bit integer value for a query parameter.
185896*/
185897SQLITE_API sqlite3_int64 sqlite3_uri_int64(
185898 const char *zFilename, /* Filename as passed to xOpen */
185899 const char *zParam, /* URI parameter sought */
185900 sqlite3_int64 bDflt /* return if parameter is missing */
185901){
185902 const char *z = sqlite3_uri_parameter(zFilename, zParam);
185903 sqlite3_int64 v;
185904 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
185905 bDflt = v;
185906 }
185907 return bDflt;
185908}
185909
185910/*
185911** Translate a filename that was handed to a VFS routine into the corresponding
185912** database, journal, or WAL file.
185913**
185914** It is an error to pass this routine a filename string that was not
185915** passed into the VFS from the SQLite core. Doing so is similar to
185916** passing free() a pointer that was not obtained from malloc() - it is
185917** an error that we cannot easily detect but that will likely cause memory
185918** corruption.
185919*/
185920SQLITE_API const char *sqlite3_filename_database(const char *zFilename){
185921 if( zFilename==0 ) return 0;
185922 return databaseName(zFilename);
185923}
185924SQLITE_API const char *sqlite3_filename_journal(const char *zFilename){
185925 if( zFilename==0 ) return 0;
185926 zFilename = databaseName(zFilename);
185927 zFilename += sqlite3Strlen30(zFilename) + 1;
185928 while( ALWAYS(zFilename)(zFilename) && zFilename[0] ){
185929 zFilename += sqlite3Strlen30(zFilename) + 1;
185930 zFilename += sqlite3Strlen30(zFilename) + 1;
185931 }
185932 return zFilename + 1;
185933}
185934SQLITE_API const char *sqlite3_filename_wal(const char *zFilename){
185935#ifdef SQLITE_OMIT_WAL
185936 return 0;
185937#else
185938 zFilename = sqlite3_filename_journal(zFilename);
185939 if( zFilename ) zFilename += sqlite3Strlen30(zFilename) + 1;
185940 return zFilename;
185941#endif
185942}
185943
185944/*
185945** Return the Btree pointer identified by zDbName. Return NULL if not found.
185946*/
185947SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
185948 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
185949 return iDb<0 ? 0 : db->aDb[iDb].pBt;
185950}
185951
185952/*
185953** Return the name of the N-th database schema. Return NULL if N is out
185954** of range.
185955*/
185956SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N){
185957#ifdef SQLITE_ENABLE_API_ARMOR1
185958 if( !sqlite3SafetyCheckOk(db) ){
185959 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(185959);
185960 return 0;
185961 }
185962#endif
185963 if( N<0 || N>=db->nDb ){
185964 return 0;
185965 }else{
185966 return db->aDb[N].zDbSName;
185967 }
185968}
185969
185970/*
185971** Return the filename of the database associated with a database
185972** connection.
185973*/
185974SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
185975 Btree *pBt;
185976#ifdef SQLITE_ENABLE_API_ARMOR1
185977 if( !sqlite3SafetyCheckOk(db) ){
185978 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(185978);
185979 return 0;
185980 }
185981#endif
185982 pBt = sqlite3DbNameToBtree(db, zDbName);
185983 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
185984}
185985
185986/*
185987** Return 1 if database is read-only or 0 if read/write. Return -1 if
185988** no such database exists.
185989*/
185990SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
185991 Btree *pBt;
185992#ifdef SQLITE_ENABLE_API_ARMOR1
185993 if( !sqlite3SafetyCheckOk(db) ){
185994 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(185994);
185995 return -1;
185996 }
185997#endif
185998 pBt = sqlite3DbNameToBtree(db, zDbName);
185999 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
186000}
186001
186002#ifdef SQLITE_ENABLE_SNAPSHOT1
186003/*
186004** Obtain a snapshot handle for the snapshot of database zDb currently
186005** being read by handle db.
186006*/
186007SQLITE_API int sqlite3_snapshot_get(
186008 sqlite3 *db,
186009 const char *zDb,
186010 sqlite3_snapshot **ppSnapshot
186011){
186012 int rc = SQLITE_ERROR1;
186013#ifndef SQLITE_OMIT_WAL
186014
186015#ifdef SQLITE_ENABLE_API_ARMOR1
186016 if( !sqlite3SafetyCheckOk(db) ){
186017 return SQLITE_MISUSE_BKPTsqlite3MisuseError(186017);
186018 }
186019#endif
186020 sqlite3_mutex_enter(db->mutex);
186021
186022 if( db->autoCommit==0 ){
186023 int iDb = sqlite3FindDbName(db, zDb);
186024 if( iDb==0 || iDb>1 ){
186025 Btree *pBt = db->aDb[iDb].pBt;
186026 if( SQLITE_TXN_WRITE2!=sqlite3BtreeTxnState(pBt) ){
186027 Pager *pPager = sqlite3BtreePager(pBt);
186028 i64 dummy = 0;
186029 sqlite3PagerSnapshotOpen(pPager, (sqlite3_snapshot*)&dummy);
186030 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
186031 sqlite3PagerSnapshotOpen(pPager, 0);
186032 if( rc==SQLITE_OK0 ){
186033 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
186034 }
186035 }
186036 }
186037 }
186038
186039 sqlite3_mutex_leave(db->mutex);
186040#endif /* SQLITE_OMIT_WAL */
186041 return rc;
186042}
186043
186044/*
186045** Open a read-transaction on the snapshot identified by pSnapshot.
186046*/
186047SQLITE_API int sqlite3_snapshot_open(
186048 sqlite3 *db,
186049 const char *zDb,
186050 sqlite3_snapshot *pSnapshot
186051){
186052 int rc = SQLITE_ERROR1;
186053#ifndef SQLITE_OMIT_WAL
186054
186055#ifdef SQLITE_ENABLE_API_ARMOR1
186056 if( !sqlite3SafetyCheckOk(db) ){
186057 return SQLITE_MISUSE_BKPTsqlite3MisuseError(186057);
186058 }
186059#endif
186060 sqlite3_mutex_enter(db->mutex);
186061 if( db->autoCommit==0 ){
186062 int iDb;
186063 iDb = sqlite3FindDbName(db, zDb);
186064 if( iDb==0 || iDb>1 ){
186065 Btree *pBt = db->aDb[iDb].pBt;
186066 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE2 ){
186067 Pager *pPager = sqlite3BtreePager(pBt);
186068 int bUnlock = 0;
186069 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE0 ){
186070 if( db->nVdbeActive==0 ){
186071 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
186072 if( rc==SQLITE_OK0 ){
186073 bUnlock = 1;
186074 rc = sqlite3BtreeCommit(pBt);
186075 }
186076 }
186077 }else{
186078 rc = SQLITE_OK0;
186079 }
186080 if( rc==SQLITE_OK0 ){
186081 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
186082 }
186083 if( rc==SQLITE_OK0 ){
186084 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
186085 sqlite3PagerSnapshotOpen(pPager, 0);
186086 }
186087 if( bUnlock ){
186088 sqlite3PagerSnapshotUnlock(pPager);
186089 }
186090 }
186091 }
186092 }
186093
186094 sqlite3_mutex_leave(db->mutex);
186095#endif /* SQLITE_OMIT_WAL */
186096 return rc;
186097}
186098
186099/*
186100** Recover as many snapshots as possible from the wal file associated with
186101** schema zDb of database db.
186102*/
186103SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
186104 int rc = SQLITE_ERROR1;
186105#ifndef SQLITE_OMIT_WAL
186106 int iDb;
186107
186108#ifdef SQLITE_ENABLE_API_ARMOR1
186109 if( !sqlite3SafetyCheckOk(db) ){
186110 return SQLITE_MISUSE_BKPTsqlite3MisuseError(186110);
186111 }
186112#endif
186113
186114 sqlite3_mutex_enter(db->mutex);
186115 iDb = sqlite3FindDbName(db, zDb);
186116 if( iDb==0 || iDb>1 ){
186117 Btree *pBt = db->aDb[iDb].pBt;
186118 if( SQLITE_TXN_NONE0==sqlite3BtreeTxnState(pBt) ){
186119 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
186120 if( rc==SQLITE_OK0 ){
186121 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
186122 sqlite3BtreeCommit(pBt);
186123 }
186124 }
186125 }
186126 sqlite3_mutex_leave(db->mutex);
186127#endif /* SQLITE_OMIT_WAL */
186128 return rc;
186129}
186130
186131/*
186132** Free a snapshot handle obtained from sqlite3_snapshot_get().
186133*/
186134SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
186135 sqlite3_free(pSnapshot);
186136}
186137#endif /* SQLITE_ENABLE_SNAPSHOT */
186138
186139#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
186140/*
186141** Given the name of a compile-time option, return true if that option
186142** was used and false if not.
186143**
186144** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
186145** is not required for a match.
186146*/
186147SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
186148 int i, n;
186149 int nOpt;
186150 const char **azCompileOpt;
186151
186152#ifdef SQLITE_ENABLE_API_ARMOR1
186153 if( zOptName==0 ){
186154 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(186154);
186155 return 0;
186156 }
186157#endif
186158
186159 azCompileOpt = sqlite3CompileOptions(&nOpt);
186160
186161 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
186162 n = sqlite3Strlen30(zOptName);
186163
186164 /* Since nOpt is normally in single digits, a linear search is
186165 ** adequate. No need for a binary search. */
186166 for(i=0; i<nOpt; i++){
186167 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, azCompileOpt[i], n)==0
186168 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
186169 ){
186170 return 1;
186171 }
186172 }
186173 return 0;
186174}
186175
186176/*
186177** Return the N-th compile-time option string. If N is out of range,
186178** return a NULL pointer.
186179*/
186180SQLITE_API const char *sqlite3_compileoption_get(int N){
186181 int nOpt;
186182 const char **azCompileOpt;
186183 azCompileOpt = sqlite3CompileOptions(&nOpt);
186184 if( N>=0 && N<nOpt ){
186185 return azCompileOpt[N];
186186 }
186187 return 0;
186188}
186189#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
186190
186191/************** End of main.c ************************************************/
186192/************** Begin file notify.c ******************************************/
186193/*
186194** 2009 March 3
186195**
186196** The author disclaims copyright to this source code. In place of
186197** a legal notice, here is a blessing:
186198**
186199** May you do good and not evil.
186200** May you find forgiveness for yourself and forgive others.
186201** May you share freely, never taking more than you give.
186202**
186203*************************************************************************
186204**
186205** This file contains the implementation of the sqlite3_unlock_notify()
186206** API method and its associated functionality.
186207*/
186208/* #include "sqliteInt.h" */
186209/* #include "btreeInt.h" */
186210
186211/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
186212#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
186213
186214/*
186215** Public interfaces:
186216**
186217** sqlite3ConnectionBlocked()
186218** sqlite3ConnectionUnlocked()
186219** sqlite3ConnectionClosed()
186220** sqlite3_unlock_notify()
186221*/
186222
186223#define assertMutexHeld() \
186224 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) )((void) (0))
186225
186226/*
186227** Head of a linked list of all sqlite3 objects created by this process
186228** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
186229** is not NULL. This variable may only accessed while the STATIC_MAIN
186230** mutex is held.
186231*/
186232static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
186233
186234#ifndef NDEBUG1
186235/*
186236** This function is a complex assert() that verifies the following
186237** properties of the blocked connections list:
186238**
186239** 1) Each entry in the list has a non-NULL value for either
186240** pUnlockConnection or pBlockingConnection, or both.
186241**
186242** 2) All entries in the list that share a common value for
186243** xUnlockNotify are grouped together.
186244**
186245** 3) If the argument db is not NULL, then none of the entries in the
186246** blocked connections list have pUnlockConnection or pBlockingConnection
186247** set to db. This is used when closing connection db.
186248*/
186249static void checkListProperties(sqlite3 *db){
186250 sqlite3 *p;
186251 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
186252 int seen = 0;
186253 sqlite3 *p2;
186254
186255 /* Verify property (1) */
186256 assert( p->pUnlockConnection || p->pBlockingConnection )((void) (0));
186257
186258 /* Verify property (2) */
186259 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
186260 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
186261 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen )((void) (0));
186262 assert( db==0 || p->pUnlockConnection!=db )((void) (0));
186263 assert( db==0 || p->pBlockingConnection!=db )((void) (0));
186264 }
186265 }
186266}
186267#else
186268# define checkListProperties(x)
186269#endif
186270
186271/*
186272** Remove connection db from the blocked connections list. If connection
186273** db is not currently a part of the list, this function is a no-op.
186274*/
186275static void removeFromBlockedList(sqlite3 *db){
186276 sqlite3 **pp;
186277 assertMutexHeld();
186278 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
186279 if( *pp==db ){
186280 *pp = (*pp)->pNextBlocked;
186281 break;
186282 }
186283 }
186284}
186285
186286/*
186287** Add connection db to the blocked connections list. It is assumed
186288** that it is not already a part of the list.
186289*/
186290static void addToBlockedList(sqlite3 *db){
186291 sqlite3 **pp;
186292 assertMutexHeld();
186293 for(
186294 pp=&sqlite3BlockedList;
186295 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
186296 pp=&(*pp)->pNextBlocked
186297 );
186298 db->pNextBlocked = *pp;
186299 *pp = db;
186300}
186301
186302/*
186303** Obtain the STATIC_MAIN mutex.
186304*/
186305static void enterMutex(void){
186306 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
186307 checkListProperties(0);
186308}
186309
186310/*
186311** Release the STATIC_MAIN mutex.
186312*/
186313static void leaveMutex(void){
186314 assertMutexHeld();
186315 checkListProperties(0);
186316 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN2));
186317}
186318
186319/*
186320** Register an unlock-notify callback.
186321**
186322** This is called after connection "db" has attempted some operation
186323** but has received an SQLITE_LOCKED error because another connection
186324** (call it pOther) in the same process was busy using the same shared
186325** cache. pOther is found by looking at db->pBlockingConnection.
186326**
186327** If there is no blocking connection, the callback is invoked immediately,
186328** before this routine returns.
186329**
186330** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
186331** a deadlock.
186332**
186333** Otherwise, make arrangements to invoke xNotify when pOther drops
186334** its locks.
186335**
186336** Each call to this routine overrides any prior callbacks registered
186337** on the same "db". If xNotify==0 then any prior callbacks are immediately
186338** cancelled.
186339*/
186340SQLITE_API int sqlite3_unlock_notify(
186341 sqlite3 *db,
186342 void (*xNotify)(void **, int),
186343 void *pArg
186344){
186345 int rc = SQLITE_OK0;
186346
186347#ifdef SQLITE_ENABLE_API_ARMOR1
186348 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(186348);
186349#endif
186350 sqlite3_mutex_enter(db->mutex);
186351 enterMutex();
186352
186353 if( xNotify==0 ){
186354 removeFromBlockedList(db);
186355 db->pBlockingConnection = 0;
186356 db->pUnlockConnection = 0;
186357 db->xUnlockNotify = 0;
186358 db->pUnlockArg = 0;
186359 }else if( 0==db->pBlockingConnection ){
186360 /* The blocking transaction has been concluded. Or there never was a
186361 ** blocking transaction. In either case, invoke the notify callback
186362 ** immediately.
186363 */
186364 xNotify(&pArg, 1);
186365 }else{
186366 sqlite3 *p;
186367
186368 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
186369 if( p ){
186370 rc = SQLITE_LOCKED6; /* Deadlock detected. */
186371 }else{
186372 db->pUnlockConnection = db->pBlockingConnection;
186373 db->xUnlockNotify = xNotify;
186374 db->pUnlockArg = pArg;
186375 removeFromBlockedList(db);
186376 addToBlockedList(db);
186377 }
186378 }
186379
186380 leaveMutex();
186381 assert( !db->mallocFailed )((void) (0));
186382 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
186383 sqlite3_mutex_leave(db->mutex);
186384 return rc;
186385}
186386
186387/*
186388** This function is called while stepping or preparing a statement
186389** associated with connection db. The operation will return SQLITE_LOCKED
186390** to the user because it requires a lock that will not be available
186391** until connection pBlocker concludes its current transaction.
186392*/
186393SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
186394 enterMutex();
186395 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
186396 addToBlockedList(db);
186397 }
186398 db->pBlockingConnection = pBlocker;
186399 leaveMutex();
186400}
186401
186402/*
186403** This function is called when
186404** the transaction opened by database db has just finished. Locks held
186405** by database connection db have been released.
186406**
186407** This function loops through each entry in the blocked connections
186408** list and does the following:
186409**
186410** 1) If the sqlite3.pBlockingConnection member of a list entry is
186411** set to db, then set pBlockingConnection=0.
186412**
186413** 2) If the sqlite3.pUnlockConnection member of a list entry is
186414** set to db, then invoke the configured unlock-notify callback and
186415** set pUnlockConnection=0.
186416**
186417** 3) If the two steps above mean that pBlockingConnection==0 and
186418** pUnlockConnection==0, remove the entry from the blocked connections
186419** list.
186420*/
186421SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db){
186422 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
186423 int nArg = 0; /* Number of entries in aArg[] */
186424 sqlite3 **pp; /* Iterator variable */
186425 void **aArg; /* Arguments to the unlock callback */
186426 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
186427 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
186428
186429 aArg = aStatic;
186430 enterMutex(); /* Enter STATIC_MAIN mutex */
186431
186432 /* This loop runs once for each entry in the blocked-connections list. */
186433 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
186434 sqlite3 *p = *pp;
186435
186436 /* Step 1. */
186437 if( p->pBlockingConnection==db ){
186438 p->pBlockingConnection = 0;
186439 }
186440
186441 /* Step 2. */
186442 if( p->pUnlockConnection==db ){
186443 assert( p->xUnlockNotify )((void) (0));
186444 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
186445 xUnlockNotify(aArg, nArg);
186446 nArg = 0;
186447 }
186448
186449 sqlite3BeginBenignMalloc();
186450 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) )((void) (0));
186451 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn )((void) (0));
186452 if( (!aDyn && nArg==(int)ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))))
186453 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
186454 ){
186455 /* The aArg[] array needs to grow. */
186456 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
186457 if( pNew ){
186458 memcpy(pNew, aArg, nArg*sizeof(void *));
186459 sqlite3_free(aDyn);
186460 aDyn = aArg = pNew;
186461 }else{
186462 /* This occurs when the array of context pointers that need to
186463 ** be passed to the unlock-notify callback is larger than the
186464 ** aStatic[] array allocated on the stack and the attempt to
186465 ** allocate a larger array from the heap has failed.
186466 **
186467 ** This is a difficult situation to handle. Returning an error
186468 ** code to the caller is insufficient, as even if an error code
186469 ** is returned the transaction on connection db will still be
186470 ** closed and the unlock-notify callbacks on blocked connections
186471 ** will go unissued. This might cause the application to wait
186472 ** indefinitely for an unlock-notify callback that will never
186473 ** arrive.
186474 **
186475 ** Instead, invoke the unlock-notify callback with the context
186476 ** array already accumulated. We can then clear the array and
186477 ** begin accumulating any further context pointers without
186478 ** requiring any dynamic allocation. This is sub-optimal because
186479 ** it means that instead of one callback with a large array of
186480 ** context pointers the application will receive two or more
186481 ** callbacks with smaller arrays of context pointers, which will
186482 ** reduce the applications ability to prioritize multiple
186483 ** connections. But it is the best that can be done under the
186484 ** circumstances.
186485 */
186486 xUnlockNotify(aArg, nArg);
186487 nArg = 0;
186488 }
186489 }
186490 sqlite3EndBenignMalloc();
186491
186492 aArg[nArg++] = p->pUnlockArg;
186493 xUnlockNotify = p->xUnlockNotify;
186494 p->pUnlockConnection = 0;
186495 p->xUnlockNotify = 0;
186496 p->pUnlockArg = 0;
186497 }
186498
186499 /* Step 3. */
186500 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
186501 /* Remove connection p from the blocked connections list. */
186502 *pp = p->pNextBlocked;
186503 p->pNextBlocked = 0;
186504 }else{
186505 pp = &p->pNextBlocked;
186506 }
186507 }
186508
186509 if( nArg!=0 ){
186510 xUnlockNotify(aArg, nArg);
186511 }
186512 sqlite3_free(aDyn);
186513 leaveMutex(); /* Leave STATIC_MAIN mutex */
186514}
186515
186516/*
186517** This is called when the database connection passed as an argument is
186518** being closed. The connection is removed from the blocked list.
186519*/
186520SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db){
186521 sqlite3ConnectionUnlocked(db);
186522 enterMutex();
186523 removeFromBlockedList(db);
186524 checkListProperties(db);
186525 leaveMutex();
186526}
186527#endif
186528
186529/************** End of notify.c **********************************************/
186530/************** Begin file fts3.c ********************************************/
186531/*
186532** 2006 Oct 10
186533**
186534** The author disclaims copyright to this source code. In place of
186535** a legal notice, here is a blessing:
186536**
186537** May you do good and not evil.
186538** May you find forgiveness for yourself and forgive others.
186539** May you share freely, never taking more than you give.
186540**
186541******************************************************************************
186542**
186543** This is an SQLite module implementing full-text search.
186544*/
186545
186546/*
186547** The code in this file is only compiled if:
186548**
186549** * The FTS3 module is being built as an extension
186550** (in which case SQLITE_CORE is not defined), or
186551**
186552** * The FTS3 module is being built into the core of
186553** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
186554*/
186555
186556/* The full-text index is stored in a series of b+tree (-like)
186557** structures called segments which map terms to doclists. The
186558** structures are like b+trees in layout, but are constructed from the
186559** bottom up in optimal fashion and are not updatable. Since trees
186560** are built from the bottom up, things will be described from the
186561** bottom up.
186562**
186563**
186564**** Varints ****
186565** The basic unit of encoding is a variable-length integer called a
186566** varint. We encode variable-length integers in little-endian order
186567** using seven bits * per byte as follows:
186568**
186569** KEY:
186570** A = 0xxxxxxx 7 bits of data and one flag bit
186571** B = 1xxxxxxx 7 bits of data and one flag bit
186572**
186573** 7 bits - A
186574** 14 bits - BA
186575** 21 bits - BBA
186576** and so on.
186577**
186578** This is similar in concept to how sqlite encodes "varints" but
186579** the encoding is not the same. SQLite varints are big-endian
186580** are are limited to 9 bytes in length whereas FTS3 varints are
186581** little-endian and can be up to 10 bytes in length (in theory).
186582**
186583** Example encodings:
186584**
186585** 1: 0x01
186586** 127: 0x7f
186587** 128: 0x81 0x00
186588**
186589**
186590**** Document lists ****
186591** A doclist (document list) holds a docid-sorted list of hits for a
186592** given term. Doclists hold docids and associated token positions.
186593** A docid is the unique integer identifier for a single document.
186594** A position is the index of a word within the document. The first
186595** word of the document has a position of 0.
186596**
186597** FTS3 used to optionally store character offsets using a compile-time
186598** option. But that functionality is no longer supported.
186599**
186600** A doclist is stored like this:
186601**
186602** array {
186603** varint docid; (delta from previous doclist)
186604** array { (position list for column 0)
186605** varint position; (2 more than the delta from previous position)
186606** }
186607** array {
186608** varint POS_COLUMN; (marks start of position list for new column)
186609** varint column; (index of new column)
186610** array {
186611** varint position; (2 more than the delta from previous position)
186612** }
186613** }
186614** varint POS_END; (marks end of positions for this document.
186615** }
186616**
186617** Here, array { X } means zero or more occurrences of X, adjacent in
186618** memory. A "position" is an index of a token in the token stream
186619** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
186620** in the same logical place as the position element, and act as sentinals
186621** ending a position list array. POS_END is 0. POS_COLUMN is 1.
186622** The positions numbers are not stored literally but rather as two more
186623** than the difference from the prior position, or the just the position plus
186624** 2 for the first position. Example:
186625**
186626** label: A B C D E F G H I J K
186627** value: 123 5 9 1 1 14 35 0 234 72 0
186628**
186629** The 123 value is the first docid. For column zero in this document
186630** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
186631** at D signals the start of a new column; the 1 at E indicates that the
186632** new column is column number 1. There are two positions at 12 and 45
186633** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
186634** 234 at I is the delta to next docid (357). It has one position 70
186635** (72-2) and then terminates with the 0 at K.
186636**
186637** A "position-list" is the list of positions for multiple columns for
186638** a single docid. A "column-list" is the set of positions for a single
186639** column. Hence, a position-list consists of one or more column-lists,
186640** a document record consists of a docid followed by a position-list and
186641** a doclist consists of one or more document records.
186642**
186643** A bare doclist omits the position information, becoming an
186644** array of varint-encoded docids.
186645**
186646**** Segment leaf nodes ****
186647** Segment leaf nodes store terms and doclists, ordered by term. Leaf
186648** nodes are written using LeafWriter, and read using LeafReader (to
186649** iterate through a single leaf node's data) and LeavesReader (to
186650** iterate through a segment's entire leaf layer). Leaf nodes have
186651** the format:
186652**
186653** varint iHeight; (height from leaf level, always 0)
186654** varint nTerm; (length of first term)
186655** char pTerm[nTerm]; (content of first term)
186656** varint nDoclist; (length of term's associated doclist)
186657** char pDoclist[nDoclist]; (content of doclist)
186658** array {
186659** (further terms are delta-encoded)
186660** varint nPrefix; (length of prefix shared with previous term)
186661** varint nSuffix; (length of unshared suffix)
186662** char pTermSuffix[nSuffix];(unshared suffix of next term)
186663** varint nDoclist; (length of term's associated doclist)
186664** char pDoclist[nDoclist]; (content of doclist)
186665** }
186666**
186667** Here, array { X } means zero or more occurrences of X, adjacent in
186668** memory.
186669**
186670** Leaf nodes are broken into blocks which are stored contiguously in
186671** the %_segments table in sorted order. This means that when the end
186672** of a node is reached, the next term is in the node with the next
186673** greater node id.
186674**
186675** New data is spilled to a new leaf node when the current node
186676** exceeds LEAF_MAX bytes (default 2048). New data which itself is
186677** larger than STANDALONE_MIN (default 1024) is placed in a standalone
186678** node (a leaf node with a single term and doclist). The goal of
186679** these settings is to pack together groups of small doclists while
186680** making it efficient to directly access large doclists. The
186681** assumption is that large doclists represent terms which are more
186682** likely to be query targets.
186683**
186684** TODO(shess) It may be useful for blocking decisions to be more
186685** dynamic. For instance, it may make more sense to have a 2.5k leaf
186686** node rather than splitting into 2k and .5k nodes. My intuition is
186687** that this might extend through 2x or 4x the pagesize.
186688**
186689**
186690**** Segment interior nodes ****
186691** Segment interior nodes store blockids for subtree nodes and terms
186692** to describe what data is stored by the each subtree. Interior
186693** nodes are written using InteriorWriter, and read using
186694** InteriorReader. InteriorWriters are created as needed when
186695** SegmentWriter creates new leaf nodes, or when an interior node
186696** itself grows too big and must be split. The format of interior
186697** nodes:
186698**
186699** varint iHeight; (height from leaf level, always >0)
186700** varint iBlockid; (block id of node's leftmost subtree)
186701** optional {
186702** varint nTerm; (length of first term)
186703** char pTerm[nTerm]; (content of first term)
186704** array {
186705** (further terms are delta-encoded)
186706** varint nPrefix; (length of shared prefix with previous term)
186707** varint nSuffix; (length of unshared suffix)
186708** char pTermSuffix[nSuffix]; (unshared suffix of next term)
186709** }
186710** }
186711**
186712** Here, optional { X } means an optional element, while array { X }
186713** means zero or more occurrences of X, adjacent in memory.
186714**
186715** An interior node encodes n terms separating n+1 subtrees. The
186716** subtree blocks are contiguous, so only the first subtree's blockid
186717** is encoded. The subtree at iBlockid will contain all terms less
186718** than the first term encoded (or all terms if no term is encoded).
186719** Otherwise, for terms greater than or equal to pTerm[i] but less
186720** than pTerm[i+1], the subtree for that term will be rooted at
186721** iBlockid+i. Interior nodes only store enough term data to
186722** distinguish adjacent children (if the rightmost term of the left
186723** child is "something", and the leftmost term of the right child is
186724** "wicked", only "w" is stored).
186725**
186726** New data is spilled to a new interior node at the same height when
186727** the current node exceeds INTERIOR_MAX bytes (default 2048).
186728** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
186729** interior nodes and making the tree too skinny. The interior nodes
186730** at a given height are naturally tracked by interior nodes at
186731** height+1, and so on.
186732**
186733**
186734**** Segment directory ****
186735** The segment directory in table %_segdir stores meta-information for
186736** merging and deleting segments, and also the root node of the
186737** segment's tree.
186738**
186739** The root node is the top node of the segment's tree after encoding
186740** the entire segment, restricted to ROOT_MAX bytes (default 1024).
186741** This could be either a leaf node or an interior node. If the top
186742** node requires more than ROOT_MAX bytes, it is flushed to %_segments
186743** and a new root interior node is generated (which should always fit
186744** within ROOT_MAX because it only needs space for 2 varints, the
186745** height and the blockid of the previous root).
186746**
186747** The meta-information in the segment directory is:
186748** level - segment level (see below)
186749** idx - index within level
186750** - (level,idx uniquely identify a segment)
186751** start_block - first leaf node
186752** leaves_end_block - last leaf node
186753** end_block - last block (including interior nodes)
186754** root - contents of root node
186755**
186756** If the root node is a leaf node, then start_block,
186757** leaves_end_block, and end_block are all 0.
186758**
186759**
186760**** Segment merging ****
186761** To amortize update costs, segments are grouped into levels and
186762** merged in batches. Each increase in level represents exponentially
186763** more documents.
186764**
186765** New documents (actually, document updates) are tokenized and
186766** written individually (using LeafWriter) to a level 0 segment, with
186767** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
186768** level 0 segments are merged into a single level 1 segment. Level 1
186769** is populated like level 0, and eventually MERGE_COUNT level 1
186770** segments are merged to a single level 2 segment (representing
186771** MERGE_COUNT^2 updates), and so on.
186772**
186773** A segment merge traverses all segments at a given level in
186774** parallel, performing a straightforward sorted merge. Since segment
186775** leaf nodes are written in to the %_segments table in order, this
186776** merge traverses the underlying sqlite disk structures efficiently.
186777** After the merge, all segment blocks from the merged level are
186778** deleted.
186779**
186780** MERGE_COUNT controls how often we merge segments. 16 seems to be
186781** somewhat of a sweet spot for insertion performance. 32 and 64 show
186782** very similar performance numbers to 16 on insertion, though they're
186783** a tiny bit slower (perhaps due to more overhead in merge-time
186784** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
186785** 16, 2 about 66% slower than 16.
186786**
186787** At query time, high MERGE_COUNT increases the number of segments
186788** which need to be scanned and merged. For instance, with 100k docs
186789** inserted:
186790**
186791** MERGE_COUNT segments
186792** 16 25
186793** 8 12
186794** 4 10
186795** 2 6
186796**
186797** This appears to have only a moderate impact on queries for very
186798** frequent terms (which are somewhat dominated by segment merge
186799** costs), and infrequent and non-existent terms still seem to be fast
186800** even with many segments.
186801**
186802** TODO(shess) That said, it would be nice to have a better query-side
186803** argument for MERGE_COUNT of 16. Also, it is possible/likely that
186804** optimizations to things like doclist merging will swing the sweet
186805** spot around.
186806**
186807**
186808**
186809**** Handling of deletions and updates ****
186810** Since we're using a segmented structure, with no docid-oriented
186811** index into the term index, we clearly cannot simply update the term
186812** index when a document is deleted or updated. For deletions, we
186813** write an empty doclist (varint(docid) varint(POS_END)), for updates
186814** we simply write the new doclist. Segment merges overwrite older
186815** data for a particular docid with newer data, so deletes or updates
186816** will eventually overtake the earlier data and knock it out. The
186817** query logic likewise merges doclists so that newer data knocks out
186818** older data.
186819*/
186820
186821/************** Include fts3Int.h in the middle of fts3.c ********************/
186822/************** Begin file fts3Int.h *****************************************/
186823/*
186824** 2009 Nov 12
186825**
186826** The author disclaims copyright to this source code. In place of
186827** a legal notice, here is a blessing:
186828**
186829** May you do good and not evil.
186830** May you find forgiveness for yourself and forgive others.
186831** May you share freely, never taking more than you give.
186832**
186833******************************************************************************
186834**
186835*/
186836#ifndef _FTSINT_H
186837#define _FTSINT_H
186838
186839#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
186840# define NDEBUG1 1
186841#endif
186842
186843/* FTS3/FTS4 require virtual tables */
186844#ifdef SQLITE_OMIT_VIRTUALTABLE
186845# undef SQLITE_ENABLE_FTS31
186846# undef SQLITE_ENABLE_FTS41
186847#endif
186848
186849/*
186850** FTS4 is really an extension for FTS3. It is enabled using the
186851** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
186852** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
186853*/
186854#if defined(SQLITE_ENABLE_FTS41) && !defined(SQLITE_ENABLE_FTS31)
186855# define SQLITE_ENABLE_FTS31
186856#endif
186857
186858#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
186859
186860/* If not building as part of the core, include sqlite3ext.h. */
186861#ifndef SQLITE_CORE1
186862/* # include "sqlite3ext.h" */
186863SQLITE_EXTENSION_INIT3
186864#endif
186865
186866/* #include "sqlite3.h" */
186867/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
186868/************** Begin file fts3_tokenizer.h **********************************/
186869/*
186870** 2006 July 10
186871**
186872** The author disclaims copyright to this source code.
186873**
186874*************************************************************************
186875** Defines the interface to tokenizers used by fulltext-search. There
186876** are three basic components:
186877**
186878** sqlite3_tokenizer_module is a singleton defining the tokenizer
186879** interface functions. This is essentially the class structure for
186880** tokenizers.
186881**
186882** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
186883** including customization information defined at creation time.
186884**
186885** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
186886** tokens from a particular input.
186887*/
186888#ifndef _FTS3_TOKENIZER_H_
186889#define _FTS3_TOKENIZER_H_
186890
186891/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
186892** If tokenizers are to be allowed to call sqlite3_*() functions, then
186893** we will need a way to register the API consistently.
186894*/
186895/* #include "sqlite3.h" */
186896
186897/*
186898** Structures used by the tokenizer interface. When a new tokenizer
186899** implementation is registered, the caller provides a pointer to
186900** an sqlite3_tokenizer_module containing pointers to the callback
186901** functions that make up an implementation.
186902**
186903** When an fts3 table is created, it passes any arguments passed to
186904** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
186905** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
186906** implementation. The xCreate() function in turn returns an
186907** sqlite3_tokenizer structure representing the specific tokenizer to
186908** be used for the fts3 table (customized by the tokenizer clause arguments).
186909**
186910** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
186911** method is called. It returns an sqlite3_tokenizer_cursor object
186912** that may be used to tokenize a specific input buffer based on
186913** the tokenization rules supplied by a specific sqlite3_tokenizer
186914** object.
186915*/
186916typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
186917typedef struct sqlite3_tokenizer sqlite3_tokenizer;
186918typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
186919
186920struct sqlite3_tokenizer_module {
186921
186922 /*
186923 ** Structure version. Should always be set to 0 or 1.
186924 */
186925 int iVersion;
186926
186927 /*
186928 ** Create a new tokenizer. The values in the argv[] array are the
186929 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
186930 ** TABLE statement that created the fts3 table. For example, if
186931 ** the following SQL is executed:
186932 **
186933 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
186934 **
186935 ** then argc is set to 2, and the argv[] array contains pointers
186936 ** to the strings "arg1" and "arg2".
186937 **
186938 ** This method should return either SQLITE_OK (0), or an SQLite error
186939 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
186940 ** to point at the newly created tokenizer structure. The generic
186941 ** sqlite3_tokenizer.pModule variable should not be initialized by
186942 ** this callback. The caller will do so.
186943 */
186944 int (*xCreate)(
186945 int argc, /* Size of argv array */
186946 const char *const*argv, /* Tokenizer argument strings */
186947 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
186948 );
186949
186950 /*
186951 ** Destroy an existing tokenizer. The fts3 module calls this method
186952 ** exactly once for each successful call to xCreate().
186953 */
186954 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
186955
186956 /*
186957 ** Create a tokenizer cursor to tokenize an input buffer. The caller
186958 ** is responsible for ensuring that the input buffer remains valid
186959 ** until the cursor is closed (using the xClose() method).
186960 */
186961 int (*xOpen)(
186962 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
186963 const char *pInput, int nBytes, /* Input buffer */
186964 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
186965 );
186966
186967 /*
186968 ** Destroy an existing tokenizer cursor. The fts3 module calls this
186969 ** method exactly once for each successful call to xOpen().
186970 */
186971 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
186972
186973 /*
186974 ** Retrieve the next token from the tokenizer cursor pCursor. This
186975 ** method should either return SQLITE_OK and set the values of the
186976 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
186977 ** the end of the buffer has been reached, or an SQLite error code.
186978 **
186979 ** *ppToken should be set to point at a buffer containing the
186980 ** normalized version of the token (i.e. after any case-folding and/or
186981 ** stemming has been performed). *pnBytes should be set to the length
186982 ** of this buffer in bytes. The input text that generated the token is
186983 ** identified by the byte offsets returned in *piStartOffset and
186984 ** *piEndOffset. *piStartOffset should be set to the index of the first
186985 ** byte of the token in the input buffer. *piEndOffset should be set
186986 ** to the index of the first byte just past the end of the token in
186987 ** the input buffer.
186988 **
186989 ** The buffer *ppToken is set to point at is managed by the tokenizer
186990 ** implementation. It is only required to be valid until the next call
186991 ** to xNext() or xClose().
186992 */
186993 /* TODO(shess) current implementation requires pInput to be
186994 ** nul-terminated. This should either be fixed, or pInput/nBytes
186995 ** should be converted to zInput.
186996 */
186997 int (*xNext)(
186998 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
186999 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
187000 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
187001 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
187002 int *piPosition /* OUT: Number of tokens returned before this one */
187003 );
187004
187005 /***********************************************************************
187006 ** Methods below this point are only available if iVersion>=1.
187007 */
187008
187009 /*
187010 ** Configure the language id of a tokenizer cursor.
187011 */
187012 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
187013};
187014
187015struct sqlite3_tokenizer {
187016 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
187017 /* Tokenizer implementations will typically add additional fields */
187018};
187019
187020struct sqlite3_tokenizer_cursor {
187021 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
187022 /* Tokenizer implementations will typically add additional fields */
187023};
187024
187025int fts3_global_term_cnt(int iTerm, int iCol);
187026int fts3_term_cnt(int iTerm, int iCol);
187027
187028
187029#endif /* _FTS3_TOKENIZER_H_ */
187030
187031/************** End of fts3_tokenizer.h **************************************/
187032/************** Continuing where we left off in fts3Int.h ********************/
187033/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
187034/************** Begin file fts3_hash.h ***************************************/
187035/*
187036** 2001 September 22
187037**
187038** The author disclaims copyright to this source code. In place of
187039** a legal notice, here is a blessing:
187040**
187041** May you do good and not evil.
187042** May you find forgiveness for yourself and forgive others.
187043** May you share freely, never taking more than you give.
187044**
187045*************************************************************************
187046** This is the header file for the generic hash-table implementation
187047** used in SQLite. We've modified it slightly to serve as a standalone
187048** hash table implementation for the full-text indexing module.
187049**
187050*/
187051#ifndef _FTS3_HASH_H_
187052#define _FTS3_HASH_H_
187053
187054/* Forward declarations of structures. */
187055typedef struct Fts3Hash Fts3Hash;
187056typedef struct Fts3HashElem Fts3HashElem;
187057
187058/* A complete hash table is an instance of the following structure.
187059** The internals of this structure are intended to be opaque -- client
187060** code should not attempt to access or modify the fields of this structure
187061** directly. Change this structure only by using the routines below.
187062** However, many of the "procedures" and "functions" for modifying and
187063** accessing this structure are really macros, so we can't really make
187064** this structure opaque.
187065*/
187066struct Fts3Hash {
187067 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
187068 char copyKey; /* True if copy of key made on insert */
187069 int count; /* Number of entries in this table */
187070 Fts3HashElem *first; /* The first element of the array */
187071 int htsize; /* Number of buckets in the hash table */
187072 struct _fts3ht { /* the hash table */
187073 int count; /* Number of entries with this hash */
187074 Fts3HashElem *chain; /* Pointer to first entry with this hash */
187075 } *ht;
187076};
187077
187078/* Each element in the hash table is an instance of the following
187079** structure. All elements are stored on a single doubly-linked list.
187080**
187081** Again, this structure is intended to be opaque, but it can't really
187082** be opaque because it is used by macros.
187083*/
187084struct Fts3HashElem {
187085 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
187086 void *data; /* Data associated with this element */
187087 void *pKey; int nKey; /* Key associated with this element */
187088};
187089
187090/*
187091** There are 2 different modes of operation for a hash table:
187092**
187093** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
187094** (including the null-terminator, if any). Case
187095** is respected in comparisons.
187096**
187097** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
187098** memcmp() is used to compare keys.
187099**
187100** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
187101*/
187102#define FTS3_HASH_STRING1 1
187103#define FTS3_HASH_BINARY2 2
187104
187105/*
187106** Access routines. To delete, insert a NULL pointer.
187107*/
187108SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
187109SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
187110SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
187111SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash*);
187112SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
187113
187114/*
187115** Shorthand for the functions above
187116*/
187117#define fts3HashInitsqlite3Fts3HashInit sqlite3Fts3HashInit
187118#define fts3HashInsertsqlite3Fts3HashInsert sqlite3Fts3HashInsert
187119#define fts3HashFindsqlite3Fts3HashFind sqlite3Fts3HashFind
187120#define fts3HashClearsqlite3Fts3HashClear sqlite3Fts3HashClear
187121#define fts3HashFindElemsqlite3Fts3HashFindElem sqlite3Fts3HashFindElem
187122
187123/*
187124** Macros for looping over all elements of a hash table. The idiom is
187125** like this:
187126**
187127** Fts3Hash h;
187128** Fts3HashElem *p;
187129** ...
187130** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
187131** SomeStructure *pData = fts3HashData(p);
187132** // do something with pData
187133** }
187134*/
187135#define fts3HashFirst(H)((H)->first) ((H)->first)
187136#define fts3HashNext(E)((E)->next) ((E)->next)
187137#define fts3HashData(E)((E)->data) ((E)->data)
187138#define fts3HashKey(E)((E)->pKey) ((E)->pKey)
187139#define fts3HashKeysize(E)((E)->nKey) ((E)->nKey)
187140
187141/*
187142** Number of entries in a hash table
187143*/
187144#define fts3HashCount(H)((H)->count) ((H)->count)
187145
187146#endif /* _FTS3_HASH_H_ */
187147
187148/************** End of fts3_hash.h *******************************************/
187149/************** Continuing where we left off in fts3Int.h ********************/
187150
187151/*
187152** This constant determines the maximum depth of an FTS expression tree
187153** that the library will create and use. FTS uses recursion to perform
187154** various operations on the query tree, so the disadvantage of a large
187155** limit is that it may allow very large queries to use large amounts
187156** of stack space (perhaps causing a stack overflow).
187157*/
187158#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH12
187159# define SQLITE_FTS3_MAX_EXPR_DEPTH12 12
187160#endif
187161
187162
187163/*
187164** This constant controls how often segments are merged. Once there are
187165** FTS3_MERGE_COUNT segments of level N, they are merged into a single
187166** segment of level N+1.
187167*/
187168#define FTS3_MERGE_COUNT16 16
187169
187170/*
187171** This is the maximum amount of data (in bytes) to store in the
187172** Fts3Table.pendingTerms hash table. Normally, the hash table is
187173** populated as documents are inserted/updated/deleted in a transaction
187174** and used to create a new segment when the transaction is committed.
187175** However if this limit is reached midway through a transaction, a new
187176** segment is created and the hash table cleared immediately.
187177*/
187178#define FTS3_MAX_PENDING_DATA(1*1024*1024) (1*1024*1024)
187179
187180/*
187181** Macro to return the number of elements in an array. SQLite has a
187182** similar macro called ArraySize(). Use a different name to avoid
187183** a collision when building an amalgamation with built-in FTS3.
187184*/
187185#define SizeofArray(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
187186
187187
187188#ifndef MIN
187189# define MIN(x,y)((x)<(y)?(x):(y)) ((x)<(y)?(x):(y))
187190#endif
187191#ifndef MAX
187192# define MAX(x,y)((x)>(y)?(x):(y)) ((x)>(y)?(x):(y))
187193#endif
187194
187195/*
187196** Maximum length of a varint encoded integer. The varint format is different
187197** from that used by SQLite, so the maximum length is 10, not 9.
187198*/
187199#define FTS3_VARINT_MAX10 10
187200
187201#define FTS3_BUFFER_PADDING8 8
187202
187203/*
187204** FTS4 virtual tables may maintain multiple indexes - one index of all terms
187205** in the document set and zero or more prefix indexes. All indexes are stored
187206** as one or more b+-trees in the %_segments and %_segdir tables.
187207**
187208** It is possible to determine which index a b+-tree belongs to based on the
187209** value stored in the "%_segdir.level" column. Given this value L, the index
187210** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
187211** level values between 0 and 1023 (inclusive) belong to index 0, all levels
187212** between 1024 and 2047 to index 1, and so on.
187213**
187214** It is considered impossible for an index to use more than 1024 levels. In
187215** theory though this may happen, but only after at least
187216** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
187217*/
187218#define FTS3_SEGDIR_MAXLEVEL1024 1024
187219#define FTS3_SEGDIR_MAXLEVEL_STR"1024" "1024"
187220
187221/*
187222** The testcase() macro is only used by the amalgamation. If undefined,
187223** make it a no-op.
187224*/
187225#ifndef testcase
187226# define testcase(X)
187227#endif
187228
187229/*
187230** Terminator values for position-lists and column-lists.
187231*/
187232#define POS_COLUMN(1) (1) /* Column-list terminator */
187233#define POS_END(0) (0) /* Position-list terminator */
187234
187235/*
187236** The assert_fts3_nc() macro is similar to the assert() macro, except that it
187237** is used for assert() conditions that are true only if it can be
187238** guranteed that the database is not corrupt.
187239*/
187240#ifdef SQLITE_DEBUG
187241SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
187242# define assert_fts3_nc(x)((void) (0)) assert(sqlite3_fts3_may_be_corrupt || (x))((void) (0))
187243#else
187244# define assert_fts3_nc(x)((void) (0)) assert(x)((void) (0))
187245#endif
187246
187247/*
187248** This section provides definitions to allow the
187249** FTS3 extension to be compiled outside of the
187250** amalgamation.
187251*/
187252#ifndef SQLITE_AMALGAMATION1
187253/*
187254** Macros indicating that conditional expressions are always true or
187255** false.
187256*/
187257#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
187258# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
187259#endif
187260#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
187261# define ALWAYS(X)(X) (1)
187262# define NEVER(X)(X) (0)
187263#elif !defined(NDEBUG1)
187264# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
187265# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
187266#else
187267# define ALWAYS(X)(X) (X)
187268# define NEVER(X)(X) (X)
187269#endif
187270
187271/*
187272** Internal types used by SQLite.
187273*/
187274typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
187275typedef short int i16; /* 2-byte (or larger) signed integer */
187276typedef unsigned int u32; /* 4-byte unsigned integer */
187277typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
187278typedef sqlite3_int64 i64; /* 8-byte signed integer */
187279
187280/*
187281** Macro used to suppress compiler warnings for unused parameters.
187282*/
187283#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
187284
187285/*
187286** Activate assert() only if SQLITE_TEST is enabled.
187287*/
187288#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
187289# define NDEBUG1 1
187290#endif
187291
187292/*
187293** The TESTONLY macro is used to enclose variable declarations or
187294** other bits of code that are needed to support the arguments
187295** within testcase() and assert() macros.
187296*/
187297#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
187298# define TESTONLY(X) X
187299#else
187300# define TESTONLY(X)
187301#endif
187302
187303#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
187304#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
187305
187306#define deliberate_fall_through__attribute__((fallthrough));
187307
187308#endif /* SQLITE_AMALGAMATION */
187309
187310#ifdef SQLITE_DEBUG
187311SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(void);
187312# define FTS_CORRUPT_VTAB(11 | (1<<8)) sqlite3Fts3Corrupt()
187313#else
187314# define FTS_CORRUPT_VTAB(11 | (1<<8)) SQLITE_CORRUPT_VTAB(11 | (1<<8))
187315#endif
187316
187317typedef struct Fts3Table Fts3Table;
187318typedef struct Fts3Cursor Fts3Cursor;
187319typedef struct Fts3Expr Fts3Expr;
187320typedef struct Fts3Phrase Fts3Phrase;
187321typedef struct Fts3PhraseToken Fts3PhraseToken;
187322
187323typedef struct Fts3Doclist Fts3Doclist;
187324typedef struct Fts3SegFilter Fts3SegFilter;
187325typedef struct Fts3DeferredToken Fts3DeferredToken;
187326typedef struct Fts3SegReader Fts3SegReader;
187327typedef struct Fts3MultiSegReader Fts3MultiSegReader;
187328
187329typedef struct MatchinfoBuffer MatchinfoBuffer;
187330
187331/*
187332** A connection to a fulltext index is an instance of the following
187333** structure. The xCreate and xConnect methods create an instance
187334** of this structure and xDestroy and xDisconnect free that instance.
187335** All other methods receive a pointer to the structure as one of their
187336** arguments.
187337*/
187338struct Fts3Table {
187339 sqlite3_vtab base; /* Base class used by SQLite core */
187340 sqlite3 *db; /* The database connection */
187341 const char *zDb; /* logical database name */
187342 const char *zName; /* virtual table name */
187343 int nColumn; /* number of named columns in virtual table */
187344 char **azColumn; /* column names. malloced */
187345 u8 *abNotindexed; /* True for 'notindexed' columns */
187346 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
187347 char *zContentTbl; /* content=xxx option, or NULL */
187348 char *zLanguageid; /* languageid=xxx option, or NULL */
187349 int nAutoincrmerge; /* Value configured by 'automerge' */
187350 u32 nLeafAdd; /* Number of leaf blocks added this trans */
187351 int bLock; /* Used to prevent recursive content= tbls */
187352
187353 /* Precompiled statements used by the implementation. Each of these
187354 ** statements is run and reset within a single virtual table API call.
187355 */
187356 sqlite3_stmt *aStmt[40];
187357 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
187358
187359 char *zReadExprlist;
187360 char *zWriteExprlist;
187361
187362 int nNodeSize; /* Soft limit for node size */
187363 u8 bFts4; /* True for FTS4, false for FTS3 */
187364 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
187365 u8 bHasDocsize; /* True if %_docsize table exists */
187366 u8 bDescIdx; /* True if doclists are in reverse order */
187367 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
187368 int nPgsz; /* Page size for host database */
187369 char *zSegmentsTbl; /* Name of %_segments table */
187370 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
187371 int iSavepoint;
187372
187373 /*
187374 ** The following array of hash tables is used to buffer pending index
187375 ** updates during transactions. All pending updates buffered at any one
187376 ** time must share a common language-id (see the FTS4 langid= feature).
187377 ** The current language id is stored in variable iPrevLangid.
187378 **
187379 ** A single FTS4 table may have multiple full-text indexes. For each index
187380 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
187381 ** terms that appear in the document set. Each subsequent index in aIndex[]
187382 ** is an index of prefixes of a specific length.
187383 **
187384 ** Variable nPendingData contains an estimate the memory consumed by the
187385 ** pending data structures, including hash table overhead, but not including
187386 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
187387 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
187388 ** recently inserted record.
187389 */
187390 int nIndex; /* Size of aIndex[] */
187391 struct Fts3Index {
187392 int nPrefix; /* Prefix length (0 for main terms index) */
187393 Fts3Hash hPending; /* Pending terms table for this index */
187394 } *aIndex;
187395 int nMaxPendingData; /* Max pending data before flush to disk */
187396 int nPendingData; /* Current bytes of pending data */
187397 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
187398 int iPrevLangid; /* Langid of recently inserted document */
187399 int bPrevDelete; /* True if last operation was a delete */
187400
187401#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
187402 /* State variables used for validating that the transaction control
187403 ** methods of the virtual table are called at appropriate times. These
187404 ** values do not contribute to FTS functionality; they are used for
187405 ** verifying the operation of the SQLite core.
187406 */
187407 int inTransaction; /* True after xBegin but before xCommit/xRollback */
187408 int mxSavepoint; /* Largest valid xSavepoint integer */
187409#endif
187410
187411#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
187412 /* True to disable the incremental doclist optimization. This is controled
187413 ** by special insert command 'test-no-incr-doclist'. */
187414 int bNoIncrDoclist;
187415
187416 /* Number of segments in a level */
187417 int nMergeCount;
187418#endif
187419};
187420
187421/* Macro to find the number of segments to merge */
187422#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
187423# define MergeCount(P)16 ((P)->nMergeCount)
187424#else
187425# define MergeCount(P)16 FTS3_MERGE_COUNT16
187426#endif
187427
187428/*
187429** When the core wants to read from the virtual table, it creates a
187430** virtual table cursor (an instance of the following structure) using
187431** the xOpen method. Cursors are destroyed using the xClose method.
187432*/
187433struct Fts3Cursor {
187434 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
187435 i16 eSearch; /* Search strategy (see below) */
187436 u8 isEof; /* True if at End Of Results */
187437 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
187438 u8 bSeekStmt; /* True if pStmt is a seek */
187439 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
187440 Fts3Expr *pExpr; /* Parsed MATCH query string */
187441 int iLangid; /* Language being queried for */
187442 int nPhrase; /* Number of matchable phrases in query */
187443 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
187444 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
187445 char *pNextId; /* Pointer into the body of aDoclist */
187446 char *aDoclist; /* List of docids for full-text queries */
187447 int nDoclist; /* Size of buffer at aDoclist */
187448 u8 bDesc; /* True to sort in descending order */
187449 int eEvalmode; /* An FTS3_EVAL_XX constant */
187450 int nRowAvg; /* Average size of database rows, in pages */
187451 sqlite3_int64 nDoc; /* Documents in table */
187452 i64 iMinDocid; /* Minimum docid to return */
187453 i64 iMaxDocid; /* Maximum docid to return */
187454 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
187455 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
187456};
187457
187458#define FTS3_EVAL_FILTER0 0
187459#define FTS3_EVAL_NEXT1 1
187460#define FTS3_EVAL_MATCHINFO2 2
187461
187462/*
187463** The Fts3Cursor.eSearch member is always set to one of the following.
187464** Actualy, Fts3Cursor.eSearch can be greater than or equal to
187465** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
187466** of the column to be searched. For example, in
187467**
187468** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
187469** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
187470**
187471** Because the LHS of the MATCH operator is 2nd column "b",
187472** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
187473** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
187474** indicating that all columns should be searched,
187475** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
187476*/
187477#define FTS3_FULLSCAN_SEARCH0 0 /* Linear scan of %_content table */
187478#define FTS3_DOCID_SEARCH1 1 /* Lookup by rowid on %_content table */
187479#define FTS3_FULLTEXT_SEARCH2 2 /* Full-text index search */
187480
187481/*
187482** The lower 16-bits of the sqlite3_index_info.idxNum value set by
187483** the xBestIndex() method contains the Fts3Cursor.eSearch value described
187484** above. The upper 16-bits contain a combination of the following
187485** bits, used to describe extra constraints on full-text searches.
187486*/
187487#define FTS3_HAVE_LANGID0x00010000 0x00010000 /* languageid=? */
187488#define FTS3_HAVE_DOCID_GE0x00020000 0x00020000 /* docid>=? */
187489#define FTS3_HAVE_DOCID_LE0x00040000 0x00040000 /* docid<=? */
187490
187491struct Fts3Doclist {
187492 char *aAll; /* Array containing doclist (or NULL) */
187493 int nAll; /* Size of a[] in bytes */
187494 char *pNextDocid; /* Pointer to next docid */
187495
187496 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
187497 int bFreeList; /* True if pList should be sqlite3_free()d */
187498 char *pList; /* Pointer to position list following iDocid */
187499 int nList; /* Length of position list */
187500};
187501
187502/*
187503** A "phrase" is a sequence of one or more tokens that must match in
187504** sequence. A single token is the base case and the most common case.
187505** For a sequence of tokens contained in double-quotes (i.e. "one two three")
187506** nToken will be the number of tokens in the string.
187507*/
187508struct Fts3PhraseToken {
187509 char *z; /* Text of the token */
187510 int n; /* Number of bytes in buffer z */
187511 int isPrefix; /* True if token ends with a "*" character */
187512 int bFirst; /* True if token must appear at position 0 */
187513
187514 /* Variables above this point are populated when the expression is
187515 ** parsed (by code in fts3_expr.c). Below this point the variables are
187516 ** used when evaluating the expression. */
187517 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
187518 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
187519};
187520
187521struct Fts3Phrase {
187522 /* Cache of doclist for this phrase. */
187523 Fts3Doclist doclist;
187524 int bIncr; /* True if doclist is loaded incrementally */
187525 int iDoclistToken;
187526
187527 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
187528 ** OR condition. */
187529 char *pOrPoslist;
187530 i64 iOrDocid;
187531
187532 /* Variables below this point are populated by fts3_expr.c when parsing
187533 ** a MATCH expression. Everything above is part of the evaluation phase.
187534 */
187535 int nToken; /* Number of tokens in the phrase */
187536 int iColumn; /* Index of column this phrase must match */
187537 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
187538};
187539
187540/*
187541** A tree of these objects forms the RHS of a MATCH operator.
187542**
187543** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
187544** points to a malloced buffer, size nDoclist bytes, containing the results
187545** of this phrase query in FTS3 doclist format. As usual, the initial
187546** "Length" field found in doclists stored on disk is omitted from this
187547** buffer.
187548**
187549** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
187550** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
187551** where nCol is the number of columns in the queried FTS table. The array
187552** is populated as follows:
187553**
187554** aMI[iCol*3 + 0] = Undefined
187555** aMI[iCol*3 + 1] = Number of occurrences
187556** aMI[iCol*3 + 2] = Number of rows containing at least one instance
187557**
187558** The aMI array is allocated using sqlite3_malloc(). It should be freed
187559** when the expression node is.
187560*/
187561struct Fts3Expr {
187562 int eType; /* One of the FTSQUERY_XXX values defined below */
187563 int nNear; /* Valid if eType==FTSQUERY_NEAR */
187564 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
187565 Fts3Expr *pLeft; /* Left operand */
187566 Fts3Expr *pRight; /* Right operand */
187567 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
187568
187569 /* The following are used by the fts3_eval.c module. */
187570 sqlite3_int64 iDocid; /* Current docid */
187571 u8 bEof; /* True this expression is at EOF already */
187572 u8 bStart; /* True if iDocid is valid */
187573 u8 bDeferred; /* True if this expression is entirely deferred */
187574
187575 /* The following are used by the fts3_snippet.c module. */
187576 int iPhrase; /* Index of this phrase in matchinfo() results */
187577 u32 *aMI; /* See above */
187578};
187579
187580/*
187581** Candidate values for Fts3Query.eType. Note that the order of the first
187582** four values is in order of precedence when parsing expressions. For
187583** example, the following:
187584**
187585** "a OR b AND c NOT d NEAR e"
187586**
187587** is equivalent to:
187588**
187589** "a OR (b AND (c NOT (d NEAR e)))"
187590*/
187591#define FTSQUERY_NEAR1 1
187592#define FTSQUERY_NOT2 2
187593#define FTSQUERY_AND3 3
187594#define FTSQUERY_OR4 4
187595#define FTSQUERY_PHRASE5 5
187596
187597
187598/* fts3_write.c */
187599SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
187600SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *);
187601SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *);
187602SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *);
187603SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
187604 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
187605SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
187606 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
187607SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *);
187608SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
187609SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
187610
187611SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
187612SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
187613
187614#ifndef SQLITE_DISABLE_FTS4_DEFERRED
187615SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
187616SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
187617SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
187618SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
187619SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
187620#else
187621# define sqlite3Fts3FreeDeferredTokens(x)
187622# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK0
187623# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK0
187624# define sqlite3Fts3FreeDeferredDoclists(x)
187625# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK0
187626#endif
187627
187628SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *);
187629SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *, int *);
187630
187631/* Special values interpreted by sqlite3SegReaderCursor() */
187632#define FTS3_SEGCURSOR_PENDING-1 -1
187633#define FTS3_SEGCURSOR_ALL-2 -2
187634
187635SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
187636SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
187637SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
187638
187639SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(Fts3Table *,
187640 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
187641
187642/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
187643#define FTS3_SEGMENT_REQUIRE_POS0x00000001 0x00000001
187644#define FTS3_SEGMENT_IGNORE_EMPTY0x00000002 0x00000002
187645#define FTS3_SEGMENT_COLUMN_FILTER0x00000004 0x00000004
187646#define FTS3_SEGMENT_PREFIX0x00000008 0x00000008
187647#define FTS3_SEGMENT_SCAN0x00000010 0x00000010
187648#define FTS3_SEGMENT_FIRST0x00000020 0x00000020
187649
187650/* Type passed as 4th argument to SegmentReaderIterate() */
187651struct Fts3SegFilter {
187652 const char *zTerm;
187653 int nTerm;
187654 int iCol;
187655 int flags;
187656};
187657
187658struct Fts3MultiSegReader {
187659 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
187660 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
187661 int nSegment; /* Size of apSegment array */
187662 int nAdvance; /* How many seg-readers to advance */
187663 Fts3SegFilter *pFilter; /* Pointer to filter object */
187664 char *aBuffer; /* Buffer to merge doclists in */
187665 i64 nBuffer; /* Allocated size of aBuffer[] in bytes */
187666
187667 int iColFilter; /* If >=0, filter for this column */
187668 int bRestart;
187669
187670 /* Used by fts3.c only. */
187671 int nCost; /* Cost of running iterator */
187672 int bLookup; /* True if a lookup of a single entry. */
187673
187674 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
187675 char *zTerm; /* Pointer to term buffer */
187676 int nTerm; /* Size of zTerm in bytes */
187677 char *aDoclist; /* Pointer to doclist buffer */
187678 int nDoclist; /* Size of aDoclist[] in bytes */
187679};
187680
187681SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
187682
187683#define fts3GetVarint32(p, piVal)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (
*piVal=*(u8*)(p), 1) )
( \
187684 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
187685)
187686
187687/* fts3.c */
187688SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char**,const char*,...);
187689SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *, sqlite3_int64);
187690SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
187691SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *);
187692SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*);
187693SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *, int *);
187694SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64);
187695SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *);
187696SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
187697SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
187698SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
187699SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
187700SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
187701SQLITE_PRIVATEstatic int sqlite3Fts3ReadInt(const char *z, int *pnOut);
187702
187703/* fts3_tokenizer.c */
187704SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *, int *);
187705SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
187706SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
187707 sqlite3_tokenizer **, char **
187708);
187709SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char);
187710
187711/* fts3_snippet.c */
187712SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
187713SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
187714 const char *, const char *, int, int
187715);
187716SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
187717SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
187718
187719/* fts3_expr.c */
187720SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
187721 char **, int, int, int, const char *, int, Fts3Expr **, char **
187722);
187723SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *);
187724#ifdef SQLITE_TEST
187725SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
187726SQLITE_PRIVATEstatic int sqlite3Fts3InitTerm(sqlite3 *db);
187727#endif
187728SQLITE_PRIVATEstatic void *sqlite3Fts3MallocZero(i64 nByte);
187729
187730SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
187731 sqlite3_tokenizer_cursor **
187732);
187733
187734/* fts3_aux.c */
187735SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db);
187736
187737SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
187738
187739SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
187740 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
187741SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
187742 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
187743SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
187744SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
187745SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
187746
187747/* fts3_tokenize_vtab.c */
187748SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
187749
187750/* fts3_unicode2.c (functions generated by parsing unicode text files) */
187751#ifndef SQLITE_DISABLE_FTS3_UNICODE
187752SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int, int);
187753SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int);
187754SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int);
187755#endif
187756
187757SQLITE_PRIVATEstatic int sqlite3Fts3ExprIterate(Fts3Expr*, int (*x)(Fts3Expr*,int,void*), void*);
187758
187759SQLITE_PRIVATEstatic int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk);
187760
187761#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
187762#endif /* _FTSINT_H */
187763
187764/************** End of fts3Int.h *********************************************/
187765/************** Continuing where we left off in fts3.c ***********************/
187766#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
187767
187768#if defined(SQLITE_ENABLE_FTS31) && !defined(SQLITE_CORE1)
187769# define SQLITE_CORE1 1
187770#endif
187771
187772/* #include <assert.h> */
187773/* #include <stdlib.h> */
187774/* #include <stddef.h> */
187775/* #include <stdio.h> */
187776/* #include <string.h> */
187777/* #include <stdarg.h> */
187778
187779/* #include "fts3.h" */
187780#ifndef SQLITE_CORE1
187781/* # include "sqlite3ext.h" */
187782 SQLITE_EXTENSION_INIT1
187783#endif
187784
187785typedef struct Fts3HashWrapper Fts3HashWrapper;
187786struct Fts3HashWrapper {
187787 Fts3Hash hash; /* Hash table */
187788 int nRef; /* Number of pointers to this object */
187789};
187790
187791static int fts3EvalNext(Fts3Cursor *pCsr);
187792static int fts3EvalStart(Fts3Cursor *pCsr);
187793static int fts3TermSegReaderCursor(
187794 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
187795
187796/*
187797** This variable is set to false when running tests for which the on disk
187798** structures should not be corrupt. Otherwise, true. If it is false, extra
187799** assert() conditions in the fts3 code are activated - conditions that are
187800** only true if it is guaranteed that the fts3 database is not corrupt.
187801*/
187802#ifdef SQLITE_DEBUG
187803SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
187804#endif
187805
187806/*
187807** Write a 64-bit variable-length integer to memory starting at p[0].
187808** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
187809** The number of bytes written is returned.
187810*/
187811SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
187812 unsigned char *q = (unsigned char *) p;
187813 sqlite_uint64 vu = v;
187814 do{
187815 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
187816 vu >>= 7;
187817 }while( vu!=0 );
187818 q[-1] &= 0x7f; /* turn off high bit in final byte */
187819 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX )((void) (0));
187820 return (int) (q - (unsigned char *)p);
187821}
187822
187823#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; }
\
187824 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
187825 if( (v & mask2)==0 ){ var = v; return ret; }
187826#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret)v = (*ptr++); if( (v & mask2)==0 ){ var = v; return ret; } \
187827 v = (*ptr++); \
187828 if( (v & mask2)==0 ){ var = v; return ret; }
187829
187830SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
187831 const unsigned char *p = (const unsigned char*)pBuf;
187832 const unsigned char *pStart = p;
187833 u32 a;
187834 u64 b;
187835 int shift;
187836
187837 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1)a = (*p++); if( (a & 0x80)==0 ){ *v = a; return 1; };
187838 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; }
;
187839 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; }
;
187840 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; }
;
187841 b = (a & 0x0FFFFFFF );
187842
187843 for(shift=28; shift<=63; shift+=7){
187844 u64 c = *p++;
187845 b += (c&0x7F) << shift;
187846 if( (c & 0x80)==0 ) break;
187847 }
187848 *v = b;
187849 return (int)(p - pStart);
187850}
187851
187852/*
187853** Read a 64-bit variable-length integer from memory starting at p[0].
187854** Return the number of bytes read, or 0 on error.
187855** The value is stored in *v.
187856*/
187857SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
187858 return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v);
187859}
187860
187861/*
187862** Read a 64-bit variable-length integer from memory starting at p[0] and
187863** not extending past pEnd[-1].
187864** Return the number of bytes read, or 0 on error.
187865** The value is stored in *v.
187866*/
187867SQLITE_PRIVATEstatic int sqlite3Fts3GetVarintBounded(
187868 const char *pBuf,
187869 const char *pEnd,
187870 sqlite_int64 *v
187871){
187872 const unsigned char *p = (const unsigned char*)pBuf;
187873 const unsigned char *pStart = p;
187874 const unsigned char *pX = (const unsigned char*)pEnd;
187875 u64 b = 0;
187876 int shift;
187877 for(shift=0; shift<=63; shift+=7){
187878 u64 c = p<pX ? *p : 0;
187879 p++;
187880 b += (c&0x7F) << shift;
187881 if( (c & 0x80)==0 ) break;
187882 }
187883 *v = b;
187884 return (int)(p - pStart);
187885}
187886
187887/*
187888** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
187889** a non-negative 32-bit integer before it is returned.
187890*/
187891SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *p, int *pi){
187892 const unsigned char *ptr = (const unsigned char*)p;
187893 u32 a;
187894
187895#ifndef fts3GetVarint32
187896 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1)a = (*ptr++); if( (a & 0x80)==0 ){ *pi = a; return 1; };
187897#else
187898 a = (*ptr++);
187899 assert( a & 0x80 )((void) (0));
187900#endif
187901
187902 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; }
;
187903 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; }
;
187904 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; }
;
187905 a = (a & 0x0FFFFFFF );
187906 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
187907 assert( 0==(a & 0x80000000) )((void) (0));
187908 assert( *pi>=0 )((void) (0));
187909 return 5;
187910}
187911
187912/*
187913** Return the number of bytes required to encode v as a varint
187914*/
187915SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64 v){
187916 int i = 0;
187917 do{
187918 i++;
187919 v >>= 7;
187920 }while( v!=0 );
187921 return i;
187922}
187923
187924/*
187925** Convert an SQL-style quoted string into a normal string by removing
187926** the quote characters. The conversion is done in-place. If the
187927** input does not begin with a quote character, then this routine
187928** is a no-op.
187929**
187930** Examples:
187931**
187932** "abc" becomes abc
187933** 'xyz' becomes xyz
187934** [pqr] becomes pqr
187935** `mno` becomes mno
187936**
187937*/
187938SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *z){
187939 char quote; /* Quote character (if any ) */
187940
187941 quote = z[0];
187942 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
187943 int iIn = 1; /* Index of next byte to read from input */
187944 int iOut = 0; /* Index of next byte to write to output */
187945
187946 /* If the first byte was a '[', then the close-quote character is a ']' */
187947 if( quote=='[' ) quote = ']';
187948
187949 while( z[iIn] ){
187950 if( z[iIn]==quote ){
187951 if( z[iIn+1]!=quote ) break;
187952 z[iOut++] = quote;
187953 iIn += 2;
187954 }else{
187955 z[iOut++] = z[iIn++];
187956 }
187957 }
187958 z[iOut] = '\0';
187959 }
187960}
187961
187962/*
187963** Read a single varint from the doclist at *pp and advance *pp to point
187964** to the first byte past the end of the varint. Add the value of the varint
187965** to *pVal.
187966*/
187967static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
187968 sqlite3_int64 iVal;
187969 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
187970 *pVal += iVal;
187971}
187972
187973/*
187974** When this function is called, *pp points to the first byte following a
187975** varint that is part of a doclist (or position-list, or any other list
187976** of varints). This function moves *pp to point to the start of that varint,
187977** and sets *pVal by the varint value.
187978**
187979** Argument pStart points to the first byte of the doclist that the
187980** varint is part of.
187981*/
187982static void fts3GetReverseVarint(
187983 char **pp,
187984 char *pStart,
187985 sqlite3_int64 *pVal
187986){
187987 sqlite3_int64 iVal;
187988 char *p;
187989
187990 /* Pointer p now points at the first byte past the varint we are
187991 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
187992 ** clear on character p[-1]. */
187993 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
187994 p++;
187995 *pp = p;
187996
187997 sqlite3Fts3GetVarint(p, &iVal);
187998 *pVal = iVal;
187999}
188000
188001/*
188002** The xDisconnect() virtual table method.
188003*/
188004static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
188005 Fts3Table *p = (Fts3Table *)pVtab;
188006 int i;
188007
188008 assert( p->nPendingData==0 )((void) (0));
188009 assert( p->pSegments==0 )((void) (0));
188010
188011 /* Free any prepared statements held */
188012 sqlite3_finalize(p->pSeekStmt);
188013 for(i=0; i<SizeofArray(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
188014 sqlite3_finalize(p->aStmt[i]);
188015 }
188016 sqlite3_free(p->zSegmentsTbl);
188017 sqlite3_free(p->zReadExprlist);
188018 sqlite3_free(p->zWriteExprlist);
188019 sqlite3_free(p->zContentTbl);
188020 sqlite3_free(p->zLanguageid);
188021
188022 /* Invoke the tokenizer destructor to free the tokenizer. */
188023 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
188024
188025 sqlite3_free(p);
188026 return SQLITE_OK0;
188027}
188028
188029/*
188030** Write an error message into *pzErr
188031*/
188032SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
188033 va_list ap;
188034 sqlite3_free(*pzErr);
188035 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
188036 *pzErr = sqlite3_vmprintf(zFormat, ap);
188037 va_end(ap)__builtin_va_end(ap);
188038}
188039
188040/*
188041** Construct one or more SQL statements from the format string given
188042** and then evaluate those statements. The success code is written
188043** into *pRc.
188044**
188045** If *pRc is initially non-zero then this routine is a no-op.
188046*/
188047static void fts3DbExec(
188048 int *pRc, /* Success code */
188049 sqlite3 *db, /* Database in which to run SQL */
188050 const char *zFormat, /* Format string for SQL */
188051 ... /* Arguments to the format string */
188052){
188053 va_list ap;
188054 char *zSql;
188055 if( *pRc ) return;
188056 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
188057 zSql = sqlite3_vmprintf(zFormat, ap);
188058 va_end(ap)__builtin_va_end(ap);
188059 if( zSql==0 ){
188060 *pRc = SQLITE_NOMEM7;
188061 }else{
188062 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
188063 sqlite3_free(zSql);
188064 }
188065}
188066
188067/*
188068** The xDestroy() virtual table method.
188069*/
188070static int fts3DestroyMethod(sqlite3_vtab *pVtab){
188071 Fts3Table *p = (Fts3Table *)pVtab;
188072 int rc = SQLITE_OK0; /* Return code */
188073 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
188074 sqlite3 *db = p->db; /* Database handle */
188075
188076 /* Drop the shadow tables */
188077 fts3DbExec(&rc, db,
188078 "DROP TABLE IF EXISTS %Q.'%q_segments';"
188079 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
188080 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
188081 "DROP TABLE IF EXISTS %Q.'%q_stat';"
188082 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
188083 zDb, p->zName,
188084 zDb, p->zName,
188085 zDb, p->zName,
188086 zDb, p->zName,
188087 (p->zContentTbl ? "--" : ""), zDb,p->zName
188088 );
188089
188090 /* If everything has worked, invoke fts3DisconnectMethod() to free the
188091 ** memory associated with the Fts3Table structure and return SQLITE_OK.
188092 ** Otherwise, return an SQLite error code.
188093 */
188094 return (rc==SQLITE_OK0 ? fts3DisconnectMethod(pVtab) : rc);
188095}
188096
188097
188098/*
188099** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
188100** passed as the first argument. This is done as part of the xConnect()
188101** and xCreate() methods.
188102**
188103** If *pRc is non-zero when this function is called, it is a no-op.
188104** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
188105** before returning.
188106*/
188107static void fts3DeclareVtab(int *pRc, Fts3Table *p){
188108 if( *pRc==SQLITE_OK0 ){
188109 int i; /* Iterator variable */
188110 int rc; /* Return code */
188111 char *zSql; /* SQL statement passed to declare_vtab() */
188112 char *zCols; /* List of user defined columns */
188113 const char *zLanguageid;
188114
188115 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
188116 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
188117 sqlite3_vtab_config(p->db, SQLITE_VTAB_INNOCUOUS2);
188118
188119 /* Create a list of user columns for the virtual table */
188120 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
188121 for(i=1; zCols && i<p->nColumn; i++){
188122 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
188123 }
188124
188125 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
188126 zSql = sqlite3_mprintf(
188127 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
188128 zCols, p->zName, zLanguageid
188129 );
188130 if( !zCols || !zSql ){
188131 rc = SQLITE_NOMEM7;
188132 }else{
188133 rc = sqlite3_declare_vtab(p->db, zSql);
188134 }
188135
188136 sqlite3_free(zSql);
188137 sqlite3_free(zCols);
188138 *pRc = rc;
188139 }
188140}
188141
188142/*
188143** Create the %_stat table if it does not already exist.
188144*/
188145SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
188146 fts3DbExec(pRc, p->db,
188147 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
188148 "(id INTEGER PRIMARY KEY, value BLOB);",
188149 p->zDb, p->zName
188150 );
188151 if( (*pRc)==SQLITE_OK0 ) p->bHasStat = 1;
188152}
188153
188154/*
188155** Create the backing store tables (%_content, %_segments and %_segdir)
188156** required by the FTS3 table passed as the only argument. This is done
188157** as part of the vtab xCreate() method.
188158**
188159** If the p->bHasDocsize boolean is true (indicating that this is an
188160** FTS4 table, not an FTS3 table) then also create the %_docsize and
188161** %_stat tables required by FTS4.
188162*/
188163static int fts3CreateTables(Fts3Table *p){
188164 int rc = SQLITE_OK0; /* Return code */
188165 int i; /* Iterator variable */
188166 sqlite3 *db = p->db; /* The database connection */
188167
188168 if( p->zContentTbl==0 ){
188169 const char *zLanguageid = p->zLanguageid;
188170 char *zContentCols; /* Columns of %_content table */
188171
188172 /* Create a list of user columns for the content table */
188173 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
188174 for(i=0; zContentCols && i<p->nColumn; i++){
188175 char *z = p->azColumn[i];
188176 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
188177 }
188178 if( zLanguageid && zContentCols ){
188179 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
188180 }
188181 if( zContentCols==0 ) rc = SQLITE_NOMEM7;
188182
188183 /* Create the content table */
188184 fts3DbExec(&rc, db,
188185 "CREATE TABLE %Q.'%q_content'(%s)",
188186 p->zDb, p->zName, zContentCols
188187 );
188188 sqlite3_free(zContentCols);
188189 }
188190
188191 /* Create other tables */
188192 fts3DbExec(&rc, db,
188193 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
188194 p->zDb, p->zName
188195 );
188196 fts3DbExec(&rc, db,
188197 "CREATE TABLE %Q.'%q_segdir'("
188198 "level INTEGER,"
188199 "idx INTEGER,"
188200 "start_block INTEGER,"
188201 "leaves_end_block INTEGER,"
188202 "end_block INTEGER,"
188203 "root BLOB,"
188204 "PRIMARY KEY(level, idx)"
188205 ");",
188206 p->zDb, p->zName
188207 );
188208 if( p->bHasDocsize ){
188209 fts3DbExec(&rc, db,
188210 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
188211 p->zDb, p->zName
188212 );
188213 }
188214 assert( p->bHasStat==p->bFts4 )((void) (0));
188215 if( p->bHasStat ){
188216 sqlite3Fts3CreateStatTable(&rc, p);
188217 }
188218 return rc;
188219}
188220
188221/*
188222** Store the current database page-size in bytes in p->nPgsz.
188223**
188224** If *pRc is non-zero when this function is called, it is a no-op.
188225** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
188226** before returning.
188227*/
188228static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
188229 if( *pRc==SQLITE_OK0 ){
188230 int rc; /* Return code */
188231 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
188232 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
188233
188234 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
188235 if( !zSql ){
188236 rc = SQLITE_NOMEM7;
188237 }else{
188238 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
188239 if( rc==SQLITE_OK0 ){
188240 sqlite3_step(pStmt);
188241 p->nPgsz = sqlite3_column_int(pStmt, 0);
188242 rc = sqlite3_finalize(pStmt);
188243 }else if( rc==SQLITE_AUTH23 ){
188244 p->nPgsz = 1024;
188245 rc = SQLITE_OK0;
188246 }
188247 }
188248 assert( p->nPgsz>0 || rc!=SQLITE_OK )((void) (0));
188249 sqlite3_free(zSql);
188250 *pRc = rc;
188251 }
188252}
188253
188254/*
188255** "Special" FTS4 arguments are column specifications of the following form:
188256**
188257** <key> = <value>
188258**
188259** There may not be whitespace surrounding the "=" character. The <value>
188260** term may be quoted, but the <key> may not.
188261*/
188262static int fts3IsSpecialColumn(
188263 const char *z,
188264 int *pnKey,
188265 char **pzValue
188266){
188267 char *zValue;
188268 const char *zCsr = z;
188269
188270 while( *zCsr!='=' ){
188271 if( *zCsr=='\0' ) return 0;
188272 zCsr++;
188273 }
188274
188275 *pnKey = (int)(zCsr-z);
188276 zValue = sqlite3_mprintf("%s", &zCsr[1]);
188277 if( zValue ){
188278 sqlite3Fts3Dequote(zValue);
188279 }
188280 *pzValue = zValue;
188281 return 1;
188282}
188283
188284/*
188285** Append the output of a printf() style formatting to an existing string.
188286*/
188287static void fts3Appendf(
188288 int *pRc, /* IN/OUT: Error code */
188289 char **pz, /* IN/OUT: Pointer to string buffer */
188290 const char *zFormat, /* Printf format string to append */
188291 ... /* Arguments for printf format string */
188292){
188293 if( *pRc==SQLITE_OK0 ){
188294 va_list ap;
188295 char *z;
188296 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
188297 z = sqlite3_vmprintf(zFormat, ap);
188298 va_end(ap)__builtin_va_end(ap);
188299 if( z && *pz ){
188300 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
188301 sqlite3_free(z);
188302 z = z2;
188303 }
188304 if( z==0 ) *pRc = SQLITE_NOMEM7;
188305 sqlite3_free(*pz);
188306 *pz = z;
188307 }
188308}
188309
188310/*
188311** Return a copy of input string zInput enclosed in double-quotes (") and
188312** with all double quote characters escaped. For example:
188313**
188314** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
188315**
188316** The pointer returned points to memory obtained from sqlite3_malloc(). It
188317** is the callers responsibility to call sqlite3_free() to release this
188318** memory.
188319*/
188320static char *fts3QuoteId(char const *zInput){
188321 sqlite3_int64 nRet;
188322 char *zRet;
188323 nRet = 2 + (int)strlen(zInput)*2 + 1;
188324 zRet = sqlite3_malloc64(nRet);
188325 if( zRet ){
188326 int i;
188327 char *z = zRet;
188328 *(z++) = '"';
188329 for(i=0; zInput[i]; i++){
188330 if( zInput[i]=='"' ) *(z++) = '"';
188331 *(z++) = zInput[i];
188332 }
188333 *(z++) = '"';
188334 *(z++) = '\0';
188335 }
188336 return zRet;
188337}
188338
188339/*
188340** Return a list of comma separated SQL expressions and a FROM clause that
188341** could be used in a SELECT statement such as the following:
188342**
188343** SELECT <list of expressions> FROM %_content AS x ...
188344**
188345** to return the docid, followed by each column of text data in order
188346** from left to write. If parameter zFunc is not NULL, then instead of
188347** being returned directly each column of text data is passed to an SQL
188348** function named zFunc first. For example, if zFunc is "unzip" and the
188349** table has the three user-defined columns "a", "b", and "c", the following
188350** string is returned:
188351**
188352** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
188353**
188354** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
188355** is the responsibility of the caller to eventually free it.
188356**
188357** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
188358** a NULL pointer is returned). Otherwise, if an OOM error is encountered
188359** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
188360** no error occurs, *pRc is left unmodified.
188361*/
188362static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
188363 char *zRet = 0;
188364 char *zFree = 0;
188365 char *zFunction;
188366 int i;
188367
188368 if( p->zContentTbl==0 ){
188369 if( !zFunc ){
188370 zFunction = "";
188371 }else{
188372 zFree = zFunction = fts3QuoteId(zFunc);
188373 }
188374 fts3Appendf(pRc, &zRet, "docid");
188375 for(i=0; i<p->nColumn; i++){
188376 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
188377 }
188378 if( p->zLanguageid ){
188379 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
188380 }
188381 sqlite3_free(zFree);
188382 }else{
188383 fts3Appendf(pRc, &zRet, "rowid");
188384 for(i=0; i<p->nColumn; i++){
188385 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
188386 }
188387 if( p->zLanguageid ){
188388 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
188389 }
188390 }
188391 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
188392 p->zDb,
188393 (p->zContentTbl ? p->zContentTbl : p->zName),
188394 (p->zContentTbl ? "" : "_content")
188395 );
188396 return zRet;
188397}
188398
188399/*
188400** Return a list of N comma separated question marks, where N is the number
188401** of columns in the %_content table (one for the docid plus one for each
188402** user-defined text column).
188403**
188404** If argument zFunc is not NULL, then all but the first question mark
188405** is preceded by zFunc and an open bracket, and followed by a closed
188406** bracket. For example, if zFunc is "zip" and the FTS3 table has three
188407** user-defined text columns, the following string is returned:
188408**
188409** "?, zip(?), zip(?), zip(?)"
188410**
188411** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
188412** is the responsibility of the caller to eventually free it.
188413**
188414** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
188415** a NULL pointer is returned). Otherwise, if an OOM error is encountered
188416** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
188417** no error occurs, *pRc is left unmodified.
188418*/
188419static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
188420 char *zRet = 0;
188421 char *zFree = 0;
188422 char *zFunction;
188423 int i;
188424
188425 if( !zFunc ){
188426 zFunction = "";
188427 }else{
188428 zFree = zFunction = fts3QuoteId(zFunc);
188429 }
188430 fts3Appendf(pRc, &zRet, "?");
188431 for(i=0; i<p->nColumn; i++){
188432 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
188433 }
188434 if( p->zLanguageid ){
188435 fts3Appendf(pRc, &zRet, ", ?");
188436 }
188437 sqlite3_free(zFree);
188438 return zRet;
188439}
188440
188441/*
188442** Buffer z contains a positive integer value encoded as utf-8 text.
188443** Decode this value and store it in *pnOut, returning the number of bytes
188444** consumed. If an overflow error occurs return a negative value.
188445*/
188446SQLITE_PRIVATEstatic int sqlite3Fts3ReadInt(const char *z, int *pnOut){
188447 u64 iVal = 0;
188448 int i;
188449 for(i=0; z[i]>='0' && z[i]<='9'; i++){
188450 iVal = iVal*10 + (z[i] - '0');
188451 if( iVal>0x7FFFFFFF ) return -1;
188452 }
188453 *pnOut = (int)iVal;
188454 return i;
188455}
188456
188457/*
188458** This function interprets the string at (*pp) as a non-negative integer
188459** value. It reads the integer and sets *pnOut to the value read, then
188460** sets *pp to point to the byte immediately following the last byte of
188461** the integer value.
188462**
188463** Only decimal digits ('0'..'9') may be part of an integer value.
188464**
188465** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
188466** the output value undefined. Otherwise SQLITE_OK is returned.
188467**
188468** This function is used when parsing the "prefix=" FTS4 parameter.
188469*/
188470static int fts3GobbleInt(const char **pp, int *pnOut){
188471 const int MAX_NPREFIX = 10000000;
188472 int nInt = 0; /* Output value */
188473 int nByte;
188474 nByte = sqlite3Fts3ReadInt(*pp, &nInt);
188475 if( nInt>MAX_NPREFIX ){
188476 nInt = 0;
188477 }
188478 if( nByte==0 ){
188479 return SQLITE_ERROR1;
188480 }
188481 *pnOut = nInt;
188482 *pp += nByte;
188483 return SQLITE_OK0;
188484}
188485
188486/*
188487** This function is called to allocate an array of Fts3Index structures
188488** representing the indexes maintained by the current FTS table. FTS tables
188489** always maintain the main "terms" index, but may also maintain one or
188490** more "prefix" indexes, depending on the value of the "prefix=" parameter
188491** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
188492**
188493** Argument zParam is passed the value of the "prefix=" option if one was
188494** specified, or NULL otherwise.
188495**
188496** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
188497** the allocated array. *pnIndex is set to the number of elements in the
188498** array. If an error does occur, an SQLite error code is returned.
188499**
188500** Regardless of whether or not an error is returned, it is the responsibility
188501** of the caller to call sqlite3_free() on the output array to free it.
188502*/
188503static int fts3PrefixParameter(
188504 const char *zParam, /* ABC in prefix=ABC parameter to parse */
188505 int *pnIndex, /* OUT: size of *apIndex[] array */
188506 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
188507){
188508 struct Fts3Index *aIndex; /* Allocated array */
188509 int nIndex = 1; /* Number of entries in array */
188510
188511 if( zParam && zParam[0] ){
188512 const char *p;
188513 nIndex++;
188514 for(p=zParam; *p; p++){
188515 if( *p==',' ) nIndex++;
188516 }
188517 }
188518
188519 aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
188520 *apIndex = aIndex;
188521 if( !aIndex ){
188522 return SQLITE_NOMEM7;
188523 }
188524
188525 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
188526 if( zParam ){
188527 const char *p = zParam;
188528 int i;
188529 for(i=1; i<nIndex; i++){
188530 int nPrefix = 0;
188531 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR1;
188532 assert( nPrefix>=0 )((void) (0));
188533 if( nPrefix==0 ){
188534 nIndex--;
188535 i--;
188536 }else{
188537 aIndex[i].nPrefix = nPrefix;
188538 }
188539 p++;
188540 }
188541 }
188542
188543 *pnIndex = nIndex;
188544 return SQLITE_OK0;
188545}
188546
188547/*
188548** This function is called when initializing an FTS4 table that uses the
188549** content=xxx option. It determines the number of and names of the columns
188550** of the new FTS4 table.
188551**
188552** The third argument passed to this function is the value passed to the
188553** config=xxx option (i.e. "xxx"). This function queries the database for
188554** a table of that name. If found, the output variables are populated
188555** as follows:
188556**
188557** *pnCol: Set to the number of columns table xxx has,
188558**
188559** *pnStr: Set to the total amount of space required to store a copy
188560** of each columns name, including the nul-terminator.
188561**
188562** *pazCol: Set to point to an array of *pnCol strings. Each string is
188563** the name of the corresponding column in table xxx. The array
188564** and its contents are allocated using a single allocation. It
188565** is the responsibility of the caller to free this allocation
188566** by eventually passing the *pazCol value to sqlite3_free().
188567**
188568** If the table cannot be found, an error code is returned and the output
188569** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
188570** returned (and the output variables are undefined).
188571*/
188572static int fts3ContentColumns(
188573 sqlite3 *db, /* Database handle */
188574 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
188575 const char *zTbl, /* Name of content table */
188576 const char ***pazCol, /* OUT: Malloc'd array of column names */
188577 int *pnCol, /* OUT: Size of array *pazCol */
188578 int *pnStr, /* OUT: Bytes of string content */
188579 char **pzErr /* OUT: error message */
188580){
188581 int rc = SQLITE_OK0; /* Return code */
188582 char *zSql; /* "SELECT *" statement on zTbl */
188583 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
188584
188585 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
188586 if( !zSql ){
188587 rc = SQLITE_NOMEM7;
188588 }else{
188589 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
188590 if( rc!=SQLITE_OK0 ){
188591 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
188592 }
188593 }
188594 sqlite3_free(zSql);
188595
188596 if( rc==SQLITE_OK0 ){
188597 const char **azCol; /* Output array */
188598 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
188599 int nCol; /* Number of table columns */
188600 int i; /* Used to iterate through columns */
188601
188602 /* Loop through the returned columns. Set nStr to the number of bytes of
188603 ** space required to store a copy of each column name, including the
188604 ** nul-terminator byte. */
188605 nCol = sqlite3_column_count(pStmt);
188606 for(i=0; i<nCol; i++){
188607 const char *zCol = sqlite3_column_name(pStmt, i);
188608 nStr += strlen(zCol) + 1;
188609 }
188610
188611 /* Allocate and populate the array to return. */
188612 azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
188613 if( azCol==0 ){
188614 rc = SQLITE_NOMEM7;
188615 }else{
188616 char *p = (char *)&azCol[nCol];
188617 for(i=0; i<nCol; i++){
188618 const char *zCol = sqlite3_column_name(pStmt, i);
188619 int n = (int)strlen(zCol)+1;
188620 memcpy(p, zCol, n);
188621 azCol[i] = p;
188622 p += n;
188623 }
188624 }
188625 sqlite3_finalize(pStmt);
188626
188627 /* Set the output variables. */
188628 *pnCol = nCol;
188629 *pnStr = nStr;
188630 *pazCol = azCol;
188631 }
188632
188633 return rc;
188634}
188635
188636/*
188637** This function is the implementation of both the xConnect and xCreate
188638** methods of the FTS3 virtual table.
188639**
188640** The argv[] array contains the following:
188641**
188642** argv[0] -> module name ("fts3" or "fts4")
188643** argv[1] -> database name
188644** argv[2] -> table name
188645** argv[...] -> "column name" and other module argument fields.
188646*/
188647static int fts3InitVtab(
188648 int isCreate, /* True for xCreate, false for xConnect */
188649 sqlite3 *db, /* The SQLite database connection */
188650 void *pAux, /* Hash table containing tokenizers */
188651 int argc, /* Number of elements in argv array */
188652 const char * const *argv, /* xCreate/xConnect argument array */
188653 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
188654 char **pzErr /* Write any error message here */
188655){
188656 Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
188657 Fts3Table *p = 0; /* Pointer to allocated vtab */
188658 int rc = SQLITE_OK0; /* Return code */
188659 int i; /* Iterator variable */
188660 sqlite3_int64 nByte; /* Size of allocation used for *p */
188661 int iCol; /* Column index */
188662 int nString = 0; /* Bytes required to hold all column names */
188663 int nCol = 0; /* Number of columns in the FTS table */
188664 char *zCsr; /* Space for holding column names */
188665 int nDb; /* Bytes required to hold database name */
188666 int nName; /* Bytes required to hold table name */
188667 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
188668 const char **aCol; /* Array of column names */
188669 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
188670
188671 int nIndex = 0; /* Size of aIndex[] array */
188672 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
188673
188674 /* The results of parsing supported FTS4 key=value options: */
188675 int bNoDocsize = 0; /* True to omit %_docsize table */
188676 int bDescIdx = 0; /* True to store descending indexes */
188677 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
188678 char *zCompress = 0; /* compress=? parameter (or NULL) */
188679 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
188680 char *zContent = 0; /* content=? parameter (or NULL) */
188681 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
188682 char **azNotindexed = 0; /* The set of notindexed= columns */
188683 int nNotindexed = 0; /* Size of azNotindexed[] array */
188684
188685 assert( strlen(argv[0])==4 )((void) (0));
188686 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)((void) (0))
188687 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)((void) (0))
188688 )((void) (0));
188689
188690 nDb = (int)strlen(argv[1]) + 1;
188691 nName = (int)strlen(argv[2]) + 1;
188692
188693 nByte = sizeof(const char *) * (argc-2);
188694 aCol = (const char **)sqlite3_malloc64(nByte);
188695 if( aCol ){
188696 memset((void*)aCol, 0, nByte);
188697 azNotindexed = (char **)sqlite3_malloc64(nByte);
188698 }
188699 if( azNotindexed ){
188700 memset(azNotindexed, 0, nByte);
188701 }
188702 if( !aCol || !azNotindexed ){
188703 rc = SQLITE_NOMEM7;
188704 goto fts3_init_out;
188705 }
188706
188707 /* Loop through all of the arguments passed by the user to the FTS3/4
188708 ** module (i.e. all the column names and special arguments). This loop
188709 ** does the following:
188710 **
188711 ** + Figures out the number of columns the FTSX table will have, and
188712 ** the number of bytes of space that must be allocated to store copies
188713 ** of the column names.
188714 **
188715 ** + If there is a tokenizer specification included in the arguments,
188716 ** initializes the tokenizer pTokenizer.
188717 */
188718 for(i=3; rc==SQLITE_OK0 && i<argc; i++){
188719 char const *z = argv[i];
188720 int nKey;
188721 char *zVal;
188722
188723 /* Check if this is a tokenizer specification */
188724 if( !pTokenizer
188725 && strlen(z)>8
188726 && 0==sqlite3_strnicmp(z, "tokenize", 8)
188727 && 0==sqlite3Fts3IsIdChar(z[8])
188728 ){
188729 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
188730 }
188731
188732 /* Check if it is an FTS4 special argument. */
188733 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
188734 struct Fts4Option {
188735 const char *zOpt;
188736 int nOpt;
188737 } aFts4Opt[] = {
188738 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
188739 { "prefix", 6 }, /* 1 -> PREFIX */
188740 { "compress", 8 }, /* 2 -> COMPRESS */
188741 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
188742 { "order", 5 }, /* 4 -> ORDER */
188743 { "content", 7 }, /* 5 -> CONTENT */
188744 { "languageid", 10 }, /* 6 -> LANGUAGEID */
188745 { "notindexed", 10 } /* 7 -> NOTINDEXED */
188746 };
188747
188748 int iOpt;
188749 if( !zVal ){
188750 rc = SQLITE_NOMEM7;
188751 }else{
188752 for(iOpt=0; iOpt<SizeofArray(aFts4Opt)((int)(sizeof(aFts4Opt)/sizeof(aFts4Opt[0]))); iOpt++){
188753 struct Fts4Option *pOp = &aFts4Opt[iOpt];
188754 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
188755 break;
188756 }
188757 }
188758 switch( iOpt ){
188759 case 0: /* MATCHINFO */
188760 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
188761 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
188762 rc = SQLITE_ERROR1;
188763 }
188764 bNoDocsize = 1;
188765 break;
188766
188767 case 1: /* PREFIX */
188768 sqlite3_free(zPrefix);
188769 zPrefix = zVal;
188770 zVal = 0;
188771 break;
188772
188773 case 2: /* COMPRESS */
188774 sqlite3_free(zCompress);
188775 zCompress = zVal;
188776 zVal = 0;
188777 break;
188778
188779 case 3: /* UNCOMPRESS */
188780 sqlite3_free(zUncompress);
188781 zUncompress = zVal;
188782 zVal = 0;
188783 break;
188784
188785 case 4: /* ORDER */
188786 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
188787 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
188788 ){
188789 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
188790 rc = SQLITE_ERROR1;
188791 }
188792 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
188793 break;
188794
188795 case 5: /* CONTENT */
188796 sqlite3_free(zContent);
188797 zContent = zVal;
188798 zVal = 0;
188799 break;
188800
188801 case 6: /* LANGUAGEID */
188802 assert( iOpt==6 )((void) (0));
188803 sqlite3_free(zLanguageid);
188804 zLanguageid = zVal;
188805 zVal = 0;
188806 break;
188807
188808 case 7: /* NOTINDEXED */
188809 azNotindexed[nNotindexed++] = zVal;
188810 zVal = 0;
188811 break;
188812
188813 default:
188814 assert( iOpt==SizeofArray(aFts4Opt) )((void) (0));
188815 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
188816 rc = SQLITE_ERROR1;
188817 break;
188818 }
188819 sqlite3_free(zVal);
188820 }
188821 }
188822
188823 /* Otherwise, the argument is a column name. */
188824 else {
188825 nString += (int)(strlen(z) + 1);
188826 aCol[nCol++] = z;
188827 }
188828 }
188829
188830 /* If a content=xxx option was specified, the following:
188831 **
188832 ** 1. Ignore any compress= and uncompress= options.
188833 **
188834 ** 2. If no column names were specified as part of the CREATE VIRTUAL
188835 ** TABLE statement, use all columns from the content table.
188836 */
188837 if( rc==SQLITE_OK0 && zContent ){
188838 sqlite3_free(zCompress);
188839 sqlite3_free(zUncompress);
188840 zCompress = 0;
188841 zUncompress = 0;
188842 if( nCol==0 ){
188843 sqlite3_free((void*)aCol);
188844 aCol = 0;
188845 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
188846
188847 /* If a languageid= option was specified, remove the language id
188848 ** column from the aCol[] array. */
188849 if( rc==SQLITE_OK0 && zLanguageid ){
188850 int j;
188851 for(j=0; j<nCol; j++){
188852 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
188853 int k;
188854 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
188855 nCol--;
188856 break;
188857 }
188858 }
188859 }
188860 }
188861 }
188862 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
188863
188864 if( nCol==0 ){
188865 assert( nString==0 )((void) (0));
188866 aCol[0] = "content";
188867 nString = 8;
188868 nCol = 1;
188869 }
188870
188871 if( pTokenizer==0 ){
188872 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
188873 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
188874 }
188875 assert( pTokenizer )((void) (0));
188876
188877 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
188878 if( rc==SQLITE_ERROR1 ){
188879 assert( zPrefix )((void) (0));
188880 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
188881 }
188882 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
188883
188884 /* Allocate and populate the Fts3Table structure. */
188885 nByte = sizeof(Fts3Table) + /* Fts3Table */
188886 nCol * sizeof(char *) + /* azColumn */
188887 nIndex * sizeof(struct Fts3Index) + /* aIndex */
188888 nCol * sizeof(u8) + /* abNotindexed */
188889 nName + /* zName */
188890 nDb + /* zDb */
188891 nString; /* Space for azColumn strings */
188892 p = (Fts3Table*)sqlite3_malloc64(nByte);
188893 if( p==0 ){
188894 rc = SQLITE_NOMEM7;
188895 goto fts3_init_out;
188896 }
188897 memset(p, 0, nByte);
188898 p->db = db;
188899 p->nColumn = nCol;
188900 p->nPendingData = 0;
188901 p->azColumn = (char **)&p[1];
188902 p->pTokenizer = pTokenizer;
188903 p->nMaxPendingData = FTS3_MAX_PENDING_DATA(1*1024*1024);
188904 p->bHasDocsize = (isFts4 && bNoDocsize==0);
188905 p->bHasStat = (u8)isFts4;
188906 p->bFts4 = (u8)isFts4;
188907 p->bDescIdx = (u8)bDescIdx;
188908 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
188909 p->zContentTbl = zContent;
188910 p->zLanguageid = zLanguageid;
188911 zContent = 0;
188912 zLanguageid = 0;
188913 TESTONLY( p->inTransaction = -1 );
188914 TESTONLY( p->mxSavepoint = -1 );
188915
188916 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
188917 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
188918 p->nIndex = nIndex;
188919 for(i=0; i<nIndex; i++){
188920 fts3HashInitsqlite3Fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING1, 1);
188921 }
188922 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
188923
188924 /* Fill in the zName and zDb fields of the vtab structure. */
188925 zCsr = (char *)&p->abNotindexed[nCol];
188926 p->zName = zCsr;
188927 memcpy(zCsr, argv[2], nName);
188928 zCsr += nName;
188929 p->zDb = zCsr;
188930 memcpy(zCsr, argv[1], nDb);
188931 zCsr += nDb;
188932
188933 /* Fill in the azColumn array */
188934 for(iCol=0; iCol<nCol; iCol++){
188935 char *z;
188936 int n = 0;
188937 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
188938 if( n>0 ){
188939 memcpy(zCsr, z, n);
188940 }
188941 zCsr[n] = '\0';
188942 sqlite3Fts3Dequote(zCsr);
188943 p->azColumn[iCol] = zCsr;
188944 zCsr += n+1;
188945 assert( zCsr <= &((char *)p)[nByte] )((void) (0));
188946 }
188947
188948 /* Fill in the abNotindexed array */
188949 for(iCol=0; iCol<nCol; iCol++){
188950 int n = (int)strlen(p->azColumn[iCol]);
188951 for(i=0; i<nNotindexed; i++){
188952 char *zNot = azNotindexed[i];
188953 if( zNot && n==(int)strlen(zNot)
188954 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
188955 ){
188956 p->abNotindexed[iCol] = 1;
188957 sqlite3_free(zNot);
188958 azNotindexed[i] = 0;
188959 }
188960 }
188961 }
188962 for(i=0; i<nNotindexed; i++){
188963 if( azNotindexed[i] ){
188964 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
188965 rc = SQLITE_ERROR1;
188966 }
188967 }
188968
188969 if( rc==SQLITE_OK0 && (zCompress==0)!=(zUncompress==0) ){
188970 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
188971 rc = SQLITE_ERROR1;
188972 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
188973 }
188974 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
188975 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
188976 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
188977
188978 /* If this is an xCreate call, create the underlying tables in the
188979 ** database. TODO: For xConnect(), it could verify that said tables exist.
188980 */
188981 if( isCreate ){
188982 rc = fts3CreateTables(p);
188983 }
188984
188985 /* Check to see if a legacy fts3 table has been "upgraded" by the
188986 ** addition of a %_stat table so that it can use incremental merge.
188987 */
188988 if( !isFts4 && !isCreate ){
188989 p->bHasStat = 2;
188990 }
188991
188992 /* Figure out the page-size for the database. This is required in order to
188993 ** estimate the cost of loading large doclists from the database. */
188994 fts3DatabasePageSize(&rc, p);
188995 p->nNodeSize = p->nPgsz-35;
188996
188997#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
188998 p->nMergeCount = FTS3_MERGE_COUNT16;
188999#endif
189000
189001 /* Declare the table schema to SQLite. */
189002 fts3DeclareVtab(&rc, p);
189003
189004fts3_init_out:
189005 sqlite3_free(zPrefix);
189006 sqlite3_free(aIndex);
189007 sqlite3_free(zCompress);
189008 sqlite3_free(zUncompress);
189009 sqlite3_free(zContent);
189010 sqlite3_free(zLanguageid);
189011 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
189012 sqlite3_free((void *)aCol);
189013 sqlite3_free((void *)azNotindexed);
189014 if( rc!=SQLITE_OK0 ){
189015 if( p ){
189016 fts3DisconnectMethod((sqlite3_vtab *)p);
189017 }else if( pTokenizer ){
189018 pTokenizer->pModule->xDestroy(pTokenizer);
189019 }
189020 }else{
189021 assert( p->pSegments==0 )((void) (0));
189022 *ppVTab = &p->base;
189023 }
189024 return rc;
189025}
189026
189027/*
189028** The xConnect() and xCreate() methods for the virtual table. All the
189029** work is done in function fts3InitVtab().
189030*/
189031static int fts3ConnectMethod(
189032 sqlite3 *db, /* Database connection */
189033 void *pAux, /* Pointer to tokenizer hash table */
189034 int argc, /* Number of elements in argv array */
189035 const char * const *argv, /* xCreate/xConnect argument array */
189036 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
189037 char **pzErr /* OUT: sqlite3_malloc'd error message */
189038){
189039 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
189040}
189041static int fts3CreateMethod(
189042 sqlite3 *db, /* Database connection */
189043 void *pAux, /* Pointer to tokenizer hash table */
189044 int argc, /* Number of elements in argv array */
189045 const char * const *argv, /* xCreate/xConnect argument array */
189046 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
189047 char **pzErr /* OUT: sqlite3_malloc'd error message */
189048){
189049 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
189050}
189051
189052/*
189053** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
189054** extension is currently being used by a version of SQLite too old to
189055** support estimatedRows. In that case this function is a no-op.
189056*/
189057static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
189058#if SQLITE_VERSION_NUMBER3047001>=3008002
189059 if( sqlite3_libversion_number()>=3008002 ){
189060 pIdxInfo->estimatedRows = nRow;
189061 }
189062#endif
189063}
189064
189065/*
189066** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
189067** extension is currently being used by a version of SQLite too old to
189068** support index-info flags. In that case this function is a no-op.
189069*/
189070static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
189071#if SQLITE_VERSION_NUMBER3047001>=3008012
189072 if( sqlite3_libversion_number()>=3008012 ){
189073 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE0x00000001;
189074 }
189075#endif
189076}
189077
189078/*
189079** Implementation of the xBestIndex method for FTS3 tables. There
189080** are three possible strategies, in order of preference:
189081**
189082** 1. Direct lookup by rowid or docid.
189083** 2. Full-text search using a MATCH operator on a non-docid column.
189084** 3. Linear scan of %_content table.
189085*/
189086static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
189087 Fts3Table *p = (Fts3Table *)pVTab;
189088 int i; /* Iterator variable */
189089 int iCons = -1; /* Index of constraint to use */
189090
189091 int iLangidCons = -1; /* Index of langid=x constraint, if present */
189092 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
189093 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
189094 int iIdx;
189095
189096 if( p->bLock ){
189097 return SQLITE_ERROR1;
189098 }
189099
189100 /* By default use a full table scan. This is an expensive option,
189101 ** so search through the constraints to see if a more efficient
189102 ** strategy is possible.
189103 */
189104 pInfo->idxNum = FTS3_FULLSCAN_SEARCH0;
189105 pInfo->estimatedCost = 5000000;
189106 for(i=0; i<pInfo->nConstraint; i++){
189107 int bDocid; /* True if this constraint is on docid */
189108 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
189109 if( pCons->usable==0 ){
189110 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
189111 /* There exists an unusable MATCH constraint. This means that if
189112 ** the planner does elect to use the results of this call as part
189113 ** of the overall query plan the user will see an "unable to use
189114 ** function MATCH in the requested context" error. To discourage
189115 ** this, return a very high cost here. */
189116 pInfo->idxNum = FTS3_FULLSCAN_SEARCH0;
189117 pInfo->estimatedCost = 1e50;
189118 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
189119 return SQLITE_OK0;
189120 }
189121 continue;
189122 }
189123
189124 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
189125
189126 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
189127 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2 && bDocid ){
189128 pInfo->idxNum = FTS3_DOCID_SEARCH1;
189129 pInfo->estimatedCost = 1.0;
189130 iCons = i;
189131 }
189132
189133 /* A MATCH constraint. Use a full-text search.
189134 **
189135 ** If there is more than one MATCH constraint available, use the first
189136 ** one encountered. If there is both a MATCH constraint and a direct
189137 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
189138 ** though the rowid/docid lookup is faster than a MATCH query, selecting
189139 ** it would lead to an "unable to use function MATCH in the requested
189140 ** context" error.
189141 */
189142 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64
189143 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
189144 ){
189145 pInfo->idxNum = FTS3_FULLTEXT_SEARCH2 + pCons->iColumn;
189146 pInfo->estimatedCost = 2.0;
189147 iCons = i;
189148 }
189149
189150 /* Equality constraint on the langid column */
189151 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2
189152 && pCons->iColumn==p->nColumn + 2
189153 ){
189154 iLangidCons = i;
189155 }
189156
189157 if( bDocid ){
189158 switch( pCons->op ){
189159 case SQLITE_INDEX_CONSTRAINT_GE32:
189160 case SQLITE_INDEX_CONSTRAINT_GT4:
189161 iDocidGe = i;
189162 break;
189163
189164 case SQLITE_INDEX_CONSTRAINT_LE8:
189165 case SQLITE_INDEX_CONSTRAINT_LT16:
189166 iDocidLe = i;
189167 break;
189168 }
189169 }
189170 }
189171
189172 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
189173 if( pInfo->idxNum==FTS3_DOCID_SEARCH1 ) fts3SetUniqueFlag(pInfo);
189174
189175 iIdx = 1;
189176 if( iCons>=0 ){
189177 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
189178 pInfo->aConstraintUsage[iCons].omit = 1;
189179 }
189180 if( iLangidCons>=0 ){
189181 pInfo->idxNum |= FTS3_HAVE_LANGID0x00010000;
189182 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
189183 }
189184 if( iDocidGe>=0 ){
189185 pInfo->idxNum |= FTS3_HAVE_DOCID_GE0x00020000;
189186 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
189187 }
189188 if( iDocidLe>=0 ){
189189 pInfo->idxNum |= FTS3_HAVE_DOCID_LE0x00040000;
189190 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
189191 }
189192
189193 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
189194 ** docid) order. Both ascending and descending are possible.
189195 */
189196 if( pInfo->nOrderBy==1 ){
189197 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
189198 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
189199 if( pOrder->desc ){
189200 pInfo->idxStr = "DESC";
189201 }else{
189202 pInfo->idxStr = "ASC";
189203 }
189204 pInfo->orderByConsumed = 1;
189205 }
189206 }
189207
189208 assert( p->pSegments==0 )((void) (0));
189209 return SQLITE_OK0;
189210}
189211
189212/*
189213** Implementation of xOpen method.
189214*/
189215static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
189216 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
189217
189218 UNUSED_PARAMETER(pVTab)(void)(pVTab);
189219
189220 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
189221 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
189222 ** if the allocation fails, return SQLITE_NOMEM.
189223 */
189224 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
189225 if( !pCsr ){
189226 return SQLITE_NOMEM7;
189227 }
189228 memset(pCsr, 0, sizeof(Fts3Cursor));
189229 return SQLITE_OK0;
189230}
189231
189232/*
189233** Finalize the statement handle at pCsr->pStmt.
189234**
189235** Or, if that statement handle is one created by fts3CursorSeekStmt(),
189236** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
189237** pointer there instead of finalizing it.
189238*/
189239static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
189240 if( pCsr->bSeekStmt ){
189241 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
189242 if( p->pSeekStmt==0 ){
189243 p->pSeekStmt = pCsr->pStmt;
189244 sqlite3_reset(pCsr->pStmt);
189245 pCsr->pStmt = 0;
189246 }
189247 pCsr->bSeekStmt = 0;
189248 }
189249 sqlite3_finalize(pCsr->pStmt);
189250}
189251
189252/*
189253** Free all resources currently held by the cursor passed as the only
189254** argument.
189255*/
189256static void fts3ClearCursor(Fts3Cursor *pCsr){
189257 fts3CursorFinalizeStmt(pCsr);
189258 sqlite3Fts3FreeDeferredTokens(pCsr);
189259 sqlite3_free(pCsr->aDoclist);
189260 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
189261 sqlite3Fts3ExprFree(pCsr->pExpr);
189262 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
189263}
189264
189265/*
189266** Close the cursor. For additional information see the documentation
189267** on the xClose method of the virtual table interface.
189268*/
189269static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
189270 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
189271 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
189272 fts3ClearCursor(pCsr);
189273 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
189274 sqlite3_free(pCsr);
189275 return SQLITE_OK0;
189276}
189277
189278/*
189279** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
189280** compose and prepare an SQL statement of the form:
189281**
189282** "SELECT <columns> FROM %_content WHERE rowid = ?"
189283**
189284** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
189285** it. If an error occurs, return an SQLite error code.
189286*/
189287static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
189288 int rc = SQLITE_OK0;
189289 if( pCsr->pStmt==0 ){
189290 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
189291 char *zSql;
189292 if( p->pSeekStmt ){
189293 pCsr->pStmt = p->pSeekStmt;
189294 p->pSeekStmt = 0;
189295 }else{
189296 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
189297 if( !zSql ) return SQLITE_NOMEM7;
189298 p->bLock++;
189299 rc = sqlite3_prepare_v3(
189300 p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0
189301 );
189302 p->bLock--;
189303 sqlite3_free(zSql);
189304 }
189305 if( rc==SQLITE_OK0 ) pCsr->bSeekStmt = 1;
189306 }
189307 return rc;
189308}
189309
189310/*
189311** Position the pCsr->pStmt statement so that it is on the row
189312** of the %_content table that contains the last match. Return
189313** SQLITE_OK on success.
189314*/
189315static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
189316 int rc = SQLITE_OK0;
189317 if( pCsr->isRequireSeek ){
189318 rc = fts3CursorSeekStmt(pCsr);
189319 if( rc==SQLITE_OK0 ){
189320 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
189321 pTab->bLock++;
189322 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
189323 pCsr->isRequireSeek = 0;
189324 if( SQLITE_ROW100==sqlite3_step(pCsr->pStmt) ){
189325 pTab->bLock--;
189326 return SQLITE_OK0;
189327 }else{
189328 pTab->bLock--;
189329 rc = sqlite3_reset(pCsr->pStmt);
189330 if( rc==SQLITE_OK0 && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
189331 /* If no row was found and no error has occurred, then the %_content
189332 ** table is missing a row that is present in the full-text index.
189333 ** The data structures are corrupt. */
189334 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
189335 pCsr->isEof = 1;
189336 }
189337 }
189338 }
189339 }
189340
189341 if( rc!=SQLITE_OK0 && pContext ){
189342 sqlite3_result_error_code(pContext, rc);
189343 }
189344 return rc;
189345}
189346
189347/*
189348** This function is used to process a single interior node when searching
189349** a b-tree for a term or term prefix. The node data is passed to this
189350** function via the zNode/nNode parameters. The term to search for is
189351** passed in zTerm/nTerm.
189352**
189353** If piFirst is not NULL, then this function sets *piFirst to the blockid
189354** of the child node that heads the sub-tree that may contain the term.
189355**
189356** If piLast is not NULL, then *piLast is set to the right-most child node
189357** that heads a sub-tree that may contain a term for which zTerm/nTerm is
189358** a prefix.
189359**
189360** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
189361*/
189362static int fts3ScanInteriorNode(
189363 const char *zTerm, /* Term to select leaves for */
189364 int nTerm, /* Size of term zTerm in bytes */
189365 const char *zNode, /* Buffer containing segment interior node */
189366 int nNode, /* Size of buffer at zNode */
189367 sqlite3_int64 *piFirst, /* OUT: Selected child node */
189368 sqlite3_int64 *piLast /* OUT: Selected child node */
189369){
189370 int rc = SQLITE_OK0; /* Return code */
189371 const char *zCsr = zNode; /* Cursor to iterate through node */
189372 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
189373 char *zBuffer = 0; /* Buffer to load terms into */
189374 i64 nAlloc = 0; /* Size of allocated buffer */
189375 int isFirstTerm = 1; /* True when processing first term on page */
189376 u64 iChild; /* Block id of child node to descend to */
189377 int nBuffer = 0; /* Total term size */
189378
189379 /* Skip over the 'height' varint that occurs at the start of every
189380 ** interior node. Then load the blockid of the left-child of the b-tree
189381 ** node into variable iChild.
189382 **
189383 ** Even if the data structure on disk is corrupted, this (reading two
189384 ** varints from the buffer) does not risk an overread. If zNode is a
189385 ** root node, then the buffer comes from a SELECT statement. SQLite does
189386 ** not make this guarantee explicitly, but in practice there are always
189387 ** either more than 20 bytes of allocated space following the nNode bytes of
189388 ** contents, or two zero bytes. Or, if the node is read from the %_segments
189389 ** table, then there are always 20 bytes of zeroed padding following the
189390 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
189391 */
189392 zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
189393 zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
189394 if( zCsr>zEnd ){
189395 return FTS_CORRUPT_VTAB(11 | (1<<8));
189396 }
189397
189398 while( zCsr<zEnd && (piFirst || piLast) ){
189399 int cmp; /* memcmp() result */
189400 int nSuffix; /* Size of term suffix */
189401 int nPrefix = 0; /* Size of term prefix */
189402
189403 /* Load the next term on the node into zBuffer. Use realloc() to expand
189404 ** the size of zBuffer if required. */
189405 if( !isFirstTerm ){
189406 zCsr += fts3GetVarint32(zCsr, &nPrefix)( (*(u8*)(zCsr)&0x80) ? sqlite3Fts3GetVarint32(zCsr, &
nPrefix) : (*&nPrefix=*(u8*)(zCsr), 1) )
;
189407 if( nPrefix>nBuffer ){
189408 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
189409 goto finish_scan;
189410 }
189411 }
189412 isFirstTerm = 0;
189413 zCsr += fts3GetVarint32(zCsr, &nSuffix)( (*(u8*)(zCsr)&0x80) ? sqlite3Fts3GetVarint32(zCsr, &
nSuffix) : (*&nSuffix=*(u8*)(zCsr), 1) )
;
189414
189415 assert( nPrefix>=0 && nSuffix>=0 )((void) (0));
189416 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
189417 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
189418 goto finish_scan;
189419 }
189420 if( (i64)nPrefix+nSuffix>nAlloc ){
189421 char *zNew;
189422 nAlloc = ((i64)nPrefix+nSuffix) * 2;
189423 zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
189424 if( !zNew ){
189425 rc = SQLITE_NOMEM7;
189426 goto finish_scan;
189427 }
189428 zBuffer = zNew;
189429 }
189430 assert( zBuffer )((void) (0));
189431 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
189432 nBuffer = nPrefix + nSuffix;
189433 zCsr += nSuffix;
189434
189435 /* Compare the term we are searching for with the term just loaded from
189436 ** the interior node. If the specified term is greater than or equal
189437 ** to the term from the interior node, then all terms on the sub-tree
189438 ** headed by node iChild are smaller than zTerm. No need to search
189439 ** iChild.
189440 **
189441 ** If the interior node term is larger than the specified term, then
189442 ** the tree headed by iChild may contain the specified term.
189443 */
189444 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
189445 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
189446 *piFirst = (i64)iChild;
189447 piFirst = 0;
189448 }
189449
189450 if( piLast && cmp<0 ){
189451 *piLast = (i64)iChild;
189452 piLast = 0;
189453 }
189454
189455 iChild++;
189456 };
189457
189458 if( piFirst ) *piFirst = (i64)iChild;
189459 if( piLast ) *piLast = (i64)iChild;
189460
189461 finish_scan:
189462 sqlite3_free(zBuffer);
189463 return rc;
189464}
189465
189466
189467/*
189468** The buffer pointed to by argument zNode (size nNode bytes) contains an
189469** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
189470** contains a term. This function searches the sub-tree headed by the zNode
189471** node for the range of leaf nodes that may contain the specified term
189472** or terms for which the specified term is a prefix.
189473**
189474** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
189475** left-most leaf node in the tree that may contain the specified term.
189476** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
189477** right-most leaf node that may contain a term for which the specified
189478** term is a prefix.
189479**
189480** It is possible that the range of returned leaf nodes does not contain
189481** the specified term or any terms for which it is a prefix. However, if the
189482** segment does contain any such terms, they are stored within the identified
189483** range. Because this function only inspects interior segment nodes (and
189484** never loads leaf nodes into memory), it is not possible to be sure.
189485**
189486** If an error occurs, an error code other than SQLITE_OK is returned.
189487*/
189488static int fts3SelectLeaf(
189489 Fts3Table *p, /* Virtual table handle */
189490 const char *zTerm, /* Term to select leaves for */
189491 int nTerm, /* Size of term zTerm in bytes */
189492 const char *zNode, /* Buffer containing segment interior node */
189493 int nNode, /* Size of buffer at zNode */
189494 sqlite3_int64 *piLeaf, /* Selected leaf node */
189495 sqlite3_int64 *piLeaf2 /* Selected leaf node */
189496){
189497 int rc = SQLITE_OK0; /* Return code */
189498 int iHeight; /* Height of this node in tree */
189499
189500 assert( piLeaf || piLeaf2 )((void) (0));
189501
189502 fts3GetVarint32(zNode, &iHeight)( (*(u8*)(zNode)&0x80) ? sqlite3Fts3GetVarint32(zNode, &
iHeight) : (*&iHeight=*(u8*)(zNode), 1) )
;
189503 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
189504 assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) )((void) (0));
189505
189506 if( rc==SQLITE_OK0 && iHeight>1 ){
189507 char *zBlob = 0; /* Blob read from %_segments table */
189508 int nBlob = 0; /* Size of zBlob in bytes */
189509
189510 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
189511 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
189512 if( rc==SQLITE_OK0 ){
189513 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
189514 }
189515 sqlite3_free(zBlob);
189516 piLeaf = 0;
189517 zBlob = 0;
189518 }
189519
189520 if( rc==SQLITE_OK0 ){
189521 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
189522 }
189523 if( rc==SQLITE_OK0 ){
189524 int iNewHeight = 0;
189525 fts3GetVarint32(zBlob, &iNewHeight)( (*(u8*)(zBlob)&0x80) ? sqlite3Fts3GetVarint32(zBlob, &
iNewHeight) : (*&iNewHeight=*(u8*)(zBlob), 1) )
;
189526 if( iNewHeight>=iHeight ){
189527 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
189528 }else{
189529 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
189530 }
189531 }
189532 sqlite3_free(zBlob);
189533 }
189534
189535 return rc;
189536}
189537
189538/*
189539** This function is used to create delta-encoded serialized lists of FTS3
189540** varints. Each call to this function appends a single varint to a list.
189541*/
189542static void fts3PutDeltaVarint(
189543 char **pp, /* IN/OUT: Output pointer */
189544 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
189545 sqlite3_int64 iVal /* Write this value to the list */
189546){
189547 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) )((void) (0));
189548 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
189549 *piPrev = iVal;
189550}
189551
189552/*
189553** When this function is called, *ppPoslist is assumed to point to the
189554** start of a position-list. After it returns, *ppPoslist points to the
189555** first byte after the position-list.
189556**
189557** A position list is list of positions (delta encoded) and columns for
189558** a single document record of a doclist. So, in other words, this
189559** routine advances *ppPoslist so that it points to the next docid in
189560** the doclist, or to the first byte past the end of the doclist.
189561**
189562** If pp is not NULL, then the contents of the position list are copied
189563** to *pp. *pp is set to point to the first byte past the last byte copied
189564** before this function returns.
189565*/
189566static void fts3PoslistCopy(char **pp, char **ppPoslist){
189567 char *pEnd = *ppPoslist;
189568 char c = 0;
189569
189570 /* The end of a position list is marked by a zero encoded as an FTS3
189571 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
189572 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
189573 ** of some other, multi-byte, value.
189574 **
189575 ** The following while-loop moves pEnd to point to the first byte that is not
189576 ** immediately preceded by a byte with the 0x80 bit set. Then increments
189577 ** pEnd once more so that it points to the byte immediately following the
189578 ** last byte in the position-list.
189579 */
189580 while( *pEnd | c ){
189581 c = *pEnd++ & 0x80;
189582 testcase( c!=0 && (*pEnd)==0 );
189583 }
189584 pEnd++; /* Advance past the POS_END terminator byte */
189585
189586 if( pp ){
189587 int n = (int)(pEnd - *ppPoslist);
189588 char *p = *pp;
189589 memcpy(p, *ppPoslist, n);
189590 p += n;
189591 *pp = p;
189592 }
189593 *ppPoslist = pEnd;
189594}
189595
189596/*
189597** When this function is called, *ppPoslist is assumed to point to the
189598** start of a column-list. After it returns, *ppPoslist points to the
189599** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
189600**
189601** A column-list is list of delta-encoded positions for a single column
189602** within a single document within a doclist.
189603**
189604** The column-list is terminated either by a POS_COLUMN varint (1) or
189605** a POS_END varint (0). This routine leaves *ppPoslist pointing to
189606** the POS_COLUMN or POS_END that terminates the column-list.
189607**
189608** If pp is not NULL, then the contents of the column-list are copied
189609** to *pp. *pp is set to point to the first byte past the last byte copied
189610** before this function returns. The POS_COLUMN or POS_END terminator
189611** is not copied into *pp.
189612*/
189613static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
189614 char *pEnd = *ppPoslist;
189615 char c = 0;
189616
189617 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
189618 ** not part of a multi-byte varint.
189619 */
189620 while( 0xFE & (*pEnd | c) ){
189621 c = *pEnd++ & 0x80;
189622 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
189623 }
189624 if( pp ){
189625 int n = (int)(pEnd - *ppPoslist);
189626 char *p = *pp;
189627 memcpy(p, *ppPoslist, n);
189628 p += n;
189629 *pp = p;
189630 }
189631 *ppPoslist = pEnd;
189632}
189633
189634/*
189635** Value used to signify the end of an position-list. This must be
189636** as large or larger than any value that might appear on the
189637** position-list, even a position list that has been corrupted.
189638*/
189639#define POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))
189640
189641/*
189642** This function is used to help parse position-lists. When this function is
189643** called, *pp may point to the start of the next varint in the position-list
189644** being parsed, or it may point to 1 byte past the end of the position-list
189645** (in which case **pp will be a terminator bytes POS_END (0) or
189646** (1)).
189647**
189648** If *pp points past the end of the current position-list, set *pi to
189649** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
189650** increment the current value of *pi by the value read, and set *pp to
189651** point to the next value before returning.
189652**
189653** Before calling this routine *pi must be initialized to the value of
189654** the previous position, or zero if we are reading the first position
189655** in the position-list. Because positions are delta-encoded, the value
189656** of the previous position is needed in order to compute the value of
189657** the next position.
189658*/
189659static void fts3ReadNextPos(
189660 char **pp, /* IN/OUT: Pointer into position-list buffer */
189661 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
189662){
189663 if( (**pp)&0xFE ){
189664 int iVal;
189665 *pp += fts3GetVarint32((*pp), &iVal)( (*(u8*)((*pp))&0x80) ? sqlite3Fts3GetVarint32((*pp), &
iVal) : (*&iVal=*(u8*)((*pp)), 1) )
;
189666 *pi += iVal;
189667 *pi -= 2;
189668 }else{
189669 *pi = POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32));
189670 }
189671}
189672
189673/*
189674** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
189675** the value of iCol encoded as a varint to *pp. This will start a new
189676** column list.
189677**
189678** Set *pp to point to the byte just after the last byte written before
189679** returning (do not modify it if iCol==0). Return the total number of bytes
189680** written (0 if iCol==0).
189681*/
189682static int fts3PutColNumber(char **pp, int iCol){
189683 int n = 0; /* Number of bytes written */
189684 if( iCol ){
189685 char *p = *pp; /* Output pointer */
189686 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
189687 *p = 0x01;
189688 *pp = &p[n];
189689 }
189690 return n;
189691}
189692
189693/*
189694** Compute the union of two position lists. The output written
189695** into *pp contains all positions of both *pp1 and *pp2 in sorted
189696** order and with any duplicates removed. All pointers are
189697** updated appropriately. The caller is responsible for insuring
189698** that there is enough space in *pp to hold the complete output.
189699*/
189700static int fts3PoslistMerge(
189701 char **pp, /* Output buffer */
189702 char **pp1, /* Left input list */
189703 char **pp2 /* Right input list */
189704){
189705 char *p = *pp;
189706 char *p1 = *pp1;
189707 char *p2 = *pp2;
189708
189709 while( *p1 || *p2 ){
189710 int iCol1; /* The current column index in pp1 */
189711 int iCol2; /* The current column index in pp2 */
189712
189713 if( *p1==POS_COLUMN(1) ){
189714 fts3GetVarint32(&p1[1], &iCol1)( (*(u8*)(&p1[1])&0x80) ? sqlite3Fts3GetVarint32(&
p1[1], &iCol1) : (*&iCol1=*(u8*)(&p1[1]), 1) )
;
189715 if( iCol1==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189716 }
189717 else if( *p1==POS_END(0) ) iCol1 = 0x7fffffff;
189718 else iCol1 = 0;
189719
189720 if( *p2==POS_COLUMN(1) ){
189721 fts3GetVarint32(&p2[1], &iCol2)( (*(u8*)(&p2[1])&0x80) ? sqlite3Fts3GetVarint32(&
p2[1], &iCol2) : (*&iCol2=*(u8*)(&p2[1]), 1) )
;
189722 if( iCol2==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
189723 }
189724 else if( *p2==POS_END(0) ) iCol2 = 0x7fffffff;
189725 else iCol2 = 0;
189726
189727 if( iCol1==iCol2 ){
189728 sqlite3_int64 i1 = 0; /* Last position from pp1 */
189729 sqlite3_int64 i2 = 0; /* Last position from pp2 */
189730 sqlite3_int64 iPrev = 0;
189731 int n = fts3PutColNumber(&p, iCol1);
189732 p1 += n;
189733 p2 += n;
189734
189735 /* At this point, both p1 and p2 point to the start of column-lists
189736 ** for the same column (the column with index iCol1 and iCol2).
189737 ** A column-list is a list of non-negative delta-encoded varints, each
189738 ** incremented by 2 before being stored. Each list is terminated by a
189739 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
189740 ** and writes the results to buffer p. p is left pointing to the byte
189741 ** after the list written. No terminator (POS_END or POS_COLUMN) is
189742 ** written to the output.
189743 */
189744 fts3GetDeltaVarint(&p1, &i1);
189745 fts3GetDeltaVarint(&p2, &i2);
189746 if( i1<2 || i2<2 ){
189747 break;
189748 }
189749 do {
189750 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
189751 iPrev -= 2;
189752 if( i1==i2 ){
189753 fts3ReadNextPos(&p1, &i1);
189754 fts3ReadNextPos(&p2, &i2);
189755 }else if( i1<i2 ){
189756 fts3ReadNextPos(&p1, &i1);
189757 }else{
189758 fts3ReadNextPos(&p2, &i2);
189759 }
189760 }while( i1!=POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) || i2!=POSITION_LIST_END(0xffffffff|(((i64)0x7fffffff)<<32)) );
189761 }else if( iCol1<iCol2 ){
189762 p1 += fts3PutColNumber(&p, iCol1);
189763 fts3ColumnlistCopy(&p, &p1);
189764 }else{
189765 p2 += fts3PutColNumber(&p, iCol2);
189766 fts3ColumnlistCopy(&p, &p2);
189767 }
189768 }
189769
189770 *p++ = POS_END(0);
189771 *pp = p;
189772 *pp1 = p1 + 1;
189773 *pp2 = p2 + 1;
189774 return SQLITE_OK0;
189775}
189776
189777/*
189778** This function is used to merge two position lists into one. When it is
189779** called, *pp1 and *pp2 must both point to position lists. A position-list is
189780** the part of a doclist that follows each document id. For example, if a row
189781** contains:
189782**
189783** 'a b c'|'x y z'|'a b b a'
189784**
189785** Then the position list for this row for token 'b' would consist of:
189786**
189787** 0x02 0x01 0x02 0x03 0x03 0x00
189788**
189789** When this function returns, both *pp1 and *pp2 are left pointing to the
189790** byte following the 0x00 terminator of their respective position lists.
189791**
189792** If isSaveLeft is 0, an entry is added to the output position list for
189793** each position in *pp2 for which there exists one or more positions in
189794** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
189795** when the *pp1 token appears before the *pp2 token, but not more than nToken
189796** slots before it.
189797**
189798** e.g. nToken==1 searches for adjacent positions.
189799*/
189800static int fts3PoslistPhraseMerge(
189801 char **pp, /* IN/OUT: Preallocated output buffer */
189802 int nToken, /* Maximum difference in token positions */
189803 int isSaveLeft, /* Save the left position */
189804 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
189805 char **pp1, /* IN/OUT: Left input list */
189806 char **pp2 /* IN/OUT: Right input list */
189807){
189808 char *p = *pp;
189809 char *p1 = *pp1;
189810 char *p2 = *pp2;
189811 int iCol1 = 0;
189812 int iCol2 = 0;
189813
189814 /* Never set both isSaveLeft and isExact for the same invocation. */
189815 assert( isSaveLeft==0 || isExact==0 )((void) (0));
189816
189817 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 )((void) (0));
189818 if( *p1==POS_COLUMN(1) ){
189819 p1++;
189820 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
189821 /* iCol1==0 indicates corruption. Column 0 does not have a POS_COLUMN
189822 ** entry, so this is actually end-of-doclist. */
189823 if( iCol1==0 ) return 0;
189824 }
189825 if( *p2==POS_COLUMN(1) ){
189826 p2++;
189827 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
189828 /* As above, iCol2==0 indicates corruption. */
189829 if( iCol2==0 ) return 0;
189830 }
189831
189832 while( 1 ){
189833 if( iCol1==iCol2 ){
189834 char *pSave = p;
189835 sqlite3_int64 iPrev = 0;
189836 sqlite3_int64 iPos1 = 0;
189837 sqlite3_int64 iPos2 = 0;
189838
189839 if( iCol1 ){
189840 *p++ = POS_COLUMN(1);
189841 p += sqlite3Fts3PutVarint(p, iCol1);
189842 }
189843
189844 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
189845 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
189846 if( iPos1<0 || iPos2<0 ) break;
189847
189848 while( 1 ){
189849 if( iPos2==iPos1+nToken
189850 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
189851 ){
189852 sqlite3_int64 iSave;
189853 iSave = isSaveLeft ? iPos1 : iPos2;
189854 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
189855 pSave = 0;
189856 assert( p )((void) (0));
189857 }
189858 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
189859 if( (*p2&0xFE)==0 ) break;
189860 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
189861 }else{
189862 if( (*p1&0xFE)==0 ) break;
189863 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
189864 }
189865 }
189866
189867 if( pSave ){
189868 assert( pp && p )((void) (0));
189869 p = pSave;
189870 }
189871
189872 fts3ColumnlistCopy(0, &p1);
189873 fts3ColumnlistCopy(0, &p2);
189874 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 )((void) (0));
189875 if( 0==*p1 || 0==*p2 ) break;
189876
189877 p1++;
189878 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
189879 p2++;
189880 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
189881 }
189882
189883 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
189884 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
189885 ** end of the position list, or the 0x01 that precedes the next
189886 ** column-number in the position list.
189887 */
189888 else if( iCol1<iCol2 ){
189889 fts3ColumnlistCopy(0, &p1);
189890 if( 0==*p1 ) break;
189891 p1++;
189892 p1 += fts3GetVarint32(p1, &iCol1)( (*(u8*)(p1)&0x80) ? sqlite3Fts3GetVarint32(p1, &iCol1
) : (*&iCol1=*(u8*)(p1), 1) )
;
189893 }else{
189894 fts3ColumnlistCopy(0, &p2);
189895 if( 0==*p2 ) break;
189896 p2++;
189897 p2 += fts3GetVarint32(p2, &iCol2)( (*(u8*)(p2)&0x80) ? sqlite3Fts3GetVarint32(p2, &iCol2
) : (*&iCol2=*(u8*)(p2), 1) )
;
189898 }
189899 }
189900
189901 fts3PoslistCopy(0, &p2);
189902 fts3PoslistCopy(0, &p1);
189903 *pp1 = p1;
189904 *pp2 = p2;
189905 if( *pp==p ){
189906 return 0;
189907 }
189908 *p++ = 0x00;
189909 *pp = p;
189910 return 1;
189911}
189912
189913/*
189914** Merge two position-lists as required by the NEAR operator. The argument
189915** position lists correspond to the left and right phrases of an expression
189916** like:
189917**
189918** "phrase 1" NEAR "phrase number 2"
189919**
189920** Position list *pp1 corresponds to the left-hand side of the NEAR
189921** expression and *pp2 to the right. As usual, the indexes in the position
189922** lists are the offsets of the last token in each phrase (tokens "1" and "2"
189923** in the example above).
189924**
189925** The output position list - written to *pp - is a copy of *pp2 with those
189926** entries that are not sufficiently NEAR entries in *pp1 removed.
189927*/
189928static int fts3PoslistNearMerge(
189929 char **pp, /* Output buffer */
189930 char *aTmp, /* Temporary buffer space */
189931 int nRight, /* Maximum difference in token positions */
189932 int nLeft, /* Maximum difference in token positions */
189933 char **pp1, /* IN/OUT: Left input list */
189934 char **pp2 /* IN/OUT: Right input list */
189935){
189936 char *p1 = *pp1;
189937 char *p2 = *pp2;
189938
189939 char *pTmp1 = aTmp;
189940 char *pTmp2;
189941 char *aTmp2;
189942 int res = 1;
189943
189944 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
189945 aTmp2 = pTmp2 = pTmp1;
189946 *pp1 = p1;
189947 *pp2 = p2;
189948 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
189949 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
189950 fts3PoslistMerge(pp, &aTmp, &aTmp2);
189951 }else if( pTmp1!=aTmp ){
189952 fts3PoslistCopy(pp, &aTmp);
189953 }else if( pTmp2!=aTmp2 ){
189954 fts3PoslistCopy(pp, &aTmp2);
189955 }else{
189956 res = 0;
189957 }
189958
189959 return res;
189960}
189961
189962/*
189963** An instance of this function is used to merge together the (potentially
189964** large number of) doclists for each term that matches a prefix query.
189965** See function fts3TermSelectMerge() for details.
189966*/
189967typedef struct TermSelect TermSelect;
189968struct TermSelect {
189969 char *aaOutput[16]; /* Malloc'd output buffers */
189970 int anOutput[16]; /* Size each output buffer in bytes */
189971};
189972
189973/*
189974** This function is used to read a single varint from a buffer. Parameter
189975** pEnd points 1 byte past the end of the buffer. When this function is
189976** called, if *pp points to pEnd or greater, then the end of the buffer
189977** has been reached. In this case *pp is set to 0 and the function returns.
189978**
189979** If *pp does not point to or past pEnd, then a single varint is read
189980** from *pp. *pp is then set to point 1 byte past the end of the read varint.
189981**
189982** If bDescIdx is false, the value read is added to *pVal before returning.
189983** If it is true, the value read is subtracted from *pVal before this
189984** function returns.
189985*/
189986static void fts3GetDeltaVarint3(
189987 char **pp, /* IN/OUT: Point to read varint from */
189988 char *pEnd, /* End of buffer */
189989 int bDescIdx, /* True if docids are descending */
189990 sqlite3_int64 *pVal /* IN/OUT: Integer value */
189991){
189992 if( *pp>=pEnd ){
189993 *pp = 0;
189994 }else{
189995 u64 iVal;
189996 *pp += sqlite3Fts3GetVarintU(*pp, &iVal);
189997 if( bDescIdx ){
189998 *pVal = (i64)((u64)*pVal - iVal);
189999 }else{
190000 *pVal = (i64)((u64)*pVal + iVal);
190001 }
190002 }
190003}
190004
190005/*
190006** This function is used to write a single varint to a buffer. The varint
190007** is written to *pp. Before returning, *pp is set to point 1 byte past the
190008** end of the value written.
190009**
190010** If *pbFirst is zero when this function is called, the value written to
190011** the buffer is that of parameter iVal.
190012**
190013** If *pbFirst is non-zero when this function is called, then the value
190014** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
190015** (if bDescIdx is non-zero).
190016**
190017** Before returning, this function always sets *pbFirst to 1 and *piPrev
190018** to the value of parameter iVal.
190019*/
190020static void fts3PutDeltaVarint3(
190021 char **pp, /* IN/OUT: Output pointer */
190022 int bDescIdx, /* True for descending docids */
190023 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
190024 int *pbFirst, /* IN/OUT: True after first int written */
190025 sqlite3_int64 iVal /* Write this value to the list */
190026){
190027 sqlite3_uint64 iWrite;
190028 if( bDescIdx==0 || *pbFirst==0 ){
190029 assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev )((void) (0));
190030 iWrite = (u64)iVal - (u64)*piPrev;
190031 }else{
190032 assert_fts3_nc( *piPrev>=iVal )((void) (0));
190033 iWrite = (u64)*piPrev - (u64)iVal;
190034 }
190035 assert( *pbFirst || *piPrev==0 )((void) (0));
190036 assert_fts3_nc( *pbFirst==0 || iWrite>0 )((void) (0));
190037 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
190038 *piPrev = iVal;
190039 *pbFirst = 1;
190040}
190041
190042
190043/*
190044** This macro is used by various functions that merge doclists. The two
190045** arguments are 64-bit docid values. If the value of the stack variable
190046** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
190047** Otherwise, (i2-i1).
190048**
190049** Using this makes it easier to write code that can merge doclists that are
190050** sorted in either ascending or descending order.
190051*/
190052/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
190053#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)))
190054
190055/*
190056** This function does an "OR" merge of two doclists (output contains all
190057** positions contained in either argument doclist). If the docids in the
190058** input doclists are sorted in ascending order, parameter bDescDoclist
190059** should be false. If they are sorted in ascending order, it should be
190060** passed a non-zero value.
190061**
190062** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
190063** containing the output doclist and SQLITE_OK is returned. In this case
190064** *pnOut is set to the number of bytes in the output doclist.
190065**
190066** If an error occurs, an SQLite error code is returned. The output values
190067** are undefined in this case.
190068*/
190069static int fts3DoclistOrMerge(
190070 int bDescDoclist, /* True if arguments are desc */
190071 char *a1, int n1, /* First doclist */
190072 char *a2, int n2, /* Second doclist */
190073 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
190074){
190075 int rc = SQLITE_OK0;
190076 sqlite3_int64 i1 = 0;
190077 sqlite3_int64 i2 = 0;
190078 sqlite3_int64 iPrev = 0;
190079 char *pEnd1 = &a1[n1];
190080 char *pEnd2 = &a2[n2];
190081 char *p1 = a1;
190082 char *p2 = a2;
190083 char *p;
190084 char *aOut;
190085 int bFirstOut = 0;
190086
190087 *paOut = 0;
190088 *pnOut = 0;
190089
190090 /* Allocate space for the output. Both the input and output doclists
190091 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
190092 ** then the first docid in each list is simply encoded as a varint. For
190093 ** each subsequent docid, the varint stored is the difference between the
190094 ** current and previous docid (a positive number - since the list is in
190095 ** ascending order).
190096 **
190097 ** The first docid written to the output is therefore encoded using the
190098 ** same number of bytes as it is in whichever of the input lists it is
190099 ** read from. And each subsequent docid read from the same input list
190100 ** consumes either the same or less bytes as it did in the input (since
190101 ** the difference between it and the previous value in the output must
190102 ** be a positive value less than or equal to the delta value read from
190103 ** the input list). The same argument applies to all but the first docid
190104 ** read from the 'other' list. And to the contents of all position lists
190105 ** that will be copied and merged from the input to the output.
190106 **
190107 ** However, if the first docid copied to the output is a negative number,
190108 ** then the encoding of the first docid from the 'other' input list may
190109 ** be larger in the output than it was in the input (since the delta value
190110 ** may be a larger positive integer than the actual docid).
190111 **
190112 ** The space required to store the output is therefore the sum of the
190113 ** sizes of the two inputs, plus enough space for exactly one of the input
190114 ** docids to grow.
190115 **
190116 ** A symetric argument may be made if the doclists are in descending
190117 ** order.
190118 */
190119 aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX10-1+FTS3_BUFFER_PADDING8);
190120 if( !aOut ) return SQLITE_NOMEM7;
190121
190122 p = aOut;
190123 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
190124 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
190125 while( p1 || p2 ){
190126 sqlite3_int64 iDiff = DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)));
190127
190128 if( p2 && p1 && iDiff==0 ){
190129 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
190130 rc = fts3PoslistMerge(&p, &p1, &p2);
190131 if( rc ) break;
190132 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
190133 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
190134 }else if( !p2 || (p1 && iDiff<0) ){
190135 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
190136 fts3PoslistCopy(&p, &p1);
190137 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
190138 }else{
190139 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
190140 fts3PoslistCopy(&p, &p2);
190141 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
190142 }
190143
190144 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) )((void) (0));
190145 }
190146
190147 if( rc!=SQLITE_OK0 ){
190148 sqlite3_free(aOut);
190149 p = aOut = 0;
190150 }else{
190151 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 )((void) (0));
190152 memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING8);
190153 }
190154 *paOut = aOut;
190155 *pnOut = (int)(p-aOut);
190156 return rc;
190157}
190158
190159/*
190160** This function does a "phrase" merge of two doclists. In a phrase merge,
190161** the output contains a copy of each position from the right-hand input
190162** doclist for which there is a position in the left-hand input doclist
190163** exactly nDist tokens before it.
190164**
190165** If the docids in the input doclists are sorted in ascending order,
190166** parameter bDescDoclist should be false. If they are sorted in ascending
190167** order, it should be passed a non-zero value.
190168**
190169** The right-hand input doclist is overwritten by this function.
190170*/
190171static int fts3DoclistPhraseMerge(
190172 int bDescDoclist, /* True if arguments are desc */
190173 int nDist, /* Distance from left to right (1=adjacent) */
190174 char *aLeft, int nLeft, /* Left doclist */
190175 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
190176){
190177 sqlite3_int64 i1 = 0;
190178 sqlite3_int64 i2 = 0;
190179 sqlite3_int64 iPrev = 0;
190180 char *aRight = *paRight;
190181 char *pEnd1 = &aLeft[nLeft];
190182 char *pEnd2 = &aRight[*pnRight];
190183 char *p1 = aLeft;
190184 char *p2 = aRight;
190185 char *p;
190186 int bFirstOut = 0;
190187 char *aOut;
190188
190189 assert( nDist>0 )((void) (0));
190190 if( bDescDoclist ){
190191 aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX10);
190192 if( aOut==0 ) return SQLITE_NOMEM7;
190193 }else{
190194 aOut = aRight;
190195 }
190196 p = aOut;
190197
190198 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
190199 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
190200
190201 while( p1 && p2 ){
190202 sqlite3_int64 iDiff = DOCID_CMP(i1, i2)((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)));
190203 if( iDiff==0 ){
190204 char *pSave = p;
190205 sqlite3_int64 iPrevSave = iPrev;
190206 int bFirstOutSave = bFirstOut;
190207
190208 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
190209 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
190210 p = pSave;
190211 iPrev = iPrevSave;
190212 bFirstOut = bFirstOutSave;
190213 }
190214 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
190215 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
190216 }else if( iDiff<0 ){
190217 fts3PoslistCopy(0, &p1);
190218 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
190219 }else{
190220 fts3PoslistCopy(0, &p2);
190221 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
190222 }
190223 }
190224
190225 *pnRight = (int)(p - aOut);
190226 if( bDescDoclist ){
190227 sqlite3_free(aRight);
190228 *paRight = aOut;
190229 }
190230
190231 return SQLITE_OK0;
190232}
190233
190234/*
190235** Argument pList points to a position list nList bytes in size. This
190236** function checks to see if the position list contains any entries for
190237** a token in position 0 (of any column). If so, it writes argument iDelta
190238** to the output buffer pOut, followed by a position list consisting only
190239** of the entries from pList at position 0, and terminated by an 0x00 byte.
190240** The value returned is the number of bytes written to pOut (if any).
190241*/
190242SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(
190243 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
190244 char *pList, /* Position list (no 0x00 term) */
190245 int nList, /* Size of pList in bytes */
190246 char *pOut /* Write output here */
190247){
190248 int nOut = 0;
190249 int bWritten = 0; /* True once iDelta has been written */
190250 char *p = pList;
190251 char *pEnd = &pList[nList];
190252
190253 if( *p!=0x01 ){
190254 if( *p==0x02 ){
190255 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
190256 pOut[nOut++] = 0x02;
190257 bWritten = 1;
190258 }
190259 fts3ColumnlistCopy(0, &p);
190260 }
190261
190262 while( p<pEnd ){
190263 sqlite3_int64 iCol;
190264 p++;
190265 p += sqlite3Fts3GetVarint(p, &iCol);
190266 if( *p==0x02 ){
190267 if( bWritten==0 ){
190268 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
190269 bWritten = 1;
190270 }
190271 pOut[nOut++] = 0x01;
190272 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
190273 pOut[nOut++] = 0x02;
190274 }
190275 fts3ColumnlistCopy(0, &p);
190276 }
190277 if( bWritten ){
190278 pOut[nOut++] = 0x00;
190279 }
190280
190281 return nOut;
190282}
190283
190284
190285/*
190286** Merge all doclists in the TermSelect.aaOutput[] array into a single
190287** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
190288** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
190289**
190290** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
190291** the responsibility of the caller to free any doclists left in the
190292** TermSelect.aaOutput[] array.
190293*/
190294static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
190295 char *aOut = 0;
190296 int nOut = 0;
190297 int i;
190298
190299 /* Loop through the doclists in the aaOutput[] array. Merge them all
190300 ** into a single doclist.
190301 */
190302 for(i=0; i<SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))); i++){
190303 if( pTS->aaOutput[i] ){
190304 if( !aOut ){
190305 aOut = pTS->aaOutput[i];
190306 nOut = pTS->anOutput[i];
190307 pTS->aaOutput[i] = 0;
190308 }else{
190309 int nNew;
190310 char *aNew;
190311
190312 int rc = fts3DoclistOrMerge(p->bDescIdx,
190313 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
190314 );
190315 if( rc!=SQLITE_OK0 ){
190316 sqlite3_free(aOut);
190317 return rc;
190318 }
190319
190320 sqlite3_free(pTS->aaOutput[i]);
190321 sqlite3_free(aOut);
190322 pTS->aaOutput[i] = 0;
190323 aOut = aNew;
190324 nOut = nNew;
190325 }
190326 }
190327 }
190328
190329 pTS->aaOutput[0] = aOut;
190330 pTS->anOutput[0] = nOut;
190331 return SQLITE_OK0;
190332}
190333
190334/*
190335** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
190336** as the first argument. The merge is an "OR" merge (see function
190337** fts3DoclistOrMerge() for details).
190338**
190339** This function is called with the doclist for each term that matches
190340** a queried prefix. It merges all these doclists into one, the doclist
190341** for the specified prefix. Since there can be a very large number of
190342** doclists to merge, the merging is done pair-wise using the TermSelect
190343** object.
190344**
190345** This function returns SQLITE_OK if the merge is successful, or an
190346** SQLite error code (SQLITE_NOMEM) if an error occurs.
190347*/
190348static int fts3TermSelectMerge(
190349 Fts3Table *p, /* FTS table handle */
190350 TermSelect *pTS, /* TermSelect object to merge into */
190351 char *aDoclist, /* Pointer to doclist */
190352 int nDoclist /* Size of aDoclist in bytes */
190353){
190354 if( pTS->aaOutput[0]==0 ){
190355 /* If this is the first term selected, copy the doclist to the output
190356 ** buffer using memcpy().
190357 **
190358 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
190359 ** allocation. This is so as to ensure that the buffer is big enough
190360 ** to hold the current doclist AND'd with any other doclist. If the
190361 ** doclists are stored in order=ASC order, this padding would not be
190362 ** required (since the size of [doclistA AND doclistB] is always less
190363 ** than or equal to the size of [doclistA] in that case). But this is
190364 ** not true for order=DESC. For example, a doclist containing (1, -1)
190365 ** may be smaller than (-1), as in the first example the -1 may be stored
190366 ** as a single-byte delta, whereas in the second it must be stored as a
190367 ** FTS3_VARINT_MAX byte varint.
190368 **
190369 ** Similar padding is added in the fts3DoclistOrMerge() function.
190370 */
190371 pTS->aaOutput[0] = sqlite3_malloc64((i64)nDoclist + FTS3_VARINT_MAX10 + 1);
190372 pTS->anOutput[0] = nDoclist;
190373 if( pTS->aaOutput[0] ){
190374 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
190375 memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX10);
190376 }else{
190377 return SQLITE_NOMEM7;
190378 }
190379 }else{
190380 char *aMerge = aDoclist;
190381 int nMerge = nDoclist;
190382 int iOut;
190383
190384 for(iOut=0; iOut<SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))); iOut++){
190385 if( pTS->aaOutput[iOut]==0 ){
190386 assert( iOut>0 )((void) (0));
190387 pTS->aaOutput[iOut] = aMerge;
190388 pTS->anOutput[iOut] = nMerge;
190389 break;
190390 }else{
190391 char *aNew;
190392 int nNew;
190393
190394 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
190395 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
190396 );
190397 if( rc!=SQLITE_OK0 ){
190398 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
190399 return rc;
190400 }
190401
190402 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
190403 sqlite3_free(pTS->aaOutput[iOut]);
190404 pTS->aaOutput[iOut] = 0;
190405
190406 aMerge = aNew;
190407 nMerge = nNew;
190408 if( (iOut+1)==SizeofArray(pTS->aaOutput)((int)(sizeof(pTS->aaOutput)/sizeof(pTS->aaOutput[0]))) ){
190409 pTS->aaOutput[iOut] = aMerge;
190410 pTS->anOutput[iOut] = nMerge;
190411 }
190412 }
190413 }
190414 }
190415 return SQLITE_OK0;
190416}
190417
190418/*
190419** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
190420*/
190421static int fts3SegReaderCursorAppend(
190422 Fts3MultiSegReader *pCsr,
190423 Fts3SegReader *pNew
190424){
190425 if( (pCsr->nSegment%16)==0 ){
190426 Fts3SegReader **apNew;
190427 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
190428 apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
190429 if( !apNew ){
190430 sqlite3Fts3SegReaderFree(pNew);
190431 return SQLITE_NOMEM7;
190432 }
190433 pCsr->apSegment = apNew;
190434 }
190435 pCsr->apSegment[pCsr->nSegment++] = pNew;
190436 return SQLITE_OK0;
190437}
190438
190439/*
190440** Add seg-reader objects to the Fts3MultiSegReader object passed as the
190441** 8th argument.
190442**
190443** This function returns SQLITE_OK if successful, or an SQLite error code
190444** otherwise.
190445*/
190446static int fts3SegReaderCursor(
190447 Fts3Table *p, /* FTS3 table handle */
190448 int iLangid, /* Language id */
190449 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
190450 int iLevel, /* Level of segments to scan */
190451 const char *zTerm, /* Term to query for */
190452 int nTerm, /* Size of zTerm in bytes */
190453 int isPrefix, /* True for a prefix search */
190454 int isScan, /* True to scan from zTerm to EOF */
190455 Fts3MultiSegReader *pCsr /* Cursor object to populate */
190456){
190457 int rc = SQLITE_OK0; /* Error code */
190458 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
190459 int rc2; /* Result of sqlite3_reset() */
190460
190461 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
190462 ** for the pending-terms. If this is a scan, then this call must be being
190463 ** made by an fts4aux module, not an FTS table. In this case calling
190464 ** Fts3SegReaderPending might segfault, as the data structures used by
190465 ** fts4aux are not completely populated. So it's easiest to filter these
190466 ** calls out here. */
190467 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
190468 Fts3SegReader *pSeg = 0;
190469 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
190470 if( rc==SQLITE_OK0 && pSeg ){
190471 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
190472 }
190473 }
190474
190475 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
190476 if( rc==SQLITE_OK0 ){
190477 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
190478 }
190479
190480 while( rc==SQLITE_OK0 && SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
190481 Fts3SegReader *pSeg = 0;
190482
190483 /* Read the values returned by the SELECT into local variables. */
190484 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
190485 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
190486 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
190487 int nRoot = sqlite3_column_bytes(pStmt, 4);
190488 char const *zRoot = sqlite3_column_blob(pStmt, 4);
190489
190490 /* If zTerm is not NULL, and this segment is not stored entirely on its
190491 ** root node, the range of leaves scanned can be reduced. Do this. */
190492 if( iStartBlock && zTerm && zRoot ){
190493 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
190494 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
190495 if( rc!=SQLITE_OK0 ) goto finished;
190496 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
190497 }
190498
190499 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
190500 (isPrefix==0 && isScan==0),
190501 iStartBlock, iLeavesEndBlock,
190502 iEndBlock, zRoot, nRoot, &pSeg
190503 );
190504 if( rc!=SQLITE_OK0 ) goto finished;
190505 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
190506 }
190507 }
190508
190509 finished:
190510 rc2 = sqlite3_reset(pStmt);
190511 if( rc==SQLITE_DONE101 ) rc = rc2;
190512
190513 return rc;
190514}
190515
190516/*
190517** Set up a cursor object for iterating through a full-text index or a
190518** single level therein.
190519*/
190520SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(
190521 Fts3Table *p, /* FTS3 table handle */
190522 int iLangid, /* Language-id to search */
190523 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
190524 int iLevel, /* Level of segments to scan */
190525 const char *zTerm, /* Term to query for */
190526 int nTerm, /* Size of zTerm in bytes */
190527 int isPrefix, /* True for a prefix search */
190528 int isScan, /* True to scan from zTerm to EOF */
190529 Fts3MultiSegReader *pCsr /* Cursor object to populate */
190530){
190531 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
190532 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
190533 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
190534 || iLevel>=0((void) (0))
190535 )((void) (0));
190536 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
190537 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 )((void) (0));
190538 assert( isPrefix==0 || isScan==0 )((void) (0));
190539
190540 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
190541 return fts3SegReaderCursor(
190542 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
190543 );
190544}
190545
190546/*
190547** In addition to its current configuration, have the Fts3MultiSegReader
190548** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
190549**
190550** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
190551*/
190552static int fts3SegReaderCursorAddZero(
190553 Fts3Table *p, /* FTS virtual table handle */
190554 int iLangid,
190555 const char *zTerm, /* Term to scan doclist of */
190556 int nTerm, /* Number of bytes in zTerm */
190557 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
190558){
190559 return fts3SegReaderCursor(p,
190560 iLangid, 0, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 0, 0,pCsr
190561 );
190562}
190563
190564/*
190565** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
190566** if isPrefix is true, to scan the doclist for all terms for which
190567** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
190568** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
190569** an SQLite error code.
190570**
190571** It is the responsibility of the caller to free this object by eventually
190572** passing it to fts3SegReaderCursorFree()
190573**
190574** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
190575** Output parameter *ppSegcsr is set to 0 if an error occurs.
190576*/
190577static int fts3TermSegReaderCursor(
190578 Fts3Cursor *pCsr, /* Virtual table cursor handle */
190579 const char *zTerm, /* Term to query for */
190580 int nTerm, /* Size of zTerm in bytes */
190581 int isPrefix, /* True for a prefix search */
190582 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
190583){
190584 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
190585 int rc = SQLITE_NOMEM7; /* Return code */
190586
190587 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
190588 if( pSegcsr ){
190589 int i;
190590 int bFound = 0; /* True once an index has been found */
190591 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
190592
190593 if( isPrefix ){
190594 for(i=1; bFound==0 && i<p->nIndex; i++){
190595 if( p->aIndex[i].nPrefix==nTerm ){
190596 bFound = 1;
190597 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
190598 i, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 0, 0, pSegcsr
190599 );
190600 pSegcsr->bLookup = 1;
190601 }
190602 }
190603
190604 for(i=1; bFound==0 && i<p->nIndex; i++){
190605 if( p->aIndex[i].nPrefix==nTerm+1 ){
190606 bFound = 1;
190607 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
190608 i, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, 1, 0, pSegcsr
190609 );
190610 if( rc==SQLITE_OK0 ){
190611 rc = fts3SegReaderCursorAddZero(
190612 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
190613 );
190614 }
190615 }
190616 }
190617 }
190618
190619 if( bFound==0 ){
190620 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
190621 0, FTS3_SEGCURSOR_ALL-2, zTerm, nTerm, isPrefix, 0, pSegcsr
190622 );
190623 pSegcsr->bLookup = !isPrefix;
190624 }
190625 }
190626
190627 *ppSegcsr = pSegcsr;
190628 return rc;
190629}
190630
190631/*
190632** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
190633*/
190634static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
190635 sqlite3Fts3SegReaderFinish(pSegcsr);
190636 sqlite3_free(pSegcsr);
190637}
190638
190639/*
190640** This function retrieves the doclist for the specified term (or term
190641** prefix) from the database.
190642*/
190643static int fts3TermSelect(
190644 Fts3Table *p, /* Virtual table handle */
190645 Fts3PhraseToken *pTok, /* Token to query for */
190646 int iColumn, /* Column to query (or -ve for all columns) */
190647 int *pnOut, /* OUT: Size of buffer at *ppOut */
190648 char **ppOut /* OUT: Malloced result buffer */
190649){
190650 int rc; /* Return code */
190651 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
190652 TermSelect tsc; /* Object for pair-wise doclist merging */
190653 Fts3SegFilter filter; /* Segment term filter configuration */
190654
190655 pSegcsr = pTok->pSegcsr;
190656 memset(&tsc, 0, sizeof(TermSelect));
190657
190658 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY0x00000002 | FTS3_SEGMENT_REQUIRE_POS0x00000001
190659 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX0x00000008 : 0)
190660 | (pTok->bFirst ? FTS3_SEGMENT_FIRST0x00000020 : 0)
190661 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER0x00000004 : 0);
190662 filter.iCol = iColumn;
190663 filter.zTerm = pTok->z;
190664 filter.nTerm = pTok->n;
190665
190666 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
190667 while( SQLITE_OK0==rc
190668 && SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
190669 ){
190670 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
190671 }
190672
190673 if( rc==SQLITE_OK0 ){
190674 rc = fts3TermSelectFinishMerge(p, &tsc);
190675 }
190676 if( rc==SQLITE_OK0 ){
190677 *ppOut = tsc.aaOutput[0];
190678 *pnOut = tsc.anOutput[0];
190679 }else{
190680 int i;
190681 for(i=0; i<SizeofArray(tsc.aaOutput)((int)(sizeof(tsc.aaOutput)/sizeof(tsc.aaOutput[0]))); i++){
190682 sqlite3_free(tsc.aaOutput[i]);
190683 }
190684 }
190685
190686 fts3SegReaderCursorFree(pSegcsr);
190687 pTok->pSegcsr = 0;
190688 return rc;
190689}
190690
190691/*
190692** This function counts the total number of docids in the doclist stored
190693** in buffer aList[], size nList bytes.
190694**
190695** If the isPoslist argument is true, then it is assumed that the doclist
190696** contains a position-list following each docid. Otherwise, it is assumed
190697** that the doclist is simply a list of docids stored as delta encoded
190698** varints.
190699*/
190700static int fts3DoclistCountDocids(char *aList, int nList){
190701 int nDoc = 0; /* Return value */
190702 if( aList ){
190703 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
190704 char *p = aList; /* Cursor */
190705 while( p<aEnd ){
190706 nDoc++;
190707 while( (*p++)&0x80 ); /* Skip docid varint */
190708 fts3PoslistCopy(0, &p); /* Skip over position list */
190709 }
190710 }
190711
190712 return nDoc;
190713}
190714
190715/*
190716** Advance the cursor to the next row in the %_content table that
190717** matches the search criteria. For a MATCH search, this will be
190718** the next row that matches. For a full-table scan, this will be
190719** simply the next row in the %_content table. For a docid lookup,
190720** this routine simply sets the EOF flag.
190721**
190722** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
190723** even if we reach end-of-file. The fts3EofMethod() will be called
190724** subsequently to determine whether or not an EOF was hit.
190725*/
190726static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
190727 int rc;
190728 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
190729 if( pCsr->eSearch==FTS3_DOCID_SEARCH1 || pCsr->eSearch==FTS3_FULLSCAN_SEARCH0 ){
190730 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
190731 pTab->bLock++;
190732 if( SQLITE_ROW100!=sqlite3_step(pCsr->pStmt) ){
190733 pCsr->isEof = 1;
190734 rc = sqlite3_reset(pCsr->pStmt);
190735 }else{
190736 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
190737 rc = SQLITE_OK0;
190738 }
190739 pTab->bLock--;
190740 }else{
190741 rc = fts3EvalNext((Fts3Cursor *)pCursor);
190742 }
190743 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
190744 return rc;
190745}
190746
190747/*
190748** If the numeric type of argument pVal is "integer", then return it
190749** converted to a 64-bit signed integer. Otherwise, return a copy of
190750** the second parameter, iDefault.
190751*/
190752static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
190753 if( pVal ){
190754 int eType = sqlite3_value_numeric_type(pVal);
190755 if( eType==SQLITE_INTEGER1 ){
190756 return sqlite3_value_int64(pVal);
190757 }
190758 }
190759 return iDefault;
190760}
190761
190762/*
190763** This is the xFilter interface for the virtual table. See
190764** the virtual table xFilter method documentation for additional
190765** information.
190766**
190767** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
190768** the %_content table.
190769**
190770** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
190771** in the %_content table.
190772**
190773** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
190774** column on the left-hand side of the MATCH operator is column
190775** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
190776** side of the MATCH operator.
190777*/
190778static int fts3FilterMethod(
190779 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
190780 int idxNum, /* Strategy index */
190781 const char *idxStr, /* Unused */
190782 int nVal, /* Number of elements in apVal */
190783 sqlite3_value **apVal /* Arguments for the indexing scheme */
190784){
190785 int rc = SQLITE_OK0;
190786 char *zSql; /* SQL statement used to access %_content */
190787 int eSearch;
190788 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
190789 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
190790
190791 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
190792 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
190793 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
190794 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
190795 int iIdx;
190796
190797 UNUSED_PARAMETER(idxStr)(void)(idxStr);
190798 UNUSED_PARAMETER(nVal)(void)(nVal);
190799
190800 if( p->bLock ){
190801 return SQLITE_ERROR1;
190802 }
190803
190804 eSearch = (idxNum & 0x0000FFFF);
190805 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) )((void) (0));
190806 assert( p->pSegments==0 )((void) (0));
190807
190808 /* Collect arguments into local variables */
190809 iIdx = 0;
190810 if( eSearch!=FTS3_FULLSCAN_SEARCH0 ) pCons = apVal[iIdx++];
190811 if( idxNum & FTS3_HAVE_LANGID0x00010000 ) pLangid = apVal[iIdx++];
190812 if( idxNum & FTS3_HAVE_DOCID_GE0x00020000 ) pDocidGe = apVal[iIdx++];
190813 if( idxNum & FTS3_HAVE_DOCID_LE0x00040000 ) pDocidLe = apVal[iIdx++];
190814 assert( iIdx==nVal )((void) (0));
190815
190816 /* In case the cursor has been used before, clear it now. */
190817 fts3ClearCursor(pCsr);
190818
190819 /* Set the lower and upper bounds on docids to return */
190820 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
190821 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
190822
190823 if( idxStr ){
190824 pCsr->bDesc = (idxStr[0]=='D');
190825 }else{
190826 pCsr->bDesc = p->bDescIdx;
190827 }
190828 pCsr->eSearch = (i16)eSearch;
190829
190830 if( eSearch!=FTS3_DOCID_SEARCH1 && eSearch!=FTS3_FULLSCAN_SEARCH0 ){
190831 int iCol = eSearch-FTS3_FULLTEXT_SEARCH2;
190832 const char *zQuery = (const char *)sqlite3_value_text(pCons);
190833
190834 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL5 ){
190835 return SQLITE_NOMEM7;
190836 }
190837
190838 pCsr->iLangid = 0;
190839 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
190840
190841 assert( p->base.zErrMsg==0 )((void) (0));
190842 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
190843 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
190844 &p->base.zErrMsg
190845 );
190846 if( rc!=SQLITE_OK0 ){
190847 return rc;
190848 }
190849
190850 rc = fts3EvalStart(pCsr);
190851 sqlite3Fts3SegmentsClose(p);
190852 if( rc!=SQLITE_OK0 ) return rc;
190853 pCsr->pNextId = pCsr->aDoclist;
190854 pCsr->iPrevId = 0;
190855 }
190856
190857 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
190858 ** statement loops through all rows of the %_content table. For a
190859 ** full-text query or docid lookup, the statement retrieves a single
190860 ** row by docid.
190861 */
190862 if( eSearch==FTS3_FULLSCAN_SEARCH0 ){
190863 if( pDocidGe || pDocidLe ){
190864 zSql = sqlite3_mprintf(
190865 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
190866 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
190867 (pCsr->bDesc ? "DESC" : "ASC")
190868 );
190869 }else{
190870 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
190871 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
190872 );
190873 }
190874 if( zSql ){
190875 p->bLock++;
190876 rc = sqlite3_prepare_v3(
190877 p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0
190878 );
190879 p->bLock--;
190880 sqlite3_free(zSql);
190881 }else{
190882 rc = SQLITE_NOMEM7;
190883 }
190884 }else if( eSearch==FTS3_DOCID_SEARCH1 ){
190885 rc = fts3CursorSeekStmt(pCsr);
190886 if( rc==SQLITE_OK0 ){
190887 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
190888 }
190889 }
190890 if( rc!=SQLITE_OK0 ) return rc;
190891
190892 return fts3NextMethod(pCursor);
190893}
190894
190895/*
190896** This is the xEof method of the virtual table. SQLite calls this
190897** routine to find out if it has reached the end of a result set.
190898*/
190899static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
190900 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
190901 if( pCsr->isEof ){
190902 fts3ClearCursor(pCsr);
190903 pCsr->isEof = 1;
190904 }
190905 return pCsr->isEof;
190906}
190907
190908/*
190909** This is the xRowid method. The SQLite core calls this routine to
190910** retrieve the rowid for the current row of the result set. fts3
190911** exposes %_content.docid as the rowid for the virtual table. The
190912** rowid should be written to *pRowid.
190913*/
190914static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
190915 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
190916 *pRowid = pCsr->iPrevId;
190917 return SQLITE_OK0;
190918}
190919
190920/*
190921** This is the xColumn method, called by SQLite to request a value from
190922** the row that the supplied cursor currently points to.
190923**
190924** If:
190925**
190926** (iCol < p->nColumn) -> The value of the iCol'th user column.
190927** (iCol == p->nColumn) -> Magic column with the same name as the table.
190928** (iCol == p->nColumn+1) -> Docid column
190929** (iCol == p->nColumn+2) -> Langid column
190930*/
190931static int fts3ColumnMethod(
190932 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
190933 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
190934 int iCol /* Index of column to read value from */
190935){
190936 int rc = SQLITE_OK0; /* Return Code */
190937 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
190938 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
190939
190940 /* The column value supplied by SQLite must be in range. */
190941 assert( iCol>=0 && iCol<=p->nColumn+2 )((void) (0));
190942
190943 switch( iCol-p->nColumn ){
190944 case 0:
190945 /* The special 'table-name' column */
190946 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
190947 break;
190948
190949 case 1:
190950 /* The docid column */
190951 sqlite3_result_int64(pCtx, pCsr->iPrevId);
190952 break;
190953
190954 case 2:
190955 if( pCsr->pExpr ){
190956 sqlite3_result_int64(pCtx, pCsr->iLangid);
190957 break;
190958 }else if( p->zLanguageid==0 ){
190959 sqlite3_result_int(pCtx, 0);
190960 break;
190961 }else{
190962 iCol = p->nColumn;
190963 /* no break */ deliberate_fall_through__attribute__((fallthrough));
190964 }
190965
190966 default:
190967 /* A user column. Or, if this is a full-table scan, possibly the
190968 ** language-id column. Seek the cursor. */
190969 rc = fts3CursorSeek(0, pCsr);
190970 if( rc==SQLITE_OK0 && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
190971 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
190972 }
190973 break;
190974 }
190975
190976 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
190977 return rc;
190978}
190979
190980/*
190981** This function is the implementation of the xUpdate callback used by
190982** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
190983** inserted, updated or deleted.
190984*/
190985static int fts3UpdateMethod(
190986 sqlite3_vtab *pVtab, /* Virtual table handle */
190987 int nArg, /* Size of argument array */
190988 sqlite3_value **apVal, /* Array of arguments */
190989 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
190990){
190991 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
190992}
190993
190994/*
190995** Implementation of xSync() method. Flush the contents of the pending-terms
190996** hash-table to the database.
190997*/
190998static int fts3SyncMethod(sqlite3_vtab *pVtab){
190999
191000 /* Following an incremental-merge operation, assuming that the input
191001 ** segments are not completely consumed (the usual case), they are updated
191002 ** in place to remove the entries that have already been merged. This
191003 ** involves updating the leaf block that contains the smallest unmerged
191004 ** entry and each block (if any) between the leaf and the root node. So
191005 ** if the height of the input segment b-trees is N, and input segments
191006 ** are merged eight at a time, updating the input segments at the end
191007 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
191008 ** small - often between 0 and 2. So the overhead of the incremental
191009 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
191010 ** dwarfing the actual productive work accomplished, the incremental merge
191011 ** is only attempted if it will write at least 64 leaf blocks. Hence
191012 ** nMinMerge.
191013 **
191014 ** Of course, updating the input segments also involves deleting a bunch
191015 ** of blocks from the segments table. But this is not considered overhead
191016 ** as it would also be required by a crisis-merge that used the same input
191017 ** segments.
191018 */
191019 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
191020
191021 Fts3Table *p = (Fts3Table*)pVtab;
191022 int rc;
191023 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
191024
191025 rc = sqlite3Fts3PendingTermsFlush(p);
191026 if( rc==SQLITE_OK0
191027 && p->nLeafAdd>(nMinMerge/16)
191028 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
191029 ){
191030 int mxLevel = 0; /* Maximum relative level value in db */
191031 int A; /* Incr-merge parameter A */
191032
191033 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
191034 assert( rc==SQLITE_OK || mxLevel==0 )((void) (0));
191035 A = p->nLeafAdd * mxLevel;
191036 A += (A/2);
191037 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
191038 }
191039 sqlite3Fts3SegmentsClose(p);
191040 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
191041 return rc;
191042}
191043
191044/*
191045** If it is currently unknown whether or not the FTS table has an %_stat
191046** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
191047** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
191048** if an error occurs.
191049*/
191050static int fts3SetHasStat(Fts3Table *p){
191051 int rc = SQLITE_OK0;
191052 if( p->bHasStat==2 ){
191053 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
191054 if( zTbl ){
191055 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
191056 sqlite3_free(zTbl);
191057 p->bHasStat = (res==SQLITE_OK0);
191058 }else{
191059 rc = SQLITE_NOMEM7;
191060 }
191061 }
191062 return rc;
191063}
191064
191065/*
191066** Implementation of xBegin() method.
191067*/
191068static int fts3BeginMethod(sqlite3_vtab *pVtab){
191069 Fts3Table *p = (Fts3Table*)pVtab;
191070 int rc;
191071 UNUSED_PARAMETER(pVtab)(void)(pVtab);
191072 assert( p->pSegments==0 )((void) (0));
191073 assert( p->nPendingData==0 )((void) (0));
191074 assert( p->inTransaction!=1 )((void) (0));
191075 p->nLeafAdd = 0;
191076 rc = fts3SetHasStat(p);
191077#ifdef SQLITE_DEBUG
191078 if( rc==SQLITE_OK0 ){
191079 p->inTransaction = 1;
191080 p->mxSavepoint = -1;
191081 }
191082#endif
191083 return rc;
191084}
191085
191086/*
191087** Implementation of xCommit() method. This is a no-op. The contents of
191088** the pending-terms hash-table have already been flushed into the database
191089** by fts3SyncMethod().
191090*/
191091static int fts3CommitMethod(sqlite3_vtab *pVtab){
191092 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
191093 UNUSED_PARAMETER(pVtab)(void)(pVtab);
191094 assert( p->nPendingData==0 )((void) (0));
191095 assert( p->inTransaction!=0 )((void) (0));
191096 assert( p->pSegments==0 )((void) (0));
191097 TESTONLY( p->inTransaction = 0 );
191098 TESTONLY( p->mxSavepoint = -1; );
191099 return SQLITE_OK0;
191100}
191101
191102/*
191103** Implementation of xRollback(). Discard the contents of the pending-terms
191104** hash-table. Any changes made to the database are reverted by SQLite.
191105*/
191106static int fts3RollbackMethod(sqlite3_vtab *pVtab){
191107 Fts3Table *p = (Fts3Table*)pVtab;
191108 sqlite3Fts3PendingTermsClear(p);
191109 assert( p->inTransaction!=0 )((void) (0));
191110 TESTONLY( p->inTransaction = 0 );
191111 TESTONLY( p->mxSavepoint = -1; );
191112 return SQLITE_OK0;
191113}
191114
191115/*
191116** When called, *ppPoslist must point to the byte immediately following the
191117** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
191118** moves *ppPoslist so that it instead points to the first byte of the
191119** same position list.
191120*/
191121static void fts3ReversePoslist(char *pStart, char **ppPoslist){
191122 char *p = &(*ppPoslist)[-2];
191123 char c = 0;
191124
191125 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
191126 while( p>pStart && (c=*p--)==0 );
191127
191128 /* Search backwards for a varint with value zero (the end of the previous
191129 ** poslist). This is an 0x00 byte preceded by some byte that does not
191130 ** have the 0x80 bit set. */
191131 while( p>pStart && (*p & 0x80) | c ){
191132 c = *p--;
191133 }
191134 assert( p==pStart || c==0 )((void) (0));
191135
191136 /* At this point p points to that preceding byte without the 0x80 bit
191137 ** set. So to find the start of the poslist, skip forward 2 bytes then
191138 ** over a varint.
191139 **
191140 ** Normally. The other case is that p==pStart and the poslist to return
191141 ** is the first in the doclist. In this case do not skip forward 2 bytes.
191142 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
191143 ** is required for cases where the first byte of a doclist and the
191144 ** doclist is empty. For example, if the first docid is 10, a doclist
191145 ** that begins with:
191146 **
191147 ** 0x0A 0x00 <next docid delta varint>
191148 */
191149 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
191150 while( *p++&0x80 );
191151 *ppPoslist = p;
191152}
191153
191154/*
191155** Helper function used by the implementation of the overloaded snippet(),
191156** offsets() and optimize() SQL functions.
191157**
191158** If the value passed as the third argument is a blob of size
191159** sizeof(Fts3Cursor*), then the blob contents are copied to the
191160** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
191161** message is written to context pContext and SQLITE_ERROR returned. The
191162** string passed via zFunc is used as part of the error message.
191163*/
191164static int fts3FunctionArg(
191165 sqlite3_context *pContext, /* SQL function call context */
191166 const char *zFunc, /* Function name */
191167 sqlite3_value *pVal, /* argv[0] passed to function */
191168 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
191169){
191170 int rc;
191171 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
191172 if( (*ppCsr)!=0 ){
191173 rc = SQLITE_OK0;
191174 }else{
191175 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
191176 sqlite3_result_error(pContext, zErr, -1);
191177 sqlite3_free(zErr);
191178 rc = SQLITE_ERROR1;
191179 }
191180 return rc;
191181}
191182
191183/*
191184** Implementation of the snippet() function for FTS3
191185*/
191186static void fts3SnippetFunc(
191187 sqlite3_context *pContext, /* SQLite function call context */
191188 int nVal, /* Size of apVal[] array */
191189 sqlite3_value **apVal /* Array of arguments */
191190){
191191 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
191192 const char *zStart = "<b>";
191193 const char *zEnd = "</b>";
191194 const char *zEllipsis = "<b>...</b>";
191195 int iCol = -1;
191196 int nToken = 15; /* Default number of tokens in snippet */
191197
191198 /* There must be at least one argument passed to this function (otherwise
191199 ** the non-overloaded version would have been called instead of this one).
191200 */
191201 assert( nVal>=1 )((void) (0));
191202
191203 if( nVal>6 ){
191204 sqlite3_result_error(pContext,
191205 "wrong number of arguments to function snippet()", -1);
191206 return;
191207 }
191208 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
191209
191210 switch( nVal ){
191211 case 6: nToken = sqlite3_value_int(apVal[5]);
191212 /* no break */ deliberate_fall_through__attribute__((fallthrough));
191213 case 5: iCol = sqlite3_value_int(apVal[4]);
191214 /* no break */ deliberate_fall_through__attribute__((fallthrough));
191215 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
191216 /* no break */ deliberate_fall_through__attribute__((fallthrough));
191217 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
191218 /* no break */ deliberate_fall_through__attribute__((fallthrough));
191219 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
191220 }
191221 if( !zEllipsis || !zEnd || !zStart ){
191222 sqlite3_result_error_nomem(pContext);
191223 }else if( nToken==0 ){
191224 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
191225 }else if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
191226 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
191227 }
191228}
191229
191230/*
191231** Implementation of the offsets() function for FTS3
191232*/
191233static void fts3OffsetsFunc(
191234 sqlite3_context *pContext, /* SQLite function call context */
191235 int nVal, /* Size of argument array */
191236 sqlite3_value **apVal /* Array of arguments */
191237){
191238 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
191239
191240 UNUSED_PARAMETER(nVal)(void)(nVal);
191241
191242 assert( nVal==1 )((void) (0));
191243 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
191244 assert( pCsr )((void) (0));
191245 if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
191246 sqlite3Fts3Offsets(pContext, pCsr);
191247 }
191248}
191249
191250/*
191251** Implementation of the special optimize() function for FTS3. This
191252** function merges all segments in the database to a single segment.
191253** Example usage is:
191254**
191255** SELECT optimize(t) FROM t LIMIT 1;
191256**
191257** where 't' is the name of an FTS3 table.
191258*/
191259static void fts3OptimizeFunc(
191260 sqlite3_context *pContext, /* SQLite function call context */
191261 int nVal, /* Size of argument array */
191262 sqlite3_value **apVal /* Array of arguments */
191263){
191264 int rc; /* Return code */
191265 Fts3Table *p; /* Virtual table handle */
191266 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
191267
191268 UNUSED_PARAMETER(nVal)(void)(nVal);
191269
191270 assert( nVal==1 )((void) (0));
191271 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
191272 p = (Fts3Table *)pCursor->base.pVtab;
191273 assert( p )((void) (0));
191274
191275 rc = sqlite3Fts3Optimize(p);
191276
191277 switch( rc ){
191278 case SQLITE_OK0:
191279 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
191280 break;
191281 case SQLITE_DONE101:
191282 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
191283 break;
191284 default:
191285 sqlite3_result_error_code(pContext, rc);
191286 break;
191287 }
191288}
191289
191290/*
191291** Implementation of the matchinfo() function for FTS3
191292*/
191293static void fts3MatchinfoFunc(
191294 sqlite3_context *pContext, /* SQLite function call context */
191295 int nVal, /* Size of argument array */
191296 sqlite3_value **apVal /* Array of arguments */
191297){
191298 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
191299 assert( nVal==1 || nVal==2 )((void) (0));
191300 if( SQLITE_OK0==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
191301 const char *zArg = 0;
191302 if( nVal>1 ){
191303 zArg = (const char *)sqlite3_value_text(apVal[1]);
191304 }
191305 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
191306 }
191307}
191308
191309/*
191310** This routine implements the xFindFunction method for the FTS3
191311** virtual table.
191312*/
191313static int fts3FindFunctionMethod(
191314 sqlite3_vtab *pVtab, /* Virtual table handle */
191315 int nArg, /* Number of SQL function arguments */
191316 const char *zName, /* Name of SQL function */
191317 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
191318 void **ppArg /* Unused */
191319){
191320 struct Overloaded {
191321 const char *zName;
191322 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
191323 } aOverload[] = {
191324 { "snippet", fts3SnippetFunc },
191325 { "offsets", fts3OffsetsFunc },
191326 { "optimize", fts3OptimizeFunc },
191327 { "matchinfo", fts3MatchinfoFunc },
191328 };
191329 int i; /* Iterator variable */
191330
191331 UNUSED_PARAMETER(pVtab)(void)(pVtab);
191332 UNUSED_PARAMETER(nArg)(void)(nArg);
191333 UNUSED_PARAMETER(ppArg)(void)(ppArg);
191334
191335 for(i=0; i<SizeofArray(aOverload)((int)(sizeof(aOverload)/sizeof(aOverload[0]))); i++){
191336 if( strcmp(zName, aOverload[i].zName)==0 ){
191337 *pxFunc = aOverload[i].xFunc;
191338 return 1;
191339 }
191340 }
191341
191342 /* No function of the specified name was found. Return 0. */
191343 return 0;
191344}
191345
191346/*
191347** Implementation of FTS3 xRename method. Rename an fts3 table.
191348*/
191349static int fts3RenameMethod(
191350 sqlite3_vtab *pVtab, /* Virtual table handle */
191351 const char *zName /* New name of table */
191352){
191353 Fts3Table *p = (Fts3Table *)pVtab;
191354 sqlite3 *db = p->db; /* Database connection */
191355 int rc; /* Return Code */
191356
191357 /* At this point it must be known if the %_stat table exists or not.
191358 ** So bHasStat may not be 2. */
191359 rc = fts3SetHasStat(p);
191360
191361 /* As it happens, the pending terms table is always empty here. This is
191362 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
191363 ** always opens a savepoint transaction. And the xSavepoint() method
191364 ** flushes the pending terms table. But leave the (no-op) call to
191365 ** PendingTermsFlush() in in case that changes.
191366 */
191367 assert( p->nPendingData==0 )((void) (0));
191368 if( rc==SQLITE_OK0 ){
191369 rc = sqlite3Fts3PendingTermsFlush(p);
191370 }
191371
191372 p->bIgnoreSavepoint = 1;
191373
191374 if( p->zContentTbl==0 ){
191375 fts3DbExec(&rc, db,
191376 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
191377 p->zDb, p->zName, zName
191378 );
191379 }
191380
191381 if( p->bHasDocsize ){
191382 fts3DbExec(&rc, db,
191383 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
191384 p->zDb, p->zName, zName
191385 );
191386 }
191387 if( p->bHasStat ){
191388 fts3DbExec(&rc, db,
191389 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
191390 p->zDb, p->zName, zName
191391 );
191392 }
191393 fts3DbExec(&rc, db,
191394 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
191395 p->zDb, p->zName, zName
191396 );
191397 fts3DbExec(&rc, db,
191398 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
191399 p->zDb, p->zName, zName
191400 );
191401
191402 p->bIgnoreSavepoint = 0;
191403 return rc;
191404}
191405
191406/*
191407** The xSavepoint() method.
191408**
191409** Flush the contents of the pending-terms table to disk.
191410*/
191411static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
191412 int rc = SQLITE_OK0;
191413 Fts3Table *pTab = (Fts3Table*)pVtab;
191414 assert( pTab->inTransaction )((void) (0));
191415 assert( pTab->mxSavepoint<=iSavepoint )((void) (0));
191416 TESTONLY( pTab->mxSavepoint = iSavepoint );
191417
191418 if( pTab->bIgnoreSavepoint==0 ){
191419 if( fts3HashCount(&pTab->aIndex[0].hPending)((&pTab->aIndex[0].hPending)->count)>0 ){
191420 char *zSql = sqlite3_mprintf("INSERT INTO %Q.%Q(%Q) VALUES('flush')",
191421 pTab->zDb, pTab->zName, pTab->zName
191422 );
191423 if( zSql ){
191424 pTab->bIgnoreSavepoint = 1;
191425 rc = sqlite3_exec(pTab->db, zSql, 0, 0, 0);
191426 pTab->bIgnoreSavepoint = 0;
191427 sqlite3_free(zSql);
191428 }else{
191429 rc = SQLITE_NOMEM7;
191430 }
191431 }
191432 if( rc==SQLITE_OK0 ){
191433 pTab->iSavepoint = iSavepoint+1;
191434 }
191435 }
191436 return rc;
191437}
191438
191439/*
191440** The xRelease() method.
191441**
191442** This is a no-op.
191443*/
191444static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
191445 Fts3Table *pTab = (Fts3Table*)pVtab;
191446 assert( pTab->inTransaction )((void) (0));
191447 assert( pTab->mxSavepoint >= iSavepoint )((void) (0));
191448 TESTONLY( pTab->mxSavepoint = iSavepoint-1 );
191449 pTab->iSavepoint = iSavepoint;
191450 return SQLITE_OK0;
191451}
191452
191453/*
191454** The xRollbackTo() method.
191455**
191456** Discard the contents of the pending terms table.
191457*/
191458static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
191459 Fts3Table *pTab = (Fts3Table*)pVtab;
191460 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
191461 assert( pTab->inTransaction )((void) (0));
191462 TESTONLY( pTab->mxSavepoint = iSavepoint );
191463 if( (iSavepoint+1)<=pTab->iSavepoint ){
191464 sqlite3Fts3PendingTermsClear(pTab);
191465 }
191466 return SQLITE_OK0;
191467}
191468
191469/*
191470** Return true if zName is the extension on one of the shadow tables used
191471** by this module.
191472*/
191473static int fts3ShadowName(const char *zName){
191474 static const char *azName[] = {
191475 "content", "docsize", "segdir", "segments", "stat",
191476 };
191477 unsigned int i;
191478 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
191479 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
191480 }
191481 return 0;
191482}
191483
191484/*
191485** Implementation of the xIntegrity() method on the FTS3/FTS4 virtual
191486** table.
191487*/
191488static int fts3IntegrityMethod(
191489 sqlite3_vtab *pVtab, /* The virtual table to be checked */
191490 const char *zSchema, /* Name of schema in which pVtab lives */
191491 const char *zTabname, /* Name of the pVTab table */
191492 int isQuick, /* True if this is a quick_check */
191493 char **pzErr /* Write error message here */
191494){
191495 Fts3Table *p = (Fts3Table*)pVtab;
191496 int rc = SQLITE_OK0;
191497 int bOk = 0;
191498
191499 UNUSED_PARAMETER(isQuick)(void)(isQuick);
191500 rc = sqlite3Fts3IntegrityCheck(p, &bOk);
191501 assert( rc!=SQLITE_CORRUPT_VTAB )((void) (0));
191502 if( rc==SQLITE_ERROR1 || (rc&0xFF)==SQLITE_CORRUPT11 ){
191503 *pzErr = sqlite3_mprintf("unable to validate the inverted index for"
191504 " FTS%d table %s.%s: %s",
191505 p->bFts4 ? 4 : 3, zSchema, zTabname, sqlite3_errstr(rc));
191506 if( *pzErr ) rc = SQLITE_OK0;
191507 }else if( rc==SQLITE_OK0 && bOk==0 ){
191508 *pzErr = sqlite3_mprintf("malformed inverted index for FTS%d table %s.%s",
191509 p->bFts4 ? 4 : 3, zSchema, zTabname);
191510 if( *pzErr==0 ) rc = SQLITE_NOMEM7;
191511 }
191512 sqlite3Fts3SegmentsClose(p);
191513 return rc;
191514}
191515
191516
191517
191518static const sqlite3_module fts3Module = {
191519 /* iVersion */ 4,
191520 /* xCreate */ fts3CreateMethod,
191521 /* xConnect */ fts3ConnectMethod,
191522 /* xBestIndex */ fts3BestIndexMethod,
191523 /* xDisconnect */ fts3DisconnectMethod,
191524 /* xDestroy */ fts3DestroyMethod,
191525 /* xOpen */ fts3OpenMethod,
191526 /* xClose */ fts3CloseMethod,
191527 /* xFilter */ fts3FilterMethod,
191528 /* xNext */ fts3NextMethod,
191529 /* xEof */ fts3EofMethod,
191530 /* xColumn */ fts3ColumnMethod,
191531 /* xRowid */ fts3RowidMethod,
191532 /* xUpdate */ fts3UpdateMethod,
191533 /* xBegin */ fts3BeginMethod,
191534 /* xSync */ fts3SyncMethod,
191535 /* xCommit */ fts3CommitMethod,
191536 /* xRollback */ fts3RollbackMethod,
191537 /* xFindFunction */ fts3FindFunctionMethod,
191538 /* xRename */ fts3RenameMethod,
191539 /* xSavepoint */ fts3SavepointMethod,
191540 /* xRelease */ fts3ReleaseMethod,
191541 /* xRollbackTo */ fts3RollbackToMethod,
191542 /* xShadowName */ fts3ShadowName,
191543 /* xIntegrity */ fts3IntegrityMethod,
191544};
191545
191546/*
191547** This function is registered as the module destructor (called when an
191548** FTS3 enabled database connection is closed). It frees the memory
191549** allocated for the tokenizer hash table.
191550*/
191551static void hashDestroy(void *p){
191552 Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
191553 pHash->nRef--;
191554 if( pHash->nRef<=0 ){
191555 sqlite3Fts3HashClear(&pHash->hash);
191556 sqlite3_free(pHash);
191557 }
191558}
191559
191560/*
191561** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
191562** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
191563** respectively. The following three forward declarations are for functions
191564** declared in these files used to retrieve the respective implementations.
191565**
191566** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
191567** to by the argument to point to the "simple" tokenizer implementation.
191568** And so on.
191569*/
191570SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
191571SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
191572#ifndef SQLITE_DISABLE_FTS3_UNICODE
191573SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
191574#endif
191575#ifdef SQLITE_ENABLE_ICU
191576SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
191577#endif
191578
191579/*
191580** Initialize the fts3 extension. If this extension is built as part
191581** of the sqlite library, then this function is called directly by
191582** SQLite. If fts3 is built as a dynamically loadable extension, this
191583** function is called by the sqlite3_extension_init() entry point.
191584*/
191585SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db){
191586 int rc = SQLITE_OK0;
191587 Fts3HashWrapper *pHash = 0;
191588 const sqlite3_tokenizer_module *pSimple = 0;
191589 const sqlite3_tokenizer_module *pPorter = 0;
191590#ifndef SQLITE_DISABLE_FTS3_UNICODE
191591 const sqlite3_tokenizer_module *pUnicode = 0;
191592#endif
191593
191594#ifdef SQLITE_ENABLE_ICU
191595 const sqlite3_tokenizer_module *pIcu = 0;
191596 sqlite3Fts3IcuTokenizerModule(&pIcu);
191597#endif
191598
191599#ifndef SQLITE_DISABLE_FTS3_UNICODE
191600 sqlite3Fts3UnicodeTokenizer(&pUnicode);
191601#endif
191602
191603#ifdef SQLITE_TEST
191604 rc = sqlite3Fts3InitTerm(db);
191605 if( rc!=SQLITE_OK0 ) return rc;
191606#endif
191607
191608 rc = sqlite3Fts3InitAux(db);
191609 if( rc!=SQLITE_OK0 ) return rc;
191610
191611 sqlite3Fts3SimpleTokenizerModule(&pSimple);
191612 sqlite3Fts3PorterTokenizerModule(&pPorter);
191613
191614 /* Allocate and initialize the hash-table used to store tokenizers. */
191615 pHash = sqlite3_malloc(sizeof(Fts3HashWrapper));
191616 if( !pHash ){
191617 rc = SQLITE_NOMEM7;
191618 }else{
191619 sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING1, 1);
191620 pHash->nRef = 0;
191621 }
191622
191623 /* Load the built-in tokenizers into the hash table */
191624 if( rc==SQLITE_OK0 ){
191625 if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple)
191626 || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter)
191627
191628#ifndef SQLITE_DISABLE_FTS3_UNICODE
191629 || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode)
191630#endif
191631#ifdef SQLITE_ENABLE_ICU
191632 || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
191633#endif
191634 ){
191635 rc = SQLITE_NOMEM7;
191636 }
191637 }
191638
191639#ifdef SQLITE_TEST
191640 if( rc==SQLITE_OK0 ){
191641 rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
191642 }
191643#endif
191644
191645 /* Create the virtual table wrapper around the hash-table and overload
191646 ** the four scalar functions. If this is successful, register the
191647 ** module with sqlite.
191648 */
191649 if( SQLITE_OK0==rc
191650 && SQLITE_OK0==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
191651 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "snippet", -1))
191652 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "offsets", 1))
191653 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 1))
191654 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 2))
191655 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "optimize", 1))
191656 ){
191657 pHash->nRef++;
191658 rc = sqlite3_create_module_v2(
191659 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
191660 );
191661 if( rc==SQLITE_OK0 ){
191662 pHash->nRef++;
191663 rc = sqlite3_create_module_v2(
191664 db, "fts4", &fts3Module, (void *)pHash, hashDestroy
191665 );
191666 }
191667 if( rc==SQLITE_OK0 ){
191668 pHash->nRef++;
191669 rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy);
191670 }
191671 return rc;
191672 }
191673
191674
191675 /* An error has occurred. Delete the hash table and return the error code. */
191676 assert( rc!=SQLITE_OK )((void) (0));
191677 if( pHash ){
191678 sqlite3Fts3HashClear(&pHash->hash);
191679 sqlite3_free(pHash);
191680 }
191681 return rc;
191682}
191683
191684/*
191685** Allocate an Fts3MultiSegReader for each token in the expression headed
191686** by pExpr.
191687**
191688** An Fts3SegReader object is a cursor that can seek or scan a range of
191689** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
191690** Fts3SegReader objects internally to provide an interface to seek or scan
191691** within the union of all segments of a b-tree. Hence the name.
191692**
191693** If the allocated Fts3MultiSegReader just seeks to a single entry in a
191694** segment b-tree (if the term is not a prefix or it is a prefix for which
191695** there exists prefix b-tree of the right length) then it may be traversed
191696** and merged incrementally. Otherwise, it has to be merged into an in-memory
191697** doclist and then traversed.
191698*/
191699static void fts3EvalAllocateReaders(
191700 Fts3Cursor *pCsr, /* FTS cursor handle */
191701 Fts3Expr *pExpr, /* Allocate readers for this expression */
191702 int *pnToken, /* OUT: Total number of tokens in phrase. */
191703 int *pnOr, /* OUT: Total number of OR nodes in expr. */
191704 int *pRc /* IN/OUT: Error code */
191705){
191706 if( pExpr && SQLITE_OK0==*pRc ){
191707 if( pExpr->eType==FTSQUERY_PHRASE5 ){
191708 int i;
191709 int nToken = pExpr->pPhrase->nToken;
191710 *pnToken += nToken;
191711 for(i=0; i<nToken; i++){
191712 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
191713 int rc = fts3TermSegReaderCursor(pCsr,
191714 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
191715 );
191716 if( rc!=SQLITE_OK0 ){
191717 *pRc = rc;
191718 return;
191719 }
191720 }
191721 assert( pExpr->pPhrase->iDoclistToken==0 )((void) (0));
191722 pExpr->pPhrase->iDoclistToken = -1;
191723 }else{
191724 *pnOr += (pExpr->eType==FTSQUERY_OR4);
191725 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
191726 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
191727 }
191728 }
191729}
191730
191731/*
191732** Arguments pList/nList contain the doclist for token iToken of phrase p.
191733** It is merged into the main doclist stored in p->doclist.aAll/nAll.
191734**
191735** This function assumes that pList points to a buffer allocated using
191736** sqlite3_malloc(). This function takes responsibility for eventually
191737** freeing the buffer.
191738**
191739** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
191740*/
191741static int fts3EvalPhraseMergeToken(
191742 Fts3Table *pTab, /* FTS Table pointer */
191743 Fts3Phrase *p, /* Phrase to merge pList/nList into */
191744 int iToken, /* Token pList/nList corresponds to */
191745 char *pList, /* Pointer to doclist */
191746 int nList /* Number of bytes in pList */
191747){
191748 int rc = SQLITE_OK0;
191749 assert( iToken!=p->iDoclistToken )((void) (0));
191750
191751 if( pList==0 ){
191752 sqlite3_free(p->doclist.aAll);
191753 p->doclist.aAll = 0;
191754 p->doclist.nAll = 0;
191755 }
191756
191757 else if( p->iDoclistToken<0 ){
191758 p->doclist.aAll = pList;
191759 p->doclist.nAll = nList;
191760 }
191761
191762 else if( p->doclist.aAll==0 ){
191763 sqlite3_free(pList);
191764 }
191765
191766 else {
191767 char *pLeft;
191768 char *pRight;
191769 int nLeft;
191770 int nRight;
191771 int nDiff;
191772
191773 if( p->iDoclistToken<iToken ){
191774 pLeft = p->doclist.aAll;
191775 nLeft = p->doclist.nAll;
191776 pRight = pList;
191777 nRight = nList;
191778 nDiff = iToken - p->iDoclistToken;
191779 }else{
191780 pRight = p->doclist.aAll;
191781 nRight = p->doclist.nAll;
191782 pLeft = pList;
191783 nLeft = nList;
191784 nDiff = p->iDoclistToken - iToken;
191785 }
191786
191787 rc = fts3DoclistPhraseMerge(
191788 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
191789 );
191790 sqlite3_free(pLeft);
191791 p->doclist.aAll = pRight;
191792 p->doclist.nAll = nRight;
191793 }
191794
191795 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
191796 return rc;
191797}
191798
191799/*
191800** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
191801** does not take deferred tokens into account.
191802**
191803** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
191804*/
191805static int fts3EvalPhraseLoad(
191806 Fts3Cursor *pCsr, /* FTS Cursor handle */
191807 Fts3Phrase *p /* Phrase object */
191808){
191809 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
191810 int iToken;
191811 int rc = SQLITE_OK0;
191812
191813 for(iToken=0; rc==SQLITE_OK0 && iToken<p->nToken; iToken++){
191814 Fts3PhraseToken *pToken = &p->aToken[iToken];
191815 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 )((void) (0));
191816
191817 if( pToken->pSegcsr ){
191818 int nThis = 0;
191819 char *pThis = 0;
191820 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
191821 if( rc==SQLITE_OK0 ){
191822 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
191823 }
191824 }
191825 assert( pToken->pSegcsr==0 )((void) (0));
191826 }
191827
191828 return rc;
191829}
191830
191831#ifndef SQLITE_DISABLE_FTS4_DEFERRED
191832/*
191833** This function is called on each phrase after the position lists for
191834** any deferred tokens have been loaded into memory. It updates the phrases
191835** current position list to include only those positions that are really
191836** instances of the phrase (after considering deferred tokens). If this
191837** means that the phrase does not appear in the current row, doclist.pList
191838** and doclist.nList are both zeroed.
191839**
191840** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
191841*/
191842static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
191843 int iToken; /* Used to iterate through phrase tokens */
191844 char *aPoslist = 0; /* Position list for deferred tokens */
191845 int nPoslist = 0; /* Number of bytes in aPoslist */
191846 int iPrev = -1; /* Token number of previous deferred token */
191847 char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0);
191848
191849 for(iToken=0; iToken<pPhrase->nToken; iToken++){
191850 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
191851 Fts3DeferredToken *pDeferred = pToken->pDeferred;
191852
191853 if( pDeferred ){
191854 char *pList;
191855 int nList;
191856 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
191857 if( rc!=SQLITE_OK0 ) return rc;
191858
191859 if( pList==0 ){
191860 sqlite3_free(aPoslist);
191861 sqlite3_free(aFree);
191862 pPhrase->doclist.pList = 0;
191863 pPhrase->doclist.nList = 0;
191864 return SQLITE_OK0;
191865
191866 }else if( aPoslist==0 ){
191867 aPoslist = pList;
191868 nPoslist = nList;
191869
191870 }else{
191871 char *aOut = pList;
191872 char *p1 = aPoslist;
191873 char *p2 = aOut;
191874
191875 assert( iPrev>=0 )((void) (0));
191876 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
191877 sqlite3_free(aPoslist);
191878 aPoslist = pList;
191879 nPoslist = (int)(aOut - aPoslist);
191880 if( nPoslist==0 ){
191881 sqlite3_free(aPoslist);
191882 sqlite3_free(aFree);
191883 pPhrase->doclist.pList = 0;
191884 pPhrase->doclist.nList = 0;
191885 return SQLITE_OK0;
191886 }
191887 }
191888 iPrev = iToken;
191889 }
191890 }
191891
191892 if( iPrev>=0 ){
191893 int nMaxUndeferred = pPhrase->iDoclistToken;
191894 if( nMaxUndeferred<0 ){
191895 pPhrase->doclist.pList = aPoslist;
191896 pPhrase->doclist.nList = nPoslist;
191897 pPhrase->doclist.iDocid = pCsr->iPrevId;
191898 pPhrase->doclist.bFreeList = 1;
191899 }else{
191900 int nDistance;
191901 char *p1;
191902 char *p2;
191903 char *aOut;
191904
191905 if( nMaxUndeferred>iPrev ){
191906 p1 = aPoslist;
191907 p2 = pPhrase->doclist.pList;
191908 nDistance = nMaxUndeferred - iPrev;
191909 }else{
191910 p1 = pPhrase->doclist.pList;
191911 p2 = aPoslist;
191912 nDistance = iPrev - nMaxUndeferred;
191913 }
191914
191915 aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING8);
191916 if( !aOut ){
191917 sqlite3_free(aPoslist);
191918 return SQLITE_NOMEM7;
191919 }
191920
191921 pPhrase->doclist.pList = aOut;
191922 assert( p1 && p2 )((void) (0));
191923 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
191924 pPhrase->doclist.bFreeList = 1;
191925 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
191926 }else{
191927 sqlite3_free(aOut);
191928 pPhrase->doclist.pList = 0;
191929 pPhrase->doclist.nList = 0;
191930 }
191931 sqlite3_free(aPoslist);
191932 }
191933 }
191934
191935 if( pPhrase->doclist.pList!=aFree ) sqlite3_free(aFree);
191936 return SQLITE_OK0;
191937}
191938#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
191939
191940/*
191941** Maximum number of tokens a phrase may have to be considered for the
191942** incremental doclists strategy.
191943*/
191944#define MAX_INCR_PHRASE_TOKENS4 4
191945
191946/*
191947** This function is called for each Fts3Phrase in a full-text query
191948** expression to initialize the mechanism for returning rows. Once this
191949** function has been called successfully on an Fts3Phrase, it may be
191950** used with fts3EvalPhraseNext() to iterate through the matching docids.
191951**
191952** If parameter bOptOk is true, then the phrase may (or may not) use the
191953** incremental loading strategy. Otherwise, the entire doclist is loaded into
191954** memory within this call.
191955**
191956** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
191957*/
191958static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
191959 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
191960 int rc = SQLITE_OK0; /* Error code */
191961 int i;
191962
191963 /* Determine if doclists may be loaded from disk incrementally. This is
191964 ** possible if the bOptOk argument is true, the FTS doclists will be
191965 ** scanned in forward order, and the phrase consists of
191966 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
191967 ** tokens or prefix tokens that cannot use a prefix-index. */
191968 int bHaveIncr = 0;
191969 int bIncrOk = (bOptOk
191970 && pCsr->bDesc==pTab->bDescIdx
191971 && p->nToken<=MAX_INCR_PHRASE_TOKENS4 && p->nToken>0
191972#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
191973 && pTab->bNoIncrDoclist==0
191974#endif
191975 );
191976 for(i=0; bIncrOk==1 && i<p->nToken; i++){
191977 Fts3PhraseToken *pToken = &p->aToken[i];
191978 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
191979 bIncrOk = 0;
191980 }
191981 if( pToken->pSegcsr ) bHaveIncr = 1;
191982 }
191983
191984 if( bIncrOk && bHaveIncr ){
191985 /* Use the incremental approach. */
191986 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
191987 for(i=0; rc==SQLITE_OK0 && i<p->nToken; i++){
191988 Fts3PhraseToken *pToken = &p->aToken[i];
191989 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
191990 if( pSegcsr ){
191991 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
191992 }
191993 }
191994 p->bIncr = 1;
191995 }else{
191996 /* Load the full doclist for the phrase into memory. */
191997 rc = fts3EvalPhraseLoad(pCsr, p);
191998 p->bIncr = 0;
191999 }
192000
192001 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr )((void) (0));
192002 return rc;
192003}
192004
192005/*
192006** This function is used to iterate backwards (from the end to start)
192007** through doclists. It is used by this module to iterate through phrase
192008** doclists in reverse and by the fts3_write.c module to iterate through
192009** pending-terms lists when writing to databases with "order=desc".
192010**
192011** The doclist may be sorted in ascending (parameter bDescIdx==0) or
192012** descending (parameter bDescIdx==1) order of docid. Regardless, this
192013** function iterates from the end of the doclist to the beginning.
192014*/
192015SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(
192016 int bDescIdx, /* True if the doclist is desc */
192017 char *aDoclist, /* Pointer to entire doclist */
192018 int nDoclist, /* Length of aDoclist in bytes */
192019 char **ppIter, /* IN/OUT: Iterator pointer */
192020 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
192021 int *pnList, /* OUT: List length pointer */
192022 u8 *pbEof /* OUT: End-of-file flag */
192023){
192024 char *p = *ppIter;
192025
192026 assert( nDoclist>0 )((void) (0));
192027 assert( *pbEof==0 )((void) (0));
192028 assert_fts3_nc( p || *piDocid==0 )((void) (0));
192029 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) )((void) (0));
192030
192031 if( p==0 ){
192032 sqlite3_int64 iDocid = 0;
192033 char *pNext = 0;
192034 char *pDocid = aDoclist;
192035 char *pEnd = &aDoclist[nDoclist];
192036 int iMul = 1;
192037
192038 while( pDocid<pEnd ){
192039 sqlite3_int64 iDelta;
192040 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
192041 iDocid += (iMul * iDelta);
192042 pNext = pDocid;
192043 fts3PoslistCopy(0, &pDocid);
192044 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
192045 iMul = (bDescIdx ? -1 : 1);
192046 }
192047
192048 *pnList = (int)(pEnd - pNext);
192049 *ppIter = pNext;
192050 *piDocid = iDocid;
192051 }else{
192052 int iMul = (bDescIdx ? -1 : 1);
192053 sqlite3_int64 iDelta;
192054 fts3GetReverseVarint(&p, aDoclist, &iDelta);
192055 *piDocid -= (iMul * iDelta);
192056
192057 if( p==aDoclist ){
192058 *pbEof = 1;
192059 }else{
192060 char *pSave = p;
192061 fts3ReversePoslist(aDoclist, &p);
192062 *pnList = (int)(pSave - p);
192063 }
192064 *ppIter = p;
192065 }
192066}
192067
192068/*
192069** Iterate forwards through a doclist.
192070*/
192071SQLITE_PRIVATEstatic void sqlite3Fts3DoclistNext(
192072 int bDescIdx, /* True if the doclist is desc */
192073 char *aDoclist, /* Pointer to entire doclist */
192074 int nDoclist, /* Length of aDoclist in bytes */
192075 char **ppIter, /* IN/OUT: Iterator pointer */
192076 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
192077 u8 *pbEof /* OUT: End-of-file flag */
192078){
192079 char *p = *ppIter;
192080
192081 assert( nDoclist>0 )((void) (0));
192082 assert( *pbEof==0 )((void) (0));
192083 assert_fts3_nc( p || *piDocid==0 )((void) (0));
192084 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) )((void) (0));
192085
192086 if( p==0 ){
192087 p = aDoclist;
192088 p += sqlite3Fts3GetVarint(p, piDocid);
192089 }else{
192090 fts3PoslistCopy(0, &p);
192091 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
192092 if( p>=&aDoclist[nDoclist] ){
192093 *pbEof = 1;
192094 }else{
192095 sqlite3_int64 iVar;
192096 p += sqlite3Fts3GetVarint(p, &iVar);
192097 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
192098 }
192099 }
192100
192101 *ppIter = p;
192102}
192103
192104/*
192105** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
192106** to true if EOF is reached.
192107*/
192108static void fts3EvalDlPhraseNext(
192109 Fts3Table *pTab,
192110 Fts3Doclist *pDL,
192111 u8 *pbEof
192112){
192113 char *pIter; /* Used to iterate through aAll */
192114 char *pEnd; /* 1 byte past end of aAll */
192115
192116 if( pDL->pNextDocid ){
192117 pIter = pDL->pNextDocid;
192118 assert( pDL->aAll!=0 || pIter==0 )((void) (0));
192119 }else{
192120 pIter = pDL->aAll;
192121 }
192122
192123 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
192124 /* We have already reached the end of this doclist. EOF. */
192125 *pbEof = 1;
192126 }else{
192127 sqlite3_int64 iDelta;
192128 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
192129 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
192130 pDL->iDocid += iDelta;
192131 }else{
192132 pDL->iDocid -= iDelta;
192133 }
192134 pDL->pList = pIter;
192135 fts3PoslistCopy(0, &pIter);
192136 pDL->nList = (int)(pIter - pDL->pList);
192137
192138 /* pIter now points just past the 0x00 that terminates the position-
192139 ** list for document pDL->iDocid. However, if this position-list was
192140 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
192141 ** point to the start of the next docid value. The following line deals
192142 ** with this case by advancing pIter past the zero-padding added by
192143 ** fts3EvalNearTrim(). */
192144 while( pIter<pEnd && *pIter==0 ) pIter++;
192145
192146 pDL->pNextDocid = pIter;
192147 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter )((void) (0));
192148 *pbEof = 0;
192149 }
192150}
192151
192152/*
192153** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
192154*/
192155typedef struct TokenDoclist TokenDoclist;
192156struct TokenDoclist {
192157 int bIgnore;
192158 sqlite3_int64 iDocid;
192159 char *pList;
192160 int nList;
192161};
192162
192163/*
192164** Token pToken is an incrementally loaded token that is part of a
192165** multi-token phrase. Advance it to the next matching document in the
192166** database and populate output variable *p with the details of the new
192167** entry. Or, if the iterator has reached EOF, set *pbEof to true.
192168**
192169** If an error occurs, return an SQLite error code. Otherwise, return
192170** SQLITE_OK.
192171*/
192172static int incrPhraseTokenNext(
192173 Fts3Table *pTab, /* Virtual table handle */
192174 Fts3Phrase *pPhrase, /* Phrase to advance token of */
192175 int iToken, /* Specific token to advance */
192176 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
192177 u8 *pbEof /* OUT: True if iterator is at EOF */
192178){
192179 int rc = SQLITE_OK0;
192180
192181 if( pPhrase->iDoclistToken==iToken ){
192182 assert( p->bIgnore==0 )((void) (0));
192183 assert( pPhrase->aToken[iToken].pSegcsr==0 )((void) (0));
192184 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
192185 p->pList = pPhrase->doclist.pList;
192186 p->nList = pPhrase->doclist.nList;
192187 p->iDocid = pPhrase->doclist.iDocid;
192188 }else{
192189 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
192190 assert( pToken->pDeferred==0 )((void) (0));
192191 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 )((void) (0));
192192 if( pToken->pSegcsr ){
192193 assert( p->bIgnore==0 )((void) (0));
192194 rc = sqlite3Fts3MsrIncrNext(
192195 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
192196 );
192197 if( p->pList==0 ) *pbEof = 1;
192198 }else{
192199 p->bIgnore = 1;
192200 }
192201 }
192202
192203 return rc;
192204}
192205
192206
192207/*
192208** The phrase iterator passed as the second argument:
192209**
192210** * features at least one token that uses an incremental doclist, and
192211**
192212** * does not contain any deferred tokens.
192213**
192214** Advance it to the next matching documnent in the database and populate
192215** the Fts3Doclist.pList and nList fields.
192216**
192217** If there is no "next" entry and no error occurs, then *pbEof is set to
192218** 1 before returning. Otherwise, if no error occurs and the iterator is
192219** successfully advanced, *pbEof is set to 0.
192220**
192221** If an error occurs, return an SQLite error code. Otherwise, return
192222** SQLITE_OK.
192223*/
192224static int fts3EvalIncrPhraseNext(
192225 Fts3Cursor *pCsr, /* FTS Cursor handle */
192226 Fts3Phrase *p, /* Phrase object to advance to next docid */
192227 u8 *pbEof /* OUT: Set to 1 if EOF */
192228){
192229 int rc = SQLITE_OK0;
192230 Fts3Doclist *pDL = &p->doclist;
192231 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
192232 u8 bEof = 0;
192233
192234 /* This is only called if it is guaranteed that the phrase has at least
192235 ** one incremental token. In which case the bIncr flag is set. */
192236 assert( p->bIncr==1 )((void) (0));
192237
192238 if( p->nToken==1 ){
192239 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
192240 &pDL->iDocid, &pDL->pList, &pDL->nList
192241 );
192242 if( pDL->pList==0 ) bEof = 1;
192243 }else{
192244 int bDescDoclist = pCsr->bDesc;
192245 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS4];
192246
192247 memset(a, 0, sizeof(a));
192248 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS )((void) (0));
192249 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS )((void) (0));
192250
192251 while( bEof==0 ){
192252 int bMaxSet = 0;
192253 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
192254 int i; /* Used to iterate through tokens */
192255
192256 /* Advance the iterator for each token in the phrase once. */
192257 for(i=0; rc==SQLITE_OK0 && i<p->nToken && bEof==0; i++){
192258 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
192259 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) ){
192260 iMax = a[i].iDocid;
192261 bMaxSet = 1;
192262 }
192263 }
192264 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) )((void) (0));
192265 assert( rc!=SQLITE_OK || bMaxSet )((void) (0));
192266
192267 /* Keep advancing iterators until they all point to the same document */
192268 for(i=0; i<p->nToken; i++){
192269 while( rc==SQLITE_OK0 && bEof==0
192270 && 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
192271 ){
192272 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
192273 if( DOCID_CMP(a[i].iDocid, iMax)((bDescDoclist?-1:1) * (a[i].iDocid>iMax?1:((a[i].iDocid==
iMax)?0:-1)))
>0 ){
192274 iMax = a[i].iDocid;
192275 i = 0;
192276 }
192277 }
192278 }
192279
192280 /* Check if the current entries really are a phrase match */
192281 if( bEof==0 ){
192282 int nList = 0;
192283 int nByte = a[p->nToken-1].nList;
192284 char *aDoclist = sqlite3_malloc64((i64)nByte+FTS3_BUFFER_PADDING8);
192285 if( !aDoclist ) return SQLITE_NOMEM7;
192286 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
192287 memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING8);
192288
192289 for(i=0; i<(p->nToken-1); i++){
192290 if( a[i].bIgnore==0 ){
192291 char *pL = a[i].pList;
192292 char *pR = aDoclist;
192293 char *pOut = aDoclist;
192294 int nDist = p->nToken-1-i;
192295 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
192296 if( res==0 ) break;
192297 nList = (int)(pOut - aDoclist);
192298 }
192299 }
192300 if( i==(p->nToken-1) ){
192301 pDL->iDocid = iMax;
192302 pDL->pList = aDoclist;
192303 pDL->nList = nList;
192304 pDL->bFreeList = 1;
192305 break;
192306 }
192307 sqlite3_free(aDoclist);
192308 }
192309 }
192310 }
192311
192312 *pbEof = bEof;
192313 return rc;
192314}
192315
192316/*
192317** Attempt to move the phrase iterator to point to the next matching docid.
192318** If an error occurs, return an SQLite error code. Otherwise, return
192319** SQLITE_OK.
192320**
192321** If there is no "next" entry and no error occurs, then *pbEof is set to
192322** 1 before returning. Otherwise, if no error occurs and the iterator is
192323** successfully advanced, *pbEof is set to 0.
192324*/
192325static int fts3EvalPhraseNext(
192326 Fts3Cursor *pCsr, /* FTS Cursor handle */
192327 Fts3Phrase *p, /* Phrase object to advance to next docid */
192328 u8 *pbEof /* OUT: Set to 1 if EOF */
192329){
192330 int rc = SQLITE_OK0;
192331 Fts3Doclist *pDL = &p->doclist;
192332 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
192333
192334 if( p->bIncr ){
192335 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
192336 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
192337 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
192338 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
192339 );
192340 pDL->pList = pDL->pNextDocid;
192341 }else{
192342 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
192343 }
192344
192345 return rc;
192346}
192347
192348/*
192349**
192350** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
192351** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
192352** expression. Also the Fts3Expr.bDeferred variable is set to true for any
192353** expressions for which all descendent tokens are deferred.
192354**
192355** If parameter bOptOk is zero, then it is guaranteed that the
192356** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
192357** each phrase in the expression (subject to deferred token processing).
192358** Or, if bOptOk is non-zero, then one or more tokens within the expression
192359** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
192360**
192361** If an error occurs within this function, *pRc is set to an SQLite error
192362** code before returning.
192363*/
192364static void fts3EvalStartReaders(
192365 Fts3Cursor *pCsr, /* FTS Cursor handle */
192366 Fts3Expr *pExpr, /* Expression to initialize phrases in */
192367 int *pRc /* IN/OUT: Error code */
192368){
192369 if( pExpr && SQLITE_OK0==*pRc ){
192370 if( pExpr->eType==FTSQUERY_PHRASE5 ){
192371 int nToken = pExpr->pPhrase->nToken;
192372 if( nToken ){
192373 int i;
192374 for(i=0; i<nToken; i++){
192375 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
192376 }
192377 pExpr->bDeferred = (i==nToken);
192378 }
192379 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
192380 }else{
192381 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
192382 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
192383 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
192384 }
192385 }
192386}
192387
192388/*
192389** An array of the following structures is assembled as part of the process
192390** of selecting tokens to defer before the query starts executing (as part
192391** of the xFilter() method). There is one element in the array for each
192392** token in the FTS expression.
192393**
192394** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
192395** to phrases that are connected only by AND and NEAR operators (not OR or
192396** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
192397** separately. The root of a tokens AND/NEAR cluster is stored in
192398** Fts3TokenAndCost.pRoot.
192399*/
192400typedef struct Fts3TokenAndCost Fts3TokenAndCost;
192401struct Fts3TokenAndCost {
192402 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
192403 int iToken; /* Position of token in phrase */
192404 Fts3PhraseToken *pToken; /* The token itself */
192405 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
192406 int nOvfl; /* Number of overflow pages to load doclist */
192407 int iCol; /* The column the token must match */
192408};
192409
192410/*
192411** This function is used to populate an allocated Fts3TokenAndCost array.
192412**
192413** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
192414** Otherwise, if an error occurs during execution, *pRc is set to an
192415** SQLite error code.
192416*/
192417static void fts3EvalTokenCosts(
192418 Fts3Cursor *pCsr, /* FTS Cursor handle */
192419 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
192420 Fts3Expr *pExpr, /* Expression to consider */
192421 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
192422 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
192423 int *pRc /* IN/OUT: Error code */
192424){
192425 if( *pRc==SQLITE_OK0 ){
192426 if( pExpr->eType==FTSQUERY_PHRASE5 ){
192427 Fts3Phrase *pPhrase = pExpr->pPhrase;
192428 int i;
192429 for(i=0; *pRc==SQLITE_OK0 && i<pPhrase->nToken; i++){
192430 Fts3TokenAndCost *pTC = (*ppTC)++;
192431 pTC->pPhrase = pPhrase;
192432 pTC->iToken = i;
192433 pTC->pRoot = pRoot;
192434 pTC->pToken = &pPhrase->aToken[i];
192435 pTC->iCol = pPhrase->iColumn;
192436 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
192437 }
192438 }else if( pExpr->eType!=FTSQUERY_NOT2 ){
192439 assert( pExpr->eType==FTSQUERY_OR((void) (0))
192440 || pExpr->eType==FTSQUERY_AND((void) (0))
192441 || pExpr->eType==FTSQUERY_NEAR((void) (0))
192442 )((void) (0));
192443 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
192444 if( pExpr->eType==FTSQUERY_OR4 ){
192445 pRoot = pExpr->pLeft;
192446 **ppOr = pRoot;
192447 (*ppOr)++;
192448 }
192449 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
192450 if( pExpr->eType==FTSQUERY_OR4 ){
192451 pRoot = pExpr->pRight;
192452 **ppOr = pRoot;
192453 (*ppOr)++;
192454 }
192455 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
192456 }
192457 }
192458}
192459
192460/*
192461** Determine the average document (row) size in pages. If successful,
192462** write this value to *pnPage and return SQLITE_OK. Otherwise, return
192463** an SQLite error code.
192464**
192465** The average document size in pages is calculated by first calculating
192466** determining the average size in bytes, B. If B is less than the amount
192467** of data that will fit on a single leaf page of an intkey table in
192468** this database, then the average docsize is 1. Otherwise, it is 1 plus
192469** the number of overflow pages consumed by a record B bytes in size.
192470*/
192471static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
192472 int rc = SQLITE_OK0;
192473 if( pCsr->nRowAvg==0 ){
192474 /* The average document size, which is required to calculate the cost
192475 ** of each doclist, has not yet been determined. Read the required
192476 ** data from the %_stat table to calculate it.
192477 **
192478 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
192479 ** varints, where nCol is the number of columns in the FTS3 table.
192480 ** The first varint is the number of documents currently stored in
192481 ** the table. The following nCol varints contain the total amount of
192482 ** data stored in all rows of each column of the table, from left
192483 ** to right.
192484 */
192485 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
192486 sqlite3_stmt *pStmt;
192487 sqlite3_int64 nDoc = 0;
192488 sqlite3_int64 nByte = 0;
192489 const char *pEnd;
192490 const char *a;
192491
192492 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
192493 if( rc!=SQLITE_OK0 ) return rc;
192494 a = sqlite3_column_blob(pStmt, 0);
192495 testcase( a==0 ); /* If %_stat.value set to X'' */
192496 if( a ){
192497 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
192498 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
192499 while( a<pEnd ){
192500 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nByte);
192501 }
192502 }
192503 if( nDoc==0 || nByte==0 ){
192504 sqlite3_reset(pStmt);
192505 return FTS_CORRUPT_VTAB(11 | (1<<8));
192506 }
192507
192508 pCsr->nDoc = nDoc;
192509 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
192510 assert( pCsr->nRowAvg>0 )((void) (0));
192511 rc = sqlite3_reset(pStmt);
192512 }
192513
192514 *pnPage = pCsr->nRowAvg;
192515 return rc;
192516}
192517
192518/*
192519** This function is called to select the tokens (if any) that will be
192520** deferred. The array aTC[] has already been populated when this is
192521** called.
192522**
192523** This function is called once for each AND/NEAR cluster in the
192524** expression. Each invocation determines which tokens to defer within
192525** the cluster with root node pRoot. See comments above the definition
192526** of struct Fts3TokenAndCost for more details.
192527**
192528** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
192529** called on each token to defer. Otherwise, an SQLite error code is
192530** returned.
192531*/
192532static int fts3EvalSelectDeferred(
192533 Fts3Cursor *pCsr, /* FTS Cursor handle */
192534 Fts3Expr *pRoot, /* Consider tokens with this root node */
192535 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
192536 int nTC /* Number of entries in aTC[] */
192537){
192538 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
192539 int nDocSize = 0; /* Number of pages per doc loaded */
192540 int rc = SQLITE_OK0; /* Return code */
192541 int ii; /* Iterator variable for various purposes */
192542 int nOvfl = 0; /* Total overflow pages used by doclists */
192543 int nToken = 0; /* Total number of tokens in cluster */
192544
192545 int nMinEst = 0; /* The minimum count for any phrase so far. */
192546 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
192547
192548 /* Tokens are never deferred for FTS tables created using the content=xxx
192549 ** option. The reason being that it is not guaranteed that the content
192550 ** table actually contains the same data as the index. To prevent this from
192551 ** causing any problems, the deferred token optimization is completely
192552 ** disabled for content=xxx tables. */
192553 if( pTab->zContentTbl ){
192554 return SQLITE_OK0;
192555 }
192556
192557 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
192558 ** associated with the tokens spill onto overflow pages, or if there is
192559 ** only 1 token, exit early. No tokens to defer in this case. */
192560 for(ii=0; ii<nTC; ii++){
192561 if( aTC[ii].pRoot==pRoot ){
192562 nOvfl += aTC[ii].nOvfl;
192563 nToken++;
192564 }
192565 }
192566 if( nOvfl==0 || nToken<2 ) return SQLITE_OK0;
192567
192568 /* Obtain the average docsize (in pages). */
192569 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
192570 assert( rc!=SQLITE_OK || nDocSize>0 )((void) (0));
192571
192572
192573 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
192574 ** of the number of overflow pages that will be loaded by the pager layer
192575 ** to retrieve the entire doclist for the token from the full-text index.
192576 ** Load the doclists for tokens that are either:
192577 **
192578 ** a. The cheapest token in the entire query (i.e. the one visited by the
192579 ** first iteration of this loop), or
192580 **
192581 ** b. Part of a multi-token phrase.
192582 **
192583 ** After each token doclist is loaded, merge it with the others from the
192584 ** same phrase and count the number of documents that the merged doclist
192585 ** contains. Set variable "nMinEst" to the smallest number of documents in
192586 ** any phrase doclist for which 1 or more token doclists have been loaded.
192587 ** Let nOther be the number of other phrases for which it is certain that
192588 ** one or more tokens will not be deferred.
192589 **
192590 ** Then, for each token, defer it if loading the doclist would result in
192591 ** loading N or more overflow pages into memory, where N is computed as:
192592 **
192593 ** (nMinEst + 4^nOther - 1) / (4^nOther)
192594 */
192595 for(ii=0; ii<nToken && rc==SQLITE_OK0; ii++){
192596 int iTC; /* Used to iterate through aTC[] array. */
192597 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
192598
192599 /* Set pTC to point to the cheapest remaining token. */
192600 for(iTC=0; iTC<nTC; iTC++){
192601 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
192602 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
192603 ){
192604 pTC = &aTC[iTC];
192605 }
192606 }
192607 assert( pTC )((void) (0));
192608
192609 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
192610 /* The number of overflow pages to load for this (and therefore all
192611 ** subsequent) tokens is greater than the estimated number of pages
192612 ** that will be loaded if all subsequent tokens are deferred.
192613 */
192614 Fts3PhraseToken *pToken = pTC->pToken;
192615 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
192616 fts3SegReaderCursorFree(pToken->pSegcsr);
192617 pToken->pSegcsr = 0;
192618 }else{
192619 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
192620 ** for-loop. Except, limit the value to 2^24 to prevent it from
192621 ** overflowing the 32-bit integer it is stored in. */
192622 if( ii<12 ) nLoad4 = nLoad4*4;
192623
192624 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
192625 /* Either this is the cheapest token in the entire query, or it is
192626 ** part of a multi-token phrase. Either way, the entire doclist will
192627 ** (eventually) be loaded into memory. It may as well be now. */
192628 Fts3PhraseToken *pToken = pTC->pToken;
192629 int nList = 0;
192630 char *pList = 0;
192631 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
192632 assert( rc==SQLITE_OK || pList==0 )((void) (0));
192633 if( rc==SQLITE_OK0 ){
192634 rc = fts3EvalPhraseMergeToken(
192635 pTab, pTC->pPhrase, pTC->iToken,pList,nList
192636 );
192637 }
192638 if( rc==SQLITE_OK0 ){
192639 int nCount;
192640 nCount = fts3DoclistCountDocids(
192641 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
192642 );
192643 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
192644 }
192645 }
192646 }
192647 pTC->pToken = 0;
192648 }
192649
192650 return rc;
192651}
192652
192653/*
192654** This function is called from within the xFilter method. It initializes
192655** the full-text query currently stored in pCsr->pExpr. To iterate through
192656** the results of a query, the caller does:
192657**
192658** fts3EvalStart(pCsr);
192659** while( 1 ){
192660** fts3EvalNext(pCsr);
192661** if( pCsr->bEof ) break;
192662** ... return row pCsr->iPrevId to the caller ...
192663** }
192664*/
192665static int fts3EvalStart(Fts3Cursor *pCsr){
192666 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
192667 int rc = SQLITE_OK0;
192668 int nToken = 0;
192669 int nOr = 0;
192670
192671 /* Allocate a MultiSegReader for each token in the expression. */
192672 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
192673
192674 /* Determine which, if any, tokens in the expression should be deferred. */
192675#ifndef SQLITE_DISABLE_FTS4_DEFERRED
192676 if( rc==SQLITE_OK0 && nToken>1 && pTab->bFts4 ){
192677 Fts3TokenAndCost *aTC;
192678 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
192679 sizeof(Fts3TokenAndCost) * nToken
192680 + sizeof(Fts3Expr *) * nOr * 2
192681 );
192682
192683 if( !aTC ){
192684 rc = SQLITE_NOMEM7;
192685 }else{
192686 Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken];
192687 int ii;
192688 Fts3TokenAndCost *pTC = aTC;
192689 Fts3Expr **ppOr = apOr;
192690
192691 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
192692 nToken = (int)(pTC-aTC);
192693 nOr = (int)(ppOr-apOr);
192694
192695 if( rc==SQLITE_OK0 ){
192696 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
192697 for(ii=0; rc==SQLITE_OK0 && ii<nOr; ii++){
192698 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
192699 }
192700 }
192701
192702 sqlite3_free(aTC);
192703 }
192704 }
192705#endif
192706
192707 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
192708 return rc;
192709}
192710
192711/*
192712** Invalidate the current position list for phrase pPhrase.
192713*/
192714static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
192715 if( pPhrase->doclist.bFreeList ){
192716 sqlite3_free(pPhrase->doclist.pList);
192717 }
192718 pPhrase->doclist.pList = 0;
192719 pPhrase->doclist.nList = 0;
192720 pPhrase->doclist.bFreeList = 0;
192721}
192722
192723/*
192724** This function is called to edit the position list associated with
192725** the phrase object passed as the fifth argument according to a NEAR
192726** condition. For example:
192727**
192728** abc NEAR/5 "def ghi"
192729**
192730** Parameter nNear is passed the NEAR distance of the expression (5 in
192731** the example above). When this function is called, *paPoslist points to
192732** the position list, and *pnToken is the number of phrase tokens in the
192733** phrase on the other side of the NEAR operator to pPhrase. For example,
192734** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
192735** the position list associated with phrase "abc".
192736**
192737** All positions in the pPhrase position list that are not sufficiently
192738** close to a position in the *paPoslist position list are removed. If this
192739** leaves 0 positions, zero is returned. Otherwise, non-zero.
192740**
192741** Before returning, *paPoslist is set to point to the position lsit
192742** associated with pPhrase. And *pnToken is set to the number of tokens in
192743** pPhrase.
192744*/
192745static int fts3EvalNearTrim(
192746 int nNear, /* NEAR distance. As in "NEAR/nNear". */
192747 char *aTmp, /* Temporary space to use */
192748 char **paPoslist, /* IN/OUT: Position list */
192749 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
192750 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
192751){
192752 int nParam1 = nNear + pPhrase->nToken;
192753 int nParam2 = nNear + *pnToken;
192754 int nNew;
192755 char *p2;
192756 char *pOut;
192757 int res;
192758
192759 assert( pPhrase->doclist.pList )((void) (0));
192760
192761 p2 = pOut = pPhrase->doclist.pList;
192762 res = fts3PoslistNearMerge(
192763 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
192764 );
192765 if( res ){
192766 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
192767 assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 )((void) (0));
192768 if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
192769 assert( pPhrase->doclist.pList[nNew]=='\0' )((void) (0));
192770 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
192771 pPhrase->doclist.nList = nNew;
192772 }
192773 *paPoslist = pPhrase->doclist.pList;
192774 *pnToken = pPhrase->nToken;
192775 }
192776
192777 return res;
192778}
192779
192780/*
192781** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
192782** Otherwise, it advances the expression passed as the second argument to
192783** point to the next matching row in the database. Expressions iterate through
192784** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
192785** or descending if it is non-zero.
192786**
192787** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
192788** successful, the following variables in pExpr are set:
192789**
192790** Fts3Expr.bEof (non-zero if EOF - there is no next row)
192791** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
192792**
192793** If the expression is of type FTSQUERY_PHRASE, and the expression is not
192794** at EOF, then the following variables are populated with the position list
192795** for the phrase for the visited row:
192796**
192797** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
192798** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
192799**
192800** It says above that this function advances the expression to the next
192801** matching row. This is usually true, but there are the following exceptions:
192802**
192803** 1. Deferred tokens are not taken into account. If a phrase consists
192804** entirely of deferred tokens, it is assumed to match every row in
192805** the db. In this case the position-list is not populated at all.
192806**
192807** Or, if a phrase contains one or more deferred tokens and one or
192808** more non-deferred tokens, then the expression is advanced to the
192809** next possible match, considering only non-deferred tokens. In other
192810** words, if the phrase is "A B C", and "B" is deferred, the expression
192811** is advanced to the next row that contains an instance of "A * C",
192812** where "*" may match any single token. The position list in this case
192813** is populated as for "A * C" before returning.
192814**
192815** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
192816** advanced to point to the next row that matches "x AND y".
192817**
192818** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
192819** really a match, taking into account deferred tokens and NEAR operators.
192820*/
192821static void fts3EvalNextRow(
192822 Fts3Cursor *pCsr, /* FTS Cursor handle */
192823 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
192824 int *pRc /* IN/OUT: Error code */
192825){
192826 if( *pRc==SQLITE_OK0 && pExpr->bEof==0 ){
192827 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
192828 pExpr->bStart = 1;
192829
192830 switch( pExpr->eType ){
192831 case FTSQUERY_NEAR1:
192832 case FTSQUERY_AND3: {
192833 Fts3Expr *pLeft = pExpr->pLeft;
192834 Fts3Expr *pRight = pExpr->pRight;
192835 assert( !pLeft->bDeferred || !pRight->bDeferred )((void) (0));
192836
192837 if( pLeft->bDeferred ){
192838 /* LHS is entirely deferred. So we assume it matches every row.
192839 ** Advance the RHS iterator to find the next row visited. */
192840 fts3EvalNextRow(pCsr, pRight, pRc);
192841 pExpr->iDocid = pRight->iDocid;
192842 pExpr->bEof = pRight->bEof;
192843 }else if( pRight->bDeferred ){
192844 /* RHS is entirely deferred. So we assume it matches every row.
192845 ** Advance the LHS iterator to find the next row visited. */
192846 fts3EvalNextRow(pCsr, pLeft, pRc);
192847 pExpr->iDocid = pLeft->iDocid;
192848 pExpr->bEof = pLeft->bEof;
192849 }else{
192850 /* Neither the RHS or LHS are deferred. */
192851 fts3EvalNextRow(pCsr, pLeft, pRc);
192852 fts3EvalNextRow(pCsr, pRight, pRc);
192853 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK0 ){
192854 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
192855 if( iDiff==0 ) break;
192856 if( iDiff<0 ){
192857 fts3EvalNextRow(pCsr, pLeft, pRc);
192858 }else{
192859 fts3EvalNextRow(pCsr, pRight, pRc);
192860 }
192861 }
192862 pExpr->iDocid = pLeft->iDocid;
192863 pExpr->bEof = (pLeft->bEof || pRight->bEof);
192864 if( pExpr->eType==FTSQUERY_NEAR1 && pExpr->bEof ){
192865 assert( pRight->eType==FTSQUERY_PHRASE )((void) (0));
192866 if( pRight->pPhrase->doclist.aAll ){
192867 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
192868 while( *pRc==SQLITE_OK0 && pRight->bEof==0 ){
192869 memset(pDl->pList, 0, pDl->nList);
192870 fts3EvalNextRow(pCsr, pRight, pRc);
192871 }
192872 }
192873 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
192874 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
192875 while( *pRc==SQLITE_OK0 && pLeft->bEof==0 ){
192876 memset(pDl->pList, 0, pDl->nList);
192877 fts3EvalNextRow(pCsr, pLeft, pRc);
192878 }
192879 }
192880 pRight->bEof = pLeft->bEof = 1;
192881 }
192882 }
192883 break;
192884 }
192885
192886 case FTSQUERY_OR4: {
192887 Fts3Expr *pLeft = pExpr->pLeft;
192888 Fts3Expr *pRight = pExpr->pRight;
192889 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
192890
192891 assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
192892 assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
192893
192894 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
192895 fts3EvalNextRow(pCsr, pLeft, pRc);
192896 }else if( pLeft->bEof || iCmp>0 ){
192897 fts3EvalNextRow(pCsr, pRight, pRc);
192898 }else{
192899 fts3EvalNextRow(pCsr, pLeft, pRc);
192900 fts3EvalNextRow(pCsr, pRight, pRc);
192901 }
192902
192903 pExpr->bEof = (pLeft->bEof && pRight->bEof);
192904 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
;
192905 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
192906 pExpr->iDocid = pLeft->iDocid;
192907 }else{
192908 pExpr->iDocid = pRight->iDocid;
192909 }
192910
192911 break;
192912 }
192913
192914 case FTSQUERY_NOT2: {
192915 Fts3Expr *pLeft = pExpr->pLeft;
192916 Fts3Expr *pRight = pExpr->pRight;
192917
192918 if( pRight->bStart==0 ){
192919 fts3EvalNextRow(pCsr, pRight, pRc);
192920 assert( *pRc!=SQLITE_OK || pRight->bStart )((void) (0));
192921 }
192922
192923 fts3EvalNextRow(pCsr, pLeft, pRc);
192924 if( pLeft->bEof==0 ){
192925 while( !*pRc
192926 && !pRight->bEof
192927 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)((bDescDoclist?-1:1) * (pLeft->iDocid>pRight->iDocid
?1:((pLeft->iDocid==pRight->iDocid)?0:-1)))
>0
192928 ){
192929 fts3EvalNextRow(pCsr, pRight, pRc);
192930 }
192931 }
192932 pExpr->iDocid = pLeft->iDocid;
192933 pExpr->bEof = pLeft->bEof;
192934 break;
192935 }
192936
192937 default: {
192938 Fts3Phrase *pPhrase = pExpr->pPhrase;
192939 fts3EvalInvalidatePoslist(pPhrase);
192940 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
192941 pExpr->iDocid = pPhrase->doclist.iDocid;
192942 break;
192943 }
192944 }
192945 }
192946}
192947
192948/*
192949** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
192950** cluster, then this function returns 1 immediately.
192951**
192952** Otherwise, it checks if the current row really does match the NEAR
192953** expression, using the data currently stored in the position lists
192954** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
192955**
192956** If the current row is a match, the position list associated with each
192957** phrase in the NEAR expression is edited in place to contain only those
192958** phrase instances sufficiently close to their peers to satisfy all NEAR
192959** constraints. In this case it returns 1. If the NEAR expression does not
192960** match the current row, 0 is returned. The position lists may or may not
192961** be edited if 0 is returned.
192962*/
192963static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
192964 int res = 1;
192965
192966 /* The following block runs if pExpr is the root of a NEAR query.
192967 ** For example, the query:
192968 **
192969 ** "w" NEAR "x" NEAR "y" NEAR "z"
192970 **
192971 ** which is represented in tree form as:
192972 **
192973 ** |
192974 ** +--NEAR--+ <-- root of NEAR query
192975 ** | |
192976 ** +--NEAR--+ "z"
192977 ** | |
192978 ** +--NEAR--+ "y"
192979 ** | |
192980 ** "w" "x"
192981 **
192982 ** The right-hand child of a NEAR node is always a phrase. The
192983 ** left-hand child may be either a phrase or a NEAR node. There are
192984 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
192985 */
192986 if( *pRc==SQLITE_OK0
192987 && pExpr->eType==FTSQUERY_NEAR1
192988 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR1)
192989 ){
192990 Fts3Expr *p;
192991 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
192992 char *aTmp; /* Temp space for PoslistNearMerge() */
192993
192994 /* Allocate temporary working space. */
192995 for(p=pExpr; p->pLeft; p=p->pLeft){
192996 assert( p->pRight->pPhrase->doclist.nList>0 )((void) (0));
192997 nTmp += p->pRight->pPhrase->doclist.nList;
192998 }
192999 nTmp += p->pPhrase->doclist.nList;
193000 aTmp = sqlite3_malloc64(nTmp*2 + FTS3_VARINT_MAX10);
193001 if( !aTmp ){
193002 *pRc = SQLITE_NOMEM7;
193003 res = 0;
193004 }else{
193005 char *aPoslist = p->pPhrase->doclist.pList;
193006 int nToken = p->pPhrase->nToken;
193007
193008 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR1; p=p->pParent){
193009 Fts3Phrase *pPhrase = p->pRight->pPhrase;
193010 int nNear = p->nNear;
193011 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
193012 }
193013
193014 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
193015 nToken = pExpr->pRight->pPhrase->nToken;
193016 for(p=pExpr->pLeft; p && res; p=p->pLeft){
193017 int nNear;
193018 Fts3Phrase *pPhrase;
193019 assert( p->pParent && p->pParent->pLeft==p )((void) (0));
193020 nNear = p->pParent->nNear;
193021 pPhrase = (
193022 p->eType==FTSQUERY_NEAR1 ? p->pRight->pPhrase : p->pPhrase
193023 );
193024 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
193025 }
193026 }
193027
193028 sqlite3_free(aTmp);
193029 }
193030
193031 return res;
193032}
193033
193034/*
193035** This function is a helper function for sqlite3Fts3EvalTestDeferred().
193036** Assuming no error occurs or has occurred, It returns non-zero if the
193037** expression passed as the second argument matches the row that pCsr
193038** currently points to, or zero if it does not.
193039**
193040** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
193041** If an error occurs during execution of this function, *pRc is set to
193042** the appropriate SQLite error code. In this case the returned value is
193043** undefined.
193044*/
193045static int fts3EvalTestExpr(
193046 Fts3Cursor *pCsr, /* FTS cursor handle */
193047 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
193048 int *pRc /* IN/OUT: Error code */
193049){
193050 int bHit = 1; /* Return value */
193051 if( *pRc==SQLITE_OK0 ){
193052 switch( pExpr->eType ){
193053 case FTSQUERY_NEAR1:
193054 case FTSQUERY_AND3:
193055 bHit = (
193056 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
193057 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
193058 && fts3EvalNearTest(pExpr, pRc)
193059 );
193060
193061 /* If the NEAR expression does not match any rows, zero the doclist for
193062 ** all phrases involved in the NEAR. This is because the snippet(),
193063 ** offsets() and matchinfo() functions are not supposed to recognize
193064 ** any instances of phrases that are part of unmatched NEAR queries.
193065 ** For example if this expression:
193066 **
193067 ** ... MATCH 'a OR (b NEAR c)'
193068 **
193069 ** is matched against a row containing:
193070 **
193071 ** 'a b d e'
193072 **
193073 ** then any snippet() should ony highlight the "a" term, not the "b"
193074 ** (as "b" is part of a non-matching NEAR clause).
193075 */
193076 if( bHit==0
193077 && pExpr->eType==FTSQUERY_NEAR1
193078 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR1)
193079 ){
193080 Fts3Expr *p;
193081 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
193082 if( p->pRight->iDocid==pCsr->iPrevId ){
193083 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
193084 }
193085 }
193086 if( p->iDocid==pCsr->iPrevId ){
193087 fts3EvalInvalidatePoslist(p->pPhrase);
193088 }
193089 }
193090
193091 break;
193092
193093 case FTSQUERY_OR4: {
193094 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
193095 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
193096 bHit = bHit1 || bHit2;
193097 break;
193098 }
193099
193100 case FTSQUERY_NOT2:
193101 bHit = (
193102 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
193103 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
193104 );
193105 break;
193106
193107 default: {
193108#ifndef SQLITE_DISABLE_FTS4_DEFERRED
193109 if( pCsr->pDeferred && (pExpr->bDeferred || (
193110 pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList
193111 ))){
193112 Fts3Phrase *pPhrase = pExpr->pPhrase;
193113 if( pExpr->bDeferred ){
193114 fts3EvalInvalidatePoslist(pPhrase);
193115 }
193116 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
193117 bHit = (pPhrase->doclist.pList!=0);
193118 pExpr->iDocid = pCsr->iPrevId;
193119 }else
193120#endif
193121 {
193122 bHit = (
193123 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
193124 && pExpr->pPhrase->doclist.nList>0
193125 );
193126 }
193127 break;
193128 }
193129 }
193130 }
193131 return bHit;
193132}
193133
193134/*
193135** This function is called as the second part of each xNext operation when
193136** iterating through the results of a full-text query. At this point the
193137** cursor points to a row that matches the query expression, with the
193138** following caveats:
193139**
193140** * Up until this point, "NEAR" operators in the expression have been
193141** treated as "AND".
193142**
193143** * Deferred tokens have not yet been considered.
193144**
193145** If *pRc is not SQLITE_OK when this function is called, it immediately
193146** returns 0. Otherwise, it tests whether or not after considering NEAR
193147** operators and deferred tokens the current row is still a match for the
193148** expression. It returns 1 if both of the following are true:
193149**
193150** 1. *pRc is SQLITE_OK when this function returns, and
193151**
193152** 2. After scanning the current FTS table row for the deferred tokens,
193153** it is determined that the row does *not* match the query.
193154**
193155** Or, if no error occurs and it seems the current row does match the FTS
193156** query, return 0.
193157*/
193158SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
193159 int rc = *pRc;
193160 int bMiss = 0;
193161 if( rc==SQLITE_OK0 ){
193162
193163 /* If there are one or more deferred tokens, load the current row into
193164 ** memory and scan it to determine the position list for each deferred
193165 ** token. Then, see if this row is really a match, considering deferred
193166 ** tokens and NEAR operators (neither of which were taken into account
193167 ** earlier, by fts3EvalNextRow()).
193168 */
193169 if( pCsr->pDeferred ){
193170 rc = fts3CursorSeek(0, pCsr);
193171 if( rc==SQLITE_OK0 ){
193172 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
193173 }
193174 }
193175 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
193176
193177 /* Free the position-lists accumulated for each deferred token above. */
193178 sqlite3Fts3FreeDeferredDoclists(pCsr);
193179 *pRc = rc;
193180 }
193181 return (rc==SQLITE_OK0 && bMiss);
193182}
193183
193184/*
193185** Advance to the next document that matches the FTS expression in
193186** Fts3Cursor.pExpr.
193187*/
193188static int fts3EvalNext(Fts3Cursor *pCsr){
193189 int rc = SQLITE_OK0; /* Return Code */
193190 Fts3Expr *pExpr = pCsr->pExpr;
193191 assert( pCsr->isEof==0 )((void) (0));
193192 if( pExpr==0 ){
193193 pCsr->isEof = 1;
193194 }else{
193195 do {
193196 if( pCsr->isRequireSeek==0 ){
193197 sqlite3_reset(pCsr->pStmt);
193198 }
193199 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
193200 fts3EvalNextRow(pCsr, pExpr, &rc);
193201 pCsr->isEof = pExpr->bEof;
193202 pCsr->isRequireSeek = 1;
193203 pCsr->isMatchinfoNeeded = 1;
193204 pCsr->iPrevId = pExpr->iDocid;
193205 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
193206 }
193207
193208 /* Check if the cursor is past the end of the docid range specified
193209 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
193210 if( rc==SQLITE_OK0 && (
193211 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
193212 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
193213 )){
193214 pCsr->isEof = 1;
193215 }
193216
193217 return rc;
193218}
193219
193220/*
193221** Restart interation for expression pExpr so that the next call to
193222** fts3EvalNext() visits the first row. Do not allow incremental
193223** loading or merging of phrase doclists for this iteration.
193224**
193225** If *pRc is other than SQLITE_OK when this function is called, it is
193226** a no-op. If an error occurs within this function, *pRc is set to an
193227** SQLite error code before returning.
193228*/
193229static void fts3EvalRestart(
193230 Fts3Cursor *pCsr,
193231 Fts3Expr *pExpr,
193232 int *pRc
193233){
193234 if( pExpr && *pRc==SQLITE_OK0 ){
193235 Fts3Phrase *pPhrase = pExpr->pPhrase;
193236
193237 if( pPhrase ){
193238 fts3EvalInvalidatePoslist(pPhrase);
193239 if( pPhrase->bIncr ){
193240 int i;
193241 for(i=0; i<pPhrase->nToken; i++){
193242 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
193243 assert( pToken->pDeferred==0 )((void) (0));
193244 if( pToken->pSegcsr ){
193245 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
193246 }
193247 }
193248 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
193249 }
193250 pPhrase->doclist.pNextDocid = 0;
193251 pPhrase->doclist.iDocid = 0;
193252 pPhrase->pOrPoslist = 0;
193253 }
193254
193255 pExpr->iDocid = 0;
193256 pExpr->bEof = 0;
193257 pExpr->bStart = 0;
193258
193259 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
193260 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
193261 }
193262}
193263
193264/*
193265** After allocating the Fts3Expr.aMI[] array for each phrase in the
193266** expression rooted at pExpr, the cursor iterates through all rows matched
193267** by pExpr, calling this function for each row. This function increments
193268** the values in Fts3Expr.aMI[] according to the position-list currently
193269** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
193270** expression nodes.
193271*/
193272static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
193273 if( pExpr ){
193274 Fts3Phrase *pPhrase = pExpr->pPhrase;
193275 if( pPhrase && pPhrase->doclist.pList ){
193276 int iCol = 0;
193277 char *p = pPhrase->doclist.pList;
193278
193279 do{
193280 u8 c = 0;
193281 int iCnt = 0;
193282 while( 0xFE & (*p | c) ){
193283 if( (c&0x80)==0 ) iCnt++;
193284 c = *p++ & 0x80;
193285 }
193286
193287 /* aMI[iCol*3 + 1] = Number of occurrences
193288 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
193289 */
193290 pExpr->aMI[iCol*3 + 1] += iCnt;
193291 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
193292 if( *p==0x00 ) break;
193293 p++;
193294 p += fts3GetVarint32(p, &iCol)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, &iCol)
: (*&iCol=*(u8*)(p), 1) )
;
193295 }while( iCol<nCol );
193296 }
193297
193298 fts3EvalUpdateCounts(pExpr->pLeft, nCol);
193299 fts3EvalUpdateCounts(pExpr->pRight, nCol);
193300 }
193301}
193302
193303/*
193304** This is an sqlite3Fts3ExprIterate() callback. If the Fts3Expr.aMI[] array
193305** has not yet been allocated, allocate and zero it. Otherwise, just zero
193306** it.
193307*/
193308static int fts3AllocateMSI(Fts3Expr *pExpr, int iPhrase, void *pCtx){
193309 Fts3Table *pTab = (Fts3Table*)pCtx;
193310 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
193311 if( pExpr->aMI==0 ){
193312 pExpr->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
193313 if( pExpr->aMI==0 ) return SQLITE_NOMEM7;
193314 }
193315 memset(pExpr->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
193316 return SQLITE_OK0;
193317}
193318
193319/*
193320** Expression pExpr must be of type FTSQUERY_PHRASE.
193321**
193322** If it is not already allocated and populated, this function allocates and
193323** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
193324** of a NEAR expression, then it also allocates and populates the same array
193325** for all other phrases that are part of the NEAR expression.
193326**
193327** SQLITE_OK is returned if the aMI[] array is successfully allocated and
193328** populated. Otherwise, if an error occurs, an SQLite error code is returned.
193329*/
193330static int fts3EvalGatherStats(
193331 Fts3Cursor *pCsr, /* Cursor object */
193332 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
193333){
193334 int rc = SQLITE_OK0; /* Return code */
193335
193336 assert( pExpr->eType==FTSQUERY_PHRASE )((void) (0));
193337 if( pExpr->aMI==0 ){
193338 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193339 Fts3Expr *pRoot; /* Root of NEAR expression */
193340
193341 sqlite3_int64 iPrevId = pCsr->iPrevId;
193342 sqlite3_int64 iDocid;
193343 u8 bEof;
193344
193345 /* Find the root of the NEAR expression */
193346 pRoot = pExpr;
193347 while( pRoot->pParent
193348 && (pRoot->pParent->eType==FTSQUERY_NEAR1 || pRoot->bDeferred)
193349 ){
193350 pRoot = pRoot->pParent;
193351 }
193352 iDocid = pRoot->iDocid;
193353 bEof = pRoot->bEof;
193354 assert( pRoot->bStart )((void) (0));
193355
193356 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
193357 rc = sqlite3Fts3ExprIterate(pRoot, fts3AllocateMSI, (void*)pTab);
193358 if( rc!=SQLITE_OK0 ) return rc;
193359 fts3EvalRestart(pCsr, pRoot, &rc);
193360
193361 while( pCsr->isEof==0 && rc==SQLITE_OK0 ){
193362
193363 do {
193364 /* Ensure the %_content statement is reset. */
193365 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
193366 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
193367
193368 /* Advance to the next document */
193369 fts3EvalNextRow(pCsr, pRoot, &rc);
193370 pCsr->isEof = pRoot->bEof;
193371 pCsr->isRequireSeek = 1;
193372 pCsr->isMatchinfoNeeded = 1;
193373 pCsr->iPrevId = pRoot->iDocid;
193374 }while( pCsr->isEof==0
193375 && pRoot->eType==FTSQUERY_NEAR1
193376 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
193377 );
193378
193379 if( rc==SQLITE_OK0 && pCsr->isEof==0 ){
193380 fts3EvalUpdateCounts(pRoot, pTab->nColumn);
193381 }
193382 }
193383
193384 pCsr->isEof = 0;
193385 pCsr->iPrevId = iPrevId;
193386
193387 if( bEof ){
193388 pRoot->bEof = bEof;
193389 }else{
193390 /* Caution: pRoot may iterate through docids in ascending or descending
193391 ** order. For this reason, even though it seems more defensive, the
193392 ** do loop can not be written:
193393 **
193394 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
193395 */
193396 fts3EvalRestart(pCsr, pRoot, &rc);
193397 do {
193398 fts3EvalNextRow(pCsr, pRoot, &rc);
193399 assert_fts3_nc( pRoot->bEof==0 )((void) (0));
193400 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
193401 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK0 );
193402 }
193403 }
193404 return rc;
193405}
193406
193407/*
193408** This function is used by the matchinfo() module to query a phrase
193409** expression node for the following information:
193410**
193411** 1. The total number of occurrences of the phrase in each column of
193412** the FTS table (considering all rows), and
193413**
193414** 2. For each column, the number of rows in the table for which the
193415** column contains at least one instance of the phrase.
193416**
193417** If no error occurs, SQLITE_OK is returned and the values for each column
193418** written into the array aiOut as follows:
193419**
193420** aiOut[iCol*3 + 1] = Number of occurrences
193421** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
193422**
193423** Caveats:
193424**
193425** * If a phrase consists entirely of deferred tokens, then all output
193426** values are set to the number of documents in the table. In other
193427** words we assume that very common tokens occur exactly once in each
193428** column of each row of the table.
193429**
193430** * If a phrase contains some deferred tokens (and some non-deferred
193431** tokens), count the potential occurrence identified by considering
193432** the non-deferred tokens instead of actual phrase occurrences.
193433**
193434** * If the phrase is part of a NEAR expression, then only phrase instances
193435** that meet the NEAR constraint are included in the counts.
193436*/
193437SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(
193438 Fts3Cursor *pCsr, /* FTS cursor handle */
193439 Fts3Expr *pExpr, /* Phrase expression */
193440 u32 *aiOut /* Array to write results into (see above) */
193441){
193442 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193443 int rc = SQLITE_OK0;
193444 int iCol;
193445
193446 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR1 ){
193447 assert( pCsr->nDoc>0 )((void) (0));
193448 for(iCol=0; iCol<pTab->nColumn; iCol++){
193449 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
193450 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
193451 }
193452 }else{
193453 rc = fts3EvalGatherStats(pCsr, pExpr);
193454 if( rc==SQLITE_OK0 ){
193455 assert( pExpr->aMI )((void) (0));
193456 for(iCol=0; iCol<pTab->nColumn; iCol++){
193457 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
193458 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
193459 }
193460 }
193461 }
193462
193463 return rc;
193464}
193465
193466/*
193467** The expression pExpr passed as the second argument to this function
193468** must be of type FTSQUERY_PHRASE.
193469**
193470** The returned value is either NULL or a pointer to a buffer containing
193471** a position-list indicating the occurrences of the phrase in column iCol
193472** of the current row.
193473**
193474** More specifically, the returned buffer contains 1 varint for each
193475** occurrence of the phrase in the column, stored using the normal (delta+2)
193476** compression and is terminated by either an 0x01 or 0x00 byte. For example,
193477** if the requested column contains "a b X c d X X" and the position-list
193478** for 'X' is requested, the buffer returned may contain:
193479**
193480** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
193481**
193482** This function works regardless of whether or not the phrase is deferred,
193483** incremental, or neither.
193484*/
193485SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(
193486 Fts3Cursor *pCsr, /* FTS3 cursor object */
193487 Fts3Expr *pExpr, /* Phrase to return doclist for */
193488 int iCol, /* Column to return position list for */
193489 char **ppOut /* OUT: Pointer to position list */
193490){
193491 Fts3Phrase *pPhrase = pExpr->pPhrase;
193492 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
193493 char *pIter;
193494 int iThis;
193495 sqlite3_int64 iDocid;
193496
193497 /* If this phrase is applies specifically to some column other than
193498 ** column iCol, return a NULL pointer. */
193499 *ppOut = 0;
193500 assert( iCol>=0 && iCol<pTab->nColumn )((void) (0));
193501 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
193502 return SQLITE_OK0;
193503 }
193504
193505 iDocid = pExpr->iDocid;
193506 pIter = pPhrase->doclist.pList;
193507 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
193508 int rc = SQLITE_OK0;
193509 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
193510 int bOr = 0;
193511 u8 bTreeEof = 0;
193512 Fts3Expr *p; /* Used to iterate from pExpr to root */
193513 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
193514 Fts3Expr *pRun; /* Closest non-deferred ancestor of pNear */
193515 int bMatch;
193516
193517 /* Check if this phrase descends from an OR expression node. If not,
193518 ** return NULL. Otherwise, the entry that corresponds to docid
193519 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
193520 ** tree that the node is part of has been marked as EOF, but the node
193521 ** itself is not EOF, then it may point to an earlier entry. */
193522 pNear = pExpr;
193523 for(p=pExpr->pParent; p; p=p->pParent){
193524 if( p->eType==FTSQUERY_OR4 ) bOr = 1;
193525 if( p->eType==FTSQUERY_NEAR1 ) pNear = p;
193526 if( p->bEof ) bTreeEof = 1;
193527 }
193528 if( bOr==0 ) return SQLITE_OK0;
193529 pRun = pNear;
193530 while( pRun->bDeferred ){
193531 assert( pRun->pParent )((void) (0));
193532 pRun = pRun->pParent;
193533 }
193534
193535 /* This is the descendent of an OR node. In this case we cannot use
193536 ** an incremental phrase. Load the entire doclist for the phrase
193537 ** into memory in this case. */
193538 if( pPhrase->bIncr ){
193539 int bEofSave = pRun->bEof;
193540 fts3EvalRestart(pCsr, pRun, &rc);
193541 while( rc==SQLITE_OK0 && !pRun->bEof ){
193542 fts3EvalNextRow(pCsr, pRun, &rc);
193543 if( bEofSave==0 && pRun->iDocid==iDocid ) break;
193544 }
193545 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 )((void) (0));
193546 if( rc==SQLITE_OK0 && pRun->bEof!=bEofSave ){
193547 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
193548 }
193549 }
193550 if( bTreeEof ){
193551 while( rc==SQLITE_OK0 && !pRun->bEof ){
193552 fts3EvalNextRow(pCsr, pRun, &rc);
193553 }
193554 }
193555 if( rc!=SQLITE_OK0 ) return rc;
193556
193557 bMatch = 1;
193558 for(p=pNear; p; p=p->pLeft){
193559 u8 bEof = 0;
193560 Fts3Expr *pTest = p;
193561 Fts3Phrase *pPh;
193562 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE )((void) (0));
193563 if( pTest->eType==FTSQUERY_NEAR1 ) pTest = pTest->pRight;
193564 assert( pTest->eType==FTSQUERY_PHRASE )((void) (0));
193565 pPh = pTest->pPhrase;
193566
193567 pIter = pPh->pOrPoslist;
193568 iDocid = pPh->iOrDocid;
193569 if( pCsr->bDesc==bDescDoclist ){
193570 bEof = !pPh->doclist.nAll ||
193571 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
193572 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)((bDescDoclist?-1:1) * (iDocid>pCsr->iPrevId?1:((iDocid
==pCsr->iPrevId)?0:-1)))
<0 ) && bEof==0 ){
193573 sqlite3Fts3DoclistNext(
193574 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
193575 &pIter, &iDocid, &bEof
193576 );
193577 }
193578 }else{
193579 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
193580 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)((bDescDoclist?-1:1) * (iDocid>pCsr->iPrevId?1:((iDocid
==pCsr->iPrevId)?0:-1)))
>0 ) && bEof==0 ){
193581 int dummy;
193582 sqlite3Fts3DoclistPrev(
193583 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
193584 &pIter, &iDocid, &dummy, &bEof
193585 );
193586 }
193587 }
193588 pPh->pOrPoslist = pIter;
193589 pPh->iOrDocid = iDocid;
193590 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
193591 }
193592
193593 if( bMatch ){
193594 pIter = pPhrase->pOrPoslist;
193595 }else{
193596 pIter = 0;
193597 }
193598 }
193599 if( pIter==0 ) return SQLITE_OK0;
193600
193601 if( *pIter==0x01 ){
193602 pIter++;
193603 pIter += fts3GetVarint32(pIter, &iThis)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iThis) : (*&iThis=*(u8*)(pIter), 1) )
;
193604 }else{
193605 iThis = 0;
193606 }
193607 while( iThis<iCol ){
193608 fts3ColumnlistCopy(0, &pIter);
193609 if( *pIter==0x00 ) return SQLITE_OK0;
193610 pIter++;
193611 pIter += fts3GetVarint32(pIter, &iThis)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iThis) : (*&iThis=*(u8*)(pIter), 1) )
;
193612 }
193613 if( *pIter==0x00 ){
193614 pIter = 0;
193615 }
193616
193617 *ppOut = ((iCol==iThis)?pIter:0);
193618 return SQLITE_OK0;
193619}
193620
193621/*
193622** Free all components of the Fts3Phrase structure that were allocated by
193623** the eval module. Specifically, this means to free:
193624**
193625** * the contents of pPhrase->doclist, and
193626** * any Fts3MultiSegReader objects held by phrase tokens.
193627*/
193628SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
193629 if( pPhrase ){
193630 int i;
193631 sqlite3_free(pPhrase->doclist.aAll);
193632 fts3EvalInvalidatePoslist(pPhrase);
193633 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
193634 for(i=0; i<pPhrase->nToken; i++){
193635 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
193636 pPhrase->aToken[i].pSegcsr = 0;
193637 }
193638 }
193639}
193640
193641
193642/*
193643** Return SQLITE_CORRUPT_VTAB.
193644*/
193645#ifdef SQLITE_DEBUG
193646SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(){
193647 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
193648}
193649#endif
193650
193651#if !SQLITE_CORE1
193652/*
193653** Initialize API pointer table, if required.
193654*/
193655#ifdef _WIN32
193656__declspec(dllexport)
193657#endif
193658SQLITE_API int sqlite3_fts3_init(
193659 sqlite3 *db,
193660 char **pzErrMsg,
193661 const sqlite3_api_routines *pApi
193662){
193663 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
193664 return sqlite3Fts3Init(db);
193665}
193666#endif
193667
193668#endif
193669
193670/************** End of fts3.c ************************************************/
193671/************** Begin file fts3_aux.c ****************************************/
193672/*
193673** 2011 Jan 27
193674**
193675** The author disclaims copyright to this source code. In place of
193676** a legal notice, here is a blessing:
193677**
193678** May you do good and not evil.
193679** May you find forgiveness for yourself and forgive others.
193680** May you share freely, never taking more than you give.
193681**
193682******************************************************************************
193683**
193684*/
193685/* #include "fts3Int.h" */
193686#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
193687
193688/* #include <string.h> */
193689/* #include <assert.h> */
193690
193691typedef struct Fts3auxTable Fts3auxTable;
193692typedef struct Fts3auxCursor Fts3auxCursor;
193693
193694struct Fts3auxTable {
193695 sqlite3_vtab base; /* Base class used by SQLite core */
193696 Fts3Table *pFts3Tab;
193697};
193698
193699struct Fts3auxCursor {
193700 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
193701 Fts3MultiSegReader csr; /* Must be right after "base" */
193702 Fts3SegFilter filter;
193703 char *zStop;
193704 int nStop; /* Byte-length of string zStop */
193705 int iLangid; /* Language id to query */
193706 int isEof; /* True if cursor is at EOF */
193707 sqlite3_int64 iRowid; /* Current rowid */
193708
193709 int iCol; /* Current value of 'col' column */
193710 int nStat; /* Size of aStat[] array */
193711 struct Fts3auxColstats {
193712 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
193713 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
193714 } *aStat;
193715};
193716
193717/*
193718** Schema of the terms table.
193719*/
193720#define FTS3_AUX_SCHEMA"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)" \
193721 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
193722
193723/*
193724** This function does all the work for both the xConnect and xCreate methods.
193725** These tables have no persistent representation of their own, so xConnect
193726** and xCreate are identical operations.
193727*/
193728static int fts3auxConnectMethod(
193729 sqlite3 *db, /* Database connection */
193730 void *pUnused, /* Unused */
193731 int argc, /* Number of elements in argv array */
193732 const char * const *argv, /* xCreate/xConnect argument array */
193733 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
193734 char **pzErr /* OUT: sqlite3_malloc'd error message */
193735){
193736 char const *zDb; /* Name of database (e.g. "main") */
193737 char const *zFts3; /* Name of fts3 table */
193738 int nDb; /* Result of strlen(zDb) */
193739 int nFts3; /* Result of strlen(zFts3) */
193740 sqlite3_int64 nByte; /* Bytes of space to allocate here */
193741 int rc; /* value returned by declare_vtab() */
193742 Fts3auxTable *p; /* Virtual table object to return */
193743
193744 UNUSED_PARAMETER(pUnused)(void)(pUnused);
193745
193746 /* The user should invoke this in one of two forms:
193747 **
193748 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
193749 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
193750 */
193751 if( argc!=4 && argc!=5 ) goto bad_args;
193752
193753 zDb = argv[1];
193754 nDb = (int)strlen(zDb);
193755 if( argc==5 ){
193756 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
193757 zDb = argv[3];
193758 nDb = (int)strlen(zDb);
193759 zFts3 = argv[4];
193760 }else{
193761 goto bad_args;
193762 }
193763 }else{
193764 zFts3 = argv[3];
193765 }
193766 nFts3 = (int)strlen(zFts3);
193767
193768 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA"CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)");
193769 if( rc!=SQLITE_OK0 ) return rc;
193770
193771 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
193772 p = (Fts3auxTable *)sqlite3_malloc64(nByte);
193773 if( !p ) return SQLITE_NOMEM7;
193774 memset(p, 0, nByte);
193775
193776 p->pFts3Tab = (Fts3Table *)&p[1];
193777 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
193778 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
193779 p->pFts3Tab->db = db;
193780 p->pFts3Tab->nIndex = 1;
193781
193782 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
193783 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
193784 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
193785
193786 *ppVtab = (sqlite3_vtab *)p;
193787 return SQLITE_OK0;
193788
193789 bad_args:
193790 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
193791 return SQLITE_ERROR1;
193792}
193793
193794/*
193795** This function does the work for both the xDisconnect and xDestroy methods.
193796** These tables have no persistent representation of their own, so xDisconnect
193797** and xDestroy are identical operations.
193798*/
193799static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
193800 Fts3auxTable *p = (Fts3auxTable *)pVtab;
193801 Fts3Table *pFts3 = p->pFts3Tab;
193802 int i;
193803
193804 /* Free any prepared statements held */
193805 for(i=0; i<SizeofArray(pFts3->aStmt)((int)(sizeof(pFts3->aStmt)/sizeof(pFts3->aStmt[0]))); i++){
193806 sqlite3_finalize(pFts3->aStmt[i]);
193807 }
193808 sqlite3_free(pFts3->zSegmentsTbl);
193809 sqlite3_free(p);
193810 return SQLITE_OK0;
193811}
193812
193813#define FTS4AUX_EQ_CONSTRAINT1 1
193814#define FTS4AUX_GE_CONSTRAINT2 2
193815#define FTS4AUX_LE_CONSTRAINT4 4
193816
193817/*
193818** xBestIndex - Analyze a WHERE and ORDER BY clause.
193819*/
193820static int fts3auxBestIndexMethod(
193821 sqlite3_vtab *pVTab,
193822 sqlite3_index_info *pInfo
193823){
193824 int i;
193825 int iEq = -1;
193826 int iGe = -1;
193827 int iLe = -1;
193828 int iLangid = -1;
193829 int iNext = 1; /* Next free argvIndex value */
193830
193831 UNUSED_PARAMETER(pVTab)(void)(pVTab);
193832
193833 /* This vtab delivers always results in "ORDER BY term ASC" order. */
193834 if( pInfo->nOrderBy==1
193835 && pInfo->aOrderBy[0].iColumn==0
193836 && pInfo->aOrderBy[0].desc==0
193837 ){
193838 pInfo->orderByConsumed = 1;
193839 }
193840
193841 /* Search for equality and range constraints on the "term" column.
193842 ** And equality constraints on the hidden "languageid" column. */
193843 for(i=0; i<pInfo->nConstraint; i++){
193844 if( pInfo->aConstraint[i].usable ){
193845 int op = pInfo->aConstraint[i].op;
193846 int iCol = pInfo->aConstraint[i].iColumn;
193847
193848 if( iCol==0 ){
193849 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iEq = i;
193850 if( op==SQLITE_INDEX_CONSTRAINT_LT16 ) iLe = i;
193851 if( op==SQLITE_INDEX_CONSTRAINT_LE8 ) iLe = i;
193852 if( op==SQLITE_INDEX_CONSTRAINT_GT4 ) iGe = i;
193853 if( op==SQLITE_INDEX_CONSTRAINT_GE32 ) iGe = i;
193854 }
193855 if( iCol==4 ){
193856 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iLangid = i;
193857 }
193858 }
193859 }
193860
193861 if( iEq>=0 ){
193862 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT1;
193863 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
193864 pInfo->estimatedCost = 5;
193865 }else{
193866 pInfo->idxNum = 0;
193867 pInfo->estimatedCost = 20000;
193868 if( iGe>=0 ){
193869 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT2;
193870 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
193871 pInfo->estimatedCost /= 2;
193872 }
193873 if( iLe>=0 ){
193874 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT4;
193875 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
193876 pInfo->estimatedCost /= 2;
193877 }
193878 }
193879 if( iLangid>=0 ){
193880 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
193881 pInfo->estimatedCost--;
193882 }
193883
193884 return SQLITE_OK0;
193885}
193886
193887/*
193888** xOpen - Open a cursor.
193889*/
193890static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
193891 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
193892
193893 UNUSED_PARAMETER(pVTab)(void)(pVTab);
193894
193895 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
193896 if( !pCsr ) return SQLITE_NOMEM7;
193897 memset(pCsr, 0, sizeof(Fts3auxCursor));
193898
193899 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
193900 return SQLITE_OK0;
193901}
193902
193903/*
193904** xClose - Close a cursor.
193905*/
193906static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
193907 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
193908 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
193909
193910 sqlite3Fts3SegmentsClose(pFts3);
193911 sqlite3Fts3SegReaderFinish(&pCsr->csr);
193912 sqlite3_free((void *)pCsr->filter.zTerm);
193913 sqlite3_free(pCsr->zStop);
193914 sqlite3_free(pCsr->aStat);
193915 sqlite3_free(pCsr);
193916 return SQLITE_OK0;
193917}
193918
193919static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
193920 if( nSize>pCsr->nStat ){
193921 struct Fts3auxColstats *aNew;
193922 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
193923 sizeof(struct Fts3auxColstats) * nSize
193924 );
193925 if( aNew==0 ) return SQLITE_NOMEM7;
193926 memset(&aNew[pCsr->nStat], 0,
193927 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
193928 );
193929 pCsr->aStat = aNew;
193930 pCsr->nStat = nSize;
193931 }
193932 return SQLITE_OK0;
193933}
193934
193935/*
193936** xNext - Advance the cursor to the next row, if any.
193937*/
193938static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
193939 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
193940 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
193941 int rc;
193942
193943 /* Increment our pretend rowid value. */
193944 pCsr->iRowid++;
193945
193946 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
193947 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK0;
193948 }
193949
193950 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
193951 if( rc==SQLITE_ROW100 ){
193952 int i = 0;
193953 int nDoclist = pCsr->csr.nDoclist;
193954 char *aDoclist = pCsr->csr.aDoclist;
193955 int iCol;
193956
193957 int eState = 0;
193958
193959 if( pCsr->zStop ){
193960 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
193961 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
193962 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
193963 pCsr->isEof = 1;
193964 return SQLITE_OK0;
193965 }
193966 }
193967
193968 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM7;
193969 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
193970 iCol = 0;
193971 rc = SQLITE_OK0;
193972
193973 while( i<nDoclist ){
193974 sqlite3_int64 v = 0;
193975
193976 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
193977 switch( eState ){
193978 /* State 0. In this state the integer just read was a docid. */
193979 case 0:
193980 pCsr->aStat[0].nDoc++;
193981 eState = 1;
193982 iCol = 0;
193983 break;
193984
193985 /* State 1. In this state we are expecting either a 1, indicating
193986 ** that the following integer will be a column number, or the
193987 ** start of a position list for column 0.
193988 **
193989 ** The only difference between state 1 and state 2 is that if the
193990 ** integer encountered in state 1 is not 0 or 1, then we need to
193991 ** increment the column 0 "nDoc" count for this term.
193992 */
193993 case 1:
193994 assert( iCol==0 )((void) (0));
193995 if( v>1 ){
193996 pCsr->aStat[1].nDoc++;
193997 }
193998 eState = 2;
193999 /* fall through */
194000
194001 case 2:
194002 if( v==0 ){ /* 0x00. Next integer will be a docid. */
194003 eState = 0;
194004 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
194005 eState = 3;
194006 }else{ /* 2 or greater. A position. */
194007 pCsr->aStat[iCol+1].nOcc++;
194008 pCsr->aStat[0].nOcc++;
194009 }
194010 break;
194011
194012 /* State 3. The integer just read is a column number. */
194013 default: assert( eState==3 )((void) (0));
194014 iCol = (int)v;
194015 if( iCol<1 ){
194016 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
194017 break;
194018 }
194019 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM7;
194020 pCsr->aStat[iCol+1].nDoc++;
194021 eState = 2;
194022 break;
194023 }
194024 }
194025
194026 pCsr->iCol = 0;
194027 }else{
194028 pCsr->isEof = 1;
194029 }
194030 return rc;
194031}
194032
194033/*
194034** xFilter - Initialize a cursor to point at the start of its data.
194035*/
194036static int fts3auxFilterMethod(
194037 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
194038 int idxNum, /* Strategy index */
194039 const char *idxStr, /* Unused */
194040 int nVal, /* Number of elements in apVal */
194041 sqlite3_value **apVal /* Arguments for the indexing scheme */
194042){
194043 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
194044 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
194045 int rc;
194046 int isScan = 0;
194047 int iLangVal = 0; /* Language id to query */
194048
194049 int iEq = -1; /* Index of term=? value in apVal */
194050 int iGe = -1; /* Index of term>=? value in apVal */
194051 int iLe = -1; /* Index of term<=? value in apVal */
194052 int iLangid = -1; /* Index of languageid=? value in apVal */
194053 int iNext = 0;
194054
194055 UNUSED_PARAMETER(nVal)(void)(nVal);
194056 UNUSED_PARAMETER(idxStr)(void)(idxStr);
194057
194058 assert( idxStr==0 )((void) (0));
194059 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0((void) (0))
194060 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT((void) (0))
194061 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)((void) (0))
194062 )((void) (0));
194063
194064 if( idxNum==FTS4AUX_EQ_CONSTRAINT1 ){
194065 iEq = iNext++;
194066 }else{
194067 isScan = 1;
194068 if( idxNum & FTS4AUX_GE_CONSTRAINT2 ){
194069 iGe = iNext++;
194070 }
194071 if( idxNum & FTS4AUX_LE_CONSTRAINT4 ){
194072 iLe = iNext++;
194073 }
194074 }
194075 if( iNext<nVal ){
194076 iLangid = iNext++;
194077 }
194078
194079 /* In case this cursor is being reused, close and zero it. */
194080 testcase(pCsr->filter.zTerm);
194081 sqlite3Fts3SegReaderFinish(&pCsr->csr);
194082 sqlite3_free((void *)pCsr->filter.zTerm);
194083 sqlite3_free(pCsr->aStat);
194084 sqlite3_free(pCsr->zStop);
194085 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
194086
194087 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001|FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
194088 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN0x00000010;
194089
194090 if( iEq>=0 || iGe>=0 ){
194091 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
194092 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) )((void) (0));
194093 if( zStr ){
194094 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
194095 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM7;
194096 pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
194097 }
194098 }
194099
194100 if( iLe>=0 ){
194101 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
194102 if( pCsr->zStop==0 ) return SQLITE_NOMEM7;
194103 pCsr->nStop = (int)strlen(pCsr->zStop);
194104 }
194105
194106 if( iLangid>=0 ){
194107 iLangVal = sqlite3_value_int(apVal[iLangid]);
194108
194109 /* If the user specified a negative value for the languageid, use zero
194110 ** instead. This works, as the "languageid=?" constraint will also
194111 ** be tested by the VDBE layer. The test will always be false (since
194112 ** this module will not return a row with a negative languageid), and
194113 ** so the overall query will return zero rows. */
194114 if( iLangVal<0 ) iLangVal = 0;
194115 }
194116 pCsr->iLangid = iLangVal;
194117
194118 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL-2,
194119 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
194120 );
194121 if( rc==SQLITE_OK0 ){
194122 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
194123 }
194124
194125 if( rc==SQLITE_OK0 ) rc = fts3auxNextMethod(pCursor);
194126 return rc;
194127}
194128
194129/*
194130** xEof - Return true if the cursor is at EOF, or false otherwise.
194131*/
194132static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
194133 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
194134 return pCsr->isEof;
194135}
194136
194137/*
194138** xColumn - Return a column value.
194139*/
194140static int fts3auxColumnMethod(
194141 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
194142 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
194143 int iCol /* Index of column to read value from */
194144){
194145 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
194146
194147 assert( p->isEof==0 )((void) (0));
194148 switch( iCol ){
194149 case 0: /* term */
194150 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
194151 break;
194152
194153 case 1: /* col */
194154 if( p->iCol ){
194155 sqlite3_result_int(pCtx, p->iCol-1);
194156 }else{
194157 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
194158 }
194159 break;
194160
194161 case 2: /* documents */
194162 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
194163 break;
194164
194165 case 3: /* occurrences */
194166 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
194167 break;
194168
194169 default: /* languageid */
194170 assert( iCol==4 )((void) (0));
194171 sqlite3_result_int(pCtx, p->iLangid);
194172 break;
194173 }
194174
194175 return SQLITE_OK0;
194176}
194177
194178/*
194179** xRowid - Return the current rowid for the cursor.
194180*/
194181static int fts3auxRowidMethod(
194182 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
194183 sqlite_int64 *pRowid /* OUT: Rowid value */
194184){
194185 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
194186 *pRowid = pCsr->iRowid;
194187 return SQLITE_OK0;
194188}
194189
194190/*
194191** Register the fts3aux module with database connection db. Return SQLITE_OK
194192** if successful or an error code if sqlite3_create_module() fails.
194193*/
194194SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db){
194195 static const sqlite3_module fts3aux_module = {
194196 0, /* iVersion */
194197 fts3auxConnectMethod, /* xCreate */
194198 fts3auxConnectMethod, /* xConnect */
194199 fts3auxBestIndexMethod, /* xBestIndex */
194200 fts3auxDisconnectMethod, /* xDisconnect */
194201 fts3auxDisconnectMethod, /* xDestroy */
194202 fts3auxOpenMethod, /* xOpen */
194203 fts3auxCloseMethod, /* xClose */
194204 fts3auxFilterMethod, /* xFilter */
194205 fts3auxNextMethod, /* xNext */
194206 fts3auxEofMethod, /* xEof */
194207 fts3auxColumnMethod, /* xColumn */
194208 fts3auxRowidMethod, /* xRowid */
194209 0, /* xUpdate */
194210 0, /* xBegin */
194211 0, /* xSync */
194212 0, /* xCommit */
194213 0, /* xRollback */
194214 0, /* xFindFunction */
194215 0, /* xRename */
194216 0, /* xSavepoint */
194217 0, /* xRelease */
194218 0, /* xRollbackTo */
194219 0, /* xShadowName */
194220 0 /* xIntegrity */
194221 };
194222 int rc; /* Return code */
194223
194224 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
194225 return rc;
194226}
194227
194228#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
194229
194230/************** End of fts3_aux.c ********************************************/
194231/************** Begin file fts3_expr.c ***************************************/
194232/*
194233** 2008 Nov 28
194234**
194235** The author disclaims copyright to this source code. In place of
194236** a legal notice, here is a blessing:
194237**
194238** May you do good and not evil.
194239** May you find forgiveness for yourself and forgive others.
194240** May you share freely, never taking more than you give.
194241**
194242******************************************************************************
194243**
194244** This module contains code that implements a parser for fts3 query strings
194245** (the right-hand argument to the MATCH operator). Because the supported
194246** syntax is relatively simple, the whole tokenizer/parser system is
194247** hand-coded.
194248*/
194249/* #include "fts3Int.h" */
194250#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
194251
194252/*
194253** By default, this module parses the legacy syntax that has been
194254** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
194255** is defined, then it uses the new syntax. The differences between
194256** the new and the old syntaxes are:
194257**
194258** a) The new syntax supports parenthesis. The old does not.
194259**
194260** b) The new syntax supports the AND and NOT operators. The old does not.
194261**
194262** c) The old syntax supports the "-" token qualifier. This is not
194263** supported by the new syntax (it is replaced by the NOT operator).
194264**
194265** d) When using the old syntax, the OR operator has a greater precedence
194266** than an implicit AND. When using the new, both implicity and explicit
194267** AND operators have a higher precedence than OR.
194268**
194269** If compiled with SQLITE_TEST defined, then this module exports the
194270** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
194271** to zero causes the module to use the old syntax. If it is set to
194272** non-zero the new syntax is activated. This is so both syntaxes can
194273** be tested using a single build of testfixture.
194274**
194275** The following describes the syntax supported by the fts3 MATCH
194276** operator in a similar format to that used by the lemon parser
194277** generator. This module does not use actually lemon, it uses a
194278** custom parser.
194279**
194280** query ::= andexpr (OR andexpr)*.
194281**
194282** andexpr ::= notexpr (AND? notexpr)*.
194283**
194284** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
194285** notexpr ::= LP query RP.
194286**
194287** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
194288**
194289** distance_opt ::= .
194290** distance_opt ::= / INTEGER.
194291**
194292** phrase ::= TOKEN.
194293** phrase ::= COLUMN:TOKEN.
194294** phrase ::= "TOKEN TOKEN TOKEN...".
194295*/
194296
194297#ifdef SQLITE_TEST
194298SQLITE_API int sqlite3_fts3_enable_parentheses1 = 0;
194299#else
194300# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS1
194301# define sqlite3_fts3_enable_parentheses1 1
194302# else
194303# define sqlite3_fts3_enable_parentheses1 0
194304# endif
194305#endif
194306
194307/*
194308** Default span for NEAR operators.
194309*/
194310#define SQLITE_FTS3_DEFAULT_NEAR_PARAM10 10
194311
194312/* #include <string.h> */
194313/* #include <assert.h> */
194314
194315/*
194316** isNot:
194317** This variable is used by function getNextNode(). When getNextNode() is
194318** called, it sets ParseContext.isNot to true if the 'next node' is a
194319** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
194320** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
194321** zero.
194322*/
194323typedef struct ParseContext ParseContext;
194324struct ParseContext {
194325 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
194326 int iLangid; /* Language id used with tokenizer */
194327 const char **azCol; /* Array of column names for fts3 table */
194328 int bFts4; /* True to allow FTS4-only syntax */
194329 int nCol; /* Number of entries in azCol[] */
194330 int iDefaultCol; /* Default column to query */
194331 int isNot; /* True if getNextNode() sees a unary - */
194332 sqlite3_context *pCtx; /* Write error message here */
194333 int nNest; /* Number of nested brackets */
194334};
194335
194336/*
194337** This function is equivalent to the standard isspace() function.
194338**
194339** The standard isspace() can be awkward to use safely, because although it
194340** is defined to accept an argument of type int, its behavior when passed
194341** an integer that falls outside of the range of the unsigned char type
194342** is undefined (and sometimes, "undefined" means segfault). This wrapper
194343** is defined to accept an argument of type char, and always returns 0 for
194344** any values that fall outside of the range of the unsigned char type (i.e.
194345** negative values).
194346*/
194347static int fts3isspace(char c){
194348 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
194349}
194350
194351/*
194352** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
194353** zero the memory before returning a pointer to it. If unsuccessful,
194354** return NULL.
194355*/
194356SQLITE_PRIVATEstatic void *sqlite3Fts3MallocZero(sqlite3_int64 nByte){
194357 void *pRet = sqlite3_malloc64(nByte);
194358 if( pRet ) memset(pRet, 0, nByte);
194359 return pRet;
194360}
194361
194362SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(
194363 sqlite3_tokenizer *pTokenizer,
194364 int iLangid,
194365 const char *z,
194366 int n,
194367 sqlite3_tokenizer_cursor **ppCsr
194368){
194369 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
194370 sqlite3_tokenizer_cursor *pCsr = 0;
194371 int rc;
194372
194373 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
194374 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
194375 if( rc==SQLITE_OK0 ){
194376 pCsr->pTokenizer = pTokenizer;
194377 if( pModule->iVersion>=1 ){
194378 rc = pModule->xLanguageid(pCsr, iLangid);
194379 if( rc!=SQLITE_OK0 ){
194380 pModule->xClose(pCsr);
194381 pCsr = 0;
194382 }
194383 }
194384 }
194385 *ppCsr = pCsr;
194386 return rc;
194387}
194388
194389/*
194390** Function getNextNode(), which is called by fts3ExprParse(), may itself
194391** call fts3ExprParse(). So this forward declaration is required.
194392*/
194393static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
194394
194395/*
194396** Extract the next token from buffer z (length n) using the tokenizer
194397** and other information (column names etc.) in pParse. Create an Fts3Expr
194398** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
194399** single token and set *ppExpr to point to it. If the end of the buffer is
194400** reached before a token is found, set *ppExpr to zero. It is the
194401** responsibility of the caller to eventually deallocate the allocated
194402** Fts3Expr structure (if any) by passing it to sqlite3_free().
194403**
194404** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
194405** fails.
194406*/
194407static int getNextToken(
194408 ParseContext *pParse, /* fts3 query parse context */
194409 int iCol, /* Value for Fts3Phrase.iColumn */
194410 const char *z, int n, /* Input string */
194411 Fts3Expr **ppExpr, /* OUT: expression */
194412 int *pnConsumed /* OUT: Number of bytes consumed */
194413){
194414 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
194415 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
194416 int rc;
194417 sqlite3_tokenizer_cursor *pCursor;
194418 Fts3Expr *pRet = 0;
194419 int i = 0;
194420
194421 /* Set variable i to the maximum number of bytes of input to tokenize. */
194422 for(i=0; i<n; i++){
194423 if( sqlite3_fts3_enable_parentheses1 && (z[i]=='(' || z[i]==')') ) break;
194424 if( z[i]=='"' ) break;
194425 }
194426
194427 *pnConsumed = i;
194428 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
194429 if( rc==SQLITE_OK0 ){
194430 const char *zToken;
194431 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
194432 sqlite3_int64 nByte; /* total space to allocate */
194433
194434 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
194435 if( rc==SQLITE_OK0 ){
194436 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
194437 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte);
194438 if( !pRet ){
194439 rc = SQLITE_NOMEM7;
194440 }else{
194441 pRet->eType = FTSQUERY_PHRASE5;
194442 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
194443 pRet->pPhrase->nToken = 1;
194444 pRet->pPhrase->iColumn = iCol;
194445 pRet->pPhrase->aToken[0].n = nToken;
194446 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
194447 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
194448
194449 if( iEnd<n && z[iEnd]=='*' ){
194450 pRet->pPhrase->aToken[0].isPrefix = 1;
194451 iEnd++;
194452 }
194453
194454 while( 1 ){
194455 if( !sqlite3_fts3_enable_parentheses1
194456 && iStart>0 && z[iStart-1]=='-'
194457 ){
194458 pParse->isNot = 1;
194459 iStart--;
194460 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
194461 pRet->pPhrase->aToken[0].bFirst = 1;
194462 iStart--;
194463 }else{
194464 break;
194465 }
194466 }
194467
194468 }
194469 *pnConsumed = iEnd;
194470 }else if( i && rc==SQLITE_DONE101 ){
194471 rc = SQLITE_OK0;
194472 }
194473
194474 pModule->xClose(pCursor);
194475 }
194476
194477 *ppExpr = pRet;
194478 return rc;
194479}
194480
194481
194482/*
194483** Enlarge a memory allocation. If an out-of-memory allocation occurs,
194484** then free the old allocation.
194485*/
194486static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
194487 void *pRet = sqlite3_realloc64(pOrig, nNew);
194488 if( !pRet ){
194489 sqlite3_free(pOrig);
194490 }
194491 return pRet;
194492}
194493
194494/*
194495** Buffer zInput, length nInput, contains the contents of a quoted string
194496** that appeared as part of an fts3 query expression. Neither quote character
194497** is included in the buffer. This function attempts to tokenize the entire
194498** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
194499** containing the results.
194500**
194501** If successful, SQLITE_OK is returned and *ppExpr set to point at the
194502** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
194503** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
194504** to 0.
194505*/
194506static int getNextString(
194507 ParseContext *pParse, /* fts3 query parse context */
194508 const char *zInput, int nInput, /* Input string */
194509 Fts3Expr **ppExpr /* OUT: expression */
194510){
194511 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
194512 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
194513 int rc;
194514 Fts3Expr *p = 0;
194515 sqlite3_tokenizer_cursor *pCursor = 0;
194516 char *zTemp = 0;
194517 int nTemp = 0;
194518
194519 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
194520 int nToken = 0;
194521
194522 /* The final Fts3Expr data structure, including the Fts3Phrase,
194523 ** Fts3PhraseToken structures token buffers are all stored as a single
194524 ** allocation so that the expression can be freed with a single call to
194525 ** sqlite3_free(). Setting this up requires a two pass approach.
194526 **
194527 ** The first pass, in the block below, uses a tokenizer cursor to iterate
194528 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
194529 ** to assemble data in two dynamic buffers:
194530 **
194531 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
194532 ** structure, followed by the array of Fts3PhraseToken
194533 ** structures. This pass only populates the Fts3PhraseToken array.
194534 **
194535 ** Buffer zTemp: Contains copies of all tokens.
194536 **
194537 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
194538 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
194539 ** structures.
194540 */
194541 rc = sqlite3Fts3OpenTokenizer(
194542 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
194543 if( rc==SQLITE_OK0 ){
194544 int ii;
194545 for(ii=0; rc==SQLITE_OK0; ii++){
194546 const char *zByte;
194547 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
194548 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
194549 if( rc==SQLITE_OK0 ){
194550 Fts3PhraseToken *pToken;
194551
194552 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
194553 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
194554 if( !zTemp || !p ){
194555 rc = SQLITE_NOMEM7;
194556 goto getnextstring_out;
194557 }
194558
194559 assert( nToken==ii )((void) (0));
194560 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
194561 memset(pToken, 0, sizeof(Fts3PhraseToken));
194562
194563 memcpy(&zTemp[nTemp], zByte, nByte);
194564 nTemp += nByte;
194565
194566 pToken->n = nByte;
194567 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
194568 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
194569 nToken = ii+1;
194570 }
194571 }
194572 }
194573
194574 if( rc==SQLITE_DONE101 ){
194575 int jj;
194576 char *zBuf = 0;
194577
194578 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
194579 if( !p ){
194580 rc = SQLITE_NOMEM7;
194581 goto getnextstring_out;
194582 }
194583 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
194584 p->eType = FTSQUERY_PHRASE5;
194585 p->pPhrase = (Fts3Phrase *)&p[1];
194586 p->pPhrase->iColumn = pParse->iDefaultCol;
194587 p->pPhrase->nToken = nToken;
194588
194589 zBuf = (char *)&p->pPhrase->aToken[nToken];
194590 assert( nTemp==0 || zTemp )((void) (0));
194591 if( zTemp ){
194592 memcpy(zBuf, zTemp, nTemp);
194593 }
194594
194595 for(jj=0; jj<p->pPhrase->nToken; jj++){
194596 p->pPhrase->aToken[jj].z = zBuf;
194597 zBuf += p->pPhrase->aToken[jj].n;
194598 }
194599 rc = SQLITE_OK0;
194600 }
194601
194602 getnextstring_out:
194603 if( pCursor ){
194604 pModule->xClose(pCursor);
194605 }
194606 sqlite3_free(zTemp);
194607 if( rc!=SQLITE_OK0 ){
194608 sqlite3_free(p);
194609 p = 0;
194610 }
194611 *ppExpr = p;
194612 return rc;
194613}
194614
194615/*
194616** The output variable *ppExpr is populated with an allocated Fts3Expr
194617** structure, or set to 0 if the end of the input buffer is reached.
194618**
194619** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
194620** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
194621** If SQLITE_ERROR is returned, pContext is populated with an error message.
194622*/
194623static int getNextNode(
194624 ParseContext *pParse, /* fts3 query parse context */
194625 const char *z, int n, /* Input string */
194626 Fts3Expr **ppExpr, /* OUT: expression */
194627 int *pnConsumed /* OUT: Number of bytes consumed */
194628){
194629 static const struct Fts3Keyword {
194630 char *z; /* Keyword text */
194631 unsigned char n; /* Length of the keyword */
194632 unsigned char parenOnly; /* Only valid in paren mode */
194633 unsigned char eType; /* Keyword code */
194634 } aKeyword[] = {
194635 { "OR" , 2, 0, FTSQUERY_OR4 },
194636 { "AND", 3, 1, FTSQUERY_AND3 },
194637 { "NOT", 3, 1, FTSQUERY_NOT2 },
194638 { "NEAR", 4, 0, FTSQUERY_NEAR1 }
194639 };
194640 int ii;
194641 int iCol;
194642 int iColLen;
194643 int rc;
194644 Fts3Expr *pRet = 0;
194645
194646 const char *zInput = z;
194647 int nInput = n;
194648
194649 pParse->isNot = 0;
194650
194651 /* Skip over any whitespace before checking for a keyword, an open or
194652 ** close bracket, or a quoted string.
194653 */
194654 while( nInput>0 && fts3isspace(*zInput) ){
194655 nInput--;
194656 zInput++;
194657 }
194658 if( nInput==0 ){
194659 return SQLITE_DONE101;
194660 }
194661
194662 /* See if we are dealing with a keyword. */
194663 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
194664 const struct Fts3Keyword *pKey = &aKeyword[ii];
194665
194666 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses1)!=0 ){
194667 continue;
194668 }
194669
194670 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
194671 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM10;
194672 int nKey = pKey->n;
194673 char cNext;
194674
194675 /* If this is a "NEAR" keyword, check for an explicit nearness. */
194676 if( pKey->eType==FTSQUERY_NEAR1 ){
194677 assert( nKey==4 )((void) (0));
194678 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
194679 nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear);
194680 }
194681 }
194682
194683 /* At this point this is probably a keyword. But for that to be true,
194684 ** the next byte must contain either whitespace, an open or close
194685 ** parenthesis, a quote character, or EOF.
194686 */
194687 cNext = zInput[nKey];
194688 if( fts3isspace(cNext)
194689 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
194690 ){
194691 pRet = (Fts3Expr *)sqlite3Fts3MallocZero(sizeof(Fts3Expr));
194692 if( !pRet ){
194693 return SQLITE_NOMEM7;
194694 }
194695 pRet->eType = pKey->eType;
194696 pRet->nNear = nNear;
194697 *ppExpr = pRet;
194698 *pnConsumed = (int)((zInput - z) + nKey);
194699 return SQLITE_OK0;
194700 }
194701
194702 /* Turns out that wasn't a keyword after all. This happens if the
194703 ** user has supplied a token such as "ORacle". Continue.
194704 */
194705 }
194706 }
194707
194708 /* See if we are dealing with a quoted phrase. If this is the case, then
194709 ** search for the closing quote and pass the whole string to getNextString()
194710 ** for processing. This is easy to do, as fts3 has no syntax for escaping
194711 ** a quote character embedded in a string.
194712 */
194713 if( *zInput=='"' ){
194714 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
194715 *pnConsumed = (int)((zInput - z) + ii + 1);
194716 if( ii==nInput ){
194717 return SQLITE_ERROR1;
194718 }
194719 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
194720 }
194721
194722 if( sqlite3_fts3_enable_parentheses1 ){
194723 if( *zInput=='(' ){
194724 int nConsumed = 0;
194725 pParse->nNest++;
194726#if !defined(SQLITE_MAX_EXPR_DEPTH1000)
194727 if( pParse->nNest>1000 ) return SQLITE_ERROR1;
194728#elif SQLITE_MAX_EXPR_DEPTH1000>0
194729 if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH1000 ) return SQLITE_ERROR1;
194730#endif
194731 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
194732 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
194733 return rc;
194734 }else if( *zInput==')' ){
194735 pParse->nNest--;
194736 *pnConsumed = (int)((zInput - z) + 1);
194737 *ppExpr = 0;
194738 return SQLITE_DONE101;
194739 }
194740 }
194741
194742 /* If control flows to this point, this must be a regular token, or
194743 ** the end of the input. Read a regular token using the sqlite3_tokenizer
194744 ** interface. Before doing so, figure out if there is an explicit
194745 ** column specifier for the token.
194746 **
194747 ** TODO: Strangely, it is not possible to associate a column specifier
194748 ** with a quoted phrase, only with a single token. Not sure if this was
194749 ** an implementation artifact or an intentional decision when fts3 was
194750 ** first implemented. Whichever it was, this module duplicates the
194751 ** limitation.
194752 */
194753 iCol = pParse->iDefaultCol;
194754 iColLen = 0;
194755 for(ii=0; ii<pParse->nCol; ii++){
194756 const char *zStr = pParse->azCol[ii];
194757 int nStr = (int)strlen(zStr);
194758 if( nInput>nStr && zInput[nStr]==':'
194759 && sqlite3_strnicmp(zStr, zInput, nStr)==0
194760 ){
194761 iCol = ii;
194762 iColLen = (int)((zInput - z) + nStr + 1);
194763 break;
194764 }
194765 }
194766 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
194767 *pnConsumed += iColLen;
194768 return rc;
194769}
194770
194771/*
194772** The argument is an Fts3Expr structure for a binary operator (any type
194773** except an FTSQUERY_PHRASE). Return an integer value representing the
194774** precedence of the operator. Lower values have a higher precedence (i.e.
194775** group more tightly). For example, in the C language, the == operator
194776** groups more tightly than ||, and would therefore have a higher precedence.
194777**
194778** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
194779** is defined), the order of the operators in precedence from highest to
194780** lowest is:
194781**
194782** NEAR
194783** NOT
194784** AND (including implicit ANDs)
194785** OR
194786**
194787** Note that when using the old query syntax, the OR operator has a higher
194788** precedence than the AND operator.
194789*/
194790static int opPrecedence(Fts3Expr *p){
194791 assert( p->eType!=FTSQUERY_PHRASE )((void) (0));
194792 if( sqlite3_fts3_enable_parentheses1 ){
194793 return p->eType;
194794 }else if( p->eType==FTSQUERY_NEAR1 ){
194795 return 1;
194796 }else if( p->eType==FTSQUERY_OR4 ){
194797 return 2;
194798 }
194799 assert( p->eType==FTSQUERY_AND )((void) (0));
194800 return 3;
194801}
194802
194803/*
194804** Argument ppHead contains a pointer to the current head of a query
194805** expression tree being parsed. pPrev is the expression node most recently
194806** inserted into the tree. This function adds pNew, which is always a binary
194807** operator node, into the expression tree based on the relative precedence
194808** of pNew and the existing nodes of the tree. This may result in the head
194809** of the tree changing, in which case *ppHead is set to the new root node.
194810*/
194811static void insertBinaryOperator(
194812 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
194813 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
194814 Fts3Expr *pNew /* New binary node to insert into expression tree */
194815){
194816 Fts3Expr *pSplit = pPrev;
194817 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
194818 pSplit = pSplit->pParent;
194819 }
194820
194821 if( pSplit->pParent ){
194822 assert( pSplit->pParent->pRight==pSplit )((void) (0));
194823 pSplit->pParent->pRight = pNew;
194824 pNew->pParent = pSplit->pParent;
194825 }else{
194826 *ppHead = pNew;
194827 }
194828 pNew->pLeft = pSplit;
194829 pSplit->pParent = pNew;
194830}
194831
194832/*
194833** Parse the fts3 query expression found in buffer z, length n. This function
194834** returns either when the end of the buffer is reached or an unmatched
194835** closing bracket - ')' - is encountered.
194836**
194837** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
194838** parsed form of the expression and *pnConsumed is set to the number of
194839** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
194840** (out of memory error) or SQLITE_ERROR (parse error) is returned.
194841*/
194842static int fts3ExprParse(
194843 ParseContext *pParse, /* fts3 query parse context */
194844 const char *z, int n, /* Text of MATCH query */
194845 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
194846 int *pnConsumed /* OUT: Number of bytes consumed */
194847){
194848 Fts3Expr *pRet = 0;
194849 Fts3Expr *pPrev = 0;
194850 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
194851 int nIn = n;
194852 const char *zIn = z;
194853 int rc = SQLITE_OK0;
194854 int isRequirePhrase = 1;
194855
194856 while( rc==SQLITE_OK0 ){
194857 Fts3Expr *p = 0;
194858 int nByte = 0;
194859
194860 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
194861 assert( nByte>0 || (rc!=SQLITE_OK && p==0) )((void) (0));
194862 if( rc==SQLITE_OK0 ){
194863 if( p ){
194864 int isPhrase;
194865
194866 if( !sqlite3_fts3_enable_parentheses1
194867 && p->eType==FTSQUERY_PHRASE5 && pParse->isNot
194868 ){
194869 /* Create an implicit NOT operator. */
194870 Fts3Expr *pNot = sqlite3Fts3MallocZero(sizeof(Fts3Expr));
194871 if( !pNot ){
194872 sqlite3Fts3ExprFree(p);
194873 rc = SQLITE_NOMEM7;
194874 goto exprparse_out;
194875 }
194876 pNot->eType = FTSQUERY_NOT2;
194877 pNot->pRight = p;
194878 p->pParent = pNot;
194879 if( pNotBranch ){
194880 pNot->pLeft = pNotBranch;
194881 pNotBranch->pParent = pNot;
194882 }
194883 pNotBranch = pNot;
194884 p = pPrev;
194885 }else{
194886 int eType = p->eType;
194887 isPhrase = (eType==FTSQUERY_PHRASE5 || p->pLeft);
194888
194889 /* The isRequirePhrase variable is set to true if a phrase or
194890 ** an expression contained in parenthesis is required. If a
194891 ** binary operator (AND, OR, NOT or NEAR) is encounted when
194892 ** isRequirePhrase is set, this is a syntax error.
194893 */
194894 if( !isPhrase && isRequirePhrase ){
194895 sqlite3Fts3ExprFree(p);
194896 rc = SQLITE_ERROR1;
194897 goto exprparse_out;
194898 }
194899
194900 if( isPhrase && !isRequirePhrase ){
194901 /* Insert an implicit AND operator. */
194902 Fts3Expr *pAnd;
194903 assert( pRet && pPrev )((void) (0));
194904 pAnd = sqlite3Fts3MallocZero(sizeof(Fts3Expr));
194905 if( !pAnd ){
194906 sqlite3Fts3ExprFree(p);
194907 rc = SQLITE_NOMEM7;
194908 goto exprparse_out;
194909 }
194910 pAnd->eType = FTSQUERY_AND3;
194911 insertBinaryOperator(&pRet, pPrev, pAnd);
194912 pPrev = pAnd;
194913 }
194914
194915 /* This test catches attempts to make either operand of a NEAR
194916 ** operator something other than a phrase. For example, either of
194917 ** the following:
194918 **
194919 ** (bracketed expression) NEAR phrase
194920 ** phrase NEAR (bracketed expression)
194921 **
194922 ** Return an error in either case.
194923 */
194924 if( pPrev && (
194925 (eType==FTSQUERY_NEAR1 && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE5)
194926 || (eType!=FTSQUERY_PHRASE5 && isPhrase && pPrev->eType==FTSQUERY_NEAR1)
194927 )){
194928 sqlite3Fts3ExprFree(p);
194929 rc = SQLITE_ERROR1;
194930 goto exprparse_out;
194931 }
194932
194933 if( isPhrase ){
194934 if( pRet ){
194935 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 )((void) (0));
194936 pPrev->pRight = p;
194937 p->pParent = pPrev;
194938 }else{
194939 pRet = p;
194940 }
194941 }else{
194942 insertBinaryOperator(&pRet, pPrev, p);
194943 }
194944 isRequirePhrase = !isPhrase;
194945 }
194946 pPrev = p;
194947 }
194948 assert( nByte>0 )((void) (0));
194949 }
194950 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) )((void) (0));
194951 nIn -= nByte;
194952 zIn += nByte;
194953 }
194954
194955 if( rc==SQLITE_DONE101 && pRet && isRequirePhrase ){
194956 rc = SQLITE_ERROR1;
194957 }
194958
194959 if( rc==SQLITE_DONE101 ){
194960 rc = SQLITE_OK0;
194961 if( !sqlite3_fts3_enable_parentheses1 && pNotBranch ){
194962 if( !pRet ){
194963 rc = SQLITE_ERROR1;
194964 }else{
194965 Fts3Expr *pIter = pNotBranch;
194966 while( pIter->pLeft ){
194967 pIter = pIter->pLeft;
194968 }
194969 pIter->pLeft = pRet;
194970 pRet->pParent = pIter;
194971 pRet = pNotBranch;
194972 }
194973 }
194974 }
194975 *pnConsumed = n - nIn;
194976
194977exprparse_out:
194978 if( rc!=SQLITE_OK0 ){
194979 sqlite3Fts3ExprFree(pRet);
194980 sqlite3Fts3ExprFree(pNotBranch);
194981 pRet = 0;
194982 }
194983 *ppExpr = pRet;
194984 return rc;
194985}
194986
194987/*
194988** Return SQLITE_ERROR if the maximum depth of the expression tree passed
194989** as the only argument is more than nMaxDepth.
194990*/
194991static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
194992 int rc = SQLITE_OK0;
194993 if( p ){
194994 if( nMaxDepth<0 ){
194995 rc = SQLITE_TOOBIG18;
194996 }else{
194997 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
194998 if( rc==SQLITE_OK0 ){
194999 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
195000 }
195001 }
195002 }
195003 return rc;
195004}
195005
195006/*
195007** This function attempts to transform the expression tree at (*pp) to
195008** an equivalent but more balanced form. The tree is modified in place.
195009** If successful, SQLITE_OK is returned and (*pp) set to point to the
195010** new root expression node.
195011**
195012** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
195013**
195014** Otherwise, if an error occurs, an SQLite error code is returned and
195015** expression (*pp) freed.
195016*/
195017static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
195018 int rc = SQLITE_OK0; /* Return code */
195019 Fts3Expr *pRoot = *pp; /* Initial root node */
195020 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
195021 int eType = pRoot->eType; /* Type of node in this tree */
195022
195023 if( nMaxDepth==0 ){
195024 rc = SQLITE_ERROR1;
195025 }
195026
195027 if( rc==SQLITE_OK0 ){
195028 if( (eType==FTSQUERY_AND3 || eType==FTSQUERY_OR4) ){
195029 Fts3Expr **apLeaf;
195030 apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
195031 if( 0==apLeaf ){
195032 rc = SQLITE_NOMEM7;
195033 }else{
195034 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
195035 }
195036
195037 if( rc==SQLITE_OK0 ){
195038 int i;
195039 Fts3Expr *p;
195040
195041 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
195042 for(p=pRoot; p->eType==eType; p=p->pLeft){
195043 assert( p->pParent==0 || p->pParent->pLeft==p )((void) (0));
195044 assert( p->pLeft && p->pRight )((void) (0));
195045 }
195046
195047 /* This loop runs once for each leaf in the tree of eType nodes. */
195048 while( 1 ){
195049 int iLvl;
195050 Fts3Expr *pParent = p->pParent; /* Current parent of p */
195051
195052 assert( pParent==0 || pParent->pLeft==p )((void) (0));
195053 p->pParent = 0;
195054 if( pParent ){
195055 pParent->pLeft = 0;
195056 }else{
195057 pRoot = 0;
195058 }
195059 rc = fts3ExprBalance(&p, nMaxDepth-1);
195060 if( rc!=SQLITE_OK0 ) break;
195061
195062 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
195063 if( apLeaf[iLvl]==0 ){
195064 apLeaf[iLvl] = p;
195065 p = 0;
195066 }else{
195067 assert( pFree )((void) (0));
195068 pFree->pLeft = apLeaf[iLvl];
195069 pFree->pRight = p;
195070 pFree->pLeft->pParent = pFree;
195071 pFree->pRight->pParent = pFree;
195072
195073 p = pFree;
195074 pFree = pFree->pParent;
195075 p->pParent = 0;
195076 apLeaf[iLvl] = 0;
195077 }
195078 }
195079 if( p ){
195080 sqlite3Fts3ExprFree(p);
195081 rc = SQLITE_TOOBIG18;
195082 break;
195083 }
195084
195085 /* If that was the last leaf node, break out of the loop */
195086 if( pParent==0 ) break;
195087
195088 /* Set $p to point to the next leaf in the tree of eType nodes */
195089 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
195090
195091 /* Remove pParent from the original tree. */
195092 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent )((void) (0));
195093 pParent->pRight->pParent = pParent->pParent;
195094 if( pParent->pParent ){
195095 pParent->pParent->pLeft = pParent->pRight;
195096 }else{
195097 assert( pParent==pRoot )((void) (0));
195098 pRoot = pParent->pRight;
195099 }
195100
195101 /* Link pParent into the free node list. It will be used as an
195102 ** internal node of the new tree. */
195103 pParent->pParent = pFree;
195104 pFree = pParent;
195105 }
195106
195107 if( rc==SQLITE_OK0 ){
195108 p = 0;
195109 for(i=0; i<nMaxDepth; i++){
195110 if( apLeaf[i] ){
195111 if( p==0 ){
195112 p = apLeaf[i];
195113 p->pParent = 0;
195114 }else{
195115 assert( pFree!=0 )((void) (0));
195116 pFree->pRight = p;
195117 pFree->pLeft = apLeaf[i];
195118 pFree->pLeft->pParent = pFree;
195119 pFree->pRight->pParent = pFree;
195120
195121 p = pFree;
195122 pFree = pFree->pParent;
195123 p->pParent = 0;
195124 }
195125 }
195126 }
195127 pRoot = p;
195128 }else{
195129 /* An error occurred. Delete the contents of the apLeaf[] array
195130 ** and pFree list. Everything else is cleaned up by the call to
195131 ** sqlite3Fts3ExprFree(pRoot) below. */
195132 Fts3Expr *pDel;
195133 for(i=0; i<nMaxDepth; i++){
195134 sqlite3Fts3ExprFree(apLeaf[i]);
195135 }
195136 while( (pDel=pFree)!=0 ){
195137 pFree = pDel->pParent;
195138 sqlite3_free(pDel);
195139 }
195140 }
195141
195142 assert( pFree==0 )((void) (0));
195143 sqlite3_free( apLeaf );
195144 }
195145 }else if( eType==FTSQUERY_NOT2 ){
195146 Fts3Expr *pLeft = pRoot->pLeft;
195147 Fts3Expr *pRight = pRoot->pRight;
195148
195149 pRoot->pLeft = 0;
195150 pRoot->pRight = 0;
195151 pLeft->pParent = 0;
195152 pRight->pParent = 0;
195153
195154 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
195155 if( rc==SQLITE_OK0 ){
195156 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
195157 }
195158
195159 if( rc!=SQLITE_OK0 ){
195160 sqlite3Fts3ExprFree(pRight);
195161 sqlite3Fts3ExprFree(pLeft);
195162 }else{
195163 assert( pLeft && pRight )((void) (0));
195164 pRoot->pLeft = pLeft;
195165 pLeft->pParent = pRoot;
195166 pRoot->pRight = pRight;
195167 pRight->pParent = pRoot;
195168 }
195169 }
195170 }
195171
195172 if( rc!=SQLITE_OK0 ){
195173 sqlite3Fts3ExprFree(pRoot);
195174 pRoot = 0;
195175 }
195176 *pp = pRoot;
195177 return rc;
195178}
195179
195180/*
195181** This function is similar to sqlite3Fts3ExprParse(), with the following
195182** differences:
195183**
195184** 1. It does not do expression rebalancing.
195185** 2. It does not check that the expression does not exceed the
195186** maximum allowable depth.
195187** 3. Even if it fails, *ppExpr may still be set to point to an
195188** expression tree. It should be deleted using sqlite3Fts3ExprFree()
195189** in this case.
195190*/
195191static int fts3ExprParseUnbalanced(
195192 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
195193 int iLangid, /* Language id for tokenizer */
195194 char **azCol, /* Array of column names for fts3 table */
195195 int bFts4, /* True to allow FTS4-only syntax */
195196 int nCol, /* Number of entries in azCol[] */
195197 int iDefaultCol, /* Default column to query */
195198 const char *z, int n, /* Text of MATCH query */
195199 Fts3Expr **ppExpr /* OUT: Parsed query structure */
195200){
195201 int nParsed;
195202 int rc;
195203 ParseContext sParse;
195204
195205 memset(&sParse, 0, sizeof(ParseContext));
195206 sParse.pTokenizer = pTokenizer;
195207 sParse.iLangid = iLangid;
195208 sParse.azCol = (const char **)azCol;
195209 sParse.nCol = nCol;
195210 sParse.iDefaultCol = iDefaultCol;
195211 sParse.bFts4 = bFts4;
195212 if( z==0 ){
195213 *ppExpr = 0;
195214 return SQLITE_OK0;
195215 }
195216 if( n<0 ){
195217 n = (int)strlen(z);
195218 }
195219 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
195220 assert( rc==SQLITE_OK || *ppExpr==0 )((void) (0));
195221
195222 /* Check for mismatched parenthesis */
195223 if( rc==SQLITE_OK0 && sParse.nNest ){
195224 rc = SQLITE_ERROR1;
195225 }
195226
195227 return rc;
195228}
195229
195230/*
195231** Parameters z and n contain a pointer to and length of a buffer containing
195232** an fts3 query expression, respectively. This function attempts to parse the
195233** query expression and create a tree of Fts3Expr structures representing the
195234** parsed expression. If successful, *ppExpr is set to point to the head
195235** of the parsed expression tree and SQLITE_OK is returned. If an error
195236** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
195237** error) is returned and *ppExpr is set to 0.
195238**
195239** If parameter n is a negative number, then z is assumed to point to a
195240** nul-terminated string and the length is determined using strlen().
195241**
195242** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
195243** use to normalize query tokens while parsing the expression. The azCol[]
195244** array, which is assumed to contain nCol entries, should contain the names
195245** of each column in the target fts3 table, in order from left to right.
195246** Column names must be nul-terminated strings.
195247**
195248** The iDefaultCol parameter should be passed the index of the table column
195249** that appears on the left-hand-side of the MATCH operator (the default
195250** column to match against for tokens for which a column name is not explicitly
195251** specified as part of the query string), or -1 if tokens may by default
195252** match any table column.
195253*/
195254SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(
195255 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
195256 int iLangid, /* Language id for tokenizer */
195257 char **azCol, /* Array of column names for fts3 table */
195258 int bFts4, /* True to allow FTS4-only syntax */
195259 int nCol, /* Number of entries in azCol[] */
195260 int iDefaultCol, /* Default column to query */
195261 const char *z, int n, /* Text of MATCH query */
195262 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
195263 char **pzErr /* OUT: Error message (sqlite3_malloc) */
195264){
195265 int rc = fts3ExprParseUnbalanced(
195266 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
195267 );
195268
195269 /* Rebalance the expression. And check that its depth does not exceed
195270 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
195271 if( rc==SQLITE_OK0 && *ppExpr ){
195272 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH12);
195273 if( rc==SQLITE_OK0 ){
195274 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH12);
195275 }
195276 }
195277
195278 if( rc!=SQLITE_OK0 ){
195279 sqlite3Fts3ExprFree(*ppExpr);
195280 *ppExpr = 0;
195281 if( rc==SQLITE_TOOBIG18 ){
195282 sqlite3Fts3ErrMsg(pzErr,
195283 "FTS expression tree is too large (maximum depth %d)",
195284 SQLITE_FTS3_MAX_EXPR_DEPTH12
195285 );
195286 rc = SQLITE_ERROR1;
195287 }else if( rc==SQLITE_ERROR1 ){
195288 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
195289 }
195290 }
195291
195292 return rc;
195293}
195294
195295/*
195296** Free a single node of an expression tree.
195297*/
195298static void fts3FreeExprNode(Fts3Expr *p){
195299 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 )((void) (0));
195300 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
195301 sqlite3_free(p->aMI);
195302 sqlite3_free(p);
195303}
195304
195305/*
195306** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
195307**
195308** This function would be simpler if it recursively called itself. But
195309** that would mean passing a sufficiently large expression to ExprParse()
195310** could cause a stack overflow.
195311*/
195312SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *pDel){
195313 Fts3Expr *p;
195314 assert( pDel==0 || pDel->pParent==0 )((void) (0));
195315 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
195316 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
195317 }
195318 while( p ){
195319 Fts3Expr *pParent = p->pParent;
195320 fts3FreeExprNode(p);
195321 if( pParent && p==pParent->pLeft && pParent->pRight ){
195322 p = pParent->pRight;
195323 while( p && (p->pLeft || p->pRight) ){
195324 assert( p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
195325 p = (p->pLeft ? p->pLeft : p->pRight);
195326 }
195327 }else{
195328 p = pParent;
195329 }
195330 }
195331}
195332
195333/****************************************************************************
195334*****************************************************************************
195335** Everything after this point is just test code.
195336*/
195337
195338#ifdef SQLITE_TEST
195339
195340/* #include <stdio.h> */
195341
195342/*
195343** Return a pointer to a buffer containing a text representation of the
195344** expression passed as the first argument. The buffer is obtained from
195345** sqlite3_malloc(). It is the responsibility of the caller to use
195346** sqlite3_free() to release the memory. If an OOM condition is encountered,
195347** NULL is returned.
195348**
195349** If the second argument is not NULL, then its contents are prepended to
195350** the returned expression text and then freed using sqlite3_free().
195351*/
195352static char *exprToString(Fts3Expr *pExpr, char *zBuf){
195353 if( pExpr==0 ){
195354 return sqlite3_mprintf("");
195355 }
195356 switch( pExpr->eType ){
195357 case FTSQUERY_PHRASE5: {
195358 Fts3Phrase *pPhrase = pExpr->pPhrase;
195359 int i;
195360 zBuf = sqlite3_mprintf(
195361 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
195362 for(i=0; zBuf && i<pPhrase->nToken; i++){
195363 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
195364 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
195365 (pPhrase->aToken[i].isPrefix?"+":"")
195366 );
195367 }
195368 return zBuf;
195369 }
195370
195371 case FTSQUERY_NEAR1:
195372 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
195373 break;
195374 case FTSQUERY_NOT2:
195375 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
195376 break;
195377 case FTSQUERY_AND3:
195378 zBuf = sqlite3_mprintf("%zAND ", zBuf);
195379 break;
195380 case FTSQUERY_OR4:
195381 zBuf = sqlite3_mprintf("%zOR ", zBuf);
195382 break;
195383 }
195384
195385 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
195386 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
195387 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
195388
195389 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
195390 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
195391
195392 return zBuf;
195393}
195394
195395/*
195396** This is the implementation of a scalar SQL function used to test the
195397** expression parser. It should be called as follows:
195398**
195399** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
195400**
195401** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
195402** to parse the query expression (see README.tokenizers). The second argument
195403** is the query expression to parse. Each subsequent argument is the name
195404** of a column of the fts3 table that the query expression may refer to.
195405** For example:
195406**
195407** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
195408*/
195409static void fts3ExprTestCommon(
195410 int bRebalance,
195411 sqlite3_context *context,
195412 int argc,
195413 sqlite3_value **argv
195414){
195415 sqlite3_tokenizer *pTokenizer = 0;
195416 int rc;
195417 char **azCol = 0;
195418 const char *zExpr;
195419 int nExpr;
195420 int nCol;
195421 int ii;
195422 Fts3Expr *pExpr;
195423 char *zBuf = 0;
195424 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
195425 const char *zTokenizer = 0;
195426 char *zErr = 0;
195427
195428 if( argc<3 ){
195429 sqlite3_result_error(context,
195430 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
195431 );
195432 return;
195433 }
195434
195435 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
195436 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
195437 if( rc!=SQLITE_OK0 ){
195438 if( rc==SQLITE_NOMEM7 ){
195439 sqlite3_result_error_nomem(context);
195440 }else{
195441 sqlite3_result_error(context, zErr, -1);
195442 }
195443 sqlite3_free(zErr);
195444 return;
195445 }
195446
195447 zExpr = (const char *)sqlite3_value_text(argv[1]);
195448 nExpr = sqlite3_value_bytes(argv[1]);
195449 nCol = argc-2;
195450 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
195451 if( !azCol ){
195452 sqlite3_result_error_nomem(context);
195453 goto exprtest_out;
195454 }
195455 for(ii=0; ii<nCol; ii++){
195456 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
195457 }
195458
195459 if( bRebalance ){
195460 char *zDummy = 0;
195461 rc = sqlite3Fts3ExprParse(
195462 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
195463 );
195464 assert( rc==SQLITE_OK || pExpr==0 )((void) (0));
195465 sqlite3_free(zDummy);
195466 }else{
195467 rc = fts3ExprParseUnbalanced(
195468 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
195469 );
195470 }
195471
195472 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 ){
195473 sqlite3Fts3ExprFree(pExpr);
195474 sqlite3_result_error(context, "Error parsing expression", -1);
195475 }else if( rc==SQLITE_NOMEM7 || !(zBuf = exprToString(pExpr, 0)) ){
195476 sqlite3_result_error_nomem(context);
195477 }else{
195478 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195479 sqlite3_free(zBuf);
195480 }
195481
195482 sqlite3Fts3ExprFree(pExpr);
195483
195484exprtest_out:
195485 if( pTokenizer ){
195486 rc = pTokenizer->pModule->xDestroy(pTokenizer);
195487 }
195488 sqlite3_free(azCol);
195489}
195490
195491static void fts3ExprTest(
195492 sqlite3_context *context,
195493 int argc,
195494 sqlite3_value **argv
195495){
195496 fts3ExprTestCommon(0, context, argc, argv);
195497}
195498static void fts3ExprTestRebalance(
195499 sqlite3_context *context,
195500 int argc,
195501 sqlite3_value **argv
195502){
195503 fts3ExprTestCommon(1, context, argc, argv);
195504}
195505
195506/*
195507** Register the query expression parser test function fts3_exprtest()
195508** with database connection db.
195509*/
195510SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
195511 int rc = sqlite3_create_function(
195512 db, "fts3_exprtest", -1, SQLITE_UTF81, (void*)pHash, fts3ExprTest, 0, 0
195513 );
195514 if( rc==SQLITE_OK0 ){
195515 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
195516 -1, SQLITE_UTF81, (void*)pHash, fts3ExprTestRebalance, 0, 0
195517 );
195518 }
195519 return rc;
195520}
195521
195522#endif
195523#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
195524
195525/************** End of fts3_expr.c *******************************************/
195526/************** Begin file fts3_hash.c ***************************************/
195527/*
195528** 2001 September 22
195529**
195530** The author disclaims copyright to this source code. In place of
195531** a legal notice, here is a blessing:
195532**
195533** May you do good and not evil.
195534** May you find forgiveness for yourself and forgive others.
195535** May you share freely, never taking more than you give.
195536**
195537*************************************************************************
195538** This is the implementation of generic hash-tables used in SQLite.
195539** We've modified it slightly to serve as a standalone hash table
195540** implementation for the full-text indexing module.
195541*/
195542
195543/*
195544** The code in this file is only compiled if:
195545**
195546** * The FTS3 module is being built as an extension
195547** (in which case SQLITE_CORE is not defined), or
195548**
195549** * The FTS3 module is being built into the core of
195550** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
195551*/
195552/* #include "fts3Int.h" */
195553#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
195554
195555/* #include <assert.h> */
195556/* #include <stdlib.h> */
195557/* #include <string.h> */
195558
195559/* #include "fts3_hash.h" */
195560
195561/*
195562** Malloc and Free functions
195563*/
195564static void *fts3HashMalloc(sqlite3_int64 n){
195565 void *p = sqlite3_malloc64(n);
195566 if( p ){
195567 memset(p, 0, n);
195568 }
195569 return p;
195570}
195571static void fts3HashFree(void *p){
195572 sqlite3_free(p);
195573}
195574
195575/* Turn bulk memory into a hash table object by initializing the
195576** fields of the Hash structure.
195577**
195578** "pNew" is a pointer to the hash table that is to be initialized.
195579** keyClass is one of the constants
195580** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
195581** determines what kind of key the hash table will use. "copyKey" is
195582** true if the hash table should make its own private copy of keys and
195583** false if it should just use the supplied pointer.
195584*/
195585SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
195586 assert( pNew!=0 )((void) (0));
195587 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY )((void) (0));
195588 pNew->keyClass = keyClass;
195589 pNew->copyKey = copyKey;
195590 pNew->first = 0;
195591 pNew->count = 0;
195592 pNew->htsize = 0;
195593 pNew->ht = 0;
195594}
195595
195596/* Remove all entries from a hash table. Reclaim all memory.
195597** Call this routine to delete a hash table or to reset a hash table
195598** to the empty state.
195599*/
195600SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash *pH){
195601 Fts3HashElem *elem; /* For looping over all elements of the table */
195602
195603 assert( pH!=0 )((void) (0));
195604 elem = pH->first;
195605 pH->first = 0;
195606 fts3HashFree(pH->ht);
195607 pH->ht = 0;
195608 pH->htsize = 0;
195609 while( elem ){
195610 Fts3HashElem *next_elem = elem->next;
195611 if( pH->copyKey && elem->pKey ){
195612 fts3HashFree(elem->pKey);
195613 }
195614 fts3HashFree(elem);
195615 elem = next_elem;
195616 }
195617 pH->count = 0;
195618}
195619
195620/*
195621** Hash and comparison functions when the mode is FTS3_HASH_STRING
195622*/
195623static int fts3StrHash(const void *pKey, int nKey){
195624 const char *z = (const char *)pKey;
195625 unsigned h = 0;
195626 if( nKey<=0 ) nKey = (int) strlen(z);
195627 while( nKey > 0 ){
195628 h = (h<<3) ^ h ^ *z++;
195629 nKey--;
195630 }
195631 return (int)(h & 0x7fffffff);
195632}
195633static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
195634 if( n1!=n2 ) return 1;
195635 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
195636}
195637
195638/*
195639** Hash and comparison functions when the mode is FTS3_HASH_BINARY
195640*/
195641static int fts3BinHash(const void *pKey, int nKey){
195642 int h = 0;
195643 const char *z = (const char *)pKey;
195644 while( nKey-- > 0 ){
195645 h = (h<<3) ^ h ^ *(z++);
195646 }
195647 return h & 0x7fffffff;
195648}
195649static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
195650 if( n1!=n2 ) return 1;
195651 return memcmp(pKey1,pKey2,n1);
195652}
195653
195654/*
195655** Return a pointer to the appropriate hash function given the key class.
195656**
195657** The C syntax in this function definition may be unfamilar to some
195658** programmers, so we provide the following additional explanation:
195659**
195660** The name of the function is "ftsHashFunction". The function takes a
195661** single parameter "keyClass". The return value of ftsHashFunction()
195662** is a pointer to another function. Specifically, the return value
195663** of ftsHashFunction() is a pointer to a function that takes two parameters
195664** with types "const void*" and "int" and returns an "int".
195665*/
195666static int (*ftsHashFunction(int keyClass))(const void*,int){
195667 if( keyClass==FTS3_HASH_STRING1 ){
195668 return &fts3StrHash;
195669 }else{
195670 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
195671 return &fts3BinHash;
195672 }
195673}
195674
195675/*
195676** Return a pointer to the appropriate hash function given the key class.
195677**
195678** For help in interpreted the obscure C code in the function definition,
195679** see the header comment on the previous function.
195680*/
195681static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
195682 if( keyClass==FTS3_HASH_STRING1 ){
195683 return &fts3StrCompare;
195684 }else{
195685 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
195686 return &fts3BinCompare;
195687 }
195688}
195689
195690/* Link an element into the hash table
195691*/
195692static void fts3HashInsertElement(
195693 Fts3Hash *pH, /* The complete hash table */
195694 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
195695 Fts3HashElem *pNew /* The element to be inserted */
195696){
195697 Fts3HashElem *pHead; /* First element already in pEntry */
195698 pHead = pEntry->chain;
195699 if( pHead ){
195700 pNew->next = pHead;
195701 pNew->prev = pHead->prev;
195702 if( pHead->prev ){ pHead->prev->next = pNew; }
195703 else { pH->first = pNew; }
195704 pHead->prev = pNew;
195705 }else{
195706 pNew->next = pH->first;
195707 if( pH->first ){ pH->first->prev = pNew; }
195708 pNew->prev = 0;
195709 pH->first = pNew;
195710 }
195711 pEntry->count++;
195712 pEntry->chain = pNew;
195713}
195714
195715
195716/* Resize the hash table so that it cantains "new_size" buckets.
195717** "new_size" must be a power of 2. The hash table might fail
195718** to resize if sqliteMalloc() fails.
195719**
195720** Return non-zero if a memory allocation error occurs.
195721*/
195722static int fts3Rehash(Fts3Hash *pH, int new_size){
195723 struct _fts3ht *new_ht; /* The new hash table */
195724 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
195725 int (*xHash)(const void*,int); /* The hash function */
195726
195727 assert( (new_size & (new_size-1))==0 )((void) (0));
195728 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
195729 if( new_ht==0 ) return 1;
195730 fts3HashFree(pH->ht);
195731 pH->ht = new_ht;
195732 pH->htsize = new_size;
195733 xHash = ftsHashFunction(pH->keyClass);
195734 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
195735 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
195736 next_elem = elem->next;
195737 fts3HashInsertElement(pH, &new_ht[h], elem);
195738 }
195739 return 0;
195740}
195741
195742/* This function (for internal use only) locates an element in an
195743** hash table that matches the given key. The hash for this key has
195744** already been computed and is passed as the 4th parameter.
195745*/
195746static Fts3HashElem *fts3FindElementByHash(
195747 const Fts3Hash *pH, /* The pH to be searched */
195748 const void *pKey, /* The key we are searching for */
195749 int nKey,
195750 int h /* The hash for this key. */
195751){
195752 Fts3HashElem *elem; /* Used to loop thru the element list */
195753 int count; /* Number of elements left to test */
195754 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
195755
195756 if( pH->ht ){
195757 struct _fts3ht *pEntry = &pH->ht[h];
195758 elem = pEntry->chain;
195759 count = pEntry->count;
195760 xCompare = ftsCompareFunction(pH->keyClass);
195761 while( count-- && elem ){
195762 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
195763 return elem;
195764 }
195765 elem = elem->next;
195766 }
195767 }
195768 return 0;
195769}
195770
195771/* Remove a single entry from the hash table given a pointer to that
195772** element and a hash on the element's key.
195773*/
195774static void fts3RemoveElementByHash(
195775 Fts3Hash *pH, /* The pH containing "elem" */
195776 Fts3HashElem* elem, /* The element to be removed from the pH */
195777 int h /* Hash value for the element */
195778){
195779 struct _fts3ht *pEntry;
195780 if( elem->prev ){
195781 elem->prev->next = elem->next;
195782 }else{
195783 pH->first = elem->next;
195784 }
195785 if( elem->next ){
195786 elem->next->prev = elem->prev;
195787 }
195788 pEntry = &pH->ht[h];
195789 if( pEntry->chain==elem ){
195790 pEntry->chain = elem->next;
195791 }
195792 pEntry->count--;
195793 if( pEntry->count<=0 ){
195794 pEntry->chain = 0;
195795 }
195796 if( pH->copyKey && elem->pKey ){
195797 fts3HashFree(elem->pKey);
195798 }
195799 fts3HashFree( elem );
195800 pH->count--;
195801 if( pH->count<=0 ){
195802 assert( pH->first==0 )((void) (0));
195803 assert( pH->count==0 )((void) (0));
195804 fts3HashClearsqlite3Fts3HashClear(pH);
195805 }
195806}
195807
195808SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(
195809 const Fts3Hash *pH,
195810 const void *pKey,
195811 int nKey
195812){
195813 int h; /* A hash on key */
195814 int (*xHash)(const void*,int); /* The hash function */
195815
195816 if( pH==0 || pH->ht==0 ) return 0;
195817 xHash = ftsHashFunction(pH->keyClass);
195818 assert( xHash!=0 )((void) (0));
195819 h = (*xHash)(pKey,nKey);
195820 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
195821 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
195822}
195823
195824/*
195825** Attempt to locate an element of the hash table pH with a key
195826** that matches pKey,nKey. Return the data for this element if it is
195827** found, or NULL if there is no match.
195828*/
195829SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
195830 Fts3HashElem *pElem; /* The element that matches key (if any) */
195831
195832 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
195833 return pElem ? pElem->data : 0;
195834}
195835
195836/* Insert an element into the hash table pH. The key is pKey,nKey
195837** and the data is "data".
195838**
195839** If no element exists with a matching key, then a new
195840** element is created. A copy of the key is made if the copyKey
195841** flag is set. NULL is returned.
195842**
195843** If another element already exists with the same key, then the
195844** new data replaces the old data and the old data is returned.
195845** The key is not copied in this instance. If a malloc fails, then
195846** the new data is returned and the hash table is unchanged.
195847**
195848** If the "data" parameter to this function is NULL, then the
195849** element corresponding to "key" is removed from the hash table.
195850*/
195851SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(
195852 Fts3Hash *pH, /* The hash table to insert into */
195853 const void *pKey, /* The key */
195854 int nKey, /* Number of bytes in the key */
195855 void *data /* The data */
195856){
195857 int hraw; /* Raw hash value of the key */
195858 int h; /* the hash of the key modulo hash table size */
195859 Fts3HashElem *elem; /* Used to loop thru the element list */
195860 Fts3HashElem *new_elem; /* New element added to the pH */
195861 int (*xHash)(const void*,int); /* The hash function */
195862
195863 assert( pH!=0 )((void) (0));
195864 xHash = ftsHashFunction(pH->keyClass);
195865 assert( xHash!=0 )((void) (0));
195866 hraw = (*xHash)(pKey, nKey);
195867 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
195868 h = hraw & (pH->htsize-1);
195869 elem = fts3FindElementByHash(pH,pKey,nKey,h);
195870 if( elem ){
195871 void *old_data = elem->data;
195872 if( data==0 ){
195873 fts3RemoveElementByHash(pH,elem,h);
195874 }else{
195875 elem->data = data;
195876 }
195877 return old_data;
195878 }
195879 if( data==0 ) return 0;
195880 if( (pH->htsize==0 && fts3Rehash(pH,8))
195881 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
195882 ){
195883 pH->count = 0;
195884 return data;
195885 }
195886 assert( pH->htsize>0 )((void) (0));
195887 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
195888 if( new_elem==0 ) return data;
195889 if( pH->copyKey && pKey!=0 ){
195890 new_elem->pKey = fts3HashMalloc( nKey );
195891 if( new_elem->pKey==0 ){
195892 fts3HashFree(new_elem);
195893 return data;
195894 }
195895 memcpy((void*)new_elem->pKey, pKey, nKey);
195896 }else{
195897 new_elem->pKey = (void*)pKey;
195898 }
195899 new_elem->nKey = nKey;
195900 pH->count++;
195901 assert( pH->htsize>0 )((void) (0));
195902 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
195903 h = hraw & (pH->htsize-1);
195904 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
195905 new_elem->data = data;
195906 return 0;
195907}
195908
195909#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
195910
195911/************** End of fts3_hash.c *******************************************/
195912/************** Begin file fts3_porter.c *************************************/
195913/*
195914** 2006 September 30
195915**
195916** The author disclaims copyright to this source code. In place of
195917** a legal notice, here is a blessing:
195918**
195919** May you do good and not evil.
195920** May you find forgiveness for yourself and forgive others.
195921** May you share freely, never taking more than you give.
195922**
195923*************************************************************************
195924** Implementation of the full-text-search tokenizer that implements
195925** a Porter stemmer.
195926*/
195927
195928/*
195929** The code in this file is only compiled if:
195930**
195931** * The FTS3 module is being built as an extension
195932** (in which case SQLITE_CORE is not defined), or
195933**
195934** * The FTS3 module is being built into the core of
195935** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
195936*/
195937/* #include "fts3Int.h" */
195938#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
195939
195940/* #include <assert.h> */
195941/* #include <stdlib.h> */
195942/* #include <stdio.h> */
195943/* #include <string.h> */
195944
195945/* #include "fts3_tokenizer.h" */
195946
195947/*
195948** Class derived from sqlite3_tokenizer
195949*/
195950typedef struct porter_tokenizer {
195951 sqlite3_tokenizer base; /* Base class */
195952} porter_tokenizer;
195953
195954/*
195955** Class derived from sqlite3_tokenizer_cursor
195956*/
195957typedef struct porter_tokenizer_cursor {
195958 sqlite3_tokenizer_cursor base;
195959 const char *zInput; /* input we are tokenizing */
195960 int nInput; /* size of the input */
195961 int iOffset; /* current position in zInput */
195962 int iToken; /* index of next token to be returned */
195963 char *zToken; /* storage for current token */
195964 int nAllocated; /* space allocated to zToken buffer */
195965} porter_tokenizer_cursor;
195966
195967
195968/*
195969** Create a new tokenizer instance.
195970*/
195971static int porterCreate(
195972 int argc, const char * const *argv,
195973 sqlite3_tokenizer **ppTokenizer
195974){
195975 porter_tokenizer *t;
195976
195977 UNUSED_PARAMETER(argc)(void)(argc);
195978 UNUSED_PARAMETER(argv)(void)(argv);
195979
195980 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
195981 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
195982 memset(t, 0, sizeof(*t));
195983 *ppTokenizer = &t->base;
195984 return SQLITE_OK0;
195985}
195986
195987/*
195988** Destroy a tokenizer
195989*/
195990static int porterDestroy(sqlite3_tokenizer *pTokenizer){
195991 sqlite3_free(pTokenizer);
195992 return SQLITE_OK0;
195993}
195994
195995/*
195996** Prepare to begin tokenizing a particular string. The input
195997** string to be tokenized is zInput[0..nInput-1]. A cursor
195998** used to incrementally tokenize this string is returned in
195999** *ppCursor.
196000*/
196001static int porterOpen(
196002 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
196003 const char *zInput, int nInput, /* String to be tokenized */
196004 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
196005){
196006 porter_tokenizer_cursor *c;
196007
196008 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
196009
196010 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
196011 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
196012
196013 c->zInput = zInput;
196014 if( zInput==0 ){
196015 c->nInput = 0;
196016 }else if( nInput<0 ){
196017 c->nInput = (int)strlen(zInput);
196018 }else{
196019 c->nInput = nInput;
196020 }
196021 c->iOffset = 0; /* start tokenizing at the beginning */
196022 c->iToken = 0;
196023 c->zToken = NULL((void*)0); /* no space allocated, yet. */
196024 c->nAllocated = 0;
196025
196026 *ppCursor = &c->base;
196027 return SQLITE_OK0;
196028}
196029
196030/*
196031** Close a tokenization cursor previously opened by a call to
196032** porterOpen() above.
196033*/
196034static int porterClose(sqlite3_tokenizer_cursor *pCursor){
196035 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
196036 sqlite3_free(c->zToken);
196037 sqlite3_free(c);
196038 return SQLITE_OK0;
196039}
196040/*
196041** Vowel or consonant
196042*/
196043static const char cType[] = {
196044 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
196045 1, 1, 1, 2, 1
196046};
196047
196048/*
196049** isConsonant() and isVowel() determine if their first character in
196050** the string they point to is a consonant or a vowel, according
196051** to Porter ruls.
196052**
196053** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
196054** 'Y' is a consonant unless it follows another consonant,
196055** in which case it is a vowel.
196056**
196057** In these routine, the letters are in reverse order. So the 'y' rule
196058** is that 'y' is a consonant unless it is followed by another
196059** consonent.
196060*/
196061static int isVowel(const char*);
196062static int isConsonant(const char *z){
196063 int j;
196064 char x = *z;
196065 if( x==0 ) return 0;
196066 assert( x>='a' && x<='z' )((void) (0));
196067 j = cType[x-'a'];
196068 if( j<2 ) return j;
196069 return z[1]==0 || isVowel(z + 1);
196070}
196071static int isVowel(const char *z){
196072 int j;
196073 char x = *z;
196074 if( x==0 ) return 0;
196075 assert( x>='a' && x<='z' )((void) (0));
196076 j = cType[x-'a'];
196077 if( j<2 ) return 1-j;
196078 return isConsonant(z + 1);
196079}
196080
196081/*
196082** Let any sequence of one or more vowels be represented by V and let
196083** C be sequence of one or more consonants. Then every word can be
196084** represented as:
196085**
196086** [C] (VC){m} [V]
196087**
196088** In prose: A word is an optional consonant followed by zero or
196089** vowel-consonant pairs followed by an optional vowel. "m" is the
196090** number of vowel consonant pairs. This routine computes the value
196091** of m for the first i bytes of a word.
196092**
196093** Return true if the m-value for z is 1 or more. In other words,
196094** return true if z contains at least one vowel that is followed
196095** by a consonant.
196096**
196097** In this routine z[] is in reverse order. So we are really looking
196098** for an instance of a consonant followed by a vowel.
196099*/
196100static int m_gt_0(const char *z){
196101 while( isVowel(z) ){ z++; }
196102 if( *z==0 ) return 0;
196103 while( isConsonant(z) ){ z++; }
196104 return *z!=0;
196105}
196106
196107/* Like mgt0 above except we are looking for a value of m which is
196108** exactly 1
196109*/
196110static int m_eq_1(const char *z){
196111 while( isVowel(z) ){ z++; }
196112 if( *z==0 ) return 0;
196113 while( isConsonant(z) ){ z++; }
196114 if( *z==0 ) return 0;
196115 while( isVowel(z) ){ z++; }
196116 if( *z==0 ) return 1;
196117 while( isConsonant(z) ){ z++; }
196118 return *z==0;
196119}
196120
196121/* Like mgt0 above except we are looking for a value of m>1 instead
196122** or m>0
196123*/
196124static int m_gt_1(const char *z){
196125 while( isVowel(z) ){ z++; }
196126 if( *z==0 ) return 0;
196127 while( isConsonant(z) ){ z++; }
196128 if( *z==0 ) return 0;
196129 while( isVowel(z) ){ z++; }
196130 if( *z==0 ) return 0;
196131 while( isConsonant(z) ){ z++; }
196132 return *z!=0;
196133}
196134
196135/*
196136** Return TRUE if there is a vowel anywhere within z[0..n-1]
196137*/
196138static int hasVowel(const char *z){
196139 while( isConsonant(z) ){ z++; }
196140 return *z!=0;
196141}
196142
196143/*
196144** Return TRUE if the word ends in a double consonant.
196145**
196146** The text is reversed here. So we are really looking at
196147** the first two characters of z[].
196148*/
196149static int doubleConsonant(const char *z){
196150 return isConsonant(z) && z[0]==z[1];
196151}
196152
196153/*
196154** Return TRUE if the word ends with three letters which
196155** are consonant-vowel-consonent and where the final consonant
196156** is not 'w', 'x', or 'y'.
196157**
196158** The word is reversed here. So we are really checking the
196159** first three letters and the first one cannot be in [wxy].
196160*/
196161static int star_oh(const char *z){
196162 return
196163 isConsonant(z) &&
196164 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
196165 isVowel(z+1) &&
196166 isConsonant(z+2);
196167}
196168
196169/*
196170** If the word ends with zFrom and xCond() is true for the stem
196171** of the word that preceeds the zFrom ending, then change the
196172** ending to zTo.
196173**
196174** The input word *pz and zFrom are both in reverse order. zTo
196175** is in normal order.
196176**
196177** Return TRUE if zFrom matches. Return FALSE if zFrom does not
196178** match. Not that TRUE is returned even if xCond() fails and
196179** no substitution occurs.
196180*/
196181static int stem(
196182 char **pz, /* The word being stemmed (Reversed) */
196183 const char *zFrom, /* If the ending matches this... (Reversed) */
196184 const char *zTo, /* ... change the ending to this (not reversed) */
196185 int (*xCond)(const char*) /* Condition that must be true */
196186){
196187 char *z = *pz;
196188 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
196189 if( *zFrom!=0 ) return 0;
196190 if( xCond && !xCond(z) ) return 1;
196191 while( *zTo ){
196192 *(--z) = *(zTo++);
196193 }
196194 *pz = z;
196195 return 1;
196196}
196197
196198/*
196199** This is the fallback stemmer used when the porter stemmer is
196200** inappropriate. The input word is copied into the output with
196201** US-ASCII case folding. If the input word is too long (more
196202** than 20 bytes if it contains no digits or more than 6 bytes if
196203** it contains digits) then word is truncated to 20 or 6 bytes
196204** by taking 10 or 3 bytes from the beginning and end.
196205*/
196206static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
196207 int i, mx, j;
196208 int hasDigit = 0;
196209 for(i=0; i<nIn; i++){
196210 char c = zIn[i];
196211 if( c>='A' && c<='Z' ){
196212 zOut[i] = c - 'A' + 'a';
196213 }else{
196214 if( c>='0' && c<='9' ) hasDigit = 1;
196215 zOut[i] = c;
196216 }
196217 }
196218 mx = hasDigit ? 3 : 10;
196219 if( nIn>mx*2 ){
196220 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
196221 zOut[j] = zOut[i];
196222 }
196223 i = j;
196224 }
196225 zOut[i] = 0;
196226 *pnOut = i;
196227}
196228
196229
196230/*
196231** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
196232** zOut is at least big enough to hold nIn bytes. Write the actual
196233** size of the output word (exclusive of the '\0' terminator) into *pnOut.
196234**
196235** Any upper-case characters in the US-ASCII character set ([A-Z])
196236** are converted to lower case. Upper-case UTF characters are
196237** unchanged.
196238**
196239** Words that are longer than about 20 bytes are stemmed by retaining
196240** a few bytes from the beginning and the end of the word. If the
196241** word contains digits, 3 bytes are taken from the beginning and
196242** 3 bytes from the end. For long words without digits, 10 bytes
196243** are taken from each end. US-ASCII case folding still applies.
196244**
196245** If the input word contains not digits but does characters not
196246** in [a-zA-Z] then no stemming is attempted and this routine just
196247** copies the input into the input into the output with US-ASCII
196248** case folding.
196249**
196250** Stemming never increases the length of the word. So there is
196251** no chance of overflowing the zOut buffer.
196252*/
196253static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
196254 int i, j;
196255 char zReverse[28];
196256 char *z, *z2;
196257 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
196258 /* The word is too big or too small for the porter stemmer.
196259 ** Fallback to the copy stemmer */
196260 copy_stemmer(zIn, nIn, zOut, pnOut);
196261 return;
196262 }
196263 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
196264 char c = zIn[i];
196265 if( c>='A' && c<='Z' ){
196266 zReverse[j] = c + 'a' - 'A';
196267 }else if( c>='a' && c<='z' ){
196268 zReverse[j] = c;
196269 }else{
196270 /* The use of a character not in [a-zA-Z] means that we fallback
196271 ** to the copy stemmer */
196272 copy_stemmer(zIn, nIn, zOut, pnOut);
196273 return;
196274 }
196275 }
196276 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
196277 z = &zReverse[j+1];
196278
196279
196280 /* Step 1a */
196281 if( z[0]=='s' ){
196282 if(
196283 !stem(&z, "sess", "ss", 0) &&
196284 !stem(&z, "sei", "i", 0) &&
196285 !stem(&z, "ss", "ss", 0)
196286 ){
196287 z++;
196288 }
196289 }
196290
196291 /* Step 1b */
196292 z2 = z;
196293 if( stem(&z, "dee", "ee", m_gt_0) ){
196294 /* Do nothing. The work was all in the test */
196295 }else if(
196296 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
196297 && z!=z2
196298 ){
196299 if( stem(&z, "ta", "ate", 0) ||
196300 stem(&z, "lb", "ble", 0) ||
196301 stem(&z, "zi", "ize", 0) ){
196302 /* Do nothing. The work was all in the test */
196303 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
196304 z++;
196305 }else if( m_eq_1(z) && star_oh(z) ){
196306 *(--z) = 'e';
196307 }
196308 }
196309
196310 /* Step 1c */
196311 if( z[0]=='y' && hasVowel(z+1) ){
196312 z[0] = 'i';
196313 }
196314
196315 /* Step 2 */
196316 switch( z[1] ){
196317 case 'a':
196318 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
196319 stem(&z, "lanoit", "tion", m_gt_0);
196320 }
196321 break;
196322 case 'c':
196323 if( !stem(&z, "icne", "ence", m_gt_0) ){
196324 stem(&z, "icna", "ance", m_gt_0);
196325 }
196326 break;
196327 case 'e':
196328 stem(&z, "rezi", "ize", m_gt_0);
196329 break;
196330 case 'g':
196331 stem(&z, "igol", "log", m_gt_0);
196332 break;
196333 case 'l':
196334 if( !stem(&z, "ilb", "ble", m_gt_0)
196335 && !stem(&z, "illa", "al", m_gt_0)
196336 && !stem(&z, "iltne", "ent", m_gt_0)
196337 && !stem(&z, "ile", "e", m_gt_0)
196338 ){
196339 stem(&z, "ilsuo", "ous", m_gt_0);
196340 }
196341 break;
196342 case 'o':
196343 if( !stem(&z, "noitazi", "ize", m_gt_0)
196344 && !stem(&z, "noita", "ate", m_gt_0)
196345 ){
196346 stem(&z, "rota", "ate", m_gt_0);
196347 }
196348 break;
196349 case 's':
196350 if( !stem(&z, "msila", "al", m_gt_0)
196351 && !stem(&z, "ssenevi", "ive", m_gt_0)
196352 && !stem(&z, "ssenluf", "ful", m_gt_0)
196353 ){
196354 stem(&z, "ssensuo", "ous", m_gt_0);
196355 }
196356 break;
196357 case 't':
196358 if( !stem(&z, "itila", "al", m_gt_0)
196359 && !stem(&z, "itivi", "ive", m_gt_0)
196360 ){
196361 stem(&z, "itilib", "ble", m_gt_0);
196362 }
196363 break;
196364 }
196365
196366 /* Step 3 */
196367 switch( z[0] ){
196368 case 'e':
196369 if( !stem(&z, "etaci", "ic", m_gt_0)
196370 && !stem(&z, "evita", "", m_gt_0)
196371 ){
196372 stem(&z, "ezila", "al", m_gt_0);
196373 }
196374 break;
196375 case 'i':
196376 stem(&z, "itici", "ic", m_gt_0);
196377 break;
196378 case 'l':
196379 if( !stem(&z, "laci", "ic", m_gt_0) ){
196380 stem(&z, "luf", "", m_gt_0);
196381 }
196382 break;
196383 case 's':
196384 stem(&z, "ssen", "", m_gt_0);
196385 break;
196386 }
196387
196388 /* Step 4 */
196389 switch( z[1] ){
196390 case 'a':
196391 if( z[0]=='l' && m_gt_1(z+2) ){
196392 z += 2;
196393 }
196394 break;
196395 case 'c':
196396 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
196397 z += 4;
196398 }
196399 break;
196400 case 'e':
196401 if( z[0]=='r' && m_gt_1(z+2) ){
196402 z += 2;
196403 }
196404 break;
196405 case 'i':
196406 if( z[0]=='c' && m_gt_1(z+2) ){
196407 z += 2;
196408 }
196409 break;
196410 case 'l':
196411 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
196412 z += 4;
196413 }
196414 break;
196415 case 'n':
196416 if( z[0]=='t' ){
196417 if( z[2]=='a' ){
196418 if( m_gt_1(z+3) ){
196419 z += 3;
196420 }
196421 }else if( z[2]=='e' ){
196422 if( !stem(&z, "tneme", "", m_gt_1)
196423 && !stem(&z, "tnem", "", m_gt_1)
196424 ){
196425 stem(&z, "tne", "", m_gt_1);
196426 }
196427 }
196428 }
196429 break;
196430 case 'o':
196431 if( z[0]=='u' ){
196432 if( m_gt_1(z+2) ){
196433 z += 2;
196434 }
196435 }else if( z[3]=='s' || z[3]=='t' ){
196436 stem(&z, "noi", "", m_gt_1);
196437 }
196438 break;
196439 case 's':
196440 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
196441 z += 3;
196442 }
196443 break;
196444 case 't':
196445 if( !stem(&z, "eta", "", m_gt_1) ){
196446 stem(&z, "iti", "", m_gt_1);
196447 }
196448 break;
196449 case 'u':
196450 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
196451 z += 3;
196452 }
196453 break;
196454 case 'v':
196455 case 'z':
196456 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
196457 z += 3;
196458 }
196459 break;
196460 }
196461
196462 /* Step 5a */
196463 if( z[0]=='e' ){
196464 if( m_gt_1(z+1) ){
196465 z++;
196466 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
196467 z++;
196468 }
196469 }
196470
196471 /* Step 5b */
196472 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
196473 z++;
196474 }
196475
196476 /* z[] is now the stemmed word in reverse order. Flip it back
196477 ** around into forward order and return.
196478 */
196479 *pnOut = i = (int)strlen(z);
196480 zOut[i] = 0;
196481 while( *z ){
196482 zOut[--i] = *(z++);
196483 }
196484}
196485
196486/*
196487** Characters that can be part of a token. We assume any character
196488** whose value is greater than 0x80 (any UTF character) can be
196489** part of a token. In other words, delimiters all must have
196490** values of 0x7f or lower.
196491*/
196492static const char porterIdChar[] = {
196493/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
196494 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
196495 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
196496 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
196497 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
196498 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
196499};
196500#define isDelim(C)(((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar
[ch-0x30]))
(((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
196501
196502/*
196503** Extract the next token from a tokenization cursor. The cursor must
196504** have been opened by a prior call to porterOpen().
196505*/
196506static int porterNext(
196507 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
196508 const char **pzToken, /* OUT: *pzToken is the token text */
196509 int *pnBytes, /* OUT: Number of bytes in token */
196510 int *piStartOffset, /* OUT: Starting offset of token */
196511 int *piEndOffset, /* OUT: Ending offset of token */
196512 int *piPosition /* OUT: Position integer of token */
196513){
196514 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
196515 const char *z = c->zInput;
196516
196517 while( c->iOffset<c->nInput ){
196518 int iStartOffset, ch;
196519
196520 /* Scan past delimiter characters */
196521 while( c->iOffset<c->nInput && isDelim(z[c->iOffset])(((ch=z[c->iOffset])&0x80)==0 && (ch<0x30 ||
!porterIdChar[ch-0x30]))
){
196522 c->iOffset++;
196523 }
196524
196525 /* Count non-delimiter characters. */
196526 iStartOffset = c->iOffset;
196527 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset])(((ch=z[c->iOffset])&0x80)==0 && (ch<0x30 ||
!porterIdChar[ch-0x30]))
){
196528 c->iOffset++;
196529 }
196530
196531 if( c->iOffset>iStartOffset ){
196532 int n = c->iOffset-iStartOffset;
196533 if( n>c->nAllocated ){
196534 char *pNew;
196535 c->nAllocated = n+20;
196536 pNew = sqlite3_realloc64(c->zToken, c->nAllocated);
196537 if( !pNew ) return SQLITE_NOMEM7;
196538 c->zToken = pNew;
196539 }
196540 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
196541 *pzToken = c->zToken;
196542 *piStartOffset = iStartOffset;
196543 *piEndOffset = c->iOffset;
196544 *piPosition = c->iToken++;
196545 return SQLITE_OK0;
196546 }
196547 }
196548 return SQLITE_DONE101;
196549}
196550
196551/*
196552** The set of routines that implement the porter-stemmer tokenizer
196553*/
196554static const sqlite3_tokenizer_module porterTokenizerModule = {
196555 0,
196556 porterCreate,
196557 porterDestroy,
196558 porterOpen,
196559 porterClose,
196560 porterNext,
196561 0
196562};
196563
196564/*
196565** Allocate a new porter tokenizer. Return a pointer to the new
196566** tokenizer in *ppModule
196567*/
196568SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(
196569 sqlite3_tokenizer_module const**ppModule
196570){
196571 *ppModule = &porterTokenizerModule;
196572}
196573
196574#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
196575
196576/************** End of fts3_porter.c *****************************************/
196577/************** Begin file fts3_tokenizer.c **********************************/
196578/*
196579** 2007 June 22
196580**
196581** The author disclaims copyright to this source code. In place of
196582** a legal notice, here is a blessing:
196583**
196584** May you do good and not evil.
196585** May you find forgiveness for yourself and forgive others.
196586** May you share freely, never taking more than you give.
196587**
196588******************************************************************************
196589**
196590** This is part of an SQLite module implementing full-text search.
196591** This particular file implements the generic tokenizer interface.
196592*/
196593
196594/*
196595** The code in this file is only compiled if:
196596**
196597** * The FTS3 module is being built as an extension
196598** (in which case SQLITE_CORE is not defined), or
196599**
196600** * The FTS3 module is being built into the core of
196601** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
196602*/
196603/* #include "fts3Int.h" */
196604#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
196605
196606/* #include <assert.h> */
196607/* #include <string.h> */
196608
196609/*
196610** Return true if the two-argument version of fts3_tokenizer()
196611** has been activated via a prior call to sqlite3_db_config(db,
196612** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
196613*/
196614static int fts3TokenizerEnabled(sqlite3_context *context){
196615 sqlite3 *db = sqlite3_context_db_handle(context);
196616 int isEnabled = 0;
196617 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004,-1,&isEnabled);
196618 return isEnabled;
196619}
196620
196621/*
196622** Implementation of the SQL scalar function for accessing the underlying
196623** hash table. This function may be called as follows:
196624**
196625** SELECT <function-name>(<key-name>);
196626** SELECT <function-name>(<key-name>, <pointer>);
196627**
196628** where <function-name> is the name passed as the second argument
196629** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
196630**
196631** If the <pointer> argument is specified, it must be a blob value
196632** containing a pointer to be stored as the hash data corresponding
196633** to the string <key-name>. If <pointer> is not specified, then
196634** the string <key-name> must already exist in the has table. Otherwise,
196635** an error is returned.
196636**
196637** Whether or not the <pointer> argument is specified, the value returned
196638** is a blob containing the pointer stored as the hash data corresponding
196639** to string <key-name> (after the hash-table is updated, if applicable).
196640*/
196641static void fts3TokenizerFunc(
196642 sqlite3_context *context,
196643 int argc,
196644 sqlite3_value **argv
196645){
196646 Fts3Hash *pHash;
196647 void *pPtr = 0;
196648 const unsigned char *zName;
196649 int nName;
196650
196651 assert( argc==1 || argc==2 )((void) (0));
196652
196653 pHash = (Fts3Hash *)sqlite3_user_data(context);
196654
196655 zName = sqlite3_value_text(argv[0]);
196656 nName = sqlite3_value_bytes(argv[0])+1;
196657
196658 if( argc==2 ){
196659 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
196660 void *pOld;
196661 int n = sqlite3_value_bytes(argv[1]);
196662 if( zName==0 || n!=sizeof(pPtr) ){
196663 sqlite3_result_error(context, "argument type mismatch", -1);
196664 return;
196665 }
196666 pPtr = *(void **)sqlite3_value_blob(argv[1]);
196667 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
196668 if( pOld==pPtr ){
196669 sqlite3_result_error(context, "out of memory", -1);
196670 }
196671 }else{
196672 sqlite3_result_error(context, "fts3tokenize disabled", -1);
196673 return;
196674 }
196675 }else{
196676 if( zName ){
196677 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
196678 }
196679 if( !pPtr ){
196680 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
196681 sqlite3_result_error(context, zErr, -1);
196682 sqlite3_free(zErr);
196683 return;
196684 }
196685 }
196686 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
196687 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
196688 }
196689}
196690
196691SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char c){
196692 static const char isFtsIdChar[] = {
196693 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
196694 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
196695 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
196696 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
196697 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
196698 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
196699 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
196700 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
196701 };
196702 return (c&0x80 || isFtsIdChar[(int)(c)]);
196703}
196704
196705SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
196706 const char *z1;
196707 const char *z2 = 0;
196708
196709 /* Find the start of the next token. */
196710 z1 = zStr;
196711 while( z2==0 ){
196712 char c = *z1;
196713 switch( c ){
196714 case '\0': return 0; /* No more tokens here */
196715 case '\'':
196716 case '"':
196717 case '`': {
196718 z2 = z1;
196719 while( *++z2 && (*z2!=c || *++z2==c) );
196720 break;
196721 }
196722 case '[':
196723 z2 = &z1[1];
196724 while( *z2 && z2[0]!=']' ) z2++;
196725 if( *z2 ) z2++;
196726 break;
196727
196728 default:
196729 if( sqlite3Fts3IsIdChar(*z1) ){
196730 z2 = &z1[1];
196731 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
196732 }else{
196733 z1++;
196734 }
196735 }
196736 }
196737
196738 *pn = (int)(z2-z1);
196739 return z1;
196740}
196741
196742SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(
196743 Fts3Hash *pHash, /* Tokenizer hash table */
196744 const char *zArg, /* Tokenizer name */
196745 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
196746 char **pzErr /* OUT: Set to malloced error message */
196747){
196748 int rc;
196749 char *z = (char *)zArg;
196750 int n = 0;
196751 char *zCopy;
196752 char *zEnd; /* Pointer to nul-term of zCopy */
196753 sqlite3_tokenizer_module *m;
196754
196755 zCopy = sqlite3_mprintf("%s", zArg);
196756 if( !zCopy ) return SQLITE_NOMEM7;
196757 zEnd = &zCopy[strlen(zCopy)];
196758
196759 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
196760 if( z==0 ){
196761 assert( n==0 )((void) (0));
196762 z = zCopy;
196763 }
196764 z[n] = '\0';
196765 sqlite3Fts3Dequote(z);
196766
196767 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
196768 if( !m ){
196769 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
196770 rc = SQLITE_ERROR1;
196771 }else{
196772 char const **aArg = 0;
196773 int iArg = 0;
196774 z = &z[n+1];
196775 while( z<zEnd && (NULL((void*)0)!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
196776 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
196777 char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
196778 if( !aNew ){
196779 sqlite3_free(zCopy);
196780 sqlite3_free((void *)aArg);
196781 return SQLITE_NOMEM7;
196782 }
196783 aArg = aNew;
196784 aArg[iArg++] = z;
196785 z[n] = '\0';
196786 sqlite3Fts3Dequote(z);
196787 z = &z[n+1];
196788 }
196789 rc = m->xCreate(iArg, aArg, ppTok);
196790 assert( rc!=SQLITE_OK || *ppTok )((void) (0));
196791 if( rc!=SQLITE_OK0 ){
196792 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
196793 }else{
196794 (*ppTok)->pModule = m;
196795 }
196796 sqlite3_free((void *)aArg);
196797 }
196798
196799 sqlite3_free(zCopy);
196800 return rc;
196801}
196802
196803
196804#ifdef SQLITE_TEST
196805
196806#include "tclsqlite.h"
196807/* #include <string.h> */
196808
196809/*
196810** Implementation of a special SQL scalar function for testing tokenizers
196811** designed to be used in concert with the Tcl testing framework. This
196812** function must be called with two or more arguments:
196813**
196814** SELECT <function-name>(<key-name>, ..., <input-string>);
196815**
196816** where <function-name> is the name passed as the second argument
196817** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
196818** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
196819**
196820** The return value is a string that may be interpreted as a Tcl
196821** list. For each token in the <input-string>, three elements are
196822** added to the returned list. The first is the token position, the
196823** second is the token text (folded, stemmed, etc.) and the third is the
196824** substring of <input-string> associated with the token. For example,
196825** using the built-in "simple" tokenizer:
196826**
196827** SELECT fts_tokenizer_test('simple', 'I don't see how');
196828**
196829** will return the string:
196830**
196831** "{0 i I 1 dont don't 2 see see 3 how how}"
196832**
196833*/
196834static void testFunc(
196835 sqlite3_context *context,
196836 int argc,
196837 sqlite3_value **argv
196838){
196839 Fts3Hash *pHash;
196840 sqlite3_tokenizer_module *p;
196841 sqlite3_tokenizer *pTokenizer = 0;
196842 sqlite3_tokenizer_cursor *pCsr = 0;
196843
196844 const char *zErr = 0;
196845
196846 const char *zName;
196847 int nName;
196848 const char *zInput;
196849 int nInput;
196850
196851 const char *azArg[64];
196852
196853 const char *zToken;
196854 int nToken = 0;
196855 int iStart = 0;
196856 int iEnd = 0;
196857 int iPos = 0;
196858 int i;
196859
196860 Tcl_Obj *pRet;
196861
196862 if( argc<2 ){
196863 sqlite3_result_error(context, "insufficient arguments", -1);
196864 return;
196865 }
196866
196867 nName = sqlite3_value_bytes(argv[0]);
196868 zName = (const char *)sqlite3_value_text(argv[0]);
196869 nInput = sqlite3_value_bytes(argv[argc-1]);
196870 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
196871
196872 pHash = (Fts3Hash *)sqlite3_user_data(context);
196873 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
196874
196875 if( !p ){
196876 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
196877 sqlite3_result_error(context, zErr2, -1);
196878 sqlite3_free(zErr2);
196879 return;
196880 }
196881
196882 pRet = Tcl_NewObj();
196883 Tcl_IncrRefCount(pRet);
196884
196885 for(i=1; i<argc-1; i++){
196886 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
196887 }
196888
196889 if( SQLITE_OK0!=p->xCreate(argc-2, azArg, &pTokenizer) ){
196890 zErr = "error in xCreate()";
196891 goto finish;
196892 }
196893 pTokenizer->pModule = p;
196894 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
196895 zErr = "error in xOpen()";
196896 goto finish;
196897 }
196898
196899 while( SQLITE_OK0==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
196900 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
196901 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
196902 zToken = &zInput[iStart];
196903 nToken = iEnd-iStart;
196904 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
196905 }
196906
196907 if( SQLITE_OK0!=p->xClose(pCsr) ){
196908 zErr = "error in xClose()";
196909 goto finish;
196910 }
196911 if( SQLITE_OK0!=p->xDestroy(pTokenizer) ){
196912 zErr = "error in xDestroy()";
196913 goto finish;
196914 }
196915
196916finish:
196917 if( zErr ){
196918 sqlite3_result_error(context, zErr, -1);
196919 }else{
196920 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
196921 }
196922 Tcl_DecrRefCount(pRet);
196923}
196924
196925static
196926int registerTokenizer(
196927 sqlite3 *db,
196928 char *zName,
196929 const sqlite3_tokenizer_module *p
196930){
196931 int rc;
196932 sqlite3_stmt *pStmt;
196933 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
196934
196935 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
196936 if( rc!=SQLITE_OK0 ){
196937 return rc;
196938 }
196939
196940 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
196941 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC((sqlite3_destructor_type)0));
196942 sqlite3_step(pStmt);
196943
196944 return sqlite3_finalize(pStmt);
196945}
196946
196947
196948static
196949int queryTokenizer(
196950 sqlite3 *db,
196951 char *zName,
196952 const sqlite3_tokenizer_module **pp
196953){
196954 int rc;
196955 sqlite3_stmt *pStmt;
196956 const char zSql[] = "SELECT fts3_tokenizer(?)";
196957
196958 *pp = 0;
196959 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
196960 if( rc!=SQLITE_OK0 ){
196961 return rc;
196962 }
196963
196964 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
196965 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
196966 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB4
196967 && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp)
196968 ){
196969 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
196970 }
196971 }
196972
196973 return sqlite3_finalize(pStmt);
196974}
196975
196976SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
196977
196978/*
196979** Implementation of the scalar function fts3_tokenizer_internal_test().
196980** This function is used for testing only, it is not included in the
196981** build unless SQLITE_TEST is defined.
196982**
196983** The purpose of this is to test that the fts3_tokenizer() function
196984** can be used as designed by the C-code in the queryTokenizer and
196985** registerTokenizer() functions above. These two functions are repeated
196986** in the README.tokenizer file as an example, so it is important to
196987** test them.
196988**
196989** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
196990** function with no arguments. An assert() will fail if a problem is
196991** detected. i.e.:
196992**
196993** SELECT fts3_tokenizer_internal_test();
196994**
196995*/
196996static void intTestFunc(
196997 sqlite3_context *context,
196998 int argc,
196999 sqlite3_value **argv
197000){
197001 int rc;
197002 const sqlite3_tokenizer_module *p1;
197003 const sqlite3_tokenizer_module *p2;
197004 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
197005
197006 UNUSED_PARAMETER(argc)(void)(argc);
197007 UNUSED_PARAMETER(argv)(void)(argv);
197008
197009 /* Test the query function */
197010 sqlite3Fts3SimpleTokenizerModule(&p1);
197011 rc = queryTokenizer(db, "simple", &p2);
197012 assert( rc==SQLITE_OK )((void) (0));
197013 assert( p1==p2 )((void) (0));
197014 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
197015 assert( rc==SQLITE_ERROR )((void) (0));
197016 assert( p2==0 )((void) (0));
197017 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") )((void) (0));
197018
197019 /* Test the storage function */
197020 if( fts3TokenizerEnabled(context) ){
197021 rc = registerTokenizer(db, "nosuchtokenizer", p1);
197022 assert( rc==SQLITE_OK )((void) (0));
197023 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
197024 assert( rc==SQLITE_OK )((void) (0));
197025 assert( p2==p1 )((void) (0));
197026 }
197027
197028 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
197029}
197030
197031#endif
197032
197033/*
197034** Set up SQL objects in database db used to access the contents of
197035** the hash table pointed to by argument pHash. The hash table must
197036** been initialized to use string keys, and to take a private copy
197037** of the key when a value is inserted. i.e. by a call similar to:
197038**
197039** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
197040**
197041** This function adds a scalar function (see header comment above
197042** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
197043** defined at compilation time, a temporary virtual table (see header
197044** comment above struct HashTableVtab) to the database schema. Both
197045** provide read/write access to the contents of *pHash.
197046**
197047** The third argument to this function, zName, is used as the name
197048** of both the scalar and, if created, the virtual table.
197049*/
197050SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(
197051 sqlite3 *db,
197052 Fts3Hash *pHash,
197053 const char *zName
197054){
197055 int rc = SQLITE_OK0;
197056 void *p = (void *)pHash;
197057 const int any = SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000;
197058
197059#ifdef SQLITE_TEST
197060 char *zTest = 0;
197061 char *zTest2 = 0;
197062 void *pdb = (void *)db;
197063 zTest = sqlite3_mprintf("%s_test", zName);
197064 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
197065 if( !zTest || !zTest2 ){
197066 rc = SQLITE_NOMEM7;
197067 }
197068#endif
197069
197070 if( SQLITE_OK0==rc ){
197071 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
197072 }
197073 if( SQLITE_OK0==rc ){
197074 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
197075 }
197076#ifdef SQLITE_TEST
197077 if( SQLITE_OK0==rc ){
197078 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
197079 }
197080 if( SQLITE_OK0==rc ){
197081 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
197082 }
197083#endif
197084
197085#ifdef SQLITE_TEST
197086 sqlite3_free(zTest);
197087 sqlite3_free(zTest2);
197088#endif
197089
197090 return rc;
197091}
197092
197093#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
197094
197095/************** End of fts3_tokenizer.c **************************************/
197096/************** Begin file fts3_tokenizer1.c *********************************/
197097/*
197098** 2006 Oct 10
197099**
197100** The author disclaims copyright to this source code. In place of
197101** a legal notice, here is a blessing:
197102**
197103** May you do good and not evil.
197104** May you find forgiveness for yourself and forgive others.
197105** May you share freely, never taking more than you give.
197106**
197107******************************************************************************
197108**
197109** Implementation of the "simple" full-text-search tokenizer.
197110*/
197111
197112/*
197113** The code in this file is only compiled if:
197114**
197115** * The FTS3 module is being built as an extension
197116** (in which case SQLITE_CORE is not defined), or
197117**
197118** * The FTS3 module is being built into the core of
197119** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
197120*/
197121/* #include "fts3Int.h" */
197122#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
197123
197124/* #include <assert.h> */
197125/* #include <stdlib.h> */
197126/* #include <stdio.h> */
197127/* #include <string.h> */
197128
197129/* #include "fts3_tokenizer.h" */
197130
197131typedef struct simple_tokenizer {
197132 sqlite3_tokenizer base;
197133 char delim[128]; /* flag ASCII delimiters */
197134} simple_tokenizer;
197135
197136typedef struct simple_tokenizer_cursor {
197137 sqlite3_tokenizer_cursor base;
197138 const char *pInput; /* input we are tokenizing */
197139 int nBytes; /* size of the input */
197140 int iOffset; /* current position in pInput */
197141 int iToken; /* index of next token to be returned */
197142 char *pToken; /* storage for current token */
197143 int nTokenAllocated; /* space allocated to zToken buffer */
197144} simple_tokenizer_cursor;
197145
197146
197147static int simpleDelim(simple_tokenizer *t, unsigned char c){
197148 return c<0x80 && t->delim[c];
197149}
197150static int fts3_isalnum(int x){
197151 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
197152}
197153
197154/*
197155** Create a new tokenizer instance.
197156*/
197157static int simpleCreate(
197158 int argc, const char * const *argv,
197159 sqlite3_tokenizer **ppTokenizer
197160){
197161 simple_tokenizer *t;
197162
197163 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
197164 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
197165 memset(t, 0, sizeof(*t));
197166
197167 /* TODO(shess) Delimiters need to remain the same from run to run,
197168 ** else we need to reindex. One solution would be a meta-table to
197169 ** track such information in the database, then we'd only want this
197170 ** information on the initial create.
197171 */
197172 if( argc>1 ){
197173 int i, n = (int)strlen(argv[1]);
197174 for(i=0; i<n; i++){
197175 unsigned char ch = argv[1][i];
197176 /* We explicitly don't support UTF-8 delimiters for now. */
197177 if( ch>=0x80 ){
197178 sqlite3_free(t);
197179 return SQLITE_ERROR1;
197180 }
197181 t->delim[ch] = 1;
197182 }
197183 } else {
197184 /* Mark non-alphanumeric ASCII characters as delimiters */
197185 int i;
197186 for(i=1; i<0x80; i++){
197187 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
197188 }
197189 }
197190
197191 *ppTokenizer = &t->base;
197192 return SQLITE_OK0;
197193}
197194
197195/*
197196** Destroy a tokenizer
197197*/
197198static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
197199 sqlite3_free(pTokenizer);
197200 return SQLITE_OK0;
197201}
197202
197203/*
197204** Prepare to begin tokenizing a particular string. The input
197205** string to be tokenized is pInput[0..nBytes-1]. A cursor
197206** used to incrementally tokenize this string is returned in
197207** *ppCursor.
197208*/
197209static int simpleOpen(
197210 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
197211 const char *pInput, int nBytes, /* String to be tokenized */
197212 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
197213){
197214 simple_tokenizer_cursor *c;
197215
197216 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
197217
197218 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
197219 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
197220
197221 c->pInput = pInput;
197222 if( pInput==0 ){
197223 c->nBytes = 0;
197224 }else if( nBytes<0 ){
197225 c->nBytes = (int)strlen(pInput);
197226 }else{
197227 c->nBytes = nBytes;
197228 }
197229 c->iOffset = 0; /* start tokenizing at the beginning */
197230 c->iToken = 0;
197231 c->pToken = NULL((void*)0); /* no space allocated, yet. */
197232 c->nTokenAllocated = 0;
197233
197234 *ppCursor = &c->base;
197235 return SQLITE_OK0;
197236}
197237
197238/*
197239** Close a tokenization cursor previously opened by a call to
197240** simpleOpen() above.
197241*/
197242static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
197243 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
197244 sqlite3_free(c->pToken);
197245 sqlite3_free(c);
197246 return SQLITE_OK0;
197247}
197248
197249/*
197250** Extract the next token from a tokenization cursor. The cursor must
197251** have been opened by a prior call to simpleOpen().
197252*/
197253static int simpleNext(
197254 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
197255 const char **ppToken, /* OUT: *ppToken is the token text */
197256 int *pnBytes, /* OUT: Number of bytes in token */
197257 int *piStartOffset, /* OUT: Starting offset of token */
197258 int *piEndOffset, /* OUT: Ending offset of token */
197259 int *piPosition /* OUT: Position integer of token */
197260){
197261 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
197262 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
197263 unsigned char *p = (unsigned char *)c->pInput;
197264
197265 while( c->iOffset<c->nBytes ){
197266 int iStartOffset;
197267
197268 /* Scan past delimiter characters */
197269 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
197270 c->iOffset++;
197271 }
197272
197273 /* Count non-delimiter characters. */
197274 iStartOffset = c->iOffset;
197275 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
197276 c->iOffset++;
197277 }
197278
197279 if( c->iOffset>iStartOffset ){
197280 int i, n = c->iOffset-iStartOffset;
197281 if( n>c->nTokenAllocated ){
197282 char *pNew;
197283 c->nTokenAllocated = n+20;
197284 pNew = sqlite3_realloc64(c->pToken, c->nTokenAllocated);
197285 if( !pNew ) return SQLITE_NOMEM7;
197286 c->pToken = pNew;
197287 }
197288 for(i=0; i<n; i++){
197289 /* TODO(shess) This needs expansion to handle UTF-8
197290 ** case-insensitivity.
197291 */
197292 unsigned char ch = p[iStartOffset+i];
197293 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
197294 }
197295 *ppToken = c->pToken;
197296 *pnBytes = n;
197297 *piStartOffset = iStartOffset;
197298 *piEndOffset = c->iOffset;
197299 *piPosition = c->iToken++;
197300
197301 return SQLITE_OK0;
197302 }
197303 }
197304 return SQLITE_DONE101;
197305}
197306
197307/*
197308** The set of routines that implement the simple tokenizer
197309*/
197310static const sqlite3_tokenizer_module simpleTokenizerModule = {
197311 0,
197312 simpleCreate,
197313 simpleDestroy,
197314 simpleOpen,
197315 simpleClose,
197316 simpleNext,
197317 0,
197318};
197319
197320/*
197321** Allocate a new simple tokenizer. Return a pointer to the new
197322** tokenizer in *ppModule
197323*/
197324SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(
197325 sqlite3_tokenizer_module const**ppModule
197326){
197327 *ppModule = &simpleTokenizerModule;
197328}
197329
197330#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
197331
197332/************** End of fts3_tokenizer1.c *************************************/
197333/************** Begin file fts3_tokenize_vtab.c ******************************/
197334/*
197335** 2013 Apr 22
197336**
197337** The author disclaims copyright to this source code. In place of
197338** a legal notice, here is a blessing:
197339**
197340** May you do good and not evil.
197341** May you find forgiveness for yourself and forgive others.
197342** May you share freely, never taking more than you give.
197343**
197344******************************************************************************
197345**
197346** This file contains code for the "fts3tokenize" virtual table module.
197347** An fts3tokenize virtual table is created as follows:
197348**
197349** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
197350** <tokenizer-name>, <arg-1>, ...
197351** );
197352**
197353** The table created has the following schema:
197354**
197355** CREATE TABLE <tbl>(input, token, start, end, position)
197356**
197357** When queried, the query must include a WHERE clause of type:
197358**
197359** input = <string>
197360**
197361** The virtual table module tokenizes this <string>, using the FTS3
197362** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
197363** statement and returns one row for each token in the result. With
197364** fields set as follows:
197365**
197366** input: Always set to a copy of <string>
197367** token: A token from the input.
197368** start: Byte offset of the token within the input <string>.
197369** end: Byte offset of the byte immediately following the end of the
197370** token within the input string.
197371** pos: Token offset of token within input.
197372**
197373*/
197374/* #include "fts3Int.h" */
197375#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
197376
197377/* #include <string.h> */
197378/* #include <assert.h> */
197379
197380typedef struct Fts3tokTable Fts3tokTable;
197381typedef struct Fts3tokCursor Fts3tokCursor;
197382
197383/*
197384** Virtual table structure.
197385*/
197386struct Fts3tokTable {
197387 sqlite3_vtab base; /* Base class used by SQLite core */
197388 const sqlite3_tokenizer_module *pMod;
197389 sqlite3_tokenizer *pTok;
197390};
197391
197392/*
197393** Virtual table cursor structure.
197394*/
197395struct Fts3tokCursor {
197396 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
197397 char *zInput; /* Input string */
197398 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
197399 int iRowid; /* Current 'rowid' value */
197400 const char *zToken; /* Current 'token' value */
197401 int nToken; /* Size of zToken in bytes */
197402 int iStart; /* Current 'start' value */
197403 int iEnd; /* Current 'end' value */
197404 int iPos; /* Current 'pos' value */
197405};
197406
197407/*
197408** Query FTS for the tokenizer implementation named zName.
197409*/
197410static int fts3tokQueryTokenizer(
197411 Fts3Hash *pHash,
197412 const char *zName,
197413 const sqlite3_tokenizer_module **pp,
197414 char **pzErr
197415){
197416 sqlite3_tokenizer_module *p;
197417 int nName = (int)strlen(zName);
197418
197419 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
197420 if( !p ){
197421 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
197422 return SQLITE_ERROR1;
197423 }
197424
197425 *pp = p;
197426 return SQLITE_OK0;
197427}
197428
197429/*
197430** The second argument, argv[], is an array of pointers to nul-terminated
197431** strings. This function makes a copy of the array and strings into a
197432** single block of memory. It then dequotes any of the strings that appear
197433** to be quoted.
197434**
197435** If successful, output parameter *pazDequote is set to point at the
197436** array of dequoted strings and SQLITE_OK is returned. The caller is
197437** responsible for eventually calling sqlite3_free() to free the array
197438** in this case. Or, if an error occurs, an SQLite error code is returned.
197439** The final value of *pazDequote is undefined in this case.
197440*/
197441static int fts3tokDequoteArray(
197442 int argc, /* Number of elements in argv[] */
197443 const char * const *argv, /* Input array */
197444 char ***pazDequote /* Output array */
197445){
197446 int rc = SQLITE_OK0; /* Return code */
197447 if( argc==0 ){
197448 *pazDequote = 0;
197449 }else{
197450 int i;
197451 int nByte = 0;
197452 char **azDequote;
197453
197454 for(i=0; i<argc; i++){
197455 nByte += (int)(strlen(argv[i]) + 1);
197456 }
197457
197458 *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
197459 if( azDequote==0 ){
197460 rc = SQLITE_NOMEM7;
197461 }else{
197462 char *pSpace = (char *)&azDequote[argc];
197463 for(i=0; i<argc; i++){
197464 int n = (int)strlen(argv[i]);
197465 azDequote[i] = pSpace;
197466 memcpy(pSpace, argv[i], n+1);
197467 sqlite3Fts3Dequote(pSpace);
197468 pSpace += (n+1);
197469 }
197470 }
197471 }
197472
197473 return rc;
197474}
197475
197476/*
197477** Schema of the tokenizer table.
197478*/
197479#define FTS3_TOK_SCHEMA"CREATE TABLE x(input, token, start, end, position)" "CREATE TABLE x(input, token, start, end, position)"
197480
197481/*
197482** This function does all the work for both the xConnect and xCreate methods.
197483** These tables have no persistent representation of their own, so xConnect
197484** and xCreate are identical operations.
197485**
197486** argv[0]: module name
197487** argv[1]: database name
197488** argv[2]: table name
197489** argv[3]: first argument (tokenizer name)
197490*/
197491static int fts3tokConnectMethod(
197492 sqlite3 *db, /* Database connection */
197493 void *pHash, /* Hash table of tokenizers */
197494 int argc, /* Number of elements in argv array */
197495 const char * const *argv, /* xCreate/xConnect argument array */
197496 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
197497 char **pzErr /* OUT: sqlite3_malloc'd error message */
197498){
197499 Fts3tokTable *pTab = 0;
197500 const sqlite3_tokenizer_module *pMod = 0;
197501 sqlite3_tokenizer *pTok = 0;
197502 int rc;
197503 char **azDequote = 0;
197504 int nDequote;
197505
197506 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA"CREATE TABLE x(input, token, start, end, position)");
197507 if( rc!=SQLITE_OK0 ) return rc;
197508
197509 nDequote = argc-3;
197510 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
197511
197512 if( rc==SQLITE_OK0 ){
197513 const char *zModule;
197514 if( nDequote<1 ){
197515 zModule = "simple";
197516 }else{
197517 zModule = azDequote[0];
197518 }
197519 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
197520 }
197521
197522 assert( (rc==SQLITE_OK)==(pMod!=0) )((void) (0));
197523 if( rc==SQLITE_OK0 ){
197524 const char * const *azArg = 0;
197525 if( nDequote>1 ) azArg = (const char * const *)&azDequote[1];
197526 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
197527 }
197528
197529 if( rc==SQLITE_OK0 ){
197530 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
197531 if( pTab==0 ){
197532 rc = SQLITE_NOMEM7;
197533 }
197534 }
197535
197536 if( rc==SQLITE_OK0 ){
197537 memset(pTab, 0, sizeof(Fts3tokTable));
197538 pTab->pMod = pMod;
197539 pTab->pTok = pTok;
197540 *ppVtab = &pTab->base;
197541 }else{
197542 if( pTok ){
197543 pMod->xDestroy(pTok);
197544 }
197545 }
197546
197547 sqlite3_free(azDequote);
197548 return rc;
197549}
197550
197551/*
197552** This function does the work for both the xDisconnect and xDestroy methods.
197553** These tables have no persistent representation of their own, so xDisconnect
197554** and xDestroy are identical operations.
197555*/
197556static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
197557 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
197558
197559 pTab->pMod->xDestroy(pTab->pTok);
197560 sqlite3_free(pTab);
197561 return SQLITE_OK0;
197562}
197563
197564/*
197565** xBestIndex - Analyze a WHERE and ORDER BY clause.
197566*/
197567static int fts3tokBestIndexMethod(
197568 sqlite3_vtab *pVTab,
197569 sqlite3_index_info *pInfo
197570){
197571 int i;
197572 UNUSED_PARAMETER(pVTab)(void)(pVTab);
197573
197574 for(i=0; i<pInfo->nConstraint; i++){
197575 if( pInfo->aConstraint[i].usable
197576 && pInfo->aConstraint[i].iColumn==0
197577 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ2
197578 ){
197579 pInfo->idxNum = 1;
197580 pInfo->aConstraintUsage[i].argvIndex = 1;
197581 pInfo->aConstraintUsage[i].omit = 1;
197582 pInfo->estimatedCost = 1;
197583 return SQLITE_OK0;
197584 }
197585 }
197586
197587 pInfo->idxNum = 0;
197588 assert( pInfo->estimatedCost>1000000.0 )((void) (0));
197589
197590 return SQLITE_OK0;
197591}
197592
197593/*
197594** xOpen - Open a cursor.
197595*/
197596static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
197597 Fts3tokCursor *pCsr;
197598 UNUSED_PARAMETER(pVTab)(void)(pVTab);
197599
197600 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
197601 if( pCsr==0 ){
197602 return SQLITE_NOMEM7;
197603 }
197604 memset(pCsr, 0, sizeof(Fts3tokCursor));
197605
197606 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
197607 return SQLITE_OK0;
197608}
197609
197610/*
197611** Reset the tokenizer cursor passed as the only argument. As if it had
197612** just been returned by fts3tokOpenMethod().
197613*/
197614static void fts3tokResetCursor(Fts3tokCursor *pCsr){
197615 if( pCsr->pCsr ){
197616 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
197617 pTab->pMod->xClose(pCsr->pCsr);
197618 pCsr->pCsr = 0;
197619 }
197620 sqlite3_free(pCsr->zInput);
197621 pCsr->zInput = 0;
197622 pCsr->zToken = 0;
197623 pCsr->nToken = 0;
197624 pCsr->iStart = 0;
197625 pCsr->iEnd = 0;
197626 pCsr->iPos = 0;
197627 pCsr->iRowid = 0;
197628}
197629
197630/*
197631** xClose - Close a cursor.
197632*/
197633static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
197634 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
197635
197636 fts3tokResetCursor(pCsr);
197637 sqlite3_free(pCsr);
197638 return SQLITE_OK0;
197639}
197640
197641/*
197642** xNext - Advance the cursor to the next row, if any.
197643*/
197644static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
197645 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
197646 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
197647 int rc; /* Return code */
197648
197649 pCsr->iRowid++;
197650 rc = pTab->pMod->xNext(pCsr->pCsr,
197651 &pCsr->zToken, &pCsr->nToken,
197652 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
197653 );
197654
197655 if( rc!=SQLITE_OK0 ){
197656 fts3tokResetCursor(pCsr);
197657 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
197658 }
197659
197660 return rc;
197661}
197662
197663/*
197664** xFilter - Initialize a cursor to point at the start of its data.
197665*/
197666static int fts3tokFilterMethod(
197667 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
197668 int idxNum, /* Strategy index */
197669 const char *idxStr, /* Unused */
197670 int nVal, /* Number of elements in apVal */
197671 sqlite3_value **apVal /* Arguments for the indexing scheme */
197672){
197673 int rc = SQLITE_ERROR1;
197674 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
197675 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
197676 UNUSED_PARAMETER(idxStr)(void)(idxStr);
197677 UNUSED_PARAMETER(nVal)(void)(nVal);
197678
197679 fts3tokResetCursor(pCsr);
197680 if( idxNum==1 ){
197681 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
197682 int nByte = sqlite3_value_bytes(apVal[0]);
197683 pCsr->zInput = sqlite3_malloc64(nByte+1);
197684 if( pCsr->zInput==0 ){
197685 rc = SQLITE_NOMEM7;
197686 }else{
197687 if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
197688 pCsr->zInput[nByte] = 0;
197689 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
197690 if( rc==SQLITE_OK0 ){
197691 pCsr->pCsr->pTokenizer = pTab->pTok;
197692 }
197693 }
197694 }
197695
197696 if( rc!=SQLITE_OK0 ) return rc;
197697 return fts3tokNextMethod(pCursor);
197698}
197699
197700/*
197701** xEof - Return true if the cursor is at EOF, or false otherwise.
197702*/
197703static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
197704 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
197705 return (pCsr->zToken==0);
197706}
197707
197708/*
197709** xColumn - Return a column value.
197710*/
197711static int fts3tokColumnMethod(
197712 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
197713 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
197714 int iCol /* Index of column to read value from */
197715){
197716 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
197717
197718 /* CREATE TABLE x(input, token, start, end, position) */
197719 switch( iCol ){
197720 case 0:
197721 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
197722 break;
197723 case 1:
197724 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
197725 break;
197726 case 2:
197727 sqlite3_result_int(pCtx, pCsr->iStart);
197728 break;
197729 case 3:
197730 sqlite3_result_int(pCtx, pCsr->iEnd);
197731 break;
197732 default:
197733 assert( iCol==4 )((void) (0));
197734 sqlite3_result_int(pCtx, pCsr->iPos);
197735 break;
197736 }
197737 return SQLITE_OK0;
197738}
197739
197740/*
197741** xRowid - Return the current rowid for the cursor.
197742*/
197743static int fts3tokRowidMethod(
197744 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
197745 sqlite_int64 *pRowid /* OUT: Rowid value */
197746){
197747 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
197748 *pRowid = (sqlite3_int64)pCsr->iRowid;
197749 return SQLITE_OK0;
197750}
197751
197752/*
197753** Register the fts3tok module with database connection db. Return SQLITE_OK
197754** if successful or an error code if sqlite3_create_module() fails.
197755*/
197756SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
197757 static const sqlite3_module fts3tok_module = {
197758 0, /* iVersion */
197759 fts3tokConnectMethod, /* xCreate */
197760 fts3tokConnectMethod, /* xConnect */
197761 fts3tokBestIndexMethod, /* xBestIndex */
197762 fts3tokDisconnectMethod, /* xDisconnect */
197763 fts3tokDisconnectMethod, /* xDestroy */
197764 fts3tokOpenMethod, /* xOpen */
197765 fts3tokCloseMethod, /* xClose */
197766 fts3tokFilterMethod, /* xFilter */
197767 fts3tokNextMethod, /* xNext */
197768 fts3tokEofMethod, /* xEof */
197769 fts3tokColumnMethod, /* xColumn */
197770 fts3tokRowidMethod, /* xRowid */
197771 0, /* xUpdate */
197772 0, /* xBegin */
197773 0, /* xSync */
197774 0, /* xCommit */
197775 0, /* xRollback */
197776 0, /* xFindFunction */
197777 0, /* xRename */
197778 0, /* xSavepoint */
197779 0, /* xRelease */
197780 0, /* xRollbackTo */
197781 0, /* xShadowName */
197782 0 /* xIntegrity */
197783 };
197784 int rc; /* Return code */
197785
197786 rc = sqlite3_create_module_v2(
197787 db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy
197788 );
197789 return rc;
197790}
197791
197792#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
197793
197794/************** End of fts3_tokenize_vtab.c **********************************/
197795/************** Begin file fts3_write.c **************************************/
197796/*
197797** 2009 Oct 23
197798**
197799** The author disclaims copyright to this source code. In place of
197800** a legal notice, here is a blessing:
197801**
197802** May you do good and not evil.
197803** May you find forgiveness for yourself and forgive others.
197804** May you share freely, never taking more than you give.
197805**
197806******************************************************************************
197807**
197808** This file is part of the SQLite FTS3 extension module. Specifically,
197809** this file contains code to insert, update and delete rows from FTS3
197810** tables. It also contains code to merge FTS3 b-tree segments. Some
197811** of the sub-routines used to merge segments are also used by the query
197812** code in fts3.c.
197813*/
197814
197815/* #include "fts3Int.h" */
197816#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
197817
197818/* #include <string.h> */
197819/* #include <assert.h> */
197820/* #include <stdlib.h> */
197821/* #include <stdio.h> */
197822
197823#define FTS_MAX_APPENDABLE_HEIGHT16 16
197824
197825/*
197826** When full-text index nodes are loaded from disk, the buffer that they
197827** are loaded into has the following number of bytes of padding at the end
197828** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
197829** of 920 bytes is allocated for it.
197830**
197831** This means that if we have a pointer into a buffer containing node data,
197832** it is always safe to read up to two varints from it without risking an
197833** overread, even if the node data is corrupted.
197834*/
197835#define FTS3_NODE_PADDING(10*2) (FTS3_VARINT_MAX10*2)
197836
197837/*
197838** Under certain circumstances, b-tree nodes (doclists) can be loaded into
197839** memory incrementally instead of all at once. This can be a big performance
197840** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
197841** method before retrieving all query results (as may happen, for example,
197842** if a query has a LIMIT clause).
197843**
197844** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
197845** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
197846** The code is written so that the hard lower-limit for each of these values
197847** is 1. Clearly such small values would be inefficient, but can be useful
197848** for testing purposes.
197849**
197850** If this module is built with SQLITE_TEST defined, these constants may
197851** be overridden at runtime for testing purposes. File fts3_test.c contains
197852** a Tcl interface to read and write the values.
197853*/
197854#ifdef SQLITE_TEST
197855int test_fts3_node_chunksize = (4*1024);
197856int test_fts3_node_chunk_threshold = (4*1024)*4;
197857# define FTS3_NODE_CHUNKSIZE(4*1024) test_fts3_node_chunksize
197858# define FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4) test_fts3_node_chunk_threshold
197859#else
197860# define FTS3_NODE_CHUNKSIZE(4*1024) (4*1024)
197861# define FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4) (FTS3_NODE_CHUNKSIZE(4*1024)*4)
197862#endif
197863
197864/*
197865** The values that may be meaningfully bound to the :1 parameter in
197866** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
197867*/
197868#define FTS_STAT_DOCTOTAL0 0
197869#define FTS_STAT_INCRMERGEHINT1 1
197870#define FTS_STAT_AUTOINCRMERGE2 2
197871
197872/*
197873** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
197874** and incremental merge operation that takes place. This is used for
197875** debugging FTS only, it should not usually be turned on in production
197876** systems.
197877*/
197878#ifdef FTS3_LOG_MERGES
197879static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
197880 sqlite3_log(SQLITE_OK0, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
197881}
197882#else
197883#define fts3LogMerge(x, y)
197884#endif
197885
197886
197887typedef struct PendingList PendingList;
197888typedef struct SegmentNode SegmentNode;
197889typedef struct SegmentWriter SegmentWriter;
197890
197891/*
197892** An instance of the following data structure is used to build doclists
197893** incrementally. See function fts3PendingListAppend() for details.
197894*/
197895struct PendingList {
197896 int nData;
197897 char *aData;
197898 int nSpace;
197899 sqlite3_int64 iLastDocid;
197900 sqlite3_int64 iLastCol;
197901 sqlite3_int64 iLastPos;
197902};
197903
197904
197905/*
197906** Each cursor has a (possibly empty) linked list of the following objects.
197907*/
197908struct Fts3DeferredToken {
197909 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
197910 int iCol; /* Column token must occur in */
197911 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
197912 PendingList *pList; /* Doclist is assembled here */
197913};
197914
197915/*
197916** An instance of this structure is used to iterate through the terms on
197917** a contiguous set of segment b-tree leaf nodes. Although the details of
197918** this structure are only manipulated by code in this file, opaque handles
197919** of type Fts3SegReader* are also used by code in fts3.c to iterate through
197920** terms when querying the full-text index. See functions:
197921**
197922** sqlite3Fts3SegReaderNew()
197923** sqlite3Fts3SegReaderFree()
197924** sqlite3Fts3SegReaderIterate()
197925**
197926** Methods used to manipulate Fts3SegReader structures:
197927**
197928** fts3SegReaderNext()
197929** fts3SegReaderFirstDocid()
197930** fts3SegReaderNextDocid()
197931*/
197932struct Fts3SegReader {
197933 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
197934 u8 bLookup; /* True for a lookup only */
197935 u8 rootOnly; /* True for a root-only reader */
197936
197937 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
197938 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
197939 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
197940 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
197941
197942 char *aNode; /* Pointer to node data (or NULL) */
197943 int nNode; /* Size of buffer at aNode (or 0) */
197944 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
197945 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
197946
197947 Fts3HashElem **ppNextElem;
197948
197949 /* Variables set by fts3SegReaderNext(). These may be read directly
197950 ** by the caller. They are valid from the time SegmentReaderNew() returns
197951 ** until SegmentReaderNext() returns something other than SQLITE_OK
197952 ** (i.e. SQLITE_DONE).
197953 */
197954 int nTerm; /* Number of bytes in current term */
197955 char *zTerm; /* Pointer to current term */
197956 int nTermAlloc; /* Allocated size of zTerm buffer */
197957 char *aDoclist; /* Pointer to doclist of current entry */
197958 int nDoclist; /* Size of doclist in current entry */
197959
197960 /* The following variables are used by fts3SegReaderNextDocid() to iterate
197961 ** through the current doclist (aDoclist/nDoclist).
197962 */
197963 char *pOffsetList;
197964 int nOffsetList; /* For descending pending seg-readers only */
197965 sqlite3_int64 iDocid;
197966};
197967
197968#define fts3SegReaderIsPending(p)((p)->ppNextElem!=0) ((p)->ppNextElem!=0)
197969#define fts3SegReaderIsRootOnly(p)((p)->rootOnly!=0) ((p)->rootOnly!=0)
197970
197971/*
197972** An instance of this structure is used to create a segment b-tree in the
197973** database. The internal details of this type are only accessed by the
197974** following functions:
197975**
197976** fts3SegWriterAdd()
197977** fts3SegWriterFlush()
197978** fts3SegWriterFree()
197979*/
197980struct SegmentWriter {
197981 SegmentNode *pTree; /* Pointer to interior tree structure */
197982 sqlite3_int64 iFirst; /* First slot in %_segments written */
197983 sqlite3_int64 iFree; /* Next free slot in %_segments */
197984 char *zTerm; /* Pointer to previous term buffer */
197985 int nTerm; /* Number of bytes in zTerm */
197986 int nMalloc; /* Size of malloc'd buffer at zMalloc */
197987 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
197988 int nSize; /* Size of allocation at aData */
197989 int nData; /* Bytes of data in aData */
197990 char *aData; /* Pointer to block from malloc() */
197991 i64 nLeafData; /* Number of bytes of leaf data written */
197992};
197993
197994/*
197995** Type SegmentNode is used by the following three functions to create
197996** the interior part of the segment b+-tree structures (everything except
197997** the leaf nodes). These functions and type are only ever used by code
197998** within the fts3SegWriterXXX() family of functions described above.
197999**
198000** fts3NodeAddTerm()
198001** fts3NodeWrite()
198002** fts3NodeFree()
198003**
198004** When a b+tree is written to the database (either as a result of a merge
198005** or the pending-terms table being flushed), leaves are written into the
198006** database file as soon as they are completely populated. The interior of
198007** the tree is assembled in memory and written out only once all leaves have
198008** been populated and stored. This is Ok, as the b+-tree fanout is usually
198009** very large, meaning that the interior of the tree consumes relatively
198010** little memory.
198011*/
198012struct SegmentNode {
198013 SegmentNode *pParent; /* Parent node (or NULL for root node) */
198014 SegmentNode *pRight; /* Pointer to right-sibling */
198015 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
198016 int nEntry; /* Number of terms written to node so far */
198017 char *zTerm; /* Pointer to previous term buffer */
198018 int nTerm; /* Number of bytes in zTerm */
198019 int nMalloc; /* Size of malloc'd buffer at zMalloc */
198020 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
198021 int nData; /* Bytes of valid data so far */
198022 char *aData; /* Node data */
198023};
198024
198025/*
198026** Valid values for the second argument to fts3SqlStmt().
198027*/
198028#define SQL_DELETE_CONTENT0 0
198029#define SQL_IS_EMPTY1 1
198030#define SQL_DELETE_ALL_CONTENT2 2
198031#define SQL_DELETE_ALL_SEGMENTS3 3
198032#define SQL_DELETE_ALL_SEGDIR4 4
198033#define SQL_DELETE_ALL_DOCSIZE5 5
198034#define SQL_DELETE_ALL_STAT6 6
198035#define SQL_SELECT_CONTENT_BY_ROWID7 7
198036#define SQL_NEXT_SEGMENT_INDEX8 8
198037#define SQL_INSERT_SEGMENTS9 9
198038#define SQL_NEXT_SEGMENTS_ID10 10
198039#define SQL_INSERT_SEGDIR11 11
198040#define SQL_SELECT_LEVEL12 12
198041#define SQL_SELECT_LEVEL_RANGE13 13
198042#define SQL_SELECT_LEVEL_COUNT14 14
198043#define SQL_SELECT_SEGDIR_MAX_LEVEL15 15
198044#define SQL_DELETE_SEGDIR_LEVEL16 16
198045#define SQL_DELETE_SEGMENTS_RANGE17 17
198046#define SQL_CONTENT_INSERT18 18
198047#define SQL_DELETE_DOCSIZE19 19
198048#define SQL_REPLACE_DOCSIZE20 20
198049#define SQL_SELECT_DOCSIZE21 21
198050#define SQL_SELECT_STAT22 22
198051#define SQL_REPLACE_STAT23 23
198052
198053#define SQL_SELECT_ALL_PREFIX_LEVEL24 24
198054#define SQL_DELETE_ALL_TERMS_SEGDIR25 25
198055#define SQL_DELETE_SEGDIR_RANGE26 26
198056#define SQL_SELECT_ALL_LANGID27 27
198057#define SQL_FIND_MERGE_LEVEL28 28
198058#define SQL_MAX_LEAF_NODE_ESTIMATE29 29
198059#define SQL_DELETE_SEGDIR_ENTRY30 30
198060#define SQL_SHIFT_SEGDIR_ENTRY31 31
198061#define SQL_SELECT_SEGDIR32 32
198062#define SQL_CHOMP_SEGDIR33 33
198063#define SQL_SEGMENT_IS_APPENDABLE34 34
198064#define SQL_SELECT_INDEXES35 35
198065#define SQL_SELECT_MXLEVEL36 36
198066
198067#define SQL_SELECT_LEVEL_RANGE237 37
198068#define SQL_UPDATE_LEVEL_IDX38 38
198069#define SQL_UPDATE_LEVEL39 39
198070
198071/*
198072** This function is used to obtain an SQLite prepared statement handle
198073** for the statement identified by the second argument. If successful,
198074** *pp is set to the requested statement handle and SQLITE_OK returned.
198075** Otherwise, an SQLite error code is returned and *pp is set to 0.
198076**
198077** If argument apVal is not NULL, then it must point to an array with
198078** at least as many entries as the requested statement has bound
198079** parameters. The values are bound to the statements parameters before
198080** returning.
198081*/
198082static int fts3SqlStmt(
198083 Fts3Table *p, /* Virtual table handle */
198084 int eStmt, /* One of the SQL_XXX constants above */
198085 sqlite3_stmt **pp, /* OUT: Statement handle */
198086 sqlite3_value **apVal /* Values to bind to statement */
198087){
198088 const char *azSql[] = {
198089/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
198090/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
198091/* 2 */ "DELETE FROM %Q.'%q_content'",
198092/* 3 */ "DELETE FROM %Q.'%q_segments'",
198093/* 4 */ "DELETE FROM %Q.'%q_segdir'",
198094/* 5 */ "DELETE FROM %Q.'%q_docsize'",
198095/* 6 */ "DELETE FROM %Q.'%q_stat'",
198096/* 7 */ "SELECT %s WHERE rowid=?",
198097/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
198098/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
198099/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
198100/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
198101
198102 /* Return segments in order from oldest to newest.*/
198103/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
198104 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
198105/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
198106 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
198107 "ORDER BY level DESC, idx ASC",
198108
198109/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
198110/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
198111
198112/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
198113/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
198114/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
198115/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
198116/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
198117/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
198118/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
198119/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
198120/* 24 */ "",
198121/* 25 */ "",
198122
198123/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
198124/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
198125
198126/* This statement is used to determine which level to read the input from
198127** when performing an incremental merge. It returns the absolute level number
198128** of the oldest level in the db that contains at least ? segments. Or,
198129** if no level in the FTS index contains more than ? segments, the statement
198130** returns zero rows. */
198131/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
198132 " GROUP BY level HAVING cnt>=?"
198133 " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1",
198134
198135/* Estimate the upper limit on the number of leaf nodes in a new segment
198136** created by merging the oldest :2 segments from absolute level :1. See
198137** function sqlite3Fts3Incrmerge() for details. */
198138/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
198139 " FROM (SELECT * FROM %Q.'%q_segdir' "
198140 " WHERE level = ? ORDER BY idx ASC LIMIT ?"
198141 " )",
198142
198143/* SQL_DELETE_SEGDIR_ENTRY
198144** Delete the %_segdir entry on absolute level :1 with index :2. */
198145/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
198146
198147/* SQL_SHIFT_SEGDIR_ENTRY
198148** Modify the idx value for the segment with idx=:3 on absolute level :2
198149** to :1. */
198150/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
198151
198152/* SQL_SELECT_SEGDIR
198153** Read a single entry from the %_segdir table. The entry from absolute
198154** level :1 with index value :2. */
198155/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
198156 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
198157
198158/* SQL_CHOMP_SEGDIR
198159** Update the start_block (:1) and root (:2) fields of the %_segdir
198160** entry located on absolute level :3 with index :4. */
198161/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
198162 "WHERE level = ? AND idx = ?",
198163
198164/* SQL_SEGMENT_IS_APPENDABLE
198165** Return a single row if the segment with end_block=? is appendable. Or
198166** no rows otherwise. */
198167/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
198168
198169/* SQL_SELECT_INDEXES
198170** Return the list of valid segment indexes for absolute level ? */
198171/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
198172
198173/* SQL_SELECT_MXLEVEL
198174** Return the largest relative level in the FTS index or indexes. */
198175/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
198176
198177 /* Return segments in order from oldest to newest.*/
198178/* 37 */ "SELECT level, idx, end_block "
198179 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
198180 "ORDER BY level DESC, idx ASC",
198181
198182 /* Update statements used while promoting segments */
198183/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
198184 "WHERE level=? AND idx=?",
198185/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
198186
198187 };
198188 int rc = SQLITE_OK0;
198189 sqlite3_stmt *pStmt;
198190
198191 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) )((void) (0));
198192 assert( eStmt<SizeofArray(azSql) && eStmt>=0 )((void) (0));
198193
198194 pStmt = p->aStmt[eStmt];
198195 if( !pStmt ){
198196 int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
198197 char *zSql;
198198 if( eStmt==SQL_CONTENT_INSERT18 ){
198199 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
198200 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID7 ){
198201 f &= ~SQLITE_PREPARE_NO_VTAB0x04;
198202 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
198203 }else{
198204 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
198205 }
198206 if( !zSql ){
198207 rc = SQLITE_NOMEM7;
198208 }else{
198209 rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL((void*)0));
198210 sqlite3_free(zSql);
198211 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
198212 p->aStmt[eStmt] = pStmt;
198213 }
198214 }
198215 if( apVal ){
198216 int i;
198217 int nParam = sqlite3_bind_parameter_count(pStmt);
198218 for(i=0; rc==SQLITE_OK0 && i<nParam; i++){
198219 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
198220 }
198221 }
198222 *pp = pStmt;
198223 return rc;
198224}
198225
198226
198227static int fts3SelectDocsize(
198228 Fts3Table *pTab, /* FTS3 table handle */
198229 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
198230 sqlite3_stmt **ppStmt /* OUT: Statement handle */
198231){
198232 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
198233 int rc; /* Return code */
198234
198235 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE21, &pStmt, 0);
198236 if( rc==SQLITE_OK0 ){
198237 sqlite3_bind_int64(pStmt, 1, iDocid);
198238 rc = sqlite3_step(pStmt);
198239 if( rc!=SQLITE_ROW100 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4 ){
198240 rc = sqlite3_reset(pStmt);
198241 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
198242 pStmt = 0;
198243 }else{
198244 rc = SQLITE_OK0;
198245 }
198246 }
198247
198248 *ppStmt = pStmt;
198249 return rc;
198250}
198251
198252SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(
198253 Fts3Table *pTab, /* Fts3 table handle */
198254 sqlite3_stmt **ppStmt /* OUT: Statement handle */
198255){
198256 sqlite3_stmt *pStmt = 0;
198257 int rc;
198258 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT22, &pStmt, 0);
198259 if( rc==SQLITE_OK0 ){
198260 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
198261 if( sqlite3_step(pStmt)!=SQLITE_ROW100
198262 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4
198263 ){
198264 rc = sqlite3_reset(pStmt);
198265 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
198266 pStmt = 0;
198267 }
198268 }
198269 *ppStmt = pStmt;
198270 return rc;
198271}
198272
198273SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(
198274 Fts3Table *pTab, /* Fts3 table handle */
198275 sqlite3_int64 iDocid, /* Docid to read size data for */
198276 sqlite3_stmt **ppStmt /* OUT: Statement handle */
198277){
198278 return fts3SelectDocsize(pTab, iDocid, ppStmt);
198279}
198280
198281/*
198282** Similar to fts3SqlStmt(). Except, after binding the parameters in
198283** array apVal[] to the SQL statement identified by eStmt, the statement
198284** is executed.
198285**
198286** Returns SQLITE_OK if the statement is successfully executed, or an
198287** SQLite error code otherwise.
198288*/
198289static void fts3SqlExec(
198290 int *pRC, /* Result code */
198291 Fts3Table *p, /* The FTS3 table */
198292 int eStmt, /* Index of statement to evaluate */
198293 sqlite3_value **apVal /* Parameters to bind */
198294){
198295 sqlite3_stmt *pStmt;
198296 int rc;
198297 if( *pRC ) return;
198298 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
198299 if( rc==SQLITE_OK0 ){
198300 sqlite3_step(pStmt);
198301 rc = sqlite3_reset(pStmt);
198302 }
198303 *pRC = rc;
198304}
198305
198306
198307/*
198308** This function ensures that the caller has obtained an exclusive
198309** shared-cache table-lock on the %_segdir table. This is required before
198310** writing data to the fts3 table. If this lock is not acquired first, then
198311** the caller may end up attempting to take this lock as part of committing
198312** a transaction, causing SQLite to return SQLITE_LOCKED or
198313** LOCKED_SHAREDCACHEto a COMMIT command.
198314**
198315** It is best to avoid this because if FTS3 returns any error when
198316** committing a transaction, the whole transaction will be rolled back.
198317** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
198318** It can still happen if the user locks the underlying tables directly
198319** instead of accessing them via FTS.
198320*/
198321static int fts3Writelock(Fts3Table *p){
198322 int rc = SQLITE_OK0;
198323
198324 if( p->nPendingData==0 ){
198325 sqlite3_stmt *pStmt;
198326 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL16, &pStmt, 0);
198327 if( rc==SQLITE_OK0 ){
198328 sqlite3_bind_null(pStmt, 1);
198329 sqlite3_step(pStmt);
198330 rc = sqlite3_reset(pStmt);
198331 }
198332 }
198333
198334 return rc;
198335}
198336
198337/*
198338** FTS maintains a separate indexes for each language-id (a 32-bit integer).
198339** Within each language id, a separate index is maintained to store the
198340** document terms, and each configured prefix size (configured the FTS
198341** "prefix=" option). And each index consists of multiple levels ("relative
198342** levels").
198343**
198344** All three of these values (the language id, the specific index and the
198345** level within the index) are encoded in 64-bit integer values stored
198346** in the %_segdir table on disk. This function is used to convert three
198347** separate component values into the single 64-bit integer value that
198348** can be used to query the %_segdir table.
198349**
198350** Specifically, each language-id/index combination is allocated 1024
198351** 64-bit integer level values ("absolute levels"). The main terms index
198352** for language-id 0 is allocate values 0-1023. The first prefix index
198353** (if any) for language-id 0 is allocated values 1024-2047. And so on.
198354** Language 1 indexes are allocated immediately following language 0.
198355**
198356** So, for a system with nPrefix prefix indexes configured, the block of
198357** absolute levels that corresponds to language-id iLangid and index
198358** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
198359*/
198360static sqlite3_int64 getAbsoluteLevel(
198361 Fts3Table *p, /* FTS3 table handle */
198362 int iLangid, /* Language id */
198363 int iIndex, /* Index in p->aIndex[] */
198364 int iLevel /* Level of segments */
198365){
198366 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
198367 assert_fts3_nc( iLangid>=0 )((void) (0));
198368 assert( p->nIndex>0 )((void) (0));
198369 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
198370
198371 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL1024;
198372 return iBase + iLevel;
198373}
198374
198375/*
198376** Set *ppStmt to a statement handle that may be used to iterate through
198377** all rows in the %_segdir table, from oldest to newest. If successful,
198378** return SQLITE_OK. If an error occurs while preparing the statement,
198379** return an SQLite error code.
198380**
198381** There is only ever one instance of this SQL statement compiled for
198382** each FTS3 table.
198383**
198384** The statement returns the following columns from the %_segdir table:
198385**
198386** 0: idx
198387** 1: start_block
198388** 2: leaves_end_block
198389** 3: end_block
198390** 4: root
198391*/
198392SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(
198393 Fts3Table *p, /* FTS3 table */
198394 int iLangid, /* Language being queried */
198395 int iIndex, /* Index for p->aIndex[] */
198396 int iLevel, /* Level to select (relative level) */
198397 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
198398){
198399 int rc;
198400 sqlite3_stmt *pStmt = 0;
198401
198402 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 )((void) (0));
198403 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
198404 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
198405
198406 if( iLevel<0 ){
198407 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
198408 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE13, &pStmt, 0);
198409 if( rc==SQLITE_OK0 ){
198410 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
198411 sqlite3_bind_int64(pStmt, 2,
198412 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
198413 );
198414 }
198415 }else{
198416 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
198417 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL12, &pStmt, 0);
198418 if( rc==SQLITE_OK0 ){
198419 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
198420 }
198421 }
198422 *ppStmt = pStmt;
198423 return rc;
198424}
198425
198426
198427/*
198428** Append a single varint to a PendingList buffer. SQLITE_OK is returned
198429** if successful, or an SQLite error code otherwise.
198430**
198431** This function also serves to allocate the PendingList structure itself.
198432** For example, to create a new PendingList structure containing two
198433** varints:
198434**
198435** PendingList *p = 0;
198436** fts3PendingListAppendVarint(&p, 1);
198437** fts3PendingListAppendVarint(&p, 2);
198438*/
198439static int fts3PendingListAppendVarint(
198440 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
198441 sqlite3_int64 i /* Value to append to data */
198442){
198443 PendingList *p = *pp;
198444
198445 /* Allocate or grow the PendingList as required. */
198446 if( !p ){
198447 p = sqlite3_malloc64(sizeof(*p) + 100);
198448 if( !p ){
198449 return SQLITE_NOMEM7;
198450 }
198451 p->nSpace = 100;
198452 p->aData = (char *)&p[1];
198453 p->nData = 0;
198454 }
198455 else if( p->nData+FTS3_VARINT_MAX10+1>p->nSpace ){
198456 i64 nNew = p->nSpace * 2;
198457 p = sqlite3_realloc64(p, sizeof(*p) + nNew);
198458 if( !p ){
198459 sqlite3_free(*pp);
198460 *pp = 0;
198461 return SQLITE_NOMEM7;
198462 }
198463 p->nSpace = (int)nNew;
198464 p->aData = (char *)&p[1];
198465 }
198466
198467 /* Append the new serialized varint to the end of the list. */
198468 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
198469 p->aData[p->nData] = '\0';
198470 *pp = p;
198471 return SQLITE_OK0;
198472}
198473
198474/*
198475** Add a docid/column/position entry to a PendingList structure. Non-zero
198476** is returned if the structure is sqlite3_realloced as part of adding
198477** the entry. Otherwise, zero.
198478**
198479** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
198480** Zero is always returned in this case. Otherwise, if no OOM error occurs,
198481** it is set to SQLITE_OK.
198482*/
198483static int fts3PendingListAppend(
198484 PendingList **pp, /* IN/OUT: PendingList structure */
198485 sqlite3_int64 iDocid, /* Docid for entry to add */
198486 sqlite3_int64 iCol, /* Column for entry to add */
198487 sqlite3_int64 iPos, /* Position of term for entry to add */
198488 int *pRc /* OUT: Return code */
198489){
198490 PendingList *p = *pp;
198491 int rc = SQLITE_OK0;
198492
198493 assert( !p || p->iLastDocid<=iDocid )((void) (0));
198494
198495 if( !p || p->iLastDocid!=iDocid ){
198496 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
198497 if( p ){
198498 assert( p->nData<p->nSpace )((void) (0));
198499 assert( p->aData[p->nData]==0 )((void) (0));
198500 p->nData++;
198501 }
198502 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
198503 goto pendinglistappend_out;
198504 }
198505 p->iLastCol = -1;
198506 p->iLastPos = 0;
198507 p->iLastDocid = iDocid;
198508 }
198509 if( iCol>0 && p->iLastCol!=iCol ){
198510 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, 1))
198511 || SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iCol))
198512 ){
198513 goto pendinglistappend_out;
198514 }
198515 p->iLastCol = iCol;
198516 p->iLastPos = 0;
198517 }
198518 if( iCol>=0 ){
198519 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) )((void) (0));
198520 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
198521 if( rc==SQLITE_OK0 ){
198522 p->iLastPos = iPos;
198523 }
198524 }
198525
198526 pendinglistappend_out:
198527 *pRc = rc;
198528 if( p!=*pp ){
198529 *pp = p;
198530 return 1;
198531 }
198532 return 0;
198533}
198534
198535/*
198536** Free a PendingList object allocated by fts3PendingListAppend().
198537*/
198538static void fts3PendingListDelete(PendingList *pList){
198539 sqlite3_free(pList);
198540}
198541
198542/*
198543** Add an entry to one of the pending-terms hash tables.
198544*/
198545static int fts3PendingTermsAddOne(
198546 Fts3Table *p,
198547 int iCol,
198548 int iPos,
198549 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
198550 const char *zToken,
198551 int nToken
198552){
198553 PendingList *pList;
198554 int rc = SQLITE_OK0;
198555
198556 pList = (PendingList *)fts3HashFindsqlite3Fts3HashFind(pHash, zToken, nToken);
198557 if( pList ){
198558 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
198559 }
198560 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
198561 if( pList==fts3HashInsertsqlite3Fts3HashInsert(pHash, zToken, nToken, pList) ){
198562 /* Malloc failed while inserting the new entry. This can only
198563 ** happen if there was no previous entry for this token.
198564 */
198565 assert( 0==fts3HashFind(pHash, zToken, nToken) )((void) (0));
198566 sqlite3_free(pList);
198567 rc = SQLITE_NOMEM7;
198568 }
198569 }
198570 if( rc==SQLITE_OK0 ){
198571 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
198572 }
198573 return rc;
198574}
198575
198576/*
198577** Tokenize the nul-terminated string zText and add all tokens to the
198578** pending-terms hash-table. The docid used is that currently stored in
198579** p->iPrevDocid, and the column is specified by argument iCol.
198580**
198581** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
198582*/
198583static int fts3PendingTermsAdd(
198584 Fts3Table *p, /* Table into which text will be inserted */
198585 int iLangid, /* Language id to use */
198586 const char *zText, /* Text of document to be inserted */
198587 int iCol, /* Column into which text is being inserted */
198588 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
198589){
198590 int rc;
198591 int iStart = 0;
198592 int iEnd = 0;
198593 int iPos = 0;
198594 int nWord = 0;
198595
198596 char const *zToken;
198597 int nToken = 0;
198598
198599 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
198600 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
198601 sqlite3_tokenizer_cursor *pCsr;
198602 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
198603 const char**,int*,int*,int*,int*);
198604
198605 assert( pTokenizer && pModule )((void) (0));
198606
198607 /* If the user has inserted a NULL value, this function may be called with
198608 ** zText==0. In this case, add zero token entries to the hash table and
198609 ** return early. */
198610 if( zText==0 ){
198611 *pnWord = 0;
198612 return SQLITE_OK0;
198613 }
198614
198615 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
198616 if( rc!=SQLITE_OK0 ){
198617 return rc;
198618 }
198619
198620 xNext = pModule->xNext;
198621 while( SQLITE_OK0==rc
198622 && SQLITE_OK0==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
198623 ){
198624 int i;
198625 if( iPos>=nWord ) nWord = iPos+1;
198626
198627 /* Positions cannot be negative; we use -1 as a terminator internally.
198628 ** Tokens must have a non-zero length.
198629 */
198630 if( iPos<0 || !zToken || nToken<=0 ){
198631 rc = SQLITE_ERROR1;
198632 break;
198633 }
198634
198635 /* Add the term to the terms index */
198636 rc = fts3PendingTermsAddOne(
198637 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
198638 );
198639
198640 /* Add the term to each of the prefix indexes that it is not too
198641 ** short for. */
198642 for(i=1; rc==SQLITE_OK0 && i<p->nIndex; i++){
198643 struct Fts3Index *pIndex = &p->aIndex[i];
198644 if( nToken<pIndex->nPrefix ) continue;
198645 rc = fts3PendingTermsAddOne(
198646 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
198647 );
198648 }
198649 }
198650
198651 pModule->xClose(pCsr);
198652 *pnWord += nWord;
198653 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
198654}
198655
198656/*
198657** Calling this function indicates that subsequent calls to
198658** fts3PendingTermsAdd() are to add term/position-list pairs for the
198659** contents of the document with docid iDocid.
198660*/
198661static int fts3PendingTermsDocid(
198662 Fts3Table *p, /* Full-text table handle */
198663 int bDelete, /* True if this op is a delete */
198664 int iLangid, /* Language id of row being written */
198665 sqlite_int64 iDocid /* Docid of row being written */
198666){
198667 assert( iLangid>=0 )((void) (0));
198668 assert( bDelete==1 || bDelete==0 )((void) (0));
198669
198670 /* TODO(shess) Explore whether partially flushing the buffer on
198671 ** forced-flush would provide better performance. I suspect that if
198672 ** we ordered the doclists by size and flushed the largest until the
198673 ** buffer was half empty, that would let the less frequent terms
198674 ** generate longer doclists.
198675 */
198676 if( iDocid<p->iPrevDocid
198677 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
198678 || p->iPrevLangid!=iLangid
198679 || p->nPendingData>p->nMaxPendingData
198680 ){
198681 int rc = sqlite3Fts3PendingTermsFlush(p);
198682 if( rc!=SQLITE_OK0 ) return rc;
198683 }
198684 p->iPrevDocid = iDocid;
198685 p->iPrevLangid = iLangid;
198686 p->bPrevDelete = bDelete;
198687 return SQLITE_OK0;
198688}
198689
198690/*
198691** Discard the contents of the pending-terms hash tables.
198692*/
198693SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *p){
198694 int i;
198695 for(i=0; i<p->nIndex; i++){
198696 Fts3HashElem *pElem;
198697 Fts3Hash *pHash = &p->aIndex[i].hPending;
198698 for(pElem=fts3HashFirst(pHash)((pHash)->first); pElem; pElem=fts3HashNext(pElem)((pElem)->next)){
198699 PendingList *pList = (PendingList *)fts3HashData(pElem)((pElem)->data);
198700 fts3PendingListDelete(pList);
198701 }
198702 fts3HashClearsqlite3Fts3HashClear(pHash);
198703 }
198704 p->nPendingData = 0;
198705}
198706
198707/*
198708** This function is called by the xUpdate() method as part of an INSERT
198709** operation. It adds entries for each term in the new record to the
198710** pendingTerms hash table.
198711**
198712** Argument apVal is the same as the similarly named argument passed to
198713** fts3InsertData(). Parameter iDocid is the docid of the new row.
198714*/
198715static int fts3InsertTerms(
198716 Fts3Table *p,
198717 int iLangid,
198718 sqlite3_value **apVal,
198719 u32 *aSz
198720){
198721 int i; /* Iterator variable */
198722 for(i=2; i<p->nColumn+2; i++){
198723 int iCol = i-2;
198724 if( p->abNotindexed[iCol]==0 ){
198725 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
198726 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
198727 if( rc!=SQLITE_OK0 ){
198728 return rc;
198729 }
198730 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
198731 }
198732 }
198733 return SQLITE_OK0;
198734}
198735
198736/*
198737** This function is called by the xUpdate() method for an INSERT operation.
198738** The apVal parameter is passed a copy of the apVal argument passed by
198739** SQLite to the xUpdate() method. i.e:
198740**
198741** apVal[0] Not used for INSERT.
198742** apVal[1] rowid
198743** apVal[2] Left-most user-defined column
198744** ...
198745** apVal[p->nColumn+1] Right-most user-defined column
198746** apVal[p->nColumn+2] Hidden column with same name as table
198747** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
198748** apVal[p->nColumn+4] Hidden languageid column
198749*/
198750static int fts3InsertData(
198751 Fts3Table *p, /* Full-text table */
198752 sqlite3_value **apVal, /* Array of values to insert */
198753 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
198754){
198755 int rc; /* Return code */
198756 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
198757
198758 if( p->zContentTbl ){
198759 sqlite3_value *pRowid = apVal[p->nColumn+3];
198760 if( sqlite3_value_type(pRowid)==SQLITE_NULL5 ){
198761 pRowid = apVal[1];
198762 }
198763 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER1 ){
198764 return SQLITE_CONSTRAINT19;
198765 }
198766 *piDocid = sqlite3_value_int64(pRowid);
198767 return SQLITE_OK0;
198768 }
198769
198770 /* Locate the statement handle used to insert data into the %_content
198771 ** table. The SQL for this statement is:
198772 **
198773 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
198774 **
198775 ** The statement features N '?' variables, where N is the number of user
198776 ** defined columns in the FTS3 table, plus one for the docid field.
198777 */
198778 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT18, &pContentInsert, &apVal[1]);
198779 if( rc==SQLITE_OK0 && p->zLanguageid ){
198780 rc = sqlite3_bind_int(
198781 pContentInsert, p->nColumn+2,
198782 sqlite3_value_int(apVal[p->nColumn+4])
198783 );
198784 }
198785 if( rc!=SQLITE_OK0 ) return rc;
198786
198787 /* There is a quirk here. The users INSERT statement may have specified
198788 ** a value for the "rowid" field, for the "docid" field, or for both.
198789 ** Which is a problem, since "rowid" and "docid" are aliases for the
198790 ** same value. For example:
198791 **
198792 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
198793 **
198794 ** In FTS3, this is an error. It is an error to specify non-NULL values
198795 ** for both docid and some other rowid alias.
198796 */
198797 if( SQLITE_NULL5!=sqlite3_value_type(apVal[3+p->nColumn]) ){
198798 if( SQLITE_NULL5==sqlite3_value_type(apVal[0])
198799 && SQLITE_NULL5!=sqlite3_value_type(apVal[1])
198800 ){
198801 /* A rowid/docid conflict. */
198802 return SQLITE_ERROR1;
198803 }
198804 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
198805 if( rc!=SQLITE_OK0 ) return rc;
198806 }
198807
198808 /* Execute the statement to insert the record. Set *piDocid to the
198809 ** new docid value.
198810 */
198811 sqlite3_step(pContentInsert);
198812 rc = sqlite3_reset(pContentInsert);
198813
198814 *piDocid = sqlite3_last_insert_rowid(p->db);
198815 return rc;
198816}
198817
198818
198819
198820/*
198821** Remove all data from the FTS3 table. Clear the hash table containing
198822** pending terms.
198823*/
198824static int fts3DeleteAll(Fts3Table *p, int bContent){
198825 int rc = SQLITE_OK0; /* Return code */
198826
198827 /* Discard the contents of the pending-terms hash table. */
198828 sqlite3Fts3PendingTermsClear(p);
198829
198830 /* Delete everything from the shadow tables. Except, leave %_content as
198831 ** is if bContent is false. */
198832 assert( p->zContentTbl==0 || bContent==0 )((void) (0));
198833 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT2, 0);
198834 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS3, 0);
198835 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR4, 0);
198836 if( p->bHasDocsize ){
198837 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE5, 0);
198838 }
198839 if( p->bHasStat ){
198840 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT6, 0);
198841 }
198842 return rc;
198843}
198844
198845/*
198846**
198847*/
198848static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
198849 int iLangid = 0;
198850 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
198851 return iLangid;
198852}
198853
198854/*
198855** The first element in the apVal[] array is assumed to contain the docid
198856** (an integer) of a row about to be deleted. Remove all terms from the
198857** full-text index.
198858*/
198859static void fts3DeleteTerms(
198860 int *pRC, /* Result code */
198861 Fts3Table *p, /* The FTS table to delete from */
198862 sqlite3_value *pRowid, /* The docid to be deleted */
198863 u32 *aSz, /* Sizes of deleted document written here */
198864 int *pbFound /* OUT: Set to true if row really does exist */
198865){
198866 int rc;
198867 sqlite3_stmt *pSelect;
198868
198869 assert( *pbFound==0 )((void) (0));
198870 if( *pRC ) return;
198871 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID7, &pSelect, &pRowid);
198872 if( rc==SQLITE_OK0 ){
198873 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
198874 int i;
198875 int iLangid = langidFromSelect(p, pSelect);
198876 i64 iDocid = sqlite3_column_int64(pSelect, 0);
198877 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
198878 for(i=1; rc==SQLITE_OK0 && i<=p->nColumn; i++){
198879 int iCol = i-1;
198880 if( p->abNotindexed[iCol]==0 ){
198881 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
198882 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
198883 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
198884 }
198885 }
198886 if( rc!=SQLITE_OK0 ){
198887 sqlite3_reset(pSelect);
198888 *pRC = rc;
198889 return;
198890 }
198891 *pbFound = 1;
198892 }
198893 rc = sqlite3_reset(pSelect);
198894 }else{
198895 sqlite3_reset(pSelect);
198896 }
198897 *pRC = rc;
198898}
198899
198900/*
198901** Forward declaration to account for the circular dependency between
198902** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
198903*/
198904static int fts3SegmentMerge(Fts3Table *, int, int, int);
198905
198906/*
198907** This function allocates a new level iLevel index in the segdir table.
198908** Usually, indexes are allocated within a level sequentially starting
198909** with 0, so the allocated index is one greater than the value returned
198910** by:
198911**
198912** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
198913**
198914** However, if there are already FTS3_MERGE_COUNT indexes at the requested
198915** level, they are merged into a single level (iLevel+1) segment and the
198916** allocated index is 0.
198917**
198918** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
198919** returned. Otherwise, an SQLite error code is returned.
198920*/
198921static int fts3AllocateSegdirIdx(
198922 Fts3Table *p,
198923 int iLangid, /* Language id */
198924 int iIndex, /* Index for p->aIndex */
198925 int iLevel,
198926 int *piIdx
198927){
198928 int rc; /* Return Code */
198929 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
198930 int iNext = 0; /* Result of query pNextIdx */
198931
198932 assert( iLangid>=0 )((void) (0));
198933 assert( p->nIndex>=1 )((void) (0));
198934
198935 /* Set variable iNext to the next available segdir index at level iLevel. */
198936 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX8, &pNextIdx, 0);
198937 if( rc==SQLITE_OK0 ){
198938 sqlite3_bind_int64(
198939 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
198940 );
198941 if( SQLITE_ROW100==sqlite3_step(pNextIdx) ){
198942 iNext = sqlite3_column_int(pNextIdx, 0);
198943 }
198944 rc = sqlite3_reset(pNextIdx);
198945 }
198946
198947 if( rc==SQLITE_OK0 ){
198948 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
198949 ** full, merge all segments in level iLevel into a single iLevel+1
198950 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
198951 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
198952 */
198953 if( iNext>=MergeCount(p)16 ){
198954 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
198955 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
198956 *piIdx = 0;
198957 }else{
198958 *piIdx = iNext;
198959 }
198960 }
198961
198962 return rc;
198963}
198964
198965/*
198966** The %_segments table is declared as follows:
198967**
198968** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
198969**
198970** This function reads data from a single row of the %_segments table. The
198971** specific row is identified by the iBlockid parameter. If paBlob is not
198972** NULL, then a buffer is allocated using sqlite3_malloc() and populated
198973** with the contents of the blob stored in the "block" column of the
198974** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
198975** to the size of the blob in bytes before returning.
198976**
198977** If an error occurs, or the table does not contain the specified row,
198978** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
198979** paBlob is non-NULL, then it is the responsibility of the caller to
198980** eventually free the returned buffer.
198981**
198982** This function may leave an open sqlite3_blob* handle in the
198983** Fts3Table.pSegments variable. This handle is reused by subsequent calls
198984** to this function. The handle may be closed by calling the
198985** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
198986** performance improvement, but the blob handle should always be closed
198987** before control is returned to the user (to prevent a lock being held
198988** on the database file for longer than necessary). Thus, any virtual table
198989** method (xFilter etc.) that may directly or indirectly call this function
198990** must call sqlite3Fts3SegmentsClose() before returning.
198991*/
198992SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(
198993 Fts3Table *p, /* FTS3 table handle */
198994 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
198995 char **paBlob, /* OUT: Blob data in malloc'd buffer */
198996 int *pnBlob, /* OUT: Size of blob data */
198997 int *pnLoad /* OUT: Bytes actually loaded */
198998){
198999 int rc; /* Return code */
199000
199001 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
199002 assert( pnBlob )((void) (0));
199003
199004 if( p->pSegments ){
199005 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
199006 }else{
199007 if( 0==p->zSegmentsTbl ){
199008 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
199009 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM7;
199010 }
199011 rc = sqlite3_blob_open(
199012 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
199013 );
199014 }
199015
199016 if( rc==SQLITE_OK0 ){
199017 int nByte = sqlite3_blob_bytes(p->pSegments);
199018 *pnBlob = nByte;
199019 if( paBlob ){
199020 char *aByte = sqlite3_malloc64((i64)nByte + FTS3_NODE_PADDING(10*2));
199021 if( !aByte ){
199022 rc = SQLITE_NOMEM7;
199023 }else{
199024 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD((4*1024)*4)) ){
199025 nByte = FTS3_NODE_CHUNKSIZE(4*1024);
199026 *pnLoad = nByte;
199027 }
199028 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
199029 memset(&aByte[nByte], 0, FTS3_NODE_PADDING(10*2));
199030 if( rc!=SQLITE_OK0 ){
199031 sqlite3_free(aByte);
199032 aByte = 0;
199033 }
199034 }
199035 *paBlob = aByte;
199036 }
199037 }else if( rc==SQLITE_ERROR1 ){
199038 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
199039 }
199040
199041 return rc;
199042}
199043
199044/*
199045** Close the blob handle at p->pSegments, if it is open. See comments above
199046** the sqlite3Fts3ReadBlock() function for details.
199047*/
199048SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *p){
199049 sqlite3_blob_close(p->pSegments);
199050 p->pSegments = 0;
199051}
199052
199053static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
199054 int nRead; /* Number of bytes to read */
199055 int rc; /* Return code */
199056
199057 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE)((pReader->nNode - pReader->nPopulate)<((4*1024))?(pReader
->nNode - pReader->nPopulate):((4*1024)))
;
199058 rc = sqlite3_blob_read(
199059 pReader->pBlob,
199060 &pReader->aNode[pReader->nPopulate],
199061 nRead,
199062 pReader->nPopulate
199063 );
199064
199065 if( rc==SQLITE_OK0 ){
199066 pReader->nPopulate += nRead;
199067 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING(10*2));
199068 if( pReader->nPopulate==pReader->nNode ){
199069 sqlite3_blob_close(pReader->pBlob);
199070 pReader->pBlob = 0;
199071 pReader->nPopulate = 0;
199072 }
199073 }
199074 return rc;
199075}
199076
199077static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
199078 int rc = SQLITE_OK0;
199079 assert( !pReader->pBlob((void) (0))
199080 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])((void) (0))
199081 )((void) (0));
199082 while( pReader->pBlob && rc==SQLITE_OK0
199083 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
199084 ){
199085 rc = fts3SegReaderIncrRead(pReader);
199086 }
199087 return rc;
199088}
199089
199090/*
199091** Set an Fts3SegReader cursor to point at EOF.
199092*/
199093static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
199094 if( !fts3SegReaderIsRootOnly(pSeg)((pSeg)->rootOnly!=0) ){
199095 sqlite3_free(pSeg->aNode);
199096 sqlite3_blob_close(pSeg->pBlob);
199097 pSeg->pBlob = 0;
199098 }
199099 pSeg->aNode = 0;
199100}
199101
199102/*
199103** Move the iterator passed as the first argument to the next term in the
199104** segment. If successful, SQLITE_OK is returned. If there is no next term,
199105** SQLITE_DONE. Otherwise, an SQLite error code.
199106*/
199107static int fts3SegReaderNext(
199108 Fts3Table *p,
199109 Fts3SegReader *pReader,
199110 int bIncr
199111){
199112 int rc; /* Return code of various sub-routines */
199113 char *pNext; /* Cursor variable */
199114 int nPrefix; /* Number of bytes in term prefix */
199115 int nSuffix; /* Number of bytes in term suffix */
199116
199117 if( !pReader->aDoclist ){
199118 pNext = pReader->aNode;
199119 }else{
199120 pNext = &pReader->aDoclist[pReader->nDoclist];
199121 }
199122
199123 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
199124
199125 if( fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
199126 Fts3HashElem *pElem = *(pReader->ppNextElem);
199127 sqlite3_free(pReader->aNode);
199128 pReader->aNode = 0;
199129 if( pElem ){
199130 char *aCopy;
199131 PendingList *pList = (PendingList *)fts3HashData(pElem)((pElem)->data);
199132 int nCopy = pList->nData+1;
199133
199134 int nTerm = fts3HashKeysize(pElem)((pElem)->nKey);
199135 if( (nTerm+1)>pReader->nTermAlloc ){
199136 sqlite3_free(pReader->zTerm);
199137 pReader->zTerm = (char*)sqlite3_malloc64(((i64)nTerm+1)*2);
199138 if( !pReader->zTerm ) return SQLITE_NOMEM7;
199139 pReader->nTermAlloc = (nTerm+1)*2;
199140 }
199141 memcpy(pReader->zTerm, fts3HashKey(pElem)((pElem)->pKey), nTerm);
199142 pReader->zTerm[nTerm] = '\0';
199143 pReader->nTerm = nTerm;
199144
199145 aCopy = (char*)sqlite3_malloc64(nCopy);
199146 if( !aCopy ) return SQLITE_NOMEM7;
199147 memcpy(aCopy, pList->aData, nCopy);
199148 pReader->nNode = pReader->nDoclist = nCopy;
199149 pReader->aNode = pReader->aDoclist = aCopy;
199150 pReader->ppNextElem++;
199151 assert( pReader->aNode )((void) (0));
199152 }
199153 return SQLITE_OK0;
199154 }
199155
199156 fts3SegReaderSetEof(pReader);
199157
199158 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
199159 ** blocks have already been traversed. */
199160#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
199161 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB )((void) (0));
199162#endif
199163 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
199164 return SQLITE_OK0;
199165 }
199166
199167 rc = sqlite3Fts3ReadBlock(
199168 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
199169 (bIncr ? &pReader->nPopulate : 0)
199170 );
199171 if( rc!=SQLITE_OK0 ) return rc;
199172 assert( pReader->pBlob==0 )((void) (0));
199173 if( bIncr && pReader->nPopulate<pReader->nNode ){
199174 pReader->pBlob = p->pSegments;
199175 p->pSegments = 0;
199176 }
199177 pNext = pReader->aNode;
199178 }
199179
199180 assert( !fts3SegReaderIsPending(pReader) )((void) (0));
199181
199182 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX10*2);
199183 if( rc!=SQLITE_OK0 ) return rc;
199184
199185 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
199186 ** safe (no risk of overread) even if the node data is corrupted. */
199187 pNext += fts3GetVarint32(pNext, &nPrefix)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
nPrefix) : (*&nPrefix=*(u8*)(pNext), 1) )
;
199188 pNext += fts3GetVarint32(pNext, &nSuffix)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
nSuffix) : (*&nSuffix=*(u8*)(pNext), 1) )
;
199189 if( nSuffix<=0
199190 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
199191 || nPrefix>pReader->nTerm
199192 ){
199193 return FTS_CORRUPT_VTAB(11 | (1<<8));
199194 }
199195
199196 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
199197 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
199198 ** overflow - hence the (i64) casts. */
199199 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
199200 i64 nNew = ((i64)nPrefix+nSuffix)*2;
199201 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
199202 if( !zNew ){
199203 return SQLITE_NOMEM7;
199204 }
199205 pReader->zTerm = zNew;
199206 pReader->nTermAlloc = nNew;
199207 }
199208
199209 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX10);
199210 if( rc!=SQLITE_OK0 ) return rc;
199211
199212 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
199213 pReader->nTerm = nPrefix+nSuffix;
199214 pNext += nSuffix;
199215 pNext += fts3GetVarint32(pNext, &pReader->nDoclist)( (*(u8*)(pNext)&0x80) ? sqlite3Fts3GetVarint32(pNext, &
pReader->nDoclist) : (*&pReader->nDoclist=*(u8*)(pNext
), 1) )
;
199216 pReader->aDoclist = pNext;
199217 pReader->pOffsetList = 0;
199218
199219 /* Check that the doclist does not appear to extend past the end of the
199220 ** b-tree node. And that the final byte of the doclist is 0x00. If either
199221 ** of these statements is untrue, then the data structure is corrupt.
199222 */
199223 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
199224 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
199225 || pReader->nDoclist==0
199226 ){
199227 return FTS_CORRUPT_VTAB(11 | (1<<8));
199228 }
199229 return SQLITE_OK0;
199230}
199231
199232/*
199233** Set the SegReader to point to the first docid in the doclist associated
199234** with the current term.
199235*/
199236static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
199237 int rc = SQLITE_OK0;
199238 assert( pReader->aDoclist )((void) (0));
199239 assert( !pReader->pOffsetList )((void) (0));
199240 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
199241 u8 bEof = 0;
199242 pReader->iDocid = 0;
199243 pReader->nOffsetList = 0;
199244 sqlite3Fts3DoclistPrev(0,
199245 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
199246 &pReader->iDocid, &pReader->nOffsetList, &bEof
199247 );
199248 }else{
199249 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX10);
199250 if( rc==SQLITE_OK0 ){
199251 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
199252 pReader->pOffsetList = &pReader->aDoclist[n];
199253 }
199254 }
199255 return rc;
199256}
199257
199258/*
199259** Advance the SegReader to point to the next docid in the doclist
199260** associated with the current term.
199261**
199262** If arguments ppOffsetList and pnOffsetList are not NULL, then
199263** *ppOffsetList is set to point to the first column-offset list
199264** in the doclist entry (i.e. immediately past the docid varint).
199265** *pnOffsetList is set to the length of the set of column-offset
199266** lists, not including the nul-terminator byte. For example:
199267*/
199268static int fts3SegReaderNextDocid(
199269 Fts3Table *pTab,
199270 Fts3SegReader *pReader, /* Reader to advance to next docid */
199271 char **ppOffsetList, /* OUT: Pointer to current position-list */
199272 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
199273){
199274 int rc = SQLITE_OK0;
199275 char *p = pReader->pOffsetList;
199276 char c = 0;
199277
199278 assert( p )((void) (0));
199279
199280 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0) ){
199281 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
199282 ** Pending-terms doclists are always built up in ascending order, so
199283 ** we have to iterate through them backwards here. */
199284 u8 bEof = 0;
199285 if( ppOffsetList ){
199286 *ppOffsetList = pReader->pOffsetList;
199287 *pnOffsetList = pReader->nOffsetList - 1;
199288 }
199289 sqlite3Fts3DoclistPrev(0,
199290 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
199291 &pReader->nOffsetList, &bEof
199292 );
199293 if( bEof ){
199294 pReader->pOffsetList = 0;
199295 }else{
199296 pReader->pOffsetList = p;
199297 }
199298 }else{
199299 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
199300
199301 /* Pointer p currently points at the first byte of an offset list. The
199302 ** following block advances it to point one byte past the end of
199303 ** the same offset list. */
199304 while( 1 ){
199305
199306 /* The following line of code (and the "p++" below the while() loop) is
199307 ** normally all that is required to move pointer p to the desired
199308 ** position. The exception is if this node is being loaded from disk
199309 ** incrementally and pointer "p" now points to the first byte past
199310 ** the populated part of pReader->aNode[].
199311 */
199312 while( *p | c ) c = *p++ & 0x80;
199313 assert( *p==0 )((void) (0));
199314
199315 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
199316 rc = fts3SegReaderIncrRead(pReader);
199317 if( rc!=SQLITE_OK0 ) return rc;
199318 }
199319 p++;
199320
199321 /* If required, populate the output variables with a pointer to and the
199322 ** size of the previous offset-list.
199323 */
199324 if( ppOffsetList ){
199325 *ppOffsetList = pReader->pOffsetList;
199326 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
199327 }
199328
199329 /* List may have been edited in place by fts3EvalNearTrim() */
199330 while( p<pEnd && *p==0 ) p++;
199331
199332 /* If there are no more entries in the doclist, set pOffsetList to
199333 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
199334 ** Fts3SegReader.pOffsetList to point to the next offset list before
199335 ** returning.
199336 */
199337 if( p>=pEnd ){
199338 pReader->pOffsetList = 0;
199339 }else{
199340 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX10);
199341 if( rc==SQLITE_OK0 ){
199342 u64 iDelta;
199343 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta);
199344 if( pTab->bDescIdx ){
199345 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
199346 }else{
199347 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
199348 }
199349 }
199350 }
199351 }
199352
199353 return rc;
199354}
199355
199356
199357SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(
199358 Fts3Cursor *pCsr,
199359 Fts3MultiSegReader *pMsr,
199360 int *pnOvfl
199361){
199362 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
199363 int nOvfl = 0;
199364 int ii;
199365 int rc = SQLITE_OK0;
199366 int pgsz = p->nPgsz;
199367
199368 assert( p->bFts4 )((void) (0));
199369 assert( pgsz>0 )((void) (0));
199370
199371 for(ii=0; rc==SQLITE_OK0 && ii<pMsr->nSegment; ii++){
199372 Fts3SegReader *pReader = pMsr->apSegment[ii];
199373 if( !fts3SegReaderIsPending(pReader)((pReader)->ppNextElem!=0)
199374 && !fts3SegReaderIsRootOnly(pReader)((pReader)->rootOnly!=0)
199375 ){
199376 sqlite3_int64 jj;
199377 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
199378 int nBlob;
199379 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
199380 if( rc!=SQLITE_OK0 ) break;
199381 if( (nBlob+35)>pgsz ){
199382 nOvfl += (nBlob + 34)/pgsz;
199383 }
199384 }
199385 }
199386 }
199387 *pnOvfl = nOvfl;
199388 return rc;
199389}
199390
199391/*
199392** Free all allocations associated with the iterator passed as the
199393** second argument.
199394*/
199395SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
199396 if( pReader ){
199397 sqlite3_free(pReader->zTerm);
199398 if( !fts3SegReaderIsRootOnly(pReader)((pReader)->rootOnly!=0) ){
199399 sqlite3_free(pReader->aNode);
199400 }
199401 sqlite3_blob_close(pReader->pBlob);
199402 }
199403 sqlite3_free(pReader);
199404}
199405
199406/*
199407** Allocate a new SegReader object.
199408*/
199409SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(
199410 int iAge, /* Segment "age". */
199411 int bLookup, /* True for a lookup only */
199412 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
199413 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
199414 sqlite3_int64 iEndBlock, /* Final block of segment */
199415 const char *zRoot, /* Buffer containing root node */
199416 int nRoot, /* Size of buffer containing root node */
199417 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
199418){
199419 Fts3SegReader *pReader; /* Newly allocated SegReader object */
199420 int nExtra = 0; /* Bytes to allocate segment root node */
199421
199422 assert( zRoot!=0 || nRoot==0 )((void) (0));
199423#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
199424 assert( zRoot!=0 || CORRUPT_DB )((void) (0));
199425#endif
199426
199427 if( iStartLeaf==0 ){
199428 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
199429 nExtra = nRoot + FTS3_NODE_PADDING(10*2);
199430 }
199431
199432 pReader = (Fts3SegReader *)sqlite3_malloc64(sizeof(Fts3SegReader) + nExtra);
199433 if( !pReader ){
199434 return SQLITE_NOMEM7;
199435 }
199436 memset(pReader, 0, sizeof(Fts3SegReader));
199437 pReader->iIdx = iAge;
199438 pReader->bLookup = bLookup!=0;
199439 pReader->iStartBlock = iStartLeaf;
199440 pReader->iLeafEndBlock = iEndLeaf;
199441 pReader->iEndBlock = iEndBlock;
199442
199443 if( nExtra ){
199444 /* The entire segment is stored in the root node. */
199445 pReader->aNode = (char *)&pReader[1];
199446 pReader->rootOnly = 1;
199447 pReader->nNode = nRoot;
199448 if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
199449 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING(10*2));
199450 }else{
199451 pReader->iCurrentBlock = iStartLeaf-1;
199452 }
199453 *ppReader = pReader;
199454 return SQLITE_OK0;
199455}
199456
199457/*
199458** This is a comparison function used as a qsort() callback when sorting
199459** an array of pending terms by term. This occurs as part of flushing
199460** the contents of the pending-terms hash table to the database.
199461*/
199462static int SQLITE_CDECL fts3CompareElemByTerm(
199463 const void *lhs,
199464 const void *rhs
199465){
199466 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs)((*(Fts3HashElem **)lhs)->pKey);
199467 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs)((*(Fts3HashElem **)rhs)->pKey);
199468 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs)((*(Fts3HashElem **)lhs)->nKey);
199469 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs)((*(Fts3HashElem **)rhs)->nKey);
199470
199471 int n = (n1<n2 ? n1 : n2);
199472 int c = memcmp(z1, z2, n);
199473 if( c==0 ){
199474 c = n1 - n2;
199475 }
199476 return c;
199477}
199478
199479/*
199480** This function is used to allocate an Fts3SegReader that iterates through
199481** a subset of the terms stored in the Fts3Table.pendingTerms array.
199482**
199483** If the isPrefixIter parameter is zero, then the returned SegReader iterates
199484** through each term in the pending-terms table. Or, if isPrefixIter is
199485** non-zero, it iterates through each term and its prefixes. For example, if
199486** the pending terms hash table contains the terms "sqlite", "mysql" and
199487** "firebird", then the iterator visits the following 'terms' (in the order
199488** shown):
199489**
199490** f fi fir fire fireb firebi firebir firebird
199491** m my mys mysq mysql
199492** s sq sql sqli sqlit sqlite
199493**
199494** Whereas if isPrefixIter is zero, the terms visited are:
199495**
199496** firebird mysql sqlite
199497*/
199498SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
199499 Fts3Table *p, /* Virtual table handle */
199500 int iIndex, /* Index for p->aIndex */
199501 const char *zTerm, /* Term to search for */
199502 int nTerm, /* Size of buffer zTerm */
199503 int bPrefix, /* True for a prefix iterator */
199504 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
199505){
199506 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
199507 Fts3HashElem *pE; /* Iterator variable */
199508 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
199509 int nElem = 0; /* Size of array at aElem */
199510 int rc = SQLITE_OK0; /* Return Code */
199511 Fts3Hash *pHash;
199512
199513 pHash = &p->aIndex[iIndex].hPending;
199514 if( bPrefix ){
199515 int nAlloc = 0; /* Size of allocated array at aElem */
199516
199517 for(pE=fts3HashFirst(pHash)((pHash)->first); pE; pE=fts3HashNext(pE)((pE)->next)){
199518 char *zKey = (char *)fts3HashKey(pE)((pE)->pKey);
199519 int nKey = fts3HashKeysize(pE)((pE)->nKey);
199520 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
199521 if( nElem==nAlloc ){
199522 Fts3HashElem **aElem2;
199523 nAlloc += 16;
199524 aElem2 = (Fts3HashElem **)sqlite3_realloc64(
199525 aElem, nAlloc*sizeof(Fts3HashElem *)
199526 );
199527 if( !aElem2 ){
199528 rc = SQLITE_NOMEM7;
199529 nElem = 0;
199530 break;
199531 }
199532 aElem = aElem2;
199533 }
199534
199535 aElem[nElem++] = pE;
199536 }
199537 }
199538
199539 /* If more than one term matches the prefix, sort the Fts3HashElem
199540 ** objects in term order using qsort(). This uses the same comparison
199541 ** callback as is used when flushing terms to disk.
199542 */
199543 if( nElem>1 ){
199544 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
199545 }
199546
199547 }else{
199548 /* The query is a simple term lookup that matches at most one term in
199549 ** the index. All that is required is a straight hash-lookup.
199550 **
199551 ** Because the stack address of pE may be accessed via the aElem pointer
199552 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
199553 ** within this entire function, not just this "else{...}" block.
199554 */
199555 pE = fts3HashFindElemsqlite3Fts3HashFindElem(pHash, zTerm, nTerm);
199556 if( pE ){
199557 aElem = &pE;
199558 nElem = 1;
199559 }
199560 }
199561
199562 if( nElem>0 ){
199563 sqlite3_int64 nByte;
199564 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
199565 pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
199566 if( !pReader ){
199567 rc = SQLITE_NOMEM7;
199568 }else{
199569 memset(pReader, 0, nByte);
199570 pReader->iIdx = 0x7FFFFFFF;
199571 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
199572 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
199573 }
199574 }
199575
199576 if( bPrefix ){
199577 sqlite3_free(aElem);
199578 }
199579 *ppReader = pReader;
199580 return rc;
199581}
199582
199583/*
199584** Compare the entries pointed to by two Fts3SegReader structures.
199585** Comparison is as follows:
199586**
199587** 1) EOF is greater than not EOF.
199588**
199589** 2) The current terms (if any) are compared using memcmp(). If one
199590** term is a prefix of another, the longer term is considered the
199591** larger.
199592**
199593** 3) By segment age. An older segment is considered larger.
199594*/
199595static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
199596 int rc;
199597 if( pLhs->aNode && pRhs->aNode ){
199598 int rc2 = pLhs->nTerm - pRhs->nTerm;
199599 if( rc2<0 ){
199600 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
199601 }else{
199602 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
199603 }
199604 if( rc==0 ){
199605 rc = rc2;
199606 }
199607 }else{
199608 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
199609 }
199610 if( rc==0 ){
199611 rc = pRhs->iIdx - pLhs->iIdx;
199612 }
199613 assert_fts3_nc( rc!=0 )((void) (0));
199614 return rc;
199615}
199616
199617/*
199618** A different comparison function for SegReader structures. In this
199619** version, it is assumed that each SegReader points to an entry in
199620** a doclist for identical terms. Comparison is made as follows:
199621**
199622** 1) EOF (end of doclist in this case) is greater than not EOF.
199623**
199624** 2) By current docid.
199625**
199626** 3) By segment age. An older segment is considered larger.
199627*/
199628static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
199629 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
199630 if( rc==0 ){
199631 if( pLhs->iDocid==pRhs->iDocid ){
199632 rc = pRhs->iIdx - pLhs->iIdx;
199633 }else{
199634 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
199635 }
199636 }
199637 assert( pLhs->aNode && pRhs->aNode )((void) (0));
199638 return rc;
199639}
199640static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
199641 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
199642 if( rc==0 ){
199643 if( pLhs->iDocid==pRhs->iDocid ){
199644 rc = pRhs->iIdx - pLhs->iIdx;
199645 }else{
199646 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
199647 }
199648 }
199649 assert( pLhs->aNode && pRhs->aNode )((void) (0));
199650 return rc;
199651}
199652
199653/*
199654** Compare the term that the Fts3SegReader object passed as the first argument
199655** points to with the term specified by arguments zTerm and nTerm.
199656**
199657** If the pSeg iterator is already at EOF, return 0. Otherwise, return
199658** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
199659** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
199660*/
199661static int fts3SegReaderTermCmp(
199662 Fts3SegReader *pSeg, /* Segment reader object */
199663 const char *zTerm, /* Term to compare to */
199664 int nTerm /* Size of term zTerm in bytes */
199665){
199666 int res = 0;
199667 if( pSeg->aNode ){
199668 if( pSeg->nTerm>nTerm ){
199669 res = memcmp(pSeg->zTerm, zTerm, nTerm);
199670 }else{
199671 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
199672 }
199673 if( res==0 ){
199674 res = pSeg->nTerm-nTerm;
199675 }
199676 }
199677 return res;
199678}
199679
199680/*
199681** Argument apSegment is an array of nSegment elements. It is known that
199682** the final (nSegment-nSuspect) members are already in sorted order
199683** (according to the comparison function provided). This function shuffles
199684** the array around until all entries are in sorted order.
199685*/
199686static void fts3SegReaderSort(
199687 Fts3SegReader **apSegment, /* Array to sort entries of */
199688 int nSegment, /* Size of apSegment array */
199689 int nSuspect, /* Unsorted entry count */
199690 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
199691){
199692 int i; /* Iterator variable */
199693
199694 assert( nSuspect<=nSegment )((void) (0));
199695
199696 if( nSuspect==nSegment ) nSuspect--;
199697 for(i=nSuspect-1; i>=0; i--){
199698 int j;
199699 for(j=i; j<(nSegment-1); j++){
199700 Fts3SegReader *pTmp;
199701 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
199702 pTmp = apSegment[j+1];
199703 apSegment[j+1] = apSegment[j];
199704 apSegment[j] = pTmp;
199705 }
199706 }
199707
199708#ifndef NDEBUG1
199709 /* Check that the list really is sorted now. */
199710 for(i=0; i<(nSuspect-1); i++){
199711 assert( xCmp(apSegment[i], apSegment[i+1])<0 )((void) (0));
199712 }
199713#endif
199714}
199715
199716/*
199717** Insert a record into the %_segments table.
199718*/
199719static int fts3WriteSegment(
199720 Fts3Table *p, /* Virtual table handle */
199721 sqlite3_int64 iBlock, /* Block id for new block */
199722 char *z, /* Pointer to buffer containing block data */
199723 int n /* Size of buffer z in bytes */
199724){
199725 sqlite3_stmt *pStmt;
199726 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS9, &pStmt, 0);
199727 if( rc==SQLITE_OK0 ){
199728 sqlite3_bind_int64(pStmt, 1, iBlock);
199729 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC((sqlite3_destructor_type)0));
199730 sqlite3_step(pStmt);
199731 rc = sqlite3_reset(pStmt);
199732 sqlite3_bind_null(pStmt, 2);
199733 }
199734 return rc;
199735}
199736
199737/*
199738** Find the largest relative level number in the table. If successful, set
199739** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
199740** set *pnMax to zero and return an SQLite error code.
199741*/
199742SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
199743 int rc;
199744 int mxLevel = 0;
199745 sqlite3_stmt *pStmt = 0;
199746
199747 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL36, &pStmt, 0);
199748 if( rc==SQLITE_OK0 ){
199749 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
199750 mxLevel = sqlite3_column_int(pStmt, 0);
199751 }
199752 rc = sqlite3_reset(pStmt);
199753 }
199754 *pnMax = mxLevel;
199755 return rc;
199756}
199757
199758/*
199759** Insert a record into the %_segdir table.
199760*/
199761static int fts3WriteSegdir(
199762 Fts3Table *p, /* Virtual table handle */
199763 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
199764 int iIdx, /* Value for "idx" field */
199765 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
199766 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
199767 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
199768 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
199769 char *zRoot, /* Blob value for "root" field */
199770 int nRoot /* Number of bytes in buffer zRoot */
199771){
199772 sqlite3_stmt *pStmt;
199773 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR11, &pStmt, 0);
199774 if( rc==SQLITE_OK0 ){
199775 sqlite3_bind_int64(pStmt, 1, iLevel);
199776 sqlite3_bind_int(pStmt, 2, iIdx);
199777 sqlite3_bind_int64(pStmt, 3, iStartBlock);
199778 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
199779 if( nLeafData==0 ){
199780 sqlite3_bind_int64(pStmt, 5, iEndBlock);
199781 }else{
199782 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
199783 if( !zEnd ) return SQLITE_NOMEM7;
199784 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
199785 }
199786 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC((sqlite3_destructor_type)0));
199787 sqlite3_step(pStmt);
199788 rc = sqlite3_reset(pStmt);
199789 sqlite3_bind_null(pStmt, 6);
199790 }
199791 return rc;
199792}
199793
199794/*
199795** Return the size of the common prefix (if any) shared by zPrev and
199796** zNext, in bytes. For example,
199797**
199798** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
199799** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
199800** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
199801*/
199802static int fts3PrefixCompress(
199803 const char *zPrev, /* Buffer containing previous term */
199804 int nPrev, /* Size of buffer zPrev in bytes */
199805 const char *zNext, /* Buffer containing next term */
199806 int nNext /* Size of buffer zNext in bytes */
199807){
199808 int n;
199809 for(n=0; n<nPrev && n<nNext && zPrev[n]==zNext[n]; n++);
199810 assert_fts3_nc( n<nNext )((void) (0));
199811 return n;
199812}
199813
199814/*
199815** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
199816** (according to memcmp) than the previous term.
199817*/
199818static int fts3NodeAddTerm(
199819 Fts3Table *p, /* Virtual table handle */
199820 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
199821 int isCopyTerm, /* True if zTerm/nTerm is transient */
199822 const char *zTerm, /* Pointer to buffer containing term */
199823 int nTerm /* Size of term in bytes */
199824){
199825 SegmentNode *pTree = *ppTree;
199826 int rc;
199827 SegmentNode *pNew;
199828
199829 /* First try to append the term to the current node. Return early if
199830 ** this is possible.
199831 */
199832 if( pTree ){
199833 int nData = pTree->nData; /* Current size of node in bytes */
199834 int nReq = nData; /* Required space after adding zTerm */
199835 int nPrefix; /* Number of bytes of prefix compression */
199836 int nSuffix; /* Suffix length */
199837
199838 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
199839 nSuffix = nTerm-nPrefix;
199840
199841 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
199842 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
199843 ** compared with BINARY collation. This indicates corruption. */
199844 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
199845
199846 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
199847 if( nReq<=p->nNodeSize || !pTree->zTerm ){
199848
199849 if( nReq>p->nNodeSize ){
199850 /* An unusual case: this is the first term to be added to the node
199851 ** and the static node buffer (p->nNodeSize bytes) is not large
199852 ** enough. Use a separately malloced buffer instead This wastes
199853 ** p->nNodeSize bytes, but since this scenario only comes about when
199854 ** the database contain two terms that share a prefix of almost 2KB,
199855 ** this is not expected to be a serious problem.
199856 */
199857 assert( pTree->aData==(char *)&pTree[1] )((void) (0));
199858 pTree->aData = (char *)sqlite3_malloc64(nReq);
199859 if( !pTree->aData ){
199860 return SQLITE_NOMEM7;
199861 }
199862 }
199863
199864 if( pTree->zTerm ){
199865 /* There is no prefix-length field for first term in a node */
199866 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
199867 }
199868
199869 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
199870 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
199871 pTree->nData = nData + nSuffix;
199872 pTree->nEntry++;
199873
199874 if( isCopyTerm ){
199875 if( pTree->nMalloc<nTerm ){
199876 char *zNew = sqlite3_realloc64(pTree->zMalloc, (i64)nTerm*2);
199877 if( !zNew ){
199878 return SQLITE_NOMEM7;
199879 }
199880 pTree->nMalloc = nTerm*2;
199881 pTree->zMalloc = zNew;
199882 }
199883 pTree->zTerm = pTree->zMalloc;
199884 memcpy(pTree->zTerm, zTerm, nTerm);
199885 pTree->nTerm = nTerm;
199886 }else{
199887 pTree->zTerm = (char *)zTerm;
199888 pTree->nTerm = nTerm;
199889 }
199890 return SQLITE_OK0;
199891 }
199892 }
199893
199894 /* If control flows to here, it was not possible to append zTerm to the
199895 ** current node. Create a new node (a right-sibling of the current node).
199896 ** If this is the first node in the tree, the term is added to it.
199897 **
199898 ** Otherwise, the term is not added to the new node, it is left empty for
199899 ** now. Instead, the term is inserted into the parent of pTree. If pTree
199900 ** has no parent, one is created here.
199901 */
199902 pNew = (SegmentNode *)sqlite3_malloc64(sizeof(SegmentNode) + p->nNodeSize);
199903 if( !pNew ){
199904 return SQLITE_NOMEM7;
199905 }
199906 memset(pNew, 0, sizeof(SegmentNode));
199907 pNew->nData = 1 + FTS3_VARINT_MAX10;
199908 pNew->aData = (char *)&pNew[1];
199909
199910 if( pTree ){
199911 SegmentNode *pParent = pTree->pParent;
199912 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
199913 if( pTree->pParent==0 ){
199914 pTree->pParent = pParent;
199915 }
199916 pTree->pRight = pNew;
199917 pNew->pLeftmost = pTree->pLeftmost;
199918 pNew->pParent = pParent;
199919 pNew->zMalloc = pTree->zMalloc;
199920 pNew->nMalloc = pTree->nMalloc;
199921 pTree->zMalloc = 0;
199922 }else{
199923 pNew->pLeftmost = pNew;
199924 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
199925 }
199926
199927 *ppTree = pNew;
199928 return rc;
199929}
199930
199931/*
199932** Helper function for fts3NodeWrite().
199933*/
199934static int fts3TreeFinishNode(
199935 SegmentNode *pTree,
199936 int iHeight,
199937 sqlite3_int64 iLeftChild
199938){
199939 int nStart;
199940 assert( iHeight>=1 && iHeight<128 )((void) (0));
199941 nStart = FTS3_VARINT_MAX10 - sqlite3Fts3VarintLen(iLeftChild);
199942 pTree->aData[nStart] = (char)iHeight;
199943 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
199944 return nStart;
199945}
199946
199947/*
199948** Write the buffer for the segment node pTree and all of its peers to the
199949** database. Then call this function recursively to write the parent of
199950** pTree and its peers to the database.
199951**
199952** Except, if pTree is a root node, do not write it to the database. Instead,
199953** set output variables *paRoot and *pnRoot to contain the root node.
199954**
199955** If successful, SQLITE_OK is returned and output variable *piLast is
199956** set to the largest blockid written to the database (or zero if no
199957** blocks were written to the db). Otherwise, an SQLite error code is
199958** returned.
199959*/
199960static int fts3NodeWrite(
199961 Fts3Table *p, /* Virtual table handle */
199962 SegmentNode *pTree, /* SegmentNode handle */
199963 int iHeight, /* Height of this node in tree */
199964 sqlite3_int64 iLeaf, /* Block id of first leaf node */
199965 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
199966 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
199967 char **paRoot, /* OUT: Data for root node */
199968 int *pnRoot /* OUT: Size of root node in bytes */
199969){
199970 int rc = SQLITE_OK0;
199971
199972 if( !pTree->pParent ){
199973 /* Root node of the tree. */
199974 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
199975 *piLast = iFree-1;
199976 *pnRoot = pTree->nData - nStart;
199977 *paRoot = &pTree->aData[nStart];
199978 }else{
199979 SegmentNode *pIter;
199980 sqlite3_int64 iNextFree = iFree;
199981 sqlite3_int64 iNextLeaf = iLeaf;
199982 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK0; pIter=pIter->pRight){
199983 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
199984 int nWrite = pIter->nData - nStart;
199985
199986 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
199987 iNextFree++;
199988 iNextLeaf += (pIter->nEntry+1);
199989 }
199990 if( rc==SQLITE_OK0 ){
199991 assert( iNextLeaf==iFree )((void) (0));
199992 rc = fts3NodeWrite(
199993 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
199994 );
199995 }
199996 }
199997
199998 return rc;
199999}
200000
200001/*
200002** Free all memory allocations associated with the tree pTree.
200003*/
200004static void fts3NodeFree(SegmentNode *pTree){
200005 if( pTree ){
200006 SegmentNode *p = pTree->pLeftmost;
200007 fts3NodeFree(p->pParent);
200008 while( p ){
200009 SegmentNode *pRight = p->pRight;
200010 if( p->aData!=(char *)&p[1] ){
200011 sqlite3_free(p->aData);
200012 }
200013 assert( pRight==0 || p->zMalloc==0 )((void) (0));
200014 sqlite3_free(p->zMalloc);
200015 sqlite3_free(p);
200016 p = pRight;
200017 }
200018 }
200019}
200020
200021/*
200022** Add a term to the segment being constructed by the SegmentWriter object
200023** *ppWriter. When adding the first term to a segment, *ppWriter should
200024** be passed NULL. This function will allocate a new SegmentWriter object
200025** and return it via the input/output variable *ppWriter in this case.
200026**
200027** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
200028*/
200029static int fts3SegWriterAdd(
200030 Fts3Table *p, /* Virtual table handle */
200031 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
200032 int isCopyTerm, /* True if buffer zTerm must be copied */
200033 const char *zTerm, /* Pointer to buffer containing term */
200034 int nTerm, /* Size of term in bytes */
200035 const char *aDoclist, /* Pointer to buffer containing doclist */
200036 int nDoclist /* Size of doclist in bytes */
200037){
200038 int nPrefix; /* Size of term prefix in bytes */
200039 int nSuffix; /* Size of term suffix in bytes */
200040 i64 nReq; /* Number of bytes required on leaf page */
200041 int nData;
200042 SegmentWriter *pWriter = *ppWriter;
200043
200044 if( !pWriter ){
200045 int rc;
200046 sqlite3_stmt *pStmt;
200047
200048 /* Allocate the SegmentWriter structure */
200049 pWriter = (SegmentWriter *)sqlite3_malloc64(sizeof(SegmentWriter));
200050 if( !pWriter ) return SQLITE_NOMEM7;
200051 memset(pWriter, 0, sizeof(SegmentWriter));
200052 *ppWriter = pWriter;
200053
200054 /* Allocate a buffer in which to accumulate data */
200055 pWriter->aData = (char *)sqlite3_malloc64(p->nNodeSize);
200056 if( !pWriter->aData ) return SQLITE_NOMEM7;
200057 pWriter->nSize = p->nNodeSize;
200058
200059 /* Find the next free blockid in the %_segments table */
200060 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID10, &pStmt, 0);
200061 if( rc!=SQLITE_OK0 ) return rc;
200062 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
200063 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
200064 pWriter->iFirst = pWriter->iFree;
200065 }
200066 rc = sqlite3_reset(pStmt);
200067 if( rc!=SQLITE_OK0 ) return rc;
200068 }
200069 nData = pWriter->nData;
200070
200071 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
200072 nSuffix = nTerm-nPrefix;
200073
200074 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
200075 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
200076 ** compared with BINARY collation. This indicates corruption. */
200077 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
200078
200079 /* Figure out how many bytes are required by this new entry */
200080 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
200081 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
200082 nSuffix + /* Term suffix */
200083 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
200084 nDoclist; /* Doclist data */
200085
200086 if( nData>0 && nData+nReq>p->nNodeSize ){
200087 int rc;
200088
200089 /* The current leaf node is full. Write it out to the database. */
200090 if( pWriter->iFree==LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ) return FTS_CORRUPT_VTAB(11 | (1<<8));
200091 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
200092 if( rc!=SQLITE_OK0 ) return rc;
200093 p->nLeafAdd++;
200094
200095 /* Add the current term to the interior node tree. The term added to
200096 ** the interior tree must:
200097 **
200098 ** a) be greater than the largest term on the leaf node just written
200099 ** to the database (still available in pWriter->zTerm), and
200100 **
200101 ** b) be less than or equal to the term about to be added to the new
200102 ** leaf node (zTerm/nTerm).
200103 **
200104 ** In other words, it must be the prefix of zTerm 1 byte longer than
200105 ** the common prefix (if any) of zTerm and pWriter->zTerm.
200106 */
200107 assert( nPrefix<nTerm )((void) (0));
200108 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
200109 if( rc!=SQLITE_OK0 ) return rc;
200110
200111 nData = 0;
200112 pWriter->nTerm = 0;
200113
200114 nPrefix = 0;
200115 nSuffix = nTerm;
200116 nReq = 1 + /* varint containing prefix size */
200117 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
200118 nTerm + /* Term suffix */
200119 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
200120 nDoclist; /* Doclist data */
200121 }
200122
200123 /* Increase the total number of bytes written to account for the new entry. */
200124 pWriter->nLeafData += nReq;
200125
200126 /* If the buffer currently allocated is too small for this entry, realloc
200127 ** the buffer to make it large enough.
200128 */
200129 if( nReq>pWriter->nSize ){
200130 char *aNew = sqlite3_realloc64(pWriter->aData, nReq);
200131 if( !aNew ) return SQLITE_NOMEM7;
200132 pWriter->aData = aNew;
200133 pWriter->nSize = nReq;
200134 }
200135 assert( nData+nReq<=pWriter->nSize )((void) (0));
200136
200137 /* Append the prefix-compressed term and doclist to the buffer. */
200138 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
200139 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
200140 assert( nSuffix>0 )((void) (0));
200141 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
200142 nData += nSuffix;
200143 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
200144 assert( nDoclist>0 )((void) (0));
200145 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
200146 pWriter->nData = nData + nDoclist;
200147
200148 /* Save the current term so that it can be used to prefix-compress the next.
200149 ** If the isCopyTerm parameter is true, then the buffer pointed to by
200150 ** zTerm is transient, so take a copy of the term data. Otherwise, just
200151 ** store a copy of the pointer.
200152 */
200153 if( isCopyTerm ){
200154 if( nTerm>pWriter->nMalloc ){
200155 char *zNew = sqlite3_realloc64(pWriter->zMalloc, (i64)nTerm*2);
200156 if( !zNew ){
200157 return SQLITE_NOMEM7;
200158 }
200159 pWriter->nMalloc = nTerm*2;
200160 pWriter->zMalloc = zNew;
200161 pWriter->zTerm = zNew;
200162 }
200163 assert( pWriter->zTerm==pWriter->zMalloc )((void) (0));
200164 assert( nTerm>0 )((void) (0));
200165 memcpy(pWriter->zTerm, zTerm, nTerm);
200166 }else{
200167 pWriter->zTerm = (char *)zTerm;
200168 }
200169 pWriter->nTerm = nTerm;
200170
200171 return SQLITE_OK0;
200172}
200173
200174/*
200175** Flush all data associated with the SegmentWriter object pWriter to the
200176** database. This function must be called after all terms have been added
200177** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
200178** returned. Otherwise, an SQLite error code.
200179*/
200180static int fts3SegWriterFlush(
200181 Fts3Table *p, /* Virtual table handle */
200182 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
200183 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
200184 int iIdx /* Value for 'idx' column of %_segdir */
200185){
200186 int rc; /* Return code */
200187 if( pWriter->pTree ){
200188 sqlite3_int64 iLast = 0; /* Largest block id written to database */
200189 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
200190 char *zRoot = NULL((void*)0); /* Pointer to buffer containing root node */
200191 int nRoot = 0; /* Size of buffer zRoot */
200192
200193 iLastLeaf = pWriter->iFree;
200194 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
200195 if( rc==SQLITE_OK0 ){
200196 rc = fts3NodeWrite(p, pWriter->pTree, 1,
200197 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
200198 }
200199 if( rc==SQLITE_OK0 ){
200200 rc = fts3WriteSegdir(p, iLevel, iIdx,
200201 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
200202 }
200203 }else{
200204 /* The entire tree fits on the root node. Write it to the segdir table. */
200205 rc = fts3WriteSegdir(p, iLevel, iIdx,
200206 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
200207 }
200208 p->nLeafAdd++;
200209 return rc;
200210}
200211
200212/*
200213** Release all memory held by the SegmentWriter object passed as the
200214** first argument.
200215*/
200216static void fts3SegWriterFree(SegmentWriter *pWriter){
200217 if( pWriter ){
200218 sqlite3_free(pWriter->aData);
200219 sqlite3_free(pWriter->zMalloc);
200220 fts3NodeFree(pWriter->pTree);
200221 sqlite3_free(pWriter);
200222 }
200223}
200224
200225/*
200226** The first value in the apVal[] array is assumed to contain an integer.
200227** This function tests if there exist any documents with docid values that
200228** are different from that integer. i.e. if deleting the document with docid
200229** pRowid would mean the FTS3 table were empty.
200230**
200231** If successful, *pisEmpty is set to true if the table is empty except for
200232** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
200233** error occurs, an SQLite error code is returned.
200234*/
200235static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
200236 sqlite3_stmt *pStmt;
200237 int rc;
200238 if( p->zContentTbl ){
200239 /* If using the content=xxx option, assume the table is never empty */
200240 *pisEmpty = 0;
200241 rc = SQLITE_OK0;
200242 }else{
200243 rc = fts3SqlStmt(p, SQL_IS_EMPTY1, &pStmt, &pRowid);
200244 if( rc==SQLITE_OK0 ){
200245 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
200246 *pisEmpty = sqlite3_column_int(pStmt, 0);
200247 }
200248 rc = sqlite3_reset(pStmt);
200249 }
200250 }
200251 return rc;
200252}
200253
200254/*
200255** Set *pnMax to the largest segment level in the database for the index
200256** iIndex.
200257**
200258** Segment levels are stored in the 'level' column of the %_segdir table.
200259**
200260** Return SQLITE_OK if successful, or an SQLite error code if not.
200261*/
200262static int fts3SegmentMaxLevel(
200263 Fts3Table *p,
200264 int iLangid,
200265 int iIndex,
200266 sqlite3_int64 *pnMax
200267){
200268 sqlite3_stmt *pStmt;
200269 int rc;
200270 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
200271
200272 /* Set pStmt to the compiled version of:
200273 **
200274 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
200275 **
200276 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
200277 */
200278 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL15, &pStmt, 0);
200279 if( rc!=SQLITE_OK0 ) return rc;
200280 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
200281 sqlite3_bind_int64(pStmt, 2,
200282 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
200283 );
200284 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
200285 *pnMax = sqlite3_column_int64(pStmt, 0);
200286 }
200287 return sqlite3_reset(pStmt);
200288}
200289
200290/*
200291** iAbsLevel is an absolute level that may be assumed to exist within
200292** the database. This function checks if it is the largest level number
200293** within its index. Assuming no error occurs, *pbMax is set to 1 if
200294** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
200295** is returned. If an error occurs, an error code is returned and the
200296** final value of *pbMax is undefined.
200297*/
200298static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
200299
200300 /* Set pStmt to the compiled version of:
200301 **
200302 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
200303 **
200304 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
200305 */
200306 sqlite3_stmt *pStmt;
200307 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL15, &pStmt, 0);
200308 if( rc!=SQLITE_OK0 ) return rc;
200309 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
200310 sqlite3_bind_int64(pStmt, 2,
200311 (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL1024)+1) * FTS3_SEGDIR_MAXLEVEL1024
200312 );
200313
200314 *pbMax = 0;
200315 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
200316 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL5;
200317 }
200318 return sqlite3_reset(pStmt);
200319}
200320
200321/*
200322** Delete all entries in the %_segments table associated with the segment
200323** opened with seg-reader pSeg. This function does not affect the contents
200324** of the %_segdir table.
200325*/
200326static int fts3DeleteSegment(
200327 Fts3Table *p, /* FTS table handle */
200328 Fts3SegReader *pSeg /* Segment to delete */
200329){
200330 int rc = SQLITE_OK0; /* Return code */
200331 if( pSeg->iStartBlock ){
200332 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
200333 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE17, &pDelete, 0);
200334 if( rc==SQLITE_OK0 ){
200335 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
200336 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
200337 sqlite3_step(pDelete);
200338 rc = sqlite3_reset(pDelete);
200339 }
200340 }
200341 return rc;
200342}
200343
200344/*
200345** This function is used after merging multiple segments into a single large
200346** segment to delete the old, now redundant, segment b-trees. Specifically,
200347** it:
200348**
200349** 1) Deletes all %_segments entries for the segments associated with
200350** each of the SegReader objects in the array passed as the third
200351** argument, and
200352**
200353** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
200354** entries regardless of level if (iLevel<0).
200355**
200356** SQLITE_OK is returned if successful, otherwise an SQLite error code.
200357*/
200358static int fts3DeleteSegdir(
200359 Fts3Table *p, /* Virtual table handle */
200360 int iLangid, /* Language id */
200361 int iIndex, /* Index for p->aIndex */
200362 int iLevel, /* Level of %_segdir entries to delete */
200363 Fts3SegReader **apSegment, /* Array of SegReader objects */
200364 int nReader /* Size of array apSegment */
200365){
200366 int rc = SQLITE_OK0; /* Return Code */
200367 int i; /* Iterator variable */
200368 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
200369
200370 for(i=0; rc==SQLITE_OK0 && i<nReader; i++){
200371 rc = fts3DeleteSegment(p, apSegment[i]);
200372 }
200373 if( rc!=SQLITE_OK0 ){
200374 return rc;
200375 }
200376
200377 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL )((void) (0));
200378 if( iLevel==FTS3_SEGCURSOR_ALL-2 ){
200379 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE26, &pDelete, 0);
200380 if( rc==SQLITE_OK0 ){
200381 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
200382 sqlite3_bind_int64(pDelete, 2,
200383 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL1024-1)
200384 );
200385 }
200386 }else{
200387 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL16, &pDelete, 0);
200388 if( rc==SQLITE_OK0 ){
200389 sqlite3_bind_int64(
200390 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
200391 );
200392 }
200393 }
200394
200395 if( rc==SQLITE_OK0 ){
200396 sqlite3_step(pDelete);
200397 rc = sqlite3_reset(pDelete);
200398 }
200399
200400 return rc;
200401}
200402
200403/*
200404** When this function is called, buffer *ppList (size *pnList bytes) contains
200405** a position list that may (or may not) feature multiple columns. This
200406** function adjusts the pointer *ppList and the length *pnList so that they
200407** identify the subset of the position list that corresponds to column iCol.
200408**
200409** If there are no entries in the input position list for column iCol, then
200410** *pnList is set to zero before returning.
200411**
200412** If parameter bZero is non-zero, then any part of the input list following
200413** the end of the output list is zeroed before returning.
200414*/
200415static void fts3ColumnFilter(
200416 int iCol, /* Column to filter on */
200417 int bZero, /* Zero out anything following *ppList */
200418 char **ppList, /* IN/OUT: Pointer to position list */
200419 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
200420){
200421 char *pList = *ppList;
200422 int nList = *pnList;
200423 char *pEnd = &pList[nList];
200424 int iCurrent = 0;
200425 char *p = pList;
200426
200427 assert( iCol>=0 )((void) (0));
200428 while( 1 ){
200429 char c = 0;
200430 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
200431
200432 if( iCol==iCurrent ){
200433 nList = (int)(p - pList);
200434 break;
200435 }
200436
200437 nList -= (int)(p - pList);
200438 pList = p;
200439 if( nList<=0 ){
200440 break;
200441 }
200442 p = &pList[1];
200443 p += fts3GetVarint32(p, &iCurrent)( (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, &iCurrent
) : (*&iCurrent=*(u8*)(p), 1) )
;
200444 }
200445
200446 if( bZero && (pEnd - &pList[nList])>0){
200447 memset(&pList[nList], 0, pEnd - &pList[nList]);
200448 }
200449 *ppList = pList;
200450 *pnList = nList;
200451}
200452
200453/*
200454** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
200455** existing data). Grow the buffer if required.
200456**
200457** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
200458** trying to resize the buffer, return SQLITE_NOMEM.
200459*/
200460static int fts3MsrBufferData(
200461 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
200462 char *pList,
200463 i64 nList
200464){
200465 if( (nList+FTS3_NODE_PADDING(10*2))>pMsr->nBuffer ){
200466 char *pNew;
200467 int nNew = nList*2 + FTS3_NODE_PADDING(10*2);
200468 pNew = (char *)sqlite3_realloc64(pMsr->aBuffer, nNew);
200469 if( !pNew ) return SQLITE_NOMEM7;
200470 pMsr->aBuffer = pNew;
200471 pMsr->nBuffer = nNew;
200472 }
200473
200474 assert( nList>0 )((void) (0));
200475 memcpy(pMsr->aBuffer, pList, nList);
200476 memset(&pMsr->aBuffer[nList], 0, FTS3_NODE_PADDING(10*2));
200477 return SQLITE_OK0;
200478}
200479
200480SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
200481 Fts3Table *p, /* Virtual table handle */
200482 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
200483 sqlite3_int64 *piDocid, /* OUT: Docid value */
200484 char **paPoslist, /* OUT: Pointer to position list */
200485 int *pnPoslist /* OUT: Size of position list in bytes */
200486){
200487 int nMerge = pMsr->nAdvance;
200488 Fts3SegReader **apSegment = pMsr->apSegment;
200489 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
200490 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
200491 );
200492
200493 if( nMerge==0 ){
200494 *paPoslist = 0;
200495 return SQLITE_OK0;
200496 }
200497
200498 while( 1 ){
200499 Fts3SegReader *pSeg;
200500 pSeg = pMsr->apSegment[0];
200501
200502 if( pSeg->pOffsetList==0 ){
200503 *paPoslist = 0;
200504 break;
200505 }else{
200506 int rc;
200507 char *pList;
200508 int nList;
200509 int j;
200510 sqlite3_int64 iDocid = apSegment[0]->iDocid;
200511
200512 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
200513 j = 1;
200514 while( rc==SQLITE_OK0
200515 && j<nMerge
200516 && apSegment[j]->pOffsetList
200517 && apSegment[j]->iDocid==iDocid
200518 ){
200519 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
200520 j++;
200521 }
200522 if( rc!=SQLITE_OK0 ) return rc;
200523 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
200524
200525 if( nList>0 && fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0) ){
200526 rc = fts3MsrBufferData(pMsr, pList, (i64)nList+1);
200527 if( rc!=SQLITE_OK0 ) return rc;
200528 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 )((void) (0));
200529 pList = pMsr->aBuffer;
200530 }
200531
200532 if( pMsr->iColFilter>=0 ){
200533 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
200534 }
200535
200536 if( nList>0 ){
200537 *paPoslist = pList;
200538 *piDocid = iDocid;
200539 *pnPoslist = nList;
200540 break;
200541 }
200542 }
200543 }
200544
200545 return SQLITE_OK0;
200546}
200547
200548static int fts3SegReaderStart(
200549 Fts3Table *p, /* Virtual table handle */
200550 Fts3MultiSegReader *pCsr, /* Cursor object */
200551 const char *zTerm, /* Term searched for (or NULL) */
200552 int nTerm /* Length of zTerm in bytes */
200553){
200554 int i;
200555 int nSeg = pCsr->nSegment;
200556
200557 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
200558 ** for, then advance each segment iterator until it points to a term of
200559 ** equal or greater value than the specified term. This prevents many
200560 ** unnecessary merge/sort operations for the case where single segment
200561 ** b-tree leaf nodes contain more than one term.
200562 */
200563 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
200564 int res = 0;
200565 Fts3SegReader *pSeg = pCsr->apSegment[i];
200566 do {
200567 int rc = fts3SegReaderNext(p, pSeg, 0);
200568 if( rc!=SQLITE_OK0 ) return rc;
200569 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
200570
200571 if( pSeg->bLookup && res!=0 ){
200572 fts3SegReaderSetEof(pSeg);
200573 }
200574 }
200575 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
200576
200577 return SQLITE_OK0;
200578}
200579
200580SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(
200581 Fts3Table *p, /* Virtual table handle */
200582 Fts3MultiSegReader *pCsr, /* Cursor object */
200583 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
200584){
200585 pCsr->pFilter = pFilter;
200586 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
200587}
200588
200589SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
200590 Fts3Table *p, /* Virtual table handle */
200591 Fts3MultiSegReader *pCsr, /* Cursor object */
200592 int iCol, /* Column to match on. */
200593 const char *zTerm, /* Term to iterate through a doclist for */
200594 int nTerm /* Number of bytes in zTerm */
200595){
200596 int i;
200597 int rc;
200598 int nSegment = pCsr->nSegment;
200599 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
200600 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
200601 );
200602
200603 assert( pCsr->pFilter==0 )((void) (0));
200604 assert( zTerm && nTerm>0 )((void) (0));
200605
200606 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
200607 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
200608 if( rc!=SQLITE_OK0 ) return rc;
200609
200610 /* Determine how many of the segments actually point to zTerm/nTerm. */
200611 for(i=0; i<nSegment; i++){
200612 Fts3SegReader *pSeg = pCsr->apSegment[i];
200613 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
200614 break;
200615 }
200616 }
200617 pCsr->nAdvance = i;
200618
200619 /* Advance each of the segments to point to the first docid. */
200620 for(i=0; i<pCsr->nAdvance; i++){
200621 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
200622 if( rc!=SQLITE_OK0 ) return rc;
200623 }
200624 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
200625
200626 assert( iCol<0 || iCol<p->nColumn )((void) (0));
200627 pCsr->iColFilter = iCol;
200628
200629 return SQLITE_OK0;
200630}
200631
200632/*
200633** This function is called on a MultiSegReader that has been started using
200634** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
200635** have been made. Calling this function puts the MultiSegReader in such
200636** a state that if the next two calls are:
200637**
200638** sqlite3Fts3SegReaderStart()
200639** sqlite3Fts3SegReaderStep()
200640**
200641** then the entire doclist for the term is available in
200642** MultiSegReader.aDoclist/nDoclist.
200643*/
200644SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
200645 int i; /* Used to iterate through segment-readers */
200646
200647 assert( pCsr->zTerm==0 )((void) (0));
200648 assert( pCsr->nTerm==0 )((void) (0));
200649 assert( pCsr->aDoclist==0 )((void) (0));
200650 assert( pCsr->nDoclist==0 )((void) (0));
200651
200652 pCsr->nAdvance = 0;
200653 pCsr->bRestart = 1;
200654 for(i=0; i<pCsr->nSegment; i++){
200655 pCsr->apSegment[i]->pOffsetList = 0;
200656 pCsr->apSegment[i]->nOffsetList = 0;
200657 pCsr->apSegment[i]->iDocid = 0;
200658 }
200659
200660 return SQLITE_OK0;
200661}
200662
200663static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, i64 nReq){
200664 if( nReq>pCsr->nBuffer ){
200665 char *aNew;
200666 pCsr->nBuffer = nReq*2;
200667 aNew = sqlite3_realloc64(pCsr->aBuffer, pCsr->nBuffer);
200668 if( !aNew ){
200669 return SQLITE_NOMEM7;
200670 }
200671 pCsr->aBuffer = aNew;
200672 }
200673 return SQLITE_OK0;
200674}
200675
200676
200677SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(
200678 Fts3Table *p, /* Virtual table handle */
200679 Fts3MultiSegReader *pCsr /* Cursor object */
200680){
200681 int rc = SQLITE_OK0;
200682
200683 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY0x00000002);
200684 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS0x00000001);
200685 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER0x00000004);
200686 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX0x00000008);
200687 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN0x00000010);
200688 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST0x00000020);
200689
200690 Fts3SegReader **apSegment = pCsr->apSegment;
200691 int nSegment = pCsr->nSegment;
200692 Fts3SegFilter *pFilter = pCsr->pFilter;
200693 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
200694 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
200695 );
200696
200697 if( pCsr->nSegment==0 ) return SQLITE_OK0;
200698
200699 do {
200700 int nMerge;
200701 int i;
200702
200703 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
200704 ** forward. Then sort the list in order of current term again.
200705 */
200706 for(i=0; i<pCsr->nAdvance; i++){
200707 Fts3SegReader *pSeg = apSegment[i];
200708 if( pSeg->bLookup ){
200709 fts3SegReaderSetEof(pSeg);
200710 }else{
200711 rc = fts3SegReaderNext(p, pSeg, 0);
200712 }
200713 if( rc!=SQLITE_OK0 ) return rc;
200714 }
200715 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
200716 pCsr->nAdvance = 0;
200717
200718 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
200719 assert( rc==SQLITE_OK )((void) (0));
200720 if( apSegment[0]->aNode==0 ) break;
200721
200722 pCsr->nTerm = apSegment[0]->nTerm;
200723 pCsr->zTerm = apSegment[0]->zTerm;
200724
200725 /* If this is a prefix-search, and if the term that apSegment[0] points
200726 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
200727 ** required callbacks have been made. In this case exit early.
200728 **
200729 ** Similarly, if this is a search for an exact match, and the first term
200730 ** of segment apSegment[0] is not a match, exit early.
200731 */
200732 if( pFilter->zTerm && !isScan ){
200733 if( pCsr->nTerm<pFilter->nTerm
200734 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
200735 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
200736 ){
200737 break;
200738 }
200739 }
200740
200741 nMerge = 1;
200742 while( nMerge<nSegment
200743 && apSegment[nMerge]->aNode
200744 && apSegment[nMerge]->nTerm==pCsr->nTerm
200745 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
200746 ){
200747 nMerge++;
200748 }
200749
200750 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) )((void) (0));
200751 if( nMerge==1
200752 && !isIgnoreEmpty
200753 && !isFirst
200754 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0)==0)
200755 ){
200756 pCsr->nDoclist = apSegment[0]->nDoclist;
200757 if( fts3SegReaderIsPending(apSegment[0])((apSegment[0])->ppNextElem!=0) ){
200758 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist,
200759 (i64)pCsr->nDoclist);
200760 pCsr->aDoclist = pCsr->aBuffer;
200761 }else{
200762 pCsr->aDoclist = apSegment[0]->aDoclist;
200763 }
200764 if( rc==SQLITE_OK0 ) rc = SQLITE_ROW100;
200765 }else{
200766 int nDoclist = 0; /* Size of doclist */
200767 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
200768
200769 /* The current term of the first nMerge entries in the array
200770 ** of Fts3SegReader objects is the same. The doclists must be merged
200771 ** and a single term returned with the merged doclist.
200772 */
200773 for(i=0; i<nMerge; i++){
200774 fts3SegReaderFirstDocid(p, apSegment[i]);
200775 }
200776 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
200777 while( apSegment[0]->pOffsetList ){
200778 int j; /* Number of segments that share a docid */
200779 char *pList = 0;
200780 int nList = 0;
200781 int nByte;
200782 sqlite3_int64 iDocid = apSegment[0]->iDocid;
200783 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
200784 j = 1;
200785 while( j<nMerge
200786 && apSegment[j]->pOffsetList
200787 && apSegment[j]->iDocid==iDocid
200788 ){
200789 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
200790 j++;
200791 }
200792
200793 if( isColFilter ){
200794 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
200795 }
200796
200797 if( !isIgnoreEmpty || nList>0 ){
200798
200799 /* Calculate the 'docid' delta value to write into the merged
200800 ** doclist. */
200801 sqlite3_int64 iDelta;
200802 if( p->bDescIdx && nDoclist>0 ){
200803 if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB(11 | (1<<8));
200804 iDelta = (i64)((u64)iPrev - (u64)iDocid);
200805 }else{
200806 if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB(11 | (1<<8));
200807 iDelta = (i64)((u64)iDocid - (u64)iPrev);
200808 }
200809
200810 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
200811
200812 rc = fts3GrowSegReaderBuffer(pCsr,
200813 (i64)nByte+nDoclist+FTS3_NODE_PADDING(10*2));
200814 if( rc ) return rc;
200815
200816 if( isFirst ){
200817 char *a = &pCsr->aBuffer[nDoclist];
200818 int nWrite;
200819
200820 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
200821 if( nWrite ){
200822 iPrev = iDocid;
200823 nDoclist += nWrite;
200824 }
200825 }else{
200826 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
200827 iPrev = iDocid;
200828 if( isRequirePos ){
200829 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
200830 nDoclist += nList;
200831 pCsr->aBuffer[nDoclist++] = '\0';
200832 }
200833 }
200834 }
200835
200836 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
200837 }
200838 if( nDoclist>0 ){
200839 rc = fts3GrowSegReaderBuffer(pCsr, (i64)nDoclist+FTS3_NODE_PADDING(10*2));
200840 if( rc ) return rc;
200841 memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING(10*2));
200842 pCsr->aDoclist = pCsr->aBuffer;
200843 pCsr->nDoclist = nDoclist;
200844 rc = SQLITE_ROW100;
200845 }
200846 }
200847 pCsr->nAdvance = nMerge;
200848 }while( rc==SQLITE_OK0 );
200849
200850 return rc;
200851}
200852
200853
200854SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(
200855 Fts3MultiSegReader *pCsr /* Cursor object */
200856){
200857 if( pCsr ){
200858 int i;
200859 for(i=0; i<pCsr->nSegment; i++){
200860 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
200861 }
200862 sqlite3_free(pCsr->apSegment);
200863 sqlite3_free(pCsr->aBuffer);
200864
200865 pCsr->nSegment = 0;
200866 pCsr->apSegment = 0;
200867 pCsr->aBuffer = 0;
200868 }
200869}
200870
200871/*
200872** Decode the "end_block" field, selected by column iCol of the SELECT
200873** statement passed as the first argument.
200874**
200875** The "end_block" field may contain either an integer, or a text field
200876** containing the text representation of two non-negative integers separated
200877** by one or more space (0x20) characters. In the first case, set *piEndBlock
200878** to the integer value and *pnByte to zero before returning. In the second,
200879** set *piEndBlock to the first value and *pnByte to the second.
200880*/
200881static void fts3ReadEndBlockField(
200882 sqlite3_stmt *pStmt,
200883 int iCol,
200884 i64 *piEndBlock,
200885 i64 *pnByte
200886){
200887 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
200888 if( zText ){
200889 int i;
200890 int iMul = 1;
200891 u64 iVal = 0;
200892 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
200893 iVal = iVal*10 + (zText[i] - '0');
200894 }
200895 *piEndBlock = (i64)iVal;
200896 while( zText[i]==' ' ) i++;
200897 iVal = 0;
200898 if( zText[i]=='-' ){
200899 i++;
200900 iMul = -1;
200901 }
200902 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
200903 iVal = iVal*10 + (zText[i] - '0');
200904 }
200905 *pnByte = ((i64)iVal * (i64)iMul);
200906 }
200907}
200908
200909
200910/*
200911** A segment of size nByte bytes has just been written to absolute level
200912** iAbsLevel. Promote any segments that should be promoted as a result.
200913*/
200914static int fts3PromoteSegments(
200915 Fts3Table *p, /* FTS table handle */
200916 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
200917 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
200918){
200919 int rc = SQLITE_OK0;
200920 sqlite3_stmt *pRange;
200921
200922 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE237, &pRange, 0);
200923
200924 if( rc==SQLITE_OK0 ){
200925 int bOk = 0;
200926 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL1024 + 1) * FTS3_SEGDIR_MAXLEVEL1024 - 1;
200927 i64 nLimit = (nByte*3)/2;
200928
200929 /* Loop through all entries in the %_segdir table corresponding to
200930 ** segments in this index on levels greater than iAbsLevel. If there is
200931 ** at least one such segment, and it is possible to determine that all
200932 ** such segments are smaller than nLimit bytes in size, they will be
200933 ** promoted to level iAbsLevel. */
200934 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
200935 sqlite3_bind_int64(pRange, 2, iLast);
200936 while( SQLITE_ROW100==sqlite3_step(pRange) ){
200937 i64 nSize = 0, dummy;
200938 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
200939 if( nSize<=0 || nSize>nLimit ){
200940 /* If nSize==0, then the %_segdir.end_block field does not not
200941 ** contain a size value. This happens if it was written by an
200942 ** old version of FTS. In this case it is not possible to determine
200943 ** the size of the segment, and so segment promotion does not
200944 ** take place. */
200945 bOk = 0;
200946 break;
200947 }
200948 bOk = 1;
200949 }
200950 rc = sqlite3_reset(pRange);
200951
200952 if( bOk ){
200953 int iIdx = 0;
200954 sqlite3_stmt *pUpdate1 = 0;
200955 sqlite3_stmt *pUpdate2 = 0;
200956
200957 if( rc==SQLITE_OK0 ){
200958 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX38, &pUpdate1, 0);
200959 }
200960 if( rc==SQLITE_OK0 ){
200961 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL39, &pUpdate2, 0);
200962 }
200963
200964 if( rc==SQLITE_OK0 ){
200965
200966 /* Loop through all %_segdir entries for segments in this index with
200967 ** levels equal to or greater than iAbsLevel. As each entry is visited,
200968 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
200969 ** oldest segment in the range, 1 for the next oldest, and so on.
200970 **
200971 ** In other words, move all segments being promoted to level -1,
200972 ** setting the "idx" fields as appropriate to keep them in the same
200973 ** order. The contents of level -1 (which is never used, except
200974 ** transiently here), will be moved back to level iAbsLevel below. */
200975 sqlite3_bind_int64(pRange, 1, iAbsLevel);
200976 while( SQLITE_ROW100==sqlite3_step(pRange) ){
200977 sqlite3_bind_int(pUpdate1, 1, iIdx++);
200978 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
200979 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
200980 sqlite3_step(pUpdate1);
200981 rc = sqlite3_reset(pUpdate1);
200982 if( rc!=SQLITE_OK0 ){
200983 sqlite3_reset(pRange);
200984 break;
200985 }
200986 }
200987 }
200988 if( rc==SQLITE_OK0 ){
200989 rc = sqlite3_reset(pRange);
200990 }
200991
200992 /* Move level -1 to level iAbsLevel */
200993 if( rc==SQLITE_OK0 ){
200994 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
200995 sqlite3_step(pUpdate2);
200996 rc = sqlite3_reset(pUpdate2);
200997 }
200998 }
200999 }
201000
201001
201002 return rc;
201003}
201004
201005/*
201006** Merge all level iLevel segments in the database into a single
201007** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
201008** single segment with a level equal to the numerically largest level
201009** currently present in the database.
201010**
201011** If this function is called with iLevel<0, but there is only one
201012** segment in the database, SQLITE_DONE is returned immediately.
201013** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
201014** an SQLite error code is returned.
201015*/
201016static int fts3SegmentMerge(
201017 Fts3Table *p,
201018 int iLangid, /* Language id to merge */
201019 int iIndex, /* Index in p->aIndex[] to merge */
201020 int iLevel /* Level to merge */
201021){
201022 int rc; /* Return code */
201023 int iIdx = 0; /* Index of new segment */
201024 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
201025 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
201026 Fts3SegFilter filter; /* Segment term filter condition */
201027 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
201028 int bIgnoreEmpty = 0; /* True to ignore empty segments */
201029 i64 iMaxLevel = 0; /* Max level number for this index/langid */
201030
201031 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
201032 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
201033 || iLevel>=0((void) (0))
201034 )((void) (0));
201035 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
201036 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
201037
201038 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
201039 if( rc!=SQLITE_OK0 || csr.nSegment==0 ) goto finished;
201040
201041 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
201042 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
201043 if( rc!=SQLITE_OK0 ) goto finished;
201044 }
201045
201046 if( iLevel==FTS3_SEGCURSOR_ALL-2 ){
201047 /* This call is to merge all segments in the database to a single
201048 ** segment. The level of the new segment is equal to the numerically
201049 ** greatest segment level currently present in the database for this
201050 ** index. The idx of the new segment is always 0. */
201051 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0])((csr.apSegment[0])->ppNextElem!=0) ){
201052 rc = SQLITE_DONE101;
201053 goto finished;
201054 }
201055 iNewLevel = iMaxLevel;
201056 bIgnoreEmpty = 1;
201057
201058 }else{
201059 /* This call is to merge all segments at level iLevel. find the next
201060 ** available segment index at level iLevel+1. The call to
201061 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
201062 ** a single iLevel+2 segment if necessary. */
201063 assert( FTS3_SEGCURSOR_PENDING==-1 )((void) (0));
201064 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
201065 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
201066 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING-1) && (iNewLevel>iMaxLevel);
201067 }
201068 if( rc!=SQLITE_OK0 ) goto finished;
201069
201070 assert( csr.nSegment>0 )((void) (0));
201071 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) )((void) (0));
201072 assert_fts3_nc(((void) (0))
201073 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)((void) (0))
201074 )((void) (0));
201075
201076 memset(&filter, 0, sizeof(Fts3SegFilter));
201077 filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001;
201078 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY0x00000002 : 0);
201079
201080 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
201081 while( SQLITE_OK0==rc ){
201082 rc = sqlite3Fts3SegReaderStep(p, &csr);
201083 if( rc!=SQLITE_ROW100 ) break;
201084 rc = fts3SegWriterAdd(p, &pWriter, 1,
201085 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
201086 }
201087 if( rc!=SQLITE_OK0 ) goto finished;
201088 assert_fts3_nc( pWriter || bIgnoreEmpty )((void) (0));
201089
201090 if( iLevel!=FTS3_SEGCURSOR_PENDING-1 ){
201091 rc = fts3DeleteSegdir(
201092 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
201093 );
201094 if( rc!=SQLITE_OK0 ) goto finished;
201095 }
201096 if( pWriter ){
201097 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
201098 if( rc==SQLITE_OK0 ){
201099 if( iLevel==FTS3_SEGCURSOR_PENDING-1 || iNewLevel<iMaxLevel ){
201100 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
201101 }
201102 }
201103 }
201104
201105 finished:
201106 fts3SegWriterFree(pWriter);
201107 sqlite3Fts3SegReaderFinish(&csr);
201108 return rc;
201109}
201110
201111
201112/*
201113** Flush the contents of pendingTerms to level 0 segments.
201114*/
201115SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
201116 int rc = SQLITE_OK0;
201117 int i;
201118
201119 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
201120 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING-1);
201121 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
201122 }
201123
201124 /* Determine the auto-incr-merge setting if unknown. If enabled,
201125 ** estimate the number of leaf blocks of content to be written
201126 */
201127 if( rc==SQLITE_OK0 && p->bHasStat
201128 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
201129 ){
201130 sqlite3_stmt *pStmt = 0;
201131 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pStmt, 0);
201132 if( rc==SQLITE_OK0 ){
201133 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE2);
201134 rc = sqlite3_step(pStmt);
201135 if( rc==SQLITE_ROW100 ){
201136 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
201137 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
201138 }else if( rc==SQLITE_DONE101 ){
201139 p->nAutoincrmerge = 0;
201140 }
201141 rc = sqlite3_reset(pStmt);
201142 }
201143 }
201144
201145 if( rc==SQLITE_OK0 ){
201146 sqlite3Fts3PendingTermsClear(p);
201147 }
201148 return rc;
201149}
201150
201151/*
201152** Encode N integers as varints into a blob.
201153*/
201154static void fts3EncodeIntArray(
201155 int N, /* The number of integers to encode */
201156 u32 *a, /* The integer values */
201157 char *zBuf, /* Write the BLOB here */
201158 int *pNBuf /* Write number of bytes if zBuf[] used here */
201159){
201160 int i, j;
201161 for(i=j=0; i<N; i++){
201162 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
201163 }
201164 *pNBuf = j;
201165}
201166
201167/*
201168** Decode a blob of varints into N integers
201169*/
201170static void fts3DecodeIntArray(
201171 int N, /* The number of integers to decode */
201172 u32 *a, /* Write the integer values */
201173 const char *zBuf, /* The BLOB containing the varints */
201174 int nBuf /* size of the BLOB */
201175){
201176 int i = 0;
201177 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
201178 int j;
201179 for(i=j=0; i<N && j<nBuf; i++){
201180 sqlite3_int64 x;
201181 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
201182 a[i] = (u32)(x & 0xffffffff);
201183 }
201184 }
201185 while( i<N ) a[i++] = 0;
201186}
201187
201188/*
201189** Insert the sizes (in tokens) for each column of the document
201190** with docid equal to p->iPrevDocid. The sizes are encoded as
201191** a blob of varints.
201192*/
201193static void fts3InsertDocsize(
201194 int *pRC, /* Result code */
201195 Fts3Table *p, /* Table into which to insert */
201196 u32 *aSz /* Sizes of each column, in tokens */
201197){
201198 char *pBlob; /* The BLOB encoding of the document size */
201199 int nBlob; /* Number of bytes in the BLOB */
201200 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
201201 int rc; /* Result code from subfunctions */
201202
201203 if( *pRC ) return;
201204 pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
201205 if( pBlob==0 ){
201206 *pRC = SQLITE_NOMEM7;
201207 return;
201208 }
201209 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
201210 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE20, &pStmt, 0);
201211 if( rc ){
201212 sqlite3_free(pBlob);
201213 *pRC = rc;
201214 return;
201215 }
201216 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
201217 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
201218 sqlite3_step(pStmt);
201219 *pRC = sqlite3_reset(pStmt);
201220}
201221
201222/*
201223** Record 0 of the %_stat table contains a blob consisting of N varints,
201224** where N is the number of user defined columns in the fts3 table plus
201225** two. If nCol is the number of user defined columns, then values of the
201226** varints are set as follows:
201227**
201228** Varint 0: Total number of rows in the table.
201229**
201230** Varint 1..nCol: For each column, the total number of tokens stored in
201231** the column for all rows of the table.
201232**
201233** Varint 1+nCol: The total size, in bytes, of all text values in all
201234** columns of all rows of the table.
201235**
201236*/
201237static void fts3UpdateDocTotals(
201238 int *pRC, /* The result code */
201239 Fts3Table *p, /* Table being updated */
201240 u32 *aSzIns, /* Size increases */
201241 u32 *aSzDel, /* Size decreases */
201242 int nChng /* Change in the number of documents */
201243){
201244 char *pBlob; /* Storage for BLOB written into %_stat */
201245 int nBlob; /* Size of BLOB written into %_stat */
201246 u32 *a; /* Array of integers that becomes the BLOB */
201247 sqlite3_stmt *pStmt; /* Statement for reading and writing */
201248 int i; /* Loop counter */
201249 int rc; /* Result code from subfunctions */
201250
201251 const int nStat = p->nColumn+2;
201252
201253 if( *pRC ) return;
201254 a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
201255 if( a==0 ){
201256 *pRC = SQLITE_NOMEM7;
201257 return;
201258 }
201259 pBlob = (char*)&a[nStat];
201260 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pStmt, 0);
201261 if( rc ){
201262 sqlite3_free(a);
201263 *pRC = rc;
201264 return;
201265 }
201266 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
201267 if( sqlite3_step(pStmt)==SQLITE_ROW100 ){
201268 fts3DecodeIntArray(nStat, a,
201269 sqlite3_column_blob(pStmt, 0),
201270 sqlite3_column_bytes(pStmt, 0));
201271 }else{
201272 memset(a, 0, sizeof(u32)*(nStat) );
201273 }
201274 rc = sqlite3_reset(pStmt);
201275 if( rc!=SQLITE_OK0 ){
201276 sqlite3_free(a);
201277 *pRC = rc;
201278 return;
201279 }
201280 if( nChng<0 && a[0]<(u32)(-nChng) ){
201281 a[0] = 0;
201282 }else{
201283 a[0] += nChng;
201284 }
201285 for(i=0; i<p->nColumn+1; i++){
201286 u32 x = a[i+1];
201287 if( x+aSzIns[i] < aSzDel[i] ){
201288 x = 0;
201289 }else{
201290 x = x + aSzIns[i] - aSzDel[i];
201291 }
201292 a[i+1] = x;
201293 }
201294 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
201295 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pStmt, 0);
201296 if( rc ){
201297 sqlite3_free(a);
201298 *pRC = rc;
201299 return;
201300 }
201301 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL0);
201302 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC((sqlite3_destructor_type)0));
201303 sqlite3_step(pStmt);
201304 *pRC = sqlite3_reset(pStmt);
201305 sqlite3_bind_null(pStmt, 2);
201306 sqlite3_free(a);
201307}
201308
201309/*
201310** Merge the entire database so that there is one segment for each
201311** iIndex/iLangid combination.
201312*/
201313static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
201314 int bSeenDone = 0;
201315 int rc;
201316 sqlite3_stmt *pAllLangid = 0;
201317
201318 rc = sqlite3Fts3PendingTermsFlush(p);
201319 if( rc==SQLITE_OK0 ){
201320 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID27, &pAllLangid, 0);
201321 }
201322 if( rc==SQLITE_OK0 ){
201323 int rc2;
201324 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
201325 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
201326 while( sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
201327 int i;
201328 int iLangid = sqlite3_column_int(pAllLangid, 0);
201329 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
201330 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL-2);
201331 if( rc==SQLITE_DONE101 ){
201332 bSeenDone = 1;
201333 rc = SQLITE_OK0;
201334 }
201335 }
201336 }
201337 rc2 = sqlite3_reset(pAllLangid);
201338 if( rc==SQLITE_OK0 ) rc = rc2;
201339 }
201340
201341 sqlite3Fts3SegmentsClose(p);
201342
201343 return (rc==SQLITE_OK0 && bReturnDone && bSeenDone) ? SQLITE_DONE101 : rc;
201344}
201345
201346/*
201347** This function is called when the user executes the following statement:
201348**
201349** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
201350**
201351** The entire FTS index is discarded and rebuilt. If the table is one
201352** created using the content=xxx option, then the new index is based on
201353** the current contents of the xxx table. Otherwise, it is rebuilt based
201354** on the contents of the %_content table.
201355*/
201356static int fts3DoRebuild(Fts3Table *p){
201357 int rc; /* Return Code */
201358
201359 rc = fts3DeleteAll(p, 0);
201360 if( rc==SQLITE_OK0 ){
201361 u32 *aSz = 0;
201362 u32 *aSzIns = 0;
201363 u32 *aSzDel = 0;
201364 sqlite3_stmt *pStmt = 0;
201365 int nEntry = 0;
201366
201367 /* Compose and prepare an SQL statement to loop through the content table */
201368 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
201369 if( !zSql ){
201370 rc = SQLITE_NOMEM7;
201371 }else{
201372 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
201373 sqlite3_free(zSql);
201374 }
201375
201376 if( rc==SQLITE_OK0 ){
201377 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
201378 aSz = (u32 *)sqlite3_malloc64(nByte);
201379 if( aSz==0 ){
201380 rc = SQLITE_NOMEM7;
201381 }else{
201382 memset(aSz, 0, nByte);
201383 aSzIns = &aSz[p->nColumn+1];
201384 aSzDel = &aSzIns[p->nColumn+1];
201385 }
201386 }
201387
201388 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
201389 int iCol;
201390 int iLangid = langidFromSelect(p, pStmt);
201391 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
201392 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
201393 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
201394 if( p->abNotindexed[iCol]==0 ){
201395 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
201396 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
201397 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
201398 }
201399 }
201400 if( p->bHasDocsize ){
201401 fts3InsertDocsize(&rc, p, aSz);
201402 }
201403 if( rc!=SQLITE_OK0 ){
201404 sqlite3_finalize(pStmt);
201405 pStmt = 0;
201406 }else{
201407 nEntry++;
201408 for(iCol=0; iCol<=p->nColumn; iCol++){
201409 aSzIns[iCol] += aSz[iCol];
201410 }
201411 }
201412 }
201413 if( p->bFts4 ){
201414 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
201415 }
201416 sqlite3_free(aSz);
201417
201418 if( pStmt ){
201419 int rc2 = sqlite3_finalize(pStmt);
201420 if( rc==SQLITE_OK0 ){
201421 rc = rc2;
201422 }
201423 }
201424 }
201425
201426 return rc;
201427}
201428
201429
201430/*
201431** This function opens a cursor used to read the input data for an
201432** incremental merge operation. Specifically, it opens a cursor to scan
201433** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
201434** level iAbsLevel.
201435*/
201436static int fts3IncrmergeCsr(
201437 Fts3Table *p, /* FTS3 table handle */
201438 sqlite3_int64 iAbsLevel, /* Absolute level to open */
201439 int nSeg, /* Number of segments to merge */
201440 Fts3MultiSegReader *pCsr /* Cursor object to populate */
201441){
201442 int rc; /* Return Code */
201443 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
201444 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
201445
201446 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
201447 memset(pCsr, 0, sizeof(*pCsr));
201448 nByte = sizeof(Fts3SegReader *) * nSeg;
201449 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
201450
201451 if( pCsr->apSegment==0 ){
201452 rc = SQLITE_NOMEM7;
201453 }else{
201454 memset(pCsr->apSegment, 0, nByte);
201455 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL12, &pStmt, 0);
201456 }
201457 if( rc==SQLITE_OK0 ){
201458 int i;
201459 int rc2;
201460 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
201461 assert( pCsr->nSegment==0 )((void) (0));
201462 for(i=0; rc==SQLITE_OK0 && sqlite3_step(pStmt)==SQLITE_ROW100 && i<nSeg; i++){
201463 rc = sqlite3Fts3SegReaderNew(i, 0,
201464 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
201465 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
201466 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
201467 sqlite3_column_blob(pStmt, 4), /* segdir.root */
201468 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
201469 &pCsr->apSegment[i]
201470 );
201471 pCsr->nSegment++;
201472 }
201473 rc2 = sqlite3_reset(pStmt);
201474 if( rc==SQLITE_OK0 ) rc = rc2;
201475 }
201476
201477 return rc;
201478}
201479
201480typedef struct IncrmergeWriter IncrmergeWriter;
201481typedef struct NodeWriter NodeWriter;
201482typedef struct Blob Blob;
201483typedef struct NodeReader NodeReader;
201484
201485/*
201486** An instance of the following structure is used as a dynamic buffer
201487** to build up nodes or other blobs of data in.
201488**
201489** The function blobGrowBuffer() is used to extend the allocation.
201490*/
201491struct Blob {
201492 char *a; /* Pointer to allocation */
201493 int n; /* Number of valid bytes of data in a[] */
201494 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
201495};
201496
201497/*
201498** This structure is used to build up buffers containing segment b-tree
201499** nodes (blocks).
201500*/
201501struct NodeWriter {
201502 sqlite3_int64 iBlock; /* Current block id */
201503 Blob key; /* Last key written to the current block */
201504 Blob block; /* Current block image */
201505};
201506
201507/*
201508** An object of this type contains the state required to create or append
201509** to an appendable b-tree segment.
201510*/
201511struct IncrmergeWriter {
201512 int nLeafEst; /* Space allocated for leaf blocks */
201513 int nWork; /* Number of leaf pages flushed */
201514 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
201515 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
201516 sqlite3_int64 iStart; /* Block number of first allocated block */
201517 sqlite3_int64 iEnd; /* Block number of last allocated block */
201518 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
201519 u8 bNoLeafData; /* If true, store 0 for segment size */
201520 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT16];
201521};
201522
201523/*
201524** An object of the following type is used to read data from a single
201525** FTS segment node. See the following functions:
201526**
201527** nodeReaderInit()
201528** nodeReaderNext()
201529** nodeReaderRelease()
201530*/
201531struct NodeReader {
201532 const char *aNode;
201533 int nNode;
201534 int iOff; /* Current offset within aNode[] */
201535
201536 /* Output variables. Containing the current node entry. */
201537 sqlite3_int64 iChild; /* Pointer to child node */
201538 Blob term; /* Current term */
201539 const char *aDoclist; /* Pointer to doclist */
201540 int nDoclist; /* Size of doclist in bytes */
201541};
201542
201543/*
201544** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
201545** Otherwise, if the allocation at pBlob->a is not already at least nMin
201546** bytes in size, extend (realloc) it to be so.
201547**
201548** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
201549** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
201550** to reflect the new size of the pBlob->a[] buffer.
201551*/
201552static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
201553 if( *pRc==SQLITE_OK0 && nMin>pBlob->nAlloc ){
201554 int nAlloc = nMin;
201555 char *a = (char *)sqlite3_realloc64(pBlob->a, nAlloc);
201556 if( a ){
201557 pBlob->nAlloc = nAlloc;
201558 pBlob->a = a;
201559 }else{
201560 *pRc = SQLITE_NOMEM7;
201561 }
201562 }
201563}
201564
201565/*
201566** Attempt to advance the node-reader object passed as the first argument to
201567** the next entry on the node.
201568**
201569** Return an error code if an error occurs (SQLITE_NOMEM is possible).
201570** Otherwise return SQLITE_OK. If there is no next entry on the node
201571** (e.g. because the current entry is the last) set NodeReader->aNode to
201572** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
201573** variables for the new entry.
201574*/
201575static int nodeReaderNext(NodeReader *p){
201576 int bFirst = (p->term.n==0); /* True for first term on the node */
201577 int nPrefix = 0; /* Bytes to copy from previous term */
201578 int nSuffix = 0; /* Bytes to append to the prefix */
201579 int rc = SQLITE_OK0; /* Return code */
201580
201581 assert( p->aNode )((void) (0));
201582 if( p->iChild && bFirst==0 ) p->iChild++;
201583 if( p->iOff>=p->nNode ){
201584 /* EOF */
201585 p->aNode = 0;
201586 }else{
201587 if( bFirst==0 ){
201588 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) )
;
201589 }
201590 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) )
;
201591
201592 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
201593 return FTS_CORRUPT_VTAB(11 | (1<<8));
201594 }
201595 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
201596 if( rc==SQLITE_OK0 && ALWAYS(p->term.a!=0)(p->term.a!=0) ){
201597 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
201598 p->term.n = nPrefix+nSuffix;
201599 p->iOff += nSuffix;
201600 if( p->iChild==0 ){
201601 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) )
;
201602 if( (p->nNode-p->iOff)<p->nDoclist ){
201603 return FTS_CORRUPT_VTAB(11 | (1<<8));
201604 }
201605 p->aDoclist = &p->aNode[p->iOff];
201606 p->iOff += p->nDoclist;
201607 }
201608 }
201609 }
201610
201611 assert_fts3_nc( p->iOff<=p->nNode )((void) (0));
201612 return rc;
201613}
201614
201615/*
201616** Release all dynamic resources held by node-reader object *p.
201617*/
201618static void nodeReaderRelease(NodeReader *p){
201619 sqlite3_free(p->term.a);
201620}
201621
201622/*
201623** Initialize a node-reader object to read the node in buffer aNode/nNode.
201624**
201625** If successful, SQLITE_OK is returned and the NodeReader object set to
201626** point to the first entry on the node (if any). Otherwise, an SQLite
201627** error code is returned.
201628*/
201629static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
201630 memset(p, 0, sizeof(NodeReader));
201631 p->aNode = aNode;
201632 p->nNode = nNode;
201633
201634 /* Figure out if this is a leaf or an internal node. */
201635 if( aNode && aNode[0] ){
201636 /* An internal node. */
201637 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
201638 }else{
201639 p->iOff = 1;
201640 }
201641
201642 return aNode ? nodeReaderNext(p) : SQLITE_OK0;
201643}
201644
201645/*
201646** This function is called while writing an FTS segment each time a leaf o
201647** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
201648** to be greater than the largest key on the node just written, but smaller
201649** than or equal to the first key that will be written to the next leaf
201650** node.
201651**
201652** The block id of the leaf node just written to disk may be found in
201653** (pWriter->aNodeWriter[0].iBlock) when this function is called.
201654*/
201655static int fts3IncrmergePush(
201656 Fts3Table *p, /* Fts3 table handle */
201657 IncrmergeWriter *pWriter, /* Writer object */
201658 const char *zTerm, /* Term to write to internal node */
201659 int nTerm /* Bytes at zTerm */
201660){
201661 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
201662 int iLayer;
201663
201664 assert( nTerm>0 )((void) (0));
201665 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT)(iLayer<16); iLayer++){
201666 sqlite3_int64 iNextPtr = 0;
201667 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
201668 int rc = SQLITE_OK0;
201669 int nPrefix;
201670 int nSuffix;
201671 int nSpace;
201672
201673 /* Figure out how much space the key will consume if it is written to
201674 ** the current node of layer iLayer. Due to the prefix compression,
201675 ** the space required changes depending on which node the key is to
201676 ** be added to. */
201677 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
201678 nSuffix = nTerm - nPrefix;
201679 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201680 nSpace = sqlite3Fts3VarintLen(nPrefix);
201681 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
201682
201683 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
201684 /* If the current node of layer iLayer contains zero keys, or if adding
201685 ** the key to it will not cause it to grow to larger than nNodeSize
201686 ** bytes in size, write the key here. */
201687
201688 Blob *pBlk = &pNode->block;
201689 if( pBlk->n==0 ){
201690 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
201691 if( rc==SQLITE_OK0 ){
201692 pBlk->a[0] = (char)iLayer;
201693 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
201694 }
201695 }
201696 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
201697 blobGrowBuffer(&pNode->key, nTerm, &rc);
201698
201699 if( rc==SQLITE_OK0 ){
201700 if( pNode->key.n ){
201701 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
201702 }
201703 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
201704 assert( nPrefix+nSuffix<=nTerm )((void) (0));
201705 assert( nPrefix>=0 )((void) (0));
201706 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
201707 pBlk->n += nSuffix;
201708
201709 memcpy(pNode->key.a, zTerm, nTerm);
201710 pNode->key.n = nTerm;
201711 }
201712 }else{
201713 /* Otherwise, flush the current node of layer iLayer to disk.
201714 ** Then allocate a new, empty sibling node. The key will be written
201715 ** into the parent of this node. */
201716 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
201717
201718 assert( pNode->block.nAlloc>=p->nNodeSize )((void) (0));
201719 pNode->block.a[0] = (char)iLayer;
201720 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
201721
201722 iNextPtr = pNode->iBlock;
201723 pNode->iBlock++;
201724 pNode->key.n = 0;
201725 }
201726
201727 if( rc!=SQLITE_OK0 || iNextPtr==0 ) return rc;
201728 iPtr = iNextPtr;
201729 }
201730
201731 assert( 0 )((void) (0));
201732 return 0;
201733}
201734
201735/*
201736** Append a term and (optionally) doclist to the FTS segment node currently
201737** stored in blob *pNode. The node need not contain any terms, but the
201738** header must be written before this function is called.
201739**
201740** A node header is a single 0x00 byte for a leaf node, or a height varint
201741** followed by the left-hand-child varint for an internal node.
201742**
201743** The term to be appended is passed via arguments zTerm/nTerm. For a
201744** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
201745** node, both aDoclist and nDoclist must be passed 0.
201746**
201747** If the size of the value in blob pPrev is zero, then this is the first
201748** term written to the node. Otherwise, pPrev contains a copy of the
201749** previous term. Before this function returns, it is updated to contain a
201750** copy of zTerm/nTerm.
201751**
201752** It is assumed that the buffer associated with pNode is already large
201753** enough to accommodate the new entry. The buffer associated with pPrev
201754** is extended by this function if requrired.
201755**
201756** If an error (i.e. OOM condition) occurs, an SQLite error code is
201757** returned. Otherwise, SQLITE_OK.
201758*/
201759static int fts3AppendToNode(
201760 Blob *pNode, /* Current node image to append to */
201761 Blob *pPrev, /* Buffer containing previous term written */
201762 const char *zTerm, /* New term to write */
201763 int nTerm, /* Size of zTerm in bytes */
201764 const char *aDoclist, /* Doclist (or NULL) to write */
201765 int nDoclist /* Size of aDoclist in bytes */
201766){
201767 int rc = SQLITE_OK0; /* Return code */
201768 int bFirst = (pPrev->n==0); /* True if this is the first term written */
201769 int nPrefix; /* Size of term prefix in bytes */
201770 int nSuffix; /* Size of term suffix in bytes */
201771
201772 /* Node must have already been started. There must be a doclist for a
201773 ** leaf node, and there must not be a doclist for an internal node. */
201774 assert( pNode->n>0 )((void) (0));
201775 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) )((void) (0));
201776
201777 blobGrowBuffer(pPrev, nTerm, &rc);
201778 if( rc!=SQLITE_OK0 ) return rc;
201779 assert( pPrev!=0 )((void) (0));
201780 assert( pPrev->a!=0 )((void) (0));
201781
201782 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
201783 nSuffix = nTerm - nPrefix;
201784 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201785 memcpy(pPrev->a, zTerm, nTerm);
201786 pPrev->n = nTerm;
201787
201788 if( bFirst==0 ){
201789 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
201790 }
201791 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
201792 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
201793 pNode->n += nSuffix;
201794
201795 if( aDoclist ){
201796 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
201797 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
201798 pNode->n += nDoclist;
201799 }
201800
201801 assert( pNode->n<=pNode->nAlloc )((void) (0));
201802
201803 return SQLITE_OK0;
201804}
201805
201806/*
201807** Append the current term and doclist pointed to by cursor pCsr to the
201808** appendable b-tree segment opened for writing by pWriter.
201809**
201810** Return SQLITE_OK if successful, or an SQLite error code otherwise.
201811*/
201812static int fts3IncrmergeAppend(
201813 Fts3Table *p, /* Fts3 table handle */
201814 IncrmergeWriter *pWriter, /* Writer object */
201815 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
201816){
201817 const char *zTerm = pCsr->zTerm;
201818 int nTerm = pCsr->nTerm;
201819 const char *aDoclist = pCsr->aDoclist;
201820 int nDoclist = pCsr->nDoclist;
201821 int rc = SQLITE_OK0; /* Return code */
201822 int nSpace; /* Total space in bytes required on leaf */
201823 int nPrefix; /* Size of prefix shared with previous term */
201824 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
201825 NodeWriter *pLeaf; /* Object used to write leaf nodes */
201826
201827 pLeaf = &pWriter->aNodeWriter[0];
201828 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
201829 nSuffix = nTerm - nPrefix;
201830 if(nSuffix<=0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
201831
201832 nSpace = sqlite3Fts3VarintLen(nPrefix);
201833 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
201834 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
201835
201836 /* If the current block is not empty, and if adding this term/doclist
201837 ** to the current block would make it larger than Fts3Table.nNodeSize bytes,
201838 ** and if there is still room for another leaf page, write this block out to
201839 ** the database. */
201840 if( pLeaf->block.n>0
201841 && (pLeaf->block.n + nSpace)>p->nNodeSize
201842 && pLeaf->iBlock < (pWriter->iStart + pWriter->nLeafEst)
201843 ){
201844 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
201845 pWriter->nWork++;
201846
201847 /* Add the current term to the parent node. The term added to the
201848 ** parent must:
201849 **
201850 ** a) be greater than the largest term on the leaf node just written
201851 ** to the database (still available in pLeaf->key), and
201852 **
201853 ** b) be less than or equal to the term about to be added to the new
201854 ** leaf node (zTerm/nTerm).
201855 **
201856 ** In other words, it must be the prefix of zTerm 1 byte longer than
201857 ** the common prefix (if any) of zTerm and pWriter->zTerm.
201858 */
201859 if( rc==SQLITE_OK0 ){
201860 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
201861 }
201862
201863 /* Advance to the next output block */
201864 pLeaf->iBlock++;
201865 pLeaf->key.n = 0;
201866 pLeaf->block.n = 0;
201867
201868 nSuffix = nTerm;
201869 nSpace = 1;
201870 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
201871 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
201872 }
201873
201874 pWriter->nLeafData += nSpace;
201875 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
201876 if( rc==SQLITE_OK0 ){
201877 if( pLeaf->block.n==0 ){
201878 pLeaf->block.n = 1;
201879 pLeaf->block.a[0] = '\0';
201880 }
201881 rc = fts3AppendToNode(
201882 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
201883 );
201884 }
201885
201886 return rc;
201887}
201888
201889/*
201890** This function is called to release all dynamic resources held by the
201891** merge-writer object pWriter, and if no error has occurred, to flush
201892** all outstanding node buffers held by pWriter to disk.
201893**
201894** If *pRc is not SQLITE_OK when this function is called, then no attempt
201895** is made to write any data to disk. Instead, this function serves only
201896** to release outstanding resources.
201897**
201898** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
201899** flushing buffers to disk, *pRc is set to an SQLite error code before
201900** returning.
201901*/
201902static void fts3IncrmergeRelease(
201903 Fts3Table *p, /* FTS3 table handle */
201904 IncrmergeWriter *pWriter, /* Merge-writer object */
201905 int *pRc /* IN/OUT: Error code */
201906){
201907 int i; /* Used to iterate through non-root layers */
201908 int iRoot; /* Index of root in pWriter->aNodeWriter */
201909 NodeWriter *pRoot; /* NodeWriter for root node */
201910 int rc = *pRc; /* Error code */
201911
201912 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
201913 ** root node. If the segment fits entirely on a single leaf node, iRoot
201914 ** will be set to 0. If the root node is the parent of the leaves, iRoot
201915 ** will be 1. And so on. */
201916 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT16-1; iRoot>=0; iRoot--){
201917 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
201918 if( pNode->block.n>0 ) break;
201919 assert( *pRc || pNode->block.nAlloc==0 )((void) (0));
201920 assert( *pRc || pNode->key.nAlloc==0 )((void) (0));
201921 sqlite3_free(pNode->block.a);
201922 sqlite3_free(pNode->key.a);
201923 }
201924
201925 /* Empty output segment. This is a no-op. */
201926 if( iRoot<0 ) return;
201927
201928 /* The entire output segment fits on a single node. Normally, this means
201929 ** the node would be stored as a blob in the "root" column of the %_segdir
201930 ** table. However, this is not permitted in this case. The problem is that
201931 ** space has already been reserved in the %_segments table, and so the
201932 ** start_block and end_block fields of the %_segdir table must be populated.
201933 ** And, by design or by accident, released versions of FTS cannot handle
201934 ** segments that fit entirely on the root node with start_block!=0.
201935 **
201936 ** Instead, create a synthetic root node that contains nothing but a
201937 ** pointer to the single content node. So that the segment consists of a
201938 ** single leaf and a single interior (root) node.
201939 **
201940 ** Todo: Better might be to defer allocating space in the %_segments
201941 ** table until we are sure it is needed.
201942 */
201943 if( iRoot==0 ){
201944 Blob *pBlock = &pWriter->aNodeWriter[1].block;
201945 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX10, &rc);
201946 if( rc==SQLITE_OK0 ){
201947 pBlock->a[0] = 0x01;
201948 pBlock->n = 1 + sqlite3Fts3PutVarint(
201949 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
201950 );
201951 }
201952 iRoot = 1;
201953 }
201954 pRoot = &pWriter->aNodeWriter[iRoot];
201955
201956 /* Flush all currently outstanding nodes to disk. */
201957 for(i=0; i<iRoot; i++){
201958 NodeWriter *pNode = &pWriter->aNodeWriter[i];
201959 if( pNode->block.n>0 && rc==SQLITE_OK0 ){
201960 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
201961 }
201962 sqlite3_free(pNode->block.a);
201963 sqlite3_free(pNode->key.a);
201964 }
201965
201966 /* Write the %_segdir record. */
201967 if( rc==SQLITE_OK0 ){
201968 rc = fts3WriteSegdir(p,
201969 pWriter->iAbsLevel+1, /* level */
201970 pWriter->iIdx, /* idx */
201971 pWriter->iStart, /* start_block */
201972 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
201973 pWriter->iEnd, /* end_block */
201974 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
201975 pRoot->block.a, pRoot->block.n /* root */
201976 );
201977 }
201978 sqlite3_free(pRoot->block.a);
201979 sqlite3_free(pRoot->key.a);
201980
201981 *pRc = rc;
201982}
201983
201984/*
201985** Compare the term in buffer zLhs (size in bytes nLhs) with that in
201986** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
201987** the other, it is considered to be smaller than the other.
201988**
201989** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
201990** if it is greater.
201991*/
201992static int fts3TermCmp(
201993 const char *zLhs, int nLhs, /* LHS of comparison */
201994 const char *zRhs, int nRhs /* RHS of comparison */
201995){
201996 int nCmp = MIN(nLhs, nRhs)((nLhs)<(nRhs)?(nLhs):(nRhs));
201997 int res;
201998
201999 if( nCmp && ALWAYS(zLhs)(zLhs) && ALWAYS(zRhs)(zRhs) ){
202000 res = memcmp(zLhs, zRhs, nCmp);
202001 }else{
202002 res = 0;
202003 }
202004 if( res==0 ) res = nLhs - nRhs;
202005
202006 return res;
202007}
202008
202009
202010/*
202011** Query to see if the entry in the %_segments table with blockid iEnd is
202012** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
202013** returning. Otherwise, set *pbRes to 0.
202014**
202015** Or, if an error occurs while querying the database, return an SQLite
202016** error code. The final value of *pbRes is undefined in this case.
202017**
202018** This is used to test if a segment is an "appendable" segment. If it
202019** is, then a NULL entry has been inserted into the %_segments table
202020** with blockid %_segdir.end_block.
202021*/
202022static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
202023 int bRes = 0; /* Result to set *pbRes to */
202024 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
202025 int rc; /* Return code */
202026
202027 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE34, &pCheck, 0);
202028 if( rc==SQLITE_OK0 ){
202029 sqlite3_bind_int64(pCheck, 1, iEnd);
202030 if( SQLITE_ROW100==sqlite3_step(pCheck) ) bRes = 1;
202031 rc = sqlite3_reset(pCheck);
202032 }
202033
202034 *pbRes = bRes;
202035 return rc;
202036}
202037
202038/*
202039** This function is called when initializing an incremental-merge operation.
202040** It checks if the existing segment with index value iIdx at absolute level
202041** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
202042** merge-writer object *pWriter is initialized to write to it.
202043**
202044** An existing segment can be appended to by an incremental merge if:
202045**
202046** * It was initially created as an appendable segment (with all required
202047** space pre-allocated), and
202048**
202049** * The first key read from the input (arguments zKey and nKey) is
202050** greater than the largest key currently stored in the potential
202051** output segment.
202052*/
202053static int fts3IncrmergeLoad(
202054 Fts3Table *p, /* Fts3 table handle */
202055 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
202056 int iIdx, /* Index of candidate output segment */
202057 const char *zKey, /* First key to write */
202058 int nKey, /* Number of bytes in nKey */
202059 IncrmergeWriter *pWriter /* Populate this object */
202060){
202061 int rc; /* Return code */
202062 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
202063
202064 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR32, &pSelect, 0);
202065 if( rc==SQLITE_OK0 ){
202066 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
202067 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
202068 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
202069 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
202070 int nRoot = 0; /* Size of aRoot[] in bytes */
202071 int rc2; /* Return code from sqlite3_reset() */
202072 int bAppendable = 0; /* Set to true if segment is appendable */
202073
202074 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
202075 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
202076 sqlite3_bind_int(pSelect, 2, iIdx);
202077 if( sqlite3_step(pSelect)==SQLITE_ROW100 ){
202078 iStart = sqlite3_column_int64(pSelect, 1);
202079 iLeafEnd = sqlite3_column_int64(pSelect, 2);
202080 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
202081 if( pWriter->nLeafData<0 ){
202082 pWriter->nLeafData = pWriter->nLeafData * -1;
202083 }
202084 pWriter->bNoLeafData = (pWriter->nLeafData==0);
202085 nRoot = sqlite3_column_bytes(pSelect, 4);
202086 aRoot = sqlite3_column_blob(pSelect, 4);
202087 if( aRoot==0 ){
202088 sqlite3_reset(pSelect);
202089 return nRoot ? SQLITE_NOMEM7 : FTS_CORRUPT_VTAB(11 | (1<<8));
202090 }
202091 }else{
202092 return sqlite3_reset(pSelect);
202093 }
202094
202095 /* Check for the zero-length marker in the %_segments table */
202096 rc = fts3IsAppendable(p, iEnd, &bAppendable);
202097
202098 /* Check that zKey/nKey is larger than the largest key the candidate */
202099 if( rc==SQLITE_OK0 && bAppendable ){
202100 char *aLeaf = 0;
202101 int nLeaf = 0;
202102
202103 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
202104 if( rc==SQLITE_OK0 ){
202105 NodeReader reader;
202106 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
202107 rc==SQLITE_OK0 && reader.aNode;
202108 rc = nodeReaderNext(&reader)
202109 ){
202110 assert( reader.aNode )((void) (0));
202111 }
202112 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
202113 bAppendable = 0;
202114 }
202115 nodeReaderRelease(&reader);
202116 }
202117 sqlite3_free(aLeaf);
202118 }
202119
202120 if( rc==SQLITE_OK0 && bAppendable ){
202121 /* It is possible to append to this segment. Set up the IncrmergeWriter
202122 ** object to do so. */
202123 int i;
202124 int nHeight = (int)aRoot[0];
202125 NodeWriter *pNode;
202126 if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT16 ){
202127 sqlite3_reset(pSelect);
202128 return FTS_CORRUPT_VTAB(11 | (1<<8));
202129 }
202130
202131 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT16;
202132 pWriter->iStart = iStart;
202133 pWriter->iEnd = iEnd;
202134 pWriter->iAbsLevel = iAbsLevel;
202135 pWriter->iIdx = iIdx;
202136
202137 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT16; i++){
202138 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
202139 }
202140
202141 pNode = &pWriter->aNodeWriter[nHeight];
202142 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
202143 blobGrowBuffer(&pNode->block,
202144 MAX(nRoot, p->nNodeSize)((nRoot)>(p->nNodeSize)?(nRoot):(p->nNodeSize))+FTS3_NODE_PADDING(10*2), &rc
202145 );
202146 if( rc==SQLITE_OK0 ){
202147 memcpy(pNode->block.a, aRoot, nRoot);
202148 pNode->block.n = nRoot;
202149 memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING(10*2));
202150 }
202151
202152 for(i=nHeight; i>=0 && rc==SQLITE_OK0; i--){
202153 NodeReader reader;
202154 memset(&reader, 0, sizeof(reader));
202155 pNode = &pWriter->aNodeWriter[i];
202156
202157 if( pNode->block.a){
202158 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
202159 while( reader.aNode && rc==SQLITE_OK0 ) rc = nodeReaderNext(&reader);
202160 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
202161 if( rc==SQLITE_OK0 ){
202162 assert_fts3_nc( reader.term.n>0 || reader.aNode==0 )((void) (0));
202163 if( reader.term.n>0 ){
202164 memcpy(pNode->key.a, reader.term.a, reader.term.n);
202165 }
202166 pNode->key.n = reader.term.n;
202167 if( i>0 ){
202168 char *aBlock = 0;
202169 int nBlock = 0;
202170 pNode = &pWriter->aNodeWriter[i-1];
202171 pNode->iBlock = reader.iChild;
202172 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock,0);
202173 blobGrowBuffer(&pNode->block,
202174 MAX(nBlock, p->nNodeSize)((nBlock)>(p->nNodeSize)?(nBlock):(p->nNodeSize))+FTS3_NODE_PADDING(10*2), &rc
202175 );
202176 if( rc==SQLITE_OK0 ){
202177 memcpy(pNode->block.a, aBlock, nBlock);
202178 pNode->block.n = nBlock;
202179 memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING(10*2));
202180 }
202181 sqlite3_free(aBlock);
202182 }
202183 }
202184 }
202185 nodeReaderRelease(&reader);
202186 }
202187 }
202188
202189 rc2 = sqlite3_reset(pSelect);
202190 if( rc==SQLITE_OK0 ) rc = rc2;
202191 }
202192
202193 return rc;
202194}
202195
202196/*
202197** Determine the largest segment index value that exists within absolute
202198** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
202199** one before returning SQLITE_OK. Or, if there are no segments at all
202200** within level iAbsLevel, set *piIdx to zero.
202201**
202202** If an error occurs, return an SQLite error code. The final value of
202203** *piIdx is undefined in this case.
202204*/
202205static int fts3IncrmergeOutputIdx(
202206 Fts3Table *p, /* FTS Table handle */
202207 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
202208 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
202209){
202210 int rc;
202211 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
202212
202213 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX8, &pOutputIdx, 0);
202214 if( rc==SQLITE_OK0 ){
202215 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
202216 sqlite3_step(pOutputIdx);
202217 *piIdx = sqlite3_column_int(pOutputIdx, 0);
202218 rc = sqlite3_reset(pOutputIdx);
202219 }
202220
202221 return rc;
202222}
202223
202224/*
202225** Allocate an appendable output segment on absolute level iAbsLevel+1
202226** with idx value iIdx.
202227**
202228** In the %_segdir table, a segment is defined by the values in three
202229** columns:
202230**
202231** start_block
202232** leaves_end_block
202233** end_block
202234**
202235** When an appendable segment is allocated, it is estimated that the
202236** maximum number of leaf blocks that may be required is the sum of the
202237** number of leaf blocks consumed by the input segments, plus the number
202238** of input segments, multiplied by two. This value is stored in stack
202239** variable nLeafEst.
202240**
202241** A total of 16*nLeafEst blocks are allocated when an appendable segment
202242** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
202243** array of leaf nodes starts at the first block allocated. The array
202244** of interior nodes that are parents of the leaf nodes start at block
202245** (start_block + (1 + end_block - start_block) / 16). And so on.
202246**
202247** In the actual code below, the value "16" is replaced with the
202248** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
202249*/
202250static int fts3IncrmergeWriter(
202251 Fts3Table *p, /* Fts3 table handle */
202252 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
202253 int iIdx, /* Index of new output segment */
202254 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
202255 IncrmergeWriter *pWriter /* Populate this object */
202256){
202257 int rc; /* Return Code */
202258 int i; /* Iterator variable */
202259 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
202260 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
202261 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
202262
202263 /* Calculate nLeafEst. */
202264 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE29, &pLeafEst, 0);
202265 if( rc==SQLITE_OK0 ){
202266 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
202267 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
202268 if( SQLITE_ROW100==sqlite3_step(pLeafEst) ){
202269 nLeafEst = sqlite3_column_int(pLeafEst, 0);
202270 }
202271 rc = sqlite3_reset(pLeafEst);
202272 }
202273 if( rc!=SQLITE_OK0 ) return rc;
202274
202275 /* Calculate the first block to use in the output segment */
202276 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID10, &pFirstBlock, 0);
202277 if( rc==SQLITE_OK0 ){
202278 if( SQLITE_ROW100==sqlite3_step(pFirstBlock) ){
202279 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
202280 pWriter->iEnd = pWriter->iStart - 1;
202281 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT16;
202282 }
202283 rc = sqlite3_reset(pFirstBlock);
202284 }
202285 if( rc!=SQLITE_OK0 ) return rc;
202286
202287 /* Insert the marker in the %_segments table to make sure nobody tries
202288 ** to steal the space just allocated. This is also used to identify
202289 ** appendable segments. */
202290 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
202291 if( rc!=SQLITE_OK0 ) return rc;
202292
202293 pWriter->iAbsLevel = iAbsLevel;
202294 pWriter->nLeafEst = nLeafEst;
202295 pWriter->iIdx = iIdx;
202296
202297 /* Set up the array of NodeWriter objects */
202298 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT16; i++){
202299 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
202300 }
202301 return SQLITE_OK0;
202302}
202303
202304/*
202305** Remove an entry from the %_segdir table. This involves running the
202306** following two statements:
202307**
202308** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
202309** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
202310**
202311** The DELETE statement removes the specific %_segdir level. The UPDATE
202312** statement ensures that the remaining segments have contiguously allocated
202313** idx values.
202314*/
202315static int fts3RemoveSegdirEntry(
202316 Fts3Table *p, /* FTS3 table handle */
202317 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
202318 int iIdx /* Index of %_segdir entry to delete */
202319){
202320 int rc; /* Return code */
202321 sqlite3_stmt *pDelete = 0; /* DELETE statement */
202322
202323 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY30, &pDelete, 0);
202324 if( rc==SQLITE_OK0 ){
202325 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
202326 sqlite3_bind_int(pDelete, 2, iIdx);
202327 sqlite3_step(pDelete);
202328 rc = sqlite3_reset(pDelete);
202329 }
202330
202331 return rc;
202332}
202333
202334/*
202335** One or more segments have just been removed from absolute level iAbsLevel.
202336** Update the 'idx' values of the remaining segments in the level so that
202337** the idx values are a contiguous sequence starting from 0.
202338*/
202339static int fts3RepackSegdirLevel(
202340 Fts3Table *p, /* FTS3 table handle */
202341 sqlite3_int64 iAbsLevel /* Absolute level to repack */
202342){
202343 int rc; /* Return code */
202344 int *aIdx = 0; /* Array of remaining idx values */
202345 int nIdx = 0; /* Valid entries in aIdx[] */
202346 int nAlloc = 0; /* Allocated size of aIdx[] */
202347 int i; /* Iterator variable */
202348 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
202349 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
202350
202351 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES35, &pSelect, 0);
202352 if( rc==SQLITE_OK0 ){
202353 int rc2;
202354 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
202355 while( SQLITE_ROW100==sqlite3_step(pSelect) ){
202356 if( nIdx>=nAlloc ){
202357 int *aNew;
202358 nAlloc += 16;
202359 aNew = sqlite3_realloc64(aIdx, nAlloc*sizeof(int));
202360 if( !aNew ){
202361 rc = SQLITE_NOMEM7;
202362 break;
202363 }
202364 aIdx = aNew;
202365 }
202366 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
202367 }
202368 rc2 = sqlite3_reset(pSelect);
202369 if( rc==SQLITE_OK0 ) rc = rc2;
202370 }
202371
202372 if( rc==SQLITE_OK0 ){
202373 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY31, &pUpdate, 0);
202374 }
202375 if( rc==SQLITE_OK0 ){
202376 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
202377 }
202378
202379 assert( p->bIgnoreSavepoint==0 )((void) (0));
202380 p->bIgnoreSavepoint = 1;
202381 for(i=0; rc==SQLITE_OK0 && i<nIdx; i++){
202382 if( aIdx[i]!=i ){
202383 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
202384 sqlite3_bind_int(pUpdate, 1, i);
202385 sqlite3_step(pUpdate);
202386 rc = sqlite3_reset(pUpdate);
202387 }
202388 }
202389 p->bIgnoreSavepoint = 0;
202390
202391 sqlite3_free(aIdx);
202392 return rc;
202393}
202394
202395static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
202396 pNode->a[0] = (char)iHeight;
202397 if( iChild ){
202398 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) )((void) (0));
202399 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
202400 }else{
202401 assert( pNode->nAlloc>=1 )((void) (0));
202402 pNode->n = 1;
202403 }
202404}
202405
202406/*
202407** The first two arguments are a pointer to and the size of a segment b-tree
202408** node. The node may be a leaf or an internal node.
202409**
202410** This function creates a new node image in blob object *pNew by copying
202411** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
202412** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
202413*/
202414static int fts3TruncateNode(
202415 const char *aNode, /* Current node image */
202416 int nNode, /* Size of aNode in bytes */
202417 Blob *pNew, /* OUT: Write new node image here */
202418 const char *zTerm, /* Omit all terms smaller than this */
202419 int nTerm, /* Size of zTerm in bytes */
202420 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
202421){
202422 NodeReader reader; /* Reader object */
202423 Blob prev = {0, 0, 0}; /* Previous term written to new node */
202424 int rc = SQLITE_OK0; /* Return code */
202425 int bLeaf; /* True for a leaf node */
202426
202427 if( nNode<1 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
202428 bLeaf = aNode[0]=='\0';
202429
202430 /* Allocate required output space */
202431 blobGrowBuffer(pNew, nNode, &rc);
202432 if( rc!=SQLITE_OK0 ) return rc;
202433 pNew->n = 0;
202434
202435 /* Populate new node buffer */
202436 for(rc = nodeReaderInit(&reader, aNode, nNode);
202437 rc==SQLITE_OK0 && reader.aNode;
202438 rc = nodeReaderNext(&reader)
202439 ){
202440 if( pNew->n==0 ){
202441 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
202442 if( res<0 || (bLeaf==0 && res==0) ) continue;
202443 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
202444 *piBlock = reader.iChild;
202445 }
202446 rc = fts3AppendToNode(
202447 pNew, &prev, reader.term.a, reader.term.n,
202448 reader.aDoclist, reader.nDoclist
202449 );
202450 if( rc!=SQLITE_OK0 ) break;
202451 }
202452 if( pNew->n==0 ){
202453 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
202454 *piBlock = reader.iChild;
202455 }
202456 assert( pNew->n<=pNew->nAlloc )((void) (0));
202457
202458 nodeReaderRelease(&reader);
202459 sqlite3_free(prev.a);
202460 return rc;
202461}
202462
202463/*
202464** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
202465** level iAbsLevel. This may involve deleting entries from the %_segments
202466** table, and modifying existing entries in both the %_segments and %_segdir
202467** tables.
202468**
202469** SQLITE_OK is returned if the segment is updated successfully. Or an
202470** SQLite error code otherwise.
202471*/
202472static int fts3TruncateSegment(
202473 Fts3Table *p, /* FTS3 table handle */
202474 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
202475 int iIdx, /* Index within level of segment to modify */
202476 const char *zTerm, /* Remove terms smaller than this */
202477 int nTerm /* Number of bytes in buffer zTerm */
202478){
202479 int rc = SQLITE_OK0; /* Return code */
202480 Blob root = {0,0,0}; /* New root page image */
202481 Blob block = {0,0,0}; /* Buffer used for any other block */
202482 sqlite3_int64 iBlock = 0; /* Block id */
202483 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
202484 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
202485 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
202486
202487 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR32, &pFetch, 0);
202488 if( rc==SQLITE_OK0 ){
202489 int rc2; /* sqlite3_reset() return code */
202490 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
202491 sqlite3_bind_int(pFetch, 2, iIdx);
202492 if( SQLITE_ROW100==sqlite3_step(pFetch) ){
202493 const char *aRoot = sqlite3_column_blob(pFetch, 4);
202494 int nRoot = sqlite3_column_bytes(pFetch, 4);
202495 iOldStart = sqlite3_column_int64(pFetch, 1);
202496 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
202497 }
202498 rc2 = sqlite3_reset(pFetch);
202499 if( rc==SQLITE_OK0 ) rc = rc2;
202500 }
202501
202502 while( rc==SQLITE_OK0 && iBlock ){
202503 char *aBlock = 0;
202504 int nBlock = 0;
202505 iNewStart = iBlock;
202506
202507 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
202508 if( rc==SQLITE_OK0 ){
202509 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
202510 }
202511 if( rc==SQLITE_OK0 ){
202512 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
202513 }
202514 sqlite3_free(aBlock);
202515 }
202516
202517 /* Variable iNewStart now contains the first valid leaf node. */
202518 if( rc==SQLITE_OK0 && iNewStart ){
202519 sqlite3_stmt *pDel = 0;
202520 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE17, &pDel, 0);
202521 if( rc==SQLITE_OK0 ){
202522 sqlite3_bind_int64(pDel, 1, iOldStart);
202523 sqlite3_bind_int64(pDel, 2, iNewStart-1);
202524 sqlite3_step(pDel);
202525 rc = sqlite3_reset(pDel);
202526 }
202527 }
202528
202529 if( rc==SQLITE_OK0 ){
202530 sqlite3_stmt *pChomp = 0;
202531 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR33, &pChomp, 0);
202532 if( rc==SQLITE_OK0 ){
202533 sqlite3_bind_int64(pChomp, 1, iNewStart);
202534 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC((sqlite3_destructor_type)0));
202535 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
202536 sqlite3_bind_int(pChomp, 4, iIdx);
202537 sqlite3_step(pChomp);
202538 rc = sqlite3_reset(pChomp);
202539 sqlite3_bind_null(pChomp, 2);
202540 }
202541 }
202542
202543 sqlite3_free(root.a);
202544 sqlite3_free(block.a);
202545 return rc;
202546}
202547
202548/*
202549** This function is called after an incrmental-merge operation has run to
202550** merge (or partially merge) two or more segments from absolute level
202551** iAbsLevel.
202552**
202553** Each input segment is either removed from the db completely (if all of
202554** its data was copied to the output segment by the incrmerge operation)
202555** or modified in place so that it no longer contains those entries that
202556** have been duplicated in the output segment.
202557*/
202558static int fts3IncrmergeChomp(
202559 Fts3Table *p, /* FTS table handle */
202560 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
202561 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
202562 int *pnRem /* Number of segments not deleted */
202563){
202564 int i;
202565 int nRem = 0;
202566 int rc = SQLITE_OK0;
202567
202568 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK0; i--){
202569 Fts3SegReader *pSeg = 0;
202570 int j;
202571
202572 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
202573 ** somewhere in the pCsr->apSegment[] array. */
202574 for(j=0; ALWAYS(j<pCsr->nSegment)(j<pCsr->nSegment); j++){
202575 pSeg = pCsr->apSegment[j];
202576 if( pSeg->iIdx==i ) break;
202577 }
202578 assert( j<pCsr->nSegment && pSeg->iIdx==i )((void) (0));
202579
202580 if( pSeg->aNode==0 ){
202581 /* Seg-reader is at EOF. Remove the entire input segment. */
202582 rc = fts3DeleteSegment(p, pSeg);
202583 if( rc==SQLITE_OK0 ){
202584 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
202585 }
202586 *pnRem = 0;
202587 }else{
202588 /* The incremental merge did not copy all the data from this
202589 ** segment to the upper level. The segment is modified in place
202590 ** so that it contains no keys smaller than zTerm/nTerm. */
202591 const char *zTerm = pSeg->zTerm;
202592 int nTerm = pSeg->nTerm;
202593 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
202594 nRem++;
202595 }
202596 }
202597
202598 if( rc==SQLITE_OK0 && nRem!=pCsr->nSegment ){
202599 rc = fts3RepackSegdirLevel(p, iAbsLevel);
202600 }
202601
202602 *pnRem = nRem;
202603 return rc;
202604}
202605
202606/*
202607** Store an incr-merge hint in the database.
202608*/
202609static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
202610 sqlite3_stmt *pReplace = 0;
202611 int rc; /* Return code */
202612
202613 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pReplace, 0);
202614 if( rc==SQLITE_OK0 ){
202615 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT1);
202616 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC((sqlite3_destructor_type)0));
202617 sqlite3_step(pReplace);
202618 rc = sqlite3_reset(pReplace);
202619 sqlite3_bind_null(pReplace, 2);
202620 }
202621
202622 return rc;
202623}
202624
202625/*
202626** Load an incr-merge hint from the database. The incr-merge hint, if one
202627** exists, is stored in the rowid==1 row of the %_stat table.
202628**
202629** If successful, populate blob *pHint with the value read from the %_stat
202630** table and return SQLITE_OK. Otherwise, if an error occurs, return an
202631** SQLite error code.
202632*/
202633static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
202634 sqlite3_stmt *pSelect = 0;
202635 int rc;
202636
202637 pHint->n = 0;
202638 rc = fts3SqlStmt(p, SQL_SELECT_STAT22, &pSelect, 0);
202639 if( rc==SQLITE_OK0 ){
202640 int rc2;
202641 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT1);
202642 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
202643 const char *aHint = sqlite3_column_blob(pSelect, 0);
202644 int nHint = sqlite3_column_bytes(pSelect, 0);
202645 if( aHint ){
202646 blobGrowBuffer(pHint, nHint, &rc);
202647 if( rc==SQLITE_OK0 ){
202648 if( ALWAYS(pHint->a!=0)(pHint->a!=0) ) memcpy(pHint->a, aHint, nHint);
202649 pHint->n = nHint;
202650 }
202651 }
202652 }
202653 rc2 = sqlite3_reset(pSelect);
202654 if( rc==SQLITE_OK0 ) rc = rc2;
202655 }
202656
202657 return rc;
202658}
202659
202660/*
202661** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
202662** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
202663** consists of two varints, the absolute level number of the input segments
202664** and the number of input segments.
202665**
202666** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
202667** set *pRc to an SQLite error code before returning.
202668*/
202669static void fts3IncrmergeHintPush(
202670 Blob *pHint, /* Hint blob to append to */
202671 i64 iAbsLevel, /* First varint to store in hint */
202672 int nInput, /* Second varint to store in hint */
202673 int *pRc /* IN/OUT: Error code */
202674){
202675 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX10, pRc);
202676 if( *pRc==SQLITE_OK0 ){
202677 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
202678 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
202679 }
202680}
202681
202682/*
202683** Read the last entry (most recently pushed) from the hint blob *pHint
202684** and then remove the entry. Write the two values read to *piAbsLevel and
202685** *pnInput before returning.
202686**
202687** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
202688** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
202689*/
202690static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
202691 const int nHint = pHint->n;
202692 int i;
202693
202694 i = pHint->n-1;
202695 if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB(11 | (1<<8));
202696 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
202697 if( i==0 ) return FTS_CORRUPT_VTAB(11 | (1<<8));
202698 i--;
202699 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
202700
202701 pHint->n = i;
202702 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
202703 i += fts3GetVarint32(&pHint->a[i], pnInput)( (*(u8*)(&pHint->a[i])&0x80) ? sqlite3Fts3GetVarint32
(&pHint->a[i], pnInput) : (*pnInput=*(u8*)(&pHint->
a[i]), 1) )
;
202704 assert( i<=nHint )((void) (0));
202705 if( i!=nHint ) return FTS_CORRUPT_VTAB(11 | (1<<8));
202706
202707 return SQLITE_OK0;
202708}
202709
202710
202711/*
202712** Attempt an incremental merge that writes nMerge leaf blocks.
202713**
202714** Incremental merges happen nMin segments at a time. The segments
202715** to be merged are the nMin oldest segments (the ones with the smallest
202716** values for the _segdir.idx field) in the highest level that contains
202717** at least nMin segments. Multiple merges might occur in an attempt to
202718** write the quota of nMerge leaf blocks.
202719*/
202720SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
202721 int rc; /* Return code */
202722 int nRem = nMerge; /* Number of leaf pages yet to be written */
202723 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
202724 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
202725 IncrmergeWriter *pWriter; /* Writer object */
202726 int nSeg = 0; /* Number of input segments */
202727 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
202728 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
202729 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
202730
202731 /* Allocate space for the cursor, filter and writer objects */
202732 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
202733 pWriter = (IncrmergeWriter *)sqlite3_malloc64(nAlloc);
202734 if( !pWriter ) return SQLITE_NOMEM7;
202735 pFilter = (Fts3SegFilter *)&pWriter[1];
202736 pCsr = (Fts3MultiSegReader *)&pFilter[1];
202737
202738 rc = fts3IncrmergeHintLoad(p, &hint);
202739 while( rc==SQLITE_OK0 && nRem>0 ){
202740 const i64 nMod = FTS3_SEGDIR_MAXLEVEL1024 * p->nIndex;
202741 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
202742 int bUseHint = 0; /* True if attempting to append */
202743 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
202744
202745 /* Search the %_segdir table for the absolute level with the smallest
202746 ** relative level number that contains at least nMin segments, if any.
202747 ** If one is found, set iAbsLevel to the absolute level number and
202748 ** nSeg to nMin. If no level with at least nMin segments can be found,
202749 ** set nSeg to -1.
202750 */
202751 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL28, &pFindLevel, 0);
202752 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin)((2)>(nMin)?(2):(nMin)));
202753 if( sqlite3_step(pFindLevel)==SQLITE_ROW100 ){
202754 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
202755 nSeg = sqlite3_column_int(pFindLevel, 1);
202756 assert( nSeg>=2 )((void) (0));
202757 }else{
202758 nSeg = -1;
202759 }
202760 rc = sqlite3_reset(pFindLevel);
202761
202762 /* If the hint read from the %_stat table is not empty, check if the
202763 ** last entry in it specifies a relative level smaller than or equal
202764 ** to the level identified by the block above (if any). If so, this
202765 ** iteration of the loop will work on merging at the hinted level.
202766 */
202767 if( rc==SQLITE_OK0 && hint.n ){
202768 int nHint = hint.n;
202769 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
202770 int nHintSeg = 0; /* Hint number of segments */
202771
202772 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
202773 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
202774 /* Based on the scan in the block above, it is known that there
202775 ** are no levels with a relative level smaller than that of
202776 ** iAbsLevel with more than nSeg segments, or if nSeg is -1,
202777 ** no levels with more than nMin segments. Use this to limit the
202778 ** value of nHintSeg to avoid a large memory allocation in case the
202779 ** merge-hint is corrupt*/
202780 iAbsLevel = iHintAbsLevel;
202781 nSeg = MIN(MAX(nMin,nSeg), nHintSeg)((((nMin)>(nSeg)?(nMin):(nSeg)))<(nHintSeg)?(((nMin)>
(nSeg)?(nMin):(nSeg))):(nHintSeg))
;
202782 bUseHint = 1;
202783 bDirtyHint = 1;
202784 }else{
202785 /* This undoes the effect of the HintPop() above - so that no entry
202786 ** is removed from the hint blob. */
202787 hint.n = nHint;
202788 }
202789 }
202790
202791 /* If nSeg is less that zero, then there is no level with at least
202792 ** nMin segments and no hint in the %_stat table. No work to do.
202793 ** Exit early in this case. */
202794 if( nSeg<=0 ) break;
202795
202796 assert( nMod<=0x7FFFFFFF )((void) (0));
202797 if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){
202798 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
202799 break;
202800 }
202801
202802 /* Open a cursor to iterate through the contents of the oldest nSeg
202803 ** indexes of absolute level iAbsLevel. If this cursor is opened using
202804 ** the 'hint' parameters, it is possible that there are less than nSeg
202805 ** segments available in level iAbsLevel. In this case, no work is
202806 ** done on iAbsLevel - fall through to the next iteration of the loop
202807 ** to start work on some other level. */
202808 memset(pWriter, 0, nAlloc);
202809 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS0x00000001;
202810
202811 if( rc==SQLITE_OK0 ){
202812 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
202813 assert( bUseHint==1 || bUseHint==0 )((void) (0));
202814 if( iIdx==0 || (bUseHint && iIdx==1) ){
202815 int bIgnore = 0;
202816 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
202817 if( bIgnore ){
202818 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
202819 }
202820 }
202821 }
202822
202823 if( rc==SQLITE_OK0 ){
202824 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
202825 }
202826 if( SQLITE_OK0==rc && pCsr->nSegment==nSeg
202827 && SQLITE_OK0==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
202828 ){
202829 int bEmpty = 0;
202830 rc = sqlite3Fts3SegReaderStep(p, pCsr);
202831 if( rc==SQLITE_OK0 ){
202832 bEmpty = 1;
202833 }else if( rc!=SQLITE_ROW100 ){
202834 sqlite3Fts3SegReaderFinish(pCsr);
202835 break;
202836 }
202837 if( bUseHint && iIdx>0 ){
202838 const char *zKey = pCsr->zTerm;
202839 int nKey = pCsr->nTerm;
202840 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
202841 }else{
202842 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
202843 }
202844
202845 if( rc==SQLITE_OK0 && pWriter->nLeafEst ){
202846 fts3LogMerge(nSeg, iAbsLevel);
202847 if( bEmpty==0 ){
202848 do {
202849 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
202850 if( rc==SQLITE_OK0 ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
202851 if( pWriter->nWork>=nRem && rc==SQLITE_ROW100 ) rc = SQLITE_OK0;
202852 }while( rc==SQLITE_ROW100 );
202853 }
202854
202855 /* Update or delete the input segments */
202856 if( rc==SQLITE_OK0 ){
202857 nRem -= (1 + pWriter->nWork);
202858 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
202859 if( nSeg!=0 ){
202860 bDirtyHint = 1;
202861 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
202862 }
202863 }
202864 }
202865
202866 if( nSeg!=0 ){
202867 pWriter->nLeafData = pWriter->nLeafData * -1;
202868 }
202869 fts3IncrmergeRelease(p, pWriter, &rc);
202870 if( nSeg==0 && pWriter->bNoLeafData==0 ){
202871 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
202872 }
202873 }
202874
202875 sqlite3Fts3SegReaderFinish(pCsr);
202876 }
202877
202878 /* Write the hint values into the %_stat table for the next incr-merger */
202879 if( bDirtyHint && rc==SQLITE_OK0 ){
202880 rc = fts3IncrmergeHintStore(p, &hint);
202881 }
202882
202883 sqlite3_free(pWriter);
202884 sqlite3_free(hint.a);
202885 return rc;
202886}
202887
202888/*
202889** Convert the text beginning at *pz into an integer and return
202890** its value. Advance *pz to point to the first character past
202891** the integer.
202892**
202893** This function used for parameters to merge= and incrmerge=
202894** commands.
202895*/
202896static int fts3Getint(const char **pz){
202897 const char *z = *pz;
202898 int i = 0;
202899 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
202900 *pz = z;
202901 return i;
202902}
202903
202904/*
202905** Process statements of the form:
202906**
202907** INSERT INTO table(table) VALUES('merge=A,B');
202908**
202909** A and B are integers that decode to be the number of leaf pages
202910** written for the merge, and the minimum number of segments on a level
202911** before it will be selected for a merge, respectively.
202912*/
202913static int fts3DoIncrmerge(
202914 Fts3Table *p, /* FTS3 table handle */
202915 const char *zParam /* Nul-terminated string containing "A,B" */
202916){
202917 int rc;
202918 int nMin = (MergeCount(p)16 / 2);
202919 int nMerge = 0;
202920 const char *z = zParam;
202921
202922 /* Read the first integer value */
202923 nMerge = fts3Getint(&z);
202924
202925 /* If the first integer value is followed by a ',', read the second
202926 ** integer value. */
202927 if( z[0]==',' && z[1]!='\0' ){
202928 z++;
202929 nMin = fts3Getint(&z);
202930 }
202931
202932 if( z[0]!='\0' || nMin<2 ){
202933 rc = SQLITE_ERROR1;
202934 }else{
202935 rc = SQLITE_OK0;
202936 if( !p->bHasStat ){
202937 assert( p->bFts4==0 )((void) (0));
202938 sqlite3Fts3CreateStatTable(&rc, p);
202939 }
202940 if( rc==SQLITE_OK0 ){
202941 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
202942 }
202943 sqlite3Fts3SegmentsClose(p);
202944 }
202945 return rc;
202946}
202947
202948/*
202949** Process statements of the form:
202950**
202951** INSERT INTO table(table) VALUES('automerge=X');
202952**
202953** where X is an integer. X==0 means to turn automerge off. X!=0 means
202954** turn it on. The setting is persistent.
202955*/
202956static int fts3DoAutoincrmerge(
202957 Fts3Table *p, /* FTS3 table handle */
202958 const char *zParam /* Nul-terminated string containing boolean */
202959){
202960 int rc = SQLITE_OK0;
202961 sqlite3_stmt *pStmt = 0;
202962 p->nAutoincrmerge = fts3Getint(&zParam);
202963 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p)16 ){
202964 p->nAutoincrmerge = 8;
202965 }
202966 if( !p->bHasStat ){
202967 assert( p->bFts4==0 )((void) (0));
202968 sqlite3Fts3CreateStatTable(&rc, p);
202969 if( rc ) return rc;
202970 }
202971 rc = fts3SqlStmt(p, SQL_REPLACE_STAT23, &pStmt, 0);
202972 if( rc ) return rc;
202973 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE2);
202974 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
202975 sqlite3_step(pStmt);
202976 rc = sqlite3_reset(pStmt);
202977 return rc;
202978}
202979
202980/*
202981** Return a 64-bit checksum for the FTS index entry specified by the
202982** arguments to this function.
202983*/
202984static u64 fts3ChecksumEntry(
202985 const char *zTerm, /* Pointer to buffer containing term */
202986 int nTerm, /* Size of zTerm in bytes */
202987 int iLangid, /* Language id for current row */
202988 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
202989 i64 iDocid, /* Docid for current row. */
202990 int iCol, /* Column number */
202991 int iPos /* Position */
202992){
202993 int i;
202994 u64 ret = (u64)iDocid;
202995
202996 ret += (ret<<3) + iLangid;
202997 ret += (ret<<3) + iIndex;
202998 ret += (ret<<3) + iCol;
202999 ret += (ret<<3) + iPos;
203000 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
203001
203002 return ret;
203003}
203004
203005/*
203006** Return a checksum of all entries in the FTS index that correspond to
203007** language id iLangid. The checksum is calculated by XORing the checksums
203008** of each individual entry (see fts3ChecksumEntry()) together.
203009**
203010** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
203011** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
203012** return value is undefined in this case.
203013*/
203014static u64 fts3ChecksumIndex(
203015 Fts3Table *p, /* FTS3 table handle */
203016 int iLangid, /* Language id to return cksum for */
203017 int iIndex, /* Index to cksum (0..p->nIndex-1) */
203018 int *pRc /* OUT: Return code */
203019){
203020 Fts3SegFilter filter;
203021 Fts3MultiSegReader csr;
203022 int rc;
203023 u64 cksum = 0;
203024
203025 if( *pRc ) return 0;
203026
203027 memset(&filter, 0, sizeof(filter));
203028 memset(&csr, 0, sizeof(csr));
203029 filter.flags = FTS3_SEGMENT_REQUIRE_POS0x00000001|FTS3_SEGMENT_IGNORE_EMPTY0x00000002;
203030 filter.flags |= FTS3_SEGMENT_SCAN0x00000010;
203031
203032 rc = sqlite3Fts3SegReaderCursor(
203033 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL-2, 0, 0, 0, 1,&csr
203034 );
203035 if( rc==SQLITE_OK0 ){
203036 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
203037 }
203038
203039 if( rc==SQLITE_OK0 ){
203040 while( SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
203041 char *pCsr = csr.aDoclist;
203042 char *pEnd = &pCsr[csr.nDoclist];
203043
203044 i64 iDocid = 0;
203045 i64 iCol = 0;
203046 u64 iPos = 0;
203047
203048 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
203049 while( pCsr<pEnd ){
203050 u64 iVal = 0;
203051 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
203052 if( pCsr<pEnd ){
203053 if( iVal==0 || iVal==1 ){
203054 iCol = 0;
203055 iPos = 0;
203056 if( iVal ){
203057 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
203058 }else{
203059 pCsr += sqlite3Fts3GetVarintU(pCsr, &iVal);
203060 if( p->bDescIdx ){
203061 iDocid = (i64)((u64)iDocid - iVal);
203062 }else{
203063 iDocid = (i64)((u64)iDocid + iVal);
203064 }
203065 }
203066 }else{
203067 iPos += (iVal - 2);
203068 cksum = cksum ^ fts3ChecksumEntry(
203069 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
203070 (int)iCol, (int)iPos
203071 );
203072 }
203073 }
203074 }
203075 }
203076 }
203077 sqlite3Fts3SegReaderFinish(&csr);
203078
203079 *pRc = rc;
203080 return cksum;
203081}
203082
203083/*
203084** Check if the contents of the FTS index match the current contents of the
203085** content table. If no error occurs and the contents do match, set *pbOk
203086** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
203087** to false before returning.
203088**
203089** If an error occurs (e.g. an OOM or IO error), return an SQLite error
203090** code. The final value of *pbOk is undefined in this case.
203091*/
203092SQLITE_PRIVATEstatic int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk){
203093 int rc = SQLITE_OK0; /* Return code */
203094 u64 cksum1 = 0; /* Checksum based on FTS index contents */
203095 u64 cksum2 = 0; /* Checksum based on %_content contents */
203096 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
203097
203098 /* This block calculates the checksum according to the FTS index. */
203099 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID27, &pAllLangid, 0);
203100 if( rc==SQLITE_OK0 ){
203101 int rc2;
203102 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
203103 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
203104 while( rc==SQLITE_OK0 && sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
203105 int iLangid = sqlite3_column_int(pAllLangid, 0);
203106 int i;
203107 for(i=0; i<p->nIndex; i++){
203108 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
203109 }
203110 }
203111 rc2 = sqlite3_reset(pAllLangid);
203112 if( rc==SQLITE_OK0 ) rc = rc2;
203113 }
203114
203115 /* This block calculates the checksum according to the %_content table */
203116 if( rc==SQLITE_OK0 ){
203117 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
203118 sqlite3_stmt *pStmt = 0;
203119 char *zSql;
203120
203121 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
203122 if( !zSql ){
203123 rc = SQLITE_NOMEM7;
203124 }else{
203125 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
203126 sqlite3_free(zSql);
203127 }
203128
203129 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
203130 i64 iDocid = sqlite3_column_int64(pStmt, 0);
203131 int iLang = langidFromSelect(p, pStmt);
203132 int iCol;
203133
203134 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
203135 if( p->abNotindexed[iCol]==0 ){
203136 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
203137 sqlite3_tokenizer_cursor *pT = 0;
203138
203139 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, -1, &pT);
203140 while( rc==SQLITE_OK0 ){
203141 char const *zToken; /* Buffer containing token */
203142 int nToken = 0; /* Number of bytes in token */
203143 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
203144 int iPos = 0; /* Position of token in zText */
203145
203146 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
203147 if( rc==SQLITE_OK0 ){
203148 int i;
203149 cksum2 = cksum2 ^ fts3ChecksumEntry(
203150 zToken, nToken, iLang, 0, iDocid, iCol, iPos
203151 );
203152 for(i=1; i<p->nIndex; i++){
203153 if( p->aIndex[i].nPrefix<=nToken ){
203154 cksum2 = cksum2 ^ fts3ChecksumEntry(
203155 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
203156 );
203157 }
203158 }
203159 }
203160 }
203161 if( pT ) pModule->xClose(pT);
203162 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
203163 }
203164 }
203165 }
203166
203167 sqlite3_finalize(pStmt);
203168 }
203169
203170 if( rc==SQLITE_CORRUPT_VTAB(11 | (1<<8)) ){
203171 rc = SQLITE_OK0;
203172 *pbOk = 0;
203173 }else{
203174 *pbOk = (rc==SQLITE_OK0 && cksum1==cksum2);
203175 }
203176 return rc;
203177}
203178
203179/*
203180** Run the integrity-check. If no error occurs and the current contents of
203181** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
203182** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
203183**
203184** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
203185** error code.
203186**
203187** The integrity-check works as follows. For each token and indexed token
203188** prefix in the document set, a 64-bit checksum is calculated (by code
203189** in fts3ChecksumEntry()) based on the following:
203190**
203191** + The index number (0 for the main index, 1 for the first prefix
203192** index etc.),
203193** + The token (or token prefix) text itself,
203194** + The language-id of the row it appears in,
203195** + The docid of the row it appears in,
203196** + The column it appears in, and
203197** + The tokens position within that column.
203198**
203199** The checksums for all entries in the index are XORed together to create
203200** a single checksum for the entire index.
203201**
203202** The integrity-check code calculates the same checksum in two ways:
203203**
203204** 1. By scanning the contents of the FTS index, and
203205** 2. By scanning and tokenizing the content table.
203206**
203207** If the two checksums are identical, the integrity-check is deemed to have
203208** passed.
203209*/
203210static int fts3DoIntegrityCheck(
203211 Fts3Table *p /* FTS3 table handle */
203212){
203213 int rc;
203214 int bOk = 0;
203215 rc = sqlite3Fts3IntegrityCheck(p, &bOk);
203216 if( rc==SQLITE_OK0 && bOk==0 ) rc = FTS_CORRUPT_VTAB(11 | (1<<8));
203217 return rc;
203218}
203219
203220/*
203221** Handle a 'special' INSERT of the form:
203222**
203223** "INSERT INTO tbl(tbl) VALUES(<expr>)"
203224**
203225** Argument pVal contains the result of <expr>. Currently the only
203226** meaningful value to insert is the text 'optimize'.
203227*/
203228static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
203229 int rc = SQLITE_ERROR1; /* Return Code */
203230 const char *zVal = (const char *)sqlite3_value_text(pVal);
203231 int nVal = sqlite3_value_bytes(pVal);
203232
203233 if( !zVal ){
203234 return SQLITE_NOMEM7;
203235 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
203236 rc = fts3DoOptimize(p, 0);
203237 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
203238 rc = fts3DoRebuild(p);
203239 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
203240 rc = fts3DoIntegrityCheck(p);
203241 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
203242 rc = fts3DoIncrmerge(p, &zVal[6]);
203243 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
203244 rc = fts3DoAutoincrmerge(p, &zVal[10]);
203245 }else if( nVal==5 && 0==sqlite3_strnicmp(zVal, "flush", 5) ){
203246 rc = sqlite3Fts3PendingTermsFlush(p);
203247 }
203248#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
203249 else{
203250 int v;
203251 if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
203252 v = atoi(&zVal[9]);
203253 if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v;
203254 rc = SQLITE_OK0;
203255 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
203256 v = atoi(&zVal[11]);
203257 if( v>=64 && v<=FTS3_MAX_PENDING_DATA(1*1024*1024) ) p->nMaxPendingData = v;
203258 rc = SQLITE_OK0;
203259 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){
203260 p->bNoIncrDoclist = atoi(&zVal[21]);
203261 rc = SQLITE_OK0;
203262 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){
203263 v = atoi(&zVal[11]);
203264 if( v>=4 && v<=FTS3_MERGE_COUNT16 && (v&1)==0 ) p->nMergeCount = v;
203265 rc = SQLITE_OK0;
203266 }
203267 }
203268#endif
203269 return rc;
203270}
203271
203272#ifndef SQLITE_DISABLE_FTS4_DEFERRED
203273/*
203274** Delete all cached deferred doclists. Deferred doclists are cached
203275** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
203276*/
203277SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
203278 Fts3DeferredToken *pDef;
203279 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
203280 fts3PendingListDelete(pDef->pList);
203281 pDef->pList = 0;
203282 }
203283}
203284
203285/*
203286** Free all entries in the pCsr->pDeffered list. Entries are added to
203287** this list using sqlite3Fts3DeferToken().
203288*/
203289SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
203290 Fts3DeferredToken *pDef;
203291 Fts3DeferredToken *pNext;
203292 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
203293 pNext = pDef->pNext;
203294 fts3PendingListDelete(pDef->pList);
203295 sqlite3_free(pDef);
203296 }
203297 pCsr->pDeferred = 0;
203298}
203299
203300/*
203301** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
203302** based on the row that pCsr currently points to.
203303**
203304** A deferred-doclist is like any other doclist with position information
203305** included, except that it only contains entries for a single row of the
203306** table, not for all rows.
203307*/
203308SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
203309 int rc = SQLITE_OK0; /* Return code */
203310 if( pCsr->pDeferred ){
203311 int i; /* Used to iterate through table columns */
203312 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
203313 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
203314
203315 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
203316 sqlite3_tokenizer *pT = p->pTokenizer;
203317 sqlite3_tokenizer_module const *pModule = pT->pModule;
203318
203319 assert( pCsr->isRequireSeek==0 )((void) (0));
203320 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
203321
203322 for(i=0; i<p->nColumn && rc==SQLITE_OK0; i++){
203323 if( p->abNotindexed[i]==0 ){
203324 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
203325 sqlite3_tokenizer_cursor *pTC = 0;
203326
203327 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
203328 while( rc==SQLITE_OK0 ){
203329 char const *zToken; /* Buffer containing token */
203330 int nToken = 0; /* Number of bytes in token */
203331 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
203332 int iPos = 0; /* Position of token in zText */
203333
203334 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
203335 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
203336 Fts3PhraseToken *pPT = pDef->pToken;
203337 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
203338 && (pPT->bFirst==0 || iPos==0)
203339 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
203340 && (0==memcmp(zToken, pPT->z, pPT->n))
203341 ){
203342 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
203343 }
203344 }
203345 }
203346 if( pTC ) pModule->xClose(pTC);
203347 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
203348 }
203349 }
203350
203351 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
203352 if( pDef->pList ){
203353 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
203354 }
203355 }
203356 }
203357
203358 return rc;
203359}
203360
203361SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(
203362 Fts3DeferredToken *p,
203363 char **ppData,
203364 int *pnData
203365){
203366 char *pRet;
203367 int nSkip;
203368 sqlite3_int64 dummy;
203369
203370 *ppData = 0;
203371 *pnData = 0;
203372
203373 if( p->pList==0 ){
203374 return SQLITE_OK0;
203375 }
203376
203377 pRet = (char *)sqlite3_malloc64(p->pList->nData);
203378 if( !pRet ) return SQLITE_NOMEM7;
203379
203380 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
203381 *pnData = p->pList->nData - nSkip;
203382 *ppData = pRet;
203383
203384 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
203385 return SQLITE_OK0;
203386}
203387
203388/*
203389** Add an entry for token pToken to the pCsr->pDeferred list.
203390*/
203391SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(
203392 Fts3Cursor *pCsr, /* Fts3 table cursor */
203393 Fts3PhraseToken *pToken, /* Token to defer */
203394 int iCol /* Column that token must appear in (or -1) */
203395){
203396 Fts3DeferredToken *pDeferred;
203397 pDeferred = sqlite3_malloc64(sizeof(*pDeferred));
203398 if( !pDeferred ){
203399 return SQLITE_NOMEM7;
203400 }
203401 memset(pDeferred, 0, sizeof(*pDeferred));
203402 pDeferred->pToken = pToken;
203403 pDeferred->pNext = pCsr->pDeferred;
203404 pDeferred->iCol = iCol;
203405 pCsr->pDeferred = pDeferred;
203406
203407 assert( pToken->pDeferred==0 )((void) (0));
203408 pToken->pDeferred = pDeferred;
203409
203410 return SQLITE_OK0;
203411}
203412#endif
203413
203414/*
203415** SQLite value pRowid contains the rowid of a row that may or may not be
203416** present in the FTS3 table. If it is, delete it and adjust the contents
203417** of subsiduary data structures accordingly.
203418*/
203419static int fts3DeleteByRowid(
203420 Fts3Table *p,
203421 sqlite3_value *pRowid,
203422 int *pnChng, /* IN/OUT: Decrement if row is deleted */
203423 u32 *aSzDel
203424){
203425 int rc = SQLITE_OK0; /* Return code */
203426 int bFound = 0; /* True if *pRowid really is in the table */
203427
203428 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
203429 if( bFound && rc==SQLITE_OK0 ){
203430 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
203431 rc = fts3IsEmpty(p, pRowid, &isEmpty);
203432 if( rc==SQLITE_OK0 ){
203433 if( isEmpty ){
203434 /* Deleting this row means the whole table is empty. In this case
203435 ** delete the contents of all three tables and throw away any
203436 ** data in the pendingTerms hash table. */
203437 rc = fts3DeleteAll(p, 1);
203438 *pnChng = 0;
203439 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
203440 }else{
203441 *pnChng = *pnChng - 1;
203442 if( p->zContentTbl==0 ){
203443 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT0, &pRowid);
203444 }
203445 if( p->bHasDocsize ){
203446 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE19, &pRowid);
203447 }
203448 }
203449 }
203450 }
203451
203452 return rc;
203453}
203454
203455/*
203456** This function does the work for the xUpdate method of FTS3 virtual
203457** tables. The schema of the virtual table being:
203458**
203459** CREATE TABLE <table name>(
203460** <user columns>,
203461** <table name> HIDDEN,
203462** docid HIDDEN,
203463** <langid> HIDDEN
203464** );
203465**
203466**
203467*/
203468SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(
203469 sqlite3_vtab *pVtab, /* FTS3 vtab object */
203470 int nArg, /* Size of argument array */
203471 sqlite3_value **apVal, /* Array of arguments */
203472 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
203473){
203474 Fts3Table *p = (Fts3Table *)pVtab;
203475 int rc = SQLITE_OK0; /* Return Code */
203476 u32 *aSzIns = 0; /* Sizes of inserted documents */
203477 u32 *aSzDel = 0; /* Sizes of deleted documents */
203478 int nChng = 0; /* Net change in number of documents */
203479 int bInsertDone = 0;
203480
203481 /* At this point it must be known if the %_stat table exists or not.
203482 ** So bHasStat may not be 2. */
203483 assert( p->bHasStat==0 || p->bHasStat==1 )((void) (0));
203484
203485 assert( p->pSegments==0 )((void) (0));
203486 assert(((void) (0))
203487 nArg==1 /* DELETE operations */((void) (0))
203488 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */((void) (0))
203489 )((void) (0));
203490
203491 /* Check for a "special" INSERT operation. One of the form:
203492 **
203493 ** INSERT INTO xyz(xyz) VALUES('command');
203494 */
203495 if( nArg>1
203496 && sqlite3_value_type(apVal[0])==SQLITE_NULL5
203497 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL5
203498 ){
203499 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
203500 goto update_out;
203501 }
203502
203503 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
203504 rc = SQLITE_CONSTRAINT19;
203505 goto update_out;
203506 }
203507
203508 /* Allocate space to hold the change in document sizes */
203509 aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
203510 if( aSzDel==0 ){
203511 rc = SQLITE_NOMEM7;
203512 goto update_out;
203513 }
203514 aSzIns = &aSzDel[p->nColumn+1];
203515 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
203516
203517 rc = fts3Writelock(p);
203518 if( rc!=SQLITE_OK0 ) goto update_out;
203519
203520 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
203521 ** value, then this operation requires constraint handling.
203522 **
203523 ** If the on-conflict mode is REPLACE, this means that the existing row
203524 ** should be deleted from the database before inserting the new row. Or,
203525 ** if the on-conflict mode is other than REPLACE, then this method must
203526 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
203527 ** modify the database file.
203528 */
203529 if( nArg>1 && p->zContentTbl==0 ){
203530 /* Find the value object that holds the new rowid value. */
203531 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
203532 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL5 ){
203533 pNewRowid = apVal[1];
203534 }
203535
203536 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL5 && (
203537 sqlite3_value_type(apVal[0])==SQLITE_NULL5
203538 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
203539 )){
203540 /* The new rowid is not NULL (in this case the rowid will be
203541 ** automatically assigned and there is no chance of a conflict), and
203542 ** the statement is either an INSERT or an UPDATE that modifies the
203543 ** rowid column. So if the conflict mode is REPLACE, then delete any
203544 ** existing row with rowid=pNewRowid.
203545 **
203546 ** Or, if the conflict mode is not REPLACE, insert the new record into
203547 ** the %_content table. If we hit the duplicate rowid constraint (or any
203548 ** other error) while doing so, return immediately.
203549 **
203550 ** This branch may also run if pNewRowid contains a value that cannot
203551 ** be losslessly converted to an integer. In this case, the eventual
203552 ** call to fts3InsertData() (either just below or further on in this
203553 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
203554 ** invoked, it will delete zero rows (since no row will have
203555 ** docid=$pNewRowid if $pNewRowid is not an integer value).
203556 */
203557 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE5 ){
203558 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
203559 }else{
203560 rc = fts3InsertData(p, apVal, pRowid);
203561 bInsertDone = 1;
203562 }
203563 }
203564 }
203565 if( rc!=SQLITE_OK0 ){
203566 goto update_out;
203567 }
203568
203569 /* If this is a DELETE or UPDATE operation, remove the old record. */
203570 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL5 ){
203571 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER )((void) (0));
203572 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
203573 }
203574
203575 /* If this is an INSERT or UPDATE operation, insert the new record. */
203576 if( nArg>1 && rc==SQLITE_OK0 ){
203577 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
203578 if( bInsertDone==0 ){
203579 rc = fts3InsertData(p, apVal, pRowid);
203580 if( rc==SQLITE_CONSTRAINT19 && p->zContentTbl==0 ){
203581 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
203582 }
203583 }
203584 if( rc==SQLITE_OK0 ){
203585 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
203586 }
203587 if( rc==SQLITE_OK0 ){
203588 assert( p->iPrevDocid==*pRowid )((void) (0));
203589 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
203590 }
203591 if( p->bHasDocsize ){
203592 fts3InsertDocsize(&rc, p, aSzIns);
203593 }
203594 nChng++;
203595 }
203596
203597 if( p->bFts4 ){
203598 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
203599 }
203600
203601 update_out:
203602 sqlite3_free(aSzDel);
203603 sqlite3Fts3SegmentsClose(p);
203604 return rc;
203605}
203606
203607/*
203608** Flush any data in the pending-terms hash table to disk. If successful,
203609** merge all segments in the database (including the new segment, if
203610** there was any data to flush) into a single segment.
203611*/
203612SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *p){
203613 int rc;
203614 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
203615 if( rc==SQLITE_OK0 ){
203616 rc = fts3DoOptimize(p, 1);
203617 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
203618 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
203619 if( rc2!=SQLITE_OK0 ) rc = rc2;
203620 }else{
203621 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
203622 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
203623 }
203624 }
203625 sqlite3Fts3SegmentsClose(p);
203626 return rc;
203627}
203628
203629#endif
203630
203631/************** End of fts3_write.c ******************************************/
203632/************** Begin file fts3_snippet.c ************************************/
203633/*
203634** 2009 Oct 23
203635**
203636** The author disclaims copyright to this source code. In place of
203637** a legal notice, here is a blessing:
203638**
203639** May you do good and not evil.
203640** May you find forgiveness for yourself and forgive others.
203641** May you share freely, never taking more than you give.
203642**
203643******************************************************************************
203644*/
203645
203646/* #include "fts3Int.h" */
203647#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
203648
203649/* #include <string.h> */
203650/* #include <assert.h> */
203651
203652#ifndef SQLITE_AMALGAMATION1
203653typedef sqlite3_int64 i64;
203654#endif
203655
203656/*
203657** Characters that may appear in the second argument to matchinfo().
203658*/
203659#define FTS3_MATCHINFO_NPHRASE'p' 'p' /* 1 value */
203660#define FTS3_MATCHINFO_NCOL'c' 'c' /* 1 value */
203661#define FTS3_MATCHINFO_NDOC'n' 'n' /* 1 value */
203662#define FTS3_MATCHINFO_AVGLENGTH'a' 'a' /* nCol values */
203663#define FTS3_MATCHINFO_LENGTH'l' 'l' /* nCol values */
203664#define FTS3_MATCHINFO_LCS's' 's' /* nCol values */
203665#define FTS3_MATCHINFO_HITS'x' 'x' /* 3*nCol*nPhrase values */
203666#define FTS3_MATCHINFO_LHITS'y' 'y' /* nCol*nPhrase values */
203667#define FTS3_MATCHINFO_LHITS_BM'b' 'b' /* nCol*nPhrase values */
203668
203669/*
203670** The default value for the second argument to matchinfo().
203671*/
203672#define FTS3_MATCHINFO_DEFAULT"pcx" "pcx"
203673
203674
203675/*
203676** Used as an sqlite3Fts3ExprIterate() context when loading phrase doclists to
203677** Fts3Expr.aDoclist[]/nDoclist.
203678*/
203679typedef struct LoadDoclistCtx LoadDoclistCtx;
203680struct LoadDoclistCtx {
203681 Fts3Cursor *pCsr; /* FTS3 Cursor */
203682 int nPhrase; /* Number of phrases seen so far */
203683 int nToken; /* Number of tokens seen so far */
203684};
203685
203686/*
203687** The following types are used as part of the implementation of the
203688** fts3BestSnippet() routine.
203689*/
203690typedef struct SnippetIter SnippetIter;
203691typedef struct SnippetPhrase SnippetPhrase;
203692typedef struct SnippetFragment SnippetFragment;
203693
203694struct SnippetIter {
203695 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
203696 int iCol; /* Extract snippet from this column */
203697 int nSnippet; /* Requested snippet length (in tokens) */
203698 int nPhrase; /* Number of phrases in query */
203699 SnippetPhrase *aPhrase; /* Array of size nPhrase */
203700 int iCurrent; /* First token of current snippet */
203701};
203702
203703struct SnippetPhrase {
203704 int nToken; /* Number of tokens in phrase */
203705 char *pList; /* Pointer to start of phrase position list */
203706 i64 iHead; /* Next value in position list */
203707 char *pHead; /* Position list data following iHead */
203708 i64 iTail; /* Next value in trailing position list */
203709 char *pTail; /* Position list data following iTail */
203710};
203711
203712struct SnippetFragment {
203713 int iCol; /* Column snippet is extracted from */
203714 int iPos; /* Index of first token in snippet */
203715 u64 covered; /* Mask of query phrases covered */
203716 u64 hlmask; /* Mask of snippet terms to highlight */
203717};
203718
203719/*
203720** This type is used as an sqlite3Fts3ExprIterate() context object while
203721** accumulating the data returned by the matchinfo() function.
203722*/
203723typedef struct MatchInfo MatchInfo;
203724struct MatchInfo {
203725 Fts3Cursor *pCursor; /* FTS3 Cursor */
203726 int nCol; /* Number of columns in table */
203727 int nPhrase; /* Number of matchable phrases in query */
203728 sqlite3_int64 nDoc; /* Number of docs in database */
203729 char flag;
203730 u32 *aMatchinfo; /* Pre-allocated buffer */
203731};
203732
203733/*
203734** An instance of this structure is used to manage a pair of buffers, each
203735** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
203736** for details.
203737*/
203738struct MatchinfoBuffer {
203739 u8 aRef[3];
203740 int nElem;
203741 int bGlobal; /* Set if global data is loaded */
203742 char *zMatchinfo;
203743 u32 aMatchinfo[1];
203744};
203745
203746
203747/*
203748** The snippet() and offsets() functions both return text values. An instance
203749** of the following structure is used to accumulate those values while the
203750** functions are running. See fts3StringAppend() for details.
203751*/
203752typedef struct StrBuffer StrBuffer;
203753struct StrBuffer {
203754 char *z; /* Pointer to buffer containing string */
203755 int n; /* Length of z in bytes (excl. nul-term) */
203756 int nAlloc; /* Allocated size of buffer z in bytes */
203757};
203758
203759
203760/*************************************************************************
203761** Start of MatchinfoBuffer code.
203762*/
203763
203764/*
203765** Allocate a two-slot MatchinfoBuffer object.
203766*/
203767static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
203768 MatchinfoBuffer *pRet;
203769 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
203770 + sizeof(MatchinfoBuffer);
203771 sqlite3_int64 nStr = strlen(zMatchinfo);
203772
203773 pRet = sqlite3Fts3MallocZero(nByte + nStr+1);
203774 if( pRet ){
203775 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
203776 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
203777 + sizeof(u32)*((int)nElem+1);
203778 pRet->nElem = (int)nElem;
203779 pRet->zMatchinfo = ((char*)pRet) + nByte;
203780 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
203781 pRet->aRef[0] = 1;
203782 }
203783
203784 return pRet;
203785}
203786
203787static void fts3MIBufferFree(void *p){
203788 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
203789
203790 assert( (u32*)p==&pBuf->aMatchinfo[1]((void) (0))
203791 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]((void) (0))
203792 )((void) (0));
203793 if( (u32*)p==&pBuf->aMatchinfo[1] ){
203794 pBuf->aRef[1] = 0;
203795 }else{
203796 pBuf->aRef[2] = 0;
203797 }
203798
203799 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
203800 sqlite3_free(pBuf);
203801 }
203802}
203803
203804static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
203805 void (*xRet)(void*) = 0;
203806 u32 *aOut = 0;
203807
203808 if( p->aRef[1]==0 ){
203809 p->aRef[1] = 1;
203810 aOut = &p->aMatchinfo[1];
203811 xRet = fts3MIBufferFree;
203812 }
203813 else if( p->aRef[2]==0 ){
203814 p->aRef[2] = 1;
203815 aOut = &p->aMatchinfo[p->nElem+2];
203816 xRet = fts3MIBufferFree;
203817 }else{
203818 aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
203819 if( aOut ){
203820 xRet = sqlite3_free;
203821 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
203822 }
203823 }
203824
203825 *paOut = aOut;
203826 return xRet;
203827}
203828
203829static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
203830 p->bGlobal = 1;
203831 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
203832}
203833
203834/*
203835** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
203836*/
203837SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
203838 if( p ){
203839 assert( p->aRef[0]==1 )((void) (0));
203840 p->aRef[0] = 0;
203841 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
203842 sqlite3_free(p);
203843 }
203844 }
203845}
203846
203847/*
203848** End of MatchinfoBuffer code.
203849*************************************************************************/
203850
203851
203852/*
203853** This function is used to help iterate through a position-list. A position
203854** list is a list of unique integers, sorted from smallest to largest. Each
203855** element of the list is represented by an FTS3 varint that takes the value
203856** of the difference between the current element and the previous one plus
203857** two. For example, to store the position-list:
203858**
203859** 4 9 113
203860**
203861** the three varints:
203862**
203863** 6 7 106
203864**
203865** are encoded.
203866**
203867** When this function is called, *pp points to the start of an element of
203868** the list. *piPos contains the value of the previous entry in the list.
203869** After it returns, *piPos contains the value of the next element of the
203870** list and *pp is advanced to the following varint.
203871*/
203872static void fts3GetDeltaPosition(char **pp, i64 *piPos){
203873 int iVal;
203874 *pp += fts3GetVarint32(*pp, &iVal)( (*(u8*)(*pp)&0x80) ? sqlite3Fts3GetVarint32(*pp, &iVal
) : (*&iVal=*(u8*)(*pp), 1) )
;
203875 *piPos += (iVal-2);
203876}
203877
203878/*
203879** Helper function for sqlite3Fts3ExprIterate() (see below).
203880*/
203881static int fts3ExprIterate2(
203882 Fts3Expr *pExpr, /* Expression to iterate phrases of */
203883 int *piPhrase, /* Pointer to phrase counter */
203884 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
203885 void *pCtx /* Second argument to pass to callback */
203886){
203887 int rc; /* Return code */
203888 int eType = pExpr->eType; /* Type of expression node pExpr */
203889
203890 if( eType!=FTSQUERY_PHRASE5 ){
203891 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
203892 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
203893 if( rc==SQLITE_OK0 && eType!=FTSQUERY_NOT2 ){
203894 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
203895 }
203896 }else{
203897 rc = x(pExpr, *piPhrase, pCtx);
203898 (*piPhrase)++;
203899 }
203900 return rc;
203901}
203902
203903/*
203904** Iterate through all phrase nodes in an FTS3 query, except those that
203905** are part of a sub-tree that is the right-hand-side of a NOT operator.
203906** For each phrase node found, the supplied callback function is invoked.
203907**
203908** If the callback function returns anything other than SQLITE_OK,
203909** the iteration is abandoned and the error code returned immediately.
203910** Otherwise, SQLITE_OK is returned after a callback has been made for
203911** all eligible phrase nodes.
203912*/
203913SQLITE_PRIVATEstatic int sqlite3Fts3ExprIterate(
203914 Fts3Expr *pExpr, /* Expression to iterate phrases of */
203915 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
203916 void *pCtx /* Second argument to pass to callback */
203917){
203918 int iPhrase = 0; /* Variable used as the phrase counter */
203919 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
203920}
203921
203922/*
203923** This is an sqlite3Fts3ExprIterate() callback used while loading the
203924** doclists for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
203925** fts3ExprLoadDoclists().
203926*/
203927static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
203928 int rc = SQLITE_OK0;
203929 Fts3Phrase *pPhrase = pExpr->pPhrase;
203930 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
203931
203932 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
203933
203934 p->nPhrase++;
203935 p->nToken += pPhrase->nToken;
203936
203937 return rc;
203938}
203939
203940/*
203941** Load the doclists for each phrase in the query associated with FTS3 cursor
203942** pCsr.
203943**
203944** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
203945** phrases in the expression (all phrases except those directly or
203946** indirectly descended from the right-hand-side of a NOT operator). If
203947** pnToken is not NULL, then it is set to the number of tokens in all
203948** matchable phrases of the expression.
203949*/
203950static int fts3ExprLoadDoclists(
203951 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
203952 int *pnPhrase, /* OUT: Number of phrases in query */
203953 int *pnToken /* OUT: Number of tokens in query */
203954){
203955 int rc; /* Return Code */
203956 LoadDoclistCtx sCtx = {0,0,0}; /* Context for sqlite3Fts3ExprIterate() */
203957 sCtx.pCsr = pCsr;
203958 rc = sqlite3Fts3ExprIterate(pCsr->pExpr,fts3ExprLoadDoclistsCb,(void*)&sCtx);
203959 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
203960 if( pnToken ) *pnToken = sCtx.nToken;
203961 return rc;
203962}
203963
203964static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
203965 (*(int *)ctx)++;
203966 pExpr->iPhrase = iPhrase;
203967 return SQLITE_OK0;
203968}
203969static int fts3ExprPhraseCount(Fts3Expr *pExpr){
203970 int nPhrase = 0;
203971 (void)sqlite3Fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
203972 return nPhrase;
203973}
203974
203975/*
203976** Advance the position list iterator specified by the first two
203977** arguments so that it points to the first element with a value greater
203978** than or equal to parameter iNext.
203979*/
203980static void fts3SnippetAdvance(char **ppIter, i64 *piIter, int iNext){
203981 char *pIter = *ppIter;
203982 if( pIter ){
203983 i64 iIter = *piIter;
203984
203985 while( iIter<iNext ){
203986 if( 0==(*pIter & 0xFE) ){
203987 iIter = -1;
203988 pIter = 0;
203989 break;
203990 }
203991 fts3GetDeltaPosition(&pIter, &iIter);
203992 }
203993
203994 *piIter = iIter;
203995 *ppIter = pIter;
203996 }
203997}
203998
203999/*
204000** Advance the snippet iterator to the next candidate snippet.
204001*/
204002static int fts3SnippetNextCandidate(SnippetIter *pIter){
204003 int i; /* Loop counter */
204004
204005 if( pIter->iCurrent<0 ){
204006 /* The SnippetIter object has just been initialized. The first snippet
204007 ** candidate always starts at offset 0 (even if this candidate has a
204008 ** score of 0.0).
204009 */
204010 pIter->iCurrent = 0;
204011
204012 /* Advance the 'head' iterator of each phrase to the first offset that
204013 ** is greater than or equal to (iNext+nSnippet).
204014 */
204015 for(i=0; i<pIter->nPhrase; i++){
204016 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
204017 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
204018 }
204019 }else{
204020 int iStart;
204021 int iEnd = 0x7FFFFFFF;
204022
204023 for(i=0; i<pIter->nPhrase; i++){
204024 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
204025 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
204026 iEnd = pPhrase->iHead;
204027 }
204028 }
204029 if( iEnd==0x7FFFFFFF ){
204030 return 1;
204031 }
204032
204033 assert( pIter->nSnippet>=0 )((void) (0));
204034 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
204035 for(i=0; i<pIter->nPhrase; i++){
204036 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
204037 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
204038 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
204039 }
204040 }
204041
204042 return 0;
204043}
204044
204045/*
204046** Retrieve information about the current candidate snippet of snippet
204047** iterator pIter.
204048*/
204049static void fts3SnippetDetails(
204050 SnippetIter *pIter, /* Snippet iterator */
204051 u64 mCovered, /* Bitmask of phrases already covered */
204052 int *piToken, /* OUT: First token of proposed snippet */
204053 int *piScore, /* OUT: "Score" for this snippet */
204054 u64 *pmCover, /* OUT: Bitmask of phrases covered */
204055 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
204056){
204057 int iStart = pIter->iCurrent; /* First token of snippet */
204058 int iScore = 0; /* Score of this snippet */
204059 int i; /* Loop counter */
204060 u64 mCover = 0; /* Mask of phrases covered by this snippet */
204061 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
204062
204063 for(i=0; i<pIter->nPhrase; i++){
204064 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
204065 if( pPhrase->pTail ){
204066 char *pCsr = pPhrase->pTail;
204067 i64 iCsr = pPhrase->iTail;
204068
204069 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
204070 int j;
204071 u64 mPhrase = (u64)1 << (i%64);
204072 u64 mPos = (u64)1 << (iCsr - iStart);
204073 assert( iCsr>=iStart && (iCsr - iStart)<=64 )((void) (0));
204074 assert( i>=0 )((void) (0));
204075 if( (mCover|mCovered)&mPhrase ){
204076 iScore++;
204077 }else{
204078 iScore += 1000;
204079 }
204080 mCover |= mPhrase;
204081
204082 for(j=0; j<pPhrase->nToken && j<pIter->nSnippet; j++){
204083 mHighlight |= (mPos>>j);
204084 }
204085
204086 if( 0==(*pCsr & 0x0FE) ) break;
204087 fts3GetDeltaPosition(&pCsr, &iCsr);
204088 }
204089 }
204090 }
204091
204092 /* Set the output variables before returning. */
204093 *piToken = iStart;
204094 *piScore = iScore;
204095 *pmCover = mCover;
204096 *pmHighlight = mHighlight;
204097}
204098
204099/*
204100** This function is an sqlite3Fts3ExprIterate() callback used by
204101** fts3BestSnippet(). Each invocation populates an element of the
204102** SnippetIter.aPhrase[] array.
204103*/
204104static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
204105 SnippetIter *p = (SnippetIter *)ctx;
204106 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
204107 char *pCsr;
204108 int rc;
204109
204110 pPhrase->nToken = pExpr->pPhrase->nToken;
204111 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
204112 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
204113 if( pCsr ){
204114 i64 iFirst = 0;
204115 pPhrase->pList = pCsr;
204116 fts3GetDeltaPosition(&pCsr, &iFirst);
204117 if( iFirst<0 ){
204118 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
204119 }else{
204120 pPhrase->pHead = pCsr;
204121 pPhrase->pTail = pCsr;
204122 pPhrase->iHead = iFirst;
204123 pPhrase->iTail = iFirst;
204124 }
204125 }else{
204126 assert( rc!=SQLITE_OK || (((void) (0))
204127 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0((void) (0))
204128 ))((void) (0));
204129 }
204130
204131 return rc;
204132}
204133
204134/*
204135** Select the fragment of text consisting of nFragment contiguous tokens
204136** from column iCol that represent the "best" snippet. The best snippet
204137** is the snippet with the highest score, where scores are calculated
204138** by adding:
204139**
204140** (a) +1 point for each occurrence of a matchable phrase in the snippet.
204141**
204142** (b) +1000 points for the first occurrence of each matchable phrase in
204143** the snippet for which the corresponding mCovered bit is not set.
204144**
204145** The selected snippet parameters are stored in structure *pFragment before
204146** returning. The score of the selected snippet is stored in *piScore
204147** before returning.
204148*/
204149static int fts3BestSnippet(
204150 int nSnippet, /* Desired snippet length */
204151 Fts3Cursor *pCsr, /* Cursor to create snippet for */
204152 int iCol, /* Index of column to create snippet from */
204153 u64 mCovered, /* Mask of phrases already covered */
204154 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
204155 SnippetFragment *pFragment, /* OUT: Best snippet found */
204156 int *piScore /* OUT: Score of snippet pFragment */
204157){
204158 int rc; /* Return Code */
204159 int nList; /* Number of phrases in expression */
204160 SnippetIter sIter; /* Iterates through snippet candidates */
204161 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
204162 int iBestScore = -1; /* Best snippet score found so far */
204163 int i; /* Loop counter */
204164
204165 memset(&sIter, 0, sizeof(sIter));
204166
204167 /* Iterate through the phrases in the expression to count them. The same
204168 ** callback makes sure the doclists are loaded for each phrase.
204169 */
204170 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
204171 if( rc!=SQLITE_OK0 ){
204172 return rc;
204173 }
204174
204175 /* Now that it is known how many phrases there are, allocate and zero
204176 ** the required space using malloc().
204177 */
204178 nByte = sizeof(SnippetPhrase) * nList;
204179 sIter.aPhrase = (SnippetPhrase *)sqlite3Fts3MallocZero(nByte);
204180 if( !sIter.aPhrase ){
204181 return SQLITE_NOMEM7;
204182 }
204183
204184 /* Initialize the contents of the SnippetIter object. Then iterate through
204185 ** the set of phrases in the expression to populate the aPhrase[] array.
204186 */
204187 sIter.pCsr = pCsr;
204188 sIter.iCol = iCol;
204189 sIter.nSnippet = nSnippet;
204190 sIter.nPhrase = nList;
204191 sIter.iCurrent = -1;
204192 rc = sqlite3Fts3ExprIterate(
204193 pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter
204194 );
204195 if( rc==SQLITE_OK0 ){
204196
204197 /* Set the *pmSeen output variable. */
204198 for(i=0; i<nList; i++){
204199 if( sIter.aPhrase[i].pHead ){
204200 *pmSeen |= (u64)1 << (i%64);
204201 }
204202 }
204203
204204 /* Loop through all candidate snippets. Store the best snippet in
204205 ** *pFragment. Store its associated 'score' in iBestScore.
204206 */
204207 pFragment->iCol = iCol;
204208 while( !fts3SnippetNextCandidate(&sIter) ){
204209 int iPos;
204210 int iScore;
204211 u64 mCover;
204212 u64 mHighlite;
204213 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
204214 assert( iScore>=0 )((void) (0));
204215 if( iScore>iBestScore ){
204216 pFragment->iPos = iPos;
204217 pFragment->hlmask = mHighlite;
204218 pFragment->covered = mCover;
204219 iBestScore = iScore;
204220 }
204221 }
204222
204223 *piScore = iBestScore;
204224 }
204225 sqlite3_free(sIter.aPhrase);
204226 return rc;
204227}
204228
204229
204230/*
204231** Append a string to the string-buffer passed as the first argument.
204232**
204233** If nAppend is negative, then the length of the string zAppend is
204234** determined using strlen().
204235*/
204236static int fts3StringAppend(
204237 StrBuffer *pStr, /* Buffer to append to */
204238 const char *zAppend, /* Pointer to data to append to buffer */
204239 int nAppend /* Size of zAppend in bytes (or -1) */
204240){
204241 if( nAppend<0 ){
204242 nAppend = (int)strlen(zAppend);
204243 }
204244
204245 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
204246 ** to grow the buffer until so that it is big enough to accomadate the
204247 ** appended data.
204248 */
204249 if( pStr->n+nAppend+1>=pStr->nAlloc ){
204250 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
204251 char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
204252 if( !zNew ){
204253 return SQLITE_NOMEM7;
204254 }
204255 pStr->z = zNew;
204256 pStr->nAlloc = nAlloc;
204257 }
204258 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) )((void) (0));
204259
204260 /* Append the data to the string buffer. */
204261 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
204262 pStr->n += nAppend;
204263 pStr->z[pStr->n] = '\0';
204264
204265 return SQLITE_OK0;
204266}
204267
204268/*
204269** The fts3BestSnippet() function often selects snippets that end with a
204270** query term. That is, the final term of the snippet is always a term
204271** that requires highlighting. For example, if 'X' is a highlighted term
204272** and '.' is a non-highlighted term, BestSnippet() may select:
204273**
204274** ........X.....X
204275**
204276** This function "shifts" the beginning of the snippet forward in the
204277** document so that there are approximately the same number of
204278** non-highlighted terms to the right of the final highlighted term as there
204279** are to the left of the first highlighted term. For example, to this:
204280**
204281** ....X.....X....
204282**
204283** This is done as part of extracting the snippet text, not when selecting
204284** the snippet. Snippet selection is done based on doclists only, so there
204285** is no way for fts3BestSnippet() to know whether or not the document
204286** actually contains terms that follow the final highlighted term.
204287*/
204288static int fts3SnippetShift(
204289 Fts3Table *pTab, /* FTS3 table snippet comes from */
204290 int iLangid, /* Language id to use in tokenizing */
204291 int nSnippet, /* Number of tokens desired for snippet */
204292 const char *zDoc, /* Document text to extract snippet from */
204293 int nDoc, /* Size of buffer zDoc in bytes */
204294 int *piPos, /* IN/OUT: First token of snippet */
204295 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
204296){
204297 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
204298
204299 if( hlmask ){
204300 int nLeft; /* Tokens to the left of first highlight */
204301 int nRight; /* Tokens to the right of last highlight */
204302 int nDesired; /* Ideal number of tokens to shift forward */
204303
204304 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
204305 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
204306 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 )((void) (0));
204307 nDesired = (nLeft-nRight)/2;
204308
204309 /* Ideally, the start of the snippet should be pushed forward in the
204310 ** document nDesired tokens. This block checks if there are actually
204311 ** nDesired tokens to the right of the snippet. If so, *piPos and
204312 ** *pHlMask are updated to shift the snippet nDesired tokens to the
204313 ** right. Otherwise, the snippet is shifted by the number of tokens
204314 ** available.
204315 */
204316 if( nDesired>0 ){
204317 int nShift; /* Number of tokens to shift snippet by */
204318 int iCurrent = 0; /* Token counter */
204319 int rc; /* Return Code */
204320 sqlite3_tokenizer_module *pMod;
204321 sqlite3_tokenizer_cursor *pC;
204322 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
204323
204324 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
204325 ** or more tokens in zDoc/nDoc.
204326 */
204327 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
204328 if( rc!=SQLITE_OK0 ){
204329 return rc;
204330 }
204331 while( rc==SQLITE_OK0 && iCurrent<(nSnippet+nDesired) ){
204332 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
204333 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
204334 }
204335 pMod->xClose(pC);
204336 if( rc!=SQLITE_OK0 && rc!=SQLITE_DONE101 ){ return rc; }
204337
204338 nShift = (rc==SQLITE_DONE101)+iCurrent-nSnippet;
204339 assert( nShift<=nDesired )((void) (0));
204340 if( nShift>0 ){
204341 *piPos += nShift;
204342 *pHlmask = hlmask >> nShift;
204343 }
204344 }
204345 }
204346 return SQLITE_OK0;
204347}
204348
204349/*
204350** Extract the snippet text for fragment pFragment from cursor pCsr and
204351** append it to string buffer pOut.
204352*/
204353static int fts3SnippetText(
204354 Fts3Cursor *pCsr, /* FTS3 Cursor */
204355 SnippetFragment *pFragment, /* Snippet to extract */
204356 int iFragment, /* Fragment number */
204357 int isLast, /* True for final fragment in snippet */
204358 int nSnippet, /* Number of tokens in extracted snippet */
204359 const char *zOpen, /* String inserted before highlighted term */
204360 const char *zClose, /* String inserted after highlighted term */
204361 const char *zEllipsis, /* String inserted between snippets */
204362 StrBuffer *pOut /* Write output here */
204363){
204364 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
204365 int rc; /* Return code */
204366 const char *zDoc; /* Document text to extract snippet from */
204367 int nDoc; /* Size of zDoc in bytes */
204368 int iCurrent = 0; /* Current token number of document */
204369 int iEnd = 0; /* Byte offset of end of current token */
204370 int isShiftDone = 0; /* True after snippet is shifted */
204371 int iPos = pFragment->iPos; /* First token of snippet */
204372 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
204373 int iCol = pFragment->iCol+1; /* Query column to extract text from */
204374 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
204375 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
204376
204377 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
204378 if( zDoc==0 ){
204379 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL5 ){
204380 return SQLITE_NOMEM7;
204381 }
204382 return SQLITE_OK0;
204383 }
204384 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
204385
204386 /* Open a token cursor on the document. */
204387 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
204388 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
204389 if( rc!=SQLITE_OK0 ){
204390 return rc;
204391 }
204392
204393 while( rc==SQLITE_OK0 ){
204394 const char *ZDUMMY; /* Dummy argument used with tokenizer */
204395 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
204396 int iBegin = 0; /* Offset in zDoc of start of token */
204397 int iFin = 0; /* Offset in zDoc of end of token */
204398 int isHighlight = 0; /* True for highlighted terms */
204399
204400 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
204401 ** in the FTS code the variable that the third argument to xNext points to
204402 ** is initialized to zero before the first (*but not necessarily
204403 ** subsequent*) call to xNext(). This is done for a particular application
204404 ** that needs to know whether or not the tokenizer is being used for
204405 ** snippet generation or for some other purpose.
204406 **
204407 ** Extreme care is required when writing code to depend on this
204408 ** initialization. It is not a documented part of the tokenizer interface.
204409 ** If a tokenizer is used directly by any code outside of FTS, this
204410 ** convention might not be respected. */
204411 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
204412 if( rc!=SQLITE_OK0 ){
204413 if( rc==SQLITE_DONE101 ){
204414 /* Special case - the last token of the snippet is also the last token
204415 ** of the column. Append any punctuation that occurred between the end
204416 ** of the previous token and the end of the document to the output.
204417 ** Then break out of the loop. */
204418 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
204419 }
204420 break;
204421 }
204422 if( iCurrent<iPos ){ continue; }
204423
204424 if( !isShiftDone ){
204425 int n = nDoc - iBegin;
204426 rc = fts3SnippetShift(
204427 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
204428 );
204429 isShiftDone = 1;
204430
204431 /* Now that the shift has been done, check if the initial "..." are
204432 ** required. They are required if (a) this is not the first fragment,
204433 ** or (b) this fragment does not begin at position 0 of its column.
204434 */
204435 if( rc==SQLITE_OK0 ){
204436 if( iPos>0 || iFragment>0 ){
204437 rc = fts3StringAppend(pOut, zEllipsis, -1);
204438 }else if( iBegin ){
204439 rc = fts3StringAppend(pOut, zDoc, iBegin);
204440 }
204441 }
204442 if( rc!=SQLITE_OK0 || iCurrent<iPos ) continue;
204443 }
204444
204445 if( iCurrent>=(iPos+nSnippet) ){
204446 if( isLast ){
204447 rc = fts3StringAppend(pOut, zEllipsis, -1);
204448 }
204449 break;
204450 }
204451
204452 /* Set isHighlight to true if this term should be highlighted. */
204453 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
204454
204455 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
204456 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
204457 if( rc==SQLITE_OK0 ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
204458 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
204459
204460 iEnd = iFin;
204461 }
204462
204463 pMod->xClose(pC);
204464 return rc;
204465}
204466
204467
204468/*
204469** This function is used to count the entries in a column-list (a
204470** delta-encoded list of term offsets within a single column of a single
204471** row). When this function is called, *ppCollist should point to the
204472** beginning of the first varint in the column-list (the varint that
204473** contains the position of the first matching term in the column data).
204474** Before returning, *ppCollist is set to point to the first byte after
204475** the last varint in the column-list (either the 0x00 signifying the end
204476** of the position-list, or the 0x01 that precedes the column number of
204477** the next column in the position-list).
204478**
204479** The number of elements in the column-list is returned.
204480*/
204481static int fts3ColumnlistCount(char **ppCollist){
204482 char *pEnd = *ppCollist;
204483 char c = 0;
204484 int nEntry = 0;
204485
204486 /* A column-list is terminated by either a 0x01 or 0x00. */
204487 while( 0xFE & (*pEnd | c) ){
204488 c = *pEnd++ & 0x80;
204489 if( !c ) nEntry++;
204490 }
204491
204492 *ppCollist = pEnd;
204493 return nEntry;
204494}
204495
204496/*
204497** This function gathers 'y' or 'b' data for a single phrase.
204498*/
204499static int fts3ExprLHits(
204500 Fts3Expr *pExpr, /* Phrase expression node */
204501 MatchInfo *p /* Matchinfo context */
204502){
204503 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
204504 int iStart;
204505 Fts3Phrase *pPhrase = pExpr->pPhrase;
204506 char *pIter = pPhrase->doclist.pList;
204507 int iCol = 0;
204508
204509 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS )((void) (0));
204510 if( p->flag==FTS3_MATCHINFO_LHITS'y' ){
204511 iStart = pExpr->iPhrase * p->nCol;
204512 }else{
204513 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
204514 }
204515
204516 if( pIter ) while( 1 ){
204517 int nHit = fts3ColumnlistCount(&pIter);
204518 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
204519 if( p->flag==FTS3_MATCHINFO_LHITS'y' ){
204520 p->aMatchinfo[iStart + iCol] = (u32)nHit;
204521 }else if( nHit ){
204522 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
204523 }
204524 }
204525 assert( *pIter==0x00 || *pIter==0x01 )((void) (0));
204526 if( *pIter!=0x01 ) break;
204527 pIter++;
204528 pIter += fts3GetVarint32(pIter, &iCol)( (*(u8*)(pIter)&0x80) ? sqlite3Fts3GetVarint32(pIter, &
iCol) : (*&iCol=*(u8*)(pIter), 1) )
;
204529 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB(11 | (1<<8));
204530 }
204531 return SQLITE_OK0;
204532}
204533
204534/*
204535** Gather the results for matchinfo directives 'y' and 'b'.
204536*/
204537static int fts3ExprLHitGather(
204538 Fts3Expr *pExpr,
204539 MatchInfo *p
204540){
204541 int rc = SQLITE_OK0;
204542 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) )((void) (0));
204543 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
204544 if( pExpr->pLeft ){
204545 rc = fts3ExprLHitGather(pExpr->pLeft, p);
204546 if( rc==SQLITE_OK0 ) rc = fts3ExprLHitGather(pExpr->pRight, p);
204547 }else{
204548 rc = fts3ExprLHits(pExpr, p);
204549 }
204550 }
204551 return rc;
204552}
204553
204554/*
204555** sqlite3Fts3ExprIterate() callback used to collect the "global" matchinfo
204556** stats for a single query.
204557**
204558** sqlite3Fts3ExprIterate() callback to load the 'global' elements of a
204559** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
204560** of the matchinfo array that are constant for all rows returned by the
204561** current query.
204562**
204563** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
204564** function populates Matchinfo.aMatchinfo[] as follows:
204565**
204566** for(iCol=0; iCol<nCol; iCol++){
204567** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
204568** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
204569** }
204570**
204571** where X is the number of matches for phrase iPhrase is column iCol of all
204572** rows of the table. Y is the number of rows for which column iCol contains
204573** at least one instance of phrase iPhrase.
204574**
204575** If the phrase pExpr consists entirely of deferred tokens, then all X and
204576** Y values are set to nDoc, where nDoc is the number of documents in the
204577** file system. This is done because the full-text index doclist is required
204578** to calculate these values properly, and the full-text index doclist is
204579** not available for deferred tokens.
204580*/
204581static int fts3ExprGlobalHitsCb(
204582 Fts3Expr *pExpr, /* Phrase expression node */
204583 int iPhrase, /* Phrase number (numbered from zero) */
204584 void *pCtx /* Pointer to MatchInfo structure */
204585){
204586 MatchInfo *p = (MatchInfo *)pCtx;
204587 return sqlite3Fts3EvalPhraseStats(
204588 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
204589 );
204590}
204591
204592/*
204593** sqlite3Fts3ExprIterate() callback used to collect the "local" part of the
204594** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
204595** array that are different for each row returned by the query.
204596*/
204597static int fts3ExprLocalHitsCb(
204598 Fts3Expr *pExpr, /* Phrase expression node */
204599 int iPhrase, /* Phrase number */
204600 void *pCtx /* Pointer to MatchInfo structure */
204601){
204602 int rc = SQLITE_OK0;
204603 MatchInfo *p = (MatchInfo *)pCtx;
204604 int iStart = iPhrase * p->nCol * 3;
204605 int i;
204606
204607 for(i=0; i<p->nCol && rc==SQLITE_OK0; i++){
204608 char *pCsr;
204609 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
204610 if( pCsr ){
204611 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
204612 }else{
204613 p->aMatchinfo[iStart+i*3] = 0;
204614 }
204615 }
204616
204617 return rc;
204618}
204619
204620static int fts3MatchinfoCheck(
204621 Fts3Table *pTab,
204622 char cArg,
204623 char **pzErr
204624){
204625 if( (cArg==FTS3_MATCHINFO_NPHRASE'p')
204626 || (cArg==FTS3_MATCHINFO_NCOL'c')
204627 || (cArg==FTS3_MATCHINFO_NDOC'n' && pTab->bFts4)
204628 || (cArg==FTS3_MATCHINFO_AVGLENGTH'a' && pTab->bFts4)
204629 || (cArg==FTS3_MATCHINFO_LENGTH'l' && pTab->bHasDocsize)
204630 || (cArg==FTS3_MATCHINFO_LCS's')
204631 || (cArg==FTS3_MATCHINFO_HITS'x')
204632 || (cArg==FTS3_MATCHINFO_LHITS'y')
204633 || (cArg==FTS3_MATCHINFO_LHITS_BM'b')
204634 ){
204635 return SQLITE_OK0;
204636 }
204637 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
204638 return SQLITE_ERROR1;
204639}
204640
204641static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
204642 size_t nVal; /* Number of integers output by cArg */
204643
204644 switch( cArg ){
204645 case FTS3_MATCHINFO_NDOC'n':
204646 case FTS3_MATCHINFO_NPHRASE'p':
204647 case FTS3_MATCHINFO_NCOL'c':
204648 nVal = 1;
204649 break;
204650
204651 case FTS3_MATCHINFO_AVGLENGTH'a':
204652 case FTS3_MATCHINFO_LENGTH'l':
204653 case FTS3_MATCHINFO_LCS's':
204654 nVal = pInfo->nCol;
204655 break;
204656
204657 case FTS3_MATCHINFO_LHITS'y':
204658 nVal = pInfo->nCol * pInfo->nPhrase;
204659 break;
204660
204661 case FTS3_MATCHINFO_LHITS_BM'b':
204662 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
204663 break;
204664
204665 default:
204666 assert( cArg==FTS3_MATCHINFO_HITS )((void) (0));
204667 nVal = pInfo->nCol * pInfo->nPhrase * 3;
204668 break;
204669 }
204670
204671 return nVal;
204672}
204673
204674static int fts3MatchinfoSelectDoctotal(
204675 Fts3Table *pTab,
204676 sqlite3_stmt **ppStmt,
204677 sqlite3_int64 *pnDoc,
204678 const char **paLen,
204679 const char **ppEnd
204680){
204681 sqlite3_stmt *pStmt;
204682 const char *a;
204683 const char *pEnd;
204684 sqlite3_int64 nDoc;
204685 int n;
204686
204687
204688 if( !*ppStmt ){
204689 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
204690 if( rc!=SQLITE_OK0 ) return rc;
204691 }
204692 pStmt = *ppStmt;
204693 assert( sqlite3_data_count(pStmt)==1 )((void) (0));
204694
204695 n = sqlite3_column_bytes(pStmt, 0);
204696 a = sqlite3_column_blob(pStmt, 0);
204697 if( a==0 ){
204698 return FTS_CORRUPT_VTAB(11 | (1<<8));
204699 }
204700 pEnd = a + n;
204701 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
204702 if( nDoc<=0 || a>pEnd ){
204703 return FTS_CORRUPT_VTAB(11 | (1<<8));
204704 }
204705 *pnDoc = nDoc;
204706
204707 if( paLen ) *paLen = a;
204708 if( ppEnd ) *ppEnd = pEnd;
204709 return SQLITE_OK0;
204710}
204711
204712/*
204713** An instance of the following structure is used to store state while
204714** iterating through a multi-column position-list corresponding to the
204715** hits for a single phrase on a single row in order to calculate the
204716** values for a matchinfo() FTS3_MATCHINFO_LCS request.
204717*/
204718typedef struct LcsIterator LcsIterator;
204719struct LcsIterator {
204720 Fts3Expr *pExpr; /* Pointer to phrase expression */
204721 int iPosOffset; /* Tokens count up to end of this phrase */
204722 char *pRead; /* Cursor used to iterate through aDoclist */
204723 int iPos; /* Current position */
204724};
204725
204726/*
204727** If LcsIterator.iCol is set to the following value, the iterator has
204728** finished iterating through all offsets for all columns.
204729*/
204730#define LCS_ITERATOR_FINISHED0x7FFFFFFF; 0x7FFFFFFF;
204731
204732static int fts3MatchinfoLcsCb(
204733 Fts3Expr *pExpr, /* Phrase expression node */
204734 int iPhrase, /* Phrase number (numbered from zero) */
204735 void *pCtx /* Pointer to MatchInfo structure */
204736){
204737 LcsIterator *aIter = (LcsIterator *)pCtx;
204738 aIter[iPhrase].pExpr = pExpr;
204739 return SQLITE_OK0;
204740}
204741
204742/*
204743** Advance the iterator passed as an argument to the next position. Return
204744** 1 if the iterator is at EOF or if it now points to the start of the
204745** position list for the next column.
204746*/
204747static int fts3LcsIteratorAdvance(LcsIterator *pIter){
204748 char *pRead;
204749 sqlite3_int64 iRead;
204750 int rc = 0;
204751
204752 if( NEVER(pIter==0)(pIter==0) ) return 1;
204753 pRead = pIter->pRead;
204754 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
204755 if( iRead==0 || iRead==1 ){
204756 pRead = 0;
204757 rc = 1;
204758 }else{
204759 pIter->iPos += (int)(iRead-2);
204760 }
204761
204762 pIter->pRead = pRead;
204763 return rc;
204764}
204765
204766/*
204767** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
204768**
204769** If the call is successful, the longest-common-substring lengths for each
204770** column are written into the first nCol elements of the pInfo->aMatchinfo[]
204771** array before returning. SQLITE_OK is returned in this case.
204772**
204773** Otherwise, if an error occurs, an SQLite error code is returned and the
204774** data written to the first nCol elements of pInfo->aMatchinfo[] is
204775** undefined.
204776*/
204777static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
204778 LcsIterator *aIter;
204779 int i;
204780 int iCol;
204781 int nToken = 0;
204782 int rc = SQLITE_OK0;
204783
204784 /* Allocate and populate the array of LcsIterator objects. The array
204785 ** contains one element for each matchable phrase in the query.
204786 **/
204787 aIter = sqlite3Fts3MallocZero(sizeof(LcsIterator) * pCsr->nPhrase);
204788 if( !aIter ) return SQLITE_NOMEM7;
204789 (void)sqlite3Fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
204790
204791 for(i=0; i<pInfo->nPhrase; i++){
204792 LcsIterator *pIter = &aIter[i];
204793 nToken -= pIter->pExpr->pPhrase->nToken;
204794 pIter->iPosOffset = nToken;
204795 }
204796
204797 for(iCol=0; iCol<pInfo->nCol; iCol++){
204798 int nLcs = 0; /* LCS value for this column */
204799 int nLive = 0; /* Number of iterators in aIter not at EOF */
204800
204801 for(i=0; i<pInfo->nPhrase; i++){
204802 LcsIterator *pIt = &aIter[i];
204803 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
204804 if( rc!=SQLITE_OK0 ) goto matchinfo_lcs_out;
204805 if( pIt->pRead ){
204806 pIt->iPos = pIt->iPosOffset;
204807 fts3LcsIteratorAdvance(pIt);
204808 if( pIt->pRead==0 ){
204809 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
204810 goto matchinfo_lcs_out;
204811 }
204812 nLive++;
204813 }
204814 }
204815
204816 while( nLive>0 ){
204817 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
204818 int nThisLcs = 0; /* LCS for the current iterator positions */
204819
204820 for(i=0; i<pInfo->nPhrase; i++){
204821 LcsIterator *pIter = &aIter[i];
204822 if( pIter->pRead==0 ){
204823 /* This iterator is already at EOF for this column. */
204824 nThisLcs = 0;
204825 }else{
204826 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
204827 pAdv = pIter;
204828 }
204829 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
204830 nThisLcs++;
204831 }else{
204832 nThisLcs = 1;
204833 }
204834 if( nThisLcs>nLcs ) nLcs = nThisLcs;
204835 }
204836 }
204837 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
204838 }
204839
204840 pInfo->aMatchinfo[iCol] = nLcs;
204841 }
204842
204843 matchinfo_lcs_out:
204844 sqlite3_free(aIter);
204845 return rc;
204846}
204847
204848/*
204849** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
204850** be returned by the matchinfo() function. Argument zArg contains the
204851** format string passed as the second argument to matchinfo (or the
204852** default value "pcx" if no second argument was specified). The format
204853** string has already been validated and the pInfo->aMatchinfo[] array
204854** is guaranteed to be large enough for the output.
204855**
204856** If bGlobal is true, then populate all fields of the matchinfo() output.
204857** If it is false, then assume that those fields that do not change between
204858** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
204859** have already been populated.
204860**
204861** Return SQLITE_OK if successful, or an SQLite error code if an error
204862** occurs. If a value other than SQLITE_OK is returned, the state the
204863** pInfo->aMatchinfo[] buffer is left in is undefined.
204864*/
204865static int fts3MatchinfoValues(
204866 Fts3Cursor *pCsr, /* FTS3 cursor object */
204867 int bGlobal, /* True to grab the global stats */
204868 MatchInfo *pInfo, /* Matchinfo context object */
204869 const char *zArg /* Matchinfo format string */
204870){
204871 int rc = SQLITE_OK0;
204872 int i;
204873 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
204874 sqlite3_stmt *pSelect = 0;
204875
204876 for(i=0; rc==SQLITE_OK0 && zArg[i]; i++){
204877 pInfo->flag = zArg[i];
204878 switch( zArg[i] ){
204879 case FTS3_MATCHINFO_NPHRASE'p':
204880 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
204881 break;
204882
204883 case FTS3_MATCHINFO_NCOL'c':
204884 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
204885 break;
204886
204887 case FTS3_MATCHINFO_NDOC'n':
204888 if( bGlobal ){
204889 sqlite3_int64 nDoc = 0;
204890 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0, 0);
204891 pInfo->aMatchinfo[0] = (u32)nDoc;
204892 }
204893 break;
204894
204895 case FTS3_MATCHINFO_AVGLENGTH'a':
204896 if( bGlobal ){
204897 sqlite3_int64 nDoc; /* Number of rows in table */
204898 const char *a; /* Aggregate column length array */
204899 const char *pEnd; /* First byte past end of length array */
204900
204901 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a, &pEnd);
204902 if( rc==SQLITE_OK0 ){
204903 int iCol;
204904 for(iCol=0; iCol<pInfo->nCol; iCol++){
204905 u32 iVal;
204906 sqlite3_int64 nToken;
204907 a += sqlite3Fts3GetVarint(a, &nToken);
204908 if( a>pEnd ){
204909 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
204910 break;
204911 }
204912 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
204913 pInfo->aMatchinfo[iCol] = iVal;
204914 }
204915 }
204916 }
204917 break;
204918
204919 case FTS3_MATCHINFO_LENGTH'l': {
204920 sqlite3_stmt *pSelectDocsize = 0;
204921 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
204922 if( rc==SQLITE_OK0 ){
204923 int iCol;
204924 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
204925 const char *pEnd = a + sqlite3_column_bytes(pSelectDocsize, 0);
204926 for(iCol=0; iCol<pInfo->nCol; iCol++){
204927 sqlite3_int64 nToken;
204928 a += sqlite3Fts3GetVarintBounded(a, pEnd, &nToken);
204929 if( a>pEnd ){
204930 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
204931 break;
204932 }
204933 pInfo->aMatchinfo[iCol] = (u32)nToken;
204934 }
204935 }
204936 sqlite3_reset(pSelectDocsize);
204937 break;
204938 }
204939
204940 case FTS3_MATCHINFO_LCS's':
204941 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
204942 if( rc==SQLITE_OK0 ){
204943 rc = fts3MatchinfoLcs(pCsr, pInfo);
204944 }
204945 break;
204946
204947 case FTS3_MATCHINFO_LHITS_BM'b':
204948 case FTS3_MATCHINFO_LHITS'y': {
204949 size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
204950 memset(pInfo->aMatchinfo, 0, nZero);
204951 rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
204952 break;
204953 }
204954
204955 default: {
204956 Fts3Expr *pExpr;
204957 assert( zArg[i]==FTS3_MATCHINFO_HITS )((void) (0));
204958 pExpr = pCsr->pExpr;
204959 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
204960 if( rc!=SQLITE_OK0 ) break;
204961 if( bGlobal ){
204962 if( pCsr->pDeferred ){
204963 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc,0,0);
204964 if( rc!=SQLITE_OK0 ) break;
204965 }
204966 rc = sqlite3Fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
204967 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
204968 if( rc!=SQLITE_OK0 ) break;
204969 }
204970 (void)sqlite3Fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
204971 break;
204972 }
204973 }
204974
204975 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
204976 }
204977
204978 sqlite3_reset(pSelect);
204979 return rc;
204980}
204981
204982
204983/*
204984** Populate pCsr->aMatchinfo[] with data for the current row. The
204985** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
204986*/
204987static void fts3GetMatchinfo(
204988 sqlite3_context *pCtx, /* Return results here */
204989 Fts3Cursor *pCsr, /* FTS3 Cursor object */
204990 const char *zArg /* Second argument to matchinfo() function */
204991){
204992 MatchInfo sInfo;
204993 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
204994 int rc = SQLITE_OK0;
204995 int bGlobal = 0; /* Collect 'global' stats as well as local */
204996
204997 u32 *aOut = 0;
204998 void (*xDestroyOut)(void*) = 0;
204999
205000 memset(&sInfo, 0, sizeof(MatchInfo));
205001 sInfo.pCursor = pCsr;
205002 sInfo.nCol = pTab->nColumn;
205003
205004 /* If there is cached matchinfo() data, but the format string for the
205005 ** cache does not match the format string for this request, discard
205006 ** the cached data. */
205007 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
205008 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
205009 pCsr->pMIBuffer = 0;
205010 }
205011
205012 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
205013 ** matchinfo function has been called for this query. In this case
205014 ** allocate the array used to accumulate the matchinfo data and
205015 ** initialize those elements that are constant for every row.
205016 */
205017 if( pCsr->pMIBuffer==0 ){
205018 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
205019 int i; /* Used to iterate through zArg */
205020
205021 /* Determine the number of phrases in the query */
205022 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
205023 sInfo.nPhrase = pCsr->nPhrase;
205024
205025 /* Determine the number of integers in the buffer returned by this call. */
205026 for(i=0; zArg[i]; i++){
205027 char *zErr = 0;
205028 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
205029 sqlite3_result_error(pCtx, zErr, -1);
205030 sqlite3_free(zErr);
205031 return;
205032 }
205033 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
205034 }
205035
205036 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
205037 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
205038 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM7;
205039
205040 pCsr->isMatchinfoNeeded = 1;
205041 bGlobal = 1;
205042 }
205043
205044 if( rc==SQLITE_OK0 ){
205045 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
205046 if( xDestroyOut==0 ){
205047 rc = SQLITE_NOMEM7;
205048 }
205049 }
205050
205051 if( rc==SQLITE_OK0 ){
205052 sInfo.aMatchinfo = aOut;
205053 sInfo.nPhrase = pCsr->nPhrase;
205054 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
205055 if( bGlobal ){
205056 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
205057 }
205058 }
205059
205060 if( rc!=SQLITE_OK0 ){
205061 sqlite3_result_error_code(pCtx, rc);
205062 if( xDestroyOut ) xDestroyOut(aOut);
205063 }else{
205064 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
205065 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
205066 }
205067}
205068
205069/*
205070** Implementation of snippet() function.
205071*/
205072SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(
205073 sqlite3_context *pCtx, /* SQLite function call context */
205074 Fts3Cursor *pCsr, /* Cursor object */
205075 const char *zStart, /* Snippet start text - "<b>" */
205076 const char *zEnd, /* Snippet end text - "</b>" */
205077 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
205078 int iCol, /* Extract snippet from this column */
205079 int nToken /* Approximate number of tokens in snippet */
205080){
205081 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
205082 int rc = SQLITE_OK0;
205083 int i;
205084 StrBuffer res = {0, 0, 0};
205085
205086 /* The returned text includes up to four fragments of text extracted from
205087 ** the data in the current row. The first iteration of the for(...) loop
205088 ** below attempts to locate a single fragment of text nToken tokens in
205089 ** size that contains at least one instance of all phrases in the query
205090 ** expression that appear in the current row. If such a fragment of text
205091 ** cannot be found, the second iteration of the loop attempts to locate
205092 ** a pair of fragments, and so on.
205093 */
205094 int nSnippet = 0; /* Number of fragments in this snippet */
205095 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
205096 int nFToken = -1; /* Number of tokens in each fragment */
205097
205098 if( !pCsr->pExpr ){
205099 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
205100 return;
205101 }
205102
205103 /* Limit the snippet length to 64 tokens. */
205104 if( nToken<-64 ) nToken = -64;
205105 if( nToken>+64 ) nToken = +64;
205106
205107 for(nSnippet=1; 1; nSnippet++){
205108
205109 int iSnip; /* Loop counter 0..nSnippet-1 */
205110 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
205111 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
205112
205113 if( nToken>=0 ){
205114 nFToken = (nToken+nSnippet-1) / nSnippet;
205115 }else{
205116 nFToken = -1 * nToken;
205117 }
205118
205119 for(iSnip=0; iSnip<nSnippet; iSnip++){
205120 int iBestScore = -1; /* Best score of columns checked so far */
205121 int iRead; /* Used to iterate through columns */
205122 SnippetFragment *pFragment = &aSnippet[iSnip];
205123
205124 memset(pFragment, 0, sizeof(*pFragment));
205125
205126 /* Loop through all columns of the table being considered for snippets.
205127 ** If the iCol argument to this function was negative, this means all
205128 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
205129 */
205130 for(iRead=0; iRead<pTab->nColumn; iRead++){
205131 SnippetFragment sF = {0, 0, 0, 0};
205132 int iS = 0;
205133 if( iCol>=0 && iRead!=iCol ) continue;
205134
205135 /* Find the best snippet of nFToken tokens in column iRead. */
205136 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
205137 if( rc!=SQLITE_OK0 ){
205138 goto snippet_out;
205139 }
205140 if( iS>iBestScore ){
205141 *pFragment = sF;
205142 iBestScore = iS;
205143 }
205144 }
205145
205146 mCovered |= pFragment->covered;
205147 }
205148
205149 /* If all query phrases seen by fts3BestSnippet() are present in at least
205150 ** one of the nSnippet snippet fragments, break out of the loop.
205151 */
205152 assert( (mCovered&mSeen)==mCovered )((void) (0));
205153 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet)((int)(sizeof(aSnippet)/sizeof(aSnippet[0]))) ) break;
205154 }
205155
205156 assert( nFToken>0 )((void) (0));
205157
205158 for(i=0; i<nSnippet && rc==SQLITE_OK0; i++){
205159 rc = fts3SnippetText(pCsr, &aSnippet[i],
205160 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
205161 );
205162 }
205163
205164 snippet_out:
205165 sqlite3Fts3SegmentsClose(pTab);
205166 if( rc!=SQLITE_OK0 ){
205167 sqlite3_result_error_code(pCtx, rc);
205168 sqlite3_free(res.z);
205169 }else{
205170 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
205171 }
205172}
205173
205174
205175typedef struct TermOffset TermOffset;
205176typedef struct TermOffsetCtx TermOffsetCtx;
205177
205178struct TermOffset {
205179 char *pList; /* Position-list */
205180 i64 iPos; /* Position just read from pList */
205181 i64 iOff; /* Offset of this term from read positions */
205182};
205183
205184struct TermOffsetCtx {
205185 Fts3Cursor *pCsr;
205186 int iCol; /* Column of table to populate aTerm for */
205187 int iTerm;
205188 sqlite3_int64 iDocid;
205189 TermOffset *aTerm;
205190};
205191
205192/*
205193** This function is an sqlite3Fts3ExprIterate() callback used by sqlite3Fts3Offsets().
205194*/
205195static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
205196 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
205197 int nTerm; /* Number of tokens in phrase */
205198 int iTerm; /* For looping through nTerm phrase terms */
205199 char *pList; /* Pointer to position list for phrase */
205200 i64 iPos = 0; /* First position in position-list */
205201 int rc;
205202
205203 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
205204 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
205205 nTerm = pExpr->pPhrase->nToken;
205206 if( pList ){
205207 fts3GetDeltaPosition(&pList, &iPos);
205208 assert_fts3_nc( iPos>=0 )((void) (0));
205209 }
205210
205211 for(iTerm=0; iTerm<nTerm; iTerm++){
205212 TermOffset *pT = &p->aTerm[p->iTerm++];
205213 pT->iOff = nTerm-iTerm-1;
205214 pT->pList = pList;
205215 pT->iPos = iPos;
205216 }
205217
205218 return rc;
205219}
205220
205221/*
205222** Implementation of offsets() function.
205223*/
205224SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(
205225 sqlite3_context *pCtx, /* SQLite function call context */
205226 Fts3Cursor *pCsr /* Cursor object */
205227){
205228 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
205229 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
205230 int rc; /* Return Code */
205231 int nToken; /* Number of tokens in query */
205232 int iCol; /* Column currently being processed */
205233 StrBuffer res = {0, 0, 0}; /* Result string */
205234 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
205235
205236 if( !pCsr->pExpr ){
205237 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
205238 return;
205239 }
205240
205241 memset(&sCtx, 0, sizeof(sCtx));
205242 assert( pCsr->isRequireSeek==0 )((void) (0));
205243
205244 /* Count the number of terms in the query */
205245 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
205246 if( rc!=SQLITE_OK0 ) goto offsets_out;
205247
205248 /* Allocate the array of TermOffset iterators. */
205249 sCtx.aTerm = (TermOffset *)sqlite3Fts3MallocZero(sizeof(TermOffset)*nToken);
205250 if( 0==sCtx.aTerm ){
205251 rc = SQLITE_NOMEM7;
205252 goto offsets_out;
205253 }
205254 sCtx.iDocid = pCsr->iPrevId;
205255 sCtx.pCsr = pCsr;
205256
205257 /* Loop through the table columns, appending offset information to
205258 ** string-buffer res for each column.
205259 */
205260 for(iCol=0; iCol<pTab->nColumn; iCol++){
205261 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
205262 const char *ZDUMMY; /* Dummy argument used with xNext() */
205263 int NDUMMY = 0; /* Dummy argument used with xNext() */
205264 int iStart = 0;
205265 int iEnd = 0;
205266 int iCurrent = 0;
205267 const char *zDoc;
205268 int nDoc;
205269
205270 /* Initialize the contents of sCtx.aTerm[] for column iCol. This
205271 ** operation may fail if the database contains corrupt records.
205272 */
205273 sCtx.iCol = iCol;
205274 sCtx.iTerm = 0;
205275 rc = sqlite3Fts3ExprIterate(
205276 pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx
205277 );
205278 if( rc!=SQLITE_OK0 ) goto offsets_out;
205279
205280 /* Retreive the text stored in column iCol. If an SQL NULL is stored
205281 ** in column iCol, jump immediately to the next iteration of the loop.
205282 ** If an OOM occurs while retrieving the data (this can happen if SQLite
205283 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
205284 ** to the caller.
205285 */
205286 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
205287 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
205288 if( zDoc==0 ){
205289 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL5 ){
205290 continue;
205291 }
205292 rc = SQLITE_NOMEM7;
205293 goto offsets_out;
205294 }
205295
205296 /* Initialize a tokenizer iterator to iterate through column iCol. */
205297 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
205298 zDoc, nDoc, &pC
205299 );
205300 if( rc!=SQLITE_OK0 ) goto offsets_out;
205301
205302 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
205303 while( rc==SQLITE_OK0 ){
205304 int i; /* Used to loop through terms */
205305 int iMinPos = 0x7FFFFFFF; /* Position of next token */
205306 TermOffset *pTerm = 0; /* TermOffset associated with next token */
205307
205308 for(i=0; i<nToken; i++){
205309 TermOffset *pT = &sCtx.aTerm[i];
205310 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
205311 iMinPos = pT->iPos-pT->iOff;
205312 pTerm = pT;
205313 }
205314 }
205315
205316 if( !pTerm ){
205317 /* All offsets for this column have been gathered. */
205318 rc = SQLITE_DONE101;
205319 }else{
205320 assert_fts3_nc( iCurrent<=iMinPos )((void) (0));
205321 if( 0==(0xFE&*pTerm->pList) ){
205322 pTerm->pList = 0;
205323 }else{
205324 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
205325 }
205326 while( rc==SQLITE_OK0 && iCurrent<iMinPos ){
205327 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
205328 }
205329 if( rc==SQLITE_OK0 ){
205330 char aBuffer[64];
205331 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
205332 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
205333 );
205334 rc = fts3StringAppend(&res, aBuffer, -1);
205335 }else if( rc==SQLITE_DONE101 && pTab->zContentTbl==0 ){
205336 rc = FTS_CORRUPT_VTAB(11 | (1<<8));
205337 }
205338 }
205339 }
205340 if( rc==SQLITE_DONE101 ){
205341 rc = SQLITE_OK0;
205342 }
205343
205344 pMod->xClose(pC);
205345 if( rc!=SQLITE_OK0 ) goto offsets_out;
205346 }
205347
205348 offsets_out:
205349 sqlite3_free(sCtx.aTerm);
205350 assert( rc!=SQLITE_DONE )((void) (0));
205351 sqlite3Fts3SegmentsClose(pTab);
205352 if( rc!=SQLITE_OK0 ){
205353 sqlite3_result_error_code(pCtx, rc);
205354 sqlite3_free(res.z);
205355 }else{
205356 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
205357 }
205358 return;
205359}
205360
205361/*
205362** Implementation of matchinfo() function.
205363*/
205364SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(
205365 sqlite3_context *pContext, /* Function call context */
205366 Fts3Cursor *pCsr, /* FTS3 table cursor */
205367 const char *zArg /* Second arg to matchinfo() function */
205368){
205369 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
205370 const char *zFormat;
205371
205372 if( zArg ){
205373 zFormat = zArg;
205374 }else{
205375 zFormat = FTS3_MATCHINFO_DEFAULT"pcx";
205376 }
205377
205378 if( !pCsr->pExpr ){
205379 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
205380 return;
205381 }else{
205382 /* Retrieve matchinfo() data. */
205383 fts3GetMatchinfo(pContext, pCsr, zFormat);
205384 sqlite3Fts3SegmentsClose(pTab);
205385 }
205386}
205387
205388#endif
205389
205390/************** End of fts3_snippet.c ****************************************/
205391/************** Begin file fts3_unicode.c ************************************/
205392/*
205393** 2012 May 24
205394**
205395** The author disclaims copyright to this source code. In place of
205396** a legal notice, here is a blessing:
205397**
205398** May you do good and not evil.
205399** May you find forgiveness for yourself and forgive others.
205400** May you share freely, never taking more than you give.
205401**
205402******************************************************************************
205403**
205404** Implementation of the "unicode" full-text-search tokenizer.
205405*/
205406
205407#ifndef SQLITE_DISABLE_FTS3_UNICODE
205408
205409/* #include "fts3Int.h" */
205410#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
205411
205412/* #include <assert.h> */
205413/* #include <stdlib.h> */
205414/* #include <stdio.h> */
205415/* #include <string.h> */
205416
205417/* #include "fts3_tokenizer.h" */
205418
205419/*
205420** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
205421** from the sqlite3 source file utf.c. If this file is compiled as part
205422** of the amalgamation, they are not required.
205423*/
205424#ifndef SQLITE_AMALGAMATION1
205425
205426static const unsigned char sqlite3Utf8Trans1[] = {
205427 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
205428 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
205429 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
205430 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
205431 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
205432 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
205433 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
205434 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
205435};
205436
205437#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; } }
\
205438 c = *(zIn++); \
205439 if( c>=0xc0 ){ \
205440 c = sqlite3Utf8Trans1[c-0xc0]; \
205441 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
205442 c = (c<<6) + (0x3f & *(zIn++)); \
205443 } \
205444 if( c<0x80 \
205445 || (c&0xFFFFF800)==0xD800 \
205446 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
205447 }
205448
205449#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); } }
{ \
205450 if( c<0x00080 ){ \
205451 *zOut++ = (u8)(c&0xFF); \
205452 } \
205453 else if( c<0x00800 ){ \
205454 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
205455 *zOut++ = 0x80 + (u8)(c & 0x3F); \
205456 } \
205457 else if( c<0x10000 ){ \
205458 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
205459 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
205460 *zOut++ = 0x80 + (u8)(c & 0x3F); \
205461 }else{ \
205462 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
205463 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
205464 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
205465 *zOut++ = 0x80 + (u8)(c & 0x3F); \
205466 } \
205467}
205468
205469#endif /* ifndef SQLITE_AMALGAMATION */
205470
205471typedef struct unicode_tokenizer unicode_tokenizer;
205472typedef struct unicode_cursor unicode_cursor;
205473
205474struct unicode_tokenizer {
205475 sqlite3_tokenizer base;
205476 int eRemoveDiacritic;
205477 int nException;
205478 int *aiException;
205479};
205480
205481struct unicode_cursor {
205482 sqlite3_tokenizer_cursor base;
205483 const unsigned char *aInput; /* Input text being tokenized */
205484 int nInput; /* Size of aInput[] in bytes */
205485 int iOff; /* Current offset within aInput[] */
205486 int iToken; /* Index of next token to be returned */
205487 char *zToken; /* storage for current token */
205488 int nAlloc; /* space allocated at zToken */
205489};
205490
205491
205492/*
205493** Destroy a tokenizer allocated by unicodeCreate().
205494*/
205495static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
205496 if( pTokenizer ){
205497 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
205498 sqlite3_free(p->aiException);
205499 sqlite3_free(p);
205500 }
205501 return SQLITE_OK0;
205502}
205503
205504/*
205505** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
205506** statement has specified that the tokenizer for this table shall consider
205507** all characters in string zIn/nIn to be separators (if bAlnum==0) or
205508** token characters (if bAlnum==1).
205509**
205510** For each codepoint in the zIn/nIn string, this function checks if the
205511** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
205512** If so, no action is taken. Otherwise, the codepoint is added to the
205513** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
205514** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
205515** codepoints in the aiException[] array.
205516**
205517** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
205518** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
205519** It is not possible to change the behavior of the tokenizer with respect
205520** to these codepoints.
205521*/
205522static int unicodeAddExceptions(
205523 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
205524 int bAlnum, /* Replace Isalnum() return value with this */
205525 const char *zIn, /* Array of characters to make exceptions */
205526 int nIn /* Length of z in bytes */
205527){
205528 const unsigned char *z = (const unsigned char *)zIn;
205529 const unsigned char *zTerm = &z[nIn];
205530 unsigned int iCode;
205531 int nEntry = 0;
205532
205533 assert( bAlnum==0 || bAlnum==1 )((void) (0));
205534
205535 while( z<zTerm ){
205536 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; } }
;
205537 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 )((void) (0));
205538 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
205539 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
205540 ){
205541 nEntry++;
205542 }
205543 }
205544
205545 if( nEntry ){
205546 int *aNew; /* New aiException[] array */
205547 int nNew; /* Number of valid entries in array aNew[] */
205548
205549 aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
205550 if( aNew==0 ) return SQLITE_NOMEM7;
205551 nNew = p->nException;
205552
205553 z = (const unsigned char *)zIn;
205554 while( z<zTerm ){
205555 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; } }
;
205556 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
205557 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
205558 ){
205559 int i, j;
205560 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
205561 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
205562 aNew[i] = (int)iCode;
205563 nNew++;
205564 }
205565 }
205566 p->aiException = aNew;
205567 p->nException = nNew;
205568 }
205569
205570 return SQLITE_OK0;
205571}
205572
205573/*
205574** Return true if the p->aiException[] array contains the value iCode.
205575*/
205576static int unicodeIsException(unicode_tokenizer *p, int iCode){
205577 if( p->nException>0 ){
205578 int *a = p->aiException;
205579 int iLo = 0;
205580 int iHi = p->nException-1;
205581
205582 while( iHi>=iLo ){
205583 int iTest = (iHi + iLo) / 2;
205584 if( iCode==a[iTest] ){
205585 return 1;
205586 }else if( iCode>a[iTest] ){
205587 iLo = iTest+1;
205588 }else{
205589 iHi = iTest-1;
205590 }
205591 }
205592 }
205593
205594 return 0;
205595}
205596
205597/*
205598** Return true if, for the purposes of tokenization, codepoint iCode is
205599** considered a token character (not a separator).
205600*/
205601static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
205602 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 )((void) (0));
205603 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
205604}
205605
205606/*
205607** Create a new tokenizer instance.
205608*/
205609static int unicodeCreate(
205610 int nArg, /* Size of array argv[] */
205611 const char * const *azArg, /* Tokenizer creation arguments */
205612 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
205613){
205614 unicode_tokenizer *pNew; /* New tokenizer object */
205615 int i;
205616 int rc = SQLITE_OK0;
205617
205618 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
205619 if( pNew==NULL((void*)0) ) return SQLITE_NOMEM7;
205620 memset(pNew, 0, sizeof(unicode_tokenizer));
205621 pNew->eRemoveDiacritic = 1;
205622
205623 for(i=0; rc==SQLITE_OK0 && i<nArg; i++){
205624 const char *z = azArg[i];
205625 int n = (int)strlen(z);
205626
205627 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
205628 pNew->eRemoveDiacritic = 1;
205629 }
205630 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
205631 pNew->eRemoveDiacritic = 0;
205632 }
205633 else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
205634 pNew->eRemoveDiacritic = 2;
205635 }
205636 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
205637 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
205638 }
205639 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
205640 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
205641 }
205642 else{
205643 /* Unrecognized argument */
205644 rc = SQLITE_ERROR1;
205645 }
205646 }
205647
205648 if( rc!=SQLITE_OK0 ){
205649 unicodeDestroy((sqlite3_tokenizer *)pNew);
205650 pNew = 0;
205651 }
205652 *pp = (sqlite3_tokenizer *)pNew;
205653 return rc;
205654}
205655
205656/*
205657** Prepare to begin tokenizing a particular string. The input
205658** string to be tokenized is pInput[0..nBytes-1]. A cursor
205659** used to incrementally tokenize this string is returned in
205660** *ppCursor.
205661*/
205662static int unicodeOpen(
205663 sqlite3_tokenizer *p, /* The tokenizer */
205664 const char *aInput, /* Input string */
205665 int nInput, /* Size of string aInput in bytes */
205666 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
205667){
205668 unicode_cursor *pCsr;
205669
205670 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
205671 if( pCsr==0 ){
205672 return SQLITE_NOMEM7;
205673 }
205674 memset(pCsr, 0, sizeof(unicode_cursor));
205675
205676 pCsr->aInput = (const unsigned char *)aInput;
205677 if( aInput==0 ){
205678 pCsr->nInput = 0;
205679 pCsr->aInput = (const unsigned char*)"";
205680 }else if( nInput<0 ){
205681 pCsr->nInput = (int)strlen(aInput);
205682 }else{
205683 pCsr->nInput = nInput;
205684 }
205685
205686 *pp = &pCsr->base;
205687 UNUSED_PARAMETER(p)(void)(p);
205688 return SQLITE_OK0;
205689}
205690
205691/*
205692** Close a tokenization cursor previously opened by a call to
205693** simpleOpen() above.
205694*/
205695static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
205696 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
205697 sqlite3_free(pCsr->zToken);
205698 sqlite3_free(pCsr);
205699 return SQLITE_OK0;
205700}
205701
205702/*
205703** Extract the next token from a tokenization cursor. The cursor must
205704** have been opened by a prior call to simpleOpen().
205705*/
205706static int unicodeNext(
205707 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
205708 const char **paToken, /* OUT: Token text */
205709 int *pnToken, /* OUT: Number of bytes at *paToken */
205710 int *piStart, /* OUT: Starting offset of token */
205711 int *piEnd, /* OUT: Ending offset of token */
205712 int *piPos /* OUT: Position integer of token */
205713){
205714 unicode_cursor *pCsr = (unicode_cursor *)pC;
205715 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
205716 unsigned int iCode = 0;
205717 char *zOut;
205718 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
205719 const unsigned char *zStart = z;
205720 const unsigned char *zEnd;
205721 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
205722
205723 /* Scan past any delimiter characters before the start of the next token.
205724 ** Return SQLITE_DONE early if this takes us all the way to the end of
205725 ** the input. */
205726 while( z<zTerm ){
205727 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; } }
;
205728 if( unicodeIsAlnum(p, (int)iCode) ) break;
205729 zStart = z;
205730 }
205731 if( zStart>=zTerm ) return SQLITE_DONE101;
205732
205733 zOut = pCsr->zToken;
205734 do {
205735 int iOut;
205736
205737 /* Grow the output buffer if required. */
205738 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
205739 char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
205740 if( !zNew ) return SQLITE_NOMEM7;
205741 zOut = &zNew[zOut - pCsr->zToken];
205742 pCsr->zToken = zNew;
205743 pCsr->nAlloc += 64;
205744 }
205745
205746 /* Write the folded case of the last character read to the output */
205747 zEnd = z;
205748 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
205749 if( iOut ){
205750 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); } }
;
205751 }
205752
205753 /* If the cursor is not at EOF, read the next character */
205754 if( z>=zTerm ) break;
205755 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; } }
;
205756 }while( unicodeIsAlnum(p, (int)iCode)
205757 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
205758 );
205759
205760 /* Set the output variables and return. */
205761 pCsr->iOff = (int)(z - pCsr->aInput);
205762 *paToken = pCsr->zToken;
205763 *pnToken = (int)(zOut - pCsr->zToken);
205764 *piStart = (int)(zStart - pCsr->aInput);
205765 *piEnd = (int)(zEnd - pCsr->aInput);
205766 *piPos = pCsr->iToken++;
205767 return SQLITE_OK0;
205768}
205769
205770/*
205771** Set *ppModule to a pointer to the sqlite3_tokenizer_module
205772** structure for the unicode tokenizer.
205773*/
205774SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
205775 static const sqlite3_tokenizer_module module = {
205776 0,
205777 unicodeCreate,
205778 unicodeDestroy,
205779 unicodeOpen,
205780 unicodeClose,
205781 unicodeNext,
205782 0,
205783 };
205784 *ppModule = &module;
205785}
205786
205787#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
205788#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
205789
205790/************** End of fts3_unicode.c ****************************************/
205791/************** Begin file fts3_unicode2.c ***********************************/
205792/*
205793** 2012-05-25
205794**
205795** The author disclaims copyright to this source code. In place of
205796** a legal notice, here is a blessing:
205797**
205798** May you do good and not evil.
205799** May you find forgiveness for yourself and forgive others.
205800** May you share freely, never taking more than you give.
205801**
205802******************************************************************************
205803*/
205804
205805/*
205806** DO NOT EDIT THIS MACHINE GENERATED FILE.
205807*/
205808
205809#ifndef SQLITE_DISABLE_FTS3_UNICODE
205810#if defined(SQLITE_ENABLE_FTS31) || defined(SQLITE_ENABLE_FTS41)
205811
205812/* #include <assert.h> */
205813
205814/*
205815** Return true if the argument corresponds to a unicode codepoint
205816** classified as either a letter or a number. Otherwise false.
205817**
205818** The results are undefined if the value passed to this function
205819** is less than zero.
205820*/
205821SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int c){
205822 /* Each unsigned integer in the following array corresponds to a contiguous
205823 ** range of unicode codepoints that are not either letters or numbers (i.e.
205824 ** codepoints for which this function should return 0).
205825 **
205826 ** The most significant 22 bits in each 32-bit value contain the first
205827 ** codepoint in the range. The least significant 10 bits are used to store
205828 ** the size of the range (always at least 1). In other words, the value
205829 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
205830 ** C. It is not possible to represent a range larger than 1023 codepoints
205831 ** using this format.
205832 */
205833 static const unsigned int aEntry[] = {
205834 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
205835 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
205836 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
205837 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
205838 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
205839 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
205840 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
205841 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
205842 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
205843 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
205844 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
205845 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
205846 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
205847 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
205848 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
205849 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
205850 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
205851 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
205852 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
205853 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
205854 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
205855 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
205856 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
205857 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
205858 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
205859 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
205860 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
205861 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
205862 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
205863 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
205864 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
205865 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
205866 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
205867 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
205868 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
205869 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
205870 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
205871 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
205872 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
205873 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
205874 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
205875 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
205876 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
205877 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
205878 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
205879 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
205880 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
205881 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
205882 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
205883 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
205884 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
205885 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
205886 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
205887 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
205888 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
205889 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
205890 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
205891 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
205892 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
205893 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
205894 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
205895 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
205896 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
205897 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
205898 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
205899 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
205900 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
205901 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
205902 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
205903 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
205904 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
205905 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
205906 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
205907 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
205908 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
205909 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
205910 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
205911 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
205912 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
205913 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
205914 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
205915 0x380400F0,
205916 };
205917 static const unsigned int aAscii[4] = {
205918 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
205919 };
205920
205921 if( (unsigned int)c<128 ){
205922 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
205923 }else if( (unsigned int)c<(1<<22) ){
205924 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
205925 int iRes = 0;
205926 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
205927 int iLo = 0;
205928 while( iHi>=iLo ){
205929 int iTest = (iHi + iLo) / 2;
205930 if( key >= aEntry[iTest] ){
205931 iRes = iTest;
205932 iLo = iTest+1;
205933 }else{
205934 iHi = iTest-1;
205935 }
205936 }
205937 assert( aEntry[0]<key )((void) (0));
205938 assert( key>=aEntry[iRes] )((void) (0));
205939 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
205940 }
205941 return 1;
205942}
205943
205944
205945/*
205946** If the argument is a codepoint corresponding to a lowercase letter
205947** in the ASCII range with a diacritic added, return the codepoint
205948** of the ASCII letter only. For example, if passed 235 - "LATIN
205949** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
205950** E"). The resuls of passing a codepoint that corresponds to an
205951** uppercase letter are undefined.
205952*/
205953static int remove_diacritic(int c, int bComplex){
205954 unsigned short aDia[] = {
205955 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
205956 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
205957 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
205958 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
205959 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
205960 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
205961 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
205962 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
205963 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
205964 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
205965 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
205966 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
205967 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
205968 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
205969 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
205970 63182, 63242, 63274, 63310, 63368, 63390,
205971 };
205972#define HIBIT((unsigned char)0x80) ((unsigned char)0x80)
205973 unsigned char aChar[] = {
205974 '\0', 'a', 'c', 'e', 'i', 'n',
205975 'o', 'u', 'y', 'y', 'a', 'c',
205976 'd', 'e', 'e', 'g', 'h', 'i',
205977 'j', 'k', 'l', 'n', 'o', 'r',
205978 's', 't', 'u', 'u', 'w', 'y',
205979 'z', 'o', 'u', 'a', 'i', 'o',
205980 'u', 'u'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'g', 'k', 'o',
205981 'o'|HIBIT((unsigned char)0x80), 'j', 'g', 'n', 'a'|HIBIT((unsigned char)0x80), 'a',
205982 'e', 'i', 'o', 'r', 'u', 's',
205983 't', 'h', 'a', 'e', 'o'|HIBIT((unsigned char)0x80), 'o',
205984 'o'|HIBIT((unsigned char)0x80), 'y', '\0', '\0', '\0', '\0',
205985 '\0', '\0', '\0', '\0', 'a', 'b',
205986 'c'|HIBIT((unsigned char)0x80), 'd', 'd', 'e'|HIBIT((unsigned char)0x80), 'e', 'e'|HIBIT((unsigned char)0x80),
205987 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT((unsigned char)0x80),
205988 'k', 'l', 'l'|HIBIT((unsigned char)0x80), 'l', 'm', 'n',
205989 'o'|HIBIT((unsigned char)0x80), 'p', 'r', 'r'|HIBIT((unsigned char)0x80), 'r', 's',
205990 's'|HIBIT((unsigned char)0x80), 't', 'u', 'u'|HIBIT((unsigned char)0x80), 'v', 'w',
205991 'w', 'x', 'y', 'z', 'h', 't',
205992 'w', 'y', 'a', 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80),
205993 'e', 'e'|HIBIT((unsigned char)0x80), 'e'|HIBIT((unsigned char)0x80), 'i', 'o', 'o'|HIBIT((unsigned char)0x80),
205994 'o'|HIBIT((unsigned char)0x80), 'o'|HIBIT((unsigned char)0x80), 'u', 'u'|HIBIT((unsigned char)0x80), 'u'|HIBIT((unsigned char)0x80), 'y',
205995 };
205996
205997 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
205998 int iRes = 0;
205999 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
206000 int iLo = 0;
206001 while( iHi>=iLo ){
206002 int iTest = (iHi + iLo) / 2;
206003 if( key >= aDia[iTest] ){
206004 iRes = iTest;
206005 iLo = iTest+1;
206006 }else{
206007 iHi = iTest-1;
206008 }
206009 }
206010 assert( key>=aDia[iRes] )((void) (0));
206011 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
206012 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
206013}
206014
206015
206016/*
206017** Return true if the argument interpreted as a unicode codepoint
206018** is a diacritical modifier character.
206019*/
206020SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int c){
206021 unsigned int mask0 = 0x08029FDF;
206022 unsigned int mask1 = 0x000361F8;
206023 if( c<768 || c>817 ) return 0;
206024 return (c < 768+32) ?
206025 (mask0 & ((unsigned int)1 << (c-768))) :
206026 (mask1 & ((unsigned int)1 << (c-768-32)));
206027}
206028
206029
206030/*
206031** Interpret the argument as a unicode codepoint. If the codepoint
206032** is an upper case character that has a lower case equivalent,
206033** return the codepoint corresponding to the lower case version.
206034** Otherwise, return a copy of the argument.
206035**
206036** The results are undefined if the value passed to this function
206037** is less than zero.
206038*/
206039SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
206040 /* Each entry in the following array defines a rule for folding a range
206041 ** of codepoints to lower case. The rule applies to a range of nRange
206042 ** codepoints starting at codepoint iCode.
206043 **
206044 ** If the least significant bit in flags is clear, then the rule applies
206045 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
206046 ** need to be folded). Or, if it is set, then the rule only applies to
206047 ** every second codepoint in the range, starting with codepoint C.
206048 **
206049 ** The 7 most significant bits in flags are an index into the aiOff[]
206050 ** array. If a specific codepoint C does require folding, then its lower
206051 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
206052 **
206053 ** The contents of this array are generated by parsing the CaseFolding.txt
206054 ** file distributed as part of the "Unicode Character Database". See
206055 ** http://www.unicode.org for details.
206056 */
206057 static const struct TableEntry {
206058 unsigned short iCode;
206059 unsigned char flags;
206060 unsigned char nRange;
206061 } aEntry[] = {
206062 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
206063 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
206064 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
206065 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
206066 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
206067 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
206068 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
206069 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
206070 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
206071 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
206072 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
206073 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
206074 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
206075 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
206076 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
206077 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
206078 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
206079 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
206080 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
206081 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
206082 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
206083 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
206084 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
206085 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
206086 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
206087 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
206088 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
206089 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
206090 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
206091 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
206092 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
206093 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
206094 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
206095 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
206096 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
206097 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
206098 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
206099 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
206100 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
206101 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
206102 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
206103 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
206104 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
206105 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
206106 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
206107 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
206108 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
206109 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
206110 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
206111 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
206112 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
206113 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
206114 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
206115 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
206116 {65313, 14, 26},
206117 };
206118 static const unsigned short aiOff[] = {
206119 1, 2, 8, 15, 16, 26, 28, 32,
206120 37, 38, 40, 48, 63, 64, 69, 71,
206121 79, 80, 116, 202, 203, 205, 206, 207,
206122 209, 210, 211, 213, 214, 217, 218, 219,
206123 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
206124 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
206125 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
206126 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
206127 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
206128 65514, 65521, 65527, 65528, 65529,
206129 };
206130
206131 int ret = c;
206132
206133 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
206134
206135 if( c<128 ){
206136 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
206137 }else if( c<65536 ){
206138 const struct TableEntry *p;
206139 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
206140 int iLo = 0;
206141 int iRes = -1;
206142
206143 assert( c>aEntry[0].iCode )((void) (0));
206144 while( iHi>=iLo ){
206145 int iTest = (iHi + iLo) / 2;
206146 int cmp = (c - aEntry[iTest].iCode);
206147 if( cmp>=0 ){
206148 iRes = iTest;
206149 iLo = iTest+1;
206150 }else{
206151 iHi = iTest-1;
206152 }
206153 }
206154
206155 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
206156 p = &aEntry[iRes];
206157 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
206158 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
206159 assert( ret>0 )((void) (0));
206160 }
206161
206162 if( eRemoveDiacritic ){
206163 ret = remove_diacritic(ret, eRemoveDiacritic==2);
206164 }
206165 }
206166
206167 else if( c>=66560 && c<66600 ){
206168 ret = c + 40;
206169 }
206170
206171 return ret;
206172}
206173#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
206174#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
206175
206176/************** End of fts3_unicode2.c ***************************************/
206177/************** Begin file json.c ********************************************/
206178/*
206179** 2015-08-12
206180**
206181** The author disclaims copyright to this source code. In place of
206182** a legal notice, here is a blessing:
206183**
206184** May you do good and not evil.
206185** May you find forgiveness for yourself and forgive others.
206186** May you share freely, never taking more than you give.
206187**
206188******************************************************************************
206189**
206190** SQLite JSON functions.
206191**
206192** This file began as an extension in ext/misc/json1.c in 2015. That
206193** extension proved so useful that it has now been moved into the core.
206194**
206195** The original design stored all JSON as pure text, canonical RFC-8259.
206196** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16).
206197** All generated JSON text still conforms strictly to RFC-8259, but text
206198** with JSON-5 extensions is accepted as input.
206199**
206200** Beginning with version 3.45.0 (circa 2024-01-01), these routines also
206201** accept BLOB values that have JSON encoded using a binary representation
206202** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk
206203** format SQLite JSONB is completely different and incompatible with
206204** PostgreSQL JSONB.
206205**
206206** Decoding and interpreting JSONB is still O(N) where N is the size of
206207** the input, the same as text JSON. However, the constant of proportionality
206208** for JSONB is much smaller due to faster parsing. The size of each
206209** element in JSONB is encoded in its header, so there is no need to search
206210** for delimiters using persnickety syntax rules. JSONB seems to be about
206211** 3x faster than text JSON as a result. JSONB is also tends to be slightly
206212** smaller than text JSON, by 5% or 10%, but there are corner cases where
206213** JSONB can be slightly larger. So you are not far mistaken to say that
206214** a JSONB blob is the same size as the equivalent RFC-8259 text.
206215**
206216**
206217** THE JSONB ENCODING:
206218**
206219** Every JSON element is encoded in JSONB as a header and a payload.
206220** The header is between 1 and 9 bytes in size. The payload is zero
206221** or more bytes.
206222**
206223** The lower 4 bits of the first byte of the header determines the
206224** element type:
206225**
206226** 0: NULL
206227** 1: TRUE
206228** 2: FALSE
206229** 3: INT -- RFC-8259 integer literal
206230** 4: INT5 -- JSON5 integer literal
206231** 5: FLOAT -- RFC-8259 floating point literal
206232** 6: FLOAT5 -- JSON5 floating point literal
206233** 7: TEXT -- Text literal acceptable to both SQL and JSON
206234** 8: TEXTJ -- Text containing RFC-8259 escapes
206235** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes
206236** 10: TEXTRAW -- Text containing unescaped syntax characters
206237** 11: ARRAY
206238** 12: OBJECT
206239**
206240** The other three possible values (13-15) are reserved for future
206241** enhancements.
206242**
206243** The upper 4 bits of the first byte determine the size of the header
206244** and sometimes also the size of the payload. If X is the first byte
206245** of the element and if X>>4 is between 0 and 11, then the payload
206246** will be that many bytes in size and the header is exactly one byte
206247** in size. Other four values for X>>4 (12-15) indicate that the header
206248** is more than one byte in size and that the payload size is determined
206249** by the remainder of the header, interpreted as a unsigned big-endian
206250** integer.
206251**
206252** Value of X>>4 Size integer Total header size
206253** ------------- -------------------- -----------------
206254** 12 1 byte (0-255) 2
206255** 13 2 byte (0-65535) 3
206256** 14 4 byte (0-4294967295) 5
206257** 15 8 byte (0-1.8e19) 9
206258**
206259** The payload size need not be expressed in its minimal form. For example,
206260** if the payload size is 10, the size can be expressed in any of 5 different
206261** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by on 0x0a byte,
206262** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by
206263** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and
206264** a single byte of 0x0a. The shorter forms are preferred, of course, but
206265** sometimes when generating JSONB, the payload size is not known in advance
206266** and it is convenient to reserve sufficient header space to cover the
206267** largest possible payload size and then come back later and patch up
206268** the size when it becomes known, resulting in a non-minimal encoding.
206269**
206270** The value (X>>4)==15 is not actually used in the current implementation
206271** (as SQLite is currently unable handle BLOBs larger than about 2GB)
206272** but is included in the design to allow for future enhancements.
206273**
206274** The payload follows the header. NULL, TRUE, and FALSE have no payload and
206275** their payload size must always be zero. The payload for INT, INT5,
206276** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the
206277** "..." or '...' delimiters are omitted from the various text encodings.
206278** The payload for ARRAY and OBJECT is a list of additional elements that
206279** are the content for the array or object. The payload for an OBJECT
206280** must be an even number of elements. The first element of each pair is
206281** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW.
206282**
206283** A valid JSONB blob consists of a single element, as described above.
206284** Usually this will be an ARRAY or OBJECT element which has many more
206285** elements as its content. But the overall blob is just a single element.
206286**
206287** Input validation for JSONB blobs simply checks that the element type
206288** code is between 0 and 12 and that the total size of the element
206289** (header plus payload) is the same as the size of the BLOB. If those
206290** checks are true, the BLOB is assumed to be JSONB and processing continues.
206291** Errors are only raised if some other miscoding is discovered during
206292** processing.
206293**
206294** Additional information can be found in the doc/jsonb.md file of the
206295** canonical SQLite source tree.
206296*/
206297#ifndef SQLITE_OMIT_JSON
206298/* #include "sqliteInt.h" */
206299
206300/* JSONB element types
206301*/
206302#define JSONB_NULL0 0 /* "null" */
206303#define JSONB_TRUE1 1 /* "true" */
206304#define JSONB_FALSE2 2 /* "false" */
206305#define JSONB_INT3 3 /* integer acceptable to JSON and SQL */
206306#define JSONB_INT54 4 /* integer in 0x000 notation */
206307#define JSONB_FLOAT5 5 /* float acceptable to JSON and SQL */
206308#define JSONB_FLOAT56 6 /* float with JSON5 extensions */
206309#define JSONB_TEXT7 7 /* Text compatible with both JSON and SQL */
206310#define JSONB_TEXTJ8 8 /* Text with JSON escapes */
206311#define JSONB_TEXT59 9 /* Text with JSON-5 escape */
206312#define JSONB_TEXTRAW10 10 /* SQL text that needs escaping for JSON */
206313#define JSONB_ARRAY11 11 /* An array */
206314#define JSONB_OBJECT12 12 /* An object */
206315
206316/* Human-readable names for the JSONB values. The index for each
206317** string must correspond to the JSONB_* integer above.
206318*/
206319static const char * const jsonbType[] = {
206320 "null", "true", "false", "integer", "integer",
206321 "real", "real", "text", "text", "text",
206322 "text", "array", "object", "", "", "", ""
206323};
206324
206325/*
206326** Growing our own isspace() routine this way is twice as fast as
206327** the library isspace() function, resulting in a 7% overall performance
206328** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
206329*/
206330static const char jsonIsSpace[] = {
206331 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
206332 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206333 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206334 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206338 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206339
206340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206341 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206344 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206345 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206346 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206347 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206348};
206349#define jsonIsspace(x)(jsonIsSpace[(unsigned char)x]) (jsonIsSpace[(unsigned char)x])
206350
206351/*
206352** The set of all space characters recognized by jsonIsspace().
206353** Useful as the second argument to strspn().
206354*/
206355static const char jsonSpaces[] = "\011\012\015\040";
206356
206357/*
206358** Characters that are special to JSON. Control characters,
206359** '"' and '\\' and '\''. Actually, '\'' is not special to
206360** canonical JSON, but it is special in JSON-5, so we include
206361** it in the set of special characters.
206362*/
206363static const char jsonIsOk[256] = {
206364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206365 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206366 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
206367 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206368 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206369 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
206370 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206371 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206372
206373 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206375 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206376 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206377 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206378 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206379 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
206380 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
206381};
206382
206383/* Objects */
206384typedef struct JsonCache JsonCache;
206385typedef struct JsonString JsonString;
206386typedef struct JsonParse JsonParse;
206387
206388/*
206389** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
206390*/
206391#define JSON_CACHE_ID(-429938) (-429938) /* Cache entry */
206392#define JSON_CACHE_SIZE4 4 /* Max number of cache entries */
206393
206394/*
206395** jsonUnescapeOneChar() returns this invalid code point if it encounters
206396** a syntax error.
206397*/
206398#define JSON_INVALID_CHAR0x99999 0x99999
206399
206400/* A cache mapping JSON text into JSONB blobs.
206401**
206402** Each cache entry is a JsonParse object with the following restrictions:
206403**
206404** * The bReadOnly flag must be set
206405**
206406** * The aBlob[] array must be owned by the JsonParse object. In other
206407** words, nBlobAlloc must be non-zero.
206408**
206409** * eEdit and delta must be zero.
206410**
206411** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
206412*/
206413struct JsonCache {
206414 sqlite3 *db; /* Database connection */
206415 int nUsed; /* Number of active entries in the cache */
206416 JsonParse *a[JSON_CACHE_SIZE4]; /* One line for each cache entry */
206417};
206418
206419/* An instance of this object represents a JSON string
206420** under construction. Really, this is a generic string accumulator
206421** that can be and is used to create strings other than JSON.
206422**
206423** If the generated string is longer than will fit into the zSpace[] buffer,
206424** then it will be an RCStr string. This aids with caching of large
206425** JSON strings.
206426*/
206427struct JsonString {
206428 sqlite3_context *pCtx; /* Function context - put error messages here */
206429 char *zBuf; /* Append JSON content here */
206430 u64 nAlloc; /* Bytes of storage available in zBuf[] */
206431 u64 nUsed; /* Bytes of zBuf[] currently used */
206432 u8 bStatic; /* True if zBuf is static space */
206433 u8 eErr; /* True if an error has been encountered */
206434 char zSpace[100]; /* Initial static space */
206435};
206436
206437/* Allowed values for JsonString.eErr */
206438#define JSTRING_OOM0x01 0x01 /* Out of memory */
206439#define JSTRING_MALFORMED0x02 0x02 /* Malformed JSONB */
206440#define JSTRING_ERR0x04 0x04 /* Error already sent to sqlite3_result */
206441
206442/* The "subtype" set for text JSON values passed through using
206443** sqlite3_result_subtype() and sqlite3_value_subtype().
206444*/
206445#define JSON_SUBTYPE74 74 /* Ascii for "J" */
206446
206447/*
206448** Bit values for the flags passed into various SQL function implementations
206449** via the sqlite3_user_data() value.
206450*/
206451#define JSON_JSON0x01 0x01 /* Result is always JSON */
206452#define JSON_SQL0x02 0x02 /* Result is always SQL */
206453#define JSON_ABPATH0x03 0x03 /* Allow abbreviated JSON path specs */
206454#define JSON_ISSET0x04 0x04 /* json_set(), not json_insert() */
206455#define JSON_BLOB0x08 0x08 /* Use the BLOB output format */
206456
206457
206458/* A parsed JSON value. Lifecycle:
206459**
206460** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
206461** original text is stored in zJson. This step is skipped if the
206462** input is JSONB instead of text JSON.
206463**
206464** 2. The aBlob[] array is searched using the JSON path notation, if needed.
206465**
206466** 3. Zero or more changes are made to aBlob[] (via json_remove() or
206467** json_replace() or json_patch() or similar).
206468**
206469** 4. New JSON text is generated from the aBlob[] for output. This step
206470** is skipped if the function is one of the jsonb_* functions that
206471** returns JSONB instead of text JSON.
206472*/
206473struct JsonParse {
206474 u8 *aBlob; /* JSONB representation of JSON value */
206475 u32 nBlob; /* Bytes of aBlob[] actually used */
206476 u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
206477 char *zJson; /* Json text used for parsing */
206478 sqlite3 *db; /* The database connection to which this object belongs */
206479 int nJson; /* Length of the zJson string in bytes */
206480 u32 nJPRef; /* Number of references to this object */
206481 u32 iErr; /* Error location in zJson[] */
206482 u16 iDepth; /* Nesting depth */
206483 u8 nErr; /* Number of errors seen */
206484 u8 oom; /* Set to true if out of memory */
206485 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
206486 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
206487 u8 bReadOnly; /* Do not modify. */
206488 /* Search and edit information. See jsonLookupStep() */
206489 u8 eEdit; /* Edit operation to apply */
206490 int delta; /* Size change due to the edit */
206491 u32 nIns; /* Number of bytes to insert */
206492 u32 iLabel; /* Location of label if search landed on an object value */
206493 u8 *aIns; /* Content to be inserted */
206494};
206495
206496/* Allowed values for JsonParse.eEdit */
206497#define JEDIT_DEL1 1 /* Delete if exists */
206498#define JEDIT_REPL2 2 /* Overwrite if exists */
206499#define JEDIT_INS3 3 /* Insert if not exists */
206500#define JEDIT_SET4 4 /* Insert or overwrite */
206501
206502/*
206503** Maximum nesting depth of JSON for this implementation.
206504**
206505** This limit is needed to avoid a stack overflow in the recursive
206506** descent parser. A depth of 1000 is far deeper than any sane JSON
206507** should go. Historical note: This limit was 2000 prior to version 3.42.0
206508*/
206509#ifndef SQLITE_JSON_MAX_DEPTH
206510# define JSON_MAX_DEPTH1000 1000
206511#else
206512# define JSON_MAX_DEPTH1000 SQLITE_JSON_MAX_DEPTH
206513#endif
206514
206515/*
206516** Allowed values for the flgs argument to jsonParseFuncArg();
206517*/
206518#define JSON_EDITABLE0x01 0x01 /* Generate a writable JsonParse object */
206519#define JSON_KEEPERROR0x02 0x02 /* Return non-NULL even if there is an error */
206520
206521/**************************************************************************
206522** Forward references
206523**************************************************************************/
206524static void jsonReturnStringAsBlob(JsonString*);
206525static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
206526static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
206527static void jsonReturnParse(sqlite3_context*,JsonParse*);
206528static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
206529static void jsonParseFree(JsonParse*);
206530static u32 jsonbPayloadSize(const JsonParse*, u32, u32*);
206531static u32 jsonUnescapeOneChar(const char*, u32, u32*);
206532
206533/**************************************************************************
206534** Utility routines for dealing with JsonCache objects
206535**************************************************************************/
206536
206537/*
206538** Free a JsonCache object.
206539*/
206540static void jsonCacheDelete(JsonCache *p){
206541 int i;
206542 for(i=0; i<p->nUsed; i++){
206543 jsonParseFree(p->a[i]);
206544 }
206545 sqlite3DbFree(p->db, p);
206546}
206547static void jsonCacheDeleteGeneric(void *p){
206548 jsonCacheDelete((JsonCache*)p);
206549}
206550
206551/*
206552** Insert a new entry into the cache. If the cache is full, expel
206553** the least recently used entry. Return SQLITE_OK on success or a
206554** result code otherwise.
206555**
206556** Cache entries are stored in age order, oldest first.
206557*/
206558static int jsonCacheInsert(
206559 sqlite3_context *ctx, /* The SQL statement context holding the cache */
206560 JsonParse *pParse /* The parse object to be added to the cache */
206561){
206562 JsonCache *p;
206563
206564 assert( pParse->zJson!=0 )((void) (0));
206565 assert( pParse->bJsonIsRCStr )((void) (0));
206566 assert( pParse->delta==0 )((void) (0));
206567 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID(-429938));
206568 if( p==0 ){
206569 sqlite3 *db = sqlite3_context_db_handle(ctx);
206570 p = sqlite3DbMallocZero(db, sizeof(*p));
206571 if( p==0 ) return SQLITE_NOMEM7;
206572 p->db = db;
206573 sqlite3_set_auxdata(ctx, JSON_CACHE_ID(-429938), p, jsonCacheDeleteGeneric);
206574 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID(-429938));
206575 if( p==0 ) return SQLITE_NOMEM7;
206576 }
206577 if( p->nUsed >= JSON_CACHE_SIZE4 ){
206578 jsonParseFree(p->a[0]);
206579 memmove(p->a, &p->a[1], (JSON_CACHE_SIZE4-1)*sizeof(p->a[0]));
206580 p->nUsed = JSON_CACHE_SIZE4-1;
206581 }
206582 assert( pParse->nBlobAlloc>0 )((void) (0));
206583 pParse->eEdit = 0;
206584 pParse->nJPRef++;
206585 pParse->bReadOnly = 1;
206586 p->a[p->nUsed] = pParse;
206587 p->nUsed++;
206588 return SQLITE_OK0;
206589}
206590
206591/*
206592** Search for a cached translation the json text supplied by pArg. Return
206593** the JsonParse object if found. Return NULL if not found.
206594**
206595** When a match if found, the matching entry is moved to become the
206596** most-recently used entry if it isn't so already.
206597**
206598** The JsonParse object returned still belongs to the Cache and might
206599** be deleted at any moment. If the caller whants the JsonParse to
206600** linger, it needs to increment the nPJRef reference counter.
206601*/
206602static JsonParse *jsonCacheSearch(
206603 sqlite3_context *ctx, /* The SQL statement context holding the cache */
206604 sqlite3_value *pArg /* Function argument containing SQL text */
206605){
206606 JsonCache *p;
206607 int i;
206608 const char *zJson;
206609 int nJson;
206610
206611 if( sqlite3_value_type(pArg)!=SQLITE_TEXT3 ){
206612 return 0;
206613 }
206614 zJson = (const char*)sqlite3_value_text(pArg);
206615 if( zJson==0 ) return 0;
206616 nJson = sqlite3_value_bytes(pArg);
206617
206618 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID(-429938));
206619 if( p==0 ){
206620 return 0;
206621 }
206622 for(i=0; i<p->nUsed; i++){
206623 if( p->a[i]->zJson==zJson ) break;
206624 }
206625 if( i>=p->nUsed ){
206626 for(i=0; i<p->nUsed; i++){
206627 if( p->a[i]->nJson!=nJson ) continue;
206628 if( memcmp(p->a[i]->zJson, zJson, nJson)==0 ) break;
206629 }
206630 }
206631 if( i<p->nUsed ){
206632 if( i<p->nUsed-1 ){
206633 /* Make the matching entry the most recently used entry */
206634 JsonParse *tmp = p->a[i];
206635 memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp));
206636 p->a[p->nUsed-1] = tmp;
206637 i = p->nUsed - 1;
206638 }
206639 assert( p->a[i]->delta==0 )((void) (0));
206640 return p->a[i];
206641 }else{
206642 return 0;
206643 }
206644}
206645
206646/**************************************************************************
206647** Utility routines for dealing with JsonString objects
206648**************************************************************************/
206649
206650/* Turn uninitialized bulk memory into a valid JsonString object
206651** holding a zero-length string.
206652*/
206653static void jsonStringZero(JsonString *p){
206654 p->zBuf = p->zSpace;
206655 p->nAlloc = sizeof(p->zSpace);
206656 p->nUsed = 0;
206657 p->bStatic = 1;
206658}
206659
206660/* Initialize the JsonString object
206661*/
206662static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){
206663 p->pCtx = pCtx;
206664 p->eErr = 0;
206665 jsonStringZero(p);
206666}
206667
206668/* Free all allocated memory and reset the JsonString object back to its
206669** initial state.
206670*/
206671static void jsonStringReset(JsonString *p){
206672 if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf);
206673 jsonStringZero(p);
206674}
206675
206676/* Report an out-of-memory (OOM) condition
206677*/
206678static void jsonStringOom(JsonString *p){
206679 p->eErr |= JSTRING_OOM0x01;
206680 if( p->pCtx ) sqlite3_result_error_nomem(p->pCtx);
206681 jsonStringReset(p);
206682}
206683
206684/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
206685** Return zero on success. Return non-zero on an OOM error
206686*/
206687static int jsonStringGrow(JsonString *p, u32 N){
206688 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
206689 char *zNew;
206690 if( p->bStatic ){
206691 if( p->eErr ) return 1;
206692 zNew = sqlite3RCStrNew(nTotal);
206693 if( zNew==0 ){
206694 jsonStringOom(p);
206695 return SQLITE_NOMEM7;
206696 }
206697 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
206698 p->zBuf = zNew;
206699 p->bStatic = 0;
206700 }else{
206701 p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
206702 if( p->zBuf==0 ){
206703 p->eErr |= JSTRING_OOM0x01;
206704 jsonStringZero(p);
206705 return SQLITE_NOMEM7;
206706 }
206707 }
206708 p->nAlloc = nTotal;
206709 return SQLITE_OK0;
206710}
206711
206712/* Append N bytes from zIn onto the end of the JsonString string.
206713*/
206714static SQLITE_NOINLINE__attribute__((noinline)) void jsonStringExpandAndAppend(
206715 JsonString *p,
206716 const char *zIn,
206717 u32 N
206718){
206719 assert( N>0 )((void) (0));
206720 if( jsonStringGrow(p,N) ) return;
206721 memcpy(p->zBuf+p->nUsed, zIn, N);
206722 p->nUsed += N;
206723}
206724static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
206725 if( N==0 ) return;
206726 if( N+p->nUsed >= p->nAlloc ){
206727 jsonStringExpandAndAppend(p,zIn,N);
206728 }else{
206729 memcpy(p->zBuf+p->nUsed, zIn, N);
206730 p->nUsed += N;
206731 }
206732}
206733static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
206734 assert( N>0 )((void) (0));
206735 if( N+p->nUsed >= p->nAlloc ){
206736 jsonStringExpandAndAppend(p,zIn,N);
206737 }else{
206738 memcpy(p->zBuf+p->nUsed, zIn, N);
206739 p->nUsed += N;
206740 }
206741}
206742
206743/* Append formatted text (not to exceed N bytes) to the JsonString.
206744*/
206745static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
206746 va_list ap;
206747 if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return;
206748 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
206749 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
206750 va_end(ap)__builtin_va_end(ap);
206751 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
206752}
206753
206754/* Append a single character
206755*/
206756static SQLITE_NOINLINE__attribute__((noinline)) void jsonAppendCharExpand(JsonString *p, char c){
206757 if( jsonStringGrow(p,1) ) return;
206758 p->zBuf[p->nUsed++] = c;
206759}
206760static void jsonAppendChar(JsonString *p, char c){
206761 if( p->nUsed>=p->nAlloc ){
206762 jsonAppendCharExpand(p,c);
206763 }else{
206764 p->zBuf[p->nUsed++] = c;
206765 }
206766}
206767
206768/* Remove a single character from the end of the string
206769*/
206770static void jsonStringTrimOneChar(JsonString *p){
206771 if( p->eErr==0 ){
206772 assert( p->nUsed>0 )((void) (0));
206773 p->nUsed--;
206774 }
206775}
206776
206777
206778/* Make sure there is a zero terminator on p->zBuf[]
206779**
206780** Return true on success. Return false if an OOM prevents this
206781** from happening.
206782*/
206783static int jsonStringTerminate(JsonString *p){
206784 jsonAppendChar(p, 0);
206785 jsonStringTrimOneChar(p);
206786 return p->eErr==0;
206787}
206788
206789/* Append a comma separator to the output buffer, if the previous
206790** character is not '[' or '{'.
206791*/
206792static void jsonAppendSeparator(JsonString *p){
206793 char c;
206794 if( p->nUsed==0 ) return;
206795 c = p->zBuf[p->nUsed-1];
206796 if( c=='[' || c=='{' ) return;
206797 jsonAppendChar(p, ',');
206798}
206799
206800/* c is a control character. Append the canonical JSON representation
206801** of that control character to p.
206802**
206803** This routine assumes that the output buffer has already been enlarged
206804** sufficiently to hold the worst-case encoding plus a nul terminator.
206805*/
206806static void jsonAppendControlChar(JsonString *p, u8 c){
206807 static const char aSpecial[] = {
206808 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
206809 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
206810 };
206811 assert( sizeof(aSpecial)==32 )((void) (0));
206812 assert( aSpecial['\b']=='b' )((void) (0));
206813 assert( aSpecial['\f']=='f' )((void) (0));
206814 assert( aSpecial['\n']=='n' )((void) (0));
206815 assert( aSpecial['\r']=='r' )((void) (0));
206816 assert( aSpecial['\t']=='t' )((void) (0));
206817 assert( c>=0 && c<sizeof(aSpecial) )((void) (0));
206818 assert( p->nUsed+7 <= p->nAlloc )((void) (0));
206819 if( aSpecial[c] ){
206820 p->zBuf[p->nUsed] = '\\';
206821 p->zBuf[p->nUsed+1] = aSpecial[c];
206822 p->nUsed += 2;
206823 }else{
206824 p->zBuf[p->nUsed] = '\\';
206825 p->zBuf[p->nUsed+1] = 'u';
206826 p->zBuf[p->nUsed+2] = '0';
206827 p->zBuf[p->nUsed+3] = '0';
206828 p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4];
206829 p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf];
206830 p->nUsed += 6;
206831 }
206832}
206833
206834/* Append the N-byte string in zIn to the end of the JsonString string
206835** under construction. Enclose the string in double-quotes ("...") and
206836** escape any double-quotes or backslash characters contained within the
206837** string.
206838**
206839** This routine is a high-runner. There is a measurable performance
206840** increase associated with unwinding the jsonIsOk[] loop.
206841*/
206842static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
206843 u32 k;
206844 u8 c;
206845 const u8 *z = (const u8*)zIn;
206846 if( z==0 ) return;
206847 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
206848 p->zBuf[p->nUsed++] = '"';
206849 while( 1 /*exit-by-break*/ ){
206850 k = 0;
206851 /* The following while() is the 4-way unwound equivalent of
206852 **
206853 ** while( k<N && jsonIsOk[z[k]] ){ k++; }
206854 */
206855 while( 1 /* Exit by break */ ){
206856 if( k+3>=N ){
206857 while( k<N && jsonIsOk[z[k]] ){ k++; }
206858 break;
206859 }
206860 if( !jsonIsOk[z[k]] ){
206861 break;
206862 }
206863 if( !jsonIsOk[z[k+1]] ){
206864 k += 1;
206865 break;
206866 }
206867 if( !jsonIsOk[z[k+2]] ){
206868 k += 2;
206869 break;
206870 }
206871 if( !jsonIsOk[z[k+3]] ){
206872 k += 3;
206873 break;
206874 }else{
206875 k += 4;
206876 }
206877 }
206878 if( k>=N ){
206879 if( k>0 ){
206880 memcpy(&p->zBuf[p->nUsed], z, k);
206881 p->nUsed += k;
206882 }
206883 break;
206884 }
206885 if( k>0 ){
206886 memcpy(&p->zBuf[p->nUsed], z, k);
206887 p->nUsed += k;
206888 z += k;
206889 N -= k;
206890 }
206891 c = z[0];
206892 if( c=='"' || c=='\\' ){
206893 if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return;
206894 p->zBuf[p->nUsed++] = '\\';
206895 p->zBuf[p->nUsed++] = c;
206896 }else if( c=='\'' ){
206897 p->zBuf[p->nUsed++] = c;
206898 }else{
206899 if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return;
206900 jsonAppendControlChar(p, c);
206901 }
206902 z++;
206903 N--;
206904 }
206905 p->zBuf[p->nUsed++] = '"';
206906 assert( p->nUsed<p->nAlloc )((void) (0));
206907}
206908
206909/*
206910** Append an sqlite3_value (such as a function parameter) to the JSON
206911** string under construction in p.
206912*/
206913static void jsonAppendSqlValue(
206914 JsonString *p, /* Append to this JSON string */
206915 sqlite3_value *pValue /* Value to append */
206916){
206917 switch( sqlite3_value_type(pValue) ){
206918 case SQLITE_NULL5: {
206919 jsonAppendRawNZ(p, "null", 4);
206920 break;
206921 }
206922 case SQLITE_FLOAT2: {
206923 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
206924 break;
206925 }
206926 case SQLITE_INTEGER1: {
206927 const char *z = (const char*)sqlite3_value_text(pValue);
206928 u32 n = (u32)sqlite3_value_bytes(pValue);
206929 jsonAppendRaw(p, z, n);
206930 break;
206931 }
206932 case SQLITE_TEXT3: {
206933 const char *z = (const char*)sqlite3_value_text(pValue);
206934 u32 n = (u32)sqlite3_value_bytes(pValue);
206935 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE74 ){
206936 jsonAppendRaw(p, z, n);
206937 }else{
206938 jsonAppendString(p, z, n);
206939 }
206940 break;
206941 }
206942 default: {
206943 if( jsonFuncArgMightBeBinary(pValue) ){
206944 JsonParse px;
206945 memset(&px, 0, sizeof(px));
206946 px.aBlob = (u8*)sqlite3_value_blob(pValue);
206947 px.nBlob = sqlite3_value_bytes(pValue);
206948 jsonTranslateBlobToText(&px, 0, p);
206949 }else if( p->eErr==0 ){
206950 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
206951 p->eErr = JSTRING_ERR0x04;
206952 jsonStringReset(p);
206953 }
206954 break;
206955 }
206956 }
206957}
206958
206959/* Make the text in p (which is probably a generated JSON text string)
206960** the result of the SQL function.
206961**
206962** The JsonString is reset.
206963**
206964** If pParse and ctx are both non-NULL, then the SQL string in p is
206965** loaded into the zJson field of the pParse object as a RCStr and the
206966** pParse is added to the cache.
206967*/
206968static void jsonReturnString(
206969 JsonString *p, /* String to return */
206970 JsonParse *pParse, /* JSONB source or NULL */
206971 sqlite3_context *ctx /* Where to cache */
206972){
206973 assert( (pParse!=0)==(ctx!=0) )((void) (0));
206974 assert( ctx==0 || ctx==p->pCtx )((void) (0));
206975 if( p->eErr==0 ){
206976 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx))((int)(long int)(sqlite3_user_data(p->pCtx)));
206977 if( flags & JSON_BLOB0x08 ){
206978 jsonReturnStringAsBlob(p);
206979 }else if( p->bStatic ){
206980 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
206981 SQLITE_TRANSIENT((sqlite3_destructor_type)-1), SQLITE_UTF81);
206982 }else if( jsonStringTerminate(p) ){
206983 if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
206984 int rc;
206985 pParse->zJson = sqlite3RCStrRef(p->zBuf);
206986 pParse->nJson = p->nUsed;
206987 pParse->bJsonIsRCStr = 1;
206988 rc = jsonCacheInsert(ctx, pParse);
206989 if( rc==SQLITE_NOMEM7 ){
206990 sqlite3_result_error_nomem(ctx);
206991 jsonStringReset(p);
206992 return;
206993 }
206994 }
206995 sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed,
206996 sqlite3RCStrUnref,
206997 SQLITE_UTF81);
206998 }else{
206999 sqlite3_result_error_nomem(p->pCtx);
207000 }
207001 }else if( p->eErr & JSTRING_OOM0x01 ){
207002 sqlite3_result_error_nomem(p->pCtx);
207003 }else if( p->eErr & JSTRING_MALFORMED0x02 ){
207004 sqlite3_result_error(p->pCtx, "malformed JSON", -1);
207005 }
207006 jsonStringReset(p);
207007}
207008
207009/**************************************************************************
207010** Utility routines for dealing with JsonParse objects
207011**************************************************************************/
207012
207013/*
207014** Reclaim all memory allocated by a JsonParse object. But do not
207015** delete the JsonParse object itself.
207016*/
207017static void jsonParseReset(JsonParse *pParse){
207018 assert( pParse->nJPRef<=1 )((void) (0));
207019 if( pParse->bJsonIsRCStr ){
207020 sqlite3RCStrUnref(pParse->zJson);
207021 pParse->zJson = 0;
207022 pParse->nJson = 0;
207023 pParse->bJsonIsRCStr = 0;
207024 }
207025 if( pParse->nBlobAlloc ){
207026 sqlite3DbFree(pParse->db, pParse->aBlob);
207027 pParse->aBlob = 0;
207028 pParse->nBlob = 0;
207029 pParse->nBlobAlloc = 0;
207030 }
207031}
207032
207033/*
207034** Decrement the reference count on the JsonParse object. When the
207035** count reaches zero, free the object.
207036*/
207037static void jsonParseFree(JsonParse *pParse){
207038 if( pParse ){
207039 if( pParse->nJPRef>1 ){
207040 pParse->nJPRef--;
207041 }else{
207042 jsonParseReset(pParse);
207043 sqlite3DbFree(pParse->db, pParse);
207044 }
207045 }
207046}
207047
207048/**************************************************************************
207049** Utility routines for the JSON text parser
207050**************************************************************************/
207051
207052/*
207053** Translate a single byte of Hex into an integer.
207054** This routine only gives a correct answer if h really is a valid hexadecimal
207055** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not
207056** assert() if the digit is not hex.
207057*/
207058static u8 jsonHexToInt(int h){
207059#ifdef SQLITE_ASCII1
207060 h += 9*(1&(h>>6));
207061#endif
207062#ifdef SQLITE_EBCDIC
207063 h += 9*(1&~(h>>4));
207064#endif
207065 return (u8)(h & 0xf);
207066}
207067
207068/*
207069** Convert a 4-byte hex string into an integer
207070*/
207071static u32 jsonHexToInt4(const char *z){
207072 u32 v;
207073 v = (jsonHexToInt(z[0])<<12)
207074 + (jsonHexToInt(z[1])<<8)
207075 + (jsonHexToInt(z[2])<<4)
207076 + jsonHexToInt(z[3]);
207077 return v;
207078}
207079
207080/*
207081** Return true if z[] begins with 2 (or more) hexadecimal digits
207082*/
207083static int jsonIs2Hex(const char *z){
207084 return sqlite3Isxdigit(z[0])(sqlite3CtypeMap[(unsigned char)(z[0])]&0x08) && sqlite3Isxdigit(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x08);
207085}
207086
207087/*
207088** Return true if z[] begins with 4 (or more) hexadecimal digits
207089*/
207090static int jsonIs4Hex(const char *z){
207091 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
207092}
207093
207094/*
207095** Return the number of bytes of JSON5 whitespace at the beginning of
207096** the input string z[].
207097**
207098** JSON5 whitespace consists of any of the following characters:
207099**
207100** Unicode UTF-8 Name
207101** U+0009 09 horizontal tab
207102** U+000a 0a line feed
207103** U+000b 0b vertical tab
207104** U+000c 0c form feed
207105** U+000d 0d carriage return
207106** U+0020 20 space
207107** U+00a0 c2 a0 non-breaking space
207108** U+1680 e1 9a 80 ogham space mark
207109** U+2000 e2 80 80 en quad
207110** U+2001 e2 80 81 em quad
207111** U+2002 e2 80 82 en space
207112** U+2003 e2 80 83 em space
207113** U+2004 e2 80 84 three-per-em space
207114** U+2005 e2 80 85 four-per-em space
207115** U+2006 e2 80 86 six-per-em space
207116** U+2007 e2 80 87 figure space
207117** U+2008 e2 80 88 punctuation space
207118** U+2009 e2 80 89 thin space
207119** U+200a e2 80 8a hair space
207120** U+2028 e2 80 a8 line separator
207121** U+2029 e2 80 a9 paragraph separator
207122** U+202f e2 80 af narrow no-break space (NNBSP)
207123** U+205f e2 81 9f medium mathematical space (MMSP)
207124** U+3000 e3 80 80 ideographical space
207125** U+FEFF ef bb bf byte order mark
207126**
207127** In addition, comments between '/', '*' and '*', '/' and
207128** from '/', '/' to end-of-line are also considered to be whitespace.
207129*/
207130static int json5Whitespace(const char *zIn){
207131 int n = 0;
207132 const u8 *z = (u8*)zIn;
207133 while( 1 /*exit by "goto whitespace_done"*/ ){
207134 switch( z[n] ){
207135 case 0x09:
207136 case 0x0a:
207137 case 0x0b:
207138 case 0x0c:
207139 case 0x0d:
207140 case 0x20: {
207141 n++;
207142 break;
207143 }
207144 case '/': {
207145 if( z[n+1]=='*' && z[n+2]!=0 ){
207146 int j;
207147 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
207148 if( z[j]==0 ) goto whitespace_done;
207149 }
207150 n = j+1;
207151 break;
207152 }else if( z[n+1]=='/' ){
207153 int j;
207154 char c;
207155 for(j=n+2; (c = z[j])!=0; j++){
207156 if( c=='\n' || c=='\r' ) break;
207157 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
207158 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
207159 ){
207160 j += 2;
207161 break;
207162 }
207163 }
207164 n = j;
207165 if( z[n] ) n++;
207166 break;
207167 }
207168 goto whitespace_done;
207169 }
207170 case 0xc2: {
207171 if( z[n+1]==0xa0 ){
207172 n += 2;
207173 break;
207174 }
207175 goto whitespace_done;
207176 }
207177 case 0xe1: {
207178 if( z[n+1]==0x9a && z[n+2]==0x80 ){
207179 n += 3;
207180 break;
207181 }
207182 goto whitespace_done;
207183 }
207184 case 0xe2: {
207185 if( z[n+1]==0x80 ){
207186 u8 c = z[n+2];
207187 if( c<0x80 ) goto whitespace_done;
207188 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
207189 n += 3;
207190 break;
207191 }
207192 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
207193 n += 3;
207194 break;
207195 }
207196 goto whitespace_done;
207197 }
207198 case 0xe3: {
207199 if( z[n+1]==0x80 && z[n+2]==0x80 ){
207200 n += 3;
207201 break;
207202 }
207203 goto whitespace_done;
207204 }
207205 case 0xef: {
207206 if( z[n+1]==0xbb && z[n+2]==0xbf ){
207207 n += 3;
207208 break;
207209 }
207210 goto whitespace_done;
207211 }
207212 default: {
207213 goto whitespace_done;
207214 }
207215 }
207216 }
207217 whitespace_done:
207218 return n;
207219}
207220
207221/*
207222** Extra floating-point literals to allow in JSON.
207223*/
207224static const struct NanInfName {
207225 char c1;
207226 char c2;
207227 char n;
207228 char eType;
207229 char nRepl;
207230 char *zMatch;
207231 char *zRepl;
207232} aNanInfName[] = {
207233 { 'i', 'I', 3, JSONB_FLOAT5, 7, "inf", "9.0e999" },
207234 { 'i', 'I', 8, JSONB_FLOAT5, 7, "infinity", "9.0e999" },
207235 { 'n', 'N', 3, JSONB_NULL0, 4, "NaN", "null" },
207236 { 'q', 'Q', 4, JSONB_NULL0, 4, "QNaN", "null" },
207237 { 's', 'S', 4, JSONB_NULL0, 4, "SNaN", "null" },
207238};
207239
207240
207241/*
207242** Report the wrong number of arguments for json_insert(), json_replace()
207243** or json_set().
207244*/
207245static void jsonWrongNumArgs(
207246 sqlite3_context *pCtx,
207247 const char *zFuncName
207248){
207249 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
207250 zFuncName);
207251 sqlite3_result_error(pCtx, zMsg, -1);
207252 sqlite3_free(zMsg);
207253}
207254
207255/****************************************************************************
207256** Utility routines for dealing with the binary BLOB representation of JSON
207257****************************************************************************/
207258
207259/*
207260** Expand pParse->aBlob so that it holds at least N bytes.
207261**
207262** Return the number of errors.
207263*/
207264static int jsonBlobExpand(JsonParse *pParse, u32 N){
207265 u8 *aNew;
207266 u32 t;
207267 assert( N>pParse->nBlobAlloc )((void) (0));
207268 if( pParse->nBlobAlloc==0 ){
207269 t = 100;
207270 }else{
207271 t = pParse->nBlobAlloc*2;
207272 }
207273 if( t<N ) t = N+100;
207274 aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t);
207275 if( aNew==0 ){ pParse->oom = 1; return 1; }
207276 pParse->aBlob = aNew;
207277 pParse->nBlobAlloc = t;
207278 return 0;
207279}
207280
207281/*
207282** If pParse->aBlob is not previously editable (because it is taken
207283** from sqlite3_value_blob(), as indicated by the fact that
207284** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
207285** by making a copy into space obtained from malloc.
207286**
207287** Return true on success. Return false on OOM.
207288*/
207289static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
207290 u8 *aOld;
207291 u32 nSize;
207292 assert( !pParse->bReadOnly )((void) (0));
207293 if( pParse->oom ) return 0;
207294 if( pParse->nBlobAlloc>0 ) return 1;
207295 aOld = pParse->aBlob;
207296 nSize = pParse->nBlob + nExtra;
207297 pParse->aBlob = 0;
207298 if( jsonBlobExpand(pParse, nSize) ){
207299 return 0;
207300 }
207301 assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra )((void) (0));
207302 memcpy(pParse->aBlob, aOld, pParse->nBlob);
207303 return 1;
207304}
207305
207306/* Expand pParse->aBlob and append one bytes.
207307*/
207308static SQLITE_NOINLINE__attribute__((noinline)) void jsonBlobExpandAndAppendOneByte(
207309 JsonParse *pParse,
207310 u8 c
207311){
207312 jsonBlobExpand(pParse, pParse->nBlob+1);
207313 if( pParse->oom==0 ){
207314 assert( pParse->nBlob+1<=pParse->nBlobAlloc )((void) (0));
207315 pParse->aBlob[pParse->nBlob++] = c;
207316 }
207317}
207318
207319/* Append a single character.
207320*/
207321static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
207322 if( pParse->nBlob >= pParse->nBlobAlloc ){
207323 jsonBlobExpandAndAppendOneByte(pParse, c);
207324 }else{
207325 pParse->aBlob[pParse->nBlob++] = c;
207326 }
207327}
207328
207329/* Slow version of jsonBlobAppendNode() that first resizes the
207330** pParse->aBlob structure.
207331*/
207332static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*);
207333static SQLITE_NOINLINE__attribute__((noinline)) void jsonBlobExpandAndAppendNode(
207334 JsonParse *pParse,
207335 u8 eType,
207336 u32 szPayload,
207337 const void *aPayload
207338){
207339 if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return;
207340 jsonBlobAppendNode(pParse, eType, szPayload, aPayload);
207341}
207342
207343
207344/* Append an node type byte together with the payload size and
207345** possibly also the payload.
207346**
207347** If aPayload is not NULL, then it is a pointer to the payload which
207348** is also appended. If aPayload is NULL, the pParse->aBlob[] array
207349** is resized (if necessary) so that it is big enough to hold the
207350** payload, but the payload is not appended and pParse->nBlob is left
207351** pointing to where the first byte of payload will eventually be.
207352*/
207353static void jsonBlobAppendNode(
207354 JsonParse *pParse, /* The JsonParse object under construction */
207355 u8 eType, /* Node type. One of JSONB_* */
207356 u32 szPayload, /* Number of bytes of payload */
207357 const void *aPayload /* The payload. Might be NULL */
207358){
207359 u8 *a;
207360 if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){
207361 jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload);
207362 return;
207363 }
207364 assert( pParse->aBlob!=0 )((void) (0));
207365 a = &pParse->aBlob[pParse->nBlob];
207366 if( szPayload<=11 ){
207367 a[0] = eType | (szPayload<<4);
207368 pParse->nBlob += 1;
207369 }else if( szPayload<=0xff ){
207370 a[0] = eType | 0xc0;
207371 a[1] = szPayload & 0xff;
207372 pParse->nBlob += 2;
207373 }else if( szPayload<=0xffff ){
207374 a[0] = eType | 0xd0;
207375 a[1] = (szPayload >> 8) & 0xff;
207376 a[2] = szPayload & 0xff;
207377 pParse->nBlob += 3;
207378 }else{
207379 a[0] = eType | 0xe0;
207380 a[1] = (szPayload >> 24) & 0xff;
207381 a[2] = (szPayload >> 16) & 0xff;
207382 a[3] = (szPayload >> 8) & 0xff;
207383 a[4] = szPayload & 0xff;
207384 pParse->nBlob += 5;
207385 }
207386 if( aPayload ){
207387 pParse->nBlob += szPayload;
207388 memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload);
207389 }
207390}
207391
207392/* Change the payload size for the node at index i to be szPayload.
207393*/
207394static int jsonBlobChangePayloadSize(
207395 JsonParse *pParse,
207396 u32 i,
207397 u32 szPayload
207398){
207399 u8 *a;
207400 u8 szType;
207401 u8 nExtra;
207402 u8 nNeeded;
207403 int delta;
207404 if( pParse->oom ) return 0;
207405 a = &pParse->aBlob[i];
207406 szType = a[0]>>4;
207407 if( szType<=11 ){
207408 nExtra = 0;
207409 }else if( szType==12 ){
207410 nExtra = 1;
207411 }else if( szType==13 ){
207412 nExtra = 2;
207413 }else{
207414 nExtra = 4;
207415 }
207416 if( szPayload<=11 ){
207417 nNeeded = 0;
207418 }else if( szPayload<=0xff ){
207419 nNeeded = 1;
207420 }else if( szPayload<=0xffff ){
207421 nNeeded = 2;
207422 }else{
207423 nNeeded = 4;
207424 }
207425 delta = nNeeded - nExtra;
207426 if( delta ){
207427 u32 newSize = pParse->nBlob + delta;
207428 if( delta>0 ){
207429 if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){
207430 return 0; /* OOM error. Error state recorded in pParse->oom. */
207431 }
207432 a = &pParse->aBlob[i];
207433 memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1));
207434 }else{
207435 memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta));
207436 }
207437 pParse->nBlob = newSize;
207438 }
207439 if( nNeeded==0 ){
207440 a[0] = (a[0] & 0x0f) | (szPayload<<4);
207441 }else if( nNeeded==1 ){
207442 a[0] = (a[0] & 0x0f) | 0xc0;
207443 a[1] = szPayload & 0xff;
207444 }else if( nNeeded==2 ){
207445 a[0] = (a[0] & 0x0f) | 0xd0;
207446 a[1] = (szPayload >> 8) & 0xff;
207447 a[2] = szPayload & 0xff;
207448 }else{
207449 a[0] = (a[0] & 0x0f) | 0xe0;
207450 a[1] = (szPayload >> 24) & 0xff;
207451 a[2] = (szPayload >> 16) & 0xff;
207452 a[3] = (szPayload >> 8) & 0xff;
207453 a[4] = szPayload & 0xff;
207454 }
207455 return delta;
207456}
207457
207458/*
207459** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
207460** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
207461** any changes to *pOp and return false.
207462*/
207463static int jsonIs4HexB(const char *z, int *pOp){
207464 if( z[0]!='u' ) return 0;
207465 if( !jsonIs4Hex(&z[1]) ) return 0;
207466 *pOp = JSONB_TEXTJ8;
207467 return 1;
207468}
207469
207470/*
207471** Check a single element of the JSONB in pParse for validity.
207472**
207473** The element to be checked starts at offset i and must end at on the
207474** last byte before iEnd.
207475**
207476** Return 0 if everything is correct. Return the 1-based byte offset of the
207477** error if a problem is detected. (In other words, if the error is at offset
207478** 0, return 1).
207479*/
207480static u32 jsonbValidityCheck(
207481 const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */
207482 u32 i, /* Start of element as pParse->aBlob[i] */
207483 u32 iEnd, /* One more than the last byte of the element */
207484 u32 iDepth /* Current nesting depth */
207485){
207486 u32 n, sz, j, k;
207487 const u8 *z;
207488 u8 x;
207489 if( iDepth>JSON_MAX_DEPTH1000 ) return i+1;
207490 sz = 0;
207491 n = jsonbPayloadSize(pParse, i, &sz);
207492 if( NEVER(n==0)(n==0) ) return i+1; /* Checked by caller */
207493 if( NEVER(i+n+sz!=iEnd)(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */
207494 z = pParse->aBlob;
207495 x = z[i] & 0x0f;
207496 switch( x ){
207497 case JSONB_NULL0:
207498 case JSONB_TRUE1:
207499 case JSONB_FALSE2: {
207500 return n+sz==1 ? 0 : i+1;
207501 }
207502 case JSONB_INT3: {
207503 if( sz<1 ) return i+1;
207504 j = i+n;
207505 if( z[j]=='-' ){
207506 j++;
207507 if( sz<2 ) return i+1;
207508 }
207509 k = i+n+sz;
207510 while( j<k ){
207511 if( sqlite3Isdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x04) ){
207512 j++;
207513 }else{
207514 return j+1;
207515 }
207516 }
207517 return 0;
207518 }
207519 case JSONB_INT54: {
207520 if( sz<3 ) return i+1;
207521 j = i+n;
207522 if( z[j]=='-' ){
207523 if( sz<4 ) return i+1;
207524 j++;
207525 }
207526 if( z[j]!='0' ) return i+1;
207527 if( z[j+1]!='x' && z[j+1]!='X' ) return j+2;
207528 j += 2;
207529 k = i+n+sz;
207530 while( j<k ){
207531 if( sqlite3Isxdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x08) ){
207532 j++;
207533 }else{
207534 return j+1;
207535 }
207536 }
207537 return 0;
207538 }
207539 case JSONB_FLOAT5:
207540 case JSONB_FLOAT56: {
207541 u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */
207542 if( sz<2 ) return i+1;
207543 j = i+n;
207544 k = j+sz;
207545 if( z[j]=='-' ){
207546 j++;
207547 if( sz<3 ) return i+1;
207548 }
207549 if( z[j]=='.' ){
207550 if( x==JSONB_FLOAT5 ) return j+1;
207551 if( !sqlite3Isdigit(z[j+1])(sqlite3CtypeMap[(unsigned char)(z[j+1])]&0x04) ) return j+1;
207552 j += 2;
207553 seen = 1;
207554 }else if( z[j]=='0' && x==JSONB_FLOAT5 ){
207555 if( j+3>k ) return j+1;
207556 if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1;
207557 j++;
207558 }
207559 for(; j<k; j++){
207560 if( sqlite3Isdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x04) ) continue;
207561 if( z[j]=='.' ){
207562 if( seen>0 ) return j+1;
207563 if( x==JSONB_FLOAT5 && (j==k-1 || !sqlite3Isdigit(z[j+1])(sqlite3CtypeMap[(unsigned char)(z[j+1])]&0x04)) ){
207564 return j+1;
207565 }
207566 seen = 1;
207567 continue;
207568 }
207569 if( z[j]=='e' || z[j]=='E' ){
207570 if( seen==2 ) return j+1;
207571 if( j==k-1 ) return j+1;
207572 if( z[j+1]=='+' || z[j+1]=='-' ){
207573 j++;
207574 if( j==k-1 ) return j+1;
207575 }
207576 seen = 2;
207577 continue;
207578 }
207579 return j+1;
207580 }
207581 if( seen==0 ) return i+1;
207582 return 0;
207583 }
207584 case JSONB_TEXT7: {
207585 j = i+n;
207586 k = j+sz;
207587 while( j<k ){
207588 if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1;
207589 j++;
207590 }
207591 return 0;
207592 }
207593 case JSONB_TEXTJ8:
207594 case JSONB_TEXT59: {
207595 j = i+n;
207596 k = j+sz;
207597 while( j<k ){
207598 if( !jsonIsOk[z[j]] && z[j]!='\'' ){
207599 if( z[j]=='"' ){
207600 if( x==JSONB_TEXTJ8 ) return j+1;
207601 }else if( z[j]<=0x1f ){
207602 /* Control characters in JSON5 string literals are ok */
207603 if( x==JSONB_TEXTJ8 ) return j+1;
207604 }else if( NEVER(z[j]!='\\')(z[j]!='\\') || j+1>=k ){
207605 return j+1;
207606 }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){
207607 j++;
207608 }else if( z[j+1]=='u' ){
207609 if( j+5>=k ) return j+1;
207610 if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1;
207611 j++;
207612 }else if( x!=JSONB_TEXT59 ){
207613 return j+1;
207614 }else{
207615 u32 c = 0;
207616 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
207617 if( c==JSON_INVALID_CHAR0x99999 ) return j+1;
207618 j += szC - 1;
207619 }
207620 }
207621 j++;
207622 }
207623 return 0;
207624 }
207625 case JSONB_TEXTRAW10: {
207626 return 0;
207627 }
207628 case JSONB_ARRAY11: {
207629 u32 sub;
207630 j = i+n;
207631 k = j+sz;
207632 while( j<k ){
207633 sz = 0;
207634 n = jsonbPayloadSize(pParse, j, &sz);
207635 if( n==0 ) return j+1;
207636 if( j+n+sz>k ) return j+1;
207637 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
207638 if( sub ) return sub;
207639 j += n + sz;
207640 }
207641 assert( j==k )((void) (0));
207642 return 0;
207643 }
207644 case JSONB_OBJECT12: {
207645 u32 cnt = 0;
207646 u32 sub;
207647 j = i+n;
207648 k = j+sz;
207649 while( j<k ){
207650 sz = 0;
207651 n = jsonbPayloadSize(pParse, j, &sz);
207652 if( n==0 ) return j+1;
207653 if( j+n+sz>k ) return j+1;
207654 if( (cnt & 1)==0 ){
207655 x = z[j] & 0x0f;
207656 if( x<JSONB_TEXT7 || x>JSONB_TEXTRAW10 ) return j+1;
207657 }
207658 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
207659 if( sub ) return sub;
207660 cnt++;
207661 j += n + sz;
207662 }
207663 assert( j==k )((void) (0));
207664 if( (cnt & 1)!=0 ) return j+1;
207665 return 0;
207666 }
207667 default: {
207668 return i+1;
207669 }
207670 }
207671}
207672
207673/*
207674** Translate a single element of JSON text at pParse->zJson[i] into
207675** its equivalent binary JSONB representation. Append the translation into
207676** pParse->aBlob[] beginning at pParse->nBlob. The size of
207677** pParse->aBlob[] is increased as necessary.
207678**
207679** Return the index of the first character past the end of the element parsed,
207680** or one of the following special result codes:
207681**
207682** 0 End of input
207683** -1 Syntax error or OOM
207684** -2 '}' seen \
207685** -3 ']' seen \___ For these returns, pParse->iErr is set to
207686** -4 ',' seen / the index in zJson[] of the seen character
207687** -5 ':' seen /
207688*/
207689static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
207690 char c;
207691 u32 j;
207692 u32 iThis, iStart;
207693 int x;
207694 u8 t;
207695 const char *z = pParse->zJson;
207696json_parse_restart:
207697 switch( (u8)z[i] ){
207698 case '{': {
207699 /* Parse object */
207700 iThis = pParse->nBlob;
207701 jsonBlobAppendNode(pParse, JSONB_OBJECT12, pParse->nJson-i, 0);
207702 if( ++pParse->iDepth > JSON_MAX_DEPTH1000 ){
207703 pParse->iErr = i;
207704 return -1;
207705 }
207706 iStart = pParse->nBlob;
207707 for(j=i+1;;j++){
207708 u32 iBlob = pParse->nBlob;
207709 x = jsonTranslateTextToBlob(pParse, j);
207710 if( x<=0 ){
207711 int op;
207712 if( x==(-2) ){
207713 j = pParse->iErr;
207714 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
207715 break;
207716 }
207717 j += json5Whitespace(&z[j]);
207718 op = JSONB_TEXT7;
207719 if( sqlite3JsonId1(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x42)
207720 || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
207721 ){
207722 int k = j+1;
207723 while( (sqlite3JsonId2(z[k])(sqlite3CtypeMap[(unsigned char)(z[k])]&0x46) && json5Whitespace(&z[k])==0)
207724 || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
207725 ){
207726 k++;
207727 }
207728 assert( iBlob==pParse->nBlob )((void) (0));
207729 jsonBlobAppendNode(pParse, op, k-j, &z[j]);
207730 pParse->hasNonstd = 1;
207731 x = k;
207732 }else{
207733 if( x!=-1 ) pParse->iErr = j;
207734 return -1;
207735 }
207736 }
207737 if( pParse->oom ) return -1;
207738 t = pParse->aBlob[iBlob] & 0x0f;
207739 if( t<JSONB_TEXT7 || t>JSONB_TEXTRAW10 ){
207740 pParse->iErr = j;
207741 return -1;
207742 }
207743 j = x;
207744 if( z[j]==':' ){
207745 j++;
207746 }else{
207747 if( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){
207748 /* strspn() is not helpful here */
207749 do{ j++; }while( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) );
207750 if( z[j]==':' ){
207751 j++;
207752 goto parse_object_value;
207753 }
207754 }
207755 x = jsonTranslateTextToBlob(pParse, j);
207756 if( x!=(-5) ){
207757 if( x!=(-1) ) pParse->iErr = j;
207758 return -1;
207759 }
207760 j = pParse->iErr+1;
207761 }
207762 parse_object_value:
207763 x = jsonTranslateTextToBlob(pParse, j);
207764 if( x<=0 ){
207765 if( x!=(-1) ) pParse->iErr = j;
207766 return -1;
207767 }
207768 j = x;
207769 if( z[j]==',' ){
207770 continue;
207771 }else if( z[j]=='}' ){
207772 break;
207773 }else{
207774 if( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){
207775 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
207776 if( z[j]==',' ){
207777 continue;
207778 }else if( z[j]=='}' ){
207779 break;
207780 }
207781 }
207782 x = jsonTranslateTextToBlob(pParse, j);
207783 if( x==(-4) ){
207784 j = pParse->iErr;
207785 continue;
207786 }
207787 if( x==(-2) ){
207788 j = pParse->iErr;
207789 break;
207790 }
207791 }
207792 pParse->iErr = j;
207793 return -1;
207794 }
207795 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
207796 pParse->iDepth--;
207797 return j+1;
207798 }
207799 case '[': {
207800 /* Parse array */
207801 iThis = pParse->nBlob;
207802 assert( i<=(u32)pParse->nJson )((void) (0));
207803 jsonBlobAppendNode(pParse, JSONB_ARRAY11, pParse->nJson - i, 0);
207804 iStart = pParse->nBlob;
207805 if( pParse->oom ) return -1;
207806 if( ++pParse->iDepth > JSON_MAX_DEPTH1000 ){
207807 pParse->iErr = i;
207808 return -1;
207809 }
207810 for(j=i+1;;j++){
207811 x = jsonTranslateTextToBlob(pParse, j);
207812 if( x<=0 ){
207813 if( x==(-3) ){
207814 j = pParse->iErr;
207815 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
207816 break;
207817 }
207818 if( x!=(-1) ) pParse->iErr = j;
207819 return -1;
207820 }
207821 j = x;
207822 if( z[j]==',' ){
207823 continue;
207824 }else if( z[j]==']' ){
207825 break;
207826 }else{
207827 if( jsonIsspace(z[j])(jsonIsSpace[(unsigned char)z[j]]) ){
207828 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
207829 if( z[j]==',' ){
207830 continue;
207831 }else if( z[j]==']' ){
207832 break;
207833 }
207834 }
207835 x = jsonTranslateTextToBlob(pParse, j);
207836 if( x==(-4) ){
207837 j = pParse->iErr;
207838 continue;
207839 }
207840 if( x==(-3) ){
207841 j = pParse->iErr;
207842 break;
207843 }
207844 }
207845 pParse->iErr = j;
207846 return -1;
207847 }
207848 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
207849 pParse->iDepth--;
207850 return j+1;
207851 }
207852 case '\'': {
207853 u8 opcode;
207854 char cDelim;
207855 pParse->hasNonstd = 1;
207856 opcode = JSONB_TEXT7;
207857 goto parse_string;
207858 case '"':
207859 /* Parse string */
207860 opcode = JSONB_TEXT7;
207861 parse_string:
207862 cDelim = z[i];
207863 j = i+1;
207864 while( 1 /*exit-by-break*/ ){
207865 if( jsonIsOk[(u8)z[j]] ){
207866 if( !jsonIsOk[(u8)z[j+1]] ){
207867 j += 1;
207868 }else if( !jsonIsOk[(u8)z[j+2]] ){
207869 j += 2;
207870 }else{
207871 j += 3;
207872 continue;
207873 }
207874 }
207875 c = z[j];
207876 if( c==cDelim ){
207877 break;
207878 }else if( c=='\\' ){
207879 c = z[++j];
207880 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
207881 || c=='n' || c=='r' || c=='t'
207882 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
207883 if( opcode==JSONB_TEXT7 ) opcode = JSONB_TEXTJ8;
207884 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
207885 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
207886 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
207887 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
207888 opcode = JSONB_TEXT59;
207889 pParse->hasNonstd = 1;
207890 }else if( c=='\r' ){
207891 if( z[j+1]=='\n' ) j++;
207892 opcode = JSONB_TEXT59;
207893 pParse->hasNonstd = 1;
207894 }else{
207895 pParse->iErr = j;
207896 return -1;
207897 }
207898 }else if( c<=0x1f ){
207899 if( c==0 ){
207900 pParse->iErr = j;
207901 return -1;
207902 }
207903 /* Control characters are not allowed in canonical JSON string
207904 ** literals, but are allowed in JSON5 string literals. */
207905 opcode = JSONB_TEXT59;
207906 pParse->hasNonstd = 1;
207907 }else if( c=='"' ){
207908 opcode = JSONB_TEXT59;
207909 }
207910 j++;
207911 }
207912 jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]);
207913 return j+1;
207914 }
207915 case 't': {
207916 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4])(sqlite3CtypeMap[(unsigned char)(z[i+4])]&0x06) ){
207917 jsonBlobAppendOneByte(pParse, JSONB_TRUE1);
207918 return i+4;
207919 }
207920 pParse->iErr = i;
207921 return -1;
207922 }
207923 case 'f': {
207924 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5])(sqlite3CtypeMap[(unsigned char)(z[i+5])]&0x06) ){
207925 jsonBlobAppendOneByte(pParse, JSONB_FALSE2);
207926 return i+5;
207927 }
207928 pParse->iErr = i;
207929 return -1;
207930 }
207931 case '+': {
207932 u8 seenE;
207933 pParse->hasNonstd = 1;
207934 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
207935 goto parse_number;
207936 case '.':
207937 if( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04) ){
207938 pParse->hasNonstd = 1;
207939 t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
207940 seenE = 0;
207941 goto parse_number_2;
207942 }
207943 pParse->iErr = i;
207944 return -1;
207945 case '-':
207946 case '0':
207947 case '1':
207948 case '2':
207949 case '3':
207950 case '4':
207951 case '5':
207952 case '6':
207953 case '7':
207954 case '8':
207955 case '9':
207956 /* Parse number */
207957 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
207958 parse_number:
207959 seenE = 0;
207960 assert( '-' < '0' )((void) (0));
207961 assert( '+' < '0' )((void) (0));
207962 assert( '.' < '0' )((void) (0));
207963 c = z[i];
207964
207965 if( c<='0' ){
207966 if( c=='0' ){
207967 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x08) ){
207968 assert( t==0x00 )((void) (0));
207969 pParse->hasNonstd = 1;
207970 t = 0x01;
207971 for(j=i+3; sqlite3Isxdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x08); j++){}
207972 goto parse_number_finish;
207973 }else if( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04) ){
207974 pParse->iErr = i+1;
207975 return -1;
207976 }
207977 }else{
207978 if( !sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04) ){
207979 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
207980 ** that case. SQLite also allows these in any case and it allows
207981 ** "+inf" and "-inf". */
207982 if( (z[i+1]=='I' || z[i+1]=='i')
207983 && sqlite3StrNICmpsqlite3_strnicmp(&z[i+1], "inf",3)==0
207984 ){
207985 pParse->hasNonstd = 1;
207986 if( z[i]=='-' ){
207987 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 6, "-9e999");
207988 }else{
207989 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 5, "9e999");
207990 }
207991 return i + (sqlite3StrNICmpsqlite3_strnicmp(&z[i+4],"inity",5)==0 ? 9 : 4);
207992 }
207993 if( z[i+1]=='.' ){
207994 pParse->hasNonstd = 1;
207995 t |= 0x01;
207996 goto parse_number_2;
207997 }
207998 pParse->iErr = i;
207999 return -1;
208000 }
208001 if( z[i+1]=='0' ){
208002 if( sqlite3Isdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x04) ){
208003 pParse->iErr = i+1;
208004 return -1;
208005 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3])(sqlite3CtypeMap[(unsigned char)(z[i+3])]&0x08) ){
208006 pParse->hasNonstd = 1;
208007 t |= 0x01;
208008 for(j=i+4; sqlite3Isxdigit(z[j])(sqlite3CtypeMap[(unsigned char)(z[j])]&0x08); j++){}
208009 goto parse_number_finish;
208010 }
208011 }
208012 }
208013 }
208014
208015 parse_number_2:
208016 for(j=i+1;; j++){
208017 c = z[j];
208018 if( sqlite3Isdigit(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x04) ) continue;
208019 if( c=='.' ){
208020 if( (t & 0x02)!=0 ){
208021 pParse->iErr = j;
208022 return -1;
208023 }
208024 t |= 0x02;
208025 continue;
208026 }
208027 if( c=='e' || c=='E' ){
208028 if( z[j-1]<'0' ){
208029 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) ){
208030 pParse->hasNonstd = 1;
208031 t |= 0x01;
208032 }else{
208033 pParse->iErr = j;
208034 return -1;
208035 }
208036 }
208037 if( seenE ){
208038 pParse->iErr = j;
208039 return -1;
208040 }
208041 t |= 0x02;
208042 seenE = 1;
208043 c = z[j+1];
208044 if( c=='+' || c=='-' ){
208045 j++;
208046 c = z[j+1];
208047 }
208048 if( c<'0' || c>'9' ){
208049 pParse->iErr = j;
208050 return -1;
208051 }
208052 continue;
208053 }
208054 break;
208055 }
208056 if( z[j-1]<'0' ){
208057 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) ){
208058 pParse->hasNonstd = 1;
208059 t |= 0x01;
208060 }else{
208061 pParse->iErr = j;
208062 return -1;
208063 }
208064 }
208065 parse_number_finish:
208066 assert( JSONB_INT+0x01==JSONB_INT5 )((void) (0));
208067 assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 )((void) (0));
208068 assert( JSONB_INT+0x02==JSONB_FLOAT )((void) (0));
208069 if( z[i]=='+' ) i++;
208070 jsonBlobAppendNode(pParse, JSONB_INT3+t, j-i, &z[i]);
208071 return j;
208072 }
208073 case '}': {
208074 pParse->iErr = i;
208075 return -2; /* End of {...} */
208076 }
208077 case ']': {
208078 pParse->iErr = i;
208079 return -3; /* End of [...] */
208080 }
208081 case ',': {
208082 pParse->iErr = i;
208083 return -4; /* List separator */
208084 }
208085 case ':': {
208086 pParse->iErr = i;
208087 return -5; /* Object label/value separator */
208088 }
208089 case 0: {
208090 return 0; /* End of file */
208091 }
208092 case 0x09:
208093 case 0x0a:
208094 case 0x0d:
208095 case 0x20: {
208096 i += 1 + (u32)strspn(&z[i+1], jsonSpaces);
208097 goto json_parse_restart;
208098 }
208099 case 0x0b:
208100 case 0x0c:
208101 case '/':
208102 case 0xc2:
208103 case 0xe1:
208104 case 0xe2:
208105 case 0xe3:
208106 case 0xef: {
208107 j = json5Whitespace(&z[i]);
208108 if( j>0 ){
208109 i += j;
208110 pParse->hasNonstd = 1;
208111 goto json_parse_restart;
208112 }
208113 pParse->iErr = i;
208114 return -1;
208115 }
208116 case 'n': {
208117 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4])(sqlite3CtypeMap[(unsigned char)(z[i+4])]&0x06) ){
208118 jsonBlobAppendOneByte(pParse, JSONB_NULL0);
208119 return i+4;
208120 }
208121 /* fall-through into the default case that checks for NaN */
208122 /* no break */ deliberate_fall_through__attribute__((fallthrough));
208123 }
208124 default: {
208125 u32 k;
208126 int nn;
208127 c = z[i];
208128 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
208129 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
208130 nn = aNanInfName[k].n;
208131 if( sqlite3StrNICmpsqlite3_strnicmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
208132 continue;
208133 }
208134 if( sqlite3Isalnum(z[i+nn])(sqlite3CtypeMap[(unsigned char)(z[i+nn])]&0x06) ) continue;
208135 if( aNanInfName[k].eType==JSONB_FLOAT5 ){
208136 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 5, "9e999");
208137 }else{
208138 jsonBlobAppendOneByte(pParse, JSONB_NULL0);
208139 }
208140 pParse->hasNonstd = 1;
208141 return i + nn;
208142 }
208143 pParse->iErr = i;
208144 return -1; /* Syntax error */
208145 }
208146 } /* End switch(z[i]) */
208147}
208148
208149
208150/*
208151** Parse a complete JSON string. Return 0 on success or non-zero if there
208152** are any errors. If an error occurs, free all memory held by pParse,
208153** but not pParse itself.
208154**
208155** pParse must be initialized to an empty parse object prior to calling
208156** this routine.
208157*/
208158static int jsonConvertTextToBlob(
208159 JsonParse *pParse, /* Initialize and fill this JsonParse object */
208160 sqlite3_context *pCtx /* Report errors here */
208161){
208162 int i;
208163 const char *zJson = pParse->zJson;
208164 i = jsonTranslateTextToBlob(pParse, 0);
208165 if( pParse->oom ) i = -1;
208166 if( i>0 ){
208167#ifdef SQLITE_DEBUG
208168 assert( pParse->iDepth==0 )((void) (0));
208169 if( sqlite3Config.bJsonSelfcheck ){
208170 assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 )((void) (0));
208171 }
208172#endif
208173 while( jsonIsspace(zJson[i])(jsonIsSpace[(unsigned char)zJson[i]]) ) i++;
208174 if( zJson[i] ){
208175 i += json5Whitespace(&zJson[i]);
208176 if( zJson[i] ){
208177 if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1);
208178 jsonParseReset(pParse);
208179 return 1;
208180 }
208181 pParse->hasNonstd = 1;
208182 }
208183 }
208184 if( i<=0 ){
208185 if( pCtx!=0 ){
208186 if( pParse->oom ){
208187 sqlite3_result_error_nomem(pCtx);
208188 }else{
208189 sqlite3_result_error(pCtx, "malformed JSON", -1);
208190 }
208191 }
208192 jsonParseReset(pParse);
208193 return 1;
208194 }
208195 return 0;
208196}
208197
208198/*
208199** The input string pStr is a well-formed JSON text string. Convert
208200** this into the JSONB format and make it the return value of the
208201** SQL function.
208202*/
208203static void jsonReturnStringAsBlob(JsonString *pStr){
208204 JsonParse px;
208205 memset(&px, 0, sizeof(px));
208206 jsonStringTerminate(pStr);
208207 if( pStr->eErr ){
208208 sqlite3_result_error_nomem(pStr->pCtx);
208209 return;
208210 }
208211 px.zJson = pStr->zBuf;
208212 px.nJson = pStr->nUsed;
208213 px.db = sqlite3_context_db_handle(pStr->pCtx);
208214 (void)jsonTranslateTextToBlob(&px, 0);
208215 if( px.oom ){
208216 sqlite3DbFree(px.db, px.aBlob);
208217 sqlite3_result_error_nomem(pStr->pCtx);
208218 }else{
208219 assert( px.nBlobAlloc>0 )((void) (0));
208220 assert( !px.bReadOnly )((void) (0));
208221 sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
208222 }
208223}
208224
208225/* The byte at index i is a node type-code. This routine
208226** determines the payload size for that node and writes that
208227** payload size in to *pSz. It returns the offset from i to the
208228** beginning of the payload. Return 0 on error.
208229*/
208230static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
208231 u8 x;
208232 u32 sz;
208233 u32 n;
208234 if( NEVER(i>pParse->nBlob)(i>pParse->nBlob) ){
208235 *pSz = 0;
208236 return 0;
208237 }
208238 x = pParse->aBlob[i]>>4;
208239 if( x<=11 ){
208240 sz = x;
208241 n = 1;
208242 }else if( x==12 ){
208243 if( i+1>=pParse->nBlob ){
208244 *pSz = 0;
208245 return 0;
208246 }
208247 sz = pParse->aBlob[i+1];
208248 n = 2;
208249 }else if( x==13 ){
208250 if( i+2>=pParse->nBlob ){
208251 *pSz = 0;
208252 return 0;
208253 }
208254 sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
208255 n = 3;
208256 }else if( x==14 ){
208257 if( i+4>=pParse->nBlob ){
208258 *pSz = 0;
208259 return 0;
208260 }
208261 sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
208262 (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
208263 n = 5;
208264 }else{
208265 if( i+8>=pParse->nBlob
208266 || pParse->aBlob[i+1]!=0
208267 || pParse->aBlob[i+2]!=0
208268 || pParse->aBlob[i+3]!=0
208269 || pParse->aBlob[i+4]!=0
208270 ){
208271 *pSz = 0;
208272 return 0;
208273 }
208274 sz = (pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) +
208275 (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8];
208276 n = 9;
208277 }
208278 if( (i64)i+sz+n > pParse->nBlob
208279 && (i64)i+sz+n > pParse->nBlob-pParse->delta
208280 ){
208281 sz = 0;
208282 n = 0;
208283 }
208284 *pSz = sz;
208285 return n;
208286}
208287
208288
208289/*
208290** Translate the binary JSONB representation of JSON beginning at
208291** pParse->aBlob[i] into a JSON text string. Append the JSON
208292** text onto the end of pOut. Return the index in pParse->aBlob[]
208293** of the first byte past the end of the element that is translated.
208294**
208295** If an error is detected in the BLOB input, the pOut->eErr flag
208296** might get set to JSTRING_MALFORMED. But not all BLOB input errors
208297** are detected. So a malformed JSONB input might either result
208298** in an error, or in incorrect JSON.
208299**
208300** The pOut->eErr JSTRING_OOM flag is set on a OOM.
208301*/
208302static u32 jsonTranslateBlobToText(
208303 const JsonParse *pParse, /* the complete parse of the JSON */
208304 u32 i, /* Start rendering at this index */
208305 JsonString *pOut /* Write JSON here */
208306){
208307 u32 sz, n, j, iEnd;
208308
208309 n = jsonbPayloadSize(pParse, i, &sz);
208310 if( n==0 ){
208311 pOut->eErr |= JSTRING_MALFORMED0x02;
208312 return pParse->nBlob+1;
208313 }
208314 switch( pParse->aBlob[i] & 0x0f ){
208315 case JSONB_NULL0: {
208316 jsonAppendRawNZ(pOut, "null", 4);
208317 return i+1;
208318 }
208319 case JSONB_TRUE1: {
208320 jsonAppendRawNZ(pOut, "true", 4);
208321 return i+1;
208322 }
208323 case JSONB_FALSE2: {
208324 jsonAppendRawNZ(pOut, "false", 5);
208325 return i+1;
208326 }
208327 case JSONB_INT3:
208328 case JSONB_FLOAT5: {
208329 if( sz==0 ) goto malformed_jsonb;
208330 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
208331 break;
208332 }
208333 case JSONB_INT54: { /* Integer literal in hexadecimal notation */
208334 u32 k = 2;
208335 sqlite3_uint64 u = 0;
208336 const char *zIn = (const char*)&pParse->aBlob[i+n];
208337 int bOverflow = 0;
208338 if( sz==0 ) goto malformed_jsonb;
208339 if( zIn[0]=='-' ){
208340 jsonAppendChar(pOut, '-');
208341 k++;
208342 }else if( zIn[0]=='+' ){
208343 k++;
208344 }
208345 for(; k<sz; k++){
208346 if( !sqlite3Isxdigit(zIn[k])(sqlite3CtypeMap[(unsigned char)(zIn[k])]&0x08) ){
208347 pOut->eErr |= JSTRING_MALFORMED0x02;
208348 break;
208349 }else if( (u>>60)!=0 ){
208350 bOverflow = 1;
208351 }else{
208352 u = u*16 + sqlite3HexToInt(zIn[k]);
208353 }
208354 }
208355 jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u);
208356 break;
208357 }
208358 case JSONB_FLOAT56: { /* Float literal missing digits beside "." */
208359 u32 k = 0;
208360 const char *zIn = (const char*)&pParse->aBlob[i+n];
208361 if( sz==0 ) goto malformed_jsonb;
208362 if( zIn[0]=='-' ){
208363 jsonAppendChar(pOut, '-');
208364 k++;
208365 }
208366 if( zIn[k]=='.' ){
208367 jsonAppendChar(pOut, '0');
208368 }
208369 for(; k<sz; k++){
208370 jsonAppendChar(pOut, zIn[k]);
208371 if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])(sqlite3CtypeMap[(unsigned char)(zIn[k+1])]&0x04)) ){
208372 jsonAppendChar(pOut, '0');
208373 }
208374 }
208375 break;
208376 }
208377 case JSONB_TEXT7:
208378 case JSONB_TEXTJ8: {
208379 jsonAppendChar(pOut, '"');
208380 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
208381 jsonAppendChar(pOut, '"');
208382 break;
208383 }
208384 case JSONB_TEXT59: {
208385 const char *zIn;
208386 u32 k;
208387 u32 sz2 = sz;
208388 zIn = (const char*)&pParse->aBlob[i+n];
208389 jsonAppendChar(pOut, '"');
208390 while( sz2>0 ){
208391 for(k=0; k<sz2 && (jsonIsOk[(u8)zIn[k]] || zIn[k]=='\''); k++){}
208392 if( k>0 ){
208393 jsonAppendRawNZ(pOut, zIn, k);
208394 if( k>=sz2 ){
208395 break;
208396 }
208397 zIn += k;
208398 sz2 -= k;
208399 }
208400 if( zIn[0]=='"' ){
208401 jsonAppendRawNZ(pOut, "\\\"", 2);
208402 zIn++;
208403 sz2--;
208404 continue;
208405 }
208406 if( zIn[0]<=0x1f ){
208407 if( pOut->nUsed+7>pOut->nAlloc && jsonStringGrow(pOut,7) ) break;
208408 jsonAppendControlChar(pOut, zIn[0]);
208409 zIn++;
208410 sz2--;
208411 continue;
208412 }
208413 assert( zIn[0]=='\\' )((void) (0));
208414 assert( sz2>=1 )((void) (0));
208415 if( sz2<2 ){
208416 pOut->eErr |= JSTRING_MALFORMED0x02;
208417 break;
208418 }
208419 switch( (u8)zIn[1] ){
208420 case '\'':
208421 jsonAppendChar(pOut, '\'');
208422 break;
208423 case 'v':
208424 jsonAppendRawNZ(pOut, "\\u0009", 6);
208425 break;
208426 case 'x':
208427 if( sz2<4 ){
208428 pOut->eErr |= JSTRING_MALFORMED0x02;
208429 sz2 = 2;
208430 break;
208431 }
208432 jsonAppendRawNZ(pOut, "\\u00", 4);
208433 jsonAppendRawNZ(pOut, &zIn[2], 2);
208434 zIn += 2;
208435 sz2 -= 2;
208436 break;
208437 case '0':
208438 jsonAppendRawNZ(pOut, "\\u0000", 6);
208439 break;
208440 case '\r':
208441 if( sz2>2 && zIn[2]=='\n' ){
208442 zIn++;
208443 sz2--;
208444 }
208445 break;
208446 case '\n':
208447 break;
208448 case 0xe2:
208449 /* '\' followed by either U+2028 or U+2029 is ignored as
208450 ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
208451 ** U+2029 is the same except for the last byte */
208452 if( sz2<4
208453 || 0x80!=(u8)zIn[2]
208454 || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
208455 ){
208456 pOut->eErr |= JSTRING_MALFORMED0x02;
208457 sz2 = 2;
208458 break;
208459 }
208460 zIn += 2;
208461 sz2 -= 2;
208462 break;
208463 default:
208464 jsonAppendRawNZ(pOut, zIn, 2);
208465 break;
208466 }
208467 assert( sz2>=2 )((void) (0));
208468 zIn += 2;
208469 sz2 -= 2;
208470 }
208471 jsonAppendChar(pOut, '"');
208472 break;
208473 }
208474 case JSONB_TEXTRAW10: {
208475 jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz);
208476 break;
208477 }
208478 case JSONB_ARRAY11: {
208479 jsonAppendChar(pOut, '[');
208480 j = i+n;
208481 iEnd = j+sz;
208482 while( j<iEnd && pOut->eErr==0 ){
208483 j = jsonTranslateBlobToText(pParse, j, pOut);
208484 jsonAppendChar(pOut, ',');
208485 }
208486 if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED0x02;
208487 if( sz>0 ) jsonStringTrimOneChar(pOut);
208488 jsonAppendChar(pOut, ']');
208489 break;
208490 }
208491 case JSONB_OBJECT12: {
208492 int x = 0;
208493 jsonAppendChar(pOut, '{');
208494 j = i+n;
208495 iEnd = j+sz;
208496 while( j<iEnd && pOut->eErr==0 ){
208497 j = jsonTranslateBlobToText(pParse, j, pOut);
208498 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
208499 }
208500 if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED0x02;
208501 if( sz>0 ) jsonStringTrimOneChar(pOut);
208502 jsonAppendChar(pOut, '}');
208503 break;
208504 }
208505
208506 default: {
208507 malformed_jsonb:
208508 pOut->eErr |= JSTRING_MALFORMED0x02;
208509 break;
208510 }
208511 }
208512 return i+n+sz;
208513}
208514
208515/* Context for recursion of json_pretty()
208516*/
208517typedef struct JsonPretty JsonPretty;
208518struct JsonPretty {
208519 JsonParse *pParse; /* The BLOB being rendered */
208520 JsonString *pOut; /* Generate pretty output into this string */
208521 const char *zIndent; /* Use this text for indentation */
208522 u32 szIndent; /* Bytes in zIndent[] */
208523 u32 nIndent; /* Current level of indentation */
208524};
208525
208526/* Append indentation to the pretty JSON under construction */
208527static void jsonPrettyIndent(JsonPretty *pPretty){
208528 u32 jj;
208529 for(jj=0; jj<pPretty->nIndent; jj++){
208530 jsonAppendRaw(pPretty->pOut, pPretty->zIndent, pPretty->szIndent);
208531 }
208532}
208533
208534/*
208535** Translate the binary JSONB representation of JSON beginning at
208536** pParse->aBlob[i] into a JSON text string. Append the JSON
208537** text onto the end of pOut. Return the index in pParse->aBlob[]
208538** of the first byte past the end of the element that is translated.
208539**
208540** This is a variant of jsonTranslateBlobToText() that "pretty-prints"
208541** the output. Extra whitespace is inserted to make the JSON easier
208542** for humans to read.
208543**
208544** If an error is detected in the BLOB input, the pOut->eErr flag
208545** might get set to JSTRING_MALFORMED. But not all BLOB input errors
208546** are detected. So a malformed JSONB input might either result
208547** in an error, or in incorrect JSON.
208548**
208549** The pOut->eErr JSTRING_OOM flag is set on a OOM.
208550*/
208551static u32 jsonTranslateBlobToPrettyText(
208552 JsonPretty *pPretty, /* Pretty-printing context */
208553 u32 i /* Start rendering at this index */
208554){
208555 u32 sz, n, j, iEnd;
208556 const JsonParse *pParse = pPretty->pParse;
208557 JsonString *pOut = pPretty->pOut;
208558 n = jsonbPayloadSize(pParse, i, &sz);
208559 if( n==0 ){
208560 pOut->eErr |= JSTRING_MALFORMED0x02;
208561 return pParse->nBlob+1;
208562 }
208563 switch( pParse->aBlob[i] & 0x0f ){
208564 case JSONB_ARRAY11: {
208565 j = i+n;
208566 iEnd = j+sz;
208567 jsonAppendChar(pOut, '[');
208568 if( j<iEnd ){
208569 jsonAppendChar(pOut, '\n');
208570 pPretty->nIndent++;
208571 while( pOut->eErr==0 ){
208572 jsonPrettyIndent(pPretty);
208573 j = jsonTranslateBlobToPrettyText(pPretty, j);
208574 if( j>=iEnd ) break;
208575 jsonAppendRawNZ(pOut, ",\n", 2);
208576 }
208577 jsonAppendChar(pOut, '\n');
208578 pPretty->nIndent--;
208579 jsonPrettyIndent(pPretty);
208580 }
208581 jsonAppendChar(pOut, ']');
208582 i = iEnd;
208583 break;
208584 }
208585 case JSONB_OBJECT12: {
208586 j = i+n;
208587 iEnd = j+sz;
208588 jsonAppendChar(pOut, '{');
208589 if( j<iEnd ){
208590 jsonAppendChar(pOut, '\n');
208591 pPretty->nIndent++;
208592 while( pOut->eErr==0 ){
208593 jsonPrettyIndent(pPretty);
208594 j = jsonTranslateBlobToText(pParse, j, pOut);
208595 if( j>iEnd ){
208596 pOut->eErr |= JSTRING_MALFORMED0x02;
208597 break;
208598 }
208599 jsonAppendRawNZ(pOut, ": ", 2);
208600 j = jsonTranslateBlobToPrettyText(pPretty, j);
208601 if( j>=iEnd ) break;
208602 jsonAppendRawNZ(pOut, ",\n", 2);
208603 }
208604 jsonAppendChar(pOut, '\n');
208605 pPretty->nIndent--;
208606 jsonPrettyIndent(pPretty);
208607 }
208608 jsonAppendChar(pOut, '}');
208609 i = iEnd;
208610 break;
208611 }
208612 default: {
208613 i = jsonTranslateBlobToText(pParse, i, pOut);
208614 break;
208615 }
208616 }
208617 return i;
208618}
208619
208620
208621/* Return true if the input pJson
208622**
208623** For performance reasons, this routine does not do a detailed check of the
208624** input BLOB to ensure that it is well-formed. Hence, false positives are
208625** possible. False negatives should never occur, however.
208626*/
208627static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
208628 u32 sz, n;
208629 const u8 *aBlob;
208630 int nBlob;
208631 JsonParse s;
208632 if( sqlite3_value_type(pJson)!=SQLITE_BLOB4 ) return 0;
208633 aBlob = sqlite3_value_blob(pJson);
208634 nBlob = sqlite3_value_bytes(pJson);
208635 if( nBlob<1 ) return 0;
208636 if( NEVER(aBlob==0)(aBlob==0) || (aBlob[0] & 0x0f)>JSONB_OBJECT12 ) return 0;
208637 memset(&s, 0, sizeof(s));
208638 s.aBlob = (u8*)aBlob;
208639 s.nBlob = nBlob;
208640 n = jsonbPayloadSize(&s, 0, &sz);
208641 if( n==0 ) return 0;
208642 if( sz+n!=(u32)nBlob ) return 0;
208643 if( (aBlob[0] & 0x0f)<=JSONB_FALSE2 && sz>0 ) return 0;
208644 return sz+n==(u32)nBlob;
208645}
208646
208647/*
208648** Given that a JSONB_ARRAY object starts at offset i, return
208649** the number of entries in that array.
208650*/
208651static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){
208652 u32 n, sz, i, iEnd;
208653 u32 k = 0;
208654 n = jsonbPayloadSize(pParse, iRoot, &sz);
208655 iEnd = iRoot+n+sz;
208656 for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){
208657 n = jsonbPayloadSize(pParse, i, &sz);
208658 }
208659 return k;
208660}
208661
208662/*
208663** Edit the payload size of the element at iRoot by the amount in
208664** pParse->delta.
208665*/
208666static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
208667 u32 sz = 0;
208668 u32 nBlob;
208669 assert( pParse->delta!=0 )((void) (0));
208670 assert( pParse->nBlobAlloc >= pParse->nBlob )((void) (0));
208671 nBlob = pParse->nBlob;
208672 pParse->nBlob = pParse->nBlobAlloc;
208673 (void)jsonbPayloadSize(pParse, iRoot, &sz);
208674 pParse->nBlob = nBlob;
208675 sz += pParse->delta;
208676 pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz);
208677}
208678
208679/*
208680** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
208681** content beginning at iDel, and replacing them with nIns bytes of
208682** content given by aIns.
208683**
208684** nDel may be zero, in which case no bytes are removed. But iDel is
208685** still important as new bytes will be insert beginning at iDel.
208686**
208687** aIns may be zero, in which case space is created to hold nIns bytes
208688** beginning at iDel, but that space is uninitialized.
208689**
208690** Set pParse->oom if an OOM occurs.
208691*/
208692static void jsonBlobEdit(
208693 JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */
208694 u32 iDel, /* First byte to be removed */
208695 u32 nDel, /* Number of bytes to remove */
208696 const u8 *aIns, /* Content to insert */
208697 u32 nIns /* Bytes of content to insert */
208698){
208699 i64 d = (i64)nIns - (i64)nDel;
208700 if( d!=0 ){
208701 if( pParse->nBlob + d > pParse->nBlobAlloc ){
208702 jsonBlobExpand(pParse, pParse->nBlob+d);
208703 if( pParse->oom ) return;
208704 }
208705 memmove(&pParse->aBlob[iDel+nIns],
208706 &pParse->aBlob[iDel+nDel],
208707 pParse->nBlob - (iDel+nDel));
208708 pParse->nBlob += d;
208709 pParse->delta += d;
208710 }
208711 if( nIns && aIns ) memcpy(&pParse->aBlob[iDel], aIns, nIns);
208712}
208713
208714/*
208715** Return the number of escaped newlines to be ignored.
208716** An escaped newline is a one of the following byte sequences:
208717**
208718** 0x5c 0x0a
208719** 0x5c 0x0d
208720** 0x5c 0x0d 0x0a
208721** 0x5c 0xe2 0x80 0xa8
208722** 0x5c 0xe2 0x80 0xa9
208723*/
208724static u32 jsonBytesToBypass(const char *z, u32 n){
208725 u32 i = 0;
208726 while( i+1<n ){
208727 if( z[i]!='\\' ) return i;
208728 if( z[i+1]=='\n' ){
208729 i += 2;
208730 continue;
208731 }
208732 if( z[i+1]=='\r' ){
208733 if( i+2<n && z[i+2]=='\n' ){
208734 i += 3;
208735 }else{
208736 i += 2;
208737 }
208738 continue;
208739 }
208740 if( 0xe2==(u8)z[i+1]
208741 && i+3<n
208742 && 0x80==(u8)z[i+2]
208743 && (0xa8==(u8)z[i+3] || 0xa9==(u8)z[i+3])
208744 ){
208745 i += 4;
208746 continue;
208747 }
208748 break;
208749 }
208750 return i;
208751}
208752
208753/*
208754** Input z[0..n] defines JSON escape sequence including the leading '\\'.
208755** Decode that escape sequence into a single character. Write that
208756** character into *piOut. Return the number of bytes in the escape sequence.
208757**
208758** If there is a syntax error of some kind (for example too few characters
208759** after the '\\' to complete the encoding) then *piOut is set to
208760** JSON_INVALID_CHAR.
208761*/
208762static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
208763 assert( n>0 )((void) (0));
208764 assert( z[0]=='\\' )((void) (0));
208765 if( n<2 ){
208766 *piOut = JSON_INVALID_CHAR0x99999;
208767 return n;
208768 }
208769 switch( (u8)z[1] ){
208770 case 'u': {
208771 u32 v, vlo;
208772 if( n<6 ){
208773 *piOut = JSON_INVALID_CHAR0x99999;
208774 return n;
208775 }
208776 v = jsonHexToInt4(&z[2]);
208777 if( (v & 0xfc00)==0xd800
208778 && n>=12
208779 && z[6]=='\\'
208780 && z[7]=='u'
208781 && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00
208782 ){
208783 *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
208784 return 12;
208785 }else{
208786 *piOut = v;
208787 return 6;
208788 }
208789 }
208790 case 'b': { *piOut = '\b'; return 2; }
208791 case 'f': { *piOut = '\f'; return 2; }
208792 case 'n': { *piOut = '\n'; return 2; }
208793 case 'r': { *piOut = '\r'; return 2; }
208794 case 't': { *piOut = '\t'; return 2; }
208795 case 'v': { *piOut = '\v'; return 2; }
208796 case '0': { *piOut = 0; return 2; }
208797 case '\'':
208798 case '"':
208799 case '/':
208800 case '\\':{ *piOut = z[1]; return 2; }
208801 case 'x': {
208802 if( n<4 ){
208803 *piOut = JSON_INVALID_CHAR0x99999;
208804 return n;
208805 }
208806 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
208807 return 4;
208808 }
208809 case 0xe2:
208810 case '\r':
208811 case '\n': {
208812 u32 nSkip = jsonBytesToBypass(z, n);
208813 if( nSkip==0 ){
208814 *piOut = JSON_INVALID_CHAR0x99999;
208815 return n;
208816 }else if( nSkip==n ){
208817 *piOut = 0;
208818 return n;
208819 }else if( z[nSkip]=='\\' ){
208820 return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut);
208821 }else{
208822 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
208823 return nSkip + sz;
208824 }
208825 }
208826 default: {
208827 *piOut = JSON_INVALID_CHAR0x99999;
208828 return 2;
208829 }
208830 }
208831}
208832
208833
208834/*
208835** Compare two object labels. Return 1 if they are equal and
208836** 0 if they differ.
208837**
208838** In this version, we know that one or the other or both of the
208839** two comparands contains an escape sequence.
208840*/
208841static SQLITE_NOINLINE__attribute__((noinline)) int jsonLabelCompareEscaped(
208842 const char *zLeft, /* The left label */
208843 u32 nLeft, /* Size of the left label in bytes */
208844 int rawLeft, /* True if zLeft contains no escapes */
208845 const char *zRight, /* The right label */
208846 u32 nRight, /* Size of the right label in bytes */
208847 int rawRight /* True if zRight is escape-free */
208848){
208849 u32 cLeft, cRight;
208850 assert( rawLeft==0 || rawRight==0 )((void) (0));
208851 while( 1 /*exit-by-return*/ ){
208852 if( nLeft==0 ){
208853 cLeft = 0;
208854 }else if( rawLeft || zLeft[0]!='\\' ){
208855 cLeft = ((u8*)zLeft)[0];
208856 if( cLeft>=0xc0 ){
208857 int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft);
208858 zLeft += sz;
208859 nLeft -= sz;
208860 }else{
208861 zLeft++;
208862 nLeft--;
208863 }
208864 }else{
208865 u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft);
208866 zLeft += n;
208867 assert( n<=nLeft )((void) (0));
208868 nLeft -= n;
208869 }
208870 if( nRight==0 ){
208871 cRight = 0;
208872 }else if( rawRight || zRight[0]!='\\' ){
208873 cRight = ((u8*)zRight)[0];
208874 if( cRight>=0xc0 ){
208875 int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight);
208876 zRight += sz;
208877 nRight -= sz;
208878 }else{
208879 zRight++;
208880 nRight--;
208881 }
208882 }else{
208883 u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight);
208884 zRight += n;
208885 assert( n<=nRight )((void) (0));
208886 nRight -= n;
208887 }
208888 if( cLeft!=cRight ) return 0;
208889 if( cLeft==0 ) return 1;
208890 }
208891}
208892
208893/*
208894** Compare two object labels. Return 1 if they are equal and
208895** 0 if they differ. Return -1 if an OOM occurs.
208896*/
208897static int jsonLabelCompare(
208898 const char *zLeft, /* The left label */
208899 u32 nLeft, /* Size of the left label in bytes */
208900 int rawLeft, /* True if zLeft contains no escapes */
208901 const char *zRight, /* The right label */
208902 u32 nRight, /* Size of the right label in bytes */
208903 int rawRight /* True if zRight is escape-free */
208904){
208905 if( rawLeft && rawRight ){
208906 /* Simpliest case: Neither label contains escapes. A simple
208907 ** memcmp() is sufficient. */
208908 if( nLeft!=nRight ) return 0;
208909 return memcmp(zLeft, zRight, nLeft)==0;
208910 }else{
208911 return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft,
208912 zRight, nRight, rawRight);
208913 }
208914}
208915
208916/*
208917** Error returns from jsonLookupStep()
208918*/
208919#define JSON_LOOKUP_ERROR0xffffffff 0xffffffff
208920#define JSON_LOOKUP_NOTFOUND0xfffffffe 0xfffffffe
208921#define JSON_LOOKUP_PATHERROR0xfffffffd 0xfffffffd
208922#define JSON_LOOKUP_ISERROR(x)((x)>=0xfffffffd) ((x)>=JSON_LOOKUP_PATHERROR0xfffffffd)
208923
208924/* Forward declaration */
208925static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
208926
208927
208928/* This helper routine for jsonLookupStep() populates pIns with
208929** binary data that is to be inserted into pParse.
208930**
208931** In the common case, pIns just points to pParse->aIns and pParse->nIns.
208932** But if the zPath of the original edit operation includes path elements
208933** that go deeper, additional substructure must be created.
208934**
208935** For example:
208936**
208937** json_insert('{}', '$.a.b.c', 123);
208938**
208939** The search stops at '$.a' But additional substructure must be
208940** created for the ".b.c" part of the patch so that the final result
208941** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with
208942** the binary equivalent of {"b":{"c":123}} so that it can be inserted.
208943**
208944** The caller is responsible for resetting pIns when it has finished
208945** using the substructure.
208946*/
208947static u32 jsonCreateEditSubstructure(
208948 JsonParse *pParse, /* The original JSONB that is being edited */
208949 JsonParse *pIns, /* Populate this with the blob data to insert */
208950 const char *zTail /* Tail of the path that determins substructure */
208951){
208952 static const u8 emptyObject[] = { JSONB_ARRAY11, JSONB_OBJECT12 };
208953 int rc;
208954 memset(pIns, 0, sizeof(*pIns));
208955 pIns->db = pParse->db;
208956 if( zTail[0]==0 ){
208957 /* No substructure. Just insert what is given in pParse. */
208958 pIns->aBlob = pParse->aIns;
208959 pIns->nBlob = pParse->nIns;
208960 rc = 0;
208961 }else{
208962 /* Construct the binary substructure */
208963 pIns->nBlob = 1;
208964 pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.'];
208965 pIns->eEdit = pParse->eEdit;
208966 pIns->nIns = pParse->nIns;
208967 pIns->aIns = pParse->aIns;
208968 rc = jsonLookupStep(pIns, 0, zTail, 0);
208969 pParse->oom |= pIns->oom;
208970 }
208971 return rc; /* Error code only */
208972}
208973
208974/*
208975** Search along zPath to find the Json element specified. Return an
208976** index into pParse->aBlob[] for the start of that element's value.
208977**
208978** If the value found by this routine is the value half of label/value pair
208979** within an object, then set pPath->iLabel to the start of the corresponding
208980** label, before returning.
208981**
208982** Return one of the JSON_LOOKUP error codes if problems are seen.
208983**
208984** This routine will also modify the blob. If pParse->eEdit is one of
208985** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
208986** made to the selected value. If an edit is performed, then the return
208987** value does not necessarily point to the select element. If an edit
208988** is performed, the return value is only useful for detecting error
208989** conditions.
208990*/
208991static u32 jsonLookupStep(
208992 JsonParse *pParse, /* The JSON to search */
208993 u32 iRoot, /* Begin the search at this element of aBlob[] */
208994 const char *zPath, /* The path to search */
208995 u32 iLabel /* Label if iRoot is a value of in an object */
208996){
208997 u32 i, j, k, nKey, sz, n, iEnd, rc;
208998 const char *zKey;
208999 u8 x;
209000
209001 if( zPath[0]==0 ){
209002 if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
209003 n = jsonbPayloadSize(pParse, iRoot, &sz);
209004 sz += n;
209005 if( pParse->eEdit==JEDIT_DEL1 ){
209006 if( iLabel>0 ){
209007 sz += iRoot - iLabel;
209008 iRoot = iLabel;
209009 }
209010 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
209011 }else if( pParse->eEdit==JEDIT_INS3 ){
209012 /* Already exists, so json_insert() is a no-op */
209013 }else{
209014 /* json_set() or json_replace() */
209015 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
209016 }
209017 }
209018 pParse->iLabel = iLabel;
209019 return iRoot;
209020 }
209021 if( zPath[0]=='.' ){
209022 int rawKey = 1;
209023 x = pParse->aBlob[iRoot];
209024 zPath++;
209025 if( zPath[0]=='"' ){
209026 zKey = zPath + 1;
209027 for(i=1; zPath[i] && zPath[i]!='"'; i++){
209028 if( zPath[i]=='\\' && zPath[i+1]!=0 ) i++;
209029 }
209030 nKey = i-1;
209031 if( zPath[i] ){
209032 i++;
209033 }else{
209034 return JSON_LOOKUP_PATHERROR0xfffffffd;
209035 }
209036 testcase( nKey==0 );
209037 rawKey = memchr(zKey, '\\', nKey)==0;
209038 }else{
209039 zKey = zPath;
209040 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
209041 nKey = i;
209042 if( nKey==0 ){
209043 return JSON_LOOKUP_PATHERROR0xfffffffd;
209044 }
209045 }
209046 if( (x & 0x0f)!=JSONB_OBJECT12 ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
209047 n = jsonbPayloadSize(pParse, iRoot, &sz);
209048 j = iRoot + n; /* j is the index of a label */
209049 iEnd = j+sz;
209050 while( j<iEnd ){
209051 int rawLabel;
209052 const char *zLabel;
209053 x = pParse->aBlob[j] & 0x0f;
209054 if( x<JSONB_TEXT7 || x>JSONB_TEXTRAW10 ) return JSON_LOOKUP_ERROR0xffffffff;
209055 n = jsonbPayloadSize(pParse, j, &sz);
209056 if( n==0 ) return JSON_LOOKUP_ERROR0xffffffff;
209057 k = j+n; /* k is the index of the label text */
209058 if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
209059 zLabel = (const char*)&pParse->aBlob[k];
209060 rawLabel = x==JSONB_TEXT7 || x==JSONB_TEXTRAW10;
209061 if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){
209062 u32 v = k+sz; /* v is the index of the value */
209063 if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT12 ) return JSON_LOOKUP_ERROR0xffffffff;
209064 n = jsonbPayloadSize(pParse, v, &sz);
209065 if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
209066 assert( j>0 )((void) (0));
209067 rc = jsonLookupStep(pParse, v, &zPath[i], j);
209068 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
209069 return rc;
209070 }
209071 j = k+sz;
209072 if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT12 ) return JSON_LOOKUP_ERROR0xffffffff;
209073 n = jsonbPayloadSize(pParse, j, &sz);
209074 if( n==0 ) return JSON_LOOKUP_ERROR0xffffffff;
209075 j += n+sz;
209076 }
209077 if( j>iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
209078 if( pParse->eEdit>=JEDIT_INS3 ){
209079 u32 nIns; /* Total bytes to insert (label+value) */
209080 JsonParse v; /* BLOB encoding of the value to be inserted */
209081 JsonParse ix; /* Header of the label to be inserted */
209082 testcase( pParse->eEdit==JEDIT_INS );
209083 testcase( pParse->eEdit==JEDIT_SET );
209084 memset(&ix, 0, sizeof(ix));
209085 ix.db = pParse->db;
209086 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW10:JSONB_TEXT59, nKey, 0);
209087 pParse->oom |= ix.oom;
209088 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
209089 if( !JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd)
209090 && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob)
209091 ){
209092 assert( !pParse->oom )((void) (0));
209093 nIns = ix.nBlob + nKey + v.nBlob;
209094 jsonBlobEdit(pParse, j, 0, 0, nIns);
209095 if( !pParse->oom ){
209096 assert( pParse->aBlob!=0 )((void) (0)); /* Because pParse->oom!=0 */
209097 assert( ix.aBlob!=0 )((void) (0)); /* Because pPasre->oom!=0 */
209098 memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
209099 k = j + ix.nBlob;
209100 memcpy(&pParse->aBlob[k], zKey, nKey);
209101 k += nKey;
209102 memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
209103 if( ALWAYS(pParse->delta)(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
209104 }
209105 }
209106 jsonParseReset(&v);
209107 jsonParseReset(&ix);
209108 return rc;
209109 }
209110 }else if( zPath[0]=='[' ){
209111 x = pParse->aBlob[iRoot] & 0x0f;
209112 if( x!=JSONB_ARRAY11 ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
209113 n = jsonbPayloadSize(pParse, iRoot, &sz);
209114 k = 0;
209115 i = 1;
209116 while( sqlite3Isdigit(zPath[i])(sqlite3CtypeMap[(unsigned char)(zPath[i])]&0x04) ){
209117 k = k*10 + zPath[i] - '0';
209118 i++;
209119 }
209120 if( i<2 || zPath[i]!=']' ){
209121 if( zPath[1]=='#' ){
209122 k = jsonbArrayCount(pParse, iRoot);
209123 i = 2;
209124 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3])(sqlite3CtypeMap[(unsigned char)(zPath[3])]&0x04) ){
209125 unsigned int nn = 0;
209126 i = 3;
209127 do{
209128 nn = nn*10 + zPath[i] - '0';
209129 i++;
209130 }while( sqlite3Isdigit(zPath[i])(sqlite3CtypeMap[(unsigned char)(zPath[i])]&0x04) );
209131 if( nn>k ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
209132 k -= nn;
209133 }
209134 if( zPath[i]!=']' ){
209135 return JSON_LOOKUP_PATHERROR0xfffffffd;
209136 }
209137 }else{
209138 return JSON_LOOKUP_PATHERROR0xfffffffd;
209139 }
209140 }
209141 j = iRoot+n;
209142 iEnd = j+sz;
209143 while( j<iEnd ){
209144 if( k==0 ){
209145 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
209146 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
209147 return rc;
209148 }
209149 k--;
209150 n = jsonbPayloadSize(pParse, j, &sz);
209151 if( n==0 ) return JSON_LOOKUP_ERROR0xffffffff;
209152 j += n+sz;
209153 }
209154 if( j>iEnd ) return JSON_LOOKUP_ERROR0xffffffff;
209155 if( k>0 ) return JSON_LOOKUP_NOTFOUND0xfffffffe;
209156 if( pParse->eEdit>=JEDIT_INS3 ){
209157 JsonParse v;
209158 testcase( pParse->eEdit==JEDIT_INS );
209159 testcase( pParse->eEdit==JEDIT_SET );
209160 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
209161 if( !JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd)
209162 && jsonBlobMakeEditable(pParse, v.nBlob)
209163 ){
209164 assert( !pParse->oom )((void) (0));
209165 jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob);
209166 }
209167 jsonParseReset(&v);
209168 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
209169 return rc;
209170 }
209171 }else{
209172 return JSON_LOOKUP_PATHERROR0xfffffffd;
209173 }
209174 return JSON_LOOKUP_NOTFOUND0xfffffffe;
209175}
209176
209177/*
209178** Convert a JSON BLOB into text and make that text the return value
209179** of an SQL function.
209180*/
209181static void jsonReturnTextJsonFromBlob(
209182 sqlite3_context *ctx,
209183 const u8 *aBlob,
209184 u32 nBlob
209185){
209186 JsonParse x;
209187 JsonString s;
209188
209189 if( NEVER(aBlob==0)(aBlob==0) ) return;
209190 memset(&x, 0, sizeof(x));
209191 x.aBlob = (u8*)aBlob;
209192 x.nBlob = nBlob;
209193 jsonStringInit(&s, ctx);
209194 jsonTranslateBlobToText(&x, 0, &s);
209195 jsonReturnString(&s, 0, 0);
209196}
209197
209198
209199/*
209200** Return the value of the BLOB node at index i.
209201**
209202** If the value is a primitive, return it as an SQL value.
209203** If the value is an array or object, return it as either
209204** JSON text or the BLOB encoding, depending on the JSON_B flag
209205** on the userdata.
209206*/
209207static void jsonReturnFromBlob(
209208 JsonParse *pParse, /* Complete JSON parse tree */
209209 u32 i, /* Index of the node */
209210 sqlite3_context *pCtx, /* Return value for this function */
209211 int textOnly /* return text JSON. Disregard user-data */
209212){
209213 u32 n, sz;
209214 int rc;
209215 sqlite3 *db = sqlite3_context_db_handle(pCtx);
209216
209217 n = jsonbPayloadSize(pParse, i, &sz);
209218 if( n==0 ){
209219 sqlite3_result_error(pCtx, "malformed JSON", -1);
209220 return;
209221 }
209222 switch( pParse->aBlob[i] & 0x0f ){
209223 case JSONB_NULL0: {
209224 if( sz ) goto returnfromblob_malformed;
209225 sqlite3_result_null(pCtx);
209226 break;
209227 }
209228 case JSONB_TRUE1: {
209229 if( sz ) goto returnfromblob_malformed;
209230 sqlite3_result_int(pCtx, 1);
209231 break;
209232 }
209233 case JSONB_FALSE2: {
209234 if( sz ) goto returnfromblob_malformed;
209235 sqlite3_result_int(pCtx, 0);
209236 break;
209237 }
209238 case JSONB_INT54:
209239 case JSONB_INT3: {
209240 sqlite3_int64 iRes = 0;
209241 char *z;
209242 int bNeg = 0;
209243 char x;
209244 if( sz==0 ) goto returnfromblob_malformed;
209245 x = (char)pParse->aBlob[i+n];
209246 if( x=='-' ){
209247 if( sz<2 ) goto returnfromblob_malformed;
209248 n++;
209249 sz--;
209250 bNeg = 1;
209251 }
209252 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
209253 if( z==0 ) goto returnfromblob_oom;
209254 rc = sqlite3DecOrHexToI64(z, &iRes);
209255 sqlite3DbFree(db, z);
209256 if( rc==0 ){
209257 sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes);
209258 }else if( rc==3 && bNeg ){
209259 sqlite3_result_int64(pCtx, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
209260 }else if( rc==1 ){
209261 goto returnfromblob_malformed;
209262 }else{
209263 if( bNeg ){ n--; sz++; }
209264 goto to_double;
209265 }
209266 break;
209267 }
209268 case JSONB_FLOAT56:
209269 case JSONB_FLOAT5: {
209270 double r;
209271 char *z;
209272 if( sz==0 ) goto returnfromblob_malformed;
209273 to_double:
209274 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
209275 if( z==0 ) goto returnfromblob_oom;
209276 rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF81);
209277 sqlite3DbFree(db, z);
209278 if( rc<=0 ) goto returnfromblob_malformed;
209279 sqlite3_result_double(pCtx, r);
209280 break;
209281 }
209282 case JSONB_TEXTRAW10:
209283 case JSONB_TEXT7: {
209284 sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz,
209285 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
209286 break;
209287 }
209288 case JSONB_TEXT59:
209289 case JSONB_TEXTJ8: {
209290 /* Translate JSON formatted string into raw text */
209291 u32 iIn, iOut;
209292 const char *z;
209293 char *zOut;
209294 u32 nOut = sz;
209295 z = (const char*)&pParse->aBlob[i+n];
209296 zOut = sqlite3DbMallocRaw(db, nOut+1);
209297 if( zOut==0 ) goto returnfromblob_oom;
209298 for(iIn=iOut=0; iIn<sz; iIn++){
209299 char c = z[iIn];
209300 if( c=='\\' ){
209301 u32 v;
209302 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
209303 if( v<=0x7f ){
209304 zOut[iOut++] = (char)v;
209305 }else if( v<=0x7ff ){
209306 assert( szEscape>=2 )((void) (0));
209307 zOut[iOut++] = (char)(0xc0 | (v>>6));
209308 zOut[iOut++] = 0x80 | (v&0x3f);
209309 }else if( v<0x10000 ){
209310 assert( szEscape>=3 )((void) (0));
209311 zOut[iOut++] = 0xe0 | (v>>12);
209312 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
209313 zOut[iOut++] = 0x80 | (v&0x3f);
209314 }else if( v==JSON_INVALID_CHAR0x99999 ){
209315 /* Silently ignore illegal unicode */
209316 }else{
209317 assert( szEscape>=4 )((void) (0));
209318 zOut[iOut++] = 0xf0 | (v>>18);
209319 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
209320 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
209321 zOut[iOut++] = 0x80 | (v&0x3f);
209322 }
209323 iIn += szEscape - 1;
209324 }else{
209325 zOut[iOut++] = c;
209326 }
209327 } /* end for() */
209328 assert( iOut<=nOut )((void) (0));
209329 zOut[iOut] = 0;
209330 sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
209331 break;
209332 }
209333 case JSONB_ARRAY11:
209334 case JSONB_OBJECT12: {
209335 int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx))((int)(long int)(sqlite3_user_data(pCtx)));
209336 if( flags & JSON_BLOB0x08 ){
209337 sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
209338 }else{
209339 jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
209340 }
209341 break;
209342 }
209343 default: {
209344 goto returnfromblob_malformed;
209345 }
209346 }
209347 return;
209348
209349returnfromblob_oom:
209350 sqlite3_result_error_nomem(pCtx);
209351 return;
209352
209353returnfromblob_malformed:
209354 sqlite3_result_error(pCtx, "malformed JSON", -1);
209355 return;
209356}
209357
209358/*
209359** pArg is a function argument that might be an SQL value or a JSON
209360** value. Figure out what it is and encode it as a JSONB blob.
209361** Return the results in pParse.
209362**
209363** pParse is uninitialized upon entry. This routine will handle the
209364** initialization of pParse. The result will be contained in
209365** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically
209366** allocated (if pParse->nBlobAlloc is greater than zero) in which case
209367** the caller is responsible for freeing the space allocated to pParse->aBlob
209368** when it has finished with it. Or pParse->aBlob might be a static string
209369** or a value obtained from sqlite3_value_blob(pArg).
209370**
209371** If the argument is a BLOB that is clearly not a JSONB, then this
209372** function might set an error message in ctx and return non-zero.
209373** It might also set an error message and return non-zero on an OOM error.
209374*/
209375static int jsonFunctionArgToBlob(
209376 sqlite3_context *ctx,
209377 sqlite3_value *pArg,
209378 JsonParse *pParse
209379){
209380 int eType = sqlite3_value_type(pArg);
209381 static u8 aNull[] = { 0x00 };
209382 memset(pParse, 0, sizeof(pParse[0]));
209383 pParse->db = sqlite3_context_db_handle(ctx);
209384 switch( eType ){
209385 default: {
209386 pParse->aBlob = aNull;
209387 pParse->nBlob = 1;
209388 return 0;
209389 }
209390 case SQLITE_BLOB4: {
209391 if( jsonFuncArgMightBeBinary(pArg) ){
209392 pParse->aBlob = (u8*)sqlite3_value_blob(pArg);
209393 pParse->nBlob = sqlite3_value_bytes(pArg);
209394 }else{
209395 sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
209396 return 1;
209397 }
209398 break;
209399 }
209400 case SQLITE_TEXT3: {
209401 const char *zJson = (const char*)sqlite3_value_text(pArg);
209402 int nJson = sqlite3_value_bytes(pArg);
209403 if( zJson==0 ) return 1;
209404 if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE74 ){
209405 pParse->zJson = (char*)zJson;
209406 pParse->nJson = nJson;
209407 if( jsonConvertTextToBlob(pParse, ctx) ){
209408 sqlite3_result_error(ctx, "malformed JSON", -1);
209409 sqlite3DbFree(pParse->db, pParse->aBlob);
209410 memset(pParse, 0, sizeof(pParse[0]));
209411 return 1;
209412 }
209413 }else{
209414 jsonBlobAppendNode(pParse, JSONB_TEXTRAW10, nJson, zJson);
209415 }
209416 break;
209417 }
209418 case SQLITE_FLOAT2: {
209419 double r = sqlite3_value_double(pArg);
209420 if( NEVER(sqlite3IsNaN(r))(sqlite3IsNaN(r)) ){
209421 jsonBlobAppendNode(pParse, JSONB_NULL0, 0, 0);
209422 }else{
209423 int n = sqlite3_value_bytes(pArg);
209424 const char *z = (const char*)sqlite3_value_text(pArg);
209425 if( z==0 ) return 1;
209426 if( z[0]=='I' ){
209427 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 5, "9e999");
209428 }else if( z[0]=='-' && z[1]=='I' ){
209429 jsonBlobAppendNode(pParse, JSONB_FLOAT5, 6, "-9e999");
209430 }else{
209431 jsonBlobAppendNode(pParse, JSONB_FLOAT5, n, z);
209432 }
209433 }
209434 break;
209435 }
209436 case SQLITE_INTEGER1: {
209437 int n = sqlite3_value_bytes(pArg);
209438 const char *z = (const char*)sqlite3_value_text(pArg);
209439 if( z==0 ) return 1;
209440 jsonBlobAppendNode(pParse, JSONB_INT3, n, z);
209441 break;
209442 }
209443 }
209444 if( pParse->oom ){
209445 sqlite3_result_error_nomem(ctx);
209446 return 1;
209447 }else{
209448 return 0;
209449 }
209450}
209451
209452/*
209453** Generate a bad path error.
209454**
209455** If ctx is not NULL then push the error message into ctx and return NULL.
209456** If ctx is NULL, then return the text of the error message.
209457*/
209458static char *jsonBadPathError(
209459 sqlite3_context *ctx, /* The function call containing the error */
209460 const char *zPath /* The path with the problem */
209461){
209462 char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
209463 if( ctx==0 ) return zMsg;
209464 if( zMsg ){
209465 sqlite3_result_error(ctx, zMsg, -1);
209466 sqlite3_free(zMsg);
209467 }else{
209468 sqlite3_result_error_nomem(ctx);
209469 }
209470 return 0;
209471}
209472
209473/* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent
209474** arguments come in parse where each pair contains a JSON path and
209475** content to insert or set at that patch. Do the updates
209476** and return the result.
209477**
209478** The specific operation is determined by eEdit, which can be one
209479** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
209480*/
209481static void jsonInsertIntoBlob(
209482 sqlite3_context *ctx,
209483 int argc,
209484 sqlite3_value **argv,
209485 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
209486){
209487 int i;
209488 u32 rc = 0;
209489 const char *zPath = 0;
209490 int flgs;
209491 JsonParse *p;
209492 JsonParse ax;
209493
209494 assert( (argc&1)==1 )((void) (0));
209495 flgs = argc==1 ? 0 : JSON_EDITABLE0x01;
209496 p = jsonParseFuncArg(ctx, argv[0], flgs);
209497 if( p==0 ) return;
209498 for(i=1; i<argc-1; i+=2){
209499 if( sqlite3_value_type(argv[i])==SQLITE_NULL5 ) continue;
209500 zPath = (const char*)sqlite3_value_text(argv[i]);
209501 if( zPath==0 ){
209502 sqlite3_result_error_nomem(ctx);
209503 jsonParseFree(p);
209504 return;
209505 }
209506 if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
209507 if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
209508 jsonParseReset(&ax);
209509 jsonParseFree(p);
209510 return;
209511 }
209512 if( zPath[1]==0 ){
209513 if( eEdit==JEDIT_REPL2 || eEdit==JEDIT_SET4 ){
209514 jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob);
209515 }
209516 rc = 0;
209517 }else{
209518 p->eEdit = eEdit;
209519 p->nIns = ax.nBlob;
209520 p->aIns = ax.aBlob;
209521 p->delta = 0;
209522 rc = jsonLookupStep(p, 0, zPath+1, 0);
209523 }
209524 jsonParseReset(&ax);
209525 if( rc==JSON_LOOKUP_NOTFOUND0xfffffffe ) continue;
209526 if( JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd) ) goto jsonInsertIntoBlob_patherror;
209527 }
209528 jsonReturnParse(ctx, p);
209529 jsonParseFree(p);
209530 return;
209531
209532jsonInsertIntoBlob_patherror:
209533 jsonParseFree(p);
209534 if( rc==JSON_LOOKUP_ERROR0xffffffff ){
209535 sqlite3_result_error(ctx, "malformed JSON", -1);
209536 }else{
209537 jsonBadPathError(ctx, zPath);
209538 }
209539 return;
209540}
209541
209542/*
209543** If pArg is a blob that seems like a JSONB blob, then initialize
209544** p to point to that JSONB and return TRUE. If pArg does not seem like
209545** a JSONB blob, then return FALSE;
209546**
209547** This routine is only called if it is already known that pArg is a
209548** blob. The only open question is whether or not the blob appears
209549** to be a JSONB blob.
209550*/
209551static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){
209552 u32 n, sz = 0;
209553 p->aBlob = (u8*)sqlite3_value_blob(pArg);
209554 p->nBlob = (u32)sqlite3_value_bytes(pArg);
209555 if( p->nBlob==0 ){
209556 p->aBlob = 0;
209557 return 0;
209558 }
209559 if( NEVER(p->aBlob==0)(p->aBlob==0) ){
209560 return 0;
209561 }
209562 if( (p->aBlob[0] & 0x0f)<=JSONB_OBJECT12
209563 && (n = jsonbPayloadSize(p, 0, &sz))>0
209564 && sz+n==p->nBlob
209565 && ((p->aBlob[0] & 0x0f)>JSONB_FALSE2 || sz==0)
209566 ){
209567 return 1;
209568 }
209569 p->aBlob = 0;
209570 p->nBlob = 0;
209571 return 0;
209572}
209573
209574/*
209575** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
209576** from the SQL function argument pArg. Return a pointer to the new
209577** JsonParse object.
209578**
209579** Ownership of the new JsonParse object is passed to the caller. The
209580** caller should invoke jsonParseFree() on the return value when it
209581** has finished using it.
209582**
209583** If any errors are detected, an appropriate error messages is set
209584** using sqlite3_result_error() or the equivalent and this routine
209585** returns NULL. This routine also returns NULL if the pArg argument
209586** is an SQL NULL value, but no error message is set in that case. This
209587** is so that SQL functions that are given NULL arguments will return
209588** a NULL value.
209589*/
209590static JsonParse *jsonParseFuncArg(
209591 sqlite3_context *ctx,
209592 sqlite3_value *pArg,
209593 u32 flgs
209594){
209595 int eType; /* Datatype of pArg */
209596 JsonParse *p = 0; /* Value to be returned */
209597 JsonParse *pFromCache = 0; /* Value taken from cache */
209598 sqlite3 *db; /* The database connection */
209599
209600 assert( ctx!=0 )((void) (0));
209601 eType = sqlite3_value_type(pArg);
209602 if( eType==SQLITE_NULL5 ){
209603 return 0;
209604 }
209605 pFromCache = jsonCacheSearch(ctx, pArg);
209606 if( pFromCache ){
209607 pFromCache->nJPRef++;
209608 if( (flgs & JSON_EDITABLE0x01)==0 ){
209609 return pFromCache;
209610 }
209611 }
209612 db = sqlite3_context_db_handle(ctx);
209613rebuild_from_cache:
209614 p = sqlite3DbMallocZero(db, sizeof(*p));
209615 if( p==0 ) goto json_pfa_oom;
209616 memset(p, 0, sizeof(*p));
209617 p->db = db;
209618 p->nJPRef = 1;
209619 if( pFromCache!=0 ){
209620 u32 nBlob = pFromCache->nBlob;
209621 p->aBlob = sqlite3DbMallocRaw(db, nBlob);
209622 if( p->aBlob==0 ) goto json_pfa_oom;
209623 memcpy(p->aBlob, pFromCache->aBlob, nBlob);
209624 p->nBlobAlloc = p->nBlob = nBlob;
209625 p->hasNonstd = pFromCache->hasNonstd;
209626 jsonParseFree(pFromCache);
209627 return p;
209628 }
209629 if( eType==SQLITE_BLOB4 ){
209630 if( jsonArgIsJsonb(pArg,p) ){
209631 if( (flgs & JSON_EDITABLE0x01)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
209632 goto json_pfa_oom;
209633 }
209634 return p;
209635 }
209636 /* If the blob is not valid JSONB, fall through into trying to cast
209637 ** the blob into text which is then interpreted as JSON. (tag-20240123-a)
209638 **
209639 ** This goes against all historical documentation about how the SQLite
209640 ** JSON functions were suppose to work. From the beginning, blob was
209641 ** reserved for expansion and a blob value should have raised an error.
209642 ** But it did not, due to a bug. And many applications came to depend
209643 ** upon this buggy behavior, espeically when using the CLI and reading
209644 ** JSON text using readfile(), which returns a blob. For this reason
209645 ** we will continue to support the bug moving forward.
209646 ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d
209647 */
209648 }
209649 p->zJson = (char*)sqlite3_value_text(pArg);
209650 p->nJson = sqlite3_value_bytes(pArg);
209651 if( db->mallocFailed ) goto json_pfa_oom;
209652 if( p->nJson==0 ) goto json_pfa_malformed;
209653 assert( p->zJson!=0 )((void) (0));
209654 if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR0x02) ? 0 : ctx) ){
209655 if( flgs & JSON_KEEPERROR0x02 ){
209656 p->nErr = 1;
209657 return p;
209658 }else{
209659 jsonParseFree(p);
209660 return 0;
209661 }
209662 }else{
209663 int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref);
209664 int rc;
209665 if( !isRCStr ){
209666 char *zNew = sqlite3RCStrNew( p->nJson );
209667 if( zNew==0 ) goto json_pfa_oom;
209668 memcpy(zNew, p->zJson, p->nJson);
209669 p->zJson = zNew;
209670 p->zJson[p->nJson] = 0;
209671 }else{
209672 sqlite3RCStrRef(p->zJson);
209673 }
209674 p->bJsonIsRCStr = 1;
209675 rc = jsonCacheInsert(ctx, p);
209676 if( rc==SQLITE_NOMEM7 ) goto json_pfa_oom;
209677 if( flgs & JSON_EDITABLE0x01 ){
209678 pFromCache = p;
209679 p = 0;
209680 goto rebuild_from_cache;
209681 }
209682 }
209683 return p;
209684
209685json_pfa_malformed:
209686 if( flgs & JSON_KEEPERROR0x02 ){
209687 p->nErr = 1;
209688 return p;
209689 }else{
209690 jsonParseFree(p);
209691 sqlite3_result_error(ctx, "malformed JSON", -1);
209692 return 0;
209693 }
209694
209695json_pfa_oom:
209696 jsonParseFree(pFromCache);
209697 jsonParseFree(p);
209698 sqlite3_result_error_nomem(ctx);
209699 return 0;
209700}
209701
209702/*
209703** Make the return value of a JSON function either the raw JSONB blob
209704** or make it JSON text, depending on whether the JSON_BLOB flag is
209705** set on the function.
209706*/
209707static void jsonReturnParse(
209708 sqlite3_context *ctx,
209709 JsonParse *p
209710){
209711 int flgs;
209712 if( p->oom ){
209713 sqlite3_result_error_nomem(ctx);
209714 return;
209715 }
209716 flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
209717 if( flgs & JSON_BLOB0x08 ){
209718 if( p->nBlobAlloc>0 && !p->bReadOnly ){
209719 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3OomClear));
209720 p->nBlobAlloc = 0;
209721 }else{
209722 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
209723 }
209724 }else{
209725 JsonString s;
209726 jsonStringInit(&s, ctx);
209727 p->delta = 0;
209728 jsonTranslateBlobToText(p, 0, &s);
209729 jsonReturnString(&s, p, ctx);
209730 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
209731 }
209732}
209733
209734/****************************************************************************
209735** SQL functions used for testing and debugging
209736****************************************************************************/
209737
209738#if SQLITE_DEBUG
209739/*
209740** Decode JSONB bytes in aBlob[] starting at iStart through but not
209741** including iEnd. Indent the
209742** content by nIndent spaces.
209743*/
209744static void jsonDebugPrintBlob(
209745 JsonParse *pParse, /* JSON content */
209746 u32 iStart, /* Start rendering here */
209747 u32 iEnd, /* Do not render this byte or any byte after this one */
209748 int nIndent, /* Indent by this many spaces */
209749 sqlite3_str *pOut /* Generate output into this sqlite3_str object */
209750){
209751 while( iStart<iEnd ){
209752 u32 i, n, nn, sz = 0;
209753 int showContent = 1;
209754 u8 x = pParse->aBlob[iStart] & 0x0f;
209755 u32 savedNBlob = pParse->nBlob;
209756 sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
209757 if( pParse->nBlobAlloc>pParse->nBlob ){
209758 pParse->nBlob = pParse->nBlobAlloc;
209759 }
209760 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
209761 if( nn==0 ) nn = 1;
209762 if( sz>0 && x<JSONB_ARRAY11 ){
209763 nn += sz;
209764 }
209765 for(i=0; i<nn; i++){
209766 sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
209767 }
209768 if( n==0 ){
209769 sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
209770 iStart = n==0 ? iStart+1 : iEnd;
209771 continue;
209772 }
209773 pParse->nBlob = savedNBlob;
209774 if( iStart+n+sz>iEnd ){
209775 iEnd = iStart+n+sz;
209776 if( iEnd>pParse->nBlob ){
209777 if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){
209778 iEnd = pParse->nBlobAlloc;
209779 }else{
209780 iEnd = pParse->nBlob;
209781 }
209782 }
209783 }
209784 sqlite3_str_appendall(pOut," <-- ");
209785 switch( x ){
209786 case JSONB_NULL0: sqlite3_str_appendall(pOut,"null"); break;
209787 case JSONB_TRUE1: sqlite3_str_appendall(pOut,"true"); break;
209788 case JSONB_FALSE2: sqlite3_str_appendall(pOut,"false"); break;
209789 case JSONB_INT3: sqlite3_str_appendall(pOut,"int"); break;
209790 case JSONB_INT54: sqlite3_str_appendall(pOut,"int5"); break;
209791 case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float"); break;
209792 case JSONB_FLOAT56: sqlite3_str_appendall(pOut,"float5"); break;
209793 case JSONB_TEXT7: sqlite3_str_appendall(pOut,"text"); break;
209794 case JSONB_TEXTJ8: sqlite3_str_appendall(pOut,"textj"); break;
209795 case JSONB_TEXT59: sqlite3_str_appendall(pOut,"text5"); break;
209796 case JSONB_TEXTRAW10: sqlite3_str_appendall(pOut,"textraw"); break;
209797 case JSONB_ARRAY11: {
209798 sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
209799 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
209800 showContent = 0;
209801 break;
209802 }
209803 case JSONB_OBJECT12: {
209804 sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
209805 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
209806 showContent = 0;
209807 break;
209808 }
209809 default: {
209810 sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
209811 showContent = 0;
209812 break;
209813 }
209814 }
209815 if( showContent ){
209816 if( sz==0 && x<=JSONB_FALSE2 ){
209817 sqlite3_str_append(pOut, "\n", 1);
209818 }else{
209819 u32 j;
209820 sqlite3_str_appendall(pOut, ": \"");
209821 for(j=iStart+n; j<iStart+n+sz; j++){
209822 u8 c = pParse->aBlob[j];
209823 if( c<0x20 || c>=0x7f ) c = '.';
209824 sqlite3_str_append(pOut, (char*)&c, 1);
209825 }
209826 sqlite3_str_append(pOut, "\"\n", 2);
209827 }
209828 }
209829 iStart += n + sz;
209830 }
209831}
209832static void jsonShowParse(JsonParse *pParse){
209833 sqlite3_str out;
209834 char zBuf[1000];
209835 if( pParse==0 ){
209836 printf("NULL pointer\n");
209837 return;
209838 }else{
209839 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
209840 printf("nBlob = %u\n", pParse->nBlob);
209841 printf("delta = %d\n", pParse->delta);
209842 if( pParse->nBlob==0 ) return;
209843 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
209844 }
209845 sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
209846 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
209847 printf("%s", sqlite3_str_value(&out));
209848 sqlite3_str_reset(&out);
209849}
209850#endif /* SQLITE_DEBUG */
209851
209852#ifdef SQLITE_DEBUG
209853/*
209854** SQL function: json_parse(JSON)
209855**
209856** Parse JSON using jsonParseFuncArg(). Return text that is a
209857** human-readable dump of the binary JSONB for the input parameter.
209858*/
209859static void jsonParseFunc(
209860 sqlite3_context *ctx,
209861 int argc,
209862 sqlite3_value **argv
209863){
209864 JsonParse *p; /* The parse */
209865 sqlite3_str out;
209866
209867 assert( argc>=1 )((void) (0));
209868 sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
209869 p = jsonParseFuncArg(ctx, argv[0], 0);
209870 if( p==0 ) return;
209871 if( argc==1 ){
209872 jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
209873 sqlite3_result_text64(ctx,out.zText,out.nChar,SQLITE_TRANSIENT((sqlite3_destructor_type)-1),SQLITE_UTF81);
209874 }else{
209875 jsonShowParse(p);
209876 }
209877 jsonParseFree(p);
209878 sqlite3_str_reset(&out);
209879}
209880#endif /* SQLITE_DEBUG */
209881
209882/****************************************************************************
209883** Scalar SQL function implementations
209884****************************************************************************/
209885
209886/*
209887** Implementation of the json_quote(VALUE) function. Return a JSON value
209888** corresponding to the SQL value input. Mostly this means putting
209889** double-quotes around strings and returning the unquoted string "null"
209890** when given a NULL input.
209891*/
209892static void jsonQuoteFunc(
209893 sqlite3_context *ctx,
209894 int argc,
209895 sqlite3_value **argv
209896){
209897 JsonString jx;
209898 UNUSED_PARAMETER(argc)(void)(argc);
209899
209900 jsonStringInit(&jx, ctx);
209901 jsonAppendSqlValue(&jx, argv[0]);
209902 jsonReturnString(&jx, 0, 0);
209903 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
209904}
209905
209906/*
209907** Implementation of the json_array(VALUE,...) function. Return a JSON
209908** array that contains all values given in arguments. Or if any argument
209909** is a BLOB, throw an error.
209910*/
209911static void jsonArrayFunc(
209912 sqlite3_context *ctx,
209913 int argc,
209914 sqlite3_value **argv
209915){
209916 int i;
209917 JsonString jx;
209918
209919 jsonStringInit(&jx, ctx);
209920 jsonAppendChar(&jx, '[');
209921 for(i=0; i<argc; i++){
209922 jsonAppendSeparator(&jx);
209923 jsonAppendSqlValue(&jx, argv[i]);
209924 }
209925 jsonAppendChar(&jx, ']');
209926 jsonReturnString(&jx, 0, 0);
209927 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
209928}
209929
209930/*
209931** json_array_length(JSON)
209932** json_array_length(JSON, PATH)
209933**
209934** Return the number of elements in the top-level JSON array.
209935** Return 0 if the input is not a well-formed JSON array.
209936*/
209937static void jsonArrayLengthFunc(
209938 sqlite3_context *ctx,
209939 int argc,
209940 sqlite3_value **argv
209941){
209942 JsonParse *p; /* The parse */
209943 sqlite3_int64 cnt = 0;
209944 u32 i;
209945 u8 eErr = 0;
209946
209947 p = jsonParseFuncArg(ctx, argv[0], 0);
209948 if( p==0 ) return;
209949 if( argc==2 ){
209950 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
209951 if( zPath==0 ){
209952 jsonParseFree(p);
209953 return;
209954 }
209955 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
209956 if( JSON_LOOKUP_ISERROR(i)((i)>=0xfffffffd) ){
209957 if( i==JSON_LOOKUP_NOTFOUND0xfffffffe ){
209958 /* no-op */
209959 }else if( i==JSON_LOOKUP_PATHERROR0xfffffffd ){
209960 jsonBadPathError(ctx, zPath);
209961 }else{
209962 sqlite3_result_error(ctx, "malformed JSON", -1);
209963 }
209964 eErr = 1;
209965 i = 0;
209966 }
209967 }else{
209968 i = 0;
209969 }
209970 if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY11 ){
209971 cnt = jsonbArrayCount(p, i);
209972 }
209973 if( !eErr ) sqlite3_result_int64(ctx, cnt);
209974 jsonParseFree(p);
209975}
209976
209977/* True if the string is all alphanumerics and underscores */
209978static int jsonAllAlphanum(const char *z, int n){
209979 int i;
209980 for(i=0; i<n && (sqlite3Isalnum(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x06) || z[i]=='_'); i++){}
209981 return i==n;
209982}
209983
209984/*
209985** json_extract(JSON, PATH, ...)
209986** "->"(JSON,PATH)
209987** "->>"(JSON,PATH)
209988**
209989** Return the element described by PATH. Return NULL if that PATH element
209990** is not found.
209991**
209992** If JSON_JSON is set or if more that one PATH argument is supplied then
209993** always return a JSON representation of the result. If JSON_SQL is set,
209994** then always return an SQL representation of the result. If neither flag
209995** is present and argc==2, then return JSON for objects and arrays and SQL
209996** for all other values.
209997**
209998** When multiple PATH arguments are supplied, the result is a JSON array
209999** containing the result of each PATH.
210000**
210001** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
210002** compatibility with PG.
210003*/
210004static void jsonExtractFunc(
210005 sqlite3_context *ctx,
210006 int argc,
210007 sqlite3_value **argv
210008){
210009 JsonParse *p = 0; /* The parse */
210010 int flags; /* Flags associated with the function */
210011 int i; /* Loop counter */
210012 JsonString jx; /* String for array result */
210013
210014 if( argc<2 ) return;
210015 p = jsonParseFuncArg(ctx, argv[0], 0);
210016 if( p==0 ) return;
210017 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
210018 jsonStringInit(&jx, ctx);
210019 if( argc>2 ){
210020 jsonAppendChar(&jx, '[');
210021 }
210022 for(i=1; i<argc; i++){
210023 /* With a single PATH argument */
210024 const char *zPath = (const char*)sqlite3_value_text(argv[i]);
210025 int nPath;
210026 u32 j;
210027 if( zPath==0 ) goto json_extract_error;
210028 nPath = sqlite3Strlen30(zPath);
210029 if( zPath[0]=='$' ){
210030 j = jsonLookupStep(p, 0, zPath+1, 0);
210031 }else if( (flags & JSON_ABPATH0x03) ){
210032 /* The -> and ->> operators accept abbreviated PATH arguments. This
210033 ** is mostly for compatibility with PostgreSQL, but also for
210034 ** convenience.
210035 **
210036 ** NUMBER ==> $[NUMBER] // PG compatible
210037 ** LABEL ==> $.LABEL // PG compatible
210038 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
210039 **
210040 ** Updated 2024-05-27: If the NUMBER is negative, then PG counts from
210041 ** the right of the array. Hence for negative NUMBER:
210042 **
210043 ** NUMBER ==> $[#NUMBER] // PG compatible
210044 */
210045 jsonStringInit(&jx, ctx);
210046 if( sqlite3_value_type(argv[i])==SQLITE_INTEGER1 ){
210047 jsonAppendRawNZ(&jx, "[", 1);
210048 if( zPath[0]=='-' ) jsonAppendRawNZ(&jx,"#",1);
210049 jsonAppendRaw(&jx, zPath, nPath);
210050 jsonAppendRawNZ(&jx, "]", 2);
210051 }else if( jsonAllAlphanum(zPath, nPath) ){
210052 jsonAppendRawNZ(&jx, ".", 1);
210053 jsonAppendRaw(&jx, zPath, nPath);
210054 }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){
210055 jsonAppendRaw(&jx, zPath, nPath);
210056 }else{
210057 jsonAppendRawNZ(&jx, ".\"", 2);
210058 jsonAppendRaw(&jx, zPath, nPath);
210059 jsonAppendRawNZ(&jx, "\"", 1);
210060 }
210061 jsonStringTerminate(&jx);
210062 j = jsonLookupStep(p, 0, jx.zBuf, 0);
210063 jsonStringReset(&jx);
210064 }else{
210065 jsonBadPathError(ctx, zPath);
210066 goto json_extract_error;
210067 }
210068 if( j<p->nBlob ){
210069 if( argc==2 ){
210070 if( flags & JSON_JSON0x01 ){
210071 jsonStringInit(&jx, ctx);
210072 jsonTranslateBlobToText(p, j, &jx);
210073 jsonReturnString(&jx, 0, 0);
210074 jsonStringReset(&jx);
210075 assert( (flags & JSON_BLOB)==0 )((void) (0));
210076 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
210077 }else{
210078 jsonReturnFromBlob(p, j, ctx, 0);
210079 if( (flags & (JSON_SQL0x02|JSON_BLOB0x08))==0
210080 && (p->aBlob[j]&0x0f)>=JSONB_ARRAY11
210081 ){
210082 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
210083 }
210084 }
210085 }else{
210086 jsonAppendSeparator(&jx);
210087 jsonTranslateBlobToText(p, j, &jx);
210088 }
210089 }else if( j==JSON_LOOKUP_NOTFOUND0xfffffffe ){
210090 if( argc==2 ){
210091 goto json_extract_error; /* Return NULL if not found */
210092 }else{
210093 jsonAppendSeparator(&jx);
210094 jsonAppendRawNZ(&jx, "null", 4);
210095 }
210096 }else if( j==JSON_LOOKUP_ERROR0xffffffff ){
210097 sqlite3_result_error(ctx, "malformed JSON", -1);
210098 goto json_extract_error;
210099 }else{
210100 jsonBadPathError(ctx, zPath);
210101 goto json_extract_error;
210102 }
210103 }
210104 if( argc>2 ){
210105 jsonAppendChar(&jx, ']');
210106 jsonReturnString(&jx, 0, 0);
210107 if( (flags & JSON_BLOB0x08)==0 ){
210108 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
210109 }
210110 }
210111json_extract_error:
210112 jsonStringReset(&jx);
210113 jsonParseFree(p);
210114 return;
210115}
210116
210117/*
210118** Return codes for jsonMergePatch()
210119*/
210120#define JSON_MERGE_OK0 0 /* Success */
210121#define JSON_MERGE_BADTARGET1 1 /* Malformed TARGET blob */
210122#define JSON_MERGE_BADPATCH2 2 /* Malformed PATCH blob */
210123#define JSON_MERGE_OOM3 3 /* Out-of-memory condition */
210124
210125/*
210126** RFC-7396 MergePatch for two JSONB blobs.
210127**
210128** pTarget is the target. pPatch is the patch. The target is updated
210129** in place. The patch is read-only.
210130**
210131** The original RFC-7396 algorithm is this:
210132**
210133** define MergePatch(Target, Patch):
210134** if Patch is an Object:
210135** if Target is not an Object:
210136** Target = {} # Ignore the contents and set it to an empty Object
210137** for each Name/Value pair in Patch:
210138** if Value is null:
210139** if Name exists in Target:
210140** remove the Name/Value pair from Target
210141** else:
210142** Target[Name] = MergePatch(Target[Name], Value)
210143** return Target
210144** else:
210145** return Patch
210146**
210147** Here is an equivalent algorithm restructured to show the actual
210148** implementation:
210149**
210150** 01 define MergePatch(Target, Patch):
210151** 02 if Patch is not an Object:
210152** 03 return Patch
210153** 04 else: // if Patch is an Object
210154** 05 if Target is not an Object:
210155** 06 Target = {}
210156** 07 for each Name/Value pair in Patch:
210157** 08 if Name exists in Target:
210158** 09 if Value is null:
210159** 10 remove the Name/Value pair from Target
210160** 11 else
210161** 12 Target[name] = MergePatch(Target[Name], Value)
210162** 13 else if Value is not NULL:
210163** 14 if Value is not an Object:
210164** 15 Target[name] = Value
210165** 16 else:
210166** 17 Target[name] = MergePatch('{}',value)
210167** 18 return Target
210168** |
210169** ^---- Line numbers referenced in comments in the implementation
210170*/
210171static int jsonMergePatch(
210172 JsonParse *pTarget, /* The JSON parser that contains the TARGET */
210173 u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */
210174 const JsonParse *pPatch, /* The PATCH */
210175 u32 iPatch /* Index of PATCH in pPatch->aBlob[] */
210176){
210177 u8 x; /* Type of a single node */
210178 u32 n, sz=0; /* Return values from jsonbPayloadSize() */
210179 u32 iTCursor; /* Cursor position while scanning the target object */
210180 u32 iTStart; /* First label in the target object */
210181 u32 iTEndBE; /* Original first byte past end of target, before edit */
210182 u32 iTEnd; /* Current first byte past end of target */
210183 u8 eTLabel; /* Node type of the target label */
210184 u32 iTLabel = 0; /* Index of the label */
210185 u32 nTLabel = 0; /* Header size in bytes for the target label */
210186 u32 szTLabel = 0; /* Size of the target label payload */
210187 u32 iTValue = 0; /* Index of the target value */
210188 u32 nTValue = 0; /* Header size of the target value */
210189 u32 szTValue = 0; /* Payload size for the target value */
210190
210191 u32 iPCursor; /* Cursor position while scanning the patch */
210192 u32 iPEnd; /* First byte past the end of the patch */
210193 u8 ePLabel; /* Node type of the patch label */
210194 u32 iPLabel; /* Start of patch label */
210195 u32 nPLabel; /* Size of header on the patch label */
210196 u32 szPLabel; /* Payload size of the patch label */
210197 u32 iPValue; /* Start of patch value */
210198 u32 nPValue; /* Header size for the patch value */
210199 u32 szPValue; /* Payload size of the patch value */
210200
210201 assert( iTarget>=0 && iTarget<pTarget->nBlob )((void) (0));
210202 assert( iPatch>=0 && iPatch<pPatch->nBlob )((void) (0));
210203 x = pPatch->aBlob[iPatch] & 0x0f;
210204 if( x!=JSONB_OBJECT12 ){ /* Algorithm line 02 */
210205 u32 szPatch; /* Total size of the patch, header+payload */
210206 u32 szTarget; /* Total size of the target, header+payload */
210207 n = jsonbPayloadSize(pPatch, iPatch, &sz);
210208 szPatch = n+sz;
210209 sz = 0;
210210 n = jsonbPayloadSize(pTarget, iTarget, &sz);
210211 szTarget = n+sz;
210212 jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch);
210213 return pTarget->oom ? JSON_MERGE_OOM3 : JSON_MERGE_OK0; /* Line 03 */
210214 }
210215 x = pTarget->aBlob[iTarget] & 0x0f;
210216 if( x!=JSONB_OBJECT12 ){ /* Algorithm line 05 */
210217 n = jsonbPayloadSize(pTarget, iTarget, &sz);
210218 jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0);
210219 x = pTarget->aBlob[iTarget];
210220 pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT12;
210221 }
210222 n = jsonbPayloadSize(pPatch, iPatch, &sz);
210223 if( NEVER(n==0)(n==0) ) return JSON_MERGE_BADPATCH2;
210224 iPCursor = iPatch+n;
210225 iPEnd = iPCursor+sz;
210226 n = jsonbPayloadSize(pTarget, iTarget, &sz);
210227 if( NEVER(n==0)(n==0) ) return JSON_MERGE_BADTARGET1;
210228 iTStart = iTarget+n;
210229 iTEndBE = iTStart+sz;
210230
210231 while( iPCursor<iPEnd ){ /* Algorithm line 07 */
210232 iPLabel = iPCursor;
210233 ePLabel = pPatch->aBlob[iPCursor] & 0x0f;
210234 if( ePLabel<JSONB_TEXT7 || ePLabel>JSONB_TEXTRAW10 ){
210235 return JSON_MERGE_BADPATCH2;
210236 }
210237 nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel);
210238 if( nPLabel==0 ) return JSON_MERGE_BADPATCH2;
210239 iPValue = iPCursor + nPLabel + szPLabel;
210240 if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH2;
210241 nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue);
210242 if( nPValue==0 ) return JSON_MERGE_BADPATCH2;
210243 iPCursor = iPValue + nPValue + szPValue;
210244 if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH2;
210245
210246 iTCursor = iTStart;
210247 iTEnd = iTEndBE + pTarget->delta;
210248 while( iTCursor<iTEnd ){
210249 int isEqual; /* true if the patch and target labels match */
210250 iTLabel = iTCursor;
210251 eTLabel = pTarget->aBlob[iTCursor] & 0x0f;
210252 if( eTLabel<JSONB_TEXT7 || eTLabel>JSONB_TEXTRAW10 ){
210253 return JSON_MERGE_BADTARGET1;
210254 }
210255 nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel);
210256 if( nTLabel==0 ) return JSON_MERGE_BADTARGET1;
210257 iTValue = iTLabel + nTLabel + szTLabel;
210258 if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET1;
210259 nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue);
210260 if( nTValue==0 ) return JSON_MERGE_BADTARGET1;
210261 if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET1;
210262 isEqual = jsonLabelCompare(
210263 (const char*)&pPatch->aBlob[iPLabel+nPLabel],
210264 szPLabel,
210265 (ePLabel==JSONB_TEXT7 || ePLabel==JSONB_TEXTRAW10),
210266 (const char*)&pTarget->aBlob[iTLabel+nTLabel],
210267 szTLabel,
210268 (eTLabel==JSONB_TEXT7 || eTLabel==JSONB_TEXTRAW10));
210269 if( isEqual ) break;
210270 iTCursor = iTValue + nTValue + szTValue;
210271 }
210272 x = pPatch->aBlob[iPValue] & 0x0f;
210273 if( iTCursor<iTEnd ){
210274 /* A match was found. Algorithm line 08 */
210275 if( x==0 ){
210276 /* Patch value is NULL. Algorithm line 09 */
210277 jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue, 0,0);
210278 /* vvvvvv----- No OOM on a delete-only edit */
210279 if( NEVER(pTarget->oom)(pTarget->oom) ) return JSON_MERGE_OOM3;
210280 }else{
210281 /* Algorithm line 12 */
210282 int rc, savedDelta = pTarget->delta;
210283 pTarget->delta = 0;
210284 rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue);
210285 if( rc ) return rc;
210286 pTarget->delta += savedDelta;
210287 }
210288 }else if( x>0 ){ /* Algorithm line 13 */
210289 /* No match and patch value is not NULL */
210290 u32 szNew = szPLabel+nPLabel;
210291 if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT12 ){ /* Line 14 */
210292 jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew);
210293 if( pTarget->oom ) return JSON_MERGE_OOM3;
210294 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
210295 memcpy(&pTarget->aBlob[iTEnd+szNew],
210296 &pPatch->aBlob[iPValue], szPValue+nPValue);
210297 }else{
210298 int rc, savedDelta;
210299 jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
210300 if( pTarget->oom ) return JSON_MERGE_OOM3;
210301 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
210302 pTarget->aBlob[iTEnd+szNew] = 0x00;
210303 savedDelta = pTarget->delta;
210304 pTarget->delta = 0;
210305 rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue);
210306 if( rc ) return rc;
210307 pTarget->delta += savedDelta;
210308 }
210309 }
210310 }
210311 if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget);
210312 return pTarget->oom ? JSON_MERGE_OOM3 : JSON_MERGE_OK0;
210313}
210314
210315
210316/*
210317** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
210318** object that is the result of running the RFC 7396 MergePatch() algorithm
210319** on the two arguments.
210320*/
210321static void jsonPatchFunc(
210322 sqlite3_context *ctx,
210323 int argc,
210324 sqlite3_value **argv
210325){
210326 JsonParse *pTarget; /* The TARGET */
210327 JsonParse *pPatch; /* The PATCH */
210328 int rc; /* Result code */
210329
210330 UNUSED_PARAMETER(argc)(void)(argc);
210331 assert( argc==2 )((void) (0));
210332 pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE0x01);
210333 if( pTarget==0 ) return;
210334 pPatch = jsonParseFuncArg(ctx, argv[1], 0);
210335 if( pPatch ){
210336 rc = jsonMergePatch(pTarget, 0, pPatch, 0);
210337 if( rc==JSON_MERGE_OK0 ){
210338 jsonReturnParse(ctx, pTarget);
210339 }else if( rc==JSON_MERGE_OOM3 ){
210340 sqlite3_result_error_nomem(ctx);
210341 }else{
210342 sqlite3_result_error(ctx, "malformed JSON", -1);
210343 }
210344 jsonParseFree(pPatch);
210345 }
210346 jsonParseFree(pTarget);
210347}
210348
210349
210350/*
210351** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
210352** object that contains all name/value given in arguments. Or if any name
210353** is not a string or if any value is a BLOB, throw an error.
210354*/
210355static void jsonObjectFunc(
210356 sqlite3_context *ctx,
210357 int argc,
210358 sqlite3_value **argv
210359){
210360 int i;
210361 JsonString jx;
210362 const char *z;
210363 u32 n;
210364
210365 if( argc&1 ){
210366 sqlite3_result_error(ctx, "json_object() requires an even number "
210367 "of arguments", -1);
210368 return;
210369 }
210370 jsonStringInit(&jx, ctx);
210371 jsonAppendChar(&jx, '{');
210372 for(i=0; i<argc; i+=2){
210373 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT3 ){
210374 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
210375 jsonStringReset(&jx);
210376 return;
210377 }
210378 jsonAppendSeparator(&jx);
210379 z = (const char*)sqlite3_value_text(argv[i]);
210380 n = sqlite3_value_bytes(argv[i]);
210381 jsonAppendString(&jx, z, n);
210382 jsonAppendChar(&jx, ':');
210383 jsonAppendSqlValue(&jx, argv[i+1]);
210384 }
210385 jsonAppendChar(&jx, '}');
210386 jsonReturnString(&jx, 0, 0);
210387 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
210388}
210389
210390
210391/*
210392** json_remove(JSON, PATH, ...)
210393**
210394** Remove the named elements from JSON and return the result. malformed
210395** JSON or PATH arguments result in an error.
210396*/
210397static void jsonRemoveFunc(
210398 sqlite3_context *ctx,
210399 int argc,
210400 sqlite3_value **argv
210401){
210402 JsonParse *p; /* The parse */
210403 const char *zPath = 0; /* Path of element to be removed */
210404 int i; /* Loop counter */
210405 u32 rc; /* Subroutine return code */
210406
210407 if( argc<1 ) return;
210408 p = jsonParseFuncArg(ctx, argv[0], argc>1 ? JSON_EDITABLE0x01 : 0);
210409 if( p==0 ) return;
210410 for(i=1; i<argc; i++){
210411 zPath = (const char*)sqlite3_value_text(argv[i]);
210412 if( zPath==0 ){
210413 goto json_remove_done;
210414 }
210415 if( zPath[0]!='$' ){
210416 goto json_remove_patherror;
210417 }
210418 if( zPath[1]==0 ){
210419 /* json_remove(j,'$') returns NULL */
210420 goto json_remove_done;
210421 }
210422 p->eEdit = JEDIT_DEL1;
210423 p->delta = 0;
210424 rc = jsonLookupStep(p, 0, zPath+1, 0);
210425 if( JSON_LOOKUP_ISERROR(rc)((rc)>=0xfffffffd) ){
210426 if( rc==JSON_LOOKUP_NOTFOUND0xfffffffe ){
210427 continue; /* No-op */
210428 }else if( rc==JSON_LOOKUP_PATHERROR0xfffffffd ){
210429 jsonBadPathError(ctx, zPath);
210430 }else{
210431 sqlite3_result_error(ctx, "malformed JSON", -1);
210432 }
210433 goto json_remove_done;
210434 }
210435 }
210436 jsonReturnParse(ctx, p);
210437 jsonParseFree(p);
210438 return;
210439
210440json_remove_patherror:
210441 jsonBadPathError(ctx, zPath);
210442
210443json_remove_done:
210444 jsonParseFree(p);
210445 return;
210446}
210447
210448/*
210449** json_replace(JSON, PATH, VALUE, ...)
210450**
210451** Replace the value at PATH with VALUE. If PATH does not already exist,
210452** this routine is a no-op. If JSON or PATH is malformed, throw an error.
210453*/
210454static void jsonReplaceFunc(
210455 sqlite3_context *ctx,
210456 int argc,
210457 sqlite3_value **argv
210458){
210459 if( argc<1 ) return;
210460 if( (argc&1)==0 ) {
210461 jsonWrongNumArgs(ctx, "replace");
210462 return;
210463 }
210464 jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL2);
210465}
210466
210467
210468/*
210469** json_set(JSON, PATH, VALUE, ...)
210470**
210471** Set the value at PATH to VALUE. Create the PATH if it does not already
210472** exist. Overwrite existing values that do exist.
210473** If JSON or PATH is malformed, throw an error.
210474**
210475** json_insert(JSON, PATH, VALUE, ...)
210476**
210477** Create PATH and initialize it to VALUE. If PATH already exists, this
210478** routine is a no-op. If JSON or PATH is malformed, throw an error.
210479*/
210480static void jsonSetFunc(
210481 sqlite3_context *ctx,
210482 int argc,
210483 sqlite3_value **argv
210484){
210485
210486 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
210487 int bIsSet = (flags&JSON_ISSET0x04)!=0;
210488
210489 if( argc<1 ) return;
210490 if( (argc&1)==0 ) {
210491 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
210492 return;
210493 }
210494 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET4 : JEDIT_INS3);
210495}
210496
210497/*
210498** json_type(JSON)
210499** json_type(JSON, PATH)
210500**
210501** Return the top-level "type" of a JSON string. json_type() raises an
210502** error if either the JSON or PATH inputs are not well-formed.
210503*/
210504static void jsonTypeFunc(
210505 sqlite3_context *ctx,
210506 int argc,
210507 sqlite3_value **argv
210508){
210509 JsonParse *p; /* The parse */
210510 const char *zPath = 0;
210511 u32 i;
210512
210513 p = jsonParseFuncArg(ctx, argv[0], 0);
210514 if( p==0 ) return;
210515 if( argc==2 ){
210516 zPath = (const char*)sqlite3_value_text(argv[1]);
210517 if( zPath==0 ) goto json_type_done;
210518 if( zPath[0]!='$' ){
210519 jsonBadPathError(ctx, zPath);
210520 goto json_type_done;
210521 }
210522 i = jsonLookupStep(p, 0, zPath+1, 0);
210523 if( JSON_LOOKUP_ISERROR(i)((i)>=0xfffffffd) ){
210524 if( i==JSON_LOOKUP_NOTFOUND0xfffffffe ){
210525 /* no-op */
210526 }else if( i==JSON_LOOKUP_PATHERROR0xfffffffd ){
210527 jsonBadPathError(ctx, zPath);
210528 }else{
210529 sqlite3_result_error(ctx, "malformed JSON", -1);
210530 }
210531 goto json_type_done;
210532 }
210533 }else{
210534 i = 0;
210535 }
210536 sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
210537json_type_done:
210538 jsonParseFree(p);
210539}
210540
210541/*
210542** json_pretty(JSON)
210543** json_pretty(JSON, INDENT)
210544**
210545** Return text that is a pretty-printed rendering of the input JSON.
210546** If the argument is not valid JSON, return NULL.
210547**
210548** The INDENT argument is text that is used for indentation. If omitted,
210549** it defaults to four spaces (the same as PostgreSQL).
210550*/
210551static void jsonPrettyFunc(
210552 sqlite3_context *ctx,
210553 int argc,
210554 sqlite3_value **argv
210555){
210556 JsonString s; /* The output string */
210557 JsonPretty x; /* Pretty printing context */
210558
210559 memset(&x, 0, sizeof(x));
210560 x.pParse = jsonParseFuncArg(ctx, argv[0], 0);
210561 if( x.pParse==0 ) return;
210562 x.pOut = &s;
210563 jsonStringInit(&s, ctx);
210564 if( argc==1 || (x.zIndent = (const char*)sqlite3_value_text(argv[1]))==0 ){
210565 x.zIndent = " ";
210566 x.szIndent = 4;
210567 }else{
210568 x.szIndent = (u32)strlen(x.zIndent);
210569 }
210570 jsonTranslateBlobToPrettyText(&x, 0);
210571 jsonReturnString(&s, 0, 0);
210572 jsonParseFree(x.pParse);
210573}
210574
210575/*
210576** json_valid(JSON)
210577** json_valid(JSON, FLAGS)
210578**
210579** Check the JSON argument to see if it is well-formed. The FLAGS argument
210580** encodes the various constraints on what is meant by "well-formed":
210581**
210582** 0x01 Canonical RFC-8259 JSON text
210583** 0x02 JSON text with optional JSON-5 extensions
210584** 0x04 Superficially appears to be JSONB
210585** 0x08 Strictly well-formed JSONB
210586**
210587** If the FLAGS argument is omitted, it defaults to 1. Useful values for
210588** FLAGS include:
210589**
210590** 1 Strict canonical JSON text
210591** 2 JSON text perhaps with JSON-5 extensions
210592** 4 Superficially appears to be JSONB
210593** 5 Canonical JSON text or superficial JSONB
210594** 6 JSON-5 text or superficial JSONB
210595** 8 Strict JSONB
210596** 9 Canonical JSON text or strict JSONB
210597** 10 JSON-5 text or strict JSONB
210598**
210599** Other flag combinations are redundant. For example, every canonical
210600** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
210601** are the same. Similarly, any input that passes a strict JSONB validation
210602** will also pass the superficial validation so 12 through 15 are the same
210603** as 8 through 11 respectively.
210604**
210605** This routine runs in linear time to validate text and when doing strict
210606** JSONB validation. Superficial JSONB validation is constant time,
210607** assuming the BLOB is already in memory. The performance advantage
210608** of superficial JSONB validation is why that option is provided.
210609** Application developers can choose to do fast superficial validation or
210610** slower strict validation, according to their specific needs.
210611**
210612** Only the lower four bits of the FLAGS argument are currently used.
210613** Higher bits are reserved for future expansion. To facilitate
210614** compatibility, the current implementation raises an error if any bit
210615** in FLAGS is set other than the lower four bits.
210616**
210617** The original circa 2015 implementation of the JSON routines in
210618** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
210619** function only accepted one argument. That is why the default value
210620** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
210621** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS
210622** argument was added when the JSON routines were extended to support
210623** JSON5-like extensions and binary JSONB stored in BLOBs.
210624**
210625** Return Values:
210626**
210627** * Raise an error if FLAGS is outside the range of 1 to 15.
210628** * Return NULL if the input is NULL
210629** * Return 1 if the input is well-formed.
210630** * Return 0 if the input is not well-formed.
210631*/
210632static void jsonValidFunc(
210633 sqlite3_context *ctx,
210634 int argc,
210635 sqlite3_value **argv
210636){
210637 JsonParse *p; /* The parse */
210638 u8 flags = 1;
210639 u8 res = 0;
210640 if( argc==2 ){
210641 i64 f = sqlite3_value_int64(argv[1]);
210642 if( f<1 || f>15 ){
210643 sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be"
210644 " between 1 and 15", -1);
210645 return;
210646 }
210647 flags = f & 0x0f;
210648 }
210649 switch( sqlite3_value_type(argv[0]) ){
210650 case SQLITE_NULL5: {
210651#ifdef SQLITE_LEGACY_JSON_VALID
210652 /* Incorrect legacy behavior was to return FALSE for a NULL input */
210653 sqlite3_result_int(ctx, 0);
210654#endif
210655 return;
210656 }
210657 case SQLITE_BLOB4: {
210658 if( jsonFuncArgMightBeBinary(argv[0]) ){
210659 if( flags & 0x04 ){
210660 /* Superficial checking only - accomplished by the
210661 ** jsonFuncArgMightBeBinary() call above. */
210662 res = 1;
210663 }else if( flags & 0x08 ){
210664 /* Strict checking. Check by translating BLOB->TEXT->BLOB. If
210665 ** no errors occur, call that a "strict check". */
210666 JsonParse px;
210667 u32 iErr;
210668 memset(&px, 0, sizeof(px));
210669 px.aBlob = (u8*)sqlite3_value_blob(argv[0]);
210670 px.nBlob = sqlite3_value_bytes(argv[0]);
210671 iErr = jsonbValidityCheck(&px, 0, px.nBlob, 1);
210672 res = iErr==0;
210673 }
210674 break;
210675 }
210676 /* Fall through into interpreting the input as text. See note
210677 ** above at tag-20240123-a. */
210678 /* no break */ deliberate_fall_through__attribute__((fallthrough));
210679 }
210680 default: {
210681 JsonParse px;
210682 if( (flags & 0x3)==0 ) break;
210683 memset(&px, 0, sizeof(px));
210684
210685 p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR0x02);
210686 if( p ){
210687 if( p->oom ){
210688 sqlite3_result_error_nomem(ctx);
210689 }else if( p->nErr ){
210690 /* no-op */
210691 }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){
210692 res = 1;
210693 }
210694 jsonParseFree(p);
210695 }else{
210696 sqlite3_result_error_nomem(ctx);
210697 }
210698 break;
210699 }
210700 }
210701 sqlite3_result_int(ctx, res);
210702}
210703
210704/*
210705** json_error_position(JSON)
210706**
210707** If the argument is NULL, return NULL
210708**
210709** If the argument is BLOB, do a full validity check and return non-zero
210710** if the check fails. The return value is the approximate 1-based offset
210711** to the byte of the element that contains the first error.
210712**
210713** Otherwise interpret the argument is TEXT (even if it is numeric) and
210714** return the 1-based character position for where the parser first recognized
210715** that the input was not valid JSON, or return 0 if the input text looks
210716** ok. JSON-5 extensions are accepted.
210717*/
210718static void jsonErrorFunc(
210719 sqlite3_context *ctx,
210720 int argc,
210721 sqlite3_value **argv
210722){
210723 i64 iErrPos = 0; /* Error position to be returned */
210724 JsonParse s;
210725
210726 assert( argc==1 )((void) (0));
210727 UNUSED_PARAMETER(argc)(void)(argc);
210728 memset(&s, 0, sizeof(s));
210729 s.db = sqlite3_context_db_handle(ctx);
210730 if( jsonFuncArgMightBeBinary(argv[0]) ){
210731 s.aBlob = (u8*)sqlite3_value_blob(argv[0]);
210732 s.nBlob = sqlite3_value_bytes(argv[0]);
210733 iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1);
210734 }else{
210735 s.zJson = (char*)sqlite3_value_text(argv[0]);
210736 if( s.zJson==0 ) return; /* NULL input or OOM */
210737 s.nJson = sqlite3_value_bytes(argv[0]);
210738 if( jsonConvertTextToBlob(&s,0) ){
210739 if( s.oom ){
210740 iErrPos = -1;
210741 }else{
210742 /* Convert byte-offset s.iErr into a character offset */
210743 u32 k;
210744 assert( s.zJson!=0 )((void) (0)); /* Because s.oom is false */
210745 for(k=0; k<s.iErr && ALWAYS(s.zJson[k])(s.zJson[k]); k++){
210746 if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
210747 }
210748 iErrPos++;
210749 }
210750 }
210751 }
210752 jsonParseReset(&s);
210753 if( iErrPos<0 ){
210754 sqlite3_result_error_nomem(ctx);
210755 }else{
210756 sqlite3_result_int64(ctx, iErrPos);
210757 }
210758}
210759
210760/****************************************************************************
210761** Aggregate SQL function implementations
210762****************************************************************************/
210763/*
210764** json_group_array(VALUE)
210765**
210766** Return a JSON array composed of all values in the aggregate.
210767*/
210768static void jsonArrayStep(
210769 sqlite3_context *ctx,
210770 int argc,
210771 sqlite3_value **argv
210772){
210773 JsonString *pStr;
210774 UNUSED_PARAMETER(argc)(void)(argc);
210775 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
210776 if( pStr ){
210777 if( pStr->zBuf==0 ){
210778 jsonStringInit(pStr, ctx);
210779 jsonAppendChar(pStr, '[');
210780 }else if( pStr->nUsed>1 ){
210781 jsonAppendChar(pStr, ',');
210782 }
210783 pStr->pCtx = ctx;
210784 jsonAppendSqlValue(pStr, argv[0]);
210785 }
210786}
210787static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
210788 JsonString *pStr;
210789 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
210790 if( pStr ){
210791 int flags;
210792 pStr->pCtx = ctx;
210793 jsonAppendChar(pStr, ']');
210794 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
210795 if( pStr->eErr ){
210796 jsonReturnString(pStr, 0, 0);
210797 return;
210798 }else if( flags & JSON_BLOB0x08 ){
210799 jsonReturnStringAsBlob(pStr);
210800 if( isFinal ){
210801 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
210802 }else{
210803 jsonStringTrimOneChar(pStr);
210804 }
210805 return;
210806 }else if( isFinal ){
210807 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
210808 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) :
210809 sqlite3RCStrUnref);
210810 pStr->bStatic = 1;
210811 }else{
210812 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
210813 jsonStringTrimOneChar(pStr);
210814 }
210815 }else{
210816 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
210817 }
210818 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
210819}
210820static void jsonArrayValue(sqlite3_context *ctx){
210821 jsonArrayCompute(ctx, 0);
210822}
210823static void jsonArrayFinal(sqlite3_context *ctx){
210824 jsonArrayCompute(ctx, 1);
210825}
210826
210827#ifndef SQLITE_OMIT_WINDOWFUNC
210828/*
210829** This method works for both json_group_array() and json_group_object().
210830** It works by removing the first element of the group by searching forward
210831** to the first comma (",") that is not within a string and deleting all
210832** text through that comma.
210833*/
210834static void jsonGroupInverse(
210835 sqlite3_context *ctx,
210836 int argc,
210837 sqlite3_value **argv
210838){
210839 unsigned int i;
210840 int inStr = 0;
210841 int nNest = 0;
210842 char *z;
210843 char c;
210844 JsonString *pStr;
210845 UNUSED_PARAMETER(argc)(void)(argc);
210846 UNUSED_PARAMETER(argv)(void)(argv);
210847 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
210848#ifdef NEVER
210849 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
210850 ** always have been called to initialize it */
210851 if( NEVER(!pStr)(!pStr) ) return;
210852#endif
210853 z = pStr->zBuf;
210854 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
210855 if( c=='"' ){
210856 inStr = !inStr;
210857 }else if( c=='\\' ){
210858 i++;
210859 }else if( !inStr ){
210860 if( c=='{' || c=='[' ) nNest++;
210861 if( c=='}' || c==']' ) nNest--;
210862 }
210863 }
210864 if( i<pStr->nUsed ){
210865 pStr->nUsed -= i;
210866 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
210867 z[pStr->nUsed] = 0;
210868 }else{
210869 pStr->nUsed = 1;
210870 }
210871}
210872#else
210873# define jsonGroupInverse 0
210874#endif
210875
210876
210877/*
210878** json_group_obj(NAME,VALUE)
210879**
210880** Return a JSON object composed of all names and values in the aggregate.
210881*/
210882static void jsonObjectStep(
210883 sqlite3_context *ctx,
210884 int argc,
210885 sqlite3_value **argv
210886){
210887 JsonString *pStr;
210888 const char *z;
210889 u32 n;
210890 UNUSED_PARAMETER(argc)(void)(argc);
210891 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
210892 if( pStr ){
210893 if( pStr->zBuf==0 ){
210894 jsonStringInit(pStr, ctx);
210895 jsonAppendChar(pStr, '{');
210896 }else if( pStr->nUsed>1 ){
210897 jsonAppendChar(pStr, ',');
210898 }
210899 pStr->pCtx = ctx;
210900 z = (const char*)sqlite3_value_text(argv[0]);
210901 n = sqlite3Strlen30(z);
210902 jsonAppendString(pStr, z, n);
210903 jsonAppendChar(pStr, ':');
210904 jsonAppendSqlValue(pStr, argv[1]);
210905 }
210906}
210907static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
210908 JsonString *pStr;
210909 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
210910 if( pStr ){
210911 int flags;
210912 jsonAppendChar(pStr, '}');
210913 pStr->pCtx = ctx;
210914 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx))((int)(long int)(sqlite3_user_data(ctx)));
210915 if( pStr->eErr ){
210916 jsonReturnString(pStr, 0, 0);
210917 return;
210918 }else if( flags & JSON_BLOB0x08 ){
210919 jsonReturnStringAsBlob(pStr);
210920 if( isFinal ){
210921 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
210922 }else{
210923 jsonStringTrimOneChar(pStr);
210924 }
210925 return;
210926 }else if( isFinal ){
210927 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
210928 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) :
210929 sqlite3RCStrUnref);
210930 pStr->bStatic = 1;
210931 }else{
210932 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
210933 jsonStringTrimOneChar(pStr);
210934 }
210935 }else{
210936 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
210937 }
210938 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
210939}
210940static void jsonObjectValue(sqlite3_context *ctx){
210941 jsonObjectCompute(ctx, 0);
210942}
210943static void jsonObjectFinal(sqlite3_context *ctx){
210944 jsonObjectCompute(ctx, 1);
210945}
210946
210947
210948
210949#ifndef SQLITE_OMIT_VIRTUALTABLE
210950/****************************************************************************
210951** The json_each virtual table
210952****************************************************************************/
210953typedef struct JsonParent JsonParent;
210954struct JsonParent {
210955 u32 iHead; /* Start of object or array */
210956 u32 iValue; /* Start of the value */
210957 u32 iEnd; /* First byte past the end */
210958 u32 nPath; /* Length of path */
210959 i64 iKey; /* Key for JSONB_ARRAY */
210960};
210961
210962typedef struct JsonEachCursor JsonEachCursor;
210963struct JsonEachCursor {
210964 sqlite3_vtab_cursor base; /* Base class - must be first */
210965 u32 iRowid; /* The rowid */
210966 u32 i; /* Index in sParse.aBlob[] of current row */
210967 u32 iEnd; /* EOF when i equals or exceeds this value */
210968 u32 nRoot; /* Size of the root path in bytes */
210969 u8 eType; /* Type of the container for element i */
210970 u8 bRecursive; /* True for json_tree(). False for json_each() */
210971 u32 nParent; /* Current nesting depth */
210972 u32 nParentAlloc; /* Space allocated for aParent[] */
210973 JsonParent *aParent; /* Parent elements of i */
210974 sqlite3 *db; /* Database connection */
210975 JsonString path; /* Current path */
210976 JsonParse sParse; /* Parse of the input JSON */
210977};
210978typedef struct JsonEachConnection JsonEachConnection;
210979struct JsonEachConnection {
210980 sqlite3_vtab base; /* Base class - must be first */
210981 sqlite3 *db; /* Database connection */
210982};
210983
210984
210985/* Constructor for the json_each virtual table */
210986static int jsonEachConnect(
210987 sqlite3 *db,
210988 void *pAux,
210989 int argc, const char *const*argv,
210990 sqlite3_vtab **ppVtab,
210991 char **pzErr
210992){
210993 JsonEachConnection *pNew;
210994 int rc;
210995
210996/* Column numbers */
210997#define JEACH_KEY0 0
210998#define JEACH_VALUE1 1
210999#define JEACH_TYPE2 2
211000#define JEACH_ATOM3 3
211001#define JEACH_ID4 4
211002#define JEACH_PARENT5 5
211003#define JEACH_FULLKEY6 6
211004#define JEACH_PATH7 7
211005/* The xBestIndex method assumes that the JSON and ROOT columns are
211006** the last two columns in the table. Should this ever changes, be
211007** sure to update the xBestIndex method. */
211008#define JEACH_JSON8 8
211009#define JEACH_ROOT9 9
211010
211011 UNUSED_PARAMETER(pzErr)(void)(pzErr);
211012 UNUSED_PARAMETER(argv)(void)(argv);
211013 UNUSED_PARAMETER(argc)(void)(argc);
211014 UNUSED_PARAMETER(pAux)(void)(pAux);
211015 rc = sqlite3_declare_vtab(db,
211016 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
211017 "json HIDDEN,root HIDDEN)");
211018 if( rc==SQLITE_OK0 ){
211019 pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew));
211020 *ppVtab = (sqlite3_vtab*)pNew;
211021 if( pNew==0 ) return SQLITE_NOMEM7;
211022 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
211023 pNew->db = db;
211024 }
211025 return rc;
211026}
211027
211028/* destructor for json_each virtual table */
211029static int jsonEachDisconnect(sqlite3_vtab *pVtab){
211030 JsonEachConnection *p = (JsonEachConnection*)pVtab;
211031 sqlite3DbFree(p->db, pVtab);
211032 return SQLITE_OK0;
211033}
211034
211035/* constructor for a JsonEachCursor object for json_each(). */
211036static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
211037 JsonEachConnection *pVtab = (JsonEachConnection*)p;
211038 JsonEachCursor *pCur;
211039
211040 UNUSED_PARAMETER(p)(void)(p);
211041 pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur));
211042 if( pCur==0 ) return SQLITE_NOMEM7;
211043 pCur->db = pVtab->db;
211044 jsonStringZero(&pCur->path);
211045 *ppCursor = &pCur->base;
211046 return SQLITE_OK0;
211047}
211048
211049/* constructor for a JsonEachCursor object for json_tree(). */
211050static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
211051 int rc = jsonEachOpenEach(p, ppCursor);
211052 if( rc==SQLITE_OK0 ){
211053 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
211054 pCur->bRecursive = 1;
211055 }
211056 return rc;
211057}
211058
211059/* Reset a JsonEachCursor back to its original state. Free any memory
211060** held. */
211061static void jsonEachCursorReset(JsonEachCursor *p){
211062 jsonParseReset(&p->sParse);
211063 jsonStringReset(&p->path);
211064 sqlite3DbFree(p->db, p->aParent);
211065 p->iRowid = 0;
211066 p->i = 0;
211067 p->aParent = 0;
211068 p->nParent = 0;
211069 p->nParentAlloc = 0;
211070 p->iEnd = 0;
211071 p->eType = 0;
211072}
211073
211074/* Destructor for a jsonEachCursor object */
211075static int jsonEachClose(sqlite3_vtab_cursor *cur){
211076 JsonEachCursor *p = (JsonEachCursor*)cur;
211077 jsonEachCursorReset(p);
211078
211079 sqlite3DbFree(p->db, cur);
211080 return SQLITE_OK0;
211081}
211082
211083/* Return TRUE if the jsonEachCursor object has been advanced off the end
211084** of the JSON object */
211085static int jsonEachEof(sqlite3_vtab_cursor *cur){
211086 JsonEachCursor *p = (JsonEachCursor*)cur;
211087 return p->i >= p->iEnd;
211088}
211089
211090/*
211091** If the cursor is currently pointing at the label of a object entry,
211092** then return the index of the value. For all other cases, return the
211093** current pointer position, which is the value.
211094*/
211095static int jsonSkipLabel(JsonEachCursor *p){
211096 if( p->eType==JSONB_OBJECT12 ){
211097 u32 sz = 0;
211098 u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
211099 return p->i + n + sz;
211100 }else{
211101 return p->i;
211102 }
211103}
211104
211105/*
211106** Append the path name for the current element.
211107*/
211108static void jsonAppendPathName(JsonEachCursor *p){
211109 assert( p->nParent>0 )((void) (0));
211110 assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT )((void) (0));
211111 if( p->eType==JSONB_ARRAY11 ){
211112 jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey);
211113 }else{
211114 u32 n, sz = 0, k, i;
211115 const char *z;
211116 int needQuote = 0;
211117 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
211118 k = p->i + n;
211119 z = (const char*)&p->sParse.aBlob[k];
211120 if( sz==0 || !sqlite3Isalpha(z[0])(sqlite3CtypeMap[(unsigned char)(z[0])]&0x02) ){
211121 needQuote = 1;
211122 }else{
211123 for(i=0; i<sz; i++){
211124 if( !sqlite3Isalnum(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x06) ){
211125 needQuote = 1;
211126 break;
211127 }
211128 }
211129 }
211130 if( needQuote ){
211131 jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z);
211132 }else{
211133 jsonPrintf(sz+2,&p->path,".%.*s", sz, z);
211134 }
211135 }
211136}
211137
211138/* Advance the cursor to the next element for json_tree() */
211139static int jsonEachNext(sqlite3_vtab_cursor *cur){
211140 JsonEachCursor *p = (JsonEachCursor*)cur;
211141 int rc = SQLITE_OK0;
211142 if( p->bRecursive ){
211143 u8 x;
211144 u8 levelChange = 0;
211145 u32 n, sz = 0;
211146 u32 i = jsonSkipLabel(p);
211147 x = p->sParse.aBlob[i] & 0x0f;
211148 n = jsonbPayloadSize(&p->sParse, i, &sz);
211149 if( x==JSONB_OBJECT12 || x==JSONB_ARRAY11 ){
211150 JsonParent *pParent;
211151 if( p->nParent>=p->nParentAlloc ){
211152 JsonParent *pNew;
211153 u64 nNew;
211154 nNew = p->nParentAlloc*2 + 3;
211155 pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew);
211156 if( pNew==0 ) return SQLITE_NOMEM7;
211157 p->nParentAlloc = (u32)nNew;
211158 p->aParent = pNew;
211159 }
211160 levelChange = 1;
211161 pParent = &p->aParent[p->nParent];
211162 pParent->iHead = p->i;
211163 pParent->iValue = i;
211164 pParent->iEnd = i + n + sz;
211165 pParent->iKey = -1;
211166 pParent->nPath = (u32)p->path.nUsed;
211167 if( p->eType && p->nParent ){
211168 jsonAppendPathName(p);
211169 if( p->path.eErr ) rc = SQLITE_NOMEM7;
211170 }
211171 p->nParent++;
211172 p->i = i + n;
211173 }else{
211174 p->i = i + n + sz;
211175 }
211176 while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){
211177 p->nParent--;
211178 p->path.nUsed = p->aParent[p->nParent].nPath;
211179 levelChange = 1;
211180 }
211181 if( levelChange ){
211182 if( p->nParent>0 ){
211183 JsonParent *pParent = &p->aParent[p->nParent-1];
211184 u32 iVal = pParent->iValue;
211185 p->eType = p->sParse.aBlob[iVal] & 0x0f;
211186 }else{
211187 p->eType = 0;
211188 }
211189 }
211190 }else{
211191 u32 n, sz = 0;
211192 u32 i = jsonSkipLabel(p);
211193 n = jsonbPayloadSize(&p->sParse, i, &sz);
211194 p->i = i + n + sz;
211195 }
211196 if( p->eType==JSONB_ARRAY11 && p->nParent ){
211197 p->aParent[p->nParent-1].iKey++;
211198 }
211199 p->iRowid++;
211200 return rc;
211201}
211202
211203/* Length of the path for rowid==0 in bRecursive mode.
211204*/
211205static int jsonEachPathLength(JsonEachCursor *p){
211206 u32 n = p->path.nUsed;
211207 char *z = p->path.zBuf;
211208 if( p->iRowid==0 && p->bRecursive && n>=2 ){
211209 while( n>1 ){
211210 n--;
211211 if( z[n]=='[' || z[n]=='.' ){
211212 u32 x, sz = 0;
211213 char cSaved = z[n];
211214 z[n] = 0;
211215 assert( p->sParse.eEdit==0 )((void) (0));
211216 x = jsonLookupStep(&p->sParse, 0, z+1, 0);
211217 z[n] = cSaved;
211218 if( JSON_LOOKUP_ISERROR(x)((x)>=0xfffffffd) ) continue;
211219 if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break;
211220 }
211221 }
211222 }
211223 return n;
211224}
211225
211226/* Return the value of a column */
211227static int jsonEachColumn(
211228 sqlite3_vtab_cursor *cur, /* The cursor */
211229 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
211230 int iColumn /* Which column to return */
211231){
211232 JsonEachCursor *p = (JsonEachCursor*)cur;
211233 switch( iColumn ){
211234 case JEACH_KEY0: {
211235 if( p->nParent==0 ){
211236 u32 n, j;
211237 if( p->nRoot==1 ) break;
211238 j = jsonEachPathLength(p);
211239 n = p->nRoot - j;
211240 if( n==0 ){
211241 break;
211242 }else if( p->path.zBuf[j]=='[' ){
211243 i64 x;
211244 sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF81);
211245 sqlite3_result_int64(ctx, x);
211246 }else if( p->path.zBuf[j+1]=='"' ){
211247 sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211248 }else{
211249 sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211250 }
211251 break;
211252 }
211253 if( p->eType==JSONB_OBJECT12 ){
211254 jsonReturnFromBlob(&p->sParse, p->i, ctx, 1);
211255 }else{
211256 assert( p->eType==JSONB_ARRAY )((void) (0));
211257 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey);
211258 }
211259 break;
211260 }
211261 case JEACH_VALUE1: {
211262 u32 i = jsonSkipLabel(p);
211263 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
211264 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY11 ){
211265 sqlite3_result_subtype(ctx, JSON_SUBTYPE74);
211266 }
211267 break;
211268 }
211269 case JEACH_TYPE2: {
211270 u32 i = jsonSkipLabel(p);
211271 u8 eType = p->sParse.aBlob[i] & 0x0f;
211272 sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
211273 break;
211274 }
211275 case JEACH_ATOM3: {
211276 u32 i = jsonSkipLabel(p);
211277 if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY11 ){
211278 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
211279 }
211280 break;
211281 }
211282 case JEACH_ID4: {
211283 sqlite3_result_int64(ctx, (sqlite3_int64)p->i);
211284 break;
211285 }
211286 case JEACH_PARENT5: {
211287 if( p->nParent>0 && p->bRecursive ){
211288 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead);
211289 }
211290 break;
211291 }
211292 case JEACH_FULLKEY6: {
211293 u64 nBase = p->path.nUsed;
211294 if( p->nParent ) jsonAppendPathName(p);
211295 sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed,
211296 SQLITE_TRANSIENT((sqlite3_destructor_type)-1), SQLITE_UTF81);
211297 p->path.nUsed = nBase;
211298 break;
211299 }
211300 case JEACH_PATH7: {
211301 u32 n = jsonEachPathLength(p);
211302 sqlite3_result_text64(ctx, p->path.zBuf, n,
211303 SQLITE_TRANSIENT((sqlite3_destructor_type)-1), SQLITE_UTF81);
211304 break;
211305 }
211306 default: {
211307 sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC((sqlite3_destructor_type)0));
211308 break;
211309 }
211310 case JEACH_JSON8: {
211311 if( p->sParse.zJson==0 ){
211312 sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob,
211313 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211314 }else{
211315 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
211316 }
211317 break;
211318 }
211319 }
211320 return SQLITE_OK0;
211321}
211322
211323/* Return the current rowid value */
211324static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
211325 JsonEachCursor *p = (JsonEachCursor*)cur;
211326 *pRowid = p->iRowid;
211327 return SQLITE_OK0;
211328}
211329
211330/* The query strategy is to look for an equality constraint on the json
211331** column. Without such a constraint, the table cannot operate. idxNum is
211332** 1 if the constraint is found, 3 if the constraint and zRoot are found,
211333** and 0 otherwise.
211334*/
211335static int jsonEachBestIndex(
211336 sqlite3_vtab *tab,
211337 sqlite3_index_info *pIdxInfo
211338){
211339 int i; /* Loop counter or computed array index */
211340 int aIdx[2]; /* Index of constraints for JSON and ROOT */
211341 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
211342 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
211343 const struct sqlite3_index_constraint *pConstraint;
211344
211345 /* This implementation assumes that JSON and ROOT are the last two
211346 ** columns in the table */
211347 assert( JEACH_ROOT == JEACH_JSON+1 )((void) (0));
211348 UNUSED_PARAMETER(tab)(void)(tab);
211349 aIdx[0] = aIdx[1] = -1;
211350 pConstraint = pIdxInfo->aConstraint;
211351 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
211352 int iCol;
211353 int iMask;
211354 if( pConstraint->iColumn < JEACH_JSON8 ) continue;
211355 iCol = pConstraint->iColumn - JEACH_JSON8;
211356 assert( iCol==0 || iCol==1 )((void) (0));
211357 testcase( iCol==0 );
211358 iMask = 1 << iCol;
211359 if( pConstraint->usable==0 ){
211360 unusableMask |= iMask;
211361 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
211362 aIdx[iCol] = i;
211363 idxMask |= iMask;
211364 }
211365 }
211366 if( pIdxInfo->nOrderBy>0
211367 && pIdxInfo->aOrderBy[0].iColumn<0
211368 && pIdxInfo->aOrderBy[0].desc==0
211369 ){
211370 pIdxInfo->orderByConsumed = 1;
211371 }
211372
211373 if( (unusableMask & ~idxMask)!=0 ){
211374 /* If there are any unusable constraints on JSON or ROOT, then reject
211375 ** this entire plan */
211376 return SQLITE_CONSTRAINT19;
211377 }
211378 if( aIdx[0]<0 ){
211379 /* No JSON input. Leave estimatedCost at the huge value that it was
211380 ** initialized to to discourage the query planner from selecting this
211381 ** plan. */
211382 pIdxInfo->idxNum = 0;
211383 }else{
211384 pIdxInfo->estimatedCost = 1.0;
211385 i = aIdx[0];
211386 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
211387 pIdxInfo->aConstraintUsage[i].omit = 1;
211388 if( aIdx[1]<0 ){
211389 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
211390 }else{
211391 i = aIdx[1];
211392 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
211393 pIdxInfo->aConstraintUsage[i].omit = 1;
211394 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
211395 }
211396 }
211397 return SQLITE_OK0;
211398}
211399
211400/* Start a search on a new JSON string */
211401static int jsonEachFilter(
211402 sqlite3_vtab_cursor *cur,
211403 int idxNum, const char *idxStr,
211404 int argc, sqlite3_value **argv
211405){
211406 JsonEachCursor *p = (JsonEachCursor*)cur;
211407 const char *zRoot = 0;
211408 u32 i, n, sz;
211409
211410 UNUSED_PARAMETER(idxStr)(void)(idxStr);
211411 UNUSED_PARAMETER(argc)(void)(argc);
211412 jsonEachCursorReset(p);
211413 if( idxNum==0 ) return SQLITE_OK0;
211414 memset(&p->sParse, 0, sizeof(p->sParse));
211415 p->sParse.nJPRef = 1;
211416 p->sParse.db = p->db;
211417 if( jsonFuncArgMightBeBinary(argv[0]) ){
211418 p->sParse.nBlob = sqlite3_value_bytes(argv[0]);
211419 p->sParse.aBlob = (u8*)sqlite3_value_blob(argv[0]);
211420 }else{
211421 p->sParse.zJson = (char*)sqlite3_value_text(argv[0]);
211422 p->sParse.nJson = sqlite3_value_bytes(argv[0]);
211423 if( p->sParse.zJson==0 ){
211424 p->i = p->iEnd = 0;
211425 return SQLITE_OK0;
211426 }
211427 if( jsonConvertTextToBlob(&p->sParse, 0) ){
211428 if( p->sParse.oom ){
211429 return SQLITE_NOMEM7;
211430 }
211431 goto json_each_malformed_input;
211432 }
211433 }
211434 if( idxNum==3 ){
211435 zRoot = (const char*)sqlite3_value_text(argv[1]);
211436 if( zRoot==0 ) return SQLITE_OK0;
211437 if( zRoot[0]!='$' ){
211438 sqlite3_free(cur->pVtab->zErrMsg);
211439 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
211440 jsonEachCursorReset(p);
211441 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
211442 }
211443 p->nRoot = sqlite3Strlen30(zRoot);
211444 if( zRoot[1]==0 ){
211445 i = p->i = 0;
211446 p->eType = 0;
211447 }else{
211448 i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0);
211449 if( JSON_LOOKUP_ISERROR(i)((i)>=0xfffffffd) ){
211450 if( i==JSON_LOOKUP_NOTFOUND0xfffffffe ){
211451 p->i = 0;
211452 p->eType = 0;
211453 p->iEnd = 0;
211454 return SQLITE_OK0;
211455 }
211456 sqlite3_free(cur->pVtab->zErrMsg);
211457 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
211458 jsonEachCursorReset(p);
211459 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
211460 }
211461 if( p->sParse.iLabel ){
211462 p->i = p->sParse.iLabel;
211463 p->eType = JSONB_OBJECT12;
211464 }else{
211465 p->i = i;
211466 p->eType = JSONB_ARRAY11;
211467 }
211468 }
211469 jsonAppendRaw(&p->path, zRoot, p->nRoot);
211470 }else{
211471 i = p->i = 0;
211472 p->eType = 0;
211473 p->nRoot = 1;
211474 jsonAppendRaw(&p->path, "$", 1);
211475 }
211476 p->nParent = 0;
211477 n = jsonbPayloadSize(&p->sParse, i, &sz);
211478 p->iEnd = i+n+sz;
211479 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY11 && !p->bRecursive ){
211480 p->i = i + n;
211481 p->eType = p->sParse.aBlob[i] & 0x0f;
211482 p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent));
211483 if( p->aParent==0 ) return SQLITE_NOMEM7;
211484 p->nParent = 1;
211485 p->nParentAlloc = 1;
211486 p->aParent[0].iKey = 0;
211487 p->aParent[0].iEnd = p->iEnd;
211488 p->aParent[0].iHead = p->i;
211489 p->aParent[0].iValue = i;
211490 }
211491 return SQLITE_OK0;
211492
211493json_each_malformed_input:
211494 sqlite3_free(cur->pVtab->zErrMsg);
211495 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
211496 jsonEachCursorReset(p);
211497 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
211498}
211499
211500/* The methods of the json_each virtual table */
211501static sqlite3_module jsonEachModule = {
211502 0, /* iVersion */
211503 0, /* xCreate */
211504 jsonEachConnect, /* xConnect */
211505 jsonEachBestIndex, /* xBestIndex */
211506 jsonEachDisconnect, /* xDisconnect */
211507 0, /* xDestroy */
211508 jsonEachOpenEach, /* xOpen - open a cursor */
211509 jsonEachClose, /* xClose - close a cursor */
211510 jsonEachFilter, /* xFilter - configure scan constraints */
211511 jsonEachNext, /* xNext - advance a cursor */
211512 jsonEachEof, /* xEof - check for end of scan */
211513 jsonEachColumn, /* xColumn - read data */
211514 jsonEachRowid, /* xRowid - read data */
211515 0, /* xUpdate */
211516 0, /* xBegin */
211517 0, /* xSync */
211518 0, /* xCommit */
211519 0, /* xRollback */
211520 0, /* xFindMethod */
211521 0, /* xRename */
211522 0, /* xSavepoint */
211523 0, /* xRelease */
211524 0, /* xRollbackTo */
211525 0, /* xShadowName */
211526 0 /* xIntegrity */
211527};
211528
211529/* The methods of the json_tree virtual table. */
211530static sqlite3_module jsonTreeModule = {
211531 0, /* iVersion */
211532 0, /* xCreate */
211533 jsonEachConnect, /* xConnect */
211534 jsonEachBestIndex, /* xBestIndex */
211535 jsonEachDisconnect, /* xDisconnect */
211536 0, /* xDestroy */
211537 jsonEachOpenTree, /* xOpen - open a cursor */
211538 jsonEachClose, /* xClose - close a cursor */
211539 jsonEachFilter, /* xFilter - configure scan constraints */
211540 jsonEachNext, /* xNext - advance a cursor */
211541 jsonEachEof, /* xEof - check for end of scan */
211542 jsonEachColumn, /* xColumn - read data */
211543 jsonEachRowid, /* xRowid - read data */
211544 0, /* xUpdate */
211545 0, /* xBegin */
211546 0, /* xSync */
211547 0, /* xCommit */
211548 0, /* xRollback */
211549 0, /* xFindMethod */
211550 0, /* xRename */
211551 0, /* xSavepoint */
211552 0, /* xRelease */
211553 0, /* xRollbackTo */
211554 0, /* xShadowName */
211555 0 /* xIntegrity */
211556};
211557#endif /* SQLITE_OMIT_VIRTUALTABLE */
211558#endif /* !defined(SQLITE_OMIT_JSON) */
211559
211560/*
211561** Register JSON functions.
211562*/
211563SQLITE_PRIVATEstatic void sqlite3RegisterJsonFunctions(void){
211564#ifndef SQLITE_OMIT_JSON
211565 static FuncDef aJsonFunc[] = {
211566 /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */
211567 /* | | */
211568 /* Uses cache ------, | | ,---- Returns JSONB */
211569 /* | | | | */
211570 /* Number of arguments ---, | | | | ,--- Flags */
211571 /* | | | | | | */
211572 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} }
,
211573 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} }
,
211574 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} }
,
211575 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} }
,
211576 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} }
,
211577 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} }
,
211578 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} }
,
211579 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} }
,
211580 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} }
,
211581 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} }
,
211582 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} }
,
211583 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} }
,
211584 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} }
,
211585 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} }
,
211586 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} }
,
211587 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} }
,
211588 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} }
,
211589 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} }
,
211590 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} }
,
211591 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} }
,
211592 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} }
,
211593 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} }
,
211594 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} }
,
211595 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} }
,
211596 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} }
,
211597 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} }
,
211598 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} }
,
211599 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} }
,
211600 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} }
,
211601 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} }
,
211602#if SQLITE_DEBUG
211603 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} }
,
211604#endif
211605 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}}
211606 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}}
211607 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}}
211608 SQLITE_DETERMINISTIC){1, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0)), 0, jsonArrayStep,jsonArrayFinal,jsonArrayValue
,jsonGroupInverse,"json_group_array", {0}}
,
211609 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}}
211610 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}}
211611 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}}
,
211612 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}}
211613 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}}
211614 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}}
,
211615 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}}
211616 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}}
211617 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}}
211618 SQLITE_DETERMINISTIC){2, 0x00800000|1|(0*0x0020)|0x000100000|0x001000000|1| 0x000000800
, ((void*)(long int)(0x08)), 0, jsonObjectStep,jsonObjectFinal
,jsonObjectValue,jsonGroupInverse,"jsonb_group_object", {0}}
211619 };
211620 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc)((int)(sizeof(aJsonFunc)/sizeof(aJsonFunc[0]))));
211621#endif
211622}
211623
211624#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
211625/*
211626** Register the JSON table-valued functions
211627*/
211628SQLITE_PRIVATEstatic int sqlite3JsonTableFunctions(sqlite3 *db){
211629 int rc = SQLITE_OK0;
211630 static const struct {
211631 const char *zName;
211632 sqlite3_module *pModule;
211633 } aMod[] = {
211634 { "json_each", &jsonEachModule },
211635 { "json_tree", &jsonTreeModule },
211636 };
211637 unsigned int i;
211638 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK0; i++){
211639 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
211640 }
211641 return rc;
211642}
211643#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */
211644
211645/************** End of json.c ************************************************/
211646/************** Begin file rtree.c *******************************************/
211647/*
211648** 2001 September 15
211649**
211650** The author disclaims copyright to this source code. In place of
211651** a legal notice, here is a blessing:
211652**
211653** May you do good and not evil.
211654** May you find forgiveness for yourself and forgive others.
211655** May you share freely, never taking more than you give.
211656**
211657*************************************************************************
211658** This file contains code for implementations of the r-tree and r*-tree
211659** algorithms packaged as an SQLite virtual table module.
211660*/
211661
211662/*
211663** Database Format of R-Tree Tables
211664** --------------------------------
211665**
211666** The data structure for a single virtual r-tree table is stored in three
211667** native SQLite tables declared as follows. In each case, the '%' character
211668** in the table name is replaced with the user-supplied name of the r-tree
211669** table.
211670**
211671** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
211672** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
211673** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
211674**
211675** The data for each node of the r-tree structure is stored in the %_node
211676** table. For each node that is not the root node of the r-tree, there is
211677** an entry in the %_parent table associating the node with its parent.
211678** And for each row of data in the table, there is an entry in the %_rowid
211679** table that maps from the entries rowid to the id of the node that it
211680** is stored on. If the r-tree contains auxiliary columns, those are stored
211681** on the end of the %_rowid table.
211682**
211683** The root node of an r-tree always exists, even if the r-tree table is
211684** empty. The nodeno of the root node is always 1. All other nodes in the
211685** table must be the same size as the root node. The content of each node
211686** is formatted as follows:
211687**
211688** 1. If the node is the root node (node 1), then the first 2 bytes
211689** of the node contain the tree depth as a big-endian integer.
211690** For non-root nodes, the first 2 bytes are left unused.
211691**
211692** 2. The next 2 bytes contain the number of entries currently
211693** stored in the node.
211694**
211695** 3. The remainder of the node contains the node entries. Each entry
211696** consists of a single 8-byte integer followed by an even number
211697** of 4-byte coordinates. For leaf nodes the integer is the rowid
211698** of a record. For internal nodes it is the node number of a
211699** child page.
211700*/
211701
211702#if !defined(SQLITE_CORE1) \
211703 || (defined(SQLITE_ENABLE_RTREE1) && !defined(SQLITE_OMIT_VIRTUALTABLE))
211704
211705#ifndef SQLITE_CORE1
211706/* #include "sqlite3ext.h" */
211707 SQLITE_EXTENSION_INIT1
211708#else
211709/* #include "sqlite3.h" */
211710#endif
211711SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
211712
211713/*
211714** If building separately, we will need some setup that is normally
211715** found in sqliteInt.h
211716*/
211717#if !defined(SQLITE_AMALGAMATION1)
211718#include "sqlite3rtree.h"
211719typedef sqlite3_int64 i64;
211720typedef sqlite3_uint64 u64;
211721typedef unsigned char u8;
211722typedef unsigned short u16;
211723typedef unsigned int u32;
211724#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
211725# define NDEBUG1 1
211726#endif
211727#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
211728# undef NDEBUG1
211729#endif
211730#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
211731# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
211732#endif
211733#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
211734# define ALWAYS(X)(X) (1)
211735# define NEVER(X)(X) (0)
211736#elif !defined(NDEBUG1)
211737# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
211738# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
211739#else
211740# define ALWAYS(X)(X) (X)
211741# define NEVER(X)(X) (X)
211742#endif
211743#endif /* !defined(SQLITE_AMALGAMATION) */
211744
211745/* Macro to check for 4-byte alignment. Only used inside of assert() */
211746#ifdef SQLITE_DEBUG
211747# define FOUR_BYTE_ALIGNED(X) ((((char*)(X) - (char*)0) & 3)==0)
211748#endif
211749
211750/* #include <string.h> */
211751/* #include <stdio.h> */
211752/* #include <assert.h> */
211753/* #include <stdlib.h> */
211754
211755/* The following macro is used to suppress compiler warnings.
211756*/
211757#ifndef UNUSED_PARAMETER
211758# define UNUSED_PARAMETER(x)(void)(x) (void)(x)
211759#endif
211760
211761typedef struct Rtree Rtree;
211762typedef struct RtreeCursor RtreeCursor;
211763typedef struct RtreeNode RtreeNode;
211764typedef struct RtreeCell RtreeCell;
211765typedef struct RtreeConstraint RtreeConstraint;
211766typedef struct RtreeMatchArg RtreeMatchArg;
211767typedef struct RtreeGeomCallback RtreeGeomCallback;
211768typedef union RtreeCoord RtreeCoord;
211769typedef struct RtreeSearchPoint RtreeSearchPoint;
211770
211771/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
211772#define RTREE_MAX_DIMENSIONS5 5
211773
211774/* Maximum number of auxiliary columns */
211775#define RTREE_MAX_AUX_COLUMN100 100
211776
211777/* Size of hash table Rtree.aHash. This hash table is not expected to
211778** ever contain very many entries, so a fixed number of buckets is
211779** used.
211780*/
211781#define HASHSIZE97 97
211782
211783/* The xBestIndex method of this virtual table requires an estimate of
211784** the number of rows in the virtual table to calculate the costs of
211785** various strategies. If possible, this estimate is loaded from the
211786** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
211787** Otherwise, if no sqlite_stat1 entry is available, use
211788** RTREE_DEFAULT_ROWEST.
211789*/
211790#define RTREE_DEFAULT_ROWEST1048576 1048576
211791#define RTREE_MIN_ROWEST100 100
211792
211793/*
211794** An rtree virtual-table object.
211795*/
211796struct Rtree {
211797 sqlite3_vtab base; /* Base class. Must be first */
211798 sqlite3 *db; /* Host database connection */
211799 int iNodeSize; /* Size in bytes of each node in the node table */
211800 u8 nDim; /* Number of dimensions */
211801 u8 nDim2; /* Twice the number of dimensions */
211802 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
211803 u8 nBytesPerCell; /* Bytes consumed per cell */
211804 u8 inWrTrans; /* True if inside write transaction */
211805 u8 nAux; /* # of auxiliary columns in %_rowid */
211806#ifdef SQLITE_ENABLE_GEOPOLY
211807 u8 nAuxNotNull; /* Number of initial not-null aux columns */
211808#endif
211809#ifdef SQLITE_DEBUG
211810 u8 bCorrupt; /* Shadow table corruption detected */
211811#endif
211812 int iDepth; /* Current depth of the r-tree structure */
211813 char *zDb; /* Name of database containing r-tree table */
211814 char *zName; /* Name of r-tree table */
211815 char *zNodeName; /* Name of the %_node table */
211816 u32 nBusy; /* Current number of users of this structure */
211817 i64 nRowEst; /* Estimated number of rows in this table */
211818 u32 nCursor; /* Number of open cursors */
211819 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
211820 char *zReadAuxSql; /* SQL for statement to read aux data */
211821
211822 /* List of nodes removed during a CondenseTree operation. List is
211823 ** linked together via the pointer normally used for hash chains -
211824 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
211825 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
211826 */
211827 RtreeNode *pDeleted;
211828
211829 /* Blob I/O on xxx_node */
211830 sqlite3_blob *pNodeBlob;
211831
211832 /* Statements to read/write/delete a record from xxx_node */
211833 sqlite3_stmt *pWriteNode;
211834 sqlite3_stmt *pDeleteNode;
211835
211836 /* Statements to read/write/delete a record from xxx_rowid */
211837 sqlite3_stmt *pReadRowid;
211838 sqlite3_stmt *pWriteRowid;
211839 sqlite3_stmt *pDeleteRowid;
211840
211841 /* Statements to read/write/delete a record from xxx_parent */
211842 sqlite3_stmt *pReadParent;
211843 sqlite3_stmt *pWriteParent;
211844 sqlite3_stmt *pDeleteParent;
211845
211846 /* Statement for writing to the "aux:" fields, if there are any */
211847 sqlite3_stmt *pWriteAux;
211848
211849 RtreeNode *aHash[HASHSIZE97]; /* Hash table of in-memory nodes. */
211850};
211851
211852/* Possible values for Rtree.eCoordType: */
211853#define RTREE_COORD_REAL320 0
211854#define RTREE_COORD_INT321 1
211855
211856/*
211857** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
211858** only deal with integer coordinates. No floating point operations
211859** will be done.
211860*/
211861#ifdef SQLITE_RTREE_INT_ONLY
211862 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
211863 typedef int RtreeValue; /* Low accuracy coordinate */
211864# define RTREE_ZERO0.0 0
211865#else
211866 typedef double RtreeDValue; /* High accuracy coordinate */
211867 typedef float RtreeValue; /* Low accuracy coordinate */
211868# define RTREE_ZERO0.0 0.0
211869#endif
211870
211871/*
211872** Set the Rtree.bCorrupt flag
211873*/
211874#ifdef SQLITE_DEBUG
211875# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
211876#else
211877# define RTREE_IS_CORRUPT(X)
211878#endif
211879
211880/*
211881** When doing a search of an r-tree, instances of the following structure
211882** record intermediate results from the tree walk.
211883**
211884** The id is always a node-id. For iLevel>=1 the id is the node-id of
211885** the node that the RtreeSearchPoint represents. When iLevel==0, however,
211886** the id is of the parent node and the cell that RtreeSearchPoint
211887** represents is the iCell-th entry in the parent node.
211888*/
211889struct RtreeSearchPoint {
211890 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
211891 sqlite3_int64 id; /* Node ID */
211892 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
211893 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
211894 u8 iCell; /* Cell index within the node */
211895};
211896
211897/*
211898** The minimum number of cells allowed for a node is a third of the
211899** maximum. In Gutman's notation:
211900**
211901** m = M/3
211902**
211903** If an R*-tree "Reinsert" operation is required, the same number of
211904** cells are removed from the overfull node and reinserted into the tree.
211905*/
211906#define RTREE_MINCELLS(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
211907#define RTREE_REINSERT(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) RTREE_MINCELLS(p)((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
211908#define RTREE_MAXCELLS51 51
211909
211910/*
211911** The smallest possible node-size is (512-64)==448 bytes. And the largest
211912** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
211913** Therefore all non-root nodes must contain at least 3 entries. Since
211914** 3^40 is greater than 2^64, an r-tree structure always has a depth of
211915** 40 or less.
211916*/
211917#define RTREE_MAX_DEPTH40 40
211918
211919
211920/*
211921** Number of entries in the cursor RtreeNode cache. The first entry is
211922** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
211923** entries cache the RtreeNode for the first elements of the priority queue.
211924*/
211925#define RTREE_CACHE_SZ5 5
211926
211927/*
211928** An rtree cursor object.
211929*/
211930struct RtreeCursor {
211931 sqlite3_vtab_cursor base; /* Base class. Must be first */
211932 u8 atEOF; /* True if at end of search */
211933 u8 bPoint; /* True if sPoint is valid */
211934 u8 bAuxValid; /* True if pReadAux is valid */
211935 int iStrategy; /* Copy of idxNum search parameter */
211936 int nConstraint; /* Number of entries in aConstraint */
211937 RtreeConstraint *aConstraint; /* Search constraints. */
211938 int nPointAlloc; /* Number of slots allocated for aPoint[] */
211939 int nPoint; /* Number of slots used in aPoint[] */
211940 int mxLevel; /* iLevel value for root of the tree */
211941 RtreeSearchPoint *aPoint; /* Priority queue for search points */
211942 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
211943 RtreeSearchPoint sPoint; /* Cached next search point */
211944 RtreeNode *aNode[RTREE_CACHE_SZ5]; /* Rtree node cache */
211945 u32 anQueue[RTREE_MAX_DEPTH40+1]; /* Number of queued entries by iLevel */
211946};
211947
211948/* Return the Rtree of a RtreeCursor */
211949#define RTREE_OF_CURSOR(X)((Rtree*)((X)->base.pVtab)) ((Rtree*)((X)->base.pVtab))
211950
211951/*
211952** A coordinate can be either a floating point number or a integer. All
211953** coordinates within a single R-Tree are always of the same time.
211954*/
211955union RtreeCoord {
211956 RtreeValue f; /* Floating point value */
211957 int i; /* Integer value */
211958 u32 u; /* Unsigned for byte-order conversions */
211959};
211960
211961/*
211962** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
211963** formatted as a RtreeDValue (double or int64). This macro assumes that local
211964** variable pRtree points to the Rtree structure associated with the
211965** RtreeCoord.
211966*/
211967#ifdef SQLITE_RTREE_INT_ONLY
211968# define DCOORD(coord)( (pRtree->eCoordType==0) ? ((double)coord.f) : ((double)coord
.i) )
((RtreeDValue)coord.i)
211969#else
211970# define DCOORD(coord)( (pRtree->eCoordType==0) ? ((double)coord.f) : ((double)coord
.i) )
( \
211971 (pRtree->eCoordType==RTREE_COORD_REAL320) ? \
211972 ((double)coord.f) : \
211973 ((double)coord.i) \
211974 )
211975#endif
211976
211977/*
211978** A search constraint.
211979*/
211980struct RtreeConstraint {
211981 int iCoord; /* Index of constrained coordinate */
211982 int op; /* Constraining operation */
211983 union {
211984 RtreeDValue rValue; /* Constraint value. */
211985 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
211986 int (*xQueryFunc)(sqlite3_rtree_query_info*);
211987 } u;
211988 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
211989};
211990
211991/* Possible values for RtreeConstraint.op */
211992#define RTREE_EQ0x41 0x41 /* A */
211993#define RTREE_LE0x42 0x42 /* B */
211994#define RTREE_LT0x43 0x43 /* C */
211995#define RTREE_GE0x44 0x44 /* D */
211996#define RTREE_GT0x45 0x45 /* E */
211997#define RTREE_MATCH0x46 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
211998#define RTREE_QUERY0x47 0x47 /* G: New-style sqlite3_rtree_query_callback() */
211999
212000/* Special operators available only on cursors. Needs to be consecutive
212001** with the normal values above, but must be less than RTREE_MATCH. These
212002** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
212003** x<'xyz' (RTREE_TRUE) */
212004#define RTREE_TRUE0x3f 0x3f /* ? */
212005#define RTREE_FALSE0x40 0x40 /* @ */
212006
212007/*
212008** An rtree structure node.
212009*/
212010struct RtreeNode {
212011 RtreeNode *pParent; /* Parent node */
212012 i64 iNode; /* The node number */
212013 int nRef; /* Number of references to this node */
212014 int isDirty; /* True if the node needs to be written to disk */
212015 u8 *zData; /* Content of the node, as should be on disk */
212016 RtreeNode *pNext; /* Next node in this hash collision chain */
212017};
212018
212019/* Return the number of cells in a node */
212020#define NCELL(pNode)readInt16(&(pNode)->zData[2]) readInt16(&(pNode)->zData[2])
212021
212022/*
212023** A single cell from a node, deserialized
212024*/
212025struct RtreeCell {
212026 i64 iRowid; /* Node or entry ID */
212027 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS5*2]; /* Bounding box coordinates */
212028};
212029
212030
212031/*
212032** This object becomes the sqlite3_user_data() for the SQL functions
212033** that are created by sqlite3_rtree_geometry_callback() and
212034** sqlite3_rtree_query_callback() and which appear on the right of MATCH
212035** operators in order to constrain a search.
212036**
212037** xGeom and xQueryFunc are the callback functions. Exactly one of
212038** xGeom and xQueryFunc fields is non-NULL, depending on whether the
212039** SQL function was created using sqlite3_rtree_geometry_callback() or
212040** sqlite3_rtree_query_callback().
212041**
212042** This object is deleted automatically by the destructor mechanism in
212043** sqlite3_create_function_v2().
212044*/
212045struct RtreeGeomCallback {
212046 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
212047 int (*xQueryFunc)(sqlite3_rtree_query_info*);
212048 void (*xDestructor)(void*);
212049 void *pContext;
212050};
212051
212052/*
212053** An instance of this structure (in the form of a BLOB) is returned by
212054** the SQL functions that sqlite3_rtree_geometry_callback() and
212055** sqlite3_rtree_query_callback() create, and is read as the right-hand
212056** operand to the MATCH operator of an R-Tree.
212057*/
212058struct RtreeMatchArg {
212059 u32 iSize; /* Size of this object */
212060 RtreeGeomCallback cb; /* Info about the callback functions */
212061 int nParam; /* Number of parameters to the SQL function */
212062 sqlite3_value **apSqlParam; /* Original SQL parameter values */
212063 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
212064};
212065
212066#ifndef MAX
212067# define MAX(x,y)((x)>(y)?(x):(y)) ((x) < (y) ? (y) : (x))
212068#endif
212069#ifndef MIN
212070# define MIN(x,y)((x)<(y)?(x):(y)) ((x) > (y) ? (y) : (x))
212071#endif
212072
212073/* What version of GCC is being used. 0 means GCC is not being used .
212074** Note that the GCC_VERSION macro will also be set correctly when using
212075** clang, since clang works hard to be gcc compatible. So the gcc
212076** optimizations will also work when compiling with clang.
212077*/
212078#ifndef GCC_VERSION(4*1000000+2*1000+1)
212079#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
212080# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
212081#else
212082# define GCC_VERSION(4*1000000+2*1000+1) 0
212083#endif
212084#endif
212085
212086/* The testcase() macro should already be defined in the amalgamation. If
212087** it is not, make it a no-op.
212088*/
212089#ifndef SQLITE_AMALGAMATION1
212090# if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
212091 unsigned int sqlite3RtreeTestcase = 0;
212092# define testcase(X) if( X ){ sqlite3RtreeTestcase += __LINE__212092; }
212093# else
212094# define testcase(X)
212095# endif
212096#endif
212097
212098/*
212099** Make sure that the compiler intrinsics we desire are enabled when
212100** compiling with an appropriate version of MSVC unless prevented by
212101** the SQLITE_DISABLE_INTRINSIC define.
212102*/
212103#if !defined(SQLITE_DISABLE_INTRINSIC)
212104# if defined(_MSC_VER) && _MSC_VER>=1400
212105# if !defined(_WIN32_WCE)
212106/* # include <intrin.h> */
212107# pragma intrinsic(_byteswap_ulong)
212108# pragma intrinsic(_byteswap_uint64)
212109# else
212110/* # include <cmnintrin.h> */
212111# endif
212112# endif
212113#endif
212114
212115/*
212116** Macros to determine whether the machine is big or little endian,
212117** and whether or not that determination is run-time or compile-time.
212118**
212119** For best performance, an attempt is made to guess at the byte-order
212120** using C-preprocessor macros. If that is unsuccessful, or if
212121** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
212122** at run-time.
212123*/
212124#ifndef SQLITE_BYTEORDER1234 /* Replicate changes at tag-20230904a */
212125# if defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_BIG_ENDIAN__4321
212126# define SQLITE_BYTEORDER1234 4321
212127# elif defined(__BYTE_ORDER__1234) && __BYTE_ORDER__1234==__ORDER_LITTLE_ENDIAN__1234
212128# define SQLITE_BYTEORDER1234 1234
212129# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
212130# define SQLITE_BYTEORDER1234 4321
212131# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
212132 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
212133 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
212134 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
212135# define SQLITE_BYTEORDER1234 1234
212136# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
212137# define SQLITE_BYTEORDER1234 4321
212138# else
212139# define SQLITE_BYTEORDER1234 0
212140# endif
212141#endif
212142
212143
212144/* What version of MSVC is being used. 0 means MSVC is not being used */
212145#ifndef MSVC_VERSION0
212146#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
212147# define MSVC_VERSION0 _MSC_VER
212148#else
212149# define MSVC_VERSION0 0
212150#endif
212151#endif
212152
212153/*
212154** Functions to deserialize a 16 bit integer, 32 bit real number and
212155** 64 bit integer. The deserialized value is returned.
212156*/
212157static int readInt16(u8 *p){
212158 return (p[0]<<8) + p[1];
212159}
212160static void readCoord(u8 *p, RtreeCoord *pCoord){
212161 assert( FOUR_BYTE_ALIGNED(p) )((void) (0));
212162#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
212163 pCoord->u = _byteswap_ulong(*(u32*)p);
212164#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
212165 pCoord->u = __builtin_bswap32(*(u32*)p);
212166#elif SQLITE_BYTEORDER1234==4321
212167 pCoord->u = *(u32*)p;
212168#else
212169 pCoord->u = (
212170 (((u32)p[0]) << 24) +
212171 (((u32)p[1]) << 16) +
212172 (((u32)p[2]) << 8) +
212173 (((u32)p[3]) << 0)
212174 );
212175#endif
212176}
212177static i64 readInt64(u8 *p){
212178#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
212179 u64 x;
212180 memcpy(&x, p, 8);
212181 return (i64)_byteswap_uint64(x);
212182#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
212183 u64 x;
212184 memcpy(&x, p, 8);
212185 return (i64)__builtin_bswap64(x);
212186#elif SQLITE_BYTEORDER1234==4321
212187 i64 x;
212188 memcpy(&x, p, 8);
212189 return x;
212190#else
212191 return (i64)(
212192 (((u64)p[0]) << 56) +
212193 (((u64)p[1]) << 48) +
212194 (((u64)p[2]) << 40) +
212195 (((u64)p[3]) << 32) +
212196 (((u64)p[4]) << 24) +
212197 (((u64)p[5]) << 16) +
212198 (((u64)p[6]) << 8) +
212199 (((u64)p[7]) << 0)
212200 );
212201#endif
212202}
212203
212204/*
212205** Functions to serialize a 16 bit integer, 32 bit real number and
212206** 64 bit integer. The value returned is the number of bytes written
212207** to the argument buffer (always 2, 4 and 8 respectively).
212208*/
212209static void writeInt16(u8 *p, int i){
212210 p[0] = (i>> 8)&0xFF;
212211 p[1] = (i>> 0)&0xFF;
212212}
212213static int writeCoord(u8 *p, RtreeCoord *pCoord){
212214 u32 i;
212215 assert( FOUR_BYTE_ALIGNED(p) )((void) (0));
212216 assert( sizeof(RtreeCoord)==4 )((void) (0));
212217 assert( sizeof(u32)==4 )((void) (0));
212218#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
212219 i = __builtin_bswap32(pCoord->u);
212220 memcpy(p, &i, 4);
212221#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
212222 i = _byteswap_ulong(pCoord->u);
212223 memcpy(p, &i, 4);
212224#elif SQLITE_BYTEORDER1234==4321
212225 i = pCoord->u;
212226 memcpy(p, &i, 4);
212227#else
212228 i = pCoord->u;
212229 p[0] = (i>>24)&0xFF;
212230 p[1] = (i>>16)&0xFF;
212231 p[2] = (i>> 8)&0xFF;
212232 p[3] = (i>> 0)&0xFF;
212233#endif
212234 return 4;
212235}
212236static int writeInt64(u8 *p, i64 i){
212237#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
212238 i = (i64)__builtin_bswap64((u64)i);
212239 memcpy(p, &i, 8);
212240#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
212241 i = (i64)_byteswap_uint64((u64)i);
212242 memcpy(p, &i, 8);
212243#elif SQLITE_BYTEORDER1234==4321
212244 memcpy(p, &i, 8);
212245#else
212246 p[0] = (i>>56)&0xFF;
212247 p[1] = (i>>48)&0xFF;
212248 p[2] = (i>>40)&0xFF;
212249 p[3] = (i>>32)&0xFF;
212250 p[4] = (i>>24)&0xFF;
212251 p[5] = (i>>16)&0xFF;
212252 p[6] = (i>> 8)&0xFF;
212253 p[7] = (i>> 0)&0xFF;
212254#endif
212255 return 8;
212256}
212257
212258/*
212259** Increment the reference count of node p.
212260*/
212261static void nodeReference(RtreeNode *p){
212262 if( p ){
212263 assert( p->nRef>0 )((void) (0));
212264 p->nRef++;
212265 }
212266}
212267
212268/*
212269** Clear the content of node p (set all bytes to 0x00).
212270*/
212271static void nodeZero(Rtree *pRtree, RtreeNode *p){
212272 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
212273 p->isDirty = 1;
212274}
212275
212276/*
212277** Given a node number iNode, return the corresponding key to use
212278** in the Rtree.aHash table.
212279*/
212280static unsigned int nodeHash(i64 iNode){
212281 return ((unsigned)iNode) % HASHSIZE97;
212282}
212283
212284/*
212285** Search the node hash table for node iNode. If found, return a pointer
212286** to it. Otherwise, return 0.
212287*/
212288static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
212289 RtreeNode *p;
212290 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
212291 return p;
212292}
212293
212294/*
212295** Add node pNode to the node hash table.
212296*/
212297static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
212298 int iHash;
212299 assert( pNode->pNext==0 )((void) (0));
212300 iHash = nodeHash(pNode->iNode);
212301 pNode->pNext = pRtree->aHash[iHash];
212302 pRtree->aHash[iHash] = pNode;
212303}
212304
212305/*
212306** Remove node pNode from the node hash table.
212307*/
212308static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
212309 RtreeNode **pp;
212310 if( pNode->iNode!=0 ){
212311 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
212312 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp)((void) (0)); }
212313 *pp = pNode->pNext;
212314 pNode->pNext = 0;
212315 }
212316}
212317
212318/*
212319** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
212320** indicating that node has not yet been assigned a node number. It is
212321** assigned a node number when nodeWrite() is called to write the
212322** node contents out to the database.
212323*/
212324static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
212325 RtreeNode *pNode;
212326 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
212327 if( pNode ){
212328 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
212329 pNode->zData = (u8 *)&pNode[1];
212330 pNode->nRef = 1;
212331 pRtree->nNodeRef++;
212332 pNode->pParent = pParent;
212333 pNode->isDirty = 1;
212334 nodeReference(pParent);
212335 }
212336 return pNode;
212337}
212338
212339/*
212340** Clear the Rtree.pNodeBlob object
212341*/
212342static void nodeBlobReset(Rtree *pRtree){
212343 sqlite3_blob *pBlob = pRtree->pNodeBlob;
212344 pRtree->pNodeBlob = 0;
212345 sqlite3_blob_close(pBlob);
212346}
212347
212348/*
212349** Obtain a reference to an r-tree node.
212350*/
212351static int nodeAcquire(
212352 Rtree *pRtree, /* R-tree structure */
212353 i64 iNode, /* Node number to load */
212354 RtreeNode *pParent, /* Either the parent node or NULL */
212355 RtreeNode **ppNode /* OUT: Acquired node */
212356){
212357 int rc = SQLITE_OK0;
212358 RtreeNode *pNode = 0;
212359
212360 /* Check if the requested node is already in the hash table. If so,
212361 ** increase its reference count and return it.
212362 */
212363 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
212364 if( pParent && ALWAYS(pParent!=pNode->pParent)(pParent!=pNode->pParent) ){
212365 RTREE_IS_CORRUPT(pRtree);
212366 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
212367 }
212368 pNode->nRef++;
212369 *ppNode = pNode;
212370 return SQLITE_OK0;
212371 }
212372
212373 if( pRtree->pNodeBlob ){
212374 sqlite3_blob *pBlob = pRtree->pNodeBlob;
212375 pRtree->pNodeBlob = 0;
212376 rc = sqlite3_blob_reopen(pBlob, iNode);
212377 pRtree->pNodeBlob = pBlob;
212378 if( rc ){
212379 nodeBlobReset(pRtree);
212380 if( rc==SQLITE_NOMEM7 ) return SQLITE_NOMEM7;
212381 }
212382 }
212383 if( pRtree->pNodeBlob==0 ){
212384 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, pRtree->zNodeName,
212385 "data", iNode, 0,
212386 &pRtree->pNodeBlob);
212387 }
212388 if( rc ){
212389 *ppNode = 0;
212390 /* If unable to open an sqlite3_blob on the desired row, that can only
212391 ** be because the shadow tables hold erroneous data. */
212392 if( rc==SQLITE_ERROR1 ){
212393 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
212394 RTREE_IS_CORRUPT(pRtree);
212395 }
212396 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
212397 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
212398 if( !pNode ){
212399 rc = SQLITE_NOMEM7;
212400 }else{
212401 pNode->pParent = pParent;
212402 pNode->zData = (u8 *)&pNode[1];
212403 pNode->nRef = 1;
212404 pRtree->nNodeRef++;
212405 pNode->iNode = iNode;
212406 pNode->isDirty = 0;
212407 pNode->pNext = 0;
212408 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
212409 pRtree->iNodeSize, 0);
212410 }
212411 }
212412
212413 /* If the root node was just loaded, set pRtree->iDepth to the height
212414 ** of the r-tree structure. A height of zero means all data is stored on
212415 ** the root node. A height of one means the children of the root node
212416 ** are the leaves, and so on. If the depth as specified on the root node
212417 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
212418 */
212419 if( rc==SQLITE_OK0 && pNode && iNode==1 ){
212420 pRtree->iDepth = readInt16(pNode->zData);
212421 if( pRtree->iDepth>RTREE_MAX_DEPTH40 ){
212422 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
212423 RTREE_IS_CORRUPT(pRtree);
212424 }
212425 }
212426
212427 /* If no error has occurred so far, check if the "number of entries"
212428 ** field on the node is too large. If so, set the return code to
212429 ** SQLITE_CORRUPT_VTAB.
212430 */
212431 if( pNode && rc==SQLITE_OK0 ){
212432 if( NCELL(pNode)readInt16(&(pNode)->zData[2])>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
212433 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
212434 RTREE_IS_CORRUPT(pRtree);
212435 }
212436 }
212437
212438 if( rc==SQLITE_OK0 ){
212439 if( pNode!=0 ){
212440 nodeReference(pParent);
212441 nodeHashInsert(pRtree, pNode);
212442 }else{
212443 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
212444 RTREE_IS_CORRUPT(pRtree);
212445 }
212446 *ppNode = pNode;
212447 }else{
212448 nodeBlobReset(pRtree);
212449 if( pNode ){
212450 pRtree->nNodeRef--;
212451 sqlite3_free(pNode);
212452 }
212453 *ppNode = 0;
212454 }
212455
212456 return rc;
212457}
212458
212459/*
212460** Overwrite cell iCell of node pNode with the contents of pCell.
212461*/
212462static void nodeOverwriteCell(
212463 Rtree *pRtree, /* The overall R-Tree */
212464 RtreeNode *pNode, /* The node into which the cell is to be written */
212465 RtreeCell *pCell, /* The cell to write */
212466 int iCell /* Index into pNode into which pCell is written */
212467){
212468 int ii;
212469 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
212470 p += writeInt64(p, pCell->iRowid);
212471 for(ii=0; ii<pRtree->nDim2; ii++){
212472 p += writeCoord(p, &pCell->aCoord[ii]);
212473 }
212474 pNode->isDirty = 1;
212475}
212476
212477/*
212478** Remove the cell with index iCell from node pNode.
212479*/
212480static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
212481 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
212482 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
212483 int nByte = (NCELL(pNode)readInt16(&(pNode)->zData[2]) - iCell - 1) * pRtree->nBytesPerCell;
212484 memmove(pDst, pSrc, nByte);
212485 writeInt16(&pNode->zData[2], NCELL(pNode)readInt16(&(pNode)->zData[2])-1);
212486 pNode->isDirty = 1;
212487}
212488
212489/*
212490** Insert the contents of cell pCell into node pNode. If the insert
212491** is successful, return SQLITE_OK.
212492**
212493** If there is not enough free space in pNode, return SQLITE_FULL.
212494*/
212495static int nodeInsertCell(
212496 Rtree *pRtree, /* The overall R-Tree */
212497 RtreeNode *pNode, /* Write new cell into this node */
212498 RtreeCell *pCell /* The cell to be inserted */
212499){
212500 int nCell; /* Current number of cells in pNode */
212501 int nMaxCell; /* Maximum number of cells for pNode */
212502
212503 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
212504 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
212505
212506 assert( nCell<=nMaxCell )((void) (0));
212507 if( nCell<nMaxCell ){
212508 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
212509 writeInt16(&pNode->zData[2], nCell+1);
212510 pNode->isDirty = 1;
212511 }
212512
212513 return (nCell==nMaxCell);
212514}
212515
212516/*
212517** If the node is dirty, write it out to the database.
212518*/
212519static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
212520 int rc = SQLITE_OK0;
212521 if( pNode->isDirty ){
212522 sqlite3_stmt *p = pRtree->pWriteNode;
212523 if( pNode->iNode ){
212524 sqlite3_bind_int64(p, 1, pNode->iNode);
212525 }else{
212526 sqlite3_bind_null(p, 1);
212527 }
212528 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC((sqlite3_destructor_type)0));
212529 sqlite3_step(p);
212530 pNode->isDirty = 0;
212531 rc = sqlite3_reset(p);
212532 sqlite3_bind_null(p, 2);
212533 if( pNode->iNode==0 && rc==SQLITE_OK0 ){
212534 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
212535 nodeHashInsert(pRtree, pNode);
212536 }
212537 }
212538 return rc;
212539}
212540
212541/*
212542** Release a reference to a node. If the node is dirty and the reference
212543** count drops to zero, the node data is written to the database.
212544*/
212545static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
212546 int rc = SQLITE_OK0;
212547 if( pNode ){
212548 assert( pNode->nRef>0 )((void) (0));
212549 assert( pRtree->nNodeRef>0 )((void) (0));
212550 pNode->nRef--;
212551 if( pNode->nRef==0 ){
212552 pRtree->nNodeRef--;
212553 if( pNode->iNode==1 ){
212554 pRtree->iDepth = -1;
212555 }
212556 if( pNode->pParent ){
212557 rc = nodeRelease(pRtree, pNode->pParent);
212558 }
212559 if( rc==SQLITE_OK0 ){
212560 rc = nodeWrite(pRtree, pNode);
212561 }
212562 nodeHashDelete(pRtree, pNode);
212563 sqlite3_free(pNode);
212564 }
212565 }
212566 return rc;
212567}
212568
212569/*
212570** Return the 64-bit integer value associated with cell iCell of
212571** node pNode. If pNode is a leaf node, this is a rowid. If it is
212572** an internal node, then the 64-bit integer is a child page number.
212573*/
212574static i64 nodeGetRowid(
212575 Rtree *pRtree, /* The overall R-Tree */
212576 RtreeNode *pNode, /* The node from which to extract the ID */
212577 int iCell /* The cell index from which to extract the ID */
212578){
212579 assert( iCell<NCELL(pNode) )((void) (0));
212580 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
212581}
212582
212583/*
212584** Return coordinate iCoord from cell iCell in node pNode.
212585*/
212586static void nodeGetCoord(
212587 Rtree *pRtree, /* The overall R-Tree */
212588 RtreeNode *pNode, /* The node from which to extract a coordinate */
212589 int iCell, /* The index of the cell within the node */
212590 int iCoord, /* Which coordinate to extract */
212591 RtreeCoord *pCoord /* OUT: Space to write result to */
212592){
212593 assert( iCell<NCELL(pNode) )((void) (0));
212594 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
212595}
212596
212597/*
212598** Deserialize cell iCell of node pNode. Populate the structure pointed
212599** to by pCell with the results.
212600*/
212601static void nodeGetCell(
212602 Rtree *pRtree, /* The overall R-Tree */
212603 RtreeNode *pNode, /* The node containing the cell to be read */
212604 int iCell, /* Index of the cell within the node */
212605 RtreeCell *pCell /* OUT: Write the cell contents here */
212606){
212607 u8 *pData;
212608 RtreeCoord *pCoord;
212609 int ii = 0;
212610 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
212611 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
212612 pCoord = pCell->aCoord;
212613 do{
212614 readCoord(pData, &pCoord[ii]);
212615 readCoord(pData+4, &pCoord[ii+1]);
212616 pData += 8;
212617 ii += 2;
212618 }while( ii<pRtree->nDim2 );
212619}
212620
212621
212622/* Forward declaration for the function that does the work of
212623** the virtual table module xCreate() and xConnect() methods.
212624*/
212625static int rtreeInit(
212626 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
212627);
212628
212629/*
212630** Rtree virtual table module xCreate method.
212631*/
212632static int rtreeCreate(
212633 sqlite3 *db,
212634 void *pAux,
212635 int argc, const char *const*argv,
212636 sqlite3_vtab **ppVtab,
212637 char **pzErr
212638){
212639 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
212640}
212641
212642/*
212643** Rtree virtual table module xConnect method.
212644*/
212645static int rtreeConnect(
212646 sqlite3 *db,
212647 void *pAux,
212648 int argc, const char *const*argv,
212649 sqlite3_vtab **ppVtab,
212650 char **pzErr
212651){
212652 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
212653}
212654
212655/*
212656** Increment the r-tree reference count.
212657*/
212658static void rtreeReference(Rtree *pRtree){
212659 pRtree->nBusy++;
212660}
212661
212662/*
212663** Decrement the r-tree reference count. When the reference count reaches
212664** zero the structure is deleted.
212665*/
212666static void rtreeRelease(Rtree *pRtree){
212667 pRtree->nBusy--;
212668 if( pRtree->nBusy==0 ){
212669 pRtree->inWrTrans = 0;
212670 assert( pRtree->nCursor==0 )((void) (0));
212671 nodeBlobReset(pRtree);
212672 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt )((void) (0));
212673 sqlite3_finalize(pRtree->pWriteNode);
212674 sqlite3_finalize(pRtree->pDeleteNode);
212675 sqlite3_finalize(pRtree->pReadRowid);
212676 sqlite3_finalize(pRtree->pWriteRowid);
212677 sqlite3_finalize(pRtree->pDeleteRowid);
212678 sqlite3_finalize(pRtree->pReadParent);
212679 sqlite3_finalize(pRtree->pWriteParent);
212680 sqlite3_finalize(pRtree->pDeleteParent);
212681 sqlite3_finalize(pRtree->pWriteAux);
212682 sqlite3_free(pRtree->zReadAuxSql);
212683 sqlite3_free(pRtree);
212684 }
212685}
212686
212687/*
212688** Rtree virtual table module xDisconnect method.
212689*/
212690static int rtreeDisconnect(sqlite3_vtab *pVtab){
212691 rtreeRelease((Rtree *)pVtab);
212692 return SQLITE_OK0;
212693}
212694
212695/*
212696** Rtree virtual table module xDestroy method.
212697*/
212698static int rtreeDestroy(sqlite3_vtab *pVtab){
212699 Rtree *pRtree = (Rtree *)pVtab;
212700 int rc;
212701 char *zCreate = sqlite3_mprintf(
212702 "DROP TABLE '%q'.'%q_node';"
212703 "DROP TABLE '%q'.'%q_rowid';"
212704 "DROP TABLE '%q'.'%q_parent';",
212705 pRtree->zDb, pRtree->zName,
212706 pRtree->zDb, pRtree->zName,
212707 pRtree->zDb, pRtree->zName
212708 );
212709 if( !zCreate ){
212710 rc = SQLITE_NOMEM7;
212711 }else{
212712 nodeBlobReset(pRtree);
212713 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
212714 sqlite3_free(zCreate);
212715 }
212716 if( rc==SQLITE_OK0 ){
212717 rtreeRelease(pRtree);
212718 }
212719
212720 return rc;
212721}
212722
212723/*
212724** Rtree virtual table module xOpen method.
212725*/
212726static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
212727 int rc = SQLITE_NOMEM7;
212728 Rtree *pRtree = (Rtree *)pVTab;
212729 RtreeCursor *pCsr;
212730
212731 pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
212732 if( pCsr ){
212733 memset(pCsr, 0, sizeof(RtreeCursor));
212734 pCsr->base.pVtab = pVTab;
212735 rc = SQLITE_OK0;
212736 pRtree->nCursor++;
212737 }
212738 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
212739
212740 return rc;
212741}
212742
212743
212744/*
212745** Reset a cursor back to its initial state.
212746*/
212747static void resetCursor(RtreeCursor *pCsr){
212748 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
212749 int ii;
212750 sqlite3_stmt *pStmt;
212751 if( pCsr->aConstraint ){
212752 int i; /* Used to iterate through constraint array */
212753 for(i=0; i<pCsr->nConstraint; i++){
212754 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
212755 if( pInfo ){
212756 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
212757 sqlite3_free(pInfo);
212758 }
212759 }
212760 sqlite3_free(pCsr->aConstraint);
212761 pCsr->aConstraint = 0;
212762 }
212763 for(ii=0; ii<RTREE_CACHE_SZ5; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
212764 sqlite3_free(pCsr->aPoint);
212765 pStmt = pCsr->pReadAux;
212766 memset(pCsr, 0, sizeof(RtreeCursor));
212767 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
212768 pCsr->pReadAux = pStmt;
212769
212770}
212771
212772/*
212773** Rtree virtual table module xClose method.
212774*/
212775static int rtreeClose(sqlite3_vtab_cursor *cur){
212776 Rtree *pRtree = (Rtree *)(cur->pVtab);
212777 RtreeCursor *pCsr = (RtreeCursor *)cur;
212778 assert( pRtree->nCursor>0 )((void) (0));
212779 resetCursor(pCsr);
212780 sqlite3_finalize(pCsr->pReadAux);
212781 sqlite3_free(pCsr);
212782 pRtree->nCursor--;
212783 if( pRtree->nCursor==0 && pRtree->inWrTrans==0 ){
212784 nodeBlobReset(pRtree);
212785 }
212786 return SQLITE_OK0;
212787}
212788
212789/*
212790** Rtree virtual table module xEof method.
212791**
212792** Return non-zero if the cursor does not currently point to a valid
212793** record (i.e if the scan has finished), or zero otherwise.
212794*/
212795static int rtreeEof(sqlite3_vtab_cursor *cur){
212796 RtreeCursor *pCsr = (RtreeCursor *)cur;
212797 return pCsr->atEOF;
212798}
212799
212800/*
212801** Convert raw bits from the on-disk RTree record into a coordinate value.
212802** The on-disk format is big-endian and needs to be converted for little-
212803** endian platforms. The on-disk record stores integer coordinates if
212804** eInt is true and it stores 32-bit floating point records if eInt is
212805** false. a[] is the four bytes of the on-disk record to be decoded.
212806** Store the results in "r".
212807**
212808** There are five versions of this macro. The last one is generic. The
212809** other four are various architectures-specific optimizations.
212810*/
212811#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
212812#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; }
{ \
212813 RtreeCoord c; /* Coordinate decoded */ \
212814 c.u = _byteswap_ulong(*(u32*)a); \
212815 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
212816}
212817#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
212818#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; }
{ \
212819 RtreeCoord c; /* Coordinate decoded */ \
212820 c.u = __builtin_bswap32(*(u32*)a); \
212821 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
212822}
212823#elif SQLITE_BYTEORDER1234==1234
212824#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; }
{ \
212825 RtreeCoord c; /* Coordinate decoded */ \
212826 memcpy(&c.u,a,4); \
212827 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
212828 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
212829 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
212830}
212831#elif SQLITE_BYTEORDER1234==4321
212832#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; }
{ \
212833 RtreeCoord c; /* Coordinate decoded */ \
212834 memcpy(&c.u,a,4); \
212835 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
212836}
212837#else
212838#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; }
{ \
212839 RtreeCoord c; /* Coordinate decoded */ \
212840 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
212841 +((u32)a[2]<<8) + a[3]; \
212842 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
212843}
212844#endif
212845
212846/*
212847** Check the RTree node or entry given by pCellData and p against the MATCH
212848** constraint pConstraint.
212849*/
212850static int rtreeCallbackConstraint(
212851 RtreeConstraint *pConstraint, /* The constraint to test */
212852 int eInt, /* True if RTree holding integer coordinates */
212853 u8 *pCellData, /* Raw cell content */
212854 RtreeSearchPoint *pSearch, /* Container of this cell */
212855 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
212856 int *peWithin /* OUT: visibility of the cell */
212857){
212858 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
212859 int nCoord = pInfo->nCoord; /* No. of coordinates */
212860 int rc; /* Callback return code */
212861 RtreeCoord c; /* Translator union */
212862 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS5*2]; /* Decoded coordinates */
212863
212864 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY )((void) (0));
212865 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 )((void) (0));
212866
212867 if( pConstraint->op==RTREE_QUERY0x47 && pSearch->iLevel==1 ){
212868 pInfo->iRowid = readInt64(pCellData);
212869 }
212870 pCellData += 8;
212871#ifndef SQLITE_RTREE_INT_ONLY
212872 if( eInt==0 ){
212873 switch( nCoord ){
212874 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
212875 readCoord(pCellData+32, &c); aCoord[8] = c.f;
212876 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
212877 readCoord(pCellData+24, &c); aCoord[6] = c.f;
212878 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
212879 readCoord(pCellData+16, &c); aCoord[4] = c.f;
212880 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
212881 readCoord(pCellData+8, &c); aCoord[2] = c.f;
212882 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
212883 readCoord(pCellData, &c); aCoord[0] = c.f;
212884 }
212885 }else
212886#endif
212887 {
212888 switch( nCoord ){
212889 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
212890 readCoord(pCellData+32, &c); aCoord[8] = c.i;
212891 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
212892 readCoord(pCellData+24, &c); aCoord[6] = c.i;
212893 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
212894 readCoord(pCellData+16, &c); aCoord[4] = c.i;
212895 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
212896 readCoord(pCellData+8, &c); aCoord[2] = c.i;
212897 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
212898 readCoord(pCellData, &c); aCoord[0] = c.i;
212899 }
212900 }
212901 if( pConstraint->op==RTREE_MATCH0x46 ){
212902 int eWithin = 0;
212903 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
212904 nCoord, aCoord, &eWithin);
212905 if( eWithin==0 ) *peWithin = NOT_WITHIN0;
212906 *prScore = RTREE_ZERO0.0;
212907 }else{
212908 pInfo->aCoord = aCoord;
212909 pInfo->iLevel = pSearch->iLevel - 1;
212910 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
212911 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
212912 rc = pConstraint->u.xQueryFunc(pInfo);
212913 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
212914 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO0.0 ){
212915 *prScore = pInfo->rScore;
212916 }
212917 }
212918 return rc;
212919}
212920
212921/*
212922** Check the internal RTree node given by pCellData against constraint p.
212923** If this constraint cannot be satisfied by any child within the node,
212924** set *peWithin to NOT_WITHIN.
212925*/
212926static void rtreeNonleafConstraint(
212927 RtreeConstraint *p, /* The constraint to test */
212928 int eInt, /* True if RTree holds integer coordinates */
212929 u8 *pCellData, /* Raw cell content as appears on disk */
212930 int *peWithin /* Adjust downward, as appropriate */
212931){
212932 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
212933
212934 /* p->iCoord might point to either a lower or upper bound coordinate
212935 ** in a coordinate pair. But make pCellData point to the lower bound.
212936 */
212937 pCellData += 8 + 4*(p->iCoord&0xfe);
212938
212939 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
212940 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE((void) (0))
212941 || p->op==RTREE_FALSE )((void) (0));
212942 assert( FOUR_BYTE_ALIGNED(pCellData) )((void) (0));
212943 switch( p->op ){
212944 case RTREE_TRUE0x3f: return; /* Always satisfied */
212945 case RTREE_FALSE0x40: break; /* Never satisfied */
212946 case RTREE_EQ0x41:
212947 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; }
;
212948 /* val now holds the lower bound of the coordinate pair */
212949 if( p->u.rValue>=val ){
212950 pCellData += 4;
212951 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; }
;
212952 /* val now holds the upper bound of the coordinate pair */
212953 if( p->u.rValue<=val ) return;
212954 }
212955 break;
212956 case RTREE_LE0x42:
212957 case RTREE_LT0x43:
212958 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; }
;
212959 /* val now holds the lower bound of the coordinate pair */
212960 if( p->u.rValue>=val ) return;
212961 break;
212962
212963 default:
212964 pCellData += 4;
212965 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; }
;
212966 /* val now holds the upper bound of the coordinate pair */
212967 if( p->u.rValue<=val ) return;
212968 break;
212969 }
212970 *peWithin = NOT_WITHIN0;
212971}
212972
212973/*
212974** Check the leaf RTree cell given by pCellData against constraint p.
212975** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
212976** If the constraint is satisfied, leave *peWithin unchanged.
212977**
212978** The constraint is of the form: xN op $val
212979**
212980** The op is given by p->op. The xN is p->iCoord-th coordinate in
212981** pCellData. $val is given by p->u.rValue.
212982*/
212983static void rtreeLeafConstraint(
212984 RtreeConstraint *p, /* The constraint to test */
212985 int eInt, /* True if RTree holds integer coordinates */
212986 u8 *pCellData, /* Raw cell content as appears on disk */
212987 int *peWithin /* Adjust downward, as appropriate */
212988){
212989 RtreeDValue xN; /* Coordinate value converted to a double */
212990
212991 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
212992 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE((void) (0))
212993 || p->op==RTREE_FALSE )((void) (0));
212994 pCellData += 8 + p->iCoord*4;
212995 assert( FOUR_BYTE_ALIGNED(pCellData) )((void) (0));
212996 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; }
;
212997 switch( p->op ){
212998 case RTREE_TRUE0x3f: return; /* Always satisfied */
212999 case RTREE_FALSE0x40: break; /* Never satisfied */
213000 case RTREE_LE0x42: if( xN <= p->u.rValue ) return; break;
213001 case RTREE_LT0x43: if( xN < p->u.rValue ) return; break;
213002 case RTREE_GE0x44: if( xN >= p->u.rValue ) return; break;
213003 case RTREE_GT0x45: if( xN > p->u.rValue ) return; break;
213004 default: if( xN == p->u.rValue ) return; break;
213005 }
213006 *peWithin = NOT_WITHIN0;
213007}
213008
213009/*
213010** One of the cells in node pNode is guaranteed to have a 64-bit
213011** integer value equal to iRowid. Return the index of this cell.
213012*/
213013static int nodeRowidIndex(
213014 Rtree *pRtree,
213015 RtreeNode *pNode,
213016 i64 iRowid,
213017 int *piIndex
213018){
213019 int ii;
213020 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
213021 assert( nCell<200 )((void) (0));
213022 for(ii=0; ii<nCell; ii++){
213023 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
213024 *piIndex = ii;
213025 return SQLITE_OK0;
213026 }
213027 }
213028 RTREE_IS_CORRUPT(pRtree);
213029 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
213030}
213031
213032/*
213033** Return the index of the cell containing a pointer to node pNode
213034** in its parent. If pNode is the root node, return -1.
213035*/
213036static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
213037 RtreeNode *pParent = pNode->pParent;
213038 if( ALWAYS(pParent)(pParent) ){
213039 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
213040 }else{
213041 *piIndex = -1;
213042 return SQLITE_OK0;
213043 }
213044}
213045
213046/*
213047** Compare two search points. Return negative, zero, or positive if the first
213048** is less than, equal to, or greater than the second.
213049**
213050** The rScore is the primary key. Smaller rScore values come first.
213051** If the rScore is a tie, then use iLevel as the tie breaker with smaller
213052** iLevel values coming first. In this way, if rScore is the same for all
213053** SearchPoints, then iLevel becomes the deciding factor and the result
213054** is a depth-first search, which is the desired default behavior.
213055*/
213056static int rtreeSearchPointCompare(
213057 const RtreeSearchPoint *pA,
213058 const RtreeSearchPoint *pB
213059){
213060 if( pA->rScore<pB->rScore ) return -1;
213061 if( pA->rScore>pB->rScore ) return +1;
213062 if( pA->iLevel<pB->iLevel ) return -1;
213063 if( pA->iLevel>pB->iLevel ) return +1;
213064 return 0;
213065}
213066
213067/*
213068** Interchange two search points in a cursor.
213069*/
213070static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
213071 RtreeSearchPoint t = p->aPoint[i];
213072 assert( i<j )((void) (0));
213073 p->aPoint[i] = p->aPoint[j];
213074 p->aPoint[j] = t;
213075 i++; j++;
213076 if( i<RTREE_CACHE_SZ5 ){
213077 if( j>=RTREE_CACHE_SZ5 ){
213078 nodeRelease(RTREE_OF_CURSOR(p)((Rtree*)((p)->base.pVtab)), p->aNode[i]);
213079 p->aNode[i] = 0;
213080 }else{
213081 RtreeNode *pTemp = p->aNode[i];
213082 p->aNode[i] = p->aNode[j];
213083 p->aNode[j] = pTemp;
213084 }
213085 }
213086}
213087
213088/*
213089** Return the search point with the lowest current score.
213090*/
213091static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
213092 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
213093}
213094
213095/*
213096** Get the RtreeNode for the search point with the lowest score.
213097*/
213098static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
213099 sqlite3_int64 id;
213100 int ii = 1 - pCur->bPoint;
213101 assert( ii==0 || ii==1 )((void) (0));
213102 assert( pCur->bPoint || pCur->nPoint )((void) (0));
213103 if( pCur->aNode[ii]==0 ){
213104 assert( pRC!=0 )((void) (0));
213105 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
213106 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab)), id, 0, &pCur->aNode[ii]);
213107 }
213108 return pCur->aNode[ii];
213109}
213110
213111/*
213112** Push a new element onto the priority queue
213113*/
213114static RtreeSearchPoint *rtreeEnqueue(
213115 RtreeCursor *pCur, /* The cursor */
213116 RtreeDValue rScore, /* Score for the new search point */
213117 u8 iLevel /* Level for the new search point */
213118){
213119 int i, j;
213120 RtreeSearchPoint *pNew;
213121 if( pCur->nPoint>=pCur->nPointAlloc ){
213122 int nNew = pCur->nPointAlloc*2 + 8;
213123 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
213124 if( pNew==0 ) return 0;
213125 pCur->aPoint = pNew;
213126 pCur->nPointAlloc = nNew;
213127 }
213128 i = pCur->nPoint++;
213129 pNew = pCur->aPoint + i;
213130 pNew->rScore = rScore;
213131 pNew->iLevel = iLevel;
213132 assert( iLevel<=RTREE_MAX_DEPTH )((void) (0));
213133 while( i>0 ){
213134 RtreeSearchPoint *pParent;
213135 j = (i-1)/2;
213136 pParent = pCur->aPoint + j;
213137 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
213138 rtreeSearchPointSwap(pCur, j, i);
213139 i = j;
213140 pNew = pParent;
213141 }
213142 return pNew;
213143}
213144
213145/*
213146** Allocate a new RtreeSearchPoint and return a pointer to it. Return
213147** NULL if malloc fails.
213148*/
213149static RtreeSearchPoint *rtreeSearchPointNew(
213150 RtreeCursor *pCur, /* The cursor */
213151 RtreeDValue rScore, /* Score for the new search point */
213152 u8 iLevel /* Level for the new search point */
213153){
213154 RtreeSearchPoint *pNew, *pFirst;
213155 pFirst = rtreeSearchPointFirst(pCur);
213156 pCur->anQueue[iLevel]++;
213157 if( pFirst==0
213158 || pFirst->rScore>rScore
213159 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
213160 ){
213161 if( pCur->bPoint ){
213162 int ii;
213163 pNew = rtreeEnqueue(pCur, rScore, iLevel);
213164 if( pNew==0 ) return 0;
213165 ii = (int)(pNew - pCur->aPoint) + 1;
213166 assert( ii==1 )((void) (0));
213167 if( ALWAYS(ii<RTREE_CACHE_SZ)(ii<5) ){
213168 assert( pCur->aNode[ii]==0 )((void) (0));
213169 pCur->aNode[ii] = pCur->aNode[0];
213170 }else{
213171 nodeRelease(RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab)), pCur->aNode[0]);
213172 }
213173 pCur->aNode[0] = 0;
213174 *pNew = pCur->sPoint;
213175 }
213176 pCur->sPoint.rScore = rScore;
213177 pCur->sPoint.iLevel = iLevel;
213178 pCur->bPoint = 1;
213179 return &pCur->sPoint;
213180 }else{
213181 return rtreeEnqueue(pCur, rScore, iLevel);
213182 }
213183}
213184
213185#if 0
213186/* Tracing routines for the RtreeSearchPoint queue */
213187static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
213188 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
213189 printf(" %d.%05lld.%02d %g %d",
213190 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
213191 );
213192 idx++;
213193 if( idx<RTREE_CACHE_SZ5 ){
213194 printf(" %p\n", pCur->aNode[idx]);
213195 }else{
213196 printf("\n");
213197 }
213198}
213199static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
213200 int ii;
213201 printf("=== %9s ", zPrefix);
213202 if( pCur->bPoint ){
213203 tracePoint(&pCur->sPoint, -1, pCur);
213204 }
213205 for(ii=0; ii<pCur->nPoint; ii++){
213206 if( ii>0 || pCur->bPoint ) printf(" ");
213207 tracePoint(&pCur->aPoint[ii], ii, pCur);
213208 }
213209}
213210# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
213211#else
213212# define RTREE_QUEUE_TRACE(A,B) /* no-op */
213213#endif
213214
213215/* Remove the search point with the lowest current score.
213216*/
213217static void rtreeSearchPointPop(RtreeCursor *p){
213218 int i, j, k, n;
213219 i = 1 - p->bPoint;
213220 assert( i==0 || i==1 )((void) (0));
213221 if( p->aNode[i] ){
213222 nodeRelease(RTREE_OF_CURSOR(p)((Rtree*)((p)->base.pVtab)), p->aNode[i]);
213223 p->aNode[i] = 0;
213224 }
213225 if( p->bPoint ){
213226 p->anQueue[p->sPoint.iLevel]--;
213227 p->bPoint = 0;
213228 }else if( ALWAYS(p->nPoint)(p->nPoint) ){
213229 p->anQueue[p->aPoint[0].iLevel]--;
213230 n = --p->nPoint;
213231 p->aPoint[0] = p->aPoint[n];
213232 if( n<RTREE_CACHE_SZ5-1 ){
213233 p->aNode[1] = p->aNode[n+1];
213234 p->aNode[n+1] = 0;
213235 }
213236 i = 0;
213237 while( (j = i*2+1)<n ){
213238 k = j+1;
213239 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
213240 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
213241 rtreeSearchPointSwap(p, i, k);
213242 i = k;
213243 }else{
213244 break;
213245 }
213246 }else{
213247 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
213248 rtreeSearchPointSwap(p, i, j);
213249 i = j;
213250 }else{
213251 break;
213252 }
213253 }
213254 }
213255 }
213256}
213257
213258
213259/*
213260** Continue the search on cursor pCur until the front of the queue
213261** contains an entry suitable for returning as a result-set row,
213262** or until the RtreeSearchPoint queue is empty, indicating that the
213263** query has completed.
213264*/
213265static int rtreeStepToLeaf(RtreeCursor *pCur){
213266 RtreeSearchPoint *p;
213267 Rtree *pRtree = RTREE_OF_CURSOR(pCur)((Rtree*)((pCur)->base.pVtab));
213268 RtreeNode *pNode;
213269 int eWithin;
213270 int rc = SQLITE_OK0;
213271 int nCell;
213272 int nConstraint = pCur->nConstraint;
213273 int ii;
213274 int eInt;
213275 RtreeSearchPoint x;
213276
213277 eInt = pRtree->eCoordType==RTREE_COORD_INT321;
213278 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
213279 u8 *pCellData;
213280 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
213281 if( rc ) return rc;
213282 nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
213283 assert( nCell<200 )((void) (0));
213284 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
213285 while( p->iCell<nCell ){
213286 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
213287 eWithin = FULLY_WITHIN2;
213288 for(ii=0; ii<nConstraint; ii++){
213289 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
213290 if( pConstraint->op>=RTREE_MATCH0x46 ){
213291 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
213292 &rScore, &eWithin);
213293 if( rc ) return rc;
213294 }else if( p->iLevel==1 ){
213295 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
213296 }else{
213297 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
213298 }
213299 if( eWithin==NOT_WITHIN0 ){
213300 p->iCell++;
213301 pCellData += pRtree->nBytesPerCell;
213302 break;
213303 }
213304 }
213305 if( eWithin==NOT_WITHIN0 ) continue;
213306 p->iCell++;
213307 x.iLevel = p->iLevel - 1;
213308 if( x.iLevel ){
213309 x.id = readInt64(pCellData);
213310 for(ii=0; ii<pCur->nPoint; ii++){
213311 if( pCur->aPoint[ii].id==x.id ){
213312 RTREE_IS_CORRUPT(pRtree);
213313 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
213314 }
213315 }
213316 x.iCell = 0;
213317 }else{
213318 x.id = p->id;
213319 x.iCell = p->iCell - 1;
213320 }
213321 if( p->iCell>=nCell ){
213322 RTREE_QUEUE_TRACE(pCur, "POP-S:");
213323 rtreeSearchPointPop(pCur);
213324 }
213325 if( rScore<RTREE_ZERO0.0 ) rScore = RTREE_ZERO0.0;
213326 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
213327 if( p==0 ) return SQLITE_NOMEM7;
213328 p->eWithin = (u8)eWithin;
213329 p->id = x.id;
213330 p->iCell = x.iCell;
213331 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
213332 break;
213333 }
213334 if( p->iCell>=nCell ){
213335 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
213336 rtreeSearchPointPop(pCur);
213337 }
213338 }
213339 pCur->atEOF = p==0;
213340 return SQLITE_OK0;
213341}
213342
213343/*
213344** Rtree virtual table module xNext method.
213345*/
213346static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
213347 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
213348 int rc = SQLITE_OK0;
213349
213350 /* Move to the next entry that matches the configured constraints. */
213351 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
213352 if( pCsr->bAuxValid ){
213353 pCsr->bAuxValid = 0;
213354 sqlite3_reset(pCsr->pReadAux);
213355 }
213356 rtreeSearchPointPop(pCsr);
213357 rc = rtreeStepToLeaf(pCsr);
213358 return rc;
213359}
213360
213361/*
213362** Rtree virtual table module xRowid method.
213363*/
213364static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
213365 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
213366 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
213367 int rc = SQLITE_OK0;
213368 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
213369 if( rc==SQLITE_OK0 && ALWAYS(p)(p) ){
213370 if( p->iCell>=NCELL(pNode)readInt16(&(pNode)->zData[2]) ){
213371 rc = SQLITE_ABORT4;
213372 }else{
213373 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr)((Rtree*)((pCsr)->base.pVtab)), pNode, p->iCell);
213374 }
213375 }
213376 return rc;
213377}
213378
213379/*
213380** Rtree virtual table module xColumn method.
213381*/
213382static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
213383 Rtree *pRtree = (Rtree *)cur->pVtab;
213384 RtreeCursor *pCsr = (RtreeCursor *)cur;
213385 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
213386 RtreeCoord c;
213387 int rc = SQLITE_OK0;
213388 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
213389
213390 if( rc ) return rc;
213391 if( NEVER(p==0)(p==0) ) return SQLITE_OK0;
213392 if( p->iCell>=NCELL(pNode)readInt16(&(pNode)->zData[2]) ) return SQLITE_ABORT4;
213393 if( i==0 ){
213394 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
213395 }else if( i<=pRtree->nDim2 ){
213396 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
213397#ifndef SQLITE_RTREE_INT_ONLY
213398 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
213399 sqlite3_result_double(ctx, c.f);
213400 }else
213401#endif
213402 {
213403 assert( pRtree->eCoordType==RTREE_COORD_INT32 )((void) (0));
213404 sqlite3_result_int(ctx, c.i);
213405 }
213406 }else{
213407 if( !pCsr->bAuxValid ){
213408 if( pCsr->pReadAux==0 ){
213409 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
213410 &pCsr->pReadAux, 0);
213411 if( rc ) return rc;
213412 }
213413 sqlite3_bind_int64(pCsr->pReadAux, 1,
213414 nodeGetRowid(pRtree, pNode, p->iCell));
213415 rc = sqlite3_step(pCsr->pReadAux);
213416 if( rc==SQLITE_ROW100 ){
213417 pCsr->bAuxValid = 1;
213418 }else{
213419 sqlite3_reset(pCsr->pReadAux);
213420 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
213421 return rc;
213422 }
213423 }
213424 sqlite3_result_value(ctx,
213425 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
213426 }
213427 return SQLITE_OK0;
213428}
213429
213430/*
213431** Use nodeAcquire() to obtain the leaf node containing the record with
213432** rowid iRowid. If successful, set *ppLeaf to point to the node and
213433** return SQLITE_OK. If there is no such record in the table, set
213434** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
213435** to zero and return an SQLite error code.
213436*/
213437static int findLeafNode(
213438 Rtree *pRtree, /* RTree to search */
213439 i64 iRowid, /* The rowid searching for */
213440 RtreeNode **ppLeaf, /* Write the node here */
213441 sqlite3_int64 *piNode /* Write the node-id here */
213442){
213443 int rc;
213444 *ppLeaf = 0;
213445 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
213446 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW100 ){
213447 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
213448 if( piNode ) *piNode = iNode;
213449 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
213450 sqlite3_reset(pRtree->pReadRowid);
213451 }else{
213452 rc = sqlite3_reset(pRtree->pReadRowid);
213453 }
213454 return rc;
213455}
213456
213457/*
213458** This function is called to configure the RtreeConstraint object passed
213459** as the second argument for a MATCH constraint. The value passed as the
213460** first argument to this function is the right-hand operand to the MATCH
213461** operator.
213462*/
213463static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
213464 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
213465 sqlite3_rtree_query_info *pInfo; /* Callback information */
213466
213467 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
213468 if( pSrc==0 ) return SQLITE_ERROR1;
213469 pInfo = (sqlite3_rtree_query_info*)
213470 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
213471 if( !pInfo ) return SQLITE_NOMEM7;
213472 memset(pInfo, 0, sizeof(*pInfo));
213473 pBlob = (RtreeMatchArg*)&pInfo[1];
213474 memcpy(pBlob, pSrc, pSrc->iSize);
213475 pInfo->pContext = pBlob->cb.pContext;
213476 pInfo->nParam = pBlob->nParam;
213477 pInfo->aParam = pBlob->aParam;
213478 pInfo->apSqlParam = pBlob->apSqlParam;
213479
213480 if( pBlob->cb.xGeom ){
213481 pCons->u.xGeom = pBlob->cb.xGeom;
213482 }else{
213483 pCons->op = RTREE_QUERY0x47;
213484 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
213485 }
213486 pCons->pInfo = pInfo;
213487 return SQLITE_OK0;
213488}
213489
213490SQLITE_PRIVATEstatic int sqlite3IntFloatCompare(i64,double);
213491
213492/*
213493** Rtree virtual table module xFilter method.
213494*/
213495static int rtreeFilter(
213496 sqlite3_vtab_cursor *pVtabCursor,
213497 int idxNum, const char *idxStr,
213498 int argc, sqlite3_value **argv
213499){
213500 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
213501 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
213502 RtreeNode *pRoot = 0;
213503 int ii;
213504 int rc = SQLITE_OK0;
213505 int iCell = 0;
213506
213507 rtreeReference(pRtree);
213508
213509 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
213510 resetCursor(pCsr);
213511
213512 pCsr->iStrategy = idxNum;
213513 if( idxNum==1 ){
213514 /* Special case - lookup by rowid. */
213515 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
213516 RtreeSearchPoint *p; /* Search point for the leaf */
213517 i64 iRowid = sqlite3_value_int64(argv[0]);
213518 i64 iNode = 0;
213519 int eType = sqlite3_value_numeric_type(argv[0]);
213520 if( eType==SQLITE_INTEGER1
213521 || (eType==SQLITE_FLOAT2
213522 && 0==sqlite3IntFloatCompare(iRowid,sqlite3_value_double(argv[0])))
213523 ){
213524 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
213525 }else{
213526 rc = SQLITE_OK0;
213527 pLeaf = 0;
213528 }
213529 if( rc==SQLITE_OK0 && pLeaf!=0 ){
213530 p = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, 0);
213531 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
213532 pCsr->aNode[0] = pLeaf;
213533 p->id = iNode;
213534 p->eWithin = PARTLY_WITHIN1;
213535 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
213536 p->iCell = (u8)iCell;
213537 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
213538 }else{
213539 pCsr->atEOF = 1;
213540 }
213541 }else{
213542 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
213543 ** with the configured constraints.
213544 */
213545 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
213546 if( rc==SQLITE_OK0 && argc>0 ){
213547 pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
213548 pCsr->nConstraint = argc;
213549 if( !pCsr->aConstraint ){
213550 rc = SQLITE_NOMEM7;
213551 }else{
213552 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
213553 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
213554 assert( (idxStr==0 && argc==0)((void) (0))
213555 || (idxStr && (int)strlen(idxStr)==argc*2) )((void) (0));
213556 for(ii=0; ii<argc; ii++){
213557 RtreeConstraint *p = &pCsr->aConstraint[ii];
213558 int eType = sqlite3_value_numeric_type(argv[ii]);
213559 p->op = idxStr[ii*2];
213560 p->iCoord = idxStr[ii*2+1]-'0';
213561 if( p->op>=RTREE_MATCH0x46 ){
213562 /* A MATCH operator. The right-hand-side must be a blob that
213563 ** can be cast into an RtreeMatchArg object. One created using
213564 ** an sqlite3_rtree_geometry_callback() SQL user function.
213565 */
213566 rc = deserializeGeometry(argv[ii], p);
213567 if( rc!=SQLITE_OK0 ){
213568 break;
213569 }
213570 p->pInfo->nCoord = pRtree->nDim2;
213571 p->pInfo->anQueue = pCsr->anQueue;
213572 p->pInfo->mxLevel = pRtree->iDepth + 1;
213573 }else if( eType==SQLITE_INTEGER1 ){
213574 sqlite3_int64 iVal = sqlite3_value_int64(argv[ii]);
213575#ifdef SQLITE_RTREE_INT_ONLY
213576 p->u.rValue = iVal;
213577#else
213578 p->u.rValue = (double)iVal;
213579 if( iVal>=((sqlite3_int64)1)<<48
213580 || iVal<=-(((sqlite3_int64)1)<<48)
213581 ){
213582 if( p->op==RTREE_LT0x43 ) p->op = RTREE_LE0x42;
213583 if( p->op==RTREE_GT0x45 ) p->op = RTREE_GE0x44;
213584 }
213585#endif
213586 }else if( eType==SQLITE_FLOAT2 ){
213587#ifdef SQLITE_RTREE_INT_ONLY
213588 p->u.rValue = sqlite3_value_int64(argv[ii]);
213589#else
213590 p->u.rValue = sqlite3_value_double(argv[ii]);
213591#endif
213592 }else{
213593 p->u.rValue = RTREE_ZERO0.0;
213594 if( eType==SQLITE_NULL5 ){
213595 p->op = RTREE_FALSE0x40;
213596 }else if( p->op==RTREE_LT0x43 || p->op==RTREE_LE0x42 ){
213597 p->op = RTREE_TRUE0x3f;
213598 }else{
213599 p->op = RTREE_FALSE0x40;
213600 }
213601 }
213602 }
213603 }
213604 }
213605 if( rc==SQLITE_OK0 ){
213606 RtreeSearchPoint *pNew;
213607 assert( pCsr->bPoint==0 )((void) (0)); /* Due to the resetCursor() call above */
213608 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, (u8)(pRtree->iDepth+1));
213609 if( NEVER(pNew==0)(pNew==0) ){ /* Because pCsr->bPoint was FALSE */
213610 return SQLITE_NOMEM7;
213611 }
213612 pNew->id = 1;
213613 pNew->iCell = 0;
213614 pNew->eWithin = PARTLY_WITHIN1;
213615 assert( pCsr->bPoint==1 )((void) (0));
213616 pCsr->aNode[0] = pRoot;
213617 pRoot = 0;
213618 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
213619 rc = rtreeStepToLeaf(pCsr);
213620 }
213621 }
213622
213623 nodeRelease(pRtree, pRoot);
213624 rtreeRelease(pRtree);
213625 return rc;
213626}
213627
213628/*
213629** Rtree virtual table module xBestIndex method. There are three
213630** table scan strategies to choose from (in order from most to
213631** least desirable):
213632**
213633** idxNum idxStr Strategy
213634** ------------------------------------------------
213635** 1 Unused Direct lookup by rowid.
213636** 2 See below R-tree query or full-table scan.
213637** ------------------------------------------------
213638**
213639** If strategy 1 is used, then idxStr is not meaningful. If strategy
213640** 2 is used, idxStr is formatted to contain 2 bytes for each
213641** constraint used. The first two bytes of idxStr correspond to
213642** the constraint in sqlite3_index_info.aConstraintUsage[] with
213643** (argvIndex==1) etc.
213644**
213645** The first of each pair of bytes in idxStr identifies the constraint
213646** operator as follows:
213647**
213648** Operator Byte Value
213649** ----------------------
213650** = 0x41 ('A')
213651** <= 0x42 ('B')
213652** < 0x43 ('C')
213653** >= 0x44 ('D')
213654** > 0x45 ('E')
213655** MATCH 0x46 ('F')
213656** ----------------------
213657**
213658** The second of each pair of bytes identifies the coordinate column
213659** to which the constraint applies. The leftmost coordinate column
213660** is 'a', the second from the left 'b' etc.
213661*/
213662static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
213663 Rtree *pRtree = (Rtree*)tab;
213664 int rc = SQLITE_OK0;
213665 int ii;
213666 int bMatch = 0; /* True if there exists a MATCH constraint */
213667 i64 nRow; /* Estimated rows returned by this scan */
213668
213669 int iIdx = 0;
213670 char zIdxStr[RTREE_MAX_DIMENSIONS5*8+1];
213671 memset(zIdxStr, 0, sizeof(zIdxStr));
213672
213673 /* Check if there exists a MATCH constraint - even an unusable one. If there
213674 ** is, do not consider the lookup-by-rowid plan as using such a plan would
213675 ** require the VDBE to evaluate the MATCH constraint, which is not currently
213676 ** possible. */
213677 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
213678 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
213679 bMatch = 1;
213680 }
213681 }
213682
213683 assert( pIdxInfo->idxStr==0 )((void) (0));
213684 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
213685 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
213686
213687 if( bMatch==0 && p->usable
213688 && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2
213689 ){
213690 /* We have an equality constraint on the rowid. Use strategy 1. */
213691 int jj;
213692 for(jj=0; jj<ii; jj++){
213693 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
213694 pIdxInfo->aConstraintUsage[jj].omit = 0;
213695 }
213696 pIdxInfo->idxNum = 1;
213697 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
213698 pIdxInfo->aConstraintUsage[jj].omit = 1;
213699
213700 /* This strategy involves a two rowid lookups on an B-Tree structures
213701 ** and then a linear search of an R-Tree node. This should be
213702 ** considered almost as quick as a direct rowid lookup (for which
213703 ** sqlite uses an internal cost of 0.0). It is expected to return
213704 ** a single row.
213705 */
213706 pIdxInfo->estimatedCost = 30.0;
213707 pIdxInfo->estimatedRows = 1;
213708 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE0x00000001;
213709 return SQLITE_OK0;
213710 }
213711
213712 if( p->usable
213713 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
213714 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH64)
213715 ){
213716 u8 op;
213717 u8 doOmit = 1;
213718 switch( p->op ){
213719 case SQLITE_INDEX_CONSTRAINT_EQ2: op = RTREE_EQ0x41; doOmit = 0; break;
213720 case SQLITE_INDEX_CONSTRAINT_GT4: op = RTREE_GT0x45; doOmit = 0; break;
213721 case SQLITE_INDEX_CONSTRAINT_LE8: op = RTREE_LE0x42; break;
213722 case SQLITE_INDEX_CONSTRAINT_LT16: op = RTREE_LT0x43; doOmit = 0; break;
213723 case SQLITE_INDEX_CONSTRAINT_GE32: op = RTREE_GE0x44; break;
213724 case SQLITE_INDEX_CONSTRAINT_MATCH64: op = RTREE_MATCH0x46; break;
213725 default: op = 0; break;
213726 }
213727 if( op ){
213728 zIdxStr[iIdx++] = op;
213729 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
213730 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
213731 pIdxInfo->aConstraintUsage[ii].omit = doOmit;
213732 }
213733 }
213734 }
213735
213736 pIdxInfo->idxNum = 2;
213737 pIdxInfo->needToFreeIdxStr = 1;
213738 if( iIdx>0 ){
213739 pIdxInfo->idxStr = sqlite3_malloc( iIdx+1 );
213740 if( pIdxInfo->idxStr==0 ){
213741 return SQLITE_NOMEM7;
213742 }
213743 memcpy(pIdxInfo->idxStr, zIdxStr, iIdx+1);
213744 }
213745
213746 nRow = pRtree->nRowEst >> (iIdx/2);
213747 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
213748 pIdxInfo->estimatedRows = nRow;
213749
213750 return rc;
213751}
213752
213753/*
213754** Return the N-dimensional volumn of the cell stored in *p.
213755*/
213756static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
213757 RtreeDValue area = (RtreeDValue)1;
213758 assert( pRtree->nDim>=1 && pRtree->nDim<=5 )((void) (0));
213759#ifndef SQLITE_RTREE_INT_ONLY
213760 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
213761 switch( pRtree->nDim ){
213762 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
213763 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
213764 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
213765 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
213766 default: area *= p->aCoord[1].f - p->aCoord[0].f;
213767 }
213768 }else
213769#endif
213770 {
213771 switch( pRtree->nDim ){
213772 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
213773 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
213774 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
213775 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
213776 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
213777 }
213778 }
213779 return area;
213780}
213781
213782/*
213783** Return the margin length of cell p. The margin length is the sum
213784** of the objects size in each dimension.
213785*/
213786static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
213787 RtreeDValue margin = 0;
213788 int ii = pRtree->nDim2 - 2;
213789 do{
213790 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) )
);
213791 ii -= 2;
213792 }while( ii>=0 );
213793 return margin;
213794}
213795
213796/*
213797** Store the union of cells p1 and p2 in p1.
213798*/
213799static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
213800 int ii = 0;
213801 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
213802 do{
213803 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))
;
213804 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))
;
213805 ii += 2;
213806 }while( ii<pRtree->nDim2 );
213807 }else{
213808 do{
213809 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))
;
213810 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))
;
213811 ii += 2;
213812 }while( ii<pRtree->nDim2 );
213813 }
213814}
213815
213816/*
213817** Return true if the area covered by p2 is a subset of the area covered
213818** by p1. False otherwise.
213819*/
213820static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
213821 int ii;
213822 if( pRtree->eCoordType==RTREE_COORD_INT321 ){
213823 for(ii=0; ii<pRtree->nDim2; ii+=2){
213824 RtreeCoord *a1 = &p1->aCoord[ii];
213825 RtreeCoord *a2 = &p2->aCoord[ii];
213826 if( a2[0].i<a1[0].i || a2[1].i>a1[1].i ) return 0;
213827 }
213828 }else{
213829 for(ii=0; ii<pRtree->nDim2; ii+=2){
213830 RtreeCoord *a1 = &p1->aCoord[ii];
213831 RtreeCoord *a2 = &p2->aCoord[ii];
213832 if( a2[0].f<a1[0].f || a2[1].f>a1[1].f ) return 0;
213833 }
213834 }
213835 return 1;
213836}
213837
213838static RtreeDValue cellOverlap(
213839 Rtree *pRtree,
213840 RtreeCell *p,
213841 RtreeCell *aCell,
213842 int nCell
213843){
213844 int ii;
213845 RtreeDValue overlap = RTREE_ZERO0.0;
213846 for(ii=0; ii<nCell; ii++){
213847 int jj;
213848 RtreeDValue o = (RtreeDValue)1;
213849 for(jj=0; jj<pRtree->nDim2; jj+=2){
213850 RtreeDValue x1, x2;
213851 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) )))
;
213852 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) )))
;
213853 if( x2<x1 ){
213854 o = (RtreeDValue)0;
213855 break;
213856 }else{
213857 o = o * (x2-x1);
213858 }
213859 }
213860 overlap += o;
213861 }
213862 return overlap;
213863}
213864
213865
213866/*
213867** This function implements the ChooseLeaf algorithm from Gutman[84].
213868** ChooseSubTree in r*tree terminology.
213869*/
213870static int ChooseLeaf(
213871 Rtree *pRtree, /* Rtree table */
213872 RtreeCell *pCell, /* Cell to insert into rtree */
213873 int iHeight, /* Height of sub-tree rooted at pCell */
213874 RtreeNode **ppLeaf /* OUT: Selected leaf page */
213875){
213876 int rc;
213877 int ii;
213878 RtreeNode *pNode = 0;
213879 rc = nodeAcquire(pRtree, 1, 0, &pNode);
213880
213881 for(ii=0; rc==SQLITE_OK0 && ii<(pRtree->iDepth-iHeight); ii++){
213882 int iCell;
213883 sqlite3_int64 iBest = 0;
213884 int bFound = 0;
213885 RtreeDValue fMinGrowth = RTREE_ZERO0.0;
213886 RtreeDValue fMinArea = RTREE_ZERO0.0;
213887 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
213888 RtreeNode *pChild = 0;
213889
213890 /* First check to see if there is are any cells in pNode that completely
213891 ** contains pCell. If two or more cells in pNode completely contain pCell
213892 ** then pick the smallest.
213893 */
213894 for(iCell=0; iCell<nCell; iCell++){
213895 RtreeCell cell;
213896 nodeGetCell(pRtree, pNode, iCell, &cell);
213897 if( cellContains(pRtree, &cell, pCell) ){
213898 RtreeDValue area = cellArea(pRtree, &cell);
213899 if( bFound==0 || area<fMinArea ){
213900 iBest = cell.iRowid;
213901 fMinArea = area;
213902 bFound = 1;
213903 }
213904 }
213905 }
213906 if( !bFound ){
213907 /* No cells of pNode will completely contain pCell. So pick the
213908 ** cell of pNode that grows by the least amount when pCell is added.
213909 ** Break ties by selecting the smaller cell.
213910 */
213911 for(iCell=0; iCell<nCell; iCell++){
213912 RtreeCell cell;
213913 RtreeDValue growth;
213914 RtreeDValue area;
213915 nodeGetCell(pRtree, pNode, iCell, &cell);
213916 area = cellArea(pRtree, &cell);
213917 cellUnion(pRtree, &cell, pCell);
213918 growth = cellArea(pRtree, &cell)-area;
213919 if( iCell==0
213920 || growth<fMinGrowth
213921 || (growth==fMinGrowth && area<fMinArea)
213922 ){
213923 fMinGrowth = growth;
213924 fMinArea = area;
213925 iBest = cell.iRowid;
213926 }
213927 }
213928 }
213929
213930 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
213931 nodeRelease(pRtree, pNode);
213932 pNode = pChild;
213933 }
213934
213935 *ppLeaf = pNode;
213936 return rc;
213937}
213938
213939/*
213940** A cell with the same content as pCell has just been inserted into
213941** the node pNode. This function updates the bounding box cells in
213942** all ancestor elements.
213943*/
213944static int AdjustTree(
213945 Rtree *pRtree, /* Rtree table */
213946 RtreeNode *pNode, /* Adjust ancestry of this node. */
213947 RtreeCell *pCell /* This cell was just inserted */
213948){
213949 RtreeNode *p = pNode;
213950 int cnt = 0;
213951 int rc;
213952 while( p->pParent ){
213953 RtreeNode *pParent = p->pParent;
213954 RtreeCell cell;
213955 int iCell;
213956
213957 cnt++;
213958 if( NEVER(cnt>100)(cnt>100) ){
213959 RTREE_IS_CORRUPT(pRtree);
213960 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
213961 }
213962 rc = nodeParentIndex(pRtree, p, &iCell);
213963 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
213964 RTREE_IS_CORRUPT(pRtree);
213965 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
213966 }
213967
213968 nodeGetCell(pRtree, pParent, iCell, &cell);
213969 if( !cellContains(pRtree, &cell, pCell) ){
213970 cellUnion(pRtree, &cell, pCell);
213971 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
213972 }
213973
213974 p = pParent;
213975 }
213976 return SQLITE_OK0;
213977}
213978
213979/*
213980** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
213981*/
213982static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
213983 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
213984 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
213985 sqlite3_step(pRtree->pWriteRowid);
213986 return sqlite3_reset(pRtree->pWriteRowid);
213987}
213988
213989/*
213990** Write mapping (iNode->iPar) to the <rtree>_parent table.
213991*/
213992static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
213993 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
213994 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
213995 sqlite3_step(pRtree->pWriteParent);
213996 return sqlite3_reset(pRtree->pWriteParent);
213997}
213998
213999static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
214000
214001
214002
214003/*
214004** Arguments aIdx, aCell and aSpare all point to arrays of size
214005** nIdx. The aIdx array contains the set of integers from 0 to
214006** (nIdx-1) in no particular order. This function sorts the values
214007** in aIdx according to dimension iDim of the cells in aCell. The
214008** minimum value of dimension iDim is considered first, the
214009** maximum used to break ties.
214010**
214011** The aSpare array is used as temporary working space by the
214012** sorting algorithm.
214013*/
214014static void SortByDimension(
214015 Rtree *pRtree,
214016 int *aIdx,
214017 int nIdx,
214018 int iDim,
214019 RtreeCell *aCell,
214020 int *aSpare
214021){
214022 if( nIdx>1 ){
214023
214024 int iLeft = 0;
214025 int iRight = 0;
214026
214027 int nLeft = nIdx/2;
214028 int nRight = nIdx-nLeft;
214029 int *aLeft = aIdx;
214030 int *aRight = &aIdx[nLeft];
214031
214032 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
214033 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
214034
214035 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
214036 aLeft = aSpare;
214037 while( iLeft<nLeft || iRight<nRight ){
214038 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) )
;
214039 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) )
;
214040 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) )
;
214041 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) )
;
214042 if( (iLeft!=nLeft) && ((iRight==nRight)
214043 || (xleft1<xright1)
214044 || (xleft1==xright1 && xleft2<xright2)
214045 )){
214046 aIdx[iLeft+iRight] = aLeft[iLeft];
214047 iLeft++;
214048 }else{
214049 aIdx[iLeft+iRight] = aRight[iRight];
214050 iRight++;
214051 }
214052 }
214053
214054#if 0
214055 /* Check that the sort worked */
214056 {
214057 int jj;
214058 for(jj=1; jj<nIdx; jj++){
214059 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
214060 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
214061 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
214062 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
214063 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) )((void) (0));
214064 }
214065 }
214066#endif
214067 }
214068}
214069
214070/*
214071** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
214072*/
214073static int splitNodeStartree(
214074 Rtree *pRtree,
214075 RtreeCell *aCell,
214076 int nCell,
214077 RtreeNode *pLeft,
214078 RtreeNode *pRight,
214079 RtreeCell *pBboxLeft,
214080 RtreeCell *pBboxRight
214081){
214082 int **aaSorted;
214083 int *aSpare;
214084 int ii;
214085
214086 int iBestDim = 0;
214087 int iBestSplit = 0;
214088 RtreeDValue fBestMargin = RTREE_ZERO0.0;
214089
214090 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
214091
214092 aaSorted = (int **)sqlite3_malloc64(nByte);
214093 if( !aaSorted ){
214094 return SQLITE_NOMEM7;
214095 }
214096
214097 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
214098 memset(aaSorted, 0, nByte);
214099 for(ii=0; ii<pRtree->nDim; ii++){
214100 int jj;
214101 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
214102 for(jj=0; jj<nCell; jj++){
214103 aaSorted[ii][jj] = jj;
214104 }
214105 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
214106 }
214107
214108 for(ii=0; ii<pRtree->nDim; ii++){
214109 RtreeDValue margin = RTREE_ZERO0.0;
214110 RtreeDValue fBestOverlap = RTREE_ZERO0.0;
214111 RtreeDValue fBestArea = RTREE_ZERO0.0;
214112 int iBestLeft = 0;
214113 int nLeft;
214114
214115 for(
214116 nLeft=RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3);
214117 nLeft<=(nCell-RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3));
214118 nLeft++
214119 ){
214120 RtreeCell left;
214121 RtreeCell right;
214122 int kk;
214123 RtreeDValue overlap;
214124 RtreeDValue area;
214125
214126 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
214127 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
214128 for(kk=1; kk<(nCell-1); kk++){
214129 if( kk<nLeft ){
214130 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
214131 }else{
214132 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
214133 }
214134 }
214135 margin += cellMargin(pRtree, &left);
214136 margin += cellMargin(pRtree, &right);
214137 overlap = cellOverlap(pRtree, &left, &right, 1);
214138 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
214139 if( (nLeft==RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3))
214140 || (overlap<fBestOverlap)
214141 || (overlap==fBestOverlap && area<fBestArea)
214142 ){
214143 iBestLeft = nLeft;
214144 fBestOverlap = overlap;
214145 fBestArea = area;
214146 }
214147 }
214148
214149 if( ii==0 || margin<fBestMargin ){
214150 iBestDim = ii;
214151 fBestMargin = margin;
214152 iBestSplit = iBestLeft;
214153 }
214154 }
214155
214156 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
214157 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
214158 for(ii=0; ii<nCell; ii++){
214159 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
214160 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
214161 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
214162 nodeInsertCell(pRtree, pTarget, pCell);
214163 cellUnion(pRtree, pBbox, pCell);
214164 }
214165
214166 sqlite3_free(aaSorted);
214167 return SQLITE_OK0;
214168}
214169
214170
214171static int updateMapping(
214172 Rtree *pRtree,
214173 i64 iRowid,
214174 RtreeNode *pNode,
214175 int iHeight
214176){
214177 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
214178 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
214179 if( iHeight>0 ){
214180 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
214181 RtreeNode *p;
214182 for(p=pNode; p; p=p->pParent){
214183 if( p==pChild ) return SQLITE_CORRUPT_VTAB(11 | (1<<8));
214184 }
214185 if( pChild ){
214186 nodeRelease(pRtree, pChild->pParent);
214187 nodeReference(pNode);
214188 pChild->pParent = pNode;
214189 }
214190 }
214191 if( NEVER(pNode==0)(pNode==0) ) return SQLITE_ERROR1;
214192 return xSetMapping(pRtree, iRowid, pNode->iNode);
214193}
214194
214195static int SplitNode(
214196 Rtree *pRtree,
214197 RtreeNode *pNode,
214198 RtreeCell *pCell,
214199 int iHeight
214200){
214201 int i;
214202 int newCellIsRight = 0;
214203
214204 int rc = SQLITE_OK0;
214205 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
214206 RtreeCell *aCell;
214207 int *aiUsed;
214208
214209 RtreeNode *pLeft = 0;
214210 RtreeNode *pRight = 0;
214211
214212 RtreeCell leftbbox;
214213 RtreeCell rightbbox;
214214
214215 /* Allocate an array and populate it with a copy of pCell and
214216 ** all cells from node pLeft. Then zero the original node.
214217 */
214218 aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
214219 if( !aCell ){
214220 rc = SQLITE_NOMEM7;
214221 goto splitnode_out;
214222 }
214223 aiUsed = (int *)&aCell[nCell+1];
214224 memset(aiUsed, 0, sizeof(int)*(nCell+1));
214225 for(i=0; i<nCell; i++){
214226 nodeGetCell(pRtree, pNode, i, &aCell[i]);
214227 }
214228 nodeZero(pRtree, pNode);
214229 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
214230 nCell++;
214231
214232 if( pNode->iNode==1 ){
214233 pRight = nodeNew(pRtree, pNode);
214234 pLeft = nodeNew(pRtree, pNode);
214235 pRtree->iDepth++;
214236 pNode->isDirty = 1;
214237 writeInt16(pNode->zData, pRtree->iDepth);
214238 }else{
214239 pLeft = pNode;
214240 pRight = nodeNew(pRtree, pLeft->pParent);
214241 pLeft->nRef++;
214242 }
214243
214244 if( !pLeft || !pRight ){
214245 rc = SQLITE_NOMEM7;
214246 goto splitnode_out;
214247 }
214248
214249 memset(pLeft->zData, 0, pRtree->iNodeSize);
214250 memset(pRight->zData, 0, pRtree->iNodeSize);
214251
214252 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
214253 &leftbbox, &rightbbox);
214254 if( rc!=SQLITE_OK0 ){
214255 goto splitnode_out;
214256 }
214257
214258 /* Ensure both child nodes have node numbers assigned to them by calling
214259 ** nodeWrite(). Node pRight always needs a node number, as it was created
214260 ** by nodeNew() above. But node pLeft sometimes already has a node number.
214261 ** In this case avoid the all to nodeWrite().
214262 */
214263 if( SQLITE_OK0!=(rc = nodeWrite(pRtree, pRight))
214264 || (0==pLeft->iNode && SQLITE_OK0!=(rc = nodeWrite(pRtree, pLeft)))
214265 ){
214266 goto splitnode_out;
214267 }
214268
214269 rightbbox.iRowid = pRight->iNode;
214270 leftbbox.iRowid = pLeft->iNode;
214271
214272 if( pNode->iNode==1 ){
214273 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
214274 if( rc!=SQLITE_OK0 ){
214275 goto splitnode_out;
214276 }
214277 }else{
214278 RtreeNode *pParent = pLeft->pParent;
214279 int iCell;
214280 rc = nodeParentIndex(pRtree, pLeft, &iCell);
214281 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
214282 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
214283 rc = AdjustTree(pRtree, pParent, &leftbbox);
214284 assert( rc==SQLITE_OK )((void) (0));
214285 }
214286 if( NEVER(rc!=SQLITE_OK)(rc!=0) ){
214287 goto splitnode_out;
214288 }
214289 }
214290 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
214291 goto splitnode_out;
214292 }
214293
214294 for(i=0; i<NCELL(pRight)readInt16(&(pRight)->zData[2]); i++){
214295 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
214296 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
214297 if( iRowid==pCell->iRowid ){
214298 newCellIsRight = 1;
214299 }
214300 if( rc!=SQLITE_OK0 ){
214301 goto splitnode_out;
214302 }
214303 }
214304 if( pNode->iNode==1 ){
214305 for(i=0; i<NCELL(pLeft)readInt16(&(pLeft)->zData[2]); i++){
214306 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
214307 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
214308 if( rc!=SQLITE_OK0 ){
214309 goto splitnode_out;
214310 }
214311 }
214312 }else if( newCellIsRight==0 ){
214313 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
214314 }
214315
214316 if( rc==SQLITE_OK0 ){
214317 rc = nodeRelease(pRtree, pRight);
214318 pRight = 0;
214319 }
214320 if( rc==SQLITE_OK0 ){
214321 rc = nodeRelease(pRtree, pLeft);
214322 pLeft = 0;
214323 }
214324
214325splitnode_out:
214326 nodeRelease(pRtree, pRight);
214327 nodeRelease(pRtree, pLeft);
214328 sqlite3_free(aCell);
214329 return rc;
214330}
214331
214332/*
214333** If node pLeaf is not the root of the r-tree and its pParent pointer is
214334** still NULL, load all ancestor nodes of pLeaf into memory and populate
214335** the pLeaf->pParent chain all the way up to the root node.
214336**
214337** This operation is required when a row is deleted (or updated - an update
214338** is implemented as a delete followed by an insert). SQLite provides the
214339** rowid of the row to delete, which can be used to find the leaf on which
214340** the entry resides (argument pLeaf). Once the leaf is located, this
214341** function is called to determine its ancestry.
214342*/
214343static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
214344 int rc = SQLITE_OK0;
214345 RtreeNode *pChild = pLeaf;
214346 while( rc==SQLITE_OK0 && pChild->iNode!=1 && pChild->pParent==0 ){
214347 int rc2 = SQLITE_OK0; /* sqlite3_reset() return code */
214348 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
214349 rc = sqlite3_step(pRtree->pReadParent);
214350 if( rc==SQLITE_ROW100 ){
214351 RtreeNode *pTest; /* Used to test for reference loops */
214352 i64 iNode; /* Node number of parent node */
214353
214354 /* Before setting pChild->pParent, test that we are not creating a
214355 ** loop of references (as we would if, say, pChild==pParent). We don't
214356 ** want to do this as it leads to a memory leak when trying to delete
214357 ** the referenced counted node structures.
214358 */
214359 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
214360 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
214361 if( pTest==0 ){
214362 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
214363 }
214364 }
214365 rc = sqlite3_reset(pRtree->pReadParent);
214366 if( rc==SQLITE_OK0 ) rc = rc2;
214367 if( rc==SQLITE_OK0 && !pChild->pParent ){
214368 RTREE_IS_CORRUPT(pRtree);
214369 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
214370 }
214371 pChild = pChild->pParent;
214372 }
214373 return rc;
214374}
214375
214376static int deleteCell(Rtree *, RtreeNode *, int, int);
214377
214378static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
214379 int rc;
214380 int rc2;
214381 RtreeNode *pParent = 0;
214382 int iCell;
214383
214384 assert( pNode->nRef==1 )((void) (0));
214385
214386 /* Remove the entry in the parent cell. */
214387 rc = nodeParentIndex(pRtree, pNode, &iCell);
214388 if( rc==SQLITE_OK0 ){
214389 pParent = pNode->pParent;
214390 pNode->pParent = 0;
214391 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
214392 testcase( rc!=SQLITE_OK );
214393 }
214394 rc2 = nodeRelease(pRtree, pParent);
214395 if( rc==SQLITE_OK0 ){
214396 rc = rc2;
214397 }
214398 if( rc!=SQLITE_OK0 ){
214399 return rc;
214400 }
214401
214402 /* Remove the xxx_node entry. */
214403 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
214404 sqlite3_step(pRtree->pDeleteNode);
214405 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
214406 return rc;
214407 }
214408
214409 /* Remove the xxx_parent entry. */
214410 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
214411 sqlite3_step(pRtree->pDeleteParent);
214412 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
214413 return rc;
214414 }
214415
214416 /* Remove the node from the in-memory hash table and link it into
214417 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
214418 */
214419 nodeHashDelete(pRtree, pNode);
214420 pNode->iNode = iHeight;
214421 pNode->pNext = pRtree->pDeleted;
214422 pNode->nRef++;
214423 pRtree->pDeleted = pNode;
214424
214425 return SQLITE_OK0;
214426}
214427
214428static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
214429 RtreeNode *pParent = pNode->pParent;
214430 int rc = SQLITE_OK0;
214431 if( pParent ){
214432 int ii;
214433 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
214434 RtreeCell box; /* Bounding box for pNode */
214435 nodeGetCell(pRtree, pNode, 0, &box);
214436 for(ii=1; ii<nCell; ii++){
214437 RtreeCell cell;
214438 nodeGetCell(pRtree, pNode, ii, &cell);
214439 cellUnion(pRtree, &box, &cell);
214440 }
214441 box.iRowid = pNode->iNode;
214442 rc = nodeParentIndex(pRtree, pNode, &ii);
214443 if( rc==SQLITE_OK0 ){
214444 nodeOverwriteCell(pRtree, pParent, &box, ii);
214445 rc = fixBoundingBox(pRtree, pParent);
214446 }
214447 }
214448 return rc;
214449}
214450
214451/*
214452** Delete the cell at index iCell of node pNode. After removing the
214453** cell, adjust the r-tree data structure if required.
214454*/
214455static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
214456 RtreeNode *pParent;
214457 int rc;
214458
214459 if( SQLITE_OK0!=(rc = fixLeafParent(pRtree, pNode)) ){
214460 return rc;
214461 }
214462
214463 /* Remove the cell from the node. This call just moves bytes around
214464 ** the in-memory node image, so it cannot fail.
214465 */
214466 nodeDeleteCell(pRtree, pNode, iCell);
214467
214468 /* If the node is not the tree root and now has less than the minimum
214469 ** number of cells, remove it from the tree. Otherwise, update the
214470 ** cell in the parent node so that it tightly contains the updated
214471 ** node.
214472 */
214473 pParent = pNode->pParent;
214474 assert( pParent || pNode->iNode==1 )((void) (0));
214475 if( pParent ){
214476 if( NCELL(pNode)readInt16(&(pNode)->zData[2])<RTREE_MINCELLS(pRtree)((((pRtree)->iNodeSize-4)/(pRtree)->nBytesPerCell)/3) ){
214477 rc = removeNode(pRtree, pNode, iHeight);
214478 }else{
214479 rc = fixBoundingBox(pRtree, pNode);
214480 }
214481 }
214482
214483 return rc;
214484}
214485
214486/*
214487** Insert cell pCell into node pNode. Node pNode is the head of a
214488** subtree iHeight high (leaf nodes have iHeight==0).
214489*/
214490static int rtreeInsertCell(
214491 Rtree *pRtree,
214492 RtreeNode *pNode,
214493 RtreeCell *pCell,
214494 int iHeight
214495){
214496 int rc = SQLITE_OK0;
214497 if( iHeight>0 ){
214498 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
214499 if( pChild ){
214500 nodeRelease(pRtree, pChild->pParent);
214501 nodeReference(pNode);
214502 pChild->pParent = pNode;
214503 }
214504 }
214505 if( nodeInsertCell(pRtree, pNode, pCell) ){
214506 rc = SplitNode(pRtree, pNode, pCell, iHeight);
214507 }else{
214508 rc = AdjustTree(pRtree, pNode, pCell);
214509 if( ALWAYS(rc==SQLITE_OK)(rc==0) ){
214510 if( iHeight==0 ){
214511 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
214512 }else{
214513 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
214514 }
214515 }
214516 }
214517 return rc;
214518}
214519
214520static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
214521 int ii;
214522 int rc = SQLITE_OK0;
214523 int nCell = NCELL(pNode)readInt16(&(pNode)->zData[2]);
214524
214525 for(ii=0; rc==SQLITE_OK0 && ii<nCell; ii++){
214526 RtreeNode *pInsert;
214527 RtreeCell cell;
214528 nodeGetCell(pRtree, pNode, ii, &cell);
214529
214530 /* Find a node to store this cell in. pNode->iNode currently contains
214531 ** the height of the sub-tree headed by the cell.
214532 */
214533 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
214534 if( rc==SQLITE_OK0 ){
214535 int rc2;
214536 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
214537 rc2 = nodeRelease(pRtree, pInsert);
214538 if( rc==SQLITE_OK0 ){
214539 rc = rc2;
214540 }
214541 }
214542 }
214543 return rc;
214544}
214545
214546/*
214547** Select a currently unused rowid for a new r-tree record.
214548*/
214549static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
214550 int rc;
214551 sqlite3_bind_null(pRtree->pWriteRowid, 1);
214552 sqlite3_bind_null(pRtree->pWriteRowid, 2);
214553 sqlite3_step(pRtree->pWriteRowid);
214554 rc = sqlite3_reset(pRtree->pWriteRowid);
214555 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
214556 return rc;
214557}
214558
214559/*
214560** Remove the entry with rowid=iDelete from the r-tree structure.
214561*/
214562static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
214563 int rc; /* Return code */
214564 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
214565 int iCell; /* Index of iDelete cell in pLeaf */
214566 RtreeNode *pRoot = 0; /* Root node of rtree structure */
214567
214568
214569 /* Obtain a reference to the root node to initialize Rtree.iDepth */
214570 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
214571
214572 /* Obtain a reference to the leaf node that contains the entry
214573 ** about to be deleted.
214574 */
214575 if( rc==SQLITE_OK0 ){
214576 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
214577 }
214578
214579#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
214580 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB )((void) (0));
214581#endif
214582
214583 /* Delete the cell in question from the leaf node. */
214584 if( rc==SQLITE_OK0 && pLeaf ){
214585 int rc2;
214586 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
214587 if( rc==SQLITE_OK0 ){
214588 rc = deleteCell(pRtree, pLeaf, iCell, 0);
214589 }
214590 rc2 = nodeRelease(pRtree, pLeaf);
214591 if( rc==SQLITE_OK0 ){
214592 rc = rc2;
214593 }
214594 }
214595
214596 /* Delete the corresponding entry in the <rtree>_rowid table. */
214597 if( rc==SQLITE_OK0 ){
214598 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
214599 sqlite3_step(pRtree->pDeleteRowid);
214600 rc = sqlite3_reset(pRtree->pDeleteRowid);
214601 }
214602
214603 /* Check if the root node now has exactly one child. If so, remove
214604 ** it, schedule the contents of the child for reinsertion and
214605 ** reduce the tree height by one.
214606 **
214607 ** This is equivalent to copying the contents of the child into
214608 ** the root node (the operation that Gutman's paper says to perform
214609 ** in this scenario).
214610 */
214611 if( rc==SQLITE_OK0 && pRtree->iDepth>0 && NCELL(pRoot)readInt16(&(pRoot)->zData[2])==1 ){
214612 int rc2;
214613 RtreeNode *pChild = 0;
214614 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
214615 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); /* tag-20210916a */
214616 if( rc==SQLITE_OK0 ){
214617 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
214618 }
214619 rc2 = nodeRelease(pRtree, pChild);
214620 if( rc==SQLITE_OK0 ) rc = rc2;
214621 if( rc==SQLITE_OK0 ){
214622 pRtree->iDepth--;
214623 writeInt16(pRoot->zData, pRtree->iDepth);
214624 pRoot->isDirty = 1;
214625 }
214626 }
214627
214628 /* Re-insert the contents of any underfull nodes removed from the tree. */
214629 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
214630 if( rc==SQLITE_OK0 ){
214631 rc = reinsertNodeContent(pRtree, pLeaf);
214632 }
214633 pRtree->pDeleted = pLeaf->pNext;
214634 pRtree->nNodeRef--;
214635 sqlite3_free(pLeaf);
214636 }
214637
214638 /* Release the reference to the root node. */
214639 if( rc==SQLITE_OK0 ){
214640 rc = nodeRelease(pRtree, pRoot);
214641 }else{
214642 nodeRelease(pRtree, pRoot);
214643 }
214644
214645 return rc;
214646}
214647
214648/*
214649** Rounding constants for float->double conversion.
214650*/
214651#define RNDTOWARDS(1.0 - 1.0/8388608.0) (1.0 - 1.0/8388608.0) /* Round towards zero */
214652#define RNDAWAY(1.0 + 1.0/8388608.0) (1.0 + 1.0/8388608.0) /* Round away from zero */
214653
214654#if !defined(SQLITE_RTREE_INT_ONLY)
214655/*
214656** Convert an sqlite3_value into an RtreeValue (presumably a float)
214657** while taking care to round toward negative or positive, respectively.
214658*/
214659static RtreeValue rtreeValueDown(sqlite3_value *v){
214660 double d = sqlite3_value_double(v);
214661 float f = (float)d;
214662 if( f>d ){
214663 f = (float)(d*(d<0 ? RNDAWAY(1.0 + 1.0/8388608.0) : RNDTOWARDS(1.0 - 1.0/8388608.0)));
214664 }
214665 return f;
214666}
214667static RtreeValue rtreeValueUp(sqlite3_value *v){
214668 double d = sqlite3_value_double(v);
214669 float f = (float)d;
214670 if( f<d ){
214671 f = (float)(d*(d<0 ? RNDTOWARDS(1.0 - 1.0/8388608.0) : RNDAWAY(1.0 + 1.0/8388608.0)));
214672 }
214673 return f;
214674}
214675#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
214676
214677/*
214678** A constraint has failed while inserting a row into an rtree table.
214679** Assuming no OOM error occurs, this function sets the error message
214680** (at pRtree->base.zErrMsg) to an appropriate value and returns
214681** SQLITE_CONSTRAINT.
214682**
214683** Parameter iCol is the index of the leftmost column involved in the
214684** constraint failure. If it is 0, then the constraint that failed is
214685** the unique constraint on the id column. Otherwise, it is the rtree
214686** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
214687**
214688** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
214689*/
214690static int rtreeConstraintError(Rtree *pRtree, int iCol){
214691 sqlite3_stmt *pStmt = 0;
214692 char *zSql;
214693 int rc;
214694
214695 assert( iCol==0 || iCol%2 )((void) (0));
214696 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
214697 if( zSql ){
214698 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
214699 }else{
214700 rc = SQLITE_NOMEM7;
214701 }
214702 sqlite3_free(zSql);
214703
214704 if( rc==SQLITE_OK0 ){
214705 if( iCol==0 ){
214706 const char *zCol = sqlite3_column_name(pStmt, 0);
214707 pRtree->base.zErrMsg = sqlite3_mprintf(
214708 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
214709 );
214710 }else{
214711 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
214712 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
214713 pRtree->base.zErrMsg = sqlite3_mprintf(
214714 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
214715 );
214716 }
214717 }
214718
214719 sqlite3_finalize(pStmt);
214720 return (rc==SQLITE_OK0 ? SQLITE_CONSTRAINT19 : rc);
214721}
214722
214723
214724
214725/*
214726** The xUpdate method for rtree module virtual tables.
214727*/
214728static int rtreeUpdate(
214729 sqlite3_vtab *pVtab,
214730 int nData,
214731 sqlite3_value **aData,
214732 sqlite_int64 *pRowid
214733){
214734 Rtree *pRtree = (Rtree *)pVtab;
214735 int rc = SQLITE_OK0;
214736 RtreeCell cell; /* New cell to insert if nData>1 */
214737 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
214738
214739 if( pRtree->nNodeRef ){
214740 /* Unable to write to the btree while another cursor is reading from it,
214741 ** since the write might do a rebalance which would disrupt the read
214742 ** cursor. */
214743 return SQLITE_LOCKED_VTAB(6 | (2<<8));
214744 }
214745 rtreeReference(pRtree);
214746 assert(nData>=1)((void) (0));
214747
214748 memset(&cell, 0, sizeof(cell));
214749
214750 /* Constraint handling. A write operation on an r-tree table may return
214751 ** SQLITE_CONSTRAINT for two reasons:
214752 **
214753 ** 1. A duplicate rowid value, or
214754 ** 2. The supplied data violates the "x2>=x1" constraint.
214755 **
214756 ** In the first case, if the conflict-handling mode is REPLACE, then
214757 ** the conflicting row can be removed before proceeding. In the second
214758 ** case, SQLITE_CONSTRAINT must be returned regardless of the
214759 ** conflict-handling mode specified by the user.
214760 */
214761 if( nData>1 ){
214762 int ii;
214763 int nn = nData - 4;
214764
214765 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
214766 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
214767 **
214768 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
214769 ** with "column" that are interpreted as table constraints.
214770 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
214771 ** This problem was discovered after years of use, so we silently ignore
214772 ** these kinds of misdeclared tables to avoid breaking any legacy.
214773 */
214774
214775#ifndef SQLITE_RTREE_INT_ONLY
214776 if( pRtree->eCoordType==RTREE_COORD_REAL320 ){
214777 for(ii=0; ii<nn; ii+=2){
214778 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
214779 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
214780 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
214781 rc = rtreeConstraintError(pRtree, ii+1);
214782 goto constraint;
214783 }
214784 }
214785 }else
214786#endif
214787 {
214788 for(ii=0; ii<nn; ii+=2){
214789 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
214790 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
214791 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
214792 rc = rtreeConstraintError(pRtree, ii+1);
214793 goto constraint;
214794 }
214795 }
214796 }
214797
214798 /* If a rowid value was supplied, check if it is already present in
214799 ** the table. If so, the constraint has failed. */
214800 if( sqlite3_value_type(aData[2])!=SQLITE_NULL5 ){
214801 cell.iRowid = sqlite3_value_int64(aData[2]);
214802 if( sqlite3_value_type(aData[0])==SQLITE_NULL5
214803 || sqlite3_value_int64(aData[0])!=cell.iRowid
214804 ){
214805 int steprc;
214806 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
214807 steprc = sqlite3_step(pRtree->pReadRowid);
214808 rc = sqlite3_reset(pRtree->pReadRowid);
214809 if( SQLITE_ROW100==steprc ){
214810 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
214811 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
214812 }else{
214813 rc = rtreeConstraintError(pRtree, 0);
214814 goto constraint;
214815 }
214816 }
214817 }
214818 bHaveRowid = 1;
214819 }
214820 }
214821
214822 /* If aData[0] is not an SQL NULL value, it is the rowid of a
214823 ** record to delete from the r-tree table. The following block does
214824 ** just that.
214825 */
214826 if( sqlite3_value_type(aData[0])!=SQLITE_NULL5 ){
214827 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
214828 }
214829
214830 /* If the aData[] array contains more than one element, elements
214831 ** (aData[2]..aData[argc-1]) contain a new record to insert into
214832 ** the r-tree structure.
214833 */
214834 if( rc==SQLITE_OK0 && nData>1 ){
214835 /* Insert the new record into the r-tree */
214836 RtreeNode *pLeaf = 0;
214837
214838 /* Figure out the rowid of the new row. */
214839 if( bHaveRowid==0 ){
214840 rc = rtreeNewRowid(pRtree, &cell.iRowid);
214841 }
214842 *pRowid = cell.iRowid;
214843
214844 if( rc==SQLITE_OK0 ){
214845 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
214846 }
214847 if( rc==SQLITE_OK0 ){
214848 int rc2;
214849 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
214850 rc2 = nodeRelease(pRtree, pLeaf);
214851 if( rc==SQLITE_OK0 ){
214852 rc = rc2;
214853 }
214854 }
214855 if( rc==SQLITE_OK0 && pRtree->nAux ){
214856 sqlite3_stmt *pUp = pRtree->pWriteAux;
214857 int jj;
214858 sqlite3_bind_int64(pUp, 1, *pRowid);
214859 for(jj=0; jj<pRtree->nAux; jj++){
214860 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
214861 }
214862 sqlite3_step(pUp);
214863 rc = sqlite3_reset(pUp);
214864 }
214865 }
214866
214867constraint:
214868 rtreeRelease(pRtree);
214869 return rc;
214870}
214871
214872/*
214873** Called when a transaction starts.
214874*/
214875static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
214876 Rtree *pRtree = (Rtree *)pVtab;
214877 assert( pRtree->inWrTrans==0 )((void) (0));
214878 pRtree->inWrTrans = 1;
214879 return SQLITE_OK0;
214880}
214881
214882/*
214883** Called when a transaction completes (either by COMMIT or ROLLBACK).
214884** The sqlite3_blob object should be released at this point.
214885*/
214886static int rtreeEndTransaction(sqlite3_vtab *pVtab){
214887 Rtree *pRtree = (Rtree *)pVtab;
214888 pRtree->inWrTrans = 0;
214889 nodeBlobReset(pRtree);
214890 return SQLITE_OK0;
214891}
214892static int rtreeRollback(sqlite3_vtab *pVtab){
214893 return rtreeEndTransaction(pVtab);
214894}
214895
214896/*
214897** The xRename method for rtree module virtual tables.
214898*/
214899static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
214900 Rtree *pRtree = (Rtree *)pVtab;
214901 int rc = SQLITE_NOMEM7;
214902 char *zSql = sqlite3_mprintf(
214903 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
214904 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
214905 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
214906 , pRtree->zDb, pRtree->zName, zNewName
214907 , pRtree->zDb, pRtree->zName, zNewName
214908 , pRtree->zDb, pRtree->zName, zNewName
214909 );
214910 if( zSql ){
214911 nodeBlobReset(pRtree);
214912 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
214913 sqlite3_free(zSql);
214914 }
214915 return rc;
214916}
214917
214918/*
214919** The xSavepoint method.
214920**
214921** This module does not need to do anything to support savepoints. However,
214922** it uses this hook to close any open blob handle. This is done because a
214923** DROP TABLE command - which fortunately always opens a savepoint - cannot
214924** succeed if there are any open blob handles. i.e. if the blob handle were
214925** not closed here, the following would fail:
214926**
214927** BEGIN;
214928** INSERT INTO rtree...
214929** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
214930** COMMIT;
214931*/
214932static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
214933 Rtree *pRtree = (Rtree *)pVtab;
214934 u8 iwt = pRtree->inWrTrans;
214935 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
214936 pRtree->inWrTrans = 0;
214937 nodeBlobReset(pRtree);
214938 pRtree->inWrTrans = iwt;
214939 return SQLITE_OK0;
214940}
214941
214942/*
214943** This function populates the pRtree->nRowEst variable with an estimate
214944** of the number of rows in the virtual table. If possible, this is based
214945** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
214946*/
214947static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
214948 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
214949 char *zSql;
214950 sqlite3_stmt *p;
214951 int rc;
214952 i64 nRow = RTREE_MIN_ROWEST100;
214953
214954 rc = sqlite3_table_column_metadata(
214955 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
214956 );
214957 if( rc!=SQLITE_OK0 ){
214958 pRtree->nRowEst = RTREE_DEFAULT_ROWEST1048576;
214959 return rc==SQLITE_ERROR1 ? SQLITE_OK0 : rc;
214960 }
214961 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
214962 if( zSql==0 ){
214963 rc = SQLITE_NOMEM7;
214964 }else{
214965 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
214966 if( rc==SQLITE_OK0 ){
214967 if( sqlite3_step(p)==SQLITE_ROW100 ) nRow = sqlite3_column_int64(p, 0);
214968 rc = sqlite3_finalize(p);
214969 }
214970 sqlite3_free(zSql);
214971 }
214972 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST)((nRow)>(100)?(nRow):(100));
214973 return rc;
214974}
214975
214976
214977/*
214978** Return true if zName is the extension on one of the shadow tables used
214979** by this module.
214980*/
214981static int rtreeShadowName(const char *zName){
214982 static const char *azName[] = {
214983 "node", "parent", "rowid"
214984 };
214985 unsigned int i;
214986 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
214987 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
214988 }
214989 return 0;
214990}
214991
214992/* Forward declaration */
214993static int rtreeIntegrity(sqlite3_vtab*, const char*, const char*, int, char**);
214994
214995static sqlite3_module rtreeModule = {
214996 4, /* iVersion */
214997 rtreeCreate, /* xCreate - create a table */
214998 rtreeConnect, /* xConnect - connect to an existing table */
214999 rtreeBestIndex, /* xBestIndex - Determine search strategy */
215000 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
215001 rtreeDestroy, /* xDestroy - Drop a table */
215002 rtreeOpen, /* xOpen - open a cursor */
215003 rtreeClose, /* xClose - close a cursor */
215004 rtreeFilter, /* xFilter - configure scan constraints */
215005 rtreeNext, /* xNext - advance a cursor */
215006 rtreeEof, /* xEof */
215007 rtreeColumn, /* xColumn - read data */
215008 rtreeRowid, /* xRowid - read data */
215009 rtreeUpdate, /* xUpdate - write data */
215010 rtreeBeginTransaction, /* xBegin - begin transaction */
215011 rtreeEndTransaction, /* xSync - sync transaction */
215012 rtreeEndTransaction, /* xCommit - commit transaction */
215013 rtreeRollback, /* xRollback - rollback transaction */
215014 0, /* xFindFunction - function overloading */
215015 rtreeRename, /* xRename - rename the table */
215016 rtreeSavepoint, /* xSavepoint */
215017 0, /* xRelease */
215018 0, /* xRollbackTo */
215019 rtreeShadowName, /* xShadowName */
215020 rtreeIntegrity /* xIntegrity */
215021};
215022
215023static int rtreeSqlInit(
215024 Rtree *pRtree,
215025 sqlite3 *db,
215026 const char *zDb,
215027 const char *zPrefix,
215028 int isCreate
215029){
215030 int rc = SQLITE_OK0;
215031
215032 #define N_STATEMENT8 8
215033 static const char *azSql[N_STATEMENT8] = {
215034 /* Write the xxx_node table */
215035 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
215036 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
215037
215038 /* Read and write the xxx_rowid table */
215039 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
215040 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
215041 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
215042
215043 /* Read and write the xxx_parent table */
215044 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
215045 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
215046 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
215047 };
215048 sqlite3_stmt **appStmt[N_STATEMENT8];
215049 int i;
215050 const int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
215051
215052 pRtree->db = db;
215053
215054 if( isCreate ){
215055 char *zCreate;
215056 sqlite3_str *p = sqlite3_str_new(db);
215057 int ii;
215058 sqlite3_str_appendf(p,
215059 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
215060 zDb, zPrefix);
215061 for(ii=0; ii<pRtree->nAux; ii++){
215062 sqlite3_str_appendf(p,",a%d",ii);
215063 }
215064 sqlite3_str_appendf(p,
215065 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
215066 zDb, zPrefix);
215067 sqlite3_str_appendf(p,
215068 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
215069 zDb, zPrefix);
215070 sqlite3_str_appendf(p,
215071 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
215072 zDb, zPrefix, pRtree->iNodeSize);
215073 zCreate = sqlite3_str_finish(p);
215074 if( !zCreate ){
215075 return SQLITE_NOMEM7;
215076 }
215077 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
215078 sqlite3_free(zCreate);
215079 if( rc!=SQLITE_OK0 ){
215080 return rc;
215081 }
215082 }
215083
215084 appStmt[0] = &pRtree->pWriteNode;
215085 appStmt[1] = &pRtree->pDeleteNode;
215086 appStmt[2] = &pRtree->pReadRowid;
215087 appStmt[3] = &pRtree->pWriteRowid;
215088 appStmt[4] = &pRtree->pDeleteRowid;
215089 appStmt[5] = &pRtree->pReadParent;
215090 appStmt[6] = &pRtree->pWriteParent;
215091 appStmt[7] = &pRtree->pDeleteParent;
215092
215093 rc = rtreeQueryStat1(db, pRtree);
215094 for(i=0; i<N_STATEMENT8 && rc==SQLITE_OK0; i++){
215095 char *zSql;
215096 const char *zFormat;
215097 if( i!=3 || pRtree->nAux==0 ){
215098 zFormat = azSql[i];
215099 }else {
215100 /* An UPSERT is very slightly slower than REPLACE, but it is needed
215101 ** if there are auxiliary columns */
215102 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
215103 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
215104 }
215105 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
215106 if( zSql ){
215107 rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
215108 }else{
215109 rc = SQLITE_NOMEM7;
215110 }
215111 sqlite3_free(zSql);
215112 }
215113 if( pRtree->nAux && rc!=SQLITE_NOMEM7 ){
215114 pRtree->zReadAuxSql = sqlite3_mprintf(
215115 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
215116 zDb, zPrefix);
215117 if( pRtree->zReadAuxSql==0 ){
215118 rc = SQLITE_NOMEM7;
215119 }else{
215120 sqlite3_str *p = sqlite3_str_new(db);
215121 int ii;
215122 char *zSql;
215123 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
215124 for(ii=0; ii<pRtree->nAux; ii++){
215125 if( ii ) sqlite3_str_append(p, ",", 1);
215126#ifdef SQLITE_ENABLE_GEOPOLY
215127 if( ii<pRtree->nAuxNotNull ){
215128 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
215129 }else
215130#endif
215131 {
215132 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
215133 }
215134 }
215135 sqlite3_str_appendf(p, " WHERE rowid=?1");
215136 zSql = sqlite3_str_finish(p);
215137 if( zSql==0 ){
215138 rc = SQLITE_NOMEM7;
215139 }else{
215140 rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
215141 sqlite3_free(zSql);
215142 }
215143 }
215144 }
215145
215146 return rc;
215147}
215148
215149/*
215150** The second argument to this function contains the text of an SQL statement
215151** that returns a single integer value. The statement is compiled and executed
215152** using database connection db. If successful, the integer value returned
215153** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
215154** code is returned and the value of *piVal after returning is not defined.
215155*/
215156static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
215157 int rc = SQLITE_NOMEM7;
215158 if( zSql ){
215159 sqlite3_stmt *pStmt = 0;
215160 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
215161 if( rc==SQLITE_OK0 ){
215162 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
215163 *piVal = sqlite3_column_int(pStmt, 0);
215164 }
215165 rc = sqlite3_finalize(pStmt);
215166 }
215167 }
215168 return rc;
215169}
215170
215171/*
215172** This function is called from within the xConnect() or xCreate() method to
215173** determine the node-size used by the rtree table being created or connected
215174** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
215175** Otherwise, an SQLite error code is returned.
215176**
215177** If this function is being called as part of an xConnect(), then the rtree
215178** table already exists. In this case the node-size is determined by inspecting
215179** the root node of the tree.
215180**
215181** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
215182** This ensures that each node is stored on a single database page. If the
215183** database page-size is so large that more than RTREE_MAXCELLS entries
215184** would fit in a single node, use a smaller node-size.
215185*/
215186static int getNodeSize(
215187 sqlite3 *db, /* Database handle */
215188 Rtree *pRtree, /* Rtree handle */
215189 int isCreate, /* True for xCreate, false for xConnect */
215190 char **pzErr /* OUT: Error message, if any */
215191){
215192 int rc;
215193 char *zSql;
215194 if( isCreate ){
215195 int iPageSize = 0;
215196 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
215197 rc = getIntFromStmt(db, zSql, &iPageSize);
215198 if( rc==SQLITE_OK0 ){
215199 pRtree->iNodeSize = iPageSize-64;
215200 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS51)<pRtree->iNodeSize ){
215201 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS51;
215202 }
215203 }else{
215204 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
215205 }
215206 }else{
215207 zSql = sqlite3_mprintf(
215208 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
215209 pRtree->zDb, pRtree->zName
215210 );
215211 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
215212 if( rc!=SQLITE_OK0 ){
215213 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
215214 }else if( pRtree->iNodeSize<(512-64) ){
215215 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
215216 RTREE_IS_CORRUPT(pRtree);
215217 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
215218 pRtree->zName);
215219 }
215220 }
215221
215222 sqlite3_free(zSql);
215223 return rc;
215224}
215225
215226/*
215227** Return the length of a token
215228*/
215229static int rtreeTokenLength(const char *z){
215230 int dummy = 0;
215231 return sqlite3GetToken((const unsigned char*)z,&dummy);
215232}
215233
215234/*
215235** This function is the implementation of both the xConnect and xCreate
215236** methods of the r-tree virtual table.
215237**
215238** argv[0] -> module name
215239** argv[1] -> database name
215240** argv[2] -> table name
215241** argv[...] -> column names...
215242*/
215243static int rtreeInit(
215244 sqlite3 *db, /* Database connection */
215245 void *pAux, /* One of the RTREE_COORD_* constants */
215246 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
215247 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
215248 char **pzErr, /* OUT: Error message, if any */
215249 int isCreate /* True for xCreate, false for xConnect */
215250){
215251 int rc = SQLITE_OK0;
215252 Rtree *pRtree;
215253 int nDb; /* Length of string argv[1] */
215254 int nName; /* Length of string argv[2] */
215255 int eCoordType = (pAux ? RTREE_COORD_INT321 : RTREE_COORD_REAL320);
215256 sqlite3_str *pSql;
215257 char *zSql;
215258 int ii = 4;
215259 int iErr;
215260
215261 const char *aErrMsg[] = {
215262 0, /* 0 */
215263 "Wrong number of columns for an rtree table", /* 1 */
215264 "Too few columns for an rtree table", /* 2 */
215265 "Too many columns for an rtree table", /* 3 */
215266 "Auxiliary rtree columns must be last" /* 4 */
215267 };
215268
215269 assert( RTREE_MAX_AUX_COLUMN<256 )((void) (0)); /* Aux columns counted by a u8 */
215270 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN100+3 ){
215271 *pzErr = sqlite3_mprintf("%s", aErrMsg[2 + (argc>=6)]);
215272 return SQLITE_ERROR1;
215273 }
215274
215275 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
215276 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
215277
215278
215279 /* Allocate the sqlite3_vtab structure */
215280 nDb = (int)strlen(argv[1]);
215281 nName = (int)strlen(argv[2]);
215282 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
215283 if( !pRtree ){
215284 return SQLITE_NOMEM7;
215285 }
215286 memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
215287 pRtree->nBusy = 1;
215288 pRtree->base.pModule = &rtreeModule;
215289 pRtree->zDb = (char *)&pRtree[1];
215290 pRtree->zName = &pRtree->zDb[nDb+1];
215291 pRtree->zNodeName = &pRtree->zName[nName+1];
215292 pRtree->eCoordType = (u8)eCoordType;
215293 memcpy(pRtree->zDb, argv[1], nDb);
215294 memcpy(pRtree->zName, argv[2], nName);
215295 memcpy(pRtree->zNodeName, argv[2], nName);
215296 memcpy(&pRtree->zNodeName[nName], "_node", 6);
215297
215298
215299 /* Create/Connect to the underlying relational database schema. If
215300 ** that is successful, call sqlite3_declare_vtab() to configure
215301 ** the r-tree table schema.
215302 */
215303 pSql = sqlite3_str_new(db);
215304 sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT",
215305 rtreeTokenLength(argv[3]), argv[3]);
215306 for(ii=4; ii<argc; ii++){
215307 const char *zArg = argv[ii];
215308 if( zArg[0]=='+' ){
215309 pRtree->nAux++;
215310 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
215311 }else if( pRtree->nAux>0 ){
215312 break;
215313 }else{
215314 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
215315 pRtree->nDim2++;
215316 sqlite3_str_appendf(pSql, azFormat[eCoordType],
215317 rtreeTokenLength(zArg), zArg);
215318 }
215319 }
215320 sqlite3_str_appendf(pSql, ");");
215321 zSql = sqlite3_str_finish(pSql);
215322 if( !zSql ){
215323 rc = SQLITE_NOMEM7;
215324 }else if( ii<argc ){
215325 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
215326 rc = SQLITE_ERROR1;
215327 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
215328 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
215329 }
215330 sqlite3_free(zSql);
215331 if( rc ) goto rtreeInit_fail;
215332 pRtree->nDim = pRtree->nDim2/2;
215333 if( pRtree->nDim<1 ){
215334 iErr = 2;
215335 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS5*2 ){
215336 iErr = 3;
215337 }else if( pRtree->nDim2 % 2 ){
215338 iErr = 1;
215339 }else{
215340 iErr = 0;
215341 }
215342 if( iErr ){
215343 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
215344 goto rtreeInit_fail;
215345 }
215346 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
215347
215348 /* Figure out the node size to use. */
215349 rc = getNodeSize(db, pRtree, isCreate, pzErr);
215350 if( rc ) goto rtreeInit_fail;
215351 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
215352 if( rc ){
215353 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
215354 goto rtreeInit_fail;
215355 }
215356
215357 *ppVtab = (sqlite3_vtab *)pRtree;
215358 return SQLITE_OK0;
215359
215360rtreeInit_fail:
215361 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
215362 assert( *ppVtab==0 )((void) (0));
215363 assert( pRtree->nBusy==1 )((void) (0));
215364 rtreeRelease(pRtree);
215365 return rc;
215366}
215367
215368
215369/*
215370** Implementation of a scalar function that decodes r-tree nodes to
215371** human readable strings. This can be used for debugging and analysis.
215372**
215373** The scalar function takes two arguments: (1) the number of dimensions
215374** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
215375** an r-tree node. For a two-dimensional r-tree structure called "rt", to
215376** deserialize all nodes, a statement like:
215377**
215378** SELECT rtreenode(2, data) FROM rt_node;
215379**
215380** The human readable string takes the form of a Tcl list with one
215381** entry for each cell in the r-tree node. Each entry is itself a
215382** list, containing the 8-byte rowid/pageno followed by the
215383** <num-dimension>*2 coordinates.
215384*/
215385static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
215386 RtreeNode node;
215387 Rtree tree;
215388 int ii;
215389 int nData;
215390 int errCode;
215391 sqlite3_str *pOut;
215392
215393 UNUSED_PARAMETER(nArg)(void)(nArg);
215394 memset(&node, 0, sizeof(RtreeNode));
215395 memset(&tree, 0, sizeof(Rtree));
215396 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
215397 if( tree.nDim<1 || tree.nDim>5 ) return;
215398 tree.nDim2 = tree.nDim*2;
215399 tree.nBytesPerCell = 8 + 8 * tree.nDim;
215400 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
215401 if( node.zData==0 ) return;
215402 nData = sqlite3_value_bytes(apArg[1]);
215403 if( nData<4 ) return;
215404 if( nData<NCELL(&node)readInt16(&(&node)->zData[2])*tree.nBytesPerCell ) return;
215405
215406 pOut = sqlite3_str_new(0);
215407 for(ii=0; ii<NCELL(&node)readInt16(&(&node)->zData[2]); ii++){
215408 RtreeCell cell;
215409 int jj;
215410
215411 nodeGetCell(&tree, &node, ii, &cell);
215412 if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
215413 sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
215414 for(jj=0; jj<tree.nDim2; jj++){
215415#ifndef SQLITE_RTREE_INT_ONLY
215416 sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
215417#else
215418 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
215419#endif
215420 }
215421 sqlite3_str_append(pOut, "}", 1);
215422 }
215423 errCode = sqlite3_str_errcode(pOut);
215424 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
215425 sqlite3_result_error_code(ctx, errCode);
215426}
215427
215428/* This routine implements an SQL function that returns the "depth" parameter
215429** from the front of a blob that is an r-tree node. For example:
215430**
215431** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
215432**
215433** The depth value is 0 for all nodes other than the root node, and the root
215434** node always has nodeno=1, so the example above is the primary use for this
215435** routine. This routine is intended for testing and analysis only.
215436*/
215437static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
215438 UNUSED_PARAMETER(nArg)(void)(nArg);
215439 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB4
215440 || sqlite3_value_bytes(apArg[0])<2
215441
215442 ){
215443 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
215444 }else{
215445 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
215446 if( zBlob ){
215447 sqlite3_result_int(ctx, readInt16(zBlob));
215448 }else{
215449 sqlite3_result_error_nomem(ctx);
215450 }
215451 }
215452}
215453
215454/*
215455** Context object passed between the various routines that make up the
215456** implementation of integrity-check function rtreecheck().
215457*/
215458typedef struct RtreeCheck RtreeCheck;
215459struct RtreeCheck {
215460 sqlite3 *db; /* Database handle */
215461 const char *zDb; /* Database containing rtree table */
215462 const char *zTab; /* Name of rtree table */
215463 int bInt; /* True for rtree_i32 table */
215464 int nDim; /* Number of dimensions for this rtree tbl */
215465 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
215466 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
215467 int nLeaf; /* Number of leaf cells in table */
215468 int nNonLeaf; /* Number of non-leaf cells in table */
215469 int rc; /* Return code */
215470 char *zReport; /* Message to report */
215471 int nErr; /* Number of lines in zReport */
215472};
215473
215474#define RTREE_CHECK_MAX_ERROR100 100
215475
215476/*
215477** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
215478** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
215479*/
215480static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
215481 int rc = sqlite3_reset(pStmt);
215482 if( pCheck->rc==SQLITE_OK0 ) pCheck->rc = rc;
215483}
215484
215485/*
215486** The second and subsequent arguments to this function are a format string
215487** and printf style arguments. This function formats the string and attempts
215488** to compile it as an SQL statement.
215489**
215490** If successful, a pointer to the new SQL statement is returned. Otherwise,
215491** NULL is returned and an error code left in RtreeCheck.rc.
215492*/
215493static sqlite3_stmt *rtreeCheckPrepare(
215494 RtreeCheck *pCheck, /* RtreeCheck object */
215495 const char *zFmt, ... /* Format string and trailing args */
215496){
215497 va_list ap;
215498 char *z;
215499 sqlite3_stmt *pRet = 0;
215500
215501 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
215502 z = sqlite3_vmprintf(zFmt, ap);
215503
215504 if( pCheck->rc==SQLITE_OK0 ){
215505 if( z==0 ){
215506 pCheck->rc = SQLITE_NOMEM7;
215507 }else{
215508 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
215509 }
215510 }
215511
215512 sqlite3_free(z);
215513 va_end(ap)__builtin_va_end(ap);
215514 return pRet;
215515}
215516
215517/*
215518** The second and subsequent arguments to this function are a printf()
215519** style format string and arguments. This function formats the string and
215520** appends it to the report being accumuated in pCheck.
215521*/
215522static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
215523 va_list ap;
215524 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
215525 if( pCheck->rc==SQLITE_OK0 && pCheck->nErr<RTREE_CHECK_MAX_ERROR100 ){
215526 char *z = sqlite3_vmprintf(zFmt, ap);
215527 if( z==0 ){
215528 pCheck->rc = SQLITE_NOMEM7;
215529 }else{
215530 pCheck->zReport = sqlite3_mprintf("%z%s%z",
215531 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
215532 );
215533 if( pCheck->zReport==0 ){
215534 pCheck->rc = SQLITE_NOMEM7;
215535 }
215536 }
215537 pCheck->nErr++;
215538 }
215539 va_end(ap)__builtin_va_end(ap);
215540}
215541
215542/*
215543** This function is a no-op if there is already an error code stored
215544** in the RtreeCheck object indicated by the first argument. NULL is
215545** returned in this case.
215546**
215547** Otherwise, the contents of rtree table node iNode are loaded from
215548** the database and copied into a buffer obtained from sqlite3_malloc().
215549** If no error occurs, a pointer to the buffer is returned and (*pnNode)
215550** is set to the size of the buffer in bytes.
215551**
215552** Or, if an error does occur, NULL is returned and an error code left
215553** in the RtreeCheck object. The final value of *pnNode is undefined in
215554** this case.
215555*/
215556static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
215557 u8 *pRet = 0; /* Return value */
215558
215559 if( pCheck->rc==SQLITE_OK0 && pCheck->pGetNode==0 ){
215560 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
215561 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
215562 pCheck->zDb, pCheck->zTab
215563 );
215564 }
215565
215566 if( pCheck->rc==SQLITE_OK0 ){
215567 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
215568 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW100 ){
215569 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
215570 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
215571 pRet = sqlite3_malloc64(nNode);
215572 if( pRet==0 ){
215573 pCheck->rc = SQLITE_NOMEM7;
215574 }else{
215575 memcpy(pRet, pNode, nNode);
215576 *pnNode = nNode;
215577 }
215578 }
215579 rtreeCheckReset(pCheck, pCheck->pGetNode);
215580 if( pCheck->rc==SQLITE_OK0 && pRet==0 ){
215581 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
215582 }
215583 }
215584
215585 return pRet;
215586}
215587
215588/*
215589** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
215590** (if bLeaf==1) table contains a specified entry. The schemas of the
215591** two tables are:
215592**
215593** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
215594** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
215595**
215596** In both cases, this function checks that there exists an entry with
215597** IPK value iKey and the second column set to iVal.
215598**
215599*/
215600static void rtreeCheckMapping(
215601 RtreeCheck *pCheck, /* RtreeCheck object */
215602 int bLeaf, /* True for a leaf cell, false for interior */
215603 i64 iKey, /* Key for mapping */
215604 i64 iVal /* Expected value for mapping */
215605){
215606 int rc;
215607 sqlite3_stmt *pStmt;
215608 const char *azSql[2] = {
215609 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
215610 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
215611 };
215612
215613 assert( bLeaf==0 || bLeaf==1 )((void) (0));
215614 if( pCheck->aCheckMapping[bLeaf]==0 ){
215615 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
215616 azSql[bLeaf], pCheck->zDb, pCheck->zTab
215617 );
215618 }
215619 if( pCheck->rc!=SQLITE_OK0 ) return;
215620
215621 pStmt = pCheck->aCheckMapping[bLeaf];
215622 sqlite3_bind_int64(pStmt, 1, iKey);
215623 rc = sqlite3_step(pStmt);
215624 if( rc==SQLITE_DONE101 ){
215625 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
215626 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
215627 );
215628 }else if( rc==SQLITE_ROW100 ){
215629 i64 ii = sqlite3_column_int64(pStmt, 0);
215630 if( ii!=iVal ){
215631 rtreeCheckAppendMsg(pCheck,
215632 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
215633 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
215634 );
215635 }
215636 }
215637 rtreeCheckReset(pCheck, pStmt);
215638}
215639
215640/*
215641** Argument pCell points to an array of coordinates stored on an rtree page.
215642** This function checks that the coordinates are internally consistent (no
215643** x1>x2 conditions) and adds an error message to the RtreeCheck object
215644** if they are not.
215645**
215646** Additionally, if pParent is not NULL, then it is assumed to point to
215647** the array of coordinates on the parent page that bound the page
215648** containing pCell. In this case it is also verified that the two
215649** sets of coordinates are mutually consistent and an error message added
215650** to the RtreeCheck object if they are not.
215651*/
215652static void rtreeCheckCellCoord(
215653 RtreeCheck *pCheck,
215654 i64 iNode, /* Node id to use in error messages */
215655 int iCell, /* Cell number to use in error messages */
215656 u8 *pCell, /* Pointer to cell coordinates */
215657 u8 *pParent /* Pointer to parent coordinates */
215658){
215659 RtreeCoord c1, c2;
215660 RtreeCoord p1, p2;
215661 int i;
215662
215663 for(i=0; i<pCheck->nDim; i++){
215664 readCoord(&pCell[4*2*i], &c1);
215665 readCoord(&pCell[4*(2*i + 1)], &c2);
215666
215667 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
215668 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
215669 rtreeCheckAppendMsg(pCheck,
215670 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
215671 );
215672 }
215673
215674 if( pParent ){
215675 readCoord(&pParent[4*2*i], &p1);
215676 readCoord(&pParent[4*(2*i + 1)], &p2);
215677
215678 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
215679 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
215680 ){
215681 rtreeCheckAppendMsg(pCheck,
215682 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
215683 , i, iCell, iNode
215684 );
215685 }
215686 }
215687 }
215688}
215689
215690/*
215691** Run rtreecheck() checks on node iNode, which is at depth iDepth within
215692** the r-tree structure. Argument aParent points to the array of coordinates
215693** that bound node iNode on the parent node.
215694**
215695** If any problems are discovered, an error message is appended to the
215696** report accumulated in the RtreeCheck object.
215697*/
215698static void rtreeCheckNode(
215699 RtreeCheck *pCheck,
215700 int iDepth, /* Depth of iNode (0==leaf) */
215701 u8 *aParent, /* Buffer containing parent coords */
215702 i64 iNode /* Node to check */
215703){
215704 u8 *aNode = 0;
215705 int nNode = 0;
215706
215707 assert( iNode==1 || aParent!=0 )((void) (0));
215708 assert( pCheck->nDim>0 )((void) (0));
215709
215710 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
215711 if( aNode ){
215712 if( nNode<4 ){
215713 rtreeCheckAppendMsg(pCheck,
215714 "Node %lld is too small (%d bytes)", iNode, nNode
215715 );
215716 }else{
215717 int nCell; /* Number of cells on page */
215718 int i; /* Used to iterate through cells */
215719 if( aParent==0 ){
215720 iDepth = readInt16(aNode);
215721 if( iDepth>RTREE_MAX_DEPTH40 ){
215722 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
215723 sqlite3_free(aNode);
215724 return;
215725 }
215726 }
215727 nCell = readInt16(&aNode[2]);
215728 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
215729 rtreeCheckAppendMsg(pCheck,
215730 "Node %lld is too small for cell count of %d (%d bytes)",
215731 iNode, nCell, nNode
215732 );
215733 }else{
215734 for(i=0; i<nCell; i++){
215735 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
215736 i64 iVal = readInt64(pCell);
215737 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
215738
215739 if( iDepth>0 ){
215740 rtreeCheckMapping(pCheck, 0, iVal, iNode);
215741 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
215742 pCheck->nNonLeaf++;
215743 }else{
215744 rtreeCheckMapping(pCheck, 1, iVal, iNode);
215745 pCheck->nLeaf++;
215746 }
215747 }
215748 }
215749 }
215750 sqlite3_free(aNode);
215751 }
215752}
215753
215754/*
215755** The second argument to this function must be either "_rowid" or
215756** "_parent". This function checks that the number of entries in the
215757** %_rowid or %_parent table is exactly nExpect. If not, it adds
215758** an error message to the report in the RtreeCheck object indicated
215759** by the first argument.
215760*/
215761static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
215762 if( pCheck->rc==SQLITE_OK0 ){
215763 sqlite3_stmt *pCount;
215764 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
215765 pCheck->zDb, pCheck->zTab, zTbl
215766 );
215767 if( pCount ){
215768 if( sqlite3_step(pCount)==SQLITE_ROW100 ){
215769 i64 nActual = sqlite3_column_int64(pCount, 0);
215770 if( nActual!=nExpect ){
215771 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
215772 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
215773 );
215774 }
215775 }
215776 pCheck->rc = sqlite3_finalize(pCount);
215777 }
215778 }
215779}
215780
215781/*
215782** This function does the bulk of the work for the rtree integrity-check.
215783** It is called by rtreecheck(), which is the SQL function implementation.
215784*/
215785static int rtreeCheckTable(
215786 sqlite3 *db, /* Database handle to access db through */
215787 const char *zDb, /* Name of db ("main", "temp" etc.) */
215788 const char *zTab, /* Name of rtree table to check */
215789 char **pzReport /* OUT: sqlite3_malloc'd report text */
215790){
215791 RtreeCheck check; /* Common context for various routines */
215792 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
215793 int nAux = 0; /* Number of extra columns. */
215794
215795 /* Initialize the context object */
215796 memset(&check, 0, sizeof(check));
215797 check.db = db;
215798 check.zDb = zDb;
215799 check.zTab = zTab;
215800
215801 /* Find the number of auxiliary columns */
215802 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
215803 if( pStmt ){
215804 nAux = sqlite3_column_count(pStmt) - 2;
215805 sqlite3_finalize(pStmt);
215806 }else
215807 if( check.rc!=SQLITE_NOMEM7 ){
215808 check.rc = SQLITE_OK0;
215809 }
215810
215811 /* Find number of dimensions in the rtree table. */
215812 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
215813 if( pStmt ){
215814 int rc;
215815 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
215816 if( check.nDim<1 ){
215817 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
215818 }else if( SQLITE_ROW100==sqlite3_step(pStmt) ){
215819 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER1);
215820 }
215821 rc = sqlite3_finalize(pStmt);
215822 if( rc!=SQLITE_CORRUPT11 ) check.rc = rc;
215823 }
215824
215825 /* Do the actual integrity-check */
215826 if( check.nDim>=1 ){
215827 if( check.rc==SQLITE_OK0 ){
215828 rtreeCheckNode(&check, 0, 0, 1);
215829 }
215830 rtreeCheckCount(&check, "_rowid", check.nLeaf);
215831 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
215832 }
215833
215834 /* Finalize SQL statements used by the integrity-check */
215835 sqlite3_finalize(check.pGetNode);
215836 sqlite3_finalize(check.aCheckMapping[0]);
215837 sqlite3_finalize(check.aCheckMapping[1]);
215838
215839 *pzReport = check.zReport;
215840 return check.rc;
215841}
215842
215843/*
215844** Implementation of the xIntegrity method for Rtree.
215845*/
215846static int rtreeIntegrity(
215847 sqlite3_vtab *pVtab, /* The virtual table to check */
215848 const char *zSchema, /* Schema in which the virtual table lives */
215849 const char *zName, /* Name of the virtual table */
215850 int isQuick, /* True for a quick_check */
215851 char **pzErr /* Write results here */
215852){
215853 Rtree *pRtree = (Rtree*)pVtab;
215854 int rc;
215855 assert( pzErr!=0 && *pzErr==0 )((void) (0));
215856 UNUSED_PARAMETER(zSchema)(void)(zSchema);
215857 UNUSED_PARAMETER(zName)(void)(zName);
215858 UNUSED_PARAMETER(isQuick)(void)(isQuick);
215859 rc = rtreeCheckTable(pRtree->db, pRtree->zDb, pRtree->zName, pzErr);
215860 if( rc==SQLITE_OK0 && *pzErr ){
215861 *pzErr = sqlite3_mprintf("In RTree %s.%s:\n%z",
215862 pRtree->zDb, pRtree->zName, *pzErr);
215863 if( (*pzErr)==0 ) rc = SQLITE_NOMEM7;
215864 }
215865 return rc;
215866}
215867
215868/*
215869** Usage:
215870**
215871** rtreecheck(<rtree-table>);
215872** rtreecheck(<database>, <rtree-table>);
215873**
215874** Invoking this SQL function runs an integrity-check on the named rtree
215875** table. The integrity-check verifies the following:
215876**
215877** 1. For each cell in the r-tree structure (%_node table), that:
215878**
215879** a) for each dimension, (coord1 <= coord2).
215880**
215881** b) unless the cell is on the root node, that the cell is bounded
215882** by the parent cell on the parent node.
215883**
215884** c) for leaf nodes, that there is an entry in the %_rowid
215885** table corresponding to the cell's rowid value that
215886** points to the correct node.
215887**
215888** d) for cells on non-leaf nodes, that there is an entry in the
215889** %_parent table mapping from the cell's child node to the
215890** node that it resides on.
215891**
215892** 2. That there are the same number of entries in the %_rowid table
215893** as there are leaf cells in the r-tree structure, and that there
215894** is a leaf cell that corresponds to each entry in the %_rowid table.
215895**
215896** 3. That there are the same number of entries in the %_parent table
215897** as there are non-leaf cells in the r-tree structure, and that
215898** there is a non-leaf cell that corresponds to each entry in the
215899** %_parent table.
215900*/
215901static void rtreecheck(
215902 sqlite3_context *ctx,
215903 int nArg,
215904 sqlite3_value **apArg
215905){
215906 if( nArg!=1 && nArg!=2 ){
215907 sqlite3_result_error(ctx,
215908 "wrong number of arguments to function rtreecheck()", -1
215909 );
215910 }else{
215911 int rc;
215912 char *zReport = 0;
215913 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
215914 const char *zTab;
215915 if( nArg==1 ){
215916 zTab = zDb;
215917 zDb = "main";
215918 }else{
215919 zTab = (const char*)sqlite3_value_text(apArg[1]);
215920 }
215921 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
215922 if( rc==SQLITE_OK0 ){
215923 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
215924 }else{
215925 sqlite3_result_error_code(ctx, rc);
215926 }
215927 sqlite3_free(zReport);
215928 }
215929}
215930
215931/* Conditionally include the geopoly code */
215932#ifdef SQLITE_ENABLE_GEOPOLY
215933/************** Include geopoly.c in the middle of rtree.c *******************/
215934/************** Begin file geopoly.c *****************************************/
215935/*
215936** 2018-05-25
215937**
215938** The author disclaims copyright to this source code. In place of
215939** a legal notice, here is a blessing:
215940**
215941** May you do good and not evil.
215942** May you find forgiveness for yourself and forgive others.
215943** May you share freely, never taking more than you give.
215944**
215945******************************************************************************
215946**
215947** This file implements an alternative R-Tree virtual table that
215948** uses polygons to express the boundaries of 2-dimensional objects.
215949**
215950** This file is #include-ed onto the end of "rtree.c" so that it has
215951** access to all of the R-Tree internals.
215952*/
215953/* #include <stdlib.h> */
215954
215955/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
215956#ifdef GEOPOLY_ENABLE_DEBUG
215957 static int geo_debug = 0;
215958# define GEODEBUG(X) if(geo_debug)printf X
215959#else
215960# define GEODEBUG(X)
215961#endif
215962
215963/* Character class routines */
215964#ifdef sqlite3Isdigit
215965 /* Use the SQLite core versions if this routine is part of the
215966 ** SQLite amalgamation */
215967# define safe_isdigit(x) sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04)
215968# define safe_isalnum(x) sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06)
215969# define safe_isxdigit(x) sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08)
215970#else
215971 /* Use the standard library for separate compilation */
215972#include <ctype.h> /* amalgamator: keep */
215973# define safe_isdigit(x) isdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISdigit)
215974# define safe_isalnum(x) isalnum((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISalnum)
215975# define safe_isxdigit(x) isxdigit((unsigned char)(x))((*__ctype_b_loc ())[(int) (((unsigned char)(x)))] & (unsigned
short int) _ISxdigit)
215976#endif
215977
215978#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
215979/*
215980** Growing our own isspace() routine this way is twice as fast as
215981** the library isspace() function.
215982*/
215983static const char geopolyIsSpace[] = {
215984 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
215985 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215986 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215987 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215988 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215989 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215990 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215991 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215992 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215993 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215995 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215996 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215997 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215998 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215999 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216000};
216001#define fast_isspace(x) (geopolyIsSpace[(unsigned char)x])
216002#endif /* JSON NULL - back to original code */
216003
216004/* Compiler and version */
216005#ifndef GCC_VERSION(4*1000000+2*1000+1)
216006#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
216007# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
216008#else
216009# define GCC_VERSION(4*1000000+2*1000+1) 0
216010#endif
216011#endif
216012#ifndef MSVC_VERSION0
216013#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
216014# define MSVC_VERSION0 _MSC_VER
216015#else
216016# define MSVC_VERSION0 0
216017#endif
216018#endif
216019
216020/* Datatype for coordinates
216021*/
216022typedef float GeoCoord;
216023
216024/*
216025** Internal representation of a polygon.
216026**
216027** The polygon consists of a sequence of vertexes. There is a line
216028** segment between each pair of vertexes, and one final segment from
216029** the last vertex back to the first. (This differs from the GeoJSON
216030** standard in which the final vertex is a repeat of the first.)
216031**
216032** The polygon follows the right-hand rule. The area to the right of
216033** each segment is "outside" and the area to the left is "inside".
216034**
216035** The on-disk representation consists of a 4-byte header followed by
216036** the values. The 4-byte header is:
216037**
216038** encoding (1 byte) 0=big-endian, 1=little-endian
216039** nvertex (3 bytes) Number of vertexes as a big-endian integer
216040**
216041** Enough space is allocated for 4 coordinates, to work around over-zealous
216042** warnings coming from some compiler (notably, clang). In reality, the size
216043** of each GeoPoly memory allocate is adjusted as necessary so that the
216044** GeoPoly.a[] array at the end is the appropriate size.
216045*/
216046typedef struct GeoPoly GeoPoly;
216047struct GeoPoly {
216048 int nVertex; /* Number of vertexes */
216049 unsigned char hdr[4]; /* Header for on-disk representation */
216050 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
216051};
216052
216053/* The size of a memory allocation needed for a GeoPoly object sufficient
216054** to hold N coordinate pairs.
216055*/
216056#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
216057
216058/* Macros to access coordinates of a GeoPoly.
216059** We have to use these macros, rather than just say p->a[i] in order
216060** to silence (incorrect) UBSAN warnings if the array index is too large.
216061*/
216062#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
216063#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
216064
216065
216066/*
216067** State of a parse of a GeoJSON input.
216068*/
216069typedef struct GeoParse GeoParse;
216070struct GeoParse {
216071 const unsigned char *z; /* Unparsed input */
216072 int nVertex; /* Number of vertexes in a[] */
216073 int nAlloc; /* Space allocated to a[] */
216074 int nErr; /* Number of errors encountered */
216075 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
216076};
216077
216078/* Do a 4-byte byte swap */
216079static void geopolySwab32(unsigned char *a){
216080 unsigned char t = a[0];
216081 a[0] = a[3];
216082 a[3] = t;
216083 t = a[1];
216084 a[1] = a[2];
216085 a[2] = t;
216086}
216087
216088/* Skip whitespace. Return the next non-whitespace character. */
216089static char geopolySkipSpace(GeoParse *p){
216090 while( fast_isspace(p->z[0]) ) p->z++;
216091 return p->z[0];
216092}
216093
216094/* Parse out a number. Write the value into *pVal if pVal!=0.
216095** return non-zero on success and zero if the next token is not a number.
216096*/
216097static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
216098 char c = geopolySkipSpace(p);
216099 const unsigned char *z = p->z;
216100 int j = 0;
216101 int seenDP = 0;
216102 int seenE = 0;
216103 if( c=='-' ){
216104 j = 1;
216105 c = z[j];
216106 }
216107 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
216108 for(;; j++){
216109 c = z[j];
216110 if( safe_isdigit(c) ) continue;
216111 if( c=='.' ){
216112 if( z[j-1]=='-' ) return 0;
216113 if( seenDP ) return 0;
216114 seenDP = 1;
216115 continue;
216116 }
216117 if( c=='e' || c=='E' ){
216118 if( z[j-1]<'0' ) return 0;
216119 if( seenE ) return -1;
216120 seenDP = seenE = 1;
216121 c = z[j+1];
216122 if( c=='+' || c=='-' ){
216123 j++;
216124 c = z[j+1];
216125 }
216126 if( c<'0' || c>'9' ) return 0;
216127 continue;
216128 }
216129 break;
216130 }
216131 if( z[j-1]<'0' ) return 0;
216132 if( pVal ){
216133#ifdef SQLITE_AMALGAMATION1
216134 /* The sqlite3AtoF() routine is much much faster than atof(), if it
216135 ** is available */
216136 double r;
216137 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF81);
216138 *pVal = r;
216139#else
216140 *pVal = (GeoCoord)atof((const char*)p->z);
216141#endif
216142 }
216143 p->z += j;
216144 return 1;
216145}
216146
216147/*
216148** If the input is a well-formed JSON array of coordinates with at least
216149** four coordinates and where each coordinate is itself a two-value array,
216150** then convert the JSON into a GeoPoly object and return a pointer to
216151** that object.
216152**
216153** If any error occurs, return NULL.
216154*/
216155static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
216156 GeoParse s;
216157 int rc = SQLITE_OK0;
216158 memset(&s, 0, sizeof(s));
216159 s.z = z;
216160 if( geopolySkipSpace(&s)=='[' ){
216161 s.z++;
216162 while( geopolySkipSpace(&s)=='[' ){
216163 int ii = 0;
216164 char c;
216165 s.z++;
216166 if( s.nVertex>=s.nAlloc ){
216167 GeoCoord *aNew;
216168 s.nAlloc = s.nAlloc*2 + 16;
216169 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
216170 if( aNew==0 ){
216171 rc = SQLITE_NOMEM7;
216172 s.nErr++;
216173 break;
216174 }
216175 s.a = aNew;
216176 }
216177 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
216178 ii++;
216179 if( ii==2 ) s.nVertex++;
216180 c = geopolySkipSpace(&s);
216181 s.z++;
216182 if( c==',' ) continue;
216183 if( c==']' && ii>=2 ) break;
216184 s.nErr++;
216185 rc = SQLITE_ERROR1;
216186 goto parse_json_err;
216187 }
216188 if( geopolySkipSpace(&s)==',' ){
216189 s.z++;
216190 continue;
216191 }
216192 break;
216193 }
216194 if( geopolySkipSpace(&s)==']'
216195 && s.nVertex>=4
216196 && s.a[0]==s.a[s.nVertex*2-2]
216197 && s.a[1]==s.a[s.nVertex*2-1]
216198 && (s.z++, geopolySkipSpace(&s)==0)
216199 ){
216200 GeoPoly *pOut;
216201 int x = 1;
216202 s.nVertex--; /* Remove the redundant vertex at the end */
216203 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
216204 x = 1;
216205 if( pOut==0 ) goto parse_json_err;
216206 pOut->nVertex = s.nVertex;
216207 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
216208 pOut->hdr[0] = *(unsigned char*)&x;
216209 pOut->hdr[1] = (s.nVertex>>16)&0xff;
216210 pOut->hdr[2] = (s.nVertex>>8)&0xff;
216211 pOut->hdr[3] = s.nVertex&0xff;
216212 sqlite3_free(s.a);
216213 if( pRc ) *pRc = SQLITE_OK0;
216214 return pOut;
216215 }else{
216216 s.nErr++;
216217 rc = SQLITE_ERROR1;
216218 }
216219 }
216220parse_json_err:
216221 if( pRc ) *pRc = rc;
216222 sqlite3_free(s.a);
216223 return 0;
216224}
216225
216226/*
216227** Given a function parameter, try to interpret it as a polygon, either
216228** in the binary format or JSON text. Compute a GeoPoly object and
216229** return a pointer to that object. Or if the input is not a well-formed
216230** polygon, put an error message in sqlite3_context and return NULL.
216231*/
216232static GeoPoly *geopolyFuncParam(
216233 sqlite3_context *pCtx, /* Context for error messages */
216234 sqlite3_value *pVal, /* The value to decode */
216235 int *pRc /* Write error here */
216236){
216237 GeoPoly *p = 0;
216238 int nByte;
216239 testcase( pCtx==0 );
216240 if( sqlite3_value_type(pVal)==SQLITE_BLOB4
216241 && (nByte = sqlite3_value_bytes(pVal))>=(int)(4+6*sizeof(GeoCoord))
216242 ){
216243 const unsigned char *a = sqlite3_value_blob(pVal);
216244 int nVertex;
216245 if( a==0 ){
216246 if( pCtx ) sqlite3_result_error_nomem(pCtx);
216247 return 0;
216248 }
216249 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
216250 if( (a[0]==0 || a[0]==1)
216251 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
216252 ){
216253 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
216254 if( p==0 ){
216255 if( pRc ) *pRc = SQLITE_NOMEM7;
216256 if( pCtx ) sqlite3_result_error_nomem(pCtx);
216257 }else{
216258 int x = 1;
216259 p->nVertex = nVertex;
216260 memcpy(p->hdr, a, nByte);
216261 if( a[0] != *(unsigned char*)&x ){
216262 int ii;
216263 for(ii=0; ii<nVertex; ii++){
216264 geopolySwab32((unsigned char*)&GeoX(p,ii));
216265 geopolySwab32((unsigned char*)&GeoY(p,ii));
216266 }
216267 p->hdr[0] ^= 1;
216268 }
216269 }
216270 }
216271 if( pRc ) *pRc = SQLITE_OK0;
216272 return p;
216273 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
216274 const unsigned char *zJson = sqlite3_value_text(pVal);
216275 if( zJson==0 ){
216276 if( pRc ) *pRc = SQLITE_NOMEM7;
216277 return 0;
216278 }
216279 return geopolyParseJson(zJson, pRc);
216280 }else{
216281 if( pRc ) *pRc = SQLITE_ERROR1;
216282 return 0;
216283 }
216284}
216285
216286/*
216287** Implementation of the geopoly_blob(X) function.
216288**
216289** If the input is a well-formed Geopoly BLOB or JSON string
216290** then return the BLOB representation of the polygon. Otherwise
216291** return NULL.
216292*/
216293static void geopolyBlobFunc(
216294 sqlite3_context *context,
216295 int argc,
216296 sqlite3_value **argv
216297){
216298 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
216299 (void)argc;
216300 if( p ){
216301 sqlite3_result_blob(context, p->hdr,
216302 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216303 sqlite3_free(p);
216304 }
216305}
216306
216307/*
216308** SQL function: geopoly_json(X)
216309**
216310** Interpret X as a polygon and render it as a JSON array
216311** of coordinates. Or, if X is not a valid polygon, return NULL.
216312*/
216313static void geopolyJsonFunc(
216314 sqlite3_context *context,
216315 int argc,
216316 sqlite3_value **argv
216317){
216318 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
216319 (void)argc;
216320 if( p ){
216321 sqlite3 *db = sqlite3_context_db_handle(context);
216322 sqlite3_str *x = sqlite3_str_new(db);
216323 int i;
216324 sqlite3_str_append(x, "[", 1);
216325 for(i=0; i<p->nVertex; i++){
216326 sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
216327 }
216328 sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
216329 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
216330 sqlite3_free(p);
216331 }
216332}
216333
216334/*
216335** SQL function: geopoly_svg(X, ....)
216336**
216337** Interpret X as a polygon and render it as a SVG <polyline>.
216338** Additional arguments are added as attributes to the <polyline>.
216339*/
216340static void geopolySvgFunc(
216341 sqlite3_context *context,
216342 int argc,
216343 sqlite3_value **argv
216344){
216345 GeoPoly *p;
216346 if( argc<1 ) return;
216347 p = geopolyFuncParam(context, argv[0], 0);
216348 if( p ){
216349 sqlite3 *db = sqlite3_context_db_handle(context);
216350 sqlite3_str *x = sqlite3_str_new(db);
216351 int i;
216352 char cSep = '\'';
216353 sqlite3_str_appendf(x, "<polyline points=");
216354 for(i=0; i<p->nVertex; i++){
216355 sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
216356 cSep = ' ';
216357 }
216358 sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
216359 for(i=1; i<argc; i++){
216360 const char *z = (const char*)sqlite3_value_text(argv[i]);
216361 if( z && z[0] ){
216362 sqlite3_str_appendf(x, " %s", z);
216363 }
216364 }
216365 sqlite3_str_appendf(x, "></polyline>");
216366 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
216367 sqlite3_free(p);
216368 }
216369}
216370
216371/*
216372** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
216373**
216374** Transform and/or translate a polygon as follows:
216375**
216376** x1 = A*x0 + B*y0 + E
216377** y1 = C*x0 + D*y0 + F
216378**
216379** For a translation:
216380**
216381** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
216382**
216383** Rotate by R around the point (0,0):
216384**
216385** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
216386*/
216387static void geopolyXformFunc(
216388 sqlite3_context *context,
216389 int argc,
216390 sqlite3_value **argv
216391){
216392 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
216393 double A = sqlite3_value_double(argv[1]);
216394 double B = sqlite3_value_double(argv[2]);
216395 double C = sqlite3_value_double(argv[3]);
216396 double D = sqlite3_value_double(argv[4]);
216397 double E = sqlite3_value_double(argv[5]);
216398 double F = sqlite3_value_double(argv[6]);
216399 GeoCoord x1, y1, x0, y0;
216400 int ii;
216401 (void)argc;
216402 if( p ){
216403 for(ii=0; ii<p->nVertex; ii++){
216404 x0 = GeoX(p,ii);
216405 y0 = GeoY(p,ii);
216406 x1 = (GeoCoord)(A*x0 + B*y0 + E);
216407 y1 = (GeoCoord)(C*x0 + D*y0 + F);
216408 GeoX(p,ii) = x1;
216409 GeoY(p,ii) = y1;
216410 }
216411 sqlite3_result_blob(context, p->hdr,
216412 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216413 sqlite3_free(p);
216414 }
216415}
216416
216417/*
216418** Compute the area enclosed by the polygon.
216419**
216420** This routine can also be used to detect polygons that rotate in
216421** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
216422** This routine returns a negative value for clockwise (CW) polygons.
216423*/
216424static double geopolyArea(GeoPoly *p){
216425 double rArea = 0.0;
216426 int ii;
216427 for(ii=0; ii<p->nVertex-1; ii++){
216428 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
216429 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
216430 * 0.5;
216431 }
216432 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
216433 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
216434 * 0.5;
216435 return rArea;
216436}
216437
216438/*
216439** Implementation of the geopoly_area(X) function.
216440**
216441** If the input is a well-formed Geopoly BLOB then return the area
216442** enclosed by the polygon. If the polygon circulates clockwise instead
216443** of counterclockwise (as it should) then return the negative of the
216444** enclosed area. Otherwise return NULL.
216445*/
216446static void geopolyAreaFunc(
216447 sqlite3_context *context,
216448 int argc,
216449 sqlite3_value **argv
216450){
216451 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
216452 (void)argc;
216453 if( p ){
216454 sqlite3_result_double(context, geopolyArea(p));
216455 sqlite3_free(p);
216456 }
216457}
216458
216459/*
216460** Implementation of the geopoly_ccw(X) function.
216461**
216462** If the rotation of polygon X is clockwise (incorrect) instead of
216463** counter-clockwise (the correct winding order according to RFC7946)
216464** then reverse the order of the vertexes in polygon X.
216465**
216466** In other words, this routine returns a CCW polygon regardless of the
216467** winding order of its input.
216468**
216469** Use this routine to sanitize historical inputs that that sometimes
216470** contain polygons that wind in the wrong direction.
216471*/
216472static void geopolyCcwFunc(
216473 sqlite3_context *context,
216474 int argc,
216475 sqlite3_value **argv
216476){
216477 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
216478 (void)argc;
216479 if( p ){
216480 if( geopolyArea(p)<0.0 ){
216481 int ii, jj;
216482 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
216483 GeoCoord t = GeoX(p,ii);
216484 GeoX(p,ii) = GeoX(p,jj);
216485 GeoX(p,jj) = t;
216486 t = GeoY(p,ii);
216487 GeoY(p,ii) = GeoY(p,jj);
216488 GeoY(p,jj) = t;
216489 }
216490 }
216491 sqlite3_result_blob(context, p->hdr,
216492 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216493 sqlite3_free(p);
216494 }
216495}
216496
216497#define GEOPOLY_PI 3.1415926535897932385
216498
216499/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
216500*/
216501static double geopolySine(double r){
216502 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI )((void) (0));
216503 if( r>=1.5*GEOPOLY_PI ){
216504 r -= 2.0*GEOPOLY_PI;
216505 }
216506 if( r>=0.5*GEOPOLY_PI ){
216507 return -geopolySine(r-GEOPOLY_PI);
216508 }else{
216509 double r2 = r*r;
216510 double r3 = r2*r;
216511 double r5 = r3*r2;
216512 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
216513 }
216514}
216515
216516/*
216517** Function: geopoly_regular(X,Y,R,N)
216518**
216519** Construct a simple, convex, regular polygon centered at X, Y
216520** with circumradius R and with N sides.
216521*/
216522static void geopolyRegularFunc(
216523 sqlite3_context *context,
216524 int argc,
216525 sqlite3_value **argv
216526){
216527 double x = sqlite3_value_double(argv[0]);
216528 double y = sqlite3_value_double(argv[1]);
216529 double r = sqlite3_value_double(argv[2]);
216530 int n = sqlite3_value_int(argv[3]);
216531 int i;
216532 GeoPoly *p;
216533 (void)argc;
216534
216535 if( n<3 || r<=0.0 ) return;
216536 if( n>1000 ) n = 1000;
216537 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
216538 if( p==0 ){
216539 sqlite3_result_error_nomem(context);
216540 return;
216541 }
216542 i = 1;
216543 p->hdr[0] = *(unsigned char*)&i;
216544 p->hdr[1] = 0;
216545 p->hdr[2] = (n>>8)&0xff;
216546 p->hdr[3] = n&0xff;
216547 for(i=0; i<n; i++){
216548 double rAngle = 2.0*GEOPOLY_PI*i/n;
216549 GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
216550 GeoY(p,i) = y + r*geopolySine(rAngle);
216551 }
216552 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216553 sqlite3_free(p);
216554}
216555
216556/*
216557** If pPoly is a polygon, compute its bounding box. Then:
216558**
216559** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
216560** (2) otherwise, compute a GeoPoly for the bounding box and return the
216561** new GeoPoly
216562**
216563** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
216564** the bounding box in aCoord and return a pointer to that GeoPoly.
216565*/
216566static GeoPoly *geopolyBBox(
216567 sqlite3_context *context, /* For recording the error */
216568 sqlite3_value *pPoly, /* The polygon */
216569 RtreeCoord *aCoord, /* Results here */
216570 int *pRc /* Error code here */
216571){
216572 GeoPoly *pOut = 0;
216573 GeoPoly *p;
216574 float mnX, mxX, mnY, mxY;
216575 if( pPoly==0 && aCoord!=0 ){
216576 p = 0;
216577 mnX = aCoord[0].f;
216578 mxX = aCoord[1].f;
216579 mnY = aCoord[2].f;
216580 mxY = aCoord[3].f;
216581 goto geopolyBboxFill;
216582 }else{
216583 p = geopolyFuncParam(context, pPoly, pRc);
216584 }
216585 if( p ){
216586 int ii;
216587 mnX = mxX = GeoX(p,0);
216588 mnY = mxY = GeoY(p,0);
216589 for(ii=1; ii<p->nVertex; ii++){
216590 double r = GeoX(p,ii);
216591 if( r<mnX ) mnX = (float)r;
216592 else if( r>mxX ) mxX = (float)r;
216593 r = GeoY(p,ii);
216594 if( r<mnY ) mnY = (float)r;
216595 else if( r>mxY ) mxY = (float)r;
216596 }
216597 if( pRc ) *pRc = SQLITE_OK0;
216598 if( aCoord==0 ){
216599 geopolyBboxFill:
216600 pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
216601 if( pOut==0 ){
216602 sqlite3_free(p);
216603 if( context ) sqlite3_result_error_nomem(context);
216604 if( pRc ) *pRc = SQLITE_NOMEM7;
216605 return 0;
216606 }
216607 pOut->nVertex = 4;
216608 ii = 1;
216609 pOut->hdr[0] = *(unsigned char*)&ii;
216610 pOut->hdr[1] = 0;
216611 pOut->hdr[2] = 0;
216612 pOut->hdr[3] = 4;
216613 GeoX(pOut,0) = mnX;
216614 GeoY(pOut,0) = mnY;
216615 GeoX(pOut,1) = mxX;
216616 GeoY(pOut,1) = mnY;
216617 GeoX(pOut,2) = mxX;
216618 GeoY(pOut,2) = mxY;
216619 GeoX(pOut,3) = mnX;
216620 GeoY(pOut,3) = mxY;
216621 }else{
216622 sqlite3_free(p);
216623 aCoord[0].f = mnX;
216624 aCoord[1].f = mxX;
216625 aCoord[2].f = mnY;
216626 aCoord[3].f = mxY;
216627 }
216628 }else if( aCoord ){
216629 memset(aCoord, 0, sizeof(RtreeCoord)*4);
216630 }
216631 return pOut;
216632}
216633
216634/*
216635** Implementation of the geopoly_bbox(X) SQL function.
216636*/
216637static void geopolyBBoxFunc(
216638 sqlite3_context *context,
216639 int argc,
216640 sqlite3_value **argv
216641){
216642 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
216643 (void)argc;
216644 if( p ){
216645 sqlite3_result_blob(context, p->hdr,
216646 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216647 sqlite3_free(p);
216648 }
216649}
216650
216651/*
216652** State vector for the geopoly_group_bbox() aggregate function.
216653*/
216654typedef struct GeoBBox GeoBBox;
216655struct GeoBBox {
216656 int isInit;
216657 RtreeCoord a[4];
216658};
216659
216660
216661/*
216662** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
216663*/
216664static void geopolyBBoxStep(
216665 sqlite3_context *context,
216666 int argc,
216667 sqlite3_value **argv
216668){
216669 RtreeCoord a[4];
216670 int rc = SQLITE_OK0;
216671 (void)argc;
216672 (void)geopolyBBox(context, argv[0], a, &rc);
216673 if( rc==SQLITE_OK0 ){
216674 GeoBBox *pBBox;
216675 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
216676 if( pBBox==0 ) return;
216677 if( pBBox->isInit==0 ){
216678 pBBox->isInit = 1;
216679 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
216680 }else{
216681 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
216682 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
216683 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
216684 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
216685 }
216686 }
216687}
216688static void geopolyBBoxFinal(
216689 sqlite3_context *context
216690){
216691 GeoPoly *p;
216692 GeoBBox *pBBox;
216693 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
216694 if( pBBox==0 ) return;
216695 p = geopolyBBox(context, 0, pBBox->a, 0);
216696 if( p ){
216697 sqlite3_result_blob(context, p->hdr,
216698 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216699 sqlite3_free(p);
216700 }
216701}
216702
216703
216704/*
216705** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
216706** Returns:
216707**
216708** +2 x0,y0 is on the line segement
216709**
216710** +1 x0,y0 is beneath line segment
216711**
216712** 0 x0,y0 is not on or beneath the line segment or the line segment
216713** is vertical and x0,y0 is not on the line segment
216714**
216715** The left-most coordinate min(x1,x2) is not considered to be part of
216716** the line segment for the purposes of this analysis.
216717*/
216718static int pointBeneathLine(
216719 double x0, double y0,
216720 double x1, double y1,
216721 double x2, double y2
216722){
216723 double y;
216724 if( x0==x1 && y0==y1 ) return 2;
216725 if( x1<x2 ){
216726 if( x0<=x1 || x0>x2 ) return 0;
216727 }else if( x1>x2 ){
216728 if( x0<=x2 || x0>x1 ) return 0;
216729 }else{
216730 /* Vertical line segment */
216731 if( x0!=x1 ) return 0;
216732 if( y0<y1 && y0<y2 ) return 0;
216733 if( y0>y1 && y0>y2 ) return 0;
216734 return 2;
216735 }
216736 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
216737 if( y0==y ) return 2;
216738 if( y0<y ) return 1;
216739 return 0;
216740}
216741
216742/*
216743** SQL function: geopoly_contains_point(P,X,Y)
216744**
216745** Return +2 if point X,Y is within polygon P.
216746** Return +1 if point X,Y is on the polygon boundary.
216747** Return 0 if point X,Y is outside the polygon
216748*/
216749static void geopolyContainsPointFunc(
216750 sqlite3_context *context,
216751 int argc,
216752 sqlite3_value **argv
216753){
216754 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
216755 double x0 = sqlite3_value_double(argv[1]);
216756 double y0 = sqlite3_value_double(argv[2]);
216757 int v = 0;
216758 int cnt = 0;
216759 int ii;
216760 (void)argc;
216761
216762 if( p1==0 ) return;
216763 for(ii=0; ii<p1->nVertex-1; ii++){
216764 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
216765 GeoX(p1,ii+1),GeoY(p1,ii+1));
216766 if( v==2 ) break;
216767 cnt += v;
216768 }
216769 if( v!=2 ){
216770 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
216771 GeoX(p1,0), GeoY(p1,0));
216772 }
216773 if( v==2 ){
216774 sqlite3_result_int(context, 1);
216775 }else if( ((v+cnt)&1)==0 ){
216776 sqlite3_result_int(context, 0);
216777 }else{
216778 sqlite3_result_int(context, 2);
216779 }
216780 sqlite3_free(p1);
216781}
216782
216783/* Forward declaration */
216784static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
216785
216786/*
216787** SQL function: geopoly_within(P1,P2)
216788**
216789** Return +2 if P1 and P2 are the same polygon
216790** Return +1 if P2 is contained within P1
216791** Return 0 if any part of P2 is on the outside of P1
216792**
216793*/
216794static void geopolyWithinFunc(
216795 sqlite3_context *context,
216796 int argc,
216797 sqlite3_value **argv
216798){
216799 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
216800 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
216801 (void)argc;
216802 if( p1 && p2 ){
216803 int x = geopolyOverlap(p1, p2);
216804 if( x<0 ){
216805 sqlite3_result_error_nomem(context);
216806 }else{
216807 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
216808 }
216809 }
216810 sqlite3_free(p1);
216811 sqlite3_free(p2);
216812}
216813
216814/* Objects used by the overlap algorihm. */
216815typedef struct GeoEvent GeoEvent;
216816typedef struct GeoSegment GeoSegment;
216817typedef struct GeoOverlap GeoOverlap;
216818struct GeoEvent {
216819 double x; /* X coordinate at which event occurs */
216820 int eType; /* 0 for ADD, 1 for REMOVE */
216821 GeoSegment *pSeg; /* The segment to be added or removed */
216822 GeoEvent *pNext; /* Next event in the sorted list */
216823};
216824struct GeoSegment {
216825 double C, B; /* y = C*x + B */
216826 double y; /* Current y value */
216827 float y0; /* Initial y value */
216828 unsigned char side; /* 1 for p1, 2 for p2 */
216829 unsigned int idx; /* Which segment within the side */
216830 GeoSegment *pNext; /* Next segment in a list sorted by y */
216831};
216832struct GeoOverlap {
216833 GeoEvent *aEvent; /* Array of all events */
216834 GeoSegment *aSegment; /* Array of all segments */
216835 int nEvent; /* Number of events */
216836 int nSegment; /* Number of segments */
216837};
216838
216839/*
216840** Add a single segment and its associated events.
216841*/
216842static void geopolyAddOneSegment(
216843 GeoOverlap *p,
216844 GeoCoord x0,
216845 GeoCoord y0,
216846 GeoCoord x1,
216847 GeoCoord y1,
216848 unsigned char side,
216849 unsigned int idx
216850){
216851 GeoSegment *pSeg;
216852 GeoEvent *pEvent;
216853 if( x0==x1 ) return; /* Ignore vertical segments */
216854 if( x0>x1 ){
216855 GeoCoord t = x0;
216856 x0 = x1;
216857 x1 = t;
216858 t = y0;
216859 y0 = y1;
216860 y1 = t;
216861 }
216862 pSeg = p->aSegment + p->nSegment;
216863 p->nSegment++;
216864 pSeg->C = (y1-y0)/(x1-x0);
216865 pSeg->B = y1 - x1*pSeg->C;
216866 pSeg->y0 = y0;
216867 pSeg->side = side;
216868 pSeg->idx = idx;
216869 pEvent = p->aEvent + p->nEvent;
216870 p->nEvent++;
216871 pEvent->x = x0;
216872 pEvent->eType = 0;
216873 pEvent->pSeg = pSeg;
216874 pEvent = p->aEvent + p->nEvent;
216875 p->nEvent++;
216876 pEvent->x = x1;
216877 pEvent->eType = 1;
216878 pEvent->pSeg = pSeg;
216879}
216880
216881
216882
216883/*
216884** Insert all segments and events for polygon pPoly.
216885*/
216886static void geopolyAddSegments(
216887 GeoOverlap *p, /* Add segments to this Overlap object */
216888 GeoPoly *pPoly, /* Take all segments from this polygon */
216889 unsigned char side /* The side of pPoly */
216890){
216891 unsigned int i;
216892 GeoCoord *x;
216893 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
216894 x = &GeoX(pPoly,i);
216895 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
216896 }
216897 x = &GeoX(pPoly,i);
216898 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
216899}
216900
216901/*
216902** Merge two lists of sorted events by X coordinate
216903*/
216904static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
216905 GeoEvent head, *pLast;
216906 head.pNext = 0;
216907 pLast = &head;
216908 while( pRight && pLeft ){
216909 if( pRight->x <= pLeft->x ){
216910 pLast->pNext = pRight;
216911 pLast = pRight;
216912 pRight = pRight->pNext;
216913 }else{
216914 pLast->pNext = pLeft;
216915 pLast = pLeft;
216916 pLeft = pLeft->pNext;
216917 }
216918 }
216919 pLast->pNext = pRight ? pRight : pLeft;
216920 return head.pNext;
216921}
216922
216923/*
216924** Sort an array of nEvent event objects into a list.
216925*/
216926static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
216927 int mx = 0;
216928 int i, j;
216929 GeoEvent *p;
216930 GeoEvent *a[50];
216931 for(i=0; i<nEvent; i++){
216932 p = &aEvent[i];
216933 p->pNext = 0;
216934 for(j=0; j<mx && a[j]; j++){
216935 p = geopolyEventMerge(a[j], p);
216936 a[j] = 0;
216937 }
216938 a[j] = p;
216939 if( j>=mx ) mx = j+1;
216940 }
216941 p = 0;
216942 for(i=0; i<mx; i++){
216943 p = geopolyEventMerge(a[i], p);
216944 }
216945 return p;
216946}
216947
216948/*
216949** Merge two lists of sorted segments by Y, and then by C.
216950*/
216951static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
216952 GeoSegment head, *pLast;
216953 head.pNext = 0;
216954 pLast = &head;
216955 while( pRight && pLeft ){
216956 double r = pRight->y - pLeft->y;
216957 if( r==0.0 ) r = pRight->C - pLeft->C;
216958 if( r<0.0 ){
216959 pLast->pNext = pRight;
216960 pLast = pRight;
216961 pRight = pRight->pNext;
216962 }else{
216963 pLast->pNext = pLeft;
216964 pLast = pLeft;
216965 pLeft = pLeft->pNext;
216966 }
216967 }
216968 pLast->pNext = pRight ? pRight : pLeft;
216969 return head.pNext;
216970}
216971
216972/*
216973** Sort a list of GeoSegments in order of increasing Y and in the event of
216974** a tie, increasing C (slope).
216975*/
216976static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
216977 int mx = 0;
216978 int i;
216979 GeoSegment *p;
216980 GeoSegment *a[50];
216981 while( pList ){
216982 p = pList;
216983 pList = pList->pNext;
216984 p->pNext = 0;
216985 for(i=0; i<mx && a[i]; i++){
216986 p = geopolySegmentMerge(a[i], p);
216987 a[i] = 0;
216988 }
216989 a[i] = p;
216990 if( i>=mx ) mx = i+1;
216991 }
216992 p = 0;
216993 for(i=0; i<mx; i++){
216994 p = geopolySegmentMerge(a[i], p);
216995 }
216996 return p;
216997}
216998
216999/*
217000** Determine the overlap between two polygons
217001*/
217002static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
217003 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
217004 GeoOverlap *p;
217005 sqlite3_int64 nByte;
217006 GeoEvent *pThisEvent;
217007 double rX;
217008 int rc = 0;
217009 int needSort = 0;
217010 GeoSegment *pActive = 0;
217011 GeoSegment *pSeg;
217012 unsigned char aOverlap[4];
217013
217014 nByte = sizeof(GeoEvent)*nVertex*2
217015 + sizeof(GeoSegment)*nVertex
217016 + sizeof(GeoOverlap);
217017 p = sqlite3_malloc64( nByte );
217018 if( p==0 ) return -1;
217019 p->aEvent = (GeoEvent*)&p[1];
217020 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
217021 p->nEvent = p->nSegment = 0;
217022 geopolyAddSegments(p, p1, 1);
217023 geopolyAddSegments(p, p2, 2);
217024 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
217025 rX = pThisEvent && pThisEvent->x==0.0 ? -1.0 : 0.0;
217026 memset(aOverlap, 0, sizeof(aOverlap));
217027 while( pThisEvent ){
217028 if( pThisEvent->x!=rX ){
217029 GeoSegment *pPrev = 0;
217030 int iMask = 0;
217031 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
217032 rX = pThisEvent->x;
217033 if( needSort ){
217034 GEODEBUG(("SORT\n"));
217035 pActive = geopolySortSegmentsByYAndC(pActive);
217036 needSort = 0;
217037 }
217038 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
217039 if( pPrev ){
217040 if( pPrev->y!=pSeg->y ){
217041 GEODEBUG(("MASK: %d\n", iMask));
217042 aOverlap[iMask] = 1;
217043 }
217044 }
217045 iMask ^= pSeg->side;
217046 pPrev = pSeg;
217047 }
217048 pPrev = 0;
217049 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
217050 double y = pSeg->C*rX + pSeg->B;
217051 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
217052 pSeg->y = y;
217053 if( pPrev ){
217054 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
217055 rc = 1;
217056 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
217057 pPrev->side, pPrev->idx,
217058 pSeg->side, pSeg->idx));
217059 goto geopolyOverlapDone;
217060 }else if( pPrev->y!=pSeg->y ){
217061 GEODEBUG(("MASK: %d\n", iMask));
217062 aOverlap[iMask] = 1;
217063 }
217064 }
217065 iMask ^= pSeg->side;
217066 pPrev = pSeg;
217067 }
217068 }
217069 GEODEBUG(("%s %d.%d C=%g B=%g\n",
217070 pThisEvent->eType ? "RM " : "ADD",
217071 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
217072 pThisEvent->pSeg->C,
217073 pThisEvent->pSeg->B));
217074 if( pThisEvent->eType==0 ){
217075 /* Add a segment */
217076 pSeg = pThisEvent->pSeg;
217077 pSeg->y = pSeg->y0;
217078 pSeg->pNext = pActive;
217079 pActive = pSeg;
217080 needSort = 1;
217081 }else{
217082 /* Remove a segment */
217083 if( pActive==pThisEvent->pSeg ){
217084 pActive = ALWAYS(pActive)(pActive) ? pActive->pNext : 0;
217085 }else{
217086 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
217087 if( pSeg->pNext==pThisEvent->pSeg ){
217088 pSeg->pNext = ALWAYS(pSeg->pNext)(pSeg->pNext) ? pSeg->pNext->pNext : 0;
217089 break;
217090 }
217091 }
217092 }
217093 }
217094 pThisEvent = pThisEvent->pNext;
217095 }
217096 if( aOverlap[3]==0 ){
217097 rc = 0;
217098 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
217099 rc = 3;
217100 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
217101 rc = 2;
217102 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
217103 rc = 4;
217104 }else{
217105 rc = 1;
217106 }
217107
217108geopolyOverlapDone:
217109 sqlite3_free(p);
217110 return rc;
217111}
217112
217113/*
217114** SQL function: geopoly_overlap(P1,P2)
217115**
217116** Determine whether or not P1 and P2 overlap. Return value:
217117**
217118** 0 The two polygons are disjoint
217119** 1 They overlap
217120** 2 P1 is completely contained within P2
217121** 3 P2 is completely contained within P1
217122** 4 P1 and P2 are the same polygon
217123** NULL Either P1 or P2 or both are not valid polygons
217124*/
217125static void geopolyOverlapFunc(
217126 sqlite3_context *context,
217127 int argc,
217128 sqlite3_value **argv
217129){
217130 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
217131 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
217132 (void)argc;
217133 if( p1 && p2 ){
217134 int x = geopolyOverlap(p1, p2);
217135 if( x<0 ){
217136 sqlite3_result_error_nomem(context);
217137 }else{
217138 sqlite3_result_int(context, x);
217139 }
217140 }
217141 sqlite3_free(p1);
217142 sqlite3_free(p2);
217143}
217144
217145/*
217146** Enable or disable debugging output
217147*/
217148static void geopolyDebugFunc(
217149 sqlite3_context *context,
217150 int argc,
217151 sqlite3_value **argv
217152){
217153 (void)context;
217154 (void)argc;
217155#ifdef GEOPOLY_ENABLE_DEBUG
217156 geo_debug = sqlite3_value_int(argv[0]);
217157#else
217158 (void)argv;
217159#endif
217160}
217161
217162/*
217163** This function is the implementation of both the xConnect and xCreate
217164** methods of the geopoly virtual table.
217165**
217166** argv[0] -> module name
217167** argv[1] -> database name
217168** argv[2] -> table name
217169** argv[...] -> column names...
217170*/
217171static int geopolyInit(
217172 sqlite3 *db, /* Database connection */
217173 void *pAux, /* One of the RTREE_COORD_* constants */
217174 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
217175 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
217176 char **pzErr, /* OUT: Error message, if any */
217177 int isCreate /* True for xCreate, false for xConnect */
217178){
217179 int rc = SQLITE_OK0;
217180 Rtree *pRtree;
217181 sqlite3_int64 nDb; /* Length of string argv[1] */
217182 sqlite3_int64 nName; /* Length of string argv[2] */
217183 sqlite3_str *pSql;
217184 char *zSql;
217185 int ii;
217186 (void)pAux;
217187
217188 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
217189 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
217190
217191 /* Allocate the sqlite3_vtab structure */
217192 nDb = strlen(argv[1]);
217193 nName = strlen(argv[2]);
217194 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
217195 if( !pRtree ){
217196 return SQLITE_NOMEM7;
217197 }
217198 memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
217199 pRtree->nBusy = 1;
217200 pRtree->base.pModule = &rtreeModule;
217201 pRtree->zDb = (char *)&pRtree[1];
217202 pRtree->zName = &pRtree->zDb[nDb+1];
217203 pRtree->zNodeName = &pRtree->zName[nName+1];
217204 pRtree->eCoordType = RTREE_COORD_REAL320;
217205 pRtree->nDim = 2;
217206 pRtree->nDim2 = 4;
217207 memcpy(pRtree->zDb, argv[1], nDb);
217208 memcpy(pRtree->zName, argv[2], nName);
217209 memcpy(pRtree->zNodeName, argv[2], nName);
217210 memcpy(&pRtree->zNodeName[nName], "_node", 6);
217211
217212
217213 /* Create/Connect to the underlying relational database schema. If
217214 ** that is successful, call sqlite3_declare_vtab() to configure
217215 ** the r-tree table schema.
217216 */
217217 pSql = sqlite3_str_new(db);
217218 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
217219 pRtree->nAux = 1; /* Add one for _shape */
217220 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
217221 for(ii=3; ii<argc; ii++){
217222 pRtree->nAux++;
217223 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
217224 }
217225 sqlite3_str_appendf(pSql, ");");
217226 zSql = sqlite3_str_finish(pSql);
217227 if( !zSql ){
217228 rc = SQLITE_NOMEM7;
217229 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
217230 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
217231 }
217232 sqlite3_free(zSql);
217233 if( rc ) goto geopolyInit_fail;
217234 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
217235
217236 /* Figure out the node size to use. */
217237 rc = getNodeSize(db, pRtree, isCreate, pzErr);
217238 if( rc ) goto geopolyInit_fail;
217239 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
217240 if( rc ){
217241 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
217242 goto geopolyInit_fail;
217243 }
217244
217245 *ppVtab = (sqlite3_vtab *)pRtree;
217246 return SQLITE_OK0;
217247
217248geopolyInit_fail:
217249 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
217250 assert( *ppVtab==0 )((void) (0));
217251 assert( pRtree->nBusy==1 )((void) (0));
217252 rtreeRelease(pRtree);
217253 return rc;
217254}
217255
217256
217257/*
217258** GEOPOLY virtual table module xCreate method.
217259*/
217260static int geopolyCreate(
217261 sqlite3 *db,
217262 void *pAux,
217263 int argc, const char *const*argv,
217264 sqlite3_vtab **ppVtab,
217265 char **pzErr
217266){
217267 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
217268}
217269
217270/*
217271** GEOPOLY virtual table module xConnect method.
217272*/
217273static int geopolyConnect(
217274 sqlite3 *db,
217275 void *pAux,
217276 int argc, const char *const*argv,
217277 sqlite3_vtab **ppVtab,
217278 char **pzErr
217279){
217280 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
217281}
217282
217283
217284/*
217285** GEOPOLY virtual table module xFilter method.
217286**
217287** Query plans:
217288**
217289** 1 rowid lookup
217290** 2 search for objects overlapping the same bounding box
217291** that contains polygon argv[0]
217292** 3 search for objects overlapping the same bounding box
217293** that contains polygon argv[0]
217294** 4 full table scan
217295*/
217296static int geopolyFilter(
217297 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
217298 int idxNum, /* Query plan */
217299 const char *idxStr, /* Not Used */
217300 int argc, sqlite3_value **argv /* Parameters to the query plan */
217301){
217302 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
217303 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
217304 RtreeNode *pRoot = 0;
217305 int rc = SQLITE_OK0;
217306 int iCell = 0;
217307 (void)idxStr;
217308
217309 rtreeReference(pRtree);
217310
217311 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
217312 resetCursor(pCsr);
217313
217314 pCsr->iStrategy = idxNum;
217315 if( idxNum==1 ){
217316 /* Special case - lookup by rowid. */
217317 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
217318 RtreeSearchPoint *p; /* Search point for the leaf */
217319 i64 iRowid = sqlite3_value_int64(argv[0]);
217320 i64 iNode = 0;
217321 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
217322 if( rc==SQLITE_OK0 && pLeaf!=0 ){
217323 p = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, 0);
217324 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
217325 pCsr->aNode[0] = pLeaf;
217326 p->id = iNode;
217327 p->eWithin = PARTLY_WITHIN1;
217328 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
217329 p->iCell = (u8)iCell;
217330 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
217331 }else{
217332 pCsr->atEOF = 1;
217333 }
217334 }else{
217335 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
217336 ** with the configured constraints.
217337 */
217338 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
217339 if( rc==SQLITE_OK0 && idxNum<=3 ){
217340 RtreeCoord bbox[4];
217341 RtreeConstraint *p;
217342 assert( argc==1 )((void) (0));
217343 assert( argv[0]!=0 )((void) (0));
217344 geopolyBBox(0, argv[0], bbox, &rc);
217345 if( rc ){
217346 goto geopoly_filter_end;
217347 }
217348 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
217349 pCsr->nConstraint = 4;
217350 if( p==0 ){
217351 rc = SQLITE_NOMEM7;
217352 }else{
217353 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
217354 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
217355 if( idxNum==2 ){
217356 /* Overlap query */
217357 p->op = 'B';
217358 p->iCoord = 0;
217359 p->u.rValue = bbox[1].f;
217360 p++;
217361 p->op = 'D';
217362 p->iCoord = 1;
217363 p->u.rValue = bbox[0].f;
217364 p++;
217365 p->op = 'B';
217366 p->iCoord = 2;
217367 p->u.rValue = bbox[3].f;
217368 p++;
217369 p->op = 'D';
217370 p->iCoord = 3;
217371 p->u.rValue = bbox[2].f;
217372 }else{
217373 /* Within query */
217374 p->op = 'D';
217375 p->iCoord = 0;
217376 p->u.rValue = bbox[0].f;
217377 p++;
217378 p->op = 'B';
217379 p->iCoord = 1;
217380 p->u.rValue = bbox[1].f;
217381 p++;
217382 p->op = 'D';
217383 p->iCoord = 2;
217384 p->u.rValue = bbox[2].f;
217385 p++;
217386 p->op = 'B';
217387 p->iCoord = 3;
217388 p->u.rValue = bbox[3].f;
217389 }
217390 }
217391 }
217392 if( rc==SQLITE_OK0 ){
217393 RtreeSearchPoint *pNew;
217394 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO0.0, (u8)(pRtree->iDepth+1));
217395 if( pNew==0 ){
217396 rc = SQLITE_NOMEM7;
217397 goto geopoly_filter_end;
217398 }
217399 pNew->id = 1;
217400 pNew->iCell = 0;
217401 pNew->eWithin = PARTLY_WITHIN1;
217402 assert( pCsr->bPoint==1 )((void) (0));
217403 pCsr->aNode[0] = pRoot;
217404 pRoot = 0;
217405 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
217406 rc = rtreeStepToLeaf(pCsr);
217407 }
217408 }
217409
217410geopoly_filter_end:
217411 nodeRelease(pRtree, pRoot);
217412 rtreeRelease(pRtree);
217413 return rc;
217414}
217415
217416/*
217417** Rtree virtual table module xBestIndex method. There are three
217418** table scan strategies to choose from (in order from most to
217419** least desirable):
217420**
217421** idxNum idxStr Strategy
217422** ------------------------------------------------
217423** 1 "rowid" Direct lookup by rowid.
217424** 2 "rtree" R-tree overlap query using geopoly_overlap()
217425** 3 "rtree" R-tree within query using geopoly_within()
217426** 4 "fullscan" full-table scan.
217427** ------------------------------------------------
217428*/
217429static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
217430 int ii;
217431 int iRowidTerm = -1;
217432 int iFuncTerm = -1;
217433 int idxNum = 0;
217434 (void)tab;
217435
217436 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
217437 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
217438 if( !p->usable ) continue;
217439 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
217440 iRowidTerm = ii;
217441 break;
217442 }
217443 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
217444 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
217445 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
217446 ** See geopolyFindFunction() */
217447 iFuncTerm = ii;
217448 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION150 + 2;
217449 }
217450 }
217451
217452 if( iRowidTerm>=0 ){
217453 pIdxInfo->idxNum = 1;
217454 pIdxInfo->idxStr = "rowid";
217455 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
217456 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
217457 pIdxInfo->estimatedCost = 30.0;
217458 pIdxInfo->estimatedRows = 1;
217459 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE0x00000001;
217460 return SQLITE_OK0;
217461 }
217462 if( iFuncTerm>=0 ){
217463 pIdxInfo->idxNum = idxNum;
217464 pIdxInfo->idxStr = "rtree";
217465 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
217466 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
217467 pIdxInfo->estimatedCost = 300.0;
217468 pIdxInfo->estimatedRows = 10;
217469 return SQLITE_OK0;
217470 }
217471 pIdxInfo->idxNum = 4;
217472 pIdxInfo->idxStr = "fullscan";
217473 pIdxInfo->estimatedCost = 3000000.0;
217474 pIdxInfo->estimatedRows = 100000;
217475 return SQLITE_OK0;
217476}
217477
217478
217479/*
217480** GEOPOLY virtual table module xColumn method.
217481*/
217482static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
217483 Rtree *pRtree = (Rtree *)cur->pVtab;
217484 RtreeCursor *pCsr = (RtreeCursor *)cur;
217485 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
217486 int rc = SQLITE_OK0;
217487 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
217488
217489 if( rc ) return rc;
217490 if( p==0 ) return SQLITE_OK0;
217491 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK0;
217492 if( i<=pRtree->nAux ){
217493 if( !pCsr->bAuxValid ){
217494 if( pCsr->pReadAux==0 ){
217495 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
217496 &pCsr->pReadAux, 0);
217497 if( rc ) return rc;
217498 }
217499 sqlite3_bind_int64(pCsr->pReadAux, 1,
217500 nodeGetRowid(pRtree, pNode, p->iCell));
217501 rc = sqlite3_step(pCsr->pReadAux);
217502 if( rc==SQLITE_ROW100 ){
217503 pCsr->bAuxValid = 1;
217504 }else{
217505 sqlite3_reset(pCsr->pReadAux);
217506 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
217507 return rc;
217508 }
217509 }
217510 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
217511 }
217512 return SQLITE_OK0;
217513}
217514
217515
217516/*
217517** The xUpdate method for GEOPOLY module virtual tables.
217518**
217519** For DELETE:
217520**
217521** argv[0] = the rowid to be deleted
217522**
217523** For INSERT:
217524**
217525** argv[0] = SQL NULL
217526** argv[1] = rowid to insert, or an SQL NULL to select automatically
217527** argv[2] = _shape column
217528** argv[3] = first application-defined column....
217529**
217530** For UPDATE:
217531**
217532** argv[0] = rowid to modify. Never NULL
217533** argv[1] = rowid after the change. Never NULL
217534** argv[2] = new value for _shape
217535** argv[3] = new value for first application-defined column....
217536*/
217537static int geopolyUpdate(
217538 sqlite3_vtab *pVtab,
217539 int nData,
217540 sqlite3_value **aData,
217541 sqlite_int64 *pRowid
217542){
217543 Rtree *pRtree = (Rtree *)pVtab;
217544 int rc = SQLITE_OK0;
217545 RtreeCell cell; /* New cell to insert if nData>1 */
217546 i64 oldRowid; /* The old rowid */
217547 int oldRowidValid; /* True if oldRowid is valid */
217548 i64 newRowid; /* The new rowid */
217549 int newRowidValid; /* True if newRowid is valid */
217550 int coordChange = 0; /* Change in coordinates */
217551
217552 if( pRtree->nNodeRef ){
217553 /* Unable to write to the btree while another cursor is reading from it,
217554 ** since the write might do a rebalance which would disrupt the read
217555 ** cursor. */
217556 return SQLITE_LOCKED_VTAB(6 | (2<<8));
217557 }
217558 rtreeReference(pRtree);
217559 assert(nData>=1)((void) (0));
217560
217561 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL5;;
217562 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
217563 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL5;
217564 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
217565 cell.iRowid = newRowid;
217566
217567 if( nData>1 /* not a DELETE */
217568 && (!oldRowidValid /* INSERT */
217569 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
217570 || oldRowid!=newRowid) /* Rowid change */
217571 ){
217572 assert( aData[2]!=0 )((void) (0));
217573 geopolyBBox(0, aData[2], cell.aCoord, &rc);
217574 if( rc ){
217575 if( rc==SQLITE_ERROR1 ){
217576 pVtab->zErrMsg =
217577 sqlite3_mprintf("_shape does not contain a valid polygon");
217578 }
217579 goto geopoly_update_end;
217580 }
217581 coordChange = 1;
217582
217583 /* If a rowid value was supplied, check if it is already present in
217584 ** the table. If so, the constraint has failed. */
217585 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
217586 int steprc;
217587 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
217588 steprc = sqlite3_step(pRtree->pReadRowid);
217589 rc = sqlite3_reset(pRtree->pReadRowid);
217590 if( SQLITE_ROW100==steprc ){
217591 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
217592 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
217593 }else{
217594 rc = rtreeConstraintError(pRtree, 0);
217595 }
217596 }
217597 }
217598 }
217599
217600 /* If aData[0] is not an SQL NULL value, it is the rowid of a
217601 ** record to delete from the r-tree table. The following block does
217602 ** just that.
217603 */
217604 if( rc==SQLITE_OK0 && (nData==1 || (coordChange && oldRowidValid)) ){
217605 rc = rtreeDeleteRowid(pRtree, oldRowid);
217606 }
217607
217608 /* If the aData[] array contains more than one element, elements
217609 ** (aData[2]..aData[argc-1]) contain a new record to insert into
217610 ** the r-tree structure.
217611 */
217612 if( rc==SQLITE_OK0 && nData>1 && coordChange ){
217613 /* Insert the new record into the r-tree */
217614 RtreeNode *pLeaf = 0;
217615 if( !newRowidValid ){
217616 rc = rtreeNewRowid(pRtree, &cell.iRowid);
217617 }
217618 *pRowid = cell.iRowid;
217619 if( rc==SQLITE_OK0 ){
217620 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
217621 }
217622 if( rc==SQLITE_OK0 ){
217623 int rc2;
217624 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
217625 rc2 = nodeRelease(pRtree, pLeaf);
217626 if( rc==SQLITE_OK0 ){
217627 rc = rc2;
217628 }
217629 }
217630 }
217631
217632 /* Change the data */
217633 if( rc==SQLITE_OK0 && nData>1 ){
217634 sqlite3_stmt *pUp = pRtree->pWriteAux;
217635 int jj;
217636 int nChange = 0;
217637 sqlite3_bind_int64(pUp, 1, cell.iRowid);
217638 assert( pRtree->nAux>=1 )((void) (0));
217639 if( sqlite3_value_nochange(aData[2]) ){
217640 sqlite3_bind_null(pUp, 2);
217641 }else{
217642 GeoPoly *p = 0;
217643 if( sqlite3_value_type(aData[2])==SQLITE_TEXT3
217644 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
217645 && rc==SQLITE_OK0
217646 ){
217647 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
217648 }else{
217649 sqlite3_bind_value(pUp, 2, aData[2]);
217650 }
217651 sqlite3_free(p);
217652 nChange = 1;
217653 }
217654 for(jj=1; jj<nData-2; jj++){
217655 nChange++;
217656 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
217657 }
217658 if( nChange ){
217659 sqlite3_step(pUp);
217660 rc = sqlite3_reset(pUp);
217661 }
217662 }
217663
217664geopoly_update_end:
217665 rtreeRelease(pRtree);
217666 return rc;
217667}
217668
217669/*
217670** Report that geopoly_overlap() is an overloaded function suitable
217671** for use in xBestIndex.
217672*/
217673static int geopolyFindFunction(
217674 sqlite3_vtab *pVtab,
217675 int nArg,
217676 const char *zName,
217677 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
217678 void **ppArg
217679){
217680 (void)pVtab;
217681 (void)nArg;
217682 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
217683 *pxFunc = geopolyOverlapFunc;
217684 *ppArg = 0;
217685 return SQLITE_INDEX_CONSTRAINT_FUNCTION150;
217686 }
217687 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
217688 *pxFunc = geopolyWithinFunc;
217689 *ppArg = 0;
217690 return SQLITE_INDEX_CONSTRAINT_FUNCTION150+1;
217691 }
217692 return 0;
217693}
217694
217695
217696static sqlite3_module geopolyModule = {
217697 3, /* iVersion */
217698 geopolyCreate, /* xCreate - create a table */
217699 geopolyConnect, /* xConnect - connect to an existing table */
217700 geopolyBestIndex, /* xBestIndex - Determine search strategy */
217701 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
217702 rtreeDestroy, /* xDestroy - Drop a table */
217703 rtreeOpen, /* xOpen - open a cursor */
217704 rtreeClose, /* xClose - close a cursor */
217705 geopolyFilter, /* xFilter - configure scan constraints */
217706 rtreeNext, /* xNext - advance a cursor */
217707 rtreeEof, /* xEof */
217708 geopolyColumn, /* xColumn - read data */
217709 rtreeRowid, /* xRowid - read data */
217710 geopolyUpdate, /* xUpdate - write data */
217711 rtreeBeginTransaction, /* xBegin - begin transaction */
217712 rtreeEndTransaction, /* xSync - sync transaction */
217713 rtreeEndTransaction, /* xCommit - commit transaction */
217714 rtreeEndTransaction, /* xRollback - rollback transaction */
217715 geopolyFindFunction, /* xFindFunction - function overloading */
217716 rtreeRename, /* xRename - rename the table */
217717 rtreeSavepoint, /* xSavepoint */
217718 0, /* xRelease */
217719 0, /* xRollbackTo */
217720 rtreeShadowName, /* xShadowName */
217721 rtreeIntegrity /* xIntegrity */
217722};
217723
217724static int sqlite3_geopoly_init(sqlite3 *db){
217725 int rc = SQLITE_OK0;
217726 static const struct {
217727 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
217728 signed char nArg;
217729 unsigned char bPure;
217730 const char *zName;
217731 } aFunc[] = {
217732 { geopolyAreaFunc, 1, 1, "geopoly_area" },
217733 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
217734 { geopolyJsonFunc, 1, 1, "geopoly_json" },
217735 { geopolySvgFunc, -1, 1, "geopoly_svg" },
217736 { geopolyWithinFunc, 2, 1, "geopoly_within" },
217737 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
217738 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
217739 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
217740 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
217741 { geopolyXformFunc, 7, 1, "geopoly_xform" },
217742 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
217743 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
217744 };
217745 static const struct {
217746 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
217747 void (*xFinal)(sqlite3_context*);
217748 const char *zName;
217749 } aAgg[] = {
217750 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
217751 };
217752 unsigned int i;
217753 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK0; i++){
217754 int enc;
217755 if( aFunc[i].bPure ){
217756 enc = SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000;
217757 }else{
217758 enc = SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000;
217759 }
217760 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
217761 enc, 0,
217762 aFunc[i].xFunc, 0, 0);
217763 }
217764 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK0; i++){
217765 rc = sqlite3_create_function(db, aAgg[i].zName, 1,
217766 SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000, 0,
217767 0, aAgg[i].xStep, aAgg[i].xFinal);
217768 }
217769 if( rc==SQLITE_OK0 ){
217770 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
217771 }
217772 return rc;
217773}
217774
217775/************** End of geopoly.c *********************************************/
217776/************** Continuing where we left off in rtree.c **********************/
217777#endif
217778
217779/*
217780** Register the r-tree module with database handle db. This creates the
217781** virtual table module "rtree" and the debugging/analysis scalar
217782** function "rtreenode".
217783*/
217784SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db){
217785 const int utf8 = SQLITE_UTF81;
217786 int rc;
217787
217788 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
217789 if( rc==SQLITE_OK0 ){
217790 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
217791 }
217792 if( rc==SQLITE_OK0 ){
217793 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
217794 }
217795 if( rc==SQLITE_OK0 ){
217796#ifdef SQLITE_RTREE_INT_ONLY
217797 void *c = (void *)RTREE_COORD_INT321;
217798#else
217799 void *c = (void *)RTREE_COORD_REAL320;
217800#endif
217801 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
217802 }
217803 if( rc==SQLITE_OK0 ){
217804 void *c = (void *)RTREE_COORD_INT321;
217805 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
217806 }
217807#ifdef SQLITE_ENABLE_GEOPOLY
217808 if( rc==SQLITE_OK0 ){
217809 rc = sqlite3_geopoly_init(db);
217810 }
217811#endif
217812
217813 return rc;
217814}
217815
217816/*
217817** This routine deletes the RtreeGeomCallback object that was attached
217818** one of the SQL functions create by sqlite3_rtree_geometry_callback()
217819** or sqlite3_rtree_query_callback(). In other words, this routine is the
217820** destructor for an RtreeGeomCallback objecct. This routine is called when
217821** the corresponding SQL function is deleted.
217822*/
217823static void rtreeFreeCallback(void *p){
217824 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
217825 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
217826 sqlite3_free(p);
217827}
217828
217829/*
217830** This routine frees the BLOB that is returned by geomCallback().
217831*/
217832static void rtreeMatchArgFree(void *pArg){
217833 int i;
217834 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
217835 for(i=0; i<p->nParam; i++){
217836 sqlite3_value_free(p->apSqlParam[i]);
217837 }
217838 sqlite3_free(p);
217839}
217840
217841/*
217842** Each call to sqlite3_rtree_geometry_callback() or
217843** sqlite3_rtree_query_callback() creates an ordinary SQLite
217844** scalar function that is implemented by this routine.
217845**
217846** All this function does is construct an RtreeMatchArg object that
217847** contains the geometry-checking callback routines and a list of
217848** parameters to this function, then return that RtreeMatchArg object
217849** as a BLOB.
217850**
217851** The R-Tree MATCH operator will read the returned BLOB, deserialize
217852** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
217853** out which elements of the R-Tree should be returned by the query.
217854*/
217855static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
217856 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
217857 RtreeMatchArg *pBlob;
217858 sqlite3_int64 nBlob;
217859 int memErr = 0;
217860
217861 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
217862 + nArg*sizeof(sqlite3_value*);
217863 pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
217864 if( !pBlob ){
217865 sqlite3_result_error_nomem(ctx);
217866 }else{
217867 int i;
217868 pBlob->iSize = nBlob;
217869 pBlob->cb = pGeomCtx[0];
217870 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
217871 pBlob->nParam = nArg;
217872 for(i=0; i<nArg; i++){
217873 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
217874 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
217875#ifdef SQLITE_RTREE_INT_ONLY
217876 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
217877#else
217878 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
217879#endif
217880 }
217881 if( memErr ){
217882 sqlite3_result_error_nomem(ctx);
217883 rtreeMatchArgFree(pBlob);
217884 }else{
217885 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
217886 }
217887 }
217888}
217889
217890/*
217891** Register a new geometry function for use with the r-tree MATCH operator.
217892*/
217893SQLITE_API int sqlite3_rtree_geometry_callback(
217894 sqlite3 *db, /* Register SQL function on this connection */
217895 const char *zGeom, /* Name of the new SQL function */
217896 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
217897 void *pContext /* Extra data associated with the callback */
217898){
217899 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
217900
217901 /* Allocate and populate the context object. */
217902 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
217903 if( !pGeomCtx ) return SQLITE_NOMEM7;
217904 pGeomCtx->xGeom = xGeom;
217905 pGeomCtx->xQueryFunc = 0;
217906 pGeomCtx->xDestructor = 0;
217907 pGeomCtx->pContext = pContext;
217908 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY5,
217909 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
217910 );
217911}
217912
217913/*
217914** Register a new 2nd-generation geometry function for use with the
217915** r-tree MATCH operator.
217916*/
217917SQLITE_API int sqlite3_rtree_query_callback(
217918 sqlite3 *db, /* Register SQL function on this connection */
217919 const char *zQueryFunc, /* Name of new SQL function */
217920 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
217921 void *pContext, /* Extra data passed into the callback */
217922 void (*xDestructor)(void*) /* Destructor for the extra data */
217923){
217924 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
217925
217926 /* Allocate and populate the context object. */
217927 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
217928 if( !pGeomCtx ){
217929 if( xDestructor ) xDestructor(pContext);
217930 return SQLITE_NOMEM7;
217931 }
217932 pGeomCtx->xGeom = 0;
217933 pGeomCtx->xQueryFunc = xQueryFunc;
217934 pGeomCtx->xDestructor = xDestructor;
217935 pGeomCtx->pContext = pContext;
217936 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY5,
217937 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
217938 );
217939}
217940
217941#if !SQLITE_CORE1
217942#ifdef _WIN32
217943__declspec(dllexport)
217944#endif
217945SQLITE_API int sqlite3_rtree_init(
217946 sqlite3 *db,
217947 char **pzErrMsg,
217948 const sqlite3_api_routines *pApi
217949){
217950 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
217951 return sqlite3RtreeInit(db);
217952}
217953#endif
217954
217955#endif
217956
217957/************** End of rtree.c ***********************************************/
217958/************** Begin file icu.c *********************************************/
217959/*
217960** 2007 May 6
217961**
217962** The author disclaims copyright to this source code. In place of
217963** a legal notice, here is a blessing:
217964**
217965** May you do good and not evil.
217966** May you find forgiveness for yourself and forgive others.
217967** May you share freely, never taking more than you give.
217968**
217969*************************************************************************
217970** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
217971**
217972** This file implements an integration between the ICU library
217973** ("International Components for Unicode", an open-source library
217974** for handling unicode data) and SQLite. The integration uses
217975** ICU to provide the following to SQLite:
217976**
217977** * An implementation of the SQL regexp() function (and hence REGEXP
217978** operator) using the ICU uregex_XX() APIs.
217979**
217980** * Implementations of the SQL scalar upper() and lower() functions
217981** for case mapping.
217982**
217983** * Integration of ICU and SQLite collation sequences.
217984**
217985** * An implementation of the LIKE operator that uses ICU to
217986** provide case-independent matching.
217987*/
217988
217989#if !defined(SQLITE_CORE1) \
217990 || defined(SQLITE_ENABLE_ICU) \
217991 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
217992
217993/* Include ICU headers */
217994#include <unicode/utypes.h>
217995#include <unicode/uregex.h>
217996#include <unicode/ustring.h>
217997#include <unicode/ucol.h>
217998
217999/* #include <assert.h> */
218000
218001#ifndef SQLITE_CORE1
218002/* #include "sqlite3ext.h" */
218003 SQLITE_EXTENSION_INIT1
218004#else
218005/* #include "sqlite3.h" */
218006#endif
218007
218008/*
218009** This function is called when an ICU function called from within
218010** the implementation of an SQL scalar function returns an error.
218011**
218012** The scalar function context passed as the first argument is
218013** loaded with an error message based on the following two args.
218014*/
218015static void icuFunctionError(
218016 sqlite3_context *pCtx, /* SQLite scalar function context */
218017 const char *zName, /* Name of ICU function that failed */
218018 UErrorCode e /* Error code returned by ICU function */
218019){
218020 char zBuf[128];
218021 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
218022 zBuf[127] = '\0';
218023 sqlite3_result_error(pCtx, zBuf, -1);
218024}
218025
218026#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
218027
218028/*
218029** Maximum length (in bytes) of the pattern in a LIKE or GLOB
218030** operator.
218031*/
218032#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
218033# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
218034#endif
218035
218036/*
218037** Version of sqlite3_free() that is always a function, never a macro.
218038*/
218039static void xFree(void *p){
218040 sqlite3_free(p);
218041}
218042
218043/*
218044** This lookup table is used to help decode the first byte of
218045** a multi-byte UTF8 character. It is copied here from SQLite source
218046** code file utf8.c.
218047*/
218048static const unsigned char icuUtf8Trans1[] = {
218049 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
218050 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
218051 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
218052 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
218053 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
218054 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
218055 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
218056 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
218057};
218058
218059#define SQLITE_ICU_READ_UTF8(zIn, c) \
218060 c = *(zIn++); \
218061 if( c>=0xc0 ){ \
218062 c = icuUtf8Trans1[c-0xc0]; \
218063 while( (*zIn & 0xc0)==0x80 ){ \
218064 c = (c<<6) + (0x3f & *(zIn++)); \
218065 } \
218066 }
218067
218068#define SQLITE_ICU_SKIP_UTF8(zIn) \
218069 assert( *zIn )((void) (0)); \
218070 if( *(zIn++)>=0xc0 ){ \
218071 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
218072 }
218073
218074
218075/*
218076** Compare two UTF-8 strings for equality where the first string is
218077** a "LIKE" expression. Return true (1) if they are the same and
218078** false (0) if they are different.
218079*/
218080static int icuLikeCompare(
218081 const uint8_t *zPattern, /* LIKE pattern */
218082 const uint8_t *zString, /* The UTF-8 string to compare against */
218083 const UChar32 uEsc /* The escape character */
218084){
218085 static const uint32_t MATCH_ONE = (uint32_t)'_';
218086 static const uint32_t MATCH_ALL = (uint32_t)'%';
218087
218088 int prevEscape = 0; /* True if the previous character was uEsc */
218089
218090 while( 1 ){
218091
218092 /* Read (and consume) the next character from the input pattern. */
218093 uint32_t uPattern;
218094 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
218095 if( uPattern==0 ) break;
218096
218097 /* There are now 4 possibilities:
218098 **
218099 ** 1. uPattern is an unescaped match-all character "%",
218100 ** 2. uPattern is an unescaped match-one character "_",
218101 ** 3. uPattern is an unescaped escape character, or
218102 ** 4. uPattern is to be handled as an ordinary character
218103 */
218104 if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){
218105 /* Case 1. */
218106 uint8_t c;
218107
218108 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
218109 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
218110 ** test string.
218111 */
218112 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
218113 if( c==MATCH_ONE ){
218114 if( *zString==0 ) return 0;
218115 SQLITE_ICU_SKIP_UTF8(zString);
218116 }
218117 zPattern++;
218118 }
218119
218120 if( *zPattern==0 ) return 1;
218121
218122 while( *zString ){
218123 if( icuLikeCompare(zPattern, zString, uEsc) ){
218124 return 1;
218125 }
218126 SQLITE_ICU_SKIP_UTF8(zString);
218127 }
218128 return 0;
218129
218130 }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){
218131 /* Case 2. */
218132 if( *zString==0 ) return 0;
218133 SQLITE_ICU_SKIP_UTF8(zString);
218134
218135 }else if( uPattern==(uint32_t)uEsc && !prevEscape ){
218136 /* Case 3. */
218137 prevEscape = 1;
218138
218139 }else{
218140 /* Case 4. */
218141 uint32_t uString;
218142 SQLITE_ICU_READ_UTF8(zString, uString);
218143 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
218144 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
218145 if( uString!=uPattern ){
218146 return 0;
218147 }
218148 prevEscape = 0;
218149 }
218150 }
218151
218152 return *zString==0;
218153}
218154
218155/*
218156** Implementation of the like() SQL function. This function implements
218157** the build-in LIKE operator. The first argument to the function is the
218158** pattern and the second argument is the string. So, the SQL statements:
218159**
218160** A LIKE B
218161**
218162** is implemented as like(B, A). If there is an escape character E,
218163**
218164** A LIKE B ESCAPE E
218165**
218166** is mapped to like(B, A, E).
218167*/
218168static void icuLikeFunc(
218169 sqlite3_context *context,
218170 int argc,
218171 sqlite3_value **argv
218172){
218173 const unsigned char *zA = sqlite3_value_text(argv[0]);
218174 const unsigned char *zB = sqlite3_value_text(argv[1]);
218175 UChar32 uEsc = 0;
218176
218177 /* Limit the length of the LIKE or GLOB pattern to avoid problems
218178 ** of deep recursion and N*N behavior in patternCompare().
218179 */
218180 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH50000 ){
218181 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
218182 return;
218183 }
218184
218185
218186 if( argc==3 ){
218187 /* The escape character string must consist of a single UTF-8 character.
218188 ** Otherwise, return an error.
218189 */
218190 int nE= sqlite3_value_bytes(argv[2]);
218191 const unsigned char *zE = sqlite3_value_text(argv[2]);
218192 int i = 0;
218193 if( zE==0 ) return;
218194 U8_NEXT(zE, i, nE, uEsc);
218195 if( i!=nE){
218196 sqlite3_result_error(context,
218197 "ESCAPE expression must be a single character", -1);
218198 return;
218199 }
218200 }
218201
218202 if( zA && zB ){
218203 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
218204 }
218205}
218206
218207/*
218208** Function to delete compiled regexp objects. Registered as
218209** a destructor function with sqlite3_set_auxdata().
218210*/
218211static void icuRegexpDelete(void *p){
218212 URegularExpression *pExpr = (URegularExpression *)p;
218213 uregex_close(pExpr);
218214}
218215
218216/*
218217** Implementation of SQLite REGEXP operator. This scalar function takes
218218** two arguments. The first is a regular expression pattern to compile
218219** the second is a string to match against that pattern. If either
218220** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
218221** is 1 if the string matches the pattern, or 0 otherwise.
218222**
218223** SQLite maps the regexp() function to the regexp() operator such
218224** that the following two are equivalent:
218225**
218226** zString REGEXP zPattern
218227** regexp(zPattern, zString)
218228**
218229** Uses the following ICU regexp APIs:
218230**
218231** uregex_open()
218232** uregex_matches()
218233** uregex_close()
218234*/
218235static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
218236 UErrorCode status = U_ZERO_ERROR;
218237 URegularExpression *pExpr;
218238 UBool res;
218239 const UChar *zString = sqlite3_value_text16(apArg[1]);
218240
218241 (void)nArg; /* Unused parameter */
218242
218243 /* If the left hand side of the regexp operator is NULL,
218244 ** then the result is also NULL.
218245 */
218246 if( !zString ){
218247 return;
218248 }
218249
218250 pExpr = sqlite3_get_auxdata(p, 0);
218251 if( !pExpr ){
218252 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
218253 if( !zPattern ){
218254 return;
218255 }
218256 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
218257
218258 if( U_SUCCESS(status) ){
218259 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
218260 pExpr = sqlite3_get_auxdata(p, 0);
218261 }
218262 if( !pExpr ){
218263 icuFunctionError(p, "uregex_open", status);
218264 return;
218265 }
218266 }
218267
218268 /* Configure the text that the regular expression operates on. */
218269 uregex_setText(pExpr, zString, -1, &status);
218270 if( !U_SUCCESS(status) ){
218271 icuFunctionError(p, "uregex_setText", status);
218272 return;
218273 }
218274
218275 /* Attempt the match */
218276 res = uregex_matches(pExpr, 0, &status);
218277 if( !U_SUCCESS(status) ){
218278 icuFunctionError(p, "uregex_matches", status);
218279 return;
218280 }
218281
218282 /* Set the text that the regular expression operates on to a NULL
218283 ** pointer. This is not really necessary, but it is tidier than
218284 ** leaving the regular expression object configured with an invalid
218285 ** pointer after this function returns.
218286 */
218287 uregex_setText(pExpr, 0, 0, &status);
218288
218289 /* Return 1 or 0. */
218290 sqlite3_result_int(p, res ? 1 : 0);
218291}
218292
218293/*
218294** Implementations of scalar functions for case mapping - upper() and
218295** lower(). Function upper() converts its input to upper-case (ABC).
218296** Function lower() converts to lower-case (abc).
218297**
218298** ICU provides two types of case mapping, "general" case mapping and
218299** "language specific". Refer to ICU documentation for the differences
218300** between the two.
218301**
218302** To utilise "general" case mapping, the upper() or lower() scalar
218303** functions are invoked with one argument:
218304**
218305** upper('ABC') -> 'abc'
218306** lower('abc') -> 'ABC'
218307**
218308** To access ICU "language specific" case mapping, upper() or lower()
218309** should be invoked with two arguments. The second argument is the name
218310** of the locale to use. Passing an empty string ("") or SQL NULL value
218311** as the second argument is the same as invoking the 1 argument version
218312** of upper() or lower().
218313**
218314** lower('I', 'en_us') -> 'i'
218315** lower('I', 'tr_tr') -> '\u131' (small dotless i)
218316**
218317** http://www.icu-project.org/userguide/posix.html#case_mappings
218318*/
218319static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
218320 const UChar *zInput; /* Pointer to input string */
218321 UChar *zOutput = 0; /* Pointer to output buffer */
218322 int nInput; /* Size of utf-16 input string in bytes */
218323 int nOut; /* Size of output buffer in bytes */
218324 int cnt;
218325 int bToUpper; /* True for toupper(), false for tolower() */
218326 UErrorCode status;
218327 const char *zLocale = 0;
218328
218329 assert(nArg==1 || nArg==2)((void) (0));
218330 bToUpper = (sqlite3_user_data(p)!=0);
218331 if( nArg==2 ){
218332 zLocale = (const char *)sqlite3_value_text(apArg[1]);
218333 }
218334
218335 zInput = sqlite3_value_text16(apArg[0]);
218336 if( !zInput ){
218337 return;
218338 }
218339 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
218340 if( nOut==0 ){
218341 sqlite3_result_text16(p, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
218342 return;
218343 }
218344
218345 for(cnt=0; cnt<2; cnt++){
218346 UChar *zNew = sqlite3_realloc(zOutput, nOut);
218347 if( zNew==0 ){
218348 sqlite3_free(zOutput);
218349 sqlite3_result_error_nomem(p);
218350 return;
218351 }
218352 zOutput = zNew;
218353 status = U_ZERO_ERROR;
218354 if( bToUpper ){
218355 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
218356 }else{
218357 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
218358 }
218359
218360 if( U_SUCCESS(status) ){
218361 sqlite3_result_text16(p, zOutput, nOut, xFree);
218362 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
218363 assert( cnt==0 )((void) (0));
218364 continue;
218365 }else{
218366 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
218367 }
218368 return;
218369 }
218370 assert( 0 )((void) (0)); /* Unreachable */
218371}
218372
218373#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
218374
218375/*
218376** Collation sequence destructor function. The pCtx argument points to
218377** a UCollator structure previously allocated using ucol_open().
218378*/
218379static void icuCollationDel(void *pCtx){
218380 UCollator *p = (UCollator *)pCtx;
218381 ucol_close(p);
218382}
218383
218384/*
218385** Collation sequence comparison function. The pCtx argument points to
218386** a UCollator structure previously allocated using ucol_open().
218387*/
218388static int icuCollationColl(
218389 void *pCtx,
218390 int nLeft,
218391 const void *zLeft,
218392 int nRight,
218393 const void *zRight
218394){
218395 UCollationResult res;
218396 UCollator *p = (UCollator *)pCtx;
218397 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
218398 switch( res ){
218399 case UCOL_LESS: return -1;
218400 case UCOL_GREATER: return +1;
218401 case UCOL_EQUAL: return 0;
218402 }
218403 assert(!"Unexpected return value from ucol_strcoll()")((void) (0));
218404 return 0;
218405}
218406
218407/*
218408** Implementation of the scalar function icu_load_collation().
218409**
218410** This scalar function is used to add ICU collation based collation
218411** types to an SQLite database connection. It is intended to be called
218412** as follows:
218413**
218414** SELECT icu_load_collation(<locale>, <collation-name>);
218415**
218416** Where <locale> is a string containing an ICU locale identifier (i.e.
218417** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
218418** collation sequence to create.
218419*/
218420static void icuLoadCollation(
218421 sqlite3_context *p,
218422 int nArg,
218423 sqlite3_value **apArg
218424){
218425 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
218426 UErrorCode status = U_ZERO_ERROR;
218427 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
218428 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
218429 UCollator *pUCollator; /* ICU library collation object */
218430 int rc; /* Return code from sqlite3_create_collation_x() */
218431
218432 assert(nArg==2 || nArg==3)((void) (0));
218433 (void)nArg; /* Unused parameter */
218434 zLocale = (const char *)sqlite3_value_text(apArg[0]);
218435 zName = (const char *)sqlite3_value_text(apArg[1]);
218436
218437 if( !zLocale || !zName ){
218438 return;
218439 }
218440
218441 pUCollator = ucol_open(zLocale, &status);
218442 if( !U_SUCCESS(status) ){
218443 icuFunctionError(p, "ucol_open", status);
218444 return;
218445 }
218446 assert(p)((void) (0));
218447 if(nArg==3){
218448 const char *zOption = (const char*)sqlite3_value_text(apArg[2]);
218449 static const struct {
218450 const char *zName;
218451 UColAttributeValue val;
218452 } aStrength[] = {
218453 { "PRIMARY", UCOL_PRIMARY },
218454 { "SECONDARY", UCOL_SECONDARY },
218455 { "TERTIARY", UCOL_TERTIARY },
218456 { "DEFAULT", UCOL_DEFAULT_STRENGTH },
218457 { "QUARTERNARY", UCOL_QUATERNARY },
218458 { "IDENTICAL", UCOL_IDENTICAL },
218459 };
218460 unsigned int i;
218461 for(i=0; i<sizeof(aStrength)/sizeof(aStrength[0]); i++){
218462 if( sqlite3_stricmp(zOption,aStrength[i].zName)==0 ){
218463 ucol_setStrength(pUCollator, aStrength[i].val);
218464 break;
218465 }
218466 }
218467 if( i>=sizeof(aStrength)/sizeof(aStrength[0]) ){
218468 sqlite3_str *pStr = sqlite3_str_new(sqlite3_context_db_handle(p));
218469 sqlite3_str_appendf(pStr,
218470 "unknown collation strength \"%s\" - should be one of:",
218471 zOption);
218472 for(i=0; i<sizeof(aStrength)/sizeof(aStrength[0]); i++){
218473 sqlite3_str_appendf(pStr, " %s", aStrength[i].zName);
218474 }
218475 sqlite3_result_error(p, sqlite3_str_value(pStr), -1);
218476 sqlite3_free(sqlite3_str_finish(pStr));
218477 return;
218478 }
218479 }
218480 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF164, (void *)pUCollator,
218481 icuCollationColl, icuCollationDel
218482 );
218483 if( rc!=SQLITE_OK0 ){
218484 ucol_close(pUCollator);
218485 sqlite3_result_error(p, "Error registering collation function", -1);
218486 }
218487}
218488
218489/*
218490** Register the ICU extension functions with database db.
218491*/
218492SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db){
218493# define SQLITEICU_EXTRAFLAGS (SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000)
218494 static const struct IcuScalar {
218495 const char *zName; /* Function name */
218496 unsigned char nArg; /* Number of arguments */
218497 unsigned int enc; /* Optimal text encoding */
218498 unsigned char iContext; /* sqlite3_user_data() context */
218499 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
218500 } scalars[] = {
218501 {"icu_load_collation",2,SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000,1, icuLoadCollation},
218502 {"icu_load_collation",3,SQLITE_UTF81|SQLITE_DIRECTONLY0x000080000,1, icuLoadCollation},
218503#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
218504 {"regexp", 2, SQLITE_ANY5|SQLITEICU_EXTRAFLAGS, 0, icuRegexpFunc},
218505 {"lower", 1, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
218506 {"lower", 2, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
218507 {"upper", 1, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
218508 {"upper", 2, SQLITE_UTF164|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
218509 {"lower", 1, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
218510 {"lower", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuCaseFunc16},
218511 {"upper", 1, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
218512 {"upper", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 1, icuCaseFunc16},
218513 {"like", 2, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
218514 {"like", 3, SQLITE_UTF81|SQLITEICU_EXTRAFLAGS, 0, icuLikeFunc},
218515#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
218516 };
218517 int rc = SQLITE_OK0;
218518 int i;
218519
218520 for(i=0; rc==SQLITE_OK0 && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
218521 const struct IcuScalar *p = &scalars[i];
218522 rc = sqlite3_create_function(
218523 db, p->zName, p->nArg, p->enc,
218524 p->iContext ? (void*)db : (void*)0,
218525 p->xFunc, 0, 0
218526 );
218527 }
218528
218529 return rc;
218530}
218531
218532#if !SQLITE_CORE1
218533#ifdef _WIN32
218534__declspec(dllexport)
218535#endif
218536SQLITE_API int sqlite3_icu_init(
218537 sqlite3 *db,
218538 char **pzErrMsg,
218539 const sqlite3_api_routines *pApi
218540){
218541 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
218542 return sqlite3IcuInit(db);
218543}
218544#endif
218545
218546#endif
218547
218548/************** End of icu.c *************************************************/
218549/************** Begin file fts3_icu.c ****************************************/
218550/*
218551** 2007 June 22
218552**
218553** The author disclaims copyright to this source code. In place of
218554** a legal notice, here is a blessing:
218555**
218556** May you do good and not evil.
218557** May you find forgiveness for yourself and forgive others.
218558** May you share freely, never taking more than you give.
218559**
218560*************************************************************************
218561** This file implements a tokenizer for fts3 based on the ICU library.
218562*/
218563/* #include "fts3Int.h" */
218564#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS31)
218565#ifdef SQLITE_ENABLE_ICU
218566
218567/* #include <assert.h> */
218568/* #include <string.h> */
218569/* #include "fts3_tokenizer.h" */
218570
218571#include <unicode/ubrk.h>
218572/* #include <unicode/ucol.h> */
218573/* #include <unicode/ustring.h> */
218574#include <unicode/utf16.h>
218575
218576typedef struct IcuTokenizer IcuTokenizer;
218577typedef struct IcuCursor IcuCursor;
218578
218579struct IcuTokenizer {
218580 sqlite3_tokenizer base;
218581 char *zLocale;
218582};
218583
218584struct IcuCursor {
218585 sqlite3_tokenizer_cursor base;
218586
218587 UBreakIterator *pIter; /* ICU break-iterator object */
218588 int nChar; /* Number of UChar elements in pInput */
218589 UChar *aChar; /* Copy of input using utf-16 encoding */
218590 int *aOffset; /* Offsets of each character in utf-8 input */
218591
218592 int nBuffer;
218593 char *zBuffer;
218594
218595 int iToken;
218596};
218597
218598/*
218599** Create a new tokenizer instance.
218600*/
218601static int icuCreate(
218602 int argc, /* Number of entries in argv[] */
218603 const char * const *argv, /* Tokenizer creation arguments */
218604 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
218605){
218606 IcuTokenizer *p;
218607 int n = 0;
218608
218609 if( argc>0 ){
218610 n = strlen(argv[0])+1;
218611 }
218612 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
218613 if( !p ){
218614 return SQLITE_NOMEM7;
218615 }
218616 memset(p, 0, sizeof(IcuTokenizer));
218617
218618 if( n ){
218619 p->zLocale = (char *)&p[1];
218620 memcpy(p->zLocale, argv[0], n);
218621 }
218622
218623 *ppTokenizer = (sqlite3_tokenizer *)p;
218624
218625 return SQLITE_OK0;
218626}
218627
218628/*
218629** Destroy a tokenizer
218630*/
218631static int icuDestroy(sqlite3_tokenizer *pTokenizer){
218632 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
218633 sqlite3_free(p);
218634 return SQLITE_OK0;
218635}
218636
218637/*
218638** Prepare to begin tokenizing a particular string. The input
218639** string to be tokenized is pInput[0..nBytes-1]. A cursor
218640** used to incrementally tokenize this string is returned in
218641** *ppCursor.
218642*/
218643static int icuOpen(
218644 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
218645 const char *zInput, /* Input string */
218646 int nInput, /* Length of zInput in bytes */
218647 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
218648){
218649 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
218650 IcuCursor *pCsr;
218651
218652 const int32_t opt = U_FOLD_CASE_DEFAULT;
218653 UErrorCode status = U_ZERO_ERROR;
218654 int nChar;
218655
218656 UChar32 c;
218657 int iInput = 0;
218658 int iOut = 0;
218659
218660 *ppCursor = 0;
218661
218662 if( zInput==0 ){
218663 nInput = 0;
218664 zInput = "";
218665 }else if( nInput<0 ){
218666 nInput = strlen(zInput);
218667 }
218668 nChar = nInput+1;
218669 pCsr = (IcuCursor *)sqlite3_malloc64(
218670 sizeof(IcuCursor) + /* IcuCursor */
218671 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
218672 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
218673 );
218674 if( !pCsr ){
218675 return SQLITE_NOMEM7;
218676 }
218677 memset(pCsr, 0, sizeof(IcuCursor));
218678 pCsr->aChar = (UChar *)&pCsr[1];
218679 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
218680
218681 pCsr->aOffset[iOut] = iInput;
218682 U8_NEXT(zInput, iInput, nInput, c);
218683 while( c>0 ){
218684 int isError = 0;
218685 c = u_foldCase(c, opt);
218686 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
218687 if( isError ){
218688 sqlite3_free(pCsr);
218689 return SQLITE_ERROR1;
218690 }
218691 pCsr->aOffset[iOut] = iInput;
218692
218693 if( iInput<nInput ){
218694 U8_NEXT(zInput, iInput, nInput, c);
218695 }else{
218696 c = 0;
218697 }
218698 }
218699
218700 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
218701 if( !U_SUCCESS(status) ){
218702 sqlite3_free(pCsr);
218703 return SQLITE_ERROR1;
218704 }
218705 pCsr->nChar = iOut;
218706
218707 ubrk_first(pCsr->pIter);
218708 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
218709 return SQLITE_OK0;
218710}
218711
218712/*
218713** Close a tokenization cursor previously opened by a call to icuOpen().
218714*/
218715static int icuClose(sqlite3_tokenizer_cursor *pCursor){
218716 IcuCursor *pCsr = (IcuCursor *)pCursor;
218717 ubrk_close(pCsr->pIter);
218718 sqlite3_free(pCsr->zBuffer);
218719 sqlite3_free(pCsr);
218720 return SQLITE_OK0;
218721}
218722
218723/*
218724** Extract the next token from a tokenization cursor.
218725*/
218726static int icuNext(
218727 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
218728 const char **ppToken, /* OUT: *ppToken is the token text */
218729 int *pnBytes, /* OUT: Number of bytes in token */
218730 int *piStartOffset, /* OUT: Starting offset of token */
218731 int *piEndOffset, /* OUT: Ending offset of token */
218732 int *piPosition /* OUT: Position integer of token */
218733){
218734 IcuCursor *pCsr = (IcuCursor *)pCursor;
218735
218736 int iStart = 0;
218737 int iEnd = 0;
218738 int nByte = 0;
218739
218740 while( iStart==iEnd ){
218741 UChar32 c;
218742
218743 iStart = ubrk_current(pCsr->pIter);
218744 iEnd = ubrk_next(pCsr->pIter);
218745 if( iEnd==UBRK_DONE ){
218746 return SQLITE_DONE101;
218747 }
218748
218749 while( iStart<iEnd ){
218750 int iWhite = iStart;
218751 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
218752 if( u_isspace(c) ){
218753 iStart = iWhite;
218754 }else{
218755 break;
218756 }
218757 }
218758 assert(iStart<=iEnd)((void) (0));
218759 }
218760
218761 do {
218762 UErrorCode status = U_ZERO_ERROR;
218763 if( nByte ){
218764 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
218765 if( !zNew ){
218766 return SQLITE_NOMEM7;
218767 }
218768 pCsr->zBuffer = zNew;
218769 pCsr->nBuffer = nByte;
218770 }
218771
218772 u_strToUTF8(
218773 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
218774 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
218775 &status /* Output success/failure */
218776 );
218777 } while( nByte>pCsr->nBuffer );
218778
218779 *ppToken = pCsr->zBuffer;
218780 *pnBytes = nByte;
218781 *piStartOffset = pCsr->aOffset[iStart];
218782 *piEndOffset = pCsr->aOffset[iEnd];
218783 *piPosition = pCsr->iToken++;
218784
218785 return SQLITE_OK0;
218786}
218787
218788/*
218789** The set of routines that implement the simple tokenizer
218790*/
218791static const sqlite3_tokenizer_module icuTokenizerModule = {
218792 0, /* iVersion */
218793 icuCreate, /* xCreate */
218794 icuDestroy, /* xCreate */
218795 icuOpen, /* xOpen */
218796 icuClose, /* xClose */
218797 icuNext, /* xNext */
218798 0, /* xLanguageid */
218799};
218800
218801/*
218802** Set *ppModule to point at the implementation of the ICU tokenizer.
218803*/
218804SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(
218805 sqlite3_tokenizer_module const**ppModule
218806){
218807 *ppModule = &icuTokenizerModule;
218808}
218809
218810#endif /* defined(SQLITE_ENABLE_ICU) */
218811#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
218812
218813/************** End of fts3_icu.c ********************************************/
218814/************** Begin file sqlite3rbu.c **************************************/
218815/*
218816** 2014 August 30
218817**
218818** The author disclaims copyright to this source code. In place of
218819** a legal notice, here is a blessing:
218820**
218821** May you do good and not evil.
218822** May you find forgiveness for yourself and forgive others.
218823** May you share freely, never taking more than you give.
218824**
218825*************************************************************************
218826**
218827**
218828** OVERVIEW
218829**
218830** The RBU extension requires that the RBU update be packaged as an
218831** SQLite database. The tables it expects to find are described in
218832** sqlite3rbu.h. Essentially, for each table xyz in the target database
218833** that the user wishes to write to, a corresponding data_xyz table is
218834** created in the RBU database and populated with one row for each row to
218835** update, insert or delete from the target table.
218836**
218837** The update proceeds in three stages:
218838**
218839** 1) The database is updated. The modified database pages are written
218840** to a *-oal file. A *-oal file is just like a *-wal file, except
218841** that it is named "<database>-oal" instead of "<database>-wal".
218842** Because regular SQLite clients do not look for file named
218843** "<database>-oal", they go on using the original database in
218844** rollback mode while the *-oal file is being generated.
218845**
218846** During this stage RBU does not update the database by writing
218847** directly to the target tables. Instead it creates "imposter"
218848** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
218849** to update each b-tree individually. All updates required by each
218850** b-tree are completed before moving on to the next, and all
218851** updates are done in sorted key order.
218852**
218853** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
218854** location using a call to rename(2). Before doing this the RBU
218855** module takes an EXCLUSIVE lock on the database file, ensuring
218856** that there are no other active readers.
218857**
218858** Once the EXCLUSIVE lock is released, any other database readers
218859** detect the new *-wal file and read the database in wal mode. At
218860** this point they see the new version of the database - including
218861** the updates made as part of the RBU update.
218862**
218863** 3) The new *-wal file is checkpointed. This proceeds in the same way
218864** as a regular database checkpoint, except that a single frame is
218865** checkpointed each time sqlite3rbu_step() is called. If the RBU
218866** handle is closed before the entire *-wal file is checkpointed,
218867** the checkpoint progress is saved in the RBU database and the
218868** checkpoint can be resumed by another RBU client at some point in
218869** the future.
218870**
218871** POTENTIAL PROBLEMS
218872**
218873** The rename() call might not be portable. And RBU is not currently
218874** syncing the directory after renaming the file.
218875**
218876** When state is saved, any commit to the *-oal file and the commit to
218877** the RBU update database are not atomic. So if the power fails at the
218878** wrong moment they might get out of sync. As the main database will be
218879** committed before the RBU update database this will likely either just
218880** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
218881** constraint violations).
218882**
218883** If some client does modify the target database mid RBU update, or some
218884** other error occurs, the RBU extension will keep throwing errors. It's
218885** not really clear how to get out of this state. The system could just
218886** by delete the RBU update database and *-oal file and have the device
218887** download the update again and start over.
218888**
218889** At present, for an UPDATE, both the new.* and old.* records are
218890** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
218891** fields are collected. This means we're probably writing a lot more
218892** data to disk when saving the state of an ongoing update to the RBU
218893** update database than is strictly necessary.
218894**
218895*/
218896
218897/* #include <assert.h> */
218898/* #include <string.h> */
218899/* #include <stdio.h> */
218900
218901/* #include "sqlite3.h" */
218902
218903#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_RBU)
218904/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
218905/************** Begin file sqlite3rbu.h **************************************/
218906/*
218907** 2014 August 30
218908**
218909** The author disclaims copyright to this source code. In place of
218910** a legal notice, here is a blessing:
218911**
218912** May you do good and not evil.
218913** May you find forgiveness for yourself and forgive others.
218914** May you share freely, never taking more than you give.
218915**
218916*************************************************************************
218917**
218918** This file contains the public interface for the RBU extension.
218919*/
218920
218921/*
218922** SUMMARY
218923**
218924** Writing a transaction containing a large number of operations on
218925** b-tree indexes that are collectively larger than the available cache
218926** memory can be very inefficient.
218927**
218928** The problem is that in order to update a b-tree, the leaf page (at least)
218929** containing the entry being inserted or deleted must be modified. If the
218930** working set of leaves is larger than the available cache memory, then a
218931** single leaf that is modified more than once as part of the transaction
218932** may be loaded from or written to the persistent media multiple times.
218933** Additionally, because the index updates are likely to be applied in
218934** random order, access to pages within the database is also likely to be in
218935** random order, which is itself quite inefficient.
218936**
218937** One way to improve the situation is to sort the operations on each index
218938** by index key before applying them to the b-tree. This leads to an IO
218939** pattern that resembles a single linear scan through the index b-tree,
218940** and all but guarantees each modified leaf page is loaded and stored
218941** exactly once. SQLite uses this trick to improve the performance of
218942** CREATE INDEX commands. This extension allows it to be used to improve
218943** the performance of large transactions on existing databases.
218944**
218945** Additionally, this extension allows the work involved in writing the
218946** large transaction to be broken down into sub-transactions performed
218947** sequentially by separate processes. This is useful if the system cannot
218948** guarantee that a single update process will run for long enough to apply
218949** the entire update, for example because the update is being applied on a
218950** mobile device that is frequently rebooted. Even after the writer process
218951** has committed one or more sub-transactions, other database clients continue
218952** to read from the original database snapshot. In other words, partially
218953** applied transactions are not visible to other clients.
218954**
218955** "RBU" stands for "Resumable Bulk Update". As in a large database update
218956** transmitted via a wireless network to a mobile device. A transaction
218957** applied using this extension is hence refered to as an "RBU update".
218958**
218959**
218960** LIMITATIONS
218961**
218962** An "RBU update" transaction is subject to the following limitations:
218963**
218964** * The transaction must consist of INSERT, UPDATE and DELETE operations
218965** only.
218966**
218967** * INSERT statements may not use any default values.
218968**
218969** * UPDATE and DELETE statements must identify their target rows by
218970** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
218971** KEY fields may not be updated or deleted. If the table being written
218972** has no PRIMARY KEY, affected rows must be identified by rowid.
218973**
218974** * UPDATE statements may not modify PRIMARY KEY columns.
218975**
218976** * No triggers will be fired.
218977**
218978** * No foreign key violations are detected or reported.
218979**
218980** * CHECK constraints are not enforced.
218981**
218982** * No constraint handling mode except for "OR ROLLBACK" is supported.
218983**
218984**
218985** PREPARATION
218986**
218987** An "RBU update" is stored as a separate SQLite database. A database
218988** containing an RBU update is an "RBU database". For each table in the
218989** target database to be updated, the RBU database should contain a table
218990** named "data_<target name>" containing the same set of columns as the
218991** target table, and one more - "rbu_control". The data_% table should
218992** have no PRIMARY KEY or UNIQUE constraints, but each column should have
218993** the same type as the corresponding column in the target database.
218994** The "rbu_control" column should have no type at all. For example, if
218995** the target database contains:
218996**
218997** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
218998**
218999** Then the RBU database should contain:
219000**
219001** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
219002**
219003** The order of the columns in the data_% table does not matter.
219004**
219005** Instead of a regular table, the RBU database may also contain virtual
219006** tables or views named using the data_<target> naming scheme.
219007**
219008** Instead of the plain data_<target> naming scheme, RBU database tables
219009** may also be named data<integer>_<target>, where <integer> is any sequence
219010** of zero or more numeric characters (0-9). This can be significant because
219011** tables within the RBU database are always processed in order sorted by
219012** name. By judicious selection of the <integer> portion of the names
219013** of the RBU tables the user can therefore control the order in which they
219014** are processed. This can be useful, for example, to ensure that "external
219015** content" FTS4 tables are updated before their underlying content tables.
219016**
219017** If the target database table is a virtual table or a table that has no
219018** PRIMARY KEY declaration, the data_% table must also contain a column
219019** named "rbu_rowid". This column is mapped to the table's implicit primary
219020** key column - "rowid". Virtual tables for which the "rowid" column does
219021** not function like a primary key value cannot be updated using RBU. For
219022** example, if the target db contains either of the following:
219023**
219024** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
219025** CREATE TABLE x1(a, b)
219026**
219027** then the RBU database should contain:
219028**
219029** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
219030**
219031** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
219032** target table must be present in the input table. For virtual tables,
219033** hidden columns are optional - they are updated by RBU if present in
219034** the input table, or not otherwise. For example, to write to an fts4
219035** table with a hidden languageid column such as:
219036**
219037** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
219038**
219039** Either of the following input table schemas may be used:
219040**
219041** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
219042** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
219043**
219044** For each row to INSERT into the target database as part of the RBU
219045** update, the corresponding data_% table should contain a single record
219046** with the "rbu_control" column set to contain integer value 0. The
219047** other columns should be set to the values that make up the new record
219048** to insert.
219049**
219050** If the target database table has an INTEGER PRIMARY KEY, it is not
219051** possible to insert a NULL value into the IPK column. Attempting to
219052** do so results in an SQLITE_MISMATCH error.
219053**
219054** For each row to DELETE from the target database as part of the RBU
219055** update, the corresponding data_% table should contain a single record
219056** with the "rbu_control" column set to contain integer value 1. The
219057** real primary key values of the row to delete should be stored in the
219058** corresponding columns of the data_% table. The values stored in the
219059** other columns are not used.
219060**
219061** For each row to UPDATE from the target database as part of the RBU
219062** update, the corresponding data_% table should contain a single record
219063** with the "rbu_control" column set to contain a value of type text.
219064** The real primary key values identifying the row to update should be
219065** stored in the corresponding columns of the data_% table row, as should
219066** the new values of all columns being update. The text value in the
219067** "rbu_control" column must contain the same number of characters as
219068** there are columns in the target database table, and must consist entirely
219069** of 'x' and '.' characters (or in some special cases 'd' - see below). For
219070** each column that is being updated, the corresponding character is set to
219071** 'x'. For those that remain as they are, the corresponding character of the
219072** rbu_control value should be set to '.'. For example, given the tables
219073** above, the update statement:
219074**
219075** UPDATE t1 SET c = 'usa' WHERE a = 4;
219076**
219077** is represented by the data_t1 row created by:
219078**
219079** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
219080**
219081** Instead of an 'x' character, characters of the rbu_control value specified
219082** for UPDATEs may also be set to 'd'. In this case, instead of updating the
219083** target table with the value stored in the corresponding data_% column, the
219084** user-defined SQL function "rbu_delta()" is invoked and the result stored in
219085** the target table column. rbu_delta() is invoked with two arguments - the
219086** original value currently stored in the target table column and the
219087** value specified in the data_xxx table.
219088**
219089** For example, this row:
219090**
219091** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
219092**
219093** is similar to an UPDATE statement such as:
219094**
219095** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
219096**
219097** Finally, if an 'f' character appears in place of a 'd' or 's' in an
219098** ota_control string, the contents of the data_xxx table column is assumed
219099** to be a "fossil delta" - a patch to be applied to a blob value in the
219100** format used by the fossil source-code management system. In this case
219101** the existing value within the target database table must be of type BLOB.
219102** It is replaced by the result of applying the specified fossil delta to
219103** itself.
219104**
219105** If the target database table is a virtual table or a table with no PRIMARY
219106** KEY, the rbu_control value should not include a character corresponding
219107** to the rbu_rowid value. For example, this:
219108**
219109** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
219110** VALUES(NULL, 'usa', 12, '.x');
219111**
219112** causes a result similar to:
219113**
219114** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
219115**
219116** The data_xxx tables themselves should have no PRIMARY KEY declarations.
219117** However, RBU is more efficient if reading the rows in from each data_xxx
219118** table in "rowid" order is roughly the same as reading them sorted by
219119** the PRIMARY KEY of the corresponding target database table. In other
219120** words, rows should be sorted using the destination table PRIMARY KEY
219121** fields before they are inserted into the data_xxx tables.
219122**
219123** USAGE
219124**
219125** The API declared below allows an application to apply an RBU update
219126** stored on disk to an existing target database. Essentially, the
219127** application:
219128**
219129** 1) Opens an RBU handle using the sqlite3rbu_open() function.
219130**
219131** 2) Registers any required virtual table modules with the database
219132** handle returned by sqlite3rbu_db(). Also, if required, register
219133** the rbu_delta() implementation.
219134**
219135** 3) Calls the sqlite3rbu_step() function one or more times on
219136** the new handle. Each call to sqlite3rbu_step() performs a single
219137** b-tree operation, so thousands of calls may be required to apply
219138** a complete update.
219139**
219140** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
219141** sqlite3rbu_step() has been called enough times to completely
219142** apply the update to the target database, then the RBU database
219143** is marked as fully applied. Otherwise, the state of the RBU
219144** update application is saved in the RBU database for later
219145** resumption.
219146**
219147** See comments below for more detail on APIs.
219148**
219149** If an update is only partially applied to the target database by the
219150** time sqlite3rbu_close() is called, various state information is saved
219151** within the RBU database. This allows subsequent processes to automatically
219152** resume the RBU update from where it left off.
219153**
219154** To remove all RBU extension state information, returning an RBU database
219155** to its original contents, it is sufficient to drop all tables that begin
219156** with the prefix "rbu_"
219157**
219158** DATABASE LOCKING
219159**
219160** An RBU update may not be applied to a database in WAL mode. Attempting
219161** to do so is an error (SQLITE_ERROR).
219162**
219163** While an RBU handle is open, a SHARED lock may be held on the target
219164** database file. This means it is possible for other clients to read the
219165** database, but not to write it.
219166**
219167** If an RBU update is started and then suspended before it is completed,
219168** then an external client writes to the database, then attempting to resume
219169** the suspended RBU update is also an error (SQLITE_BUSY).
219170*/
219171
219172#ifndef _SQLITE3RBU_H
219173#define _SQLITE3RBU_H
219174
219175/* #include "sqlite3.h" ** Required for error code definitions ** */
219176
219177#if 0
219178extern "C" {
219179#endif
219180
219181typedef struct sqlite3rbu sqlite3rbu;
219182
219183/*
219184** Open an RBU handle.
219185**
219186** Argument zTarget is the path to the target database. Argument zRbu is
219187** the path to the RBU database. Each call to this function must be matched
219188** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
219189** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
219190** or zRbu begin with "file:", it will be interpreted as an SQLite
219191** database URI, not a regular file name.
219192**
219193** If the zState argument is passed a NULL value, the RBU extension stores
219194** the current state of the update (how many rows have been updated, which
219195** indexes are yet to be updated etc.) within the RBU database itself. This
219196** can be convenient, as it means that the RBU application does not need to
219197** organize removing a separate state file after the update is concluded.
219198** Or, if zState is non-NULL, it must be a path to a database file in which
219199** the RBU extension can store the state of the update.
219200**
219201** When resuming an RBU update, the zState argument must be passed the same
219202** value as when the RBU update was started.
219203**
219204** Once the RBU update is finished, the RBU extension does not
219205** automatically remove any zState database file, even if it created it.
219206**
219207** By default, RBU uses the default VFS to access the files on disk. To
219208** use a VFS other than the default, an SQLite "file:" URI containing a
219209** "vfs=..." option may be passed as the zTarget option.
219210**
219211** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
219212** SQLite's built-in VFSs, including the multiplexor VFS. However it does
219213** not work out of the box with zipvfs. Refer to the comment describing
219214** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
219215*/
219216SQLITE_API sqlite3rbu *sqlite3rbu_open(
219217 const char *zTarget,
219218 const char *zRbu,
219219 const char *zState
219220);
219221
219222/*
219223** Open an RBU handle to perform an RBU vacuum on database file zTarget.
219224** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
219225** that it can be suspended and resumed like an RBU update.
219226**
219227** The second argument to this function identifies a database in which
219228** to store the state of the RBU vacuum operation if it is suspended. The
219229** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
219230** operation, the state database should either not exist or be empty
219231** (contain no tables). If an RBU vacuum is suspended by calling
219232** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
219233** returned SQLITE_DONE, the vacuum state is stored in the state database.
219234** The vacuum can be resumed by calling this function to open a new RBU
219235** handle specifying the same target and state databases.
219236**
219237** If the second argument passed to this function is NULL, then the
219238** name of the state database is "<database>-vacuum", where <database>
219239** is the name of the target database file. In this case, on UNIX, if the
219240** state database is not already present in the file-system, it is created
219241** with the same permissions as the target db is made.
219242**
219243** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
219244** state database ends with "-vactmp". This name is reserved for internal
219245** use.
219246**
219247** This function does not delete the state database after an RBU vacuum
219248** is completed, even if it created it. However, if the call to
219249** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
219250** of the state tables within the state database are zeroed. This way,
219251** the next call to sqlite3rbu_vacuum() opens a handle that starts a
219252** new RBU vacuum operation.
219253**
219254** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
219255** describing the sqlite3rbu_create_vfs() API function below for
219256** a description of the complications associated with using RBU with
219257** zipvfs databases.
219258*/
219259SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
219260 const char *zTarget,
219261 const char *zState
219262);
219263
219264/*
219265** Configure a limit for the amount of temp space that may be used by
219266** the RBU handle passed as the first argument. The new limit is specified
219267** in bytes by the second parameter. If it is positive, the limit is updated.
219268** If the second parameter to this function is passed zero, then the limit
219269** is removed entirely. If the second parameter is negative, the limit is
219270** not modified (this is useful for querying the current limit).
219271**
219272** In all cases the returned value is the current limit in bytes (zero
219273** indicates unlimited).
219274**
219275** If the temp space limit is exceeded during operation, an SQLITE_FULL
219276** error is returned.
219277*/
219278SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
219279
219280/*
219281** Return the current amount of temp file space, in bytes, currently used by
219282** the RBU handle passed as the only argument.
219283*/
219284SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
219285
219286/*
219287** Internally, each RBU connection uses a separate SQLite database
219288** connection to access the target and rbu update databases. This
219289** API allows the application direct access to these database handles.
219290**
219291** The first argument passed to this function must be a valid, open, RBU
219292** handle. The second argument should be passed zero to access the target
219293** database handle, or non-zero to access the rbu update database handle.
219294** Accessing the underlying database handles may be useful in the
219295** following scenarios:
219296**
219297** * If any target tables are virtual tables, it may be necessary to
219298** call sqlite3_create_module() on the target database handle to
219299** register the required virtual table implementations.
219300**
219301** * If the data_xxx tables in the RBU source database are virtual
219302** tables, the application may need to call sqlite3_create_module() on
219303** the rbu update db handle to any required virtual table
219304** implementations.
219305**
219306** * If the application uses the "rbu_delta()" feature described above,
219307** it must use sqlite3_create_function() or similar to register the
219308** rbu_delta() implementation with the target database handle.
219309**
219310** If an error has occurred, either while opening or stepping the RBU object,
219311** this function may return NULL. The error code and message may be collected
219312** when sqlite3rbu_close() is called.
219313**
219314** Database handles returned by this function remain valid until the next
219315** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
219316*/
219317SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
219318
219319/*
219320** Do some work towards applying the RBU update to the target db.
219321**
219322** Return SQLITE_DONE if the update has been completely applied, or
219323** SQLITE_OK if no error occurs but there remains work to do to apply
219324** the RBU update. If an error does occur, some other error code is
219325** returned.
219326**
219327** Once a call to sqlite3rbu_step() has returned a value other than
219328** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
219329** that immediately return the same value.
219330*/
219331SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
219332
219333/*
219334** Force RBU to save its state to disk.
219335**
219336** If a power failure or application crash occurs during an update, following
219337** system recovery RBU may resume the update from the point at which the state
219338** was last saved. In other words, from the most recent successful call to
219339** sqlite3rbu_close() or this function.
219340**
219341** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
219342*/
219343SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
219344
219345/*
219346** Close an RBU handle.
219347**
219348** If the RBU update has been completely applied, mark the RBU database
219349** as fully applied. Otherwise, assuming no error has occurred, save the
219350** current state of the RBU update appliation to the RBU database.
219351**
219352** If an error has already occurred as part of an sqlite3rbu_step()
219353** or sqlite3rbu_open() call, or if one occurs within this function, an
219354** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
219355** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
219356** English language error message. It is the responsibility of the caller to
219357** eventually free any such buffer using sqlite3_free().
219358**
219359** Otherwise, if no error occurs, this function returns SQLITE_OK if the
219360** update has been partially applied, or SQLITE_DONE if it has been
219361** completely applied.
219362*/
219363SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
219364
219365/*
219366** Return the total number of key-value operations (inserts, deletes or
219367** updates) that have been performed on the target database since the
219368** current RBU update was started.
219369*/
219370SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
219371
219372/*
219373** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
219374** progress indications for the two stages of an RBU update. This API may
219375** be useful for driving GUI progress indicators and similar.
219376**
219377** An RBU update is divided into two stages:
219378**
219379** * Stage 1, in which changes are accumulated in an oal/wal file, and
219380** * Stage 2, in which the contents of the wal file are copied into the
219381** main database.
219382**
219383** The update is visible to non-RBU clients during stage 2. During stage 1
219384** non-RBU reader clients may see the original database.
219385**
219386** If this API is called during stage 2 of the update, output variable
219387** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
219388** to a value between 0 and 10000 to indicate the permyriadage progress of
219389** stage 2. A value of 5000 indicates that stage 2 is half finished,
219390** 9000 indicates that it is 90% finished, and so on.
219391**
219392** If this API is called during stage 1 of the update, output variable
219393** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
219394** value to which (*pnOne) is set depends on whether or not the RBU
219395** database contains an "rbu_count" table. The rbu_count table, if it
219396** exists, must contain the same columns as the following:
219397**
219398** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
219399**
219400** There must be one row in the table for each source (data_xxx) table within
219401** the RBU database. The 'tbl' column should contain the name of the source
219402** table. The 'cnt' column should contain the number of rows within the
219403** source table.
219404**
219405** If the rbu_count table is present and populated correctly and this
219406** API is called during stage 1, the *pnOne output variable is set to the
219407** permyriadage progress of the same stage. If the rbu_count table does
219408** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
219409** table exists but is not correctly populated, the value of the *pnOne
219410** output variable during stage 1 is undefined.
219411*/
219412SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
219413
219414/*
219415** Obtain an indication as to the current stage of an RBU update or vacuum.
219416** This function always returns one of the SQLITE_RBU_STATE_XXX constants
219417** defined in this file. Return values should be interpreted as follows:
219418**
219419** SQLITE_RBU_STATE_OAL:
219420** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
219421** may either add further data to the *-oal file, or compute data that will
219422** be added by a subsequent call.
219423**
219424** SQLITE_RBU_STATE_MOVE:
219425** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
219426** will move the *-oal file to the equivalent *-wal path. If the current
219427** operation is an RBU update, then the updated version of the database
219428** file will become visible to ordinary SQLite clients following the next
219429** call to sqlite3rbu_step().
219430**
219431** SQLITE_RBU_STATE_CHECKPOINT:
219432** RBU is currently performing an incremental checkpoint. The next call to
219433** sqlite3rbu_step() will copy a page of data from the *-wal file into
219434** the target database file.
219435**
219436** SQLITE_RBU_STATE_DONE:
219437** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
219438** will immediately return SQLITE_DONE.
219439**
219440** SQLITE_RBU_STATE_ERROR:
219441** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
219442** immediately return the SQLite error code associated with the error.
219443*/
219444#define SQLITE_RBU_STATE_OAL 1
219445#define SQLITE_RBU_STATE_MOVE 2
219446#define SQLITE_RBU_STATE_CHECKPOINT 3
219447#define SQLITE_RBU_STATE_DONE 4
219448#define SQLITE_RBU_STATE_ERROR 5
219449
219450SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
219451
219452/*
219453** As part of applying an RBU update or performing an RBU vacuum operation,
219454** the system must at one point move the *-oal file to the equivalent *-wal
219455** path. Normally, it does this by invoking POSIX function rename(2) directly.
219456** Except on WINCE platforms, where it uses win32 API MoveFileW(). This
219457** function may be used to register a callback that the RBU module will invoke
219458** instead of one of these APIs.
219459**
219460** If a callback is registered with an RBU handle, it invokes it instead
219461** of rename(2) when it needs to move a file within the file-system. The
219462** first argument passed to the xRename() callback is a copy of the second
219463** argument (pArg) passed to this function. The second is the full path
219464** to the file to move and the third the full path to which it should be
219465** moved. The callback function should return SQLITE_OK to indicate
219466** success. If an error occurs, it should return an SQLite error code.
219467** In this case the RBU operation will be abandoned and the error returned
219468** to the RBU user.
219469**
219470** Passing a NULL pointer in place of the xRename argument to this function
219471** restores the default behaviour.
219472*/
219473SQLITE_API void sqlite3rbu_rename_handler(
219474 sqlite3rbu *pRbu,
219475 void *pArg,
219476 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
219477);
219478
219479
219480/*
219481** Create an RBU VFS named zName that accesses the underlying file-system
219482** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
219483** then the new RBU VFS uses the default system VFS to access the file-system.
219484** The new object is registered as a non-default VFS with SQLite before
219485** returning.
219486**
219487** Part of the RBU implementation uses a custom VFS object. Usually, this
219488** object is created and deleted automatically by RBU.
219489**
219490** The exception is for applications that also use zipvfs. In this case,
219491** the custom VFS must be explicitly created by the user before the RBU
219492** handle is opened. The RBU VFS should be installed so that the zipvfs
219493** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
219494** (for example multiplexor) to access the file-system. For example,
219495** to assemble an RBU enabled VFS stack that uses both zipvfs and
219496** multiplexor (error checking omitted):
219497**
219498** // Create a VFS named "multiplex" (not the default).
219499** sqlite3_multiplex_initialize(0, 0);
219500**
219501** // Create an rbu VFS named "rbu" that uses multiplexor. If the
219502** // second argument were replaced with NULL, the "rbu" VFS would
219503** // access the file-system via the system default VFS, bypassing the
219504** // multiplexor.
219505** sqlite3rbu_create_vfs("rbu", "multiplex");
219506**
219507** // Create a zipvfs VFS named "zipvfs" that uses rbu.
219508** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
219509**
219510** // Make zipvfs the default VFS.
219511** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
219512**
219513** Because the default VFS created above includes a RBU functionality, it
219514** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
219515** that does not include the RBU layer results in an error.
219516**
219517** The overhead of adding the "rbu" VFS to the system is negligible for
219518** non-RBU users. There is no harm in an application accessing the
219519** file-system via "rbu" all the time, even if it only uses RBU functionality
219520** occasionally.
219521*/
219522SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
219523
219524/*
219525** Deregister and destroy an RBU vfs created by an earlier call to
219526** sqlite3rbu_create_vfs().
219527**
219528** VFS objects are not reference counted. If a VFS object is destroyed
219529** before all database handles that use it have been closed, the results
219530** are undefined.
219531*/
219532SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
219533
219534#if 0
219535} /* end of the 'extern "C"' block */
219536#endif
219537
219538#endif /* _SQLITE3RBU_H */
219539
219540/************** End of sqlite3rbu.h ******************************************/
219541/************** Continuing where we left off in sqlite3rbu.c *****************/
219542
219543#if defined(_WIN32_WCE)
219544/* #include "windows.h" */
219545#endif
219546
219547/* Maximum number of prepared UPDATE statements held by this module */
219548#define SQLITE_RBU_UPDATE_CACHESIZE 16
219549
219550/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
219551** to enable checksum verification.
219552*/
219553#ifndef RBU_ENABLE_DELTA_CKSUM
219554# define RBU_ENABLE_DELTA_CKSUM 0
219555#endif
219556
219557/*
219558** Swap two objects of type TYPE.
219559*/
219560#if !defined(SQLITE_AMALGAMATION1)
219561# define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
219562#endif
219563
219564/*
219565** Name of the URI option that causes RBU to take an exclusive lock as
219566** part of the incremental checkpoint operation.
219567*/
219568#define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
219569
219570
219571/*
219572** The rbu_state table is used to save the state of a partially applied
219573** update so that it can be resumed later. The table consists of integer
219574** keys mapped to values as follows:
219575**
219576** RBU_STATE_STAGE:
219577** May be set to integer values 1, 2, 4 or 5. As follows:
219578** 1: the *-rbu file is currently under construction.
219579** 2: the *-rbu file has been constructed, but not yet moved
219580** to the *-wal path.
219581** 4: the checkpoint is underway.
219582** 5: the rbu update has been checkpointed.
219583**
219584** RBU_STATE_TBL:
219585** Only valid if STAGE==1. The target database name of the table
219586** currently being written.
219587**
219588** RBU_STATE_IDX:
219589** Only valid if STAGE==1. The target database name of the index
219590** currently being written, or NULL if the main table is currently being
219591** updated.
219592**
219593** RBU_STATE_ROW:
219594** Only valid if STAGE==1. Number of rows already processed for the current
219595** table/index.
219596**
219597** RBU_STATE_PROGRESS:
219598** Trbul number of sqlite3rbu_step() calls made so far as part of this
219599** rbu update.
219600**
219601** RBU_STATE_CKPT:
219602** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
219603** header created by recovering the *-wal file. This is used to detect
219604** cases when another client appends frames to the *-wal file in the
219605** middle of an incremental checkpoint (an incremental checkpoint cannot
219606** be continued if this happens).
219607**
219608** RBU_STATE_COOKIE:
219609** Valid if STAGE==1. The current change-counter cookie value in the
219610** target db file.
219611**
219612** RBU_STATE_OALSZ:
219613** Valid if STAGE==1. The size in bytes of the *-oal file.
219614**
219615** RBU_STATE_DATATBL:
219616** Only valid if STAGE==1. The RBU database name of the table
219617** currently being read.
219618*/
219619#define RBU_STATE_STAGE 1
219620#define RBU_STATE_TBL 2
219621#define RBU_STATE_IDX 3
219622#define RBU_STATE_ROW 4
219623#define RBU_STATE_PROGRESS 5
219624#define RBU_STATE_CKPT 6
219625#define RBU_STATE_COOKIE 7
219626#define RBU_STATE_OALSZ 8
219627#define RBU_STATE_PHASEONESTEP 9
219628#define RBU_STATE_DATATBL 10
219629
219630#define RBU_STAGE_OAL 1
219631#define RBU_STAGE_MOVE 2
219632#define RBU_STAGE_CAPTURE 3
219633#define RBU_STAGE_CKPT 4
219634#define RBU_STAGE_DONE 5
219635
219636
219637#define RBU_CREATE_STATE \
219638 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
219639
219640typedef struct RbuFrame RbuFrame;
219641typedef struct RbuObjIter RbuObjIter;
219642typedef struct RbuState RbuState;
219643typedef struct RbuSpan RbuSpan;
219644typedef struct rbu_vfs rbu_vfs;
219645typedef struct rbu_file rbu_file;
219646typedef struct RbuUpdateStmt RbuUpdateStmt;
219647
219648#if !defined(SQLITE_AMALGAMATION1)
219649typedef unsigned int u32;
219650typedef unsigned short u16;
219651typedef unsigned char u8;
219652typedef sqlite3_int64 i64;
219653typedef sqlite3_uint64 u64;
219654#endif
219655
219656/*
219657** These values must match the values defined in wal.c for the equivalent
219658** locks. These are not magic numbers as they are part of the SQLite file
219659** format.
219660*/
219661#define WAL_LOCK_WRITE 0
219662#define WAL_LOCK_CKPT 1
219663#define WAL_LOCK_READ0 3
219664
219665#define SQLITE_FCNTL_RBUCNT 5149216
219666
219667/*
219668** A structure to store values read from the rbu_state table in memory.
219669*/
219670struct RbuState {
219671 int eStage;
219672 char *zTbl;
219673 char *zDataTbl;
219674 char *zIdx;
219675 i64 iWalCksum;
219676 int nRow;
219677 i64 nProgress;
219678 u32 iCookie;
219679 i64 iOalSz;
219680 i64 nPhaseOneStep;
219681};
219682
219683struct RbuUpdateStmt {
219684 char *zMask; /* Copy of update mask used with pUpdate */
219685 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
219686 RbuUpdateStmt *pNext;
219687};
219688
219689struct RbuSpan {
219690 const char *zSpan;
219691 int nSpan;
219692};
219693
219694/*
219695** An iterator of this type is used to iterate through all objects in
219696** the target database that require updating. For each such table, the
219697** iterator visits, in order:
219698**
219699** * the table itself,
219700** * each index of the table (zero or more points to visit), and
219701** * a special "cleanup table" state.
219702**
219703** abIndexed:
219704** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
219705** it points to an array of flags nTblCol elements in size. The flag is
219706** set for each column that is either a part of the PK or a part of an
219707** index. Or clear otherwise.
219708**
219709** If there are one or more partial indexes on the table, all fields of
219710** this array set set to 1. This is because in that case, the module has
219711** no way to tell which fields will be required to add and remove entries
219712** from the partial indexes.
219713**
219714*/
219715struct RbuObjIter {
219716 sqlite3_stmt *pTblIter; /* Iterate through tables */
219717 sqlite3_stmt *pIdxIter; /* Index iterator */
219718 int nTblCol; /* Size of azTblCol[] array */
219719 char **azTblCol; /* Array of unquoted target column names */
219720 char **azTblType; /* Array of target column types */
219721 int *aiSrcOrder; /* src table col -> target table col */
219722 u8 *abTblPk; /* Array of flags, set on target PK columns */
219723 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
219724 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
219725 int eType; /* Table type - an RBU_PK_XXX value */
219726
219727 /* Output variables. zTbl==0 implies EOF. */
219728 int bCleanup; /* True in "cleanup" state */
219729 const char *zTbl; /* Name of target db table */
219730 const char *zDataTbl; /* Name of rbu db table (or null) */
219731 const char *zIdx; /* Name of target db index (or null) */
219732 int iTnum; /* Root page of current object */
219733 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
219734 int bUnique; /* Current index is unique */
219735 int nIndex; /* Number of aux. indexes on table zTbl */
219736
219737 /* Statements created by rbuObjIterPrepareAll() */
219738 int nCol; /* Number of columns in current object */
219739 sqlite3_stmt *pSelect; /* Source data */
219740 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
219741 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
219742 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
219743 int nIdxCol;
219744 RbuSpan *aIdxCol;
219745 char *zIdxSql;
219746
219747 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
219748 RbuUpdateStmt *pRbuUpdate;
219749};
219750
219751/*
219752** Values for RbuObjIter.eType
219753**
219754** 0: Table does not exist (error)
219755** 1: Table has an implicit rowid.
219756** 2: Table has an explicit IPK column.
219757** 3: Table has an external PK index.
219758** 4: Table is WITHOUT ROWID.
219759** 5: Table is a virtual table.
219760*/
219761#define RBU_PK_NOTABLE 0
219762#define RBU_PK_NONE 1
219763#define RBU_PK_IPK 2
219764#define RBU_PK_EXTERNAL 3
219765#define RBU_PK_WITHOUT_ROWID 4
219766#define RBU_PK_VTAB 5
219767
219768
219769/*
219770** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
219771** one of the following operations.
219772*/
219773#define RBU_INSERT 1 /* Insert on a main table b-tree */
219774#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
219775#define RBU_REPLACE 3 /* Delete and then insert a row */
219776#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
219777#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
219778
219779#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
219780
219781/*
219782** A single step of an incremental checkpoint - frame iWalFrame of the wal
219783** file should be copied to page iDbPage of the database file.
219784*/
219785struct RbuFrame {
219786 u32 iDbPage;
219787 u32 iWalFrame;
219788};
219789
219790#ifndef UNUSED_PARAMETER
219791/*
219792** The following macros are used to suppress compiler warnings and to
219793** make it clear to human readers when a function parameter is deliberately
219794** left unused within the body of a function. This usually happens when
219795** a function is called via a function pointer. For example the
219796** implementation of an SQL aggregate step callback may not use the
219797** parameter indicating the number of arguments passed to the aggregate,
219798** if it knows that this is enforced elsewhere.
219799**
219800** When a function parameter is not used at all within the body of a function,
219801** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
219802** However, these macros may also be used to suppress warnings related to
219803** parameters that may or may not be used depending on compilation options.
219804** For example those parameters only used in assert() statements. In these
219805** cases the parameters are named as per the usual conventions.
219806*/
219807#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
219808#define UNUSED_PARAMETER2(x,y)(void)(x),(void)(y) UNUSED_PARAMETER(x)(void)(x),UNUSED_PARAMETER(y)(void)(y)
219809#endif
219810
219811/*
219812** RBU handle.
219813**
219814** nPhaseOneStep:
219815** If the RBU database contains an rbu_count table, this value is set to
219816** a running estimate of the number of b-tree operations required to
219817** finish populating the *-oal file. This allows the sqlite3_bp_progress()
219818** API to calculate the permyriadage progress of populating the *-oal file
219819** using the formula:
219820**
219821** permyriadage = (10000 * nProgress) / nPhaseOneStep
219822**
219823** nPhaseOneStep is initialized to the sum of:
219824**
219825** nRow * (nIndex + 1)
219826**
219827** for all source tables in the RBU database, where nRow is the number
219828** of rows in the source table and nIndex the number of indexes on the
219829** corresponding target database table.
219830**
219831** This estimate is accurate if the RBU update consists entirely of
219832** INSERT operations. However, it is inaccurate if:
219833**
219834** * the RBU update contains any UPDATE operations. If the PK specified
219835** for an UPDATE operation does not exist in the target table, then
219836** no b-tree operations are required on index b-trees. Or if the
219837** specified PK does exist, then (nIndex*2) such operations are
219838** required (one delete and one insert on each index b-tree).
219839**
219840** * the RBU update contains any DELETE operations for which the specified
219841** PK does not exist. In this case no operations are required on index
219842** b-trees.
219843**
219844** * the RBU update contains REPLACE operations. These are similar to
219845** UPDATE operations.
219846**
219847** nPhaseOneStep is updated to account for the conditions above during the
219848** first pass of each source table. The updated nPhaseOneStep value is
219849** stored in the rbu_state table if the RBU update is suspended.
219850*/
219851struct sqlite3rbu {
219852 int eStage; /* Value of RBU_STATE_STAGE field */
219853 sqlite3 *dbMain; /* target database handle */
219854 sqlite3 *dbRbu; /* rbu database handle */
219855 char *zTarget; /* Path to target db */
219856 char *zRbu; /* Path to rbu db */
219857 char *zState; /* Path to state db (or NULL if zRbu) */
219858 char zStateDb[5]; /* Db name for state ("stat" or "main") */
219859 int rc; /* Value returned by last rbu_step() call */
219860 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
219861 int nStep; /* Rows processed for current object */
219862 sqlite3_int64 nProgress; /* Rows processed for all objects */
219863 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
219864 const char *zVfsName; /* Name of automatically created rbu vfs */
219865 rbu_file *pTargetFd; /* File handle open on target db */
219866 int nPagePerSector; /* Pages per sector for pTargetFd */
219867 i64 iOalSz;
219868 i64 nPhaseOneStep;
219869 void *pRenameArg;
219870 int (*xRename)(void*, const char*, const char*);
219871
219872 /* The following state variables are used as part of the incremental
219873 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
219874 ** function rbuSetupCheckpoint() for details. */
219875 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
219876 u32 mLock;
219877 int nFrame; /* Entries in aFrame[] array */
219878 int nFrameAlloc; /* Allocated size of aFrame[] array */
219879 RbuFrame *aFrame;
219880 int pgsz;
219881 u8 *aBuf;
219882 i64 iWalCksum;
219883 i64 szTemp; /* Current size of all temp files in use */
219884 i64 szTempLimit; /* Total size limit for temp files */
219885
219886 /* Used in RBU vacuum mode only */
219887 int nRbu; /* Number of RBU VFS in the stack */
219888 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
219889};
219890
219891/*
219892** An rbu VFS is implemented using an instance of this structure.
219893**
219894** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
219895** It is NULL for RBU VFS objects created explicitly using
219896** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
219897** space used by the RBU handle.
219898*/
219899struct rbu_vfs {
219900 sqlite3_vfs base; /* rbu VFS shim methods */
219901 sqlite3_vfs *pRealVfs; /* Underlying VFS */
219902 sqlite3_mutex *mutex; /* Mutex to protect pMain */
219903 sqlite3rbu *pRbu; /* Owner RBU object */
219904 rbu_file *pMain; /* List of main db files */
219905 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
219906};
219907
219908/*
219909** Each file opened by an rbu VFS is represented by an instance of
219910** the following structure.
219911**
219912** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
219913** "sz" is set to the current size of the database file.
219914*/
219915struct rbu_file {
219916 sqlite3_file base; /* sqlite3_file methods */
219917 sqlite3_file *pReal; /* Underlying file handle */
219918 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
219919 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
219920 i64 sz; /* Size of file in bytes (temp only) */
219921
219922 int openFlags; /* Flags this file was opened with */
219923 u32 iCookie; /* Cookie value for main db files */
219924 u8 iWriteVer; /* "write-version" value for main db files */
219925 u8 bNolock; /* True to fail EXCLUSIVE locks */
219926
219927 int nShm; /* Number of entries in apShm[] array */
219928 char **apShm; /* Array of mmap'd *-shm regions */
219929 char *zDel; /* Delete this when closing file */
219930
219931 const char *zWal; /* Wal filename for this main db file */
219932 rbu_file *pWalFd; /* Wal file descriptor for this main db */
219933 rbu_file *pMainNext; /* Next MAIN_DB file */
219934 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
219935};
219936
219937/*
219938** True for an RBU vacuum handle, or false otherwise.
219939*/
219940#define rbuIsVacuum(p) ((p)->zTarget==0)
219941
219942
219943/*************************************************************************
219944** The following three functions, found below:
219945**
219946** rbuDeltaGetInt()
219947** rbuDeltaChecksum()
219948** rbuDeltaApply()
219949**
219950** are lifted from the fossil source code (http://fossil-scm.org). They
219951** are used to implement the scalar SQL function rbu_fossil_delta().
219952*/
219953
219954/*
219955** Read bytes from *pz and convert them into a positive integer. When
219956** finished, leave *pz pointing to the first character past the end of
219957** the integer. The *pLen parameter holds the length of the string
219958** in *pz and is decremented once for each character in the integer.
219959*/
219960static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
219961 static const signed char zValue[] = {
219962 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219963 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219964 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219965 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
219966 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
219967 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
219968 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
219969 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
219970 };
219971 unsigned int v = 0;
219972 int c;
219973 unsigned char *z = (unsigned char*)*pz;
219974 unsigned char *zStart = z;
219975 while( (c = zValue[0x7f&*(z++)])>=0 ){
219976 v = (v<<6) + c;
219977 }
219978 z--;
219979 *pLen -= (int)(z - zStart);
219980 *pz = (char*)z;
219981 return v;
219982}
219983
219984#if RBU_ENABLE_DELTA_CKSUM
219985/*
219986** Compute a 32-bit checksum on the N-byte buffer. Return the result.
219987*/
219988static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
219989 const unsigned char *z = (const unsigned char *)zIn;
219990 unsigned sum0 = 0;
219991 unsigned sum1 = 0;
219992 unsigned sum2 = 0;
219993 unsigned sum3 = 0;
219994 while(N >= 16){
219995 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
219996 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
219997 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
219998 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
219999 z += 16;
220000 N -= 16;
220001 }
220002 while(N >= 4){
220003 sum0 += z[0];
220004 sum1 += z[1];
220005 sum2 += z[2];
220006 sum3 += z[3];
220007 z += 4;
220008 N -= 4;
220009 }
220010 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
220011 switch(N){
220012 case 3: sum3 += (z[2] << 8);
220013 case 2: sum3 += (z[1] << 16);
220014 case 1: sum3 += (z[0] << 24);
220015 default: ;
220016 }
220017 return sum3;
220018}
220019#endif
220020
220021/*
220022** Apply a delta.
220023**
220024** The output buffer should be big enough to hold the whole output
220025** file and a NUL terminator at the end. The delta_output_size()
220026** routine will determine this size for you.
220027**
220028** The delta string should be null-terminated. But the delta string
220029** may contain embedded NUL characters (if the input and output are
220030** binary files) so we also have to pass in the length of the delta in
220031** the lenDelta parameter.
220032**
220033** This function returns the size of the output file in bytes (excluding
220034** the final NUL terminator character). Except, if the delta string is
220035** malformed or intended for use with a source file other than zSrc,
220036** then this routine returns -1.
220037**
220038** Refer to the delta_create() documentation above for a description
220039** of the delta file format.
220040*/
220041static int rbuDeltaApply(
220042 const char *zSrc, /* The source or pattern file */
220043 int lenSrc, /* Length of the source file */
220044 const char *zDelta, /* Delta to apply to the pattern */
220045 int lenDelta, /* Length of the delta */
220046 char *zOut /* Write the output into this preallocated buffer */
220047){
220048 unsigned int limit;
220049 unsigned int total = 0;
220050#if RBU_ENABLE_DELTA_CKSUM
220051 char *zOrigOut = zOut;
220052#endif
220053
220054 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
220055 if( *zDelta!='\n' ){
220056 /* ERROR: size integer not terminated by "\n" */
220057 return -1;
220058 }
220059 zDelta++; lenDelta--;
220060 while( *zDelta && lenDelta>0 ){
220061 unsigned int cnt, ofst;
220062 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
220063 switch( zDelta[0] ){
220064 case '@': {
220065 zDelta++; lenDelta--;
220066 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
220067 if( lenDelta>0 && zDelta[0]!=',' ){
220068 /* ERROR: copy command not terminated by ',' */
220069 return -1;
220070 }
220071 zDelta++; lenDelta--;
220072 total += cnt;
220073 if( total>limit ){
220074 /* ERROR: copy exceeds output file size */
220075 return -1;
220076 }
220077 if( (int)(ofst+cnt) > lenSrc ){
220078 /* ERROR: copy extends past end of input */
220079 return -1;
220080 }
220081 memcpy(zOut, &zSrc[ofst], cnt);
220082 zOut += cnt;
220083 break;
220084 }
220085 case ':': {
220086 zDelta++; lenDelta--;
220087 total += cnt;
220088 if( total>limit ){
220089 /* ERROR: insert command gives an output larger than predicted */
220090 return -1;
220091 }
220092 if( (int)cnt>lenDelta ){
220093 /* ERROR: insert count exceeds size of delta */
220094 return -1;
220095 }
220096 memcpy(zOut, zDelta, cnt);
220097 zOut += cnt;
220098 zDelta += cnt;
220099 lenDelta -= cnt;
220100 break;
220101 }
220102 case ';': {
220103 zDelta++; lenDelta--;
220104 zOut[0] = 0;
220105#if RBU_ENABLE_DELTA_CKSUM
220106 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
220107 /* ERROR: bad checksum */
220108 return -1;
220109 }
220110#endif
220111 if( total!=limit ){
220112 /* ERROR: generated size does not match predicted size */
220113 return -1;
220114 }
220115 return total;
220116 }
220117 default: {
220118 /* ERROR: unknown delta operator */
220119 return -1;
220120 }
220121 }
220122 }
220123 /* ERROR: unterminated delta */
220124 return -1;
220125}
220126
220127static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
220128 int size;
220129 size = rbuDeltaGetInt(&zDelta, &lenDelta);
220130 if( *zDelta!='\n' ){
220131 /* ERROR: size integer not terminated by "\n" */
220132 return -1;
220133 }
220134 return size;
220135}
220136
220137/*
220138** End of code taken from fossil.
220139*************************************************************************/
220140
220141/*
220142** Implementation of SQL scalar function rbu_fossil_delta().
220143**
220144** This function applies a fossil delta patch to a blob. Exactly two
220145** arguments must be passed to this function. The first is the blob to
220146** patch and the second the patch to apply. If no error occurs, this
220147** function returns the patched blob.
220148*/
220149static void rbuFossilDeltaFunc(
220150 sqlite3_context *context,
220151 int argc,
220152 sqlite3_value **argv
220153){
220154 const char *aDelta;
220155 int nDelta;
220156 const char *aOrig;
220157 int nOrig;
220158
220159 int nOut;
220160 int nOut2;
220161 char *aOut;
220162
220163 assert( argc==2 )((void) (0));
220164 UNUSED_PARAMETER(argc)(void)(argc);
220165
220166 nOrig = sqlite3_value_bytes(argv[0]);
220167 aOrig = (const char*)sqlite3_value_blob(argv[0]);
220168 nDelta = sqlite3_value_bytes(argv[1]);
220169 aDelta = (const char*)sqlite3_value_blob(argv[1]);
220170
220171 /* Figure out the size of the output */
220172 nOut = rbuDeltaOutputSize(aDelta, nDelta);
220173 if( nOut<0 ){
220174 sqlite3_result_error(context, "corrupt fossil delta", -1);
220175 return;
220176 }
220177
220178 aOut = sqlite3_malloc(nOut+1);
220179 if( aOut==0 ){
220180 sqlite3_result_error_nomem(context);
220181 }else{
220182 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
220183 if( nOut2!=nOut ){
220184 sqlite3_free(aOut);
220185 sqlite3_result_error(context, "corrupt fossil delta", -1);
220186 }else{
220187 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
220188 }
220189 }
220190}
220191
220192
220193/*
220194** Prepare the SQL statement in buffer zSql against database handle db.
220195** If successful, set *ppStmt to point to the new statement and return
220196** SQLITE_OK.
220197**
220198** Otherwise, if an error does occur, set *ppStmt to NULL and return
220199** an SQLite error code. Additionally, set output variable *pzErrmsg to
220200** point to a buffer containing an error message. It is the responsibility
220201** of the caller to (eventually) free this buffer using sqlite3_free().
220202*/
220203static int prepareAndCollectError(
220204 sqlite3 *db,
220205 sqlite3_stmt **ppStmt,
220206 char **pzErrmsg,
220207 const char *zSql
220208){
220209 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
220210 if( rc!=SQLITE_OK0 ){
220211 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
220212 *ppStmt = 0;
220213 }
220214 return rc;
220215}
220216
220217/*
220218** Reset the SQL statement passed as the first argument. Return a copy
220219** of the value returned by sqlite3_reset().
220220**
220221** If an error has occurred, then set *pzErrmsg to point to a buffer
220222** containing an error message. It is the responsibility of the caller
220223** to eventually free this buffer using sqlite3_free().
220224*/
220225static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
220226 int rc = sqlite3_reset(pStmt);
220227 if( rc!=SQLITE_OK0 ){
220228 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
220229 }
220230 return rc;
220231}
220232
220233/*
220234** Unless it is NULL, argument zSql points to a buffer allocated using
220235** sqlite3_malloc containing an SQL statement. This function prepares the SQL
220236** statement against database db and frees the buffer. If statement
220237** compilation is successful, *ppStmt is set to point to the new statement
220238** handle and SQLITE_OK is returned.
220239**
220240** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
220241** returned. In this case, *pzErrmsg may also be set to point to an error
220242** message. It is the responsibility of the caller to free this error message
220243** buffer using sqlite3_free().
220244**
220245** If argument zSql is NULL, this function assumes that an OOM has occurred.
220246** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
220247*/
220248static int prepareFreeAndCollectError(
220249 sqlite3 *db,
220250 sqlite3_stmt **ppStmt,
220251 char **pzErrmsg,
220252 char *zSql
220253){
220254 int rc;
220255 assert( *pzErrmsg==0 )((void) (0));
220256 if( zSql==0 ){
220257 rc = SQLITE_NOMEM7;
220258 *ppStmt = 0;
220259 }else{
220260 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
220261 sqlite3_free(zSql);
220262 }
220263 return rc;
220264}
220265
220266/*
220267** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
220268** by an earlier call to rbuObjIterCacheTableInfo().
220269*/
220270static void rbuObjIterFreeCols(RbuObjIter *pIter){
220271 int i;
220272 for(i=0; i<pIter->nTblCol; i++){
220273 sqlite3_free(pIter->azTblCol[i]);
220274 sqlite3_free(pIter->azTblType[i]);
220275 }
220276 sqlite3_free(pIter->azTblCol);
220277 pIter->azTblCol = 0;
220278 pIter->azTblType = 0;
220279 pIter->aiSrcOrder = 0;
220280 pIter->abTblPk = 0;
220281 pIter->abNotNull = 0;
220282 pIter->nTblCol = 0;
220283 pIter->eType = 0; /* Invalid value */
220284}
220285
220286/*
220287** Finalize all statements and free all allocations that are specific to
220288** the current object (table/index pair).
220289*/
220290static void rbuObjIterClearStatements(RbuObjIter *pIter){
220291 RbuUpdateStmt *pUp;
220292
220293 sqlite3_finalize(pIter->pSelect);
220294 sqlite3_finalize(pIter->pInsert);
220295 sqlite3_finalize(pIter->pDelete);
220296 sqlite3_finalize(pIter->pTmpInsert);
220297 pUp = pIter->pRbuUpdate;
220298 while( pUp ){
220299 RbuUpdateStmt *pTmp = pUp->pNext;
220300 sqlite3_finalize(pUp->pUpdate);
220301 sqlite3_free(pUp);
220302 pUp = pTmp;
220303 }
220304 sqlite3_free(pIter->aIdxCol);
220305 sqlite3_free(pIter->zIdxSql);
220306
220307 pIter->pSelect = 0;
220308 pIter->pInsert = 0;
220309 pIter->pDelete = 0;
220310 pIter->pRbuUpdate = 0;
220311 pIter->pTmpInsert = 0;
220312 pIter->nCol = 0;
220313 pIter->nIdxCol = 0;
220314 pIter->aIdxCol = 0;
220315 pIter->zIdxSql = 0;
220316}
220317
220318/*
220319** Clean up any resources allocated as part of the iterator object passed
220320** as the only argument.
220321*/
220322static void rbuObjIterFinalize(RbuObjIter *pIter){
220323 rbuObjIterClearStatements(pIter);
220324 sqlite3_finalize(pIter->pTblIter);
220325 sqlite3_finalize(pIter->pIdxIter);
220326 rbuObjIterFreeCols(pIter);
220327 memset(pIter, 0, sizeof(RbuObjIter));
220328}
220329
220330/*
220331** Advance the iterator to the next position.
220332**
220333** If no error occurs, SQLITE_OK is returned and the iterator is left
220334** pointing to the next entry. Otherwise, an error code and message is
220335** left in the RBU handle passed as the first argument. A copy of the
220336** error code is returned.
220337*/
220338static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
220339 int rc = p->rc;
220340 if( rc==SQLITE_OK0 ){
220341
220342 /* Free any SQLite statements used while processing the previous object */
220343 rbuObjIterClearStatements(pIter);
220344 if( pIter->zIdx==0 ){
220345 rc = sqlite3_exec(p->dbMain,
220346 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
220347 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
220348 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
220349 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
220350 , 0, 0, &p->zErrmsg
220351 );
220352 }
220353
220354 if( rc==SQLITE_OK0 ){
220355 if( pIter->bCleanup ){
220356 rbuObjIterFreeCols(pIter);
220357 pIter->bCleanup = 0;
220358 rc = sqlite3_step(pIter->pTblIter);
220359 if( rc!=SQLITE_ROW100 ){
220360 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
220361 pIter->zTbl = 0;
220362 pIter->zDataTbl = 0;
220363 }else{
220364 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
220365 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
220366 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK0 : SQLITE_NOMEM7;
220367 }
220368 }else{
220369 if( pIter->zIdx==0 ){
220370 sqlite3_stmt *pIdx = pIter->pIdxIter;
220371 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
220372 }
220373 if( rc==SQLITE_OK0 ){
220374 rc = sqlite3_step(pIter->pIdxIter);
220375 if( rc!=SQLITE_ROW100 ){
220376 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
220377 pIter->bCleanup = 1;
220378 pIter->zIdx = 0;
220379 }else{
220380 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
220381 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
220382 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
220383 rc = pIter->zIdx ? SQLITE_OK0 : SQLITE_NOMEM7;
220384 }
220385 }
220386 }
220387 }
220388 }
220389
220390 if( rc!=SQLITE_OK0 ){
220391 rbuObjIterFinalize(pIter);
220392 p->rc = rc;
220393 }
220394 return rc;
220395}
220396
220397
220398/*
220399** The implementation of the rbu_target_name() SQL function. This function
220400** accepts one or two arguments. The first argument is the name of a table -
220401** the name of a table in the RBU database. The second, if it is present, is 1
220402** for a view or 0 for a table.
220403**
220404** For a non-vacuum RBU handle, if the table name matches the pattern:
220405**
220406** data[0-9]_<name>
220407**
220408** where <name> is any sequence of 1 or more characters, <name> is returned.
220409** Otherwise, if the only argument does not match the above pattern, an SQL
220410** NULL is returned.
220411**
220412** "data_t1" -> "t1"
220413** "data0123_t2" -> "t2"
220414** "dataAB_t3" -> NULL
220415**
220416** For an rbu vacuum handle, a copy of the first argument is returned if
220417** the second argument is either missing or 0 (not a view).
220418*/
220419static void rbuTargetNameFunc(
220420 sqlite3_context *pCtx,
220421 int argc,
220422 sqlite3_value **argv
220423){
220424 sqlite3rbu *p = sqlite3_user_data(pCtx);
220425 const char *zIn;
220426 assert( argc==1 || argc==2 )((void) (0));
220427
220428 zIn = (const char*)sqlite3_value_text(argv[0]);
220429 if( zIn ){
220430 if( rbuIsVacuum(p) ){
220431 assert( argc==2 || argc==1 )((void) (0));
220432 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
220433 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
220434 }
220435 }else{
220436 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
220437 int i;
220438 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
220439 if( zIn[i]=='_' && zIn[i+1] ){
220440 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
220441 }
220442 }
220443 }
220444 }
220445}
220446
220447/*
220448** Initialize the iterator structure passed as the second argument.
220449**
220450** If no error occurs, SQLITE_OK is returned and the iterator is left
220451** pointing to the first entry. Otherwise, an error code and message is
220452** left in the RBU handle passed as the first argument. A copy of the
220453** error code is returned.
220454*/
220455static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
220456 int rc;
220457 memset(pIter, 0, sizeof(RbuObjIter));
220458
220459 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
220460 sqlite3_mprintf(
220461 "SELECT rbu_target_name(name, type='view') AS target, name "
220462 "FROM sqlite_schema "
220463 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
220464 " %s "
220465 "ORDER BY name"
220466 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
220467
220468 if( rc==SQLITE_OK0 ){
220469 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
220470 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
220471 " FROM main.sqlite_schema "
220472 " WHERE type='index' AND tbl_name = ?"
220473 );
220474 }
220475
220476 pIter->bCleanup = 1;
220477 p->rc = rc;
220478 return rbuObjIterNext(p, pIter);
220479}
220480
220481/*
220482** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
220483** an error code is stored in the RBU handle passed as the first argument.
220484**
220485** If an error has already occurred (p->rc is already set to something other
220486** than SQLITE_OK), then this function returns NULL without modifying the
220487** stored error code. In this case it still calls sqlite3_free() on any
220488** printf() parameters associated with %z conversions.
220489*/
220490static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
220491 char *zSql = 0;
220492 va_list ap;
220493 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
220494 zSql = sqlite3_vmprintf(zFmt, ap);
220495 if( p->rc==SQLITE_OK0 ){
220496 if( zSql==0 ) p->rc = SQLITE_NOMEM7;
220497 }else{
220498 sqlite3_free(zSql);
220499 zSql = 0;
220500 }
220501 va_end(ap)__builtin_va_end(ap);
220502 return zSql;
220503}
220504
220505/*
220506** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
220507** arguments are the usual subsitution values. This function performs
220508** the printf() style substitutions and executes the result as an SQL
220509** statement on the RBU handles database.
220510**
220511** If an error occurs, an error code and error message is stored in the
220512** RBU handle. If an error has already occurred when this function is
220513** called, it is a no-op.
220514*/
220515static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
220516 va_list ap;
220517 char *zSql;
220518 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
220519 zSql = sqlite3_vmprintf(zFmt, ap);
220520 if( p->rc==SQLITE_OK0 ){
220521 if( zSql==0 ){
220522 p->rc = SQLITE_NOMEM7;
220523 }else{
220524 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
220525 }
220526 }
220527 sqlite3_free(zSql);
220528 va_end(ap)__builtin_va_end(ap);
220529 return p->rc;
220530}
220531
220532/*
220533** Attempt to allocate and return a pointer to a zeroed block of nByte
220534** bytes.
220535**
220536** If an error (i.e. an OOM condition) occurs, return NULL and leave an
220537** error code in the rbu handle passed as the first argument. Or, if an
220538** error has already occurred when this function is called, return NULL
220539** immediately without attempting the allocation or modifying the stored
220540** error code.
220541*/
220542static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
220543 void *pRet = 0;
220544 if( p->rc==SQLITE_OK0 ){
220545 assert( nByte>0 )((void) (0));
220546 pRet = sqlite3_malloc64(nByte);
220547 if( pRet==0 ){
220548 p->rc = SQLITE_NOMEM7;
220549 }else{
220550 memset(pRet, 0, nByte);
220551 }
220552 }
220553 return pRet;
220554}
220555
220556
220557/*
220558** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
220559** there is room for at least nCol elements. If an OOM occurs, store an
220560** error code in the RBU handle passed as the first argument.
220561*/
220562static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
220563 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
220564 char **azNew;
220565
220566 azNew = (char**)rbuMalloc(p, nByte);
220567 if( azNew ){
220568 pIter->azTblCol = azNew;
220569 pIter->azTblType = &azNew[nCol];
220570 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
220571 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
220572 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
220573 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
220574 }
220575}
220576
220577/*
220578** The first argument must be a nul-terminated string. This function
220579** returns a copy of the string in memory obtained from sqlite3_malloc().
220580** It is the responsibility of the caller to eventually free this memory
220581** using sqlite3_free().
220582**
220583** If an OOM condition is encountered when attempting to allocate memory,
220584** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
220585** if the allocation succeeds, (*pRc) is left unchanged.
220586*/
220587static char *rbuStrndup(const char *zStr, int *pRc){
220588 char *zRet = 0;
220589
220590 if( *pRc==SQLITE_OK0 ){
220591 if( zStr ){
220592 size_t nCopy = strlen(zStr) + 1;
220593 zRet = (char*)sqlite3_malloc64(nCopy);
220594 if( zRet ){
220595 memcpy(zRet, zStr, nCopy);
220596 }else{
220597 *pRc = SQLITE_NOMEM7;
220598 }
220599 }
220600 }
220601
220602 return zRet;
220603}
220604
220605/*
220606** Finalize the statement passed as the second argument.
220607**
220608** If the sqlite3_finalize() call indicates that an error occurs, and the
220609** rbu handle error code is not already set, set the error code and error
220610** message accordingly.
220611*/
220612static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
220613 sqlite3 *db = sqlite3_db_handle(pStmt);
220614 int rc = sqlite3_finalize(pStmt);
220615 if( p->rc==SQLITE_OK0 && rc!=SQLITE_OK0 ){
220616 p->rc = rc;
220617 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
220618 }
220619}
220620
220621/* Determine the type of a table.
220622**
220623** peType is of type (int*), a pointer to an output parameter of type
220624** (int). This call sets the output parameter as follows, depending
220625** on the type of the table specified by parameters dbName and zTbl.
220626**
220627** RBU_PK_NOTABLE: No such table.
220628** RBU_PK_NONE: Table has an implicit rowid.
220629** RBU_PK_IPK: Table has an explicit IPK column.
220630** RBU_PK_EXTERNAL: Table has an external PK index.
220631** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
220632** RBU_PK_VTAB: Table is a virtual table.
220633**
220634** Argument *piPk is also of type (int*), and also points to an output
220635** parameter. Unless the table has an external primary key index
220636** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
220637** if the table does have an external primary key index, then *piPk
220638** is set to the root page number of the primary key index before
220639** returning.
220640**
220641** ALGORITHM:
220642**
220643** if( no entry exists in sqlite_schema ){
220644** return RBU_PK_NOTABLE
220645** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
220646** return RBU_PK_VTAB
220647** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
220648** if( the index that is the pk exists in sqlite_schema ){
220649** *piPK = rootpage of that index.
220650** return RBU_PK_EXTERNAL
220651** }else{
220652** return RBU_PK_WITHOUT_ROWID
220653** }
220654** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
220655** return RBU_PK_IPK
220656** }else{
220657** return RBU_PK_NONE
220658** }
220659*/
220660static void rbuTableType(
220661 sqlite3rbu *p,
220662 const char *zTab,
220663 int *peType,
220664 int *piTnum,
220665 int *piPk
220666){
220667 /*
220668 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
220669 ** 1) PRAGMA index_list = ?
220670 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
220671 ** 3) PRAGMA table_info = ?
220672 */
220673 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
220674
220675 *peType = RBU_PK_NOTABLE;
220676 *piPk = 0;
220677
220678 assert( p->rc==SQLITE_OK )((void) (0));
220679 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
220680 sqlite3_mprintf(
220681 "SELECT "
220682 " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
220683 " rootpage"
220684 " FROM sqlite_schema"
220685 " WHERE name=%Q", zTab
220686 ));
220687 if( p->rc!=SQLITE_OK0 || sqlite3_step(aStmt[0])!=SQLITE_ROW100 ){
220688 /* Either an error, or no such table. */
220689 goto rbuTableType_end;
220690 }
220691 if( sqlite3_column_int(aStmt[0], 0) ){
220692 *peType = RBU_PK_VTAB; /* virtual table */
220693 goto rbuTableType_end;
220694 }
220695 *piTnum = sqlite3_column_int(aStmt[0], 1);
220696
220697 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
220698 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
220699 );
220700 if( p->rc ) goto rbuTableType_end;
220701 while( sqlite3_step(aStmt[1])==SQLITE_ROW100 ){
220702 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
220703 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
220704 if( zOrig && zIdx && zOrig[0]=='p' ){
220705 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
220706 sqlite3_mprintf(
220707 "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
220708 ));
220709 if( p->rc==SQLITE_OK0 ){
220710 if( sqlite3_step(aStmt[2])==SQLITE_ROW100 ){
220711 *piPk = sqlite3_column_int(aStmt[2], 0);
220712 *peType = RBU_PK_EXTERNAL;
220713 }else{
220714 *peType = RBU_PK_WITHOUT_ROWID;
220715 }
220716 }
220717 goto rbuTableType_end;
220718 }
220719 }
220720
220721 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
220722 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
220723 );
220724 if( p->rc==SQLITE_OK0 ){
220725 while( sqlite3_step(aStmt[3])==SQLITE_ROW100 ){
220726 if( sqlite3_column_int(aStmt[3],5)>0 ){
220727 *peType = RBU_PK_IPK; /* explicit IPK column */
220728 goto rbuTableType_end;
220729 }
220730 }
220731 *peType = RBU_PK_NONE;
220732 }
220733
220734rbuTableType_end: {
220735 unsigned int i;
220736 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
220737 rbuFinalize(p, aStmt[i]);
220738 }
220739 }
220740}
220741
220742/*
220743** This is a helper function for rbuObjIterCacheTableInfo(). It populates
220744** the pIter->abIndexed[] array.
220745*/
220746static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
220747 sqlite3_stmt *pList = 0;
220748 int bIndex = 0;
220749
220750 if( p->rc==SQLITE_OK0 ){
220751 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
220752 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
220753 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
220754 );
220755 }
220756
220757 pIter->nIndex = 0;
220758 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pList) ){
220759 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
220760 int bPartial = sqlite3_column_int(pList, 4);
220761 sqlite3_stmt *pXInfo = 0;
220762 if( zIdx==0 ) break;
220763 if( bPartial ){
220764 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
220765 }
220766 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
220767 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
220768 );
220769 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
220770 int iCid = sqlite3_column_int(pXInfo, 1);
220771 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
220772 if( iCid==-2 ){
220773 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
220774 }
220775 }
220776 rbuFinalize(p, pXInfo);
220777 bIndex = 1;
220778 pIter->nIndex++;
220779 }
220780
220781 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
220782 /* "PRAGMA index_list" includes the main PK b-tree */
220783 pIter->nIndex--;
220784 }
220785
220786 rbuFinalize(p, pList);
220787 if( bIndex==0 ) pIter->abIndexed = 0;
220788}
220789
220790
220791/*
220792** If they are not already populated, populate the pIter->azTblCol[],
220793** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
220794** the table (not index) that the iterator currently points to.
220795**
220796** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
220797** an error does occur, an error code and error message are also left in
220798** the RBU handle.
220799*/
220800static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
220801 if( pIter->azTblCol==0 ){
220802 sqlite3_stmt *pStmt = 0;
220803 int nCol = 0;
220804 int i; /* for() loop iterator variable */
220805 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
220806 int iOrder = 0;
220807 int iTnum = 0;
220808
220809 /* Figure out the type of table this step will deal with. */
220810 assert( pIter->eType==0 )((void) (0));
220811 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
220812 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_NOTABLE ){
220813 p->rc = SQLITE_ERROR1;
220814 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
220815 }
220816 if( p->rc ) return p->rc;
220817 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
220818
220819 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK((void) (0))
220820 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID((void) (0))
220821 || pIter->eType==RBU_PK_VTAB((void) (0))
220822 )((void) (0));
220823
220824 /* Populate the azTblCol[] and nTblCol variables based on the columns
220825 ** of the input table. Ignore any input table columns that begin with
220826 ** "rbu_". */
220827 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
220828 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
220829 );
220830 if( p->rc==SQLITE_OK0 ){
220831 nCol = sqlite3_column_count(pStmt);
220832 rbuAllocateIterArrays(p, pIter, nCol);
220833 }
220834 for(i=0; p->rc==SQLITE_OK0 && i<nCol; i++){
220835 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
220836 if( sqlite3_strnicmp("rbu_", zName, 4) ){
220837 char *zCopy = rbuStrndup(zName, &p->rc);
220838 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
220839 pIter->azTblCol[pIter->nTblCol++] = zCopy;
220840 }
220841 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
220842 bRbuRowid = 1;
220843 }
220844 }
220845 sqlite3_finalize(pStmt);
220846 pStmt = 0;
220847
220848 if( p->rc==SQLITE_OK0
220849 && rbuIsVacuum(p)==0
220850 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
220851 ){
220852 p->rc = SQLITE_ERROR1;
220853 p->zErrmsg = sqlite3_mprintf(
220854 "table %q %s rbu_rowid column", pIter->zDataTbl,
220855 (bRbuRowid ? "may not have" : "requires")
220856 );
220857 }
220858
220859 /* Check that all non-HIDDEN columns in the destination table are also
220860 ** present in the input table. Populate the abTblPk[], azTblType[] and
220861 ** aiTblOrder[] arrays at the same time. */
220862 if( p->rc==SQLITE_OK0 ){
220863 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
220864 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
220865 );
220866 }
220867 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
220868 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
220869 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
220870 for(i=iOrder; i<pIter->nTblCol; i++){
220871 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
220872 }
220873 if( i==pIter->nTblCol ){
220874 p->rc = SQLITE_ERROR1;
220875 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
220876 pIter->zDataTbl, zName
220877 );
220878 }else{
220879 int iPk = sqlite3_column_int(pStmt, 5);
220880 int bNotNull = sqlite3_column_int(pStmt, 3);
220881 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
220882
220883 if( i!=iOrder ){
220884 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]){int t=pIter->aiSrcOrder[i]; pIter->aiSrcOrder[i]=pIter
->aiSrcOrder[iOrder]; pIter->aiSrcOrder[iOrder]=t;}
;
220885 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]){char* t=pIter->azTblCol[i]; pIter->azTblCol[i]=pIter->
azTblCol[iOrder]; pIter->azTblCol[iOrder]=t;}
;
220886 }
220887
220888 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
220889 assert( iPk>=0 )((void) (0));
220890 pIter->abTblPk[iOrder] = (u8)iPk;
220891 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
220892 iOrder++;
220893 }
220894 }
220895
220896 rbuFinalize(p, pStmt);
220897 rbuObjIterCacheIndexedCols(p, pIter);
220898 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 )((void) (0));
220899 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 )((void) (0));
220900 }
220901
220902 return p->rc;
220903}
220904
220905/*
220906** This function constructs and returns a pointer to a nul-terminated
220907** string containing some SQL clause or list based on one or more of the
220908** column names currently stored in the pIter->azTblCol[] array.
220909*/
220910static char *rbuObjIterGetCollist(
220911 sqlite3rbu *p, /* RBU object */
220912 RbuObjIter *pIter /* Object iterator for column names */
220913){
220914 char *zList = 0;
220915 const char *zSep = "";
220916 int i;
220917 for(i=0; i<pIter->nTblCol; i++){
220918 const char *z = pIter->azTblCol[i];
220919 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
220920 zSep = ", ";
220921 }
220922 return zList;
220923}
220924
220925/*
220926** Return a comma separated list of the quoted PRIMARY KEY column names,
220927** in order, for the current table. Before each column name, add the text
220928** zPre. After each column name, add the zPost text. Use zSeparator as
220929** the separator text (usually ", ").
220930*/
220931static char *rbuObjIterGetPkList(
220932 sqlite3rbu *p, /* RBU object */
220933 RbuObjIter *pIter, /* Object iterator for column names */
220934 const char *zPre, /* Before each quoted column name */
220935 const char *zSeparator, /* Separator to use between columns */
220936 const char *zPost /* After each quoted column name */
220937){
220938 int iPk = 1;
220939 char *zRet = 0;
220940 const char *zSep = "";
220941 while( 1 ){
220942 int i;
220943 for(i=0; i<pIter->nTblCol; i++){
220944 if( (int)pIter->abTblPk[i]==iPk ){
220945 const char *zCol = pIter->azTblCol[i];
220946 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
220947 zSep = zSeparator;
220948 break;
220949 }
220950 }
220951 if( i==pIter->nTblCol ) break;
220952 iPk++;
220953 }
220954 return zRet;
220955}
220956
220957/*
220958** This function is called as part of restarting an RBU vacuum within
220959** stage 1 of the process (while the *-oal file is being built) while
220960** updating a table (not an index). The table may be a rowid table or
220961** a WITHOUT ROWID table. It queries the target database to find the
220962** largest key that has already been written to the target table and
220963** constructs a WHERE clause that can be used to extract the remaining
220964** rows from the source table. For a rowid table, the WHERE clause
220965** is of the form:
220966**
220967** "WHERE _rowid_ > ?"
220968**
220969** and for WITHOUT ROWID tables:
220970**
220971** "WHERE (key1, key2) > (?, ?)"
220972**
220973** Instead of "?" placeholders, the actual WHERE clauses created by
220974** this function contain literal SQL values.
220975*/
220976static char *rbuVacuumTableStart(
220977 sqlite3rbu *p, /* RBU handle */
220978 RbuObjIter *pIter, /* RBU iterator object */
220979 int bRowid, /* True for a rowid table */
220980 const char *zWrite /* Target table name prefix */
220981){
220982 sqlite3_stmt *pMax = 0;
220983 char *zRet = 0;
220984 if( bRowid ){
220985 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
220986 sqlite3_mprintf(
220987 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
220988 )
220989 );
220990 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
220991 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
220992 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
220993 }
220994 rbuFinalize(p, pMax);
220995 }else{
220996 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
220997 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
220998 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
220999
221000 if( p->rc==SQLITE_OK0 ){
221001 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
221002 sqlite3_mprintf(
221003 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
221004 zSelect, zWrite, pIter->zTbl, zOrder
221005 )
221006 );
221007 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
221008 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
221009 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
221010 }
221011 rbuFinalize(p, pMax);
221012 }
221013
221014 sqlite3_free(zOrder);
221015 sqlite3_free(zSelect);
221016 sqlite3_free(zList);
221017 }
221018 return zRet;
221019}
221020
221021/*
221022** This function is called as part of restating an RBU vacuum when the
221023** current operation is writing content to an index. If possible, it
221024** queries the target index b-tree for the largest key already written to
221025** it, then composes and returns an expression that can be used in a WHERE
221026** clause to select the remaining required rows from the source table.
221027** It is only possible to return such an expression if:
221028**
221029** * The index contains no DESC columns, and
221030** * The last key written to the index before the operation was
221031** suspended does not contain any NULL values.
221032**
221033** The expression is of the form:
221034**
221035** (index-field1, index-field2, ...) > (?, ?, ...)
221036**
221037** except that the "?" placeholders are replaced with literal values.
221038**
221039** If the expression cannot be created, NULL is returned. In this case,
221040** the caller has to use an OFFSET clause to extract only the required
221041** rows from the sourct table, just as it does for an RBU update operation.
221042*/
221043static char *rbuVacuumIndexStart(
221044 sqlite3rbu *p, /* RBU handle */
221045 RbuObjIter *pIter /* RBU iterator object */
221046){
221047 char *zOrder = 0;
221048 char *zLhs = 0;
221049 char *zSelect = 0;
221050 char *zVector = 0;
221051 char *zRet = 0;
221052 int bFailed = 0;
221053 const char *zSep = "";
221054 int iCol = 0;
221055 sqlite3_stmt *pXInfo = 0;
221056
221057 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
221058 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
221059 );
221060 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
221061 int iCid = sqlite3_column_int(pXInfo, 1);
221062 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
221063 const char *zCol;
221064 if( sqlite3_column_int(pXInfo, 3) ){
221065 bFailed = 1;
221066 break;
221067 }
221068
221069 if( iCid<0 ){
221070 if( pIter->eType==RBU_PK_IPK ){
221071 int i;
221072 for(i=0; pIter->abTblPk[i]==0; i++);
221073 assert( i<pIter->nTblCol )((void) (0));
221074 zCol = pIter->azTblCol[i];
221075 }else{
221076 zCol = "_rowid_";
221077 }
221078 }else{
221079 zCol = pIter->azTblCol[iCid];
221080 }
221081
221082 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
221083 zLhs, zSep, zCol, zCollate
221084 );
221085 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
221086 zOrder, zSep, iCol, zCol, zCollate
221087 );
221088 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
221089 zSelect, zSep, iCol, zCol
221090 );
221091 zSep = ", ";
221092 iCol++;
221093 }
221094 rbuFinalize(p, pXInfo);
221095 if( bFailed ) goto index_start_out;
221096
221097 if( p->rc==SQLITE_OK0 ){
221098 sqlite3_stmt *pSel = 0;
221099
221100 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
221101 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
221102 zSelect, pIter->zTbl, zOrder
221103 )
221104 );
221105 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pSel) ){
221106 zSep = "";
221107 for(iCol=0; iCol<pIter->nCol; iCol++){
221108 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
221109 if( zQuoted==0 ){
221110 p->rc = SQLITE_NOMEM7;
221111 }else if( zQuoted[0]=='N' ){
221112 bFailed = 1;
221113 break;
221114 }
221115 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
221116 zSep = ", ";
221117 }
221118
221119 if( !bFailed ){
221120 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
221121 }
221122 }
221123 rbuFinalize(p, pSel);
221124 }
221125
221126 index_start_out:
221127 sqlite3_free(zOrder);
221128 sqlite3_free(zSelect);
221129 sqlite3_free(zVector);
221130 sqlite3_free(zLhs);
221131 return zRet;
221132}
221133
221134/*
221135** This function is used to create a SELECT list (the list of SQL
221136** expressions that follows a SELECT keyword) for a SELECT statement
221137** used to read from an data_xxx or rbu_tmp_xxx table while updating the
221138** index object currently indicated by the iterator object passed as the
221139** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
221140** to obtain the required information.
221141**
221142** If the index is of the following form:
221143**
221144** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
221145**
221146** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
221147** "ipk", the returned string is:
221148**
221149** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
221150**
221151** As well as the returned string, three other malloc'd strings are
221152** returned via output parameters. As follows:
221153**
221154** pzImposterCols: ...
221155** pzImposterPk: ...
221156** pzWhere: ...
221157*/
221158static char *rbuObjIterGetIndexCols(
221159 sqlite3rbu *p, /* RBU object */
221160 RbuObjIter *pIter, /* Object iterator for column names */
221161 char **pzImposterCols, /* OUT: Columns for imposter table */
221162 char **pzImposterPk, /* OUT: Imposter PK clause */
221163 char **pzWhere, /* OUT: WHERE clause */
221164 int *pnBind /* OUT: Trbul number of columns */
221165){
221166 int rc = p->rc; /* Error code */
221167 int rc2; /* sqlite3_finalize() return code */
221168 char *zRet = 0; /* String to return */
221169 char *zImpCols = 0; /* String to return via *pzImposterCols */
221170 char *zImpPK = 0; /* String to return via *pzImposterPK */
221171 char *zWhere = 0; /* String to return via *pzWhere */
221172 int nBind = 0; /* Value to return via *pnBind */
221173 const char *zCom = ""; /* Set to ", " later on */
221174 const char *zAnd = ""; /* Set to " AND " later on */
221175 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
221176
221177 if( rc==SQLITE_OK0 ){
221178 assert( p->zErrmsg==0 )((void) (0));
221179 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
221180 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
221181 );
221182 }
221183
221184 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
221185 int iCid = sqlite3_column_int(pXInfo, 1);
221186 int bDesc = sqlite3_column_int(pXInfo, 3);
221187 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
221188 const char *zCol = 0;
221189 const char *zType;
221190
221191 if( iCid==-2 ){
221192 int iSeq = sqlite3_column_int(pXInfo, 0);
221193 zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
221194 pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
221195 );
221196 zType = "";
221197 }else {
221198 if( iCid<0 ){
221199 /* An integer primary key. If the table has an explicit IPK, use
221200 ** its name. Otherwise, use "rbu_rowid". */
221201 if( pIter->eType==RBU_PK_IPK ){
221202 int i;
221203 for(i=0; pIter->abTblPk[i]==0; i++);
221204 assert( i<pIter->nTblCol )((void) (0));
221205 zCol = pIter->azTblCol[i];
221206 }else if( rbuIsVacuum(p) ){
221207 zCol = "_rowid_";
221208 }else{
221209 zCol = "rbu_rowid";
221210 }
221211 zType = "INTEGER";
221212 }else{
221213 zCol = pIter->azTblCol[iCid];
221214 zType = pIter->azTblType[iCid];
221215 }
221216 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
221217 }
221218
221219 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
221220 const char *zOrder = (bDesc ? " DESC" : "");
221221 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
221222 zImpPK, zCom, nBind, zCol, zOrder
221223 );
221224 }
221225 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
221226 zImpCols, zCom, nBind, zCol, zType, zCollate
221227 );
221228 zWhere = sqlite3_mprintf(
221229 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
221230 );
221231 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM7;
221232 zCom = ", ";
221233 zAnd = " AND ";
221234 nBind++;
221235 }
221236
221237 rc2 = sqlite3_finalize(pXInfo);
221238 if( rc==SQLITE_OK0 ) rc = rc2;
221239
221240 if( rc!=SQLITE_OK0 ){
221241 sqlite3_free(zRet);
221242 sqlite3_free(zImpCols);
221243 sqlite3_free(zImpPK);
221244 sqlite3_free(zWhere);
221245 zRet = 0;
221246 zImpCols = 0;
221247 zImpPK = 0;
221248 zWhere = 0;
221249 p->rc = rc;
221250 }
221251
221252 *pzImposterCols = zImpCols;
221253 *pzImposterPk = zImpPK;
221254 *pzWhere = zWhere;
221255 *pnBind = nBind;
221256 return zRet;
221257}
221258
221259/*
221260** Assuming the current table columns are "a", "b" and "c", and the zObj
221261** paramter is passed "old", return a string of the form:
221262**
221263** "old.a, old.b, old.b"
221264**
221265** With the column names escaped.
221266**
221267** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
221268** the text ", old._rowid_" to the returned value.
221269*/
221270static char *rbuObjIterGetOldlist(
221271 sqlite3rbu *p,
221272 RbuObjIter *pIter,
221273 const char *zObj
221274){
221275 char *zList = 0;
221276 if( p->rc==SQLITE_OK0 && pIter->abIndexed ){
221277 const char *zS = "";
221278 int i;
221279 for(i=0; i<pIter->nTblCol; i++){
221280 if( pIter->abIndexed[i] ){
221281 const char *zCol = pIter->azTblCol[i];
221282 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
221283 }else{
221284 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
221285 }
221286 zS = ", ";
221287 if( zList==0 ){
221288 p->rc = SQLITE_NOMEM7;
221289 break;
221290 }
221291 }
221292
221293 /* For a table with implicit rowids, append "old._rowid_" to the list. */
221294 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
221295 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
221296 }
221297 }
221298 return zList;
221299}
221300
221301/*
221302** Return an expression that can be used in a WHERE clause to match the
221303** primary key of the current table. For example, if the table is:
221304**
221305** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
221306**
221307** Return the string:
221308**
221309** "b = ?1 AND c = ?2"
221310*/
221311static char *rbuObjIterGetWhere(
221312 sqlite3rbu *p,
221313 RbuObjIter *pIter
221314){
221315 char *zList = 0;
221316 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
221317 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
221318 }else if( pIter->eType==RBU_PK_EXTERNAL ){
221319 const char *zSep = "";
221320 int i;
221321 for(i=0; i<pIter->nTblCol; i++){
221322 if( pIter->abTblPk[i] ){
221323 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
221324 zSep = " AND ";
221325 }
221326 }
221327 zList = rbuMPrintf(p,
221328 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
221329 );
221330
221331 }else{
221332 const char *zSep = "";
221333 int i;
221334 for(i=0; i<pIter->nTblCol; i++){
221335 if( pIter->abTblPk[i] ){
221336 const char *zCol = pIter->azTblCol[i];
221337 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
221338 zSep = " AND ";
221339 }
221340 }
221341 }
221342 return zList;
221343}
221344
221345/*
221346** The SELECT statement iterating through the keys for the current object
221347** (p->objiter.pSelect) currently points to a valid row. However, there
221348** is something wrong with the rbu_control value in the rbu_control value
221349** stored in the (p->nCol+1)'th column. Set the error code and error message
221350** of the RBU handle to something reflecting this.
221351*/
221352static void rbuBadControlError(sqlite3rbu *p){
221353 p->rc = SQLITE_ERROR1;
221354 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
221355}
221356
221357
221358/*
221359** Return a nul-terminated string containing the comma separated list of
221360** assignments that should be included following the "SET" keyword of
221361** an UPDATE statement used to update the table object that the iterator
221362** passed as the second argument currently points to if the rbu_control
221363** column of the data_xxx table entry is set to zMask.
221364**
221365** The memory for the returned string is obtained from sqlite3_malloc().
221366** It is the responsibility of the caller to eventually free it using
221367** sqlite3_free().
221368**
221369** If an OOM error is encountered when allocating space for the new
221370** string, an error code is left in the rbu handle passed as the first
221371** argument and NULL is returned. Or, if an error has already occurred
221372** when this function is called, NULL is returned immediately, without
221373** attempting the allocation or modifying the stored error code.
221374*/
221375static char *rbuObjIterGetSetlist(
221376 sqlite3rbu *p,
221377 RbuObjIter *pIter,
221378 const char *zMask
221379){
221380 char *zList = 0;
221381 if( p->rc==SQLITE_OK0 ){
221382 int i;
221383
221384 if( (int)strlen(zMask)!=pIter->nTblCol ){
221385 rbuBadControlError(p);
221386 }else{
221387 const char *zSep = "";
221388 for(i=0; i<pIter->nTblCol; i++){
221389 char c = zMask[pIter->aiSrcOrder[i]];
221390 if( c=='x' ){
221391 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
221392 zList, zSep, pIter->azTblCol[i], i+1
221393 );
221394 zSep = ", ";
221395 }
221396 else if( c=='d' ){
221397 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
221398 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
221399 );
221400 zSep = ", ";
221401 }
221402 else if( c=='f' ){
221403 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
221404 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
221405 );
221406 zSep = ", ";
221407 }
221408 }
221409 }
221410 }
221411 return zList;
221412}
221413
221414/*
221415** Return a nul-terminated string consisting of nByte comma separated
221416** "?" expressions. For example, if nByte is 3, return a pointer to
221417** a buffer containing the string "?,?,?".
221418**
221419** The memory for the returned string is obtained from sqlite3_malloc().
221420** It is the responsibility of the caller to eventually free it using
221421** sqlite3_free().
221422**
221423** If an OOM error is encountered when allocating space for the new
221424** string, an error code is left in the rbu handle passed as the first
221425** argument and NULL is returned. Or, if an error has already occurred
221426** when this function is called, NULL is returned immediately, without
221427** attempting the allocation or modifying the stored error code.
221428*/
221429static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
221430 char *zRet = 0;
221431 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
221432
221433 zRet = (char*)rbuMalloc(p, nByte);
221434 if( zRet ){
221435 int i;
221436 for(i=0; i<nBind; i++){
221437 zRet[i*2] = '?';
221438 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
221439 }
221440 }
221441 return zRet;
221442}
221443
221444/*
221445** The iterator currently points to a table (not index) of type
221446** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
221447** declaration for the corresponding imposter table. For example,
221448** if the iterator points to a table created as:
221449**
221450** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
221451**
221452** this function returns:
221453**
221454** PRIMARY KEY("b", "a" DESC)
221455*/
221456static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
221457 char *z = 0;
221458 assert( pIter->zIdx==0 )((void) (0));
221459 if( p->rc==SQLITE_OK0 ){
221460 const char *zSep = "PRIMARY KEY(";
221461 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
221462 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
221463
221464 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
221465 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
221466 );
221467 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXList) ){
221468 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
221469 if( zOrig && strcmp(zOrig, "pk")==0 ){
221470 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
221471 if( zIdx ){
221472 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
221473 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
221474 );
221475 }
221476 break;
221477 }
221478 }
221479 rbuFinalize(p, pXList);
221480
221481 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
221482 if( sqlite3_column_int(pXInfo, 5) ){
221483 /* int iCid = sqlite3_column_int(pXInfo, 0); */
221484 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
221485 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
221486 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
221487 zSep = ", ";
221488 }
221489 }
221490 z = rbuMPrintf(p, "%z)", z);
221491 rbuFinalize(p, pXInfo);
221492 }
221493 return z;
221494}
221495
221496/*
221497** This function creates the second imposter table used when writing to
221498** a table b-tree where the table has an external primary key. If the
221499** iterator passed as the second argument does not currently point to
221500** a table (not index) with an external primary key, this function is a
221501** no-op.
221502**
221503** Assuming the iterator does point to a table with an external PK, this
221504** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
221505** used to access that PK index. For example, if the target table is
221506** declared as follows:
221507**
221508** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
221509**
221510** then the imposter table schema is:
221511**
221512** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
221513**
221514*/
221515static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
221516 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_EXTERNAL ){
221517 int tnum = pIter->iPkTnum; /* Root page of PK index */
221518 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
221519 const char *zIdx = 0; /* Name of PK index */
221520 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
221521 const char *zComma = "";
221522 char *zCols = 0; /* Used to build up list of table cols */
221523 char *zPk = 0; /* Used to build up table PK declaration */
221524
221525 /* Figure out the name of the primary key index for the current table.
221526 ** This is needed for the argument to "PRAGMA index_xinfo". Set
221527 ** zIdx to point to a nul-terminated string containing this name. */
221528 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
221529 "SELECT name FROM sqlite_schema WHERE rootpage = ?"
221530 );
221531 if( p->rc==SQLITE_OK0 ){
221532 sqlite3_bind_int(pQuery, 1, tnum);
221533 if( SQLITE_ROW100==sqlite3_step(pQuery) ){
221534 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
221535 }
221536 }
221537 if( zIdx ){
221538 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
221539 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
221540 );
221541 }
221542 rbuFinalize(p, pQuery);
221543
221544 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
221545 int bKey = sqlite3_column_int(pXInfo, 5);
221546 if( bKey ){
221547 int iCid = sqlite3_column_int(pXInfo, 1);
221548 int bDesc = sqlite3_column_int(pXInfo, 3);
221549 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
221550 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
221551 iCid, pIter->azTblType[iCid], zCollate
221552 );
221553 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
221554 zComma = ", ";
221555 }
221556 }
221557 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
221558 rbuFinalize(p, pXInfo);
221559
221560 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
221561 rbuMPrintfExec(p, p->dbMain,
221562 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
221563 zCols, zPk
221564 );
221565 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
221566 }
221567}
221568
221569/*
221570** If an error has already occurred when this function is called, it
221571** immediately returns zero (without doing any work). Or, if an error
221572** occurs during the execution of this function, it sets the error code
221573** in the sqlite3rbu object indicated by the first argument and returns
221574** zero.
221575**
221576** The iterator passed as the second argument is guaranteed to point to
221577** a table (not an index) when this function is called. This function
221578** attempts to create any imposter table required to write to the main
221579** table b-tree of the table before returning. Non-zero is returned if
221580** an imposter table are created, or zero otherwise.
221581**
221582** An imposter table is required in all cases except RBU_PK_VTAB. Only
221583** virtual tables are written to directly. The imposter table has the
221584** same schema as the actual target table (less any UNIQUE constraints).
221585** More precisely, the "same schema" means the same columns, types,
221586** collation sequences. For tables that do not have an external PRIMARY
221587** KEY, it also means the same PRIMARY KEY declaration.
221588*/
221589static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
221590 if( p->rc==SQLITE_OK0 && pIter->eType!=RBU_PK_VTAB ){
221591 int tnum = pIter->iTnum;
221592 const char *zComma = "";
221593 char *zSql = 0;
221594 int iCol;
221595 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
221596
221597 for(iCol=0; p->rc==SQLITE_OK0 && iCol<pIter->nTblCol; iCol++){
221598 const char *zPk = "";
221599 const char *zCol = pIter->azTblCol[iCol];
221600 const char *zColl = 0;
221601
221602 p->rc = sqlite3_table_column_metadata(
221603 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
221604 );
221605
221606 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
221607 /* If the target table column is an "INTEGER PRIMARY KEY", add
221608 ** "PRIMARY KEY" to the imposter table column declaration. */
221609 zPk = "PRIMARY KEY ";
221610 }
221611 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
221612 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
221613 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
221614 );
221615 zComma = ", ";
221616 }
221617
221618 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
221619 char *zPk = rbuWithoutRowidPK(p, pIter);
221620 if( zPk ){
221621 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
221622 }
221623 }
221624
221625 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
221626 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
221627 pIter->zTbl, zSql,
221628 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
221629 );
221630 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
221631 }
221632}
221633
221634/*
221635** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
221636** Specifically a statement of the form:
221637**
221638** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
221639**
221640** The number of bound variables is equal to the number of columns in
221641** the target table, plus one (for the rbu_control column), plus one more
221642** (for the rbu_rowid column) if the target table is an implicit IPK or
221643** virtual table.
221644*/
221645static void rbuObjIterPrepareTmpInsert(
221646 sqlite3rbu *p,
221647 RbuObjIter *pIter,
221648 const char *zCollist,
221649 const char *zRbuRowid
221650){
221651 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
221652 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
221653 if( zBind ){
221654 assert( pIter->pTmpInsert==0 )((void) (0));
221655 p->rc = prepareFreeAndCollectError(
221656 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
221657 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
221658 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
221659 ));
221660 }
221661}
221662
221663static void rbuTmpInsertFunc(
221664 sqlite3_context *pCtx,
221665 int nVal,
221666 sqlite3_value **apVal
221667){
221668 sqlite3rbu *p = sqlite3_user_data(pCtx);
221669 int rc = SQLITE_OK0;
221670 int i;
221671
221672 assert( sqlite3_value_int(apVal[0])!=0((void) (0))
221673 || p->objiter.eType==RBU_PK_EXTERNAL((void) (0))
221674 || p->objiter.eType==RBU_PK_NONE((void) (0))
221675 )((void) (0));
221676 if( sqlite3_value_int(apVal[0])!=0 ){
221677 p->nPhaseOneStep += p->objiter.nIndex;
221678 }
221679
221680 for(i=0; rc==SQLITE_OK0 && i<nVal; i++){
221681 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
221682 }
221683 if( rc==SQLITE_OK0 ){
221684 sqlite3_step(p->objiter.pTmpInsert);
221685 rc = sqlite3_reset(p->objiter.pTmpInsert);
221686 }
221687
221688 if( rc!=SQLITE_OK0 ){
221689 sqlite3_result_error_code(pCtx, rc);
221690 }
221691}
221692
221693static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
221694 sqlite3_stmt *pStmt = 0;
221695 int rc = p->rc;
221696 char *zRet = 0;
221697
221698 assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 )((void) (0));
221699
221700 if( rc==SQLITE_OK0 ){
221701 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
221702 "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
221703 );
221704 }
221705 if( rc==SQLITE_OK0 ){
221706 int rc2;
221707 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
221708 if( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
221709 char *zSql = (char*)sqlite3_column_text(pStmt, 0);
221710 if( zSql ){
221711 pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
221712 }
221713 if( zSql ){
221714 int nParen = 0; /* Number of open parenthesis */
221715 int i;
221716 int iIdxCol = 0;
221717 int nIdxAlloc = 0;
221718 for(i=0; zSql[i]; i++){
221719 char c = zSql[i];
221720
221721 /* If necessary, grow the pIter->aIdxCol[] array */
221722 if( iIdxCol==nIdxAlloc ){
221723 RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
221724 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
221725 );
221726 if( aIdxCol==0 ){
221727 rc = SQLITE_NOMEM7;
221728 break;
221729 }
221730 pIter->aIdxCol = aIdxCol;
221731 nIdxAlloc += 16;
221732 }
221733
221734 if( c=='(' ){
221735 if( nParen==0 ){
221736 assert( iIdxCol==0 )((void) (0));
221737 pIter->aIdxCol[0].zSpan = &zSql[i+1];
221738 }
221739 nParen++;
221740 }
221741 else if( c==')' ){
221742 nParen--;
221743 if( nParen==0 ){
221744 int nSpan = (int)(&zSql[i] - pIter->aIdxCol[iIdxCol].zSpan);
221745 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
221746 i++;
221747 break;
221748 }
221749 }else if( c==',' && nParen==1 ){
221750 int nSpan = (int)(&zSql[i] - pIter->aIdxCol[iIdxCol].zSpan);
221751 pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
221752 pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
221753 }else if( c=='"' || c=='\'' || c=='`' ){
221754 for(i++; 1; i++){
221755 if( zSql[i]==c ){
221756 if( zSql[i+1]!=c ) break;
221757 i++;
221758 }
221759 }
221760 }else if( c=='[' ){
221761 for(i++; 1; i++){
221762 if( zSql[i]==']' ) break;
221763 }
221764 }else if( c=='-' && zSql[i+1]=='-' ){
221765 for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
221766 if( zSql[i]=='\0' ) break;
221767 }else if( c=='/' && zSql[i+1]=='*' ){
221768 for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
221769 if( zSql[i]=='\0' ) break;
221770 i++;
221771 }
221772 }
221773 if( zSql[i] ){
221774 zRet = rbuStrndup(&zSql[i], &rc);
221775 }
221776 pIter->nIdxCol = iIdxCol;
221777 }
221778 }
221779
221780 rc2 = sqlite3_finalize(pStmt);
221781 if( rc==SQLITE_OK0 ) rc = rc2;
221782 }
221783
221784 p->rc = rc;
221785 return zRet;
221786}
221787
221788/*
221789** Ensure that the SQLite statement handles required to update the
221790** target database object currently indicated by the iterator passed
221791** as the second argument are available.
221792*/
221793static int rbuObjIterPrepareAll(
221794 sqlite3rbu *p,
221795 RbuObjIter *pIter,
221796 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
221797){
221798 assert( pIter->bCleanup==0 )((void) (0));
221799 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK0 ){
221800 const int tnum = pIter->iTnum;
221801 char *zCollist = 0; /* List of indexed columns */
221802 char **pz = &p->zErrmsg;
221803 const char *zIdx = pIter->zIdx;
221804 char *zLimit = 0;
221805
221806 if( nOffset ){
221807 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
221808 if( !zLimit ) p->rc = SQLITE_NOMEM7;
221809 }
221810
221811 if( zIdx ){
221812 const char *zTbl = pIter->zTbl;
221813 char *zImposterCols = 0; /* Columns for imposter table */
221814 char *zImposterPK = 0; /* Primary key declaration for imposter */
221815 char *zWhere = 0; /* WHERE clause on PK columns */
221816 char *zBind = 0;
221817 char *zPart = 0;
221818 int nBind = 0;
221819
221820 assert( pIter->eType!=RBU_PK_VTAB )((void) (0));
221821 zPart = rbuObjIterGetIndexWhere(p, pIter);
221822 zCollist = rbuObjIterGetIndexCols(
221823 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
221824 );
221825 zBind = rbuObjIterGetBindlist(p, nBind);
221826
221827 /* Create the imposter table used to write to this index. */
221828 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
221829 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1,tnum);
221830 rbuMPrintfExec(p, p->dbMain,
221831 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
221832 zTbl, zImposterCols, zImposterPK
221833 );
221834 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
221835
221836 /* Create the statement to insert index entries */
221837 pIter->nCol = nBind;
221838 if( p->rc==SQLITE_OK0 ){
221839 p->rc = prepareFreeAndCollectError(
221840 p->dbMain, &pIter->pInsert, &p->zErrmsg,
221841 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
221842 );
221843 }
221844
221845 /* And to delete index entries */
221846 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
221847 p->rc = prepareFreeAndCollectError(
221848 p->dbMain, &pIter->pDelete, &p->zErrmsg,
221849 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
221850 );
221851 }
221852
221853 /* Create the SELECT statement to read keys in sorted order */
221854 if( p->rc==SQLITE_OK0 ){
221855 char *zSql;
221856 if( rbuIsVacuum(p) ){
221857 char *zStart = 0;
221858 if( nOffset ){
221859 zStart = rbuVacuumIndexStart(p, pIter);
221860 if( zStart ){
221861 sqlite3_free(zLimit);
221862 zLimit = 0;
221863 }
221864 }
221865
221866 zSql = sqlite3_mprintf(
221867 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
221868 zCollist,
221869 pIter->zDataTbl,
221870 zPart,
221871 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
221872 zCollist, zLimit
221873 );
221874 sqlite3_free(zStart);
221875 }else
221876
221877 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
221878 zSql = sqlite3_mprintf(
221879 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
221880 zCollist, p->zStateDb, pIter->zDataTbl,
221881 zPart, zCollist, zLimit
221882 );
221883 }else{
221884 zSql = sqlite3_mprintf(
221885 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
221886 "UNION ALL "
221887 "SELECT %s, rbu_control FROM '%q' "
221888 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
221889 "ORDER BY %s%s",
221890 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
221891 zCollist, pIter->zDataTbl,
221892 zPart,
221893 (zPart ? "AND" : "WHERE"),
221894 zCollist, zLimit
221895 );
221896 }
221897 if( p->rc==SQLITE_OK0 ){
221898 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
221899 }else{
221900 sqlite3_free(zSql);
221901 }
221902 }
221903
221904 sqlite3_free(zImposterCols);
221905 sqlite3_free(zImposterPK);
221906 sqlite3_free(zWhere);
221907 sqlite3_free(zBind);
221908 sqlite3_free(zPart);
221909 }else{
221910 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
221911 ||(pIter->eType==RBU_PK_NONE)
221912 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
221913 const char *zTbl = pIter->zTbl; /* Table this step applies to */
221914 const char *zWrite; /* Imposter table name */
221915
221916 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
221917 char *zWhere = rbuObjIterGetWhere(p, pIter);
221918 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
221919 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
221920
221921 zCollist = rbuObjIterGetCollist(p, pIter);
221922 pIter->nCol = pIter->nTblCol;
221923
221924 /* Create the imposter table or tables (if required). */
221925 rbuCreateImposterTable(p, pIter);
221926 rbuCreateImposterTable2(p, pIter);
221927 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
221928
221929 /* Create the INSERT statement to write to the target PK b-tree */
221930 if( p->rc==SQLITE_OK0 ){
221931 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
221932 sqlite3_mprintf(
221933 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
221934 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
221935 )
221936 );
221937 }
221938
221939 /* Create the DELETE statement to write to the target PK b-tree.
221940 ** Because it only performs INSERT operations, this is not required for
221941 ** an rbu vacuum handle. */
221942 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
221943 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
221944 sqlite3_mprintf(
221945 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
221946 )
221947 );
221948 }
221949
221950 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
221951 const char *zRbuRowid = "";
221952 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
221953 zRbuRowid = ", rbu_rowid";
221954 }
221955
221956 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
221957 rbuMPrintfExec(p, p->dbRbu,
221958 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
221959 "SELECT *%s FROM '%q' WHERE 0;"
221960 , p->zStateDb, pIter->zDataTbl
221961 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
221962 , pIter->zDataTbl
221963 );
221964
221965 rbuMPrintfExec(p, p->dbMain,
221966 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
221967 "BEGIN "
221968 " SELECT rbu_tmp_insert(3, %s);"
221969 "END;"
221970
221971 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
221972 "BEGIN "
221973 " SELECT rbu_tmp_insert(3, %s);"
221974 "END;"
221975
221976 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
221977 "BEGIN "
221978 " SELECT rbu_tmp_insert(4, %s);"
221979 "END;",
221980 zWrite, zTbl, zOldlist,
221981 zWrite, zTbl, zOldlist,
221982 zWrite, zTbl, zNewlist
221983 );
221984
221985 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
221986 rbuMPrintfExec(p, p->dbMain,
221987 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
221988 "BEGIN "
221989 " SELECT rbu_tmp_insert(0, %s);"
221990 "END;",
221991 zWrite, zTbl, zNewlist
221992 );
221993 }
221994
221995 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
221996 }
221997
221998 /* Create the SELECT statement to read keys from data_xxx */
221999 if( p->rc==SQLITE_OK0 ){
222000 const char *zRbuRowid = "";
222001 char *zStart = 0;
222002 char *zOrder = 0;
222003 if( bRbuRowid ){
222004 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
222005 }
222006
222007 if( rbuIsVacuum(p) ){
222008 if( nOffset ){
222009 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
222010 if( zStart ){
222011 sqlite3_free(zLimit);
222012 zLimit = 0;
222013 }
222014 }
222015 if( bRbuRowid ){
222016 zOrder = rbuMPrintf(p, "_rowid_");
222017 }else{
222018 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
222019 }
222020 }
222021
222022 if( p->rc==SQLITE_OK0 ){
222023 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
222024 sqlite3_mprintf(
222025 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
222026 zCollist,
222027 (rbuIsVacuum(p) ? "0 AS " : ""),
222028 zRbuRowid,
222029 pIter->zDataTbl, (zStart ? zStart : ""),
222030 (zOrder ? "ORDER BY" : ""), zOrder,
222031 zLimit
222032 )
222033 );
222034 }
222035 sqlite3_free(zStart);
222036 sqlite3_free(zOrder);
222037 }
222038
222039 sqlite3_free(zWhere);
222040 sqlite3_free(zOldlist);
222041 sqlite3_free(zNewlist);
222042 sqlite3_free(zBindings);
222043 }
222044 sqlite3_free(zCollist);
222045 sqlite3_free(zLimit);
222046 }
222047
222048 return p->rc;
222049}
222050
222051/*
222052** Set output variable *ppStmt to point to an UPDATE statement that may
222053** be used to update the imposter table for the main table b-tree of the
222054** table object that pIter currently points to, assuming that the
222055** rbu_control column of the data_xyz table contains zMask.
222056**
222057** If the zMask string does not specify any columns to update, then this
222058** is not an error. Output variable *ppStmt is set to NULL in this case.
222059*/
222060static int rbuGetUpdateStmt(
222061 sqlite3rbu *p, /* RBU handle */
222062 RbuObjIter *pIter, /* Object iterator */
222063 const char *zMask, /* rbu_control value ('x.x.') */
222064 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
222065){
222066 RbuUpdateStmt **pp;
222067 RbuUpdateStmt *pUp = 0;
222068 int nUp = 0;
222069
222070 /* In case an error occurs */
222071 *ppStmt = 0;
222072
222073 /* Search for an existing statement. If one is found, shift it to the front
222074 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
222075 ** to the number of statements currently in the cache and pUp to the
222076 ** last object in the list. */
222077 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
222078 pUp = *pp;
222079 if( strcmp(pUp->zMask, zMask)==0 ){
222080 *pp = pUp->pNext;
222081 pUp->pNext = pIter->pRbuUpdate;
222082 pIter->pRbuUpdate = pUp;
222083 *ppStmt = pUp->pUpdate;
222084 return SQLITE_OK0;
222085 }
222086 nUp++;
222087 }
222088 assert( pUp==0 || pUp->pNext==0 )((void) (0));
222089
222090 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
222091 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
222092 *pp = 0;
222093 sqlite3_finalize(pUp->pUpdate);
222094 pUp->pUpdate = 0;
222095 }else{
222096 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
222097 }
222098
222099 if( pUp ){
222100 char *zWhere = rbuObjIterGetWhere(p, pIter);
222101 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
222102 char *zUpdate = 0;
222103
222104 pUp->zMask = (char*)&pUp[1];
222105 memcpy(pUp->zMask, zMask, pIter->nTblCol);
222106 pUp->pNext = pIter->pRbuUpdate;
222107 pIter->pRbuUpdate = pUp;
222108
222109 if( zSet ){
222110 const char *zPrefix = "";
222111
222112 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
222113 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
222114 zPrefix, pIter->zTbl, zSet, zWhere
222115 );
222116 p->rc = prepareFreeAndCollectError(
222117 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
222118 );
222119 *ppStmt = pUp->pUpdate;
222120 }
222121 sqlite3_free(zWhere);
222122 sqlite3_free(zSet);
222123 }
222124
222125 return p->rc;
222126}
222127
222128static sqlite3 *rbuOpenDbhandle(
222129 sqlite3rbu *p,
222130 const char *zName,
222131 int bUseVfs
222132){
222133 sqlite3 *db = 0;
222134 if( p->rc==SQLITE_OK0 ){
222135 const int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_URI0x00000040;
222136 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
222137 if( p->rc ){
222138 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
222139 sqlite3_close(db);
222140 db = 0;
222141 }
222142 }
222143 return db;
222144}
222145
222146/*
222147** Free an RbuState object allocated by rbuLoadState().
222148*/
222149static void rbuFreeState(RbuState *p){
222150 if( p ){
222151 sqlite3_free(p->zTbl);
222152 sqlite3_free(p->zDataTbl);
222153 sqlite3_free(p->zIdx);
222154 sqlite3_free(p);
222155 }
222156}
222157
222158/*
222159** Allocate an RbuState object and load the contents of the rbu_state
222160** table into it. Return a pointer to the new object. It is the
222161** responsibility of the caller to eventually free the object using
222162** sqlite3_free().
222163**
222164** If an error occurs, leave an error code and message in the rbu handle
222165** and return NULL.
222166*/
222167static RbuState *rbuLoadState(sqlite3rbu *p){
222168 RbuState *pRet = 0;
222169 sqlite3_stmt *pStmt = 0;
222170 int rc;
222171 int rc2;
222172
222173 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
222174 if( pRet==0 ) return 0;
222175
222176 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
222177 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
222178 );
222179 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
222180 switch( sqlite3_column_int(pStmt, 0) ){
222181 case RBU_STATE_STAGE:
222182 pRet->eStage = sqlite3_column_int(pStmt, 1);
222183 if( pRet->eStage!=RBU_STAGE_OAL
222184 && pRet->eStage!=RBU_STAGE_MOVE
222185 && pRet->eStage!=RBU_STAGE_CKPT
222186 ){
222187 p->rc = SQLITE_CORRUPT11;
222188 }
222189 break;
222190
222191 case RBU_STATE_TBL:
222192 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
222193 break;
222194
222195 case RBU_STATE_IDX:
222196 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
222197 break;
222198
222199 case RBU_STATE_ROW:
222200 pRet->nRow = sqlite3_column_int(pStmt, 1);
222201 break;
222202
222203 case RBU_STATE_PROGRESS:
222204 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
222205 break;
222206
222207 case RBU_STATE_CKPT:
222208 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
222209 break;
222210
222211 case RBU_STATE_COOKIE:
222212 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
222213 break;
222214
222215 case RBU_STATE_OALSZ:
222216 pRet->iOalSz = sqlite3_column_int64(pStmt, 1);
222217 break;
222218
222219 case RBU_STATE_PHASEONESTEP:
222220 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
222221 break;
222222
222223 case RBU_STATE_DATATBL:
222224 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
222225 break;
222226
222227 default:
222228 rc = SQLITE_CORRUPT11;
222229 break;
222230 }
222231 }
222232 rc2 = sqlite3_finalize(pStmt);
222233 if( rc==SQLITE_OK0 ) rc = rc2;
222234
222235 p->rc = rc;
222236 return pRet;
222237}
222238
222239
222240/*
222241** Open the database handle and attach the RBU database as "rbu". If an
222242** error occurs, leave an error code and message in the RBU handle.
222243**
222244** If argument dbMain is not NULL, then it is a database handle already
222245** open on the target database. Use this handle instead of opening a new
222246** one.
222247*/
222248static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){
222249 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) )((void) (0));
222250 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 )((void) (0));
222251 assert( dbMain==0 || rbuIsVacuum(p)==0 )((void) (0));
222252
222253 /* Open the RBU database */
222254 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
222255 p->dbMain = dbMain;
222256
222257 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
222258 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
222259 if( p->zState==0 ){
222260 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
222261 p->zState = rbuMPrintf(p, "file:///%s-vacuum?modeof=%s", zFile, zFile);
222262 }
222263 }
222264
222265 /* If using separate RBU and state databases, attach the state database to
222266 ** the RBU db handle now. */
222267 if( p->zState ){
222268 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
222269 memcpy(p->zStateDb, "stat", 4);
222270 }else{
222271 memcpy(p->zStateDb, "main", 4);
222272 }
222273
222274#if 0
222275 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
222276 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
222277 }
222278#endif
222279
222280 /* If it has not already been created, create the rbu_state table */
222281 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
222282
222283#if 0
222284 if( rbuIsVacuum(p) ){
222285 if( p->rc==SQLITE_OK0 ){
222286 int rc2;
222287 int bOk = 0;
222288 sqlite3_stmt *pCnt = 0;
222289 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
222290 "SELECT count(*) FROM stat.sqlite_schema"
222291 );
222292 if( p->rc==SQLITE_OK0
222293 && sqlite3_step(pCnt)==SQLITE_ROW100
222294 && 1==sqlite3_column_int(pCnt, 0)
222295 ){
222296 bOk = 1;
222297 }
222298 rc2 = sqlite3_finalize(pCnt);
222299 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
222300
222301 if( p->rc==SQLITE_OK0 && bOk==0 ){
222302 p->rc = SQLITE_ERROR1;
222303 p->zErrmsg = sqlite3_mprintf("invalid state database");
222304 }
222305
222306 if( p->rc==SQLITE_OK0 ){
222307 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
222308 }
222309 }
222310 }
222311#endif
222312
222313 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
222314 int bOpen = 0;
222315 int rc;
222316 p->nRbu = 0;
222317 p->pRbuFd = 0;
222318 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
222319 if( rc!=SQLITE_NOTFOUND12 ) p->rc = rc;
222320 if( p->eStage>=RBU_STAGE_MOVE ){
222321 bOpen = 1;
222322 }else{
222323 RbuState *pState = rbuLoadState(p);
222324 if( pState ){
222325 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
222326 rbuFreeState(pState);
222327 }
222328 }
222329 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
222330 }
222331
222332 p->eStage = 0;
222333 if( p->rc==SQLITE_OK0 && p->dbMain==0 ){
222334 if( !rbuIsVacuum(p) ){
222335 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
222336 }else if( p->pRbuFd->pWalFd ){
222337 if( pbRetry ){
222338 p->pRbuFd->bNolock = 0;
222339 sqlite3_close(p->dbRbu);
222340 sqlite3_close(p->dbMain);
222341 p->dbMain = 0;
222342 p->dbRbu = 0;
222343 *pbRetry = 1;
222344 return;
222345 }
222346 p->rc = SQLITE_ERROR1;
222347 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
222348 }else{
222349 char *zTarget;
222350 char *zExtra = 0;
222351 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
222352 zExtra = &p->zRbu[5];
222353 while( *zExtra ){
222354 if( *zExtra++=='?' ) break;
222355 }
222356 if( *zExtra=='\0' ) zExtra = 0;
222357 }
222358
222359 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
222360 sqlite3_db_filename(p->dbRbu, "main"),
222361 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
222362 );
222363
222364 if( zTarget==0 ){
222365 p->rc = SQLITE_NOMEM7;
222366 return;
222367 }
222368 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
222369 sqlite3_free(zTarget);
222370 }
222371 }
222372
222373 if( p->rc==SQLITE_OK0 ){
222374 p->rc = sqlite3_create_function(p->dbMain,
222375 "rbu_tmp_insert", -1, SQLITE_UTF81, (void*)p, rbuTmpInsertFunc, 0, 0
222376 );
222377 }
222378
222379 if( p->rc==SQLITE_OK0 ){
222380 p->rc = sqlite3_create_function(p->dbMain,
222381 "rbu_fossil_delta", 2, SQLITE_UTF81, 0, rbuFossilDeltaFunc, 0, 0
222382 );
222383 }
222384
222385 if( p->rc==SQLITE_OK0 ){
222386 p->rc = sqlite3_create_function(p->dbRbu,
222387 "rbu_target_name", -1, SQLITE_UTF81, (void*)p, rbuTargetNameFunc, 0, 0
222388 );
222389 }
222390
222391 if( p->rc==SQLITE_OK0 ){
222392 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
222393 }
222394 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
222395
222396 /* Mark the database file just opened as an RBU target database. If
222397 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
222398 ** This is an error. */
222399 if( p->rc==SQLITE_OK0 ){
222400 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
222401 }
222402
222403 if( p->rc==SQLITE_NOTFOUND12 ){
222404 p->rc = SQLITE_ERROR1;
222405 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
222406 }
222407}
222408
222409/*
222410** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
222411** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
222412**
222413** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
222414** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
222415** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
222416** three characters, then shorten the suffix on z[] to be the last three
222417** characters of the original suffix.
222418**
222419** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
222420** do the suffix shortening regardless of URI parameter.
222421**
222422** Examples:
222423**
222424** test.db-journal => test.nal
222425** test.db-wal => test.wal
222426** test.db-shm => test.shm
222427** test.db-mj7f3319fa => test.9fa
222428*/
222429static void rbuFileSuffix3(const char *zBase, char *z){
222430#ifdef SQLITE_ENABLE_8_3_NAMES
222431#if SQLITE_ENABLE_8_3_NAMES<2
222432 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
222433#endif
222434 {
222435 int i, sz;
222436 sz = (int)strlen(z)&0xffffff;
222437 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
222438 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
222439 }
222440#else
222441 UNUSED_PARAMETER2(zBase,z)(void)(zBase),(void)(z);
222442#endif
222443}
222444
222445/*
222446** Return the current wal-index header checksum for the target database
222447** as a 64-bit integer.
222448**
222449** The checksum is store in the first page of xShmMap memory as an 8-byte
222450** blob starting at byte offset 40.
222451*/
222452static i64 rbuShmChecksum(sqlite3rbu *p){
222453 i64 iRet = 0;
222454 if( p->rc==SQLITE_OK0 ){
222455 sqlite3_file *pDb = p->pTargetFd->pReal;
222456 u32 volatile *ptr;
222457 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
222458 if( p->rc==SQLITE_OK0 ){
222459 iRet = (i64)(((u64)ptr[10] << 32) + ptr[11]);
222460 }
222461 }
222462 return iRet;
222463}
222464
222465/*
222466** This function is called as part of initializing or reinitializing an
222467** incremental checkpoint.
222468**
222469** It populates the sqlite3rbu.aFrame[] array with the set of
222470** (wal frame -> db page) copy operations required to checkpoint the
222471** current wal file, and obtains the set of shm locks required to safely
222472** perform the copy operations directly on the file-system.
222473**
222474** If argument pState is not NULL, then the incremental checkpoint is
222475** being resumed. In this case, if the checksum of the wal-index-header
222476** following recovery is not the same as the checksum saved in the RbuState
222477** object, then the rbu handle is set to DONE state. This occurs if some
222478** other client appends a transaction to the wal file in the middle of
222479** an incremental checkpoint.
222480*/
222481static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
222482
222483 /* If pState is NULL, then the wal file may not have been opened and
222484 ** recovered. Running a read-statement here to ensure that doing so
222485 ** does not interfere with the "capture" process below. */
222486 if( pState==0 ){
222487 p->eStage = 0;
222488 if( p->rc==SQLITE_OK0 ){
222489 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
222490 }
222491 }
222492
222493 /* Assuming no error has occurred, run a "restart" checkpoint with the
222494 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
222495 ** special behaviour in the rbu VFS:
222496 **
222497 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
222498 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
222499 ** proceed with running a passive checkpoint instead of failing).
222500 **
222501 ** * Attempts to read from the *-wal file or write to the database file
222502 ** do not perform any IO. Instead, the frame/page combinations that
222503 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
222504 ** array.
222505 **
222506 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
222507 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
222508 ** no-ops. These locks will not be released until the connection
222509 ** is closed.
222510 **
222511 ** * Attempting to xSync() the database file causes an SQLITE_NOTICE
222512 ** error.
222513 **
222514 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
222515 ** checkpoint below fails with SQLITE_NOTICE, and leaves the aFrame[]
222516 ** array populated with a set of (frame -> page) mappings. Because the
222517 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
222518 ** data from the wal file into the database file according to the
222519 ** contents of aFrame[].
222520 */
222521 if( p->rc==SQLITE_OK0 ){
222522 int rc2;
222523 p->eStage = RBU_STAGE_CAPTURE;
222524 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
222525 if( rc2!=SQLITE_NOTICE27 ) p->rc = rc2;
222526 }
222527
222528 if( p->rc==SQLITE_OK0 && p->nFrame>0 ){
222529 p->eStage = RBU_STAGE_CKPT;
222530 p->nStep = (pState ? pState->nRow : 0);
222531 p->aBuf = rbuMalloc(p, p->pgsz);
222532 p->iWalCksum = rbuShmChecksum(p);
222533 }
222534
222535 if( p->rc==SQLITE_OK0 ){
222536 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
222537 p->rc = SQLITE_DONE101;
222538 p->eStage = RBU_STAGE_DONE;
222539 }else{
222540 int nSectorSize;
222541 sqlite3_file *pDb = p->pTargetFd->pReal;
222542 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
222543 assert( p->nPagePerSector==0 )((void) (0));
222544 nSectorSize = pDb->pMethods->xSectorSize(pDb);
222545 if( nSectorSize>p->pgsz ){
222546 p->nPagePerSector = nSectorSize / p->pgsz;
222547 }else{
222548 p->nPagePerSector = 1;
222549 }
222550
222551 /* Call xSync() on the wal file. This causes SQLite to sync the
222552 ** directory in which the target database and the wal file reside, in
222553 ** case it has not been synced since the rename() call in
222554 ** rbuMoveOalFile(). */
222555 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL0x00002);
222556 }
222557 }
222558}
222559
222560/*
222561** Called when iAmt bytes are read from offset iOff of the wal file while
222562** the rbu object is in capture mode. Record the frame number of the frame
222563** being read in the aFrame[] array.
222564*/
222565static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
222566 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
222567 u32 iFrame;
222568
222569 if( pRbu->mLock!=mReq ){
222570 pRbu->rc = SQLITE_BUSY5;
222571 return SQLITE_NOTICE_RBU(27 | (3<<8));
222572 }
222573
222574 pRbu->pgsz = iAmt;
222575 if( pRbu->nFrame==pRbu->nFrameAlloc ){
222576 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
222577 RbuFrame *aNew;
222578 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
222579 if( aNew==0 ) return SQLITE_NOMEM7;
222580 pRbu->aFrame = aNew;
222581 pRbu->nFrameAlloc = nNew;
222582 }
222583
222584 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
222585 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
222586 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
222587 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
222588 pRbu->nFrame++;
222589 return SQLITE_OK0;
222590}
222591
222592/*
222593** Called when a page of data is written to offset iOff of the database
222594** file while the rbu handle is in capture mode. Record the page number
222595** of the page being written in the aFrame[] array.
222596*/
222597static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
222598 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
222599 return SQLITE_OK0;
222600}
222601
222602/*
222603** This is called as part of an incremental checkpoint operation. Copy
222604** a single frame of data from the wal file into the database file, as
222605** indicated by the RbuFrame object.
222606*/
222607static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
222608 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
222609 sqlite3_file *pDb = p->pTargetFd->pReal;
222610 i64 iOff;
222611
222612 assert( p->rc==SQLITE_OK )((void) (0));
222613 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
222614 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
222615 if( p->rc ) return;
222616
222617 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
222618 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
222619}
222620
222621/*
222622** This value is copied from the definition of ZIPVFS_CTRL_FILE_POINTER
222623** in zipvfs.h.
222624*/
222625#define RBU_ZIPVFS_CTRL_FILE_POINTER 230439
222626
222627/*
222628** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
222629** successful, or an SQLite error code otherwise.
222630*/
222631static int rbuLockDatabase(sqlite3 *db){
222632 int rc = SQLITE_OK0;
222633 sqlite3_file *fd = 0;
222634
222635 sqlite3_file_control(db, "main", RBU_ZIPVFS_CTRL_FILE_POINTER, &fd);
222636 if( fd ){
222637 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER7, &fd);
222638 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED1);
222639 if( rc==SQLITE_OK0 ){
222640 rc = fd->pMethods->xUnlock(fd, SQLITE_LOCK_NONE0);
222641 }
222642 sqlite3_file_control(db, "main", RBU_ZIPVFS_CTRL_FILE_POINTER, &fd);
222643 }else{
222644 sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER7, &fd);
222645 }
222646
222647 if( rc==SQLITE_OK0 && fd->pMethods ){
222648 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED1);
222649 if( rc==SQLITE_OK0 ){
222650 rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE4);
222651 }
222652 }
222653 return rc;
222654}
222655
222656/*
222657** Return true if the database handle passed as the only argument
222658** was opened with the rbu_exclusive_checkpoint=1 URI parameter
222659** specified. Or false otherwise.
222660*/
222661static int rbuExclusiveCheckpoint(sqlite3 *db){
222662 const char *zUri = sqlite3_db_filename(db, 0);
222663 return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0);
222664}
222665
222666#if defined(_WIN32_WCE)
222667static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
222668 int nChar;
222669 LPWSTR zWideFilename;
222670
222671 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL((void*)0), 0);
222672 if( nChar==0 ){
222673 return 0;
222674 }
222675 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
222676 if( zWideFilename==0 ){
222677 return 0;
222678 }
222679 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
222680 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
222681 nChar);
222682 if( nChar==0 ){
222683 sqlite3_free(zWideFilename);
222684 zWideFilename = 0;
222685 }
222686 return zWideFilename;
222687}
222688#endif
222689
222690/*
222691** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
222692** on the database file. This proc moves the *-oal file to the *-wal path,
222693** then reopens the database file (this time in vanilla, non-oal, WAL mode).
222694** If an error occurs, leave an error code and error message in the rbu
222695** handle.
222696*/
222697static void rbuMoveOalFile(sqlite3rbu *p){
222698 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
222699 const char *zMove = zBase;
222700 char *zOal;
222701 char *zWal;
222702
222703 if( rbuIsVacuum(p) ){
222704 zMove = sqlite3_db_filename(p->dbRbu, "main");
222705 }
222706 zOal = sqlite3_mprintf("%s-oal", zMove);
222707 zWal = sqlite3_mprintf("%s-wal", zMove);
222708
222709 assert( p->eStage==RBU_STAGE_MOVE )((void) (0));
222710 assert( p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
222711 if( zWal==0 || zOal==0 ){
222712 p->rc = SQLITE_NOMEM7;
222713 }else{
222714 /* Move the *-oal file to *-wal. At this point connection p->db is
222715 ** holding a SHARED lock on the target database file (because it is
222716 ** in WAL mode). So no other connection may be writing the db.
222717 **
222718 ** In order to ensure that there are no database readers, an EXCLUSIVE
222719 ** lock is obtained here before the *-oal is moved to *-wal.
222720 */
222721 sqlite3 *dbMain = 0;
222722 rbuFileSuffix3(zBase, zWal);
222723 rbuFileSuffix3(zBase, zOal);
222724
222725 /* Re-open the databases. */
222726 rbuObjIterFinalize(&p->objiter);
222727 sqlite3_close(p->dbRbu);
222728 sqlite3_close(p->dbMain);
222729 p->dbMain = 0;
222730 p->dbRbu = 0;
222731
222732 dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
222733 if( dbMain ){
222734 assert( p->rc==SQLITE_OK )((void) (0));
222735 p->rc = rbuLockDatabase(dbMain);
222736 }
222737
222738 if( p->rc==SQLITE_OK0 ){
222739 p->rc = p->xRename(p->pRenameArg, zOal, zWal);
222740 }
222741
222742 if( p->rc!=SQLITE_OK0
222743 || rbuIsVacuum(p)
222744 || rbuExclusiveCheckpoint(dbMain)==0
222745 ){
222746 sqlite3_close(dbMain);
222747 dbMain = 0;
222748 }
222749
222750 if( p->rc==SQLITE_OK0 ){
222751 rbuOpenDatabase(p, dbMain, 0);
222752 rbuSetupCheckpoint(p, 0);
222753 }
222754 }
222755
222756 sqlite3_free(zWal);
222757 sqlite3_free(zOal);
222758}
222759
222760/*
222761** The SELECT statement iterating through the keys for the current object
222762** (p->objiter.pSelect) currently points to a valid row. This function
222763** determines the type of operation requested by this row and returns
222764** one of the following values to indicate the result:
222765**
222766** * RBU_INSERT
222767** * RBU_DELETE
222768** * RBU_IDX_DELETE
222769** * RBU_UPDATE
222770**
222771** If RBU_UPDATE is returned, then output variable *pzMask is set to
222772** point to the text value indicating the columns to update.
222773**
222774** If the rbu_control field contains an invalid value, an error code and
222775** message are left in the RBU handle and zero returned.
222776*/
222777static int rbuStepType(sqlite3rbu *p, const char **pzMask){
222778 int iCol = p->objiter.nCol; /* Index of rbu_control column */
222779 int res = 0; /* Return value */
222780
222781 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
222782 case SQLITE_INTEGER1: {
222783 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
222784 switch( iVal ){
222785 case 0: res = RBU_INSERT; break;
222786 case 1: res = RBU_DELETE; break;
222787 case 2: res = RBU_REPLACE; break;
222788 case 3: res = RBU_IDX_DELETE; break;
222789 case 4: res = RBU_IDX_INSERT; break;
222790 }
222791 break;
222792 }
222793
222794 case SQLITE_TEXT3: {
222795 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
222796 if( z==0 ){
222797 p->rc = SQLITE_NOMEM7;
222798 }else{
222799 *pzMask = (const char*)z;
222800 }
222801 res = RBU_UPDATE;
222802
222803 break;
222804 }
222805
222806 default:
222807 break;
222808 }
222809
222810 if( res==0 ){
222811 rbuBadControlError(p);
222812 }
222813 return res;
222814}
222815
222816#ifdef SQLITE_DEBUG
222817/*
222818** Assert that column iCol of statement pStmt is named zName.
222819*/
222820static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
222821 const char *zCol = sqlite3_column_name(pStmt, iCol);
222822 assert( 0==sqlite3_stricmp(zName, zCol) )((void) (0));
222823}
222824#else
222825# define assertColumnName(x,y,z)
222826#endif
222827
222828/*
222829** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
222830** RBU_IDX_DELETE. This function performs the work of a single
222831** sqlite3rbu_step() call for the type of operation specified by eType.
222832*/
222833static void rbuStepOneOp(sqlite3rbu *p, int eType){
222834 RbuObjIter *pIter = &p->objiter;
222835 sqlite3_value *pVal;
222836 sqlite3_stmt *pWriter;
222837 int i;
222838
222839 assert( p->rc==SQLITE_OK )((void) (0));
222840 assert( eType!=RBU_DELETE || pIter->zIdx==0 )((void) (0));
222841 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE((void) (0))
222842 || eType==RBU_INSERT || eType==RBU_IDX_INSERT((void) (0))
222843 )((void) (0));
222844
222845 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
222846 ** statement below does actually delete a row, nPhaseOneStep will be
222847 ** incremented by the same amount when SQL function rbu_tmp_insert()
222848 ** is invoked by the trigger. */
222849 if( eType==RBU_DELETE ){
222850 p->nPhaseOneStep -= p->objiter.nIndex;
222851 }
222852
222853 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
222854 pWriter = pIter->pDelete;
222855 }else{
222856 pWriter = pIter->pInsert;
222857 }
222858
222859 for(i=0; i<pIter->nCol; i++){
222860 /* If this is an INSERT into a table b-tree and the table has an
222861 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
222862 ** to write a NULL into the IPK column. That is not permitted. */
222863 if( eType==RBU_INSERT
222864 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
222865 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL5
222866 ){
222867 p->rc = SQLITE_MISMATCH20;
222868 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
222869 return;
222870 }
222871
222872 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
222873 continue;
222874 }
222875
222876 pVal = sqlite3_column_value(pIter->pSelect, i);
222877 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
222878 if( p->rc ) return;
222879 }
222880 if( pIter->zIdx==0 ){
222881 if( pIter->eType==RBU_PK_VTAB
222882 || pIter->eType==RBU_PK_NONE
222883 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
222884 ){
222885 /* For a virtual table, or a table with no primary key, the
222886 ** SELECT statement is:
222887 **
222888 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
222889 **
222890 ** Hence column_value(pIter->nCol+1).
222891 */
222892 assertColumnName(pIter->pSelect, pIter->nCol+1,
222893 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
222894 );
222895 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
222896 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
222897 }
222898 }
222899 if( p->rc==SQLITE_OK0 ){
222900 sqlite3_step(pWriter);
222901 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
222902 }
222903}
222904
222905/*
222906** This function does the work for an sqlite3rbu_step() call.
222907**
222908** The object-iterator (p->objiter) currently points to a valid object,
222909** and the input cursor (p->objiter.pSelect) currently points to a valid
222910** input row. Perform whatever processing is required and return.
222911**
222912** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
222913** and message is left in the RBU handle and a copy of the error code
222914** returned.
222915*/
222916static int rbuStep(sqlite3rbu *p){
222917 RbuObjIter *pIter = &p->objiter;
222918 const char *zMask = 0;
222919 int eType = rbuStepType(p, &zMask);
222920
222921 if( eType ){
222922 assert( eType==RBU_INSERT || eType==RBU_DELETE((void) (0))
222923 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE((void) (0))
222924 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE((void) (0))
222925 )((void) (0));
222926 assert( eType!=RBU_UPDATE || pIter->zIdx==0 )((void) (0));
222927
222928 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
222929 rbuBadControlError(p);
222930 }
222931 else if( eType==RBU_REPLACE ){
222932 if( pIter->zIdx==0 ){
222933 p->nPhaseOneStep += p->objiter.nIndex;
222934 rbuStepOneOp(p, RBU_DELETE);
222935 }
222936 if( p->rc==SQLITE_OK0 ) rbuStepOneOp(p, RBU_INSERT);
222937 }
222938 else if( eType!=RBU_UPDATE ){
222939 rbuStepOneOp(p, eType);
222940 }
222941 else{
222942 sqlite3_value *pVal;
222943 sqlite3_stmt *pUpdate = 0;
222944 assert( eType==RBU_UPDATE )((void) (0));
222945 p->nPhaseOneStep -= p->objiter.nIndex;
222946 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
222947 if( pUpdate ){
222948 int i;
222949 for(i=0; p->rc==SQLITE_OK0 && i<pIter->nCol; i++){
222950 char c = zMask[pIter->aiSrcOrder[i]];
222951 pVal = sqlite3_column_value(pIter->pSelect, i);
222952 if( pIter->abTblPk[i] || c!='.' ){
222953 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
222954 }
222955 }
222956 if( p->rc==SQLITE_OK0
222957 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
222958 ){
222959 /* Bind the rbu_rowid value to column _rowid_ */
222960 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
222961 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
222962 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
222963 }
222964 if( p->rc==SQLITE_OK0 ){
222965 sqlite3_step(pUpdate);
222966 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
222967 }
222968 }
222969 }
222970 }
222971 return p->rc;
222972}
222973
222974/*
222975** Increment the schema cookie of the main database opened by p->dbMain.
222976**
222977** Or, if this is an RBU vacuum, set the schema cookie of the main db
222978** opened by p->dbMain to one more than the schema cookie of the main
222979** db opened by p->dbRbu.
222980*/
222981static void rbuIncrSchemaCookie(sqlite3rbu *p){
222982 if( p->rc==SQLITE_OK0 ){
222983 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
222984 int iCookie = 1000000;
222985 sqlite3_stmt *pStmt;
222986
222987 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
222988 "PRAGMA schema_version"
222989 );
222990 if( p->rc==SQLITE_OK0 ){
222991 /* Coverage: it may be that this sqlite3_step() cannot fail. There
222992 ** is already a transaction open, so the prepared statement cannot
222993 ** throw an SQLITE_SCHEMA exception. The only database page the
222994 ** statement reads is page 1, which is guaranteed to be in the cache.
222995 ** And no memory allocations are required. */
222996 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
222997 iCookie = sqlite3_column_int(pStmt, 0);
222998 }
222999 rbuFinalize(p, pStmt);
223000 }
223001 if( p->rc==SQLITE_OK0 ){
223002 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
223003 }
223004 }
223005}
223006
223007/*
223008** Update the contents of the rbu_state table within the rbu database. The
223009** value stored in the RBU_STATE_STAGE column is eStage. All other values
223010** are determined by inspecting the rbu handle passed as the first argument.
223011*/
223012static void rbuSaveState(sqlite3rbu *p, int eStage){
223013 if( p->rc==SQLITE_OK0 || p->rc==SQLITE_DONE101 ){
223014 sqlite3_stmt *pInsert = 0;
223015 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
223016 int rc;
223017
223018 assert( p->zErrmsg==0 )((void) (0));
223019 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
223020 sqlite3_mprintf(
223021 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
223022 "(%d, %d), "
223023 "(%d, %Q), "
223024 "(%d, %Q), "
223025 "(%d, %d), "
223026 "(%d, %lld), "
223027 "(%d, %lld), "
223028 "(%d, %lld), "
223029 "(%d, %lld), "
223030 "(%d, %lld), "
223031 "(%d, %Q) ",
223032 p->zStateDb,
223033 RBU_STATE_STAGE, eStage,
223034 RBU_STATE_TBL, p->objiter.zTbl,
223035 RBU_STATE_IDX, p->objiter.zIdx,
223036 RBU_STATE_ROW, p->nStep,
223037 RBU_STATE_PROGRESS, p->nProgress,
223038 RBU_STATE_CKPT, p->iWalCksum,
223039 RBU_STATE_COOKIE, (i64)pFd->iCookie,
223040 RBU_STATE_OALSZ, p->iOalSz,
223041 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
223042 RBU_STATE_DATATBL, p->objiter.zDataTbl
223043 )
223044 );
223045 assert( pInsert==0 || rc==SQLITE_OK )((void) (0));
223046
223047 if( rc==SQLITE_OK0 ){
223048 sqlite3_step(pInsert);
223049 rc = sqlite3_finalize(pInsert);
223050 }
223051 if( rc!=SQLITE_OK0 ) p->rc = rc;
223052 }
223053}
223054
223055
223056/*
223057** The second argument passed to this function is the name of a PRAGMA
223058** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
223059** This function executes the following on sqlite3rbu.dbRbu:
223060**
223061** "PRAGMA main.$zPragma"
223062**
223063** where $zPragma is the string passed as the second argument, then
223064** on sqlite3rbu.dbMain:
223065**
223066** "PRAGMA main.$zPragma = $val"
223067**
223068** where $val is the value returned by the first PRAGMA invocation.
223069**
223070** In short, it copies the value of the specified PRAGMA setting from
223071** dbRbu to dbMain.
223072*/
223073static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
223074 if( p->rc==SQLITE_OK0 ){
223075 sqlite3_stmt *pPragma = 0;
223076 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
223077 sqlite3_mprintf("PRAGMA main.%s", zPragma)
223078 );
223079 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pPragma) ){
223080 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
223081 zPragma, sqlite3_column_int(pPragma, 0)
223082 );
223083 }
223084 rbuFinalize(p, pPragma);
223085 }
223086}
223087
223088/*
223089** The RBU handle passed as the only argument has just been opened and
223090** the state database is empty. If this RBU handle was opened for an
223091** RBU vacuum operation, create the schema in the target db.
223092*/
223093static void rbuCreateTargetSchema(sqlite3rbu *p){
223094 sqlite3_stmt *pSql = 0;
223095 sqlite3_stmt *pInsert = 0;
223096
223097 assert( rbuIsVacuum(p) )((void) (0));
223098 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
223099 if( p->rc==SQLITE_OK0 ){
223100 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
223101 "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
223102 " AND name!='sqlite_sequence' "
223103 " ORDER BY type DESC"
223104 );
223105 }
223106
223107 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
223108 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
223109 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
223110 }
223111 rbuFinalize(p, pSql);
223112 if( p->rc!=SQLITE_OK0 ) return;
223113
223114 if( p->rc==SQLITE_OK0 ){
223115 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
223116 "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
223117 );
223118 }
223119
223120 if( p->rc==SQLITE_OK0 ){
223121 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
223122 "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
223123 );
223124 }
223125
223126 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
223127 int i;
223128 for(i=0; i<5; i++){
223129 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
223130 }
223131 sqlite3_step(pInsert);
223132 p->rc = sqlite3_reset(pInsert);
223133 }
223134 if( p->rc==SQLITE_OK0 ){
223135 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
223136 }
223137
223138 rbuFinalize(p, pSql);
223139 rbuFinalize(p, pInsert);
223140}
223141
223142/*
223143** Step the RBU object.
223144*/
223145SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
223146 if( p ){
223147 switch( p->eStage ){
223148 case RBU_STAGE_OAL: {
223149 RbuObjIter *pIter = &p->objiter;
223150
223151 /* If this is an RBU vacuum operation and the state table was empty
223152 ** when this handle was opened, create the target database schema. */
223153 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK0 ){
223154 rbuCreateTargetSchema(p);
223155 rbuCopyPragma(p, "user_version");
223156 rbuCopyPragma(p, "application_id");
223157 }
223158
223159 while( p->rc==SQLITE_OK0 && pIter->zTbl ){
223160
223161 if( pIter->bCleanup ){
223162 /* Clean up the rbu_tmp_xxx table for the previous table. It
223163 ** cannot be dropped as there are currently active SQL statements.
223164 ** But the contents can be deleted. */
223165 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
223166 rbuMPrintfExec(p, p->dbRbu,
223167 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
223168 );
223169 }
223170 }else{
223171 rbuObjIterPrepareAll(p, pIter, 0);
223172
223173 /* Advance to the next row to process. */
223174 if( p->rc==SQLITE_OK0 ){
223175 int rc = sqlite3_step(pIter->pSelect);
223176 if( rc==SQLITE_ROW100 ){
223177 p->nProgress++;
223178 p->nStep++;
223179 return rbuStep(p);
223180 }
223181 p->rc = sqlite3_reset(pIter->pSelect);
223182 p->nStep = 0;
223183 }
223184 }
223185
223186 rbuObjIterNext(p, pIter);
223187 }
223188
223189 if( p->rc==SQLITE_OK0 ){
223190 assert( pIter->zTbl==0 )((void) (0));
223191 rbuSaveState(p, RBU_STAGE_MOVE);
223192 rbuIncrSchemaCookie(p);
223193 if( p->rc==SQLITE_OK0 ){
223194 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
223195 }
223196 if( p->rc==SQLITE_OK0 ){
223197 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
223198 }
223199 p->eStage = RBU_STAGE_MOVE;
223200 }
223201 break;
223202 }
223203
223204 case RBU_STAGE_MOVE: {
223205 if( p->rc==SQLITE_OK0 ){
223206 rbuMoveOalFile(p);
223207 p->nProgress++;
223208 }
223209 break;
223210 }
223211
223212 case RBU_STAGE_CKPT: {
223213 if( p->rc==SQLITE_OK0 ){
223214 if( p->nStep>=p->nFrame ){
223215 sqlite3_file *pDb = p->pTargetFd->pReal;
223216
223217 /* Sync the db file */
223218 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
223219
223220 /* Update nBackfill */
223221 if( p->rc==SQLITE_OK0 ){
223222 void volatile *ptr;
223223 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
223224 if( p->rc==SQLITE_OK0 ){
223225 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
223226 }
223227 }
223228
223229 if( p->rc==SQLITE_OK0 ){
223230 p->eStage = RBU_STAGE_DONE;
223231 p->rc = SQLITE_DONE101;
223232 }
223233 }else{
223234 /* At one point the following block copied a single frame from the
223235 ** wal file to the database file. So that one call to sqlite3rbu_step()
223236 ** checkpointed a single frame.
223237 **
223238 ** However, if the sector-size is larger than the page-size, and the
223239 ** application calls sqlite3rbu_savestate() or close() immediately
223240 ** after this step, then rbu_step() again, then a power failure occurs,
223241 ** then the database page written here may be damaged. Work around
223242 ** this by checkpointing frames until the next page in the aFrame[]
223243 ** lies on a different disk sector to the current one. */
223244 u32 iSector;
223245 do{
223246 RbuFrame *pFrame = &p->aFrame[p->nStep];
223247 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
223248 rbuCheckpointFrame(p, pFrame);
223249 p->nStep++;
223250 }while( p->nStep<p->nFrame
223251 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
223252 && p->rc==SQLITE_OK0
223253 );
223254 }
223255 p->nProgress++;
223256 }
223257 break;
223258 }
223259
223260 default:
223261 break;
223262 }
223263 return p->rc;
223264 }else{
223265 return SQLITE_NOMEM7;
223266 }
223267}
223268
223269/*
223270** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
223271** otherwise. Either or both argument may be NULL. Two NULL values are
223272** considered equal, and NULL is considered distinct from all other values.
223273*/
223274static int rbuStrCompare(const char *z1, const char *z2){
223275 if( z1==0 && z2==0 ) return 0;
223276 if( z1==0 || z2==0 ) return 1;
223277 return (sqlite3_stricmp(z1, z2)!=0);
223278}
223279
223280/*
223281** This function is called as part of sqlite3rbu_open() when initializing
223282** an rbu handle in OAL stage. If the rbu update has not started (i.e.
223283** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
223284** things so that the next call to sqlite3rbu_step() continues on from
223285** where the previous rbu handle left off.
223286**
223287** If an error occurs, an error code and error message are left in the
223288** rbu handle passed as the first argument.
223289*/
223290static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
223291 assert( p->rc==SQLITE_OK )((void) (0));
223292 if( pState->zTbl ){
223293 RbuObjIter *pIter = &p->objiter;
223294 int rc = SQLITE_OK0;
223295
223296 while( rc==SQLITE_OK0 && pIter->zTbl && (pIter->bCleanup
223297 || rbuStrCompare(pIter->zIdx, pState->zIdx)
223298 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
223299 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
223300 )){
223301 rc = rbuObjIterNext(p, pIter);
223302 }
223303
223304 if( rc==SQLITE_OK0 && !pIter->zTbl ){
223305 rc = SQLITE_ERROR1;
223306 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
223307 }
223308
223309 if( rc==SQLITE_OK0 ){
223310 p->nStep = pState->nRow;
223311 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
223312 }
223313
223314 p->rc = rc;
223315 }
223316}
223317
223318/*
223319** If there is a "*-oal" file in the file-system corresponding to the
223320** target database in the file-system, delete it. If an error occurs,
223321** leave an error code and error message in the rbu handle.
223322*/
223323static void rbuDeleteOalFile(sqlite3rbu *p){
223324 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
223325 if( zOal ){
223326 sqlite3_vfs *pVfs = 0;
223327 sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_VFS_POINTER27, &pVfs);
223328 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
223329 pVfs->xDelete(pVfs, zOal, 0);
223330 sqlite3_free(zOal);
223331 }
223332}
223333
223334/*
223335** Allocate a private rbu VFS for the rbu handle passed as the only
223336** argument. This VFS will be used unless the call to sqlite3rbu_open()
223337** specified a URI with a vfs=? option in place of a target database
223338** file name.
223339*/
223340static void rbuCreateVfs(sqlite3rbu *p){
223341 int rnd;
223342 char zRnd[64];
223343
223344 assert( p->rc==SQLITE_OK )((void) (0));
223345 sqlite3_randomness(sizeof(int), (void*)&rnd);
223346 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
223347 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
223348 if( p->rc==SQLITE_OK0 ){
223349 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
223350 assert( pVfs )((void) (0));
223351 p->zVfsName = pVfs->zName;
223352 ((rbu_vfs*)pVfs)->pRbu = p;
223353 }
223354}
223355
223356/*
223357** Destroy the private VFS created for the rbu handle passed as the only
223358** argument by an earlier call to rbuCreateVfs().
223359*/
223360static void rbuDeleteVfs(sqlite3rbu *p){
223361 if( p->zVfsName ){
223362 sqlite3rbu_destroy_vfs(p->zVfsName);
223363 p->zVfsName = 0;
223364 }
223365}
223366
223367/*
223368** This user-defined SQL function is invoked with a single argument - the
223369** name of a table expected to appear in the target database. It returns
223370** the number of auxilliary indexes on the table.
223371*/
223372static void rbuIndexCntFunc(
223373 sqlite3_context *pCtx,
223374 int nVal,
223375 sqlite3_value **apVal
223376){
223377 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
223378 sqlite3_stmt *pStmt = 0;
223379 char *zErrmsg = 0;
223380 int rc;
223381 sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
223382
223383 assert( nVal==1 )((void) (0));
223384 UNUSED_PARAMETER(nVal)(void)(nVal);
223385
223386 rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
223387 sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
223388 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
223389 );
223390 if( rc!=SQLITE_OK0 ){
223391 sqlite3_result_error(pCtx, zErrmsg, -1);
223392 }else{
223393 int nIndex = 0;
223394 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
223395 nIndex = sqlite3_column_int(pStmt, 0);
223396 }
223397 rc = sqlite3_finalize(pStmt);
223398 if( rc==SQLITE_OK0 ){
223399 sqlite3_result_int(pCtx, nIndex);
223400 }else{
223401 sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
223402 }
223403 }
223404
223405 sqlite3_free(zErrmsg);
223406}
223407
223408/*
223409** If the RBU database contains the rbu_count table, use it to initialize
223410** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
223411** is assumed to contain the same columns as:
223412**
223413** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
223414**
223415** There should be one row in the table for each data_xxx table in the
223416** database. The 'tbl' column should contain the name of a data_xxx table,
223417** and the cnt column the number of rows it contains.
223418**
223419** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
223420** for all rows in the rbu_count table, where nIndex is the number of
223421** indexes on the corresponding target database table.
223422*/
223423static void rbuInitPhaseOneSteps(sqlite3rbu *p){
223424 if( p->rc==SQLITE_OK0 ){
223425 sqlite3_stmt *pStmt = 0;
223426 int bExists = 0; /* True if rbu_count exists */
223427
223428 p->nPhaseOneStep = -1;
223429
223430 p->rc = sqlite3_create_function(p->dbRbu,
223431 "rbu_index_cnt", 1, SQLITE_UTF81, (void*)p, rbuIndexCntFunc, 0, 0
223432 );
223433
223434 /* Check for the rbu_count table. If it does not exist, or if an error
223435 ** occurs, nPhaseOneStep will be left set to -1. */
223436 if( p->rc==SQLITE_OK0 ){
223437 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
223438 "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
223439 );
223440 }
223441 if( p->rc==SQLITE_OK0 ){
223442 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
223443 bExists = 1;
223444 }
223445 p->rc = sqlite3_finalize(pStmt);
223446 }
223447
223448 if( p->rc==SQLITE_OK0 && bExists ){
223449 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
223450 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
223451 "FROM rbu_count"
223452 );
223453 if( p->rc==SQLITE_OK0 ){
223454 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
223455 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
223456 }
223457 p->rc = sqlite3_finalize(pStmt);
223458 }
223459 }
223460 }
223461}
223462
223463
223464static sqlite3rbu *openRbuHandle(
223465 const char *zTarget,
223466 const char *zRbu,
223467 const char *zState
223468){
223469 sqlite3rbu *p;
223470 size_t nTarget = zTarget ? strlen(zTarget) : 0;
223471 size_t nRbu = strlen(zRbu);
223472 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
223473
223474 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
223475 if( p ){
223476 RbuState *pState = 0;
223477
223478 /* Create the custom VFS. */
223479 memset(p, 0, sizeof(sqlite3rbu));
223480 sqlite3rbu_rename_handler(p, 0, 0);
223481 rbuCreateVfs(p);
223482
223483 /* Open the target, RBU and state databases */
223484 if( p->rc==SQLITE_OK0 ){
223485 char *pCsr = (char*)&p[1];
223486 int bRetry = 0;
223487 if( zTarget ){
223488 p->zTarget = pCsr;
223489 memcpy(p->zTarget, zTarget, nTarget+1);
223490 pCsr += nTarget+1;
223491 }
223492 p->zRbu = pCsr;
223493 memcpy(p->zRbu, zRbu, nRbu+1);
223494 pCsr += nRbu+1;
223495 if( zState ){
223496 p->zState = rbuMPrintf(p, "%s", zState);
223497 }
223498
223499 /* If the first attempt to open the database file fails and the bRetry
223500 ** flag it set, this means that the db was not opened because it seemed
223501 ** to be a wal-mode db. But, this may have happened due to an earlier
223502 ** RBU vacuum operation leaving an old wal file in the directory.
223503 ** If this is the case, it will have been checkpointed and deleted
223504 ** when the handle was closed and a second attempt to open the
223505 ** database may succeed. */
223506 rbuOpenDatabase(p, 0, &bRetry);
223507 if( bRetry ){
223508 rbuOpenDatabase(p, 0, 0);
223509 }
223510 }
223511
223512 if( p->rc==SQLITE_OK0 ){
223513 pState = rbuLoadState(p);
223514 assert( pState || p->rc!=SQLITE_OK )((void) (0));
223515 if( p->rc==SQLITE_OK0 ){
223516
223517 if( pState->eStage==0 ){
223518 rbuDeleteOalFile(p);
223519 rbuInitPhaseOneSteps(p);
223520 p->eStage = RBU_STAGE_OAL;
223521 }else{
223522 p->eStage = pState->eStage;
223523 p->nPhaseOneStep = pState->nPhaseOneStep;
223524 }
223525 p->nProgress = pState->nProgress;
223526 p->iOalSz = pState->iOalSz;
223527 }
223528 }
223529 assert( p->rc!=SQLITE_OK || p->eStage!=0 )((void) (0));
223530
223531 if( p->rc==SQLITE_OK0 && p->pTargetFd->pWalFd ){
223532 if( p->eStage==RBU_STAGE_OAL ){
223533 p->rc = SQLITE_ERROR1;
223534 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
223535 }else if( p->eStage==RBU_STAGE_MOVE ){
223536 p->eStage = RBU_STAGE_CKPT;
223537 p->nStep = 0;
223538 }
223539 }
223540
223541 if( p->rc==SQLITE_OK0
223542 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
223543 && pState->eStage!=0
223544 ){
223545 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
223546 if( pFd->iCookie!=pState->iCookie ){
223547 /* At this point (pTargetFd->iCookie) contains the value of the
223548 ** change-counter cookie (the thing that gets incremented when a
223549 ** transaction is committed in rollback mode) currently stored on
223550 ** page 1 of the database file. */
223551 p->rc = SQLITE_BUSY5;
223552 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
223553 (rbuIsVacuum(p) ? "vacuum" : "update")
223554 );
223555 }
223556 }
223557
223558 if( p->rc==SQLITE_OK0 ){
223559 if( p->eStage==RBU_STAGE_OAL ){
223560 sqlite3 *db = p->dbMain;
223561 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
223562
223563 /* Point the object iterator at the first object */
223564 if( p->rc==SQLITE_OK0 ){
223565 p->rc = rbuObjIterFirst(p, &p->objiter);
223566 }
223567
223568 /* If the RBU database contains no data_xxx tables, declare the RBU
223569 ** update finished. */
223570 if( p->rc==SQLITE_OK0 && p->objiter.zTbl==0 ){
223571 p->rc = SQLITE_DONE101;
223572 p->eStage = RBU_STAGE_DONE;
223573 }else{
223574 if( p->rc==SQLITE_OK0 && pState->eStage==0 && rbuIsVacuum(p) ){
223575 rbuCopyPragma(p, "page_size");
223576 rbuCopyPragma(p, "auto_vacuum");
223577 }
223578
223579 /* Open transactions both databases. The *-oal file is opened or
223580 ** created at this point. */
223581 if( p->rc==SQLITE_OK0 ){
223582 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
223583 }
223584
223585 /* Check if the main database is a zipvfs db. If it is, set the upper
223586 ** level pager to use "journal_mode=off". This prevents it from
223587 ** generating a large journal using a temp file. */
223588 if( p->rc==SQLITE_OK0 ){
223589 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS25, 0);
223590 if( frc==SQLITE_OK0 ){
223591 p->rc = sqlite3_exec(
223592 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
223593 }
223594 }
223595
223596 if( p->rc==SQLITE_OK0 ){
223597 rbuSetupOal(p, pState);
223598 }
223599 }
223600 }else if( p->eStage==RBU_STAGE_MOVE ){
223601 /* no-op */
223602 }else if( p->eStage==RBU_STAGE_CKPT ){
223603 if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){
223604 /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
223605 ** and an incremental checkpoint is being resumed, attempt an
223606 ** exclusive lock on the db file. If this fails, so be it. */
223607 p->eStage = RBU_STAGE_DONE;
223608 rbuLockDatabase(p->dbMain);
223609 p->eStage = RBU_STAGE_CKPT;
223610 }
223611 rbuSetupCheckpoint(p, pState);
223612 }else if( p->eStage==RBU_STAGE_DONE ){
223613 p->rc = SQLITE_DONE101;
223614 }else{
223615 p->rc = SQLITE_CORRUPT11;
223616 }
223617 }
223618
223619 rbuFreeState(pState);
223620 }
223621
223622 return p;
223623}
223624
223625/*
223626** Allocate and return an RBU handle with all fields zeroed except for the
223627** error code, which is set to SQLITE_MISUSE.
223628*/
223629static sqlite3rbu *rbuMisuseError(void){
223630 sqlite3rbu *pRet;
223631 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
223632 if( pRet ){
223633 memset(pRet, 0, sizeof(sqlite3rbu));
223634 pRet->rc = SQLITE_MISUSE21;
223635 }
223636 return pRet;
223637}
223638
223639/*
223640** Open and return a new RBU handle.
223641*/
223642SQLITE_API sqlite3rbu *sqlite3rbu_open(
223643 const char *zTarget,
223644 const char *zRbu,
223645 const char *zState
223646){
223647 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
223648 return openRbuHandle(zTarget, zRbu, zState);
223649}
223650
223651/*
223652** Open a handle to begin or resume an RBU VACUUM operation.
223653*/
223654SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
223655 const char *zTarget,
223656 const char *zState
223657){
223658 if( zTarget==0 ){ return rbuMisuseError(); }
223659 if( zState ){
223660 size_t n = strlen(zState);
223661 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
223662 return rbuMisuseError();
223663 }
223664 }
223665 /* TODO: Check that both arguments are non-NULL */
223666 return openRbuHandle(0, zTarget, zState);
223667}
223668
223669/*
223670** Return the database handle used by pRbu.
223671*/
223672SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
223673 sqlite3 *db = 0;
223674 if( pRbu ){
223675 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
223676 }
223677 return db;
223678}
223679
223680
223681/*
223682** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
223683** then edit any error message string so as to remove all occurrences of
223684** the pattern "rbu_imp_[0-9]*".
223685*/
223686static void rbuEditErrmsg(sqlite3rbu *p){
223687 if( p->rc==SQLITE_CONSTRAINT19 && p->zErrmsg ){
223688 unsigned int i;
223689 size_t nErrmsg = strlen(p->zErrmsg);
223690 for(i=0; i<(nErrmsg-8); i++){
223691 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
223692 int nDel = 8;
223693 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
223694 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
223695 nErrmsg -= nDel;
223696 }
223697 }
223698 }
223699}
223700
223701/*
223702** Close the RBU handle.
223703*/
223704SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
223705 int rc;
223706 if( p ){
223707
223708 /* Commit the transaction to the *-oal file. */
223709 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
223710 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
223711 }
223712
223713 /* Sync the db file if currently doing an incremental checkpoint */
223714 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
223715 sqlite3_file *pDb = p->pTargetFd->pReal;
223716 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
223717 }
223718
223719 rbuSaveState(p, p->eStage);
223720
223721 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
223722 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
223723 }
223724
223725 /* Close any open statement handles. */
223726 rbuObjIterFinalize(&p->objiter);
223727
223728 /* If this is an RBU vacuum handle and the vacuum has either finished
223729 ** successfully or encountered an error, delete the contents of the
223730 ** state table. This causes the next call to sqlite3rbu_vacuum()
223731 ** specifying the current target and state databases to start a new
223732 ** vacuum from scratch. */
223733 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK0 && p->dbRbu ){
223734 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
223735 if( p->rc==SQLITE_DONE101 && rc2!=SQLITE_OK0 ) p->rc = rc2;
223736 }
223737
223738 /* Close the open database handle and VFS object. */
223739 sqlite3_close(p->dbRbu);
223740 sqlite3_close(p->dbMain);
223741 assert( p->szTemp==0 )((void) (0));
223742 rbuDeleteVfs(p);
223743 sqlite3_free(p->aBuf);
223744 sqlite3_free(p->aFrame);
223745
223746 rbuEditErrmsg(p);
223747 rc = p->rc;
223748 if( pzErrmsg ){
223749 *pzErrmsg = p->zErrmsg;
223750 }else{
223751 sqlite3_free(p->zErrmsg);
223752 }
223753 sqlite3_free(p->zState);
223754 sqlite3_free(p);
223755 }else{
223756 rc = SQLITE_NOMEM7;
223757 *pzErrmsg = 0;
223758 }
223759 return rc;
223760}
223761
223762/*
223763** Return the total number of key-value operations (inserts, deletes or
223764** updates) that have been performed on the target database since the
223765** current RBU update was started.
223766*/
223767SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
223768 return pRbu->nProgress;
223769}
223770
223771/*
223772** Return permyriadage progress indications for the two main stages of
223773** an RBU update.
223774*/
223775SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
223776 const int MAX_PROGRESS = 10000;
223777 switch( p->eStage ){
223778 case RBU_STAGE_OAL:
223779 if( p->nPhaseOneStep>0 ){
223780 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
223781 }else{
223782 *pnOne = -1;
223783 }
223784 *pnTwo = 0;
223785 break;
223786
223787 case RBU_STAGE_MOVE:
223788 *pnOne = MAX_PROGRESS;
223789 *pnTwo = 0;
223790 break;
223791
223792 case RBU_STAGE_CKPT:
223793 *pnOne = MAX_PROGRESS;
223794 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
223795 break;
223796
223797 case RBU_STAGE_DONE:
223798 *pnOne = MAX_PROGRESS;
223799 *pnTwo = MAX_PROGRESS;
223800 break;
223801
223802 default:
223803 assert( 0 )((void) (0));
223804 }
223805}
223806
223807/*
223808** Return the current state of the RBU vacuum or update operation.
223809*/
223810SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
223811 int aRes[] = {
223812 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
223813 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
223814 };
223815
223816 assert( RBU_STAGE_OAL==1 )((void) (0));
223817 assert( RBU_STAGE_MOVE==2 )((void) (0));
223818 assert( RBU_STAGE_CKPT==4 )((void) (0));
223819 assert( RBU_STAGE_DONE==5 )((void) (0));
223820 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL )((void) (0));
223821 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE )((void) (0));
223822 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT )((void) (0));
223823 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE )((void) (0));
223824
223825 if( p->rc!=SQLITE_OK0 && p->rc!=SQLITE_DONE101 ){
223826 return SQLITE_RBU_STATE_ERROR;
223827 }else{
223828 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE )((void) (0));
223829 assert( p->eStage==RBU_STAGE_OAL((void) (0))
223830 || p->eStage==RBU_STAGE_MOVE((void) (0))
223831 || p->eStage==RBU_STAGE_CKPT((void) (0))
223832 || p->eStage==RBU_STAGE_DONE((void) (0))
223833 )((void) (0));
223834 return aRes[p->eStage];
223835 }
223836}
223837
223838SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
223839 int rc = p->rc;
223840 if( rc==SQLITE_DONE101 ) return SQLITE_OK0;
223841
223842 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE )((void) (0));
223843 if( p->eStage==RBU_STAGE_OAL ){
223844 assert( rc!=SQLITE_DONE )((void) (0));
223845 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
223846 }
223847
223848 /* Sync the db file */
223849 if( rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
223850 sqlite3_file *pDb = p->pTargetFd->pReal;
223851 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
223852 }
223853
223854 p->rc = rc;
223855 rbuSaveState(p, p->eStage);
223856 rc = p->rc;
223857
223858 if( p->eStage==RBU_STAGE_OAL ){
223859 assert( rc!=SQLITE_DONE )((void) (0));
223860 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
223861 if( rc==SQLITE_OK0 ){
223862 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
223863 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
223864 }
223865 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
223866 }
223867
223868 p->rc = rc;
223869 return rc;
223870}
223871
223872/*
223873** Default xRename callback for RBU.
223874*/
223875static int xDefaultRename(void *pArg, const char *zOld, const char *zNew){
223876 int rc = SQLITE_OK0;
223877 UNUSED_PARAMETER(pArg)(void)(pArg);
223878#if defined(_WIN32_WCE)
223879 {
223880 LPWSTR zWideOld;
223881 LPWSTR zWideNew;
223882
223883 zWideOld = rbuWinUtf8ToUnicode(zOld);
223884 if( zWideOld ){
223885 zWideNew = rbuWinUtf8ToUnicode(zNew);
223886 if( zWideNew ){
223887 if( MoveFileW(zWideOld, zWideNew) ){
223888 rc = SQLITE_OK0;
223889 }else{
223890 rc = SQLITE_IOERR10;
223891 }
223892 sqlite3_free(zWideNew);
223893 }else{
223894 rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
223895 }
223896 sqlite3_free(zWideOld);
223897 }else{
223898 rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
223899 }
223900 }
223901#else
223902 rc = rename(zOld, zNew) ? SQLITE_IOERR10 : SQLITE_OK0;
223903#endif
223904 return rc;
223905}
223906
223907SQLITE_API void sqlite3rbu_rename_handler(
223908 sqlite3rbu *pRbu,
223909 void *pArg,
223910 int (*xRename)(void *pArg, const char *zOld, const char *zNew)
223911){
223912 if( xRename ){
223913 pRbu->xRename = xRename;
223914 pRbu->pRenameArg = pArg;
223915 }else{
223916 pRbu->xRename = xDefaultRename;
223917 pRbu->pRenameArg = 0;
223918 }
223919}
223920
223921/**************************************************************************
223922** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
223923** of a standard VFS in the following ways:
223924**
223925** 1. Whenever the first page of a main database file is read or
223926** written, the value of the change-counter cookie is stored in
223927** rbu_file.iCookie. Similarly, the value of the "write-version"
223928** database header field is stored in rbu_file.iWriteVer. This ensures
223929** that the values are always trustworthy within an open transaction.
223930**
223931** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
223932** member variable of the associated database file descriptor is set
223933** to point to the new file. A mutex protected linked list of all main
223934** db fds opened using a particular RBU VFS is maintained at
223935** rbu_vfs.pMain to facilitate this.
223936**
223937** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
223938** object can be marked as the target database of an RBU update. This
223939** turns on the following extra special behaviour:
223940**
223941** 3a. If xAccess() is called to check if there exists a *-wal file
223942** associated with an RBU target database currently in RBU_STAGE_OAL
223943** stage (preparing the *-oal file), the following special handling
223944** applies:
223945**
223946** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
223947** target database may not be in wal mode already.
223948**
223949** * if the *-wal file does not exist, set the output parameter to
223950** non-zero (to tell SQLite that it does exist) anyway.
223951**
223952** Then, when xOpen() is called to open the *-wal file associated with
223953** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
223954** file, the rbu vfs opens the corresponding *-oal file instead.
223955**
223956** 3b. The *-shm pages returned by xShmMap() for a target db file in
223957** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
223958** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
223959** are no-ops on target database files in RBU_STAGE_OAL mode. This is
223960** because assert() statements in some VFS implementations fail if
223961** xShmLock() is called before xShmMap().
223962**
223963** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
223964** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
223965** fails with an SQLITE_BUSY error. This is to stop RBU connections
223966** from automatically checkpointing a *-wal (or *-oal) file from within
223967** sqlite3_close().
223968**
223969** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
223970** all xWrite() calls on the target database file perform no IO.
223971** Instead the frame and page numbers that would be read and written
223972** are recorded. Additionally, successful attempts to obtain exclusive
223973** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
223974** database file are recorded. xShmLock() calls to unlock the same
223975** locks are no-ops (so that once obtained, these locks are never
223976** relinquished). Finally, calls to xSync() on the target database
223977** file fail with SQLITE_NOTICE errors.
223978*/
223979
223980static void rbuUnlockShm(rbu_file *p){
223981 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
223982 if( p->pRbu ){
223983 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
223984 int i;
223985 for(i=0; i<SQLITE_SHM_NLOCK8;i++){
223986 if( (1<<i) & p->pRbu->mLock ){
223987 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK1|SQLITE_SHM_EXCLUSIVE8);
223988 }
223989 }
223990 p->pRbu->mLock = 0;
223991 }
223992}
223993
223994/*
223995*/
223996static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
223997 sqlite3rbu *pRbu = pFd->pRbu;
223998 i64 nDiff = nNew - pFd->sz;
223999 pRbu->szTemp += nDiff;
224000 pFd->sz = nNew;
224001 assert( pRbu->szTemp>=0 )((void) (0));
224002 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL13;
224003 return SQLITE_OK0;
224004}
224005
224006/*
224007** Add an item to the main-db lists, if it is not already present.
224008**
224009** There are two main-db lists. One for all file descriptors, and one
224010** for all file descriptors with rbu_file.pDb!=0. If the argument has
224011** rbu_file.pDb!=0, then it is assumed to already be present on the
224012** main list and is only added to the pDb!=0 list.
224013*/
224014static void rbuMainlistAdd(rbu_file *p){
224015 rbu_vfs *pRbuVfs = p->pRbuVfs;
224016 rbu_file *pIter;
224017 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) )((void) (0));
224018 sqlite3_mutex_enter(pRbuVfs->mutex);
224019 if( p->pRbu==0 ){
224020 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
224021 p->pMainNext = pRbuVfs->pMain;
224022 pRbuVfs->pMain = p;
224023 }else{
224024 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
224025 if( pIter==0 ){
224026 p->pMainRbuNext = pRbuVfs->pMainRbu;
224027 pRbuVfs->pMainRbu = p;
224028 }
224029 }
224030 sqlite3_mutex_leave(pRbuVfs->mutex);
224031}
224032
224033/*
224034** Remove an item from the main-db lists.
224035*/
224036static void rbuMainlistRemove(rbu_file *p){
224037 rbu_file **pp;
224038 sqlite3_mutex_enter(p->pRbuVfs->mutex);
224039 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
224040 if( *pp ) *pp = p->pMainNext;
224041 p->pMainNext = 0;
224042 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
224043 if( *pp ) *pp = p->pMainRbuNext;
224044 p->pMainRbuNext = 0;
224045 sqlite3_mutex_leave(p->pRbuVfs->mutex);
224046}
224047
224048/*
224049** Given that zWal points to a buffer containing a wal file name passed to
224050** either the xOpen() or xAccess() VFS method, search the main-db list for
224051** a file-handle opened by the same database connection on the corresponding
224052** database file.
224053**
224054** If parameter bRbu is true, only search for file-descriptors with
224055** rbu_file.pDb!=0.
224056*/
224057static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
224058 rbu_file *pDb;
224059 sqlite3_mutex_enter(pRbuVfs->mutex);
224060 if( bRbu ){
224061 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
224062 }else{
224063 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
224064 }
224065 sqlite3_mutex_leave(pRbuVfs->mutex);
224066 return pDb;
224067}
224068
224069/*
224070** Close an rbu file.
224071*/
224072static int rbuVfsClose(sqlite3_file *pFile){
224073 rbu_file *p = (rbu_file*)pFile;
224074 int rc;
224075 int i;
224076
224077 /* Free the contents of the apShm[] array. And the array itself. */
224078 for(i=0; i<p->nShm; i++){
224079 sqlite3_free(p->apShm[i]);
224080 }
224081 sqlite3_free(p->apShm);
224082 p->apShm = 0;
224083 sqlite3_free(p->zDel);
224084
224085 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
224086 const sqlite3_io_methods *pMeth = p->pReal->pMethods;
224087 rbuMainlistRemove(p);
224088 rbuUnlockShm(p);
224089 if( pMeth->iVersion>1 && pMeth->xShmUnmap ){
224090 pMeth->xShmUnmap(p->pReal, 0);
224091 }
224092 }
224093 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
224094 rbuUpdateTempSize(p, 0);
224095 }
224096 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p )((void) (0));
224097
224098 /* Close the underlying file handle */
224099 rc = p->pReal->pMethods->xClose(p->pReal);
224100 return rc;
224101}
224102
224103
224104/*
224105** Read and return an unsigned 32-bit big-endian integer from the buffer
224106** passed as the only argument.
224107*/
224108static u32 rbuGetU32(u8 *aBuf){
224109 return ((u32)aBuf[0] << 24)
224110 + ((u32)aBuf[1] << 16)
224111 + ((u32)aBuf[2] << 8)
224112 + ((u32)aBuf[3]);
224113}
224114
224115/*
224116** Write an unsigned 32-bit value in big-endian format to the supplied
224117** buffer.
224118*/
224119static void rbuPutU32(u8 *aBuf, u32 iVal){
224120 aBuf[0] = (iVal >> 24) & 0xFF;
224121 aBuf[1] = (iVal >> 16) & 0xFF;
224122 aBuf[2] = (iVal >> 8) & 0xFF;
224123 aBuf[3] = (iVal >> 0) & 0xFF;
224124}
224125
224126static void rbuPutU16(u8 *aBuf, u16 iVal){
224127 aBuf[0] = (iVal >> 8) & 0xFF;
224128 aBuf[1] = (iVal >> 0) & 0xFF;
224129}
224130
224131/*
224132** Read data from an rbuVfs-file.
224133*/
224134static int rbuVfsRead(
224135 sqlite3_file *pFile,
224136 void *zBuf,
224137 int iAmt,
224138 sqlite_int64 iOfst
224139){
224140 rbu_file *p = (rbu_file*)pFile;
224141 sqlite3rbu *pRbu = p->pRbu;
224142 int rc;
224143
224144 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
224145 assert( p->openFlags & SQLITE_OPEN_WAL )((void) (0));
224146 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
224147 }else{
224148 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
224149 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
224150 && iOfst>=pRbu->iOalSz
224151 ){
224152 rc = SQLITE_OK0;
224153 memset(zBuf, 0, iAmt);
224154 }else{
224155 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
224156#if 1
224157 /* If this is being called to read the first page of the target
224158 ** database as part of an rbu vacuum operation, synthesize the
224159 ** contents of the first page if it does not yet exist. Otherwise,
224160 ** SQLite will not check for a *-wal file. */
224161 if( pRbu && rbuIsVacuum(pRbu)
224162 && rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) && iOfst==0
224163 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
224164 && pRbu->rc==SQLITE_OK0
224165 ){
224166 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
224167 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
224168 if( rc==SQLITE_OK0 ){
224169 u8 *aBuf = (u8*)zBuf;
224170 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
224171 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
224172 rbuPutU32(&aBuf[36], 0); /* number of free pages */
224173 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
224174 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
224175 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
224176
224177 if( iAmt>100 ){
224178 memset(&aBuf[100], 0, iAmt-100);
224179 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
224180 aBuf[100] = 0x0D;
224181 }
224182 }
224183 }
224184#endif
224185 }
224186 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
224187 /* These look like magic numbers. But they are stable, as they are part
224188 ** of the definition of the SQLite file format, which may not change. */
224189 u8 *pBuf = (u8*)zBuf;
224190 p->iCookie = rbuGetU32(&pBuf[24]);
224191 p->iWriteVer = pBuf[19];
224192 }
224193 }
224194 return rc;
224195}
224196
224197/*
224198** Write data to an rbuVfs-file.
224199*/
224200static int rbuVfsWrite(
224201 sqlite3_file *pFile,
224202 const void *zBuf,
224203 int iAmt,
224204 sqlite_int64 iOfst
224205){
224206 rbu_file *p = (rbu_file*)pFile;
224207 sqlite3rbu *pRbu = p->pRbu;
224208 int rc;
224209
224210 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
224211 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
224212 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
224213 }else{
224214 if( pRbu ){
224215 if( pRbu->eStage==RBU_STAGE_OAL
224216 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
224217 && iOfst>=pRbu->iOalSz
224218 ){
224219 pRbu->iOalSz = iAmt + iOfst;
224220 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
224221 i64 szNew = iAmt+iOfst;
224222 if( szNew>p->sz ){
224223 rc = rbuUpdateTempSize(p, szNew);
224224 if( rc!=SQLITE_OK0 ) return rc;
224225 }
224226 }
224227 }
224228 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
224229 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
224230 /* These look like magic numbers. But they are stable, as they are part
224231 ** of the definition of the SQLite file format, which may not change. */
224232 u8 *pBuf = (u8*)zBuf;
224233 p->iCookie = rbuGetU32(&pBuf[24]);
224234 p->iWriteVer = pBuf[19];
224235 }
224236 }
224237 return rc;
224238}
224239
224240/*
224241** Truncate an rbuVfs-file.
224242*/
224243static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
224244 rbu_file *p = (rbu_file*)pFile;
224245 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
224246 int rc = rbuUpdateTempSize(p, size);
224247 if( rc!=SQLITE_OK0 ) return rc;
224248 }
224249 return p->pReal->pMethods->xTruncate(p->pReal, size);
224250}
224251
224252/*
224253** Sync an rbuVfs-file.
224254*/
224255static int rbuVfsSync(sqlite3_file *pFile, int flags){
224256 rbu_file *p = (rbu_file *)pFile;
224257 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
224258 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
224259 return SQLITE_NOTICE_RBU(27 | (3<<8));
224260 }
224261 return SQLITE_OK0;
224262 }
224263 return p->pReal->pMethods->xSync(p->pReal, flags);
224264}
224265
224266/*
224267** Return the current file-size of an rbuVfs-file.
224268*/
224269static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
224270 rbu_file *p = (rbu_file *)pFile;
224271 int rc;
224272 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
224273
224274 /* If this is an RBU vacuum operation and this is the target database,
224275 ** pretend that it has at least one page. Otherwise, SQLite will not
224276 ** check for the existance of a *-wal file. rbuVfsRead() contains
224277 ** similar logic. */
224278 if( rc==SQLITE_OK0 && *pSize==0
224279 && p->pRbu && rbuIsVacuum(p->pRbu)
224280 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
224281 ){
224282 *pSize = 1024;
224283 }
224284 return rc;
224285}
224286
224287/*
224288** Lock an rbuVfs-file.
224289*/
224290static int rbuVfsLock(sqlite3_file *pFile, int eLock){
224291 rbu_file *p = (rbu_file*)pFile;
224292 sqlite3rbu *pRbu = p->pRbu;
224293 int rc = SQLITE_OK0;
224294
224295 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
224296 if( eLock==SQLITE_LOCK_EXCLUSIVE4
224297 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
224298 ){
224299 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
224300 ** prevents it from checkpointing the database from sqlite3_close(). */
224301 rc = SQLITE_BUSY5;
224302 }else{
224303 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
224304 }
224305
224306 return rc;
224307}
224308
224309/*
224310** Unlock an rbuVfs-file.
224311*/
224312static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
224313 rbu_file *p = (rbu_file *)pFile;
224314 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
224315}
224316
224317/*
224318** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
224319*/
224320static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
224321 rbu_file *p = (rbu_file *)pFile;
224322 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
224323}
224324
224325/*
224326** File control method. For custom operations on an rbuVfs-file.
224327*/
224328static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
224329 rbu_file *p = (rbu_file *)pFile;
224330 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
224331 int rc;
224332
224333 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)((void) (0))
224334 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)((void) (0))
224335 )((void) (0));
224336 if( op==SQLITE_FCNTL_RBU26 ){
224337 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
224338
224339 /* First try to find another RBU vfs lower down in the vfs stack. If
224340 ** one is found, this vfs will operate in pass-through mode. The lower
224341 ** level vfs will do the special RBU handling. */
224342 rc = xControl(p->pReal, op, pArg);
224343
224344 if( rc==SQLITE_NOTFOUND12 ){
224345 /* Now search for a zipvfs instance lower down in the VFS stack. If
224346 ** one is found, this is an error. */
224347 void *dummy = 0;
224348 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS25, &dummy);
224349 if( rc==SQLITE_OK0 ){
224350 rc = SQLITE_ERROR1;
224351 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
224352 }else if( rc==SQLITE_NOTFOUND12 ){
224353 pRbu->pTargetFd = p;
224354 p->pRbu = pRbu;
224355 rbuMainlistAdd(p);
224356 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
224357 rc = SQLITE_OK0;
224358 }
224359 }
224360 return rc;
224361 }
224362 else if( op==SQLITE_FCNTL_RBUCNT ){
224363 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
224364 pRbu->nRbu++;
224365 pRbu->pRbuFd = p;
224366 p->bNolock = 1;
224367 }
224368
224369 rc = xControl(p->pReal, op, pArg);
224370 if( rc==SQLITE_OK0 && op==SQLITE_FCNTL_VFSNAME12 ){
224371 rbu_vfs *pRbuVfs = p->pRbuVfs;
224372 char *zIn = *(char**)pArg;
224373 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
224374 *(char**)pArg = zOut;
224375 if( zOut==0 ) rc = SQLITE_NOMEM7;
224376 }
224377
224378 return rc;
224379}
224380
224381/*
224382** Return the sector-size in bytes for an rbuVfs-file.
224383*/
224384static int rbuVfsSectorSize(sqlite3_file *pFile){
224385 rbu_file *p = (rbu_file *)pFile;
224386 return p->pReal->pMethods->xSectorSize(p->pReal);
224387}
224388
224389/*
224390** Return the device characteristic flags supported by an rbuVfs-file.
224391*/
224392static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
224393 rbu_file *p = (rbu_file *)pFile;
224394 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
224395}
224396
224397/*
224398** Take or release a shared-memory lock.
224399*/
224400static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
224401 rbu_file *p = (rbu_file*)pFile;
224402 sqlite3rbu *pRbu = p->pRbu;
224403 int rc = SQLITE_OK0;
224404
224405#ifdef SQLITE_AMALGAMATION1
224406 assert( WAL_CKPT_LOCK==1 )((void) (0));
224407#endif
224408
224409 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
224410 if( pRbu && (
224411 pRbu->eStage==RBU_STAGE_OAL
224412 || pRbu->eStage==RBU_STAGE_MOVE
224413 || pRbu->eStage==RBU_STAGE_DONE
224414 )){
224415 /* Prevent SQLite from taking a shm-lock on the target file when it
224416 ** is supplying heap memory to the upper layer in place of *-shm
224417 ** segments. */
224418 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY5;
224419 }else{
224420 int bCapture = 0;
224421 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
224422 bCapture = 1;
224423 }
224424 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK1) ){
224425 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
224426 if( bCapture && rc==SQLITE_OK0 ){
224427 pRbu->mLock |= ((1<<n) - 1) << ofst;
224428 }
224429 }
224430 }
224431
224432 return rc;
224433}
224434
224435/*
224436** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
224437*/
224438static int rbuVfsShmMap(
224439 sqlite3_file *pFile,
224440 int iRegion,
224441 int szRegion,
224442 int isWrite,
224443 void volatile **pp
224444){
224445 rbu_file *p = (rbu_file*)pFile;
224446 int rc = SQLITE_OK0;
224447 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
224448
224449 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
224450 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
224451 ** instead of a file on disk. */
224452 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
224453 if( eStage==RBU_STAGE_OAL ){
224454 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
224455 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
224456
224457 /* This is an RBU connection that uses its own heap memory for the
224458 ** pages of the *-shm file. Since no other process can have run
224459 ** recovery, the connection must request *-shm pages in order
224460 ** from start to finish. */
224461 assert( iRegion==p->nShm )((void) (0));
224462 if( apNew==0 ){
224463 rc = SQLITE_NOMEM7;
224464 }else{
224465 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
224466 p->apShm = apNew;
224467 p->nShm = iRegion+1;
224468 }
224469
224470 if( rc==SQLITE_OK0 ){
224471 char *pNew = (char*)sqlite3_malloc64(szRegion);
224472 if( pNew==0 ){
224473 rc = SQLITE_NOMEM7;
224474 }else{
224475 memset(pNew, 0, szRegion);
224476 p->apShm[iRegion] = pNew;
224477 }
224478 }
224479
224480 if( rc==SQLITE_OK0 ){
224481 *pp = p->apShm[iRegion];
224482 }else{
224483 *pp = 0;
224484 }
224485 }else{
224486 assert( p->apShm==0 )((void) (0));
224487 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
224488 }
224489
224490 return rc;
224491}
224492
224493/*
224494** Memory barrier.
224495*/
224496static void rbuVfsShmBarrier(sqlite3_file *pFile){
224497 rbu_file *p = (rbu_file *)pFile;
224498 p->pReal->pMethods->xShmBarrier(p->pReal);
224499}
224500
224501/*
224502** The xShmUnmap method.
224503*/
224504static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
224505 rbu_file *p = (rbu_file*)pFile;
224506 int rc = SQLITE_OK0;
224507 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
224508
224509 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
224510 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
224511 /* no-op */
224512 }else{
224513 /* Release the checkpointer and writer locks */
224514 rbuUnlockShm(p);
224515 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
224516 }
224517 return rc;
224518}
224519
224520/*
224521** Open an rbu file handle.
224522*/
224523static int rbuVfsOpen(
224524 sqlite3_vfs *pVfs,
224525 const char *zName,
224526 sqlite3_file *pFile,
224527 int flags,
224528 int *pOutFlags
224529){
224530 static sqlite3_io_methods rbuvfs_io_methods = {
224531 2, /* iVersion */
224532 rbuVfsClose, /* xClose */
224533 rbuVfsRead, /* xRead */
224534 rbuVfsWrite, /* xWrite */
224535 rbuVfsTruncate, /* xTruncate */
224536 rbuVfsSync, /* xSync */
224537 rbuVfsFileSize, /* xFileSize */
224538 rbuVfsLock, /* xLock */
224539 rbuVfsUnlock, /* xUnlock */
224540 rbuVfsCheckReservedLock, /* xCheckReservedLock */
224541 rbuVfsFileControl, /* xFileControl */
224542 rbuVfsSectorSize, /* xSectorSize */
224543 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
224544 rbuVfsShmMap, /* xShmMap */
224545 rbuVfsShmLock, /* xShmLock */
224546 rbuVfsShmBarrier, /* xShmBarrier */
224547 rbuVfsShmUnmap, /* xShmUnmap */
224548 0, 0 /* xFetch, xUnfetch */
224549 };
224550 static sqlite3_io_methods rbuvfs_io_methods1 = {
224551 1, /* iVersion */
224552 rbuVfsClose, /* xClose */
224553 rbuVfsRead, /* xRead */
224554 rbuVfsWrite, /* xWrite */
224555 rbuVfsTruncate, /* xTruncate */
224556 rbuVfsSync, /* xSync */
224557 rbuVfsFileSize, /* xFileSize */
224558 rbuVfsLock, /* xLock */
224559 rbuVfsUnlock, /* xUnlock */
224560 rbuVfsCheckReservedLock, /* xCheckReservedLock */
224561 rbuVfsFileControl, /* xFileControl */
224562 rbuVfsSectorSize, /* xSectorSize */
224563 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
224564 0, 0, 0, 0, 0, 0
224565 };
224566
224567
224568
224569 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
224570 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
224571 rbu_file *pFd = (rbu_file *)pFile;
224572 int rc = SQLITE_OK0;
224573 const char *zOpen = zName;
224574 int oflags = flags;
224575
224576 memset(pFd, 0, sizeof(rbu_file));
224577 pFd->pReal = (sqlite3_file*)&pFd[1];
224578 pFd->pRbuVfs = pRbuVfs;
224579 pFd->openFlags = flags;
224580 if( zName ){
224581 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
224582 /* A main database has just been opened. The following block sets
224583 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
224584 ** the name of the *-wal file this db connection will use. SQLite
224585 ** happens to pass a pointer to this buffer when using xAccess()
224586 ** or xOpen() to operate on the *-wal file. */
224587 pFd->zWal = sqlite3_filename_wal(zName);
224588 }
224589 else if( flags & SQLITE_OPEN_WAL0x00080000 ){
224590 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
224591 if( pDb ){
224592 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
224593 /* This call is to open a *-wal file. Intead, open the *-oal. */
224594 size_t nOpen;
224595 if( rbuIsVacuum(pDb->pRbu) ){
224596 zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
224597 zOpen = sqlite3_filename_wal(zOpen);
224598 }
224599 nOpen = strlen(zOpen);
224600 ((char*)zOpen)[nOpen-3] = 'o';
224601 pFd->pRbu = pDb->pRbu;
224602 }
224603 pDb->pWalFd = pFd;
224604 }
224605 }
224606 }else{
224607 pFd->pRbu = pRbuVfs->pRbu;
224608 }
224609
224610 if( oflags & SQLITE_OPEN_MAIN_DB0x00000100
224611 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
224612 ){
224613 assert( oflags & SQLITE_OPEN_MAIN_DB )((void) (0));
224614 oflags = SQLITE_OPEN_TEMP_DB0x00000200 | SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
224615 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
224616 zOpen = 0;
224617 }
224618
224619 if( rc==SQLITE_OK0 ){
224620 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
224621 }
224622 if( pFd->pReal->pMethods ){
224623 const sqlite3_io_methods *pMeth = pFd->pReal->pMethods;
224624 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
224625 ** pointer and, if the file is a main database file, link it into the
224626 ** mutex protected linked list of all such files. */
224627 if( pMeth->iVersion<2 || pMeth->xShmLock==0 ){
224628 pFile->pMethods = &rbuvfs_io_methods1;
224629 }else{
224630 pFile->pMethods = &rbuvfs_io_methods;
224631 }
224632 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
224633 rbuMainlistAdd(pFd);
224634 }
224635 }else{
224636 sqlite3_free(pFd->zDel);
224637 }
224638
224639 return rc;
224640}
224641
224642/*
224643** Delete the file located at zPath.
224644*/
224645static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
224646 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224647 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
224648}
224649
224650/*
224651** Test for access permissions. Return true if the requested permission
224652** is available, or false otherwise.
224653*/
224654static int rbuVfsAccess(
224655 sqlite3_vfs *pVfs,
224656 const char *zPath,
224657 int flags,
224658 int *pResOut
224659){
224660 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
224661 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
224662 int rc;
224663
224664 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
224665
224666 /* If this call is to check if a *-wal file associated with an RBU target
224667 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
224668 ** the following special handling is activated:
224669 **
224670 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
224671 ** ensures that the RBU extension never tries to update a database
224672 ** in wal mode, even if the first page of the database file has
224673 ** been damaged.
224674 **
224675 ** b) if the *-wal file does not exist, claim that it does anyway,
224676 ** causing SQLite to call xOpen() to open it. This call will also
224677 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
224678 ** file opened instead.
224679 */
224680 if( rc==SQLITE_OK0 && flags==SQLITE_ACCESS_EXISTS0 ){
224681 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
224682 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
224683 assert( pDb->pRbu )((void) (0));
224684 if( *pResOut ){
224685 rc = SQLITE_CANTOPEN14;
224686 }else{
224687 sqlite3_int64 sz = 0;
224688 rc = rbuVfsFileSize(&pDb->base, &sz);
224689 *pResOut = (sz>0);
224690 }
224691 }
224692 }
224693
224694 return rc;
224695}
224696
224697/*
224698** Populate buffer zOut with the full canonical pathname corresponding
224699** to the pathname in zPath. zOut is guaranteed to point to a buffer
224700** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
224701*/
224702static int rbuVfsFullPathname(
224703 sqlite3_vfs *pVfs,
224704 const char *zPath,
224705 int nOut,
224706 char *zOut
224707){
224708 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224709 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
224710}
224711
224712#ifndef SQLITE_OMIT_LOAD_EXTENSION1
224713/*
224714** Open the dynamic library located at zPath and return a handle.
224715*/
224716static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
224717 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224718 return pRealVfs->xDlOpen(pRealVfs, zPath);
224719}
224720
224721/*
224722** Populate the buffer zErrMsg (size nByte bytes) with a human readable
224723** utf-8 string describing the most recent error encountered associated
224724** with dynamic libraries.
224725*/
224726static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
224727 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224728 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
224729}
224730
224731/*
224732** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
224733*/
224734static void (*rbuVfsDlSym(
224735 sqlite3_vfs *pVfs,
224736 void *pArg,
224737 const char *zSym
224738))(void){
224739 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224740 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
224741}
224742
224743/*
224744** Close the dynamic library handle pHandle.
224745*/
224746static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
224747 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224748 pRealVfs->xDlClose(pRealVfs, pHandle);
224749}
224750#endif /* SQLITE_OMIT_LOAD_EXTENSION */
224751
224752/*
224753** Populate the buffer pointed to by zBufOut with nByte bytes of
224754** random data.
224755*/
224756static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
224757 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224758 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
224759}
224760
224761/*
224762** Sleep for nMicro microseconds. Return the number of microseconds
224763** actually slept.
224764*/
224765static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
224766 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224767 return pRealVfs->xSleep(pRealVfs, nMicro);
224768}
224769
224770/*
224771** Return the current time as a Julian Day number in *pTimeOut.
224772*/
224773static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
224774 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
224775 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
224776}
224777
224778/*
224779** No-op.
224780*/
224781static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
224782 UNUSED_PARAMETER(pVfs)(void)(pVfs);
224783 UNUSED_PARAMETER(a)(void)(a);
224784 UNUSED_PARAMETER(b)(void)(b);
224785 return 0;
224786}
224787
224788/*
224789** Deregister and destroy an RBU vfs created by an earlier call to
224790** sqlite3rbu_create_vfs().
224791*/
224792SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
224793 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
224794 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
224795 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
224796 sqlite3_vfs_unregister(pVfs);
224797 sqlite3_free(pVfs);
224798 }
224799}
224800
224801/*
224802** Create an RBU VFS named zName that accesses the underlying file-system
224803** via existing VFS zParent. The new object is registered as a non-default
224804** VFS with SQLite before returning.
224805*/
224806SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
224807
224808 /* Template for VFS */
224809 static sqlite3_vfs vfs_template = {
224810 1, /* iVersion */
224811 0, /* szOsFile */
224812 0, /* mxPathname */
224813 0, /* pNext */
224814 0, /* zName */
224815 0, /* pAppData */
224816 rbuVfsOpen, /* xOpen */
224817 rbuVfsDelete, /* xDelete */
224818 rbuVfsAccess, /* xAccess */
224819 rbuVfsFullPathname, /* xFullPathname */
224820
224821#ifndef SQLITE_OMIT_LOAD_EXTENSION1
224822 rbuVfsDlOpen, /* xDlOpen */
224823 rbuVfsDlError, /* xDlError */
224824 rbuVfsDlSym, /* xDlSym */
224825 rbuVfsDlClose, /* xDlClose */
224826#else
224827 0, 0, 0, 0,
224828#endif
224829
224830 rbuVfsRandomness, /* xRandomness */
224831 rbuVfsSleep, /* xSleep */
224832 rbuVfsCurrentTime, /* xCurrentTime */
224833 rbuVfsGetLastError, /* xGetLastError */
224834 0, /* xCurrentTimeInt64 (version 2) */
224835 0, 0, 0 /* Unimplemented version 3 methods */
224836 };
224837
224838 rbu_vfs *pNew = 0; /* Newly allocated VFS */
224839 int rc = SQLITE_OK0;
224840 size_t nName;
224841 size_t nByte;
224842
224843 nName = strlen(zName);
224844 nByte = sizeof(rbu_vfs) + nName + 1;
224845 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
224846 if( pNew==0 ){
224847 rc = SQLITE_NOMEM7;
224848 }else{
224849 sqlite3_vfs *pParent; /* Parent VFS */
224850 memset(pNew, 0, nByte);
224851 pParent = sqlite3_vfs_find(zParent);
224852 if( pParent==0 ){
224853 rc = SQLITE_NOTFOUND12;
224854 }else{
224855 char *zSpace;
224856 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
224857 pNew->base.mxPathname = pParent->mxPathname;
224858 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
224859 pNew->pRealVfs = pParent;
224860 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
224861 memcpy(zSpace, zName, nName);
224862
224863 /* Allocate the mutex and register the new VFS (not as the default) */
224864 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE1);
224865 if( pNew->mutex==0 ){
224866 rc = SQLITE_NOMEM7;
224867 }else{
224868 rc = sqlite3_vfs_register(&pNew->base, 0);
224869 }
224870 }
224871
224872 if( rc!=SQLITE_OK0 ){
224873 sqlite3_mutex_free(pNew->mutex);
224874 sqlite3_free(pNew);
224875 }
224876 }
224877
224878 return rc;
224879}
224880
224881/*
224882** Configure the aggregate temp file size limit for this RBU handle.
224883*/
224884SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
224885 if( n>=0 ){
224886 pRbu->szTempLimit = n;
224887 }
224888 return pRbu->szTempLimit;
224889}
224890
224891SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
224892 return pRbu->szTemp;
224893}
224894
224895
224896/**************************************************************************/
224897
224898#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
224899
224900/************** End of sqlite3rbu.c ******************************************/
224901/************** Begin file dbstat.c ******************************************/
224902/*
224903** 2010 July 12
224904**
224905** The author disclaims copyright to this source code. In place of
224906** a legal notice, here is a blessing:
224907**
224908** May you do good and not evil.
224909** May you find forgiveness for yourself and forgive others.
224910** May you share freely, never taking more than you give.
224911**
224912******************************************************************************
224913**
224914** This file contains an implementation of the "dbstat" virtual table.
224915**
224916** The dbstat virtual table is used to extract low-level storage
224917** information from an SQLite database in order to implement the
224918** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
224919** for an example implementation.
224920**
224921** Additional information is available on the "dbstat.html" page of the
224922** official SQLite documentation.
224923*/
224924
224925/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
224926#if (defined(SQLITE_ENABLE_DBSTAT_VTAB1) || defined(SQLITE_TEST)) \
224927 && !defined(SQLITE_OMIT_VIRTUALTABLE)
224928
224929/*
224930** The pager and btree modules arrange objects in memory so that there are
224931** always approximately 200 bytes of addressable memory following each page
224932** buffer. This way small buffer overreads caused by corrupt database pages
224933** do not cause undefined behaviour. This module pads each page buffer
224934** by the following number of bytes for the same purpose.
224935*/
224936#define DBSTAT_PAGE_PADDING_BYTES256 256
224937
224938/*
224939** Page paths:
224940**
224941** The value of the 'path' column describes the path taken from the
224942** root-node of the b-tree structure to each page. The value of the
224943** root-node path is '/'.
224944**
224945** The value of the path for the left-most child page of the root of
224946** a b-tree is '/000/'. (Btrees store content ordered from left to right
224947** so the pages to the left have smaller keys than the pages to the right.)
224948** The next to left-most child of the root page is
224949** '/001', and so on, each sibling page identified by a 3-digit hex
224950** value. The children of the 451st left-most sibling have paths such
224951** as '/1c2/000/, '/1c2/001/' etc.
224952**
224953** Overflow pages are specified by appending a '+' character and a
224954** six-digit hexadecimal value to the path to the cell they are linked
224955** from. For example, the three overflow pages in a chain linked from
224956** the left-most cell of the 450th child of the root page are identified
224957** by the paths:
224958**
224959** '/1c2/000+000000' // First page in overflow chain
224960** '/1c2/000+000001' // Second page in overflow chain
224961** '/1c2/000+000002' // Third page in overflow chain
224962**
224963** If the paths are sorted using the BINARY collation sequence, then
224964** the overflow pages associated with a cell will appear earlier in the
224965** sort-order than its child page:
224966**
224967** '/1c2/000/' // Left-most child of 451st child of root
224968*/
224969static const char zDbstatSchema[] =
224970 "CREATE TABLE x("
224971 " name TEXT," /* 0 Name of table or index */
224972 " path TEXT," /* 1 Path to page from root (NULL for agg) */
224973 " pageno INTEGER," /* 2 Page number (page count for aggregates) */
224974 " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */
224975 " ncell INTEGER," /* 4 Cells on page (0 for overflow) */
224976 " payload INTEGER," /* 5 Bytes of payload on this page */
224977 " unused INTEGER," /* 6 Bytes of unused space on this page */
224978 " mx_payload INTEGER," /* 7 Largest payload size of all cells */
224979 " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */
224980 " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */
224981 " schema TEXT HIDDEN," /* 10 Database schema being analyzed */
224982 " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */
224983 ")"
224984;
224985
224986/* Forward reference to data structured used in this module */
224987typedef struct StatTable StatTable;
224988typedef struct StatCursor StatCursor;
224989typedef struct StatPage StatPage;
224990typedef struct StatCell StatCell;
224991
224992/* Size information for a single cell within a btree page */
224993struct StatCell {
224994 int nLocal; /* Bytes of local payload */
224995 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
224996 int nOvfl; /* Entries in aOvfl[] */
224997 u32 *aOvfl; /* Array of overflow page numbers */
224998 int nLastOvfl; /* Bytes of payload on final overflow page */
224999 int iOvfl; /* Iterates through aOvfl[] */
225000};
225001
225002/* Size information for a single btree page */
225003struct StatPage {
225004 u32 iPgno; /* Page number */
225005 u8 *aPg; /* Page buffer from sqlite3_malloc() */
225006 int iCell; /* Current cell */
225007 char *zPath; /* Path to this page */
225008
225009 /* Variables populated by statDecodePage(): */
225010 u8 flags; /* Copy of flags byte */
225011 int nCell; /* Number of cells on page */
225012 int nUnused; /* Number of unused bytes on page */
225013 StatCell *aCell; /* Array of parsed cells */
225014 u32 iRightChildPg; /* Right-child page number (or 0) */
225015 int nMxPayload; /* Largest payload of any cell on the page */
225016};
225017
225018/* The cursor for scanning the dbstat virtual table */
225019struct StatCursor {
225020 sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */
225021 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
225022 u8 isEof; /* After pStmt has returned SQLITE_DONE */
225023 u8 isAgg; /* Aggregate results for each table */
225024 int iDb; /* Schema used for this query */
225025
225026 StatPage aPage[32]; /* Pages in path to current page */
225027 int iPage; /* Current entry in aPage[] */
225028
225029 /* Values to return. */
225030 u32 iPageno; /* Value of 'pageno' column */
225031 char *zName; /* Value of 'name' column */
225032 char *zPath; /* Value of 'path' column */
225033 char *zPagetype; /* Value of 'pagetype' column */
225034 int nPage; /* Number of pages in current btree */
225035 int nCell; /* Value of 'ncell' column */
225036 int nMxPayload; /* Value of 'mx_payload' column */
225037 i64 nUnused; /* Value of 'unused' column */
225038 i64 nPayload; /* Value of 'payload' column */
225039 i64 iOffset; /* Value of 'pgOffset' column */
225040 i64 szPage; /* Value of 'pgSize' column */
225041};
225042
225043/* An instance of the DBSTAT virtual table */
225044struct StatTable {
225045 sqlite3_vtab base; /* base class. MUST BE FIRST! */
225046 sqlite3 *db; /* Database connection that owns this vtab */
225047 int iDb; /* Index of database to analyze */
225048};
225049
225050#ifndef get2byte
225051# define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
225052#endif
225053
225054/*
225055** Connect to or create a new DBSTAT virtual table.
225056*/
225057static int statConnect(
225058 sqlite3 *db,
225059 void *pAux,
225060 int argc, const char *const*argv,
225061 sqlite3_vtab **ppVtab,
225062 char **pzErr
225063){
225064 StatTable *pTab = 0;
225065 int rc = SQLITE_OK0;
225066 int iDb;
225067 (void)pAux;
225068
225069 if( argc>=4 ){
225070 Token nm;
225071 sqlite3TokenInit(&nm, (char*)argv[3]);
225072 iDb = sqlite3FindDb(db, &nm);
225073 if( iDb<0 ){
225074 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
225075 return SQLITE_ERROR1;
225076 }
225077 }else{
225078 iDb = 0;
225079 }
225080 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY3);
225081 rc = sqlite3_declare_vtab(db, zDbstatSchema);
225082 if( rc==SQLITE_OK0 ){
225083 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
225084 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
225085 }
225086
225087 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
225088 if( rc==SQLITE_OK0 ){
225089 memset(pTab, 0, sizeof(StatTable));
225090 pTab->db = db;
225091 pTab->iDb = iDb;
225092 }
225093
225094 *ppVtab = (sqlite3_vtab*)pTab;
225095 return rc;
225096}
225097
225098/*
225099** Disconnect from or destroy the DBSTAT virtual table.
225100*/
225101static int statDisconnect(sqlite3_vtab *pVtab){
225102 sqlite3_free(pVtab);
225103 return SQLITE_OK0;
225104}
225105
225106/*
225107** Compute the best query strategy and return the result in idxNum.
225108**
225109** idxNum-Bit Meaning
225110** ---------- ----------------------------------------------
225111** 0x01 There is a schema=? term in the WHERE clause
225112** 0x02 There is a name=? term in the WHERE clause
225113** 0x04 There is an aggregate=? term in the WHERE clause
225114** 0x08 Output should be ordered by name and path
225115*/
225116static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
225117 int i;
225118 int iSchema = -1;
225119 int iName = -1;
225120 int iAgg = -1;
225121 (void)tab;
225122
225123 /* Look for a valid schema=? constraint. If found, change the idxNum to
225124 ** 1 and request the value of that constraint be sent to xFilter. And
225125 ** lower the cost estimate to encourage the constrained version to be
225126 ** used.
225127 */
225128 for(i=0; i<pIdxInfo->nConstraint; i++){
225129 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
225130 if( pIdxInfo->aConstraint[i].usable==0 ){
225131 /* Force DBSTAT table should always be the right-most table in a join */
225132 return SQLITE_CONSTRAINT19;
225133 }
225134 switch( pIdxInfo->aConstraint[i].iColumn ){
225135 case 0: { /* name */
225136 iName = i;
225137 break;
225138 }
225139 case 10: { /* schema */
225140 iSchema = i;
225141 break;
225142 }
225143 case 11: { /* aggregate */
225144 iAgg = i;
225145 break;
225146 }
225147 }
225148 }
225149 i = 0;
225150 if( iSchema>=0 ){
225151 pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i;
225152 pIdxInfo->aConstraintUsage[iSchema].omit = 1;
225153 pIdxInfo->idxNum |= 0x01;
225154 }
225155 if( iName>=0 ){
225156 pIdxInfo->aConstraintUsage[iName].argvIndex = ++i;
225157 pIdxInfo->idxNum |= 0x02;
225158 }
225159 if( iAgg>=0 ){
225160 pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i;
225161 pIdxInfo->idxNum |= 0x04;
225162 }
225163 pIdxInfo->estimatedCost = 1.0;
225164
225165 /* Records are always returned in ascending order of (name, path).
225166 ** If this will satisfy the client, set the orderByConsumed flag so that
225167 ** SQLite does not do an external sort.
225168 */
225169 if( ( pIdxInfo->nOrderBy==1
225170 && pIdxInfo->aOrderBy[0].iColumn==0
225171 && pIdxInfo->aOrderBy[0].desc==0
225172 ) ||
225173 ( pIdxInfo->nOrderBy==2
225174 && pIdxInfo->aOrderBy[0].iColumn==0
225175 && pIdxInfo->aOrderBy[0].desc==0
225176 && pIdxInfo->aOrderBy[1].iColumn==1
225177 && pIdxInfo->aOrderBy[1].desc==0
225178 )
225179 ){
225180 pIdxInfo->orderByConsumed = 1;
225181 pIdxInfo->idxNum |= 0x08;
225182 }
225183 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_HEX0x00000002;
225184
225185 return SQLITE_OK0;
225186}
225187
225188/*
225189** Open a new DBSTAT cursor.
225190*/
225191static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
225192 StatTable *pTab = (StatTable *)pVTab;
225193 StatCursor *pCsr;
225194
225195 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
225196 if( pCsr==0 ){
225197 return SQLITE_NOMEM_BKPT7;
225198 }else{
225199 memset(pCsr, 0, sizeof(StatCursor));
225200 pCsr->base.pVtab = pVTab;
225201 pCsr->iDb = pTab->iDb;
225202 }
225203
225204 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
225205 return SQLITE_OK0;
225206}
225207
225208static void statClearCells(StatPage *p){
225209 int i;
225210 if( p->aCell ){
225211 for(i=0; i<p->nCell; i++){
225212 sqlite3_free(p->aCell[i].aOvfl);
225213 }
225214 sqlite3_free(p->aCell);
225215 }
225216 p->nCell = 0;
225217 p->aCell = 0;
225218}
225219
225220static void statClearPage(StatPage *p){
225221 u8 *aPg = p->aPg;
225222 statClearCells(p);
225223 sqlite3_free(p->zPath);
225224 memset(p, 0, sizeof(StatPage));
225225 p->aPg = aPg;
225226}
225227
225228static void statResetCsr(StatCursor *pCsr){
225229 int i;
225230 /* In some circumstances, specifically if an OOM has occurred, the call
225231 ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
225232 ** important that statClearPage() is called to free any page refs before
225233 ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
225234 for(i=0; i<ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))); i++){
225235 statClearPage(&pCsr->aPage[i]);
225236 sqlite3_free(pCsr->aPage[i].aPg);
225237 pCsr->aPage[i].aPg = 0;
225238 }
225239 sqlite3_reset(pCsr->pStmt);
225240 pCsr->iPage = 0;
225241 sqlite3_free(pCsr->zPath);
225242 pCsr->zPath = 0;
225243 pCsr->isEof = 0;
225244}
225245
225246/* Resize the space-used counters inside of the cursor */
225247static void statResetCounts(StatCursor *pCsr){
225248 pCsr->nCell = 0;
225249 pCsr->nMxPayload = 0;
225250 pCsr->nUnused = 0;
225251 pCsr->nPayload = 0;
225252 pCsr->szPage = 0;
225253 pCsr->nPage = 0;
225254}
225255
225256/*
225257** Close a DBSTAT cursor.
225258*/
225259static int statClose(sqlite3_vtab_cursor *pCursor){
225260 StatCursor *pCsr = (StatCursor *)pCursor;
225261 statResetCsr(pCsr);
225262 sqlite3_finalize(pCsr->pStmt);
225263 sqlite3_free(pCsr);
225264 return SQLITE_OK0;
225265}
225266
225267/*
225268** For a single cell on a btree page, compute the number of bytes of
225269** content (payload) stored on that page. That is to say, compute the
225270** number of bytes of content not found on overflow pages.
225271*/
225272static int getLocalPayload(
225273 int nUsable, /* Usable bytes per page */
225274 u8 flags, /* Page flags */
225275 int nTotal /* Total record (payload) size */
225276){
225277 int nLocal;
225278 int nMinLocal;
225279 int nMaxLocal;
225280
225281 if( flags==0x0D ){ /* Table leaf node */
225282 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
225283 nMaxLocal = nUsable - 35;
225284 }else{ /* Index interior and leaf nodes */
225285 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
225286 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
225287 }
225288
225289 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
225290 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
225291 return nLocal;
225292}
225293
225294/* Populate the StatPage object with information about the all
225295** cells found on the page currently under analysis.
225296*/
225297static int statDecodePage(Btree *pBt, StatPage *p){
225298 int nUnused;
225299 int iOff;
225300 int nHdr;
225301 int isLeaf;
225302 int szPage;
225303
225304 u8 *aData = p->aPg;
225305 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
225306
225307 p->flags = aHdr[0];
225308 if( p->flags==0x0A || p->flags==0x0D ){
225309 isLeaf = 1;
225310 nHdr = 8;
225311 }else if( p->flags==0x05 || p->flags==0x02 ){
225312 isLeaf = 0;
225313 nHdr = 12;
225314 }else{
225315 goto statPageIsCorrupt;
225316 }
225317 if( p->iPgno==1 ) nHdr += 100;
225318 p->nCell = get2byte(&aHdr[3])((&aHdr[3])[0]<<8 | (&aHdr[3])[1]);
225319 p->nMxPayload = 0;
225320 szPage = sqlite3BtreeGetPageSize(pBt);
225321
225322 nUnused = get2byte(&aHdr[5])((&aHdr[5])[0]<<8 | (&aHdr[5])[1]) - nHdr - 2*p->nCell;
225323 nUnused += (int)aHdr[7];
225324 iOff = get2byte(&aHdr[1])((&aHdr[1])[0]<<8 | (&aHdr[1])[1]);
225325 while( iOff ){
225326 int iNext;
225327 if( iOff>=szPage ) goto statPageIsCorrupt;
225328 nUnused += get2byte(&aData[iOff+2])((&aData[iOff+2])[0]<<8 | (&aData[iOff+2])[1]);
225329 iNext = get2byte(&aData[iOff])((&aData[iOff])[0]<<8 | (&aData[iOff])[1]);
225330 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
225331 iOff = iNext;
225332 }
225333 p->nUnused = nUnused;
225334 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
225335
225336 if( p->nCell ){
225337 int i; /* Used to iterate through cells */
225338 int nUsable; /* Usable bytes per page */
225339
225340 sqlite3BtreeEnter(pBt);
225341 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
225342 sqlite3BtreeLeave(pBt);
225343 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
225344 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT7;
225345 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
225346
225347 for(i=0; i<p->nCell; i++){
225348 StatCell *pCell = &p->aCell[i];
225349
225350 iOff = get2byte(&aData[nHdr+i*2])((&aData[nHdr+i*2])[0]<<8 | (&aData[nHdr+i*2])[
1])
;
225351 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
225352 if( !isLeaf ){
225353 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
225354 iOff += 4;
225355 }
225356 if( p->flags==0x05 ){
225357 /* A table interior node. nPayload==0. */
225358 }else{
225359 u32 nPayload; /* Bytes of payload total (local+overflow) */
225360 int nLocal; /* Bytes of payload stored locally */
225361 iOff += getVarint32(&aData[iOff], nPayload)(u8)((*(&aData[iOff])<(u8)0x80)?((nPayload)=(u32)*(&
aData[iOff])),1:sqlite3GetVarint32((&aData[iOff]),(u32 *)
&(nPayload)))
;
225362 if( p->flags==0x0D ){
225363 u64 dummy;
225364 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
225365 }
225366 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
225367 nLocal = getLocalPayload(nUsable, p->flags, nPayload);
225368 if( nLocal<0 ) goto statPageIsCorrupt;
225369 pCell->nLocal = nLocal;
225370 assert( nPayload>=(u32)nLocal )((void) (0));
225371 assert( nLocal<=(nUsable-35) )((void) (0));
225372 if( nPayload>(u32)nLocal ){
225373 int j;
225374 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
225375 if( iOff+nLocal+4>nUsable || nPayload>0x7fffffff ){
225376 goto statPageIsCorrupt;
225377 }
225378 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
225379 pCell->nOvfl = nOvfl;
225380 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
225381 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT7;
225382 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
225383 for(j=1; j<nOvfl; j++){
225384 int rc;
225385 u32 iPrev = pCell->aOvfl[j-1];
225386 DbPage *pPg = 0;
225387 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
225388 if( rc!=SQLITE_OK0 ){
225389 assert( pPg==0 )((void) (0));
225390 return rc;
225391 }
225392 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
225393 sqlite3PagerUnref(pPg);
225394 }
225395 }
225396 }
225397 }
225398 }
225399
225400 return SQLITE_OK0;
225401
225402statPageIsCorrupt:
225403 p->flags = 0;
225404 statClearCells(p);
225405 return SQLITE_OK0;
225406}
225407
225408/*
225409** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
225410** the current value of pCsr->iPageno.
225411*/
225412static void statSizeAndOffset(StatCursor *pCsr){
225413 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
225414 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
225415 Pager *pPager = sqlite3BtreePager(pBt);
225416 sqlite3_file *fd;
225417 sqlite3_int64 x[2];
225418
225419 /* If connected to a ZIPVFS backend, find the page size and
225420 ** offset from ZIPVFS.
225421 */
225422 fd = sqlite3PagerFile(pPager);
225423 x[0] = pCsr->iPageno;
225424 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK0 ){
225425 pCsr->iOffset = x[0];
225426 pCsr->szPage += x[1];
225427 }else{
225428 /* Not ZIPVFS: The default page size and offset */
225429 pCsr->szPage += sqlite3BtreeGetPageSize(pBt);
225430 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
225431 }
225432}
225433
225434/*
225435** Load a copy of the page data for page iPg into the buffer belonging
225436** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
225437** if successful, or an SQLite error code otherwise.
225438*/
225439static int statGetPage(
225440 Btree *pBt, /* Load page from this b-tree */
225441 u32 iPg, /* Page number to load */
225442 StatPage *pPg /* Load page into this object */
225443){
225444 int pgsz = sqlite3BtreeGetPageSize(pBt);
225445 DbPage *pDbPage = 0;
225446 int rc;
225447
225448 if( pPg->aPg==0 ){
225449 pPg->aPg = (u8*)sqlite3_malloc(pgsz + DBSTAT_PAGE_PADDING_BYTES256);
225450 if( pPg->aPg==0 ){
225451 return SQLITE_NOMEM_BKPT7;
225452 }
225453 memset(&pPg->aPg[pgsz], 0, DBSTAT_PAGE_PADDING_BYTES256);
225454 }
225455
225456 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPg, &pDbPage, 0);
225457 if( rc==SQLITE_OK0 ){
225458 const u8 *a = sqlite3PagerGetData(pDbPage);
225459 memcpy(pPg->aPg, a, pgsz);
225460 sqlite3PagerUnref(pDbPage);
225461 }
225462
225463 return rc;
225464}
225465
225466/*
225467** Move a DBSTAT cursor to the next entry. Normally, the next
225468** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
225469** the next entry is the next btree.
225470*/
225471static int statNext(sqlite3_vtab_cursor *pCursor){
225472 int rc;
225473 int nPayload;
225474 char *z;
225475 StatCursor *pCsr = (StatCursor *)pCursor;
225476 StatTable *pTab = (StatTable *)pCursor->pVtab;
225477 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
225478 Pager *pPager = sqlite3BtreePager(pBt);
225479
225480 sqlite3_free(pCsr->zPath);
225481 pCsr->zPath = 0;
225482
225483statNextRestart:
225484 if( pCsr->iPage<0 ){
225485 /* Start measuring space on the next btree */
225486 statResetCounts(pCsr);
225487 rc = sqlite3_step(pCsr->pStmt);
225488 if( rc==SQLITE_ROW100 ){
225489 int nPage;
225490 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
225491 sqlite3PagerPagecount(pPager, &nPage);
225492 if( nPage==0 ){
225493 pCsr->isEof = 1;
225494 return sqlite3_reset(pCsr->pStmt);
225495 }
225496 rc = statGetPage(pBt, iRoot, &pCsr->aPage[0]);
225497 pCsr->aPage[0].iPgno = iRoot;
225498 pCsr->aPage[0].iCell = 0;
225499 if( !pCsr->isAgg ){
225500 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
225501 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
225502 }
225503 pCsr->iPage = 0;
225504 pCsr->nPage = 1;
225505 }else{
225506 pCsr->isEof = 1;
225507 return sqlite3_reset(pCsr->pStmt);
225508 }
225509 }else{
225510 /* Continue analyzing the btree previously started */
225511 StatPage *p = &pCsr->aPage[pCsr->iPage];
225512 if( !pCsr->isAgg ) statResetCounts(pCsr);
225513 while( p->iCell<p->nCell ){
225514 StatCell *pCell = &p->aCell[p->iCell];
225515 while( pCell->iOvfl<pCell->nOvfl ){
225516 int nUsable, iOvfl;
225517 sqlite3BtreeEnter(pBt);
225518 nUsable = sqlite3BtreeGetPageSize(pBt) -
225519 sqlite3BtreeGetReserveNoMutex(pBt);
225520 sqlite3BtreeLeave(pBt);
225521 pCsr->nPage++;
225522 statSizeAndOffset(pCsr);
225523 if( pCell->iOvfl<pCell->nOvfl-1 ){
225524 pCsr->nPayload += nUsable - 4;
225525 }else{
225526 pCsr->nPayload += pCell->nLastOvfl;
225527 pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl;
225528 }
225529 iOvfl = pCell->iOvfl;
225530 pCell->iOvfl++;
225531 if( !pCsr->isAgg ){
225532 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
225533 pCsr->iPageno = pCell->aOvfl[iOvfl];
225534 pCsr->zPagetype = "overflow";
225535 pCsr->zPath = z = sqlite3_mprintf(
225536 "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl
225537 );
225538 return z==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
225539 }
225540 }
225541 if( p->iRightChildPg ) break;
225542 p->iCell++;
225543 }
225544
225545 if( !p->iRightChildPg || p->iCell>p->nCell ){
225546 statClearPage(p);
225547 pCsr->iPage--;
225548 if( pCsr->isAgg && pCsr->iPage<0 ){
225549 /* label-statNext-done: When computing aggregate space usage over
225550 ** an entire btree, this is the exit point from this function */
225551 return SQLITE_OK0;
225552 }
225553 goto statNextRestart; /* Tail recursion */
225554 }
225555 pCsr->iPage++;
225556 if( pCsr->iPage>=ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))) ){
225557 statResetCsr(pCsr);
225558 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(225558);
225559 }
225560 assert( p==&pCsr->aPage[pCsr->iPage-1] )((void) (0));
225561
225562 if( p->iCell==p->nCell ){
225563 p[1].iPgno = p->iRightChildPg;
225564 }else{
225565 p[1].iPgno = p->aCell[p->iCell].iChildPg;
225566 }
225567 rc = statGetPage(pBt, p[1].iPgno, &p[1]);
225568 pCsr->nPage++;
225569 p[1].iCell = 0;
225570 if( !pCsr->isAgg ){
225571 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
225572 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
225573 }
225574 p->iCell++;
225575 }
225576
225577
225578 /* Populate the StatCursor fields with the values to be returned
225579 ** by the xColumn() and xRowid() methods.
225580 */
225581 if( rc==SQLITE_OK0 ){
225582 int i;
225583 StatPage *p = &pCsr->aPage[pCsr->iPage];
225584 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
225585 pCsr->iPageno = p->iPgno;
225586
225587 rc = statDecodePage(pBt, p);
225588 if( rc==SQLITE_OK0 ){
225589 statSizeAndOffset(pCsr);
225590
225591 switch( p->flags ){
225592 case 0x05: /* table internal */
225593 case 0x02: /* index internal */
225594 pCsr->zPagetype = "internal";
225595 break;
225596 case 0x0D: /* table leaf */
225597 case 0x0A: /* index leaf */
225598 pCsr->zPagetype = "leaf";
225599 break;
225600 default:
225601 pCsr->zPagetype = "corrupted";
225602 break;
225603 }
225604 pCsr->nCell += p->nCell;
225605 pCsr->nUnused += p->nUnused;
225606 if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload;
225607 if( !pCsr->isAgg ){
225608 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
225609 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
225610 }
225611 nPayload = 0;
225612 for(i=0; i<p->nCell; i++){
225613 nPayload += p->aCell[i].nLocal;
225614 }
225615 pCsr->nPayload += nPayload;
225616
225617 /* If computing aggregate space usage by btree, continue with the
225618 ** next page. The loop will exit via the return at label-statNext-done
225619 */
225620 if( pCsr->isAgg ) goto statNextRestart;
225621 }
225622 }
225623
225624 return rc;
225625}
225626
225627static int statEof(sqlite3_vtab_cursor *pCursor){
225628 StatCursor *pCsr = (StatCursor *)pCursor;
225629 return pCsr->isEof;
225630}
225631
225632/* Initialize a cursor according to the query plan idxNum using the
225633** arguments in argv[0]. See statBestIndex() for a description of the
225634** meaning of the bits in idxNum.
225635*/
225636static int statFilter(
225637 sqlite3_vtab_cursor *pCursor,
225638 int idxNum, const char *idxStr,
225639 int argc, sqlite3_value **argv
225640){
225641 StatCursor *pCsr = (StatCursor *)pCursor;
225642 StatTable *pTab = (StatTable*)(pCursor->pVtab);
225643 sqlite3_str *pSql; /* Query of btrees to analyze */
225644 char *zSql; /* String value of pSql */
225645 int iArg = 0; /* Count of argv[] parameters used so far */
225646 int rc = SQLITE_OK0; /* Result of this operation */
225647 const char *zName = 0; /* Only provide analysis of this table */
225648 (void)argc;
225649 (void)idxStr;
225650
225651 statResetCsr(pCsr);
225652 sqlite3_finalize(pCsr->pStmt);
225653 pCsr->pStmt = 0;
225654 if( idxNum & 0x01 ){
225655 /* schema=? constraint is present. Get its value */
225656 const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]);
225657 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
225658 if( pCsr->iDb<0 ){
225659 pCsr->iDb = 0;
225660 pCsr->isEof = 1;
225661 return SQLITE_OK0;
225662 }
225663 }else{
225664 pCsr->iDb = pTab->iDb;
225665 }
225666 if( idxNum & 0x02 ){
225667 /* name=? constraint is present */
225668 zName = (const char*)sqlite3_value_text(argv[iArg++]);
225669 }
225670 if( idxNum & 0x04 ){
225671 /* aggregate=? constraint is present */
225672 pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0;
225673 }else{
225674 pCsr->isAgg = 0;
225675 }
225676 pSql = sqlite3_str_new(pTab->db);
225677 sqlite3_str_appendf(pSql,
225678 "SELECT * FROM ("
225679 "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type"
225680 " UNION ALL "
225681 "SELECT name,rootpage,type"
225682 " FROM \"%w\".sqlite_schema WHERE rootpage!=0)",
225683 pTab->db->aDb[pCsr->iDb].zDbSName);
225684 if( zName ){
225685 sqlite3_str_appendf(pSql, "WHERE name=%Q", zName);
225686 }
225687 if( idxNum & 0x08 ){
225688 sqlite3_str_appendf(pSql, " ORDER BY name");
225689 }
225690 zSql = sqlite3_str_finish(pSql);
225691 if( zSql==0 ){
225692 return SQLITE_NOMEM_BKPT7;
225693 }else{
225694 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
225695 sqlite3_free(zSql);
225696 }
225697
225698 if( rc==SQLITE_OK0 ){
225699 pCsr->iPage = -1;
225700 rc = statNext(pCursor);
225701 }
225702 return rc;
225703}
225704
225705static int statColumn(
225706 sqlite3_vtab_cursor *pCursor,
225707 sqlite3_context *ctx,
225708 int i
225709){
225710 StatCursor *pCsr = (StatCursor *)pCursor;
225711 switch( i ){
225712 case 0: /* name */
225713 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
225714 break;
225715 case 1: /* path */
225716 if( !pCsr->isAgg ){
225717 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
225718 }
225719 break;
225720 case 2: /* pageno */
225721 if( pCsr->isAgg ){
225722 sqlite3_result_int64(ctx, pCsr->nPage);
225723 }else{
225724 sqlite3_result_int64(ctx, pCsr->iPageno);
225725 }
225726 break;
225727 case 3: /* pagetype */
225728 if( !pCsr->isAgg ){
225729 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
225730 }
225731 break;
225732 case 4: /* ncell */
225733 sqlite3_result_int64(ctx, pCsr->nCell);
225734 break;
225735 case 5: /* payload */
225736 sqlite3_result_int64(ctx, pCsr->nPayload);
225737 break;
225738 case 6: /* unused */
225739 sqlite3_result_int64(ctx, pCsr->nUnused);
225740 break;
225741 case 7: /* mx_payload */
225742 sqlite3_result_int64(ctx, pCsr->nMxPayload);
225743 break;
225744 case 8: /* pgoffset */
225745 if( !pCsr->isAgg ){
225746 sqlite3_result_int64(ctx, pCsr->iOffset);
225747 }
225748 break;
225749 case 9: /* pgsize */
225750 sqlite3_result_int64(ctx, pCsr->szPage);
225751 break;
225752 case 10: { /* schema */
225753 sqlite3 *db = sqlite3_context_db_handle(ctx);
225754 int iDb = pCsr->iDb;
225755 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
225756 break;
225757 }
225758 default: { /* aggregate */
225759 sqlite3_result_int(ctx, pCsr->isAgg);
225760 break;
225761 }
225762 }
225763 return SQLITE_OK0;
225764}
225765
225766static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
225767 StatCursor *pCsr = (StatCursor *)pCursor;
225768 *pRowid = pCsr->iPageno;
225769 return SQLITE_OK0;
225770}
225771
225772/*
225773** Invoke this routine to register the "dbstat" virtual table module
225774*/
225775SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){
225776 static sqlite3_module dbstat_module = {
225777 0, /* iVersion */
225778 statConnect, /* xCreate */
225779 statConnect, /* xConnect */
225780 statBestIndex, /* xBestIndex */
225781 statDisconnect, /* xDisconnect */
225782 statDisconnect, /* xDestroy */
225783 statOpen, /* xOpen - open a cursor */
225784 statClose, /* xClose - close a cursor */
225785 statFilter, /* xFilter - configure scan constraints */
225786 statNext, /* xNext - advance a cursor */
225787 statEof, /* xEof - check for end of scan */
225788 statColumn, /* xColumn - read data */
225789 statRowid, /* xRowid - read data */
225790 0, /* xUpdate */
225791 0, /* xBegin */
225792 0, /* xSync */
225793 0, /* xCommit */
225794 0, /* xRollback */
225795 0, /* xFindMethod */
225796 0, /* xRename */
225797 0, /* xSavepoint */
225798 0, /* xRelease */
225799 0, /* xRollbackTo */
225800 0, /* xShadowName */
225801 0 /* xIntegrity */
225802 };
225803 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
225804}
225805#elif defined(SQLITE_ENABLE_DBSTAT_VTAB1)
225806SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK0; }
225807#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
225808
225809/************** End of dbstat.c **********************************************/
225810/************** Begin file dbpage.c ******************************************/
225811/*
225812** 2017-10-11
225813**
225814** The author disclaims copyright to this source code. In place of
225815** a legal notice, here is a blessing:
225816**
225817** May you do good and not evil.
225818** May you find forgiveness for yourself and forgive others.
225819** May you share freely, never taking more than you give.
225820**
225821******************************************************************************
225822**
225823** This file contains an implementation of the "sqlite_dbpage" virtual table.
225824**
225825** The sqlite_dbpage virtual table is used to read or write whole raw
225826** pages of the database file. The pager interface is used so that
225827** uncommitted changes and changes recorded in the WAL file are correctly
225828** retrieved.
225829**
225830** Usage example:
225831**
225832** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
225833**
225834** This is an eponymous virtual table so it does not need to be created before
225835** use. The optional argument to the sqlite_dbpage() table name is the
225836** schema for the database file that is to be read. The default schema is
225837** "main".
225838**
225839** The data field of sqlite_dbpage table can be updated. The new
225840** value must be a BLOB which is the correct page size, otherwise the
225841** update fails. INSERT operations also work, and operate as if they
225842** where REPLACE. The size of the database can be extended by INSERT-ing
225843** new pages on the end.
225844**
225845** Rows may not be deleted. However, doing an INSERT to page number N
225846** with NULL page data causes the N-th page and all subsequent pages to be
225847** deleted and the database to be truncated.
225848*/
225849
225850/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
225851#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
225852 && !defined(SQLITE_OMIT_VIRTUALTABLE)
225853
225854typedef struct DbpageTable DbpageTable;
225855typedef struct DbpageCursor DbpageCursor;
225856
225857struct DbpageCursor {
225858 sqlite3_vtab_cursor base; /* Base class. Must be first */
225859 int pgno; /* Current page number */
225860 int mxPgno; /* Last page to visit on this scan */
225861 Pager *pPager; /* Pager being read/written */
225862 DbPage *pPage1; /* Page 1 of the database */
225863 int iDb; /* Index of database to analyze */
225864 int szPage; /* Size of each page in bytes */
225865};
225866
225867struct DbpageTable {
225868 sqlite3_vtab base; /* Base class. Must be first */
225869 sqlite3 *db; /* The database */
225870 int iDbTrunc; /* Database to truncate */
225871 Pgno pgnoTrunc; /* Size to truncate to */
225872};
225873
225874/* Columns */
225875#define DBPAGE_COLUMN_PGNO 0
225876#define DBPAGE_COLUMN_DATA 1
225877#define DBPAGE_COLUMN_SCHEMA 2
225878
225879
225880/*
225881** Connect to or create a dbpagevfs virtual table.
225882*/
225883static int dbpageConnect(
225884 sqlite3 *db,
225885 void *pAux,
225886 int argc, const char *const*argv,
225887 sqlite3_vtab **ppVtab,
225888 char **pzErr
225889){
225890 DbpageTable *pTab = 0;
225891 int rc = SQLITE_OK0;
225892 (void)pAux;
225893 (void)argc;
225894 (void)argv;
225895 (void)pzErr;
225896
225897 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY3);
225898 sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS4);
225899 rc = sqlite3_declare_vtab(db,
225900 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
225901 if( rc==SQLITE_OK0 ){
225902 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
225903 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
225904 }
225905
225906 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
225907 if( rc==SQLITE_OK0 ){
225908 memset(pTab, 0, sizeof(DbpageTable));
225909 pTab->db = db;
225910 }
225911
225912 *ppVtab = (sqlite3_vtab*)pTab;
225913 return rc;
225914}
225915
225916/*
225917** Disconnect from or destroy a dbpagevfs virtual table.
225918*/
225919static int dbpageDisconnect(sqlite3_vtab *pVtab){
225920 sqlite3_free(pVtab);
225921 return SQLITE_OK0;
225922}
225923
225924/*
225925** idxNum:
225926**
225927** 0 schema=main, full table scan
225928** 1 schema=main, pgno=?1
225929** 2 schema=?1, full table scan
225930** 3 schema=?1, pgno=?2
225931*/
225932static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
225933 int i;
225934 int iPlan = 0;
225935 (void)tab;
225936
225937 /* If there is a schema= constraint, it must be honored. Report a
225938 ** ridiculously large estimated cost if the schema= constraint is
225939 ** unavailable
225940 */
225941 for(i=0; i<pIdxInfo->nConstraint; i++){
225942 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
225943 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
225944 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
225945 if( !p->usable ){
225946 /* No solution. */
225947 return SQLITE_CONSTRAINT19;
225948 }
225949 iPlan = 2;
225950 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
225951 pIdxInfo->aConstraintUsage[i].omit = 1;
225952 break;
225953 }
225954
225955 /* If we reach this point, it means that either there is no schema=
225956 ** constraint (in which case we use the "main" schema) or else the
225957 ** schema constraint was accepted. Lower the estimated cost accordingly
225958 */
225959 pIdxInfo->estimatedCost = 1.0e6;
225960
225961 /* Check for constraints against pgno */
225962 for(i=0; i<pIdxInfo->nConstraint; i++){
225963 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
225964 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
225965 pIdxInfo->estimatedRows = 1;
225966 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE0x00000001;
225967 pIdxInfo->estimatedCost = 1.0;
225968 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
225969 pIdxInfo->aConstraintUsage[i].omit = 1;
225970 iPlan |= 1;
225971 break;
225972 }
225973 }
225974 pIdxInfo->idxNum = iPlan;
225975
225976 if( pIdxInfo->nOrderBy>=1
225977 && pIdxInfo->aOrderBy[0].iColumn<=0
225978 && pIdxInfo->aOrderBy[0].desc==0
225979 ){
225980 pIdxInfo->orderByConsumed = 1;
225981 }
225982 return SQLITE_OK0;
225983}
225984
225985/*
225986** Open a new dbpagevfs cursor.
225987*/
225988static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
225989 DbpageCursor *pCsr;
225990
225991 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
225992 if( pCsr==0 ){
225993 return SQLITE_NOMEM_BKPT7;
225994 }else{
225995 memset(pCsr, 0, sizeof(DbpageCursor));
225996 pCsr->base.pVtab = pVTab;
225997 pCsr->pgno = -1;
225998 }
225999
226000 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
226001 return SQLITE_OK0;
226002}
226003
226004/*
226005** Close a dbpagevfs cursor.
226006*/
226007static int dbpageClose(sqlite3_vtab_cursor *pCursor){
226008 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
226009 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
226010 sqlite3_free(pCsr);
226011 return SQLITE_OK0;
226012}
226013
226014/*
226015** Move a dbpagevfs cursor to the next entry in the file.
226016*/
226017static int dbpageNext(sqlite3_vtab_cursor *pCursor){
226018 int rc = SQLITE_OK0;
226019 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
226020 pCsr->pgno++;
226021 return rc;
226022}
226023
226024static int dbpageEof(sqlite3_vtab_cursor *pCursor){
226025 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
226026 return pCsr->pgno > pCsr->mxPgno;
226027}
226028
226029/*
226030** idxNum:
226031**
226032** 0 schema=main, full table scan
226033** 1 schema=main, pgno=?1
226034** 2 schema=?1, full table scan
226035** 3 schema=?1, pgno=?2
226036**
226037** idxStr is not used
226038*/
226039static int dbpageFilter(
226040 sqlite3_vtab_cursor *pCursor,
226041 int idxNum, const char *idxStr,
226042 int argc, sqlite3_value **argv
226043){
226044 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
226045 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
226046 int rc;
226047 sqlite3 *db = pTab->db;
226048 Btree *pBt;
226049
226050 (void)idxStr;
226051
226052 /* Default setting is no rows of result */
226053 pCsr->pgno = 1;
226054 pCsr->mxPgno = 0;
226055
226056 if( idxNum & 2 ){
226057 const char *zSchema;
226058 assert( argc>=1 )((void) (0));
226059 zSchema = (const char*)sqlite3_value_text(argv[0]);
226060 pCsr->iDb = sqlite3FindDbName(db, zSchema);
226061 if( pCsr->iDb<0 ) return SQLITE_OK0;
226062 }else{
226063 pCsr->iDb = 0;
226064 }
226065 pBt = db->aDb[pCsr->iDb].pBt;
226066 if( NEVER(pBt==0)(pBt==0) ) return SQLITE_OK0;
226067 pCsr->pPager = sqlite3BtreePager(pBt);
226068 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
226069 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
226070 if( idxNum & 1 ){
226071 assert( argc>(idxNum>>1) )((void) (0));
226072 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
226073 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
226074 pCsr->pgno = 1;
226075 pCsr->mxPgno = 0;
226076 }else{
226077 pCsr->mxPgno = pCsr->pgno;
226078 }
226079 }else{
226080 assert( pCsr->pgno==1 )((void) (0));
226081 }
226082 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
226083 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
226084 return rc;
226085}
226086
226087static int dbpageColumn(
226088 sqlite3_vtab_cursor *pCursor,
226089 sqlite3_context *ctx,
226090 int i
226091){
226092 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
226093 int rc = SQLITE_OK0;
226094 switch( i ){
226095 case 0: { /* pgno */
226096 sqlite3_result_int(ctx, pCsr->pgno);
226097 break;
226098 }
226099 case 1: { /* data */
226100 DbPage *pDbPage = 0;
226101 if( pCsr->pgno==((PENDING_BYTEsqlite3PendingByte/pCsr->szPage)+1) ){
226102 /* The pending byte page. Assume it is zeroed out. Attempting to
226103 ** request this page from the page is an SQLITE_CORRUPT error. */
226104 sqlite3_result_zeroblob(ctx, pCsr->szPage);
226105 }else{
226106 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
226107 if( rc==SQLITE_OK0 ){
226108 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
226109 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
226110 }
226111 sqlite3PagerUnref(pDbPage);
226112 }
226113 break;
226114 }
226115 default: { /* schema */
226116 sqlite3 *db = sqlite3_context_db_handle(ctx);
226117 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
226118 break;
226119 }
226120 }
226121 return rc;
226122}
226123
226124static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
226125 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
226126 *pRowid = pCsr->pgno;
226127 return SQLITE_OK0;
226128}
226129
226130static int dbpageUpdate(
226131 sqlite3_vtab *pVtab,
226132 int argc,
226133 sqlite3_value **argv,
226134 sqlite_int64 *pRowid
226135){
226136 DbpageTable *pTab = (DbpageTable *)pVtab;
226137 Pgno pgno;
226138 DbPage *pDbPage = 0;
226139 int rc = SQLITE_OK0;
226140 char *zErr = 0;
226141 int iDb;
226142 Btree *pBt;
226143 Pager *pPager;
226144 int szPage;
226145 int isInsert;
226146
226147 (void)pRowid;
226148 if( pTab->db->flags & SQLITE_Defensive0x10000000 ){
226149 zErr = "read-only";
226150 goto update_fail;
226151 }
226152 if( argc==1 ){
226153 zErr = "cannot delete";
226154 goto update_fail;
226155 }
226156 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ){
226157 pgno = (Pgno)sqlite3_value_int(argv[2]);
226158 isInsert = 1;
226159 }else{
226160 pgno = sqlite3_value_int(argv[0]);
226161 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
226162 zErr = "cannot insert";
226163 goto update_fail;
226164 }
226165 isInsert = 0;
226166 }
226167 if( sqlite3_value_type(argv[4])==SQLITE_NULL5 ){
226168 iDb = 0;
226169 }else{
226170 const char *zSchema = (const char*)sqlite3_value_text(argv[4]);
226171 iDb = sqlite3FindDbName(pTab->db, zSchema);
226172 if( iDb<0 ){
226173 zErr = "no such schema";
226174 goto update_fail;
226175 }
226176 }
226177 pBt = pTab->db->aDb[iDb].pBt;
226178 if( pgno<1 || NEVER(pBt==0)(pBt==0) ){
226179 zErr = "bad page number";
226180 goto update_fail;
226181 }
226182 szPage = sqlite3BtreeGetPageSize(pBt);
226183 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB4
226184 || sqlite3_value_bytes(argv[3])!=szPage
226185 ){
226186 if( sqlite3_value_type(argv[3])==SQLITE_NULL5 && isInsert && pgno>1 ){
226187 /* "INSERT INTO dbpage($PGNO,NULL)" causes page number $PGNO and
226188 ** all subsequent pages to be deleted. */
226189 pTab->iDbTrunc = iDb;
226190 pgno--;
226191 pTab->pgnoTrunc = pgno;
226192 }else{
226193 zErr = "bad page value";
226194 goto update_fail;
226195 }
226196 }
226197 pPager = sqlite3BtreePager(pBt);
226198 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
226199 if( rc==SQLITE_OK0 ){
226200 const void *pData = sqlite3_value_blob(argv[3]);
226201 if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK0 && pData ){
226202 unsigned char *aPage = sqlite3PagerGetData(pDbPage);
226203 memcpy(aPage, pData, szPage);
226204 pTab->pgnoTrunc = 0;
226205 }
226206 }
226207 sqlite3PagerUnref(pDbPage);
226208 return rc;
226209
226210update_fail:
226211 sqlite3_free(pVtab->zErrMsg);
226212 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
226213 return SQLITE_ERROR1;
226214}
226215
226216/* Since we do not know in advance which database files will be
226217** written by the sqlite_dbpage virtual table, start a write transaction
226218** on them all.
226219*/
226220static int dbpageBegin(sqlite3_vtab *pVtab){
226221 DbpageTable *pTab = (DbpageTable *)pVtab;
226222 sqlite3 *db = pTab->db;
226223 int i;
226224 for(i=0; i<db->nDb; i++){
226225 Btree *pBt = db->aDb[i].pBt;
226226 if( pBt ) (void)sqlite3BtreeBeginTrans(pBt, 1, 0);
226227 }
226228 pTab->pgnoTrunc = 0;
226229 return SQLITE_OK0;
226230}
226231
226232/* Invoke sqlite3PagerTruncate() as necessary, just prior to COMMIT
226233*/
226234static int dbpageSync(sqlite3_vtab *pVtab){
226235 DbpageTable *pTab = (DbpageTable *)pVtab;
226236 if( pTab->pgnoTrunc>0 ){
226237 Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt;
226238 Pager *pPager = sqlite3BtreePager(pBt);
226239 sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc);
226240 }
226241 pTab->pgnoTrunc = 0;
226242 return SQLITE_OK0;
226243}
226244
226245/* Cancel any pending truncate.
226246*/
226247static int dbpageRollbackTo(sqlite3_vtab *pVtab, int notUsed1){
226248 DbpageTable *pTab = (DbpageTable *)pVtab;
226249 pTab->pgnoTrunc = 0;
226250 (void)notUsed1;
226251 return SQLITE_OK0;
226252}
226253
226254/*
226255** Invoke this routine to register the "dbpage" virtual table module
226256*/
226257SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){
226258 static sqlite3_module dbpage_module = {
226259 0, /* iVersion */
226260 dbpageConnect, /* xCreate */
226261 dbpageConnect, /* xConnect */
226262 dbpageBestIndex, /* xBestIndex */
226263 dbpageDisconnect, /* xDisconnect */
226264 dbpageDisconnect, /* xDestroy */
226265 dbpageOpen, /* xOpen - open a cursor */
226266 dbpageClose, /* xClose - close a cursor */
226267 dbpageFilter, /* xFilter - configure scan constraints */
226268 dbpageNext, /* xNext - advance a cursor */
226269 dbpageEof, /* xEof - check for end of scan */
226270 dbpageColumn, /* xColumn - read data */
226271 dbpageRowid, /* xRowid - read data */
226272 dbpageUpdate, /* xUpdate */
226273 dbpageBegin, /* xBegin */
226274 dbpageSync, /* xSync */
226275 0, /* xCommit */
226276 0, /* xRollback */
226277 0, /* xFindMethod */
226278 0, /* xRename */
226279 0, /* xSavepoint */
226280 0, /* xRelease */
226281 dbpageRollbackTo, /* xRollbackTo */
226282 0, /* xShadowName */
226283 0 /* xIntegrity */
226284 };
226285 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
226286}
226287#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
226288SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK0; }
226289#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
226290
226291/************** End of dbpage.c **********************************************/
226292/************** Begin file sqlite3session.c **********************************/
226293
226294#if defined(SQLITE_ENABLE_SESSION1) && defined(SQLITE_ENABLE_PREUPDATE_HOOK1)
226295/* #include "sqlite3session.h" */
226296/* #include <assert.h> */
226297/* #include <string.h> */
226298
226299#ifndef SQLITE_AMALGAMATION1
226300/* # include "sqliteInt.h" */
226301/* # include "vdbeInt.h" */
226302#endif
226303
226304typedef struct SessionTable SessionTable;
226305typedef struct SessionChange SessionChange;
226306typedef struct SessionBuffer SessionBuffer;
226307typedef struct SessionInput SessionInput;
226308
226309/*
226310** Minimum chunk size used by streaming versions of functions.
226311*/
226312#ifndef SESSIONS_STRM_CHUNK_SIZE1024
226313# ifdef SQLITE_TEST
226314# define SESSIONS_STRM_CHUNK_SIZE1024 64
226315# else
226316# define SESSIONS_STRM_CHUNK_SIZE1024 1024
226317# endif
226318#endif
226319
226320#define SESSIONS_ROWID"_rowid_" "_rowid_"
226321
226322static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE1024;
226323
226324typedef struct SessionHook SessionHook;
226325struct SessionHook {
226326 void *pCtx;
226327 int (*xOld)(void*,int,sqlite3_value**);
226328 int (*xNew)(void*,int,sqlite3_value**);
226329 int (*xCount)(void*);
226330 int (*xDepth)(void*);
226331};
226332
226333/*
226334** Session handle structure.
226335*/
226336struct sqlite3_session {
226337 sqlite3 *db; /* Database handle session is attached to */
226338 char *zDb; /* Name of database session is attached to */
226339 int bEnableSize; /* True if changeset_size() enabled */
226340 int bEnable; /* True if currently recording */
226341 int bIndirect; /* True if all changes are indirect */
226342 int bAutoAttach; /* True to auto-attach tables */
226343 int bImplicitPK; /* True to handle tables with implicit PK */
226344 int rc; /* Non-zero if an error has occurred */
226345 void *pFilterCtx; /* First argument to pass to xTableFilter */
226346 int (*xTableFilter)(void *pCtx, const char *zTab);
226347 i64 nMalloc; /* Number of bytes of data allocated */
226348 i64 nMaxChangesetSize;
226349 sqlite3_value *pZeroBlob; /* Value containing X'' */
226350 sqlite3_session *pNext; /* Next session object on same db. */
226351 SessionTable *pTable; /* List of attached tables */
226352 SessionHook hook; /* APIs to grab new and old data with */
226353};
226354
226355/*
226356** Instances of this structure are used to build strings or binary records.
226357*/
226358struct SessionBuffer {
226359 u8 *aBuf; /* Pointer to changeset buffer */
226360 int nBuf; /* Size of buffer aBuf */
226361 int nAlloc; /* Size of allocation containing aBuf */
226362};
226363
226364/*
226365** An object of this type is used internally as an abstraction for
226366** input data. Input data may be supplied either as a single large buffer
226367** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
226368** sqlite3changeset_start_strm()).
226369**
226370** bNoDiscard:
226371** If true, then the only time data is discarded is as a result of explicit
226372** sessionDiscardData() calls. Not within every sessionInputBuffer() call.
226373*/
226374struct SessionInput {
226375 int bNoDiscard; /* If true, do not discard in InputBuffer() */
226376 int iCurrent; /* Offset in aData[] of current change */
226377 int iNext; /* Offset in aData[] of next change */
226378 u8 *aData; /* Pointer to buffer containing changeset */
226379 int nData; /* Number of bytes in aData */
226380
226381 SessionBuffer buf; /* Current read buffer */
226382 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
226383 void *pIn; /* First argument to xInput */
226384 int bEof; /* Set to true after xInput finished */
226385};
226386
226387/*
226388** Structure for changeset iterators.
226389*/
226390struct sqlite3_changeset_iter {
226391 SessionInput in; /* Input buffer or stream */
226392 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
226393 int bPatchset; /* True if this is a patchset */
226394 int bInvert; /* True to invert changeset */
226395 int bSkipEmpty; /* Skip noop UPDATE changes */
226396 int rc; /* Iterator error code */
226397 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
226398 char *zTab; /* Current table */
226399 int nCol; /* Number of columns in zTab */
226400 int op; /* Current operation */
226401 int bIndirect; /* True if current change was indirect */
226402 u8 *abPK; /* Primary key array */
226403 sqlite3_value **apValue; /* old.* and new.* values */
226404};
226405
226406/*
226407** Each session object maintains a set of the following structures, one
226408** for each table the session object is monitoring. The structures are
226409** stored in a linked list starting at sqlite3_session.pTable.
226410**
226411** The keys of the SessionTable.aChange[] hash table are all rows that have
226412** been modified in any way since the session object was attached to the
226413** table.
226414**
226415** The data associated with each hash-table entry is a structure containing
226416** a subset of the initial values that the modified row contained at the
226417** start of the session. Or no initial values if the row was inserted.
226418**
226419** pDfltStmt:
226420** This is only used by the sqlite3changegroup_xxx() APIs, not by
226421** regular sqlite3_session objects. It is a SELECT statement that
226422** selects the default value for each table column. For example,
226423** if the table is
226424**
226425** CREATE TABLE xx(a DEFAULT 1, b, c DEFAULT 'abc')
226426**
226427** then this variable is the compiled version of:
226428**
226429** SELECT 1, NULL, 'abc'
226430*/
226431struct SessionTable {
226432 SessionTable *pNext;
226433 char *zName; /* Local name of table */
226434 int nCol; /* Number of columns in table zName */
226435 int bStat1; /* True if this is sqlite_stat1 */
226436 int bRowid; /* True if this table uses rowid for PK */
226437 const char **azCol; /* Column names */
226438 const char **azDflt; /* Default value expressions */
226439 u8 *abPK; /* Array of primary key flags */
226440 int nEntry; /* Total number of entries in hash table */
226441 int nChange; /* Size of apChange[] array */
226442 SessionChange **apChange; /* Hash table buckets */
226443 sqlite3_stmt *pDfltStmt;
226444};
226445
226446/*
226447** RECORD FORMAT:
226448**
226449** The following record format is similar to (but not compatible with) that
226450** used in SQLite database files. This format is used as part of the
226451** change-set binary format, and so must be architecture independent.
226452**
226453** Unlike the SQLite database record format, each field is self-contained -
226454** there is no separation of header and data. Each field begins with a
226455** single byte describing its type, as follows:
226456**
226457** 0x00: Undefined value.
226458** 0x01: Integer value.
226459** 0x02: Real value.
226460** 0x03: Text value.
226461** 0x04: Blob value.
226462** 0x05: SQL NULL value.
226463**
226464** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
226465** and so on in sqlite3.h. For undefined and NULL values, the field consists
226466** only of the single type byte. For other types of values, the type byte
226467** is followed by:
226468**
226469** Text values:
226470** A varint containing the number of bytes in the value (encoded using
226471** UTF-8). Followed by a buffer containing the UTF-8 representation
226472** of the text value. There is no nul terminator.
226473**
226474** Blob values:
226475** A varint containing the number of bytes in the value, followed by
226476** a buffer containing the value itself.
226477**
226478** Integer values:
226479** An 8-byte big-endian integer value.
226480**
226481** Real values:
226482** An 8-byte big-endian IEEE 754-2008 real value.
226483**
226484** Varint values are encoded in the same way as varints in the SQLite
226485** record format.
226486**
226487** CHANGESET FORMAT:
226488**
226489** A changeset is a collection of DELETE, UPDATE and INSERT operations on
226490** one or more tables. Operations on a single table are grouped together,
226491** but may occur in any order (i.e. deletes, updates and inserts are all
226492** mixed together).
226493**
226494** Each group of changes begins with a table header:
226495**
226496** 1 byte: Constant 0x54 (capital 'T')
226497** Varint: Number of columns in the table.
226498** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
226499** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
226500**
226501** Followed by one or more changes to the table.
226502**
226503** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
226504** 1 byte: The "indirect-change" flag.
226505** old.* record: (delete and update only)
226506** new.* record: (insert and update only)
226507**
226508** The "old.*" and "new.*" records, if present, are N field records in the
226509** format described above under "RECORD FORMAT", where N is the number of
226510** columns in the table. The i'th field of each record is associated with
226511** the i'th column of the table, counting from left to right in the order
226512** in which columns were declared in the CREATE TABLE statement.
226513**
226514** The new.* record that is part of each INSERT change contains the values
226515** that make up the new row. Similarly, the old.* record that is part of each
226516** DELETE change contains the values that made up the row that was deleted
226517** from the database. In the changeset format, the records that are part
226518** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
226519** fields.
226520**
226521** Within the old.* record associated with an UPDATE change, all fields
226522** associated with table columns that are not PRIMARY KEY columns and are
226523** not modified by the UPDATE change are set to "undefined". Other fields
226524** are set to the values that made up the row before the UPDATE that the
226525** change records took place. Within the new.* record, fields associated
226526** with table columns modified by the UPDATE change contain the new
226527** values. Fields associated with table columns that are not modified
226528** are set to "undefined".
226529**
226530** PATCHSET FORMAT:
226531**
226532** A patchset is also a collection of changes. It is similar to a changeset,
226533** but leaves undefined those fields that are not useful if no conflict
226534** resolution is required when applying the changeset.
226535**
226536** Each group of changes begins with a table header:
226537**
226538** 1 byte: Constant 0x50 (capital 'P')
226539** Varint: Number of columns in the table.
226540** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
226541** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
226542**
226543** Followed by one or more changes to the table.
226544**
226545** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
226546** 1 byte: The "indirect-change" flag.
226547** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
226548** full record for INSERT).
226549**
226550** As in the changeset format, each field of the single record that is part
226551** of a patchset change is associated with the correspondingly positioned
226552** table column, counting from left to right within the CREATE TABLE
226553** statement.
226554**
226555** For a DELETE change, all fields within the record except those associated
226556** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
226557** values identifying the row to delete.
226558**
226559** For an UPDATE change, all fields except those associated with PRIMARY KEY
226560** columns and columns that are modified by the UPDATE are set to "undefined".
226561** PRIMARY KEY fields contain the values identifying the table row to update,
226562** and fields associated with modified columns contain the new column values.
226563**
226564** The records associated with INSERT changes are in the same format as for
226565** changesets. It is not possible for a record associated with an INSERT
226566** change to contain a field set to "undefined".
226567**
226568** REBASE BLOB FORMAT:
226569**
226570** A rebase blob may be output by sqlite3changeset_apply_v2() and its
226571** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
226572** existing changesets. A rebase blob contains one entry for each conflict
226573** resolved using either the OMIT or REPLACE strategies within the apply_v2()
226574** call.
226575**
226576** The format used for a rebase blob is very similar to that used for
226577** changesets. All entries related to a single table are grouped together.
226578**
226579** Each group of entries begins with a table header in changeset format:
226580**
226581** 1 byte: Constant 0x54 (capital 'T')
226582** Varint: Number of columns in the table.
226583** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
226584** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
226585**
226586** Followed by one or more entries associated with the table.
226587**
226588** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
226589** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
226590** record: (in the record format defined above).
226591**
226592** In a rebase blob, the first field is set to SQLITE_INSERT if the change
226593** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
226594** it was a DELETE. The second field is set to 0x01 if the conflict
226595** resolution strategy was REPLACE, or 0x00 if it was OMIT.
226596**
226597** If the change that caused the conflict was a DELETE, then the single
226598** record is a copy of the old.* record from the original changeset. If it
226599** was an INSERT, then the single record is a copy of the new.* record. If
226600** the conflicting change was an UPDATE, then the single record is a copy
226601** of the new.* record with the PK fields filled in based on the original
226602** old.* record.
226603*/
226604
226605/*
226606** For each row modified during a session, there exists a single instance of
226607** this structure stored in a SessionTable.aChange[] hash table.
226608*/
226609struct SessionChange {
226610 u8 op; /* One of UPDATE, DELETE, INSERT */
226611 u8 bIndirect; /* True if this change is "indirect" */
226612 u16 nRecordField; /* Number of fields in aRecord[] */
226613 int nMaxSize; /* Max size of eventual changeset record */
226614 int nRecord; /* Number of bytes in buffer aRecord[] */
226615 u8 *aRecord; /* Buffer containing old.* record */
226616 SessionChange *pNext; /* For hash-table collisions */
226617};
226618
226619/*
226620** Write a varint with value iVal into the buffer at aBuf. Return the
226621** number of bytes written.
226622*/
226623static int sessionVarintPut(u8 *aBuf, int iVal){
226624 return putVarint32(aBuf, iVal)(u8)(((u32)(iVal)<(u32)0x80)?(*(aBuf)=(unsigned char)(iVal
)),1: sqlite3PutVarint((aBuf),(iVal)))
;
226625}
226626
226627/*
226628** Return the number of bytes required to store value iVal as a varint.
226629*/
226630static int sessionVarintLen(int iVal){
226631 return sqlite3VarintLen(iVal);
226632}
226633
226634/*
226635** Read a varint value from aBuf[] into *piVal. Return the number of
226636** bytes read.
226637*/
226638static int sessionVarintGet(const u8 *aBuf, int *piVal){
226639 return getVarint32(aBuf, *piVal)(u8)((*(aBuf)<(u8)0x80)?((*piVal)=(u32)*(aBuf)),1:sqlite3GetVarint32
((aBuf),(u32 *)&(*piVal)))
;
226640}
226641
226642/* Load an unaligned and unsigned 32-bit integer */
226643#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])
226644
226645/*
226646** Read a 64-bit big-endian integer value from buffer aRec[]. Return
226647** the value read.
226648*/
226649static sqlite3_int64 sessionGetI64(u8 *aRec){
226650 u64 x = SESSION_UINT32(aRec)(((u32)(aRec)[0]<<24)|((aRec)[1]<<16)|((aRec)[2]<<
8)|(aRec)[3])
;
226651 u32 y = SESSION_UINT32(aRec+4)(((u32)(aRec+4)[0]<<24)|((aRec+4)[1]<<16)|((aRec+
4)[2]<<8)|(aRec+4)[3])
;
226652 x = (x<<32) + y;
226653 return (sqlite3_int64)x;
226654}
226655
226656/*
226657** Write a 64-bit big-endian integer value to the buffer aBuf[].
226658*/
226659static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
226660 aBuf[0] = (i>>56) & 0xFF;
226661 aBuf[1] = (i>>48) & 0xFF;
226662 aBuf[2] = (i>>40) & 0xFF;
226663 aBuf[3] = (i>>32) & 0xFF;
226664 aBuf[4] = (i>>24) & 0xFF;
226665 aBuf[5] = (i>>16) & 0xFF;
226666 aBuf[6] = (i>> 8) & 0xFF;
226667 aBuf[7] = (i>> 0) & 0xFF;
226668}
226669
226670/*
226671** This function is used to serialize the contents of value pValue (see
226672** comment titled "RECORD FORMAT" above).
226673**
226674** If it is non-NULL, the serialized form of the value is written to
226675** buffer aBuf. *pnWrite is set to the number of bytes written before
226676** returning. Or, if aBuf is NULL, the only thing this function does is
226677** set *pnWrite.
226678**
226679** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
226680** within a call to sqlite3_value_text() (may fail if the db is utf-16))
226681** SQLITE_NOMEM is returned.
226682*/
226683static int sessionSerializeValue(
226684 u8 *aBuf, /* If non-NULL, write serialized value here */
226685 sqlite3_value *pValue, /* Value to serialize */
226686 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
226687){
226688 int nByte; /* Size of serialized value in bytes */
226689
226690 if( pValue ){
226691 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
226692
226693 eType = sqlite3_value_type(pValue);
226694 if( aBuf ) aBuf[0] = eType;
226695
226696 switch( eType ){
226697 case SQLITE_NULL5:
226698 nByte = 1;
226699 break;
226700
226701 case SQLITE_INTEGER1:
226702 case SQLITE_FLOAT2:
226703 if( aBuf ){
226704 /* TODO: SQLite does something special to deal with mixed-endian
226705 ** floating point values (e.g. ARM7). This code probably should
226706 ** too. */
226707 u64 i;
226708 if( eType==SQLITE_INTEGER1 ){
226709 i = (u64)sqlite3_value_int64(pValue);
226710 }else{
226711 double r;
226712 assert( sizeof(double)==8 && sizeof(u64)==8 )((void) (0));
226713 r = sqlite3_value_double(pValue);
226714 memcpy(&i, &r, 8);
226715 }
226716 sessionPutI64(&aBuf[1], i);
226717 }
226718 nByte = 9;
226719 break;
226720
226721 default: {
226722 u8 *z;
226723 int n;
226724 int nVarint;
226725
226726 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
226727 if( eType==SQLITE_TEXT3 ){
226728 z = (u8 *)sqlite3_value_text(pValue);
226729 }else{
226730 z = (u8 *)sqlite3_value_blob(pValue);
226731 }
226732 n = sqlite3_value_bytes(pValue);
226733 if( z==0 && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
226734 nVarint = sessionVarintLen(n);
226735
226736 if( aBuf ){
226737 sessionVarintPut(&aBuf[1], n);
226738 if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n);
226739 }
226740
226741 nByte = 1 + nVarint + n;
226742 break;
226743 }
226744 }
226745 }else{
226746 nByte = 1;
226747 if( aBuf ) aBuf[0] = '\0';
226748 }
226749
226750 if( pnWrite ) *pnWrite += nByte;
226751 return SQLITE_OK0;
226752}
226753
226754/*
226755** Allocate and return a pointer to a buffer nByte bytes in size. If
226756** pSession is not NULL, increase the sqlite3_session.nMalloc variable
226757** by the number of bytes allocated.
226758*/
226759static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){
226760 void *pRet = sqlite3_malloc64(nByte);
226761 if( pSession ) pSession->nMalloc += sqlite3_msize(pRet);
226762 return pRet;
226763}
226764
226765/*
226766** Free buffer pFree, which must have been allocated by an earlier
226767** call to sessionMalloc64(). If pSession is not NULL, decrease the
226768** sqlite3_session.nMalloc counter by the number of bytes freed.
226769*/
226770static void sessionFree(sqlite3_session *pSession, void *pFree){
226771 if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree);
226772 sqlite3_free(pFree);
226773}
226774
226775/*
226776** This macro is used to calculate hash key values for data structures. In
226777** order to use this macro, the entire data structure must be represented
226778** as a series of unsigned integers. In order to calculate a hash-key value
226779** for a data structure represented as three such integers, the macro may
226780** then be used as follows:
226781**
226782** int hash_key_value;
226783** hash_key_value = HASH_APPEND(0, <value 1>);
226784** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
226785** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
226786**
226787** In practice, the data structures this macro is used for are the primary
226788** key values of modified rows.
226789*/
226790#define HASH_APPEND(hash, add)((hash) << 3) ^ (hash) ^ (unsigned int)(add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
226791
226792/*
226793** Append the hash of the 64-bit integer passed as the second argument to the
226794** hash-key value passed as the first. Return the new hash-key value.
226795*/
226796static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
226797 h = HASH_APPEND(h, i & 0xFFFFFFFF)((h) << 3) ^ (h) ^ (unsigned int)(i & 0xFFFFFFFF);
226798 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF)((h) << 3) ^ (h) ^ (unsigned int)((i>>32)&0xFFFFFFFF
)
;
226799}
226800
226801/*
226802** Append the hash of the blob passed via the second and third arguments to
226803** the hash-key value passed as the first. Return the new hash-key value.
226804*/
226805static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
226806 int i;
226807 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i])((h) << 3) ^ (h) ^ (unsigned int)(z[i]);
226808 return h;
226809}
226810
226811/*
226812** Append the hash of the data type passed as the second argument to the
226813** hash-key value passed as the first. Return the new hash-key value.
226814*/
226815static unsigned int sessionHashAppendType(unsigned int h, int eType){
226816 return HASH_APPEND(h, eType)((h) << 3) ^ (h) ^ (unsigned int)(eType);
226817}
226818
226819/*
226820** This function may only be called from within a pre-update callback.
226821** It calculates a hash based on the primary key values of the old.* or
226822** new.* row currently available and, assuming no error occurs, writes it to
226823** *piHash before returning. If the primary key contains one or more NULL
226824** values, *pbNullPK is set to true before returning.
226825**
226826** If an error occurs, an SQLite error code is returned and the final values
226827** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
226828** and the output variables are set as described above.
226829*/
226830static int sessionPreupdateHash(
226831 sqlite3_session *pSession, /* Session object that owns pTab */
226832 i64 iRowid,
226833 SessionTable *pTab, /* Session table handle */
226834 int bNew, /* True to hash the new.* PK */
226835 int *piHash, /* OUT: Hash value */
226836 int *pbNullPK /* OUT: True if there are NULL values in PK */
226837){
226838 unsigned int h = 0; /* Hash value to return */
226839 int i; /* Used to iterate through columns */
226840
226841 if( pTab->bRowid ){
226842 assert( pTab->nCol-1==pSession->hook.xCount(pSession->hook.pCtx) )((void) (0));
226843 h = sessionHashAppendI64(h, iRowid);
226844 }else{
226845 assert( *pbNullPK==0 )((void) (0));
226846 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) )((void) (0));
226847 for(i=0; i<pTab->nCol; i++){
226848 if( pTab->abPK[i] ){
226849 int rc;
226850 int eType;
226851 sqlite3_value *pVal;
226852
226853 if( bNew ){
226854 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
226855 }else{
226856 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
226857 }
226858 if( rc!=SQLITE_OK0 ) return rc;
226859
226860 eType = sqlite3_value_type(pVal);
226861 h = sessionHashAppendType(h, eType);
226862 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
226863 i64 iVal;
226864 if( eType==SQLITE_INTEGER1 ){
226865 iVal = sqlite3_value_int64(pVal);
226866 }else{
226867 double rVal = sqlite3_value_double(pVal);
226868 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
226869 memcpy(&iVal, &rVal, 8);
226870 }
226871 h = sessionHashAppendI64(h, iVal);
226872 }else if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
226873 const u8 *z;
226874 int n;
226875 if( eType==SQLITE_TEXT3 ){
226876 z = (const u8 *)sqlite3_value_text(pVal);
226877 }else{
226878 z = (const u8 *)sqlite3_value_blob(pVal);
226879 }
226880 n = sqlite3_value_bytes(pVal);
226881 if( !z && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
226882 h = sessionHashAppendBlob(h, n, z);
226883 }else{
226884 assert( eType==SQLITE_NULL )((void) (0));
226885 assert( pTab->bStat1==0 || i!=1 )((void) (0));
226886 *pbNullPK = 1;
226887 }
226888 }
226889 }
226890 }
226891
226892 *piHash = (h % pTab->nChange);
226893 return SQLITE_OK0;
226894}
226895
226896/*
226897** The buffer that the argument points to contains a serialized SQL value.
226898** Return the number of bytes of space occupied by the value (including
226899** the type byte).
226900*/
226901static int sessionSerialLen(const u8 *a){
226902 int e;
226903 int n;
226904 assert( a!=0 )((void) (0));
226905 e = *a;
226906 if( e==0 || e==0xFF ) return 1;
226907 if( e==SQLITE_NULL5 ) return 1;
226908 if( e==SQLITE_INTEGER1 || e==SQLITE_FLOAT2 ) return 9;
226909 return sessionVarintGet(&a[1], &n) + 1 + n;
226910}
226911
226912/*
226913** Based on the primary key values stored in change aRecord, calculate a
226914** hash key. Assume the has table has nBucket buckets. The hash keys
226915** calculated by this function are compatible with those calculated by
226916** sessionPreupdateHash().
226917**
226918** The bPkOnly argument is non-zero if the record at aRecord[] is from
226919** a patchset DELETE. In this case the non-PK fields are omitted entirely.
226920*/
226921static unsigned int sessionChangeHash(
226922 SessionTable *pTab, /* Table handle */
226923 int bPkOnly, /* Record consists of PK fields only */
226924 u8 *aRecord, /* Change record */
226925 int nBucket /* Assume this many buckets in hash table */
226926){
226927 unsigned int h = 0; /* Value to return */
226928 int i; /* Used to iterate through columns */
226929 u8 *a = aRecord; /* Used to iterate through change record */
226930
226931 for(i=0; i<pTab->nCol; i++){
226932 int eType = *a;
226933 int isPK = pTab->abPK[i];
226934 if( bPkOnly && isPK==0 ) continue;
226935
226936 /* It is not possible for eType to be SQLITE_NULL here. The session
226937 ** module does not record changes for rows with NULL values stored in
226938 ** primary key columns. */
226939 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
226940 || eType==SQLITE_TEXT || eType==SQLITE_BLOB((void) (0))
226941 || eType==SQLITE_NULL || eType==0((void) (0))
226942 )((void) (0));
226943 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) )((void) (0));
226944
226945 if( isPK ){
226946 a++;
226947 h = sessionHashAppendType(h, eType);
226948 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
226949 h = sessionHashAppendI64(h, sessionGetI64(a));
226950 a += 8;
226951 }else{
226952 int n;
226953 a += sessionVarintGet(a, &n);
226954 h = sessionHashAppendBlob(h, n, a);
226955 a += n;
226956 }
226957 }else{
226958 a += sessionSerialLen(a);
226959 }
226960 }
226961 return (h % nBucket);
226962}
226963
226964/*
226965** Arguments aLeft and aRight are pointers to change records for table pTab.
226966** This function returns true if the two records apply to the same row (i.e.
226967** have the same values stored in the primary key columns), or false
226968** otherwise.
226969*/
226970static int sessionChangeEqual(
226971 SessionTable *pTab, /* Table used for PK definition */
226972 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
226973 u8 *aLeft, /* Change record */
226974 int bRightPkOnly, /* True if aRight[] contains PK fields only */
226975 u8 *aRight /* Change record */
226976){
226977 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
226978 u8 *a2 = aRight; /* Cursor to iterate through aRight */
226979 int iCol; /* Used to iterate through table columns */
226980
226981 for(iCol=0; iCol<pTab->nCol; iCol++){
226982 if( pTab->abPK[iCol] ){
226983 int n1 = sessionSerialLen(a1);
226984 int n2 = sessionSerialLen(a2);
226985
226986 if( n1!=n2 || memcmp(a1, a2, n1) ){
226987 return 0;
226988 }
226989 a1 += n1;
226990 a2 += n2;
226991 }else{
226992 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
226993 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
226994 }
226995 }
226996
226997 return 1;
226998}
226999
227000/*
227001** Arguments aLeft and aRight both point to buffers containing change
227002** records with nCol columns. This function "merges" the two records into
227003** a single records which is written to the buffer at *paOut. *paOut is
227004** then set to point to one byte after the last byte written before
227005** returning.
227006**
227007** The merging of records is done as follows: For each column, if the
227008** aRight record contains a value for the column, copy the value from
227009** their. Otherwise, if aLeft contains a value, copy it. If neither
227010** record contains a value for a given column, then neither does the
227011** output record.
227012*/
227013static void sessionMergeRecord(
227014 u8 **paOut,
227015 int nCol,
227016 u8 *aLeft,
227017 u8 *aRight
227018){
227019 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
227020 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
227021 u8 *aOut = *paOut; /* Output cursor */
227022 int iCol; /* Used to iterate from 0 to nCol */
227023
227024 for(iCol=0; iCol<nCol; iCol++){
227025 int n1 = sessionSerialLen(a1);
227026 int n2 = sessionSerialLen(a2);
227027 if( *a2 ){
227028 memcpy(aOut, a2, n2);
227029 aOut += n2;
227030 }else{
227031 memcpy(aOut, a1, n1);
227032 aOut += n1;
227033 }
227034 a1 += n1;
227035 a2 += n2;
227036 }
227037
227038 *paOut = aOut;
227039}
227040
227041/*
227042** This is a helper function used by sessionMergeUpdate().
227043**
227044** When this function is called, both *paOne and *paTwo point to a value
227045** within a change record. Before it returns, both have been advanced so
227046** as to point to the next value in the record.
227047**
227048** If, when this function is called, *paTwo points to a valid value (i.e.
227049** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
227050** pointer is returned and *pnVal is set to the number of bytes in the
227051** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
227052** set to the number of bytes in the value at *paOne. If *paOne points
227053** to the "no value" placeholder, *pnVal is set to 1. In other words:
227054**
227055** if( *paTwo is valid ) return *paTwo;
227056** return *paOne;
227057**
227058*/
227059static u8 *sessionMergeValue(
227060 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
227061 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
227062 int *pnVal /* OUT: Bytes in returned value */
227063){
227064 u8 *a1 = *paOne;
227065 u8 *a2 = *paTwo;
227066 u8 *pRet = 0;
227067 int n1;
227068
227069 assert( a1 )((void) (0));
227070 if( a2 ){
227071 int n2 = sessionSerialLen(a2);
227072 if( *a2 ){
227073 *pnVal = n2;
227074 pRet = a2;
227075 }
227076 *paTwo = &a2[n2];
227077 }
227078
227079 n1 = sessionSerialLen(a1);
227080 if( pRet==0 ){
227081 *pnVal = n1;
227082 pRet = a1;
227083 }
227084 *paOne = &a1[n1];
227085
227086 return pRet;
227087}
227088
227089/*
227090** This function is used by changeset_concat() to merge two UPDATE changes
227091** on the same row.
227092*/
227093static int sessionMergeUpdate(
227094 u8 **paOut, /* IN/OUT: Pointer to output buffer */
227095 SessionTable *pTab, /* Table change pertains to */
227096 int bPatchset, /* True if records are patchset records */
227097 u8 *aOldRecord1, /* old.* record for first change */
227098 u8 *aOldRecord2, /* old.* record for second change */
227099 u8 *aNewRecord1, /* new.* record for first change */
227100 u8 *aNewRecord2 /* new.* record for second change */
227101){
227102 u8 *aOld1 = aOldRecord1;
227103 u8 *aOld2 = aOldRecord2;
227104 u8 *aNew1 = aNewRecord1;
227105 u8 *aNew2 = aNewRecord2;
227106
227107 u8 *aOut = *paOut;
227108 int i;
227109
227110 if( bPatchset==0 ){
227111 int bRequired = 0;
227112
227113 assert( aOldRecord1 && aNewRecord1 )((void) (0));
227114
227115 /* Write the old.* vector first. */
227116 for(i=0; i<pTab->nCol; i++){
227117 int nOld;
227118 u8 *aOld;
227119 int nNew;
227120 u8 *aNew;
227121
227122 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
227123 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
227124 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
227125 if( pTab->abPK[i]==0 ) bRequired = 1;
227126 memcpy(aOut, aOld, nOld);
227127 aOut += nOld;
227128 }else{
227129 *(aOut++) = '\0';
227130 }
227131 }
227132
227133 if( !bRequired ) return 0;
227134 }
227135
227136 /* Write the new.* vector */
227137 aOld1 = aOldRecord1;
227138 aOld2 = aOldRecord2;
227139 aNew1 = aNewRecord1;
227140 aNew2 = aNewRecord2;
227141 for(i=0; i<pTab->nCol; i++){
227142 int nOld;
227143 u8 *aOld;
227144 int nNew;
227145 u8 *aNew;
227146
227147 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
227148 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
227149 if( bPatchset==0
227150 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
227151 ){
227152 *(aOut++) = '\0';
227153 }else{
227154 memcpy(aOut, aNew, nNew);
227155 aOut += nNew;
227156 }
227157 }
227158
227159 *paOut = aOut;
227160 return 1;
227161}
227162
227163/*
227164** This function is only called from within a pre-update-hook callback.
227165** It determines if the current pre-update-hook change affects the same row
227166** as the change stored in argument pChange. If so, it returns true. Otherwise
227167** if the pre-update-hook does not affect the same row as pChange, it returns
227168** false.
227169*/
227170static int sessionPreupdateEqual(
227171 sqlite3_session *pSession, /* Session object that owns SessionTable */
227172 i64 iRowid, /* Rowid value if pTab->bRowid */
227173 SessionTable *pTab, /* Table associated with change */
227174 SessionChange *pChange, /* Change to compare to */
227175 int op /* Current pre-update operation */
227176){
227177 int iCol; /* Used to iterate through columns */
227178 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
227179
227180 if( pTab->bRowid ){
227181 if( a[0]!=SQLITE_INTEGER1 ) return 0;
227182 return sessionGetI64(&a[1])==iRowid;
227183 }
227184
227185 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE )((void) (0));
227186 for(iCol=0; iCol<pTab->nCol; iCol++){
227187 if( !pTab->abPK[iCol] ){
227188 a += sessionSerialLen(a);
227189 }else{
227190 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
227191 int rc; /* Error code from preupdate_new/old */
227192 int eType = *a++; /* Type of value from change record */
227193
227194 /* The following calls to preupdate_new() and preupdate_old() can not
227195 ** fail. This is because they cache their return values, and by the
227196 ** time control flows to here they have already been called once from
227197 ** within sessionPreupdateHash(). The first two asserts below verify
227198 ** this (that the method has already been called). */
227199 if( op==SQLITE_INSERT18 ){
227200 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
227201 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
227202 }else{
227203 /* assert( db->pPreUpdate->pUnpacked ); */
227204 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
227205 }
227206 assert( rc==SQLITE_OK )((void) (0));
227207 (void)rc; /* Suppress warning about unused variable */
227208 if( sqlite3_value_type(pVal)!=eType ) return 0;
227209
227210 /* A SessionChange object never has a NULL value in a PK column */
227211 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
227212 || eType==SQLITE_BLOB || eType==SQLITE_TEXT((void) (0))
227213 )((void) (0));
227214
227215 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
227216 i64 iVal = sessionGetI64(a);
227217 a += 8;
227218 if( eType==SQLITE_INTEGER1 ){
227219 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
227220 }else{
227221 double rVal;
227222 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
227223 memcpy(&rVal, &iVal, 8);
227224 if( sqlite3_value_double(pVal)!=rVal ) return 0;
227225 }
227226 }else{
227227 int n;
227228 const u8 *z;
227229 a += sessionVarintGet(a, &n);
227230 if( sqlite3_value_bytes(pVal)!=n ) return 0;
227231 if( eType==SQLITE_TEXT3 ){
227232 z = sqlite3_value_text(pVal);
227233 }else{
227234 z = sqlite3_value_blob(pVal);
227235 }
227236 if( n>0 && memcmp(a, z, n) ) return 0;
227237 a += n;
227238 }
227239 }
227240 }
227241
227242 return 1;
227243}
227244
227245/*
227246** If required, grow the hash table used to store changes on table pTab
227247** (part of the session pSession). If a fatal OOM error occurs, set the
227248** session object to failed and return SQLITE_ERROR. Otherwise, return
227249** SQLITE_OK.
227250**
227251** It is possible that a non-fatal OOM error occurs in this function. In
227252** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
227253** Growing the hash table in this case is a performance optimization only,
227254** it is not required for correct operation.
227255*/
227256static int sessionGrowHash(
227257 sqlite3_session *pSession, /* For memory accounting. May be NULL */
227258 int bPatchset,
227259 SessionTable *pTab
227260){
227261 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
227262 int i;
227263 SessionChange **apNew;
227264 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
227265
227266 apNew = (SessionChange**)sessionMalloc64(
227267 pSession, sizeof(SessionChange*) * nNew
227268 );
227269 if( apNew==0 ){
227270 if( pTab->nChange==0 ){
227271 return SQLITE_ERROR1;
227272 }
227273 return SQLITE_OK0;
227274 }
227275 memset(apNew, 0, sizeof(SessionChange *) * nNew);
227276
227277 for(i=0; i<pTab->nChange; i++){
227278 SessionChange *p;
227279 SessionChange *pNext;
227280 for(p=pTab->apChange[i]; p; p=pNext){
227281 int bPkOnly = (p->op==SQLITE_DELETE9 && bPatchset);
227282 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
227283 pNext = p->pNext;
227284 p->pNext = apNew[iHash];
227285 apNew[iHash] = p;
227286 }
227287 }
227288
227289 sessionFree(pSession, pTab->apChange);
227290 pTab->nChange = nNew;
227291 pTab->apChange = apNew;
227292 }
227293
227294 return SQLITE_OK0;
227295}
227296
227297/*
227298** This function queries the database for the names of the columns of table
227299** zThis, in schema zDb.
227300**
227301** Otherwise, if they are not NULL, variable *pnCol is set to the number
227302** of columns in the database table and variable *pzTab is set to point to a
227303** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
227304** point to an array of pointers to column names. And *pabPK (again, if not
227305** NULL) is set to point to an array of booleans - true if the corresponding
227306** column is part of the primary key.
227307**
227308** For example, if the table is declared as:
227309**
227310** CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z));
227311**
227312** Then the five output variables are populated as follows:
227313**
227314** *pnCol = 4
227315** *pzTab = "tbl1"
227316** *pazCol = {"w", "x", "y", "z"}
227317** *pazDflt = {NULL, 'abc', NULL, NULL}
227318** *pabPK = {1, 0, 0, 1}
227319**
227320** All returned buffers are part of the same single allocation, which must
227321** be freed using sqlite3_free() by the caller
227322*/
227323static int sessionTableInfo(
227324 sqlite3_session *pSession, /* For memory accounting. May be NULL */
227325 sqlite3 *db, /* Database connection */
227326 const char *zDb, /* Name of attached database (e.g. "main") */
227327 const char *zThis, /* Table name */
227328 int *pnCol, /* OUT: number of columns */
227329 const char **pzTab, /* OUT: Copy of zThis */
227330 const char ***pazCol, /* OUT: Array of column names for table */
227331 const char ***pazDflt, /* OUT: Array of default value expressions */
227332 u8 **pabPK, /* OUT: Array of booleans - true for PK col */
227333 int *pbRowid /* OUT: True if only PK is a rowid */
227334){
227335 char *zPragma;
227336 sqlite3_stmt *pStmt;
227337 int rc;
227338 sqlite3_int64 nByte;
227339 int nDbCol = 0;
227340 int nThis;
227341 int i;
227342 u8 *pAlloc = 0;
227343 char **azCol = 0;
227344 char **azDflt = 0;
227345 u8 *abPK = 0;
227346 int bRowid = 0; /* Set to true to use rowid as PK */
227347
227348 assert( pazCol && pabPK )((void) (0));
227349
227350 *pazCol = 0;
227351 *pabPK = 0;
227352 *pnCol = 0;
227353 if( pzTab ) *pzTab = 0;
227354 if( pazDflt ) *pazDflt = 0;
227355
227356 nThis = sqlite3Strlen30(zThis);
227357 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
227358 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
227359 if( rc==SQLITE_OK0 ){
227360 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
227361 zPragma = sqlite3_mprintf(
227362 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
227363 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
227364 "SELECT 2, 'stat', '', 0, '', 0"
227365 );
227366 }else if( rc==SQLITE_ERROR1 ){
227367 zPragma = sqlite3_mprintf("");
227368 }else{
227369 return rc;
227370 }
227371 }else{
227372 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
227373 }
227374 if( !zPragma ){
227375 return SQLITE_NOMEM7;
227376 }
227377
227378 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
227379 sqlite3_free(zPragma);
227380 if( rc!=SQLITE_OK0 ){
227381 return rc;
227382 }
227383
227384 nByte = nThis + 1;
227385 bRowid = (pbRowid!=0);
227386 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
227387 nByte += sqlite3_column_bytes(pStmt, 1); /* name */
227388 nByte += sqlite3_column_bytes(pStmt, 4); /* dflt_value */
227389 nDbCol++;
227390 if( sqlite3_column_int(pStmt, 5) ) bRowid = 0; /* pk */
227391 }
227392 if( nDbCol==0 ) bRowid = 0;
227393 nDbCol += bRowid;
227394 nByte += strlen(SESSIONS_ROWID"_rowid_");
227395 rc = sqlite3_reset(pStmt);
227396
227397 if( rc==SQLITE_OK0 ){
227398 nByte += nDbCol * (sizeof(const char *)*2 + sizeof(u8) + 1 + 1);
227399 pAlloc = sessionMalloc64(pSession, nByte);
227400 if( pAlloc==0 ){
227401 rc = SQLITE_NOMEM7;
227402 }else{
227403 memset(pAlloc, 0, nByte);
227404 }
227405 }
227406 if( rc==SQLITE_OK0 ){
227407 azCol = (char **)pAlloc;
227408 azDflt = (char**)&azCol[nDbCol];
227409 pAlloc = (u8 *)&azDflt[nDbCol];
227410 abPK = (u8 *)pAlloc;
227411 pAlloc = &abPK[nDbCol];
227412 if( pzTab ){
227413 memcpy(pAlloc, zThis, nThis+1);
227414 *pzTab = (char *)pAlloc;
227415 pAlloc += nThis+1;
227416 }
227417
227418 i = 0;
227419 if( bRowid ){
227420 size_t nName = strlen(SESSIONS_ROWID"_rowid_");
227421 memcpy(pAlloc, SESSIONS_ROWID"_rowid_", nName+1);
227422 azCol[i] = (char*)pAlloc;
227423 pAlloc += nName+1;
227424 abPK[i] = 1;
227425 i++;
227426 }
227427 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
227428 int nName = sqlite3_column_bytes(pStmt, 1);
227429 int nDflt = sqlite3_column_bytes(pStmt, 4);
227430 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
227431 const unsigned char *zDflt = sqlite3_column_text(pStmt, 4);
227432
227433 if( zName==0 ) break;
227434 memcpy(pAlloc, zName, nName+1);
227435 azCol[i] = (char *)pAlloc;
227436 pAlloc += nName+1;
227437 if( zDflt ){
227438 memcpy(pAlloc, zDflt, nDflt+1);
227439 azDflt[i] = (char *)pAlloc;
227440 pAlloc += nDflt+1;
227441 }else{
227442 azDflt[i] = 0;
227443 }
227444 abPK[i] = sqlite3_column_int(pStmt, 5);
227445 i++;
227446 }
227447 rc = sqlite3_reset(pStmt);
227448 }
227449
227450 /* If successful, populate the output variables. Otherwise, zero them and
227451 ** free any allocation made. An error code will be returned in this case.
227452 */
227453 if( rc==SQLITE_OK0 ){
227454 *pazCol = (const char**)azCol;
227455 if( pazDflt ) *pazDflt = (const char**)azDflt;
227456 *pabPK = abPK;
227457 *pnCol = nDbCol;
227458 }else{
227459 sessionFree(pSession, azCol);
227460 }
227461 if( pbRowid ) *pbRowid = bRowid;
227462 sqlite3_finalize(pStmt);
227463 return rc;
227464}
227465
227466/*
227467** This function is called to initialize the SessionTable.nCol, azCol[]
227468** abPK[] and azDflt[] members of SessionTable object pTab. If these
227469** fields are already initilialized, this function is a no-op.
227470**
227471** If an error occurs, an error code is stored in sqlite3_session.rc and
227472** non-zero returned. Or, if no error occurs but the table has no primary
227473** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
227474** indicate that updates on this table should be ignored. SessionTable.abPK
227475** is set to NULL in this case.
227476*/
227477static int sessionInitTable(
227478 sqlite3_session *pSession, /* Optional session handle */
227479 SessionTable *pTab, /* Table object to initialize */
227480 sqlite3 *db, /* Database handle to read schema from */
227481 const char *zDb /* Name of db - "main", "temp" etc. */
227482){
227483 int rc = SQLITE_OK0;
227484
227485 if( pTab->nCol==0 ){
227486 u8 *abPK;
227487 assert( pTab->azCol==0 || pTab->abPK==0 )((void) (0));
227488 rc = sessionTableInfo(pSession, db, zDb,
227489 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &pTab->azDflt, &abPK,
227490 ((pSession==0 || pSession->bImplicitPK) ? &pTab->bRowid : 0)
227491 );
227492 if( rc==SQLITE_OK0 ){
227493 int i;
227494 for(i=0; i<pTab->nCol; i++){
227495 if( abPK[i] ){
227496 pTab->abPK = abPK;
227497 break;
227498 }
227499 }
227500 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
227501 pTab->bStat1 = 1;
227502 }
227503
227504 if( pSession && pSession->bEnableSize ){
227505 pSession->nMaxChangesetSize += (
227506 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1
227507 );
227508 }
227509 }
227510 }
227511
227512 if( pSession ){
227513 pSession->rc = rc;
227514 return (rc || pTab->abPK==0);
227515 }
227516 return rc;
227517}
227518
227519/*
227520** Re-initialize table object pTab.
227521*/
227522static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){
227523 int nCol = 0;
227524 const char **azCol = 0;
227525 const char **azDflt = 0;
227526 u8 *abPK = 0;
227527 int bRowid = 0;
227528
227529 assert( pSession->rc==SQLITE_OK )((void) (0));
227530
227531 pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb,
227532 pTab->zName, &nCol, 0, &azCol, &azDflt, &abPK,
227533 (pSession->bImplicitPK ? &bRowid : 0)
227534 );
227535 if( pSession->rc==SQLITE_OK0 ){
227536 if( pTab->nCol>nCol || pTab->bRowid!=bRowid ){
227537 pSession->rc = SQLITE_SCHEMA17;
227538 }else{
227539 int ii;
227540 int nOldCol = pTab->nCol;
227541 for(ii=0; ii<nCol; ii++){
227542 if( ii<pTab->nCol ){
227543 if( pTab->abPK[ii]!=abPK[ii] ){
227544 pSession->rc = SQLITE_SCHEMA17;
227545 }
227546 }else if( abPK[ii] ){
227547 pSession->rc = SQLITE_SCHEMA17;
227548 }
227549 }
227550
227551 if( pSession->rc==SQLITE_OK0 ){
227552 const char **a = pTab->azCol;
227553 pTab->azCol = azCol;
227554 pTab->nCol = nCol;
227555 pTab->azDflt = azDflt;
227556 pTab->abPK = abPK;
227557 azCol = a;
227558 }
227559 if( pSession->bEnableSize ){
227560 pSession->nMaxChangesetSize += (nCol - nOldCol);
227561 pSession->nMaxChangesetSize += sessionVarintLen(nCol);
227562 pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol);
227563 }
227564 }
227565 }
227566
227567 sqlite3_free((char*)azCol);
227568 return pSession->rc;
227569}
227570
227571/*
227572** Session-change object (*pp) contains an old.* record with fewer than
227573** nCol fields. This function updates it with the default values for
227574** the missing fields.
227575*/
227576static void sessionUpdateOneChange(
227577 sqlite3_session *pSession, /* For memory accounting */
227578 int *pRc, /* IN/OUT: Error code */
227579 SessionChange **pp, /* IN/OUT: Change object to update */
227580 int nCol, /* Number of columns now in table */
227581 sqlite3_stmt *pDflt /* SELECT <default-values...> */
227582){
227583 SessionChange *pOld = *pp;
227584
227585 while( pOld->nRecordField<nCol ){
227586 SessionChange *pNew = 0;
227587 int nByte = 0;
227588 int nIncr = 0;
227589 int iField = pOld->nRecordField;
227590 int eType = sqlite3_column_type(pDflt, iField);
227591 switch( eType ){
227592 case SQLITE_NULL5:
227593 nIncr = 1;
227594 break;
227595 case SQLITE_INTEGER1:
227596 case SQLITE_FLOAT2:
227597 nIncr = 9;
227598 break;
227599 default: {
227600 int n = sqlite3_column_bytes(pDflt, iField);
227601 nIncr = 1 + sessionVarintLen(n) + n;
227602 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
227603 break;
227604 }
227605 }
227606
227607 nByte = nIncr + (sizeof(SessionChange) + pOld->nRecord);
227608 pNew = sessionMalloc64(pSession, nByte);
227609 if( pNew==0 ){
227610 *pRc = SQLITE_NOMEM7;
227611 return;
227612 }else{
227613 memcpy(pNew, pOld, sizeof(SessionChange));
227614 pNew->aRecord = (u8*)&pNew[1];
227615 memcpy(pNew->aRecord, pOld->aRecord, pOld->nRecord);
227616 pNew->aRecord[pNew->nRecord++] = (u8)eType;
227617 switch( eType ){
227618 case SQLITE_INTEGER1: {
227619 i64 iVal = sqlite3_column_int64(pDflt, iField);
227620 sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
227621 pNew->nRecord += 8;
227622 break;
227623 }
227624
227625 case SQLITE_FLOAT2: {
227626 double rVal = sqlite3_column_double(pDflt, iField);
227627 i64 iVal = 0;
227628 memcpy(&iVal, &rVal, sizeof(rVal));
227629 sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal);
227630 pNew->nRecord += 8;
227631 break;
227632 }
227633
227634 case SQLITE_TEXT3: {
227635 int n = sqlite3_column_bytes(pDflt, iField);
227636 const char *z = (const char*)sqlite3_column_text(pDflt, iField);
227637 pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
227638 memcpy(&pNew->aRecord[pNew->nRecord], z, n);
227639 pNew->nRecord += n;
227640 break;
227641 }
227642
227643 case SQLITE_BLOB4: {
227644 int n = sqlite3_column_bytes(pDflt, iField);
227645 const u8 *z = (const u8*)sqlite3_column_blob(pDflt, iField);
227646 pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n);
227647 memcpy(&pNew->aRecord[pNew->nRecord], z, n);
227648 pNew->nRecord += n;
227649 break;
227650 }
227651
227652 default:
227653 assert( eType==SQLITE_NULL )((void) (0));
227654 break;
227655 }
227656
227657 sessionFree(pSession, pOld);
227658 *pp = pOld = pNew;
227659 pNew->nRecordField++;
227660 pNew->nMaxSize += nIncr;
227661 if( pSession ){
227662 pSession->nMaxChangesetSize += nIncr;
227663 }
227664 }
227665 }
227666}
227667
227668/*
227669** Ensure that there is room in the buffer to append nByte bytes of data.
227670** If not, use sqlite3_realloc() to grow the buffer so that there is.
227671**
227672** If successful, return zero. Otherwise, if an OOM condition is encountered,
227673** set *pRc to SQLITE_NOMEM and return non-zero.
227674*/
227675static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){
227676#define SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1) (0x7FFFFF00 - 1)
227677 i64 nReq = p->nBuf + nByte;
227678 if( *pRc==SQLITE_OK0 && nReq>p->nAlloc ){
227679 u8 *aNew;
227680 i64 nNew = p->nAlloc ? p->nAlloc : 128;
227681
227682 do {
227683 nNew = nNew*2;
227684 }while( nNew<nReq );
227685
227686 /* The value of SESSION_MAX_BUFFER_SZ is copied from the implementation
227687 ** of sqlite3_realloc64(). Allocations greater than this size in bytes
227688 ** always fail. It is used here to ensure that this routine can always
227689 ** allocate up to this limit - instead of up to the largest power of
227690 ** two smaller than the limit. */
227691 if( nNew>SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1) ){
227692 nNew = SESSION_MAX_BUFFER_SZ(0x7FFFFF00 - 1);
227693 if( nNew<nReq ){
227694 *pRc = SQLITE_NOMEM7;
227695 return 1;
227696 }
227697 }
227698
227699 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
227700 if( 0==aNew ){
227701 *pRc = SQLITE_NOMEM7;
227702 }else{
227703 p->aBuf = aNew;
227704 p->nAlloc = nNew;
227705 }
227706 }
227707 return (*pRc!=SQLITE_OK0);
227708}
227709
227710
227711/*
227712** This function is a no-op if *pRc is other than SQLITE_OK when it is
227713** called. Otherwise, append a string to the buffer. All bytes in the string
227714** up to (but not including) the nul-terminator are written to the buffer.
227715**
227716** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
227717** returning.
227718*/
227719static void sessionAppendStr(
227720 SessionBuffer *p,
227721 const char *zStr,
227722 int *pRc
227723){
227724 int nStr = sqlite3Strlen30(zStr);
227725 if( 0==sessionBufferGrow(p, nStr+1, pRc) ){
227726 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
227727 p->nBuf += nStr;
227728 p->aBuf[p->nBuf] = 0x00;
227729 }
227730}
227731
227732/*
227733** Format a string using printf() style formatting and then append it to the
227734** buffer using sessionAppendString().
227735*/
227736static void sessionAppendPrintf(
227737 SessionBuffer *p, /* Buffer to append to */
227738 int *pRc,
227739 const char *zFmt,
227740 ...
227741){
227742 if( *pRc==SQLITE_OK0 ){
227743 char *zApp = 0;
227744 va_list ap;
227745 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
227746 zApp = sqlite3_vmprintf(zFmt, ap);
227747 if( zApp==0 ){
227748 *pRc = SQLITE_NOMEM7;
227749 }else{
227750 sessionAppendStr(p, zApp, pRc);
227751 }
227752 va_end(ap)__builtin_va_end(ap);
227753 sqlite3_free(zApp);
227754 }
227755}
227756
227757/*
227758** Prepare a statement against database handle db that SELECTs a single
227759** row containing the default values for each column in table pTab. For
227760** example, if pTab is declared as:
227761**
227762** CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd');
227763**
227764** Then this function prepares and returns the SQL statement:
227765**
227766** SELECT NULL, 123, 'abcd';
227767*/
227768static int sessionPrepareDfltStmt(
227769 sqlite3 *db, /* Database handle */
227770 SessionTable *pTab, /* Table to prepare statement for */
227771 sqlite3_stmt **ppStmt /* OUT: Statement handle */
227772){
227773 SessionBuffer sql = {0,0,0};
227774 int rc = SQLITE_OK0;
227775 const char *zSep = " ";
227776 int ii = 0;
227777
227778 *ppStmt = 0;
227779 sessionAppendPrintf(&sql, &rc, "SELECT");
227780 for(ii=0; ii<pTab->nCol; ii++){
227781 const char *zDflt = pTab->azDflt[ii] ? pTab->azDflt[ii] : "NULL";
227782 sessionAppendPrintf(&sql, &rc, "%s%s", zSep, zDflt);
227783 zSep = ", ";
227784 }
227785 if( rc==SQLITE_OK0 ){
227786 rc = sqlite3_prepare_v2(db, (const char*)sql.aBuf, -1, ppStmt, 0);
227787 }
227788 sqlite3_free(sql.aBuf);
227789
227790 return rc;
227791}
227792
227793/*
227794** Table pTab has one or more existing change-records with old.* records
227795** with fewer than pTab->nCol columns. This function updates all such
227796** change-records with the default values for the missing columns.
227797*/
227798static int sessionUpdateChanges(sqlite3_session *pSession, SessionTable *pTab){
227799 sqlite3_stmt *pStmt = 0;
227800 int rc = pSession->rc;
227801
227802 rc = sessionPrepareDfltStmt(pSession->db, pTab, &pStmt);
227803 if( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
227804 int ii = 0;
227805 SessionChange **pp = 0;
227806 for(ii=0; ii<pTab->nChange; ii++){
227807 for(pp=&pTab->apChange[ii]; *pp; pp=&((*pp)->pNext)){
227808 if( (*pp)->nRecordField!=pTab->nCol ){
227809 sessionUpdateOneChange(pSession, &rc, pp, pTab->nCol, pStmt);
227810 }
227811 }
227812 }
227813 }
227814
227815 pSession->rc = rc;
227816 rc = sqlite3_finalize(pStmt);
227817 if( pSession->rc==SQLITE_OK0 ) pSession->rc = rc;
227818 return pSession->rc;
227819}
227820
227821/*
227822** Versions of the four methods in object SessionHook for use with the
227823** sqlite_stat1 table. The purpose of this is to substitute a zero-length
227824** blob each time a NULL value is read from the "idx" column of the
227825** sqlite_stat1 table.
227826*/
227827typedef struct SessionStat1Ctx SessionStat1Ctx;
227828struct SessionStat1Ctx {
227829 SessionHook hook;
227830 sqlite3_session *pSession;
227831};
227832static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
227833 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
227834 sqlite3_value *pVal = 0;
227835 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
227836 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
227837 pVal = p->pSession->pZeroBlob;
227838 }
227839 *ppVal = pVal;
227840 return rc;
227841}
227842static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
227843 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
227844 sqlite3_value *pVal = 0;
227845 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
227846 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
227847 pVal = p->pSession->pZeroBlob;
227848 }
227849 *ppVal = pVal;
227850 return rc;
227851}
227852static int sessionStat1Count(void *pCtx){
227853 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
227854 return p->hook.xCount(p->hook.pCtx);
227855}
227856static int sessionStat1Depth(void *pCtx){
227857 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
227858 return p->hook.xDepth(p->hook.pCtx);
227859}
227860
227861static int sessionUpdateMaxSize(
227862 int op,
227863 sqlite3_session *pSession, /* Session object pTab is attached to */
227864 SessionTable *pTab, /* Table that change applies to */
227865 SessionChange *pC /* Update pC->nMaxSize */
227866){
227867 i64 nNew = 2;
227868 if( pC->op==SQLITE_INSERT18 ){
227869 if( pTab->bRowid ) nNew += 9;
227870 if( op!=SQLITE_DELETE9 ){
227871 int ii;
227872 for(ii=0; ii<pTab->nCol; ii++){
227873 sqlite3_value *p = 0;
227874 pSession->hook.xNew(pSession->hook.pCtx, ii, &p);
227875 sessionSerializeValue(0, p, &nNew);
227876 }
227877 }
227878 }else if( op==SQLITE_DELETE9 ){
227879 nNew += pC->nRecord;
227880 if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){
227881 nNew += pC->nRecord;
227882 }
227883 }else{
227884 int ii;
227885 u8 *pCsr = pC->aRecord;
227886 if( pTab->bRowid ){
227887 nNew += 9 + 1;
227888 pCsr += 9;
227889 }
227890 for(ii=pTab->bRowid; ii<pTab->nCol; ii++){
227891 int bChanged = 1;
227892 int nOld = 0;
227893 int eType;
227894 sqlite3_value *p = 0;
227895 pSession->hook.xNew(pSession->hook.pCtx, ii-pTab->bRowid, &p);
227896 if( p==0 ){
227897 return SQLITE_NOMEM7;
227898 }
227899
227900 eType = *pCsr++;
227901 switch( eType ){
227902 case SQLITE_NULL5:
227903 bChanged = sqlite3_value_type(p)!=SQLITE_NULL5;
227904 break;
227905
227906 case SQLITE_FLOAT2:
227907 case SQLITE_INTEGER1: {
227908 if( eType==sqlite3_value_type(p) ){
227909 sqlite3_int64 iVal = sessionGetI64(pCsr);
227910 if( eType==SQLITE_INTEGER1 ){
227911 bChanged = (iVal!=sqlite3_value_int64(p));
227912 }else{
227913 double dVal;
227914 memcpy(&dVal, &iVal, 8);
227915 bChanged = (dVal!=sqlite3_value_double(p));
227916 }
227917 }
227918 nOld = 8;
227919 pCsr += 8;
227920 break;
227921 }
227922
227923 default: {
227924 int nByte;
227925 nOld = sessionVarintGet(pCsr, &nByte);
227926 pCsr += nOld;
227927 nOld += nByte;
227928 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
227929 if( eType==sqlite3_value_type(p)
227930 && nByte==sqlite3_value_bytes(p)
227931 && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte))
227932 ){
227933 bChanged = 0;
227934 }
227935 pCsr += nByte;
227936 break;
227937 }
227938 }
227939
227940 if( bChanged && pTab->abPK[ii] ){
227941 nNew = pC->nRecord + 2;
227942 break;
227943 }
227944
227945 if( bChanged ){
227946 nNew += 1 + nOld;
227947 sessionSerializeValue(0, p, &nNew);
227948 }else if( pTab->abPK[ii] ){
227949 nNew += 2 + nOld;
227950 }else{
227951 nNew += 2;
227952 }
227953 }
227954 }
227955
227956 if( nNew>pC->nMaxSize ){
227957 int nIncr = nNew - pC->nMaxSize;
227958 pC->nMaxSize = nNew;
227959 pSession->nMaxChangesetSize += nIncr;
227960 }
227961 return SQLITE_OK0;
227962}
227963
227964/*
227965** This function is only called from with a pre-update-hook reporting a
227966** change on table pTab (attached to session pSession). The type of change
227967** (UPDATE, INSERT, DELETE) is specified by the first argument.
227968**
227969** Unless one is already present or an error occurs, an entry is added
227970** to the changed-rows hash table associated with table pTab.
227971*/
227972static void sessionPreupdateOneChange(
227973 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
227974 i64 iRowid,
227975 sqlite3_session *pSession, /* Session object pTab is attached to */
227976 SessionTable *pTab /* Table that change applies to */
227977){
227978 int iHash;
227979 int bNull = 0;
227980 int rc = SQLITE_OK0;
227981 int nExpect = 0;
227982 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
227983
227984 if( pSession->rc ) return;
227985
227986 /* Load table details if required */
227987 if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return;
227988
227989 /* Check the number of columns in this xPreUpdate call matches the
227990 ** number of columns in the table. */
227991 nExpect = pSession->hook.xCount(pSession->hook.pCtx);
227992 if( (pTab->nCol-pTab->bRowid)<nExpect ){
227993 if( sessionReinitTable(pSession, pTab) ) return;
227994 if( sessionUpdateChanges(pSession, pTab) ) return;
227995 }
227996 if( (pTab->nCol-pTab->bRowid)!=nExpect ){
227997 pSession->rc = SQLITE_SCHEMA17;
227998 return;
227999 }
228000
228001 /* Grow the hash table if required */
228002 if( sessionGrowHash(pSession, 0, pTab) ){
228003 pSession->rc = SQLITE_NOMEM7;
228004 return;
228005 }
228006
228007 if( pTab->bStat1 ){
228008 stat1.hook = pSession->hook;
228009 stat1.pSession = pSession;
228010 pSession->hook.pCtx = (void*)&stat1;
228011 pSession->hook.xNew = sessionStat1New;
228012 pSession->hook.xOld = sessionStat1Old;
228013 pSession->hook.xCount = sessionStat1Count;
228014 pSession->hook.xDepth = sessionStat1Depth;
228015 if( pSession->pZeroBlob==0 ){
228016 sqlite3_value *p = sqlite3ValueNew(0);
228017 if( p==0 ){
228018 rc = SQLITE_NOMEM7;
228019 goto error_out;
228020 }
228021 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
228022 pSession->pZeroBlob = p;
228023 }
228024 }
228025
228026 /* Calculate the hash-key for this change. If the primary key of the row
228027 ** includes a NULL value, exit early. Such changes are ignored by the
228028 ** session module. */
228029 rc = sessionPreupdateHash(
228030 pSession, iRowid, pTab, op==SQLITE_INSERT18, &iHash, &bNull
228031 );
228032 if( rc!=SQLITE_OK0 ) goto error_out;
228033
228034 if( bNull==0 ){
228035 /* Search the hash table for an existing record for this row. */
228036 SessionChange *pC;
228037 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
228038 if( sessionPreupdateEqual(pSession, iRowid, pTab, pC, op) ) break;
228039 }
228040
228041 if( pC==0 ){
228042 /* Create a new change object containing all the old values (if
228043 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
228044 ** values (if this is an INSERT). */
228045 sqlite3_int64 nByte; /* Number of bytes to allocate */
228046 int i; /* Used to iterate through columns */
228047
228048 assert( rc==SQLITE_OK )((void) (0));
228049 pTab->nEntry++;
228050
228051 /* Figure out how large an allocation is required */
228052 nByte = sizeof(SessionChange);
228053 for(i=0; i<(pTab->nCol-pTab->bRowid); i++){
228054 sqlite3_value *p = 0;
228055 if( op!=SQLITE_INSERT18 ){
228056 /* This may fail if the column has a non-NULL default and was added
228057 ** using ALTER TABLE ADD COLUMN after this record was created. */
228058 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &p);
228059 }else if( pTab->abPK[i] ){
228060 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
228061 assert( trc==SQLITE_OK )((void) (0));
228062 }
228063
228064 if( rc==SQLITE_OK0 ){
228065 /* This may fail if SQLite value p contains a utf-16 string that must
228066 ** be converted to utf-8 and an OOM error occurs while doing so. */
228067 rc = sessionSerializeValue(0, p, &nByte);
228068 }
228069 if( rc!=SQLITE_OK0 ) goto error_out;
228070 }
228071 if( pTab->bRowid ){
228072 nByte += 9; /* Size of rowid field - an integer */
228073 }
228074
228075 /* Allocate the change object */
228076 pC = (SessionChange*)sessionMalloc64(pSession, nByte);
228077 if( !pC ){
228078 rc = SQLITE_NOMEM7;
228079 goto error_out;
228080 }else{
228081 memset(pC, 0, sizeof(SessionChange));
228082 pC->aRecord = (u8 *)&pC[1];
228083 }
228084
228085 /* Populate the change object. None of the preupdate_old(),
228086 ** preupdate_new() or SerializeValue() calls below may fail as all
228087 ** required values and encodings have already been cached in memory.
228088 ** It is not possible for an OOM to occur in this block. */
228089 nByte = 0;
228090 if( pTab->bRowid ){
228091 pC->aRecord[0] = SQLITE_INTEGER1;
228092 sessionPutI64(&pC->aRecord[1], iRowid);
228093 nByte = 9;
228094 }
228095 for(i=0; i<(pTab->nCol-pTab->bRowid); i++){
228096 sqlite3_value *p = 0;
228097 if( op!=SQLITE_INSERT18 ){
228098 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
228099 }else if( pTab->abPK[i] ){
228100 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
228101 }
228102 sessionSerializeValue(&pC->aRecord[nByte], p, &nByte);
228103 }
228104
228105 /* Add the change to the hash-table */
228106 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
228107 pC->bIndirect = 1;
228108 }
228109 pC->nRecordField = pTab->nCol;
228110 pC->nRecord = nByte;
228111 pC->op = op;
228112 pC->pNext = pTab->apChange[iHash];
228113 pTab->apChange[iHash] = pC;
228114
228115 }else if( pC->bIndirect ){
228116 /* If the existing change is considered "indirect", but this current
228117 ** change is "direct", mark the change object as direct. */
228118 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
228119 && pSession->bIndirect==0
228120 ){
228121 pC->bIndirect = 0;
228122 }
228123 }
228124
228125 assert( rc==SQLITE_OK )((void) (0));
228126 if( pSession->bEnableSize ){
228127 rc = sessionUpdateMaxSize(op, pSession, pTab, pC);
228128 }
228129 }
228130
228131
228132 /* If an error has occurred, mark the session object as failed. */
228133 error_out:
228134 if( pTab->bStat1 ){
228135 pSession->hook = stat1.hook;
228136 }
228137 if( rc!=SQLITE_OK0 ){
228138 pSession->rc = rc;
228139 }
228140}
228141
228142static int sessionFindTable(
228143 sqlite3_session *pSession,
228144 const char *zName,
228145 SessionTable **ppTab
228146){
228147 int rc = SQLITE_OK0;
228148 int nName = sqlite3Strlen30(zName);
228149 SessionTable *pRet;
228150
228151 /* Search for an existing table */
228152 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
228153 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
228154 }
228155
228156 if( pRet==0 && pSession->bAutoAttach ){
228157 /* If there is a table-filter configured, invoke it. If it returns 0,
228158 ** do not automatically add the new table. */
228159 if( pSession->xTableFilter==0
228160 || pSession->xTableFilter(pSession->pFilterCtx, zName)
228161 ){
228162 rc = sqlite3session_attach(pSession, zName);
228163 if( rc==SQLITE_OK0 ){
228164 pRet = pSession->pTable;
228165 while( ALWAYS(pRet)(pRet) && pRet->pNext ){
228166 pRet = pRet->pNext;
228167 }
228168 assert( pRet!=0 )((void) (0));
228169 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) )((void) (0));
228170 }
228171 }
228172 }
228173
228174 assert( rc==SQLITE_OK || pRet==0 )((void) (0));
228175 *ppTab = pRet;
228176 return rc;
228177}
228178
228179/*
228180** The 'pre-update' hook registered by this module with SQLite databases.
228181*/
228182static void xPreUpdate(
228183 void *pCtx, /* Copy of third arg to preupdate_hook() */
228184 sqlite3 *db, /* Database handle */
228185 int op, /* SQLITE_UPDATE, DELETE or INSERT */
228186 char const *zDb, /* Database name */
228187 char const *zName, /* Table name */
228188 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
228189 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
228190){
228191 sqlite3_session *pSession;
228192 int nDb = sqlite3Strlen30(zDb);
228193
228194 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
228195 (void)iKey1;
228196 (void)iKey2;
228197
228198 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
228199 SessionTable *pTab;
228200
228201 /* If this session is attached to a different database ("main", "temp"
228202 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
228203 ** to the next session object attached to this database. */
228204 if( pSession->bEnable==0 ) continue;
228205 if( pSession->rc ) continue;
228206 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
228207
228208 pSession->rc = sessionFindTable(pSession, zName, &pTab);
228209 if( pTab ){
228210 assert( pSession->rc==SQLITE_OK )((void) (0));
228211 assert( op==SQLITE_UPDATE || iKey1==iKey2 )((void) (0));
228212 sessionPreupdateOneChange(op, iKey1, pSession, pTab);
228213 if( op==SQLITE_UPDATE23 ){
228214 sessionPreupdateOneChange(SQLITE_INSERT18, iKey2, pSession, pTab);
228215 }
228216 }
228217 }
228218}
228219
228220/*
228221** The pre-update hook implementations.
228222*/
228223static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
228224 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
228225}
228226static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
228227 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
228228}
228229static int sessionPreupdateCount(void *pCtx){
228230 return sqlite3_preupdate_count((sqlite3*)pCtx);
228231}
228232static int sessionPreupdateDepth(void *pCtx){
228233 return sqlite3_preupdate_depth((sqlite3*)pCtx);
228234}
228235
228236/*
228237** Install the pre-update hooks on the session object passed as the only
228238** argument.
228239*/
228240static void sessionPreupdateHooks(
228241 sqlite3_session *pSession
228242){
228243 pSession->hook.pCtx = (void*)pSession->db;
228244 pSession->hook.xOld = sessionPreupdateOld;
228245 pSession->hook.xNew = sessionPreupdateNew;
228246 pSession->hook.xCount = sessionPreupdateCount;
228247 pSession->hook.xDepth = sessionPreupdateDepth;
228248}
228249
228250typedef struct SessionDiffCtx SessionDiffCtx;
228251struct SessionDiffCtx {
228252 sqlite3_stmt *pStmt;
228253 int bRowid;
228254 int nOldOff;
228255};
228256
228257/*
228258** The diff hook implementations.
228259*/
228260static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
228261 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
228262 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff+p->bRowid);
228263 return SQLITE_OK0;
228264}
228265static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
228266 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
228267 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->bRowid);
228268 return SQLITE_OK0;
228269}
228270static int sessionDiffCount(void *pCtx){
228271 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
228272 return (p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt)) - p->bRowid;
228273}
228274static int sessionDiffDepth(void *pCtx){
228275 (void)pCtx;
228276 return 0;
228277}
228278
228279/*
228280** Install the diff hooks on the session object passed as the only
228281** argument.
228282*/
228283static void sessionDiffHooks(
228284 sqlite3_session *pSession,
228285 SessionDiffCtx *pDiffCtx
228286){
228287 pSession->hook.pCtx = (void*)pDiffCtx;
228288 pSession->hook.xOld = sessionDiffOld;
228289 pSession->hook.xNew = sessionDiffNew;
228290 pSession->hook.xCount = sessionDiffCount;
228291 pSession->hook.xDepth = sessionDiffDepth;
228292}
228293
228294static char *sessionExprComparePK(
228295 int nCol,
228296 const char *zDb1, const char *zDb2,
228297 const char *zTab,
228298 const char **azCol, u8 *abPK
228299){
228300 int i;
228301 const char *zSep = "";
228302 char *zRet = 0;
228303
228304 for(i=0; i<nCol; i++){
228305 if( abPK[i] ){
228306 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
228307 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
228308 );
228309 zSep = " AND ";
228310 if( zRet==0 ) break;
228311 }
228312 }
228313
228314 return zRet;
228315}
228316
228317static char *sessionExprCompareOther(
228318 int nCol,
228319 const char *zDb1, const char *zDb2,
228320 const char *zTab,
228321 const char **azCol, u8 *abPK
228322){
228323 int i;
228324 const char *zSep = "";
228325 char *zRet = 0;
228326 int bHave = 0;
228327
228328 for(i=0; i<nCol; i++){
228329 if( abPK[i]==0 ){
228330 bHave = 1;
228331 zRet = sqlite3_mprintf(
228332 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
228333 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
228334 );
228335 zSep = " OR ";
228336 if( zRet==0 ) break;
228337 }
228338 }
228339
228340 if( bHave==0 ){
228341 assert( zRet==0 )((void) (0));
228342 zRet = sqlite3_mprintf("0");
228343 }
228344
228345 return zRet;
228346}
228347
228348static char *sessionSelectFindNew(
228349 const char *zDb1, /* Pick rows in this db only */
228350 const char *zDb2, /* But not in this one */
228351 int bRowid,
228352 const char *zTbl, /* Table name */
228353 const char *zExpr
228354){
228355 const char *zSel = (bRowid ? SESSIONS_ROWID"_rowid_" ", *" : "*");
228356 char *zRet = sqlite3_mprintf(
228357 "SELECT %s FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
228358 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
228359 ")",
228360 zSel, zDb1, zTbl, zDb2, zTbl, zExpr
228361 );
228362 return zRet;
228363}
228364
228365static int sessionDiffFindNew(
228366 int op,
228367 sqlite3_session *pSession,
228368 SessionTable *pTab,
228369 const char *zDb1,
228370 const char *zDb2,
228371 char *zExpr
228372){
228373 int rc = SQLITE_OK0;
228374 char *zStmt = sessionSelectFindNew(
228375 zDb1, zDb2, pTab->bRowid, pTab->zName, zExpr
228376 );
228377
228378 if( zStmt==0 ){
228379 rc = SQLITE_NOMEM7;
228380 }else{
228381 sqlite3_stmt *pStmt;
228382 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
228383 if( rc==SQLITE_OK0 ){
228384 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
228385 pDiffCtx->pStmt = pStmt;
228386 pDiffCtx->nOldOff = 0;
228387 pDiffCtx->bRowid = pTab->bRowid;
228388 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
228389 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
228390 sessionPreupdateOneChange(op, iRowid, pSession, pTab);
228391 }
228392 rc = sqlite3_finalize(pStmt);
228393 }
228394 sqlite3_free(zStmt);
228395 }
228396
228397 return rc;
228398}
228399
228400/*
228401** Return a comma-separated list of the fully-qualified (with both database
228402** and table name) column names from table pTab. e.g.
228403**
228404** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c"
228405*/
228406static char *sessionAllCols(
228407 const char *zDb,
228408 SessionTable *pTab
228409){
228410 int ii;
228411 char *zRet = 0;
228412 for(ii=0; ii<pTab->nCol; ii++){
228413 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"",
228414 zRet, (zRet ? ", " : ""), zDb, pTab->zName, pTab->azCol[ii]
228415 );
228416 if( !zRet ) break;
228417 }
228418 return zRet;
228419}
228420
228421static int sessionDiffFindModified(
228422 sqlite3_session *pSession,
228423 SessionTable *pTab,
228424 const char *zFrom,
228425 const char *zExpr
228426){
228427 int rc = SQLITE_OK0;
228428
228429 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
228430 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
228431 );
228432 if( zExpr2==0 ){
228433 rc = SQLITE_NOMEM7;
228434 }else{
228435 char *z1 = sessionAllCols(pSession->zDb, pTab);
228436 char *z2 = sessionAllCols(zFrom, pTab);
228437 char *zStmt = sqlite3_mprintf(
228438 "SELECT %s,%s FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
228439 z1, z2, pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
228440 );
228441 if( zStmt==0 || z1==0 || z2==0 ){
228442 rc = SQLITE_NOMEM7;
228443 }else{
228444 sqlite3_stmt *pStmt;
228445 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
228446
228447 if( rc==SQLITE_OK0 ){
228448 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
228449 pDiffCtx->pStmt = pStmt;
228450 pDiffCtx->nOldOff = pTab->nCol;
228451 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
228452 i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0);
228453 sessionPreupdateOneChange(SQLITE_UPDATE23, iRowid, pSession, pTab);
228454 }
228455 rc = sqlite3_finalize(pStmt);
228456 }
228457 }
228458 sqlite3_free(zStmt);
228459 sqlite3_free(z1);
228460 sqlite3_free(z2);
228461 }
228462
228463 return rc;
228464}
228465
228466SQLITE_API int sqlite3session_diff(
228467 sqlite3_session *pSession,
228468 const char *zFrom,
228469 const char *zTbl,
228470 char **pzErrMsg
228471){
228472 const char *zDb = pSession->zDb;
228473 int rc = pSession->rc;
228474 SessionDiffCtx d;
228475
228476 memset(&d, 0, sizeof(d));
228477 sessionDiffHooks(pSession, &d);
228478
228479 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
228480 if( pzErrMsg ) *pzErrMsg = 0;
228481 if( rc==SQLITE_OK0 ){
228482 char *zExpr = 0;
228483 sqlite3 *db = pSession->db;
228484 SessionTable *pTo; /* Table zTbl */
228485
228486 /* Locate and if necessary initialize the target table object */
228487 rc = sessionFindTable(pSession, zTbl, &pTo);
228488 if( pTo==0 ) goto diff_out;
228489 if( sessionInitTable(pSession, pTo, pSession->db, pSession->zDb) ){
228490 rc = pSession->rc;
228491 goto diff_out;
228492 }
228493
228494 /* Check the table schemas match */
228495 if( rc==SQLITE_OK0 ){
228496 int bHasPk = 0;
228497 int bMismatch = 0;
228498 int nCol; /* Columns in zFrom.zTbl */
228499 int bRowid = 0;
228500 u8 *abPK;
228501 const char **azCol = 0;
228502 rc = sessionTableInfo(0, db, zFrom, zTbl, &nCol, 0, &azCol, 0, &abPK,
228503 pSession->bImplicitPK ? &bRowid : 0
228504 );
228505 if( rc==SQLITE_OK0 ){
228506 if( pTo->nCol!=nCol ){
228507 bMismatch = 1;
228508 }else{
228509 int i;
228510 for(i=0; i<nCol; i++){
228511 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
228512 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
228513 if( abPK[i] ) bHasPk = 1;
228514 }
228515 }
228516 }
228517 sqlite3_free((char*)azCol);
228518 if( bMismatch ){
228519 if( pzErrMsg ){
228520 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
228521 }
228522 rc = SQLITE_SCHEMA17;
228523 }
228524 if( bHasPk==0 ){
228525 /* Ignore tables with no primary keys */
228526 goto diff_out;
228527 }
228528 }
228529
228530 if( rc==SQLITE_OK0 ){
228531 zExpr = sessionExprComparePK(pTo->nCol,
228532 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
228533 );
228534 }
228535
228536 /* Find new rows */
228537 if( rc==SQLITE_OK0 ){
228538 rc = sessionDiffFindNew(SQLITE_INSERT18, pSession, pTo, zDb, zFrom, zExpr);
228539 }
228540
228541 /* Find old rows */
228542 if( rc==SQLITE_OK0 ){
228543 rc = sessionDiffFindNew(SQLITE_DELETE9, pSession, pTo, zFrom, zDb, zExpr);
228544 }
228545
228546 /* Find modified rows */
228547 if( rc==SQLITE_OK0 ){
228548 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
228549 }
228550
228551 sqlite3_free(zExpr);
228552 }
228553
228554 diff_out:
228555 sessionPreupdateHooks(pSession);
228556 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
228557 return rc;
228558}
228559
228560/*
228561** Create a session object. This session object will record changes to
228562** database zDb attached to connection db.
228563*/
228564SQLITE_API int sqlite3session_create(
228565 sqlite3 *db, /* Database handle */
228566 const char *zDb, /* Name of db (e.g. "main") */
228567 sqlite3_session **ppSession /* OUT: New session object */
228568){
228569 sqlite3_session *pNew; /* Newly allocated session object */
228570 sqlite3_session *pOld; /* Session object already attached to db */
228571 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
228572
228573 /* Zero the output value in case an error occurs. */
228574 *ppSession = 0;
228575
228576 /* Allocate and populate the new session object. */
228577 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
228578 if( !pNew ) return SQLITE_NOMEM7;
228579 memset(pNew, 0, sizeof(sqlite3_session));
228580 pNew->db = db;
228581 pNew->zDb = (char *)&pNew[1];
228582 pNew->bEnable = 1;
228583 memcpy(pNew->zDb, zDb, nDb+1);
228584 sessionPreupdateHooks(pNew);
228585
228586 /* Add the new session object to the linked list of session objects
228587 ** attached to database handle $db. Do this under the cover of the db
228588 ** handle mutex. */
228589 sqlite3_mutex_enter(sqlite3_db_mutex(db));
228590 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
228591 pNew->pNext = pOld;
228592 sqlite3_mutex_leave(sqlite3_db_mutex(db));
228593
228594 *ppSession = pNew;
228595 return SQLITE_OK0;
228596}
228597
228598/*
228599** Free the list of table objects passed as the first argument. The contents
228600** of the changed-rows hash tables are also deleted.
228601*/
228602static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){
228603 SessionTable *pNext;
228604 SessionTable *pTab;
228605
228606 for(pTab=pList; pTab; pTab=pNext){
228607 int i;
228608 pNext = pTab->pNext;
228609 for(i=0; i<pTab->nChange; i++){
228610 SessionChange *p;
228611 SessionChange *pNextChange;
228612 for(p=pTab->apChange[i]; p; p=pNextChange){
228613 pNextChange = p->pNext;
228614 sessionFree(pSession, p);
228615 }
228616 }
228617 sqlite3_finalize(pTab->pDfltStmt);
228618 sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */
228619 sessionFree(pSession, pTab->apChange);
228620 sessionFree(pSession, pTab);
228621 }
228622}
228623
228624/*
228625** Delete a session object previously allocated using sqlite3session_create().
228626*/
228627SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
228628 sqlite3 *db = pSession->db;
228629 sqlite3_session *pHead;
228630 sqlite3_session **pp;
228631
228632 /* Unlink the session from the linked list of sessions attached to the
228633 ** database handle. Hold the db mutex while doing so. */
228634 sqlite3_mutex_enter(sqlite3_db_mutex(db));
228635 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
228636 for(pp=&pHead; ALWAYS((*pp)!=0)((*pp)!=0); pp=&((*pp)->pNext)){
228637 if( (*pp)==pSession ){
228638 *pp = (*pp)->pNext;
228639 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
228640 break;
228641 }
228642 }
228643 sqlite3_mutex_leave(sqlite3_db_mutex(db));
228644 sqlite3ValueFree(pSession->pZeroBlob);
228645
228646 /* Delete all attached table objects. And the contents of their
228647 ** associated hash-tables. */
228648 sessionDeleteTable(pSession, pSession->pTable);
228649
228650 /* Free the session object. */
228651 sqlite3_free(pSession);
228652}
228653
228654/*
228655** Set a table filter on a Session Object.
228656*/
228657SQLITE_API void sqlite3session_table_filter(
228658 sqlite3_session *pSession,
228659 int(*xFilter)(void*, const char*),
228660 void *pCtx /* First argument passed to xFilter */
228661){
228662 pSession->bAutoAttach = 1;
228663 pSession->pFilterCtx = pCtx;
228664 pSession->xTableFilter = xFilter;
228665}
228666
228667/*
228668** Attach a table to a session. All subsequent changes made to the table
228669** while the session object is enabled will be recorded.
228670**
228671** Only tables that have a PRIMARY KEY defined may be attached. It does
228672** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
228673** or not.
228674*/
228675SQLITE_API int sqlite3session_attach(
228676 sqlite3_session *pSession, /* Session object */
228677 const char *zName /* Table name */
228678){
228679 int rc = SQLITE_OK0;
228680 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
228681
228682 if( !zName ){
228683 pSession->bAutoAttach = 1;
228684 }else{
228685 SessionTable *pTab; /* New table object (if required) */
228686 int nName; /* Number of bytes in string zName */
228687
228688 /* First search for an existing entry. If one is found, this call is
228689 ** a no-op. Return early. */
228690 nName = sqlite3Strlen30(zName);
228691 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
228692 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
228693 }
228694
228695 if( !pTab ){
228696 /* Allocate new SessionTable object. */
228697 int nByte = sizeof(SessionTable) + nName + 1;
228698 pTab = (SessionTable*)sessionMalloc64(pSession, nByte);
228699 if( !pTab ){
228700 rc = SQLITE_NOMEM7;
228701 }else{
228702 /* Populate the new SessionTable object and link it into the list.
228703 ** The new object must be linked onto the end of the list, not
228704 ** simply added to the start of it in order to ensure that tables
228705 ** appear in the correct order when a changeset or patchset is
228706 ** eventually generated. */
228707 SessionTable **ppTab;
228708 memset(pTab, 0, sizeof(SessionTable));
228709 pTab->zName = (char *)&pTab[1];
228710 memcpy(pTab->zName, zName, nName+1);
228711 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
228712 *ppTab = pTab;
228713 }
228714 }
228715 }
228716
228717 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
228718 return rc;
228719}
228720
228721/*
228722** Append the value passed as the second argument to the buffer passed
228723** as the first.
228724**
228725** This function is a no-op if *pRc is non-zero when it is called.
228726** Otherwise, if an error occurs, *pRc is set to an SQLite error code
228727** before returning.
228728*/
228729static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
228730 int rc = *pRc;
228731 if( rc==SQLITE_OK0 ){
228732 sqlite3_int64 nByte = 0;
228733 rc = sessionSerializeValue(0, pVal, &nByte);
228734 sessionBufferGrow(p, nByte, &rc);
228735 if( rc==SQLITE_OK0 ){
228736 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
228737 p->nBuf += nByte;
228738 }else{
228739 *pRc = rc;
228740 }
228741 }
228742}
228743
228744/*
228745** This function is a no-op if *pRc is other than SQLITE_OK when it is
228746** called. Otherwise, append a single byte to the buffer.
228747**
228748** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
228749** returning.
228750*/
228751static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
228752 if( 0==sessionBufferGrow(p, 1, pRc) ){
228753 p->aBuf[p->nBuf++] = v;
228754 }
228755}
228756
228757/*
228758** This function is a no-op if *pRc is other than SQLITE_OK when it is
228759** called. Otherwise, append a single varint to the buffer.
228760**
228761** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
228762** returning.
228763*/
228764static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
228765 if( 0==sessionBufferGrow(p, 9, pRc) ){
228766 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
228767 }
228768}
228769
228770/*
228771** This function is a no-op if *pRc is other than SQLITE_OK when it is
228772** called. Otherwise, append a blob of data to the buffer.
228773**
228774** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
228775** returning.
228776*/
228777static void sessionAppendBlob(
228778 SessionBuffer *p,
228779 const u8 *aBlob,
228780 int nBlob,
228781 int *pRc
228782){
228783 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
228784 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
228785 p->nBuf += nBlob;
228786 }
228787}
228788
228789/*
228790** This function is a no-op if *pRc is other than SQLITE_OK when it is
228791** called. Otherwise, append the string representation of integer iVal
228792** to the buffer. No nul-terminator is written.
228793**
228794** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
228795** returning.
228796*/
228797static void sessionAppendInteger(
228798 SessionBuffer *p, /* Buffer to append to */
228799 int iVal, /* Value to write the string rep. of */
228800 int *pRc /* IN/OUT: Error code */
228801){
228802 char aBuf[24];
228803 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
228804 sessionAppendStr(p, aBuf, pRc);
228805}
228806
228807/*
228808** This function is a no-op if *pRc is other than SQLITE_OK when it is
228809** called. Otherwise, append the string zStr enclosed in quotes (") and
228810** with any embedded quote characters escaped to the buffer. No
228811** nul-terminator byte is written.
228812**
228813** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
228814** returning.
228815*/
228816static void sessionAppendIdent(
228817 SessionBuffer *p, /* Buffer to a append to */
228818 const char *zStr, /* String to quote, escape and append */
228819 int *pRc /* IN/OUT: Error code */
228820){
228821 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 2;
228822 if( 0==sessionBufferGrow(p, nStr, pRc) ){
228823 char *zOut = (char *)&p->aBuf[p->nBuf];
228824 const char *zIn = zStr;
228825 *zOut++ = '"';
228826 while( *zIn ){
228827 if( *zIn=='"' ) *zOut++ = '"';
228828 *zOut++ = *(zIn++);
228829 }
228830 *zOut++ = '"';
228831 p->nBuf = (int)((u8 *)zOut - p->aBuf);
228832 p->aBuf[p->nBuf] = 0x00;
228833 }
228834}
228835
228836/*
228837** This function is a no-op if *pRc is other than SQLITE_OK when it is
228838** called. Otherwse, it appends the serialized version of the value stored
228839** in column iCol of the row that SQL statement pStmt currently points
228840** to to the buffer.
228841*/
228842static void sessionAppendCol(
228843 SessionBuffer *p, /* Buffer to append to */
228844 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
228845 int iCol, /* Column to read value from */
228846 int *pRc /* IN/OUT: Error code */
228847){
228848 if( *pRc==SQLITE_OK0 ){
228849 int eType = sqlite3_column_type(pStmt, iCol);
228850 sessionAppendByte(p, (u8)eType, pRc);
228851 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
228852 sqlite3_int64 i;
228853 u8 aBuf[8];
228854 if( eType==SQLITE_INTEGER1 ){
228855 i = sqlite3_column_int64(pStmt, iCol);
228856 }else{
228857 double r = sqlite3_column_double(pStmt, iCol);
228858 memcpy(&i, &r, 8);
228859 }
228860 sessionPutI64(aBuf, i);
228861 sessionAppendBlob(p, aBuf, 8, pRc);
228862 }
228863 if( eType==SQLITE_BLOB4 || eType==SQLITE_TEXT3 ){
228864 u8 *z;
228865 int nByte;
228866 if( eType==SQLITE_BLOB4 ){
228867 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
228868 }else{
228869 z = (u8 *)sqlite3_column_text(pStmt, iCol);
228870 }
228871 nByte = sqlite3_column_bytes(pStmt, iCol);
228872 if( z || (eType==SQLITE_BLOB4 && nByte==0) ){
228873 sessionAppendVarint(p, nByte, pRc);
228874 sessionAppendBlob(p, z, nByte, pRc);
228875 }else{
228876 *pRc = SQLITE_NOMEM7;
228877 }
228878 }
228879 }
228880}
228881
228882/*
228883**
228884** This function appends an update change to the buffer (see the comments
228885** under "CHANGESET FORMAT" at the top of the file). An update change
228886** consists of:
228887**
228888** 1 byte: SQLITE_UPDATE (0x17)
228889** n bytes: old.* record (see RECORD FORMAT)
228890** m bytes: new.* record (see RECORD FORMAT)
228891**
228892** The SessionChange object passed as the third argument contains the
228893** values that were stored in the row when the session began (the old.*
228894** values). The statement handle passed as the second argument points
228895** at the current version of the row (the new.* values).
228896**
228897** If all of the old.* values are equal to their corresponding new.* value
228898** (i.e. nothing has changed), then no data at all is appended to the buffer.
228899**
228900** Otherwise, the old.* record contains all primary key values and the
228901** original values of any fields that have been modified. The new.* record
228902** contains the new values of only those fields that have been modified.
228903*/
228904static int sessionAppendUpdate(
228905 SessionBuffer *pBuf, /* Buffer to append to */
228906 int bPatchset, /* True for "patchset", 0 for "changeset" */
228907 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
228908 SessionChange *p, /* Object containing old values */
228909 u8 *abPK /* Boolean array - true for PK columns */
228910){
228911 int rc = SQLITE_OK0;
228912 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
228913 int bNoop = 1; /* Set to zero if any values are modified */
228914 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
228915 int i; /* Used to iterate through columns */
228916 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
228917
228918 assert( abPK!=0 )((void) (0));
228919 sessionAppendByte(pBuf, SQLITE_UPDATE23, &rc);
228920 sessionAppendByte(pBuf, p->bIndirect, &rc);
228921 for(i=0; i<sqlite3_column_count(pStmt); i++){
228922 int bChanged = 0;
228923 int nAdvance;
228924 int eType = *pCsr;
228925 switch( eType ){
228926 case SQLITE_NULL5:
228927 nAdvance = 1;
228928 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
228929 bChanged = 1;
228930 }
228931 break;
228932
228933 case SQLITE_FLOAT2:
228934 case SQLITE_INTEGER1: {
228935 nAdvance = 9;
228936 if( eType==sqlite3_column_type(pStmt, i) ){
228937 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
228938 if( eType==SQLITE_INTEGER1 ){
228939 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
228940 }else{
228941 double dVal;
228942 memcpy(&dVal, &iVal, 8);
228943 if( dVal==sqlite3_column_double(pStmt, i) ) break;
228944 }
228945 }
228946 bChanged = 1;
228947 break;
228948 }
228949
228950 default: {
228951 int n;
228952 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
228953 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
228954 nAdvance = nHdr + n;
228955 if( eType==sqlite3_column_type(pStmt, i)
228956 && n==sqlite3_column_bytes(pStmt, i)
228957 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
228958 ){
228959 break;
228960 }
228961 bChanged = 1;
228962 }
228963 }
228964
228965 /* If at least one field has been modified, this is not a no-op. */
228966 if( bChanged ) bNoop = 0;
228967
228968 /* Add a field to the old.* record. This is omitted if this module is
228969 ** currently generating a patchset. */
228970 if( bPatchset==0 ){
228971 if( bChanged || abPK[i] ){
228972 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
228973 }else{
228974 sessionAppendByte(pBuf, 0, &rc);
228975 }
228976 }
228977
228978 /* Add a field to the new.* record. Or the only record if currently
228979 ** generating a patchset. */
228980 if( bChanged || (bPatchset && abPK[i]) ){
228981 sessionAppendCol(&buf2, pStmt, i, &rc);
228982 }else{
228983 sessionAppendByte(&buf2, 0, &rc);
228984 }
228985
228986 pCsr += nAdvance;
228987 }
228988
228989 if( bNoop ){
228990 pBuf->nBuf = nRewind;
228991 }else{
228992 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
228993 }
228994 sqlite3_free(buf2.aBuf);
228995
228996 return rc;
228997}
228998
228999/*
229000** Append a DELETE change to the buffer passed as the first argument. Use
229001** the changeset format if argument bPatchset is zero, or the patchset
229002** format otherwise.
229003*/
229004static int sessionAppendDelete(
229005 SessionBuffer *pBuf, /* Buffer to append to */
229006 int bPatchset, /* True for "patchset", 0 for "changeset" */
229007 SessionChange *p, /* Object containing old values */
229008 int nCol, /* Number of columns in table */
229009 u8 *abPK /* Boolean array - true for PK columns */
229010){
229011 int rc = SQLITE_OK0;
229012
229013 sessionAppendByte(pBuf, SQLITE_DELETE9, &rc);
229014 sessionAppendByte(pBuf, p->bIndirect, &rc);
229015
229016 if( bPatchset==0 ){
229017 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
229018 }else{
229019 int i;
229020 u8 *a = p->aRecord;
229021 for(i=0; i<nCol; i++){
229022 u8 *pStart = a;
229023 int eType = *a++;
229024
229025 switch( eType ){
229026 case 0:
229027 case SQLITE_NULL5:
229028 assert( abPK[i]==0 )((void) (0));
229029 break;
229030
229031 case SQLITE_FLOAT2:
229032 case SQLITE_INTEGER1:
229033 a += 8;
229034 break;
229035
229036 default: {
229037 int n;
229038 a += sessionVarintGet(a, &n);
229039 a += n;
229040 break;
229041 }
229042 }
229043 if( abPK[i] ){
229044 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
229045 }
229046 }
229047 assert( (a - p->aRecord)==p->nRecord )((void) (0));
229048 }
229049
229050 return rc;
229051}
229052
229053/*
229054** Formulate and prepare a SELECT statement to retrieve a row from table
229055** zTab in database zDb based on its primary key. i.e.
229056**
229057** SELECT *, <noop-test> FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...)
229058**
229059** where <noop-test> is:
229060**
229061** 1 AND (?A OR ?1 IS <column>) AND ...
229062**
229063** for each non-pk <column>.
229064*/
229065static int sessionSelectStmt(
229066 sqlite3 *db, /* Database handle */
229067 int bIgnoreNoop,
229068 const char *zDb, /* Database name */
229069 const char *zTab, /* Table name */
229070 int bRowid,
229071 int nCol, /* Number of columns in table */
229072 const char **azCol, /* Names of table columns */
229073 u8 *abPK, /* PRIMARY KEY array */
229074 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
229075){
229076 int rc = SQLITE_OK0;
229077 char *zSql = 0;
229078 const char *zSep = "";
229079 const char *zCols = bRowid ? SESSIONS_ROWID"_rowid_" ", *" : "*";
229080 int nSql = -1;
229081 int i;
229082
229083 SessionBuffer nooptest = {0, 0, 0};
229084 SessionBuffer pkfield = {0, 0, 0};
229085 SessionBuffer pkvar = {0, 0, 0};
229086
229087 sessionAppendStr(&nooptest, ", 1", &rc);
229088
229089 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
229090 sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc);
229091 sessionAppendStr(&pkfield, "tbl, idx", &rc);
229092 sessionAppendStr(&pkvar,
229093 "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc
229094 );
229095 zCols = "tbl, ?2, stat";
229096 }else{
229097 for(i=0; i<nCol; i++){
229098 if( abPK[i] ){
229099 sessionAppendStr(&pkfield, zSep, &rc);
229100 sessionAppendStr(&pkvar, zSep, &rc);
229101 zSep = ", ";
229102 sessionAppendIdent(&pkfield, azCol[i], &rc);
229103 sessionAppendPrintf(&pkvar, &rc, "?%d", i+1);
229104 }else{
229105 sessionAppendPrintf(&nooptest, &rc,
229106 " AND (?%d OR ?%d IS %w.%w)", i+1+nCol, i+1, zTab, azCol[i]
229107 );
229108 }
229109 }
229110 }
229111
229112 if( rc==SQLITE_OK0 ){
229113 zSql = sqlite3_mprintf(
229114 "SELECT %s%s FROM %Q.%Q WHERE (%s) IS (%s)",
229115 zCols, (bIgnoreNoop ? (char*)nooptest.aBuf : ""),
229116 zDb, zTab, (char*)pkfield.aBuf, (char*)pkvar.aBuf
229117 );
229118 if( zSql==0 ) rc = SQLITE_NOMEM7;
229119 }
229120
229121#if 0
229122 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
229123 zSql = sqlite3_mprintf(
229124 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
229125 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
229126 );
229127 if( zSql==0 ) rc = SQLITE_NOMEM7;
229128 }else{
229129 const char *zSep = "";
229130 SessionBuffer buf = {0, 0, 0};
229131
229132 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
229133 sessionAppendIdent(&buf, zDb, &rc);
229134 sessionAppendStr(&buf, ".", &rc);
229135 sessionAppendIdent(&buf, zTab, &rc);
229136 sessionAppendStr(&buf, " WHERE ", &rc);
229137 for(i=0; i<nCol; i++){
229138 if( abPK[i] ){
229139 sessionAppendStr(&buf, zSep, &rc);
229140 sessionAppendIdent(&buf, azCol[i], &rc);
229141 sessionAppendStr(&buf, " IS ?", &rc);
229142 sessionAppendInteger(&buf, i+1, &rc);
229143 zSep = " AND ";
229144 }
229145 }
229146 zSql = (char*)buf.aBuf;
229147 nSql = buf.nBuf;
229148 }
229149#endif
229150
229151 if( rc==SQLITE_OK0 ){
229152 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
229153 }
229154 sqlite3_free(zSql);
229155 sqlite3_free(nooptest.aBuf);
229156 sqlite3_free(pkfield.aBuf);
229157 sqlite3_free(pkvar.aBuf);
229158 return rc;
229159}
229160
229161/*
229162** Bind the PRIMARY KEY values from the change passed in argument pChange
229163** to the SELECT statement passed as the first argument. The SELECT statement
229164** is as prepared by function sessionSelectStmt().
229165**
229166** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
229167** error code (e.g. SQLITE_NOMEM) otherwise.
229168*/
229169static int sessionSelectBind(
229170 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
229171 int nCol, /* Number of columns in table */
229172 u8 *abPK, /* PRIMARY KEY array */
229173 SessionChange *pChange /* Change structure */
229174){
229175 int i;
229176 int rc = SQLITE_OK0;
229177 u8 *a = pChange->aRecord;
229178
229179 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
229180 int eType = *a++;
229181
229182 switch( eType ){
229183 case 0:
229184 case SQLITE_NULL5:
229185 assert( abPK[i]==0 )((void) (0));
229186 break;
229187
229188 case SQLITE_INTEGER1: {
229189 if( abPK[i] ){
229190 i64 iVal = sessionGetI64(a);
229191 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
229192 }
229193 a += 8;
229194 break;
229195 }
229196
229197 case SQLITE_FLOAT2: {
229198 if( abPK[i] ){
229199 double rVal;
229200 i64 iVal = sessionGetI64(a);
229201 memcpy(&rVal, &iVal, 8);
229202 rc = sqlite3_bind_double(pSelect, i+1, rVal);
229203 }
229204 a += 8;
229205 break;
229206 }
229207
229208 case SQLITE_TEXT3: {
229209 int n;
229210 a += sessionVarintGet(a, &n);
229211 if( abPK[i] ){
229212 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
229213 }
229214 a += n;
229215 break;
229216 }
229217
229218 default: {
229219 int n;
229220 assert( eType==SQLITE_BLOB )((void) (0));
229221 a += sessionVarintGet(a, &n);
229222 if( abPK[i] ){
229223 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
229224 }
229225 a += n;
229226 break;
229227 }
229228 }
229229 }
229230
229231 return rc;
229232}
229233
229234/*
229235** This function is a no-op if *pRc is set to other than SQLITE_OK when it
229236** is called. Otherwise, append a serialized table header (part of the binary
229237** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
229238** SQLite error code before returning.
229239*/
229240static void sessionAppendTableHdr(
229241 SessionBuffer *pBuf, /* Append header to this buffer */
229242 int bPatchset, /* Use the patchset format if true */
229243 SessionTable *pTab, /* Table object to append header for */
229244 int *pRc /* IN/OUT: Error code */
229245){
229246 /* Write a table header */
229247 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
229248 sessionAppendVarint(pBuf, pTab->nCol, pRc);
229249 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
229250 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
229251}
229252
229253/*
229254** Generate either a changeset (if argument bPatchset is zero) or a patchset
229255** (if it is non-zero) based on the current contents of the session object
229256** passed as the first argument.
229257**
229258** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
229259** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
229260** occurs, an SQLite error code is returned and both output variables set
229261** to 0.
229262*/
229263static int sessionGenerateChangeset(
229264 sqlite3_session *pSession, /* Session object */
229265 int bPatchset, /* True for patchset, false for changeset */
229266 int (*xOutput)(void *pOut, const void *pData, int nData),
229267 void *pOut, /* First argument for xOutput */
229268 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
229269 void **ppChangeset /* OUT: Buffer containing changeset */
229270){
229271 sqlite3 *db = pSession->db; /* Source database handle */
229272 SessionTable *pTab; /* Used to iterate through attached tables */
229273 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
229274 int rc; /* Return code */
229275
229276 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) )((void) (0));
229277 assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) )((void) (0));
229278
229279 /* Zero the output variables in case an error occurs. If this session
229280 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
229281 ** this call will be a no-op. */
229282 if( xOutput==0 ){
229283 assert( pnChangeset!=0 && ppChangeset!=0 )((void) (0));
229284 *pnChangeset = 0;
229285 *ppChangeset = 0;
229286 }
229287
229288 if( pSession->rc ) return pSession->rc;
229289 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
229290 if( rc!=SQLITE_OK0 ) return rc;
229291
229292 sqlite3_mutex_enter(sqlite3_db_mutex(db));
229293
229294 for(pTab=pSession->pTable; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
229295 if( pTab->nEntry ){
229296 const char *zName = pTab->zName;
229297 int i; /* Used to iterate through hash buckets */
229298 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
229299 int nRewind = buf.nBuf; /* Initial size of write buffer */
229300 int nNoop; /* Size of buffer after writing tbl header */
229301 int nOldCol = pTab->nCol;
229302
229303 /* Check the table schema is still Ok. */
229304 rc = sessionReinitTable(pSession, pTab);
229305 if( rc==SQLITE_OK0 && pTab->nCol!=nOldCol ){
229306 rc = sessionUpdateChanges(pSession, pTab);
229307 }
229308
229309 /* Write a table header */
229310 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
229311
229312 /* Build and compile a statement to execute: */
229313 if( rc==SQLITE_OK0 ){
229314 rc = sessionSelectStmt(db, 0, pSession->zDb,
229315 zName, pTab->bRowid, pTab->nCol, pTab->azCol, pTab->abPK, &pSel
229316 );
229317 }
229318
229319 nNoop = buf.nBuf;
229320 for(i=0; i<pTab->nChange && rc==SQLITE_OK0; i++){
229321 SessionChange *p; /* Used to iterate through changes */
229322
229323 for(p=pTab->apChange[i]; rc==SQLITE_OK0 && p; p=p->pNext){
229324 rc = sessionSelectBind(pSel, pTab->nCol, pTab->abPK, p);
229325 if( rc!=SQLITE_OK0 ) continue;
229326 if( sqlite3_step(pSel)==SQLITE_ROW100 ){
229327 if( p->op==SQLITE_INSERT18 ){
229328 int iCol;
229329 sessionAppendByte(&buf, SQLITE_INSERT18, &rc);
229330 sessionAppendByte(&buf, p->bIndirect, &rc);
229331 for(iCol=0; iCol<pTab->nCol; iCol++){
229332 sessionAppendCol(&buf, pSel, iCol, &rc);
229333 }
229334 }else{
229335 assert( pTab->abPK!=0 )((void) (0));
229336 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, pTab->abPK);
229337 }
229338 }else if( p->op!=SQLITE_INSERT18 ){
229339 rc = sessionAppendDelete(&buf, bPatchset, p, pTab->nCol,pTab->abPK);
229340 }
229341 if( rc==SQLITE_OK0 ){
229342 rc = sqlite3_reset(pSel);
229343 }
229344
229345 /* If the buffer is now larger than sessions_strm_chunk_size, pass
229346 ** its contents to the xOutput() callback. */
229347 if( xOutput
229348 && rc==SQLITE_OK0
229349 && buf.nBuf>nNoop
229350 && buf.nBuf>sessions_strm_chunk_size
229351 ){
229352 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
229353 nNoop = -1;
229354 buf.nBuf = 0;
229355 }
229356
229357 }
229358 }
229359
229360 sqlite3_finalize(pSel);
229361 if( buf.nBuf==nNoop ){
229362 buf.nBuf = nRewind;
229363 }
229364 }
229365 }
229366
229367 if( rc==SQLITE_OK0 ){
229368 if( xOutput==0 ){
229369 *pnChangeset = buf.nBuf;
229370 *ppChangeset = buf.aBuf;
229371 buf.aBuf = 0;
229372 }else if( buf.nBuf>0 ){
229373 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
229374 }
229375 }
229376
229377 sqlite3_free(buf.aBuf);
229378 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
229379 sqlite3_mutex_leave(sqlite3_db_mutex(db));
229380 return rc;
229381}
229382
229383/*
229384** Obtain a changeset object containing all changes recorded by the
229385** session object passed as the first argument.
229386**
229387** It is the responsibility of the caller to eventually free the buffer
229388** using sqlite3_free().
229389*/
229390SQLITE_API int sqlite3session_changeset(
229391 sqlite3_session *pSession, /* Session object */
229392 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
229393 void **ppChangeset /* OUT: Buffer containing changeset */
229394){
229395 int rc;
229396
229397 if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE21;
229398 rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
229399 assert( rc || pnChangeset==0((void) (0))
229400 || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize((void) (0))
229401 )((void) (0));
229402 return rc;
229403}
229404
229405/*
229406** Streaming version of sqlite3session_changeset().
229407*/
229408SQLITE_API int sqlite3session_changeset_strm(
229409 sqlite3_session *pSession,
229410 int (*xOutput)(void *pOut, const void *pData, int nData),
229411 void *pOut
229412){
229413 if( xOutput==0 ) return SQLITE_MISUSE21;
229414 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
229415}
229416
229417/*
229418** Streaming version of sqlite3session_patchset().
229419*/
229420SQLITE_API int sqlite3session_patchset_strm(
229421 sqlite3_session *pSession,
229422 int (*xOutput)(void *pOut, const void *pData, int nData),
229423 void *pOut
229424){
229425 if( xOutput==0 ) return SQLITE_MISUSE21;
229426 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
229427}
229428
229429/*
229430** Obtain a patchset object containing all changes recorded by the
229431** session object passed as the first argument.
229432**
229433** It is the responsibility of the caller to eventually free the buffer
229434** using sqlite3_free().
229435*/
229436SQLITE_API int sqlite3session_patchset(
229437 sqlite3_session *pSession, /* Session object */
229438 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
229439 void **ppPatchset /* OUT: Buffer containing changeset */
229440){
229441 if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE21;
229442 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
229443}
229444
229445/*
229446** Enable or disable the session object passed as the first argument.
229447*/
229448SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
229449 int ret;
229450 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
229451 if( bEnable>=0 ){
229452 pSession->bEnable = bEnable;
229453 }
229454 ret = pSession->bEnable;
229455 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
229456 return ret;
229457}
229458
229459/*
229460** Enable or disable the session object passed as the first argument.
229461*/
229462SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
229463 int ret;
229464 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
229465 if( bIndirect>=0 ){
229466 pSession->bIndirect = bIndirect;
229467 }
229468 ret = pSession->bIndirect;
229469 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
229470 return ret;
229471}
229472
229473/*
229474** Return true if there have been no changes to monitored tables recorded
229475** by the session object passed as the only argument.
229476*/
229477SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
229478 int ret = 0;
229479 SessionTable *pTab;
229480
229481 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
229482 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
229483 ret = (pTab->nEntry>0);
229484 }
229485 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
229486
229487 return (ret==0);
229488}
229489
229490/*
229491** Return the amount of heap memory in use.
229492*/
229493SQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){
229494 return pSession->nMalloc;
229495}
229496
229497/*
229498** Configure the session object passed as the first argument.
229499*/
229500SQLITE_API int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){
229501 int rc = SQLITE_OK0;
229502 switch( op ){
229503 case SQLITE_SESSION_OBJCONFIG_SIZE1: {
229504 int iArg = *(int*)pArg;
229505 if( iArg>=0 ){
229506 if( pSession->pTable ){
229507 rc = SQLITE_MISUSE21;
229508 }else{
229509 pSession->bEnableSize = (iArg!=0);
229510 }
229511 }
229512 *(int*)pArg = pSession->bEnableSize;
229513 break;
229514 }
229515
229516 case SQLITE_SESSION_OBJCONFIG_ROWID2: {
229517 int iArg = *(int*)pArg;
229518 if( iArg>=0 ){
229519 if( pSession->pTable ){
229520 rc = SQLITE_MISUSE21;
229521 }else{
229522 pSession->bImplicitPK = (iArg!=0);
229523 }
229524 }
229525 *(int*)pArg = pSession->bImplicitPK;
229526 break;
229527 }
229528
229529 default:
229530 rc = SQLITE_MISUSE21;
229531 }
229532
229533 return rc;
229534}
229535
229536/*
229537** Return the maximum size of sqlite3session_changeset() output.
229538*/
229539SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){
229540 return pSession->nMaxChangesetSize;
229541}
229542
229543/*
229544** Do the work for either sqlite3changeset_start() or start_strm().
229545*/
229546static int sessionChangesetStart(
229547 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
229548 int (*xInput)(void *pIn, void *pData, int *pnData),
229549 void *pIn,
229550 int nChangeset, /* Size of buffer pChangeset in bytes */
229551 void *pChangeset, /* Pointer to buffer containing changeset */
229552 int bInvert, /* True to invert changeset */
229553 int bSkipEmpty /* True to skip empty UPDATE changes */
229554){
229555 sqlite3_changeset_iter *pRet; /* Iterator to return */
229556 int nByte; /* Number of bytes to allocate for iterator */
229557
229558 assert( xInput==0 || (pChangeset==0 && nChangeset==0) )((void) (0));
229559
229560 /* Zero the output variable in case an error occurs. */
229561 *pp = 0;
229562
229563 /* Allocate and initialize the iterator structure. */
229564 nByte = sizeof(sqlite3_changeset_iter);
229565 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
229566 if( !pRet ) return SQLITE_NOMEM7;
229567 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
229568 pRet->in.aData = (u8 *)pChangeset;
229569 pRet->in.nData = nChangeset;
229570 pRet->in.xInput = xInput;
229571 pRet->in.pIn = pIn;
229572 pRet->in.bEof = (xInput ? 0 : 1);
229573 pRet->bInvert = bInvert;
229574 pRet->bSkipEmpty = bSkipEmpty;
229575
229576 /* Populate the output variable and return success. */
229577 *pp = pRet;
229578 return SQLITE_OK0;
229579}
229580
229581/*
229582** Create an iterator used to iterate through the contents of a changeset.
229583*/
229584SQLITE_API int sqlite3changeset_start(
229585 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
229586 int nChangeset, /* Size of buffer pChangeset in bytes */
229587 void *pChangeset /* Pointer to buffer containing changeset */
229588){
229589 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0);
229590}
229591SQLITE_API int sqlite3changeset_start_v2(
229592 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
229593 int nChangeset, /* Size of buffer pChangeset in bytes */
229594 void *pChangeset, /* Pointer to buffer containing changeset */
229595 int flags
229596){
229597 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT0x0002);
229598 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0);
229599}
229600
229601/*
229602** Streaming version of sqlite3changeset_start().
229603*/
229604SQLITE_API int sqlite3changeset_start_strm(
229605 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
229606 int (*xInput)(void *pIn, void *pData, int *pnData),
229607 void *pIn
229608){
229609 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0);
229610}
229611SQLITE_API int sqlite3changeset_start_v2_strm(
229612 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
229613 int (*xInput)(void *pIn, void *pData, int *pnData),
229614 void *pIn,
229615 int flags
229616){
229617 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT0x0002);
229618 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0);
229619}
229620
229621/*
229622** If the SessionInput object passed as the only argument is a streaming
229623** object and the buffer is full, discard some data to free up space.
229624*/
229625static void sessionDiscardData(SessionInput *pIn){
229626 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
229627 int nMove = pIn->buf.nBuf - pIn->iNext;
229628 assert( nMove>=0 )((void) (0));
229629 if( nMove>0 ){
229630 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
229631 }
229632 pIn->buf.nBuf -= pIn->iNext;
229633 pIn->iNext = 0;
229634 pIn->nData = pIn->buf.nBuf;
229635 }
229636}
229637
229638/*
229639** Ensure that there are at least nByte bytes available in the buffer. Or,
229640** if there are not nByte bytes remaining in the input, that all available
229641** data is in the buffer.
229642**
229643** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
229644*/
229645static int sessionInputBuffer(SessionInput *pIn, int nByte){
229646 int rc = SQLITE_OK0;
229647 if( pIn->xInput ){
229648 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK0 ){
229649 int nNew = sessions_strm_chunk_size;
229650
229651 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
229652 if( SQLITE_OK0==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
229653 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
229654 if( nNew==0 ){
229655 pIn->bEof = 1;
229656 }else{
229657 pIn->buf.nBuf += nNew;
229658 }
229659 }
229660
229661 pIn->aData = pIn->buf.aBuf;
229662 pIn->nData = pIn->buf.nBuf;
229663 }
229664 }
229665 return rc;
229666}
229667
229668/*
229669** When this function is called, *ppRec points to the start of a record
229670** that contains nCol values. This function advances the pointer *ppRec
229671** until it points to the byte immediately following that record.
229672*/
229673static void sessionSkipRecord(
229674 u8 **ppRec, /* IN/OUT: Record pointer */
229675 int nCol /* Number of values in record */
229676){
229677 u8 *aRec = *ppRec;
229678 int i;
229679 for(i=0; i<nCol; i++){
229680 int eType = *aRec++;
229681 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
229682 int nByte;
229683 aRec += sessionVarintGet((u8*)aRec, &nByte);
229684 aRec += nByte;
229685 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
229686 aRec += 8;
229687 }
229688 }
229689
229690 *ppRec = aRec;
229691}
229692
229693/*
229694** This function sets the value of the sqlite3_value object passed as the
229695** first argument to a copy of the string or blob held in the aData[]
229696** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
229697** error occurs.
229698*/
229699static int sessionValueSetStr(
229700 sqlite3_value *pVal, /* Set the value of this object */
229701 u8 *aData, /* Buffer containing string or blob data */
229702 int nData, /* Size of buffer aData[] in bytes */
229703 u8 enc /* String encoding (0 for blobs) */
229704){
229705 /* In theory this code could just pass SQLITE_TRANSIENT as the final
229706 ** argument to sqlite3ValueSetStr() and have the copy created
229707 ** automatically. But doing so makes it difficult to detect any OOM
229708 ** error. Hence the code to create the copy externally. */
229709 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
229710 if( aCopy==0 ) return SQLITE_NOMEM7;
229711 memcpy(aCopy, aData, nData);
229712 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
229713 return SQLITE_OK0;
229714}
229715
229716/*
229717** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
229718** for details.
229719**
229720** When this function is called, *paChange points to the start of the record
229721** to deserialize. Assuming no error occurs, *paChange is set to point to
229722** one byte after the end of the same record before this function returns.
229723** If the argument abPK is NULL, then the record contains nCol values. Or,
229724** if abPK is other than NULL, then the record contains only the PK fields
229725** (in other words, it is a patchset DELETE record).
229726**
229727** If successful, each element of the apOut[] array (allocated by the caller)
229728** is set to point to an sqlite3_value object containing the value read
229729** from the corresponding position in the record. If that value is not
229730** included in the record (i.e. because the record is part of an UPDATE change
229731** and the field was not modified), the corresponding element of apOut[] is
229732** set to NULL.
229733**
229734** It is the responsibility of the caller to free all sqlite_value structures
229735** using sqlite3_free().
229736**
229737** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
229738** The apOut[] array may have been partially populated in this case.
229739*/
229740static int sessionReadRecord(
229741 SessionInput *pIn, /* Input data */
229742 int nCol, /* Number of values in record */
229743 u8 *abPK, /* Array of primary key flags, or NULL */
229744 sqlite3_value **apOut, /* Write values to this array */
229745 int *pbEmpty
229746){
229747 int i; /* Used to iterate through columns */
229748 int rc = SQLITE_OK0;
229749
229750 assert( pbEmpty==0 || *pbEmpty==0 )((void) (0));
229751 if( pbEmpty ) *pbEmpty = 1;
229752 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
229753 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
229754 if( abPK && abPK[i]==0 ) continue;
229755 rc = sessionInputBuffer(pIn, 9);
229756 if( rc==SQLITE_OK0 ){
229757 if( pIn->iNext>=pIn->nData ){
229758 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(229758);
229759 }else{
229760 eType = pIn->aData[pIn->iNext++];
229761 assert( apOut[i]==0 )((void) (0));
229762 if( eType ){
229763 if( pbEmpty ) *pbEmpty = 0;
229764 apOut[i] = sqlite3ValueNew(0);
229765 if( !apOut[i] ) rc = SQLITE_NOMEM7;
229766 }
229767 }
229768 }
229769
229770 if( rc==SQLITE_OK0 ){
229771 u8 *aVal = &pIn->aData[pIn->iNext];
229772 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
229773 int nByte;
229774 pIn->iNext += sessionVarintGet(aVal, &nByte);
229775 rc = sessionInputBuffer(pIn, nByte);
229776 if( rc==SQLITE_OK0 ){
229777 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
229778 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(229778);
229779 }else{
229780 u8 enc = (eType==SQLITE_TEXT3 ? SQLITE_UTF81 : 0);
229781 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
229782 pIn->iNext += nByte;
229783 }
229784 }
229785 }
229786 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
229787 if( (pIn->nData-pIn->iNext)<8 ){
229788 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(229788);
229789 }else{
229790 sqlite3_int64 v = sessionGetI64(aVal);
229791 if( eType==SQLITE_INTEGER1 ){
229792 sqlite3VdbeMemSetInt64(apOut[i], v);
229793 }else{
229794 double d;
229795 memcpy(&d, &v, 8);
229796 sqlite3VdbeMemSetDouble(apOut[i], d);
229797 }
229798 pIn->iNext += 8;
229799 }
229800 }
229801 }
229802 }
229803
229804 return rc;
229805}
229806
229807/*
229808** The input pointer currently points to the second byte of a table-header.
229809** Specifically, to the following:
229810**
229811** + number of columns in table (varint)
229812** + array of PK flags (1 byte per column),
229813** + table name (nul terminated).
229814**
229815** This function ensures that all of the above is present in the input
229816** buffer (i.e. that it can be accessed without any calls to xInput()).
229817** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
229818** The input pointer is not moved.
229819*/
229820static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
229821 int rc = SQLITE_OK0;
229822 int nCol = 0;
229823 int nRead = 0;
229824
229825 rc = sessionInputBuffer(pIn, 9);
229826 if( rc==SQLITE_OK0 ){
229827 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
229828 /* The hard upper limit for the number of columns in an SQLite
229829 ** database table is, according to sqliteLimit.h, 32676. So
229830 ** consider any table-header that purports to have more than 65536
229831 ** columns to be corrupt. This is convenient because otherwise,
229832 ** if the (nCol>65536) condition below were omitted, a sufficiently
229833 ** large value for nCol may cause nRead to wrap around and become
229834 ** negative. Leading to a crash. */
229835 if( nCol<0 || nCol>65536 ){
229836 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(229836);
229837 }else{
229838 rc = sessionInputBuffer(pIn, nRead+nCol+100);
229839 nRead += nCol;
229840 }
229841 }
229842
229843 while( rc==SQLITE_OK0 ){
229844 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
229845 nRead++;
229846 }
229847 if( (pIn->iNext + nRead)<pIn->nData ) break;
229848 rc = sessionInputBuffer(pIn, nRead + 100);
229849 }
229850 *pnByte = nRead+1;
229851 return rc;
229852}
229853
229854/*
229855** The input pointer currently points to the first byte of the first field
229856** of a record consisting of nCol columns. This function ensures the entire
229857** record is buffered. It does not move the input pointer.
229858**
229859** If successful, SQLITE_OK is returned and *pnByte is set to the size of
229860** the record in bytes. Otherwise, an SQLite error code is returned. The
229861** final value of *pnByte is undefined in this case.
229862*/
229863static int sessionChangesetBufferRecord(
229864 SessionInput *pIn, /* Input data */
229865 int nCol, /* Number of columns in record */
229866 int *pnByte /* OUT: Size of record in bytes */
229867){
229868 int rc = SQLITE_OK0;
229869 int nByte = 0;
229870 int i;
229871 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
229872 int eType;
229873 rc = sessionInputBuffer(pIn, nByte + 10);
229874 if( rc==SQLITE_OK0 ){
229875 eType = pIn->aData[pIn->iNext + nByte++];
229876 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
229877 int n;
229878 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
229879 nByte += n;
229880 rc = sessionInputBuffer(pIn, nByte);
229881 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
229882 nByte += 8;
229883 }
229884 }
229885 }
229886 *pnByte = nByte;
229887 return rc;
229888}
229889
229890/*
229891** The input pointer currently points to the second byte of a table-header.
229892** Specifically, to the following:
229893**
229894** + number of columns in table (varint)
229895** + array of PK flags (1 byte per column),
229896** + table name (nul terminated).
229897**
229898** This function decodes the table-header and populates the p->nCol,
229899** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
229900** also allocated or resized according to the new value of p->nCol. The
229901** input pointer is left pointing to the byte following the table header.
229902**
229903** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
229904** is returned and the final values of the various fields enumerated above
229905** are undefined.
229906*/
229907static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
229908 int rc;
229909 int nCopy;
229910 assert( p->rc==SQLITE_OK )((void) (0));
229911
229912 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
229913 if( rc==SQLITE_OK0 ){
229914 int nByte;
229915 int nVarint;
229916 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
229917 if( p->nCol>0 ){
229918 nCopy -= nVarint;
229919 p->in.iNext += nVarint;
229920 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
229921 p->tblhdr.nBuf = 0;
229922 sessionBufferGrow(&p->tblhdr, nByte, &rc);
229923 }else{
229924 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(229924);
229925 }
229926 }
229927
229928 if( rc==SQLITE_OK0 ){
229929 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
229930 memset(p->tblhdr.aBuf, 0, iPK);
229931 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
229932 p->in.iNext += nCopy;
229933 }
229934
229935 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
229936 if( p->apValue==0 ){
229937 p->abPK = 0;
229938 p->zTab = 0;
229939 }else{
229940 p->abPK = (u8*)&p->apValue[p->nCol*2];
229941 p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0;
229942 }
229943 return (p->rc = rc);
229944}
229945
229946/*
229947** Advance the changeset iterator to the next change. The differences between
229948** this function and sessionChangesetNext() are that
229949**
229950** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
229951** that modifies no columns), this function sets (*pbEmpty) to 1.
229952**
229953** * If the iterator is configured to skip no-op UPDATEs,
229954** sessionChangesetNext() does that. This function does not.
229955*/
229956static int sessionChangesetNextOne(
229957 sqlite3_changeset_iter *p, /* Changeset iterator */
229958 u8 **paRec, /* If non-NULL, store record pointer here */
229959 int *pnRec, /* If non-NULL, store size of record here */
229960 int *pbNew, /* If non-NULL, true if new table */
229961 int *pbEmpty
229962){
229963 int i;
229964 u8 op;
229965
229966 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) )((void) (0));
229967 assert( pbEmpty==0 || *pbEmpty==0 )((void) (0));
229968
229969 /* If the iterator is in the error-state, return immediately. */
229970 if( p->rc!=SQLITE_OK0 ) return p->rc;
229971
229972 /* Free the current contents of p->apValue[], if any. */
229973 if( p->apValue ){
229974 for(i=0; i<p->nCol*2; i++){
229975 sqlite3ValueFree(p->apValue[i]);
229976 }
229977 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
229978 }
229979
229980 /* Make sure the buffer contains at least 10 bytes of input data, or all
229981 ** remaining data if there are less than 10 bytes available. This is
229982 ** sufficient either for the 'T' or 'P' byte and the varint that follows
229983 ** it, or for the two single byte values otherwise. */
229984 p->rc = sessionInputBuffer(&p->in, 2);
229985 if( p->rc!=SQLITE_OK0 ) return p->rc;
229986
229987 sessionDiscardData(&p->in);
229988 p->in.iCurrent = p->in.iNext;
229989
229990 /* If the iterator is already at the end of the changeset, return DONE. */
229991 if( p->in.iNext>=p->in.nData ){
229992 return SQLITE_DONE101;
229993 }
229994
229995 op = p->in.aData[p->in.iNext++];
229996 while( op=='T' || op=='P' ){
229997 if( pbNew ) *pbNew = 1;
229998 p->bPatchset = (op=='P');
229999 if( sessionChangesetReadTblhdr(p) ) return p->rc;
230000 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
230001 p->in.iCurrent = p->in.iNext;
230002 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE101;
230003 op = p->in.aData[p->in.iNext++];
230004 }
230005
230006 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
230007 /* The first record in the changeset is not a table header. Must be a
230008 ** corrupt changeset. */
230009 assert( p->in.iNext==1 || p->zTab )((void) (0));
230010 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(230010));
230011 }
230012
230013 p->op = op;
230014 p->bIndirect = p->in.aData[p->in.iNext++];
230015 if( p->op!=SQLITE_UPDATE23 && p->op!=SQLITE_DELETE9 && p->op!=SQLITE_INSERT18 ){
230016 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(230016));
230017 }
230018
230019 if( paRec ){
230020 int nVal; /* Number of values to buffer */
230021 if( p->bPatchset==0 && op==SQLITE_UPDATE23 ){
230022 nVal = p->nCol * 2;
230023 }else if( p->bPatchset && op==SQLITE_DELETE9 ){
230024 nVal = 0;
230025 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
230026 }else{
230027 nVal = p->nCol;
230028 }
230029 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
230030 if( p->rc!=SQLITE_OK0 ) return p->rc;
230031 *paRec = &p->in.aData[p->in.iNext];
230032 p->in.iNext += *pnRec;
230033 }else{
230034 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
230035 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
230036
230037 /* If this is an UPDATE or DELETE, read the old.* record. */
230038 if( p->op!=SQLITE_INSERT18 && (p->bPatchset==0 || p->op==SQLITE_DELETE9) ){
230039 u8 *abPK = p->bPatchset ? p->abPK : 0;
230040 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0);
230041 if( p->rc!=SQLITE_OK0 ) return p->rc;
230042 }
230043
230044 /* If this is an INSERT or UPDATE, read the new.* record. */
230045 if( p->op!=SQLITE_DELETE9 ){
230046 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty);
230047 if( p->rc!=SQLITE_OK0 ) return p->rc;
230048 }
230049
230050 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE23 ){
230051 /* If this is an UPDATE that is part of a patchset, then all PK and
230052 ** modified fields are present in the new.* record. The old.* record
230053 ** is currently completely empty. This block shifts the PK fields from
230054 ** new.* to old.*, to accommodate the code that reads these arrays. */
230055 for(i=0; i<p->nCol; i++){
230056 assert( p->bPatchset==0 || p->apValue[i]==0 )((void) (0));
230057 if( p->abPK[i] ){
230058 assert( p->apValue[i]==0 )((void) (0));
230059 p->apValue[i] = p->apValue[i+p->nCol];
230060 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(230060));
230061 p->apValue[i+p->nCol] = 0;
230062 }
230063 }
230064 }else if( p->bInvert ){
230065 if( p->op==SQLITE_INSERT18 ) p->op = SQLITE_DELETE9;
230066 else if( p->op==SQLITE_DELETE9 ) p->op = SQLITE_INSERT18;
230067 }
230068
230069 /* If this is an UPDATE that is part of a changeset, then check that
230070 ** there are no fields in the old.* record that are not (a) PK fields,
230071 ** or (b) also present in the new.* record.
230072 **
230073 ** Such records are technically corrupt, but the rebaser was at one
230074 ** point generating them. Under most circumstances this is benign, but
230075 ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
230076 if( p->bPatchset==0 && p->op==SQLITE_UPDATE23){
230077 for(i=0; i<p->nCol; i++){
230078 if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){
230079 sqlite3ValueFree(p->apValue[i]);
230080 p->apValue[i] = 0;
230081 }
230082 }
230083 }
230084 }
230085
230086 return SQLITE_ROW100;
230087}
230088
230089/*
230090** Advance the changeset iterator to the next change.
230091**
230092** If both paRec and pnRec are NULL, then this function works like the public
230093** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
230094** sqlite3changeset_new() and old() APIs may be used to query for values.
230095**
230096** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
230097** record is written to *paRec before returning and the number of bytes in
230098** the record to *pnRec.
230099**
230100** Either way, this function returns SQLITE_ROW if the iterator is
230101** successfully advanced to the next change in the changeset, an SQLite
230102** error code if an error occurs, or SQLITE_DONE if there are no further
230103** changes in the changeset.
230104*/
230105static int sessionChangesetNext(
230106 sqlite3_changeset_iter *p, /* Changeset iterator */
230107 u8 **paRec, /* If non-NULL, store record pointer here */
230108 int *pnRec, /* If non-NULL, store size of record here */
230109 int *pbNew /* If non-NULL, true if new table */
230110){
230111 int bEmpty;
230112 int rc;
230113 do {
230114 bEmpty = 0;
230115 rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty);
230116 }while( rc==SQLITE_ROW100 && p->bSkipEmpty && bEmpty);
230117 return rc;
230118}
230119
230120/*
230121** Advance an iterator created by sqlite3changeset_start() to the next
230122** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
230123** or SQLITE_CORRUPT.
230124**
230125** This function may not be called on iterators passed to a conflict handler
230126** callback by changeset_apply().
230127*/
230128SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
230129 return sessionChangesetNext(p, 0, 0, 0);
230130}
230131
230132/*
230133** The following function extracts information on the current change
230134** from a changeset iterator. It may only be called after changeset_next()
230135** has returned SQLITE_ROW.
230136*/
230137SQLITE_API int sqlite3changeset_op(
230138 sqlite3_changeset_iter *pIter, /* Iterator handle */
230139 const char **pzTab, /* OUT: Pointer to table name */
230140 int *pnCol, /* OUT: Number of columns in table */
230141 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
230142 int *pbIndirect /* OUT: True if change is indirect */
230143){
230144 *pOp = pIter->op;
230145 *pnCol = pIter->nCol;
230146 *pzTab = pIter->zTab;
230147 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
230148 return SQLITE_OK0;
230149}
230150
230151/*
230152** Return information regarding the PRIMARY KEY and number of columns in
230153** the database table affected by the change that pIter currently points
230154** to. This function may only be called after changeset_next() returns
230155** SQLITE_ROW.
230156*/
230157SQLITE_API int sqlite3changeset_pk(
230158 sqlite3_changeset_iter *pIter, /* Iterator object */
230159 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
230160 int *pnCol /* OUT: Number of entries in output array */
230161){
230162 *pabPK = pIter->abPK;
230163 if( pnCol ) *pnCol = pIter->nCol;
230164 return SQLITE_OK0;
230165}
230166
230167/*
230168** This function may only be called while the iterator is pointing to an
230169** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
230170** Otherwise, SQLITE_MISUSE is returned.
230171**
230172** It sets *ppValue to point to an sqlite3_value structure containing the
230173** iVal'th value in the old.* record. Or, if that particular value is not
230174** included in the record (because the change is an UPDATE and the field
230175** was not modified and is not a PK column), set *ppValue to NULL.
230176**
230177** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
230178** not modified. Otherwise, SQLITE_OK.
230179*/
230180SQLITE_API int sqlite3changeset_old(
230181 sqlite3_changeset_iter *pIter, /* Changeset iterator */
230182 int iVal, /* Index of old.* value to retrieve */
230183 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
230184){
230185 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_DELETE9 ){
230186 return SQLITE_MISUSE21;
230187 }
230188 if( iVal<0 || iVal>=pIter->nCol ){
230189 return SQLITE_RANGE25;
230190 }
230191 *ppValue = pIter->apValue[iVal];
230192 return SQLITE_OK0;
230193}
230194
230195/*
230196** This function may only be called while the iterator is pointing to an
230197** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
230198** Otherwise, SQLITE_MISUSE is returned.
230199**
230200** It sets *ppValue to point to an sqlite3_value structure containing the
230201** iVal'th value in the new.* record. Or, if that particular value is not
230202** included in the record (because the change is an UPDATE and the field
230203** was not modified), set *ppValue to NULL.
230204**
230205** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
230206** not modified. Otherwise, SQLITE_OK.
230207*/
230208SQLITE_API int sqlite3changeset_new(
230209 sqlite3_changeset_iter *pIter, /* Changeset iterator */
230210 int iVal, /* Index of new.* value to retrieve */
230211 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
230212){
230213 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_INSERT18 ){
230214 return SQLITE_MISUSE21;
230215 }
230216 if( iVal<0 || iVal>=pIter->nCol ){
230217 return SQLITE_RANGE25;
230218 }
230219 *ppValue = pIter->apValue[pIter->nCol+iVal];
230220 return SQLITE_OK0;
230221}
230222
230223/*
230224** The following two macros are used internally. They are similar to the
230225** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
230226** they omit all error checking and return a pointer to the requested value.
230227*/
230228#define sessionChangesetNew(pIter, iVal)(pIter)->apValue[(pIter)->nCol+(iVal)] (pIter)->apValue[(pIter)->nCol+(iVal)]
230229#define sessionChangesetOld(pIter, iVal)(pIter)->apValue[(iVal)] (pIter)->apValue[(iVal)]
230230
230231/*
230232** This function may only be called with a changeset iterator that has been
230233** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
230234** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
230235**
230236** If successful, *ppValue is set to point to an sqlite3_value structure
230237** containing the iVal'th value of the conflicting record.
230238**
230239** If value iVal is out-of-range or some other error occurs, an SQLite error
230240** code is returned. Otherwise, SQLITE_OK.
230241*/
230242SQLITE_API int sqlite3changeset_conflict(
230243 sqlite3_changeset_iter *pIter, /* Changeset iterator */
230244 int iVal, /* Index of conflict record value to fetch */
230245 sqlite3_value **ppValue /* OUT: Value from conflicting row */
230246){
230247 if( !pIter->pConflict ){
230248 return SQLITE_MISUSE21;
230249 }
230250 if( iVal<0 || iVal>=pIter->nCol ){
230251 return SQLITE_RANGE25;
230252 }
230253 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
230254 return SQLITE_OK0;
230255}
230256
230257/*
230258** This function may only be called with an iterator passed to an
230259** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
230260** it sets the output variable to the total number of known foreign key
230261** violations in the destination database and returns SQLITE_OK.
230262**
230263** In all other cases this function returns SQLITE_MISUSE.
230264*/
230265SQLITE_API int sqlite3changeset_fk_conflicts(
230266 sqlite3_changeset_iter *pIter, /* Changeset iterator */
230267 int *pnOut /* OUT: Number of FK violations */
230268){
230269 if( pIter->pConflict || pIter->apValue ){
230270 return SQLITE_MISUSE21;
230271 }
230272 *pnOut = pIter->nCol;
230273 return SQLITE_OK0;
230274}
230275
230276
230277/*
230278** Finalize an iterator allocated with sqlite3changeset_start().
230279**
230280** This function may not be called on iterators passed to a conflict handler
230281** callback by changeset_apply().
230282*/
230283SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
230284 int rc = SQLITE_OK0;
230285 if( p ){
230286 int i; /* Used to iterate through p->apValue[] */
230287 rc = p->rc;
230288 if( p->apValue ){
230289 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
230290 }
230291 sqlite3_free(p->tblhdr.aBuf);
230292 sqlite3_free(p->in.buf.aBuf);
230293 sqlite3_free(p);
230294 }
230295 return rc;
230296}
230297
230298static int sessionChangesetInvert(
230299 SessionInput *pInput, /* Input changeset */
230300 int (*xOutput)(void *pOut, const void *pData, int nData),
230301 void *pOut,
230302 int *pnInverted, /* OUT: Number of bytes in output changeset */
230303 void **ppInverted /* OUT: Inverse of pChangeset */
230304){
230305 int rc = SQLITE_OK0; /* Return value */
230306 SessionBuffer sOut; /* Output buffer */
230307 int nCol = 0; /* Number of cols in current table */
230308 u8 *abPK = 0; /* PK array for current table */
230309 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
230310 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
230311
230312 /* Initialize the output buffer */
230313 memset(&sOut, 0, sizeof(SessionBuffer));
230314
230315 /* Zero the output variables in case an error occurs. */
230316 if( ppInverted ){
230317 *ppInverted = 0;
230318 *pnInverted = 0;
230319 }
230320
230321 while( 1 ){
230322 u8 eType;
230323
230324 /* Test for EOF. */
230325 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
230326 if( pInput->iNext>=pInput->nData ) break;
230327 eType = pInput->aData[pInput->iNext];
230328
230329 switch( eType ){
230330 case 'T': {
230331 /* A 'table' record consists of:
230332 **
230333 ** * A constant 'T' character,
230334 ** * Number of columns in said table (a varint),
230335 ** * An array of nCol bytes (sPK),
230336 ** * A nul-terminated table name.
230337 */
230338 int nByte;
230339 int nVar;
230340 pInput->iNext++;
230341 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
230342 goto finished_invert;
230343 }
230344 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
230345 sPK.nBuf = 0;
230346 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
230347 sessionAppendByte(&sOut, eType, &rc);
230348 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
230349 if( rc ) goto finished_invert;
230350
230351 pInput->iNext += nByte;
230352 sqlite3_free(apVal);
230353 apVal = 0;
230354 abPK = sPK.aBuf;
230355 break;
230356 }
230357
230358 case SQLITE_INSERT18:
230359 case SQLITE_DELETE9: {
230360 int nByte;
230361 int bIndirect = pInput->aData[pInput->iNext+1];
230362 int eType2 = (eType==SQLITE_DELETE9 ? SQLITE_INSERT18 : SQLITE_DELETE9);
230363 pInput->iNext += 2;
230364 assert( rc==SQLITE_OK )((void) (0));
230365 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
230366 sessionAppendByte(&sOut, eType2, &rc);
230367 sessionAppendByte(&sOut, bIndirect, &rc);
230368 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
230369 pInput->iNext += nByte;
230370 if( rc ) goto finished_invert;
230371 break;
230372 }
230373
230374 case SQLITE_UPDATE23: {
230375 int iCol;
230376
230377 if( 0==apVal ){
230378 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
230379 if( 0==apVal ){
230380 rc = SQLITE_NOMEM7;
230381 goto finished_invert;
230382 }
230383 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
230384 }
230385
230386 /* Write the header for the new UPDATE change. Same as the original. */
230387 sessionAppendByte(&sOut, eType, &rc);
230388 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
230389
230390 /* Read the old.* and new.* records for the update change. */
230391 pInput->iNext += 2;
230392 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0);
230393 if( rc==SQLITE_OK0 ){
230394 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0);
230395 }
230396
230397 /* Write the new old.* record. Consists of the PK columns from the
230398 ** original old.* record, and the other values from the original
230399 ** new.* record. */
230400 for(iCol=0; iCol<nCol; iCol++){
230401 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
230402 sessionAppendValue(&sOut, pVal, &rc);
230403 }
230404
230405 /* Write the new new.* record. Consists of a copy of all values
230406 ** from the original old.* record, except for the PK columns, which
230407 ** are set to "undefined". */
230408 for(iCol=0; iCol<nCol; iCol++){
230409 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
230410 sessionAppendValue(&sOut, pVal, &rc);
230411 }
230412
230413 for(iCol=0; iCol<nCol*2; iCol++){
230414 sqlite3ValueFree(apVal[iCol]);
230415 }
230416 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
230417 if( rc!=SQLITE_OK0 ){
230418 goto finished_invert;
230419 }
230420
230421 break;
230422 }
230423
230424 default:
230425 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(230425);
230426 goto finished_invert;
230427 }
230428
230429 assert( rc==SQLITE_OK )((void) (0));
230430 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
230431 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
230432 sOut.nBuf = 0;
230433 if( rc!=SQLITE_OK0 ) goto finished_invert;
230434 }
230435 }
230436
230437 assert( rc==SQLITE_OK )((void) (0));
230438 if( pnInverted && ALWAYS(ppInverted)(ppInverted) ){
230439 *pnInverted = sOut.nBuf;
230440 *ppInverted = sOut.aBuf;
230441 sOut.aBuf = 0;
230442 }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0)(xOutput!=0) ){
230443 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
230444 }
230445
230446 finished_invert:
230447 sqlite3_free(sOut.aBuf);
230448 sqlite3_free(apVal);
230449 sqlite3_free(sPK.aBuf);
230450 return rc;
230451}
230452
230453
230454/*
230455** Invert a changeset object.
230456*/
230457SQLITE_API int sqlite3changeset_invert(
230458 int nChangeset, /* Number of bytes in input */
230459 const void *pChangeset, /* Input changeset */
230460 int *pnInverted, /* OUT: Number of bytes in output changeset */
230461 void **ppInverted /* OUT: Inverse of pChangeset */
230462){
230463 SessionInput sInput;
230464
230465 /* Set up the input stream */
230466 memset(&sInput, 0, sizeof(SessionInput));
230467 sInput.nData = nChangeset;
230468 sInput.aData = (u8*)pChangeset;
230469
230470 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
230471}
230472
230473/*
230474** Streaming version of sqlite3changeset_invert().
230475*/
230476SQLITE_API int sqlite3changeset_invert_strm(
230477 int (*xInput)(void *pIn, void *pData, int *pnData),
230478 void *pIn,
230479 int (*xOutput)(void *pOut, const void *pData, int nData),
230480 void *pOut
230481){
230482 SessionInput sInput;
230483 int rc;
230484
230485 /* Set up the input stream */
230486 memset(&sInput, 0, sizeof(SessionInput));
230487 sInput.xInput = xInput;
230488 sInput.pIn = pIn;
230489
230490 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
230491 sqlite3_free(sInput.buf.aBuf);
230492 return rc;
230493}
230494
230495
230496typedef struct SessionUpdate SessionUpdate;
230497struct SessionUpdate {
230498 sqlite3_stmt *pStmt;
230499 u32 *aMask;
230500 SessionUpdate *pNext;
230501};
230502
230503typedef struct SessionApplyCtx SessionApplyCtx;
230504struct SessionApplyCtx {
230505 sqlite3 *db;
230506 sqlite3_stmt *pDelete; /* DELETE statement */
230507 sqlite3_stmt *pInsert; /* INSERT statement */
230508 sqlite3_stmt *pSelect; /* SELECT statement */
230509 int nCol; /* Size of azCol[] and abPK[] arrays */
230510 const char **azCol; /* Array of column names */
230511 u8 *abPK; /* Boolean array - true if column is in PK */
230512 u32 *aUpdateMask; /* Used by sessionUpdateFind */
230513 SessionUpdate *pUp;
230514 int bStat1; /* True if table is sqlite_stat1 */
230515 int bDeferConstraints; /* True to defer constraints */
230516 int bInvertConstraints; /* Invert when iterating constraints buffer */
230517 SessionBuffer constraints; /* Deferred constraints are stored here */
230518 SessionBuffer rebase; /* Rebase information (if any) here */
230519 u8 bRebaseStarted; /* If table header is already in rebase */
230520 u8 bRebase; /* True to collect rebase information */
230521 u8 bIgnoreNoop; /* True to ignore no-op conflicts */
230522 int bRowid;
230523};
230524
230525/* Number of prepared UPDATE statements to cache. */
230526#define SESSION_UPDATE_CACHE_SZ12 12
230527
230528/*
230529** Find a prepared UPDATE statement suitable for the UPDATE step currently
230530** being visited by the iterator. The UPDATE is of the form:
230531**
230532** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
230533*/
230534static int sessionUpdateFind(
230535 sqlite3_changeset_iter *pIter,
230536 SessionApplyCtx *p,
230537 int bPatchset,
230538 sqlite3_stmt **ppStmt
230539){
230540 int rc = SQLITE_OK0;
230541 SessionUpdate *pUp = 0;
230542 int nCol = pIter->nCol;
230543 int nU32 = (pIter->nCol+33)/32;
230544 int ii;
230545
230546 if( p->aUpdateMask==0 ){
230547 p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32));
230548 if( p->aUpdateMask==0 ){
230549 rc = SQLITE_NOMEM7;
230550 }
230551 }
230552
230553 if( rc==SQLITE_OK0 ){
230554 memset(p->aUpdateMask, 0, nU32*sizeof(u32));
230555 rc = SQLITE_CORRUPT11;
230556 for(ii=0; ii<pIter->nCol; ii++){
230557 if( sessionChangesetNew(pIter, ii)(pIter)->apValue[(pIter)->nCol+(ii)] ){
230558 p->aUpdateMask[ii/32] |= (1<<(ii%32));
230559 rc = SQLITE_OK0;
230560 }
230561 }
230562 }
230563
230564 if( rc==SQLITE_OK0 ){
230565 if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32));
230566
230567 if( p->pUp ){
230568 int nUp = 0;
230569 SessionUpdate **pp = &p->pUp;
230570 while( 1 ){
230571 nUp++;
230572 if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){
230573 pUp = *pp;
230574 *pp = pUp->pNext;
230575 pUp->pNext = p->pUp;
230576 p->pUp = pUp;
230577 break;
230578 }
230579
230580 if( (*pp)->pNext ){
230581 pp = &(*pp)->pNext;
230582 }else{
230583 if( nUp>=SESSION_UPDATE_CACHE_SZ12 ){
230584 sqlite3_finalize((*pp)->pStmt);
230585 sqlite3_free(*pp);
230586 *pp = 0;
230587 }
230588 break;
230589 }
230590 }
230591 }
230592
230593 if( pUp==0 ){
230594 int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32);
230595 int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0);
230596 pUp = (SessionUpdate*)sqlite3_malloc(nByte);
230597 if( pUp==0 ){
230598 rc = SQLITE_NOMEM7;
230599 }else{
230600 const char *zSep = "";
230601 SessionBuffer buf;
230602
230603 memset(&buf, 0, sizeof(buf));
230604 pUp->aMask = (u32*)&pUp[1];
230605 memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32));
230606
230607 sessionAppendStr(&buf, "UPDATE main.", &rc);
230608 sessionAppendIdent(&buf, pIter->zTab, &rc);
230609 sessionAppendStr(&buf, " SET ", &rc);
230610
230611 /* Create the assignments part of the UPDATE */
230612 for(ii=0; ii<pIter->nCol; ii++){
230613 if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii)(pIter)->apValue[(pIter)->nCol+(ii)] ){
230614 sessionAppendStr(&buf, zSep, &rc);
230615 sessionAppendIdent(&buf, p->azCol[ii], &rc);
230616 sessionAppendStr(&buf, " = ?", &rc);
230617 sessionAppendInteger(&buf, ii*2+1, &rc);
230618 zSep = ", ";
230619 }
230620 }
230621
230622 /* Create the WHERE clause part of the UPDATE */
230623 zSep = "";
230624 sessionAppendStr(&buf, " WHERE ", &rc);
230625 for(ii=0; ii<pIter->nCol; ii++){
230626 if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)(pIter)->apValue[(ii)]) ){
230627 sessionAppendStr(&buf, zSep, &rc);
230628 if( bStat1 && ii==1 ){
230629 assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 )((void) (0));
230630 sessionAppendStr(&buf,
230631 "idx IS CASE "
230632 "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL "
230633 "ELSE ?4 END ", &rc
230634 );
230635 }else{
230636 sessionAppendIdent(&buf, p->azCol[ii], &rc);
230637 sessionAppendStr(&buf, " IS ?", &rc);
230638 sessionAppendInteger(&buf, ii*2+2, &rc);
230639 }
230640 zSep = " AND ";
230641 }
230642 }
230643
230644 if( rc==SQLITE_OK0 ){
230645 char *zSql = (char*)buf.aBuf;
230646 rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0);
230647 }
230648
230649 if( rc!=SQLITE_OK0 ){
230650 sqlite3_free(pUp);
230651 pUp = 0;
230652 }else{
230653 pUp->pNext = p->pUp;
230654 p->pUp = pUp;
230655 }
230656 sqlite3_free(buf.aBuf);
230657 }
230658 }
230659 }
230660
230661 assert( (rc==SQLITE_OK)==(pUp!=0) )((void) (0));
230662 if( pUp ){
230663 *ppStmt = pUp->pStmt;
230664 }else{
230665 *ppStmt = 0;
230666 }
230667 return rc;
230668}
230669
230670/*
230671** Free all cached UPDATE statements.
230672*/
230673static void sessionUpdateFree(SessionApplyCtx *p){
230674 SessionUpdate *pUp;
230675 SessionUpdate *pNext;
230676 for(pUp=p->pUp; pUp; pUp=pNext){
230677 pNext = pUp->pNext;
230678 sqlite3_finalize(pUp->pStmt);
230679 sqlite3_free(pUp);
230680 }
230681 p->pUp = 0;
230682 sqlite3_free(p->aUpdateMask);
230683 p->aUpdateMask = 0;
230684}
230685
230686/*
230687** Formulate a statement to DELETE a row from database db. Assuming a table
230688** structure like this:
230689**
230690** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
230691**
230692** The DELETE statement looks like this:
230693**
230694** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
230695**
230696** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
230697** matching b and d values, or 1 otherwise. The second case comes up if the
230698** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
230699**
230700** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
230701** pointing to the prepared version of the SQL statement.
230702*/
230703static int sessionDeleteRow(
230704 sqlite3 *db, /* Database handle */
230705 const char *zTab, /* Table name */
230706 SessionApplyCtx *p /* Session changeset-apply context */
230707){
230708 int i;
230709 const char *zSep = "";
230710 int rc = SQLITE_OK0;
230711 SessionBuffer buf = {0, 0, 0};
230712 int nPk = 0;
230713
230714 sessionAppendStr(&buf, "DELETE FROM main.", &rc);
230715 sessionAppendIdent(&buf, zTab, &rc);
230716 sessionAppendStr(&buf, " WHERE ", &rc);
230717
230718 for(i=0; i<p->nCol; i++){
230719 if( p->abPK[i] ){
230720 nPk++;
230721 sessionAppendStr(&buf, zSep, &rc);
230722 sessionAppendIdent(&buf, p->azCol[i], &rc);
230723 sessionAppendStr(&buf, " = ?", &rc);
230724 sessionAppendInteger(&buf, i+1, &rc);
230725 zSep = " AND ";
230726 }
230727 }
230728
230729 if( nPk<p->nCol ){
230730 sessionAppendStr(&buf, " AND (?", &rc);
230731 sessionAppendInteger(&buf, p->nCol+1, &rc);
230732 sessionAppendStr(&buf, " OR ", &rc);
230733
230734 zSep = "";
230735 for(i=0; i<p->nCol; i++){
230736 if( !p->abPK[i] ){
230737 sessionAppendStr(&buf, zSep, &rc);
230738 sessionAppendIdent(&buf, p->azCol[i], &rc);
230739 sessionAppendStr(&buf, " IS ?", &rc);
230740 sessionAppendInteger(&buf, i+1, &rc);
230741 zSep = "AND ";
230742 }
230743 }
230744 sessionAppendStr(&buf, ")", &rc);
230745 }
230746
230747 if( rc==SQLITE_OK0 ){
230748 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
230749 }
230750 sqlite3_free(buf.aBuf);
230751
230752 return rc;
230753}
230754
230755/*
230756** Formulate and prepare an SQL statement to query table zTab by primary
230757** key. Assuming the following table structure:
230758**
230759** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
230760**
230761** The SELECT statement looks like this:
230762**
230763** SELECT * FROM x WHERE a = ?1 AND c = ?3
230764**
230765** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
230766** pointing to the prepared version of the SQL statement.
230767*/
230768static int sessionSelectRow(
230769 sqlite3 *db, /* Database handle */
230770 const char *zTab, /* Table name */
230771 SessionApplyCtx *p /* Session changeset-apply context */
230772){
230773 /* TODO */
230774 return sessionSelectStmt(db, p->bIgnoreNoop,
230775 "main", zTab, p->bRowid, p->nCol, p->azCol, p->abPK, &p->pSelect
230776 );
230777}
230778
230779/*
230780** Formulate and prepare an INSERT statement to add a record to table zTab.
230781** For example:
230782**
230783** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
230784**
230785** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
230786** pointing to the prepared version of the SQL statement.
230787*/
230788static int sessionInsertRow(
230789 sqlite3 *db, /* Database handle */
230790 const char *zTab, /* Table name */
230791 SessionApplyCtx *p /* Session changeset-apply context */
230792){
230793 int rc = SQLITE_OK0;
230794 int i;
230795 SessionBuffer buf = {0, 0, 0};
230796
230797 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
230798 sessionAppendIdent(&buf, zTab, &rc);
230799 sessionAppendStr(&buf, "(", &rc);
230800 for(i=0; i<p->nCol; i++){
230801 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
230802 sessionAppendIdent(&buf, p->azCol[i], &rc);
230803 }
230804
230805 sessionAppendStr(&buf, ") VALUES(?", &rc);
230806 for(i=1; i<p->nCol; i++){
230807 sessionAppendStr(&buf, ", ?", &rc);
230808 }
230809 sessionAppendStr(&buf, ")", &rc);
230810
230811 if( rc==SQLITE_OK0 ){
230812 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
230813 }
230814 sqlite3_free(buf.aBuf);
230815 return rc;
230816}
230817
230818static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
230819 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
230820}
230821
230822/*
230823** Prepare statements for applying changes to the sqlite_stat1 table.
230824** These are similar to those created by sessionSelectRow(),
230825** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
230826** other tables.
230827*/
230828static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
230829 int rc = sessionSelectRow(db, "sqlite_stat1", p);
230830 if( rc==SQLITE_OK0 ){
230831 rc = sessionPrepare(db, &p->pInsert,
230832 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
230833 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
230834 "?3)"
230835 );
230836 }
230837 if( rc==SQLITE_OK0 ){
230838 rc = sessionPrepare(db, &p->pDelete,
230839 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
230840 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
230841 "AND (?4 OR stat IS ?3)"
230842 );
230843 }
230844 return rc;
230845}
230846
230847/*
230848** A wrapper around sqlite3_bind_value() that detects an extra problem.
230849** See comments in the body of this function for details.
230850*/
230851static int sessionBindValue(
230852 sqlite3_stmt *pStmt, /* Statement to bind value to */
230853 int i, /* Parameter number to bind to */
230854 sqlite3_value *pVal /* Value to bind */
230855){
230856 int eType = sqlite3_value_type(pVal);
230857 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
230858 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
230859 ** the (pVal->z) variable remains as it was or the type of the value is
230860 ** set to SQLITE_NULL. */
230861 if( (eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4) && pVal->z==0 ){
230862 /* This condition occurs when an earlier OOM in a call to
230863 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
230864 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
230865 return SQLITE_NOMEM7;
230866 }
230867 return sqlite3_bind_value(pStmt, i, pVal);
230868}
230869
230870/*
230871** Iterator pIter must point to an SQLITE_INSERT entry. This function
230872** transfers new.* values from the current iterator entry to statement
230873** pStmt. The table being inserted into has nCol columns.
230874**
230875** New.* value $i from the iterator is bound to variable ($i+1) of
230876** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
230877** are transfered to the statement. Otherwise, if abPK is not NULL, it points
230878** to an array nCol elements in size. In this case only those values for
230879** which abPK[$i] is true are read from the iterator and bound to the
230880** statement.
230881**
230882** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
230883*/
230884static int sessionBindRow(
230885 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
230886 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
230887 int nCol, /* Number of columns */
230888 u8 *abPK, /* If not NULL, bind only if true */
230889 sqlite3_stmt *pStmt /* Bind values to this statement */
230890){
230891 int i;
230892 int rc = SQLITE_OK0;
230893
230894 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
230895 ** argument iterator points to a suitable entry. Make sure that xValue
230896 ** is one of these to guarantee that it is safe to ignore the return
230897 ** in the code below. */
230898 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new )((void) (0));
230899
230900 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
230901 if( !abPK || abPK[i] ){
230902 sqlite3_value *pVal = 0;
230903 (void)xValue(pIter, i, &pVal);
230904 if( pVal==0 ){
230905 /* The value in the changeset was "undefined". This indicates a
230906 ** corrupt changeset blob. */
230907 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(230907);
230908 }else{
230909 rc = sessionBindValue(pStmt, i+1, pVal);
230910 }
230911 }
230912 }
230913 return rc;
230914}
230915
230916/*
230917** SQL statement pSelect is as generated by the sessionSelectRow() function.
230918** This function binds the primary key values from the change that changeset
230919** iterator pIter points to to the SELECT and attempts to seek to the table
230920** entry. If a row is found, the SELECT statement left pointing at the row
230921** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
230922** has occured, the statement is reset and SQLITE_OK is returned. If an
230923** error occurs, the statement is reset and an SQLite error code is returned.
230924**
230925** If this function returns SQLITE_ROW, the caller must eventually reset()
230926** statement pSelect. If any other value is returned, the statement does
230927** not require a reset().
230928**
230929** If the iterator currently points to an INSERT record, bind values from the
230930** new.* record to the SELECT statement. Or, if it points to a DELETE or
230931** UPDATE, bind values from the old.* record.
230932*/
230933static int sessionSeekToRow(
230934 sqlite3_changeset_iter *pIter, /* Changeset iterator */
230935 SessionApplyCtx *p
230936){
230937 sqlite3_stmt *pSelect = p->pSelect;
230938 int rc; /* Return code */
230939 int nCol; /* Number of columns in table */
230940 int op; /* Changset operation (SQLITE_UPDATE etc.) */
230941 const char *zDummy; /* Unused */
230942
230943 sqlite3_clear_bindings(pSelect);
230944 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
230945 rc = sessionBindRow(pIter,
230946 op==SQLITE_INSERT18 ? sqlite3changeset_new : sqlite3changeset_old,
230947 nCol, p->abPK, pSelect
230948 );
230949
230950 if( op!=SQLITE_DELETE9 && p->bIgnoreNoop ){
230951 int ii;
230952 for(ii=0; rc==SQLITE_OK0 && ii<nCol; ii++){
230953 if( p->abPK[ii]==0 ){
230954 sqlite3_value *pVal = 0;
230955 sqlite3changeset_new(pIter, ii, &pVal);
230956 sqlite3_bind_int(pSelect, ii+1+nCol, (pVal==0));
230957 if( pVal ) rc = sessionBindValue(pSelect, ii+1, pVal);
230958 }
230959 }
230960 }
230961
230962 if( rc==SQLITE_OK0 ){
230963 rc = sqlite3_step(pSelect);
230964 if( rc!=SQLITE_ROW100 ) rc = sqlite3_reset(pSelect);
230965 }
230966
230967 return rc;
230968}
230969
230970/*
230971** This function is called from within sqlite3changeset_apply_v2() when
230972** a conflict is encountered and resolved using conflict resolution
230973** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
230974** It adds a conflict resolution record to the buffer in
230975** SessionApplyCtx.rebase, which will eventually be returned to the caller
230976** of apply_v2() as the "rebase" buffer.
230977**
230978** Return SQLITE_OK if successful, or an SQLite error code otherwise.
230979*/
230980static int sessionRebaseAdd(
230981 SessionApplyCtx *p, /* Apply context */
230982 int eType, /* Conflict resolution (OMIT or REPLACE) */
230983 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
230984){
230985 int rc = SQLITE_OK0;
230986 if( p->bRebase ){
230987 int i;
230988 int eOp = pIter->op;
230989 if( p->bRebaseStarted==0 ){
230990 /* Append a table-header to the rebase buffer */
230991 const char *zTab = pIter->zTab;
230992 sessionAppendByte(&p->rebase, 'T', &rc);
230993 sessionAppendVarint(&p->rebase, p->nCol, &rc);
230994 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
230995 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
230996 p->bRebaseStarted = 1;
230997 }
230998
230999 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT )((void) (0));
231000 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE )((void) (0));
231001
231002 sessionAppendByte(&p->rebase,
231003 (eOp==SQLITE_DELETE9 ? SQLITE_DELETE9 : SQLITE_INSERT18), &rc
231004 );
231005 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE1), &rc);
231006 for(i=0; i<p->nCol; i++){
231007 sqlite3_value *pVal = 0;
231008 if( eOp==SQLITE_DELETE9 || (eOp==SQLITE_UPDATE23 && p->abPK[i]) ){
231009 sqlite3changeset_old(pIter, i, &pVal);
231010 }else{
231011 sqlite3changeset_new(pIter, i, &pVal);
231012 }
231013 sessionAppendValue(&p->rebase, pVal, &rc);
231014 }
231015 }
231016 return rc;
231017}
231018
231019/*
231020** Invoke the conflict handler for the change that the changeset iterator
231021** currently points to.
231022**
231023** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
231024** If argument pbReplace is NULL, then the type of conflict handler invoked
231025** depends solely on eType, as follows:
231026**
231027** eType value Value passed to xConflict
231028** -------------------------------------------------
231029** CHANGESET_DATA CHANGESET_NOTFOUND
231030** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
231031**
231032** Or, if pbReplace is not NULL, then an attempt is made to find an existing
231033** record with the same primary key as the record about to be deleted, updated
231034** or inserted. If such a record can be found, it is available to the conflict
231035** handler as the "conflicting" record. In this case the type of conflict
231036** handler invoked is as follows:
231037**
231038** eType value PK Record found? Value passed to xConflict
231039** ----------------------------------------------------------------
231040** CHANGESET_DATA Yes CHANGESET_DATA
231041** CHANGESET_DATA No CHANGESET_NOTFOUND
231042** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
231043** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
231044**
231045** If pbReplace is not NULL, and a record with a matching PK is found, and
231046** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
231047** is set to non-zero before returning SQLITE_OK.
231048**
231049** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
231050** returned. Or, if the conflict handler returns an invalid value,
231051** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
231052** this function returns SQLITE_OK.
231053*/
231054static int sessionConflictHandler(
231055 int eType, /* Either CHANGESET_DATA or CONFLICT */
231056 SessionApplyCtx *p, /* changeset_apply() context */
231057 sqlite3_changeset_iter *pIter, /* Changeset iterator */
231058 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
231059 void *pCtx, /* First argument for conflict handler */
231060 int *pbReplace /* OUT: Set to true if PK row is found */
231061){
231062 int res = 0; /* Value returned by conflict handler */
231063 int rc;
231064 int nCol;
231065 int op;
231066 const char *zDummy;
231067
231068 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
231069
231070 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA )((void) (0));
231071 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT )((void) (0));
231072 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND )((void) (0));
231073
231074 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
231075 if( pbReplace ){
231076 rc = sessionSeekToRow(pIter, p);
231077 }else{
231078 rc = SQLITE_OK0;
231079 }
231080
231081 if( rc==SQLITE_ROW100 ){
231082 /* There exists another row with the new.* primary key. */
231083 if( p->bIgnoreNoop
231084 && sqlite3_column_int(p->pSelect, sqlite3_column_count(p->pSelect)-1)
231085 ){
231086 res = SQLITE_CHANGESET_OMIT0;
231087 }else{
231088 pIter->pConflict = p->pSelect;
231089 res = xConflict(pCtx, eType, pIter);
231090 pIter->pConflict = 0;
231091 }
231092 rc = sqlite3_reset(p->pSelect);
231093 }else if( rc==SQLITE_OK0 ){
231094 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT3 ){
231095 /* Instead of invoking the conflict handler, append the change blob
231096 ** to the SessionApplyCtx.constraints buffer. */
231097 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
231098 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
231099 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
231100 return SQLITE_OK0;
231101 }else{
231102 /* No other row with the new.* primary key. */
231103 res = xConflict(pCtx, eType+1, pIter);
231104 if( res==SQLITE_CHANGESET_REPLACE1 ) rc = SQLITE_MISUSE21;
231105 }
231106 }
231107
231108 if( rc==SQLITE_OK0 ){
231109 switch( res ){
231110 case SQLITE_CHANGESET_REPLACE1:
231111 assert( pbReplace )((void) (0));
231112 *pbReplace = 1;
231113 break;
231114
231115 case SQLITE_CHANGESET_OMIT0:
231116 break;
231117
231118 case SQLITE_CHANGESET_ABORT2:
231119 rc = SQLITE_ABORT4;
231120 break;
231121
231122 default:
231123 rc = SQLITE_MISUSE21;
231124 break;
231125 }
231126 if( rc==SQLITE_OK0 ){
231127 rc = sessionRebaseAdd(p, res, pIter);
231128 }
231129 }
231130
231131 return rc;
231132}
231133
231134/*
231135** Attempt to apply the change that the iterator passed as the first argument
231136** currently points to to the database. If a conflict is encountered, invoke
231137** the conflict handler callback.
231138**
231139** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
231140** one is encountered, update or delete the row with the matching primary key
231141** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
231142** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
231143** to true before returning. In this case the caller will invoke this function
231144** again, this time with pbRetry set to NULL.
231145**
231146** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
231147** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
231148** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
231149** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
231150** before retrying. In this case the caller attempts to remove the conflicting
231151** row before invoking this function again, this time with pbReplace set
231152** to NULL.
231153**
231154** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
231155** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
231156** returned.
231157*/
231158static int sessionApplyOneOp(
231159 sqlite3_changeset_iter *pIter, /* Changeset iterator */
231160 SessionApplyCtx *p, /* changeset_apply() context */
231161 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
231162 void *pCtx, /* First argument for the conflict handler */
231163 int *pbReplace, /* OUT: True to remove PK row and retry */
231164 int *pbRetry /* OUT: True to retry. */
231165){
231166 const char *zDummy;
231167 int op;
231168 int nCol;
231169 int rc = SQLITE_OK0;
231170
231171 assert( p->pDelete && p->pInsert && p->pSelect )((void) (0));
231172 assert( p->azCol && p->abPK )((void) (0));
231173 assert( !pbReplace || *pbReplace==0 )((void) (0));
231174
231175 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
231176
231177 if( op==SQLITE_DELETE9 ){
231178
231179 /* Bind values to the DELETE statement. If conflict handling is required,
231180 ** bind values for all columns and set bound variable (nCol+1) to true.
231181 ** Or, if conflict handling is not required, bind just the PK column
231182 ** values and, if it exists, set (nCol+1) to false. Conflict handling
231183 ** is not required if:
231184 **
231185 ** * this is a patchset, or
231186 ** * (pbRetry==0), or
231187 ** * all columns of the table are PK columns (in this case there is
231188 ** no (nCol+1) variable to bind to).
231189 */
231190 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
231191 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
231192 if( rc==SQLITE_OK0 && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
231193 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
231194 }
231195 if( rc!=SQLITE_OK0 ) return rc;
231196
231197 sqlite3_step(p->pDelete);
231198 rc = sqlite3_reset(p->pDelete);
231199 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 && p->bIgnoreNoop==0 ){
231200 rc = sessionConflictHandler(
231201 SQLITE_CHANGESET_DATA1, p, pIter, xConflict, pCtx, pbRetry
231202 );
231203 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
231204 rc = sessionConflictHandler(
231205 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, 0
231206 );
231207 }
231208
231209 }else if( op==SQLITE_UPDATE23 ){
231210 int i;
231211 sqlite3_stmt *pUp = 0;
231212 int bPatchset = (pbRetry==0 || pIter->bPatchset);
231213
231214 rc = sessionUpdateFind(pIter, p, bPatchset, &pUp);
231215
231216 /* Bind values to the UPDATE statement. */
231217 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
231218 sqlite3_value *pOld = sessionChangesetOld(pIter, i)(pIter)->apValue[(i)];
231219 sqlite3_value *pNew = sessionChangesetNew(pIter, i)(pIter)->apValue[(pIter)->nCol+(i)];
231220 if( p->abPK[i] || (bPatchset==0 && pOld) ){
231221 rc = sessionBindValue(pUp, i*2+2, pOld);
231222 }
231223 if( rc==SQLITE_OK0 && pNew ){
231224 rc = sessionBindValue(pUp, i*2+1, pNew);
231225 }
231226 }
231227 if( rc!=SQLITE_OK0 ) return rc;
231228
231229 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
231230 ** the result will be SQLITE_OK with 0 rows modified. */
231231 sqlite3_step(pUp);
231232 rc = sqlite3_reset(pUp);
231233
231234 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 ){
231235 /* A NOTFOUND or DATA error. Search the table to see if it contains
231236 ** a row with a matching primary key. If so, this is a DATA conflict.
231237 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
231238
231239 rc = sessionConflictHandler(
231240 SQLITE_CHANGESET_DATA1, p, pIter, xConflict, pCtx, pbRetry
231241 );
231242
231243 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
231244 /* This is always a CONSTRAINT conflict. */
231245 rc = sessionConflictHandler(
231246 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, 0
231247 );
231248 }
231249
231250 }else{
231251 assert( op==SQLITE_INSERT )((void) (0));
231252 if( p->bStat1 ){
231253 /* Check if there is a conflicting row. For sqlite_stat1, this needs
231254 ** to be done using a SELECT, as there is no PRIMARY KEY in the
231255 ** database schema to throw an exception if a duplicate is inserted. */
231256 rc = sessionSeekToRow(pIter, p);
231257 if( rc==SQLITE_ROW100 ){
231258 rc = SQLITE_CONSTRAINT19;
231259 sqlite3_reset(p->pSelect);
231260 }
231261 }
231262
231263 if( rc==SQLITE_OK0 ){
231264 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
231265 if( rc!=SQLITE_OK0 ) return rc;
231266
231267 sqlite3_step(p->pInsert);
231268 rc = sqlite3_reset(p->pInsert);
231269 }
231270
231271 if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
231272 rc = sessionConflictHandler(
231273 SQLITE_CHANGESET_CONFLICT3, p, pIter, xConflict, pCtx, pbReplace
231274 );
231275 }
231276 }
231277
231278 return rc;
231279}
231280
231281/*
231282** Attempt to apply the change that the iterator passed as the first argument
231283** currently points to to the database. If a conflict is encountered, invoke
231284** the conflict handler callback.
231285**
231286** The difference between this function and sessionApplyOne() is that this
231287** function handles the case where the conflict-handler is invoked and
231288** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
231289** retried in some manner.
231290*/
231291static int sessionApplyOneWithRetry(
231292 sqlite3 *db, /* Apply change to "main" db of this handle */
231293 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
231294 SessionApplyCtx *pApply, /* Apply context */
231295 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
231296 void *pCtx /* First argument passed to xConflict */
231297){
231298 int bReplace = 0;
231299 int bRetry = 0;
231300 int rc;
231301
231302 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
231303 if( rc==SQLITE_OK0 ){
231304 /* If the bRetry flag is set, the change has not been applied due to an
231305 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
231306 ** a row with the correct PK is present in the db, but one or more other
231307 ** fields do not contain the expected values) and the conflict handler
231308 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
231309 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
231310 ** the SQLITE_CHANGESET_DATA problem. */
231311 if( bRetry ){
231312 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE )((void) (0));
231313 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
231314 }
231315
231316 /* If the bReplace flag is set, the change is an INSERT that has not
231317 ** been performed because the database already contains a row with the
231318 ** specified primary key and the conflict handler returned
231319 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
231320 ** before reattempting the INSERT. */
231321 else if( bReplace ){
231322 assert( pIter->op==SQLITE_INSERT )((void) (0));
231323 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
231324 if( rc==SQLITE_OK0 ){
231325 rc = sessionBindRow(pIter,
231326 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
231327 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
231328 }
231329 if( rc==SQLITE_OK0 ){
231330 sqlite3_step(pApply->pDelete);
231331 rc = sqlite3_reset(pApply->pDelete);
231332 }
231333 if( rc==SQLITE_OK0 ){
231334 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
231335 }
231336 if( rc==SQLITE_OK0 ){
231337 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
231338 }
231339 }
231340 }
231341
231342 return rc;
231343}
231344
231345/*
231346** Retry the changes accumulated in the pApply->constraints buffer.
231347*/
231348static int sessionRetryConstraints(
231349 sqlite3 *db,
231350 int bPatchset,
231351 const char *zTab,
231352 SessionApplyCtx *pApply,
231353 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
231354 void *pCtx /* First argument passed to xConflict */
231355){
231356 int rc = SQLITE_OK0;
231357
231358 while( pApply->constraints.nBuf ){
231359 sqlite3_changeset_iter *pIter2 = 0;
231360 SessionBuffer cons = pApply->constraints;
231361 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
231362
231363 rc = sessionChangesetStart(
231364 &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1
231365 );
231366 if( rc==SQLITE_OK0 ){
231367 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
231368 int rc2;
231369 pIter2->bPatchset = bPatchset;
231370 pIter2->zTab = (char*)zTab;
231371 pIter2->nCol = pApply->nCol;
231372 pIter2->abPK = pApply->abPK;
231373 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
231374 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
231375 if( rc==SQLITE_OK0 ) memset(pIter2->apValue, 0, nByte);
231376
231377 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter2) ){
231378 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
231379 }
231380
231381 rc2 = sqlite3changeset_finalize(pIter2);
231382 if( rc==SQLITE_OK0 ) rc = rc2;
231383 }
231384 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 )((void) (0));
231385
231386 sqlite3_free(cons.aBuf);
231387 if( rc!=SQLITE_OK0 ) break;
231388 if( pApply->constraints.nBuf>=cons.nBuf ){
231389 /* No progress was made on the last round. */
231390 pApply->bDeferConstraints = 0;
231391 }
231392 }
231393
231394 return rc;
231395}
231396
231397/*
231398** Argument pIter is a changeset iterator that has been initialized, but
231399** not yet passed to sqlite3changeset_next(). This function applies the
231400** changeset to the main database attached to handle "db". The supplied
231401** conflict handler callback is invoked to resolve any conflicts encountered
231402** while applying the change.
231403*/
231404static int sessionChangesetApply(
231405 sqlite3 *db, /* Apply change to "main" db of this handle */
231406 sqlite3_changeset_iter *pIter, /* Changeset to apply */
231407 int(*xFilter)(
231408 void *pCtx, /* Copy of sixth arg to _apply() */
231409 const char *zTab /* Table name */
231410 ),
231411 int(*xConflict)(
231412 void *pCtx, /* Copy of fifth arg to _apply() */
231413 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
231414 sqlite3_changeset_iter *p /* Handle describing change and conflict */
231415 ),
231416 void *pCtx, /* First argument passed to xConflict */
231417 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
231418 int flags /* SESSION_APPLY_XXX flags */
231419){
231420 int schemaMismatch = 0;
231421 int rc = SQLITE_OK0; /* Return code */
231422 const char *zTab = 0; /* Name of current table */
231423 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
231424 SessionApplyCtx sApply; /* changeset_apply() context object */
231425 int bPatchset;
231426 u64 savedFlag = db->flags & SQLITE_FkNoAction((u64)(0x00008)<<32);
231427
231428 assert( xConflict!=0 )((void) (0));
231429
231430 sqlite3_mutex_enter(sqlite3_db_mutex(db));
231431 if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION0x0008 ){
231432 db->flags |= ((u64)SQLITE_FkNoAction((u64)(0x00008)<<32));
231433 db->aDb[0].pSchema->schema_cookie -= 32;
231434 }
231435
231436 pIter->in.bNoDiscard = 1;
231437 memset(&sApply, 0, sizeof(sApply));
231438 sApply.bRebase = (ppRebase && pnRebase);
231439 sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
231440 sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP0x0004);
231441 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001)==0 ){
231442 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
231443 }
231444 if( rc==SQLITE_OK0 ){
231445 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
231446 }
231447 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter) ){
231448 int nCol;
231449 int op;
231450 const char *zNew;
231451
231452 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
231453
231454 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
231455 u8 *abPK;
231456
231457 rc = sessionRetryConstraints(
231458 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
231459 );
231460 if( rc!=SQLITE_OK0 ) break;
231461
231462 sessionUpdateFree(&sApply);
231463 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
231464 sqlite3_finalize(sApply.pDelete);
231465 sqlite3_finalize(sApply.pInsert);
231466 sqlite3_finalize(sApply.pSelect);
231467 sApply.db = db;
231468 sApply.pDelete = 0;
231469 sApply.pInsert = 0;
231470 sApply.pSelect = 0;
231471 sApply.nCol = 0;
231472 sApply.azCol = 0;
231473 sApply.abPK = 0;
231474 sApply.bStat1 = 0;
231475 sApply.bDeferConstraints = 1;
231476 sApply.bRebaseStarted = 0;
231477 sApply.bRowid = 0;
231478 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
231479
231480 /* If an xFilter() callback was specified, invoke it now. If the
231481 ** xFilter callback returns zero, skip this table. If it returns
231482 ** non-zero, proceed. */
231483 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
231484 if( schemaMismatch ){
231485 zTab = sqlite3_mprintf("%s", zNew);
231486 if( zTab==0 ){
231487 rc = SQLITE_NOMEM7;
231488 break;
231489 }
231490 nTab = (int)strlen(zTab);
231491 sApply.azCol = (const char **)zTab;
231492 }else{
231493 int nMinCol = 0;
231494 int i;
231495
231496 sqlite3changeset_pk(pIter, &abPK, 0);
231497 rc = sessionTableInfo(0, db, "main", zNew,
231498 &sApply.nCol, &zTab, &sApply.azCol, 0, &sApply.abPK, &sApply.bRowid
231499 );
231500 if( rc!=SQLITE_OK0 ) break;
231501 for(i=0; i<sApply.nCol; i++){
231502 if( sApply.abPK[i] ) nMinCol = i+1;
231503 }
231504
231505 if( sApply.nCol==0 ){
231506 schemaMismatch = 1;
231507 sqlite3_log(SQLITE_SCHEMA17,
231508 "sqlite3changeset_apply(): no such table: %s", zTab
231509 );
231510 }
231511 else if( sApply.nCol<nCol ){
231512 schemaMismatch = 1;
231513 sqlite3_log(SQLITE_SCHEMA17,
231514 "sqlite3changeset_apply(): table %s has %d columns, "
231515 "expected %d or more",
231516 zTab, sApply.nCol, nCol
231517 );
231518 }
231519 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
231520 schemaMismatch = 1;
231521 sqlite3_log(SQLITE_SCHEMA17, "sqlite3changeset_apply(): "
231522 "primary key mismatch for table %s", zTab
231523 );
231524 }
231525 else{
231526 sApply.nCol = nCol;
231527 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
231528 if( (rc = sessionStat1Sql(db, &sApply) ) ){
231529 break;
231530 }
231531 sApply.bStat1 = 1;
231532 }else{
231533 if( (rc = sessionSelectRow(db, zTab, &sApply))
231534 || (rc = sessionDeleteRow(db, zTab, &sApply))
231535 || (rc = sessionInsertRow(db, zTab, &sApply))
231536 ){
231537 break;
231538 }
231539 sApply.bStat1 = 0;
231540 }
231541 }
231542 nTab = sqlite3Strlen30(zTab);
231543 }
231544 }
231545
231546 /* If there is a schema mismatch on the current table, proceed to the
231547 ** next change. A log message has already been issued. */
231548 if( schemaMismatch ) continue;
231549
231550 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
231551 }
231552
231553 bPatchset = pIter->bPatchset;
231554 if( rc==SQLITE_OK0 ){
231555 rc = sqlite3changeset_finalize(pIter);
231556 }else{
231557 sqlite3changeset_finalize(pIter);
231558 }
231559
231560 if( rc==SQLITE_OK0 ){
231561 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
231562 }
231563
231564 if( rc==SQLITE_OK0 ){
231565 int nFk, notUsed;
231566 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS10, &nFk, &notUsed, 0);
231567 if( nFk!=0 ){
231568 int res = SQLITE_CHANGESET_ABORT2;
231569 sqlite3_changeset_iter sIter;
231570 memset(&sIter, 0, sizeof(sIter));
231571 sIter.nCol = nFk;
231572 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY5, &sIter);
231573 if( res!=SQLITE_CHANGESET_OMIT0 ){
231574 rc = SQLITE_CONSTRAINT19;
231575 }
231576 }
231577 }
231578 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
231579
231580 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT0x0001)==0 ){
231581 if( rc==SQLITE_OK0 ){
231582 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
231583 }else{
231584 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
231585 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
231586 }
231587 }
231588
231589 assert( sApply.bRebase || sApply.rebase.nBuf==0 )((void) (0));
231590 if( rc==SQLITE_OK0 && bPatchset==0 && sApply.bRebase ){
231591 *ppRebase = (void*)sApply.rebase.aBuf;
231592 *pnRebase = sApply.rebase.nBuf;
231593 sApply.rebase.aBuf = 0;
231594 }
231595 sessionUpdateFree(&sApply);
231596 sqlite3_finalize(sApply.pInsert);
231597 sqlite3_finalize(sApply.pDelete);
231598 sqlite3_finalize(sApply.pSelect);
231599 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
231600 sqlite3_free((char*)sApply.constraints.aBuf);
231601 sqlite3_free((char*)sApply.rebase.aBuf);
231602
231603 if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION0x0008) && savedFlag==0 ){
231604 assert( db->flags & SQLITE_FkNoAction )((void) (0));
231605 db->flags &= ~((u64)SQLITE_FkNoAction((u64)(0x00008)<<32));
231606 db->aDb[0].pSchema->schema_cookie -= 32;
231607 }
231608 sqlite3_mutex_leave(sqlite3_db_mutex(db));
231609 return rc;
231610}
231611
231612/*
231613** Apply the changeset passed via pChangeset/nChangeset to the main
231614** database attached to handle "db".
231615*/
231616SQLITE_API int sqlite3changeset_apply_v2(
231617 sqlite3 *db, /* Apply change to "main" db of this handle */
231618 int nChangeset, /* Size of changeset in bytes */
231619 void *pChangeset, /* Changeset blob */
231620 int(*xFilter)(
231621 void *pCtx, /* Copy of sixth arg to _apply() */
231622 const char *zTab /* Table name */
231623 ),
231624 int(*xConflict)(
231625 void *pCtx, /* Copy of sixth arg to _apply() */
231626 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
231627 sqlite3_changeset_iter *p /* Handle describing change and conflict */
231628 ),
231629 void *pCtx, /* First argument passed to xConflict */
231630 void **ppRebase, int *pnRebase,
231631 int flags
231632){
231633 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
231634 int bInv = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
231635 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset, bInv, 1);
231636
231637 if( rc==SQLITE_OK0 ){
231638 rc = sessionChangesetApply(
231639 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
231640 );
231641 }
231642
231643 return rc;
231644}
231645
231646/*
231647** Apply the changeset passed via pChangeset/nChangeset to the main database
231648** attached to handle "db". Invoke the supplied conflict handler callback
231649** to resolve any conflicts encountered while applying the change.
231650*/
231651SQLITE_API int sqlite3changeset_apply(
231652 sqlite3 *db, /* Apply change to "main" db of this handle */
231653 int nChangeset, /* Size of changeset in bytes */
231654 void *pChangeset, /* Changeset blob */
231655 int(*xFilter)(
231656 void *pCtx, /* Copy of sixth arg to _apply() */
231657 const char *zTab /* Table name */
231658 ),
231659 int(*xConflict)(
231660 void *pCtx, /* Copy of fifth arg to _apply() */
231661 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
231662 sqlite3_changeset_iter *p /* Handle describing change and conflict */
231663 ),
231664 void *pCtx /* First argument passed to xConflict */
231665){
231666 return sqlite3changeset_apply_v2(
231667 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
231668 );
231669}
231670
231671/*
231672** Apply the changeset passed via xInput/pIn to the main database
231673** attached to handle "db". Invoke the supplied conflict handler callback
231674** to resolve any conflicts encountered while applying the change.
231675*/
231676SQLITE_API int sqlite3changeset_apply_v2_strm(
231677 sqlite3 *db, /* Apply change to "main" db of this handle */
231678 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
231679 void *pIn, /* First arg for xInput */
231680 int(*xFilter)(
231681 void *pCtx, /* Copy of sixth arg to _apply() */
231682 const char *zTab /* Table name */
231683 ),
231684 int(*xConflict)(
231685 void *pCtx, /* Copy of sixth arg to _apply() */
231686 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
231687 sqlite3_changeset_iter *p /* Handle describing change and conflict */
231688 ),
231689 void *pCtx, /* First argument passed to xConflict */
231690 void **ppRebase, int *pnRebase,
231691 int flags
231692){
231693 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
231694 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT0x0002);
231695 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse, 1);
231696 if( rc==SQLITE_OK0 ){
231697 rc = sessionChangesetApply(
231698 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
231699 );
231700 }
231701 return rc;
231702}
231703SQLITE_API int sqlite3changeset_apply_strm(
231704 sqlite3 *db, /* Apply change to "main" db of this handle */
231705 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
231706 void *pIn, /* First arg for xInput */
231707 int(*xFilter)(
231708 void *pCtx, /* Copy of sixth arg to _apply() */
231709 const char *zTab /* Table name */
231710 ),
231711 int(*xConflict)(
231712 void *pCtx, /* Copy of sixth arg to _apply() */
231713 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
231714 sqlite3_changeset_iter *p /* Handle describing change and conflict */
231715 ),
231716 void *pCtx /* First argument passed to xConflict */
231717){
231718 return sqlite3changeset_apply_v2_strm(
231719 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
231720 );
231721}
231722
231723/*
231724** sqlite3_changegroup handle.
231725*/
231726struct sqlite3_changegroup {
231727 int rc; /* Error code */
231728 int bPatch; /* True to accumulate patchsets */
231729 SessionTable *pList; /* List of tables in current patch */
231730 SessionBuffer rec;
231731
231732 sqlite3 *db; /* Configured by changegroup_schema() */
231733 char *zDb; /* Configured by changegroup_schema() */
231734};
231735
231736/*
231737** This function is called to merge two changes to the same row together as
231738** part of an sqlite3changeset_concat() operation. A new change object is
231739** allocated and a pointer to it stored in *ppNew.
231740*/
231741static int sessionChangeMerge(
231742 SessionTable *pTab, /* Table structure */
231743 int bRebase, /* True for a rebase hash-table */
231744 int bPatchset, /* True for patchsets */
231745 SessionChange *pExist, /* Existing change */
231746 int op2, /* Second change operation */
231747 int bIndirect, /* True if second change is indirect */
231748 u8 *aRec, /* Second change record */
231749 int nRec, /* Number of bytes in aRec */
231750 SessionChange **ppNew /* OUT: Merged change */
231751){
231752 SessionChange *pNew = 0;
231753 int rc = SQLITE_OK0;
231754 assert( aRec!=0 )((void) (0));
231755
231756 if( !pExist ){
231757 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
231758 if( !pNew ){
231759 return SQLITE_NOMEM7;
231760 }
231761 memset(pNew, 0, sizeof(SessionChange));
231762 pNew->op = op2;
231763 pNew->bIndirect = bIndirect;
231764 pNew->aRecord = (u8*)&pNew[1];
231765 if( bIndirect==0 || bRebase==0 ){
231766 pNew->nRecord = nRec;
231767 memcpy(pNew->aRecord, aRec, nRec);
231768 }else{
231769 int i;
231770 u8 *pIn = aRec;
231771 u8 *pOut = pNew->aRecord;
231772 for(i=0; i<pTab->nCol; i++){
231773 int nIn = sessionSerialLen(pIn);
231774 if( *pIn==0 ){
231775 *pOut++ = 0;
231776 }else if( pTab->abPK[i]==0 ){
231777 *pOut++ = 0xFF;
231778 }else{
231779 memcpy(pOut, pIn, nIn);
231780 pOut += nIn;
231781 }
231782 pIn += nIn;
231783 }
231784 pNew->nRecord = pOut - pNew->aRecord;
231785 }
231786 }else if( bRebase ){
231787 if( pExist->op==SQLITE_DELETE9 && pExist->bIndirect ){
231788 *ppNew = pExist;
231789 }else{
231790 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
231791 pNew = (SessionChange*)sqlite3_malloc64(nByte);
231792 if( pNew==0 ){
231793 rc = SQLITE_NOMEM7;
231794 }else{
231795 int i;
231796 u8 *a1 = pExist->aRecord;
231797 u8 *a2 = aRec;
231798 u8 *pOut;
231799
231800 memset(pNew, 0, nByte);
231801 pNew->bIndirect = bIndirect || pExist->bIndirect;
231802 pNew->op = op2;
231803 pOut = pNew->aRecord = (u8*)&pNew[1];
231804
231805 for(i=0; i<pTab->nCol; i++){
231806 int n1 = sessionSerialLen(a1);
231807 int n2 = sessionSerialLen(a2);
231808 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
231809 *pOut++ = 0xFF;
231810 }else if( *a2==0 ){
231811 memcpy(pOut, a1, n1);
231812 pOut += n1;
231813 }else{
231814 memcpy(pOut, a2, n2);
231815 pOut += n2;
231816 }
231817 a1 += n1;
231818 a2 += n2;
231819 }
231820 pNew->nRecord = pOut - pNew->aRecord;
231821 }
231822 sqlite3_free(pExist);
231823 }
231824 }else{
231825 int op1 = pExist->op;
231826
231827 /*
231828 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
231829 ** op1=INSERT, op2=UPDATE -> INSERT.
231830 ** op1=INSERT, op2=DELETE -> (none)
231831 **
231832 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
231833 ** op1=UPDATE, op2=UPDATE -> UPDATE.
231834 ** op1=UPDATE, op2=DELETE -> DELETE.
231835 **
231836 ** op1=DELETE, op2=INSERT -> UPDATE.
231837 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
231838 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
231839 */
231840 if( (op1==SQLITE_INSERT18 && op2==SQLITE_INSERT18)
231841 || (op1==SQLITE_UPDATE23 && op2==SQLITE_INSERT18)
231842 || (op1==SQLITE_DELETE9 && op2==SQLITE_UPDATE23)
231843 || (op1==SQLITE_DELETE9 && op2==SQLITE_DELETE9)
231844 ){
231845 pNew = pExist;
231846 }else if( op1==SQLITE_INSERT18 && op2==SQLITE_DELETE9 ){
231847 sqlite3_free(pExist);
231848 assert( pNew==0 )((void) (0));
231849 }else{
231850 u8 *aExist = pExist->aRecord;
231851 sqlite3_int64 nByte;
231852 u8 *aCsr;
231853
231854 /* Allocate a new SessionChange object. Ensure that the aRecord[]
231855 ** buffer of the new object is large enough to hold any record that
231856 ** may be generated by combining the input records. */
231857 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
231858 pNew = (SessionChange *)sqlite3_malloc64(nByte);
231859 if( !pNew ){
231860 sqlite3_free(pExist);
231861 return SQLITE_NOMEM7;
231862 }
231863 memset(pNew, 0, sizeof(SessionChange));
231864 pNew->bIndirect = (bIndirect && pExist->bIndirect);
231865 aCsr = pNew->aRecord = (u8 *)&pNew[1];
231866
231867 if( op1==SQLITE_INSERT18 ){ /* INSERT + UPDATE */
231868 u8 *a1 = aRec;
231869 assert( op2==SQLITE_UPDATE )((void) (0));
231870 pNew->op = SQLITE_INSERT18;
231871 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
231872 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
231873 }else if( op1==SQLITE_DELETE9 ){ /* DELETE + INSERT */
231874 assert( op2==SQLITE_INSERT )((void) (0));
231875 pNew->op = SQLITE_UPDATE23;
231876 if( bPatchset ){
231877 memcpy(aCsr, aRec, nRec);
231878 aCsr += nRec;
231879 }else{
231880 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
231881 sqlite3_free(pNew);
231882 pNew = 0;
231883 }
231884 }
231885 }else if( op2==SQLITE_UPDATE23 ){ /* UPDATE + UPDATE */
231886 u8 *a1 = aExist;
231887 u8 *a2 = aRec;
231888 assert( op1==SQLITE_UPDATE )((void) (0));
231889 if( bPatchset==0 ){
231890 sessionSkipRecord(&a1, pTab->nCol);
231891 sessionSkipRecord(&a2, pTab->nCol);
231892 }
231893 pNew->op = SQLITE_UPDATE23;
231894 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
231895 sqlite3_free(pNew);
231896 pNew = 0;
231897 }
231898 }else{ /* UPDATE + DELETE */
231899 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE )((void) (0));
231900 pNew->op = SQLITE_DELETE9;
231901 if( bPatchset ){
231902 memcpy(aCsr, aRec, nRec);
231903 aCsr += nRec;
231904 }else{
231905 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
231906 }
231907 }
231908
231909 if( pNew ){
231910 pNew->nRecord = (int)(aCsr - pNew->aRecord);
231911 }
231912 sqlite3_free(pExist);
231913 }
231914 }
231915
231916 *ppNew = pNew;
231917 return rc;
231918}
231919
231920/*
231921** Check if a changeset entry with nCol columns and the PK array passed
231922** as the final argument to this function is compatible with SessionTable
231923** pTab. If so, return 1. Otherwise, if they are incompatible in some way,
231924** return 0.
231925*/
231926static int sessionChangesetCheckCompat(
231927 SessionTable *pTab,
231928 int nCol,
231929 u8 *abPK
231930){
231931 if( pTab->azCol && nCol<pTab->nCol ){
231932 int ii;
231933 for(ii=0; ii<pTab->nCol; ii++){
231934 u8 bPK = (ii < nCol) ? abPK[ii] : 0;
231935 if( pTab->abPK[ii]!=bPK ) return 0;
231936 }
231937 return 1;
231938 }
231939 return (pTab->nCol==nCol && 0==memcmp(abPK, pTab->abPK, nCol));
231940}
231941
231942static int sessionChangesetExtendRecord(
231943 sqlite3_changegroup *pGrp,
231944 SessionTable *pTab,
231945 int nCol,
231946 int op,
231947 const u8 *aRec,
231948 int nRec,
231949 SessionBuffer *pOut
231950){
231951 int rc = SQLITE_OK0;
231952 int ii = 0;
231953
231954 assert( pTab->azCol )((void) (0));
231955 assert( nCol<pTab->nCol )((void) (0));
231956
231957 pOut->nBuf = 0;
231958 if( op==SQLITE_INSERT18 || (op==SQLITE_DELETE9 && pGrp->bPatch==0) ){
231959 /* Append the missing default column values to the record. */
231960 sessionAppendBlob(pOut, aRec, nRec, &rc);
231961 if( rc==SQLITE_OK0 && pTab->pDfltStmt==0 ){
231962 rc = sessionPrepareDfltStmt(pGrp->db, pTab, &pTab->pDfltStmt);
231963 if( rc==SQLITE_OK0 && SQLITE_ROW100!=sqlite3_step(pTab->pDfltStmt) ){
231964 rc = sqlite3_errcode(pGrp->db);
231965 }
231966 }
231967 for(ii=nCol; rc==SQLITE_OK0 && ii<pTab->nCol; ii++){
231968 int eType = sqlite3_column_type(pTab->pDfltStmt, ii);
231969 sessionAppendByte(pOut, eType, &rc);
231970 switch( eType ){
231971 case SQLITE_FLOAT2:
231972 case SQLITE_INTEGER1: {
231973 i64 iVal;
231974 if( eType==SQLITE_INTEGER1 ){
231975 iVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
231976 }else{
231977 double rVal = sqlite3_column_int64(pTab->pDfltStmt, ii);
231978 memcpy(&iVal, &rVal, sizeof(i64));
231979 }
231980 if( SQLITE_OK0==sessionBufferGrow(pOut, 8, &rc) ){
231981 sessionPutI64(&pOut->aBuf[pOut->nBuf], iVal);
231982 pOut->nBuf += 8;
231983 }
231984 break;
231985 }
231986
231987 case SQLITE_BLOB4:
231988 case SQLITE_TEXT3: {
231989 int n = sqlite3_column_bytes(pTab->pDfltStmt, ii);
231990 sessionAppendVarint(pOut, n, &rc);
231991 if( eType==SQLITE_TEXT3 ){
231992 const u8 *z = (const u8*)sqlite3_column_text(pTab->pDfltStmt, ii);
231993 sessionAppendBlob(pOut, z, n, &rc);
231994 }else{
231995 const u8 *z = (const u8*)sqlite3_column_blob(pTab->pDfltStmt, ii);
231996 sessionAppendBlob(pOut, z, n, &rc);
231997 }
231998 break;
231999 }
232000
232001 default:
232002 assert( eType==SQLITE_NULL )((void) (0));
232003 break;
232004 }
232005 }
232006 }else if( op==SQLITE_UPDATE23 ){
232007 /* Append missing "undefined" entries to the old.* record. And, if this
232008 ** is an UPDATE, to the new.* record as well. */
232009 int iOff = 0;
232010 if( pGrp->bPatch==0 ){
232011 for(ii=0; ii<nCol; ii++){
232012 iOff += sessionSerialLen(&aRec[iOff]);
232013 }
232014 sessionAppendBlob(pOut, aRec, iOff, &rc);
232015 for(ii=0; ii<(pTab->nCol-nCol); ii++){
232016 sessionAppendByte(pOut, 0x00, &rc);
232017 }
232018 }
232019
232020 sessionAppendBlob(pOut, &aRec[iOff], nRec-iOff, &rc);
232021 for(ii=0; ii<(pTab->nCol-nCol); ii++){
232022 sessionAppendByte(pOut, 0x00, &rc);
232023 }
232024 }else{
232025 assert( op==SQLITE_DELETE && pGrp->bPatch )((void) (0));
232026 sessionAppendBlob(pOut, aRec, nRec, &rc);
232027 }
232028
232029 return rc;
232030}
232031
232032/*
232033** Locate or create a SessionTable object that may be used to add the
232034** change currently pointed to by iterator pIter to changegroup pGrp.
232035** If successful, set output variable (*ppTab) to point to the table
232036** object and return SQLITE_OK. Otherwise, if some error occurs, return
232037** an SQLite error code and leave (*ppTab) set to NULL.
232038*/
232039static int sessionChangesetFindTable(
232040 sqlite3_changegroup *pGrp,
232041 const char *zTab,
232042 sqlite3_changeset_iter *pIter,
232043 SessionTable **ppTab
232044){
232045 int rc = SQLITE_OK0;
232046 SessionTable *pTab = 0;
232047 int nTab = (int)strlen(zTab);
232048 u8 *abPK = 0;
232049 int nCol = 0;
232050
232051 *ppTab = 0;
232052 sqlite3changeset_pk(pIter, &abPK, &nCol);
232053
232054 /* Search the list for an existing table */
232055 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
232056 if( 0==sqlite3_strnicmp(pTab->zName, zTab, nTab+1) ) break;
232057 }
232058
232059 /* If one was not found above, create a new table now */
232060 if( !pTab ){
232061 SessionTable **ppNew;
232062
232063 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nTab+1);
232064 if( !pTab ){
232065 return SQLITE_NOMEM7;
232066 }
232067 memset(pTab, 0, sizeof(SessionTable));
232068 pTab->nCol = nCol;
232069 pTab->abPK = (u8*)&pTab[1];
232070 memcpy(pTab->abPK, abPK, nCol);
232071 pTab->zName = (char*)&pTab->abPK[nCol];
232072 memcpy(pTab->zName, zTab, nTab+1);
232073
232074 if( pGrp->db ){
232075 pTab->nCol = 0;
232076 rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb);
232077 if( rc ){
232078 assert( pTab->azCol==0 )((void) (0));
232079 sqlite3_free(pTab);
232080 return rc;
232081 }
232082 }
232083
232084 /* The new object must be linked on to the end of the list, not
232085 ** simply added to the start of it. This is to ensure that the
232086 ** tables within the output of sqlite3changegroup_output() are in
232087 ** the right order. */
232088 for(ppNew=&pGrp->pList; *ppNew; ppNew=&(*ppNew)->pNext);
232089 *ppNew = pTab;
232090 }
232091
232092 /* Check that the table is compatible. */
232093 if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){
232094 rc = SQLITE_SCHEMA17;
232095 }
232096
232097 *ppTab = pTab;
232098 return rc;
232099}
232100
232101/*
232102** Add the change currently indicated by iterator pIter to the hash table
232103** belonging to changegroup pGrp.
232104*/
232105static int sessionOneChangeToHash(
232106 sqlite3_changegroup *pGrp,
232107 sqlite3_changeset_iter *pIter,
232108 int bRebase
232109){
232110 int rc = SQLITE_OK0;
232111 int nCol = 0;
232112 int op = 0;
232113 int iHash = 0;
232114 int bIndirect = 0;
232115 SessionChange *pChange = 0;
232116 SessionChange *pExist = 0;
232117 SessionChange **pp = 0;
232118 SessionTable *pTab = 0;
232119 u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2];
232120 int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2;
232121
232122 assert( nRec>0 )((void) (0));
232123
232124 /* Ensure that only changesets, or only patchsets, but not a mixture
232125 ** of both, are being combined. It is an error to try to combine a
232126 ** changeset and a patchset. */
232127 if( pGrp->pList==0 ){
232128 pGrp->bPatch = pIter->bPatchset;
232129 }else if( pIter->bPatchset!=pGrp->bPatch ){
232130 rc = SQLITE_ERROR1;
232131 }
232132
232133 if( rc==SQLITE_OK0 ){
232134 const char *zTab = 0;
232135 sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect);
232136 rc = sessionChangesetFindTable(pGrp, zTab, pIter, &pTab);
232137 }
232138
232139 if( rc==SQLITE_OK0 && nCol<pTab->nCol ){
232140 SessionBuffer *pBuf = &pGrp->rec;
232141 rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, pBuf);
232142 aRec = pBuf->aBuf;
232143 nRec = pBuf->nBuf;
232144 assert( pGrp->db )((void) (0));
232145 }
232146
232147 if( rc==SQLITE_OK0 && sessionGrowHash(0, pIter->bPatchset, pTab) ){
232148 rc = SQLITE_NOMEM7;
232149 }
232150
232151 if( rc==SQLITE_OK0 ){
232152 /* Search for existing entry. If found, remove it from the hash table.
232153 ** Code below may link it back in. */
232154 iHash = sessionChangeHash(
232155 pTab, (pIter->bPatchset && op==SQLITE_DELETE9), aRec, pTab->nChange
232156 );
232157 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
232158 int bPkOnly1 = 0;
232159 int bPkOnly2 = 0;
232160 if( pIter->bPatchset ){
232161 bPkOnly1 = (*pp)->op==SQLITE_DELETE9;
232162 bPkOnly2 = op==SQLITE_DELETE9;
232163 }
232164 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
232165 pExist = *pp;
232166 *pp = (*pp)->pNext;
232167 pTab->nEntry--;
232168 break;
232169 }
232170 }
232171 }
232172
232173 if( rc==SQLITE_OK0 ){
232174 rc = sessionChangeMerge(pTab, bRebase,
232175 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
232176 );
232177 }
232178 if( rc==SQLITE_OK0 && pChange ){
232179 pChange->pNext = pTab->apChange[iHash];
232180 pTab->apChange[iHash] = pChange;
232181 pTab->nEntry++;
232182 }
232183
232184 if( rc==SQLITE_OK0 ) rc = pIter->rc;
232185 return rc;
232186}
232187
232188/*
232189** Add all changes in the changeset traversed by the iterator passed as
232190** the first argument to the changegroup hash tables.
232191*/
232192static int sessionChangesetToHash(
232193 sqlite3_changeset_iter *pIter, /* Iterator to read from */
232194 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
232195 int bRebase /* True if hash table is for rebasing */
232196){
232197 u8 *aRec;
232198 int nRec;
232199 int rc = SQLITE_OK0;
232200
232201 pIter->in.bNoDiscard = 1;
232202 while( SQLITE_ROW100==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){
232203 rc = sessionOneChangeToHash(pGrp, pIter, bRebase);
232204 if( rc!=SQLITE_OK0 ) break;
232205 }
232206
232207 if( rc==SQLITE_OK0 ) rc = pIter->rc;
232208 return rc;
232209}
232210
232211/*
232212** Serialize a changeset (or patchset) based on all changesets (or patchsets)
232213** added to the changegroup object passed as the first argument.
232214**
232215** If xOutput is not NULL, then the changeset/patchset is returned to the
232216** user via one or more calls to xOutput, as with the other streaming
232217** interfaces.
232218**
232219** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
232220** buffer containing the output changeset before this function returns. In
232221** this case (*pnOut) is set to the size of the output buffer in bytes. It
232222** is the responsibility of the caller to free the output buffer using
232223** sqlite3_free() when it is no longer required.
232224**
232225** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
232226** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
232227** are both set to 0 before returning.
232228*/
232229static int sessionChangegroupOutput(
232230 sqlite3_changegroup *pGrp,
232231 int (*xOutput)(void *pOut, const void *pData, int nData),
232232 void *pOut,
232233 int *pnOut,
232234 void **ppOut
232235){
232236 int rc = SQLITE_OK0;
232237 SessionBuffer buf = {0, 0, 0};
232238 SessionTable *pTab;
232239 assert( xOutput==0 || (ppOut==0 && pnOut==0) )((void) (0));
232240
232241 /* Create the serialized output changeset based on the contents of the
232242 ** hash tables attached to the SessionTable objects in list p->pList.
232243 */
232244 for(pTab=pGrp->pList; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
232245 int i;
232246 if( pTab->nEntry==0 ) continue;
232247
232248 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
232249 for(i=0; i<pTab->nChange; i++){
232250 SessionChange *p;
232251 for(p=pTab->apChange[i]; p; p=p->pNext){
232252 sessionAppendByte(&buf, p->op, &rc);
232253 sessionAppendByte(&buf, p->bIndirect, &rc);
232254 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
232255 if( rc==SQLITE_OK0 && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
232256 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
232257 buf.nBuf = 0;
232258 }
232259 }
232260 }
232261 }
232262
232263 if( rc==SQLITE_OK0 ){
232264 if( xOutput ){
232265 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
232266 }else if( ppOut ){
232267 *ppOut = buf.aBuf;
232268 if( pnOut ) *pnOut = buf.nBuf;
232269 buf.aBuf = 0;
232270 }
232271 }
232272 sqlite3_free(buf.aBuf);
232273
232274 return rc;
232275}
232276
232277/*
232278** Allocate a new, empty, sqlite3_changegroup.
232279*/
232280SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
232281 int rc = SQLITE_OK0; /* Return code */
232282 sqlite3_changegroup *p; /* New object */
232283 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
232284 if( p==0 ){
232285 rc = SQLITE_NOMEM7;
232286 }else{
232287 memset(p, 0, sizeof(sqlite3_changegroup));
232288 }
232289 *pp = p;
232290 return rc;
232291}
232292
232293/*
232294** Provide a database schema to the changegroup object.
232295*/
232296SQLITE_API int sqlite3changegroup_schema(
232297 sqlite3_changegroup *pGrp,
232298 sqlite3 *db,
232299 const char *zDb
232300){
232301 int rc = SQLITE_OK0;
232302
232303 if( pGrp->pList || pGrp->db ){
232304 /* Cannot add a schema after one or more calls to sqlite3changegroup_add(),
232305 ** or after sqlite3changegroup_schema() has already been called. */
232306 rc = SQLITE_MISUSE21;
232307 }else{
232308 pGrp->zDb = sqlite3_mprintf("%s", zDb);
232309 if( pGrp->zDb==0 ){
232310 rc = SQLITE_NOMEM7;
232311 }else{
232312 pGrp->db = db;
232313 }
232314 }
232315 return rc;
232316}
232317
232318/*
232319** Add the changeset currently stored in buffer pData, size nData bytes,
232320** to changeset-group p.
232321*/
232322SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
232323 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
232324 int rc; /* Return code */
232325
232326 rc = sqlite3changeset_start(&pIter, nData, pData);
232327 if( rc==SQLITE_OK0 ){
232328 rc = sessionChangesetToHash(pIter, pGrp, 0);
232329 }
232330 sqlite3changeset_finalize(pIter);
232331 return rc;
232332}
232333
232334/*
232335** Add a single change to a changeset-group.
232336*/
232337SQLITE_API int sqlite3changegroup_add_change(
232338 sqlite3_changegroup *pGrp,
232339 sqlite3_changeset_iter *pIter
232340){
232341 if( pIter->in.iCurrent==pIter->in.iNext
232342 || pIter->rc!=SQLITE_OK0
232343 || pIter->bInvert
232344 ){
232345 /* Iterator does not point to any valid entry or is an INVERT iterator. */
232346 return SQLITE_ERROR1;
232347 }
232348 return sessionOneChangeToHash(pGrp, pIter, 0);
232349}
232350
232351/*
232352** Obtain a buffer containing a changeset representing the concatenation
232353** of all changesets added to the group so far.
232354*/
232355SQLITE_API int sqlite3changegroup_output(
232356 sqlite3_changegroup *pGrp,
232357 int *pnData,
232358 void **ppData
232359){
232360 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
232361}
232362
232363/*
232364** Streaming versions of changegroup_add().
232365*/
232366SQLITE_API int sqlite3changegroup_add_strm(
232367 sqlite3_changegroup *pGrp,
232368 int (*xInput)(void *pIn, void *pData, int *pnData),
232369 void *pIn
232370){
232371 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
232372 int rc; /* Return code */
232373
232374 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
232375 if( rc==SQLITE_OK0 ){
232376 rc = sessionChangesetToHash(pIter, pGrp, 0);
232377 }
232378 sqlite3changeset_finalize(pIter);
232379 return rc;
232380}
232381
232382/*
232383** Streaming versions of changegroup_output().
232384*/
232385SQLITE_API int sqlite3changegroup_output_strm(
232386 sqlite3_changegroup *pGrp,
232387 int (*xOutput)(void *pOut, const void *pData, int nData),
232388 void *pOut
232389){
232390 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
232391}
232392
232393/*
232394** Delete a changegroup object.
232395*/
232396SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
232397 if( pGrp ){
232398 sqlite3_free(pGrp->zDb);
232399 sessionDeleteTable(0, pGrp->pList);
232400 sqlite3_free(pGrp->rec.aBuf);
232401 sqlite3_free(pGrp);
232402 }
232403}
232404
232405/*
232406** Combine two changesets together.
232407*/
232408SQLITE_API int sqlite3changeset_concat(
232409 int nLeft, /* Number of bytes in lhs input */
232410 void *pLeft, /* Lhs input changeset */
232411 int nRight /* Number of bytes in rhs input */,
232412 void *pRight, /* Rhs input changeset */
232413 int *pnOut, /* OUT: Number of bytes in output changeset */
232414 void **ppOut /* OUT: changeset (left <concat> right) */
232415){
232416 sqlite3_changegroup *pGrp;
232417 int rc;
232418
232419 rc = sqlite3changegroup_new(&pGrp);
232420 if( rc==SQLITE_OK0 ){
232421 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
232422 }
232423 if( rc==SQLITE_OK0 ){
232424 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
232425 }
232426 if( rc==SQLITE_OK0 ){
232427 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
232428 }
232429 sqlite3changegroup_delete(pGrp);
232430
232431 return rc;
232432}
232433
232434/*
232435** Streaming version of sqlite3changeset_concat().
232436*/
232437SQLITE_API int sqlite3changeset_concat_strm(
232438 int (*xInputA)(void *pIn, void *pData, int *pnData),
232439 void *pInA,
232440 int (*xInputB)(void *pIn, void *pData, int *pnData),
232441 void *pInB,
232442 int (*xOutput)(void *pOut, const void *pData, int nData),
232443 void *pOut
232444){
232445 sqlite3_changegroup *pGrp;
232446 int rc;
232447
232448 rc = sqlite3changegroup_new(&pGrp);
232449 if( rc==SQLITE_OK0 ){
232450 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
232451 }
232452 if( rc==SQLITE_OK0 ){
232453 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
232454 }
232455 if( rc==SQLITE_OK0 ){
232456 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
232457 }
232458 sqlite3changegroup_delete(pGrp);
232459
232460 return rc;
232461}
232462
232463/*
232464** Changeset rebaser handle.
232465*/
232466struct sqlite3_rebaser {
232467 sqlite3_changegroup grp; /* Hash table */
232468};
232469
232470/*
232471** Buffers a1 and a2 must both contain a sessions module record nCol
232472** fields in size. This function appends an nCol sessions module
232473** record to buffer pBuf that is a copy of a1, except that for
232474** each field that is undefined in a1[], swap in the field from a2[].
232475*/
232476static void sessionAppendRecordMerge(
232477 SessionBuffer *pBuf, /* Buffer to append to */
232478 int nCol, /* Number of columns in each record */
232479 u8 *a1, int n1, /* Record 1 */
232480 u8 *a2, int n2, /* Record 2 */
232481 int *pRc /* IN/OUT: error code */
232482){
232483 sessionBufferGrow(pBuf, n1+n2, pRc);
232484 if( *pRc==SQLITE_OK0 ){
232485 int i;
232486 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
232487 for(i=0; i<nCol; i++){
232488 int nn1 = sessionSerialLen(a1);
232489 int nn2 = sessionSerialLen(a2);
232490 if( *a1==0 || *a1==0xFF ){
232491 memcpy(pOut, a2, nn2);
232492 pOut += nn2;
232493 }else{
232494 memcpy(pOut, a1, nn1);
232495 pOut += nn1;
232496 }
232497 a1 += nn1;
232498 a2 += nn2;
232499 }
232500
232501 pBuf->nBuf = pOut-pBuf->aBuf;
232502 assert( pBuf->nBuf<=pBuf->nAlloc )((void) (0));
232503 }
232504}
232505
232506/*
232507** This function is called when rebasing a local UPDATE change against one
232508** or more remote UPDATE changes. The aRec/nRec buffer contains the current
232509** old.* and new.* records for the change. The rebase buffer (a single
232510** record) is in aChange/nChange. The rebased change is appended to buffer
232511** pBuf.
232512**
232513** Rebasing the UPDATE involves:
232514**
232515** * Removing any changes to fields for which the corresponding field
232516** in the rebase buffer is set to "replaced" (type 0xFF). If this
232517** means the UPDATE change updates no fields, nothing is appended
232518** to the output buffer.
232519**
232520** * For each field modified by the local change for which the
232521** corresponding field in the rebase buffer is not "undefined" (0x00)
232522** or "replaced" (0xFF), the old.* value is replaced by the value
232523** in the rebase buffer.
232524*/
232525static void sessionAppendPartialUpdate(
232526 SessionBuffer *pBuf, /* Append record here */
232527 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
232528 u8 *aRec, int nRec, /* Local change */
232529 u8 *aChange, int nChange, /* Record to rebase against */
232530 int *pRc /* IN/OUT: Return Code */
232531){
232532 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
232533 if( *pRc==SQLITE_OK0 ){
232534 int bData = 0;
232535 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
232536 int i;
232537 u8 *a1 = aRec;
232538 u8 *a2 = aChange;
232539
232540 *pOut++ = SQLITE_UPDATE23;
232541 *pOut++ = pIter->bIndirect;
232542 for(i=0; i<pIter->nCol; i++){
232543 int n1 = sessionSerialLen(a1);
232544 int n2 = sessionSerialLen(a2);
232545 if( pIter->abPK[i] || a2[0]==0 ){
232546 if( !pIter->abPK[i] && a1[0] ) bData = 1;
232547 memcpy(pOut, a1, n1);
232548 pOut += n1;
232549 }else if( a2[0]!=0xFF && a1[0] ){
232550 bData = 1;
232551 memcpy(pOut, a2, n2);
232552 pOut += n2;
232553 }else{
232554 *pOut++ = '\0';
232555 }
232556 a1 += n1;
232557 a2 += n2;
232558 }
232559 if( bData ){
232560 a2 = aChange;
232561 for(i=0; i<pIter->nCol; i++){
232562 int n1 = sessionSerialLen(a1);
232563 int n2 = sessionSerialLen(a2);
232564 if( pIter->abPK[i] || a2[0]!=0xFF ){
232565 memcpy(pOut, a1, n1);
232566 pOut += n1;
232567 }else{
232568 *pOut++ = '\0';
232569 }
232570 a1 += n1;
232571 a2 += n2;
232572 }
232573 pBuf->nBuf = (pOut - pBuf->aBuf);
232574 }
232575 }
232576}
232577
232578/*
232579** pIter is configured to iterate through a changeset. This function rebases
232580** that changeset according to the current configuration of the rebaser
232581** object passed as the first argument. If no error occurs and argument xOutput
232582** is not NULL, then the changeset is returned to the caller by invoking
232583** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
232584** then (*ppOut) is set to point to a buffer containing the rebased changeset
232585** before this function returns. In this case (*pnOut) is set to the size of
232586** the buffer in bytes. It is the responsibility of the caller to eventually
232587** free the (*ppOut) buffer using sqlite3_free().
232588**
232589** If an error occurs, an SQLite error code is returned. If ppOut and
232590** pnOut are not NULL, then the two output parameters are set to 0 before
232591** returning.
232592*/
232593static int sessionRebase(
232594 sqlite3_rebaser *p, /* Rebaser hash table */
232595 sqlite3_changeset_iter *pIter, /* Input data */
232596 int (*xOutput)(void *pOut, const void *pData, int nData),
232597 void *pOut, /* Context for xOutput callback */
232598 int *pnOut, /* OUT: Number of bytes in output changeset */
232599 void **ppOut /* OUT: Inverse of pChangeset */
232600){
232601 int rc = SQLITE_OK0;
232602 u8 *aRec = 0;
232603 int nRec = 0;
232604 int bNew = 0;
232605 SessionTable *pTab = 0;
232606 SessionBuffer sOut = {0,0,0};
232607
232608 while( SQLITE_ROW100==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
232609 SessionChange *pChange = 0;
232610 int bDone = 0;
232611
232612 if( bNew ){
232613 const char *zTab = pIter->zTab;
232614 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
232615 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
232616 }
232617 bNew = 0;
232618
232619 /* A patchset may not be rebased */
232620 if( pIter->bPatchset ){
232621 rc = SQLITE_ERROR1;
232622 }
232623
232624 /* Append a table header to the output for this new table */
232625 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
232626 sessionAppendVarint(&sOut, pIter->nCol, &rc);
232627 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
232628 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
232629 }
232630
232631 if( pTab && rc==SQLITE_OK0 ){
232632 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
232633
232634 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
232635 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
232636 break;
232637 }
232638 }
232639 }
232640
232641 if( pChange ){
232642 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT )((void) (0));
232643 switch( pIter->op ){
232644 case SQLITE_INSERT18:
232645 if( pChange->op==SQLITE_INSERT18 ){
232646 bDone = 1;
232647 if( pChange->bIndirect==0 ){
232648 sessionAppendByte(&sOut, SQLITE_UPDATE23, &rc);
232649 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
232650 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
232651 sessionAppendBlob(&sOut, aRec, nRec, &rc);
232652 }
232653 }
232654 break;
232655
232656 case SQLITE_UPDATE23:
232657 bDone = 1;
232658 if( pChange->op==SQLITE_DELETE9 ){
232659 if( pChange->bIndirect==0 ){
232660 u8 *pCsr = aRec;
232661 sessionSkipRecord(&pCsr, pIter->nCol);
232662 sessionAppendByte(&sOut, SQLITE_INSERT18, &rc);
232663 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
232664 sessionAppendRecordMerge(&sOut, pIter->nCol,
232665 pCsr, nRec-(pCsr-aRec),
232666 pChange->aRecord, pChange->nRecord, &rc
232667 );
232668 }
232669 }else{
232670 sessionAppendPartialUpdate(&sOut, pIter,
232671 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
232672 );
232673 }
232674 break;
232675
232676 default:
232677 assert( pIter->op==SQLITE_DELETE )((void) (0));
232678 bDone = 1;
232679 if( pChange->op==SQLITE_INSERT18 ){
232680 sessionAppendByte(&sOut, SQLITE_DELETE9, &rc);
232681 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
232682 sessionAppendRecordMerge(&sOut, pIter->nCol,
232683 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
232684 );
232685 }
232686 break;
232687 }
232688 }
232689
232690 if( bDone==0 ){
232691 sessionAppendByte(&sOut, pIter->op, &rc);
232692 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
232693 sessionAppendBlob(&sOut, aRec, nRec, &rc);
232694 }
232695 if( rc==SQLITE_OK0 && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
232696 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
232697 sOut.nBuf = 0;
232698 }
232699 if( rc ) break;
232700 }
232701
232702 if( rc!=SQLITE_OK0 ){
232703 sqlite3_free(sOut.aBuf);
232704 memset(&sOut, 0, sizeof(sOut));
232705 }
232706
232707 if( rc==SQLITE_OK0 ){
232708 if( xOutput ){
232709 if( sOut.nBuf>0 ){
232710 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
232711 }
232712 }else if( ppOut ){
232713 *ppOut = (void*)sOut.aBuf;
232714 *pnOut = sOut.nBuf;
232715 sOut.aBuf = 0;
232716 }
232717 }
232718 sqlite3_free(sOut.aBuf);
232719 return rc;
232720}
232721
232722/*
232723** Create a new rebaser object.
232724*/
232725SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
232726 int rc = SQLITE_OK0;
232727 sqlite3_rebaser *pNew;
232728
232729 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
232730 if( pNew==0 ){
232731 rc = SQLITE_NOMEM7;
232732 }else{
232733 memset(pNew, 0, sizeof(sqlite3_rebaser));
232734 }
232735 *ppNew = pNew;
232736 return rc;
232737}
232738
232739/*
232740** Call this one or more times to configure a rebaser.
232741*/
232742SQLITE_API int sqlite3rebaser_configure(
232743 sqlite3_rebaser *p,
232744 int nRebase, const void *pRebase
232745){
232746 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
232747 int rc; /* Return code */
232748 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
232749 if( rc==SQLITE_OK0 ){
232750 rc = sessionChangesetToHash(pIter, &p->grp, 1);
232751 }
232752 sqlite3changeset_finalize(pIter);
232753 return rc;
232754}
232755
232756/*
232757** Rebase a changeset according to current rebaser configuration
232758*/
232759SQLITE_API int sqlite3rebaser_rebase(
232760 sqlite3_rebaser *p,
232761 int nIn, const void *pIn,
232762 int *pnOut, void **ppOut
232763){
232764 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
232765 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
232766
232767 if( rc==SQLITE_OK0 ){
232768 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
232769 sqlite3changeset_finalize(pIter);
232770 }
232771
232772 return rc;
232773}
232774
232775/*
232776** Rebase a changeset according to current rebaser configuration
232777*/
232778SQLITE_API int sqlite3rebaser_rebase_strm(
232779 sqlite3_rebaser *p,
232780 int (*xInput)(void *pIn, void *pData, int *pnData),
232781 void *pIn,
232782 int (*xOutput)(void *pOut, const void *pData, int nData),
232783 void *pOut
232784){
232785 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
232786 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
232787
232788 if( rc==SQLITE_OK0 ){
232789 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
232790 sqlite3changeset_finalize(pIter);
232791 }
232792
232793 return rc;
232794}
232795
232796/*
232797** Destroy a rebaser object
232798*/
232799SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
232800 if( p ){
232801 sessionDeleteTable(0, p->grp.pList);
232802 sqlite3_free(p->grp.rec.aBuf);
232803 sqlite3_free(p);
232804 }
232805}
232806
232807/*
232808** Global configuration
232809*/
232810SQLITE_API int sqlite3session_config(int op, void *pArg){
232811 int rc = SQLITE_OK0;
232812 switch( op ){
232813 case SQLITE_SESSION_CONFIG_STRMSIZE1: {
232814 int *pInt = (int*)pArg;
232815 if( *pInt>0 ){
232816 sessions_strm_chunk_size = *pInt;
232817 }
232818 *pInt = sessions_strm_chunk_size;
232819 break;
232820 }
232821 default:
232822 rc = SQLITE_MISUSE21;
232823 break;
232824 }
232825 return rc;
232826}
232827
232828#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
232829
232830/************** End of sqlite3session.c **************************************/
232831/************** Begin file fts5.c ********************************************/
232832
232833/*
232834** This, the "fts5.c" source file, is a composite file that is itself
232835** assembled from the following files:
232836**
232837** fts5.h
232838** fts5Int.h
232839** fts5parse.h <--- Generated from fts5parse.y by Lemon
232840** fts5parse.c <--- Generated from fts5parse.y by Lemon
232841** fts5_aux.c
232842** fts5_buffer.c
232843** fts5_config.c
232844** fts5_expr.c
232845** fts5_hash.c
232846** fts5_index.c
232847** fts5_main.c
232848** fts5_storage.c
232849** fts5_tokenize.c
232850** fts5_unicode2.c
232851** fts5_varint.c
232852** fts5_vocab.c
232853*/
232854#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS51)
232855
232856#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
232857# define NDEBUG1 1
232858#endif
232859#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
232860# undef NDEBUG1
232861#endif
232862
232863#ifdef HAVE_STDINT_H
232864/* #include <stdint.h> */
232865#endif
232866#ifdef HAVE_INTTYPES_H
232867/* #include <inttypes.h> */
232868#endif
232869/*
232870** 2014 May 31
232871**
232872** The author disclaims copyright to this source code. In place of
232873** a legal notice, here is a blessing:
232874**
232875** May you do good and not evil.
232876** May you find forgiveness for yourself and forgive others.
232877** May you share freely, never taking more than you give.
232878**
232879******************************************************************************
232880**
232881** Interfaces to extend FTS5. Using the interfaces defined in this file,
232882** FTS5 may be extended with:
232883**
232884** * custom tokenizers, and
232885** * custom auxiliary functions.
232886*/
232887
232888
232889#ifndef _FTS5_H
232890#define _FTS5_H
232891
232892/* #include "sqlite3.h" */
232893
232894#if 0
232895extern "C" {
232896#endif
232897
232898/*************************************************************************
232899** CUSTOM AUXILIARY FUNCTIONS
232900**
232901** Virtual table implementations may overload SQL functions by implementing
232902** the sqlite3_module.xFindFunction() method.
232903*/
232904
232905typedef struct Fts5ExtensionApi Fts5ExtensionApi;
232906typedef struct Fts5Context Fts5Context;
232907typedef struct Fts5PhraseIter Fts5PhraseIter;
232908
232909typedef void (*fts5_extension_function)(
232910 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
232911 Fts5Context *pFts, /* First arg to pass to pApi functions */
232912 sqlite3_context *pCtx, /* Context for returning result/error */
232913 int nVal, /* Number of values in apVal[] array */
232914 sqlite3_value **apVal /* Array of trailing arguments */
232915);
232916
232917struct Fts5PhraseIter {
232918 const unsigned char *a;
232919 const unsigned char *b;
232920};
232921
232922/*
232923** EXTENSION API FUNCTIONS
232924**
232925** xUserData(pFts):
232926** Return a copy of the pUserData pointer passed to the xCreateFunction()
232927** API when the extension function was registered.
232928**
232929** xColumnTotalSize(pFts, iCol, pnToken):
232930** If parameter iCol is less than zero, set output variable *pnToken
232931** to the total number of tokens in the FTS5 table. Or, if iCol is
232932** non-negative but less than the number of columns in the table, return
232933** the total number of tokens in column iCol, considering all rows in
232934** the FTS5 table.
232935**
232936** If parameter iCol is greater than or equal to the number of columns
232937** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
232938** an OOM condition or IO error), an appropriate SQLite error code is
232939** returned.
232940**
232941** xColumnCount(pFts):
232942** Return the number of columns in the table.
232943**
232944** xColumnSize(pFts, iCol, pnToken):
232945** If parameter iCol is less than zero, set output variable *pnToken
232946** to the total number of tokens in the current row. Or, if iCol is
232947** non-negative but less than the number of columns in the table, set
232948** *pnToken to the number of tokens in column iCol of the current row.
232949**
232950** If parameter iCol is greater than or equal to the number of columns
232951** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
232952** an OOM condition or IO error), an appropriate SQLite error code is
232953** returned.
232954**
232955** This function may be quite inefficient if used with an FTS5 table
232956** created with the "columnsize=0" option.
232957**
232958** xColumnText:
232959** If parameter iCol is less than zero, or greater than or equal to the
232960** number of columns in the table, SQLITE_RANGE is returned.
232961**
232962** Otherwise, this function attempts to retrieve the text of column iCol of
232963** the current document. If successful, (*pz) is set to point to a buffer
232964** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
232965** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
232966** if an error occurs, an SQLite error code is returned and the final values
232967** of (*pz) and (*pn) are undefined.
232968**
232969** xPhraseCount:
232970** Returns the number of phrases in the current query expression.
232971**
232972** xPhraseSize:
232973** If parameter iCol is less than zero, or greater than or equal to the
232974** number of phrases in the current query, as returned by xPhraseCount,
232975** 0 is returned. Otherwise, this function returns the number of tokens in
232976** phrase iPhrase of the query. Phrases are numbered starting from zero.
232977**
232978** xInstCount:
232979** Set *pnInst to the total number of occurrences of all phrases within
232980** the query within the current row. Return SQLITE_OK if successful, or
232981** an error code (i.e. SQLITE_NOMEM) if an error occurs.
232982**
232983** This API can be quite slow if used with an FTS5 table created with the
232984** "detail=none" or "detail=column" option. If the FTS5 table is created
232985** with either "detail=none" or "detail=column" and "content=" option
232986** (i.e. if it is a contentless table), then this API always returns 0.
232987**
232988** xInst:
232989** Query for the details of phrase match iIdx within the current row.
232990** Phrase matches are numbered starting from zero, so the iIdx argument
232991** should be greater than or equal to zero and smaller than the value
232992** output by xInstCount(). If iIdx is less than zero or greater than
232993** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
232994**
232995** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
232996** to the column in which it occurs and *piOff the token offset of the
232997** first token of the phrase. SQLITE_OK is returned if successful, or an
232998** error code (i.e. SQLITE_NOMEM) if an error occurs.
232999**
233000** This API can be quite slow if used with an FTS5 table created with the
233001** "detail=none" or "detail=column" option.
233002**
233003** xRowid:
233004** Returns the rowid of the current row.
233005**
233006** xTokenize:
233007** Tokenize text using the tokenizer belonging to the FTS5 table.
233008**
233009** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
233010** This API function is used to query the FTS table for phrase iPhrase
233011** of the current query. Specifically, a query equivalent to:
233012**
233013** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
233014**
233015** with $p set to a phrase equivalent to the phrase iPhrase of the
233016** current query is executed. Any column filter that applies to
233017** phrase iPhrase of the current query is included in $p. For each
233018** row visited, the callback function passed as the fourth argument
233019** is invoked. The context and API objects passed to the callback
233020** function may be used to access the properties of each matched row.
233021** Invoking Api.xUserData() returns a copy of the pointer passed as
233022** the third argument to pUserData.
233023**
233024** If parameter iPhrase is less than zero, or greater than or equal to
233025** the number of phrases in the query, as returned by xPhraseCount(),
233026** this function returns SQLITE_RANGE.
233027**
233028** If the callback function returns any value other than SQLITE_OK, the
233029** query is abandoned and the xQueryPhrase function returns immediately.
233030** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
233031** Otherwise, the error code is propagated upwards.
233032**
233033** If the query runs to completion without incident, SQLITE_OK is returned.
233034** Or, if some error occurs before the query completes or is aborted by
233035** the callback, an SQLite error code is returned.
233036**
233037**
233038** xSetAuxdata(pFts5, pAux, xDelete)
233039**
233040** Save the pointer passed as the second argument as the extension function's
233041** "auxiliary data". The pointer may then be retrieved by the current or any
233042** future invocation of the same fts5 extension function made as part of
233043** the same MATCH query using the xGetAuxdata() API.
233044**
233045** Each extension function is allocated a single auxiliary data slot for
233046** each FTS query (MATCH expression). If the extension function is invoked
233047** more than once for a single FTS query, then all invocations share a
233048** single auxiliary data context.
233049**
233050** If there is already an auxiliary data pointer when this function is
233051** invoked, then it is replaced by the new pointer. If an xDelete callback
233052** was specified along with the original pointer, it is invoked at this
233053** point.
233054**
233055** The xDelete callback, if one is specified, is also invoked on the
233056** auxiliary data pointer after the FTS5 query has finished.
233057**
233058** If an error (e.g. an OOM condition) occurs within this function,
233059** the auxiliary data is set to NULL and an error code returned. If the
233060** xDelete parameter was not NULL, it is invoked on the auxiliary data
233061** pointer before returning.
233062**
233063**
233064** xGetAuxdata(pFts5, bClear)
233065**
233066** Returns the current auxiliary data pointer for the fts5 extension
233067** function. See the xSetAuxdata() method for details.
233068**
233069** If the bClear argument is non-zero, then the auxiliary data is cleared
233070** (set to NULL) before this function returns. In this case the xDelete,
233071** if any, is not invoked.
233072**
233073**
233074** xRowCount(pFts5, pnRow)
233075**
233076** This function is used to retrieve the total number of rows in the table.
233077** In other words, the same value that would be returned by:
233078**
233079** SELECT count(*) FROM ftstable;
233080**
233081** xPhraseFirst()
233082** This function is used, along with type Fts5PhraseIter and the xPhraseNext
233083** method, to iterate through all instances of a single query phrase within
233084** the current row. This is the same information as is accessible via the
233085** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
233086** to use, this API may be faster under some circumstances. To iterate
233087** through instances of phrase iPhrase, use the following code:
233088**
233089** Fts5PhraseIter iter;
233090** int iCol, iOff;
233091** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
233092** iCol>=0;
233093** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
233094** ){
233095** // An instance of phrase iPhrase at offset iOff of column iCol
233096** }
233097**
233098** The Fts5PhraseIter structure is defined above. Applications should not
233099** modify this structure directly - it should only be used as shown above
233100** with the xPhraseFirst() and xPhraseNext() API methods (and by
233101** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
233102**
233103** This API can be quite slow if used with an FTS5 table created with the
233104** "detail=none" or "detail=column" option. If the FTS5 table is created
233105** with either "detail=none" or "detail=column" and "content=" option
233106** (i.e. if it is a contentless table), then this API always iterates
233107** through an empty set (all calls to xPhraseFirst() set iCol to -1).
233108**
233109** In all cases, matches are visited in (column ASC, offset ASC) order.
233110** i.e. all those in column 0, sorted by offset, followed by those in
233111** column 1, etc.
233112**
233113** xPhraseNext()
233114** See xPhraseFirst above.
233115**
233116** xPhraseFirstColumn()
233117** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
233118** and xPhraseNext() APIs described above. The difference is that instead
233119** of iterating through all instances of a phrase in the current row, these
233120** APIs are used to iterate through the set of columns in the current row
233121** that contain one or more instances of a specified phrase. For example:
233122**
233123** Fts5PhraseIter iter;
233124** int iCol;
233125** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
233126** iCol>=0;
233127** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
233128** ){
233129** // Column iCol contains at least one instance of phrase iPhrase
233130** }
233131**
233132** This API can be quite slow if used with an FTS5 table created with the
233133** "detail=none" option. If the FTS5 table is created with either
233134** "detail=none" "content=" option (i.e. if it is a contentless table),
233135** then this API always iterates through an empty set (all calls to
233136** xPhraseFirstColumn() set iCol to -1).
233137**
233138** The information accessed using this API and its companion
233139** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
233140** (or xInst/xInstCount). The chief advantage of this API is that it is
233141** significantly more efficient than those alternatives when used with
233142** "detail=column" tables.
233143**
233144** xPhraseNextColumn()
233145** See xPhraseFirstColumn above.
233146**
233147** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
233148** This is used to access token iToken of phrase iPhrase of the current
233149** query. Before returning, output parameter *ppToken is set to point
233150** to a buffer containing the requested token, and *pnToken to the
233151** size of this buffer in bytes.
233152**
233153** If iPhrase or iToken are less than zero, or if iPhrase is greater than
233154** or equal to the number of phrases in the query as reported by
233155** xPhraseCount(), or if iToken is equal to or greater than the number of
233156** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
233157 are both zeroed.
233158**
233159** The output text is not a copy of the query text that specified the
233160** token. It is the output of the tokenizer module. For tokendata=1
233161** tables, this includes any embedded 0x00 and trailing data.
233162**
233163** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
233164** This is used to access token iToken of phrase hit iIdx within the
233165** current row. If iIdx is less than zero or greater than or equal to the
233166** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
233167** output variable (*ppToken) is set to point to a buffer containing the
233168** matching document token, and (*pnToken) to the size of that buffer in
233169** bytes. This API is not available if the specified token matches a
233170** prefix query term. In that case both output variables are always set
233171** to 0.
233172**
233173** The output text is not a copy of the document text that was tokenized.
233174** It is the output of the tokenizer module. For tokendata=1 tables, this
233175** includes any embedded 0x00 and trailing data.
233176**
233177** This API can be quite slow if used with an FTS5 table created with the
233178** "detail=none" or "detail=column" option.
233179**
233180** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
233181** If parameter iCol is less than zero, or greater than or equal to the
233182** number of columns in the table, SQLITE_RANGE is returned.
233183**
233184** Otherwise, this function attempts to retrieve the locale associated
233185** with column iCol of the current row. Usually, there is no associated
233186** locale, and output parameters (*pzLocale) and (*pnLocale) are set
233187** to NULL and 0, respectively. However, if the fts5_locale() function
233188** was used to associate a locale with the value when it was inserted
233189** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated
233190** buffer containing the name of the locale in utf-8 encoding. (*pnLocale)
233191** is set to the size in bytes of the buffer, not including the
233192** nul-terminator.
233193**
233194** If successful, SQLITE_OK is returned. Or, if an error occurs, an
233195** SQLite error code is returned. The final value of the output parameters
233196** is undefined in this case.
233197**
233198** xTokenize_v2:
233199** Tokenize text using the tokenizer belonging to the FTS5 table. This
233200** API is the same as the xTokenize() API, except that it allows a tokenizer
233201** locale to be specified.
233202*/
233203struct Fts5ExtensionApi {
233204 int iVersion; /* Currently always set to 4 */
233205
233206 void *(*xUserData)(Fts5Context*);
233207
233208 int (*xColumnCount)(Fts5Context*);
233209 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
233210 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
233211
233212 int (*xTokenize)(Fts5Context*,
233213 const char *pText, int nText, /* Text to tokenize */
233214 void *pCtx, /* Context passed to xToken() */
233215 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
233216 );
233217
233218 int (*xPhraseCount)(Fts5Context*);
233219 int (*xPhraseSize)(Fts5Context*, int iPhrase);
233220
233221 int (*xInstCount)(Fts5Context*, int *pnInst);
233222 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
233223
233224 sqlite3_int64 (*xRowid)(Fts5Context*);
233225 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
233226 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
233227
233228 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
233229 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
233230 );
233231 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
233232 void *(*xGetAuxdata)(Fts5Context*, int bClear);
233233
233234 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
233235 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
233236
233237 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
233238 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
233239
233240 /* Below this point are iVersion>=3 only */
233241 int (*xQueryToken)(Fts5Context*,
233242 int iPhrase, int iToken,
233243 const char **ppToken, int *pnToken
233244 );
233245 int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
233246
233247 /* Below this point are iVersion>=4 only */
233248 int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn);
233249 int (*xTokenize_v2)(Fts5Context*,
233250 const char *pText, int nText, /* Text to tokenize */
233251 const char *pLocale, int nLocale, /* Locale to pass to tokenizer */
233252 void *pCtx, /* Context passed to xToken() */
233253 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
233254 );
233255};
233256
233257/*
233258** CUSTOM AUXILIARY FUNCTIONS
233259*************************************************************************/
233260
233261/*************************************************************************
233262** CUSTOM TOKENIZERS
233263**
233264** Applications may also register custom tokenizer types. A tokenizer
233265** is registered by providing fts5 with a populated instance of the
233266** following structure. All structure methods must be defined, setting
233267** any member of the fts5_tokenizer struct to NULL leads to undefined
233268** behaviour. The structure methods are expected to function as follows:
233269**
233270** xCreate:
233271** This function is used to allocate and initialize a tokenizer instance.
233272** A tokenizer instance is required to actually tokenize text.
233273**
233274** The first argument passed to this function is a copy of the (void*)
233275** pointer provided by the application when the fts5_tokenizer_v2 object
233276** was registered with FTS5 (the third argument to xCreateTokenizer()).
233277** The second and third arguments are an array of nul-terminated strings
233278** containing the tokenizer arguments, if any, specified following the
233279** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
233280** to create the FTS5 table.
233281**
233282** The final argument is an output variable. If successful, (*ppOut)
233283** should be set to point to the new tokenizer handle and SQLITE_OK
233284** returned. If an error occurs, some value other than SQLITE_OK should
233285** be returned. In this case, fts5 assumes that the final value of *ppOut
233286** is undefined.
233287**
233288** xDelete:
233289** This function is invoked to delete a tokenizer handle previously
233290** allocated using xCreate(). Fts5 guarantees that this function will
233291** be invoked exactly once for each successful call to xCreate().
233292**
233293** xTokenize:
233294** This function is expected to tokenize the nText byte string indicated
233295** by argument pText. pText may or may not be nul-terminated. The first
233296** argument passed to this function is a pointer to an Fts5Tokenizer object
233297** returned by an earlier call to xCreate().
233298**
233299** The third argument indicates the reason that FTS5 is requesting
233300** tokenization of the supplied text. This is always one of the following
233301** four values:
233302**
233303** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
233304** or removed from the FTS table. The tokenizer is being invoked to
233305** determine the set of tokens to add to (or delete from) the
233306** FTS index.
233307**
233308** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
233309** against the FTS index. The tokenizer is being called to tokenize
233310** a bareword or quoted string specified as part of the query.
233311**
233312** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
233313** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
233314** followed by a "*" character, indicating that the last token
233315** returned by the tokenizer will be treated as a token prefix.
233316**
233317** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
233318** satisfy an fts5_api.xTokenize() request made by an auxiliary
233319** function. Or an fts5_api.xColumnSize() request made by the same
233320** on a columnsize=0 database.
233321** </ul>
233322**
233323** The sixth and seventh arguments passed to xTokenize() - pLocale and
233324** nLocale - are a pointer to a buffer containing the locale to use for
233325** tokenization (e.g. "en_US") and its size in bytes, respectively. The
233326** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in
233327** which case nLocale is always 0) to indicate that the tokenizer should
233328** use its default locale.
233329**
233330** For each token in the input string, the supplied callback xToken() must
233331** be invoked. The first argument to it should be a copy of the pointer
233332** passed as the second argument to xTokenize(). The third and fourth
233333** arguments are a pointer to a buffer containing the token text, and the
233334** size of the token in bytes. The 4th and 5th arguments are the byte offsets
233335** of the first byte of and first byte immediately following the text from
233336** which the token is derived within the input.
233337**
233338** The second argument passed to the xToken() callback ("tflags") should
233339** normally be set to 0. The exception is if the tokenizer supports
233340** synonyms. In this case see the discussion below for details.
233341**
233342** FTS5 assumes the xToken() callback is invoked for each token in the
233343** order that they occur within the input text.
233344**
233345** If an xToken() callback returns any value other than SQLITE_OK, then
233346** the tokenization should be abandoned and the xTokenize() method should
233347** immediately return a copy of the xToken() return value. Or, if the
233348** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
233349** if an error occurs with the xTokenize() implementation itself, it
233350** may abandon the tokenization and return any error code other than
233351** SQLITE_OK or SQLITE_DONE.
233352**
233353** If the tokenizer is registered using an fts5_tokenizer_v2 object,
233354** then the xTokenize() method has two additional arguments - pLocale
233355** and nLocale. These specify the locale that the tokenizer should use
233356** for the current request. If pLocale and nLocale are both 0, then the
233357** tokenizer should use its default locale. Otherwise, pLocale points to
233358** an nLocale byte buffer containing the name of the locale to use as utf-8
233359** text. pLocale is not nul-terminated.
233360**
233361** FTS5_TOKENIZER
233362**
233363** There is also an fts5_tokenizer object. This is an older, deprecated,
233364** version of fts5_tokenizer_v2. It is similar except that:
233365**
233366** <ul>
233367** <li> There is no "iVersion" field, and
233368** <li> The xTokenize() method does not take a locale argument.
233369** </ul>
233370**
233371** Legacy fts5_tokenizer tokenizers must be registered using the
233372** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2().
233373**
233374** Tokenizer implementations registered using either API may be retrieved
233375** using both xFindTokenizer() and xFindTokenizer_v2().
233376**
233377** SYNONYM SUPPORT
233378**
233379** Custom tokenizers may also support synonyms. Consider a case in which a
233380** user wishes to query for a phrase such as "first place". Using the
233381** built-in tokenizers, the FTS5 query 'first + place' will match instances
233382** of "first place" within the document set, but not alternative forms
233383** such as "1st place". In some applications, it would be better to match
233384** all instances of "first place" or "1st place" regardless of which form
233385** the user specified in the MATCH query text.
233386**
233387** There are several ways to approach this in FTS5:
233388**
233389** <ol><li> By mapping all synonyms to a single token. In this case, using
233390** the above example, this means that the tokenizer returns the
233391** same token for inputs "first" and "1st". Say that token is in
233392** fact "first", so that when the user inserts the document "I won
233393** 1st place" entries are added to the index for tokens "i", "won",
233394** "first" and "place". If the user then queries for '1st + place',
233395** the tokenizer substitutes "first" for "1st" and the query works
233396** as expected.
233397**
233398** <li> By querying the index for all synonyms of each query term
233399** separately. In this case, when tokenizing query text, the
233400** tokenizer may provide multiple synonyms for a single term
233401** within the document. FTS5 then queries the index for each
233402** synonym individually. For example, faced with the query:
233403**
233404** <codeblock>
233405** ... MATCH 'first place'</codeblock>
233406**
233407** the tokenizer offers both "1st" and "first" as synonyms for the
233408** first token in the MATCH query and FTS5 effectively runs a query
233409** similar to:
233410**
233411** <codeblock>
233412** ... MATCH '(first OR 1st) place'</codeblock>
233413**
233414** except that, for the purposes of auxiliary functions, the query
233415** still appears to contain just two phrases - "(first OR 1st)"
233416** being treated as a single phrase.
233417**
233418** <li> By adding multiple synonyms for a single term to the FTS index.
233419** Using this method, when tokenizing document text, the tokenizer
233420** provides multiple synonyms for each token. So that when a
233421** document such as "I won first place" is tokenized, entries are
233422** added to the FTS index for "i", "won", "first", "1st" and
233423** "place".
233424**
233425** This way, even if the tokenizer does not provide synonyms
233426** when tokenizing query text (it should not - to do so would be
233427** inefficient), it doesn't matter if the user queries for
233428** 'first + place' or '1st + place', as there are entries in the
233429** FTS index corresponding to both forms of the first token.
233430** </ol>
233431**
233432** Whether it is parsing document or query text, any call to xToken that
233433** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
233434** is considered to supply a synonym for the previous token. For example,
233435** when parsing the document "I won first place", a tokenizer that supports
233436** synonyms would call xToken() 5 times, as follows:
233437**
233438** <codeblock>
233439** xToken(pCtx, 0, "i", 1, 0, 1);
233440** xToken(pCtx, 0, "won", 3, 2, 5);
233441** xToken(pCtx, 0, "first", 5, 6, 11);
233442** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
233443** xToken(pCtx, 0, "place", 5, 12, 17);
233444**</codeblock>
233445**
233446** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
233447** xToken() is called. Multiple synonyms may be specified for a single token
233448** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
233449** There is no limit to the number of synonyms that may be provided for a
233450** single token.
233451**
233452** In many cases, method (1) above is the best approach. It does not add
233453** extra data to the FTS index or require FTS5 to query for multiple terms,
233454** so it is efficient in terms of disk space and query speed. However, it
233455** does not support prefix queries very well. If, as suggested above, the
233456** token "first" is substituted for "1st" by the tokenizer, then the query:
233457**
233458** <codeblock>
233459** ... MATCH '1s*'</codeblock>
233460**
233461** will not match documents that contain the token "1st" (as the tokenizer
233462** will probably not map "1s" to any prefix of "first").
233463**
233464** For full prefix support, method (3) may be preferred. In this case,
233465** because the index contains entries for both "first" and "1st", prefix
233466** queries such as 'fi*' or '1s*' will match correctly. However, because
233467** extra entries are added to the FTS index, this method uses more space
233468** within the database.
233469**
233470** Method (2) offers a midpoint between (1) and (3). Using this method,
233471** a query such as '1s*' will match documents that contain the literal
233472** token "1st", but not "first" (assuming the tokenizer is not able to
233473** provide synonyms for prefixes). However, a non-prefix query like '1st'
233474** will match against "1st" and "first". This method does not require
233475** extra disk space, as no extra entries are added to the FTS index.
233476** On the other hand, it may require more CPU cycles to run MATCH queries,
233477** as separate queries of the FTS index are required for each synonym.
233478**
233479** When using methods (2) or (3), it is important that the tokenizer only
233480** provide synonyms when tokenizing document text (method (3)) or query
233481** text (method (2)), not both. Doing so will not cause any errors, but is
233482** inefficient.
233483*/
233484typedef struct Fts5Tokenizer Fts5Tokenizer;
233485typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2;
233486struct fts5_tokenizer_v2 {
233487 int iVersion; /* Currently always 2 */
233488
233489 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
233490 void (*xDelete)(Fts5Tokenizer*);
233491 int (*xTokenize)(Fts5Tokenizer*,
233492 void *pCtx,
233493 int flags, /* Mask of FTS5_TOKENIZE_* flags */
233494 const char *pText, int nText,
233495 const char *pLocale, int nLocale,
233496 int (*xToken)(
233497 void *pCtx, /* Copy of 2nd argument to xTokenize() */
233498 int tflags, /* Mask of FTS5_TOKEN_* flags */
233499 const char *pToken, /* Pointer to buffer containing token */
233500 int nToken, /* Size of token in bytes */
233501 int iStart, /* Byte offset of token within input text */
233502 int iEnd /* Byte offset of end of token within input text */
233503 )
233504 );
233505};
233506
233507/*
233508** New code should use the fts5_tokenizer_v2 type to define tokenizer
233509** implementations. The following type is included for legacy applications
233510** that still use it.
233511*/
233512typedef struct fts5_tokenizer fts5_tokenizer;
233513struct fts5_tokenizer {
233514 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
233515 void (*xDelete)(Fts5Tokenizer*);
233516 int (*xTokenize)(Fts5Tokenizer*,
233517 void *pCtx,
233518 int flags, /* Mask of FTS5_TOKENIZE_* flags */
233519 const char *pText, int nText,
233520 int (*xToken)(
233521 void *pCtx, /* Copy of 2nd argument to xTokenize() */
233522 int tflags, /* Mask of FTS5_TOKEN_* flags */
233523 const char *pToken, /* Pointer to buffer containing token */
233524 int nToken, /* Size of token in bytes */
233525 int iStart, /* Byte offset of token within input text */
233526 int iEnd /* Byte offset of end of token within input text */
233527 )
233528 );
233529};
233530
233531
233532/* Flags that may be passed as the third argument to xTokenize() */
233533#define FTS5_TOKENIZE_QUERY0x0001 0x0001
233534#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
233535#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
233536#define FTS5_TOKENIZE_AUX0x0008 0x0008
233537
233538/* Flags that may be passed by the tokenizer implementation back to FTS5
233539** as the third argument to the supplied xToken callback. */
233540#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
233541
233542/*
233543** END OF CUSTOM TOKENIZERS
233544*************************************************************************/
233545
233546/*************************************************************************
233547** FTS5 EXTENSION REGISTRATION API
233548*/
233549typedef struct fts5_api fts5_api;
233550struct fts5_api {
233551 int iVersion; /* Currently always set to 3 */
233552
233553 /* Create a new tokenizer */
233554 int (*xCreateTokenizer)(
233555 fts5_api *pApi,
233556 const char *zName,
233557 void *pUserData,
233558 fts5_tokenizer *pTokenizer,
233559 void (*xDestroy)(void*)
233560 );
233561
233562 /* Find an existing tokenizer */
233563 int (*xFindTokenizer)(
233564 fts5_api *pApi,
233565 const char *zName,
233566 void **ppUserData,
233567 fts5_tokenizer *pTokenizer
233568 );
233569
233570 /* Create a new auxiliary function */
233571 int (*xCreateFunction)(
233572 fts5_api *pApi,
233573 const char *zName,
233574 void *pUserData,
233575 fts5_extension_function xFunction,
233576 void (*xDestroy)(void*)
233577 );
233578
233579 /* APIs below this point are only available if iVersion>=3 */
233580
233581 /* Create a new tokenizer */
233582 int (*xCreateTokenizer_v2)(
233583 fts5_api *pApi,
233584 const char *zName,
233585 void *pUserData,
233586 fts5_tokenizer_v2 *pTokenizer,
233587 void (*xDestroy)(void*)
233588 );
233589
233590 /* Find an existing tokenizer */
233591 int (*xFindTokenizer_v2)(
233592 fts5_api *pApi,
233593 const char *zName,
233594 void **ppUserData,
233595 fts5_tokenizer_v2 **ppTokenizer
233596 );
233597};
233598
233599/*
233600** END OF REGISTRATION API
233601*************************************************************************/
233602
233603#if 0
233604} /* end of the 'extern "C"' block */
233605#endif
233606
233607#endif /* _FTS5_H */
233608
233609/*
233610** 2014 May 31
233611**
233612** The author disclaims copyright to this source code. In place of
233613** a legal notice, here is a blessing:
233614**
233615** May you do good and not evil.
233616** May you find forgiveness for yourself and forgive others.
233617** May you share freely, never taking more than you give.
233618**
233619******************************************************************************
233620**
233621*/
233622#ifndef _FTS5INT_H
233623#define _FTS5INT_H
233624
233625/* #include "fts5.h" */
233626/* #include "sqlite3ext.h" */
233627SQLITE_EXTENSION_INIT1
233628
233629/* #include <string.h> */
233630/* #include <assert.h> */
233631
233632#ifndef SQLITE_AMALGAMATION1
233633
233634typedef unsigned char u8;
233635typedef unsigned int u32;
233636typedef unsigned short u16;
233637typedef short i16;
233638typedef sqlite3_int64 i64;
233639typedef sqlite3_uint64 u64;
233640
233641#ifndef ArraySize
233642# define ArraySize(x)((int)(sizeof(x)/sizeof(x[0]))) ((int)(sizeof(x) / sizeof(x[0])))
233643#endif
233644
233645#define testcase(x)
233646
233647#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
233648# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
233649#endif
233650#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
233651# define ALWAYS(X)(X) (1)
233652# define NEVER(X)(X) (0)
233653#elif !defined(NDEBUG1)
233654# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
233655# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
233656#else
233657# define ALWAYS(X)(X) (X)
233658# define NEVER(X)(X) (X)
233659#endif
233660
233661#define MIN(x,y)((x)<(y)?(x):(y)) (((x) < (y)) ? (x) : (y))
233662#define MAX(x,y)((x)>(y)?(x):(y)) (((x) > (y)) ? (x) : (y))
233663
233664/*
233665** Constants for the largest and smallest possible 64-bit signed integers.
233666*/
233667# define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
233668# define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
233669
233670/* The uptr type is an unsigned integer large enough to hold a pointer
233671*/
233672#if defined(HAVE_STDINT_H)
233673 typedef uintptr_t uptr;
233674#elif SQLITE_PTRSIZE8==4
233675 typedef u32 uptr;
233676#else
233677 typedef u64 uptr;
233678#endif
233679
233680#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
233681# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&3)==0)
233682#else
233683# define EIGHT_BYTE_ALIGNMENT(X)((((uptr)(X) - (uptr)0)&7)==0) ((((uptr)(X) - (uptr)0)&7)==0)
233684#endif
233685
233686#endif
233687
233688/* Truncate very long tokens to this many bytes. Hard limit is
233689** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
233690** field that occurs at the start of each leaf page (see fts5_index.c). */
233691#define FTS5_MAX_TOKEN_SIZE32768 32768
233692
233693/*
233694** Maximum number of prefix indexes on single FTS5 table. This must be
233695** less than 32. If it is set to anything large than that, an #error
233696** directive in fts5_index.c will cause the build to fail.
233697*/
233698#define FTS5_MAX_PREFIX_INDEXES31 31
233699
233700/*
233701** Maximum segments permitted in a single index
233702*/
233703#define FTS5_MAX_SEGMENT2000 2000
233704
233705#define FTS5_DEFAULT_NEARDIST10 10
233706#define FTS5_DEFAULT_RANK"bm25" "bm25"
233707
233708/* Name of rank and rowid columns */
233709#define FTS5_RANK_NAME"rank" "rank"
233710#define FTS5_ROWID_NAME"rowid" "rowid"
233711
233712#ifdef SQLITE_DEBUG
233713# define FTS5_CORRUPT(11 | (1<<8)) sqlite3Fts5Corrupt()
233714static int sqlite3Fts5Corrupt(void);
233715#else
233716# define FTS5_CORRUPT(11 | (1<<8)) SQLITE_CORRUPT_VTAB(11 | (1<<8))
233717#endif
233718
233719/*
233720** The assert_nc() macro is similar to the assert() macro, except that it
233721** is used for assert() conditions that are true only if it can be
233722** guranteed that the database is not corrupt.
233723*/
233724#ifdef SQLITE_DEBUG
233725SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
233726# define assert_nc(x)((void) (0)) assert(sqlite3_fts5_may_be_corrupt || (x))((void) (0))
233727#else
233728# define assert_nc(x)((void) (0)) assert(x)((void) (0))
233729#endif
233730
233731/*
233732** A version of memcmp() that does not cause asan errors if one of the pointer
233733** parameters is NULL and the number of bytes to compare is zero.
233734*/
233735#define fts5Memcmp(s1, s2, n)((n)<=0 ? 0 : memcmp((s1), (s2), (n))) ((n)<=0 ? 0 : memcmp((s1), (s2), (n)))
233736
233737/* Mark a function parameter as unused, to suppress nuisance compiler
233738** warnings. */
233739#ifndef UNUSED_PARAM
233740# define UNUSED_PARAM(X)(void)(X) (void)(X)
233741#endif
233742
233743#ifndef UNUSED_PARAM2
233744# define UNUSED_PARAM2(X, Y)(void)(X), (void)(Y) (void)(X), (void)(Y)
233745#endif
233746
233747typedef struct Fts5Global Fts5Global;
233748typedef struct Fts5Colset Fts5Colset;
233749
233750/* If a NEAR() clump or phrase may only match a specific set of columns,
233751** then an object of the following type is used to record the set of columns.
233752** Each entry in the aiCol[] array is a column that may be matched.
233753**
233754** This object is used by fts5_expr.c and fts5_index.c.
233755*/
233756struct Fts5Colset {
233757 int nCol;
233758 int aiCol[1];
233759};
233760
233761
233762
233763/**************************************************************************
233764** Interface to code in fts5_config.c. fts5_config.c contains contains code
233765** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
233766*/
233767
233768typedef struct Fts5Config Fts5Config;
233769typedef struct Fts5TokenizerConfig Fts5TokenizerConfig;
233770
233771struct Fts5TokenizerConfig {
233772 Fts5Tokenizer *pTok;
233773 fts5_tokenizer_v2 *pApi2;
233774 fts5_tokenizer *pApi1;
233775 const char **azArg;
233776 int nArg;
233777 int ePattern; /* FTS_PATTERN_XXX constant */
233778 const char *pLocale; /* Current locale to use */
233779 int nLocale; /* Size of pLocale in bytes */
233780};
233781
233782/*
233783** An instance of the following structure encodes all information that can
233784** be gleaned from the CREATE VIRTUAL TABLE statement.
233785**
233786** And all information loaded from the %_config table.
233787**
233788** nAutomerge:
233789** The minimum number of segments that an auto-merge operation should
233790** attempt to merge together. A value of 1 sets the object to use the
233791** compile time default. Zero disables auto-merge altogether.
233792**
233793** bContentlessDelete:
233794** True if the contentless_delete option was present in the CREATE
233795** VIRTUAL TABLE statement.
233796**
233797** zContent:
233798**
233799** zContentRowid:
233800** The value of the content_rowid= option, if one was specified. Or
233801** the string "rowid" otherwise. This text is not quoted - if it is
233802** used as part of an SQL statement it needs to be quoted appropriately.
233803**
233804** zContentExprlist:
233805**
233806** pzErrmsg:
233807** This exists in order to allow the fts5_index.c module to return a
233808** decent error message if it encounters a file-format version it does
233809** not understand.
233810**
233811** bColumnsize:
233812** True if the %_docsize table is created.
233813**
233814** bPrefixIndex:
233815** This is only used for debugging. If set to false, any prefix indexes
233816** are ignored. This value is configured using:
233817**
233818** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
233819**
233820** bLocale:
233821** Set to true if locale=1 was specified when the table was created.
233822*/
233823struct Fts5Config {
233824 sqlite3 *db; /* Database handle */
233825 Fts5Global *pGlobal; /* Global fts5 object for handle db */
233826 char *zDb; /* Database holding FTS index (e.g. "main") */
233827 char *zName; /* Name of FTS index */
233828 int nCol; /* Number of columns */
233829 char **azCol; /* Column names */
233830 u8 *abUnindexed; /* True for unindexed columns */
233831 int nPrefix; /* Number of prefix indexes */
233832 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
233833 int eContent; /* An FTS5_CONTENT value */
233834 int bContentlessDelete; /* "contentless_delete=" option (dflt==0) */
233835 int bContentlessUnindexed; /* "contentless_unindexed=" option (dflt=0) */
233836 char *zContent; /* content table */
233837 char *zContentRowid; /* "content_rowid=" option value */
233838 int bColumnsize; /* "columnsize=" option value (dflt==1) */
233839 int bTokendata; /* "tokendata=" option value (dflt==0) */
233840 int bLocale; /* "locale=" option value (dflt==0) */
233841 int eDetail; /* FTS5_DETAIL_XXX value */
233842 char *zContentExprlist;
233843 Fts5TokenizerConfig t;
233844 int bLock; /* True when table is preparing statement */
233845
233846
233847 /* Values loaded from the %_config table */
233848 int iVersion; /* fts5 file format 'version' */
233849 int iCookie; /* Incremented when %_config is modified */
233850 int pgsz; /* Approximate page size used in %_data */
233851 int nAutomerge; /* 'automerge' setting */
233852 int nCrisisMerge; /* Maximum allowed segments per level */
233853 int nUsermerge; /* 'usermerge' setting */
233854 int nHashSize; /* Bytes of memory for in-memory hash */
233855 char *zRank; /* Name of rank function */
233856 char *zRankArgs; /* Arguments to rank function */
233857 int bSecureDelete; /* 'secure-delete' */
233858 int nDeleteMerge; /* 'deletemerge' */
233859
233860 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
233861 char **pzErrmsg;
233862
233863#ifdef SQLITE_DEBUG
233864 int bPrefixIndex; /* True to use prefix-indexes */
233865#endif
233866};
233867
233868/* Current expected value of %_config table 'version' field. And
233869** the expected version if the 'secure-delete' option has ever been
233870** set on the table. */
233871#define FTS5_CURRENT_VERSION4 4
233872#define FTS5_CURRENT_VERSION_SECUREDELETE5 5
233873
233874#define FTS5_CONTENT_NORMAL0 0
233875#define FTS5_CONTENT_NONE1 1
233876#define FTS5_CONTENT_EXTERNAL2 2
233877#define FTS5_CONTENT_UNINDEXED3 3
233878
233879#define FTS5_DETAIL_FULL0 0
233880#define FTS5_DETAIL_NONE1 1
233881#define FTS5_DETAIL_COLUMNS2 2
233882
233883#define FTS5_PATTERN_NONE0 0
233884#define FTS5_PATTERN_LIKE65 65 /* matches SQLITE_INDEX_CONSTRAINT_LIKE */
233885#define FTS5_PATTERN_GLOB66 66 /* matches SQLITE_INDEX_CONSTRAINT_GLOB */
233886
233887static int sqlite3Fts5ConfigParse(
233888 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
233889);
233890static void sqlite3Fts5ConfigFree(Fts5Config*);
233891
233892static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
233893
233894static int sqlite3Fts5Tokenize(
233895 Fts5Config *pConfig, /* FTS5 Configuration object */
233896 int flags, /* FTS5_TOKENIZE_* flags */
233897 const char *pText, int nText, /* Text to tokenize */
233898 void *pCtx, /* Context passed to xToken() */
233899 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
233900);
233901
233902static void sqlite3Fts5Dequote(char *z);
233903
233904/* Load the contents of the %_config table */
233905static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
233906
233907/* Set the value of a single config attribute */
233908static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
233909
233910static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
233911
233912static void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...);
233913
233914/*
233915** End of interface to code in fts5_config.c.
233916**************************************************************************/
233917
233918/**************************************************************************
233919** Interface to code in fts5_buffer.c.
233920*/
233921
233922/*
233923** Buffer object for the incremental building of string data.
233924*/
233925typedef struct Fts5Buffer Fts5Buffer;
233926struct Fts5Buffer {
233927 u8 *p;
233928 int n;
233929 int nSpace;
233930};
233931
233932static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
233933static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
233934static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
233935static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
233936static void sqlite3Fts5BufferFree(Fts5Buffer*);
233937static void sqlite3Fts5BufferZero(Fts5Buffer*);
233938static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
233939static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
233940
233941static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
233942
233943#define fts5BufferZero(x)sqlite3Fts5BufferZero(x) sqlite3Fts5BufferZero(x)
233944#define fts5BufferAppendVarint(a,b,c)sqlite3Fts5BufferAppendVarint(a,b,(i64)c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c)
233945#define fts5BufferFree(a)sqlite3Fts5BufferFree(a) sqlite3Fts5BufferFree(a)
233946#define fts5BufferAppendBlob(a,b,c,d)sqlite3Fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
233947#define fts5BufferSet(a,b,c,d)sqlite3Fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
233948
233949#define fts5BufferGrow(pRc,pBuf,nn)( (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n)
)
( \
233950 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
233951 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
233952)
233953
233954/* Write and decode big-endian 32-bit integer values */
233955static void sqlite3Fts5Put32(u8*, int);
233956static int sqlite3Fts5Get32(const u8*);
233957
233958#define FTS5_POS2COLUMN(iPos)(int)((iPos >> 32) & 0x7FFFFFFF) (int)((iPos >> 32) & 0x7FFFFFFF)
233959#define FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF) (int)(iPos & 0x7FFFFFFF)
233960
233961typedef struct Fts5PoslistReader Fts5PoslistReader;
233962struct Fts5PoslistReader {
233963 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
233964 const u8 *a; /* Position list to iterate through */
233965 int n; /* Size of buffer at a[] in bytes */
233966 int i; /* Current offset in a[] */
233967
233968 u8 bFlag; /* For client use (any custom purpose) */
233969
233970 /* Output variables */
233971 u8 bEof; /* Set to true at EOF */
233972 i64 iPos; /* (iCol<<32) + iPos */
233973};
233974static int sqlite3Fts5PoslistReaderInit(
233975 const u8 *a, int n, /* Poslist buffer to iterate through */
233976 Fts5PoslistReader *pIter /* Iterator object to initialize */
233977);
233978static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
233979
233980typedef struct Fts5PoslistWriter Fts5PoslistWriter;
233981struct Fts5PoslistWriter {
233982 i64 iPrev;
233983};
233984static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
233985static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
233986
233987static int sqlite3Fts5PoslistNext64(
233988 const u8 *a, int n, /* Buffer containing poslist */
233989 int *pi, /* IN/OUT: Offset within a[] */
233990 i64 *piOff /* IN/OUT: Current offset */
233991);
233992
233993/* Malloc utility */
233994static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
233995static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
233996
233997/* Character set tests (like isspace(), isalpha() etc.) */
233998static int sqlite3Fts5IsBareword(char t);
233999
234000
234001/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
234002typedef struct Fts5Termset Fts5Termset;
234003static int sqlite3Fts5TermsetNew(Fts5Termset**);
234004static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
234005static void sqlite3Fts5TermsetFree(Fts5Termset*);
234006
234007/*
234008** End of interface to code in fts5_buffer.c.
234009**************************************************************************/
234010
234011/**************************************************************************
234012** Interface to code in fts5_index.c. fts5_index.c contains contains code
234013** to access the data stored in the %_data table.
234014*/
234015
234016typedef struct Fts5Index Fts5Index;
234017typedef struct Fts5IndexIter Fts5IndexIter;
234018
234019struct Fts5IndexIter {
234020 i64 iRowid;
234021 const u8 *pData;
234022 int nData;
234023 u8 bEof;
234024};
234025
234026#define sqlite3Fts5IterEof(x)((x)->bEof) ((x)->bEof)
234027
234028/*
234029** Values used as part of the flags argument passed to IndexQuery().
234030*/
234031#define FTS5INDEX_QUERY_PREFIX0x0001 0x0001 /* Prefix query */
234032#define FTS5INDEX_QUERY_DESC0x0002 0x0002 /* Docs in descending rowid order */
234033#define FTS5INDEX_QUERY_TEST_NOIDX0x0004 0x0004 /* Do not use prefix index */
234034#define FTS5INDEX_QUERY_SCAN0x0008 0x0008 /* Scan query (fts5vocab) */
234035
234036/* The following are used internally by the fts5_index.c module. They are
234037** defined here only to make it easier to avoid clashes with the flags
234038** above. */
234039#define FTS5INDEX_QUERY_SKIPEMPTY0x0010 0x0010
234040#define FTS5INDEX_QUERY_NOOUTPUT0x0020 0x0020
234041#define FTS5INDEX_QUERY_SKIPHASH0x0040 0x0040
234042#define FTS5INDEX_QUERY_NOTOKENDATA0x0080 0x0080
234043#define FTS5INDEX_QUERY_SCANONETERM0x0100 0x0100
234044
234045/*
234046** Create/destroy an Fts5Index object.
234047*/
234048static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
234049static int sqlite3Fts5IndexClose(Fts5Index *p);
234050
234051/*
234052** Return a simple checksum value based on the arguments.
234053*/
234054static u64 sqlite3Fts5IndexEntryCksum(
234055 i64 iRowid,
234056 int iCol,
234057 int iPos,
234058 int iIdx,
234059 const char *pTerm,
234060 int nTerm
234061);
234062
234063/*
234064** Argument p points to a buffer containing utf-8 text that is n bytes in
234065** size. Return the number of bytes in the nChar character prefix of the
234066** buffer, or 0 if there are less than nChar characters in total.
234067*/
234068static int sqlite3Fts5IndexCharlenToBytelen(
234069 const char *p,
234070 int nByte,
234071 int nChar
234072);
234073
234074/*
234075** Open a new iterator to iterate though all rowids that match the
234076** specified token or token prefix.
234077*/
234078static int sqlite3Fts5IndexQuery(
234079 Fts5Index *p, /* FTS index to query */
234080 const char *pToken, int nToken, /* Token (or prefix) to query for */
234081 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
234082 Fts5Colset *pColset, /* Match these columns only */
234083 Fts5IndexIter **ppIter /* OUT: New iterator object */
234084);
234085
234086/*
234087** The various operations on open token or token prefix iterators opened
234088** using sqlite3Fts5IndexQuery().
234089*/
234090static int sqlite3Fts5IterNext(Fts5IndexIter*);
234091static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
234092
234093/*
234094** Close an iterator opened by sqlite3Fts5IndexQuery().
234095*/
234096static void sqlite3Fts5IterClose(Fts5IndexIter*);
234097
234098/*
234099** Close the reader blob handle, if it is open.
234100*/
234101static void sqlite3Fts5IndexCloseReader(Fts5Index*);
234102
234103/*
234104** This interface is used by the fts5vocab module.
234105*/
234106static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
234107static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
234108static void *sqlite3Fts5StructureRef(Fts5Index*);
234109static void sqlite3Fts5StructureRelease(void*);
234110static int sqlite3Fts5StructureTest(Fts5Index*, void*);
234111
234112/*
234113** Used by xInstToken():
234114*/
234115static int sqlite3Fts5IterToken(Fts5IndexIter*, i64, int, int, const char**, int*);
234116
234117/*
234118** Insert or remove data to or from the index. Each time a document is
234119** added to or removed from the index, this function is called one or more
234120** times.
234121**
234122** For an insert, it must be called once for each token in the new document.
234123** If the operation is a delete, it must be called (at least) once for each
234124** unique token in the document with an iCol value less than zero. The iPos
234125** argument is ignored for a delete.
234126*/
234127static int sqlite3Fts5IndexWrite(
234128 Fts5Index *p, /* Index to write to */
234129 int iCol, /* Column token appears in (-ve -> delete) */
234130 int iPos, /* Position of token within column */
234131 const char *pToken, int nToken /* Token to add or remove to or from index */
234132);
234133
234134/*
234135** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
234136** document iDocid.
234137*/
234138static int sqlite3Fts5IndexBeginWrite(
234139 Fts5Index *p, /* Index to write to */
234140 int bDelete, /* True if current operation is a delete */
234141 i64 iDocid /* Docid to add or remove data from */
234142);
234143
234144/*
234145** Flush any data stored in the in-memory hash tables to the database.
234146** Also close any open blob handles.
234147*/
234148static int sqlite3Fts5IndexSync(Fts5Index *p);
234149
234150/*
234151** Discard any data stored in the in-memory hash tables. Do not write it
234152** to the database. Additionally, assume that the contents of the %_data
234153** table may have changed on disk. So any in-memory caches of %_data
234154** records must be invalidated.
234155*/
234156static int sqlite3Fts5IndexRollback(Fts5Index *p);
234157
234158/*
234159** Get or set the "averages" values.
234160*/
234161static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
234162static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
234163
234164/*
234165** Functions called by the storage module as part of integrity-check.
234166*/
234167static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum, int bUseCksum);
234168
234169/*
234170** Called during virtual module initialization to register UDF
234171** fts5_decode() with SQLite
234172*/
234173static int sqlite3Fts5IndexInit(sqlite3*);
234174
234175static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
234176
234177/*
234178** Return the total number of entries read from the %_data table by
234179** this connection since it was created.
234180*/
234181static int sqlite3Fts5IndexReads(Fts5Index *p);
234182
234183static int sqlite3Fts5IndexReinit(Fts5Index *p);
234184static int sqlite3Fts5IndexOptimize(Fts5Index *p);
234185static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
234186static int sqlite3Fts5IndexReset(Fts5Index *p);
234187
234188static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
234189
234190static int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin);
234191static int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid);
234192
234193static void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter*);
234194
234195/* Used to populate hash tables for xInstToken in detail=none/column mode. */
234196static int sqlite3Fts5IndexIterWriteTokendata(
234197 Fts5IndexIter*, const char*, int, i64 iRowid, int iCol, int iOff
234198);
234199
234200/*
234201** End of interface to code in fts5_index.c.
234202**************************************************************************/
234203
234204/**************************************************************************
234205** Interface to code in fts5_varint.c.
234206*/
234207static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
234208static int sqlite3Fts5GetVarintLen(u32 iVal);
234209static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
234210static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
234211
234212#define fts5GetVarint32(a,b)sqlite3Fts5GetVarint32(a,(u32*)&(b)) sqlite3Fts5GetVarint32(a,(u32*)&(b))
234213#define fts5GetVarintsqlite3Fts5GetVarint sqlite3Fts5GetVarint
234214
234215#define fts5FastGetVarint32(a, iOff, nVal){ nVal = (a)[iOff++]; if( nVal & 0x80 ){ iOff--; iOff += sqlite3Fts5GetVarint32
(&(a)[iOff],(u32*)&(nVal)); } }
{ \
234216 nVal = (a)[iOff++]; \
234217 if( nVal & 0x80 ){ \
234218 iOff--; \
234219 iOff += fts5GetVarint32(&(a)[iOff], nVal)sqlite3Fts5GetVarint32(&(a)[iOff],(u32*)&(nVal)); \
234220 } \
234221}
234222
234223
234224/*
234225** End of interface to code in fts5_varint.c.
234226**************************************************************************/
234227
234228
234229/**************************************************************************
234230** Interface to code in fts5_main.c.
234231*/
234232
234233/*
234234** Virtual-table object.
234235*/
234236typedef struct Fts5Table Fts5Table;
234237struct Fts5Table {
234238 sqlite3_vtab base; /* Base class used by SQLite core */
234239 Fts5Config *pConfig; /* Virtual table configuration */
234240 Fts5Index *pIndex; /* Full-text index */
234241};
234242
234243static int sqlite3Fts5LoadTokenizer(Fts5Config *pConfig);
234244
234245static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
234246
234247static int sqlite3Fts5FlushToDisk(Fts5Table*);
234248
234249static void sqlite3Fts5ClearLocale(Fts5Config *pConfig);
234250static void sqlite3Fts5SetLocale(Fts5Config *pConfig, const char *pLoc, int nLoc);
234251
234252static int sqlite3Fts5IsLocaleValue(Fts5Config *pConfig, sqlite3_value *pVal);
234253static int sqlite3Fts5DecodeLocaleValue(sqlite3_value *pVal,
234254 const char **ppText, int *pnText, const char **ppLoc, int *pnLoc
234255);
234256
234257/*
234258** End of interface to code in fts5.c.
234259**************************************************************************/
234260
234261/**************************************************************************
234262** Interface to code in fts5_hash.c.
234263*/
234264typedef struct Fts5Hash Fts5Hash;
234265
234266/*
234267** Create a hash table, free a hash table.
234268*/
234269static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
234270static void sqlite3Fts5HashFree(Fts5Hash*);
234271
234272static int sqlite3Fts5HashWrite(
234273 Fts5Hash*,
234274 i64 iRowid, /* Rowid for this entry */
234275 int iCol, /* Column token appears in (-ve -> delete) */
234276 int iPos, /* Position of token within column */
234277 char bByte,
234278 const char *pToken, int nToken /* Token to add or remove to or from index */
234279);
234280
234281/*
234282** Empty (but do not delete) a hash table.
234283*/
234284static void sqlite3Fts5HashClear(Fts5Hash*);
234285
234286/*
234287** Return true if the hash is empty, false otherwise.
234288*/
234289static int sqlite3Fts5HashIsEmpty(Fts5Hash*);
234290
234291static int sqlite3Fts5HashQuery(
234292 Fts5Hash*, /* Hash table to query */
234293 int nPre,
234294 const char *pTerm, int nTerm, /* Query term */
234295 void **ppObj, /* OUT: Pointer to doclist for pTerm */
234296 int *pnDoclist /* OUT: Size of doclist in bytes */
234297);
234298
234299static int sqlite3Fts5HashScanInit(
234300 Fts5Hash*, /* Hash table to query */
234301 const char *pTerm, int nTerm /* Query prefix */
234302);
234303static void sqlite3Fts5HashScanNext(Fts5Hash*);
234304static int sqlite3Fts5HashScanEof(Fts5Hash*);
234305static void sqlite3Fts5HashScanEntry(Fts5Hash *,
234306 const char **pzTerm, /* OUT: term (nul-terminated) */
234307 int *pnTerm, /* OUT: Size of term in bytes */
234308 const u8 **ppDoclist, /* OUT: pointer to doclist */
234309 int *pnDoclist /* OUT: size of doclist in bytes */
234310);
234311
234312
234313
234314/*
234315** End of interface to code in fts5_hash.c.
234316**************************************************************************/
234317
234318/**************************************************************************
234319** Interface to code in fts5_storage.c. fts5_storage.c contains contains
234320** code to access the data stored in the %_content and %_docsize tables.
234321*/
234322
234323#define FTS5_STMT_SCAN_ASC0 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
234324#define FTS5_STMT_SCAN_DESC1 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
234325#define FTS5_STMT_LOOKUP2 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
234326
234327typedef struct Fts5Storage Fts5Storage;
234328
234329static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
234330static int sqlite3Fts5StorageClose(Fts5Storage *p);
234331static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
234332
234333static int sqlite3Fts5DropAll(Fts5Config*);
234334static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
234335
234336static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**, int);
234337static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, int, sqlite3_value**, i64*);
234338static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
234339
234340static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg);
234341
234342static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
234343static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
234344
234345static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
234346static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
234347static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
234348
234349static int sqlite3Fts5StorageSync(Fts5Storage *p);
234350static int sqlite3Fts5StorageRollback(Fts5Storage *p);
234351
234352static int sqlite3Fts5StorageConfigValue(
234353 Fts5Storage *p, const char*, sqlite3_value*, int
234354);
234355
234356static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
234357static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
234358static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
234359static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
234360static int sqlite3Fts5StorageReset(Fts5Storage *p);
234361
234362static void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage*);
234363static int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel);
234364
234365/*
234366** End of interface to code in fts5_storage.c.
234367**************************************************************************/
234368
234369
234370/**************************************************************************
234371** Interface to code in fts5_expr.c.
234372*/
234373typedef struct Fts5Expr Fts5Expr;
234374typedef struct Fts5ExprNode Fts5ExprNode;
234375typedef struct Fts5Parse Fts5Parse;
234376typedef struct Fts5Token Fts5Token;
234377typedef struct Fts5ExprPhrase Fts5ExprPhrase;
234378typedef struct Fts5ExprNearset Fts5ExprNearset;
234379
234380struct Fts5Token {
234381 const char *p; /* Token text (not NULL terminated) */
234382 int n; /* Size of buffer p in bytes */
234383};
234384
234385/* Parse a MATCH expression. */
234386static int sqlite3Fts5ExprNew(
234387 Fts5Config *pConfig,
234388 int bPhraseToAnd,
234389 int iCol, /* Column on LHS of MATCH operator */
234390 const char *zExpr,
234391 Fts5Expr **ppNew,
234392 char **pzErr
234393);
234394static int sqlite3Fts5ExprPattern(
234395 Fts5Config *pConfig,
234396 int bGlob,
234397 int iCol,
234398 const char *zText,
234399 Fts5Expr **pp
234400);
234401
234402/*
234403** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
234404** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
234405** rc = sqlite3Fts5ExprNext(pExpr)
234406** ){
234407** // The document with rowid iRowid matches the expression!
234408** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
234409** }
234410*/
234411static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
234412static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
234413static int sqlite3Fts5ExprEof(Fts5Expr*);
234414static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
234415
234416static void sqlite3Fts5ExprFree(Fts5Expr*);
234417static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
234418
234419/* Called during startup to register a UDF with SQLite */
234420static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
234421
234422static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
234423static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
234424static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
234425
234426typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
234427static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
234428static int sqlite3Fts5ExprPopulatePoslists(
234429 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
234430);
234431static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
234432
234433static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
234434
234435static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
234436
234437static int sqlite3Fts5ExprQueryToken(Fts5Expr*, int, int, const char**, int*);
234438static int sqlite3Fts5ExprInstToken(Fts5Expr*, i64, int, int, int, int, const char**, int*);
234439static void sqlite3Fts5ExprClearTokens(Fts5Expr*);
234440
234441/*******************************************
234442** The fts5_expr.c API above this point is used by the other hand-written
234443** C code in this module. The interfaces below this point are called by
234444** the parser code in fts5parse.y. */
234445
234446static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
234447
234448static Fts5ExprNode *sqlite3Fts5ParseNode(
234449 Fts5Parse *pParse,
234450 int eType,
234451 Fts5ExprNode *pLeft,
234452 Fts5ExprNode *pRight,
234453 Fts5ExprNearset *pNear
234454);
234455
234456static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
234457 Fts5Parse *pParse,
234458 Fts5ExprNode *pLeft,
234459 Fts5ExprNode *pRight
234460);
234461
234462static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
234463 Fts5Parse *pParse,
234464 Fts5ExprPhrase *pPhrase,
234465 Fts5Token *pToken,
234466 int bPrefix
234467);
234468
234469static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
234470
234471static Fts5ExprNearset *sqlite3Fts5ParseNearset(
234472 Fts5Parse*,
234473 Fts5ExprNearset*,
234474 Fts5ExprPhrase*
234475);
234476
234477static Fts5Colset *sqlite3Fts5ParseColset(
234478 Fts5Parse*,
234479 Fts5Colset*,
234480 Fts5Token *
234481);
234482
234483static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
234484static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
234485static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
234486
234487static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
234488static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
234489static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
234490static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
234491static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
234492
234493/*
234494** End of interface to code in fts5_expr.c.
234495**************************************************************************/
234496
234497
234498
234499/**************************************************************************
234500** Interface to code in fts5_aux.c.
234501*/
234502
234503static int sqlite3Fts5AuxInit(fts5_api*);
234504/*
234505** End of interface to code in fts5_aux.c.
234506**************************************************************************/
234507
234508/**************************************************************************
234509** Interface to code in fts5_tokenizer.c.
234510*/
234511
234512static int sqlite3Fts5TokenizerInit(fts5_api*);
234513static int sqlite3Fts5TokenizerPattern(
234514 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
234515 Fts5Tokenizer *pTok
234516);
234517static int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig*);
234518/*
234519** End of interface to code in fts5_tokenizer.c.
234520**************************************************************************/
234521
234522/**************************************************************************
234523** Interface to code in fts5_vocab.c.
234524*/
234525
234526static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
234527
234528/*
234529** End of interface to code in fts5_vocab.c.
234530**************************************************************************/
234531
234532
234533/**************************************************************************
234534** Interface to automatically generated code in fts5_unicode2.c.
234535*/
234536static int sqlite3Fts5UnicodeIsdiacritic(int c);
234537static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
234538
234539static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
234540static int sqlite3Fts5UnicodeCategory(u32 iCode);
234541static void sqlite3Fts5UnicodeAscii(u8*, u8*);
234542/*
234543** End of interface to code in fts5_unicode2.c.
234544**************************************************************************/
234545
234546#endif
234547
234548#define FTS5_OR1 1
234549#define FTS5_AND2 2
234550#define FTS5_NOT3 3
234551#define FTS5_TERM4 4
234552#define FTS5_COLON5 5
234553#define FTS5_MINUS6 6
234554#define FTS5_LCP7 7
234555#define FTS5_RCP8 8
234556#define FTS5_STRING9 9
234557#define FTS5_LP10 10
234558#define FTS5_RP11 11
234559#define FTS5_CARET12 12
234560#define FTS5_COMMA13 13
234561#define FTS5_PLUS14 14
234562#define FTS5_STAR15 15
234563
234564/* This file is automatically generated by Lemon from input grammar
234565** source file "fts5parse.y".
234566*/
234567/*
234568** 2000-05-29
234569**
234570** The author disclaims copyright to this source code. In place of
234571** a legal notice, here is a blessing:
234572**
234573** May you do good and not evil.
234574** May you find forgiveness for yourself and forgive others.
234575** May you share freely, never taking more than you give.
234576**
234577*************************************************************************
234578** Driver template for the LEMON parser generator.
234579**
234580** The "lemon" program processes an LALR(1) input grammar file, then uses
234581** this template to construct a parser. The "lemon" program inserts text
234582** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
234583** interstitial "-" characters) contained in this template is changed into
234584** the value of the %name directive from the grammar. Otherwise, the content
234585** of this template is copied straight through into the generate parser
234586** source file.
234587**
234588** The following is the concatenation of all %include directives from the
234589** input grammar file:
234590*/
234591/************ Begin %include sections from the grammar ************************/
234592
234593/* #include "fts5Int.h" */
234594/* #include "fts5parse.h" */
234595
234596/*
234597** Disable all error recovery processing in the parser push-down
234598** automaton.
234599*/
234600#define fts5YYNOERRORRECOVERY1 1
234601
234602/*
234603** Make fts5yytestcase() the same as testcase()
234604*/
234605#define fts5yytestcase(X) testcase(X)
234606
234607/*
234608** Indicate that sqlite3ParserFree() will never be called with a null
234609** pointer.
234610*/
234611#define fts5YYPARSEFREENOTNULL1 1
234612
234613/*
234614** Alternative datatype for the argument to the malloc() routine passed
234615** into sqlite3ParserAlloc(). The default is size_t.
234616*/
234617#define fts5YYMALLOCARGTYPEu64 u64
234618
234619/**************** End of %include directives **********************************/
234620/* These constants specify the various numeric values for terminal symbols.
234621***************** Begin token definitions *************************************/
234622#ifndef FTS5_OR1
234623#define FTS5_OR1 1
234624#define FTS5_AND2 2
234625#define FTS5_NOT3 3
234626#define FTS5_TERM4 4
234627#define FTS5_COLON5 5
234628#define FTS5_MINUS6 6
234629#define FTS5_LCP7 7
234630#define FTS5_RCP8 8
234631#define FTS5_STRING9 9
234632#define FTS5_LP10 10
234633#define FTS5_RP11 11
234634#define FTS5_CARET12 12
234635#define FTS5_COMMA13 13
234636#define FTS5_PLUS14 14
234637#define FTS5_STAR15 15
234638#endif
234639/**************** End token definitions ***************************************/
234640
234641/* The next sections is a series of control #defines.
234642** various aspects of the generated parser.
234643** fts5YYCODETYPE is the data type used to store the integer codes
234644** that represent terminal and non-terminal symbols.
234645** "unsigned char" is used if there are fewer than
234646** 256 symbols. Larger types otherwise.
234647** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
234648** any terminal or nonterminal symbol.
234649** fts5YYFALLBACK If defined, this indicates that one or more tokens
234650** (also known as: "terminal symbols") have fall-back
234651** values which should be used if the original symbol
234652** would not parse. This permits keywords to sometimes
234653** be used as identifiers, for example.
234654** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
234655** that indicate what to do in response to the next
234656** token.
234657** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
234658** symbols. Background: A "minor type" is a semantic
234659** value associated with a terminal or non-terminal
234660** symbols. For example, for an "ID" terminal symbol,
234661** the minor type might be the name of the identifier.
234662** Each non-terminal can have a different minor type.
234663** Terminal symbols all have the same minor type, though.
234664** This macros defines the minor type for terminal
234665** symbols.
234666** fts5YYMINORTYPE is the data type used for all minor types.
234667** This is typically a union of many types, one of
234668** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
234669** for terminal symbols is called "fts5yy0".
234670** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
234671** zero the stack is dynamically sized using realloc()
234672** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
234673** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
234674** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
234675** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
234676** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
234677** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
234678** fts5YYREALLOC Name of the realloc() function to use
234679** fts5YYFREE Name of the free() function to use
234680** fts5YYDYNSTACK True if stack space should be extended on heap
234681** fts5YYERRORSYMBOL is the code number of the error symbol. If not
234682** defined, then do no error processing.
234683** fts5YYNSTATE the combined number of states.
234684** fts5YYNRULE the number of rules in the grammar
234685** fts5YYNFTS5TOKEN Number of terminal symbols
234686** fts5YY_MAX_SHIFT Maximum value for shift actions
234687** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
234688** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
234689** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
234690** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
234691** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
234692** fts5YY_MIN_REDUCE Minimum value for reduce actions
234693** fts5YY_MAX_REDUCE Maximum value for reduce actions
234694** fts5YY_MIN_DSTRCTR Minimum symbol value that has a destructor
234695** fts5YY_MAX_DSTRCTR Maximum symbol value that has a destructor
234696*/
234697#ifndef INTERFACE1
234698# define INTERFACE1 1
234699#endif
234700/************* Begin control #defines *****************************************/
234701#define fts5YYCODETYPEunsigned char unsigned char
234702#define fts5YYNOCODE27 27
234703#define fts5YYACTIONTYPEunsigned char unsigned char
234704#define sqlite3Fts5ParserFTS5TOKENTYPEFts5Token Fts5Token
234705typedef union {
234706 int fts5yyinit;
234707 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yy0;
234708 int fts5yy4;
234709 Fts5Colset* fts5yy11;
234710 Fts5ExprNode* fts5yy24;
234711 Fts5ExprNearset* fts5yy46;
234712 Fts5ExprPhrase* fts5yy53;
234713} fts5YYMINORTYPE;
234714#ifndef fts5YYSTACKDEPTH100
234715#define fts5YYSTACKDEPTH100 100
234716#endif
234717#define sqlite3Fts5ParserARG_SDECLFts5Parse *pParse; Fts5Parse *pParse;
234718#define sqlite3Fts5ParserARG_PDECL,Fts5Parse *pParse ,Fts5Parse *pParse
234719#define sqlite3Fts5ParserARG_PARAM,pParse ,pParse
234720#define sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse; Fts5Parse *pParse=fts5yypParser->pParse;
234721#define sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; fts5yypParser->pParse=pParse;
234722#define fts5YYREALLOCrealloc realloc
234723#define fts5YYFREEfree free
234724#define fts5YYDYNSTACK0 0
234725#define sqlite3Fts5ParserCTX_SDECL
234726#define sqlite3Fts5ParserCTX_PDECL
234727#define sqlite3Fts5ParserCTX_PARAM
234728#define sqlite3Fts5ParserCTX_FETCH
234729#define sqlite3Fts5ParserCTX_STORE
234730#define fts5YYNSTATE35 35
234731#define fts5YYNRULE28 28
234732#define fts5YYNRULE_WITH_ACTION28 28
234733#define fts5YYNFTS5TOKEN16 16
234734#define fts5YY_MAX_SHIFT34 34
234735#define fts5YY_MIN_SHIFTREDUCE52 52
234736#define fts5YY_MAX_SHIFTREDUCE79 79
234737#define fts5YY_ERROR_ACTION80 80
234738#define fts5YY_ACCEPT_ACTION81 81
234739#define fts5YY_NO_ACTION82 82
234740#define fts5YY_MIN_REDUCE83 83
234741#define fts5YY_MAX_REDUCE110 110
234742#define fts5YY_MIN_DSTRCTR16 16
234743#define fts5YY_MAX_DSTRCTR24 24
234744/************* End control #defines *******************************************/
234745#define fts5YY_NLOOKAHEAD((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0]))) ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
234746
234747/* Define the fts5yytestcase() macro to be a no-op if is not already defined
234748** otherwise.
234749**
234750** Applications can choose to define fts5yytestcase() in the %include section
234751** to a macro that can assist in verifying code coverage. For production
234752** code the fts5yytestcase() macro should be turned off. But it is useful
234753** for testing.
234754*/
234755#ifndef fts5yytestcase
234756# define fts5yytestcase(X)
234757#endif
234758
234759/* Macro to determine if stack space has the ability to grow using
234760** heap memory.
234761*/
234762#if fts5YYSTACKDEPTH100<=0 || fts5YYDYNSTACK0
234763# define fts5YYGROWABLESTACK0 1
234764#else
234765# define fts5YYGROWABLESTACK0 0
234766#endif
234767
234768/* Guarantee a minimum number of initial stack slots.
234769*/
234770#if fts5YYSTACKDEPTH100<=0
234771# undef fts5YYSTACKDEPTH100
234772# define fts5YYSTACKDEPTH100 2 /* Need a minimum stack size */
234773#endif
234774
234775
234776/* Next are the tables used to determine what action to take based on the
234777** current state and lookahead token. These tables are used to implement
234778** functions that take a state number and lookahead value and return an
234779** action integer.
234780**
234781** Suppose the action integer is N. Then the action is determined as
234782** follows
234783**
234784** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
234785** token onto the stack and goto state N.
234786**
234787** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
234788** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
234789**
234790** N == fts5YY_ERROR_ACTION A syntax error has occurred.
234791**
234792** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
234793**
234794** N == fts5YY_NO_ACTION No such action. Denotes unused
234795** slots in the fts5yy_action[] table.
234796**
234797** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
234798** and fts5YY_MAX_REDUCE
234799**
234800** The action table is constructed as a single large table named fts5yy_action[].
234801** Given state S and lookahead X, the action is computed as either:
234802**
234803** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
234804** (B) N = fts5yy_default[S]
234805**
234806** The (A) formula is preferred. The B formula is used instead if
234807** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
234808**
234809** The formulas above are for computing the action when the lookahead is
234810** a terminal symbol. If the lookahead is a non-terminal (as occurs after
234811** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
234812** the fts5yy_shift_ofst[] array.
234813**
234814** The following are the tables generated in this section:
234815**
234816** fts5yy_action[] A single table containing all actions.
234817** fts5yy_lookahead[] A table containing the lookahead for each entry in
234818** fts5yy_action. Used to detect hash collisions.
234819** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
234820** shifting terminals.
234821** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
234822** shifting non-terminals after a reduce.
234823** fts5yy_default[] Default action for each state.
234824**
234825*********** Begin parsing tables **********************************************/
234826#define fts5YY_ACTTAB_COUNT(105) (105)
234827static const fts5YYACTIONTYPEunsigned char fts5yy_action[] = {
234828 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
234829 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
234830 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
234831 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
234832 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
234833 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
234834 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
234835 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
234836 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
234837 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
234838 /* 100 */ 57, 15, 82, 82, 71,
234839};
234840static const fts5YYCODETYPEunsigned char fts5yy_lookahead[] = {
234841 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
234842 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
234843 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
234844 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
234845 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
234846 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
234847 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
234848 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
234849 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
234850 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
234851 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
234852 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
234853 /* 120 */ 27,
234854};
234855#define fts5YY_SHIFT_COUNT(34) (34)
234856#define fts5YY_SHIFT_MIN(0) (0)
234857#define fts5YY_SHIFT_MAX(93) (93)
234858static const unsigned char fts5yy_shift_ofst[] = {
234859 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
234860 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
234861 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
234862 /* 30 */ 53, 87, 92, 53, 93,
234863};
234864#define fts5YY_REDUCE_COUNT(17) (17)
234865#define fts5YY_REDUCE_MIN(-17) (-17)
234866#define fts5YY_REDUCE_MAX(67) (67)
234867static const signed char fts5yy_reduce_ofst[] = {
234868 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
234869 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
234870};
234871static const fts5YYACTIONTYPEunsigned char fts5yy_default[] = {
234872 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
234873 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
234874 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
234875 /* 30 */ 103, 80, 80, 104, 80,
234876};
234877/********** End of lemon-generated parsing tables *****************************/
234878
234879/* The next table maps tokens (terminal symbols) into fallback tokens.
234880** If a construct like the following:
234881**
234882** %fallback ID X Y Z.
234883**
234884** appears in the grammar, then ID becomes a fallback token for X, Y,
234885** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
234886** but it does not parse, the type of the token is changed to ID and
234887** the parse is retried before an error is thrown.
234888**
234889** This feature can be used, for example, to cause some keywords in a language
234890** to revert to identifiers if they keyword does not apply in the context where
234891** it appears.
234892*/
234893#ifdef fts5YYFALLBACK
234894static const fts5YYCODETYPEunsigned char fts5yyFallback[] = {
234895};
234896#endif /* fts5YYFALLBACK */
234897
234898/* The following structure represents a single element of the
234899** parser's stack. Information stored includes:
234900**
234901** + The state number for the parser at this level of the stack.
234902**
234903** + The value of the token stored at this level of the stack.
234904** (In other words, the "major" token.)
234905**
234906** + The semantic value stored at this level of the stack. This is
234907** the information used by the action routines in the grammar.
234908** It is sometimes called the "minor" token.
234909**
234910** After the "shift" half of a SHIFTREDUCE action, the stateno field
234911** actually contains the reduce action for the second half of the
234912** SHIFTREDUCE.
234913*/
234914struct fts5yyStackEntry {
234915 fts5YYACTIONTYPEunsigned char stateno; /* The state-number, or reduce action in SHIFTREDUCE */
234916 fts5YYCODETYPEunsigned char major; /* The major token value. This is the code
234917 ** number for the token at this stack level */
234918 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
234919 ** is the value of the token */
234920};
234921typedef struct fts5yyStackEntry fts5yyStackEntry;
234922
234923/* The state of the parser is completely contained in an instance of
234924** the following structure */
234925struct fts5yyParser {
234926 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
234927#ifdef fts5YYTRACKMAXSTACKDEPTH
234928 int fts5yyhwm; /* High-water mark of the stack */
234929#endif
234930#ifndef fts5YYNOERRORRECOVERY1
234931 int fts5yyerrcnt; /* Shifts left before out of the error */
234932#endif
234933 sqlite3Fts5ParserARG_SDECLFts5Parse *pParse; /* A place to hold %extra_argument */
234934 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
234935 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
234936 fts5yyStackEntry *fts5yystack; /* The parser stack */
234937 fts5yyStackEntry fts5yystk0[fts5YYSTACKDEPTH100]; /* Initial stack space */
234938};
234939typedef struct fts5yyParser fts5yyParser;
234940
234941/* #include <assert.h> */
234942#ifndef NDEBUG1
234943/* #include <stdio.h> */
234944static FILE *fts5yyTraceFILE = 0;
234945static char *fts5yyTracePrompt = 0;
234946#endif /* NDEBUG */
234947
234948#ifndef NDEBUG1
234949/*
234950** Turn parser tracing on by giving a stream to which to write the trace
234951** and a prompt to preface each trace message. Tracing is turned off
234952** by making either argument NULL
234953**
234954** Inputs:
234955** <ul>
234956** <li> A FILE* to which trace output should be written.
234957** If NULL, then tracing is turned off.
234958** <li> A prefix string written at the beginning of every
234959** line of trace output. If NULL, then tracing is
234960** turned off.
234961** </ul>
234962**
234963** Outputs:
234964** None.
234965*/
234966static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
234967 fts5yyTraceFILE = TraceFILE;
234968 fts5yyTracePrompt = zTracePrompt;
234969 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
234970 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
234971}
234972#endif /* NDEBUG */
234973
234974#if defined(fts5YYCOVERAGE) || !defined(NDEBUG1)
234975/* For tracing shifts, the names of all terminals and nonterminals
234976** are required. The following table supplies these names */
234977static const char *const fts5yyTokenName[] = {
234978 /* 0 */ "$",
234979 /* 1 */ "OR",
234980 /* 2 */ "AND",
234981 /* 3 */ "NOT",
234982 /* 4 */ "TERM",
234983 /* 5 */ "COLON",
234984 /* 6 */ "MINUS",
234985 /* 7 */ "LCP",
234986 /* 8 */ "RCP",
234987 /* 9 */ "STRING",
234988 /* 10 */ "LP",
234989 /* 11 */ "RP",
234990 /* 12 */ "CARET",
234991 /* 13 */ "COMMA",
234992 /* 14 */ "PLUS",
234993 /* 15 */ "STAR",
234994 /* 16 */ "input",
234995 /* 17 */ "expr",
234996 /* 18 */ "cnearset",
234997 /* 19 */ "exprlist",
234998 /* 20 */ "colset",
234999 /* 21 */ "colsetlist",
235000 /* 22 */ "nearset",
235001 /* 23 */ "nearphrases",
235002 /* 24 */ "phrase",
235003 /* 25 */ "neardist_opt",
235004 /* 26 */ "star_opt",
235005};
235006#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
235007
235008#ifndef NDEBUG1
235009/* For tracing reduce actions, the names of all rules are required.
235010*/
235011static const char *const fts5yyRuleName[] = {
235012 /* 0 */ "input ::= expr",
235013 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
235014 /* 2 */ "colset ::= LCP colsetlist RCP",
235015 /* 3 */ "colset ::= STRING",
235016 /* 4 */ "colset ::= MINUS STRING",
235017 /* 5 */ "colsetlist ::= colsetlist STRING",
235018 /* 6 */ "colsetlist ::= STRING",
235019 /* 7 */ "expr ::= expr AND expr",
235020 /* 8 */ "expr ::= expr OR expr",
235021 /* 9 */ "expr ::= expr NOT expr",
235022 /* 10 */ "expr ::= colset COLON LP expr RP",
235023 /* 11 */ "expr ::= LP expr RP",
235024 /* 12 */ "expr ::= exprlist",
235025 /* 13 */ "exprlist ::= cnearset",
235026 /* 14 */ "exprlist ::= exprlist cnearset",
235027 /* 15 */ "cnearset ::= nearset",
235028 /* 16 */ "cnearset ::= colset COLON nearset",
235029 /* 17 */ "nearset ::= phrase",
235030 /* 18 */ "nearset ::= CARET phrase",
235031 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
235032 /* 20 */ "nearphrases ::= phrase",
235033 /* 21 */ "nearphrases ::= nearphrases phrase",
235034 /* 22 */ "neardist_opt ::=",
235035 /* 23 */ "neardist_opt ::= COMMA STRING",
235036 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
235037 /* 25 */ "phrase ::= STRING star_opt",
235038 /* 26 */ "star_opt ::= STAR",
235039 /* 27 */ "star_opt ::=",
235040};
235041#endif /* NDEBUG */
235042
235043
235044#if fts5YYGROWABLESTACK0
235045/*
235046** Try to increase the size of the parser stack. Return the number
235047** of errors. Return 0 on success.
235048*/
235049static int fts5yyGrowStack(fts5yyParser *p)1{
235050 int oldSize = 1 + (int)(p->fts5yystackEnd - p->fts5yystack);
235051 int newSize;
235052 int idx;
235053 fts5yyStackEntry *pNew;
235054
235055 newSize = oldSize*2 + 100;
235056 idx = (int)(p->fts5yytos - p->fts5yystack);
235057 if( p->fts5yystack==p->fts5yystk0 ){
235058 pNew = fts5YYREALLOCrealloc(0, newSize*sizeof(pNew[0]));
235059 if( pNew==0 ) return 1;
235060 memcpy(pNew, p->fts5yystack, oldSize*sizeof(pNew[0]));
235061 }else{
235062 pNew = fts5YYREALLOCrealloc(p->fts5yystack, newSize*sizeof(pNew[0]));
235063 if( pNew==0 ) return 1;
235064 }
235065 p->fts5yystack = pNew;
235066 p->fts5yytos = &p->fts5yystack[idx];
235067#ifndef NDEBUG1
235068 if( fts5yyTraceFILE ){
235069 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
235070 fts5yyTracePrompt, oldSize, newSize);
235071 }
235072#endif
235073 p->fts5yystackEnd = &p->fts5yystack[newSize-1];
235074 return 0;
235075}
235076#endif /* fts5YYGROWABLESTACK */
235077
235078#if !fts5YYGROWABLESTACK0
235079/* For builds that do no have a growable stack, fts5yyGrowStack always
235080** returns an error.
235081*/
235082# define fts5yyGrowStack(X)1 1
235083#endif
235084
235085/* Datatype of the argument to the memory allocated passed as the
235086** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
235087** putting an appropriate #define in the %include section of the input
235088** grammar.
235089*/
235090#ifndef fts5YYMALLOCARGTYPEu64
235091# define fts5YYMALLOCARGTYPEu64 size_t
235092#endif
235093
235094/* Initialize a new parser that has already been allocated.
235095*/
235096static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
235097 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
235098 sqlite3Fts5ParserCTX_STORE
235099#ifdef fts5YYTRACKMAXSTACKDEPTH
235100 fts5yypParser->fts5yyhwm = 0;
235101#endif
235102 fts5yypParser->fts5yystack = fts5yypParser->fts5yystk0;
235103 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH100-1];
235104#ifndef fts5YYNOERRORRECOVERY1
235105 fts5yypParser->fts5yyerrcnt = -1;
235106#endif
235107 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
235108 fts5yypParser->fts5yystack[0].stateno = 0;
235109 fts5yypParser->fts5yystack[0].major = 0;
235110}
235111
235112#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
235113/*
235114** This function allocates a new parser.
235115** The only argument is a pointer to a function which works like
235116** malloc.
235117**
235118** Inputs:
235119** A pointer to the function used to allocate memory.
235120**
235121** Outputs:
235122** A pointer to a parser. This pointer is used in subsequent calls
235123** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
235124*/
235125static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPEu64) sqlite3Fts5ParserCTX_PDECL){
235126 fts5yyParser *fts5yypParser;
235127 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPEu64)sizeof(fts5yyParser) );
235128 if( fts5yypParser ){
235129 sqlite3Fts5ParserCTX_STORE
235130 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
235131 }
235132 return (void*)fts5yypParser;
235133}
235134#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
235135
235136
235137/* The following function deletes the "minor type" or semantic value
235138** associated with a symbol. The symbol can be either a terminal
235139** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
235140** a pointer to the value to be deleted. The code used to do the
235141** deletions is derived from the %destructor and/or %token_destructor
235142** directives of the input grammar.
235143*/
235144static void fts5yy_destructor(
235145 fts5yyParser *fts5yypParser, /* The parser */
235146 fts5YYCODETYPEunsigned char fts5yymajor, /* Type code for object to destroy */
235147 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
235148){
235149 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
235150 sqlite3Fts5ParserCTX_FETCH
235151 switch( fts5yymajor ){
235152 /* Here is inserted the actions which take place when a
235153 ** terminal or non-terminal is destroyed. This can happen
235154 ** when the symbol is popped from the stack during a
235155 ** reduce or during error processing or when a parser is
235156 ** being destroyed before it is finished parsing.
235157 **
235158 ** Note: during a reduce, the only symbols destroyed are those
235159 ** which appear on the RHS of the rule, but which are *not* used
235160 ** inside the C code.
235161 */
235162/********* Begin destructor definitions ***************************************/
235163 case 16: /* input */
235164{
235165 (void)pParse;
235166}
235167 break;
235168 case 17: /* expr */
235169 case 18: /* cnearset */
235170 case 19: /* exprlist */
235171{
235172 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
235173}
235174 break;
235175 case 20: /* colset */
235176 case 21: /* colsetlist */
235177{
235178 sqlite3_free((fts5yypminor->fts5yy11));
235179}
235180 break;
235181 case 22: /* nearset */
235182 case 23: /* nearphrases */
235183{
235184 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
235185}
235186 break;
235187 case 24: /* phrase */
235188{
235189 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
235190}
235191 break;
235192/********* End destructor definitions *****************************************/
235193 default: break; /* If no destructor action specified: do nothing */
235194 }
235195}
235196
235197/*
235198** Pop the parser's stack once.
235199**
235200** If there is a destructor routine associated with the token which
235201** is popped from the stack, then call it.
235202*/
235203static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
235204 fts5yyStackEntry *fts5yytos;
235205 assert( pParser->fts5yytos!=0 )((void) (0));
235206 assert( pParser->fts5yytos > pParser->fts5yystack )((void) (0));
235207 fts5yytos = pParser->fts5yytos--;
235208#ifndef NDEBUG1
235209 if( fts5yyTraceFILE ){
235210 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
235211 fts5yyTracePrompt,
235212 fts5yyTokenName[fts5yytos->major]);
235213 }
235214#endif
235215 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
235216}
235217
235218/*
235219** Clear all secondary memory allocations from the parser
235220*/
235221static void sqlite3Fts5ParserFinalize(void *p){
235222 fts5yyParser *pParser = (fts5yyParser*)p;
235223
235224 /* In-lined version of calling fts5yy_pop_parser_stack() for each
235225 ** element left in the stack */
235226 fts5yyStackEntry *fts5yytos = pParser->fts5yytos;
235227 while( fts5yytos>pParser->fts5yystack ){
235228#ifndef NDEBUG1
235229 if( fts5yyTraceFILE ){
235230 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
235231 fts5yyTracePrompt,
235232 fts5yyTokenName[fts5yytos->major]);
235233 }
235234#endif
235235 if( fts5yytos->major>=fts5YY_MIN_DSTRCTR16 ){
235236 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
235237 }
235238 fts5yytos--;
235239 }
235240
235241#if fts5YYGROWABLESTACK0
235242 if( pParser->fts5yystack!=pParser->fts5yystk0 ) fts5YYFREEfree(pParser->fts5yystack);
235243#endif
235244}
235245
235246#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
235247/*
235248** Deallocate and destroy a parser. Destructors are called for
235249** all stack elements before shutting the parser down.
235250**
235251** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
235252** is defined in a %include section of the input grammar) then it is
235253** assumed that the input pointer is never NULL.
235254*/
235255static void sqlite3Fts5ParserFree(
235256 void *p, /* The parser to be deleted */
235257 void (*freeProc)(void*) /* Function used to reclaim memory */
235258){
235259#ifndef fts5YYPARSEFREENEVERNULL
235260 if( p==0 ) return;
235261#endif
235262 sqlite3Fts5ParserFinalize(p);
235263 (*freeProc)(p);
235264}
235265#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
235266
235267/*
235268** Return the peak depth of the stack for a parser.
235269*/
235270#ifdef fts5YYTRACKMAXSTACKDEPTH
235271static int sqlite3Fts5ParserStackPeak(void *p){
235272 fts5yyParser *pParser = (fts5yyParser*)p;
235273 return pParser->fts5yyhwm;
235274}
235275#endif
235276
235277/* This array of booleans keeps track of the parser statement
235278** coverage. The element fts5yycoverage[X][Y] is set when the parser
235279** is in state X and has a lookahead token Y. In a well-tested
235280** systems, every element of this matrix should end up being set.
235281*/
235282#if defined(fts5YYCOVERAGE)
235283static unsigned char fts5yycoverage[fts5YYNSTATE35][fts5YYNFTS5TOKEN16];
235284#endif
235285
235286/*
235287** Write into out a description of every state/lookahead combination that
235288**
235289** (1) has not been used by the parser, and
235290** (2) is not a syntax error.
235291**
235292** Return the number of missed state/lookahead combinations.
235293*/
235294#if defined(fts5YYCOVERAGE)
235295static int sqlite3Fts5ParserCoverage(FILE *out){
235296 int stateno, iLookAhead, i;
235297 int nMissed = 0;
235298 for(stateno=0; stateno<fts5YYNSTATE35; stateno++){
235299 i = fts5yy_shift_ofst[stateno];
235300 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN16; iLookAhead++){
235301 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
235302 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
235303 if( out ){
235304 fprintf(out,"State %d lookahead %s %s\n", stateno,
235305 fts5yyTokenName[iLookAhead],
235306 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
235307 }
235308 }
235309 }
235310 return nMissed;
235311}
235312#endif
235313
235314/*
235315** Find the appropriate action for a parser given the terminal
235316** look-ahead token iLookAhead.
235317*/
235318static fts5YYACTIONTYPEunsigned char fts5yy_find_shift_action(
235319 fts5YYCODETYPEunsigned char iLookAhead, /* The look-ahead token */
235320 fts5YYACTIONTYPEunsigned char stateno /* Current state number */
235321){
235322 int i;
235323
235324 if( stateno>fts5YY_MAX_SHIFT34 ) return stateno;
235325 assert( stateno <= fts5YY_SHIFT_COUNT )((void) (0));
235326#if defined(fts5YYCOVERAGE)
235327 fts5yycoverage[stateno][iLookAhead] = 1;
235328#endif
235329 do{
235330 i = fts5yy_shift_ofst[stateno];
235331 assert( i>=0 )((void) (0));
235332 assert( i<=fts5YY_ACTTAB_COUNT )((void) (0));
235333 assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD )((void) (0));
235334 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
235335 assert( iLookAhead < fts5YYNFTS5TOKEN )((void) (0));
235336 i += iLookAhead;
235337 assert( i<(int)fts5YY_NLOOKAHEAD )((void) (0));
235338 if( fts5yy_lookahead[i]!=iLookAhead ){
235339#ifdef fts5YYFALLBACK
235340 fts5YYCODETYPEunsigned char iFallback; /* Fallback token */
235341 assert( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0]) )((void) (0));
235342 iFallback = fts5yyFallback[iLookAhead];
235343 if( iFallback!=0 ){
235344#ifndef NDEBUG1
235345 if( fts5yyTraceFILE ){
235346 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
235347 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
235348 }
235349#endif
235350 assert( fts5yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
235351 iLookAhead = iFallback;
235352 continue;
235353 }
235354#endif
235355#ifdef fts5YYWILDCARD
235356 {
235357 int j = i - iLookAhead + fts5YYWILDCARD;
235358 assert( j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) )((void) (0));
235359 if( fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0 ){
235360#ifndef NDEBUG1
235361 if( fts5yyTraceFILE ){
235362 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
235363 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
235364 fts5yyTokenName[fts5YYWILDCARD]);
235365 }
235366#endif /* NDEBUG */
235367 return fts5yy_action[j];
235368 }
235369 }
235370#endif /* fts5YYWILDCARD */
235371 return fts5yy_default[stateno];
235372 }else{
235373 assert( i>=0 && i<(int)(sizeof(fts5yy_action)/sizeof(fts5yy_action[0])) )((void) (0));
235374 return fts5yy_action[i];
235375 }
235376 }while(1);
235377}
235378
235379/*
235380** Find the appropriate action for a parser given the non-terminal
235381** look-ahead token iLookAhead.
235382*/
235383static fts5YYACTIONTYPEunsigned char fts5yy_find_reduce_action(
235384 fts5YYACTIONTYPEunsigned char stateno, /* Current state number */
235385 fts5YYCODETYPEunsigned char iLookAhead /* The look-ahead token */
235386){
235387 int i;
235388#ifdef fts5YYERRORSYMBOL
235389 if( stateno>fts5YY_REDUCE_COUNT(17) ){
235390 return fts5yy_default[stateno];
235391 }
235392#else
235393 assert( stateno<=fts5YY_REDUCE_COUNT )((void) (0));
235394#endif
235395 i = fts5yy_reduce_ofst[stateno];
235396 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
235397 i += iLookAhead;
235398#ifdef fts5YYERRORSYMBOL
235399 if( i<0 || i>=fts5YY_ACTTAB_COUNT(105) || fts5yy_lookahead[i]!=iLookAhead ){
235400 return fts5yy_default[stateno];
235401 }
235402#else
235403 assert( i>=0 && i<fts5YY_ACTTAB_COUNT )((void) (0));
235404 assert( fts5yy_lookahead[i]==iLookAhead )((void) (0));
235405#endif
235406 return fts5yy_action[i];
235407}
235408
235409/*
235410** The following routine is called if the stack overflows.
235411*/
235412static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
235413 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
235414 sqlite3Fts5ParserCTX_FETCH
235415#ifndef NDEBUG1
235416 if( fts5yyTraceFILE ){
235417 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
235418 }
235419#endif
235420 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
235421 /* Here code is inserted which will execute if the parser
235422 ** stack every overflows */
235423/******** Begin %stack_overflow code ******************************************/
235424
235425 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
235426/******** End %stack_overflow code ********************************************/
235427 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument var */
235428 sqlite3Fts5ParserCTX_STORE
235429}
235430
235431/*
235432** Print tracing information for a SHIFT action
235433*/
235434#ifndef NDEBUG1
235435static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
235436 if( fts5yyTraceFILE ){
235437 if( fts5yyNewState<fts5YYNSTATE35 ){
235438 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
235439 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
235440 fts5yyNewState);
235441 }else{
235442 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
235443 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
235444 fts5yyNewState - fts5YY_MIN_REDUCE83);
235445 }
235446 }
235447}
235448#else
235449# define fts5yyTraceShift(X,Y,Z)
235450#endif
235451
235452/*
235453** Perform a shift action.
235454*/
235455static void fts5yy_shift(
235456 fts5yyParser *fts5yypParser, /* The parser to be shifted */
235457 fts5YYACTIONTYPEunsigned char fts5yyNewState, /* The new state to shift in */
235458 fts5YYCODETYPEunsigned char fts5yyMajor, /* The major token to shift in */
235459 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyMinor /* The minor token to shift in */
235460){
235461 fts5yyStackEntry *fts5yytos;
235462 fts5yypParser->fts5yytos++;
235463#ifdef fts5YYTRACKMAXSTACKDEPTH
235464 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
235465 fts5yypParser->fts5yyhwm++;
235466 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) )((void) (0));
235467 }
235468#endif
235469 fts5yytos = fts5yypParser->fts5yytos;
235470 if( fts5yytos>fts5yypParser->fts5yystackEnd ){
235471 if( fts5yyGrowStack(fts5yypParser)1 ){
235472 fts5yypParser->fts5yytos--;
235473 fts5yyStackOverflow(fts5yypParser);
235474 return;
235475 }
235476 fts5yytos = fts5yypParser->fts5yytos;
235477 assert( fts5yytos <= fts5yypParser->fts5yystackEnd )((void) (0));
235478 }
235479 if( fts5yyNewState > fts5YY_MAX_SHIFT34 ){
235480 fts5yyNewState += fts5YY_MIN_REDUCE83 - fts5YY_MIN_SHIFTREDUCE52;
235481 }
235482 fts5yytos->stateno = fts5yyNewState;
235483 fts5yytos->major = fts5yyMajor;
235484 fts5yytos->minor.fts5yy0 = fts5yyMinor;
235485 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
235486}
235487
235488/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
235489** of that rule */
235490static const fts5YYCODETYPEunsigned char fts5yyRuleInfoLhs[] = {
235491 16, /* (0) input ::= expr */
235492 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
235493 20, /* (2) colset ::= LCP colsetlist RCP */
235494 20, /* (3) colset ::= STRING */
235495 20, /* (4) colset ::= MINUS STRING */
235496 21, /* (5) colsetlist ::= colsetlist STRING */
235497 21, /* (6) colsetlist ::= STRING */
235498 17, /* (7) expr ::= expr AND expr */
235499 17, /* (8) expr ::= expr OR expr */
235500 17, /* (9) expr ::= expr NOT expr */
235501 17, /* (10) expr ::= colset COLON LP expr RP */
235502 17, /* (11) expr ::= LP expr RP */
235503 17, /* (12) expr ::= exprlist */
235504 19, /* (13) exprlist ::= cnearset */
235505 19, /* (14) exprlist ::= exprlist cnearset */
235506 18, /* (15) cnearset ::= nearset */
235507 18, /* (16) cnearset ::= colset COLON nearset */
235508 22, /* (17) nearset ::= phrase */
235509 22, /* (18) nearset ::= CARET phrase */
235510 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
235511 23, /* (20) nearphrases ::= phrase */
235512 23, /* (21) nearphrases ::= nearphrases phrase */
235513 25, /* (22) neardist_opt ::= */
235514 25, /* (23) neardist_opt ::= COMMA STRING */
235515 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
235516 24, /* (25) phrase ::= STRING star_opt */
235517 26, /* (26) star_opt ::= STAR */
235518 26, /* (27) star_opt ::= */
235519};
235520
235521/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
235522** of symbols on the right-hand side of that rule. */
235523static const signed char fts5yyRuleInfoNRhs[] = {
235524 -1, /* (0) input ::= expr */
235525 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
235526 -3, /* (2) colset ::= LCP colsetlist RCP */
235527 -1, /* (3) colset ::= STRING */
235528 -2, /* (4) colset ::= MINUS STRING */
235529 -2, /* (5) colsetlist ::= colsetlist STRING */
235530 -1, /* (6) colsetlist ::= STRING */
235531 -3, /* (7) expr ::= expr AND expr */
235532 -3, /* (8) expr ::= expr OR expr */
235533 -3, /* (9) expr ::= expr NOT expr */
235534 -5, /* (10) expr ::= colset COLON LP expr RP */
235535 -3, /* (11) expr ::= LP expr RP */
235536 -1, /* (12) expr ::= exprlist */
235537 -1, /* (13) exprlist ::= cnearset */
235538 -2, /* (14) exprlist ::= exprlist cnearset */
235539 -1, /* (15) cnearset ::= nearset */
235540 -3, /* (16) cnearset ::= colset COLON nearset */
235541 -1, /* (17) nearset ::= phrase */
235542 -2, /* (18) nearset ::= CARET phrase */
235543 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
235544 -1, /* (20) nearphrases ::= phrase */
235545 -2, /* (21) nearphrases ::= nearphrases phrase */
235546 0, /* (22) neardist_opt ::= */
235547 -2, /* (23) neardist_opt ::= COMMA STRING */
235548 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
235549 -2, /* (25) phrase ::= STRING star_opt */
235550 -1, /* (26) star_opt ::= STAR */
235551 0, /* (27) star_opt ::= */
235552};
235553
235554static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
235555
235556/*
235557** Perform a reduce action and the shift that must immediately
235558** follow the reduce.
235559**
235560** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
235561** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
235562** if the lookahead token has already been consumed. As this procedure is
235563** only called from one place, optimizing compilers will in-line it, which
235564** means that the extra parameters have no performance impact.
235565*/
235566static fts5YYACTIONTYPEunsigned char fts5yy_reduce(
235567 fts5yyParser *fts5yypParser, /* The parser */
235568 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
235569 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
235570 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyLookaheadToken /* Value of the lookahead token */
235571 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
235572){
235573 int fts5yygoto; /* The next state */
235574 fts5YYACTIONTYPEunsigned char fts5yyact; /* The next action */
235575 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
235576 int fts5yysize; /* Amount to pop the stack */
235577 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
235578 (void)fts5yyLookahead;
235579 (void)fts5yyLookaheadToken;
235580 fts5yymsp = fts5yypParser->fts5yytos;
235581
235582 switch( fts5yyruleno ){
235583 /* Beginning here are the reduction cases. A typical example
235584 ** follows:
235585 ** case 0:
235586 ** #line <lineno> <grammarfile>
235587 ** { ... } // User supplied code
235588 ** #line <lineno> <thisfile>
235589 ** break;
235590 */
235591/********** Begin reduce actions **********************************************/
235592 fts5YYMINORTYPE fts5yylhsminor;
235593 case 0: /* input ::= expr */
235594{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
235595 break;
235596 case 1: /* colset ::= MINUS LCP colsetlist RCP */
235597{
235598 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
235599}
235600 break;
235601 case 2: /* colset ::= LCP colsetlist RCP */
235602{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
235603 break;
235604 case 3: /* colset ::= STRING */
235605{
235606 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
235607}
235608 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
235609 break;
235610 case 4: /* colset ::= MINUS STRING */
235611{
235612 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
235613 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
235614}
235615 break;
235616 case 5: /* colsetlist ::= colsetlist STRING */
235617{
235618 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
235619 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
235620 break;
235621 case 6: /* colsetlist ::= STRING */
235622{
235623 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
235624}
235625 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
235626 break;
235627 case 7: /* expr ::= expr AND expr */
235628{
235629 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND2, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
235630}
235631 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235632 break;
235633 case 8: /* expr ::= expr OR expr */
235634{
235635 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR1, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
235636}
235637 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235638 break;
235639 case 9: /* expr ::= expr NOT expr */
235640{
235641 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT3, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
235642}
235643 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235644 break;
235645 case 10: /* expr ::= colset COLON LP expr RP */
235646{
235647 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
235648 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
235649}
235650 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235651 break;
235652 case 11: /* expr ::= LP expr RP */
235653{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
235654 break;
235655 case 12: /* expr ::= exprlist */
235656 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
235657{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
235658 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235659 break;
235660 case 14: /* exprlist ::= exprlist cnearset */
235661{
235662 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
235663}
235664 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235665 break;
235666 case 15: /* cnearset ::= nearset */
235667{
235668 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING9, 0, 0, fts5yymsp[0].minor.fts5yy46);
235669}
235670 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235671 break;
235672 case 16: /* cnearset ::= colset COLON nearset */
235673{
235674 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING9, 0, 0, fts5yymsp[0].minor.fts5yy46);
235675 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
235676}
235677 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
235678 break;
235679 case 17: /* nearset ::= phrase */
235680{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
235681 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
235682 break;
235683 case 18: /* nearset ::= CARET phrase */
235684{
235685 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
235686 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
235687}
235688 break;
235689 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
235690{
235691 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
235692 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
235693 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
235694}
235695 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
235696 break;
235697 case 20: /* nearphrases ::= phrase */
235698{
235699 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
235700}
235701 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
235702 break;
235703 case 21: /* nearphrases ::= nearphrases phrase */
235704{
235705 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
235706}
235707 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
235708 break;
235709 case 22: /* neardist_opt ::= */
235710{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
235711 break;
235712 case 23: /* neardist_opt ::= COMMA STRING */
235713{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
235714 break;
235715 case 24: /* phrase ::= phrase PLUS STRING star_opt */
235716{
235717 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
235718}
235719 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
235720 break;
235721 case 25: /* phrase ::= STRING star_opt */
235722{
235723 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
235724}
235725 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
235726 break;
235727 case 26: /* star_opt ::= STAR */
235728{ fts5yymsp[0].minor.fts5yy4 = 1; }
235729 break;
235730 case 27: /* star_opt ::= */
235731{ fts5yymsp[1].minor.fts5yy4 = 0; }
235732 break;
235733 default:
235734 break;
235735/********** End reduce actions ************************************************/
235736 };
235737 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) )((void) (0));
235738 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
235739 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
235740 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPEunsigned char)fts5yygoto);
235741
235742 /* There are no SHIFTREDUCE actions on nonterminals because the table
235743 ** generator has simplified them to pure REDUCE actions. */
235744 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) )((void) (0));
235745
235746 /* It is not possible for a REDUCE to be followed by an error */
235747 assert( fts5yyact!=fts5YY_ERROR_ACTION )((void) (0));
235748
235749 fts5yymsp += fts5yysize+1;
235750 fts5yypParser->fts5yytos = fts5yymsp;
235751 fts5yymsp->stateno = (fts5YYACTIONTYPEunsigned char)fts5yyact;
235752 fts5yymsp->major = (fts5YYCODETYPEunsigned char)fts5yygoto;
235753 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
235754 return fts5yyact;
235755}
235756
235757/*
235758** The following code executes when the parse fails
235759*/
235760#ifndef fts5YYNOERRORRECOVERY1
235761static void fts5yy_parse_failed(
235762 fts5yyParser *fts5yypParser /* The parser */
235763){
235764 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
235765 sqlite3Fts5ParserCTX_FETCH
235766#ifndef NDEBUG1
235767 if( fts5yyTraceFILE ){
235768 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
235769 }
235770#endif
235771 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
235772 /* Here code is inserted which will be executed whenever the
235773 ** parser fails */
235774/************ Begin %parse_failure code ***************************************/
235775/************ End %parse_failure code *****************************************/
235776 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
235777 sqlite3Fts5ParserCTX_STORE
235778}
235779#endif /* fts5YYNOERRORRECOVERY */
235780
235781/*
235782** The following code executes when a syntax error first occurs.
235783*/
235784static void fts5yy_syntax_error(
235785 fts5yyParser *fts5yypParser, /* The parser */
235786 int fts5yymajor, /* The major type of the error token */
235787 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyminor /* The minor type of the error token */
235788){
235789 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
235790 sqlite3Fts5ParserCTX_FETCH
235791#define FTS5TOKENfts5yyminor fts5yyminor
235792/************ Begin %syntax_error code ****************************************/
235793
235794 UNUSED_PARAM(fts5yymajor)(void)(fts5yymajor); /* Silence a compiler warning */
235795 sqlite3Fts5ParseError(
235796 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKENfts5yyminor.n,FTS5TOKENfts5yyminor.p
235797 );
235798/************ End %syntax_error code ******************************************/
235799 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
235800 sqlite3Fts5ParserCTX_STORE
235801}
235802
235803/*
235804** The following is executed when the parser accepts
235805*/
235806static void fts5yy_accept(
235807 fts5yyParser *fts5yypParser /* The parser */
235808){
235809 sqlite3Fts5ParserARG_FETCHFts5Parse *pParse=fts5yypParser->pParse;
235810 sqlite3Fts5ParserCTX_FETCH
235811#ifndef NDEBUG1
235812 if( fts5yyTraceFILE ){
235813 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
235814 }
235815#endif
235816#ifndef fts5YYNOERRORRECOVERY1
235817 fts5yypParser->fts5yyerrcnt = -1;
235818#endif
235819 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack )((void) (0));
235820 /* Here code is inserted which will be executed whenever the
235821 ** parser accepts */
235822/*********** Begin %parse_accept code *****************************************/
235823/*********** End %parse_accept code *******************************************/
235824 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse; /* Suppress warning about unused %extra_argument variable */
235825 sqlite3Fts5ParserCTX_STORE
235826}
235827
235828/* The main parser program.
235829** The first argument is a pointer to a structure obtained from
235830** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
235831** The second argument is the major token number. The third is
235832** the minor token. The fourth optional argument is whatever the
235833** user wants (and specified in the grammar) and is available for
235834** use by the action routines.
235835**
235836** Inputs:
235837** <ul>
235838** <li> A pointer to the parser (an opaque structure.)
235839** <li> The major token number.
235840** <li> The minor token number.
235841** <li> An option argument of a grammar-specified type.
235842** </ul>
235843**
235844** Outputs:
235845** None.
235846*/
235847static void sqlite3Fts5Parser(
235848 void *fts5yyp, /* The parser */
235849 int fts5yymajor, /* The major token code number */
235850 sqlite3Fts5ParserFTS5TOKENTYPEFts5Token fts5yyminor /* The value for the token */
235851 sqlite3Fts5ParserARG_PDECL,Fts5Parse *pParse /* Optional %extra_argument parameter */
235852){
235853 fts5YYMINORTYPE fts5yyminorunion;
235854 fts5YYACTIONTYPEunsigned char fts5yyact; /* The parser action. */
235855#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY1)
235856 int fts5yyendofinput; /* True if we are at the end of input */
235857#endif
235858#ifdef fts5YYERRORSYMBOL
235859 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
235860#endif
235861 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
235862 sqlite3Fts5ParserCTX_FETCH
235863 sqlite3Fts5ParserARG_STOREfts5yypParser->pParse=pParse;
235864
235865 assert( fts5yypParser->fts5yytos!=0 )((void) (0));
235866#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY1)
235867 fts5yyendofinput = (fts5yymajor==0);
235868#endif
235869
235870 fts5yyact = fts5yypParser->fts5yytos->stateno;
235871#ifndef NDEBUG1
235872 if( fts5yyTraceFILE ){
235873 if( fts5yyact < fts5YY_MIN_REDUCE83 ){
235874 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
235875 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
235876 }else{
235877 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
235878 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE83);
235879 }
235880 }
235881#endif
235882
235883 while(1){ /* Exit by "break" */
235884 assert( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystack )((void) (0));
235885 assert( fts5yyact==fts5yypParser->fts5yytos->stateno )((void) (0));
235886 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPEunsigned char)fts5yymajor,fts5yyact);
235887 if( fts5yyact >= fts5YY_MIN_REDUCE83 ){
235888 unsigned int fts5yyruleno = fts5yyact - fts5YY_MIN_REDUCE83; /* Reduce by this rule */
235889#ifndef NDEBUG1
235890 assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) )((void) (0));
235891 if( fts5yyTraceFILE ){
235892 int fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
235893 if( fts5yysize ){
235894 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
235895 fts5yyTracePrompt,
235896 fts5yyruleno, fts5yyRuleName[fts5yyruleno],
235897 fts5yyruleno<fts5YYNRULE_WITH_ACTION28 ? "" : " without external action",
235898 fts5yypParser->fts5yytos[fts5yysize].stateno);
235899 }else{
235900 fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s.\n",
235901 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno],
235902 fts5yyruleno<fts5YYNRULE_WITH_ACTION28 ? "" : " without external action");
235903 }
235904 }
235905#endif /* NDEBUG */
235906
235907 /* Check that the stack is large enough to grow by a single entry
235908 ** if the RHS of the rule is empty. This ensures that there is room
235909 ** enough on the stack to push the LHS value */
235910 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
235911#ifdef fts5YYTRACKMAXSTACKDEPTH
235912 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
235913 fts5yypParser->fts5yyhwm++;
235914 assert( fts5yypParser->fts5yyhwm ==((void) (0))
235915 (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack))((void) (0));
235916 }
235917#endif
235918 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
235919 if( fts5yyGrowStack(fts5yypParser)1 ){
235920 fts5yyStackOverflow(fts5yypParser);
235921 break;
235922 }
235923 }
235924 }
235925 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyruleno,fts5yymajor,fts5yyminor sqlite3Fts5ParserCTX_PARAM);
235926 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE79 ){
235927 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPEunsigned char)fts5yymajor,fts5yyminor);
235928#ifndef fts5YYNOERRORRECOVERY1
235929 fts5yypParser->fts5yyerrcnt--;
235930#endif
235931 break;
235932 }else if( fts5yyact==fts5YY_ACCEPT_ACTION81 ){
235933 fts5yypParser->fts5yytos--;
235934 fts5yy_accept(fts5yypParser);
235935 return;
235936 }else{
235937 assert( fts5yyact == fts5YY_ERROR_ACTION )((void) (0));
235938 fts5yyminorunion.fts5yy0 = fts5yyminor;
235939#ifdef fts5YYERRORSYMBOL
235940 int fts5yymx;
235941#endif
235942#ifndef NDEBUG1
235943 if( fts5yyTraceFILE ){
235944 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
235945 }
235946#endif
235947#ifdef fts5YYERRORSYMBOL
235948 /* A syntax error has occurred.
235949 ** The response to an error depends upon whether or not the
235950 ** grammar defines an error token "ERROR".
235951 **
235952 ** This is what we do if the grammar does define ERROR:
235953 **
235954 ** * Call the %syntax_error function.
235955 **
235956 ** * Begin popping the stack until we enter a state where
235957 ** it is legal to shift the error symbol, then shift
235958 ** the error symbol.
235959 **
235960 ** * Set the error count to three.
235961 **
235962 ** * Begin accepting and shifting new tokens. No new error
235963 ** processing will occur until three tokens have been
235964 ** shifted successfully.
235965 **
235966 */
235967 if( fts5yypParser->fts5yyerrcnt<0 ){
235968 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
235969 }
235970 fts5yymx = fts5yypParser->fts5yytos->major;
235971 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
235972#ifndef NDEBUG1
235973 if( fts5yyTraceFILE ){
235974 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
235975 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
235976 }
235977#endif
235978 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPEunsigned char)fts5yymajor, &fts5yyminorunion);
235979 fts5yymajor = fts5YYNOCODE27;
235980 }else{
235981 while( fts5yypParser->fts5yytos > fts5yypParser->fts5yystack ){
235982 fts5yyact = fts5yy_find_reduce_action(fts5yypParser->fts5yytos->stateno,
235983 fts5YYERRORSYMBOL);
235984 if( fts5yyact<=fts5YY_MAX_SHIFTREDUCE79 ) break;
235985 fts5yy_pop_parser_stack(fts5yypParser);
235986 }
235987 if( fts5yypParser->fts5yytos <= fts5yypParser->fts5yystack || fts5yymajor==0 ){
235988 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
235989 fts5yy_parse_failed(fts5yypParser);
235990#ifndef fts5YYNOERRORRECOVERY1
235991 fts5yypParser->fts5yyerrcnt = -1;
235992#endif
235993 fts5yymajor = fts5YYNOCODE27;
235994 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
235995 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
235996 }
235997 }
235998 fts5yypParser->fts5yyerrcnt = 3;
235999 fts5yyerrorhit = 1;
236000 if( fts5yymajor==fts5YYNOCODE27 ) break;
236001 fts5yyact = fts5yypParser->fts5yytos->stateno;
236002#elif defined(fts5YYNOERRORRECOVERY1)
236003 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
236004 ** do any kind of error recovery. Instead, simply invoke the syntax
236005 ** error routine and continue going as if nothing had happened.
236006 **
236007 ** Applications can set this macro (for example inside %include) if
236008 ** they intend to abandon the parse upon the first syntax error seen.
236009 */
236010 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
236011 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
236012 break;
236013#else /* fts5YYERRORSYMBOL is not defined */
236014 /* This is what we do if the grammar does not define ERROR:
236015 **
236016 ** * Report an error message, and throw away the input token.
236017 **
236018 ** * If the input token is $, then fail the parse.
236019 **
236020 ** As before, subsequent error messages are suppressed until
236021 ** three input tokens have been successfully shifted.
236022 */
236023 if( fts5yypParser->fts5yyerrcnt<=0 ){
236024 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
236025 }
236026 fts5yypParser->fts5yyerrcnt = 3;
236027 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPEunsigned char)fts5yymajor,&fts5yyminorunion);
236028 if( fts5yyendofinput ){
236029 fts5yy_parse_failed(fts5yypParser);
236030#ifndef fts5YYNOERRORRECOVERY1
236031 fts5yypParser->fts5yyerrcnt = -1;
236032#endif
236033 }
236034 break;
236035#endif
236036 }
236037 }
236038#ifndef NDEBUG1
236039 if( fts5yyTraceFILE ){
236040 fts5yyStackEntry *i;
236041 char cDiv = '[';
236042 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
236043 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
236044 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
236045 cDiv = ' ';
236046 }
236047 fprintf(fts5yyTraceFILE,"]\n");
236048 }
236049#endif
236050 return;
236051}
236052
236053/*
236054** Return the fallback token corresponding to canonical token iToken, or
236055** 0 if iToken has no fallback.
236056*/
236057static int sqlite3Fts5ParserFallback(int iToken){
236058#ifdef fts5YYFALLBACK
236059 assert( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) )((void) (0));
236060 return fts5yyFallback[iToken];
236061#else
236062 (void)iToken;
236063 return 0;
236064#endif
236065}
236066
236067/*
236068** 2014 May 31
236069**
236070** The author disclaims copyright to this source code. In place of
236071** a legal notice, here is a blessing:
236072**
236073** May you do good and not evil.
236074** May you find forgiveness for yourself and forgive others.
236075** May you share freely, never taking more than you give.
236076**
236077******************************************************************************
236078*/
236079
236080
236081/* #include "fts5Int.h" */
236082#include <math.h> /* amalgamator: keep */
236083
236084/*
236085** Object used to iterate through all "coalesced phrase instances" in
236086** a single column of the current row. If the phrase instances in the
236087** column being considered do not overlap, this object simply iterates
236088** through them. Or, if they do overlap (share one or more tokens in
236089** common), each set of overlapping instances is treated as a single
236090** match. See documentation for the highlight() auxiliary function for
236091** details.
236092**
236093** Usage is:
236094**
236095** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
236096** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
236097** rc = fts5CInstIterNext(&iter)
236098** ){
236099** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
236100** }
236101**
236102*/
236103typedef struct CInstIter CInstIter;
236104struct CInstIter {
236105 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
236106 Fts5Context *pFts; /* First arg to pass to pApi functions */
236107 int iCol; /* Column to search */
236108 int iInst; /* Next phrase instance index */
236109 int nInst; /* Total number of phrase instances */
236110
236111 /* Output variables */
236112 int iStart; /* First token in coalesced phrase instance */
236113 int iEnd; /* Last token in coalesced phrase instance */
236114};
236115
236116/*
236117** Advance the iterator to the next coalesced phrase instance. Return
236118** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
236119*/
236120static int fts5CInstIterNext(CInstIter *pIter){
236121 int rc = SQLITE_OK0;
236122 pIter->iStart = -1;
236123 pIter->iEnd = -1;
236124
236125 while( rc==SQLITE_OK0 && pIter->iInst<pIter->nInst ){
236126 int ip; int ic; int io;
236127 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
236128 if( rc==SQLITE_OK0 ){
236129 if( ic==pIter->iCol ){
236130 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
236131 if( pIter->iStart<0 ){
236132 pIter->iStart = io;
236133 pIter->iEnd = iEnd;
236134 }else if( io<=pIter->iEnd ){
236135 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
236136 }else{
236137 break;
236138 }
236139 }
236140 pIter->iInst++;
236141 }
236142 }
236143
236144 return rc;
236145}
236146
236147/*
236148** Initialize the iterator object indicated by the final parameter to
236149** iterate through coalesced phrase instances in column iCol.
236150*/
236151static int fts5CInstIterInit(
236152 const Fts5ExtensionApi *pApi,
236153 Fts5Context *pFts,
236154 int iCol,
236155 CInstIter *pIter
236156){
236157 int rc;
236158
236159 memset(pIter, 0, sizeof(CInstIter));
236160 pIter->pApi = pApi;
236161 pIter->pFts = pFts;
236162 pIter->iCol = iCol;
236163 rc = pApi->xInstCount(pFts, &pIter->nInst);
236164
236165 if( rc==SQLITE_OK0 ){
236166 rc = fts5CInstIterNext(pIter);
236167 }
236168
236169 return rc;
236170}
236171
236172
236173
236174/*************************************************************************
236175** Start of highlight() implementation.
236176*/
236177typedef struct HighlightContext HighlightContext;
236178struct HighlightContext {
236179 /* Constant parameters to fts5HighlightCb() */
236180 int iRangeStart; /* First token to include */
236181 int iRangeEnd; /* If non-zero, last token to include */
236182 const char *zOpen; /* Opening highlight */
236183 const char *zClose; /* Closing highlight */
236184 const char *zIn; /* Input text */
236185 int nIn; /* Size of input text in bytes */
236186
236187 /* Variables modified by fts5HighlightCb() */
236188 CInstIter iter; /* Coalesced Instance Iterator */
236189 int iPos; /* Current token offset in zIn[] */
236190 int iOff; /* Have copied up to this offset in zIn[] */
236191 int bOpen; /* True if highlight is open */
236192 char *zOut; /* Output value */
236193};
236194
236195/*
236196** Append text to the HighlightContext output string - p->zOut. Argument
236197** z points to a buffer containing n bytes of text to append. If n is
236198** negative, everything up until the first '\0' is appended to the output.
236199**
236200** If *pRc is set to any value other than SQLITE_OK when this function is
236201** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
236202** *pRc is set to an error code before returning.
236203*/
236204static void fts5HighlightAppend(
236205 int *pRc,
236206 HighlightContext *p,
236207 const char *z, int n
236208){
236209 if( *pRc==SQLITE_OK0 && z ){
236210 if( n<0 ) n = (int)strlen(z);
236211 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
236212 if( p->zOut==0 ) *pRc = SQLITE_NOMEM7;
236213 }
236214}
236215
236216/*
236217** Tokenizer callback used by implementation of highlight() function.
236218*/
236219static int fts5HighlightCb(
236220 void *pContext, /* Pointer to HighlightContext object */
236221 int tflags, /* Mask of FTS5_TOKEN_* flags */
236222 const char *pToken, /* Buffer containing token */
236223 int nToken, /* Size of token in bytes */
236224 int iStartOff, /* Start byte offset of token */
236225 int iEndOff /* End byte offset of token */
236226){
236227 HighlightContext *p = (HighlightContext*)pContext;
236228 int rc = SQLITE_OK0;
236229 int iPos;
236230
236231 UNUSED_PARAM2(pToken, nToken)(void)(pToken), (void)(nToken);
236232
236233 if( tflags & FTS5_TOKEN_COLOCATED0x0001 ) return SQLITE_OK0;
236234 iPos = p->iPos++;
236235
236236 if( p->iRangeEnd>=0 ){
236237 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK0;
236238 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
236239 }
236240
236241 /* If the parenthesis is open, and this token is not part of the current
236242 ** phrase, and the starting byte offset of this token is past the point
236243 ** that has currently been copied into the output buffer, close the
236244 ** parenthesis. */
236245 if( p->bOpen
236246 && (iPos<=p->iter.iStart || p->iter.iStart<0)
236247 && iStartOff>p->iOff
236248 ){
236249 fts5HighlightAppend(&rc, p, p->zClose, -1);
236250 p->bOpen = 0;
236251 }
236252
236253 /* If this is the start of a new phrase, and the highlight is not open:
236254 **
236255 ** * copy text from the input up to the start of the phrase, and
236256 ** * open the highlight.
236257 */
236258 if( iPos==p->iter.iStart && p->bOpen==0 ){
236259 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
236260 fts5HighlightAppend(&rc, p, p->zOpen, -1);
236261 p->iOff = iStartOff;
236262 p->bOpen = 1;
236263 }
236264
236265 if( iPos==p->iter.iEnd ){
236266 if( p->bOpen==0 ){
236267 assert( p->iRangeEnd>=0 )((void) (0));
236268 fts5HighlightAppend(&rc, p, p->zOpen, -1);
236269 p->bOpen = 1;
236270 }
236271 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
236272 p->iOff = iEndOff;
236273
236274 if( rc==SQLITE_OK0 ){
236275 rc = fts5CInstIterNext(&p->iter);
236276 }
236277 }
236278
236279 if( iPos==p->iRangeEnd ){
236280 if( p->bOpen ){
236281 if( p->iter.iStart>=0 && iPos>=p->iter.iStart ){
236282 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
236283 p->iOff = iEndOff;
236284 }
236285 fts5HighlightAppend(&rc, p, p->zClose, -1);
236286 p->bOpen = 0;
236287 }
236288 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
236289 p->iOff = iEndOff;
236290 }
236291
236292 return rc;
236293}
236294
236295
236296/*
236297** Implementation of highlight() function.
236298*/
236299static void fts5HighlightFunction(
236300 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
236301 Fts5Context *pFts, /* First arg to pass to pApi functions */
236302 sqlite3_context *pCtx, /* Context for returning result/error */
236303 int nVal, /* Number of values in apVal[] array */
236304 sqlite3_value **apVal /* Array of trailing arguments */
236305){
236306 HighlightContext ctx;
236307 int rc;
236308 int iCol;
236309
236310 if( nVal!=3 ){
236311 const char *zErr = "wrong number of arguments to function highlight()";
236312 sqlite3_result_error(pCtx, zErr, -1);
236313 return;
236314 }
236315
236316 iCol = sqlite3_value_int(apVal[0]);
236317 memset(&ctx, 0, sizeof(HighlightContext));
236318 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
236319 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
236320 ctx.iRangeEnd = -1;
236321 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
236322 if( rc==SQLITE_RANGE25 ){
236323 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
236324 rc = SQLITE_OK0;
236325 }else if( ctx.zIn ){
236326 const char *pLoc = 0; /* Locale of column iCol */
236327 int nLoc = 0; /* Size of pLoc in bytes */
236328 if( rc==SQLITE_OK0 ){
236329 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
236330 }
236331
236332 if( rc==SQLITE_OK0 ){
236333 rc = pApi->xColumnLocale(pFts, iCol, &pLoc, &nLoc);
236334 }
236335 if( rc==SQLITE_OK0 ){
236336 rc = pApi->xTokenize_v2(
236337 pFts, ctx.zIn, ctx.nIn, pLoc, nLoc, (void*)&ctx, fts5HighlightCb
236338 );
236339 }
236340 if( ctx.bOpen ){
236341 fts5HighlightAppend(&rc, &ctx, ctx.zClose, -1);
236342 }
236343 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
236344
236345 if( rc==SQLITE_OK0 ){
236346 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
236347 }
236348 sqlite3_free(ctx.zOut);
236349 }
236350 if( rc!=SQLITE_OK0 ){
236351 sqlite3_result_error_code(pCtx, rc);
236352 }
236353}
236354/*
236355** End of highlight() implementation.
236356**************************************************************************/
236357
236358/*
236359** Context object passed to the fts5SentenceFinderCb() function.
236360*/
236361typedef struct Fts5SFinder Fts5SFinder;
236362struct Fts5SFinder {
236363 int iPos; /* Current token position */
236364 int nFirstAlloc; /* Allocated size of aFirst[] */
236365 int nFirst; /* Number of entries in aFirst[] */
236366 int *aFirst; /* Array of first token in each sentence */
236367 const char *zDoc; /* Document being tokenized */
236368};
236369
236370/*
236371** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
236372** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
236373** error occurs.
236374*/
236375static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
236376 if( p->nFirstAlloc==p->nFirst ){
236377 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
236378 int *aNew;
236379
236380 aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
236381 if( aNew==0 ) return SQLITE_NOMEM7;
236382 p->aFirst = aNew;
236383 p->nFirstAlloc = nNew;
236384 }
236385 p->aFirst[p->nFirst++] = iAdd;
236386 return SQLITE_OK0;
236387}
236388
236389/*
236390** This function is an xTokenize() callback used by the auxiliary snippet()
236391** function. Its job is to identify tokens that are the first in a sentence.
236392** For each such token, an entry is added to the SFinder.aFirst[] array.
236393*/
236394static int fts5SentenceFinderCb(
236395 void *pContext, /* Pointer to HighlightContext object */
236396 int tflags, /* Mask of FTS5_TOKEN_* flags */
236397 const char *pToken, /* Buffer containing token */
236398 int nToken, /* Size of token in bytes */
236399 int iStartOff, /* Start offset of token */
236400 int iEndOff /* End offset of token */
236401){
236402 int rc = SQLITE_OK0;
236403
236404 UNUSED_PARAM2(pToken, nToken)(void)(pToken), (void)(nToken);
236405 UNUSED_PARAM(iEndOff)(void)(iEndOff);
236406
236407 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
236408 Fts5SFinder *p = (Fts5SFinder*)pContext;
236409 if( p->iPos>0 ){
236410 int i;
236411 char c = 0;
236412 for(i=iStartOff-1; i>=0; i--){
236413 c = p->zDoc[i];
236414 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
236415 }
236416 if( i!=iStartOff-1 && (c=='.' || c==':') ){
236417 rc = fts5SentenceFinderAdd(p, p->iPos);
236418 }
236419 }else{
236420 rc = fts5SentenceFinderAdd(p, 0);
236421 }
236422 p->iPos++;
236423 }
236424 return rc;
236425}
236426
236427static int fts5SnippetScore(
236428 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
236429 Fts5Context *pFts, /* First arg to pass to pApi functions */
236430 int nDocsize, /* Size of column in tokens */
236431 unsigned char *aSeen, /* Array with one element per query phrase */
236432 int iCol, /* Column to score */
236433 int iPos, /* Starting offset to score */
236434 int nToken, /* Max tokens per snippet */
236435 int *pnScore, /* OUT: Score */
236436 int *piPos /* OUT: Adjusted offset */
236437){
236438 int rc;
236439 int i;
236440 int ip = 0;
236441 int ic = 0;
236442 int iOff = 0;
236443 int iFirst = -1;
236444 int nInst;
236445 int nScore = 0;
236446 int iLast = 0;
236447 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
236448
236449 rc = pApi->xInstCount(pFts, &nInst);
236450 for(i=0; i<nInst && rc==SQLITE_OK0; i++){
236451 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
236452 if( rc==SQLITE_OK0 && ic==iCol && iOff>=iPos && iOff<iEnd ){
236453 nScore += (aSeen[ip] ? 1 : 1000);
236454 aSeen[ip] = 1;
236455 if( iFirst<0 ) iFirst = iOff;
236456 iLast = iOff + pApi->xPhraseSize(pFts, ip);
236457 }
236458 }
236459
236460 *pnScore = nScore;
236461 if( piPos ){
236462 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
236463 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
236464 if( iAdj<0 ) iAdj = 0;
236465 *piPos = (int)iAdj;
236466 }
236467
236468 return rc;
236469}
236470
236471/*
236472** Return the value in pVal interpreted as utf-8 text. Except, if pVal
236473** contains a NULL value, return a pointer to a static string zero
236474** bytes in length instead of a NULL pointer.
236475*/
236476static const char *fts5ValueToText(sqlite3_value *pVal){
236477 const char *zRet = (const char*)sqlite3_value_text(pVal);
236478 return zRet ? zRet : "";
236479}
236480
236481/*
236482** Implementation of snippet() function.
236483*/
236484static void fts5SnippetFunction(
236485 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
236486 Fts5Context *pFts, /* First arg to pass to pApi functions */
236487 sqlite3_context *pCtx, /* Context for returning result/error */
236488 int nVal, /* Number of values in apVal[] array */
236489 sqlite3_value **apVal /* Array of trailing arguments */
236490){
236491 HighlightContext ctx;
236492 int rc = SQLITE_OK0; /* Return code */
236493 int iCol; /* 1st argument to snippet() */
236494 const char *zEllips; /* 4th argument to snippet() */
236495 int nToken; /* 5th argument to snippet() */
236496 int nInst = 0; /* Number of instance matches this row */
236497 int i; /* Used to iterate through instances */
236498 int nPhrase; /* Number of phrases in query */
236499 unsigned char *aSeen; /* Array of "seen instance" flags */
236500 int iBestCol; /* Column containing best snippet */
236501 int iBestStart = 0; /* First token of best snippet */
236502 int nBestScore = 0; /* Score of best snippet */
236503 int nColSize = 0; /* Total size of iBestCol in tokens */
236504 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
236505 int nCol;
236506
236507 if( nVal!=5 ){
236508 const char *zErr = "wrong number of arguments to function snippet()";
236509 sqlite3_result_error(pCtx, zErr, -1);
236510 return;
236511 }
236512
236513 nCol = pApi->xColumnCount(pFts);
236514 memset(&ctx, 0, sizeof(HighlightContext));
236515 iCol = sqlite3_value_int(apVal[0]);
236516 ctx.zOpen = fts5ValueToText(apVal[1]);
236517 ctx.zClose = fts5ValueToText(apVal[2]);
236518 ctx.iRangeEnd = -1;
236519 zEllips = fts5ValueToText(apVal[3]);
236520 nToken = sqlite3_value_int(apVal[4]);
236521
236522 iBestCol = (iCol>=0 ? iCol : 0);
236523 nPhrase = pApi->xPhraseCount(pFts);
236524 aSeen = sqlite3_malloc(nPhrase);
236525 if( aSeen==0 ){
236526 rc = SQLITE_NOMEM7;
236527 }
236528 if( rc==SQLITE_OK0 ){
236529 rc = pApi->xInstCount(pFts, &nInst);
236530 }
236531
236532 memset(&sFinder, 0, sizeof(Fts5SFinder));
236533 for(i=0; i<nCol; i++){
236534 if( iCol<0 || iCol==i ){
236535 const char *pLoc = 0; /* Locale of column iCol */
236536 int nLoc = 0; /* Size of pLoc in bytes */
236537 int nDoc;
236538 int nDocsize;
236539 int ii;
236540 sFinder.iPos = 0;
236541 sFinder.nFirst = 0;
236542 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
236543 if( rc!=SQLITE_OK0 ) break;
236544 rc = pApi->xColumnLocale(pFts, i, &pLoc, &nLoc);
236545 if( rc!=SQLITE_OK0 ) break;
236546 rc = pApi->xTokenize_v2(pFts,
236547 sFinder.zDoc, nDoc, pLoc, nLoc, (void*)&sFinder, fts5SentenceFinderCb
236548 );
236549 if( rc!=SQLITE_OK0 ) break;
236550 rc = pApi->xColumnSize(pFts, i, &nDocsize);
236551 if( rc!=SQLITE_OK0 ) break;
236552
236553 for(ii=0; rc==SQLITE_OK0 && ii<nInst; ii++){
236554 int ip, ic, io;
236555 int iAdj;
236556 int nScore;
236557 int jj;
236558
236559 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
236560 if( ic!=i ) continue;
236561 if( io>nDocsize ) rc = FTS5_CORRUPT(11 | (1<<8));
236562 if( rc!=SQLITE_OK0 ) continue;
236563 memset(aSeen, 0, nPhrase);
236564 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
236565 io, nToken, &nScore, &iAdj
236566 );
236567 if( rc==SQLITE_OK0 && nScore>nBestScore ){
236568 nBestScore = nScore;
236569 iBestCol = i;
236570 iBestStart = iAdj;
236571 nColSize = nDocsize;
236572 }
236573
236574 if( rc==SQLITE_OK0 && sFinder.nFirst && nDocsize>nToken ){
236575 for(jj=0; jj<(sFinder.nFirst-1); jj++){
236576 if( sFinder.aFirst[jj+1]>io ) break;
236577 }
236578
236579 if( sFinder.aFirst[jj]<io ){
236580 memset(aSeen, 0, nPhrase);
236581 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
236582 sFinder.aFirst[jj], nToken, &nScore, 0
236583 );
236584
236585 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
236586 if( rc==SQLITE_OK0 && nScore>nBestScore ){
236587 nBestScore = nScore;
236588 iBestCol = i;
236589 iBestStart = sFinder.aFirst[jj];
236590 nColSize = nDocsize;
236591 }
236592 }
236593 }
236594 }
236595 }
236596 }
236597
236598 if( rc==SQLITE_OK0 ){
236599 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
236600 }
236601 if( rc==SQLITE_OK0 && nColSize==0 ){
236602 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
236603 }
236604 if( ctx.zIn ){
236605 const char *pLoc = 0; /* Locale of column iBestCol */
236606 int nLoc = 0; /* Bytes in pLoc */
236607
236608 if( rc==SQLITE_OK0 ){
236609 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
236610 }
236611
236612 ctx.iRangeStart = iBestStart;
236613 ctx.iRangeEnd = iBestStart + nToken - 1;
236614
236615 if( iBestStart>0 ){
236616 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
236617 }
236618
236619 /* Advance iterator ctx.iter so that it points to the first coalesced
236620 ** phrase instance at or following position iBestStart. */
236621 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK0 ){
236622 rc = fts5CInstIterNext(&ctx.iter);
236623 }
236624
236625 if( rc==SQLITE_OK0 ){
236626 rc = pApi->xColumnLocale(pFts, iBestCol, &pLoc, &nLoc);
236627 }
236628 if( rc==SQLITE_OK0 ){
236629 rc = pApi->xTokenize_v2(
236630 pFts, ctx.zIn, ctx.nIn, pLoc, nLoc, (void*)&ctx,fts5HighlightCb
236631 );
236632 }
236633 if( ctx.bOpen ){
236634 fts5HighlightAppend(&rc, &ctx, ctx.zClose, -1);
236635 }
236636 if( ctx.iRangeEnd>=(nColSize-1) ){
236637 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
236638 }else{
236639 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
236640 }
236641 }
236642 if( rc==SQLITE_OK0 ){
236643 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
236644 }else{
236645 sqlite3_result_error_code(pCtx, rc);
236646 }
236647 sqlite3_free(ctx.zOut);
236648 sqlite3_free(aSeen);
236649 sqlite3_free(sFinder.aFirst);
236650}
236651
236652/************************************************************************/
236653
236654/*
236655** The first time the bm25() function is called for a query, an instance
236656** of the following structure is allocated and populated.
236657*/
236658typedef struct Fts5Bm25Data Fts5Bm25Data;
236659struct Fts5Bm25Data {
236660 int nPhrase; /* Number of phrases in query */
236661 double avgdl; /* Average number of tokens in each row */
236662 double *aIDF; /* IDF for each phrase */
236663 double *aFreq; /* Array used to calculate phrase freq. */
236664};
236665
236666/*
236667** Callback used by fts5Bm25GetData() to count the number of rows in the
236668** table matched by each individual phrase within the query.
236669*/
236670static int fts5CountCb(
236671 const Fts5ExtensionApi *pApi,
236672 Fts5Context *pFts,
236673 void *pUserData /* Pointer to sqlite3_int64 variable */
236674){
236675 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
236676 UNUSED_PARAM2(pApi, pFts)(void)(pApi), (void)(pFts);
236677 (*pn)++;
236678 return SQLITE_OK0;
236679}
236680
236681/*
236682** Set *ppData to point to the Fts5Bm25Data object for the current query.
236683** If the object has not already been allocated, allocate and populate it
236684** now.
236685*/
236686static int fts5Bm25GetData(
236687 const Fts5ExtensionApi *pApi,
236688 Fts5Context *pFts,
236689 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
236690){
236691 int rc = SQLITE_OK0; /* Return code */
236692 Fts5Bm25Data *p; /* Object to return */
236693
236694 p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0);
236695 if( p==0 ){
236696 int nPhrase; /* Number of phrases in query */
236697 sqlite3_int64 nRow = 0; /* Number of rows in table */
236698 sqlite3_int64 nToken = 0; /* Number of tokens in table */
236699 sqlite3_int64 nByte; /* Bytes of space to allocate */
236700 int i;
236701
236702 /* Allocate the Fts5Bm25Data object */
236703 nPhrase = pApi->xPhraseCount(pFts);
236704 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
236705 p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
236706 if( p==0 ){
236707 rc = SQLITE_NOMEM7;
236708 }else{
236709 memset(p, 0, (size_t)nByte);
236710 p->nPhrase = nPhrase;
236711 p->aIDF = (double*)&p[1];
236712 p->aFreq = &p->aIDF[nPhrase];
236713 }
236714
236715 /* Calculate the average document length for this FTS5 table */
236716 if( rc==SQLITE_OK0 ) rc = pApi->xRowCount(pFts, &nRow);
236717 assert( rc!=SQLITE_OK || nRow>0 )((void) (0));
236718 if( rc==SQLITE_OK0 ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
236719 if( rc==SQLITE_OK0 ) p->avgdl = (double)nToken / (double)nRow;
236720
236721 /* Calculate an IDF for each phrase in the query */
236722 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
236723 sqlite3_int64 nHit = 0;
236724 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
236725 if( rc==SQLITE_OK0 ){
236726 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
236727 ** This is done using the standard BM25 formula as found on wikipedia:
236728 **
236729 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
236730 **
236731 ** where "N" is the total number of documents in the set and nHit
236732 ** is the number that contain at least one instance of the phrase
236733 ** under consideration.
236734 **
236735 ** The problem with this is that if (N < 2*nHit), the IDF is
236736 ** negative. Which is undesirable. So the mimimum allowable IDF is
236737 ** (1e-6) - roughly the same as a term that appears in just over
236738 ** half of set of 5,000,000 documents. */
236739 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
236740 if( idf<=0.0 ) idf = 1e-6;
236741 p->aIDF[i] = idf;
236742 }
236743 }
236744
236745 if( rc!=SQLITE_OK0 ){
236746 sqlite3_free(p);
236747 }else{
236748 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
236749 }
236750 if( rc!=SQLITE_OK0 ) p = 0;
236751 }
236752 *ppData = p;
236753 return rc;
236754}
236755
236756/*
236757** Implementation of bm25() function.
236758*/
236759static void fts5Bm25Function(
236760 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
236761 Fts5Context *pFts, /* First arg to pass to pApi functions */
236762 sqlite3_context *pCtx, /* Context for returning result/error */
236763 int nVal, /* Number of values in apVal[] array */
236764 sqlite3_value **apVal /* Array of trailing arguments */
236765){
236766 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
236767 const double b = 0.75; /* Constant "b" from BM25 formula */
236768 int rc; /* Error code */
236769 double score = 0.0; /* SQL function return value */
236770 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
236771 int i; /* Iterator variable */
236772 int nInst = 0; /* Value returned by xInstCount() */
236773 double D = 0.0; /* Total number of tokens in row */
236774 double *aFreq = 0; /* Array of phrase freq. for current row */
236775
236776 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
236777 ** for each phrase in the query for the current row. */
236778 rc = fts5Bm25GetData(pApi, pFts, &pData);
236779 if( rc==SQLITE_OK0 ){
236780 aFreq = pData->aFreq;
236781 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
236782 rc = pApi->xInstCount(pFts, &nInst);
236783 }
236784 for(i=0; rc==SQLITE_OK0 && i<nInst; i++){
236785 int ip; int ic; int io;
236786 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
236787 if( rc==SQLITE_OK0 ){
236788 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
236789 aFreq[ip] += w;
236790 }
236791 }
236792
236793 /* Figure out the total size of the current row in tokens. */
236794 if( rc==SQLITE_OK0 ){
236795 int nTok;
236796 rc = pApi->xColumnSize(pFts, -1, &nTok);
236797 D = (double)nTok;
236798 }
236799
236800 /* Determine and return the BM25 score for the current row. Or, if an
236801 ** error has occurred, throw an exception. */
236802 if( rc==SQLITE_OK0 ){
236803 for(i=0; i<pData->nPhrase; i++){
236804 score += pData->aIDF[i] * (
236805 ( aFreq[i] * (k1 + 1.0) ) /
236806 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
236807 );
236808 }
236809 sqlite3_result_double(pCtx, -1.0 * score);
236810 }else{
236811 sqlite3_result_error_code(pCtx, rc);
236812 }
236813}
236814
236815/*
236816** Implementation of fts5_get_locale() function.
236817*/
236818static void fts5GetLocaleFunction(
236819 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
236820 Fts5Context *pFts, /* First arg to pass to pApi functions */
236821 sqlite3_context *pCtx, /* Context for returning result/error */
236822 int nVal, /* Number of values in apVal[] array */
236823 sqlite3_value **apVal /* Array of trailing arguments */
236824){
236825 int iCol = 0;
236826 int eType = 0;
236827 int rc = SQLITE_OK0;
236828 const char *zLocale = 0;
236829 int nLocale = 0;
236830
236831 /* xColumnLocale() must be available */
236832 assert( pApi->iVersion>=4 )((void) (0));
236833
236834 if( nVal!=1 ){
236835 const char *z = "wrong number of arguments to function fts5_get_locale()";
236836 sqlite3_result_error(pCtx, z, -1);
236837 return;
236838 }
236839
236840 eType = sqlite3_value_numeric_type(apVal[0]);
236841 if( eType!=SQLITE_INTEGER1 ){
236842 const char *z = "non-integer argument passed to function fts5_get_locale()";
236843 sqlite3_result_error(pCtx, z, -1);
236844 return;
236845 }
236846
236847 iCol = sqlite3_value_int(apVal[0]);
236848 if( iCol<0 || iCol>=pApi->xColumnCount(pFts) ){
236849 sqlite3_result_error_code(pCtx, SQLITE_RANGE25);
236850 return;
236851 }
236852
236853 rc = pApi->xColumnLocale(pFts, iCol, &zLocale, &nLocale);
236854 if( rc!=SQLITE_OK0 ){
236855 sqlite3_result_error_code(pCtx, rc);
236856 return;
236857 }
236858
236859 sqlite3_result_text(pCtx, zLocale, nLocale, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
236860}
236861
236862static int sqlite3Fts5AuxInit(fts5_api *pApi){
236863 struct Builtin {
236864 const char *zFunc; /* Function name (nul-terminated) */
236865 void *pUserData; /* User-data pointer */
236866 fts5_extension_function xFunc;/* Callback function */
236867 void (*xDestroy)(void*); /* Destructor function */
236868 } aBuiltin [] = {
236869 { "snippet", 0, fts5SnippetFunction, 0 },
236870 { "highlight", 0, fts5HighlightFunction, 0 },
236871 { "bm25", 0, fts5Bm25Function, 0 },
236872 { "fts5_get_locale", 0, fts5GetLocaleFunction, 0 },
236873 };
236874 int rc = SQLITE_OK0; /* Return code */
236875 int i; /* To iterate through builtin functions */
236876
236877 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
236878 rc = pApi->xCreateFunction(pApi,
236879 aBuiltin[i].zFunc,
236880 aBuiltin[i].pUserData,
236881 aBuiltin[i].xFunc,
236882 aBuiltin[i].xDestroy
236883 );
236884 }
236885
236886 return rc;
236887}
236888
236889/*
236890** 2014 May 31
236891**
236892** The author disclaims copyright to this source code. In place of
236893** a legal notice, here is a blessing:
236894**
236895** May you do good and not evil.
236896** May you find forgiveness for yourself and forgive others.
236897** May you share freely, never taking more than you give.
236898**
236899******************************************************************************
236900*/
236901
236902
236903
236904/* #include "fts5Int.h" */
236905
236906static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
236907 if( (u32)pBuf->nSpace<nByte ){
236908 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
236909 u8 *pNew;
236910 while( nNew<nByte ){
236911 nNew = nNew * 2;
236912 }
236913 pNew = sqlite3_realloc64(pBuf->p, nNew);
236914 if( pNew==0 ){
236915 *pRc = SQLITE_NOMEM7;
236916 return 1;
236917 }else{
236918 pBuf->nSpace = (int)nNew;
236919 pBuf->p = pNew;
236920 }
236921 }
236922 return 0;
236923}
236924
236925
236926/*
236927** Encode value iVal as an SQLite varint and append it to the buffer object
236928** pBuf. If an OOM error occurs, set the error code in p.
236929*/
236930static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
236931 if( fts5BufferGrow(pRc, pBuf, 9)( (u32)((pBuf)->n) + (u32)(9) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(9)+(pBuf)->n) )
) return;
236932 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
236933}
236934
236935static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
236936 aBuf[0] = (iVal>>24) & 0x00FF;
236937 aBuf[1] = (iVal>>16) & 0x00FF;
236938 aBuf[2] = (iVal>> 8) & 0x00FF;
236939 aBuf[3] = (iVal>> 0) & 0x00FF;
236940}
236941
236942static int sqlite3Fts5Get32(const u8 *aBuf){
236943 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
236944}
236945
236946/*
236947** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
236948** the error code in p. If an error has already occurred when this function
236949** is called, it is a no-op.
236950*/
236951static void sqlite3Fts5BufferAppendBlob(
236952 int *pRc,
236953 Fts5Buffer *pBuf,
236954 u32 nData,
236955 const u8 *pData
236956){
236957 if( nData ){
236958 if( fts5BufferGrow(pRc, pBuf, nData)( (u32)((pBuf)->n) + (u32)(nData) <= (u32)((pBuf)->nSpace
) ? 0 : sqlite3Fts5BufferSize((pRc),(pBuf),(nData)+(pBuf)->
n) )
) return;
236959 assert( pBuf->p!=0 )((void) (0));
236960 memcpy(&pBuf->p[pBuf->n], pData, nData);
236961 pBuf->n += nData;
236962 }
236963}
236964
236965/*
236966** Append the nul-terminated string zStr to the buffer pBuf. This function
236967** ensures that the byte following the buffer data is set to 0x00, even
236968** though this byte is not included in the pBuf->n count.
236969*/
236970static void sqlite3Fts5BufferAppendString(
236971 int *pRc,
236972 Fts5Buffer *pBuf,
236973 const char *zStr
236974){
236975 int nStr = (int)strlen(zStr);
236976 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
236977 pBuf->n--;
236978}
236979
236980/*
236981** Argument zFmt is a printf() style format string. This function performs
236982** the printf() style processing, then appends the results to buffer pBuf.
236983**
236984** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
236985** following the buffer data is set to 0x00, even though this byte is not
236986** included in the pBuf->n count.
236987*/
236988static void sqlite3Fts5BufferAppendPrintf(
236989 int *pRc,
236990 Fts5Buffer *pBuf,
236991 char *zFmt, ...
236992){
236993 if( *pRc==SQLITE_OK0 ){
236994 char *zTmp;
236995 va_list ap;
236996 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
236997 zTmp = sqlite3_vmprintf(zFmt, ap);
236998 va_end(ap)__builtin_va_end(ap);
236999
237000 if( zTmp==0 ){
237001 *pRc = SQLITE_NOMEM7;
237002 }else{
237003 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
237004 sqlite3_free(zTmp);
237005 }
237006 }
237007}
237008
237009static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
237010 char *zRet = 0;
237011 if( *pRc==SQLITE_OK0 ){
237012 va_list ap;
237013 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
237014 zRet = sqlite3_vmprintf(zFmt, ap);
237015 va_end(ap)__builtin_va_end(ap);
237016 if( zRet==0 ){
237017 *pRc = SQLITE_NOMEM7;
237018 }
237019 }
237020 return zRet;
237021}
237022
237023
237024/*
237025** Free any buffer allocated by pBuf. Zero the structure before returning.
237026*/
237027static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
237028 sqlite3_free(pBuf->p);
237029 memset(pBuf, 0, sizeof(Fts5Buffer));
237030}
237031
237032/*
237033** Zero the contents of the buffer object. But do not free the associated
237034** memory allocation.
237035*/
237036static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
237037 pBuf->n = 0;
237038}
237039
237040/*
237041** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
237042** the error code in p. If an error has already occurred when this function
237043** is called, it is a no-op.
237044*/
237045static void sqlite3Fts5BufferSet(
237046 int *pRc,
237047 Fts5Buffer *pBuf,
237048 int nData,
237049 const u8 *pData
237050){
237051 pBuf->n = 0;
237052 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
237053}
237054
237055static int sqlite3Fts5PoslistNext64(
237056 const u8 *a, int n, /* Buffer containing poslist */
237057 int *pi, /* IN/OUT: Offset within a[] */
237058 i64 *piOff /* IN/OUT: Current offset */
237059){
237060 int i = *pi;
237061 assert( a!=0 || i==0 )((void) (0));
237062 if( i>=n ){
237063 /* EOF */
237064 *piOff = -1;
237065 return 1;
237066 }else{
237067 i64 iOff = *piOff;
237068 u32 iVal;
237069 assert( a!=0 )((void) (0));
237070 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&(iVal)); } }
;
237071 if( iVal<=1 ){
237072 if( iVal==0 ){
237073 *pi = i;
237074 return 0;
237075 }
237076 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&(iVal)); } }
;
237077 iOff = ((i64)iVal) << 32;
237078 assert( iOff>=0 )((void) (0));
237079 fts5FastGetVarint32(a, i, iVal){ iVal = (a)[i++]; if( iVal & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(a)[i],(u32*)&(iVal)); } }
;
237080 if( iVal<2 ){
237081 /* This is a corrupt record. So stop parsing it here. */
237082 *piOff = -1;
237083 return 1;
237084 }
237085 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
237086 }else{
237087 *piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF);
237088 }
237089 *pi = i;
237090 assert_nc( *piOff>=iOff )((void) (0));
237091 return 0;
237092 }
237093}
237094
237095
237096/*
237097** Advance the iterator object passed as the only argument. Return true
237098** if the iterator reaches EOF, or false otherwise.
237099*/
237100static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
237101 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
237102 pIter->bEof = 1;
237103 }
237104 return pIter->bEof;
237105}
237106
237107static int sqlite3Fts5PoslistReaderInit(
237108 const u8 *a, int n, /* Poslist buffer to iterate through */
237109 Fts5PoslistReader *pIter /* Iterator object to initialize */
237110){
237111 memset(pIter, 0, sizeof(*pIter));
237112 pIter->a = a;
237113 pIter->n = n;
237114 sqlite3Fts5PoslistReaderNext(pIter);
237115 return pIter->bEof;
237116}
237117
237118/*
237119** Append position iPos to the position list being accumulated in buffer
237120** pBuf, which must be already be large enough to hold the new data.
237121** The previous position written to this list is *piPrev. *piPrev is set
237122** to iPos before returning.
237123*/
237124static void sqlite3Fts5PoslistSafeAppend(
237125 Fts5Buffer *pBuf,
237126 i64 *piPrev,
237127 i64 iPos
237128){
237129 if( iPos>=*piPrev ){
237130 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
237131 if( (iPos & colmask) != (*piPrev & colmask) ){
237132 pBuf->p[pBuf->n++] = 1;
237133 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
237134 *piPrev = (iPos & colmask);
237135 }
237136 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
237137 *piPrev = iPos;
237138 }
237139}
237140
237141static int sqlite3Fts5PoslistWriterAppend(
237142 Fts5Buffer *pBuf,
237143 Fts5PoslistWriter *pWriter,
237144 i64 iPos
237145){
237146 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
237147 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;
237148 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
237149 return SQLITE_OK0;
237150}
237151
237152static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
237153 void *pRet = 0;
237154 if( *pRc==SQLITE_OK0 ){
237155 pRet = sqlite3_malloc64(nByte);
237156 if( pRet==0 ){
237157 if( nByte>0 ) *pRc = SQLITE_NOMEM7;
237158 }else{
237159 memset(pRet, 0, (size_t)nByte);
237160 }
237161 }
237162 return pRet;
237163}
237164
237165/*
237166** Return a nul-terminated copy of the string indicated by pIn. If nIn
237167** is non-negative, then it is the length of the string in bytes. Otherwise,
237168** the length of the string is determined using strlen().
237169**
237170** It is the responsibility of the caller to eventually free the returned
237171** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
237172*/
237173static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
237174 char *zRet = 0;
237175 if( *pRc==SQLITE_OK0 ){
237176 if( nIn<0 ){
237177 nIn = (int)strlen(pIn);
237178 }
237179 zRet = (char*)sqlite3_malloc(nIn+1);
237180 if( zRet ){
237181 memcpy(zRet, pIn, nIn);
237182 zRet[nIn] = '\0';
237183 }else{
237184 *pRc = SQLITE_NOMEM7;
237185 }
237186 }
237187 return zRet;
237188}
237189
237190
237191/*
237192** Return true if character 't' may be part of an FTS5 bareword, or false
237193** otherwise. Characters that may be part of barewords:
237194**
237195** * All non-ASCII characters,
237196** * The 52 upper and lower case ASCII characters, and
237197** * The 10 integer ASCII characters.
237198** * The underscore character "_" (0x5F).
237199** * The unicode "subsitute" character (0x1A).
237200*/
237201static int sqlite3Fts5IsBareword(char t){
237202 u8 aBareword[128] = {
237203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
237204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
237205 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
237206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
237207 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
237208 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
237209 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
237210 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
237211 };
237212
237213 return (t & 0x80) || aBareword[(int)t];
237214}
237215
237216
237217/*************************************************************************
237218*/
237219typedef struct Fts5TermsetEntry Fts5TermsetEntry;
237220struct Fts5TermsetEntry {
237221 char *pTerm;
237222 int nTerm;
237223 int iIdx; /* Index (main or aPrefix[] entry) */
237224 Fts5TermsetEntry *pNext;
237225};
237226
237227struct Fts5Termset {
237228 Fts5TermsetEntry *apHash[512];
237229};
237230
237231static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
237232 int rc = SQLITE_OK0;
237233 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
237234 return rc;
237235}
237236
237237static int sqlite3Fts5TermsetAdd(
237238 Fts5Termset *p,
237239 int iIdx,
237240 const char *pTerm, int nTerm,
237241 int *pbPresent
237242){
237243 int rc = SQLITE_OK0;
237244 *pbPresent = 0;
237245 if( p ){
237246 int i;
237247 u32 hash = 13;
237248 Fts5TermsetEntry *pEntry;
237249
237250 /* Calculate a hash value for this term. This is the same hash checksum
237251 ** used by the fts5_hash.c module. This is not important for correct
237252 ** operation of the module, but is necessary to ensure that some tests
237253 ** designed to produce hash table collisions really do work. */
237254 for(i=nTerm-1; i>=0; i--){
237255 hash = (hash << 3) ^ hash ^ pTerm[i];
237256 }
237257 hash = (hash << 3) ^ hash ^ iIdx;
237258 hash = hash % ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0])));
237259
237260 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
237261 if( pEntry->iIdx==iIdx
237262 && pEntry->nTerm==nTerm
237263 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
237264 ){
237265 *pbPresent = 1;
237266 break;
237267 }
237268 }
237269
237270 if( pEntry==0 ){
237271 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
237272 if( pEntry ){
237273 pEntry->pTerm = (char*)&pEntry[1];
237274 pEntry->nTerm = nTerm;
237275 pEntry->iIdx = iIdx;
237276 memcpy(pEntry->pTerm, pTerm, nTerm);
237277 pEntry->pNext = p->apHash[hash];
237278 p->apHash[hash] = pEntry;
237279 }
237280 }
237281 }
237282
237283 return rc;
237284}
237285
237286static void sqlite3Fts5TermsetFree(Fts5Termset *p){
237287 if( p ){
237288 u32 i;
237289 for(i=0; i<ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0]))); i++){
237290 Fts5TermsetEntry *pEntry = p->apHash[i];
237291 while( pEntry ){
237292 Fts5TermsetEntry *pDel = pEntry;
237293 pEntry = pEntry->pNext;
237294 sqlite3_free(pDel);
237295 }
237296 }
237297 sqlite3_free(p);
237298 }
237299}
237300
237301/*
237302** 2014 Jun 09
237303**
237304** The author disclaims copyright to this source code. In place of
237305** a legal notice, here is a blessing:
237306**
237307** May you do good and not evil.
237308** May you find forgiveness for yourself and forgive others.
237309** May you share freely, never taking more than you give.
237310**
237311******************************************************************************
237312**
237313** This is an SQLite module implementing full-text search.
237314*/
237315
237316
237317/* #include "fts5Int.h" */
237318
237319#define FTS5_DEFAULT_PAGE_SIZE4050 4050
237320#define FTS5_DEFAULT_AUTOMERGE4 4
237321#define FTS5_DEFAULT_USERMERGE4 4
237322#define FTS5_DEFAULT_CRISISMERGE16 16
237323#define FTS5_DEFAULT_HASHSIZE(1024*1024) (1024*1024)
237324
237325#define FTS5_DEFAULT_DELETE_AUTOMERGE10 10 /* default 10% */
237326
237327/* Maximum allowed page size */
237328#define FTS5_MAX_PAGE_SIZE(64*1024) (64*1024)
237329
237330static int fts5_iswhitespace(char x){
237331 return (x==' ');
237332}
237333
237334static int fts5_isopenquote(char x){
237335 return (x=='"' || x=='\'' || x=='[' || x=='`');
237336}
237337
237338/*
237339** Argument pIn points to a character that is part of a nul-terminated
237340** string. Return a pointer to the first character following *pIn in
237341** the string that is not a white-space character.
237342*/
237343static const char *fts5ConfigSkipWhitespace(const char *pIn){
237344 const char *p = pIn;
237345 if( p ){
237346 while( fts5_iswhitespace(*p) ){ p++; }
237347 }
237348 return p;
237349}
237350
237351/*
237352** Argument pIn points to a character that is part of a nul-terminated
237353** string. Return a pointer to the first character following *pIn in
237354** the string that is not a "bareword" character.
237355*/
237356static const char *fts5ConfigSkipBareword(const char *pIn){
237357 const char *p = pIn;
237358 while ( sqlite3Fts5IsBareword(*p) ) p++;
237359 if( p==pIn ) p = 0;
237360 return p;
237361}
237362
237363static int fts5_isdigit(char a){
237364 return (a>='0' && a<='9');
237365}
237366
237367
237368
237369static const char *fts5ConfigSkipLiteral(const char *pIn){
237370 const char *p = pIn;
237371 switch( *p ){
237372 case 'n': case 'N':
237373 if( sqlite3_strnicmp("null", p, 4)==0 ){
237374 p = &p[4];
237375 }else{
237376 p = 0;
237377 }
237378 break;
237379
237380 case 'x': case 'X':
237381 p++;
237382 if( *p=='\'' ){
237383 p++;
237384 while( (*p>='a' && *p<='f')
237385 || (*p>='A' && *p<='F')
237386 || (*p>='0' && *p<='9')
237387 ){
237388 p++;
237389 }
237390 if( *p=='\'' && 0==((p-pIn)%2) ){
237391 p++;
237392 }else{
237393 p = 0;
237394 }
237395 }else{
237396 p = 0;
237397 }
237398 break;
237399
237400 case '\'':
237401 p++;
237402 while( p ){
237403 if( *p=='\'' ){
237404 p++;
237405 if( *p!='\'' ) break;
237406 }
237407 p++;
237408 if( *p==0 ) p = 0;
237409 }
237410 break;
237411
237412 default:
237413 /* maybe a number */
237414 if( *p=='+' || *p=='-' ) p++;
237415 while( fts5_isdigit(*p) ) p++;
237416
237417 /* At this point, if the literal was an integer, the parse is
237418 ** finished. Or, if it is a floating point value, it may continue
237419 ** with either a decimal point or an 'E' character. */
237420 if( *p=='.' && fts5_isdigit(p[1]) ){
237421 p += 2;
237422 while( fts5_isdigit(*p) ) p++;
237423 }
237424 if( p==pIn ) p = 0;
237425
237426 break;
237427 }
237428
237429 return p;
237430}
237431
237432/*
237433** The first character of the string pointed to by argument z is guaranteed
237434** to be an open-quote character (see function fts5_isopenquote()).
237435**
237436** This function searches for the corresponding close-quote character within
237437** the string and, if found, dequotes the string in place and adds a new
237438** nul-terminator byte.
237439**
237440** If the close-quote is found, the value returned is the byte offset of
237441** the character immediately following it. Or, if the close-quote is not
237442** found, -1 is returned. If -1 is returned, the buffer is left in an
237443** undefined state.
237444*/
237445static int fts5Dequote(char *z){
237446 char q;
237447 int iIn = 1;
237448 int iOut = 0;
237449 q = z[0];
237450
237451 /* Set stack variable q to the close-quote character */
237452 assert( q=='[' || q=='\'' || q=='"' || q=='`' )((void) (0));
237453 if( q=='[' ) q = ']';
237454
237455 while( z[iIn] ){
237456 if( z[iIn]==q ){
237457 if( z[iIn+1]!=q ){
237458 /* Character iIn was the close quote. */
237459 iIn++;
237460 break;
237461 }else{
237462 /* Character iIn and iIn+1 form an escaped quote character. Skip
237463 ** the input cursor past both and copy a single quote character
237464 ** to the output buffer. */
237465 iIn += 2;
237466 z[iOut++] = q;
237467 }
237468 }else{
237469 z[iOut++] = z[iIn++];
237470 }
237471 }
237472
237473 z[iOut] = '\0';
237474 return iIn;
237475}
237476
237477/*
237478** Convert an SQL-style quoted string into a normal string by removing
237479** the quote characters. The conversion is done in-place. If the
237480** input does not begin with a quote character, then this routine
237481** is a no-op.
237482**
237483** Examples:
237484**
237485** "abc" becomes abc
237486** 'xyz' becomes xyz
237487** [pqr] becomes pqr
237488** `mno` becomes mno
237489*/
237490static void sqlite3Fts5Dequote(char *z){
237491 char quote; /* Quote character (if any ) */
237492
237493 assert( 0==fts5_iswhitespace(z[0]) )((void) (0));
237494 quote = z[0];
237495 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
237496 fts5Dequote(z);
237497 }
237498}
237499
237500
237501struct Fts5Enum {
237502 const char *zName;
237503 int eVal;
237504};
237505typedef struct Fts5Enum Fts5Enum;
237506
237507static int fts5ConfigSetEnum(
237508 const Fts5Enum *aEnum,
237509 const char *zEnum,
237510 int *peVal
237511){
237512 int nEnum = (int)strlen(zEnum);
237513 int i;
237514 int iVal = -1;
237515
237516 for(i=0; aEnum[i].zName; i++){
237517 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
237518 if( iVal>=0 ) return SQLITE_ERROR1;
237519 iVal = aEnum[i].eVal;
237520 }
237521 }
237522
237523 *peVal = iVal;
237524 return iVal<0 ? SQLITE_ERROR1 : SQLITE_OK0;
237525}
237526
237527/*
237528** Parse a "special" CREATE VIRTUAL TABLE directive and update
237529** configuration object pConfig as appropriate.
237530**
237531** If successful, object pConfig is updated and SQLITE_OK returned. If
237532** an error occurs, an SQLite error code is returned and an error message
237533** may be left in *pzErr. It is the responsibility of the caller to
237534** eventually free any such error message using sqlite3_free().
237535*/
237536static int fts5ConfigParseSpecial(
237537 Fts5Config *pConfig, /* Configuration object to update */
237538 const char *zCmd, /* Special command to parse */
237539 const char *zArg, /* Argument to parse */
237540 char **pzErr /* OUT: Error message */
237541){
237542 int rc = SQLITE_OK0;
237543 int nCmd = (int)strlen(zCmd);
237544
237545 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
237546 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES31;
237547 const char *p;
237548 int bFirst = 1;
237549 if( pConfig->aPrefix==0 ){
237550 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
237551 if( rc ) return rc;
237552 }
237553
237554 p = zArg;
237555 while( 1 ){
237556 int nPre = 0;
237557
237558 while( p[0]==' ' ) p++;
237559 if( bFirst==0 && p[0]==',' ){
237560 p++;
237561 while( p[0]==' ' ) p++;
237562 }else if( p[0]=='\0' ){
237563 break;
237564 }
237565 if( p[0]<'0' || p[0]>'9' ){
237566 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
237567 rc = SQLITE_ERROR1;
237568 break;
237569 }
237570
237571 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES31 ){
237572 *pzErr = sqlite3_mprintf(
237573 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES31
237574 );
237575 rc = SQLITE_ERROR1;
237576 break;
237577 }
237578
237579 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
237580 nPre = nPre*10 + (p[0] - '0');
237581 p++;
237582 }
237583
237584 if( nPre<=0 || nPre>=1000 ){
237585 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
237586 rc = SQLITE_ERROR1;
237587 break;
237588 }
237589
237590 pConfig->aPrefix[pConfig->nPrefix] = nPre;
237591 pConfig->nPrefix++;
237592 bFirst = 0;
237593 }
237594 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES )((void) (0));
237595 return rc;
237596 }
237597
237598 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
237599 const char *p = (const char*)zArg;
237600 sqlite3_int64 nArg = strlen(zArg) + 1;
237601 char **azArg = sqlite3Fts5MallocZero(&rc, (sizeof(char*) + 2) * nArg);
237602
237603 if( azArg ){
237604 char *pSpace = (char*)&azArg[nArg];
237605 if( pConfig->t.azArg ){
237606 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
237607 rc = SQLITE_ERROR1;
237608 }else{
237609 for(nArg=0; p && *p; nArg++){
237610 const char *p2 = fts5ConfigSkipWhitespace(p);
237611 if( *p2=='\'' ){
237612 p = fts5ConfigSkipLiteral(p2);
237613 }else{
237614 p = fts5ConfigSkipBareword(p2);
237615 }
237616 if( p ){
237617 memcpy(pSpace, p2, p-p2);
237618 azArg[nArg] = pSpace;
237619 sqlite3Fts5Dequote(pSpace);
237620 pSpace += (p - p2) + 1;
237621 p = fts5ConfigSkipWhitespace(p);
237622 }
237623 }
237624 if( p==0 ){
237625 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
237626 rc = SQLITE_ERROR1;
237627 }else{
237628 pConfig->t.azArg = (const char**)azArg;
237629 pConfig->t.nArg = nArg;
237630 azArg = 0;
237631 }
237632 }
237633 }
237634 sqlite3_free(azArg);
237635
237636 return rc;
237637 }
237638
237639 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
237640 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0 ){
237641 *pzErr = sqlite3_mprintf("multiple content=... directives");
237642 rc = SQLITE_ERROR1;
237643 }else{
237644 if( zArg[0] ){
237645 pConfig->eContent = FTS5_CONTENT_EXTERNAL2;
237646 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
237647 }else{
237648 pConfig->eContent = FTS5_CONTENT_NONE1;
237649 }
237650 }
237651 return rc;
237652 }
237653
237654 if( sqlite3_strnicmp("contentless_delete", zCmd, nCmd)==0 ){
237655 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
237656 *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive");
237657 rc = SQLITE_ERROR1;
237658 }else{
237659 pConfig->bContentlessDelete = (zArg[0]=='1');
237660 }
237661 return rc;
237662 }
237663
237664 if( sqlite3_strnicmp("contentless_unindexed", zCmd, nCmd)==0 ){
237665 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
237666 *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive");
237667 rc = SQLITE_ERROR1;
237668 }else{
237669 pConfig->bContentlessUnindexed = (zArg[0]=='1');
237670 }
237671 return rc;
237672 }
237673
237674 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
237675 if( pConfig->zContentRowid ){
237676 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
237677 rc = SQLITE_ERROR1;
237678 }else{
237679 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
237680 }
237681 return rc;
237682 }
237683
237684 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
237685 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
237686 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
237687 rc = SQLITE_ERROR1;
237688 }else{
237689 pConfig->bColumnsize = (zArg[0]=='1');
237690 }
237691 return rc;
237692 }
237693
237694 if( sqlite3_strnicmp("locale", zCmd, nCmd)==0 ){
237695 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
237696 *pzErr = sqlite3_mprintf("malformed locale=... directive");
237697 rc = SQLITE_ERROR1;
237698 }else{
237699 pConfig->bLocale = (zArg[0]=='1');
237700 }
237701 return rc;
237702 }
237703
237704 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
237705 const Fts5Enum aDetail[] = {
237706 { "none", FTS5_DETAIL_NONE1 },
237707 { "full", FTS5_DETAIL_FULL0 },
237708 { "columns", FTS5_DETAIL_COLUMNS2 },
237709 { 0, 0 }
237710 };
237711
237712 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
237713 *pzErr = sqlite3_mprintf("malformed detail=... directive");
237714 }
237715 return rc;
237716 }
237717
237718 if( sqlite3_strnicmp("tokendata", zCmd, nCmd)==0 ){
237719 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
237720 *pzErr = sqlite3_mprintf("malformed tokendata=... directive");
237721 rc = SQLITE_ERROR1;
237722 }else{
237723 pConfig->bTokendata = (zArg[0]=='1');
237724 }
237725 return rc;
237726 }
237727
237728 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
237729 return SQLITE_ERROR1;
237730}
237731
237732/*
237733** Gobble up the first bareword or quoted word from the input buffer zIn.
237734** Return a pointer to the character immediately following the last in
237735** the gobbled word if successful, or a NULL pointer otherwise (failed
237736** to find close-quote character).
237737**
237738** Before returning, set pzOut to point to a new buffer containing a
237739** nul-terminated, dequoted copy of the gobbled word. If the word was
237740** quoted, *pbQuoted is also set to 1 before returning.
237741**
237742** If *pRc is other than SQLITE_OK when this function is called, it is
237743** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
237744** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
237745** set if a parse error (failed to find close quote) occurs.
237746*/
237747static const char *fts5ConfigGobbleWord(
237748 int *pRc, /* IN/OUT: Error code */
237749 const char *zIn, /* Buffer to gobble string/bareword from */
237750 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
237751 int *pbQuoted /* OUT: Set to true if dequoting required */
237752){
237753 const char *zRet = 0;
237754
237755 sqlite3_int64 nIn = strlen(zIn);
237756 char *zOut = sqlite3_malloc64(nIn+1);
237757
237758 assert( *pRc==SQLITE_OK )((void) (0));
237759 *pbQuoted = 0;
237760 *pzOut = 0;
237761
237762 if( zOut==0 ){
237763 *pRc = SQLITE_NOMEM7;
237764 }else{
237765 memcpy(zOut, zIn, (size_t)(nIn+1));
237766 if( fts5_isopenquote(zOut[0]) ){
237767 int ii = fts5Dequote(zOut);
237768 zRet = &zIn[ii];
237769 *pbQuoted = 1;
237770 }else{
237771 zRet = fts5ConfigSkipBareword(zIn);
237772 if( zRet ){
237773 zOut[zRet-zIn] = '\0';
237774 }
237775 }
237776 }
237777
237778 if( zRet==0 ){
237779 sqlite3_free(zOut);
237780 }else{
237781 *pzOut = zOut;
237782 }
237783
237784 return zRet;
237785}
237786
237787static int fts5ConfigParseColumn(
237788 Fts5Config *p,
237789 char *zCol,
237790 char *zArg,
237791 char **pzErr,
237792 int *pbUnindexed
237793){
237794 int rc = SQLITE_OK0;
237795 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME"rank")
237796 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME"rowid")
237797 ){
237798 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
237799 rc = SQLITE_ERROR1;
237800 }else if( zArg ){
237801 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
237802 p->abUnindexed[p->nCol] = 1;
237803 *pbUnindexed = 1;
237804 }else{
237805 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
237806 rc = SQLITE_ERROR1;
237807 }
237808 }
237809
237810 p->azCol[p->nCol++] = zCol;
237811 return rc;
237812}
237813
237814/*
237815** Populate the Fts5Config.zContentExprlist string.
237816*/
237817static int fts5ConfigMakeExprlist(Fts5Config *p){
237818 int i;
237819 int rc = SQLITE_OK0;
237820 Fts5Buffer buf = {0, 0, 0};
237821
237822 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
237823 if( p->eContent!=FTS5_CONTENT_NONE1 ){
237824 assert( p->eContent==FTS5_CONTENT_EXTERNAL((void) (0))
237825 || p->eContent==FTS5_CONTENT_NORMAL((void) (0))
237826 || p->eContent==FTS5_CONTENT_UNINDEXED((void) (0))
237827 )((void) (0));
237828 for(i=0; i<p->nCol; i++){
237829 if( p->eContent==FTS5_CONTENT_EXTERNAL2 ){
237830 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
237831 }else if( p->eContent==FTS5_CONTENT_NORMAL0 || p->abUnindexed[i] ){
237832 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
237833 }else{
237834 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL");
237835 }
237836 }
237837 }
237838 if( p->eContent==FTS5_CONTENT_NORMAL0 && p->bLocale ){
237839 for(i=0; i<p->nCol; i++){
237840 if( p->abUnindexed[i]==0 ){
237841 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.l%d", i);
237842 }else{
237843 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL");
237844 }
237845 }
237846 }
237847
237848 assert( p->zContentExprlist==0 )((void) (0));
237849 p->zContentExprlist = (char*)buf.p;
237850 return rc;
237851}
237852
237853/*
237854** Arguments nArg/azArg contain the string arguments passed to the xCreate
237855** or xConnect method of the virtual table. This function attempts to
237856** allocate an instance of Fts5Config containing the results of parsing
237857** those arguments.
237858**
237859** If successful, SQLITE_OK is returned and *ppOut is set to point to the
237860** new Fts5Config object. If an error occurs, an SQLite error code is
237861** returned, *ppOut is set to NULL and an error message may be left in
237862** *pzErr. It is the responsibility of the caller to eventually free any
237863** such error message using sqlite3_free().
237864*/
237865static int sqlite3Fts5ConfigParse(
237866 Fts5Global *pGlobal,
237867 sqlite3 *db,
237868 int nArg, /* Number of arguments */
237869 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
237870 Fts5Config **ppOut, /* OUT: Results of parse */
237871 char **pzErr /* OUT: Error message */
237872){
237873 int rc = SQLITE_OK0; /* Return code */
237874 Fts5Config *pRet; /* New object to return */
237875 int i;
237876 sqlite3_int64 nByte;
237877 int bUnindexed = 0; /* True if there are one or more UNINDEXED */
237878
237879 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
237880 if( pRet==0 ) return SQLITE_NOMEM7;
237881 memset(pRet, 0, sizeof(Fts5Config));
237882 pRet->pGlobal = pGlobal;
237883 pRet->db = db;
237884 pRet->iCookie = -1;
237885
237886 nByte = nArg * (sizeof(char*) + sizeof(u8));
237887 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
237888 pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0;
237889 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
237890 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
237891 pRet->bColumnsize = 1;
237892 pRet->eDetail = FTS5_DETAIL_FULL0;
237893#ifdef SQLITE_DEBUG
237894 pRet->bPrefixIndex = 1;
237895#endif
237896 if( rc==SQLITE_OK0 && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME"rank")==0 ){
237897 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
237898 rc = SQLITE_ERROR1;
237899 }
237900
237901 assert( (pRet->abUnindexed && pRet->azCol) || rc!=SQLITE_OK )((void) (0));
237902 for(i=3; rc==SQLITE_OK0 && i<nArg; i++){
237903 const char *zOrig = azArg[i];
237904 const char *z;
237905 char *zOne = 0;
237906 char *zTwo = 0;
237907 int bOption = 0;
237908 int bMustBeCol = 0;
237909
237910 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
237911 z = fts5ConfigSkipWhitespace(z);
237912 if( z && *z=='=' ){
237913 bOption = 1;
237914 assert( zOne!=0 )((void) (0));
237915 z++;
237916 if( bMustBeCol ) z = 0;
237917 }
237918 z = fts5ConfigSkipWhitespace(z);
237919 if( z && z[0] ){
237920 int bDummy;
237921 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
237922 if( z && z[0] ) z = 0;
237923 }
237924
237925 if( rc==SQLITE_OK0 ){
237926 if( z==0 ){
237927 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
237928 rc = SQLITE_ERROR1;
237929 }else{
237930 if( bOption ){
237931 rc = fts5ConfigParseSpecial(pRet,
237932 ALWAYS(zOne)(zOne)?zOne:"",
237933 zTwo?zTwo:"",
237934 pzErr
237935 );
237936 }else{
237937 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr, &bUnindexed);
237938 zOne = 0;
237939 }
237940 }
237941 }
237942
237943 sqlite3_free(zOne);
237944 sqlite3_free(zTwo);
237945 }
237946
237947 /* We only allow contentless_delete=1 if the table is indeed contentless. */
237948 if( rc==SQLITE_OK0
237949 && pRet->bContentlessDelete
237950 && pRet->eContent!=FTS5_CONTENT_NONE1
237951 ){
237952 *pzErr = sqlite3_mprintf(
237953 "contentless_delete=1 requires a contentless table"
237954 );
237955 rc = SQLITE_ERROR1;
237956 }
237957
237958 /* We only allow contentless_delete=1 if columnsize=0 is not present.
237959 **
237960 ** This restriction may be removed at some point.
237961 */
237962 if( rc==SQLITE_OK0 && pRet->bContentlessDelete && pRet->bColumnsize==0 ){
237963 *pzErr = sqlite3_mprintf(
237964 "contentless_delete=1 is incompatible with columnsize=0"
237965 );
237966 rc = SQLITE_ERROR1;
237967 }
237968
237969 /* We only allow contentless_unindexed=1 if the table is actually a
237970 ** contentless one.
237971 */
237972 if( rc==SQLITE_OK0
237973 && pRet->bContentlessUnindexed
237974 && pRet->eContent!=FTS5_CONTENT_NONE1
237975 ){
237976 *pzErr = sqlite3_mprintf(
237977 "contentless_unindexed=1 requires a contentless table"
237978 );
237979 rc = SQLITE_ERROR1;
237980 }
237981
237982 /* If no zContent option was specified, fill in the default values. */
237983 if( rc==SQLITE_OK0 && pRet->zContent==0 ){
237984 const char *zTail = 0;
237985 assert( pRet->eContent==FTS5_CONTENT_NORMAL((void) (0))
237986 || pRet->eContent==FTS5_CONTENT_NONE((void) (0))
237987 )((void) (0));
237988 if( pRet->eContent==FTS5_CONTENT_NORMAL0 ){
237989 zTail = "content";
237990 }else if( bUnindexed && pRet->bContentlessUnindexed ){
237991 pRet->eContent = FTS5_CONTENT_UNINDEXED3;
237992 zTail = "content";
237993 }else if( pRet->bColumnsize ){
237994 zTail = "docsize";
237995 }
237996
237997 if( zTail ){
237998 pRet->zContent = sqlite3Fts5Mprintf(
237999 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
238000 );
238001 }
238002 }
238003
238004 if( rc==SQLITE_OK0 && pRet->zContentRowid==0 ){
238005 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
238006 }
238007
238008 /* Formulate the zContentExprlist text */
238009 if( rc==SQLITE_OK0 ){
238010 rc = fts5ConfigMakeExprlist(pRet);
238011 }
238012
238013 if( rc!=SQLITE_OK0 ){
238014 sqlite3Fts5ConfigFree(pRet);
238015 *ppOut = 0;
238016 }
238017 return rc;
238018}
238019
238020/*
238021** Free the configuration object passed as the only argument.
238022*/
238023static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
238024 if( pConfig ){
238025 int i;
238026 if( pConfig->t.pTok ){
238027 if( pConfig->t.pApi1 ){
238028 pConfig->t.pApi1->xDelete(pConfig->t.pTok);
238029 }else{
238030 pConfig->t.pApi2->xDelete(pConfig->t.pTok);
238031 }
238032 }
238033 sqlite3_free((char*)pConfig->t.azArg);
238034 sqlite3_free(pConfig->zDb);
238035 sqlite3_free(pConfig->zName);
238036 for(i=0; i<pConfig->nCol; i++){
238037 sqlite3_free(pConfig->azCol[i]);
238038 }
238039 sqlite3_free(pConfig->azCol);
238040 sqlite3_free(pConfig->aPrefix);
238041 sqlite3_free(pConfig->zRank);
238042 sqlite3_free(pConfig->zRankArgs);
238043 sqlite3_free(pConfig->zContent);
238044 sqlite3_free(pConfig->zContentRowid);
238045 sqlite3_free(pConfig->zContentExprlist);
238046 sqlite3_free(pConfig);
238047 }
238048}
238049
238050/*
238051** Call sqlite3_declare_vtab() based on the contents of the configuration
238052** object passed as the only argument. Return SQLITE_OK if successful, or
238053** an SQLite error code if an error occurs.
238054*/
238055static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
238056 int i;
238057 int rc = SQLITE_OK0;
238058 char *zSql;
238059
238060 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
238061 for(i=0; zSql && i<pConfig->nCol; i++){
238062 const char *zSep = (i==0?"":", ");
238063 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
238064 }
238065 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
238066 zSql, pConfig->zName, FTS5_RANK_NAME"rank"
238067 );
238068
238069 assert( zSql || rc==SQLITE_NOMEM )((void) (0));
238070 if( zSql ){
238071 rc = sqlite3_declare_vtab(pConfig->db, zSql);
238072 sqlite3_free(zSql);
238073 }
238074
238075 return rc;
238076}
238077
238078/*
238079** Tokenize the text passed via the second and third arguments.
238080**
238081** The callback is invoked once for each token in the input text. The
238082** arguments passed to it are, in order:
238083**
238084** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
238085** const char *pToken // Pointer to buffer containing token
238086** int nToken // Size of token in bytes
238087** int iStart // Byte offset of start of token within input text
238088** int iEnd // Byte offset of end of token within input text
238089** int iPos // Position of token in input (first token is 0)
238090**
238091** If the callback returns a non-zero value the tokenization is abandoned
238092** and no further callbacks are issued.
238093**
238094** This function returns SQLITE_OK if successful or an SQLite error code
238095** if an error occurs. If the tokenization was abandoned early because
238096** the callback returned SQLITE_DONE, this is not an error and this function
238097** still returns SQLITE_OK. Or, if the tokenization was abandoned early
238098** because the callback returned another non-zero value, it is assumed
238099** to be an SQLite error code and returned to the caller.
238100*/
238101static int sqlite3Fts5Tokenize(
238102 Fts5Config *pConfig, /* FTS5 Configuration object */
238103 int flags, /* FTS5_TOKENIZE_* flags */
238104 const char *pText, int nText, /* Text to tokenize */
238105 void *pCtx, /* Context passed to xToken() */
238106 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
238107){
238108 int rc = SQLITE_OK0;
238109 if( pText ){
238110 if( pConfig->t.pTok==0 ){
238111 rc = sqlite3Fts5LoadTokenizer(pConfig);
238112 }
238113 if( rc==SQLITE_OK0 ){
238114 if( pConfig->t.pApi1 ){
238115 rc = pConfig->t.pApi1->xTokenize(
238116 pConfig->t.pTok, pCtx, flags, pText, nText, xToken
238117 );
238118 }else{
238119 rc = pConfig->t.pApi2->xTokenize(pConfig->t.pTok, pCtx, flags,
238120 pText, nText, pConfig->t.pLocale, pConfig->t.nLocale, xToken
238121 );
238122 }
238123 }
238124 }
238125 return rc;
238126}
238127
238128/*
238129** Argument pIn points to the first character in what is expected to be
238130** a comma-separated list of SQL literals followed by a ')' character.
238131** If it actually is this, return a pointer to the ')'. Otherwise, return
238132** NULL to indicate a parse error.
238133*/
238134static const char *fts5ConfigSkipArgs(const char *pIn){
238135 const char *p = pIn;
238136
238137 while( 1 ){
238138 p = fts5ConfigSkipWhitespace(p);
238139 p = fts5ConfigSkipLiteral(p);
238140 p = fts5ConfigSkipWhitespace(p);
238141 if( p==0 || *p==')' ) break;
238142 if( *p!=',' ){
238143 p = 0;
238144 break;
238145 }
238146 p++;
238147 }
238148
238149 return p;
238150}
238151
238152/*
238153** Parameter zIn contains a rank() function specification. The format of
238154** this is:
238155**
238156** + Bareword (function name)
238157** + Open parenthesis - "("
238158** + Zero or more SQL literals in a comma separated list
238159** + Close parenthesis - ")"
238160*/
238161static int sqlite3Fts5ConfigParseRank(
238162 const char *zIn, /* Input string */
238163 char **pzRank, /* OUT: Rank function name */
238164 char **pzRankArgs /* OUT: Rank function arguments */
238165){
238166 const char *p = zIn;
238167 const char *pRank;
238168 char *zRank = 0;
238169 char *zRankArgs = 0;
238170 int rc = SQLITE_OK0;
238171
238172 *pzRank = 0;
238173 *pzRankArgs = 0;
238174
238175 if( p==0 ){
238176 rc = SQLITE_ERROR1;
238177 }else{
238178 p = fts5ConfigSkipWhitespace(p);
238179 pRank = p;
238180 p = fts5ConfigSkipBareword(p);
238181
238182 if( p ){
238183 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
238184 if( zRank ) memcpy(zRank, pRank, p-pRank);
238185 }else{
238186 rc = SQLITE_ERROR1;
238187 }
238188
238189 if( rc==SQLITE_OK0 ){
238190 p = fts5ConfigSkipWhitespace(p);
238191 if( *p!='(' ) rc = SQLITE_ERROR1;
238192 p++;
238193 }
238194 if( rc==SQLITE_OK0 ){
238195 const char *pArgs;
238196 p = fts5ConfigSkipWhitespace(p);
238197 pArgs = p;
238198 if( *p!=')' ){
238199 p = fts5ConfigSkipArgs(p);
238200 if( p==0 ){
238201 rc = SQLITE_ERROR1;
238202 }else{
238203 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
238204 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
238205 }
238206 }
238207 }
238208 }
238209
238210 if( rc!=SQLITE_OK0 ){
238211 sqlite3_free(zRank);
238212 assert( zRankArgs==0 )((void) (0));
238213 }else{
238214 *pzRank = zRank;
238215 *pzRankArgs = zRankArgs;
238216 }
238217 return rc;
238218}
238219
238220static int sqlite3Fts5ConfigSetValue(
238221 Fts5Config *pConfig,
238222 const char *zKey,
238223 sqlite3_value *pVal,
238224 int *pbBadkey
238225){
238226 int rc = SQLITE_OK0;
238227
238228 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
238229 int pgsz = 0;
238230 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238231 pgsz = sqlite3_value_int(pVal);
238232 }
238233 if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE(64*1024) ){
238234 *pbBadkey = 1;
238235 }else{
238236 pConfig->pgsz = pgsz;
238237 }
238238 }
238239
238240 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
238241 int nHashSize = -1;
238242 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238243 nHashSize = sqlite3_value_int(pVal);
238244 }
238245 if( nHashSize<=0 ){
238246 *pbBadkey = 1;
238247 }else{
238248 pConfig->nHashSize = nHashSize;
238249 }
238250 }
238251
238252 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
238253 int nAutomerge = -1;
238254 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238255 nAutomerge = sqlite3_value_int(pVal);
238256 }
238257 if( nAutomerge<0 || nAutomerge>64 ){
238258 *pbBadkey = 1;
238259 }else{
238260 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE4;
238261 pConfig->nAutomerge = nAutomerge;
238262 }
238263 }
238264
238265 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
238266 int nUsermerge = -1;
238267 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238268 nUsermerge = sqlite3_value_int(pVal);
238269 }
238270 if( nUsermerge<2 || nUsermerge>16 ){
238271 *pbBadkey = 1;
238272 }else{
238273 pConfig->nUsermerge = nUsermerge;
238274 }
238275 }
238276
238277 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
238278 int nCrisisMerge = -1;
238279 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238280 nCrisisMerge = sqlite3_value_int(pVal);
238281 }
238282 if( nCrisisMerge<0 ){
238283 *pbBadkey = 1;
238284 }else{
238285 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE16;
238286 if( nCrisisMerge>=FTS5_MAX_SEGMENT2000 ) nCrisisMerge = FTS5_MAX_SEGMENT2000-1;
238287 pConfig->nCrisisMerge = nCrisisMerge;
238288 }
238289 }
238290
238291 else if( 0==sqlite3_stricmp(zKey, "deletemerge") ){
238292 int nVal = -1;
238293 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238294 nVal = sqlite3_value_int(pVal);
238295 }else{
238296 *pbBadkey = 1;
238297 }
238298 if( nVal<0 ) nVal = FTS5_DEFAULT_DELETE_AUTOMERGE10;
238299 if( nVal>100 ) nVal = 0;
238300 pConfig->nDeleteMerge = nVal;
238301 }
238302
238303 else if( 0==sqlite3_stricmp(zKey, "rank") ){
238304 const char *zIn = (const char*)sqlite3_value_text(pVal);
238305 char *zRank;
238306 char *zRankArgs;
238307 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
238308 if( rc==SQLITE_OK0 ){
238309 sqlite3_free(pConfig->zRank);
238310 sqlite3_free(pConfig->zRankArgs);
238311 pConfig->zRank = zRank;
238312 pConfig->zRankArgs = zRankArgs;
238313 }else if( rc==SQLITE_ERROR1 ){
238314 rc = SQLITE_OK0;
238315 *pbBadkey = 1;
238316 }
238317 }
238318
238319 else if( 0==sqlite3_stricmp(zKey, "secure-delete") ){
238320 int bVal = -1;
238321 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
238322 bVal = sqlite3_value_int(pVal);
238323 }
238324 if( bVal<0 ){
238325 *pbBadkey = 1;
238326 }else{
238327 pConfig->bSecureDelete = (bVal ? 1 : 0);
238328 }
238329 }else{
238330 *pbBadkey = 1;
238331 }
238332 return rc;
238333}
238334
238335/*
238336** Load the contents of the %_config table into memory.
238337*/
238338static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
238339 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
238340 char *zSql;
238341 sqlite3_stmt *p = 0;
238342 int rc = SQLITE_OK0;
238343 int iVersion = 0;
238344
238345 /* Set default values */
238346 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE4050;
238347 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE4;
238348 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE4;
238349 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE16;
238350 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE(1024*1024);
238351 pConfig->nDeleteMerge = FTS5_DEFAULT_DELETE_AUTOMERGE10;
238352
238353 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
238354 if( zSql ){
238355 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
238356 sqlite3_free(zSql);
238357 }
238358
238359 assert( rc==SQLITE_OK || p==0 )((void) (0));
238360 if( rc==SQLITE_OK0 ){
238361 while( SQLITE_ROW100==sqlite3_step(p) ){
238362 const char *zK = (const char*)sqlite3_column_text(p, 0);
238363 sqlite3_value *pVal = sqlite3_column_value(p, 1);
238364 if( 0==sqlite3_stricmp(zK, "version") ){
238365 iVersion = sqlite3_value_int(pVal);
238366 }else{
238367 int bDummy = 0;
238368 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
238369 }
238370 }
238371 rc = sqlite3_finalize(p);
238372 }
238373
238374 if( rc==SQLITE_OK0
238375 && iVersion!=FTS5_CURRENT_VERSION4
238376 && iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE5
238377 ){
238378 rc = SQLITE_ERROR1;
238379 sqlite3Fts5ConfigErrmsg(pConfig, "invalid fts5 file format "
238380 "(found %d, expected %d or %d) - run 'rebuild'",
238381 iVersion, FTS5_CURRENT_VERSION4, FTS5_CURRENT_VERSION_SECUREDELETE5
238382 );
238383 }else{
238384 pConfig->iVersion = iVersion;
238385 }
238386
238387 if( rc==SQLITE_OK0 ){
238388 pConfig->iCookie = iCookie;
238389 }
238390 return rc;
238391}
238392
238393/*
238394** Set (*pConfig->pzErrmsg) to point to an sqlite3_malloc()ed buffer
238395** containing the error message created using printf() style formatting
238396** string zFmt and its trailing arguments.
238397*/
238398static void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...){
238399 va_list ap; /* ... printf arguments */
238400 char *zMsg = 0;
238401
238402 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
238403 zMsg = sqlite3_vmprintf(zFmt, ap);
238404 if( pConfig->pzErrmsg ){
238405 assert( *pConfig->pzErrmsg==0 )((void) (0));
238406 *pConfig->pzErrmsg = zMsg;
238407 }else{
238408 sqlite3_free(zMsg);
238409 }
238410
238411 va_end(ap)__builtin_va_end(ap);
238412}
238413
238414
238415
238416/*
238417** 2014 May 31
238418**
238419** The author disclaims copyright to this source code. In place of
238420** a legal notice, here is a blessing:
238421**
238422** May you do good and not evil.
238423** May you find forgiveness for yourself and forgive others.
238424** May you share freely, never taking more than you give.
238425**
238426******************************************************************************
238427**
238428*/
238429
238430
238431
238432/* #include "fts5Int.h" */
238433/* #include "fts5parse.h" */
238434
238435#ifndef SQLITE_FTS5_MAX_EXPR_DEPTH256
238436# define SQLITE_FTS5_MAX_EXPR_DEPTH256 256
238437#endif
238438
238439/*
238440** All token types in the generated fts5parse.h file are greater than 0.
238441*/
238442#define FTS5_EOF0 0
238443
238444#define FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
238445
238446typedef struct Fts5ExprTerm Fts5ExprTerm;
238447
238448/*
238449** Functions generated by lemon from fts5parse.y.
238450*/
238451static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
238452static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
238453static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
238454#ifndef NDEBUG1
238455/* #include <stdio.h> */
238456static void sqlite3Fts5ParserTrace(FILE*, char*);
238457#endif
238458static int sqlite3Fts5ParserFallback(int);
238459
238460
238461struct Fts5Expr {
238462 Fts5Index *pIndex;
238463 Fts5Config *pConfig;
238464 Fts5ExprNode *pRoot;
238465 int bDesc; /* Iterate in descending rowid order */
238466 int nPhrase; /* Number of phrases in expression */
238467 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
238468};
238469
238470/*
238471** eType:
238472** Expression node type. Usually one of:
238473**
238474** FTS5_AND (nChild, apChild valid)
238475** FTS5_OR (nChild, apChild valid)
238476** FTS5_NOT (nChild, apChild valid)
238477** FTS5_STRING (pNear valid)
238478** FTS5_TERM (pNear valid)
238479**
238480** An expression node with eType==0 may also exist. It always matches zero
238481** rows. This is created when a phrase containing no tokens is parsed.
238482** e.g. "".
238483**
238484** iHeight:
238485** Distance from this node to furthest leaf. This is always 0 for nodes
238486** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one
238487** greater than the largest child value.
238488*/
238489struct Fts5ExprNode {
238490 int eType; /* Node type */
238491 int bEof; /* True at EOF */
238492 int bNomatch; /* True if entry is not a match */
238493 int iHeight; /* Distance to tree leaf nodes */
238494
238495 /* Next method for this node. */
238496 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
238497
238498 i64 iRowid; /* Current rowid */
238499 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
238500
238501 /* Child nodes. For a NOT node, this array always contains 2 entries. For
238502 ** AND or OR nodes, it contains 2 or more entries. */
238503 int nChild; /* Number of child nodes */
238504 Fts5ExprNode *apChild[1]; /* Array of child nodes */
238505};
238506
238507#define Fts5NodeIsString(p)((p)->eType==4 || (p)->eType==9) ((p)->eType==FTS5_TERM4 || (p)->eType==FTS5_STRING9)
238508
238509/*
238510** Invoke the xNext method of an Fts5ExprNode object. This macro should be
238511** used as if it has the same signature as the xNext() methods themselves.
238512*/
238513#define fts5ExprNodeNext(a,b,c,d)(b)->xNext((a), (b), (c), (d)) (b)->xNext((a), (b), (c), (d))
238514
238515/*
238516** An instance of the following structure represents a single search term
238517** or term prefix.
238518*/
238519struct Fts5ExprTerm {
238520 u8 bPrefix; /* True for a prefix term */
238521 u8 bFirst; /* True if token must be first in column */
238522 char *pTerm; /* Term data */
238523 int nQueryTerm; /* Effective size of term in bytes */
238524 int nFullTerm; /* Size of term in bytes incl. tokendata */
238525 Fts5IndexIter *pIter; /* Iterator for this term */
238526 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
238527};
238528
238529/*
238530** A phrase. One or more terms that must appear in a contiguous sequence
238531** within a document for it to match.
238532*/
238533struct Fts5ExprPhrase {
238534 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
238535 Fts5Buffer poslist; /* Current position list */
238536 int nTerm; /* Number of entries in aTerm[] */
238537 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
238538};
238539
238540/*
238541** One or more phrases that must appear within a certain token distance of
238542** each other within each matching document.
238543*/
238544struct Fts5ExprNearset {
238545 int nNear; /* NEAR parameter */
238546 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
238547 int nPhrase; /* Number of entries in aPhrase[] array */
238548 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
238549};
238550
238551
238552/*
238553** Parse context.
238554*/
238555struct Fts5Parse {
238556 Fts5Config *pConfig;
238557 char *zErr;
238558 int rc;
238559 int nPhrase; /* Size of apPhrase array */
238560 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
238561 Fts5ExprNode *pExpr; /* Result of a successful parse */
238562 int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
238563};
238564
238565/*
238566** Check that the Fts5ExprNode.iHeight variables are set correctly in
238567** the expression tree passed as the only argument.
238568*/
238569#ifndef NDEBUG1
238570static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){
238571 if( rc==SQLITE_OK0 ){
238572 if( p->eType==FTS5_TERM4 || p->eType==FTS5_STRING9 || p->eType==0 ){
238573 assert( p->iHeight==0 )((void) (0));
238574 }else{
238575 int ii;
238576 int iMaxChild = 0;
238577 for(ii=0; ii<p->nChild; ii++){
238578 Fts5ExprNode *pChild = p->apChild[ii];
238579 iMaxChild = MAX(iMaxChild, pChild->iHeight)((iMaxChild)>(pChild->iHeight)?(iMaxChild):(pChild->
iHeight))
;
238580 assert_expr_depth_ok(SQLITE_OK, pChild);
238581 }
238582 assert( p->iHeight==iMaxChild+1 )((void) (0));
238583 }
238584 }
238585}
238586#else
238587# define assert_expr_depth_ok(rc, p)
238588#endif
238589
238590static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
238591 va_list ap;
238592 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
238593 if( pParse->rc==SQLITE_OK0 ){
238594 assert( pParse->zErr==0 )((void) (0));
238595 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
238596 pParse->rc = SQLITE_ERROR1;
238597 }
238598 va_end(ap)__builtin_va_end(ap);
238599}
238600
238601static int fts5ExprIsspace(char t){
238602 return t==' ' || t=='\t' || t=='\n' || t=='\r';
238603}
238604
238605/*
238606** Read the first token from the nul-terminated string at *pz.
238607*/
238608static int fts5ExprGetToken(
238609 Fts5Parse *pParse,
238610 const char **pz, /* IN/OUT: Pointer into buffer */
238611 Fts5Token *pToken
238612){
238613 const char *z = *pz;
238614 int tok;
238615
238616 /* Skip past any whitespace */
238617 while( fts5ExprIsspace(*z) ) z++;
238618
238619 pToken->p = z;
238620 pToken->n = 1;
238621 switch( *z ){
238622 case '(': tok = FTS5_LP10; break;
238623 case ')': tok = FTS5_RP11; break;
238624 case '{': tok = FTS5_LCP7; break;
238625 case '}': tok = FTS5_RCP8; break;
238626 case ':': tok = FTS5_COLON5; break;
238627 case ',': tok = FTS5_COMMA13; break;
238628 case '+': tok = FTS5_PLUS14; break;
238629 case '*': tok = FTS5_STAR15; break;
238630 case '-': tok = FTS5_MINUS6; break;
238631 case '^': tok = FTS5_CARET12; break;
238632 case '\0': tok = FTS5_EOF0; break;
238633
238634 case '"': {
238635 const char *z2;
238636 tok = FTS5_STRING9;
238637
238638 for(z2=&z[1]; 1; z2++){
238639 if( z2[0]=='"' ){
238640 z2++;
238641 if( z2[0]!='"' ) break;
238642 }
238643 if( z2[0]=='\0' ){
238644 sqlite3Fts5ParseError(pParse, "unterminated string");
238645 return FTS5_EOF0;
238646 }
238647 }
238648 pToken->n = (z2 - z);
238649 break;
238650 }
238651
238652 default: {
238653 const char *z2;
238654 if( sqlite3Fts5IsBareword(z[0])==0 ){
238655 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
238656 return FTS5_EOF0;
238657 }
238658 tok = FTS5_STRING9;
238659 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
238660 pToken->n = (z2 - z);
238661 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR1;
238662 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT3;
238663 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND2;
238664 break;
238665 }
238666 }
238667
238668 *pz = &pToken->p[pToken->n];
238669 return tok;
238670}
238671
238672static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
238673static void fts5ParseFree(void *p){ sqlite3_free(p); }
238674
238675static int sqlite3Fts5ExprNew(
238676 Fts5Config *pConfig, /* FTS5 Configuration */
238677 int bPhraseToAnd,
238678 int iCol,
238679 const char *zExpr, /* Expression text */
238680 Fts5Expr **ppNew,
238681 char **pzErr
238682){
238683 Fts5Parse sParse;
238684 Fts5Token token;
238685 const char *z = zExpr;
238686 int t; /* Next token type */
238687 void *pEngine;
238688 Fts5Expr *pNew;
238689
238690 *ppNew = 0;
238691 *pzErr = 0;
238692 memset(&sParse, 0, sizeof(sParse));
238693 sParse.bPhraseToAnd = bPhraseToAnd;
238694 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
238695 if( pEngine==0 ){ return SQLITE_NOMEM7; }
238696 sParse.pConfig = pConfig;
238697
238698 do {
238699 t = fts5ExprGetToken(&sParse, &z, &token);
238700 sqlite3Fts5Parser(pEngine, t, token, &sParse);
238701 }while( sParse.rc==SQLITE_OK0 && t!=FTS5_EOF0 );
238702 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
238703
238704 assert( sParse.pExpr || sParse.rc!=SQLITE_OK )((void) (0));
238705 assert_expr_depth_ok(sParse.rc, sParse.pExpr);
238706
238707 /* If the LHS of the MATCH expression was a user column, apply the
238708 ** implicit column-filter. */
238709 if( sParse.rc==SQLITE_OK0 && iCol<pConfig->nCol ){
238710 int n = sizeof(Fts5Colset);
238711 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
238712 if( pColset ){
238713 pColset->nCol = 1;
238714 pColset->aiCol[0] = iCol;
238715 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
238716 }
238717 }
238718
238719 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 )((void) (0));
238720 if( sParse.rc==SQLITE_OK0 ){
238721 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
238722 if( pNew==0 ){
238723 sParse.rc = SQLITE_NOMEM7;
238724 sqlite3Fts5ParseNodeFree(sParse.pExpr);
238725 }else{
238726 pNew->pRoot = sParse.pExpr;
238727 pNew->pIndex = 0;
238728 pNew->pConfig = pConfig;
238729 pNew->apExprPhrase = sParse.apPhrase;
238730 pNew->nPhrase = sParse.nPhrase;
238731 pNew->bDesc = 0;
238732 sParse.apPhrase = 0;
238733 }
238734 }else{
238735 sqlite3Fts5ParseNodeFree(sParse.pExpr);
238736 }
238737
238738 sqlite3_free(sParse.apPhrase);
238739 if( 0==*pzErr ){
238740 *pzErr = sParse.zErr;
238741 }else{
238742 sqlite3_free(sParse.zErr);
238743 }
238744 return sParse.rc;
238745}
238746
238747/*
238748** Assuming that buffer z is at least nByte bytes in size and contains a
238749** valid utf-8 string, return the number of characters in the string.
238750*/
238751static int fts5ExprCountChar(const char *z, int nByte){
238752 int nRet = 0;
238753 int ii;
238754 for(ii=0; ii<nByte; ii++){
238755 if( (z[ii] & 0xC0)!=0x80 ) nRet++;
238756 }
238757 return nRet;
238758}
238759
238760/*
238761** This function is only called when using the special 'trigram' tokenizer.
238762** Argument zText contains the text of a LIKE or GLOB pattern matched
238763** against column iCol. This function creates and compiles an FTS5 MATCH
238764** expression that will match a superset of the rows matched by the LIKE or
238765** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
238766** code.
238767*/
238768static int sqlite3Fts5ExprPattern(
238769 Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
238770){
238771 i64 nText = strlen(zText);
238772 char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1);
238773 int rc = SQLITE_OK0;
238774
238775 if( zExpr==0 ){
238776 rc = SQLITE_NOMEM7;
238777 }else{
238778 char aSpec[3];
238779 int iOut = 0;
238780 int i = 0;
238781 int iFirst = 0;
238782
238783 if( bGlob==0 ){
238784 aSpec[0] = '_';
238785 aSpec[1] = '%';
238786 aSpec[2] = 0;
238787 }else{
238788 aSpec[0] = '*';
238789 aSpec[1] = '?';
238790 aSpec[2] = '[';
238791 }
238792
238793 while( i<=nText ){
238794 if( i==nText
238795 || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
238796 ){
238797
238798 if( fts5ExprCountChar(&zText[iFirst], i-iFirst)>=3 ){
238799 int jj;
238800 zExpr[iOut++] = '"';
238801 for(jj=iFirst; jj<i; jj++){
238802 zExpr[iOut++] = zText[jj];
238803 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
238804 }
238805 zExpr[iOut++] = '"';
238806 zExpr[iOut++] = ' ';
238807 }
238808 if( zText[i]==aSpec[2] ){
238809 i += 2;
238810 if( zText[i-1]=='^' ) i++;
238811 while( i<nText && zText[i]!=']' ) i++;
238812 }
238813 iFirst = i+1;
238814 }
238815 i++;
238816 }
238817 if( iOut>0 ){
238818 int bAnd = 0;
238819 if( pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
238820 bAnd = 1;
238821 if( pConfig->eDetail==FTS5_DETAIL_NONE1 ){
238822 iCol = pConfig->nCol;
238823 }
238824 }
238825 zExpr[iOut] = '\0';
238826 rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg);
238827 }else{
238828 *pp = 0;
238829 }
238830 sqlite3_free(zExpr);
238831 }
238832
238833 return rc;
238834}
238835
238836/*
238837** Free the expression node object passed as the only argument.
238838*/
238839static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
238840 if( p ){
238841 int i;
238842 for(i=0; i<p->nChild; i++){
238843 sqlite3Fts5ParseNodeFree(p->apChild[i]);
238844 }
238845 sqlite3Fts5ParseNearsetFree(p->pNear);
238846 sqlite3_free(p);
238847 }
238848}
238849
238850/*
238851** Free the expression object passed as the only argument.
238852*/
238853static void sqlite3Fts5ExprFree(Fts5Expr *p){
238854 if( p ){
238855 sqlite3Fts5ParseNodeFree(p->pRoot);
238856 sqlite3_free(p->apExprPhrase);
238857 sqlite3_free(p);
238858 }
238859}
238860
238861static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
238862 Fts5Parse sParse;
238863 memset(&sParse, 0, sizeof(sParse));
238864
238865 if( *pp1 && p2 ){
238866 Fts5Expr *p1 = *pp1;
238867 int nPhrase = p1->nPhrase + p2->nPhrase;
238868
238869 p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND2, p1->pRoot, p2->pRoot,0);
238870 p2->pRoot = 0;
238871
238872 if( sParse.rc==SQLITE_OK0 ){
238873 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
238874 p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
238875 );
238876 if( ap==0 ){
238877 sParse.rc = SQLITE_NOMEM7;
238878 }else{
238879 int i;
238880 memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
238881 for(i=0; i<p2->nPhrase; i++){
238882 ap[i] = p2->apExprPhrase[i];
238883 }
238884 p1->nPhrase = nPhrase;
238885 p1->apExprPhrase = ap;
238886 }
238887 }
238888 sqlite3_free(p2->apExprPhrase);
238889 sqlite3_free(p2);
238890 }else if( p2 ){
238891 *pp1 = p2;
238892 }
238893
238894 return sParse.rc;
238895}
238896
238897/*
238898** Argument pTerm must be a synonym iterator. Return the current rowid
238899** that it points to.
238900*/
238901static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
238902 i64 iRet = 0;
238903 int bRetValid = 0;
238904 Fts5ExprTerm *p;
238905
238906 assert( pTerm )((void) (0));
238907 assert( pTerm->pSynonym )((void) (0));
238908 assert( bDesc==0 || bDesc==1 )((void) (0));
238909 for(p=pTerm; p; p=p->pSynonym){
238910 if( 0==sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof) ){
238911 i64 iRowid = p->pIter->iRowid;
238912 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
238913 iRet = iRowid;
238914 bRetValid = 1;
238915 }
238916 }
238917 }
238918
238919 if( pbEof && bRetValid==0 ) *pbEof = 1;
238920 return iRet;
238921}
238922
238923/*
238924** Argument pTerm must be a synonym iterator.
238925*/
238926static int fts5ExprSynonymList(
238927 Fts5ExprTerm *pTerm,
238928 i64 iRowid,
238929 Fts5Buffer *pBuf, /* Use this buffer for space if required */
238930 u8 **pa, int *pn
238931){
238932 Fts5PoslistReader aStatic[4];
238933 Fts5PoslistReader *aIter = aStatic;
238934 int nIter = 0;
238935 int nAlloc = 4;
238936 int rc = SQLITE_OK0;
238937 Fts5ExprTerm *p;
238938
238939 assert( pTerm->pSynonym )((void) (0));
238940 for(p=pTerm; p; p=p->pSynonym){
238941 Fts5IndexIter *pIter = p->pIter;
238942 if( sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 && pIter->iRowid==iRowid ){
238943 if( pIter->nData==0 ) continue;
238944 if( nIter==nAlloc ){
238945 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
238946 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
238947 if( aNew==0 ){
238948 rc = SQLITE_NOMEM7;
238949 goto synonym_poslist_out;
238950 }
238951 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
238952 nAlloc = nAlloc*2;
238953 if( aIter!=aStatic ) sqlite3_free(aIter);
238954 aIter = aNew;
238955 }
238956 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
238957 assert( aIter[nIter].bEof==0 )((void) (0));
238958 nIter++;
238959 }
238960 }
238961
238962 if( nIter==1 ){
238963 *pa = (u8*)aIter[0].a;
238964 *pn = aIter[0].n;
238965 }else{
238966 Fts5PoslistWriter writer = {0};
238967 i64 iPrev = -1;
238968 fts5BufferZero(pBuf)sqlite3Fts5BufferZero(pBuf);
238969 while( 1 ){
238970 int i;
238971 i64 iMin = FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
238972 for(i=0; i<nIter; i++){
238973 if( aIter[i].bEof==0 ){
238974 if( aIter[i].iPos==iPrev ){
238975 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
238976 }
238977 if( aIter[i].iPos<iMin ){
238978 iMin = aIter[i].iPos;
238979 }
238980 }
238981 }
238982 if( iMin==FTS5_LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) || rc!=SQLITE_OK0 ) break;
238983 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
238984 iPrev = iMin;
238985 }
238986 if( rc==SQLITE_OK0 ){
238987 *pa = pBuf->p;
238988 *pn = pBuf->n;
238989 }
238990 }
238991
238992 synonym_poslist_out:
238993 if( aIter!=aStatic ) sqlite3_free(aIter);
238994 return rc;
238995}
238996
238997
238998/*
238999** All individual term iterators in pPhrase are guaranteed to be valid and
239000** pointing to the same rowid when this function is called. This function
239001** checks if the current rowid really is a match, and if so populates
239002** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
239003** is set to true if this is really a match, or false otherwise.
239004**
239005** SQLITE_OK is returned if an error occurs, or an SQLite error code
239006** otherwise. It is not considered an error code if the current rowid is
239007** not a match.
239008*/
239009static int fts5ExprPhraseIsMatch(
239010 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
239011 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
239012 int *pbMatch /* OUT: Set to true if really a match */
239013){
239014 Fts5PoslistWriter writer = {0};
239015 Fts5PoslistReader aStatic[4];
239016 Fts5PoslistReader *aIter = aStatic;
239017 int i;
239018 int rc = SQLITE_OK0;
239019 int bFirst = pPhrase->aTerm[0].bFirst;
239020
239021 fts5BufferZero(&pPhrase->poslist)sqlite3Fts5BufferZero(&pPhrase->poslist);
239022
239023 /* If the aStatic[] array is not large enough, allocate a large array
239024 ** using sqlite3_malloc(). This approach could be improved upon. */
239025 if( pPhrase->nTerm>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
239026 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
239027 aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
239028 if( !aIter ) return SQLITE_NOMEM7;
239029 }
239030 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
239031
239032 /* Initialize a term iterator for each term in the phrase */
239033 for(i=0; i<pPhrase->nTerm; i++){
239034 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
239035 int n = 0;
239036 int bFlag = 0;
239037 u8 *a = 0;
239038 if( pTerm->pSynonym ){
239039 Fts5Buffer buf = {0, 0, 0};
239040 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
239041 if( rc ){
239042 sqlite3_free(a);
239043 goto ismatch_out;
239044 }
239045 if( a==buf.p ) bFlag = 1;
239046 }else{
239047 a = (u8*)pTerm->pIter->pData;
239048 n = pTerm->pIter->nData;
239049 }
239050 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
239051 aIter[i].bFlag = (u8)bFlag;
239052 if( aIter[i].bEof ) goto ismatch_out;
239053 }
239054
239055 while( 1 ){
239056 int bMatch;
239057 i64 iPos = aIter[0].iPos;
239058 do {
239059 bMatch = 1;
239060 for(i=0; i<pPhrase->nTerm; i++){
239061 Fts5PoslistReader *pPos = &aIter[i];
239062 i64 iAdj = iPos + i;
239063 if( pPos->iPos!=iAdj ){
239064 bMatch = 0;
239065 while( pPos->iPos<iAdj ){
239066 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
239067 }
239068 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
239069 }
239070 }
239071 }while( bMatch==0 );
239072
239073 /* Append position iPos to the output */
239074 if( bFirst==0 || FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF)==0 ){
239075 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
239076 if( rc!=SQLITE_OK0 ) goto ismatch_out;
239077 }
239078
239079 for(i=0; i<pPhrase->nTerm; i++){
239080 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
239081 }
239082 }
239083
239084 ismatch_out:
239085 *pbMatch = (pPhrase->poslist.n>0);
239086 for(i=0; i<pPhrase->nTerm; i++){
239087 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
239088 }
239089 if( aIter!=aStatic ) sqlite3_free(aIter);
239090 return rc;
239091}
239092
239093typedef struct Fts5LookaheadReader Fts5LookaheadReader;
239094struct Fts5LookaheadReader {
239095 const u8 *a; /* Buffer containing position list */
239096 int n; /* Size of buffer a[] in bytes */
239097 int i; /* Current offset in position list */
239098 i64 iPos; /* Current position */
239099 i64 iLookahead; /* Next position */
239100};
239101
239102#define FTS5_LOOKAHEAD_EOF(((i64)1) << 62) (((i64)1) << 62)
239103
239104static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
239105 p->iPos = p->iLookahead;
239106 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
239107 p->iLookahead = FTS5_LOOKAHEAD_EOF(((i64)1) << 62);
239108 }
239109 return (p->iPos==FTS5_LOOKAHEAD_EOF(((i64)1) << 62));
239110}
239111
239112static int fts5LookaheadReaderInit(
239113 const u8 *a, int n, /* Buffer to read position list from */
239114 Fts5LookaheadReader *p /* Iterator object to initialize */
239115){
239116 memset(p, 0, sizeof(Fts5LookaheadReader));
239117 p->a = a;
239118 p->n = n;
239119 fts5LookaheadReaderNext(p);
239120 return fts5LookaheadReaderNext(p);
239121}
239122
239123typedef struct Fts5NearTrimmer Fts5NearTrimmer;
239124struct Fts5NearTrimmer {
239125 Fts5LookaheadReader reader; /* Input iterator */
239126 Fts5PoslistWriter writer; /* Writer context */
239127 Fts5Buffer *pOut; /* Output poslist */
239128};
239129
239130/*
239131** The near-set object passed as the first argument contains more than
239132** one phrase. All phrases currently point to the same row. The
239133** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
239134** tests if the current row contains instances of each phrase sufficiently
239135** close together to meet the NEAR constraint. Non-zero is returned if it
239136** does, or zero otherwise.
239137**
239138** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
239139** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
239140** occurs within this function (*pRc) is set accordingly before returning.
239141** The return value is undefined in both these cases.
239142**
239143** If no error occurs and non-zero (a match) is returned, the position-list
239144** of each phrase object is edited to contain only those entries that
239145** meet the constraint before returning.
239146*/
239147static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
239148 Fts5NearTrimmer aStatic[4];
239149 Fts5NearTrimmer *a = aStatic;
239150 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
239151
239152 int i;
239153 int rc = *pRc;
239154 int bMatch;
239155
239156 assert( pNear->nPhrase>1 )((void) (0));
239157
239158 /* If the aStatic[] array is not large enough, allocate a large array
239159 ** using sqlite3_malloc(). This approach could be improved upon. */
239160 if( pNear->nPhrase>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
239161 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
239162 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
239163 }else{
239164 memset(aStatic, 0, sizeof(aStatic));
239165 }
239166 if( rc!=SQLITE_OK0 ){
239167 *pRc = rc;
239168 return 0;
239169 }
239170
239171 /* Initialize a lookahead iterator for each phrase. After passing the
239172 ** buffer and buffer size to the lookaside-reader init function, zero
239173 ** the phrase poslist buffer. The new poslist for the phrase (containing
239174 ** the same entries as the original with some entries removed on account
239175 ** of the NEAR constraint) is written over the original even as it is
239176 ** being read. This is safe as the entries for the new poslist are a
239177 ** subset of the old, so it is not possible for data yet to be read to
239178 ** be overwritten. */
239179 for(i=0; i<pNear->nPhrase; i++){
239180 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
239181 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
239182 pPoslist->n = 0;
239183 a[i].pOut = pPoslist;
239184 }
239185
239186 while( 1 ){
239187 int iAdv;
239188 i64 iMin;
239189 i64 iMax;
239190
239191 /* This block advances the phrase iterators until they point to a set of
239192 ** entries that together comprise a match. */
239193 iMax = a[0].reader.iPos;
239194 do {
239195 bMatch = 1;
239196 for(i=0; i<pNear->nPhrase; i++){
239197 Fts5LookaheadReader *pPos = &a[i].reader;
239198 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
239199 if( pPos->iPos<iMin || pPos->iPos>iMax ){
239200 bMatch = 0;
239201 while( pPos->iPos<iMin ){
239202 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
239203 }
239204 if( pPos->iPos>iMax ) iMax = pPos->iPos;
239205 }
239206 }
239207 }while( bMatch==0 );
239208
239209 /* Add an entry to each output position list */
239210 for(i=0; i<pNear->nPhrase; i++){
239211 i64 iPos = a[i].reader.iPos;
239212 Fts5PoslistWriter *pWriter = &a[i].writer;
239213 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
239214 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
239215 }
239216 }
239217
239218 iAdv = 0;
239219 iMin = a[0].reader.iLookahead;
239220 for(i=0; i<pNear->nPhrase; i++){
239221 if( a[i].reader.iLookahead < iMin ){
239222 iMin = a[i].reader.iLookahead;
239223 iAdv = i;
239224 }
239225 }
239226 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
239227 }
239228
239229 ismatch_out: {
239230 int bRet = a[0].pOut->n>0;
239231 *pRc = rc;
239232 if( a!=aStatic ) sqlite3_free(a);
239233 return bRet;
239234 }
239235}
239236
239237/*
239238** Advance iterator pIter until it points to a value equal to or laster
239239** than the initial value of *piLast. If this means the iterator points
239240** to a value laster than *piLast, update *piLast to the new lastest value.
239241**
239242** If the iterator reaches EOF, set *pbEof to true before returning. If
239243** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
239244** are set, return a non-zero value. Otherwise, return zero.
239245*/
239246static int fts5ExprAdvanceto(
239247 Fts5IndexIter *pIter, /* Iterator to advance */
239248 int bDesc, /* True if iterator is "rowid DESC" */
239249 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
239250 int *pRc, /* OUT: Error code */
239251 int *pbEof /* OUT: Set to true if EOF */
239252){
239253 i64 iLast = *piLast;
239254 i64 iRowid;
239255
239256 iRowid = pIter->iRowid;
239257 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
239258 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
239259 if( rc || sqlite3Fts5IterEof(pIter)((pIter)->bEof) ){
239260 *pRc = rc;
239261 *pbEof = 1;
239262 return 1;
239263 }
239264 iRowid = pIter->iRowid;
239265 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) )((void) (0));
239266 }
239267 *piLast = iRowid;
239268
239269 return 0;
239270}
239271
239272static int fts5ExprSynonymAdvanceto(
239273 Fts5ExprTerm *pTerm, /* Term iterator to advance */
239274 int bDesc, /* True if iterator is "rowid DESC" */
239275 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
239276 int *pRc /* OUT: Error code */
239277){
239278 int rc = SQLITE_OK0;
239279 i64 iLast = *piLast;
239280 Fts5ExprTerm *p;
239281 int bEof = 0;
239282
239283 for(p=pTerm; rc==SQLITE_OK0 && p; p=p->pSynonym){
239284 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
239285 i64 iRowid = p->pIter->iRowid;
239286 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
239287 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
239288 }
239289 }
239290 }
239291
239292 if( rc!=SQLITE_OK0 ){
239293 *pRc = rc;
239294 bEof = 1;
239295 }else{
239296 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
239297 }
239298 return bEof;
239299}
239300
239301
239302static int fts5ExprNearTest(
239303 int *pRc,
239304 Fts5Expr *pExpr, /* Expression that pNear is a part of */
239305 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
239306){
239307 Fts5ExprNearset *pNear = pNode->pNear;
239308 int rc = *pRc;
239309
239310 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
239311 Fts5ExprTerm *pTerm;
239312 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
239313 pPhrase->poslist.n = 0;
239314 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
239315 Fts5IndexIter *pIter = pTerm->pIter;
239316 if( sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 ){
239317 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
239318 pPhrase->poslist.n = 1;
239319 }
239320 }
239321 }
239322 return pPhrase->poslist.n;
239323 }else{
239324 int i;
239325
239326 /* Check that each phrase in the nearset matches the current row.
239327 ** Populate the pPhrase->poslist buffers at the same time. If any
239328 ** phrase is not a match, break out of the loop early. */
239329 for(i=0; rc==SQLITE_OK0 && i<pNear->nPhrase; i++){
239330 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
239331 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
239332 || pNear->pColset || pPhrase->aTerm[0].bFirst
239333 ){
239334 int bMatch = 0;
239335 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
239336 if( bMatch==0 ) break;
239337 }else{
239338 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
239339 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData)sqlite3Fts5BufferSet(&rc,&pPhrase->poslist,pIter->
nData,pIter->pData)
;
239340 }
239341 }
239342
239343 *pRc = rc;
239344 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
239345 return 1;
239346 }
239347 return 0;
239348 }
239349}
239350
239351
239352/*
239353** Initialize all term iterators in the pNear object. If any term is found
239354** to match no documents at all, return immediately without initializing any
239355** further iterators.
239356**
239357** If an error occurs, return an SQLite error code. Otherwise, return
239358** SQLITE_OK. It is not considered an error if some term matches zero
239359** documents.
239360*/
239361static int fts5ExprNearInitAll(
239362 Fts5Expr *pExpr,
239363 Fts5ExprNode *pNode
239364){
239365 Fts5ExprNearset *pNear = pNode->pNear;
239366 int i;
239367
239368 assert( pNode->bNomatch==0 )((void) (0));
239369 for(i=0; i<pNear->nPhrase; i++){
239370 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
239371 if( pPhrase->nTerm==0 ){
239372 pNode->bEof = 1;
239373 return SQLITE_OK0;
239374 }else{
239375 int j;
239376 for(j=0; j<pPhrase->nTerm; j++){
239377 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
239378 Fts5ExprTerm *p;
239379 int bHit = 0;
239380
239381 for(p=pTerm; p; p=p->pSynonym){
239382 int rc;
239383 if( p->pIter ){
239384 sqlite3Fts5IterClose(p->pIter);
239385 p->pIter = 0;
239386 }
239387 rc = sqlite3Fts5IndexQuery(
239388 pExpr->pIndex, p->pTerm, p->nQueryTerm,
239389 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX0x0001 : 0) |
239390 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC0x0002 : 0),
239391 pNear->pColset,
239392 &p->pIter
239393 );
239394 assert( (rc==SQLITE_OK)==(p->pIter!=0) )((void) (0));
239395 if( rc!=SQLITE_OK0 ) return rc;
239396 if( 0==sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof) ){
239397 bHit = 1;
239398 }
239399 }
239400
239401 if( bHit==0 ){
239402 pNode->bEof = 1;
239403 return SQLITE_OK0;
239404 }
239405 }
239406 }
239407 }
239408
239409 pNode->bEof = 0;
239410 return SQLITE_OK0;
239411}
239412
239413/*
239414** If pExpr is an ASC iterator, this function returns a value with the
239415** same sign as:
239416**
239417** (iLhs - iRhs)
239418**
239419** Otherwise, if this is a DESC iterator, the opposite is returned:
239420**
239421** (iRhs - iLhs)
239422*/
239423static int fts5RowidCmp(
239424 Fts5Expr *pExpr,
239425 i64 iLhs,
239426 i64 iRhs
239427){
239428 assert( pExpr->bDesc==0 || pExpr->bDesc==1 )((void) (0));
239429 if( pExpr->bDesc==0 ){
239430 if( iLhs<iRhs ) return -1;
239431 return (iLhs > iRhs);
239432 }else{
239433 if( iLhs>iRhs ) return -1;
239434 return (iLhs < iRhs);
239435 }
239436}
239437
239438static void fts5ExprSetEof(Fts5ExprNode *pNode){
239439 int i;
239440 pNode->bEof = 1;
239441 pNode->bNomatch = 0;
239442 for(i=0; i<pNode->nChild; i++){
239443 fts5ExprSetEof(pNode->apChild[i]);
239444 }
239445}
239446
239447static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
239448 if( pNode->eType==FTS5_STRING9 || pNode->eType==FTS5_TERM4 ){
239449 Fts5ExprNearset *pNear = pNode->pNear;
239450 int i;
239451 for(i=0; i<pNear->nPhrase; i++){
239452 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
239453 pPhrase->poslist.n = 0;
239454 }
239455 }else{
239456 int i;
239457 for(i=0; i<pNode->nChild; i++){
239458 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
239459 }
239460 }
239461}
239462
239463
239464
239465/*
239466** Compare the values currently indicated by the two nodes as follows:
239467**
239468** res = (*p1) - (*p2)
239469**
239470** Nodes that point to values that come later in the iteration order are
239471** considered to be larger. Nodes at EOF are the largest of all.
239472**
239473** This means that if the iteration order is ASC, then numerically larger
239474** rowids are considered larger. Or if it is the default DESC, numerically
239475** smaller rowids are larger.
239476*/
239477static int fts5NodeCompare(
239478 Fts5Expr *pExpr,
239479 Fts5ExprNode *p1,
239480 Fts5ExprNode *p2
239481){
239482 if( p2->bEof ) return -1;
239483 if( p1->bEof ) return +1;
239484 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
239485}
239486
239487/*
239488** All individual term iterators in pNear are guaranteed to be valid when
239489** this function is called. This function checks if all term iterators
239490** point to the same rowid, and if not, advances them until they do.
239491** If an EOF is reached before this happens, *pbEof is set to true before
239492** returning.
239493**
239494** SQLITE_OK is returned if an error occurs, or an SQLite error code
239495** otherwise. It is not considered an error code if an iterator reaches
239496** EOF.
239497*/
239498static int fts5ExprNodeTest_STRING(
239499 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
239500 Fts5ExprNode *pNode
239501){
239502 Fts5ExprNearset *pNear = pNode->pNear;
239503 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
239504 int rc = SQLITE_OK0;
239505 i64 iLast; /* Lastest rowid any iterator points to */
239506 int i, j; /* Phrase and token index, respectively */
239507 int bMatch; /* True if all terms are at the same rowid */
239508 const int bDesc = pExpr->bDesc;
239509
239510 /* Check that this node should not be FTS5_TERM */
239511 assert( pNear->nPhrase>1((void) (0))
239512 || pNear->apPhrase[0]->nTerm>1((void) (0))
239513 || pNear->apPhrase[0]->aTerm[0].pSynonym((void) (0))
239514 || pNear->apPhrase[0]->aTerm[0].bFirst((void) (0))
239515 )((void) (0));
239516
239517 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
239518 ** iterator skips through rowids in the default ascending order, this means
239519 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
239520 ** means the minimum rowid. */
239521 if( pLeft->aTerm[0].pSynonym ){
239522 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
239523 }else{
239524 iLast = pLeft->aTerm[0].pIter->iRowid;
239525 }
239526
239527 do {
239528 bMatch = 1;
239529 for(i=0; i<pNear->nPhrase; i++){
239530 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
239531 for(j=0; j<pPhrase->nTerm; j++){
239532 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
239533 if( pTerm->pSynonym ){
239534 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
239535 if( iRowid==iLast ) continue;
239536 bMatch = 0;
239537 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
239538 pNode->bNomatch = 0;
239539 pNode->bEof = 1;
239540 return rc;
239541 }
239542 }else{
239543 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
239544 if( pIter->iRowid==iLast ) continue;
239545 bMatch = 0;
239546 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
239547 return rc;
239548 }
239549 }
239550 }
239551 }
239552 }while( bMatch==0 );
239553
239554 pNode->iRowid = iLast;
239555 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK0);
239556 assert( pNode->bEof==0 || pNode->bNomatch==0 )((void) (0));
239557
239558 return rc;
239559}
239560
239561/*
239562** Advance the first term iterator in the first phrase of pNear. Set output
239563** variable *pbEof to true if it reaches EOF or if an error occurs.
239564**
239565** Return SQLITE_OK if successful, or an SQLite error code if an error
239566** occurs.
239567*/
239568static int fts5ExprNodeNext_STRING(
239569 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
239570 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
239571 int bFromValid,
239572 i64 iFrom
239573){
239574 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
239575 int rc = SQLITE_OK0;
239576
239577 pNode->bNomatch = 0;
239578 if( pTerm->pSynonym ){
239579 int bEof = 1;
239580 Fts5ExprTerm *p;
239581
239582 /* Find the firstest rowid any synonym points to. */
239583 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
239584
239585 /* Advance each iterator that currently points to iRowid. Or, if iFrom
239586 ** is valid - each iterator that points to a rowid before iFrom. */
239587 for(p=pTerm; p; p=p->pSynonym){
239588 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
239589 i64 ii = p->pIter->iRowid;
239590 if( ii==iRowid
239591 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
239592 ){
239593 if( bFromValid ){
239594 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
239595 }else{
239596 rc = sqlite3Fts5IterNext(p->pIter);
239597 }
239598 if( rc!=SQLITE_OK0 ) break;
239599 if( sqlite3Fts5IterEof(p->pIter)((p->pIter)->bEof)==0 ){
239600 bEof = 0;
239601 }
239602 }else{
239603 bEof = 0;
239604 }
239605 }
239606 }
239607
239608 /* Set the EOF flag if either all synonym iterators are at EOF or an
239609 ** error has occurred. */
239610 pNode->bEof = (rc || bEof);
239611 }else{
239612 Fts5IndexIter *pIter = pTerm->pIter;
239613
239614 assert( Fts5NodeIsString(pNode) )((void) (0));
239615 if( bFromValid ){
239616 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
239617 }else{
239618 rc = sqlite3Fts5IterNext(pIter);
239619 }
239620
239621 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter)((pIter)->bEof));
239622 }
239623
239624 if( pNode->bEof==0 ){
239625 assert( rc==SQLITE_OK )((void) (0));
239626 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
239627 }
239628
239629 return rc;
239630}
239631
239632
239633static int fts5ExprNodeTest_TERM(
239634 Fts5Expr *pExpr, /* Expression that pNear is a part of */
239635 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
239636){
239637 /* As this "NEAR" object is actually a single phrase that consists
239638 ** of a single term only, grab pointers into the poslist managed by the
239639 ** fts5_index.c iterator object. This is much faster than synthesizing
239640 ** a new poslist the way we have to for more complicated phrase or NEAR
239641 ** expressions. */
239642 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
239643 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
239644
239645 assert( pNode->eType==FTS5_TERM )((void) (0));
239646 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 )((void) (0));
239647 assert( pPhrase->aTerm[0].pSynonym==0 )((void) (0));
239648
239649 pPhrase->poslist.n = pIter->nData;
239650 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL0 ){
239651 pPhrase->poslist.p = (u8*)pIter->pData;
239652 }
239653 pNode->iRowid = pIter->iRowid;
239654 pNode->bNomatch = (pPhrase->poslist.n==0);
239655 return SQLITE_OK0;
239656}
239657
239658/*
239659** xNext() method for a node of type FTS5_TERM.
239660*/
239661static int fts5ExprNodeNext_TERM(
239662 Fts5Expr *pExpr,
239663 Fts5ExprNode *pNode,
239664 int bFromValid,
239665 i64 iFrom
239666){
239667 int rc;
239668 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
239669
239670 assert( pNode->bEof==0 )((void) (0));
239671 if( bFromValid ){
239672 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
239673 }else{
239674 rc = sqlite3Fts5IterNext(pIter);
239675 }
239676 if( rc==SQLITE_OK0 && sqlite3Fts5IterEof(pIter)((pIter)->bEof)==0 ){
239677 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
239678 }else{
239679 pNode->bEof = 1;
239680 pNode->bNomatch = 0;
239681 }
239682 return rc;
239683}
239684
239685static void fts5ExprNodeTest_OR(
239686 Fts5Expr *pExpr, /* Expression of which pNode is a part */
239687 Fts5ExprNode *pNode /* Expression node to test */
239688){
239689 Fts5ExprNode *pNext = pNode->apChild[0];
239690 int i;
239691
239692 for(i=1; i<pNode->nChild; i++){
239693 Fts5ExprNode *pChild = pNode->apChild[i];
239694 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
239695 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
239696 pNext = pChild;
239697 }
239698 }
239699 pNode->iRowid = pNext->iRowid;
239700 pNode->bEof = pNext->bEof;
239701 pNode->bNomatch = pNext->bNomatch;
239702}
239703
239704static int fts5ExprNodeNext_OR(
239705 Fts5Expr *pExpr,
239706 Fts5ExprNode *pNode,
239707 int bFromValid,
239708 i64 iFrom
239709){
239710 int i;
239711 i64 iLast = pNode->iRowid;
239712
239713 for(i=0; i<pNode->nChild; i++){
239714 Fts5ExprNode *p1 = pNode->apChild[i];
239715 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 )((void) (0));
239716 if( p1->bEof==0 ){
239717 if( (p1->iRowid==iLast)
239718 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
239719 ){
239720 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom)(p1)->xNext((pExpr), (p1), (bFromValid), (iFrom));
239721 if( rc!=SQLITE_OK0 ){
239722 pNode->bNomatch = 0;
239723 return rc;
239724 }
239725 }
239726 }
239727 }
239728
239729 fts5ExprNodeTest_OR(pExpr, pNode);
239730 return SQLITE_OK0;
239731}
239732
239733/*
239734** Argument pNode is an FTS5_AND node.
239735*/
239736static int fts5ExprNodeTest_AND(
239737 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
239738 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
239739){
239740 int iChild;
239741 i64 iLast = pAnd->iRowid;
239742 int rc = SQLITE_OK0;
239743 int bMatch;
239744
239745 assert( pAnd->bEof==0 )((void) (0));
239746 do {
239747 pAnd->bNomatch = 0;
239748 bMatch = 1;
239749 for(iChild=0; iChild<pAnd->nChild; iChild++){
239750 Fts5ExprNode *pChild = pAnd->apChild[iChild];
239751 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
239752 if( cmp>0 ){
239753 /* Advance pChild until it points to iLast or laster */
239754 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast)(pChild)->xNext((pExpr), (pChild), (1), (iLast));
239755 if( rc!=SQLITE_OK0 ){
239756 pAnd->bNomatch = 0;
239757 return rc;
239758 }
239759 }
239760
239761 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
239762 ** the child node is guaranteed to have advanced at least as far as
239763 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
239764 ** new lastest rowid seen so far. */
239765 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 )((void) (0));
239766 if( pChild->bEof ){
239767 fts5ExprSetEof(pAnd);
239768 bMatch = 1;
239769 break;
239770 }else if( iLast!=pChild->iRowid ){
239771 bMatch = 0;
239772 iLast = pChild->iRowid;
239773 }
239774
239775 if( pChild->bNomatch ){
239776 pAnd->bNomatch = 1;
239777 }
239778 }
239779 }while( bMatch==0 );
239780
239781 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
239782 fts5ExprNodeZeroPoslist(pAnd);
239783 }
239784 pAnd->iRowid = iLast;
239785 return SQLITE_OK0;
239786}
239787
239788static int fts5ExprNodeNext_AND(
239789 Fts5Expr *pExpr,
239790 Fts5ExprNode *pNode,
239791 int bFromValid,
239792 i64 iFrom
239793){
239794 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom)(pNode->apChild[0])->xNext((pExpr), (pNode->apChild[
0]), (bFromValid), (iFrom))
;
239795 if( rc==SQLITE_OK0 ){
239796 rc = fts5ExprNodeTest_AND(pExpr, pNode);
239797 }else{
239798 pNode->bNomatch = 0;
239799 }
239800 return rc;
239801}
239802
239803static int fts5ExprNodeTest_NOT(
239804 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
239805 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
239806){
239807 int rc = SQLITE_OK0;
239808 Fts5ExprNode *p1 = pNode->apChild[0];
239809 Fts5ExprNode *p2 = pNode->apChild[1];
239810 assert( pNode->nChild==2 )((void) (0));
239811
239812 while( rc==SQLITE_OK0 && p1->bEof==0 ){
239813 int cmp = fts5NodeCompare(pExpr, p1, p2);
239814 if( cmp>0 ){
239815 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid)(p2)->xNext((pExpr), (p2), (1), (p1->iRowid));
239816 cmp = fts5NodeCompare(pExpr, p1, p2);
239817 }
239818 assert( rc!=SQLITE_OK || cmp<=0 )((void) (0));
239819 if( cmp || p2->bNomatch ) break;
239820 rc = fts5ExprNodeNext(pExpr, p1, 0, 0)(p1)->xNext((pExpr), (p1), (0), (0));
239821 }
239822 pNode->bEof = p1->bEof;
239823 pNode->bNomatch = p1->bNomatch;
239824 pNode->iRowid = p1->iRowid;
239825 if( p1->bEof ){
239826 fts5ExprNodeZeroPoslist(p2);
239827 }
239828 return rc;
239829}
239830
239831static int fts5ExprNodeNext_NOT(
239832 Fts5Expr *pExpr,
239833 Fts5ExprNode *pNode,
239834 int bFromValid,
239835 i64 iFrom
239836){
239837 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom)(pNode->apChild[0])->xNext((pExpr), (pNode->apChild[
0]), (bFromValid), (iFrom))
;
239838 if( rc==SQLITE_OK0 ){
239839 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
239840 }
239841 if( rc!=SQLITE_OK0 ){
239842 pNode->bNomatch = 0;
239843 }
239844 return rc;
239845}
239846
239847/*
239848** If pNode currently points to a match, this function returns SQLITE_OK
239849** without modifying it. Otherwise, pNode is advanced until it does point
239850** to a match or EOF is reached.
239851*/
239852static int fts5ExprNodeTest(
239853 Fts5Expr *pExpr, /* Expression of which pNode is a part */
239854 Fts5ExprNode *pNode /* Expression node to test */
239855){
239856 int rc = SQLITE_OK0;
239857 if( pNode->bEof==0 ){
239858 switch( pNode->eType ){
239859
239860 case FTS5_STRING9: {
239861 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
239862 break;
239863 }
239864
239865 case FTS5_TERM4: {
239866 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
239867 break;
239868 }
239869
239870 case FTS5_AND2: {
239871 rc = fts5ExprNodeTest_AND(pExpr, pNode);
239872 break;
239873 }
239874
239875 case FTS5_OR1: {
239876 fts5ExprNodeTest_OR(pExpr, pNode);
239877 break;
239878 }
239879
239880 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
239881 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
239882 break;
239883 }
239884 }
239885 }
239886 return rc;
239887}
239888
239889
239890/*
239891** Set node pNode, which is part of expression pExpr, to point to the first
239892** match. If there are no matches, set the Node.bEof flag to indicate EOF.
239893**
239894** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
239895** It is not an error if there are no matches.
239896*/
239897static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
239898 int rc = SQLITE_OK0;
239899 pNode->bEof = 0;
239900 pNode->bNomatch = 0;
239901
239902 if( Fts5NodeIsString(pNode)((pNode)->eType==4 || (pNode)->eType==9) ){
239903 /* Initialize all term iterators in the NEAR object. */
239904 rc = fts5ExprNearInitAll(pExpr, pNode);
239905 }else if( pNode->xNext==0 ){
239906 pNode->bEof = 1;
239907 }else{
239908 int i;
239909 int nEof = 0;
239910 for(i=0; i<pNode->nChild && rc==SQLITE_OK0; i++){
239911 Fts5ExprNode *pChild = pNode->apChild[i];
239912 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
239913 assert( pChild->bEof==0 || pChild->bEof==1 )((void) (0));
239914 nEof += pChild->bEof;
239915 }
239916 pNode->iRowid = pNode->apChild[0]->iRowid;
239917
239918 switch( pNode->eType ){
239919 case FTS5_AND2:
239920 if( nEof>0 ) fts5ExprSetEof(pNode);
239921 break;
239922
239923 case FTS5_OR1:
239924 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
239925 break;
239926
239927 default:
239928 assert( pNode->eType==FTS5_NOT )((void) (0));
239929 pNode->bEof = pNode->apChild[0]->bEof;
239930 break;
239931 }
239932 }
239933
239934 if( rc==SQLITE_OK0 ){
239935 rc = fts5ExprNodeTest(pExpr, pNode);
239936 }
239937 return rc;
239938}
239939
239940
239941/*
239942** Begin iterating through the set of documents in index pIdx matched by
239943** the MATCH expression passed as the first argument. If the "bDesc"
239944** parameter is passed a non-zero value, iteration is in descending rowid
239945** order. Or, if it is zero, in ascending order.
239946**
239947** If iterating in ascending rowid order (bDesc==0), the first document
239948** visited is that with the smallest rowid that is larger than or equal
239949** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
239950** then the first document visited must have a rowid smaller than or
239951** equal to iFirst.
239952**
239953** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
239954** is not considered an error if the query does not match any documents.
239955*/
239956static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
239957 Fts5ExprNode *pRoot = p->pRoot;
239958 int rc; /* Return code */
239959
239960 p->pIndex = pIdx;
239961 p->bDesc = bDesc;
239962 rc = fts5ExprNodeFirst(p, pRoot);
239963
239964 /* If not at EOF but the current rowid occurs earlier than iFirst in
239965 ** the iteration order, move to document iFirst or later. */
239966 if( rc==SQLITE_OK0
239967 && 0==pRoot->bEof
239968 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
239969 ){
239970 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst)(pRoot)->xNext((p), (pRoot), (1), (iFirst));
239971 }
239972
239973 /* If the iterator is not at a real match, skip forward until it is. */
239974 while( pRoot->bNomatch && rc==SQLITE_OK0 ){
239975 assert( pRoot->bEof==0 )((void) (0));
239976 rc = fts5ExprNodeNext(p, pRoot, 0, 0)(pRoot)->xNext((p), (pRoot), (0), (0));
239977 }
239978 return rc;
239979}
239980
239981/*
239982** Move to the next document
239983**
239984** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
239985** is not considered an error if the query does not match any documents.
239986*/
239987static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
239988 int rc;
239989 Fts5ExprNode *pRoot = p->pRoot;
239990 assert( pRoot->bEof==0 && pRoot->bNomatch==0 )((void) (0));
239991 do {
239992 rc = fts5ExprNodeNext(p, pRoot, 0, 0)(pRoot)->xNext((p), (pRoot), (0), (0));
239993 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) )((void) (0));
239994 }while( pRoot->bNomatch );
239995 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
239996 pRoot->bEof = 1;
239997 }
239998 return rc;
239999}
240000
240001static int sqlite3Fts5ExprEof(Fts5Expr *p){
240002 return p->pRoot->bEof;
240003}
240004
240005static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
240006 return p->pRoot->iRowid;
240007}
240008
240009static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
240010 int rc = SQLITE_OK0;
240011 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
240012 return rc;
240013}
240014
240015/*
240016** Free the phrase object passed as the only argument.
240017*/
240018static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
240019 if( pPhrase ){
240020 int i;
240021 for(i=0; i<pPhrase->nTerm; i++){
240022 Fts5ExprTerm *pSyn;
240023 Fts5ExprTerm *pNext;
240024 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
240025 sqlite3_free(pTerm->pTerm);
240026 sqlite3Fts5IterClose(pTerm->pIter);
240027 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
240028 pNext = pSyn->pSynonym;
240029 sqlite3Fts5IterClose(pSyn->pIter);
240030 fts5BufferFree((Fts5Buffer*)&pSyn[1])sqlite3Fts5BufferFree((Fts5Buffer*)&pSyn[1]);
240031 sqlite3_free(pSyn);
240032 }
240033 }
240034 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist)sqlite3Fts5BufferFree(&pPhrase->poslist);
240035 sqlite3_free(pPhrase);
240036 }
240037}
240038
240039/*
240040** Set the "bFirst" flag on the first token of the phrase passed as the
240041** only argument.
240042*/
240043static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
240044 if( pPhrase && pPhrase->nTerm ){
240045 pPhrase->aTerm[0].bFirst = 1;
240046 }
240047}
240048
240049/*
240050** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
240051** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
240052** appended to it and the results returned.
240053**
240054** If an OOM error occurs, both the pNear and pPhrase objects are freed and
240055** NULL returned.
240056*/
240057static Fts5ExprNearset *sqlite3Fts5ParseNearset(
240058 Fts5Parse *pParse, /* Parse context */
240059 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
240060 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
240061){
240062 const int SZALLOC = 8;
240063 Fts5ExprNearset *pRet = 0;
240064
240065 if( pParse->rc==SQLITE_OK0 ){
240066 if( pNear==0 ){
240067 sqlite3_int64 nByte;
240068 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
240069 pRet = sqlite3_malloc64(nByte);
240070 if( pRet==0 ){
240071 pParse->rc = SQLITE_NOMEM7;
240072 }else{
240073 memset(pRet, 0, (size_t)nByte);
240074 }
240075 }else if( (pNear->nPhrase % SZALLOC)==0 ){
240076 int nNew = pNear->nPhrase + SZALLOC;
240077 sqlite3_int64 nByte;
240078
240079 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
240080 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
240081 if( pRet==0 ){
240082 pParse->rc = SQLITE_NOMEM7;
240083 }
240084 }else{
240085 pRet = pNear;
240086 }
240087 }
240088
240089 if( pRet==0 ){
240090 assert( pParse->rc!=SQLITE_OK )((void) (0));
240091 sqlite3Fts5ParseNearsetFree(pNear);
240092 sqlite3Fts5ParsePhraseFree(pPhrase);
240093 }else{
240094 if( pRet->nPhrase>0 ){
240095 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
240096 assert( pParse!=0 )((void) (0));
240097 assert( pParse->apPhrase!=0 )((void) (0));
240098 assert( pParse->nPhrase>=2 )((void) (0));
240099 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] )((void) (0));
240100 if( pPhrase->nTerm==0 ){
240101 fts5ExprPhraseFree(pPhrase);
240102 pRet->nPhrase--;
240103 pParse->nPhrase--;
240104 pPhrase = pLast;
240105 }else if( pLast->nTerm==0 ){
240106 fts5ExprPhraseFree(pLast);
240107 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
240108 pParse->nPhrase--;
240109 pRet->nPhrase--;
240110 }
240111 }
240112 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
240113 }
240114 return pRet;
240115}
240116
240117typedef struct TokenCtx TokenCtx;
240118struct TokenCtx {
240119 Fts5ExprPhrase *pPhrase;
240120 Fts5Config *pConfig;
240121 int rc;
240122};
240123
240124/*
240125** Callback for tokenizing terms used by ParseTerm().
240126*/
240127static int fts5ParseTokenize(
240128 void *pContext, /* Pointer to Fts5InsertCtx object */
240129 int tflags, /* Mask of FTS5_TOKEN_* flags */
240130 const char *pToken, /* Buffer containing token */
240131 int nToken, /* Size of token in bytes */
240132 int iUnused1, /* Start offset of token */
240133 int iUnused2 /* End offset of token */
240134){
240135 int rc = SQLITE_OK0;
240136 const int SZALLOC = 8;
240137 TokenCtx *pCtx = (TokenCtx*)pContext;
240138 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
240139
240140 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
240141
240142 /* If an error has already occurred, this is a no-op */
240143 if( pCtx->rc!=SQLITE_OK0 ) return pCtx->rc;
240144 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
240145
240146 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED0x0001) ){
240147 Fts5ExprTerm *pSyn;
240148 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
240149 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
240150 if( pSyn==0 ){
240151 rc = SQLITE_NOMEM7;
240152 }else{
240153 memset(pSyn, 0, (size_t)nByte);
240154 pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
240155 pSyn->nFullTerm = pSyn->nQueryTerm = nToken;
240156 if( pCtx->pConfig->bTokendata ){
240157 pSyn->nQueryTerm = (int)strlen(pSyn->pTerm);
240158 }
240159 memcpy(pSyn->pTerm, pToken, nToken);
240160 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
240161 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
240162 }
240163 }else{
240164 Fts5ExprTerm *pTerm;
240165 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
240166 Fts5ExprPhrase *pNew;
240167 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
240168
240169 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
240170 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
240171 );
240172 if( pNew==0 ){
240173 rc = SQLITE_NOMEM7;
240174 }else{
240175 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
240176 pCtx->pPhrase = pPhrase = pNew;
240177 pNew->nTerm = nNew - SZALLOC;
240178 }
240179 }
240180
240181 if( rc==SQLITE_OK0 ){
240182 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
240183 memset(pTerm, 0, sizeof(Fts5ExprTerm));
240184 pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
240185 pTerm->nFullTerm = pTerm->nQueryTerm = nToken;
240186 if( pCtx->pConfig->bTokendata && rc==SQLITE_OK0 ){
240187 pTerm->nQueryTerm = (int)strlen(pTerm->pTerm);
240188 }
240189 }
240190 }
240191
240192 pCtx->rc = rc;
240193 return rc;
240194}
240195
240196
240197/*
240198** Free the phrase object passed as the only argument.
240199*/
240200static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
240201 fts5ExprPhraseFree(pPhrase);
240202}
240203
240204/*
240205** Free the phrase object passed as the second argument.
240206*/
240207static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
240208 if( pNear ){
240209 int i;
240210 for(i=0; i<pNear->nPhrase; i++){
240211 fts5ExprPhraseFree(pNear->apPhrase[i]);
240212 }
240213 sqlite3_free(pNear->pColset);
240214 sqlite3_free(pNear);
240215 }
240216}
240217
240218static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
240219 assert( pParse->pExpr==0 )((void) (0));
240220 pParse->pExpr = p;
240221}
240222
240223static int parseGrowPhraseArray(Fts5Parse *pParse){
240224 if( (pParse->nPhrase % 8)==0 ){
240225 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
240226 Fts5ExprPhrase **apNew;
240227 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
240228 if( apNew==0 ){
240229 pParse->rc = SQLITE_NOMEM7;
240230 return SQLITE_NOMEM7;
240231 }
240232 pParse->apPhrase = apNew;
240233 }
240234 return SQLITE_OK0;
240235}
240236
240237/*
240238** This function is called by the parser to process a string token. The
240239** string may or may not be quoted. In any case it is tokenized and a
240240** phrase object consisting of all tokens returned.
240241*/
240242static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
240243 Fts5Parse *pParse, /* Parse context */
240244 Fts5ExprPhrase *pAppend, /* Phrase to append to */
240245 Fts5Token *pToken, /* String to tokenize */
240246 int bPrefix /* True if there is a trailing "*" */
240247){
240248 Fts5Config *pConfig = pParse->pConfig;
240249 TokenCtx sCtx; /* Context object passed to callback */
240250 int rc; /* Tokenize return code */
240251 char *z = 0;
240252
240253 memset(&sCtx, 0, sizeof(TokenCtx));
240254 sCtx.pPhrase = pAppend;
240255 sCtx.pConfig = pConfig;
240256
240257 rc = fts5ParseStringFromToken(pToken, &z);
240258 if( rc==SQLITE_OK0 ){
240259 int flags = FTS5_TOKENIZE_QUERY0x0001 | (bPrefix ? FTS5_TOKENIZE_PREFIX0x0002 : 0);
240260 int n;
240261 sqlite3Fts5Dequote(z);
240262 n = (int)strlen(z);
240263 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
240264 }
240265 sqlite3_free(z);
240266 if( rc || (rc = sCtx.rc) ){
240267 pParse->rc = rc;
240268 fts5ExprPhraseFree(sCtx.pPhrase);
240269 sCtx.pPhrase = 0;
240270 }else{
240271
240272 if( pAppend==0 ){
240273 if( parseGrowPhraseArray(pParse) ){
240274 fts5ExprPhraseFree(sCtx.pPhrase);
240275 return 0;
240276 }
240277 pParse->nPhrase++;
240278 }
240279
240280 if( sCtx.pPhrase==0 ){
240281 /* This happens when parsing a token or quoted phrase that contains
240282 ** no token characters at all. (e.g ... MATCH '""'). */
240283 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
240284 }else if( sCtx.pPhrase->nTerm ){
240285 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
240286 }
240287 assert( pParse->apPhrase!=0 )((void) (0));
240288 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
240289 }
240290
240291 return sCtx.pPhrase;
240292}
240293
240294/*
240295** Create a new FTS5 expression by cloning phrase iPhrase of the
240296** expression passed as the second argument.
240297*/
240298static int sqlite3Fts5ExprClonePhrase(
240299 Fts5Expr *pExpr,
240300 int iPhrase,
240301 Fts5Expr **ppNew
240302){
240303 int rc = SQLITE_OK0; /* Return code */
240304 Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */
240305 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
240306 TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
240307 if( !pExpr || iPhrase<0 || iPhrase>=pExpr->nPhrase ){
240308 rc = SQLITE_RANGE25;
240309 }else{
240310 pOrig = pExpr->apExprPhrase[iPhrase];
240311 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
240312 }
240313 if( rc==SQLITE_OK0 ){
240314 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
240315 sizeof(Fts5ExprPhrase*));
240316 }
240317 if( rc==SQLITE_OK0 ){
240318 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
240319 sizeof(Fts5ExprNode));
240320 }
240321 if( rc==SQLITE_OK0 ){
240322 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
240323 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
240324 }
240325 if( rc==SQLITE_OK0 && ALWAYS(pOrig!=0)(pOrig!=0) ){
240326 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
240327 if( pColsetOrig ){
240328 sqlite3_int64 nByte;
240329 Fts5Colset *pColset;
240330 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
240331 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
240332 if( pColset ){
240333 memcpy(pColset, pColsetOrig, (size_t)nByte);
240334 }
240335 pNew->pRoot->pNear->pColset = pColset;
240336 }
240337 }
240338
240339 if( rc==SQLITE_OK0 ){
240340 if( pOrig->nTerm ){
240341 int i; /* Used to iterate through phrase terms */
240342 sCtx.pConfig = pExpr->pConfig;
240343 for(i=0; rc==SQLITE_OK0 && i<pOrig->nTerm; i++){
240344 int tflags = 0;
240345 Fts5ExprTerm *p;
240346 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK0; p=p->pSynonym){
240347 rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0);
240348 tflags = FTS5_TOKEN_COLOCATED0x0001;
240349 }
240350 if( rc==SQLITE_OK0 ){
240351 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
240352 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
240353 }
240354 }
240355 }else{
240356 /* This happens when parsing a token or quoted phrase that contains
240357 ** no token characters at all. (e.g ... MATCH '""'). */
240358 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
240359 }
240360 }
240361
240362 if( rc==SQLITE_OK0 && ALWAYS(sCtx.pPhrase)(sCtx.pPhrase) ){
240363 /* All the allocations succeeded. Put the expression object together. */
240364 pNew->pIndex = pExpr->pIndex;
240365 pNew->pConfig = pExpr->pConfig;
240366 pNew->nPhrase = 1;
240367 pNew->apExprPhrase[0] = sCtx.pPhrase;
240368 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
240369 pNew->pRoot->pNear->nPhrase = 1;
240370 sCtx.pPhrase->pNode = pNew->pRoot;
240371
240372 if( pOrig->nTerm==1
240373 && pOrig->aTerm[0].pSynonym==0
240374 && pOrig->aTerm[0].bFirst==0
240375 ){
240376 pNew->pRoot->eType = FTS5_TERM4;
240377 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
240378 }else{
240379 pNew->pRoot->eType = FTS5_STRING9;
240380 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
240381 }
240382 }else{
240383 sqlite3Fts5ExprFree(pNew);
240384 fts5ExprPhraseFree(sCtx.pPhrase);
240385 pNew = 0;
240386 }
240387
240388 *ppNew = pNew;
240389 return rc;
240390}
240391
240392
240393/*
240394** Token pTok has appeared in a MATCH expression where the NEAR operator
240395** is expected. If token pTok does not contain "NEAR", store an error
240396** in the pParse object.
240397*/
240398static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
240399 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
240400 sqlite3Fts5ParseError(
240401 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
240402 );
240403 }
240404}
240405
240406static void sqlite3Fts5ParseSetDistance(
240407 Fts5Parse *pParse,
240408 Fts5ExprNearset *pNear,
240409 Fts5Token *p
240410){
240411 if( pNear ){
240412 int nNear = 0;
240413 int i;
240414 if( p->n ){
240415 for(i=0; i<p->n; i++){
240416 char c = (char)p->p[i];
240417 if( c<'0' || c>'9' ){
240418 sqlite3Fts5ParseError(
240419 pParse, "expected integer, got \"%.*s\"", p->n, p->p
240420 );
240421 return;
240422 }
240423 nNear = nNear * 10 + (p->p[i] - '0');
240424 }
240425 }else{
240426 nNear = FTS5_DEFAULT_NEARDIST10;
240427 }
240428 pNear->nNear = nNear;
240429 }
240430}
240431
240432/*
240433** The second argument passed to this function may be NULL, or it may be
240434** an existing Fts5Colset object. This function returns a pointer to
240435** a new colset object containing the contents of (p) with new value column
240436** number iCol appended.
240437**
240438** If an OOM error occurs, store an error code in pParse and return NULL.
240439** The old colset object (if any) is not freed in this case.
240440*/
240441static Fts5Colset *fts5ParseColset(
240442 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
240443 Fts5Colset *p, /* Existing colset object */
240444 int iCol /* New column to add to colset object */
240445){
240446 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
240447 Fts5Colset *pNew; /* New colset object to return */
240448
240449 assert( pParse->rc==SQLITE_OK )((void) (0));
240450 assert( iCol>=0 && iCol<pParse->pConfig->nCol )((void) (0));
240451
240452 pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
240453 if( pNew==0 ){
240454 pParse->rc = SQLITE_NOMEM7;
240455 }else{
240456 int *aiCol = pNew->aiCol;
240457 int i, j;
240458 for(i=0; i<nCol; i++){
240459 if( aiCol[i]==iCol ) return pNew;
240460 if( aiCol[i]>iCol ) break;
240461 }
240462 for(j=nCol; j>i; j--){
240463 aiCol[j] = aiCol[j-1];
240464 }
240465 aiCol[i] = iCol;
240466 pNew->nCol = nCol+1;
240467
240468#ifndef NDEBUG1
240469 /* Check that the array is in order and contains no duplicate entries. */
240470 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] )((void) (0));
240471#endif
240472 }
240473
240474 return pNew;
240475}
240476
240477/*
240478** Allocate and return an Fts5Colset object specifying the inverse of
240479** the colset passed as the second argument. Free the colset passed
240480** as the second argument before returning.
240481*/
240482static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
240483 Fts5Colset *pRet;
240484 int nCol = pParse->pConfig->nCol;
240485
240486 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
240487 sizeof(Fts5Colset) + sizeof(int)*nCol
240488 );
240489 if( pRet ){
240490 int i;
240491 int iOld = 0;
240492 for(i=0; i<nCol; i++){
240493 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
240494 pRet->aiCol[pRet->nCol++] = i;
240495 }else{
240496 iOld++;
240497 }
240498 }
240499 }
240500
240501 sqlite3_free(p);
240502 return pRet;
240503}
240504
240505static Fts5Colset *sqlite3Fts5ParseColset(
240506 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
240507 Fts5Colset *pColset, /* Existing colset object */
240508 Fts5Token *p
240509){
240510 Fts5Colset *pRet = 0;
240511 int iCol;
240512 char *z; /* Dequoted copy of token p */
240513
240514 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
240515 if( pParse->rc==SQLITE_OK0 ){
240516 Fts5Config *pConfig = pParse->pConfig;
240517 sqlite3Fts5Dequote(z);
240518 for(iCol=0; iCol<pConfig->nCol; iCol++){
240519 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
240520 }
240521 if( iCol==pConfig->nCol ){
240522 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
240523 }else{
240524 pRet = fts5ParseColset(pParse, pColset, iCol);
240525 }
240526 sqlite3_free(z);
240527 }
240528
240529 if( pRet==0 ){
240530 assert( pParse->rc!=SQLITE_OK )((void) (0));
240531 sqlite3_free(pColset);
240532 }
240533
240534 return pRet;
240535}
240536
240537/*
240538** If argument pOrig is NULL, or if (*pRc) is set to anything other than
240539** SQLITE_OK when this function is called, NULL is returned.
240540**
240541** Otherwise, a copy of (*pOrig) is made into memory obtained from
240542** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
240543** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
240544*/
240545static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
240546 Fts5Colset *pRet;
240547 if( pOrig ){
240548 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
240549 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
240550 if( pRet ){
240551 memcpy(pRet, pOrig, (size_t)nByte);
240552 }
240553 }else{
240554 pRet = 0;
240555 }
240556 return pRet;
240557}
240558
240559/*
240560** Remove from colset pColset any columns that are not also in colset pMerge.
240561*/
240562static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
240563 int iIn = 0; /* Next input in pColset */
240564 int iMerge = 0; /* Next input in pMerge */
240565 int iOut = 0; /* Next output slot in pColset */
240566
240567 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
240568 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
240569 if( iDiff==0 ){
240570 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
240571 iMerge++;
240572 iIn++;
240573 }else if( iDiff>0 ){
240574 iMerge++;
240575 }else{
240576 iIn++;
240577 }
240578 }
240579 pColset->nCol = iOut;
240580}
240581
240582/*
240583** Recursively apply colset pColset to expression node pNode and all of
240584** its decendents. If (*ppFree) is not NULL, it contains a spare copy
240585** of pColset. This function may use the spare copy and set (*ppFree) to
240586** zero, or it may create copies of pColset using fts5CloneColset().
240587*/
240588static void fts5ParseSetColset(
240589 Fts5Parse *pParse,
240590 Fts5ExprNode *pNode,
240591 Fts5Colset *pColset,
240592 Fts5Colset **ppFree
240593){
240594 if( pParse->rc==SQLITE_OK0 ){
240595 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING((void) (0))
240596 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR((void) (0))
240597 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF((void) (0))
240598 )((void) (0));
240599 if( pNode->eType==FTS5_STRING9 || pNode->eType==FTS5_TERM4 ){
240600 Fts5ExprNearset *pNear = pNode->pNear;
240601 if( pNear->pColset ){
240602 fts5MergeColset(pNear->pColset, pColset);
240603 if( pNear->pColset->nCol==0 ){
240604 pNode->eType = FTS5_EOF0;
240605 pNode->xNext = 0;
240606 }
240607 }else if( *ppFree ){
240608 pNear->pColset = pColset;
240609 *ppFree = 0;
240610 }else{
240611 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
240612 }
240613 }else{
240614 int i;
240615 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 )((void) (0));
240616 for(i=0; i<pNode->nChild; i++){
240617 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
240618 }
240619 }
240620 }
240621}
240622
240623/*
240624** Apply colset pColset to expression node pExpr and all of its descendents.
240625*/
240626static void sqlite3Fts5ParseSetColset(
240627 Fts5Parse *pParse,
240628 Fts5ExprNode *pExpr,
240629 Fts5Colset *pColset
240630){
240631 Fts5Colset *pFree = pColset;
240632 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
240633 sqlite3Fts5ParseError(pParse,
240634 "fts5: column queries are not supported (detail=none)"
240635 );
240636 }else{
240637 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
240638 }
240639 sqlite3_free(pFree);
240640}
240641
240642static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
240643 switch( pNode->eType ){
240644 case FTS5_STRING9: {
240645 Fts5ExprNearset *pNear = pNode->pNear;
240646 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
240647 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
240648 && pNear->apPhrase[0]->aTerm[0].bFirst==0
240649 ){
240650 pNode->eType = FTS5_TERM4;
240651 pNode->xNext = fts5ExprNodeNext_TERM;
240652 }else{
240653 pNode->xNext = fts5ExprNodeNext_STRING;
240654 }
240655 break;
240656 };
240657
240658 case FTS5_OR1: {
240659 pNode->xNext = fts5ExprNodeNext_OR;
240660 break;
240661 };
240662
240663 case FTS5_AND2: {
240664 pNode->xNext = fts5ExprNodeNext_AND;
240665 break;
240666 };
240667
240668 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
240669 pNode->xNext = fts5ExprNodeNext_NOT;
240670 break;
240671 };
240672 }
240673}
240674
240675/*
240676** Add pSub as a child of p.
240677*/
240678static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
240679 int ii = p->nChild;
240680 if( p->eType!=FTS5_NOT3 && pSub->eType==p->eType ){
240681 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
240682 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
240683 p->nChild += pSub->nChild;
240684 sqlite3_free(pSub);
240685 }else{
240686 p->apChild[p->nChild++] = pSub;
240687 }
240688 for( ; ii<p->nChild; ii++){
240689 p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1)((p->iHeight)>(p->apChild[ii]->iHeight + 1)?(p->
iHeight):(p->apChild[ii]->iHeight + 1))
;
240690 }
240691}
240692
240693/*
240694** This function is used when parsing LIKE or GLOB patterns against
240695** trigram indexes that specify either detail=column or detail=none.
240696** It converts a phrase:
240697**
240698** abc + def + ghi
240699**
240700** into an AND tree:
240701**
240702** abc AND def AND ghi
240703*/
240704static Fts5ExprNode *fts5ParsePhraseToAnd(
240705 Fts5Parse *pParse,
240706 Fts5ExprNearset *pNear
240707){
240708 int nTerm = pNear->apPhrase[0]->nTerm;
240709 int ii;
240710 int nByte;
240711 Fts5ExprNode *pRet;
240712
240713 assert( pNear->nPhrase==1 )((void) (0));
240714 assert( pParse->bPhraseToAnd )((void) (0));
240715
240716 nByte = sizeof(Fts5ExprNode) + nTerm*sizeof(Fts5ExprNode*);
240717 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
240718 if( pRet ){
240719 pRet->eType = FTS5_AND2;
240720 pRet->nChild = nTerm;
240721 pRet->iHeight = 1;
240722 fts5ExprAssignXNext(pRet);
240723 pParse->nPhrase--;
240724 for(ii=0; ii<nTerm; ii++){
240725 Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
240726 &pParse->rc, sizeof(Fts5ExprPhrase)
240727 );
240728 if( pPhrase ){
240729 if( parseGrowPhraseArray(pParse) ){
240730 fts5ExprPhraseFree(pPhrase);
240731 }else{
240732 Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii];
240733 Fts5ExprTerm *pTo = &pPhrase->aTerm[0];
240734 pParse->apPhrase[pParse->nPhrase++] = pPhrase;
240735 pPhrase->nTerm = 1;
240736 pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm);
240737 pTo->nQueryTerm = p->nQueryTerm;
240738 pTo->nFullTerm = p->nFullTerm;
240739 pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING9,
240740 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase)
240741 );
240742 }
240743 }
240744 }
240745
240746 if( pParse->rc ){
240747 sqlite3Fts5ParseNodeFree(pRet);
240748 pRet = 0;
240749 }else{
240750 sqlite3Fts5ParseNearsetFree(pNear);
240751 }
240752 }
240753
240754 return pRet;
240755}
240756
240757/*
240758** Allocate and return a new expression object. If anything goes wrong (i.e.
240759** OOM error), leave an error code in pParse and return NULL.
240760*/
240761static Fts5ExprNode *sqlite3Fts5ParseNode(
240762 Fts5Parse *pParse, /* Parse context */
240763 int eType, /* FTS5_STRING, AND, OR or NOT */
240764 Fts5ExprNode *pLeft, /* Left hand child expression */
240765 Fts5ExprNode *pRight, /* Right hand child expression */
240766 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
240767){
240768 Fts5ExprNode *pRet = 0;
240769
240770 if( pParse->rc==SQLITE_OK0 ){
240771 int nChild = 0; /* Number of children of returned node */
240772 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
240773
240774 assert( (eType!=FTS5_STRING && !pNear)((void) (0))
240775 || (eType==FTS5_STRING && !pLeft && !pRight)((void) (0))
240776 )((void) (0));
240777 if( eType==FTS5_STRING9 && pNear==0 ) return 0;
240778 if( eType!=FTS5_STRING9 && pLeft==0 ) return pRight;
240779 if( eType!=FTS5_STRING9 && pRight==0 ) return pLeft;
240780
240781 if( eType==FTS5_STRING9
240782 && pParse->bPhraseToAnd
240783 && pNear->apPhrase[0]->nTerm>1
240784 ){
240785 pRet = fts5ParsePhraseToAnd(pParse, pNear);
240786 }else{
240787 if( eType==FTS5_NOT3 ){
240788 nChild = 2;
240789 }else if( eType==FTS5_AND2 || eType==FTS5_OR1 ){
240790 nChild = 2;
240791 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
240792 if( pRight->eType==eType ) nChild += pRight->nChild-1;
240793 }
240794
240795 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
240796 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
240797
240798 if( pRet ){
240799 pRet->eType = eType;
240800 pRet->pNear = pNear;
240801 fts5ExprAssignXNext(pRet);
240802 if( eType==FTS5_STRING9 ){
240803 int iPhrase;
240804 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
240805 pNear->apPhrase[iPhrase]->pNode = pRet;
240806 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
240807 pRet->xNext = 0;
240808 pRet->eType = FTS5_EOF0;
240809 }
240810 }
240811
240812 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
240813 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
240814 if( pNear->nPhrase!=1
240815 || pPhrase->nTerm>1
240816 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
240817 ){
240818 sqlite3Fts5ParseError(pParse,
240819 "fts5: %s queries are not supported (detail!=full)",
240820 pNear->nPhrase==1 ? "phrase": "NEAR"
240821 );
240822 sqlite3Fts5ParseNodeFree(pRet);
240823 pRet = 0;
240824 pNear = 0;
240825 assert( pLeft==0 && pRight==0 )((void) (0));
240826 }
240827 }
240828 }else{
240829 assert( pNear==0 )((void) (0));
240830 fts5ExprAddChildren(pRet, pLeft);
240831 fts5ExprAddChildren(pRet, pRight);
240832 pLeft = pRight = 0;
240833 if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH256 ){
240834 sqlite3Fts5ParseError(pParse,
240835 "fts5 expression tree is too large (maximum depth %d)",
240836 SQLITE_FTS5_MAX_EXPR_DEPTH256
240837 );
240838 sqlite3Fts5ParseNodeFree(pRet);
240839 pRet = 0;
240840 }
240841 }
240842 }
240843 }
240844 }
240845
240846 if( pRet==0 ){
240847 assert( pParse->rc!=SQLITE_OK )((void) (0));
240848 sqlite3Fts5ParseNodeFree(pLeft);
240849 sqlite3Fts5ParseNodeFree(pRight);
240850 sqlite3Fts5ParseNearsetFree(pNear);
240851 }
240852 return pRet;
240853}
240854
240855static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
240856 Fts5Parse *pParse, /* Parse context */
240857 Fts5ExprNode *pLeft, /* Left hand child expression */
240858 Fts5ExprNode *pRight /* Right hand child expression */
240859){
240860 Fts5ExprNode *pRet = 0;
240861 Fts5ExprNode *pPrev;
240862
240863 if( pParse->rc ){
240864 sqlite3Fts5ParseNodeFree(pLeft);
240865 sqlite3Fts5ParseNodeFree(pRight);
240866 }else{
240867
240868 assert( pLeft->eType==FTS5_STRING((void) (0))
240869 || pLeft->eType==FTS5_TERM((void) (0))
240870 || pLeft->eType==FTS5_EOF((void) (0))
240871 || pLeft->eType==FTS5_AND((void) (0))
240872 )((void) (0));
240873 assert( pRight->eType==FTS5_STRING((void) (0))
240874 || pRight->eType==FTS5_TERM((void) (0))
240875 || pRight->eType==FTS5_EOF((void) (0))
240876 || (pRight->eType==FTS5_AND && pParse->bPhraseToAnd)((void) (0))
240877 )((void) (0));
240878
240879 if( pLeft->eType==FTS5_AND2 ){
240880 pPrev = pLeft->apChild[pLeft->nChild-1];
240881 }else{
240882 pPrev = pLeft;
240883 }
240884 assert( pPrev->eType==FTS5_STRING((void) (0))
240885 || pPrev->eType==FTS5_TERM((void) (0))
240886 || pPrev->eType==FTS5_EOF((void) (0))
240887 )((void) (0));
240888
240889 if( pRight->eType==FTS5_EOF0 ){
240890 assert( pParse->apPhrase!=0 )((void) (0));
240891 assert( pParse->nPhrase>0 )((void) (0));
240892 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] )((void) (0));
240893 sqlite3Fts5ParseNodeFree(pRight);
240894 pRet = pLeft;
240895 pParse->nPhrase--;
240896 }
240897 else if( pPrev->eType==FTS5_EOF0 ){
240898 Fts5ExprPhrase **ap;
240899
240900 if( pPrev==pLeft ){
240901 pRet = pRight;
240902 }else{
240903 pLeft->apChild[pLeft->nChild-1] = pRight;
240904 pRet = pLeft;
240905 }
240906
240907 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
240908 assert( ap[0]==pPrev->pNear->apPhrase[0] )((void) (0));
240909 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
240910 pParse->nPhrase--;
240911
240912 sqlite3Fts5ParseNodeFree(pPrev);
240913 }
240914 else{
240915 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND2, pLeft, pRight, 0);
240916 }
240917 }
240918
240919 return pRet;
240920}
240921
240922#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
240923static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
240924 sqlite3_int64 nByte = 0;
240925 Fts5ExprTerm *p;
240926 char *zQuoted;
240927
240928 /* Determine the maximum amount of space required. */
240929 for(p=pTerm; p; p=p->pSynonym){
240930 nByte += pTerm->nQueryTerm * 2 + 3 + 2;
240931 }
240932 zQuoted = sqlite3_malloc64(nByte);
240933
240934 if( zQuoted ){
240935 int i = 0;
240936 for(p=pTerm; p; p=p->pSynonym){
240937 char *zIn = p->pTerm;
240938 char *zEnd = &zIn[p->nQueryTerm];
240939 zQuoted[i++] = '"';
240940 while( zIn<zEnd ){
240941 if( *zIn=='"' ) zQuoted[i++] = '"';
240942 zQuoted[i++] = *zIn++;
240943 }
240944 zQuoted[i++] = '"';
240945 if( p->pSynonym ) zQuoted[i++] = '|';
240946 }
240947 if( pTerm->bPrefix ){
240948 zQuoted[i++] = ' ';
240949 zQuoted[i++] = '*';
240950 }
240951 zQuoted[i++] = '\0';
240952 }
240953 return zQuoted;
240954}
240955
240956static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
240957 char *zNew;
240958 va_list ap;
240959 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
240960 zNew = sqlite3_vmprintf(zFmt, ap);
240961 va_end(ap)__builtin_va_end(ap);
240962 if( zApp && zNew ){
240963 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
240964 sqlite3_free(zNew);
240965 zNew = zNew2;
240966 }
240967 sqlite3_free(zApp);
240968 return zNew;
240969}
240970
240971/*
240972** Compose a tcl-readable representation of expression pExpr. Return a
240973** pointer to a buffer containing that representation. It is the
240974** responsibility of the caller to at some point free the buffer using
240975** sqlite3_free().
240976*/
240977static char *fts5ExprPrintTcl(
240978 Fts5Config *pConfig,
240979 const char *zNearsetCmd,
240980 Fts5ExprNode *pExpr
240981){
240982 char *zRet = 0;
240983 if( pExpr->eType==FTS5_STRING9 || pExpr->eType==FTS5_TERM4 ){
240984 Fts5ExprNearset *pNear = pExpr->pNear;
240985 int i;
240986 int iTerm;
240987
240988 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
240989 if( zRet==0 ) return 0;
240990 if( pNear->pColset ){
240991 int *aiCol = pNear->pColset->aiCol;
240992 int nCol = pNear->pColset->nCol;
240993 if( nCol==1 ){
240994 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
240995 }else{
240996 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
240997 for(i=1; i<pNear->pColset->nCol; i++){
240998 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
240999 }
241000 zRet = fts5PrintfAppend(zRet, "} ");
241001 }
241002 if( zRet==0 ) return 0;
241003 }
241004
241005 if( pNear->nPhrase>1 ){
241006 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
241007 if( zRet==0 ) return 0;
241008 }
241009
241010 zRet = fts5PrintfAppend(zRet, "--");
241011 if( zRet==0 ) return 0;
241012
241013 for(i=0; i<pNear->nPhrase; i++){
241014 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
241015
241016 zRet = fts5PrintfAppend(zRet, " {");
241017 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
241018 Fts5ExprTerm *p = &pPhrase->aTerm[iTerm];
241019 zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ",
241020 p->nQueryTerm, p->pTerm
241021 );
241022 if( pPhrase->aTerm[iTerm].bPrefix ){
241023 zRet = fts5PrintfAppend(zRet, "*");
241024 }
241025 }
241026
241027 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
241028 if( zRet==0 ) return 0;
241029 }
241030
241031 }else if( pExpr->eType==0 ){
241032 zRet = sqlite3_mprintf("{}");
241033 }else{
241034 char const *zOp = 0;
241035 int i;
241036 switch( pExpr->eType ){
241037 case FTS5_AND2: zOp = "AND"; break;
241038 case FTS5_NOT3: zOp = "NOT"; break;
241039 default:
241040 assert( pExpr->eType==FTS5_OR )((void) (0));
241041 zOp = "OR";
241042 break;
241043 }
241044
241045 zRet = sqlite3_mprintf("%s", zOp);
241046 for(i=0; zRet && i<pExpr->nChild; i++){
241047 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
241048 if( !z ){
241049 sqlite3_free(zRet);
241050 zRet = 0;
241051 }else{
241052 zRet = fts5PrintfAppend(zRet, " [%z]", z);
241053 }
241054 }
241055 }
241056
241057 return zRet;
241058}
241059
241060static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
241061 char *zRet = 0;
241062 if( pExpr->eType==0 ){
241063 return sqlite3_mprintf("\"\"");
241064 }else
241065 if( pExpr->eType==FTS5_STRING9 || pExpr->eType==FTS5_TERM4 ){
241066 Fts5ExprNearset *pNear = pExpr->pNear;
241067 int i;
241068 int iTerm;
241069
241070 if( pNear->pColset ){
241071 int ii;
241072 Fts5Colset *pColset = pNear->pColset;
241073 if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
241074 for(ii=0; ii<pColset->nCol; ii++){
241075 zRet = fts5PrintfAppend(zRet, "%s%s",
241076 pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
241077 );
241078 }
241079 if( zRet ){
241080 zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
241081 }
241082 if( zRet==0 ) return 0;
241083 }
241084
241085 if( pNear->nPhrase>1 ){
241086 zRet = fts5PrintfAppend(zRet, "NEAR(");
241087 if( zRet==0 ) return 0;
241088 }
241089
241090 for(i=0; i<pNear->nPhrase; i++){
241091 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
241092 if( i!=0 ){
241093 zRet = fts5PrintfAppend(zRet, " ");
241094 if( zRet==0 ) return 0;
241095 }
241096 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
241097 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
241098 if( zTerm ){
241099 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
241100 sqlite3_free(zTerm);
241101 }
241102 if( zTerm==0 || zRet==0 ){
241103 sqlite3_free(zRet);
241104 return 0;
241105 }
241106 }
241107 }
241108
241109 if( pNear->nPhrase>1 ){
241110 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
241111 if( zRet==0 ) return 0;
241112 }
241113
241114 }else{
241115 char const *zOp = 0;
241116 int i;
241117
241118 switch( pExpr->eType ){
241119 case FTS5_AND2: zOp = " AND "; break;
241120 case FTS5_NOT3: zOp = " NOT "; break;
241121 default:
241122 assert( pExpr->eType==FTS5_OR )((void) (0));
241123 zOp = " OR ";
241124 break;
241125 }
241126
241127 for(i=0; i<pExpr->nChild; i++){
241128 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
241129 if( z==0 ){
241130 sqlite3_free(zRet);
241131 zRet = 0;
241132 }else{
241133 int e = pExpr->apChild[i]->eType;
241134 int b = (e!=FTS5_STRING9 && e!=FTS5_TERM4 && e!=FTS5_EOF0);
241135 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
241136 (i==0 ? "" : zOp),
241137 (b?"(":""), z, (b?")":"")
241138 );
241139 }
241140 if( zRet==0 ) break;
241141 }
241142 }
241143
241144 return zRet;
241145}
241146
241147/*
241148** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
241149** and fts5_expr_tcl() (bTcl!=0).
241150*/
241151static void fts5ExprFunction(
241152 sqlite3_context *pCtx, /* Function call context */
241153 int nArg, /* Number of args */
241154 sqlite3_value **apVal, /* Function arguments */
241155 int bTcl
241156){
241157 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
241158 sqlite3 *db = sqlite3_context_db_handle(pCtx);
241159 const char *zExpr = 0;
241160 char *zErr = 0;
241161 Fts5Expr *pExpr = 0;
241162 int rc;
241163 int i;
241164
241165 const char **azConfig; /* Array of arguments for Fts5Config */
241166 const char *zNearsetCmd = "nearset";
241167 int nConfig; /* Size of azConfig[] */
241168 Fts5Config *pConfig = 0;
241169 int iArg = 1;
241170
241171 if( nArg<1 ){
241172 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
241173 bTcl ? "fts5_expr_tcl" : "fts5_expr"
241174 );
241175 sqlite3_result_error(pCtx, zErr, -1);
241176 sqlite3_free(zErr);
241177 return;
241178 }
241179
241180 if( bTcl && nArg>1 ){
241181 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
241182 iArg = 2;
241183 }
241184
241185 nConfig = 3 + (nArg-iArg);
241186 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
241187 if( azConfig==0 ){
241188 sqlite3_result_error_nomem(pCtx);
241189 return;
241190 }
241191 azConfig[0] = 0;
241192 azConfig[1] = "main";
241193 azConfig[2] = "tbl";
241194 for(i=3; iArg<nArg; iArg++){
241195 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
241196 azConfig[i++] = (z ? z : "");
241197 }
241198
241199 zExpr = (const char*)sqlite3_value_text(apVal[0]);
241200 if( zExpr==0 ) zExpr = "";
241201
241202 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
241203 if( rc==SQLITE_OK0 ){
241204 rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
241205 }
241206 if( rc==SQLITE_OK0 ){
241207 char *zText;
241208 if( pExpr->pRoot->xNext==0 ){
241209 zText = sqlite3_mprintf("");
241210 }else if( bTcl ){
241211 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
241212 }else{
241213 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
241214 }
241215 if( zText==0 ){
241216 rc = SQLITE_NOMEM7;
241217 }else{
241218 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
241219 sqlite3_free(zText);
241220 }
241221 }
241222
241223 if( rc!=SQLITE_OK0 ){
241224 if( zErr ){
241225 sqlite3_result_error(pCtx, zErr, -1);
241226 sqlite3_free(zErr);
241227 }else{
241228 sqlite3_result_error_code(pCtx, rc);
241229 }
241230 }
241231 sqlite3_free((void *)azConfig);
241232 sqlite3Fts5ConfigFree(pConfig);
241233 sqlite3Fts5ExprFree(pExpr);
241234}
241235
241236static void fts5ExprFunctionHr(
241237 sqlite3_context *pCtx, /* Function call context */
241238 int nArg, /* Number of args */
241239 sqlite3_value **apVal /* Function arguments */
241240){
241241 fts5ExprFunction(pCtx, nArg, apVal, 0);
241242}
241243static void fts5ExprFunctionTcl(
241244 sqlite3_context *pCtx, /* Function call context */
241245 int nArg, /* Number of args */
241246 sqlite3_value **apVal /* Function arguments */
241247){
241248 fts5ExprFunction(pCtx, nArg, apVal, 1);
241249}
241250
241251/*
241252** The implementation of an SQLite user-defined-function that accepts a
241253** single integer as an argument. If the integer is an alpha-numeric
241254** unicode code point, 1 is returned. Otherwise 0.
241255*/
241256static void fts5ExprIsAlnum(
241257 sqlite3_context *pCtx, /* Function call context */
241258 int nArg, /* Number of args */
241259 sqlite3_value **apVal /* Function arguments */
241260){
241261 int iCode;
241262 u8 aArr[32];
241263 if( nArg!=1 ){
241264 sqlite3_result_error(pCtx,
241265 "wrong number of arguments to function fts5_isalnum", -1
241266 );
241267 return;
241268 }
241269 memset(aArr, 0, sizeof(aArr));
241270 sqlite3Fts5UnicodeCatParse("L*", aArr);
241271 sqlite3Fts5UnicodeCatParse("N*", aArr);
241272 sqlite3Fts5UnicodeCatParse("Co", aArr);
241273 iCode = sqlite3_value_int(apVal[0]);
241274 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
241275}
241276
241277static void fts5ExprFold(
241278 sqlite3_context *pCtx, /* Function call context */
241279 int nArg, /* Number of args */
241280 sqlite3_value **apVal /* Function arguments */
241281){
241282 if( nArg!=1 && nArg!=2 ){
241283 sqlite3_result_error(pCtx,
241284 "wrong number of arguments to function fts5_fold", -1
241285 );
241286 }else{
241287 int iCode;
241288 int bRemoveDiacritics = 0;
241289 iCode = sqlite3_value_int(apVal[0]);
241290 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
241291 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
241292 }
241293}
241294#endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */
241295
241296/*
241297** This is called during initialization to register the fts5_expr() scalar
241298** UDF with the SQLite handle passed as the only argument.
241299*/
241300static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
241301#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
241302 struct Fts5ExprFunc {
241303 const char *z;
241304 void (*x)(sqlite3_context*,int,sqlite3_value**);
241305 } aFunc[] = {
241306 { "fts5_expr", fts5ExprFunctionHr },
241307 { "fts5_expr_tcl", fts5ExprFunctionTcl },
241308 { "fts5_isalnum", fts5ExprIsAlnum },
241309 { "fts5_fold", fts5ExprFold },
241310 };
241311 int i;
241312 int rc = SQLITE_OK0;
241313 void *pCtx = (void*)pGlobal;
241314
241315 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aFunc)((int)(sizeof(aFunc)/sizeof(aFunc[0]))); i++){
241316 struct Fts5ExprFunc *p = &aFunc[i];
241317 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF81, pCtx, p->x, 0, 0);
241318 }
241319#else
241320 int rc = SQLITE_OK0;
241321 UNUSED_PARAM2(pGlobal,db)(void)(pGlobal), (void)(db);
241322#endif
241323
241324 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
241325 ** sqlite3Fts5ParserFallback() are unused */
241326#ifndef NDEBUG1
241327 (void)sqlite3Fts5ParserTrace;
241328#endif
241329 (void)sqlite3Fts5ParserFallback;
241330
241331 return rc;
241332}
241333
241334/*
241335** Return the number of phrases in expression pExpr.
241336*/
241337static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
241338 return (pExpr ? pExpr->nPhrase : 0);
241339}
241340
241341/*
241342** Return the number of terms in the iPhrase'th phrase in pExpr.
241343*/
241344static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
241345 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
241346 return pExpr->apExprPhrase[iPhrase]->nTerm;
241347}
241348
241349/*
241350** This function is used to access the current position list for phrase
241351** iPhrase.
241352*/
241353static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
241354 int nRet;
241355 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
241356 Fts5ExprNode *pNode = pPhrase->pNode;
241357 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
241358 *pa = pPhrase->poslist.p;
241359 nRet = pPhrase->poslist.n;
241360 }else{
241361 *pa = 0;
241362 nRet = 0;
241363 }
241364 return nRet;
241365}
241366
241367struct Fts5PoslistPopulator {
241368 Fts5PoslistWriter writer;
241369 int bOk; /* True if ok to populate */
241370 int bMiss;
241371};
241372
241373/*
241374** Clear the position lists associated with all phrases in the expression
241375** passed as the first argument. Argument bLive is true if the expression
241376** might be pointing to a real entry, otherwise it has just been reset.
241377**
241378** At present this function is only used for detail=col and detail=none
241379** fts5 tables. This implies that all phrases must be at most 1 token
241380** in size, as phrase matches are not supported without detail=full.
241381*/
241382static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
241383 Fts5PoslistPopulator *pRet;
241384 pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
241385 if( pRet ){
241386 int i;
241387 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
241388 for(i=0; i<pExpr->nPhrase; i++){
241389 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
241390 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
241391 assert( pExpr->apExprPhrase[i]->nTerm<=1 )((void) (0));
241392 if( bLive &&
241393 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
241394 ){
241395 pRet[i].bMiss = 1;
241396 }else{
241397 pBuf->n = 0;
241398 }
241399 }
241400 }
241401 return pRet;
241402}
241403
241404struct Fts5ExprCtx {
241405 Fts5Expr *pExpr;
241406 Fts5PoslistPopulator *aPopulator;
241407 i64 iOff;
241408};
241409typedef struct Fts5ExprCtx Fts5ExprCtx;
241410
241411/*
241412** TODO: Make this more efficient!
241413*/
241414static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
241415 int i;
241416 for(i=0; i<pColset->nCol; i++){
241417 if( pColset->aiCol[i]==iCol ) return 1;
241418 }
241419 return 0;
241420}
241421
241422/*
241423** pToken is a buffer nToken bytes in size that may or may not contain
241424** an embedded 0x00 byte. If it does, return the number of bytes in
241425** the buffer before the 0x00. If it does not, return nToken.
241426*/
241427static int fts5QueryTerm(const char *pToken, int nToken){
241428 int ii;
241429 for(ii=0; ii<nToken && pToken[ii]; ii++){}
241430 return ii;
241431}
241432
241433static int fts5ExprPopulatePoslistsCb(
241434 void *pCtx, /* Copy of 2nd argument to xTokenize() */
241435 int tflags, /* Mask of FTS5_TOKEN_* flags */
241436 const char *pToken, /* Pointer to buffer containing token */
241437 int nToken, /* Size of token in bytes */
241438 int iUnused1, /* Byte offset of token within input text */
241439 int iUnused2 /* Byte offset of end of token within input text */
241440){
241441 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
241442 Fts5Expr *pExpr = p->pExpr;
241443 int i;
241444 int nQuery = nToken;
241445 i64 iRowid = pExpr->pRoot->iRowid;
241446
241447 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
241448
241449 if( nQuery>FTS5_MAX_TOKEN_SIZE32768 ) nQuery = FTS5_MAX_TOKEN_SIZE32768;
241450 if( pExpr->pConfig->bTokendata ){
241451 nQuery = fts5QueryTerm(pToken, nQuery);
241452 }
241453 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ) p->iOff++;
241454 for(i=0; i<pExpr->nPhrase; i++){
241455 Fts5ExprTerm *pT;
241456 if( p->aPopulator[i].bOk==0 ) continue;
241457 for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){
241458 if( (pT->nQueryTerm==nQuery || (pT->nQueryTerm<nQuery && pT->bPrefix))
241459 && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0
241460 ){
241461 int rc = sqlite3Fts5PoslistWriterAppend(
241462 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
241463 );
241464 if( rc==SQLITE_OK0 && pExpr->pConfig->bTokendata && !pT->bPrefix ){
241465 int iCol = p->iOff>>32;
241466 int iTokOff = p->iOff & 0x7FFFFFFF;
241467 rc = sqlite3Fts5IndexIterWriteTokendata(
241468 pT->pIter, pToken, nToken, iRowid, iCol, iTokOff
241469 );
241470 }
241471 if( rc ) return rc;
241472 break;
241473 }
241474 }
241475 }
241476 return SQLITE_OK0;
241477}
241478
241479static int sqlite3Fts5ExprPopulatePoslists(
241480 Fts5Config *pConfig,
241481 Fts5Expr *pExpr,
241482 Fts5PoslistPopulator *aPopulator,
241483 int iCol,
241484 const char *z, int n
241485){
241486 int i;
241487 Fts5ExprCtx sCtx;
241488 sCtx.pExpr = pExpr;
241489 sCtx.aPopulator = aPopulator;
241490 sCtx.iOff = (((i64)iCol) << 32) - 1;
241491
241492 for(i=0; i<pExpr->nPhrase; i++){
241493 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
241494 Fts5Colset *pColset = pNode->pNear->pColset;
241495 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
241496 || aPopulator[i].bMiss
241497 ){
241498 aPopulator[i].bOk = 0;
241499 }else{
241500 aPopulator[i].bOk = 1;
241501 }
241502 }
241503
241504 return sqlite3Fts5Tokenize(pConfig,
241505 FTS5_TOKENIZE_DOCUMENT0x0004, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
241506 );
241507}
241508
241509static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
241510 if( pNode->eType==FTS5_TERM4 || pNode->eType==FTS5_STRING9 ){
241511 pNode->pNear->apPhrase[0]->poslist.n = 0;
241512 }else{
241513 int i;
241514 for(i=0; i<pNode->nChild; i++){
241515 fts5ExprClearPoslists(pNode->apChild[i]);
241516 }
241517 }
241518}
241519
241520static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
241521 pNode->iRowid = iRowid;
241522 pNode->bEof = 0;
241523 switch( pNode->eType ){
241524 case 0:
241525 case FTS5_TERM4:
241526 case FTS5_STRING9:
241527 return (pNode->pNear->apPhrase[0]->poslist.n>0);
241528
241529 case FTS5_AND2: {
241530 int i;
241531 for(i=0; i<pNode->nChild; i++){
241532 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
241533 fts5ExprClearPoslists(pNode);
241534 return 0;
241535 }
241536 }
241537 break;
241538 }
241539
241540 case FTS5_OR1: {
241541 int i;
241542 int bRet = 0;
241543 for(i=0; i<pNode->nChild; i++){
241544 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
241545 bRet = 1;
241546 }
241547 }
241548 return bRet;
241549 }
241550
241551 default: {
241552 assert( pNode->eType==FTS5_NOT )((void) (0));
241553 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
241554 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
241555 ){
241556 fts5ExprClearPoslists(pNode);
241557 return 0;
241558 }
241559 break;
241560 }
241561 }
241562 return 1;
241563}
241564
241565static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
241566 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
241567}
241568
241569/*
241570** This function is only called for detail=columns tables.
241571*/
241572static int sqlite3Fts5ExprPhraseCollist(
241573 Fts5Expr *pExpr,
241574 int iPhrase,
241575 const u8 **ppCollist,
241576 int *pnCollist
241577){
241578 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
241579 Fts5ExprNode *pNode = pPhrase->pNode;
241580 int rc = SQLITE_OK0;
241581
241582 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase )((void) (0));
241583 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
241584
241585 if( pNode->bEof==0
241586 && pNode->iRowid==pExpr->pRoot->iRowid
241587 && pPhrase->poslist.n>0
241588 ){
241589 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
241590 if( pTerm->pSynonym ){
241591 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
241592 rc = fts5ExprSynonymList(
241593 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
241594 );
241595 }else{
241596 *ppCollist = pPhrase->aTerm[0].pIter->pData;
241597 *pnCollist = pPhrase->aTerm[0].pIter->nData;
241598 }
241599 }else{
241600 *ppCollist = 0;
241601 *pnCollist = 0;
241602 }
241603
241604 return rc;
241605}
241606
241607/*
241608** Does the work of the fts5_api.xQueryToken() API method.
241609*/
241610static int sqlite3Fts5ExprQueryToken(
241611 Fts5Expr *pExpr,
241612 int iPhrase,
241613 int iToken,
241614 const char **ppOut,
241615 int *pnOut
241616){
241617 Fts5ExprPhrase *pPhrase = 0;
241618
241619 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
241620 return SQLITE_RANGE25;
241621 }
241622 pPhrase = pExpr->apExprPhrase[iPhrase];
241623 if( iToken<0 || iToken>=pPhrase->nTerm ){
241624 return SQLITE_RANGE25;
241625 }
241626
241627 *ppOut = pPhrase->aTerm[iToken].pTerm;
241628 *pnOut = pPhrase->aTerm[iToken].nFullTerm;
241629 return SQLITE_OK0;
241630}
241631
241632/*
241633** Does the work of the fts5_api.xInstToken() API method.
241634*/
241635static int sqlite3Fts5ExprInstToken(
241636 Fts5Expr *pExpr,
241637 i64 iRowid,
241638 int iPhrase,
241639 int iCol,
241640 int iOff,
241641 int iToken,
241642 const char **ppOut,
241643 int *pnOut
241644){
241645 Fts5ExprPhrase *pPhrase = 0;
241646 Fts5ExprTerm *pTerm = 0;
241647 int rc = SQLITE_OK0;
241648
241649 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
241650 return SQLITE_RANGE25;
241651 }
241652 pPhrase = pExpr->apExprPhrase[iPhrase];
241653 if( iToken<0 || iToken>=pPhrase->nTerm ){
241654 return SQLITE_RANGE25;
241655 }
241656 pTerm = &pPhrase->aTerm[iToken];
241657 if( pTerm->bPrefix==0 ){
241658 if( pExpr->pConfig->bTokendata ){
241659 rc = sqlite3Fts5IterToken(
241660 pTerm->pIter, iRowid, iCol, iOff+iToken, ppOut, pnOut
241661 );
241662 }else{
241663 *ppOut = pTerm->pTerm;
241664 *pnOut = pTerm->nFullTerm;
241665 }
241666 }
241667 return rc;
241668}
241669
241670/*
241671** Clear the token mappings for all Fts5IndexIter objects mannaged by
241672** the expression passed as the only argument.
241673*/
241674static void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){
241675 int ii;
241676 for(ii=0; ii<pExpr->nPhrase; ii++){
241677 Fts5ExprTerm *pT;
241678 for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){
241679 sqlite3Fts5IndexIterClearTokendata(pT->pIter);
241680 }
241681 }
241682}
241683
241684/*
241685** 2014 August 11
241686**
241687** The author disclaims copyright to this source code. In place of
241688** a legal notice, here is a blessing:
241689**
241690** May you do good and not evil.
241691** May you find forgiveness for yourself and forgive others.
241692** May you share freely, never taking more than you give.
241693**
241694******************************************************************************
241695**
241696*/
241697
241698
241699
241700/* #include "fts5Int.h" */
241701
241702typedef struct Fts5HashEntry Fts5HashEntry;
241703
241704/*
241705** This file contains the implementation of an in-memory hash table used
241706** to accumuluate "term -> doclist" content before it is flused to a level-0
241707** segment.
241708*/
241709
241710
241711struct Fts5Hash {
241712 int eDetail; /* Copy of Fts5Config.eDetail */
241713 int *pnByte; /* Pointer to bytes counter */
241714 int nEntry; /* Number of entries currently in hash */
241715 int nSlot; /* Size of aSlot[] array */
241716 Fts5HashEntry *pScan; /* Current ordered scan item */
241717 Fts5HashEntry **aSlot; /* Array of hash slots */
241718};
241719
241720/*
241721** Each entry in the hash table is represented by an object of the
241722** following type. Each object, its key, and its current data are stored
241723** in a single memory allocation. The key immediately follows the object
241724** in memory. The position list data immediately follows the key data
241725** in memory.
241726**
241727** The key is Fts5HashEntry.nKey bytes in size. It consists of a single
241728** byte identifying the index (either the main term index or a prefix-index),
241729** followed by the term data. For example: "0token". There is no
241730** nul-terminator - in this case nKey=6.
241731**
241732** The data that follows the key is in a similar, but not identical format
241733** to the doclist data stored in the database. It is:
241734**
241735** * Rowid, as a varint
241736** * Position list, without 0x00 terminator.
241737** * Size of previous position list and rowid, as a 4 byte
241738** big-endian integer.
241739**
241740** iRowidOff:
241741** Offset of last rowid written to data area. Relative to first byte of
241742** structure.
241743**
241744** nData:
241745** Bytes of data written since iRowidOff.
241746*/
241747struct Fts5HashEntry {
241748 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
241749 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
241750
241751 int nAlloc; /* Total size of allocation */
241752 int iSzPoslist; /* Offset of space for 4-byte poslist size */
241753 int nData; /* Total bytes of data (incl. structure) */
241754 int nKey; /* Length of key in bytes */
241755 u8 bDel; /* Set delete-flag @ iSzPoslist */
241756 u8 bContent; /* Set content-flag (detail=none mode) */
241757 i16 iCol; /* Column of last value written */
241758 int iPos; /* Position of last value written */
241759 i64 iRowid; /* Rowid of last value written */
241760};
241761
241762/*
241763** Eqivalent to:
241764**
241765** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
241766*/
241767#define fts5EntryKey(p)( ((char *)(&(p)[1])) ) ( ((char *)(&(p)[1])) )
241768
241769
241770/*
241771** Allocate a new hash table.
241772*/
241773static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
241774 int rc = SQLITE_OK0;
241775 Fts5Hash *pNew;
241776
241777 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
241778 if( pNew==0 ){
241779 rc = SQLITE_NOMEM7;
241780 }else{
241781 sqlite3_int64 nByte;
241782 memset(pNew, 0, sizeof(Fts5Hash));
241783 pNew->pnByte = pnByte;
241784 pNew->eDetail = pConfig->eDetail;
241785
241786 pNew->nSlot = 1024;
241787 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
241788 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
241789 if( pNew->aSlot==0 ){
241790 sqlite3_free(pNew);
241791 *ppNew = 0;
241792 rc = SQLITE_NOMEM7;
241793 }else{
241794 memset(pNew->aSlot, 0, (size_t)nByte);
241795 }
241796 }
241797 return rc;
241798}
241799
241800/*
241801** Free a hash table object.
241802*/
241803static void sqlite3Fts5HashFree(Fts5Hash *pHash){
241804 if( pHash ){
241805 sqlite3Fts5HashClear(pHash);
241806 sqlite3_free(pHash->aSlot);
241807 sqlite3_free(pHash);
241808 }
241809}
241810
241811/*
241812** Empty (but do not delete) a hash table.
241813*/
241814static void sqlite3Fts5HashClear(Fts5Hash *pHash){
241815 int i;
241816 for(i=0; i<pHash->nSlot; i++){
241817 Fts5HashEntry *pNext;
241818 Fts5HashEntry *pSlot;
241819 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
241820 pNext = pSlot->pHashNext;
241821 sqlite3_free(pSlot);
241822 }
241823 }
241824 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
241825 pHash->nEntry = 0;
241826}
241827
241828static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
241829 int i;
241830 unsigned int h = 13;
241831 for(i=n-1; i>=0; i--){
241832 h = (h << 3) ^ h ^ p[i];
241833 }
241834 return (h % nSlot);
241835}
241836
241837static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
241838 int i;
241839 unsigned int h = 13;
241840 for(i=n-1; i>=0; i--){
241841 h = (h << 3) ^ h ^ p[i];
241842 }
241843 h = (h << 3) ^ h ^ b;
241844 return (h % nSlot);
241845}
241846
241847/*
241848** Resize the hash table by doubling the number of slots.
241849*/
241850static int fts5HashResize(Fts5Hash *pHash){
241851 int nNew = pHash->nSlot*2;
241852 int i;
241853 Fts5HashEntry **apNew;
241854 Fts5HashEntry **apOld = pHash->aSlot;
241855
241856 apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
241857 if( !apNew ) return SQLITE_NOMEM7;
241858 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
241859
241860 for(i=0; i<pHash->nSlot; i++){
241861 while( apOld[i] ){
241862 unsigned int iHash;
241863 Fts5HashEntry *p = apOld[i];
241864 apOld[i] = p->pHashNext;
241865 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p)( ((char *)(&(p)[1])) ), p->nKey);
241866 p->pHashNext = apNew[iHash];
241867 apNew[iHash] = p;
241868 }
241869 }
241870
241871 sqlite3_free(apOld);
241872 pHash->nSlot = nNew;
241873 pHash->aSlot = apNew;
241874 return SQLITE_OK0;
241875}
241876
241877static int fts5HashAddPoslistSize(
241878 Fts5Hash *pHash,
241879 Fts5HashEntry *p,
241880 Fts5HashEntry *p2
241881){
241882 int nRet = 0;
241883 if( p->iSzPoslist ){
241884 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
241885 int nData = p->nData;
241886 if( pHash->eDetail==FTS5_DETAIL_NONE1 ){
241887 assert( nData==p->iSzPoslist )((void) (0));
241888 if( p->bDel ){
241889 pPtr[nData++] = 0x00;
241890 if( p->bContent ){
241891 pPtr[nData++] = 0x00;
241892 }
241893 }
241894 }else{
241895 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
241896 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
241897
241898 assert( p->bDel==0 || p->bDel==1 )((void) (0));
241899 if( nPos<=127 ){
241900 pPtr[p->iSzPoslist] = (u8)nPos;
241901 }else{
241902 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
241903 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
241904 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
241905 nData += (nByte-1);
241906 }
241907 }
241908
241909 nRet = nData - p->nData;
241910 if( p2==0 ){
241911 p->iSzPoslist = 0;
241912 p->bDel = 0;
241913 p->bContent = 0;
241914 p->nData = nData;
241915 }
241916 }
241917 return nRet;
241918}
241919
241920/*
241921** Add an entry to the in-memory hash table. The key is the concatenation
241922** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
241923**
241924** (bByte || pToken) -> (iRowid,iCol,iPos)
241925**
241926** Or, if iCol is negative, then the value is a delete marker.
241927*/
241928static int sqlite3Fts5HashWrite(
241929 Fts5Hash *pHash,
241930 i64 iRowid, /* Rowid for this entry */
241931 int iCol, /* Column token appears in (-ve -> delete) */
241932 int iPos, /* Position of token within column */
241933 char bByte, /* First byte of token */
241934 const char *pToken, int nToken /* Token to add or remove to or from index */
241935){
241936 unsigned int iHash;
241937 Fts5HashEntry *p;
241938 u8 *pPtr;
241939 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
241940 int bNew; /* If non-delete entry should be written */
241941
241942 bNew = (pHash->eDetail==FTS5_DETAIL_FULL0);
241943
241944 /* Attempt to locate an existing hash entry */
241945 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
241946 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
241947 char *zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
241948 if( zKey[0]==bByte
241949 && p->nKey==nToken+1
241950 && memcmp(&zKey[1], pToken, nToken)==0
241951 ){
241952 break;
241953 }
241954 }
241955
241956 /* If an existing hash entry cannot be found, create a new one. */
241957 if( p==0 ){
241958 /* Figure out how much space to allocate */
241959 char *zKey;
241960 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
241961 if( nByte<128 ) nByte = 128;
241962
241963 /* Grow the Fts5Hash.aSlot[] array if necessary. */
241964 if( (pHash->nEntry*2)>=pHash->nSlot ){
241965 int rc = fts5HashResize(pHash);
241966 if( rc!=SQLITE_OK0 ) return rc;
241967 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
241968 }
241969
241970 /* Allocate new Fts5HashEntry and add it to the hash table. */
241971 p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
241972 if( !p ) return SQLITE_NOMEM7;
241973 memset(p, 0, sizeof(Fts5HashEntry));
241974 p->nAlloc = (int)nByte;
241975 zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
241976 zKey[0] = bByte;
241977 memcpy(&zKey[1], pToken, nToken);
241978 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) )((void) (0));
241979 p->nKey = nToken+1;
241980 zKey[nToken+1] = '\0';
241981 p->nData = nToken+1 + sizeof(Fts5HashEntry);
241982 p->pHashNext = pHash->aSlot[iHash];
241983 pHash->aSlot[iHash] = p;
241984 pHash->nEntry++;
241985
241986 /* Add the first rowid field to the hash-entry */
241987 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
241988 p->iRowid = iRowid;
241989
241990 p->iSzPoslist = p->nData;
241991 if( pHash->eDetail!=FTS5_DETAIL_NONE1 ){
241992 p->nData += 1;
241993 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL0 ? 0 : -1);
241994 }
241995
241996 }else{
241997
241998 /* Appending to an existing hash-entry. Check that there is enough
241999 ** space to append the largest possible new entry. Worst case scenario
242000 ** is:
242001 **
242002 ** + 9 bytes for a new rowid,
242003 ** + 4 byte reserved for the "poslist size" varint.
242004 ** + 1 byte for a "new column" byte,
242005 ** + 3 bytes for a new column number (16-bit max) as a varint,
242006 ** + 5 bytes for the new position offset (32-bit max).
242007 */
242008 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
242009 sqlite3_int64 nNew = p->nAlloc * 2;
242010 Fts5HashEntry *pNew;
242011 Fts5HashEntry **pp;
242012 pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
242013 if( pNew==0 ) return SQLITE_NOMEM7;
242014 pNew->nAlloc = (int)nNew;
242015 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
242016 *pp = pNew;
242017 p = pNew;
242018 }
242019 nIncr -= p->nData;
242020 }
242021 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) )((void) (0));
242022
242023 pPtr = (u8*)p;
242024
242025 /* If this is a new rowid, append the 4-byte size field for the previous
242026 ** entry, and the new rowid for this entry. */
242027 if( iRowid!=p->iRowid ){
242028 u64 iDiff = (u64)iRowid - (u64)p->iRowid;
242029 fts5HashAddPoslistSize(pHash, p, 0);
242030 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iDiff);
242031 p->iRowid = iRowid;
242032 bNew = 1;
242033 p->iSzPoslist = p->nData;
242034 if( pHash->eDetail!=FTS5_DETAIL_NONE1 ){
242035 p->nData += 1;
242036 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL0 ? 0 : -1);
242037 p->iPos = 0;
242038 }
242039 }
242040
242041 if( iCol>=0 ){
242042 if( pHash->eDetail==FTS5_DETAIL_NONE1 ){
242043 p->bContent = 1;
242044 }else{
242045 /* Append a new column value, if necessary */
242046 assert_nc( iCol>=p->iCol )((void) (0));
242047 if( iCol!=p->iCol ){
242048 if( pHash->eDetail==FTS5_DETAIL_FULL0 ){
242049 pPtr[p->nData++] = 0x01;
242050 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
242051 p->iCol = (i16)iCol;
242052 p->iPos = 0;
242053 }else{
242054 bNew = 1;
242055 p->iCol = (i16)(iPos = iCol);
242056 }
242057 }
242058
242059 /* Append the new position offset, if necessary */
242060 if( bNew ){
242061 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
242062 p->iPos = iPos;
242063 }
242064 }
242065 }else{
242066 /* This is a delete. Set the delete flag. */
242067 p->bDel = 1;
242068 }
242069
242070 nIncr += p->nData;
242071 *pHash->pnByte += nIncr;
242072 return SQLITE_OK0;
242073}
242074
242075
242076/*
242077** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
242078** each sorted in key order. This function merges the two lists into a
242079** single list and returns a pointer to its first element.
242080*/
242081static Fts5HashEntry *fts5HashEntryMerge(
242082 Fts5HashEntry *pLeft,
242083 Fts5HashEntry *pRight
242084){
242085 Fts5HashEntry *p1 = pLeft;
242086 Fts5HashEntry *p2 = pRight;
242087 Fts5HashEntry *pRet = 0;
242088 Fts5HashEntry **ppOut = &pRet;
242089
242090 while( p1 || p2 ){
242091 if( p1==0 ){
242092 *ppOut = p2;
242093 p2 = 0;
242094 }else if( p2==0 ){
242095 *ppOut = p1;
242096 p1 = 0;
242097 }else{
242098 char *zKey1 = fts5EntryKey(p1)( ((char *)(&(p1)[1])) );
242099 char *zKey2 = fts5EntryKey(p2)( ((char *)(&(p2)[1])) );
242100 int nMin = MIN(p1->nKey, p2->nKey)((p1->nKey)<(p2->nKey)?(p1->nKey):(p2->nKey));
242101
242102 int cmp = memcmp(zKey1, zKey2, nMin);
242103 if( cmp==0 ){
242104 cmp = p1->nKey - p2->nKey;
242105 }
242106 assert( cmp!=0 )((void) (0));
242107
242108 if( cmp>0 ){
242109 /* p2 is smaller */
242110 *ppOut = p2;
242111 ppOut = &p2->pScanNext;
242112 p2 = p2->pScanNext;
242113 }else{
242114 /* p1 is smaller */
242115 *ppOut = p1;
242116 ppOut = &p1->pScanNext;
242117 p1 = p1->pScanNext;
242118 }
242119 *ppOut = 0;
242120 }
242121 }
242122
242123 return pRet;
242124}
242125
242126/*
242127** Link all tokens from hash table iHash into a list in sorted order. The
242128** tokens are not removed from the hash table.
242129*/
242130static int fts5HashEntrySort(
242131 Fts5Hash *pHash,
242132 const char *pTerm, int nTerm, /* Query prefix, if any */
242133 Fts5HashEntry **ppSorted
242134){
242135 const int nMergeSlot = 32;
242136 Fts5HashEntry **ap;
242137 Fts5HashEntry *pList;
242138 int iSlot;
242139 int i;
242140
242141 *ppSorted = 0;
242142 ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
242143 if( !ap ) return SQLITE_NOMEM7;
242144 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
242145
242146 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
242147 Fts5HashEntry *pIter;
242148 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
242149 if( pTerm==0
242150 || (pIter->nKey>=nTerm && 0==memcmp(fts5EntryKey(pIter)( ((char *)(&(pIter)[1])) ), pTerm, nTerm))
242151 ){
242152 Fts5HashEntry *pEntry = pIter;
242153 pEntry->pScanNext = 0;
242154 for(i=0; ap[i]; i++){
242155 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
242156 ap[i] = 0;
242157 }
242158 ap[i] = pEntry;
242159 }
242160 }
242161 }
242162
242163 pList = 0;
242164 for(i=0; i<nMergeSlot; i++){
242165 pList = fts5HashEntryMerge(pList, ap[i]);
242166 }
242167
242168 sqlite3_free(ap);
242169 *ppSorted = pList;
242170 return SQLITE_OK0;
242171}
242172
242173/*
242174** Query the hash table for a doclist associated with term pTerm/nTerm.
242175*/
242176static int sqlite3Fts5HashQuery(
242177 Fts5Hash *pHash, /* Hash table to query */
242178 int nPre,
242179 const char *pTerm, int nTerm, /* Query term */
242180 void **ppOut, /* OUT: Pointer to new object */
242181 int *pnDoclist /* OUT: Size of doclist in bytes */
242182){
242183 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
242184 char *zKey = 0;
242185 Fts5HashEntry *p;
242186
242187 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
242188 zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
242189 if( nTerm==p->nKey && memcmp(zKey, pTerm, nTerm)==0 ) break;
242190 }
242191
242192 if( p ){
242193 int nHashPre = sizeof(Fts5HashEntry) + nTerm;
242194 int nList = p->nData - nHashPre;
242195 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
242196 if( pRet ){
242197 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
242198 memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
242199 nList += fts5HashAddPoslistSize(pHash, p, pFaux);
242200 *pnDoclist = nList;
242201 }else{
242202 *pnDoclist = 0;
242203 return SQLITE_NOMEM7;
242204 }
242205 }else{
242206 *ppOut = 0;
242207 *pnDoclist = 0;
242208 }
242209
242210 return SQLITE_OK0;
242211}
242212
242213static int sqlite3Fts5HashScanInit(
242214 Fts5Hash *p, /* Hash table to query */
242215 const char *pTerm, int nTerm /* Query prefix */
242216){
242217 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
242218}
242219
242220#ifdef SQLITE_DEBUG
242221static int fts5HashCount(Fts5Hash *pHash){
242222 int nEntry = 0;
242223 int ii;
242224 for(ii=0; ii<pHash->nSlot; ii++){
242225 Fts5HashEntry *p = 0;
242226 for(p=pHash->aSlot[ii]; p; p=p->pHashNext){
242227 nEntry++;
242228 }
242229 }
242230 return nEntry;
242231}
242232#endif
242233
242234/*
242235** Return true if the hash table is empty, false otherwise.
242236*/
242237static int sqlite3Fts5HashIsEmpty(Fts5Hash *pHash){
242238 assert( pHash->nEntry==fts5HashCount(pHash) )((void) (0));
242239 return pHash->nEntry==0;
242240}
242241
242242static void sqlite3Fts5HashScanNext(Fts5Hash *p){
242243 assert( !sqlite3Fts5HashScanEof(p) )((void) (0));
242244 p->pScan = p->pScan->pScanNext;
242245}
242246
242247static int sqlite3Fts5HashScanEof(Fts5Hash *p){
242248 return (p->pScan==0);
242249}
242250
242251static void sqlite3Fts5HashScanEntry(
242252 Fts5Hash *pHash,
242253 const char **pzTerm, /* OUT: term (nul-terminated) */
242254 int *pnTerm, /* OUT: Size of term in bytes */
242255 const u8 **ppDoclist, /* OUT: pointer to doclist */
242256 int *pnDoclist /* OUT: size of doclist in bytes */
242257){
242258 Fts5HashEntry *p;
242259 if( (p = pHash->pScan) ){
242260 char *zKey = fts5EntryKey(p)( ((char *)(&(p)[1])) );
242261 int nTerm = p->nKey;
242262 fts5HashAddPoslistSize(pHash, p, 0);
242263 *pzTerm = zKey;
242264 *pnTerm = nTerm;
242265 *ppDoclist = (const u8*)&zKey[nTerm];
242266 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm);
242267 }else{
242268 *pzTerm = 0;
242269 *pnTerm = 0;
242270 *ppDoclist = 0;
242271 *pnDoclist = 0;
242272 }
242273}
242274
242275/*
242276** 2014 May 31
242277**
242278** The author disclaims copyright to this source code. In place of
242279** a legal notice, here is a blessing:
242280**
242281** May you do good and not evil.
242282** May you find forgiveness for yourself and forgive others.
242283** May you share freely, never taking more than you give.
242284**
242285******************************************************************************
242286**
242287** Low level access to the FTS index stored in the database file. The
242288** routines in this file file implement all read and write access to the
242289** %_data table. Other parts of the system access this functionality via
242290** the interface defined in fts5Int.h.
242291*/
242292
242293
242294/* #include "fts5Int.h" */
242295
242296/*
242297** Overview:
242298**
242299** The %_data table contains all the FTS indexes for an FTS5 virtual table.
242300** As well as the main term index, there may be up to 31 prefix indexes.
242301** The format is similar to FTS3/4, except that:
242302**
242303** * all segment b-tree leaf data is stored in fixed size page records
242304** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
242305** taken to ensure it is possible to iterate in either direction through
242306** the entries in a doclist, or to seek to a specific entry within a
242307** doclist, without loading it into memory.
242308**
242309** * large doclists that span many pages have associated "doclist index"
242310** records that contain a copy of the first rowid on each page spanned by
242311** the doclist. This is used to speed up seek operations, and merges of
242312** large doclists with very small doclists.
242313**
242314** * extra fields in the "structure record" record the state of ongoing
242315** incremental merge operations.
242316**
242317*/
242318
242319
242320#define FTS5_OPT_WORK_UNIT1000 1000 /* Number of leaf pages per optimize step */
242321#define FTS5_WORK_UNIT64 64 /* Number of leaf pages in unit of work */
242322
242323#define FTS5_MIN_DLIDX_SIZE4 4 /* Add dlidx if this many empty pages */
242324
242325#define FTS5_MAIN_PREFIX'0' '0'
242326
242327#if FTS5_MAX_PREFIX_INDEXES31 > 31
242328# error "FTS5_MAX_PREFIX_INDEXES is too large"
242329#endif
242330
242331#define FTS5_MAX_LEVEL64 64
242332
242333/*
242334** There are two versions of the format used for the structure record:
242335**
242336** 1. the legacy format, that may be read by all fts5 versions, and
242337**
242338** 2. the V2 format, which is used by contentless_delete=1 databases.
242339**
242340** Both begin with a 4-byte "configuration cookie" value. Then, a legacy
242341** format structure record contains a varint - the number of levels in
242342** the structure. Whereas a V2 structure record contains the constant
242343** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a
242344** varint has to be at least 16256 to begin with "0xFF". And the default
242345** maximum number of levels is 64.
242346**
242347** See below for more on structure record formats.
242348*/
242349#define FTS5_STRUCTURE_V2"\xFF\x00\x00\x01" "\xFF\x00\x00\x01"
242350
242351/*
242352** Details:
242353**
242354** The %_data table managed by this module,
242355**
242356** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
242357**
242358** , contains the following 6 types of records. See the comments surrounding
242359** the FTS5_*_ROWID macros below for a description of how %_data rowids are
242360** assigned to each fo them.
242361**
242362** 1. Structure Records:
242363**
242364** The set of segments that make up an index - the index structure - are
242365** recorded in a single record within the %_data table. The record consists
242366** of a single 32-bit configuration cookie value followed by a list of
242367** SQLite varints.
242368**
242369** If the structure record is a V2 record, the configuration cookie is
242370** followed by the following 4 bytes: [0xFF 0x00 0x00 0x01].
242371**
242372** Next, the record continues with three varints:
242373**
242374** + number of levels,
242375** + total number of segments on all levels,
242376** + value of write counter.
242377**
242378** Then, for each level from 0 to nMax:
242379**
242380** + number of input segments in ongoing merge.
242381** + total number of segments in level.
242382** + for each segment from oldest to newest:
242383** + segment id (always > 0)
242384** + first leaf page number (often 1, always greater than 0)
242385** + final leaf page number
242386**
242387** Then, for V2 structures only:
242388**
242389** + lower origin counter value,
242390** + upper origin counter value,
242391** + the number of tombstone hash pages.
242392**
242393** 2. The Averages Record:
242394**
242395** A single record within the %_data table. The data is a list of varints.
242396** The first value is the number of rows in the index. Then, for each column
242397** from left to right, the total number of tokens in the column for all
242398** rows of the table.
242399**
242400** 3. Segment leaves:
242401**
242402** TERM/DOCLIST FORMAT:
242403**
242404** Most of each segment leaf is taken up by term/doclist data. The
242405** general format of term/doclist, starting with the first term
242406** on the leaf page, is:
242407**
242408** varint : size of first term
242409** blob: first term data
242410** doclist: first doclist
242411** zero-or-more {
242412** varint: number of bytes in common with previous term
242413** varint: number of bytes of new term data (nNew)
242414** blob: nNew bytes of new term data
242415** doclist: next doclist
242416** }
242417**
242418** doclist format:
242419**
242420** varint: first rowid
242421** poslist: first poslist
242422** zero-or-more {
242423** varint: rowid delta (always > 0)
242424** poslist: next poslist
242425** }
242426**
242427** poslist format:
242428**
242429** varint: size of poslist in bytes multiplied by 2, not including
242430** this field. Plus 1 if this entry carries the "delete" flag.
242431** collist: collist for column 0
242432** zero-or-more {
242433** 0x01 byte
242434** varint: column number (I)
242435** collist: collist for column I
242436** }
242437**
242438** collist format:
242439**
242440** varint: first offset + 2
242441** zero-or-more {
242442** varint: offset delta + 2
242443** }
242444**
242445** PAGE FORMAT
242446**
242447** Each leaf page begins with a 4-byte header containing 2 16-bit
242448** unsigned integer fields in big-endian format. They are:
242449**
242450** * The byte offset of the first rowid on the page, if it exists
242451** and occurs before the first term (otherwise 0).
242452**
242453** * The byte offset of the start of the page footer. If the page
242454** footer is 0 bytes in size, then this field is the same as the
242455** size of the leaf page in bytes.
242456**
242457** The page footer consists of a single varint for each term located
242458** on the page. Each varint is the byte offset of the current term
242459** within the page, delta-compressed against the previous value. In
242460** other words, the first varint in the footer is the byte offset of
242461** the first term, the second is the byte offset of the second less that
242462** of the first, and so on.
242463**
242464** The term/doclist format described above is accurate if the entire
242465** term/doclist data fits on a single leaf page. If this is not the case,
242466** the format is changed in two ways:
242467**
242468** + if the first rowid on a page occurs before the first term, it
242469** is stored as a literal value:
242470**
242471** varint: first rowid
242472**
242473** + the first term on each page is stored in the same way as the
242474** very first term of the segment:
242475**
242476** varint : size of first term
242477** blob: first term data
242478**
242479** 5. Segment doclist indexes:
242480**
242481** Doclist indexes are themselves b-trees, however they usually consist of
242482** a single leaf record only. The format of each doclist index leaf page
242483** is:
242484**
242485** * Flags byte. Bits are:
242486** 0x01: Clear if leaf is also the root page, otherwise set.
242487**
242488** * Page number of fts index leaf page. As a varint.
242489**
242490** * First rowid on page indicated by previous field. As a varint.
242491**
242492** * A list of varints, one for each subsequent termless page. A
242493** positive delta if the termless page contains at least one rowid,
242494** or an 0x00 byte otherwise.
242495**
242496** Internal doclist index nodes are:
242497**
242498** * Flags byte. Bits are:
242499** 0x01: Clear for root page, otherwise set.
242500**
242501** * Page number of first child page. As a varint.
242502**
242503** * Copy of first rowid on page indicated by previous field. As a varint.
242504**
242505** * A list of delta-encoded varints - the first rowid on each subsequent
242506** child page.
242507**
242508** 6. Tombstone Hash Page
242509**
242510** These records are only ever present in contentless_delete=1 tables.
242511** There are zero or more of these associated with each segment. They
242512** are used to store the tombstone rowids for rows contained in the
242513** associated segments.
242514**
242515** The set of nHashPg tombstone hash pages associated with a single
242516** segment together form a single hash table containing tombstone rowids.
242517** To find the page of the hash on which a key might be stored:
242518**
242519** iPg = (rowid % nHashPg)
242520**
242521** Then, within page iPg, which has nSlot slots:
242522**
242523** iSlot = (rowid / nHashPg) % nSlot
242524**
242525** Each tombstone hash page begins with an 8 byte header:
242526**
242527** 1-byte: Key-size (the size in bytes of each slot). Either 4 or 8.
242528** 1-byte: rowid-0-tombstone flag. This flag is only valid on the
242529** first tombstone hash page for each segment (iPg=0). If set,
242530** the hash table contains rowid 0. If clear, it does not.
242531** Rowid 0 is handled specially.
242532** 2-bytes: unused.
242533** 4-bytes: Big-endian integer containing number of entries on page.
242534**
242535** Following this are nSlot 4 or 8 byte slots (depending on the key-size
242536** in the first byte of the page header). The number of slots may be
242537** determined based on the size of the page record and the key-size:
242538**
242539** nSlot = (nByte - 8) / key-size
242540*/
242541
242542/*
242543** Rowids for the averages and structure records in the %_data table.
242544*/
242545#define FTS5_AVERAGES_ROWID1 1 /* Rowid used for the averages record */
242546#define FTS5_STRUCTURE_ROWID10 10 /* The structure record */
242547
242548/*
242549** Macros determining the rowids used by segment leaves and dlidx leaves
242550** and nodes. All nodes and leaves are stored in the %_data table with large
242551** positive rowids.
242552**
242553** Each segment has a unique non-zero 16-bit id.
242554**
242555** The rowid for each segment leaf is found by passing the segment id and
242556** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
242557** sequentially starting from 1.
242558*/
242559#define FTS5_DATA_ID_B16 16 /* Max seg id number 65535 */
242560#define FTS5_DATA_DLI_B1 1 /* Doclist-index flag (1 bit) */
242561#define FTS5_DATA_HEIGHT_B5 5 /* Max dlidx tree height of 32 */
242562#define FTS5_DATA_PAGE_B31 31 /* Max page number of 2147483648 */
242563
242564#define fts5_dri(segid, dlidx, height, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(dlidx) <<
(31 + 5)) + ((i64)(height) << (31)) + ((i64)(pgno)) )
( \
242565 ((i64)(segid) << (FTS5_DATA_PAGE_B31+FTS5_DATA_HEIGHT_B5+FTS5_DATA_DLI_B1)) + \
242566 ((i64)(dlidx) << (FTS5_DATA_PAGE_B31 + FTS5_DATA_HEIGHT_B5)) + \
242567 ((i64)(height) << (FTS5_DATA_PAGE_B31)) + \
242568 ((i64)(pgno)) \
242569)
242570
242571#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)) )
242572#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)) )
242573#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)
) )
242574
242575#ifdef SQLITE_DEBUG
242576static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB(11 | (1<<8)); }
242577#endif
242578
242579
242580/*
242581** Each time a blob is read from the %_data table, it is padded with this
242582** many zero bytes. This makes it easier to decode the various record formats
242583** without overreading if the records are corrupt.
242584*/
242585#define FTS5_DATA_ZERO_PADDING8 8
242586#define FTS5_DATA_PADDING20 20
242587
242588typedef struct Fts5Data Fts5Data;
242589typedef struct Fts5DlidxIter Fts5DlidxIter;
242590typedef struct Fts5DlidxLvl Fts5DlidxLvl;
242591typedef struct Fts5DlidxWriter Fts5DlidxWriter;
242592typedef struct Fts5Iter Fts5Iter;
242593typedef struct Fts5PageWriter Fts5PageWriter;
242594typedef struct Fts5SegIter Fts5SegIter;
242595typedef struct Fts5DoclistIter Fts5DoclistIter;
242596typedef struct Fts5SegWriter Fts5SegWriter;
242597typedef struct Fts5Structure Fts5Structure;
242598typedef struct Fts5StructureLevel Fts5StructureLevel;
242599typedef struct Fts5StructureSegment Fts5StructureSegment;
242600typedef struct Fts5TokenDataIter Fts5TokenDataIter;
242601typedef struct Fts5TokenDataMap Fts5TokenDataMap;
242602typedef struct Fts5TombstoneArray Fts5TombstoneArray;
242603
242604struct Fts5Data {
242605 u8 *p; /* Pointer to buffer containing record */
242606 int nn; /* Size of record in bytes */
242607 int szLeaf; /* Size of leaf without page-index */
242608};
242609
242610/*
242611** One object per %_data table.
242612**
242613** nContentlessDelete:
242614** The number of contentless delete operations since the most recent
242615** call to fts5IndexFlush() or fts5IndexDiscardData(). This is tracked
242616** so that extra auto-merge work can be done by fts5IndexFlush() to
242617** account for the delete operations.
242618*/
242619struct Fts5Index {
242620 Fts5Config *pConfig; /* Virtual table configuration */
242621 char *zDataTbl; /* Name of %_data table */
242622 int nWorkUnit; /* Leaf pages in a "unit" of work */
242623
242624 /*
242625 ** Variables related to the accumulation of tokens and doclists within the
242626 ** in-memory hash tables before they are flushed to disk.
242627 */
242628 Fts5Hash *pHash; /* Hash table for in-memory data */
242629 int nPendingData; /* Current bytes of pending data */
242630 i64 iWriteRowid; /* Rowid for current doc being written */
242631 int bDelete; /* Current write is a delete */
242632 int nContentlessDelete; /* Number of contentless delete ops */
242633 int nPendingRow; /* Number of INSERT in hash table */
242634
242635 /* Error state. */
242636 int rc; /* Current error code */
242637 int flushRc;
242638
242639 /* State used by the fts5DataXXX() functions. */
242640 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
242641 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
242642 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
242643 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
242644 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=?" */
242645 sqlite3_stmt *pIdxSelect;
242646 sqlite3_stmt *pIdxNextSelect;
242647 int nRead; /* Total number of blocks read */
242648
242649 sqlite3_stmt *pDeleteFromIdx;
242650
242651 sqlite3_stmt *pDataVersion;
242652 i64 iStructVersion; /* data_version when pStruct read */
242653 Fts5Structure *pStruct; /* Current db structure (or NULL) */
242654};
242655
242656struct Fts5DoclistIter {
242657 u8 *aEof; /* Pointer to 1 byte past end of doclist */
242658
242659 /* Output variables. aPoslist==0 at EOF */
242660 i64 iRowid;
242661 u8 *aPoslist;
242662 int nPoslist;
242663 int nSize;
242664};
242665
242666/*
242667** The contents of the "structure" record for each index are represented
242668** using an Fts5Structure record in memory. Which uses instances of the
242669** other Fts5StructureXXX types as components.
242670**
242671** nOriginCntr:
242672** This value is set to non-zero for structure records created for
242673** contentlessdelete=1 tables only. In that case it represents the
242674** origin value to apply to the next top-level segment created.
242675*/
242676struct Fts5StructureSegment {
242677 int iSegid; /* Segment id */
242678 int pgnoFirst; /* First leaf page number in segment */
242679 int pgnoLast; /* Last leaf page number in segment */
242680
242681 /* contentlessdelete=1 tables only: */
242682 u64 iOrigin1;
242683 u64 iOrigin2;
242684 int nPgTombstone; /* Number of tombstone hash table pages */
242685 u64 nEntryTombstone; /* Number of tombstone entries that "count" */
242686 u64 nEntry; /* Number of rows in this segment */
242687};
242688struct Fts5StructureLevel {
242689 int nMerge; /* Number of segments in incr-merge */
242690 int nSeg; /* Total number of segments on level */
242691 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
242692};
242693struct Fts5Structure {
242694 int nRef; /* Object reference count */
242695 u64 nWriteCounter; /* Total leaves written to level 0 */
242696 u64 nOriginCntr; /* Origin value for next top-level segment */
242697 int nSegment; /* Total segments in this structure */
242698 int nLevel; /* Number of levels in this index */
242699 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
242700};
242701
242702/*
242703** An object of type Fts5SegWriter is used to write to segments.
242704*/
242705struct Fts5PageWriter {
242706 int pgno; /* Page number for this page */
242707 int iPrevPgidx; /* Previous value written into pgidx */
242708 Fts5Buffer buf; /* Buffer containing leaf data */
242709 Fts5Buffer pgidx; /* Buffer containing page-index */
242710 Fts5Buffer term; /* Buffer containing previous term on page */
242711};
242712struct Fts5DlidxWriter {
242713 int pgno; /* Page number for this page */
242714 int bPrevValid; /* True if iPrev is valid */
242715 i64 iPrev; /* Previous rowid value written to page */
242716 Fts5Buffer buf; /* Buffer containing page data */
242717};
242718struct Fts5SegWriter {
242719 int iSegid; /* Segid to write to */
242720 Fts5PageWriter writer; /* PageWriter object */
242721 i64 iPrevRowid; /* Previous rowid written to current leaf */
242722 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
242723 u8 bFirstRowidInPage; /* True if next rowid is first in page */
242724 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
242725 u8 bFirstTermInPage; /* True if next term will be first in leaf */
242726 int nLeafWritten; /* Number of leaf pages written */
242727 int nEmpty; /* Number of contiguous term-less nodes */
242728
242729 int nDlidx; /* Allocated size of aDlidx[] array */
242730 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
242731
242732 /* Values to insert into the %_idx table */
242733 Fts5Buffer btterm; /* Next term to insert into %_idx table */
242734 int iBtPage; /* Page number corresponding to btterm */
242735};
242736
242737typedef struct Fts5CResult Fts5CResult;
242738struct Fts5CResult {
242739 u16 iFirst; /* aSeg[] index of firstest iterator */
242740 u8 bTermEq; /* True if the terms are equal */
242741};
242742
242743/*
242744** Object for iterating through a single segment, visiting each term/rowid
242745** pair in the segment.
242746**
242747** pSeg:
242748** The segment to iterate through.
242749**
242750** iLeafPgno:
242751** Current leaf page number within segment.
242752**
242753** iLeafOffset:
242754** Byte offset within the current leaf that is the first byte of the
242755** position list data (one byte passed the position-list size field).
242756**
242757** pLeaf:
242758** Buffer containing current leaf page data. Set to NULL at EOF.
242759**
242760** iTermLeafPgno, iTermLeafOffset:
242761** Leaf page number containing the last term read from the segment. And
242762** the offset immediately following the term data.
242763**
242764** flags:
242765** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
242766**
242767** FTS5_SEGITER_ONETERM:
242768** If set, set the iterator to point to EOF after the current doclist
242769** has been exhausted. Do not proceed to the next term in the segment.
242770**
242771** FTS5_SEGITER_REVERSE:
242772** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
242773** it is set, iterate through rowid in descending order instead of the
242774** default ascending order.
242775**
242776** iRowidOffset/nRowidOffset/aRowidOffset:
242777** These are used if the FTS5_SEGITER_REVERSE flag is set.
242778**
242779** For each rowid on the page corresponding to the current term, the
242780** corresponding aRowidOffset[] entry is set to the byte offset of the
242781** start of the "position-list-size" field within the page.
242782**
242783** iTermIdx:
242784** Index of current term on iTermLeafPgno.
242785**
242786** apTombstone/nTombstone:
242787** These are used for contentless_delete=1 tables only. When the cursor
242788** is first allocated, the apTombstone[] array is allocated so that it
242789** is large enough for all tombstones hash pages associated with the
242790** segment. The pages themselves are loaded lazily from the database as
242791** they are required.
242792*/
242793struct Fts5SegIter {
242794 Fts5StructureSegment *pSeg; /* Segment to iterate through */
242795 int flags; /* Mask of configuration flags */
242796 int iLeafPgno; /* Current leaf page number */
242797 Fts5Data *pLeaf; /* Current leaf data */
242798 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
242799 i64 iLeafOffset; /* Byte offset within current leaf */
242800 Fts5TombstoneArray *pTombArray; /* Array of tombstone pages */
242801
242802 /* Next method */
242803 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
242804
242805 /* The page and offset from which the current term was read. The offset
242806 ** is the offset of the first rowid in the current doclist. */
242807 int iTermLeafPgno;
242808 int iTermLeafOffset;
242809
242810 int iPgidxOff; /* Next offset in pgidx */
242811 int iEndofDoclist;
242812
242813 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
242814 int iRowidOffset; /* Current entry in aRowidOffset[] */
242815 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
242816 int *aRowidOffset; /* Array of offset to rowid fields */
242817
242818 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
242819
242820 /* Variables populated based on current entry. */
242821 Fts5Buffer term; /* Current term */
242822 i64 iRowid; /* Current rowid */
242823 int nPos; /* Number of bytes in current position list */
242824 u8 bDel; /* True if the delete flag is set */
242825};
242826
242827/*
242828** Array of tombstone pages. Reference counted.
242829*/
242830struct Fts5TombstoneArray {
242831 int nRef; /* Number of pointers to this object */
242832 int nTombstone;
242833 Fts5Data *apTombstone[1]; /* Array of tombstone pages */
242834};
242835
242836/*
242837** Argument is a pointer to an Fts5Data structure that contains a
242838** leaf page.
242839*/
242840#define ASSERT_SZLEAF_OK(x)((void) (0)) assert( \((void) (0))
242841 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \((void) (0))
242842)((void) (0))
242843
242844#define FTS5_SEGITER_ONETERM0x01 0x01
242845#define FTS5_SEGITER_REVERSE0x02 0x02
242846
242847/*
242848** Argument is a pointer to an Fts5Data structure that contains a leaf
242849** page. This macro evaluates to true if the leaf contains no terms, or
242850** false if it contains at least one term.
242851*/
242852#define fts5LeafIsTermless(x)((x)->szLeaf >= (x)->nn) ((x)->szLeaf >= (x)->nn)
242853
242854#define fts5LeafTermOff(x, i)(fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2])) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
242855
242856#define fts5LeafFirstRowidOff(x)(fts5GetU16((x)->p)) (fts5GetU16((x)->p))
242857
242858/*
242859** Object for iterating through the merged results of one or more segments,
242860** visiting each term/rowid pair in the merged data.
242861**
242862** nSeg is always a power of two greater than or equal to the number of
242863** segments that this object is merging data from. Both the aSeg[] and
242864** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
242865** with zeroed objects - these are handled as if they were iterators opened
242866** on empty segments.
242867**
242868** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
242869** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
242870** comparison in this context is the index of the iterator that currently
242871** points to the smaller term/rowid combination. Iterators at EOF are
242872** considered to be greater than all other iterators.
242873**
242874** aFirst[1] contains the index in aSeg[] of the iterator that points to
242875** the smallest key overall. aFirst[0] is unused.
242876**
242877** poslist:
242878** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
242879** There is no way to tell if this is populated or not.
242880**
242881** pColset:
242882** If not NULL, points to an object containing a set of column indices.
242883** Only matches that occur in one of these columns will be returned.
242884** The Fts5Iter does not own the Fts5Colset object, and so it is not
242885** freed when the iterator is closed - it is owned by the upper layer.
242886*/
242887struct Fts5Iter {
242888 Fts5IndexIter base; /* Base class containing output vars */
242889 Fts5TokenDataIter *pTokenDataIter;
242890
242891 Fts5Index *pIndex; /* Index that owns this iterator */
242892 Fts5Buffer poslist; /* Buffer containing current poslist */
242893 Fts5Colset *pColset; /* Restrict matches to these columns */
242894
242895 /* Invoked to set output variables. */
242896 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
242897
242898 int nSeg; /* Size of aSeg[] array */
242899 int bRev; /* True to iterate in reverse order */
242900 u8 bSkipEmpty; /* True to skip deleted entries */
242901
242902 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
242903 Fts5CResult *aFirst; /* Current merge state (see above) */
242904 Fts5SegIter aSeg[1]; /* Array of segment iterators */
242905};
242906
242907/*
242908** An instance of the following type is used to iterate through the contents
242909** of a doclist-index record.
242910**
242911** pData:
242912** Record containing the doclist-index data.
242913**
242914** bEof:
242915** Set to true once iterator has reached EOF.
242916**
242917** iOff:
242918** Set to the current offset within record pData.
242919*/
242920struct Fts5DlidxLvl {
242921 Fts5Data *pData; /* Data for current page of this level */
242922 int iOff; /* Current offset into pData */
242923 int bEof; /* At EOF already */
242924 int iFirstOff; /* Used by reverse iterators */
242925
242926 /* Output variables */
242927 int iLeafPgno; /* Page number of current leaf page */
242928 i64 iRowid; /* First rowid on leaf iLeafPgno */
242929};
242930struct Fts5DlidxIter {
242931 int nLvl;
242932 int iSegid;
242933 Fts5DlidxLvl aLvl[1];
242934};
242935
242936static void fts5PutU16(u8 *aOut, u16 iVal){
242937 aOut[0] = (iVal>>8);
242938 aOut[1] = (iVal&0xFF);
242939}
242940
242941static u16 fts5GetU16(const u8 *aIn){
242942 return ((u16)aIn[0] << 8) + aIn[1];
242943}
242944
242945/*
242946** The only argument points to a buffer at least 8 bytes in size. This
242947** function interprets the first 8 bytes of the buffer as a 64-bit big-endian
242948** unsigned integer and returns the result.
242949*/
242950static u64 fts5GetU64(u8 *a){
242951 return ((u64)a[0] << 56)
242952 + ((u64)a[1] << 48)
242953 + ((u64)a[2] << 40)
242954 + ((u64)a[3] << 32)
242955 + ((u64)a[4] << 24)
242956 + ((u64)a[5] << 16)
242957 + ((u64)a[6] << 8)
242958 + ((u64)a[7] << 0);
242959}
242960
242961/*
242962** The only argument points to a buffer at least 4 bytes in size. This
242963** function interprets the first 4 bytes of the buffer as a 32-bit big-endian
242964** unsigned integer and returns the result.
242965*/
242966static u32 fts5GetU32(const u8 *a){
242967 return ((u32)a[0] << 24)
242968 + ((u32)a[1] << 16)
242969 + ((u32)a[2] << 8)
242970 + ((u32)a[3] << 0);
242971}
242972
242973/*
242974** Write iVal, formated as a 64-bit big-endian unsigned integer, to the
242975** buffer indicated by the first argument.
242976*/
242977static void fts5PutU64(u8 *a, u64 iVal){
242978 a[0] = ((iVal >> 56) & 0xFF);
242979 a[1] = ((iVal >> 48) & 0xFF);
242980 a[2] = ((iVal >> 40) & 0xFF);
242981 a[3] = ((iVal >> 32) & 0xFF);
242982 a[4] = ((iVal >> 24) & 0xFF);
242983 a[5] = ((iVal >> 16) & 0xFF);
242984 a[6] = ((iVal >> 8) & 0xFF);
242985 a[7] = ((iVal >> 0) & 0xFF);
242986}
242987
242988/*
242989** Write iVal, formated as a 32-bit big-endian unsigned integer, to the
242990** buffer indicated by the first argument.
242991*/
242992static void fts5PutU32(u8 *a, u32 iVal){
242993 a[0] = ((iVal >> 24) & 0xFF);
242994 a[1] = ((iVal >> 16) & 0xFF);
242995 a[2] = ((iVal >> 8) & 0xFF);
242996 a[3] = ((iVal >> 0) & 0xFF);
242997}
242998
242999/*
243000** Allocate and return a buffer at least nByte bytes in size.
243001**
243002** If an OOM error is encountered, return NULL and set the error code in
243003** the Fts5Index handle passed as the first argument.
243004*/
243005static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
243006 return sqlite3Fts5MallocZero(&p->rc, nByte);
243007}
243008
243009/*
243010** Compare the contents of the pLeft buffer with the pRight/nRight blob.
243011**
243012** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
243013** +ve if pRight is smaller than pLeft. In other words:
243014**
243015** res = *pLeft - *pRight
243016*/
243017#ifdef SQLITE_DEBUG
243018static int fts5BufferCompareBlob(
243019 Fts5Buffer *pLeft, /* Left hand side of comparison */
243020 const u8 *pRight, int nRight /* Right hand side of comparison */
243021){
243022 int nCmp = MIN(pLeft->n, nRight)((pLeft->n)<(nRight)?(pLeft->n):(nRight));
243023 int res = memcmp(pLeft->p, pRight, nCmp);
243024 return (res==0 ? (pLeft->n - nRight) : res);
243025}
243026#endif
243027
243028/*
243029** Compare the contents of the two buffers using memcmp(). If one buffer
243030** is a prefix of the other, it is considered the lesser.
243031**
243032** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
243033** +ve if pRight is smaller than pLeft. In other words:
243034**
243035** res = *pLeft - *pRight
243036*/
243037static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
243038 int nCmp, res;
243039 nCmp = MIN(pLeft->n, pRight->n)((pLeft->n)<(pRight->n)?(pLeft->n):(pRight->n)
)
;
243040 assert( nCmp<=0 || pLeft->p!=0 )((void) (0));
243041 assert( nCmp<=0 || pRight->p!=0 )((void) (0));
243042 res = fts5Memcmp(pLeft->p, pRight->p, nCmp)((nCmp)<=0 ? 0 : memcmp((pLeft->p), (pRight->p), (nCmp
)))
;
243043 return (res==0 ? (pLeft->n - pRight->n) : res);
243044}
243045
243046static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
243047 int ret;
243048 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret)sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(ret))
;
243049 return ret;
243050}
243051
243052/*
243053** Close the read-only blob handle, if it is open.
243054*/
243055static void sqlite3Fts5IndexCloseReader(Fts5Index *p){
243056 if( p->pReader ){
243057 sqlite3_blob *pReader = p->pReader;
243058 p->pReader = 0;
243059 sqlite3_blob_close(pReader);
243060 }
243061}
243062
243063/*
243064** Retrieve a record from the %_data table.
243065**
243066** If an error occurs, NULL is returned and an error left in the
243067** Fts5Index object.
243068*/
243069static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
243070 Fts5Data *pRet = 0;
243071 if( p->rc==SQLITE_OK0 ){
243072 int rc = SQLITE_OK0;
243073
243074 if( p->pReader ){
243075 /* This call may return SQLITE_ABORT if there has been a savepoint
243076 ** rollback since it was last used. In this case a new blob handle
243077 ** is required. */
243078 sqlite3_blob *pBlob = p->pReader;
243079 p->pReader = 0;
243080 rc = sqlite3_blob_reopen(pBlob, iRowid);
243081 assert( p->pReader==0 )((void) (0));
243082 p->pReader = pBlob;
243083 if( rc!=SQLITE_OK0 ){
243084 sqlite3Fts5IndexCloseReader(p);
243085 }
243086 if( rc==SQLITE_ABORT4 ) rc = SQLITE_OK0;
243087 }
243088
243089 /* If the blob handle is not open at this point, open it and seek
243090 ** to the requested entry. */
243091 if( p->pReader==0 && rc==SQLITE_OK0 ){
243092 Fts5Config *pConfig = p->pConfig;
243093 rc = sqlite3_blob_open(pConfig->db,
243094 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
243095 );
243096 }
243097
243098 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
243099 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
243100 ** All the reasons those functions might return SQLITE_ERROR - missing
243101 ** table, missing row, non-blob/text in block column - indicate
243102 ** backing store corruption. */
243103 if( rc==SQLITE_ERROR1 ) rc = FTS5_CORRUPT(11 | (1<<8));
243104
243105 if( rc==SQLITE_OK0 ){
243106 u8 *aOut = 0; /* Read blob data into this buffer */
243107 int nByte = sqlite3_blob_bytes(p->pReader);
243108 int szData = (sizeof(Fts5Data) + 7) & ~7;
243109 sqlite3_int64 nAlloc = szData + nByte + FTS5_DATA_PADDING20;
243110 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
243111 if( pRet ){
243112 pRet->nn = nByte;
243113 aOut = pRet->p = (u8*)pRet + szData;
243114 }else{
243115 rc = SQLITE_NOMEM7;
243116 }
243117
243118 if( rc==SQLITE_OK0 ){
243119 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
243120 }
243121 if( rc!=SQLITE_OK0 ){
243122 sqlite3_free(pRet);
243123 pRet = 0;
243124 }else{
243125 /* TODO1: Fix this */
243126 pRet->p[nByte] = 0x00;
243127 pRet->p[nByte+1] = 0x00;
243128 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
243129 }
243130 }
243131 p->rc = rc;
243132 p->nRead++;
243133 }
243134
243135 assert( (pRet==0)==(p->rc!=SQLITE_OK) )((void) (0));
243136 assert( pRet==0 || EIGHT_BYTE_ALIGNMENT( pRet->p ) )((void) (0));
243137 return pRet;
243138}
243139
243140
243141/*
243142** Release a reference to data record returned by an earlier call to
243143** fts5DataRead().
243144*/
243145static void fts5DataRelease(Fts5Data *pData){
243146 sqlite3_free(pData);
243147}
243148
243149static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
243150 Fts5Data *pRet = fts5DataRead(p, iRowid);
243151 if( pRet ){
243152 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
243153 p->rc = FTS5_CORRUPT(11 | (1<<8));
243154 fts5DataRelease(pRet);
243155 pRet = 0;
243156 }
243157 }
243158 return pRet;
243159}
243160
243161static int fts5IndexPrepareStmt(
243162 Fts5Index *p,
243163 sqlite3_stmt **ppStmt,
243164 char *zSql
243165){
243166 if( p->rc==SQLITE_OK0 ){
243167 if( zSql ){
243168 p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
243169 SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04,
243170 ppStmt, 0);
243171 }else{
243172 p->rc = SQLITE_NOMEM7;
243173 }
243174 }
243175 sqlite3_free(zSql);
243176 return p->rc;
243177}
243178
243179
243180/*
243181** INSERT OR REPLACE a record into the %_data table.
243182*/
243183static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
243184 if( p->rc!=SQLITE_OK0 ) return;
243185
243186 if( p->pWriter==0 ){
243187 Fts5Config *pConfig = p->pConfig;
243188 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
243189 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
243190 pConfig->zDb, pConfig->zName
243191 ));
243192 if( p->rc ) return;
243193 }
243194
243195 sqlite3_bind_int64(p->pWriter, 1, iRowid);
243196 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC((sqlite3_destructor_type)0));
243197 sqlite3_step(p->pWriter);
243198 p->rc = sqlite3_reset(p->pWriter);
243199 sqlite3_bind_null(p->pWriter, 2);
243200}
243201
243202/*
243203** Execute the following SQL:
243204**
243205** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
243206*/
243207static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
243208 if( p->rc!=SQLITE_OK0 ) return;
243209
243210 if( p->pDeleter==0 ){
243211 Fts5Config *pConfig = p->pConfig;
243212 char *zSql = sqlite3_mprintf(
243213 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
243214 pConfig->zDb, pConfig->zName
243215 );
243216 if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
243217 }
243218
243219 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
243220 sqlite3_bind_int64(p->pDeleter, 2, iLast);
243221 sqlite3_step(p->pDeleter);
243222 p->rc = sqlite3_reset(p->pDeleter);
243223}
243224
243225/*
243226** Remove all records associated with segment iSegid.
243227*/
243228static void fts5DataRemoveSegment(Fts5Index *p, Fts5StructureSegment *pSeg){
243229 int iSegid = pSeg->iSegid;
243230 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(0)) )
;
243231 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)( ((i64)(iSegid+1) << (31 +5 +1)) + ((i64)(0) << (
31 + 5)) + ((i64)(0) << (31)) + ((i64)(0)) )
-1;
243232 fts5DataDelete(p, iFirst, iLast);
243233
243234 if( pSeg->nPgTombstone ){
243235 i64 iTomb1 = FTS5_TOMBSTONE_ROWID(iSegid, 0)( ((i64)(iSegid+(1<<16)) << (31 +5 +1)) + ((i64)(
0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64)(0))
)
;
243236 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)) )
;
243237 fts5DataDelete(p, iTomb1, iTomb2);
243238 }
243239 if( p->pIdxDeleter==0 ){
243240 Fts5Config *pConfig = p->pConfig;
243241 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
243242 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
243243 pConfig->zDb, pConfig->zName
243244 ));
243245 }
243246 if( p->rc==SQLITE_OK0 ){
243247 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
243248 sqlite3_step(p->pIdxDeleter);
243249 p->rc = sqlite3_reset(p->pIdxDeleter);
243250 }
243251}
243252
243253/*
243254** Release a reference to an Fts5Structure object returned by an earlier
243255** call to fts5StructureRead() or fts5StructureDecode().
243256*/
243257static void fts5StructureRelease(Fts5Structure *pStruct){
243258 if( pStruct && 0>=(--pStruct->nRef) ){
243259 int i;
243260 assert( pStruct->nRef==0 )((void) (0));
243261 for(i=0; i<pStruct->nLevel; i++){
243262 sqlite3_free(pStruct->aLevel[i].aSeg);
243263 }
243264 sqlite3_free(pStruct);
243265 }
243266}
243267
243268static void fts5StructureRef(Fts5Structure *pStruct){
243269 pStruct->nRef++;
243270}
243271
243272static void *sqlite3Fts5StructureRef(Fts5Index *p){
243273 fts5StructureRef(p->pStruct);
243274 return (void*)p->pStruct;
243275}
243276static void sqlite3Fts5StructureRelease(void *p){
243277 if( p ){
243278 fts5StructureRelease((Fts5Structure*)p);
243279 }
243280}
243281static int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){
243282 if( p->pStruct!=(Fts5Structure*)pStruct ){
243283 return SQLITE_ABORT4;
243284 }
243285 return SQLITE_OK0;
243286}
243287
243288/*
243289** Ensure that structure object (*pp) is writable.
243290**
243291** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
243292** an error occurs, (*pRc) is set to an SQLite error code before returning.
243293*/
243294static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){
243295 Fts5Structure *p = *pp;
243296 if( *pRc==SQLITE_OK0 && p->nRef>1 ){
243297 i64 nByte = sizeof(Fts5Structure)+(p->nLevel-1)*sizeof(Fts5StructureLevel);
243298 Fts5Structure *pNew;
243299 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte);
243300 if( pNew ){
243301 int i;
243302 memcpy(pNew, p, nByte);
243303 for(i=0; i<p->nLevel; i++) pNew->aLevel[i].aSeg = 0;
243304 for(i=0; i<p->nLevel; i++){
243305 Fts5StructureLevel *pLvl = &pNew->aLevel[i];
243306 nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg;
243307 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte);
243308 if( pLvl->aSeg==0 ){
243309 for(i=0; i<p->nLevel; i++){
243310 sqlite3_free(pNew->aLevel[i].aSeg);
243311 }
243312 sqlite3_free(pNew);
243313 return;
243314 }
243315 memcpy(pLvl->aSeg, p->aLevel[i].aSeg, nByte);
243316 }
243317 p->nRef--;
243318 pNew->nRef = 1;
243319 }
243320 *pp = pNew;
243321 }
243322}
243323
243324/*
243325** Deserialize and return the structure record currently stored in serialized
243326** form within buffer pData/nData.
243327**
243328** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
243329** are over-allocated by one slot. This allows the structure contents
243330** to be more easily edited.
243331**
243332** If an error occurs, *ppOut is set to NULL and an SQLite error code
243333** returned. Otherwise, *ppOut is set to point to the new object and
243334** SQLITE_OK returned.
243335*/
243336static int fts5StructureDecode(
243337 const u8 *pData, /* Buffer containing serialized structure */
243338 int nData, /* Size of buffer pData in bytes */
243339 int *piCookie, /* Configuration cookie value */
243340 Fts5Structure **ppOut /* OUT: Deserialized object */
243341){
243342 int rc = SQLITE_OK0;
243343 int i = 0;
243344 int iLvl;
243345 int nLevel = 0;
243346 int nSegment = 0;
243347 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
243348 Fts5Structure *pRet = 0; /* Structure object to return */
243349 int bStructureV2 = 0; /* True for FTS5_STRUCTURE_V2 */
243350 u64 nOriginCntr = 0; /* Largest origin value seen so far */
243351
243352 /* Grab the cookie value */
243353 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
243354 i = 4;
243355
243356 /* Check if this is a V2 structure record. Set bStructureV2 if it is. */
243357 if( 0==memcmp(&pData[i], FTS5_STRUCTURE_V2"\xFF\x00\x00\x01", 4) ){
243358 i += 4;
243359 bStructureV2 = 1;
243360 }
243361
243362 /* Read the total number of levels and segments from the start of the
243363 ** structure record. */
243364 i += fts5GetVarint32(&pData[i], nLevel)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(nLevel));
243365 i += fts5GetVarint32(&pData[i], nSegment)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(nSegment));
243366 if( nLevel>FTS5_MAX_SEGMENT2000 || nLevel<0
243367 || nSegment>FTS5_MAX_SEGMENT2000 || nSegment<0
243368 ){
243369 return FTS5_CORRUPT(11 | (1<<8));
243370 }
243371 nByte = (
243372 sizeof(Fts5Structure) + /* Main structure */
243373 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
243374 );
243375 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
243376
243377 if( pRet ){
243378 pRet->nRef = 1;
243379 pRet->nLevel = nLevel;
243380 pRet->nSegment = nSegment;
243381 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
243382
243383 for(iLvl=0; rc==SQLITE_OK0 && iLvl<nLevel; iLvl++){
243384 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
243385 int nTotal = 0;
243386 int iSeg;
243387
243388 if( i>=nData ){
243389 rc = FTS5_CORRUPT(11 | (1<<8));
243390 }else{
243391 i += fts5GetVarint32(&pData[i], pLvl->nMerge)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pLvl->nMerge
))
;
243392 i += fts5GetVarint32(&pData[i], nTotal)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(nTotal));
243393 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT(11 | (1<<8));
243394 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
243395 nTotal * sizeof(Fts5StructureSegment)
243396 );
243397 nSegment -= nTotal;
243398 }
243399
243400 if( rc==SQLITE_OK0 ){
243401 pLvl->nSeg = nTotal;
243402 for(iSeg=0; iSeg<nTotal; iSeg++){
243403 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
243404 if( i>=nData ){
243405 rc = FTS5_CORRUPT(11 | (1<<8));
243406 break;
243407 }
243408 assert( pSeg!=0 )((void) (0));
243409 i += fts5GetVarint32(&pData[i], pSeg->iSegid)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->iSegid
))
;
243410 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->pgnoFirst
))
;
243411 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->pgnoLast
))
;
243412 if( bStructureV2 ){
243413 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->iOrigin1);
243414 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->iOrigin2);
243415 i += fts5GetVarint32(&pData[i], pSeg->nPgTombstone)sqlite3Fts5GetVarint32(&pData[i],(u32*)&(pSeg->nPgTombstone
))
;
243416 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->nEntryTombstone);
243417 i += fts5GetVarintsqlite3Fts5GetVarint(&pData[i], &pSeg->nEntry);
243418 nOriginCntr = MAX(nOriginCntr, pSeg->iOrigin2)((nOriginCntr)>(pSeg->iOrigin2)?(nOriginCntr):(pSeg->
iOrigin2))
;
243419 }
243420 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
243421 rc = FTS5_CORRUPT(11 | (1<<8));
243422 break;
243423 }
243424 }
243425 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT(11 | (1<<8));
243426 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT(11 | (1<<8));
243427 }
243428 }
243429 if( nSegment!=0 && rc==SQLITE_OK0 ) rc = FTS5_CORRUPT(11 | (1<<8));
243430 if( bStructureV2 ){
243431 pRet->nOriginCntr = nOriginCntr+1;
243432 }
243433
243434 if( rc!=SQLITE_OK0 ){
243435 fts5StructureRelease(pRet);
243436 pRet = 0;
243437 }
243438 }
243439
243440 *ppOut = pRet;
243441 return rc;
243442}
243443
243444/*
243445** Add a level to the Fts5Structure.aLevel[] array of structure object
243446** (*ppStruct).
243447*/
243448static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
243449 fts5StructureMakeWritable(pRc, ppStruct);
243450 assert( (ppStruct!=0 && (*ppStruct)!=0) || (*pRc)!=SQLITE_OK )((void) (0));
243451 if( *pRc==SQLITE_OK0 ){
243452 Fts5Structure *pStruct = *ppStruct;
243453 int nLevel = pStruct->nLevel;
243454 sqlite3_int64 nByte = (
243455 sizeof(Fts5Structure) + /* Main structure */
243456 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
243457 );
243458
243459 pStruct = sqlite3_realloc64(pStruct, nByte);
243460 if( pStruct ){
243461 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
243462 pStruct->nLevel++;
243463 *ppStruct = pStruct;
243464 }else{
243465 *pRc = SQLITE_NOMEM7;
243466 }
243467 }
243468}
243469
243470/*
243471** Extend level iLvl so that there is room for at least nExtra more
243472** segments.
243473*/
243474static void fts5StructureExtendLevel(
243475 int *pRc,
243476 Fts5Structure *pStruct,
243477 int iLvl,
243478 int nExtra,
243479 int bInsert
243480){
243481 if( *pRc==SQLITE_OK0 ){
243482 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
243483 Fts5StructureSegment *aNew;
243484 sqlite3_int64 nByte;
243485
243486 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
243487 aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
243488 if( aNew ){
243489 if( bInsert==0 ){
243490 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
243491 }else{
243492 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
243493 memmove(&aNew[nExtra], aNew, nMove);
243494 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
243495 }
243496 pLvl->aSeg = aNew;
243497 }else{
243498 *pRc = SQLITE_NOMEM7;
243499 }
243500 }
243501}
243502
243503static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
243504 Fts5Structure *pRet = 0;
243505 Fts5Config *pConfig = p->pConfig;
243506 int iCookie; /* Configuration cookie */
243507 Fts5Data *pData;
243508
243509 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID10);
243510 if( p->rc==SQLITE_OK0 ){
243511 /* TODO: Do we need this if the leaf-index is appended? Probably... */
243512 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING20);
243513 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
243514 if( p->rc==SQLITE_OK0 && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
243515 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
243516 }
243517 fts5DataRelease(pData);
243518 if( p->rc!=SQLITE_OK0 ){
243519 fts5StructureRelease(pRet);
243520 pRet = 0;
243521 }
243522 }
243523
243524 return pRet;
243525}
243526
243527static i64 fts5IndexDataVersion(Fts5Index *p){
243528 i64 iVersion = 0;
243529
243530 if( p->rc==SQLITE_OK0 ){
243531 if( p->pDataVersion==0 ){
243532 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
243533 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
243534 );
243535 if( p->rc ) return 0;
243536 }
243537
243538 if( SQLITE_ROW100==sqlite3_step(p->pDataVersion) ){
243539 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
243540 }
243541 p->rc = sqlite3_reset(p->pDataVersion);
243542 }
243543
243544 return iVersion;
243545}
243546
243547/*
243548** Read, deserialize and return the structure record.
243549**
243550** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
243551** are over-allocated as described for function fts5StructureDecode()
243552** above.
243553**
243554** If an error occurs, NULL is returned and an error code left in the
243555** Fts5Index handle. If an error has already occurred when this function
243556** is called, it is a no-op.
243557*/
243558static Fts5Structure *fts5StructureRead(Fts5Index *p){
243559
243560 if( p->pStruct==0 ){
243561 p->iStructVersion = fts5IndexDataVersion(p);
243562 if( p->rc==SQLITE_OK0 ){
243563 p->pStruct = fts5StructureReadUncached(p);
243564 }
243565 }
243566
243567#if 0
243568 else{
243569 Fts5Structure *pTest = fts5StructureReadUncached(p);
243570 if( pTest ){
243571 int i, j;
243572 assert_nc( p->pStruct->nSegment==pTest->nSegment )((void) (0));
243573 assert_nc( p->pStruct->nLevel==pTest->nLevel )((void) (0));
243574 for(i=0; i<pTest->nLevel; i++){
243575 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge )((void) (0));
243576 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg )((void) (0));
243577 for(j=0; j<pTest->aLevel[i].nSeg; j++){
243578 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
243579 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
243580 assert_nc( p1->iSegid==p2->iSegid )((void) (0));
243581 assert_nc( p1->pgnoFirst==p2->pgnoFirst )((void) (0));
243582 assert_nc( p1->pgnoLast==p2->pgnoLast )((void) (0));
243583 }
243584 }
243585 fts5StructureRelease(pTest);
243586 }
243587 }
243588#endif
243589
243590 if( p->rc!=SQLITE_OK0 ) return 0;
243591 assert( p->iStructVersion!=0 )((void) (0));
243592 assert( p->pStruct!=0 )((void) (0));
243593 fts5StructureRef(p->pStruct);
243594 return p->pStruct;
243595}
243596
243597static void fts5StructureInvalidate(Fts5Index *p){
243598 if( p->pStruct ){
243599 fts5StructureRelease(p->pStruct);
243600 p->pStruct = 0;
243601 }
243602}
243603
243604/*
243605** Return the total number of segments in index structure pStruct. This
243606** function is only ever used as part of assert() conditions.
243607*/
243608#ifdef SQLITE_DEBUG
243609static int fts5StructureCountSegments(Fts5Structure *pStruct){
243610 int nSegment = 0; /* Total number of segments */
243611 if( pStruct ){
243612 int iLvl; /* Used to iterate through levels */
243613 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
243614 nSegment += pStruct->aLevel[iLvl].nSeg;
243615 }
243616 }
243617
243618 return nSegment;
243619}
243620#endif
243621
243622#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pBlob
, nBlob); (pBuf)->n += nBlob; }
{ \
243623 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) )((void) (0)); \
243624 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
243625 (pBuf)->n += nBlob; \
243626}
243627
243628#define fts5BufferSafeAppendVarint(pBuf, iVal){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iVal)); ((void) (0)); }
{ \
243629 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
243630 assert( (pBuf)->nSpace>=(pBuf)->n )((void) (0)); \
243631}
243632
243633
243634/*
243635** Serialize and store the "structure" record.
243636**
243637** If an error occurs, leave an error code in the Fts5Index object. If an
243638** error has already occurred, this function is a no-op.
243639*/
243640static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
243641 if( p->rc==SQLITE_OK0 ){
243642 Fts5Buffer buf; /* Buffer to serialize record into */
243643 int iLvl; /* Used to iterate through levels */
243644 int iCookie; /* Cookie value to store */
243645 int nHdr = (pStruct->nOriginCntr>0 ? (4+4+9+9+9) : (4+9+9));
243646
243647 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
243648 memset(&buf, 0, sizeof(Fts5Buffer));
243649
243650 /* Append the current configuration cookie */
243651 iCookie = p->pConfig->iCookie;
243652 if( iCookie<0 ) iCookie = 0;
243653
243654 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, nHdr) ){
243655 sqlite3Fts5Put32(buf.p, iCookie);
243656 buf.n = 4;
243657 if( pStruct->nOriginCntr>0 ){
243658 fts5BufferSafeAppendBlob(&buf, FTS5_STRUCTURE_V2, 4){ ((void) (0)); memcpy(&(&buf)->p[(&buf)->n
], "\xFF\x00\x00\x01", 4); (&buf)->n += 4; }
;
243659 }
243660 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], (pStruct->nLevel)); ((void) (0)); }
;
243661 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], (pStruct->nSegment)); ((void) (0)); }
;
243662 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter){ (&buf)->n += sqlite3Fts5PutVarint(&(&buf)->
p[(&buf)->n], ((i64)pStruct->nWriteCounter)); ((void
) (0)); }
;
243663 }
243664
243665 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
243666 int iSeg; /* Used to iterate through segments */
243667 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
243668 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pLvl
->nMerge)
;
243669 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pLvl
->nSeg)
;
243670 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
243671
243672 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
243673 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
243674 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iSegid)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->iSegid)
;
243675 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoFirst)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->pgnoFirst)
;
243676 fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoLast)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->pgnoLast)
;
243677 if( pStruct->nOriginCntr>0 ){
243678 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin1)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->iOrigin1)
;
243679 fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin2)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->iOrigin2)
;
243680 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nPgTombstone)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->nPgTombstone)
;
243681 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntryTombstone)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->nEntryTombstone)
;
243682 fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntry)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->nEntry)
;
243683 }
243684 }
243685 }
243686
243687 fts5DataWrite(p, FTS5_STRUCTURE_ROWID10, buf.p, buf.n);
243688 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
243689 }
243690}
243691
243692#if 0
243693static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
243694static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
243695 int rc = SQLITE_OK0;
243696 Fts5Buffer buf;
243697 memset(&buf, 0, sizeof(buf));
243698 fts5DebugStructure(&rc, &buf, pStruct);
243699 fprintf(stdoutstdout, "%s: %s\n", zCaption, buf.p);
243700 fflush(stdoutstdout);
243701 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
243702}
243703#else
243704# define fts5PrintStructure(x,y)
243705#endif
243706
243707static int fts5SegmentSize(Fts5StructureSegment *pSeg){
243708 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
243709}
243710
243711/*
243712** Return a copy of index structure pStruct. Except, promote as many
243713** segments as possible to level iPromote. If an OOM occurs, NULL is
243714** returned.
243715*/
243716static void fts5StructurePromoteTo(
243717 Fts5Index *p,
243718 int iPromote,
243719 int szPromote,
243720 Fts5Structure *pStruct
243721){
243722 int il, is;
243723 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
243724
243725 if( pOut->nMerge==0 ){
243726 for(il=iPromote+1; il<pStruct->nLevel; il++){
243727 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
243728 if( pLvl->nMerge ) return;
243729 for(is=pLvl->nSeg-1; is>=0; is--){
243730 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
243731 if( sz>szPromote ) return;
243732 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
243733 if( p->rc ) return;
243734 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
243735 pOut->nSeg++;
243736 pLvl->nSeg--;
243737 }
243738 }
243739 }
243740}
243741
243742/*
243743** A new segment has just been written to level iLvl of index structure
243744** pStruct. This function determines if any segments should be promoted
243745** as a result. Segments are promoted in two scenarios:
243746**
243747** a) If the segment just written is smaller than one or more segments
243748** within the previous populated level, it is promoted to the previous
243749** populated level.
243750**
243751** b) If the segment just written is larger than the newest segment on
243752** the next populated level, then that segment, and any other adjacent
243753** segments that are also smaller than the one just written, are
243754** promoted.
243755**
243756** If one or more segments are promoted, the structure object is updated
243757** to reflect this.
243758*/
243759static void fts5StructurePromote(
243760 Fts5Index *p, /* FTS5 backend object */
243761 int iLvl, /* Index level just updated */
243762 Fts5Structure *pStruct /* Index structure */
243763){
243764 if( p->rc==SQLITE_OK0 ){
243765 int iTst;
243766 int iPromote = -1;
243767 int szPromote = 0; /* Promote anything this size or smaller */
243768 Fts5StructureSegment *pSeg; /* Segment just written */
243769 int szSeg; /* Size of segment just written */
243770 int nSeg = pStruct->aLevel[iLvl].nSeg;
243771
243772 if( nSeg==0 ) return;
243773 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
243774 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
243775
243776 /* Check for condition (a) */
243777 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
243778 if( iTst>=0 ){
243779 int i;
243780 int szMax = 0;
243781 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
243782 assert( pTst->nMerge==0 )((void) (0));
243783 for(i=0; i<pTst->nSeg; i++){
243784 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
243785 if( sz>szMax ) szMax = sz;
243786 }
243787 if( szMax>=szSeg ){
243788 /* Condition (a) is true. Promote the newest segment on level
243789 ** iLvl to level iTst. */
243790 iPromote = iTst;
243791 szPromote = szMax;
243792 }
243793 }
243794
243795 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
243796 ** is a no-op if it is not. */
243797 if( iPromote<0 ){
243798 iPromote = iLvl;
243799 szPromote = szSeg;
243800 }
243801 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
243802 }
243803}
243804
243805
243806/*
243807** Advance the iterator passed as the only argument. If the end of the
243808** doclist-index page is reached, return non-zero.
243809*/
243810static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
243811 Fts5Data *pData = pLvl->pData;
243812
243813 if( pLvl->iOff==0 ){
243814 assert( pLvl->bEof==0 )((void) (0));
243815 pLvl->iOff = 1;
243816 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno)sqlite3Fts5GetVarint32(&pData->p[1],(u32*)&(pLvl->
iLeafPgno))
;
243817 pLvl->iOff += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
243818 pLvl->iFirstOff = pLvl->iOff;
243819 }else{
243820 int iOff;
243821 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
243822 if( pData->p[iOff] ) break;
243823 }
243824
243825 if( iOff<pData->nn ){
243826 u64 iVal;
243827 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
243828 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[iOff], &iVal);
243829 pLvl->iRowid += iVal;
243830 pLvl->iOff = iOff;
243831 }else{
243832 pLvl->bEof = 1;
243833 }
243834 }
243835
243836 return pLvl->bEof;
243837}
243838
243839/*
243840** Advance the iterator passed as the only argument.
243841*/
243842static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
243843 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
243844
243845 assert( iLvl<pIter->nLvl )((void) (0));
243846 if( fts5DlidxLvlNext(pLvl) ){
243847 if( (iLvl+1) < pIter->nLvl ){
243848 fts5DlidxIterNextR(p, pIter, iLvl+1);
243849 if( pLvl[1].bEof==0 ){
243850 fts5DataRelease(pLvl->pData);
243851 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
243852 pLvl->pData = fts5DataRead(p,
243853 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
)) )
243854 );
243855 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
243856 }
243857 }
243858 }
243859
243860 return pIter->aLvl[0].bEof;
243861}
243862static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
243863 return fts5DlidxIterNextR(p, pIter, 0);
243864}
243865
243866/*
243867** The iterator passed as the first argument has the following fields set
243868** as follows. This function sets up the rest of the iterator so that it
243869** points to the first rowid in the doclist-index.
243870**
243871** pData:
243872** pointer to doclist-index record,
243873**
243874** When this function is called pIter->iLeafPgno is the page number the
243875** doclist is associated with (the one featuring the term).
243876*/
243877static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
243878 int i;
243879 for(i=0; i<pIter->nLvl; i++){
243880 fts5DlidxLvlNext(&pIter->aLvl[i]);
243881 }
243882 return pIter->aLvl[0].bEof;
243883}
243884
243885
243886static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
243887 return p->rc!=SQLITE_OK0 || pIter->aLvl[0].bEof;
243888}
243889
243890static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
243891 int i;
243892
243893 /* Advance each level to the last entry on the last page */
243894 for(i=pIter->nLvl-1; p->rc==SQLITE_OK0 && i>=0; i--){
243895 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
243896 while( fts5DlidxLvlNext(pLvl)==0 );
243897 pLvl->bEof = 0;
243898
243899 if( i>0 ){
243900 Fts5DlidxLvl *pChild = &pLvl[-1];
243901 fts5DataRelease(pChild->pData);
243902 memset(pChild, 0, sizeof(Fts5DlidxLvl));
243903 pChild->pData = fts5DataRead(p,
243904 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
)) )
243905 );
243906 }
243907 }
243908}
243909
243910/*
243911** Move the iterator passed as the only argument to the previous entry.
243912*/
243913static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
243914 int iOff = pLvl->iOff;
243915
243916 assert( pLvl->bEof==0 )((void) (0));
243917 if( iOff<=pLvl->iFirstOff ){
243918 pLvl->bEof = 1;
243919 }else{
243920 u8 *a = pLvl->pData->p;
243921
243922 pLvl->iOff = 0;
243923 fts5DlidxLvlNext(pLvl);
243924 while( 1 ){
243925 int nZero = 0;
243926 int ii = pLvl->iOff;
243927 u64 delta = 0;
243928
243929 while( a[ii]==0 ){
243930 nZero++;
243931 ii++;
243932 }
243933 ii += sqlite3Fts5GetVarint(&a[ii], &delta);
243934
243935 if( ii>=iOff ) break;
243936 pLvl->iLeafPgno += nZero+1;
243937 pLvl->iRowid += delta;
243938 pLvl->iOff = ii;
243939 }
243940 }
243941
243942 return pLvl->bEof;
243943}
243944
243945static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
243946 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
243947
243948 assert( iLvl<pIter->nLvl )((void) (0));
243949 if( fts5DlidxLvlPrev(pLvl) ){
243950 if( (iLvl+1) < pIter->nLvl ){
243951 fts5DlidxIterPrevR(p, pIter, iLvl+1);
243952 if( pLvl[1].bEof==0 ){
243953 fts5DataRelease(pLvl->pData);
243954 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
243955 pLvl->pData = fts5DataRead(p,
243956 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
)) )
243957 );
243958 if( pLvl->pData ){
243959 while( fts5DlidxLvlNext(pLvl)==0 );
243960 pLvl->bEof = 0;
243961 }
243962 }
243963 }
243964 }
243965
243966 return pIter->aLvl[0].bEof;
243967}
243968static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
243969 return fts5DlidxIterPrevR(p, pIter, 0);
243970}
243971
243972/*
243973** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
243974*/
243975static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
243976 if( pIter ){
243977 int i;
243978 for(i=0; i<pIter->nLvl; i++){
243979 fts5DataRelease(pIter->aLvl[i].pData);
243980 }
243981 sqlite3_free(pIter);
243982 }
243983}
243984
243985static Fts5DlidxIter *fts5DlidxIterInit(
243986 Fts5Index *p, /* Fts5 Backend to iterate within */
243987 int bRev, /* True for ORDER BY ASC */
243988 int iSegid, /* Segment id */
243989 int iLeafPg /* Leaf page number to load dlidx for */
243990){
243991 Fts5DlidxIter *pIter = 0;
243992 int i;
243993 int bDone = 0;
243994
243995 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
243996 sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
243997 Fts5DlidxIter *pNew;
243998
243999 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
244000 if( pNew==0 ){
244001 p->rc = SQLITE_NOMEM7;
244002 }else{
244003 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(1) << (31
+ 5)) + ((i64)(i) << (31)) + ((i64)(iLeafPg)) )
;
244004 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
244005 pIter = pNew;
244006 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
244007 pLvl->pData = fts5DataRead(p, iRowid);
244008 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
244009 bDone = 1;
244010 }
244011 pIter->nLvl = i+1;
244012 }
244013 }
244014
244015 if( p->rc==SQLITE_OK0 ){
244016 pIter->iSegid = iSegid;
244017 if( bRev==0 ){
244018 fts5DlidxIterFirst(pIter);
244019 }else{
244020 fts5DlidxIterLast(p, pIter);
244021 }
244022 }
244023
244024 if( p->rc!=SQLITE_OK0 ){
244025 fts5DlidxIterFree(pIter);
244026 pIter = 0;
244027 }
244028
244029 return pIter;
244030}
244031
244032static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
244033 return pIter->aLvl[0].iRowid;
244034}
244035static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
244036 return pIter->aLvl[0].iLeafPgno;
244037}
244038
244039/*
244040** Load the next leaf page into the segment iterator.
244041*/
244042static void fts5SegIterNextPage(
244043 Fts5Index *p, /* FTS5 backend object */
244044 Fts5SegIter *pIter /* Iterator to advance to next page */
244045){
244046 Fts5Data *pLeaf;
244047 Fts5StructureSegment *pSeg = pIter->pSeg;
244048 fts5DataRelease(pIter->pLeaf);
244049 pIter->iLeafPgno++;
244050 if( pIter->pNextLeaf ){
244051 pIter->pLeaf = pIter->pNextLeaf;
244052 pIter->pNextLeaf = 0;
244053 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
244054 pIter->pLeaf = fts5LeafRead(p,
244055 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pIter->iLeafPgno
)) )
244056 );
244057 }else{
244058 pIter->pLeaf = 0;
244059 }
244060 pLeaf = pIter->pLeaf;
244061
244062 if( pLeaf ){
244063 pIter->iPgidxOff = pLeaf->szLeaf;
244064 if( fts5LeafIsTermless(pLeaf)((pLeaf)->szLeaf >= (pLeaf)->nn) ){
244065 pIter->iEndofDoclist = pLeaf->nn+1;
244066 }else{
244067 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],sqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&(pIter->iEndofDoclist))
244068 pIter->iEndofDoclistsqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&(pIter->iEndofDoclist))
244069 )sqlite3Fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
(u32*)&(pIter->iEndofDoclist))
;
244070 }
244071 }
244072}
244073
244074/*
244075** Argument p points to a buffer containing a varint to be interpreted as a
244076** position list size field. Read the varint and return the number of bytes
244077** read. Before returning, set *pnSz to the number of bytes in the position
244078** list, and *pbDel to true if the delete flag is set, or false otherwise.
244079*/
244080static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
244081 int nSz;
244082 int n = 0;
244083 fts5FastGetVarint32(p, n, nSz){ nSz = (p)[n++]; if( nSz & 0x80 ){ n--; n += sqlite3Fts5GetVarint32
(&(p)[n],(u32*)&(nSz)); } }
;
244084 assert_nc( nSz>=0 )((void) (0));
244085 *pnSz = nSz/2;
244086 *pbDel = nSz & 0x0001;
244087 return n;
244088}
244089
244090/*
244091** Fts5SegIter.iLeafOffset currently points to the first byte of a
244092** position-list size field. Read the value of the field and store it
244093** in the following variables:
244094**
244095** Fts5SegIter.nPos
244096** Fts5SegIter.bDel
244097**
244098** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
244099** position list content (if any).
244100*/
244101static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
244102 if( p->rc==SQLITE_OK0 ){
244103 int iOff = pIter->iLeafOffset; /* Offset to read at */
244104 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
244105 if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
244106 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf)((pIter->iEndofDoclist)<(pIter->pLeaf->szLeaf)?(pIter
->iEndofDoclist):(pIter->pLeaf->szLeaf))
;
244107 pIter->bDel = 0;
244108 pIter->nPos = 1;
244109 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
244110 pIter->bDel = 1;
244111 iOff++;
244112 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
244113 pIter->nPos = 1;
244114 iOff++;
244115 }else{
244116 pIter->nPos = 0;
244117 }
244118 }
244119 }else{
244120 int nSz;
244121 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz){ nSz = (pIter->pLeaf->p)[iOff++]; if( nSz & 0x80 )
{ iOff--; iOff += sqlite3Fts5GetVarint32(&(pIter->pLeaf
->p)[iOff],(u32*)&(nSz)); } }
;
244122 pIter->bDel = (nSz & 0x0001);
244123 pIter->nPos = nSz>>1;
244124 assert_nc( pIter->nPos>=0 )((void) (0));
244125 }
244126 pIter->iLeafOffset = iOff;
244127 }
244128}
244129
244130static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
244131 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
244132 i64 iOff = pIter->iLeafOffset;
244133
244134 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
244135 while( iOff>=pIter->pLeaf->szLeaf ){
244136 fts5SegIterNextPage(p, pIter);
244137 if( pIter->pLeaf==0 ){
244138 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
244139 return;
244140 }
244141 iOff = 4;
244142 a = pIter->pLeaf->p;
244143 }
244144 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
244145 pIter->iLeafOffset = iOff;
244146}
244147
244148/*
244149** Fts5SegIter.iLeafOffset currently points to the first byte of the
244150** "nSuffix" field of a term. Function parameter nKeep contains the value
244151** of the "nPrefix" field (if there was one - it is passed 0 if this is
244152** the first term in the segment).
244153**
244154** This function populates:
244155**
244156** Fts5SegIter.term
244157** Fts5SegIter.rowid
244158**
244159** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
244160** the first position list. The position list belonging to document
244161** (Fts5SegIter.iRowid).
244162*/
244163static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
244164 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
244165 i64 iOff = pIter->iLeafOffset; /* Offset to read at */
244166 int nNew; /* Bytes of new data */
244167
244168 iOff += fts5GetVarint32(&a[iOff], nNew)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nNew));
244169 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
244170 p->rc = FTS5_CORRUPT(11 | (1<<8));
244171 return;
244172 }
244173 pIter->term.n = nKeep;
244174 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&pIter->term
,nNew,&a[iOff])
;
244175 assert( pIter->term.n<=pIter->term.nSpace )((void) (0));
244176 iOff += nNew;
244177 pIter->iTermLeafOffset = iOff;
244178 pIter->iTermLeafPgno = pIter->iLeafPgno;
244179 pIter->iLeafOffset = iOff;
244180
244181 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
244182 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
244183 }else{
244184 int nExtra;
244185 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra)sqlite3Fts5GetVarint32(&a[pIter->iPgidxOff],(u32*)&
(nExtra))
;
244186 pIter->iEndofDoclist += nExtra;
244187 }
244188
244189 fts5SegIterLoadRowid(p, pIter);
244190}
244191
244192static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
244193static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
244194static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
244195
244196static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
244197 if( pIter->flags & FTS5_SEGITER_REVERSE0x02 ){
244198 pIter->xNext = fts5SegIterNext_Reverse;
244199 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
244200 pIter->xNext = fts5SegIterNext_None;
244201 }else{
244202 pIter->xNext = fts5SegIterNext;
244203 }
244204}
244205
244206/*
244207** Allocate a tombstone hash page array object (pIter->pTombArray) for
244208** the iterator passed as the second argument. If an OOM error occurs,
244209** leave an error in the Fts5Index object.
244210*/
244211static void fts5SegIterAllocTombstone(Fts5Index *p, Fts5SegIter *pIter){
244212 const int nTomb = pIter->pSeg->nPgTombstone;
244213 if( nTomb>0 ){
244214 int nByte = nTomb * sizeof(Fts5Data*) + sizeof(Fts5TombstoneArray);
244215 Fts5TombstoneArray *pNew;
244216 pNew = (Fts5TombstoneArray*)sqlite3Fts5MallocZero(&p->rc, nByte);
244217 if( pNew ){
244218 pNew->nTombstone = nTomb;
244219 pNew->nRef = 1;
244220 pIter->pTombArray = pNew;
244221 }
244222 }
244223}
244224
244225/*
244226** Initialize the iterator object pIter to iterate through the entries in
244227** segment pSeg. The iterator is left pointing to the first entry when
244228** this function returns.
244229**
244230** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
244231** an error has already occurred when this function is called, it is a no-op.
244232*/
244233static void fts5SegIterInit(
244234 Fts5Index *p, /* FTS index object */
244235 Fts5StructureSegment *pSeg, /* Description of segment */
244236 Fts5SegIter *pIter /* Object to populate */
244237){
244238 if( pSeg->pgnoFirst==0 ){
244239 /* This happens if the segment is being used as an input to an incremental
244240 ** merge and all data has already been "trimmed". See function
244241 ** fts5TrimSegments() for details. In this case leave the iterator empty.
244242 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
244243 ** at EOF already. */
244244 assert( pIter->pLeaf==0 )((void) (0));
244245 return;
244246 }
244247
244248 if( p->rc==SQLITE_OK0 ){
244249 memset(pIter, 0, sizeof(*pIter));
244250 fts5SegIterSetNext(p, pIter);
244251 pIter->pSeg = pSeg;
244252 pIter->iLeafPgno = pSeg->pgnoFirst-1;
244253 do {
244254 fts5SegIterNextPage(p, pIter);
244255 }while( p->rc==SQLITE_OK0 && pIter->pLeaf && pIter->pLeaf->nn==4 );
244256 }
244257
244258 if( p->rc==SQLITE_OK0 && pIter->pLeaf ){
244259 pIter->iLeafOffset = 4;
244260 assert( pIter->pLeaf!=0 )((void) (0));
244261 assert_nc( pIter->pLeaf->nn>4 )((void) (0));
244262 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 )((void) (0));
244263 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
244264 fts5SegIterLoadTerm(p, pIter, 0);
244265 fts5SegIterLoadNPos(p, pIter);
244266 fts5SegIterAllocTombstone(p, pIter);
244267 }
244268}
244269
244270/*
244271** This function is only ever called on iterators created by calls to
244272** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
244273**
244274** The iterator is in an unusual state when this function is called: the
244275** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
244276** the position-list size field for the first relevant rowid on the page.
244277** Fts5SegIter.rowid is set, but nPos and bDel are not.
244278**
244279** This function advances the iterator so that it points to the last
244280** relevant rowid on the page and, if necessary, initializes the
244281** aRowidOffset[] and iRowidOffset variables. At this point the iterator
244282** is in its regular state - Fts5SegIter.iLeafOffset points to the first
244283** byte of the position list content associated with said rowid.
244284*/
244285static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
244286 int eDetail = p->pConfig->eDetail;
244287 int n = pIter->pLeaf->szLeaf;
244288 int i = pIter->iLeafOffset;
244289 u8 *a = pIter->pLeaf->p;
244290 int iRowidOffset = 0;
244291
244292 if( n>pIter->iEndofDoclist ){
244293 n = pIter->iEndofDoclist;
244294 }
244295
244296 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
244297 while( 1 ){
244298 u64 iDelta = 0;
244299
244300 if( eDetail==FTS5_DETAIL_NONE1 ){
244301 /* todo */
244302 if( i<n && a[i]==0 ){
244303 i++;
244304 if( i<n && a[i]==0 ) i++;
244305 }
244306 }else{
244307 int nPos;
244308 int bDummy;
244309 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
244310 i += nPos;
244311 }
244312 if( i>=n ) break;
244313 i += fts5GetVarintsqlite3Fts5GetVarint(&a[i], &iDelta);
244314 pIter->iRowid += iDelta;
244315
244316 /* If necessary, grow the pIter->aRowidOffset[] array. */
244317 if( iRowidOffset>=pIter->nRowidOffset ){
244318 int nNew = pIter->nRowidOffset + 8;
244319 int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
244320 if( aNew==0 ){
244321 p->rc = SQLITE_NOMEM7;
244322 break;
244323 }
244324 pIter->aRowidOffset = aNew;
244325 pIter->nRowidOffset = nNew;
244326 }
244327
244328 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
244329 pIter->iLeafOffset = i;
244330 }
244331 pIter->iRowidOffset = iRowidOffset;
244332 fts5SegIterLoadNPos(p, pIter);
244333}
244334
244335/*
244336**
244337*/
244338static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
244339 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
244340 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
244341
244342 fts5DataRelease(pIter->pLeaf);
244343 pIter->pLeaf = 0;
244344 while( p->rc==SQLITE_OK0 && pIter->iLeafPgno>pIter->iTermLeafPgno ){
244345 Fts5Data *pNew;
244346 pIter->iLeafPgno--;
244347 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
244348 pIter->pSeg->iSegid, pIter->iLeafPgno( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
244349 )( ((i64)(pIter->pSeg->iSegid) << (31 +5 +1)) + ((
i64)(0) << (31 + 5)) + ((i64)(0) << (31)) + ((i64
)(pIter->iLeafPgno)) )
);
244350 if( pNew ){
244351 /* iTermLeafOffset may be equal to szLeaf if the term is the last
244352 ** thing on the page - i.e. the first rowid is on the following page.
244353 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
244354 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
244355 assert( pIter->pLeaf==0 )((void) (0));
244356 if( pIter->iTermLeafOffset<pNew->szLeaf ){
244357 pIter->pLeaf = pNew;
244358 pIter->iLeafOffset = pIter->iTermLeafOffset;
244359 }
244360 }else{
244361 int iRowidOff;
244362 iRowidOff = fts5LeafFirstRowidOff(pNew)(fts5GetU16((pNew)->p));
244363 if( iRowidOff ){
244364 if( iRowidOff>=pNew->szLeaf ){
244365 p->rc = FTS5_CORRUPT(11 | (1<<8));
244366 }else{
244367 pIter->pLeaf = pNew;
244368 pIter->iLeafOffset = iRowidOff;
244369 }
244370 }
244371 }
244372
244373 if( pIter->pLeaf ){
244374 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
244375 pIter->iLeafOffset += fts5GetVarintsqlite3Fts5GetVarint(a, (u64*)&pIter->iRowid);
244376 break;
244377 }else{
244378 fts5DataRelease(pNew);
244379 }
244380 }
244381 }
244382
244383 if( pIter->pLeaf ){
244384 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
244385 fts5SegIterReverseInitPage(p, pIter);
244386 }
244387}
244388
244389/*
244390** Return true if the iterator passed as the second argument currently
244391** points to a delete marker. A delete marker is an entry with a 0 byte
244392** position-list.
244393*/
244394static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
244395 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
244396 return (p->rc==SQLITE_OK0 && pSeg->pLeaf && pSeg->nPos==0);
244397}
244398
244399/*
244400** Advance iterator pIter to the next entry.
244401**
244402** This version of fts5SegIterNext() is only used by reverse iterators.
244403*/
244404static void fts5SegIterNext_Reverse(
244405 Fts5Index *p, /* FTS5 backend object */
244406 Fts5SegIter *pIter, /* Iterator to advance */
244407 int *pbUnused /* Unused */
244408){
244409 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
244410 assert( pIter->pNextLeaf==0 )((void) (0));
244411 UNUSED_PARAM(pbUnused)(void)(pbUnused);
244412
244413 if( pIter->iRowidOffset>0 ){
244414 u8 *a = pIter->pLeaf->p;
244415 int iOff;
244416 u64 iDelta;
244417
244418 pIter->iRowidOffset--;
244419 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
244420 fts5SegIterLoadNPos(p, pIter);
244421 iOff = pIter->iLeafOffset;
244422 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE1 ){
244423 iOff += pIter->nPos;
244424 }
244425 fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], &iDelta);
244426 pIter->iRowid -= iDelta;
244427 }else{
244428 fts5SegIterReverseNewPage(p, pIter);
244429 }
244430}
244431
244432/*
244433** Advance iterator pIter to the next entry.
244434**
244435** This version of fts5SegIterNext() is only used if detail=none and the
244436** iterator is not a reverse direction iterator.
244437*/
244438static void fts5SegIterNext_None(
244439 Fts5Index *p, /* FTS5 backend object */
244440 Fts5SegIter *pIter, /* Iterator to advance */
244441 int *pbNewTerm /* OUT: Set for new term */
244442){
244443 int iOff;
244444
244445 assert( p->rc==SQLITE_OK )((void) (0));
244446 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 )((void) (0));
244447 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
244448
244449 ASSERT_SZLEAF_OK(pIter->pLeaf)((void) (0));
244450 iOff = pIter->iLeafOffset;
244451
244452 /* Next entry is on the next page */
244453 while( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
244454 fts5SegIterNextPage(p, pIter);
244455 if( p->rc || pIter->pLeaf==0 ) return;
244456 pIter->iRowid = 0;
244457 iOff = 4;
244458 }
244459
244460 if( iOff<pIter->iEndofDoclist ){
244461 /* Next entry is on the current page */
244462 u64 iDelta;
244463 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
244464 pIter->iLeafOffset = iOff;
244465 pIter->iRowid += iDelta;
244466 }else if( (pIter->flags & FTS5_SEGITER_ONETERM0x01)==0 ){
244467 if( pIter->pSeg ){
244468 int nKeep = 0;
244469 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
244470 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep)sqlite3Fts5GetVarint32(&pIter->pLeaf->p[iOff],(u32*
)&(nKeep))
;
244471 }
244472 pIter->iLeafOffset = iOff;
244473 fts5SegIterLoadTerm(p, pIter, nKeep);
244474 }else{
244475 const u8 *pList = 0;
244476 const char *zTerm = 0;
244477 int nTerm = 0;
244478 int nList;
244479 sqlite3Fts5HashScanNext(p->pHash);
244480 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList);
244481 if( pList==0 ) goto next_none_eof;
244482 pIter->pLeaf->p = (u8*)pList;
244483 pIter->pLeaf->nn = nList;
244484 pIter->pLeaf->szLeaf = nList;
244485 pIter->iEndofDoclist = nList;
244486 sqlite3Fts5BufferSet(&p->rc,&pIter->term, nTerm, (u8*)zTerm);
244487 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pList, (u64*)&pIter->iRowid);
244488 }
244489
244490 if( pbNewTerm ) *pbNewTerm = 1;
244491 }else{
244492 goto next_none_eof;
244493 }
244494
244495 fts5SegIterLoadNPos(p, pIter);
244496
244497 return;
244498 next_none_eof:
244499 fts5DataRelease(pIter->pLeaf);
244500 pIter->pLeaf = 0;
244501}
244502
244503
244504/*
244505** Advance iterator pIter to the next entry.
244506**
244507** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
244508** is not considered an error if the iterator reaches EOF. If an error has
244509** already occurred when this function is called, it is a no-op.
244510*/
244511static void fts5SegIterNext(
244512 Fts5Index *p, /* FTS5 backend object */
244513 Fts5SegIter *pIter, /* Iterator to advance */
244514 int *pbNewTerm /* OUT: Set for new term */
244515){
244516 Fts5Data *pLeaf = pIter->pLeaf;
244517 int iOff;
244518 int bNewTerm = 0;
244519 int nKeep = 0;
244520 u8 *a;
244521 int n;
244522
244523 assert( pbNewTerm==0 || *pbNewTerm==0 )((void) (0));
244524 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
244525
244526 /* Search for the end of the position list within the current page. */
244527 a = pLeaf->p;
244528 n = pLeaf->szLeaf;
244529
244530 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
244531 iOff = pIter->iLeafOffset + pIter->nPos;
244532
244533 if( iOff<n ){
244534 /* The next entry is on the current page. */
244535 assert_nc( iOff<=pIter->iEndofDoclist )((void) (0));
244536 if( iOff>=pIter->iEndofDoclist ){
244537 bNewTerm = 1;
244538 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
244539 iOff += fts5GetVarint32(&a[iOff], nKeep)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nKeep));
244540 }
244541 }else{
244542 u64 iDelta;
244543 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
244544 pIter->iRowid += iDelta;
244545 assert_nc( iDelta>0 )((void) (0));
244546 }
244547 pIter->iLeafOffset = iOff;
244548
244549 }else if( pIter->pSeg==0 ){
244550 const u8 *pList = 0;
244551 const char *zTerm = 0;
244552 int nTerm = 0;
244553 int nList = 0;
244554 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm )((void) (0));
244555 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM0x01) ){
244556 sqlite3Fts5HashScanNext(p->pHash);
244557 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList);
244558 }
244559 if( pList==0 ){
244560 fts5DataRelease(pIter->pLeaf);
244561 pIter->pLeaf = 0;
244562 }else{
244563 pIter->pLeaf->p = (u8*)pList;
244564 pIter->pLeaf->nn = nList;
244565 pIter->pLeaf->szLeaf = nList;
244566 pIter->iEndofDoclist = nList+1;
244567 sqlite3Fts5BufferSet(&p->rc, &pIter->term, nTerm, (u8*)zTerm);
244568 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pList, (u64*)&pIter->iRowid);
244569 *pbNewTerm = 1;
244570 }
244571 }else{
244572 iOff = 0;
244573 /* Next entry is not on the current page */
244574 while( iOff==0 ){
244575 fts5SegIterNextPage(p, pIter);
244576 pLeaf = pIter->pLeaf;
244577 if( pLeaf==0 ) break;
244578 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
244579 if( (iOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p))) && iOff<pLeaf->szLeaf ){
244580 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
244581 pIter->iLeafOffset = iOff;
244582
244583 if( pLeaf->nn>pLeaf->szLeaf ){
244584 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(pIter->iEndofDoclist))
244585 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclistsqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(pIter->iEndofDoclist))
244586 )sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(pIter->iEndofDoclist))
;
244587 }
244588 }
244589 else if( pLeaf->nn>pLeaf->szLeaf ){
244590 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(iOff))
244591 &pLeaf->p[pLeaf->szLeaf], iOffsqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(iOff))
244592 )sqlite3Fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf],(u32
*)&(iOff))
;
244593 pIter->iLeafOffset = iOff;
244594 pIter->iEndofDoclist = iOff;
244595 bNewTerm = 1;
244596 }
244597 assert_nc( iOff<pLeaf->szLeaf )((void) (0));
244598 if( iOff>pLeaf->szLeaf ){
244599 p->rc = FTS5_CORRUPT(11 | (1<<8));
244600 return;
244601 }
244602 }
244603 }
244604
244605 /* Check if the iterator is now at EOF. If so, return early. */
244606 if( pIter->pLeaf ){
244607 if( bNewTerm ){
244608 if( pIter->flags & FTS5_SEGITER_ONETERM0x01 ){
244609 fts5DataRelease(pIter->pLeaf);
244610 pIter->pLeaf = 0;
244611 }else{
244612 fts5SegIterLoadTerm(p, pIter, nKeep);
244613 fts5SegIterLoadNPos(p, pIter);
244614 if( pbNewTerm ) *pbNewTerm = 1;
244615 }
244616 }else{
244617 /* The following could be done by calling fts5SegIterLoadNPos(). But
244618 ** this block is particularly performance critical, so equivalent
244619 ** code is inlined. */
244620 int nSz;
244621 assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn )((void) (0));
244622 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)); } }
;
244623 pIter->bDel = (nSz & 0x0001);
244624 pIter->nPos = nSz>>1;
244625 assert_nc( pIter->nPos>=0 )((void) (0));
244626 }
244627 }
244628}
244629
244630#define SWAPVAL(T, a, b){ T tmp; tmp=a; a=b; b=tmp; } { T tmp; tmp=a; a=b; b=tmp; }
244631
244632#define fts5IndexSkipVarint(a, iOff){ int iEnd = iOff+9; while( (a[iOff++] & 0x80) &&
iOff<iEnd ); }
{ \
244633 int iEnd = iOff+9; \
244634 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
244635}
244636
244637/*
244638** Iterator pIter currently points to the first rowid in a doclist. This
244639** function sets the iterator up so that iterates in reverse order through
244640** the doclist.
244641*/
244642static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
244643 Fts5DlidxIter *pDlidx = pIter->pDlidx;
244644 Fts5Data *pLast = 0;
244645 int pgnoLast = 0;
244646
244647 if( pDlidx && p->pConfig->iVersion==FTS5_CURRENT_VERSION4 ){
244648 int iSegid = pIter->pSeg->iSegid;
244649 pgnoLast = fts5DlidxIterPgno(pDlidx);
244650 pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgnoLast)) )
);
244651 }else{
244652 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
244653
244654 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
244655 ** position-list content for the current rowid. Back it up so that it
244656 ** points to the start of the position-list size field. */
244657 int iPoslist;
244658 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
244659 iPoslist = pIter->iTermLeafOffset;
244660 }else{
244661 iPoslist = 4;
244662 }
244663 fts5IndexSkipVarint(pLeaf->p, iPoslist){ int iEnd = iPoslist+9; while( (pLeaf->p[iPoslist++] &
0x80) && iPoslist<iEnd ); }
;
244664 pIter->iLeafOffset = iPoslist;
244665
244666 /* If this condition is true then the largest rowid for the current
244667 ** term may not be stored on the current page. So search forward to
244668 ** see where said rowid really is. */
244669 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
244670 int pgno;
244671 Fts5StructureSegment *pSeg = pIter->pSeg;
244672
244673 /* The last rowid in the doclist may not be on the current page. Search
244674 ** forward to find the page containing the last rowid. */
244675 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
244676 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
244677 Fts5Data *pNew = fts5LeafRead(p, iAbs);
244678 if( pNew ){
244679 int iRowid, bTermless;
244680 iRowid = fts5LeafFirstRowidOff(pNew)(fts5GetU16((pNew)->p));
244681 bTermless = fts5LeafIsTermless(pNew)((pNew)->szLeaf >= (pNew)->nn);
244682 if( iRowid ){
244683 SWAPVAL(Fts5Data*, pNew, pLast){ Fts5Data* tmp; tmp=pNew; pNew=pLast; pLast=tmp; };
244684 pgnoLast = pgno;
244685 }
244686 fts5DataRelease(pNew);
244687 if( bTermless==0 ) break;
244688 }
244689 }
244690 }
244691 }
244692
244693 /* If pLast is NULL at this point, then the last rowid for this doclist
244694 ** lies on the page currently indicated by the iterator. In this case
244695 ** pIter->iLeafOffset is already set to point to the position-list size
244696 ** field associated with the first relevant rowid on the page.
244697 **
244698 ** Or, if pLast is non-NULL, then it is the page that contains the last
244699 ** rowid. In this case configure the iterator so that it points to the
244700 ** first rowid on this page.
244701 */
244702 if( pLast ){
244703 int iOff;
244704 fts5DataRelease(pIter->pLeaf);
244705 pIter->pLeaf = pLast;
244706 pIter->iLeafPgno = pgnoLast;
244707 iOff = fts5LeafFirstRowidOff(pLast)(fts5GetU16((pLast)->p));
244708 if( iOff>pLast->szLeaf ){
244709 p->rc = FTS5_CORRUPT(11 | (1<<8));
244710 return;
244711 }
244712 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
244713 pIter->iLeafOffset = iOff;
244714
244715 if( fts5LeafIsTermless(pLast)((pLast)->szLeaf >= (pLast)->nn) ){
244716 pIter->iEndofDoclist = pLast->nn+1;
244717 }else{
244718 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
244719 }
244720 }
244721
244722 fts5SegIterReverseInitPage(p, pIter);
244723}
244724
244725/*
244726** Iterator pIter currently points to the first rowid of a doclist.
244727** There is a doclist-index associated with the final term on the current
244728** page. If the current term is the last term on the page, load the
244729** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
244730*/
244731static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
244732 int iSeg = pIter->pSeg->iSegid;
244733 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE0x02);
244734 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
244735
244736 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
244737 assert( pIter->pDlidx==0 )((void) (0));
244738
244739 /* Check if the current doclist ends on this page. If it does, return
244740 ** early without loading the doclist-index (as it belongs to a different
244741 ** term. */
244742 if( pIter->iTermLeafPgno==pIter->iLeafPgno
244743 && pIter->iEndofDoclist<pLeaf->szLeaf
244744 ){
244745 return;
244746 }
244747
244748 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
244749}
244750
244751/*
244752** The iterator object passed as the second argument currently contains
244753** no valid values except for the Fts5SegIter.pLeaf member variable. This
244754** function searches the leaf page for a term matching (pTerm/nTerm).
244755**
244756** If the specified term is found on the page, then the iterator is left
244757** pointing to it. If argument bGe is zero and the term is not found,
244758** the iterator is left pointing at EOF.
244759**
244760** If bGe is non-zero and the specified term is not found, then the
244761** iterator is left pointing to the smallest term in the segment that
244762** is larger than the specified term, even if this term is not on the
244763** current page.
244764*/
244765static void fts5LeafSeek(
244766 Fts5Index *p, /* Leave any error code here */
244767 int bGe, /* True for a >= search */
244768 Fts5SegIter *pIter, /* Iterator to seek */
244769 const u8 *pTerm, int nTerm /* Term to search for */
244770){
244771 u32 iOff;
244772 const u8 *a = pIter->pLeaf->p;
244773 u32 n = (u32)pIter->pLeaf->nn;
244774
244775 u32 nMatch = 0;
244776 u32 nKeep = 0;
244777 u32 nNew = 0;
244778 u32 iTermOff;
244779 u32 iPgidx; /* Current offset in pgidx */
244780 int bEndOfPage = 0;
244781
244782 assert( p->rc==SQLITE_OK )((void) (0));
244783
244784 iPgidx = (u32)pIter->pLeaf->szLeaf;
244785 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&(iTermOff));
244786 iOff = iTermOff;
244787 if( iOff>n ){
244788 p->rc = FTS5_CORRUPT(11 | (1<<8));
244789 return;
244790 }
244791
244792 while( 1 ){
244793
244794 /* Figure out how many new bytes are in this term */
244795 fts5FastGetVarint32(a, iOff, nNew){ nNew = (a)[iOff++]; if( nNew & 0x80 ){ iOff--; iOff += sqlite3Fts5GetVarint32
(&(a)[iOff],(u32*)&(nNew)); } }
;
244796 if( nKeep<nMatch ){
244797 goto search_failed;
244798 }
244799
244800 assert( nKeep>=nMatch )((void) (0));
244801 if( nKeep==nMatch ){
244802 u32 nCmp;
244803 u32 i;
244804 nCmp = (u32)MIN(nNew, nTerm-nMatch)((nNew)<(nTerm-nMatch)?(nNew):(nTerm-nMatch));
244805 for(i=0; i<nCmp; i++){
244806 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
244807 }
244808 nMatch += i;
244809
244810 if( (u32)nTerm==nMatch ){
244811 if( i==nNew ){
244812 goto search_success;
244813 }else{
244814 goto search_failed;
244815 }
244816 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
244817 goto search_failed;
244818 }
244819 }
244820
244821 if( iPgidx>=n ){
244822 bEndOfPage = 1;
244823 break;
244824 }
244825
244826 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&(nKeep));
244827 iTermOff += nKeep;
244828 iOff = iTermOff;
244829
244830 if( iOff>=n ){
244831 p->rc = FTS5_CORRUPT(11 | (1<<8));
244832 return;
244833 }
244834
244835 /* Read the nKeep field of the next term. */
244836 fts5FastGetVarint32(a, iOff, nKeep){ nKeep = (a)[iOff++]; if( nKeep & 0x80 ){ iOff--; iOff +=
sqlite3Fts5GetVarint32(&(a)[iOff],(u32*)&(nKeep)); }
}
;
244837 }
244838
244839 search_failed:
244840 if( bGe==0 ){
244841 fts5DataRelease(pIter->pLeaf);
244842 pIter->pLeaf = 0;
244843 return;
244844 }else if( bEndOfPage ){
244845 do {
244846 fts5SegIterNextPage(p, pIter);
244847 if( pIter->pLeaf==0 ) return;
244848 a = pIter->pLeaf->p;
244849 if( fts5LeafIsTermless(pIter->pLeaf)((pIter->pLeaf)->szLeaf >= (pIter->pLeaf)->nn)==0 ){
244850 iPgidx = (u32)pIter->pLeaf->szLeaf;
244851 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff)sqlite3Fts5GetVarint32(&pIter->pLeaf->p[iPgidx],(u32
*)&(iOff))
;
244852 if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){
244853 p->rc = FTS5_CORRUPT(11 | (1<<8));
244854 return;
244855 }else{
244856 nKeep = 0;
244857 iTermOff = iOff;
244858 n = (u32)pIter->pLeaf->nn;
244859 iOff += fts5GetVarint32(&a[iOff], nNew)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nNew));
244860 break;
244861 }
244862 }
244863 }while( 1 );
244864 }
244865
244866 search_success:
244867 if( (i64)iOff+nNew>n || nNew<1 ){
244868 p->rc = FTS5_CORRUPT(11 | (1<<8));
244869 return;
244870 }
244871 pIter->iLeafOffset = iOff + nNew;
244872 pIter->iTermLeafOffset = pIter->iLeafOffset;
244873 pIter->iTermLeafPgno = pIter->iLeafPgno;
244874
244875 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm)sqlite3Fts5BufferSet(&p->rc,&pIter->term,nKeep,
pTerm)
;
244876 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&pIter->term
,nNew,&a[iOff])
;
244877
244878 if( iPgidx>=n ){
244879 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
244880 }else{
244881 int nExtra;
244882 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra)sqlite3Fts5GetVarint32(&a[iPgidx],(u32*)&(nExtra));
244883 pIter->iEndofDoclist = iTermOff + nExtra;
244884 }
244885 pIter->iPgidxOff = iPgidx;
244886
244887 fts5SegIterLoadRowid(p, pIter);
244888 fts5SegIterLoadNPos(p, pIter);
244889}
244890
244891static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
244892 if( p->pIdxSelect==0 ){
244893 Fts5Config *pConfig = p->pConfig;
244894 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
244895 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
244896 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
244897 pConfig->zDb, pConfig->zName
244898 ));
244899 }
244900 return p->pIdxSelect;
244901}
244902
244903/*
244904** Initialize the object pIter to point to term pTerm/nTerm within segment
244905** pSeg. If there is no such term in the index, the iterator is set to EOF.
244906**
244907** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
244908** an error has already occurred when this function is called, it is a no-op.
244909*/
244910static void fts5SegIterSeekInit(
244911 Fts5Index *p, /* FTS5 backend */
244912 const u8 *pTerm, int nTerm, /* Term to seek to */
244913 int flags, /* Mask of FTS5INDEX_XXX flags */
244914 Fts5StructureSegment *pSeg, /* Description of segment */
244915 Fts5SegIter *pIter /* Object to populate */
244916){
244917 int iPg = 1;
244918 int bGe = (flags & FTS5INDEX_QUERY_SCAN0x0008);
244919 int bDlidx = 0; /* True if there is a doclist-index */
244920 sqlite3_stmt *pIdxSelect = 0;
244921
244922 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 )((void) (0));
244923 assert( pTerm && nTerm )((void) (0));
244924 memset(pIter, 0, sizeof(*pIter));
244925 pIter->pSeg = pSeg;
244926
244927 /* This block sets stack variable iPg to the leaf page number that may
244928 ** contain term (pTerm/nTerm), if it is present in the segment. */
244929 pIdxSelect = fts5IdxSelectStmt(p);
244930 if( p->rc ) return;
244931 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
244932 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC((sqlite3_destructor_type)0));
244933 if( SQLITE_ROW100==sqlite3_step(pIdxSelect) ){
244934 i64 val = sqlite3_column_int(pIdxSelect, 0);
244935 iPg = (int)(val>>1);
244936 bDlidx = (val & 0x0001);
244937 }
244938 p->rc = sqlite3_reset(pIdxSelect);
244939 sqlite3_bind_null(pIdxSelect, 2);
244940
244941 if( iPg<pSeg->pgnoFirst ){
244942 iPg = pSeg->pgnoFirst;
244943 bDlidx = 0;
244944 }
244945
244946 pIter->iLeafPgno = iPg - 1;
244947 fts5SegIterNextPage(p, pIter);
244948
244949 if( pIter->pLeaf ){
244950 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
244951 }
244952
244953 if( p->rc==SQLITE_OK0 && (bGe==0 || (flags & FTS5INDEX_QUERY_SCANONETERM0x0100)) ){
244954 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
244955 if( pIter->pLeaf ){
244956 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
244957 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
244958 }
244959 if( bDlidx ){
244960 fts5SegIterLoadDlidx(p, pIter);
244961 }
244962 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
244963 fts5SegIterReverse(p, pIter);
244964 }
244965 }
244966 }
244967
244968 fts5SegIterSetNext(p, pIter);
244969 if( 0==(flags & FTS5INDEX_QUERY_SCANONETERM0x0100) ){
244970 fts5SegIterAllocTombstone(p, pIter);
244971 }
244972
244973 /* Either:
244974 **
244975 ** 1) an error has occurred, or
244976 ** 2) the iterator points to EOF, or
244977 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
244978 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
244979 ** to an entry with a term greater than or equal to (pTerm/nTerm).
244980 */
244981 assert_nc( p->rc!=SQLITE_OK /* 1 */((void) (0))
244982 || pIter->pLeaf==0 /* 2 */((void) (0))
244983 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */((void) (0))
244984 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */((void) (0))
244985 )((void) (0));
244986}
244987
244988
244989/*
244990** SQL used by fts5SegIterNextInit() to find the page to open.
244991*/
244992static sqlite3_stmt *fts5IdxNextStmt(Fts5Index *p){
244993 if( p->pIdxNextSelect==0 ){
244994 Fts5Config *pConfig = p->pConfig;
244995 fts5IndexPrepareStmt(p, &p->pIdxNextSelect, sqlite3_mprintf(
244996 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
244997 "segid=? AND term>? ORDER BY term ASC LIMIT 1",
244998 pConfig->zDb, pConfig->zName
244999 ));
245000
245001 }
245002 return p->pIdxNextSelect;
245003}
245004
245005/*
245006** This is similar to fts5SegIterSeekInit(), except that it initializes
245007** the segment iterator to point to the first term following the page
245008** with pToken/nToken on it.
245009*/
245010static void fts5SegIterNextInit(
245011 Fts5Index *p,
245012 const char *pTerm, int nTerm,
245013 Fts5StructureSegment *pSeg, /* Description of segment */
245014 Fts5SegIter *pIter /* Object to populate */
245015){
245016 int iPg = -1; /* Page of segment to open */
245017 int bDlidx = 0;
245018 sqlite3_stmt *pSel = 0; /* SELECT to find iPg */
245019
245020 pSel = fts5IdxNextStmt(p);
245021 if( pSel ){
245022 assert( p->rc==SQLITE_OK )((void) (0));
245023 sqlite3_bind_int(pSel, 1, pSeg->iSegid);
245024 sqlite3_bind_blob(pSel, 2, pTerm, nTerm, SQLITE_STATIC((sqlite3_destructor_type)0));
245025
245026 if( sqlite3_step(pSel)==SQLITE_ROW100 ){
245027 i64 val = sqlite3_column_int64(pSel, 0);
245028 iPg = (int)(val>>1);
245029 bDlidx = (val & 0x0001);
245030 }
245031 p->rc = sqlite3_reset(pSel);
245032 sqlite3_bind_null(pSel, 2);
245033 if( p->rc ) return;
245034 }
245035
245036 memset(pIter, 0, sizeof(*pIter));
245037 pIter->pSeg = pSeg;
245038 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
245039 if( iPg>=0 ){
245040 pIter->iLeafPgno = iPg - 1;
245041 fts5SegIterNextPage(p, pIter);
245042 fts5SegIterSetNext(p, pIter);
245043 }
245044 if( pIter->pLeaf ){
245045 const u8 *a = pIter->pLeaf->p;
245046 int iTermOff = 0;
245047
245048 pIter->iPgidxOff = pIter->pLeaf->szLeaf;
245049 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[pIter->iPgidxOff],(u32*)&
(iTermOff))
;
245050 pIter->iLeafOffset = iTermOff;
245051 fts5SegIterLoadTerm(p, pIter, 0);
245052 fts5SegIterLoadNPos(p, pIter);
245053 if( bDlidx ) fts5SegIterLoadDlidx(p, pIter);
245054
245055 assert( p->rc!=SQLITE_OK ||((void) (0))
245056 fts5BufferCompareBlob(&pIter->term, (const u8*)pTerm, nTerm)>0((void) (0))
245057 )((void) (0));
245058 }
245059}
245060
245061/*
245062** Initialize the object pIter to point to term pTerm/nTerm within the
245063** in-memory hash table. If there is no such term in the hash-table, the
245064** iterator is set to EOF.
245065**
245066** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
245067** an error has already occurred when this function is called, it is a no-op.
245068*/
245069static void fts5SegIterHashInit(
245070 Fts5Index *p, /* FTS5 backend */
245071 const u8 *pTerm, int nTerm, /* Term to seek to */
245072 int flags, /* Mask of FTS5INDEX_XXX flags */
245073 Fts5SegIter *pIter /* Object to populate */
245074){
245075 int nList = 0;
245076 const u8 *z = 0;
245077 int n = 0;
245078 Fts5Data *pLeaf = 0;
245079
245080 assert( p->pHash )((void) (0));
245081 assert( p->rc==SQLITE_OK )((void) (0));
245082
245083 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN0x0008) ){
245084 const u8 *pList = 0;
245085
245086 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
245087 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &n, &pList, &nList);
245088 if( pList ){
245089 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
245090 if( pLeaf ){
245091 pLeaf->p = (u8*)pList;
245092 }
245093 }
245094
245095 /* The call to sqlite3Fts5HashScanInit() causes the hash table to
245096 ** fill the size field of all existing position lists. This means they
245097 ** can no longer be appended to. Since the only scenario in which they
245098 ** can be appended to is if the previous operation on this table was
245099 ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
245100 ** possibility altogether. */
245101 p->bDelete = 0;
245102 }else{
245103 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
245104 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
245105 );
245106 if( pLeaf ){
245107 pLeaf->p = (u8*)&pLeaf[1];
245108 }
245109 z = pTerm;
245110 n = nTerm;
245111 pIter->flags |= FTS5_SEGITER_ONETERM0x01;
245112 }
245113
245114 if( pLeaf ){
245115 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
245116 pLeaf->nn = pLeaf->szLeaf = nList;
245117 pIter->pLeaf = pLeaf;
245118 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
245119 pIter->iEndofDoclist = pLeaf->nn;
245120
245121 if( flags & FTS5INDEX_QUERY_DESC0x0002 ){
245122 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
245123 fts5SegIterReverseInitPage(p, pIter);
245124 }else{
245125 fts5SegIterLoadNPos(p, pIter);
245126 }
245127 }
245128
245129 fts5SegIterSetNext(p, pIter);
245130}
245131
245132/*
245133** Array ap[] contains n elements. Release each of these elements using
245134** fts5DataRelease(). Then free the array itself using sqlite3_free().
245135*/
245136static void fts5IndexFreeArray(Fts5Data **ap, int n){
245137 if( ap ){
245138 int ii;
245139 for(ii=0; ii<n; ii++){
245140 fts5DataRelease(ap[ii]);
245141 }
245142 sqlite3_free(ap);
245143 }
245144}
245145
245146/*
245147** Decrement the ref-count of the object passed as the only argument. If it
245148** reaches 0, free it and its contents.
245149*/
245150static void fts5TombstoneArrayDelete(Fts5TombstoneArray *p){
245151 if( p ){
245152 p->nRef--;
245153 if( p->nRef<=0 ){
245154 int ii;
245155 for(ii=0; ii<p->nTombstone; ii++){
245156 fts5DataRelease(p->apTombstone[ii]);
245157 }
245158 sqlite3_free(p);
245159 }
245160 }
245161}
245162
245163/*
245164** Zero the iterator passed as the only argument.
245165*/
245166static void fts5SegIterClear(Fts5SegIter *pIter){
245167 fts5BufferFree(&pIter->term)sqlite3Fts5BufferFree(&pIter->term);
245168 fts5DataRelease(pIter->pLeaf);
245169 fts5DataRelease(pIter->pNextLeaf);
245170 fts5TombstoneArrayDelete(pIter->pTombArray);
245171 fts5DlidxIterFree(pIter->pDlidx);
245172 sqlite3_free(pIter->aRowidOffset);
245173 memset(pIter, 0, sizeof(Fts5SegIter));
245174}
245175
245176#ifdef SQLITE_DEBUG
245177
245178/*
245179** This function is used as part of the big assert() procedure implemented by
245180** fts5AssertMultiIterSetup(). It ensures that the result currently stored
245181** in *pRes is the correct result of comparing the current positions of the
245182** two iterators.
245183*/
245184static void fts5AssertComparisonResult(
245185 Fts5Iter *pIter,
245186 Fts5SegIter *p1,
245187 Fts5SegIter *p2,
245188 Fts5CResult *pRes
245189){
245190 int i1 = p1 - pIter->aSeg;
245191 int i2 = p2 - pIter->aSeg;
245192
245193 if( p1->pLeaf || p2->pLeaf ){
245194 if( p1->pLeaf==0 ){
245195 assert( pRes->iFirst==i2 )((void) (0));
245196 }else if( p2->pLeaf==0 ){
245197 assert( pRes->iFirst==i1 )((void) (0));
245198 }else{
245199 int nMin = MIN(p1->term.n, p2->term.n)((p1->term.n)<(p2->term.n)?(p1->term.n):(p2->term
.n))
;
245200 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin)((nMin)<=0 ? 0 : memcmp((p1->term.p), (p2->term.p), (
nMin)))
;
245201 if( res==0 ) res = p1->term.n - p2->term.n;
245202
245203 if( res==0 ){
245204 assert( pRes->bTermEq==1 )((void) (0));
245205 assert( p1->iRowid!=p2->iRowid )((void) (0));
245206 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
245207 }else{
245208 assert( pRes->bTermEq==0 )((void) (0));
245209 }
245210
245211 if( res<0 ){
245212 assert( pRes->iFirst==i1 )((void) (0));
245213 }else{
245214 assert( pRes->iFirst==i2 )((void) (0));
245215 }
245216 }
245217 }
245218}
245219
245220/*
245221** This function is a no-op unless SQLITE_DEBUG is defined when this module
245222** is compiled. In that case, this function is essentially an assert()
245223** statement used to verify that the contents of the pIter->aFirst[] array
245224** are correct.
245225*/
245226static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
245227 if( p->rc==SQLITE_OK0 ){
245228 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
245229 int i;
245230
245231 assert( (pFirst->pLeaf==0)==pIter->base.bEof )((void) (0));
245232
245233 /* Check that pIter->iSwitchRowid is set correctly. */
245234 for(i=0; i<pIter->nSeg; i++){
245235 Fts5SegIter *p1 = &pIter->aSeg[i];
245236 assert( p1==pFirst((void) (0))
245237 || p1->pLeaf==0((void) (0))
245238 || fts5BufferCompare(&pFirst->term, &p1->term)((void) (0))
245239 || p1->iRowid==pIter->iSwitchRowid((void) (0))
245240 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev((void) (0))
245241 )((void) (0));
245242 }
245243
245244 for(i=0; i<pIter->nSeg; i+=2){
245245 Fts5SegIter *p1 = &pIter->aSeg[i];
245246 Fts5SegIter *p2 = &pIter->aSeg[i+1];
245247 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
245248 fts5AssertComparisonResult(pIter, p1, p2, pRes);
245249 }
245250
245251 for(i=1; i<(pIter->nSeg / 2); i+=2){
245252 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
245253 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
245254 Fts5CResult *pRes = &pIter->aFirst[i];
245255 fts5AssertComparisonResult(pIter, p1, p2, pRes);
245256 }
245257 }
245258}
245259#else
245260# define fts5AssertMultiIterSetup(x,y)
245261#endif
245262
245263/*
245264** Do the comparison necessary to populate pIter->aFirst[iOut].
245265**
245266** If the returned value is non-zero, then it is the index of an entry
245267** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
245268** to a key that is a duplicate of another, higher priority,
245269** segment-iterator in the pSeg->aSeg[] array.
245270*/
245271static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
245272 int i1; /* Index of left-hand Fts5SegIter */
245273 int i2; /* Index of right-hand Fts5SegIter */
245274 int iRes;
245275 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
245276 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
245277 Fts5CResult *pRes = &pIter->aFirst[iOut];
245278
245279 assert( iOut<pIter->nSeg && iOut>0 )((void) (0));
245280 assert( pIter->bRev==0 || pIter->bRev==1 )((void) (0));
245281
245282 if( iOut>=(pIter->nSeg/2) ){
245283 i1 = (iOut - pIter->nSeg/2) * 2;
245284 i2 = i1 + 1;
245285 }else{
245286 i1 = pIter->aFirst[iOut*2].iFirst;
245287 i2 = pIter->aFirst[iOut*2+1].iFirst;
245288 }
245289 p1 = &pIter->aSeg[i1];
245290 p2 = &pIter->aSeg[i2];
245291
245292 pRes->bTermEq = 0;
245293 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
245294 iRes = i2;
245295 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
245296 iRes = i1;
245297 }else{
245298 int res = fts5BufferCompare(&p1->term, &p2->term);
245299 if( res==0 ){
245300 assert_nc( i2>i1 )((void) (0));
245301 assert_nc( i2!=0 )((void) (0));
245302 pRes->bTermEq = 1;
245303 if( p1->iRowid==p2->iRowid ){
245304 return i2;
245305 }
245306 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
245307 }
245308 assert( res!=0 )((void) (0));
245309 if( res<0 ){
245310 iRes = i1;
245311 }else{
245312 iRes = i2;
245313 }
245314 }
245315
245316 pRes->iFirst = (u16)iRes;
245317 return 0;
245318}
245319
245320/*
245321** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
245322** It is an error if leaf iLeafPgno does not exist. Unless the db is
245323** a 'secure-delete' db, if it contains no rowids then this is also an error.
245324*/
245325static void fts5SegIterGotoPage(
245326 Fts5Index *p, /* FTS5 backend object */
245327 Fts5SegIter *pIter, /* Iterator to advance */
245328 int iLeafPgno
245329){
245330 assert( iLeafPgno>pIter->iLeafPgno )((void) (0));
245331
245332 if( iLeafPgno>pIter->pSeg->pgnoLast ){
245333 p->rc = FTS5_CORRUPT(11 | (1<<8));
245334 }else{
245335 fts5DataRelease(pIter->pNextLeaf);
245336 pIter->pNextLeaf = 0;
245337 pIter->iLeafPgno = iLeafPgno-1;
245338
245339 while( p->rc==SQLITE_OK0 ){
245340 int iOff;
245341 fts5SegIterNextPage(p, pIter);
245342 if( pIter->pLeaf==0 ) break;
245343 iOff = fts5LeafFirstRowidOff(pIter->pLeaf)(fts5GetU16((pIter->pLeaf)->p));
245344 if( iOff>0 ){
245345 u8 *a = pIter->pLeaf->p;
245346 int n = pIter->pLeaf->szLeaf;
245347 if( iOff<4 || iOff>=n ){
245348 p->rc = FTS5_CORRUPT(11 | (1<<8));
245349 }else{
245350 iOff += fts5GetVarintsqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
245351 pIter->iLeafOffset = iOff;
245352 fts5SegIterLoadNPos(p, pIter);
245353 }
245354 break;
245355 }
245356 }
245357 }
245358}
245359
245360/*
245361** Advance the iterator passed as the second argument until it is at or
245362** past rowid iFrom. Regardless of the value of iFrom, the iterator is
245363** always advanced at least once.
245364*/
245365static void fts5SegIterNextFrom(
245366 Fts5Index *p, /* FTS5 backend object */
245367 Fts5SegIter *pIter, /* Iterator to advance */
245368 i64 iMatch /* Advance iterator at least this far */
245369){
245370 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE0x02);
245371 Fts5DlidxIter *pDlidx = pIter->pDlidx;
245372 int iLeafPgno = pIter->iLeafPgno;
245373 int bMove = 1;
245374
245375 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
245376 assert( pIter->pDlidx )((void) (0));
245377 assert( pIter->pLeaf )((void) (0));
245378
245379 if( bRev==0 ){
245380 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
245381 iLeafPgno = fts5DlidxIterPgno(pDlidx);
245382 fts5DlidxIterNext(p, pDlidx);
245383 }
245384 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc )((void) (0));
245385 if( iLeafPgno>pIter->iLeafPgno ){
245386 fts5SegIterGotoPage(p, pIter, iLeafPgno);
245387 bMove = 0;
245388 }
245389 }else{
245390 assert( pIter->pNextLeaf==0 )((void) (0));
245391 assert( iMatch<pIter->iRowid )((void) (0));
245392 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
245393 fts5DlidxIterPrev(p, pDlidx);
245394 }
245395 iLeafPgno = fts5DlidxIterPgno(pDlidx);
245396
245397 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno )((void) (0));
245398
245399 if( iLeafPgno<pIter->iLeafPgno ){
245400 pIter->iLeafPgno = iLeafPgno+1;
245401 fts5SegIterReverseNewPage(p, pIter);
245402 bMove = 0;
245403 }
245404 }
245405
245406 do{
245407 if( bMove && p->rc==SQLITE_OK0 ) pIter->xNext(p, pIter, 0);
245408 if( pIter->pLeaf==0 ) break;
245409 if( bRev==0 && pIter->iRowid>=iMatch ) break;
245410 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
245411 bMove = 1;
245412 }while( p->rc==SQLITE_OK0 );
245413}
245414
245415/*
245416** Free the iterator object passed as the second argument.
245417*/
245418static void fts5MultiIterFree(Fts5Iter *pIter){
245419 if( pIter ){
245420 int i;
245421 for(i=0; i<pIter->nSeg; i++){
245422 fts5SegIterClear(&pIter->aSeg[i]);
245423 }
245424 fts5BufferFree(&pIter->poslist)sqlite3Fts5BufferFree(&pIter->poslist);
245425 sqlite3_free(pIter);
245426 }
245427}
245428
245429static void fts5MultiIterAdvanced(
245430 Fts5Index *p, /* FTS5 backend to iterate within */
245431 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
245432 int iChanged, /* Index of sub-iterator just advanced */
245433 int iMinset /* Minimum entry in aFirst[] to set */
245434){
245435 int i;
245436 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK0; i=i/2){
245437 int iEq;
245438 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
245439 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
245440 assert( p->rc==SQLITE_OK )((void) (0));
245441 pSeg->xNext(p, pSeg, 0);
245442 i = pIter->nSeg + iEq;
245443 }
245444 }
245445}
245446
245447/*
245448** Sub-iterator iChanged of iterator pIter has just been advanced. It still
245449** points to the same term though - just a different rowid. This function
245450** attempts to update the contents of the pIter->aFirst[] accordingly.
245451** If it does so successfully, 0 is returned. Otherwise 1.
245452**
245453** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
245454** on the iterator instead. That function does the same as this one, except
245455** that it deals with more complicated cases as well.
245456*/
245457static int fts5MultiIterAdvanceRowid(
245458 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
245459 int iChanged, /* Index of sub-iterator just advanced */
245460 Fts5SegIter **ppFirst
245461){
245462 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
245463
245464 if( pNew->iRowid==pIter->iSwitchRowid
245465 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
245466 ){
245467 int i;
245468 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
245469 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
245470 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
245471 Fts5CResult *pRes = &pIter->aFirst[i];
245472
245473 assert( pNew->pLeaf )((void) (0));
245474 assert( pRes->bTermEq==0 || pOther->pLeaf )((void) (0));
245475
245476 if( pRes->bTermEq ){
245477 if( pNew->iRowid==pOther->iRowid ){
245478 return 1;
245479 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
245480 pIter->iSwitchRowid = pOther->iRowid;
245481 pNew = pOther;
245482 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
245483 pIter->iSwitchRowid = pOther->iRowid;
245484 }
245485 }
245486 pRes->iFirst = (u16)(pNew - pIter->aSeg);
245487 if( i==1 ) break;
245488
245489 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
245490 }
245491 }
245492
245493 *ppFirst = pNew;
245494 return 0;
245495}
245496
245497/*
245498** Set the pIter->bEof variable based on the state of the sub-iterators.
245499*/
245500static void fts5MultiIterSetEof(Fts5Iter *pIter){
245501 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
245502 pIter->base.bEof = pSeg->pLeaf==0;
245503 pIter->iSwitchRowid = pSeg->iRowid;
245504}
245505
245506/*
245507** The argument to this macro must be an Fts5Data structure containing a
245508** tombstone hash page. This macro returns the key-size of the hash-page.
245509*/
245510#define TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8) (pPg->p[0]==4 ? 4 : 8)
245511
245512#define TOMBSTONE_NSLOT(pPg)((pPg->nn > 16) ? ((pPg->nn-8) / (pPg->p[0]==4 ? 4
: 8)) : 1)
\
245513 ((pPg->nn > 16) ? ((pPg->nn-8) / TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8)) : 1)
245514
245515/*
245516** Query a single tombstone hash table for rowid iRowid. Return true if
245517** it is found or false otherwise. The tombstone hash table is one of
245518** nHashTable tables.
245519*/
245520static int fts5IndexTombstoneQuery(
245521 Fts5Data *pHash, /* Hash table page to query */
245522 int nHashTable, /* Number of pages attached to segment */
245523 u64 iRowid /* Rowid to query hash for */
245524){
245525 const int szKey = TOMBSTONE_KEYSIZE(pHash)(pHash->p[0]==4 ? 4 : 8);
245526 const int nSlot = TOMBSTONE_NSLOT(pHash)((pHash->nn > 16) ? ((pHash->nn-8) / (pHash->p[0]
==4 ? 4 : 8)) : 1)
;
245527 int iSlot = (iRowid / nHashTable) % nSlot;
245528 int nCollide = nSlot;
245529
245530 if( iRowid==0 ){
245531 return pHash->p[1];
245532 }else if( szKey==4 ){
245533 u32 *aSlot = (u32*)&pHash->p[8];
245534 while( aSlot[iSlot] ){
245535 if( fts5GetU32((u8*)&aSlot[iSlot])==iRowid ) return 1;
245536 if( nCollide--==0 ) break;
245537 iSlot = (iSlot+1)%nSlot;
245538 }
245539 }else{
245540 u64 *aSlot = (u64*)&pHash->p[8];
245541 while( aSlot[iSlot] ){
245542 if( fts5GetU64((u8*)&aSlot[iSlot])==iRowid ) return 1;
245543 if( nCollide--==0 ) break;
245544 iSlot = (iSlot+1)%nSlot;
245545 }
245546 }
245547
245548 return 0;
245549}
245550
245551/*
245552** Return true if the iterator passed as the only argument points
245553** to an segment entry for which there is a tombstone. Return false
245554** if there is no tombstone or if the iterator is already at EOF.
245555*/
245556static int fts5MultiIterIsDeleted(Fts5Iter *pIter){
245557 int iFirst = pIter->aFirst[1].iFirst;
245558 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
245559 Fts5TombstoneArray *pArray = pSeg->pTombArray;
245560
245561 if( pSeg->pLeaf && pArray ){
245562 /* Figure out which page the rowid might be present on. */
245563 int iPg = ((u64)pSeg->iRowid) % pArray->nTombstone;
245564 assert( iPg>=0 )((void) (0));
245565
245566 /* If tombstone hash page iPg has not yet been loaded from the
245567 ** database, load it now. */
245568 if( pArray->apTombstone[iPg]==0 ){
245569 pArray->apTombstone[iPg] = fts5DataRead(pIter->pIndex,
245570 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)) )
245571 );
245572 if( pArray->apTombstone[iPg]==0 ) return 0;
245573 }
245574
245575 return fts5IndexTombstoneQuery(
245576 pArray->apTombstone[iPg],
245577 pArray->nTombstone,
245578 pSeg->iRowid
245579 );
245580 }
245581
245582 return 0;
245583}
245584
245585/*
245586** Move the iterator to the next entry.
245587**
245588** If an error occurs, an error code is left in Fts5Index.rc. It is not
245589** considered an error if the iterator reaches EOF, or if it is already at
245590** EOF when this function is called.
245591*/
245592static void fts5MultiIterNext(
245593 Fts5Index *p,
245594 Fts5Iter *pIter,
245595 int bFrom, /* True if argument iFrom is valid */
245596 i64 iFrom /* Advance at least as far as this */
245597){
245598 int bUseFrom = bFrom;
245599 assert( pIter->base.bEof==0 )((void) (0));
245600 while( p->rc==SQLITE_OK0 ){
245601 int iFirst = pIter->aFirst[1].iFirst;
245602 int bNewTerm = 0;
245603 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
245604 assert( p->rc==SQLITE_OK )((void) (0));
245605 if( bUseFrom && pSeg->pDlidx ){
245606 fts5SegIterNextFrom(p, pSeg, iFrom);
245607 }else{
245608 pSeg->xNext(p, pSeg, &bNewTerm);
245609 }
245610
245611 if( pSeg->pLeaf==0 || bNewTerm
245612 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
245613 ){
245614 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
245615 fts5MultiIterSetEof(pIter);
245616 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
245617 if( pSeg->pLeaf==0 ) return;
245618 }
245619
245620 fts5AssertMultiIterSetup(p, pIter);
245621 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf )((void) (0));
245622 if( (pIter->bSkipEmpty==0 || pSeg->nPos)
245623 && 0==fts5MultiIterIsDeleted(pIter)
245624 ){
245625 pIter->xSetOutputs(pIter, pSeg);
245626 return;
245627 }
245628 bUseFrom = 0;
245629 }
245630}
245631
245632static void fts5MultiIterNext2(
245633 Fts5Index *p,
245634 Fts5Iter *pIter,
245635 int *pbNewTerm /* OUT: True if *might* be new term */
245636){
245637 assert( pIter->bSkipEmpty )((void) (0));
245638 if( p->rc==SQLITE_OK0 ){
245639 *pbNewTerm = 0;
245640 do{
245641 int iFirst = pIter->aFirst[1].iFirst;
245642 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
245643 int bNewTerm = 0;
245644
245645 assert( p->rc==SQLITE_OK )((void) (0));
245646 pSeg->xNext(p, pSeg, &bNewTerm);
245647 if( pSeg->pLeaf==0 || bNewTerm
245648 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
245649 ){
245650 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
245651 fts5MultiIterSetEof(pIter);
245652 *pbNewTerm = 1;
245653 }
245654 fts5AssertMultiIterSetup(p, pIter);
245655
245656 }while( (fts5MultiIterIsEmpty(p, pIter) || fts5MultiIterIsDeleted(pIter))
245657 && (p->rc==SQLITE_OK0)
245658 );
245659 }
245660}
245661
245662static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
245663 UNUSED_PARAM2(pUnused1, pUnused2)(void)(pUnused1), (void)(pUnused2);
245664}
245665
245666static Fts5Iter *fts5MultiIterAlloc(
245667 Fts5Index *p, /* FTS5 backend to iterate within */
245668 int nSeg
245669){
245670 Fts5Iter *pNew;
245671 i64 nSlot; /* Power of two >= nSeg */
245672
245673 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
245674 pNew = fts5IdxMalloc(p,
245675 sizeof(Fts5Iter) + /* pNew */
245676 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
245677 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
245678 );
245679 if( pNew ){
245680 pNew->nSeg = nSlot;
245681 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
245682 pNew->pIndex = p;
245683 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
245684 }
245685 return pNew;
245686}
245687
245688static void fts5PoslistCallback(
245689 Fts5Index *pUnused,
245690 void *pContext,
245691 const u8 *pChunk, int nChunk
245692){
245693 UNUSED_PARAM(pUnused)(void)(pUnused);
245694 assert_nc( nChunk>=0 )((void) (0));
245695 if( nChunk>0 ){
245696 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk){ ((void) (0)); memcpy(&((Fts5Buffer*)pContext)->p[((Fts5Buffer
*)pContext)->n], pChunk, nChunk); ((Fts5Buffer*)pContext)->
n += nChunk; }
;
245697 }
245698}
245699
245700typedef struct PoslistCallbackCtx PoslistCallbackCtx;
245701struct PoslistCallbackCtx {
245702 Fts5Buffer *pBuf; /* Append to this buffer */
245703 Fts5Colset *pColset; /* Restrict matches to this column */
245704 int eState; /* See above */
245705};
245706
245707typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
245708struct PoslistOffsetsCtx {
245709 Fts5Buffer *pBuf; /* Append to this buffer */
245710 Fts5Colset *pColset; /* Restrict matches to this column */
245711 int iRead;
245712 int iWrite;
245713};
245714
245715/*
245716** TODO: Make this more efficient!
245717*/
245718static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
245719 int i;
245720 for(i=0; i<pColset->nCol; i++){
245721 if( pColset->aiCol[i]==iCol ) return 1;
245722 }
245723 return 0;
245724}
245725
245726static void fts5PoslistOffsetsCallback(
245727 Fts5Index *pUnused,
245728 void *pContext,
245729 const u8 *pChunk, int nChunk
245730){
245731 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
245732 UNUSED_PARAM(pUnused)(void)(pUnused);
245733 assert_nc( nChunk>=0 )((void) (0));
245734 if( nChunk>0 ){
245735 int i = 0;
245736 while( i<nChunk ){
245737 int iVal;
245738 i += fts5GetVarint32(&pChunk[i], iVal)sqlite3Fts5GetVarint32(&pChunk[i],(u32*)&(iVal));
245739 iVal += pCtx->iRead - 2;
245740 pCtx->iRead = iVal;
245741 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
245742 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite){ (pCtx->pBuf)->n += sqlite3Fts5PutVarint(&(pCtx->
pBuf)->p[(pCtx->pBuf)->n], (iVal + 2 - pCtx->iWrite
)); ((void) (0)); }
;
245743 pCtx->iWrite = iVal;
245744 }
245745 }
245746 }
245747}
245748
245749static void fts5PoslistFilterCallback(
245750 Fts5Index *pUnused,
245751 void *pContext,
245752 const u8 *pChunk, int nChunk
245753){
245754 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
245755 UNUSED_PARAM(pUnused)(void)(pUnused);
245756 assert_nc( nChunk>=0 )((void) (0));
245757 if( nChunk>0 ){
245758 /* Search through to find the first varint with value 1. This is the
245759 ** start of the next columns hits. */
245760 int i = 0;
245761 int iStart = 0;
245762
245763 if( pCtx->eState==2 ){
245764 int iCol;
245765 fts5FastGetVarint32(pChunk, i, iCol){ iCol = (pChunk)[i++]; if( iCol & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(pChunk)[i],(u32*)&(iCol)); } }
;
245766 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
245767 pCtx->eState = 1;
245768 fts5BufferSafeAppendVarint(pCtx->pBuf, 1){ (pCtx->pBuf)->n += sqlite3Fts5PutVarint(&(pCtx->
pBuf)->p[(pCtx->pBuf)->n], (1)); ((void) (0)); }
;
245769 }else{
245770 pCtx->eState = 0;
245771 }
245772 }
245773
245774 do {
245775 while( i<nChunk && pChunk[i]!=0x01 ){
245776 while( pChunk[i] & 0x80 ) i++;
245777 i++;
245778 }
245779 if( pCtx->eState ){
245780 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; }
;
245781 }
245782 if( i<nChunk ){
245783 int iCol;
245784 iStart = i;
245785 i++;
245786 if( i>=nChunk ){
245787 pCtx->eState = 2;
245788 }else{
245789 fts5FastGetVarint32(pChunk, i, iCol){ iCol = (pChunk)[i++]; if( iCol & 0x80 ){ i--; i += sqlite3Fts5GetVarint32
(&(pChunk)[i],(u32*)&(iCol)); } }
;
245790 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
245791 if( pCtx->eState ){
245792 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; }
;
245793 iStart = i;
245794 }
245795 }
245796 }
245797 }while( i<nChunk );
245798 }
245799}
245800
245801static void fts5ChunkIterate(
245802 Fts5Index *p, /* Index object */
245803 Fts5SegIter *pSeg, /* Poslist of this iterator */
245804 void *pCtx, /* Context pointer for xChunk callback */
245805 void (*xChunk)(Fts5Index*, void*, const u8*, int)
245806){
245807 int nRem = pSeg->nPos; /* Number of bytes still to come */
245808 Fts5Data *pData = 0;
245809 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
245810 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)((nRem)<(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)
?(nRem):(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset))
;
245811 int pgno = pSeg->iLeafPgno;
245812 int pgnoSave = 0;
245813
245814 /* This function does not work with detail=none databases. */
245815 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
245816
245817 if( (pSeg->flags & FTS5_SEGITER_REVERSE0x02)==0 ){
245818 pgnoSave = pgno+1;
245819 }
245820
245821 while( 1 ){
245822 xChunk(p, pCtx, pChunk, nChunk);
245823 nRem -= nChunk;
245824 fts5DataRelease(pData);
245825 if( nRem<=0 ){
245826 break;
245827 }else if( pSeg->pSeg==0 ){
245828 p->rc = FTS5_CORRUPT(11 | (1<<8));
245829 return;
245830 }else{
245831 pgno++;
245832 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
)) )
);
245833 if( pData==0 ) break;
245834 pChunk = &pData->p[4];
245835 nChunk = MIN(nRem, pData->szLeaf - 4)((nRem)<(pData->szLeaf - 4)?(nRem):(pData->szLeaf - 4
))
;
245836 if( pgno==pgnoSave ){
245837 assert( pSeg->pNextLeaf==0 )((void) (0));
245838 pSeg->pNextLeaf = pData;
245839 pData = 0;
245840 }
245841 }
245842 }
245843}
245844
245845/*
245846** Iterator pIter currently points to a valid entry (not EOF). This
245847** function appends the position list data for the current entry to
245848** buffer pBuf. It does not make a copy of the position-list size
245849** field.
245850*/
245851static void fts5SegiterPoslist(
245852 Fts5Index *p,
245853 Fts5SegIter *pSeg,
245854 Fts5Colset *pColset,
245855 Fts5Buffer *pBuf
245856){
245857 assert( pBuf!=0 )((void) (0));
245858 assert( pSeg!=0 )((void) (0));
245859 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) )
){
245860 assert( pBuf->p!=0 )((void) (0));
245861 assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING )((void) (0));
245862 memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING8);
245863 if( pColset==0 ){
245864 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
245865 }else{
245866 if( p->pConfig->eDetail==FTS5_DETAIL_FULL0 ){
245867 PoslistCallbackCtx sCtx;
245868 sCtx.pBuf = pBuf;
245869 sCtx.pColset = pColset;
245870 sCtx.eState = fts5IndexColsetTest(pColset, 0);
245871 assert( sCtx.eState==0 || sCtx.eState==1 )((void) (0));
245872 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
245873 }else{
245874 PoslistOffsetsCtx sCtx;
245875 memset(&sCtx, 0, sizeof(sCtx));
245876 sCtx.pBuf = pBuf;
245877 sCtx.pColset = pColset;
245878 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
245879 }
245880 }
245881 }
245882}
245883
245884/*
245885** Parameter pPos points to a buffer containing a position list, size nPos.
245886** This function filters it according to pColset (which must be non-NULL)
245887** and sets pIter->base.pData/nData to point to the new position list.
245888** If memory is required for the new position list, use buffer pIter->poslist.
245889** Or, if the new position list is a contiguous subset of the input, set
245890** pIter->base.pData/nData to point directly to it.
245891**
245892** This function is a no-op if *pRc is other than SQLITE_OK when it is
245893** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
245894** before returning.
245895*/
245896static void fts5IndexExtractColset(
245897 int *pRc,
245898 Fts5Colset *pColset, /* Colset to filter on */
245899 const u8 *pPos, int nPos, /* Position list */
245900 Fts5Iter *pIter
245901){
245902 if( *pRc==SQLITE_OK0 ){
245903 const u8 *p = pPos;
245904 const u8 *aCopy = p;
245905 const u8 *pEnd = &p[nPos]; /* One byte past end of position list */
245906 int i = 0;
245907 int iCurrent = 0;
245908
245909 if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, &pIter->poslist, nPos) ){
245910 return;
245911 }
245912
245913 while( 1 ){
245914 while( pColset->aiCol[i]<iCurrent ){
245915 i++;
245916 if( i==pColset->nCol ){
245917 pIter->base.pData = pIter->poslist.p;
245918 pIter->base.nData = pIter->poslist.n;
245919 return;
245920 }
245921 }
245922
245923 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
245924 ** not part of a varint */
245925 while( p<pEnd && *p!=0x01 ){
245926 while( *p++ & 0x80 );
245927 }
245928
245929 if( pColset->aiCol[i]==iCurrent ){
245930 if( pColset->nCol==1 ){
245931 pIter->base.pData = aCopy;
245932 pIter->base.nData = p-aCopy;
245933 return;
245934 }
245935 fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy){ ((void) (0)); memcpy(&(&pIter->poslist)->p[(&
pIter->poslist)->n], aCopy, p-aCopy); (&pIter->poslist
)->n += p-aCopy; }
;
245936 }
245937 if( p>=pEnd ){
245938 pIter->base.pData = pIter->poslist.p;
245939 pIter->base.nData = pIter->poslist.n;
245940 return;
245941 }
245942 aCopy = p++;
245943 iCurrent = *p++;
245944 if( iCurrent & 0x80 ){
245945 p--;
245946 p += fts5GetVarint32(p, iCurrent)sqlite3Fts5GetVarint32(p,(u32*)&(iCurrent));
245947 }
245948 }
245949 }
245950
245951}
245952
245953/*
245954** xSetOutputs callback used by detail=none tables.
245955*/
245956static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
245957 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
245958 pIter->base.iRowid = pSeg->iRowid;
245959 pIter->base.nData = pSeg->nPos;
245960}
245961
245962/*
245963** xSetOutputs callback used by detail=full and detail=col tables when no
245964** column filters are specified.
245965*/
245966static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
245967 pIter->base.iRowid = pSeg->iRowid;
245968 pIter->base.nData = pSeg->nPos;
245969
245970 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
245971 assert( pIter->pColset==0 )((void) (0));
245972
245973 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
245974 /* All data is stored on the current page. Populate the output
245975 ** variables to point into the body of the page object. */
245976 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
245977 }else{
245978 /* The data is distributed over two or more pages. Copy it into the
245979 ** Fts5Iter.poslist buffer and then set the output pointer to point
245980 ** to this buffer. */
245981 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
245982 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
245983 pIter->base.pData = pIter->poslist.p;
245984 }
245985}
245986
245987/*
245988** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
245989** against no columns at all).
245990*/
245991static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
245992 UNUSED_PARAM(pSeg)(void)(pSeg);
245993 pIter->base.nData = 0;
245994}
245995
245996/*
245997** xSetOutputs callback used by detail=col when there is a column filter
245998** and there are 100 or more columns. Also called as a fallback from
245999** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
246000*/
246001static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
246002 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
246003 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
246004 pIter->base.iRowid = pSeg->iRowid;
246005 pIter->base.pData = pIter->poslist.p;
246006 pIter->base.nData = pIter->poslist.n;
246007}
246008
246009/*
246010** xSetOutputs callback used when:
246011**
246012** * detail=col,
246013** * there is a column filter, and
246014** * the table contains 100 or fewer columns.
246015**
246016** The last point is to ensure all column numbers are stored as
246017** single-byte varints.
246018*/
246019static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
246020
246021 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
246022 assert( pIter->pColset )((void) (0));
246023
246024 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
246025 fts5IterSetOutputs_Col(pIter, pSeg);
246026 }else{
246027 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
246028 u8 *pEnd = (u8*)&a[pSeg->nPos];
246029 int iPrev = 0;
246030 int *aiCol = pIter->pColset->aiCol;
246031 int *aiColEnd = &aiCol[pIter->pColset->nCol];
246032
246033 u8 *aOut = pIter->poslist.p;
246034 int iPrevOut = 0;
246035
246036 pIter->base.iRowid = pSeg->iRowid;
246037
246038 while( a<pEnd ){
246039 iPrev += (int)a++[0] - 2;
246040 while( *aiCol<iPrev ){
246041 aiCol++;
246042 if( aiCol==aiColEnd ) goto setoutputs_col_out;
246043 }
246044 if( *aiCol==iPrev ){
246045 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
246046 iPrevOut = iPrev;
246047 }
246048 }
246049
246050setoutputs_col_out:
246051 pIter->base.pData = pIter->poslist.p;
246052 pIter->base.nData = aOut - pIter->poslist.p;
246053 }
246054}
246055
246056/*
246057** xSetOutputs callback used by detail=full when there is a column filter.
246058*/
246059static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
246060 Fts5Colset *pColset = pIter->pColset;
246061 pIter->base.iRowid = pSeg->iRowid;
246062
246063 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL )((void) (0));
246064 assert( pColset )((void) (0));
246065
246066 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
246067 /* All data is stored on the current page. Populate the output
246068 ** variables to point into the body of the page object. */
246069 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
246070 int *pRc = &pIter->pIndex->rc;
246071 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
246072 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, pIter);
246073 }else{
246074 /* The data is distributed over two or more pages. Copy it into the
246075 ** Fts5Iter.poslist buffer and then set the output pointer to point
246076 ** to this buffer. */
246077 fts5BufferZero(&pIter->poslist)sqlite3Fts5BufferZero(&pIter->poslist);
246078 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
246079 pIter->base.pData = pIter->poslist.p;
246080 pIter->base.nData = pIter->poslist.n;
246081 }
246082}
246083
246084static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
246085 assert( pIter!=0 || (*pRc)!=SQLITE_OK )((void) (0));
246086 if( *pRc==SQLITE_OK0 ){
246087 Fts5Config *pConfig = pIter->pIndex->pConfig;
246088 if( pConfig->eDetail==FTS5_DETAIL_NONE1 ){
246089 pIter->xSetOutputs = fts5IterSetOutputs_None;
246090 }
246091
246092 else if( pIter->pColset==0 ){
246093 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
246094 }
246095
246096 else if( pIter->pColset->nCol==0 ){
246097 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
246098 }
246099
246100 else if( pConfig->eDetail==FTS5_DETAIL_FULL0 ){
246101 pIter->xSetOutputs = fts5IterSetOutputs_Full;
246102 }
246103
246104 else{
246105 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
246106 if( pConfig->nCol<=100 ){
246107 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
246108 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
246109 }else{
246110 pIter->xSetOutputs = fts5IterSetOutputs_Col;
246111 }
246112 }
246113 }
246114}
246115
246116/*
246117** All the component segment-iterators of pIter have been set up. This
246118** functions finishes setup for iterator pIter itself.
246119*/
246120static void fts5MultiIterFinishSetup(Fts5Index *p, Fts5Iter *pIter){
246121 int iIter;
246122 for(iIter=pIter->nSeg-1; iIter>0; iIter--){
246123 int iEq;
246124 if( (iEq = fts5MultiIterDoCompare(pIter, iIter)) ){
246125 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
246126 if( p->rc==SQLITE_OK0 ) pSeg->xNext(p, pSeg, 0);
246127 fts5MultiIterAdvanced(p, pIter, iEq, iIter);
246128 }
246129 }
246130 fts5MultiIterSetEof(pIter);
246131 fts5AssertMultiIterSetup(p, pIter);
246132
246133 if( (pIter->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter))
246134 || fts5MultiIterIsDeleted(pIter)
246135 ){
246136 fts5MultiIterNext(p, pIter, 0, 0);
246137 }else if( pIter->base.bEof==0 ){
246138 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
246139 pIter->xSetOutputs(pIter, pSeg);
246140 }
246141}
246142
246143/*
246144** Allocate a new Fts5Iter object.
246145**
246146** The new object will be used to iterate through data in structure pStruct.
246147** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
246148** is zero or greater, data from the first nSegment segments on level iLevel
246149** is merged.
246150**
246151** The iterator initially points to the first term/rowid entry in the
246152** iterated data.
246153*/
246154static void fts5MultiIterNew(
246155 Fts5Index *p, /* FTS5 backend to iterate within */
246156 Fts5Structure *pStruct, /* Structure of specific index */
246157 int flags, /* FTS5INDEX_QUERY_XXX flags */
246158 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
246159 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
246160 int iLevel, /* Level to iterate (-1 for all) */
246161 int nSegment, /* Number of segments to merge (iLevel>=0) */
246162 Fts5Iter **ppOut /* New object */
246163){
246164 int nSeg = 0; /* Number of segment-iters in use */
246165 int iIter = 0; /* */
246166 int iSeg; /* Used to iterate through segments */
246167 Fts5StructureLevel *pLvl;
246168 Fts5Iter *pNew;
246169
246170 assert( (pTerm==0 && nTerm==0) || iLevel<0 )((void) (0));
246171
246172 /* Allocate space for the new multi-seg-iterator. */
246173 if( p->rc==SQLITE_OK0 ){
246174 if( iLevel<0 ){
246175 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
246176 nSeg = pStruct->nSegment;
246177 nSeg += (p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH0x0040));
246178 }else{
246179 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment)((pStruct->aLevel[iLevel].nSeg)<(nSegment)?(pStruct->
aLevel[iLevel].nSeg):(nSegment))
;
246180 }
246181 }
246182 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
246183 if( pNew==0 ){
246184 assert( p->rc!=SQLITE_OK )((void) (0));
246185 goto fts5MultiIterNew_post_check;
246186 }
246187 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC0x0002));
246188 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY0x0010));
246189 pNew->pColset = pColset;
246190 if( (flags & FTS5INDEX_QUERY_NOOUTPUT0x0020)==0 ){
246191 fts5IterSetOutputCb(&p->rc, pNew);
246192 }
246193
246194 /* Initialize each of the component segment iterators. */
246195 if( p->rc==SQLITE_OK0 ){
246196 if( iLevel<0 ){
246197 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
246198 if( p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH0x0040) ){
246199 /* Add a segment iterator for the current contents of the hash table. */
246200 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
246201 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
246202 }
246203 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
246204 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
246205 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
246206 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
246207 if( pTerm==0 ){
246208 fts5SegIterInit(p, pSeg, pIter);
246209 }else{
246210 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
246211 }
246212 }
246213 }
246214 }else{
246215 pLvl = &pStruct->aLevel[iLevel];
246216 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
246217 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
246218 }
246219 }
246220 assert( iIter==nSeg )((void) (0));
246221 }
246222
246223 /* If the above was successful, each component iterator now points
246224 ** to the first entry in its segment. In this case initialize the
246225 ** aFirst[] array. Or, if an error has occurred, free the iterator
246226 ** object and set the output variable to NULL. */
246227 if( p->rc==SQLITE_OK0 ){
246228 fts5MultiIterFinishSetup(p, pNew);
246229 }else{
246230 fts5MultiIterFree(pNew);
246231 *ppOut = 0;
246232 }
246233
246234fts5MultiIterNew_post_check:
246235 assert( (*ppOut)!=0 || p->rc!=SQLITE_OK )((void) (0));
246236 return;
246237}
246238
246239/*
246240** Create an Fts5Iter that iterates through the doclist provided
246241** as the second argument.
246242*/
246243static void fts5MultiIterNew2(
246244 Fts5Index *p, /* FTS5 backend to iterate within */
246245 Fts5Data *pData, /* Doclist to iterate through */
246246 int bDesc, /* True for descending rowid order */
246247 Fts5Iter **ppOut /* New object */
246248){
246249 Fts5Iter *pNew;
246250 pNew = fts5MultiIterAlloc(p, 2);
246251 if( pNew ){
246252 Fts5SegIter *pIter = &pNew->aSeg[1];
246253 pIter->flags = FTS5_SEGITER_ONETERM0x01;
246254 if( pData->szLeaf>0 ){
246255 pIter->pLeaf = pData;
246256 pIter->iLeafOffset = fts5GetVarintsqlite3Fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
246257 pIter->iEndofDoclist = pData->nn;
246258 pNew->aFirst[1].iFirst = 1;
246259 if( bDesc ){
246260 pNew->bRev = 1;
246261 pIter->flags |= FTS5_SEGITER_REVERSE0x02;
246262 fts5SegIterReverseInitPage(p, pIter);
246263 }else{
246264 fts5SegIterLoadNPos(p, pIter);
246265 }
246266 pData = 0;
246267 }else{
246268 pNew->base.bEof = 1;
246269 }
246270 fts5SegIterSetNext(p, pIter);
246271
246272 *ppOut = pNew;
246273 }
246274
246275 fts5DataRelease(pData);
246276}
246277
246278/*
246279** Return true if the iterator is at EOF or if an error has occurred.
246280** False otherwise.
246281*/
246282static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
246283 assert( pIter!=0 || p->rc!=SQLITE_OK )((void) (0));
246284 assert( p->rc!=SQLITE_OK((void) (0))
246285 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof((void) (0))
246286 )((void) (0));
246287 return (p->rc || pIter->base.bEof);
246288}
246289
246290/*
246291** Return the rowid of the entry that the iterator currently points
246292** to. If the iterator points to EOF when this function is called the
246293** results are undefined.
246294*/
246295static i64 fts5MultiIterRowid(Fts5Iter *pIter){
246296 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf )((void) (0));
246297 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
246298}
246299
246300/*
246301** Move the iterator to the next entry at or following iMatch.
246302*/
246303static void fts5MultiIterNextFrom(
246304 Fts5Index *p,
246305 Fts5Iter *pIter,
246306 i64 iMatch
246307){
246308 while( 1 ){
246309 i64 iRowid;
246310 fts5MultiIterNext(p, pIter, 1, iMatch);
246311 if( fts5MultiIterEof(p, pIter) ) break;
246312 iRowid = fts5MultiIterRowid(pIter);
246313 if( pIter->bRev==0 && iRowid>=iMatch ) break;
246314 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
246315 }
246316}
246317
246318/*
246319** Return a pointer to a buffer containing the term associated with the
246320** entry that the iterator currently points to.
246321*/
246322static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
246323 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
246324 *pn = p->term.n;
246325 return p->term.p;
246326}
246327
246328/*
246329** Allocate a new segment-id for the structure pStruct. The new segment
246330** id must be between 1 and 65335 inclusive, and must not be used by
246331** any currently existing segment. If a free segment id cannot be found,
246332** SQLITE_FULL is returned.
246333**
246334** If an error has already occurred, this function is a no-op. 0 is
246335** returned in this case.
246336*/
246337static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
246338 int iSegid = 0;
246339
246340 if( p->rc==SQLITE_OK0 ){
246341 if( pStruct->nSegment>=FTS5_MAX_SEGMENT2000 ){
246342 p->rc = SQLITE_FULL13;
246343 }else{
246344 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
246345 ** array is 63 elements, or 252 bytes, in size. */
246346 u32 aUsed[(FTS5_MAX_SEGMENT2000+31) / 32];
246347 int iLvl, iSeg;
246348 int i;
246349 u32 mask;
246350 memset(aUsed, 0, sizeof(aUsed));
246351 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
246352 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
246353 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
246354 if( iId<=FTS5_MAX_SEGMENT2000 && iId>0 ){
246355 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
246356 }
246357 }
246358 }
246359
246360 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
246361 mask = aUsed[i];
246362 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
246363 iSegid += 1 + i*32;
246364
246365#ifdef SQLITE_DEBUG
246366 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
246367 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
246368 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid )((void) (0));
246369 }
246370 }
246371 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT )((void) (0));
246372
246373 {
246374 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
246375 if( p->rc==SQLITE_OK0 ){
246376 u8 aBlob[2] = {0xff, 0xff};
246377 sqlite3_bind_int(pIdxSelect, 1, iSegid);
246378 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC((sqlite3_destructor_type)0));
246379 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW )((void) (0));
246380 p->rc = sqlite3_reset(pIdxSelect);
246381 sqlite3_bind_null(pIdxSelect, 2);
246382 }
246383 }
246384#endif
246385 }
246386 }
246387
246388 return iSegid;
246389}
246390
246391/*
246392** Discard all data currently cached in the hash-tables.
246393*/
246394static void fts5IndexDiscardData(Fts5Index *p){
246395 assert( p->pHash || p->nPendingData==0 )((void) (0));
246396 if( p->pHash ){
246397 sqlite3Fts5HashClear(p->pHash);
246398 p->nPendingData = 0;
246399 p->nPendingRow = 0;
246400 p->flushRc = SQLITE_OK0;
246401 }
246402 p->nContentlessDelete = 0;
246403}
246404
246405/*
246406** Return the size of the prefix, in bytes, that buffer
246407** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
246408**
246409** Buffer (pNew/<length-unknown>) is guaranteed to be greater
246410** than buffer (pOld/nOld).
246411*/
246412static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
246413 int i;
246414 for(i=0; i<nOld; i++){
246415 if( pOld[i]!=pNew[i] ) break;
246416 }
246417 return i;
246418}
246419
246420static void fts5WriteDlidxClear(
246421 Fts5Index *p,
246422 Fts5SegWriter *pWriter,
246423 int bFlush /* If true, write dlidx to disk */
246424){
246425 int i;
246426 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) )((void) (0));
246427 for(i=0; i<pWriter->nDlidx; i++){
246428 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
246429 if( pDlidx->buf.n==0 ) break;
246430 if( bFlush ){
246431 assert( pDlidx->pgno!=0 )((void) (0));
246432 fts5DataWrite(p,
246433 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(1)
<< (31 + 5)) + ((i64)(i) << (31)) + ((i64)(pDlidx
->pgno)) )
,
246434 pDlidx->buf.p, pDlidx->buf.n
246435 );
246436 }
246437 sqlite3Fts5BufferZero(&pDlidx->buf);
246438 pDlidx->bPrevValid = 0;
246439 }
246440}
246441
246442/*
246443** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
246444** Any new array elements are zeroed before returning.
246445*/
246446static int fts5WriteDlidxGrow(
246447 Fts5Index *p,
246448 Fts5SegWriter *pWriter,
246449 int nLvl
246450){
246451 if( p->rc==SQLITE_OK0 && nLvl>=pWriter->nDlidx ){
246452 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
246453 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
246454 );
246455 if( aDlidx==0 ){
246456 p->rc = SQLITE_NOMEM7;
246457 }else{
246458 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
246459 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
246460 pWriter->aDlidx = aDlidx;
246461 pWriter->nDlidx = nLvl;
246462 }
246463 }
246464 return p->rc;
246465}
246466
246467/*
246468** If the current doclist-index accumulating in pWriter->aDlidx[] is large
246469** enough, flush it to disk and return 1. Otherwise discard it and return
246470** zero.
246471*/
246472static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
246473 int bFlag = 0;
246474
246475 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
246476 ** to the database, also write the doclist-index to disk. */
246477 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE4 ){
246478 bFlag = 1;
246479 }
246480 fts5WriteDlidxClear(p, pWriter, bFlag);
246481 pWriter->nEmpty = 0;
246482 return bFlag;
246483}
246484
246485/*
246486** This function is called whenever processing of the doclist for the
246487** last term on leaf page (pWriter->iBtPage) is completed.
246488**
246489** The doclist-index for that term is currently stored in-memory within the
246490** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
246491** writes it out to disk. Or, if it is too small to bother with, discards
246492** it.
246493**
246494** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
246495*/
246496static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
246497 int bFlag;
246498
246499 assert( pWriter->iBtPage || pWriter->nEmpty==0 )((void) (0));
246500 if( pWriter->iBtPage==0 ) return;
246501 bFlag = fts5WriteFlushDlidx(p, pWriter);
246502
246503 if( p->rc==SQLITE_OK0 ){
246504 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
246505 /* The following was already done in fts5WriteInit(): */
246506 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
246507 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC((sqlite3_destructor_type)0));
246508 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
246509 sqlite3_step(p->pIdxWriter);
246510 p->rc = sqlite3_reset(p->pIdxWriter);
246511 sqlite3_bind_null(p->pIdxWriter, 2);
246512 }
246513 pWriter->iBtPage = 0;
246514}
246515
246516/*
246517** This is called once for each leaf page except the first that contains
246518** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
246519** is larger than all terms written to earlier leaves, and equal to or
246520** smaller than the first term on the new leaf.
246521**
246522** If an error occurs, an error code is left in Fts5Index.rc. If an error
246523** has already occurred when this function is called, it is a no-op.
246524*/
246525static void fts5WriteBtreeTerm(
246526 Fts5Index *p, /* FTS5 backend object */
246527 Fts5SegWriter *pWriter, /* Writer object */
246528 int nTerm, const u8 *pTerm /* First term on new page */
246529){
246530 fts5WriteFlushBtree(p, pWriter);
246531 if( p->rc==SQLITE_OK0 ){
246532 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&pWriter->btterm,nTerm
,pTerm)
;
246533 pWriter->iBtPage = pWriter->writer.pgno;
246534 }
246535}
246536
246537/*
246538** This function is called when flushing a leaf page that contains no
246539** terms at all to disk.
246540*/
246541static void fts5WriteBtreeNoTerm(
246542 Fts5Index *p, /* FTS5 backend object */
246543 Fts5SegWriter *pWriter /* Writer object */
246544){
246545 /* If there were no rowids on the leaf page either and the doclist-index
246546 ** has already been started, append an 0x00 byte to it. */
246547 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
246548 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
246549 assert( pDlidx->bPrevValid )((void) (0));
246550 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
246551 }
246552
246553 /* Increment the "number of sequential leaves without a term" counter. */
246554 pWriter->nEmpty++;
246555}
246556
246557static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
246558 i64 iRowid;
246559 int iOff;
246560
246561 iOff = 1 + fts5GetVarintsqlite3Fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
246562 fts5GetVarintsqlite3Fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
246563 return iRowid;
246564}
246565
246566/*
246567** Rowid iRowid has just been appended to the current leaf page. It is the
246568** first on the page. This function appends an appropriate entry to the current
246569** doclist-index.
246570*/
246571static void fts5WriteDlidxAppend(
246572 Fts5Index *p,
246573 Fts5SegWriter *pWriter,
246574 i64 iRowid
246575){
246576 int i;
246577 int bDone = 0;
246578
246579 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
246580 i64 iVal;
246581 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
246582
246583 if( pDlidx->buf.n>=p->pConfig->pgsz ){
246584 /* The current doclist-index page is full. Write it to disk and push
246585 ** a copy of iRowid (which will become the first rowid on the next
246586 ** doclist-index leaf page) up into the next level of the b-tree
246587 ** hierarchy. If the node being flushed is currently the root node,
246588 ** also push its first rowid upwards. */
246589 pDlidx->buf.p[0] = 0x01; /* Not the root node */
246590 fts5DataWrite(p,
246591 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(1)
<< (31 + 5)) + ((i64)(i) << (31)) + ((i64)(pDlidx
->pgno)) )
,
246592 pDlidx->buf.p, pDlidx->buf.n
246593 );
246594 fts5WriteDlidxGrow(p, pWriter, i+2);
246595 pDlidx = &pWriter->aDlidx[i];
246596 if( p->rc==SQLITE_OK0 && pDlidx[1].buf.n==0 ){
246597 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
246598
246599 /* This was the root node. Push its first rowid up to the new root. */
246600 pDlidx[1].pgno = pDlidx->pgno;
246601 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
246602 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
246603 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
246604 pDlidx[1].bPrevValid = 1;
246605 pDlidx[1].iPrev = iFirst;
246606 }
246607
246608 sqlite3Fts5BufferZero(&pDlidx->buf);
246609 pDlidx->bPrevValid = 0;
246610 pDlidx->pgno++;
246611 }else{
246612 bDone = 1;
246613 }
246614
246615 if( pDlidx->bPrevValid ){
246616 iVal = (u64)iRowid - (u64)pDlidx->iPrev;
246617 }else{
246618 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
246619 assert( pDlidx->buf.n==0 )((void) (0));
246620 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
246621 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
246622 iVal = iRowid;
246623 }
246624
246625 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
246626 pDlidx->bPrevValid = 1;
246627 pDlidx->iPrev = iRowid;
246628 }
246629}
246630
246631static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
246632 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
246633 Fts5PageWriter *pPage = &pWriter->writer;
246634 i64 iRowid;
246635
246636 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) )((void) (0));
246637
246638 /* Set the szLeaf header field. */
246639 assert( 0==fts5GetU16(&pPage->buf.p[2]) )((void) (0));
246640 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
246641
246642 if( pWriter->bFirstTermInPage ){
246643 /* No term was written to this page. */
246644 assert( pPage->pgidx.n==0 )((void) (0));
246645 fts5WriteBtreeNoTerm(p, pWriter);
246646 }else{
246647 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
246648 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
pPage->pgidx.n,pPage->pgidx.p)
;
246649 }
246650
246651 /* Write the page out to disk */
246652 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno)( ((i64)(pWriter->iSegid) << (31 +5 +1)) + ((i64)(0)
<< (31 + 5)) + ((i64)(0) << (31)) + ((i64)(pPage
->pgno)) )
;
246653 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
246654
246655 /* Initialize the next page. */
246656 fts5BufferZero(&pPage->buf)sqlite3Fts5BufferZero(&pPage->buf);
246657 fts5BufferZero(&pPage->pgidx)sqlite3Fts5BufferZero(&pPage->pgidx);
246658 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
4,zero)
;
246659 pPage->iPrevPgidx = 0;
246660 pPage->pgno++;
246661
246662 /* Increase the leaves written counter */
246663 pWriter->nLeafWritten++;
246664
246665 /* The new leaf holds no terms or rowids */
246666 pWriter->bFirstTermInPage = 1;
246667 pWriter->bFirstRowidInPage = 1;
246668}
246669
246670/*
246671** Append term pTerm/nTerm to the segment being written by the writer passed
246672** as the second argument.
246673**
246674** If an error occurs, set the Fts5Index.rc error code. If an error has
246675** already occurred, this function is a no-op.
246676*/
246677static void fts5WriteAppendTerm(
246678 Fts5Index *p,
246679 Fts5SegWriter *pWriter,
246680 int nTerm, const u8 *pTerm
246681){
246682 int nPrefix; /* Bytes of prefix compression for term */
246683 Fts5PageWriter *pPage = &pWriter->writer;
246684 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
246685 int nMin = MIN(pPage->term.n, nTerm)((pPage->term.n)<(nTerm)?(pPage->term.n):(nTerm));
246686
246687 assert( p->rc==SQLITE_OK )((void) (0));
246688 assert( pPage->buf.n>=4 )((void) (0));
246689 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage )((void) (0));
246690
246691 /* If the current leaf page is full, flush it to disk. */
246692 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
246693 if( pPage->buf.n>4 ){
246694 fts5WriteFlushLeaf(p, pWriter);
246695 if( p->rc!=SQLITE_OK0 ) return;
246696 }
246697 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) )
;
246698 }
246699
246700 /* TODO1: Updating pgidx here. */
246701 pPgidx->n += sqlite3Fts5PutVarint(
246702 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
246703 );
246704 pPage->iPrevPgidx = pPage->buf.n;
246705#if 0
246706 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
246707 pPgidx->n += 2;
246708#endif
246709
246710 if( pWriter->bFirstTermInPage ){
246711 nPrefix = 0;
246712 if( pPage->pgno!=1 ){
246713 /* This is the first term on a leaf that is not the leftmost leaf in
246714 ** the segment b-tree. In this case it is necessary to add a term to
246715 ** the b-tree hierarchy that is (a) larger than the largest term
246716 ** already written to the segment and (b) smaller than or equal to
246717 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
246718 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
246719 ** previous term.
246720 **
246721 ** Usually, the previous term is available in pPage->term. The exception
246722 ** is if this is the first term written in an incremental-merge step.
246723 ** In this case the previous term is not available, so just write a
246724 ** copy of (pTerm/nTerm) into the parent node. This is slightly
246725 ** inefficient, but still correct. */
246726 int n = nTerm;
246727 if( pPage->term.n ){
246728 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
246729 }
246730 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
246731 if( p->rc!=SQLITE_OK0 ) return;
246732 pPage = &pWriter->writer;
246733 }
246734 }else{
246735 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
246736 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)nPrefix)
;
246737 }
246738
246739 /* Append the number of bytes of new data, then the term data itself
246740 ** to the page. */
246741 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)nTerm - nPrefix)
;
246742 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix])sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
nTerm - nPrefix,&pTerm[nPrefix])
;
246743
246744 /* Update the Fts5PageWriter.term field. */
246745 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&pPage->term,nTerm,
pTerm)
;
246746 pWriter->bFirstTermInPage = 0;
246747
246748 pWriter->bFirstRowidInPage = 0;
246749 pWriter->bFirstRowidInDoclist = 1;
246750
246751 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) )((void) (0));
246752 pWriter->aDlidx[0].pgno = pPage->pgno;
246753}
246754
246755/*
246756** Append a rowid and position-list size field to the writers output.
246757*/
246758static void fts5WriteAppendRowid(
246759 Fts5Index *p,
246760 Fts5SegWriter *pWriter,
246761 i64 iRowid
246762){
246763 if( p->rc==SQLITE_OK0 ){
246764 Fts5PageWriter *pPage = &pWriter->writer;
246765
246766 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
246767 fts5WriteFlushLeaf(p, pWriter);
246768 }
246769
246770 /* If this is to be the first rowid written to the page, set the
246771 ** rowid-pointer in the page-header. Also append a value to the dlidx
246772 ** buffer, in case a doclist-index is required. */
246773 if( pWriter->bFirstRowidInPage ){
246774 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
246775 fts5WriteDlidxAppend(p, pWriter, iRowid);
246776 }
246777
246778 /* Write the rowid. */
246779 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
246780 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid)sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)iRowid)
;
246781 }else{
246782 assert_nc( p->rc || iRowid>pWriter->iPrevRowid )((void) (0));
246783 fts5BufferAppendVarint(&p->rc, &pPage->buf,sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)(u64)iRowid - (u64)pWriter->iPrevRowid)
246784 (u64)iRowid - (u64)pWriter->iPrevRowidsqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)(u64)iRowid - (u64)pWriter->iPrevRowid)
246785 )sqlite3Fts5BufferAppendVarint(&p->rc,&pPage->buf
,(i64)(u64)iRowid - (u64)pWriter->iPrevRowid)
;
246786 }
246787 pWriter->iPrevRowid = iRowid;
246788 pWriter->bFirstRowidInDoclist = 0;
246789 pWriter->bFirstRowidInPage = 0;
246790 }
246791}
246792
246793static void fts5WriteAppendPoslistData(
246794 Fts5Index *p,
246795 Fts5SegWriter *pWriter,
246796 const u8 *aData,
246797 int nData
246798){
246799 Fts5PageWriter *pPage = &pWriter->writer;
246800 const u8 *a = aData;
246801 int n = nData;
246802
246803 assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK )((void) (0));
246804 while( p->rc==SQLITE_OK0
246805 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
246806 ){
246807 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
246808 int nCopy = 0;
246809 while( nCopy<nReq ){
246810 i64 dummy;
246811 nCopy += fts5GetVarintsqlite3Fts5GetVarint(&a[nCopy], (u64*)&dummy);
246812 }
246813 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
nCopy,a)
;
246814 a += nCopy;
246815 n -= nCopy;
246816 fts5WriteFlushLeaf(p, pWriter);
246817 }
246818 if( n>0 ){
246819 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a)sqlite3Fts5BufferAppendBlob(&p->rc,&pPage->buf,
n,a)
;
246820 }
246821}
246822
246823/*
246824** Flush any data cached by the writer object to the database. Free any
246825** allocations associated with the writer.
246826*/
246827static void fts5WriteFinish(
246828 Fts5Index *p,
246829 Fts5SegWriter *pWriter, /* Writer object */
246830 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
246831){
246832 int i;
246833 Fts5PageWriter *pLeaf = &pWriter->writer;
246834 if( p->rc==SQLITE_OK0 ){
246835 assert( pLeaf->pgno>=1 )((void) (0));
246836 if( pLeaf->buf.n>4 ){
246837 fts5WriteFlushLeaf(p, pWriter);
246838 }
246839 *pnLeaf = pLeaf->pgno-1;
246840 if( pLeaf->pgno>1 ){
246841 fts5WriteFlushBtree(p, pWriter);
246842 }
246843 }
246844 fts5BufferFree(&pLeaf->term)sqlite3Fts5BufferFree(&pLeaf->term);
246845 fts5BufferFree(&pLeaf->buf)sqlite3Fts5BufferFree(&pLeaf->buf);
246846 fts5BufferFree(&pLeaf->pgidx)sqlite3Fts5BufferFree(&pLeaf->pgidx);
246847 fts5BufferFree(&pWriter->btterm)sqlite3Fts5BufferFree(&pWriter->btterm);
246848
246849 for(i=0; i<pWriter->nDlidx; i++){
246850 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
246851 }
246852 sqlite3_free(pWriter->aDlidx);
246853}
246854
246855static void fts5WriteInit(
246856 Fts5Index *p,
246857 Fts5SegWriter *pWriter,
246858 int iSegid
246859){
246860 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING20;
246861
246862 memset(pWriter, 0, sizeof(Fts5SegWriter));
246863 pWriter->iSegid = iSegid;
246864
246865 fts5WriteDlidxGrow(p, pWriter, 1);
246866 pWriter->writer.pgno = 1;
246867 pWriter->bFirstTermInPage = 1;
246868 pWriter->iBtPage = 1;
246869
246870 assert( pWriter->writer.buf.n==0 )((void) (0));
246871 assert( pWriter->writer.pgidx.n==0 )((void) (0));
246872
246873 /* Grow the two buffers to pgsz + padding bytes in size. */
246874 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
246875 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
246876
246877 if( p->pIdxWriter==0 ){
246878 Fts5Config *pConfig = p->pConfig;
246879 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
246880 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
246881 pConfig->zDb, pConfig->zName
246882 ));
246883 }
246884
246885 if( p->rc==SQLITE_OK0 ){
246886 /* Initialize the 4-byte leaf-page header to 0x00. */
246887 memset(pWriter->writer.buf.p, 0, 4);
246888 pWriter->writer.buf.n = 4;
246889
246890 /* Bind the current output segment id to the index-writer. This is an
246891 ** optimization over binding the same value over and over as rows are
246892 ** inserted into %_idx by the current writer. */
246893 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
246894 }
246895}
246896
246897/*
246898** Iterator pIter was used to iterate through the input segments of on an
246899** incremental merge operation. This function is called if the incremental
246900** merge step has finished but the input has not been completely exhausted.
246901*/
246902static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
246903 int i;
246904 Fts5Buffer buf;
246905 memset(&buf, 0, sizeof(Fts5Buffer));
246906 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK0; i++){
246907 Fts5SegIter *pSeg = &pIter->aSeg[i];
246908 if( pSeg->pSeg==0 ){
246909 /* no-op */
246910 }else if( pSeg->pLeaf==0 ){
246911 /* All keys from this input segment have been transfered to the output.
246912 ** Set both the first and last page-numbers to 0 to indicate that the
246913 ** segment is now empty. */
246914 pSeg->pSeg->pgnoLast = 0;
246915 pSeg->pSeg->pgnoFirst = 0;
246916 }else{
246917 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
246918 i64 iLeafRowid;
246919 Fts5Data *pData;
246920 int iId = pSeg->pSeg->iSegid;
246921 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
246922
246923 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno)( ((i64)(iId) << (31 +5 +1)) + ((i64)(0) << (31 +
5)) + ((i64)(0) << (31)) + ((i64)(pSeg->iTermLeafPgno
)) )
;
246924 pData = fts5LeafRead(p, iLeafRowid);
246925 if( pData ){
246926 if( iOff>pData->szLeaf ){
246927 /* This can occur if the pages that the segments occupy overlap - if
246928 ** a single page has been assigned to more than one segment. In
246929 ** this case a prior iteration of this loop may have corrupted the
246930 ** segment currently being trimmed. */
246931 p->rc = FTS5_CORRUPT(11 | (1<<8));
246932 }else{
246933 fts5BufferZero(&buf)sqlite3Fts5BufferZero(&buf);
246934 fts5BufferGrow(&p->rc, &buf, pData->nn)( (u32)((&buf)->n) + (u32)(pData->nn) <= (u32)((
&buf)->nSpace) ? 0 : sqlite3Fts5BufferSize((&p->
rc),(&buf),(pData->nn)+(&buf)->n) )
;
246935 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr)sqlite3Fts5BufferAppendBlob(&p->rc,&buf,sizeof(aHdr
),aHdr)
;
246936 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)pSeg
->term.n)
;
246937 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p)sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pSeg->term
.n,pSeg->term.p)
;
246938 fts5BufferAppendBlob(&p->rc, &buf,pData->szLeaf-iOff,&pData->p[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
szLeaf-iOff,&pData->p[iOff])
;
246939 if( p->rc==SQLITE_OK0 ){
246940 /* Set the szLeaf field */
246941 fts5PutU16(&buf.p[2], (u16)buf.n);
246942 }
246943
246944 /* Set up the new page-index array */
246945 fts5BufferAppendVarint(&p->rc, &buf, 4)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)4);
246946 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
246947 && pSeg->iEndofDoclist<pData->szLeaf
246948 && pSeg->iPgidxOff<=pData->nn
246949 ){
246950 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
246951 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4)sqlite3Fts5BufferAppendVarint(&p->rc,&buf,(i64)buf
.n - 1 - nDiff - 4)
;
246952 fts5BufferAppendBlob(&p->rc, &buf,sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
246953 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
246954 )sqlite3Fts5BufferAppendBlob(&p->rc,&buf,pData->
nn - pSeg->iPgidxOff,&pData->p[pSeg->iPgidxOff])
;
246955 }
246956
246957 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
246958 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1)( ((i64)(iId) << (31 +5 +1)) + ((i64)(0) << (31 +
5)) + ((i64)(0) << (31)) + ((i64)(1)) )
, iLeafRowid);
246959 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
246960 }
246961 fts5DataRelease(pData);
246962 }
246963 }
246964 }
246965 fts5BufferFree(&buf)sqlite3Fts5BufferFree(&buf);
246966}
246967
246968static void fts5MergeChunkCallback(
246969 Fts5Index *p,
246970 void *pCtx,
246971 const u8 *pChunk, int nChunk
246972){
246973 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
246974 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
246975}
246976
246977/*
246978**
246979*/
246980static void fts5IndexMergeLevel(
246981 Fts5Index *p, /* FTS5 backend object */
246982 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
246983 int iLvl, /* Level to read input from */
246984 int *pnRem /* Write up to this many output leaves */
246985){
246986 Fts5Structure *pStruct = *ppStruct;
246987 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
246988 Fts5StructureLevel *pLvlOut;
246989 Fts5Iter *pIter = 0; /* Iterator to read input data */
246990 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
246991 int nInput; /* Number of input segments */
246992 Fts5SegWriter writer; /* Writer object */
246993 Fts5StructureSegment *pSeg; /* Output segment */
246994 Fts5Buffer term;
246995 int bOldest; /* True if the output segment is the oldest */
246996 int eDetail = p->pConfig->eDetail;
246997 const int flags = FTS5INDEX_QUERY_NOOUTPUT0x0020;
246998 int bTermWritten = 0; /* True if current term already output */
246999
247000 assert( iLvl<pStruct->nLevel )((void) (0));
247001 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
247002
247003 memset(&writer, 0, sizeof(Fts5SegWriter));
247004 memset(&term, 0, sizeof(Fts5Buffer));
247005 if( pLvl->nMerge ){
247006 pLvlOut = &pStruct->aLevel[iLvl+1];
247007 assert( pLvlOut->nSeg>0 )((void) (0));
247008 nInput = pLvl->nMerge;
247009 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
247010
247011 fts5WriteInit(p, &writer, pSeg->iSegid);
247012 writer.writer.pgno = pSeg->pgnoLast+1;
247013 writer.iBtPage = 0;
247014 }else{
247015 int iSegid = fts5AllocateSegid(p, pStruct);
247016
247017 /* Extend the Fts5Structure object as required to ensure the output
247018 ** segment exists. */
247019 if( iLvl==pStruct->nLevel-1 ){
247020 fts5StructureAddLevel(&p->rc, ppStruct);
247021 pStruct = *ppStruct;
247022 }
247023 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
247024 if( p->rc ) return;
247025 pLvl = &pStruct->aLevel[iLvl];
247026 pLvlOut = &pStruct->aLevel[iLvl+1];
247027
247028 fts5WriteInit(p, &writer, iSegid);
247029
247030 /* Add the new segment to the output level */
247031 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
247032 pLvlOut->nSeg++;
247033 pSeg->pgnoFirst = 1;
247034 pSeg->iSegid = iSegid;
247035 pStruct->nSegment++;
247036
247037 /* Read input from all segments in the input level */
247038 nInput = pLvl->nSeg;
247039
247040 /* Set the range of origins that will go into the output segment. */
247041 if( pStruct->nOriginCntr>0 ){
247042 pSeg->iOrigin1 = pLvl->aSeg[0].iOrigin1;
247043 pSeg->iOrigin2 = pLvl->aSeg[pLvl->nSeg-1].iOrigin2;
247044 }
247045 }
247046 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
247047
247048 assert( iLvl>=0 )((void) (0));
247049 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
247050 fts5MultiIterEof(p, pIter)==0;
247051 fts5MultiIterNext(p, pIter, 0, 0)
247052 ){
247053 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
247054 int nPos; /* position-list size field value */
247055 int nTerm;
247056 const u8 *pTerm;
247057
247058 pTerm = fts5MultiIterTerm(pIter, &nTerm);
247059 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm)((nTerm)<=0 ? 0 : memcmp((pTerm), (term.p), (nTerm))) ){
247060 if( pnRem && writer.nLeafWritten>nRem ){
247061 break;
247062 }
247063 fts5BufferSet(&p->rc, &term, nTerm, pTerm)sqlite3Fts5BufferSet(&p->rc,&term,nTerm,pTerm);
247064 bTermWritten =0;
247065 }
247066
247067 /* Check for key annihilation. */
247068 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
247069
247070 if( p->rc==SQLITE_OK0 && bTermWritten==0 ){
247071 /* This is a new term. Append a term to the output segment. */
247072 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
247073 bTermWritten = 1;
247074 }
247075
247076 /* Append the rowid to the output */
247077 /* WRITEPOSLISTSIZE */
247078 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
247079
247080 if( eDetail==FTS5_DETAIL_NONE1 ){
247081 if( pSegIter->bDel ){
247082 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,(i64)0)
;
247083 if( pSegIter->nPos>0 ){
247084 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,(i64)0)
;
247085 }
247086 }
247087 }else{
247088 /* Append the position-list data to the output */
247089 nPos = pSegIter->nPos*2 + pSegIter->bDel;
247090 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos)sqlite3Fts5BufferAppendVarint(&p->rc,&writer.writer
.buf,(i64)nPos)
;
247091 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
247092 }
247093 }
247094
247095 /* Flush the last leaf page to disk. Set the output segment b-tree height
247096 ** and last leaf page number at the same time. */
247097 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
247098
247099 assert( pIter!=0 || p->rc!=SQLITE_OK )((void) (0));
247100 if( fts5MultiIterEof(p, pIter) ){
247101 int i;
247102
247103 /* Remove the redundant segments from the %_data table */
247104 assert( pSeg->nEntry==0 )((void) (0));
247105 for(i=0; i<nInput; i++){
247106 Fts5StructureSegment *pOld = &pLvl->aSeg[i];
247107 pSeg->nEntry += (pOld->nEntry - pOld->nEntryTombstone);
247108 fts5DataRemoveSegment(p, pOld);
247109 }
247110
247111 /* Remove the redundant segments from the input level */
247112 if( pLvl->nSeg!=nInput ){
247113 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
247114 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
247115 }
247116 pStruct->nSegment -= nInput;
247117 pLvl->nSeg -= nInput;
247118 pLvl->nMerge = 0;
247119 if( pSeg->pgnoLast==0 ){
247120 pLvlOut->nSeg--;
247121 pStruct->nSegment--;
247122 }
247123 }else{
247124 assert( pSeg->pgnoLast>0 )((void) (0));
247125 fts5TrimSegments(p, pIter);
247126 pLvl->nMerge = nInput;
247127 }
247128
247129 fts5MultiIterFree(pIter);
247130 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
247131 if( pnRem ) *pnRem -= writer.nLeafWritten;
247132}
247133
247134/*
247135** If this is not a contentless_delete=1 table, or if the 'deletemerge'
247136** configuration option is set to 0, then this function always returns -1.
247137** Otherwise, it searches the structure object passed as the second argument
247138** for a level suitable for merging due to having a large number of
247139** tombstones in the tombstone hash. If one is found, its index is returned.
247140** Otherwise, if there is no suitable level, -1.
247141*/
247142static int fts5IndexFindDeleteMerge(Fts5Index *p, Fts5Structure *pStruct){
247143 Fts5Config *pConfig = p->pConfig;
247144 int iRet = -1;
247145 if( pConfig->bContentlessDelete && pConfig->nDeleteMerge>0 ){
247146 int ii;
247147 int nBest = 0;
247148
247149 for(ii=0; ii<pStruct->nLevel; ii++){
247150 Fts5StructureLevel *pLvl = &pStruct->aLevel[ii];
247151 i64 nEntry = 0;
247152 i64 nTomb = 0;
247153 int iSeg;
247154 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
247155 nEntry += pLvl->aSeg[iSeg].nEntry;
247156 nTomb += pLvl->aSeg[iSeg].nEntryTombstone;
247157 }
247158 assert_nc( nEntry>0 || pLvl->nSeg==0 )((void) (0));
247159 if( nEntry>0 ){
247160 int nPercent = (nTomb * 100) / nEntry;
247161 if( nPercent>=pConfig->nDeleteMerge && nPercent>nBest ){
247162 iRet = ii;
247163 nBest = nPercent;
247164 }
247165 }
247166
247167 /* If pLvl is already the input level to an ongoing merge, look no
247168 ** further for a merge candidate. The caller should be allowed to
247169 ** continue merging from pLvl first. */
247170 if( pLvl->nMerge ) break;
247171 }
247172 }
247173 return iRet;
247174}
247175
247176/*
247177** Do up to nPg pages of automerge work on the index.
247178**
247179** Return true if any changes were actually made, or false otherwise.
247180*/
247181static int fts5IndexMerge(
247182 Fts5Index *p, /* FTS5 backend object */
247183 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
247184 int nPg, /* Pages of work to do */
247185 int nMin /* Minimum number of segments to merge */
247186){
247187 int nRem = nPg;
247188 int bRet = 0;
247189 Fts5Structure *pStruct = *ppStruct;
247190 while( nRem>0 && p->rc==SQLITE_OK0 ){
247191 int iLvl; /* To iterate through levels */
247192 int iBestLvl = 0; /* Level offering the most input segments */
247193 int nBest = 0; /* Number of input segments on best level */
247194
247195 /* Set iBestLvl to the level to read input segments from. Or to -1 if
247196 ** there is no level suitable to merge segments from. */
247197 assert( pStruct->nLevel>0 )((void) (0));
247198 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
247199 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
247200 if( pLvl->nMerge ){
247201 if( pLvl->nMerge>nBest ){
247202 iBestLvl = iLvl;
247203 nBest = nMin;
247204 }
247205 break;
247206 }
247207 if( pLvl->nSeg>nBest ){
247208 nBest = pLvl->nSeg;
247209 iBestLvl = iLvl;
247210 }
247211 }
247212 if( nBest<nMin ){
247213 iBestLvl = fts5IndexFindDeleteMerge(p, pStruct);
247214 }
247215
247216 if( iBestLvl<0 ) break;
247217 bRet = 1;
247218 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
247219 if( p->rc==SQLITE_OK0 && pStruct->aLevel[iBestLvl].nMerge==0 ){
247220 fts5StructurePromote(p, iBestLvl+1, pStruct);
247221 }
247222
247223 if( nMin==1 ) nMin = 2;
247224 }
247225 *ppStruct = pStruct;
247226 return bRet;
247227}
247228
247229/*
247230** A total of nLeaf leaf pages of data has just been flushed to a level-0
247231** segment. This function updates the write-counter accordingly and, if
247232** necessary, performs incremental merge work.
247233**
247234** If an error occurs, set the Fts5Index.rc error code. If an error has
247235** already occurred, this function is a no-op.
247236*/
247237static void fts5IndexAutomerge(
247238 Fts5Index *p, /* FTS5 backend object */
247239 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
247240 int nLeaf /* Number of output leaves just written */
247241){
247242 if( p->rc==SQLITE_OK0 && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0)((*ppStruct)!=0) ){
247243 Fts5Structure *pStruct = *ppStruct;
247244 u64 nWrite; /* Initial value of write-counter */
247245 int nWork; /* Number of work-quanta to perform */
247246 int nRem; /* Number of leaf pages left to write */
247247
247248 /* Update the write-counter. While doing so, set nWork. */
247249 nWrite = pStruct->nWriteCounter;
247250 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
247251 pStruct->nWriteCounter += nLeaf;
247252 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
247253
247254 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
247255 }
247256}
247257
247258static void fts5IndexCrisismerge(
247259 Fts5Index *p, /* FTS5 backend object */
247260 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
247261){
247262 const int nCrisis = p->pConfig->nCrisisMerge;
247263 Fts5Structure *pStruct = *ppStruct;
247264 if( pStruct && pStruct->nLevel>0 ){
247265 int iLvl = 0;
247266 while( p->rc==SQLITE_OK0 && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
247267 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
247268 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) )((void) (0));
247269 fts5StructurePromote(p, iLvl+1, pStruct);
247270 iLvl++;
247271 }
247272 *ppStruct = pStruct;
247273 }
247274}
247275
247276static int fts5IndexReturn(Fts5Index *p){
247277 int rc = p->rc;
247278 p->rc = SQLITE_OK0;
247279 return rc;
247280}
247281
247282typedef struct Fts5FlushCtx Fts5FlushCtx;
247283struct Fts5FlushCtx {
247284 Fts5Index *pIdx;
247285 Fts5SegWriter writer;
247286};
247287
247288/*
247289** Buffer aBuf[] contains a list of varints, all small enough to fit
247290** in a 32-bit integer. Return the size of the largest prefix of this
247291** list nMax bytes or less in size.
247292*/
247293static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
247294 int ret;
247295 u32 dummy;
247296 ret = fts5GetVarint32(aBuf, dummy)sqlite3Fts5GetVarint32(aBuf,(u32*)&(dummy));
247297 if( ret<nMax ){
247298 while( 1 ){
247299 int i = fts5GetVarint32(&aBuf[ret], dummy)sqlite3Fts5GetVarint32(&aBuf[ret],(u32*)&(dummy));
247300 if( (ret + i) > nMax ) break;
247301 ret += i;
247302 }
247303 }
247304 return ret;
247305}
247306
247307/*
247308** Execute the SQL statement:
247309**
247310** DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno);
247311**
247312** This is used when a secure-delete operation removes the last term
247313** from a segment leaf page. In that case the %_idx entry is removed
247314** too. This is done to ensure that if all instances of a token are
247315** removed from an fts5 database in secure-delete mode, no trace of
247316** the token itself remains in the database.
247317*/
247318static void fts5SecureDeleteIdxEntry(
247319 Fts5Index *p, /* FTS5 backend object */
247320 int iSegid, /* Id of segment to delete entry for */
247321 int iPgno /* Page number within segment */
247322){
247323 if( iPgno!=1 ){
247324 assert( p->pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE )((void) (0));
247325 if( p->pDeleteFromIdx==0 ){
247326 fts5IndexPrepareStmt(p, &p->pDeleteFromIdx, sqlite3_mprintf(
247327 "DELETE FROM '%q'.'%q_idx' WHERE (segid, (pgno/2)) = (?1, ?2)",
247328 p->pConfig->zDb, p->pConfig->zName
247329 ));
247330 }
247331 if( p->rc==SQLITE_OK0 ){
247332 sqlite3_bind_int(p->pDeleteFromIdx, 1, iSegid);
247333 sqlite3_bind_int(p->pDeleteFromIdx, 2, iPgno);
247334 sqlite3_step(p->pDeleteFromIdx);
247335 p->rc = sqlite3_reset(p->pDeleteFromIdx);
247336 }
247337 }
247338}
247339
247340/*
247341** This is called when a secure-delete operation removes a position-list
247342** that overflows onto segment page iPgno of segment pSeg. This function
247343** rewrites node iPgno, and possibly one or more of its right-hand peers,
247344** to remove this portion of the position list.
247345**
247346** Output variable (*pbLastInDoclist) is set to true if the position-list
247347** removed is followed by a new term or the end-of-segment, or false if
247348** it is followed by another rowid/position list.
247349*/
247350static void fts5SecureDeleteOverflow(
247351 Fts5Index *p,
247352 Fts5StructureSegment *pSeg,
247353 int iPgno,
247354 int *pbLastInDoclist
247355){
247356 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE1);
247357 int pgno;
247358 Fts5Data *pLeaf = 0;
247359 assert( iPgno!=1 )((void) (0));
247360
247361 *pbLastInDoclist = 1;
247362 for(pgno=iPgno; p->rc==SQLITE_OK0 && pgno<=pSeg->pgnoLast; pgno++){
247363 i64 iRowid = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
247364 int iNext = 0;
247365 u8 *aPg = 0;
247366
247367 pLeaf = fts5DataRead(p, iRowid);
247368 if( pLeaf==0 ) break;
247369 aPg = pLeaf->p;
247370
247371 iNext = fts5GetU16(&aPg[0]);
247372 if( iNext!=0 ){
247373 *pbLastInDoclist = 0;
247374 }
247375 if( iNext==0 && pLeaf->szLeaf!=pLeaf->nn ){
247376 fts5GetVarint32(&aPg[pLeaf->szLeaf], iNext)sqlite3Fts5GetVarint32(&aPg[pLeaf->szLeaf],(u32*)&
(iNext))
;
247377 }
247378
247379 if( iNext==0 ){
247380 /* The page contains no terms or rowids. Replace it with an empty
247381 ** page and move on to the right-hand peer. */
247382 const u8 aEmpty[] = {0x00, 0x00, 0x00, 0x04};
247383 assert_nc( bDetailNone==0 || pLeaf->nn==4 )((void) (0));
247384 if( bDetailNone==0 ) fts5DataWrite(p, iRowid, aEmpty, sizeof(aEmpty));
247385 fts5DataRelease(pLeaf);
247386 pLeaf = 0;
247387 }else if( bDetailNone ){
247388 break;
247389 }else if( iNext>=pLeaf->szLeaf || pLeaf->nn<pLeaf->szLeaf || iNext<4 ){
247390 p->rc = FTS5_CORRUPT(11 | (1<<8));
247391 break;
247392 }else{
247393 int nShift = iNext - 4;
247394 int nPg;
247395
247396 int nIdx = 0;
247397 u8 *aIdx = 0;
247398
247399 /* Unless the current page footer is 0 bytes in size (in which case
247400 ** the new page footer will be as well), allocate and populate a
247401 ** buffer containing the new page footer. Set stack variables aIdx
247402 ** and nIdx accordingly. */
247403 if( pLeaf->nn>pLeaf->szLeaf ){
247404 int iFirst = 0;
247405 int i1 = pLeaf->szLeaf;
247406 int i2 = 0;
247407
247408 i1 += fts5GetVarint32(&aPg[i1], iFirst)sqlite3Fts5GetVarint32(&aPg[i1],(u32*)&(iFirst));
247409 if( iFirst<iNext ){
247410 p->rc = FTS5_CORRUPT(11 | (1<<8));
247411 break;
247412 }
247413 aIdx = sqlite3Fts5MallocZero(&p->rc, (pLeaf->nn-pLeaf->szLeaf)+2);
247414 if( aIdx==0 ) break;
247415 i2 = sqlite3Fts5PutVarint(aIdx, iFirst-nShift);
247416 if( i1<pLeaf->nn ){
247417 memcpy(&aIdx[i2], &aPg[i1], pLeaf->nn-i1);
247418 i2 += (pLeaf->nn-i1);
247419 }
247420 nIdx = i2;
247421 }
247422
247423 /* Modify the contents of buffer aPg[]. Set nPg to the new size
247424 ** in bytes. The new page is always smaller than the old. */
247425 nPg = pLeaf->szLeaf - nShift;
247426 memmove(&aPg[4], &aPg[4+nShift], nPg-4);
247427 fts5PutU16(&aPg[2], nPg);
247428 if( fts5GetU16(&aPg[0]) ) fts5PutU16(&aPg[0], 4);
247429 if( nIdx>0 ){
247430 memcpy(&aPg[nPg], aIdx, nIdx);
247431 nPg += nIdx;
247432 }
247433 sqlite3_free(aIdx);
247434
247435 /* Write the new page to disk and exit the loop */
247436 assert( nPg>4 || fts5GetU16(aPg)==0 )((void) (0));
247437 fts5DataWrite(p, iRowid, aPg, nPg);
247438 break;
247439 }
247440 }
247441 fts5DataRelease(pLeaf);
247442}
247443
247444/*
247445** Completely remove the entry that pSeg currently points to from
247446** the database.
247447*/
247448static void fts5DoSecureDelete(
247449 Fts5Index *p,
247450 Fts5SegIter *pSeg
247451){
247452 const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE1);
247453 int iSegid = pSeg->pSeg->iSegid;
247454 u8 *aPg = pSeg->pLeaf->p;
247455 int nPg = pSeg->pLeaf->nn;
247456 int iPgIdx = pSeg->pLeaf->szLeaf;
247457
247458 u64 iDelta = 0;
247459 int iNextOff = 0;
247460 int iOff = 0;
247461 int nIdx = 0;
247462 u8 *aIdx = 0;
247463 int bLastInDoclist = 0;
247464 int iIdx = 0;
247465 int iStart = 0;
247466 int iDelKeyOff = 0; /* Offset of deleted key, if any */
247467
247468 nIdx = nPg-iPgIdx;
247469 aIdx = sqlite3Fts5MallocZero(&p->rc, nIdx+16);
247470 if( p->rc ) return;
247471 memcpy(aIdx, &aPg[iPgIdx], nIdx);
247472
247473 /* At this point segment iterator pSeg points to the entry
247474 ** this function should remove from the b-tree segment.
247475 **
247476 ** In detail=full or detail=column mode, pSeg->iLeafOffset is the
247477 ** offset of the first byte in the position-list for the entry to
247478 ** remove. Immediately before this comes two varints that will also
247479 ** need to be removed:
247480 **
247481 ** + the rowid or delta rowid value for the entry, and
247482 ** + the size of the position list in bytes.
247483 **
247484 ** Or, in detail=none mode, there is a single varint prior to
247485 ** pSeg->iLeafOffset - the rowid or delta rowid value.
247486 **
247487 ** This block sets the following variables:
247488 **
247489 ** iStart:
247490 ** The offset of the first byte of the rowid or delta-rowid
247491 ** value for the doclist entry being removed.
247492 **
247493 ** iDelta:
247494 ** The value of the rowid or delta-rowid value for the doclist
247495 ** entry being removed.
247496 **
247497 ** iNextOff:
247498 ** The offset of the next entry following the position list
247499 ** for the one being removed. If the position list for this
247500 ** entry overflows onto the next leaf page, this value will be
247501 ** greater than pLeaf->szLeaf.
247502 */
247503 {
247504 int iSOP; /* Start-Of-Position-list */
247505 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno ){
247506 iStart = pSeg->iTermLeafOffset;
247507 }else{
247508 iStart = fts5GetU16(&aPg[0]);
247509 }
247510
247511 iSOP = iStart + fts5GetVarintsqlite3Fts5GetVarint(&aPg[iStart], &iDelta);
247512 assert_nc( iSOP<=pSeg->iLeafOffset )((void) (0));
247513
247514 if( bDetailNone ){
247515 while( iSOP<pSeg->iLeafOffset ){
247516 if( aPg[iSOP]==0x00 ) iSOP++;
247517 if( aPg[iSOP]==0x00 ) iSOP++;
247518 iStart = iSOP;
247519 iSOP = iStart + fts5GetVarintsqlite3Fts5GetVarint(&aPg[iStart], &iDelta);
247520 }
247521
247522 iNextOff = iSOP;
247523 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
247524 if( iNextOff<pSeg->iEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++;
247525
247526 }else{
247527 int nPos = 0;
247528 iSOP += fts5GetVarint32(&aPg[iSOP], nPos)sqlite3Fts5GetVarint32(&aPg[iSOP],(u32*)&(nPos));
247529 while( iSOP<pSeg->iLeafOffset ){
247530 iStart = iSOP + (nPos/2);
247531 iSOP = iStart + fts5GetVarintsqlite3Fts5GetVarint(&aPg[iStart], &iDelta);
247532 iSOP += fts5GetVarint32(&aPg[iSOP], nPos)sqlite3Fts5GetVarint32(&aPg[iSOP],(u32*)&(nPos));
247533 }
247534 assert_nc( iSOP==pSeg->iLeafOffset )((void) (0));
247535 iNextOff = pSeg->iLeafOffset + pSeg->nPos;
247536 }
247537 }
247538
247539 iOff = iStart;
247540
247541 /* If the position-list for the entry being removed flows over past
247542 ** the end of this page, delete the portion of the position-list on the
247543 ** next page and beyond.
247544 **
247545 ** Set variable bLastInDoclist to true if this entry happens
247546 ** to be the last rowid in the doclist for its term. */
247547 if( iNextOff>=iPgIdx ){
247548 int pgno = pSeg->iLeafPgno+1;
247549 fts5SecureDeleteOverflow(p, pSeg->pSeg, pgno, &bLastInDoclist);
247550 iNextOff = iPgIdx;
247551 }
247552
247553 if( pSeg->bDel==0 ){
247554 if( iNextOff!=iPgIdx ){
247555 /* Loop through the page-footer. If iNextOff (offset of the
247556 ** entry following the one we are removing) is equal to the
247557 ** offset of a key on this page, then the entry is the last
247558 ** in its doclist. */
247559 int iKeyOff = 0;
247560 for(iIdx=0; iIdx<nIdx; /* no-op */){
247561 u32 iVal = 0;
247562 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal)sqlite3Fts5GetVarint32(&aIdx[iIdx],(u32*)&(iVal));
247563 iKeyOff += iVal;
247564 if( iKeyOff==iNextOff ){
247565 bLastInDoclist = 1;
247566 }
247567 }
247568 }
247569
247570 /* If this is (a) the first rowid on a page and (b) is not followed by
247571 ** another position list on the same page, set the "first-rowid" field
247572 ** of the header to 0. */
247573 if( fts5GetU16(&aPg[0])==iStart && (bLastInDoclist || iNextOff==iPgIdx) ){
247574 fts5PutU16(&aPg[0], 0);
247575 }
247576 }
247577
247578 if( pSeg->bDel ){
247579 iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta);
247580 aPg[iOff++] = 0x01;
247581 }else if( bLastInDoclist==0 ){
247582 if( iNextOff!=iPgIdx ){
247583 u64 iNextDelta = 0;
247584 iNextOff += fts5GetVarintsqlite3Fts5GetVarint(&aPg[iNextOff], &iNextDelta);
247585 iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta + iNextDelta);
247586 }
247587 }else if(
247588 pSeg->iLeafPgno==pSeg->iTermLeafPgno
247589 && iStart==pSeg->iTermLeafOffset
247590 ){
247591 /* The entry being removed was the only position list in its
247592 ** doclist. Therefore the term needs to be removed as well. */
247593 int iKey = 0;
247594 int iKeyOff = 0;
247595
247596 /* Set iKeyOff to the offset of the term that will be removed - the
247597 ** last offset in the footer that is not greater than iStart. */
247598 for(iIdx=0; iIdx<nIdx; iKey++){
247599 u32 iVal = 0;
247600 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal)sqlite3Fts5GetVarint32(&aIdx[iIdx],(u32*)&(iVal));
247601 if( (iKeyOff+iVal)>(u32)iStart ) break;
247602 iKeyOff += iVal;
247603 }
247604 assert_nc( iKey>=1 )((void) (0));
247605
247606 /* Set iDelKeyOff to the value of the footer entry to remove from
247607 ** the page. */
247608 iDelKeyOff = iOff = iKeyOff;
247609
247610 if( iNextOff!=iPgIdx ){
247611 /* This is the only position-list associated with the term, and there
247612 ** is another term following it on this page. So the subsequent term
247613 ** needs to be moved to replace the term associated with the entry
247614 ** being removed. */
247615 int nPrefix = 0;
247616 int nSuffix = 0;
247617 int nPrefix2 = 0;
247618 int nSuffix2 = 0;
247619
247620 iDelKeyOff = iNextOff;
247621 iNextOff += fts5GetVarint32(&aPg[iNextOff], nPrefix2)sqlite3Fts5GetVarint32(&aPg[iNextOff],(u32*)&(nPrefix2
))
;
247622 iNextOff += fts5GetVarint32(&aPg[iNextOff], nSuffix2)sqlite3Fts5GetVarint32(&aPg[iNextOff],(u32*)&(nSuffix2
))
;
247623
247624 if( iKey!=1 ){
247625 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nPrefix)sqlite3Fts5GetVarint32(&aPg[iKeyOff],(u32*)&(nPrefix)
)
;
247626 }
247627 iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nSuffix)sqlite3Fts5GetVarint32(&aPg[iKeyOff],(u32*)&(nSuffix)
)
;
247628
247629 nPrefix = MIN(nPrefix, nPrefix2)((nPrefix)<(nPrefix2)?(nPrefix):(nPrefix2));
247630 nSuffix = (nPrefix2 + nSuffix2) - nPrefix;
247631
247632 if( (iKeyOff+nSuffix)>iPgIdx || (iNextOff+nSuffix2)>iPgIdx ){
247633 p->rc = FTS5_CORRUPT(11 | (1<<8));
247634 }else{
247635 if( iKey!=1 ){
247636 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix);
247637 }
247638 iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix);
247639 if( nPrefix2>pSeg->term.n ){
247640 p->rc = FTS5_CORRUPT(11 | (1<<8));
247641 }else if( nPrefix2>nPrefix ){
247642 memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix);
247643 iOff += (nPrefix2-nPrefix);
247644 }
247645 memmove(&aPg[iOff], &aPg[iNextOff], nSuffix2);
247646 iOff += nSuffix2;
247647 iNextOff += nSuffix2;
247648 }
247649 }
247650 }else if( iStart==4 ){
247651 int iPgno;
247652
247653 assert_nc( pSeg->iLeafPgno>pSeg->iTermLeafPgno )((void) (0));
247654 /* The entry being removed may be the only position list in
247655 ** its doclist. */
247656 for(iPgno=pSeg->iLeafPgno-1; iPgno>pSeg->iTermLeafPgno; iPgno-- ){
247657 Fts5Data *pPg = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPgno)) )
);
247658 int bEmpty = (pPg && pPg->nn==4);
247659 fts5DataRelease(pPg);
247660 if( bEmpty==0 ) break;
247661 }
247662
247663 if( iPgno==pSeg->iTermLeafPgno ){
247664 i64 iId = FTS5_SEGMENT_ROWID(iSegid, pSeg->iTermLeafPgno)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pSeg->iTermLeafPgno
)) )
;
247665 Fts5Data *pTerm = fts5DataRead(p, iId);
247666 if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){
247667 u8 *aTermIdx = &pTerm->p[pTerm->szLeaf];
247668 int nTermIdx = pTerm->nn - pTerm->szLeaf;
247669 int iTermIdx = 0;
247670 int iTermOff = 0;
247671
247672 while( 1 ){
247673 u32 iVal = 0;
247674 int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal)sqlite3Fts5GetVarint32(&aTermIdx[iTermIdx],(u32*)&(iVal
))
;
247675 iTermOff += iVal;
247676 if( (iTermIdx+nByte)>=nTermIdx ) break;
247677 iTermIdx += nByte;
247678 }
247679 nTermIdx = iTermIdx;
247680
247681 memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx);
247682 fts5PutU16(&pTerm->p[2], iTermOff);
247683
247684 fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx);
247685 if( nTermIdx==0 ){
247686 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno);
247687 }
247688 }
247689 fts5DataRelease(pTerm);
247690 }
247691 }
247692
247693 /* Assuming no error has occurred, this block does final edits to the
247694 ** leaf page before writing it back to disk. Input variables are:
247695 **
247696 ** nPg: Total initial size of leaf page.
247697 ** iPgIdx: Initial offset of page footer.
247698 **
247699 ** iOff: Offset to move data to
247700 ** iNextOff: Offset to move data from
247701 */
247702 if( p->rc==SQLITE_OK0 ){
247703 const int nMove = nPg - iNextOff; /* Number of bytes to move */
247704 int nShift = iNextOff - iOff; /* Distance to move them */
247705
247706 int iPrevKeyOut = 0;
247707 int iKeyIn = 0;
247708
247709 memmove(&aPg[iOff], &aPg[iNextOff], nMove);
247710 iPgIdx -= nShift;
247711 nPg = iPgIdx;
247712 fts5PutU16(&aPg[2], iPgIdx);
247713
247714 for(iIdx=0; iIdx<nIdx; /* no-op */){
247715 u32 iVal = 0;
247716 iIdx += fts5GetVarint32(&aIdx[iIdx], iVal)sqlite3Fts5GetVarint32(&aIdx[iIdx],(u32*)&(iVal));
247717 iKeyIn += iVal;
247718 if( iKeyIn!=iDelKeyOff ){
247719 int iKeyOut = (iKeyIn - (iKeyIn>iOff ? nShift : 0));
247720 nPg += sqlite3Fts5PutVarint(&aPg[nPg], iKeyOut - iPrevKeyOut);
247721 iPrevKeyOut = iKeyOut;
247722 }
247723 }
247724
247725 if( iPgIdx==nPg && nIdx>0 && pSeg->iLeafPgno!=1 ){
247726 fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iLeafPgno);
247727 }
247728
247729 assert_nc( nPg>4 || fts5GetU16(aPg)==0 )((void) (0));
247730 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);
247731 }
247732 sqlite3_free(aIdx);
247733}
247734
247735/*
247736** This is called as part of flushing a delete to disk in 'secure-delete'
247737** mode. It edits the segments within the database described by argument
247738** pStruct to remove the entries for term zTerm, rowid iRowid.
247739*/
247740static void fts5FlushSecureDelete(
247741 Fts5Index *p,
247742 Fts5Structure *pStruct,
247743 const char *zTerm,
247744 int nTerm,
247745 i64 iRowid
247746){
247747 const int f = FTS5INDEX_QUERY_SKIPHASH0x0040;
247748 Fts5Iter *pIter = 0; /* Used to find term instance */
247749
247750 fts5MultiIterNew(p, pStruct, f, 0, (const u8*)zTerm, nTerm, -1, 0, &pIter);
247751 if( fts5MultiIterEof(p, pIter)==0 ){
247752 i64 iThis = fts5MultiIterRowid(pIter);
247753 if( iThis<iRowid ){
247754 fts5MultiIterNextFrom(p, pIter, iRowid);
247755 }
247756
247757 if( p->rc==SQLITE_OK0
247758 && fts5MultiIterEof(p, pIter)==0
247759 && iRowid==fts5MultiIterRowid(pIter)
247760 ){
247761 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
247762 fts5DoSecureDelete(p, pSeg);
247763 }
247764 }
247765
247766 fts5MultiIterFree(pIter);
247767}
247768
247769
247770/*
247771** Flush the contents of in-memory hash table iHash to a new level-0
247772** segment on disk. Also update the corresponding structure record.
247773**
247774** If an error occurs, set the Fts5Index.rc error code. If an error has
247775** already occurred, this function is a no-op.
247776*/
247777static void fts5FlushOneHash(Fts5Index *p){
247778 Fts5Hash *pHash = p->pHash;
247779 Fts5Structure *pStruct;
247780 int iSegid;
247781 int pgnoLast = 0; /* Last leaf page number in segment */
247782
247783 /* Obtain a reference to the index structure and allocate a new segment-id
247784 ** for the new level-0 segment. */
247785 pStruct = fts5StructureRead(p);
247786 fts5StructureInvalidate(p);
247787
247788 if( sqlite3Fts5HashIsEmpty(pHash)==0 ){
247789 iSegid = fts5AllocateSegid(p, pStruct);
247790 if( iSegid ){
247791 const int pgsz = p->pConfig->pgsz;
247792 int eDetail = p->pConfig->eDetail;
247793 int bSecureDelete = p->pConfig->bSecureDelete;
247794 Fts5StructureSegment *pSeg; /* New segment within pStruct */
247795 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
247796 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
247797
247798 Fts5SegWriter writer;
247799 fts5WriteInit(p, &writer, iSegid);
247800
247801 pBuf = &writer.writer.buf;
247802 pPgidx = &writer.writer.pgidx;
247803
247804 /* fts5WriteInit() should have initialized the buffers to (most likely)
247805 ** the maximum space required. */
247806 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
247807 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
247808
247809 /* Begin scanning through hash table entries. This loop runs once for each
247810 ** term/doclist currently stored within the hash table. */
247811 if( p->rc==SQLITE_OK0 ){
247812 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
247813 }
247814 while( p->rc==SQLITE_OK0 && 0==sqlite3Fts5HashScanEof(pHash) ){
247815 const char *zTerm; /* Buffer containing term */
247816 int nTerm; /* Size of zTerm in bytes */
247817 const u8 *pDoclist; /* Pointer to doclist for this term */
247818 int nDoclist; /* Size of doclist in bytes */
247819
247820 /* Get the term and doclist for this entry. */
247821 sqlite3Fts5HashScanEntry(pHash, &zTerm, &nTerm, &pDoclist, &nDoclist);
247822 if( bSecureDelete==0 ){
247823 fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm);
247824 if( p->rc!=SQLITE_OK0 ) break;
247825 assert( writer.bFirstRowidInPage==0 )((void) (0));
247826 }
247827
247828 if( !bSecureDelete && pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
247829 /* The entire doclist will fit on the current leaf. */
247830 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pDoclist
, nDoclist); (pBuf)->n += nDoclist; }
;
247831 }else{
247832 int bTermWritten = !bSecureDelete;
247833 i64 iRowid = 0;
247834 i64 iPrev = 0;
247835 int iOff = 0;
247836
247837 /* The entire doclist will not fit on this leaf. The following
247838 ** loop iterates through the poslists that make up the current
247839 ** doclist. */
247840 while( p->rc==SQLITE_OK0 && iOff<nDoclist ){
247841 u64 iDelta = 0;
247842 iOff += fts5GetVarintsqlite3Fts5GetVarint(&pDoclist[iOff], &iDelta);
247843 iRowid += iDelta;
247844
247845 /* If in secure delete mode, and if this entry in the poslist is
247846 ** in fact a delete, then edit the existing segments directly
247847 ** using fts5FlushSecureDelete(). */
247848 if( bSecureDelete ){
247849 if( eDetail==FTS5_DETAIL_NONE1 ){
247850 if( iOff<nDoclist && pDoclist[iOff]==0x00 ){
247851 fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid);
247852 iOff++;
247853 if( iOff<nDoclist && pDoclist[iOff]==0x00 ){
247854 iOff++;
247855 nDoclist = 0;
247856 }else{
247857 continue;
247858 }
247859 }
247860 }else if( (pDoclist[iOff] & 0x01) ){
247861 fts5FlushSecureDelete(p, pStruct, zTerm, nTerm, iRowid);
247862 if( p->rc!=SQLITE_OK0 || pDoclist[iOff]==0x01 ){
247863 iOff++;
247864 continue;
247865 }
247866 }
247867 }
247868
247869 if( p->rc==SQLITE_OK0 && bTermWritten==0 ){
247870 fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm);
247871 bTermWritten = 1;
247872 assert( p->rc!=SQLITE_OK || writer.bFirstRowidInPage==0 )((void) (0));
247873 }
247874
247875 if( writer.bFirstRowidInPage ){
247876 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
247877 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
247878 writer.bFirstRowidInPage = 0;
247879 fts5WriteDlidxAppend(p, &writer, iRowid);
247880 }else{
247881 u64 iRowidDelta = (u64)iRowid - (u64)iPrev;
247882 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowidDelta);
247883 }
247884 if( p->rc!=SQLITE_OK0 ) break;
247885 assert( pBuf->n<=pBuf->nSpace )((void) (0));
247886 iPrev = iRowid;
247887
247888 if( eDetail==FTS5_DETAIL_NONE1 ){
247889 if( iOff<nDoclist && pDoclist[iOff]==0 ){
247890 pBuf->p[pBuf->n++] = 0;
247891 iOff++;
247892 if( iOff<nDoclist && pDoclist[iOff]==0 ){
247893 pBuf->p[pBuf->n++] = 0;
247894 iOff++;
247895 }
247896 }
247897 if( (pBuf->n + pPgidx->n)>=pgsz ){
247898 fts5WriteFlushLeaf(p, &writer);
247899 }
247900 }else{
247901 int bDel = 0;
247902 int nPos = 0;
247903 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDel);
247904 if( bDel && bSecureDelete ){
247905 fts5BufferAppendVarint(&p->rc, pBuf, nPos*2)sqlite3Fts5BufferAppendVarint(&p->rc,pBuf,(i64)nPos*2);
247906 iOff += nCopy;
247907 nCopy = nPos;
247908 }else{
247909 nCopy += nPos;
247910 }
247911 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
247912 /* The entire poslist will fit on the current leaf. So copy
247913 ** it in one go. */
247914 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], &
pDoclist[iOff], nCopy); (pBuf)->n += nCopy; }
;
247915 }else{
247916 /* The entire poslist will not fit on this leaf. So it needs
247917 ** to be broken into sections. The only qualification being
247918 ** that each varint must be stored contiguously. */
247919 const u8 *pPoslist = &pDoclist[iOff];
247920 int iPos = 0;
247921 while( p->rc==SQLITE_OK0 ){
247922 int nSpace = pgsz - pBuf->n - pPgidx->n;
247923 int n = 0;
247924 if( (nCopy - iPos)<=nSpace ){
247925 n = nCopy - iPos;
247926 }else{
247927 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
247928 }
247929 assert( n>0 )((void) (0));
247930 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], &
pPoslist[iPos], n); (pBuf)->n += n; }
;
247931 iPos += n;
247932 if( (pBuf->n + pPgidx->n)>=pgsz ){
247933 fts5WriteFlushLeaf(p, &writer);
247934 }
247935 if( iPos>=nCopy ) break;
247936 }
247937 }
247938 iOff += nCopy;
247939 }
247940 }
247941 }
247942
247943 /* TODO2: Doclist terminator written here. */
247944 /* pBuf->p[pBuf->n++] = '\0'; */
247945 assert( pBuf->n<=pBuf->nSpace )((void) (0));
247946 if( p->rc==SQLITE_OK0 ) sqlite3Fts5HashScanNext(pHash);
247947 }
247948 fts5WriteFinish(p, &writer, &pgnoLast);
247949
247950 assert( p->rc!=SQLITE_OK || bSecureDelete || pgnoLast>0 )((void) (0));
247951 if( pgnoLast>0 ){
247952 /* Update the Fts5Structure. It is written back to the database by the
247953 ** fts5StructureRelease() call below. */
247954 if( pStruct->nLevel==0 ){
247955 fts5StructureAddLevel(&p->rc, &pStruct);
247956 }
247957 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
247958 if( p->rc==SQLITE_OK0 ){
247959 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
247960 pSeg->iSegid = iSegid;
247961 pSeg->pgnoFirst = 1;
247962 pSeg->pgnoLast = pgnoLast;
247963 if( pStruct->nOriginCntr>0 ){
247964 pSeg->iOrigin1 = pStruct->nOriginCntr;
247965 pSeg->iOrigin2 = pStruct->nOriginCntr;
247966 pSeg->nEntry = p->nPendingRow;
247967 pStruct->nOriginCntr++;
247968 }
247969 pStruct->nSegment++;
247970 }
247971 fts5StructurePromote(p, 0, pStruct);
247972 }
247973 }
247974 }
247975
247976 fts5IndexAutomerge(p, &pStruct, pgnoLast + p->nContentlessDelete);
247977 fts5IndexCrisismerge(p, &pStruct);
247978 fts5StructureWrite(p, pStruct);
247979 fts5StructureRelease(pStruct);
247980}
247981
247982/*
247983** Flush any data stored in the in-memory hash tables to the database.
247984*/
247985static void fts5IndexFlush(Fts5Index *p){
247986 /* Unless it is empty, flush the hash table to disk */
247987 if( p->flushRc ){
247988 p->rc = p->flushRc;
247989 return;
247990 }
247991 if( p->nPendingData || p->nContentlessDelete ){
247992 assert( p->pHash )((void) (0));
247993 fts5FlushOneHash(p);
247994 if( p->rc==SQLITE_OK0 ){
247995 sqlite3Fts5HashClear(p->pHash);
247996 p->nPendingData = 0;
247997 p->nPendingRow = 0;
247998 p->nContentlessDelete = 0;
247999 }else if( p->nPendingData || p->nContentlessDelete ){
248000 p->flushRc = p->rc;
248001 }
248002 }
248003}
248004
248005static Fts5Structure *fts5IndexOptimizeStruct(
248006 Fts5Index *p,
248007 Fts5Structure *pStruct
248008){
248009 Fts5Structure *pNew = 0;
248010 sqlite3_int64 nByte = sizeof(Fts5Structure);
248011 int nSeg = pStruct->nSegment;
248012 int i;
248013
248014 /* Figure out if this structure requires optimization. A structure does
248015 ** not require optimization if either:
248016 **
248017 ** 1. it consists of fewer than two segments, or
248018 ** 2. all segments are on the same level, or
248019 ** 3. all segments except one are currently inputs to a merge operation.
248020 **
248021 ** In the first case, if there are no tombstone hash pages, return NULL. In
248022 ** the second, increment the ref-count on *pStruct and return a copy of the
248023 ** pointer to it.
248024 */
248025 if( nSeg==0 ) return 0;
248026 for(i=0; i<pStruct->nLevel; i++){
248027 int nThis = pStruct->aLevel[i].nSeg;
248028 int nMerge = pStruct->aLevel[i].nMerge;
248029 if( nThis>0 && (nThis==nSeg || (nThis==nSeg-1 && nMerge==nThis)) ){
248030 if( nSeg==1 && nThis==1 && pStruct->aLevel[i].aSeg[0].nPgTombstone==0 ){
248031 return 0;
248032 }
248033 fts5StructureRef(pStruct);
248034 return pStruct;
248035 }
248036 assert( pStruct->aLevel[i].nMerge<=nThis )((void) (0));
248037 }
248038
248039 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
248040 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
248041
248042 if( pNew ){
248043 Fts5StructureLevel *pLvl;
248044 nByte = nSeg * sizeof(Fts5StructureSegment);
248045 pNew->nLevel = MIN(pStruct->nLevel+1, FTS5_MAX_LEVEL)((pStruct->nLevel+1)<(64)?(pStruct->nLevel+1):(64));
248046 pNew->nRef = 1;
248047 pNew->nWriteCounter = pStruct->nWriteCounter;
248048 pNew->nOriginCntr = pStruct->nOriginCntr;
248049 pLvl = &pNew->aLevel[pNew->nLevel-1];
248050 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
248051 if( pLvl->aSeg ){
248052 int iLvl, iSeg;
248053 int iSegOut = 0;
248054 /* Iterate through all segments, from oldest to newest. Add them to
248055 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
248056 ** segment in the data structure. */
248057 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
248058 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
248059 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
248060 iSegOut++;
248061 }
248062 }
248063 pNew->nSegment = pLvl->nSeg = nSeg;
248064 }else{
248065 sqlite3_free(pNew);
248066 pNew = 0;
248067 }
248068 }
248069
248070 return pNew;
248071}
248072
248073static int sqlite3Fts5IndexOptimize(Fts5Index *p){
248074 Fts5Structure *pStruct;
248075 Fts5Structure *pNew = 0;
248076
248077 assert( p->rc==SQLITE_OK )((void) (0));
248078 fts5IndexFlush(p);
248079 assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 )((void) (0));
248080 pStruct = fts5StructureRead(p);
248081 assert( p->rc!=SQLITE_OK || pStruct!=0 )((void) (0));
248082 fts5StructureInvalidate(p);
248083
248084 if( pStruct ){
248085 pNew = fts5IndexOptimizeStruct(p, pStruct);
248086 }
248087 fts5StructureRelease(pStruct);
248088
248089 assert( pNew==0 || pNew->nSegment>0 )((void) (0));
248090 if( pNew ){
248091 int iLvl;
248092 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
248093 while( p->rc==SQLITE_OK0 && pNew->aLevel[iLvl].nSeg>0 ){
248094 int nRem = FTS5_OPT_WORK_UNIT1000;
248095 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
248096 }
248097
248098 fts5StructureWrite(p, pNew);
248099 fts5StructureRelease(pNew);
248100 }
248101
248102 return fts5IndexReturn(p);
248103}
248104
248105/*
248106** This is called to implement the special "VALUES('merge', $nMerge)"
248107** INSERT command.
248108*/
248109static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
248110 Fts5Structure *pStruct = 0;
248111
248112 fts5IndexFlush(p);
248113 pStruct = fts5StructureRead(p);
248114 if( pStruct ){
248115 int nMin = p->pConfig->nUsermerge;
248116 fts5StructureInvalidate(p);
248117 if( nMerge<0 ){
248118 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
248119 fts5StructureRelease(pStruct);
248120 pStruct = pNew;
248121 nMin = 1;
248122 nMerge = nMerge*-1;
248123 }
248124 if( pStruct && pStruct->nLevel ){
248125 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
248126 fts5StructureWrite(p, pStruct);
248127 }
248128 }
248129 fts5StructureRelease(pStruct);
248130 }
248131 return fts5IndexReturn(p);
248132}
248133
248134static void fts5AppendRowid(
248135 Fts5Index *p,
248136 u64 iDelta,
248137 Fts5Iter *pUnused,
248138 Fts5Buffer *pBuf
248139){
248140 UNUSED_PARAM(pUnused)(void)(pUnused);
248141 fts5BufferAppendVarint(&p->rc, pBuf, iDelta)sqlite3Fts5BufferAppendVarint(&p->rc,pBuf,(i64)iDelta);
248142}
248143
248144static void fts5AppendPoslist(
248145 Fts5Index *p,
248146 u64 iDelta,
248147 Fts5Iter *pMulti,
248148 Fts5Buffer *pBuf
248149){
248150 int nData = pMulti->base.nData;
248151 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING8;
248152 assert( nData>0 )((void) (0));
248153 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) )
){
248154 fts5BufferSafeAppendVarint(pBuf, iDelta){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iDelta)); ((void) (0)); }
;
248155 fts5BufferSafeAppendVarint(pBuf, nData*2){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (nData*2)); ((void) (0)); }
;
248156 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData){ ((void) (0)); memcpy(&(pBuf)->p[(pBuf)->n], pMulti
->base.pData, nData); (pBuf)->n += nData; }
;
248157 memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING8);
248158 }
248159}
248160
248161
248162static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
248163 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
248164
248165 assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) )((void) (0));
248166 if( p>=pIter->aEof ){
248167 pIter->aPoslist = 0;
248168 }else{
248169 i64 iDelta;
248170
248171 p += fts5GetVarintsqlite3Fts5GetVarint(p, (u64*)&iDelta);
248172 pIter->iRowid += iDelta;
248173
248174 /* Read position list size */
248175 if( p[0] & 0x80 ){
248176 int nPos;
248177 pIter->nSize = fts5GetVarint32(p, nPos)sqlite3Fts5GetVarint32(p,(u32*)&(nPos));
248178 pIter->nPoslist = (nPos>>1);
248179 }else{
248180 pIter->nPoslist = ((int)(p[0])) >> 1;
248181 pIter->nSize = 1;
248182 }
248183
248184 pIter->aPoslist = p;
248185 if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){
248186 pIter->aPoslist = 0;
248187 }
248188 }
248189}
248190
248191static void fts5DoclistIterInit(
248192 Fts5Buffer *pBuf,
248193 Fts5DoclistIter *pIter
248194){
248195 memset(pIter, 0, sizeof(*pIter));
248196 if( pBuf->n>0 ){
248197 pIter->aPoslist = pBuf->p;
248198 pIter->aEof = &pBuf->p[pBuf->n];
248199 fts5DoclistIterNext(pIter);
248200 }
248201}
248202
248203#if 0
248204/*
248205** Append a doclist to buffer pBuf.
248206**
248207** This function assumes that space within the buffer has already been
248208** allocated.
248209*/
248210static 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); }
248211 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); }
248212 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); }
248213 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); }
248214){ ((void) (0)); { ((Fts5Buffer *pBuf))->n += sqlite3Fts5PutVarint
(&((Fts5Buffer *pBuf))->p[((Fts5Buffer *pBuf))->n],
((u64)(i64 iRowid) - (u64)(i64 *piLastRowid))); ((void) (0))
; }; (i64 *piLastRowid) = (i64 iRowid); }
{
248215 assert( pBuf->n!=0 || (*piLastRowid)==0 )((void) (0));
248216 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid){ (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf
)->n], (iRowid - *piLastRowid)); ((void) (0)); }
;
248217 *piLastRowid = iRowid;
248218}
248219#endif
248220
248221#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid){ ((void) (0)); { ((pBuf))->n += sqlite3Fts5PutVarint(&
((pBuf))->p[((pBuf))->n], ((u64)(iRowid) - (u64)(iLastRowid
))); ((void) (0)); }; (iLastRowid) = (iRowid); }
{ \
248222 assert( (pBuf)->n!=0 || (iLastRowid)==0 )((void) (0)); \
248223 fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)){ ((pBuf))->n += sqlite3Fts5PutVarint(&((pBuf))->p[
((pBuf))->n], ((u64)(iRowid) - (u64)(iLastRowid))); ((void
) (0)); }
; \
248224 (iLastRowid) = (iRowid); \
248225}
248226
248227/*
248228** Swap the contents of buffer *p1 with that of *p2.
248229*/
248230static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
248231 Fts5Buffer tmp = *p1;
248232 *p1 = *p2;
248233 *p2 = tmp;
248234}
248235
248236static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
248237 int i = *piOff;
248238 if( i>=pBuf->n ){
248239 *piOff = -1;
248240 }else{
248241 u64 iVal;
248242 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
248243 *piRowid += iVal;
248244 }
248245}
248246
248247/*
248248** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
248249** In this case the buffers consist of a delta-encoded list of rowids only.
248250*/
248251static void fts5MergeRowidLists(
248252 Fts5Index *p, /* FTS5 backend object */
248253 Fts5Buffer *p1, /* First list to merge */
248254 int nBuf, /* Number of entries in apBuf[] */
248255 Fts5Buffer *aBuf /* Array of other lists to merge into p1 */
248256){
248257 int i1 = 0;
248258 int i2 = 0;
248259 i64 iRowid1 = 0;
248260 i64 iRowid2 = 0;
248261 i64 iOut = 0;
248262 Fts5Buffer *p2 = &aBuf[0];
248263 Fts5Buffer out;
248264
248265 (void)nBuf;
248266 memset(&out, 0, sizeof(out));
248267 assert( nBuf==1 )((void) (0));
248268 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
248269 if( p->rc ) return;
248270
248271 fts5NextRowid(p1, &i1, &iRowid1);
248272 fts5NextRowid(p2, &i2, &iRowid2);
248273 while( i1>=0 || i2>=0 ){
248274 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
248275 assert( iOut==0 || iRowid1>iOut )((void) (0));
248276 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], (iRowid1 - iOut)); ((void) (0)); }
;
248277 iOut = iRowid1;
248278 fts5NextRowid(p1, &i1, &iRowid1);
248279 }else{
248280 assert( iOut==0 || iRowid2>iOut )((void) (0));
248281 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], (iRowid2 - iOut)); ((void) (0)); }
;
248282 iOut = iRowid2;
248283 if( i1>=0 && iRowid1==iRowid2 ){
248284 fts5NextRowid(p1, &i1, &iRowid1);
248285 }
248286 fts5NextRowid(p2, &i2, &iRowid2);
248287 }
248288 }
248289
248290 fts5BufferSwap(&out, p1);
248291 fts5BufferFree(&out)sqlite3Fts5BufferFree(&out);
248292}
248293
248294typedef struct PrefixMerger PrefixMerger;
248295struct PrefixMerger {
248296 Fts5DoclistIter iter; /* Doclist iterator */
248297 i64 iPos; /* For iterating through a position list */
248298 int iOff;
248299 u8 *aPos;
248300 PrefixMerger *pNext; /* Next in docid/poslist order */
248301};
248302
248303static void fts5PrefixMergerInsertByRowid(
248304 PrefixMerger **ppHead,
248305 PrefixMerger *p
248306){
248307 if( p->iter.aPoslist ){
248308 PrefixMerger **pp = ppHead;
248309 while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){
248310 pp = &(*pp)->pNext;
248311 }
248312 p->pNext = *pp;
248313 *pp = p;
248314 }
248315}
248316
248317static void fts5PrefixMergerInsertByPosition(
248318 PrefixMerger **ppHead,
248319 PrefixMerger *p
248320){
248321 if( p->iPos>=0 ){
248322 PrefixMerger **pp = ppHead;
248323 while( *pp && p->iPos>(*pp)->iPos ){
248324 pp = &(*pp)->pNext;
248325 }
248326 p->pNext = *pp;
248327 *pp = p;
248328 }
248329}
248330
248331
248332/*
248333** Array aBuf[] contains nBuf doclists. These are all merged in with the
248334** doclist in buffer p1.
248335*/
248336static void fts5MergePrefixLists(
248337 Fts5Index *p, /* FTS5 backend object */
248338 Fts5Buffer *p1, /* First list to merge */
248339 int nBuf, /* Number of buffers in array aBuf[] */
248340 Fts5Buffer *aBuf /* Other lists to merge in */
248341){
248342#define fts5PrefixMergerNextPosition(p)sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&
(p)->iOff,&(p)->iPos)
\
248343 sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos)
248344#define FTS5_MERGE_NLIST16 16
248345 PrefixMerger aMerger[FTS5_MERGE_NLIST16];
248346 PrefixMerger *pHead = 0;
248347 int i;
248348 int nOut = 0;
248349 Fts5Buffer out = {0, 0, 0};
248350 Fts5Buffer tmp = {0, 0, 0};
248351 i64 iLastRowid = 0;
248352
248353 /* Initialize a doclist-iterator for each input buffer. Arrange them in
248354 ** a linked-list starting at pHead in ascending order of rowid. Avoid
248355 ** linking any iterators already at EOF into the linked list at all. */
248356 assert( nBuf+1<=(int)(sizeof(aMerger)/sizeof(aMerger[0])) )((void) (0));
248357 memset(aMerger, 0, sizeof(PrefixMerger)*(nBuf+1));
248358 pHead = &aMerger[nBuf];
248359 fts5DoclistIterInit(p1, &pHead->iter);
248360 for(i=0; i<nBuf; i++){
248361 fts5DoclistIterInit(&aBuf[i], &aMerger[i].iter);
248362 fts5PrefixMergerInsertByRowid(&pHead, &aMerger[i]);
248363 nOut += aBuf[i].n;
248364 }
248365 if( nOut==0 ) return;
248366 nOut += p1->n + 9 + 10*nBuf;
248367
248368 /* The maximum size of the output is equal to the sum of the
248369 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
248370 ** first rowid in one input is a large negative number, and the first in
248371 ** the other a non-negative number, the delta for the non-negative
248372 ** number will be larger on disk than the literal integer value
248373 ** was.
248374 **
248375 ** Or, if the input position-lists are corrupt, then the output might
248376 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
248377 ** (the value PoslistNext64() uses for EOF) as a position and appending
248378 ** it to the output. This can happen at most once for each input
248379 ** position-list, hence (nBuf+1) 10 byte paddings. */
248380 if( sqlite3Fts5BufferSize(&p->rc, &out, nOut) ) return;
248381
248382 while( pHead ){
248383 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); }
;
248384
248385 if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){
248386 /* Merge data from two or more poslists */
248387 i64 iPrev = 0;
248388 int nTmp = FTS5_DATA_ZERO_PADDING8;
248389 int nMerge = 0;
248390 PrefixMerger *pSave = pHead;
248391 PrefixMerger *pThis = 0;
248392 int nTail = 0;
248393
248394 pHead = 0;
248395 while( pSave && pSave->iter.iRowid==iLastRowid ){
248396 PrefixMerger *pNext = pSave->pNext;
248397 pSave->iOff = 0;
248398 pSave->iPos = 0;
248399 pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize];
248400 fts5PrefixMergerNextPosition(pSave)sqlite3Fts5PoslistNext64((pSave)->aPos,(pSave)->iter.nPoslist
,&(pSave)->iOff,&(pSave)->iPos)
;
248401 nTmp += pSave->iter.nPoslist + 10;
248402 nMerge++;
248403 fts5PrefixMergerInsertByPosition(&pHead, pSave);
248404 pSave = pNext;
248405 }
248406
248407 if( pHead==0 || pHead->pNext==0 ){
248408 p->rc = FTS5_CORRUPT(11 | (1<<8));
248409 break;
248410 }
248411
248412 /* See the earlier comment in this function for an explanation of why
248413 ** corrupt input position lists might cause the output to consume
248414 ** at most nMerge*10 bytes of unexpected space. */
248415 if( sqlite3Fts5BufferSize(&p->rc, &tmp, nTmp+nMerge*10) ){
248416 break;
248417 }
248418 fts5BufferZero(&tmp)sqlite3Fts5BufferZero(&tmp);
248419
248420 pThis = pHead;
248421 pHead = pThis->pNext;
248422 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
248423 fts5PrefixMergerNextPosition(pThis)sqlite3Fts5PoslistNext64((pThis)->aPos,(pThis)->iter.nPoslist
,&(pThis)->iOff,&(pThis)->iPos)
;
248424 fts5PrefixMergerInsertByPosition(&pHead, pThis);
248425
248426 while( pHead->pNext ){
248427 pThis = pHead;
248428 if( pThis->iPos!=iPrev ){
248429 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos);
248430 }
248431 fts5PrefixMergerNextPosition(pThis)sqlite3Fts5PoslistNext64((pThis)->aPos,(pThis)->iter.nPoslist
,&(pThis)->iOff,&(pThis)->iPos)
;
248432 pHead = pThis->pNext;
248433 fts5PrefixMergerInsertByPosition(&pHead, pThis);
248434 }
248435
248436 if( pHead->iPos!=iPrev ){
248437 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pHead->iPos);
248438 }
248439 nTail = pHead->iter.nPoslist - pHead->iOff;
248440
248441 /* WRITEPOSLISTSIZE */
248442 assert_nc( tmp.n+nTail<=nTmp )((void) (0));
248443 assert( tmp.n+nTail<=nTmp+nMerge*10 )((void) (0));
248444 if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING8 ){
248445 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
248446 break;
248447 }
248448 fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2){ (&out)->n += sqlite3Fts5PutVarint(&(&out)->
p[(&out)->n], ((tmp.n+nTail) * 2)); ((void) (0)); }
;
248449 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], tmp.p, tmp.n); (&out)->n += tmp.n; }
;
248450 if( nTail>0 ){
248451 fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail){ ((void) (0)); memcpy(&(&out)->p[(&out)->n
], &pHead->aPos[pHead->iOff], nTail); (&out)->
n += nTail; }
;
248452 }
248453
248454 pHead = pSave;
248455 for(i=0; i<nBuf+1; i++){
248456 PrefixMerger *pX = &aMerger[i];
248457 if( pX->iter.aPoslist && pX->iter.iRowid==iLastRowid ){
248458 fts5DoclistIterNext(&pX->iter);
248459 fts5PrefixMergerInsertByRowid(&pHead, pX);
248460 }
248461 }
248462
248463 }else{
248464 /* Copy poslist from pHead to output */
248465 PrefixMerger *pThis = pHead;
248466 Fts5DoclistIter *pI = &pThis->iter;
248467 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; }
;
248468 fts5DoclistIterNext(pI);
248469 pHead = pThis->pNext;
248470 fts5PrefixMergerInsertByRowid(&pHead, pThis);
248471 }
248472 }
248473
248474 fts5BufferFree(p1)sqlite3Fts5BufferFree(p1);
248475 fts5BufferFree(&tmp)sqlite3Fts5BufferFree(&tmp);
248476 memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING8);
248477 *p1 = out;
248478}
248479
248480static void fts5SetupPrefixIter(
248481 Fts5Index *p, /* Index to read from */
248482 int bDesc, /* True for "ORDER BY rowid DESC" */
248483 int iIdx, /* Index to scan for data */
248484 u8 *pToken, /* Buffer containing prefix to match */
248485 int nToken, /* Size of buffer pToken in bytes */
248486 Fts5Colset *pColset, /* Restrict matches to these columns */
248487 Fts5Iter **ppIter /* OUT: New iterator */
248488){
248489 Fts5Structure *pStruct;
248490 Fts5Buffer *aBuf;
248491 int nBuf = 32;
248492 int nMerge = 1;
248493
248494 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
248495 void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
248496 if( p->pConfig->eDetail==FTS5_DETAIL_NONE1 ){
248497 xMerge = fts5MergeRowidLists;
248498 xAppend = fts5AppendRowid;
248499 }else{
248500 nMerge = FTS5_MERGE_NLIST16-1;
248501 nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
248502 xMerge = fts5MergePrefixLists;
248503 xAppend = fts5AppendPoslist;
248504 }
248505
248506 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
248507 pStruct = fts5StructureRead(p);
248508 assert( p->rc!=SQLITE_OK || (aBuf && pStruct) )((void) (0));
248509
248510 if( p->rc==SQLITE_OK0 ){
248511 const int flags = FTS5INDEX_QUERY_SCAN0x0008
248512 | FTS5INDEX_QUERY_SKIPEMPTY0x0010
248513 | FTS5INDEX_QUERY_NOOUTPUT0x0020;
248514 int i;
248515 i64 iLastRowid = 0;
248516 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
248517 Fts5Data *pData;
248518 Fts5Buffer doclist;
248519 int bNewTerm = 1;
248520
248521 memset(&doclist, 0, sizeof(doclist));
248522
248523 /* If iIdx is non-zero, then it is the number of a prefix-index for
248524 ** prefixes 1 character longer than the prefix being queried for. That
248525 ** index contains all the doclists required, except for the one
248526 ** corresponding to the prefix itself. That one is extracted from the
248527 ** main term index here. */
248528 if( iIdx!=0 ){
248529 int dummy = 0;
248530 const int f2 = FTS5INDEX_QUERY_SKIPEMPTY0x0010|FTS5INDEX_QUERY_NOOUTPUT0x0020;
248531 pToken[0] = FTS5_MAIN_PREFIX'0';
248532 fts5MultiIterNew(p, pStruct, f2, pColset, pToken, nToken, -1, 0, &p1);
248533 fts5IterSetOutputCb(&p->rc, p1);
248534 for(;
248535 fts5MultiIterEof(p, p1)==0;
248536 fts5MultiIterNext2(p, p1, &dummy)
248537 ){
248538 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
248539 p1->xSetOutputs(p1, pSeg);
248540 if( p1->base.nData ){
248541 xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
248542 iLastRowid = p1->base.iRowid;
248543 }
248544 }
248545 fts5MultiIterFree(p1);
248546 }
248547
248548 pToken[0] = FTS5_MAIN_PREFIX'0' + iIdx;
248549 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
248550 fts5IterSetOutputCb(&p->rc, p1);
248551
248552 for( /* no-op */ ;
248553 fts5MultiIterEof(p, p1)==0;
248554 fts5MultiIterNext2(p, p1, &bNewTerm)
248555 ){
248556 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
248557 int nTerm = pSeg->term.n;
248558 const u8 *pTerm = pSeg->term.p;
248559 p1->xSetOutputs(p1, pSeg);
248560
248561 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 )((void) (0));
248562 if( bNewTerm ){
248563 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
248564 }
248565
248566 if( p1->base.nData==0 ) continue;
248567 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
248568 for(i=0; p->rc==SQLITE_OK0 && doclist.n; i++){
248569 int i1 = i*nMerge;
248570 int iStore;
248571 assert( i1+nMerge<=nBuf )((void) (0));
248572 for(iStore=i1; iStore<i1+nMerge; iStore++){
248573 if( aBuf[iStore].n==0 ){
248574 fts5BufferSwap(&doclist, &aBuf[iStore]);
248575 fts5BufferZero(&doclist)sqlite3Fts5BufferZero(&doclist);
248576 break;
248577 }
248578 }
248579 if( iStore==i1+nMerge ){
248580 xMerge(p, &doclist, nMerge, &aBuf[i1]);
248581 for(iStore=i1; iStore<i1+nMerge; iStore++){
248582 fts5BufferZero(&aBuf[iStore])sqlite3Fts5BufferZero(&aBuf[iStore]);
248583 }
248584 }
248585 }
248586 iLastRowid = 0;
248587 }
248588
248589 xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
248590 iLastRowid = p1->base.iRowid;
248591 }
248592
248593 assert( (nBuf%nMerge)==0 )((void) (0));
248594 for(i=0; i<nBuf; i+=nMerge){
248595 int iFree;
248596 if( p->rc==SQLITE_OK0 ){
248597 xMerge(p, &doclist, nMerge, &aBuf[i]);
248598 }
248599 for(iFree=i; iFree<i+nMerge; iFree++){
248600 fts5BufferFree(&aBuf[iFree])sqlite3Fts5BufferFree(&aBuf[iFree]);
248601 }
248602 }
248603 fts5MultiIterFree(p1);
248604
248605 pData = fts5IdxMalloc(p, sizeof(*pData)+doclist.n+FTS5_DATA_ZERO_PADDING8);
248606 if( pData ){
248607 pData->p = (u8*)&pData[1];
248608 pData->nn = pData->szLeaf = doclist.n;
248609 if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
248610 fts5MultiIterNew2(p, pData, bDesc, ppIter);
248611 }
248612 fts5BufferFree(&doclist)sqlite3Fts5BufferFree(&doclist);
248613 }
248614
248615 fts5StructureRelease(pStruct);
248616 sqlite3_free(aBuf);
248617}
248618
248619
248620/*
248621** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
248622** to the document with rowid iRowid.
248623*/
248624static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
248625 assert( p->rc==SQLITE_OK )((void) (0));
248626
248627 /* Allocate the hash table if it has not already been allocated */
248628 if( p->pHash==0 ){
248629 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
248630 }
248631
248632 /* Flush the hash table to disk if required */
248633 if( iRowid<p->iWriteRowid
248634 || (iRowid==p->iWriteRowid && p->bDelete==0)
248635 || (p->nPendingData > p->pConfig->nHashSize)
248636 ){
248637 fts5IndexFlush(p);
248638 }
248639
248640 p->iWriteRowid = iRowid;
248641 p->bDelete = bDelete;
248642 if( bDelete==0 ){
248643 p->nPendingRow++;
248644 }
248645 return fts5IndexReturn(p);
248646}
248647
248648/*
248649** Commit data to disk.
248650*/
248651static int sqlite3Fts5IndexSync(Fts5Index *p){
248652 assert( p->rc==SQLITE_OK )((void) (0));
248653 fts5IndexFlush(p);
248654 sqlite3Fts5IndexCloseReader(p);
248655 return fts5IndexReturn(p);
248656}
248657
248658/*
248659** Discard any data stored in the in-memory hash tables. Do not write it
248660** to the database. Additionally, assume that the contents of the %_data
248661** table may have changed on disk. So any in-memory caches of %_data
248662** records must be invalidated.
248663*/
248664static int sqlite3Fts5IndexRollback(Fts5Index *p){
248665 sqlite3Fts5IndexCloseReader(p);
248666 fts5IndexDiscardData(p);
248667 fts5StructureInvalidate(p);
248668 /* assert( p->rc==SQLITE_OK ); */
248669 return SQLITE_OK0;
248670}
248671
248672/*
248673** The %_data table is completely empty when this function is called. This
248674** function populates it with the initial structure objects for each index,
248675** and the initial version of the "averages" record (a zero-byte blob).
248676*/
248677static int sqlite3Fts5IndexReinit(Fts5Index *p){
248678 Fts5Structure s;
248679 fts5StructureInvalidate(p);
248680 fts5IndexDiscardData(p);
248681 memset(&s, 0, sizeof(Fts5Structure));
248682 if( p->pConfig->bContentlessDelete ){
248683 s.nOriginCntr = 1;
248684 }
248685 fts5DataWrite(p, FTS5_AVERAGES_ROWID1, (const u8*)"", 0);
248686 fts5StructureWrite(p, &s);
248687 return fts5IndexReturn(p);
248688}
248689
248690/*
248691** Open a new Fts5Index handle. If the bCreate argument is true, create
248692** and initialize the underlying %_data table.
248693**
248694** If successful, set *pp to point to the new object and return SQLITE_OK.
248695** Otherwise, set *pp to NULL and return an SQLite error code.
248696*/
248697static int sqlite3Fts5IndexOpen(
248698 Fts5Config *pConfig,
248699 int bCreate,
248700 Fts5Index **pp,
248701 char **pzErr
248702){
248703 int rc = SQLITE_OK0;
248704 Fts5Index *p; /* New object */
248705
248706 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
248707 if( rc==SQLITE_OK0 ){
248708 p->pConfig = pConfig;
248709 p->nWorkUnit = FTS5_WORK_UNIT64;
248710 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
248711 if( p->zDataTbl && bCreate ){
248712 rc = sqlite3Fts5CreateTable(
248713 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
248714 );
248715 if( rc==SQLITE_OK0 ){
248716 rc = sqlite3Fts5CreateTable(pConfig, "idx",
248717 "segid, term, pgno, PRIMARY KEY(segid, term)",
248718 1, pzErr
248719 );
248720 }
248721 if( rc==SQLITE_OK0 ){
248722 rc = sqlite3Fts5IndexReinit(p);
248723 }
248724 }
248725 }
248726
248727 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK )((void) (0));
248728 if( rc ){
248729 sqlite3Fts5IndexClose(p);
248730 *pp = 0;
248731 }
248732 return rc;
248733}
248734
248735/*
248736** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
248737*/
248738static int sqlite3Fts5IndexClose(Fts5Index *p){
248739 int rc = SQLITE_OK0;
248740 if( p ){
248741 assert( p->pReader==0 )((void) (0));
248742 fts5StructureInvalidate(p);
248743 sqlite3_finalize(p->pWriter);
248744 sqlite3_finalize(p->pDeleter);
248745 sqlite3_finalize(p->pIdxWriter);
248746 sqlite3_finalize(p->pIdxDeleter);
248747 sqlite3_finalize(p->pIdxSelect);
248748 sqlite3_finalize(p->pIdxNextSelect);
248749 sqlite3_finalize(p->pDataVersion);
248750 sqlite3_finalize(p->pDeleteFromIdx);
248751 sqlite3Fts5HashFree(p->pHash);
248752 sqlite3_free(p->zDataTbl);
248753 sqlite3_free(p);
248754 }
248755 return rc;
248756}
248757
248758/*
248759** Argument p points to a buffer containing utf-8 text that is n bytes in
248760** size. Return the number of bytes in the nChar character prefix of the
248761** buffer, or 0 if there are less than nChar characters in total.
248762*/
248763static int sqlite3Fts5IndexCharlenToBytelen(
248764 const char *p,
248765 int nByte,
248766 int nChar
248767){
248768 int n = 0;
248769 int i;
248770 for(i=0; i<nChar; i++){
248771 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
248772 if( (unsigned char)p[n++]>=0xc0 ){
248773 if( n>=nByte ) return 0;
248774 while( (p[n] & 0xc0)==0x80 ){
248775 n++;
248776 if( n>=nByte ){
248777 if( i+1==nChar ) break;
248778 return 0;
248779 }
248780 }
248781 }
248782 }
248783 return n;
248784}
248785
248786/*
248787** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
248788** unicode characters in the string.
248789*/
248790static int fts5IndexCharlen(const char *pIn, int nIn){
248791 int nChar = 0;
248792 int i = 0;
248793 while( i<nIn ){
248794 if( (unsigned char)pIn[i++]>=0xc0 ){
248795 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
248796 }
248797 nChar++;
248798 }
248799 return nChar;
248800}
248801
248802/*
248803** Insert or remove data to or from the index. Each time a document is
248804** added to or removed from the index, this function is called one or more
248805** times.
248806**
248807** For an insert, it must be called once for each token in the new document.
248808** If the operation is a delete, it must be called (at least) once for each
248809** unique token in the document with an iCol value less than zero. The iPos
248810** argument is ignored for a delete.
248811*/
248812static int sqlite3Fts5IndexWrite(
248813 Fts5Index *p, /* Index to write to */
248814 int iCol, /* Column token appears in (-ve -> delete) */
248815 int iPos, /* Position of token within column */
248816 const char *pToken, int nToken /* Token to add or remove to or from index */
248817){
248818 int i; /* Used to iterate through indexes */
248819 int rc = SQLITE_OK0; /* Return code */
248820 Fts5Config *pConfig = p->pConfig;
248821
248822 assert( p->rc==SQLITE_OK )((void) (0));
248823 assert( (iCol<0)==p->bDelete )((void) (0));
248824
248825 /* Add the entry to the main terms index. */
248826 rc = sqlite3Fts5HashWrite(
248827 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX'0', pToken, nToken
248828 );
248829
248830 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK0; i++){
248831 const int nChar = pConfig->aPrefix[i];
248832 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
248833 if( nByte ){
248834 rc = sqlite3Fts5HashWrite(p->pHash,
248835 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX'0'+i+1), pToken,
248836 nByte
248837 );
248838 }
248839 }
248840
248841 return rc;
248842}
248843
248844/*
248845** pToken points to a buffer of size nToken bytes containing a search
248846** term, including the index number at the start, used on a tokendata=1
248847** table. This function returns true if the term in buffer pBuf matches
248848** token pToken/nToken.
248849*/
248850static int fts5IsTokendataPrefix(
248851 Fts5Buffer *pBuf,
248852 const u8 *pToken,
248853 int nToken
248854){
248855 return (
248856 pBuf->n>=nToken
248857 && 0==memcmp(pBuf->p, pToken, nToken)
248858 && (pBuf->n==nToken || pBuf->p[nToken]==0x00)
248859 );
248860}
248861
248862/*
248863** Ensure the segment-iterator passed as the only argument points to EOF.
248864*/
248865static void fts5SegIterSetEOF(Fts5SegIter *pSeg){
248866 fts5DataRelease(pSeg->pLeaf);
248867 pSeg->pLeaf = 0;
248868}
248869
248870/*
248871** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an
248872** array of these for each row it visits. Or, for an iterator used by an
248873** "ORDER BY rank" query, it accumulates an array of these for the entire
248874** query.
248875**
248876** Each instance in the array indicates the iterator (and therefore term)
248877** associated with position iPos of rowid iRowid. This is used by the
248878** xInstToken() API.
248879*/
248880struct Fts5TokenDataMap {
248881 i64 iRowid; /* Row this token is located in */
248882 i64 iPos; /* Position of token */
248883 int iIter; /* Iterator token was read from */
248884};
248885
248886/*
248887** An object used to supplement Fts5Iter for tokendata=1 iterators.
248888*/
248889struct Fts5TokenDataIter {
248890 int nIter;
248891 int nIterAlloc;
248892
248893 int nMap;
248894 int nMapAlloc;
248895 Fts5TokenDataMap *aMap;
248896
248897 Fts5PoslistReader *aPoslistReader;
248898 int *aPoslistToIter;
248899 Fts5Iter *apIter[1];
248900};
248901
248902/*
248903** This function appends iterator pAppend to Fts5TokenDataIter pIn and
248904** returns the result.
248905*/
248906static Fts5TokenDataIter *fts5AppendTokendataIter(
248907 Fts5Index *p, /* Index object (for error code) */
248908 Fts5TokenDataIter *pIn, /* Current Fts5TokenDataIter struct */
248909 Fts5Iter *pAppend /* Append this iterator */
248910){
248911 Fts5TokenDataIter *pRet = pIn;
248912
248913 if( p->rc==SQLITE_OK0 ){
248914 if( pIn==0 || pIn->nIter==pIn->nIterAlloc ){
248915 int nAlloc = pIn ? pIn->nIterAlloc*2 : 16;
248916 int nByte = nAlloc * sizeof(Fts5Iter*) + sizeof(Fts5TokenDataIter);
248917 Fts5TokenDataIter *pNew = (Fts5TokenDataIter*)sqlite3_realloc(pIn, nByte);
248918
248919 if( pNew==0 ){
248920 p->rc = SQLITE_NOMEM7;
248921 }else{
248922 if( pIn==0 ) memset(pNew, 0, nByte);
248923 pRet = pNew;
248924 pNew->nIterAlloc = nAlloc;
248925 }
248926 }
248927 }
248928 if( p->rc ){
248929 sqlite3Fts5IterClose((Fts5IndexIter*)pAppend);
248930 }else{
248931 pRet->apIter[pRet->nIter++] = pAppend;
248932 }
248933 assert( pRet==0 || pRet->nIter<=pRet->nIterAlloc )((void) (0));
248934
248935 return pRet;
248936}
248937
248938/*
248939** Delete an Fts5TokenDataIter structure and its contents.
248940*/
248941static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){
248942 if( pSet ){
248943 int ii;
248944 for(ii=0; ii<pSet->nIter; ii++){
248945 fts5MultiIterFree(pSet->apIter[ii]);
248946 }
248947 sqlite3_free(pSet->aPoslistReader);
248948 sqlite3_free(pSet->aMap);
248949 sqlite3_free(pSet);
248950 }
248951}
248952
248953/*
248954** Append a mapping to the token-map belonging to object pT.
248955*/
248956static void fts5TokendataIterAppendMap(
248957 Fts5Index *p,
248958 Fts5TokenDataIter *pT,
248959 int iIter,
248960 i64 iRowid,
248961 i64 iPos
248962){
248963 if( p->rc==SQLITE_OK0 ){
248964 if( pT->nMap==pT->nMapAlloc ){
248965 int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64;
248966 int nByte = nNew * sizeof(Fts5TokenDataMap);
248967 Fts5TokenDataMap *aNew;
248968
248969 aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nByte);
248970 if( aNew==0 ){
248971 p->rc = SQLITE_NOMEM7;
248972 return;
248973 }
248974
248975 pT->aMap = aNew;
248976 pT->nMapAlloc = nNew;
248977 }
248978
248979 pT->aMap[pT->nMap].iRowid = iRowid;
248980 pT->aMap[pT->nMap].iPos = iPos;
248981 pT->aMap[pT->nMap].iIter = iIter;
248982 pT->nMap++;
248983 }
248984}
248985
248986/*
248987** The iterator passed as the only argument must be a tokendata=1 iterator
248988** (pIter->pTokenDataIter!=0). This function sets the iterator output
248989** variables (pIter->base.*) according to the contents of the current
248990** row.
248991*/
248992static void fts5IterSetOutputsTokendata(Fts5Iter *pIter){
248993 int ii;
248994 int nHit = 0;
248995 i64 iRowid = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
248996 int iMin = 0;
248997
248998 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
248999
249000 pIter->base.nData = 0;
249001 pIter->base.pData = 0;
249002
249003 for(ii=0; ii<pT->nIter; ii++){
249004 Fts5Iter *p = pT->apIter[ii];
249005 if( p->base.bEof==0 ){
249006 if( nHit==0 || p->base.iRowid<iRowid ){
249007 iRowid = p->base.iRowid;
249008 nHit = 1;
249009 pIter->base.pData = p->base.pData;
249010 pIter->base.nData = p->base.nData;
249011 iMin = ii;
249012 }else if( p->base.iRowid==iRowid ){
249013 nHit++;
249014 }
249015 }
249016 }
249017
249018 if( nHit==0 ){
249019 pIter->base.bEof = 1;
249020 }else{
249021 int eDetail = pIter->pIndex->pConfig->eDetail;
249022 pIter->base.bEof = 0;
249023 pIter->base.iRowid = iRowid;
249024
249025 if( nHit==1 && eDetail==FTS5_DETAIL_FULL0 ){
249026 fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, iRowid, -1);
249027 }else
249028 if( nHit>1 && eDetail!=FTS5_DETAIL_NONE1 ){
249029 int nReader = 0;
249030 int nByte = 0;
249031 i64 iPrev = 0;
249032
249033 /* Allocate array of iterators if they are not already allocated. */
249034 if( pT->aPoslistReader==0 ){
249035 pT->aPoslistReader = (Fts5PoslistReader*)sqlite3Fts5MallocZero(
249036 &pIter->pIndex->rc,
249037 pT->nIter * (sizeof(Fts5PoslistReader) + sizeof(int))
249038 );
249039 if( pT->aPoslistReader==0 ) return;
249040 pT->aPoslistToIter = (int*)&pT->aPoslistReader[pT->nIter];
249041 }
249042
249043 /* Populate an iterator for each poslist that will be merged */
249044 for(ii=0; ii<pT->nIter; ii++){
249045 Fts5Iter *p = pT->apIter[ii];
249046 if( iRowid==p->base.iRowid ){
249047 pT->aPoslistToIter[nReader] = ii;
249048 sqlite3Fts5PoslistReaderInit(
249049 p->base.pData, p->base.nData, &pT->aPoslistReader[nReader++]
249050 );
249051 nByte += p->base.nData;
249052 }
249053 }
249054
249055 /* Ensure the output buffer is large enough */
249056 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) )
){
249057 return;
249058 }
249059
249060 /* Ensure the token-mapping is large enough */
249061 if( eDetail==FTS5_DETAIL_FULL0 && pT->nMapAlloc<(pT->nMap + nByte) ){
249062 int nNew = (pT->nMapAlloc + nByte) * 2;
249063 Fts5TokenDataMap *aNew = (Fts5TokenDataMap*)sqlite3_realloc(
249064 pT->aMap, nNew*sizeof(Fts5TokenDataMap)
249065 );
249066 if( aNew==0 ){
249067 pIter->pIndex->rc = SQLITE_NOMEM7;
249068 return;
249069 }
249070 pT->aMap = aNew;
249071 pT->nMapAlloc = nNew;
249072 }
249073
249074 pIter->poslist.n = 0;
249075
249076 while( 1 ){
249077 i64 iMinPos = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
249078
249079 /* Find smallest position */
249080 iMin = 0;
249081 for(ii=0; ii<nReader; ii++){
249082 Fts5PoslistReader *pReader = &pT->aPoslistReader[ii];
249083 if( pReader->bEof==0 ){
249084 if( pReader->iPos<iMinPos ){
249085 iMinPos = pReader->iPos;
249086 iMin = ii;
249087 }
249088 }
249089 }
249090
249091 /* If all readers were at EOF, break out of the loop. */
249092 if( iMinPos==LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ) break;
249093
249094 sqlite3Fts5PoslistSafeAppend(&pIter->poslist, &iPrev, iMinPos);
249095 sqlite3Fts5PoslistReaderNext(&pT->aPoslistReader[iMin]);
249096
249097 if( eDetail==FTS5_DETAIL_FULL0 ){
249098 pT->aMap[pT->nMap].iPos = iMinPos;
249099 pT->aMap[pT->nMap].iIter = pT->aPoslistToIter[iMin];
249100 pT->aMap[pT->nMap].iRowid = iRowid;
249101 pT->nMap++;
249102 }
249103 }
249104
249105 pIter->base.pData = pIter->poslist.p;
249106 pIter->base.nData = pIter->poslist.n;
249107 }
249108 }
249109}
249110
249111/*
249112** The iterator passed as the only argument must be a tokendata=1 iterator
249113** (pIter->pTokenDataIter!=0). This function advances the iterator. If
249114** argument bFrom is false, then the iterator is advanced to the next
249115** entry. Or, if bFrom is true, it is advanced to the first entry with
249116** a rowid of iFrom or greater.
249117*/
249118static void fts5TokendataIterNext(Fts5Iter *pIter, int bFrom, i64 iFrom){
249119 int ii;
249120 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
249121 Fts5Index *pIndex = pIter->pIndex;
249122
249123 for(ii=0; ii<pT->nIter; ii++){
249124 Fts5Iter *p = pT->apIter[ii];
249125 if( p->base.bEof==0
249126 && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom))
249127 ){
249128 fts5MultiIterNext(pIndex, p, bFrom, iFrom);
249129 while( bFrom && p->base.bEof==0
249130 && p->base.iRowid<iFrom
249131 && pIndex->rc==SQLITE_OK0
249132 ){
249133 fts5MultiIterNext(pIndex, p, 0, 0);
249134 }
249135 }
249136 }
249137
249138 if( pIndex->rc==SQLITE_OK0 ){
249139 fts5IterSetOutputsTokendata(pIter);
249140 }
249141}
249142
249143/*
249144** If the segment-iterator passed as the first argument is at EOF, then
249145** set pIter->term to a copy of buffer pTerm.
249146*/
249147static void fts5TokendataSetTermIfEof(Fts5Iter *pIter, Fts5Buffer *pTerm){
249148 if( pIter && pIter->aSeg[0].pLeaf==0 ){
249149 fts5BufferSet(&pIter->pIndex->rc, &pIter->aSeg[0].term, pTerm->n, pTerm->p)sqlite3Fts5BufferSet(&pIter->pIndex->rc,&pIter->
aSeg[0].term,pTerm->n,pTerm->p)
;
249150 }
249151}
249152
249153/*
249154** This function sets up an iterator to use for a non-prefix query on a
249155** tokendata=1 table.
249156*/
249157static Fts5Iter *fts5SetupTokendataIter(
249158 Fts5Index *p, /* FTS index to query */
249159 const u8 *pToken, /* Buffer containing query term */
249160 int nToken, /* Size of buffer pToken in bytes */
249161 Fts5Colset *pColset /* Colset to filter on */
249162){
249163 Fts5Iter *pRet = 0;
249164 Fts5TokenDataIter *pSet = 0;
249165 Fts5Structure *pStruct = 0;
249166 const int flags = FTS5INDEX_QUERY_SCANONETERM0x0100 | FTS5INDEX_QUERY_SCAN0x0008;
249167
249168 Fts5Buffer bSeek = {0, 0, 0};
249169 Fts5Buffer *pSmall = 0;
249170
249171 fts5IndexFlush(p);
249172 pStruct = fts5StructureRead(p);
249173
249174 while( p->rc==SQLITE_OK0 ){
249175 Fts5Iter *pPrev = pSet ? pSet->apIter[pSet->nIter-1] : 0;
249176 Fts5Iter *pNew = 0;
249177 Fts5SegIter *pNewIter = 0;
249178 Fts5SegIter *pPrevIter = 0;
249179
249180 int iLvl, iSeg, ii;
249181
249182 pNew = fts5MultiIterAlloc(p, pStruct->nSegment);
249183 if( pSmall ){
249184 fts5BufferSet(&p->rc, &bSeek, pSmall->n, pSmall->p)sqlite3Fts5BufferSet(&p->rc,&bSeek,pSmall->n,pSmall
->p)
;
249185 fts5BufferAppendBlob(&p->rc, &bSeek, 1, (const u8*)"\0")sqlite3Fts5BufferAppendBlob(&p->rc,&bSeek,1,(const
u8*)"\0")
;
249186 }else{
249187 fts5BufferSet(&p->rc, &bSeek, nToken, pToken)sqlite3Fts5BufferSet(&p->rc,&bSeek,nToken,pToken);
249188 }
249189 if( p->rc ){
249190 sqlite3Fts5IterClose((Fts5IndexIter*)pNew);
249191 break;
249192 }
249193
249194 pNewIter = &pNew->aSeg[0];
249195 pPrevIter = (pPrev ? &pPrev->aSeg[0] : 0);
249196 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
249197 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
249198 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
249199 int bDone = 0;
249200
249201 if( pPrevIter ){
249202 if( fts5BufferCompare(pSmall, &pPrevIter->term) ){
249203 memcpy(pNewIter, pPrevIter, sizeof(Fts5SegIter));
249204 memset(pPrevIter, 0, sizeof(Fts5SegIter));
249205 bDone = 1;
249206 }else if( pPrevIter->iEndofDoclist>pPrevIter->pLeaf->szLeaf ){
249207 fts5SegIterNextInit(p,(const char*)bSeek.p,bSeek.n-1,pSeg,pNewIter);
249208 bDone = 1;
249209 }
249210 }
249211
249212 if( bDone==0 ){
249213 fts5SegIterSeekInit(p, bSeek.p, bSeek.n, flags, pSeg, pNewIter);
249214 }
249215
249216 if( pPrevIter ){
249217 if( pPrevIter->pTombArray ){
249218 pNewIter->pTombArray = pPrevIter->pTombArray;
249219 pNewIter->pTombArray->nRef++;
249220 }
249221 }else{
249222 fts5SegIterAllocTombstone(p, pNewIter);
249223 }
249224
249225 pNewIter++;
249226 if( pPrevIter ) pPrevIter++;
249227 if( p->rc ) break;
249228 }
249229 }
249230 fts5TokendataSetTermIfEof(pPrev, pSmall);
249231
249232 pNew->bSkipEmpty = 1;
249233 pNew->pColset = pColset;
249234 fts5IterSetOutputCb(&p->rc, pNew);
249235
249236 /* Loop through all segments in the new iterator. Find the smallest
249237 ** term that any segment-iterator points to. Iterator pNew will be
249238 ** used for this term. Also, set any iterator that points to a term that
249239 ** does not match pToken/nToken to point to EOF */
249240 pSmall = 0;
249241 for(ii=0; ii<pNew->nSeg; ii++){
249242 Fts5SegIter *pII = &pNew->aSeg[ii];
249243 if( 0==fts5IsTokendataPrefix(&pII->term, pToken, nToken) ){
249244 fts5SegIterSetEOF(pII);
249245 }
249246 if( pII->pLeaf && (!pSmall || fts5BufferCompare(pSmall, &pII->term)>0) ){
249247 pSmall = &pII->term;
249248 }
249249 }
249250
249251 /* If pSmall is still NULL at this point, then the new iterator does
249252 ** not point to any terms that match the query. So delete it and break
249253 ** out of the loop - all required iterators have been collected. */
249254 if( pSmall==0 ){
249255 sqlite3Fts5IterClose((Fts5IndexIter*)pNew);
249256 break;
249257 }
249258
249259 /* Append this iterator to the set and continue. */
249260 pSet = fts5AppendTokendataIter(p, pSet, pNew);
249261 }
249262
249263 if( p->rc==SQLITE_OK0 && pSet ){
249264 int ii;
249265 for(ii=0; ii<pSet->nIter; ii++){
249266 Fts5Iter *pIter = pSet->apIter[ii];
249267 int iSeg;
249268 for(iSeg=0; iSeg<pIter->nSeg; iSeg++){
249269 pIter->aSeg[iSeg].flags |= FTS5_SEGITER_ONETERM0x01;
249270 }
249271 fts5MultiIterFinishSetup(p, pIter);
249272 }
249273 }
249274
249275 if( p->rc==SQLITE_OK0 ){
249276 pRet = fts5MultiIterAlloc(p, 0);
249277 }
249278 if( pRet ){
249279 pRet->pTokenDataIter = pSet;
249280 if( pSet ){
249281 fts5IterSetOutputsTokendata(pRet);
249282 }else{
249283 pRet->base.bEof = 1;
249284 }
249285 }else{
249286 fts5TokendataIterDelete(pSet);
249287 }
249288
249289 fts5StructureRelease(pStruct);
249290 fts5BufferFree(&bSeek)sqlite3Fts5BufferFree(&bSeek);
249291 return pRet;
249292}
249293
249294
249295/*
249296** Open a new iterator to iterate though all rowid that match the
249297** specified token or token prefix.
249298*/
249299static int sqlite3Fts5IndexQuery(
249300 Fts5Index *p, /* FTS index to query */
249301 const char *pToken, int nToken, /* Token (or prefix) to query for */
249302 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
249303 Fts5Colset *pColset, /* Match these columns only */
249304 Fts5IndexIter **ppIter /* OUT: New iterator object */
249305){
249306 Fts5Config *pConfig = p->pConfig;
249307 Fts5Iter *pRet = 0;
249308 Fts5Buffer buf = {0, 0, 0};
249309
249310 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
249311 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN )((void) (0));
249312
249313 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
249314 int iIdx = 0; /* Index to search */
249315 int iPrefixIdx = 0; /* +1 prefix index */
249316 int bTokendata = pConfig->bTokendata;
249317 if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken);
249318
249319 if( flags & (FTS5INDEX_QUERY_NOTOKENDATA0x0080|FTS5INDEX_QUERY_SCAN0x0008) ){
249320 bTokendata = 0;
249321 }
249322
249323 /* Figure out which index to search and set iIdx accordingly. If this
249324 ** is a prefix query for which there is no prefix index, set iIdx to
249325 ** greater than pConfig->nPrefix to indicate that the query will be
249326 ** satisfied by scanning multiple terms in the main index.
249327 **
249328 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
249329 ** prefix-query. Instead of using a prefix-index (if one exists),
249330 ** evaluate the prefix query using the main FTS index. This is used
249331 ** for internal sanity checking by the integrity-check in debug
249332 ** mode only. */
249333#ifdef SQLITE_DEBUG
249334 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX0x0004) ){
249335 assert( flags & FTS5INDEX_QUERY_PREFIX )((void) (0));
249336 iIdx = 1+pConfig->nPrefix;
249337 }else
249338#endif
249339 if( flags & FTS5INDEX_QUERY_PREFIX0x0001 ){
249340 int nChar = fts5IndexCharlen(pToken, nToken);
249341 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
249342 int nIdxChar = pConfig->aPrefix[iIdx-1];
249343 if( nIdxChar==nChar ) break;
249344 if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
249345 }
249346 }
249347
249348 if( bTokendata && iIdx==0 ){
249349 buf.p[0] = '0';
249350 pRet = fts5SetupTokendataIter(p, buf.p, nToken+1, pColset);
249351 }else if( iIdx<=pConfig->nPrefix ){
249352 /* Straight index lookup */
249353 Fts5Structure *pStruct = fts5StructureRead(p);
249354 buf.p[0] = (u8)(FTS5_MAIN_PREFIX'0' + iIdx);
249355 if( pStruct ){
249356 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY0x0010,
249357 pColset, buf.p, nToken+1, -1, 0, &pRet
249358 );
249359 fts5StructureRelease(pStruct);
249360 }
249361 }else{
249362 /* Scan multiple terms in the main index */
249363 int bDesc = (flags & FTS5INDEX_QUERY_DESC0x0002)!=0;
249364 fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet);
249365 if( pRet==0 ){
249366 assert( p->rc!=SQLITE_OK )((void) (0));
249367 }else{
249368 assert( pRet->pColset==0 )((void) (0));
249369 fts5IterSetOutputCb(&p->rc, pRet);
249370 if( p->rc==SQLITE_OK0 ){
249371 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
249372 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
249373 }
249374 }
249375 }
249376
249377 if( p->rc ){
249378 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
249379 pRet = 0;
249380 sqlite3Fts5IndexCloseReader(p);
249381 }
249382
249383 *ppIter = (Fts5IndexIter*)pRet;
249384 sqlite3Fts5BufferFree(&buf);
249385 }
249386 return fts5IndexReturn(p);
249387}
249388
249389/*
249390** Return true if the iterator passed as the only argument is at EOF.
249391*/
249392/*
249393** Move to the next matching rowid.
249394*/
249395static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
249396 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249397 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
249398 if( pIter->pTokenDataIter ){
249399 fts5TokendataIterNext(pIter, 0, 0);
249400 }else{
249401 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
249402 }
249403 return fts5IndexReturn(pIter->pIndex);
249404}
249405
249406/*
249407** Move to the next matching term/rowid. Used by the fts5vocab module.
249408*/
249409static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
249410 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249411 Fts5Index *p = pIter->pIndex;
249412
249413 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
249414
249415 fts5MultiIterNext(p, pIter, 0, 0);
249416 if( p->rc==SQLITE_OK0 ){
249417 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
249418 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX'0' ){
249419 fts5DataRelease(pSeg->pLeaf);
249420 pSeg->pLeaf = 0;
249421 pIter->base.bEof = 1;
249422 }
249423 }
249424
249425 return fts5IndexReturn(pIter->pIndex);
249426}
249427
249428/*
249429** Move to the next matching rowid that occurs at or after iMatch. The
249430** definition of "at or after" depends on whether this iterator iterates
249431** in ascending or descending rowid order.
249432*/
249433static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
249434 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249435 if( pIter->pTokenDataIter ){
249436 fts5TokendataIterNext(pIter, 1, iMatch);
249437 }else{
249438 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
249439 }
249440 return fts5IndexReturn(pIter->pIndex);
249441}
249442
249443/*
249444** Return the current term.
249445*/
249446static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
249447 int n;
249448 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
249449 assert_nc( z || n<=1 )((void) (0));
249450 *pn = n-1;
249451 return (z ? &z[1] : 0);
249452}
249453
249454/*
249455** This is used by xInstToken() to access the token at offset iOff, column
249456** iCol of row iRowid. The token is returned via output variables *ppOut
249457** and *pnOut. The iterator passed as the first argument must be a tokendata=1
249458** iterator (pIter->pTokenDataIter!=0).
249459*/
249460static int sqlite3Fts5IterToken(
249461 Fts5IndexIter *pIndexIter,
249462 i64 iRowid,
249463 int iCol,
249464 int iOff,
249465 const char **ppOut, int *pnOut
249466){
249467 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249468 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
249469 Fts5TokenDataMap *aMap = pT->aMap;
249470 i64 iPos = (((i64)iCol)<<32) + iOff;
249471
249472 int i1 = 0;
249473 int i2 = pT->nMap;
249474 int iTest = 0;
249475
249476 while( i2>i1 ){
249477 iTest = (i1 + i2) / 2;
249478
249479 if( aMap[iTest].iRowid<iRowid ){
249480 i1 = iTest+1;
249481 }else if( aMap[iTest].iRowid>iRowid ){
249482 i2 = iTest;
249483 }else{
249484 if( aMap[iTest].iPos<iPos ){
249485 if( aMap[iTest].iPos<0 ){
249486 break;
249487 }
249488 i1 = iTest+1;
249489 }else if( aMap[iTest].iPos>iPos ){
249490 i2 = iTest;
249491 }else{
249492 break;
249493 }
249494 }
249495 }
249496
249497 if( i2>i1 ){
249498 Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter];
249499 *ppOut = (const char*)pMap->aSeg[0].term.p+1;
249500 *pnOut = pMap->aSeg[0].term.n-1;
249501 }
249502
249503 return SQLITE_OK0;
249504}
249505
249506/*
249507** Clear any existing entries from the token-map associated with the
249508** iterator passed as the only argument.
249509*/
249510static void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter *pIndexIter){
249511 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249512 if( pIter && pIter->pTokenDataIter ){
249513 pIter->pTokenDataIter->nMap = 0;
249514 }
249515}
249516
249517/*
249518** Set a token-mapping for the iterator passed as the first argument. This
249519** is used in detail=column or detail=none mode when a token is requested
249520** using the xInstToken() API. In this case the caller tokenizers the
249521** current row and configures the token-mapping via multiple calls to this
249522** function.
249523*/
249524static int sqlite3Fts5IndexIterWriteTokendata(
249525 Fts5IndexIter *pIndexIter,
249526 const char *pToken, int nToken,
249527 i64 iRowid, int iCol, int iOff
249528){
249529 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249530 Fts5TokenDataIter *pT = pIter->pTokenDataIter;
249531 Fts5Index *p = pIter->pIndex;
249532 int ii;
249533
249534 assert( p->pConfig->eDetail!=FTS5_DETAIL_FULL )((void) (0));
249535 assert( pIter->pTokenDataIter )((void) (0));
249536
249537 for(ii=0; ii<pT->nIter; ii++){
249538 Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term;
249539 if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break;
249540 }
249541 if( ii<pT->nIter ){
249542 fts5TokendataIterAppendMap(p, pT, ii, iRowid, (((i64)iCol)<<32) + iOff);
249543 }
249544 return fts5IndexReturn(p);
249545}
249546
249547/*
249548** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
249549*/
249550static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
249551 if( pIndexIter ){
249552 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249553 Fts5Index *pIndex = pIter->pIndex;
249554 fts5TokendataIterDelete(pIter->pTokenDataIter);
249555 fts5MultiIterFree(pIter);
249556 sqlite3Fts5IndexCloseReader(pIndex);
249557 }
249558}
249559
249560/*
249561** Read and decode the "averages" record from the database.
249562**
249563** Parameter anSize must point to an array of size nCol, where nCol is
249564** the number of user defined columns in the FTS table.
249565*/
249566static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
249567 int nCol = p->pConfig->nCol;
249568 Fts5Data *pData;
249569
249570 *pnRow = 0;
249571 memset(anSize, 0, sizeof(i64) * nCol);
249572 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID1);
249573 if( p->rc==SQLITE_OK0 && pData->nn ){
249574 int i = 0;
249575 int iCol;
249576 i += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[i], (u64*)pnRow);
249577 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
249578 i += fts5GetVarintsqlite3Fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
249579 }
249580 }
249581
249582 fts5DataRelease(pData);
249583 return fts5IndexReturn(p);
249584}
249585
249586/*
249587** Replace the current "averages" record with the contents of the buffer
249588** supplied as the second argument.
249589*/
249590static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
249591 assert( p->rc==SQLITE_OK )((void) (0));
249592 fts5DataWrite(p, FTS5_AVERAGES_ROWID1, pData, nData);
249593 return fts5IndexReturn(p);
249594}
249595
249596/*
249597** Return the total number of blocks this module has read from the %_data
249598** table since it was created.
249599*/
249600static int sqlite3Fts5IndexReads(Fts5Index *p){
249601 return p->nRead;
249602}
249603
249604/*
249605** Set the 32-bit cookie value stored at the start of all structure
249606** records to the value passed as the second argument.
249607**
249608** Return SQLITE_OK if successful, or an SQLite error code if an error
249609** occurs.
249610*/
249611static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
249612 int rc; /* Return code */
249613 Fts5Config *pConfig = p->pConfig; /* Configuration object */
249614 u8 aCookie[4]; /* Binary representation of iNew */
249615 sqlite3_blob *pBlob = 0;
249616
249617 assert( p->rc==SQLITE_OK )((void) (0));
249618 sqlite3Fts5Put32(aCookie, iNew);
249619
249620 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
249621 "block", FTS5_STRUCTURE_ROWID10, 1, &pBlob
249622 );
249623 if( rc==SQLITE_OK0 ){
249624 sqlite3_blob_write(pBlob, aCookie, 4, 0);
249625 rc = sqlite3_blob_close(pBlob);
249626 }
249627
249628 return rc;
249629}
249630
249631static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
249632 Fts5Structure *pStruct;
249633 pStruct = fts5StructureRead(p);
249634 fts5StructureRelease(pStruct);
249635 return fts5IndexReturn(p);
249636}
249637
249638/*
249639** Retrieve the origin value that will be used for the segment currently
249640** being accumulated in the in-memory hash table when it is flushed to
249641** disk. If successful, SQLITE_OK is returned and (*piOrigin) set to
249642** the queried value. Or, if an error occurs, an error code is returned
249643** and the final value of (*piOrigin) is undefined.
249644*/
249645static int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin){
249646 Fts5Structure *pStruct;
249647 pStruct = fts5StructureRead(p);
249648 if( pStruct ){
249649 *piOrigin = pStruct->nOriginCntr;
249650 fts5StructureRelease(pStruct);
249651 }
249652 return fts5IndexReturn(p);
249653}
249654
249655/*
249656** Buffer pPg contains a page of a tombstone hash table - one of nPg pages
249657** associated with the same segment. This function adds rowid iRowid to
249658** the hash table. The caller is required to guarantee that there is at
249659** least one free slot on the page.
249660**
249661** If parameter bForce is false and the hash table is deemed to be full
249662** (more than half of the slots are occupied), then non-zero is returned
249663** and iRowid not inserted. Or, if bForce is true or if the hash table page
249664** is not full, iRowid is inserted and zero returned.
249665*/
249666static int fts5IndexTombstoneAddToPage(
249667 Fts5Data *pPg,
249668 int bForce,
249669 int nPg,
249670 u64 iRowid
249671){
249672 const int szKey = TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8);
249673 const int nSlot = TOMBSTONE_NSLOT(pPg)((pPg->nn > 16) ? ((pPg->nn-8) / (pPg->p[0]==4 ? 4
: 8)) : 1)
;
249674 const int nElem = fts5GetU32(&pPg->p[4]);
249675 int iSlot = (iRowid / nPg) % nSlot;
249676 int nCollide = nSlot;
249677
249678 if( szKey==4 && iRowid>0xFFFFFFFF ) return 2;
249679 if( iRowid==0 ){
249680 pPg->p[1] = 0x01;
249681 return 0;
249682 }
249683
249684 if( bForce==0 && nElem>=(nSlot/2) ){
249685 return 1;
249686 }
249687
249688 fts5PutU32(&pPg->p[4], nElem+1);
249689 if( szKey==4 ){
249690 u32 *aSlot = (u32*)&pPg->p[8];
249691 while( aSlot[iSlot] ){
249692 iSlot = (iSlot + 1) % nSlot;
249693 if( nCollide--==0 ) return 0;
249694 }
249695 fts5PutU32((u8*)&aSlot[iSlot], (u32)iRowid);
249696 }else{
249697 u64 *aSlot = (u64*)&pPg->p[8];
249698 while( aSlot[iSlot] ){
249699 iSlot = (iSlot + 1) % nSlot;
249700 if( nCollide--==0 ) return 0;
249701 }
249702 fts5PutU64((u8*)&aSlot[iSlot], iRowid);
249703 }
249704
249705 return 0;
249706}
249707
249708/*
249709** This function attempts to build a new hash containing all the keys
249710** currently in the tombstone hash table for segment pSeg. The new
249711** hash will be stored in the nOut buffers passed in array apOut[].
249712** All pages of the new hash use key-size szKey (4 or 8).
249713**
249714** Return 0 if the hash is successfully rebuilt into the nOut pages.
249715** Or non-zero if it is not (because one page became overfull). In this
249716** case the caller should retry with a larger nOut parameter.
249717**
249718** Parameter pData1 is page iPg1 of the hash table being rebuilt.
249719*/
249720static int fts5IndexTombstoneRehash(
249721 Fts5Index *p,
249722 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
249723 Fts5Data *pData1, /* One page of current hash - or NULL */
249724 int iPg1, /* Which page of the current hash is pData1 */
249725 int szKey, /* 4 or 8, the keysize */
249726 int nOut, /* Number of output pages */
249727 Fts5Data **apOut /* Array of output hash pages */
249728){
249729 int ii;
249730 int res = 0;
249731
249732 /* Initialize the headers of all the output pages */
249733 for(ii=0; ii<nOut; ii++){
249734 apOut[ii]->p[0] = szKey;
249735 fts5PutU32(&apOut[ii]->p[4], 0);
249736 }
249737
249738 /* Loop through the current pages of the hash table. */
249739 for(ii=0; res==0 && ii<pSeg->nPgTombstone; ii++){
249740 Fts5Data *pData = 0; /* Page ii of the current hash table */
249741 Fts5Data *pFree = 0; /* Free this at the end of the loop */
249742
249743 if( iPg1==ii ){
249744 pData = pData1;
249745 }else{
249746 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)) )
);
249747 }
249748
249749 if( pData ){
249750 int szKeyIn = TOMBSTONE_KEYSIZE(pData)(pData->p[0]==4 ? 4 : 8);
249751 int nSlotIn = (pData->nn - 8) / szKeyIn;
249752 int iIn;
249753 for(iIn=0; iIn<nSlotIn; iIn++){
249754 u64 iVal = 0;
249755
249756 /* Read the value from slot iIn of the input page into iVal. */
249757 if( szKeyIn==4 ){
249758 u32 *aSlot = (u32*)&pData->p[8];
249759 if( aSlot[iIn] ) iVal = fts5GetU32((u8*)&aSlot[iIn]);
249760 }else{
249761 u64 *aSlot = (u64*)&pData->p[8];
249762 if( aSlot[iIn] ) iVal = fts5GetU64((u8*)&aSlot[iIn]);
249763 }
249764
249765 /* If iVal is not 0 at this point, insert it into the new hash table */
249766 if( iVal ){
249767 Fts5Data *pPg = apOut[(iVal % nOut)];
249768 res = fts5IndexTombstoneAddToPage(pPg, 0, nOut, iVal);
249769 if( res ) break;
249770 }
249771 }
249772
249773 /* If this is page 0 of the old hash, copy the rowid-0-flag from the
249774 ** old hash to the new. */
249775 if( ii==0 ){
249776 apOut[0]->p[1] = pData->p[1];
249777 }
249778 }
249779 fts5DataRelease(pFree);
249780 }
249781
249782 return res;
249783}
249784
249785/*
249786** This is called to rebuild the hash table belonging to segment pSeg.
249787** If parameter pData1 is not NULL, then one page of the existing hash table
249788** has already been loaded - pData1, which is page iPg1. The key-size for
249789** the new hash table is szKey (4 or 8).
249790**
249791** If successful, the new hash table is not written to disk. Instead,
249792** output parameter (*pnOut) is set to the number of pages in the new
249793** hash table, and (*papOut) to point to an array of buffers containing
249794** the new page data.
249795**
249796** If an error occurs, an error code is left in the Fts5Index object and
249797** both output parameters set to 0 before returning.
249798*/
249799static void fts5IndexTombstoneRebuild(
249800 Fts5Index *p,
249801 Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */
249802 Fts5Data *pData1, /* One page of current hash - or NULL */
249803 int iPg1, /* Which page of the current hash is pData1 */
249804 int szKey, /* 4 or 8, the keysize */
249805 int *pnOut, /* OUT: Number of output pages */
249806 Fts5Data ***papOut /* OUT: Output hash pages */
249807){
249808 const int MINSLOT = 32;
249809 int nSlotPerPage = MAX(MINSLOT, (p->pConfig->pgsz - 8) / szKey)((MINSLOT)>((p->pConfig->pgsz - 8) / szKey)?(MINSLOT
):((p->pConfig->pgsz - 8) / szKey))
;
249810 int nSlot = 0; /* Number of slots in each output page */
249811 int nOut = 0;
249812
249813 /* Figure out how many output pages (nOut) and how many slots per
249814 ** page (nSlot). There are three possibilities:
249815 **
249816 ** 1. The hash table does not yet exist. In this case the new hash
249817 ** table will consist of a single page with MINSLOT slots.
249818 **
249819 ** 2. The hash table exists but is currently a single page. In this
249820 ** case an attempt is made to grow the page to accommodate the new
249821 ** entry. The page is allowed to grow up to nSlotPerPage (see above)
249822 ** slots.
249823 **
249824 ** 3. The hash table already consists of more than one page, or of
249825 ** a single page already so large that it cannot be grown. In this
249826 ** case the new hash consists of (nPg*2+1) pages of nSlotPerPage
249827 ** slots each, where nPg is the current number of pages in the
249828 ** hash table.
249829 */
249830 if( pSeg->nPgTombstone==0 ){
249831 /* Case 1. */
249832 nOut = 1;
249833 nSlot = MINSLOT;
249834 }else if( pSeg->nPgTombstone==1 ){
249835 /* Case 2. */
249836 int nElem = (int)fts5GetU32(&pData1->p[4]);
249837 assert( pData1 && iPg1==0 )((void) (0));
249838 nOut = 1;
249839 nSlot = MAX(nElem*4, MINSLOT)((nElem*4)>(MINSLOT)?(nElem*4):(MINSLOT));
249840 if( nSlot>nSlotPerPage ) nOut = 0;
249841 }
249842 if( nOut==0 ){
249843 /* Case 3. */
249844 nOut = (pSeg->nPgTombstone * 2 + 1);
249845 nSlot = nSlotPerPage;
249846 }
249847
249848 /* Allocate the required array and output pages */
249849 while( 1 ){
249850 int res = 0;
249851 int ii = 0;
249852 int szPage = 0;
249853 Fts5Data **apOut = 0;
249854
249855 /* Allocate space for the new hash table */
249856 assert( nSlot>=MINSLOT )((void) (0));
249857 apOut = (Fts5Data**)sqlite3Fts5MallocZero(&p->rc, sizeof(Fts5Data*) * nOut);
249858 szPage = 8 + nSlot*szKey;
249859 for(ii=0; ii<nOut; ii++){
249860 Fts5Data *pNew = (Fts5Data*)sqlite3Fts5MallocZero(&p->rc,
249861 sizeof(Fts5Data)+szPage
249862 );
249863 if( pNew ){
249864 pNew->nn = szPage;
249865 pNew->p = (u8*)&pNew[1];
249866 apOut[ii] = pNew;
249867 }
249868 }
249869
249870 /* Rebuild the hash table. */
249871 if( p->rc==SQLITE_OK0 ){
249872 res = fts5IndexTombstoneRehash(p, pSeg, pData1, iPg1, szKey, nOut, apOut);
249873 }
249874 if( res==0 ){
249875 if( p->rc ){
249876 fts5IndexFreeArray(apOut, nOut);
249877 apOut = 0;
249878 nOut = 0;
249879 }
249880 *pnOut = nOut;
249881 *papOut = apOut;
249882 break;
249883 }
249884
249885 /* If control flows to here, it was not possible to rebuild the hash
249886 ** table. Free all buffers and then try again with more pages. */
249887 assert( p->rc==SQLITE_OK )((void) (0));
249888 fts5IndexFreeArray(apOut, nOut);
249889 nSlot = nSlotPerPage;
249890 nOut = nOut*2 + 1;
249891 }
249892}
249893
249894
249895/*
249896** Add a tombstone for rowid iRowid to segment pSeg.
249897*/
249898static void fts5IndexTombstoneAdd(
249899 Fts5Index *p,
249900 Fts5StructureSegment *pSeg,
249901 u64 iRowid
249902){
249903 Fts5Data *pPg = 0;
249904 int iPg = -1;
249905 int szKey = 0;
249906 int nHash = 0;
249907 Fts5Data **apHash = 0;
249908
249909 p->nContentlessDelete++;
249910
249911 if( pSeg->nPgTombstone>0 ){
249912 iPg = iRowid % pSeg->nPgTombstone;
249913 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)) )
);
249914 if( pPg==0 ){
249915 assert( p->rc!=SQLITE_OK )((void) (0));
249916 return;
249917 }
249918
249919 if( 0==fts5IndexTombstoneAddToPage(pPg, 0, pSeg->nPgTombstone, iRowid) ){
249920 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);
249921 fts5DataRelease(pPg);
249922 return;
249923 }
249924 }
249925
249926 /* Have to rebuild the hash table. First figure out the key-size (4 or 8). */
249927 szKey = pPg ? TOMBSTONE_KEYSIZE(pPg)(pPg->p[0]==4 ? 4 : 8) : 4;
249928 if( iRowid>0xFFFFFFFF ) szKey = 8;
249929
249930 /* Rebuild the hash table */
249931 fts5IndexTombstoneRebuild(p, pSeg, pPg, iPg, szKey, &nHash, &apHash);
249932 assert( p->rc==SQLITE_OK || (nHash==0 && apHash==0) )((void) (0));
249933
249934 /* If all has succeeded, write the new rowid into one of the new hash
249935 ** table pages, then write them all out to disk. */
249936 if( nHash ){
249937 int ii = 0;
249938 fts5IndexTombstoneAddToPage(apHash[iRowid % nHash], 1, nHash, iRowid);
249939 for(ii=0; ii<nHash; ii++){
249940 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)) )
;
249941 fts5DataWrite(p, iTombstoneRowid, apHash[ii]->p, apHash[ii]->nn);
249942 }
249943 pSeg->nPgTombstone = nHash;
249944 fts5StructureWrite(p, p->pStruct);
249945 }
249946
249947 fts5DataRelease(pPg);
249948 fts5IndexFreeArray(apHash, nHash);
249949}
249950
249951/*
249952** Add iRowid to the tombstone list of the segment or segments that contain
249953** rows from origin iOrigin. Return SQLITE_OK if successful, or an SQLite
249954** error code otherwise.
249955*/
249956static int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid){
249957 Fts5Structure *pStruct;
249958 pStruct = fts5StructureRead(p);
249959 if( pStruct ){
249960 int bFound = 0; /* True after pSeg->nEntryTombstone incr. */
249961 int iLvl;
249962 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
249963 int iSeg;
249964 for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){
249965 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
249966 if( pSeg->iOrigin1<=(u64)iOrigin && pSeg->iOrigin2>=(u64)iOrigin ){
249967 if( bFound==0 ){
249968 pSeg->nEntryTombstone++;
249969 bFound = 1;
249970 }
249971 fts5IndexTombstoneAdd(p, pSeg, iRowid);
249972 }
249973 }
249974 }
249975 fts5StructureRelease(pStruct);
249976 }
249977 return fts5IndexReturn(p);
249978}
249979
249980/*************************************************************************
249981**************************************************************************
249982** Below this point is the implementation of the integrity-check
249983** functionality.
249984*/
249985
249986/*
249987** Return a simple checksum value based on the arguments.
249988*/
249989static u64 sqlite3Fts5IndexEntryCksum(
249990 i64 iRowid,
249991 int iCol,
249992 int iPos,
249993 int iIdx,
249994 const char *pTerm,
249995 int nTerm
249996){
249997 int i;
249998 u64 ret = iRowid;
249999 ret += (ret<<3) + iCol;
250000 ret += (ret<<3) + iPos;
250001 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX'0' + iIdx);
250002 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
250003 return ret;
250004}
250005
250006#ifdef SQLITE_DEBUG
250007/*
250008** This function is purely an internal test. It does not contribute to
250009** FTS functionality, or even the integrity-check, in any way.
250010**
250011** Instead, it tests that the same set of pgno/rowid combinations are
250012** visited regardless of whether the doclist-index identified by parameters
250013** iSegid/iLeaf is iterated in forwards or reverse order.
250014*/
250015static void fts5TestDlidxReverse(
250016 Fts5Index *p,
250017 int iSegid, /* Segment id to load from */
250018 int iLeaf /* Load doclist-index for this leaf */
250019){
250020 Fts5DlidxIter *pDlidx = 0;
250021 u64 cksum1 = 13;
250022 u64 cksum2 = 13;
250023
250024 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
250025 fts5DlidxIterEof(p, pDlidx)==0;
250026 fts5DlidxIterNext(p, pDlidx)
250027 ){
250028 i64 iRowid = fts5DlidxIterRowid(pDlidx);
250029 int pgno = fts5DlidxIterPgno(pDlidx);
250030 assert( pgno>iLeaf )((void) (0));
250031 cksum1 += iRowid + ((i64)pgno<<32);
250032 }
250033 fts5DlidxIterFree(pDlidx);
250034 pDlidx = 0;
250035
250036 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
250037 fts5DlidxIterEof(p, pDlidx)==0;
250038 fts5DlidxIterPrev(p, pDlidx)
250039 ){
250040 i64 iRowid = fts5DlidxIterRowid(pDlidx);
250041 int pgno = fts5DlidxIterPgno(pDlidx);
250042 assert( fts5DlidxIterPgno(pDlidx)>iLeaf )((void) (0));
250043 cksum2 += iRowid + ((i64)pgno<<32);
250044 }
250045 fts5DlidxIterFree(pDlidx);
250046 pDlidx = 0;
250047
250048 if( p->rc==SQLITE_OK0 && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250049}
250050
250051static int fts5QueryCksum(
250052 Fts5Index *p, /* Fts5 index object */
250053 int iIdx,
250054 const char *z, /* Index key to query for */
250055 int n, /* Size of index key in bytes */
250056 int flags, /* Flags for Fts5IndexQuery */
250057 u64 *pCksum /* IN/OUT: Checksum value */
250058){
250059 int eDetail = p->pConfig->eDetail;
250060 u64 cksum = *pCksum;
250061 Fts5IndexIter *pIter = 0;
250062 int rc = sqlite3Fts5IndexQuery(
250063 p, z, n, (flags | FTS5INDEX_QUERY_NOTOKENDATA0x0080), 0, &pIter
250064 );
250065
250066 while( rc==SQLITE_OK0 && ALWAYS(pIter!=0)(pIter!=0) && 0==sqlite3Fts5IterEof(pIter)((pIter)->bEof) ){
250067 i64 rowid = pIter->iRowid;
250068
250069 if( eDetail==FTS5_DETAIL_NONE1 ){
250070 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
250071 }else{
250072 Fts5PoslistReader sReader;
250073 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
250074 sReader.bEof==0;
250075 sqlite3Fts5PoslistReaderNext(&sReader)
250076 ){
250077 int iCol = FTS5_POS2COLUMN(sReader.iPos)(int)((sReader.iPos >> 32) & 0x7FFFFFFF);
250078 int iOff = FTS5_POS2OFFSET(sReader.iPos)(int)(sReader.iPos & 0x7FFFFFFF);
250079 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
250080 }
250081 }
250082 if( rc==SQLITE_OK0 ){
250083 rc = sqlite3Fts5IterNext(pIter);
250084 }
250085 }
250086 sqlite3Fts5IterClose(pIter);
250087
250088 *pCksum = cksum;
250089 return rc;
250090}
250091
250092/*
250093** Check if buffer z[], size n bytes, contains as series of valid utf-8
250094** encoded codepoints. If so, return 0. Otherwise, if the buffer does not
250095** contain valid utf-8, return non-zero.
250096*/
250097static int fts5TestUtf8(const char *z, int n){
250098 int i = 0;
250099 assert_nc( n>0 )((void) (0));
250100 while( i<n ){
250101 if( (z[i] & 0x80)==0x00 ){
250102 i++;
250103 }else
250104 if( (z[i] & 0xE0)==0xC0 ){
250105 if( i+1>=n || (z[i+1] & 0xC0)!=0x80 ) return 1;
250106 i += 2;
250107 }else
250108 if( (z[i] & 0xF0)==0xE0 ){
250109 if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
250110 i += 3;
250111 }else
250112 if( (z[i] & 0xF8)==0xF0 ){
250113 if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1;
250114 if( (z[i+2] & 0xC0)!=0x80 ) return 1;
250115 i += 3;
250116 }else{
250117 return 1;
250118 }
250119 }
250120
250121 return 0;
250122}
250123
250124/*
250125** This function is also purely an internal test. It does not contribute to
250126** FTS functionality, or even the integrity-check, in any way.
250127*/
250128static void fts5TestTerm(
250129 Fts5Index *p,
250130 Fts5Buffer *pPrev, /* Previous term */
250131 const char *z, int n, /* Possibly new term to test */
250132 u64 expected,
250133 u64 *pCksum
250134){
250135 int rc = p->rc;
250136 if( pPrev->n==0 ){
250137 fts5BufferSet(&rc, pPrev, n, (const u8*)z)sqlite3Fts5BufferSet(&rc,pPrev,n,(const u8*)z);
250138 }else
250139 if( rc==SQLITE_OK0 && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
250140 u64 cksum3 = *pCksum;
250141 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
250142 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
250143 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX'0');
250144 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX0x0001);
250145 u64 ck1 = 0;
250146 u64 ck2 = 0;
250147
250148 /* Check that the results returned for ASC and DESC queries are
250149 ** the same. If not, call this corruption. */
250150 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
250151 if( rc==SQLITE_OK0 ){
250152 int f = flags|FTS5INDEX_QUERY_DESC0x0002;
250153 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
250154 }
250155 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
250156
250157 /* If this is a prefix query, check that the results returned if the
250158 ** the index is disabled are the same. In both ASC and DESC order.
250159 **
250160 ** This check may only be performed if the hash table is empty. This
250161 ** is because the hash table only supports a single scan query at
250162 ** a time, and the multi-iter loop from which this function is called
250163 ** is already performing such a scan.
250164 **
250165 ** Also only do this if buffer zTerm contains nTerm bytes of valid
250166 ** utf-8. Otherwise, the last part of the buffer contents might contain
250167 ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8
250168 ** character stored in the main fts index, which will cause the
250169 ** test to fail. */
250170 if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){
250171 if( iIdx>0 && rc==SQLITE_OK0 ){
250172 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX0x0004;
250173 ck2 = 0;
250174 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
250175 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
250176 }
250177 if( iIdx>0 && rc==SQLITE_OK0 ){
250178 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX0x0004|FTS5INDEX_QUERY_DESC0x0002;
250179 ck2 = 0;
250180 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
250181 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT(11 | (1<<8));
250182 }
250183 }
250184
250185 cksum3 ^= ck1;
250186 fts5BufferSet(&rc, pPrev, n, (const u8*)z)sqlite3Fts5BufferSet(&rc,pPrev,n,(const u8*)z);
250187
250188 if( rc==SQLITE_OK0 && cksum3!=expected ){
250189 rc = FTS5_CORRUPT(11 | (1<<8));
250190 }
250191 *pCksum = cksum3;
250192 }
250193 p->rc = rc;
250194}
250195
250196#else
250197# define fts5TestDlidxReverse(x,y,z)
250198# define fts5TestTerm(u,v,w,x,y,z)
250199#endif
250200
250201/*
250202** Check that:
250203**
250204** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
250205** contain zero terms.
250206** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
250207** contain zero rowids.
250208*/
250209static void fts5IndexIntegrityCheckEmpty(
250210 Fts5Index *p,
250211 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
250212 int iFirst,
250213 int iNoRowid,
250214 int iLast
250215){
250216 int i;
250217
250218 /* Now check that the iter.nEmpty leaves following the current leaf
250219 ** (a) exist and (b) contain no terms. */
250220 for(i=iFirst; p->rc==SQLITE_OK0 && i<=iLast; i++){
250221 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)) )
);
250222 if( pLeaf ){
250223 if( !fts5LeafIsTermless(pLeaf)((pLeaf)->szLeaf >= (pLeaf)->nn) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250224 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p)) ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250225 }
250226 fts5DataRelease(pLeaf);
250227 }
250228}
250229
250230static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
250231 i64 iTermOff = 0;
250232 int ii;
250233
250234 Fts5Buffer buf1 = {0,0,0};
250235 Fts5Buffer buf2 = {0,0,0};
250236
250237 ii = pLeaf->szLeaf;
250238 while( ii<pLeaf->nn && p->rc==SQLITE_OK0 ){
250239 int res;
250240 i64 iOff;
250241 int nIncr;
250242
250243 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr)sqlite3Fts5GetVarint32(&pLeaf->p[ii],(u32*)&(nIncr
))
;
250244 iTermOff += nIncr;
250245 iOff = iTermOff;
250246
250247 if( iOff>=pLeaf->szLeaf ){
250248 p->rc = FTS5_CORRUPT(11 | (1<<8));
250249 }else if( iTermOff==nIncr ){
250250 int nByte;
250251 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nByte
))
;
250252 if( (iOff+nByte)>pLeaf->szLeaf ){
250253 p->rc = FTS5_CORRUPT(11 | (1<<8));
250254 }else{
250255 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff])sqlite3Fts5BufferSet(&p->rc,&buf1,nByte,&pLeaf
->p[iOff])
;
250256 }
250257 }else{
250258 int nKeep, nByte;
250259 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nKeep
))
;
250260 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nByte
))
;
250261 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
250262 p->rc = FTS5_CORRUPT(11 | (1<<8));
250263 }else{
250264 buf1.n = nKeep;
250265 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff])sqlite3Fts5BufferAppendBlob(&p->rc,&buf1,nByte,&
pLeaf->p[iOff])
;
250266 }
250267
250268 if( p->rc==SQLITE_OK0 ){
250269 res = fts5BufferCompare(&buf1, &buf2);
250270 if( res<=0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250271 }
250272 }
250273 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p)sqlite3Fts5BufferSet(&p->rc,&buf2,buf1.n,buf1.p);
250274 }
250275
250276 fts5BufferFree(&buf1)sqlite3Fts5BufferFree(&buf1);
250277 fts5BufferFree(&buf2)sqlite3Fts5BufferFree(&buf2);
250278}
250279
250280static void fts5IndexIntegrityCheckSegment(
250281 Fts5Index *p, /* FTS5 backend object */
250282 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
250283){
250284 Fts5Config *pConfig = p->pConfig;
250285 int bSecureDelete = (pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE5);
250286 sqlite3_stmt *pStmt = 0;
250287 int rc2;
250288 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
250289 int iDlidxPrevLeaf = pSeg->pgnoLast;
250290
250291 if( pSeg->pgnoFirst==0 ) return;
250292
250293 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
250294 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d "
250295 "ORDER BY 1, 2",
250296 pConfig->zDb, pConfig->zName, pSeg->iSegid
250297 ));
250298
250299 /* Iterate through the b-tree hierarchy. */
250300 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
250301 i64 iRow; /* Rowid for this leaf */
250302 Fts5Data *pLeaf; /* Data for this leaf */
250303
250304 const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1);
250305 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
250306 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
250307 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
250308
250309 /* If the leaf in question has already been trimmed from the segment,
250310 ** ignore this b-tree entry. Otherwise, load it into memory. */
250311 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
250312 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf)( ((i64)(pSeg->iSegid) << (31 +5 +1)) + ((i64)(0) <<
(31 + 5)) + ((i64)(0) << (31)) + ((i64)(iIdxLeaf)) )
;
250313 pLeaf = fts5LeafRead(p, iRow);
250314 if( pLeaf==0 ) break;
250315
250316 /* Check that the leaf contains at least one term, and that it is equal
250317 ** to or larger than the split-key in zIdxTerm. Also check that if there
250318 ** is also a rowid pointer within the leaf page header, it points to a
250319 ** location before the term. */
250320 if( pLeaf->nn<=pLeaf->szLeaf ){
250321
250322 if( nIdxTerm==0
250323 && pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE5
250324 && pLeaf->nn==pLeaf->szLeaf
250325 && pLeaf->nn==4
250326 ){
250327 /* special case - the very first page in a segment keeps its %_idx
250328 ** entry even if all the terms are removed from it by secure-delete
250329 ** operations. */
250330 }else{
250331 p->rc = FTS5_CORRUPT(11 | (1<<8));
250332 }
250333
250334 }else{
250335 int iOff; /* Offset of first term on leaf */
250336 int iRowidOff; /* Offset of first rowid on leaf */
250337 int nTerm; /* Size of term on leaf in bytes */
250338 int res; /* Comparison of term and split-key */
250339
250340 iOff = fts5LeafFirstTermOff(pLeaf);
250341 iRowidOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p));
250342 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
250343 p->rc = FTS5_CORRUPT(11 | (1<<8));
250344 }else{
250345 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm)sqlite3Fts5GetVarint32(&pLeaf->p[iOff],(u32*)&(nTerm
))
;
250346 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm))((((nTerm)<(nIdxTerm)?(nTerm):(nIdxTerm)))<=0 ? 0 : memcmp
((&pLeaf->p[iOff]), (zIdxTerm), (((nTerm)<(nIdxTerm
)?(nTerm):(nIdxTerm)))))
;
250347 if( res==0 ) res = nTerm - nIdxTerm;
250348 if( res<0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250349 }
250350
250351 fts5IntegrityCheckPgidx(p, pLeaf);
250352 }
250353 fts5DataRelease(pLeaf);
250354 if( p->rc ) break;
250355
250356 /* Now check that the iter.nEmpty leaves following the current leaf
250357 ** (a) exist and (b) contain no terms. */
250358 fts5IndexIntegrityCheckEmpty(
250359 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
250360 );
250361 if( p->rc ) break;
250362
250363 /* If there is a doclist-index, check that it looks right. */
250364 if( bIdxDlidx ){
250365 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
250366 int iPrevLeaf = iIdxLeaf;
250367 int iSegid = pSeg->iSegid;
250368 int iPg = 0;
250369 i64 iKey;
250370
250371 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
250372 fts5DlidxIterEof(p, pDlidx)==0;
250373 fts5DlidxIterNext(p, pDlidx)
250374 ){
250375
250376 /* Check any rowid-less pages that occur before the current leaf. */
250377 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
250378 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPg)) )
;
250379 pLeaf = fts5DataRead(p, iKey);
250380 if( pLeaf ){
250381 if( fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p))!=0 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250382 fts5DataRelease(pLeaf);
250383 }
250384 }
250385 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
250386
250387 /* Check that the leaf page indicated by the iterator really does
250388 ** contain the rowid suggested by the same. */
250389 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf)( ((i64)(iSegid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(iPrevLeaf)) )
;
250390 pLeaf = fts5DataRead(p, iKey);
250391 if( pLeaf ){
250392 i64 iRowid;
250393 int iRowidOff = fts5LeafFirstRowidOff(pLeaf)(fts5GetU16((pLeaf)->p));
250394 ASSERT_SZLEAF_OK(pLeaf)((void) (0));
250395 if( iRowidOff>=pLeaf->szLeaf ){
250396 p->rc = FTS5_CORRUPT(11 | (1<<8));
250397 }else if( bSecureDelete==0 || iRowidOff>0 ){
250398 i64 iDlRowid = fts5DlidxIterRowid(pDlidx);
250399 fts5GetVarintsqlite3Fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
250400 if( iRowid<iDlRowid || (bSecureDelete==0 && iRowid!=iDlRowid) ){
250401 p->rc = FTS5_CORRUPT(11 | (1<<8));
250402 }
250403 }
250404 fts5DataRelease(pLeaf);
250405 }
250406 }
250407
250408 iDlidxPrevLeaf = iPg;
250409 fts5DlidxIterFree(pDlidx);
250410 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
250411 }else{
250412 iDlidxPrevLeaf = pSeg->pgnoLast;
250413 /* TODO: Check there is no doclist index */
250414 }
250415
250416 iIdxPrevLeaf = iIdxLeaf;
250417 }
250418
250419 rc2 = sqlite3_finalize(pStmt);
250420 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
250421
250422 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
250423#if 0
250424 if( p->rc==SQLITE_OK0 && iter.iLeaf!=pSeg->pgnoLast ){
250425 p->rc = FTS5_CORRUPT(11 | (1<<8));
250426 }
250427#endif
250428}
250429
250430
250431/*
250432** Run internal checks to ensure that the FTS index (a) is internally
250433** consistent and (b) contains entries for which the XOR of the checksums
250434** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
250435**
250436** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
250437** checksum does not match. Return SQLITE_OK if all checks pass without
250438** error, or some other SQLite error code if another error (e.g. OOM)
250439** occurs.
250440*/
250441static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum, int bUseCksum){
250442 int eDetail = p->pConfig->eDetail;
250443 u64 cksum2 = 0; /* Checksum based on contents of indexes */
250444 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
250445 Fts5Iter *pIter; /* Used to iterate through entire index */
250446 Fts5Structure *pStruct; /* Index structure */
250447 int iLvl, iSeg;
250448
250449#ifdef SQLITE_DEBUG
250450 /* Used by extra internal tests only run if NDEBUG is not defined */
250451 u64 cksum3 = 0; /* Checksum based on contents of indexes */
250452 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
250453#endif
250454 const int flags = FTS5INDEX_QUERY_NOOUTPUT0x0020;
250455
250456 /* Load the FTS index structure */
250457 pStruct = fts5StructureRead(p);
250458 if( pStruct==0 ){
250459 assert( p->rc!=SQLITE_OK )((void) (0));
250460 return fts5IndexReturn(p);
250461 }
250462
250463 /* Check that the internal nodes of each segment match the leaves */
250464 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
250465 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
250466 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
250467 fts5IndexIntegrityCheckSegment(p, pSeg);
250468 }
250469 }
250470
250471 /* The cksum argument passed to this function is a checksum calculated
250472 ** based on all expected entries in the FTS index (including prefix index
250473 ** entries). This block checks that a checksum calculated based on the
250474 ** actual contents of FTS index is identical.
250475 **
250476 ** Two versions of the same checksum are calculated. The first (stack
250477 ** variable cksum2) based on entries extracted from the full-text index
250478 ** while doing a linear scan of each individual index in turn.
250479 **
250480 ** As each term visited by the linear scans, a separate query for the
250481 ** same term is performed. cksum3 is calculated based on the entries
250482 ** extracted by these queries.
250483 */
250484 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
250485 fts5MultiIterEof(p, pIter)==0;
250486 fts5MultiIterNext(p, pIter, 0, 0)
250487 ){
250488 int n; /* Size of term in bytes */
250489 i64 iPos = 0; /* Position read from poslist */
250490 int iOff = 0; /* Offset within poslist */
250491 i64 iRowid = fts5MultiIterRowid(pIter);
250492 char *z = (char*)fts5MultiIterTerm(pIter, &n);
250493
250494 /* If this is a new term, query for it. Update cksum3 with the results. */
250495 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
250496 if( p->rc ) break;
250497
250498 if( eDetail==FTS5_DETAIL_NONE1 ){
250499 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
250500 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
250501 }
250502 }else{
250503 poslist.n = 0;
250504 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
250505 fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0")sqlite3Fts5BufferAppendBlob(&p->rc,&poslist,4,(const
u8*)"\0\0\0\0")
;
250506 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
250507 int iCol = FTS5_POS2COLUMN(iPos)(int)((iPos >> 32) & 0x7FFFFFFF);
250508 int iTokOff = FTS5_POS2OFFSET(iPos)(int)(iPos & 0x7FFFFFFF);
250509 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
250510 }
250511 }
250512 }
250513 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
250514
250515 fts5MultiIterFree(pIter);
250516 if( p->rc==SQLITE_OK0 && bUseCksum && cksum!=cksum2 ) p->rc = FTS5_CORRUPT(11 | (1<<8));
250517
250518 fts5StructureRelease(pStruct);
250519#ifdef SQLITE_DEBUG
250520 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
250521#endif
250522 fts5BufferFree(&poslist)sqlite3Fts5BufferFree(&poslist);
250523 return fts5IndexReturn(p);
250524}
250525
250526/*************************************************************************
250527**************************************************************************
250528** Below this point is the implementation of the fts5_decode() scalar
250529** function only.
250530*/
250531
250532#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250533/*
250534** Decode a segment-data rowid from the %_data table. This function is
250535** the opposite of macro FTS5_SEGMENT_ROWID().
250536*/
250537static void fts5DecodeRowid(
250538 i64 iRowid, /* Rowid from %_data table */
250539 int *pbTombstone, /* OUT: Tombstone hash flag */
250540 int *piSegid, /* OUT: Segment id */
250541 int *pbDlidx, /* OUT: Dlidx flag */
250542 int *piHeight, /* OUT: Height */
250543 int *piPgno /* OUT: Page number */
250544){
250545 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B31) - 1));
250546 iRowid >>= FTS5_DATA_PAGE_B31;
250547
250548 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B5) - 1));
250549 iRowid >>= FTS5_DATA_HEIGHT_B5;
250550
250551 *pbDlidx = (int)(iRowid & 0x0001);
250552 iRowid >>= FTS5_DATA_DLI_B1;
250553
250554 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B16) - 1));
250555 iRowid >>= FTS5_DATA_ID_B16;
250556
250557 *pbTombstone = (int)(iRowid & 0x0001);
250558}
250559#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250560
250561#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250562static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
250563 int iSegid, iHeight, iPgno, bDlidx, bTomb; /* Rowid compenents */
250564 fts5DecodeRowid(iKey, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
250565
250566 if( iSegid==0 ){
250567 if( iKey==FTS5_AVERAGES_ROWID1 ){
250568 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
250569 }else{
250570 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
250571 }
250572 }
250573 else{
250574 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%s%ssegid=%d h=%d pgno=%d}",
250575 bDlidx ? "dlidx " : "",
250576 bTomb ? "tombstone " : "",
250577 iSegid, iHeight, iPgno
250578 );
250579 }
250580}
250581#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250582
250583#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250584static void fts5DebugStructure(
250585 int *pRc, /* IN/OUT: error code */
250586 Fts5Buffer *pBuf,
250587 Fts5Structure *p
250588){
250589 int iLvl, iSeg; /* Iterate through levels, segments */
250590
250591 for(iLvl=0; iLvl<p->nLevel; iLvl++){
250592 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
250593 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
250594 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
250595 );
250596 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
250597 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
250598 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d",
250599 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
250600 );
250601 if( pSeg->iOrigin1>0 ){
250602 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " origin=%lld..%lld",
250603 pSeg->iOrigin1, pSeg->iOrigin2
250604 );
250605 }
250606 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
250607 }
250608 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
250609 }
250610}
250611#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250612
250613#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250614/*
250615** This is part of the fts5_decode() debugging aid.
250616**
250617** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
250618** function appends a human-readable representation of the same object
250619** to the buffer passed as the second argument.
250620*/
250621static void fts5DecodeStructure(
250622 int *pRc, /* IN/OUT: error code */
250623 Fts5Buffer *pBuf,
250624 const u8 *pBlob, int nBlob
250625){
250626 int rc; /* Return code */
250627 Fts5Structure *p = 0; /* Decoded structure object */
250628
250629 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
250630 if( rc!=SQLITE_OK0 ){
250631 *pRc = rc;
250632 return;
250633 }
250634
250635 fts5DebugStructure(pRc, pBuf, p);
250636 fts5StructureRelease(p);
250637}
250638#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250639
250640#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250641/*
250642** This is part of the fts5_decode() debugging aid.
250643**
250644** Arguments pBlob/nBlob contain an "averages" record. This function
250645** appends a human-readable representation of record to the buffer passed
250646** as the second argument.
250647*/
250648static void fts5DecodeAverages(
250649 int *pRc, /* IN/OUT: error code */
250650 Fts5Buffer *pBuf,
250651 const u8 *pBlob, int nBlob
250652){
250653 int i = 0;
250654 const char *zSpace = "";
250655
250656 while( i<nBlob ){
250657 u64 iVal;
250658 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
250659 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
250660 zSpace = " ";
250661 }
250662}
250663#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250664
250665#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250666/*
250667** Buffer (a/n) is assumed to contain a list of serialized varints. Read
250668** each varint and append its string representation to buffer pBuf. Return
250669** after either the input buffer is exhausted or a 0 value is read.
250670**
250671** The return value is the number of bytes read from the input buffer.
250672*/
250673static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
250674 int iOff = 0;
250675 while( iOff<n ){
250676 int iVal;
250677 iOff += fts5GetVarint32(&a[iOff], iVal)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(iVal));
250678 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
250679 }
250680 return iOff;
250681}
250682#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250683
250684#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250685/*
250686** The start of buffer (a/n) contains the start of a doclist. The doclist
250687** may or may not finish within the buffer. This function appends a text
250688** representation of the part of the doclist that is present to buffer
250689** pBuf.
250690**
250691** The return value is the number of bytes read from the input buffer.
250692*/
250693static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
250694 i64 iDocid = 0;
250695 int iOff = 0;
250696
250697 if( n>0 ){
250698 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
250699 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
250700 }
250701 while( iOff<n ){
250702 int nPos;
250703 int bDel;
250704 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
250705 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
250706 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos)((n-iOff)<(nPos)?(n-iOff):(nPos)));
250707 if( iOff<n ){
250708 i64 iDelta;
250709 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
250710 iDocid += iDelta;
250711 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
250712 }
250713 }
250714
250715 return iOff;
250716}
250717#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250718
250719#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250720/*
250721** This function is part of the fts5_decode() debugging function. It is
250722** only ever used with detail=none tables.
250723**
250724** Buffer (pData/nData) contains a doclist in the format used by detail=none
250725** tables. This function appends a human-readable version of that list to
250726** buffer pBuf.
250727**
250728** If *pRc is other than SQLITE_OK when this function is called, it is a
250729** no-op. If an OOM or other error occurs within this function, *pRc is
250730** set to an SQLite error code before returning. The final state of buffer
250731** pBuf is undefined in this case.
250732*/
250733static void fts5DecodeRowidList(
250734 int *pRc, /* IN/OUT: Error code */
250735 Fts5Buffer *pBuf, /* Buffer to append text to */
250736 const u8 *pData, int nData /* Data to decode list-of-rowids from */
250737){
250738 int i = 0;
250739 i64 iRowid = 0;
250740
250741 while( i<nData ){
250742 const char *zApp = "";
250743 u64 iVal;
250744 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
250745 iRowid += iVal;
250746
250747 if( i<nData && pData[i]==0x00 ){
250748 i++;
250749 if( i<nData && pData[i]==0x00 ){
250750 i++;
250751 zApp = "+";
250752 }else{
250753 zApp = "*";
250754 }
250755 }
250756
250757 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
250758 }
250759}
250760#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250761
250762#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250763static void fts5BufferAppendTerm(int *pRc, Fts5Buffer *pBuf, Fts5Buffer *pTerm){
250764 int ii;
250765 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) )
;
250766 if( *pRc==SQLITE_OK0 ){
250767 for(ii=0; ii<pTerm->n; ii++){
250768 if( pTerm->p[ii]==0x00 ){
250769 pBuf->p[pBuf->n++] = '\\';
250770 pBuf->p[pBuf->n++] = '0';
250771 }else{
250772 pBuf->p[pBuf->n++] = pTerm->p[ii];
250773 }
250774 }
250775 pBuf->p[pBuf->n] = 0x00;
250776 }
250777}
250778#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
250779
250780#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
250781/*
250782** The implementation of user-defined scalar function fts5_decode().
250783*/
250784static void fts5DecodeFunction(
250785 sqlite3_context *pCtx, /* Function call context */
250786 int nArg, /* Number of args (always 2) */
250787 sqlite3_value **apVal /* Function arguments */
250788){
250789 i64 iRowid; /* Rowid for record being decoded */
250790 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
250791 int bTomb;
250792 const u8 *aBlob; int n; /* Record to decode */
250793 u8 *a = 0;
250794 Fts5Buffer s; /* Build up text to return here */
250795 int rc = SQLITE_OK0; /* Return code */
250796 sqlite3_int64 nSpace = 0;
250797 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
250798
250799 assert( nArg==2 )((void) (0));
250800 UNUSED_PARAM(nArg)(void)(nArg);
250801 memset(&s, 0, sizeof(Fts5Buffer));
250802 iRowid = sqlite3_value_int64(apVal[0]);
250803
250804 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
250805 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
250806 ** buffer overreads even if the record is corrupt. */
250807 n = sqlite3_value_bytes(apVal[1]);
250808 aBlob = sqlite3_value_blob(apVal[1]);
250809 nSpace = n + FTS5_DATA_ZERO_PADDING8;
250810 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
250811 if( a==0 ) goto decode_out;
250812 if( n>0 ) memcpy(a, aBlob, n);
250813
250814 fts5DecodeRowid(iRowid, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno);
250815
250816 fts5DebugRowid(&rc, &s, iRowid);
250817 if( bDlidx ){
250818 Fts5Data dlidx;
250819 Fts5DlidxLvl lvl;
250820
250821 dlidx.p = a;
250822 dlidx.nn = n;
250823
250824 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
250825 lvl.pData = &dlidx;
250826 lvl.iLeafPgno = iPgno;
250827
250828 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
250829 sqlite3Fts5BufferAppendPrintf(&rc, &s,
250830 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
250831 );
250832 }
250833 }else if( bTomb ){
250834 u32 nElem = fts5GetU32(&a[4]);
250835 int szKey = (aBlob[0]==4 || aBlob[0]==8) ? aBlob[0] : 8;
250836 int nSlot = (n - 8) / szKey;
250837 int ii;
250838 sqlite3Fts5BufferAppendPrintf(&rc, &s, " nElem=%d", (int)nElem);
250839 if( aBlob[1] ){
250840 sqlite3Fts5BufferAppendPrintf(&rc, &s, " 0");
250841 }
250842 for(ii=0; ii<nSlot; ii++){
250843 u64 iVal = 0;
250844 if( szKey==4 ){
250845 u32 *aSlot = (u32*)&aBlob[8];
250846 if( aSlot[ii] ) iVal = fts5GetU32((u8*)&aSlot[ii]);
250847 }else{
250848 u64 *aSlot = (u64*)&aBlob[8];
250849 if( aSlot[ii] ) iVal = fts5GetU64((u8*)&aSlot[ii]);
250850 }
250851 if( iVal!=0 ){
250852 sqlite3Fts5BufferAppendPrintf(&rc, &s, " %lld", (i64)iVal);
250853 }
250854 }
250855 }else if( iSegid==0 ){
250856 if( iRowid==FTS5_AVERAGES_ROWID1 ){
250857 fts5DecodeAverages(&rc, &s, a, n);
250858 }else{
250859 fts5DecodeStructure(&rc, &s, a, n);
250860 }
250861 }else if( eDetailNone ){
250862 Fts5Buffer term; /* Current term read from page */
250863 int szLeaf;
250864 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
250865 int iTermOff;
250866 int nKeep = 0;
250867 int iOff;
250868
250869 memset(&term, 0, sizeof(Fts5Buffer));
250870
250871 /* Decode any entries that occur before the first term. */
250872 if( szLeaf<n ){
250873 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(iTermOff
))
;
250874 }else{
250875 iTermOff = szLeaf;
250876 }
250877 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
250878
250879 iOff = iTermOff;
250880 while( iOff<szLeaf && rc==SQLITE_OK0 ){
250881 int nAppend;
250882
250883 /* Read the term data for the next term*/
250884 iOff += fts5GetVarint32(&a[iOff], nAppend)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nAppend));
250885 term.n = nKeep;
250886 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff])sqlite3Fts5BufferAppendBlob(&rc,&term,nAppend,&a[
iOff])
;
250887 sqlite3Fts5BufferAppendPrintf(&rc, &s, " term=");
250888 fts5BufferAppendTerm(&rc, &s, &term);
250889 iOff += nAppend;
250890
250891 /* Figure out where the doclist for this term ends */
250892 if( iPgidxOff<n ){
250893 int nIncr;
250894 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(nIncr));
250895 iTermOff += nIncr;
250896 }else{
250897 iTermOff = szLeaf;
250898 }
250899 if( iTermOff>szLeaf ){
250900 rc = FTS5_CORRUPT(11 | (1<<8));
250901 }else{
250902 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
250903 }
250904 iOff = iTermOff;
250905 if( iOff<szLeaf ){
250906 iOff += fts5GetVarint32(&a[iOff], nKeep)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nKeep));
250907 }
250908 }
250909
250910 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
250911 }else{
250912 Fts5Buffer term; /* Current term read from page */
250913 int szLeaf; /* Offset of pgidx in a[] */
250914 int iPgidxOff;
250915 int iPgidxPrev = 0; /* Previous value read from pgidx */
250916 int iTermOff = 0;
250917 int iRowidOff = 0;
250918 int iOff;
250919 int nDoclist;
250920
250921 memset(&term, 0, sizeof(Fts5Buffer));
250922
250923 if( n<4 ){
250924 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
250925 goto decode_out;
250926 }else{
250927 iRowidOff = fts5GetU16(&a[0]);
250928 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
250929 if( iPgidxOff<n ){
250930 fts5GetVarint32(&a[iPgidxOff], iTermOff)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(iTermOff
))
;
250931 }else if( iPgidxOff>n ){
250932 rc = FTS5_CORRUPT(11 | (1<<8));
250933 goto decode_out;
250934 }
250935 }
250936
250937 /* Decode the position list tail at the start of the page */
250938 if( iRowidOff!=0 ){
250939 iOff = iRowidOff;
250940 }else if( iTermOff!=0 ){
250941 iOff = iTermOff;
250942 }else{
250943 iOff = szLeaf;
250944 }
250945 if( iOff>n ){
250946 rc = FTS5_CORRUPT(11 | (1<<8));
250947 goto decode_out;
250948 }
250949 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
250950
250951 /* Decode any more doclist data that appears on the page before the
250952 ** first term. */
250953 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
250954 if( nDoclist+iOff>n ){
250955 rc = FTS5_CORRUPT(11 | (1<<8));
250956 goto decode_out;
250957 }
250958 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
250959
250960 while( iPgidxOff<n && rc==SQLITE_OK0 ){
250961 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
250962 int nByte; /* Bytes of data */
250963 int iEnd;
250964
250965 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(nByte));
250966 iPgidxPrev += nByte;
250967 iOff = iPgidxPrev;
250968
250969 if( iPgidxOff<n ){
250970 fts5GetVarint32(&a[iPgidxOff], nByte)sqlite3Fts5GetVarint32(&a[iPgidxOff],(u32*)&(nByte));
250971 iEnd = iPgidxPrev + nByte;
250972 }else{
250973 iEnd = szLeaf;
250974 }
250975 if( iEnd>szLeaf ){
250976 rc = FTS5_CORRUPT(11 | (1<<8));
250977 break;
250978 }
250979
250980 if( bFirst==0 ){
250981 iOff += fts5GetVarint32(&a[iOff], nByte)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nByte));
250982 if( nByte>term.n ){
250983 rc = FTS5_CORRUPT(11 | (1<<8));
250984 break;
250985 }
250986 term.n = nByte;
250987 }
250988 iOff += fts5GetVarint32(&a[iOff], nByte)sqlite3Fts5GetVarint32(&a[iOff],(u32*)&(nByte));
250989 if( iOff+nByte>n ){
250990 rc = FTS5_CORRUPT(11 | (1<<8));
250991 break;
250992 }
250993 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff])sqlite3Fts5BufferAppendBlob(&rc,&term,nByte,&a[iOff
])
;
250994 iOff += nByte;
250995
250996 sqlite3Fts5BufferAppendPrintf(&rc, &s, " term=");
250997 fts5BufferAppendTerm(&rc, &s, &term);
250998 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
250999 }
251000
251001 fts5BufferFree(&term)sqlite3Fts5BufferFree(&term);
251002 }
251003
251004 decode_out:
251005 sqlite3_free(a);
251006 if( rc==SQLITE_OK0 ){
251007 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
251008 }else{
251009 sqlite3_result_error_code(pCtx, rc);
251010 }
251011 fts5BufferFree(&s)sqlite3Fts5BufferFree(&s);
251012}
251013#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
251014
251015#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
251016/*
251017** The implementation of user-defined scalar function fts5_rowid().
251018*/
251019static void fts5RowidFunction(
251020 sqlite3_context *pCtx, /* Function call context */
251021 int nArg, /* Number of args (always 2) */
251022 sqlite3_value **apVal /* Function arguments */
251023){
251024 const char *zArg;
251025 if( nArg==0 ){
251026 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
251027 }else{
251028 zArg = (const char*)sqlite3_value_text(apVal[0]);
251029 if( 0==sqlite3_stricmp(zArg, "segment") ){
251030 i64 iRowid;
251031 int segid, pgno;
251032 if( nArg!=3 ){
251033 sqlite3_result_error(pCtx,
251034 "should be: fts5_rowid('segment', segid, pgno))", -1
251035 );
251036 }else{
251037 segid = sqlite3_value_int(apVal[1]);
251038 pgno = sqlite3_value_int(apVal[2]);
251039 iRowid = FTS5_SEGMENT_ROWID(segid, pgno)( ((i64)(segid) << (31 +5 +1)) + ((i64)(0) << (31
+ 5)) + ((i64)(0) << (31)) + ((i64)(pgno)) )
;
251040 sqlite3_result_int64(pCtx, iRowid);
251041 }
251042 }else{
251043 sqlite3_result_error(pCtx,
251044 "first arg to fts5_rowid() must be 'segment'" , -1
251045 );
251046 }
251047 }
251048}
251049#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
251050
251051#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
251052
251053typedef struct Fts5StructVtab Fts5StructVtab;
251054struct Fts5StructVtab {
251055 sqlite3_vtab base;
251056};
251057
251058typedef struct Fts5StructVcsr Fts5StructVcsr;
251059struct Fts5StructVcsr {
251060 sqlite3_vtab_cursor base;
251061 Fts5Structure *pStruct;
251062 int iLevel;
251063 int iSeg;
251064 int iRowid;
251065};
251066
251067/*
251068** Create a new fts5_structure() table-valued function.
251069*/
251070static int fts5structConnectMethod(
251071 sqlite3 *db,
251072 void *pAux,
251073 int argc, const char *const*argv,
251074 sqlite3_vtab **ppVtab,
251075 char **pzErr
251076){
251077 Fts5StructVtab *pNew = 0;
251078 int rc = SQLITE_OK0;
251079
251080 rc = sqlite3_declare_vtab(db,
251081 "CREATE TABLE xyz("
251082 "level, segment, merge, segid, leaf1, leaf2, loc1, loc2, "
251083 "npgtombstone, nentrytombstone, nentry, struct HIDDEN);"
251084 );
251085 if( rc==SQLITE_OK0 ){
251086 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
251087 }
251088
251089 *ppVtab = (sqlite3_vtab*)pNew;
251090 return rc;
251091}
251092
251093/*
251094** We must have a single struct=? constraint that will be passed through
251095** into the xFilter method. If there is no valid struct=? constraint,
251096** then return an SQLITE_CONSTRAINT error.
251097*/
251098static int fts5structBestIndexMethod(
251099 sqlite3_vtab *tab,
251100 sqlite3_index_info *pIdxInfo
251101){
251102 int i;
251103 int rc = SQLITE_CONSTRAINT19;
251104 struct sqlite3_index_constraint *p;
251105 pIdxInfo->estimatedCost = (double)100;
251106 pIdxInfo->estimatedRows = 100;
251107 pIdxInfo->idxNum = 0;
251108 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
251109 if( p->usable==0 ) continue;
251110 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && p->iColumn==11 ){
251111 rc = SQLITE_OK0;
251112 pIdxInfo->aConstraintUsage[i].omit = 1;
251113 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
251114 break;
251115 }
251116 }
251117 return rc;
251118}
251119
251120/*
251121** This method is the destructor for bytecodevtab objects.
251122*/
251123static int fts5structDisconnectMethod(sqlite3_vtab *pVtab){
251124 Fts5StructVtab *p = (Fts5StructVtab*)pVtab;
251125 sqlite3_free(p);
251126 return SQLITE_OK0;
251127}
251128
251129/*
251130** Constructor for a new bytecodevtab_cursor object.
251131*/
251132static int fts5structOpenMethod(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
251133 int rc = SQLITE_OK0;
251134 Fts5StructVcsr *pNew = 0;
251135
251136 pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
251137 *ppCsr = (sqlite3_vtab_cursor*)pNew;
251138
251139 return SQLITE_OK0;
251140}
251141
251142/*
251143** Destructor for a bytecodevtab_cursor.
251144*/
251145static int fts5structCloseMethod(sqlite3_vtab_cursor *cur){
251146 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
251147 fts5StructureRelease(pCsr->pStruct);
251148 sqlite3_free(pCsr);
251149 return SQLITE_OK0;
251150}
251151
251152
251153/*
251154** Advance a bytecodevtab_cursor to its next row of output.
251155*/
251156static int fts5structNextMethod(sqlite3_vtab_cursor *cur){
251157 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
251158 Fts5Structure *p = pCsr->pStruct;
251159
251160 assert( pCsr->pStruct )((void) (0));
251161 pCsr->iSeg++;
251162 pCsr->iRowid++;
251163 while( pCsr->iLevel<p->nLevel && pCsr->iSeg>=p->aLevel[pCsr->iLevel].nSeg ){
251164 pCsr->iLevel++;
251165 pCsr->iSeg = 0;
251166 }
251167 if( pCsr->iLevel>=p->nLevel ){
251168 fts5StructureRelease(pCsr->pStruct);
251169 pCsr->pStruct = 0;
251170 }
251171 return SQLITE_OK0;
251172}
251173
251174/*
251175** Return TRUE if the cursor has been moved off of the last
251176** row of output.
251177*/
251178static int fts5structEofMethod(sqlite3_vtab_cursor *cur){
251179 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
251180 return pCsr->pStruct==0;
251181}
251182
251183static int fts5structRowidMethod(
251184 sqlite3_vtab_cursor *cur,
251185 sqlite_int64 *piRowid
251186){
251187 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
251188 *piRowid = pCsr->iRowid;
251189 return SQLITE_OK0;
251190}
251191
251192/*
251193** Return values of columns for the row at which the bytecodevtab_cursor
251194** is currently pointing.
251195*/
251196static int fts5structColumnMethod(
251197 sqlite3_vtab_cursor *cur, /* The cursor */
251198 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
251199 int i /* Which column to return */
251200){
251201 Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur;
251202 Fts5Structure *p = pCsr->pStruct;
251203 Fts5StructureSegment *pSeg = &p->aLevel[pCsr->iLevel].aSeg[pCsr->iSeg];
251204
251205 switch( i ){
251206 case 0: /* level */
251207 sqlite3_result_int(ctx, pCsr->iLevel);
251208 break;
251209 case 1: /* segment */
251210 sqlite3_result_int(ctx, pCsr->iSeg);
251211 break;
251212 case 2: /* merge */
251213 sqlite3_result_int(ctx, pCsr->iSeg < p->aLevel[pCsr->iLevel].nMerge);
251214 break;
251215 case 3: /* segid */
251216 sqlite3_result_int(ctx, pSeg->iSegid);
251217 break;
251218 case 4: /* leaf1 */
251219 sqlite3_result_int(ctx, pSeg->pgnoFirst);
251220 break;
251221 case 5: /* leaf2 */
251222 sqlite3_result_int(ctx, pSeg->pgnoLast);
251223 break;
251224 case 6: /* origin1 */
251225 sqlite3_result_int64(ctx, pSeg->iOrigin1);
251226 break;
251227 case 7: /* origin2 */
251228 sqlite3_result_int64(ctx, pSeg->iOrigin2);
251229 break;
251230 case 8: /* npgtombstone */
251231 sqlite3_result_int(ctx, pSeg->nPgTombstone);
251232 break;
251233 case 9: /* nentrytombstone */
251234 sqlite3_result_int64(ctx, pSeg->nEntryTombstone);
251235 break;
251236 case 10: /* nentry */
251237 sqlite3_result_int64(ctx, pSeg->nEntry);
251238 break;
251239 }
251240 return SQLITE_OK0;
251241}
251242
251243/*
251244** Initialize a cursor.
251245**
251246** idxNum==0 means show all subprograms
251247** idxNum==1 means show only the main bytecode and omit subprograms.
251248*/
251249static int fts5structFilterMethod(
251250 sqlite3_vtab_cursor *pVtabCursor,
251251 int idxNum, const char *idxStr,
251252 int argc, sqlite3_value **argv
251253){
251254 Fts5StructVcsr *pCsr = (Fts5StructVcsr *)pVtabCursor;
251255 int rc = SQLITE_OK0;
251256
251257 const u8 *aBlob = 0;
251258 int nBlob = 0;
251259
251260 assert( argc==1 )((void) (0));
251261 fts5StructureRelease(pCsr->pStruct);
251262 pCsr->pStruct = 0;
251263
251264 nBlob = sqlite3_value_bytes(argv[0]);
251265 aBlob = (const u8*)sqlite3_value_blob(argv[0]);
251266 rc = fts5StructureDecode(aBlob, nBlob, 0, &pCsr->pStruct);
251267 if( rc==SQLITE_OK0 ){
251268 pCsr->iLevel = 0;
251269 pCsr->iRowid = 0;
251270 pCsr->iSeg = -1;
251271 rc = fts5structNextMethod(pVtabCursor);
251272 }
251273
251274 return rc;
251275}
251276
251277#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */
251278
251279/*
251280** This is called as part of registering the FTS5 module with database
251281** connection db. It registers several user-defined scalar functions useful
251282** with FTS5.
251283**
251284** If successful, SQLITE_OK is returned. If an error occurs, some other
251285** SQLite error code is returned instead.
251286*/
251287static int sqlite3Fts5IndexInit(sqlite3 *db){
251288#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
251289 int rc = sqlite3_create_function(
251290 db, "fts5_decode", 2, SQLITE_UTF81, 0, fts5DecodeFunction, 0, 0
251291 );
251292
251293 if( rc==SQLITE_OK0 ){
251294 rc = sqlite3_create_function(
251295 db, "fts5_decode_none", 2,
251296 SQLITE_UTF81, (void*)db, fts5DecodeFunction, 0, 0
251297 );
251298 }
251299
251300 if( rc==SQLITE_OK0 ){
251301 rc = sqlite3_create_function(
251302 db, "fts5_rowid", -1, SQLITE_UTF81, 0, fts5RowidFunction, 0, 0
251303 );
251304 }
251305
251306 if( rc==SQLITE_OK0 ){
251307 static const sqlite3_module fts5structure_module = {
251308 0, /* iVersion */
251309 0, /* xCreate */
251310 fts5structConnectMethod, /* xConnect */
251311 fts5structBestIndexMethod, /* xBestIndex */
251312 fts5structDisconnectMethod, /* xDisconnect */
251313 0, /* xDestroy */
251314 fts5structOpenMethod, /* xOpen */
251315 fts5structCloseMethod, /* xClose */
251316 fts5structFilterMethod, /* xFilter */
251317 fts5structNextMethod, /* xNext */
251318 fts5structEofMethod, /* xEof */
251319 fts5structColumnMethod, /* xColumn */
251320 fts5structRowidMethod, /* xRowid */
251321 0, /* xUpdate */
251322 0, /* xBegin */
251323 0, /* xSync */
251324 0, /* xCommit */
251325 0, /* xRollback */
251326 0, /* xFindFunction */
251327 0, /* xRename */
251328 0, /* xSavepoint */
251329 0, /* xRelease */
251330 0, /* xRollbackTo */
251331 0, /* xShadowName */
251332 0 /* xIntegrity */
251333 };
251334 rc = sqlite3_create_module(db, "fts5_structure", &fts5structure_module, 0);
251335 }
251336 return rc;
251337#else
251338 return SQLITE_OK0;
251339 UNUSED_PARAM(db)(void)(db);
251340#endif
251341}
251342
251343
251344static int sqlite3Fts5IndexReset(Fts5Index *p){
251345 assert( p->pStruct==0 || p->iStructVersion!=0 )((void) (0));
251346 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
251347 fts5StructureInvalidate(p);
251348 }
251349 return fts5IndexReturn(p);
251350}
251351
251352/*
251353** 2014 Jun 09
251354**
251355** The author disclaims copyright to this source code. In place of
251356** a legal notice, here is a blessing:
251357**
251358** May you do good and not evil.
251359** May you find forgiveness for yourself and forgive others.
251360** May you share freely, never taking more than you give.
251361**
251362******************************************************************************
251363**
251364** This is an SQLite module implementing full-text search.
251365*/
251366
251367
251368/* #include "fts5Int.h" */
251369
251370/*
251371** This variable is set to false when running tests for which the on disk
251372** structures should not be corrupt. Otherwise, true. If it is false, extra
251373** assert() conditions in the fts5 code are activated - conditions that are
251374** only true if it is guaranteed that the fts5 database is not corrupt.
251375*/
251376#ifdef SQLITE_DEBUG
251377SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
251378#endif
251379
251380
251381typedef struct Fts5Auxdata Fts5Auxdata;
251382typedef struct Fts5Auxiliary Fts5Auxiliary;
251383typedef struct Fts5Cursor Fts5Cursor;
251384typedef struct Fts5FullTable Fts5FullTable;
251385typedef struct Fts5Sorter Fts5Sorter;
251386typedef struct Fts5TokenizerModule Fts5TokenizerModule;
251387
251388/*
251389** NOTES ON TRANSACTIONS:
251390**
251391** SQLite invokes the following virtual table methods as transactions are
251392** opened and closed by the user:
251393**
251394** xBegin(): Start of a new transaction.
251395** xSync(): Initial part of two-phase commit.
251396** xCommit(): Final part of two-phase commit.
251397** xRollback(): Rollback the transaction.
251398**
251399** Anything that is required as part of a commit that may fail is performed
251400** in the xSync() callback. Current versions of SQLite ignore any errors
251401** returned by xCommit().
251402**
251403** And as sub-transactions are opened/closed:
251404**
251405** xSavepoint(int S): Open savepoint S.
251406** xRelease(int S): Commit and close savepoint S.
251407** xRollbackTo(int S): Rollback to start of savepoint S.
251408**
251409** During a write-transaction the fts5_index.c module may cache some data
251410** in-memory. It is flushed to disk whenever xSync(), xRelease() or
251411** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
251412** is called.
251413**
251414** Additionally, if SQLITE_DEBUG is defined, an instance of the following
251415** structure is used to record the current transaction state. This information
251416** is not required, but it is used in the assert() statements executed by
251417** function fts5CheckTransactionState() (see below).
251418*/
251419struct Fts5TransactionState {
251420 int eState; /* 0==closed, 1==open, 2==synced */
251421 int iSavepoint; /* Number of open savepoints (0 -> none) */
251422};
251423
251424/*
251425** A single object of this type is allocated when the FTS5 module is
251426** registered with a database handle. It is used to store pointers to
251427** all registered FTS5 extensions - tokenizers and auxiliary functions.
251428*/
251429struct Fts5Global {
251430 fts5_api api; /* User visible part of object (see fts5.h) */
251431 sqlite3 *db; /* Associated database connection */
251432 i64 iNextId; /* Used to allocate unique cursor ids */
251433 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
251434 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
251435 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
251436 Fts5Cursor *pCsr; /* First in list of all open cursors */
251437 u32 aLocaleHdr[4];
251438};
251439
251440/*
251441** Size of header on fts5_locale() values. And macro to access a buffer
251442** containing a copy of the header from an Fts5Config pointer.
251443*/
251444#define FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )) ((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))
251445#define FTS5_LOCALE_HDR(pConfig)((const u8*)(pConfig->pGlobal->aLocaleHdr)) ((const u8*)(pConfig->pGlobal->aLocaleHdr))
251446
251447
251448/*
251449** Each auxiliary function registered with the FTS5 module is represented
251450** by an object of the following type. All such objects are stored as part
251451** of the Fts5Global.pAux list.
251452*/
251453struct Fts5Auxiliary {
251454 Fts5Global *pGlobal; /* Global context for this function */
251455 char *zFunc; /* Function name (nul-terminated) */
251456 void *pUserData; /* User-data pointer */
251457 fts5_extension_function xFunc; /* Callback function */
251458 void (*xDestroy)(void*); /* Destructor function */
251459 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
251460};
251461
251462/*
251463** Each tokenizer module registered with the FTS5 module is represented
251464** by an object of the following type. All such objects are stored as part
251465** of the Fts5Global.pTok list.
251466**
251467** bV2Native:
251468** True if the tokenizer was registered using xCreateTokenizer_v2(), false
251469** for xCreateTokenizer(). If this variable is true, then x2 is populated
251470** with the routines as supplied by the caller and x1 contains synthesized
251471** wrapper routines. In this case the user-data pointer passed to
251472** x1.xCreate should be a pointer to the Fts5TokenizerModule structure,
251473** not a copy of pUserData.
251474**
251475** Of course, if bV2Native is false, then x1 contains the real routines and
251476** x2 the synthesized ones. In this case a pointer to the Fts5TokenizerModule
251477** object should be passed to x2.xCreate.
251478**
251479** The synthesized wrapper routines are necessary for xFindTokenizer(_v2)
251480** calls.
251481*/
251482struct Fts5TokenizerModule {
251483 char *zName; /* Name of tokenizer */
251484 void *pUserData; /* User pointer passed to xCreate() */
251485 int bV2Native; /* True if v2 native tokenizer */
251486 fts5_tokenizer x1; /* Tokenizer functions */
251487 fts5_tokenizer_v2 x2; /* V2 tokenizer functions */
251488 void (*xDestroy)(void*); /* Destructor function */
251489 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
251490};
251491
251492struct Fts5FullTable {
251493 Fts5Table p; /* Public class members from fts5Int.h */
251494 Fts5Storage *pStorage; /* Document store */
251495 Fts5Global *pGlobal; /* Global (connection wide) data */
251496 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
251497 int iSavepoint; /* Successful xSavepoint()+1 */
251498
251499#ifdef SQLITE_DEBUG
251500 struct Fts5TransactionState ts;
251501#endif
251502};
251503
251504struct Fts5MatchPhrase {
251505 Fts5Buffer *pPoslist; /* Pointer to current poslist */
251506 int nTerm; /* Size of phrase in terms */
251507};
251508
251509/*
251510** pStmt:
251511** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
251512**
251513** aIdx[]:
251514** There is one entry in the aIdx[] array for each phrase in the query,
251515** the value of which is the offset within aPoslist[] following the last
251516** byte of the position list for the corresponding phrase.
251517*/
251518struct Fts5Sorter {
251519 sqlite3_stmt *pStmt;
251520 i64 iRowid; /* Current rowid */
251521 const u8 *aPoslist; /* Position lists for current row */
251522 int nIdx; /* Number of entries in aIdx[] */
251523 int aIdx[1]; /* Offsets into aPoslist for current row */
251524};
251525
251526
251527/*
251528** Virtual-table cursor object.
251529**
251530** iSpecial:
251531** If this is a 'special' query (refer to function fts5SpecialMatch()),
251532** then this variable contains the result of the query.
251533**
251534** iFirstRowid, iLastRowid:
251535** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
251536** cursor iterates in ascending order of rowids, iFirstRowid is the lower
251537** limit of rowids to return, and iLastRowid the upper. In other words, the
251538** WHERE clause in the user's query might have been:
251539**
251540** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
251541**
251542** If the cursor iterates in descending order of rowid, iFirstRowid
251543** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
251544** the lower.
251545*/
251546struct Fts5Cursor {
251547 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
251548 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
251549 int *aColumnSize; /* Values for xColumnSize() */
251550 i64 iCsrId; /* Cursor id */
251551
251552 /* Zero from this point onwards on cursor reset */
251553 int ePlan; /* FTS5_PLAN_XXX value */
251554 int bDesc; /* True for "ORDER BY rowid DESC" queries */
251555 i64 iFirstRowid; /* Return no rowids earlier than this */
251556 i64 iLastRowid; /* Return no rowids later than this */
251557 sqlite3_stmt *pStmt; /* Statement used to read %_content */
251558 Fts5Expr *pExpr; /* Expression for MATCH queries */
251559 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
251560 int csrflags; /* Mask of cursor flags (see below) */
251561 i64 iSpecial; /* Result of special query */
251562
251563 /* "rank" function. Populated on demand from vtab.xColumn(). */
251564 char *zRank; /* Custom rank function */
251565 char *zRankArgs; /* Custom rank function args */
251566 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
251567 int nRankArg; /* Number of trailing arguments for rank() */
251568 sqlite3_value **apRankArg; /* Array of trailing arguments */
251569 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
251570
251571 /* Auxiliary data storage */
251572 Fts5Auxiliary *pAux; /* Currently executing extension function */
251573 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
251574
251575 /* Cache used by auxiliary API functions xInst() and xInstCount() */
251576 Fts5PoslistReader *aInstIter; /* One for each phrase */
251577 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
251578 int nInstCount; /* Number of phrase instances */
251579 int *aInst; /* 3 integers per phrase instance */
251580};
251581
251582/*
251583** Bits that make up the "idxNum" parameter passed indirectly by
251584** xBestIndex() to xFilter().
251585*/
251586#define FTS5_BI_MATCH0x0001 0x0001 /* <tbl> MATCH ? */
251587#define FTS5_BI_RANK0x0002 0x0002 /* rank MATCH ? */
251588#define FTS5_BI_ROWID_EQ0x0004 0x0004 /* rowid == ? */
251589#define FTS5_BI_ROWID_LE0x0008 0x0008 /* rowid <= ? */
251590#define FTS5_BI_ROWID_GE0x0010 0x0010 /* rowid >= ? */
251591
251592#define FTS5_BI_ORDER_RANK0x0020 0x0020
251593#define FTS5_BI_ORDER_ROWID0x0040 0x0040
251594#define FTS5_BI_ORDER_DESC0x0080 0x0080
251595
251596/*
251597** Values for Fts5Cursor.csrflags
251598*/
251599#define FTS5CSR_EOF0x01 0x01
251600#define FTS5CSR_REQUIRE_CONTENT0x02 0x02
251601#define FTS5CSR_REQUIRE_DOCSIZE0x04 0x04
251602#define FTS5CSR_REQUIRE_INST0x08 0x08
251603#define FTS5CSR_FREE_ZRANK0x10 0x10
251604#define FTS5CSR_REQUIRE_RESEEK0x20 0x20
251605#define FTS5CSR_REQUIRE_POSLIST0x40 0x40
251606
251607#define BitFlagAllTest(x,y)(((x) & (y))==(y)) (((x) & (y))==(y))
251608#define BitFlagTest(x,y)(((x) & (y))!=0) (((x) & (y))!=0)
251609
251610
251611/*
251612** Macros to Set(), Clear() and Test() cursor flags.
251613*/
251614#define CsrFlagSet(pCsr, flag)((pCsr)->csrflags |= (flag)) ((pCsr)->csrflags |= (flag))
251615#define CsrFlagClear(pCsr, flag)((pCsr)->csrflags &= ~(flag)) ((pCsr)->csrflags &= ~(flag))
251616#define CsrFlagTest(pCsr, flag)((pCsr)->csrflags & (flag)) ((pCsr)->csrflags & (flag))
251617
251618struct Fts5Auxdata {
251619 Fts5Auxiliary *pAux; /* Extension to which this belongs */
251620 void *pPtr; /* Pointer value */
251621 void(*xDelete)(void*); /* Destructor */
251622 Fts5Auxdata *pNext; /* Next object in linked list */
251623};
251624
251625#ifdef SQLITE_DEBUG
251626#define FTS5_BEGIN 1
251627#define FTS5_SYNC 2
251628#define FTS5_COMMIT 3
251629#define FTS5_ROLLBACK 4
251630#define FTS5_SAVEPOINT 5
251631#define FTS5_RELEASE 6
251632#define FTS5_ROLLBACKTO 7
251633static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
251634 switch( op ){
251635 case FTS5_BEGIN:
251636 assert( p->ts.eState==0 )((void) (0));
251637 p->ts.eState = 1;
251638 p->ts.iSavepoint = -1;
251639 break;
251640
251641 case FTS5_SYNC:
251642 assert( p->ts.eState==1 || p->ts.eState==2 )((void) (0));
251643 p->ts.eState = 2;
251644 break;
251645
251646 case FTS5_COMMIT:
251647 assert( p->ts.eState==2 )((void) (0));
251648 p->ts.eState = 0;
251649 break;
251650
251651 case FTS5_ROLLBACK:
251652 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 )((void) (0));
251653 p->ts.eState = 0;
251654 break;
251655
251656 case FTS5_SAVEPOINT:
251657 assert( p->ts.eState>=1 )((void) (0));
251658 assert( iSavepoint>=0 )((void) (0));
251659 assert( iSavepoint>=p->ts.iSavepoint )((void) (0));
251660 p->ts.iSavepoint = iSavepoint;
251661 break;
251662
251663 case FTS5_RELEASE:
251664 assert( p->ts.eState>=1 )((void) (0));
251665 assert( iSavepoint>=0 )((void) (0));
251666 assert( iSavepoint<=p->ts.iSavepoint )((void) (0));
251667 p->ts.iSavepoint = iSavepoint-1;
251668 break;
251669
251670 case FTS5_ROLLBACKTO:
251671 assert( p->ts.eState>=1 )((void) (0));
251672 assert( iSavepoint>=-1 )((void) (0));
251673 /* The following assert() can fail if another vtab strikes an error
251674 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
251675 ** having called xSavepoint() on this vtab. */
251676 /* assert( iSavepoint<=p->ts.iSavepoint ); */
251677 p->ts.iSavepoint = iSavepoint;
251678 break;
251679 }
251680}
251681#else
251682# define fts5CheckTransactionState(x,y,z)
251683#endif
251684
251685/*
251686** Return true if pTab is a contentless table. If parameter bIncludeUnindexed
251687** is true, this includes contentless tables that store UNINDEXED columns
251688** only.
251689*/
251690static int fts5IsContentless(Fts5FullTable *pTab, int bIncludeUnindexed){
251691 int eContent = pTab->p.pConfig->eContent;
251692 return (
251693 eContent==FTS5_CONTENT_NONE1
251694 || (bIncludeUnindexed && eContent==FTS5_CONTENT_UNINDEXED3)
251695 );
251696}
251697
251698/*
251699** Delete a virtual table handle allocated by fts5InitVtab().
251700*/
251701static void fts5FreeVtab(Fts5FullTable *pTab){
251702 if( pTab ){
251703 sqlite3Fts5IndexClose(pTab->p.pIndex);
251704 sqlite3Fts5StorageClose(pTab->pStorage);
251705 sqlite3Fts5ConfigFree(pTab->p.pConfig);
251706 sqlite3_free(pTab);
251707 }
251708}
251709
251710/*
251711** The xDisconnect() virtual table method.
251712*/
251713static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
251714 fts5FreeVtab((Fts5FullTable*)pVtab);
251715 return SQLITE_OK0;
251716}
251717
251718/*
251719** The xDestroy() virtual table method.
251720*/
251721static int fts5DestroyMethod(sqlite3_vtab *pVtab){
251722 Fts5Table *pTab = (Fts5Table*)pVtab;
251723 int rc = sqlite3Fts5DropAll(pTab->pConfig);
251724 if( rc==SQLITE_OK0 ){
251725 fts5FreeVtab((Fts5FullTable*)pVtab);
251726 }
251727 return rc;
251728}
251729
251730/*
251731** This function is the implementation of both the xConnect and xCreate
251732** methods of the FTS3 virtual table.
251733**
251734** The argv[] array contains the following:
251735**
251736** argv[0] -> module name ("fts5")
251737** argv[1] -> database name
251738** argv[2] -> table name
251739** argv[...] -> "column name" and other module argument fields.
251740*/
251741static int fts5InitVtab(
251742 int bCreate, /* True for xCreate, false for xConnect */
251743 sqlite3 *db, /* The SQLite database connection */
251744 void *pAux, /* Hash table containing tokenizers */
251745 int argc, /* Number of elements in argv array */
251746 const char * const *argv, /* xCreate/xConnect argument array */
251747 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
251748 char **pzErr /* Write any error message here */
251749){
251750 Fts5Global *pGlobal = (Fts5Global*)pAux;
251751 const char **azConfig = (const char**)argv;
251752 int rc = SQLITE_OK0; /* Return code */
251753 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
251754 Fts5FullTable *pTab = 0; /* New virtual table object */
251755
251756 /* Allocate the new vtab object and parse the configuration */
251757 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
251758 if( rc==SQLITE_OK0 ){
251759 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
251760 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 )((void) (0));
251761 }
251762 if( rc==SQLITE_OK0 ){
251763 pConfig->pzErrmsg = pzErr;
251764 pTab->p.pConfig = pConfig;
251765 pTab->pGlobal = pGlobal;
251766 if( bCreate || sqlite3Fts5TokenizerPreload(&pConfig->t) ){
251767 rc = sqlite3Fts5LoadTokenizer(pConfig);
251768 }
251769 }
251770
251771 /* Open the index sub-system */
251772 if( rc==SQLITE_OK0 ){
251773 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
251774 }
251775
251776 /* Open the storage sub-system */
251777 if( rc==SQLITE_OK0 ){
251778 rc = sqlite3Fts5StorageOpen(
251779 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
251780 );
251781 }
251782
251783 /* Call sqlite3_declare_vtab() */
251784 if( rc==SQLITE_OK0 ){
251785 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
251786 }
251787
251788 /* Load the initial configuration */
251789 if( rc==SQLITE_OK0 ){
251790 rc = sqlite3Fts5ConfigLoad(pTab->p.pConfig, pTab->p.pConfig->iCookie-1);
251791 }
251792
251793 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
251794 rc = sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, (int)1);
251795 }
251796 if( rc==SQLITE_OK0 ){
251797 rc = sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS2);
251798 }
251799
251800 if( pConfig ) pConfig->pzErrmsg = 0;
251801 if( rc!=SQLITE_OK0 ){
251802 fts5FreeVtab(pTab);
251803 pTab = 0;
251804 }else if( bCreate ){
251805 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
251806 }
251807 *ppVTab = (sqlite3_vtab*)pTab;
251808 return rc;
251809}
251810
251811/*
251812** The xConnect() and xCreate() methods for the virtual table. All the
251813** work is done in function fts5InitVtab().
251814*/
251815static int fts5ConnectMethod(
251816 sqlite3 *db, /* Database connection */
251817 void *pAux, /* Pointer to tokenizer hash table */
251818 int argc, /* Number of elements in argv array */
251819 const char * const *argv, /* xCreate/xConnect argument array */
251820 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
251821 char **pzErr /* OUT: sqlite3_malloc'd error message */
251822){
251823 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
251824}
251825static int fts5CreateMethod(
251826 sqlite3 *db, /* Database connection */
251827 void *pAux, /* Pointer to tokenizer hash table */
251828 int argc, /* Number of elements in argv array */
251829 const char * const *argv, /* xCreate/xConnect argument array */
251830 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
251831 char **pzErr /* OUT: sqlite3_malloc'd error message */
251832){
251833 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
251834}
251835
251836/*
251837** The different query plans.
251838*/
251839#define FTS5_PLAN_MATCH1 1 /* (<tbl> MATCH ?) */
251840#define FTS5_PLAN_SOURCE2 2 /* A source cursor for SORTED_MATCH */
251841#define FTS5_PLAN_SPECIAL3 3 /* An internal query */
251842#define FTS5_PLAN_SORTED_MATCH4 4 /* (<tbl> MATCH ? ORDER BY rank) */
251843#define FTS5_PLAN_SCAN5 5 /* No usable constraint */
251844#define FTS5_PLAN_ROWID6 6 /* (rowid = ?) */
251845
251846/*
251847** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
251848** extension is currently being used by a version of SQLite too old to
251849** support index-info flags. In that case this function is a no-op.
251850*/
251851static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
251852#if SQLITE_VERSION_NUMBER3047001>=3008012
251853#ifndef SQLITE_CORE1
251854 if( sqlite3_libversion_number()>=3008012 )
251855#endif
251856 {
251857 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE0x00000001;
251858 }
251859#endif
251860}
251861
251862static int fts5UsePatternMatch(
251863 Fts5Config *pConfig,
251864 struct sqlite3_index_constraint *p
251865){
251866 assert( FTS5_PATTERN_GLOB==SQLITE_INDEX_CONSTRAINT_GLOB )((void) (0));
251867 assert( FTS5_PATTERN_LIKE==SQLITE_INDEX_CONSTRAINT_LIKE )((void) (0));
251868 if( pConfig->t.ePattern==FTS5_PATTERN_GLOB66 && p->op==FTS5_PATTERN_GLOB66 ){
251869 return 1;
251870 }
251871 if( pConfig->t.ePattern==FTS5_PATTERN_LIKE65
251872 && (p->op==FTS5_PATTERN_LIKE65 || p->op==FTS5_PATTERN_GLOB66)
251873 ){
251874 return 1;
251875 }
251876 return 0;
251877}
251878
251879/*
251880** Implementation of the xBestIndex method for FTS5 tables. Within the
251881** WHERE constraint, it searches for the following:
251882**
251883** 1. A MATCH constraint against the table column.
251884** 2. A MATCH constraint against the "rank" column.
251885** 3. A MATCH constraint against some other column.
251886** 4. An == constraint against the rowid column.
251887** 5. A < or <= constraint against the rowid column.
251888** 6. A > or >= constraint against the rowid column.
251889**
251890** Within the ORDER BY, the following are supported:
251891**
251892** 5. ORDER BY rank [ASC|DESC]
251893** 6. ORDER BY rowid [ASC|DESC]
251894**
251895** Information for the xFilter call is passed via both the idxNum and
251896** idxStr variables. Specifically, idxNum is a bitmask of the following
251897** flags used to encode the ORDER BY clause:
251898**
251899** FTS5_BI_ORDER_RANK
251900** FTS5_BI_ORDER_ROWID
251901** FTS5_BI_ORDER_DESC
251902**
251903** idxStr is used to encode data from the WHERE clause. For each argument
251904** passed to the xFilter method, the following is appended to idxStr:
251905**
251906** Match against table column: "m"
251907** Match against rank column: "r"
251908** Match against other column: "M<column-number>"
251909** LIKE against other column: "L<column-number>"
251910** GLOB against other column: "G<column-number>"
251911** Equality constraint against the rowid: "="
251912** A < or <= against the rowid: "<"
251913** A > or >= against the rowid: ">"
251914**
251915** This function ensures that there is at most one "r" or "=". And that if
251916** there exists an "=" then there is no "<" or ">".
251917**
251918** If an unusable MATCH operator is present in the WHERE clause, then
251919** SQLITE_CONSTRAINT is returned.
251920**
251921** Costs are assigned as follows:
251922**
251923** a) If a MATCH operator is present, the cost depends on the other
251924** constraints also present. As follows:
251925**
251926** * No other constraints: cost=1000.0
251927** * One rowid range constraint: cost=750.0
251928** * Both rowid range constraints: cost=500.0
251929** * An == rowid constraint: cost=100.0
251930**
251931** b) Otherwise, if there is no MATCH:
251932**
251933** * No other constraints: cost=1000000.0
251934** * One rowid range constraint: cost=750000.0
251935** * Both rowid range constraints: cost=250000.0
251936** * An == rowid constraint: cost=10.0
251937**
251938** Costs are not modified by the ORDER BY clause.
251939*/
251940static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
251941 Fts5Table *pTab = (Fts5Table*)pVTab;
251942 Fts5Config *pConfig = pTab->pConfig;
251943 const int nCol = pConfig->nCol;
251944 int idxFlags = 0; /* Parameter passed through to xFilter() */
251945 int i;
251946
251947 char *idxStr;
251948 int iIdxStr = 0;
251949 int iCons = 0;
251950
251951 int bSeenEq = 0;
251952 int bSeenGt = 0;
251953 int bSeenLt = 0;
251954 int nSeenMatch = 0;
251955 int bSeenRank = 0;
251956
251957
251958 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
251959 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
251960 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
251961 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
251962 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
251963
251964 if( pConfig->bLock ){
251965 pTab->base.zErrMsg = sqlite3_mprintf(
251966 "recursively defined fts5 content table"
251967 );
251968 return SQLITE_ERROR1;
251969 }
251970
251971 idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 8 + 1);
251972 if( idxStr==0 ) return SQLITE_NOMEM7;
251973 pInfo->idxStr = idxStr;
251974 pInfo->needToFreeIdxStr = 1;
251975
251976 for(i=0; i<pInfo->nConstraint; i++){
251977 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
251978 int iCol = p->iColumn;
251979 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH64
251980 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol>=nCol)
251981 ){
251982 /* A MATCH operator or equivalent */
251983 if( p->usable==0 || iCol<0 ){
251984 /* As there exists an unusable MATCH constraint this is an
251985 ** unusable plan. Return SQLITE_CONSTRAINT. */
251986 return SQLITE_CONSTRAINT19;
251987 }else{
251988 if( iCol==nCol+1 ){
251989 if( bSeenRank ) continue;
251990 idxStr[iIdxStr++] = 'r';
251991 bSeenRank = 1;
251992 }else{
251993 nSeenMatch++;
251994 idxStr[iIdxStr++] = 'M';
251995 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
251996 idxStr += strlen(&idxStr[iIdxStr]);
251997 assert( idxStr[iIdxStr]=='\0' )((void) (0));
251998 }
251999 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
252000 pInfo->aConstraintUsage[i].omit = 1;
252001 }
252002 }else if( p->usable ){
252003 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
252004 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB )((void) (0));
252005 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE65 ? 'L' : 'G';
252006 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
252007 idxStr += strlen(&idxStr[iIdxStr]);
252008 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
252009 assert( idxStr[iIdxStr]=='\0' )((void) (0));
252010 nSeenMatch++;
252011 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol<0 ){
252012 idxStr[iIdxStr++] = '=';
252013 bSeenEq = 1;
252014 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
252015 }
252016 }
252017 }
252018
252019 if( bSeenEq==0 ){
252020 for(i=0; i<pInfo->nConstraint; i++){
252021 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
252022 if( p->iColumn<0 && p->usable ){
252023 int op = p->op;
252024 if( op==SQLITE_INDEX_CONSTRAINT_LT16 || op==SQLITE_INDEX_CONSTRAINT_LE8 ){
252025 if( bSeenLt ) continue;
252026 idxStr[iIdxStr++] = '<';
252027 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
252028 bSeenLt = 1;
252029 }else
252030 if( op==SQLITE_INDEX_CONSTRAINT_GT4 || op==SQLITE_INDEX_CONSTRAINT_GE32 ){
252031 if( bSeenGt ) continue;
252032 idxStr[iIdxStr++] = '>';
252033 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
252034 bSeenGt = 1;
252035 }
252036 }
252037 }
252038 }
252039 idxStr[iIdxStr] = '\0';
252040
252041 /* Set idxFlags flags for the ORDER BY clause
252042 **
252043 ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC".
252044 */
252045 if( pInfo->nOrderBy==1 ){
252046 int iSort = pInfo->aOrderBy[0].iColumn;
252047 if( iSort==(pConfig->nCol+1) && nSeenMatch>0 ){
252048 idxFlags |= FTS5_BI_ORDER_RANK0x0020;
252049 }else if( iSort==-1 && (!pInfo->aOrderBy[0].desc || !pConfig->bTokendata) ){
252050 idxFlags |= FTS5_BI_ORDER_ROWID0x0040;
252051 }
252052 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID)(((idxFlags) & (0x0020|0x0040))!=0) ){
252053 pInfo->orderByConsumed = 1;
252054 if( pInfo->aOrderBy[0].desc ){
252055 idxFlags |= FTS5_BI_ORDER_DESC0x0080;
252056 }
252057 }
252058 }
252059
252060 /* Calculate the estimated cost based on the flags set in idxFlags. */
252061 if( bSeenEq ){
252062 pInfo->estimatedCost = nSeenMatch ? 1000.0 : 10.0;
252063 if( nSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
252064 }else if( bSeenLt && bSeenGt ){
252065 pInfo->estimatedCost = nSeenMatch ? 5000.0 : 250000.0;
252066 }else if( bSeenLt || bSeenGt ){
252067 pInfo->estimatedCost = nSeenMatch ? 7500.0 : 750000.0;
252068 }else{
252069 pInfo->estimatedCost = nSeenMatch ? 10000.0 : 1000000.0;
252070 }
252071 for(i=1; i<nSeenMatch; i++){
252072 pInfo->estimatedCost *= 0.4;
252073 }
252074
252075 pInfo->idxNum = idxFlags;
252076 return SQLITE_OK0;
252077}
252078
252079static int fts5NewTransaction(Fts5FullTable *pTab){
252080 Fts5Cursor *pCsr;
252081 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
252082 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK0;
252083 }
252084 return sqlite3Fts5StorageReset(pTab->pStorage);
252085}
252086
252087/*
252088** Implementation of xOpen method.
252089*/
252090static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
252091 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
252092 Fts5Config *pConfig = pTab->p.pConfig;
252093 Fts5Cursor *pCsr = 0; /* New cursor object */
252094 sqlite3_int64 nByte; /* Bytes of space to allocate */
252095 int rc; /* Return code */
252096
252097 rc = fts5NewTransaction(pTab);
252098 if( rc==SQLITE_OK0 ){
252099 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
252100 pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
252101 if( pCsr ){
252102 Fts5Global *pGlobal = pTab->pGlobal;
252103 memset(pCsr, 0, (size_t)nByte);
252104 pCsr->aColumnSize = (int*)&pCsr[1];
252105 pCsr->pNext = pGlobal->pCsr;
252106 pGlobal->pCsr = pCsr;
252107 pCsr->iCsrId = ++pGlobal->iNextId;
252108 }else{
252109 rc = SQLITE_NOMEM7;
252110 }
252111 }
252112 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
252113 return rc;
252114}
252115
252116static int fts5StmtType(Fts5Cursor *pCsr){
252117 if( pCsr->ePlan==FTS5_PLAN_SCAN5 ){
252118 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC1 : FTS5_STMT_SCAN_ASC0;
252119 }
252120 return FTS5_STMT_LOOKUP2;
252121}
252122
252123/*
252124** This function is called after the cursor passed as the only argument
252125** is moved to point at a different row. It clears all cached data
252126** specific to the previous row stored by the cursor object.
252127*/
252128static void fts5CsrNewrow(Fts5Cursor *pCsr){
252129 CsrFlagSet(pCsr,((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
252130 FTS5CSR_REQUIRE_CONTENT((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
252131 | FTS5CSR_REQUIRE_DOCSIZE((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
252132 | FTS5CSR_REQUIRE_INST((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
252133 | FTS5CSR_REQUIRE_POSLIST((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40))
252134 )((pCsr)->csrflags |= (0x02 | 0x04 | 0x08 | 0x40));
252135}
252136
252137static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
252138 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
252139 Fts5Auxdata *pData;
252140 Fts5Auxdata *pNext;
252141
252142 sqlite3_free(pCsr->aInstIter);
252143 sqlite3_free(pCsr->aInst);
252144 if( pCsr->pStmt ){
252145 int eStmt = fts5StmtType(pCsr);
252146 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
252147 }
252148 if( pCsr->pSorter ){
252149 Fts5Sorter *pSorter = pCsr->pSorter;
252150 sqlite3_finalize(pSorter->pStmt);
252151 sqlite3_free(pSorter);
252152 }
252153
252154 if( pCsr->ePlan!=FTS5_PLAN_SOURCE2 ){
252155 sqlite3Fts5ExprFree(pCsr->pExpr);
252156 }
252157
252158 for(pData=pCsr->pAuxdata; pData; pData=pNext){
252159 pNext = pData->pNext;
252160 if( pData->xDelete ) pData->xDelete(pData->pPtr);
252161 sqlite3_free(pData);
252162 }
252163
252164 sqlite3_finalize(pCsr->pRankArgStmt);
252165 sqlite3_free(pCsr->apRankArg);
252166
252167 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK)((pCsr)->csrflags & (0x10)) ){
252168 sqlite3_free(pCsr->zRank);
252169 sqlite3_free(pCsr->zRankArgs);
252170 }
252171
252172 sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
252173 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
252174}
252175
252176
252177/*
252178** Close the cursor. For additional information see the documentation
252179** on the xClose method of the virtual table interface.
252180*/
252181static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
252182 if( pCursor ){
252183 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
252184 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
252185 Fts5Cursor **pp;
252186
252187 fts5FreeCursorComponents(pCsr);
252188 /* Remove the cursor from the Fts5Global.pCsr list */
252189 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
252190 *pp = pCsr->pNext;
252191
252192 sqlite3_free(pCsr);
252193 }
252194 return SQLITE_OK0;
252195}
252196
252197static int fts5SorterNext(Fts5Cursor *pCsr){
252198 Fts5Sorter *pSorter = pCsr->pSorter;
252199 int rc;
252200
252201 rc = sqlite3_step(pSorter->pStmt);
252202 if( rc==SQLITE_DONE101 ){
252203 rc = SQLITE_OK0;
252204 CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags |= (0x01|0x02));
252205 }else if( rc==SQLITE_ROW100 ){
252206 const u8 *a;
252207 const u8 *aBlob;
252208 int nBlob;
252209 int i;
252210 int iOff = 0;
252211 rc = SQLITE_OK0;
252212
252213 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
252214 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
252215 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
252216
252217 /* nBlob==0 in detail=none mode. */
252218 if( nBlob>0 ){
252219 for(i=0; i<(pSorter->nIdx-1); i++){
252220 int iVal;
252221 a += fts5GetVarint32(a, iVal)sqlite3Fts5GetVarint32(a,(u32*)&(iVal));
252222 iOff += iVal;
252223 pSorter->aIdx[i] = iOff;
252224 }
252225 pSorter->aIdx[i] = &aBlob[nBlob] - a;
252226 pSorter->aPoslist = a;
252227 }
252228
252229 fts5CsrNewrow(pCsr);
252230 }
252231
252232 return rc;
252233}
252234
252235
252236/*
252237** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
252238** open on table pTab.
252239*/
252240static void fts5TripCursors(Fts5FullTable *pTab){
252241 Fts5Cursor *pCsr;
252242 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
252243 if( pCsr->ePlan==FTS5_PLAN_MATCH1
252244 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
252245 ){
252246 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags |= (0x20));
252247 }
252248 }
252249}
252250
252251/*
252252** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
252253** argument, close and reopen all Fts5IndexIter iterators that the cursor
252254** is using. Then attempt to move the cursor to a rowid equal to or laster
252255** (in the cursors sort order - ASC or DESC) than the current rowid.
252256**
252257** If the new rowid is not equal to the old, set output parameter *pbSkip
252258** to 1 before returning. Otherwise, leave it unchanged.
252259**
252260** Return SQLITE_OK if successful or if no reseek was required, or an
252261** error code if an error occurred.
252262*/
252263static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
252264 int rc = SQLITE_OK0;
252265 assert( *pbSkip==0 )((void) (0));
252266 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags & (0x20)) ){
252267 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
252268 int bDesc = pCsr->bDesc;
252269 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
252270
252271 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
252272 if( rc==SQLITE_OK0 && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
252273 *pbSkip = 1;
252274 }
252275
252276 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK)((pCsr)->csrflags &= ~(0x20));
252277 fts5CsrNewrow(pCsr);
252278 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
252279 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
252280 *pbSkip = 1;
252281 }
252282 }
252283 return rc;
252284}
252285
252286
252287/*
252288** Advance the cursor to the next row in the table that matches the
252289** search criteria.
252290**
252291** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
252292** even if we reach end-of-file. The fts5EofMethod() will be called
252293** subsequently to determine whether or not an EOF was hit.
252294*/
252295static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
252296 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
252297 int rc;
252298
252299 assert( (pCsr->ePlan<3)==((void) (0))
252300 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)((void) (0))
252301 )((void) (0));
252302 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) )((void) (0));
252303
252304 /* If this cursor uses FTS5_PLAN_MATCH and this is a tokendata=1 table,
252305 ** clear any token mappings accumulated at the fts5_index.c level. In
252306 ** other cases, specifically FTS5_PLAN_SOURCE and FTS5_PLAN_SORTED_MATCH,
252307 ** we need to retain the mappings for the entire query. */
252308 if( pCsr->ePlan==FTS5_PLAN_MATCH1
252309 && ((Fts5Table*)pCursor->pVtab)->pConfig->bTokendata
252310 ){
252311 sqlite3Fts5ExprClearTokens(pCsr->pExpr);
252312 }
252313
252314 if( pCsr->ePlan<3 ){
252315 int bSkip = 0;
252316 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
252317 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
252318 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr))((pCsr)->csrflags |= (sqlite3Fts5ExprEof(pCsr->pExpr)));
252319 fts5CsrNewrow(pCsr);
252320 }else{
252321 switch( pCsr->ePlan ){
252322 case FTS5_PLAN_SPECIAL3: {
252323 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
252324 rc = SQLITE_OK0;
252325 break;
252326 }
252327
252328 case FTS5_PLAN_SORTED_MATCH4: {
252329 rc = fts5SorterNext(pCsr);
252330 break;
252331 }
252332
252333 default: {
252334 Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig;
252335 pConfig->bLock++;
252336 rc = sqlite3_step(pCsr->pStmt);
252337 pConfig->bLock--;
252338 if( rc!=SQLITE_ROW100 ){
252339 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
252340 rc = sqlite3_reset(pCsr->pStmt);
252341 if( rc!=SQLITE_OK0 ){
252342 pCursor->pVtab->zErrMsg = sqlite3_mprintf(
252343 "%s", sqlite3_errmsg(pConfig->db)
252344 );
252345 }
252346 }else{
252347 rc = SQLITE_OK0;
252348 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags |= (0x04));
252349 }
252350 break;
252351 }
252352 }
252353 }
252354
252355 return rc;
252356}
252357
252358
252359static int fts5PrepareStatement(
252360 sqlite3_stmt **ppStmt,
252361 Fts5Config *pConfig,
252362 const char *zFmt,
252363 ...
252364){
252365 sqlite3_stmt *pRet = 0;
252366 int rc;
252367 char *zSql;
252368 va_list ap;
252369
252370 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
252371 zSql = sqlite3_vmprintf(zFmt, ap);
252372 if( zSql==0 ){
252373 rc = SQLITE_NOMEM7;
252374 }else{
252375 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
252376 SQLITE_PREPARE_PERSISTENT0x01, &pRet, 0);
252377 if( rc!=SQLITE_OK0 ){
252378 sqlite3Fts5ConfigErrmsg(pConfig, "%s", sqlite3_errmsg(pConfig->db));
252379 }
252380 sqlite3_free(zSql);
252381 }
252382
252383 va_end(ap)__builtin_va_end(ap);
252384 *ppStmt = pRet;
252385 return rc;
252386}
252387
252388static int fts5CursorFirstSorted(
252389 Fts5FullTable *pTab,
252390 Fts5Cursor *pCsr,
252391 int bDesc
252392){
252393 Fts5Config *pConfig = pTab->p.pConfig;
252394 Fts5Sorter *pSorter;
252395 int nPhrase;
252396 sqlite3_int64 nByte;
252397 int rc;
252398 const char *zRank = pCsr->zRank;
252399 const char *zRankArgs = pCsr->zRankArgs;
252400
252401 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
252402 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
252403 pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
252404 if( pSorter==0 ) return SQLITE_NOMEM7;
252405 memset(pSorter, 0, (size_t)nByte);
252406 pSorter->nIdx = nPhrase;
252407
252408 /* TODO: It would be better to have some system for reusing statement
252409 ** handles here, rather than preparing a new one for each query. But that
252410 ** is not possible as SQLite reference counts the virtual table objects.
252411 ** And since the statement required here reads from this very virtual
252412 ** table, saving it creates a circular reference.
252413 **
252414 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
252415 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
252416 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
252417 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
252418 (zRankArgs ? ", " : ""),
252419 (zRankArgs ? zRankArgs : ""),
252420 bDesc ? "DESC" : "ASC"
252421 );
252422
252423 pCsr->pSorter = pSorter;
252424 if( rc==SQLITE_OK0 ){
252425 assert( pTab->pSortCsr==0 )((void) (0));
252426 pTab->pSortCsr = pCsr;
252427 rc = fts5SorterNext(pCsr);
252428 pTab->pSortCsr = 0;
252429 }
252430
252431 if( rc!=SQLITE_OK0 ){
252432 sqlite3_finalize(pSorter->pStmt);
252433 sqlite3_free(pSorter);
252434 pCsr->pSorter = 0;
252435 }
252436
252437 return rc;
252438}
252439
252440static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
252441 int rc;
252442 Fts5Expr *pExpr = pCsr->pExpr;
252443 rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
252444 if( sqlite3Fts5ExprEof(pExpr) ){
252445 CsrFlagSet(pCsr, FTS5CSR_EOF)((pCsr)->csrflags |= (0x01));
252446 }
252447 fts5CsrNewrow(pCsr);
252448 return rc;
252449}
252450
252451/*
252452** Process a "special" query. A special query is identified as one with a
252453** MATCH expression that begins with a '*' character. The remainder of
252454** the text passed to the MATCH operator are used as the special query
252455** parameters.
252456*/
252457static int fts5SpecialMatch(
252458 Fts5FullTable *pTab,
252459 Fts5Cursor *pCsr,
252460 const char *zQuery
252461){
252462 int rc = SQLITE_OK0; /* Return code */
252463 const char *z = zQuery; /* Special query text */
252464 int n; /* Number of bytes in text at z */
252465
252466 while( z[0]==' ' ) z++;
252467 for(n=0; z[n] && z[n]!=' '; n++);
252468
252469 assert( pTab->p.base.zErrMsg==0 )((void) (0));
252470 pCsr->ePlan = FTS5_PLAN_SPECIAL3;
252471
252472 if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){
252473 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
252474 }
252475 else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){
252476 pCsr->iSpecial = pCsr->iCsrId;
252477 }
252478 else{
252479 /* An unrecognized directive. Return an error message. */
252480 pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
252481 rc = SQLITE_ERROR1;
252482 }
252483
252484 return rc;
252485}
252486
252487/*
252488** Search for an auxiliary function named zName that can be used with table
252489** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
252490** structure. Otherwise, if no such function exists, return NULL.
252491*/
252492static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
252493 Fts5Auxiliary *pAux;
252494
252495 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
252496 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
252497 }
252498
252499 /* No function of the specified name was found. Return 0. */
252500 return 0;
252501}
252502
252503
252504static int fts5FindRankFunction(Fts5Cursor *pCsr){
252505 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
252506 Fts5Config *pConfig = pTab->p.pConfig;
252507 int rc = SQLITE_OK0;
252508 Fts5Auxiliary *pAux = 0;
252509 const char *zRank = pCsr->zRank;
252510 const char *zRankArgs = pCsr->zRankArgs;
252511
252512 if( zRankArgs ){
252513 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
252514 if( zSql ){
252515 sqlite3_stmt *pStmt = 0;
252516 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
252517 SQLITE_PREPARE_PERSISTENT0x01, &pStmt, 0);
252518 sqlite3_free(zSql);
252519 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 )((void) (0));
252520 if( rc==SQLITE_OK0 ){
252521 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
252522 sqlite3_int64 nByte;
252523 pCsr->nRankArg = sqlite3_column_count(pStmt);
252524 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
252525 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
252526 if( rc==SQLITE_OK0 ){
252527 int i;
252528 for(i=0; i<pCsr->nRankArg; i++){
252529 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
252530 }
252531 }
252532 pCsr->pRankArgStmt = pStmt;
252533 }else{
252534 rc = sqlite3_finalize(pStmt);
252535 assert( rc!=SQLITE_OK )((void) (0));
252536 }
252537 }
252538 }
252539 }
252540
252541 if( rc==SQLITE_OK0 ){
252542 pAux = fts5FindAuxiliary(pTab, zRank);
252543 if( pAux==0 ){
252544 assert( pTab->p.base.zErrMsg==0 )((void) (0));
252545 pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
252546 rc = SQLITE_ERROR1;
252547 }
252548 }
252549
252550 pCsr->pRank = pAux;
252551 return rc;
252552}
252553
252554
252555static int fts5CursorParseRank(
252556 Fts5Config *pConfig,
252557 Fts5Cursor *pCsr,
252558 sqlite3_value *pRank
252559){
252560 int rc = SQLITE_OK0;
252561 if( pRank ){
252562 const char *z = (const char*)sqlite3_value_text(pRank);
252563 char *zRank = 0;
252564 char *zRankArgs = 0;
252565
252566 if( z==0 ){
252567 if( sqlite3_value_type(pRank)==SQLITE_NULL5 ) rc = SQLITE_ERROR1;
252568 }else{
252569 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
252570 }
252571 if( rc==SQLITE_OK0 ){
252572 pCsr->zRank = zRank;
252573 pCsr->zRankArgs = zRankArgs;
252574 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK)((pCsr)->csrflags |= (0x10));
252575 }else if( rc==SQLITE_ERROR1 ){
252576 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
252577 "parse error in rank function: %s", z
252578 );
252579 }
252580 }else{
252581 if( pConfig->zRank ){
252582 pCsr->zRank = (char*)pConfig->zRank;
252583 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
252584 }else{
252585 pCsr->zRank = (char*)FTS5_DEFAULT_RANK"bm25";
252586 pCsr->zRankArgs = 0;
252587 }
252588 }
252589 return rc;
252590}
252591
252592static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
252593 if( pVal ){
252594 int eType = sqlite3_value_numeric_type(pVal);
252595 if( eType==SQLITE_INTEGER1 ){
252596 return sqlite3_value_int64(pVal);
252597 }
252598 }
252599 return iDefault;
252600}
252601
252602/*
252603** Set the error message on the virtual table passed as the first argument.
252604*/
252605static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
252606 va_list ap; /* ... printf arguments */
252607 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
252608 sqlite3_free(p->p.base.zErrMsg);
252609 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
252610 va_end(ap)__builtin_va_end(ap);
252611}
252612
252613/*
252614** Arrange for subsequent calls to sqlite3Fts5Tokenize() to use the locale
252615** specified by pLocale/nLocale. The buffer indicated by pLocale must remain
252616** valid until after the final call to sqlite3Fts5Tokenize() that will use
252617** the locale.
252618*/
252619static void sqlite3Fts5SetLocale(
252620 Fts5Config *pConfig,
252621 const char *zLocale,
252622 int nLocale
252623){
252624 Fts5TokenizerConfig *pT = &pConfig->t;
252625 pT->pLocale = zLocale;
252626 pT->nLocale = nLocale;
252627}
252628
252629/*
252630** Clear any locale configured by an earlier call to sqlite3Fts5SetLocale().
252631*/
252632static void sqlite3Fts5ClearLocale(Fts5Config *pConfig){
252633 sqlite3Fts5SetLocale(pConfig, 0, 0);
252634}
252635
252636/*
252637** Return true if the value passed as the only argument is an
252638** fts5_locale() value.
252639*/
252640static int sqlite3Fts5IsLocaleValue(Fts5Config *pConfig, sqlite3_value *pVal){
252641 int ret = 0;
252642 if( sqlite3_value_type(pVal)==SQLITE_BLOB4 ){
252643 /* Call sqlite3_value_bytes() after sqlite3_value_blob() in this case.
252644 ** If the blob was created using zeroblob(), then sqlite3_value_blob()
252645 ** may call malloc(). If this malloc() fails, then the values returned
252646 ** by both value_blob() and value_bytes() will be 0. If value_bytes() were
252647 ** called first, then the NULL pointer returned by value_blob() might
252648 ** be dereferenced. */
252649 const u8 *pBlob = sqlite3_value_blob(pVal);
252650 int nBlob = sqlite3_value_bytes(pVal);
252651 if( nBlob>FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))
252652 && 0==memcmp(pBlob, FTS5_LOCALE_HDR(pConfig)((const u8*)(pConfig->pGlobal->aLocaleHdr)), FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )))
252653 ){
252654 ret = 1;
252655 }
252656 }
252657 return ret;
252658}
252659
252660/*
252661** Value pVal is guaranteed to be an fts5_locale() value, according to
252662** sqlite3Fts5IsLocaleValue(). This function extracts the text and locale
252663** from the value and returns them separately.
252664**
252665** If successful, SQLITE_OK is returned and (*ppText) and (*ppLoc) set
252666** to point to buffers containing the text and locale, as utf-8,
252667** respectively. In this case output parameters (*pnText) and (*pnLoc) are
252668** set to the sizes in bytes of these two buffers.
252669**
252670** Or, if an error occurs, then an SQLite error code is returned. The final
252671** value of the four output parameters is undefined in this case.
252672*/
252673static int sqlite3Fts5DecodeLocaleValue(
252674 sqlite3_value *pVal,
252675 const char **ppText,
252676 int *pnText,
252677 const char **ppLoc,
252678 int *pnLoc
252679){
252680 const char *p = sqlite3_value_blob(pVal);
252681 int n = sqlite3_value_bytes(pVal);
252682 int nLoc = 0;
252683
252684 assert( sqlite3_value_type(pVal)==SQLITE_BLOB )((void) (0));
252685 assert( n>FTS5_LOCALE_HDR_SIZE )((void) (0));
252686
252687 for(nLoc=FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )); p[nLoc]; nLoc++){
252688 if( nLoc==(n-1) ){
252689 return SQLITE_MISMATCH20;
252690 }
252691 }
252692 *ppLoc = &p[FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))];
252693 *pnLoc = nLoc - FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ));
252694
252695 *ppText = &p[nLoc+1];
252696 *pnText = n - nLoc - 1;
252697 return SQLITE_OK0;
252698}
252699
252700/*
252701** Argument pVal is the text of a full-text search expression. It may or
252702** may not have been wrapped by fts5_locale(). This function extracts
252703** the text of the expression, and sets output variable (*pzText) to
252704** point to a nul-terminated buffer containing the expression.
252705**
252706** If pVal was an fts5_locale() value, then sqlite3Fts5SetLocale() is called
252707** to set the tokenizer to use the specified locale.
252708**
252709** If output variable (*pbFreeAndReset) is set to true, then the caller
252710** is required to (a) call sqlite3Fts5ClearLocale() to reset the tokenizer
252711** locale, and (b) call sqlite3_free() to free (*pzText).
252712*/
252713static int fts5ExtractExprText(
252714 Fts5Config *pConfig, /* Fts5 configuration */
252715 sqlite3_value *pVal, /* Value to extract expression text from */
252716 char **pzText, /* OUT: nul-terminated buffer of text */
252717 int *pbFreeAndReset /* OUT: Free (*pzText) and clear locale */
252718){
252719 int rc = SQLITE_OK0;
252720
252721 if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
252722 const char *pText = 0;
252723 int nText = 0;
252724 const char *pLoc = 0;
252725 int nLoc = 0;
252726 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
252727 *pzText = sqlite3Fts5Mprintf(&rc, "%.*s", nText, pText);
252728 if( rc==SQLITE_OK0 ){
252729 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
252730 }
252731 *pbFreeAndReset = 1;
252732 }else{
252733 *pzText = (char*)sqlite3_value_text(pVal);
252734 *pbFreeAndReset = 0;
252735 }
252736
252737 return rc;
252738}
252739
252740
252741/*
252742** This is the xFilter interface for the virtual table. See
252743** the virtual table xFilter method documentation for additional
252744** information.
252745**
252746** There are three possible query strategies:
252747**
252748** 1. Full-text search using a MATCH operator.
252749** 2. A by-rowid lookup.
252750** 3. A full-table scan.
252751*/
252752static int fts5FilterMethod(
252753 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
252754 int idxNum, /* Strategy index */
252755 const char *idxStr, /* Unused */
252756 int nVal, /* Number of elements in apVal */
252757 sqlite3_value **apVal /* Arguments for the indexing scheme */
252758){
252759 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
252760 Fts5Config *pConfig = pTab->p.pConfig;
252761 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
252762 int rc = SQLITE_OK0; /* Error code */
252763 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
252764 int bOrderByRank; /* True if ORDER BY rank */
252765 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
252766 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
252767 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
252768 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
252769 int iCol; /* Column on LHS of MATCH operator */
252770 char **pzErrmsg = pConfig->pzErrmsg;
252771 int i;
252772 int iIdxStr = 0;
252773 Fts5Expr *pExpr = 0;
252774
252775 assert( pConfig->bLock==0 )((void) (0));
252776 if( pCsr->ePlan ){
252777 fts5FreeCursorComponents(pCsr);
252778 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
252779 }
252780
252781 assert( pCsr->pStmt==0 )((void) (0));
252782 assert( pCsr->pExpr==0 )((void) (0));
252783 assert( pCsr->csrflags==0 )((void) (0));
252784 assert( pCsr->pRank==0 )((void) (0));
252785 assert( pCsr->zRank==0 )((void) (0));
252786 assert( pCsr->zRankArgs==0 )((void) (0));
252787 assert( pTab->pSortCsr==0 || nVal==0 )((void) (0));
252788
252789 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg )((void) (0));
252790 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
252791
252792 /* Decode the arguments passed through to this function. */
252793 for(i=0; i<nVal; i++){
252794 switch( idxStr[iIdxStr++] ){
252795 case 'r':
252796 pRank = apVal[i];
252797 break;
252798 case 'M': {
252799 char *zText = 0;
252800 int bFreeAndReset = 0;
252801 int bInternal = 0;
252802
252803 rc = fts5ExtractExprText(pConfig, apVal[i], &zText, &bFreeAndReset);
252804 if( rc!=SQLITE_OK0 ) goto filter_out;
252805 if( zText==0 ) zText = "";
252806
252807 iCol = 0;
252808 do{
252809 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
252810 iIdxStr++;
252811 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
252812
252813 if( zText[0]=='*' ){
252814 /* The user has issued a query of the form "MATCH '*...'". This
252815 ** indicates that the MATCH expression is not a full text query,
252816 ** but a request for an internal parameter. */
252817 rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
252818 bInternal = 1;
252819 }else{
252820 char **pzErr = &pTab->p.base.zErrMsg;
252821 rc = sqlite3Fts5ExprNew(pConfig, 0, iCol, zText, &pExpr, pzErr);
252822 if( rc==SQLITE_OK0 ){
252823 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
252824 pExpr = 0;
252825 }
252826 }
252827
252828 if( bFreeAndReset ){
252829 sqlite3_free(zText);
252830 sqlite3Fts5ClearLocale(pConfig);
252831 }
252832
252833 if( bInternal || rc!=SQLITE_OK0 ) goto filter_out;
252834
252835 break;
252836 }
252837 case 'L':
252838 case 'G': {
252839 int bGlob = (idxStr[iIdxStr-1]=='G');
252840 const char *zText = (const char*)sqlite3_value_text(apVal[i]);
252841 iCol = 0;
252842 do{
252843 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
252844 iIdxStr++;
252845 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
252846 if( zText ){
252847 rc = sqlite3Fts5ExprPattern(pConfig, bGlob, iCol, zText, &pExpr);
252848 }
252849 if( rc==SQLITE_OK0 ){
252850 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
252851 pExpr = 0;
252852 }
252853 if( rc!=SQLITE_OK0 ) goto filter_out;
252854 break;
252855 }
252856 case '=':
252857 pRowidEq = apVal[i];
252858 break;
252859 case '<':
252860 pRowidLe = apVal[i];
252861 break;
252862 default: assert( idxStr[iIdxStr-1]=='>' )((void) (0));
252863 pRowidGe = apVal[i];
252864 break;
252865 }
252866 }
252867 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK0x0020) ? 1 : 0);
252868 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC0x0080) ? 1 : 0);
252869
252870 /* Set the cursor upper and lower rowid limits. Only some strategies
252871 ** actually use them. This is ok, as the xBestIndex() method leaves the
252872 ** sqlite3_index_constraint.omit flag clear for range constraints
252873 ** on the rowid field. */
252874 if( pRowidEq ){
252875 pRowidLe = pRowidGe = pRowidEq;
252876 }
252877 if( bDesc ){
252878 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
252879 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
252880 }else{
252881 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
252882 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
252883 }
252884
252885 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
252886 if( rc!=SQLITE_OK0 ) goto filter_out;
252887
252888 if( pTab->pSortCsr ){
252889 /* If pSortCsr is non-NULL, then this call is being made as part of
252890 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
252891 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
252892 ** return results to the user for this query. The current cursor
252893 ** (pCursor) is used to execute the query issued by function
252894 ** fts5CursorFirstSorted() above. */
252895 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 )((void) (0));
252896 assert( nVal==0 && bOrderByRank==0 && bDesc==0 )((void) (0));
252897 assert( pCsr->iLastRowid==LARGEST_INT64 )((void) (0));
252898 assert( pCsr->iFirstRowid==SMALLEST_INT64 )((void) (0));
252899 if( pTab->pSortCsr->bDesc ){
252900 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
252901 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
252902 }else{
252903 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
252904 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
252905 }
252906 pCsr->ePlan = FTS5_PLAN_SOURCE2;
252907 pCsr->pExpr = pTab->pSortCsr->pExpr;
252908 rc = fts5CursorFirst(pTab, pCsr, bDesc);
252909 }else if( pCsr->pExpr ){
252910 assert( rc==SQLITE_OK )((void) (0));
252911 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
252912 if( rc==SQLITE_OK0 ){
252913 if( bOrderByRank ){
252914 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH4;
252915 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
252916 }else{
252917 pCsr->ePlan = FTS5_PLAN_MATCH1;
252918 rc = fts5CursorFirst(pTab, pCsr, bDesc);
252919 }
252920 }
252921 }else if( pConfig->zContent==0 ){
252922 fts5SetVtabError(pTab,"%s: table does not support scanning",pConfig->zName);
252923 rc = SQLITE_ERROR1;
252924 }else{
252925 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
252926 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
252927 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID6 : FTS5_PLAN_SCAN5);
252928 rc = sqlite3Fts5StorageStmt(
252929 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
252930 );
252931 if( rc==SQLITE_OK0 ){
252932 if( pRowidEq!=0 ){
252933 assert( pCsr->ePlan==FTS5_PLAN_ROWID )((void) (0));
252934 sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
252935 }else{
252936 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
252937 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
252938 }
252939 rc = fts5NextMethod(pCursor);
252940 }
252941 }
252942
252943 filter_out:
252944 sqlite3Fts5ExprFree(pExpr);
252945 pConfig->pzErrmsg = pzErrmsg;
252946 return rc;
252947}
252948
252949/*
252950** This is the xEof method of the virtual table. SQLite calls this
252951** routine to find out if it has reached the end of a result set.
252952*/
252953static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
252954 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
252955 return (CsrFlagTest(pCsr, FTS5CSR_EOF)((pCsr)->csrflags & (0x01)) ? 1 : 0);
252956}
252957
252958/*
252959** Return the rowid that the cursor currently points to.
252960*/
252961static i64 fts5CursorRowid(Fts5Cursor *pCsr){
252962 assert( pCsr->ePlan==FTS5_PLAN_MATCH((void) (0))
252963 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH((void) (0))
252964 || pCsr->ePlan==FTS5_PLAN_SOURCE((void) (0))
252965 || pCsr->ePlan==FTS5_PLAN_SCAN((void) (0))
252966 || pCsr->ePlan==FTS5_PLAN_ROWID((void) (0))
252967 )((void) (0));
252968 if( pCsr->pSorter ){
252969 return pCsr->pSorter->iRowid;
252970 }else if( pCsr->ePlan>=FTS5_PLAN_SCAN5 ){
252971 return sqlite3_column_int64(pCsr->pStmt, 0);
252972 }else{
252973 return sqlite3Fts5ExprRowid(pCsr->pExpr);
252974 }
252975}
252976
252977/*
252978** This is the xRowid method. The SQLite core calls this routine to
252979** retrieve the rowid for the current row of the result set. fts5
252980** exposes %_content.rowid as the rowid for the virtual table. The
252981** rowid should be written to *pRowid.
252982*/
252983static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
252984 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
252985 int ePlan = pCsr->ePlan;
252986
252987 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
252988 if( ePlan==FTS5_PLAN_SPECIAL3 ){
252989 *pRowid = 0;
252990 }else{
252991 *pRowid = fts5CursorRowid(pCsr);
252992 }
252993
252994 return SQLITE_OK0;
252995}
252996
252997
252998/*
252999** If the cursor requires seeking (bSeekRequired flag is set), seek it.
253000** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
253001**
253002** If argument bErrormsg is true and an error occurs, an error message may
253003** be left in sqlite3_vtab.zErrMsg.
253004*/
253005static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
253006 int rc = SQLITE_OK0;
253007
253008 /* If the cursor does not yet have a statement handle, obtain one now. */
253009 if( pCsr->pStmt==0 ){
253010 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
253011 int eStmt = fts5StmtType(pCsr);
253012 rc = sqlite3Fts5StorageStmt(
253013 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
253014 );
253015 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 )((void) (0));
253016 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) )((void) (0));
253017 }
253018
253019 if( rc==SQLITE_OK0 && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags & (0x02)) ){
253020 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
253021 assert( pCsr->pExpr )((void) (0));
253022 sqlite3_reset(pCsr->pStmt);
253023 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
253024 pTab->pConfig->bLock++;
253025 rc = sqlite3_step(pCsr->pStmt);
253026 pTab->pConfig->bLock--;
253027 if( rc==SQLITE_ROW100 ){
253028 rc = SQLITE_OK0;
253029 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT)((pCsr)->csrflags &= ~(0x02));
253030 }else{
253031 rc = sqlite3_reset(pCsr->pStmt);
253032 if( rc==SQLITE_OK0 ){
253033 rc = FTS5_CORRUPT(11 | (1<<8));
253034 fts5SetVtabError((Fts5FullTable*)pTab,
253035 "fts5: missing row %lld from content table %s",
253036 fts5CursorRowid(pCsr),
253037 pTab->pConfig->zContent
253038 );
253039 }else if( pTab->pConfig->pzErrmsg ){
253040 fts5SetVtabError((Fts5FullTable*)pTab,
253041 "%s", sqlite3_errmsg(pTab->pConfig->db)
253042 );
253043 }
253044 }
253045 }
253046 return rc;
253047}
253048
253049/*
253050** This function is called to handle an FTS INSERT command. In other words,
253051** an INSERT statement of the form:
253052**
253053** INSERT INTO fts(fts) VALUES($pCmd)
253054** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
253055**
253056** Argument pVal is the value assigned to column "fts" by the INSERT
253057** statement. This function returns SQLITE_OK if successful, or an SQLite
253058** error code if an error occurs.
253059**
253060** The commands implemented by this function are documented in the "Special
253061** INSERT Directives" section of the documentation. It should be updated if
253062** more commands are added to this function.
253063*/
253064static int fts5SpecialInsert(
253065 Fts5FullTable *pTab, /* Fts5 table object */
253066 const char *zCmd, /* Text inserted into table-name column */
253067 sqlite3_value *pVal /* Value inserted into rank column */
253068){
253069 Fts5Config *pConfig = pTab->p.pConfig;
253070 int rc = SQLITE_OK0;
253071 int bError = 0;
253072 int bLoadConfig = 0;
253073
253074 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
253075 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
253076 fts5SetVtabError(pTab,
253077 "'delete-all' may only be used with a "
253078 "contentless or external content fts5 table"
253079 );
253080 rc = SQLITE_ERROR1;
253081 }else{
253082 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
253083 }
253084 bLoadConfig = 1;
253085 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
253086 if( fts5IsContentless(pTab, 1) ){
253087 fts5SetVtabError(pTab,
253088 "'rebuild' may not be used with a contentless fts5 table"
253089 );
253090 rc = SQLITE_ERROR1;
253091 }else{
253092 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
253093 }
253094 bLoadConfig = 1;
253095 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
253096 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
253097 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
253098 int nMerge = sqlite3_value_int(pVal);
253099 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
253100 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
253101 int iArg = sqlite3_value_int(pVal);
253102 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, iArg);
253103#ifdef SQLITE_DEBUG
253104 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
253105 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
253106#endif
253107 }else if( 0==sqlite3_stricmp("flush", zCmd) ){
253108 rc = sqlite3Fts5FlushToDisk(&pTab->p);
253109 }else{
253110 rc = sqlite3Fts5FlushToDisk(&pTab->p);
253111 if( rc==SQLITE_OK0 ){
253112 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
253113 }
253114 if( rc==SQLITE_OK0 ){
253115 rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
253116 }
253117 if( rc==SQLITE_OK0 ){
253118 if( bError ){
253119 rc = SQLITE_ERROR1;
253120 }else{
253121 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
253122 }
253123 }
253124 }
253125
253126 if( rc==SQLITE_OK0 && bLoadConfig ){
253127 pTab->p.pConfig->iCookie--;
253128 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
253129 }
253130
253131 return rc;
253132}
253133
253134static int fts5SpecialDelete(
253135 Fts5FullTable *pTab,
253136 sqlite3_value **apVal
253137){
253138 int rc = SQLITE_OK0;
253139 int eType1 = sqlite3_value_type(apVal[1]);
253140 if( eType1==SQLITE_INTEGER1 ){
253141 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
253142 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2], 0);
253143 }
253144 return rc;
253145}
253146
253147static void fts5StorageInsert(
253148 int *pRc,
253149 Fts5FullTable *pTab,
253150 sqlite3_value **apVal,
253151 i64 *piRowid
253152){
253153 int rc = *pRc;
253154 if( rc==SQLITE_OK0 ){
253155 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, 0, apVal, piRowid);
253156 }
253157 if( rc==SQLITE_OK0 ){
253158 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
253159 }
253160 *pRc = rc;
253161}
253162
253163/*
253164**
253165** This function is called when the user attempts an UPDATE on a contentless
253166** table. Parameter bRowidModified is true if the UPDATE statement modifies
253167** the rowid value. Parameter apVal[] contains the new values for each user
253168** defined column of the fts5 table. pConfig is the configuration object of the
253169** table being updated (guaranteed to be contentless). The contentless_delete=1
253170** and contentless_unindexed=1 options may or may not be set.
253171**
253172** This function returns SQLITE_OK if the UPDATE can go ahead, or an SQLite
253173** error code if it cannot. In this case an error message is also loaded into
253174** pConfig. Output parameter (*pbContent) is set to true if the caller should
253175** update the %_content table only - not the FTS index or any other shadow
253176** table. This occurs when an UPDATE modifies only UNINDEXED columns of the
253177** table.
253178**
253179** An UPDATE may proceed if:
253180**
253181** * The only columns modified are UNINDEXED columns, or
253182**
253183** * The contentless_delete=1 option was specified and all of the indexed
253184** columns (not a subset) have been modified.
253185*/
253186static int fts5ContentlessUpdate(
253187 Fts5Config *pConfig,
253188 sqlite3_value **apVal,
253189 int bRowidModified,
253190 int *pbContent
253191){
253192 int ii;
253193 int bSeenIndex = 0; /* Have seen modified indexed column */
253194 int bSeenIndexNC = 0; /* Have seen unmodified indexed column */
253195 int rc = SQLITE_OK0;
253196
253197 for(ii=0; ii<pConfig->nCol; ii++){
253198 if( pConfig->abUnindexed[ii]==0 ){
253199 if( sqlite3_value_nochange(apVal[ii]) ){
253200 bSeenIndexNC++;
253201 }else{
253202 bSeenIndex++;
253203 }
253204 }
253205 }
253206
253207 if( bSeenIndex==0 && bRowidModified==0 ){
253208 *pbContent = 1;
253209 }else{
253210 if( bSeenIndexNC || pConfig->bContentlessDelete==0 ){
253211 rc = SQLITE_ERROR1;
253212 sqlite3Fts5ConfigErrmsg(pConfig,
253213 (pConfig->bContentlessDelete ?
253214 "%s a subset of columns on fts5 contentless-delete table: %s" :
253215 "%s contentless fts5 table: %s")
253216 , "cannot UPDATE", pConfig->zName
253217 );
253218 }
253219 }
253220
253221 return rc;
253222}
253223
253224/*
253225** This function is the implementation of the xUpdate callback used by
253226** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
253227** inserted, updated or deleted.
253228**
253229** A delete specifies a single argument - the rowid of the row to remove.
253230**
253231** Update and insert operations pass:
253232**
253233** 1. The "old" rowid, or NULL.
253234** 2. The "new" rowid.
253235** 3. Values for each of the nCol matchable columns.
253236** 4. Values for the two hidden columns (<tablename> and "rank").
253237*/
253238static int fts5UpdateMethod(
253239 sqlite3_vtab *pVtab, /* Virtual table handle */
253240 int nArg, /* Size of argument array */
253241 sqlite3_value **apVal, /* Array of arguments */
253242 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
253243){
253244 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
253245 Fts5Config *pConfig = pTab->p.pConfig;
253246 int eType0; /* value_type() of apVal[0] */
253247 int rc = SQLITE_OK0; /* Return code */
253248 int bUpdateOrDelete = 0;
253249
253250 /* A transaction must be open when this is called. */
253251 assert( pTab->ts.eState==1 || pTab->ts.eState==2 )((void) (0));
253252
253253 assert( pVtab->zErrMsg==0 )((void) (0));
253254 assert( nArg==1 || nArg==(2+pConfig->nCol+2) )((void) (0));
253255 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER((void) (0))
253256 || sqlite3_value_type(apVal[0])==SQLITE_NULL((void) (0))
253257 )((void) (0));
253258 assert( pTab->p.pConfig->pzErrmsg==0 )((void) (0));
253259 if( pConfig->pgsz==0 ){
253260 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
253261 if( rc!=SQLITE_OK0 ) return rc;
253262 }
253263
253264 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
253265
253266 /* Put any active cursors into REQUIRE_SEEK state. */
253267 fts5TripCursors(pTab);
253268
253269 eType0 = sqlite3_value_type(apVal[0]);
253270 if( eType0==SQLITE_NULL5
253271 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL5
253272 ){
253273 /* A "special" INSERT op. These are handled separately. */
253274 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
253275 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0
253276 && 0==sqlite3_stricmp("delete", z)
253277 ){
253278 if( pConfig->bContentlessDelete ){
253279 fts5SetVtabError(pTab,
253280 "'delete' may not be used with a contentless_delete=1 table"
253281 );
253282 rc = SQLITE_ERROR1;
253283 }else{
253284 rc = fts5SpecialDelete(pTab, apVal);
253285 bUpdateOrDelete = 1;
253286 }
253287 }else{
253288 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
253289 }
253290 }else{
253291 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
253292 ** any conflict on the rowid value must be detected before any
253293 ** modifications are made to the database file. There are 4 cases:
253294 **
253295 ** 1) DELETE
253296 ** 2) UPDATE (rowid not modified)
253297 ** 3) UPDATE (rowid modified)
253298 ** 4) INSERT
253299 **
253300 ** Cases 3 and 4 may violate the rowid constraint.
253301 */
253302 int eConflict = SQLITE_ABORT4;
253303 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 || pConfig->bContentlessDelete ){
253304 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
253305 }
253306
253307 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL )((void) (0));
253308 assert( nArg!=1 || eType0==SQLITE_INTEGER )((void) (0));
253309
253310 /* DELETE */
253311 if( nArg==1 ){
253312 /* It is only possible to DELETE from a contentless table if the
253313 ** contentless_delete=1 flag is set. */
253314 if( fts5IsContentless(pTab, 1) && pConfig->bContentlessDelete==0 ){
253315 fts5SetVtabError(pTab,
253316 "cannot DELETE from contentless fts5 table: %s", pConfig->zName
253317 );
253318 rc = SQLITE_ERROR1;
253319 }else{
253320 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
253321 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0, 0);
253322 bUpdateOrDelete = 1;
253323 }
253324 }
253325
253326 /* INSERT or UPDATE */
253327 else{
253328 int eType1 = sqlite3_value_numeric_type(apVal[1]);
253329
253330 /* It is an error to write an fts5_locale() value to a table without
253331 ** the locale=1 option. */
253332 if( pConfig->bLocale==0 ){
253333 int ii;
253334 for(ii=0; ii<pConfig->nCol; ii++){
253335 sqlite3_value *pVal = apVal[ii+2];
253336 if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
253337 fts5SetVtabError(pTab, "fts5_locale() requires locale=1");
253338 rc = SQLITE_MISMATCH20;
253339 goto update_out;
253340 }
253341 }
253342 }
253343
253344 if( eType0!=SQLITE_INTEGER1 ){
253345 /* An INSERT statement. If the conflict-mode is REPLACE, first remove
253346 ** the current entry (if any). */
253347 if( eConflict==SQLITE_REPLACE5 && eType1==SQLITE_INTEGER1 ){
253348 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
253349 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0, 0);
253350 bUpdateOrDelete = 1;
253351 }
253352 fts5StorageInsert(&rc, pTab, apVal, pRowid);
253353 }
253354
253355 /* UPDATE */
253356 else{
253357 Fts5Storage *pStorage = pTab->pStorage;
253358 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
253359 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
253360 int bContent = 0; /* Content only update */
253361
253362 /* If this is a contentless table (including contentless_unindexed=1
253363 ** tables), check if the UPDATE may proceed. */
253364 if( fts5IsContentless(pTab, 1) ){
253365 rc = fts5ContentlessUpdate(pConfig, &apVal[2], iOld!=iNew, &bContent);
253366 if( rc!=SQLITE_OK0 ) goto update_out;
253367 }
253368
253369 if( eType1!=SQLITE_INTEGER1 ){
253370 rc = SQLITE_MISMATCH20;
253371 }else if( iOld!=iNew ){
253372 assert( bContent==0 )((void) (0));
253373 if( eConflict==SQLITE_REPLACE5 ){
253374 rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 1);
253375 if( rc==SQLITE_OK0 ){
253376 rc = sqlite3Fts5StorageDelete(pStorage, iNew, 0, 0);
253377 }
253378 fts5StorageInsert(&rc, pTab, apVal, pRowid);
253379 }else{
253380 rc = sqlite3Fts5StorageFindDeleteRow(pStorage, iOld);
253381 if( rc==SQLITE_OK0 ){
253382 rc = sqlite3Fts5StorageContentInsert(pStorage, 0, apVal, pRowid);
253383 }
253384 if( rc==SQLITE_OK0 ){
253385 rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 0);
253386 }
253387 if( rc==SQLITE_OK0 ){
253388 rc = sqlite3Fts5StorageIndexInsert(pStorage, apVal, *pRowid);
253389 }
253390 }
253391 }else if( bContent ){
253392 /* This occurs when an UPDATE on a contentless table affects *only*
253393 ** UNINDEXED columns. This is a no-op for contentless_unindexed=0
253394 ** tables, or a write to the %_content table only for =1 tables. */
253395 assert( fts5IsContentless(pTab, 1) )((void) (0));
253396 rc = sqlite3Fts5StorageFindDeleteRow(pStorage, iOld);
253397 if( rc==SQLITE_OK0 ){
253398 rc = sqlite3Fts5StorageContentInsert(pStorage, 1, apVal, pRowid);
253399 }
253400 }else{
253401 rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 1);
253402 fts5StorageInsert(&rc, pTab, apVal, pRowid);
253403 }
253404 bUpdateOrDelete = 1;
253405 sqlite3Fts5StorageReleaseDeleteRow(pStorage);
253406 }
253407
253408 }
253409 }
253410
253411 if( rc==SQLITE_OK0
253412 && bUpdateOrDelete
253413 && pConfig->bSecureDelete
253414 && pConfig->iVersion==FTS5_CURRENT_VERSION4
253415 ){
253416 rc = sqlite3Fts5StorageConfigValue(
253417 pTab->pStorage, "version", 0, FTS5_CURRENT_VERSION_SECUREDELETE5
253418 );
253419 if( rc==SQLITE_OK0 ){
253420 pConfig->iVersion = FTS5_CURRENT_VERSION_SECUREDELETE5;
253421 }
253422 }
253423
253424 update_out:
253425 pTab->p.pConfig->pzErrmsg = 0;
253426 return rc;
253427}
253428
253429/*
253430** Implementation of xSync() method.
253431*/
253432static int fts5SyncMethod(sqlite3_vtab *pVtab){
253433 int rc;
253434 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
253435 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
253436 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
253437 rc = sqlite3Fts5FlushToDisk(&pTab->p);
253438 pTab->p.pConfig->pzErrmsg = 0;
253439 return rc;
253440}
253441
253442/*
253443** Implementation of xBegin() method.
253444*/
253445static int fts5BeginMethod(sqlite3_vtab *pVtab){
253446 int rc = fts5NewTransaction((Fts5FullTable*)pVtab);
253447 if( rc==SQLITE_OK0 ){
253448 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
253449 }
253450 return rc;
253451}
253452
253453/*
253454** Implementation of xCommit() method. This is a no-op. The contents of
253455** the pending-terms hash-table have already been flushed into the database
253456** by fts5SyncMethod().
253457*/
253458static int fts5CommitMethod(sqlite3_vtab *pVtab){
253459 UNUSED_PARAM(pVtab)(void)(pVtab); /* Call below is a no-op for NDEBUG builds */
253460 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
253461 return SQLITE_OK0;
253462}
253463
253464/*
253465** Implementation of xRollback(). Discard the contents of the pending-terms
253466** hash-table. Any changes made to the database are reverted by SQLite.
253467*/
253468static int fts5RollbackMethod(sqlite3_vtab *pVtab){
253469 int rc;
253470 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
253471 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
253472 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
253473 return rc;
253474}
253475
253476static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
253477
253478static void *fts5ApiUserData(Fts5Context *pCtx){
253479 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253480 return pCsr->pAux->pUserData;
253481}
253482
253483static int fts5ApiColumnCount(Fts5Context *pCtx){
253484 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253485 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
253486}
253487
253488static int fts5ApiColumnTotalSize(
253489 Fts5Context *pCtx,
253490 int iCol,
253491 sqlite3_int64 *pnToken
253492){
253493 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253494 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
253495 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
253496}
253497
253498static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
253499 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253500 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
253501 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
253502}
253503
253504/*
253505** Implementation of xTokenize_v2() API.
253506*/
253507static int fts5ApiTokenize_v2(
253508 Fts5Context *pCtx,
253509 const char *pText, int nText,
253510 const char *pLoc, int nLoc,
253511 void *pUserData,
253512 int (*xToken)(void*, int, const char*, int, int, int)
253513){
253514 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253515 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
253516 int rc = SQLITE_OK0;
253517
253518 sqlite3Fts5SetLocale(pTab->pConfig, pLoc, nLoc);
253519 rc = sqlite3Fts5Tokenize(pTab->pConfig,
253520 FTS5_TOKENIZE_AUX0x0008, pText, nText, pUserData, xToken
253521 );
253522 sqlite3Fts5SetLocale(pTab->pConfig, 0, 0);
253523
253524 return rc;
253525}
253526
253527/*
253528** Implementation of xTokenize() API. This is just xTokenize_v2() with NULL/0
253529** passed as the locale.
253530*/
253531static int fts5ApiTokenize(
253532 Fts5Context *pCtx,
253533 const char *pText, int nText,
253534 void *pUserData,
253535 int (*xToken)(void*, int, const char*, int, int, int)
253536){
253537 return fts5ApiTokenize_v2(pCtx, pText, nText, 0, 0, pUserData, xToken);
253538}
253539
253540static int fts5ApiPhraseCount(Fts5Context *pCtx){
253541 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253542 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
253543}
253544
253545static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
253546 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253547 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
253548}
253549
253550/*
253551** Argument pStmt is an SQL statement of the type used by Fts5Cursor. This
253552** function extracts the text value of column iCol of the current row.
253553** Additionally, if there is an associated locale, it invokes
253554** sqlite3Fts5SetLocale() to configure the tokenizer. In all cases the caller
253555** should invoke sqlite3Fts5ClearLocale() to clear the locale at some point
253556** after this function returns.
253557**
253558** If successful, (*ppText) is set to point to a buffer containing the text
253559** value as utf-8 and SQLITE_OK returned. (*pnText) is set to the size of that
253560** buffer in bytes. It is not guaranteed to be nul-terminated. If an error
253561** occurs, an SQLite error code is returned. The final values of the two
253562** output parameters are undefined in this case.
253563*/
253564static int fts5TextFromStmt(
253565 Fts5Config *pConfig,
253566 sqlite3_stmt *pStmt,
253567 int iCol,
253568 const char **ppText,
253569 int *pnText
253570){
253571 sqlite3_value *pVal = sqlite3_column_value(pStmt, iCol+1);
253572 const char *pLoc = 0;
253573 int nLoc = 0;
253574 int rc = SQLITE_OK0;
253575
253576 if( pConfig->bLocale
253577 && pConfig->eContent==FTS5_CONTENT_EXTERNAL2
253578 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
253579 ){
253580 rc = sqlite3Fts5DecodeLocaleValue(pVal, ppText, pnText, &pLoc, &nLoc);
253581 }else{
253582 *ppText = (const char*)sqlite3_value_text(pVal);
253583 *pnText = sqlite3_value_bytes(pVal);
253584 if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
253585 pLoc = (const char*)sqlite3_column_text(pStmt, iCol+1+pConfig->nCol);
253586 nLoc = sqlite3_column_bytes(pStmt, iCol+1+pConfig->nCol);
253587 }
253588 }
253589 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
253590 return rc;
253591}
253592
253593static int fts5ApiColumnText(
253594 Fts5Context *pCtx,
253595 int iCol,
253596 const char **pz,
253597 int *pn
253598){
253599 int rc = SQLITE_OK0;
253600 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253601 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
253602
253603 assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL )((void) (0));
253604 if( iCol<0 || iCol>=pTab->pConfig->nCol ){
253605 rc = SQLITE_RANGE25;
253606 }else if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab), 0) ){
253607 *pz = 0;
253608 *pn = 0;
253609 }else{
253610 rc = fts5SeekCursor(pCsr, 0);
253611 if( rc==SQLITE_OK0 ){
253612 rc = fts5TextFromStmt(pTab->pConfig, pCsr->pStmt, iCol, pz, pn);
253613 sqlite3Fts5ClearLocale(pTab->pConfig);
253614 }
253615 }
253616 return rc;
253617}
253618
253619/*
253620** This is called by various API functions - xInst, xPhraseFirst,
253621** xPhraseFirstColumn etc. - to obtain the position list for phrase iPhrase
253622** of the current row. This function works for both detail=full tables (in
253623** which case the position-list was read from the fts index) or for other
253624** detail= modes if the row content is available.
253625*/
253626static int fts5CsrPoslist(
253627 Fts5Cursor *pCsr, /* Fts5 cursor object */
253628 int iPhrase, /* Phrase to find position list for */
253629 const u8 **pa, /* OUT: Pointer to position list buffer */
253630 int *pn /* OUT: Size of (*pa) in bytes */
253631){
253632 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
253633 int rc = SQLITE_OK0;
253634 int bLive = (pCsr->pSorter==0);
253635
253636 if( iPhrase<0 || iPhrase>=sqlite3Fts5ExprPhraseCount(pCsr->pExpr) ){
253637 rc = SQLITE_RANGE25;
253638 }else if( pConfig->eDetail!=FTS5_DETAIL_FULL0
253639 && fts5IsContentless((Fts5FullTable*)pCsr->base.pVtab, 1)
253640 ){
253641 *pa = 0;
253642 *pn = 0;
253643 return SQLITE_OK0;
253644 }else if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST)((pCsr)->csrflags & (0x40)) ){
253645 if( pConfig->eDetail!=FTS5_DETAIL_FULL0 ){
253646 Fts5PoslistPopulator *aPopulator;
253647 int i;
253648
253649 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
253650 if( aPopulator==0 ) rc = SQLITE_NOMEM7;
253651 if( rc==SQLITE_OK0 ){
253652 rc = fts5SeekCursor(pCsr, 0);
253653 }
253654 for(i=0; i<pConfig->nCol && rc==SQLITE_OK0; i++){
253655 const char *z = 0;
253656 int n = 0;
253657 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
253658 if( rc==SQLITE_OK0 ){
253659 rc = sqlite3Fts5ExprPopulatePoslists(
253660 pConfig, pCsr->pExpr, aPopulator, i, z, n
253661 );
253662 }
253663 sqlite3Fts5ClearLocale(pConfig);
253664 }
253665 sqlite3_free(aPopulator);
253666
253667 if( pCsr->pSorter ){
253668 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
253669 }
253670 }
253671 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST)((pCsr)->csrflags &= ~(0x40));
253672 }
253673
253674 if( rc==SQLITE_OK0 ){
253675 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL0 ){
253676 Fts5Sorter *pSorter = pCsr->pSorter;
253677 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
253678 *pn = pSorter->aIdx[iPhrase] - i1;
253679 *pa = &pSorter->aPoslist[i1];
253680 }else{
253681 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
253682 }
253683 }else{
253684 *pa = 0;
253685 *pn = 0;
253686 }
253687
253688 return rc;
253689}
253690
253691/*
253692** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
253693** correctly for the current view. Return SQLITE_OK if successful, or an
253694** SQLite error code otherwise.
253695*/
253696static int fts5CacheInstArray(Fts5Cursor *pCsr){
253697 int rc = SQLITE_OK0;
253698 Fts5PoslistReader *aIter; /* One iterator for each phrase */
253699 int nIter; /* Number of iterators/phrases */
253700 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
253701
253702 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
253703 if( pCsr->aInstIter==0 ){
253704 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
253705 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
253706 }
253707 aIter = pCsr->aInstIter;
253708
253709 if( aIter ){
253710 int nInst = 0; /* Number instances seen so far */
253711 int i;
253712
253713 /* Initialize all iterators */
253714 for(i=0; i<nIter && rc==SQLITE_OK0; i++){
253715 const u8 *a;
253716 int n;
253717 rc = fts5CsrPoslist(pCsr, i, &a, &n);
253718 if( rc==SQLITE_OK0 ){
253719 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
253720 }
253721 }
253722
253723 if( rc==SQLITE_OK0 ){
253724 while( 1 ){
253725 int *aInst;
253726 int iBest = -1;
253727 for(i=0; i<nIter; i++){
253728 if( (aIter[i].bEof==0)
253729 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
253730 ){
253731 iBest = i;
253732 }
253733 }
253734 if( iBest<0 ) break;
253735
253736 nInst++;
253737 if( nInst>=pCsr->nInstAlloc ){
253738 int nNewSize = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
253739 aInst = (int*)sqlite3_realloc64(
253740 pCsr->aInst, nNewSize*sizeof(int)*3
253741 );
253742 if( aInst ){
253743 pCsr->aInst = aInst;
253744 pCsr->nInstAlloc = nNewSize;
253745 }else{
253746 nInst--;
253747 rc = SQLITE_NOMEM7;
253748 break;
253749 }
253750 }
253751
253752 aInst = &pCsr->aInst[3 * (nInst-1)];
253753 aInst[0] = iBest;
253754 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos)(int)((aIter[iBest].iPos >> 32) & 0x7FFFFFFF);
253755 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos)(int)(aIter[iBest].iPos & 0x7FFFFFFF);
253756 assert( aInst[1]>=0 )((void) (0));
253757 if( aInst[1]>=nCol ){
253758 rc = FTS5_CORRUPT(11 | (1<<8));
253759 break;
253760 }
253761 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
253762 }
253763 }
253764
253765 pCsr->nInstCount = nInst;
253766 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags &= ~(0x08));
253767 }
253768 return rc;
253769}
253770
253771static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
253772 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253773 int rc = SQLITE_OK0;
253774 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
253775 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr)) ){
253776 *pnInst = pCsr->nInstCount;
253777 }
253778 return rc;
253779}
253780
253781static int fts5ApiInst(
253782 Fts5Context *pCtx,
253783 int iIdx,
253784 int *piPhrase,
253785 int *piCol,
253786 int *piOff
253787){
253788 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253789 int rc = SQLITE_OK0;
253790 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
253791 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr))
253792 ){
253793 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
253794 rc = SQLITE_RANGE25;
253795 }else{
253796 *piPhrase = pCsr->aInst[iIdx*3];
253797 *piCol = pCsr->aInst[iIdx*3 + 1];
253798 *piOff = pCsr->aInst[iIdx*3 + 2];
253799 }
253800 }
253801 return rc;
253802}
253803
253804static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
253805 return fts5CursorRowid((Fts5Cursor*)pCtx);
253806}
253807
253808static int fts5ColumnSizeCb(
253809 void *pContext, /* Pointer to int */
253810 int tflags,
253811 const char *pUnused, /* Buffer containing token */
253812 int nUnused, /* Size of token in bytes */
253813 int iUnused1, /* Start offset of token */
253814 int iUnused2 /* End offset of token */
253815){
253816 int *pCnt = (int*)pContext;
253817 UNUSED_PARAM2(pUnused, nUnused)(void)(pUnused), (void)(nUnused);
253818 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
253819 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
253820 (*pCnt)++;
253821 }
253822 return SQLITE_OK0;
253823}
253824
253825static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
253826 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253827 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
253828 Fts5Config *pConfig = pTab->p.pConfig;
253829 int rc = SQLITE_OK0;
253830
253831 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags & (0x04)) ){
253832 if( pConfig->bColumnsize ){
253833 i64 iRowid = fts5CursorRowid(pCsr);
253834 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
253835 }else if( !pConfig->zContent || pConfig->eContent==FTS5_CONTENT_UNINDEXED3 ){
253836 int i;
253837 for(i=0; i<pConfig->nCol; i++){
253838 if( pConfig->abUnindexed[i]==0 ){
253839 pCsr->aColumnSize[i] = -1;
253840 }
253841 }
253842 }else{
253843 int i;
253844 rc = fts5SeekCursor(pCsr, 0);
253845 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
253846 if( pConfig->abUnindexed[i]==0 ){
253847 const char *z = 0;
253848 int n = 0;
253849 pCsr->aColumnSize[i] = 0;
253850 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
253851 if( rc==SQLITE_OK0 ){
253852 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_AUX0x0008,
253853 z, n, (void*)&pCsr->aColumnSize[i], fts5ColumnSizeCb
253854 );
253855 }
253856 sqlite3Fts5ClearLocale(pConfig);
253857 }
253858 }
253859 }
253860 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE)((pCsr)->csrflags &= ~(0x04));
253861 }
253862 if( iCol<0 ){
253863 int i;
253864 *pnToken = 0;
253865 for(i=0; i<pConfig->nCol; i++){
253866 *pnToken += pCsr->aColumnSize[i];
253867 }
253868 }else if( iCol<pConfig->nCol ){
253869 *pnToken = pCsr->aColumnSize[iCol];
253870 }else{
253871 *pnToken = 0;
253872 rc = SQLITE_RANGE25;
253873 }
253874 return rc;
253875}
253876
253877/*
253878** Implementation of the xSetAuxdata() method.
253879*/
253880static int fts5ApiSetAuxdata(
253881 Fts5Context *pCtx, /* Fts5 context */
253882 void *pPtr, /* Pointer to save as auxdata */
253883 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
253884){
253885 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253886 Fts5Auxdata *pData;
253887
253888 /* Search through the cursors list of Fts5Auxdata objects for one that
253889 ** corresponds to the currently executing auxiliary function. */
253890 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
253891 if( pData->pAux==pCsr->pAux ) break;
253892 }
253893
253894 if( pData ){
253895 if( pData->xDelete ){
253896 pData->xDelete(pData->pPtr);
253897 }
253898 }else{
253899 int rc = SQLITE_OK0;
253900 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
253901 if( pData==0 ){
253902 if( xDelete ) xDelete(pPtr);
253903 return rc;
253904 }
253905 pData->pAux = pCsr->pAux;
253906 pData->pNext = pCsr->pAuxdata;
253907 pCsr->pAuxdata = pData;
253908 }
253909
253910 pData->xDelete = xDelete;
253911 pData->pPtr = pPtr;
253912 return SQLITE_OK0;
253913}
253914
253915static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
253916 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253917 Fts5Auxdata *pData;
253918 void *pRet = 0;
253919
253920 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
253921 if( pData->pAux==pCsr->pAux ) break;
253922 }
253923
253924 if( pData ){
253925 pRet = pData->pPtr;
253926 if( bClear ){
253927 pData->pPtr = 0;
253928 pData->xDelete = 0;
253929 }
253930 }
253931
253932 return pRet;
253933}
253934
253935static void fts5ApiPhraseNext(
253936 Fts5Context *pCtx,
253937 Fts5PhraseIter *pIter,
253938 int *piCol, int *piOff
253939){
253940 if( pIter->a>=pIter->b ){
253941 *piCol = -1;
253942 *piOff = -1;
253943 }else{
253944 int iVal;
253945 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(iVal));
253946 if( iVal==1 ){
253947 /* Avoid returning a (*piCol) value that is too large for the table,
253948 ** even if the position-list is corrupt. The caller might not be
253949 ** expecting it. */
253950 int nCol = ((Fts5Table*)(((Fts5Cursor*)pCtx)->base.pVtab))->pConfig->nCol;
253951 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(iVal));
253952 *piCol = (iVal>=nCol ? nCol-1 : iVal);
253953 *piOff = 0;
253954 pIter->a += fts5GetVarint32(pIter->a, iVal)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(iVal));
253955 }
253956 *piOff += (iVal-2);
253957 }
253958}
253959
253960static int fts5ApiPhraseFirst(
253961 Fts5Context *pCtx,
253962 int iPhrase,
253963 Fts5PhraseIter *pIter,
253964 int *piCol, int *piOff
253965){
253966 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253967 int n;
253968 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
253969 if( rc==SQLITE_OK0 ){
253970 assert( pIter->a || n==0 )((void) (0));
253971 pIter->b = (pIter->a ? &pIter->a[n] : 0);
253972 *piCol = 0;
253973 *piOff = 0;
253974 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
253975 }
253976 return rc;
253977}
253978
253979static void fts5ApiPhraseNextColumn(
253980 Fts5Context *pCtx,
253981 Fts5PhraseIter *pIter,
253982 int *piCol
253983){
253984 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
253985 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
253986
253987 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
253988 if( pIter->a>=pIter->b ){
253989 *piCol = -1;
253990 }else{
253991 int iIncr;
253992 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr)sqlite3Fts5GetVarint32(&pIter->a[0],(u32*)&(iIncr)
)
;
253993 *piCol += (iIncr-2);
253994 }
253995 }else{
253996 while( 1 ){
253997 int dummy;
253998 if( pIter->a>=pIter->b ){
253999 *piCol = -1;
254000 return;
254001 }
254002 if( pIter->a[0]==0x01 ) break;
254003 pIter->a += fts5GetVarint32(pIter->a, dummy)sqlite3Fts5GetVarint32(pIter->a,(u32*)&(dummy));
254004 }
254005 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol)sqlite3Fts5GetVarint32(&pIter->a[1],(u32*)&(*piCol
))
;
254006 }
254007}
254008
254009static int fts5ApiPhraseFirstColumn(
254010 Fts5Context *pCtx,
254011 int iPhrase,
254012 Fts5PhraseIter *pIter,
254013 int *piCol
254014){
254015 int rc = SQLITE_OK0;
254016 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
254017 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
254018
254019 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
254020 Fts5Sorter *pSorter = pCsr->pSorter;
254021 int n;
254022 if( pSorter ){
254023 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
254024 n = pSorter->aIdx[iPhrase] - i1;
254025 pIter->a = &pSorter->aPoslist[i1];
254026 }else{
254027 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
254028 }
254029 if( rc==SQLITE_OK0 ){
254030 assert( pIter->a || n==0 )((void) (0));
254031 pIter->b = (pIter->a ? &pIter->a[n] : 0);
254032 *piCol = 0;
254033 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
254034 }
254035 }else{
254036 int n;
254037 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
254038 if( rc==SQLITE_OK0 ){
254039 assert( pIter->a || n==0 )((void) (0));
254040 pIter->b = (pIter->a ? &pIter->a[n] : 0);
254041 if( n<=0 ){
254042 *piCol = -1;
254043 }else if( pIter->a[0]==0x01 ){
254044 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol)sqlite3Fts5GetVarint32(&pIter->a[1],(u32*)&(*piCol
))
;
254045 }else{
254046 *piCol = 0;
254047 }
254048 }
254049 }
254050
254051 return rc;
254052}
254053
254054/*
254055** xQueryToken() API implemenetation.
254056*/
254057static int fts5ApiQueryToken(
254058 Fts5Context* pCtx,
254059 int iPhrase,
254060 int iToken,
254061 const char **ppOut,
254062 int *pnOut
254063){
254064 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
254065 return sqlite3Fts5ExprQueryToken(pCsr->pExpr, iPhrase, iToken, ppOut, pnOut);
254066}
254067
254068/*
254069** xInstToken() API implemenetation.
254070*/
254071static int fts5ApiInstToken(
254072 Fts5Context *pCtx,
254073 int iIdx,
254074 int iToken,
254075 const char **ppOut, int *pnOut
254076){
254077 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
254078 int rc = SQLITE_OK0;
254079 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)((pCsr)->csrflags & (0x08))==0
254080 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr))
254081 ){
254082 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
254083 rc = SQLITE_RANGE25;
254084 }else{
254085 int iPhrase = pCsr->aInst[iIdx*3];
254086 int iCol = pCsr->aInst[iIdx*3 + 1];
254087 int iOff = pCsr->aInst[iIdx*3 + 2];
254088 i64 iRowid = fts5CursorRowid(pCsr);
254089 rc = sqlite3Fts5ExprInstToken(
254090 pCsr->pExpr, iRowid, iPhrase, iCol, iOff, iToken, ppOut, pnOut
254091 );
254092 }
254093 }
254094 return rc;
254095}
254096
254097
254098static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
254099 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
254100);
254101
254102/*
254103** The xColumnLocale() API.
254104*/
254105static int fts5ApiColumnLocale(
254106 Fts5Context *pCtx,
254107 int iCol,
254108 const char **pzLocale,
254109 int *pnLocale
254110){
254111 int rc = SQLITE_OK0;
254112 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
254113 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
254114
254115 *pzLocale = 0;
254116 *pnLocale = 0;
254117
254118 assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL )((void) (0));
254119 if( iCol<0 || iCol>=pConfig->nCol ){
254120 rc = SQLITE_RANGE25;
254121 }else if(
254122 pConfig->abUnindexed[iCol]==0
254123 && 0==fts5IsContentless((Fts5FullTable*)pCsr->base.pVtab, 1)
254124 && pConfig->bLocale
254125 ){
254126 rc = fts5SeekCursor(pCsr, 0);
254127 if( rc==SQLITE_OK0 ){
254128 const char *zDummy = 0;
254129 int nDummy = 0;
254130 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, iCol, &zDummy, &nDummy);
254131 if( rc==SQLITE_OK0 ){
254132 *pzLocale = pConfig->t.pLocale;
254133 *pnLocale = pConfig->t.nLocale;
254134 }
254135 sqlite3Fts5ClearLocale(pConfig);
254136 }
254137 }
254138
254139 return rc;
254140}
254141
254142static const Fts5ExtensionApi sFts5Api = {
254143 4, /* iVersion */
254144 fts5ApiUserData,
254145 fts5ApiColumnCount,
254146 fts5ApiRowCount,
254147 fts5ApiColumnTotalSize,
254148 fts5ApiTokenize,
254149 fts5ApiPhraseCount,
254150 fts5ApiPhraseSize,
254151 fts5ApiInstCount,
254152 fts5ApiInst,
254153 fts5ApiRowid,
254154 fts5ApiColumnText,
254155 fts5ApiColumnSize,
254156 fts5ApiQueryPhrase,
254157 fts5ApiSetAuxdata,
254158 fts5ApiGetAuxdata,
254159 fts5ApiPhraseFirst,
254160 fts5ApiPhraseNext,
254161 fts5ApiPhraseFirstColumn,
254162 fts5ApiPhraseNextColumn,
254163 fts5ApiQueryToken,
254164 fts5ApiInstToken,
254165 fts5ApiColumnLocale,
254166 fts5ApiTokenize_v2
254167};
254168
254169/*
254170** Implementation of API function xQueryPhrase().
254171*/
254172static int fts5ApiQueryPhrase(
254173 Fts5Context *pCtx,
254174 int iPhrase,
254175 void *pUserData,
254176 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
254177){
254178 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
254179 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
254180 int rc;
254181 Fts5Cursor *pNew = 0;
254182
254183 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
254184 if( rc==SQLITE_OK0 ){
254185 pNew->ePlan = FTS5_PLAN_MATCH1;
254186 pNew->iFirstRowid = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
254187 pNew->iLastRowid = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
254188 pNew->base.pVtab = (sqlite3_vtab*)pTab;
254189 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
254190 }
254191
254192 if( rc==SQLITE_OK0 ){
254193 for(rc = fts5CursorFirst(pTab, pNew, 0);
254194 rc==SQLITE_OK0 && CsrFlagTest(pNew, FTS5CSR_EOF)((pNew)->csrflags & (0x01))==0;
254195 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
254196 ){
254197 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
254198 if( rc!=SQLITE_OK0 ){
254199 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
254200 break;
254201 }
254202 }
254203 }
254204
254205 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
254206 return rc;
254207}
254208
254209static void fts5ApiInvoke(
254210 Fts5Auxiliary *pAux,
254211 Fts5Cursor *pCsr,
254212 sqlite3_context *context,
254213 int argc,
254214 sqlite3_value **argv
254215){
254216 assert( pCsr->pAux==0 )((void) (0));
254217 assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL )((void) (0));
254218 pCsr->pAux = pAux;
254219 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
254220 pCsr->pAux = 0;
254221}
254222
254223static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
254224 Fts5Cursor *pCsr;
254225 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
254226 if( pCsr->iCsrId==iCsrId ) break;
254227 }
254228 return pCsr;
254229}
254230
254231/*
254232** Parameter zFmt is a printf() style formatting string. This function
254233** formats it using the trailing arguments and returns the result as
254234** an error message to the context passed as the first argument.
254235*/
254236static void fts5ResultError(sqlite3_context *pCtx, const char *zFmt, ...){
254237 char *zErr = 0;
254238 va_list ap;
254239 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
254240 zErr = sqlite3_vmprintf(zFmt, ap);
254241 sqlite3_result_error(pCtx, zErr, -1);
254242 sqlite3_free(zErr);
254243 va_end(ap)__builtin_va_end(ap);
254244}
254245
254246static void fts5ApiCallback(
254247 sqlite3_context *context,
254248 int argc,
254249 sqlite3_value **argv
254250){
254251
254252 Fts5Auxiliary *pAux;
254253 Fts5Cursor *pCsr;
254254 i64 iCsrId;
254255
254256 assert( argc>=1 )((void) (0));
254257 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
254258 iCsrId = sqlite3_value_int64(argv[0]);
254259
254260 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
254261 if( pCsr==0 || (pCsr->ePlan==0 || pCsr->ePlan==FTS5_PLAN_SPECIAL3) ){
254262 fts5ResultError(context, "no such cursor: %lld", iCsrId);
254263 }else{
254264 sqlite3_vtab *pTab = pCsr->base.pVtab;
254265 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
254266 sqlite3_free(pTab->zErrMsg);
254267 pTab->zErrMsg = 0;
254268 }
254269}
254270
254271
254272/*
254273** Given cursor id iId, return a pointer to the corresponding Fts5Table
254274** object. Or NULL If the cursor id does not exist.
254275*/
254276static Fts5Table *sqlite3Fts5TableFromCsrid(
254277 Fts5Global *pGlobal, /* FTS5 global context for db handle */
254278 i64 iCsrId /* Id of cursor to find */
254279){
254280 Fts5Cursor *pCsr;
254281 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
254282 if( pCsr ){
254283 return (Fts5Table*)pCsr->base.pVtab;
254284 }
254285 return 0;
254286}
254287
254288/*
254289** Return a "position-list blob" corresponding to the current position of
254290** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
254291** the current position-list for each phrase in the query associated with
254292** cursor pCsr.
254293**
254294** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
254295** the number of phrases in the query. Following the varints are the
254296** concatenated position lists for each phrase, in order.
254297**
254298** The first varint (if it exists) contains the size of the position list
254299** for phrase 0. The second (same disclaimer) contains the size of position
254300** list 1. And so on. There is no size field for the final position list,
254301** as it can be derived from the total size of the blob.
254302*/
254303static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
254304 int i;
254305 int rc = SQLITE_OK0;
254306 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
254307 Fts5Buffer val;
254308
254309 memset(&val, 0, sizeof(Fts5Buffer));
254310 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
254311 case FTS5_DETAIL_FULL0:
254312
254313 /* Append the varints */
254314 for(i=0; i<(nPhrase-1); i++){
254315 const u8 *dummy;
254316 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
254317 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
254318 }
254319
254320 /* Append the position lists */
254321 for(i=0; i<nPhrase; i++){
254322 const u8 *pPoslist;
254323 int nPoslist;
254324 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
254325 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
254326 }
254327 break;
254328
254329 case FTS5_DETAIL_COLUMNS2:
254330
254331 /* Append the varints */
254332 for(i=0; rc==SQLITE_OK0 && i<(nPhrase-1); i++){
254333 const u8 *dummy;
254334 int nByte;
254335 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
254336 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
254337 }
254338
254339 /* Append the position lists */
254340 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
254341 const u8 *pPoslist;
254342 int nPoslist;
254343 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
254344 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
254345 }
254346 break;
254347
254348 default:
254349 break;
254350 }
254351
254352 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
254353 return rc;
254354}
254355
254356/*
254357** This is the xColumn method, called by SQLite to request a value from
254358** the row that the supplied cursor currently points to.
254359*/
254360static int fts5ColumnMethod(
254361 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
254362 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
254363 int iCol /* Index of column to read value from */
254364){
254365 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
254366 Fts5Config *pConfig = pTab->p.pConfig;
254367 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
254368 int rc = SQLITE_OK0;
254369
254370 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
254371
254372 if( pCsr->ePlan==FTS5_PLAN_SPECIAL3 ){
254373 if( iCol==pConfig->nCol ){
254374 sqlite3_result_int64(pCtx, pCsr->iSpecial);
254375 }
254376 }else
254377
254378 if( iCol==pConfig->nCol ){
254379 /* User is requesting the value of the special column with the same name
254380 ** as the table. Return the cursor integer id number. This value is only
254381 ** useful in that it may be passed as the first argument to an FTS5
254382 ** auxiliary function. */
254383 sqlite3_result_int64(pCtx, pCsr->iCsrId);
254384 }else if( iCol==pConfig->nCol+1 ){
254385 /* The value of the "rank" column. */
254386
254387 if( pCsr->ePlan==FTS5_PLAN_SOURCE2 ){
254388 fts5PoslistBlob(pCtx, pCsr);
254389 }else if(
254390 pCsr->ePlan==FTS5_PLAN_MATCH1
254391 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH4
254392 ){
254393 if( pCsr->pRank || SQLITE_OK0==(rc = fts5FindRankFunction(pCsr)) ){
254394 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
254395 }
254396 }
254397 }else{
254398 if( !sqlite3_vtab_nochange(pCtx) && pConfig->eContent!=FTS5_CONTENT_NONE1 ){
254399 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
254400 rc = fts5SeekCursor(pCsr, 1);
254401 if( rc==SQLITE_OK0 ){
254402 sqlite3_value *pVal = sqlite3_column_value(pCsr->pStmt, iCol+1);
254403 if( pConfig->bLocale
254404 && pConfig->eContent==FTS5_CONTENT_EXTERNAL2
254405 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
254406 ){
254407 const char *z = 0;
254408 int n = 0;
254409 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, iCol, &z, &n);
254410 if( rc==SQLITE_OK0 ){
254411 sqlite3_result_text(pCtx, z, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
254412 }
254413 sqlite3Fts5ClearLocale(pConfig);
254414 }else{
254415 sqlite3_result_value(pCtx, pVal);
254416 }
254417 }
254418
254419 pConfig->pzErrmsg = 0;
254420 }
254421 }
254422
254423 return rc;
254424}
254425
254426
254427/*
254428** This routine implements the xFindFunction method for the FTS3
254429** virtual table.
254430*/
254431static int fts5FindFunctionMethod(
254432 sqlite3_vtab *pVtab, /* Virtual table handle */
254433 int nUnused, /* Number of SQL function arguments */
254434 const char *zName, /* Name of SQL function */
254435 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
254436 void **ppArg /* OUT: User data for *pxFunc */
254437){
254438 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
254439 Fts5Auxiliary *pAux;
254440
254441 UNUSED_PARAM(nUnused)(void)(nUnused);
254442 pAux = fts5FindAuxiliary(pTab, zName);
254443 if( pAux ){
254444 *pxFunc = fts5ApiCallback;
254445 *ppArg = (void*)pAux;
254446 return 1;
254447 }
254448
254449 /* No function of the specified name was found. Return 0. */
254450 return 0;
254451}
254452
254453/*
254454** Implementation of FTS5 xRename method. Rename an fts5 table.
254455*/
254456static int fts5RenameMethod(
254457 sqlite3_vtab *pVtab, /* Virtual table handle */
254458 const char *zName /* New name of table */
254459){
254460 int rc;
254461 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
254462 rc = sqlite3Fts5StorageRename(pTab->pStorage, zName);
254463 return rc;
254464}
254465
254466static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
254467 fts5TripCursors((Fts5FullTable*)pTab);
254468 return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
254469}
254470
254471/*
254472** The xSavepoint() method.
254473**
254474** Flush the contents of the pending-terms table to disk.
254475*/
254476static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
254477 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
254478 int rc = SQLITE_OK0;
254479
254480 fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
254481 rc = sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
254482 if( rc==SQLITE_OK0 ){
254483 pTab->iSavepoint = iSavepoint+1;
254484 }
254485 return rc;
254486}
254487
254488/*
254489** The xRelease() method.
254490**
254491** This is a no-op.
254492*/
254493static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
254494 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
254495 int rc = SQLITE_OK0;
254496 fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
254497 if( (iSavepoint+1)<pTab->iSavepoint ){
254498 rc = sqlite3Fts5FlushToDisk(&pTab->p);
254499 if( rc==SQLITE_OK0 ){
254500 pTab->iSavepoint = iSavepoint;
254501 }
254502 }
254503 return rc;
254504}
254505
254506/*
254507** The xRollbackTo() method.
254508**
254509** Discard the contents of the pending terms table.
254510*/
254511static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
254512 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
254513 int rc = SQLITE_OK0;
254514 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
254515 fts5TripCursors(pTab);
254516 if( (iSavepoint+1)<=pTab->iSavepoint ){
254517 pTab->p.pConfig->pgsz = 0;
254518 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
254519 }
254520 return rc;
254521}
254522
254523/*
254524** Register a new auxiliary function with global context pGlobal.
254525*/
254526static int fts5CreateAux(
254527 fts5_api *pApi, /* Global context (one per db handle) */
254528 const char *zName, /* Name of new function */
254529 void *pUserData, /* User data for aux. function */
254530 fts5_extension_function xFunc, /* Aux. function implementation */
254531 void(*xDestroy)(void*) /* Destructor for pUserData */
254532){
254533 Fts5Global *pGlobal = (Fts5Global*)pApi;
254534 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
254535 if( rc==SQLITE_OK0 ){
254536 Fts5Auxiliary *pAux;
254537 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
254538 sqlite3_int64 nByte; /* Bytes of space to allocate */
254539
254540 nName = strlen(zName) + 1;
254541 nByte = sizeof(Fts5Auxiliary) + nName;
254542 pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
254543 if( pAux ){
254544 memset(pAux, 0, (size_t)nByte);
254545 pAux->zFunc = (char*)&pAux[1];
254546 memcpy(pAux->zFunc, zName, nName);
254547 pAux->pGlobal = pGlobal;
254548 pAux->pUserData = pUserData;
254549 pAux->xFunc = xFunc;
254550 pAux->xDestroy = xDestroy;
254551 pAux->pNext = pGlobal->pAux;
254552 pGlobal->pAux = pAux;
254553 }else{
254554 rc = SQLITE_NOMEM7;
254555 }
254556 }
254557
254558 return rc;
254559}
254560
254561/*
254562** This function is used by xCreateTokenizer_v2() and xCreateTokenizer().
254563** It allocates and partially populates a new Fts5TokenizerModule object.
254564** The new object is already linked into the Fts5Global context before
254565** returning.
254566**
254567** If successful, SQLITE_OK is returned and a pointer to the new
254568** Fts5TokenizerModule object returned via output parameter (*ppNew). All
254569** that is required is for the caller to fill in the methods in
254570** Fts5TokenizerModule.x1 and x2, and to set Fts5TokenizerModule.bV2Native
254571** as appropriate.
254572**
254573** If an error occurs, an SQLite error code is returned and the final value
254574** of (*ppNew) undefined.
254575*/
254576static int fts5NewTokenizerModule(
254577 Fts5Global *pGlobal, /* Global context (one per db handle) */
254578 const char *zName, /* Name of new function */
254579 void *pUserData, /* User data for aux. function */
254580 void(*xDestroy)(void*), /* Destructor for pUserData */
254581 Fts5TokenizerModule **ppNew
254582){
254583 int rc = SQLITE_OK0;
254584 Fts5TokenizerModule *pNew;
254585 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
254586 sqlite3_int64 nByte; /* Bytes of space to allocate */
254587
254588 nName = strlen(zName) + 1;
254589 nByte = sizeof(Fts5TokenizerModule) + nName;
254590 *ppNew = pNew = (Fts5TokenizerModule*)sqlite3Fts5MallocZero(&rc, nByte);
254591 if( pNew ){
254592 pNew->zName = (char*)&pNew[1];
254593 memcpy(pNew->zName, zName, nName);
254594 pNew->pUserData = pUserData;
254595 pNew->xDestroy = xDestroy;
254596 pNew->pNext = pGlobal->pTok;
254597 pGlobal->pTok = pNew;
254598 if( pNew->pNext==0 ){
254599 pGlobal->pDfltTok = pNew;
254600 }
254601 }
254602
254603 return rc;
254604}
254605
254606/*
254607** An instance of this type is used as the Fts5Tokenizer object for
254608** wrapper tokenizers - those that provide access to a v1 tokenizer via
254609** the fts5_tokenizer_v2 API, and those that provide access to a v2 tokenizer
254610** via the fts5_tokenizer API.
254611*/
254612typedef struct Fts5VtoVTokenizer Fts5VtoVTokenizer;
254613struct Fts5VtoVTokenizer {
254614 int bV2Native; /* True if v2 native tokenizer */
254615 fts5_tokenizer x1; /* Tokenizer functions */
254616 fts5_tokenizer_v2 x2; /* V2 tokenizer functions */
254617 Fts5Tokenizer *pReal;
254618};
254619
254620/*
254621** Create a wrapper tokenizer. The context argument pCtx points to the
254622** Fts5TokenizerModule object.
254623*/
254624static int fts5VtoVCreate(
254625 void *pCtx,
254626 const char **azArg,
254627 int nArg,
254628 Fts5Tokenizer **ppOut
254629){
254630 Fts5TokenizerModule *pMod = (Fts5TokenizerModule*)pCtx;
254631 Fts5VtoVTokenizer *pNew = 0;
254632 int rc = SQLITE_OK0;
254633
254634 pNew = (Fts5VtoVTokenizer*)sqlite3Fts5MallocZero(&rc, sizeof(*pNew));
254635 if( rc==SQLITE_OK0 ){
254636 pNew->x1 = pMod->x1;
254637 pNew->x2 = pMod->x2;
254638 pNew->bV2Native = pMod->bV2Native;
254639 if( pMod->bV2Native ){
254640 rc = pMod->x2.xCreate(pMod->pUserData, azArg, nArg, &pNew->pReal);
254641 }else{
254642 rc = pMod->x1.xCreate(pMod->pUserData, azArg, nArg, &pNew->pReal);
254643 }
254644 if( rc!=SQLITE_OK0 ){
254645 sqlite3_free(pNew);
254646 pNew = 0;
254647 }
254648 }
254649
254650 *ppOut = (Fts5Tokenizer*)pNew;
254651 return rc;
254652}
254653
254654/*
254655** Delete an Fts5VtoVTokenizer wrapper tokenizer.
254656*/
254657static void fts5VtoVDelete(Fts5Tokenizer *pTok){
254658 Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok;
254659 if( p ){
254660 if( p->bV2Native ){
254661 p->x2.xDelete(p->pReal);
254662 }else{
254663 p->x1.xDelete(p->pReal);
254664 }
254665 sqlite3_free(p);
254666 }
254667}
254668
254669
254670/*
254671** xTokenizer method for a wrapper tokenizer that offers the v1 interface
254672** (no support for locales).
254673*/
254674static int fts5V1toV2Tokenize(
254675 Fts5Tokenizer *pTok,
254676 void *pCtx, int flags,
254677 const char *pText, int nText,
254678 int (*xToken)(void*, int, const char*, int, int, int)
254679){
254680 Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok;
254681 assert( p->bV2Native )((void) (0));
254682 return p->x2.xTokenize(p->pReal, pCtx, flags, pText, nText, 0, 0, xToken);
254683}
254684
254685/*
254686** xTokenizer method for a wrapper tokenizer that offers the v2 interface
254687** (with locale support).
254688*/
254689static int fts5V2toV1Tokenize(
254690 Fts5Tokenizer *pTok,
254691 void *pCtx, int flags,
254692 const char *pText, int nText,
254693 const char *pLocale, int nLocale,
254694 int (*xToken)(void*, int, const char*, int, int, int)
254695){
254696 Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok;
254697 assert( p->bV2Native==0 )((void) (0));
254698 UNUSED_PARAM2(pLocale,nLocale)(void)(pLocale), (void)(nLocale);
254699 return p->x1.xTokenize(p->pReal, pCtx, flags, pText, nText, xToken);
254700}
254701
254702/*
254703** Register a new tokenizer. This is the implementation of the
254704** fts5_api.xCreateTokenizer_v2() method.
254705*/
254706static int fts5CreateTokenizer_v2(
254707 fts5_api *pApi, /* Global context (one per db handle) */
254708 const char *zName, /* Name of new function */
254709 void *pUserData, /* User data for aux. function */
254710 fts5_tokenizer_v2 *pTokenizer, /* Tokenizer implementation */
254711 void(*xDestroy)(void*) /* Destructor for pUserData */
254712){
254713 Fts5Global *pGlobal = (Fts5Global*)pApi;
254714 int rc = SQLITE_OK0;
254715
254716 if( pTokenizer->iVersion>2 ){
254717 rc = SQLITE_ERROR1;
254718 }else{
254719 Fts5TokenizerModule *pNew = 0;
254720 rc = fts5NewTokenizerModule(pGlobal, zName, pUserData, xDestroy, &pNew);
254721 if( pNew ){
254722 pNew->x2 = *pTokenizer;
254723 pNew->bV2Native = 1;
254724 pNew->x1.xCreate = fts5VtoVCreate;
254725 pNew->x1.xTokenize = fts5V1toV2Tokenize;
254726 pNew->x1.xDelete = fts5VtoVDelete;
254727 }
254728 }
254729
254730 return rc;
254731}
254732
254733/*
254734** The fts5_api.xCreateTokenizer() method.
254735*/
254736static int fts5CreateTokenizer(
254737 fts5_api *pApi, /* Global context (one per db handle) */
254738 const char *zName, /* Name of new function */
254739 void *pUserData, /* User data for aux. function */
254740 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
254741 void(*xDestroy)(void*) /* Destructor for pUserData */
254742){
254743 Fts5TokenizerModule *pNew = 0;
254744 int rc = SQLITE_OK0;
254745
254746 rc = fts5NewTokenizerModule(
254747 (Fts5Global*)pApi, zName, pUserData, xDestroy, &pNew
254748 );
254749 if( pNew ){
254750 pNew->x1 = *pTokenizer;
254751 pNew->x2.xCreate = fts5VtoVCreate;
254752 pNew->x2.xTokenize = fts5V2toV1Tokenize;
254753 pNew->x2.xDelete = fts5VtoVDelete;
254754 }
254755 return rc;
254756}
254757
254758/*
254759** Search the global context passed as the first argument for a tokenizer
254760** module named zName. If found, return a pointer to the Fts5TokenizerModule
254761** object. Otherwise, return NULL.
254762*/
254763static Fts5TokenizerModule *fts5LocateTokenizer(
254764 Fts5Global *pGlobal, /* Global (one per db handle) object */
254765 const char *zName /* Name of tokenizer module to find */
254766){
254767 Fts5TokenizerModule *pMod = 0;
254768
254769 if( zName==0 ){
254770 pMod = pGlobal->pDfltTok;
254771 }else{
254772 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
254773 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
254774 }
254775 }
254776
254777 return pMod;
254778}
254779
254780/*
254781** Find a tokenizer. This is the implementation of the
254782** fts5_api.xFindTokenizer_v2() method.
254783*/
254784static int fts5FindTokenizer_v2(
254785 fts5_api *pApi, /* Global context (one per db handle) */
254786 const char *zName, /* Name of tokenizer */
254787 void **ppUserData,
254788 fts5_tokenizer_v2 **ppTokenizer /* Populate this object */
254789){
254790 int rc = SQLITE_OK0;
254791 Fts5TokenizerModule *pMod;
254792
254793 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
254794 if( pMod ){
254795 if( pMod->bV2Native ){
254796 *ppUserData = pMod->pUserData;
254797 }else{
254798 *ppUserData = (void*)pMod;
254799 }
254800 *ppTokenizer = &pMod->x2;
254801 }else{
254802 *ppTokenizer = 0;
254803 *ppUserData = 0;
254804 rc = SQLITE_ERROR1;
254805 }
254806
254807 return rc;
254808}
254809
254810/*
254811** Find a tokenizer. This is the implementation of the
254812** fts5_api.xFindTokenizer() method.
254813*/
254814static int fts5FindTokenizer(
254815 fts5_api *pApi, /* Global context (one per db handle) */
254816 const char *zName, /* Name of new function */
254817 void **ppUserData,
254818 fts5_tokenizer *pTokenizer /* Populate this object */
254819){
254820 int rc = SQLITE_OK0;
254821 Fts5TokenizerModule *pMod;
254822
254823 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
254824 if( pMod ){
254825 if( pMod->bV2Native==0 ){
254826 *ppUserData = pMod->pUserData;
254827 }else{
254828 *ppUserData = (void*)pMod;
254829 }
254830 *pTokenizer = pMod->x1;
254831 }else{
254832 memset(pTokenizer, 0, sizeof(*pTokenizer));
254833 *ppUserData = 0;
254834 rc = SQLITE_ERROR1;
254835 }
254836
254837 return rc;
254838}
254839
254840/*
254841** Attempt to instantiate the tokenizer.
254842*/
254843static int sqlite3Fts5LoadTokenizer(Fts5Config *pConfig){
254844 const char **azArg = pConfig->t.azArg;
254845 const int nArg = pConfig->t.nArg;
254846 Fts5TokenizerModule *pMod = 0;
254847 int rc = SQLITE_OK0;
254848
254849 pMod = fts5LocateTokenizer(pConfig->pGlobal, nArg==0 ? 0 : azArg[0]);
254850 if( pMod==0 ){
254851 assert( nArg>0 )((void) (0));
254852 rc = SQLITE_ERROR1;
254853 sqlite3Fts5ConfigErrmsg(pConfig, "no such tokenizer: %s", azArg[0]);
254854 }else{
254855 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**) = 0;
254856 if( pMod->bV2Native ){
254857 xCreate = pMod->x2.xCreate;
254858 pConfig->t.pApi2 = &pMod->x2;
254859 }else{
254860 pConfig->t.pApi1 = &pMod->x1;
254861 xCreate = pMod->x1.xCreate;
254862 }
254863
254864 rc = xCreate(pMod->pUserData,
254865 (azArg?&azArg[1]:0), (nArg?nArg-1:0), &pConfig->t.pTok
254866 );
254867
254868 if( rc!=SQLITE_OK0 ){
254869 if( rc!=SQLITE_NOMEM7 ){
254870 sqlite3Fts5ConfigErrmsg(pConfig, "error in tokenizer constructor");
254871 }
254872 }else if( pMod->bV2Native==0 ){
254873 pConfig->t.ePattern = sqlite3Fts5TokenizerPattern(
254874 pMod->x1.xCreate, pConfig->t.pTok
254875 );
254876 }
254877 }
254878
254879 if( rc!=SQLITE_OK0 ){
254880 pConfig->t.pApi1 = 0;
254881 pConfig->t.pApi2 = 0;
254882 pConfig->t.pTok = 0;
254883 }
254884
254885 return rc;
254886}
254887
254888
254889/*
254890** xDestroy callback passed to sqlite3_create_module(). This is invoked
254891** when the db handle is being closed. Free memory associated with
254892** tokenizers and aux functions registered with this db handle.
254893*/
254894static void fts5ModuleDestroy(void *pCtx){
254895 Fts5TokenizerModule *pTok, *pNextTok;
254896 Fts5Auxiliary *pAux, *pNextAux;
254897 Fts5Global *pGlobal = (Fts5Global*)pCtx;
254898
254899 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
254900 pNextAux = pAux->pNext;
254901 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
254902 sqlite3_free(pAux);
254903 }
254904
254905 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
254906 pNextTok = pTok->pNext;
254907 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
254908 sqlite3_free(pTok);
254909 }
254910
254911 sqlite3_free(pGlobal);
254912}
254913
254914/*
254915** Implementation of the fts5() function used by clients to obtain the
254916** API pointer.
254917*/
254918static void fts5Fts5Func(
254919 sqlite3_context *pCtx, /* Function call context */
254920 int nArg, /* Number of args */
254921 sqlite3_value **apArg /* Function arguments */
254922){
254923 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
254924 fts5_api **ppApi;
254925 UNUSED_PARAM(nArg)(void)(nArg);
254926 assert( nArg==1 )((void) (0));
254927 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
254928 if( ppApi ) *ppApi = &pGlobal->api;
254929}
254930
254931/*
254932** Implementation of fts5_source_id() function.
254933*/
254934static void fts5SourceIdFunc(
254935 sqlite3_context *pCtx, /* Function call context */
254936 int nArg, /* Number of args */
254937 sqlite3_value **apUnused /* Function arguments */
254938){
254939 assert( nArg==0 )((void) (0));
254940 UNUSED_PARAM2(nArg, apUnused)(void)(nArg), (void)(apUnused);
254941 sqlite3_result_text(pCtx, "fts5: 2024-11-25 12:07:48 b95d11e958643b969c47a8e5857f3793b9e69700b8f1469371386369a26e577e", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
254942}
254943
254944/*
254945** Implementation of fts5_locale(LOCALE, TEXT) function.
254946**
254947** If parameter LOCALE is NULL, or a zero-length string, then a copy of
254948** TEXT is returned. Otherwise, both LOCALE and TEXT are interpreted as
254949** text, and the value returned is a blob consisting of:
254950**
254951** * The 4 bytes 0x00, 0xE0, 0xB2, 0xEb (FTS5_LOCALE_HEADER).
254952** * The LOCALE, as utf-8 text, followed by
254953** * 0x00, followed by
254954** * The TEXT, as utf-8 text.
254955**
254956** There is no final nul-terminator following the TEXT value.
254957*/
254958static void fts5LocaleFunc(
254959 sqlite3_context *pCtx, /* Function call context */
254960 int nArg, /* Number of args */
254961 sqlite3_value **apArg /* Function arguments */
254962){
254963 const char *zLocale = 0;
254964 int nLocale = 0;
254965 const char *zText = 0;
254966 int nText = 0;
254967
254968 assert( nArg==2 )((void) (0));
254969 UNUSED_PARAM(nArg)(void)(nArg);
254970
254971 zLocale = (const char*)sqlite3_value_text(apArg[0]);
254972 nLocale = sqlite3_value_bytes(apArg[0]);
254973
254974 zText = (const char*)sqlite3_value_text(apArg[1]);
254975 nText = sqlite3_value_bytes(apArg[1]);
254976
254977 if( zLocale==0 || zLocale[0]=='\0' ){
254978 sqlite3_result_text(pCtx, zText, nText, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
254979 }else{
254980 Fts5Global *p = (Fts5Global*)sqlite3_user_data(pCtx);
254981 u8 *pBlob = 0;
254982 u8 *pCsr = 0;
254983 int nBlob = 0;
254984
254985 nBlob = FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )) + nLocale + 1 + nText;
254986 pBlob = (u8*)sqlite3_malloc(nBlob);
254987 if( pBlob==0 ){
254988 sqlite3_result_error_nomem(pCtx);
254989 return;
254990 }
254991
254992 pCsr = pBlob;
254993 memcpy(pCsr, (const u8*)p->aLocaleHdr, FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )));
254994 pCsr += FTS5_LOCALE_HDR_SIZE((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ));
254995 memcpy(pCsr, zLocale, nLocale);
254996 pCsr += nLocale;
254997 (*pCsr++) = 0x00;
254998 if( zText ) memcpy(pCsr, zText, nText);
254999 assert( &pCsr[nText]==&pBlob[nBlob] )((void) (0));
255000
255001 sqlite3_result_blob(pCtx, pBlob, nBlob, sqlite3_free);
255002 }
255003}
255004
255005/*
255006** Return true if zName is the extension on one of the shadow tables used
255007** by this module.
255008*/
255009static int fts5ShadowName(const char *zName){
255010 static const char *azName[] = {
255011 "config", "content", "data", "docsize", "idx"
255012 };
255013 unsigned int i;
255014 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
255015 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
255016 }
255017 return 0;
255018}
255019
255020/*
255021** Run an integrity check on the FTS5 data structures. Return a string
255022** if anything is found amiss. Return a NULL pointer if everything is
255023** OK.
255024*/
255025static int fts5IntegrityMethod(
255026 sqlite3_vtab *pVtab, /* the FTS5 virtual table to check */
255027 const char *zSchema, /* Name of schema in which this table lives */
255028 const char *zTabname, /* Name of the table itself */
255029 int isQuick, /* True if this is a quick-check */
255030 char **pzErr /* Write error message here */
255031){
255032 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
255033 int rc;
255034
255035 assert( pzErr!=0 && *pzErr==0 )((void) (0));
255036 UNUSED_PARAM(isQuick)(void)(isQuick);
255037 assert( pTab->p.pConfig->pzErrmsg==0 )((void) (0));
255038 pTab->p.pConfig->pzErrmsg = pzErr;
255039 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, 0);
255040 if( *pzErr==0 && rc!=SQLITE_OK0 ){
255041 if( (rc&0xff)==SQLITE_CORRUPT11 ){
255042 *pzErr = sqlite3_mprintf("malformed inverted index for FTS5 table %s.%s",
255043 zSchema, zTabname);
255044 rc = (*pzErr) ? SQLITE_OK0 : SQLITE_NOMEM7;
255045 }else{
255046 *pzErr = sqlite3_mprintf("unable to validate the inverted index for"
255047 " FTS5 table %s.%s: %s",
255048 zSchema, zTabname, sqlite3_errstr(rc));
255049 }
255050 }
255051
255052 sqlite3Fts5IndexCloseReader(pTab->p.pIndex);
255053 pTab->p.pConfig->pzErrmsg = 0;
255054
255055 return rc;
255056}
255057
255058static int fts5Init(sqlite3 *db){
255059 static const sqlite3_module fts5Mod = {
255060 /* iVersion */ 4,
255061 /* xCreate */ fts5CreateMethod,
255062 /* xConnect */ fts5ConnectMethod,
255063 /* xBestIndex */ fts5BestIndexMethod,
255064 /* xDisconnect */ fts5DisconnectMethod,
255065 /* xDestroy */ fts5DestroyMethod,
255066 /* xOpen */ fts5OpenMethod,
255067 /* xClose */ fts5CloseMethod,
255068 /* xFilter */ fts5FilterMethod,
255069 /* xNext */ fts5NextMethod,
255070 /* xEof */ fts5EofMethod,
255071 /* xColumn */ fts5ColumnMethod,
255072 /* xRowid */ fts5RowidMethod,
255073 /* xUpdate */ fts5UpdateMethod,
255074 /* xBegin */ fts5BeginMethod,
255075 /* xSync */ fts5SyncMethod,
255076 /* xCommit */ fts5CommitMethod,
255077 /* xRollback */ fts5RollbackMethod,
255078 /* xFindFunction */ fts5FindFunctionMethod,
255079 /* xRename */ fts5RenameMethod,
255080 /* xSavepoint */ fts5SavepointMethod,
255081 /* xRelease */ fts5ReleaseMethod,
255082 /* xRollbackTo */ fts5RollbackToMethod,
255083 /* xShadowName */ fts5ShadowName,
255084 /* xIntegrity */ fts5IntegrityMethod
255085 };
255086
255087 int rc;
255088 Fts5Global *pGlobal = 0;
255089
255090 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
255091 if( pGlobal==0 ){
255092 rc = SQLITE_NOMEM7;
255093 }else{
255094 void *p = (void*)pGlobal;
255095 memset(pGlobal, 0, sizeof(Fts5Global));
255096 pGlobal->db = db;
255097 pGlobal->api.iVersion = 3;
255098 pGlobal->api.xCreateFunction = fts5CreateAux;
255099 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
255100 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
255101 pGlobal->api.xCreateTokenizer_v2 = fts5CreateTokenizer_v2;
255102 pGlobal->api.xFindTokenizer_v2 = fts5FindTokenizer_v2;
255103
255104 /* Initialize pGlobal->aLocaleHdr[] to a 128-bit pseudo-random vector.
255105 ** The constants below were generated randomly. */
255106 sqlite3_randomness(sizeof(pGlobal->aLocaleHdr), pGlobal->aLocaleHdr);
255107 pGlobal->aLocaleHdr[0] ^= 0xF924976D;
255108 pGlobal->aLocaleHdr[1] ^= 0x16596E13;
255109 pGlobal->aLocaleHdr[2] ^= 0x7C80BEAA;
255110 pGlobal->aLocaleHdr[3] ^= 0x9B03A67F;
255111 assert( sizeof(pGlobal->aLocaleHdr)==16 )((void) (0));
255112
255113 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
255114 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5IndexInit(db);
255115 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5ExprInit(pGlobal, db);
255116 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
255117 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
255118 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5VocabInit(pGlobal, db);
255119 if( rc==SQLITE_OK0 ){
255120 rc = sqlite3_create_function(
255121 db, "fts5", 1, SQLITE_UTF81, p, fts5Fts5Func, 0, 0
255122 );
255123 }
255124 if( rc==SQLITE_OK0 ){
255125 rc = sqlite3_create_function(
255126 db, "fts5_source_id", 0,
255127 SQLITE_UTF81|SQLITE_DETERMINISTIC0x000000800|SQLITE_INNOCUOUS0x000200000,
255128 p, fts5SourceIdFunc, 0, 0
255129 );
255130 }
255131 if( rc==SQLITE_OK0 ){
255132 rc = sqlite3_create_function(
255133 db, "fts5_locale", 2,
255134 SQLITE_UTF81|SQLITE_INNOCUOUS0x000200000|SQLITE_RESULT_SUBTYPE0x001000000,
255135 p, fts5LocaleFunc, 0, 0
255136 );
255137 }
255138 }
255139
255140 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
255141 ** fts5_test_mi.c is compiled and linked into the executable. And call
255142 ** its entry point to enable the matchinfo() demo. */
255143#ifdef SQLITE_FTS5_ENABLE_TEST_MI
255144 if( rc==SQLITE_OK0 ){
255145 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
255146 rc = sqlite3Fts5TestRegisterMatchinfo(db);
255147 }
255148#endif
255149
255150 return rc;
255151}
255152
255153/*
255154** The following functions are used to register the module with SQLite. If
255155** this module is being built as part of the SQLite core (SQLITE_CORE is
255156** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
255157**
255158** Or, if this module is being built as a loadable extension,
255159** sqlite3Fts5Init() is omitted and the two standard entry points
255160** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
255161*/
255162#ifndef SQLITE_CORE1
255163#ifdef _WIN32
255164__declspec(dllexport)
255165#endif
255166SQLITE_API int sqlite3_fts_init(
255167 sqlite3 *db,
255168 char **pzErrMsg,
255169 const sqlite3_api_routines *pApi
255170){
255171 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
255172 (void)pzErrMsg; /* Unused parameter */
255173 return fts5Init(db);
255174}
255175
255176#ifdef _WIN32
255177__declspec(dllexport)
255178#endif
255179SQLITE_API int sqlite3_fts5_init(
255180 sqlite3 *db,
255181 char **pzErrMsg,
255182 const sqlite3_api_routines *pApi
255183){
255184 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
255185 (void)pzErrMsg; /* Unused parameter */
255186 return fts5Init(db);
255187}
255188#else
255189SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3 *db){
255190 return fts5Init(db);
255191}
255192#endif
255193
255194/*
255195** 2014 May 31
255196**
255197** The author disclaims copyright to this source code. In place of
255198** a legal notice, here is a blessing:
255199**
255200** May you do good and not evil.
255201** May you find forgiveness for yourself and forgive others.
255202** May you share freely, never taking more than you give.
255203**
255204******************************************************************************
255205**
255206*/
255207
255208
255209
255210/* #include "fts5Int.h" */
255211
255212/*
255213** pSavedRow:
255214** SQL statement FTS5_STMT_LOOKUP2 is a copy of FTS5_STMT_LOOKUP, it
255215** does a by-rowid lookup to retrieve a single row from the %_content
255216** table or equivalent external-content table/view.
255217**
255218** However, FTS5_STMT_LOOKUP2 is only used when retrieving the original
255219** values for a row being UPDATEd. In that case, the SQL statement is
255220** not reset and pSavedRow is set to point at it. This is so that the
255221** insert operation that follows the delete may access the original
255222** row values for any new values for which sqlite3_value_nochange() returns
255223** true. i.e. if the user executes:
255224**
255225** CREATE VIRTUAL TABLE ft USING fts5(a, b, c, locale=1);
255226** ...
255227** UPDATE fts SET a=?, b=? WHERE rowid=?;
255228**
255229** then the value passed to the xUpdate() method of this table as the
255230** new.c value is an sqlite3_value_nochange() value. So in this case it
255231** must be read from the saved row stored in Fts5Storage.pSavedRow.
255232**
255233** This is necessary - using sqlite3_value_nochange() instead of just having
255234** SQLite pass the original value back via xUpdate() - so as not to discard
255235** any locale information associated with such values.
255236**
255237*/
255238struct Fts5Storage {
255239 Fts5Config *pConfig;
255240 Fts5Index *pIndex;
255241 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
255242 i64 nTotalRow; /* Total number of rows in FTS table */
255243 i64 *aTotalSize; /* Total sizes of each column */
255244 sqlite3_stmt *pSavedRow;
255245 sqlite3_stmt *aStmt[12];
255246};
255247
255248
255249#if FTS5_STMT_SCAN_ASC0!=0
255250# error "FTS5_STMT_SCAN_ASC mismatch"
255251#endif
255252#if FTS5_STMT_SCAN_DESC1!=1
255253# error "FTS5_STMT_SCAN_DESC mismatch"
255254#endif
255255#if FTS5_STMT_LOOKUP2!=2
255256# error "FTS5_STMT_LOOKUP mismatch"
255257#endif
255258
255259#define FTS5_STMT_LOOKUP23 3
255260#define FTS5_STMT_INSERT_CONTENT4 4
255261#define FTS5_STMT_REPLACE_CONTENT5 5
255262#define FTS5_STMT_DELETE_CONTENT6 6
255263#define FTS5_STMT_REPLACE_DOCSIZE7 7
255264#define FTS5_STMT_DELETE_DOCSIZE8 8
255265#define FTS5_STMT_LOOKUP_DOCSIZE9 9
255266#define FTS5_STMT_REPLACE_CONFIG10 10
255267#define FTS5_STMT_SCAN11 11
255268
255269/*
255270** Prepare the two insert statements - Fts5Storage.pInsertContent and
255271** Fts5Storage.pInsertDocsize - if they have not already been prepared.
255272** Return SQLITE_OK if successful, or an SQLite error code if an error
255273** occurs.
255274*/
255275static int fts5StorageGetStmt(
255276 Fts5Storage *p, /* Storage handle */
255277 int eStmt, /* FTS5_STMT_XXX constant */
255278 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
255279 char **pzErrMsg /* OUT: Error message (if any) */
255280){
255281 int rc = SQLITE_OK0;
255282
255283 /* If there is no %_docsize table, there should be no requests for
255284 ** statements to operate on it. */
255285 assert( p->pConfig->bColumnsize || (((void) (0))
255286 eStmt!=FTS5_STMT_REPLACE_DOCSIZE((void) (0))
255287 && eStmt!=FTS5_STMT_DELETE_DOCSIZE((void) (0))
255288 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE((void) (0))
255289 ))((void) (0));
255290
255291 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) )((void) (0));
255292 if( p->aStmt[eStmt]==0 ){
255293 const char *azStmt[] = {
255294 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
255295 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
255296 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
255297 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP2 */
255298
255299 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
255300 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
255301 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
255302 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?%s)", /* REPLACE_DOCSIZE */
255303 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
255304
255305 "SELECT sz%s FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
255306
255307 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
255308 "SELECT %s FROM %s AS T", /* SCAN */
255309 };
255310 Fts5Config *pC = p->pConfig;
255311 char *zSql = 0;
255312
255313 assert( ArraySize(azStmt)==ArraySize(p->aStmt) )((void) (0));
255314
255315 switch( eStmt ){
255316 case FTS5_STMT_SCAN11:
255317 zSql = sqlite3_mprintf(azStmt[eStmt],
255318 pC->zContentExprlist, pC->zContent
255319 );
255320 break;
255321
255322 case FTS5_STMT_SCAN_ASC0:
255323 case FTS5_STMT_SCAN_DESC1:
255324 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
255325 pC->zContent, pC->zContentRowid, pC->zContentRowid,
255326 pC->zContentRowid
255327 );
255328 break;
255329
255330 case FTS5_STMT_LOOKUP2:
255331 case FTS5_STMT_LOOKUP23:
255332 zSql = sqlite3_mprintf(azStmt[eStmt],
255333 pC->zContentExprlist, pC->zContent, pC->zContentRowid
255334 );
255335 break;
255336
255337 case FTS5_STMT_INSERT_CONTENT4:
255338 case FTS5_STMT_REPLACE_CONTENT5: {
255339 char *zBind = 0;
255340 int i;
255341
255342 assert( pC->eContent==FTS5_CONTENT_NORMAL((void) (0))
255343 || pC->eContent==FTS5_CONTENT_UNINDEXED((void) (0))
255344 )((void) (0));
255345
255346 /* Add bindings for the "c*" columns - those that store the actual
255347 ** table content. If eContent==NORMAL, then there is one binding
255348 ** for each column. Or, if eContent==UNINDEXED, then there are only
255349 ** bindings for the UNINDEXED columns. */
255350 for(i=0; rc==SQLITE_OK0 && i<(pC->nCol+1); i++){
255351 if( !i || pC->eContent==FTS5_CONTENT_NORMAL0 || pC->abUnindexed[i-1] ){
255352 zBind = sqlite3Fts5Mprintf(&rc, "%z%s?%d", zBind, zBind?",":"",i+1);
255353 }
255354 }
255355
255356 /* Add bindings for any "l*" columns. Only non-UNINDEXED columns
255357 ** require these. */
255358 if( pC->bLocale && pC->eContent==FTS5_CONTENT_NORMAL0 ){
255359 for(i=0; rc==SQLITE_OK0 && i<pC->nCol; i++){
255360 if( pC->abUnindexed[i]==0 ){
255361 zBind = sqlite3Fts5Mprintf(&rc, "%z,?%d", zBind, pC->nCol+i+2);
255362 }
255363 }
255364 }
255365
255366 zSql = sqlite3Fts5Mprintf(&rc, azStmt[eStmt], pC->zDb, pC->zName,zBind);
255367 sqlite3_free(zBind);
255368 break;
255369 }
255370
255371 case FTS5_STMT_REPLACE_DOCSIZE7:
255372 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName,
255373 (pC->bContentlessDelete ? ",?" : "")
255374 );
255375 break;
255376
255377 case FTS5_STMT_LOOKUP_DOCSIZE9:
255378 zSql = sqlite3_mprintf(azStmt[eStmt],
255379 (pC->bContentlessDelete ? ",origin" : ""),
255380 pC->zDb, pC->zName
255381 );
255382 break;
255383
255384 default:
255385 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
255386 break;
255387 }
255388
255389 if( zSql==0 ){
255390 rc = SQLITE_NOMEM7;
255391 }else{
255392 int f = SQLITE_PREPARE_PERSISTENT0x01;
255393 if( eStmt>FTS5_STMT_LOOKUP23 ) f |= SQLITE_PREPARE_NO_VTAB0x04;
255394 p->pConfig->bLock++;
255395 rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
255396 p->pConfig->bLock--;
255397 sqlite3_free(zSql);
255398 if( rc!=SQLITE_OK0 && pzErrMsg ){
255399 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
255400 }
255401 }
255402 }
255403
255404 *ppStmt = p->aStmt[eStmt];
255405 sqlite3_reset(*ppStmt);
255406 return rc;
255407}
255408
255409
255410static int fts5ExecPrintf(
255411 sqlite3 *db,
255412 char **pzErr,
255413 const char *zFormat,
255414 ...
255415){
255416 int rc;
255417 va_list ap; /* ... printf arguments */
255418 char *zSql;
255419
255420 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
255421 zSql = sqlite3_vmprintf(zFormat, ap);
255422
255423 if( zSql==0 ){
255424 rc = SQLITE_NOMEM7;
255425 }else{
255426 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
255427 sqlite3_free(zSql);
255428 }
255429
255430 va_end(ap)__builtin_va_end(ap);
255431 return rc;
255432}
255433
255434/*
255435** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
255436** code otherwise.
255437*/
255438static int sqlite3Fts5DropAll(Fts5Config *pConfig){
255439 int rc = fts5ExecPrintf(pConfig->db, 0,
255440 "DROP TABLE IF EXISTS %Q.'%q_data';"
255441 "DROP TABLE IF EXISTS %Q.'%q_idx';"
255442 "DROP TABLE IF EXISTS %Q.'%q_config';",
255443 pConfig->zDb, pConfig->zName,
255444 pConfig->zDb, pConfig->zName,
255445 pConfig->zDb, pConfig->zName
255446 );
255447 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
255448 rc = fts5ExecPrintf(pConfig->db, 0,
255449 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
255450 pConfig->zDb, pConfig->zName
255451 );
255452 }
255453 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
255454 rc = fts5ExecPrintf(pConfig->db, 0,
255455 "DROP TABLE IF EXISTS %Q.'%q_content';",
255456 pConfig->zDb, pConfig->zName
255457 );
255458 }
255459 return rc;
255460}
255461
255462static void fts5StorageRenameOne(
255463 Fts5Config *pConfig, /* Current FTS5 configuration */
255464 int *pRc, /* IN/OUT: Error code */
255465 const char *zTail, /* Tail of table name e.g. "data", "config" */
255466 const char *zName /* New name of FTS5 table */
255467){
255468 if( *pRc==SQLITE_OK0 ){
255469 *pRc = fts5ExecPrintf(pConfig->db, 0,
255470 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
255471 pConfig->zDb, pConfig->zName, zTail, zName, zTail
255472 );
255473 }
255474}
255475
255476static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
255477 Fts5Config *pConfig = pStorage->pConfig;
255478 int rc = sqlite3Fts5StorageSync(pStorage);
255479
255480 fts5StorageRenameOne(pConfig, &rc, "data", zName);
255481 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
255482 fts5StorageRenameOne(pConfig, &rc, "config", zName);
255483 if( pConfig->bColumnsize ){
255484 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
255485 }
255486 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
255487 fts5StorageRenameOne(pConfig, &rc, "content", zName);
255488 }
255489 return rc;
255490}
255491
255492/*
255493** Create the shadow table named zPost, with definition zDefn. Return
255494** SQLITE_OK if successful, or an SQLite error code otherwise.
255495*/
255496static int sqlite3Fts5CreateTable(
255497 Fts5Config *pConfig, /* FTS5 configuration */
255498 const char *zPost, /* Shadow table to create (e.g. "content") */
255499 const char *zDefn, /* Columns etc. for shadow table */
255500 int bWithout, /* True for without rowid */
255501 char **pzErr /* OUT: Error message */
255502){
255503 int rc;
255504 char *zErr = 0;
255505
255506 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
255507 pConfig->zDb, pConfig->zName, zPost, zDefn,
255508#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
255509 bWithout?" WITHOUT ROWID":
255510#endif
255511 ""
255512 );
255513 if( zErr ){
255514 *pzErr = sqlite3_mprintf(
255515 "fts5: error creating shadow table %q_%s: %s",
255516 pConfig->zName, zPost, zErr
255517 );
255518 sqlite3_free(zErr);
255519 }
255520
255521 return rc;
255522}
255523
255524/*
255525** Open a new Fts5Index handle. If the bCreate argument is true, create
255526** and initialize the underlying tables
255527**
255528** If successful, set *pp to point to the new object and return SQLITE_OK.
255529** Otherwise, set *pp to NULL and return an SQLite error code.
255530*/
255531static int sqlite3Fts5StorageOpen(
255532 Fts5Config *pConfig,
255533 Fts5Index *pIndex,
255534 int bCreate,
255535 Fts5Storage **pp,
255536 char **pzErr /* OUT: Error message */
255537){
255538 int rc = SQLITE_OK0;
255539 Fts5Storage *p; /* New object */
255540 sqlite3_int64 nByte; /* Bytes of space to allocate */
255541
255542 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
255543 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
255544 *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
255545 if( !p ) return SQLITE_NOMEM7;
255546
255547 memset(p, 0, (size_t)nByte);
255548 p->aTotalSize = (i64*)&p[1];
255549 p->pConfig = pConfig;
255550 p->pIndex = pIndex;
255551
255552 if( bCreate ){
255553 if( pConfig->eContent==FTS5_CONTENT_NORMAL0
255554 || pConfig->eContent==FTS5_CONTENT_UNINDEXED3
255555 ){
255556 int nDefn = 32 + pConfig->nCol*10;
255557 char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 20);
255558 if( zDefn==0 ){
255559 rc = SQLITE_NOMEM7;
255560 }else{
255561 int i;
255562 int iOff;
255563 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
255564 iOff = (int)strlen(zDefn);
255565 for(i=0; i<pConfig->nCol; i++){
255566 if( pConfig->eContent==FTS5_CONTENT_NORMAL0
255567 || pConfig->abUnindexed[i]
255568 ){
255569 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
255570 iOff += (int)strlen(&zDefn[iOff]);
255571 }
255572 }
255573 if( pConfig->bLocale ){
255574 for(i=0; i<pConfig->nCol; i++){
255575 if( pConfig->abUnindexed[i]==0 ){
255576 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", l%d", i);
255577 iOff += (int)strlen(&zDefn[iOff]);
255578 }
255579 }
255580 }
255581 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
255582 }
255583 sqlite3_free(zDefn);
255584 }
255585
255586 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
255587 const char *zCols = "id INTEGER PRIMARY KEY, sz BLOB";
255588 if( pConfig->bContentlessDelete ){
255589 zCols = "id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER";
255590 }
255591 rc = sqlite3Fts5CreateTable(pConfig, "docsize", zCols, 0, pzErr);
255592 }
255593 if( rc==SQLITE_OK0 ){
255594 rc = sqlite3Fts5CreateTable(
255595 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
255596 );
255597 }
255598 if( rc==SQLITE_OK0 ){
255599 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION4);
255600 }
255601 }
255602
255603 if( rc ){
255604 sqlite3Fts5StorageClose(p);
255605 *pp = 0;
255606 }
255607 return rc;
255608}
255609
255610/*
255611** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
255612*/
255613static int sqlite3Fts5StorageClose(Fts5Storage *p){
255614 int rc = SQLITE_OK0;
255615 if( p ){
255616 int i;
255617
255618 /* Finalize all SQL statements */
255619 for(i=0; i<ArraySize(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
255620 sqlite3_finalize(p->aStmt[i]);
255621 }
255622
255623 sqlite3_free(p);
255624 }
255625 return rc;
255626}
255627
255628typedef struct Fts5InsertCtx Fts5InsertCtx;
255629struct Fts5InsertCtx {
255630 Fts5Storage *pStorage;
255631 int iCol;
255632 int szCol; /* Size of column value in tokens */
255633};
255634
255635/*
255636** Tokenization callback used when inserting tokens into the FTS index.
255637*/
255638static int fts5StorageInsertCallback(
255639 void *pContext, /* Pointer to Fts5InsertCtx object */
255640 int tflags,
255641 const char *pToken, /* Buffer containing token */
255642 int nToken, /* Size of token in bytes */
255643 int iUnused1, /* Start offset of token */
255644 int iUnused2 /* End offset of token */
255645){
255646 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
255647 Fts5Index *pIdx = pCtx->pStorage->pIndex;
255648 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
255649 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
255650 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
255651 pCtx->szCol++;
255652 }
255653 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
255654}
255655
255656/*
255657** This function is used as part of an UPDATE statement that modifies the
255658** rowid of a row. In that case, this function is called first to set
255659** Fts5Storage.pSavedRow to point to a statement that may be used to
255660** access the original values of the row being deleted - iDel.
255661**
255662** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
255663** It is not considered an error if row iDel does not exist. In this case
255664** pSavedRow is not set and SQLITE_OK returned.
255665*/
255666static int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel){
255667 int rc = SQLITE_OK0;
255668 sqlite3_stmt *pSeek = 0;
255669
255670 assert( p->pSavedRow==0 )((void) (0));
255671 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP2+1, &pSeek, 0);
255672 if( rc==SQLITE_OK0 ){
255673 sqlite3_bind_int64(pSeek, 1, iDel);
255674 if( sqlite3_step(pSeek)!=SQLITE_ROW100 ){
255675 rc = sqlite3_reset(pSeek);
255676 }else{
255677 p->pSavedRow = pSeek;
255678 }
255679 }
255680
255681 return rc;
255682}
255683
255684/*
255685** If a row with rowid iDel is present in the %_content table, add the
255686** delete-markers to the FTS index necessary to delete it. Do not actually
255687** remove the %_content row at this time though.
255688**
255689** If parameter bSaveRow is true, then Fts5Storage.pSavedRow is left
255690** pointing to a statement (FTS5_STMT_LOOKUP2) that may be used to access
255691** the original values of the row being deleted. This is used by UPDATE
255692** statements.
255693*/
255694static int fts5StorageDeleteFromIndex(
255695 Fts5Storage *p,
255696 i64 iDel,
255697 sqlite3_value **apVal,
255698 int bSaveRow /* True to set pSavedRow */
255699){
255700 Fts5Config *pConfig = p->pConfig;
255701 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
255702 int rc = SQLITE_OK0; /* Return code */
255703 int rc2; /* sqlite3_reset() return code */
255704 int iCol;
255705 Fts5InsertCtx ctx;
255706
255707 assert( bSaveRow==0 || apVal==0 )((void) (0));
255708 assert( bSaveRow==0 || bSaveRow==1 )((void) (0));
255709 assert( FTS5_STMT_LOOKUP2==FTS5_STMT_LOOKUP+1 )((void) (0));
255710
255711 if( apVal==0 ){
255712 if( p->pSavedRow && bSaveRow ){
255713 pSeek = p->pSavedRow;
255714 p->pSavedRow = 0;
255715 }else{
255716 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP2+bSaveRow, &pSeek, 0);
255717 if( rc!=SQLITE_OK0 ) return rc;
255718 sqlite3_bind_int64(pSeek, 1, iDel);
255719 if( sqlite3_step(pSeek)!=SQLITE_ROW100 ){
255720 return sqlite3_reset(pSeek);
255721 }
255722 }
255723 }
255724
255725 ctx.pStorage = p;
255726 ctx.iCol = -1;
255727 for(iCol=1; rc==SQLITE_OK0 && iCol<=pConfig->nCol; iCol++){
255728 if( pConfig->abUnindexed[iCol-1]==0 ){
255729 sqlite3_value *pVal = 0;
255730 const char *pText = 0;
255731 int nText = 0;
255732 const char *pLoc = 0;
255733 int nLoc = 0;
255734
255735 assert( pSeek==0 || apVal==0 )((void) (0));
255736 assert( pSeek!=0 || apVal!=0 )((void) (0));
255737 if( pSeek ){
255738 pVal = sqlite3_column_value(pSeek, iCol);
255739 }else{
255740 pVal = apVal[iCol-1];
255741 }
255742
255743 if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
255744 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
255745 }else{
255746 pText = (const char*)sqlite3_value_text(pVal);
255747 nText = sqlite3_value_bytes(pVal);
255748 if( pConfig->bLocale && pSeek ){
255749 pLoc = (const char*)sqlite3_column_text(pSeek, iCol + pConfig->nCol);
255750 nLoc = sqlite3_column_bytes(pSeek, iCol + pConfig->nCol);
255751 }
255752 }
255753
255754 if( rc==SQLITE_OK0 ){
255755 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
255756 ctx.szCol = 0;
255757 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT0x0004,
255758 pText, nText, (void*)&ctx, fts5StorageInsertCallback
255759 );
255760 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
255761 if( rc==SQLITE_OK0 && p->aTotalSize[iCol-1]<0 ){
255762 rc = FTS5_CORRUPT(11 | (1<<8));
255763 }
255764 sqlite3Fts5ClearLocale(pConfig);
255765 }
255766 }
255767 }
255768 if( rc==SQLITE_OK0 && p->nTotalRow<1 ){
255769 rc = FTS5_CORRUPT(11 | (1<<8));
255770 }else{
255771 p->nTotalRow--;
255772 }
255773
255774 if( rc==SQLITE_OK0 && bSaveRow ){
255775 assert( p->pSavedRow==0 )((void) (0));
255776 p->pSavedRow = pSeek;
255777 }else{
255778 rc2 = sqlite3_reset(pSeek);
255779 if( rc==SQLITE_OK0 ) rc = rc2;
255780 }
255781 return rc;
255782}
255783
255784/*
255785** Reset any saved statement pSavedRow. Zero pSavedRow as well. This
255786** should be called by the xUpdate() method of the fts5 table before
255787** returning from any operation that may have set Fts5Storage.pSavedRow.
255788*/
255789static void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage *pStorage){
255790 assert( pStorage->pSavedRow==0((void) (0))
255791 || pStorage->pSavedRow==pStorage->aStmt[FTS5_STMT_LOOKUP2]((void) (0))
255792 )((void) (0));
255793 sqlite3_reset(pStorage->pSavedRow);
255794 pStorage->pSavedRow = 0;
255795}
255796
255797/*
255798** This function is called to process a DELETE on a contentless_delete=1
255799** table. It adds the tombstone required to delete the entry with rowid
255800** iDel. If successful, SQLITE_OK is returned. Or, if an error occurs,
255801** an SQLite error code.
255802*/
255803static int fts5StorageContentlessDelete(Fts5Storage *p, i64 iDel){
255804 i64 iOrigin = 0;
255805 sqlite3_stmt *pLookup = 0;
255806 int rc = SQLITE_OK0;
255807
255808 assert( p->pConfig->bContentlessDelete )((void) (0));
255809 assert( p->pConfig->eContent==FTS5_CONTENT_NONE((void) (0))
255810 || p->pConfig->eContent==FTS5_CONTENT_UNINDEXED((void) (0))
255811 )((void) (0));
255812
255813 /* Look up the origin of the document in the %_docsize table. Store
255814 ** this in stack variable iOrigin. */
255815 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE9, &pLookup, 0);
255816 if( rc==SQLITE_OK0 ){
255817 sqlite3_bind_int64(pLookup, 1, iDel);
255818 if( SQLITE_ROW100==sqlite3_step(pLookup) ){
255819 iOrigin = sqlite3_column_int64(pLookup, 1);
255820 }
255821 rc = sqlite3_reset(pLookup);
255822 }
255823
255824 if( rc==SQLITE_OK0 && iOrigin!=0 ){
255825 rc = sqlite3Fts5IndexContentlessDelete(p->pIndex, iOrigin, iDel);
255826 }
255827
255828 return rc;
255829}
255830
255831/*
255832** Insert a record into the %_docsize table. Specifically, do:
255833**
255834** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
255835**
255836** If there is no %_docsize table (as happens if the columnsize=0 option
255837** is specified when the FTS5 table is created), this function is a no-op.
255838*/
255839static int fts5StorageInsertDocsize(
255840 Fts5Storage *p, /* Storage module to write to */
255841 i64 iRowid, /* id value */
255842 Fts5Buffer *pBuf /* sz value */
255843){
255844 int rc = SQLITE_OK0;
255845 if( p->pConfig->bColumnsize ){
255846 sqlite3_stmt *pReplace = 0;
255847 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE7, &pReplace, 0);
255848 if( rc==SQLITE_OK0 ){
255849 sqlite3_bind_int64(pReplace, 1, iRowid);
255850 if( p->pConfig->bContentlessDelete ){
255851 i64 iOrigin = 0;
255852 rc = sqlite3Fts5IndexGetOrigin(p->pIndex, &iOrigin);
255853 sqlite3_bind_int64(pReplace, 3, iOrigin);
255854 }
255855 }
255856 if( rc==SQLITE_OK0 ){
255857 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC((sqlite3_destructor_type)0));
255858 sqlite3_step(pReplace);
255859 rc = sqlite3_reset(pReplace);
255860 sqlite3_bind_null(pReplace, 2);
255861 }
255862 }
255863 return rc;
255864}
255865
255866/*
255867** Load the contents of the "averages" record from disk into the
255868** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
255869** argument bCache is true, set the p->bTotalsValid flag to indicate
255870** that the contents of aTotalSize[] and nTotalRow are valid until
255871** further notice.
255872**
255873** Return SQLITE_OK if successful, or an SQLite error code if an error
255874** occurs.
255875*/
255876static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
255877 int rc = SQLITE_OK0;
255878 if( p->bTotalsValid==0 ){
255879 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
255880 p->bTotalsValid = bCache;
255881 }
255882 return rc;
255883}
255884
255885/*
255886** Store the current contents of the p->nTotalRow and p->aTotalSize[]
255887** variables in the "averages" record on disk.
255888**
255889** Return SQLITE_OK if successful, or an SQLite error code if an error
255890** occurs.
255891*/
255892static int fts5StorageSaveTotals(Fts5Storage *p){
255893 int nCol = p->pConfig->nCol;
255894 int i;
255895 Fts5Buffer buf;
255896 int rc = SQLITE_OK0;
255897 memset(&buf, 0, sizeof(buf));
255898
255899 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
255900 for(i=0; i<nCol; i++){
255901 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
255902 }
255903 if( rc==SQLITE_OK0 ){
255904 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
255905 }
255906 sqlite3_free(buf.p);
255907
255908 return rc;
255909}
255910
255911/*
255912** Remove a row from the FTS table.
255913*/
255914static int sqlite3Fts5StorageDelete(
255915 Fts5Storage *p, /* Storage object */
255916 i64 iDel, /* Rowid to delete from table */
255917 sqlite3_value **apVal, /* Optional - values to remove from index */
255918 int bSaveRow /* If true, set pSavedRow for deleted row */
255919){
255920 Fts5Config *pConfig = p->pConfig;
255921 int rc;
255922 sqlite3_stmt *pDel = 0;
255923
255924 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 )((void) (0));
255925 rc = fts5StorageLoadTotals(p, 1);
255926
255927 /* Delete the index records */
255928 if( rc==SQLITE_OK0 ){
255929 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
255930 }
255931
255932 if( rc==SQLITE_OK0 ){
255933 if( p->pConfig->bContentlessDelete ){
255934 rc = fts5StorageContentlessDelete(p, iDel);
255935 if( rc==SQLITE_OK0
255936 && bSaveRow
255937 && p->pConfig->eContent==FTS5_CONTENT_UNINDEXED3
255938 ){
255939 rc = sqlite3Fts5StorageFindDeleteRow(p, iDel);
255940 }
255941 }else{
255942 rc = fts5StorageDeleteFromIndex(p, iDel, apVal, bSaveRow);
255943 }
255944 }
255945
255946 /* Delete the %_docsize record */
255947 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
255948 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE8, &pDel, 0);
255949 if( rc==SQLITE_OK0 ){
255950 sqlite3_bind_int64(pDel, 1, iDel);
255951 sqlite3_step(pDel);
255952 rc = sqlite3_reset(pDel);
255953 }
255954 }
255955
255956 /* Delete the %_content record */
255957 if( pConfig->eContent==FTS5_CONTENT_NORMAL0
255958 || pConfig->eContent==FTS5_CONTENT_UNINDEXED3
255959 ){
255960 if( rc==SQLITE_OK0 ){
255961 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT6, &pDel, 0);
255962 }
255963 if( rc==SQLITE_OK0 ){
255964 sqlite3_bind_int64(pDel, 1, iDel);
255965 sqlite3_step(pDel);
255966 rc = sqlite3_reset(pDel);
255967 }
255968 }
255969
255970 return rc;
255971}
255972
255973/*
255974** Delete all entries in the FTS5 index.
255975*/
255976static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
255977 Fts5Config *pConfig = p->pConfig;
255978 int rc;
255979
255980 p->bTotalsValid = 0;
255981
255982 /* Delete the contents of the %_data and %_docsize tables. */
255983 rc = fts5ExecPrintf(pConfig->db, 0,
255984 "DELETE FROM %Q.'%q_data';"
255985 "DELETE FROM %Q.'%q_idx';",
255986 pConfig->zDb, pConfig->zName,
255987 pConfig->zDb, pConfig->zName
255988 );
255989 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
255990 rc = fts5ExecPrintf(pConfig->db, 0,
255991 "DELETE FROM %Q.'%q_docsize';", pConfig->zDb, pConfig->zName
255992 );
255993 }
255994
255995 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_UNINDEXED3 ){
255996 rc = fts5ExecPrintf(pConfig->db, 0,
255997 "DELETE FROM %Q.'%q_content';", pConfig->zDb, pConfig->zName
255998 );
255999 }
256000
256001 /* Reinitialize the %_data table. This call creates the initial structure
256002 ** and averages records. */
256003 if( rc==SQLITE_OK0 ){
256004 rc = sqlite3Fts5IndexReinit(p->pIndex);
256005 }
256006 if( rc==SQLITE_OK0 ){
256007 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION4);
256008 }
256009 return rc;
256010}
256011
256012static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
256013 Fts5Buffer buf = {0,0,0};
256014 Fts5Config *pConfig = p->pConfig;
256015 sqlite3_stmt *pScan = 0;
256016 Fts5InsertCtx ctx;
256017 int rc, rc2;
256018
256019 memset(&ctx, 0, sizeof(Fts5InsertCtx));
256020 ctx.pStorage = p;
256021 rc = sqlite3Fts5StorageDeleteAll(p);
256022 if( rc==SQLITE_OK0 ){
256023 rc = fts5StorageLoadTotals(p, 1);
256024 }
256025
256026 if( rc==SQLITE_OK0 ){
256027 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN11, &pScan, pConfig->pzErrmsg);
256028 }
256029
256030 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pScan) ){
256031 i64 iRowid = sqlite3_column_int64(pScan, 0);
256032
256033 sqlite3Fts5BufferZero(&buf);
256034 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
256035 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
256036 ctx.szCol = 0;
256037 if( pConfig->abUnindexed[ctx.iCol]==0 ){
256038 int nText = 0; /* Size of pText in bytes */
256039 const char *pText = 0; /* Pointer to buffer containing text value */
256040 int nLoc = 0; /* Size of pLoc in bytes */
256041 const char *pLoc = 0; /* Pointer to buffer containing text value */
256042
256043 sqlite3_value *pVal = sqlite3_column_value(pScan, ctx.iCol+1);
256044 if( pConfig->eContent==FTS5_CONTENT_EXTERNAL2
256045 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
256046 ){
256047 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
256048 }else{
256049 pText = (const char*)sqlite3_value_text(pVal);
256050 nText = sqlite3_value_bytes(pVal);
256051 if( pConfig->bLocale ){
256052 int iCol = ctx.iCol + 1 + pConfig->nCol;
256053 pLoc = (const char*)sqlite3_column_text(pScan, iCol);
256054 nLoc = sqlite3_column_bytes(pScan, iCol);
256055 }
256056 }
256057
256058 if( rc==SQLITE_OK0 ){
256059 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
256060 rc = sqlite3Fts5Tokenize(pConfig,
256061 FTS5_TOKENIZE_DOCUMENT0x0004,
256062 pText, nText,
256063 (void*)&ctx,
256064 fts5StorageInsertCallback
256065 );
256066 sqlite3Fts5ClearLocale(pConfig);
256067 }
256068 }
256069 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
256070 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
256071 }
256072 p->nTotalRow++;
256073
256074 if( rc==SQLITE_OK0 ){
256075 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
256076 }
256077 }
256078 sqlite3_free(buf.p);
256079 rc2 = sqlite3_reset(pScan);
256080 if( rc==SQLITE_OK0 ) rc = rc2;
256081
256082 /* Write the averages record */
256083 if( rc==SQLITE_OK0 ){
256084 rc = fts5StorageSaveTotals(p);
256085 }
256086 return rc;
256087}
256088
256089static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
256090 return sqlite3Fts5IndexOptimize(p->pIndex);
256091}
256092
256093static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
256094 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
256095}
256096
256097static int sqlite3Fts5StorageReset(Fts5Storage *p){
256098 return sqlite3Fts5IndexReset(p->pIndex);
256099}
256100
256101/*
256102** Allocate a new rowid. This is used for "external content" tables when
256103** a NULL value is inserted into the rowid column. The new rowid is allocated
256104** by inserting a dummy row into the %_docsize table. The dummy will be
256105** overwritten later.
256106**
256107** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
256108** this case the user is required to provide a rowid explicitly.
256109*/
256110static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
256111 int rc = SQLITE_MISMATCH20;
256112 if( p->pConfig->bColumnsize ){
256113 sqlite3_stmt *pReplace = 0;
256114 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE7, &pReplace, 0);
256115 if( rc==SQLITE_OK0 ){
256116 sqlite3_bind_null(pReplace, 1);
256117 sqlite3_bind_null(pReplace, 2);
256118 sqlite3_step(pReplace);
256119 rc = sqlite3_reset(pReplace);
256120 }
256121 if( rc==SQLITE_OK0 ){
256122 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
256123 }
256124 }
256125 return rc;
256126}
256127
256128/*
256129** Insert a new row into the FTS content table.
256130*/
256131static int sqlite3Fts5StorageContentInsert(
256132 Fts5Storage *p,
256133 int bReplace, /* True to use REPLACE instead of INSERT */
256134 sqlite3_value **apVal,
256135 i64 *piRowid
256136){
256137 Fts5Config *pConfig = p->pConfig;
256138 int rc = SQLITE_OK0;
256139
256140 /* Insert the new row into the %_content table. */
256141 if( pConfig->eContent!=FTS5_CONTENT_NORMAL0
256142 && pConfig->eContent!=FTS5_CONTENT_UNINDEXED3
256143 ){
256144 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER1 ){
256145 *piRowid = sqlite3_value_int64(apVal[1]);
256146 }else{
256147 rc = fts5StorageNewRowid(p, piRowid);
256148 }
256149 }else{
256150 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
256151 int i; /* Counter variable */
256152
256153 assert( FTS5_STMT_INSERT_CONTENT+1==FTS5_STMT_REPLACE_CONTENT )((void) (0));
256154 assert( bReplace==0 || bReplace==1 )((void) (0));
256155 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT4+bReplace, &pInsert, 0);
256156 if( pInsert ) sqlite3_clear_bindings(pInsert);
256157
256158 /* Bind the rowid value */
256159 sqlite3_bind_value(pInsert, 1, apVal[1]);
256160
256161 /* Loop through values for user-defined columns. i=2 is the leftmost
256162 ** user-defined column. As is column 1 of pSavedRow. */
256163 for(i=2; rc==SQLITE_OK0 && i<=pConfig->nCol+1; i++){
256164 int bUnindexed = pConfig->abUnindexed[i-2];
256165 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 || bUnindexed ){
256166 sqlite3_value *pVal = apVal[i];
256167
256168 if( sqlite3_value_nochange(pVal) && p->pSavedRow ){
256169 /* This is an UPDATE statement, and user-defined column (i-2) was not
256170 ** modified. Retrieve the value from Fts5Storage.pSavedRow. */
256171 pVal = sqlite3_column_value(p->pSavedRow, i-1);
256172 if( pConfig->bLocale && bUnindexed==0 ){
256173 sqlite3_bind_value(pInsert, pConfig->nCol + i,
256174 sqlite3_column_value(p->pSavedRow, pConfig->nCol + i - 1)
256175 );
256176 }
256177 }else if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
256178 const char *pText = 0;
256179 const char *pLoc = 0;
256180 int nText = 0;
256181 int nLoc = 0;
256182 assert( pConfig->bLocale )((void) (0));
256183
256184 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
256185 if( rc==SQLITE_OK0 ){
256186 sqlite3_bind_text(pInsert, i, pText, nText, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
256187 if( bUnindexed==0 ){
256188 int iLoc = pConfig->nCol + i;
256189 sqlite3_bind_text(pInsert, iLoc, pLoc, nLoc, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
256190 }
256191 }
256192
256193 continue;
256194 }
256195
256196 rc = sqlite3_bind_value(pInsert, i, pVal);
256197 }
256198 }
256199 if( rc==SQLITE_OK0 ){
256200 sqlite3_step(pInsert);
256201 rc = sqlite3_reset(pInsert);
256202 }
256203 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
256204 }
256205
256206 return rc;
256207}
256208
256209/*
256210** Insert new entries into the FTS index and %_docsize table.
256211*/
256212static int sqlite3Fts5StorageIndexInsert(
256213 Fts5Storage *p,
256214 sqlite3_value **apVal,
256215 i64 iRowid
256216){
256217 Fts5Config *pConfig = p->pConfig;
256218 int rc = SQLITE_OK0; /* Return code */
256219 Fts5InsertCtx ctx; /* Tokenization callback context object */
256220 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
256221
256222 memset(&buf, 0, sizeof(Fts5Buffer));
256223 ctx.pStorage = p;
256224 rc = fts5StorageLoadTotals(p, 1);
256225
256226 if( rc==SQLITE_OK0 ){
256227 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
256228 }
256229 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
256230 ctx.szCol = 0;
256231 if( pConfig->abUnindexed[ctx.iCol]==0 ){
256232 int nText = 0; /* Size of pText in bytes */
256233 const char *pText = 0; /* Pointer to buffer containing text value */
256234 int nLoc = 0; /* Size of pText in bytes */
256235 const char *pLoc = 0; /* Pointer to buffer containing text value */
256236
256237 sqlite3_value *pVal = apVal[ctx.iCol+2];
256238 if( p->pSavedRow && sqlite3_value_nochange(pVal) ){
256239 pVal = sqlite3_column_value(p->pSavedRow, ctx.iCol+1);
256240 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 && pConfig->bLocale ){
256241 int iCol = ctx.iCol + 1 + pConfig->nCol;
256242 pLoc = (const char*)sqlite3_column_text(p->pSavedRow, iCol);
256243 nLoc = sqlite3_column_bytes(p->pSavedRow, iCol);
256244 }
256245 }else{
256246 pVal = apVal[ctx.iCol+2];
256247 }
256248
256249 if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){
256250 rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc);
256251 }else{
256252 pText = (const char*)sqlite3_value_text(pVal);
256253 nText = sqlite3_value_bytes(pVal);
256254 }
256255
256256 if( rc==SQLITE_OK0 ){
256257 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
256258 rc = sqlite3Fts5Tokenize(pConfig,
256259 FTS5_TOKENIZE_DOCUMENT0x0004, pText, nText, (void*)&ctx,
256260 fts5StorageInsertCallback
256261 );
256262 sqlite3Fts5ClearLocale(pConfig);
256263 }
256264 }
256265 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
256266 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
256267 }
256268 p->nTotalRow++;
256269
256270 /* Write the %_docsize record */
256271 if( rc==SQLITE_OK0 ){
256272 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
256273 }
256274 sqlite3_free(buf.p);
256275
256276 return rc;
256277}
256278
256279static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
256280 Fts5Config *pConfig = p->pConfig;
256281 char *zSql;
256282 int rc;
256283
256284 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
256285 pConfig->zDb, pConfig->zName, zSuffix
256286 );
256287 if( zSql==0 ){
256288 rc = SQLITE_NOMEM7;
256289 }else{
256290 sqlite3_stmt *pCnt = 0;
256291 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
256292 if( rc==SQLITE_OK0 ){
256293 if( SQLITE_ROW100==sqlite3_step(pCnt) ){
256294 *pnRow = sqlite3_column_int64(pCnt, 0);
256295 }
256296 rc = sqlite3_finalize(pCnt);
256297 }
256298 }
256299
256300 sqlite3_free(zSql);
256301 return rc;
256302}
256303
256304/*
256305** Context object used by sqlite3Fts5StorageIntegrity().
256306*/
256307typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
256308struct Fts5IntegrityCtx {
256309 i64 iRowid;
256310 int iCol;
256311 int szCol;
256312 u64 cksum;
256313 Fts5Termset *pTermset;
256314 Fts5Config *pConfig;
256315};
256316
256317
256318/*
256319** Tokenization callback used by integrity check.
256320*/
256321static int fts5StorageIntegrityCallback(
256322 void *pContext, /* Pointer to Fts5IntegrityCtx object */
256323 int tflags,
256324 const char *pToken, /* Buffer containing token */
256325 int nToken, /* Size of token in bytes */
256326 int iUnused1, /* Start offset of token */
256327 int iUnused2 /* End offset of token */
256328){
256329 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
256330 Fts5Termset *pTermset = pCtx->pTermset;
256331 int bPresent;
256332 int ii;
256333 int rc = SQLITE_OK0;
256334 int iPos;
256335 int iCol;
256336
256337 UNUSED_PARAM2(iUnused1, iUnused2)(void)(iUnused1), (void)(iUnused2);
256338 if( nToken>FTS5_MAX_TOKEN_SIZE32768 ) nToken = FTS5_MAX_TOKEN_SIZE32768;
256339
256340 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
256341 pCtx->szCol++;
256342 }
256343
256344 switch( pCtx->pConfig->eDetail ){
256345 case FTS5_DETAIL_FULL0:
256346 iPos = pCtx->szCol-1;
256347 iCol = pCtx->iCol;
256348 break;
256349
256350 case FTS5_DETAIL_COLUMNS2:
256351 iPos = pCtx->iCol;
256352 iCol = 0;
256353 break;
256354
256355 default:
256356 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
256357 iPos = 0;
256358 iCol = 0;
256359 break;
256360 }
256361
256362 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
256363 if( rc==SQLITE_OK0 && bPresent==0 ){
256364 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
256365 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
256366 );
256367 }
256368
256369 for(ii=0; rc==SQLITE_OK0 && ii<pCtx->pConfig->nPrefix; ii++){
256370 const int nChar = pCtx->pConfig->aPrefix[ii];
256371 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
256372 if( nByte ){
256373 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
256374 if( bPresent==0 ){
256375 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
256376 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
256377 );
256378 }
256379 }
256380 }
256381
256382 return rc;
256383}
256384
256385/*
256386** Check that the contents of the FTS index match that of the %_content
256387** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
256388** some other SQLite error code if an error occurs while attempting to
256389** determine this.
256390*/
256391static int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){
256392 Fts5Config *pConfig = p->pConfig;
256393 int rc = SQLITE_OK0; /* Return code */
256394 int *aColSize; /* Array of size pConfig->nCol */
256395 i64 *aTotalSize; /* Array of size pConfig->nCol */
256396 Fts5IntegrityCtx ctx;
256397 sqlite3_stmt *pScan;
256398 int bUseCksum;
256399
256400 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
256401 ctx.pConfig = p->pConfig;
256402 aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
256403 if( !aTotalSize ) return SQLITE_NOMEM7;
256404 aColSize = (int*)&aTotalSize[pConfig->nCol];
256405 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
256406
256407 bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL0
256408 || (pConfig->eContent==FTS5_CONTENT_EXTERNAL2 && iArg)
256409 );
256410 if( bUseCksum ){
256411 /* Generate the expected index checksum based on the contents of the
256412 ** %_content table. This block stores the checksum in ctx.cksum. */
256413 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN11, &pScan, 0);
256414 if( rc==SQLITE_OK0 ){
256415 int rc2;
256416 while( SQLITE_ROW100==sqlite3_step(pScan) ){
256417 int i;
256418 ctx.iRowid = sqlite3_column_int64(pScan, 0);
256419 ctx.szCol = 0;
256420 if( pConfig->bColumnsize ){
256421 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
256422 }
256423 if( rc==SQLITE_OK0 && pConfig->eDetail==FTS5_DETAIL_NONE1 ){
256424 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
256425 }
256426 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
256427 if( pConfig->abUnindexed[i]==0 ){
256428 const char *pText = 0;
256429 int nText = 0;
256430 const char *pLoc = 0;
256431 int nLoc = 0;
256432 sqlite3_value *pVal = sqlite3_column_value(pScan, i+1);
256433
256434 if( pConfig->eContent==FTS5_CONTENT_EXTERNAL2
256435 && sqlite3Fts5IsLocaleValue(pConfig, pVal)
256436 ){
256437 rc = sqlite3Fts5DecodeLocaleValue(
256438 pVal, &pText, &nText, &pLoc, &nLoc
256439 );
256440 }else{
256441 if( pConfig->eContent==FTS5_CONTENT_NORMAL0 && pConfig->bLocale ){
256442 int iCol = i + 1 + pConfig->nCol;
256443 pLoc = (const char*)sqlite3_column_text(pScan, iCol);
256444 nLoc = sqlite3_column_bytes(pScan, iCol);
256445 }
256446 pText = (const char*)sqlite3_value_text(pVal);
256447 nText = sqlite3_value_bytes(pVal);
256448 }
256449
256450 ctx.iCol = i;
256451 ctx.szCol = 0;
256452
256453 if( rc==SQLITE_OK0 && pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
256454 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
256455 }
256456
256457 if( rc==SQLITE_OK0 ){
256458 sqlite3Fts5SetLocale(pConfig, pLoc, nLoc);
256459 rc = sqlite3Fts5Tokenize(pConfig,
256460 FTS5_TOKENIZE_DOCUMENT0x0004,
256461 pText, nText,
256462 (void*)&ctx,
256463 fts5StorageIntegrityCallback
256464 );
256465 sqlite3Fts5ClearLocale(pConfig);
256466 }
256467
256468 /* If this is not a columnsize=0 database, check that the number
256469 ** of tokens in the value matches the aColSize[] value read from
256470 ** the %_docsize table. */
256471 if( rc==SQLITE_OK0
256472 && pConfig->bColumnsize
256473 && ctx.szCol!=aColSize[i]
256474 ){
256475 rc = FTS5_CORRUPT(11 | (1<<8));
256476 }
256477 aTotalSize[i] += ctx.szCol;
256478 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS2 ){
256479 sqlite3Fts5TermsetFree(ctx.pTermset);
256480 ctx.pTermset = 0;
256481 }
256482 }
256483 }
256484 sqlite3Fts5TermsetFree(ctx.pTermset);
256485 ctx.pTermset = 0;
256486
256487 if( rc!=SQLITE_OK0 ) break;
256488 }
256489 rc2 = sqlite3_reset(pScan);
256490 if( rc==SQLITE_OK0 ) rc = rc2;
256491 }
256492
256493 /* Test that the "totals" (sometimes called "averages") record looks Ok */
256494 if( rc==SQLITE_OK0 ){
256495 int i;
256496 rc = fts5StorageLoadTotals(p, 0);
256497 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
256498 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT(11 | (1<<8));
256499 }
256500 }
256501
256502 /* Check that the %_docsize and %_content tables contain the expected
256503 ** number of rows. */
256504 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL0 ){
256505 i64 nRow = 0;
256506 rc = fts5StorageCount(p, "content", &nRow);
256507 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT(11 | (1<<8));
256508 }
256509 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
256510 i64 nRow = 0;
256511 rc = fts5StorageCount(p, "docsize", &nRow);
256512 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT(11 | (1<<8));
256513 }
256514 }
256515
256516 /* Pass the expected checksum down to the FTS index module. It will
256517 ** verify, amongst other things, that it matches the checksum generated by
256518 ** inspecting the index itself. */
256519 if( rc==SQLITE_OK0 ){
256520 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum, bUseCksum);
256521 }
256522
256523 sqlite3_free(aTotalSize);
256524 return rc;
256525}
256526
256527/*
256528** Obtain an SQLite statement handle that may be used to read data from the
256529** %_content table.
256530*/
256531static int sqlite3Fts5StorageStmt(
256532 Fts5Storage *p,
256533 int eStmt,
256534 sqlite3_stmt **pp,
256535 char **pzErrMsg
256536){
256537 int rc;
256538 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
256539 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
256540 || eStmt==FTS5_STMT_LOOKUP((void) (0))
256541 )((void) (0));
256542 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
256543 if( rc==SQLITE_OK0 ){
256544 assert( p->aStmt[eStmt]==*pp )((void) (0));
256545 p->aStmt[eStmt] = 0;
256546 }
256547 return rc;
256548}
256549
256550/*
256551** Release an SQLite statement handle obtained via an earlier call to
256552** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
256553** must match that passed to the sqlite3Fts5StorageStmt() call.
256554*/
256555static void sqlite3Fts5StorageStmtRelease(
256556 Fts5Storage *p,
256557 int eStmt,
256558 sqlite3_stmt *pStmt
256559){
256560 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
256561 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
256562 || eStmt==FTS5_STMT_LOOKUP((void) (0))
256563 )((void) (0));
256564 if( p->aStmt[eStmt]==0 ){
256565 sqlite3_reset(pStmt);
256566 p->aStmt[eStmt] = pStmt;
256567 }else{
256568 sqlite3_finalize(pStmt);
256569 }
256570}
256571
256572static int fts5StorageDecodeSizeArray(
256573 int *aCol, int nCol, /* Array to populate */
256574 const u8 *aBlob, int nBlob /* Record to read varints from */
256575){
256576 int i;
256577 int iOff = 0;
256578 for(i=0; i<nCol; i++){
256579 if( iOff>=nBlob ) return 1;
256580 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i])sqlite3Fts5GetVarint32(&aBlob[iOff],(u32*)&(aCol[i]));
256581 }
256582 return (iOff!=nBlob);
256583}
256584
256585/*
256586** Argument aCol points to an array of integers containing one entry for
256587** each table column. This function reads the %_docsize record for the
256588** specified rowid and populates aCol[] with the results.
256589**
256590** An SQLite error code is returned if an error occurs, or SQLITE_OK
256591** otherwise.
256592*/
256593static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
256594 int nCol = p->pConfig->nCol; /* Number of user columns in table */
256595 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
256596 int rc; /* Return Code */
256597
256598 assert( p->pConfig->bColumnsize )((void) (0));
256599 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE9, &pLookup, 0);
256600 if( pLookup ){
256601 int bCorrupt = 1;
256602 assert( rc==SQLITE_OK )((void) (0));
256603 sqlite3_bind_int64(pLookup, 1, iRowid);
256604 if( SQLITE_ROW100==sqlite3_step(pLookup) ){
256605 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
256606 int nBlob = sqlite3_column_bytes(pLookup, 0);
256607 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
256608 bCorrupt = 0;
256609 }
256610 }
256611 rc = sqlite3_reset(pLookup);
256612 if( bCorrupt && rc==SQLITE_OK0 ){
256613 rc = FTS5_CORRUPT(11 | (1<<8));
256614 }
256615 }else{
256616 assert( rc!=SQLITE_OK )((void) (0));
256617 }
256618
256619 return rc;
256620}
256621
256622static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
256623 int rc = fts5StorageLoadTotals(p, 0);
256624 if( rc==SQLITE_OK0 ){
256625 *pnToken = 0;
256626 if( iCol<0 ){
256627 int i;
256628 for(i=0; i<p->pConfig->nCol; i++){
256629 *pnToken += p->aTotalSize[i];
256630 }
256631 }else if( iCol<p->pConfig->nCol ){
256632 *pnToken = p->aTotalSize[iCol];
256633 }else{
256634 rc = SQLITE_RANGE25;
256635 }
256636 }
256637 return rc;
256638}
256639
256640static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
256641 int rc = fts5StorageLoadTotals(p, 0);
256642 if( rc==SQLITE_OK0 ){
256643 /* nTotalRow being zero does not necessarily indicate a corrupt
256644 ** database - it might be that the FTS5 table really does contain zero
256645 ** rows. However this function is only called from the xRowCount() API,
256646 ** and there is no way for that API to be invoked if the table contains
256647 ** no rows. Hence the FTS5_CORRUPT return. */
256648 *pnRow = p->nTotalRow;
256649 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT(11 | (1<<8));
256650 }
256651 return rc;
256652}
256653
256654/*
256655** Flush any data currently held in-memory to disk.
256656*/
256657static int sqlite3Fts5StorageSync(Fts5Storage *p){
256658 int rc = SQLITE_OK0;
256659 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
256660 if( p->bTotalsValid ){
256661 rc = fts5StorageSaveTotals(p);
256662 if( rc==SQLITE_OK0 ){
256663 p->bTotalsValid = 0;
256664 }
256665 }
256666 if( rc==SQLITE_OK0 ){
256667 rc = sqlite3Fts5IndexSync(p->pIndex);
256668 }
256669 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
256670 return rc;
256671}
256672
256673static int sqlite3Fts5StorageRollback(Fts5Storage *p){
256674 p->bTotalsValid = 0;
256675 return sqlite3Fts5IndexRollback(p->pIndex);
256676}
256677
256678static int sqlite3Fts5StorageConfigValue(
256679 Fts5Storage *p,
256680 const char *z,
256681 sqlite3_value *pVal,
256682 int iVal
256683){
256684 sqlite3_stmt *pReplace = 0;
256685 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG10, &pReplace, 0);
256686 if( rc==SQLITE_OK0 ){
256687 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
256688 if( pVal ){
256689 sqlite3_bind_value(pReplace, 2, pVal);
256690 }else{
256691 sqlite3_bind_int(pReplace, 2, iVal);
256692 }
256693 sqlite3_step(pReplace);
256694 rc = sqlite3_reset(pReplace);
256695 sqlite3_bind_null(pReplace, 1);
256696 }
256697 if( rc==SQLITE_OK0 && pVal ){
256698 int iNew = p->pConfig->iCookie + 1;
256699 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
256700 if( rc==SQLITE_OK0 ){
256701 p->pConfig->iCookie = iNew;
256702 }
256703 }
256704 return rc;
256705}
256706
256707/*
256708** 2014 May 31
256709**
256710** The author disclaims copyright to this source code. In place of
256711** a legal notice, here is a blessing:
256712**
256713** May you do good and not evil.
256714** May you find forgiveness for yourself and forgive others.
256715** May you share freely, never taking more than you give.
256716**
256717******************************************************************************
256718*/
256719
256720
256721/* #include "fts5Int.h" */
256722
256723/**************************************************************************
256724** Start of ascii tokenizer implementation.
256725*/
256726
256727/*
256728** For tokenizers with no "unicode" modifier, the set of token characters
256729** is the same as the set of ASCII range alphanumeric characters.
256730*/
256731static unsigned char aAsciiTokenChar[128] = {
256732 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
256733 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
256734 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
256735 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
256736 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
256737 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
256738 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
256739 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
256740};
256741
256742typedef struct AsciiTokenizer AsciiTokenizer;
256743struct AsciiTokenizer {
256744 unsigned char aTokenChar[128];
256745};
256746
256747static void fts5AsciiAddExceptions(
256748 AsciiTokenizer *p,
256749 const char *zArg,
256750 int bTokenChars
256751){
256752 int i;
256753 for(i=0; zArg[i]; i++){
256754 if( (zArg[i] & 0x80)==0 ){
256755 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
256756 }
256757 }
256758}
256759
256760/*
256761** Delete a "ascii" tokenizer.
256762*/
256763static void fts5AsciiDelete(Fts5Tokenizer *p){
256764 sqlite3_free(p);
256765}
256766
256767/*
256768** Create an "ascii" tokenizer.
256769*/
256770static int fts5AsciiCreate(
256771 void *pUnused,
256772 const char **azArg, int nArg,
256773 Fts5Tokenizer **ppOut
256774){
256775 int rc = SQLITE_OK0;
256776 AsciiTokenizer *p = 0;
256777 UNUSED_PARAM(pUnused)(void)(pUnused);
256778 if( nArg%2 ){
256779 rc = SQLITE_ERROR1;
256780 }else{
256781 p = sqlite3_malloc(sizeof(AsciiTokenizer));
256782 if( p==0 ){
256783 rc = SQLITE_NOMEM7;
256784 }else{
256785 int i;
256786 memset(p, 0, sizeof(AsciiTokenizer));
256787 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
256788 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
256789 const char *zArg = azArg[i+1];
256790 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
256791 fts5AsciiAddExceptions(p, zArg, 1);
256792 }else
256793 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
256794 fts5AsciiAddExceptions(p, zArg, 0);
256795 }else{
256796 rc = SQLITE_ERROR1;
256797 }
256798 }
256799 if( rc!=SQLITE_OK0 ){
256800 fts5AsciiDelete((Fts5Tokenizer*)p);
256801 p = 0;
256802 }
256803 }
256804 }
256805
256806 *ppOut = (Fts5Tokenizer*)p;
256807 return rc;
256808}
256809
256810
256811static void asciiFold(char *aOut, const char *aIn, int nByte){
256812 int i;
256813 for(i=0; i<nByte; i++){
256814 char c = aIn[i];
256815 if( c>='A' && c<='Z' ) c += 32;
256816 aOut[i] = c;
256817 }
256818}
256819
256820/*
256821** Tokenize some text using the ascii tokenizer.
256822*/
256823static int fts5AsciiTokenize(
256824 Fts5Tokenizer *pTokenizer,
256825 void *pCtx,
256826 int iUnused,
256827 const char *pText, int nText,
256828 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
256829){
256830 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
256831 int rc = SQLITE_OK0;
256832 int ie;
256833 int is = 0;
256834
256835 char aFold[64];
256836 int nFold = sizeof(aFold);
256837 char *pFold = aFold;
256838 unsigned char *a = p->aTokenChar;
256839
256840 UNUSED_PARAM(iUnused)(void)(iUnused);
256841
256842 while( is<nText && rc==SQLITE_OK0 ){
256843 int nByte;
256844
256845 /* Skip any leading divider characters. */
256846 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
256847 is++;
256848 }
256849 if( is==nText ) break;
256850
256851 /* Count the token characters */
256852 ie = is+1;
256853 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
256854 ie++;
256855 }
256856
256857 /* Fold to lower case */
256858 nByte = ie-is;
256859 if( nByte>nFold ){
256860 if( pFold!=aFold ) sqlite3_free(pFold);
256861 pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
256862 if( pFold==0 ){
256863 rc = SQLITE_NOMEM7;
256864 break;
256865 }
256866 nFold = nByte*2;
256867 }
256868 asciiFold(pFold, &pText[is], nByte);
256869
256870 /* Invoke the token callback */
256871 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
256872 is = ie+1;
256873 }
256874
256875 if( pFold!=aFold ) sqlite3_free(pFold);
256876 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
256877 return rc;
256878}
256879
256880/**************************************************************************
256881** Start of unicode61 tokenizer implementation.
256882*/
256883
256884
256885/*
256886** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
256887** from the sqlite3 source file utf.c. If this file is compiled as part
256888** of the amalgamation, they are not required.
256889*/
256890#ifndef SQLITE_AMALGAMATION1
256891
256892static const unsigned char sqlite3Utf8Trans1[] = {
256893 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
256894 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
256895 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
256896 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
256897 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
256898 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
256899 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
256900 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
256901};
256902
256903#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; } }
\
256904 c = *(zIn++); \
256905 if( c>=0xc0 ){ \
256906 c = sqlite3Utf8Trans1[c-0xc0]; \
256907 while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \
256908 c = (c<<6) + (0x3f & *(zIn++)); \
256909 } \
256910 if( c<0x80 \
256911 || (c&0xFFFFF800)==0xD800 \
256912 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
256913 }
256914
256915
256916#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); } }
{ \
256917 if( c<0x00080 ){ \
256918 *zOut++ = (unsigned char)(c&0xFF); \
256919 } \
256920 else if( c<0x00800 ){ \
256921 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
256922 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
256923 } \
256924 else if( c<0x10000 ){ \
256925 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
256926 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
256927 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
256928 }else{ \
256929 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
256930 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
256931 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
256932 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
256933 } \
256934}
256935
256936#endif /* ifndef SQLITE_AMALGAMATION */
256937
256938#define FTS5_SKIP_UTF8(zIn){ if( ((unsigned char)(*(zIn++)))>=0xc0 ){ while( (((unsigned
char)*zIn) & 0xc0)==0x80 ){ zIn++; } } }
{ \
256939 if( ((unsigned char)(*(zIn++)))>=0xc0 ){ \
256940 while( (((unsigned char)*zIn) & 0xc0)==0x80 ){ zIn++; } \
256941 } \
256942}
256943
256944typedef struct Unicode61Tokenizer Unicode61Tokenizer;
256945struct Unicode61Tokenizer {
256946 unsigned char aTokenChar[128]; /* ASCII range token characters */
256947 char *aFold; /* Buffer to fold text into */
256948 int nFold; /* Size of aFold[] in bytes */
256949 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
256950 int nException;
256951 int *aiException;
256952
256953 unsigned char aCategory[32]; /* True for token char categories */
256954};
256955
256956/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
256957#define FTS5_REMOVE_DIACRITICS_NONE0 0
256958#define FTS5_REMOVE_DIACRITICS_SIMPLE1 1
256959#define FTS5_REMOVE_DIACRITICS_COMPLEX2 2
256960
256961static int fts5UnicodeAddExceptions(
256962 Unicode61Tokenizer *p, /* Tokenizer object */
256963 const char *z, /* Characters to treat as exceptions */
256964 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
256965){
256966 int rc = SQLITE_OK0;
256967 int n = (int)strlen(z);
256968 int *aNew;
256969
256970 if( n>0 ){
256971 aNew = (int*)sqlite3_realloc64(p->aiException,
256972 (n+p->nException)*sizeof(int));
256973 if( aNew ){
256974 int nNew = p->nException;
256975 const unsigned char *zCsr = (const unsigned char*)z;
256976 const unsigned char *zTerm = (const unsigned char*)&z[n];
256977 while( zCsr<zTerm ){
256978 u32 iCode;
256979 int bToken;
256980 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; } }
;
256981 if( iCode<128 ){
256982 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
256983 }else{
256984 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
256985 assert( (bToken==0 || bToken==1) )((void) (0));
256986 assert( (bTokenChars==0 || bTokenChars==1) )((void) (0));
256987 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
256988 int i;
256989 for(i=0; i<nNew; i++){
256990 if( (u32)aNew[i]>iCode ) break;
256991 }
256992 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
256993 aNew[i] = iCode;
256994 nNew++;
256995 }
256996 }
256997 }
256998 p->aiException = aNew;
256999 p->nException = nNew;
257000 }else{
257001 rc = SQLITE_NOMEM7;
257002 }
257003 }
257004
257005 return rc;
257006}
257007
257008/*
257009** Return true if the p->aiException[] array contains the value iCode.
257010*/
257011static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
257012 if( p->nException>0 ){
257013 int *a = p->aiException;
257014 int iLo = 0;
257015 int iHi = p->nException-1;
257016
257017 while( iHi>=iLo ){
257018 int iTest = (iHi + iLo) / 2;
257019 if( iCode==a[iTest] ){
257020 return 1;
257021 }else if( iCode>a[iTest] ){
257022 iLo = iTest+1;
257023 }else{
257024 iHi = iTest-1;
257025 }
257026 }
257027 }
257028
257029 return 0;
257030}
257031
257032/*
257033** Delete a "unicode61" tokenizer.
257034*/
257035static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
257036 if( pTok ){
257037 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
257038 sqlite3_free(p->aiException);
257039 sqlite3_free(p->aFold);
257040 sqlite3_free(p);
257041 }
257042 return;
257043}
257044
257045static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
257046 const char *z = zCat;
257047
257048 while( *z ){
257049 while( *z==' ' || *z=='\t' ) z++;
257050 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
257051 return SQLITE_ERROR1;
257052 }
257053 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
257054 }
257055
257056 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
257057 return SQLITE_OK0;
257058}
257059
257060/*
257061** Create a "unicode61" tokenizer.
257062*/
257063static int fts5UnicodeCreate(
257064 void *pUnused,
257065 const char **azArg, int nArg,
257066 Fts5Tokenizer **ppOut
257067){
257068 int rc = SQLITE_OK0; /* Return code */
257069 Unicode61Tokenizer *p = 0; /* New tokenizer object */
257070
257071 UNUSED_PARAM(pUnused)(void)(pUnused);
257072
257073 if( nArg%2 ){
257074 rc = SQLITE_ERROR1;
257075 }else{
257076 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
257077 if( p ){
257078 const char *zCat = "L* N* Co";
257079 int i;
257080 memset(p, 0, sizeof(Unicode61Tokenizer));
257081
257082 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE1;
257083 p->nFold = 64;
257084 p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
257085 if( p->aFold==0 ){
257086 rc = SQLITE_NOMEM7;
257087 }
257088
257089 /* Search for a "categories" argument */
257090 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
257091 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
257092 zCat = azArg[i+1];
257093 }
257094 }
257095 if( rc==SQLITE_OK0 ){
257096 rc = unicodeSetCategories(p, zCat);
257097 }
257098
257099 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
257100 const char *zArg = azArg[i+1];
257101 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
257102 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
257103 rc = SQLITE_ERROR1;
257104 }else{
257105 p->eRemoveDiacritic = (zArg[0] - '0');
257106 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE((void) (0))
257107 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE((void) (0))
257108 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX((void) (0))
257109 )((void) (0));
257110 }
257111 }else
257112 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
257113 rc = fts5UnicodeAddExceptions(p, zArg, 1);
257114 }else
257115 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
257116 rc = fts5UnicodeAddExceptions(p, zArg, 0);
257117 }else
257118 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
257119 /* no-op */
257120 }else{
257121 rc = SQLITE_ERROR1;
257122 }
257123 }
257124 }else{
257125 rc = SQLITE_NOMEM7;
257126 }
257127 if( rc!=SQLITE_OK0 ){
257128 fts5UnicodeDelete((Fts5Tokenizer*)p);
257129 p = 0;
257130 }
257131 *ppOut = (Fts5Tokenizer*)p;
257132 }
257133 return rc;
257134}
257135
257136/*
257137** Return true if, for the purposes of tokenizing with the tokenizer
257138** passed as the first argument, codepoint iCode is considered a token
257139** character (not a separator).
257140*/
257141static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
257142 return (
257143 p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
257144 ^ fts5UnicodeIsException(p, iCode)
257145 );
257146}
257147
257148static int fts5UnicodeTokenize(
257149 Fts5Tokenizer *pTokenizer,
257150 void *pCtx,
257151 int iUnused,
257152 const char *pText, int nText,
257153 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
257154){
257155 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
257156 int rc = SQLITE_OK0;
257157 unsigned char *a = p->aTokenChar;
257158
257159 unsigned char *zTerm = (unsigned char*)&pText[nText];
257160 unsigned char *zCsr = (unsigned char *)pText;
257161
257162 /* Output buffer */
257163 char *aFold = p->aFold;
257164 int nFold = p->nFold;
257165 const char *pEnd = &aFold[nFold-6];
257166
257167 UNUSED_PARAM(iUnused)(void)(iUnused);
257168
257169 /* Each iteration of this loop gobbles up a contiguous run of separators,
257170 ** then the next token. */
257171 while( rc==SQLITE_OK0 ){
257172 u32 iCode; /* non-ASCII codepoint read from input */
257173 char *zOut = aFold;
257174 int is;
257175 int ie;
257176
257177 /* Skip any separator characters. */
257178 while( 1 ){
257179 if( zCsr>=zTerm ) goto tokenize_done;
257180 if( *zCsr & 0x80 ) {
257181 /* A character outside of the ascii range. Skip past it if it is
257182 ** a separator character. Or break out of the loop if it is not. */
257183 is = zCsr - (unsigned char*)pText;
257184 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; } }
;
257185 if( fts5UnicodeIsAlnum(p, iCode) ){
257186 goto non_ascii_tokenchar;
257187 }
257188 }else{
257189 if( a[*zCsr] ){
257190 is = zCsr - (unsigned char*)pText;
257191 goto ascii_tokenchar;
257192 }
257193 zCsr++;
257194 }
257195 }
257196
257197 /* Run through the tokenchars. Fold them into the output buffer along
257198 ** the way. */
257199 while( zCsr<zTerm ){
257200
257201 /* Grow the output buffer so that there is sufficient space to fit the
257202 ** largest possible utf-8 character. */
257203 if( zOut>pEnd ){
257204 aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
257205 if( aFold==0 ){
257206 rc = SQLITE_NOMEM7;
257207 goto tokenize_done;
257208 }
257209 zOut = &aFold[zOut - p->aFold];
257210 memcpy(aFold, p->aFold, nFold);
257211 sqlite3_free(p->aFold);
257212 p->aFold = aFold;
257213 p->nFold = nFold = nFold*2;
257214 pEnd = &aFold[nFold-6];
257215 }
257216
257217 if( *zCsr & 0x80 ){
257218 /* An non-ascii-range character. Fold it into the output buffer if
257219 ** it is a token character, or break out of the loop if it is not. */
257220 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; } }
;
257221 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
257222 non_ascii_tokenchar:
257223 iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
257224 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); } }
;
257225 }else{
257226 break;
257227 }
257228 }else if( a[*zCsr]==0 ){
257229 /* An ascii-range separator character. End of token. */
257230 break;
257231 }else{
257232 ascii_tokenchar:
257233 if( *zCsr>='A' && *zCsr<='Z' ){
257234 *zOut++ = *zCsr + 32;
257235 }else{
257236 *zOut++ = *zCsr;
257237 }
257238 zCsr++;
257239 }
257240 ie = zCsr - (unsigned char*)pText;
257241 }
257242
257243 /* Invoke the token callback */
257244 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
257245 }
257246
257247 tokenize_done:
257248 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
257249 return rc;
257250}
257251
257252/**************************************************************************
257253** Start of porter stemmer implementation.
257254*/
257255
257256/* Any tokens larger than this (in bytes) are passed through without
257257** stemming. */
257258#define FTS5_PORTER_MAX_TOKEN64 64
257259
257260typedef struct PorterTokenizer PorterTokenizer;
257261struct PorterTokenizer {
257262 fts5_tokenizer_v2 tokenizer_v2; /* Parent tokenizer module */
257263 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
257264 char aBuf[FTS5_PORTER_MAX_TOKEN64 + 64];
257265};
257266
257267/*
257268** Delete a "porter" tokenizer.
257269*/
257270static void fts5PorterDelete(Fts5Tokenizer *pTok){
257271 if( pTok ){
257272 PorterTokenizer *p = (PorterTokenizer*)pTok;
257273 if( p->pTokenizer ){
257274 p->tokenizer_v2.xDelete(p->pTokenizer);
257275 }
257276 sqlite3_free(p);
257277 }
257278}
257279
257280/*
257281** Create a "porter" tokenizer.
257282*/
257283static int fts5PorterCreate(
257284 void *pCtx,
257285 const char **azArg, int nArg,
257286 Fts5Tokenizer **ppOut
257287){
257288 fts5_api *pApi = (fts5_api*)pCtx;
257289 int rc = SQLITE_OK0;
257290 PorterTokenizer *pRet;
257291 void *pUserdata = 0;
257292 const char *zBase = "unicode61";
257293 fts5_tokenizer_v2 *pV2 = 0;
257294
257295 if( nArg>0 ){
257296 zBase = azArg[0];
257297 }
257298
257299 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
257300 if( pRet ){
257301 memset(pRet, 0, sizeof(PorterTokenizer));
257302 rc = pApi->xFindTokenizer_v2(pApi, zBase, &pUserdata, &pV2);
257303 }else{
257304 rc = SQLITE_NOMEM7;
257305 }
257306 if( rc==SQLITE_OK0 ){
257307 int nArg2 = (nArg>0 ? nArg-1 : 0);
257308 const char **az2 = (nArg2 ? &azArg[1] : 0);
257309 memcpy(&pRet->tokenizer_v2, pV2, sizeof(fts5_tokenizer_v2));
257310 rc = pRet->tokenizer_v2.xCreate(pUserdata, az2, nArg2, &pRet->pTokenizer);
257311 }
257312
257313 if( rc!=SQLITE_OK0 ){
257314 fts5PorterDelete((Fts5Tokenizer*)pRet);
257315 pRet = 0;
257316 }
257317 *ppOut = (Fts5Tokenizer*)pRet;
257318 return rc;
257319}
257320
257321typedef struct PorterContext PorterContext;
257322struct PorterContext {
257323 void *pCtx;
257324 int (*xToken)(void*, int, const char*, int, int, int);
257325 char *aBuf;
257326};
257327
257328typedef struct PorterRule PorterRule;
257329struct PorterRule {
257330 const char *zSuffix;
257331 int nSuffix;
257332 int (*xCond)(char *zStem, int nStem);
257333 const char *zOutput;
257334 int nOutput;
257335};
257336
257337#if 0
257338static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
257339 int ret = -1;
257340 int nBuf = *pnBuf;
257341 PorterRule *p;
257342
257343 for(p=aRule; p->zSuffix; p++){
257344 assert( strlen(p->zSuffix)==p->nSuffix )((void) (0));
257345 assert( strlen(p->zOutput)==p->nOutput )((void) (0));
257346 if( nBuf<p->nSuffix ) continue;
257347 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
257348 }
257349
257350 if( p->zSuffix ){
257351 int nStem = nBuf - p->nSuffix;
257352 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
257353 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
257354 *pnBuf = nStem + p->nOutput;
257355 ret = p - aRule;
257356 }
257357 }
257358
257359 return ret;
257360}
257361#endif
257362
257363static int fts5PorterIsVowel(char c, int bYIsVowel){
257364 return (
257365 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
257366 );
257367}
257368
257369static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
257370 int i;
257371 int bCons = bPrevCons;
257372
257373 /* Scan for a vowel */
257374 for(i=0; i<nStem; i++){
257375 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
257376 }
257377
257378 /* Scan for a consonent */
257379 for(i++; i<nStem; i++){
257380 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
257381 }
257382 return 0;
257383}
257384
257385/* porter rule condition: (m > 0) */
257386static int fts5Porter_MGt0(char *zStem, int nStem){
257387 return !!fts5PorterGobbleVC(zStem, nStem, 0);
257388}
257389
257390/* porter rule condition: (m > 1) */
257391static int fts5Porter_MGt1(char *zStem, int nStem){
257392 int n;
257393 n = fts5PorterGobbleVC(zStem, nStem, 0);
257394 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
257395 return 1;
257396 }
257397 return 0;
257398}
257399
257400/* porter rule condition: (m = 1) */
257401static int fts5Porter_MEq1(char *zStem, int nStem){
257402 int n;
257403 n = fts5PorterGobbleVC(zStem, nStem, 0);
257404 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
257405 return 1;
257406 }
257407 return 0;
257408}
257409
257410/* porter rule condition: (*o) */
257411static int fts5Porter_Ostar(char *zStem, int nStem){
257412 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
257413 return 0;
257414 }else{
257415 int i;
257416 int mask = 0;
257417 int bCons = 0;
257418 for(i=0; i<nStem; i++){
257419 bCons = !fts5PorterIsVowel(zStem[i], bCons);
257420 assert( bCons==0 || bCons==1 )((void) (0));
257421 mask = (mask << 1) + bCons;
257422 }
257423 return ((mask & 0x0007)==0x0005);
257424 }
257425}
257426
257427/* porter rule condition: (m > 1 and (*S or *T)) */
257428static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
257429 assert( nStem>0 )((void) (0));
257430 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
257431 && fts5Porter_MGt1(zStem, nStem);
257432}
257433
257434/* porter rule condition: (*v*) */
257435static int fts5Porter_Vowel(char *zStem, int nStem){
257436 int i;
257437 for(i=0; i<nStem; i++){
257438 if( fts5PorterIsVowel(zStem[i], i>0) ){
257439 return 1;
257440 }
257441 }
257442 return 0;
257443}
257444
257445
257446/**************************************************************************
257447***************************************************************************
257448** GENERATED CODE STARTS HERE (mkportersteps.tcl)
257449*/
257450
257451static int fts5PorterStep4(char *aBuf, int *pnBuf){
257452 int ret = 0;
257453 int nBuf = *pnBuf;
257454 switch( aBuf[nBuf-2] ){
257455
257456 case 'a':
257457 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
257458 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
257459 *pnBuf = nBuf - 2;
257460 }
257461 }
257462 break;
257463
257464 case 'c':
257465 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
257466 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
257467 *pnBuf = nBuf - 4;
257468 }
257469 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
257470 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
257471 *pnBuf = nBuf - 4;
257472 }
257473 }
257474 break;
257475
257476 case 'e':
257477 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
257478 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
257479 *pnBuf = nBuf - 2;
257480 }
257481 }
257482 break;
257483
257484 case 'i':
257485 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
257486 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
257487 *pnBuf = nBuf - 2;
257488 }
257489 }
257490 break;
257491
257492 case 'l':
257493 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
257494 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
257495 *pnBuf = nBuf - 4;
257496 }
257497 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
257498 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
257499 *pnBuf = nBuf - 4;
257500 }
257501 }
257502 break;
257503
257504 case 'n':
257505 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
257506 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257507 *pnBuf = nBuf - 3;
257508 }
257509 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
257510 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
257511 *pnBuf = nBuf - 5;
257512 }
257513 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
257514 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
257515 *pnBuf = nBuf - 4;
257516 }
257517 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
257518 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257519 *pnBuf = nBuf - 3;
257520 }
257521 }
257522 break;
257523
257524 case 'o':
257525 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
257526 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
257527 *pnBuf = nBuf - 3;
257528 }
257529 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
257530 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
257531 *pnBuf = nBuf - 2;
257532 }
257533 }
257534 break;
257535
257536 case 's':
257537 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
257538 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257539 *pnBuf = nBuf - 3;
257540 }
257541 }
257542 break;
257543
257544 case 't':
257545 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
257546 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257547 *pnBuf = nBuf - 3;
257548 }
257549 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
257550 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257551 *pnBuf = nBuf - 3;
257552 }
257553 }
257554 break;
257555
257556 case 'u':
257557 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
257558 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257559 *pnBuf = nBuf - 3;
257560 }
257561 }
257562 break;
257563
257564 case 'v':
257565 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
257566 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257567 *pnBuf = nBuf - 3;
257568 }
257569 }
257570 break;
257571
257572 case 'z':
257573 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
257574 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
257575 *pnBuf = nBuf - 3;
257576 }
257577 }
257578 break;
257579
257580 }
257581 return ret;
257582}
257583
257584
257585static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
257586 int ret = 0;
257587 int nBuf = *pnBuf;
257588 switch( aBuf[nBuf-2] ){
257589
257590 case 'a':
257591 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
257592 memcpy(&aBuf[nBuf-2], "ate", 3);
257593 *pnBuf = nBuf - 2 + 3;
257594 ret = 1;
257595 }
257596 break;
257597
257598 case 'b':
257599 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
257600 memcpy(&aBuf[nBuf-2], "ble", 3);
257601 *pnBuf = nBuf - 2 + 3;
257602 ret = 1;
257603 }
257604 break;
257605
257606 case 'i':
257607 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
257608 memcpy(&aBuf[nBuf-2], "ize", 3);
257609 *pnBuf = nBuf - 2 + 3;
257610 ret = 1;
257611 }
257612 break;
257613
257614 }
257615 return ret;
257616}
257617
257618
257619static int fts5PorterStep2(char *aBuf, int *pnBuf){
257620 int ret = 0;
257621 int nBuf = *pnBuf;
257622 switch( aBuf[nBuf-2] ){
257623
257624 case 'a':
257625 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
257626 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
257627 memcpy(&aBuf[nBuf-7], "ate", 3);
257628 *pnBuf = nBuf - 7 + 3;
257629 }
257630 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
257631 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
257632 memcpy(&aBuf[nBuf-6], "tion", 4);
257633 *pnBuf = nBuf - 6 + 4;
257634 }
257635 }
257636 break;
257637
257638 case 'c':
257639 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
257640 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257641 memcpy(&aBuf[nBuf-4], "ence", 4);
257642 *pnBuf = nBuf - 4 + 4;
257643 }
257644 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
257645 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257646 memcpy(&aBuf[nBuf-4], "ance", 4);
257647 *pnBuf = nBuf - 4 + 4;
257648 }
257649 }
257650 break;
257651
257652 case 'e':
257653 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
257654 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257655 memcpy(&aBuf[nBuf-4], "ize", 3);
257656 *pnBuf = nBuf - 4 + 3;
257657 }
257658 }
257659 break;
257660
257661 case 'g':
257662 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
257663 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257664 memcpy(&aBuf[nBuf-4], "log", 3);
257665 *pnBuf = nBuf - 4 + 3;
257666 }
257667 }
257668 break;
257669
257670 case 'l':
257671 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
257672 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
257673 memcpy(&aBuf[nBuf-3], "ble", 3);
257674 *pnBuf = nBuf - 3 + 3;
257675 }
257676 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
257677 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257678 memcpy(&aBuf[nBuf-4], "al", 2);
257679 *pnBuf = nBuf - 4 + 2;
257680 }
257681 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
257682 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257683 memcpy(&aBuf[nBuf-5], "ent", 3);
257684 *pnBuf = nBuf - 5 + 3;
257685 }
257686 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
257687 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
257688 memcpy(&aBuf[nBuf-3], "e", 1);
257689 *pnBuf = nBuf - 3 + 1;
257690 }
257691 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
257692 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257693 memcpy(&aBuf[nBuf-5], "ous", 3);
257694 *pnBuf = nBuf - 5 + 3;
257695 }
257696 }
257697 break;
257698
257699 case 'o':
257700 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
257701 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
257702 memcpy(&aBuf[nBuf-7], "ize", 3);
257703 *pnBuf = nBuf - 7 + 3;
257704 }
257705 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
257706 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257707 memcpy(&aBuf[nBuf-5], "ate", 3);
257708 *pnBuf = nBuf - 5 + 3;
257709 }
257710 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
257711 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257712 memcpy(&aBuf[nBuf-4], "ate", 3);
257713 *pnBuf = nBuf - 4 + 3;
257714 }
257715 }
257716 break;
257717
257718 case 's':
257719 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
257720 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257721 memcpy(&aBuf[nBuf-5], "al", 2);
257722 *pnBuf = nBuf - 5 + 2;
257723 }
257724 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
257725 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
257726 memcpy(&aBuf[nBuf-7], "ive", 3);
257727 *pnBuf = nBuf - 7 + 3;
257728 }
257729 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
257730 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
257731 memcpy(&aBuf[nBuf-7], "ful", 3);
257732 *pnBuf = nBuf - 7 + 3;
257733 }
257734 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
257735 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
257736 memcpy(&aBuf[nBuf-7], "ous", 3);
257737 *pnBuf = nBuf - 7 + 3;
257738 }
257739 }
257740 break;
257741
257742 case 't':
257743 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
257744 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257745 memcpy(&aBuf[nBuf-5], "al", 2);
257746 *pnBuf = nBuf - 5 + 2;
257747 }
257748 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
257749 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257750 memcpy(&aBuf[nBuf-5], "ive", 3);
257751 *pnBuf = nBuf - 5 + 3;
257752 }
257753 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
257754 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
257755 memcpy(&aBuf[nBuf-6], "ble", 3);
257756 *pnBuf = nBuf - 6 + 3;
257757 }
257758 }
257759 break;
257760
257761 }
257762 return ret;
257763}
257764
257765
257766static int fts5PorterStep3(char *aBuf, int *pnBuf){
257767 int ret = 0;
257768 int nBuf = *pnBuf;
257769 switch( aBuf[nBuf-2] ){
257770
257771 case 'a':
257772 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
257773 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257774 memcpy(&aBuf[nBuf-4], "ic", 2);
257775 *pnBuf = nBuf - 4 + 2;
257776 }
257777 }
257778 break;
257779
257780 case 's':
257781 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
257782 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
257783 *pnBuf = nBuf - 4;
257784 }
257785 }
257786 break;
257787
257788 case 't':
257789 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
257790 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257791 memcpy(&aBuf[nBuf-5], "ic", 2);
257792 *pnBuf = nBuf - 5 + 2;
257793 }
257794 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
257795 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257796 memcpy(&aBuf[nBuf-5], "ic", 2);
257797 *pnBuf = nBuf - 5 + 2;
257798 }
257799 }
257800 break;
257801
257802 case 'u':
257803 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
257804 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
257805 *pnBuf = nBuf - 3;
257806 }
257807 }
257808 break;
257809
257810 case 'v':
257811 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
257812 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257813 *pnBuf = nBuf - 5;
257814 }
257815 }
257816 break;
257817
257818 case 'z':
257819 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
257820 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
257821 memcpy(&aBuf[nBuf-5], "al", 2);
257822 *pnBuf = nBuf - 5 + 2;
257823 }
257824 }
257825 break;
257826
257827 }
257828 return ret;
257829}
257830
257831
257832static int fts5PorterStep1B(char *aBuf, int *pnBuf){
257833 int ret = 0;
257834 int nBuf = *pnBuf;
257835 switch( aBuf[nBuf-2] ){
257836
257837 case 'e':
257838 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
257839 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
257840 memcpy(&aBuf[nBuf-3], "ee", 2);
257841 *pnBuf = nBuf - 3 + 2;
257842 }
257843 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
257844 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
257845 *pnBuf = nBuf - 2;
257846 ret = 1;
257847 }
257848 }
257849 break;
257850
257851 case 'n':
257852 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
257853 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
257854 *pnBuf = nBuf - 3;
257855 ret = 1;
257856 }
257857 }
257858 break;
257859
257860 }
257861 return ret;
257862}
257863
257864/*
257865** GENERATED CODE ENDS HERE (mkportersteps.tcl)
257866***************************************************************************
257867**************************************************************************/
257868
257869static void fts5PorterStep1A(char *aBuf, int *pnBuf){
257870 int nBuf = *pnBuf;
257871 if( aBuf[nBuf-1]=='s' ){
257872 if( aBuf[nBuf-2]=='e' ){
257873 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
257874 || (nBuf>3 && aBuf[nBuf-3]=='i' )
257875 ){
257876 *pnBuf = nBuf-2;
257877 }else{
257878 *pnBuf = nBuf-1;
257879 }
257880 }
257881 else if( aBuf[nBuf-2]!='s' ){
257882 *pnBuf = nBuf-1;
257883 }
257884 }
257885}
257886
257887static int fts5PorterCb(
257888 void *pCtx,
257889 int tflags,
257890 const char *pToken,
257891 int nToken,
257892 int iStart,
257893 int iEnd
257894){
257895 PorterContext *p = (PorterContext*)pCtx;
257896
257897 char *aBuf;
257898 int nBuf;
257899
257900 if( nToken>FTS5_PORTER_MAX_TOKEN64 || nToken<3 ) goto pass_through;
257901 aBuf = p->aBuf;
257902 nBuf = nToken;
257903 memcpy(aBuf, pToken, nBuf);
257904
257905 /* Step 1. */
257906 fts5PorterStep1A(aBuf, &nBuf);
257907 if( fts5PorterStep1B(aBuf, &nBuf) ){
257908 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
257909 char c = aBuf[nBuf-1];
257910 if( fts5PorterIsVowel(c, 0)==0
257911 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
257912 ){
257913 nBuf--;
257914 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
257915 aBuf[nBuf++] = 'e';
257916 }
257917 }
257918 }
257919
257920 /* Step 1C. */
257921 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
257922 aBuf[nBuf-1] = 'i';
257923 }
257924
257925 /* Steps 2 through 4. */
257926 fts5PorterStep2(aBuf, &nBuf);
257927 fts5PorterStep3(aBuf, &nBuf);
257928 fts5PorterStep4(aBuf, &nBuf);
257929
257930 /* Step 5a. */
257931 assert( nBuf>0 )((void) (0));
257932 if( aBuf[nBuf-1]=='e' ){
257933 if( fts5Porter_MGt1(aBuf, nBuf-1)
257934 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
257935 ){
257936 nBuf--;
257937 }
257938 }
257939
257940 /* Step 5b. */
257941 if( nBuf>1 && aBuf[nBuf-1]=='l'
257942 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
257943 ){
257944 nBuf--;
257945 }
257946
257947 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
257948
257949 pass_through:
257950 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
257951}
257952
257953/*
257954** Tokenize using the porter tokenizer.
257955*/
257956static int fts5PorterTokenize(
257957 Fts5Tokenizer *pTokenizer,
257958 void *pCtx,
257959 int flags,
257960 const char *pText, int nText,
257961 const char *pLoc, int nLoc,
257962 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
257963){
257964 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
257965 PorterContext sCtx;
257966 sCtx.xToken = xToken;
257967 sCtx.pCtx = pCtx;
257968 sCtx.aBuf = p->aBuf;
257969 return p->tokenizer_v2.xTokenize(
257970 p->pTokenizer, (void*)&sCtx, flags, pText, nText, pLoc, nLoc, fts5PorterCb
257971 );
257972}
257973
257974/**************************************************************************
257975** Start of trigram implementation.
257976*/
257977typedef struct TrigramTokenizer TrigramTokenizer;
257978struct TrigramTokenizer {
257979 int bFold; /* True to fold to lower-case */
257980 int iFoldParam; /* Parameter to pass to Fts5UnicodeFold() */
257981};
257982
257983/*
257984** Free a trigram tokenizer.
257985*/
257986static void fts5TriDelete(Fts5Tokenizer *p){
257987 sqlite3_free(p);
257988}
257989
257990/*
257991** Allocate a trigram tokenizer.
257992*/
257993static int fts5TriCreate(
257994 void *pUnused,
257995 const char **azArg,
257996 int nArg,
257997 Fts5Tokenizer **ppOut
257998){
257999 int rc = SQLITE_OK0;
258000 TrigramTokenizer *pNew = 0;
258001 UNUSED_PARAM(pUnused)(void)(pUnused);
258002 if( nArg%2 ){
258003 rc = SQLITE_ERROR1;
258004 }else{
258005 int i;
258006 pNew = (TrigramTokenizer*)sqlite3_malloc(sizeof(*pNew));
258007 if( pNew==0 ){
258008 rc = SQLITE_NOMEM7;
258009 }else{
258010 pNew->bFold = 1;
258011 pNew->iFoldParam = 0;
258012
258013 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
258014 const char *zArg = azArg[i+1];
258015 if( 0==sqlite3_stricmp(azArg[i], "case_sensitive") ){
258016 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
258017 rc = SQLITE_ERROR1;
258018 }else{
258019 pNew->bFold = (zArg[0]=='0');
258020 }
258021 }else if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
258022 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
258023 rc = SQLITE_ERROR1;
258024 }else{
258025 pNew->iFoldParam = (zArg[0]!='0') ? 2 : 0;
258026 }
258027 }else{
258028 rc = SQLITE_ERROR1;
258029 }
258030 }
258031
258032 if( pNew->iFoldParam!=0 && pNew->bFold==0 ){
258033 rc = SQLITE_ERROR1;
258034 }
258035
258036 if( rc!=SQLITE_OK0 ){
258037 fts5TriDelete((Fts5Tokenizer*)pNew);
258038 pNew = 0;
258039 }
258040 }
258041 }
258042 *ppOut = (Fts5Tokenizer*)pNew;
258043 return rc;
258044}
258045
258046/*
258047** Trigram tokenizer tokenize routine.
258048*/
258049static int fts5TriTokenize(
258050 Fts5Tokenizer *pTok,
258051 void *pCtx,
258052 int unusedFlags,
258053 const char *pText, int nText,
258054 int (*xToken)(void*, int, const char*, int, int, int)
258055){
258056 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
258057 int rc = SQLITE_OK0;
258058 char aBuf[32];
258059 char *zOut = aBuf;
258060 int ii;
258061 const unsigned char *zIn = (const unsigned char*)pText;
258062 const unsigned char *zEof = &zIn[nText];
258063 u32 iCode;
258064 int aStart[3]; /* Input offset of each character in aBuf[] */
258065
258066 UNUSED_PARAM(unusedFlags)(void)(unusedFlags);
258067
258068 /* Populate aBuf[] with the characters for the first trigram. */
258069 for(ii=0; ii<3; ii++){
258070 do {
258071 aStart[ii] = zIn - (const unsigned char*)pText;
258072 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; } }
;
258073 if( iCode==0 ) return SQLITE_OK0;
258074 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
258075 }while( iCode==0 );
258076 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); } }
;
258077 }
258078
258079 /* At the start of each iteration of this loop:
258080 **
258081 ** aBuf: Contains 3 characters. The 3 characters of the next trigram.
258082 ** zOut: Points to the byte following the last character in aBuf.
258083 ** aStart[3]: Contains the byte offset in the input text corresponding
258084 ** to the start of each of the three characters in the buffer.
258085 */
258086 assert( zIn<=zEof )((void) (0));
258087 while( 1 ){
258088 int iNext; /* Start of character following current tri */
258089 const char *z1;
258090
258091 /* Read characters from the input up until the first non-diacritic */
258092 do {
258093 iNext = zIn - (const unsigned char*)pText;
258094 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; } }
;
258095 if( iCode==0 ) break;
258096 if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
258097 }while( iCode==0 );
258098
258099 /* Pass the current trigram back to fts5 */
258100 rc = xToken(pCtx, 0, aBuf, zOut-aBuf, aStart[0], iNext);
258101 if( iCode==0 || rc!=SQLITE_OK0 ) break;
258102
258103 /* Remove the first character from buffer aBuf[]. Append the character
258104 ** with codepoint iCode. */
258105 z1 = aBuf;
258106 FTS5_SKIP_UTF8(z1){ if( ((unsigned char)(*(z1++)))>=0xc0 ){ while( (((unsigned
char)*z1) & 0xc0)==0x80 ){ z1++; } } }
;
258107 memmove(aBuf, z1, zOut - z1);
258108 zOut -= (z1 - aBuf);
258109 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); } }
;
258110
258111 /* Update the aStart[] array */
258112 aStart[0] = aStart[1];
258113 aStart[1] = aStart[2];
258114 aStart[2] = iNext;
258115 }
258116
258117 return rc;
258118}
258119
258120/*
258121** Argument xCreate is a pointer to a constructor function for a tokenizer.
258122** pTok is a tokenizer previously created using the same method. This function
258123** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB
258124** indicating the style of pattern matching that the tokenizer can support.
258125** In practice, this is:
258126**
258127** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB
258128** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE
258129** all other tokenizers - FTS5_PATTERN_NONE
258130*/
258131static int sqlite3Fts5TokenizerPattern(
258132 int (*xCreate)(void*, const char**, int, Fts5Tokenizer**),
258133 Fts5Tokenizer *pTok
258134){
258135 if( xCreate==fts5TriCreate ){
258136 TrigramTokenizer *p = (TrigramTokenizer*)pTok;
258137 if( p->iFoldParam==0 ){
258138 return p->bFold ? FTS5_PATTERN_LIKE65 : FTS5_PATTERN_GLOB66;
258139 }
258140 }
258141 return FTS5_PATTERN_NONE0;
258142}
258143
258144/*
258145** Return true if the tokenizer described by p->azArg[] is the trigram
258146** tokenizer. This tokenizer needs to be loaded before xBestIndex is
258147** called for the first time in order to correctly handle LIKE/GLOB.
258148*/
258149static int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig *p){
258150 return (p->nArg>=1 && 0==sqlite3_stricmp(p->azArg[0], "trigram"));
258151}
258152
258153
258154/*
258155** Register all built-in tokenizers with FTS5.
258156*/
258157static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
258158 struct BuiltinTokenizer {
258159 const char *zName;
258160 fts5_tokenizer x;
258161 } aBuiltin[] = {
258162 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
258163 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
258164 { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}},
258165 };
258166
258167 int rc = SQLITE_OK0; /* Return code */
258168 int i; /* To iterate through builtin functions */
258169
258170 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
258171 rc = pApi->xCreateTokenizer(pApi,
258172 aBuiltin[i].zName,
258173 (void*)pApi,
258174 &aBuiltin[i].x,
258175 0
258176 );
258177 }
258178 if( rc==SQLITE_OK0 ){
258179 fts5_tokenizer_v2 sPorter = {
258180 2,
258181 fts5PorterCreate,
258182 fts5PorterDelete,
258183 fts5PorterTokenize
258184 };
258185 rc = pApi->xCreateTokenizer_v2(pApi,
258186 "porter",
258187 (void*)pApi,
258188 &sPorter,
258189 0
258190 );
258191 }
258192 return rc;
258193}
258194
258195/*
258196** 2012-05-25
258197**
258198** The author disclaims copyright to this source code. In place of
258199** a legal notice, here is a blessing:
258200**
258201** May you do good and not evil.
258202** May you find forgiveness for yourself and forgive others.
258203** May you share freely, never taking more than you give.
258204**
258205******************************************************************************
258206*/
258207
258208/*
258209** DO NOT EDIT THIS MACHINE GENERATED FILE.
258210*/
258211
258212
258213/* #include <assert.h> */
258214
258215
258216
258217/*
258218** If the argument is a codepoint corresponding to a lowercase letter
258219** in the ASCII range with a diacritic added, return the codepoint
258220** of the ASCII letter only. For example, if passed 235 - "LATIN
258221** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
258222** E"). The resuls of passing a codepoint that corresponds to an
258223** uppercase letter are undefined.
258224*/
258225static int fts5_remove_diacritic(int c, int bComplex){
258226 unsigned short aDia[] = {
258227 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
258228 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
258229 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
258230 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
258231 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
258232 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
258233 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
258234 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
258235 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
258236 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
258237 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
258238 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
258239 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
258240 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
258241 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
258242 63182, 63242, 63274, 63310, 63368, 63390,
258243 };
258244#define HIBIT((unsigned char)0x80) ((unsigned char)0x80)
258245 unsigned char aChar[] = {
258246 '\0', 'a', 'c', 'e', 'i', 'n',
258247 'o', 'u', 'y', 'y', 'a', 'c',
258248 'd', 'e', 'e', 'g', 'h', 'i',
258249 'j', 'k', 'l', 'n', 'o', 'r',
258250 's', 't', 'u', 'u', 'w', 'y',
258251 'z', 'o', 'u', 'a', 'i', 'o',
258252 'u', 'u'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'g', 'k', 'o',
258253 'o'|HIBIT((unsigned char)0x80), 'j', 'g', 'n', 'a'|HIBIT((unsigned char)0x80), 'a',
258254 'e', 'i', 'o', 'r', 'u', 's',
258255 't', 'h', 'a', 'e', 'o'|HIBIT((unsigned char)0x80), 'o',
258256 'o'|HIBIT((unsigned char)0x80), 'y', '\0', '\0', '\0', '\0',
258257 '\0', '\0', '\0', '\0', 'a', 'b',
258258 'c'|HIBIT((unsigned char)0x80), 'd', 'd', 'e'|HIBIT((unsigned char)0x80), 'e', 'e'|HIBIT((unsigned char)0x80),
258259 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT((unsigned char)0x80),
258260 'k', 'l', 'l'|HIBIT((unsigned char)0x80), 'l', 'm', 'n',
258261 'o'|HIBIT((unsigned char)0x80), 'p', 'r', 'r'|HIBIT((unsigned char)0x80), 'r', 's',
258262 's'|HIBIT((unsigned char)0x80), 't', 'u', 'u'|HIBIT((unsigned char)0x80), 'v', 'w',
258263 'w', 'x', 'y', 'z', 'h', 't',
258264 'w', 'y', 'a', 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80), 'a'|HIBIT((unsigned char)0x80),
258265 'e', 'e'|HIBIT((unsigned char)0x80), 'e'|HIBIT((unsigned char)0x80), 'i', 'o', 'o'|HIBIT((unsigned char)0x80),
258266 'o'|HIBIT((unsigned char)0x80), 'o'|HIBIT((unsigned char)0x80), 'u', 'u'|HIBIT((unsigned char)0x80), 'u'|HIBIT((unsigned char)0x80), 'y',
258267 };
258268
258269 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
258270 int iRes = 0;
258271 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
258272 int iLo = 0;
258273 while( iHi>=iLo ){
258274 int iTest = (iHi + iLo) / 2;
258275 if( key >= aDia[iTest] ){
258276 iRes = iTest;
258277 iLo = iTest+1;
258278 }else{
258279 iHi = iTest-1;
258280 }
258281 }
258282 assert( key>=aDia[iRes] )((void) (0));
258283 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
258284 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
258285}
258286
258287
258288/*
258289** Return true if the argument interpreted as a unicode codepoint
258290** is a diacritical modifier character.
258291*/
258292static int sqlite3Fts5UnicodeIsdiacritic(int c){
258293 unsigned int mask0 = 0x08029FDF;
258294 unsigned int mask1 = 0x000361F8;
258295 if( c<768 || c>817 ) return 0;
258296 return (c < 768+32) ?
258297 (mask0 & ((unsigned int)1 << (c-768))) :
258298 (mask1 & ((unsigned int)1 << (c-768-32)));
258299}
258300
258301
258302/*
258303** Interpret the argument as a unicode codepoint. If the codepoint
258304** is an upper case character that has a lower case equivalent,
258305** return the codepoint corresponding to the lower case version.
258306** Otherwise, return a copy of the argument.
258307**
258308** The results are undefined if the value passed to this function
258309** is less than zero.
258310*/
258311static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
258312 /* Each entry in the following array defines a rule for folding a range
258313 ** of codepoints to lower case. The rule applies to a range of nRange
258314 ** codepoints starting at codepoint iCode.
258315 **
258316 ** If the least significant bit in flags is clear, then the rule applies
258317 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
258318 ** need to be folded). Or, if it is set, then the rule only applies to
258319 ** every second codepoint in the range, starting with codepoint C.
258320 **
258321 ** The 7 most significant bits in flags are an index into the aiOff[]
258322 ** array. If a specific codepoint C does require folding, then its lower
258323 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
258324 **
258325 ** The contents of this array are generated by parsing the CaseFolding.txt
258326 ** file distributed as part of the "Unicode Character Database". See
258327 ** http://www.unicode.org for details.
258328 */
258329 static const struct TableEntry {
258330 unsigned short iCode;
258331 unsigned char flags;
258332 unsigned char nRange;
258333 } aEntry[] = {
258334 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
258335 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
258336 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
258337 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
258338 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
258339 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
258340 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
258341 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
258342 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
258343 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
258344 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
258345 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
258346 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
258347 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
258348 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
258349 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
258350 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
258351 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
258352 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
258353 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
258354 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
258355 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
258356 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
258357 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
258358 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
258359 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
258360 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
258361 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
258362 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
258363 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
258364 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
258365 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
258366 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
258367 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
258368 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
258369 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
258370 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
258371 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
258372 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
258373 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
258374 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
258375 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
258376 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
258377 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
258378 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
258379 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
258380 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
258381 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
258382 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
258383 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
258384 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
258385 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
258386 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
258387 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
258388 {65313, 14, 26},
258389 };
258390 static const unsigned short aiOff[] = {
258391 1, 2, 8, 15, 16, 26, 28, 32,
258392 37, 38, 40, 48, 63, 64, 69, 71,
258393 79, 80, 116, 202, 203, 205, 206, 207,
258394 209, 210, 211, 213, 214, 217, 218, 219,
258395 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
258396 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
258397 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
258398 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
258399 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
258400 65514, 65521, 65527, 65528, 65529,
258401 };
258402
258403 int ret = c;
258404
258405 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
258406
258407 if( c<128 ){
258408 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
258409 }else if( c<65536 ){
258410 const struct TableEntry *p;
258411 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
258412 int iLo = 0;
258413 int iRes = -1;
258414
258415 assert( c>aEntry[0].iCode )((void) (0));
258416 while( iHi>=iLo ){
258417 int iTest = (iHi + iLo) / 2;
258418 int cmp = (c - aEntry[iTest].iCode);
258419 if( cmp>=0 ){
258420 iRes = iTest;
258421 iLo = iTest+1;
258422 }else{
258423 iHi = iTest-1;
258424 }
258425 }
258426
258427 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
258428 p = &aEntry[iRes];
258429 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
258430 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
258431 assert( ret>0 )((void) (0));
258432 }
258433
258434 if( eRemoveDiacritic ){
258435 ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
258436 }
258437 }
258438
258439 else if( c>=66560 && c<66600 ){
258440 ret = c + 40;
258441 }
258442
258443 return ret;
258444}
258445
258446
258447static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
258448 aArray[0] = 1;
258449 switch( zCat[0] ){
258450 case 'C':
258451 switch( zCat[1] ){
258452 case 'c': aArray[1] = 1; break;
258453 case 'f': aArray[2] = 1; break;
258454 case 'n': aArray[3] = 1; break;
258455 case 's': aArray[4] = 1; break;
258456 case 'o': aArray[31] = 1; break;
258457 case '*':
258458 aArray[1] = 1;
258459 aArray[2] = 1;
258460 aArray[3] = 1;
258461 aArray[4] = 1;
258462 aArray[31] = 1;
258463 break;
258464 default: return 1; }
258465 break;
258466
258467 case 'L':
258468 switch( zCat[1] ){
258469 case 'l': aArray[5] = 1; break;
258470 case 'm': aArray[6] = 1; break;
258471 case 'o': aArray[7] = 1; break;
258472 case 't': aArray[8] = 1; break;
258473 case 'u': aArray[9] = 1; break;
258474 case 'C': aArray[30] = 1; break;
258475 case '*':
258476 aArray[5] = 1;
258477 aArray[6] = 1;
258478 aArray[7] = 1;
258479 aArray[8] = 1;
258480 aArray[9] = 1;
258481 aArray[30] = 1;
258482 break;
258483 default: return 1; }
258484 break;
258485
258486 case 'M':
258487 switch( zCat[1] ){
258488 case 'c': aArray[10] = 1; break;
258489 case 'e': aArray[11] = 1; break;
258490 case 'n': aArray[12] = 1; break;
258491 case '*':
258492 aArray[10] = 1;
258493 aArray[11] = 1;
258494 aArray[12] = 1;
258495 break;
258496 default: return 1; }
258497 break;
258498
258499 case 'N':
258500 switch( zCat[1] ){
258501 case 'd': aArray[13] = 1; break;
258502 case 'l': aArray[14] = 1; break;
258503 case 'o': aArray[15] = 1; break;
258504 case '*':
258505 aArray[13] = 1;
258506 aArray[14] = 1;
258507 aArray[15] = 1;
258508 break;
258509 default: return 1; }
258510 break;
258511
258512 case 'P':
258513 switch( zCat[1] ){
258514 case 'c': aArray[16] = 1; break;
258515 case 'd': aArray[17] = 1; break;
258516 case 'e': aArray[18] = 1; break;
258517 case 'f': aArray[19] = 1; break;
258518 case 'i': aArray[20] = 1; break;
258519 case 'o': aArray[21] = 1; break;
258520 case 's': aArray[22] = 1; break;
258521 case '*':
258522 aArray[16] = 1;
258523 aArray[17] = 1;
258524 aArray[18] = 1;
258525 aArray[19] = 1;
258526 aArray[20] = 1;
258527 aArray[21] = 1;
258528 aArray[22] = 1;
258529 break;
258530 default: return 1; }
258531 break;
258532
258533 case 'S':
258534 switch( zCat[1] ){
258535 case 'c': aArray[23] = 1; break;
258536 case 'k': aArray[24] = 1; break;
258537 case 'm': aArray[25] = 1; break;
258538 case 'o': aArray[26] = 1; break;
258539 case '*':
258540 aArray[23] = 1;
258541 aArray[24] = 1;
258542 aArray[25] = 1;
258543 aArray[26] = 1;
258544 break;
258545 default: return 1; }
258546 break;
258547
258548 case 'Z':
258549 switch( zCat[1] ){
258550 case 'l': aArray[27] = 1; break;
258551 case 'p': aArray[28] = 1; break;
258552 case 's': aArray[29] = 1; break;
258553 case '*':
258554 aArray[27] = 1;
258555 aArray[28] = 1;
258556 aArray[29] = 1;
258557 break;
258558 default: return 1; }
258559 break;
258560
258561
258562 default:
258563 return 1;
258564 }
258565 return 0;
258566}
258567
258568static u16 aFts5UnicodeBlock[] = {
258569 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
258570 1760, 1760, 1760, 1760, 1760, 1763, 1765,
258571 };
258572static u16 aFts5UnicodeMap[] = {
258573 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
258574 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
258575 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
258576 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
258577 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
258578 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
258579 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
258580 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
258581 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
258582 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
258583 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
258584 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
258585 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
258586 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
258587 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
258588 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
258589 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
258590 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
258591 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
258592 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
258593 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
258594 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
258595 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
258596 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
258597 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
258598 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
258599 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
258600 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
258601 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
258602 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
258603 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
258604 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
258605 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
258606 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
258607 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
258608 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
258609 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
258610 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
258611 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
258612 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
258613 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
258614 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
258615 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
258616 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
258617 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
258618 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
258619 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
258620 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
258621 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
258622 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
258623 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
258624 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
258625 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
258626 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
258627 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
258628 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
258629 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
258630 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
258631 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
258632 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
258633 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
258634 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
258635 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
258636 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
258637 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
258638 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
258639 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
258640 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
258641 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
258642 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
258643 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
258644 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
258645 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
258646 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
258647 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
258648 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
258649 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
258650 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
258651 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
258652 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
258653 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
258654 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
258655 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
258656 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
258657 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
258658 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
258659 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
258660 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
258661 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
258662 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
258663 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
258664 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
258665 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
258666 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
258667 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
258668 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
258669 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
258670 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
258671 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
258672 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
258673 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
258674 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
258675 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
258676 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
258677 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
258678 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
258679 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
258680 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
258681 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
258682 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
258683 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
258684 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
258685 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
258686 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
258687 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
258688 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
258689 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
258690 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
258691 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
258692 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
258693 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
258694 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
258695 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
258696 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
258697 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
258698 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
258699 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
258700 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
258701 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
258702 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
258703 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
258704 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
258705 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
258706 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
258707 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
258708 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
258709 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
258710 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
258711 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
258712 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
258713 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
258714 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
258715 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
258716 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
258717 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
258718 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
258719 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
258720 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
258721 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
258722 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
258723 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
258724 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
258725 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
258726 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
258727 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
258728 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
258729 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
258730 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
258731 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
258732 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
258733 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
258734 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
258735 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
258736 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
258737 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
258738 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
258739 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
258740 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
258741 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
258742 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
258743 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
258744 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
258745 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
258746 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
258747 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
258748 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
258749 1, 32, 256, 0, 65533,
258750 };
258751static u16 aFts5UnicodeData[] = {
258752 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
258753 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
258754 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
258755 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
258756 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
258757 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
258758 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
258759 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
258760 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
258761 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
258762 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
258763 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
258764 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
258765 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
258766 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
258767 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
258768 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
258769 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
258770 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
258771 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
258772 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
258773 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
258774 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
258775 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
258776 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
258777 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
258778 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
258779 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
258780 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
258781 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
258782 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
258783 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
258784 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
258785 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
258786 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
258787 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
258788 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
258789 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
258790 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
258791 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
258792 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
258793 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
258794 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
258795 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
258796 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
258797 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
258798 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
258799 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
258800 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
258801 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
258802 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
258803 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
258804 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
258805 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
258806 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
258807 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
258808 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
258809 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
258810 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
258811 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
258812 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
258813 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
258814 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
258815 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
258816 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
258817 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
258818 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
258819 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
258820 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
258821 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
258822 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
258823 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
258824 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
258825 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
258826 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
258827 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
258828 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
258829 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
258830 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
258831 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
258832 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
258833 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
258834 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
258835 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
258836 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
258837 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
258838 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
258839 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
258840 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
258841 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
258842 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
258843 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
258844 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
258845 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
258846 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
258847 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
258848 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
258849 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
258850 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
258851 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
258852 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
258853 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
258854 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
258855 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
258856 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
258857 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
258858 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
258859 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
258860 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
258861 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
258862 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
258863 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
258864 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
258865 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
258866 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
258867 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
258868 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
258869 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
258870 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
258871 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
258872 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
258873 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
258874 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
258875 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
258876 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
258877 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
258878 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
258879 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
258880 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
258881 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
258882 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
258883 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
258884 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
258885 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
258886 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
258887 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
258888 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
258889 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
258890 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
258891 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
258892 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
258893 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
258894 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
258895 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
258896 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
258897 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
258898 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
258899 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
258900 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
258901 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
258902 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
258903 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
258904 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
258905 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
258906 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
258907 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
258908 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
258909 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
258910 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
258911 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
258912 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
258913 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
258914 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
258915 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
258916 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
258917 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
258918 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
258919 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
258920 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
258921 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
258922 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
258923 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
258924 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
258925 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
258926 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
258927 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
258928 34, 3074, 7692, 63, 63,
258929 };
258930
258931static int sqlite3Fts5UnicodeCategory(u32 iCode) {
258932 int iRes = -1;
258933 int iHi;
258934 int iLo;
258935 int ret;
258936 u16 iKey;
258937
258938 if( iCode>=(1<<20) ){
258939 return 0;
258940 }
258941 iLo = aFts5UnicodeBlock[(iCode>>16)];
258942 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
258943 iKey = (iCode & 0xFFFF);
258944 while( iHi>iLo ){
258945 int iTest = (iHi + iLo) / 2;
258946 assert( iTest>=iLo && iTest<iHi )((void) (0));
258947 if( iKey>=aFts5UnicodeMap[iTest] ){
258948 iRes = iTest;
258949 iLo = iTest+1;
258950 }else{
258951 iHi = iTest;
258952 }
258953 }
258954
258955 if( iRes<0 ) return 0;
258956 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
258957 ret = aFts5UnicodeData[iRes] & 0x1F;
258958 if( ret!=30 ) return ret;
258959 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
258960}
258961
258962static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
258963 int i = 0;
258964 int iTbl = 0;
258965 while( i<128 ){
258966 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
258967 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
258968 for(; i<128 && i<n; i++){
258969 aAscii[i] = (u8)bToken;
258970 }
258971 iTbl++;
258972 }
258973 aAscii[0] = 0; /* 0x00 is never a token character */
258974}
258975
258976
258977/*
258978** 2015 May 30
258979**
258980** The author disclaims copyright to this source code. In place of
258981** a legal notice, here is a blessing:
258982**
258983** May you do good and not evil.
258984** May you find forgiveness for yourself and forgive others.
258985** May you share freely, never taking more than you give.
258986**
258987******************************************************************************
258988**
258989** Routines for varint serialization and deserialization.
258990*/
258991
258992
258993/* #include "fts5Int.h" */
258994
258995/*
258996** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
258997** Except, this version does handle the single byte case that the core
258998** version depends on being handled before its function is called.
258999*/
259000static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
259001 u32 a,b;
259002
259003 /* The 1-byte case. Overwhelmingly the most common. */
259004 a = *p;
259005 /* a: p0 (unmasked) */
259006 if (!(a&0x80))
259007 {
259008 /* Values between 0 and 127 */
259009 *v = a;
259010 return 1;
259011 }
259012
259013 /* The 2-byte case */
259014 p++;
259015 b = *p;
259016 /* b: p1 (unmasked) */
259017 if (!(b&0x80))
259018 {
259019 /* Values between 128 and 16383 */
259020 a &= 0x7f;
259021 a = a<<7;
259022 *v = a | b;
259023 return 2;
259024 }
259025
259026 /* The 3-byte case */
259027 p++;
259028 a = a<<14;
259029 a |= *p;
259030 /* a: p0<<14 | p2 (unmasked) */
259031 if (!(a&0x80))
259032 {
259033 /* Values between 16384 and 2097151 */
259034 a &= (0x7f<<14)|(0x7f);
259035 b &= 0x7f;
259036 b = b<<7;
259037 *v = a | b;
259038 return 3;
259039 }
259040
259041 /* A 32-bit varint is used to store size information in btrees.
259042 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
259043 ** A 3-byte varint is sufficient, for example, to record the size
259044 ** of a 1048569-byte BLOB or string.
259045 **
259046 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
259047 ** rare larger cases can be handled by the slower 64-bit varint
259048 ** routine.
259049 */
259050 {
259051 u64 v64;
259052 u8 n;
259053 p -= 2;
259054 n = sqlite3Fts5GetVarint(p, &v64);
259055 *v = ((u32)v64) & 0x7FFFFFFF;
259056 assert( n>3 && n<=9 )((void) (0));
259057 return n;
259058 }
259059}
259060
259061
259062/*
259063** Bitmasks used by sqlite3GetVarint(). These precomputed constants
259064** are defined here rather than simply putting the constant expressions
259065** inline in order to work around bugs in the RVT compiler.
259066**
259067** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
259068**
259069** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
259070*/
259071#define SLOT_2_00x001fc07f 0x001fc07f
259072#define SLOT_4_2_00xf01fc07f 0xf01fc07f
259073
259074/*
259075** Read a 64-bit variable-length integer from memory starting at p[0].
259076** Return the number of bytes read. The value is stored in *v.
259077*/
259078static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
259079 u32 a,b,s;
259080
259081 a = *p;
259082 /* a: p0 (unmasked) */
259083 if (!(a&0x80))
259084 {
259085 *v = a;
259086 return 1;
259087 }
259088
259089 p++;
259090 b = *p;
259091 /* b: p1 (unmasked) */
259092 if (!(b&0x80))
259093 {
259094 a &= 0x7f;
259095 a = a<<7;
259096 a |= b;
259097 *v = a;
259098 return 2;
259099 }
259100
259101 /* Verify that constants are precomputed correctly */
259102 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
259103 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
259104
259105 p++;
259106 a = a<<14;
259107 a |= *p;
259108 /* a: p0<<14 | p2 (unmasked) */
259109 if (!(a&0x80))
259110 {
259111 a &= SLOT_2_00x001fc07f;
259112 b &= 0x7f;
259113 b = b<<7;
259114 a |= b;
259115 *v = a;
259116 return 3;
259117 }
259118
259119 /* CSE1 from below */
259120 a &= SLOT_2_00x001fc07f;
259121 p++;
259122 b = b<<14;
259123 b |= *p;
259124 /* b: p1<<14 | p3 (unmasked) */
259125 if (!(b&0x80))
259126 {
259127 b &= SLOT_2_00x001fc07f;
259128 /* moved CSE1 up */
259129 /* a &= (0x7f<<14)|(0x7f); */
259130 a = a<<7;
259131 a |= b;
259132 *v = a;
259133 return 4;
259134 }
259135
259136 /* a: p0<<14 | p2 (masked) */
259137 /* b: p1<<14 | p3 (unmasked) */
259138 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
259139 /* moved CSE1 up */
259140 /* a &= (0x7f<<14)|(0x7f); */
259141 b &= SLOT_2_00x001fc07f;
259142 s = a;
259143 /* s: p0<<14 | p2 (masked) */
259144
259145 p++;
259146 a = a<<14;
259147 a |= *p;
259148 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
259149 if (!(a&0x80))
259150 {
259151 /* we can skip these cause they were (effectively) done above in calc'ing s */
259152 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
259153 /* b &= (0x7f<<14)|(0x7f); */
259154 b = b<<7;
259155 a |= b;
259156 s = s>>18;
259157 *v = ((u64)s)<<32 | a;
259158 return 5;
259159 }
259160
259161 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
259162 s = s<<7;
259163 s |= b;
259164 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
259165
259166 p++;
259167 b = b<<14;
259168 b |= *p;
259169 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
259170 if (!(b&0x80))
259171 {
259172 /* we can skip this cause it was (effectively) done above in calc'ing s */
259173 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
259174 a &= SLOT_2_00x001fc07f;
259175 a = a<<7;
259176 a |= b;
259177 s = s>>18;
259178 *v = ((u64)s)<<32 | a;
259179 return 6;
259180 }
259181
259182 p++;
259183 a = a<<14;
259184 a |= *p;
259185 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
259186 if (!(a&0x80))
259187 {
259188 a &= SLOT_4_2_00xf01fc07f;
259189 b &= SLOT_2_00x001fc07f;
259190 b = b<<7;
259191 a |= b;
259192 s = s>>11;
259193 *v = ((u64)s)<<32 | a;
259194 return 7;
259195 }
259196
259197 /* CSE2 from below */
259198 a &= SLOT_2_00x001fc07f;
259199 p++;
259200 b = b<<14;
259201 b |= *p;
259202 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
259203 if (!(b&0x80))
259204 {
259205 b &= SLOT_4_2_00xf01fc07f;
259206 /* moved CSE2 up */
259207 /* a &= (0x7f<<14)|(0x7f); */
259208 a = a<<7;
259209 a |= b;
259210 s = s>>4;
259211 *v = ((u64)s)<<32 | a;
259212 return 8;
259213 }
259214
259215 p++;
259216 a = a<<15;
259217 a |= *p;
259218 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
259219
259220 /* moved CSE2 up */
259221 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
259222 b &= SLOT_2_00x001fc07f;
259223 b = b<<8;
259224 a |= b;
259225
259226 s = s<<4;
259227 b = p[-4];
259228 b &= 0x7f;
259229 b = b>>3;
259230 s |= b;
259231
259232 *v = ((u64)s)<<32 | a;
259233
259234 return 9;
259235}
259236
259237/*
259238** The variable-length integer encoding is as follows:
259239**
259240** KEY:
259241** A = 0xxxxxxx 7 bits of data and one flag bit
259242** B = 1xxxxxxx 7 bits of data and one flag bit
259243** C = xxxxxxxx 8 bits of data
259244**
259245** 7 bits - A
259246** 14 bits - BA
259247** 21 bits - BBA
259248** 28 bits - BBBA
259249** 35 bits - BBBBA
259250** 42 bits - BBBBBA
259251** 49 bits - BBBBBBA
259252** 56 bits - BBBBBBBA
259253** 64 bits - BBBBBBBBC
259254*/
259255
259256#ifdef SQLITE_NOINLINE__attribute__((noinline))
259257# define FTS5_NOINLINE__attribute__((noinline)) SQLITE_NOINLINE__attribute__((noinline))
259258#else
259259# define FTS5_NOINLINE__attribute__((noinline))
259260#endif
259261
259262/*
259263** Write a 64-bit variable-length integer to memory starting at p[0].
259264** The length of data write will be between 1 and 9 bytes. The number
259265** of bytes written is returned.
259266**
259267** A variable-length integer consists of the lower 7 bits of each byte
259268** for all bytes that have the 8th bit set and one byte with the 8th
259269** bit clear. Except, if we get to the 9th byte, it stores the full
259270** 8 bits and is the last byte.
259271*/
259272static int FTS5_NOINLINE__attribute__((noinline)) fts5PutVarint64(unsigned char *p, u64 v){
259273 int i, j, n;
259274 u8 buf[10];
259275 if( v & (((u64)0xff000000)<<32) ){
259276 p[8] = (u8)v;
259277 v >>= 8;
259278 for(i=7; i>=0; i--){
259279 p[i] = (u8)((v & 0x7f) | 0x80);
259280 v >>= 7;
259281 }
259282 return 9;
259283 }
259284 n = 0;
259285 do{
259286 buf[n++] = (u8)((v & 0x7f) | 0x80);
259287 v >>= 7;
259288 }while( v!=0 );
259289 buf[0] &= 0x7f;
259290 assert( n<=9 )((void) (0));
259291 for(i=0, j=n-1; j>=0; j--, i++){
259292 p[i] = buf[j];
259293 }
259294 return n;
259295}
259296
259297static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
259298 if( v<=0x7f ){
259299 p[0] = v&0x7f;
259300 return 1;
259301 }
259302 if( v<=0x3fff ){
259303 p[0] = ((v>>7)&0x7f)|0x80;
259304 p[1] = v&0x7f;
259305 return 2;
259306 }
259307 return fts5PutVarint64(p,v);
259308}
259309
259310
259311static int sqlite3Fts5GetVarintLen(u32 iVal){
259312#if 0
259313 if( iVal<(1 << 7 ) ) return 1;
259314#endif
259315 assert( iVal>=(1 << 7) )((void) (0));
259316 if( iVal<(1 << 14) ) return 2;
259317 if( iVal<(1 << 21) ) return 3;
259318 if( iVal<(1 << 28) ) return 4;
259319 return 5;
259320}
259321
259322/*
259323** 2015 May 08
259324**
259325** The author disclaims copyright to this source code. In place of
259326** a legal notice, here is a blessing:
259327**
259328** May you do good and not evil.
259329** May you find forgiveness for yourself and forgive others.
259330** May you share freely, never taking more than you give.
259331**
259332******************************************************************************
259333**
259334** This is an SQLite virtual table module implementing direct access to an
259335** existing FTS5 index. The module may create several different types of
259336** tables:
259337**
259338** col:
259339** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
259340**
259341** One row for each term/column combination. The value of $doc is set to
259342** the number of fts5 rows that contain at least one instance of term
259343** $term within column $col. Field $cnt is set to the total number of
259344** instances of term $term in column $col (in any row of the fts5 table).
259345**
259346** row:
259347** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
259348**
259349** One row for each term in the database. The value of $doc is set to
259350** the number of fts5 rows that contain at least one instance of term
259351** $term. Field $cnt is set to the total number of instances of term
259352** $term in the database.
259353**
259354** instance:
259355** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
259356**
259357** One row for each term instance in the database.
259358*/
259359
259360
259361/* #include "fts5Int.h" */
259362
259363
259364typedef struct Fts5VocabTable Fts5VocabTable;
259365typedef struct Fts5VocabCursor Fts5VocabCursor;
259366
259367struct Fts5VocabTable {
259368 sqlite3_vtab base;
259369 char *zFts5Tbl; /* Name of fts5 table */
259370 char *zFts5Db; /* Db containing fts5 table */
259371 sqlite3 *db; /* Database handle */
259372 Fts5Global *pGlobal; /* FTS5 global object for this database */
259373 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
259374 unsigned bBusy; /* True if busy */
259375};
259376
259377struct Fts5VocabCursor {
259378 sqlite3_vtab_cursor base;
259379 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
259380 Fts5Table *pFts5; /* Associated FTS5 table */
259381
259382 int bEof; /* True if this cursor is at EOF */
259383 Fts5IndexIter *pIter; /* Term/rowid iterator object */
259384 void *pStruct; /* From sqlite3Fts5StructureRef() */
259385
259386 int nLeTerm; /* Size of zLeTerm in bytes */
259387 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
259388 int colUsed; /* Copy of sqlite3_index_info.colUsed */
259389
259390 /* These are used by 'col' tables only */
259391 int iCol;
259392 i64 *aCnt;
259393 i64 *aDoc;
259394
259395 /* Output values used by all tables. */
259396 i64 rowid; /* This table's current rowid value */
259397 Fts5Buffer term; /* Current value of 'term' column */
259398
259399 /* Output values Used by 'instance' tables only */
259400 i64 iInstPos;
259401 int iInstOff;
259402};
259403
259404#define FTS5_VOCAB_COL0 0
259405#define FTS5_VOCAB_ROW1 1
259406#define FTS5_VOCAB_INSTANCE2 2
259407
259408#define FTS5_VOCAB_COL_SCHEMA"term, col, doc, cnt" "term, col, doc, cnt"
259409#define FTS5_VOCAB_ROW_SCHEMA"term, doc, cnt" "term, doc, cnt"
259410#define FTS5_VOCAB_INST_SCHEMA"term, doc, col, offset" "term, doc, col, offset"
259411
259412/*
259413** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
259414*/
259415#define FTS5_VOCAB_TERM_EQ0x0100 0x0100
259416#define FTS5_VOCAB_TERM_GE0x0200 0x0200
259417#define FTS5_VOCAB_TERM_LE0x0400 0x0400
259418
259419#define FTS5_VOCAB_COLUSED_MASK0xFF 0xFF
259420
259421
259422/*
259423** Translate a string containing an fts5vocab table type to an
259424** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
259425** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
259426** and return SQLITE_ERROR.
259427*/
259428static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
259429 int rc = SQLITE_OK0;
259430 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
259431 if( rc==SQLITE_OK0 ){
259432 sqlite3Fts5Dequote(zCopy);
259433 if( sqlite3_stricmp(zCopy, "col")==0 ){
259434 *peType = FTS5_VOCAB_COL0;
259435 }else
259436
259437 if( sqlite3_stricmp(zCopy, "row")==0 ){
259438 *peType = FTS5_VOCAB_ROW1;
259439 }else
259440 if( sqlite3_stricmp(zCopy, "instance")==0 ){
259441 *peType = FTS5_VOCAB_INSTANCE2;
259442 }else
259443 {
259444 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
259445 rc = SQLITE_ERROR1;
259446 }
259447 sqlite3_free(zCopy);
259448 }
259449
259450 return rc;
259451}
259452
259453
259454/*
259455** The xDisconnect() virtual table method.
259456*/
259457static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
259458 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
259459 sqlite3_free(pTab);
259460 return SQLITE_OK0;
259461}
259462
259463/*
259464** The xDestroy() virtual table method.
259465*/
259466static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
259467 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
259468 sqlite3_free(pTab);
259469 return SQLITE_OK0;
259470}
259471
259472/*
259473** This function is the implementation of both the xConnect and xCreate
259474** methods of the FTS3 virtual table.
259475**
259476** The argv[] array contains the following:
259477**
259478** argv[0] -> module name ("fts5vocab")
259479** argv[1] -> database name
259480** argv[2] -> table name
259481**
259482** then:
259483**
259484** argv[3] -> name of fts5 table
259485** argv[4] -> type of fts5vocab table
259486**
259487** or, for tables in the TEMP schema only.
259488**
259489** argv[3] -> name of fts5 tables database
259490** argv[4] -> name of fts5 table
259491** argv[5] -> type of fts5vocab table
259492*/
259493static int fts5VocabInitVtab(
259494 sqlite3 *db, /* The SQLite database connection */
259495 void *pAux, /* Pointer to Fts5Global object */
259496 int argc, /* Number of elements in argv array */
259497 const char * const *argv, /* xCreate/xConnect argument array */
259498 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
259499 char **pzErr /* Write any error message here */
259500){
259501 const char *azSchema[] = {
259502 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA"term, col, doc, cnt" ")",
259503 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA"term, doc, cnt" ")",
259504 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA"term, doc, col, offset" ")"
259505 };
259506
259507 Fts5VocabTable *pRet = 0;
259508 int rc = SQLITE_OK0; /* Return code */
259509 int bDb;
259510
259511 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
259512
259513 if( argc!=5 && bDb==0 ){
259514 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
259515 rc = SQLITE_ERROR1;
259516 }else{
259517 int nByte; /* Bytes of space to allocate */
259518 const char *zDb = bDb ? argv[3] : argv[1];
259519 const char *zTab = bDb ? argv[4] : argv[3];
259520 const char *zType = bDb ? argv[5] : argv[4];
259521 int nDb = (int)strlen(zDb)+1;
259522 int nTab = (int)strlen(zTab)+1;
259523 int eType = 0;
259524
259525 rc = fts5VocabTableType(zType, pzErr, &eType);
259526 if( rc==SQLITE_OK0 ){
259527 assert( eType>=0 && eType<ArraySize(azSchema) )((void) (0));
259528 rc = sqlite3_declare_vtab(db, azSchema[eType]);
259529 }
259530
259531 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
259532 pRet = sqlite3Fts5MallocZero(&rc, nByte);
259533 if( pRet ){
259534 pRet->pGlobal = (Fts5Global*)pAux;
259535 pRet->eType = eType;
259536 pRet->db = db;
259537 pRet->zFts5Tbl = (char*)&pRet[1];
259538 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
259539 memcpy(pRet->zFts5Tbl, zTab, nTab);
259540 memcpy(pRet->zFts5Db, zDb, nDb);
259541 sqlite3Fts5Dequote(pRet->zFts5Tbl);
259542 sqlite3Fts5Dequote(pRet->zFts5Db);
259543 }
259544 }
259545
259546 *ppVTab = (sqlite3_vtab*)pRet;
259547 return rc;
259548}
259549
259550
259551/*
259552** The xConnect() and xCreate() methods for the virtual table. All the
259553** work is done in function fts5VocabInitVtab().
259554*/
259555static int fts5VocabConnectMethod(
259556 sqlite3 *db, /* Database connection */
259557 void *pAux, /* Pointer to tokenizer hash table */
259558 int argc, /* Number of elements in argv array */
259559 const char * const *argv, /* xCreate/xConnect argument array */
259560 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
259561 char **pzErr /* OUT: sqlite3_malloc'd error message */
259562){
259563 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
259564}
259565static int fts5VocabCreateMethod(
259566 sqlite3 *db, /* Database connection */
259567 void *pAux, /* Pointer to tokenizer hash table */
259568 int argc, /* Number of elements in argv array */
259569 const char * const *argv, /* xCreate/xConnect argument array */
259570 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
259571 char **pzErr /* OUT: sqlite3_malloc'd error message */
259572){
259573 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
259574}
259575
259576/*
259577** Implementation of the xBestIndex method.
259578**
259579** Only constraints of the form:
259580**
259581** term <= ?
259582** term == ?
259583** term >= ?
259584**
259585** are interpreted. Less-than and less-than-or-equal are treated
259586** identically, as are greater-than and greater-than-or-equal.
259587*/
259588static int fts5VocabBestIndexMethod(
259589 sqlite3_vtab *pUnused,
259590 sqlite3_index_info *pInfo
259591){
259592 int i;
259593 int iTermEq = -1;
259594 int iTermGe = -1;
259595 int iTermLe = -1;
259596 int idxNum = (int)pInfo->colUsed;
259597 int nArg = 0;
259598
259599 UNUSED_PARAM(pUnused)(void)(pUnused);
259600
259601 assert( (pInfo->colUsed & FTS5_VOCAB_COLUSED_MASK)==pInfo->colUsed )((void) (0));
259602
259603 for(i=0; i<pInfo->nConstraint; i++){
259604 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
259605 if( p->usable==0 ) continue;
259606 if( p->iColumn==0 ){ /* term column */
259607 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iTermEq = i;
259608 if( p->op==SQLITE_INDEX_CONSTRAINT_LE8 ) iTermLe = i;
259609 if( p->op==SQLITE_INDEX_CONSTRAINT_LT16 ) iTermLe = i;
259610 if( p->op==SQLITE_INDEX_CONSTRAINT_GE32 ) iTermGe = i;
259611 if( p->op==SQLITE_INDEX_CONSTRAINT_GT4 ) iTermGe = i;
259612 }
259613 }
259614
259615 if( iTermEq>=0 ){
259616 idxNum |= FTS5_VOCAB_TERM_EQ0x0100;
259617 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
259618 pInfo->estimatedCost = 100;
259619 }else{
259620 pInfo->estimatedCost = 1000000;
259621 if( iTermGe>=0 ){
259622 idxNum |= FTS5_VOCAB_TERM_GE0x0200;
259623 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
259624 pInfo->estimatedCost = pInfo->estimatedCost / 2;
259625 }
259626 if( iTermLe>=0 ){
259627 idxNum |= FTS5_VOCAB_TERM_LE0x0400;
259628 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
259629 pInfo->estimatedCost = pInfo->estimatedCost / 2;
259630 }
259631 }
259632
259633 /* This virtual table always delivers results in ascending order of
259634 ** the "term" column (column 0). So if the user has requested this
259635 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
259636 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
259637 ** are already in sorted order. */
259638 if( pInfo->nOrderBy==1
259639 && pInfo->aOrderBy[0].iColumn==0
259640 && pInfo->aOrderBy[0].desc==0
259641 ){
259642 pInfo->orderByConsumed = 1;
259643 }
259644
259645 pInfo->idxNum = idxNum;
259646 return SQLITE_OK0;
259647}
259648
259649/*
259650** Implementation of xOpen method.
259651*/
259652static int fts5VocabOpenMethod(
259653 sqlite3_vtab *pVTab,
259654 sqlite3_vtab_cursor **ppCsr
259655){
259656 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
259657 Fts5Table *pFts5 = 0;
259658 Fts5VocabCursor *pCsr = 0;
259659 int rc = SQLITE_OK0;
259660 sqlite3_stmt *pStmt = 0;
259661 char *zSql = 0;
259662
259663 if( pTab->bBusy ){
259664 pVTab->zErrMsg = sqlite3_mprintf(
259665 "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
259666 );
259667 return SQLITE_ERROR1;
259668 }
259669 zSql = sqlite3Fts5Mprintf(&rc,
259670 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
259671 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
259672 );
259673 if( zSql ){
259674 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
259675 }
259676 sqlite3_free(zSql);
259677 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
259678 if( rc==SQLITE_ERROR1 ) rc = SQLITE_OK0;
259679
259680 pTab->bBusy = 1;
259681 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW100 ){
259682 i64 iId = sqlite3_column_int64(pStmt, 0);
259683 pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
259684 }
259685 pTab->bBusy = 0;
259686
259687 if( rc==SQLITE_OK0 ){
259688 if( pFts5==0 ){
259689 rc = sqlite3_finalize(pStmt);
259690 pStmt = 0;
259691 if( rc==SQLITE_OK0 ){
259692 pVTab->zErrMsg = sqlite3_mprintf(
259693 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
259694 );
259695 rc = SQLITE_ERROR1;
259696 }
259697 }else{
259698 rc = sqlite3Fts5FlushToDisk(pFts5);
259699 }
259700 }
259701
259702 if( rc==SQLITE_OK0 ){
259703 i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
259704 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
259705 }
259706
259707 if( pCsr ){
259708 pCsr->pFts5 = pFts5;
259709 pCsr->pStmt = pStmt;
259710 pCsr->aCnt = (i64*)&pCsr[1];
259711 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
259712 }else{
259713 sqlite3_finalize(pStmt);
259714 }
259715
259716 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
259717 return rc;
259718}
259719
259720static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
259721 pCsr->rowid = 0;
259722 sqlite3Fts5IterClose(pCsr->pIter);
259723 sqlite3Fts5StructureRelease(pCsr->pStruct);
259724 pCsr->pStruct = 0;
259725 pCsr->pIter = 0;
259726 sqlite3_free(pCsr->zLeTerm);
259727 pCsr->nLeTerm = -1;
259728 pCsr->zLeTerm = 0;
259729 pCsr->bEof = 0;
259730}
259731
259732/*
259733** Close the cursor. For additional information see the documentation
259734** on the xClose method of the virtual table interface.
259735*/
259736static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
259737 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
259738 fts5VocabResetCursor(pCsr);
259739 sqlite3Fts5BufferFree(&pCsr->term);
259740 sqlite3_finalize(pCsr->pStmt);
259741 sqlite3_free(pCsr);
259742 return SQLITE_OK0;
259743}
259744
259745static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
259746 int rc = SQLITE_OK0;
259747
259748 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ){
259749 pCsr->bEof = 1;
259750 }else{
259751 const char *zTerm;
259752 int nTerm;
259753 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
259754 if( pCsr->nLeTerm>=0 ){
259755 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
259756 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
259757 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
259758 pCsr->bEof = 1;
259759 }
259760 }
259761
259762 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
259763 }
259764 return rc;
259765}
259766
259767static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
259768 int eDetail = pCsr->pFts5->pConfig->eDetail;
259769 int rc = SQLITE_OK0;
259770 Fts5IndexIter *pIter = pCsr->pIter;
259771 i64 *pp = &pCsr->iInstPos;
259772 int *po = &pCsr->iInstOff;
259773
259774 assert( sqlite3Fts5IterEof(pIter)==0 )((void) (0));
259775 assert( pCsr->bEof==0 )((void) (0));
259776 while( eDetail==FTS5_DETAIL_NONE1
259777 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
259778 ){
259779 pCsr->iInstPos = 0;
259780 pCsr->iInstOff = 0;
259781
259782 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
259783 if( rc==SQLITE_OK0 ){
259784 rc = fts5VocabInstanceNewTerm(pCsr);
259785 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE1 ) break;
259786 }
259787 if( rc ){
259788 pCsr->bEof = 1;
259789 break;
259790 }
259791 }
259792
259793 return rc;
259794}
259795
259796/*
259797** Advance the cursor to the next row in the table.
259798*/
259799static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
259800 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
259801 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
259802 int nCol = pCsr->pFts5->pConfig->nCol;
259803 int rc;
259804
259805 rc = sqlite3Fts5StructureTest(pCsr->pFts5->pIndex, pCsr->pStruct);
259806 if( rc!=SQLITE_OK0 ) return rc;
259807 pCsr->rowid++;
259808
259809 if( pTab->eType==FTS5_VOCAB_INSTANCE2 ){
259810 return fts5VocabInstanceNext(pCsr);
259811 }
259812
259813 if( pTab->eType==FTS5_VOCAB_COL0 ){
259814 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
259815 if( pCsr->aDoc[pCsr->iCol] ) break;
259816 }
259817 }
259818
259819 if( pTab->eType!=FTS5_VOCAB_COL0 || pCsr->iCol>=nCol ){
259820 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ){
259821 pCsr->bEof = 1;
259822 }else{
259823 const char *zTerm;
259824 int nTerm;
259825
259826 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
259827 assert( nTerm>=0 )((void) (0));
259828 if( pCsr->nLeTerm>=0 ){
259829 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
259830 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
259831 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
259832 pCsr->bEof = 1;
259833 return SQLITE_OK0;
259834 }
259835 }
259836
259837 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
259838 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
259839 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
259840 pCsr->iCol = 0;
259841
259842 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW )((void) (0));
259843 while( rc==SQLITE_OK0 ){
259844 int eDetail = pCsr->pFts5->pConfig->eDetail;
259845 const u8 *pPos; int nPos; /* Position list */
259846 i64 iPos = 0; /* 64-bit position read from poslist */
259847 int iOff = 0; /* Current offset within position list */
259848
259849 pPos = pCsr->pIter->pData;
259850 nPos = pCsr->pIter->nData;
259851
259852 switch( pTab->eType ){
259853 case FTS5_VOCAB_ROW1:
259854 /* Do not bother counting the number of instances if the "cnt"
259855 ** column is not being read (according to colUsed). */
259856 if( eDetail==FTS5_DETAIL_FULL0 && (pCsr->colUsed & 0x04) ){
259857 while( iPos<nPos ){
259858 u32 ii;
259859 fts5FastGetVarint32(pPos, iPos, ii){ ii = (pPos)[iPos++]; if( ii & 0x80 ){ iPos--; iPos += sqlite3Fts5GetVarint32
(&(pPos)[iPos],(u32*)&(ii)); } }
;
259860 if( ii==1 ){
259861 /* New column in the position list */
259862 fts5FastGetVarint32(pPos, iPos, ii){ ii = (pPos)[iPos++]; if( ii & 0x80 ){ iPos--; iPos += sqlite3Fts5GetVarint32
(&(pPos)[iPos],(u32*)&(ii)); } }
;
259863 }else{
259864 /* An instance - increment pCsr->aCnt[] */
259865 pCsr->aCnt[0]++;
259866 }
259867 }
259868 }
259869 pCsr->aDoc[0]++;
259870 break;
259871
259872 case FTS5_VOCAB_COL0:
259873 if( eDetail==FTS5_DETAIL_FULL0 ){
259874 int iCol = -1;
259875 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
259876 int ii = FTS5_POS2COLUMN(iPos)(int)((iPos >> 32) & 0x7FFFFFFF);
259877 if( iCol!=ii ){
259878 if( ii>=nCol ){
259879 rc = FTS5_CORRUPT(11 | (1<<8));
259880 break;
259881 }
259882 pCsr->aDoc[ii]++;
259883 iCol = ii;
259884 }
259885 pCsr->aCnt[ii]++;
259886 }
259887 }else if( eDetail==FTS5_DETAIL_COLUMNS2 ){
259888 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
259889 assert_nc( iPos>=0 && iPos<nCol )((void) (0));
259890 if( iPos>=nCol ){
259891 rc = FTS5_CORRUPT(11 | (1<<8));
259892 break;
259893 }
259894 pCsr->aDoc[iPos]++;
259895 }
259896 }else{
259897 assert( eDetail==FTS5_DETAIL_NONE )((void) (0));
259898 pCsr->aDoc[0]++;
259899 }
259900 break;
259901
259902 default:
259903 assert( pTab->eType==FTS5_VOCAB_INSTANCE )((void) (0));
259904 break;
259905 }
259906
259907 if( rc==SQLITE_OK0 ){
259908 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
259909 }
259910 if( pTab->eType==FTS5_VOCAB_INSTANCE2 ) break;
259911
259912 if( rc==SQLITE_OK0 ){
259913 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
259914 if( nTerm!=pCsr->term.n
259915 || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm))
259916 ){
259917 break;
259918 }
259919 if( sqlite3Fts5IterEof(pCsr->pIter)((pCsr->pIter)->bEof) ) break;
259920 }
259921 }
259922 }
259923 }
259924
259925 if( rc==SQLITE_OK0 && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL0 ){
259926 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
259927 if( pCsr->iCol==nCol ){
259928 rc = FTS5_CORRUPT(11 | (1<<8));
259929 }
259930 }
259931 return rc;
259932}
259933
259934/*
259935** This is the xFilter implementation for the virtual table.
259936*/
259937static int fts5VocabFilterMethod(
259938 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
259939 int idxNum, /* Strategy index */
259940 const char *zUnused, /* Unused */
259941 int nUnused, /* Number of elements in apVal */
259942 sqlite3_value **apVal /* Arguments for the indexing scheme */
259943){
259944 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
259945 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
259946 int eType = pTab->eType;
259947 int rc = SQLITE_OK0;
259948
259949 int iVal = 0;
259950 int f = FTS5INDEX_QUERY_SCAN0x0008;
259951 const char *zTerm = 0;
259952 int nTerm = 0;
259953
259954 sqlite3_value *pEq = 0;
259955 sqlite3_value *pGe = 0;
259956 sqlite3_value *pLe = 0;
259957
259958 UNUSED_PARAM2(zUnused, nUnused)(void)(zUnused), (void)(nUnused);
259959
259960 fts5VocabResetCursor(pCsr);
259961 if( idxNum & FTS5_VOCAB_TERM_EQ0x0100 ) pEq = apVal[iVal++];
259962 if( idxNum & FTS5_VOCAB_TERM_GE0x0200 ) pGe = apVal[iVal++];
259963 if( idxNum & FTS5_VOCAB_TERM_LE0x0400 ) pLe = apVal[iVal++];
259964 pCsr->colUsed = (idxNum & FTS5_VOCAB_COLUSED_MASK0xFF);
259965
259966 if( pEq ){
259967 zTerm = (const char *)sqlite3_value_text(pEq);
259968 nTerm = sqlite3_value_bytes(pEq);
259969 f = FTS5INDEX_QUERY_NOTOKENDATA0x0080;
259970 }else{
259971 if( pGe ){
259972 zTerm = (const char *)sqlite3_value_text(pGe);
259973 nTerm = sqlite3_value_bytes(pGe);
259974 }
259975 if( pLe ){
259976 const char *zCopy = (const char *)sqlite3_value_text(pLe);
259977 if( zCopy==0 ) zCopy = "";
259978 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
259979 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
259980 if( pCsr->zLeTerm==0 ){
259981 rc = SQLITE_NOMEM7;
259982 }else{
259983 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
259984 }
259985 }
259986 }
259987
259988 if( rc==SQLITE_OK0 ){
259989 Fts5Index *pIndex = pCsr->pFts5->pIndex;
259990 rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
259991 if( rc==SQLITE_OK0 ){
259992 pCsr->pStruct = sqlite3Fts5StructureRef(pIndex);
259993 }
259994 }
259995 if( rc==SQLITE_OK0 && eType==FTS5_VOCAB_INSTANCE2 ){
259996 rc = fts5VocabInstanceNewTerm(pCsr);
259997 }
259998 if( rc==SQLITE_OK0 && !pCsr->bEof
259999 && (eType!=FTS5_VOCAB_INSTANCE2
260000 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE1)
260001 ){
260002 rc = fts5VocabNextMethod(pCursor);
260003 }
260004
260005 return rc;
260006}
260007
260008/*
260009** This is the xEof method of the virtual table. SQLite calls this
260010** routine to find out if it has reached the end of a result set.
260011*/
260012static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
260013 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
260014 return pCsr->bEof;
260015}
260016
260017static int fts5VocabColumnMethod(
260018 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
260019 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
260020 int iCol /* Index of column to read value from */
260021){
260022 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
260023 int eDetail = pCsr->pFts5->pConfig->eDetail;
260024 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
260025 i64 iVal = 0;
260026
260027 if( iCol==0 ){
260028 sqlite3_result_text(
260029 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1)
260030 );
260031 }else if( eType==FTS5_VOCAB_COL0 ){
260032 assert( iCol==1 || iCol==2 || iCol==3 )((void) (0));
260033 if( iCol==1 ){
260034 if( eDetail!=FTS5_DETAIL_NONE1 ){
260035 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
260036 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
260037 }
260038 }else if( iCol==2 ){
260039 iVal = pCsr->aDoc[pCsr->iCol];
260040 }else{
260041 iVal = pCsr->aCnt[pCsr->iCol];
260042 }
260043 }else if( eType==FTS5_VOCAB_ROW1 ){
260044 assert( iCol==1 || iCol==2 )((void) (0));
260045 if( iCol==1 ){
260046 iVal = pCsr->aDoc[0];
260047 }else{
260048 iVal = pCsr->aCnt[0];
260049 }
260050 }else{
260051 assert( eType==FTS5_VOCAB_INSTANCE )((void) (0));
260052 switch( iCol ){
260053 case 1:
260054 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
260055 break;
260056 case 2: {
260057 int ii = -1;
260058 if( eDetail==FTS5_DETAIL_FULL0 ){
260059 ii = FTS5_POS2COLUMN(pCsr->iInstPos)(int)((pCsr->iInstPos >> 32) & 0x7FFFFFFF);
260060 }else if( eDetail==FTS5_DETAIL_COLUMNS2 ){
260061 ii = (int)pCsr->iInstPos;
260062 }
260063 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
260064 const char *z = pCsr->pFts5->pConfig->azCol[ii];
260065 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
260066 }
260067 break;
260068 }
260069 default: {
260070 assert( iCol==3 )((void) (0));
260071 if( eDetail==FTS5_DETAIL_FULL0 ){
260072 int ii = FTS5_POS2OFFSET(pCsr->iInstPos)(int)(pCsr->iInstPos & 0x7FFFFFFF);
260073 sqlite3_result_int(pCtx, ii);
260074 }
260075 break;
260076 }
260077 }
260078 }
260079
260080 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
260081 return SQLITE_OK0;
260082}
260083
260084/*
260085** This is the xRowid method. The SQLite core calls this routine to
260086** retrieve the rowid for the current row of the result set. The
260087** rowid should be written to *pRowid.
260088*/
260089static int fts5VocabRowidMethod(
260090 sqlite3_vtab_cursor *pCursor,
260091 sqlite_int64 *pRowid
260092){
260093 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
260094 *pRowid = pCsr->rowid;
260095 return SQLITE_OK0;
260096}
260097
260098static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
260099 static const sqlite3_module fts5Vocab = {
260100 /* iVersion */ 2,
260101 /* xCreate */ fts5VocabCreateMethod,
260102 /* xConnect */ fts5VocabConnectMethod,
260103 /* xBestIndex */ fts5VocabBestIndexMethod,
260104 /* xDisconnect */ fts5VocabDisconnectMethod,
260105 /* xDestroy */ fts5VocabDestroyMethod,
260106 /* xOpen */ fts5VocabOpenMethod,
260107 /* xClose */ fts5VocabCloseMethod,
260108 /* xFilter */ fts5VocabFilterMethod,
260109 /* xNext */ fts5VocabNextMethod,
260110 /* xEof */ fts5VocabEofMethod,
260111 /* xColumn */ fts5VocabColumnMethod,
260112 /* xRowid */ fts5VocabRowidMethod,
260113 /* xUpdate */ 0,
260114 /* xBegin */ 0,
260115 /* xSync */ 0,
260116 /* xCommit */ 0,
260117 /* xRollback */ 0,
260118 /* xFindFunction */ 0,
260119 /* xRename */ 0,
260120 /* xSavepoint */ 0,
260121 /* xRelease */ 0,
260122 /* xRollbackTo */ 0,
260123 /* xShadowName */ 0,
260124 /* xIntegrity */ 0
260125 };
260126 void *p = (void*)pGlobal;
260127
260128 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
260129}
260130
260131
260132/* Here ends the fts5.c composite file. */
260133#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
260134
260135/************** End of fts5.c ************************************************/
260136/************** Begin file stmt.c ********************************************/
260137/*
260138** 2017-05-31
260139**
260140** The author disclaims copyright to this source code. In place of
260141** a legal notice, here is a blessing:
260142**
260143** May you do good and not evil.
260144** May you find forgiveness for yourself and forgive others.
260145** May you share freely, never taking more than you give.
260146**
260147*************************************************************************
260148**
260149** This file demonstrates an eponymous virtual table that returns information
260150** about all prepared statements for the database connection.
260151**
260152** Usage example:
260153**
260154** .load ./stmt
260155** .mode line
260156** .header on
260157** SELECT * FROM stmt;
260158*/
260159#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_STMTVTAB)
260160#if !defined(SQLITEINT_H)
260161/* #include "sqlite3ext.h" */
260162#endif
260163SQLITE_EXTENSION_INIT1
260164/* #include <assert.h> */
260165/* #include <string.h> */
260166
260167#ifndef SQLITE_OMIT_VIRTUALTABLE
260168
260169
260170#define STMT_NUM_INTEGER_COLUMN 10
260171typedef struct StmtRow StmtRow;
260172struct StmtRow {
260173 sqlite3_int64 iRowid; /* Rowid value */
260174 char *zSql; /* column "sql" */
260175 int aCol[STMT_NUM_INTEGER_COLUMN+1]; /* all other column values */
260176 StmtRow *pNext; /* Next row to return */
260177};
260178
260179/* stmt_vtab is a subclass of sqlite3_vtab which will
260180** serve as the underlying representation of a stmt virtual table
260181*/
260182typedef struct stmt_vtab stmt_vtab;
260183struct stmt_vtab {
260184 sqlite3_vtab base; /* Base class - must be first */
260185 sqlite3 *db; /* Database connection for this stmt vtab */
260186};
260187
260188/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
260189** serve as the underlying representation of a cursor that scans
260190** over rows of the result
260191*/
260192typedef struct stmt_cursor stmt_cursor;
260193struct stmt_cursor {
260194 sqlite3_vtab_cursor base; /* Base class - must be first */
260195 sqlite3 *db; /* Database connection for this cursor */
260196 StmtRow *pRow; /* Current row */
260197};
260198
260199/*
260200** The stmtConnect() method is invoked to create a new
260201** stmt_vtab that describes the stmt virtual table.
260202**
260203** Think of this routine as the constructor for stmt_vtab objects.
260204**
260205** All this routine needs to do is:
260206**
260207** (1) Allocate the stmt_vtab object and initialize all fields.
260208**
260209** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
260210** result set of queries against stmt will look like.
260211*/
260212static int stmtConnect(
260213 sqlite3 *db,
260214 void *pAux,
260215 int argc, const char *const*argv,
260216 sqlite3_vtab **ppVtab,
260217 char **pzErr
260218){
260219 stmt_vtab *pNew;
260220 int rc;
260221
260222/* Column numbers */
260223#define STMT_COLUMN_SQL 0 /* SQL for the statement */
260224#define STMT_COLUMN_NCOL 1 /* Number of result columns */
260225#define STMT_COLUMN_RO 2 /* True if read-only */
260226#define STMT_COLUMN_BUSY 3 /* True if currently busy */
260227#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
260228#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
260229#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
260230#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
260231#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
260232#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
260233#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
260234
260235
260236 (void)pAux;
260237 (void)argc;
260238 (void)argv;
260239 (void)pzErr;
260240 rc = sqlite3_declare_vtab(db,
260241 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
260242 "reprep,run,mem)");
260243 if( rc==SQLITE_OK0 ){
260244 pNew = sqlite3_malloc64( sizeof(*pNew) );
260245 *ppVtab = (sqlite3_vtab*)pNew;
260246 if( pNew==0 ) return SQLITE_NOMEM7;
260247 memset(pNew, 0, sizeof(*pNew));
260248 pNew->db = db;
260249 }
260250 return rc;
260251}
260252
260253/*
260254** This method is the destructor for stmt_cursor objects.
260255*/
260256static int stmtDisconnect(sqlite3_vtab *pVtab){
260257 sqlite3_free(pVtab);
260258 return SQLITE_OK0;
260259}
260260
260261/*
260262** Constructor for a new stmt_cursor object.
260263*/
260264static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
260265 stmt_cursor *pCur;
260266 pCur = sqlite3_malloc64( sizeof(*pCur) );
260267 if( pCur==0 ) return SQLITE_NOMEM7;
260268 memset(pCur, 0, sizeof(*pCur));
260269 pCur->db = ((stmt_vtab*)p)->db;
260270 *ppCursor = &pCur->base;
260271 return SQLITE_OK0;
260272}
260273
260274static void stmtCsrReset(stmt_cursor *pCur){
260275 StmtRow *pRow = 0;
260276 StmtRow *pNext = 0;
260277 for(pRow=pCur->pRow; pRow; pRow=pNext){
260278 pNext = pRow->pNext;
260279 sqlite3_free(pRow);
260280 }
260281 pCur->pRow = 0;
260282}
260283
260284/*
260285** Destructor for a stmt_cursor.
260286*/
260287static int stmtClose(sqlite3_vtab_cursor *cur){
260288 stmtCsrReset((stmt_cursor*)cur);
260289 sqlite3_free(cur);
260290 return SQLITE_OK0;
260291}
260292
260293
260294/*
260295** Advance a stmt_cursor to its next row of output.
260296*/
260297static int stmtNext(sqlite3_vtab_cursor *cur){
260298 stmt_cursor *pCur = (stmt_cursor*)cur;
260299 StmtRow *pNext = pCur->pRow->pNext;
260300 sqlite3_free(pCur->pRow);
260301 pCur->pRow = pNext;
260302 return SQLITE_OK0;
260303}
260304
260305/*
260306** Return values of columns for the row at which the stmt_cursor
260307** is currently pointing.
260308*/
260309static int stmtColumn(
260310 sqlite3_vtab_cursor *cur, /* The cursor */
260311 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
260312 int i /* Which column to return */
260313){
260314 stmt_cursor *pCur = (stmt_cursor*)cur;
260315 StmtRow *pRow = pCur->pRow;
260316 if( i==STMT_COLUMN_SQL ){
260317 sqlite3_result_text(ctx, pRow->zSql, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
260318 }else{
260319 sqlite3_result_int(ctx, pRow->aCol[i]);
260320 }
260321 return SQLITE_OK0;
260322}
260323
260324/*
260325** Return the rowid for the current row. In this implementation, the
260326** rowid is the same as the output value.
260327*/
260328static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
260329 stmt_cursor *pCur = (stmt_cursor*)cur;
260330 *pRowid = pCur->pRow->iRowid;
260331 return SQLITE_OK0;
260332}
260333
260334/*
260335** Return TRUE if the cursor has been moved off of the last
260336** row of output.
260337*/
260338static int stmtEof(sqlite3_vtab_cursor *cur){
260339 stmt_cursor *pCur = (stmt_cursor*)cur;
260340 return pCur->pRow==0;
260341}
260342
260343/*
260344** This method is called to "rewind" the stmt_cursor object back
260345** to the first row of output. This method is always called at least
260346** once prior to any call to stmtColumn() or stmtRowid() or
260347** stmtEof().
260348*/
260349static int stmtFilter(
260350 sqlite3_vtab_cursor *pVtabCursor,
260351 int idxNum, const char *idxStr,
260352 int argc, sqlite3_value **argv
260353){
260354 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
260355 sqlite3_stmt *p = 0;
260356 sqlite3_int64 iRowid = 1;
260357 StmtRow **ppRow = 0;
260358
260359 (void)idxNum;
260360 (void)idxStr;
260361 (void)argc;
260362 (void)argv;
260363 stmtCsrReset(pCur);
260364 ppRow = &pCur->pRow;
260365 for(p=sqlite3_next_stmt(pCur->db, 0); p; p=sqlite3_next_stmt(pCur->db, p)){
260366 const char *zSql = sqlite3_sql(p);
260367 sqlite3_int64 nSql = zSql ? strlen(zSql)+1 : 0;
260368 StmtRow *pNew = (StmtRow*)sqlite3_malloc64(sizeof(StmtRow) + nSql);
260369
260370 if( pNew==0 ) return SQLITE_NOMEM7;
260371 memset(pNew, 0, sizeof(StmtRow));
260372 if( zSql ){
260373 pNew->zSql = (char*)&pNew[1];
260374 memcpy(pNew->zSql, zSql, nSql);
260375 }
260376 pNew->aCol[STMT_COLUMN_NCOL] = sqlite3_column_count(p);
260377 pNew->aCol[STMT_COLUMN_RO] = sqlite3_stmt_readonly(p);
260378 pNew->aCol[STMT_COLUMN_BUSY] = sqlite3_stmt_busy(p);
260379 pNew->aCol[STMT_COLUMN_NSCAN] = sqlite3_stmt_status(
260380 p, SQLITE_STMTSTATUS_FULLSCAN_STEP1, 0
260381 );
260382 pNew->aCol[STMT_COLUMN_NSORT] = sqlite3_stmt_status(
260383 p, SQLITE_STMTSTATUS_SORT2, 0
260384 );
260385 pNew->aCol[STMT_COLUMN_NAIDX] = sqlite3_stmt_status(
260386 p, SQLITE_STMTSTATUS_AUTOINDEX3, 0
260387 );
260388 pNew->aCol[STMT_COLUMN_NSTEP] = sqlite3_stmt_status(
260389 p, SQLITE_STMTSTATUS_VM_STEP4, 0
260390 );
260391 pNew->aCol[STMT_COLUMN_REPREP] = sqlite3_stmt_status(
260392 p, SQLITE_STMTSTATUS_REPREPARE5, 0
260393 );
260394 pNew->aCol[STMT_COLUMN_RUN] = sqlite3_stmt_status(
260395 p, SQLITE_STMTSTATUS_RUN6, 0
260396 );
260397 pNew->aCol[STMT_COLUMN_MEM] = sqlite3_stmt_status(
260398 p, SQLITE_STMTSTATUS_MEMUSED99, 0
260399 );
260400 pNew->iRowid = iRowid++;
260401 *ppRow = pNew;
260402 ppRow = &pNew->pNext;
260403 }
260404
260405 return SQLITE_OK0;
260406}
260407
260408/*
260409** SQLite will invoke this method one or more times while planning a query
260410** that uses the stmt virtual table. This routine needs to create
260411** a query plan for each invocation and compute an estimated cost for that
260412** plan.
260413*/
260414static int stmtBestIndex(
260415 sqlite3_vtab *tab,
260416 sqlite3_index_info *pIdxInfo
260417){
260418 (void)tab;
260419 pIdxInfo->estimatedCost = (double)500;
260420 pIdxInfo->estimatedRows = 500;
260421 return SQLITE_OK0;
260422}
260423
260424/*
260425** This following structure defines all the methods for the
260426** stmt virtual table.
260427*/
260428static sqlite3_module stmtModule = {
260429 0, /* iVersion */
260430 0, /* xCreate */
260431 stmtConnect, /* xConnect */
260432 stmtBestIndex, /* xBestIndex */
260433 stmtDisconnect, /* xDisconnect */
260434 0, /* xDestroy */
260435 stmtOpen, /* xOpen - open a cursor */
260436 stmtClose, /* xClose - close a cursor */
260437 stmtFilter, /* xFilter - configure scan constraints */
260438 stmtNext, /* xNext - advance a cursor */
260439 stmtEof, /* xEof - check for end of scan */
260440 stmtColumn, /* xColumn - read data */
260441 stmtRowid, /* xRowid - read data */
260442 0, /* xUpdate */
260443 0, /* xBegin */
260444 0, /* xSync */
260445 0, /* xCommit */
260446 0, /* xRollback */
260447 0, /* xFindMethod */
260448 0, /* xRename */
260449 0, /* xSavepoint */
260450 0, /* xRelease */
260451 0, /* xRollbackTo */
260452 0, /* xShadowName */
260453 0 /* xIntegrity */
260454};
260455
260456#endif /* SQLITE_OMIT_VIRTUALTABLE */
260457
260458SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3 *db){
260459 int rc = SQLITE_OK0;
260460#ifndef SQLITE_OMIT_VIRTUALTABLE
260461 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
260462#endif
260463 return rc;
260464}
260465
260466#ifndef SQLITE_CORE1
260467#ifdef _WIN32
260468__declspec(dllexport)
260469#endif
260470SQLITE_API int sqlite3_stmt_init(
260471 sqlite3 *db,
260472 char **pzErrMsg,
260473 const sqlite3_api_routines *pApi
260474){
260475 int rc = SQLITE_OK0;
260476 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
260477#ifndef SQLITE_OMIT_VIRTUALTABLE
260478 rc = sqlite3StmtVtabInit(db);
260479#endif
260480 return rc;
260481}
260482#endif /* SQLITE_CORE */
260483#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
260484
260485/************** End of stmt.c ************************************************/
260486/* Return the source-id for this library */
260487SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID"2024-11-25 12:07:48 b95d11e958643b969c47a8e5857f3793b9e69700b8f1469371386369a26e577e"; }
260488/************************** End of sqlite3.c ******************************/